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 18141 : 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 18141 : hPublicData->scfCountLongBlock[0] = sub( hIgfInfo->grid[0].swb_offset_len, 1 );
29 18141 : hPublicData->scfCountLongBlock[1] = sub( hIgfInfo->grid[1].swb_offset_len, 1 );
30 18141 : hPublicData->scfCountLongBlock[2] = sub( hIgfInfo->grid[2].swb_offset_len, 1 );
31 18141 : move16();
32 18141 : move16();
33 18141 : move16();
34 :
35 18141 : hPublicData->t = 0;
36 18141 : move16(); /* protect against the invalid request of starting decoding with a dependent block */
37 :
38 18141 : 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 18141 : }
40 :
41 :
42 1767203 : 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 1767203 : result = s_min( abs_s( ctx ), IGF_CTX_OFFSET ); /* limit the absolute value to IGF_CTX_OFFSET */
54 1767203 : IF( ctx < 0 ) /* add the sign back, if needed */
55 : {
56 878822 : result = negate( result );
57 : }
58 :
59 1767203 : return result;
60 : }
61 :
62 532035 : 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 532035 : x = 0;
74 532035 : move16();
75 1608857 : FOR( i = 0; i < nBits; ++i ) /* nBits > 0 */
76 : {
77 1076822 : x = lshl( x, 1 );
78 : /* decode one bit using the new raw AC function */
79 1076822 : bit = ari_decode_14bits_bit_ext_fx( st, &hPrivateData->acState );
80 1076822 : if ( bit != 0 )
81 : {
82 540424 : x = s_or( x, 1 );
83 : }
84 : }
85 :
86 532035 : return x;
87 : }
88 :
89 2269597 : 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 2269597 : Word16 x = 0; /* to avoid a compiler warning (potentially uninitialized local variable used) */
98 : Word16 extra;
99 2269597 : move16();
100 :
101 : /* decode one of the IGF_SYMBOLS_IN_TABLE == 27 alphabet symbols using the new raw AC function */
102 2269597 : 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 2269597 : test();
107 2269597 : IF( ( val != 0 ) && ( NE_16( val, IGF_SYMBOLS_IN_TABLE - 1 ) ) )
108 : {
109 2263365 : x = add( val, -1 + IGF_MIN_ENC_SEPARATE ); /* (val - 1) + IGF_MIN_ENC_SEPARATE */ // Q0
110 :
111 :
112 2263365 : x = sub( x, tableOffset ); // Q0
113 :
114 2263365 : return x;
115 : }
116 :
117 : /* decode one of the tails of the distribution */
118 : /* decode extra with 4 bits */
119 6232 : extra = arith_decode_bits_fx( hPrivateData, st, 4 ); // Q0
120 6232 : IF( EQ_16( extra, 15 ) ) /* escape code 15 to indicate extra >= 15 */
121 : {
122 : /* decode addtional extra with 6 bits */
123 72 : extra = arith_decode_bits_fx( hPrivateData, st, 6 ); // Q0
124 72 : 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 72 : extra = add( 15, extra ); // Q0
131 : }
132 :
133 6232 : IF( val == 0 )
134 : {
135 : /* escape code 0 to indicate x <= IGF_MIN_ENC_SEPARATE - 1 */
136 4700 : x = sub( IGF_MIN_ENC_SEPARATE - 1, extra ); // Q0
137 : }
138 6232 : 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 1532 : x = add( IGF_MAX_ENC_SEPARATE + 1, extra ); // Q0
142 : }
143 :
144 6232 : x = sub( x, tableOffset ); // Q0
145 :
146 6232 : return x;
147 : }
148 :
149 535863 : static void arith_decode_flush_fx(
150 : Decoder_State *st /* i/o: pointer to decoder state */
151 : )
152 : {
153 :
154 535863 : get_next_indice_tmp_fx( st, -14 ); /* return back the least significant 14 bits to the bitstream */
155 535863 : }
156 :
157 535863 : 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 3331191 : FOR( f = 0; f < length; ++f )
185 : {
186 2795328 : IF( t == 0 )
187 : {
188 2751727 : 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 525731 : res = ari_decode_14bits_s27_ext_fx( st, &hPrivateData->acState, (const UWord16 *) hPrivateData->cf_se00 );
193 :
194 525731 : pred = arith_decode_bits_fx( hPrivateData, st, 2 ); /* LSBs as 2 bit raw */
195 525731 : x[f] = add( shl( res, 2 ), pred ); // Q0
196 525731 : move16();
197 : }
198 2225996 : ELSE IF( EQ_16( f, 1 ) )
199 : {
200 : /* (t == 0) && (f == 1) */
201 525731 : res = arith_decode_residual_fx( hPrivateData,
202 : st,
203 : hPrivateData->cf_se01,
204 525731 : hPrivateData->cf_off_se01 );
205 525731 : x[f] = add( x[0], res ); // Q0
206 525731 : move16(); /* f - increment is 0, pred = b */
207 : }
208 : ELSE
209 : {
210 : /* (t == 0) && (f >= 2) */
211 1700265 : prev_offset = sub( f, 1 );
212 1700265 : ctx = quant_ctx_fx( sub( x[prev_offset], x[sub( prev_offset, 1 )] ) ); /* Q(b - e) */
213 : /* index1 is (IGF_SYMBOLS_IN_TABLE + 1) * (CTX_OFFSET + ctx) */
214 1700265 : index1 = L_mac0( ( IGF_SYMBOLS_IN_TABLE + 1 ) * IGF_CTX_OFFSET, ( IGF_SYMBOLS_IN_TABLE + 1 ), ctx );
215 : /* index2 is IGF_CTX_OFFSET + ctx */
216 1700265 : index2 = L_mac0( IGF_CTX_OFFSET, 1, ctx );
217 1700265 : res = arith_decode_residual_fx( hPrivateData,
218 : st,
219 1700265 : hPrivateData->cf_se02 + index1,
220 1700265 : hPrivateData->cf_off_se02[index2] );
221 1700265 : x[f] = add( x[prev_offset], res ); // Q0
222 1700265 : move16(); /* pred = b */
223 : }
224 : }
225 : ELSE
226 : {
227 : /* t == 1 */
228 43601 : IF( f == 0 )
229 : {
230 : /* (t == 1) && (f == 0) */
231 10132 : res = arith_decode_residual_fx( hPrivateData,
232 : st,
233 : hPrivateData->cf_se10,
234 10132 : hPrivateData->cf_off_se10 );
235 10132 : x[f] = add( prev_x[f], res ); // Q0
236 10132 : move16(); /* pred = a */
237 : }
238 : ELSE
239 : {
240 : /* (t == 1) && (f >= 1) */
241 33469 : prev_offset = sub( f, 1 );
242 33469 : pred = add( prev_x[f], x[prev_offset] );
243 33469 : pred = sub( pred, prev_x[prev_offset] ); /* pred = a + b - c */
244 33469 : ctx_f = quant_ctx_fx( sub( prev_x[f], prev_x[prev_offset] ) ); /* Q(a - c) */
245 33469 : ctx_t = quant_ctx_fx( sub( x[prev_offset], prev_x[prev_offset] ) ); /* Q(b - c) */
246 : /* index1 is (IGF_SYMBOLS_IN_TABLE + 1) * IGF_CTX_COUNT * (IGF_CTX_OFFSET + ctx_t)
247 : + (IGF_SYMBOLS_IN_TABLE + 1) * (IGF_CTX_OFFSET + ctx_f) */
248 33469 : index1 = L_mac0(
249 : ( ( IGF_SYMBOLS_IN_TABLE + 1 ) * IGF_CTX_COUNT + ( IGF_SYMBOLS_IN_TABLE + 1 ) ) * IGF_CTX_OFFSET,
250 : ( IGF_SYMBOLS_IN_TABLE + 1 ) * IGF_CTX_COUNT, ctx_t );
251 33469 : index1 = L_mac0( index1, ( IGF_SYMBOLS_IN_TABLE + 1 ), ctx_f );
252 : /* index2 is IGF_CTX_COUNT * (IGF_CTX_OFFSET + ctx_t) + (IGF_CTX_OFFSET + ctx_f) */
253 33469 : index2 = L_mac0( ( IGF_CTX_COUNT + 1 ) * IGF_CTX_OFFSET, IGF_CTX_COUNT, ctx_t );
254 33469 : index2 = L_mac0( index2, 1, ctx_f );
255 33469 : res = arith_decode_residual_fx( hPrivateData,
256 : st,
257 33469 : hPrivateData->cf_se11 + index1,
258 33469 : hPrivateData->cf_off_se11[index2] );
259 33469 : x[f] = add( pred, res ); // Q0
260 33469 : move16();
261 : }
262 : }
263 :
264 2795328 : IF( x[f] < 0 )
265 : {
266 0 : x[f] = 0;
267 0 : move16();
268 0 : st->BER_detect = 1;
269 0 : move16();
270 : }
271 :
272 2795328 : IF( GT_16( x[f], 91 ) )
273 : {
274 0 : x[f] = 91;
275 0 : move16();
276 0 : st->BER_detect = 1;
277 0 : move16();
278 : }
279 : }
280 535863 : }
281 :
282 : /**********************************************************************/ /**
283 : resets the internal decoder memory (context memory)
284 : **************************************************************************/
285 526128 : void IGFSCFDecoderReset(
286 : IGFSCFDEC_INSTANCE_HANDLE hPublicData /* i/o: handle to public data */
287 : )
288 : {
289 :
290 :
291 : /* reset of coder */
292 526128 : hPublicData->t = 0; /* indicate that an independent block follows */
293 526128 : move16();
294 : /* we do not need to fill hPublicData->prev with zeros, because when t = 0 no previous information is used */
295 526128 : }
296 :
297 : /**********************************************************************/ /**
298 : main decoder function
299 : **************************************************************************/
300 535863 : void IGFSCFDecoderDecode(
301 : IGFSCFDEC_INSTANCE_HANDLE hPublicData, /* i/o: handle to public data or NULL in case there was no instance created */
302 : Decoder_State *st, /* i/o: pointer to decoder state */
303 : Word16 *sfe, /* o : ptr to an array which will contain the decoded quantized coefficients */
304 : const Word16 igfGridIdx, /* i : igf grid index see declaration of IGF_GRID_IDX for details */
305 : const Word16 indepFlag /* i : if 1 on input the decoder will be forced to reset,
306 : if 0 on input the decoder will be forced to encode without a reset */
307 : )
308 : {
309 : /* insert data */
310 535863 : hPublicData->bitsRead = st->next_bit_pos;
311 535863 : move16();
312 535863 : ari_start_decoding_14bits_fx( st, &hPublicData->acState ); /* start AC decoding */
313 :
314 : /* check if coder needs a reset and do it if necessary */
315 535863 : IF( indepFlag != 0 )
316 : {
317 : /* reset of coder */
318 525731 : IGFSCFDecoderReset( hPublicData );
319 : }
320 :
321 535863 : decode_sfe_vector_fx( hPublicData, st, hPublicData->t, hPublicData->prev, sfe, hPublicData->scfCountLongBlock[igfGridIdx] );
322 :
323 535863 : arith_decode_flush_fx( st ); /* finish AC decoding */
324 :
325 :
326 : /* advance history */
327 535863 : Copy( sfe, hPublicData->prev, hPublicData->scfCountLongBlock[igfGridIdx] );
328 535863 : hPublicData->t = add( hPublicData->t, 1 );
329 535863 : move16();
330 :
331 535863 : hPublicData->bitsRead = sub( st->next_bit_pos, hPublicData->bitsRead );
332 535863 : move16();
333 535863 : }
|