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 : #include <stdint.h>
34 : #include "options.h"
35 : #ifdef DEBUGGING
36 : #include "debug.h"
37 : #endif
38 : #include "cnst.h"
39 : #include "stat_enc.h"
40 : #include "rom_com.h"
41 : #include "ivas_prot.h"
42 : #include "ivas_rom_com.h"
43 : #include "ivas_cnst.h"
44 : #include "prot.h"
45 : #include "wmc_auto.h"
46 :
47 :
48 : /*-------------------------------------------------------------------*
49 : * Local constants
50 : *-------------------------------------------------------------------*/
51 :
52 : #define NB_RATE_POSS 10 /* Nmbr of possible FCB bitrate for half rate mode*/
53 : #define MIN_SEC_ACB_RATE ( 2 * 8 * FRAMES_PER_SEC ) /* 2 subfr ACB bitrate */
54 : #define MIN_SEC_LPC_RATE ( ( 24 + 2 ) * FRAMES_PER_SEC ) /* LPC min rate */
55 :
56 : #define MAX_SC_FCB_RATE ( 24 + 1 ) /* Maximum bit for a FCB subfr */
57 : #define MIN_4SUBFR_FCB_RATE 40 /* Minimum 4 subfr bitrate (4x10 bits) */
58 : #define MIN_GAIN_BITS 6 /* Minimum number of bits used */
59 :
60 : #define TDM_UC_NORMAL_MODE_MBRATE 9000 /* Max bitrate for normal UC if LP coded */
61 : #define TDM_UC_NORMAL_MODE_MBRATE_LP_R 8200 /* Normal bitrate for normal UC if LP is reused */
62 : #define TDM_UC_NORMAL_MODE_MINBR_LP_R 7000 /* If bitrate is below 7k, add back MID_LP_BRATE */
63 : #define MID_LP_BRATE ( 31 + 5 ) * FRAMES_PER_SEC /* average bitrate for LP */
64 : #define MAX_TDM_UC_BRATE 11000 /* Maximum bitrate for tdm normal UC mode */
65 :
66 : #define MIN_SIGN_RATE ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS * FRAMES_PER_SEC ) /* min. 2 time 12 bits FCB, used only when LPC and/or pitch is transmitted */
67 : #define MIN_FCB_SECRATE2 ( 2 * 16 * FRAMES_PER_SEC ) /* min. 2 time 14 bits FCB, used only when LPC and/or pitch is transmitted */
68 :
69 :
70 : /*-------------------------------------------------------------------*
71 : * tdm_bit_alloc()
72 : *
73 : * Bitbudget distribution between Primary and Secondary channel in TD stereo
74 : *-------------------------------------------------------------------*/
75 :
76 14747 : void tdm_bit_alloc(
77 : const int16_t ivas_format, /* i : IVAS format */
78 : const int16_t ism_mode, /* i : ISM mode in combined format */
79 : const int32_t element_brate_wo_meta, /* i : element bitrate without metadata */
80 : const int16_t tdm_lp_reuse_flag, /* i : LPC reusage flag */
81 : int32_t *total_brate_pri, /* o : Allocated primary channel bitrate */
82 : int32_t *total_brate_sec, /* o : Allocated secondary channel bitrate */
83 : int16_t *tdm_low_rate_mode, /* o : secondary channel low rate mode flag*/
84 : const int16_t coder_type, /* i : secondary channel coder type */
85 : const int16_t ener_ratio_idx, /* i : correlation ratio indexe */
86 : const int16_t tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag */
87 : const int16_t bwidth_pri, /* i : bandwidth of the primary channel */
88 : const int16_t bwidth_sec, /* i : bandwidth of the secondary channel */
89 : const int16_t flag_ACELP16k_pri, /* i : ACELP@16kHz core flag, primary chan.*/
90 : const int16_t tdm_LRTD_flag, /* i : LRTD stereo mode flag */
91 : const int16_t coder_type0, /* i : coder type (temporary in the encoder, from bitstream in decoder) */
92 : const int16_t tdm_inst_ratio_idx_ref /* i : instantaneous correlation ratio idx */
93 : )
94 : {
95 : int16_t idx, four_subfr_fcb, two_subfr_fcb;
96 : float bit_rate_diff;
97 : int16_t BWE_brate, tmp_bits;
98 14747 : int16_t tdm_inst_ratio_idx = tdm_inst_ratio_idx_ref;
99 14747 : if ( tdm_inst_ratio_idx == TDM_NQ )
100 : {
101 400 : tdm_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM; /* Bit rate almost split half and half*/
102 : }
103 :
104 : /* Decision on using the low rate mode or the normal mode */
105 : /* default is using the low rate mode for the secondary channel coding*/
106 : /* UC and IC are automatically coded with low rate mode */
107 14747 : *tdm_low_rate_mode = 1;
108 :
109 : /* Allocating different bitrate to channels */
110 14747 : idx = 0;
111 14747 : if ( element_brate_wo_meta <= IVAS_13k2 )
112 : {
113 0 : idx = 0;
114 : }
115 14747 : else if ( element_brate_wo_meta <= IVAS_16k4 )
116 : {
117 2203 : idx = 1;
118 : }
119 12544 : else if ( element_brate_wo_meta <= IVAS_24k4 )
120 : {
121 4487 : idx = 2;
122 : }
123 8057 : else if ( element_brate_wo_meta <= IVAS_32k )
124 : {
125 8057 : idx = 3;
126 : }
127 0 : else if ( element_brate_wo_meta <= IVAS_48k )
128 : {
129 0 : idx = 4;
130 : }
131 :
132 14747 : if ( coder_type == UNVOICED && tdm_bit_allc_tbl[idx][coder_type] >= 4200 )
133 : {
134 0 : *tdm_low_rate_mode = 0;
135 : }
136 :
137 : /* Secondary channel based bitrate allocation */
138 14747 : *total_brate_sec = tdm_bit_allc_tbl[idx][coder_type];
139 :
140 : /* secondary channel bitrate allocation based on the energy scaling ratio */
141 14747 : if ( ( ( ivas_format != MASA_ISM_FORMAT || ism_mode == ISM_MODE_NONE ) && ( ( coder_type != UNVOICED ) || tdm_LRTD_flag == 1 ) ) || ( ivas_format == MASA_ISM_FORMAT && ism_mode != ISM_MODE_NONE && coder_type > UNVOICED ) )
142 : {
143 14683 : bit_rate_diff = (float) ( element_brate_wo_meta - 2 * *total_brate_sec );
144 :
145 14683 : if ( tdm_LRTD_flag == 1 ) /* > element_brate > STEREO_22k or CT0 not used */
146 : {
147 : /* further adjustment in function of the energy/correlation ratio */
148 14267 : if ( coder_type == INACTIVE )
149 : {
150 0 : *total_brate_sec = max( *total_brate_sec, (int16_t) ( 0.3f * ( element_brate_wo_meta - 500 ) / 100 ) * 100 );
151 0 : tmp_bits = (int16_t) ( -abs( tdm_inst_ratio_idx - 16 ) * 200 * idx );
152 : }
153 : else
154 : {
155 14267 : *total_brate_sec = max( *total_brate_sec, (int16_t) ( 0.5f * ( element_brate_wo_meta - 500 ) / 100 ) * 100 );
156 : /* tmp_bits = -abs(tdm_inst_ratio_idx-16)*200*idx; */
157 14267 : tmp_bits = (int16_t) ( -abs( tdm_inst_ratio_idx - 16 ) * 100 * idx );
158 : }
159 :
160 : /* tmp_bits should be subtract from the secondary channel bitrate */
161 : /* If the primary channel doesn't correspond to the channel having the highest correlation to the mono- inverse the bitrate compensation */
162 14267 : if ( ( ener_ratio_idx >= LRTD_STEREO_MID_IS_PRIM && tdm_inst_ratio_idx < LRTD_STEREO_MID_IS_PRIM ) || ( ener_ratio_idx < LRTD_STEREO_MID_IS_PRIM && tdm_inst_ratio_idx >= LRTD_STEREO_MID_IS_PRIM ) )
163 : {
164 6303 : tmp_bits *= -1;
165 : }
166 14267 : bit_rate_diff = tmp_bits;
167 : }
168 : else
169 : {
170 416 : if ( ener_ratio_idx < LRTD_STEREO_MID_IS_PRIM )
171 : {
172 256 : bit_rate_diff = ( LRTD_STEREO_MID_IS_PRIM - ener_ratio_idx ) * bit_rate_diff * 0.05f;
173 : }
174 : else
175 : {
176 160 : bit_rate_diff = ( ener_ratio_idx - LRTD_STEREO_MID_IS_PRIM ) * bit_rate_diff * 0.05f;
177 : }
178 : }
179 : /*bit_rate_diff2 = ((int16_t)(10.f*(-0.5f*ener_ratio_LR+0.5f)*bit_rate_diff)/100)*100;*/
180 14683 : *total_brate_sec += ( (int16_t) ( bit_rate_diff / 100 ) * 100 );
181 14683 : *total_brate_sec = max( *total_brate_sec, tdm_bit_allc_tbl[idx][coder_type] );
182 :
183 14683 : if ( coder_type == INACTIVE && tdm_LRTD_flag == 0 )
184 : {
185 0 : *total_brate_sec = min( *total_brate_sec, MIN_BRATE_SWB_BWE );
186 : }
187 :
188 14683 : if ( ( ener_ratio_idx <= 1 || ener_ratio_idx >= 29 ) && coder_type >= UNVOICED )
189 14451 : {
190 14451 : int16_t delta_brate = 0;
191 :
192 14451 : if ( bwidth_pri > WB )
193 : {
194 11348 : delta_brate = 600; /* To slightly compensate for SWB BWE instead of WB BWE */
195 11348 : if ( element_brate_wo_meta <= IVAS_16k4 )
196 : {
197 1748 : delta_brate = 1250; /* To compensate for SWB BWE instead of WB BWE */
198 : }
199 : }
200 :
201 14451 : if ( element_brate_wo_meta <= IVAS_13k2 )
202 : {
203 0 : *total_brate_sec = max( *total_brate_sec, 5600 + delta_brate ); /* ~42-47 % of the total bitrate */
204 : }
205 14451 : else if ( element_brate_wo_meta <= IVAS_16k4 )
206 : {
207 2152 : *total_brate_sec = max( *total_brate_sec, 6500 + delta_brate ); /* ~40-43 % of the total bitrate */
208 : }
209 12299 : else if ( element_brate_wo_meta <= IVAS_24k4 )
210 : {
211 4399 : *total_brate_sec = max( *total_brate_sec, 9000 + delta_brate ); /* ~37-39 % of the total bitrate */
212 : }
213 : else
214 : {
215 7900 : *total_brate_sec = max( *total_brate_sec, 9600 + delta_brate ); /* ~30-32% of the total bitrate */
216 : }
217 : }
218 : else
219 : {
220 232 : *total_brate_sec = min( *total_brate_sec, (int16_t) ( 0.0045f * element_brate_wo_meta ) * 100 );
221 : }
222 :
223 14683 : *total_brate_sec = min( *total_brate_sec, 18000 );
224 : }
225 64 : else if ( coder_type == UNVOICED )
226 : {
227 64 : if ( tdm_lp_reuse_flag == 0 )
228 : {
229 32 : *total_brate_sec += ( 31 + 5 ) * FRAMES_PER_SEC;
230 : }
231 : }
232 :
233 14747 : if ( coder_type <= UNVOICED )
234 : {
235 380 : *total_brate_sec = min( *total_brate_sec, MAX_TDM_UC_BRATE );
236 :
237 380 : if ( *total_brate_sec >= TDM_UC_NORMAL_MODE_MBRATE && tdm_lp_reuse_flag == 0 )
238 : {
239 272 : *tdm_low_rate_mode = 0;
240 : }
241 108 : else if ( *total_brate_sec >= TDM_UC_NORMAL_MODE_MBRATE_LP_R )
242 : {
243 0 : *tdm_low_rate_mode = 0;
244 : }
245 108 : else if ( ( tdm_lp_reuse_flag == 0 && *total_brate_sec < TDM_UC_NORMAL_MODE_MINBR_LP_R && coder_type == UNVOICED ) || ( tdm_lp_reuse_flag == 0 && *total_brate_sec < ( tdm_bit_allc_tbl[idx][0] + MID_LP_BRATE ) ) )
246 : {
247 32 : *total_brate_sec += MID_LP_BRATE;
248 : }
249 : }
250 :
251 : /* verify that primary channel bitrate is higher than the minimum supported bitrate */
252 14747 : if ( flag_ACELP16k_pri )
253 : {
254 8145 : BWE_brate = SWB_TBE_1k75;
255 8145 : if ( element_brate_wo_meta < IVAS_24k4 )
256 : {
257 0 : BWE_brate = SWB_TBE_1k10;
258 : }
259 :
260 8145 : if ( bwidth_pri > WB && tdm_LRTD_flag == 0 )
261 : {
262 349 : BWE_brate += ( STEREO_BITS_ICBWE + STEREO_ICBWE_MSFLAG_BITS ) * FRAMES_PER_SEC;
263 : }
264 8145 : if ( bwidth_pri > SWB && tdm_LRTD_flag == 1 )
265 : {
266 2854 : BWE_brate += 300;
267 : }
268 :
269 8145 : if ( bwidth_pri == FB )
270 : {
271 2899 : BWE_brate += ( FB_TBE_1k8 - SWB_TBE_1k75 );
272 : }
273 :
274 8145 : if ( element_brate_wo_meta - *total_brate_sec - BWE_brate < 14000 )
275 : {
276 3668 : *total_brate_sec = element_brate_wo_meta - 14000 - BWE_brate;
277 : }
278 : }
279 : else
280 : {
281 6602 : BWE_brate = SWB_TBE_1k75;
282 6602 : if ( bwidth_pri == WB )
283 : {
284 1664 : BWE_brate = WB_BWE_0k35;
285 1664 : if ( tdm_LRTD_flag == 0 )
286 : {
287 12 : BWE_brate += 250; /* ICA Brate */
288 : }
289 : }
290 4938 : else if ( tdm_LRTD_flag == 0 )
291 : {
292 39 : BWE_brate += 350; /* ICA Brate */
293 : }
294 : }
295 :
296 14747 : if ( coder_type0 == TRANSITION )
297 : {
298 92 : if ( element_brate_wo_meta > IVAS_13k2 )
299 : {
300 92 : *total_brate_sec = min( *total_brate_sec, element_brate_wo_meta - ( ACELP_8k00 + BWE_brate ) );
301 : }
302 : else
303 : {
304 0 : *total_brate_sec = min( *total_brate_sec, element_brate_wo_meta - ( ACELP_7k20 + BWE_brate ) );
305 : }
306 : }
307 : else
308 : {
309 14655 : *total_brate_sec = min( *total_brate_sec, element_brate_wo_meta - ( 5900 + BWE_brate ) );
310 : }
311 :
312 14747 : if ( coder_type == INACTIVE )
313 : {
314 0 : *total_brate_sec = max( *total_brate_sec, tdm_bit_allc_tbl[0][0] ); /* sanity check to ensure the secondary channel always gets the minimal bitrate it needs */
315 : }
316 : else
317 : {
318 14747 : *total_brate_sec = max( *total_brate_sec, 3500 ); /* sanity check to ensure the secondary channel always gets the minimal bitrate it needs */
319 : }
320 :
321 : /* Secondary channel bitrate adjusment */
322 : /* First, adjust the bitrate depending of what is transmitted */
323 : /* Second, choose the number of subframe for ACELP core depending of the targetted bitratre */
324 : /* Finally, verify that the concordance between the number of subframe, the parameters sent and the bitrate available */
325 14747 : if ( coder_type == GENERIC /* || coder_type == AUDIO*/ )
326 : {
327 : /* Adjust the bitrate depending of what is transmitted */
328 : /* If LPC are transmitted, ensure enough bits are used */
329 14367 : if ( tdm_lp_reuse_flag == 0 )
330 : {
331 : /* Pitch is transmitted as well, further increase the bitrate */
332 13831 : if ( tdm_Pitch_reuse_flag == 0 )
333 : {
334 13699 : *total_brate_sec = max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_ACB_RATE + MIN_SEC_LPC_RATE + MIN_SIGN_RATE );
335 :
336 13699 : if ( tdm_LRTD_flag == 1 && bwidth_sec == SWB )
337 : {
338 : /* ensure that there are enough bits to code SWB TBE_1k10 as well */
339 7702 : *total_brate_sec = max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_ACB_RATE + MIN_SEC_LPC_RATE + MIN_SIGN_RATE + SWB_TBE_1k10 );
340 : }
341 : }
342 : else /* only LPC is tranmitted -> if ( *total_brate_sec < MIN_SEC_BRATE+MIN_SEC_LPC_RATE ) */
343 : {
344 132 : *total_brate_sec = max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_LPC_RATE + MIN_SIGN_RATE );
345 : }
346 : }
347 536 : else if ( /*tdm_lp_reuse_flag == 1*/ tdm_Pitch_reuse_flag == 0 )
348 : {
349 536 : *total_brate_sec = max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_ACB_RATE + MIN_SIGN_RATE );
350 : }
351 :
352 : /* Choose between 2 and 4 subfr, depending of the bitrate available and prevent the gap between the 2 atlernative */
353 14367 : if ( tdm_LRTD_flag == 1 )
354 : {
355 13951 : four_subfr_fcb = (int16_t) ( *total_brate_sec - ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS - STEREO_BITS_TCA + 1 + 4 * MIN_GAIN_BITS ) * FRAMES_PER_SEC );
356 13951 : two_subfr_fcb = (int16_t) ( *total_brate_sec - ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS - STEREO_BITS_TCA + 1 + 2 * MIN_GAIN_BITS ) * FRAMES_PER_SEC );
357 : }
358 : else
359 : {
360 416 : four_subfr_fcb = (int16_t) ( *total_brate_sec - ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS + 1 + 4 * MIN_GAIN_BITS ) * FRAMES_PER_SEC );
361 416 : two_subfr_fcb = (int16_t) ( *total_brate_sec - ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS + 1 + 2 * MIN_GAIN_BITS ) * FRAMES_PER_SEC );
362 : }
363 :
364 14367 : if ( tdm_lp_reuse_flag == 0 )
365 : {
366 13831 : four_subfr_fcb -= MIN_SEC_LPC_RATE;
367 13831 : two_subfr_fcb -= MIN_SEC_LPC_RATE;
368 : }
369 :
370 14367 : if ( tdm_Pitch_reuse_flag == 0 )
371 : {
372 14235 : four_subfr_fcb -= ( MIN_SEC_ACB_RATE + 10 * FRAMES_PER_SEC );
373 14235 : two_subfr_fcb -= MIN_SEC_ACB_RATE;
374 : }
375 :
376 : /* Too much bits for the 2 subfr model but not enough for the the 4 subfr model -> slightly reduce the 2nd channel bitrate */
377 14367 : if ( two_subfr_fcb > 2 * MAX_SC_FCB_RATE * FRAMES_PER_SEC && four_subfr_fcb < MIN_4SUBFR_FCB_RATE * FRAMES_PER_SEC )
378 : {
379 0 : if ( tdm_LRTD_flag == 1 )
380 : {
381 0 : *total_brate_sec = 2 * MAX_SC_FCB_RATE * FRAMES_PER_SEC + ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS - STEREO_BITS_TCA + 1 + 2 * MIN_GAIN_BITS ) * FRAMES_PER_SEC;
382 : }
383 : else
384 : {
385 0 : *total_brate_sec = 2 * MAX_SC_FCB_RATE * FRAMES_PER_SEC + ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS + 1 + 2 * MIN_GAIN_BITS ) * FRAMES_PER_SEC;
386 : }
387 :
388 0 : if ( tdm_lp_reuse_flag == 0 )
389 : {
390 0 : *total_brate_sec += MIN_SEC_LPC_RATE;
391 : }
392 :
393 0 : if ( tdm_Pitch_reuse_flag == 0 )
394 : {
395 0 : *total_brate_sec += MIN_SEC_ACB_RATE;
396 : }
397 : }
398 14367 : else if ( four_subfr_fcb >= ( 40 ) * FRAMES_PER_SEC ) /* Enough bits to have minimally 2 x 12 + 2*7 bits FCB */
399 : {
400 14367 : *tdm_low_rate_mode = 0; /* Use normal rate mode */
401 : }
402 : else /* Possible slight increase of secondary channel bit budget to compensate for FCB limited flexibility */
403 : {
404 : int16_t tmp_rate, i;
405 0 : tmp_rate = two_subfr_fcb;
406 0 : idx = NB_RATE_POSS - 2;
407 :
408 0 : for ( i = 0; i < NB_RATE_POSS; i++ )
409 : {
410 0 : if ( tmp_rate <= fast_FCB_rates_2sfr[i] )
411 : {
412 0 : idx = i;
413 0 : break;
414 : }
415 : }
416 0 : *total_brate_sec += ( fast_FCB_rates_2sfr[idx] - tmp_rate );
417 : }
418 : /* To prevent 13.2 kb/s for primary channel as some bitstream issues arrise with it */
419 14367 : if ( element_brate_wo_meta - *total_brate_sec == ACELP_13k20 )
420 : {
421 0 : *total_brate_sec += 100;
422 : }
423 : }
424 : /* prevent 2.4 kb/s and 2.8 kb/s as they are reserved bitrates for DTX and VBR */
425 14747 : if ( *total_brate_sec == PPP_NELP_2k80 || *total_brate_sec == SID_2k40 )
426 : {
427 0 : *total_brate_sec -= 100;
428 : }
429 :
430 14747 : *total_brate_pri = element_brate_wo_meta - *total_brate_sec;
431 :
432 14747 : return;
433 : }
434 :
435 :
436 : /*-------------------------------------------------------------------*
437 : * td_stereo_param_updt()
438 : *
439 : * copy certain TD stereo parameters from primary channel to secondary channel
440 : *-------------------------------------------------------------------*/
441 :
442 15131 : void td_stereo_param_updt(
443 : const float lsp_old_PCh[], /* i : primary channel old LSPs */
444 : const float lsf_old_PCh[], /* i : primary channel old LSFs */
445 : const float pitch_buf_PCh[], /* i : primary channel pitch buffer */
446 : float tdm_lspQ_PCh[], /* o : Q LSPs for primary channel */
447 : float tdm_lsfQ_PCh[], /* o : Q LSFs for primary channel */
448 : float tdm_Pri_pitch_buf[], /* o : pitch values for primary channel */
449 : const int16_t flag_ACELP16k, /* i : ACELP@16kHz flag */
450 : const int16_t tdm_use_IAWB_Ave_lpc /* i : flag to indicate the usage of mean inactive LP coefficients */
451 : )
452 : {
453 : int16_t i;
454 :
455 : /* Copy some primary channel information into the secondary channel structure for later usage */
456 15131 : if ( tdm_use_IAWB_Ave_lpc == 1 )
457 : {
458 0 : mvr2r( IAWB_Ave, tdm_lsfQ_PCh, M );
459 :
460 0 : if ( tdm_lspQ_PCh != NULL )
461 : {
462 0 : lsf2lsp( tdm_lsfQ_PCh, tdm_lspQ_PCh, M, INT_FS_12k8 );
463 : }
464 : }
465 15131 : else if ( flag_ACELP16k == 1 )
466 : {
467 8403 : if ( tdm_lspQ_PCh != NULL )
468 : {
469 6294 : mvr2r( lsp_old_PCh, tdm_lspQ_PCh, M );
470 6294 : lsp_convert_poly( tdm_lspQ_PCh, L_FRAME, 0 );
471 6294 : lsp2lsf( tdm_lspQ_PCh, tdm_lsfQ_PCh, M, INT_FS_12k8 );
472 : }
473 : else
474 : {
475 : float lsp_temp[M];
476 2109 : mvr2r( lsp_old_PCh, lsp_temp, M );
477 2109 : lsp_convert_poly( lsp_temp, L_FRAME, 0 );
478 2109 : lsp2lsf( lsp_temp, tdm_lsfQ_PCh, M, INT_FS_12k8 );
479 : }
480 : }
481 : else
482 : {
483 6728 : if ( tdm_lspQ_PCh != NULL )
484 : {
485 5046 : mvr2r( lsp_old_PCh, tdm_lspQ_PCh, M );
486 : }
487 6728 : mvr2r( lsf_old_PCh, tdm_lsfQ_PCh, M );
488 : }
489 :
490 : /* This is only to keep the buffer up-to-date */
491 15131 : if ( flag_ACELP16k == 1 )
492 : {
493 42015 : for ( i = 0; i < NB_SUBFR; i++ )
494 : {
495 33612 : tdm_Pri_pitch_buf[i] = pitch_buf_PCh[i] * 0.8f;
496 33612 : tdm_Pri_pitch_buf[i] = max( tdm_Pri_pitch_buf[i], PIT_MIN );
497 : }
498 : }
499 : else
500 : {
501 6728 : mvr2r( pitch_buf_PCh, tdm_Pri_pitch_buf, NB_SUBFR );
502 : }
503 :
504 15131 : return;
505 : }
506 :
507 :
508 : /*-------------------------------------------------------------------*
509 : * tdm_SCh_LSF_intra_pred_zero_bits()
510 : *
511 : *
512 : *-------------------------------------------------------------------*/
513 :
514 416 : static void tdm_SCh_LSF_intra_pred_zero_bits(
515 : const float *tdm_lsfQ_PCh, /* i : primary channel LSFs */
516 : float *pred_lsf_SCh, /* o : predicted secondary channel LSFs */
517 : const float *lsf_mean, /* i : secondary channel mean LSFs */
518 : const float beta /* i : pull to average beta factor */
519 : )
520 : {
521 : int16_t i;
522 :
523 : /* pulling the LSFs closer to the average */
524 7072 : for ( i = 0; i < M; i++ )
525 : {
526 6656 : pred_lsf_SCh[i] = beta * tdm_lsfQ_PCh[i] + ( 1.0f - beta ) * lsf_mean[i];
527 : }
528 :
529 416 : return;
530 : }
531 :
532 :
533 : /*-------------------------------------------------------------------*
534 : * tdm_SCh_LSF_intra_pred_tri_diag_mat()
535 : *
536 : *
537 : *-------------------------------------------------------------------*/
538 :
539 984 : static void tdm_SCh_LSF_intra_pred_tri_diag_mat(
540 : float *lsf_SCh, /* i/o: secondary channel LSFs */
541 : const float *lsf_mean_in, /* i : secondary channel mean LSFs (in) */
542 : const float *lsf_mean_out, /* i : secondary channel mean LSFs (out) */
543 : const float *prd_diag_3 /* i : secondary channel mean LSFs */
544 : )
545 : {
546 : int16_t i;
547 : float lsf_tmp[M];
548 : const float *prd_ptr;
549 : float *lsf_tmp_ptr1;
550 : float *lsf_tmp_ptr2;
551 : float *lsf_SCh_ptr;
552 :
553 984 : prd_ptr = prd_diag_3;
554 :
555 984 : v_sub( lsf_SCh, lsf_mean_in, lsf_tmp, M );
556 :
557 984 : lsf_tmp_ptr1 = lsf_tmp;
558 984 : lsf_SCh_ptr = lsf_SCh;
559 :
560 984 : lsf_tmp_ptr2 = lsf_tmp_ptr1;
561 984 : *lsf_SCh_ptr = ( *lsf_tmp_ptr1++ ) * ( *prd_ptr++ );
562 984 : ( *lsf_SCh_ptr++ ) += ( *lsf_tmp_ptr1 ) * ( *prd_ptr++ );
563 :
564 14760 : for ( i = 1; i < M - 1; i++ )
565 : {
566 13776 : lsf_tmp_ptr1 = lsf_tmp_ptr2;
567 13776 : *lsf_SCh_ptr = ( *lsf_tmp_ptr1++ ) * ( *prd_ptr++ );
568 13776 : lsf_tmp_ptr2 = lsf_tmp_ptr1;
569 13776 : *lsf_SCh_ptr += ( *lsf_tmp_ptr1++ ) * ( *prd_ptr++ );
570 13776 : ( *lsf_SCh_ptr++ ) += ( *lsf_tmp_ptr1 ) * ( *prd_ptr++ );
571 : }
572 :
573 984 : lsf_tmp_ptr1 = lsf_tmp_ptr2;
574 984 : *lsf_SCh_ptr = ( *lsf_tmp_ptr1++ ) * ( *prd_ptr++ );
575 984 : *lsf_SCh_ptr += ( *lsf_tmp_ptr1 ) * ( *prd_ptr );
576 :
577 984 : v_add( lsf_SCh, lsf_mean_out, lsf_SCh, M );
578 :
579 984 : return;
580 : }
581 :
582 :
583 : /*-------------------------------------------------------------------*
584 : * tdm_SCh_LSF_intra_pred()
585 : *
586 : *
587 : *-------------------------------------------------------------------*/
588 :
589 416 : void tdm_SCh_LSF_intra_pred(
590 : const int32_t element_brate, /* i : element bitrate */
591 : const float *tdm_lsfQ_PCh, /* i : primary channel LSFs */
592 : float *pred_lsf_SCh /* o : predicted secondary channel LSFs */
593 : )
594 : {
595 : float fixed_beta;
596 :
597 416 : if ( element_brate <= IVAS_13k2 )
598 : {
599 0 : fixed_beta = 0.87F;
600 : }
601 416 : else if ( element_brate <= IVAS_16k4 )
602 : {
603 51 : fixed_beta = 0.94F;
604 : }
605 365 : else if ( element_brate <= IVAS_24k4 )
606 : {
607 56 : fixed_beta = 0.91F;
608 : }
609 309 : else if ( element_brate <= IVAS_32k )
610 : {
611 309 : fixed_beta = 0.92F;
612 : }
613 : else
614 : {
615 0 : fixed_beta = 0.91F;
616 : }
617 :
618 416 : tdm_SCh_LSF_intra_pred_zero_bits( tdm_lsfQ_PCh, pred_lsf_SCh, tdm_LSF_MEAN_PRED_QNT, fixed_beta );
619 :
620 416 : tdm_SCh_LSF_intra_pred_tri_diag_mat( pred_lsf_SCh, tdm_LSF_MEAN_PRED_QNT_IN, tdm_LSF_MEAN_PRED_QNT_OUT, tdm_PRED_QNT_fixed_beta_prd_diag_3 );
621 :
622 416 : return;
623 : }
624 :
625 :
626 : /*-------------------------------------------------------------------*
627 : * tdm_SCh_LSF_intra_pred_one_bit_dec()
628 : *
629 : *
630 : *-------------------------------------------------------------------*/
631 :
632 568 : static void tdm_SCh_LSF_intra_pred_one_bit_dec(
633 : const float *tdm_lsfQ_PCh, /* i : primary channel LSFs */
634 : float *pred_lsf_SCh, /* o : predicted secondary channel LSFs */
635 : const float *lsf_mean, /* i : secondary channel mean LSFs */
636 : const float *Beta_Q_x, /* i : beta quantization values */
637 : const int16_t beta_index /* i : the quantization bits for beta (-1 if beta fixed)*/
638 : )
639 : {
640 : int16_t i;
641 : float beta;
642 :
643 568 : beta = Beta_Q_x[beta_index];
644 :
645 : /* pulling the LSFs closer to the avergae */
646 9656 : for ( i = 0; i < M; i++ )
647 : {
648 9088 : pred_lsf_SCh[i] = beta * tdm_lsfQ_PCh[i] + ( 1.0f - beta ) * lsf_mean[i];
649 : }
650 :
651 568 : return;
652 : }
653 :
654 :
655 : /*-------------------------------------------------------------------*
656 : * tdm_SCh_LSF_intra_pred_one_bit_enc()
657 : *
658 : *
659 : *-------------------------------------------------------------------*/
660 :
661 142 : static void tdm_SCh_LSF_intra_pred_one_bit_enc(
662 : const float *lsf_SCh, /* i : secondary channel LSFs */
663 : const float *tdm_lsfQ_PCh, /* i : primary channel LSFs */
664 : float *pred_lsf_SCh, /* o : predicted secondary channel LSFs */
665 : const float *lsf_mean, /* i : secondary channel mean LSFs */
666 : const float *lsf_wgts_new, /* i : Improved wgts for LSFs */
667 : const float *Beta_Q_x, /* i : beta quantization values */
668 : int16_t *beta_index /* o : the quantization bits for beta (-1 if beta fixed) */
669 : )
670 : {
671 : int16_t i;
672 : float A_temp[M];
673 : float B_temp[M];
674 : float WD[2];
675 :
676 2414 : for ( i = 0; i < M; i++ )
677 : {
678 2272 : A_temp[i] = lsf_SCh[i] - lsf_mean[i];
679 2272 : B_temp[i] = lsf_mean[i] - tdm_lsfQ_PCh[i];
680 : }
681 :
682 142 : WD[0] = 0.f;
683 142 : WD[1] = 0.f;
684 2414 : for ( i = 0; i < M; i++ )
685 : {
686 2272 : WD[0] += lsf_wgts_new[i] * SQR( A_temp[i] ) + 2.0f * Beta_Q_x[0] * lsf_wgts_new[i] * A_temp[i] * B_temp[i] + lsf_wgts_new[i] * SQR( Beta_Q_x[0] ) * SQR( B_temp[i] );
687 2272 : WD[1] += lsf_wgts_new[i] * SQR( A_temp[i] ) + 2.0f * Beta_Q_x[1] * lsf_wgts_new[i] * A_temp[i] * B_temp[i] + lsf_wgts_new[i] * SQR( Beta_Q_x[1] ) * SQR( B_temp[i] );
688 : }
689 :
690 142 : if ( WD[0] < WD[1] )
691 : {
692 126 : *beta_index = 0;
693 : }
694 : else
695 : {
696 16 : *beta_index = 1;
697 : }
698 :
699 142 : tdm_SCh_LSF_intra_pred_one_bit_dec( tdm_lsfQ_PCh, pred_lsf_SCh, tdm_LSF_MEAN_RE_USE, Beta_Q_x, *beta_index );
700 :
701 142 : return;
702 : }
703 :
704 :
705 : /*-------------------------------------------------------------------*
706 : * tdm_SCh_lsf_reuse()
707 : *
708 : *
709 : *-------------------------------------------------------------------*/
710 :
711 568 : void tdm_SCh_lsf_reuse(
712 : const int16_t enc_dec, /* i : encoder/decoder flag */
713 : const int32_t element_brate, /* i : element bitrate */
714 : float lsf_new[M], /* i/o: LSFs at the end of the frame */
715 : float lsp_new[M], /* i/o: LSPs at the end of the frame */
716 : const float tdm_lsfQ_PCh[M], /* i : primary channel LSFs */
717 : const float lsf_wgts[M], /* i : LSF weights */
718 : int16_t *beta_index /* i/o: quantization index */
719 : )
720 : {
721 : const float *Beta_Q1bit_re_use;
722 :
723 568 : if ( element_brate <= IVAS_13k2 )
724 : {
725 0 : Beta_Q1bit_re_use = tdm_Beta_Q1bit_re_use_13k2;
726 : }
727 568 : else if ( element_brate <= IVAS_16k4 )
728 : {
729 48 : Beta_Q1bit_re_use = tdm_Beta_Q1bit_re_use_16k4;
730 : }
731 520 : else if ( element_brate <= IVAS_32k )
732 : {
733 520 : Beta_Q1bit_re_use = tdm_Beta_Q1bit_re_use_24k4_32k;
734 : }
735 : else
736 : {
737 0 : Beta_Q1bit_re_use = tdm_Beta_Q1bit_re_use_48k;
738 : }
739 :
740 568 : if ( enc_dec == ENC )
741 : {
742 142 : tdm_SCh_LSF_intra_pred_one_bit_enc( lsf_new, tdm_lsfQ_PCh, lsf_new, tdm_LSF_MEAN_RE_USE, lsf_wgts, Beta_Q1bit_re_use, beta_index );
743 : }
744 : else /* DEC */
745 : {
746 426 : tdm_SCh_LSF_intra_pred_one_bit_dec( tdm_lsfQ_PCh, lsf_new, tdm_LSF_MEAN_RE_USE, Beta_Q1bit_re_use, *beta_index );
747 : }
748 :
749 568 : tdm_SCh_LSF_intra_pred_tri_diag_mat( lsf_new, tdm_LSF_MEAN_RE_USE_IN, tdm_LSF_MEAN_RE_USE_OUT, tdm_RE_USE_adaptive_beta_prd_diag_3 );
750 :
751 568 : lsf2lsp( lsf_new, lsp_new, M, INT_FS_12k8 );
752 :
753 568 : return;
754 : }
|