EXPORT int speex_std_stereo_request_handler(SpeexBits * bits, void *state, void *data) { (void)state; RealSpeexStereoState *stereo; spx_word16_t sign = 1, dexp; int tmp; stereo = (RealSpeexStereoState *) data; COMPATIBILITY_HACK(stereo); if (speex_bits_unpack_unsigned(bits, 1)) sign = -1; dexp = speex_bits_unpack_unsigned(bits, 5); #ifndef FIXED_POINT stereo->balance = exp(sign * .25 * dexp); #else stereo->balance = spx_exp(MULT16_16(sign, SHL16(dexp, 9))); #endif tmp = speex_bits_unpack_unsigned(bits, 2); stereo->e_ratio = e_ratio_quant[tmp]; return 0; }
void lsp_unquant_nb(spx_lsp_t *lsp, int order, SpeexBits *bits) { int i, id; for (i=0;i<order;i++) lsp[i]=LSP_LINEAR(i); id=speex_bits_unpack_unsigned(bits, 6); for (i=0;i<10;i++) lsp[i] = ADD32(lsp[i], LSP_DIV_256(cdbk_nb[id*10+i])); id=speex_bits_unpack_unsigned(bits, 6); for (i=0;i<5;i++) lsp[i] = ADD16(lsp[i], LSP_DIV_512(cdbk_nb_low1[id*5+i])); id=speex_bits_unpack_unsigned(bits, 6); for (i=0;i<5;i++) lsp[i] = ADD32(lsp[i], LSP_DIV_1024(cdbk_nb_low2[id*5+i])); id=speex_bits_unpack_unsigned(bits, 6); for (i=0;i<5;i++) lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_512(cdbk_nb_high1[id*5+i])); id=speex_bits_unpack_unsigned(bits, 6); for (i=0;i<5;i++) lsp[i+5] = ADD32(lsp[i+5], LSP_DIV_1024(cdbk_nb_high2[id*5+i])); }
void split_cb_shape_sign_unquant( spx_sig_t *exc, const void *par, /* non-overlapping codebook */ int nsf, /* number of samples in subframe */ SpeexBits *bits, char *stack, spx_int32_t *seed ) { int i,j; VARDECL(int *ind); VARDECL(int *signs); const signed char *shape_cb; //int shape_cb_size; int subvect_size, nb_subvect; const split_cb_params *params; int have_sign; params = (const split_cb_params *) par; subvect_size = params->subvect_size; nb_subvect = params->nb_subvect; //shape_cb_size = 1<<params->shape_bits; shape_cb = params->shape_cb; have_sign = params->have_sign; ALLOC(ind, nb_subvect, int); ALLOC(signs, nb_subvect, int); /* Decode codewords and gains */ for (i=0;i<nb_subvect;i++) { if (have_sign) signs[i] = speex_bits_unpack_unsigned(bits, 1); else signs[i] = 0; ind[i] = speex_bits_unpack_unsigned(bits, params->shape_bits); } /* Compute decoded excitation */ for (i=0;i<nb_subvect;i++) { spx_word16_t s=1; if (signs[i]) s=-1; #ifdef FIXED_POINT if (s==1) { for (j=0;j<subvect_size;j++) exc[subvect_size*i+j]=SHL32(EXTEND32(shape_cb[ind[i]*subvect_size+j]),SIG_SHIFT-5); } else { for (j=0;j<subvect_size;j++) exc[subvect_size*i+j]=NEG32(SHL32(EXTEND32(shape_cb[ind[i]*subvect_size+j]),SIG_SHIFT-5)); } #else for (j=0;j<subvect_size;j++) exc[subvect_size*i+j]+=s*0.03125*shape_cb[ind[i]*subvect_size+j]; #endif } }
int speex_std_vbr_quality_request_handler(SpeexBits *bits, void *state, void *data) { int qual; qual = speex_bits_unpack_unsigned(bits, 4); speex_encoder_ctl(data, SPEEX_SET_VBR_QUALITY, &qual); return 0; }
int speex_std_enh_request_handler(SpeexBits *bits, void *state, void *data) { int enh; enh = speex_bits_unpack_unsigned(bits, 1); speex_decoder_ctl(data, SPEEX_SET_ENH, &enh); return 0; }
int speex_std_high_mode_request_handler(SpeexBits *bits, void *state, void *data) { int m; m = speex_bits_unpack_unsigned(bits, 4); speex_encoder_ctl(data, SPEEX_SET_HIGH_MODE, &m); return 0; }
int speex_std_char_handler(SpeexBits *bits, void *state, void *data) { unsigned char ch; ch = speex_bits_unpack_unsigned(bits, 8); _speex_putc(ch, data); return 0; }
EXPORT int speex_std_low_mode_request_handler(SpeexBits *bits, void *state, void *data) { spx_int32_t m; m = speex_bits_unpack_unsigned(bits, 4); speex_encoder_ctl(data, SPEEX_SET_LOW_MODE, &m); return 0; }
int speex_std_vbr_request_handler(SpeexBits *bits, void *state, void *data) { int vbr; vbr = speex_bits_unpack_unsigned(bits, 1); speex_encoder_ctl(data, SPEEX_SET_VBR, &vbr); return 0; }
int speex_inband_handler(SpeexBits *bits, SpeexCallback *callback_list, void *state) { int id; SpeexCallback *callback; /*speex_bits_advance(bits, 5);*/ id=speex_bits_unpack_unsigned(bits, 4); callback = callback_list+id; if (callback->func) { return callback->func(bits, state, callback->data); } else /*If callback is not registered, skip the right number of bits*/ { int adv; if (id<2) adv = 1; else if (id<8) adv = 4; else if (id<10) adv = 8; else if (id<12) adv = 16; else if (id<14) adv = 32; else adv = 64; speex_bits_advance(bits, adv); } return 0; }
int speex_std_char_handler(SpeexBits *bits, void *state, void *data) { unsigned char ch; ch = speex_bits_unpack_unsigned(bits, 8); _speex_putc(ch, data); /*printf("speex_std_char_handler ch=%x\n", ch);*/ return 0; }
void lsp_unquant_high(spx_lsp_t *lsp, int order, SpeexBits *bits) { int i, id; for (i=0;i<order;i++) lsp[i]=LSP_LINEAR_HIGH(i); id=speex_bits_unpack_unsigned(bits, 6); for (i=0;i<order;i++) lsp[i] += LSP_DIV_256(high_lsp_cdbk[id*order+i]); id=speex_bits_unpack_unsigned(bits, 6); for (i=0;i<order;i++) lsp[i] += LSP_DIV_512(high_lsp_cdbk2[id*order+i]); }
int speex_std_stereo_request_handler(SpeexBits *bits, void *state, void *data) { SpeexStereoState *stereo; float sign=1; int tmp; stereo = (SpeexStereoState*)data; if (speex_bits_unpack_unsigned(bits, 1)) sign=-1; tmp = speex_bits_unpack_unsigned(bits, 5); stereo->balance = exp(sign*.25*tmp); tmp = speex_bits_unpack_unsigned(bits, 2); stereo->e_ratio = e_ratio_quant[tmp]; return 0; }
int speex_std_char_handler(SpeexBits *bits, void *state, void *data) { unsigned char ch; FILE *f = (FILE *)data; ch = speex_bits_unpack_unsigned(bits, 8); fputc(ch, f); return 0; }
/* This function will iterate frames & submodes in the Speex bits. * Returns 0 if a frame found, otherwise returns -1. */ int speex_get_next_frame(SpeexBits *bits) { static const int inband_skip_table[NB_SUBMODES] = {1, 1, 4, 4, 4, 4, 4, 4, 8, 8, 16, 16, 32, 32, 64, 64 }; static const int wb_skip_table[SB_SUBMODES] = {SB_SUBMODE_BITS+1, 36, 112, 192, 352, -1, -1, -1}; unsigned submode; unsigned nb_count = 0; while (speex_bits_remaining(bits) >= 5) { unsigned wb_count = 0; unsigned bit_ptr = bits->bitPtr; unsigned char_ptr = bits->charPtr; /* WB frame */ while ((speex_bits_remaining(bits) >= 4) && speex_bits_unpack_unsigned(bits, 1)) { int advance; submode = speex_bits_unpack_unsigned(bits, 3); advance = wb_skip_table[submode]; if (advance < 0) { TRACE__((THIS_FUNC, "Invalid mode encountered. " "The stream is corrupted.")); return -1; } TRACE__((THIS_FUNC, "WB layer skipped: %d bits", advance)); advance -= (SB_SUBMODE_BITS+1); speex_bits_advance(bits, advance); bit_ptr = bits->bitPtr; char_ptr = bits->charPtr; /* Consecutive subband frames may not exceed 2 frames */ if (++wb_count > 2) return -1; } /* End of bits, return the frame */ if (speex_bits_remaining(bits) < 4) { TRACE__((THIS_FUNC, "End of stream")); return 0; } /* Stop iteration, return the frame */ if (nb_count > 0) { bits->bitPtr = bit_ptr; bits->charPtr = char_ptr; return 0; } /* Get control bits */ submode = speex_bits_unpack_unsigned(bits, 4); TRACE__((THIS_FUNC, "Control bits: %d at %d", submode, bits->charPtr*8+bits->bitPtr)); if (submode == 15) { TRACE__((THIS_FUNC, "Found submode: terminator")); return -1; } else if (submode == 14) { /* in-band signal; next 4 bits contain signal id */ submode = speex_bits_unpack_unsigned(bits, 4); TRACE__((THIS_FUNC, "Found submode: in-band %d bits", inband_skip_table[submode])); speex_bits_advance(bits, inband_skip_table[submode]); } else if (submode == 13) { /* user in-band; next 5 bits contain msg len */ submode = speex_bits_unpack_unsigned(bits, 5); TRACE__((THIS_FUNC, "Found submode: user-band %d bytes", submode)); speex_bits_advance(bits, submode * 8); } else if (submode > 8) { TRACE__((THIS_FUNC, "Unknown sub-mode %d", submode)); return -1; } else { /* NB frame */ unsigned int advance = submode; speex_mode_query(&speex_nb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance); if (advance < 0) { TRACE__((THIS_FUNC, "Invalid mode encountered. " "The stream is corrupted.")); return -1; } TRACE__((THIS_FUNC, "Submode %d: %d bits", submode, advance)); advance -= (NB_SUBMODE_BITS+1); speex_bits_advance(bits, advance); ++nb_count; } } return 0; }
void pitch_unquant_3tap( spx_word16_t exc[], /* Input excitation */ spx_word32_t exc_out[], /* Output excitation */ int start, /* Smallest pitch value allowed */ int end, /* Largest pitch value allowed */ spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */ const void* par, int nsf, /* Number of samples in subframe */ int* pitch_val, spx_word16_t* gain_val, SpeexBits* bits, char* stack, int count_lost, int subframe_offset, spx_word16_t last_pitch_gain, int cdbk_offset ) { int i; int pitch; int gain_index; spx_word16_t gain[3]; const signed char* gain_cdbk; int gain_cdbk_size; const ltp_params* params; params = (const ltp_params*) par; gain_cdbk_size = 1 << params->gain_bits; gain_cdbk = params->gain_cdbk + 4 * gain_cdbk_size * cdbk_offset; pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits); pitch += start; gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits); /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/ #ifdef FIXED_POINT gain[0] = ADD16(32, (spx_word16_t)gain_cdbk[gain_index * 4]); gain[1] = ADD16(32, (spx_word16_t)gain_cdbk[gain_index * 4 + 1]); gain[2] = ADD16(32, (spx_word16_t)gain_cdbk[gain_index * 4 + 2]); #else gain[0] = 0.015625 * gain_cdbk[gain_index * 4] + .5; gain[1] = 0.015625 * gain_cdbk[gain_index * 4 + 1] + .5; gain[2] = 0.015625 * gain_cdbk[gain_index * 4 + 2] + .5; #endif if (count_lost && pitch > subframe_offset) { spx_word16_t gain_sum; if (1) { #ifdef FIXED_POINT spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : SHR16(last_pitch_gain, 1); if (tmp > 62) tmp = 62; #else spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : 0.5 * last_pitch_gain; if (tmp > .95) tmp = .95; #endif gain_sum = gain_3tap_to_1tap(gain); if (gain_sum > tmp) { spx_word16_t fact = DIV32_16(SHL32(EXTEND32(tmp), 14), gain_sum); for (i = 0; i < 3; i++) gain[i] = MULT16_16_Q14(fact, gain[i]); } } } *pitch_val = pitch; gain_val[0] = gain[0]; gain_val[1] = gain[1]; gain_val[2] = gain[2]; gain[0] = SHL16(gain[0], 7); gain[1] = SHL16(gain[1], 7); gain[2] = SHL16(gain[2], 7); SPEEX_MEMSET(exc_out, 0, nsf); for (i = 0; i < 3; i++) { int j; int tmp1, tmp3; int pp = pitch + 1 - i; tmp1 = nsf; if (tmp1 > pp) tmp1 = pp; for (j = 0; j < tmp1; j++) exc_out[j] = MAC16_16(exc_out[j], gain[2 - i], exc[j - pp]); tmp3 = nsf; if (tmp3 > pp + pitch) tmp3 = pp + pitch; for (j = tmp1; j < tmp3; j++) exc_out[j] = MAC16_16(exc_out[j], gain[2 - i], exc[j - pp - pitch]); } /*for (i=0;i<nsf;i++) exc[i]=PSHR32(exc32[i],13);*/ }
void pitch_unquant_3tap( spx_sig_t exc[], /* Excitation */ int start, /* Smallest pitch value allowed */ int end, /* Largest pitch value allowed */ spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */ const void *par, int nsf, /* Number of samples in subframe */ int *pitch_val, spx_word16_t *gain_val, SpeexBits *bits, char *stack, int count_lost, int subframe_offset, spx_word16_t last_pitch_gain, int cdbk_offset ) { int i; int pitch; int gain_index; spx_word16_t gain[3]; const signed char *gain_cdbk; int gain_cdbk_size; const ltp_params *params; params = (const ltp_params*) par; gain_cdbk_size = 1<<params->gain_bits; gain_cdbk = params->gain_cdbk + 3*gain_cdbk_size*cdbk_offset; pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits); pitch += start; gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits); /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/ #ifdef FIXED_POINT gain[0] = 32+(spx_word16_t)gain_cdbk[gain_index*3]; gain[1] = 32+(spx_word16_t)gain_cdbk[gain_index*3+1]; gain[2] = 32+(spx_word16_t)gain_cdbk[gain_index*3+2]; #else gain[0] = 0.015625*gain_cdbk[gain_index*3]+.5; gain[1] = 0.015625*gain_cdbk[gain_index*3+1]+.5; gain[2] = 0.015625*gain_cdbk[gain_index*3+2]+.5; #endif if (count_lost && pitch > subframe_offset) { float gain_sum; if (1) { float tmp = count_lost < 4 ? GAIN_SCALING_1*last_pitch_gain : 0.4 * GAIN_SCALING_1 * last_pitch_gain; if (tmp>.95) tmp=.95; gain_sum = GAIN_SCALING_1*gain_3tap_to_1tap(gain); if (gain_sum > tmp) { float fact = tmp/gain_sum; for (i=0; i<3; i++) gain[i]*=fact; } } } *pitch_val = pitch; gain_val[0]=gain[0]; gain_val[1]=gain[1]; gain_val[2]=gain[2]; { spx_sig_t *e[3]; VARDECL(spx_sig_t *tmp2); ALLOC(tmp2, 3*nsf, spx_sig_t); e[0]=tmp2; e[1]=tmp2+nsf; e[2]=tmp2+2*nsf; for (i=0; i<3; i++) { int j; int pp=pitch+1-i; #if 0 for (j=0; j<nsf; j++) { if (j-pp<0) e[i][j]=exc[j-pp]; else if (j-pp-pitch<0) e[i][j]=exc[j-pp-pitch]; else e[i][j]=0; } #else { int tmp1, tmp3; tmp1=nsf; if (tmp1>pp) tmp1=pp; for (j=0; j<tmp1; j++) e[i][j]=exc[j-pp]; tmp3=nsf; if (tmp3>pp+pitch) tmp3=pp+pitch; for (j=tmp1; j<tmp3; j++) e[i][j]=exc[j-pp-pitch]; for (j=tmp3; j<nsf; j++) e[i][j]=0; } #endif } #ifdef FIXED_POINT { for (i=0; i<nsf; i++) exc[i]=SHL32(ADD32(ADD32(MULT16_32_Q15(SHL16(gain[0],7),e[2][i]), MULT16_32_Q15(SHL16(gain[1],7),e[1][i])), MULT16_32_Q15(SHL16(gain[2],7),e[0][i])), 2); } #else for (i=0; i<nsf; i++) exc[i]=VERY_SMALL+gain[0]*e[2][i]+gain[1]*e[1][i]+gain[2]*e[0][i]; #endif } }
/* Default handler for user callbacks: skip it */ int speex_default_user_handler(SpeexBits *bits, void *state, void *data) { int req_size = speex_bits_unpack_unsigned(bits, 4); speex_bits_advance(bits, 5+8*req_size); return 0; }