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