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 1438210 : 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 1438210 : kv = xriq; /* reuse vector to save memory */
81 1438210 : bits = *nb_bits;
82 1438210 : bitsMod = 0;
83 1438210 : underflow = 0;
84 1438210 : unusedbitsFlag = 0;
85 1438210 : nullVec = 0;
86 1438210 : dummy_bits = 0;
87 1438210 : svOrder[Nsv - 1] = trgtSvPos;
88 1438210 : svOrder[0] = 0;
89 1438210 : svOrder[1] = 1;
90 1438210 : i = 2;
91 1438210 : j = i;
92 1438210 : if ( avq_bit_sFlag == 2 )
93 : {
94 653643 : j = i + 1;
95 : }
96 :
97 8909167 : while ( i < Nsv - 1 )
98 : {
99 7470957 : svOrder[i] = j;
100 7470957 : i++;
101 7470957 : j++;
102 : }
103 :
104 50337350 : for ( i = 0; i < NSV_MAX; i++ )
105 : {
106 48899140 : I[i] = (uint16_t) -1;
107 : }
108 :
109 12344112 : for ( i = 0; i < Nsv; i++ )
110 : {
111 11778575 : k = svOrder[i];
112 11778575 : if ( avq_bit_sFlag == 2 && bits % 5 == 4 && bits > 8 && bits < 30 && k >= trgtSvPos && i < Nsv - 1 )
113 : {
114 83574 : ordr_esti( Nsv - i, &trgtSvPos, &svOrder[i], Nsv );
115 83574 : k = svOrder[i];
116 83574 : avq_bit_sFlag = 1;
117 : }
118 :
119 11778575 : if ( k == trgtSvPos && avq_bit_sFlag > 0 )
120 : {
121 1267260 : if ( ( *nb_bits - bits ) == 7 || bits < BIT_SAVING_LOW_THR || bits >= BIT_SAVING_HIGH_THR )
122 : {
123 394587 : avq_bit_sFlag = 0;
124 : }
125 : else
126 : {
127 : break;
128 : }
129 : }
130 :
131 10905902 : nq[k] = 0; /* initialization and also forced if the budget is exceeded */
132 :
133 10905902 : if ( bits > 8 )
134 : {
135 : /* read the unary code including the stop bit for nq[i] */
136 10515137 : nq[k] = -1;
137 : do
138 : {
139 23546333 : ( nq[k] )++;
140 :
141 23546333 : if ( 5 * nq[k] + 4 == bits )
142 : {
143 95197 : break;
144 : }
145 23451136 : } while ( get_next_indice_1( st ) );
146 :
147 10515137 : if ( 5 * nq[k] + 4 == bits ) /* check the overflow */
148 : {
149 95197 : bits++; /* overflow stop bit */
150 : }
151 :
152 : /* check for potential bit errors */
153 10515137 : 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 10515137 : bits -= nq[k];
164 10515137 : bits--; /* count the stop bit */
165 :
166 10515137 : if ( nq[k] > 0 )
167 : {
168 7830099 : nq[k]++;
169 : }
170 :
171 : /* read codebook indices (rank I and event. Voronoi index kv) */
172 10515137 : read_cv( st, &I[k], &kv[k * 8], nq[k], &bits );
173 : }
174 : } /* for */
175 :
176 : /* Bit Saving Solution */
177 1438210 : if ( avq_bit_sFlag > 0 && bits > 8 )
178 : {
179 872673 : i = svOrder[Nsv - 1];
180 872673 : nq[i] = 0;
181 872673 : bitsMod = bits % 5;
182 872673 : if ( i != Nsv - 1 )
183 : {
184 467382 : nullVec = 0;
185 2804292 : for ( j = i; j < Nsv - 1; j++ )
186 : {
187 2336910 : if ( nq[svOrder[j]] == 0 )
188 : {
189 336114 : nullVec++;
190 : }
191 : }
192 467382 : nq_est = bits / 5;
193 467382 : 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 7485 : dummy_bits = 5 - bitsMod;
196 7485 : bits = bits + dummy_bits; /* add dummy bits */
197 7485 : bitsMod = 0;
198 : }
199 459897 : 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 1350 : underflow = 0;
202 1350 : if ( ( bitsMod + nullVec ) % 5 != 0 )
203 : {
204 954 : underflow = 1;
205 : }
206 1350 : dummy_bits = nullVec + underflow;
207 1350 : bits = bits + dummy_bits; /* add dummy bits */
208 1350 : bitsMod = 0;
209 : }
210 : }
211 872673 : underflow = 1;
212 872673 : if ( bitsMod != 4 )
213 : {
214 726012 : underflow = 0;
215 726012 : bits -= bitsMod;
216 : }
217 872673 : bits += underflow;
218 :
219 : /* read the unary code for unused bit*/
220 872673 : unused_bits_idx = -1;
221 : do
222 : {
223 1449192 : ( unused_bits_idx )++;
224 1449192 : if ( 5 * unused_bits_idx + 4 == bits - 1 )
225 : {
226 20850 : break;
227 : }
228 1428342 : } while ( get_next_indice_1( st ) );
229 :
230 872673 : unusedbitsFlag = 0;
231 :
232 872673 : if ( dummy_bits == 0 )
233 : {
234 863838 : if ( unused_bits_idx == 0 && bits > BIT_SAVING_LOW_THR )
235 : {
236 380667 : unusedbitsFlag = 1;
237 : }
238 483171 : else if ( unused_bits_idx == 1 && bits > BIT_SAVING_LOW_THR )
239 : {
240 209082 : unusedbitsFlag = -1;
241 : }
242 : }
243 :
244 : /*Compute AVQ code book number from unused Bits */
245 872673 : nq_est = (int16_t) ceil( 0.2f * ( bits - 5 * ( unusedbitsFlag + unused_bits_idx ) ) );
246 :
247 872673 : if ( nq_est == 1 )
248 : {
249 20850 : nq_est = 0;
250 : }
251 872673 : bits -= underflow;
252 :
253 872673 : bits -= unused_bits_idx;
254 :
255 872673 : if ( nq_est != 0 )
256 : {
257 851823 : bits--;
258 : }
259 :
260 872673 : nq[i] = nq_est;
261 : /* read codebook indices (rank I and event. Voronoi index kv) */
262 872673 : read_cv( st, &I[i], &kv[i * 8], nq[i], &bits );
263 :
264 872673 : bits -= dummy_bits;
265 :
266 872673 : if ( bitsMod != 4 )
267 : {
268 726012 : bits += bitsMod;
269 : }
270 : }
271 :
272 : /* decode all subvectors */
273 13216785 : for ( i = 0; i < Nsv; i++ )
274 : {
275 : /* multi-rate RE8 decoder */
276 11778575 : re8_dec( nq[i], I[i], &kv[8 * i], code );
277 :
278 : /* write decoded RE8 vector to decoded subvector #i */
279 106007175 : for ( j = 0; j < 8; j++ )
280 : {
281 94228600 : xriq[i * 8 + j] = code[j];
282 : }
283 : }
284 :
285 1438210 : *nb_bits = bits;
286 :
287 1438210 : 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 23401429 : 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 23401429 : pos = Nsv - 1;
309 :
310 70204287 : for ( l = 0; l < Nsv; l++ )
311 : {
312 46802858 : pos += indx[l];
313 : }
314 :
315 : /* decode all subvectors */
316 70204287 : for ( l = Nsv - 1; l >= 0; l-- )
317 : {
318 46802858 : nq = indx[l]; /* quantizer number (0,2,3..n) */
319 :
320 46802858 : nk = 0;
321 46802858 : n = nq;
322 :
323 46802858 : if ( nq > 4 )
324 : {
325 3023475 : nk = ( nq - 3 ) >> 1;
326 3023475 : n = nq - nk * 2;
327 : }
328 :
329 : /* read n groups of 4-bit for Voronoi index (k[]) */
330 421225722 : for ( i = 0; i < 8; i++ )
331 : {
332 374422864 : kv[i] = 0;
333 : }
334 :
335 49895373 : while ( nk-- > 0 )
336 : {
337 3092515 : ival = ( indx[pos--] & 0x0F );
338 3092515 : ival <<= 4;
339 3092515 : ival += ( indx[pos--] & 0x0F );
340 :
341 27832635 : for ( i = 7; i >= 0; i-- )
342 : {
343 24740120 : kv[i] <<= 1;
344 24740120 : kv[i] += ( ival & 0x01 );
345 24740120 : ival >>= 1;
346 : }
347 : }
348 :
349 : /* read n groups of 4-bit for base codebook index (I) */
350 46802858 : I = 0;
351 168106558 : while ( n-- > 0 )
352 : {
353 121303700 : I <<= 4;
354 121303700 : I += ( indx[pos--] & 0x0F );
355 : }
356 :
357 : /* multi-rate RE8 decoder */
358 46802858 : re8_dec( nq, (int16_t) I, kv, c );
359 :
360 : /* write decoded RE8 vector */
361 421225722 : for ( i = 0; i < 8; i++ )
362 : {
363 374422864 : nvecq[( l * 8 ) + i] = c[i];
364 : }
365 : }
366 :
367 23401429 : return;
368 : }
369 :
370 :
371 : /*-----------------------------------------------------------------*
372 : * read_cv()
373 : *
374 : * read codebook indices (rank I and event. Voronoi index kv)
375 : *-----------------------------------------------------------------*/
376 :
377 11387810 : 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 11387810 : bits = *nbits;
389 :
390 : /* read codebook indices (rank I and event. Voronoi index kv) */
391 11387810 : if ( nq == 0 ) /* Q0 */
392 : {
393 : /* nothing to read */
394 : }
395 8681922 : else if ( nq < 5 ) /* Q2, Q3, Q4 */
396 : {
397 8404632 : *I = get_next_indice( st, 4 * nq );
398 8404632 : bits -= ( 4 * nq );
399 : }
400 277290 : else if ( nq % 2 == 0 ) /* Q4 + Voronoi extensions r=1,2,3,... */
401 : {
402 108601 : *I = get_next_indice( st, 4 * 4 );
403 108601 : bits -= 4 * 4;
404 108601 : order_v = (int16_t) ( nq / 2 ) - 2;
405 :
406 977409 : for ( j = 0; j < 8; j++ )
407 : {
408 868808 : kv[j] = get_next_indice( st, order_v );
409 : }
410 108601 : bits -= 8 * order_v;
411 : }
412 : else /* Q3 + Voronoi extensions r=1,2,3,... */
413 : {
414 168689 : *I = get_next_indice( st, 4 * 3 );
415 168689 : bits -= 4 * 3;
416 168689 : order_v = (int16_t) ( nq / 2 ) - 1;
417 :
418 1518201 : for ( j = 0; j < 8; j++ )
419 : {
420 1349512 : kv[j] = get_next_indice( st, order_v );
421 : }
422 168689 : bits -= 8 * order_v;
423 : }
424 11387810 : *nbits = bits;
425 :
426 11387810 : return;
427 : }
|