1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
9 * by the Xiph.Org Foundation https://xiph.org/ *
11 ********************************************************************
13 function: normalized modified discrete cosine transform
14 power of two length transform only [64 <= n ]
16 Original algorithm adapted long ago from _The use of multirate filter
17 banks for coding of high quality digital audio_, by T. Sporer,
18 K. Brandenburg and B. Edler, collection of the European Signal
19 Processing Conference (EUSIPCO), Amsterdam, June 1992, Vol.1, pp
22 The below code implements an algorithm that no longer looks much like
23 that presented in the paper, but the basic structure remains if you
24 dig deep enough to see it.
26 This module DOES NOT INCLUDE code to generate/apply the window
27 function. Everybody has their own weird favorite including me... I
28 happen to like the properties of y=sin(.5PI*sin^2(x)), but others may
31 ********************************************************************/
33 /* this can also be run as an integer transform by uncommenting a
34 define in mdct.h; the integerization is a first pass and although
35 it's likely stable for Vorbis, the dynamic range is constrained and
36 roundoff isn't done (so it's noisy). Consider it functional, but
37 only a starting point. There's no point on a machine with an FPU */
43 #include "vorbis/codec.h"
48 /* build lookups for trig functions; also pre-figure scaling and
49 some window function algebra. */
51 void mdct_init(mdct_lookup *lookup,int n){
52 int *bitrev=_ogg_malloc(sizeof(*bitrev)*(n/4));
53 DATA_TYPE *T=_ogg_malloc(sizeof(*T)*(n+n/4));
57 int log2n=lookup->log2n=rint(log((float)n)/log(2.f));
60 lookup->bitrev=bitrev;
65 T[i*2]=FLOAT_CONV(cos((M_PI/n)*(4*i)));
66 T[i*2+1]=FLOAT_CONV(-sin((M_PI/n)*(4*i)));
67 T[n2+i*2]=FLOAT_CONV(cos((M_PI/(2*n))*(2*i+1)));
68 T[n2+i*2+1]=FLOAT_CONV(sin((M_PI/(2*n))*(2*i+1)));
71 T[n+i*2]=FLOAT_CONV(cos((M_PI/n)*(4*i+2))*.5);
72 T[n+i*2+1]=FLOAT_CONV(-sin((M_PI/n)*(4*i+2))*.5);
75 /* bitreverse lookup... */
78 int mask=(1<<(log2n-1))-1,i,j;
83 if((msb>>j)&i)acc|=1<<j;
84 bitrev[i*2]=((~acc)&mask)-1;
89 lookup->scale=FLOAT_CONV(4.f/n);
92 /* 8 point butterfly (in place, 4 register) */
93 STIN void mdct_butterfly_8(DATA_TYPE *x){
94 REG_TYPE r0 = x[6] + x[2];
95 REG_TYPE r1 = x[6] - x[2];
96 REG_TYPE r2 = x[4] + x[0];
97 REG_TYPE r3 = x[4] - x[0];
116 /* 16 point butterfly (in place, 4 register) */
117 STIN void mdct_butterfly_16(DATA_TYPE *x){
118 REG_TYPE r0 = x[1] - x[9];
119 REG_TYPE r1 = x[0] - x[8];
123 x[0] = MULT_NORM((r0 + r1) * cPI2_8);
124 x[1] = MULT_NORM((r0 - r1) * cPI2_8);
137 x[4] = MULT_NORM((r0 - r1) * cPI2_8);
138 x[5] = MULT_NORM((r0 + r1) * cPI2_8);
148 mdct_butterfly_8(x+8);
151 /* 32 point butterfly (in place, 4 register) */
152 STIN void mdct_butterfly_32(DATA_TYPE *x){
153 REG_TYPE r0 = x[30] - x[14];
154 REG_TYPE r1 = x[31] - x[15];
165 x[12] = MULT_NORM( r0 * cPI1_8 - r1 * cPI3_8 );
166 x[13] = MULT_NORM( r0 * cPI3_8 + r1 * cPI1_8 );
172 x[10] = MULT_NORM(( r0 - r1 ) * cPI2_8);
173 x[11] = MULT_NORM(( r0 + r1 ) * cPI2_8);
179 x[8] = MULT_NORM( r0 * cPI3_8 - r1 * cPI1_8 );
180 x[9] = MULT_NORM( r1 * cPI3_8 + r0 * cPI1_8 );
193 x[4] = MULT_NORM( r1 * cPI1_8 + r0 * cPI3_8 );
194 x[5] = MULT_NORM( r1 * cPI3_8 - r0 * cPI1_8 );
200 x[2] = MULT_NORM(( r1 + r0 ) * cPI2_8);
201 x[3] = MULT_NORM(( r1 - r0 ) * cPI2_8);
207 x[0] = MULT_NORM( r1 * cPI3_8 + r0 * cPI1_8 );
208 x[1] = MULT_NORM( r1 * cPI1_8 - r0 * cPI3_8 );
210 mdct_butterfly_16(x);
211 mdct_butterfly_16(x+16);
215 /* N point first stage butterfly (in place, 2 register) */
216 STIN void mdct_butterfly_first(DATA_TYPE *T,
220 DATA_TYPE *x1 = x + points - 8;
221 DATA_TYPE *x2 = x + (points>>1) - 8;
231 x2[6] = MULT_NORM(r1 * T[1] + r0 * T[0]);
232 x2[7] = MULT_NORM(r1 * T[0] - r0 * T[1]);
238 x2[4] = MULT_NORM(r1 * T[5] + r0 * T[4]);
239 x2[5] = MULT_NORM(r1 * T[4] - r0 * T[5]);
245 x2[2] = MULT_NORM(r1 * T[9] + r0 * T[8]);
246 x2[3] = MULT_NORM(r1 * T[8] - r0 * T[9]);
252 x2[0] = MULT_NORM(r1 * T[13] + r0 * T[12]);
253 x2[1] = MULT_NORM(r1 * T[12] - r0 * T[13]);
262 /* N/stage point generic N stage butterfly (in place, 2 register) */
263 STIN void mdct_butterfly_generic(DATA_TYPE *T,
268 DATA_TYPE *x1 = x + points - 8;
269 DATA_TYPE *x2 = x + (points>>1) - 8;
279 x2[6] = MULT_NORM(r1 * T[1] + r0 * T[0]);
280 x2[7] = MULT_NORM(r1 * T[0] - r0 * T[1]);
288 x2[4] = MULT_NORM(r1 * T[1] + r0 * T[0]);
289 x2[5] = MULT_NORM(r1 * T[0] - r0 * T[1]);
297 x2[2] = MULT_NORM(r1 * T[1] + r0 * T[0]);
298 x2[3] = MULT_NORM(r1 * T[0] - r0 * T[1]);
306 x2[0] = MULT_NORM(r1 * T[1] + r0 * T[0]);
307 x2[1] = MULT_NORM(r1 * T[0] - r0 * T[1]);
316 STIN void mdct_butterflies(mdct_lookup *init,
320 DATA_TYPE *T=init->trig;
321 int stages=init->log2n-5;
325 mdct_butterfly_first(T,x,points);
328 for(i=1;--stages>0;i++){
329 for(j=0;j<(1<<i);j++)
330 mdct_butterfly_generic(T,x+(points>>i)*j,points>>i,4<<i);
333 for(j=0;j<points;j+=32)
334 mdct_butterfly_32(x+j);
338 void mdct_clear(mdct_lookup *l){
340 if(l->trig)_ogg_free(l->trig);
341 if(l->bitrev)_ogg_free(l->bitrev);
342 memset(l,0,sizeof(*l));
346 STIN void mdct_bitreverse(mdct_lookup *init,
349 int *bit = init->bitrev;
351 DATA_TYPE *w1 = x = w0+(n>>1);
352 DATA_TYPE *T = init->trig+n;
355 DATA_TYPE *x0 = x+bit[0];
356 DATA_TYPE *x1 = x+bit[1];
358 REG_TYPE r0 = x0[1] - x1[1];
359 REG_TYPE r1 = x0[0] + x1[0];
360 REG_TYPE r2 = MULT_NORM(r1 * T[0] + r0 * T[1]);
361 REG_TYPE r3 = MULT_NORM(r1 * T[1] - r0 * T[0]);
365 r0 = HALVE(x0[1] + x1[1]);
366 r1 = HALVE(x0[0] - x1[0]);
378 r2 = MULT_NORM(r1 * T[2] + r0 * T[3]);
379 r3 = MULT_NORM(r1 * T[3] - r0 * T[2]);
381 r0 = HALVE(x0[1] + x1[1]);
382 r1 = HALVE(x0[0] - x1[0]);
396 void mdct_backward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out){
403 DATA_TYPE *iX = in+n2-7;
404 DATA_TYPE *oX = out+n2+n4;
405 DATA_TYPE *T = init->trig+n4;
409 oX[0] = MULT_NORM(-iX[2] * T[3] - iX[0] * T[2]);
410 oX[1] = MULT_NORM (iX[0] * T[3] - iX[2] * T[2]);
411 oX[2] = MULT_NORM(-iX[6] * T[1] - iX[4] * T[0]);
412 oX[3] = MULT_NORM (iX[4] * T[1] - iX[6] * T[0]);
423 oX[0] = MULT_NORM (iX[4] * T[3] + iX[6] * T[2]);
424 oX[1] = MULT_NORM (iX[4] * T[2] - iX[6] * T[3]);
425 oX[2] = MULT_NORM (iX[0] * T[1] + iX[2] * T[0]);
426 oX[3] = MULT_NORM (iX[0] * T[0] - iX[2] * T[1]);
431 mdct_butterflies(init,out+n2,n2);
432 mdct_bitreverse(init,out);
434 /* roatate + window */
437 DATA_TYPE *oX1=out+n2+n4;
438 DATA_TYPE *oX2=out+n2+n4;
445 oX1[3] = MULT_NORM (iX[0] * T[1] - iX[1] * T[0]);
446 oX2[0] = -MULT_NORM (iX[0] * T[0] + iX[1] * T[1]);
448 oX1[2] = MULT_NORM (iX[2] * T[3] - iX[3] * T[2]);
449 oX2[1] = -MULT_NORM (iX[2] * T[2] + iX[3] * T[3]);
451 oX1[1] = MULT_NORM (iX[4] * T[5] - iX[5] * T[4]);
452 oX2[2] = -MULT_NORM (iX[4] * T[4] + iX[5] * T[5]);
454 oX1[0] = MULT_NORM (iX[6] * T[7] - iX[7] * T[6]);
455 oX2[3] = -MULT_NORM (iX[6] * T[6] + iX[7] * T[7]);
470 oX2[0] = -(oX1[3] = iX[3]);
471 oX2[1] = -(oX1[2] = iX[2]);
472 oX2[2] = -(oX1[1] = iX[1]);
473 oX2[3] = -(oX1[0] = iX[0]);
492 void mdct_forward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out){
497 DATA_TYPE *w=alloca(n*sizeof(*w)); /* forward needs working space */
502 /* window + rotate + step 1 */
506 DATA_TYPE *x0=in+n2+n4;
508 DATA_TYPE *T=init->trig+n2;
517 w2[i]= MULT_NORM(r1*T[1] + r0*T[0]);
518 w2[i+1]= MULT_NORM(r1*T[0] - r0*T[1]);
529 w2[i]= MULT_NORM(r1*T[1] + r0*T[0]);
530 w2[i+1]= MULT_NORM(r1*T[0] - r0*T[1]);
541 w2[i]= MULT_NORM(r1*T[1] + r0*T[0]);
542 w2[i+1]= MULT_NORM(r1*T[0] - r0*T[1]);
547 mdct_butterflies(init,w+n2,n2);
548 mdct_bitreverse(init,w);
550 /* roatate + window */
557 out[i] =MULT_NORM((w[0]*T[0]+w[1]*T[1])*init->scale);
558 x0[0] =MULT_NORM((w[0]*T[1]-w[1]*T[0])*init->scale);