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 223314 : 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 223314 : kv = xriq; /* reuse vector to save memory */
81 223314 : bits = *nb_bits;
82 223314 : bitsMod = 0;
83 223314 : underflow = 0;
84 223314 : unusedbitsFlag = 0;
85 223314 : nullVec = 0;
86 223314 : dummy_bits = 0;
87 223314 : svOrder[Nsv - 1] = trgtSvPos;
88 223314 : svOrder[0] = 0;
89 223314 : svOrder[1] = 1;
90 223314 : i = 2;
91 223314 : j = i;
92 223314 : if ( avq_bit_sFlag == 2 )
93 : {
94 59265 : j = i + 1;
95 : }
96 :
97 1365084 : while ( i < Nsv - 1 )
98 : {
99 1141770 : svOrder[i] = j;
100 1141770 : i++;
101 1141770 : j++;
102 : }
103 :
104 7815990 : for ( i = 0; i < NSV_MAX; i++ )
105 : {
106 7592676 : I[i] = (uint16_t) -1;
107 : }
108 :
109 1897599 : for ( i = 0; i < Nsv; i++ )
110 : {
111 1811268 : k = svOrder[i];
112 1811268 : if ( avq_bit_sFlag == 2 && bits % 5 == 4 && bits > 8 && bits < 30 && k >= trgtSvPos && i < Nsv - 1 )
113 : {
114 8748 : ordr_esti( Nsv - i, &trgtSvPos, &svOrder[i], Nsv );
115 8748 : k = svOrder[i];
116 8748 : avq_bit_sFlag = 1;
117 : }
118 :
119 1811268 : if ( k == trgtSvPos && avq_bit_sFlag > 0 )
120 : {
121 208920 : if ( ( *nb_bits - bits ) == 7 || bits < BIT_SAVING_LOW_THR || bits >= BIT_SAVING_HIGH_THR )
122 : {
123 71937 : avq_bit_sFlag = 0;
124 : }
125 : else
126 : {
127 : break;
128 : }
129 : }
130 :
131 1674285 : nq[k] = 0; /* initialization and also forced if the budget is exceeded */
132 :
133 1674285 : if ( bits > 8 )
134 : {
135 : /* read the unary code including the stop bit for nq[i] */
136 1588428 : nq[k] = -1;
137 : do
138 : {
139 3294978 : ( nq[k] )++;
140 :
141 3294978 : if ( 5 * nq[k] + 4 == bits )
142 : {
143 19098 : break;
144 : }
145 3275880 : } while ( get_next_indice_1( st ) );
146 :
147 1588428 : if ( 5 * nq[k] + 4 == bits ) /* check the overflow */
148 : {
149 19098 : bits++; /* overflow stop bit */
150 : }
151 :
152 : /* check for potential bit errors */
153 1588428 : 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 1588428 : bits -= nq[k];
164 1588428 : bits--; /* count the stop bit */
165 :
166 1588428 : if ( nq[k] > 0 )
167 : {
168 1169946 : nq[k]++;
169 : }
170 :
171 : /* read codebook indices (rank I and event. Voronoi index kv) */
172 1588428 : read_cv( st, &I[k], &kv[k * 8], nq[k], &bits );
173 : }
174 : } /* for */
175 :
176 : /* Bit Saving Solution */
177 223314 : if ( avq_bit_sFlag > 0 && bits > 8 )
178 : {
179 136983 : i = svOrder[Nsv - 1];
180 136983 : nq[i] = 0;
181 136983 : bitsMod = bits % 5;
182 136983 : if ( i != Nsv - 1 )
183 : {
184 46371 : nullVec = 0;
185 278226 : for ( j = i; j < Nsv - 1; j++ )
186 : {
187 231855 : if ( nq[svOrder[j]] == 0 )
188 : {
189 31872 : nullVec++;
190 : }
191 : }
192 46371 : nq_est = bits / 5;
193 46371 : 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 309 : dummy_bits = 5 - bitsMod;
196 309 : bits = bits + dummy_bits; /* add dummy bits */
197 309 : bitsMod = 0;
198 : }
199 46062 : 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 12 : underflow = 0;
202 12 : if ( ( bitsMod + nullVec ) % 5 != 0 )
203 : {
204 12 : underflow = 1;
205 : }
206 12 : dummy_bits = nullVec + underflow;
207 12 : bits = bits + dummy_bits; /* add dummy bits */
208 12 : bitsMod = 0;
209 : }
210 : }
211 136983 : underflow = 1;
212 136983 : if ( bitsMod != 4 )
213 : {
214 114648 : underflow = 0;
215 114648 : bits -= bitsMod;
216 : }
217 136983 : bits += underflow;
218 :
219 : /* read the unary code for unused bit*/
220 136983 : unused_bits_idx = -1;
221 : do
222 : {
223 184422 : ( unused_bits_idx )++;
224 184422 : if ( 5 * unused_bits_idx + 4 == bits - 1 )
225 : {
226 666 : break;
227 : }
228 183756 : } while ( get_next_indice_1( st ) );
229 :
230 136983 : unusedbitsFlag = 0;
231 :
232 136983 : if ( dummy_bits == 0 )
233 : {
234 136662 : if ( unused_bits_idx == 0 && bits > BIT_SAVING_LOW_THR )
235 : {
236 62475 : unusedbitsFlag = 1;
237 : }
238 74187 : else if ( unused_bits_idx == 1 && bits > BIT_SAVING_LOW_THR )
239 : {
240 26112 : unusedbitsFlag = -1;
241 : }
242 : }
243 :
244 : /*Compute AVQ code book number from unused Bits */
245 136983 : nq_est = (int16_t) ceil( 0.2f * ( bits - 5 * ( unusedbitsFlag + unused_bits_idx ) ) );
246 :
247 136983 : if ( nq_est == 1 )
248 : {
249 666 : nq_est = 0;
250 : }
251 136983 : bits -= underflow;
252 :
253 136983 : bits -= unused_bits_idx;
254 :
255 136983 : if ( nq_est != 0 )
256 : {
257 136317 : bits--;
258 : }
259 :
260 136983 : nq[i] = nq_est;
261 : /* read codebook indices (rank I and event. Voronoi index kv) */
262 136983 : read_cv( st, &I[i], &kv[i * 8], nq[i], &bits );
263 :
264 136983 : bits -= dummy_bits;
265 :
266 136983 : if ( bitsMod != 4 )
267 : {
268 114648 : bits += bitsMod;
269 : }
270 : }
271 :
272 : /* decode all subvectors */
273 2034582 : for ( i = 0; i < Nsv; i++ )
274 : {
275 : /* multi-rate RE8 decoder */
276 1811268 : re8_dec( nq[i], I[i], &kv[8 * i], code );
277 :
278 : /* write decoded RE8 vector to decoded subvector #i */
279 16301412 : for ( j = 0; j < 8; j++ )
280 : {
281 14490144 : xriq[i * 8 + j] = code[j];
282 : }
283 : }
284 :
285 223314 : *nb_bits = bits;
286 :
287 223314 : 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 1895844 : 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 1895844 : pos = Nsv - 1;
309 :
310 5687532 : for ( l = 0; l < Nsv; l++ )
311 : {
312 3791688 : pos += indx[l];
313 : }
314 :
315 : /* decode all subvectors */
316 5687532 : for ( l = Nsv - 1; l >= 0; l-- )
317 : {
318 3791688 : nq = indx[l]; /* quantizer number (0,2,3..n) */
319 :
320 3791688 : nk = 0;
321 3791688 : n = nq;
322 :
323 3791688 : if ( nq > 4 )
324 : {
325 247797 : nk = ( nq - 3 ) >> 1;
326 247797 : n = nq - nk * 2;
327 : }
328 :
329 : /* read n groups of 4-bit for Voronoi index (k[]) */
330 34125192 : for ( i = 0; i < 8; i++ )
331 : {
332 30333504 : kv[i] = 0;
333 : }
334 :
335 4042875 : while ( nk-- > 0 )
336 : {
337 251187 : ival = ( indx[pos--] & 0x0F );
338 251187 : ival <<= 4;
339 251187 : ival += ( indx[pos--] & 0x0F );
340 :
341 2260683 : for ( i = 7; i >= 0; i-- )
342 : {
343 2009496 : kv[i] <<= 1;
344 2009496 : kv[i] += ( ival & 0x01 );
345 2009496 : ival >>= 1;
346 : }
347 : }
348 :
349 : /* read n groups of 4-bit for base codebook index (I) */
350 3791688 : I = 0;
351 13883334 : while ( n-- > 0 )
352 : {
353 10091646 : I <<= 4;
354 10091646 : I += ( indx[pos--] & 0x0F );
355 : }
356 :
357 : /* multi-rate RE8 decoder */
358 3791688 : re8_dec( nq, (int16_t) I, kv, c );
359 :
360 : /* write decoded RE8 vector */
361 34125192 : for ( i = 0; i < 8; i++ )
362 : {
363 30333504 : nvecq[( l * 8 ) + i] = c[i];
364 : }
365 : }
366 :
367 1895844 : return;
368 : }
369 :
370 :
371 : /*-----------------------------------------------------------------*
372 : * read_cv()
373 : *
374 : * read codebook indices (rank I and event. Voronoi index kv)
375 : *-----------------------------------------------------------------*/
376 :
377 1725411 : 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 1725411 : bits = *nbits;
389 :
390 : /* read codebook indices (rank I and event. Voronoi index kv) */
391 1725411 : if ( nq == 0 ) /* Q0 */
392 : {
393 : /* nothing to read */
394 : }
395 1306263 : else if ( nq < 5 ) /* Q2, Q3, Q4 */
396 : {
397 1291965 : *I = get_next_indice( st, 4 * nq );
398 1291965 : bits -= ( 4 * nq );
399 : }
400 14298 : else if ( nq % 2 == 0 ) /* Q4 + Voronoi extensions r=1,2,3,... */
401 : {
402 5055 : *I = get_next_indice( st, 4 * 4 );
403 5055 : bits -= 4 * 4;
404 5055 : order_v = (int16_t) ( nq / 2 ) - 2;
405 :
406 45495 : for ( j = 0; j < 8; j++ )
407 : {
408 40440 : kv[j] = get_next_indice( st, order_v );
409 : }
410 5055 : bits -= 8 * order_v;
411 : }
412 : else /* Q3 + Voronoi extensions r=1,2,3,... */
413 : {
414 9243 : *I = get_next_indice( st, 4 * 3 );
415 9243 : bits -= 4 * 3;
416 9243 : order_v = (int16_t) ( nq / 2 ) - 1;
417 :
418 83187 : for ( j = 0; j < 8; j++ )
419 : {
420 73944 : kv[j] = get_next_indice( st, order_v );
421 : }
422 9243 : bits -= 8 * order_v;
423 : }
424 1725411 : *nbits = bits;
425 :
426 1725411 : return;
427 : }
|