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 6586 : 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 6586 : *n_sel_bands = 0;
84 :
85 6586 : if ( bwidth == FB )
86 : {
87 5578 : num_sfm = SFM_N_HARM_FB;
88 : }
89 : else
90 : {
91 1008 : num_sfm = SFM_N_HARM;
92 : }
93 :
94 6586 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
95 : {
96 4287 : band_max_bits = HVQ_BAND_MAX_BITS_24k;
97 4287 : k_start = HVQ_THRES_SFM_24k;
98 4287 : if ( bwidth == FB )
99 : {
100 3303 : reciprocal = 2731;
101 : }
102 : else
103 : {
104 984 : reciprocal = 3277;
105 : }
106 : }
107 : else
108 : {
109 2299 : band_max_bits = HVQ_BAND_MAX_BITS_32k;
110 2299 : k_start = HVQ_THRES_SFM_32k;
111 2299 : if ( bwidth == FB )
112 : {
113 2275 : reciprocal = 3641;
114 : }
115 : else
116 : {
117 24 : reciprocal = 4681;
118 : }
119 : }
120 :
121 6586 : num_bands = num_bits / band_max_bits;
122 6586 : num_bits = num_bits - num_bands * band_max_bits;
123 :
124 6586 : if ( num_bits >= HVQ_NEW_BAND_BIT_THR )
125 : {
126 3698 : num_bands++;
127 : }
128 : else
129 : {
130 2888 : num_bits += band_max_bits;
131 : }
132 :
133 : /* safety check in case of bit errors */
134 6586 : if ( num_bands < 1 )
135 : {
136 34 : return 0;
137 : }
138 :
139 6552 : env_mean = 0;
140 6552 : E_max = 0;
141 6552 : k_max = k_start;
142 69711 : for ( k = k_start; k < num_sfm; k++ )
143 : {
144 63159 : tmp = dicn[ynrm[k]];
145 63159 : env_mean += ynrm[k];
146 63159 : if ( tmp > E_max )
147 : {
148 12469 : E_max = tmp;
149 12469 : k_max = k;
150 : }
151 : }
152 6552 : env_mean = 2 * ( env_mean * reciprocal );
153 :
154 6552 : if ( band_len_harm[k_max] == 96 )
155 : {
156 64 : bit_cost = 61;
157 : }
158 : else
159 : {
160 6488 : QuantaPerDsDirac( band_len_harm[k_max], 1, hBitsN, &bit_cost );
161 : }
162 :
163 :
164 6552 : expo = max( 0, ynrm[k_max] - 1 ) >> 1;
165 6552 : E_max5 = E_max5_tbl[ynrm[k_max]];
166 6552 : align = expo - expE_peak;
167 6552 : align = align + ( 19 - 14 ) - ( 31 - 2 * 12 );
168 6552 : if ( align < 0 )
169 : {
170 6160 : acc = E_max5 - ( manE_peak >> -align );
171 : }
172 : else
173 : {
174 392 : acc = ( E_max5 >> align ) - manE_peak;
175 : }
176 6552 : if ( acc > 0 && ( ( env_mean - ( ynrm[k_max] << 16 ) ) > 0x30000L ) && num_bands > 1 && ( num_bits - HVQ_PVQ_GAIN_BITS ) << 3 >= bit_cost )
177 : {
178 1459 : sel_bands[*n_sel_bands] = k_max;
179 1459 : ( *n_sel_bands )++;
180 1459 : R[k_max] = 1; /* Mark that the band has been encoded for fill_spectrum */
181 : }
182 :
183 : /* Allocate bits */
184 12097 : for ( k = 0; k < num_bands - 1; k++ )
185 : {
186 5545 : Rk[k] = band_max_bits - HVQ_PVQ_GAIN_BITS;
187 : }
188 6552 : Rk[num_bands - 1] = num_bits - HVQ_PVQ_GAIN_BITS;
189 :
190 6552 : return num_bands;
191 : }
|