mdct.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. #include <stdio.h>
  35. #if defined(ONLY_C)
  36. STIN void presymmetry(DATA_TYPE *in,int n2,int step){
  37. DATA_TYPE *aX;
  38. DATA_TYPE *bX;
  39. LOOKUP_T *T;
  40. int n4=n2>>1;
  41. aX = in+n2-3;
  42. T = sincos_lookup0;
  43. do{
  44. REG_TYPE s0= aX[0];
  45. REG_TYPE s2= aX[2];
  46. XPROD31( s0, s2, T[0], T[1], &aX[0], &aX[2] ); T+=step;
  47. aX-=4;
  48. }while(aX>=in+n4);
  49. do{
  50. REG_TYPE s0= aX[0];
  51. REG_TYPE s2= aX[2];
  52. XPROD31( s0, s2, T[1], T[0], &aX[0], &aX[2] ); T-=step;
  53. aX-=4;
  54. }while(aX>=in);
  55. aX = in+n2-4;
  56. bX = in;
  57. T = sincos_lookup0;
  58. do{
  59. REG_TYPE ri0= aX[0];
  60. REG_TYPE ri2= aX[2];
  61. REG_TYPE ro0= bX[0];
  62. REG_TYPE ro2= bX[2];
  63. XNPROD31( ro2, ro0, T[1], T[0], &aX[0], &aX[2] ); T+=step;
  64. XNPROD31( ri2, ri0, T[0], T[1], &bX[0], &bX[2] );
  65. aX-=4;
  66. bX+=4;
  67. }while(aX>=bX);
  68. }
  69. /* 8 point butterfly (in place) */
  70. STIN void mdct_butterfly_8(DATA_TYPE *x){
  71. REG_TYPE s0 = x[0] + x[1];
  72. REG_TYPE s1 = x[0] - x[1];
  73. REG_TYPE s2 = x[2] + x[3];
  74. REG_TYPE s3 = x[2] - x[3];
  75. REG_TYPE s4 = x[4] + x[5];
  76. REG_TYPE s5 = x[4] - x[5];
  77. REG_TYPE s6 = x[6] + x[7];
  78. REG_TYPE s7 = x[6] - x[7];
  79. x[0] = s5 + s3;
  80. x[1] = s7 - s1;
  81. x[2] = s5 - s3;
  82. x[3] = s7 + s1;
  83. x[4] = s4 - s0;
  84. x[5] = s6 - s2;
  85. x[6] = s4 + s0;
  86. x[7] = s6 + s2;
  87. MB();
  88. }
  89. /* 16 point butterfly (in place, 4 register) */
  90. STIN void mdct_butterfly_16(DATA_TYPE *x){
  91. REG_TYPE s0, s1, s2, s3;
  92. s0 = x[ 8] - x[ 9]; x[ 8] += x[ 9];
  93. s1 = x[10] - x[11]; x[10] += x[11];
  94. s2 = x[ 1] - x[ 0]; x[ 9] = x[ 1] + x[0];
  95. s3 = x[ 3] - x[ 2]; x[11] = x[ 3] + x[2];
  96. x[ 0] = MULT31((s0 - s1) , cPI2_8);
  97. x[ 1] = MULT31((s2 + s3) , cPI2_8);
  98. x[ 2] = MULT31((s0 + s1) , cPI2_8);
  99. x[ 3] = MULT31((s3 - s2) , cPI2_8);
  100. MB();
  101. s2 = x[12] - x[13]; x[12] += x[13];
  102. s3 = x[14] - x[15]; x[14] += x[15];
  103. s0 = x[ 4] - x[ 5]; x[13] = x[ 5] + x[ 4];
  104. s1 = x[ 7] - x[ 6]; x[15] = x[ 7] + x[ 6];
  105. x[ 4] = s2; x[ 5] = s1;
  106. x[ 6] = s3; x[ 7] = s0;
  107. MB();
  108. mdct_butterfly_8(x);
  109. mdct_butterfly_8(x+8);
  110. }
  111. /* 32 point butterfly (in place, 4 register) */
  112. STIN void mdct_butterfly_32(DATA_TYPE *x){
  113. REG_TYPE s0, s1, s2, s3;
  114. s0 = x[16] - x[17]; x[16] += x[17];
  115. s1 = x[18] - x[19]; x[18] += x[19];
  116. s2 = x[ 1] - x[ 0]; x[17] = x[ 1] + x[ 0];
  117. s3 = x[ 3] - x[ 2]; x[19] = x[ 3] + x[ 2];
  118. XNPROD31( s0, s1, cPI3_8, cPI1_8, &x[ 0], &x[ 2] );
  119. XPROD31 ( s2, s3, cPI1_8, cPI3_8, &x[ 1], &x[ 3] );
  120. MB();
  121. s0 = x[20] - x[21]; x[20] += x[21];
  122. s1 = x[22] - x[23]; x[22] += x[23];
  123. s2 = x[ 5] - x[ 4]; x[21] = x[ 5] + x[ 4];
  124. s3 = x[ 7] - x[ 6]; x[23] = x[ 7] + x[ 6];
  125. x[ 4] = MULT31((s0 - s1) , cPI2_8);
  126. x[ 5] = MULT31((s3 + s2) , cPI2_8);
  127. x[ 6] = MULT31((s0 + s1) , cPI2_8);
  128. x[ 7] = MULT31((s3 - s2) , cPI2_8);
  129. MB();
  130. s0 = x[24] - x[25]; x[24] += x[25];
  131. s1 = x[26] - x[27]; x[26] += x[27];
  132. s2 = x[ 9] - x[ 8]; x[25] = x[ 9] + x[ 8];
  133. s3 = x[11] - x[10]; x[27] = x[11] + x[10];
  134. XNPROD31( s0, s1, cPI1_8, cPI3_8, &x[ 8], &x[10] );
  135. XPROD31 ( s2, s3, cPI3_8, cPI1_8, &x[ 9], &x[11] );
  136. MB();
  137. s0 = x[28] - x[29]; x[28] += x[29];
  138. s1 = x[30] - x[31]; x[30] += x[31];
  139. s2 = x[12] - x[13]; x[29] = x[13] + x[12];
  140. s3 = x[15] - x[14]; x[31] = x[15] + x[14];
  141. x[12] = s0; x[13] = s3;
  142. x[14] = s1; x[15] = s2;
  143. MB();
  144. mdct_butterfly_16(x);
  145. mdct_butterfly_16(x+16);
  146. }
  147. /* N/stage point generic N stage butterfly (in place, 2 register) */
  148. STIN void mdct_butterfly_generic(DATA_TYPE *x,int points,int step){
  149. LOOKUP_T *T = sincos_lookup0;
  150. DATA_TYPE *x1 = x + points - 4;
  151. DATA_TYPE *x2 = x + (points>>1) - 4;
  152. REG_TYPE s0, s1, s2, s3;
  153. do{
  154. s0 = x1[0] - x1[1]; x1[0] += x1[1];
  155. s1 = x1[3] - x1[2]; x1[2] += x1[3];
  156. s2 = x2[1] - x2[0]; x1[1] = x2[1] + x2[0];
  157. s3 = x2[3] - x2[2]; x1[3] = x2[3] + x2[2];
  158. XPROD31( s1, s0, T[0], T[1], &x2[0], &x2[2] );
  159. XPROD31( s2, s3, T[0], T[1], &x2[1], &x2[3] ); T+=step;
  160. x1-=4;
  161. x2-=4;
  162. }while(T<sincos_lookup0+1024);
  163. x1 = x + (points>>1) + (points>>2) - 4;
  164. x2 = x + (points>>2) - 4;
  165. T = sincos_lookup0+1024;
  166. do{
  167. s0 = x1[0] - x1[1]; x1[0] += x1[1];
  168. s1 = x1[2] - x1[3]; x1[2] += x1[3];
  169. s2 = x2[0] - x2[1]; x1[1] = x2[1] + x2[0];
  170. s3 = x2[3] - x2[2]; x1[3] = x2[3] + x2[2];
  171. XNPROD31( s0, s1, T[0], T[1], &x2[0], &x2[2] );
  172. XNPROD31( s3, s2, T[0], T[1], &x2[1], &x2[3] ); T-=step;
  173. x1-=4;
  174. x2-=4;
  175. }while(T>sincos_lookup0);
  176. }
  177. STIN void mdct_butterflies(DATA_TYPE *x,int points,int shift){
  178. int stages=7-shift;
  179. int i,j;
  180. for(i=0;--stages>=0;i++){
  181. for(j=0;j<(1<<i);j++)
  182. {
  183. mdct_butterfly_generic(x+(points>>i)*j,points>>i,4<<(i+shift));
  184. }
  185. }
  186. for(j=0;j<points;j+=32)
  187. mdct_butterfly_32(x+j);
  188. }
  189. static unsigned char bitrev[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
  190. STIN int bitrev12(int x){
  191. return bitrev[x>>8]|(bitrev[(x&0x0f0)>>4]<<4)|(((int)bitrev[x&0x00f])<<8);
  192. }
  193. STIN void mdct_bitreverse(DATA_TYPE *x,int n,int shift){
  194. int bit = 0;
  195. DATA_TYPE *w = x+(n>>1);
  196. do{
  197. DATA_TYPE b = bitrev12(bit++);
  198. DATA_TYPE *xx = x + (b>>shift);
  199. REG_TYPE r;
  200. w -= 2;
  201. if(w>xx){
  202. r = xx[0];
  203. xx[0] = w[0];
  204. w[0] = r;
  205. r = xx[1];
  206. xx[1] = w[1];
  207. w[1] = r;
  208. }
  209. }while(w>x);
  210. }
  211. STIN void mdct_step7(DATA_TYPE *x,int n,int step){
  212. DATA_TYPE *w0 = x;
  213. DATA_TYPE *w1 = x+(n>>1);
  214. LOOKUP_T *T = (step>=4)?(sincos_lookup0+(step>>1)):sincos_lookup1;
  215. LOOKUP_T *Ttop = T+1024;
  216. REG_TYPE s0, s1, s2, s3;
  217. do{
  218. w1 -= 2;
  219. s0 = w0[0] + w1[0];
  220. s1 = w1[1] - w0[1];
  221. s2 = MULT32(s0, T[1]) + MULT32(s1, T[0]);
  222. s3 = MULT32(s1, T[1]) - MULT32(s0, T[0]);
  223. T+=step;
  224. s0 = (w0[1] + w1[1])>>1;
  225. s1 = (w0[0] - w1[0])>>1;
  226. w0[0] = s0 + s2;
  227. w0[1] = s1 + s3;
  228. w1[0] = s0 - s2;
  229. w1[1] = s3 - s1;
  230. w0 += 2;
  231. }while(T<Ttop);
  232. do{
  233. w1 -= 2;
  234. s0 = w0[0] + w1[0];
  235. s1 = w1[1] - w0[1];
  236. T-=step;
  237. s2 = MULT32(s0, T[0]) + MULT32(s1, T[1]);
  238. s3 = MULT32(s1, T[0]) - MULT32(s0, T[1]);
  239. s0 = (w0[1] + w1[1])>>1;
  240. s1 = (w0[0] - w1[0])>>1;
  241. w0[0] = s0 + s2;
  242. w0[1] = s1 + s3;
  243. w1[0] = s0 - s2;
  244. w1[1] = s3 - s1;
  245. w0 += 2;
  246. }while(w0<w1);
  247. }
  248. #endif
  249. STIN void mdct_step8(DATA_TYPE *x, int n, int step){
  250. LOOKUP_T *T;
  251. LOOKUP_T *V;
  252. DATA_TYPE *iX =x+(n>>1);
  253. switch(step) {
  254. #if defined(ONLY_C)
  255. default:
  256. T=(step>=4)?(sincos_lookup0+(step>>1)):sincos_lookup1;
  257. do{
  258. REG_TYPE s0 = x[0];
  259. REG_TYPE s1 = -x[1];
  260. XPROD31( s0, s1, T[0], T[1], x, x+1); T+=step;
  261. x +=2;
  262. }while(x<iX);
  263. break;
  264. #endif
  265. case 1:
  266. {
  267. /* linear interpolation between table values: offset=0.5, step=1 */
  268. REG_TYPE t0,t1,v0,v1,s0,s1;
  269. T = sincos_lookup0;
  270. V = sincos_lookup1;
  271. t0 = (*T++)>>1;
  272. t1 = (*T++)>>1;
  273. do{
  274. s0 = x[0];
  275. s1 = -x[1];
  276. t0 += (v0 = (*V++)>>1);
  277. t1 += (v1 = (*V++)>>1);
  278. XPROD31( s0, s1, t0, t1, x, x+1 );
  279. s0 = x[2];
  280. s1 = -x[3];
  281. v0 += (t0 = (*T++)>>1);
  282. v1 += (t1 = (*T++)>>1);
  283. XPROD31( s0, s1, v0, v1, x+2, x+3 );
  284. x += 4;
  285. }while(x<iX);
  286. break;
  287. }
  288. case 0:
  289. {
  290. /* linear interpolation between table values: offset=0.25, step=0.5 */
  291. REG_TYPE t0,t1,v0,v1,q0,q1,s0,s1;
  292. T = sincos_lookup0;
  293. V = sincos_lookup1;
  294. t0 = *T++;
  295. t1 = *T++;
  296. do{
  297. v0 = *V++;
  298. v1 = *V++;
  299. t0 += (q0 = (v0-t0)>>2);
  300. t1 += (q1 = (v1-t1)>>2);
  301. s0 = x[0];
  302. s1 = -x[1];
  303. XPROD31( s0, s1, t0, t1, x, x+1 );
  304. t0 = v0-q0;
  305. t1 = v1-q1;
  306. s0 = x[2];
  307. s1 = -x[3];
  308. XPROD31( s0, s1, t0, t1, x+2, x+3 );
  309. t0 = *T++;
  310. t1 = *T++;
  311. v0 += (q0 = (t0-v0)>>2);
  312. v1 += (q1 = (t1-v1)>>2);
  313. s0 = x[4];
  314. s1 = -x[5];
  315. XPROD31( s0, s1, v0, v1, x+4, x+5 );
  316. v0 = t0-q0;
  317. v1 = t1-q1;
  318. s0 = x[6];
  319. s1 = -x[7];
  320. XPROD31( s0, s1, v0, v1, x+5, x+6 );
  321. x+=8;
  322. }while(x<iX);
  323. break;
  324. }
  325. }
  326. }
  327. extern int mdct_backwardARM(int n, DATA_TYPE *in);
  328. /* partial; doesn't perform last-step deinterleave/unrolling. That
  329. can be done more efficiently during pcm output */
  330. void mdct_backward(int n, DATA_TYPE *in){
  331. int step;
  332. #if defined(ONLY_C)
  333. int shift;
  334. for (shift=4;!(n&(1<<shift));shift++);
  335. shift=13-shift;
  336. step=2<<shift;
  337. presymmetry(in,n>>1,step);
  338. mdct_butterflies(in,n>>1,shift);
  339. mdct_bitreverse(in,n,shift);
  340. mdct_step7(in,n,step);
  341. mdct_step8(in,n,step>>2);
  342. #else
  343. step = mdct_backwardARM(n, in);
  344. if (step < 1)
  345. mdct_step8(in,n,step);
  346. #endif
  347. }
  348. #if defined(ONLY_C)
  349. void mdct_shift_right(int n, DATA_TYPE *in, DATA_TYPE *right){
  350. int i;
  351. n>>=2;
  352. in+=1;
  353. for(i=0;i<n;i++)
  354. right[i]=in[i<<1];
  355. }
  356. #endif
  357. extern ogg_int16_t *mdct_unroll_prelap(ogg_int16_t *out,
  358. DATA_TYPE *post,
  359. DATA_TYPE *l,
  360. int step);
  361. extern ogg_int16_t *mdct_unroll_part2(ogg_int16_t *out,
  362. DATA_TYPE *post,
  363. DATA_TYPE *l,
  364. DATA_TYPE *r,
  365. int step,
  366. LOOKUP_T *wL,
  367. LOOKUP_T *wR);
  368. extern ogg_int16_t *mdct_unroll_part3(ogg_int16_t *out,
  369. DATA_TYPE *post,
  370. DATA_TYPE *l,
  371. DATA_TYPE *r,
  372. int step,
  373. LOOKUP_T *wL,
  374. LOOKUP_T *wR);
  375. extern ogg_int16_t *mdct_unroll_postlap(ogg_int16_t *out,
  376. DATA_TYPE *post,
  377. DATA_TYPE *l,
  378. int step);
  379. void mdct_unroll_lap(int n0,int n1,
  380. int lW,int W,
  381. DATA_TYPE *in,
  382. DATA_TYPE *right,
  383. LOOKUP_T *w0,
  384. LOOKUP_T *w1,
  385. ogg_int16_t *out,
  386. int step,
  387. int start, /* samples, this frame */
  388. int end /* samples, this frame */){
  389. DATA_TYPE *l=in+(W&&lW ? n1>>1 : n0>>1);
  390. DATA_TYPE *r=right+(lW ? n1>>2 : n0>>2);
  391. DATA_TYPE *post;
  392. LOOKUP_T *wR=(W && lW ? w1+(n1>>1) : w0+(n0>>1));
  393. LOOKUP_T *wL=(W && lW ? w1 : w0 );
  394. int preLap=(lW && !W ? (n1>>2)-(n0>>2) : 0 );
  395. int halfLap=(lW && W ? (n1>>2) : (n0>>2) );
  396. int postLap=(!lW && W ? (n1>>2)-(n0>>2) : 0 );
  397. int n,off;
  398. /* preceeding direct-copy lapping from previous frame, if any */
  399. if(preLap){
  400. n = (end<preLap?end:preLap);
  401. off = (start<preLap?start:preLap);
  402. post = r-n;
  403. r -= off;
  404. start -= off;
  405. end -= n;
  406. #if defined(ONLY_C)
  407. while(r>post){
  408. *out = CLIP_TO_15((*--r)>>9);
  409. out+=step;
  410. }
  411. #else
  412. out = mdct_unroll_prelap(out,post,r,step);
  413. n -= off;
  414. if (n < 0)
  415. n = 0;
  416. r -= n;
  417. #endif
  418. }
  419. /* cross-lap; two halves due to wrap-around */
  420. n = (end<halfLap?end:halfLap);
  421. off = (start<halfLap?start:halfLap);
  422. post = r-n;
  423. r -= off;
  424. l -= off*2;
  425. start -= off;
  426. wR -= off;
  427. wL += off;
  428. end -= n;
  429. #if defined(ONLY_C)
  430. while(r>post){
  431. l-=2;
  432. *out = CLIP_TO_15((MULT31(*--r,*--wR) + MULT31(*l,*wL++))>>9);
  433. out+=step;
  434. }
  435. #else
  436. out = mdct_unroll_part2(out, post, l, r, step, wL, wR);
  437. n -= off;
  438. if (n < 0)
  439. n = 0;
  440. l -= 2*n;
  441. r -= n;
  442. wR -= n;
  443. wL += n;
  444. #endif
  445. n = (end<halfLap?end:halfLap);
  446. off = (start<halfLap?start:halfLap);
  447. post = r+n;
  448. r += off;
  449. l += off*2;
  450. start -= off;
  451. end -= n;
  452. wR -= off;
  453. wL += off;
  454. #if defined(ONLY_C)
  455. while(r<post){
  456. *out = CLIP_TO_15((MULT31(*r++,*--wR) - MULT31(*l,*wL++))>>9);
  457. out+=step;
  458. l+=2;
  459. }
  460. #else
  461. out = mdct_unroll_part3(out, post, l, r, step, wL, wR);
  462. n -= off;
  463. if (n < 0)
  464. n = 0;
  465. l += 2*n;
  466. r += n;
  467. wR -= n;
  468. wL += n;
  469. #endif
  470. /* preceeding direct-copy lapping from previous frame, if any */
  471. if(postLap){
  472. n = (end<postLap?end:postLap);
  473. off = (start<postLap?start:postLap);
  474. post = l+n*2;
  475. l += off*2;
  476. #if defined(ONLY_C)
  477. while(l<post){
  478. *out = CLIP_TO_15((-*l)>>9);
  479. out+=step;
  480. l+=2;
  481. }
  482. #else
  483. out = mdct_unroll_postlap(out,post,l,step);
  484. #endif
  485. }
  486. }