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_enc.h"
44 : #include "stat_com.h"
45 : #include "cnst.h"
46 : #include "wmc_auto.h"
47 :
48 :
49 : /*---------------------------------------------------------------------*
50 : * IGFSCFEncoderOpen()
51 : *
52 : * initialization of an instance of this module, pass a ptr to a hPublicData
53 : *---------------------------------------------------------------------*/
54 :
55 529056 : void IGFSCFEncoderOpen(
56 : IGFSCFENC_INSTANCE_HANDLE hPublicData, /* i/o: handle to public data */
57 : H_IGF_INFO hIgfInfo, /* i : IGF info handle */
58 : const int32_t total_brate, /* i : total bitrate */
59 : const int16_t bwidth, /* i : audio bandwidth */
60 : const int16_t element_mode, /* i : IVAS element mode */
61 : const int16_t rf_mode /* i : flag to signal the RF mode */
62 : )
63 : {
64 :
65 529056 : hPublicData->ptrBitIndex = 0;
66 529056 : hPublicData->bitCount = 0;
67 529056 : hPublicData->Tsave = 0;
68 529056 : hPublicData->contex_saved = 0;
69 529056 : hPublicData->acState.low = 0;
70 529056 : hPublicData->acState.high = 0;
71 529056 : hPublicData->acState.bits_to_follow = 0;
72 529056 : set_s( hPublicData->prev, 0, 64 );
73 529056 : set_s( hPublicData->prevSave, 0, 64 );
74 :
75 529056 : hPublicData->scfCountLongBlock[0] = hIgfInfo->grid[0].swb_offset_len - 1;
76 529056 : hPublicData->scfCountLongBlock[1] = hIgfInfo->grid[1].swb_offset_len - 1;
77 529056 : hPublicData->scfCountLongBlock[2] = hIgfInfo->grid[2].swb_offset_len - 1;
78 529056 : hPublicData->t = 0; /* protect against the invalid request of starting encoding with a dependent block */
79 :
80 529056 : 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 );
81 :
82 529056 : return;
83 : }
84 :
85 :
86 : /*---------------------------------------------------------------------*
87 : * quant_ctx()
88 : *
89 : *
90 : *---------------------------------------------------------------------*/
91 :
92 61564384 : static int16_t quant_ctx(
93 : const int16_t ctx )
94 : {
95 : /*
96 : ctx ... -5 -4 -3 -2 -1 0 1 2 3 4 5 ...
97 : Q(ctx)... -3 -3 -3 -2 -1 0 1 2 3 3 3 ...
98 : */
99 61564384 : if ( abs( ctx ) <= 3 )
100 : {
101 49681635 : return ctx;
102 : }
103 11882749 : else if ( ctx > 3 )
104 : {
105 5285519 : return 3;
106 : }
107 : else /* ctx < -3 */
108 : {
109 6597230 : return -3;
110 : }
111 : }
112 :
113 :
114 : /*---------------------------------------------------------------------*
115 : * arith_encode_bits()
116 : *
117 : *
118 : *---------------------------------------------------------------------*/
119 :
120 16818277 : static void arith_encode_bits(
121 : IGFSCFENC_INSTANCE_HANDLE hPrivateData, /* i/o: instance handle */
122 : int16_t *ptr, /* i : pointer to expanded bit buffer, one bit in each short*/
123 : const int16_t x, /* i : value to encode */
124 : const int16_t nBits /* i : number of bits to encode */
125 : )
126 : {
127 : int16_t i;
128 : int16_t bit;
129 :
130 50939355 : for ( i = nBits - 1; i >= 0; --i )
131 : {
132 34121078 : bit = ( x >> i ) & 1;
133 34121078 : hPrivateData->ptrBitIndex = ari_encode_14bits_sign( ptr, hPrivateData->ptrBitIndex, MAX16B, /* disable the bit count limitation */ &hPrivateData->acState, bit );
134 : }
135 :
136 16818277 : return;
137 : }
138 :
139 :
140 : /*---------------------------------------------------------------------*
141 : * arith_encode_residual()
142 : *
143 : *
144 : *---------------------------------------------------------------------*/
145 :
146 77690604 : static void arith_encode_residual(
147 : IGFSCFENC_INSTANCE_HANDLE hPrivateData, /* i/o: instance handle */
148 : int16_t *ptr, /* i : pointer to expanded bit buffer, one bit in each short */
149 : int16_t x, /* i : prediction residual to encode */
150 : const uint16_t *cumulativeFrequencyTable, /* i : cumulative frequency table to be used */
151 : const int16_t tableOffset /* i : offset used to align the table */
152 : )
153 : {
154 : int16_t extra;
155 :
156 77690604 : x += tableOffset;
157 77690604 : if ( ( x >= IGF_MIN_ENC_SEPARATE ) && ( x <= IGF_MAX_ENC_SEPARATE ) )
158 : {
159 : /* encode one of the IGF_SYMBOLS_IN_TABLE == 27 alphabet symbols using the new raw AC function */
160 77469084 : hPrivateData->ptrBitIndex = ari_encode_14bits_ext( ptr, hPrivateData->ptrBitIndex, &hPrivateData->acState, ( x - IGF_MIN_ENC_SEPARATE ) + 1, cumulativeFrequencyTable );
161 :
162 77469084 : return;
163 : }
164 221520 : else if ( x < IGF_MIN_ENC_SEPARATE )
165 : {
166 : /* send escape code 0 to indicate x <= IGF_MIN_ENC_SEPARATE - 1 */
167 96906 : extra = ( IGF_MIN_ENC_SEPARATE - 1 ) - x;
168 96906 : hPrivateData->ptrBitIndex = ari_encode_14bits_ext( ptr, hPrivateData->ptrBitIndex, &hPrivateData->acState, 0, cumulativeFrequencyTable );
169 : }
170 : else
171 : {
172 : /* x > IGF_MAX_ENC_SEPARATE */
173 : /* send escape code (IGF_SYMBOLS_IN_TABLE - 1) to indicate x >= IGF_MAX_ENC_SEPARATE + 1 */
174 124614 : extra = x - ( IGF_MAX_ENC_SEPARATE + 1 );
175 124614 : hPrivateData->ptrBitIndex = ari_encode_14bits_ext( ptr, hPrivateData->ptrBitIndex, &hPrivateData->acState, IGF_SYMBOLS_IN_TABLE - 1, cumulativeFrequencyTable );
176 : }
177 :
178 : /* encode one of the tails of the distribution */
179 221520 : if ( extra < 15 )
180 : {
181 : /* encode extra with 4 bits if extra < 15 */
182 211149 : arith_encode_bits( hPrivateData, ptr, extra, 4 );
183 : }
184 : else
185 : {
186 : /* extra >= 15 */
187 : /* send escape code 15 to indicate extra >= 15 */
188 10371 : arith_encode_bits( hPrivateData, ptr, 15, 4 );
189 10371 : extra -= 15;
190 :
191 10371 : if ( extra < 63 )
192 : {
193 : /* encode additional extra with 6 bits */
194 10371 : arith_encode_bits( hPrivateData, ptr, extra, 6 );
195 : }
196 : else /* extra >= 63 */
197 : {
198 0 : arith_encode_bits( hPrivateData, ptr, 63, 6 );
199 0 : extra -= 63;
200 : /* encode safety extra with 7 bits */
201 0 : arith_encode_bits( hPrivateData, ptr, extra, 7 );
202 : }
203 : }
204 :
205 221520 : return;
206 : }
207 :
208 :
209 : /*---------------------------------------------------------------------*
210 : * encode_sfe_vector()
211 : *
212 : *
213 : *---------------------------------------------------------------------*/
214 :
215 16753464 : static void encode_sfe_vector(
216 : IGFSCFENC_INSTANCE_HANDLE hPrivateData, /* i/o: instance handle */
217 : int16_t *ptr, /* i : pointer to expanded bit buffer, one bit in each short */
218 : const int16_t t, /* i : frame counter reset to 0 at each independent frame */
219 : int16_t *prev_x, /* i : previous vector */
220 : int16_t *x, /* i : current vector to encode */
221 : const int16_t length /* i : number of elements to encode */
222 : )
223 : {
224 : /*
225 : f
226 : ^
227 : | d a x
228 : | c b
229 : | e --> t
230 : */
231 : int16_t f, pred;
232 : int16_t ctx, ctx_f, ctx_t;
233 :
234 111030454 : for ( f = 0; f < length; f++ )
235 : {
236 94276990 : if ( t == 0 )
237 : {
238 93482668 : if ( f == 0 )
239 : {
240 : /* encode one of the IGF_SYMBOLS_IN_TABLE == 27 alphabet symbols using the new raw AC function */
241 16586386 : hPrivateData->ptrBitIndex = ari_encode_14bits_ext( ptr, hPrivateData->ptrBitIndex, &hPrivateData->acState, x[f] >> 2, hPrivateData->cf_se00 );
242 16586386 : arith_encode_bits( hPrivateData, ptr, x[f] & 3, 2 ); /* LSBs as 2 bit raw */
243 : }
244 76896282 : else if ( f == 1 )
245 : {
246 16586386 : pred = x[f - 1]; /* pred = b */
247 16586386 : arith_encode_residual( hPrivateData, ptr, x[f] - pred, hPrivateData->cf_se01, hPrivateData->cf_off_se01 );
248 : }
249 : else
250 : {
251 : /* f >= 2 */
252 60309896 : pred = x[f - 1]; /* pred = b */
253 60309896 : ctx = quant_ctx( x[f - 1] - x[f - 2] ); /* Q(b - e) */
254 60309896 : arith_encode_residual( hPrivateData, ptr, x[f] - pred, &hPrivateData->cf_se02[( IGF_SYMBOLS_IN_TABLE + 1 ) * ( IGF_CTX_OFFSET + ctx )], hPrivateData->cf_off_se02[IGF_CTX_OFFSET + ctx] );
255 : }
256 : }
257 : else
258 : {
259 : /* t == 1 */
260 794322 : if ( f == 0 )
261 : {
262 167078 : pred = prev_x[f]; /* pred = a */
263 167078 : arith_encode_residual( hPrivateData, ptr, x[f] - pred, hPrivateData->cf_se10, hPrivateData->cf_off_se10 );
264 : }
265 : else
266 : {
267 : /* (t == 1) && (f >= 1) */
268 627244 : pred = prev_x[f] + x[f - 1] - prev_x[f - 1]; /* pred = a + b - c */
269 627244 : ctx_f = quant_ctx( prev_x[f] - prev_x[f - 1] ); /* Q(a - c) */
270 627244 : ctx_t = quant_ctx( x[f - 1] - prev_x[f - 1] ); /* Q(b - c) */
271 627244 : arith_encode_residual( hPrivateData, ptr, x[f] - pred, &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 )] );
272 : }
273 : }
274 : }
275 :
276 16753464 : return;
277 : }
278 :
279 :
280 : /*---------------------------------------------------------------------*
281 : * IGFSCFEncoderReset()
282 : *
283 : * Reset of Arith enc context memory
284 : *---------------------------------------------------------------------*/
285 :
286 16944181 : void IGFSCFEncoderReset(
287 : IGFSCFENC_INSTANCE_HANDLE hPublicData /* i/o: handle to public data */
288 : )
289 : {
290 16944181 : hPublicData->t = 0;
291 :
292 : /* we do not need to fill hPublicData->prev with zeros, because when t = 0 no previous information is used */
293 :
294 16944181 : return;
295 : }
296 :
297 :
298 : /*---------------------------------------------------------------------*
299 : * IGFSCFEncoderEncode()
300 : *
301 : * main IGF encoder function
302 : *---------------------------------------------------------------------*/
303 :
304 16753464 : int16_t IGFSCFEncoderEncode(
305 : IGFSCFENC_INSTANCE_HANDLE hPublicData, /* i/o: handle to public data or NULL in case there was no instance created */
306 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
307 : const int16_t bitCount, /* i : offset to the first bit in bitbuffer which should be readed by iisArithDecoderDecode function */
308 : int16_t *sfe, /* i : ptr to an array which contain quantized scalefactor energies */
309 : const int16_t igfGridIdx, /* i : igf grid index see declaration of IGF_GRID_IDX for details */
310 : const int16_t indepFlag /* i : if 1 frame is independent, 0 = frame is coded with data from previous frame */
311 : )
312 : {
313 : int16_t ptr[IGF_BITBUFSIZE]; /* temporary expanded bit buffer, one bit in each short */
314 : int16_t i;
315 :
316 : /* insert data: */
317 16753464 : hPublicData->ptrBitIndex = 0;
318 16753464 : hPublicData->bitCount = bitCount;
319 16753464 : ari_start_encoding_14bits( &hPublicData->acState ); /* start AC encoding */
320 :
321 : /* check if coder needs a reset and do it if necessary */
322 16753464 : if ( indepFlag )
323 : {
324 16586271 : IGFSCFEncoderReset( hPublicData );
325 : }
326 :
327 16753464 : encode_sfe_vector( hPublicData, ptr, hPublicData->t, hPublicData->prev, sfe, hPublicData->scfCountLongBlock[igfGridIdx] );
328 :
329 16753464 : hPublicData->ptrBitIndex = ari_done_encoding_14bits( ptr, hPublicData->ptrBitIndex, &hPublicData->acState ); /* finish AC encoding */
330 16753464 : hPublicData->bitCount = hPublicData->bitCount + hPublicData->ptrBitIndex;
331 :
332 : /* advancing history: */
333 16753464 : mvs2s( sfe, hPublicData->prev, hPublicData->scfCountLongBlock[igfGridIdx] );
334 16753464 : hPublicData->t++;
335 :
336 : /* copy the bits from the temporary bit buffer, if doRealEncoding is enabled */
337 16753464 : if ( hBstr )
338 : {
339 216277286 : for ( i = 0; i < hPublicData->ptrBitIndex; ++i )
340 : {
341 207733343 : push_next_indice( hBstr, ptr[i], 1 );
342 : }
343 : }
344 :
345 : /* return next bit offset in the stream */
346 16753464 : return hPublicData->bitCount;
347 : }
348 :
349 :
350 : /*---------------------------------------------------------------------*
351 : * IGFSCFEncoderSaveContextState()
352 : *
353 : * for a closed loop enc, the ArithEncoder needs to memorize the context
354 : *---------------------------------------------------------------------*/
355 :
356 8387733 : void IGFSCFEncoderSaveContextState(
357 : IGFSCFENC_INSTANCE_HANDLE hPublicData, /* i/o: handle to public data or NULL in case there was no instance created */
358 : const int16_t igfGridIdx /* i : igf grid index see declaration of IGF_GRID_IDX for details */
359 : )
360 : {
361 8387733 : hPublicData->Tsave = hPublicData->t;
362 :
363 8387733 : mvs2s( hPublicData->prev, hPublicData->prevSave, hPublicData->scfCountLongBlock[igfGridIdx] );
364 :
365 8387733 : return;
366 : }
367 :
368 :
369 : /*---------------------------------------------------------------------*
370 : * IGFSCFEncoderRestoreContextState()
371 : *
372 : * for a closed loop enc, the ArithEncoder needs to memorize the context
373 : *---------------------------------------------------------------------*/
374 :
375 8387733 : void IGFSCFEncoderRestoreContextState(
376 : IGFSCFENC_INSTANCE_HANDLE hPublicData, /* i/o: handle to public data or NULL in case there was no instance created */
377 : const int16_t igfGridIdx /* i : igf grid index see declaration of IGF_GRID_IDX for details */
378 : )
379 : {
380 8387733 : hPublicData->t = hPublicData->Tsave;
381 :
382 8387733 : mvs2s( hPublicData->prevSave, hPublicData->prev, hPublicData->scfCountLongBlock[igfGridIdx] );
383 :
384 8387733 : return;
385 : }
|