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_fx.h"
40 : #include "stat_enc.h"
41 : #include "stat_com.h"
42 : #include "cnst.h"
43 : #include "wmc_auto.h"
44 : #include "prot_fx_enc.h"
45 :
46 :
47 : /*---------------------------------------------------------------------*
48 : * quant_ctx()
49 : *
50 : *
51 : *---------------------------------------------------------------------*/
52 :
53 3803227 : static Word16 quant_ctx(
54 : const Word16 ctx )
55 : {
56 : /*
57 : ctx ... -5 -4 -3 -2 -1 0 1 2 3 4 5 ...
58 : Q(ctx)... -3 -3 -3 -2 -1 0 1 2 3 3 3 ...
59 : */
60 3803227 : IF( LE_16( abs_s( ctx ), 3 ) )
61 : {
62 3256291 : return ctx;
63 : }
64 546936 : ELSE IF( GT_16( ctx, 3 ) )
65 : {
66 142614 : return 3;
67 : }
68 : ELSE /* ctx < -3 */
69 : {
70 404322 : return -3;
71 : }
72 : }
73 :
74 :
75 : /*---------------------------------------------------------------------*
76 : * arith_encode_bits()
77 : *
78 : *
79 : *---------------------------------------------------------------------*/
80 :
81 1163384 : static void arith_encode_bits(
82 : IGFSCFENC_INSTANCE_HANDLE hPrivateData, /* i/o: instance handle */
83 : Word16 *ptr, /* i/o: pointer to expanded bit buffer, one bit in each Word16 */
84 : Word16 x, /* i: value to encode */
85 : Word16 nBits /* i: number of bits to encode */
86 : )
87 : {
88 : Word16 i;
89 : Word16 bit;
90 :
91 :
92 3517292 : FOR( i = nBits - 1; i >= 0; --i ) /* nBits > 0 */
93 : {
94 2353908 : bit = s_and( shr( x, i ), 1 );
95 4707816 : hPrivateData->ptrBitIndex = ari_encode_14bits_sign_ivas_fx( ptr,
96 2353908 : hPrivateData->ptrBitIndex,
97 : 32767, /* disable the bit count limitation */
98 : &hPrivateData->acState,
99 : bit );
100 2353908 : move16();
101 : }
102 1163384 : }
103 :
104 :
105 : /*---------------------------------------------------------------------*
106 : * arith_encode_residual()
107 : *
108 : *
109 : *---------------------------------------------------------------------*/
110 :
111 4927739 : static void arith_encode_residual(
112 : IGFSCFENC_INSTANCE_HANDLE hPrivateData, /* i/o: instance handle */
113 : Word16 *ptr, /* i/o: pointer to expanded bit buffer, one bit in each Word16 */
114 : Word16 x, /* i: residual value to encode */
115 : const UWord16 *cumulativeFrequencyTable, /* i: cumulative frequency table to be used */
116 : Word16 tableOffset /* i: offset used to align the table */
117 : )
118 : {
119 : Word16 extra;
120 : Word16 extra_tmp;
121 : Word16 extra_safety;
122 :
123 :
124 4927739 : x = add( x, tableOffset );
125 :
126 4927739 : test();
127 4927739 : IF( ( GE_16( x, IGF_MIN_ENC_SEPARATE ) ) && ( LE_16( x, IGF_MAX_ENC_SEPARATE ) ) )
128 : {
129 4914543 : x = sub( x, IGF_MIN_ENC_SEPARATE - 1 ); /* (x - IGF_MIN_ENC_SEPARATE) + 1 */
130 : /* encode one of the IGF_SYMBOLS_IN_TABLE == 27 alphabet symbols using the new raw AC function */
131 9829086 : hPrivateData->ptrBitIndex = ari_encode_14bits_ext_ivas_fx( ptr,
132 4914543 : hPrivateData->ptrBitIndex,
133 : &hPrivateData->acState,
134 : x,
135 : cumulativeFrequencyTable );
136 4914543 : move16();
137 4914543 : return;
138 : }
139 :
140 13196 : IF( LT_16( x, IGF_MIN_ENC_SEPARATE ) )
141 : {
142 : /* send escape code 0 to indicate x <= IGF_MIN_ENC_SEPARATE - 1 */
143 10108 : extra = sub( IGF_MIN_ENC_SEPARATE - 1, x );
144 20216 : hPrivateData->ptrBitIndex = ari_encode_14bits_ext_ivas_fx( ptr,
145 10108 : hPrivateData->ptrBitIndex,
146 : &hPrivateData->acState,
147 : 0,
148 : cumulativeFrequencyTable );
149 10108 : move16();
150 : }
151 : ELSE /* x > IGF_MAX_ENC_SEPARATE */
152 : {
153 : /* send escape code (IGF_SYMBOLS_IN_TABLE - 1) to indicate x >= IGF_MAX_ENC_SEPARATE + 1 */
154 3088 : extra = sub( x, IGF_MAX_ENC_SEPARATE + 1 );
155 6176 : hPrivateData->ptrBitIndex = ari_encode_14bits_ext_ivas_fx( ptr,
156 3088 : hPrivateData->ptrBitIndex,
157 : &hPrivateData->acState,
158 : IGF_SYMBOLS_IN_TABLE - 1,
159 : cumulativeFrequencyTable );
160 3088 : move16();
161 : }
162 :
163 : /* encode one of the tails of the distribution */
164 13196 : extra_tmp = sub( extra, 15 );
165 13196 : IF( extra_tmp < 0 )
166 : {
167 : /* encode extra with 4 bits if extra < 15 */
168 13009 : arith_encode_bits( hPrivateData, ptr, extra, 4 );
169 : }
170 : ELSE /* extra >= 15 */
171 : {
172 : /* send escape code 15 to indicate extra >= 15 */
173 187 : arith_encode_bits( hPrivateData, ptr, 15, 4 );
174 :
175 187 : extra_safety = sub( extra_tmp, 63 );
176 187 : IF( extra_safety < 0 )
177 : {
178 : /* encode additional extra with 6 bits */
179 187 : arith_encode_bits( hPrivateData, ptr, extra_tmp, 6 );
180 : }
181 : ELSE
182 : { /* extra_tmp >= 63 */
183 0 : arith_encode_bits( hPrivateData, ptr, 63, 6 );
184 : /* encode safety extra with 7 bits */
185 0 : arith_encode_bits( hPrivateData, ptr, extra_safety, 7 );
186 : }
187 : }
188 : }
189 :
190 :
191 : /*---------------------------------------------------------------------*
192 : * encode_sfe_vector()
193 : *
194 : *
195 : *---------------------------------------------------------------------*/
196 1161041 : static void encode_sfe_vector(
197 : IGFSCFENC_INSTANCE_HANDLE hPrivateData, /* i/o: instance handle */
198 : Word16 *ptr, /* i : pointer to expanded bit buffer, one bit in each short */
199 : const Word16 t, /* i : frame counter reset to 0 at each independent frame */
200 : Word16 *prev_x, /* i : previous vector */
201 : Word16 *x, /* i : current vector to encode */
202 : const Word16 length /* i : number of elements to encode */
203 : )
204 : {
205 : /*
206 : f
207 : ^
208 : | d a x
209 : | c b
210 : | e --> t
211 : */
212 : Word16 f;
213 : Word16 pred;
214 : Word16 ctx;
215 : Word16 ctx_f;
216 : Word16 ctx_t;
217 :
218 7238781 : FOR( f = 0; f < length; f++ )
219 : {
220 6077740 : IF( t == 0 )
221 : {
222 6030171 : IF( f == 0 )
223 : {
224 : /* (t == 0) && (f == 0) */
225 : /* encode one of the IGF_SYMBOLS_IN_TABLE == 27 alphabet symbols using the new raw AC function */
226 3450003 : hPrivateData->ptrBitIndex = ari_encode_14bits_ext_ivas_fx( ptr,
227 1150001 : hPrivateData->ptrBitIndex,
228 : &hPrivateData->acState,
229 1150001 : shr( x[f], 2 ),
230 : (const UWord16 *) hPrivateData->cf_se00 );
231 1150001 : move16();
232 1150001 : arith_encode_bits( hPrivateData, ptr, x[f] & 3, 2 ); /* LSBs as 2 bit raw */
233 : }
234 4880170 : ELSE IF( EQ_16( f, 1 ) )
235 : {
236 1150001 : pred = x[f - 1]; /* pred = b */
237 1150001 : move16();
238 1150001 : arith_encode_residual( hPrivateData, ptr, x[f] - pred, hPrivateData->cf_se01, hPrivateData->cf_off_se01 );
239 : }
240 : ELSE
241 : {
242 : /* f >= 2 */
243 3730169 : pred = x[f - 1]; /* pred = b */
244 3730169 : move16();
245 3730169 : ctx = quant_ctx( sub( x[f - 1], x[f - 2] ) ); /* Q(b - e) */
246 3730169 : arith_encode_residual( hPrivateData, ptr, sub( x[f], pred ), &hPrivateData->cf_se02[( IGF_SYMBOLS_IN_TABLE + 1 ) * ( IGF_CTX_OFFSET + ctx )], hPrivateData->cf_off_se02[IGF_CTX_OFFSET + ctx] );
247 : }
248 6030171 : move16();
249 : }
250 : ELSE
251 : {
252 : /* t == 1 */
253 47569 : IF( f == 0 )
254 : {
255 11040 : pred = prev_x[f]; /* pred = a */
256 11040 : move16();
257 11040 : arith_encode_residual( hPrivateData, ptr, sub( x[f], pred ), hPrivateData->cf_se10, hPrivateData->cf_off_se10 );
258 : }
259 : ELSE
260 : {
261 : /* (t == 1) && (f >= 1) */
262 36529 : pred = add( prev_x[f], sub( x[f - 1], prev_x[f - 1] ) ); /* pred = a + b - c */
263 36529 : ctx_f = quant_ctx( sub( prev_x[f], prev_x[f - 1] ) ); /* Q(a - c) */
264 36529 : ctx_t = quant_ctx( sub( x[f - 1], prev_x[f - 1] ) ); /* Q(b - c) */
265 36529 : 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 )] );
266 : }
267 : }
268 : }
269 :
270 1161041 : return;
271 : }
272 :
273 :
274 : /*---------------------------------------------------------------------*
275 : * IGFSCFEncoderEncode_ivas_fx()
276 : *
277 : * main IGF encoder function
278 : *---------------------------------------------------------------------*/
279 1161041 : Word16 IGFSCFEncoderEncode_ivas_fx(
280 : IGFSCFENC_INSTANCE_HANDLE hPublicData, /* i/o: handle to public data or NULL in case there was no instance created */
281 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
282 : const Word16 bitCount, /* i : offset to the first bit in bitbuffer which should be readed by iisArithDecoderDecode function */
283 : Word16 *sfe, /* i : ptr to an array which contain quantized scalefactor energies */
284 : const Word16 igfGridIdx, /* i : igf grid index see declaration of IGF_GRID_IDX for details */
285 : const Word16 indepFlag /* i : if 1 frame is independent, 0 = frame is coded with data from previous frame */
286 : )
287 : {
288 : Word16 ptr[IGF_BITBUFSIZE]; /* temporary expanded bit buffer, one bit in each short */
289 : Word16 i;
290 :
291 : /* insert data: */
292 1161041 : hPublicData->ptrBitIndex = 0;
293 1161041 : move16();
294 1161041 : hPublicData->bitCount = bitCount;
295 1161041 : move16();
296 1161041 : ari_start_encoding_14bits_ivas_fx( &hPublicData->acState ); /* start AC encoding */
297 :
298 : /* check if coder needs a reset and do it if necessary */
299 1161041 : IF( indepFlag != 0 )
300 : {
301 1150001 : IGFSCFEncoderReset_fx( hPublicData );
302 : }
303 :
304 1161041 : encode_sfe_vector( hPublicData, ptr, hPublicData->t, hPublicData->prev, sfe, hPublicData->scfCountLongBlock[igfGridIdx] );
305 :
306 1161041 : hPublicData->ptrBitIndex = ari_done_encoding_14bits_ivas_fx( ptr, hPublicData->ptrBitIndex, &hPublicData->acState ); /* finish AC encoding */
307 1161041 : hPublicData->bitCount = add( hPublicData->bitCount, hPublicData->ptrBitIndex );
308 1161041 : move16();
309 1161041 : move16();
310 :
311 : /* advancing history: */
312 1161041 : Copy( sfe, hPublicData->prev, hPublicData->scfCountLongBlock[igfGridIdx] );
313 1161041 : hPublicData->t = add( hPublicData->t, 1 );
314 1161041 : move16();
315 :
316 : /* copy the bits from the temporary bit buffer, if doRealEncoding is enabled */
317 1161041 : IF( hBstr )
318 : {
319 13210983 : FOR( i = 0; i < hPublicData->ptrBitIndex; ++i )
320 : {
321 12619422 : push_next_indice( hBstr, ptr[i], 1 );
322 : }
323 : }
324 :
325 : /* return next bit offset in the stream */
326 1161041 : return hPublicData->bitCount;
327 : }
|