Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <assert.h>
6 : #include <stdio.h>
7 : #include <stdlib.h>
8 : #include <memory.h>
9 : #include "options.h"
10 : #include "stl.h"
11 : #include "prot_fx.h"
12 : #include "stat_dec.h"
13 : #include "basop_util.h"
14 :
15 : /**********************************************************************/ /**
16 : initialization of an instance of this module
17 : **************************************************************************/
18 19738 : void IGFSCFDecoderOpen(
19 : IGFSCFDEC_INSTANCE_HANDLE hPublicData, /* i : handle to public data */
20 : H_IGF_INFO hIgfInfo, /* i : IGF info handle */
21 : const Word32 total_brate,
22 : const Word16 bwidth,
23 : const Word16 element_mode,
24 : const Word16 rf_mode )
25 : {
26 :
27 :
28 19738 : hPublicData->scfCountLongBlock[0] = sub( hIgfInfo->grid[0].swb_offset_len, 1 );
29 19738 : hPublicData->scfCountLongBlock[1] = sub( hIgfInfo->grid[1].swb_offset_len, 1 );
30 19738 : hPublicData->scfCountLongBlock[2] = sub( hIgfInfo->grid[2].swb_offset_len, 1 );
31 19738 : move16();
32 19738 : move16();
33 19738 : move16();
34 :
35 19738 : hPublicData->t = 0;
36 19738 : move16(); /* protect against the invalid request of starting decoding with a dependent block */
37 :
38 19738 : IGFCommonFuncsIGFGetCFTables_fx( 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 );
39 19738 : }
40 :
41 :
42 1884912 : static Word16 quant_ctx_fx(
43 : Word16 ctx /* i: the context value to be quantized */
44 : )
45 : {
46 : /*
47 : ctx ... -5 -4 -3 -2 -1 0 1 2 3 4 5 ...
48 : Q(ctx)... -3 -3 -3 -2 -1 0 1 2 3 3 3 ...
49 : */
50 : Word16 result;
51 :
52 :
53 1884912 : result = s_min( abs_s( ctx ), IGF_CTX_OFFSET ); /* limit the absolute value to IGF_CTX_OFFSET */
54 1884912 : IF( ctx < 0 ) /* add the sign back, if needed */
55 : {
56 934171 : result = negate( result );
57 : }
58 :
59 1884912 : return result;
60 : }
61 :
62 565220 : static Word16 arith_decode_bits_fx(
63 : IGFSCFDEC_INSTANCE_HANDLE hPrivateData, /* i/o: instance handle */
64 : Decoder_State *st, /* i/o: pointer to bitstream decoder state */
65 : Word16 nBits /* i: number of bits to decode */
66 : )
67 : {
68 : Word16 i;
69 : Word16 x;
70 : Word16 bit;
71 :
72 :
73 565220 : x = 0;
74 565220 : move16();
75 1710022 : FOR( i = 0; i < nBits; ++i ) /* nBits > 0 */
76 : {
77 1144802 : x = lshl( x, 1 );
78 : /* decode one bit using the new raw AC function */
79 1144802 : bit = ari_decode_14bits_bit_ext_fx( st, &hPrivateData->acState );
80 1144802 : if ( bit != 0 )
81 : {
82 573899 : x = s_or( x, 1 );
83 : }
84 : }
85 :
86 565220 : return x;
87 : }
88 :
89 2416950 : static Word16 arith_decode_residual_fx(
90 : IGFSCFDEC_INSTANCE_HANDLE hPrivateData, /* i/o: instance handle */
91 : Decoder_State *st, /* i/o: pointer to decoder state */
92 : const UWord16 *cumulativeFrequencyTable, /* i: cumulative frequency table to be used Q0 */
93 : Word16 tableOffset /* i: offset used to align the table */
94 : )
95 : {
96 : Word16 val;
97 2416950 : Word16 x = 0; /* to avoid a compiler warning (potentially uninitialized local variable used) */
98 : Word16 extra;
99 2416950 : move16();
100 :
101 : /* decode one of the IGF_SYMBOLS_IN_TABLE == 27 alphabet symbols using the new raw AC function */
102 2416950 : val = ari_decode_14bits_s27_ext_fx( st, &hPrivateData->acState, cumulativeFrequencyTable ); // Q0
103 :
104 : /* meaning of the values of val: */
105 : /* esc_{0} IGF_MIN_ENC_SEPARATE ... IGF_MAX_ENC_SEPARATE esc_{IGF_SYMBOLS_IN_TABLE - 1} */
106 2416950 : test();
107 2416950 : IF( ( val != 0 ) && ( NE_16( val, IGF_SYMBOLS_IN_TABLE - 1 ) ) )
108 : {
109 2409923 : x = add( val, -1 + IGF_MIN_ENC_SEPARATE ); /* (val - 1) + IGF_MIN_ENC_SEPARATE */ // Q0
110 :
111 :
112 2409923 : x = sub( x, tableOffset ); // Q0
113 :
114 2409923 : return x;
115 : }
116 :
117 : /* decode one of the tails of the distribution */
118 : /* decode extra with 4 bits */
119 7027 : extra = arith_decode_bits_fx( hPrivateData, st, 4 ); // Q0
120 7027 : IF( EQ_16( extra, 15 ) ) /* escape code 15 to indicate extra >= 15 */
121 : {
122 : /* decode addtional extra with 6 bits */
123 77 : extra = arith_decode_bits_fx( hPrivateData, st, 6 ); // Q0
124 77 : IF( EQ_16( extra, 63 ) ) /* escape code 63 to indicate extra >= 63 */
125 : {
126 : /* decode safety extra with 7 bits */
127 0 : extra = arith_decode_bits_fx( hPrivateData, st, 7 ); // Q0
128 0 : extra = add( 63, extra );
129 : }
130 77 : extra = add( 15, extra ); // Q0
131 : }
132 :
133 7027 : IF( val == 0 )
134 : {
135 : /* escape code 0 to indicate x <= IGF_MIN_ENC_SEPARATE - 1 */
136 5344 : x = sub( IGF_MIN_ENC_SEPARATE - 1, extra ); // Q0
137 : }
138 7027 : IF( EQ_16( val, IGF_SYMBOLS_IN_TABLE - 1 ) )
139 : {
140 : /* escape code (IGF_SYMBOLS_IN_TABLE - 1) to indicate x >= IGF_MAX_ENC_SEPARATE + 1 */
141 1683 : x = add( IGF_MAX_ENC_SEPARATE + 1, extra ); // Q0
142 : }
143 :
144 7027 : x = sub( x, tableOffset ); // Q0
145 :
146 7027 : return x;
147 : }
148 :
149 569330 : static void arith_decode_flush_fx(
150 : Decoder_State *st /* i/o: pointer to decoder state */
151 : )
152 : {
153 :
154 569330 : get_next_indice_tmp_fx( st, -14 ); /* return back the least significant 14 bits to the bitstream */
155 569330 : }
156 :
157 569330 : static void decode_sfe_vector_fx(
158 : IGFSCFDEC_INSTANCE_HANDLE hPrivateData, /* i/o: instance handle */
159 : Decoder_State *st, /* i/o: pointer to decoder state */
160 : Word16 t, /* i: counter reset to 0 at each independent block */
161 : Word16 *prev_x, /* i: previous vector */
162 : Word16 *x, /* o: current vector to decode */
163 : Word16 length /* i: number of elements to decode */
164 : )
165 : {
166 : /*
167 : f
168 : ^
169 : | d a x
170 : | c b
171 : | e --> t
172 : */
173 : Word16 f;
174 : Word16 pred;
175 : Word16 res;
176 : Word16 ctx;
177 : Word16 ctx_f;
178 : Word16 ctx_t;
179 : Word16 prev_offset;
180 : Word32 index1;
181 : Word32 index2;
182 :
183 :
184 3544396 : FOR( f = 0; f < length; ++f )
185 : {
186 2975066 : IF( t == 0 )
187 : {
188 2926560 : IF( f == 0 )
189 : {
190 : /* (t == 0) && (f == 0) */
191 : /* decode one of the IGF_SYMBOLS_IN_TABLE == 27 alphabet symbols using the new raw AC function */
192 : /* NOTE: the float equivalent uses a tmp variable + explicit cast here to avoid undefined behaviour here. This BASOP version does not need it as it already uses signed Word16 return value here. */
193 558116 : res = ari_decode_14bits_s27_ext_fx( st, &hPrivateData->acState, (const UWord16 *) hPrivateData->cf_se00 );
194 :
195 558116 : pred = arith_decode_bits_fx( hPrivateData, st, 2 ); /* LSBs as 2 bit raw */
196 558116 : x[f] = add( shl( res, 2 ), pred ); // Q0
197 558116 : move16();
198 : }
199 2368444 : ELSE IF( EQ_16( f, 1 ) )
200 : {
201 : /* (t == 0) && (f == 1) */
202 558116 : res = arith_decode_residual_fx( hPrivateData,
203 : st,
204 : hPrivateData->cf_se01,
205 558116 : hPrivateData->cf_off_se01 );
206 558116 : x[f] = add( x[0], res ); // Q0
207 558116 : move16(); /* f - increment is 0, pred = b */
208 : }
209 : ELSE
210 : {
211 : /* (t == 0) && (f >= 2) */
212 1810328 : prev_offset = sub( f, 1 );
213 1810328 : ctx = quant_ctx_fx( sub( x[prev_offset], x[sub( prev_offset, 1 )] ) ); /* Q(b - e) */
214 : /* index1 is (IGF_SYMBOLS_IN_TABLE + 1) * (CTX_OFFSET + ctx) */
215 1810328 : index1 = L_mac0( ( IGF_SYMBOLS_IN_TABLE + 1 ) * IGF_CTX_OFFSET, ( IGF_SYMBOLS_IN_TABLE + 1 ), ctx );
216 : /* index2 is IGF_CTX_OFFSET + ctx */
217 1810328 : index2 = L_mac0( IGF_CTX_OFFSET, 1, ctx );
218 1810328 : res = arith_decode_residual_fx( hPrivateData,
219 : st,
220 1810328 : hPrivateData->cf_se02 + index1,
221 1810328 : hPrivateData->cf_off_se02[index2] );
222 1810328 : x[f] = add( x[prev_offset], res ); // Q0
223 1810328 : move16(); /* pred = b */
224 : }
225 : }
226 : ELSE
227 : {
228 : /* t == 1 */
229 48506 : IF( f == 0 )
230 : {
231 : /* (t == 1) && (f == 0) */
232 11214 : res = arith_decode_residual_fx( hPrivateData,
233 : st,
234 : hPrivateData->cf_se10,
235 11214 : hPrivateData->cf_off_se10 );
236 11214 : x[f] = add( prev_x[f], res ); // Q0
237 11214 : move16(); /* pred = a */
238 : }
239 : ELSE
240 : {
241 : /* (t == 1) && (f >= 1) */
242 37292 : prev_offset = sub( f, 1 );
243 37292 : pred = add( prev_x[f], x[prev_offset] );
244 37292 : pred = sub( pred, prev_x[prev_offset] ); /* pred = a + b - c */
245 37292 : ctx_f = quant_ctx_fx( sub( prev_x[f], prev_x[prev_offset] ) ); /* Q(a - c) */
246 37292 : ctx_t = quant_ctx_fx( sub( x[prev_offset], prev_x[prev_offset] ) ); /* Q(b - c) */
247 : /* index1 is (IGF_SYMBOLS_IN_TABLE + 1) * IGF_CTX_COUNT * (IGF_CTX_OFFSET + ctx_t)
248 : + (IGF_SYMBOLS_IN_TABLE + 1) * (IGF_CTX_OFFSET + ctx_f) */
249 37292 : index1 = L_mac0(
250 : ( ( IGF_SYMBOLS_IN_TABLE + 1 ) * IGF_CTX_COUNT + ( IGF_SYMBOLS_IN_TABLE + 1 ) ) * IGF_CTX_OFFSET,
251 : ( IGF_SYMBOLS_IN_TABLE + 1 ) * IGF_CTX_COUNT, ctx_t );
252 37292 : index1 = L_mac0( index1, ( IGF_SYMBOLS_IN_TABLE + 1 ), ctx_f );
253 : /* index2 is IGF_CTX_COUNT * (IGF_CTX_OFFSET + ctx_t) + (IGF_CTX_OFFSET + ctx_f) */
254 37292 : index2 = L_mac0( ( IGF_CTX_COUNT + 1 ) * IGF_CTX_OFFSET, IGF_CTX_COUNT, ctx_t );
255 37292 : index2 = L_mac0( index2, 1, ctx_f );
256 37292 : res = arith_decode_residual_fx( hPrivateData,
257 : st,
258 37292 : hPrivateData->cf_se11 + index1,
259 37292 : hPrivateData->cf_off_se11[index2] );
260 37292 : x[f] = add( pred, res ); // Q0
261 37292 : move16();
262 : }
263 : }
264 :
265 2975066 : IF( x[f] < 0 )
266 : {
267 0 : x[f] = 0;
268 0 : move16();
269 0 : st->BER_detect = 1;
270 0 : move16();
271 : }
272 :
273 2975066 : IF( GT_16( x[f], 91 ) )
274 : {
275 0 : x[f] = 91;
276 0 : move16();
277 0 : st->BER_detect = 1;
278 0 : move16();
279 : }
280 : }
281 569330 : }
282 :
283 : /**********************************************************************/ /**
284 : resets the internal decoder memory (context memory)
285 : **************************************************************************/
286 558517 : void IGFSCFDecoderReset(
287 : IGFSCFDEC_INSTANCE_HANDLE hPublicData /* i/o: handle to public data */
288 : )
289 : {
290 :
291 :
292 : /* reset of coder */
293 558517 : hPublicData->t = 0; /* indicate that an independent block follows */
294 558517 : move16();
295 : /* we do not need to fill hPublicData->prev with zeros, because when t = 0 no previous information is used */
296 558517 : }
297 :
298 : /**********************************************************************/ /**
299 : main decoder function
300 : **************************************************************************/
301 569330 : 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 : Word16 *sfe, /* o : ptr to an array which will contain the decoded quantized coefficients */
305 : const Word16 igfGridIdx, /* i : igf grid index see declaration of IGF_GRID_IDX for details */
306 : const Word16 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 569330 : hPublicData->bitsRead = st->next_bit_pos;
312 569330 : move16();
313 569330 : ari_start_decoding_14bits_fx( st, &hPublicData->acState ); /* start AC decoding */
314 :
315 : /* check if coder needs a reset and do it if necessary */
316 569330 : IF( indepFlag != 0 )
317 : {
318 : /* reset of coder */
319 558116 : IGFSCFDecoderReset( hPublicData );
320 : }
321 :
322 569330 : decode_sfe_vector_fx( hPublicData, st, hPublicData->t, hPublicData->prev, sfe, hPublicData->scfCountLongBlock[igfGridIdx] );
323 :
324 569330 : arith_decode_flush_fx( st ); /* finish AC decoding */
325 :
326 :
327 : /* advance history */
328 569330 : Copy( sfe, hPublicData->prev, hPublicData->scfCountLongBlock[igfGridIdx] );
329 569330 : hPublicData->t = add( hPublicData->t, 1 );
330 569330 : move16();
331 :
332 569330 : hPublicData->bitsRead = sub( st->next_bit_pos, hPublicData->bitsRead );
333 569330 : move16();
334 569330 : }
|