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 "prot_fx.h"
36 : #include "ivas_cnst.h"
37 : #include "ivas_rom_com.h"
38 : #include <assert.h>
39 : #include "wmc_auto.h"
40 : #include "ivas_prot_fx.h"
41 :
42 :
43 : /*------------------------------------------------------------------------------------------*
44 : * Static function declarations
45 : *------------------------------------------------------------------------------------------*/
46 :
47 : static Word16 ivas_huffman_code_bits_present( const Word16 *codebook, const Word16 code, const Word16 bits, const Word16 len );
48 :
49 :
50 : /*-----------------------------------------------------------------------------------------*
51 : * Function ivas_arith_decode_array()
52 : *
53 : * Arith decoding of an array
54 : *-----------------------------------------------------------------------------------------*/
55 :
56 631077 : static void ivas_arith_decode_array(
57 : ivas_arith_t *pArith,
58 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
59 : const Word16 in_len,
60 : Word16 *pSymbols /*Q0*/
61 : )
62 : {
63 : Word16 i, ind, model_idx;
64 631077 : Word16 *pCum_freq = NULL;
65 : Tastat as;
66 631077 : Word16 extra_bits_read = 0;
67 631077 : move16();
68 :
69 631077 : test();
70 631077 : IF( ( in_len > 0 ) && GT_16( pArith->range, 1 ) )
71 : {
72 561564 : IF( pArith->dyn_model_bits > 0 )
73 : {
74 561564 : model_idx = get_next_indice_fx( st0, pArith->dyn_model_bits );
75 :
76 561564 : IF( model_idx > 0 )
77 : {
78 276376 : pCum_freq = pArith->cum_freq[model_idx];
79 : }
80 : ELSE
81 : {
82 285188 : pCum_freq = pArith->cum_freq[0];
83 : }
84 : }
85 : ELSE
86 : {
87 0 : pCum_freq = pArith->cum_freq[0];
88 : }
89 :
90 561564 : ivas_ari_start_decoding_14bits_ext_1_lfe( st0, &as, &extra_bits_read );
91 :
92 12399320 : FOR( i = 0; i < in_len; i++ )
93 : {
94 11837756 : ind = ivas_ari_decode_14bits_bit_ext_1_lfe( st0, &as, (const UWord16 *) pCum_freq, &extra_bits_read );
95 :
96 11837756 : pSymbols[i] = pArith->vals[ind];
97 11837756 : move16();
98 : }
99 :
100 561564 : ivas_ari_done_decoding_14bits_ext_1_lfe( st0, extra_bits_read );
101 : }
102 : ELSE
103 : {
104 :
105 69513 : FOR( i = 0; i < in_len; i++ )
106 : {
107 0 : pSymbols[i] = 0;
108 0 : move16();
109 : }
110 : }
111 :
112 631077 : return;
113 : }
114 :
115 :
116 : /*-----------------------------------------------------------------------------------------*
117 : * Function description ivas_arithCoder_decode_array_diff()
118 : *
119 : * Diffrential arith decoding
120 : *-----------------------------------------------------------------------------------------*/
121 :
122 227580 : static void ivas_arithCoder_decode_array_diff(
123 : ivas_arith_t *pArith,
124 : ivas_arith_t *pArith_diff,
125 : Word16 *pSymbol_old, /*Q0*/
126 : const Word16 length,
127 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
128 : Word16 *pOutput_arr /*Q0*/
129 : )
130 : {
131 : Word16 n;
132 227580 : Word16 offset = pArith->vals[0];
133 227580 : move16();
134 :
135 227580 : IF( length > 0 )
136 : {
137 203259 : ivas_arith_decode_array( pArith_diff, st0, length, pOutput_arr );
138 : }
139 :
140 3149412 : FOR( n = 0; n < length; n++ )
141 : {
142 2921832 : pOutput_arr[n] = add( sub( pSymbol_old[n], offset ), pOutput_arr[n] );
143 2921832 : move16();
144 : }
145 :
146 227580 : ivas_wrap_arround_fx( pOutput_arr, 0, sub( pArith_diff->range, 1 ), length );
147 :
148 3149412 : FOR( n = 0; n < length; n++ )
149 : {
150 2921832 : pOutput_arr[n] = pArith->vals[pOutput_arr[n]];
151 2921832 : move16();
152 : }
153 :
154 227580 : return;
155 : }
156 :
157 :
158 : /*-----------------------------------------------------------------------------------------*
159 : * Function ivas_huffman_code_bits_present()
160 : *
161 : * Huffman code bits present
162 : *-----------------------------------------------------------------------------------------*/
163 :
164 126786 : static Word16 ivas_huffman_code_bits_present(
165 : const Word16 *codebook, /*Q0*/
166 : const Word16 code,
167 : const Word16 bits,
168 : const Word16 len )
169 : {
170 126786 : Word16 index = add( len, 1 );
171 126786 : Word16 i = 0;
172 126786 : move16();
173 : Word16 ind_t, code_t, bits_t;
174 :
175 2712092 : WHILE( i < len )
176 : {
177 2654474 : ind_t = codebook[0];
178 2654474 : move16();
179 2654474 : bits_t = codebook[1];
180 2654474 : move16();
181 2654474 : code_t = codebook[2];
182 2654474 : move16();
183 :
184 2654474 : test();
185 2654474 : IF( ( EQ_16( code, code_t ) ) && ( EQ_16( bits, bits_t ) ) )
186 : {
187 69168 : return ind_t;
188 : }
189 2585306 : codebook = codebook + 3;
190 2585306 : i = add( i, 1 );
191 : }
192 :
193 57618 : return index;
194 : }
195 :
196 :
197 : /*-----------------------------------------------------------------------------------------*
198 : * Function ivas_huffman_decode()
199 : *
200 : * Huffman decoding on the encoded stream
201 : *-----------------------------------------------------------------------------------------*/
202 :
203 69168 : ivas_error ivas_huffman_decode(
204 : ivas_huffman_cfg_t *huff_cfg,
205 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
206 : Word16 *dec_out )
207 : {
208 : Word16 code, num_bits_read, ind, bit;
209 :
210 69168 : code = get_next_indice_fx( st0, huff_cfg->min_len );
211 69168 : num_bits_read = huff_cfg->min_len;
212 69168 : move16();
213 :
214 69168 : ind = ivas_huffman_code_bits_present( huff_cfg->codebook, code, num_bits_read, huff_cfg->sym_len );
215 :
216 126786 : WHILE( ind > huff_cfg->sym_len )
217 : {
218 57618 : bit = get_next_indice_fx( st0, 1 );
219 57618 : num_bits_read = add( num_bits_read, 1 );
220 57618 : code = s_or( shl( code, 1 ), bit );
221 57618 : ind = ivas_huffman_code_bits_present( huff_cfg->codebook, code, num_bits_read, huff_cfg->sym_len );
222 57618 : IF( GT_16( num_bits_read, huff_cfg->max_len ) )
223 : {
224 0 : return IVAS_ERR_INTERNAL;
225 : }
226 : }
227 69168 : *dec_out = ind;
228 69168 : move16();
229 :
230 69168 : return IVAS_ERR_OK;
231 : }
232 :
233 :
234 : /*-----------------------------------------------------------------------------------------*
235 : * Function arith_decode_cell_array()
236 : *
237 : * Arithman decoding of cell array
238 : *-----------------------------------------------------------------------------------------*/
239 :
240 427818 : static void arith_decode_cell_array(
241 : ivas_cell_dim_t *pCell_dims,
242 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
243 : Word16 num_bands,
244 : ivas_arith_t *pArith,
245 : Word16 *pSymbol /*Q0*/
246 : )
247 : {
248 427818 : Word16 total_symbol_len = 0;
249 427818 : move16();
250 : Word16 i;
251 :
252 3967638 : FOR( i = 0; i < num_bands; i++ )
253 : {
254 3539820 : total_symbol_len = add( total_symbol_len, imult1616( pCell_dims[i].dim1, pCell_dims[i].dim2 ) );
255 : }
256 :
257 427818 : assert( LE_16( total_symbol_len, IVAS_MAX_INPUT_LEN ) );
258 :
259 427818 : ivas_arith_decode_array( pArith, st0, total_symbol_len, pSymbol );
260 :
261 427818 : return;
262 : }
263 :
264 :
265 : /*-----------------------------------------------------------------------------------------*
266 : * Function arith_decode_cell_array_diff()
267 : *
268 : * Arithman decoding of differential cell array
269 : *-----------------------------------------------------------------------------------------*/
270 :
271 227580 : static void arith_decode_cell_array_diff(
272 : ivas_cell_dim_t *pCell_dims,
273 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
274 : Word16 num_bands,
275 : ivas_arith_t *pArith,
276 : ivas_arith_t *pArith_diff,
277 : Word16 *pSymbol, /*Q0*/
278 : Word16 *pSymbol_old /*Q0*/
279 : )
280 : {
281 227580 : Word16 total_symbol_len = 0;
282 227580 : move16();
283 : Word16 i;
284 :
285 2048220 : FOR( i = 0; i < num_bands; i++ )
286 : {
287 1820640 : total_symbol_len = add( total_symbol_len, imult1616( pCell_dims[i].dim1, pCell_dims[i].dim2 ) );
288 : }
289 :
290 227580 : assert( LE_16( total_symbol_len, IVAS_MAX_INPUT_LEN ) );
291 :
292 227580 : ivas_arithCoder_decode_array_diff( pArith, pArith_diff, pSymbol_old, total_symbol_len, st0, pSymbol );
293 :
294 227580 : return;
295 : }
296 :
297 : /*-----------------------------------------------------------------------------------------*
298 : * Function ivas_arith_decode_cmplx_cell_array()
299 : *
300 : * Arithman decoding of complex cell array
301 : *-----------------------------------------------------------------------------------------*/
302 :
303 427818 : void ivas_arith_decode_cmplx_cell_array(
304 : ivas_arith_t *pArith_re,
305 : ivas_arith_t *pArith_re_diff,
306 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
307 : ivas_cell_dim_t *pCell_dims,
308 : Word16 *pDo_diff, /*Q0*/
309 : const Word16 num_bands,
310 : Word16 *pSymbol_re, /*Q0*/
311 : Word16 *pSymbol_re_old /*Q0*/
312 : )
313 : {
314 427818 : Word16 i, j, len, all_diff = 1, any_diff = 0;
315 427818 : move16();
316 427818 : move16();
317 : Word16 cell_arr_diff[IVAS_MAX_INPUT_LEN];
318 : Word16 cell_arr_no_diff[IVAS_MAX_INPUT_LEN];
319 : Word16 cell_arr_diff_out[IVAS_MAX_INPUT_LEN];
320 427818 : Word16 idx2 = 0;
321 427818 : move16();
322 :
323 3967638 : FOR( i = 0; i < num_bands; i++ )
324 : {
325 3539820 : IF( pDo_diff[i] != 0 )
326 : {
327 1365480 : any_diff = 1;
328 1365480 : move16();
329 : }
330 : ELSE
331 : {
332 2174340 : all_diff = 0;
333 2174340 : move16();
334 : }
335 : }
336 :
337 427818 : IF( EQ_16( any_diff, 1 ) )
338 : {
339 227580 : IF( EQ_16( all_diff, 1 ) )
340 : {
341 0 : arith_decode_cell_array_diff( pCell_dims, st0, num_bands, pArith_re, pArith_re_diff, pSymbol_re, pSymbol_re_old );
342 : }
343 : ELSE
344 : {
345 : ivas_cell_dim_t cell_dims[IVAS_MAX_NUM_BANDS];
346 : ivas_cell_dim_t cell_dims_diff[IVAS_MAX_NUM_BANDS];
347 227580 : Word16 idx1 = 0, idx = 0;
348 227580 : move16();
349 227580 : move16();
350 :
351 2048220 : FOR( i = 0; i < num_bands; i++ )
352 : {
353 1820640 : len = imult1616( pCell_dims[i].dim1, pCell_dims[i].dim2 );
354 1820640 : IF( pDo_diff[i] != 0 )
355 : {
356 1365480 : cell_dims[i].dim1 = 0;
357 1365480 : move16();
358 1365480 : cell_dims[i].dim2 = 0;
359 1365480 : move16();
360 :
361 4287312 : FOR( j = 0; j < len; j++ )
362 : {
363 2921832 : cell_arr_diff[idx] = pSymbol_re_old[idx1];
364 2921832 : move16();
365 2921832 : idx = add( idx, 1 );
366 2921832 : idx1 = add( idx1, 1 );
367 : }
368 :
369 1365480 : cell_dims_diff[i].dim1 = pCell_dims[i].dim1;
370 1365480 : move16();
371 1365480 : cell_dims_diff[i].dim2 = pCell_dims[i].dim2;
372 1365480 : move16();
373 : }
374 : ELSE
375 : {
376 1429104 : FOR( j = 0; j < len; j++ )
377 : {
378 973944 : cell_arr_diff[idx] = 0;
379 973944 : move16();
380 973944 : idx1 = add( idx1, 1 );
381 : }
382 :
383 455160 : cell_dims[i].dim1 = pCell_dims[i].dim1;
384 455160 : move16();
385 455160 : cell_dims[i].dim2 = pCell_dims[i].dim2;
386 455160 : move16();
387 455160 : cell_dims_diff[i].dim1 = 0;
388 455160 : move16();
389 455160 : cell_dims_diff[i].dim2 = 0;
390 455160 : move16();
391 : }
392 : }
393 :
394 227580 : arith_decode_cell_array( cell_dims, st0, num_bands, pArith_re, cell_arr_no_diff );
395 :
396 227580 : arith_decode_cell_array_diff( cell_dims_diff, st0, num_bands, pArith_re, pArith_re_diff, cell_arr_diff_out, cell_arr_diff );
397 :
398 227580 : idx = 0;
399 227580 : move16();
400 227580 : idx1 = 0;
401 227580 : move16();
402 :
403 2048220 : FOR( i = 0; i < num_bands; i++ )
404 : {
405 1820640 : IF( pDo_diff[i] != 0 )
406 : {
407 4287312 : FOR( j = 0; j < cell_dims_diff[i].dim1 * cell_dims_diff[i].dim2; j++ )
408 : {
409 2921832 : pSymbol_re[idx] = cell_arr_diff_out[idx2];
410 2921832 : move16();
411 2921832 : idx = add( idx, 1 );
412 2921832 : idx2 = add( idx2, 1 );
413 : }
414 : }
415 : ELSE
416 : {
417 1429104 : FOR( j = 0; j < cell_dims[i].dim1 * cell_dims[i].dim2; j++ )
418 : {
419 973944 : pSymbol_re[idx] = cell_arr_no_diff[idx1];
420 973944 : move16();
421 973944 : idx = add( idx, 1 );
422 973944 : idx1 = add( idx1, 1 );
423 : }
424 : }
425 : }
426 : }
427 : }
428 : ELSE
429 : {
430 200238 : arith_decode_cell_array( pCell_dims, st0, num_bands, pArith_re, pSymbol_re );
431 : }
432 :
433 427818 : return;
434 : }
|