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 "rom_com.h"
43 : #include "prot.h"
44 : #include <assert.h>
45 : #include "wmc_auto.h"
46 :
47 : /*--------------------------------------------------------------------------
48 : * subband_gain_bits()
49 : *
50 : * HQ core encoder
51 : *--------------------------------------------------------------------------*/
52 :
53 27934 : static void subband_gain_bits(
54 : const int16_t *Rk, /* i : bit allocation per band (Q3)*/
55 : const int16_t N, /* i : number of bands */
56 : int16_t *bits, /* o : gain bits per band */
57 : const int16_t *sfmsize /* i : Size of bands */
58 : )
59 : {
60 : int16_t i, b, tot;
61 : int16_t bps;
62 :
63 27934 : tot = 0;
64 :
65 1150883 : for ( i = 0; i < N; i++ )
66 : {
67 1122949 : bps = ( Rk[i] * fg_inv_tbl_fx[sfmsize[i] >> 3] ) >> 18;
68 1122949 : if ( ( ( sfmsize[i] * ( bps + 1 ) ) << 3 ) - Rk[i] == 0 )
69 : { /* correct approx. division result, to obtain exact integer division output */
70 0 : bps++;
71 : }
72 1122949 : bps = min( 7, bps );
73 :
74 1122949 : b = fine_gain_bits[bps];
75 1122949 : bits[i] = b;
76 1122949 : tot += b;
77 : }
78 :
79 27934 : if ( tot == 0 )
80 : {
81 : /* If no gain bits were assigned, use one bit anyway for potential PVQ overage */
82 5265 : bits[0] = 1;
83 : }
84 :
85 27934 : return;
86 : }
87 :
88 : /*--------------------------------------------------------------------------*
89 : * assign_gain_bits()
90 : *
91 : * Assign gain adjustment bits and update bit budget
92 : *--------------------------------------------------------------------------*/
93 :
94 : /*! r: Number of assigned gain bits */
95 118459 : int16_t assign_gain_bits(
96 : const int16_t core, /* i : HQ core */
97 : const int16_t BANDS, /* i : Number of bands */
98 : const int16_t *band_width, /* i : Sub band bandwidth */
99 : int16_t *Rk, /* i/o: Bit allocation/Adjusted bit alloc. (Q3) */
100 : int16_t *gain_bits_array, /* o : Assigned gain bits */
101 : int16_t *Rcalc /* o : Bit budget for shape quantizer (Q3) */
102 : )
103 : {
104 : int16_t gain_bits_tot;
105 : int16_t i;
106 :
107 : /* Allocate gain bits for every subband used, based on bitrate and bandwidth */
108 118459 : if ( core == HQ_CORE )
109 : {
110 27934 : subband_gain_bits( Rk, BANDS, gain_bits_array, band_width );
111 : }
112 : else
113 : {
114 90525 : set_s( gain_bits_array, 0, BANDS );
115 : }
116 :
117 : /* Re-adjust bit budget for gain quantization */
118 118459 : gain_bits_tot = 0;
119 118459 : *Rcalc = 0;
120 1891559 : for ( i = 0; i < BANDS; i++ )
121 : {
122 1773100 : if ( Rk[i] > 0 )
123 : {
124 1168515 : Rk[i] -= gain_bits_array[i] * 8;
125 1168515 : gain_bits_tot += gain_bits_array[i];
126 1168515 : *Rcalc += Rk[i];
127 : }
128 : }
129 :
130 118459 : return gain_bits_tot;
131 : }
|