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 1077972 : 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 1077972 : int16_t cldfb_bin_width = 4;
91 : int16_t bwd_count_wider_bw, l_frame;
92 :
93 1077972 : bwd_count_wider_bw = BWD_COUNT_WIDER_BW;
94 1077972 : if ( st->ini_frame > 0 && ( ( st->element_mode == IVAS_CPE_MDCT && ( st->element_brate >= BWD_MIN_BRATE_WIDER_BW_MDCT || mct_on ) ) ||
95 277446 : ( ivas_format == ISM_FORMAT && st->element_brate >= BWD_MIN_BRATE_WIDER_BW_ISM ) ) )
96 : {
97 807149 : bwd_count_wider_bw = BWD_COUNT_WIDER_BW_MDCT;
98 : }
99 :
100 1077972 : if ( st->input_Fs > 8000 )
101 : {
102 1077972 : if ( enerBuffer != NULL )
103 : {
104 : float ScalFac;
105 :
106 417483 : ScalFac = 1 / ( st->cldfbAnaEnc->scale * st->cldfbAnaEnc->scale * 8.f );
107 417483 : set_f( cldfb_bin, 0.001f, 9 );
108 :
109 : /* NB: 1.2 - 2.8 kHz, 4 cldfb-bands*/
110 417483 : 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 417483 : if ( st->input_Fs >= 16000 )
114 : {
115 417483 : cldfb_bin[1] += sum_f( &( enerBuffer[11] ), cldfb_bin_width );
116 417483 : cldfb_bin[2] += sum_f( &( enerBuffer[14] ), cldfb_bin_width );
117 : }
118 :
119 : /* SWB: 9.2 - 15.6 kHz, 16 cldfb-bands */
120 417483 : if ( st->input_Fs >= 32000 )
121 : {
122 399283 : cldfb_bin[3] += sum_f( &( enerBuffer[23] ), cldfb_bin_width );
123 399283 : cldfb_bin[4] += sum_f( &( enerBuffer[27] ), cldfb_bin_width );
124 399283 : cldfb_bin[5] += sum_f( &( enerBuffer[31] ), cldfb_bin_width );
125 399283 : cldfb_bin[6] += sum_f( &( enerBuffer[35] ), cldfb_bin_width );
126 : }
127 :
128 : /* FB: 16.8 - 20.0 kHz, 8 cldfb-bands */
129 417483 : if ( st->input_Fs >= 48000 )
130 : {
131 342705 : cldfb_bin[7] += sum_f( &( enerBuffer[42] ), cldfb_bin_width );
132 342705 : cldfb_bin[8] += sum_f( &( enerBuffer[46] ), cldfb_bin_width );
133 : }
134 :
135 4174830 : for ( i = 0; i < 9; i++ )
136 : {
137 3757347 : 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 660489 : if ( st->input_Fs == 16000 )
144 : {
145 41988 : bw_max = WB;
146 41988 : bin_width = 60;
147 41988 : n_bins = 5; /* spectrum to 7.5 kHz */
148 : }
149 618501 : else if ( st->input_Fs == 32000 )
150 : {
151 162646 : bw_max = SWB;
152 162646 : bin_width = 30;
153 162646 : n_bins = 10; /* spectrum to 15 kHz */
154 : }
155 : else /* st->input_Fs == 48000 */
156 : {
157 455855 : bw_max = FB;
158 455855 : bin_width = 20;
159 455855 : n_bins = BWD_N_BINS_MAX; /* spectrum to 19.5 kHz */
160 : }
161 :
162 660489 : if ( signal_in != NULL )
163 : {
164 : /*---------------------------------------------------------------------*
165 : * windowing of the input signal
166 : *---------------------------------------------------------------------*/
167 :
168 0 : pt = signal_in;
169 0 : pt1 = hann_window_320;
170 :
171 : /* 1st half of the window */
172 0 : for ( i = 0; i < BWD_TOTAL_WIDTH / 2; i++ )
173 : {
174 0 : in_win[i] = *pt++ * *pt1++;
175 : }
176 0 : pt1--;
177 :
178 : /* 2nd half of the window */
179 0 : for ( ; i < BWD_TOTAL_WIDTH; i++ )
180 : {
181 0 : in_win[i] = *pt++ * *pt1--;
182 : }
183 :
184 : /*---------------------------------------------------------------------*
185 : * tranform into frequency domain
186 : *---------------------------------------------------------------------*/
187 :
188 0 : edct( in_win, spect, BWD_TOTAL_WIDTH, st->element_mode );
189 : }
190 : else
191 : {
192 660489 : l_frame = (int16_t) ( st->input_Fs / FRAMES_PER_SEC );
193 660489 : if ( st->core == TCX_10_CORE )
194 : {
195 29622 : l_frame /= 2;
196 : }
197 :
198 660489 : bin_width *= ( l_frame / BWD_TOTAL_WIDTH );
199 660489 : mvr2r( spectrum, spect, l_frame );
200 : }
201 : /*---------------------------------------------------------------------*
202 : * compute energy per spectral bins
203 : *---------------------------------------------------------------------*/
204 :
205 660489 : set_f( spect_bin, 0.001f, n_bins );
206 :
207 3055823 : for ( k = 0; k <= bw_max; k++ )
208 : {
209 7762515 : for ( i = bwd_start_bin[k]; i <= bwd_end_bin[k]; i++ )
210 : {
211 318160501 : for ( j = 0; j < bin_width; j++ )
212 : {
213 312793320 : spect_bin[i] += spect[i * bin_width + j] * spect[i * bin_width + j];
214 : }
215 5367181 : spect_bin[i] = (float) log10( spect_bin[i] );
216 : }
217 : }
218 : }
219 :
220 1077972 : if ( enerBuffer != NULL )
221 : {
222 : /* cldfb detections */
223 417483 : mean_NB = mean( cldfb_bin, 1 ); /* NB: 1.2 - 2.8 kHz, 4 cldfb-bands (1 bin) */
224 417483 : maximum( cldfb_bin, 1, &max_NB );
225 417483 : mean_WB = mean( cldfb_bin + 1, 2 ); /* WB: 4.4 - 7.2 kHz, 8 cldfb-bands (2 bins) */
226 417483 : maximum( cldfb_bin + 1, 2, &max_WB );
227 :
228 417483 : mean_NB += CLDFB_ENER_OFFSET;
229 417483 : max_NB += CLDFB_ENER_OFFSET;
230 417483 : mean_WB += CLDFB_ENER_OFFSET;
231 417483 : max_WB += CLDFB_ENER_OFFSET;
232 :
233 417483 : if ( st->input_Fs == 16000 )
234 : {
235 : /* for 16kHz sampled inputs, do not check SWB & FB */
236 18200 : mean_SWB = 0.0f;
237 18200 : max_SWB = 0.0f;
238 18200 : mean_FB = 0.0f;
239 18200 : max_FB = 0.0f;
240 : }
241 399283 : else if ( st->input_Fs == 32000 )
242 : {
243 : /* for 32kHz sampled inputs, do not check FB */
244 56578 : mean_FB = 0.0f;
245 56578 : max_FB = 0.0f;
246 56578 : mean_SWB = mean( cldfb_bin + 3, 4 ); /* SWB: 9.2 - 15.6 kHz, 16 cldfb-bands (4 bins) */
247 56578 : maximum( cldfb_bin + 3, 4, &max_SWB );
248 56578 : mean_SWB += CLDFB_ENER_OFFSET;
249 56578 : max_SWB += CLDFB_ENER_OFFSET;
250 : }
251 : else
252 : {
253 342705 : mean_SWB = mean( cldfb_bin + 3, 4 ); /* SWB: 9.2 - 15.6 kHz, 16 cldfb-bands (4 bins) */
254 342705 : maximum( cldfb_bin + 3, 4, &max_SWB );
255 342705 : mean_FB = mean( cldfb_bin + 7, 2 ); /* FB: 16.8 - 20.0 kHz, 8 cldfb-bands (2 bins) */
256 342705 : maximum( cldfb_bin + 7, 2, &max_FB );
257 :
258 342705 : mean_SWB += CLDFB_ENER_OFFSET;
259 342705 : max_SWB += CLDFB_ENER_OFFSET;
260 342705 : mean_FB += CLDFB_ENER_OFFSET;
261 342705 : max_FB += CLDFB_ENER_OFFSET;
262 : }
263 : }
264 : else
265 : {
266 660489 : 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 660489 : maximum( spect_bin + bwd_start_bin[0], bwd_end_bin[0] - bwd_start_bin[0] + 1, &max_NB );
268 660489 : 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 660489 : maximum( spect_bin + bwd_start_bin[1], bwd_end_bin[1] - bwd_start_bin[1] + 1, &max_WB );
270 :
271 660489 : if ( st->input_Fs == 16000 )
272 : {
273 : /* for 16kHz sampled inputs, do not check SWB & FB */
274 41988 : mean_SWB = 0.0f;
275 41988 : max_SWB = 0.0f;
276 41988 : mean_FB = 0.0f;
277 41988 : max_FB = 0.0f;
278 : }
279 618501 : else if ( st->input_Fs == 32000 )
280 : {
281 162646 : 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 162646 : 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 162646 : mean_FB = 0.0f;
286 162646 : max_FB = 0.0f;
287 : }
288 : else
289 : {
290 455855 : 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 455855 : maximum( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1, &max_SWB );
292 455855 : 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 455855 : 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 1077972 : if ( st->localVAD || st->lp_noise > 30 )
302 : {
303 1009489 : st->lt_mean_NB = ALPHA_BWD * st->lt_mean_NB + ( 1 - ALPHA_BWD ) * mean_NB;
304 1009489 : st->lt_mean_WB = ALPHA_BWD * st->lt_mean_WB + ( 1 - ALPHA_BWD ) * mean_WB;
305 1009489 : st->lt_mean_SWB = ALPHA_BWD * st->lt_mean_SWB + ( 1 - ALPHA_BWD ) * mean_SWB;
306 :
307 1009489 : if ( enerBuffer != NULL )
308 : {
309 372386 : if ( 0.9f * max_WB > BWD_LT_THRESH * st->lt_mean_NB )
310 : {
311 258973 : if ( 2.5f * max_WB > max_NB )
312 : {
313 258748 : st->count_WB++;
314 : }
315 : }
316 : else
317 : {
318 113413 : if ( 3.5f * mean_WB < mean_NB )
319 : {
320 5547 : st->count_WB--;
321 : }
322 : }
323 :
324 372386 : if ( 0.83f * max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
325 : {
326 265913 : if ( 2 * max_SWB > max_WB )
327 : {
328 265271 : st->count_SWB++;
329 : }
330 : }
331 : else
332 : {
333 106473 : if ( 3 * mean_SWB < mean_WB )
334 : {
335 21954 : st->count_SWB--;
336 : }
337 : }
338 :
339 372386 : 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 187634 : if ( 3 * max_FB > max_SWB )
342 : {
343 187252 : st->count_FB++;
344 : }
345 : }
346 : else
347 : {
348 184752 : if ( 4.1f * mean_FB < mean_SWB )
349 : {
350 86182 : st->count_FB--;
351 : }
352 : }
353 : }
354 : else
355 : {
356 637103 : if ( max_WB > BWD_LT_THRESH * st->lt_mean_NB )
357 : {
358 548611 : if ( 2 * max_WB > max_NB )
359 : {
360 547292 : st->count_WB++;
361 : }
362 : }
363 : else
364 : {
365 88492 : if ( 2.6f * mean_WB < mean_NB )
366 : {
367 16691 : st->count_WB--;
368 : }
369 : }
370 :
371 637103 : if ( max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
372 : {
373 501339 : if ( 2 * max_SWB > max_WB )
374 : {
375 499329 : st->count_SWB++;
376 : }
377 : }
378 : else
379 : {
380 135764 : if ( 3 * mean_SWB < mean_WB )
381 : {
382 42108 : st->count_SWB--;
383 : }
384 : }
385 :
386 637103 : 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 346038 : if ( 2 * max_FB > max_SWB )
389 : {
390 342588 : st->count_FB++;
391 : }
392 : }
393 : else
394 : {
395 291065 : if ( 3 * mean_FB < mean_SWB )
396 : {
397 160264 : st->count_FB--;
398 : }
399 : }
400 : }
401 :
402 1009489 : st->count_WB = min( st->count_WB, BWD_COUNT_MAX );
403 1009489 : st->count_SWB = min( st->count_SWB, BWD_COUNT_MAX );
404 1009489 : st->count_FB = min( st->count_FB, BWD_COUNT_MAX );
405 1009489 : st->count_WB = max( st->count_WB, 0 );
406 1009489 : st->count_SWB = max( st->count_SWB, 0 );
407 1009489 : 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 1009489 : if ( st->last_input_bwidth == NB )
416 : {
417 1895 : if ( st->count_WB > bwd_count_wider_bw )
418 : {
419 42 : st->input_bwidth = WB;
420 42 : st->count_WB = BWD_COUNT_MAX;
421 :
422 42 : if ( st->count_SWB > bwd_count_wider_bw )
423 : {
424 17 : st->input_bwidth = SWB;
425 17 : st->count_SWB = BWD_COUNT_MAX;
426 :
427 17 : if ( st->count_FB > bwd_count_wider_bw )
428 : {
429 0 : st->input_bwidth = FB;
430 0 : st->count_FB = BWD_COUNT_MAX;
431 : }
432 : }
433 : }
434 : }
435 :
436 1009489 : if ( st->last_input_bwidth == WB && st->input_Fs > 16000 )
437 : {
438 28076 : if ( st->count_SWB > bwd_count_wider_bw )
439 : {
440 28059 : st->input_bwidth = SWB;
441 28059 : st->count_SWB = BWD_COUNT_MAX;
442 :
443 28059 : if ( st->count_FB > bwd_count_wider_bw )
444 : {
445 28042 : st->input_bwidth = FB;
446 28042 : st->count_FB = BWD_COUNT_MAX;
447 : }
448 : }
449 : }
450 :
451 1009489 : if ( st->last_input_bwidth == SWB && st->input_Fs > 32000 )
452 : {
453 122066 : if ( st->count_FB > bwd_count_wider_bw )
454 : {
455 109281 : st->input_bwidth = FB;
456 109281 : st->count_FB = BWD_COUNT_MAX;
457 : }
458 : }
459 :
460 : /* switching to a lower BW */
461 1009489 : if ( st->last_input_bwidth == FB )
462 : {
463 600402 : if ( st->count_FB < 10 )
464 : {
465 28 : st->input_bwidth = SWB;
466 28 : st->count_FB = 0;
467 : }
468 600402 : if ( st->count_SWB < 10 )
469 : {
470 0 : st->input_bwidth = WB;
471 0 : st->count_SWB = 0;
472 0 : st->count_FB = 0;
473 : }
474 600402 : if ( st->count_WB < 10 )
475 : {
476 21 : st->input_bwidth = NB;
477 21 : st->count_WB = 0;
478 21 : st->count_SWB = 0;
479 21 : st->count_FB = 0;
480 : }
481 : }
482 :
483 1009489 : if ( st->last_input_bwidth == SWB )
484 : {
485 321684 : if ( st->count_SWB < 10 )
486 : {
487 0 : st->input_bwidth = WB;
488 0 : st->count_SWB = 0;
489 0 : st->count_FB = 0;
490 : }
491 321684 : if ( st->count_WB < 10 )
492 : {
493 35 : st->input_bwidth = NB;
494 35 : st->count_WB = 0;
495 35 : st->count_SWB = 0;
496 35 : st->count_FB = 0;
497 : }
498 : }
499 :
500 1009489 : if ( st->last_input_bwidth == WB )
501 : {
502 85508 : if ( st->count_WB < 10 )
503 : {
504 12 : st->input_bwidth = NB;
505 12 : st->count_WB = 0;
506 12 : st->count_SWB = 0;
507 12 : st->count_FB = 0;
508 : }
509 : }
510 : }
511 : }
512 :
513 : /* verify that maximum encoded bandwidth (specified on the command line) is not exceeded */
514 1077972 : if ( st->input_bwidth > st->max_bwidth )
515 : {
516 137436 : st->input_bwidth = st->max_bwidth;
517 : }
518 :
519 1077972 : if ( st->element_mode == EVS_MONO )
520 : {
521 3100 : set_bw( -1, -1, st, st->codec_mode );
522 : }
523 :
524 1077972 : return;
525 : }
526 :
527 : /*-------------------------------------------------------------------*
528 : * set_bw()
529 : *
530 : * Set and limit the encoded bandwidth
531 : *-------------------------------------------------------------------*/
532 :
533 454107 : 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 454107 : st->bwidth = st->input_bwidth;
542 :
543 454107 : if ( codec_mode == MODE1 )
544 : {
545 : int32_t total_brate;
546 :
547 453057 : st->bwidth = st->input_bwidth;
548 453057 : total_brate = st->total_brate;
549 :
550 453057 : if ( element_mode > IVAS_SCE )
551 : {
552 67261 : if ( element_brate < MIN_BRATE_SWB_STEREO )
553 : {
554 0 : st->bwidth = WB;
555 : }
556 : else
557 : {
558 67261 : if ( st->idchan == 0 || element_mode == IVAS_CPE_MDCT )
559 : {
560 63470 : if ( element_brate >= MIN_BRATE_FB_STEREO )
561 : {
562 23350 : st->bwidth = min( st->bwidth, FB );
563 : }
564 : else
565 : {
566 40120 : st->bwidth = min( st->bwidth, SWB );
567 : }
568 63470 : st->bwidth = max( st->bwidth, WB );
569 : }
570 : else
571 : {
572 3791 : st->bwidth = WB;
573 : }
574 : }
575 : }
576 385796 : else if ( element_mode == IVAS_SCE )
577 : {
578 383746 : if ( element_brate < MIN_BRATE_SWB_SCE || st->bwidth < WB )
579 : {
580 9749 : st->bwidth = WB;
581 : }
582 373997 : else if ( st->bwidth > SWB && ( ( element_brate < MIN_BRATE_FB_STEREO && !st->is_ism_format ) ||
583 1358 : ( element_brate < MIN_BRATE_FB_ISM && st->is_ism_format ) ) )
584 : {
585 1983 : st->bwidth = SWB;
586 : }
587 372014 : else if ( element_brate > BWD_MAX_BRATE_WIDER_BW_ISM )
588 : {
589 28308 : st->bwidth = st->max_bwidth;
590 : }
591 : }
592 : /* element_mode == EVS_MONO */
593 2050 : else if ( total_brate <= ACELP_9k60 && st->bwidth > WB )
594 : {
595 0 : st->bwidth = WB;
596 : }
597 2050 : else if ( total_brate >= ACELP_13k20 && total_brate <= ACELP_16k40 && st->bwidth > SWB )
598 : {
599 0 : st->bwidth = SWB;
600 : }
601 2050 : else if ( total_brate >= ACELP_32k && st->bwidth < WB )
602 : {
603 0 : st->bwidth = WB;
604 : }
605 : }
606 1050 : else if ( codec_mode == MODE2 )
607 : {
608 : int16_t n, bits_frame_nominal, tmpBandwidthMin;
609 :
610 1050 : bits_frame_nominal = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
611 8400 : for ( n = 0; n < FRAME_SIZE_NB; n++ )
612 : {
613 8400 : if ( FrameSizeConfig[n].frame_bits == bits_frame_nominal )
614 : {
615 1050 : break;
616 : }
617 : }
618 1050 : if ( n == FRAME_SIZE_NB )
619 : {
620 0 : assert( !"Bitrate not supported: not part of EVS" );
621 : }
622 :
623 1050 : tmpBandwidthMin = FrameSizeConfig[n].bandwidth_min;
624 :
625 1050 : if ( st->rf_mode )
626 : {
627 0 : tmpBandwidthMin = WB;
628 : }
629 :
630 1050 : st->bwidth = max( min( st->input_bwidth, FrameSizeConfig[n].bandwidth_max ), tmpBandwidthMin );
631 : }
632 :
633 454107 : return;
634 : }
635 :
636 : /*-------------------------------------------------------------------*
637 : * set_bw_stereo()
638 : *
639 : * Set encoded bandwidth for stereo (CPE) channels
640 : *-------------------------------------------------------------------*/
641 :
642 87077 : void set_bw_stereo(
643 : CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structures */
644 : )
645 : {
646 87077 : Encoder_State **sts = hCPE->hCoreCoder;
647 :
648 87077 : if ( hCPE->element_brate > BWD_MAX_BRATE_WIDER_BW_MDCT )
649 : {
650 14930 : sts[0]->bwidth = sts[0]->max_bwidth;
651 14930 : sts[1]->bwidth = sts[1]->max_bwidth;
652 : }
653 72147 : else if ( hCPE->element_mode == IVAS_CPE_MDCT )
654 : {
655 : /* ensure that both CPE channels have the same audio band-width */
656 72147 : if ( sts[0]->input_bwidth == sts[1]->input_bwidth )
657 : {
658 71614 : sts[0]->bwidth = sts[0]->input_bwidth;
659 71614 : sts[1]->bwidth = sts[0]->input_bwidth;
660 : }
661 : else
662 : {
663 533 : sts[0]->bwidth = max( sts[0]->input_bwidth, sts[1]->input_bwidth );
664 533 : sts[1]->bwidth = max( sts[0]->input_bwidth, sts[1]->input_bwidth );
665 : }
666 : }
667 :
668 87077 : sts[0]->bwidth = max( sts[0]->bwidth, WB );
669 87077 : sts[1]->bwidth = max( sts[1]->bwidth, WB );
670 :
671 87077 : 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 100939 : 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 100939 : mct_bwidth = WB; /* minimum coded audio band-width */
691 100939 : last_mct_bwidth = hCPE[0]->hCoreCoder[0]->last_bwidth; /* supposes that LFE is not in the first channel */
692 :
693 371247 : for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
694 : {
695 810924 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
696 : {
697 540616 : st = hCPE[cpe_id]->hCoreCoder[ch];
698 540616 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
699 : {
700 58829 : continue;
701 : }
702 :
703 481787 : mct_bwidth = max( mct_bwidth, st->input_bwidth );
704 : }
705 : }
706 :
707 371247 : for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
708 : {
709 270308 : if ( hCPE[cpe_id]->element_brate > BWD_MAX_BRATE_WIDER_BW_MDCT )
710 : {
711 222148 : mct_bwidth = max( mct_bwidth, hCPE[cpe_id]->hCoreCoder[0]->max_bwidth );
712 : }
713 : }
714 :
715 100939 : bw_changed = 0;
716 100939 : if ( mct_bwidth != last_mct_bwidth )
717 : {
718 179 : 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 371247 : for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
727 : {
728 810924 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
729 : {
730 540616 : st = hCPE[cpe_id]->hCoreCoder[ch];
731 540616 : st->bwidth = mct_bwidth;
732 : }
733 : }
734 :
735 100939 : return bw_changed;
736 : }
|