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