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 864730 : 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 864730 : kv = xriq; /* reuse vector to save memory */
81 864730 : bits = *nb_bits;
82 864730 : bitsMod = 0;
83 864730 : underflow = 0;
84 864730 : unusedbitsFlag = 0;
85 864730 : nullVec = 0;
86 864730 : dummy_bits = 0;
87 864730 : svOrder[Nsv - 1] = trgtSvPos;
88 864730 : svOrder[0] = 0;
89 864730 : svOrder[1] = 1;
90 864730 : i = 2;
91 864730 : j = i;
92 864730 : if ( avq_bit_sFlag == 2 )
93 : {
94 270515 : j = i + 1;
95 : }
96 :
97 5224298 : while ( i < Nsv - 1 )
98 : {
99 4359568 : svOrder[i] = j;
100 4359568 : i++;
101 4359568 : j++;
102 : }
103 :
104 30265550 : for ( i = 0; i < NSV_MAX; i++ )
105 : {
106 29400820 : I[i] = (uint16_t) -1;
107 : }
108 :
109 7276289 : for ( i = 0; i < Nsv; i++ )
110 : {
111 6953098 : k = svOrder[i];
112 6953098 : if ( avq_bit_sFlag == 2 && bits % 5 == 4 && bits > 8 && bits < 30 && k >= trgtSvPos && i < Nsv - 1 )
113 : {
114 42996 : ordr_esti( Nsv - i, &trgtSvPos, &svOrder[i], Nsv );
115 42996 : k = svOrder[i];
116 42996 : avq_bit_sFlag = 1;
117 : }
118 :
119 6953098 : if ( k == trgtSvPos && avq_bit_sFlag > 0 )
120 : {
121 832120 : if ( ( *nb_bits - bits ) == 7 || bits < BIT_SAVING_LOW_THR || bits >= BIT_SAVING_HIGH_THR )
122 : {
123 290581 : avq_bit_sFlag = 0;
124 : }
125 : else
126 : {
127 : break;
128 : }
129 : }
130 :
131 6411559 : nq[k] = 0; /* initialization and also forced if the budget is exceeded */
132 :
133 6411559 : if ( bits > 8 )
134 : {
135 : /* read the unary code including the stop bit for nq[i] */
136 6095294 : nq[k] = -1;
137 : do
138 : {
139 12461291 : ( nq[k] )++;
140 :
141 12461291 : if ( 5 * nq[k] + 4 == bits )
142 : {
143 71436 : break;
144 : }
145 12389855 : } while ( get_next_indice_1( st ) );
146 :
147 6095294 : if ( 5 * nq[k] + 4 == bits ) /* check the overflow */
148 : {
149 71436 : bits++; /* overflow stop bit */
150 : }
151 :
152 : /* check for potential bit errors */
153 6095294 : 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 6095294 : bits -= nq[k];
164 6095294 : bits--; /* count the stop bit */
165 :
166 6095294 : if ( nq[k] > 0 )
167 : {
168 4313778 : nq[k]++;
169 : }
170 :
171 : /* read codebook indices (rank I and event. Voronoi index kv) */
172 6095294 : read_cv( st, &I[k], &kv[k * 8], nq[k], &bits );
173 : }
174 : } /* for */
175 :
176 : /* Bit Saving Solution */
177 864730 : if ( avq_bit_sFlag > 0 && bits > 8 )
178 : {
179 541539 : i = svOrder[Nsv - 1];
180 541539 : nq[i] = 0;
181 541539 : bitsMod = bits % 5;
182 541539 : if ( i != Nsv - 1 )
183 : {
184 192851 : nullVec = 0;
185 1157106 : for ( j = i; j < Nsv - 1; j++ )
186 : {
187 964255 : if ( nq[svOrder[j]] == 0 )
188 : {
189 157602 : nullVec++;
190 : }
191 : }
192 192851 : nq_est = bits / 5;
193 192851 : 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 2691 : dummy_bits = 5 - bitsMod;
196 2691 : bits = bits + dummy_bits; /* add dummy bits */
197 2691 : bitsMod = 0;
198 : }
199 190160 : 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 957 : underflow = 0;
202 957 : if ( ( bitsMod + nullVec ) % 5 != 0 )
203 : {
204 636 : underflow = 1;
205 : }
206 957 : dummy_bits = nullVec + underflow;
207 957 : bits = bits + dummy_bits; /* add dummy bits */
208 957 : bitsMod = 0;
209 : }
210 : }
211 541539 : underflow = 1;
212 541539 : if ( bitsMod != 4 )
213 : {
214 453422 : underflow = 0;
215 453422 : bits -= bitsMod;
216 : }
217 541539 : bits += underflow;
218 :
219 : /* read the unary code for unused bit*/
220 541539 : unused_bits_idx = -1;
221 : do
222 : {
223 756913 : ( unused_bits_idx )++;
224 756913 : if ( 5 * unused_bits_idx + 4 == bits - 1 )
225 : {
226 4898 : break;
227 : }
228 752015 : } while ( get_next_indice_1( st ) );
229 :
230 541539 : unusedbitsFlag = 0;
231 :
232 541539 : if ( dummy_bits == 0 )
233 : {
234 537891 : if ( unused_bits_idx == 0 && bits > BIT_SAVING_LOW_THR )
235 : {
236 246083 : unusedbitsFlag = 1;
237 : }
238 291808 : else if ( unused_bits_idx == 1 && bits > BIT_SAVING_LOW_THR )
239 : {
240 106253 : unusedbitsFlag = -1;
241 : }
242 : }
243 :
244 : /*Compute AVQ code book number from unused Bits */
245 541539 : nq_est = (int16_t) ceil( 0.2f * ( bits - 5 * ( unusedbitsFlag + unused_bits_idx ) ) );
246 :
247 541539 : if ( nq_est == 1 )
248 : {
249 4898 : nq_est = 0;
250 : }
251 541539 : bits -= underflow;
252 :
253 541539 : bits -= unused_bits_idx;
254 :
255 541539 : if ( nq_est != 0 )
256 : {
257 536641 : bits--;
258 : }
259 :
260 541539 : nq[i] = nq_est;
261 : /* read codebook indices (rank I and event. Voronoi index kv) */
262 541539 : read_cv( st, &I[i], &kv[i * 8], nq[i], &bits );
263 :
264 541539 : bits -= dummy_bits;
265 :
266 541539 : if ( bitsMod != 4 )
267 : {
268 453422 : bits += bitsMod;
269 : }
270 : }
271 :
272 : /* decode all subvectors */
273 7817828 : for ( i = 0; i < Nsv; i++ )
274 : {
275 : /* multi-rate RE8 decoder */
276 6953098 : re8_dec( nq[i], I[i], &kv[8 * i], code );
277 :
278 : /* write decoded RE8 vector to decoded subvector #i */
279 62577882 : for ( j = 0; j < 8; j++ )
280 : {
281 55624784 : xriq[i * 8 + j] = code[j];
282 : }
283 : }
284 :
285 864730 : *nb_bits = bits;
286 :
287 864730 : 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 10666654 : 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 10666654 : pos = Nsv - 1;
309 :
310 31999962 : for ( l = 0; l < Nsv; l++ )
311 : {
312 21333308 : pos += indx[l];
313 : }
314 :
315 : /* decode all subvectors */
316 31999962 : for ( l = Nsv - 1; l >= 0; l-- )
317 : {
318 21333308 : nq = indx[l]; /* quantizer number (0,2,3..n) */
319 :
320 21333308 : nk = 0;
321 21333308 : n = nq;
322 :
323 21333308 : if ( nq > 4 )
324 : {
325 1216885 : nk = ( nq - 3 ) >> 1;
326 1216885 : n = nq - nk * 2;
327 : }
328 :
329 : /* read n groups of 4-bit for Voronoi index (k[]) */
330 191999772 : for ( i = 0; i < 8; i++ )
331 : {
332 170666464 : kv[i] = 0;
333 : }
334 :
335 22620636 : while ( nk-- > 0 )
336 : {
337 1287328 : ival = ( indx[pos--] & 0x0F );
338 1287328 : ival <<= 4;
339 1287328 : ival += ( indx[pos--] & 0x0F );
340 :
341 11585952 : for ( i = 7; i >= 0; i-- )
342 : {
343 10298624 : kv[i] <<= 1;
344 10298624 : kv[i] += ( ival & 0x01 );
345 10298624 : ival >>= 1;
346 : }
347 : }
348 :
349 : /* read n groups of 4-bit for base codebook index (I) */
350 21333308 : I = 0;
351 78545662 : while ( n-- > 0 )
352 : {
353 57212354 : I <<= 4;
354 57212354 : I += ( indx[pos--] & 0x0F );
355 : }
356 :
357 : /* multi-rate RE8 decoder */
358 21333308 : re8_dec( nq, (int16_t) I, kv, c );
359 :
360 : /* write decoded RE8 vector */
361 191999772 : for ( i = 0; i < 8; i++ )
362 : {
363 170666464 : nvecq[( l * 8 ) + i] = c[i];
364 : }
365 : }
366 :
367 10666654 : return;
368 : }
369 :
370 :
371 : /*-----------------------------------------------------------------*
372 : * read_cv()
373 : *
374 : * read codebook indices (rank I and event. Voronoi index kv)
375 : *-----------------------------------------------------------------*/
376 :
377 6636833 : 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 6636833 : bits = *nbits;
389 :
390 : /* read codebook indices (rank I and event. Voronoi index kv) */
391 6636833 : if ( nq == 0 ) /* Q0 */
392 : {
393 : /* nothing to read */
394 : }
395 4850419 : else if ( nq < 5 ) /* Q2, Q3, Q4 */
396 : {
397 4753787 : *I = get_next_indice( st, 4 * nq );
398 4753787 : bits -= ( 4 * nq );
399 : }
400 96632 : else if ( nq % 2 == 0 ) /* Q4 + Voronoi extensions r=1,2,3,... */
401 : {
402 44697 : *I = get_next_indice( st, 4 * 4 );
403 44697 : bits -= 4 * 4;
404 44697 : order_v = (int16_t) ( nq / 2 ) - 2;
405 :
406 402273 : for ( j = 0; j < 8; j++ )
407 : {
408 357576 : kv[j] = get_next_indice( st, order_v );
409 : }
410 44697 : bits -= 8 * order_v;
411 : }
412 : else /* Q3 + Voronoi extensions r=1,2,3,... */
413 : {
414 51935 : *I = get_next_indice( st, 4 * 3 );
415 51935 : bits -= 4 * 3;
416 51935 : order_v = (int16_t) ( nq / 2 ) - 1;
417 :
418 467415 : for ( j = 0; j < 8; j++ )
419 : {
420 415480 : kv[j] = get_next_indice( st, order_v );
421 : }
422 51935 : bits -= 8 * order_v;
423 : }
424 6636833 : *nbits = bits;
425 :
426 6636833 : return;
427 : }
|