nuclear@0: /******************************************************************** nuclear@0: * * nuclear@0: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * nuclear@0: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * nuclear@0: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * nuclear@0: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * nuclear@0: * * nuclear@0: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * nuclear@0: * by the Xiph.Org Foundation http://www.xiph.org/ * nuclear@0: * * nuclear@0: ******************************************************************** nuclear@0: nuclear@0: function: normalized modified discrete cosine transform nuclear@0: power of two length transform only [64 <= n ] nuclear@0: last mod: $Id: mdct.c 16227 2009-07-08 06:58:46Z xiphmont $ nuclear@0: nuclear@0: Original algorithm adapted long ago from _The use of multirate filter nuclear@0: banks for coding of high quality digital audio_, by T. Sporer, nuclear@0: K. Brandenburg and B. Edler, collection of the European Signal nuclear@0: Processing Conference (EUSIPCO), Amsterdam, June 1992, Vol.1, pp nuclear@0: 211-214 nuclear@0: nuclear@0: The below code implements an algorithm that no longer looks much like nuclear@0: that presented in the paper, but the basic structure remains if you nuclear@0: dig deep enough to see it. nuclear@0: nuclear@0: This module DOES NOT INCLUDE code to generate/apply the window nuclear@0: function. Everybody has their own weird favorite including me... I nuclear@0: happen to like the properties of y=sin(.5PI*sin^2(x)), but others may nuclear@0: vehemently disagree. nuclear@0: nuclear@0: ********************************************************************/ nuclear@0: nuclear@0: /* this can also be run as an integer transform by uncommenting a nuclear@0: define in mdct.h; the integerization is a first pass and although nuclear@0: it's likely stable for Vorbis, the dynamic range is constrained and nuclear@0: roundoff isn't done (so it's noisy). Consider it functional, but nuclear@0: only a starting point. There's no point on a machine with an FPU */ nuclear@0: nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include "vorbis/codec.h" nuclear@0: #include "mdct.h" nuclear@0: #include "os.h" nuclear@0: #include "misc.h" nuclear@0: nuclear@0: /* build lookups for trig functions; also pre-figure scaling and nuclear@0: some window function algebra. */ nuclear@0: nuclear@0: void mdct_init(mdct_lookup *lookup,int n){ nuclear@0: int *bitrev=_ogg_malloc(sizeof(*bitrev)*(n/4)); nuclear@0: DATA_TYPE *T=_ogg_malloc(sizeof(*T)*(n+n/4)); nuclear@0: nuclear@0: int i; nuclear@0: int n2=n>>1; nuclear@0: int log2n=lookup->log2n=rint(log((float)n)/log(2.f)); nuclear@0: lookup->n=n; nuclear@0: lookup->trig=T; nuclear@0: lookup->bitrev=bitrev; nuclear@0: nuclear@0: /* trig lookups... */ nuclear@0: nuclear@0: for(i=0;i>j;j++) nuclear@0: if((msb>>j)&i)acc|=1<scale=FLOAT_CONV(4.f/n); nuclear@0: } nuclear@0: nuclear@0: /* 8 point butterfly (in place, 4 register) */ nuclear@0: STIN void mdct_butterfly_8(DATA_TYPE *x){ nuclear@0: REG_TYPE r0 = x[6] + x[2]; nuclear@0: REG_TYPE r1 = x[6] - x[2]; nuclear@0: REG_TYPE r2 = x[4] + x[0]; nuclear@0: REG_TYPE r3 = x[4] - x[0]; nuclear@0: nuclear@0: x[6] = r0 + r2; nuclear@0: x[4] = r0 - r2; nuclear@0: nuclear@0: r0 = x[5] - x[1]; nuclear@0: r2 = x[7] - x[3]; nuclear@0: x[0] = r1 + r0; nuclear@0: x[2] = r1 - r0; nuclear@0: nuclear@0: r0 = x[5] + x[1]; nuclear@0: r1 = x[7] + x[3]; nuclear@0: x[3] = r2 + r3; nuclear@0: x[1] = r2 - r3; nuclear@0: x[7] = r1 + r0; nuclear@0: x[5] = r1 - r0; nuclear@0: nuclear@0: } nuclear@0: nuclear@0: /* 16 point butterfly (in place, 4 register) */ nuclear@0: STIN void mdct_butterfly_16(DATA_TYPE *x){ nuclear@0: REG_TYPE r0 = x[1] - x[9]; nuclear@0: REG_TYPE r1 = x[0] - x[8]; nuclear@0: nuclear@0: x[8] += x[0]; nuclear@0: x[9] += x[1]; nuclear@0: x[0] = MULT_NORM((r0 + r1) * cPI2_8); nuclear@0: x[1] = MULT_NORM((r0 - r1) * cPI2_8); nuclear@0: nuclear@0: r0 = x[3] - x[11]; nuclear@0: r1 = x[10] - x[2]; nuclear@0: x[10] += x[2]; nuclear@0: x[11] += x[3]; nuclear@0: x[2] = r0; nuclear@0: x[3] = r1; nuclear@0: nuclear@0: r0 = x[12] - x[4]; nuclear@0: r1 = x[13] - x[5]; nuclear@0: x[12] += x[4]; nuclear@0: x[13] += x[5]; nuclear@0: x[4] = MULT_NORM((r0 - r1) * cPI2_8); nuclear@0: x[5] = MULT_NORM((r0 + r1) * cPI2_8); nuclear@0: nuclear@0: r0 = x[14] - x[6]; nuclear@0: r1 = x[15] - x[7]; nuclear@0: x[14] += x[6]; nuclear@0: x[15] += x[7]; nuclear@0: x[6] = r0; nuclear@0: x[7] = r1; nuclear@0: nuclear@0: mdct_butterfly_8(x); nuclear@0: mdct_butterfly_8(x+8); nuclear@0: } nuclear@0: nuclear@0: /* 32 point butterfly (in place, 4 register) */ nuclear@0: STIN void mdct_butterfly_32(DATA_TYPE *x){ nuclear@0: REG_TYPE r0 = x[30] - x[14]; nuclear@0: REG_TYPE r1 = x[31] - x[15]; nuclear@0: nuclear@0: x[30] += x[14]; nuclear@0: x[31] += x[15]; nuclear@0: x[14] = r0; nuclear@0: x[15] = r1; nuclear@0: nuclear@0: r0 = x[28] - x[12]; nuclear@0: r1 = x[29] - x[13]; nuclear@0: x[28] += x[12]; nuclear@0: x[29] += x[13]; nuclear@0: x[12] = MULT_NORM( r0 * cPI1_8 - r1 * cPI3_8 ); nuclear@0: x[13] = MULT_NORM( r0 * cPI3_8 + r1 * cPI1_8 ); nuclear@0: nuclear@0: r0 = x[26] - x[10]; nuclear@0: r1 = x[27] - x[11]; nuclear@0: x[26] += x[10]; nuclear@0: x[27] += x[11]; nuclear@0: x[10] = MULT_NORM(( r0 - r1 ) * cPI2_8); nuclear@0: x[11] = MULT_NORM(( r0 + r1 ) * cPI2_8); nuclear@0: nuclear@0: r0 = x[24] - x[8]; nuclear@0: r1 = x[25] - x[9]; nuclear@0: x[24] += x[8]; nuclear@0: x[25] += x[9]; nuclear@0: x[8] = MULT_NORM( r0 * cPI3_8 - r1 * cPI1_8 ); nuclear@0: x[9] = MULT_NORM( r1 * cPI3_8 + r0 * cPI1_8 ); nuclear@0: nuclear@0: r0 = x[22] - x[6]; nuclear@0: r1 = x[7] - x[23]; nuclear@0: x[22] += x[6]; nuclear@0: x[23] += x[7]; nuclear@0: x[6] = r1; nuclear@0: x[7] = r0; nuclear@0: nuclear@0: r0 = x[4] - x[20]; nuclear@0: r1 = x[5] - x[21]; nuclear@0: x[20] += x[4]; nuclear@0: x[21] += x[5]; nuclear@0: x[4] = MULT_NORM( r1 * cPI1_8 + r0 * cPI3_8 ); nuclear@0: x[5] = MULT_NORM( r1 * cPI3_8 - r0 * cPI1_8 ); nuclear@0: nuclear@0: r0 = x[2] - x[18]; nuclear@0: r1 = x[3] - x[19]; nuclear@0: x[18] += x[2]; nuclear@0: x[19] += x[3]; nuclear@0: x[2] = MULT_NORM(( r1 + r0 ) * cPI2_8); nuclear@0: x[3] = MULT_NORM(( r1 - r0 ) * cPI2_8); nuclear@0: nuclear@0: r0 = x[0] - x[16]; nuclear@0: r1 = x[1] - x[17]; nuclear@0: x[16] += x[0]; nuclear@0: x[17] += x[1]; nuclear@0: x[0] = MULT_NORM( r1 * cPI3_8 + r0 * cPI1_8 ); nuclear@0: x[1] = MULT_NORM( r1 * cPI1_8 - r0 * cPI3_8 ); nuclear@0: nuclear@0: mdct_butterfly_16(x); nuclear@0: mdct_butterfly_16(x+16); nuclear@0: nuclear@0: } nuclear@0: nuclear@0: /* N point first stage butterfly (in place, 2 register) */ nuclear@0: STIN void mdct_butterfly_first(DATA_TYPE *T, nuclear@0: DATA_TYPE *x, nuclear@0: int points){ nuclear@0: nuclear@0: DATA_TYPE *x1 = x + points - 8; nuclear@0: DATA_TYPE *x2 = x + (points>>1) - 8; nuclear@0: REG_TYPE r0; nuclear@0: REG_TYPE r1; nuclear@0: nuclear@0: do{ nuclear@0: nuclear@0: r0 = x1[6] - x2[6]; nuclear@0: r1 = x1[7] - x2[7]; nuclear@0: x1[6] += x2[6]; nuclear@0: x1[7] += x2[7]; nuclear@0: x2[6] = MULT_NORM(r1 * T[1] + r0 * T[0]); nuclear@0: x2[7] = MULT_NORM(r1 * T[0] - r0 * T[1]); nuclear@0: nuclear@0: r0 = x1[4] - x2[4]; nuclear@0: r1 = x1[5] - x2[5]; nuclear@0: x1[4] += x2[4]; nuclear@0: x1[5] += x2[5]; nuclear@0: x2[4] = MULT_NORM(r1 * T[5] + r0 * T[4]); nuclear@0: x2[5] = MULT_NORM(r1 * T[4] - r0 * T[5]); nuclear@0: nuclear@0: r0 = x1[2] - x2[2]; nuclear@0: r1 = x1[3] - x2[3]; nuclear@0: x1[2] += x2[2]; nuclear@0: x1[3] += x2[3]; nuclear@0: x2[2] = MULT_NORM(r1 * T[9] + r0 * T[8]); nuclear@0: x2[3] = MULT_NORM(r1 * T[8] - r0 * T[9]); nuclear@0: nuclear@0: r0 = x1[0] - x2[0]; nuclear@0: r1 = x1[1] - x2[1]; nuclear@0: x1[0] += x2[0]; nuclear@0: x1[1] += x2[1]; nuclear@0: x2[0] = MULT_NORM(r1 * T[13] + r0 * T[12]); nuclear@0: x2[1] = MULT_NORM(r1 * T[12] - r0 * T[13]); nuclear@0: nuclear@0: x1-=8; nuclear@0: x2-=8; nuclear@0: T+=16; nuclear@0: nuclear@0: }while(x2>=x); nuclear@0: } nuclear@0: nuclear@0: /* N/stage point generic N stage butterfly (in place, 2 register) */ nuclear@0: STIN void mdct_butterfly_generic(DATA_TYPE *T, nuclear@0: DATA_TYPE *x, nuclear@0: int points, nuclear@0: int trigint){ nuclear@0: nuclear@0: DATA_TYPE *x1 = x + points - 8; nuclear@0: DATA_TYPE *x2 = x + (points>>1) - 8; nuclear@0: REG_TYPE r0; nuclear@0: REG_TYPE r1; nuclear@0: nuclear@0: do{ nuclear@0: nuclear@0: r0 = x1[6] - x2[6]; nuclear@0: r1 = x1[7] - x2[7]; nuclear@0: x1[6] += x2[6]; nuclear@0: x1[7] += x2[7]; nuclear@0: x2[6] = MULT_NORM(r1 * T[1] + r0 * T[0]); nuclear@0: x2[7] = MULT_NORM(r1 * T[0] - r0 * T[1]); nuclear@0: nuclear@0: T+=trigint; nuclear@0: nuclear@0: r0 = x1[4] - x2[4]; nuclear@0: r1 = x1[5] - x2[5]; nuclear@0: x1[4] += x2[4]; nuclear@0: x1[5] += x2[5]; nuclear@0: x2[4] = MULT_NORM(r1 * T[1] + r0 * T[0]); nuclear@0: x2[5] = MULT_NORM(r1 * T[0] - r0 * T[1]); nuclear@0: nuclear@0: T+=trigint; nuclear@0: nuclear@0: r0 = x1[2] - x2[2]; nuclear@0: r1 = x1[3] - x2[3]; nuclear@0: x1[2] += x2[2]; nuclear@0: x1[3] += x2[3]; nuclear@0: x2[2] = MULT_NORM(r1 * T[1] + r0 * T[0]); nuclear@0: x2[3] = MULT_NORM(r1 * T[0] - r0 * T[1]); nuclear@0: nuclear@0: T+=trigint; nuclear@0: nuclear@0: r0 = x1[0] - x2[0]; nuclear@0: r1 = x1[1] - x2[1]; nuclear@0: x1[0] += x2[0]; nuclear@0: x1[1] += x2[1]; nuclear@0: x2[0] = MULT_NORM(r1 * T[1] + r0 * T[0]); nuclear@0: x2[1] = MULT_NORM(r1 * T[0] - r0 * T[1]); nuclear@0: nuclear@0: T+=trigint; nuclear@0: x1-=8; nuclear@0: x2-=8; nuclear@0: nuclear@0: }while(x2>=x); nuclear@0: } nuclear@0: nuclear@0: STIN void mdct_butterflies(mdct_lookup *init, nuclear@0: DATA_TYPE *x, nuclear@0: int points){ nuclear@0: nuclear@0: DATA_TYPE *T=init->trig; nuclear@0: int stages=init->log2n-5; nuclear@0: int i,j; nuclear@0: nuclear@0: if(--stages>0){ nuclear@0: mdct_butterfly_first(T,x,points); nuclear@0: } nuclear@0: nuclear@0: for(i=1;--stages>0;i++){ nuclear@0: for(j=0;j<(1<>i)*j,points>>i,4<trig)_ogg_free(l->trig); nuclear@0: if(l->bitrev)_ogg_free(l->bitrev); nuclear@0: memset(l,0,sizeof(*l)); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: STIN void mdct_bitreverse(mdct_lookup *init, nuclear@0: DATA_TYPE *x){ nuclear@0: int n = init->n; nuclear@0: int *bit = init->bitrev; nuclear@0: DATA_TYPE *w0 = x; nuclear@0: DATA_TYPE *w1 = x = w0+(n>>1); nuclear@0: DATA_TYPE *T = init->trig+n; nuclear@0: nuclear@0: do{ nuclear@0: DATA_TYPE *x0 = x+bit[0]; nuclear@0: DATA_TYPE *x1 = x+bit[1]; nuclear@0: nuclear@0: REG_TYPE r0 = x0[1] - x1[1]; nuclear@0: REG_TYPE r1 = x0[0] + x1[0]; nuclear@0: REG_TYPE r2 = MULT_NORM(r1 * T[0] + r0 * T[1]); nuclear@0: REG_TYPE r3 = MULT_NORM(r1 * T[1] - r0 * T[0]); nuclear@0: nuclear@0: w1 -= 4; nuclear@0: nuclear@0: r0 = HALVE(x0[1] + x1[1]); nuclear@0: r1 = HALVE(x0[0] - x1[0]); nuclear@0: nuclear@0: w0[0] = r0 + r2; nuclear@0: w1[2] = r0 - r2; nuclear@0: w0[1] = r1 + r3; nuclear@0: w1[3] = r3 - r1; nuclear@0: nuclear@0: x0 = x+bit[2]; nuclear@0: x1 = x+bit[3]; nuclear@0: nuclear@0: r0 = x0[1] - x1[1]; nuclear@0: r1 = x0[0] + x1[0]; nuclear@0: r2 = MULT_NORM(r1 * T[2] + r0 * T[3]); nuclear@0: r3 = MULT_NORM(r1 * T[3] - r0 * T[2]); nuclear@0: nuclear@0: r0 = HALVE(x0[1] + x1[1]); nuclear@0: r1 = HALVE(x0[0] - x1[0]); nuclear@0: nuclear@0: w0[2] = r0 + r2; nuclear@0: w1[0] = r0 - r2; nuclear@0: w0[3] = r1 + r3; nuclear@0: w1[1] = r3 - r1; nuclear@0: nuclear@0: T += 4; nuclear@0: bit += 4; nuclear@0: w0 += 4; nuclear@0: nuclear@0: }while(w0n; nuclear@0: int n2=n>>1; nuclear@0: int n4=n>>2; nuclear@0: nuclear@0: /* rotate */ nuclear@0: nuclear@0: DATA_TYPE *iX = in+n2-7; nuclear@0: DATA_TYPE *oX = out+n2+n4; nuclear@0: DATA_TYPE *T = init->trig+n4; nuclear@0: nuclear@0: do{ nuclear@0: oX -= 4; nuclear@0: oX[0] = MULT_NORM(-iX[2] * T[3] - iX[0] * T[2]); nuclear@0: oX[1] = MULT_NORM (iX[0] * T[3] - iX[2] * T[2]); nuclear@0: oX[2] = MULT_NORM(-iX[6] * T[1] - iX[4] * T[0]); nuclear@0: oX[3] = MULT_NORM (iX[4] * T[1] - iX[6] * T[0]); nuclear@0: iX -= 8; nuclear@0: T += 4; nuclear@0: }while(iX>=in); nuclear@0: nuclear@0: iX = in+n2-8; nuclear@0: oX = out+n2+n4; nuclear@0: T = init->trig+n4; nuclear@0: nuclear@0: do{ nuclear@0: T -= 4; nuclear@0: oX[0] = MULT_NORM (iX[4] * T[3] + iX[6] * T[2]); nuclear@0: oX[1] = MULT_NORM (iX[4] * T[2] - iX[6] * T[3]); nuclear@0: oX[2] = MULT_NORM (iX[0] * T[1] + iX[2] * T[0]); nuclear@0: oX[3] = MULT_NORM (iX[0] * T[0] - iX[2] * T[1]); nuclear@0: iX -= 8; nuclear@0: oX += 4; nuclear@0: }while(iX>=in); nuclear@0: nuclear@0: mdct_butterflies(init,out+n2,n2); nuclear@0: mdct_bitreverse(init,out); nuclear@0: nuclear@0: /* roatate + window */ nuclear@0: nuclear@0: { nuclear@0: DATA_TYPE *oX1=out+n2+n4; nuclear@0: DATA_TYPE *oX2=out+n2+n4; nuclear@0: DATA_TYPE *iX =out; nuclear@0: T =init->trig+n2; nuclear@0: nuclear@0: do{ nuclear@0: oX1-=4; nuclear@0: nuclear@0: oX1[3] = MULT_NORM (iX[0] * T[1] - iX[1] * T[0]); nuclear@0: oX2[0] = -MULT_NORM (iX[0] * T[0] + iX[1] * T[1]); nuclear@0: nuclear@0: oX1[2] = MULT_NORM (iX[2] * T[3] - iX[3] * T[2]); nuclear@0: oX2[1] = -MULT_NORM (iX[2] * T[2] + iX[3] * T[3]); nuclear@0: nuclear@0: oX1[1] = MULT_NORM (iX[4] * T[5] - iX[5] * T[4]); nuclear@0: oX2[2] = -MULT_NORM (iX[4] * T[4] + iX[5] * T[5]); nuclear@0: nuclear@0: oX1[0] = MULT_NORM (iX[6] * T[7] - iX[7] * T[6]); nuclear@0: oX2[3] = -MULT_NORM (iX[6] * T[6] + iX[7] * T[7]); nuclear@0: nuclear@0: oX2+=4; nuclear@0: iX += 8; nuclear@0: T += 8; nuclear@0: }while(iXoX2); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void mdct_forward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out){ nuclear@0: int n=init->n; nuclear@0: int n2=n>>1; nuclear@0: int n4=n>>2; nuclear@0: int n8=n>>3; nuclear@0: DATA_TYPE *w=alloca(n*sizeof(*w)); /* forward needs working space */ nuclear@0: DATA_TYPE *w2=w+n2; nuclear@0: nuclear@0: /* rotate */ nuclear@0: nuclear@0: /* window + rotate + step 1 */ nuclear@0: nuclear@0: REG_TYPE r0; nuclear@0: REG_TYPE r1; nuclear@0: DATA_TYPE *x0=in+n2+n4; nuclear@0: DATA_TYPE *x1=x0+1; nuclear@0: DATA_TYPE *T=init->trig+n2; nuclear@0: nuclear@0: int i=0; nuclear@0: nuclear@0: for(i=0;itrig+n2; nuclear@0: x0=out+n2; nuclear@0: nuclear@0: for(i=0;iscale); nuclear@0: x0[0] =MULT_NORM((w[0]*T[1]-w[1]*T[0])*init->scale); nuclear@0: w+=2; nuclear@0: T+=2; nuclear@0: } nuclear@0: }