Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include <assert.h>
7 : #include "options.h" /* Compilation switches */
8 : #include "prot_fx.h"
9 : #include "cnst.h" /* Common constants */
10 : #include "rom_com.h" /* Common constants */
11 : #include "basop_util.h" /* Function prototypes */
12 : #include "prot_fx_enc.h"
13 : #ifdef DEBUGGING
14 : #include "debug.h"
15 : #endif
16 :
17 : /*-------------------------------------------------------------------*
18 : * Local function prototypes
19 : *-------------------------------------------------------------------*/
20 :
21 : static void initFrameHeader_loc( Encoder_State *st );
22 : static void writeFrameHeader_loc( Encoder_State *st );
23 : static void configure_core_coder_loc( Encoder_State *st );
24 :
25 : /*-------------------------------------------------------------------*
26 : * evs_enc()
27 : *
28 : * Principal encoder routine
29 : *-------------------------------------------------------------------*/
30 :
31 3100 : ivas_error evs_enc_fx(
32 : Encoder_State *st, /* i/o: encoder state structure */
33 : const Word16 *data, /* i : input signal Q0*/
34 : Word32 *mem_hp20_in_fx, /* i/o: hp20 filter memory Qx*/
35 : const Word16 n_samples /* i : number of input samples Q0*/
36 : )
37 : {
38 : Word16 i, input_frame, delay;
39 3100 : Word16 old_inp_12k8[L_INP_12k8], *inp = 0; /* buffer of input signal @ 12k8 */
40 : Word16 old_inp_16k[L_INP]; /* buffer of input signal @ 16kHz */
41 : Word32 fr_bands[2 * NB_BANDS]; /* energy in frequency bands */
42 : Word16 Etot; /* total energy; correlation shift */
43 : Word32 ener; /* residual energy from Levinson-Durbin */
44 : Word16 A[NB_SUBFR16k * ( M + 1 )]; /* A(z) unquantized for subframes */
45 : Word16 Aw[NB_SUBFR16k * ( M + 1 )]; /* weighted A(z) unquantized for subframes */
46 : Word16 epsP_h[M + 1]; /* LP prediction errors */
47 : Word16 epsP_l[M + 1]; /* LP prediction errors */
48 : Word32 epsP[M + 1]; /* LP prediction errors */
49 : Word16 lsp_new[M]; /* LSPs at the end of the frame */
50 : Word16 lsp_mid[M]; /* ISPs in the middle of the frame */
51 : Word16 vad_hover_flag;
52 : Word16 hq_core_type; /* HQ core type (HQ, or LR-MDCT) */
53 :
54 : Word16 attack_flag; /* attack flag (GSC or TC) */
55 : Word16 new_inp_resamp16k[L_FRAME16k]; /* new input signal @16kHz, non pre-emphasised, used by the WB TBE/BWE */
56 : Word16 old_syn_12k8_16k[L_FRAME16k]; /* ACELP core synthesis at 12.8kHz or 16kHz to be used by the SWB BWE */
57 : Word16 shb_speech[L_FRAME16k];
58 : Word16 hb_speech[L_FRAME16k / 4];
59 : Word16 new_swb_speech[L_FRAME48k];
60 : Word32 bwe_exc_extended[L_FRAME32k + NL_BUFF_OFFSET]; /* non-linear bandwidth extension buffer */
61 : Word16 voice_factors[NB_SUBFR16k];
62 : Word16 fb_exc[L_FRAME16k];
63 : Word16 Voicing_flag;
64 3100 : Word16 pitch_buf[NB_SUBFR16k] = { 0 };
65 : Word16 unbits;
66 :
67 : Word16 padBits;
68 : Word32 realBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* real buffer */
69 : Word32 imagBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* imag buffer */
70 : CLDFB_SCALE_FACTOR cldfbScale;
71 : Word16 Q_new, shift, Q_synth;
72 : Word16 Q_r[2];
73 : Word16 Q_shb_spch, Q_fb_exc;
74 : Word32 L_tmp;
75 : UWord16 lsb;
76 : Word16 tmp;
77 : #ifndef FIX_I4_OL_PITCH
78 : Word16 pitch_orig[3]; /* original open-loop pitch values that might be altered in core_acelp_tcx20_switching_fx() within MODE2 */
79 : #endif
80 : ivas_error error;
81 3100 : SC_VBR_ENC_HANDLE hSC_VBR = st->hSC_VBR;
82 : // NOISE_EST_HANDLE hNoiseEst = st->hNoiseEst;
83 : // VAD_HANDLE hVAD = st->hVAD;
84 : // DTX_ENC_HANDLE hDtxEnc = st->hDtxEnc;
85 3100 : TD_CNG_ENC_HANDLE hTdCngEnc = st->hTdCngEnc;
86 3100 : LPD_state_HANDLE hLPDmem = st->hLPDmem;
87 3100 : TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
88 3100 : BSTR_ENC_HANDLE hBstr = st->hBstr;
89 3100 : RF_ENC_HANDLE hRF = st->hRF;
90 :
91 3100 : error = IVAS_ERR_OK;
92 :
93 3100 : push_wmops( "evs_enc_fx" );
94 :
95 3100 : Q_shb_spch = 0;
96 3100 : move16(); /* to avoid compiler warnings */
97 :
98 :
99 : /*------------------------------------------------------------------*
100 : * Initializiation
101 : *-----------------------------------------------------------------*/
102 :
103 3100 : input_frame = st->input_frame_fx;
104 3100 : move16();
105 3100 : st->core = -1;
106 3100 : move16();
107 3100 : st->extl = -1;
108 3100 : move16();
109 3100 : st->core_brate = -1;
110 3100 : move32();
111 3100 : st->input_bwidth = st->last_input_bwidth;
112 3100 : move16();
113 3100 : st->bwidth = st->last_bwidth;
114 3100 : move16();
115 3100 : hq_core_type = -1;
116 3100 : move16();
117 3100 : unbits = 0;
118 3100 : move16();
119 :
120 3100 : st->bits_frame_core = 0;
121 3100 : move16(); /* For getting bit consumption in core coder */
122 3100 : hTdCngEnc->lp_cng_mode2 = 0;
123 3100 : move16();
124 3100 : st->mdct_sw_enable = 0;
125 3100 : move16();
126 3100 : st->mdct_sw = 0;
127 3100 : move16();
128 3100 : shift = st->old_wsp_shift;
129 3100 : move16();
130 3100 : st->rate_switching_reset = 0;
131 3100 : move16();
132 :
133 3100 : st->idchan = 0;
134 3100 : move16();
135 3100 : st->flag_ACELP16k = set_ACELP_flag( EVS_MONO, -1, st->total_brate, 0, 0, -1, -1 );
136 3100 : move16();
137 :
138 :
139 : /*----------------------------------------------------------------*
140 : * set input samples buffer
141 : *----------------------------------------------------------------*/
142 :
143 : /* get delay to synchronize ACELP and MDCT frame */
144 3100 : delay = NS2SA_FX2( st->input_Fs, DELAY_FIR_RESAMPL_NS );
145 :
146 3100 : Copy( st->input_fx - delay, st->old_input_signal_fx, input_frame + delay ); /* st->q_inp */
147 :
148 : /*----------------------------------------------------------------*
149 : * convert 'short' input data to 'float'
150 : *----------------------------------------------------------------*/
151 :
152 3100 : Copy( data, st->input_fx, input_frame ); /* st-> q_inp */
153 3100 : IF( LT_16( n_samples, input_frame ) )
154 : {
155 0 : set16_fx( st->input_fx + n_samples, 0, sub( input_frame, n_samples ) );
156 : }
157 :
158 : /*----------------------------------------------------------------*
159 : * HP filtering
160 : *----------------------------------------------------------------*/
161 :
162 3100 : hp20( st->input_fx, 1, input_frame, mem_hp20_in_fx, st->input_Fs );
163 :
164 : /*----------------------------------------------------------------*
165 : * Updates in case of AMR-WB IO mode -> EVS primary mode switching
166 : *----------------------------------------------------------------*/
167 :
168 3100 : IF( EQ_16( st->last_core, AMR_WB_CORE ) )
169 : {
170 0 : updt_IO_switch_enc_fx( st, input_frame );
171 0 : set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
172 0 : cldfb_reset_memory( st->cldfbAnaEnc );
173 0 : cldfb_reset_memory( st->cldfbSynTd );
174 : }
175 :
176 : /*---------------------------------------------------------------------*
177 : * Pre-processing
178 : *---------------------------------------------------------------------*/
179 : #ifdef FIX_I4_OL_PITCH
180 : pre_proc( st, input_frame, old_inp_12k8, old_inp_16k, &inp, fr_bands, &Etot, &ener, A, Aw, epsP, lsp_new, lsp_mid, &vad_hover_flag, &attack_flag, new_inp_resamp16k, &Voicing_flag, realBuffer, imagBuffer, &hq_core_type );
181 : #else
182 3100 : pre_proc_fx( st, input_frame, old_inp_12k8, old_inp_16k, &inp, fr_bands, &Etot, &ener,
183 : pitch_orig, A, Aw, epsP_h, epsP_l, epsP, lsp_new, lsp_mid, &vad_hover_flag,
184 3100 : &attack_flag, new_inp_resamp16k, &Voicing_flag, realBuffer, imagBuffer, &cldfbScale, hLPDmem->old_exc,
185 : &hq_core_type, &Q_new, &shift, Q_r );
186 : #endif
187 : // st->sharpFlag = sharpFlag;
188 :
189 3100 : IF( EQ_16( st->mdct_sw, MODE2 ) )
190 : {
191 :
192 44 : Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
193 44 : st->bits_frame_nominal = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
194 :
195 44 : initFrameHeader_loc( st );
196 44 : writeFrameHeader_loc( st );
197 :
198 44 : test();
199 44 : test();
200 44 : test();
201 44 : IF( ( GT_32( st->total_brate, ACELP_24k40 ) && LT_32( st->total_brate, HQ_96k ) ) || ( EQ_32( st->total_brate, ACELP_24k40 ) && GE_16( st->bwidth, WB ) ) )
202 : {
203 44 : st->L_frame = L_FRAME16k;
204 44 : move16();
205 44 : st->gamma = GAMMA16k;
206 44 : move16();
207 44 : st->preemph_fac = PREEMPH_FAC_16k;
208 44 : move16();
209 :
210 44 : weight_a_subfr_fx( NB_SUBFR16k, A, Aw, GAMMA16k, M );
211 44 : test();
212 44 : IF( EQ_16( st->last_L_frame, L_FRAME ) && st->ini_frame != 0 )
213 : {
214 : /* this is just an approximation, but it is sufficient */
215 0 : Copy( st->lsp_old1_fx, st->lspold_enc_fx, M ); /* Q15 */
216 : }
217 : }
218 : ELSE
219 : {
220 0 : st->L_frame = L_FRAME;
221 0 : move16();
222 0 : st->gamma = GAMMA1;
223 0 : move16();
224 0 : st->preemph_fac = PREEMPH_FAC;
225 0 : move16();
226 : }
227 :
228 44 : st->sr_core = L_mult0( FRAMES_PER_SEC, st->L_frame ); /* Q0 */
229 44 : st->core_brate = st->total_brate;
230 44 : move32();
231 :
232 44 : st->igf = 0;
233 44 : move16();
234 44 : hq_core_type = NORMAL_HQ_CORE;
235 44 : move16();
236 44 : test();
237 44 : test();
238 44 : IF( ( EQ_16( st->bwidth, SWB ) || EQ_16( st->bwidth, WB ) ) && LE_32( st->total_brate, LRMDCT_CROSSOVER_POINT ) )
239 : {
240 : /* note that FB (bitrate >= 24400bps) is always coded with NORMAL_HQ_CORE */
241 0 : hq_core_type = LOW_RATE_HQ_CORE;
242 0 : move16();
243 : }
244 44 : ELSE IF( EQ_16( st->bwidth, NB ) )
245 : {
246 0 : hq_core_type = LOW_RATE_HQ_CORE;
247 0 : move16();
248 : }
249 : }
250 :
251 3100 : IF( EQ_16( st->codec_mode, MODE1 ) )
252 : {
253 : /*---------------------------------------------------------------------*
254 : * Write signalling info into the bitstream
255 : *---------------------------------------------------------------------*/
256 :
257 1834 : signalling_enc_fx( st );
258 :
259 : /*---------------------------------------------------------------------*
260 : * Preprocessing (preparing) for ACELP/HQ core switching
261 : *---------------------------------------------------------------------*/
262 :
263 1834 : core_switching_pre_enc_fx( st, old_inp_12k8, old_inp_16k, 0, 0 );
264 :
265 : /*---------------------------------------------------------------------*
266 : * ACELP core encoding
267 : *---------------------------------------------------------------------*/
268 :
269 1834 : IF( EQ_16( st->core, ACELP_CORE ) )
270 : {
271 1427 : acelp_core_enc_fx( st, inp, ener, A, Aw, epsP_h, epsP_l, lsp_new, lsp_mid, vad_hover_flag,
272 : attack_flag, bwe_exc_extended, voice_factors, old_syn_12k8_16k, pitch_buf, &unbits, NULL, NULL, Q_new, shift );
273 : }
274 : /*---------------------------------------------------------------------*
275 : * HQ core encoding
276 : *---------------------------------------------------------------------*/
277 :
278 1834 : IF( EQ_16( st->core, HQ_CORE ) )
279 : {
280 407 : hq_core_enc_fx( st, st->input_fx - delay, input_frame, hq_core_type, Voicing_flag, vad_hover_flag );
281 : }
282 :
283 : /*---------------------------------------------------------------------*
284 : * Postprocessing for ACELP/HQ core switching
285 : *---------------------------------------------------------------------*/
286 1834 : core_switching_post_enc_fx( st, old_inp_12k8, old_inp_16k, A, shift, Q_new, st->Q_syn2, &Q_synth );
287 : #ifndef FIX_I4_OL_PITCH
288 1834 : IF( EQ_16( st->core, HQ_CORE ) )
289 : {
290 407 : Copy( pitch_orig, st->pitch, 3 ); /* original open-loop pitch values might be altered in core_acelp_tcx20_switching_fx() */
291 : }
292 : #endif
293 : }
294 : ELSE /* MODE2 */
295 : {
296 : /*----------------------------------------------------------------*
297 : * Configuration of core coder/SID
298 : * Write Frame Header
299 : *----------------------------------------------------------------*/
300 :
301 1266 : configure_core_coder_loc( st );
302 :
303 1266 : IF( st->mdct_sw != MODE1 )
304 : {
305 1006 : writeFrameHeader_loc( st );
306 : }
307 :
308 : /*----------------------------------------------------------------*
309 : * Core-Coder
310 : *----------------------------------------------------------------*/
311 :
312 : /* Call main encoding function */
313 1266 : enc_acelp_tcx_main_fx( old_inp_16k + L_INP_MEM, st, Aw, lsp_new, lsp_mid,
314 : bwe_exc_extended, voice_factors, pitch_buf,
315 : vad_hover_flag, &Q_new, &shift );
316 : #ifndef FIX_I4_OL_PITCH
317 1266 : Copy( pitch_orig, st->pitch, 3 ); /* populate the original OL pitch values back Q0*/
318 : #endif
319 :
320 : /*---------------------------------------------------------------------*
321 : * Postprocessing for codec switching
322 : *---------------------------------------------------------------------*/
323 : /* TBE interface */
324 1266 : test();
325 1266 : IF( st->igf != 0 && GT_32( st->core_brate, SID_2k40 ) )
326 : {
327 1266 : IF( EQ_16( st->core, ACELP_CORE ) )
328 : {
329 604 : SWITCH( st->bwidth )
330 : {
331 0 : case WB:
332 0 : st->extl = WB_TBE;
333 0 : move16();
334 0 : st->extl_brate = WB_TBE_0k35;
335 0 : move32();
336 0 : BREAK;
337 :
338 604 : case SWB:
339 604 : st->extl = SWB_TBE;
340 604 : move16();
341 604 : st->extl_brate = SWB_TBE_1k6;
342 604 : move32();
343 604 : IF( LT_32( st->total_brate, ACELP_13k20 ) )
344 : {
345 0 : st->extl_brate = SWB_TBE_0k95;
346 0 : move32();
347 : }
348 604 : ELSE IF( GE_32( st->total_brate, ACELP_24k40 ) )
349 : {
350 604 : st->extl_brate = SWB_TBE_2k8;
351 604 : move32();
352 : }
353 604 : BREAK;
354 0 : case FB:
355 0 : st->extl = FB_TBE;
356 0 : move16();
357 0 : st->extl_brate = FB_TBE_1k8;
358 0 : move32();
359 :
360 0 : if ( GE_32( st->total_brate, ACELP_24k40 ) )
361 : {
362 0 : st->extl_brate = FB_TBE_3k0;
363 0 : move32();
364 : }
365 0 : BREAK;
366 : }
367 604 : }
368 : ELSE
369 : {
370 662 : st->coder_type = -1;
371 662 : move16();
372 662 : st->extl = IGF_BWE;
373 662 : move16();
374 662 : st->extl_brate = 0;
375 662 : move32();
376 : }
377 :
378 1266 : st->core_brate = L_sub( st->total_brate, st->extl_brate ); /* Q0 */
379 :
380 1266 : IF( EQ_16( st->tec_tfa, 1 ) )
381 : {
382 1006 : st->core_brate = L_sub( st->core_brate, BITS_TEC ); /* Q0 */
383 1006 : st->core_brate = L_sub( st->core_brate, BITS_TFA ); /* Q0 */
384 : }
385 : }
386 :
387 : /*----------------------------------------------------------------*
388 : * Complete Bitstream Writing
389 : *----------------------------------------------------------------*/
390 :
391 1266 : padBits = 0;
392 :
393 1266 : test();
394 1266 : test();
395 1266 : IF( st->igf != 0 && EQ_16( st->core, ACELP_CORE ) && GT_32( st->core_brate, SID_2k40 ) )
396 : {
397 : /* padBits = ((st->bits_frame+7)/8)*8 - (st->nb_bits_tot + (st->rf_target_bits_write - ((st->Opt_RF_ON==1)?1:0) ) + get_tbe_bits(st->total_brate, st->bwidth, st->rf_mode )); */
398 604 : tmp = add( get_tbe_bits_fx( st->total_brate, st->bwidth, st->rf_mode ), sub( st->rf_target_bits_write, st->rf_mode ) ); /* Q0 */
399 604 : padBits = sub( sub( shl( shr( add( st->bits_frame, 7 ), 3 ), 3 ), hBstr->nb_bits_tot ), tmp ); /* Q0 */
400 : }
401 : ELSE
402 : {
403 : /* padBits = ((st->bits_frame+7)/8)*8 - (st->nb_bits_tot + (st->rf_target_bits_write - ((st->Opt_RF_ON==1)?1:0) )); */
404 662 : tmp = sub( st->rf_target_bits_write, st->rf_mode ); /* Q0 */
405 662 : padBits = sub( shl( shr( add( st->bits_frame, 7 ), 3 ), 3 ), add( hBstr->nb_bits_tot, tmp ) ); /* Q0 */
406 : }
407 :
408 1589 : FOR( i = 0; i < padBits; i++ )
409 : {
410 323 : push_next_indice( hBstr, 0, 1 );
411 : }
412 : }
413 :
414 : /*---------------------------------------------------------------------*
415 : * WB TBE encoding
416 : * WB BWE encoding
417 : *---------------------------------------------------------------------*/
418 :
419 3100 : test();
420 3100 : IF( GE_32( st->input_Fs, 16000 ) && ( LT_16( st->bwidth, SWB ) ) )
421 : {
422 : /* Common pre-processing for WB TBE and WB BWE */
423 0 : wb_pre_proc_fx( st, new_inp_resamp16k, hb_speech );
424 : /* o: new_inp_resamp16k at Q = -1 */
425 : }
426 :
427 3100 : IF( EQ_16( st->extl, WB_TBE ) )
428 : {
429 : /* WB TBE encoder */
430 0 : wb_tbe_enc_fx( st, st->coder_type, hb_speech, bwe_exc_extended, Q_new, voice_factors, pitch_buf, st->voicing_fx );
431 :
432 0 : IF( EQ_16( st->codec_mode, MODE2 ) )
433 : {
434 0 : tbe_write_bitstream_fx( st );
435 : }
436 : }
437 3100 : ELSE IF( EQ_16( st->extl, WB_BWE ) )
438 : {
439 : /* WB BWE encoder */
440 0 : wb_bwe_enc_fx( st, new_inp_resamp16k, st->coder_type );
441 : }
442 :
443 : /*---------------------------------------------------------------------*
444 : * SWB(FB) TBE encoding
445 : * SWB BWE encoding
446 : *---------------------------------------------------------------------*/
447 3100 : test();
448 3100 : IF( !st->Opt_SC_VBR && GE_32( st->input_Fs, 32000 ) )
449 : {
450 : /* Common pre-processing for SWB(FB) TBE and SWB BWE */
451 3100 : swb_pre_proc_fx( st, st->input_fx, new_swb_speech, shb_speech, &Q_shb_spch, realBuffer, imagBuffer, &cldfbScale );
452 : }
453 0 : ELSE IF( GE_32( st->input_Fs, 32000 ) )
454 : {
455 0 : set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
456 0 : set16_fx( shb_speech, 0, L_FRAME16k );
457 : }
458 :
459 : /* SWB TBE encoder */
460 3100 : test();
461 3100 : test();
462 3100 : test();
463 3100 : test();
464 3100 : test();
465 3100 : test();
466 3100 : IF( EQ_16( st->extl, SWB_TBE ) || EQ_16( st->extl, FB_TBE ) || ( st->igf != 0 && EQ_16( st->core, ACELP_CORE ) && NE_16( st->extl, WB_TBE ) ) )
467 : {
468 1350 : test();
469 1350 : IF( NE_32( st->core_brate, FRAME_NO_DATA ) && NE_32( st->core_brate, SID_2k40 ) )
470 : {
471 1350 : swb_tbe_enc_fx( st, st->coder_type, shb_speech, bwe_exc_extended, voice_factors, fb_exc, &Q_fb_exc, Q_new, Q_shb_spch, st->voicing_fx, pitch_buf );
472 :
473 1350 : IF( EQ_16( st->extl, FB_TBE ) )
474 : {
475 : /* FB TBE encoder */
476 0 : fb_tbe_enc_fx( st, st->input_fx, fb_exc, Q_fb_exc );
477 : }
478 :
479 1350 : IF( EQ_16( st->codec_mode, MODE2 ) )
480 : {
481 604 : IF( EQ_16( st->tec_tfa, 1 ) )
482 : {
483 604 : tecEnc_TBE_fx( &( st->hTECEnc->corrFlag ), st->voicing_fx, st->coder_type );
484 :
485 604 : IF( EQ_16( st->coder_type, INACTIVE ) )
486 : {
487 0 : st->tec_flag = 0;
488 0 : move16();
489 0 : st->hTECEnc->corrFlag = 0;
490 0 : move16();
491 : }
492 604 : st->tfa_flag = tfaEnc_TBE_fx( st->tfa_enr, st->last_core, st->voicing_fx, pitch_buf, shl( Q_shb_spch, 1 ) ); /* Q0 */
493 604 : set_TEC_TFA_code_fx( st->hTECEnc->corrFlag, &st->tec_flag, &st->tfa_flag );
494 : }
495 : ELSE
496 : {
497 0 : st->tec_flag = 0;
498 0 : move16();
499 0 : st->hTECEnc->corrFlag = 0;
500 0 : move16();
501 0 : st->tfa_flag = 0;
502 0 : move16();
503 : }
504 :
505 604 : tbe_write_bitstream_fx( st );
506 : }
507 : }
508 : }
509 1750 : ELSE IF( EQ_16( st->extl, SWB_BWE ) || EQ_16( st->extl, FB_BWE ) )
510 : {
511 : /* SWB BWE encoder */
512 7 : swb_bwe_enc_fx( st, old_inp_12k8, old_inp_16k, old_syn_12k8_16k, new_swb_speech, shb_speech, st->coder_type, Q_shb_spch, sub( Q_new, 1 ) );
513 : }
514 1743 : ELSE IF( EQ_16( st->extl, SWB_BWE_HIGHRATE ) || EQ_16( st->extl, FB_BWE_HIGHRATE ) )
515 : {
516 : /* SWB HR BWE encoder */
517 674 : swb_bwe_enc_hr_fx( st, st->input_fx - delay, st->Q_syn2, input_frame, unbits );
518 : }
519 :
520 : /*---------------------------------------------------------------------*
521 : * SWB DTX/CNG encoding
522 : *---------------------------------------------------------------------*/
523 :
524 3100 : test();
525 3100 : IF( st->Opt_DTX_ON && GE_16( input_frame, L_FRAME32k ) )
526 : {
527 0 : swb_CNG_enc_fx( st, shb_speech, old_syn_12k8_16k );
528 : }
529 :
530 : /*---------------------------------------------------------------------*
531 : * Channel-aware mode - write signaling information into the bitstream
532 : *---------------------------------------------------------------------*/
533 :
534 3100 : signalling_enc_rf_fx( st );
535 :
536 3100 : updt_enc_common_fx( st, Etot, Q_new );
537 :
538 3100 : if ( EQ_16( st->mdct_sw, MODE1 ) )
539 : {
540 260 : st->codec_mode = MODE1;
541 260 : move16();
542 : }
543 3100 : if ( hTdCngEnc->lp_cng_mode2 )
544 : {
545 0 : st->codec_mode = MODE2;
546 0 : move16();
547 : }
548 :
549 3100 : IF( EQ_16( st->rf_mode, 1 ) )
550 : {
551 0 : IF( EQ_16( hRF->rf_frame_type, RF_NELP ) )
552 : {
553 0 : hSC_VBR->last_nelp_mode = 1;
554 : }
555 : ELSE
556 : {
557 0 : hSC_VBR->last_nelp_mode = 0;
558 : }
559 : }
560 :
561 : /* RF mode updates */
562 3100 : st->rf_mode_last = st->rf_mode;
563 3100 : IF( EQ_16( st->Opt_RF_ON, 1 ) )
564 : {
565 0 : st->L_frame = L_FRAME;
566 0 : st->rf_mode = 1;
567 : }
568 :
569 : #ifdef DEBUG_MODE_INFO
570 : dbgwrite( &st->codec_mode, sizeof( short ), 1, input_frame, "res/codec" );
571 : dbgwrite( &st->core, sizeof( short ), 1, input_frame, "res/core" );
572 : dbgwrite( &st->extl, sizeof( short ), 1, input_frame, "res/extl" );
573 : dbgwrite( &st->bwidth, sizeof( short ), 1, input_frame, "res/bwidth" );
574 : float ener_flt = st->total_brate / 1000.0f;
575 : dbgwrite( &ener_flt, sizeof( float ), 1, input_frame, "res/total_brate" );
576 : ener_flt = st->core_brate / 1000.0f;
577 : dbgwrite( &ener_flt, sizeof( float ), 1, input_frame, "res/core_brate" );
578 : ener_flt = st->extl_brate / 1000.0f;
579 : dbgwrite( &ener_flt, sizeof( float ), 1, input_frame, "res/extl_brate" );
580 : dbgwrite( &st->coder_type, sizeof( short ), 1, input_frame, "res/coder_type" );
581 : dbgwrite( &st->clas, sizeof( short ), 1, input_frame, "res/clas" );
582 : dbgwrite( &st->cng_type, sizeof( short ), 1, input_frame, "res/cng_type" );
583 : dbgwrite( &st->L_frame, sizeof( short ), 1, input_frame, "res/L_frame" );
584 : dbgwrite( &st->vad_flag, sizeof( short ), 1, input_frame, "res/vad_flag" );
585 : #endif
586 :
587 3100 : pop_wmops();
588 :
589 3100 : return error;
590 : }
591 :
592 :
593 : /*-------------------------------------------------------------------*
594 : * initFrameHeader()
595 : *
596 : * Initialize MODE2 frame header
597 : *-------------------------------------------------------------------*/
598 1310 : static void initFrameHeader_loc( Encoder_State *st )
599 : {
600 :
601 : Word16 n;
602 :
603 1310 : IF( EQ_32( st->core_brate, SID_2k40 ) )
604 : {
605 : /*Get size of frame*/
606 0 : st->bits_frame = FRAME_2_4;
607 0 : move16();
608 0 : st->bits_frame_core = add( st->bits_frame_core, FRAME_2_4 - 4 ); /*1 bit for SID on/off + 3 bits for bandwith in case of SID.*/
609 0 : st->frame_size_index = 2;
610 0 : move16();
611 : }
612 1310 : ELSE IF( EQ_32( st->core_brate, FRAME_NO_DATA ) )
613 : {
614 0 : st->bits_frame = FRAME_0;
615 0 : move16();
616 0 : st->bits_frame_core = add( st->bits_frame_core, st->bits_frame );
617 0 : st->frame_size_index = 0;
618 0 : move16();
619 : }
620 : ELSE
621 : {
622 : // PMT("Frame header needs review")
623 9960 : FOR( n = 0; n < FRAME_SIZE_NB; n++ )
624 : {
625 9960 : IF( EQ_16( FrameSizeConfig[n].frame_bits, st->bits_frame_nominal ) )
626 : {
627 1310 : st->frame_size_index = n;
628 1310 : move16();
629 1310 : st->bits_frame = FrameSizeConfig[n].frame_bits;
630 1310 : move16();
631 1310 : st->bits_frame_core = FrameSizeConfig[n].frame_net_bits;
632 1310 : move16();
633 1310 : BREAK;
634 : }
635 : }
636 : }
637 :
638 1310 : return;
639 : }
640 :
641 : /*-------------------------------------------------------------------*
642 : * writeFrameHeader()
643 : *
644 : * Write MODE2 frame header
645 : *-------------------------------------------------------------------*/
646 1050 : static void writeFrameHeader_loc( Encoder_State *st )
647 : {
648 :
649 1050 : BSTR_ENC_HANDLE hBstr = st->hBstr;
650 :
651 1050 : IF( NE_32( st->core_brate, FRAME_NO_DATA ) )
652 : {
653 : /* SID flag at 2.4kbps */
654 1050 : IF( EQ_32( st->core_brate, SID_2k40 ) )
655 : {
656 0 : IF( EQ_16( st->cng_type, FD_CNG ) )
657 : {
658 : /* write SID/CNG type flag */
659 0 : push_next_indice( hBstr, 1, 1 );
660 :
661 : /* write bandwidth mode */
662 0 : push_next_indice( hBstr, st->bwidth, 2 );
663 :
664 : /* write L_frame */
665 0 : IF( EQ_16( st->L_frame, L_FRAME ) )
666 : {
667 0 : push_next_indice( hBstr, 0, 1 );
668 : }
669 : ELSE
670 : {
671 0 : push_next_indice( hBstr, 1, 1 );
672 : }
673 : }
674 : }
675 : ELSE /* active frames */
676 : {
677 1050 : IF( st->rf_mode == 0 )
678 : {
679 1050 : push_next_indice( hBstr, sub( st->bwidth, FrameSizeConfig[st->frame_size_index].bandwidth_min ), FrameSizeConfig[st->frame_size_index].bandwidth_bits );
680 : }
681 : }
682 :
683 : /* Write reserved bit */
684 1050 : test();
685 1050 : IF( FrameSizeConfig[st->frame_size_index].reserved_bits && st->rf_mode == 0 )
686 : {
687 1050 : push_next_indice( hBstr, 0, FrameSizeConfig[st->frame_size_index].reserved_bits );
688 : }
689 : }
690 :
691 1050 : return;
692 : }
693 :
694 : /*------------------------------------------------------------------------*
695 : * Configuration of core coder/SID
696 : *------------------------------------------------------------------------*/
697 :
698 1266 : static void configure_core_coder_loc(
699 : Encoder_State *st /* i/o: encoder state structure */
700 : )
701 : {
702 :
703 1266 : initFrameHeader_loc( st );
704 :
705 1266 : test();
706 1266 : IF( NE_32( st->core_brate, SID_2k40 ) && NE_32( st->core_brate, FRAME_NO_DATA ) )
707 : {
708 1266 : if ( st->tcxonly )
709 : {
710 0 : st->coder_type = GENERIC;
711 0 : move16();
712 : }
713 :
714 1266 : st->hTcxCfg->coder_type = st->coder_type;
715 1266 : move16();
716 :
717 :
718 1266 : test();
719 1266 : test();
720 1266 : if ( !st->tcxonly && !st->localVAD && EQ_16( st->hTcxCfg->coder_type, GENERIC ) )
721 : {
722 50 : st->hTcxCfg->coder_type = UNVOICED;
723 50 : move16();
724 : }
725 : }
726 :
727 1266 : st->igf = getIgfPresent_fx( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
728 :
729 1266 : test();
730 1266 : if ( NE_32( st->core_brate, SID_2k40 ) && NE_32( st->core_brate, FRAME_NO_DATA ) )
731 : {
732 1266 : st->core_brate = st->total_brate;
733 1266 : move32();
734 : }
735 :
736 1266 : return;
737 : }
|