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.h"
36 : #include "ivas_prot.h"
37 : #include "ivas_cnst.h"
38 : #ifdef DEBUGGING
39 : #include "debug.h"
40 : #endif
41 : #include "ivas_rom_com.h"
42 : #include <assert.h>
43 : #include "wmc_auto.h"
44 :
45 :
46 : /*------------------------------------------------------------------------------------------*
47 : * Static function declarations
48 : *------------------------------------------------------------------------------------------*/
49 :
50 : static int16_t ivas_huffman_code_bits_present( const int16_t *codebook, const int16_t code, const int16_t bits, const int16_t len );
51 :
52 :
53 : /*-----------------------------------------------------------------------------------------*
54 : * Function ivas_arith_decode_array()
55 : *
56 : * Arith decoding of an array
57 : *-----------------------------------------------------------------------------------------*/
58 :
59 1925451 : static void ivas_arith_decode_array(
60 : ivas_arith_t *pArith,
61 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
62 : const int16_t in_len,
63 : int16_t *pSymbols )
64 : {
65 : int16_t i, ind, model_idx;
66 1925451 : int16_t *pCum_freq = NULL;
67 : Tastat as;
68 1925451 : int16_t extra_bits_read = 0;
69 :
70 1925451 : if ( in_len > 0 && pArith->range > 1 )
71 : {
72 1716225 : if ( pArith->dyn_model_bits > 0 )
73 : {
74 1716225 : model_idx = get_next_indice( st0, pArith->dyn_model_bits );
75 :
76 1716225 : if ( model_idx > 0 )
77 : {
78 846240 : pCum_freq = pArith->cum_freq[model_idx];
79 : }
80 : else
81 : {
82 869985 : pCum_freq = pArith->cum_freq[0];
83 : }
84 : }
85 : else
86 : {
87 0 : pCum_freq = pArith->cum_freq[0];
88 : }
89 :
90 1716225 : ivas_ari_start_decoding_14bits_ext_1_lfe( st0, &as, &extra_bits_read );
91 :
92 38344509 : for ( i = 0; i < in_len; i++ )
93 : {
94 36628284 : ind = ivas_ari_decode_14bits_bit_ext_1_lfe( st0, &as, (const uint16_t *) pCum_freq, &extra_bits_read );
95 :
96 36628284 : pSymbols[i] = pArith->vals[ind];
97 : }
98 :
99 1716225 : ivas_ari_done_decoding_14bits_ext_1_lfe( st0, extra_bits_read );
100 : }
101 : else
102 : {
103 209226 : for ( i = 0; i < in_len; i++ )
104 : {
105 0 : pSymbols[i] = 0;
106 : }
107 : }
108 :
109 1925451 : return;
110 : }
111 :
112 :
113 : /*-----------------------------------------------------------------------------------------*
114 : * Function description ivas_arithCoder_decode_array_diff()
115 : *
116 : * Diffrential arith decoding
117 : *-----------------------------------------------------------------------------------------*/
118 :
119 691056 : static void ivas_arithCoder_decode_array_diff(
120 : ivas_arith_t *pArith,
121 : ivas_arith_t *pArith_diff,
122 : int16_t *pSymbol_old,
123 : const int16_t length,
124 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
125 : int16_t *pOutput_arr )
126 : {
127 : int16_t n;
128 691056 : int16_t offset = pArith->vals[0];
129 :
130 691056 : if ( length > 0 )
131 : {
132 617958 : ivas_arith_decode_array( pArith_diff, st0, length, pOutput_arr );
133 : }
134 :
135 9558036 : for ( n = 0; n < length; n++ )
136 : {
137 8866980 : pOutput_arr[n] = pSymbol_old[n] - offset + pOutput_arr[n];
138 : }
139 :
140 691056 : ivas_wrap_arround( pOutput_arr, 0, pArith_diff->range - 1, length );
141 :
142 9558036 : for ( n = 0; n < length; n++ )
143 : {
144 8866980 : pOutput_arr[n] = pArith->vals[pOutput_arr[n]];
145 : }
146 :
147 691056 : return;
148 : }
149 :
150 :
151 : /*-----------------------------------------------------------------------------------------*
152 : * Function ivas_huffman_code_bits_present()
153 : *
154 : * Huffman code bits present
155 : *-----------------------------------------------------------------------------------------*/
156 :
157 381387 : static int16_t ivas_huffman_code_bits_present(
158 : const int16_t *codebook,
159 : const int16_t code,
160 : const int16_t bits,
161 : const int16_t len )
162 : {
163 381387 : int16_t index = len + 1;
164 381387 : int16_t i = 0;
165 : int16_t ind_t, code_t, bits_t;
166 :
167 8139888 : while ( i < len )
168 : {
169 7966869 : ind_t = codebook[0];
170 7966869 : bits_t = codebook[1];
171 7966869 : code_t = codebook[2];
172 7966869 : if ( ( code == code_t ) && ( bits == bits_t ) )
173 : {
174 208368 : return ind_t;
175 : }
176 7758501 : codebook = codebook + 3;
177 7758501 : i++;
178 : }
179 :
180 173019 : return index;
181 : }
182 :
183 :
184 : /*-----------------------------------------------------------------------------------------*
185 : * Function ivas_huffman_decode()
186 : *
187 : * Huffman decoding on the encoded stream
188 : *-----------------------------------------------------------------------------------------*/
189 :
190 208368 : ivas_error ivas_huffman_decode(
191 : ivas_huffman_cfg_t *huff_cfg,
192 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
193 : int16_t *dec_out )
194 : {
195 : int16_t code, num_bits_read, ind, bit;
196 :
197 208368 : code = get_next_indice( st0, huff_cfg->min_len );
198 208368 : num_bits_read = huff_cfg->min_len;
199 :
200 208368 : ind = ivas_huffman_code_bits_present( huff_cfg->codebook, code, num_bits_read, huff_cfg->sym_len );
201 :
202 381387 : while ( ind > huff_cfg->sym_len )
203 : {
204 173019 : bit = get_next_indice( st0, 1 );
205 173019 : num_bits_read += 1;
206 173019 : code = code << 1 | bit;
207 173019 : ind = ivas_huffman_code_bits_present( huff_cfg->codebook, code, num_bits_read, huff_cfg->sym_len );
208 173019 : if ( num_bits_read > huff_cfg->max_len )
209 : {
210 0 : return IVAS_ERR_INTERNAL;
211 : }
212 : }
213 208368 : *dec_out = ind;
214 :
215 208368 : return IVAS_ERR_OK;
216 : }
217 :
218 :
219 : /*-----------------------------------------------------------------------------------------*
220 : * Function arith_decode_cell_array()
221 : *
222 : * Arithman decoding of cell array
223 : *-----------------------------------------------------------------------------------------*/
224 :
225 1307493 : static void arith_decode_cell_array(
226 : ivas_cell_dim_t *pCell_dims,
227 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
228 : int16_t num_bands,
229 : ivas_arith_t *pArith,
230 : int16_t *pSymbol )
231 : {
232 1307493 : int16_t total_symbol_len = 0;
233 : int16_t i;
234 :
235 12155301 : for ( i = 0; i < num_bands; i++ )
236 : {
237 10847808 : total_symbol_len += ( pCell_dims[i].dim1 * pCell_dims[i].dim2 );
238 : }
239 :
240 1307493 : assert( total_symbol_len <= IVAS_MAX_INPUT_LEN );
241 :
242 1307493 : ivas_arith_decode_array( pArith, st0, total_symbol_len, pSymbol );
243 :
244 1307493 : return;
245 : }
246 :
247 :
248 : /*-----------------------------------------------------------------------------------------*
249 : * Function arith_decode_cell_array_diff()
250 : *
251 : * Arithman decoding of differential cell array
252 : *-----------------------------------------------------------------------------------------*/
253 :
254 691056 : static void arith_decode_cell_array_diff(
255 : ivas_cell_dim_t *pCell_dims,
256 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
257 : int16_t num_bands,
258 : ivas_arith_t *pArith,
259 : ivas_arith_t *pArith_diff,
260 : int16_t *pSymbol,
261 : int16_t *pSymbol_old )
262 : {
263 691056 : int16_t total_symbol_len = 0;
264 : int16_t i;
265 :
266 6219504 : for ( i = 0; i < num_bands; i++ )
267 : {
268 5528448 : total_symbol_len += ( pCell_dims[i].dim1 * pCell_dims[i].dim2 );
269 : }
270 :
271 691056 : assert( total_symbol_len <= IVAS_MAX_INPUT_LEN );
272 :
273 691056 : ivas_arithCoder_decode_array_diff( pArith, pArith_diff, pSymbol_old, total_symbol_len, st0, pSymbol );
274 :
275 691056 : return;
276 : }
277 :
278 : /*-----------------------------------------------------------------------------------------*
279 : * Function ivas_arith_decode_cmplx_cell_array()
280 : *
281 : * Arithman decoding of complex cell array
282 : *-----------------------------------------------------------------------------------------*/
283 :
284 1307493 : void ivas_arith_decode_cmplx_cell_array(
285 : ivas_arith_t *pArith_re,
286 : ivas_arith_t *pArith_re_diff,
287 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
288 : ivas_cell_dim_t *pCell_dims,
289 : int16_t *pDo_diff,
290 : const int16_t num_bands,
291 : int16_t *pSymbol_re,
292 : int16_t *pSymbol_re_old )
293 : {
294 1307493 : int16_t i, j, len, all_diff = 1, any_diff = 0;
295 : int16_t cell_arr_diff[IVAS_MAX_INPUT_LEN];
296 : int16_t cell_arr_no_diff[IVAS_MAX_INPUT_LEN];
297 : int16_t cell_arr_diff_out[IVAS_MAX_INPUT_LEN];
298 1307493 : int16_t idx2 = 0;
299 :
300 12155301 : for ( i = 0; i < num_bands; i++ )
301 : {
302 10847808 : if ( pDo_diff[i] != 0 )
303 : {
304 4146336 : any_diff = 1;
305 : }
306 : else
307 : {
308 6701472 : all_diff = 0;
309 : }
310 : }
311 :
312 1307493 : if ( any_diff == 1 )
313 : {
314 691056 : if ( all_diff == 1 )
315 : {
316 0 : arith_decode_cell_array_diff( pCell_dims, st0, num_bands, pArith_re, pArith_re_diff, pSymbol_re, pSymbol_re_old );
317 : }
318 : else
319 : {
320 : ivas_cell_dim_t cell_dims[IVAS_MAX_NUM_BANDS];
321 : ivas_cell_dim_t cell_dims_diff[IVAS_MAX_NUM_BANDS];
322 691056 : int16_t idx1 = 0, idx = 0;
323 :
324 6219504 : for ( i = 0; i < num_bands; i++ )
325 : {
326 5528448 : len = pCell_dims[i].dim1 * pCell_dims[i].dim2;
327 5528448 : if ( pDo_diff[i] != 0 )
328 : {
329 4146336 : cell_dims[i].dim1 = 0;
330 4146336 : cell_dims[i].dim2 = 0;
331 :
332 13013316 : for ( j = 0; j < len; j++ )
333 : {
334 8866980 : cell_arr_diff[idx++] = pSymbol_re_old[idx1++];
335 : }
336 :
337 4146336 : cell_dims_diff[i].dim1 = pCell_dims[i].dim1;
338 4146336 : cell_dims_diff[i].dim2 = pCell_dims[i].dim2;
339 : }
340 : else
341 : {
342 4337772 : for ( j = 0; j < len; j++ )
343 : {
344 2955660 : cell_arr_diff[idx] = 0;
345 2955660 : idx1++;
346 : }
347 :
348 1382112 : cell_dims[i].dim1 = pCell_dims[i].dim1;
349 1382112 : cell_dims[i].dim2 = pCell_dims[i].dim2;
350 1382112 : cell_dims_diff[i].dim1 = 0;
351 1382112 : cell_dims_diff[i].dim2 = 0;
352 : }
353 : }
354 :
355 691056 : arith_decode_cell_array( cell_dims, st0, num_bands, pArith_re, cell_arr_no_diff );
356 :
357 691056 : arith_decode_cell_array_diff( cell_dims_diff, st0, num_bands, pArith_re, pArith_re_diff, cell_arr_diff_out, cell_arr_diff );
358 :
359 691056 : idx = 0;
360 691056 : idx1 = 0;
361 6219504 : for ( i = 0; i < num_bands; i++ )
362 : {
363 5528448 : if ( pDo_diff[i] != 0 )
364 : {
365 13013316 : for ( j = 0; j < cell_dims_diff[i].dim1 * cell_dims_diff[i].dim2; j++ )
366 : {
367 8866980 : pSymbol_re[idx++] = cell_arr_diff_out[idx2++];
368 : }
369 : }
370 : else
371 : {
372 4337772 : for ( j = 0; j < cell_dims[i].dim1 * cell_dims[i].dim2; j++ )
373 : {
374 2955660 : pSymbol_re[idx++] = cell_arr_no_diff[idx1++];
375 : }
376 : }
377 : }
378 : }
379 : }
380 : else
381 : {
382 616437 : arith_decode_cell_array( pCell_dims, st0, num_bands, pArith_re, pSymbol_re );
383 : }
384 :
385 1307493 : return;
386 : }
|