Esempio n. 1
0
int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
		unsigned char *data, int max_data_bytes)
{
	void *silk_enc;
	CELTEncoder *celt_enc;
    int i;
	int ret=0;
	SKP_int32 nBytes;
	ec_enc enc;
	int framerate, period;
    int silk_internal_bandwidth=-1;
    int bytes_target;
    int prefill=0;
    int start_band = 0;
    int redundancy = 0;
    int redundancy_bytes = 0;
    int celt_to_silk = 0;
    /* TODO: This is 60 only so we can handle 60ms speech/audio switching 
       it shouldn't bee too hard to reduce to 20 ms if needed */
    short pcm_buf[60*48*2];
    int nb_compr_bytes;
    int to_celt = 0;
    celt_int32 mono_rate;

    silk_enc = (char*)st+st->silk_enc_offset;
    celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);

    if (st->user_bitrate_bps==OPUS_BITRATE_AUTO)
        st->bitrate_bps = 60*st->Fs/frame_size + st->Fs*st->channels;
    else
        st->bitrate_bps = st->user_bitrate_bps;

    /* Rate-dependent mono-stereo decision */
    if (st->mode == MODE_CELT_ONLY && st->channels == 2)
    {
        celt_int32 decision_rate;
        decision_rate = st->bitrate_bps + st->voice_ratio*st->voice_ratio;
        /* Add some hysteresis */
        if (st->stream_channels == 2)
            decision_rate += 4000;
        else
            decision_rate -= 4000;
        if (decision_rate>48000)
            st->stream_channels = 2;
        else
            st->stream_channels = 1;
    }
    /* Equivalent bit-rate for mono */
    mono_rate = st->bitrate_bps;
    if (st->stream_channels==2)
        mono_rate = (mono_rate+10000)/2;
    /* Compensate for smaller frame sizes assuming an equivalent overhead
       of 60 bits/frame */
    mono_rate -= 60*(st->Fs/frame_size - 50);

    /* Mode selection */
    if (st->user_mode==OPUS_MODE_AUTO)
    {
        celt_int32 decision_rate;
        /* SILK/CELT threshold is higher for voice than for music */
        decision_rate = mono_rate - 3*st->voice_ratio*st->voice_ratio;
        /* Hysteresis */
        if (st->prev_mode == MODE_CELT_ONLY)
            decision_rate += 4000;
        else if (st->prev_mode>0)
            decision_rate -= 4000;
        if (decision_rate>24000)
            st->mode = MODE_CELT_ONLY;
        else
            st->mode = MODE_SILK_ONLY;
    } else if (st->user_mode==OPUS_MODE_VOICE)
    {
        st->mode = MODE_SILK_ONLY;
    } else {/* OPUS_AUDIO_MODE */
        st->mode = MODE_CELT_ONLY;
    }

    /* Automatic (rate-dependent) bandwidth selection */
    if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
    {
    	const int *bandwidth_thresholds;
    	int bandwidth = BANDWIDTH_FULLBAND;

    	bandwidth_thresholds = st->mode == MODE_CELT_ONLY ? audio_bandwidth_thresholds : voice_bandwidth_thresholds;
    	do {
    		int threshold, hysteresis;
    		threshold = bandwidth_thresholds[2*(bandwidth-BANDWIDTH_MEDIUMBAND)];
    		hysteresis = bandwidth_thresholds[2*(bandwidth-BANDWIDTH_MEDIUMBAND)+1];
    		if (!st->first)
    		{
    			if (st->bandwidth >= bandwidth)
    				threshold -= hysteresis;
    			else
    				threshold += hysteresis;
    		}
    		if (mono_rate >= threshold)
    			break;
    	} while (--bandwidth>BANDWIDTH_NARROWBAND);
    	st->bandwidth = bandwidth;
    	/* Prevents any transition to SWB/FB until the SILK layer has fully
    	   switched to WB mode and turned the variable LP filter off */
    	if (st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > BANDWIDTH_WIDEBAND)
    		st->bandwidth = BANDWIDTH_WIDEBAND;
    }

    /* Prevents Opus from wasting bits on frequencies that are above
       the Nyquist rate of the input signal */
    if (st->Fs <= 24000 && st->bandwidth > BANDWIDTH_SUPERWIDEBAND)
    	st->bandwidth = BANDWIDTH_SUPERWIDEBAND;
    if (st->Fs <= 16000 && st->bandwidth > BANDWIDTH_WIDEBAND)
    	st->bandwidth = BANDWIDTH_WIDEBAND;
    if (st->Fs <= 12000 && st->bandwidth > BANDWIDTH_MEDIUMBAND)
    	st->bandwidth = BANDWIDTH_MEDIUMBAND;
    if (st->Fs <= 8000 && st->bandwidth > BANDWIDTH_NARROWBAND)
    	st->bandwidth = BANDWIDTH_NARROWBAND;

    if (st->user_bandwidth != BANDWIDTH_AUTO)
    	st->bandwidth = st->user_bandwidth;

    /* Prevents nonsensical configurations, i.e. modes that don't exist */
    if (frame_size < st->Fs/100 && st->mode != MODE_CELT_ONLY)
        st->mode = MODE_CELT_ONLY;
    if (frame_size > st->Fs/50 && st->mode != MODE_SILK_ONLY)
        st->mode = MODE_SILK_ONLY;
    if (st->mode == MODE_CELT_ONLY && st->bandwidth == BANDWIDTH_MEDIUMBAND)
        st->bandwidth = BANDWIDTH_WIDEBAND;
    if (st->mode == MODE_SILK_ONLY && st->bandwidth > BANDWIDTH_WIDEBAND)
        st->mode = MODE_HYBRID;
    if (st->mode == MODE_HYBRID && st->bandwidth <= BANDWIDTH_WIDEBAND)
        st->mode = MODE_SILK_ONLY;

	bytes_target = st->bitrate_bps * frame_size / (st->Fs * 8) - 1;

	data += 1;
	if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
	{
		silk_EncControlStruct dummy;
		silk_InitEncoder( st->silk_enc, &dummy);
		prefill=1;
	}
	if (st->prev_mode > 0 &&
	       ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
	        (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
	{
	    redundancy = 1;
	    celt_to_silk = (st->mode != MODE_CELT_ONLY);
	    if (!celt_to_silk)
	    {
	        /* Switch to SILK/hybrid if frame size is 10 ms or more*/
	        if (frame_size >= st->Fs/100)
	        {
		        st->mode = st->prev_mode;
		        to_celt = 1;
	        } else {
	        	redundancy=0;
	        }
	    }
	}

	ec_enc_init(&enc, data, max_data_bytes-1);

	/* SILK processing */
    if (st->mode != MODE_CELT_ONLY)
    {
        st->silk_mode.bitRate = st->bitrate_bps - 8*st->Fs/frame_size;
        if( st->mode == MODE_HYBRID ) {
            st->silk_mode.bitRate /= st->stream_channels;
            if( st->bandwidth == BANDWIDTH_SUPERWIDEBAND ) {
                if( st->Fs == 100 * frame_size ) {
                    /* 24 kHz, 10 ms */
                    st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 2000 + st->use_vbr * 1000 ) * 2 ) / 3;
                } else {
                    /* 24 kHz, 20 ms */
                    st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 1000 + st->use_vbr * 1000 ) * 2 ) / 3;
                }
            } else {
                if( st->Fs == 100 * frame_size ) {
                    /* 48 kHz, 10 ms */
                    st->silk_mode.bitRate = ( st->silk_mode.bitRate + 8000 + st->use_vbr * 3000 ) / 2;
                } else {
                    /* 48 kHz, 20 ms */
                    st->silk_mode.bitRate = ( st->silk_mode.bitRate + 9000 + st->use_vbr * 1000 ) / 2;
                }
            }
            st->silk_mode.bitRate *= st->stream_channels;
            /* don't let SILK use more than 80% */
            if( st->silk_mode.bitRate > ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5 ) {
                st->silk_mode.bitRate = ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5;
            }
        }

        st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
        st->silk_mode.nChannelsAPI = st->channels;
        st->silk_mode.nChannelsInternal = st->stream_channels;
        if (st->bandwidth == BANDWIDTH_NARROWBAND) {
        	st->silk_mode.desiredInternalSampleRate = 8000;
        } else if (st->bandwidth == BANDWIDTH_MEDIUMBAND) {
        	st->silk_mode.desiredInternalSampleRate = 12000;
        } else {
            SKP_assert( st->mode == MODE_HYBRID || st->bandwidth == BANDWIDTH_WIDEBAND );
            st->silk_mode.desiredInternalSampleRate = 16000;
        }
        if( st->mode == MODE_HYBRID ) {
            /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
            st->silk_mode.minInternalSampleRate = 16000;
        } else {
            st->silk_mode.minInternalSampleRate = 8000;
        }
        st->silk_mode.maxInternalSampleRate = 16000;

        /* Call SILK encoder for the low band */
        nBytes = max_data_bytes-1;
        if (prefill)
        {
            int zero=0;
        	silk_Encode( silk_enc, &st->silk_mode, st->delay_buffer, st->encoder_buffer, NULL, &zero, 1 );
        }

        ret = silk_Encode( silk_enc, &st->silk_mode, pcm, frame_size, &enc, &nBytes, 0 );
        if( ret ) {
            fprintf (stderr, "SILK encode error: %d\n", ret);
            /* Handle error */
        }
        if (nBytes==0)
            return 0;
        /* Extract SILK internal bandwidth for signaling in first byte */
        if( st->mode == MODE_SILK_ONLY ) {
            if( st->silk_mode.internalSampleRate == 8000 ) {
                silk_internal_bandwidth = BANDWIDTH_NARROWBAND;
            } else if( st->silk_mode.internalSampleRate == 12000 ) {
                silk_internal_bandwidth = BANDWIDTH_MEDIUMBAND;
            } else if( st->silk_mode.internalSampleRate == 16000 ) {
                silk_internal_bandwidth = BANDWIDTH_WIDEBAND;
            }
        } else {
            SKP_assert( st->silk_mode.internalSampleRate == 16000 );
        }
    }

    /* CELT processing */
	{
	    int endband=21;

	    switch(st->bandwidth)
	    {
	    case BANDWIDTH_NARROWBAND:
	    	endband = 13;
	    	break;
	    case BANDWIDTH_WIDEBAND:
	    	endband = 17;
	    	break;
	    case BANDWIDTH_SUPERWIDEBAND:
	    	endband = 19;
	    	break;
	    case BANDWIDTH_FULLBAND:
	    	endband = 21;
	    	break;
	    }
	    celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
	    celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
	}
	if (st->mode != MODE_SILK_ONLY)
	{
        celt_encoder_ctl(celt_enc, CELT_SET_VBR(0));
        celt_encoder_ctl(celt_enc, CELT_SET_BITRATE(510000));
        if (st->prev_mode == MODE_SILK_ONLY)
        {
        	unsigned char dummy[10];
        	celt_encoder_ctl(celt_enc, CELT_RESET_STATE);
        	celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
        	celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
        	/* TODO: This wastes CPU a bit compared to just prefilling the buffer */
        	celt_encode(celt_enc, &st->delay_buffer[(st->encoder_buffer-st->delay_compensation-st->Fs/400)*st->channels], st->Fs/400, dummy, 10);
        } else {
        	celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(2));
        }

        if (st->mode == MODE_HYBRID)
        {
            int len;

            len = (ec_tell(&enc)+7)>>3;
            if( st->use_vbr ) {
                nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * frame_size) / (8 * st->Fs);
            } else {
                /* check if SILK used up too much */
                nb_compr_bytes = len > bytes_target ? len : bytes_target;
            }
        } else {
Esempio n. 2
0
static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
                             opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
{
    void *silk_dec;
    CELTDecoder *celt_dec;
    int i, silk_ret=0, celt_ret=0;
    ec_dec dec;
    opus_int32 silk_frame_size;
    int pcm_silk_size;
    VARDECL(opus_int16, pcm_silk);
    int pcm_transition_silk_size;
    VARDECL(opus_val16, pcm_transition_silk);
    int pcm_transition_celt_size;
    VARDECL(opus_val16, pcm_transition_celt);
    opus_val16 *pcm_transition=NULL;
    int redundant_audio_size;
    VARDECL(opus_val16, redundant_audio);

    int audiosize;
    int mode;
    int transition=0;
    int start_band;
    int redundancy=0;
    int redundancy_bytes = 0;
    int celt_to_silk=0;
    int c;
    int F2_5, F5, F10, F20;
    const opus_val16 *window;
    opus_uint32 redundant_rng = 0;
    int celt_accum;
    ALLOC_STACK;

    silk_dec = (char*)st+st->silk_dec_offset;
    celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
    F20 = st->Fs/50;
    F10 = F20>>1;
    F5 = F10>>1;
    F2_5 = F5>>1;
    if (frame_size < F2_5)
    {
        RESTORE_STACK;
        return OPUS_BUFFER_TOO_SMALL;
    }
    /* Limit frame_size to avoid excessive stack allocations. */
    frame_size = IMIN(frame_size, st->Fs/25*3);
    /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */
    if (len<=1)
    {
        data = NULL;
        /* In that case, don't conceal more than what the ToC says */
        frame_size = IMIN(frame_size, st->frame_size);
    }
    if (data != NULL)
    {
        audiosize = st->frame_size;
        mode = st->mode;
        ec_dec_init(&dec,(unsigned char*)data,len);
    } else {
        audiosize = frame_size;
        mode = st->prev_mode;

        if (mode == 0)
        {
            /* If we haven't got any packet yet, all we can do is return zeros */
            for (i=0; i<audiosize*st->channels; i++)
                pcm[i] = 0;
            RESTORE_STACK;
            return audiosize;
        }

        /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT),
           10, or 20 (e.g. 12.5 or 30 ms). */
        if (audiosize > F20)
        {
            do {
                int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0);
                if (ret<0)
                {
                    RESTORE_STACK;
                    return ret;
                }
                pcm += ret*st->channels;
                audiosize -= ret;
            } while (audiosize > 0);
            RESTORE_STACK;
            return frame_size;
        } else if (audiosize < F20)
        {
            if (audiosize > F10)
                audiosize = F10;
            else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10)
                audiosize = F5;
        }
    }

    /* In fixed-point, we can tell CELT to do the accumulation on top of the
       SILK PCM buffer. This saves some stack space. */
#ifdef FIXED_POINT
    celt_accum = (mode != MODE_CELT_ONLY) && (frame_size >= F10);
#else
    celt_accum = 0;
#endif

    pcm_transition_silk_size = ALLOC_NONE;
    pcm_transition_celt_size = ALLOC_NONE;
    if (data!=NULL && st->prev_mode > 0 && (
                (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy)
                || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) )
       )
    {
        transition = 1;
        /* Decide where to allocate the stack memory for pcm_transition */
        if (mode == MODE_CELT_ONLY)
            pcm_transition_celt_size = F5*st->channels;
        else
            pcm_transition_silk_size = F5*st->channels;
    }
    ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_val16);
    if (transition && mode == MODE_CELT_ONLY)
    {
        pcm_transition = pcm_transition_celt;
        opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
    }
    if (audiosize > frame_size)
    {
        /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/
        RESTORE_STACK;
        return OPUS_BAD_ARG;
    } else {
        frame_size = audiosize;
    }

    /* Don't allocate any memory when in CELT-only mode */
    pcm_silk_size = (mode != MODE_CELT_ONLY && !celt_accum) ? IMAX(F10, frame_size)*st->channels : ALLOC_NONE;
    ALLOC(pcm_silk, pcm_silk_size, opus_int16);

    /* SILK processing */
    if (mode != MODE_CELT_ONLY)
    {
        int lost_flag, decoded_samples;
        opus_int16 *pcm_ptr;
#ifdef FIXED_POINT
        if (celt_accum)
            pcm_ptr = pcm;
        else
#endif
            pcm_ptr = pcm_silk;

        if (st->prev_mode==MODE_CELT_ONLY)
            silk_InitDecoder( silk_dec );

        /* The SILK PLC cannot produce frames of less than 10 ms */
        st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs);

        if (data != NULL)
        {
            st->DecControl.nChannelsInternal = st->stream_channels;
            if( mode == MODE_SILK_ONLY ) {
                if( st->bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
                    st->DecControl.internalSampleRate = 8000;
                } else if( st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
                    st->DecControl.internalSampleRate = 12000;
                } else if( st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
                    st->DecControl.internalSampleRate = 16000;
                } else {
                    st->DecControl.internalSampleRate = 16000;
                    silk_assert( 0 );
                }
            } else {
                /* Hybrid mode */
                st->DecControl.internalSampleRate = 16000;
            }
        }

        lost_flag = data == NULL ? 1 : 2 * decode_fec;
        decoded_samples = 0;
        do {
            /* Call SILK decoder */
            int first_frame = decoded_samples == 0;
            silk_ret = silk_Decode( silk_dec, &st->DecControl,
                                    lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size );
            if( silk_ret ) {
                if (lost_flag) {
                    /* PLC failure should not be fatal */
                    silk_frame_size = frame_size;
                    for (i=0; i<frame_size*st->channels; i++)
                        pcm_ptr[i] = 0;
                } else {
                    RESTORE_STACK;
                    return OPUS_INTERNAL_ERROR;
                }
            }
            pcm_ptr += silk_frame_size * st->channels;
            decoded_samples += silk_frame_size;
        } while( decoded_samples < frame_size );
    }

    start_band = 0;
    if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL
            && ec_tell(&dec)+17+20*(st->mode == MODE_HYBRID) <= 8*len)
    {
        /* Check if we have a redundant 0-8 kHz band */
        if (mode == MODE_HYBRID)
            redundancy = ec_dec_bit_logp(&dec, 12);
        else
            redundancy = 1;
        if (redundancy)
        {
            celt_to_silk = ec_dec_bit_logp(&dec, 1);
            /* redundancy_bytes will be at least two, in the non-hybrid
               case due to the ec_tell() check above */
            redundancy_bytes = mode==MODE_HYBRID ?
                               (opus_int32)ec_dec_uint(&dec, 256)+2 :
                               len-((ec_tell(&dec)+7)>>3);
            len -= redundancy_bytes;
            /* This is a sanity check. It should never happen for a valid
               packet, so the exact behaviour is not normative. */
            if (len*8 < ec_tell(&dec))
            {
                len = 0;
                redundancy_bytes = 0;
                redundancy = 0;
            }
            /* Shrink decoder because of raw bits */
            dec.storage -= redundancy_bytes;
        }
    }
    if (mode != MODE_CELT_ONLY)
        start_band = 17;

    {
        int endband=21;

        switch(st->bandwidth)
        {
        case OPUS_BANDWIDTH_NARROWBAND:
            endband = 13;
            break;
        case OPUS_BANDWIDTH_MEDIUMBAND:
        case OPUS_BANDWIDTH_WIDEBAND:
            endband = 17;
            break;
        case OPUS_BANDWIDTH_SUPERWIDEBAND:
            endband = 19;
            break;
        case OPUS_BANDWIDTH_FULLBAND:
            endband = 21;
            break;
        }
        celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband));
        celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels));
    }

    if (redundancy)
    {
        transition = 0;
        pcm_transition_silk_size=ALLOC_NONE;
    }

    ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16);

    if (transition && mode != MODE_CELT_ONLY)
    {
        pcm_transition = pcm_transition_silk;
        opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
    }

    /* Only allocation memory for redundancy if/when needed */
    redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE;
    ALLOC(redundant_audio, redundant_audio_size, opus_val16);

    /* 5 ms redundant frame for CELT->SILK*/
    if (redundancy && celt_to_silk)
    {
        celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
        celt_decode_with_ec(celt_dec, data+len, redundancy_bytes,
                            redundant_audio, F5, NULL, 0);
        celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
    }

    /* MUST be after PLC */
    celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band));

    if (mode != MODE_SILK_ONLY)
    {
        int celt_frame_size = IMIN(F20, frame_size);
        /* Make sure to discard any previous CELT state */
        if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy)
            celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
        /* Decode CELT */
        celt_ret = celt_decode_with_ec(celt_dec, decode_fec ? NULL : data,
                                       len, pcm, celt_frame_size, &dec, celt_accum);
    } else {
        unsigned char silence[2] = {0xFF, 0xFF};
        if (!celt_accum)
        {
            for (i=0; i<frame_size*st->channels; i++)
                pcm[i] = 0;
        }
        /* For hybrid -> SILK transitions, we let the CELT MDCT
           do a fade-out by decoding a silence frame */
        if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) )
        {
            celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
            celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum);
        }
    }

    if (mode != MODE_CELT_ONLY && !celt_accum)
    {
#ifdef FIXED_POINT
        for (i=0; i<frame_size*st->channels; i++)
            pcm[i] = SAT16(ADD32(pcm[i], pcm_silk[i]));
#else
        for (i=0; i<frame_size*st->channels; i++)
            pcm[i] = pcm[i] + (opus_val16)((1.f/32768.f)*pcm_silk[i]);
#endif
    }

    {
        const CELTMode *celt_mode;
        celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode));
        window = celt_mode->window;
    }

    /* 5 ms redundant frame for SILK->CELT */
    if (redundancy && !celt_to_silk)
    {
        celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
        celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));

        celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0);
        celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
        smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
                    pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
    }
    if (redundancy && celt_to_silk)
    {
        for (c=0; c<st->channels; c++)
        {
            for (i=0; i<F2_5; i++)
                pcm[st->channels*i+c] = redundant_audio[st->channels*i+c];
        }
        smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5,
                    pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs);
    }
    if (transition)
    {
        if (audiosize >= F5)
        {
            for (i=0; i<st->channels*F2_5; i++)
                pcm[i] = pcm_transition[i];
            smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
                        pcm+st->channels*F2_5, F2_5,
                        st->channels, window, st->Fs);
        } else {
            /* Not enough time to do a clean transition, but we do it anyway
               This will not preserve amplitude perfectly and may introduce
               a bit of temporal aliasing, but it shouldn't be too bad and
               that's pretty much the best we can do. In any case, generating this
               transition it pretty silly in the first place */
            smooth_fade(pcm_transition, pcm,
                        pcm, F2_5,
                        st->channels, window, st->Fs);
        }
    }

    if(st->decode_gain)
    {
        opus_val32 gain;
        gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain));
        for (i=0; i<frame_size*st->channels; i++)
        {
            opus_val32 x;
            x = MULT16_32_P16(pcm[i],gain);
            pcm[i] = SATURATE(x, 32767);
        }
    }

    if (len <= 1)
        st->rangeFinal = 0;
    else
        st->rangeFinal = dec.rng ^ redundant_rng;

    st->prev_mode = mode;
    st->prev_redundancy = redundancy && !celt_to_silk;

    if (celt_ret>=0)
    {
        if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels))
            OPUS_PRINT_INT(audiosize);
    }

    RESTORE_STACK;
    return celt_ret < 0 ? celt_ret : audiosize;

}
Esempio n. 3
0
static gboolean
gst_celt_enc_setup (GstCeltEnc * enc)
{
  gint error = CELT_OK;

#ifdef HAVE_CELT_0_7
  enc->mode = celt_mode_create (enc->rate, enc->frame_size, &error);
#else
  enc->mode =
      celt_mode_create (enc->rate, enc->channels, enc->frame_size, &error);
#endif
  if (!enc->mode)
    goto mode_initialization_failed;

#ifdef HAVE_CELT_0_11
  celt_header_init (&enc->header, enc->mode, enc->frame_size, enc->channels);
#else
#ifdef HAVE_CELT_0_7
  celt_header_init (&enc->header, enc->mode, enc->channels);
#else
  celt_header_init (&enc->header, enc->mode);
#endif
#endif
  enc->header.nb_channels = enc->channels;

#ifdef HAVE_CELT_0_8
  enc->frame_size = enc->header.frame_size;
#else
  celt_mode_info (enc->mode, CELT_GET_FRAME_SIZE, &enc->frame_size);
#endif

#ifdef HAVE_CELT_0_11
  enc->state = celt_encoder_create_custom (enc->mode, enc->channels, &error);
#else
#ifdef HAVE_CELT_0_7
  enc->state = celt_encoder_create (enc->mode, enc->channels, &error);
#else
  enc->state = celt_encoder_create (enc->mode);
#endif
#endif
  if (!enc->state)
    goto encoder_creation_failed;

#ifdef CELT_SET_VBR_RATE
  if (!enc->cbr) {
    celt_encoder_ctl (enc->state, CELT_SET_VBR_RATE (enc->bitrate / 1000), 0);
  }
#endif
#ifdef CELT_SET_COMPLEXITY
  celt_encoder_ctl (enc->state, CELT_SET_COMPLEXITY (enc->complexity), 0);
#endif
#ifdef CELT_SET_PREDICTION
  celt_encoder_ctl (enc->state, CELT_SET_PREDICTION (enc->prediction), 0);
#endif
#ifdef CELT_SET_START_BAND
  celt_encoder_ctl (enc->state, CELT_SET_START_BAND (enc->start_band), 0);
#endif

  GST_LOG_OBJECT (enc, "we have frame size %d", enc->frame_size);

  return TRUE;

mode_initialization_failed:
  GST_ERROR_OBJECT (enc, "Mode initialization failed: %d", error);
  return FALSE;

encoder_creation_failed:
#ifdef HAVE_CELT_0_7
  GST_ERROR_OBJECT (enc, "Encoder creation failed: %d", error);
#else
  GST_ERROR_OBJECT (enc, "Encoder creation failed");
#endif
  return FALSE;
}