OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
imdct.c
Go to the documentation of this file.
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: RCSL 1.0/RPSL 1.0
3 *
4 * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
5 *
6 * The contents of this file, and the files included with this file, are
7 * subject to the current version of the RealNetworks Public Source License
8 * Version 1.0 (the "RPSL") available at
9 * http://www.helixcommunity.org/content/rpsl unless you have licensed
10 * the file under the RealNetworks Community Source License Version 1.0
11 * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
12 * in which case the RCSL will apply. You may also obtain the license terms
13 * directly from RealNetworks. You may not use this file except in
14 * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
15 * applicable to this file, the RCSL. Please see the applicable RPSL or
16 * RCSL for the rights, obligations and limitations governing use of the
17 * contents of the file.
18 *
19 * This file is part of the Helix DNA Technology. RealNetworks is the
20 * developer of the Original Code and owns the copyrights in the portions
21 * it created.
22 *
23 * This file, and the files included with this file, is distributed and made
24 * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
25 * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
26 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
27 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
28 *
29 * Technology Compatibility Kit Test Suite(s) Location:
30 * http://www.helixcommunity.org/content/tck
31 *
32 * Contributor(s):
33 *
34 * ***** END LICENSE BLOCK ***** */
35
36/**************************************************************************************
37 * Fixed-point MP3 decoder
38 * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
39 * June 2003
40 *
41 * imdct.c - antialias, inverse transform (short/long/mixed), windowing,
42 * overlap-add, frequency inversion
43 **************************************************************************************/
44
45#include "coder.h"
46#include "assembly.h"
47
48/**************************************************************************************
49 * Function: AntiAlias
50 *
51 * Description: smooth transition across DCT block boundaries (every 18 coefficients)
52 *
53 * Inputs: vector of dequantized coefficients, length = (nBfly+1) * 18
54 * number of "butterflies" to perform (one butterfly means one
55 * inter-block smoothing operation)
56 *
57 * Outputs: updated coefficient vector x
58 *
59 * Return: none
60 *
61 * Notes: weighted average of opposite bands (pairwise) from the 8 samples
62 * before and after each block boundary
63 * nBlocks = (nonZeroBound + 7) / 18, since nZB is the first ZERO sample
64 * above which all other samples are also zero
65 * max gain per sample = 1.372
66 * MAX(i) (abs(csa[i][0]) + abs(csa[i][1]))
67 * bits gained = 0
68 * assume at least 1 guard bit in x[] to avoid overflow
69 * (should be guaranteed from dequant, and max gain from stproc * max
70 * gain from AntiAlias < 2.0)
71 **************************************************************************************/
72// a little bit faster in RAM (< 1 ms per block)
73static void AntiAlias(int *x, int nBfly)
74{
75 int k, a0, b0, c0, c1;
76 const int *c;
77
78 /* csa = Q31 */
79 for (k = nBfly; k > 0; k--) {
80 c = csa[0];
81 x += 18;
82
83 a0 = x[-1]; c0 = *c; c++; b0 = x[0]; c1 = *c; c++;
84 x[-1] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
85 x[0] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
86
87 a0 = x[-2]; c0 = *c; c++; b0 = x[1]; c1 = *c; c++;
88 x[-2] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
89 x[1] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
90
91 a0 = x[-3]; c0 = *c; c++; b0 = x[2]; c1 = *c; c++;
92 x[-3] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
93 x[2] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
94
95 a0 = x[-4]; c0 = *c; c++; b0 = x[3]; c1 = *c; c++;
96 x[-4] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
97 x[3] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
98
99 a0 = x[-5]; c0 = *c; c++; b0 = x[4]; c1 = *c; c++;
100 x[-5] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
101 x[4] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
102
103 a0 = x[-6]; c0 = *c; c++; b0 = x[5]; c1 = *c; c++;
104 x[-6] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
105 x[5] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
106
107 a0 = x[-7]; c0 = *c; c++; b0 = x[6]; c1 = *c; c++;
108 x[-7] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
109 x[6] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
110
111 a0 = x[-8]; c0 = *c; c++; b0 = x[7]; c1 = *c; c++;
112 x[-8] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
113 x[7] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
114 }
115}
116
117/**************************************************************************************
118 * Function: WinPrevious
119 *
120 * Description: apply specified window to second half of previous IMDCT (overlap part)
121 *
122 * Inputs: vector of 9 coefficients (xPrev)
123 *
124 * Outputs: 18 windowed output coefficients (gain 1 integer bit)
125 * window type (0, 1, 2, 3)
126 *
127 * Return: none
128 *
129 * Notes: produces 9 output samples from 18 input samples via symmetry
130 * all blocks gain at least 1 guard bit via window (long blocks get extra
131 * sign bit, short blocks can have one addition but max gain < 1.0)
132 **************************************************************************************/
133static void WinPrevious(int *xPrev, int *xPrevWin, int btPrev)
134{
135 int i, x, *xp, *xpwLo, *xpwHi, wLo, wHi;
136 const int *wpLo, *wpHi;
137
138 xp = xPrev;
139 /* mapping (see IMDCT12x3): xPrev[0-2] = sum[6-8], xPrev[3-8] = sum[12-17] */
140 if (btPrev == 2) {
141 /* this could be reordered for minimum loads/stores */
142 wpLo = imdctWin[btPrev];
143 xPrevWin[ 0] = MULSHIFT32(wpLo[ 6], xPrev[2]) + MULSHIFT32(wpLo[0], xPrev[6]);
144 xPrevWin[ 1] = MULSHIFT32(wpLo[ 7], xPrev[1]) + MULSHIFT32(wpLo[1], xPrev[7]);
145 xPrevWin[ 2] = MULSHIFT32(wpLo[ 8], xPrev[0]) + MULSHIFT32(wpLo[2], xPrev[8]);
146 xPrevWin[ 3] = MULSHIFT32(wpLo[ 9], xPrev[0]) + MULSHIFT32(wpLo[3], xPrev[8]);
147 xPrevWin[ 4] = MULSHIFT32(wpLo[10], xPrev[1]) + MULSHIFT32(wpLo[4], xPrev[7]);
148 xPrevWin[ 5] = MULSHIFT32(wpLo[11], xPrev[2]) + MULSHIFT32(wpLo[5], xPrev[6]);
149 xPrevWin[ 6] = MULSHIFT32(wpLo[ 6], xPrev[5]);
150 xPrevWin[ 7] = MULSHIFT32(wpLo[ 7], xPrev[4]);
151 xPrevWin[ 8] = MULSHIFT32(wpLo[ 8], xPrev[3]);
152 xPrevWin[ 9] = MULSHIFT32(wpLo[ 9], xPrev[3]);
153 xPrevWin[10] = MULSHIFT32(wpLo[10], xPrev[4]);
154 xPrevWin[11] = MULSHIFT32(wpLo[11], xPrev[5]);
155 xPrevWin[12] = xPrevWin[13] = xPrevWin[14] = xPrevWin[15] = xPrevWin[16] = xPrevWin[17] = 0;
156 } else {
157 /* use ARM-style pointers (*ptr++) so that ADS compiles well */
158 wpLo = imdctWin[btPrev] + 18;
159 wpHi = wpLo + 17;
160 xpwLo = xPrevWin;
161 xpwHi = xPrevWin + 17;
162 for (i = 9; i > 0; i--) {
163 x = *xp++; wLo = *wpLo++; wHi = *wpHi--;
164 *xpwLo++ = MULSHIFT32(wLo, x);
165 *xpwHi-- = MULSHIFT32(wHi, x);
166 }
167 }
168}
169
170/**************************************************************************************
171 * Function: FreqInvertRescale
172 *
173 * Description: do frequency inversion (odd samples of odd blocks) and rescale
174 * if necessary (extra guard bits added before IMDCT)
175 *
176 * Inputs: output vector y (18 new samples, spaced NBANDS apart)
177 * previous sample vector xPrev (9 samples)
178 * index of current block
179 * number of extra shifts added before IMDCT (usually 0)
180 *
181 * Outputs: inverted and rescaled (as necessary) outputs
182 * rescaled (as necessary) previous samples
183 *
184 * Return: updated mOut (from new outputs y)
185 **************************************************************************************/
186static int FreqInvertRescale(int *y, int *xPrev, int blockIdx, int es)
187{
188 int i, d, mOut;
189 int y0, y1, y2, y3, y4, y5, y6, y7, y8;
190
191 if (es == 0) {
192 /* fast case - frequency invert only (no rescaling) - can fuse into overlap-add for speed, if desired */
193 if (blockIdx & 0x01) {
194 y += NBANDS;
195 y0 = *y; y += 2*NBANDS;
196 y1 = *y; y += 2*NBANDS;
197 y2 = *y; y += 2*NBANDS;
198 y3 = *y; y += 2*NBANDS;
199 y4 = *y; y += 2*NBANDS;
200 y5 = *y; y += 2*NBANDS;
201 y6 = *y; y += 2*NBANDS;
202 y7 = *y; y += 2*NBANDS;
203 y8 = *y; y += 2*NBANDS;
204
205 y -= 18*NBANDS;
206 *y = -y0; y += 2*NBANDS;
207 *y = -y1; y += 2*NBANDS;
208 *y = -y2; y += 2*NBANDS;
209 *y = -y3; y += 2*NBANDS;
210 *y = -y4; y += 2*NBANDS;
211 *y = -y5; y += 2*NBANDS;
212 *y = -y6; y += 2*NBANDS;
213 *y = -y7; y += 2*NBANDS;
214 *y = -y8; y += 2*NBANDS;
215 }
216 return 0;
217 } else {
218 /* undo pre-IMDCT scaling, clipping if necessary */
219 mOut = 0;
220 if (blockIdx & 0x01) {
221 /* frequency invert */
222 for (i = 0; i < 18; i+=2) {
223 d = *y; CLIP_2N(d, 31 - es); *y = d << es; mOut |= FASTABS(*y); y += NBANDS;
224 d = -*y; CLIP_2N(d, 31 - es); *y = d << es; mOut |= FASTABS(*y); y += NBANDS;
225 d = *xPrev; CLIP_2N(d, 31 - es); *xPrev++ = d << es;
226 }
227 } else {
228 for (i = 0; i < 18; i+=2) {
229 d = *y; CLIP_2N(d, 31 - es); *y = d << es; mOut |= FASTABS(*y); y += NBANDS;
230 d = *y; CLIP_2N(d, 31 - es); *y = d << es; mOut |= FASTABS(*y); y += NBANDS;
231 d = *xPrev; CLIP_2N(d, 31 - es); *xPrev++ = d << es;
232 }
233 }
234 return mOut;
235 }
236}
237
238/* format = Q31
239 * #define M_PI 3.14159265358979323846
240 * double u = 2.0 * M_PI / 9.0;
241 * float c0 = sqrt(3.0) / 2.0;
242 * float c1 = cos(u);
243 * float c2 = cos(2*u);
244 * float c3 = sin(u);
245 * float c4 = sin(2*u);
246 */
247
248static const int c9_0 = 0x6ed9eba1;
249static const int c9_1 = 0x620dbe8b;
250static const int c9_2 = 0x163a1a7e;
251static const int c9_3 = 0x5246dd49;
252static const int c9_4 = 0x7e0e2e32;
253
254/* format = Q31
255 * cos(((0:8) + 0.5) * (pi/18))
256 */
257static const int c18[9] = {
258 0x7f834ed0, 0x7ba3751d, 0x7401e4c1, 0x68d9f964, 0x5a82799a, 0x496af3e2, 0x36185aee, 0x2120fb83, 0x0b27eb5c,
259};
260
261/* require at least 3 guard bits in x[] to ensure no overflow */
262static __inline void idct9(int *x)
263{
264 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
265 int a10, a11, a12, a13, a14, a15, a16, a17, a18;
266 int a19, a20, a21, a22, a23, a24, a25, a26, a27;
267 int m1, m3, m5, m6, m7, m8, m9, m10, m11, m12;
268 int x0, x1, x2, x3, x4, x5, x6, x7, x8;
269
270 x0 = x[0]; x1 = x[1]; x2 = x[2]; x3 = x[3]; x4 = x[4];
271 x5 = x[5]; x6 = x[6]; x7 = x[7]; x8 = x[8];
272
273 a1 = x0 - x6;
274 a2 = x1 - x5;
275 a3 = x1 + x5;
276 a4 = x2 - x4;
277 a5 = x2 + x4;
278 a6 = x2 + x8;
279 a7 = x1 + x7;
280
281 a8 = a6 - a5; /* ie x[8] - x[4] */
282 a9 = a3 - a7; /* ie x[5] - x[7] */
283 a10 = a2 - x7; /* ie x[1] - x[5] - x[7] */
284 a11 = a4 - x8; /* ie x[2] - x[4] - x[8] */
285
286 /* do the << 1 as constant shifts where mX is actually used (free, no stall or extra inst.) */
287 m1 = MULSHIFT32(c9_0, x3);
288 m3 = MULSHIFT32(c9_0, a10);
289 m5 = MULSHIFT32(c9_1, a5);
290 m6 = MULSHIFT32(c9_2, a6);
291 m7 = MULSHIFT32(c9_1, a8);
292 m8 = MULSHIFT32(c9_2, a5);
293 m9 = MULSHIFT32(c9_3, a9);
294 m10 = MULSHIFT32(c9_4, a7);
295 m11 = MULSHIFT32(c9_3, a3);
296 m12 = MULSHIFT32(c9_4, a9);
297
298 a12 = x[0] + (x[6] >> 1);
299 a13 = a12 + ( m1 << 1);
300 a14 = a12 - ( m1 << 1);
301 a15 = a1 + ( a11 >> 1);
302 a16 = ( m5 << 1) + (m6 << 1);
303 a17 = ( m7 << 1) - (m8 << 1);
304 a18 = a16 + a17;
305 a19 = ( m9 << 1) + (m10 << 1);
306 a20 = (m11 << 1) - (m12 << 1);
307
308 a21 = a20 - a19;
309 a22 = a13 + a16;
310 a23 = a14 + a16;
311 a24 = a14 + a17;
312 a25 = a13 + a17;
313 a26 = a14 - a18;
314 a27 = a13 - a18;
315
316 x0 = a22 + a19; x[0] = x0;
317 x1 = a15 + (m3 << 1); x[1] = x1;
318 x2 = a24 + a20; x[2] = x2;
319 x3 = a26 - a21; x[3] = x3;
320 x4 = a1 - a11; x[4] = x4;
321 x5 = a27 + a21; x[5] = x5;
322 x6 = a25 - a20; x[6] = x6;
323 x7 = a15 - (m3 << 1); x[7] = x7;
324 x8 = a23 - a19; x[8] = x8;
325}
326
327/* let c(j) = cos(M_PI/36 * ((j)+0.5)), s(j) = sin(M_PI/36 * ((j)+0.5))
328 * then fastWin[2*j+0] = c(j)*(s(j) + c(j)), j = [0, 8]
329 * fastWin[2*j+1] = c(j)*(s(j) - c(j))
330 * format = Q30
331 */
332int fastWin36[18] = {
333 0x42aace8b, 0xc2e92724, 0x47311c28, 0xc95f619a, 0x4a868feb, 0xd0859d8c,
334 0x4c913b51, 0xd8243ea0, 0x4d413ccc, 0xe0000000, 0x4c913b51, 0xe7dbc161,
335 0x4a868feb, 0xef7a6275, 0x47311c28, 0xf6a09e67, 0x42aace8b, 0xfd16d8dd,
336};
337
338/**************************************************************************************
339 * Function: IMDCT36
340 *
341 * Description: 36-point modified DCT, with windowing and overlap-add (50% overlap)
342 *
343 * Inputs: vector of 18 coefficients (N/2 inputs produces N outputs, by symmetry)
344 * overlap part of last IMDCT (9 samples - see output comments)
345 * window type (0,1,2,3) of current and previous block
346 * current block index (for deciding whether to do frequency inversion)
347 * number of guard bits in input vector
348 *
349 * Outputs: 18 output samples, after windowing and overlap-add with last frame
350 * second half of (unwindowed) 36-point IMDCT - save for next time
351 * only save 9 xPrev samples, using symmetry (see WinPrevious())
352 *
353 * Notes: this is Ken's hyper-fast algorithm, including symmetric sin window
354 * optimization, if applicable
355 * total number of multiplies, general case:
356 * 2*10 (idct9) + 9 (last stage imdct) + 36 (for windowing) = 65
357 * total number of multiplies, btCurr == 0 && btPrev == 0:
358 * 2*10 (idct9) + 9 (last stage imdct) + 18 (for windowing) = 47
359 *
360 * blockType == 0 is by far the most common case, so it should be
361 * possible to use the fast path most of the time
362 * this is the fastest known algorithm for performing
363 * long IMDCT + windowing + overlap-add in MP3
364 *
365 * Return: mOut (OR of abs(y) for all y calculated here)
366 *
367 * TODO: optimize for ARM (reorder window coefs, ARM-style pointers in C,
368 * inline asm may or may not be helpful)
369 **************************************************************************************/
370// barely faster in RAM
371static int IMDCT36(int *xCurr, int *xPrev, int *y, int btCurr, int btPrev, int blockIdx, int gb)
372{
373 int i, es, xBuf[18], xPrevWin[18];
374 int acc1, acc2, s, d, t, mOut;
375 int xo, xe, c, *xp, yLo, yHi;
376 const int *cp, *wp;
377
378 acc1 = acc2 = 0;
379 xCurr += 17;
380
381 /* 7 gb is always adequate for antialias + accumulator loop + idct9 */
382 if (gb < 7) {
383 /* rarely triggered - 5% to 10% of the time on normal clips (with Q25 input) */
384 es = 7 - gb;
385 for (i = 8; i >= 0; i--) {
386 acc1 = ((*xCurr--) >> es) - acc1;
387 acc2 = acc1 - acc2;
388 acc1 = ((*xCurr--) >> es) - acc1;
389 xBuf[i+9] = acc2; /* odd */
390 xBuf[i+0] = acc1; /* even */
391 xPrev[i] >>= es;
392 }
393 } else {
394 es = 0;
395 /* max gain = 18, assume adequate guard bits */
396 for (i = 8; i >= 0; i--) {
397 acc1 = (*xCurr--) - acc1;
398 acc2 = acc1 - acc2;
399 acc1 = (*xCurr--) - acc1;
400 xBuf[i+9] = acc2; /* odd */
401 xBuf[i+0] = acc1; /* even */
402 }
403 }
404 /* xEven[0] and xOdd[0] scaled by 0.5 */
405 xBuf[9] >>= 1;
406 xBuf[0] >>= 1;
407
408 /* do 9-point IDCT on even and odd */
409 idct9(xBuf+0); /* even */
410 idct9(xBuf+9); /* odd */
411
412 xp = xBuf + 8;
413 cp = c18 + 8;
414 mOut = 0;
415 if (btPrev == 0 && btCurr == 0) {
416 /* fast path - use symmetry of sin window to reduce windowing multiplies to 18 (N/2) */
417 wp = fastWin36;
418 for (i = 0; i < 9; i++) {
419 /* do ARM-style pointer arithmetic (i still needed for y[] indexing - compiler spills if 2 y pointers) */
420 c = *cp--; xo = *(xp + 9); xe = *xp--;
421 /* gain 2 int bits here */
422 xo = MULSHIFT32(c, xo); /* 2*c18*xOdd (mul by 2 implicit in scaling) */
423 xe >>= 2;
424
425 s = -(*xPrev); /* sum from last block (always at least 2 guard bits) */
426 d = -(xe - xo); /* gain 2 int bits, don't shift xo (effective << 1 to eat sign bit, << 1 for mul by 2) */
427 (*xPrev++) = xe + xo; /* symmetry - xPrev[i] = xPrev[17-i] for long blocks */
428 t = s - d;
429
430 yLo = (d + (MULSHIFT32(t, *wp++) << 2));
431 yHi = (s + (MULSHIFT32(t, *wp++) << 2));
432 y[(i)*NBANDS] = yLo;
433 y[(17-i)*NBANDS] = yHi;
434 mOut |= FASTABS(yLo);
435 mOut |= FASTABS(yHi);
436 }
437 } else {
438 /* slower method - either prev or curr is using window type != 0 so do full 36-point window
439 * output xPrevWin has at least 3 guard bits (xPrev has 2, gain 1 in WinPrevious)
440 */
441 WinPrevious(xPrev, xPrevWin, btPrev);
442
443 wp = imdctWin[btCurr];
444 for (i = 0; i < 9; i++) {
445 c = *cp--; xo = *(xp + 9); xe = *xp--;
446 /* gain 2 int bits here */
447 xo = MULSHIFT32(c, xo); /* 2*c18*xOdd (mul by 2 implicit in scaling) */
448 xe >>= 2;
449
450 d = xe - xo;
451 (*xPrev++) = xe + xo; /* symmetry - xPrev[i] = xPrev[17-i] for long blocks */
452
453 yLo = (xPrevWin[i] + MULSHIFT32(d, wp[i])) << 2;
454 yHi = (xPrevWin[17-i] + MULSHIFT32(d, wp[17-i])) << 2;
455 y[(i)*NBANDS] = yLo;
456 y[(17-i)*NBANDS] = yHi;
457 mOut |= FASTABS(yLo);
458 mOut |= FASTABS(yHi);
459 }
460 }
461
462 xPrev -= 9;
463 mOut |= FreqInvertRescale(y, xPrev, blockIdx, es);
464
465 return mOut;
466}
467
468static int c3_0 = 0x6ed9eba1; /* format = Q31, cos(pi/6) */
469static int c6[3] = { 0x7ba3751d, 0x5a82799a, 0x2120fb83 }; /* format = Q31, cos(((0:2) + 0.5) * (pi/6)) */
470
471/* 12-point inverse DCT, used in IMDCT12x3()
472 * 4 input guard bits will ensure no overflow
473 */
474static __inline void imdct12 (int *x, int *out)
475{
476 int a0, a1, a2;
477 int x0, x1, x2, x3, x4, x5;
478
479 x0 = *x; x+=3; x1 = *x; x+=3;
480 x2 = *x; x+=3; x3 = *x; x+=3;
481 x4 = *x; x+=3; x5 = *x; x+=3;
482
483 x4 -= x5;
484 x3 -= x4;
485 x2 -= x3;
486 x3 -= x5;
487 x1 -= x2;
488 x0 -= x1;
489 x1 -= x3;
490
491 x0 >>= 1;
492 x1 >>= 1;
493
494 a0 = MULSHIFT32(c3_0, x2) << 1;
495 a1 = x0 + (x4 >> 1);
496 a2 = x0 - x4;
497 x0 = a1 + a0;
498 x2 = a2;
499 x4 = a1 - a0;
500
501 a0 = MULSHIFT32(c3_0, x3) << 1;
502 a1 = x1 + (x5 >> 1);
503 a2 = x1 - x5;
504
505 /* cos window odd samples, mul by 2, eat sign bit */
506 x1 = MULSHIFT32(c6[0], a1 + a0) << 2;
507 x3 = MULSHIFT32(c6[1], a2) << 2;
508 x5 = MULSHIFT32(c6[2], a1 - a0) << 2;
509
510 *out = x0 + x1; out++;
511 *out = x2 + x3; out++;
512 *out = x4 + x5; out++;
513 *out = x4 - x5; out++;
514 *out = x2 - x3; out++;
515 *out = x0 - x1;
516}
517
518/**************************************************************************************
519 * Function: IMDCT12x3
520 *
521 * Description: three 12-point modified DCT's for short blocks, with windowing,
522 * short block concatenation, and overlap-add
523 *
524 * Inputs: 3 interleaved vectors of 6 samples each
525 * (block0[0], block1[0], block2[0], block0[1], block1[1]....)
526 * overlap part of last IMDCT (9 samples - see output comments)
527 * window type (0,1,2,3) of previous block
528 * current block index (for deciding whether to do frequency inversion)
529 * number of guard bits in input vector
530 *
531 * Outputs: updated sample vector x, net gain of 1 integer bit
532 * second half of (unwindowed) IMDCT's - save for next time
533 * only save 9 xPrev samples, using symmetry (see WinPrevious())
534 *
535 * Return: mOut (OR of abs(y) for all y calculated here)
536 *
537 * TODO: optimize for ARM
538 **************************************************************************************/
539 // barely faster in RAM
540static int IMDCT12x3(int *xCurr, int *xPrev, int *y, int btPrev, int blockIdx, int gb)
541{
542 int i, es, mOut, yLo, xBuf[18], xPrevWin[18]; /* need temp buffer for reordering short blocks */
543 const int *wp;
544
545 es = 0;
546 /* 7 gb is always adequate for accumulator loop + idct12 + window + overlap */
547 if (gb < 7) {
548 es = 7 - gb;
549 for (i = 0; i < 18; i+=2) {
550 xCurr[i+0] >>= es;
551 xCurr[i+1] >>= es;
552 *xPrev++ >>= es;
553 }
554 xPrev -= 9;
555 }
556
557 /* requires 4 input guard bits for each imdct12 */
558 imdct12(xCurr + 0, xBuf + 0);
559 imdct12(xCurr + 1, xBuf + 6);
560 imdct12(xCurr + 2, xBuf + 12);
561
562 /* window previous from last time */
563 WinPrevious(xPrev, xPrevWin, btPrev);
564
565 /* could unroll this for speed, minimum loads (short blocks usually rare, so doesn't make much overall difference)
566 * xPrevWin[i] << 2 still has 1 gb always, max gain of windowed xBuf stuff also < 1.0 and gain the sign bit
567 * so y calculations won't overflow
568 */
569 wp = imdctWin[2];
570 mOut = 0;
571 for (i = 0; i < 3; i++) {
572 yLo = (xPrevWin[ 0+i] << 2);
573 mOut |= FASTABS(yLo); y[( 0+i)*NBANDS] = yLo;
574 yLo = (xPrevWin[ 3+i] << 2);
575 mOut |= FASTABS(yLo); y[( 3+i)*NBANDS] = yLo;
576 yLo = (xPrevWin[ 6+i] << 2) + (MULSHIFT32(wp[0+i], xBuf[3+i]));
577 mOut |= FASTABS(yLo); y[( 6+i)*NBANDS] = yLo;
578 yLo = (xPrevWin[ 9+i] << 2) + (MULSHIFT32(wp[3+i], xBuf[5-i]));
579 mOut |= FASTABS(yLo); y[( 9+i)*NBANDS] = yLo;
580 yLo = (xPrevWin[12+i] << 2) + (MULSHIFT32(wp[6+i], xBuf[2-i]) + MULSHIFT32(wp[0+i], xBuf[(6+3)+i]));
581 mOut |= FASTABS(yLo); y[(12+i)*NBANDS] = yLo;
582 yLo = (xPrevWin[15+i] << 2) + (MULSHIFT32(wp[9+i], xBuf[0+i]) + MULSHIFT32(wp[3+i], xBuf[(6+5)-i]));
583 mOut |= FASTABS(yLo); y[(15+i)*NBANDS] = yLo;
584 }
585
586 /* save previous (unwindowed) for overlap - only need samples 6-8, 12-17 */
587 for (i = 6; i < 9; i++)
588 *xPrev++ = xBuf[i] >> 2;
589 for (i = 12; i < 18; i++)
590 *xPrev++ = xBuf[i] >> 2;
591
592 xPrev -= 9;
593 mOut |= FreqInvertRescale(y, xPrev, blockIdx, es);
594
595 return mOut;
596}
597
598/**************************************************************************************
599 * Function: HybridTransform
600 *
601 * Description: IMDCT's, windowing, and overlap-add on long/short/mixed blocks
602 *
603 * Inputs: vector of input coefficients, length = nBlocksTotal * 18)
604 * vector of overlap samples from last time, length = nBlocksPrev * 9)
605 * buffer for output samples, length = MAXNSAMP
606 * SideInfoSub struct for this granule/channel
607 * BlockCount struct with necessary info
608 * number of non-zero input and overlap blocks
609 * number of long blocks in input vector (rest assumed to be short blocks)
610 * number of blocks which use long window (type) 0 in case of mixed block
611 * (bc->currWinSwitch, 0 for non-mixed blocks)
612 *
613 * Outputs: transformed, windowed, and overlapped sample buffer
614 * does frequency inversion on odd blocks
615 * updated buffer of samples for overlap
616 *
617 * Return: number of non-zero IMDCT blocks calculated in this call
618 * (including overlap-add)
619 *
620 * TODO: examine mixedBlock/winSwitch logic carefully (test he_mode.bit)
621 **************************************************************************************/
622static int HybridTransform(int *xCurr, int *xPrev, int y[BLOCK_SIZE][NBANDS], SideInfoSub *sis, BlockCount *bc)
623{
624 int xPrevWin[18], currWinIdx, prevWinIdx;
625 int i, j, nBlocksOut, nonZero, mOut;
626 int fiBit, xp;
627
628 ASSERT(bc->nBlocksLong <= NBANDS);
629 ASSERT(bc->nBlocksTotal <= NBANDS);
630 ASSERT(bc->nBlocksPrev <= NBANDS);
631
632 mOut = 0;
633
634 /* do long blocks, if any */
635 for(i = 0; i < bc->nBlocksLong; i++) {
636 /* currWinIdx picks the right window for long blocks (if mixed, long blocks use window type 0) */
637 currWinIdx = sis->blockType;
638 if (sis->mixedBlock && i < bc->currWinSwitch)
639 currWinIdx = 0;
640
641 prevWinIdx = bc->prevType;
642 if (i < bc->prevWinSwitch)
643 prevWinIdx = 0;
644
645 /* do 36-point IMDCT, including windowing and overlap-add */
646 mOut |= IMDCT36(xCurr, xPrev, &(y[0][i]), currWinIdx, prevWinIdx, i, bc->gbIn);
647 xCurr += 18;
648 xPrev += 9;
649 }
650
651 /* do short blocks (if any) */
652 for ( ; i < bc->nBlocksTotal; i++) {
653 ASSERT(sis->blockType == 2);
654
655 prevWinIdx = bc->prevType;
656 if (i < bc->prevWinSwitch)
657 prevWinIdx = 0;
658
659 mOut |= IMDCT12x3(xCurr, xPrev, &(y[0][i]), prevWinIdx, i, bc->gbIn);
660 xCurr += 18;
661 xPrev += 9;
662 }
663 nBlocksOut = i;
664
665 /* window and overlap prev if prev longer that current */
666 for ( ; i < bc->nBlocksPrev; i++) {
667 prevWinIdx = bc->prevType;
668 if (i < bc->prevWinSwitch)
669 prevWinIdx = 0;
670 WinPrevious(xPrev, xPrevWin, prevWinIdx);
671
672 nonZero = 0;
673 fiBit = i << 31;
674 for (j = 0; j < 9; j++) {
675 xp = xPrevWin[2*j+0] << 2; /* << 2 temp for scaling */
676 nonZero |= xp;
677 y[2*j+0][i] = xp;
678 mOut |= FASTABS(xp);
679
680 /* frequency inversion on odd blocks/odd samples (flip sign if i odd, j odd) */
681 xp = xPrevWin[2*j+1] << 2;
682 xp = (xp ^ (fiBit >> 31)) + (i & 0x01);
683 nonZero |= xp;
684 y[2*j+1][i] = xp;
685 mOut |= FASTABS(xp);
686
687 xPrev[j] = 0;
688 }
689 xPrev += 9;
690 if (nonZero)
691 nBlocksOut = i;
692 }
693
694 /* clear rest of blocks */
695 for ( ; i < 32; i++) {
696 for (j = 0; j < 18; j++)
697 y[j][i] = 0;
698 }
699
700 bc->gbOut = CLZ(mOut) - 1;
701
702 return nBlocksOut;
703}
704
705/**************************************************************************************
706 * Function: IMDCT
707 *
708 * Description: do alias reduction, inverse MDCT, overlap-add, and frequency inversion
709 *
710 * Inputs: MP3DecInfo structure filled by UnpackFrameHeader(), UnpackSideInfo(),
711 * UnpackScaleFactors(), and DecodeHuffman() (for this granule, channel)
712 * includes PCM samples in overBuf (from last call to IMDCT) for OLA
713 * index of current granule and channel
714 *
715 * Outputs: PCM samples in outBuf, for input to subband transform
716 * PCM samples in overBuf, for OLA next time
717 * updated hi->nonZeroBound index for this channel
718 *
719 * Return: 0 on success, -1 if null input pointers
720 **************************************************************************************/
721 // a bit faster in RAM
722int IMDCT(MP3DecInfo *mp3DecInfo, int gr, int ch)
723{
724 int nBfly, blockCutoff;
725 FrameHeader *fh;
726 SideInfo *si;
727 HuffmanInfo *hi;
728 IMDCTInfo *mi;
729 BlockCount bc;
730
731 /* validate pointers */
732 if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || !mp3DecInfo->SideInfoPS ||
733 !mp3DecInfo->HuffmanInfoPS || !mp3DecInfo->IMDCTInfoPS)
734 return -1;
735
736 /* si is an array of up to 4 structs, stored as gr0ch0, gr0ch1, gr1ch0, gr1ch1 */
737 fh = (FrameHeader *)(mp3DecInfo->FrameHeaderPS);
738 si = (SideInfo *)(mp3DecInfo->SideInfoPS);
739 hi = (HuffmanInfo*)(mp3DecInfo->HuffmanInfoPS);
740 mi = (IMDCTInfo *)(mp3DecInfo->IMDCTInfoPS);
741
742 /* anti-aliasing done on whole long blocks only
743 * for mixed blocks, nBfly always 1, except 3 for 8 kHz MPEG 2.5 (see sfBandTab)
744 * nLongBlocks = number of blocks with (possibly) non-zero power
745 * nBfly = number of butterflies to do (nLongBlocks - 1, unless no long blocks)
746 */
747 blockCutoff = fh->sfBand->l[(fh->ver == MPEG1 ? 8 : 6)] / 18; /* same as 3* num short sfb's in spec */
748 if (si->sis[gr][ch].blockType != 2) {
749 /* all long transforms */
750 bc.nBlocksLong = MIN((hi->nonZeroBound[ch] + 7) / 18 + 1, 32);
751 nBfly = bc.nBlocksLong - 1;
752 } else if (si->sis[gr][ch].blockType == 2 && si->sis[gr][ch].mixedBlock) {
753 /* mixed block - long transforms until cutoff, then short transforms */
754 bc.nBlocksLong = blockCutoff;
755 nBfly = bc.nBlocksLong - 1;
756 } else {
757 /* all short transforms */
758 bc.nBlocksLong = 0;
759 nBfly = 0;
760 }
761
762 AntiAlias(hi->huffDecBuf[ch], nBfly);
763 hi->nonZeroBound[ch] = MAX(hi->nonZeroBound[ch], (nBfly * 18) + 8);
764
765 ASSERT(hi->nonZeroBound[ch] <= MAX_NSAMP);
766
767 /* for readability, use a struct instead of passing a million parameters to HybridTransform() */
768 bc.nBlocksTotal = (hi->nonZeroBound[ch] + 17) / 18;
769 bc.nBlocksPrev = mi->numPrevIMDCT[ch];
770 bc.prevType = mi->prevType[ch];
771 bc.prevWinSwitch = mi->prevWinSwitch[ch];
772 bc.currWinSwitch = (si->sis[gr][ch].mixedBlock ? blockCutoff : 0); /* where WINDOW switches (not nec. transform) */
773 bc.gbIn = hi->gb[ch];
774
775 mi->numPrevIMDCT[ch] = HybridTransform(hi->huffDecBuf[ch], mi->overBuf[ch], mi->outBuf[ch], &si->sis[gr][ch], &bc);
776 mi->prevType[ch] = si->sis[gr][ch].blockType;
777 mi->prevWinSwitch[ch] = bc.currWinSwitch; /* 0 means not a mixed block (either all short or all long) */
778 mi->gb[ch] = bc.gbOut;
779
780 ASSERT(mi->numPrevIMDCT[ch] <= NBANDS);
781
782 /* output has gained 2 int bits */
783 return 0;
784}
UINT16 y
Definition BmfFile.h:84
UINT16 x
Definition BmfFile.h:83
#define NBANDS
Definition coder.h:88
#define CLIP_2N(y, n)
Definition coder.h:67
#define ASSERT(x)
Definition coder.h:55
#define BLOCK_SIZE
Definition coder.h:87
#define imdctWin
Definition coder.h:111
#define csa
Definition coder.h:110
#define MAX(a, b)
Definition coder.h:59
#define MIN(a, b)
Definition deflate.c:1673
int fastWin36[18]
Definition imdct.c:332
@ MPEG1
Definition mp3dec.h:84
#define MAX_NSAMP
Definition mp3dec.h:80
#define IMDCT
Definition statname.h:70
int nBlocksTotal
Definition coder.h:222
int nBlocksPrev
Definition coder.h:223
int prevWinSwitch
Definition coder.h:225
int gbOut
Definition coder.h:228
int gbIn
Definition coder.h:227
int currWinSwitch
Definition coder.h:226
int prevType
Definition coder.h:224
int nBlocksLong
Definition coder.h:221
const SFBandTable * sfBand
Definition coder.h:150
MPEGVersion ver
Definition coder.h:136
int nonZeroBound[MAX_NCHAN]
Definition coder.h:192
int huffDecBuf[MAX_NCHAN][MAX_NSAMP]
Definition coder.h:191
int gb[MAX_NCHAN]
Definition coder.h:193
int gb[MAX_NCHAN]
Definition coder.h:217
int overBuf[MAX_NCHAN][MAX_NSAMP/2]
Definition coder.h:213
int numPrevIMDCT[MAX_NCHAN]
Definition coder.h:214
int prevType[MAX_NCHAN]
Definition coder.h:215
int prevWinSwitch[MAX_NCHAN]
Definition coder.h:216
int outBuf[MAX_NCHAN][BLOCK_SIZE][NBANDS]
Definition coder.h:212
void * IMDCTInfoPS
Definition mp3common.h:71
void * FrameHeaderPS
Definition mp3common.h:66
void * SideInfoPS
Definition mp3common.h:67
void * HuffmanInfoPS
Definition mp3common.h:69
short l[23]
Definition mp3common.h:99
SideInfoSub sis[MAX_NGRAN][MAX_NCHAN]
Definition coder.h:175
int mixedBlock
Definition coder.h:160
int blockType
Definition coder.h:159