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 <math.h>
43 : #include "prot.h"
44 : #include "rom_com.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * Local prototypes
49 : *-------------------------------------------------------------------*/
50 :
51 : static void wrte_cv( BSTR_ENC_HANDLE hBstr, const int16_t nq, const int16_t i_ind, const int16_t kv_ind, uint16_t I, int16_t kv[], int16_t *bits );
52 :
53 : /*-------------------------------------------------------------------*
54 : * Function AVQ_cod() *
55 : * *
56 : * Split algebraic vector quantizer (AVQ) based on RE8 latice *
57 : *-------------------------------------------------------------------*/
58 :
59 : /*! r: comfort noise gain factor */
60 616470 : float AVQ_cod(
61 : const float xri[], /* i : vector to quantize */
62 : int16_t xriq[], /* o : quantized normalized vector (assuming the bit budget is enough) */
63 : const int16_t nb_bits, /* i : number of allocated bits */
64 : const int16_t Nsv /* i : number of subvectors (lg=Nsv*8) */
65 : )
66 : {
67 : int16_t i, j, iter;
68 : int16_t c[8];
69 : float gain_inv, x1[8], ener, tmp, nbits, nbits_max, fac, offset;
70 : float ebits[NSV_MAX];
71 :
72 : /* find energy of each subvector in log domain (scaled for bits estimation) */
73 5665449 : for ( i = 0; i < Nsv; i++ )
74 : {
75 5048979 : ener = 2.0f; /* to set ebits >= 0 */
76 45440811 : for ( j = 0; j < 8; j++ )
77 : {
78 40391832 : x1[j] = xri[i * 8 + j];
79 40391832 : ener += x1[j] * x1[j];
80 : }
81 :
82 : /* estimated bit consumption when gain=1 */
83 5048979 : ebits[i] = 5.0f * FAC_LOG2 * (float) log10( ener * 0.5f );
84 : }
85 :
86 : /* estimate gain according to number of bits allowed */
87 616470 : fac = 128.0f; /* start at the middle (offset range = 0 to 255.75) */
88 616470 : offset = 0.0f;
89 616470 : nbits_max = 0.95f * ( (float) ( nb_bits - Nsv ) );
90 :
91 : /* tree search with 10 iterations : offset with step of 0.25 bits (0.3 dB) */
92 6781170 : for ( iter = 0; iter < 10; iter++ )
93 : {
94 6164700 : offset += fac;
95 : /* calculate the required number of bits */
96 6164700 : nbits = 0.0;
97 56654490 : for ( i = 0; i < Nsv; i++ )
98 : {
99 50489790 : tmp = ebits[i] - offset;
100 50489790 : if ( tmp < 0.0 )
101 : {
102 13800286 : tmp = 0.0;
103 : }
104 50489790 : nbits += tmp;
105 : }
106 : /* decrease gain when no overflow occurs */
107 6164700 : if ( nbits <= nbits_max )
108 : {
109 3608653 : offset -= fac;
110 : }
111 6164700 : fac *= 0.5;
112 : }
113 :
114 : /* estimated gain (when offset=0, estimated gain=1) */
115 616470 : gain_inv = 1.0f / (float) pow( 10.0f, (float) ( offset / ( 2.0f * 5.0f * FAC_LOG2 ) ) );
116 :
117 : /* quantize all subvector using estimated gain */
118 5665449 : for ( i = 0; i < Nsv; i++ )
119 : {
120 45440811 : for ( j = 0; j < 8; j++ )
121 : {
122 40391832 : x1[j] = xri[i * 8 + j] * gain_inv;
123 : }
124 :
125 5048979 : re8_PPV( x1, c );
126 45440811 : for ( j = 0; j < 8; j++ )
127 : {
128 40391832 : xriq[i * 8 + j] = c[j];
129 : }
130 : }
131 :
132 616470 : fac = 0;
133 :
134 : /* round bit allocations and save */
135 5665449 : for ( i = 0; i < Nsv; i++ )
136 : {
137 5048979 : xriq[( Nsv * 8 ) + i] = (int16_t) floor( ebits[i] * 128.0f );
138 : }
139 :
140 616470 : return ( fac );
141 : }
142 :
143 :
144 : /*-----------------------------------------------------------------*
145 : * AVQ_encmux()
146 : *
147 : * Encode subvectors and write indexes into the bitstream
148 : *-----------------------------------------------------------------*/
149 :
150 616470 : void AVQ_encmux(
151 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
152 : const int16_t extl, /* i : extension layer */
153 : int16_t xriq[], /* i/o: rounded subvectors [0..8*Nsv-1] followedby rounded bit allocations [8*Nsv..8*Nsv+Nsv-1]*/
154 : int16_t *nb_bits, /* i/o: number of allocated bits */
155 : const int16_t Nsv, /* i : number of subvectors */
156 : int16_t nq[], /* o : AVQ nq index */
157 : int16_t avq_bit_sFlag, /* i : flag for AVQ bit saving solution */
158 : int16_t trgtSvPos /* i : target SV for AVQ bit savings */
159 : )
160 : {
161 616470 : int16_t i, j = 0, bits, pos, pos_max, overflow;
162 : int16_t sort_idx[NSV_MAX];
163 : int16_t *t, kv[NSV_MAX * 8];
164 : uint16_t I[NSV_MAX];
165 : int16_t nq_ind, i_ind, kv_ind;
166 : int16_t nq_est, unused_bits, unused_bits_idx;
167 : int16_t bitsMod;
168 : int16_t unusedbitsFlag;
169 : int16_t svOrder[NSV_MAX], k, nullVec, dummy_bits;
170 :
171 616470 : if ( extl == SWB_BWE_HIGHRATE || extl == FB_BWE_HIGHRATE )
172 : {
173 19245 : nq_ind = IND_NQ2;
174 19245 : i_ind = IND_I2;
175 19245 : kv_ind = IND_KV2;
176 : }
177 : else
178 : {
179 597225 : nq_ind = IND_NQ;
180 597225 : i_ind = IND_I;
181 597225 : kv_ind = IND_KV;
182 : }
183 :
184 616470 : unusedbitsFlag = 0;
185 616470 : bitsMod = 0;
186 :
187 21576450 : for ( i = 0; i < NSV_MAX; i++ )
188 : {
189 20959980 : I[i] = (uint16_t) -1;
190 : }
191 :
192 : /*-----------------------------------------------------------------
193 : * Encode subvectors and fix possible overflows in total bit budget,
194 : * i.e. find for each subvector a codebook index nq (nq=0,2,3,4,...,NSV_MAX),
195 : * a base codebook index (I), and a Voronoi index (kv)
196 : *-----------------------------------------------------------------*/
197 :
198 : /* sort subvectors by estimated bit allocations in decreasing order */
199 616470 : t = kv; /* reuse vector to save memory */
200 5665449 : for ( i = 0; i < Nsv; i++ )
201 : {
202 5048979 : t[i] = xriq[8 * Nsv + i];
203 : }
204 :
205 5665449 : for ( i = 0; i < Nsv; i++ )
206 : {
207 5048979 : bits = t[0];
208 5048979 : pos = 0;
209 44030755 : for ( j = 1; j < Nsv; j++ )
210 : {
211 38981776 : if ( t[j] > bits )
212 : {
213 7742758 : bits = t[j];
214 7742758 : pos = j;
215 : }
216 : }
217 5048979 : sort_idx[i] = pos;
218 5048979 : t[pos] = -1;
219 : }
220 :
221 : /* compute multi-rate indices and avoid bit budget overflow */
222 616470 : pos_max = 0;
223 616470 : bits = 0;
224 5665449 : for ( i = 0; i < Nsv; i++ )
225 : {
226 : /* find vector to quantize (criteria: nb of estimated bits) */
227 5048979 : pos = sort_idx[i];
228 :
229 : /* compute multi-rate index of rounded subvector (nq,I,kv[]) */
230 5048979 : re8_cod( &xriq[pos * 8], &nq[pos], &I[pos], &kv[8 * pos] );
231 :
232 5048979 : if ( nq[pos] > 0 )
233 : {
234 4232083 : j = pos_max;
235 4232083 : if ( pos > j )
236 : {
237 1673581 : j = pos;
238 : }
239 :
240 : /* compute (number of bits -1) to describe Q #nq */
241 4232083 : if ( nq[pos] >= 2 )
242 : {
243 4232083 : overflow = nq[pos] * 5 - 1;
244 : }
245 : else
246 : {
247 0 : overflow = 0;
248 : }
249 :
250 : /* check for overflow and compute number of bits-1 (n) */
251 4232083 : if ( ( bits + overflow + j ) > *nb_bits )
252 : {
253 : /* if budget overflow */
254 4699098 : for ( j = pos * 8; j < ( pos * 8 ) + 8; j++ )
255 : {
256 4176976 : xriq[j] = 0;
257 : }
258 522122 : nq[pos] = 0; /* force Q0 */
259 : }
260 : else
261 : {
262 3709961 : bits += overflow;
263 3709961 : pos_max = j; /* update index of the last described subvector */
264 : }
265 : }
266 : }
267 :
268 616470 : nullVec = 0;
269 616470 : dummy_bits = 0;
270 616470 : svOrder[Nsv - 1] = trgtSvPos;
271 616470 : svOrder[0] = 0;
272 616470 : svOrder[1] = 1;
273 616470 : i = 2;
274 616470 : j = i;
275 616470 : if ( avq_bit_sFlag == 2 )
276 : {
277 247827 : j = i + 1;
278 : }
279 3818885 : while ( i < Nsv - 1 )
280 : {
281 3202415 : svOrder[i] = j;
282 3202415 : i++;
283 3202415 : j++;
284 : }
285 :
286 : /* write indexes to the bitstream */
287 : /* ============================== */
288 :
289 616470 : bits = *nb_bits;
290 616470 : overflow = 0;
291 5309420 : for ( i = 0; i < Nsv; i++ )
292 : {
293 5048979 : k = svOrder[i];
294 5048979 : if ( avq_bit_sFlag == 2 && bits % 5 == 4 && bits > 8 && bits < 30 && k >= trgtSvPos && i < Nsv - 1 )
295 : {
296 32373 : ordr_esti( Nsv - i, &trgtSvPos, &svOrder[i], Nsv );
297 32373 : k = svOrder[i];
298 32373 : avq_bit_sFlag = 1;
299 : }
300 :
301 5048979 : if ( k == trgtSvPos && avq_bit_sFlag > 0 )
302 : {
303 521485 : if ( ( *nb_bits - bits ) == 7 || bits < BIT_SAVING_LOW_THR || bits >= BIT_SAVING_HIGH_THR )
304 : {
305 165456 : avq_bit_sFlag = 0;
306 : }
307 : else
308 : {
309 : break;
310 : }
311 : }
312 :
313 4692950 : if ( 5 * nq[k] - 1 == bits ) /* check the overflow */
314 : {
315 43180 : overflow = 1;
316 : }
317 :
318 4692950 : if ( bits > 8 )
319 : {
320 : /* write the unary code for nq[i] */
321 4511932 : j = nq[k] - 1;
322 4511932 : if ( nq[k] > 0 )
323 : {
324 : /* write the unary code */
325 3361318 : while ( j > 16 )
326 : {
327 0 : push_indice( hBstr, nq_ind, 65535, 16 );
328 0 : bits -= 16;
329 0 : j -= 16;
330 : }
331 :
332 3361318 : if ( j > 0 )
333 : {
334 3361318 : push_indice( hBstr, nq_ind, ( 1 << j ) - 1, j );
335 3361318 : bits -= j;
336 : }
337 : }
338 :
339 4511932 : if ( !overflow )
340 : {
341 : /* write the stop bit */
342 4468752 : push_indice( hBstr, nq_ind, 0, 1 );
343 4468752 : bits--;
344 : }
345 :
346 : /* write codebook indices (rank I and event. Voronoi index kv) */
347 4511932 : wrte_cv( hBstr, nq[k], i_ind, kv_ind, I[k], &kv[k * 8], &bits );
348 : }
349 : } /* for */
350 :
351 : /* Bit Saving Solution */
352 616470 : if ( avq_bit_sFlag > 0 && bits > 8 )
353 : {
354 356029 : bitsMod = bits % 5;
355 356029 : i = svOrder[Nsv - 1];
356 356029 : if ( i != Nsv - 1 )
357 : {
358 178690 : nullVec = 0;
359 1072140 : for ( j = i; j < Nsv - 1; j++ )
360 : {
361 893450 : if ( nq[svOrder[j]] == 0 )
362 : {
363 128607 : nullVec++;
364 : }
365 : }
366 178690 : nq_est = bits / 5;
367 178690 : if ( ( bitsMod > 0 || ( nullVec == 4 && nq_est == 5 ) ) && bitsMod != 4 && ( bits + nullVec ) >= 5 * nq_est + 4 && nq[svOrder[Nsv - 2]] == 0 ) /* detect need for dummy bits */
368 : {
369 2704 : dummy_bits = 5 - bitsMod;
370 2704 : bits = bits + dummy_bits; /* add dummy bits */
371 2704 : bitsMod = 0;
372 : }
373 175986 : 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..*/
374 : {
375 462 : overflow = 0;
376 462 : if ( ( bitsMod + nullVec ) % 5 != 0 )
377 : {
378 326 : overflow = 1;
379 : }
380 462 : dummy_bits = nullVec + overflow;
381 462 : bits = bits + dummy_bits; /* add dummy bits */
382 462 : bitsMod = 0;
383 : }
384 : }
385 :
386 356029 : overflow = 1;
387 356029 : if ( bitsMod != 4 )
388 : {
389 296542 : overflow = 0;
390 296542 : bits -= bitsMod;
391 : }
392 356029 : bits = bits + overflow; /*add fake bit */
393 356029 : unused_bits = bits - 5 * nq[i];
394 356029 : if ( nq[i] == 0 ) /*no bit savings*/
395 : {
396 7386 : unused_bits--; /*Stop Bit*/
397 : }
398 356029 : unused_bits_idx = (int16_t) unused_bits / 5;
399 :
400 356029 : unusedbitsFlag = 0;
401 :
402 356029 : if ( dummy_bits == 0 )
403 : {
404 352863 : if ( unused_bits_idx == 1 && bits > BIT_SAVING_LOW_THR )
405 : {
406 156360 : unused_bits_idx = 0;
407 156360 : unusedbitsFlag = 1;
408 : }
409 196503 : else if ( unused_bits_idx == 0 && bits > BIT_SAVING_LOW_THR )
410 : {
411 82253 : unused_bits_idx = 1;
412 82253 : unusedbitsFlag = -1;
413 : }
414 : }
415 :
416 356029 : j = unused_bits_idx;
417 : /*Encode Unused Bit Unary Codeword */
418 356029 : if ( j > 0 )
419 : {
420 : /* write the unary code */
421 117621 : push_indice( hBstr, nq_ind, ( 1 << j ) - 1, j );
422 : }
423 :
424 356029 : if ( nq[i] != 0 )
425 : {
426 : /* write the stop bit */
427 348643 : push_indice( hBstr, nq_ind, 0, 1 );
428 : }
429 :
430 : /*Compute AVQ code book number from unused Bits */
431 356029 : nq_est = (int16_t) ceil( 0.2f * ( bits - 5 * ( unusedbitsFlag + unused_bits_idx ) ) );
432 :
433 356029 : if ( nq_est == 1 )
434 : {
435 7386 : nq_est = 0;
436 : }
437 356029 : bits -= overflow;
438 :
439 356029 : bits -= j;
440 :
441 356029 : if ( nq_est != 0 )
442 : {
443 348643 : bits--;
444 : }
445 356029 : nq[i] = nq_est;
446 :
447 : /* write codebook indices (rank I and event. Voronoi index kv) */
448 356029 : wrte_cv( hBstr, nq[i], i_ind, kv_ind, I[i], &kv[i * 8], &bits );
449 :
450 356029 : bits -= dummy_bits;
451 :
452 356029 : if ( bitsMod != 4 )
453 : {
454 296542 : bits += bitsMod;
455 : }
456 : }
457 :
458 :
459 616470 : *nb_bits = bits;
460 :
461 616470 : return;
462 : }
463 :
464 :
465 : /*-------------------------------------------------------------------*
466 : * Function AVQ_cod_lpc() *
467 : * *
468 : * Split algebraic vector quantizer (AVQ) for LPC quantization *
469 : *-------------------------------------------------------------------*/
470 :
471 9739486 : void AVQ_cod_lpc(
472 : const float nvec[], /* i : vector to quantize */
473 : int16_t nvecq[], /* o : quantized normalized vector (assuming the bit budget is enough) */
474 : int16_t *indx, /* o : index[] (4 bits per words) */
475 : const int16_t Nsv /* i : number of subvectors (lg=Nsv*8) */
476 : )
477 : {
478 : int16_t nq, c[8];
479 : int16_t i, l, n, nk, pos, ival, kv[8];
480 : float x1[8];
481 : uint16_t I;
482 :
483 : /* quantize all subvector using estimated gain */
484 9739486 : pos = Nsv;
485 :
486 29218458 : for ( l = 0; l < Nsv; l++ )
487 : {
488 175310748 : for ( i = 0; i < 8; i++ )
489 : {
490 155831776 : x1[i] = nvec[l * 8 + i];
491 : }
492 :
493 19478972 : re8_PPV( x1, c );
494 :
495 19478972 : re8_cod( c, &nq, &I, kv );
496 :
497 175310748 : for ( i = 0; i < 8; i++ )
498 : {
499 155831776 : nvecq[l * 8 + i] = c[i];
500 : }
501 :
502 19478972 : indx[l] = nq; /* index[0..Nsv-1] = quantizer number (0,2,3,4...) */
503 :
504 19478972 : nk = 0;
505 19478972 : n = nq;
506 :
507 19478972 : if ( nq > 4 )
508 : {
509 1245881 : nk = ( nq - 3 ) >> 1;
510 1245881 : n = nq - nk * 2;
511 : }
512 :
513 : /* write n groups of 4-bit for base codebook index (I) */
514 67658530 : while ( n-- > 0 )
515 : {
516 48179558 : indx[pos++] = ( I & 0x0F );
517 48179558 : I >>= 4;
518 : }
519 :
520 : /* write n groups of 4-bit for Voronoi index (k[]) */
521 20760506 : while ( nk-- > 0 )
522 : {
523 1281534 : ival = 0;
524 :
525 11533806 : for ( i = 0; i < 8; i++ )
526 : {
527 10252272 : ival <<= 1;
528 10252272 : ival += ( kv[i] & 0x01 );
529 10252272 : kv[i] >>= 1;
530 : }
531 1281534 : indx[pos++] = ( ival & 0x0F );
532 1281534 : ival >>= 4;
533 1281534 : indx[pos++] = ( ival & 0x0F );
534 : }
535 : }
536 :
537 9739486 : return;
538 : }
539 :
540 : /*-------------------------------------------------------------------*
541 : * Function wrte_cv() *
542 : * *
543 : * write codebook indices (rank I and event. Voronoi index kv) *
544 : *-------------------------------------------------------------------*/
545 :
546 4867961 : static void wrte_cv(
547 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
548 : const int16_t nq, /* i : AVQ nq index */
549 : const int16_t i_ind, /* i : Base Bitstream index */
550 : const int16_t kv_ind, /* i : Vornoi Bitstream index */
551 : uint16_t I, /* o : rank I code book index */
552 : int16_t kv[], /* o : Vornoi index kv */
553 : int16_t *nbits /* i/o: bits */
554 : )
555 : {
556 : int16_t pos, j;
557 : int16_t bits;
558 :
559 4867961 : bits = *nbits;
560 :
561 : /* write codebook indices (rank I and event. Voronoi index kv) */
562 4867961 : if ( nq == 0 ) /* Q0 */
563 : {
564 : /* nothing to write */
565 : }
566 3709961 : else if ( nq < 5 ) /* Q2, Q3, Q4 */
567 : {
568 3580751 : push_indice( hBstr, i_ind, I, 4 * nq );
569 3580751 : bits -= ( 4 * nq );
570 : }
571 129210 : else if ( nq % 2 == 0 ) /* Q4 + Voronoi extensions r=1,2,3,... */
572 : {
573 51352 : push_indice( hBstr, i_ind, I, 4 * 4 );
574 51352 : bits -= 4 * 4;
575 51352 : pos = (int16_t) ( nq / 2 - 2 ); /* Voronoi order determination */
576 462168 : for ( j = 0; j < 8; j++ )
577 : {
578 410816 : push_indice( hBstr, kv_ind, kv[j], pos );
579 : }
580 :
581 51352 : bits -= 8 * pos;
582 : }
583 : else /* Q3 + Voronoi extensions r=1,2,3,... */
584 : {
585 77858 : push_indice( hBstr, i_ind, I, 4 * 3 );
586 77858 : bits -= 4 * 3;
587 :
588 77858 : pos = (int16_t) ( nq / 2 - 1 ); /* Voronoi order determination */
589 700722 : for ( j = 0; j < 8; j++ )
590 : {
591 622864 : push_indice( hBstr, kv_ind, kv[j], pos );
592 : }
593 :
594 77858 : bits -= 8 * pos;
595 : }
596 :
597 4867961 : *nbits = bits;
598 4867961 : return;
599 : }
|