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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include <math.h>
44 : #include "cnst.h"
45 : #include "rom_enc.h"
46 : #include "rom_com.h"
47 : #include "prot.h"
48 : #include "ivas_prot.h"
49 : #include "wmc_auto.h"
50 :
51 : /*-------------------------------------------------------------------*
52 : * Local constants
53 : *-------------------------------------------------------------------*/
54 :
55 : #define BWD_MIN_BRATE_WIDER_BW_MDCT IVAS_48k
56 : #ifdef FIX_1381_BWD
57 : #define BWD_MIN_BRATE_WIDER_BW_MASA IVAS_48k
58 : #endif
59 : #define BWD_MIN_BRATE_WIDER_BW_ISM IVAS_32k
60 : #define BWD_MAX_BRATE_WIDER_BW_MDCT IVAS_80k
61 : #define BWD_MAX_BRATE_WIDER_BW_ISM IVAS_64k
62 :
63 : #define ALPHA_BWD 0.75f
64 : #define BWD_LT_THRESH 0.6f
65 :
66 : #define BWD_COUNT_MAX 100
67 : #define BWD_COUNT_WIDER_BW 10
68 : #define BWD_COUNT_WIDER_BW_MDCT 0
69 :
70 : #define CLDFB_ENER_OFFSET 1.6f
71 :
72 : /*-------------------------------------------------------------------*
73 : * bw_detect()
74 : *
75 : * bandwidth detector
76 : *-------------------------------------------------------------------*/
77 :
78 812262 : void bw_detect(
79 : Encoder_State *st, /* i/o: Encoder State */
80 : const float signal_in[], /* i : input signal */
81 : float *spectrum, /* i : MDCT spectrum */
82 : const float *enerBuffer, /* i : energy buffer */
83 : const IVAS_FORMAT ivas_format, /* i : IVAS format */
84 : const int16_t mct_on /* i : flag MCT mode */
85 : )
86 : {
87 : int16_t i, j, k, bw_max, bin_width, n_bins;
88 : float spect[L_FRAME48k], in_win[BWD_TOTAL_WIDTH];
89 : float spect_bin[BWD_N_BINS_MAX];
90 : float cldfb_bin[9];
91 : const float *pt, *pt1;
92 : float max_NB, max_WB, max_SWB, max_FB, mean_NB, mean_WB, mean_SWB, mean_FB;
93 812262 : int16_t cldfb_bin_width = 4;
94 : int16_t bwd_count_wider_bw, l_frame;
95 :
96 812262 : bwd_count_wider_bw = BWD_COUNT_WIDER_BW;
97 812262 : if ( st->ini_frame > 0 && ( ( st->element_mode == IVAS_CPE_MDCT && ( st->element_brate >= BWD_MIN_BRATE_WIDER_BW_MDCT || mct_on ) ) ||
98 : #ifdef FIX_1381_BWD
99 321744 : ( st->is_ism_format && st->element_brate >= BWD_MIN_BRATE_WIDER_BW_ISM ) ||
100 20222 : ( ivas_format == MASA_FORMAT && st->element_brate >= BWD_MIN_BRATE_WIDER_BW_MASA ) ) )
101 : #else
102 : ( ivas_format == ISM_FORMAT && st->element_brate >= BWD_MIN_BRATE_WIDER_BW_ISM ) ) )
103 : #endif
104 : {
105 599966 : bwd_count_wider_bw = BWD_COUNT_WIDER_BW_MDCT;
106 : }
107 :
108 812262 : if ( st->input_Fs > 8000 )
109 : {
110 812262 : if ( enerBuffer != NULL )
111 : {
112 : float ScalFac;
113 :
114 327317 : ScalFac = 1 / ( st->cldfbAnaEnc->scale * st->cldfbAnaEnc->scale * 8.f );
115 327317 : set_f( cldfb_bin, 0.001f, 9 );
116 :
117 : /* NB: 1.2 - 2.8 kHz, 4 cldfb-bands*/
118 327317 : cldfb_bin[0] += sum_f( &( enerBuffer[3] ), cldfb_bin_width );
119 :
120 : /* WB: 4.4 - 7.2 kHz, 8 cldfb-bands, mid band(14) counted twice */
121 327317 : if ( st->input_Fs >= 16000 )
122 : {
123 327317 : cldfb_bin[1] += sum_f( &( enerBuffer[11] ), cldfb_bin_width );
124 327317 : cldfb_bin[2] += sum_f( &( enerBuffer[14] ), cldfb_bin_width );
125 : }
126 :
127 : /* SWB: 9.2 - 15.6 kHz, 16 cldfb-bands */
128 327317 : if ( st->input_Fs >= 32000 )
129 : {
130 315367 : cldfb_bin[3] += sum_f( &( enerBuffer[23] ), cldfb_bin_width );
131 315367 : cldfb_bin[4] += sum_f( &( enerBuffer[27] ), cldfb_bin_width );
132 315367 : cldfb_bin[5] += sum_f( &( enerBuffer[31] ), cldfb_bin_width );
133 315367 : cldfb_bin[6] += sum_f( &( enerBuffer[35] ), cldfb_bin_width );
134 : }
135 :
136 : /* FB: 16.8 - 20.0 kHz, 8 cldfb-bands */
137 327317 : if ( st->input_Fs >= 48000 )
138 : {
139 289787 : cldfb_bin[7] += sum_f( &( enerBuffer[42] ), cldfb_bin_width );
140 289787 : cldfb_bin[8] += sum_f( &( enerBuffer[46] ), cldfb_bin_width );
141 : }
142 :
143 3273170 : for ( i = 0; i < 9; i++ )
144 : {
145 2945853 : cldfb_bin[i] = (float) log10( cldfb_bin[i] * ScalFac ); /* see formula used in perform_noise_estimation_enc() for CNG */
146 : }
147 : }
148 : else
149 : {
150 : /* set width of a speactral bin (corresponds to 1.5kHz) */
151 484945 : if ( st->input_Fs == 16000 )
152 : {
153 30607 : bw_max = WB;
154 30607 : bin_width = 60;
155 30607 : n_bins = 5; /* spectrum to 7.5 kHz */
156 : }
157 454338 : else if ( st->input_Fs == 32000 )
158 : {
159 96674 : bw_max = SWB;
160 96674 : bin_width = 30;
161 96674 : n_bins = 10; /* spectrum to 15 kHz */
162 : }
163 : else /* st->input_Fs == 48000 */
164 : {
165 357664 : bw_max = FB;
166 357664 : bin_width = 20;
167 357664 : n_bins = BWD_N_BINS_MAX; /* spectrum to 19.5 kHz */
168 : }
169 :
170 484945 : if ( signal_in != NULL )
171 : {
172 : /*---------------------------------------------------------------------*
173 : * windowing of the input signal
174 : *---------------------------------------------------------------------*/
175 :
176 0 : pt = signal_in;
177 0 : pt1 = hann_window_320;
178 :
179 : /* 1st half of the window */
180 0 : for ( i = 0; i < BWD_TOTAL_WIDTH / 2; i++ )
181 : {
182 0 : in_win[i] = *pt++ * *pt1++;
183 : }
184 0 : pt1--;
185 :
186 : /* 2nd half of the window */
187 0 : for ( ; i < BWD_TOTAL_WIDTH; i++ )
188 : {
189 0 : in_win[i] = *pt++ * *pt1--;
190 : }
191 :
192 : /*---------------------------------------------------------------------*
193 : * tranform into frequency domain
194 : *---------------------------------------------------------------------*/
195 :
196 0 : edct( in_win, spect, BWD_TOTAL_WIDTH, st->element_mode );
197 : }
198 : else
199 : {
200 484945 : l_frame = (int16_t) ( st->input_Fs / FRAMES_PER_SEC );
201 484945 : if ( st->core == TCX_10_CORE )
202 : {
203 22550 : l_frame /= 2;
204 : }
205 :
206 484945 : bin_width *= ( l_frame / BWD_TOTAL_WIDTH );
207 484945 : mvr2r( spectrum, spect, l_frame );
208 : }
209 : /*---------------------------------------------------------------------*
210 : * compute energy per spectral bins
211 : *---------------------------------------------------------------------*/
212 :
213 484945 : set_f( spect_bin, 0.001f, n_bins );
214 :
215 2266837 : for ( k = 0; k <= bw_max; k++ )
216 : {
217 5769407 : for ( i = bwd_start_bin[k]; i <= bwd_end_bin[k]; i++ )
218 : {
219 236109335 : for ( j = 0; j < bin_width; j++ )
220 : {
221 232121820 : spect_bin[i] += spect[i * bin_width + j] * spect[i * bin_width + j];
222 : }
223 3987515 : spect_bin[i] = (float) log10( spect_bin[i] );
224 : }
225 : }
226 : }
227 :
228 812262 : if ( enerBuffer != NULL )
229 : {
230 : /* cldfb detections */
231 327317 : mean_NB = mean( cldfb_bin, 1 ); /* NB: 1.2 - 2.8 kHz, 4 cldfb-bands (1 bin) */
232 327317 : maximum( cldfb_bin, 1, &max_NB );
233 327317 : mean_WB = mean( cldfb_bin + 1, 2 ); /* WB: 4.4 - 7.2 kHz, 8 cldfb-bands (2 bins) */
234 327317 : maximum( cldfb_bin + 1, 2, &max_WB );
235 :
236 327317 : mean_NB += CLDFB_ENER_OFFSET;
237 327317 : max_NB += CLDFB_ENER_OFFSET;
238 327317 : mean_WB += CLDFB_ENER_OFFSET;
239 327317 : max_WB += CLDFB_ENER_OFFSET;
240 :
241 327317 : if ( st->input_Fs == 16000 )
242 : {
243 : /* for 16kHz sampled inputs, do not check SWB & FB */
244 11950 : mean_SWB = 0.0f;
245 11950 : max_SWB = 0.0f;
246 11950 : mean_FB = 0.0f;
247 11950 : max_FB = 0.0f;
248 : }
249 315367 : else if ( st->input_Fs == 32000 )
250 : {
251 : /* for 32kHz sampled inputs, do not check FB */
252 25580 : mean_FB = 0.0f;
253 25580 : max_FB = 0.0f;
254 25580 : mean_SWB = mean( cldfb_bin + 3, 4 ); /* SWB: 9.2 - 15.6 kHz, 16 cldfb-bands (4 bins) */
255 25580 : maximum( cldfb_bin + 3, 4, &max_SWB );
256 25580 : mean_SWB += CLDFB_ENER_OFFSET;
257 25580 : max_SWB += CLDFB_ENER_OFFSET;
258 : }
259 : else
260 : {
261 289787 : mean_SWB = mean( cldfb_bin + 3, 4 ); /* SWB: 9.2 - 15.6 kHz, 16 cldfb-bands (4 bins) */
262 289787 : maximum( cldfb_bin + 3, 4, &max_SWB );
263 289787 : mean_FB = mean( cldfb_bin + 7, 2 ); /* FB: 16.8 - 20.0 kHz, 8 cldfb-bands (2 bins) */
264 289787 : maximum( cldfb_bin + 7, 2, &max_FB );
265 :
266 289787 : mean_SWB += CLDFB_ENER_OFFSET;
267 289787 : max_SWB += CLDFB_ENER_OFFSET;
268 289787 : mean_FB += CLDFB_ENER_OFFSET;
269 289787 : max_FB += CLDFB_ENER_OFFSET;
270 : }
271 : }
272 : else
273 : {
274 484945 : mean_NB = mean( spect_bin + bwd_start_bin[0], bwd_end_bin[0] - bwd_start_bin[0] + 1 ); /* NB: 1.5-3.0kHz (1 bin) */
275 484945 : maximum( spect_bin + bwd_start_bin[0], bwd_end_bin[0] - bwd_start_bin[0] + 1, &max_NB );
276 484945 : mean_WB = mean( spect_bin + bwd_start_bin[1], bwd_end_bin[1] - bwd_start_bin[1] + 1 ); /* WB: 4.5-7.5kHz (2 bins) */
277 484945 : maximum( spect_bin + bwd_start_bin[1], bwd_end_bin[1] - bwd_start_bin[1] + 1, &max_WB );
278 :
279 484945 : if ( st->input_Fs == 16000 )
280 : {
281 : /* for 16kHz sampled inputs, do not check SWB & FB */
282 30607 : mean_SWB = 0.0f;
283 30607 : max_SWB = 0.0f;
284 30607 : mean_FB = 0.0f;
285 30607 : max_FB = 0.0f;
286 : }
287 454338 : else if ( st->input_Fs == 32000 )
288 : {
289 96674 : mean_SWB = mean( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1 ); /* SWB: 9.0-15.0kHz (4 bins) */
290 96674 : maximum( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1, &max_SWB );
291 :
292 : /* for 32kHz sampled inputs, do not check FB */
293 96674 : mean_FB = 0.0f;
294 96674 : max_FB = 0.0f;
295 : }
296 : else
297 : {
298 357664 : mean_SWB = mean( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1 ); /* SWB: 9.0-15.0kHz (4 bins) */
299 357664 : maximum( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1, &max_SWB );
300 357664 : mean_FB = mean( spect_bin + bwd_start_bin[3], bwd_end_bin[3] - bwd_start_bin[3] + 1 ); /* FB: 16.5-19.5kHz (2 bins) */
301 357664 : maximum( spect_bin + bwd_start_bin[3], bwd_end_bin[3] - bwd_start_bin[3] + 1, &max_FB );
302 : }
303 : }
304 :
305 : /*---------------------------------------------------------------------*
306 : * update LT counters and energies
307 : *---------------------------------------------------------------------*/
308 :
309 812262 : if ( st->localVAD || st->lp_noise > 30 )
310 : {
311 747562 : st->lt_mean_NB = ALPHA_BWD * st->lt_mean_NB + ( 1 - ALPHA_BWD ) * mean_NB;
312 747562 : st->lt_mean_WB = ALPHA_BWD * st->lt_mean_WB + ( 1 - ALPHA_BWD ) * mean_WB;
313 747562 : st->lt_mean_SWB = ALPHA_BWD * st->lt_mean_SWB + ( 1 - ALPHA_BWD ) * mean_SWB;
314 :
315 747562 : if ( enerBuffer != NULL )
316 : {
317 284574 : if ( 0.9f * max_WB > BWD_LT_THRESH * st->lt_mean_NB )
318 : {
319 204986 : if ( 2.5f * max_WB > max_NB )
320 : {
321 204833 : st->count_WB++;
322 : }
323 : }
324 : else
325 : {
326 79588 : if ( 3.5f * mean_WB < mean_NB )
327 : {
328 2629 : st->count_WB--;
329 : }
330 : }
331 :
332 284574 : if ( 0.83f * max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
333 : {
334 207750 : if ( 2 * max_SWB > max_WB )
335 : {
336 207369 : st->count_SWB++;
337 : }
338 : }
339 : else
340 : {
341 76824 : if ( 3 * mean_SWB < mean_WB )
342 : {
343 15022 : st->count_SWB--;
344 : }
345 : }
346 :
347 284574 : if ( max_FB > BWD_LT_THRESH * st->lt_mean_SWB && 0.83f * max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
348 : {
349 147705 : if ( 3 * max_FB > max_SWB )
350 : {
351 147350 : st->count_FB++;
352 : }
353 : }
354 : else
355 : {
356 136869 : if ( 4.1f * mean_FB < mean_SWB )
357 : {
358 59706 : st->count_FB--;
359 : }
360 : }
361 : }
362 : else
363 : {
364 462988 : if ( max_WB > BWD_LT_THRESH * st->lt_mean_NB )
365 : {
366 409151 : if ( 2 * max_WB > max_NB )
367 : {
368 408354 : st->count_WB++;
369 : }
370 : }
371 : else
372 : {
373 53837 : if ( 2.6f * mean_WB < mean_NB )
374 : {
375 7700 : st->count_WB--;
376 : }
377 : }
378 :
379 462988 : if ( max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
380 : {
381 374282 : if ( 2 * max_SWB > max_WB )
382 : {
383 372943 : st->count_SWB++;
384 : }
385 : }
386 : else
387 : {
388 88706 : if ( 3 * mean_SWB < mean_WB )
389 : {
390 29714 : st->count_SWB--;
391 : }
392 : }
393 :
394 462988 : if ( max_FB > BWD_LT_THRESH * st->lt_mean_SWB && max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
395 : {
396 275406 : if ( 2 * max_FB > max_SWB )
397 : {
398 272720 : st->count_FB++;
399 : }
400 : }
401 : else
402 : {
403 187582 : if ( 3 * mean_FB < mean_SWB )
404 : {
405 94913 : st->count_FB--;
406 : }
407 : }
408 : }
409 :
410 747562 : st->count_WB = min( st->count_WB, BWD_COUNT_MAX );
411 747562 : st->count_SWB = min( st->count_SWB, BWD_COUNT_MAX );
412 747562 : st->count_FB = min( st->count_FB, BWD_COUNT_MAX );
413 747562 : st->count_WB = max( st->count_WB, 0 );
414 747562 : st->count_SWB = max( st->count_SWB, 0 );
415 747562 : st->count_FB = max( st->count_FB, 0 );
416 :
417 : /*---------------------------------------------------------------------*
418 : * check against thresholds
419 : * detect a band-width change
420 : *---------------------------------------------------------------------*/
421 :
422 : /* switching to a higher BW */
423 747562 : if ( st->last_input_bwidth == NB )
424 : {
425 485 : if ( st->count_WB > bwd_count_wider_bw )
426 : {
427 6 : st->input_bwidth = WB;
428 6 : st->count_WB = BWD_COUNT_MAX;
429 :
430 6 : if ( st->count_SWB > bwd_count_wider_bw )
431 : {
432 2 : st->input_bwidth = SWB;
433 2 : st->count_SWB = BWD_COUNT_MAX;
434 :
435 2 : if ( st->count_FB > bwd_count_wider_bw )
436 : {
437 0 : st->input_bwidth = FB;
438 0 : st->count_FB = BWD_COUNT_MAX;
439 : }
440 : }
441 : }
442 : }
443 :
444 747562 : if ( st->last_input_bwidth == WB && st->input_Fs > 16000 )
445 : {
446 30453 : if ( st->count_SWB > bwd_count_wider_bw )
447 : {
448 30451 : st->input_bwidth = SWB;
449 30451 : st->count_SWB = BWD_COUNT_MAX;
450 :
451 30451 : if ( st->count_FB > bwd_count_wider_bw )
452 : {
453 30449 : st->input_bwidth = FB;
454 30449 : st->count_FB = BWD_COUNT_MAX;
455 : }
456 : }
457 : }
458 :
459 747562 : if ( st->last_input_bwidth == SWB && st->input_Fs > 32000 )
460 : {
461 94662 : if ( st->count_FB > bwd_count_wider_bw )
462 : {
463 85607 : st->input_bwidth = FB;
464 85607 : st->count_FB = BWD_COUNT_MAX;
465 : }
466 : }
467 :
468 : /* switching to a lower BW */
469 747562 : if ( st->last_input_bwidth == FB )
470 : {
471 473123 : if ( st->count_FB < 10 )
472 : {
473 20 : st->input_bwidth = SWB;
474 20 : st->count_FB = 0;
475 : }
476 473123 : if ( st->count_SWB < 10 )
477 : {
478 0 : st->input_bwidth = WB;
479 0 : st->count_SWB = 0;
480 0 : st->count_FB = 0;
481 : }
482 473123 : if ( st->count_WB < 10 )
483 : {
484 3 : st->input_bwidth = NB;
485 3 : st->count_WB = 0;
486 3 : st->count_SWB = 0;
487 3 : st->count_FB = 0;
488 : }
489 : }
490 :
491 747562 : if ( st->last_input_bwidth == SWB )
492 : {
493 202685 : if ( st->count_SWB < 10 )
494 : {
495 0 : st->input_bwidth = WB;
496 0 : st->count_SWB = 0;
497 0 : st->count_FB = 0;
498 : }
499 202685 : if ( st->count_WB < 10 )
500 : {
501 7 : st->input_bwidth = NB;
502 7 : st->count_WB = 0;
503 7 : st->count_SWB = 0;
504 7 : st->count_FB = 0;
505 : }
506 : }
507 :
508 747562 : if ( st->last_input_bwidth == WB )
509 : {
510 71269 : if ( st->count_WB < 10 )
511 : {
512 3 : st->input_bwidth = NB;
513 3 : st->count_WB = 0;
514 3 : st->count_SWB = 0;
515 3 : st->count_FB = 0;
516 : }
517 : }
518 : }
519 : }
520 :
521 : /* verify that maximum encoded bandwidth (specified on the command line) is not exceeded */
522 812262 : if ( st->input_bwidth > st->max_bwidth )
523 : {
524 116190 : st->input_bwidth = st->max_bwidth;
525 : }
526 :
527 812262 : if ( st->element_mode == EVS_MONO )
528 : {
529 5150 : set_bw( -1, -1, st, st->codec_mode );
530 : }
531 :
532 812262 : return;
533 : }
534 :
535 : /*-------------------------------------------------------------------*
536 : * set_bw()
537 : *
538 : * Set and limit the encoded bandwidth
539 : *-------------------------------------------------------------------*/
540 :
541 362876 : void set_bw(
542 : const int16_t element_mode, /* i : element mode */
543 : const int32_t element_brate, /* i : element bitrate */
544 : Encoder_State *st, /* i/o: Encoder State */
545 : const int16_t codec_mode /* i : codec mode */
546 : )
547 : {
548 : /* initialization */
549 362876 : st->bwidth = st->input_bwidth;
550 :
551 362876 : if ( codec_mode == MODE1 )
552 : {
553 : int32_t total_brate;
554 :
555 360776 : st->bwidth = st->input_bwidth;
556 360776 : total_brate = st->total_brate;
557 :
558 360776 : if ( element_mode > IVAS_SCE )
559 : {
560 54476 : if ( element_brate < MIN_BRATE_SWB_STEREO )
561 : {
562 0 : st->bwidth = WB;
563 : }
564 : else
565 : {
566 54476 : if ( st->idchan == 0 || element_mode == IVAS_CPE_MDCT )
567 : {
568 52414 : if ( element_brate >= MIN_BRATE_FB_STEREO )
569 : {
570 16898 : st->bwidth = min( st->bwidth, FB );
571 : }
572 : else
573 : {
574 35516 : st->bwidth = min( st->bwidth, SWB );
575 : }
576 52414 : st->bwidth = max( st->bwidth, WB );
577 : }
578 : else
579 : {
580 2062 : st->bwidth = WB;
581 : }
582 : }
583 : }
584 306300 : else if ( element_mode == IVAS_SCE )
585 : {
586 303250 : if ( element_brate < MIN_BRATE_SWB_SCE || st->bwidth < WB )
587 : {
588 4013 : st->bwidth = WB;
589 : }
590 299237 : else if ( st->bwidth > SWB && ( ( element_brate < MIN_BRATE_FB_STEREO && !st->is_ism_format ) ||
591 5876 : ( element_brate < MIN_BRATE_FB_ISM && st->is_ism_format ) ) )
592 : {
593 6301 : st->bwidth = SWB;
594 : }
595 292936 : else if ( element_brate > BWD_MAX_BRATE_WIDER_BW_ISM )
596 : {
597 28166 : st->bwidth = st->max_bwidth;
598 : }
599 : }
600 : /* element_mode == EVS_MONO */
601 3050 : else if ( total_brate <= ACELP_9k60 && st->bwidth > WB )
602 : {
603 0 : st->bwidth = WB;
604 : }
605 3050 : else if ( total_brate >= ACELP_13k20 && total_brate <= ACELP_16k40 && st->bwidth > SWB )
606 : {
607 0 : st->bwidth = SWB;
608 : }
609 3050 : else if ( total_brate >= ACELP_32k && st->bwidth < WB )
610 : {
611 0 : st->bwidth = WB;
612 : }
613 : }
614 2100 : else if ( codec_mode == MODE2 )
615 : {
616 : int16_t n, bits_frame_nominal, tmpBandwidthMin;
617 :
618 2100 : bits_frame_nominal = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
619 16800 : for ( n = 0; n < FRAME_SIZE_NB; n++ )
620 : {
621 16800 : if ( FrameSizeConfig[n].frame_bits == bits_frame_nominal )
622 : {
623 2100 : break;
624 : }
625 : }
626 2100 : if ( n == FRAME_SIZE_NB )
627 : {
628 0 : assert( !"Bitrate not supported: not part of EVS" );
629 : }
630 :
631 2100 : tmpBandwidthMin = FrameSizeConfig[n].bandwidth_min;
632 :
633 2100 : if ( st->rf_mode )
634 : {
635 0 : tmpBandwidthMin = WB;
636 : }
637 :
638 2100 : st->bwidth = max( min( st->input_bwidth, FrameSizeConfig[n].bandwidth_max ), tmpBandwidthMin );
639 : }
640 :
641 362876 : return;
642 : }
643 :
644 : /*-------------------------------------------------------------------*
645 : * set_bw_stereo()
646 : *
647 : * Set encoded bandwidth for stereo (CPE) channels
648 : *-------------------------------------------------------------------*/
649 :
650 68460 : void set_bw_stereo(
651 : CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structures */
652 : )
653 : {
654 68460 : Encoder_State **sts = hCPE->hCoreCoder;
655 :
656 68460 : if ( hCPE->element_brate > BWD_MAX_BRATE_WIDER_BW_MDCT )
657 : {
658 24579 : sts[0]->bwidth = sts[0]->max_bwidth;
659 24579 : sts[1]->bwidth = sts[1]->max_bwidth;
660 : }
661 43881 : else if ( hCPE->element_mode == IVAS_CPE_MDCT )
662 : {
663 : /* ensure that both CPE channels have the same audio band-width */
664 43881 : if ( sts[0]->input_bwidth == sts[1]->input_bwidth )
665 : {
666 42978 : sts[0]->bwidth = sts[0]->input_bwidth;
667 42978 : sts[1]->bwidth = sts[0]->input_bwidth;
668 : }
669 : else
670 : {
671 903 : sts[0]->bwidth = max( sts[0]->input_bwidth, sts[1]->input_bwidth );
672 903 : sts[1]->bwidth = max( sts[0]->input_bwidth, sts[1]->input_bwidth );
673 : }
674 : }
675 :
676 68460 : sts[0]->bwidth = max( sts[0]->bwidth, WB );
677 68460 : sts[1]->bwidth = max( sts[1]->bwidth, WB );
678 :
679 68460 : return;
680 : }
681 :
682 : /*-------------------------------------------------------------------*
683 : * set_bw_mct()
684 : *
685 : * Set encoded bandwidth for MCT
686 : *-------------------------------------------------------------------*/
687 :
688 : /*! r: flag indicating whether the coded BW has changed */
689 69829 : int16_t set_bw_mct(
690 : CPE_ENC_HANDLE hCPE[MCT_MAX_BLOCKS], /* i/o: CPE encoder structures */
691 : const int16_t nCPE /* i : number of CPEs */
692 : )
693 : {
694 : Encoder_State *st;
695 : int16_t ch, cpe_id;
696 : int16_t mct_bwidth, last_mct_bwidth, bw_changed;
697 :
698 69829 : mct_bwidth = WB; /* minimum coded audio band-width */
699 69829 : last_mct_bwidth = hCPE[0]->hCoreCoder[0]->last_bwidth; /* supposes that LFE is not in the first channel */
700 :
701 256267 : for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
702 : {
703 559314 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
704 : {
705 372876 : st = hCPE[cpe_id]->hCoreCoder[ch];
706 372876 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
707 : {
708 33579 : continue;
709 : }
710 :
711 339297 : mct_bwidth = max( mct_bwidth, st->input_bwidth );
712 : }
713 : }
714 :
715 256267 : for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
716 : {
717 186438 : if ( hCPE[cpe_id]->element_brate > BWD_MAX_BRATE_WIDER_BW_MDCT )
718 : {
719 154798 : mct_bwidth = max( mct_bwidth, hCPE[cpe_id]->hCoreCoder[0]->max_bwidth );
720 : }
721 : }
722 :
723 69829 : bw_changed = 0;
724 69829 : if ( mct_bwidth != last_mct_bwidth )
725 : {
726 179 : bw_changed = 1;
727 : }
728 :
729 : /*
730 : * always set bw for all CPEs even if it is the same value as before,
731 : * in case of bw + br switching when changing to MCT, this overwrites
732 : * potentially incorrect initial values
733 : */
734 256267 : for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
735 : {
736 559314 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
737 : {
738 372876 : st = hCPE[cpe_id]->hCoreCoder[ch];
739 372876 : st->bwidth = mct_bwidth;
740 : }
741 : }
742 :
743 69829 : return bw_changed;
744 : }
|