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 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include "prot.h"
43 : #include "cnst.h"
44 : #include "wmc_auto.h"
45 : #include <math.h>
46 :
47 : /*-------------------------------------------------------------------*
48 : * Local prototypes
49 : *-------------------------------------------------------------------*/
50 :
51 : static void read_cv( Decoder_State *st, uint16_t *I, int16_t *kv, int16_t nq, int16_t *nbits );
52 :
53 : /*-----------------------------------------------------------------*
54 : * AVQ_demuxdec()
55 : *
56 : * Demultiplex and decode subvectors using
57 : * split algebraic vector dequantizer based on RE8 latice.
58 : *-----------------------------------------------------------------*/
59 :
60 2383263 : void AVQ_demuxdec(
61 : Decoder_State *st, /* i/o: decoder state structure */
62 : int16_t xriq[], /* o : decoded subvectors [0..8*Nsv-1]*/
63 : int16_t *nb_bits, /* i/o: number of allocated bits */
64 : const int16_t Nsv, /* i : number of subvectors */
65 : int16_t nq[], /* i/o: AVQ nq index */
66 : int16_t avq_bit_sFlag, /* i : flag for AVQ bit saving solution */
67 : int16_t trgtSvPos /* i : target SV for AVQ bit savings */
68 : )
69 : {
70 : int16_t i, j, bits;
71 : uint16_t I[NSV_MAX];
72 : int16_t code[8];
73 : int16_t *kv;
74 : int16_t nq_est, unused_bits_idx;
75 : int16_t bitsMod;
76 : int16_t underflow;
77 : int16_t unusedbitsFlag;
78 : int16_t svOrder[NSV_MAX], k, nullVec, dummy_bits;
79 :
80 2383263 : kv = xriq; /* reuse vector to save memory */
81 2383263 : bits = *nb_bits;
82 2383263 : bitsMod = 0;
83 2383263 : underflow = 0;
84 2383263 : unusedbitsFlag = 0;
85 2383263 : nullVec = 0;
86 2383263 : dummy_bits = 0;
87 2383263 : svOrder[Nsv - 1] = trgtSvPos;
88 2383263 : svOrder[0] = 0;
89 2383263 : svOrder[1] = 1;
90 2383263 : i = 2;
91 2383263 : j = i;
92 2383263 : if ( avq_bit_sFlag == 2 )
93 : {
94 935786 : j = i + 1;
95 : }
96 :
97 14623803 : while ( i < Nsv - 1 )
98 : {
99 12240540 : svOrder[i] = j;
100 12240540 : i++;
101 12240540 : j++;
102 : }
103 :
104 83414205 : for ( i = 0; i < NSV_MAX; i++ )
105 : {
106 81030942 : I[i] = (uint16_t) -1;
107 : }
108 :
109 20300528 : for ( i = 0; i < Nsv; i++ )
110 : {
111 19382509 : k = svOrder[i];
112 19382509 : if ( avq_bit_sFlag == 2 && bits % 5 == 4 && bits > 8 && bits < 30 && k >= trgtSvPos && i < Nsv - 1 )
113 : {
114 128492 : ordr_esti( Nsv - i, &trgtSvPos, &svOrder[i], Nsv );
115 128492 : k = svOrder[i];
116 128492 : avq_bit_sFlag = 1;
117 : }
118 :
119 19382509 : if ( k == trgtSvPos && avq_bit_sFlag > 0 )
120 : {
121 2174905 : if ( ( *nb_bits - bits ) == 7 || bits < BIT_SAVING_LOW_THR || bits >= BIT_SAVING_HIGH_THR )
122 : {
123 709661 : avq_bit_sFlag = 0;
124 : }
125 : else
126 : {
127 : break;
128 : }
129 : }
130 :
131 17917265 : nq[k] = 0; /* initialization and also forced if the budget is exceeded */
132 :
133 17917265 : if ( bits > 8 )
134 : {
135 : /* read the unary code including the stop bit for nq[i] */
136 17179501 : nq[k] = -1;
137 : do
138 : {
139 37177753 : ( nq[k] )++;
140 :
141 37177753 : if ( 5 * nq[k] + 4 == bits )
142 : {
143 174421 : break;
144 : }
145 37003332 : } while ( get_next_indice_1( st ) );
146 :
147 17179501 : if ( 5 * nq[k] + 4 == bits ) /* check the overflow */
148 : {
149 174421 : bits++; /* overflow stop bit */
150 : }
151 :
152 : /* check for potential bit errors */
153 17179501 : if ( nq[k] > NB_SPHERE )
154 : {
155 0 : st->BER_detect = 1;
156 0 : set_s( xriq, 0, Nsv * 8 );
157 0 : set_s( nq, 0, Nsv );
158 0 : *nb_bits = 0;
159 :
160 0 : return;
161 : }
162 :
163 17179501 : bits -= nq[k];
164 17179501 : bits--; /* count the stop bit */
165 :
166 17179501 : if ( nq[k] > 0 )
167 : {
168 12606887 : nq[k]++;
169 : }
170 :
171 : /* read codebook indices (rank I and event. Voronoi index kv) */
172 17179501 : read_cv( st, &I[k], &kv[k * 8], nq[k], &bits );
173 : }
174 : } /* for */
175 :
176 : /* Bit Saving Solution */
177 2383263 : if ( avq_bit_sFlag > 0 && bits > 8 )
178 : {
179 1465244 : i = svOrder[Nsv - 1];
180 1465244 : nq[i] = 0;
181 1465244 : bitsMod = bits % 5;
182 1465244 : if ( i != Nsv - 1 )
183 : {
184 673010 : nullVec = 0;
185 4038060 : for ( j = i; j < Nsv - 1; j++ )
186 : {
187 3365050 : if ( nq[svOrder[j]] == 0 )
188 : {
189 499030 : nullVec++;
190 : }
191 : }
192 673010 : nq_est = bits / 5;
193 673010 : if ( ( bitsMod > 0 || ( nullVec == 4 && nq_est == 5 ) ) && bitsMod != 4 && ( bits + nullVec ) >= 5 * nq_est + 4 && nq[svOrder[Nsv - 2]] == 0 ) /* dummy bits */
194 : {
195 9954 : dummy_bits = 5 - bitsMod;
196 9954 : bits = bits + dummy_bits; /* add dummy bits */
197 9954 : bitsMod = 0;
198 : }
199 663056 : else if ( nq_est > 4 && ( ( bitsMod == 0 && nullVec > 3 && nullVec < 6 ) || ( bitsMod == 4 && nullVec == 5 ) ) && nq[svOrder[Nsv - 2]] == 0 ) /* wasted bits 4, 5 for nq 6,7..*/
200 : {
201 2263 : underflow = 0;
202 2263 : if ( ( bitsMod + nullVec ) % 5 != 0 )
203 : {
204 1561 : underflow = 1;
205 : }
206 2263 : dummy_bits = nullVec + underflow;
207 2263 : bits = bits + dummy_bits; /* add dummy bits */
208 2263 : bitsMod = 0;
209 : }
210 : }
211 1465244 : underflow = 1;
212 1465244 : if ( bitsMod != 4 )
213 : {
214 1223071 : underflow = 0;
215 1223071 : bits -= bitsMod;
216 : }
217 1465244 : bits += underflow;
218 :
219 : /* read the unary code for unused bit*/
220 1465244 : unused_bits_idx = -1;
221 : do
222 : {
223 2260535 : ( unused_bits_idx )++;
224 2260535 : if ( 5 * unused_bits_idx + 4 == bits - 1 )
225 : {
226 25291 : break;
227 : }
228 2235244 : } while ( get_next_indice_1( st ) );
229 :
230 1465244 : unusedbitsFlag = 0;
231 :
232 1465244 : if ( dummy_bits == 0 )
233 : {
234 1453027 : if ( unused_bits_idx == 0 && bits > BIT_SAVING_LOW_THR )
235 : {
236 651344 : unusedbitsFlag = 1;
237 : }
238 801683 : else if ( unused_bits_idx == 1 && bits > BIT_SAVING_LOW_THR )
239 : {
240 322364 : unusedbitsFlag = -1;
241 : }
242 : }
243 :
244 : /*Compute AVQ code book number from unused Bits */
245 1465244 : nq_est = (int16_t) ceil( 0.2f * ( bits - 5 * ( unusedbitsFlag + unused_bits_idx ) ) );
246 :
247 1465244 : if ( nq_est == 1 )
248 : {
249 25291 : nq_est = 0;
250 : }
251 1465244 : bits -= underflow;
252 :
253 1465244 : bits -= unused_bits_idx;
254 :
255 1465244 : if ( nq_est != 0 )
256 : {
257 1439953 : bits--;
258 : }
259 :
260 1465244 : nq[i] = nq_est;
261 : /* read codebook indices (rank I and event. Voronoi index kv) */
262 1465244 : read_cv( st, &I[i], &kv[i * 8], nq[i], &bits );
263 :
264 1465244 : bits -= dummy_bits;
265 :
266 1465244 : if ( bitsMod != 4 )
267 : {
268 1223071 : bits += bitsMod;
269 : }
270 : }
271 :
272 : /* decode all subvectors */
273 21765772 : for ( i = 0; i < Nsv; i++ )
274 : {
275 : /* multi-rate RE8 decoder */
276 19382509 : re8_dec( nq[i], I[i], &kv[8 * i], code );
277 :
278 : /* write decoded RE8 vector to decoded subvector #i */
279 174442581 : for ( j = 0; j < 8; j++ )
280 : {
281 155060072 : xriq[i * 8 + j] = code[j];
282 : }
283 : }
284 :
285 2383263 : *nb_bits = bits;
286 :
287 2383263 : return;
288 : }
289 :
290 :
291 : /*-----------------------------------------------------------------*
292 : * AVQ_dec_lpc()
293 : *
294 : * Demultiplex and decode subvectors for LPC dequantization
295 : * using split algebraic vector dequantizer
296 : *-----------------------------------------------------------------*/
297 :
298 35251772 : void AVQ_dec_lpc(
299 : const int16_t indx[], /* i : index[] (4 bits per words) */
300 : int16_t nvecq[], /* o : vector quantized */
301 : const int16_t Nsv /* i : number of subvectors (lg=Nsv*8) */
302 : )
303 : {
304 : int16_t i, l, n, nq, nk, pos, ival, c[8], kv[8];
305 : int32_t I;
306 :
307 : /* last index word */
308 35251772 : pos = Nsv - 1;
309 :
310 105755316 : for ( l = 0; l < Nsv; l++ )
311 : {
312 70503544 : pos += indx[l];
313 : }
314 :
315 : /* decode all subvectors */
316 105755316 : for ( l = Nsv - 1; l >= 0; l-- )
317 : {
318 70503544 : nq = indx[l]; /* quantizer number (0,2,3..n) */
319 :
320 70503544 : nk = 0;
321 70503544 : n = nq;
322 :
323 70503544 : if ( nq > 4 )
324 : {
325 4357760 : nk = ( nq - 3 ) >> 1;
326 4357760 : n = nq - nk * 2;
327 : }
328 :
329 : /* read n groups of 4-bit for Voronoi index (k[]) */
330 634531896 : for ( i = 0; i < 8; i++ )
331 : {
332 564028352 : kv[i] = 0;
333 : }
334 :
335 75000744 : while ( nk-- > 0 )
336 : {
337 4497200 : ival = ( indx[pos--] & 0x0F );
338 4497200 : ival <<= 4;
339 4497200 : ival += ( indx[pos--] & 0x0F );
340 :
341 40474800 : for ( i = 7; i >= 0; i-- )
342 : {
343 35977600 : kv[i] <<= 1;
344 35977600 : kv[i] += ( ival & 0x01 );
345 35977600 : ival >>= 1;
346 : }
347 : }
348 :
349 : /* read n groups of 4-bit for base codebook index (I) */
350 70503544 : I = 0;
351 255456954 : while ( n-- > 0 )
352 : {
353 184953410 : I <<= 4;
354 184953410 : I += ( indx[pos--] & 0x0F );
355 : }
356 :
357 : /* multi-rate RE8 decoder */
358 70503544 : re8_dec( nq, (int16_t) I, kv, c );
359 :
360 : /* write decoded RE8 vector */
361 634531896 : for ( i = 0; i < 8; i++ )
362 : {
363 564028352 : nvecq[( l * 8 ) + i] = c[i];
364 : }
365 : }
366 :
367 35251772 : return;
368 : }
369 :
370 :
371 : /*-----------------------------------------------------------------*
372 : * read_cv()
373 : *
374 : * read codebook indices (rank I and event. Voronoi index kv)
375 : *-----------------------------------------------------------------*/
376 :
377 18644745 : static void read_cv(
378 : Decoder_State *st, /* i/o: decoder state structure */
379 : uint16_t *I, /* o : rank I code book index */
380 : int16_t *kv, /* o : Voronoi index kv */
381 : int16_t nq, /* i : AVQ nq index */
382 : int16_t *nbits /* i/o: bits available */
383 : )
384 :
385 : {
386 : int16_t j, bits, order_v;
387 :
388 18644745 : bits = *nbits;
389 :
390 : /* read codebook indices (rank I and event. Voronoi index kv) */
391 18644745 : if ( nq == 0 ) /* Q0 */
392 : {
393 : /* nothing to read */
394 : }
395 14046840 : else if ( nq < 5 ) /* Q2, Q3, Q4 */
396 : {
397 13680437 : *I = get_next_indice( st, 4 * nq );
398 13680437 : bits -= ( 4 * nq );
399 : }
400 366403 : else if ( nq % 2 == 0 ) /* Q4 + Voronoi extensions r=1,2,3,... */
401 : {
402 148815 : *I = get_next_indice( st, 4 * 4 );
403 148815 : bits -= 4 * 4;
404 148815 : order_v = (int16_t) ( nq / 2 ) - 2;
405 :
406 1339335 : for ( j = 0; j < 8; j++ )
407 : {
408 1190520 : kv[j] = get_next_indice( st, order_v );
409 : }
410 148815 : bits -= 8 * order_v;
411 : }
412 : else /* Q3 + Voronoi extensions r=1,2,3,... */
413 : {
414 217588 : *I = get_next_indice( st, 4 * 3 );
415 217588 : bits -= 4 * 3;
416 217588 : order_v = (int16_t) ( nq / 2 ) - 1;
417 :
418 1958292 : for ( j = 0; j < 8; j++ )
419 : {
420 1740704 : kv[j] = get_next_indice( st, order_v );
421 : }
422 217588 : bits -= 8 * order_v;
423 : }
424 18644745 : *nbits = bits;
425 :
426 18644745 : return;
427 : }
|