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 : #include <stdint.h>
34 : #include "options.h"
35 : #include "cnst.h"
36 : #include "ivas_prot.h"
37 : #include "prot.h"
38 : #include "stat_enc.h"
39 : #include "wmc_auto.h"
40 : #include "ivas_rom_com.h"
41 : #ifdef DEBUGGING
42 : #include "debug.h"
43 : #endif
44 :
45 :
46 : /*---------------------------------------------------------------------*
47 : * Local function prototypes
48 : *---------------------------------------------------------------------*/
49 :
50 : static int16_t adapt_GR_sg( const int16_t *in, const int16_t len, const int16_t no_symb, int16_t *nbits, int16_t *in_enc, const int16_t *map0, const int16_t no_GR_ord );
51 :
52 :
53 : /*---------------------------------------------------------------------*
54 : * calculate_diff()
55 : *
56 : * calculate difference wrt previous frame and maps it to positive integer
57 : *---------------------------------------------------------------------*/
58 :
59 50834 : static void calculate_diff(
60 : const int16_t *in, /* i : current frame values */
61 : const int16_t *prev, /* i : previous frmae values */
62 : int16_t *in_diff, /* o : difference values */
63 : const int16_t len /* i : vector length */
64 : )
65 : {
66 : int16_t i, tmp;
67 :
68 424465 : for ( i = 0; i < len; i++ )
69 : {
70 373631 : tmp = in[i] - prev[i];
71 373631 : if ( tmp > 0 )
72 : {
73 83628 : in_diff[i] = 2 * tmp - 1;
74 : }
75 : else
76 : {
77 290003 : if ( tmp < 0 )
78 : {
79 60242 : in_diff[i] = -2 * tmp;
80 : }
81 : else
82 : {
83 229761 : in_diff[i] = 0;
84 : }
85 : }
86 : }
87 :
88 50834 : return;
89 : }
90 :
91 :
92 : /*---------------------------------------------------------------------*
93 : * calculate_bit_diff()
94 : *
95 : * estimates the GR number of bits for encoding differential values
96 : * selects between GR order 0 or 1
97 : *---------------------------------------------------------------------*/
98 :
99 : /*! r: number of bits */
100 50834 : static int16_t calculate_bit_diff(
101 : int16_t *in_diff, /* i : values to be encoded */
102 : int16_t len, /* i : number of params */
103 : int16_t *flag_diff /* o : selected GR order */
104 : )
105 : {
106 : int16_t i, nb, nb1;
107 :
108 50834 : nb = 0;
109 50834 : nb1 = 0;
110 424465 : for ( i = 0; i < len; i++ )
111 : {
112 373631 : nb += in_diff[i];
113 373631 : nb1 += ( ( in_diff[i] ) >> 1 );
114 : }
115 50834 : nb += len;
116 50834 : nb1 += 2 * len;
117 :
118 50834 : if ( nb1 < nb )
119 : {
120 5464 : nb = nb1 + 1;
121 5464 : *flag_diff = 1; /* GR order for differential coding */
122 : }
123 : else
124 : {
125 45370 : nb = nb + 1;
126 45370 : *flag_diff = 0;
127 : }
128 :
129 50834 : return nb;
130 : }
131 :
132 :
133 : /*---------------------------------------------------------------------*
134 : * adapt_GR_ief()
135 : *
136 : * adaptive GR coding considering also the differential case
137 : *---------------------------------------------------------------------*/
138 :
139 : /*! r: used GR order */
140 45135 : int16_t adapt_GR_ief(
141 : const int16_t *in, /* i : vector to encode */
142 : int16_t *in_diff, /* o : encoded symbols in case of differential encoding */
143 : const int16_t *prev, /* i : previous frame data */
144 : const int16_t len, /* i : input vector length */
145 : const int16_t no_symb, /* i : number of symbols */
146 : int16_t *nbits, /* o : number of used bits */
147 : int16_t *in_enc, /* o : symbold actually encoded after adapt_GR */
148 : const int16_t *map0, /* i : mapping array */
149 : const int16_t no_GR_ord, /* i : number of GR order to try 2: 0,1; 3:0,1,2 */
150 : int16_t *nbits_diff, /* o : number bits in diff encoding */
151 : const int16_t side_gain_counter, /* i : number of frames since last abs coding */
152 : float *side_gain_bitdiff_lp, /* i/o: LP-filtered bit difference between abs/diff */
153 : const int16_t try_diff /* i : diff coding allowed 1/0 */
154 : )
155 : {
156 : int16_t nb_diff, flag, flag_diff;
157 : int16_t bitdiff;
158 :
159 45135 : flag = adapt_GR_sg( in, len, no_symb, nbits, in_enc, map0, no_GR_ord );
160 :
161 45135 : if ( try_diff ) /* inter-frame prediction allowed */
162 : {
163 10672 : calculate_diff( in, prev, in_diff, len );
164 10672 : nb_diff = calculate_bit_diff( in_diff, len, &flag_diff );
165 10672 : *nbits_diff = nb_diff;
166 10672 : bitdiff = min( 5 * len, *nbits ) - ( nb_diff + 1 );
167 10672 : *side_gain_bitdiff_lp = STEREO_DFT_BITDIFF_LP_FAC * ( (float) bitdiff ) + ( 1.0f - STEREO_DFT_BITDIFF_LP_FAC ) * ( *side_gain_bitdiff_lp );
168 10672 : if ( bitdiff > STEREO_DFT_BITDIFF_ABS_SELECT * ( *side_gain_bitdiff_lp ) * ( (float) side_gain_counter ) / ( (float) ( STEREO_DFT_FEC_THRESHOLD + 1 ) ) )
169 : {
170 9001 : flag = no_GR_ord + flag_diff;
171 9001 : *nbits = nb_diff + 1;
172 : }
173 : }
174 : else
175 : {
176 34463 : *nbits_diff = 100;
177 34463 : flag_diff = -1;
178 : }
179 :
180 45135 : return flag;
181 : }
182 :
183 :
184 : /*---------------------------------------------------------------------*
185 : * adapt_GR_rpg1_ief()
186 : *
187 : * use adaptive GR for RPG's considering also the differential case
188 : *---------------------------------------------------------------------*/
189 :
190 : /*! r: used GR order */
191 42086 : int16_t adapt_GR_rpg1_ief(
192 : const int16_t *in, /* i : res pred gains input vector */
193 : int16_t *in_diff, /* o : encoded symbols in case of differential encoding */
194 : const int16_t *prev, /* i : previous frame data */
195 : const int16_t len, /* i : input vector length */
196 : const int16_t no_symb, /* i : number of symbols */
197 : int16_t *nbits, /* o : number of used bits */
198 : int16_t *in_enc, /* o : symbold actually encoded after adapt_GR */
199 : const int16_t *maps, /* i : mapping array */
200 : int16_t *nbits_diff, /* o : estimated no of bits for differential encoding */
201 : const int16_t no_GR_ord, /* i : number of GR order to try 2: 0,1; 3:0,1,2 */
202 : const int16_t try_diff /* i : diff coding allowed 1/0 */
203 : )
204 : {
205 : const int16_t *map0;
206 : int16_t s, i, GR_ord, flag_diff, nb_diff;
207 :
208 42086 : map0 = &maps[8 * NO_SYMB_GR_PRED_G];
209 :
210 42086 : if ( in[0] == 0 )
211 : {
212 2460 : s = 0;
213 19397 : for ( i = 0; i < len; i++ )
214 : {
215 16937 : s += in[i];
216 : }
217 :
218 2460 : if ( s == 0 )
219 : {
220 : /* encode only the first zero with GR1 */
221 1135 : *nbits = ( ( map0[0] ) >> 1 ) + 2;
222 1135 : in_enc[0] = map0[0];
223 : }
224 : else
225 : {
226 1325 : *nbits = 1000; /* large number such that the plain coding will be selected */
227 : }
228 2460 : GR_ord = 1;
229 : }
230 : else
231 : {
232 39626 : GR_ord = adapt_GR_sg( in, len, no_symb, nbits, in_enc, maps, no_GR_ord );
233 : }
234 :
235 42086 : if ( try_diff )
236 : {
237 40162 : calculate_diff( in, prev, in_diff, len );
238 40162 : nb_diff = calculate_bit_diff( in_diff, len, &flag_diff );
239 40162 : *nbits_diff = nb_diff;
240 :
241 40162 : if ( ( nb_diff + 1 ) < *nbits ) /* there is one more bit to signal differential coding */
242 : {
243 31054 : GR_ord = no_GR_ord + flag_diff;
244 :
245 31054 : *nbits = nb_diff + 1;
246 : }
247 : }
248 : else
249 : {
250 1924 : *nbits_diff = 100;
251 : }
252 :
253 42086 : return GR_ord;
254 : }
255 :
256 :
257 : /*---------------------------------------------------------------------*
258 : * adapt_GR_sg()
259 : *
260 : * adaptive GR coding
261 : *---------------------------------------------------------------------*/
262 :
263 : /*! r: GR order */
264 84761 : static int16_t adapt_GR_sg(
265 : const int16_t *in, /* i : data to be encoded */
266 : const int16_t len, /* i : number of params to be encoded */
267 : const int16_t no_symb, /* i : max number of symbols */
268 : int16_t *nbits, /* o : estimated number of bits */
269 : int16_t *in_enc, /* o : mapped symbols */
270 : const int16_t *map0, /* i : mapping */
271 : const int16_t no_GR_ord /* i : number of GR orders to be tested (2 or 3)*/
272 : )
273 : {
274 : const int16_t *map;
275 : int16_t nbits0, nbits1, nbits2, symb, map_symb, i, nb0, nb1, nb2;
276 :
277 84761 : if ( no_symb == NO_SYMB_GR_SIDE_G )
278 : {
279 45135 : map = &map0[15 * no_symb];
280 : }
281 : else
282 : {
283 39626 : map = &( map0[8 * no_symb] );
284 : }
285 :
286 84761 : set_s( in_enc, 0, len );
287 :
288 84761 : nbits1 = 0;
289 84761 : nbits0 = 0;
290 84761 : nbits2 = 0;
291 :
292 784826 : for ( i = 0; i < len; i++ )
293 : {
294 700065 : symb = in[i];
295 700065 : map_symb = map[symb];
296 :
297 700065 : if ( i == 0 )
298 : {
299 : /* GR order is 1 for first band*/
300 84761 : nb0 = ( map_symb >> 1 ) + 1;
301 84761 : nb2 = nb0 - 2;
302 : }
303 : else
304 : {
305 615304 : nb0 = map_symb;
306 615304 : nb2 = ( ( map_symb ) >> 2 );
307 : }
308 700065 : nb1 = ( ( map_symb ) >> 1 );
309 :
310 700065 : in_enc[i] = map_symb;
311 700065 : nbits0 += nb0;
312 700065 : nbits1 += nb1;
313 700065 : nbits2 += nb2;
314 :
315 : /* update counts */
316 700065 : map = &( map0[symb * no_symb] );
317 : }
318 :
319 84761 : if ( no_GR_ord == 2 )
320 : {
321 39626 : nbits0 += len + 1;
322 39626 : nbits1 += 2 * len + 1;
323 39626 : if ( ( nbits0 <= nbits1 ) )
324 : {
325 21261 : *nbits = nbits0;
326 21261 : return 0;
327 : }
328 : else
329 : {
330 18365 : *nbits = nbits1;
331 18365 : return 1;
332 : }
333 : }
334 : else
335 : {
336 45135 : if ( no_GR_ord == 3 )
337 : {
338 45135 : nbits0 += len + 2;
339 45135 : nbits1 += 2 * len + 1;
340 45135 : nbits2 += 3 * len + 2;
341 :
342 45135 : if ( ( nbits1 <= nbits2 ) && ( nbits1 <= nbits0 ) )
343 : {
344 28066 : *nbits = nbits1;
345 28066 : return 1;
346 : }
347 :
348 17069 : if ( ( nbits0 <= nbits2 ) && ( nbits0 <= nbits1 ) )
349 : {
350 11937 : *nbits = nbits0;
351 11937 : return 0;
352 : }
353 :
354 5132 : if ( ( nbits2 <= nbits0 ) && ( nbits2 <= nbits1 ) )
355 : {
356 5132 : *nbits = nbits2;
357 5132 : return 2;
358 : }
359 : }
360 : else
361 : {
362 0 : IVAS_ERROR( IVAS_ERR_INTERNAL, "Wrong number of GR orders!" );
363 : }
364 : }
365 :
366 0 : return -1;
367 : }
368 :
369 :
370 : /*---------------------------------------------------------------------*
371 : * write_GR0()
372 : *
373 : * write data with GR order 0
374 : *---------------------------------------------------------------------*/
375 :
376 : /*! r: number of bits written */
377 62336 : int16_t write_GR0(
378 : BSTR_ENC_HANDLE hBstr, /* i/o: Encoder bitstream handle */
379 : const int16_t ind, /* i : bitstream index */
380 : const int16_t *in, /* i : data to be encoded */
381 : const int16_t len /* i : input data length */
382 : )
383 : {
384 62336 : int16_t nb = 0, i, n, val;
385 :
386 474544 : for ( i = 0; i < len; i++ )
387 : {
388 412208 : n = in[i] + 1;
389 :
390 412208 : if ( n < 16 )
391 : {
392 412207 : val = ( 1 << in[i] ) - 1;
393 412207 : val = 2 * val;
394 412207 : push_indice( hBstr, ind, val, n );
395 : }
396 : else
397 : {
398 1 : push_indice( hBstr, ind, 16383, 14 );
399 1 : push_indice( hBstr, ind, ( 1 << ( n - 15 ) ) - 1, n - 15 );
400 1 : push_indice( hBstr, ind, 0, 1 );
401 : }
402 412208 : nb += n;
403 : }
404 :
405 62336 : return nb;
406 : }
407 :
408 :
409 : /*---------------------------------------------------------------------*
410 : * write_GR1()
411 : *
412 : * write data with GR order 1
413 : *---------------------------------------------------------------------*/
414 :
415 : /*! r: number of bits written */
416 68562 : int16_t write_GR1(
417 : BSTR_ENC_HANDLE hBstr, /* i/o: Encoder bitstream handle */
418 : const int16_t ind, /* i : bitstream index */
419 : const int16_t *in, /* i : data to be encoded */
420 : const int16_t len /* i : input data length */
421 : )
422 : {
423 68562 : int16_t nb = 0, i, n, val;
424 :
425 327485 : for ( i = 0; i < len; i++ )
426 : {
427 258923 : n = ( in[i] >> 1 );
428 258923 : if ( n < 14 )
429 : {
430 258764 : val = ( 1 << n ) - 1;
431 258764 : val = 4 * val + ( in[i] & 1 );
432 258764 : push_indice( hBstr, ind, val, n + 2 );
433 : }
434 : else
435 : {
436 159 : push_indice( hBstr, ind, 16383, 14 );
437 159 : if ( n > 14 )
438 : {
439 0 : push_indice( hBstr, ind, ( 1 << ( n - 14 ) ) - 1, n - 14 ); /* 1 n times */
440 : }
441 159 : push_indice( hBstr, ind, 0, 1 );
442 159 : push_indice( hBstr, ind, in[i] & 1, 1 );
443 : }
444 258923 : nb += n + 2;
445 : }
446 :
447 68562 : return nb;
448 : }
449 :
450 :
451 : /*---------------------------------------------------------------------*
452 : * write_GR2()
453 : *
454 : * write data with GR order 2
455 : *---------------------------------------------------------------------*/
456 :
457 : /*! r: number of bits written */
458 2789 : static int16_t write_GR2(
459 : BSTR_ENC_HANDLE hBstr, /* i/o: bitstream handle */
460 : const int16_t ind, /* i : bitstream index */
461 : const int16_t *in, /* i : data to be encoded */
462 : const int16_t len /* i : input data length */
463 : )
464 : {
465 2789 : int16_t nb = 0, i, n;
466 :
467 25677 : for ( i = 0; i < len; i++ )
468 : {
469 22888 : n = ( in[i] >> 2 );
470 22888 : if ( n < 14 )
471 : {
472 22888 : if ( n > 0 )
473 : {
474 12918 : push_indice( hBstr, ind, ( 1 << n ) - 1, n ); /* 1 n times */
475 : }
476 22888 : push_indice( hBstr, ind, 0, 1 );
477 22888 : push_indice( hBstr, ind, in[i] & 3, 2 );
478 : }
479 : else
480 : {
481 0 : push_indice( hBstr, ind, 16383, 14 );
482 0 : if ( n > 14 )
483 : {
484 0 : push_indice( hBstr, ind, ( 1 << ( n - 14 ) ) - 1, n - 14 ); /* 1 n times */
485 : }
486 0 : push_indice( hBstr, ind, 0, 1 );
487 0 : push_indice( hBstr, ind, in[i] & 3, 2 );
488 : }
489 22888 : nb += n + 3;
490 : }
491 :
492 2789 : return nb;
493 : }
494 :
495 :
496 : /*---------------------------------------------------------------------*
497 : * write_bitstream_GR()
498 : *
499 : * generic GR encoder for GR order 0 or 1
500 : *---------------------------------------------------------------------*/
501 :
502 : /*! r: number of bits written */
503 39340 : int16_t write_bitstream_GR(
504 : BSTR_ENC_HANDLE hBstr, /* i/o: Encoder bitstream handle */
505 : const int16_t ind, /* i : bitstream index */
506 : const int16_t *in, /* i : input vector */
507 : const int16_t len, /* i : input vector length */
508 : const int16_t GR_ord /* i : GR order */
509 : )
510 : {
511 39340 : int16_t nb = 0;
512 :
513 39340 : push_indice( hBstr, ind, GR_ord, 1 );
514 39340 : nb += 1;
515 :
516 39340 : if ( GR_ord == 0 )
517 : {
518 38651 : nb += write_GR0( hBstr, ind, in, len );
519 : }
520 : else
521 : {
522 689 : nb += write_GR1( hBstr, ind, in, len );
523 : }
524 :
525 39340 : return nb;
526 : }
527 :
528 :
529 : /*---------------------------------------------------------------------*
530 : * write_bitstream_adapt_GR()
531 : *
532 : * write encoded data using adaptive GR
533 : *---------------------------------------------------------------------*/
534 :
535 : /*! r: number of bits written */
536 42482 : int16_t write_bitstream_adapt_GR(
537 : BSTR_ENC_HANDLE hBstr, /* i/o: Encoder bitstream handle */
538 : const int16_t ind, /* i : bitstream index */
539 : const int16_t *in, /* i : values to be written in bitstream */
540 : const int16_t len, /* i : values vector length */
541 : const int16_t GR_ord, /* i : GR order to be used */
542 : const int16_t no_GR_ord /* i : speech/music 0/1 */
543 : )
544 : {
545 42482 : int16_t nb = 0;
546 :
547 42482 : nb = 0;
548 : /* first component first */
549 42482 : nb += write_GR1( hBstr, ind, in, 1 );
550 :
551 : /* write the GR order */
552 42482 : if ( no_GR_ord == 2 )
553 : {
554 7407 : push_indice( hBstr, ind, GR_ord, 1 );
555 7407 : nb += 1;
556 : }
557 : else
558 : {
559 35075 : if ( GR_ord == 1 )
560 : {
561 23164 : push_indice( hBstr, ind, 0, 1 ); /* GR order = 1*/
562 23164 : nb += 1;
563 : }
564 : else
565 : {
566 11911 : push_indice( hBstr, ind, 1, 1 );
567 11911 : push_indice( hBstr, ind, GR_ord >> 1, 1 ); /* '10' for GR ord = 0; '11' for GR ord 2 */
568 11911 : nb += 2;
569 : }
570 : }
571 :
572 42482 : if ( GR_ord == 0 )
573 : {
574 :
575 15331 : nb += write_GR0( hBstr, ind, &in[1], len - 1 );
576 : }
577 : else
578 : {
579 27151 : if ( GR_ord == 1 )
580 : {
581 24362 : nb += write_GR1( hBstr, ind, &in[1], len - 1 );
582 : }
583 : else
584 : {
585 2789 : nb += write_GR2( hBstr, ind, &in[1], len - 1 );
586 : }
587 : }
588 :
589 42482 : return nb;
590 : }
|