Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdlib.h>
6 : #include <assert.h>
7 : #include <limits.h>
8 : #include <string.h>
9 : #include "options.h"
10 : #include "cnst.h"
11 : #include "rom_com.h"
12 : #include "stl.h"
13 : #include "prot_fx.h"
14 : #include "prot_fx_enc.h" /* Function prototypes */
15 :
16 :
17 0 : static void enc_prm_hm(
18 : Word16 *prm_hm, /* Q0 */
19 : Encoder_State *st,
20 : Word16 L_frame /* Q0 */
21 : )
22 : {
23 : Word8 flag;
24 0 : BSTR_ENC_HANDLE hBstr = st->hBstr;
25 :
26 :
27 : /* Disable HM for non-GC,VC modes */
28 0 : test();
29 0 : IF( NE_16( st->hTcxCfg->coder_type, VOICED ) && NE_16( st->hTcxCfg->coder_type, GENERIC ) )
30 : {
31 0 : return;
32 : }
33 :
34 : /* Flag */
35 0 : push_next_indice( hBstr, prm_hm[0], 1 );
36 :
37 0 : IF( prm_hm[0] )
38 : {
39 : /* Periodicy index */
40 0 : flag = 0;
41 0 : move16();
42 0 : if ( GE_16( L_frame, 256 ) )
43 : {
44 0 : flag = 1;
45 0 : move16();
46 : }
47 0 : EncodeIndex_fx( flag, prm_hm[1], hBstr );
48 :
49 0 : IF( EQ_16( st->hTcxCfg->coder_type, VOICED ) )
50 : {
51 : /* Gain index */
52 0 : push_next_indice( hBstr, prm_hm[2], kTcxHmNumGainBits );
53 : }
54 : }
55 : }
56 22339 : static void enc_prm_hm_ivas_fx(
57 : Word16 *prm_hm, /* Q0 */
58 : Encoder_State *st,
59 : Word16 L_frame /* Q0 */
60 : )
61 : {
62 : Word8 flag;
63 22339 : BSTR_ENC_HANDLE hBstr = st->hBstr;
64 :
65 :
66 : /* Disable HM for non-GC,VC modes */
67 22339 : test();
68 22339 : IF( NE_16( st->hTcxCfg->coder_type, VOICED ) && NE_16( st->hTcxCfg->coder_type, GENERIC ) )
69 : {
70 2802 : return;
71 : }
72 :
73 : /* Flag */
74 19537 : push_next_indice( hBstr, prm_hm[0], 1 );
75 :
76 19537 : IF( prm_hm[0] )
77 : {
78 : /* Periodicy index */
79 10010 : flag = 0;
80 10010 : move16();
81 10010 : if ( GE_16( L_frame, L_FRAME ) )
82 : {
83 10010 : flag = 1;
84 10010 : move16();
85 : }
86 10010 : EncodeIndex_ivas_fx( flag, prm_hm[1], hBstr );
87 :
88 10010 : IF( EQ_16( st->hTcxCfg->coder_type, VOICED ) )
89 : {
90 : /* Gain index */
91 3076 : push_next_indice( hBstr, prm_hm[2], kTcxHmNumGainBits );
92 : }
93 : }
94 : }
95 :
96 : /*-----------------------------------------------------------------*
97 : * Function enc_prm_rf_fx() *
98 : * ~~~~~~~~~~~~~~~~~~~~~~ *
99 : * *
100 : * encode RF parameters for ACELP and TCX partial copy *
101 : *-----------------------------------------------------------------*/
102 :
103 0 : void enc_prm_rf_ivas_fx(
104 : Encoder_State *st,
105 : const Word16 rf_frame_type, /* Q0 */
106 : const Word16 fec_offset /* Q0 */
107 : )
108 : {
109 : Word16 sfr, nb_subfr, n, index;
110 : Word16 ltp_mode, ltf_mode, gains_mode;
111 0 : RF_ENC_HANDLE hRF = st->hRF;
112 0 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
113 0 : BSTR_ENC_HANDLE hBstr = st->hBstr;
114 :
115 :
116 0 : nb_subfr = st->nb_subfr; /* Q0 */
117 0 : move16();
118 : /* partial copy bitstream writing */
119 0 : test();
120 0 : IF( GE_16( rf_frame_type, RF_TCXFD ) && LE_16( rf_frame_type, RF_TCXTD2 ) )
121 : {
122 : /* TCX frames partial copy write */
123 :
124 : /* LSF indices */
125 0 : IF( EQ_16( rf_frame_type, RF_TCXFD ) )
126 : {
127 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], lsf_numbits[0] ); /* VQ 1 */
128 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], lsf_numbits[1] ); /* VQ 2 */
129 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][2], lsf_numbits[2] ); /* VQ 3 */
130 : }
131 :
132 : /* classification */
133 0 : test();
134 0 : IF( EQ_16( hRF->rf_clas[fec_offset], UNVOICED_CLAS ) )
135 : {
136 0 : index = 0;
137 0 : move16();
138 : }
139 0 : ELSE IF( ( EQ_16( hRF->rf_clas[fec_offset], VOICED_TRANSITION ) ) || ( EQ_16( hRF->rf_clas[fec_offset], UNVOICED_TRANSITION ) ) )
140 : {
141 0 : index = 1;
142 0 : move16();
143 : }
144 0 : ELSE IF( EQ_16( hRF->rf_clas[fec_offset], VOICED_CLAS ) )
145 : {
146 0 : index = 2;
147 0 : move16();
148 : }
149 : ELSE
150 : {
151 0 : index = 3;
152 0 : move16();
153 : }
154 0 : push_next_indice( hBstr, index, 2 );
155 :
156 0 : IF( EQ_16( rf_frame_type, RF_TCXFD ) )
157 : {
158 : /* TCX global gain = 7 bits */
159 0 : push_next_indice( hBstr, hRF->rf_gain_tcx[fec_offset], 7 );
160 : /*window info
161 : 1 bit for long overlap
162 : 2 if minimum or half overlap*/
163 : }
164 : ELSE
165 : {
166 : /*gains adapt
167 : gains inov*/
168 :
169 : /*LPC on full rate -> excitation */
170 : /* pitch and gain */
171 : /* LTP data */
172 0 : test();
173 0 : test();
174 0 : IF( ( EQ_16( rf_frame_type, RF_TCXTD1 ) || EQ_16( rf_frame_type, RF_TCXTD2 ) ) && hTcxEnc->tcxltp != 0 )
175 : {
176 0 : push_next_indice( hBstr, hRF->rf_tcxltp_param[fec_offset], 9 );
177 : }
178 : }
179 : }
180 0 : ELSE IF( EQ_16( rf_frame_type, 7 ) ) /* NELP bitstream writing */
181 : {
182 : /* LSF indices */
183 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
184 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
185 :
186 : /* NELP gain indices */
187 0 : push_next_indice( hBstr, hRF->rf_indx_nelp_iG1[fec_offset], 5 );
188 0 : push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][0], 6 );
189 0 : push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][1], 6 );
190 :
191 : /* NELP filter selection index */
192 0 : push_next_indice( hBstr, hRF->rf_indx_nelp_fid[fec_offset], 2 );
193 :
194 : /* tbe gainFr */
195 0 : push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 5 );
196 : }
197 0 : ELSE IF( GE_16( rf_frame_type, 4 ) ) /* rf_frame_type ALL_PRED: 4, NO_PRED: 5, GEN_PRED: 6 */
198 : {
199 : /* LSF indices */
200 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
201 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
202 :
203 : /* ES pred */
204 0 : push_next_indice( hBstr, hRF->rf_indx_EsPred[fec_offset], 3 );
205 :
206 0 : ltp_mode = ACELP_LTP_MODE[1][1][rf_frame_type];
207 0 : ltf_mode = ACELP_LTF_MODE[1][1][rf_frame_type];
208 0 : gains_mode = ACELP_GAINS_MODE[1][1][rf_frame_type];
209 0 : move16();
210 0 : move16();
211 0 : move16();
212 : /* Subframe parameters */
213 0 : FOR( sfr = 0; sfr < nb_subfr; sfr++ )
214 : {
215 : /* Pitch lag (5, or 8 bits) */
216 0 : n = ACELP_LTP_BITS_SFR[ltp_mode][sfr];
217 0 : move16();
218 0 : IF( n != 0 )
219 : {
220 0 : push_next_indice( hBstr, hRF->rf_indx_pitch[fec_offset][sfr], n );
221 : }
222 :
223 : /* Adaptive codebook filtering (1 bit) */
224 0 : IF( EQ_16( ltf_mode, 2 ) )
225 : {
226 0 : push_next_indice( hBstr, hRF->rf_indx_ltfMode[fec_offset][sfr], 1 );
227 : }
228 :
229 : /*Innovative codebook*/
230 0 : test();
231 0 : test();
232 0 : test();
233 0 : IF( ( EQ_16( rf_frame_type, RF_NOPRED ) ) ||
234 : ( EQ_16( rf_frame_type, RF_GENPRED ) &&
235 : ( sfr == 0 || EQ_16( sfr, 2 ) ) ) )
236 : {
237 0 : push_next_indice( hBstr, hRF->rf_indx_fcb[fec_offset][sfr], 7 );
238 : }
239 :
240 : /* Gains (5b, 6b or 7b / subfr) */
241 0 : test();
242 0 : IF( sfr == 0 || EQ_16( sfr, 2 ) )
243 : {
244 0 : n = ACELP_GAINS_BITS[gains_mode];
245 0 : move16();
246 0 : push_next_indice( hBstr, hRF->rf_indx_gain[fec_offset][sfr], n );
247 : }
248 : }
249 : /* tbe gainFr */
250 0 : push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 2 );
251 : }
252 :
253 : /***************/
254 : /*IMPORTANT: The last three bits are always the rf_frame_type in the bitstream (for both acelp and tcx partial copy);
255 : the rf_frame_type indicates the length of the partial copy payload at the decoder.
256 : The 2 bits before the rf_frame_type contains the fec_offset */
257 :
258 : /***************/
259 : /* write FEC offset just before the rf_frame_type */
260 0 : test();
261 0 : test();
262 0 : IF( EQ_16( fec_offset, 2 ) )
263 : {
264 0 : push_next_indice( hBstr, 0, 2 );
265 : }
266 0 : ELSE IF( EQ_16( fec_offset, 3 ) || EQ_16( fec_offset, 5 ) || EQ_16( fec_offset, 7 ) )
267 : {
268 0 : push_next_indice( hBstr, shr( sub( fec_offset, 1 ), 1 ), 2 );
269 : }
270 :
271 : /* write RF frame type last in the bitstream */
272 0 : push_next_indice( hBstr, rf_frame_type, 3 );
273 0 : }
274 0 : void enc_prm_rf_fx(
275 : Encoder_State *st,
276 : const Word16 rf_frame_type, /* Q0 */
277 : const Word16 fec_offset /* Q0 */
278 : )
279 : {
280 : Word16 sfr, nb_subfr, n, index;
281 : Word16 ltp_mode, ltf_mode, gains_mode;
282 0 : RF_ENC_HANDLE hRF = st->hRF;
283 0 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
284 0 : BSTR_ENC_HANDLE hBstr = st->hBstr;
285 :
286 :
287 0 : nb_subfr = st->nb_subfr; /* Q0 */
288 :
289 : /* partial copy bitstream writing */
290 0 : test();
291 0 : IF( GE_16( rf_frame_type, RF_TCXFD ) && LE_16( rf_frame_type, RF_TCXTD2 ) )
292 : {
293 : /* TCX frames partial copy write */
294 :
295 : /* LSF indices */
296 0 : IF( EQ_16( rf_frame_type, RF_TCXFD ) )
297 : {
298 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], lsf_numbits[0] ); /* VQ 1 */
299 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], lsf_numbits[1] ); /* VQ 2 */
300 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][2], lsf_numbits[2] ); /* VQ 3 */
301 : }
302 :
303 : /* classification */
304 0 : test();
305 0 : test();
306 0 : IF( EQ_16( hRF->rf_clas[fec_offset], UNVOICED_CLAS ) )
307 : {
308 0 : index = 0;
309 0 : move16();
310 : }
311 0 : ELSE IF( ( EQ_16( hRF->rf_clas[fec_offset], VOICED_TRANSITION ) ) || ( EQ_16( hRF->rf_clas[fec_offset], UNVOICED_TRANSITION ) ) )
312 : {
313 0 : index = 1;
314 0 : move16();
315 : }
316 0 : ELSE IF( EQ_16( hRF->rf_clas[fec_offset], VOICED_CLAS ) )
317 : {
318 0 : index = 2;
319 0 : move16();
320 : }
321 : ELSE
322 : {
323 0 : index = 3;
324 0 : move16();
325 : }
326 0 : push_next_indice( hBstr, index, 2 );
327 :
328 0 : IF( EQ_16( rf_frame_type, RF_TCXFD ) )
329 : {
330 : /* TCX global gain = 7 bits */
331 0 : push_next_indice( hBstr, hRF->rf_gain_tcx[fec_offset], 7 );
332 : /*window info
333 : 1 bit for long overlap
334 : 2 if minimum or half overlap*/
335 : }
336 : ELSE
337 : {
338 : /*gains adapt
339 : gains inov*/
340 :
341 : /*LPC on full rate -> excitation */
342 : /* pitch and gain */
343 : /* LTP data */
344 0 : test();
345 0 : IF( ( EQ_16( rf_frame_type, RF_TCXTD1 ) || EQ_16( rf_frame_type, RF_TCXTD2 ) ) && hTcxEnc->tcxltp != 0 )
346 : {
347 0 : push_next_indice( hBstr, hRF->rf_tcxltp_param[fec_offset], 9 );
348 : }
349 : }
350 : }
351 0 : ELSE IF( EQ_16( rf_frame_type, 7 ) ) /* NELP bitstream writing */
352 : {
353 : /* LSF indices */
354 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
355 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
356 :
357 : /* NELP gain indices */
358 0 : push_next_indice( hBstr, hRF->rf_indx_nelp_iG1[fec_offset], 5 );
359 0 : push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][0], 6 );
360 0 : push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][1], 6 );
361 :
362 : /* NELP filter selection index */
363 0 : push_next_indice( hBstr, hRF->rf_indx_nelp_fid[fec_offset], 2 );
364 :
365 : /* tbe gainFr */
366 0 : push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 5 );
367 : }
368 0 : ELSE IF( GE_16( rf_frame_type, 4 ) ) /* rf_frame_type ALL_PRED: 4, NO_PRED: 5, GEN_PRED: 6 */
369 : {
370 : /* LSF indices */
371 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
372 0 : push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
373 :
374 : /* ES pred */
375 0 : push_next_indice( hBstr, hRF->rf_indx_EsPred[fec_offset], 3 );
376 :
377 0 : ltp_mode = ACELP_LTP_MODE[1][1][rf_frame_type]; /* Q0 */
378 0 : ltf_mode = ACELP_LTF_MODE[1][1][rf_frame_type]; /* Q0 */
379 0 : gains_mode = ACELP_GAINS_MODE[1][1][rf_frame_type]; /* Q0 */
380 :
381 : /* Subframe parameters */
382 0 : FOR( sfr = 0; sfr < nb_subfr; sfr++ )
383 : {
384 : /* Pitch lag (5, or 8 bits) */
385 0 : n = ACELP_LTP_BITS_SFR[ltp_mode][sfr];
386 0 : IF( n != 0 )
387 : {
388 0 : push_next_indice( hBstr, hRF->rf_indx_pitch[fec_offset][sfr], n );
389 : }
390 :
391 : /* Adaptive codebook filtering (1 bit) */
392 0 : IF( EQ_16( ltf_mode, 2 ) )
393 : {
394 0 : push_next_indice( hBstr, hRF->rf_indx_ltfMode[fec_offset][sfr], 1 );
395 : }
396 :
397 : /*Innovative codebook*/
398 0 : test();
399 0 : test();
400 0 : test();
401 0 : IF( ( EQ_16( rf_frame_type, RF_NOPRED ) ) ||
402 : ( EQ_16( rf_frame_type, RF_GENPRED ) &&
403 : ( sfr == 0 || EQ_16( sfr, 2 ) ) ) )
404 : {
405 0 : push_next_indice( hBstr, hRF->rf_indx_fcb[fec_offset][sfr], 7 );
406 : }
407 :
408 : /* Gains (5b, 6b or 7b / subfr) */
409 0 : test();
410 0 : IF( sfr == 0 || EQ_16( sfr, 2 ) )
411 : {
412 0 : n = ACELP_GAINS_BITS[gains_mode];
413 0 : push_next_indice( hBstr, hRF->rf_indx_gain[fec_offset][sfr], n );
414 : }
415 : }
416 : /* tbe gainFr */
417 0 : push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 2 );
418 : }
419 :
420 : /***************/
421 : /*IMPORTANT: The last three bits are always the rf_frame_type in the bitstream (for both acelp and tcx partial copy);
422 : the rf_frame_type indicates the length of the partial copy payload at the decoder.
423 : The 2 bits before the rf_frame_type contains the fec_offset */
424 :
425 : /***************/
426 : /* write FEC offset just before the rf_frame_type */
427 0 : test();
428 0 : test();
429 0 : IF( EQ_16( fec_offset, 2 ) )
430 : {
431 0 : push_next_indice( hBstr, 0, 2 );
432 : }
433 0 : ELSE IF( EQ_16( fec_offset, 3 ) || EQ_16( fec_offset, 5 ) || EQ_16( fec_offset, 7 ) )
434 : {
435 0 : push_next_indice( hBstr, ( fec_offset - 1 ) / 2, 2 );
436 : }
437 :
438 : /* write RF frame type last in the bitstream */
439 0 : push_next_indice( hBstr, rf_frame_type, 3 );
440 0 : }
441 :
442 :
443 : /*-----------------------------------------------------------------*
444 : * Funtion enc_prm_fx() *
445 : * ~~~~~~~~~~~~~~~~~~~~~~ *
446 : * *
447 : * encode parameters according to selected mode including *
448 : * the FAC parameters when transition occurs. *
449 : *-----------------------------------------------------------------*/
450 :
451 1246 : void enc_prm_fx(
452 : const Word16 coder_type, /* (i) : coding type */
453 : Word16 param[], /* (i) : parameters Q0*/
454 : Word16 param_lpc[], /* (i) : LPC parameters Q0*/
455 : Encoder_State *st, /* i/o : quantization Analysis values */
456 : Word16 L_frame, /* Q0 */
457 : CONTEXT_HM_CONFIG hm_cfg[],
458 : Word16 *bits_param_lpc, /* Q0 */
459 : Word16 no_param_lpc /* Q0 */
460 : )
461 : {
462 : Word16 j, k, n, sfr, core, last_core, *prm, tmp;
463 : Word16 nbits_start, total_nbbits;
464 : Word16 nbits_header;
465 : Word16 nbits_lpc;
466 : Word16 nbits_tcx;
467 : Word16 lg, nb_subfr;
468 : Word16 lgFB;
469 : Word16 nTnsParams;
470 : Word16 nTnsBits;
471 : Word16 ix, j_old, wordcnt, bitcnt;
472 : Word16 hm_size;
473 : Word16 numlpc;
474 : Word8 flag_ctx_hm;
475 : Word16 index;
476 : Word32 tmp32;
477 : CONTEXT_HM_CONFIG *phm_cfg;
478 : Word16 idx;
479 : Word16 start_idx;
480 : Word16 nBits;
481 1246 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
482 1246 : BSTR_ENC_HANDLE hBstr = st->hBstr;
483 :
484 : /*--------------------------------------------------------------------------------*
485 : * INIT
486 : *--------------------------------------------------------------------------------*/
487 :
488 1246 : move16();
489 1246 : move16();
490 1246 : move16();
491 1246 : move16();
492 1246 : move16();
493 1246 : move16();
494 1246 : nbits_lpc = 0;
495 1246 : nbits_tcx = 0;
496 :
497 1246 : flag_ctx_hm = 0;
498 :
499 : /* Useful parameters */
500 1246 : move16();
501 1246 : move16();
502 1246 : move16();
503 1246 : nb_subfr = st->nb_subfr;
504 1246 : core = st->core;
505 1246 : last_core = st->last_core;
506 :
507 : /* Initialize pointers */
508 1246 : move16();
509 1246 : prm = param;
510 :
511 : /* Init counters */
512 1246 : move16();
513 1246 : move16();
514 1246 : j = 0;
515 1246 : nbits_start = hBstr->nb_bits_tot; /* Q0 */
516 :
517 :
518 : /*--------------------------------------------------------------------------------*
519 : * HEADER
520 : *--------------------------------------------------------------------------------*/
521 :
522 1246 : IF( EQ_16( st->mdct_sw, MODE1 ) )
523 : {
524 : /* Adjust st->bits_frame_core not to subtract MODE2 bandwidth signaling */
525 262 : st->bits_frame_core = add( st->bits_frame_core, FrameSizeConfig[st->frame_size_index].bandwidth_bits ); /* Q0 */
526 :
527 : /* Write MODE1 core mode signaling */
528 262 : signalling_mode1_tcx20_enc_fx( st, 1 );
529 : }
530 :
531 : /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
532 :
533 1246 : IF( st->tcxonly )
534 : {
535 0 : push_next_indice( hBstr, core == TCX_10_CORE, 1 );
536 : {
537 0 : index = 3;
538 0 : move16();
539 0 : test();
540 0 : IF( EQ_16( st->clas, UNVOICED_CLAS ) )
541 : {
542 0 : index = 0;
543 0 : move16();
544 : }
545 0 : ELSE IF( ( EQ_16( st->clas, VOICED_TRANSITION ) ) || ( EQ_16( st->clas, UNVOICED_TRANSITION ) ) )
546 : {
547 0 : index = 1;
548 0 : move16();
549 : }
550 0 : ELSE IF( EQ_16( st->clas, VOICED_CLAS ) )
551 : {
552 0 : index = 2;
553 0 : move16();
554 : }
555 0 : push_next_indice( hBstr, index, 2 );
556 : }
557 : }
558 : ELSE
559 : {
560 1246 : IF( core == ACELP_CORE )
561 : {
562 : /* write the RF signalling information */
563 612 : IF( EQ_16( st->rf_mode, 1 ) )
564 : {
565 : /* find the section in the ACELP signalling table corresponding to bitrate */
566 0 : idx = 0;
567 0 : WHILE( NE_32( acelp_sig_tbl[idx], st->total_brate ) ) /* total bitrate is kept at 13.2kbps */
568 : {
569 0 : idx = add( idx, 1 );
570 : }
571 :
572 : /* retrieve the number of bits for signalling */
573 0 : nBits = (Word16) acelp_sig_tbl[++idx]; /* Q0 */
574 :
575 : /* retrieve the signalling index */
576 0 : idx = add( idx, 1 ); /* Q0 */
577 0 : start_idx = idx; /* Q0 */
578 0 : tmp32 = SIG2IND_fx( coder_type, st->bwidth, st->sharpFlag, st->rf_mode );
579 0 : WHILE( NE_32( acelp_sig_tbl[idx], tmp32 ) )
580 : {
581 0 : idx = add( idx, 1 ); /* Q0 */
582 : }
583 0 : push_next_indice( hBstr, idx - start_idx, nBits );
584 0 : push_next_indice( hBstr, 0, 1 ); /* Indicate to the decoder that the core is ACELP*/
585 0 : nbits_start = 3; /* Q0 */
586 : }
587 : ELSE
588 : {
589 612 : push_next_indice( hBstr, coder_type, 3 );
590 : }
591 : }
592 : ELSE
593 : {
594 634 : IF( EQ_16( st->mdct_sw, MODE1 ) )
595 : {
596 : /* 2 bits instead of 3 as TCX is already signaled */
597 262 : push_next_indice( hBstr, st->hTcxCfg->coder_type, 2 );
598 : }
599 : ELSE
600 : {
601 372 : IF( EQ_16( st->mdct_sw_enable, MODE2 ) )
602 : {
603 372 : push_next_indice( hBstr, 1, 1 ); /* TCX */
604 372 : push_next_indice( hBstr, 0, 1 ); /* not HQ_CORE */
605 372 : push_next_indice( hBstr, st->hTcxCfg->coder_type, 2 );
606 : }
607 : ELSE
608 : {
609 : /*write the RF signalling information*/
610 0 : IF( EQ_16( st->rf_mode, 1 ) )
611 : {
612 : /* find the section in the ACELP signalling table corresponding to bitrate */
613 0 : idx = 0;
614 0 : WHILE( NE_32( acelp_sig_tbl[idx], st->total_brate ) )
615 : {
616 0 : idx = add( idx, 1 );
617 : }
618 :
619 : /* retrieve the number of bits for signalling */
620 0 : nBits = (Word16) acelp_sig_tbl[++idx]; /* Q0 */
621 :
622 0 : test();
623 0 : test();
624 0 : IF( EQ_16( st->hTcxCfg->coder_type, VOICED ) ||
625 : EQ_16( st->hTcxCfg->coder_type, GENERIC ) ||
626 : EQ_16( st->hTcxCfg->coder_type, TRANSITION ) )
627 : {
628 0 : st->sharpFlag = 1;
629 : }
630 : ELSE
631 : {
632 0 : st->sharpFlag = 0;
633 : }
634 :
635 : /* retrieve the signalling index */
636 0 : idx = add( idx, 1 ); /* Q0 */
637 0 : start_idx = idx; /* Q0 */
638 0 : tmp32 = SIG2IND_fx( st->hTcxCfg->coder_type, st->bwidth, st->sharpFlag, st->rf_mode );
639 0 : WHILE( NE_32( acelp_sig_tbl[idx], tmp32 ) )
640 : {
641 0 : idx = add( idx, 1 );
642 : }
643 0 : push_next_indice( hBstr, idx - start_idx, nBits );
644 0 : push_next_indice( hBstr, 1, 1 ); /* Indicate to the decoder that the core is TCX*/
645 0 : nbits_start = 3;
646 : }
647 : ELSE
648 : {
649 0 : push_next_indice( hBstr, 4 + st->hTcxCfg->coder_type, 3 );
650 : }
651 : }
652 : }
653 : }
654 : }
655 :
656 : /* Encode previous mode for error concealment */
657 1246 : test();
658 1246 : IF( !( core == ACELP_CORE && st->hTcxCfg->lfacNext <= 0 ) )
659 : {
660 634 : tmp = 0;
661 634 : move16();
662 634 : test();
663 634 : IF( NE_16( last_core, ACELP_CORE ) || EQ_16( core, TCX_10_CORE ) )
664 :
665 : {
666 596 : tmp = TCX_20_CORE;
667 596 : move16();
668 : }
669 634 : push_next_indice( hBstr, tmp, 1 );
670 : }
671 :
672 : /* write TCX overlap mode (1 bit: full, 2 bits: half or no overlap) */
673 1246 : IF( core != ACELP_CORE )
674 : {
675 : Word16 overlap_code;
676 634 : assert( st->hTcxCfg->tcx_curr_overlap_mode != NOT_SUPPORTED && st->hTcxCfg->tcx_curr_overlap_mode <= ALDO_WINDOW && st->hTcxCfg->tcx_curr_overlap_mode >= FULL_OVERLAP ); /*1 is not allowed!*/
677 634 : IF( EQ_16( st->hTcxCfg->tcx_curr_overlap_mode, MIN_OVERLAP ) )
678 : {
679 6 : nbits_tcx = 2;
680 6 : move16();
681 6 : overlap_code = 2;
682 6 : move16();
683 : }
684 628 : ELSE IF( EQ_16( st->hTcxCfg->tcx_curr_overlap_mode, HALF_OVERLAP ) )
685 : {
686 5 : nbits_tcx = 2;
687 5 : move16();
688 5 : overlap_code = 3;
689 5 : move16();
690 : }
691 : ELSE
692 : {
693 623 : nbits_tcx = 1;
694 623 : move16();
695 623 : overlap_code = 0;
696 623 : move16();
697 : }
698 634 : push_next_indice( hBstr, overlap_code, nbits_tcx );
699 : }
700 :
701 1246 : IF( st->hPlcExt->enableGplc )
702 : {
703 : /* encode side information. */
704 984 : enc_prm_side_Info_fx( st->hPlcExt, st );
705 : }
706 :
707 1246 : IF( st->glr != 0 )
708 : {
709 984 : test();
710 984 : test();
711 984 : test();
712 984 : test();
713 984 : if ( core != ACELP_CORE || EQ_16( coder_type, INACTIVE ) || ( st->last_core == ACELP_CORE && EQ_16( st->last_coder_type_raw, INACTIVE ) ) || st->glr_reset != 0 )
714 : {
715 378 : st->glr_idx[0] = 0;
716 378 : move16();
717 : }
718 :
719 984 : IF( EQ_16( core, ACELP_CORE ) )
720 : {
721 612 : push_next_indice( hBstr, st->glr_idx[0], G_LPC_RECOVERY_BITS );
722 : }
723 : }
724 :
725 1246 : st->glr_reset = 0;
726 1246 : move16();
727 :
728 1246 : nbits_header = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
729 :
730 :
731 : /*--------------------------------------------------------------------------------*
732 : * LPC PARAMETERS
733 : *--------------------------------------------------------------------------------*/
734 :
735 1246 : IF( s_and( st->enableTcxLpc != 0, core != ACELP_CORE ) )
736 : {
737 : /* Encode the indices */
738 0 : nbits_lpc = enc_lsf_tcxlpc_fx( ¶m_lpc, hBstr ); /* Q0 */
739 : }
740 : ELSE
741 : {
742 1246 : IF( st->lpcQuantization == 0 )
743 : {
744 : /* LPC quantizer */
745 0 : numlpc = 2;
746 0 : move16();
747 0 : if ( EQ_16( core, TCX_20_CORE ) )
748 : {
749 0 : numlpc = 1;
750 0 : move16();
751 : }
752 :
753 0 : nbits_lpc = encode_lpc_avq_fx( hBstr, numlpc, param_lpc, st->core, st->element_mode ); /* Q0 */
754 : }
755 1246 : ELSE IF( EQ_16( st->lpcQuantization, 1 ) )
756 : {
757 1246 : test();
758 1246 : test();
759 1246 : IF( EQ_32( st->sr_core, 16000 ) && EQ_16( coder_type, VOICED ) && EQ_16( core, ACELP_CORE ) )
760 : {
761 270 : nbits_lpc = lsf_bctcvq_encprm_fx( hBstr, param_lpc, bits_param_lpc, no_param_lpc ); /* Q0 */
762 : }
763 : ELSE
764 : {
765 976 : nbits_lpc = lsf_msvq_ma_encprm_fx( hBstr, param_lpc, core, coder_type, st->acelp_cfg.midLpc, bits_param_lpc, no_param_lpc ); /* Q0 */
766 : }
767 : }
768 : ELSE
769 : {
770 0 : assert( 0 && "LPC quant not supported!" );
771 : }
772 : }
773 :
774 :
775 : /*--------------------------------------------------------------------------------*
776 : * PRINT BIT ALLOCATION
777 : *--------------------------------------------------------------------------------*/
778 :
779 :
780 : /*--------------------------------------------------------------------------------*
781 : * ACELP
782 : *--------------------------------------------------------------------------------*/
783 :
784 1246 : IF( core == ACELP_CORE )
785 : {
786 : /* Adaptive BPF (2 bits)*/
787 612 : n = ACELP_BPF_BITS[st->acelp_cfg.bpf_mode];
788 :
789 612 : IF( n != 0 )
790 : {
791 585 : push_next_indice( hBstr, st->bpf_gain_param, n );
792 : }
793 :
794 : /* Mean energy (2 or 3 bits) */
795 612 : n = ACELP_NRG_BITS[st->acelp_cfg.nrg_mode]; /* Q0 */
796 :
797 612 : IF( n != 0 )
798 : {
799 612 : push_next_indice( hBstr, prm[j++], n );
800 : }
801 :
802 : /* Subframe parameters */
803 :
804 3672 : FOR( sfr = 0; sfr < nb_subfr; sfr++ )
805 : {
806 : /* Pitch lag (4, 5, 6, 8 or 9 bits) */
807 3060 : move16();
808 3060 : n = ACELP_LTP_BITS_SFR[st->acelp_cfg.ltp_mode][sfr]; /* Q0 */
809 :
810 3060 : IF( n != 0 )
811 : {
812 3060 : push_next_indice( hBstr, prm[j++], n );
813 : }
814 :
815 : /* Adaptive codebook filtering (1 bit) */
816 :
817 3060 : IF( EQ_16( st->acelp_cfg.ltf_mode, 2 ) )
818 : {
819 1350 : push_next_indice( hBstr, prm[j++], 1 );
820 : }
821 :
822 : /*Innovative codebook*/
823 : {
824 3060 : move16();
825 3060 : j_old = j;
826 :
827 3060 : if ( ( st->acelp_cfg.fixed_cdk_index[sfr] >= ACELP_FIXED_CDK_NB ) || ( st->acelp_cfg.fixed_cdk_index[sfr] < 0 ) )
828 : {
829 0 : fprintf( stderr, "ACELP bits allocation: wrong fixed cdk bit allocation\n" );
830 0 : assert( 0 );
831 : }
832 :
833 :
834 3060 : wordcnt = shr( ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[sfr] ), 4 );
835 :
836 3060 : bitcnt = s_and( ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[sfr] ), 15 );
837 :
838 :
839 12378 : FOR( ix = 0; ix < wordcnt; ix++ )
840 : {
841 9318 : push_next_indice( hBstr, prm[j++], 16 );
842 : }
843 :
844 3060 : IF( bitcnt )
845 : {
846 3060 : push_next_indice( hBstr, prm[j++], bitcnt );
847 : }
848 :
849 3060 : j = add( j_old, 8 ); /* Q0 */
850 : }
851 :
852 : /* Gains (5b, 6b or 7b / subfr) */
853 3060 : n = ACELP_GAINS_BITS[st->acelp_cfg.gains_mode[sfr]];
854 3060 : push_next_indice( hBstr, prm[j++], n );
855 : } /*end of for(sfr)*/
856 : } /*end of mode[0]==0*/
857 :
858 :
859 : /*--------------------------------------------------------------------------------*
860 : * TCX20
861 : *--------------------------------------------------------------------------------*/
862 1246 : IF( s_or( (Word16) EQ_16( core, TCX_20_CORE ), (Word16) EQ_16( core, HQ_CORE ) ) )
863 : {
864 634 : flag_ctx_hm = 0;
865 634 : move16();
866 634 : IF( st->enablePlcWaveadjust )
867 : {
868 0 : push_next_indice( hBstr, st->Tonal_SideInfo, 1 );
869 : }
870 :
871 : /* TCX Gain = 7 bits */
872 634 : push_next_indice( hBstr, prm[j++], 7 );
873 :
874 : /* TCX Noise Filling = NBITS_NOISE_FILL_LEVEL bits */
875 634 : push_next_indice( hBstr, prm[j++], NBITS_NOISE_FILL_LEVEL );
876 :
877 : /* LTP data */
878 634 : test();
879 634 : IF( hTcxEnc->tcxltp || GT_32( st->sr_core, 25600 ) )
880 : {
881 634 : IF( prm[j] )
882 : {
883 452 : push_next_indice( hBstr, 1, 1 );
884 452 : push_next_indice( hBstr, prm[j + 1], 9 );
885 452 : push_next_indice( hBstr, prm[j + 2], 2 );
886 : }
887 : ELSE
888 : {
889 182 : push_next_indice( hBstr, 0, 1 );
890 : }
891 : }
892 634 : j = add( j, 3 );
893 :
894 : /* TCX spectral data */
895 634 : lg = L_frame; /* Q0 */
896 634 : move16();
897 634 : lgFB = st->hTcxCfg->tcx_coded_lines; /* Q0 */
898 634 : move16();
899 :
900 634 : IF( last_core == ACELP_CORE )
901 : {
902 : /* ACE->TCX transition */
903 38 : lg = add( lg, st->hTcxCfg->tcx_offset );
904 38 : lgFB = add( lgFB, shr( lgFB, 2 ) );
905 38 : if ( st->hTcxCfg->lfacNext < 0 )
906 : {
907 38 : lg = sub( lg, st->hTcxCfg->lfacNext ); /* Q0 */
908 : }
909 : }
910 :
911 : /* TNS data */
912 634 : nTnsParams = 0;
913 634 : move16();
914 634 : nTnsBits = 0;
915 634 : move16();
916 :
917 634 : IF( st->hTcxCfg->fIsTNSAllowed )
918 : {
919 372 : WriteTnsData_fx( st->hTcxCfg->pCurrentTnsConfig, prm + j, &nTnsParams, st, &nTnsBits );
920 372 : j = add( j, nTnsParams ); /* Q0 */
921 : }
922 :
923 634 : hm_size = shl( mult( st->hTcxCfg->bandwidth, lg ), 1 );
924 634 : test();
925 634 : IF( hTcxEnc->tcx_lpc_shaped_ari && NE_16( last_core, ACELP_CORE ) )
926 : {
927 0 : enc_prm_hm( &prm[j], st, hm_size );
928 : }
929 :
930 : /*Context HM flag*/
931 634 : test();
932 634 : IF( st->hTcxCfg->ctx_hm && NE_16( last_core, ACELP_CORE ) )
933 : {
934 596 : push_next_indice( hBstr, prm[j], 1 );
935 :
936 596 : IF( prm[j] )
937 : {
938 54 : EncodeIndex_fx( sub( hm_size, 256 ) >= 0, prm[j + 1], hBstr );
939 54 : flag_ctx_hm = 1;
940 54 : move16();
941 : }
942 : }
943 634 : j = add( j, NPRM_CTX_HM );
944 :
945 634 : IF( st->igf )
946 : {
947 634 : st->hIGFEnc->infoTotalBitsPerFrameWritten = 0;
948 634 : move16();
949 634 : IF( EQ_16( st->last_core, ACELP_CORE ) )
950 : {
951 38 : IGFEncWriteBitstream_fx( st->hIGFEnc, hBstr, &st->hIGFEnc->infoTotalBitsPerFrameWritten, IGF_GRID_LB_TRAN, 1 );
952 : }
953 : ELSE
954 : {
955 596 : IGFEncWriteBitstream_fx( st->hIGFEnc, hBstr, &st->hIGFEnc->infoTotalBitsPerFrameWritten, IGF_GRID_LB_NORM, 1 );
956 : }
957 : }
958 634 : total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
959 634 : if ( EQ_16( st->rf_mode, 1 ) )
960 : {
961 0 : total_nbbits = add( total_nbbits, st->rf_target_bits_write ); /* Q0 */
962 : }
963 634 : nbits_tcx = sub( st->bits_frame_core, total_nbbits ); /* Q0 */
964 :
965 634 : IF( hTcxEnc->tcx_lpc_shaped_ari != 0 )
966 : {
967 0 : push_next_bits( hBstr, (UWord16 *) &prm[++j], nbits_tcx );
968 0 : j = add( j, nbits_tcx ); /* Q0 */
969 : }
970 : ELSE
971 : {
972 634 : phm_cfg = NULL;
973 634 : move16();
974 634 : if ( flag_ctx_hm )
975 : {
976 54 : phm_cfg = hm_cfg;
977 54 : move16();
978 : }
979 634 : ACcontextMapping_encode2_no_mem_s17_LC_fx( hBstr, prm + j,
980 : lgFB,
981 634 : prm[j - 1], /* lastnz */
982 634 : nbits_tcx, NPRM_RESQ * st->hTcxCfg->resq, phm_cfg );
983 : }
984 : }
985 :
986 :
987 : /*--------------------------------------------------------------------------------*
988 : * TCX10
989 : *--------------------------------------------------------------------------------*/
990 :
991 :
992 1246 : IF( EQ_16( core, TCX_10_CORE ) )
993 : {
994 0 : Word16 nbits_igf = 0;
995 0 : move16();
996 0 : IF( st->igf )
997 : {
998 0 : nbits_igf = IGFEncWriteConcatenatedBitstream_fx( st->hIGFEnc, hBstr ); /* Q0 */
999 : }
1000 0 : FOR( k = 0; k < 2; k++ )
1001 : {
1002 0 : flag_ctx_hm = 0;
1003 0 : move16();
1004 :
1005 0 : move16();
1006 0 : move16();
1007 0 : prm = param + ( k * NPRM_DIV );
1008 :
1009 0 : j = 0;
1010 :
1011 0 : move16();
1012 0 : nbits_tcx = total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
1013 :
1014 0 : test();
1015 0 : IF( st->enablePlcWaveadjust && k )
1016 : {
1017 0 : push_next_indice( hBstr, st->Tonal_SideInfo, 1 );
1018 : }
1019 :
1020 : /* TCX Gain = 7 bits */
1021 0 : push_next_indice( hBstr, prm[j++], 7 );
1022 :
1023 : /* TCX Noise Filling = NBITS_NOISE_FILL_LEVEL bits */
1024 0 : push_next_indice( hBstr, prm[j++], NBITS_NOISE_FILL_LEVEL );
1025 :
1026 : /* LTP data */
1027 0 : test();
1028 0 : test();
1029 0 : IF( ( k == 0 ) && ( hTcxEnc->tcxltp != 0 || GT_32( st->sr_core, 25600 ) ) ) /* PLC pitch info for HB */
1030 : {
1031 0 : IF( prm[j] )
1032 : {
1033 0 : push_next_indice( hBstr, 1, 1 );
1034 0 : push_next_indice( hBstr, prm[j + 1], 9 );
1035 0 : push_next_indice( hBstr, prm[j + 2], 2 );
1036 : }
1037 : ELSE
1038 : {
1039 0 : push_next_indice( hBstr, 0, 1 );
1040 : }
1041 : }
1042 0 : j = add( j, 3 );
1043 :
1044 : /* TCX spectral data */
1045 0 : lg = shr( L_frame, 1 );
1046 0 : lgFB = shr( st->hTcxCfg->tcx_coded_lines, 1 );
1047 :
1048 0 : IF( s_and( k == 0, last_core == ACELP_CORE ) )
1049 : {
1050 : /* ACE->TCX transition */
1051 0 : lg = add( lg, st->hTcxCfg->tcx_offset ); /* Q0 */
1052 0 : lgFB = add( lgFB, shr( lgFB, 1 ) );
1053 0 : if ( st->hTcxCfg->lfacNext < 0 )
1054 : {
1055 0 : lg = sub( lg, st->hTcxCfg->lfacNext ); /* Q0 */
1056 : }
1057 : }
1058 :
1059 : /* TNS data */
1060 0 : move16();
1061 0 : move16();
1062 0 : nTnsParams = 0;
1063 0 : nTnsBits = 0;
1064 :
1065 0 : IF( st->hTcxCfg->fIsTNSAllowed )
1066 : {
1067 :
1068 0 : SetTnsConfig( st->hTcxCfg, 0, ( last_core == ACELP_CORE ) && ( k == 0 ) );
1069 :
1070 0 : WriteTnsData_fx( st->hTcxCfg->pCurrentTnsConfig, prm + j, &nTnsParams, st, &nTnsBits );
1071 0 : j = add( j, nTnsParams );
1072 : }
1073 :
1074 0 : hm_size = shl( mult( st->hTcxCfg->bandwidth, lgFB ), 1 );
1075 :
1076 : /*Context HM flag*/
1077 0 : test();
1078 0 : test();
1079 0 : IF( st->hTcxCfg->ctx_hm && !( last_core == ACELP_CORE && k == 0 ) )
1080 : {
1081 0 : push_next_indice( hBstr, prm[j], 1 );
1082 :
1083 0 : IF( prm[j] )
1084 : {
1085 0 : EncodeIndex_fx( sub( hm_size, 256 ) >= 0, prm[j + 1], hBstr );
1086 0 : flag_ctx_hm = 1;
1087 0 : move16();
1088 : }
1089 : }
1090 0 : j = add( j, NPRM_CTX_HM );
1091 :
1092 0 : total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
1093 :
1094 0 : nbits_tcx = sub( shr( sub( add( sub( sub( sub( st->bits_frame_core, nbits_header ), nbits_lpc ), nbits_igf ), 1 ), k ), 1 ), sub( total_nbbits, nbits_tcx ) ); /* Q0 */
1095 :
1096 0 : phm_cfg = NULL;
1097 0 : move16();
1098 0 : if ( flag_ctx_hm )
1099 : {
1100 0 : phm_cfg = &hm_cfg[k];
1101 0 : move16();
1102 : }
1103 0 : ACcontextMapping_encode2_no_mem_s17_LC_fx( hBstr, prm + j,
1104 : lgFB,
1105 0 : prm[j - 1], /* lastnz */
1106 0 : nbits_tcx, NPRM_RESQ * st->hTcxCfg->resq, phm_cfg );
1107 :
1108 : } /* k, window index */
1109 : }
1110 :
1111 :
1112 : /*--------------------------------------------------------------------------------*
1113 : * END
1114 : *--------------------------------------------------------------------------------*/
1115 :
1116 :
1117 1246 : total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
1118 :
1119 :
1120 : /* Check if total encoded bits does not exceed CBR target bits (->this must never happen) */
1121 1246 : if ( st->bits_frame_core && ( total_nbbits > st->bits_frame_core ) )
1122 : {
1123 0 : fprintf( stderr, "AllocatedBits: %d Used bits: %d \n", st->bits_frame_core, total_nbbits );
1124 0 : assert( !"Core totalbits > CBR target bitrate" );
1125 : }
1126 :
1127 1246 : return;
1128 : }
1129 :
1130 :
1131 : /*-------------------------------------------------------------------*
1132 : * writeLPCparam_fx()
1133 : *
1134 : * write LTC parameters
1135 : *-------------------------------------------------------------------*/
1136 :
1137 283250 : void writeLPCparam_fx(
1138 : Encoder_State *st, /* i/o: encoder state structure */
1139 : BSTR_ENC_HANDLE hBstr, /* i/o: bitstream handle */
1140 : const Word16 param_lpc[], /* i : LPC parameters to write Q0*/
1141 : const Word16 bits_param_lpc[], /* i : bits per LPC parameter Q0*/
1142 : const Word16 no_param_lpc, /* i : number of LPC parameters Q0*/
1143 : Word16 *nbits_lpc /* o : LPC bits written Q0*/
1144 : )
1145 : {
1146 : Word16 numlpc;
1147 :
1148 283250 : test();
1149 283250 : IF( st->enableTcxLpc && st->core != ACELP_CORE )
1150 : {
1151 : /* Encode the indices */
1152 24233 : *nbits_lpc = enc_lsf_tcxlpc_ivas_fx( ¶m_lpc, hBstr ); /* Q0 */
1153 24233 : move16();
1154 : }
1155 : ELSE
1156 : {
1157 259017 : IF( st->lpcQuantization == 0 )
1158 : {
1159 : /* LPC quantizer */
1160 122166 : IF( EQ_16( st->core, TCX_20_CORE ) )
1161 : {
1162 119536 : numlpc = 1;
1163 119536 : move16();
1164 : }
1165 : ELSE
1166 : {
1167 2630 : numlpc = 2;
1168 2630 : move16();
1169 : }
1170 :
1171 122166 : *nbits_lpc = encode_lpc_avq_ivas_fx( hBstr, numlpc, param_lpc, st->core, st->element_mode ); /* Q0 */
1172 122166 : move16();
1173 : }
1174 136851 : ELSE IF( EQ_16( st->lpcQuantization, 1 ) )
1175 : {
1176 136851 : test();
1177 136851 : test();
1178 136851 : IF( EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( st->coder_type, VOICED ) && EQ_16( st->core, ACELP_CORE ) )
1179 : {
1180 0 : assert( st->element_mode == EVS_MONO );
1181 :
1182 0 : *nbits_lpc = lsf_bctcvq_encprm_ivas_fx( hBstr, param_lpc, bits_param_lpc, no_param_lpc ); /* Q0 */
1183 0 : move16();
1184 : }
1185 : ELSE
1186 : {
1187 136851 : *nbits_lpc = lsf_msvq_ma_encprm_ivas_fx( hBstr, param_lpc, st->core, st->coder_type, st->acelp_cfg.midLpc, bits_param_lpc, no_param_lpc ); /* Q0 */
1188 136851 : move16();
1189 : }
1190 : }
1191 : ELSE
1192 : {
1193 0 : assert( 0 );
1194 : }
1195 : }
1196 :
1197 283250 : return;
1198 : }
1199 :
1200 : /*-------------------------------------------------------------------*
1201 : * writeTCXparam_fx()
1202 : *
1203 : * write TCX core parameters
1204 : *-------------------------------------------------------------------*/
1205 :
1206 1686816 : void writeTCXparam_fx(
1207 : Encoder_State *st, /* i/o: Encoder State handle */
1208 : BSTR_ENC_HANDLE hBstr, /* i/o: bitstream handle */
1209 : CONTEXT_HM_CONFIG hm_cfg[], /* i/o: HM config */
1210 : Word16 param[], /* i : parameters Q0*/
1211 : const Word16 nbits_header, /* Q0 */
1212 : const Word16 nbits_start, /* Q0 */
1213 : const Word16 nbits_lpc, /* Q0 */
1214 : const Word16 *no_param_tns, /* i : number of TNS parameters per subframe Q0*/
1215 : Word16 p_param[2], /* i/o: pointer to parameters from previous bs writing Q0*/
1216 : const Word16 target_bitsTCX10[2], /* Q0 */
1217 : const Word16 pre_past_flag /* Q0 */
1218 : )
1219 : {
1220 : Word16 *prm;
1221 : Word16 j, k, nSubframes, core, last_core;
1222 : Word16 lg, lgFB, hm_size, flag_ctx_hm;
1223 : Word16 total_nbbits, nbits_igf, nbits_tcx;
1224 : Word16 nTnsParams, nTnsBits;
1225 : Word16 pre_part, post_part;
1226 :
1227 1686816 : IF( pre_past_flag == 0 )
1228 : {
1229 701724 : pre_part = 1;
1230 701724 : post_part = 0;
1231 701724 : move16();
1232 701724 : move16();
1233 : }
1234 985092 : ELSE IF( EQ_16( pre_past_flag, 1 ) )
1235 : {
1236 701842 : pre_part = 0;
1237 701842 : post_part = 1;
1238 701842 : move16();
1239 701842 : move16();
1240 : }
1241 : ELSE
1242 : {
1243 283250 : pre_part = 1;
1244 283250 : post_part = 1;
1245 283250 : move16();
1246 283250 : move16();
1247 : }
1248 :
1249 : /* Initialization */
1250 1686816 : core = st->core;
1251 1686816 : last_core = st->last_core;
1252 1686816 : nbits_igf = 0;
1253 1686816 : move16();
1254 1686816 : move16();
1255 1686816 : move16();
1256 :
1257 1686816 : nSubframes = 1;
1258 1686816 : move16();
1259 1686816 : if ( EQ_16( core, TCX_10_CORE ) )
1260 : {
1261 35480 : nSubframes = 2;
1262 35480 : move16();
1263 : }
1264 :
1265 : /* loop over subframes */
1266 3409112 : FOR( k = 0; k < nSubframes; k++ )
1267 : {
1268 1722296 : test();
1269 1722296 : test();
1270 1722296 : test();
1271 1722296 : IF( LT_16( st->element_mode, IVAS_CPE_MDCT ) && k == 0 && st->igf && EQ_16( core, TCX_10_CORE ) )
1272 : {
1273 2472 : nbits_igf = IGFEncWriteConcatenatedBitstream_ivas_fx( st->hIGFEnc, hBstr ); /* Q0 */
1274 : }
1275 :
1276 1722296 : flag_ctx_hm = 0;
1277 1722296 : move16();
1278 :
1279 1722296 : prm = param + imult1616( k, NPRM_DIV );
1280 1722296 : j = 0;
1281 1722296 : move16();
1282 :
1283 1722296 : nbits_tcx = total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
1284 1722296 : move16();
1285 :
1286 1722296 : test();
1287 1722296 : test();
1288 1722296 : IF( pre_part && st->enablePlcWaveadjust && EQ_16( k, sub( nSubframes, 1 ) ) )
1289 : {
1290 0 : push_next_indice( hBstr, st->Tonal_SideInfo, 1 );
1291 : }
1292 :
1293 1722296 : IF( post_part )
1294 : {
1295 : /* TCX Gain */
1296 1004147 : push_next_indice( hBstr, prm[j++], NBITS_TCX_GAIN );
1297 :
1298 : /* TCX Noise Filling = NBITS_NOISE_FILL_LEVEL bits */
1299 1004147 : push_next_indice( hBstr, prm[j++], NBITS_NOISE_FILL_LEVEL );
1300 : }
1301 : ELSE
1302 : {
1303 718149 : j = add( j, 1 + NOISE_FILL_RANGES );
1304 : }
1305 :
1306 : /* LTP data */
1307 1722296 : IF( pre_part )
1308 : {
1309 1004029 : test();
1310 1004029 : test();
1311 1004029 : IF( ( k == 0 ) && ( st->hTcxEnc->tcxltp || GT_32( st->sr_core, 25600 ) ) ) /* PLC pitch info for HB */
1312 : {
1313 984974 : IF( prm[j] )
1314 : {
1315 629307 : push_next_indice( hBstr, 1, 1 );
1316 629307 : push_next_indice( hBstr, prm[j + 1], 9 );
1317 629307 : push_next_indice( hBstr, prm[j + 2], 2 );
1318 : }
1319 : ELSE
1320 : {
1321 355667 : push_next_indice( hBstr, 0, 1 );
1322 : }
1323 : }
1324 1004029 : j = add( j, 3 );
1325 : }
1326 : ELSE
1327 : {
1328 718267 : j = add( j, sub( p_param[k], 2 ) ); /* Q0 */
1329 : }
1330 :
1331 : /* TCX spectral data */
1332 1722296 : lg = shr( st->last_L_frame, sub( nSubframes, 1 ) );
1333 1722296 : lgFB = shr( st->hTcxCfg->tcx_coded_lines, sub( nSubframes, 1 ) );
1334 :
1335 1722296 : test();
1336 1722296 : test();
1337 1722296 : IF( post_part && k == 0 && last_core == ACELP_CORE )
1338 : {
1339 : /* ACE->TCX transition */
1340 12274 : lg = add( lg, st->hTcxCfg->tcx_offset ); /* Q0 */
1341 12274 : lgFB = add( lgFB, shr( lgFB, sub( 3, nSubframes ) ) ); /* Q0 */
1342 :
1343 12274 : IF( st->hTcxCfg->lfacNext < 0 )
1344 : {
1345 12274 : lg = sub( lg, st->hTcxCfg->lfacNext ); /* Q0 */
1346 : }
1347 : }
1348 :
1349 1722296 : IF( pre_part )
1350 : {
1351 : /* TNS data */
1352 1004029 : nTnsParams = 0;
1353 1004029 : nTnsBits = 0;
1354 1004029 : move16();
1355 1004029 : move16();
1356 :
1357 1004029 : IF( st->hTcxCfg->fIsTNSAllowed )
1358 : {
1359 913464 : test();
1360 913464 : SetTnsConfig( st->hTcxCfg, core == TCX_20_CORE, ( last_core == ACELP_CORE ) && ( k == 0 ) );
1361 :
1362 913464 : IF( no_param_tns )
1363 : {
1364 14220 : IF( st->hTcxEnc->tnsData[k].nFilters < 0 )
1365 : {
1366 3212 : push_next_indice( hBstr, 1, 1 ); /* common_tns_data[] for subframe k */
1367 : }
1368 : ELSE
1369 : {
1370 11008 : push_next_indice( hBstr, 0, 1 ); /* common_tns_data[] for subframe k */
1371 : }
1372 : }
1373 913464 : test();
1374 913464 : IF( no_param_tns && ( st->hTcxEnc->tnsData[k].nFilters < 0 ) )
1375 : {
1376 : /* a negative filter count means that the filters are identical to those in the first channel at the same sub-frame */
1377 3212 : nTnsParams = no_param_tns[k]; /* Q0 */
1378 3212 : move16();
1379 : }
1380 : ELSE
1381 : {
1382 910252 : WriteTnsData_ivas_fx( st->hTcxCfg->pCurrentTnsConfig, prm + j, &nTnsParams, hBstr, &nTnsBits );
1383 : }
1384 913464 : IF( no_param_tns )
1385 : {
1386 14220 : nTnsBits = add( nTnsBits, 1 ); /* Q0 */
1387 : }
1388 913464 : j = add( j, nTnsParams ); /* Q0 */
1389 : }
1390 :
1391 1004029 : IF( post_part )
1392 : {
1393 285880 : hm_size = imult1616( mult( 2, st->hTcxCfg->bandwidth ), lg ); /* Q0 */
1394 :
1395 285880 : test();
1396 285880 : test();
1397 285880 : IF( st->hTcxEnc->tcx_lpc_shaped_ari && last_core != ACELP_CORE && EQ_16( core, TCX_20_CORE ) )
1398 : {
1399 22339 : enc_prm_hm_ivas_fx( &prm[j], st, hm_size );
1400 : }
1401 :
1402 : /*Context HM flag*/
1403 285880 : test();
1404 285880 : test();
1405 285880 : test();
1406 285880 : IF( st->hTcxCfg != NULL && st->hTcxCfg->ctx_hm && !( last_core == ACELP_CORE && k == 0 ) )
1407 : {
1408 208196 : push_next_indice( hBstr, prm[j], 1 );
1409 :
1410 208196 : IF( prm[j] )
1411 : {
1412 14570 : EncodeIndex_ivas_fx( (Word16) GE_16( hm_size, 256 ), prm[j + 1], hBstr );
1413 :
1414 14570 : flag_ctx_hm = 1;
1415 14570 : move16();
1416 : }
1417 : }
1418 : }
1419 1004029 : j = add( j, NPRM_CTX_HM );
1420 :
1421 1004029 : IF( post_part )
1422 : {
1423 : /* IGF data */
1424 285880 : IF( st->igf && EQ_16( core, TCX_20_CORE ) )
1425 : {
1426 : Word16 gridIdx;
1427 266167 : gridIdx = IGF_GRID_LB_NORM;
1428 266167 : move16();
1429 266167 : if ( last_core == ACELP_CORE )
1430 : {
1431 9014 : gridIdx = IGF_GRID_LB_TRAN;
1432 9014 : move16();
1433 : }
1434 266167 : st->hIGFEnc->infoTotalBitsPerFrameWritten = 0;
1435 266167 : move16();
1436 266167 : IGFEncWriteBitstream_ivas_fx( st->hIGFEnc, hBstr, &st->hIGFEnc->infoTotalBitsPerFrameWritten, gridIdx, 1 );
1437 : }
1438 :
1439 285880 : total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
1440 :
1441 285880 : IF( EQ_16( core, TCX_20_CORE ) )
1442 : {
1443 280620 : IF( st->rf_mode )
1444 : {
1445 0 : total_nbbits = add( total_nbbits, st->rf_target_bits_write ); /* Q0 */
1446 : }
1447 280620 : nbits_tcx = sub( st->bits_frame_core, total_nbbits ); /* Q0 */
1448 : }
1449 : ELSE /* TCX_10_CORE */
1450 : {
1451 5260 : nbits_tcx = sub( shr( sub( add( sub( sub( sub( st->bits_frame_core, nbits_header ), nbits_lpc ), nbits_igf ), 1 ), k ), 1 ), sub( total_nbbits, nbits_tcx ) ); /* Q0 */
1452 : }
1453 : }
1454 : ELSE
1455 : {
1456 : /*Context HM flag*/
1457 718149 : p_param[k] = j; /* Q0 */
1458 718149 : move16();
1459 : }
1460 : }
1461 : ELSE
1462 : {
1463 718267 : nbits_tcx = st->bits_frame_channel; /* Q0 */
1464 718267 : move16();
1465 718267 : IF( EQ_16( core, TCX_10_CORE ) )
1466 : {
1467 32850 : nbits_tcx = sub( target_bitsTCX10[k], NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL ); /* Q0 */
1468 : }
1469 : }
1470 :
1471 1722296 : IF( post_part )
1472 : {
1473 1004147 : test();
1474 1004147 : IF( st->hTcxEnc->tcx_lpc_shaped_ari && EQ_16( core, TCX_20_CORE ) )
1475 : {
1476 24233 : push_next_bits( hBstr, (UWord16 *) &prm[++j], nbits_tcx );
1477 24233 : j = add( j, nbits_tcx ); /* Q0 */
1478 : }
1479 : ELSE
1480 : {
1481 979914 : IF( st->element_mode > EVS_MONO )
1482 : {
1483 979914 : IF( flag_ctx_hm )
1484 : {
1485 14570 : RCcontextMapping_encode2_no_mem_s17_LCS_fx( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
1486 14570 : nbits_tcx, imult1616( NPRM_RESQ, st->hTcxCfg->resq ), &hm_cfg[k] );
1487 : }
1488 : ELSE
1489 : {
1490 965344 : RCcontextMapping_encode2_no_mem_s17_LCS_fx( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
1491 965344 : nbits_tcx, imult1616( NPRM_RESQ, st->hTcxCfg->resq ), NULL );
1492 : }
1493 : }
1494 : ELSE
1495 : {
1496 0 : IF( flag_ctx_hm )
1497 : {
1498 0 : ACcontextMapping_encode2_no_mem_s17_LC_fx( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
1499 0 : nbits_tcx, imult1616( NPRM_RESQ, st->hTcxCfg->resq ), &hm_cfg[k] );
1500 : }
1501 : ELSE
1502 : {
1503 0 : ACcontextMapping_encode2_no_mem_s17_LC_fx( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
1504 0 : nbits_tcx, imult1616( NPRM_RESQ, st->hTcxCfg->resq ), NULL );
1505 : }
1506 : }
1507 : }
1508 : }
1509 : }
1510 :
1511 1686816 : return;
1512 : }
1513 :
1514 : /*-------------------------------------------------------------------*
1515 : * writeTCXMode_fx()
1516 : *
1517 : * write TCX mode
1518 : *--------------------------------------------------------------------*/
1519 :
1520 984974 : void writeTCXMode_fx(
1521 : Encoder_State *st, /* i/o: encoder state structure */
1522 : BSTR_ENC_HANDLE hBstr, /* i/o: bitstream handle */
1523 : const Word16 MCT_flag, /* i : hMCT handle allocated (1) or not (0) Q0*/
1524 : Word16 *nbits_start /* o : nbits start Q0*/
1525 : )
1526 : {
1527 : UWord16 index;
1528 : Word16 idx, start_idx;
1529 : Word16 nBits;
1530 :
1531 984974 : IF( st->tcxonly )
1532 : {
1533 823890 : push_next_indice( hBstr, (UWord16) EQ_16( st->core, TCX_10_CORE ), 1 );
1534 :
1535 823890 : test();
1536 823890 : IF( EQ_16( st->clas, UNVOICED_CLAS ) )
1537 : {
1538 432622 : index = 0;
1539 432622 : move16();
1540 : }
1541 391268 : ELSE IF( EQ_16( st->clas, VOICED_TRANSITION ) || EQ_16( st->clas, UNVOICED_TRANSITION ) )
1542 : {
1543 52560 : index = 1;
1544 52560 : move16();
1545 : }
1546 338708 : ELSE IF( EQ_16( st->clas, VOICED_CLAS ) )
1547 : {
1548 292376 : index = 2;
1549 292376 : move16();
1550 : }
1551 : ELSE
1552 : {
1553 46332 : index = 3;
1554 46332 : move16();
1555 : }
1556 :
1557 823890 : push_next_indice( hBstr, index, 2 );
1558 :
1559 823890 : test();
1560 823890 : IF( EQ_16( st->element_mode, IVAS_CPE_MDCT ) && !MCT_flag )
1561 : {
1562 225312 : push_next_indice( hBstr, st->vad_flag, 1 );
1563 : }
1564 : }
1565 : ELSE
1566 : {
1567 161084 : IF( EQ_16( st->core, ACELP_CORE ) )
1568 : {
1569 : /* write the RF signaling information */
1570 0 : IF( EQ_16( st->rf_mode, 1 ) )
1571 : {
1572 : /* find the section in the ACELP signaling table corresponding to bitrate */
1573 0 : idx = 0;
1574 0 : move16();
1575 0 : WHILE( NE_32( acelp_sig_tbl[idx], st->total_brate ) ) /* total bitrate is kept at 13.2kbps */
1576 : {
1577 0 : idx = add( idx, 1 );
1578 : }
1579 :
1580 : /* retrieve the number of bits for signaling */
1581 0 : idx = add( idx, 1 ); /* Q0 */
1582 0 : nBits = extract_l( acelp_sig_tbl[idx] ); /* Q0 */
1583 :
1584 : /* retrieve the signaling index */
1585 0 : idx = add( idx, 1 );
1586 0 : start_idx = idx;
1587 0 : move16();
1588 0 : WHILE( NE_32( acelp_sig_tbl[idx], SIG2IND( st->coder_type, st->bwidth, st->sharpFlag, st->rf_mode ) ) )
1589 : {
1590 0 : idx = add( idx, 1 );
1591 : }
1592 0 : push_next_indice( hBstr, sub( idx, start_idx ), nBits );
1593 0 : push_next_indice( hBstr, 0, 1 ); /* Indicate to the decoder that the core is ACELP*/
1594 0 : *nbits_start = 3;
1595 0 : move16();
1596 : }
1597 : ELSE
1598 : {
1599 0 : push_next_indice( hBstr, st->coder_type, 3 );
1600 : }
1601 : }
1602 : ELSE
1603 : {
1604 161084 : IF( EQ_16( st->mdct_sw, MODE1 ) )
1605 : {
1606 : /* 2 bits instead of 3 as TCX is already signaled */
1607 161084 : push_next_indice( hBstr, st->hTcxCfg->coder_type, 2 );
1608 : }
1609 : ELSE
1610 : {
1611 0 : IF( EQ_16( st->mdct_sw_enable, MODE2 ) )
1612 : {
1613 0 : push_next_indice( hBstr, 1, 1 ); /* TCX */
1614 0 : push_next_indice( hBstr, 0, 1 ); /* not HQ_CORE */
1615 0 : push_next_indice( hBstr, st->hTcxCfg->coder_type, 2 );
1616 : }
1617 : ELSE
1618 : {
1619 : /*write the RF signaling information*/
1620 0 : IF( EQ_16( st->rf_mode, 1 ) )
1621 : {
1622 : /* find the section in the ACELP signaling table corresponding to bitrate */
1623 0 : idx = 0;
1624 0 : move16();
1625 0 : WHILE( NE_32( acelp_sig_tbl[idx], st->total_brate ) )
1626 : {
1627 0 : idx = add( idx, 1 );
1628 : }
1629 :
1630 : /* retrieve the number of bits for signaling */
1631 0 : idx = add( idx, 1 );
1632 0 : nBits = extract_l( acelp_sig_tbl[idx] );
1633 :
1634 0 : test();
1635 0 : test();
1636 0 : IF( EQ_16( st->hTcxCfg->coder_type, VOICED ) || EQ_16( st->hTcxCfg->coder_type, GENERIC ) || EQ_16( st->hTcxCfg->coder_type, TRANSITION ) )
1637 : {
1638 0 : st->sharpFlag = 1;
1639 0 : move16();
1640 : }
1641 : ELSE
1642 : {
1643 0 : st->sharpFlag = 0;
1644 0 : move16();
1645 : }
1646 :
1647 : /* retrieve the signaling index */
1648 0 : idx = add( idx, 1 );
1649 0 : start_idx = idx;
1650 0 : move16();
1651 0 : WHILE( NE_32( acelp_sig_tbl[idx], SIG2IND( st->hTcxCfg->coder_type, st->bwidth, st->sharpFlag, st->rf_mode ) ) )
1652 : {
1653 0 : idx = add( idx, 1 );
1654 : }
1655 0 : push_next_indice( hBstr, sub( idx, start_idx ), nBits );
1656 0 : push_next_indice( hBstr, 1, 1 ); /* Indicate to the decoder that the core is TCX*/
1657 0 : *nbits_start = 3;
1658 0 : move16();
1659 : }
1660 : ELSE
1661 : {
1662 0 : push_next_indice( hBstr, add( ACELP_MODE_MAX, st->hTcxCfg->coder_type ), 3 );
1663 : }
1664 : }
1665 : }
1666 : }
1667 : }
1668 :
1669 984974 : return;
1670 : }
1671 :
1672 :
1673 : /*-------------------------------------------------------------------*
1674 : * writeTCXWindowing_fx()
1675 : *
1676 : * write TCX transform type
1677 : *-------------------------------------------------------------------*/
1678 :
1679 1004029 : void writeTCXWindowing_fx(
1680 : BSTR_ENC_HANDLE hBstr, /* i/o: bitstream handle */
1681 : const Word16 overlap_mode /* i : overlap mode Q0*/
1682 : )
1683 : {
1684 :
1685 1004029 : IF( EQ_16( overlap_mode, MIN_OVERLAP ) )
1686 : {
1687 7225 : push_next_indice( hBstr, 2, 2 );
1688 : }
1689 996804 : ELSE IF( EQ_16( overlap_mode, HALF_OVERLAP ) )
1690 : {
1691 13100 : push_next_indice( hBstr, 3, 2 );
1692 : }
1693 : ELSE
1694 : {
1695 983704 : push_next_indice( hBstr, 0, 1 );
1696 : }
1697 :
1698 1004029 : return;
1699 : }
|