2
0

mdct.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: normalized modified discrete cosine transform
  14. power of two length transform only [64 <= n ]
  15. last mod: $Id: mdct.c,v 1.9.6.5 2003/04/29 04:03:27 xiphmont Exp $
  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
  20. 211-214
  21. The below code implements an algorithm that no longer looks much like
  22. that presented in the paper, but the basic structure remains if you
  23. dig deep enough to see it.
  24. This module DOES NOT INCLUDE code to generate/apply the window
  25. function. Everybody has their own weird favorite including me... I
  26. happen to like the properties of y=sin(.5PI*sin^2(x)), but others may
  27. vehemently disagree.
  28. ********************************************************************/
  29. #include "ivorbiscodec.h"
  30. #include "os.h"
  31. #include "misc.h"
  32. #include "mdct.h"
  33. #include "mdct_lookup.h"
  34. STIN void presymmetry(DATA_TYPE *in,int n2,int step){
  35. DATA_TYPE *aX;
  36. DATA_TYPE *bX;
  37. LOOKUP_T *T;
  38. int n4=n2>>1;
  39. aX = in+n2-3;
  40. T = sincos_lookup0;
  41. do{
  42. REG_TYPE r0= aX[0];
  43. REG_TYPE r2= aX[2];
  44. XPROD31( r0, r2, T[0], T[1], &aX[0], &aX[2] ); T+=step;
  45. aX-=4;
  46. }while(aX>=in+n4);
  47. do{
  48. REG_TYPE r0= aX[0];
  49. REG_TYPE r2= aX[2];
  50. XPROD31( r0, r2, T[1], T[0], &aX[0], &aX[2] ); T-=step;
  51. aX-=4;
  52. }while(aX>=in);
  53. aX = in+n2-4;
  54. bX = in;
  55. T = sincos_lookup0;
  56. do{
  57. REG_TYPE ri0= aX[0];
  58. REG_TYPE ri2= aX[2];
  59. REG_TYPE ro0= bX[0];
  60. REG_TYPE ro2= bX[2];
  61. XNPROD31( ro2, ro0, T[1], T[0], &aX[0], &aX[2] ); T+=step;
  62. XNPROD31( ri2, ri0, T[0], T[1], &bX[0], &bX[2] );
  63. aX-=4;
  64. bX+=4;
  65. }while(aX>=in+n4);
  66. }
  67. /* 8 point butterfly (in place) */
  68. STIN void mdct_butterfly_8(DATA_TYPE *x){
  69. REG_TYPE r0 = x[0] + x[1];
  70. REG_TYPE r1 = x[0] - x[1];
  71. REG_TYPE r2 = x[2] + x[3];
  72. REG_TYPE r3 = x[2] - x[3];
  73. REG_TYPE r4 = x[4] + x[5];
  74. REG_TYPE r5 = x[4] - x[5];
  75. REG_TYPE r6 = x[6] + x[7];
  76. REG_TYPE r7 = x[6] - x[7];
  77. x[0] = r5 + r3;
  78. x[1] = r7 - r1;
  79. x[2] = r5 - r3;
  80. x[3] = r7 + r1;
  81. x[4] = r4 - r0;
  82. x[5] = r6 - r2;
  83. x[6] = r4 + r0;
  84. x[7] = r6 + r2;
  85. MB();
  86. }
  87. /* 16 point butterfly (in place, 4 register) */
  88. STIN void mdct_butterfly_16(DATA_TYPE *x){
  89. REG_TYPE r0, r1, r2, r3;
  90. r0 = x[ 8] - x[ 9]; x[ 8] += x[ 9];
  91. r1 = x[10] - x[11]; x[10] += x[11];
  92. r2 = x[ 1] - x[ 0]; x[ 9] = x[ 1] + x[0];
  93. r3 = x[ 3] - x[ 2]; x[11] = x[ 3] + x[2];
  94. x[ 0] = MULT31((r0 - r1) , cPI2_8);
  95. x[ 1] = MULT31((r2 + r3) , cPI2_8);
  96. x[ 2] = MULT31((r0 + r1) , cPI2_8);
  97. x[ 3] = MULT31((r3 - r2) , cPI2_8);
  98. MB();
  99. r2 = x[12] - x[13]; x[12] += x[13];
  100. r3 = x[14] - x[15]; x[14] += x[15];
  101. r0 = x[ 4] - x[ 5]; x[13] = x[ 5] + x[ 4];
  102. r1 = x[ 7] - x[ 6]; x[15] = x[ 7] + x[ 6];
  103. x[ 4] = r2; x[ 5] = r1;
  104. x[ 6] = r3; x[ 7] = r0;
  105. MB();
  106. mdct_butterfly_8(x);
  107. mdct_butterfly_8(x+8);
  108. }
  109. /* 32 point butterfly (in place, 4 register) */
  110. STIN void mdct_butterfly_32(DATA_TYPE *x){
  111. REG_TYPE r0, r1, r2, r3;
  112. r0 = x[16] - x[17]; x[16] += x[17];
  113. r1 = x[18] - x[19]; x[18] += x[19];
  114. r2 = x[ 1] - x[ 0]; x[17] = x[ 1] + x[ 0];
  115. r3 = x[ 3] - x[ 2]; x[19] = x[ 3] + x[ 2];
  116. XNPROD31( r0, r1, cPI3_8, cPI1_8, &x[ 0], &x[ 2] );
  117. XPROD31 ( r2, r3, cPI1_8, cPI3_8, &x[ 1], &x[ 3] );
  118. MB();
  119. r0 = x[20] - x[21]; x[20] += x[21];
  120. r1 = x[22] - x[23]; x[22] += x[23];
  121. r2 = x[ 5] - x[ 4]; x[21] = x[ 5] + x[ 4];
  122. r3 = x[ 7] - x[ 6]; x[23] = x[ 7] + x[ 6];
  123. x[ 4] = MULT31((r0 - r1) , cPI2_8);
  124. x[ 5] = MULT31((r3 + r2) , cPI2_8);
  125. x[ 6] = MULT31((r0 + r1) , cPI2_8);
  126. x[ 7] = MULT31((r3 - r2) , cPI2_8);
  127. MB();
  128. r0 = x[24] - x[25]; x[24] += x[25];
  129. r1 = x[26] - x[27]; x[26] += x[27];
  130. r2 = x[ 9] - x[ 8]; x[25] = x[ 9] + x[ 8];
  131. r3 = x[11] - x[10]; x[27] = x[11] + x[10];
  132. XNPROD31( r0, r1, cPI1_8, cPI3_8, &x[ 8], &x[10] );
  133. XPROD31 ( r2, r3, cPI3_8, cPI1_8, &x[ 9], &x[11] );
  134. MB();
  135. r0 = x[28] - x[29]; x[28] += x[29];
  136. r1 = x[30] - x[31]; x[30] += x[31];
  137. r2 = x[12] - x[13]; x[29] = x[13] + x[12];
  138. r3 = x[15] - x[14]; x[31] = x[15] + x[14];
  139. x[12] = r0; x[13] = r3;
  140. x[14] = r1; x[15] = r2;
  141. MB();
  142. mdct_butterfly_16(x);
  143. mdct_butterfly_16(x+16);
  144. }
  145. /* N/stage point generic N stage butterfly (in place, 2 register) */
  146. STIN void mdct_butterfly_generic(DATA_TYPE *x,int points,int step){
  147. LOOKUP_T *T = sincos_lookup0;
  148. DATA_TYPE *x1 = x + points - 4;
  149. DATA_TYPE *x2 = x + (points>>1) - 4;
  150. REG_TYPE r0, r1, r2, r3;
  151. do{
  152. r0 = x1[0] - x1[1]; x1[0] += x1[1];
  153. r1 = x1[3] - x1[2]; x1[2] += x1[3];
  154. r2 = x2[1] - x2[0]; x1[1] = x2[1] + x2[0];
  155. r3 = x2[3] - x2[2]; x1[3] = x2[3] + x2[2];
  156. XPROD31( r1, r0, T[0], T[1], &x2[0], &x2[2] );
  157. XPROD31( r2, r3, T[0], T[1], &x2[1], &x2[3] ); T+=step;
  158. x1-=4;
  159. x2-=4;
  160. }while(T<sincos_lookup0+1024);
  161. do{
  162. r0 = x1[0] - x1[1]; x1[0] += x1[1];
  163. r1 = x1[2] - x1[3]; x1[2] += x1[3];
  164. r2 = x2[0] - x2[1]; x1[1] = x2[1] + x2[0];
  165. r3 = x2[3] - x2[2]; x1[3] = x2[3] + x2[2];
  166. XNPROD31( r0, r1, T[0], T[1], &x2[0], &x2[2] );
  167. XNPROD31( r3, r2, T[0], T[1], &x2[1], &x2[3] ); T-=step;
  168. x1-=4;
  169. x2-=4;
  170. }while(T>sincos_lookup0);
  171. }
  172. STIN void mdct_butterflies(DATA_TYPE *x,int points,int shift){
  173. int stages=8-shift;
  174. int i,j;
  175. for(i=0;--stages>0;i++){
  176. for(j=0;j<(1<<i);j++)
  177. mdct_butterfly_generic(x+(points>>i)*j,points>>i,4<<(i+shift));
  178. }
  179. for(j=0;j<points;j+=32)
  180. mdct_butterfly_32(x+j);
  181. }
  182. static unsigned char bitrev[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
  183. STIN int bitrev12(int x){
  184. return bitrev[x>>8]|(bitrev[(x&0x0f0)>>4]<<4)|(((int)bitrev[x&0x00f])<<8);
  185. }
  186. STIN void mdct_bitreverse(DATA_TYPE *x,int n,int shift){
  187. int bit = 0;
  188. DATA_TYPE *w = x+(n>>1);
  189. do{
  190. DATA_TYPE b = bitrev12(bit++);
  191. DATA_TYPE *xx = x + (b>>shift);
  192. REG_TYPE r;
  193. w -= 2;
  194. if(w>xx){
  195. r = xx[0];
  196. xx[0] = w[0];
  197. w[0] = r;
  198. r = xx[1];
  199. xx[1] = w[1];
  200. w[1] = r;
  201. }
  202. }while(w>x);
  203. }
  204. STIN void mdct_step7(DATA_TYPE *x,int n,int step){
  205. DATA_TYPE *w0 = x;
  206. DATA_TYPE *w1 = x+(n>>1);
  207. LOOKUP_T *T = (step>=4)?(sincos_lookup0+(step>>1)):sincos_lookup1;
  208. LOOKUP_T *Ttop = T+1024;
  209. REG_TYPE r0, r1, r2, r3;
  210. do{
  211. w1 -= 2;
  212. r0 = w0[0] + w1[0];
  213. r1 = w1[1] - w0[1];
  214. r2 = MULT32(r0, T[1]) + MULT32(r1, T[0]);
  215. r3 = MULT32(r1, T[1]) - MULT32(r0, T[0]);
  216. T+=step;
  217. r0 = (w0[1] + w1[1])>>1;
  218. r1 = (w0[0] - w1[0])>>1;
  219. w0[0] = r0 + r2;
  220. w0[1] = r1 + r3;
  221. w1[0] = r0 - r2;
  222. w1[1] = r3 - r1;
  223. w0 += 2;
  224. }while(T<Ttop);
  225. do{
  226. w1 -= 2;
  227. r0 = w0[0] + w1[0];
  228. r1 = w1[1] - w0[1];
  229. T-=step;
  230. r2 = MULT32(r0, T[0]) + MULT32(r1, T[1]);
  231. r3 = MULT32(r1, T[0]) - MULT32(r0, T[1]);
  232. r0 = (w0[1] + w1[1])>>1;
  233. r1 = (w0[0] - w1[0])>>1;
  234. w0[0] = r0 + r2;
  235. w0[1] = r1 + r3;
  236. w1[0] = r0 - r2;
  237. w1[1] = r3 - r1;
  238. w0 += 2;
  239. }while(w0<w1);
  240. }
  241. STIN void mdct_step8(DATA_TYPE *x, int n, int step){
  242. LOOKUP_T *T;
  243. LOOKUP_T *V;
  244. DATA_TYPE *iX =x+(n>>1);
  245. step>>=2;
  246. switch(step) {
  247. default:
  248. T=(step>=4)?(sincos_lookup0+(step>>1)):sincos_lookup1;
  249. do{
  250. REG_TYPE r0 = x[0];
  251. REG_TYPE r1 = -x[1];
  252. XPROD31( r0, r1, T[0], T[1], x, x+1); T+=step;
  253. x +=2;
  254. }while(x<iX);
  255. break;
  256. case 1:
  257. {
  258. /* linear interpolation between table values: offset=0.5, step=1 */
  259. REG_TYPE t0,t1,v0,v1,r0,r1;
  260. T = sincos_lookup0;
  261. V = sincos_lookup1;
  262. t0 = (*T++)>>1;
  263. t1 = (*T++)>>1;
  264. do{
  265. r0 = x[0];
  266. r1 = -x[1];
  267. t0 += (v0 = (*V++)>>1);
  268. t1 += (v1 = (*V++)>>1);
  269. XPROD31( r0, r1, t0, t1, x, x+1 );
  270. r0 = x[2];
  271. r1 = -x[3];
  272. v0 += (t0 = (*T++)>>1);
  273. v1 += (t1 = (*T++)>>1);
  274. XPROD31( r0, r1, v0, v1, x+2, x+3 );
  275. x += 4;
  276. }while(x<iX);
  277. break;
  278. }
  279. case 0:
  280. {
  281. /* linear interpolation between table values: offset=0.25, step=0.5 */
  282. REG_TYPE t0,t1,v0,v1,q0,q1,r0,r1;
  283. T = sincos_lookup0;
  284. V = sincos_lookup1;
  285. t0 = *T++;
  286. t1 = *T++;
  287. do{
  288. v0 = *V++;
  289. v1 = *V++;
  290. t0 += (q0 = (v0-t0)>>2);
  291. t1 += (q1 = (v1-t1)>>2);
  292. r0 = x[0];
  293. r1 = -x[1];
  294. XPROD31( r0, r1, t0, t1, x, x+1 );
  295. t0 = v0-q0;
  296. t1 = v1-q1;
  297. r0 = x[2];
  298. r1 = -x[3];
  299. XPROD31( r0, r1, t0, t1, x+2, x+3 );
  300. t0 = *T++;
  301. t1 = *T++;
  302. v0 += (q0 = (t0-v0)>>2);
  303. v1 += (q1 = (t1-v1)>>2);
  304. r0 = x[4];
  305. r1 = -x[5];
  306. XPROD31( r0, r1, v0, v1, x+4, x+5 );
  307. v0 = t0-q0;
  308. v1 = t1-q1;
  309. r0 = x[6];
  310. r1 = -x[7];
  311. XPROD31( r0, r1, v0, v1, x+5, x+6 );
  312. x+=8;
  313. }while(x<iX);
  314. break;
  315. }
  316. }
  317. }
  318. /* partial; doesn't perform last-step deinterleave/unrolling. That
  319. can be done more efficiently during pcm output */
  320. void mdct_backward(int n, DATA_TYPE *in){
  321. int shift;
  322. int step;
  323. for (shift=4;!(n&(1<<shift));shift++);
  324. shift=13-shift;
  325. step=2<<shift;
  326. presymmetry(in,n>>1,step);
  327. mdct_butterflies(in,n>>1,shift);
  328. mdct_bitreverse(in,n,shift);
  329. mdct_step7(in,n,step);
  330. mdct_step8(in,n,step);
  331. }
  332. void mdct_shift_right(int n, DATA_TYPE *in, DATA_TYPE *right){
  333. int i;
  334. n>>=2;
  335. in+=1;
  336. for(i=0;i<n;i++)
  337. right[i]=in[i<<1];
  338. }
  339. void mdct_unroll_lap(int n0,int n1,
  340. int lW,int W,
  341. DATA_TYPE *in,
  342. DATA_TYPE *right,
  343. LOOKUP_T *w0,
  344. LOOKUP_T *w1,
  345. ogg_int16_t *out,
  346. int step,
  347. int start, /* samples, this frame */
  348. int end /* samples, this frame */){
  349. DATA_TYPE *l=in+(W&&lW ? n1>>1 : n0>>1);
  350. DATA_TYPE *r=right+(lW ? n1>>2 : n0>>2);
  351. DATA_TYPE *post;
  352. LOOKUP_T *wR=(W && lW ? w1+(n1>>1) : w0+(n0>>1));
  353. LOOKUP_T *wL=(W && lW ? w1 : w0 );
  354. int preLap=(lW && !W ? (n1>>2)-(n0>>2) : 0 );
  355. int halfLap=(lW && W ? (n1>>2) : (n0>>2) );
  356. int postLap=(!lW && W ? (n1>>2)-(n0>>2) : 0 );
  357. int n,off;
  358. /* preceeding direct-copy lapping from previous frame, if any */
  359. if(preLap){
  360. n = (end<preLap?end:preLap);
  361. off = (start<preLap?start:preLap);
  362. post = r-n;
  363. r -= off;
  364. start -= off;
  365. end -= n;
  366. while(r>post){
  367. *out = CLIP_TO_15((*--r)>>9);
  368. out+=step;
  369. }
  370. }
  371. /* cross-lap; two halves due to wrap-around */
  372. n = (end<halfLap?end:halfLap);
  373. off = (start<halfLap?start:halfLap);
  374. post = r-n;
  375. r -= off;
  376. l -= off*2;
  377. start -= off;
  378. wR -= off;
  379. wL += off;
  380. end -= n;
  381. while(r>post){
  382. l-=2;
  383. *out = CLIP_TO_15((MULT31(*--r,*--wR) + MULT31(*l,*wL++))>>9);
  384. out+=step;
  385. }
  386. n = (end<halfLap?end:halfLap);
  387. off = (start<halfLap?start:halfLap);
  388. post = r+n;
  389. r += off;
  390. l += off*2;
  391. start -= off;
  392. end -= n;
  393. wR -= off;
  394. wL += off;
  395. while(r<post){
  396. *out = CLIP_TO_15((MULT31(*r++,*--wR) - MULT31(*l,*wL++))>>9);
  397. out+=step;
  398. l+=2;
  399. }
  400. /* preceeding direct-copy lapping from previous frame, if any */
  401. if(postLap){
  402. n = (end<postLap?end:postLap);
  403. off = (start<postLap?start:postLap);
  404. post = l+n*2;
  405. l += off*2;
  406. while(l<post){
  407. *out = CLIP_TO_15((-*l)>>9);
  408. out+=step;
  409. l+=2;
  410. }
  411. }
  412. }