2003-08-30 22:30:28 +00:00
|
|
|
|
/*
|
|
|
|
|
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
2004-06-02 22:59:04 +00:00
|
|
|
|
** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
|
2003-08-30 22:30:28 +00:00
|
|
|
|
**
|
|
|
|
|
** This program is free software; you can redistribute it and/or modify
|
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
|
** the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
** (at your option) any later version.
|
|
|
|
|
**
|
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
|
**
|
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
|
** along with this program; if not, write to the Free Software
|
|
|
|
|
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
**
|
|
|
|
|
** Any non-GPL usage of this software or parts of this software is strictly
|
|
|
|
|
** forbidden.
|
|
|
|
|
**
|
|
|
|
|
** Commercial non-GPL licensing of this software is possible.
|
|
|
|
|
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
|
|
|
**
|
2004-06-23 13:50:53 +00:00
|
|
|
|
** Initially modified for use with MPlayer by Arpad Gere<EFBFBD>ffy on 2003/08/30
|
2004-09-24 17:31:36 +00:00
|
|
|
|
** $Id: mdct.c,v 1.4 2004/06/23 13:50:51 diego Exp $
|
2004-06-23 13:50:53 +00:00
|
|
|
|
** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
|
2003-08-30 22:30:28 +00:00
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Fast (I)MDCT Implementation using (I)FFT ((Inverse) Fast Fourier Transform)
|
|
|
|
|
* and consists of three steps: pre-(I)FFT complex multiplication, complex
|
|
|
|
|
* (I)FFT, post-(I)FFT complex multiplication,
|
|
|
|
|
*
|
|
|
|
|
* As described in:
|
|
|
|
|
* P. Duhamel, Y. Mahieux, and J.P. Petit, "A Fast Algorithm for the
|
|
|
|
|
* Implementation of Filter Banks Based on 'Time Domain Aliasing
|
|
|
|
|
* Cancellation<EFBFBD>," IEEE Proc. on ICASSP<53>91, 1991, pp. 2209-2212.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* As of April 6th 2002 completely rewritten.
|
|
|
|
|
* This (I)MDCT can now be used for any data size n, where n is divisible by 8.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
#include "structs.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
|
#define assert(x)
|
|
|
|
|
#else
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "cfft.h"
|
|
|
|
|
#include "mdct.h"
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#include "mdct_tab.h"
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mdct_info *faad_mdct_init(uint16_t N)
|
|
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
|
mdct_info *mdct = (mdct_info*)faad_malloc(sizeof(mdct_info));
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
|
|
assert(N % 8 == 0);
|
|
|
|
|
|
|
|
|
|
mdct->N = N;
|
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
|
/* NOTE: For "small framelengths" in FIXED_POINT the coefficients need to be
|
|
|
|
|
* scaled by sqrt("(nearest power of 2) > N" / N) */
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
|
/* RE(mdct->sincos[k]) = scale*(real_t)(cos(2.0*M_PI*(k+1./8.) / (real_t)N));
|
|
|
|
|
* IM(mdct->sincos[k]) = scale*(real_t)(sin(2.0*M_PI*(k+1./8.) / (real_t)N)); */
|
|
|
|
|
/* scale is 1 for fixed point, sqrt(N) for floating point */
|
|
|
|
|
switch (N)
|
2003-08-30 22:30:28 +00:00
|
|
|
|
{
|
2004-09-24 17:31:36 +00:00
|
|
|
|
case 2048: mdct->sincos = (complex_t*)mdct_tab_2048; break;
|
|
|
|
|
case 256: mdct->sincos = (complex_t*)mdct_tab_256; break;
|
|
|
|
|
#ifdef LD_DEC
|
|
|
|
|
case 1024: mdct->sincos = (complex_t*)mdct_tab_1024; break;
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef ALLOW_SMALL_FRAMELENGTH
|
|
|
|
|
case 1920: mdct->sincos = (complex_t*)mdct_tab_1920; break;
|
|
|
|
|
case 240: mdct->sincos = (complex_t*)mdct_tab_240; break;
|
|
|
|
|
#ifdef LD_DEC
|
|
|
|
|
case 960: mdct->sincos = (complex_t*)mdct_tab_960; break;
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SSR_DEC
|
|
|
|
|
case 512: mdct->sincos = (complex_t*)mdct_tab_512; break;
|
|
|
|
|
case 64: mdct->sincos = (complex_t*)mdct_tab_64; break;
|
2003-10-03 22:23:26 +00:00
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* initialise fft */
|
|
|
|
|
mdct->cfft = cffti(N/4);
|
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
#ifdef PROFILE
|
|
|
|
|
mdct->cycles = 0;
|
|
|
|
|
mdct->fft_cycles = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
|
return mdct;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void faad_mdct_end(mdct_info *mdct)
|
|
|
|
|
{
|
|
|
|
|
if (mdct != NULL)
|
|
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
|
#ifdef PROFILE
|
|
|
|
|
printf("MDCT[%.4d]: %I64d cycles\n", mdct->N, mdct->cycles);
|
|
|
|
|
printf("CFFT[%.4d]: %I64d cycles\n", mdct->N/4, mdct->fft_cycles);
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
|
cfftu(mdct->cfft);
|
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
faad_free(mdct);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void faad_imdct(mdct_info *mdct, real_t *X_in, real_t *X_out)
|
|
|
|
|
{
|
|
|
|
|
uint16_t k;
|
|
|
|
|
|
|
|
|
|
complex_t x;
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#ifdef ALLOW_SMALL_FRAMELENGTH
|
|
|
|
|
#ifdef FIXED_POINT
|
|
|
|
|
real_t scale, b_scale = 0;
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
2004-06-02 22:59:04 +00:00
|
|
|
|
ALIGN complex_t Z1[512];
|
2003-08-30 22:30:28 +00:00
|
|
|
|
complex_t *sincos = mdct->sincos;
|
|
|
|
|
|
|
|
|
|
uint16_t N = mdct->N;
|
|
|
|
|
uint16_t N2 = N >> 1;
|
|
|
|
|
uint16_t N4 = N >> 2;
|
|
|
|
|
uint16_t N8 = N >> 3;
|
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
#ifdef PROFILE
|
|
|
|
|
int64_t count1, count2 = faad_get_ts();
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#ifdef ALLOW_SMALL_FRAMELENGTH
|
|
|
|
|
#ifdef FIXED_POINT
|
|
|
|
|
/* detect non-power of 2 */
|
|
|
|
|
if (N & (N-1))
|
|
|
|
|
{
|
|
|
|
|
/* adjust scale for non-power of 2 MDCT */
|
|
|
|
|
/* 2048/1920 */
|
|
|
|
|
b_scale = 1;
|
|
|
|
|
scale = COEF_CONST(1.0666666666666667);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
|
/* pre-IFFT complex multiplication */
|
|
|
|
|
for (k = 0; k < N4; k++)
|
|
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
|
ComplexMult(&IM(Z1[k]), &RE(Z1[k]),
|
|
|
|
|
X_in[2*k], X_in[N2 - 1 - 2*k], RE(sincos[k]), IM(sincos[k]));
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
#ifdef PROFILE
|
|
|
|
|
count1 = faad_get_ts();
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
/* complex IFFT, any non-scaling FFT can be used here */
|
2003-08-30 22:30:28 +00:00
|
|
|
|
cfftb(mdct->cfft, Z1);
|
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
#ifdef PROFILE
|
|
|
|
|
count1 = faad_get_ts() - count1;
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
|
/* post-IFFT complex multiplication */
|
|
|
|
|
for (k = 0; k < N4; k++)
|
|
|
|
|
{
|
|
|
|
|
RE(x) = RE(Z1[k]);
|
|
|
|
|
IM(x) = IM(Z1[k]);
|
2004-06-02 22:59:04 +00:00
|
|
|
|
ComplexMult(&IM(Z1[k]), &RE(Z1[k]),
|
|
|
|
|
IM(x), RE(x), RE(sincos[k]), IM(sincos[k]));
|
2004-09-24 17:31:36 +00:00
|
|
|
|
|
|
|
|
|
#ifdef ALLOW_SMALL_FRAMELENGTH
|
|
|
|
|
#ifdef FIXED_POINT
|
|
|
|
|
/* non-power of 2 MDCT scaling */
|
|
|
|
|
if (b_scale)
|
|
|
|
|
{
|
|
|
|
|
RE(Z1[k]) = MUL_C(RE(Z1[k]), scale);
|
|
|
|
|
IM(Z1[k]) = MUL_C(IM(Z1[k]), scale);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* reordering */
|
2004-06-02 22:59:04 +00:00
|
|
|
|
for (k = 0; k < N8; k+=2)
|
2003-08-30 22:30:28 +00:00
|
|
|
|
{
|
2003-10-03 22:23:26 +00:00
|
|
|
|
X_out[ 2*k] = IM(Z1[N8 + k]);
|
2004-06-02 22:59:04 +00:00
|
|
|
|
X_out[ 2 + 2*k] = IM(Z1[N8 + 1 + k]);
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
X_out[ 1 + 2*k] = -RE(Z1[N8 - 1 - k]);
|
2004-06-02 22:59:04 +00:00
|
|
|
|
X_out[ 3 + 2*k] = -RE(Z1[N8 - 2 - k]);
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
X_out[N4 + 2*k] = RE(Z1[ k]);
|
2004-06-02 22:59:04 +00:00
|
|
|
|
X_out[N4 + + 2 + 2*k] = RE(Z1[ 1 + k]);
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
X_out[N4 + 1 + 2*k] = -IM(Z1[N4 - 1 - k]);
|
2004-06-02 22:59:04 +00:00
|
|
|
|
X_out[N4 + 3 + 2*k] = -IM(Z1[N4 - 2 - k]);
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
X_out[N2 + 2*k] = RE(Z1[N8 + k]);
|
2004-06-02 22:59:04 +00:00
|
|
|
|
X_out[N2 + + 2 + 2*k] = RE(Z1[N8 + 1 + k]);
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
X_out[N2 + 1 + 2*k] = -IM(Z1[N8 - 1 - k]);
|
2004-06-02 22:59:04 +00:00
|
|
|
|
X_out[N2 + 3 + 2*k] = -IM(Z1[N8 - 2 - k]);
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
X_out[N2 + N4 + 2*k] = -IM(Z1[ k]);
|
2004-06-02 22:59:04 +00:00
|
|
|
|
X_out[N2 + N4 + 2 + 2*k] = -IM(Z1[ 1 + k]);
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
X_out[N2 + N4 + 1 + 2*k] = RE(Z1[N4 - 1 - k]);
|
2004-06-02 22:59:04 +00:00
|
|
|
|
X_out[N2 + N4 + 3 + 2*k] = RE(Z1[N4 - 2 - k]);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
2004-06-02 22:59:04 +00:00
|
|
|
|
|
|
|
|
|
#ifdef PROFILE
|
|
|
|
|
count2 = faad_get_ts() - count2;
|
|
|
|
|
mdct->fft_cycles += count1;
|
|
|
|
|
mdct->cycles += (count2 - count1);
|
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef LTP_DEC
|
|
|
|
|
void faad_mdct(mdct_info *mdct, real_t *X_in, real_t *X_out)
|
|
|
|
|
{
|
|
|
|
|
uint16_t k;
|
|
|
|
|
|
|
|
|
|
complex_t x;
|
2004-06-02 22:59:04 +00:00
|
|
|
|
ALIGN complex_t Z1[512];
|
2003-08-30 22:30:28 +00:00
|
|
|
|
complex_t *sincos = mdct->sincos;
|
|
|
|
|
|
|
|
|
|
uint16_t N = mdct->N;
|
|
|
|
|
uint16_t N2 = N >> 1;
|
|
|
|
|
uint16_t N4 = N >> 2;
|
|
|
|
|
uint16_t N8 = N >> 3;
|
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
#ifndef FIXED_POINT
|
2003-08-30 22:30:28 +00:00
|
|
|
|
real_t scale = REAL_CONST(N);
|
2004-06-02 22:59:04 +00:00
|
|
|
|
#else
|
|
|
|
|
real_t scale = REAL_CONST(4.0/N);
|
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#ifdef ALLOW_SMALL_FRAMELENGTH
|
|
|
|
|
#ifdef FIXED_POINT
|
|
|
|
|
/* detect non-power of 2 */
|
|
|
|
|
if (N & (N-1))
|
|
|
|
|
{
|
|
|
|
|
/* adjust scale for non-power of 2 MDCT */
|
|
|
|
|
/* *= sqrt(2048/1920) */
|
|
|
|
|
scale = MUL_C(scale, COEF_CONST(1.0327955589886444));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
|
/* pre-FFT complex multiplication */
|
|
|
|
|
for (k = 0; k < N8; k++)
|
|
|
|
|
{
|
|
|
|
|
uint16_t n = k << 1;
|
|
|
|
|
RE(x) = X_in[N - N4 - 1 - n] + X_in[N - N4 + n];
|
|
|
|
|
IM(x) = X_in[ N4 + n] - X_in[ N4 - 1 - n];
|
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
ComplexMult(&RE(Z1[k]), &IM(Z1[k]),
|
|
|
|
|
RE(x), IM(x), RE(sincos[k]), IM(sincos[k]));
|
|
|
|
|
|
|
|
|
|
RE(Z1[k]) = MUL_R(RE(Z1[k]), scale);
|
|
|
|
|
IM(Z1[k]) = MUL_R(IM(Z1[k]), scale);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
|
|
RE(x) = X_in[N2 - 1 - n] - X_in[ n];
|
|
|
|
|
IM(x) = X_in[N2 + n] + X_in[N - 1 - n];
|
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
ComplexMult(&RE(Z1[k + N8]), &IM(Z1[k + N8]),
|
|
|
|
|
RE(x), IM(x), RE(sincos[k + N8]), IM(sincos[k + N8]));
|
|
|
|
|
|
|
|
|
|
RE(Z1[k + N8]) = MUL_R(RE(Z1[k + N8]), scale);
|
|
|
|
|
IM(Z1[k + N8]) = MUL_R(IM(Z1[k + N8]), scale);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
/* complex FFT, any non-scaling FFT can be used here */
|
2003-08-30 22:30:28 +00:00
|
|
|
|
cfftf(mdct->cfft, Z1);
|
|
|
|
|
|
|
|
|
|
/* post-FFT complex multiplication */
|
|
|
|
|
for (k = 0; k < N4; k++)
|
|
|
|
|
{
|
|
|
|
|
uint16_t n = k << 1;
|
2004-06-02 22:59:04 +00:00
|
|
|
|
ComplexMult(&RE(x), &IM(x),
|
|
|
|
|
RE(Z1[k]), IM(Z1[k]), RE(sincos[k]), IM(sincos[k]));
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
X_out[ n] = -RE(x);
|
|
|
|
|
X_out[N2 - 1 - n] = IM(x);
|
|
|
|
|
X_out[N2 + n] = -IM(x);
|
|
|
|
|
X_out[N - 1 - n] = RE(x);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|