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 612889 : 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 612889 : Word16 *pCum_freq = NULL;
65 : Tastat as;
66 612889 : Word16 extra_bits_read = 0;
67 612889 : move16();
68 :
69 612889 : test();
70 612889 : IF( ( in_len > 0 ) && GT_16( pArith->range, 1 ) )
71 : {
72 548008 : IF( pArith->dyn_model_bits > 0 )
73 : {
74 548008 : model_idx = get_next_indice_fx( st0, pArith->dyn_model_bits );
75 :
76 548008 : IF( model_idx > 0 )
77 : {
78 269906 : pCum_freq = pArith->cum_freq[model_idx];
79 : }
80 : ELSE
81 : {
82 278102 : pCum_freq = pArith->cum_freq[0];
83 : }
84 : }
85 : ELSE
86 : {
87 0 : pCum_freq = pArith->cum_freq[0];
88 : }
89 :
90 548008 : ivas_ari_start_decoding_14bits_ext_1_lfe( st0, &as, &extra_bits_read );
91 :
92 12199760 : FOR( i = 0; i < in_len; i++ )
93 : {
94 11651752 : ind = ivas_ari_decode_14bits_bit_ext_1_lfe( st0, &as, (const UWord16 *) pCum_freq, &extra_bits_read );
95 :
96 11651752 : pSymbols[i] = pArith->vals[ind];
97 11651752 : move16();
98 : }
99 :
100 548008 : ivas_ari_done_decoding_14bits_ext_1_lfe( st0, extra_bits_read );
101 : }
102 : ELSE
103 : {
104 :
105 64881 : FOR( i = 0; i < in_len; i++ )
106 : {
107 0 : pSymbols[i] = 0;
108 0 : move16();
109 : }
110 : }
111 :
112 612889 : return;
113 : }
114 :
115 :
116 : /*-----------------------------------------------------------------------------------------*
117 : * Function description ivas_arithCoder_decode_array_diff()
118 : *
119 : * Diffrential arith decoding
120 : *-----------------------------------------------------------------------------------------*/
121 :
122 223962 : 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 223962 : Word16 offset = pArith->vals[0];
133 223962 : move16();
134 :
135 223962 : IF( length > 0 )
136 : {
137 199750 : ivas_arith_decode_array( pArith_diff, st0, length, pOutput_arr );
138 : }
139 :
140 3095748 : FOR( n = 0; n < length; n++ )
141 : {
142 2871786 : pOutput_arr[n] = add( sub( pSymbol_old[n], offset ), pOutput_arr[n] );
143 2871786 : move16();
144 : }
145 :
146 223962 : ivas_wrap_arround_fx( pOutput_arr, 0, sub( pArith_diff->range, 1 ), length );
147 :
148 3095748 : FOR( n = 0; n < length; n++ )
149 : {
150 2871786 : pOutput_arr[n] = pArith->vals[pOutput_arr[n]];
151 2871786 : move16();
152 : }
153 :
154 223962 : return;
155 : }
156 :
157 :
158 : /*-----------------------------------------------------------------------------------------*
159 : * Function ivas_huffman_code_bits_present()
160 : *
161 : * Huffman code bits present
162 : *-----------------------------------------------------------------------------------------*/
163 :
164 125276 : 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 125276 : Word16 index = add( len, 1 );
171 125276 : Word16 i = 0;
172 125276 : move16();
173 : Word16 ind_t, code_t, bits_t;
174 :
175 2675664 : WHILE( i < len )
176 : {
177 2618852 : ind_t = codebook[0];
178 2618852 : move16();
179 2618852 : bits_t = codebook[1];
180 2618852 : move16();
181 2618852 : code_t = codebook[2];
182 2618852 : move16();
183 :
184 2618852 : test();
185 2618852 : IF( ( EQ_16( code, code_t ) ) && ( EQ_16( bits, bits_t ) ) )
186 : {
187 68464 : return ind_t;
188 : }
189 2550388 : codebook = codebook + 3;
190 2550388 : i = add( i, 1 );
191 : }
192 :
193 56812 : return index;
194 : }
195 :
196 :
197 : /*-----------------------------------------------------------------------------------------*
198 : * Function ivas_huffman_decode()
199 : *
200 : * Huffman decoding on the encoded stream
201 : *-----------------------------------------------------------------------------------------*/
202 :
203 68464 : 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 68464 : code = get_next_indice_fx( st0, huff_cfg->min_len );
211 68464 : num_bits_read = huff_cfg->min_len;
212 68464 : move16();
213 :
214 68464 : ind = ivas_huffman_code_bits_present( huff_cfg->codebook, code, num_bits_read, huff_cfg->sym_len );
215 :
216 125276 : WHILE( ind > huff_cfg->sym_len )
217 : {
218 56812 : bit = get_next_indice_fx( st0, 1 );
219 56812 : num_bits_read = add( num_bits_read, 1 );
220 56812 : code = s_or( shl( code, 1 ), bit );
221 56812 : ind = ivas_huffman_code_bits_present( huff_cfg->codebook, code, num_bits_read, huff_cfg->sym_len );
222 56812 : IF( GT_16( num_bits_read, huff_cfg->max_len ) )
223 : {
224 0 : return IVAS_ERR_INTERNAL;
225 : }
226 : }
227 68464 : *dec_out = ind;
228 68464 : move16();
229 :
230 68464 : return IVAS_ERR_OK;
231 : }
232 :
233 :
234 : /*-----------------------------------------------------------------------------------------*
235 : * Function arith_decode_cell_array()
236 : *
237 : * Arithman decoding of cell array
238 : *-----------------------------------------------------------------------------------------*/
239 :
240 413139 : 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 413139 : Word16 total_symbol_len = 0;
249 413139 : move16();
250 : Word16 i;
251 :
252 3837675 : FOR( i = 0; i < num_bands; i++ )
253 : {
254 3424536 : total_symbol_len = add( total_symbol_len, imult1616( pCell_dims[i].dim1, pCell_dims[i].dim2 ) );
255 : }
256 :
257 413139 : assert( LE_16( total_symbol_len, IVAS_MAX_INPUT_LEN ) );
258 :
259 413139 : ivas_arith_decode_array( pArith, st0, total_symbol_len, pSymbol );
260 :
261 413139 : return;
262 : }
263 :
264 :
265 : /*-----------------------------------------------------------------------------------------*
266 : * Function arith_decode_cell_array_diff()
267 : *
268 : * Arithman decoding of differential cell array
269 : *-----------------------------------------------------------------------------------------*/
270 :
271 223962 : 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 223962 : Word16 total_symbol_len = 0;
282 223962 : move16();
283 : Word16 i;
284 :
285 2015658 : FOR( i = 0; i < num_bands; i++ )
286 : {
287 1791696 : total_symbol_len = add( total_symbol_len, imult1616( pCell_dims[i].dim1, pCell_dims[i].dim2 ) );
288 : }
289 :
290 223962 : assert( LE_16( total_symbol_len, IVAS_MAX_INPUT_LEN ) );
291 :
292 223962 : ivas_arithCoder_decode_array_diff( pArith, pArith_diff, pSymbol_old, total_symbol_len, st0, pSymbol );
293 :
294 223962 : return;
295 : }
296 :
297 : /*-----------------------------------------------------------------------------------------*
298 : * Function ivas_arith_decode_cmplx_cell_array()
299 : *
300 : * Arithman decoding of complex cell array
301 : *-----------------------------------------------------------------------------------------*/
302 :
303 413139 : 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 413139 : Word16 i, j, len, all_diff = 1, any_diff = 0;
315 413139 : move16();
316 413139 : 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 413139 : Word16 idx2 = 0;
321 413139 : move16();
322 :
323 3837675 : FOR( i = 0; i < num_bands; i++ )
324 : {
325 3424536 : IF( pDo_diff[i] != 0 )
326 : {
327 1343772 : any_diff = 1;
328 1343772 : move16();
329 : }
330 : ELSE
331 : {
332 2080764 : all_diff = 0;
333 2080764 : move16();
334 : }
335 : }
336 :
337 413139 : IF( EQ_16( any_diff, 1 ) )
338 : {
339 223962 : 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 223962 : Word16 idx1 = 0, idx = 0;
348 223962 : move16();
349 223962 : move16();
350 :
351 2015658 : FOR( i = 0; i < num_bands; i++ )
352 : {
353 1791696 : len = imult1616( pCell_dims[i].dim1, pCell_dims[i].dim2 );
354 1791696 : IF( pDo_diff[i] != 0 )
355 : {
356 1343772 : cell_dims[i].dim1 = 0;
357 1343772 : move16();
358 1343772 : cell_dims[i].dim2 = 0;
359 1343772 : move16();
360 :
361 4215558 : FOR( j = 0; j < len; j++ )
362 : {
363 2871786 : cell_arr_diff[idx] = pSymbol_re_old[idx1];
364 2871786 : move16();
365 2871786 : idx = add( idx, 1 );
366 2871786 : idx1 = add( idx1, 1 );
367 : }
368 :
369 1343772 : cell_dims_diff[i].dim1 = pCell_dims[i].dim1;
370 1343772 : move16();
371 1343772 : cell_dims_diff[i].dim2 = pCell_dims[i].dim2;
372 1343772 : move16();
373 : }
374 : ELSE
375 : {
376 1405186 : FOR( j = 0; j < len; j++ )
377 : {
378 957262 : cell_arr_diff[idx] = 0;
379 957262 : move16();
380 957262 : idx1 = add( idx1, 1 );
381 : }
382 :
383 447924 : cell_dims[i].dim1 = pCell_dims[i].dim1;
384 447924 : move16();
385 447924 : cell_dims[i].dim2 = pCell_dims[i].dim2;
386 447924 : move16();
387 447924 : cell_dims_diff[i].dim1 = 0;
388 447924 : move16();
389 447924 : cell_dims_diff[i].dim2 = 0;
390 447924 : move16();
391 : }
392 : }
393 :
394 223962 : arith_decode_cell_array( cell_dims, st0, num_bands, pArith_re, cell_arr_no_diff );
395 :
396 223962 : 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 223962 : idx = 0;
399 223962 : move16();
400 223962 : idx1 = 0;
401 223962 : move16();
402 :
403 2015658 : FOR( i = 0; i < num_bands; i++ )
404 : {
405 1791696 : IF( pDo_diff[i] != 0 )
406 : {
407 4215558 : FOR( j = 0; j < cell_dims_diff[i].dim1 * cell_dims_diff[i].dim2; j++ )
408 : {
409 2871786 : pSymbol_re[idx] = cell_arr_diff_out[idx2];
410 2871786 : move16();
411 2871786 : idx = add( idx, 1 );
412 2871786 : idx2 = add( idx2, 1 );
413 : }
414 : }
415 : ELSE
416 : {
417 1405186 : FOR( j = 0; j < cell_dims[i].dim1 * cell_dims[i].dim2; j++ )
418 : {
419 957262 : pSymbol_re[idx] = cell_arr_no_diff[idx1];
420 957262 : move16();
421 957262 : idx = add( idx, 1 );
422 957262 : idx1 = add( idx1, 1 );
423 : }
424 : }
425 : }
426 : }
427 : }
428 : ELSE
429 : {
430 189177 : arith_decode_cell_array( pCell_dims, st0, num_bands, pArith_re, pSymbol_re );
431 : }
432 :
433 413139 : return;
434 : }
|