fdct.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include "encint.h"
  16. #include "dct.h"
  17. /*Performs a forward 8 point Type-II DCT transform.
  18. The output is scaled by a factor of 2 from the orthonormal version of the
  19. transform.
  20. _y: The buffer to store the result in.
  21. Data will be placed the first 8 entries (e.g., in a row of an 8x8 block).
  22. _x: The input coefficients.
  23. Every 8th entry is used (e.g., from a column of an 8x8 block).*/
  24. static void oc_fdct8(ogg_int16_t _y[8],const ogg_int16_t *_x){
  25. int t0;
  26. int t1;
  27. int t2;
  28. int t3;
  29. int t4;
  30. int t5;
  31. int t6;
  32. int t7;
  33. int r;
  34. int s;
  35. int u;
  36. int v;
  37. /*Stage 1:*/
  38. /*0-7 butterfly.*/
  39. t0=_x[0<<3]+(int)_x[7<<3];
  40. t7=_x[0<<3]-(int)_x[7<<3];
  41. /*1-6 butterfly.*/
  42. t1=_x[1<<3]+(int)_x[6<<3];
  43. t6=_x[1<<3]-(int)_x[6<<3];
  44. /*2-5 butterfly.*/
  45. t2=_x[2<<3]+(int)_x[5<<3];
  46. t5=_x[2<<3]-(int)_x[5<<3];
  47. /*3-4 butterfly.*/
  48. t3=_x[3<<3]+(int)_x[4<<3];
  49. t4=_x[3<<3]-(int)_x[4<<3];
  50. /*Stage 2:*/
  51. /*0-3 butterfly.*/
  52. r=t0+t3;
  53. t3=t0-t3;
  54. t0=r;
  55. /*1-2 butterfly.*/
  56. r=t1+t2;
  57. t2=t1-t2;
  58. t1=r;
  59. /*6-5 butterfly.*/
  60. r=t6+t5;
  61. t5=t6-t5;
  62. t6=r;
  63. /*Stages 3 and 4 are where all the approximation occurs.
  64. These are chosen to be as close to an exact inverse of the approximations
  65. made in the iDCT as possible, while still using mostly 16-bit arithmetic.
  66. We use some 16x16->32 signed MACs, but those still commonly execute in 1
  67. cycle on a 16-bit DSP.
  68. For example, s=(27146*t5+0x4000>>16)+t5+(t5!=0) is an exact inverse of
  69. t5=(OC_C4S4*s>>16).
  70. That is, applying the latter to the output of the former will recover t5
  71. exactly (over the valid input range of t5, -23171...23169).
  72. We increase the rounding bias to 0xB500 in this particular case so that
  73. errors inverting the subsequent butterfly are not one-sided (e.g., the
  74. mean error is very close to zero).
  75. The (t5!=0) term could be replaced simply by 1, but we want to send 0 to 0.
  76. The fDCT of an all-zeros block will still not be zero, because of the
  77. biases we added at the very beginning of the process, but it will be close
  78. enough that it is guaranteed to round to zero.*/
  79. /*Stage 3:*/
  80. /*4-5 butterfly.*/
  81. s=(27146*t5+0xB500>>16)+t5+(t5!=0)>>1;
  82. r=t4+s;
  83. t5=t4-s;
  84. t4=r;
  85. /*7-6 butterfly.*/
  86. s=(27146*t6+0xB500>>16)+t6+(t6!=0)>>1;
  87. r=t7+s;
  88. t6=t7-s;
  89. t7=r;
  90. /*Stage 4:*/
  91. /*0-1 butterfly.*/
  92. r=(27146*t0+0x4000>>16)+t0+(t0!=0);
  93. s=(27146*t1+0xB500>>16)+t1+(t1!=0);
  94. u=r+s>>1;
  95. v=r-u;
  96. _y[0]=u;
  97. _y[4]=v;
  98. /*3-2 rotation by 6pi/16*/
  99. u=(OC_C6S2*t2+OC_C2S6*t3+0x6CB7>>16)+(t3!=0);
  100. s=(OC_C6S2*u>>16)-t2;
  101. v=(s*21600+0x2800>>18)+s+(s!=0);
  102. _y[2]=u;
  103. _y[6]=v;
  104. /*6-5 rotation by 3pi/16*/
  105. u=(OC_C5S3*t6+OC_C3S5*t5+0x0E3D>>16)+(t5!=0);
  106. s=t6-(OC_C5S3*u>>16);
  107. v=(s*26568+0x3400>>17)+s+(s!=0);
  108. _y[5]=u;
  109. _y[3]=v;
  110. /*7-4 rotation by 7pi/16*/
  111. u=(OC_C7S1*t4+OC_C1S7*t7+0x7B1B>>16)+(t7!=0);
  112. s=(OC_C7S1*u>>16)-t4;
  113. v=(s*20539+0x3000>>20)+s+(s!=0);
  114. _y[1]=u;
  115. _y[7]=v;
  116. }
  117. /*Performs a forward 8x8 Type-II DCT transform.
  118. The output is scaled by a factor of 4 relative to the orthonormal version
  119. of the transform.
  120. _y: The buffer to store the result in.
  121. This may be the same as _x.
  122. _x: The input coefficients. */
  123. void oc_enc_fdct8x8_c(ogg_int16_t _y[64],const ogg_int16_t _x[64]){
  124. const ogg_int16_t *in;
  125. ogg_int16_t *end;
  126. ogg_int16_t *out;
  127. ogg_int16_t w[64];
  128. int i;
  129. /*Add two extra bits of working precision to improve accuracy; any more and
  130. we could overflow.*/
  131. for(i=0;i<64;i++)w[i]=_x[i]<<2;
  132. /*These biases correct for some systematic error that remains in the full
  133. fDCT->iDCT round trip.*/
  134. w[0]+=(w[0]!=0)+1;
  135. w[1]++;
  136. w[8]--;
  137. /*Transform columns of w into rows of _y.*/
  138. for(in=w,out=_y,end=out+64;out<end;in++,out+=8)oc_fdct8(out,in);
  139. /*Transform columns of _y into rows of w.*/
  140. for(in=_y,out=w,end=out+64;out<end;in++,out+=8)oc_fdct8(out,in);
  141. /*Round the result back to the external working precision (which is still
  142. scaled by four relative to the orthogonal result).
  143. TODO: We should just update the external working precision.*/
  144. for(i=0;i<64;i++)_y[i]=w[OC_FZIG_ZAG[i]]+2>>2;
  145. }
  146. /*This does not seem to outperform simple LFE border padding before MC.
  147. It yields higher PSNR, but much higher bitrate usage.*/
  148. #if 0
  149. typedef struct oc_extension_info oc_extension_info;
  150. /*Information needed to pad boundary blocks.
  151. We multiply each row/column by an extension matrix that fills in the padding
  152. values as a linear combination of the active values, so that an equivalent
  153. number of coefficients are forced to zero.
  154. This costs at most 16 multiplies, the same as a 1-D fDCT itself, and as
  155. little as 7 multiplies.
  156. We compute the extension matrices for every possible shape in advance, as
  157. there are only 35.
  158. The coefficients for all matrices are stored in a single array to take
  159. advantage of the overlap and repetitiveness of many of the shapes.
  160. A similar technique is applied to the offsets into this array.
  161. This reduces the required table storage by about 48%.
  162. See tools/extgen.c for details.
  163. We could conceivably do the same for all 256 possible shapes.*/
  164. struct oc_extension_info{
  165. /*The mask of the active pixels in the shape.*/
  166. short mask;
  167. /*The number of active pixels in the shape.*/
  168. short na;
  169. /*The extension matrix.
  170. This is (8-na)xna*/
  171. const ogg_int16_t *const *ext;
  172. /*The pixel indices: na active pixels followed by 8-na padding pixels.*/
  173. unsigned char pi[8];
  174. /*The coefficient indices: na unconstrained coefficients followed by 8-na
  175. coefficients to be forced to zero.*/
  176. unsigned char ci[8];
  177. };
  178. /*The number of shapes we need.*/
  179. #define OC_NSHAPES (35)
  180. static const ogg_int16_t OC_EXT_COEFFS[229]={
  181. 0x7FFF,0xE1F8,0x6903,0xAA79,0x5587,0x7FFF,0x1E08,0x7FFF,
  182. 0x5587,0xAA79,0x6903,0xE1F8,0x7FFF,0x0000,0x0000,0x0000,
  183. 0x7FFF,0x0000,0x0000,0x7FFF,0x8000,0x7FFF,0x0000,0x0000,
  184. 0x7FFF,0xE1F8,0x1E08,0xB0A7,0xAA1D,0x337C,0x7FFF,0x4345,
  185. 0x2267,0x4345,0x7FFF,0x337C,0xAA1D,0xB0A7,0x8A8C,0x4F59,
  186. 0x03B4,0xE2D6,0x7FFF,0x2CF3,0x7FFF,0xE2D6,0x03B4,0x4F59,
  187. 0x8A8C,0x1103,0x7AEF,0x5225,0xDF60,0xC288,0xDF60,0x5225,
  188. 0x7AEF,0x1103,0x668A,0xD6EE,0x3A16,0x0E6C,0xFA07,0x0E6C,
  189. 0x3A16,0xD6EE,0x668A,0x2A79,0x2402,0x980F,0x50F5,0x4882,
  190. 0x50F5,0x980F,0x2402,0x2A79,0xF976,0x2768,0x5F22,0x2768,
  191. 0xF976,0x1F91,0x76C1,0xE9AE,0x76C1,0x1F91,0x7FFF,0xD185,
  192. 0x0FC8,0xD185,0x7FFF,0x4F59,0x4345,0xED62,0x4345,0x4F59,
  193. 0xF574,0x5D99,0x2CF3,0x5D99,0xF574,0x5587,0x3505,0x30FC,
  194. 0xF482,0x953C,0xEAC4,0x7FFF,0x4F04,0x7FFF,0xEAC4,0x953C,
  195. 0xF482,0x30FC,0x4F04,0x273D,0xD8C3,0x273D,0x1E09,0x61F7,
  196. 0x1E09,0x273D,0xD8C3,0x273D,0x4F04,0x30FC,0xA57E,0x153C,
  197. 0x6AC4,0x3C7A,0x1E08,0x3C7A,0x6AC4,0x153C,0xA57E,0x7FFF,
  198. 0xA57E,0x5A82,0x6AC4,0x153C,0xC386,0xE1F8,0xC386,0x153C,
  199. 0x6AC4,0x5A82,0xD8C3,0x273D,0x7FFF,0xE1F7,0x7FFF,0x273D,
  200. 0xD8C3,0x4F04,0x30FC,0xD8C3,0x273D,0xD8C3,0x30FC,0x4F04,
  201. 0x1FC8,0x67AD,0x1853,0xE038,0x1853,0x67AD,0x1FC8,0x4546,
  202. 0xE038,0x1FC8,0x3ABA,0x1FC8,0xE038,0x4546,0x3505,0x5587,
  203. 0xF574,0xBC11,0x78F4,0x4AFB,0xE6F3,0x4E12,0x3C11,0xF8F4,
  204. 0x4AFB,0x3C7A,0xF88B,0x3C11,0x78F4,0xCAFB,0x7FFF,0x08CC,
  205. 0x070C,0x236D,0x5587,0x236D,0x070C,0xF88B,0x3C7A,0x4AFB,
  206. 0xF8F4,0x3C11,0x7FFF,0x153C,0xCAFB,0x153C,0x7FFF,0x1E08,
  207. 0xE1F8,0x7FFF,0x08CC,0x7FFF,0xCAFB,0x78F4,0x3C11,0x4E12,
  208. 0xE6F3,0x4AFB,0x78F4,0xBC11,0xFE3D,0x7FFF,0xFE3D,0x2F3A,
  209. 0x7FFF,0x2F3A,0x89BC,0x7FFF,0x89BC
  210. };
  211. static const ogg_int16_t *const OC_EXT_ROWS[96]={
  212. OC_EXT_COEFFS+ 0,OC_EXT_COEFFS+ 0,OC_EXT_COEFFS+ 0,OC_EXT_COEFFS+ 0,
  213. OC_EXT_COEFFS+ 0,OC_EXT_COEFFS+ 0,OC_EXT_COEFFS+ 0,OC_EXT_COEFFS+ 6,
  214. OC_EXT_COEFFS+ 27,OC_EXT_COEFFS+ 38,OC_EXT_COEFFS+ 43,OC_EXT_COEFFS+ 32,
  215. OC_EXT_COEFFS+ 49,OC_EXT_COEFFS+ 58,OC_EXT_COEFFS+ 67,OC_EXT_COEFFS+ 71,
  216. OC_EXT_COEFFS+ 62,OC_EXT_COEFFS+ 53,OC_EXT_COEFFS+ 12,OC_EXT_COEFFS+ 15,
  217. OC_EXT_COEFFS+ 14,OC_EXT_COEFFS+ 13,OC_EXT_COEFFS+ 76,OC_EXT_COEFFS+ 81,
  218. OC_EXT_COEFFS+ 86,OC_EXT_COEFFS+ 91,OC_EXT_COEFFS+ 96,OC_EXT_COEFFS+ 98,
  219. OC_EXT_COEFFS+ 93,OC_EXT_COEFFS+ 88,OC_EXT_COEFFS+ 83,OC_EXT_COEFFS+ 78,
  220. OC_EXT_COEFFS+ 12,OC_EXT_COEFFS+ 15,OC_EXT_COEFFS+ 15,OC_EXT_COEFFS+ 12,
  221. OC_EXT_COEFFS+ 12,OC_EXT_COEFFS+ 15,OC_EXT_COEFFS+ 12,OC_EXT_COEFFS+ 15,
  222. OC_EXT_COEFFS+ 15,OC_EXT_COEFFS+ 12,OC_EXT_COEFFS+ 103,OC_EXT_COEFFS+ 108,
  223. OC_EXT_COEFFS+ 126,OC_EXT_COEFFS+ 16,OC_EXT_COEFFS+ 137,OC_EXT_COEFFS+ 141,
  224. OC_EXT_COEFFS+ 20,OC_EXT_COEFFS+ 130,OC_EXT_COEFFS+ 113,OC_EXT_COEFFS+ 116,
  225. OC_EXT_COEFFS+ 146,OC_EXT_COEFFS+ 153,OC_EXT_COEFFS+ 160,OC_EXT_COEFFS+ 167,
  226. OC_EXT_COEFFS+ 170,OC_EXT_COEFFS+ 163,OC_EXT_COEFFS+ 156,OC_EXT_COEFFS+ 149,
  227. OC_EXT_COEFFS+ 119,OC_EXT_COEFFS+ 122,OC_EXT_COEFFS+ 174,OC_EXT_COEFFS+ 177,
  228. OC_EXT_COEFFS+ 182,OC_EXT_COEFFS+ 187,OC_EXT_COEFFS+ 192,OC_EXT_COEFFS+ 197,
  229. OC_EXT_COEFFS+ 202,OC_EXT_COEFFS+ 207,OC_EXT_COEFFS+ 210,OC_EXT_COEFFS+ 215,
  230. OC_EXT_COEFFS+ 179,OC_EXT_COEFFS+ 189,OC_EXT_COEFFS+ 24,OC_EXT_COEFFS+ 204,
  231. OC_EXT_COEFFS+ 184,OC_EXT_COEFFS+ 194,OC_EXT_COEFFS+ 212,OC_EXT_COEFFS+ 199,
  232. OC_EXT_COEFFS+ 217,OC_EXT_COEFFS+ 100,OC_EXT_COEFFS+ 134,OC_EXT_COEFFS+ 135,
  233. OC_EXT_COEFFS+ 135,OC_EXT_COEFFS+ 12,OC_EXT_COEFFS+ 15,OC_EXT_COEFFS+ 134,
  234. OC_EXT_COEFFS+ 134,OC_EXT_COEFFS+ 135,OC_EXT_COEFFS+ 220,OC_EXT_COEFFS+ 223,
  235. OC_EXT_COEFFS+ 226,OC_EXT_COEFFS+ 227,OC_EXT_COEFFS+ 224,OC_EXT_COEFFS+ 221
  236. };
  237. static const oc_extension_info OC_EXTENSION_INFO[OC_NSHAPES]={
  238. {0x7F,7,OC_EXT_ROWS+ 0,{0,1,2,3,4,5,6,7},{0,1,2,4,5,6,7,3}},
  239. {0xFE,7,OC_EXT_ROWS+ 7,{1,2,3,4,5,6,7,0},{0,1,2,4,5,6,7,3}},
  240. {0x3F,6,OC_EXT_ROWS+ 8,{0,1,2,3,4,5,7,6},{0,1,3,4,6,7,5,2}},
  241. {0xFC,6,OC_EXT_ROWS+ 10,{2,3,4,5,6,7,1,0},{0,1,3,4,6,7,5,2}},
  242. {0x1F,5,OC_EXT_ROWS+ 12,{0,1,2,3,4,7,6,5},{0,2,3,5,7,6,4,1}},
  243. {0xF8,5,OC_EXT_ROWS+ 15,{3,4,5,6,7,2,1,0},{0,2,3,5,7,6,4,1}},
  244. {0x0F,4,OC_EXT_ROWS+ 18,{0,1,2,3,7,6,5,4},{0,2,4,6,7,5,3,1}},
  245. {0xF0,4,OC_EXT_ROWS+ 18,{4,5,6,7,3,2,1,0},{0,2,4,6,7,5,3,1}},
  246. {0x07,3,OC_EXT_ROWS+ 22,{0,1,2,7,6,5,4,3},{0,3,6,7,5,4,2,1}},
  247. {0xE0,3,OC_EXT_ROWS+ 27,{5,6,7,4,3,2,1,0},{0,3,6,7,5,4,2,1}},
  248. {0x03,2,OC_EXT_ROWS+ 32,{0,1,7,6,5,4,3,2},{0,4,7,6,5,3,2,1}},
  249. {0xC0,2,OC_EXT_ROWS+ 32,{6,7,5,4,3,2,1,0},{0,4,7,6,5,3,2,1}},
  250. {0x01,1,OC_EXT_ROWS+ 0,{0,7,6,5,4,3,2,1},{0,7,6,5,4,3,2,1}},
  251. {0x80,1,OC_EXT_ROWS+ 0,{7,6,5,4,3,2,1,0},{0,7,6,5,4,3,2,1}},
  252. {0x7E,6,OC_EXT_ROWS+ 42,{1,2,3,4,5,6,7,0},{0,1,2,5,6,7,4,3}},
  253. {0x7C,5,OC_EXT_ROWS+ 44,{2,3,4,5,6,7,1,0},{0,1,4,5,7,6,3,2}},
  254. {0x3E,5,OC_EXT_ROWS+ 47,{1,2,3,4,5,7,6,0},{0,1,4,5,7,6,3,2}},
  255. {0x78,4,OC_EXT_ROWS+ 50,{3,4,5,6,7,2,1,0},{0,4,5,7,6,3,2,1}},
  256. {0x3C,4,OC_EXT_ROWS+ 54,{2,3,4,5,7,6,1,0},{0,3,4,7,6,5,2,1}},
  257. {0x1E,4,OC_EXT_ROWS+ 58,{1,2,3,4,7,6,5,0},{0,4,5,7,6,3,2,1}},
  258. {0x70,3,OC_EXT_ROWS+ 62,{4,5,6,7,3,2,1,0},{0,5,7,6,4,3,2,1}},
  259. {0x38,3,OC_EXT_ROWS+ 67,{3,4,5,7,6,2,1,0},{0,5,6,7,4,3,2,1}},
  260. {0x1C,3,OC_EXT_ROWS+ 72,{2,3,4,7,6,5,1,0},{0,5,6,7,4,3,2,1}},
  261. {0x0E,3,OC_EXT_ROWS+ 77,{1,2,3,7,6,5,4,0},{0,5,7,6,4,3,2,1}},
  262. {0x60,2,OC_EXT_ROWS+ 82,{5,6,7,4,3,2,1,0},{0,2,7,6,5,4,3,1}},
  263. {0x30,2,OC_EXT_ROWS+ 36,{4,5,7,6,3,2,1,0},{0,4,7,6,5,3,2,1}},
  264. {0x18,2,OC_EXT_ROWS+ 90,{3,4,7,6,5,2,1,0},{0,1,7,6,5,4,3,2}},
  265. {0x0C,2,OC_EXT_ROWS+ 34,{2,3,7,6,5,4,1,0},{0,4,7,6,5,3,2,1}},
  266. {0x06,2,OC_EXT_ROWS+ 84,{1,2,7,6,5,4,3,0},{0,2,7,6,5,4,3,1}},
  267. {0x40,1,OC_EXT_ROWS+ 0,{6,7,5,4,3,2,1,0},{0,7,6,5,4,3,2,1}},
  268. {0x20,1,OC_EXT_ROWS+ 0,{5,7,6,4,3,2,1,0},{0,7,6,5,4,3,2,1}},
  269. {0x10,1,OC_EXT_ROWS+ 0,{4,7,6,5,3,2,1,0},{0,7,6,5,4,3,2,1}},
  270. {0x08,1,OC_EXT_ROWS+ 0,{3,7,6,5,4,2,1,0},{0,7,6,5,4,3,2,1}},
  271. {0x04,1,OC_EXT_ROWS+ 0,{2,7,6,5,4,3,1,0},{0,7,6,5,4,3,2,1}},
  272. {0x02,1,OC_EXT_ROWS+ 0,{1,7,6,5,4,3,2,0},{0,7,6,5,4,3,2,1}}
  273. };
  274. /*Pads a single column of a partial block and then performs a forward Type-II
  275. DCT on the result.
  276. The input is scaled by a factor of 4 and biased appropriately for the current
  277. fDCT implementation.
  278. The output is scaled by an additional factor of 2 from the orthonormal
  279. version of the transform.
  280. _y: The buffer to store the result in.
  281. Data will be placed the first 8 entries (e.g., in a row of an 8x8 block).
  282. _x: The input coefficients.
  283. Every 8th entry is used (e.g., from a column of an 8x8 block).
  284. _e: The extension information for the shape.*/
  285. static void oc_fdct8_ext(ogg_int16_t _y[8],ogg_int16_t *_x,
  286. const oc_extension_info *_e){
  287. const unsigned char *pi;
  288. int na;
  289. na=_e->na;
  290. pi=_e->pi;
  291. if(na==1){
  292. int ci;
  293. /*While the branch below is still correct for shapes with na==1, we can
  294. perform the entire transform with just 1 multiply in this case instead
  295. of 23.*/
  296. _y[0]=(ogg_int16_t)(OC_DIV2_16(OC_C4S4*(_x[pi[0]])));
  297. for(ci=1;ci<8;ci++)_y[ci]=0;
  298. }
  299. else{
  300. const ogg_int16_t *const *ext;
  301. int zpi;
  302. int api;
  303. int nz;
  304. /*First multiply by the extension matrix to compute the padding values.*/
  305. nz=8-na;
  306. ext=_e->ext;
  307. for(zpi=0;zpi<nz;zpi++){
  308. ogg_int32_t v;
  309. v=0;
  310. for(api=0;api<na;api++){
  311. v+=ext[zpi][api]*(ogg_int32_t)(_x[pi[api]<<3]<<1);
  312. }
  313. _x[pi[na+zpi]<<3]=(ogg_int16_t)(v+0x8000>>16)+1>>1;
  314. }
  315. oc_fdct8(_y,_x);
  316. }
  317. }
  318. /*Performs a forward 8x8 Type-II DCT transform on blocks which overlap the
  319. border of the picture region.
  320. This method ONLY works with rectangular regions.
  321. _border: A description of which pixels are inside the border.
  322. _y: The buffer to store the result in.
  323. This may be the same as _x.
  324. _x: The input pixel values.
  325. Pixel values outside the border will be ignored.*/
  326. void oc_fdct8x8_border(const oc_border_info *_border,
  327. ogg_int16_t _y[64],const ogg_int16_t _x[64]){
  328. ogg_int16_t *in;
  329. ogg_int16_t *out;
  330. ogg_int16_t w[64];
  331. ogg_int64_t mask;
  332. const oc_extension_info *cext;
  333. const oc_extension_info *rext;
  334. int cmask;
  335. int rmask;
  336. int ri;
  337. int ci;
  338. /*Identify the shapes of the non-zero rows and columns.*/
  339. rmask=cmask=0;
  340. mask=_border->mask;
  341. for(ri=0;ri<8;ri++){
  342. /*This aggregation is _only_ correct for rectangular masks.*/
  343. cmask|=((mask&0xFF)!=0)<<ri;
  344. rmask|=mask&0xFF;
  345. mask>>=8;
  346. }
  347. /*Find the associated extension info for these shapes.*/
  348. if(cmask==0xFF)cext=NULL;
  349. else for(cext=OC_EXTENSION_INFO;cext->mask!=cmask;){
  350. /*If we somehow can't find the shape, then just do an unpadded fDCT.
  351. It won't be efficient, but it should still be correct.*/
  352. if(++cext>=OC_EXTENSION_INFO+OC_NSHAPES){
  353. oc_enc_fdct8x8_c(_y,_x);
  354. return;
  355. }
  356. }
  357. if(rmask==0xFF)rext=NULL;
  358. else for(rext=OC_EXTENSION_INFO;rext->mask!=rmask;){
  359. /*If we somehow can't find the shape, then just do an unpadded fDCT.
  360. It won't be efficient, but it should still be correct.*/
  361. if(++rext>=OC_EXTENSION_INFO+OC_NSHAPES){
  362. oc_enc_fdct8x8_c(_y,_x);
  363. return;
  364. }
  365. }
  366. /*Add two extra bits of working precision to improve accuracy; any more and
  367. we could overflow.*/
  368. for(ci=0;ci<64;ci++)w[ci]=_x[ci]<<2;
  369. /*These biases correct for some systematic error that remains in the full
  370. fDCT->iDCT round trip.
  371. We can safely add them before padding, since if these pixel values are
  372. overwritten, we didn't care what they were anyway (and the unbiased values
  373. will usually yield smaller DCT coefficient magnitudes).*/
  374. w[0]+=(w[0]!=0)+1;
  375. w[1]++;
  376. w[8]--;
  377. /*Transform the columns.
  378. We can ignore zero columns without a problem.*/
  379. in=w;
  380. out=_y;
  381. if(cext==NULL)for(ci=0;ci<8;ci++)oc_fdct8(out+(ci<<3),in+ci);
  382. else for(ci=0;ci<8;ci++)if(rmask&(1<<ci))oc_fdct8_ext(out+(ci<<3),in+ci,cext);
  383. /*Transform the rows.
  384. We transform even rows that are supposedly zero, because rounding errors
  385. may make them slightly non-zero, and this will give a more precise
  386. reconstruction with very small quantizers.*/
  387. in=_y;
  388. out=w;
  389. if(rext==NULL)for(ri=0;ri<8;ri++)oc_fdct8(out+(ri<<3),in+ri);
  390. else for(ri=0;ri<8;ri++)oc_fdct8_ext(out+(ri<<3),in+ri,rext);
  391. /*Round the result back to the external working precision (which is still
  392. scaled by four relative to the orthogonal result).
  393. TODO: We should just update the external working precision.*/
  394. for(ci=0;ci<64;ci++)_y[ci]=w[ci]+2>>2;
  395. }
  396. #endif