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 "rom_com.h"
44 : #include "prot.h"
45 : #include "wmc_auto.h"
46 :
47 : /*--------------------------------------------------------------------------*
48 : * fine_gain_pred()
49 : *
50 : * Fine gain prediction
51 : *--------------------------------------------------------------------------*/
52 :
53 123653 : void fine_gain_pred(
54 : const int16_t *sfm_start, /* i : Sub band start indices */
55 : const int16_t *sfm_end, /* i : Sub band end indices */
56 : const int16_t *sfm_size, /* i : Sub band bandwidths */
57 : const int16_t *i_sort, /* i : Energy sorting indices */
58 : const int16_t *K, /* i : Number of pulses per band */
59 : const int16_t *maxpulse, /* i : Maximum pulse per band */
60 : const int16_t *R, /* i : Bits per sub band (Q3) */
61 : const int16_t num_sfm, /* i : Number of sub bands */
62 : float *xq, /* i/o: Quantized vector /quantized vector with finegain adj */
63 : int16_t *y, /* i/o: Quantized vector */
64 : float *fg_pred, /* o : Predicted fine gains */
65 : const int16_t core /* i : Core */
66 : )
67 : {
68 : int16_t i, band;
69 : float gp;
70 : float xx;
71 : float accuracy;
72 : int16_t k, bw;
73 : float att;
74 :
75 1906932 : for ( band = 0; band < num_sfm; band++ )
76 : {
77 :
78 1783279 : k = K[i_sort[band]];
79 1783279 : if ( k > 0 )
80 : {
81 1058679 : bw = sfm_size[i_sort[band]];
82 1058679 : xx = 0;
83 16230891 : for ( i = sfm_start[i_sort[band]]; i < sfm_end[i_sort[band]]; i++ )
84 : {
85 15172212 : xx += xq[i] * xq[i];
86 : }
87 :
88 1058679 : if ( xx > 0 )
89 : {
90 : /* Normalize synthesis to RMS=1.0 */
91 1058679 : gp = (float) sqrt( bw / xx );
92 :
93 1058679 : if ( core == HQ_CORE && R != NULL && R[i_sort[band]] <= 256 )
94 : {
95 544473 : accuracy = ( (float) k / (float) bw ) * maxpulse[i_sort[band]];
96 544473 : att = 1.0f - 0.05f / accuracy;
97 544473 : att = max( 0.840896f, att ); /* Limit attenuation to norm quantizer error, 2^-0.25 */
98 544473 : gp *= att;
99 : }
100 :
101 1058679 : fg_pred[band] = gp;
102 : }
103 : else
104 : {
105 0 : fg_pred[band] = 0;
106 : }
107 : }
108 : else
109 : {
110 724600 : fg_pred[band] = 0;
111 16578708 : for ( i = sfm_start[i_sort[band]]; i < sfm_end[i_sort[band]]; i++ )
112 : {
113 15854108 : y[i] = 0;
114 15854108 : xq[i] = 0;
115 : }
116 : }
117 : }
118 123653 : return;
119 : }
120 :
121 : /*--------------------------------------------------------------------------*
122 : * get_max_pulses()
123 : *
124 : * Find the maximum pulse height (in unit pulses) in each band
125 : *--------------------------------------------------------------------------*/
126 :
127 118459 : void get_max_pulses(
128 : const int16_t *band_start, /* i : Sub band start indices */
129 : const int16_t *band_end, /* i : Sub band end indices */
130 : const int16_t *k_sort, /* i : Indices for sorting by energy */
131 : const int16_t *npulses, /* i : Pulses per sub band */
132 : const int16_t BANDS, /* i : Number of bands */
133 : int16_t *inp_vector, /* i/o: Encoded shape vectors */
134 : int16_t *maxpulse /* o : Maximum pulse height per band */
135 : )
136 : {
137 : int16_t i, k;
138 : int16_t npul;
139 : int16_t maxp;
140 :
141 1891559 : for ( k = 0; k < BANDS; k++ )
142 : {
143 1773100 : npul = npulses[k_sort[k]];
144 1773100 : maxp = 0;
145 1773100 : if ( npul > 0 )
146 : {
147 15950328 : for ( i = band_start[k_sort[k]]; i < band_end[k_sort[k]]; i++ )
148 : {
149 14901828 : if ( abs( inp_vector[i] ) > maxp )
150 : {
151 1582310 : maxp = (int16_t) abs( inp_vector[i] );
152 : }
153 : }
154 : }
155 1773100 : maxpulse[k_sort[k]] = maxp;
156 : }
157 :
158 118459 : return;
159 : }
160 :
161 : /*--------------------------------------------------------------------------*
162 : * fine_gain_dec()
163 : *
164 : * Fine gain decoder. Decodes fine gain adjustments and applies correction to
165 : * predicted fine gains
166 : *--------------------------------------------------------------------------*/
167 :
168 92424 : void fine_gain_dec(
169 : Decoder_State *st, /* i/o: Decoder state struct */
170 : const int16_t *ord, /* i : Indices for energy order */
171 : const int16_t num_sfm, /* i : Number of bands */
172 : const int16_t *gain_bits, /* i : Gain adjustment bits per sub band */
173 : float *fg_pred /* i/o: Predicted gains / Corrected gains */
174 : )
175 : {
176 : int16_t band;
177 : int16_t gbits;
178 : int16_t idx;
179 : float gain_dbq;
180 :
181 1423926 : for ( band = 0; band < num_sfm; band++ )
182 : {
183 1331502 : gbits = gain_bits[ord[band]];
184 1331502 : if ( fg_pred[band] != 0 && gbits > 0 )
185 : {
186 106608 : idx = get_next_indice( st, gbits );
187 106608 : gain_dbq = finegain[gbits - 1][idx];
188 :
189 : /* Update prediced gain with quantized correction */
190 106608 : fg_pred[band] *= (float) pow( 10, gain_dbq * 0.05f );
191 : }
192 : }
193 :
194 92424 : return;
195 : }
|