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 : #include "math.h"
36 : #include "prot_fx.h"
37 : #include "ivas_cnst.h"
38 : #include "ivas_rom_com.h"
39 : #include "wmc_auto.h"
40 : #include "ivas_prot_fx.h"
41 :
42 :
43 : /*-----------------------------------------------------------------------------------------*
44 : * Function ivas_lfe_arith_coding()
45 : *
46 : * LFE Arithmetic coding block. Writes encoded data to bitstream.
47 : *-----------------------------------------------------------------------------------------*/
48 3972 : static void ivas_lfe_arith_coding_fx(
49 : LFE_ENC_HANDLE hLFE,
50 : BSTR_ENC_HANDLE hBstr,
51 : const Word16 quant_strategy,
52 : Word16 *values )
53 : {
54 : Tastat as;
55 : Word16 index, i;
56 : Word16 max_value, offset;
57 : UWord16 num_ele_per_grp;
58 : Word16 num_groups;
59 3972 : index = 0;
60 3972 : num_ele_per_grp = IVAS_LFE_NUM_COEFFS_IN_SUBGRP << 1;
61 3972 : num_groups = shr( ivas_lfe_num_dct_pass_bins_tbl[quant_strategy], 1 );
62 3972 : move16();
63 3972 : move16();
64 :
65 19860 : FOR( i = 0; i < num_groups; i++ )
66 : {
67 15888 : max_value = ivas_lfe_num_ele_in_coder_models[quant_strategy][i];
68 15888 : move16();
69 15888 : ari_start_encoding_14bits_ivas_fx( &as );
70 15888 : offset = imult1616( 4, i );
71 :
72 79440 : FOR( index = 0; index < num_ele_per_grp; index++ )
73 : {
74 63552 : if ( GT_16( values[index + offset], max_value ) )
75 : {
76 1224 : values[index + offset] = max_value;
77 1224 : move16();
78 : }
79 63552 : ivas_ari_encode_14bits_ext_fx( hBstr, &as, values[index + offset], hLFE->cum_freq_models[quant_strategy][i] );
80 : }
81 :
82 15888 : ivas_ari_done_encoding_14bits_fx( hBstr, &as );
83 : }
84 :
85 3972 : return;
86 : }
87 :
88 : /*-----------------------------------------------------------------------------------------*
89 : * Function ivas_lfe_enc_quant()
90 : *
91 : * LFE quatization block, calls arithmetic coding block inside
92 : *-----------------------------------------------------------------------------------------*/
93 :
94 26420 : static void ivas_lfe_enc_quant_fx(
95 : LFE_ENC_HANDLE hLFE,
96 : Word32 *pLfe_dct,
97 : Word16 q_pLfe_dct,
98 : BSTR_ENC_HANDLE hBstr )
99 : {
100 : Word16 bits_written;
101 : Word16 nb_ind_tot;
102 : UWord16 quant_strategy, write_bit;
103 : Word16 num_quant_strategies;
104 : Word16 shift_bits;
105 : Word16 values[IVAS_LFE_MAX_NUM_DCT_COEFFS << 1];
106 : Word32 temp_lfe_dct[IVAS_LFE_MAX_NUM_DCT_COEFFS];
107 : Word16 target_bits;
108 : Word16 base2_num_bits_tot;
109 : Word16 coding_strategy;
110 : Word16 bits_written_arith_enc;
111 : Word16 next_ind_pos_arith_enc;
112 26420 : Word16 num_ele_per_grp = IVAS_LFE_NUM_COEFFS_IN_SUBGRP << 1;
113 26420 : move16();
114 :
115 26420 : target_bits = (Word16) ( IVAS_LFE_BITRATE_5000 / FRAMES_PER_SEC );
116 26420 : move16();
117 :
118 26420 : write_bit = 0;
119 26420 : move16();
120 26420 : num_quant_strategies = IVAS_MAX_NUM_QUANT_STRATS;
121 26420 : move16();
122 26420 : shift_bits = IVAS_LFE_SHIFT_BITS;
123 26420 : move16();
124 26420 : bits_written = hBstr->nb_bits_tot;
125 26420 : move16();
126 26420 : nb_ind_tot = hBstr->nb_ind_tot;
127 26420 : move16();
128 :
129 :
130 26420 : FOR( quant_strategy = 0; quant_strategy < num_quant_strategies; quant_strategy++ )
131 : {
132 : Word16 q_tmp, q_res_e, q_lfe_abs_sum;
133 : Word32 tmp;
134 : Word64 lfe_abs_sum;
135 : Word16 num_dct_pass_bins;
136 : Word16 max_of_vals, num_groups;
137 : Word16 shift;
138 : UWord16 min_shift, i, j;
139 : UWord16 max_shift;
140 : UWord16 max_value, num_lfe_ele;
141 : UWord16 all_zeros_dct;
142 :
143 26420 : q_lfe_abs_sum = q_pLfe_dct;
144 26420 : move16();
145 26420 : lfe_abs_sum = 0;
146 26420 : move64();
147 26420 : coding_strategy = 0;
148 26420 : move16();
149 26420 : num_dct_pass_bins = ivas_lfe_num_dct_pass_bins_tbl[quant_strategy];
150 26420 : move16();
151 26420 : max_of_vals = 0;
152 26420 : move16();
153 26420 : num_groups = shr( num_dct_pass_bins, 1 );
154 26420 : shift = 0;
155 26420 : move16();
156 26420 : min_shift = ivas_lfe_min_shift_tbl[quant_strategy];
157 26420 : move16();
158 26420 : max_shift = (UWord16) L_add( L_mult0( min_shift, IVAS_LFE_SHIFTS_PER_DOUBLE ), sub( shl( 1, shift_bits ), 1 ) );
159 26420 : num_lfe_ele = imult1616( num_dct_pass_bins, IVAS_LFE_NUM_COEFFS_IN_SUBGRP );
160 26420 : move16();
161 26420 : all_zeros_dct = 0;
162 26420 : move16();
163 26420 : max_value = ivas_lfe_num_ele_in_coder_models[quant_strategy][0];
164 26420 : move16();
165 :
166 26420 : Copy32( &pLfe_dct[IVAS_LFE_MAX_NUM_DCT_PASS_BINS], &pLfe_dct[num_dct_pass_bins], num_dct_pass_bins );
167 :
168 132100 : FOR( i = 0; i < num_groups; i++ )
169 : {
170 105680 : temp_lfe_dct[4 * i] = pLfe_dct[2 * i];
171 105680 : move32();
172 105680 : lfe_abs_sum = W_add( lfe_abs_sum, L_abs( temp_lfe_dct[4 * i] ) );
173 105680 : temp_lfe_dct[4 * i + 1] = pLfe_dct[2 * i + 1];
174 105680 : move32();
175 105680 : lfe_abs_sum = W_add( lfe_abs_sum, L_abs( temp_lfe_dct[4 * i + 1] ) );
176 :
177 105680 : temp_lfe_dct[4 * i + 2] = pLfe_dct[2 * i + num_dct_pass_bins];
178 105680 : move32();
179 105680 : lfe_abs_sum = W_add( lfe_abs_sum, L_abs( temp_lfe_dct[4 * i + 2] ) );
180 105680 : temp_lfe_dct[4 * i + 3] = pLfe_dct[2 * i + num_dct_pass_bins + 1];
181 105680 : move32();
182 105680 : lfe_abs_sum = W_add( lfe_abs_sum, L_abs( temp_lfe_dct[4 * i + 3] ) );
183 : }
184 :
185 26420 : IF( LE_64( lfe_abs_sum, W_shr( IVAS_LFE_ABS_SUM_FLT_THR_Q42, sub( 42, q_pLfe_dct ) ) ) )
186 : {
187 0 : shift = max_shift;
188 0 : move16();
189 : }
190 : ELSE
191 : {
192 26420 : q_tmp = W_norm( lfe_abs_sum );
193 26420 : IF( LT_16( q_tmp, 32 ) )
194 : {
195 75 : q_tmp = sub( 32, q_tmp );
196 75 : lfe_abs_sum = W_shr( lfe_abs_sum, q_tmp );
197 75 : q_lfe_abs_sum = sub( q_lfe_abs_sum, q_tmp );
198 : }
199 :
200 26420 : tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( max_value, W_extract_l( lfe_abs_sum ), &q_tmp ) );
201 : // tmp = L_shl( tmp, sub( q_lfe_abs_sum, sub( 15, q_tmp ) ) ); /* Q0 (max_value / lfe_abs_sum) */
202 26420 : q_tmp = sub( Q16, sub( q_lfe_abs_sum, sub( 15, q_tmp ) ) );
203 :
204 : /* log2_f(max_value / lfe_abs_sum) -> Q25 */
205 26420 : tmp = BASOP_Util_Log2( tmp );
206 26420 : tmp = L_add( tmp, L_shl( sub( Q31, q_tmp ), Q25 ) ) /* (31<<25) -> 1040187392 */; /* Q25 */
207 :
208 : /* IVAS_LFE_SHIFTS_PER_DOUBLE * log2_f(max_value / lfe_abs_sum) */
209 26420 : tmp = Mpy_32_16_1( tmp, IVAS_LFE_SHIFTS_PER_DOUBLE ); /* 25-15 -> Q10 */
210 :
211 26420 : shift = extract_l( L_shr( tmp, 10 ) ); /* Q0 */
212 : }
213 :
214 26420 : shift = s_max( imult1616( min_shift, IVAS_LFE_SHIFTS_PER_DOUBLE ), s_min( max_shift, shift ) );
215 26420 : move16();
216 :
217 : WHILE( 1 )
218 : {
219 26420 : IF( EQ_32( shift, max_shift ) )
220 : {
221 : /* write all LFE bits as 0 */
222 22448 : all_zeros_dct = 1;
223 22448 : move16();
224 22448 : BREAK;
225 : }
226 : ELSE
227 : {
228 3972 : max_of_vals = 0;
229 3972 : move16();
230 67524 : FOR( i = 0; i < num_lfe_ele; i++ )
231 : {
232 63552 : tmp = BASOP_Util_Divide1616_Scale( shift, IVAS_LFE_SHIFTS_PER_DOUBLE, &q_tmp ); /*Q = 15 - q_tmp*/
233 63552 : q_tmp = sub( 15, q_tmp );
234 :
235 63552 : tmp = BASOP_util_Pow2( tmp, sub( 31, q_tmp ), &q_res_e );
236 63552 : q_tmp = sub( 31, q_res_e );
237 :
238 63552 : tmp = Mpy_32_32( temp_lfe_dct[i], tmp ); /* q_tmp + q_pLfe_dct - 31 */
239 63552 : q_tmp = sub( add( q_tmp, q_pLfe_dct ), 31 );
240 :
241 63552 : IF( tmp < 0 )
242 : {
243 31716 : values[i] = add( extract_l( L_shr( L_sub( tmp, L_shl( 1 /*0.5 Q1-> 1*/, sub( q_tmp, 1 ) ) ), q_tmp ) ), 1 );
244 31716 : move16();
245 : }
246 : ELSE
247 : {
248 31836 : values[i] = extract_l( L_shr( L_add( tmp, L_shl( 1 /*0.5 Q1-> 1*/, sub( q_tmp, 1 ) ) ), q_tmp ) );
249 31836 : move16();
250 : }
251 :
252 63552 : IF( LT_32( max_of_vals, abs_s( values[i] ) ) )
253 : {
254 9790 : max_of_vals = abs_s( values[i] );
255 9790 : move16();
256 : }
257 : }
258 : }
259 3972 : IF( LE_32( max_of_vals, max_value ) )
260 : {
261 3972 : BREAK;
262 : }
263 0 : shift = sub( shift, 1 );
264 : }
265 :
266 26420 : IF( LT_32( shift, min_shift * IVAS_LFE_SHIFTS_PER_DOUBLE ) )
267 : {
268 0 : CONTINUE;
269 : }
270 :
271 26420 : IF( NE_32( all_zeros_dct, 1 ) )
272 : {
273 3972 : push_next_indice( hBstr, all_zeros_dct, 1 );
274 3972 : push_next_indice( hBstr, quant_strategy, 1 );
275 3972 : push_next_indice( hBstr, (UWord16) L_sub( shift, min_shift * IVAS_LFE_SHIFTS_PER_DOUBLE ), shift_bits );
276 : }
277 : ELSE
278 : {
279 22448 : push_next_indice( hBstr, all_zeros_dct, 1 );
280 22448 : hLFE->lfe_bits = sub( hBstr->nb_bits_tot, bits_written );
281 22448 : move16();
282 22448 : return;
283 : }
284 :
285 67524 : FOR( i = 0; i < num_lfe_ele; i++ )
286 : {
287 63552 : IF( values[i] < 0 )
288 : {
289 20023 : write_bit = 1;
290 20023 : move16();
291 20023 : values[i] = add( values[i], 1 );
292 20023 : move16();
293 : }
294 : ELSE
295 : {
296 43529 : write_bit = 0;
297 43529 : move16();
298 : }
299 :
300 63552 : values[i] = abs_s( values[i] );
301 63552 : move16();
302 :
303 63552 : push_next_indice( hBstr, write_bit, 1 );
304 : }
305 :
306 3972 : bits_written_arith_enc = hBstr->nb_bits_tot;
307 3972 : move16();
308 3972 : next_ind_pos_arith_enc = hBstr->nb_ind_tot;
309 3972 : move16();
310 3972 : push_next_indice( hBstr, coding_strategy, 1 );
311 3972 : base2_num_bits_tot = sub( hBstr->nb_bits_tot, bits_written );
312 :
313 3972 : ivas_lfe_arith_coding_fx( hLFE, hBstr, quant_strategy, values );
314 :
315 19860 : FOR( i = 0; i < num_groups; i++ )
316 : {
317 15888 : Word16 base2_num_bits = hLFE->lfe_enc_indices_coeffs_tbl[quant_strategy][i];
318 15888 : move16();
319 15888 : base2_num_bits_tot = extract_l( L_add( base2_num_bits_tot, L_mult0( num_ele_per_grp, base2_num_bits ) ) );
320 : }
321 :
322 3972 : IF( LT_16( base2_num_bits_tot, sub( hBstr->nb_bits_tot, bits_written ) ) )
323 : {
324 415 : test();
325 415 : IF( EQ_32( quant_strategy, sub( num_quant_strategies, 1 ) ) || GE_16( add( target_bits, IVAS_LFE_ID_BITS ), base2_num_bits_tot ) )
326 : {
327 : /* reset bits buffer and code the indices with base 2 coding */
328 28743 : FOR( j = hBstr->nb_ind_tot - 1; j >= next_ind_pos_arith_enc; j-- )
329 : {
330 28328 : hBstr->ind_list[j].nb_bits = -1;
331 28328 : move16();
332 : }
333 415 : hBstr->nb_ind_tot = next_ind_pos_arith_enc;
334 415 : move16();
335 415 : hBstr->nb_bits_tot = bits_written_arith_enc;
336 415 : move16();
337 415 : coding_strategy = 1;
338 415 : move16();
339 415 : push_next_indice( hBstr, coding_strategy, 1 );
340 :
341 2075 : FOR( i = 0; i < num_groups; i++ )
342 : {
343 1660 : Word16 base2_write_bits = hLFE->lfe_enc_indices_coeffs_tbl[quant_strategy][i];
344 1660 : move16();
345 1660 : Word16 offset = imult1616( 4, i );
346 1660 : move16();
347 1660 : max_value = ivas_lfe_num_ele_in_coder_models[quant_strategy][i];
348 1660 : move16();
349 :
350 8300 : FOR( j = 0; j < num_ele_per_grp; j++ )
351 : {
352 6640 : if ( GT_16( values[j + offset], max_value ) )
353 : {
354 0 : values[j + offset] = max_value;
355 0 : move16();
356 : }
357 6640 : push_next_indice( hBstr, values[j + offset], base2_write_bits );
358 : }
359 : }
360 3972 : BREAK;
361 : }
362 : }
363 : ELSE
364 : {
365 3557 : IF( GE_16( add( target_bits, IVAS_LFE_ID_BITS ), sub( hBstr->nb_bits_tot, bits_written ) ) )
366 : {
367 3557 : BREAK;
368 : }
369 : ELSE
370 : {
371 0 : IF( LT_32( quant_strategy, sub( num_quant_strategies, 1 ) ) )
372 : {
373 0 : FOR( j = hBstr->nb_ind_tot - 1; j >= nb_ind_tot; j-- )
374 : {
375 0 : hBstr->ind_list[j].nb_bits = -1;
376 0 : move16();
377 : }
378 :
379 0 : hBstr->nb_bits_tot = bits_written;
380 0 : move16();
381 0 : hBstr->nb_ind_tot = nb_ind_tot;
382 0 : move16();
383 : }
384 : }
385 : }
386 : }
387 :
388 : /* bits spent for LFE coding */
389 3972 : hLFE->lfe_bits = sub( hBstr->nb_bits_tot, bits_written );
390 3972 : move16();
391 :
392 3972 : return;
393 : }
394 :
395 :
396 : /*-----------------------------------------------------------------------------------------*
397 : * Function ivas_lfe_enc()
398 : *
399 : * LFE channel encoder
400 : *-----------------------------------------------------------------------------------------*/
401 :
402 26420 : void ivas_lfe_enc_fx(
403 : LFE_ENC_HANDLE hLFE, /* i/o: LFE encoder handle */
404 : Word32 data_lfe_ch[], /* i : input LFE signal */
405 : Word16 q_data_lfe_ch, /* i : Q of input LFE signal */
406 : const Word16 input_frame, /* i : input frame length per channel */
407 : BSTR_ENC_HANDLE hBstr /* i/o: bitstream handle */
408 : )
409 : {
410 : Word32 t_audio_fx[L_FRAME48k];
411 : Word32 wtda_audio_fx[L_FRAME48k];
412 : Word32 lfe_dct_fx[IVAS_LFE_MAX_NUM_DCT_COEFFS];
413 : Word16 num_dct_pass_bins;
414 : Word16 fade_len, full_len, dct_len, zero_pad_len;
415 : Word16 q_out1, q_out2, q_tmp1, q_tmp2;
416 : const Word32 *pWindow_coeffs_fx;
417 :
418 : /* Initializations */
419 26420 : num_dct_pass_bins = IVAS_LFE_MAX_NUM_DCT_PASS_BINS;
420 26420 : move16();
421 26420 : fade_len = hLFE->pWindow_state->fade_len;
422 26420 : move16();
423 26420 : full_len = hLFE->pWindow_state->full_len;
424 26420 : move16();
425 26420 : dct_len = hLFE->pWindow_state->dct_len;
426 26420 : move16();
427 26420 : zero_pad_len = hLFE->pWindow_state->zero_pad_len;
428 26420 : move16();
429 :
430 26420 : pWindow_coeffs_fx = hLFE->pWindow_state->pWindow_coeffs_fx;
431 :
432 :
433 26420 : IF( LT_16( q_data_lfe_ch, hLFE->q_old_wtda_audio ) )
434 : {
435 334 : Scale_sig32( hLFE->old_wtda_audio_fx, fade_len, sub( q_data_lfe_ch, hLFE->q_old_wtda_audio ) ); // hLFE->q_old_wtda_audio
436 334 : hLFE->q_old_wtda_audio = q_data_lfe_ch;
437 334 : move16();
438 : }
439 :
440 26420 : IF( GT_16( q_data_lfe_ch, hLFE->q_old_wtda_audio ) )
441 : {
442 0 : Scale_sig32( data_lfe_ch, input_frame, sub( hLFE->q_old_wtda_audio, q_data_lfe_ch ) ); // hLFE->q_old_wtda_audio;
443 0 : q_data_lfe_ch = hLFE->q_old_wtda_audio;
444 0 : move16();
445 : }
446 :
447 :
448 : /* Windowing */
449 26420 : ivas_dct_windowing_fx( fade_len, full_len, dct_len, zero_pad_len, pWindow_coeffs_fx, input_frame, wtda_audio_fx, hLFE->old_wtda_audio_fx, data_lfe_ch );
450 :
451 26420 : q_out1 = Find_Max_Norm32( wtda_audio_fx, input_frame );
452 26420 : q_out1 = sub( q_out1, find_guarded_bits_fx( dct_len ) );
453 26420 : Scale_sig32( wtda_audio_fx, input_frame, q_out1 ); // Q10 + q_out1
454 26420 : q_out1 = add( q_data_lfe_ch, q_out1 );
455 :
456 26420 : ivas_mdct_fx( wtda_audio_fx, t_audio_fx, dct_len, &q_out1 );
457 :
458 26420 : Copy32( t_audio_fx, lfe_dct_fx, num_dct_pass_bins );
459 :
460 : /* windowing */
461 26420 : ivas_dct_windowing_fx( fade_len, full_len, dct_len, zero_pad_len, pWindow_coeffs_fx, input_frame, wtda_audio_fx, hLFE->old_wtda_audio_fx, data_lfe_ch + dct_len );
462 :
463 : /* mdct */
464 26420 : q_out2 = Find_Max_Norm32( wtda_audio_fx, input_frame );
465 26420 : q_out2 = sub( q_out2, find_guarded_bits_fx( dct_len ) );
466 26420 : Scale_sig32( wtda_audio_fx, input_frame, q_out2 ); // Q10 + q_out2
467 26420 : q_out2 = add( q_data_lfe_ch, q_out2 );
468 26420 : ivas_mdct_fx( wtda_audio_fx, t_audio_fx, dct_len, &q_out2 );
469 :
470 26420 : Copy32( t_audio_fx, lfe_dct_fx + num_dct_pass_bins, num_dct_pass_bins );
471 :
472 26420 : q_tmp1 = Find_Max_Norm32( lfe_dct_fx, num_dct_pass_bins );
473 26420 : q_tmp2 = Find_Max_Norm32( lfe_dct_fx + num_dct_pass_bins, num_dct_pass_bins );
474 :
475 26420 : IF( GT_16( q_out1, q_out2 ) )
476 : {
477 7069 : Scale_sig32( lfe_dct_fx + num_dct_pass_bins, num_dct_pass_bins, s_min( q_tmp2, sub( q_out1, q_out2 ) ) );
478 7069 : q_out2 = add( q_out2, s_min( q_tmp2, sub( q_out1, q_out2 ) ) );
479 7069 : IF( GT_16( q_out1, q_out2 ) )
480 : {
481 25 : Scale_sig32( lfe_dct_fx, num_dct_pass_bins, sub( q_out2, q_out1 ) ); // q_out2
482 25 : q_out1 = q_out2;
483 25 : move16();
484 : }
485 : }
486 19351 : ELSE IF( LT_16( q_out1, q_out2 ) )
487 : {
488 6552 : Scale_sig32( lfe_dct_fx, num_dct_pass_bins, s_min( q_tmp1, sub( q_out2, q_out1 ) ) );
489 6552 : q_out1 = add( q_out1, s_min( q_tmp1, sub( q_out2, q_out1 ) ) );
490 6552 : IF( LT_16( q_out1, q_out2 ) )
491 : {
492 0 : Scale_sig32( lfe_dct_fx + num_dct_pass_bins, num_dct_pass_bins, sub( q_out1, q_out2 ) ); // q_out1
493 0 : q_out1 = q_out2;
494 0 : move16();
495 : }
496 : }
497 :
498 26420 : ivas_lfe_enc_quant_fx( hLFE, lfe_dct_fx, q_out1, hBstr );
499 :
500 26420 : return;
501 : }
502 :
503 :
504 : /*-------------------------------------------------------------------------
505 : * ivas_create_lfe_enc()
506 : *
507 : * Create, allocate and initialize IVAS encoder LFE handle
508 : *-------------------------------------------------------------------------*/
509 334 : ivas_error ivas_create_lfe_enc_fx(
510 : LFE_ENC_HANDLE *hLFE_out, /* o : IVAS LFE encoder structure */
511 : const Word32 input_Fs /* i : input sampling rate */
512 : )
513 : {
514 : Word16 input_frame;
515 : LFE_ENC_HANDLE hLFE;
516 : Word16 i, j;
517 :
518 334 : input_frame = extract_l( Mpy_32_16_1( input_Fs, INV_FRAME_PER_SEC_Q15 ) );
519 :
520 : /*-----------------------------------------------------------------*
521 : * Allocate LFE handle
522 : *-----------------------------------------------------------------*/
523 :
524 334 : IF( ( hLFE = (LFE_ENC_HANDLE) malloc( sizeof( LFE_ENC_DATA ) ) ) == NULL )
525 : {
526 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LFE\n" ) );
527 : }
528 :
529 : /* initializations */
530 334 : hLFE->lfe_bits = 0;
531 334 : move16();
532 334 : hLFE->pWindow_state = NULL;
533 334 : hLFE->hBstr = NULL;
534 :
535 : /*-----------------------------------------------------------------*
536 : * Input memory buffer: allocate and initialize
537 : *-----------------------------------------------------------------*/
538 :
539 334 : IF( ( hLFE->old_wtda_audio_fx = (Word32 *) malloc( sizeof( hLFE->old_wtda_audio_fx[0] ) * NS2SA_FX2( input_Fs, IVAS_LFE_FADE_NS ) ) ) == NULL )
540 : {
541 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LFE memory\n" ) );
542 : }
543 :
544 334 : set32_fx( hLFE->old_wtda_audio_fx, 0, NS2SA_FX2( input_Fs, IVAS_LFE_FADE_NS ) );
545 334 : hLFE->q_old_wtda_audio = 31;
546 334 : move16();
547 :
548 : /*-----------------------------------------------------------------*
549 : * LFE Window: allocate and initialize
550 : *-----------------------------------------------------------------*/
551 :
552 334 : IF( ( hLFE->pWindow_state = (LFE_WINDOW_HANDLE) malloc( sizeof( LFE_WINDOW_DATA ) ) ) == NULL )
553 : {
554 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LFE window structure\n" ) );
555 : }
556 :
557 334 : ivas_lfe_window_init_fx( hLFE->pWindow_state, input_Fs, input_frame );
558 :
559 : /* Initialization for entropy coding */
560 334 : hLFE->cum_freq_models[0][0] = ivas_str_lfe_freq_models.entropy_coder_model_fine_sg1;
561 334 : hLFE->cum_freq_models[0][1] = ivas_str_lfe_freq_models.entropy_coder_model_fine_sg2;
562 334 : hLFE->cum_freq_models[0][2] = ivas_str_lfe_freq_models.entropy_coder_model_fine_sg3;
563 334 : hLFE->cum_freq_models[0][3] = ivas_str_lfe_freq_models.entropy_coder_model_fine_sg4;
564 334 : hLFE->cum_freq_models[1][0] = ivas_str_lfe_freq_models.entropy_coder_model_coarse_sg1;
565 334 : hLFE->cum_freq_models[1][1] = ivas_str_lfe_freq_models.entropy_coder_model_coarse_sg2;
566 334 : hLFE->cum_freq_models[1][2] = ivas_str_lfe_freq_models.entropy_coder_model_coarse_sg3;
567 334 : hLFE->cum_freq_models[1][3] = &ivas_str_lfe_freq_models.entropy_coder_model_coarse_sg4;
568 :
569 : /* Initialization base2 bits for each subgroup for no entropy coding */
570 1002 : FOR( i = 0; i < IVAS_MAX_NUM_QUANT_STRATS; i++ )
571 : {
572 3340 : FOR( j = 0; j < IVAS_MAX_NUM_DCT_COEF_GROUPS; j++ )
573 : {
574 : // hLFE->lfe_enc_indices_coeffs_tbl[i][j] =
575 : // (int16_t) ceilf( log2f( (float) ( ivas_lfe_num_ele_in_coder_models[i][j] + 1 ) ) );
576 5344 : hLFE->lfe_enc_indices_coeffs_tbl[i][j] =
577 2672 : floor_log_2( add( ivas_lfe_num_ele_in_coder_models[i][j], 1 ) ); // Q0
578 2672 : move16();
579 : }
580 : }
581 :
582 334 : *hLFE_out = hLFE;
583 :
584 334 : return IVAS_ERR_OK;
585 : }
586 :
587 : /*-------------------------------------------------------------------------
588 : * ivas_lfe_enc_close()
589 : *
590 : * Destroy IVAS cncoder LFE handle
591 : *-------------------------------------------------------------------------*/
592 902 : void ivas_lfe_enc_close_fx(
593 : LFE_ENC_HANDLE *hLFE /* i/o: LFE encoder handle */
594 : )
595 : {
596 902 : test();
597 902 : IF( hLFE == NULL || *hLFE == NULL )
598 : {
599 568 : return;
600 : }
601 :
602 334 : IF( ( *hLFE )->old_wtda_audio_fx != NULL )
603 : {
604 334 : free( ( *hLFE )->old_wtda_audio_fx );
605 334 : ( *hLFE )->old_wtda_audio_fx = NULL;
606 : }
607 :
608 334 : IF( ( *hLFE )->pWindow_state )
609 : {
610 334 : free( ( *hLFE )->pWindow_state );
611 334 : ( *hLFE )->pWindow_state = NULL;
612 : }
613 :
614 334 : free( ( *hLFE ) );
615 334 : ( *hLFE ) = NULL;
616 :
617 334 : return;
618 : }
619 :
620 : /*-------------------------------------------------------------------------
621 : * ivas_create_lfe_lpf_enc()
622 : *
623 : * Create, allocate and initialize IVAS encoder LFE low pass filter state handle
624 : *-------------------------------------------------------------------------*/
625 81 : ivas_error ivas_create_lfe_lpf_enc_fx(
626 : ivas_filters_process_state_t **hLfeLpf, /* o : LFE LPF handle */
627 : const Word32 input_Fs /* i : input sampling rate */
628 : )
629 : {
630 : const Word32 *filt_coeff; // 31 - filt_coeff_e
631 81 : const Word16 *filt_coeff_e = NULL;
632 :
633 81 : IF( hLfeLpf == NULL )
634 : {
635 0 : return ( IVAS_ERROR( IVAS_ERR_UNEXPECTED_NULL_POINTER, "Can not allocate memory for LFE LPF\n" ) );
636 : }
637 :
638 : /*-----------------------------------------------------------------*
639 : * Allocate LFE LPF handle
640 : *-----------------------------------------------------------------*/
641 :
642 81 : IF( ( *hLfeLpf = (ivas_filters_process_state_t *) malloc( sizeof( ivas_filters_process_state_t ) ) ) == NULL )
643 : {
644 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LFE LPF\n" ) );
645 : }
646 :
647 81 : ivas_lfe_lpf_select_filt_coeff_fx( input_Fs, IVAS_FILTER_ORDER_4, &filt_coeff, &filt_coeff_e );
648 :
649 81 : ivas_filters_init_fx( *hLfeLpf, filt_coeff, filt_coeff_e, IVAS_FILTER_ORDER_4 );
650 :
651 81 : return IVAS_ERR_OK;
652 : }
653 :
654 : /*-------------------------------------------------------------------------
655 : * ivas_lfe_lpf_enc_close()
656 : *
657 : * Destroy IVAS cncoder LFE low pass filter state
658 : *-------------------------------------------------------------------------*/
659 624 : void ivas_lfe_lpf_enc_close_fx(
660 : ivas_filters_process_state_t **hLfeLpf /* i/o: LFE LPF handle */
661 : )
662 : {
663 624 : test();
664 624 : IF( hLfeLpf == NULL || *hLfeLpf == NULL )
665 : {
666 543 : return;
667 : }
668 :
669 81 : free( ( *hLfeLpf ) );
670 81 : ( *hLfeLpf ) = NULL;
671 :
672 81 : return;
673 : }
674 :
675 :
676 : /*-------------------------------------------------------------------------
677 : * ivas_lfe_lpf_enc_apply()
678 : *
679 : * Apply IVAS cncoder LFE low pass filter
680 : *-------------------------------------------------------------------------*/
681 47850 : void ivas_lfe_lpf_enc_apply_fx(
682 : ivas_filters_process_state_t *hLfeLpf, /* i/o: LFE LPF handle */
683 : Word32 data_lfe_ch[], /* i/o: LFE signal Q11*/
684 : const Word16 input_frame /* i : input frame length per channel */
685 : )
686 : {
687 47850 : ivas_filter_process_fx( hLfeLpf, data_lfe_ch, input_frame, (Word16) Q11 );
688 :
689 47850 : return;
690 : }
|