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 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #include "prot.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include "stat_dec.h"
44 : #include "wmc_auto.h"
45 :
46 :
47 : /*---------------------------------------------------------------------*
48 : * IGFSCFDecoderOpen()
49 : *
50 : * initialization of an instance of this module, pass a ptr to a hPublicData
51 : *---------------------------------------------------------------------*/
52 :
53 63417 : void IGFSCFDecoderOpen(
54 : IGFSCFDEC_INSTANCE_HANDLE hPublicData, /* i/o: handle to public data */
55 : H_IGF_INFO hIgfInfo, /* i : IGF info handle */
56 : const int32_t total_brate,
57 : const int16_t bwidth,
58 : const int16_t element_mode,
59 : const int16_t rf_mode /* i : flag to signal the RF mode */
60 : )
61 : {
62 :
63 63417 : hPublicData->scfCountLongBlock[0] = hIgfInfo->grid[0].swb_offset_len - 1;
64 63417 : hPublicData->scfCountLongBlock[1] = hIgfInfo->grid[1].swb_offset_len - 1;
65 63417 : hPublicData->scfCountLongBlock[2] = hIgfInfo->grid[2].swb_offset_len - 1;
66 63417 : hPublicData->t = 0; /* protect against the invalid request of starting decoding with a dependent block */
67 :
68 63417 : IGFCommonFuncsIGFGetCFTables( total_brate, bwidth, element_mode, rf_mode, &hPublicData->cf_se00, &hPublicData->cf_se01, &hPublicData->cf_off_se01, &hPublicData->cf_se02, &hPublicData->cf_off_se02, &hPublicData->cf_se10, &hPublicData->cf_off_se10, &hPublicData->cf_se11, &hPublicData->cf_off_se11 );
69 :
70 63417 : return;
71 : }
72 :
73 : /*---------------------------------------------------------------------*
74 : * quant_ctx()
75 : *
76 : *
77 : *---------------------------------------------------------------------*/
78 :
79 5869173 : static int16_t quant_ctx(
80 : const int16_t ctx )
81 : {
82 : /*
83 : ctx ... -5 -4 -3 -2 -1 0 1 2 3 4 5 ...
84 : Q(ctx)... -3 -3 -3 -2 -1 0 1 2 3 3 3 ...
85 : */
86 5869173 : if ( abs( ctx ) <= 3 )
87 : {
88 4989435 : return ctx;
89 : }
90 879738 : else if ( ctx > 3 )
91 : {
92 239565 : return 3;
93 : }
94 : else
95 : {
96 : /* ctx < -3 */
97 640173 : return -3;
98 : }
99 : }
100 :
101 : /*---------------------------------------------------------------------*
102 : * arith_decode_bits()
103 : *
104 : *
105 : *---------------------------------------------------------------------*/
106 :
107 1785381 : static int16_t arith_decode_bits(
108 : Tastat *ac_state, /* i/o: arith coding state */
109 : Decoder_State *st, /* i : pointer to decoder state */
110 : const int16_t nBits /* i : number of bits to decode */
111 : )
112 : {
113 : int16_t i, x;
114 : uint16_t bit;
115 :
116 1785381 : x = 0;
117 5400411 : for ( i = 0; i < nBits; ++i )
118 : {
119 : /* decode one bit using the new raw AC function */
120 3615030 : ari_decode_14bits_bit_ext( st, &bit, ac_state );
121 3615030 : x = ( x << 1 ) | bit;
122 : }
123 :
124 1785381 : return x;
125 : }
126 :
127 :
128 : /*---------------------------------------------------------------------*
129 : * arith_decode_residual()
130 : *
131 : *
132 : *---------------------------------------------------------------------*/
133 :
134 7552608 : static int16_t arith_decode_residual(
135 : Tastat *ac_state, /* i/o: arith coding state */
136 : Decoder_State *st, /* i : pointer to decoder state */
137 : const uint16_t *cumulativeFrequencyTable, /* i : cumulative frequency table to be used */
138 : const int16_t tableOffset /* i : offset used to align the table */
139 : )
140 : {
141 : uint16_t val;
142 : int16_t x, extra, extra_tmp;
143 :
144 : /* decode one of the IGF_SYMBOLS_IN_TABLE == 27 alphabet symbols using the new raw AC function */
145 7552608 : ari_decode_14bits_s27_ext( st, &val, ac_state, cumulativeFrequencyTable );
146 :
147 : /* meaning of the values of val: */
148 : /* esc_{0} IGF_MIN_ENC_SEPARATE ... IGF_MAX_ENC_SEPARATE esc_{IGF_SYMBOLS_IN_TABLE - 1} */
149 7552608 : if ( ( val != 0 ) && ( val != IGF_SYMBOLS_IN_TABLE - 1 ) )
150 : {
151 7530942 : x = ( val - 1 ) + IGF_MIN_ENC_SEPARATE;
152 :
153 :
154 7530942 : x -= tableOffset;
155 7530942 : return x;
156 : }
157 :
158 : /* decode one of the tails of the distribution */
159 : /* decode extra with 4 bits */
160 21666 : extra = arith_decode_bits( ac_state, st, 4 );
161 21666 : if ( extra == 15 )
162 : {
163 : /* escape code 15 to indicate extra >= 15 */
164 : /* decode addtional extra with 6 bits */
165 234 : extra_tmp = arith_decode_bits( ac_state, st, 6 );
166 234 : if ( extra_tmp == 63 )
167 : {
168 : /* escape code 63 to indicate extra_tmp >= 63 */
169 : /* decode safety extra with 7 bits */
170 0 : extra_tmp = 63 + arith_decode_bits( ac_state, st, 7 );
171 : }
172 234 : extra = 15 + extra_tmp;
173 : }
174 :
175 21666 : if ( val == 0 )
176 : {
177 : /* escape code 0 to indicate x <= IGF_MIN_ENC_SEPARATE - 1 */
178 16305 : x = ( IGF_MIN_ENC_SEPARATE - 1 ) - extra;
179 : }
180 : else
181 : {
182 : /* escape code (IGF_SYMBOLS_IN_TABLE - 1) to indicate x >= IGF_MAX_ENC_SEPARATE + 1 */
183 5361 : x = ( IGF_MAX_ENC_SEPARATE + 1 ) + extra;
184 : }
185 :
186 21666 : x -= tableOffset;
187 :
188 21666 : return x;
189 : }
190 :
191 :
192 : /*---------------------------------------------------------------------*
193 : * decode_sfe_vector()
194 : *
195 : *
196 : *---------------------------------------------------------------------*/
197 :
198 1798212 : static void decode_sfe_vector(
199 : IGFSCFDEC_INSTANCE_HANDLE hPrivateData, /* i/o: instance handle */
200 : Decoder_State *st, /* i : pointer to decoder state */
201 : const int16_t t, /* i : frame counter reset to 0 at each independent frame */
202 : const int16_t *prev_x, /* i : previous vector */
203 : int16_t *x, /* i/o: current vector to decode */
204 : const int16_t length /* i : number of elements to decode */
205 : )
206 : {
207 : /*
208 : f
209 : ^
210 : | d a x
211 : | c b
212 : | e --> t
213 : */
214 : int16_t pred;
215 : uint16_t pred_tmp;
216 : int16_t f, ctx, ctx_f, ctx_t;
217 :
218 11114301 : for ( f = 0; f < length; f++ )
219 : {
220 9316089 : if ( t == 0 )
221 : {
222 9166581 : if ( f == 0 )
223 : {
224 : /* decode one of the IGF_SYMBOLS_IN_TABLE == 27 alphabet symbols using the new raw AC function */
225 1763481 : ari_decode_14bits_s27_ext( st, &pred_tmp, &hPrivateData->acState, hPrivateData->cf_se00 );
226 1763481 : pred = (int16_t) pred_tmp;
227 1763481 : x[f] = pred << 2;
228 1763481 : x[f] += arith_decode_bits( &hPrivateData->acState, st, 2 ); /* LSBs as 2 bit raw */
229 : }
230 7403100 : else if ( f == 1 )
231 : {
232 1763481 : pred = x[f - 1]; /* pred = b */
233 1763481 : x[f] = pred + arith_decode_residual( &hPrivateData->acState, st, hPrivateData->cf_se01, hPrivateData->cf_off_se01 );
234 : }
235 : else
236 : {
237 : /* f >= 2 */
238 5639619 : pred = x[f - 1]; /* pred = b */
239 5639619 : ctx = quant_ctx( x[f - 1] - x[f - 2] ); /* Q(b - e) */
240 5639619 : x[f] = pred + arith_decode_residual( &hPrivateData->acState, st, &hPrivateData->cf_se02[( IGF_SYMBOLS_IN_TABLE + 1 ) * ( IGF_CTX_OFFSET + ctx )], hPrivateData->cf_off_se02[IGF_CTX_OFFSET + ctx] );
241 : }
242 : }
243 : else
244 : {
245 : /* t == 1 */
246 149508 : if ( f == 0 )
247 : {
248 34731 : pred = prev_x[f]; /* pred = a */
249 34731 : x[f] = pred + arith_decode_residual( &hPrivateData->acState, st, hPrivateData->cf_se10, hPrivateData->cf_off_se10 );
250 : }
251 : else
252 : {
253 : /* (t == 1) && (f >= 1) */
254 114777 : pred = prev_x[f] + x[f - 1] - prev_x[f - 1]; /* pred = a + b - c */
255 114777 : ctx_f = quant_ctx( prev_x[f] - prev_x[f - 1] ); /* Q(a - c) */
256 114777 : ctx_t = quant_ctx( x[f - 1] - prev_x[f - 1] ); /* Q(b - c) */
257 114777 : x[f] = pred + arith_decode_residual( &hPrivateData->acState, st, &hPrivateData->cf_se11[( IGF_SYMBOLS_IN_TABLE + 1 ) * IGF_CTX_COUNT * ( IGF_CTX_OFFSET + ctx_t ) + ( IGF_SYMBOLS_IN_TABLE + 1 ) * ( IGF_CTX_OFFSET + ctx_f )], hPrivateData->cf_off_se11[IGF_CTX_COUNT * ( IGF_CTX_OFFSET + ctx_t ) + ( IGF_CTX_OFFSET + ctx_f )] );
258 : }
259 : }
260 9316089 : if ( x[f] < 0 )
261 : {
262 0 : x[f] = 0;
263 0 : st->BER_detect = 1;
264 : }
265 9316089 : if ( x[f] > 91 )
266 : {
267 0 : x[f] = 91;
268 0 : st->BER_detect = 1;
269 : }
270 : }
271 :
272 1798212 : return;
273 : }
274 :
275 :
276 : /*---------------------------------------------------------------------*
277 : * IGFSCFDecoderReset()
278 : *
279 : * resets the internal decoder memory (context memory)
280 : *---------------------------------------------------------------------*/
281 :
282 1764681 : void IGFSCFDecoderReset(
283 : IGFSCFDEC_INSTANCE_HANDLE hPublicData /* i/o: handle to public data or NULL in case there was no instance created */
284 : )
285 : {
286 : /* reset of coder */
287 1764681 : hPublicData->t = 0;
288 :
289 : /* we do not need to fill hPublicData->prev with zeros, because when t = 0 no previous information is used */
290 :
291 1764681 : return;
292 : }
293 :
294 :
295 : /*---------------------------------------------------------------------*
296 : * IGFSCFDecoderDecode()
297 : *
298 : * main IGF decoder function
299 : *---------------------------------------------------------------------*/
300 :
301 1798212 : void IGFSCFDecoderDecode(
302 : IGFSCFDEC_INSTANCE_HANDLE hPublicData, /* i/o: handle to public data or NULL in case there was no instance created */
303 : Decoder_State *st, /* i/o: pointer to decoder state */
304 : int16_t *sfe, /* o : ptr to an array which will contain the decoded quantized coefficients */
305 : const int16_t igfGridIdx, /* i : igf grid index see declaration of IGF_GRID_IDX for details */
306 : const int16_t indepFlag /* i : if 1 on input the decoder will be forced to reset,
307 : if 0 on input the decoder will be forced to encode without a reset */
308 : )
309 : {
310 : /* insert data */
311 1798212 : hPublicData->bitsRead = st->next_bit_pos;
312 1798212 : ari_start_decoding_14bits( st, &hPublicData->acState ); /* start AC decoding */
313 :
314 : /* check if coder needs a reset and do it if neccessary */
315 1798212 : if ( indepFlag )
316 : {
317 : /* reset of coder */
318 1763481 : IGFSCFDecoderReset( hPublicData );
319 : }
320 :
321 1798212 : decode_sfe_vector( hPublicData, st, hPublicData->t, hPublicData->prev, sfe, hPublicData->scfCountLongBlock[igfGridIdx] );
322 :
323 1798212 : get_next_indice_tmp( st, -14 ); /* finish decoding (arith_decode_flush) return back the least significant 14 bits to the bitstream */
324 :
325 : /* advance history */
326 1798212 : mvs2s( sfe, hPublicData->prev, hPublicData->scfCountLongBlock[igfGridIdx] );
327 1798212 : hPublicData->t++;
328 :
329 1798212 : hPublicData->bitsRead = st->next_bit_pos - hPublicData->bitsRead;
330 :
331 1798212 : return;
332 : }
|