1 /********************************************************************
3 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS DISTRIBUTING. *
8 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999 *
9 * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: modified discrete cosine transform
15 power of two length transform only [16 <= n ]
17 author: Monty <xiphmont@mit.edu>
18 modifications by: Monty
19 last modification date: Oct 10 1999
21 Algorithm adapted from _The use of multirate filter banks for coding
22 of high quality digital audio_, by T. Sporer, K. Brandenburg and
23 B. Edler, collection of the European Signal Processing Conference
24 (EUSIPCO), Amsterdam, June 1992, Vol.1, pp 211-214
26 Note that the below code won't make much sense without the paper;
27 The presented algorithm was already fairly polished, and the code
28 once followed it closely. The current code both corrects several
29 typos in the paper and goes beyond the presented optimizations
30 (steps 4 through 6 are, for example, entirely eliminated).
32 This module DOES NOT INCLUDE code to generate the window function.
33 Everybody has their own weird favorite including me... I happen to
34 like the properties of y=sin(2PI*sin^2(x)), but others may vehemently
37 ********************************************************************/
46 /* build lookups for trig functions; also pre-figure scaling and
47 some window function algebra. */
49 void mdct_init(mdct_lookup *lookup,int n){
50 int *bitrev=malloc(sizeof(int)*(n/4));
51 double *trig=malloc(sizeof(double)*(n+n/4));
60 int log2n=lookup->log2n=rint(log(n)/log(2));
63 lookup->bitrev=bitrev;
68 AE[i*2]=cos((M_PI/n)*(4*i));
69 AO[i*2]=-sin((M_PI/n)*(4*i));
70 BE[i*2]=cos((M_PI/(2*n))*(2*i+1));
71 BO[i*2]=sin((M_PI/(2*n))*(2*i+1));
74 CE[i*2]=cos((M_PI/n)*(4*i+2));
75 CO[i*2]=-sin((M_PI/n)*(4*i+2));
78 /* bitreverse lookup... */
81 int mask=(1<<(log2n-1))-1,i,j;
86 if((msb>>j)&i)acc|=1<<j;
87 bitrev[i*2]=((~acc)&mask);
93 void mdct_clear(mdct_lookup *l){
95 if(l->trig)free(l->trig);
96 if(l->bitrev)free(l->bitrev);
97 memset(l,0,sizeof(mdct_lookup));
101 static double *_mdct_kernel(double *x, double *w,
102 int n, int n2, int n4, int n8,
111 double *A=init->trig+n2;
116 w2[i]= *xA++ + *xB++;
122 w[i++]= x0 * A[0] + x1 * A[1];
123 w[i]= x1 * A[0] - x0 * A[1];
125 w2[i++]= *xA++ + *xB++;
134 for(i=0;i<init->log2n-3;i++){
138 double *A=init->trig;
141 for(r=0;r<(k0>>2);r++){
149 for(s=0;s<(2<<i);s++){
153 wA =w[++w1] -w[++w2];
156 x[w2] =wA*AEv - wB*AOv;
157 x[w2-1]=wB*AEv + wA*AOv;
173 /* step 4, 5, 6, 7 */
175 double *C=init->trig+n;
176 int *bit=init->bitrev;
183 double wA=w[t1]-w[t2+1];
184 double wB=w[t1-1]+w[t2];
185 double wC=w[t1]+w[t2+1];
186 double wD=w[t1-1]-w[t2];
189 double wBCE=wB* *C++;
191 double wBCO=wB* *C++;
193 *x1++=( wC+wACO+wBCE)*.5;
194 *x2--=(-wD+wBCO-wACE)*.5;
195 *x1++=( wD+wBCO-wACE)*.5;
196 *x2--=( wC-wACO-wBCE)*.5;
202 void mdct_forward(mdct_lookup *init, double *in, double *out, double *window){
204 double *x=alloca(sizeof(double)*(n/2));
205 double *w=alloca(sizeof(double)*(n/2));
212 /* window + rotate + step 1 */
217 double *A=init->trig+n2;
223 tempA= in[in1+2]*window[in1+2] + in[in2]*window[in2];
224 tempB= in[in1]*window[in1] + in[in2+2]*window[in2+2];
226 x[i]= tempB*A[1] + tempA*A[0];
227 x[i+1]= tempB*A[0] - tempA*A[1];
234 tempA= in[in1+2]*window[in1+2] - in[in2]*window[in2];
235 tempB= in[in1]*window[in1] - in[in2+2]*window[in2+2];
237 x[i]= tempB*A[1] + tempA*A[0];
238 x[i+1]= tempB*A[0] - tempA*A[1];
245 tempA= -in[in1+2]*window[in1+2] - in[in2]*window[in2];
246 tempB= -in[in1]*window[in1] - in[in2+2]*window[in2+2];
248 x[i]= tempB*A[1] + tempA*A[0];
249 x[i+1]= tempB*A[0] - tempA*A[1];
253 xx=_mdct_kernel(x,w,n,n2,n4,n8,init);
258 double *B=init->trig+n2;
262 out[i] =(xx[0]*B[0]+xx[1]*B[1])*scale;
263 *(--out2)=(xx[0]*B[1]-xx[1]*B[0])*scale;
271 void mdct_backward(mdct_lookup *init, double *in, double *out, double *window){
273 double *x=alloca(sizeof(double)*(n/2));
274 double *w=alloca(sizeof(double)*(n/2));
281 /* window + rotate + step 1 */
285 double *A=init->trig+n2;
289 *xO++=-*(inO+2)*A[1] - *inO*A[0];
290 *xO++= *inO*A[1] - *(inO+2)*A[0];
298 *xO++=*inO*A[1] + *(inO+2)*A[0];
299 *xO++=*inO*A[0] - *(inO+2)*A[1];
305 xx=_mdct_kernel(x,w,n,n2,n4,n8,init);
310 double *B=init->trig+n2;
312 int o3=n4+n2,o4=o3-1;
315 double temp1= (*xx * B[1] - *(xx+1) * B[0]);
316 double temp2=-(*xx * B[0] + *(xx+1) * B[1]);
318 out[o1]=-temp1*window[o1];
319 out[o2]= temp1*window[o2];
320 out[o3]= temp2*window[o3];
321 out[o4]= temp2*window[o4];