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 75388 : 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 686744 : for ( i = 0; i < Nsv; i++ )
74 : {
75 611356 : ener = 2.0f; /* to set ebits >= 0 */
76 5502204 : for ( j = 0; j < 8; j++ )
77 : {
78 4890848 : x1[j] = xri[i * 8 + j];
79 4890848 : ener += x1[j] * x1[j];
80 : }
81 :
82 : /* estimated bit consumption when gain=1 */
83 611356 : ebits[i] = 5.0f * FAC_LOG2 * (float) log10( ener * 0.5f );
84 : }
85 :
86 : /* estimate gain according to number of bits allowed */
87 75388 : fac = 128.0f; /* start at the middle (offset range = 0 to 255.75) */
88 75388 : offset = 0.0f;
89 75388 : 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 829268 : for ( iter = 0; iter < 10; iter++ )
93 : {
94 753880 : offset += fac;
95 : /* calculate the required number of bits */
96 753880 : nbits = 0.0;
97 6867440 : for ( i = 0; i < Nsv; i++ )
98 : {
99 6113560 : tmp = ebits[i] - offset;
100 6113560 : if ( tmp < 0.0 )
101 : {
102 1621457 : tmp = 0.0;
103 : }
104 6113560 : nbits += tmp;
105 : }
106 : /* decrease gain when no overflow occurs */
107 753880 : if ( nbits <= nbits_max )
108 : {
109 430479 : offset -= fac;
110 : }
111 753880 : fac *= 0.5;
112 : }
113 :
114 : /* estimated gain (when offset=0, estimated gain=1) */
115 75388 : 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 686744 : for ( i = 0; i < Nsv; i++ )
119 : {
120 5502204 : for ( j = 0; j < 8; j++ )
121 : {
122 4890848 : x1[j] = xri[i * 8 + j] * gain_inv;
123 : }
124 :
125 611356 : re8_PPV( x1, c );
126 5502204 : for ( j = 0; j < 8; j++ )
127 : {
128 4890848 : xriq[i * 8 + j] = c[j];
129 : }
130 : }
131 :
132 75388 : fac = 0;
133 :
134 : /* round bit allocations and save */
135 686744 : for ( i = 0; i < Nsv; i++ )
136 : {
137 611356 : xriq[( Nsv * 8 ) + i] = (int16_t) floor( ebits[i] * 128.0f );
138 : }
139 :
140 75388 : return ( fac );
141 : }
142 :
143 :
144 : /*-----------------------------------------------------------------*
145 : * AVQ_encmux()
146 : *
147 : * Encode subvectors and write indexes into the bitstream
148 : *-----------------------------------------------------------------*/
149 :
150 75388 : 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 75388 : 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 75388 : if ( extl == SWB_BWE_HIGHRATE || extl == FB_BWE_HIGHRATE )
172 : {
173 1453 : nq_ind = IND_NQ2;
174 1453 : i_ind = IND_I2;
175 1453 : kv_ind = IND_KV2;
176 : }
177 : else
178 : {
179 73935 : nq_ind = IND_NQ;
180 73935 : i_ind = IND_I;
181 73935 : kv_ind = IND_KV;
182 : }
183 :
184 75388 : unusedbitsFlag = 0;
185 75388 : bitsMod = 0;
186 :
187 2638580 : for ( i = 0; i < NSV_MAX; i++ )
188 : {
189 2563192 : 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 75388 : t = kv; /* reuse vector to save memory */
200 686744 : for ( i = 0; i < Nsv; i++ )
201 : {
202 611356 : t[i] = xriq[8 * Nsv + i];
203 : }
204 :
205 686744 : for ( i = 0; i < Nsv; i++ )
206 : {
207 611356 : bits = t[0];
208 611356 : pos = 0;
209 5144580 : for ( j = 1; j < Nsv; j++ )
210 : {
211 4533224 : if ( t[j] > bits )
212 : {
213 910744 : bits = t[j];
214 910744 : pos = j;
215 : }
216 : }
217 611356 : sort_idx[i] = pos;
218 611356 : t[pos] = -1;
219 : }
220 :
221 : /* compute multi-rate indices and avoid bit budget overflow */
222 75388 : pos_max = 0;
223 75388 : bits = 0;
224 686744 : for ( i = 0; i < Nsv; i++ )
225 : {
226 : /* find vector to quantize (criteria: nb of estimated bits) */
227 611356 : pos = sort_idx[i];
228 :
229 : /* compute multi-rate index of rounded subvector (nq,I,kv[]) */
230 611356 : re8_cod( &xriq[pos * 8], &nq[pos], &I[pos], &kv[8 * pos] );
231 :
232 611356 : if ( nq[pos] > 0 )
233 : {
234 525806 : j = pos_max;
235 525806 : if ( pos > j )
236 : {
237 206334 : j = pos;
238 : }
239 :
240 : /* compute (number of bits -1) to describe Q #nq */
241 525806 : if ( nq[pos] >= 2 )
242 : {
243 525806 : 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 525806 : if ( ( bits + overflow + j ) > *nb_bits )
252 : {
253 : /* if budget overflow */
254 764163 : for ( j = pos * 8; j < ( pos * 8 ) + 8; j++ )
255 : {
256 679256 : xriq[j] = 0;
257 : }
258 84907 : nq[pos] = 0; /* force Q0 */
259 : }
260 : else
261 : {
262 440899 : bits += overflow;
263 440899 : pos_max = j; /* update index of the last described subvector */
264 : }
265 : }
266 : }
267 :
268 75388 : nullVec = 0;
269 75388 : dummy_bits = 0;
270 75388 : svOrder[Nsv - 1] = trgtSvPos;
271 75388 : svOrder[0] = 0;
272 75388 : svOrder[1] = 1;
273 75388 : i = 2;
274 75388 : j = i;
275 75388 : if ( avq_bit_sFlag == 2 )
276 : {
277 20139 : j = i + 1;
278 : }
279 460728 : while ( i < Nsv - 1 )
280 : {
281 385340 : svOrder[i] = j;
282 385340 : i++;
283 385340 : j++;
284 : }
285 :
286 : /* write indexes to the bitstream */
287 : /* ============================== */
288 :
289 75388 : bits = *nb_bits;
290 75388 : overflow = 0;
291 640451 : for ( i = 0; i < Nsv; i++ )
292 : {
293 611356 : k = svOrder[i];
294 611356 : if ( avq_bit_sFlag == 2 && bits % 5 == 4 && bits > 8 && bits < 30 && k >= trgtSvPos && i < Nsv - 1 )
295 : {
296 2972 : ordr_esti( Nsv - i, &trgtSvPos, &svOrder[i], Nsv );
297 2972 : k = svOrder[i];
298 2972 : avq_bit_sFlag = 1;
299 : }
300 :
301 611356 : if ( k == trgtSvPos && avq_bit_sFlag > 0 )
302 : {
303 70590 : if ( ( *nb_bits - bits ) == 7 || bits < BIT_SAVING_LOW_THR || bits >= BIT_SAVING_HIGH_THR )
304 : {
305 24297 : avq_bit_sFlag = 0;
306 : }
307 : else
308 : {
309 : break;
310 : }
311 : }
312 :
313 565063 : if ( 5 * nq[k] - 1 == bits ) /* check the overflow */
314 : {
315 6453 : overflow = 1;
316 : }
317 :
318 565063 : if ( bits > 8 )
319 : {
320 : /* write the unary code for nq[i] */
321 536111 : j = nq[k] - 1;
322 536111 : if ( nq[k] > 0 )
323 : {
324 : /* write the unary code */
325 394831 : while ( j > 16 )
326 : {
327 0 : push_indice( hBstr, nq_ind, 65535, 16 );
328 0 : bits -= 16;
329 0 : j -= 16;
330 : }
331 :
332 394831 : if ( j > 0 )
333 : {
334 394831 : push_indice( hBstr, nq_ind, ( 1 << j ) - 1, j );
335 394831 : bits -= j;
336 : }
337 : }
338 :
339 536111 : if ( !overflow )
340 : {
341 : /* write the stop bit */
342 529658 : push_indice( hBstr, nq_ind, 0, 1 );
343 529658 : bits--;
344 : }
345 :
346 : /* write codebook indices (rank I and event. Voronoi index kv) */
347 536111 : wrte_cv( hBstr, nq[k], i_ind, kv_ind, I[k], &kv[k * 8], &bits );
348 : }
349 : } /* for */
350 :
351 : /* Bit Saving Solution */
352 75388 : if ( avq_bit_sFlag > 0 && bits > 8 )
353 : {
354 46293 : bitsMod = bits % 5;
355 46293 : i = svOrder[Nsv - 1];
356 46293 : if ( i != Nsv - 1 )
357 : {
358 15742 : nullVec = 0;
359 94452 : for ( j = i; j < Nsv - 1; j++ )
360 : {
361 78710 : if ( nq[svOrder[j]] == 0 )
362 : {
363 10848 : nullVec++;
364 : }
365 : }
366 15742 : nq_est = bits / 5;
367 15742 : 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 106 : dummy_bits = 5 - bitsMod;
370 106 : bits = bits + dummy_bits; /* add dummy bits */
371 106 : bitsMod = 0;
372 : }
373 15636 : 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 4 : overflow = 0;
376 4 : if ( ( bitsMod + nullVec ) % 5 != 0 )
377 : {
378 4 : overflow = 1;
379 : }
380 4 : dummy_bits = nullVec + overflow;
381 4 : bits = bits + dummy_bits; /* add dummy bits */
382 4 : bitsMod = 0;
383 : }
384 : }
385 :
386 46293 : overflow = 1;
387 46293 : if ( bitsMod != 4 )
388 : {
389 38725 : overflow = 0;
390 38725 : bits -= bitsMod;
391 : }
392 46293 : bits = bits + overflow; /*add fake bit */
393 46293 : unused_bits = bits - 5 * nq[i];
394 46293 : if ( nq[i] == 0 ) /*no bit savings*/
395 : {
396 225 : unused_bits--; /*Stop Bit*/
397 : }
398 46293 : unused_bits_idx = (int16_t) unused_bits / 5;
399 :
400 46293 : unusedbitsFlag = 0;
401 :
402 46293 : if ( dummy_bits == 0 )
403 : {
404 46183 : if ( unused_bits_idx == 1 && bits > BIT_SAVING_LOW_THR )
405 : {
406 21125 : unused_bits_idx = 0;
407 21125 : unusedbitsFlag = 1;
408 : }
409 25058 : else if ( unused_bits_idx == 0 && bits > BIT_SAVING_LOW_THR )
410 : {
411 8841 : unused_bits_idx = 1;
412 8841 : unusedbitsFlag = -1;
413 : }
414 : }
415 :
416 46293 : j = unused_bits_idx;
417 : /*Encode Unused Bit Unary Codeword */
418 46293 : if ( j > 0 )
419 : {
420 : /* write the unary code */
421 11706 : push_indice( hBstr, nq_ind, ( 1 << j ) - 1, j );
422 : }
423 :
424 46293 : if ( nq[i] != 0 )
425 : {
426 : /* write the stop bit */
427 46068 : push_indice( hBstr, nq_ind, 0, 1 );
428 : }
429 :
430 : /*Compute AVQ code book number from unused Bits */
431 46293 : nq_est = (int16_t) ceil( 0.2f * ( bits - 5 * ( unusedbitsFlag + unused_bits_idx ) ) );
432 :
433 46293 : if ( nq_est == 1 )
434 : {
435 225 : nq_est = 0;
436 : }
437 46293 : bits -= overflow;
438 :
439 46293 : bits -= j;
440 :
441 46293 : if ( nq_est != 0 )
442 : {
443 46068 : bits--;
444 : }
445 46293 : nq[i] = nq_est;
446 :
447 : /* write codebook indices (rank I and event. Voronoi index kv) */
448 46293 : wrte_cv( hBstr, nq[i], i_ind, kv_ind, I[i], &kv[i * 8], &bits );
449 :
450 46293 : bits -= dummy_bits;
451 :
452 46293 : if ( bitsMod != 4 )
453 : {
454 38725 : bits += bitsMod;
455 : }
456 : }
457 :
458 :
459 75388 : *nb_bits = bits;
460 :
461 75388 : return;
462 : }
463 :
464 :
465 : /*-------------------------------------------------------------------*
466 : * Function AVQ_cod_lpc() *
467 : * *
468 : * Split algebraic vector quantizer (AVQ) for LPC quantization *
469 : *-------------------------------------------------------------------*/
470 :
471 674525 : 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 674525 : pos = Nsv;
485 :
486 2023575 : for ( l = 0; l < Nsv; l++ )
487 : {
488 12141450 : for ( i = 0; i < 8; i++ )
489 : {
490 10792400 : x1[i] = nvec[l * 8 + i];
491 : }
492 :
493 1349050 : re8_PPV( x1, c );
494 :
495 1349050 : re8_cod( c, &nq, &I, kv );
496 :
497 12141450 : for ( i = 0; i < 8; i++ )
498 : {
499 10792400 : nvecq[l * 8 + i] = c[i];
500 : }
501 :
502 1349050 : indx[l] = nq; /* index[0..Nsv-1] = quantizer number (0,2,3,4...) */
503 :
504 1349050 : nk = 0;
505 1349050 : n = nq;
506 :
507 1349050 : if ( nq > 4 )
508 : {
509 91984 : nk = ( nq - 3 ) >> 1;
510 91984 : n = nq - nk * 2;
511 : }
512 :
513 : /* write n groups of 4-bit for base codebook index (I) */
514 4862220 : while ( n-- > 0 )
515 : {
516 3513170 : indx[pos++] = ( I & 0x0F );
517 3513170 : I >>= 4;
518 : }
519 :
520 : /* write n groups of 4-bit for Voronoi index (k[]) */
521 1443455 : while ( nk-- > 0 )
522 : {
523 94405 : ival = 0;
524 :
525 849645 : for ( i = 0; i < 8; i++ )
526 : {
527 755240 : ival <<= 1;
528 755240 : ival += ( kv[i] & 0x01 );
529 755240 : kv[i] >>= 1;
530 : }
531 94405 : indx[pos++] = ( ival & 0x0F );
532 94405 : ival >>= 4;
533 94405 : indx[pos++] = ( ival & 0x0F );
534 : }
535 : }
536 :
537 674525 : return;
538 : }
539 :
540 : /*-------------------------------------------------------------------*
541 : * Function wrte_cv() *
542 : * *
543 : * write codebook indices (rank I and event. Voronoi index kv) *
544 : *-------------------------------------------------------------------*/
545 :
546 582404 : 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 582404 : bits = *nbits;
560 :
561 : /* write codebook indices (rank I and event. Voronoi index kv) */
562 582404 : if ( nq == 0 ) /* Q0 */
563 : {
564 : /* nothing to write */
565 : }
566 440899 : else if ( nq < 5 ) /* Q2, Q3, Q4 */
567 : {
568 436003 : push_indice( hBstr, i_ind, I, 4 * nq );
569 436003 : bits -= ( 4 * nq );
570 : }
571 4896 : else if ( nq % 2 == 0 ) /* Q4 + Voronoi extensions r=1,2,3,... */
572 : {
573 1745 : push_indice( hBstr, i_ind, I, 4 * 4 );
574 1745 : bits -= 4 * 4;
575 1745 : pos = (int16_t) ( nq / 2 - 2 ); /* Voronoi order determination */
576 15705 : for ( j = 0; j < 8; j++ )
577 : {
578 13960 : push_indice( hBstr, kv_ind, kv[j], pos );
579 : }
580 :
581 1745 : bits -= 8 * pos;
582 : }
583 : else /* Q3 + Voronoi extensions r=1,2,3,... */
584 : {
585 3151 : push_indice( hBstr, i_ind, I, 4 * 3 );
586 3151 : bits -= 4 * 3;
587 :
588 3151 : pos = (int16_t) ( nq / 2 - 1 ); /* Voronoi order determination */
589 28359 : for ( j = 0; j < 8; j++ )
590 : {
591 25208 : push_indice( hBstr, kv_ind, kv[j], pos );
592 : }
593 :
594 3151 : bits -= 8 * pos;
595 : }
596 :
597 582404 : *nbits = bits;
598 582404 : return;
599 : }
|