Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include "cnst.h"
43 : #include "rom_com.h"
44 : #include "prot.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * Local function prototypes
49 : *-------------------------------------------------------------------*/
50 :
51 : static void configure_core_coder( Encoder_State *st );
52 :
53 : static void writeFrameHeader( Encoder_State *st );
54 :
55 : static void initFrameHeader( Encoder_State *st );
56 :
57 :
58 : /*-------------------------------------------------------------------*
59 : * evs_enc()
60 : *
61 : * Principal encoder routine
62 : *-------------------------------------------------------------------*/
63 :
64 3100 : ivas_error evs_enc(
65 : Encoder_State *st, /* i/o: encoder state structure */
66 : const int16_t *data, /* i : input signal */
67 : float *mem_hp20_in, /* i/o: hp20 filter memory */
68 : const int16_t n_samples /* i : number of input samples */
69 : )
70 : {
71 : int16_t i, input_frame, delay;
72 : float old_inp_12k8[L_INP_12k8], *inp; /* buffer of input signal @ 12k8 */
73 : float old_inp_16k[L_INP]; /* buffer of input signal @ 16kHz */
74 : float fr_bands[2 * NB_BANDS]; /* energy in frequency bands */
75 : float ener; /* residual energy from Levinson-Durbin */
76 : float A[NB_SUBFR16k * ( M + 1 )]; /* A(z) unquantized for subframes */
77 : float Aw[NB_SUBFR16k * ( M + 1 )]; /* weighted A(z) unquantized for subframes */
78 : float epsP[M + 1]; /* LP prediction errors */
79 : float lsp_new[M]; /* LSPs at the end of the frame */
80 : float lsp_mid[M]; /* ISPs in the middle of the frame */
81 : int16_t vad_hover_flag; /* VAD hangover flag */
82 : int16_t hq_core_type; /* HQ core type (HQ, or LR-MDCT) */
83 : int16_t attack_flag; /* attack flag (GSC or TC) */
84 : float new_inp_resamp16k[L_FRAME16k]; /* new input signal @16kHz, non pre-emphasised, used by the WB TBE/BWE */
85 : float old_syn_12k8_16k[L_FRAME16k]; /* ACELP core synthesis at 12.8kHz or 16kHz to be used by the SWB BWE */
86 : float shb_speech[L_FRAME16k];
87 : float hb_speech[L_FRAME16k / 4];
88 : float new_swb_speech[L_FRAME48k];
89 : float bwe_exc_extended[L_FRAME32k + NL_BUFF_OFFSET];
90 : float voice_factors[NB_SUBFR16k];
91 : float fb_exc[L_FRAME16k];
92 : int16_t Voicing_flag;
93 : float pitch_buf[NB_SUBFR16k];
94 : int16_t unbits;
95 : int16_t padBits;
96 : float realBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* real buffer */
97 : float imagBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* imag buffer */
98 : #ifndef FIX_I4_OL_PITCH
99 : int16_t pitch_orig[3]; /* original open-loop pitch values that might be altered in core_acelp_tcx20_switching() within MODE2 */
100 : #endif
101 : ivas_error error;
102 :
103 3100 : error = IVAS_ERR_OK;
104 :
105 3100 : push_wmops( "evs_enc" );
106 :
107 : /*------------------------------------------------------------------*
108 : * Initialization
109 : *-----------------------------------------------------------------*/
110 :
111 3100 : input_frame = (int16_t) ( st->input_Fs / FRAMES_PER_SEC );
112 3100 : st->core = -1;
113 3100 : st->extl = -1;
114 3100 : st->core_brate = -1;
115 3100 : st->input_bwidth = st->last_input_bwidth;
116 3100 : st->bwidth = st->last_bwidth;
117 3100 : hq_core_type = -1;
118 3100 : unbits = 0;
119 :
120 3100 : st->bits_frame_core = 0; /* For getting bit consumption in core coder */
121 3100 : st->hTdCngEnc->lp_cng_mode2 = 0;
122 3100 : st->mdct_sw_enable = 0;
123 3100 : st->mdct_sw = 0;
124 3100 : st->rate_switching_reset = 0;
125 :
126 3100 : st->idchan = 0;
127 3100 : st->flag_ACELP16k = set_ACELP_flag( EVS_MONO, -1, st->total_brate, 0, 0, -1, -1 );
128 :
129 : /*----------------------------------------------------------------*
130 : * set input samples buffer
131 : *----------------------------------------------------------------*/
132 :
133 : /* get delay to synchronize ACELP and MDCT frame */
134 3100 : delay = NS2SA( st->input_Fs, DELAY_FIR_RESAMPL_NS );
135 :
136 3100 : mvr2r( st->input - delay, st->old_input_signal, input_frame + delay );
137 :
138 : /*----------------------------------------------------------------*
139 : * convert 'short' input data to 'float'
140 : *----------------------------------------------------------------*/
141 :
142 2643100 : for ( i = 0; i < n_samples; i++ )
143 : {
144 2640000 : st->input[i] = (float) data[i];
145 : }
146 :
147 3100 : if ( n_samples < input_frame )
148 : {
149 0 : set_f( st->input + n_samples, 0.0f, input_frame - n_samples );
150 : }
151 :
152 : /*----------------------------------------------------------------*
153 : * HP filtering
154 : *----------------------------------------------------------------*/
155 :
156 3100 : hp20( st->input, input_frame, mem_hp20_in, st->input_Fs );
157 :
158 : /*----------------------------------------------------------------*
159 : * Updates in case of AMR-WB IO mode -> EVS primary mode switching
160 : *----------------------------------------------------------------*/
161 :
162 3100 : if ( st->last_core == AMR_WB_CORE )
163 : {
164 0 : updt_IO_switch_enc( st, input_frame );
165 0 : cldfb_reset_memory( st->cldfbAnaEnc );
166 0 : cldfb_reset_memory( st->cldfbSynTd );
167 : }
168 :
169 : /*---------------------------------------------------------------------*
170 : * Pre-processing
171 : *---------------------------------------------------------------------*/
172 :
173 : #ifdef FIX_I4_OL_PITCH
174 : 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 );
175 : #else
176 3100 : pre_proc( st, input_frame, old_inp_12k8, old_inp_16k, &inp, fr_bands, &ener, pitch_orig, A, Aw, epsP, lsp_new, lsp_mid, &vad_hover_flag, &attack_flag, new_inp_resamp16k, &Voicing_flag, realBuffer, imagBuffer, &hq_core_type );
177 : #endif
178 :
179 3100 : if ( st->mdct_sw == MODE2 )
180 : {
181 75 : st->bits_frame_nominal = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
182 75 : initFrameHeader( st );
183 :
184 75 : writeFrameHeader( st );
185 :
186 75 : if ( ( st->total_brate > ACELP_24k40 && st->total_brate < HQ_96k ) || ( st->total_brate == ACELP_24k40 && st->bwidth >= WB ) )
187 : {
188 75 : st->L_frame = L_FRAME16k;
189 75 : st->gamma = GAMMA16k;
190 75 : st->preemph_fac = PREEMPH_FAC_16k;
191 :
192 75 : weight_a_subfr( NB_SUBFR16k, A, Aw, GAMMA16k, M );
193 :
194 75 : if ( st->last_L_frame == L_FRAME && st->ini_frame != 0 )
195 : {
196 : /* this is just an approximation, but it is sufficient */
197 0 : mvr2r( st->lsp_old1, st->lspold_enc, M );
198 : }
199 : }
200 : else
201 : {
202 0 : st->L_frame = L_FRAME;
203 0 : st->gamma = GAMMA1;
204 0 : st->preemph_fac = PREEMPH_FAC;
205 : }
206 :
207 75 : st->sr_core = st->L_frame * FRAMES_PER_SEC;
208 75 : st->core_brate = st->total_brate;
209 :
210 75 : st->igf = 0;
211 75 : hq_core_type = NORMAL_HQ_CORE;
212 :
213 75 : if ( ( st->bwidth == SWB || st->bwidth == WB ) && st->total_brate <= LRMDCT_CROSSOVER_POINT )
214 : {
215 : /* note that FB (bitrate >= 24400 bps) is always coded with NORMAL_HQ_CORE */
216 0 : hq_core_type = LOW_RATE_HQ_CORE;
217 : }
218 75 : else if ( st->bwidth == NB )
219 : {
220 0 : hq_core_type = LOW_RATE_HQ_CORE;
221 : }
222 : }
223 :
224 : /*---------------------------------------------------------------------*
225 : * Encoding
226 : *---------------------------------------------------------------------*/
227 :
228 3100 : if ( st->codec_mode == MODE1 )
229 : {
230 : /* write signaling info into the bitstream */
231 1850 : signaling_enc( st );
232 :
233 : /*---------------------------------------------------------------------*
234 : * Preprocessing (preparing) for ACELP/HQ core switching
235 : *---------------------------------------------------------------------*/
236 :
237 1850 : core_switching_pre_enc( st, old_inp_12k8, old_inp_16k, 0, 0 );
238 :
239 : /*---------------------------------------------------------------------*
240 : * ACELP core encoding
241 : *---------------------------------------------------------------------*/
242 :
243 1850 : if ( st->core == ACELP_CORE )
244 : {
245 1410 : if ( ( error = acelp_core_enc( st, inp, ener, A, Aw, epsP, lsp_new, lsp_mid, vad_hover_flag, attack_flag, bwe_exc_extended, voice_factors, old_syn_12k8_16k, pitch_buf, &unbits, NULL, NULL ) ) != IVAS_ERR_OK )
246 : {
247 0 : return error;
248 : }
249 : }
250 :
251 : /*---------------------------------------------------------------------*
252 : * HQ core encoding
253 : *---------------------------------------------------------------------*/
254 :
255 1850 : if ( st->core == HQ_CORE )
256 : {
257 440 : hq_core_enc( st, st->input - delay, input_frame, hq_core_type, Voicing_flag, vad_hover_flag );
258 : }
259 :
260 : /*---------------------------------------------------------------------*
261 : * Postprocessing for ACELP/HQ core switching
262 : *---------------------------------------------------------------------*/
263 :
264 1850 : core_switching_post_enc( st, old_inp_12k8, old_inp_16k, A );
265 :
266 : #ifndef FIX_I4_OL_PITCH
267 1850 : if ( st->core == HQ_CORE )
268 : {
269 440 : mvs2s( pitch_orig, st->pitch, 3 ); /* original open-loop pitch values might be altered in core_acelp_tcx20_switching() */
270 : }
271 : #endif
272 : }
273 :
274 : else /* MODE2 */
275 : {
276 :
277 : /*----------------------------------------------------------------*
278 : * Configuration of core coder/SID
279 : * Write Frame Header
280 : *----------------------------------------------------------------*/
281 :
282 1250 : configure_core_coder( st );
283 :
284 1250 : if ( st->mdct_sw != MODE1 )
285 : {
286 975 : writeFrameHeader( st );
287 : }
288 :
289 : /*----------------------------------------------------------------*
290 : * Core-Coder
291 : *----------------------------------------------------------------*/
292 :
293 : /* Call main encoding function */
294 1250 : enc_acelp_tcx_main( st, old_inp_16k + L_INP_MEM, Aw, lsp_new, lsp_mid, bwe_exc_extended, voice_factors, pitch_buf, vad_hover_flag );
295 :
296 : #ifndef FIX_I4_OL_PITCH
297 1250 : mvs2s( pitch_orig, st->pitch, 3 ); /* populate the original OL pitch values back */
298 : #endif
299 :
300 : /*---------------------------------------------------------------------*
301 : * Postprocessing for Mode 1/2 switching
302 : *---------------------------------------------------------------------*/
303 : /* TBE for Mode 2 interface */
304 1250 : if ( st->igf && st->core_brate > SID_2k40 )
305 : {
306 1250 : if ( st->core == ACELP_CORE )
307 : {
308 604 : switch ( st->bwidth )
309 : {
310 0 : case WB:
311 0 : st->extl = WB_TBE;
312 0 : st->extl_brate = WB_TBE_0k35;
313 0 : break;
314 :
315 604 : case SWB:
316 604 : st->extl = SWB_TBE;
317 604 : st->extl_brate = SWB_TBE_1k6;
318 :
319 604 : if ( st->total_brate < ACELP_13k20 )
320 : {
321 0 : st->extl_brate = SWB_TBE_0k95;
322 : }
323 604 : else if ( st->total_brate >= ACELP_24k40 )
324 : {
325 604 : st->extl_brate = SWB_TBE_2k8;
326 : }
327 :
328 604 : break;
329 :
330 0 : case FB:
331 0 : st->extl = FB_TBE;
332 0 : st->extl_brate = FB_TBE_1k8;
333 :
334 0 : if ( st->total_brate >= ACELP_24k40 )
335 : {
336 0 : st->extl_brate = FB_TBE_3k0;
337 : }
338 :
339 0 : break;
340 : }
341 604 : }
342 : else
343 : {
344 646 : st->coder_type = -1;
345 646 : st->extl = IGF_BWE;
346 646 : st->extl_brate = 0;
347 : }
348 :
349 1250 : st->core_brate = st->total_brate - st->extl_brate;
350 :
351 1250 : if ( st->tec_tfa == 1 )
352 : {
353 975 : st->core_brate -= BITS_TEC;
354 975 : st->core_brate -= BITS_TFA;
355 : }
356 : }
357 :
358 : /*----------------------------------------------------------------*
359 : * Complete Bitstream Writing
360 : *----------------------------------------------------------------*/
361 :
362 : /* Pad the bitstream with zeros and byte-alignment*/
363 1250 : if ( st->igf && st->core == ACELP_CORE && st->core_brate > SID_2k40 )
364 : {
365 604 : padBits = ( ( st->bits_frame + 7 ) / 8 ) * 8 - ( st->hBstr->nb_bits_tot + ( st->rf_target_bits_write - ( ( st->rf_mode == 1 ) ? 1 : 0 ) ) + get_tbe_bits( st->total_brate, st->bwidth, st->rf_mode ) );
366 : }
367 : else
368 : {
369 646 : padBits = ( ( st->bits_frame + 7 ) / 8 ) * 8 - ( st->hBstr->nb_bits_tot + ( st->rf_target_bits_write - ( ( st->rf_mode == 1 ) ? 1 : 0 ) ) );
370 : }
371 1565 : for ( i = 0; i < padBits; i++ )
372 : {
373 315 : push_next_indice( st->hBstr, 0, 1 );
374 : }
375 : }
376 :
377 : /*---------------------------------------------------------------------*
378 : * WB TBE encoding
379 : * WB BWE encoding
380 : *---------------------------------------------------------------------*/
381 3100 : push_wmops( "BWE_encoding" );
382 :
383 3100 : if ( st->input_Fs >= 16000 && st->bwidth < SWB )
384 : {
385 : /* Common pre-processing for WB TBE and WB BWE */
386 0 : wb_pre_proc( st, -1, new_inp_resamp16k, hb_speech );
387 : }
388 :
389 3100 : if ( st->extl == WB_TBE )
390 : {
391 : /* WB TBE encoder */
392 0 : wb_tbe_enc( st, hb_speech, bwe_exc_extended, voice_factors, pitch_buf );
393 :
394 0 : if ( st->codec_mode == MODE2 )
395 : {
396 0 : tbe_write_bitstream( st );
397 : }
398 : }
399 3100 : else if ( st->extl == WB_BWE )
400 : {
401 : /* WB BWE encoder */
402 0 : wb_bwe_enc( st, new_inp_resamp16k );
403 : }
404 :
405 : /*---------------------------------------------------------------------*
406 : * SWB(FB) TBE encoding
407 : * SWB(FB) BWE encoding
408 : *---------------------------------------------------------------------*/
409 :
410 3100 : if ( !st->Opt_SC_VBR && st->input_Fs >= 32000 )
411 : {
412 : /* Common pre-processing for SWB(FB) TBE and SWB(FB) BWE */
413 3100 : swb_pre_proc( st, new_swb_speech, shb_speech, realBuffer, imagBuffer, NULL );
414 : }
415 0 : else if ( st->input_Fs >= 32000 )
416 : {
417 0 : InitSWBencBufferStates( st->hBWE_TD, shb_speech );
418 : }
419 :
420 : /* SWB TBE encoder */
421 3100 : if ( st->extl == SWB_TBE || st->extl == FB_TBE || ( st->igf && st->core == ACELP_CORE && st->extl != WB_TBE ) )
422 : {
423 1339 : if ( st->core_brate != FRAME_NO_DATA && st->core_brate != SID_2k40 )
424 : {
425 1339 : swb_tbe_enc( st, NULL, shb_speech, bwe_exc_extended, voice_factors, fb_exc, pitch_buf );
426 :
427 1339 : if ( st->extl == FB_TBE )
428 : {
429 : /* FB TBE encoder */
430 0 : fb_tbe_enc( st, st->input, fb_exc );
431 : }
432 :
433 1339 : if ( st->codec_mode == MODE2 )
434 : {
435 604 : if ( st->tec_tfa == 1 )
436 : {
437 604 : tecEnc_TBE( &( st->hTECEnc->corrFlag ), st->voicing, st->coder_type );
438 :
439 604 : if ( st->coder_type == INACTIVE )
440 : {
441 0 : st->tec_flag = 0;
442 0 : st->hTECEnc->corrFlag = 0;
443 : }
444 604 : st->tfa_flag = tfaEnc_TBE( st->tfa_enr, st->last_core, st->voicing, pitch_buf );
445 604 : set_TEC_TFA_code( st->hTECEnc->corrFlag, &st->tec_flag, &st->tfa_flag );
446 : }
447 : else
448 : {
449 0 : st->tec_flag = 0;
450 0 : st->hTECEnc->corrFlag = 0;
451 0 : st->tfa_flag = 0;
452 : }
453 :
454 604 : tbe_write_bitstream( st );
455 : }
456 : }
457 : }
458 1761 : else if ( st->extl == SWB_BWE || st->extl == FB_BWE )
459 : {
460 : /* SWB(FB) BWE encoder */
461 6 : swb_bwe_enc( st, EVS_MONO, old_inp_12k8, old_inp_16k, old_syn_12k8_16k, new_swb_speech, shb_speech );
462 : }
463 1755 : else if ( st->extl == SWB_BWE_HIGHRATE || st->extl == FB_BWE_HIGHRATE )
464 : {
465 669 : swb_bwe_enc_hr( st, st->input - delay, input_frame, unbits );
466 : }
467 :
468 :
469 : /*---------------------------------------------------------------------*
470 : * SWB DTX/CNG encoding
471 : *---------------------------------------------------------------------*/
472 :
473 3100 : if ( st->Opt_DTX_ON && input_frame >= L_FRAME32k )
474 : {
475 : /* SHB DTX/CNG encoder */
476 0 : swb_CNG_enc( st, shb_speech, old_syn_12k8_16k );
477 : }
478 :
479 3100 : pop_wmops();
480 : /*---------------------------------------------------------------------*
481 : * Channel-aware mode - write signaling information into the bitstream
482 : *---------------------------------------------------------------------*/
483 :
484 3100 : signaling_enc_rf( st );
485 :
486 :
487 : /*---------------------------------------------------------------------*
488 : * Updates
489 : *---------------------------------------------------------------------*/
490 :
491 3100 : updt_enc_common( st );
492 :
493 3100 : if ( st->mdct_sw == MODE1 )
494 : {
495 275 : st->codec_mode = MODE1;
496 : }
497 :
498 :
499 3100 : if ( st->hTdCngEnc->lp_cng_mode2 )
500 : {
501 0 : st->codec_mode = MODE2;
502 : }
503 :
504 : #ifdef DEBUG_MODE_INFO
505 : dbgwrite( &st->codec_mode, sizeof( int16_t ), 1, input_frame, "res/codec" );
506 : dbgwrite( &st->core, sizeof( int16_t ), 1, input_frame, "res/core" );
507 : dbgwrite( &st->extl, sizeof( int16_t ), 1, input_frame, "res/extl" );
508 : dbgwrite( &st->bwidth, sizeof( int16_t ), 1, input_frame, "res/bwidth" );
509 : ener = st->total_brate / 1000.0f;
510 : dbgwrite( &ener, sizeof( float ), 1, input_frame, "res/total_brate" );
511 : ener = st->core_brate / 1000.0f;
512 : dbgwrite( &ener, sizeof( float ), 1, input_frame, "res/core_brate" );
513 : ener = st->extl_brate / 1000.0f;
514 : dbgwrite( &ener, sizeof( float ), 1, input_frame, "res/extl_brate" );
515 : dbgwrite( &st->coder_type, sizeof( int16_t ), 1, input_frame, "res/coder_type" );
516 : dbgwrite( &st->clas, sizeof( int16_t ), 1, input_frame, "res/clas" );
517 : dbgwrite( &st->cng_type, sizeof( int16_t ), 1, input_frame, "res/cng_type" );
518 : dbgwrite( &st->L_frame, sizeof( int16_t ), 1, input_frame, "res/L_frame" );
519 : dbgwrite( &st->vad_flag, sizeof( int16_t ), 1, input_frame, "res/vad_flag" );
520 :
521 : {
522 : int16_t _pitch[3];
523 : if ( ( st->coder_type == INACTIVE ) || ( st->coder_type == UNVOICED ) )
524 : {
525 : _pitch[0] = 0;
526 : _pitch[1] = 0;
527 : _pitch[2] = 0;
528 : }
529 : else
530 : {
531 : _pitch[0] = st->pitch[0];
532 : _pitch[1] = st->pitch[1];
533 : _pitch[2] = st->pitch[2];
534 : }
535 : dbgwrite( &_pitch[0], sizeof( int16_t ), 1, ( input_frame / 8 * 3 ), "res/pitch" );
536 : dbgwrite( &_pitch[1], sizeof( int16_t ), 1, ( input_frame / 8 * 3 ), "res/pitch" );
537 : dbgwrite( &_pitch[2], sizeof( int16_t ), 1, ( input_frame / 8 * 2 ), "res/pitch" );
538 : }
539 :
540 : if ( st->core != ACELP_CORE )
541 : {
542 : set_f( new_swb_speech, 0, input_frame );
543 : dbgwrite( new_swb_speech, 4, st->L_frame, 1, "res/exc.enc" );
544 : }
545 : #endif
546 :
547 3100 : pop_wmops();
548 :
549 3100 : return error;
550 : }
551 :
552 :
553 : /*-------------------------------------------------------------------*
554 : * initFrameHeader()
555 : *
556 : * Init Mode 2 frame header
557 : *-------------------------------------------------------------------*/
558 :
559 1325 : static void initFrameHeader(
560 : Encoder_State *st /* i/o: encoder state structure */
561 : )
562 : {
563 : int16_t n;
564 :
565 1325 : if ( st->core_brate == SID_2k40 )
566 : {
567 : /*Get size of frame*/
568 0 : st->bits_frame = FRAME_2_4;
569 0 : st->bits_frame_core += FRAME_2_4 - 4; /*1 bit for SID on/off + 2 bits for bandwith in case of SID + 1 bit CNG type */
570 0 : st->frame_size_index = 2;
571 : }
572 1325 : else if ( st->core_brate == FRAME_NO_DATA )
573 : {
574 0 : st->bits_frame = FRAME_0;
575 0 : st->bits_frame_core += st->bits_frame;
576 0 : st->frame_size_index = 0;
577 : }
578 : else
579 : {
580 10050 : for ( n = 0; n < FRAME_SIZE_NB; n++ )
581 : {
582 10050 : if ( n < FRAME_SIZE_NB - 1 )
583 : {
584 10050 : if ( FrameSizeConfig[n].frame_bits <= st->bits_frame_nominal && FrameSizeConfig[n + 1].frame_bits > st->bits_frame_nominal )
585 : {
586 1325 : st->frame_size_index = n;
587 1325 : st->bits_frame = st->bits_frame_nominal;
588 1325 : st->bits_frame_core = st->bits_frame_nominal - FrameSizeConfig[n].transmission_bits - FrameSizeConfig[n].bandwidth_bits - FrameSizeConfig[n].reserved_bits;
589 1325 : break;
590 : }
591 : }
592 : else
593 : {
594 0 : if ( FrameSizeConfig[n].frame_bits <= st->bits_frame_nominal )
595 : {
596 0 : st->frame_size_index = n;
597 0 : st->bits_frame = st->bits_frame_nominal;
598 0 : st->bits_frame_core = st->bits_frame_nominal - FrameSizeConfig[n].transmission_bits - FrameSizeConfig[n].bandwidth_bits - FrameSizeConfig[n].reserved_bits;
599 0 : break;
600 : }
601 : }
602 : }
603 : }
604 :
605 1325 : return;
606 : }
607 :
608 : /*-------------------------------------------------------------------*
609 : * writeFrameHeader()
610 : *
611 : * Write Mode 2 frame header
612 : *-------------------------------------------------------------------*/
613 :
614 1050 : static void writeFrameHeader(
615 : Encoder_State *st /* i/o: encoder state structure */
616 : )
617 : {
618 1050 : BSTR_ENC_HANDLE hBstr = st->hBstr;
619 :
620 1050 : if ( st->core_brate != FRAME_NO_DATA )
621 : {
622 : /* SID flag at 2.4kbps */
623 1050 : if ( st->core_brate == SID_2k40 )
624 : {
625 0 : if ( st->cng_type == FD_CNG )
626 : {
627 : /* write SID/CNG type flag */
628 0 : push_next_indice( hBstr, 1, 1 );
629 :
630 : /* write bandwidth info */
631 0 : push_next_indice( hBstr, st->bwidth, 2 );
632 :
633 : /* write L_frame */
634 0 : if ( st->L_frame == L_FRAME )
635 : {
636 0 : push_next_indice( hBstr, 0, 1 );
637 : }
638 : else
639 : {
640 0 : push_next_indice( hBstr, 1, 1 );
641 : }
642 : }
643 : }
644 : else /* active frames */
645 : {
646 1050 : if ( st->rf_mode == 0 )
647 : {
648 1050 : push_next_indice( hBstr, st->bwidth - FrameSizeConfig[st->frame_size_index].bandwidth_min, FrameSizeConfig[st->frame_size_index].bandwidth_bits );
649 : }
650 : }
651 :
652 : /* Write reserved bit */
653 1050 : if ( FrameSizeConfig[st->frame_size_index].reserved_bits && st->rf_mode == 0 )
654 : {
655 1050 : push_next_indice( hBstr, 0, FrameSizeConfig[st->frame_size_index].reserved_bits );
656 : }
657 : }
658 :
659 1050 : return;
660 : }
661 :
662 : /*------------------------------------------------------------------------*
663 : * Configuration of core coder/SID
664 : *------------------------------------------------------------------------*/
665 :
666 1250 : static void configure_core_coder(
667 : Encoder_State *st /* i/o: encoder state structure */
668 : )
669 : {
670 1250 : initFrameHeader( st );
671 :
672 1250 : if ( st->core_brate != SID_2k40 && st->core_brate != FRAME_NO_DATA )
673 : {
674 1250 : if ( st->tcxonly )
675 : {
676 0 : st->coder_type = GENERIC;
677 : }
678 :
679 1250 : st->hTcxCfg->coder_type = st->coder_type;
680 :
681 :
682 1250 : if ( !st->tcxonly && !st->localVAD && st->hTcxCfg->coder_type == GENERIC )
683 : {
684 43 : st->hTcxCfg->coder_type = UNVOICED;
685 : }
686 : }
687 :
688 1250 : st->igf = getIgfPresent( 0, st->total_brate, st->bwidth, st->rf_mode );
689 :
690 1250 : if ( st->core_brate != SID_2k40 && st->core_brate != FRAME_NO_DATA )
691 : {
692 1250 : st->core_brate = st->total_brate;
693 : }
694 :
695 1250 : return;
696 : }
|