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 "prot.h"
36 : #include "ivas_prot.h"
37 : #include "rom_com.h"
38 : #include "ivas_rom_com.h"
39 : #include "ivas_cnst.h"
40 : #include <math.h>
41 : #include "wmc_auto.h"
42 :
43 : /*-------------------------------------------------------------------
44 : * sns_1st_dec()
45 : *
46 : *
47 : *-------------------------------------------------------------------*/
48 :
49 1059981 : static void sns_1st_dec(
50 : const int16_t index, /* i : codebook index */
51 : const int16_t core,
52 : const int16_t L_frame,
53 : float *snsq /* i/o: i:prediction o:quantized sns */
54 : )
55 : {
56 : int16_t i;
57 : const int16_t *p_dico, *means;
58 1059981 : const float cdbk_fix2float = 1.f / powf( 2, SNS_CDBKS_BITS_4_FRAC );
59 1059981 : const float means_fix2float = 1.f / powf( 2, SNS_MEANS_BITS_4_FRAC );
60 :
61 1059981 : means = NULL;
62 1059981 : switch ( L_frame )
63 : {
64 110388 : case L_FRAME16k:
65 110388 : means = &sns_1st_means_16k[core - 1][0];
66 110388 : break;
67 320415 : case L_FRAME25_6k:
68 320415 : means = &sns_1st_means_25k6[core - 1][0];
69 320415 : break;
70 629178 : case L_FRAME32k:
71 629178 : means = &sns_1st_means_32k[core - 1][0];
72 629178 : break;
73 0 : default:
74 0 : assert( !"illegal frame length in sns_1st_cod" );
75 : }
76 :
77 :
78 1059981 : p_dico = &sns_1st_cdbk[0][core - 1][0] + ( index % 32 ) * ( M / 2 );
79 :
80 9539829 : for ( i = 0; i < M / 2; i++ )
81 : {
82 8479848 : snsq[i] = ( *p_dico++ ) * cdbk_fix2float + means[i] * means_fix2float;
83 : }
84 :
85 1059981 : p_dico = &sns_1st_cdbk[1][core - 1][0] + ( index >> 5 ) * ( M / 2 );
86 :
87 9539829 : for ( i = M / 2; i < M; i++ )
88 : {
89 8479848 : snsq[i] = ( *p_dico++ ) * cdbk_fix2float + means[i] * means_fix2float;
90 : }
91 :
92 1059981 : return;
93 : }
94 :
95 :
96 : /*-------------------------------------------------------------------
97 : * sns_2st_dec()
98 : *
99 : *
100 : *-------------------------------------------------------------------*/
101 :
102 1573833 : static void sns_2st_dec(
103 : float *snsq, /* i/o: i:1st stage o:1st+2nd stage */
104 : int16_t *indx /* i : index[] (4 bits per words) */
105 : )
106 : {
107 : int16_t i;
108 : int16_t xq[M];
109 1573833 : float scale = 1.0f / 2.5f;
110 :
111 : /* quantize */
112 1573833 : AVQ_dec_lpc( indx, xq, 2 );
113 :
114 26755161 : for ( i = 0; i < M; i++ )
115 : {
116 25181328 : snsq[i] += scale * (float) xq[i];
117 : }
118 :
119 1573833 : return;
120 : }
121 :
122 :
123 : /*-------------------------------------------------------------------
124 : * sns_avq_dec()
125 : *
126 : * Stereo noise-shaping AVQ decoder for 1 channel
127 : *-------------------------------------------------------------------*/
128 :
129 222618 : void sns_avq_dec(
130 : int16_t *index, /* i : Quantization indices */
131 : float SNS_Q[NB_DIV][M], /* o : Quantized SNS vectors */
132 : const int16_t L_frame, /* i : frame length */
133 : const int16_t numlpc /* i : Number of sets of lpc */
134 : )
135 : {
136 : int16_t i, nbi, last;
137 : int16_t q_type;
138 :
139 : /* go from one-based indexing to zero-based indexing */
140 222618 : last = numlpc - 1;
141 :
142 222618 : index++;
143 :
144 : /* Decode last LPC */
145 222618 : sns_1st_dec( *index++, numlpc, L_frame, SNS_Q[last] );
146 222618 : sns_2st_dec( SNS_Q[last], index );
147 222618 : nbi = 2 + index[0] + index[1];
148 222618 : index += nbi;
149 :
150 : /* Decode intermediate LPC (512 framing) */
151 222618 : if ( numlpc == 2 )
152 : {
153 36486 : q_type = *index++;
154 :
155 36486 : if ( q_type == 0 )
156 : {
157 13518 : sns_1st_dec( *index++, numlpc, L_frame, SNS_Q[0] );
158 13518 : sns_2st_dec( SNS_Q[0], index );
159 : }
160 22968 : else if ( q_type == 1 )
161 : {
162 390456 : for ( i = 0; i < M; i++ )
163 : {
164 367488 : SNS_Q[0][i] = SNS_Q[1][i];
165 : }
166 22968 : sns_2st_dec( SNS_Q[0], index );
167 : }
168 : }
169 :
170 222618 : return;
171 : }
172 :
173 :
174 : /*-------------------------------------------------------------------
175 : * sns_avq_dec_stereo()
176 : *
177 : * Stereo noise-shaping AVQ decoder for 21 channels
178 : *-------------------------------------------------------------------*/
179 :
180 687453 : void sns_avq_dec_stereo(
181 : int16_t *indexl, /* i : Quantization indices (left channel) */
182 : int16_t *indexr, /* i : Quantization indices (right channe) */
183 : const int16_t L_frame, /* i : frame length */
184 : float *SNS_Ql, /* o : Quantized SNS vectors (left channel) */
185 : float *SNS_Qr /* o : Quantized SNS vectors (right channe) */
186 : )
187 : {
188 : int16_t i, stereo_mode;
189 : float mid_q[M], side_q[M];
190 :
191 687453 : stereo_mode = *indexl++;
192 687453 : indexr++;
193 :
194 687453 : if ( stereo_mode == 2 )
195 : {
196 : /* MS coding */
197 551061 : sns_1st_dec( *indexl++, TCX_20_CORE, L_frame, mid_q );
198 551061 : sns_2st_dec( mid_q, indexl );
199 :
200 9368037 : for ( i = 0; i < M; i++ )
201 : {
202 8816976 : side_q[i] = 0.f;
203 : }
204 :
205 551061 : if ( *indexr++ == -1 )
206 : {
207 490884 : sns_2st_dec( side_q, indexr );
208 : }
209 :
210 9368037 : for ( i = 0; i < M; i++ )
211 : {
212 8816976 : SNS_Ql[i] = mid_q[i] + side_q[i] * 0.5f;
213 8816976 : SNS_Qr[i] = mid_q[i] - side_q[i] * 0.5f;
214 : }
215 : }
216 : else
217 : {
218 : /* LR decoding */
219 136392 : sns_1st_dec( *indexl++, TCX_20_CORE, L_frame, SNS_Ql );
220 136392 : sns_2st_dec( SNS_Ql, indexl );
221 :
222 136392 : sns_1st_dec( *indexr++, TCX_20_CORE, L_frame, SNS_Qr );
223 136392 : sns_2st_dec( SNS_Qr, indexr );
224 : }
225 :
226 687453 : return;
227 : }
228 :
229 :
230 : /*-------------------------------------------------------------------
231 : * dequantize_sns()
232 : *
233 : * Dequantize SNS
234 : *-------------------------------------------------------------------*/
235 :
236 139920 : void dequantize_sns(
237 : int16_t indices[CPE_CHANNELS][NPRM_LPC_NEW],
238 : float snsQ_out[CPE_CHANNELS][NB_DIV][M],
239 : Decoder_State **sts )
240 : {
241 : int16_t nSubframes, k, ch;
242 : int16_t sns_stereo_mode[NB_DIV];
243 : int16_t zero_side_flag[NB_DIV];
244 : Decoder_State *st;
245 :
246 139920 : sns_stereo_mode[0] = indices[0][0];
247 139920 : sns_stereo_mode[1] = indices[0][1];
248 139920 : zero_side_flag[0] = indices[0][2];
249 139920 : zero_side_flag[1] = indices[0][3];
250 :
251 419760 : for ( ch = 0; ch < CPE_CHANNELS; ++ch )
252 : {
253 : int16_t idxIndices;
254 :
255 279840 : st = sts[ch];
256 279840 : nSubframes = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
257 279840 : idxIndices = 0;
258 :
259 566703 : for ( k = 0; k < nSubframes; ++k )
260 : {
261 286863 : const int16_t is_side = ch == 1 && sns_stereo_mode[k] == SNS_STEREO_MODE_MS;
262 286863 : const float *const *cdbks = ( nSubframes == 1 ) ? ivas_sns_cdbks_tcx20 : ivas_sns_cdbks_tcx10;
263 286863 : int16_t nStages = ( ( nSubframes == 1 ) ? SNS_MSVQ_NSTAGES_TCX20 : SNS_MSVQ_NSTAGES_TCX10 );
264 286863 : float *snsQ = snsQ_out[ch][k];
265 :
266 286863 : if ( is_side )
267 : {
268 132141 : const float *const *side_cdbks = ( st->core == TCX_20_CORE ) ? ivas_sns_cdbks_side_tcx20 : ivas_sns_cdbks_side_tcx10;
269 132141 : if ( zero_side_flag[k] )
270 : {
271 28296 : set_zero( snsQ, M );
272 28296 : continue;
273 : }
274 :
275 103845 : nStages = SNS_MSVQ_NSTAGES_SIDE;
276 103845 : msvq_dec( side_cdbks, NULL, NULL, nStages, M, M, &indices[ch][idxIndices + SNS_STEREO_MODE_OFFSET_INDICES], 0, NULL, snsQ, NULL );
277 : }
278 : else
279 : {
280 154722 : msvq_dec( cdbks, NULL, NULL, nStages, M, M, &indices[ch][idxIndices + SNS_STEREO_MODE_OFFSET_INDICES], 0, NULL, snsQ, NULL );
281 : }
282 258567 : idxIndices += nStages;
283 : }
284 : }
285 :
286 139920 : if ( sns_stereo_mode[0] == SNS_STEREO_MODE_MS || sns_stereo_mode[1] == SNS_STEREO_MODE_MS )
287 : {
288 129558 : nSubframes = ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV;
289 261987 : for ( k = 0; k < nSubframes; ++k )
290 : {
291 132429 : if ( sns_stereo_mode[k] == SNS_STEREO_MODE_MS )
292 : {
293 132141 : inverseMS( M, snsQ_out[0][k], snsQ_out[1][k], 1.f );
294 : }
295 : }
296 : }
297 :
298 139920 : return;
299 : }
|