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 "cnst.h"
43 : #include "prot.h"
44 : #include "rom_com.h"
45 : #include "wmc_auto.h"
46 :
47 : /*--------------------------------------------------------------------------*/
48 : /* Function hvq_pvq_bitalloc */
49 : /* ~~~~~~~~~~~~~~~~~~~~~~~~ */
50 : /* */
51 : /* Calculate the number of PVQ bands to code and allocate bits based on */
52 : /* the number of available bits. */
53 : /*--------------------------------------------------------------------------*/
54 :
55 : /*! r: Number of low frequency bands */
56 5194 : int16_t hvq_pvq_bitalloc(
57 : int16_t num_bits, /* i/o: Number of available bits (including gain bits)*/
58 : const int32_t core_brate, /* i : core bitrate */
59 : const int16_t bwidth, /* i : Encoded bandwidth */
60 : const int16_t *ynrm, /* i : Envelope coefficients */
61 : const int32_t manE_peak, /* i : Peak energy mantissa */
62 : const int16_t expE_peak, /* i : Peak energy exponent */
63 : int16_t *Rk, /* o : bit allocation for concatenated vector */
64 : int16_t *R, /* i/o: Global bit allocation */
65 : int16_t *sel_bands, /* o : Selected bands for encoding */
66 : int16_t *n_sel_bands /* o : No. of selected bands for encoding */
67 : )
68 : {
69 : int16_t num_bands, k, band_max_bits;
70 : int16_t k_max;
71 : int16_t k_start;
72 : float E_max;
73 : int32_t E_max5;
74 : int16_t expo;
75 : int16_t align;
76 : int32_t acc;
77 : float tmp;
78 : int32_t env_mean;
79 : int16_t reciprocal;
80 : int16_t num_sfm;
81 : int16_t bit_cost;
82 :
83 5194 : if ( bwidth == FB )
84 : {
85 4462 : num_sfm = SFM_N_HARM_FB;
86 : }
87 : else
88 : {
89 732 : num_sfm = SFM_N_HARM;
90 : }
91 :
92 5194 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
93 : {
94 2999 : band_max_bits = HVQ_BAND_MAX_BITS_24k;
95 2999 : k_start = HVQ_THRES_SFM_24k;
96 2999 : if ( bwidth == FB )
97 : {
98 2291 : reciprocal = 2731;
99 : }
100 : else
101 : {
102 708 : reciprocal = 3277;
103 : }
104 : }
105 : else
106 : {
107 2195 : band_max_bits = HVQ_BAND_MAX_BITS_32k;
108 2195 : k_start = HVQ_THRES_SFM_32k;
109 2195 : if ( bwidth == FB )
110 : {
111 2171 : reciprocal = 3641;
112 : }
113 : else
114 : {
115 24 : reciprocal = 4681;
116 : }
117 : }
118 :
119 5194 : num_bands = num_bits / band_max_bits;
120 5194 : num_bits = num_bits - num_bands * band_max_bits;
121 :
122 5194 : if ( num_bits >= HVQ_NEW_BAND_BIT_THR )
123 : {
124 2836 : num_bands++;
125 : }
126 : else
127 : {
128 2358 : num_bits += band_max_bits;
129 : }
130 :
131 : /* safety check in case of bit errors */
132 5194 : if ( num_bands < 1 )
133 : {
134 28 : return 0;
135 : }
136 :
137 5166 : *n_sel_bands = 0;
138 5166 : env_mean = 0;
139 5166 : E_max = 0;
140 5166 : k_max = k_start;
141 53943 : for ( k = k_start; k < num_sfm; k++ )
142 : {
143 48777 : tmp = dicn[ynrm[k]];
144 48777 : env_mean += ynrm[k];
145 48777 : if ( tmp > E_max )
146 : {
147 10079 : E_max = tmp;
148 10079 : k_max = k;
149 : }
150 : }
151 5166 : env_mean = 2 * ( env_mean * reciprocal );
152 :
153 5166 : if ( band_len_harm[k_max] == 96 )
154 : {
155 64 : bit_cost = 61;
156 : }
157 : else
158 : {
159 5102 : QuantaPerDsDirac( band_len_harm[k_max], 1, hBitsN, &bit_cost );
160 : }
161 :
162 :
163 5166 : expo = max( 0, ynrm[k_max] - 1 ) >> 1;
164 5166 : E_max5 = E_max5_tbl[ynrm[k_max]];
165 5166 : align = expo - expE_peak;
166 5166 : align = align + ( 19 - 14 ) - ( 31 - 2 * 12 );
167 5166 : if ( align < 0 )
168 : {
169 4974 : acc = E_max5 - ( manE_peak >> -align );
170 : }
171 : else
172 : {
173 192 : acc = ( E_max5 >> align ) - manE_peak;
174 : }
175 5166 : if ( acc > 0 && ( ( env_mean - ( ynrm[k_max] << 16 ) ) > 0x30000L ) && num_bands > 1 && ( num_bits - HVQ_PVQ_GAIN_BITS ) << 3 >= bit_cost )
176 : {
177 1405 : sel_bands[*n_sel_bands] = k_max;
178 1405 : ( *n_sel_bands )++;
179 1405 : R[k_max] = 1; /* Mark that the band has been encoded for fill_spectrum */
180 : }
181 :
182 : /* Allocate bits */
183 10179 : for ( k = 0; k < num_bands - 1; k++ )
184 : {
185 5013 : Rk[k] = band_max_bits - HVQ_PVQ_GAIN_BITS;
186 : }
187 5166 : Rk[num_bands - 1] = num_bits - HVQ_PVQ_GAIN_BITS;
188 :
189 5166 : return num_bands;
190 : }
|