idct.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora 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 Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id: idct.c 17410 2010-09-21 21:53:48Z tterribe $
  14. ********************************************************************/
  15. #include <string.h>
  16. #include "internal.h"
  17. #include "dct.h"
  18. /*Performs an inverse 8 point Type-II DCT transform.
  19. The output is scaled by a factor of 2 relative to the orthonormal version of
  20. the transform.
  21. _y: The buffer to store the result in.
  22. Data will be placed in every 8th entry (e.g., in a column of an 8x8
  23. block).
  24. _x: The input coefficients.
  25. The first 8 entries are used (e.g., from a row of an 8x8 block).*/
  26. static void idct8(ogg_int16_t *_y,const ogg_int16_t _x[8]){
  27. ogg_int32_t t[8];
  28. ogg_int32_t r;
  29. /*Stage 1:*/
  30. /*0-1 butterfly.*/
  31. t[0]=OC_C4S4*(ogg_int16_t)(_x[0]+_x[4])>>16;
  32. t[1]=OC_C4S4*(ogg_int16_t)(_x[0]-_x[4])>>16;
  33. /*2-3 rotation by 6pi/16.*/
  34. t[2]=(OC_C6S2*_x[2]>>16)-(OC_C2S6*_x[6]>>16);
  35. t[3]=(OC_C2S6*_x[2]>>16)+(OC_C6S2*_x[6]>>16);
  36. /*4-7 rotation by 7pi/16.*/
  37. t[4]=(OC_C7S1*_x[1]>>16)-(OC_C1S7*_x[7]>>16);
  38. /*5-6 rotation by 3pi/16.*/
  39. t[5]=(OC_C3S5*_x[5]>>16)-(OC_C5S3*_x[3]>>16);
  40. t[6]=(OC_C5S3*_x[5]>>16)+(OC_C3S5*_x[3]>>16);
  41. t[7]=(OC_C1S7*_x[1]>>16)+(OC_C7S1*_x[7]>>16);
  42. /*Stage 2:*/
  43. /*4-5 butterfly.*/
  44. r=t[4]+t[5];
  45. t[5]=OC_C4S4*(ogg_int16_t)(t[4]-t[5])>>16;
  46. t[4]=r;
  47. /*7-6 butterfly.*/
  48. r=t[7]+t[6];
  49. t[6]=OC_C4S4*(ogg_int16_t)(t[7]-t[6])>>16;
  50. t[7]=r;
  51. /*Stage 3:*/
  52. /*0-3 butterfly.*/
  53. r=t[0]+t[3];
  54. t[3]=t[0]-t[3];
  55. t[0]=r;
  56. /*1-2 butterfly.*/
  57. r=t[1]+t[2];
  58. t[2]=t[1]-t[2];
  59. t[1]=r;
  60. /*6-5 butterfly.*/
  61. r=t[6]+t[5];
  62. t[5]=t[6]-t[5];
  63. t[6]=r;
  64. /*Stage 4:*/
  65. /*0-7 butterfly.*/
  66. _y[0<<3]=(ogg_int16_t)(t[0]+t[7]);
  67. /*1-6 butterfly.*/
  68. _y[1<<3]=(ogg_int16_t)(t[1]+t[6]);
  69. /*2-5 butterfly.*/
  70. _y[2<<3]=(ogg_int16_t)(t[2]+t[5]);
  71. /*3-4 butterfly.*/
  72. _y[3<<3]=(ogg_int16_t)(t[3]+t[4]);
  73. _y[4<<3]=(ogg_int16_t)(t[3]-t[4]);
  74. _y[5<<3]=(ogg_int16_t)(t[2]-t[5]);
  75. _y[6<<3]=(ogg_int16_t)(t[1]-t[6]);
  76. _y[7<<3]=(ogg_int16_t)(t[0]-t[7]);
  77. }
  78. /*Performs an inverse 8 point Type-II DCT transform.
  79. The output is scaled by a factor of 2 relative to the orthonormal version of
  80. the transform.
  81. _y: The buffer to store the result in.
  82. Data will be placed in every 8th entry (e.g., in a column of an 8x8
  83. block).
  84. _x: The input coefficients.
  85. Only the first 4 entries are used.
  86. The other 4 are assumed to be 0.*/
  87. static void idct8_4(ogg_int16_t *_y,const ogg_int16_t _x[8]){
  88. ogg_int32_t t[8];
  89. ogg_int32_t r;
  90. /*Stage 1:*/
  91. t[0]=OC_C4S4*_x[0]>>16;
  92. t[2]=OC_C6S2*_x[2]>>16;
  93. t[3]=OC_C2S6*_x[2]>>16;
  94. t[4]=OC_C7S1*_x[1]>>16;
  95. t[5]=-(OC_C5S3*_x[3]>>16);
  96. t[6]=OC_C3S5*_x[3]>>16;
  97. t[7]=OC_C1S7*_x[1]>>16;
  98. /*Stage 2:*/
  99. r=t[4]+t[5];
  100. t[5]=OC_C4S4*(ogg_int16_t)(t[4]-t[5])>>16;
  101. t[4]=r;
  102. r=t[7]+t[6];
  103. t[6]=OC_C4S4*(ogg_int16_t)(t[7]-t[6])>>16;
  104. t[7]=r;
  105. /*Stage 3:*/
  106. t[1]=t[0]+t[2];
  107. t[2]=t[0]-t[2];
  108. r=t[0]+t[3];
  109. t[3]=t[0]-t[3];
  110. t[0]=r;
  111. r=t[6]+t[5];
  112. t[5]=t[6]-t[5];
  113. t[6]=r;
  114. /*Stage 4:*/
  115. _y[0<<3]=(ogg_int16_t)(t[0]+t[7]);
  116. _y[1<<3]=(ogg_int16_t)(t[1]+t[6]);
  117. _y[2<<3]=(ogg_int16_t)(t[2]+t[5]);
  118. _y[3<<3]=(ogg_int16_t)(t[3]+t[4]);
  119. _y[4<<3]=(ogg_int16_t)(t[3]-t[4]);
  120. _y[5<<3]=(ogg_int16_t)(t[2]-t[5]);
  121. _y[6<<3]=(ogg_int16_t)(t[1]-t[6]);
  122. _y[7<<3]=(ogg_int16_t)(t[0]-t[7]);
  123. }
  124. /*Performs an inverse 8 point Type-II DCT transform.
  125. The output is scaled by a factor of 2 relative to the orthonormal version of
  126. the transform.
  127. _y: The buffer to store the result in.
  128. Data will be placed in every 8th entry (e.g., in a column of an 8x8
  129. block).
  130. _x: The input coefficients.
  131. Only the first 3 entries are used.
  132. The other 5 are assumed to be 0.*/
  133. static void idct8_3(ogg_int16_t *_y,const ogg_int16_t _x[8]){
  134. ogg_int32_t t[8];
  135. ogg_int32_t r;
  136. /*Stage 1:*/
  137. t[0]=OC_C4S4*_x[0]>>16;
  138. t[2]=OC_C6S2*_x[2]>>16;
  139. t[3]=OC_C2S6*_x[2]>>16;
  140. t[4]=OC_C7S1*_x[1]>>16;
  141. t[7]=OC_C1S7*_x[1]>>16;
  142. /*Stage 2:*/
  143. t[5]=OC_C4S4*t[4]>>16;
  144. t[6]=OC_C4S4*t[7]>>16;
  145. /*Stage 3:*/
  146. t[1]=t[0]+t[2];
  147. t[2]=t[0]-t[2];
  148. r=t[0]+t[3];
  149. t[3]=t[0]-t[3];
  150. t[0]=r;
  151. r=t[6]+t[5];
  152. t[5]=t[6]-t[5];
  153. t[6]=r;
  154. /*Stage 4:*/
  155. _y[0<<3]=(ogg_int16_t)(t[0]+t[7]);
  156. _y[1<<3]=(ogg_int16_t)(t[1]+t[6]);
  157. _y[2<<3]=(ogg_int16_t)(t[2]+t[5]);
  158. _y[3<<3]=(ogg_int16_t)(t[3]+t[4]);
  159. _y[4<<3]=(ogg_int16_t)(t[3]-t[4]);
  160. _y[5<<3]=(ogg_int16_t)(t[2]-t[5]);
  161. _y[6<<3]=(ogg_int16_t)(t[1]-t[6]);
  162. _y[7<<3]=(ogg_int16_t)(t[0]-t[7]);
  163. }
  164. /*Performs an inverse 8 point Type-II DCT transform.
  165. The output is scaled by a factor of 2 relative to the orthonormal version of
  166. the transform.
  167. _y: The buffer to store the result in.
  168. Data will be placed in every 8th entry (e.g., in a column of an 8x8
  169. block).
  170. _x: The input coefficients.
  171. Only the first 2 entries are used.
  172. The other 6 are assumed to be 0.*/
  173. static void idct8_2(ogg_int16_t *_y,const ogg_int16_t _x[8]){
  174. ogg_int32_t t[8];
  175. ogg_int32_t r;
  176. /*Stage 1:*/
  177. t[0]=OC_C4S4*_x[0]>>16;
  178. t[4]=OC_C7S1*_x[1]>>16;
  179. t[7]=OC_C1S7*_x[1]>>16;
  180. /*Stage 2:*/
  181. t[5]=OC_C4S4*t[4]>>16;
  182. t[6]=OC_C4S4*t[7]>>16;
  183. /*Stage 3:*/
  184. r=t[6]+t[5];
  185. t[5]=t[6]-t[5];
  186. t[6]=r;
  187. /*Stage 4:*/
  188. _y[0<<3]=(ogg_int16_t)(t[0]+t[7]);
  189. _y[1<<3]=(ogg_int16_t)(t[0]+t[6]);
  190. _y[2<<3]=(ogg_int16_t)(t[0]+t[5]);
  191. _y[3<<3]=(ogg_int16_t)(t[0]+t[4]);
  192. _y[4<<3]=(ogg_int16_t)(t[0]-t[4]);
  193. _y[5<<3]=(ogg_int16_t)(t[0]-t[5]);
  194. _y[6<<3]=(ogg_int16_t)(t[0]-t[6]);
  195. _y[7<<3]=(ogg_int16_t)(t[0]-t[7]);
  196. }
  197. /*Performs an inverse 8 point Type-II DCT transform.
  198. The output is scaled by a factor of 2 relative to the orthonormal version of
  199. the transform.
  200. _y: The buffer to store the result in.
  201. Data will be placed in every 8th entry (e.g., in a column of an 8x8
  202. block).
  203. _x: The input coefficients.
  204. Only the first entry is used.
  205. The other 7 are assumed to be 0.*/
  206. static void idct8_1(ogg_int16_t *_y,const ogg_int16_t _x[1]){
  207. _y[0<<3]=_y[1<<3]=_y[2<<3]=_y[3<<3]=
  208. _y[4<<3]=_y[5<<3]=_y[6<<3]=_y[7<<3]=(ogg_int16_t)(OC_C4S4*_x[0]>>16);
  209. }
  210. /*Performs an inverse 8x8 Type-II DCT transform.
  211. The input is assumed to be scaled by a factor of 4 relative to orthonormal
  212. version of the transform.
  213. All coefficients but the first 3 in zig-zag scan order are assumed to be 0:
  214. x x 0 0 0 0 0 0
  215. x 0 0 0 0 0 0 0
  216. 0 0 0 0 0 0 0 0
  217. 0 0 0 0 0 0 0 0
  218. 0 0 0 0 0 0 0 0
  219. 0 0 0 0 0 0 0 0
  220. 0 0 0 0 0 0 0 0
  221. 0 0 0 0 0 0 0 0
  222. _y: The buffer to store the result in.
  223. This may be the same as _x.
  224. _x: The input coefficients.*/
  225. static void oc_idct8x8_3(ogg_int16_t _y[64],ogg_int16_t _x[64]){
  226. ogg_int16_t w[64];
  227. int i;
  228. /*Transform rows of x into columns of w.*/
  229. idct8_2(w,_x);
  230. idct8_1(w+1,_x+8);
  231. /*Transform rows of w into columns of y.*/
  232. for(i=0;i<8;i++)idct8_2(_y+i,w+i*8);
  233. /*Adjust for the scale factor.*/
  234. for(i=0;i<64;i++)_y[i]=(ogg_int16_t)(_y[i]+8>>4);
  235. /*Clear input data for next block (decoder only).*/
  236. if(_x!=_y)_x[0]=_x[1]=_x[8]=0;
  237. }
  238. /*Performs an inverse 8x8 Type-II DCT transform.
  239. The input is assumed to be scaled by a factor of 4 relative to orthonormal
  240. version of the transform.
  241. All coefficients but the first 10 in zig-zag scan order are assumed to be 0:
  242. x x x x 0 0 0 0
  243. x x x 0 0 0 0 0
  244. x x 0 0 0 0 0 0
  245. x 0 0 0 0 0 0 0
  246. 0 0 0 0 0 0 0 0
  247. 0 0 0 0 0 0 0 0
  248. 0 0 0 0 0 0 0 0
  249. 0 0 0 0 0 0 0 0
  250. _y: The buffer to store the result in.
  251. This may be the same as _x.
  252. _x: The input coefficients.*/
  253. static void oc_idct8x8_10(ogg_int16_t _y[64],ogg_int16_t _x[64]){
  254. ogg_int16_t w[64];
  255. int i;
  256. /*Transform rows of x into columns of w.*/
  257. idct8_4(w,_x);
  258. idct8_3(w+1,_x+8);
  259. idct8_2(w+2,_x+16);
  260. idct8_1(w+3,_x+24);
  261. /*Transform rows of w into columns of y.*/
  262. for(i=0;i<8;i++)idct8_4(_y+i,w+i*8);
  263. /*Adjust for the scale factor.*/
  264. for(i=0;i<64;i++)_y[i]=(ogg_int16_t)(_y[i]+8>>4);
  265. /*Clear input data for next block (decoder only).*/
  266. if(_x!=_y)_x[0]=_x[1]=_x[2]=_x[3]=_x[8]=_x[9]=_x[10]=_x[16]=_x[17]=_x[24]=0;
  267. }
  268. /*Performs an inverse 8x8 Type-II DCT transform.
  269. The input is assumed to be scaled by a factor of 4 relative to orthonormal
  270. version of the transform.
  271. _y: The buffer to store the result in.
  272. This may be the same as _x.
  273. _x: The input coefficients.*/
  274. static void oc_idct8x8_slow(ogg_int16_t _y[64],ogg_int16_t _x[64]){
  275. ogg_int16_t w[64];
  276. int i;
  277. /*Transform rows of x into columns of w.*/
  278. for(i=0;i<8;i++)idct8(w+i,_x+i*8);
  279. /*Transform rows of w into columns of y.*/
  280. for(i=0;i<8;i++)idct8(_y+i,w+i*8);
  281. /*Adjust for the scale factor.*/
  282. for(i=0;i<64;i++)_y[i]=(ogg_int16_t)(_y[i]+8>>4);
  283. if(_x!=_y)for(i=0;i<64;i++)_x[i]=0;
  284. }
  285. /*Performs an inverse 8x8 Type-II DCT transform.
  286. The input is assumed to be scaled by a factor of 4 relative to orthonormal
  287. version of the transform.*/
  288. void oc_idct8x8_c(ogg_int16_t _y[64],ogg_int16_t _x[64],int _last_zzi){
  289. /*_last_zzi is subtly different from an actual count of the number of
  290. coefficients we decoded for this block.
  291. It contains the value of zzi BEFORE the final token in the block was
  292. decoded.
  293. In most cases this is an EOB token (the continuation of an EOB run from a
  294. previous block counts), and so this is the same as the coefficient count.
  295. However, in the case that the last token was NOT an EOB token, but filled
  296. the block up with exactly 64 coefficients, _last_zzi will be less than 64.
  297. Provided the last token was not a pure zero run, the minimum value it can
  298. be is 46, and so that doesn't affect any of the cases in this routine.
  299. However, if the last token WAS a pure zero run of length 63, then _last_zzi
  300. will be 1 while the number of coefficients decoded is 64.
  301. Thus, we will trigger the following special case, where the real
  302. coefficient count would not.
  303. Note also that a zero run of length 64 will give _last_zzi a value of 0,
  304. but we still process the DC coefficient, which might have a non-zero value
  305. due to DC prediction.
  306. Although convoluted, this is arguably the correct behavior: it allows us to
  307. use a smaller transform when the block ends with a long zero run instead
  308. of a normal EOB token.
  309. It could be smarter... multiple separate zero runs at the end of a block
  310. will fool it, but an encoder that generates these really deserves what it
  311. gets.
  312. Needless to say we inherited this approach from VP3.*/
  313. /*Then perform the iDCT.*/
  314. if(_last_zzi<=3)oc_idct8x8_3(_y,_x);
  315. else if(_last_zzi<=10)oc_idct8x8_10(_y,_x);
  316. else oc_idct8x8_slow(_y,_x);
  317. }