mdct.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /********************************************************************
  2. * *
  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. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
  9. * by the Xiph.Org Foundation https://xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: normalized modified discrete cosine transform
  13. power of two length transform only [64 <= n ]
  14. Original algorithm adapted long ago from _The use of multirate filter
  15. banks for coding of high quality digital audio_, by T. Sporer,
  16. K. Brandenburg and B. Edler, collection of the European Signal
  17. Processing Conference (EUSIPCO), Amsterdam, June 1992, Vol.1, pp
  18. 211-214
  19. The below code implements an algorithm that no longer looks much like
  20. that presented in the paper, but the basic structure remains if you
  21. dig deep enough to see it.
  22. This module DOES NOT INCLUDE code to generate/apply the window
  23. function. Everybody has their own weird favorite including me... I
  24. happen to like the properties of y=sin(.5PI*sin^2(x)), but others may
  25. vehemently disagree.
  26. ********************************************************************/
  27. /* this can also be run as an integer transform by uncommenting a
  28. define in mdct.h; the integerization is a first pass and although
  29. it's likely stable for Vorbis, the dynamic range is constrained and
  30. roundoff isn't done (so it's noisy). Consider it functional, but
  31. only a starting point. There's no point on a machine with an FPU */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <math.h>
  36. #include "vorbis/codec.h"
  37. #include "mdct.h"
  38. #include "os.h"
  39. #include "misc.h"
  40. /* build lookups for trig functions; also pre-figure scaling and
  41. some window function algebra. */
  42. void mdct_init(mdct_lookup *lookup,int n){
  43. int *bitrev=_ogg_malloc(sizeof(*bitrev)*(n/4));
  44. DATA_TYPE *T=_ogg_malloc(sizeof(*T)*(n+n/4));
  45. int i;
  46. int n2=n>>1;
  47. int log2n=lookup->log2n=rint(log((float)n)/log(2.f));
  48. lookup->n=n;
  49. lookup->trig=T;
  50. lookup->bitrev=bitrev;
  51. /* trig lookups... */
  52. for(i=0;i<n/4;i++){
  53. T[i*2]=FLOAT_CONV(cos((M_PI/n)*(4*i)));
  54. T[i*2+1]=FLOAT_CONV(-sin((M_PI/n)*(4*i)));
  55. T[n2+i*2]=FLOAT_CONV(cos((M_PI/(2*n))*(2*i+1)));
  56. T[n2+i*2+1]=FLOAT_CONV(sin((M_PI/(2*n))*(2*i+1)));
  57. }
  58. for(i=0;i<n/8;i++){
  59. T[n+i*2]=FLOAT_CONV(cos((M_PI/n)*(4*i+2))*.5);
  60. T[n+i*2+1]=FLOAT_CONV(-sin((M_PI/n)*(4*i+2))*.5);
  61. }
  62. /* bitreverse lookup... */
  63. {
  64. int mask=(1<<(log2n-1))-1,i,j;
  65. int msb=1<<(log2n-2);
  66. for(i=0;i<n/8;i++){
  67. int acc=0;
  68. for(j=0;msb>>j;j++)
  69. if((msb>>j)&i)acc|=1<<j;
  70. bitrev[i*2]=((~acc)&mask)-1;
  71. bitrev[i*2+1]=acc;
  72. }
  73. }
  74. lookup->scale=FLOAT_CONV(4.f/n);
  75. }
  76. /* 8 point butterfly (in place, 4 register) */
  77. STIN void mdct_butterfly_8(DATA_TYPE *x){
  78. REG_TYPE r0 = x[6] + x[2];
  79. REG_TYPE r1 = x[6] - x[2];
  80. REG_TYPE r2 = x[4] + x[0];
  81. REG_TYPE r3 = x[4] - x[0];
  82. x[6] = r0 + r2;
  83. x[4] = r0 - r2;
  84. r0 = x[5] - x[1];
  85. r2 = x[7] - x[3];
  86. x[0] = r1 + r0;
  87. x[2] = r1 - r0;
  88. r0 = x[5] + x[1];
  89. r1 = x[7] + x[3];
  90. x[3] = r2 + r3;
  91. x[1] = r2 - r3;
  92. x[7] = r1 + r0;
  93. x[5] = r1 - r0;
  94. }
  95. /* 16 point butterfly (in place, 4 register) */
  96. STIN void mdct_butterfly_16(DATA_TYPE *x){
  97. REG_TYPE r0 = x[1] - x[9];
  98. REG_TYPE r1 = x[0] - x[8];
  99. x[8] += x[0];
  100. x[9] += x[1];
  101. x[0] = MULT_NORM((r0 + r1) * cPI2_8);
  102. x[1] = MULT_NORM((r0 - r1) * cPI2_8);
  103. r0 = x[3] - x[11];
  104. r1 = x[10] - x[2];
  105. x[10] += x[2];
  106. x[11] += x[3];
  107. x[2] = r0;
  108. x[3] = r1;
  109. r0 = x[12] - x[4];
  110. r1 = x[13] - x[5];
  111. x[12] += x[4];
  112. x[13] += x[5];
  113. x[4] = MULT_NORM((r0 - r1) * cPI2_8);
  114. x[5] = MULT_NORM((r0 + r1) * cPI2_8);
  115. r0 = x[14] - x[6];
  116. r1 = x[15] - x[7];
  117. x[14] += x[6];
  118. x[15] += x[7];
  119. x[6] = r0;
  120. x[7] = r1;
  121. mdct_butterfly_8(x);
  122. mdct_butterfly_8(x+8);
  123. }
  124. /* 32 point butterfly (in place, 4 register) */
  125. STIN void mdct_butterfly_32(DATA_TYPE *x){
  126. REG_TYPE r0 = x[30] - x[14];
  127. REG_TYPE r1 = x[31] - x[15];
  128. x[30] += x[14];
  129. x[31] += x[15];
  130. x[14] = r0;
  131. x[15] = r1;
  132. r0 = x[28] - x[12];
  133. r1 = x[29] - x[13];
  134. x[28] += x[12];
  135. x[29] += x[13];
  136. x[12] = MULT_NORM( r0 * cPI1_8 - r1 * cPI3_8 );
  137. x[13] = MULT_NORM( r0 * cPI3_8 + r1 * cPI1_8 );
  138. r0 = x[26] - x[10];
  139. r1 = x[27] - x[11];
  140. x[26] += x[10];
  141. x[27] += x[11];
  142. x[10] = MULT_NORM(( r0 - r1 ) * cPI2_8);
  143. x[11] = MULT_NORM(( r0 + r1 ) * cPI2_8);
  144. r0 = x[24] - x[8];
  145. r1 = x[25] - x[9];
  146. x[24] += x[8];
  147. x[25] += x[9];
  148. x[8] = MULT_NORM( r0 * cPI3_8 - r1 * cPI1_8 );
  149. x[9] = MULT_NORM( r1 * cPI3_8 + r0 * cPI1_8 );
  150. r0 = x[22] - x[6];
  151. r1 = x[7] - x[23];
  152. x[22] += x[6];
  153. x[23] += x[7];
  154. x[6] = r1;
  155. x[7] = r0;
  156. r0 = x[4] - x[20];
  157. r1 = x[5] - x[21];
  158. x[20] += x[4];
  159. x[21] += x[5];
  160. x[4] = MULT_NORM( r1 * cPI1_8 + r0 * cPI3_8 );
  161. x[5] = MULT_NORM( r1 * cPI3_8 - r0 * cPI1_8 );
  162. r0 = x[2] - x[18];
  163. r1 = x[3] - x[19];
  164. x[18] += x[2];
  165. x[19] += x[3];
  166. x[2] = MULT_NORM(( r1 + r0 ) * cPI2_8);
  167. x[3] = MULT_NORM(( r1 - r0 ) * cPI2_8);
  168. r0 = x[0] - x[16];
  169. r1 = x[1] - x[17];
  170. x[16] += x[0];
  171. x[17] += x[1];
  172. x[0] = MULT_NORM( r1 * cPI3_8 + r0 * cPI1_8 );
  173. x[1] = MULT_NORM( r1 * cPI1_8 - r0 * cPI3_8 );
  174. mdct_butterfly_16(x);
  175. mdct_butterfly_16(x+16);
  176. }
  177. /* N point first stage butterfly (in place, 2 register) */
  178. STIN void mdct_butterfly_first(DATA_TYPE *T,
  179. DATA_TYPE *x,
  180. int points){
  181. DATA_TYPE *x1 = x + points - 8;
  182. DATA_TYPE *x2 = x + (points>>1) - 8;
  183. REG_TYPE r0;
  184. REG_TYPE r1;
  185. do{
  186. r0 = x1[6] - x2[6];
  187. r1 = x1[7] - x2[7];
  188. x1[6] += x2[6];
  189. x1[7] += x2[7];
  190. x2[6] = MULT_NORM(r1 * T[1] + r0 * T[0]);
  191. x2[7] = MULT_NORM(r1 * T[0] - r0 * T[1]);
  192. r0 = x1[4] - x2[4];
  193. r1 = x1[5] - x2[5];
  194. x1[4] += x2[4];
  195. x1[5] += x2[5];
  196. x2[4] = MULT_NORM(r1 * T[5] + r0 * T[4]);
  197. x2[5] = MULT_NORM(r1 * T[4] - r0 * T[5]);
  198. r0 = x1[2] - x2[2];
  199. r1 = x1[3] - x2[3];
  200. x1[2] += x2[2];
  201. x1[3] += x2[3];
  202. x2[2] = MULT_NORM(r1 * T[9] + r0 * T[8]);
  203. x2[3] = MULT_NORM(r1 * T[8] - r0 * T[9]);
  204. r0 = x1[0] - x2[0];
  205. r1 = x1[1] - x2[1];
  206. x1[0] += x2[0];
  207. x1[1] += x2[1];
  208. x2[0] = MULT_NORM(r1 * T[13] + r0 * T[12]);
  209. x2[1] = MULT_NORM(r1 * T[12] - r0 * T[13]);
  210. x1-=8;
  211. x2-=8;
  212. T+=16;
  213. }while(x2>=x);
  214. }
  215. /* N/stage point generic N stage butterfly (in place, 2 register) */
  216. STIN void mdct_butterfly_generic(DATA_TYPE *T,
  217. DATA_TYPE *x,
  218. int points,
  219. int trigint){
  220. DATA_TYPE *x1 = x + points - 8;
  221. DATA_TYPE *x2 = x + (points>>1) - 8;
  222. REG_TYPE r0;
  223. REG_TYPE r1;
  224. do{
  225. r0 = x1[6] - x2[6];
  226. r1 = x1[7] - x2[7];
  227. x1[6] += x2[6];
  228. x1[7] += x2[7];
  229. x2[6] = MULT_NORM(r1 * T[1] + r0 * T[0]);
  230. x2[7] = MULT_NORM(r1 * T[0] - r0 * T[1]);
  231. T+=trigint;
  232. r0 = x1[4] - x2[4];
  233. r1 = x1[5] - x2[5];
  234. x1[4] += x2[4];
  235. x1[5] += x2[5];
  236. x2[4] = MULT_NORM(r1 * T[1] + r0 * T[0]);
  237. x2[5] = MULT_NORM(r1 * T[0] - r0 * T[1]);
  238. T+=trigint;
  239. r0 = x1[2] - x2[2];
  240. r1 = x1[3] - x2[3];
  241. x1[2] += x2[2];
  242. x1[3] += x2[3];
  243. x2[2] = MULT_NORM(r1 * T[1] + r0 * T[0]);
  244. x2[3] = MULT_NORM(r1 * T[0] - r0 * T[1]);
  245. T+=trigint;
  246. r0 = x1[0] - x2[0];
  247. r1 = x1[1] - x2[1];
  248. x1[0] += x2[0];
  249. x1[1] += x2[1];
  250. x2[0] = MULT_NORM(r1 * T[1] + r0 * T[0]);
  251. x2[1] = MULT_NORM(r1 * T[0] - r0 * T[1]);
  252. T+=trigint;
  253. x1-=8;
  254. x2-=8;
  255. }while(x2>=x);
  256. }
  257. STIN void mdct_butterflies(mdct_lookup *init,
  258. DATA_TYPE *x,
  259. int points){
  260. DATA_TYPE *T=init->trig;
  261. int stages=init->log2n-5;
  262. int i,j;
  263. if(--stages>0){
  264. mdct_butterfly_first(T,x,points);
  265. }
  266. for(i=1;--stages>0;i++){
  267. for(j=0;j<(1<<i);j++)
  268. mdct_butterfly_generic(T,x+(points>>i)*j,points>>i,4<<i);
  269. }
  270. for(j=0;j<points;j+=32)
  271. mdct_butterfly_32(x+j);
  272. }
  273. void mdct_clear(mdct_lookup *l){
  274. if(l){
  275. if(l->trig)_ogg_free(l->trig);
  276. if(l->bitrev)_ogg_free(l->bitrev);
  277. memset(l,0,sizeof(*l));
  278. }
  279. }
  280. STIN void mdct_bitreverse(mdct_lookup *init,
  281. DATA_TYPE *x){
  282. int n = init->n;
  283. int *bit = init->bitrev;
  284. DATA_TYPE *w0 = x;
  285. DATA_TYPE *w1 = x = w0+(n>>1);
  286. DATA_TYPE *T = init->trig+n;
  287. do{
  288. DATA_TYPE *x0 = x+bit[0];
  289. DATA_TYPE *x1 = x+bit[1];
  290. REG_TYPE r0 = x0[1] - x1[1];
  291. REG_TYPE r1 = x0[0] + x1[0];
  292. REG_TYPE r2 = MULT_NORM(r1 * T[0] + r0 * T[1]);
  293. REG_TYPE r3 = MULT_NORM(r1 * T[1] - r0 * T[0]);
  294. w1 -= 4;
  295. r0 = HALVE(x0[1] + x1[1]);
  296. r1 = HALVE(x0[0] - x1[0]);
  297. w0[0] = r0 + r2;
  298. w1[2] = r0 - r2;
  299. w0[1] = r1 + r3;
  300. w1[3] = r3 - r1;
  301. x0 = x+bit[2];
  302. x1 = x+bit[3];
  303. r0 = x0[1] - x1[1];
  304. r1 = x0[0] + x1[0];
  305. r2 = MULT_NORM(r1 * T[2] + r0 * T[3]);
  306. r3 = MULT_NORM(r1 * T[3] - r0 * T[2]);
  307. r0 = HALVE(x0[1] + x1[1]);
  308. r1 = HALVE(x0[0] - x1[0]);
  309. w0[2] = r0 + r2;
  310. w1[0] = r0 - r2;
  311. w0[3] = r1 + r3;
  312. w1[1] = r3 - r1;
  313. T += 4;
  314. bit += 4;
  315. w0 += 4;
  316. }while(w0<w1);
  317. }
  318. void mdct_backward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out){
  319. int n=init->n;
  320. int n2=n>>1;
  321. int n4=n>>2;
  322. /* rotate */
  323. DATA_TYPE *iX = in+n2-7;
  324. DATA_TYPE *oX = out+n2+n4;
  325. DATA_TYPE *T = init->trig+n4;
  326. do{
  327. oX -= 4;
  328. oX[0] = MULT_NORM(-iX[2] * T[3] - iX[0] * T[2]);
  329. oX[1] = MULT_NORM (iX[0] * T[3] - iX[2] * T[2]);
  330. oX[2] = MULT_NORM(-iX[6] * T[1] - iX[4] * T[0]);
  331. oX[3] = MULT_NORM (iX[4] * T[1] - iX[6] * T[0]);
  332. iX -= 8;
  333. T += 4;
  334. }while(iX>=in);
  335. iX = in+n2-8;
  336. oX = out+n2+n4;
  337. T = init->trig+n4;
  338. do{
  339. T -= 4;
  340. oX[0] = MULT_NORM (iX[4] * T[3] + iX[6] * T[2]);
  341. oX[1] = MULT_NORM (iX[4] * T[2] - iX[6] * T[3]);
  342. oX[2] = MULT_NORM (iX[0] * T[1] + iX[2] * T[0]);
  343. oX[3] = MULT_NORM (iX[0] * T[0] - iX[2] * T[1]);
  344. iX -= 8;
  345. oX += 4;
  346. }while(iX>=in);
  347. mdct_butterflies(init,out+n2,n2);
  348. mdct_bitreverse(init,out);
  349. /* roatate + window */
  350. {
  351. DATA_TYPE *oX1=out+n2+n4;
  352. DATA_TYPE *oX2=out+n2+n4;
  353. DATA_TYPE *iX =out;
  354. T =init->trig+n2;
  355. do{
  356. oX1-=4;
  357. oX1[3] = MULT_NORM (iX[0] * T[1] - iX[1] * T[0]);
  358. oX2[0] = -MULT_NORM (iX[0] * T[0] + iX[1] * T[1]);
  359. oX1[2] = MULT_NORM (iX[2] * T[3] - iX[3] * T[2]);
  360. oX2[1] = -MULT_NORM (iX[2] * T[2] + iX[3] * T[3]);
  361. oX1[1] = MULT_NORM (iX[4] * T[5] - iX[5] * T[4]);
  362. oX2[2] = -MULT_NORM (iX[4] * T[4] + iX[5] * T[5]);
  363. oX1[0] = MULT_NORM (iX[6] * T[7] - iX[7] * T[6]);
  364. oX2[3] = -MULT_NORM (iX[6] * T[6] + iX[7] * T[7]);
  365. oX2+=4;
  366. iX += 8;
  367. T += 8;
  368. }while(iX<oX1);
  369. iX=out+n2+n4;
  370. oX1=out+n4;
  371. oX2=oX1;
  372. do{
  373. oX1-=4;
  374. iX-=4;
  375. oX2[0] = -(oX1[3] = iX[3]);
  376. oX2[1] = -(oX1[2] = iX[2]);
  377. oX2[2] = -(oX1[1] = iX[1]);
  378. oX2[3] = -(oX1[0] = iX[0]);
  379. oX2+=4;
  380. }while(oX2<iX);
  381. iX=out+n2+n4;
  382. oX1=out+n2+n4;
  383. oX2=out+n2;
  384. do{
  385. oX1-=4;
  386. oX1[0]= iX[3];
  387. oX1[1]= iX[2];
  388. oX1[2]= iX[1];
  389. oX1[3]= iX[0];
  390. iX+=4;
  391. }while(oX1>oX2);
  392. }
  393. }
  394. void mdct_forward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out){
  395. int n=init->n;
  396. int n2=n>>1;
  397. int n4=n>>2;
  398. int n8=n>>3;
  399. DATA_TYPE *w=alloca(n*sizeof(*w)); /* forward needs working space */
  400. DATA_TYPE *w2=w+n2;
  401. /* rotate */
  402. /* window + rotate + step 1 */
  403. REG_TYPE r0;
  404. REG_TYPE r1;
  405. DATA_TYPE *x0=in+n2+n4;
  406. DATA_TYPE *x1=x0+1;
  407. DATA_TYPE *T=init->trig+n2;
  408. int i=0;
  409. for(i=0;i<n8;i+=2){
  410. x0 -=4;
  411. T-=2;
  412. r0= x0[2] + x1[0];
  413. r1= x0[0] + x1[2];
  414. w2[i]= MULT_NORM(r1*T[1] + r0*T[0]);
  415. w2[i+1]= MULT_NORM(r1*T[0] - r0*T[1]);
  416. x1 +=4;
  417. }
  418. x1=in+1;
  419. for(;i<n2-n8;i+=2){
  420. T-=2;
  421. x0 -=4;
  422. r0= x0[2] - x1[0];
  423. r1= x0[0] - x1[2];
  424. w2[i]= MULT_NORM(r1*T[1] + r0*T[0]);
  425. w2[i+1]= MULT_NORM(r1*T[0] - r0*T[1]);
  426. x1 +=4;
  427. }
  428. x0=in+n;
  429. for(;i<n2;i+=2){
  430. T-=2;
  431. x0 -=4;
  432. r0= -x0[2] - x1[0];
  433. r1= -x0[0] - x1[2];
  434. w2[i]= MULT_NORM(r1*T[1] + r0*T[0]);
  435. w2[i+1]= MULT_NORM(r1*T[0] - r0*T[1]);
  436. x1 +=4;
  437. }
  438. mdct_butterflies(init,w+n2,n2);
  439. mdct_bitreverse(init,w);
  440. /* roatate + window */
  441. T=init->trig+n2;
  442. x0=out+n2;
  443. for(i=0;i<n4;i++){
  444. x0--;
  445. out[i] =MULT_NORM((w[0]*T[0]+w[1]*T[1])*init->scale);
  446. x0[0] =MULT_NORM((w[0]*T[1]-w[1]*T[0])*init->scale);
  447. w+=2;
  448. T+=2;
  449. }
  450. }