From 6008abea50f28f4e89a46c357952e2253072c494 Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Wed, 6 Aug 2008 11:30:45 -0400 Subject: Making it possible to disable either the encoder or the decoder using macros --- libspeex/cb_search.c | 11 +- libspeex/filters.c | 33 +- libspeex/lpc.c | 3 + libspeex/lsp.c | 2 + libspeex/ltp.c | 10 +- libspeex/mdf.c | 2 +- libspeex/modes.c | 43 +- libspeex/modes_wb.c | 54 +- libspeex/nb_celp.c | 759 ++++++++++++++-------------- libspeex/quant_lsp.c | 29 +- libspeex/sb_celp.c | 1374 ++++++++++++++++++++++++-------------------------- libspeex/stereo.c | 7 + libspeex/vq.c | 12 +- 13 files changed, 1201 insertions(+), 1138 deletions(-) (limited to 'libspeex') diff --git a/libspeex/cb_search.c b/libspeex/cb_search.c index 63f4c6a..fa96dac 100644 --- a/libspeex/cb_search.c +++ b/libspeex/cb_search.c @@ -49,6 +49,8 @@ #include "cb_search_bfin.h" #endif +#ifndef DISABLE_ENCODER + #ifndef OVERRIDE_COMPUTE_WEIGHTED_CODEBOOK static void compute_weighted_codebook(const signed char *shape_cb, const spx_word16_t *r, spx_word16_t *resp, spx_word16_t *resp2, spx_word32_t *E, int shape_cb_size, int subvect_size, char *stack) { @@ -509,8 +511,9 @@ int update_target target[j]=SUB16(target[j],PSHR16(r2[j],2)); } } +#endif /* DISABLE_ENCODER */ - +#ifndef DISABLE_DECODER void split_cb_shape_sign_unquant( spx_sig_t *exc, const void *par, /* non-overlapping codebook */ @@ -568,7 +571,9 @@ spx_int32_t *seed #endif } } +#endif /* DISABLE_DECODER */ +#ifndef DISABLE_ENCODER void noise_codebook_quant( spx_word16_t target[], /* target vector */ spx_coef_t ak[], /* LPCs for this subframe */ @@ -594,8 +599,9 @@ int update_target exc[i]+=SHL32(EXTEND32(tmp[i]),8); SPEEX_MEMSET(target, 0, nsf); } +#endif /* DISABLE_ENCODER */ - +#ifndef DISABLE_DECODER void noise_codebook_unquant( spx_sig_t *exc, const void *par, /* non-overlapping codebook */ @@ -610,3 +616,4 @@ spx_int32_t *seed for (i=0;iframeSize; + break; + case SPEEX_SET_LOW_MODE: + case SPEEX_SET_MODE: + st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_LOW_MODE: + case SPEEX_GET_MODE: + (*(spx_int32_t*)ptr) = st->submodeID; + break; +#ifndef DISABLE_VBR + case SPEEX_SET_VBR: + st->vbr_enabled = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_VBR: + (*(spx_int32_t*)ptr) = st->vbr_enabled; + break; + case SPEEX_SET_VAD: + st->vad_enabled = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_VAD: + (*(spx_int32_t*)ptr) = st->vad_enabled; + break; + case SPEEX_SET_DTX: + st->dtx_enabled = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_DTX: + (*(spx_int32_t*)ptr) = st->dtx_enabled; + break; + case SPEEX_SET_ABR: + st->abr_enabled = (*(spx_int32_t*)ptr); + st->vbr_enabled = st->abr_enabled!=0; + if (st->vbr_enabled) + { + spx_int32_t i=10; + spx_int32_t rate, target; + float vbr_qual; + target = (*(spx_int32_t*)ptr); + while (i>=0) + { + speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i); + speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate); + if (rate <= target) + break; + i--; + } + vbr_qual=i; + if (vbr_qual<0) + vbr_qual=0; + speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual); + st->abr_count=0; + st->abr_drift=0; + st->abr_drift2=0; + } + + break; + case SPEEX_GET_ABR: + (*(spx_int32_t*)ptr) = st->abr_enabled; + break; +#endif /* #ifndef DISABLE_VBR */ +#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) + case SPEEX_SET_VBR_QUALITY: + st->vbr_quality = (*(float*)ptr); + break; + case SPEEX_GET_VBR_QUALITY: + (*(float*)ptr) = st->vbr_quality; + break; +#endif /* !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */ + case SPEEX_SET_QUALITY: + { + int quality = (*(spx_int32_t*)ptr); + if (quality < 0) + quality = 0; + if (quality > 10) + quality = 10; + st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality]; + } + break; + case SPEEX_SET_COMPLEXITY: + st->complexity = (*(spx_int32_t*)ptr); + if (st->complexity<0) + st->complexity=0; + break; + case SPEEX_GET_COMPLEXITY: + (*(spx_int32_t*)ptr) = st->complexity; + break; + case SPEEX_SET_BITRATE: + { + spx_int32_t i=10; + spx_int32_t rate, target; + target = (*(spx_int32_t*)ptr); + while (i>=0) + { + speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i); + speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate); + if (rate <= target) + break; + i--; + } + } + break; + case SPEEX_GET_BITRATE: + if (st->submodes[st->submodeID]) + (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize; + else + (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize; + break; + case SPEEX_SET_SAMPLING_RATE: + st->sampling_rate = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_SAMPLING_RATE: + (*(spx_int32_t*)ptr)=st->sampling_rate; + break; + case SPEEX_RESET_STATE: + { + int i; + st->bounded_pitch = 1; + st->first = 1; + for (i=0;ilpcSize;i++) + st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1); + for (i=0;ilpcSize;i++) + st->mem_sw[i]=st->mem_sw_whole[i]=st->mem_sp[i]=st->mem_exc[i]=0; + for (i=0;iframeSize+st->max_pitch+1;i++) + st->excBuf[i]=st->swBuf[i]=0; + for (i=0;iwindowSize-st->frameSize;i++) + st->winBuf[i]=0; + } + break; + case SPEEX_SET_SUBMODE_ENCODING: + st->encode_submode = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_SUBMODE_ENCODING: + (*(spx_int32_t*)ptr) = st->encode_submode; + break; + case SPEEX_GET_LOOKAHEAD: + (*(spx_int32_t*)ptr)=(st->windowSize-st->frameSize); + break; + case SPEEX_SET_PLC_TUNING: + st->plc_tuning = (*(spx_int32_t*)ptr); + if (st->plc_tuning>100) + st->plc_tuning=100; + break; + case SPEEX_GET_PLC_TUNING: + (*(spx_int32_t*)ptr)=(st->plc_tuning); + break; +#ifndef DISABLE_VBR + case SPEEX_SET_VBR_MAX_BITRATE: + st->vbr_max = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_VBR_MAX_BITRATE: + (*(spx_int32_t*)ptr) = st->vbr_max; + break; +#endif /* #ifndef DISABLE_VBR */ + case SPEEX_SET_HIGHPASS: + st->highpass_enabled = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_HIGHPASS: + (*(spx_int32_t*)ptr) = st->highpass_enabled; + break; + + /* This is all internal stuff past this point */ + case SPEEX_GET_PI_GAIN: + { + int i; + spx_word32_t *g = (spx_word32_t*)ptr; + for (i=0;inbSubframes;i++) + g[i]=st->pi_gain[i]; + } + break; + case SPEEX_GET_EXC: + { + int i; + for (i=0;inbSubframes;i++) + ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*st->subframeSize, st->subframeSize); + } + break; +#ifndef DISABLE_VBR + case SPEEX_GET_RELATIVE_QUALITY: + (*(float*)ptr)=st->relative_quality; + break; +#endif /* #ifndef DISABLE_VBR */ + case SPEEX_SET_INNOVATION_SAVE: + st->innov_rms_save = (spx_word16_t*)ptr; + break; + case SPEEX_SET_WIDEBAND: + st->isWideband = *((spx_int32_t*)ptr); + break; + case SPEEX_GET_STACK: + *((char**)ptr) = st->stack; + break; + default: + speex_warning_int("Unknown nb_ctl request: ", request); + return -1; + } + return 0; +} + + int nb_encode(void *state, void *vin, SpeexBits *bits) { EncState *st; @@ -837,10 +1042,10 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) } for (i=0;isubframeSize;i++) exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767)); +#ifndef DISABLE_WIDEBAND if (st->innov_rms_save) - { st->innov_rms_save[sub] = compute_rms(innov, st->subframeSize); - } +#endif } /* Final signal synthesis from excitation */ @@ -887,7 +1092,10 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) return 1; } +#endif /* DISABLE_ENCODER */ + +#ifndef DISABLE_DECODER void *nb_decoder_init(const SpeexMode *m) { DecState *st; @@ -963,51 +1171,177 @@ void nb_decoder_destroy(void *state) speex_free(state); } -#define median3(a, b, c) ((a) < (b) ? ((b) < (c) ? (b) : ((a) < (c) ? (c) : (a))) : ((c) < (b) ? (b) : ((c) < (a) ? (c) : (a)))) - -#ifdef FIXED_POINT -const spx_word16_t attenuation[10] = {32767, 31483, 27923, 22861, 17278, 12055, 7764, 4616, 2533, 1283}; -#else -const spx_word16_t attenuation[10] = {1., 0.961, 0.852, 0.698, 0.527, 0.368, 0.237, 0.141, 0.077, 0.039}; - -#endif - -static void nb_decode_lost(DecState *st, spx_word16_t *out, char *stack) +int nb_decoder_ctl(void *state, int request, void *ptr) { - int i; - int pitch_val; - spx_word16_t pitch_gain; - spx_word16_t fact; - spx_word16_t gain_med; - spx_word16_t innov_gain; - spx_word16_t noise_gain; - - st->exc = st->excBuf + 2*st->max_pitch + st->subframeSize + 6; - - if (st->count_lost<10) - fact = attenuation[st->count_lost]; - else - fact = 0; - - gain_med = median3(st->pitch_gain_buf[0], st->pitch_gain_buf[1], st->pitch_gain_buf[2]); - if (gain_med < st->last_pitch_gain) - st->last_pitch_gain = gain_med; - -#ifdef FIXED_POINT - pitch_gain = st->last_pitch_gain; - if (pitch_gain>54) - pitch_gain = 54; - pitch_gain = SHL16(pitch_gain, 9); -#else - pitch_gain = GAIN_SCALING_1*st->last_pitch_gain; - if (pitch_gain>.85) - pitch_gain=.85; -#endif - pitch_gain = MULT16_16_Q15(fact,pitch_gain) + VERY_SMALL; - /* FIXME: This was rms of innovation (not exc) */ - innov_gain = compute_rms16(st->exc, st->frameSize); - noise_gain = MULT16_16_Q15(innov_gain, MULT16_16_Q15(fact, SUB16(Q15ONE,MULT16_16_Q15(pitch_gain,pitch_gain)))); - /* Shift all buffers by one frame */ + DecState *st; + st=(DecState*)state; + switch(request) + { + case SPEEX_SET_LOW_MODE: + case SPEEX_SET_MODE: + st->submodeID = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_LOW_MODE: + case SPEEX_GET_MODE: + (*(spx_int32_t*)ptr) = st->submodeID; + break; + case SPEEX_SET_ENH: + st->lpc_enh_enabled = *((spx_int32_t*)ptr); + break; + case SPEEX_GET_ENH: + *((spx_int32_t*)ptr) = st->lpc_enh_enabled; + break; + case SPEEX_GET_FRAME_SIZE: + (*(spx_int32_t*)ptr) = st->frameSize; + break; + case SPEEX_GET_BITRATE: + if (st->submodes[st->submodeID]) + (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize; + else + (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize; + break; + case SPEEX_SET_SAMPLING_RATE: + st->sampling_rate = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_SAMPLING_RATE: + (*(spx_int32_t*)ptr)=st->sampling_rate; + break; + case SPEEX_SET_HANDLER: + { + SpeexCallback *c = (SpeexCallback*)ptr; + st->speex_callbacks[c->callback_id].func=c->func; + st->speex_callbacks[c->callback_id].data=c->data; + st->speex_callbacks[c->callback_id].callback_id=c->callback_id; + } + break; + case SPEEX_SET_USER_HANDLER: + { + SpeexCallback *c = (SpeexCallback*)ptr; + st->user_callback.func=c->func; + st->user_callback.data=c->data; + st->user_callback.callback_id=c->callback_id; + } + break; + case SPEEX_RESET_STATE: + { + int i; + for (i=0;ilpcSize;i++) + st->mem_sp[i]=0; + for (i=0;iframeSize + st->max_pitch + 1;i++) + st->excBuf[i]=0; + } + break; + case SPEEX_SET_SUBMODE_ENCODING: + st->encode_submode = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_SUBMODE_ENCODING: + (*(spx_int32_t*)ptr) = st->encode_submode; + break; + case SPEEX_GET_LOOKAHEAD: + (*(spx_int32_t*)ptr)=st->subframeSize; + break; + case SPEEX_SET_HIGHPASS: + st->highpass_enabled = (*(spx_int32_t*)ptr); + break; + case SPEEX_GET_HIGHPASS: + (*(spx_int32_t*)ptr) = st->highpass_enabled; + break; + /* FIXME: Convert to fixed-point and re-enable even when float API is disabled */ +#ifndef DISABLE_FLOAT_API + case SPEEX_GET_ACTIVITY: + { + float ret; + ret = log(st->level/st->min_level)/log(st->max_level/st->min_level); + if (ret>1) + ret = 1; + /* Done in a strange way to catch NaNs as well */ + if (!(ret > 0)) + ret = 0; + /*printf ("%f %f %f %f\n", st->level, st->min_level, st->max_level, ret);*/ + (*(spx_int32_t*)ptr) = (int)(100*ret); + } + break; +#endif + case SPEEX_GET_PI_GAIN: + { + int i; + spx_word32_t *g = (spx_word32_t*)ptr; + for (i=0;inbSubframes;i++) + g[i]=st->pi_gain[i]; + } + break; + case SPEEX_GET_EXC: + { + int i; + for (i=0;inbSubframes;i++) + ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*st->subframeSize, st->subframeSize); + } + break; + case SPEEX_GET_DTX_STATUS: + *((spx_int32_t*)ptr) = st->dtx_enabled; + break; + case SPEEX_SET_INNOVATION_SAVE: + st->innov_save = (spx_word16_t*)ptr; + break; + case SPEEX_SET_WIDEBAND: + st->isWideband = *((spx_int32_t*)ptr); + break; + case SPEEX_GET_STACK: + *((char**)ptr) = st->stack; + break; + default: + speex_warning_int("Unknown nb_ctl request: ", request); + return -1; + } + return 0; +} + + +#define median3(a, b, c) ((a) < (b) ? ((b) < (c) ? (b) : ((a) < (c) ? (c) : (a))) : ((c) < (b) ? (b) : ((c) < (a) ? (c) : (a)))) + +#ifdef FIXED_POINT +const spx_word16_t attenuation[10] = {32767, 31483, 27923, 22861, 17278, 12055, 7764, 4616, 2533, 1283}; +#else +const spx_word16_t attenuation[10] = {1., 0.961, 0.852, 0.698, 0.527, 0.368, 0.237, 0.141, 0.077, 0.039}; + +#endif + +static void nb_decode_lost(DecState *st, spx_word16_t *out, char *stack) +{ + int i; + int pitch_val; + spx_word16_t pitch_gain; + spx_word16_t fact; + spx_word16_t gain_med; + spx_word16_t innov_gain; + spx_word16_t noise_gain; + + st->exc = st->excBuf + 2*st->max_pitch + st->subframeSize + 6; + + if (st->count_lost<10) + fact = attenuation[st->count_lost]; + else + fact = 0; + + gain_med = median3(st->pitch_gain_buf[0], st->pitch_gain_buf[1], st->pitch_gain_buf[2]); + if (gain_med < st->last_pitch_gain) + st->last_pitch_gain = gain_med; + +#ifdef FIXED_POINT + pitch_gain = st->last_pitch_gain; + if (pitch_gain>54) + pitch_gain = 54; + pitch_gain = SHL16(pitch_gain, 9); +#else + pitch_gain = GAIN_SCALING_1*st->last_pitch_gain; + if (pitch_gain>.85) + pitch_gain=.85; +#endif + pitch_gain = MULT16_16_Q15(fact,pitch_gain) + VERY_SMALL; + /* FIXME: This was rms of innovation (not exc) */ + innov_gain = compute_rms16(st->exc, st->frameSize); + noise_gain = MULT16_16_Q15(innov_gain, MULT16_16_Q15(fact, SUB16(Q15ONE,MULT16_16_Q15(pitch_gain,pitch_gain)))); + /* Shift all buffers by one frame */ SPEEX_MOVE(st->excBuf, st->excBuf+st->frameSize, 2*st->max_pitch + st->subframeSize + 12); @@ -1531,332 +1865,5 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) return 0; } +#endif /* DISABLE_DECODER */ -int nb_encoder_ctl(void *state, int request, void *ptr) -{ - EncState *st; - st=(EncState*)state; - switch(request) - { - case SPEEX_GET_FRAME_SIZE: - (*(spx_int32_t*)ptr) = st->frameSize; - break; - case SPEEX_SET_LOW_MODE: - case SPEEX_SET_MODE: - st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_LOW_MODE: - case SPEEX_GET_MODE: - (*(spx_int32_t*)ptr) = st->submodeID; - break; -#ifndef DISABLE_VBR - case SPEEX_SET_VBR: - st->vbr_enabled = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_VBR: - (*(spx_int32_t*)ptr) = st->vbr_enabled; - break; - case SPEEX_SET_VAD: - st->vad_enabled = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_VAD: - (*(spx_int32_t*)ptr) = st->vad_enabled; - break; - case SPEEX_SET_DTX: - st->dtx_enabled = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_DTX: - (*(spx_int32_t*)ptr) = st->dtx_enabled; - break; - case SPEEX_SET_ABR: - st->abr_enabled = (*(spx_int32_t*)ptr); - st->vbr_enabled = st->abr_enabled!=0; - if (st->vbr_enabled) - { - spx_int32_t i=10; - spx_int32_t rate, target; - float vbr_qual; - target = (*(spx_int32_t*)ptr); - while (i>=0) - { - speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i); - speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate); - if (rate <= target) - break; - i--; - } - vbr_qual=i; - if (vbr_qual<0) - vbr_qual=0; - speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual); - st->abr_count=0; - st->abr_drift=0; - st->abr_drift2=0; - } - - break; - case SPEEX_GET_ABR: - (*(spx_int32_t*)ptr) = st->abr_enabled; - break; -#endif /* #ifndef DISABLE_VBR */ -#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) - case SPEEX_SET_VBR_QUALITY: - st->vbr_quality = (*(float*)ptr); - break; - case SPEEX_GET_VBR_QUALITY: - (*(float*)ptr) = st->vbr_quality; - break; -#endif /* !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */ - case SPEEX_SET_QUALITY: - { - int quality = (*(spx_int32_t*)ptr); - if (quality < 0) - quality = 0; - if (quality > 10) - quality = 10; - st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality]; - } - break; - case SPEEX_SET_COMPLEXITY: - st->complexity = (*(spx_int32_t*)ptr); - if (st->complexity<0) - st->complexity=0; - break; - case SPEEX_GET_COMPLEXITY: - (*(spx_int32_t*)ptr) = st->complexity; - break; - case SPEEX_SET_BITRATE: - { - spx_int32_t i=10; - spx_int32_t rate, target; - target = (*(spx_int32_t*)ptr); - while (i>=0) - { - speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i); - speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate); - if (rate <= target) - break; - i--; - } - } - break; - case SPEEX_GET_BITRATE: - if (st->submodes[st->submodeID]) - (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize; - else - (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize; - break; - case SPEEX_SET_SAMPLING_RATE: - st->sampling_rate = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_SAMPLING_RATE: - (*(spx_int32_t*)ptr)=st->sampling_rate; - break; - case SPEEX_RESET_STATE: - { - int i; - st->bounded_pitch = 1; - st->first = 1; - for (i=0;ilpcSize;i++) - st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1); - for (i=0;ilpcSize;i++) - st->mem_sw[i]=st->mem_sw_whole[i]=st->mem_sp[i]=st->mem_exc[i]=0; - for (i=0;iframeSize+st->max_pitch+1;i++) - st->excBuf[i]=st->swBuf[i]=0; - for (i=0;iwindowSize-st->frameSize;i++) - st->winBuf[i]=0; - } - break; - case SPEEX_SET_SUBMODE_ENCODING: - st->encode_submode = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_SUBMODE_ENCODING: - (*(spx_int32_t*)ptr) = st->encode_submode; - break; - case SPEEX_GET_LOOKAHEAD: - (*(spx_int32_t*)ptr)=(st->windowSize-st->frameSize); - break; - case SPEEX_SET_PLC_TUNING: - st->plc_tuning = (*(spx_int32_t*)ptr); - if (st->plc_tuning>100) - st->plc_tuning=100; - break; - case SPEEX_GET_PLC_TUNING: - (*(spx_int32_t*)ptr)=(st->plc_tuning); - break; -#ifndef DISABLE_VBR - case SPEEX_SET_VBR_MAX_BITRATE: - st->vbr_max = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_VBR_MAX_BITRATE: - (*(spx_int32_t*)ptr) = st->vbr_max; - break; -#endif /* #ifndef DISABLE_VBR */ - case SPEEX_SET_HIGHPASS: - st->highpass_enabled = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_HIGHPASS: - (*(spx_int32_t*)ptr) = st->highpass_enabled; - break; - - /* This is all internal stuff past this point */ - case SPEEX_GET_PI_GAIN: - { - int i; - spx_word32_t *g = (spx_word32_t*)ptr; - for (i=0;inbSubframes;i++) - g[i]=st->pi_gain[i]; - } - break; - case SPEEX_GET_EXC: - { - int i; - for (i=0;inbSubframes;i++) - ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*st->subframeSize, st->subframeSize); - } - break; -#ifndef DISABLE_VBR - case SPEEX_GET_RELATIVE_QUALITY: - (*(float*)ptr)=st->relative_quality; - break; -#endif /* #ifndef DISABLE_VBR */ - case SPEEX_SET_INNOVATION_SAVE: - st->innov_rms_save = (spx_word16_t*)ptr; - break; - case SPEEX_SET_WIDEBAND: - st->isWideband = *((spx_int32_t*)ptr); - break; - case SPEEX_GET_STACK: - *((char**)ptr) = st->stack; - break; - default: - speex_warning_int("Unknown nb_ctl request: ", request); - return -1; - } - return 0; -} - -int nb_decoder_ctl(void *state, int request, void *ptr) -{ - DecState *st; - st=(DecState*)state; - switch(request) - { - case SPEEX_SET_LOW_MODE: - case SPEEX_SET_MODE: - st->submodeID = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_LOW_MODE: - case SPEEX_GET_MODE: - (*(spx_int32_t*)ptr) = st->submodeID; - break; - case SPEEX_SET_ENH: - st->lpc_enh_enabled = *((spx_int32_t*)ptr); - break; - case SPEEX_GET_ENH: - *((spx_int32_t*)ptr) = st->lpc_enh_enabled; - break; - case SPEEX_GET_FRAME_SIZE: - (*(spx_int32_t*)ptr) = st->frameSize; - break; - case SPEEX_GET_BITRATE: - if (st->submodes[st->submodeID]) - (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize; - else - (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize; - break; - case SPEEX_SET_SAMPLING_RATE: - st->sampling_rate = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_SAMPLING_RATE: - (*(spx_int32_t*)ptr)=st->sampling_rate; - break; - case SPEEX_SET_HANDLER: - { - SpeexCallback *c = (SpeexCallback*)ptr; - st->speex_callbacks[c->callback_id].func=c->func; - st->speex_callbacks[c->callback_id].data=c->data; - st->speex_callbacks[c->callback_id].callback_id=c->callback_id; - } - break; - case SPEEX_SET_USER_HANDLER: - { - SpeexCallback *c = (SpeexCallback*)ptr; - st->user_callback.func=c->func; - st->user_callback.data=c->data; - st->user_callback.callback_id=c->callback_id; - } - break; - case SPEEX_RESET_STATE: - { - int i; - for (i=0;ilpcSize;i++) - st->mem_sp[i]=0; - for (i=0;iframeSize + st->max_pitch + 1;i++) - st->excBuf[i]=0; - } - break; - case SPEEX_SET_SUBMODE_ENCODING: - st->encode_submode = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_SUBMODE_ENCODING: - (*(spx_int32_t*)ptr) = st->encode_submode; - break; - case SPEEX_GET_LOOKAHEAD: - (*(spx_int32_t*)ptr)=st->subframeSize; - break; - case SPEEX_SET_HIGHPASS: - st->highpass_enabled = (*(spx_int32_t*)ptr); - break; - case SPEEX_GET_HIGHPASS: - (*(spx_int32_t*)ptr) = st->highpass_enabled; - break; - /* FIXME: Convert to fixed-point and re-enable even when float API is disabled */ -#ifndef DISABLE_FLOAT_API - case SPEEX_GET_ACTIVITY: - { - float ret; - ret = log(st->level/st->min_level)/log(st->max_level/st->min_level); - if (ret>1) - ret = 1; - /* Done in a strange way to catch NaNs as well */ - if (!(ret > 0)) - ret = 0; - /*printf ("%f %f %f %f\n", st->level, st->min_level, st->max_level, ret);*/ - (*(spx_int32_t*)ptr) = (int)(100*ret); - } - break; -#endif - case SPEEX_GET_PI_GAIN: - { - int i; - spx_word32_t *g = (spx_word32_t*)ptr; - for (i=0;inbSubframes;i++) - g[i]=st->pi_gain[i]; - } - break; - case SPEEX_GET_EXC: - { - int i; - for (i=0;inbSubframes;i++) - ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*st->subframeSize, st->subframeSize); - } - break; - case SPEEX_GET_DTX_STATUS: - *((spx_int32_t*)ptr) = st->dtx_enabled; - break; - case SPEEX_SET_INNOVATION_SAVE: - st->innov_save = (spx_word16_t*)ptr; - break; - case SPEEX_SET_WIDEBAND: - st->isWideband = *((spx_int32_t*)ptr); - break; - case SPEEX_GET_STACK: - *((char**)ptr) = st->stack; - break; - default: - speex_warning_int("Unknown nb_ctl request: ", request); - return -1; - } - return 0; -} diff --git a/libspeex/quant_lsp.c b/libspeex/quant_lsp.c index e624d1a..699716b 100644 --- a/libspeex/quant_lsp.c +++ b/libspeex/quant_lsp.c @@ -68,6 +68,7 @@ #endif +#ifndef DISABLE_ENCODER static void compute_quant_weights(spx_lsp_t *qlsp, spx_word16_t *quant_weight, int order) { int i; @@ -209,7 +210,9 @@ void lsp_quant_nb(spx_lsp_t *lsp, spx_lsp_t *qlsp, int order, SpeexBits *bits) for (i=0;istack; - mode = (const SpeexSBMode*)(st->mode->mode); - low = in; - high = in+st->frame_size; - - /* High-band buffering / sync with low band */ - /* Compute the two sub-bands by filtering with QMF h0*/ - qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack); - -#ifndef DISABLE_VBR - if (st->vbr_enabled || st->vad_enabled) + st=(SBEncState*)state; + switch(request) { - /* Need to compute things here before the signal is trashed by the encoder */ - /*FIXME: Are the two signals (low, high) in sync? */ - e_low = compute_rms16(low, st->frame_size); - e_high = compute_rms16(high, st->frame_size); - } + case SPEEX_GET_FRAME_SIZE: + (*(spx_int32_t*)ptr) = st->full_frame_size; + break; + case SPEEX_SET_HIGH_MODE: + st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr); + break; + case SPEEX_SET_LOW_MODE: + speex_encoder_ctl(st->st_low, SPEEX_SET_LOW_MODE, ptr); + break; + case SPEEX_SET_DTX: + speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr); + break; + case SPEEX_GET_DTX: + speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr); + break; + case SPEEX_GET_LOW_MODE: + speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr); + break; + case SPEEX_SET_MODE: + speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr); + break; +#ifndef DISABLE_VBR + case SPEEX_SET_VBR: + st->vbr_enabled = (*(spx_int32_t*)ptr); + speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr); + break; + case SPEEX_GET_VBR: + (*(spx_int32_t*)ptr) = st->vbr_enabled; + break; + case SPEEX_SET_VAD: + st->vad_enabled = (*(spx_int32_t*)ptr); + speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr); + break; + case SPEEX_GET_VAD: + (*(spx_int32_t*)ptr) = st->vad_enabled; + break; #endif /* #ifndef DISABLE_VBR */ - - ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t); - speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms); - /* Encode the narrowband part*/ - speex_encode_native(st->st_low, low, bits); - - high = high - (st->windowSize-st->frame_size); - SPEEX_COPY(high, st->high, st->windowSize-st->frame_size); - SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size); - - - ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t); - ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t); - speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain); - speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms); - - speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx); - - if (dtx==0) - dtx=1; - else - dtx=0; - - ALLOC(lpc, st->lpcSize, spx_coef_t); - ALLOC(interp_lpc, st->lpcSize, spx_coef_t); - ALLOC(bw_lpc1, st->lpcSize, spx_coef_t); - ALLOC(bw_lpc2, st->lpcSize, spx_coef_t); - - ALLOC(lsp, st->lpcSize, spx_lsp_t); - ALLOC(qlsp, st->lpcSize, spx_lsp_t); - ALLOC(interp_lsp, st->lpcSize, spx_lsp_t); - ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t); - - { - VARDECL(spx_word16_t *autocorr); - VARDECL(spx_word16_t *w_sig); - ALLOC(autocorr, st->lpcSize+1, spx_word16_t); - ALLOC(w_sig, st->windowSize, spx_word16_t); - /* Window for analysis */ - /* FIXME: This is a kludge */ - if (st->subframeSize==80) +#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) + case SPEEX_SET_VBR_QUALITY: { - for (i=0;iwindowSize;i++) - w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT)); - } else { - for (i=0;iwindowSize;i++) - w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT)); - } - /* Compute auto-correlation */ - _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize); - autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */ - - /* Lag windowing: equivalent to filtering in the power-spectrum domain */ - for (i=0;ilpcSize+1;i++) - autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]); - - /* Levinson-Durbin */ - _spx_lpc(lpc, autocorr, st->lpcSize); - } - - /* LPC to LSPs (x-domain) transform */ - roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack); - if (roots!=st->lpcSize) - { - roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack); - if (roots!=st->lpcSize) { - /*If we can't find all LSP's, do some damage control and use a flat filter*/ - for (i=0;ilpcSize;i++) - { - lsp[i]=st->old_lsp[i]; - } + spx_int32_t q; + float qual = (*(float*)ptr)+.6; + st->vbr_quality = (*(float*)ptr); + if (qual>10) + qual=10; + q=(int)floor(.5+*(float*)ptr); + if (q>10) + q=10; + speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual); + speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q); + break; } - } - + case SPEEX_GET_VBR_QUALITY: + (*(float*)ptr) = st->vbr_quality; + break; +#endif /* #if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */ #ifndef DISABLE_VBR - /* VBR code */ - if ((st->vbr_enabled || st->vad_enabled) && !dtx) - { - float ratio; - if (st->abr_enabled) + case SPEEX_SET_ABR: + st->abr_enabled = (*(spx_int32_t*)ptr); + st->vbr_enabled = st->abr_enabled!=0; + speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled); + if (st->vbr_enabled) { - float qual_change=0; - if (st->abr_drift2 * st->abr_drift > 0) + spx_int32_t i=10, rate, target; + float vbr_qual; + target = (*(spx_int32_t*)ptr); + while (i>=0) { - /* Only adapt if long-term and short-term drift are the same sign */ - qual_change = -.00001*st->abr_drift/(1+st->abr_count); - if (qual_change>.1) - qual_change=.1; - if (qual_change<-.1) - qual_change=-.1; + speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i); + speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate); + if (rate <= target) + break; + i--; } - st->vbr_quality += qual_change; - if (st->vbr_quality>10) - st->vbr_quality=10; - if (st->vbr_quality<0) - st->vbr_quality=0; + vbr_qual=i; + if (vbr_qual<0) + vbr_qual=0; + speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual); + st->abr_count=0; + st->abr_drift=0; + st->abr_drift2=0; } - - - ratio = 2*log((1.f+e_high)/(1.f+e_low)); - speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality); - if (ratio<-4) - ratio=-4; - if (ratio>2) - ratio=2; - /*if (ratio>-2)*/ - if (st->vbr_enabled) + break; + case SPEEX_GET_ABR: + (*(spx_int32_t*)ptr) = st->abr_enabled; + break; +#endif /* #ifndef DISABLE_VBR */ + + case SPEEX_SET_QUALITY: { - spx_int32_t modeid; - modeid = mode->nb_modes-1; - st->relative_quality+=1.0*(ratio+2); - if (st->relative_quality<-1) - st->relative_quality=-1; - while (modeid) + spx_int32_t nb_qual; + int quality = (*(spx_int32_t*)ptr); + if (quality < 0) + quality = 0; + if (quality > 10) + quality = 10; + st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality]; + nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality]; + speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual); + } + break; + case SPEEX_SET_COMPLEXITY: + speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr); + st->complexity = (*(spx_int32_t*)ptr); + if (st->complexity<1) + st->complexity=1; + break; + case SPEEX_GET_COMPLEXITY: + (*(spx_int32_t*)ptr) = st->complexity; + break; + case SPEEX_SET_BITRATE: + { + spx_int32_t i=10; + spx_int32_t rate, target; + target = (*(spx_int32_t*)ptr); + while (i>=0) { - int v1; - float thresh; - v1=(int)floor(st->vbr_quality); - if (v1==10) - thresh = mode->vbr_thresh[modeid][v1]; - else - thresh = (st->vbr_quality-v1) * mode->vbr_thresh[modeid][v1+1] + - (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1]; - if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high) + speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i); + speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate); + if (rate <= target) break; - modeid--; + i--; } - speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid); - if (st->abr_enabled) + } + break; + case SPEEX_GET_BITRATE: + speex_encoder_ctl(st->st_low, request, ptr); + /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/ + if (st->submodes[st->submodeID]) + (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size; + else + (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size; + /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/ + break; + case SPEEX_SET_SAMPLING_RATE: + { + spx_int32_t tmp=(*(spx_int32_t*)ptr); + st->sampling_rate = tmp; + tmp>>=1; + speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp); + } + break; + case SPEEX_GET_SAMPLING_RATE: + (*(spx_int32_t*)ptr)=st->sampling_rate; + break; + case SPEEX_RESET_STATE: + { + int i; + st->first = 1; + for (i=0;ilpcSize;i++) + st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1); + for (i=0;ilpcSize;i++) + st->mem_sw[i]=st->mem_sp[i]=st->mem_sp2[i]=0; + for (i=0;ih0_mem[i]=st->h1_mem[i]=0; + } + break; + case SPEEX_SET_SUBMODE_ENCODING: + st->encode_submode = (*(spx_int32_t*)ptr); + speex_encoder_ctl(st->st_low, SPEEX_SET_SUBMODE_ENCODING, ptr); + break; + case SPEEX_GET_SUBMODE_ENCODING: + (*(spx_int32_t*)ptr) = st->encode_submode; + break; + case SPEEX_GET_LOOKAHEAD: + speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr); + (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1; + break; + case SPEEX_SET_PLC_TUNING: + speex_encoder_ctl(st->st_low, SPEEX_SET_PLC_TUNING, ptr); + break; + case SPEEX_GET_PLC_TUNING: + speex_encoder_ctl(st->st_low, SPEEX_GET_PLC_TUNING, ptr); + break; +#ifndef DISABLE_VBR + case SPEEX_SET_VBR_MAX_BITRATE: + { + st->vbr_max = (*(spx_int32_t*)ptr); + if (SPEEX_SET_VBR_MAX_BITRATE<1) { - spx_int32_t bitrate; - speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate); - st->abr_drift+=(bitrate-st->abr_enabled); - st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled); - st->abr_count += 1.0; + speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &st->vbr_max); + st->vbr_max_high = 17600; + } else { + spx_int32_t low_rate; + if (st->vbr_max >= 42200) + { + st->vbr_max_high = 17600; + } else if (st->vbr_max >= 27800) + { + st->vbr_max_high = 9600; + } else if (st->vbr_max > 20600) + { + st->vbr_max_high = 5600; + } else { + st->vbr_max_high = 1800; + } + if (st->subframeSize==80) + st->vbr_max_high = 1800; + low_rate = st->vbr_max - st->vbr_max_high; + speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate); } + } + break; + case SPEEX_GET_VBR_MAX_BITRATE: + (*(spx_int32_t*)ptr) = st->vbr_max; + break; +#endif /* #ifndef DISABLE_VBR */ + case SPEEX_SET_HIGHPASS: + speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr); + break; + case SPEEX_GET_HIGHPASS: + speex_encoder_ctl(st->st_low, SPEEX_GET_HIGHPASS, ptr); + break; - } else { - /* VAD only */ - int modeid; - if (st->relative_quality<2.0) - modeid=1; - else - modeid=st->submodeSelect; - /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/ - st->submodeID=modeid; + /* This is all internal stuff past this point */ + case SPEEX_GET_PI_GAIN: + { + int i; + spx_word32_t *g = (spx_word32_t*)ptr; + for (i=0;inbSubframes;i++) + g[i]=st->pi_gain[i]; } - /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/ - } + break; + case SPEEX_GET_EXC: + { + int i; + for (i=0;inbSubframes;i++) + ((spx_word16_t*)ptr)[i] = st->exc_rms[i]; + } + break; +#ifndef DISABLE_VBR + case SPEEX_GET_RELATIVE_QUALITY: + (*(float*)ptr)=st->relative_quality; + break; #endif /* #ifndef DISABLE_VBR */ - - if (st->encode_submode) - { - speex_bits_pack(bits, 1, 1); - if (dtx) - speex_bits_pack(bits, 0, SB_SUBMODE_BITS); - else - speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS); + case SPEEX_SET_INNOVATION_SAVE: + st->innov_rms_save = (spx_word16_t*)ptr; + break; + case SPEEX_SET_WIDEBAND: + speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr); + break; + case SPEEX_GET_STACK: + *((char**)ptr) = st->stack; + break; + default: + speex_warning_int("Unknown nb_ctl request: ", request); + return -1; } + return 0; +} - /* If null mode (no transmission), just set a couple things to zero*/ - if (dtx || st->submodes[st->submodeID] == NULL) - { - for (i=0;iframe_size;i++) - high[i]=VERY_SMALL; - - for (i=0;ilpcSize;i++) - st->mem_sw[i]=0; - st->first=1; - - /* Final signal synthesis from excitation */ - iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack); - if (dtx) - return 0; - else - return 1; +int sb_encode(void *state, void *vin, SpeexBits *bits) +{ + SBEncState *st; + int i, roots, sub; + char *stack; + VARDECL(spx_mem_t *mem); + VARDECL(spx_sig_t *innov); + VARDECL(spx_word16_t *target); + VARDECL(spx_word16_t *syn_resp); + VARDECL(spx_word32_t *low_pi_gain); + spx_word16_t *low; + spx_word16_t *high; + VARDECL(spx_word16_t *low_exc_rms); + VARDECL(spx_word16_t *low_innov_rms); + const SpeexSBMode *mode; + spx_int32_t dtx; + spx_word16_t *in = (spx_word16_t*)vin; + spx_word16_t e_low=0, e_high=0; + VARDECL(spx_coef_t *lpc); + VARDECL(spx_coef_t *interp_lpc); + VARDECL(spx_coef_t *bw_lpc1); + VARDECL(spx_coef_t *bw_lpc2); + VARDECL(spx_lsp_t *lsp); + VARDECL(spx_lsp_t *qlsp); + VARDECL(spx_lsp_t *interp_lsp); + VARDECL(spx_lsp_t *interp_qlsp); + + st = (SBEncState*)state; + stack=st->stack; + mode = (const SpeexSBMode*)(st->mode->mode); + low = in; + high = in+st->frame_size; + + /* High-band buffering / sync with low band */ + /* Compute the two sub-bands by filtering with QMF h0*/ + qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack); + +#ifndef DISABLE_VBR + if (st->vbr_enabled || st->vad_enabled) + { + /* Need to compute things here before the signal is trashed by the encoder */ + /*FIXME: Are the two signals (low, high) in sync? */ + e_low = compute_rms16(low, st->frame_size); + e_high = compute_rms16(high, st->frame_size); } +#endif /* #ifndef DISABLE_VBR */ + ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t); + speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms); + /* Encode the narrowband part*/ + speex_encode_native(st->st_low, low, bits); - /* LSP quantization */ + high = high - (st->windowSize-st->frame_size); + SPEEX_COPY(high, st->high, st->windowSize-st->frame_size); + SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size); + + + ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t); + ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t); + speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain); + speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms); + + speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx); + + if (dtx==0) + dtx=1; + else + dtx=0; + + ALLOC(lpc, st->lpcSize, spx_coef_t); + ALLOC(interp_lpc, st->lpcSize, spx_coef_t); + ALLOC(bw_lpc1, st->lpcSize, spx_coef_t); + ALLOC(bw_lpc2, st->lpcSize, spx_coef_t); + + ALLOC(lsp, st->lpcSize, spx_lsp_t); + ALLOC(qlsp, st->lpcSize, spx_lsp_t); + ALLOC(interp_lsp, st->lpcSize, spx_lsp_t); + ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t); + + { + VARDECL(spx_word16_t *autocorr); + VARDECL(spx_word16_t *w_sig); + ALLOC(autocorr, st->lpcSize+1, spx_word16_t); + ALLOC(w_sig, st->windowSize, spx_word16_t); + /* Window for analysis */ + /* FIXME: This is a kludge */ + if (st->subframeSize==80) + { + for (i=0;iwindowSize;i++) + w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT)); + } else { + for (i=0;iwindowSize;i++) + w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT)); + } + /* Compute auto-correlation */ + _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize); + autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */ + + /* Lag windowing: equivalent to filtering in the power-spectrum domain */ + for (i=0;ilpcSize+1;i++) + autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]); + + /* Levinson-Durbin */ + _spx_lpc(lpc, autocorr, st->lpcSize); + } + + /* LPC to LSPs (x-domain) transform */ + roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack); + if (roots!=st->lpcSize) + { + roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack); + if (roots!=st->lpcSize) { + /*If we can't find all LSP's, do some damage control and use a flat filter*/ + for (i=0;ilpcSize;i++) + { + lsp[i]=st->old_lsp[i]; + } + } + } + +#ifndef DISABLE_VBR + /* VBR code */ + if ((st->vbr_enabled || st->vad_enabled) && !dtx) + { + float ratio; + if (st->abr_enabled) + { + float qual_change=0; + if (st->abr_drift2 * st->abr_drift > 0) + { + /* Only adapt if long-term and short-term drift are the same sign */ + qual_change = -.00001*st->abr_drift/(1+st->abr_count); + if (qual_change>.1) + qual_change=.1; + if (qual_change<-.1) + qual_change=-.1; + } + st->vbr_quality += qual_change; + if (st->vbr_quality>10) + st->vbr_quality=10; + if (st->vbr_quality<0) + st->vbr_quality=0; + } + + + ratio = 2*log((1.f+e_high)/(1.f+e_low)); + + speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality); + if (ratio<-4) + ratio=-4; + if (ratio>2) + ratio=2; + /*if (ratio>-2)*/ + if (st->vbr_enabled) + { + spx_int32_t modeid; + modeid = mode->nb_modes-1; + st->relative_quality+=1.0*(ratio+2); + if (st->relative_quality<-1) + st->relative_quality=-1; + while (modeid) + { + int v1; + float thresh; + v1=(int)floor(st->vbr_quality); + if (v1==10) + thresh = mode->vbr_thresh[modeid][v1]; + else + thresh = (st->vbr_quality-v1) * mode->vbr_thresh[modeid][v1+1] + + (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1]; + if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high) + break; + modeid--; + } + speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid); + if (st->abr_enabled) + { + spx_int32_t bitrate; + speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate); + st->abr_drift+=(bitrate-st->abr_enabled); + st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled); + st->abr_count += 1.0; + } + + } else { + /* VAD only */ + int modeid; + if (st->relative_quality<2.0) + modeid=1; + else + modeid=st->submodeSelect; + /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/ + st->submodeID=modeid; + + } + /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/ + } +#endif /* #ifndef DISABLE_VBR */ + + if (st->encode_submode) + { + speex_bits_pack(bits, 1, 1); + if (dtx) + speex_bits_pack(bits, 0, SB_SUBMODE_BITS); + else + speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS); + } + + /* If null mode (no transmission), just set a couple things to zero*/ + if (dtx || st->submodes[st->submodeID] == NULL) + { + for (i=0;iframe_size;i++) + high[i]=VERY_SMALL; + + for (i=0;ilpcSize;i++) + st->mem_sw[i]=0; + st->first=1; + + /* Final signal synthesis from excitation */ + iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack); + + if (dtx) + return 0; + else + return 1; + } + + + /* LSP quantization */ SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits); if (st->first) @@ -748,10 +968,10 @@ int sb_encode(void *state, void *vin, SpeexBits *bits) return 1; } +#endif /* DISABLE_ENCODER */ - - +#ifndef DISABLE_DECODER void *sb_decoder_init(const SpeexMode *m) { spx_int32_t tmp; @@ -832,85 +1052,215 @@ void sb_decoder_destroy(void *state) speex_free(state); } -static void sb_decode_lost(SBDecState *st, spx_word16_t *out, int dtx, char *stack) -{ - int i; - int saved_modeid=0; - - if (dtx) - { - saved_modeid=st->submodeID; - st->submodeID=1; - } else { - bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize); - } - - st->first=1; - - - /* Final signal synthesis from excitation */ - if (!dtx) - { - st->last_ener = MULT16_16_Q15(QCONST16(.9f,15),st->last_ener); - } - for (i=0;iframe_size;i++) - out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed); - - iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize, - st->mem_sp, stack); - - - /* Reconstruct the original */ - qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack); - if (dtx) - { - st->submodeID=saved_modeid; - } - return; -} -int sb_decode(void *state, SpeexBits *bits, void *vout) +int sb_decoder_ctl(void *state, int request, void *ptr) { - int i, sub; SBDecState *st; - int wideband; - int ret; - char *stack; - VARDECL(spx_word32_t *low_pi_gain); - VARDECL(spx_word16_t *low_exc_rms); - VARDECL(spx_coef_t *ak); - VARDECL(spx_lsp_t *qlsp); - VARDECL(spx_lsp_t *interp_qlsp); - spx_int32_t dtx; - const SpeexSBMode *mode; - spx_word16_t *out = (spx_word16_t*)vout; - spx_word16_t *low_innov_alias; - spx_word32_t exc_ener_sum = 0; - - st = (SBDecState*)state; - stack=st->stack; - mode = (const SpeexSBMode*)(st->mode->mode); - - low_innov_alias = out+st->frame_size; - speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias); - /* Decode the low-band */ - ret = speex_decode_native(st->st_low, bits, out); - - speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx); - - /* If error decoding the narrowband part, propagate error */ - if (ret!=0) - { - return ret; - } - - if (!bits) + st=(SBDecState*)state; + switch(request) { - sb_decode_lost(st, out, dtx, stack); - return 0; - } - + case SPEEX_SET_HIGH_MODE: + st->submodeID = (*(spx_int32_t*)ptr); + break; + case SPEEX_SET_LOW_MODE: + speex_decoder_ctl(st->st_low, SPEEX_SET_LOW_MODE, ptr); + break; + case SPEEX_GET_LOW_MODE: + speex_decoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr); + break; + case SPEEX_GET_FRAME_SIZE: + (*(spx_int32_t*)ptr) = st->full_frame_size; + break; + case SPEEX_SET_ENH: + speex_decoder_ctl(st->st_low, request, ptr); + st->lpc_enh_enabled = *((spx_int32_t*)ptr); + break; + case SPEEX_GET_ENH: + *((spx_int32_t*)ptr) = st->lpc_enh_enabled; + break; + case SPEEX_SET_MODE: + case SPEEX_SET_QUALITY: + { + spx_int32_t nb_qual; + int quality = (*(spx_int32_t*)ptr); + if (quality < 0) + quality = 0; + if (quality > 10) + quality = 10; + st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality]; + nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality]; + speex_decoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual); + } + break; + case SPEEX_GET_BITRATE: + speex_decoder_ctl(st->st_low, request, ptr); + if (st->submodes[st->submodeID]) + (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size; + else + (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size; + break; + case SPEEX_SET_SAMPLING_RATE: + { + spx_int32_t tmp=(*(spx_int32_t*)ptr); + st->sampling_rate = tmp; + tmp>>=1; + speex_decoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp); + } + break; + case SPEEX_GET_SAMPLING_RATE: + (*(spx_int32_t*)ptr)=st->sampling_rate; + break; + case SPEEX_SET_HANDLER: + speex_decoder_ctl(st->st_low, SPEEX_SET_HANDLER, ptr); + break; + case SPEEX_SET_USER_HANDLER: + speex_decoder_ctl(st->st_low, SPEEX_SET_USER_HANDLER, ptr); + break; + case SPEEX_RESET_STATE: + { + int i; + for (i=0;i<2*st->lpcSize;i++) + st->mem_sp[i]=0; + for (i=0;ig0_mem[i]=st->g1_mem[i]=0; + st->last_ener=0; + } + break; + case SPEEX_SET_SUBMODE_ENCODING: + st->encode_submode = (*(spx_int32_t*)ptr); + speex_decoder_ctl(st->st_low, SPEEX_SET_SUBMODE_ENCODING, ptr); + break; + case SPEEX_GET_SUBMODE_ENCODING: + (*(spx_int32_t*)ptr) = st->encode_submode; + break; + case SPEEX_GET_LOOKAHEAD: + speex_decoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr); + (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr); + break; + case SPEEX_SET_HIGHPASS: + speex_decoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr); + break; + case SPEEX_GET_HIGHPASS: + speex_decoder_ctl(st->st_low, SPEEX_GET_HIGHPASS, ptr); + break; + case SPEEX_GET_ACTIVITY: + speex_decoder_ctl(st->st_low, SPEEX_GET_ACTIVITY, ptr); + break; + case SPEEX_GET_PI_GAIN: + { + int i; + spx_word32_t *g = (spx_word32_t*)ptr; + for (i=0;inbSubframes;i++) + g[i]=st->pi_gain[i]; + } + break; + case SPEEX_GET_EXC: + { + int i; + for (i=0;inbSubframes;i++) + ((spx_word16_t*)ptr)[i] = st->exc_rms[i]; + } + break; + case SPEEX_GET_DTX_STATUS: + speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, ptr); + break; + case SPEEX_SET_INNOVATION_SAVE: + st->innov_save = (spx_word16_t*)ptr; + break; + case SPEEX_SET_WIDEBAND: + speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr); + break; + case SPEEX_GET_STACK: + *((char**)ptr) = st->stack; + break; + default: + speex_warning_int("Unknown nb_ctl request: ", request); + return -1; + } + return 0; +} + + +static void sb_decode_lost(SBDecState *st, spx_word16_t *out, int dtx, char *stack) +{ + int i; + int saved_modeid=0; + + if (dtx) + { + saved_modeid=st->submodeID; + st->submodeID=1; + } else { + bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize); + } + + st->first=1; + + + /* Final signal synthesis from excitation */ + if (!dtx) + { + st->last_ener = MULT16_16_Q15(QCONST16(.9f,15),st->last_ener); + } + for (i=0;iframe_size;i++) + out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed); + + iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize, + st->mem_sp, stack); + + + /* Reconstruct the original */ + qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack); + if (dtx) + { + st->submodeID=saved_modeid; + } + + return; +} + +int sb_decode(void *state, SpeexBits *bits, void *vout) +{ + int i, sub; + SBDecState *st; + int wideband; + int ret; + char *stack; + VARDECL(spx_word32_t *low_pi_gain); + VARDECL(spx_word16_t *low_exc_rms); + VARDECL(spx_coef_t *ak); + VARDECL(spx_lsp_t *qlsp); + VARDECL(spx_lsp_t *interp_qlsp); + spx_int32_t dtx; + const SpeexSBMode *mode; + spx_word16_t *out = (spx_word16_t*)vout; + spx_word16_t *low_innov_alias; + spx_word32_t exc_ener_sum = 0; + + st = (SBDecState*)state; + stack=st->stack; + mode = (const SpeexSBMode*)(st->mode->mode); + + low_innov_alias = out+st->frame_size; + speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias); + /* Decode the low-band */ + ret = speex_decode_native(st->st_low, bits, out); + + speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx); + + /* If error decoding the narrowband part, propagate error */ + if (ret!=0) + { + return ret; + } + + if (!bits) + { + sb_decode_lost(st, out, dtx, stack); + return 0; + } + if (st->encode_submode) { @@ -1096,393 +1446,9 @@ int sb_decode(void *state, SpeexBits *bits, void *vout) return 0; } +#endif /* DISABLE_DECODER */ -int sb_encoder_ctl(void *state, int request, void *ptr) -{ - SBEncState *st; - st=(SBEncState*)state; - switch(request) - { - case SPEEX_GET_FRAME_SIZE: - (*(spx_int32_t*)ptr) = st->full_frame_size; - break; - case SPEEX_SET_HIGH_MODE: - st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr); - break; - case SPEEX_SET_LOW_MODE: - speex_encoder_ctl(st->st_low, SPEEX_SET_LOW_MODE, ptr); - break; - case SPEEX_SET_DTX: - speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr); - break; - case SPEEX_GET_DTX: - speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr); - break; - case SPEEX_GET_LOW_MODE: - speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr); - break; - case SPEEX_SET_MODE: - speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr); - break; -#ifndef DISABLE_VBR - case SPEEX_SET_VBR: - st->vbr_enabled = (*(spx_int32_t*)ptr); - speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr); - break; - case SPEEX_GET_VBR: - (*(spx_int32_t*)ptr) = st->vbr_enabled; - break; - case SPEEX_SET_VAD: - st->vad_enabled = (*(spx_int32_t*)ptr); - speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr); - break; - case SPEEX_GET_VAD: - (*(spx_int32_t*)ptr) = st->vad_enabled; - break; -#endif /* #ifndef DISABLE_VBR */ -#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) - case SPEEX_SET_VBR_QUALITY: - { - spx_int32_t q; - float qual = (*(float*)ptr)+.6; - st->vbr_quality = (*(float*)ptr); - if (qual>10) - qual=10; - q=(int)floor(.5+*(float*)ptr); - if (q>10) - q=10; - speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual); - speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q); - break; - } - case SPEEX_GET_VBR_QUALITY: - (*(float*)ptr) = st->vbr_quality; - break; -#endif /* #if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */ -#ifndef DISABLE_VBR - case SPEEX_SET_ABR: - st->abr_enabled = (*(spx_int32_t*)ptr); - st->vbr_enabled = st->abr_enabled!=0; - speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled); - if (st->vbr_enabled) - { - spx_int32_t i=10, rate, target; - float vbr_qual; - target = (*(spx_int32_t*)ptr); - while (i>=0) - { - speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i); - speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate); - if (rate <= target) - break; - i--; - } - vbr_qual=i; - if (vbr_qual<0) - vbr_qual=0; - speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual); - st->abr_count=0; - st->abr_drift=0; - st->abr_drift2=0; - } - - break; - case SPEEX_GET_ABR: - (*(spx_int32_t*)ptr) = st->abr_enabled; - break; -#endif /* #ifndef DISABLE_VBR */ - - case SPEEX_SET_QUALITY: - { - spx_int32_t nb_qual; - int quality = (*(spx_int32_t*)ptr); - if (quality < 0) - quality = 0; - if (quality > 10) - quality = 10; - st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality]; - nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality]; - speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual); - } - break; - case SPEEX_SET_COMPLEXITY: - speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr); - st->complexity = (*(spx_int32_t*)ptr); - if (st->complexity<1) - st->complexity=1; - break; - case SPEEX_GET_COMPLEXITY: - (*(spx_int32_t*)ptr) = st->complexity; - break; - case SPEEX_SET_BITRATE: - { - spx_int32_t i=10; - spx_int32_t rate, target; - target = (*(spx_int32_t*)ptr); - while (i>=0) - { - speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i); - speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate); - if (rate <= target) - break; - i--; - } - } - break; - case SPEEX_GET_BITRATE: - speex_encoder_ctl(st->st_low, request, ptr); - /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/ - if (st->submodes[st->submodeID]) - (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size; - else - (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size; - /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/ - break; - case SPEEX_SET_SAMPLING_RATE: - { - spx_int32_t tmp=(*(spx_int32_t*)ptr); - st->sampling_rate = tmp; - tmp>>=1; - speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp); - } - break; - case SPEEX_GET_SAMPLING_RATE: - (*(spx_int32_t*)ptr)=st->sampling_rate; - break; - case SPEEX_RESET_STATE: - { - int i; - st->first = 1; - for (i=0;ilpcSize;i++) - st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1); - for (i=0;ilpcSize;i++) - st->mem_sw[i]=st->mem_sp[i]=st->mem_sp2[i]=0; - for (i=0;ih0_mem[i]=st->h1_mem[i]=0; - } - break; - case SPEEX_SET_SUBMODE_ENCODING: - st->encode_submode = (*(spx_int32_t*)ptr); - speex_encoder_ctl(st->st_low, SPEEX_SET_SUBMODE_ENCODING, ptr); - break; - case SPEEX_GET_SUBMODE_ENCODING: - (*(spx_int32_t*)ptr) = st->encode_submode; - break; - case SPEEX_GET_LOOKAHEAD: - speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr); - (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1; - break; - case SPEEX_SET_PLC_TUNING: - speex_encoder_ctl(st->st_low, SPEEX_SET_PLC_TUNING, ptr); - break; - case SPEEX_GET_PLC_TUNING: - speex_encoder_ctl(st->st_low, SPEEX_GET_PLC_TUNING, ptr); - break; -#ifndef DISABLE_VBR - case SPEEX_SET_VBR_MAX_BITRATE: - { - st->vbr_max = (*(spx_int32_t*)ptr); - if (SPEEX_SET_VBR_MAX_BITRATE<1) - { - speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &st->vbr_max); - st->vbr_max_high = 17600; - } else { - spx_int32_t low_rate; - if (st->vbr_max >= 42200) - { - st->vbr_max_high = 17600; - } else if (st->vbr_max >= 27800) - { - st->vbr_max_high = 9600; - } else if (st->vbr_max > 20600) - { - st->vbr_max_high = 5600; - } else { - st->vbr_max_high = 1800; - } - if (st->subframeSize==80) - st->vbr_max_high = 1800; - low_rate = st->vbr_max - st->vbr_max_high; - speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate); - } - } - break; - case SPEEX_GET_VBR_MAX_BITRATE: - (*(spx_int32_t*)ptr) = st->vbr_max; - break; -#endif /* #ifndef DISABLE_VBR */ - case SPEEX_SET_HIGHPASS: - speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr); - break; - case SPEEX_GET_HIGHPASS: - speex_encoder_ctl(st->st_low, SPEEX_GET_HIGHPASS, ptr); - break; - - - /* This is all internal stuff past this point */ - case SPEEX_GET_PI_GAIN: - { - int i; - spx_word32_t *g = (spx_word32_t*)ptr; - for (i=0;inbSubframes;i++) - g[i]=st->pi_gain[i]; - } - break; - case SPEEX_GET_EXC: - { - int i; - for (i=0;inbSubframes;i++) - ((spx_word16_t*)ptr)[i] = st->exc_rms[i]; - } - break; -#ifndef DISABLE_VBR - case SPEEX_GET_RELATIVE_QUALITY: - (*(float*)ptr)=st->relative_quality; - break; -#endif /* #ifndef DISABLE_VBR */ - case SPEEX_SET_INNOVATION_SAVE: - st->innov_rms_save = (spx_word16_t*)ptr; - break; - case SPEEX_SET_WIDEBAND: - speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr); - break; - case SPEEX_GET_STACK: - *((char**)ptr) = st->stack; - break; - default: - speex_warning_int("Unknown nb_ctl request: ", request); - return -1; - } - return 0; -} - -int sb_decoder_ctl(void *state, int request, void *ptr) -{ - SBDecState *st; - st=(SBDecState*)state; - switch(request) - { - case SPEEX_SET_HIGH_MODE: - st->submodeID = (*(spx_int32_t*)ptr); - break; - case SPEEX_SET_LOW_MODE: - speex_decoder_ctl(st->st_low, SPEEX_SET_LOW_MODE, ptr); - break; - case SPEEX_GET_LOW_MODE: - speex_decoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr); - break; - case SPEEX_GET_FRAME_SIZE: - (*(spx_int32_t*)ptr) = st->full_frame_size; - break; - case SPEEX_SET_ENH: - speex_decoder_ctl(st->st_low, request, ptr); - st->lpc_enh_enabled = *((spx_int32_t*)ptr); - break; - case SPEEX_GET_ENH: - *((spx_int32_t*)ptr) = st->lpc_enh_enabled; - break; - case SPEEX_SET_MODE: - case SPEEX_SET_QUALITY: - { - spx_int32_t nb_qual; - int quality = (*(spx_int32_t*)ptr); - if (quality < 0) - quality = 0; - if (quality > 10) - quality = 10; - st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality]; - nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality]; - speex_decoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual); - } - break; - case SPEEX_GET_BITRATE: - speex_decoder_ctl(st->st_low, request, ptr); - if (st->submodes[st->submodeID]) - (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size; - else - (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size; - break; - case SPEEX_SET_SAMPLING_RATE: - { - spx_int32_t tmp=(*(spx_int32_t*)ptr); - st->sampling_rate = tmp; - tmp>>=1; - speex_decoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp); - } - break; - case SPEEX_GET_SAMPLING_RATE: - (*(spx_int32_t*)ptr)=st->sampling_rate; - break; - case SPEEX_SET_HANDLER: - speex_decoder_ctl(st->st_low, SPEEX_SET_HANDLER, ptr); - break; - case SPEEX_SET_USER_HANDLER: - speex_decoder_ctl(st->st_low, SPEEX_SET_USER_HANDLER, ptr); - break; - case SPEEX_RESET_STATE: - { - int i; - for (i=0;i<2*st->lpcSize;i++) - st->mem_sp[i]=0; - for (i=0;ig0_mem[i]=st->g1_mem[i]=0; - st->last_ener=0; - } - break; - case SPEEX_SET_SUBMODE_ENCODING: - st->encode_submode = (*(spx_int32_t*)ptr); - speex_decoder_ctl(st->st_low, SPEEX_SET_SUBMODE_ENCODING, ptr); - break; - case SPEEX_GET_SUBMODE_ENCODING: - (*(spx_int32_t*)ptr) = st->encode_submode; - break; - case SPEEX_GET_LOOKAHEAD: - speex_decoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr); - (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr); - break; - case SPEEX_SET_HIGHPASS: - speex_decoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr); - break; - case SPEEX_GET_HIGHPASS: - speex_decoder_ctl(st->st_low, SPEEX_GET_HIGHPASS, ptr); - break; - case SPEEX_GET_ACTIVITY: - speex_decoder_ctl(st->st_low, SPEEX_GET_ACTIVITY, ptr); - break; - case SPEEX_GET_PI_GAIN: - { - int i; - spx_word32_t *g = (spx_word32_t*)ptr; - for (i=0;inbSubframes;i++) - g[i]=st->pi_gain[i]; - } - break; - case SPEEX_GET_EXC: - { - int i; - for (i=0;inbSubframes;i++) - ((spx_word16_t*)ptr)[i] = st->exc_rms[i]; - } - break; - case SPEEX_GET_DTX_STATUS: - speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, ptr); - break; - case SPEEX_SET_INNOVATION_SAVE: - st->innov_save = (spx_word16_t*)ptr; - break; - case SPEEX_SET_WIDEBAND: - speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr); - break; - case SPEEX_GET_STACK: - *((char**)ptr) = st->stack; - break; - default: - speex_warning_int("Unknown nb_ctl request: ", request); - return -1; - } - return 0; -} #endif diff --git a/libspeex/stereo.c b/libspeex/stereo.c index 1f29a1c..02337ef 100644 --- a/libspeex/stereo.c +++ b/libspeex/stereo.c @@ -104,6 +104,7 @@ EXPORT void speex_stereo_state_destroy(SpeexStereoState *stereo) speex_free(stereo); } +#ifndef DISABLE_ENCODER #ifndef DISABLE_FLOAT_API EXPORT void speex_encode_stereo(float *data, int frame_size, SpeexBits *bits) { @@ -216,6 +217,12 @@ EXPORT void speex_encode_stereo_int(spx_int16_t *data, int frame_size, SpeexBits /*fprintf (stderr, "%d %d %d %d\n", largest, smallest, balance_id, e_ratio);*/ speex_bits_pack(bits, tmp, 2); } +#else /* DISABLE_ENCODER */ +EXPORT void speex_encode_stereo_int(spx_int16_t *data, int frame_size, SpeexBits *bits) +{ +} +#endif /* DISABLE_ENCODER */ + #ifndef DISABLE_FLOAT_API EXPORT void speex_decode_stereo(float *data, int frame_size, SpeexStereoState *_stereo) diff --git a/libspeex/vq.c b/libspeex/vq.c index 609f124..e95e1f2 100644 --- a/libspeex/vq.c +++ b/libspeex/vq.c @@ -47,7 +47,7 @@ #include "vq_bfin.h" #endif - +#ifndef DISABLE_ENCODER int scal_quant(spx_word16_t in, const spx_word16_t *boundary, int entries) { int i=0; @@ -69,9 +69,9 @@ int scal_quant32(spx_word32_t in, const spx_word32_t *boundary, int entries) } return i; } +#endif /* DISABLE_ENCODER */ - -#ifndef OVERRIDE_VQ_NBEST +#if !defined(OVERRIDE_VQ_NBEST) && !defined(DISABLE_ENCODER) /*Finds the indices of the n-best entries in a codebook*/ void vq_nbest(spx_word16_t *in, const spx_word16_t *codebook, int len, int entries, spx_word32_t *E, int N, int *nbest, spx_word32_t *best_dist, char *stack) { @@ -100,12 +100,12 @@ void vq_nbest(spx_word16_t *in, const spx_word16_t *codebook, int len, int entri } } } -#endif +#endif /* !defined(OVERRIDE_VQ_NBEST) && !defined(DISABLE_ENCODER) */ -#ifndef OVERRIDE_VQ_NBEST_SIGN +#if !defined(OVERRIDE_VQ_NBEST_SIGN) && !defined(DISABLE_WIDEBAND) && !defined(DISABLE_ENCODER) /*Finds the indices of the n-best entries in a codebook with sign*/ void vq_nbest_sign(spx_word16_t *in, const spx_word16_t *codebook, int len, int entries, spx_word32_t *E, int N, int *nbest, spx_word32_t *best_dist, char *stack) { @@ -144,4 +144,4 @@ void vq_nbest_sign(spx_word16_t *in, const spx_word16_t *codebook, int len, int } } } -#endif +#endif /* !defined(OVERRIDE_VQ_NBEST_SIGN) && !defined(DISABLE_WIDEBAND) && !defined(DISABLE_ENCODER) */ -- cgit v1.2.3