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 37444 : 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 37444 : hPublicData->ptrBitIndex = 0;
66 37444 : hPublicData->bitCount = 0;
67 37444 : hPublicData->Tsave = 0;
68 37444 : hPublicData->contex_saved = 0;
69 37444 : hPublicData->acState.low = 0;
70 37444 : hPublicData->acState.high = 0;
71 37444 : hPublicData->acState.bits_to_follow = 0;
72 37444 : set_s( hPublicData->prev, 0, 64 );
73 37444 : set_s( hPublicData->prevSave, 0, 64 );
74 :
75 37444 : hPublicData->scfCountLongBlock[0] = hIgfInfo->grid[0].swb_offset_len - 1;
76 37444 : hPublicData->scfCountLongBlock[1] = hIgfInfo->grid[1].swb_offset_len - 1;
77 37444 : hPublicData->scfCountLongBlock[2] = hIgfInfo->grid[2].swb_offset_len - 1;
78 37444 : hPublicData->t = 0; /* protect against the invalid request of starting encoding with a dependent block */
79 :
80 37444 : 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 37444 : return;
83 : }
84 :
85 :
86 : /*---------------------------------------------------------------------*
87 : * quant_ctx()
88 : *
89 : *
90 : *---------------------------------------------------------------------*/
91 :
92 3899393 : 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 3899393 : if ( abs( ctx ) <= 3 )
100 : {
101 3336665 : return ctx;
102 : }
103 562728 : else if ( ctx > 3 )
104 : {
105 148078 : return 3;
106 : }
107 : else /* ctx < -3 */
108 : {
109 414650 : return -3;
110 : }
111 : }
112 :
113 :
114 : /*---------------------------------------------------------------------*
115 : * arith_encode_bits()
116 : *
117 : *
118 : *---------------------------------------------------------------------*/
119 :
120 1200650 : 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 3628526 : for ( i = nBits - 1; i >= 0; --i )
131 : {
132 2427876 : bit = ( x >> i ) & 1;
133 2427876 : hPrivateData->ptrBitIndex = ari_encode_14bits_sign( ptr, hPrivateData->ptrBitIndex, MAX16B, /* disable the bit count limitation */ &hPrivateData->acState, bit );
134 : }
135 :
136 1200650 : return;
137 : }
138 :
139 :
140 : /*---------------------------------------------------------------------*
141 : * arith_encode_residual()
142 : *
143 : *
144 : *---------------------------------------------------------------------*/
145 :
146 5059794 : 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 5059794 : x += tableOffset;
157 5059794 : 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 5046782 : hPrivateData->ptrBitIndex = ari_encode_14bits_ext( ptr, hPrivateData->ptrBitIndex, &hPrivateData->acState, ( x - IGF_MIN_ENC_SEPARATE ) + 1, cumulativeFrequencyTable );
161 :
162 5046782 : return;
163 : }
164 13012 : else if ( x < IGF_MIN_ENC_SEPARATE )
165 : {
166 : /* send escape code 0 to indicate x <= IGF_MIN_ENC_SEPARATE - 1 */
167 9739 : extra = ( IGF_MIN_ENC_SEPARATE - 1 ) - x;
168 9739 : 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 3273 : extra = x - ( IGF_MAX_ENC_SEPARATE + 1 );
175 3273 : 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 13012 : if ( extra < 15 )
180 : {
181 : /* encode extra with 4 bits if extra < 15 */
182 12874 : arith_encode_bits( hPrivateData, ptr, extra, 4 );
183 : }
184 : else
185 : {
186 : /* extra >= 15 */
187 : /* send escape code 15 to indicate extra >= 15 */
188 138 : arith_encode_bits( hPrivateData, ptr, 15, 4 );
189 138 : extra -= 15;
190 :
191 138 : if ( extra < 63 )
192 : {
193 : /* encode additional extra with 6 bits */
194 138 : 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 13012 : return;
206 : }
207 :
208 :
209 : /*---------------------------------------------------------------------*
210 : * encode_sfe_vector()
211 : *
212 : *
213 : *---------------------------------------------------------------------*/
214 :
215 1199302 : 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 7446596 : for ( f = 0; f < length; f++ )
235 : {
236 6247294 : if ( t == 0 )
237 : {
238 6196591 : if ( f == 0 )
239 : {
240 : /* encode one of the IGF_SYMBOLS_IN_TABLE == 27 alphabet symbols using the new raw AC function */
241 1187500 : hPrivateData->ptrBitIndex = ari_encode_14bits_ext( ptr, hPrivateData->ptrBitIndex, &hPrivateData->acState, x[f] >> 2, hPrivateData->cf_se00 );
242 1187500 : arith_encode_bits( hPrivateData, ptr, x[f] & 3, 2 ); /* LSBs as 2 bit raw */
243 : }
244 5009091 : else if ( f == 1 )
245 : {
246 1187500 : pred = x[f - 1]; /* pred = b */
247 1187500 : arith_encode_residual( hPrivateData, ptr, x[f] - pred, hPrivateData->cf_se01, hPrivateData->cf_off_se01 );
248 : }
249 : else
250 : {
251 : /* f >= 2 */
252 3821591 : pred = x[f - 1]; /* pred = b */
253 3821591 : ctx = quant_ctx( x[f - 1] - x[f - 2] ); /* Q(b - e) */
254 3821591 : 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 50703 : if ( f == 0 )
261 : {
262 11802 : pred = prev_x[f]; /* pred = a */
263 11802 : 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 38901 : pred = prev_x[f] + x[f - 1] - prev_x[f - 1]; /* pred = a + b - c */
269 38901 : ctx_f = quant_ctx( prev_x[f] - prev_x[f - 1] ); /* Q(a - c) */
270 38901 : ctx_t = quant_ctx( x[f - 1] - prev_x[f - 1] ); /* Q(b - c) */
271 38901 : 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 1199302 : return;
277 : }
278 :
279 :
280 : /*---------------------------------------------------------------------*
281 : * IGFSCFEncoderReset()
282 : *
283 : * Reset of Arith enc context memory
284 : *---------------------------------------------------------------------*/
285 :
286 1188386 : void IGFSCFEncoderReset(
287 : IGFSCFENC_INSTANCE_HANDLE hPublicData /* i/o: handle to public data */
288 : )
289 : {
290 1188386 : 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 1188386 : return;
295 : }
296 :
297 :
298 : /*---------------------------------------------------------------------*
299 : * IGFSCFEncoderEncode()
300 : *
301 : * main IGF encoder function
302 : *---------------------------------------------------------------------*/
303 :
304 1199302 : 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 1199302 : hPublicData->ptrBitIndex = 0;
318 1199302 : hPublicData->bitCount = bitCount;
319 1199302 : ari_start_encoding_14bits( &hPublicData->acState ); /* start AC encoding */
320 :
321 : /* check if coder needs a reset and do it if necessary */
322 1199302 : if ( indepFlag )
323 : {
324 1187500 : IGFSCFEncoderReset( hPublicData );
325 : }
326 :
327 1199302 : encode_sfe_vector( hPublicData, ptr, hPublicData->t, hPublicData->prev, sfe, hPublicData->scfCountLongBlock[igfGridIdx] );
328 :
329 1199302 : hPublicData->ptrBitIndex = ari_done_encoding_14bits( ptr, hPublicData->ptrBitIndex, &hPublicData->acState ); /* finish AC encoding */
330 1199302 : hPublicData->bitCount = hPublicData->bitCount + hPublicData->ptrBitIndex;
331 :
332 : /* advancing history: */
333 1199302 : mvs2s( sfe, hPublicData->prev, hPublicData->scfCountLongBlock[igfGridIdx] );
334 1199302 : hPublicData->t++;
335 :
336 : /* copy the bits from the temporary bit buffer, if doRealEncoding is enabled */
337 1199302 : if ( hBstr )
338 : {
339 13622240 : for ( i = 0; i < hPublicData->ptrBitIndex; ++i )
340 : {
341 13010785 : push_next_indice( hBstr, ptr[i], 1 );
342 : }
343 : }
344 :
345 : /* return next bit offset in the stream */
346 1199302 : 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 588287 : 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 588287 : hPublicData->Tsave = hPublicData->t;
362 :
363 588287 : mvs2s( hPublicData->prev, hPublicData->prevSave, hPublicData->scfCountLongBlock[igfGridIdx] );
364 :
365 588287 : return;
366 : }
367 :
368 :
369 : /*---------------------------------------------------------------------*
370 : * IGFSCFEncoderRestoreContextState()
371 : *
372 : * for a closed loop enc, the ArithEncoder needs to memorize the context
373 : *---------------------------------------------------------------------*/
374 :
375 588287 : 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 588287 : hPublicData->t = hPublicData->Tsave;
381 :
382 588287 : mvs2s( hPublicData->prevSave, hPublicData->prev, hPublicData->scfCountLongBlock[igfGridIdx] );
383 :
384 588287 : return;
385 : }
|