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 "rom_com.h"
44 : #include "prot.h"
45 : #include "wmc_auto.h"
46 : #include <math.h> /* for cosf, sinf */
47 : #include <assert.h>
48 :
49 39172130 : static ivas_error get_edct_table(
50 : const float **edct_table,
51 : const int16_t length )
52 : {
53 : ivas_error error;
54 :
55 39172130 : error = IVAS_ERR_OK;
56 39172130 : *edct_table = NULL;
57 :
58 39172130 : switch ( length )
59 : {
60 90468 : case 1200:
61 90468 : *edct_table = edct_table_600;
62 90468 : break;
63 10402922 : case 960:
64 10402922 : *edct_table = edct_table_480;
65 10402922 : break;
66 10088563 : case 640:
67 10088563 : *edct_table = edct_table_320;
68 10088563 : break;
69 9540151 : case 320:
70 9540151 : *edct_table = edct_table_160;
71 9540151 : break;
72 2795250 : case 256:
73 2795250 : *edct_table = edct_table_128;
74 2795250 : break;
75 585776 : case 240:
76 585776 : *edct_table = edct_table_120;
77 585776 : break;
78 12 : case 200:
79 12 : *edct_table = edct_table_100;
80 12 : break;
81 896867 : case 160:
82 896867 : *edct_table = edct_table_80;
83 896867 : break;
84 96 : case 40:
85 96 : *edct_table = edct_table_20;
86 96 : break;
87 43233 : case 800:
88 43233 : *edct_table = edct_table_400;
89 43233 : break;
90 3574251 : case 512:
91 3574251 : *edct_table = edct_table_256;
92 3574251 : break;
93 277922 : case 480:
94 277922 : *edct_table = edct_table_240;
95 277922 : break;
96 130340 : case 400:
97 130340 : *edct_table = edct_table_200;
98 130340 : break;
99 271013 : case 128:
100 271013 : *edct_table = edct_table_64;
101 271013 : break;
102 475266 : case 80:
103 475266 : *edct_table = edct_table_40;
104 475266 : break;
105 : #ifdef DEBUGGING
106 : default:
107 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "edct/edst(): length is not in table!" );
108 : #endif
109 : }
110 :
111 39172130 : return error;
112 : }
113 :
114 :
115 : /*-----------------------------------------------------------------*
116 : * edct()
117 : *
118 : * DCT-IV transform
119 : *-----------------------------------------------------------------*/
120 :
121 36559225 : void edct(
122 : const float *x, /* i : input signal */
123 : float *y, /* o : output transform */
124 : const int16_t length, /* i : length */
125 : const int16_t element_mode /* i : IVAS element mode */
126 : )
127 : {
128 : int16_t i;
129 : float re[L_FRAME_PLUS / 2];
130 : float im[L_FRAME_PLUS / 2];
131 36559225 : const float *edct_table = 0;
132 : float local;
133 :
134 36559225 : get_edct_table( &edct_table, length );
135 :
136 : /* Twiddling and Pre-rotate */
137 10312593853 : for ( i = 0; i < length / 2; i++ )
138 : {
139 10276034628 : re[i] = x[2 * i] * edct_table[i] + x[length - 1 - 2 * i] * edct_table[length / 2 - 1 - i];
140 10276034628 : im[i] = x[length - 1 - 2 * i] * edct_table[i] - x[2 * i] * edct_table[length / 2 - 1 - i];
141 : }
142 :
143 36559225 : if ( element_mode == EVS_MONO )
144 : {
145 372864 : DoFFT( re, im, length / 2 );
146 : }
147 : else
148 : {
149 36186361 : fft( re, im, length / 2, 1 );
150 : }
151 :
152 36559225 : local = ( 0.75f * EVS_PI ) / length;
153 :
154 10312593853 : for ( i = 0; i < length / 2; i++ )
155 : {
156 : float tmp;
157 10276034628 : tmp = re[i] - im[i] * local; /* 3*pi/(4*length) is a constant */
158 10276034628 : im[i] = im[i] + re[i] * local;
159 10276034628 : re[i] = tmp;
160 : }
161 :
162 : /* Post-rotate and obtain the output data */
163 10312593853 : for ( i = 0; i < length / 2; i++ )
164 : {
165 10276034628 : y[2 * i] = re[i] * edct_table[i] + im[i] * edct_table[length / 2 - 1 - i];
166 10276034628 : y[length - 1 - 2 * i] = re[i] * edct_table[length / 2 - 1 - i] - im[i] * edct_table[i];
167 : }
168 :
169 36559225 : return;
170 : }
171 :
172 :
173 : /*-------------------------------------------------------------------------*
174 : * edst()
175 : *
176 : * DST-IV transform
177 : *-------------------------------------------------------------------------*/
178 :
179 2612905 : void edst(
180 : const float *x, /* i : input signal */
181 : float *y, /* o : output transform */
182 : const int16_t length, /* i : length */
183 : const int16_t element_mode /* i : IVAS element mode */
184 : )
185 : {
186 : int16_t i;
187 : float re[L_FRAME_PLUS / 2];
188 : float im[L_FRAME_PLUS / 2];
189 2612905 : const float *edct_table = 0;
190 : float local;
191 :
192 2612905 : get_edct_table( &edct_table, length );
193 :
194 : /* Twiddling and Pre-rotate */
195 1090289765 : for ( i = 0; i < length / 2; i++ )
196 : {
197 1087676860 : re[i] = x[length - 1 - 2 * i] * edct_table[i] + x[2 * i] * edct_table[length / 2 - 1 - i];
198 1087676860 : im[i] = x[2 * i] * edct_table[i] - x[length - 1 - 2 * i] * edct_table[length / 2 - 1 - i];
199 : }
200 :
201 2612905 : if ( element_mode == EVS_MONO )
202 : {
203 62821 : DoFFT( re, im, length / 2 );
204 : }
205 : else
206 : {
207 2550084 : fft( re, im, length / 2, 1 );
208 : }
209 :
210 2612905 : local = ( 0.75f * EVS_PI ) / length;
211 :
212 1090289765 : for ( i = 0; i < length / 2; i++ )
213 : {
214 : float tmp;
215 1087676860 : tmp = re[i] - im[i] * local; /* 3*pi/(4*length) is a constant */
216 1087676860 : im[i] = im[i] + re[i] * local;
217 1087676860 : re[i] = tmp;
218 : }
219 :
220 : /* Post-rotate and obtain the output data */
221 1090289765 : for ( i = 0; i < length / 2; i++ )
222 : {
223 1087676860 : y[2 * i] = re[i] * edct_table[i] + im[i] * edct_table[length / 2 - 1 - i];
224 1087676860 : y[length - 1 - 2 * i] = im[i] * edct_table[i] - re[i] * edct_table[length / 2 - 1 - i];
225 : }
226 :
227 2612905 : return;
228 : }
229 :
230 :
231 : /*-------------------------------------------------------------------------*
232 : * edxt()
233 : *
234 : * DCT/DST-II or III transform (currently also calculates DCT-IV and DST-IV)
235 : *-------------------------------------------------------------------------*/
236 :
237 6704 : void edxt(
238 : const float *x, /* i : input signal */
239 : float *y, /* o : output transform */
240 : const int16_t length, /* i : length */
241 : const uint16_t kernelType, /* i : kernel type (0 - 3) */
242 : const uint16_t synthesis /* i : nonzero for inverse */
243 : )
244 : {
245 6704 : const float pi_len = EVS_PI / length;
246 : int16_t k;
247 :
248 6704 : if ( kernelType == MDST_II || kernelType == MDCT_II )
249 6704 : {
250 6704 : const int16_t Nm1 = length - 1;
251 6704 : const float xSign = 2.f * ( kernelType >> 1 ) - 1.f;
252 6704 : const float scale = 0.5f * pi_len;
253 : float re[L_FRAME_PLUS];
254 : float im[L_FRAME_PLUS];
255 :
256 6704 : if ( !synthesis )
257 : {
258 211640 : for ( k = Nm1 >> 1; k >= 0; k-- ) /* pre-modulation of audio input */
259 : {
260 210960 : re[k] = x[2 * k];
261 210960 : re[Nm1 - k] = x[2 * k + 1] * xSign;
262 210960 : im[k] = im[Nm1 - k] = 0.f;
263 : }
264 :
265 680 : if ( length == 512 )
266 : {
267 0 : DoRTFTn( re, im, 512 );
268 : }
269 : else /* fft() doesn't support 512 */
270 : {
271 680 : fft( re, im, length, 1 );
272 : }
273 :
274 680 : if ( kernelType >> 1 )
275 : {
276 104800 : for ( k = Nm1 >> 1; k > 0; k-- )
277 : {
278 104461 : const float wRe = cosf( scale * k );
279 104461 : const float wIm = sinf( scale * k );
280 :
281 104461 : y[k] /*pt 1*/ = wRe * re[k] + wIm * im[k];
282 104461 : y[length - k] = wIm * re[k] - wRe * im[k];
283 : }
284 339 : y[length >> 1] = re[length >> 1] * sqrtf( 0.5f );
285 : }
286 : else /* forw. DST-II */
287 : {
288 106160 : for ( k = Nm1 >> 1; k > 0; k-- )
289 : {
290 105819 : const float wRe = cosf( scale * k );
291 105819 : const float wIm = sinf( scale * k );
292 :
293 105819 : y[Nm1 - k] = wRe * re[k] + wIm * im[k];
294 105819 : y[k - 1] = wIm * re[k] - wRe * im[k];
295 : }
296 341 : y[Nm1 >> 1] = re[length >> 1] * sqrtf( 0.5f );
297 : }
298 :
299 680 : y[Nm1 - Nm1 * ( kernelType >> 1 )] = re[0] * 0.5f;
300 : }
301 : else /* inverse II = III */
302 : {
303 6024 : if ( kernelType >> 1 )
304 : {
305 866240 : for ( k = Nm1 >> 1; k > 0; k-- )
306 : {
307 863086 : const float wRe = cosf( scale * k ) * 0.5f;
308 863086 : const float wIm = sinf( scale * k ) * 0.5f;
309 :
310 863086 : re[k] = wRe * x[k] + wIm * x[length - k];
311 863086 : im[k] = wRe * x[length - k] - wIm * x[k];
312 : }
313 3154 : re[length >> 1] = x[length >> 1] * sqrtf( 0.5f );
314 : }
315 : else /* DST type III */
316 : {
317 744560 : for ( k = Nm1 >> 1; k > 0; k-- )
318 : {
319 741690 : const float wRe = cosf( scale * k ) * 0.5f;
320 741690 : const float wIm = sinf( scale * k ) * 0.5f;
321 :
322 741690 : re[k] = wRe * x[Nm1 - k] + wIm * x[k - 1];
323 741690 : im[k] = wRe * x[k - 1] - wIm * x[Nm1 - k];
324 : }
325 2870 : re[length >> 1] = x[Nm1 >> 1] * sqrtf( 0.5f );
326 : }
327 :
328 6024 : re[0] = x[Nm1 - Nm1 * ( kernelType >> 1 )];
329 6024 : im[0] = im[length >> 1] = 0.f;
330 1610800 : for ( k = Nm1 >> 1; k > 0; k-- )
331 : {
332 1604776 : re[length - k] = re[k];
333 1604776 : im[length - k] = -im[k];
334 : }
335 :
336 6024 : if ( length == 512 )
337 : {
338 1914 : DoRTFTn( re, im, 512 );
339 : }
340 : else /* fft() doesn't support 512 */
341 : {
342 4110 : fft( re, im, length, 1 );
343 : }
344 :
345 1616824 : for ( k = Nm1 >> 1; k >= 0; k-- ) /* post-modulation of FFT output */
346 : {
347 1610800 : y[2 * k] = re[k];
348 1610800 : y[2 * k + 1] = re[Nm1 - k] * xSign;
349 : }
350 : }
351 : }
352 : else
353 : {
354 0 : assert( !"Unsupported Kernel type in edxt()" );
355 : }
356 :
357 6704 : v_multc( y, ( kernelType == MDCT_II ? -1.f : 1.f ) * sqrtf( 2.f / length ), y, length );
358 :
359 6704 : return;
360 : }
361 :
362 : /*-----------------------------------------------------------------*
363 : * iedct_short()
364 : *
365 : * Inverse EDCT for short frames
366 : *-----------------------------------------------------------------*/
367 :
368 31332 : void iedct_short(
369 : const float *in, /* i : input vector */
370 : float *out, /* o : output vector */
371 : const int16_t segment_length, /* i : length */
372 : const int16_t element_mode /* i : IVAS element mode */
373 : )
374 : {
375 : float alias[MAX_SEGMENT_LENGTH];
376 : int16_t i;
377 :
378 31332 : edct( in, alias, segment_length / 2, element_mode );
379 :
380 2249252 : for ( i = 0; i < segment_length / 4; i++ )
381 : {
382 2217920 : out[i] = alias[segment_length / 4 + i];
383 2217920 : out[segment_length / 4 + i] = -alias[segment_length / 2 - 1 - i];
384 2217920 : out[segment_length / 2 + i] = -alias[segment_length / 4 - 1 - i];
385 2217920 : out[3 * segment_length / 4 + i] = -alias[i];
386 : }
387 :
388 31332 : return;
389 : }
|