You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1385 lines
52KB

  1. /*
  2. * DSP utils
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * gmc & q-pel & 32/64 bit based MC by Michael Niedermayer <michaelni@gmx.at>
  20. */
  21. #include "avcodec.h"
  22. #include "dsputil.h"
  23. #include "simple_idct.h"
  24. void (*ff_idct)(DCTELEM *block);
  25. void (*ff_idct_put)(UINT8 *dest, int line_size, DCTELEM *block);
  26. void (*ff_idct_add)(UINT8 *dest, int line_size, DCTELEM *block);
  27. void (*av_fdct)(DCTELEM *block);
  28. void (*get_pixels)(DCTELEM *block, const UINT8 *pixels, int line_size);
  29. void (*diff_pixels)(DCTELEM *block, const UINT8 *s1, const UINT8 *s2, int stride);
  30. void (*put_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
  31. void (*add_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
  32. void (*gmc1)(UINT8 *dst, UINT8 *src, int srcStride, int h, int x16, int y16, int rounder);
  33. void (*clear_blocks)(DCTELEM *blocks);
  34. op_pixels_abs_func pix_abs16x16;
  35. op_pixels_abs_func pix_abs16x16_x2;
  36. op_pixels_abs_func pix_abs16x16_y2;
  37. op_pixels_abs_func pix_abs16x16_xy2;
  38. op_pixels_abs_func pix_abs8x8;
  39. op_pixels_abs_func pix_abs8x8_x2;
  40. op_pixels_abs_func pix_abs8x8_y2;
  41. op_pixels_abs_func pix_abs8x8_xy2;
  42. UINT8 cropTbl[256 + 2 * MAX_NEG_CROP];
  43. UINT32 squareTbl[512];
  44. extern INT16 default_intra_matrix[64];
  45. extern INT16 default_non_intra_matrix[64];
  46. extern INT16 ff_mpeg4_default_intra_matrix[64];
  47. extern INT16 ff_mpeg4_default_non_intra_matrix[64];
  48. UINT8 zigzag_direct[64] = {
  49. 0, 1, 8, 16, 9, 2, 3, 10,
  50. 17, 24, 32, 25, 18, 11, 4, 5,
  51. 12, 19, 26, 33, 40, 48, 41, 34,
  52. 27, 20, 13, 6, 7, 14, 21, 28,
  53. 35, 42, 49, 56, 57, 50, 43, 36,
  54. 29, 22, 15, 23, 30, 37, 44, 51,
  55. 58, 59, 52, 45, 38, 31, 39, 46,
  56. 53, 60, 61, 54, 47, 55, 62, 63
  57. };
  58. /* not permutated inverse zigzag_direct + 1 for MMX quantizer */
  59. UINT16 __align8 inv_zigzag_direct16[64];
  60. /* not permutated zigzag_direct for MMX quantizer */
  61. UINT8 zigzag_direct_noperm[64];
  62. UINT8 ff_alternate_horizontal_scan[64] = {
  63. 0, 1, 2, 3, 8, 9, 16, 17,
  64. 10, 11, 4, 5, 6, 7, 15, 14,
  65. 13, 12, 19, 18, 24, 25, 32, 33,
  66. 26, 27, 20, 21, 22, 23, 28, 29,
  67. 30, 31, 34, 35, 40, 41, 48, 49,
  68. 42, 43, 36, 37, 38, 39, 44, 45,
  69. 46, 47, 50, 51, 56, 57, 58, 59,
  70. 52, 53, 54, 55, 60, 61, 62, 63,
  71. };
  72. UINT8 ff_alternate_vertical_scan[64] = {
  73. 0, 8, 16, 24, 1, 9, 2, 10,
  74. 17, 25, 32, 40, 48, 56, 57, 49,
  75. 41, 33, 26, 18, 3, 11, 4, 12,
  76. 19, 27, 34, 42, 50, 58, 35, 43,
  77. 51, 59, 20, 28, 5, 13, 6, 14,
  78. 21, 29, 36, 44, 52, 60, 37, 45,
  79. 53, 61, 22, 30, 7, 15, 23, 31,
  80. 38, 46, 54, 62, 39, 47, 55, 63,
  81. };
  82. #ifdef SIMPLE_IDCT
  83. /* Input permutation for the simple_idct_mmx */
  84. static UINT8 simple_mmx_permutation[64]={
  85. 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
  86. 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
  87. 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
  88. 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
  89. 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
  90. 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
  91. 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
  92. 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
  93. };
  94. #endif
  95. /* a*inverse[b]>>32 == a/b for all 0<=a<=65536 && 2<=b<=255 */
  96. UINT32 inverse[256]={
  97. 0, 4294967295U,2147483648U,1431655766, 1073741824, 858993460, 715827883, 613566757,
  98. 536870912, 477218589, 429496730, 390451573, 357913942, 330382100, 306783379, 286331154,
  99. 268435456, 252645136, 238609295, 226050911, 214748365, 204522253, 195225787, 186737709,
  100. 178956971, 171798692, 165191050, 159072863, 153391690, 148102321, 143165577, 138547333,
  101. 134217728, 130150525, 126322568, 122713352, 119304648, 116080198, 113025456, 110127367,
  102. 107374183, 104755300, 102261127, 99882961, 97612894, 95443718, 93368855, 91382283,
  103. 89478486, 87652394, 85899346, 84215046, 82595525, 81037119, 79536432, 78090315,
  104. 76695845, 75350304, 74051161, 72796056, 71582789, 70409300, 69273667, 68174085,
  105. 67108864, 66076420, 65075263, 64103990, 63161284, 62245903, 61356676, 60492498,
  106. 59652324, 58835169, 58040099, 57266231, 56512728, 55778797, 55063684, 54366675,
  107. 53687092, 53024288, 52377650, 51746594, 51130564, 50529028, 49941481, 49367441,
  108. 48806447, 48258060, 47721859, 47197443, 46684428, 46182445, 45691142, 45210183,
  109. 44739243, 44278014, 43826197, 43383509, 42949673, 42524429, 42107523, 41698712,
  110. 41297763, 40904451, 40518560, 40139882, 39768216, 39403370, 39045158, 38693400,
  111. 38347923, 38008561, 37675152, 37347542, 37025581, 36709123, 36398028, 36092163,
  112. 35791395, 35495598, 35204650, 34918434, 34636834, 34359739, 34087043, 33818641,
  113. 33554432, 33294321, 33038210, 32786010, 32537632, 32292988, 32051995, 31814573,
  114. 31580642, 31350127, 31122952, 30899046, 30678338, 30460761, 30246249, 30034737,
  115. 29826162, 29620465, 29417585, 29217465, 29020050, 28825284, 28633116, 28443493,
  116. 28256364, 28071682, 27889399, 27709467, 27531842, 27356480, 27183338, 27012373,
  117. 26843546, 26676816, 26512144, 26349493, 26188825, 26030105, 25873297, 25718368,
  118. 25565282, 25414008, 25264514, 25116768, 24970741, 24826401, 24683721, 24542671,
  119. 24403224, 24265352, 24129030, 23994231, 23860930, 23729102, 23598722, 23469767,
  120. 23342214, 23216040, 23091223, 22967740, 22845571, 22724695, 22605092, 22486740,
  121. 22369622, 22253717, 22139007, 22025474, 21913099, 21801865, 21691755, 21582751,
  122. 21474837, 21367997, 21262215, 21157475, 21053762, 20951060, 20849356, 20748635,
  123. 20648882, 20550083, 20452226, 20355296, 20259280, 20164166, 20069941, 19976593,
  124. 19884108, 19792477, 19701685, 19611723, 19522579, 19434242, 19346700, 19259944,
  125. 19173962, 19088744, 19004281, 18920561, 18837576, 18755316, 18673771, 18592933,
  126. 18512791, 18433337, 18354562, 18276457, 18199014, 18122225, 18046082, 17970575,
  127. 17895698, 17821442, 17747799, 17674763, 17602325, 17530479, 17459217, 17388532,
  128. 17318417, 17248865, 17179870, 17111424, 17043522, 16976156, 16909321, 16843010,
  129. };
  130. /* used to skip zeros at the end */
  131. UINT8 zigzag_end[64];
  132. UINT8 permutation[64];
  133. //UINT8 invPermutation[64];
  134. static void build_zigzag_end(void)
  135. {
  136. int lastIndex;
  137. int lastIndexAfterPerm=0;
  138. for(lastIndex=0; lastIndex<64; lastIndex++)
  139. {
  140. if(zigzag_direct[lastIndex] > lastIndexAfterPerm)
  141. lastIndexAfterPerm= zigzag_direct[lastIndex];
  142. zigzag_end[lastIndex]= lastIndexAfterPerm + 1;
  143. }
  144. }
  145. void get_pixels_c(DCTELEM *block, const UINT8 *pixels, int line_size)
  146. {
  147. DCTELEM *p;
  148. const UINT8 *pix;
  149. int i;
  150. /* read the pixels */
  151. p = block;
  152. pix = pixels;
  153. for(i=0;i<8;i++) {
  154. p[0] = pix[0];
  155. p[1] = pix[1];
  156. p[2] = pix[2];
  157. p[3] = pix[3];
  158. p[4] = pix[4];
  159. p[5] = pix[5];
  160. p[6] = pix[6];
  161. p[7] = pix[7];
  162. pix += line_size;
  163. p += 8;
  164. }
  165. }
  166. void diff_pixels_c(DCTELEM *block, const UINT8 *s1, const UINT8 *s2, int stride){
  167. DCTELEM *p;
  168. int i;
  169. /* read the pixels */
  170. p = block;
  171. for(i=0;i<8;i++) {
  172. p[0] = s1[0] - s2[0];
  173. p[1] = s1[1] - s2[1];
  174. p[2] = s1[2] - s2[2];
  175. p[3] = s1[3] - s2[3];
  176. p[4] = s1[4] - s2[4];
  177. p[5] = s1[5] - s2[5];
  178. p[6] = s1[6] - s2[6];
  179. p[7] = s1[7] - s2[7];
  180. s1 += stride;
  181. s2 += stride;
  182. p += 8;
  183. }
  184. }
  185. void put_pixels_clamped_c(const DCTELEM *block, UINT8 *pixels, int line_size)
  186. {
  187. const DCTELEM *p;
  188. UINT8 *pix;
  189. int i;
  190. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  191. /* read the pixels */
  192. p = block;
  193. pix = pixels;
  194. for(i=0;i<8;i++) {
  195. pix[0] = cm[p[0]];
  196. pix[1] = cm[p[1]];
  197. pix[2] = cm[p[2]];
  198. pix[3] = cm[p[3]];
  199. pix[4] = cm[p[4]];
  200. pix[5] = cm[p[5]];
  201. pix[6] = cm[p[6]];
  202. pix[7] = cm[p[7]];
  203. pix += line_size;
  204. p += 8;
  205. }
  206. }
  207. void add_pixels_clamped_c(const DCTELEM *block, UINT8 *pixels, int line_size)
  208. {
  209. const DCTELEM *p;
  210. UINT8 *pix;
  211. int i;
  212. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  213. /* read the pixels */
  214. p = block;
  215. pix = pixels;
  216. for(i=0;i<8;i++) {
  217. pix[0] = cm[pix[0] + p[0]];
  218. pix[1] = cm[pix[1] + p[1]];
  219. pix[2] = cm[pix[2] + p[2]];
  220. pix[3] = cm[pix[3] + p[3]];
  221. pix[4] = cm[pix[4] + p[4]];
  222. pix[5] = cm[pix[5] + p[5]];
  223. pix[6] = cm[pix[6] + p[6]];
  224. pix[7] = cm[pix[7] + p[7]];
  225. pix += line_size;
  226. p += 8;
  227. }
  228. }
  229. #if 0
  230. #define PIXOP2(OPNAME, OP) \
  231. void OPNAME ## _pixels(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  232. {\
  233. int i;\
  234. for(i=0; i<h; i++){\
  235. OP(*((uint64_t*)block), LD64(pixels));\
  236. pixels+=line_size;\
  237. block +=line_size;\
  238. }\
  239. }\
  240. \
  241. void OPNAME ## _no_rnd_pixels_x2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  242. {\
  243. int i;\
  244. for(i=0; i<h; i++){\
  245. const uint64_t a= LD64(pixels );\
  246. const uint64_t b= LD64(pixels+1);\
  247. OP(*((uint64_t*)block), (a&b) + (((a^b)&0xFEFEFEFEFEFEFEFEULL)>>1));\
  248. pixels+=line_size;\
  249. block +=line_size;\
  250. }\
  251. }\
  252. \
  253. void OPNAME ## _pixels_x2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  254. {\
  255. int i;\
  256. for(i=0; i<h; i++){\
  257. const uint64_t a= LD64(pixels );\
  258. const uint64_t b= LD64(pixels+1);\
  259. OP(*((uint64_t*)block), (a|b) - (((a^b)&0xFEFEFEFEFEFEFEFEULL)>>1));\
  260. pixels+=line_size;\
  261. block +=line_size;\
  262. }\
  263. }\
  264. \
  265. void OPNAME ## _no_rnd_pixels_y2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  266. {\
  267. int i;\
  268. for(i=0; i<h; i++){\
  269. const uint64_t a= LD64(pixels );\
  270. const uint64_t b= LD64(pixels+line_size);\
  271. OP(*((uint64_t*)block), (a&b) + (((a^b)&0xFEFEFEFEFEFEFEFEULL)>>1));\
  272. pixels+=line_size;\
  273. block +=line_size;\
  274. }\
  275. }\
  276. \
  277. void OPNAME ## _pixels_y2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  278. {\
  279. int i;\
  280. for(i=0; i<h; i++){\
  281. const uint64_t a= LD64(pixels );\
  282. const uint64_t b= LD64(pixels+line_size);\
  283. OP(*((uint64_t*)block), (a|b) - (((a^b)&0xFEFEFEFEFEFEFEFEULL)>>1));\
  284. pixels+=line_size;\
  285. block +=line_size;\
  286. }\
  287. }\
  288. \
  289. void OPNAME ## _pixels_xy2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  290. {\
  291. int i;\
  292. const uint64_t a= LD64(pixels );\
  293. const uint64_t b= LD64(pixels+1);\
  294. uint64_t l0= (a&0x0303030303030303ULL)\
  295. + (b&0x0303030303030303ULL)\
  296. + 0x0202020202020202ULL;\
  297. uint64_t h0= ((a&0xFCFCFCFCFCFCFCFCULL)>>2)\
  298. + ((b&0xFCFCFCFCFCFCFCFCULL)>>2);\
  299. uint64_t l1,h1;\
  300. \
  301. pixels+=line_size;\
  302. for(i=0; i<h; i+=2){\
  303. uint64_t a= LD64(pixels );\
  304. uint64_t b= LD64(pixels+1);\
  305. l1= (a&0x0303030303030303ULL)\
  306. + (b&0x0303030303030303ULL);\
  307. h1= ((a&0xFCFCFCFCFCFCFCFCULL)>>2)\
  308. + ((b&0xFCFCFCFCFCFCFCFCULL)>>2);\
  309. OP(*((uint64_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0F0F0F0F0FULL));\
  310. pixels+=line_size;\
  311. block +=line_size;\
  312. a= LD64(pixels );\
  313. b= LD64(pixels+1);\
  314. l0= (a&0x0303030303030303ULL)\
  315. + (b&0x0303030303030303ULL)\
  316. + 0x0202020202020202ULL;\
  317. h0= ((a&0xFCFCFCFCFCFCFCFCULL)>>2)\
  318. + ((b&0xFCFCFCFCFCFCFCFCULL)>>2);\
  319. OP(*((uint64_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0F0F0F0F0FULL));\
  320. pixels+=line_size;\
  321. block +=line_size;\
  322. }\
  323. }\
  324. \
  325. void OPNAME ## _no_rnd_pixels_xy2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  326. {\
  327. int i;\
  328. const uint64_t a= LD64(pixels );\
  329. const uint64_t b= LD64(pixels+1);\
  330. uint64_t l0= (a&0x0303030303030303ULL)\
  331. + (b&0x0303030303030303ULL)\
  332. + 0x0101010101010101ULL;\
  333. uint64_t h0= ((a&0xFCFCFCFCFCFCFCFCULL)>>2)\
  334. + ((b&0xFCFCFCFCFCFCFCFCULL)>>2);\
  335. uint64_t l1,h1;\
  336. \
  337. pixels+=line_size;\
  338. for(i=0; i<h; i+=2){\
  339. uint64_t a= LD64(pixels );\
  340. uint64_t b= LD64(pixels+1);\
  341. l1= (a&0x0303030303030303ULL)\
  342. + (b&0x0303030303030303ULL);\
  343. h1= ((a&0xFCFCFCFCFCFCFCFCULL)>>2)\
  344. + ((b&0xFCFCFCFCFCFCFCFCULL)>>2);\
  345. OP(*((uint64_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0F0F0F0F0FULL));\
  346. pixels+=line_size;\
  347. block +=line_size;\
  348. a= LD64(pixels );\
  349. b= LD64(pixels+1);\
  350. l0= (a&0x0303030303030303ULL)\
  351. + (b&0x0303030303030303ULL)\
  352. + 0x0101010101010101ULL;\
  353. h0= ((a&0xFCFCFCFCFCFCFCFCULL)>>2)\
  354. + ((b&0xFCFCFCFCFCFCFCFCULL)>>2);\
  355. OP(*((uint64_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0F0F0F0F0FULL));\
  356. pixels+=line_size;\
  357. block +=line_size;\
  358. }\
  359. }\
  360. \
  361. void (*OPNAME ## _pixels_tab[4])(uint8_t *block, const uint8_t *pixels, int line_size, int h) = {\
  362. OPNAME ## _pixels,\
  363. OPNAME ## _pixels_x2,\
  364. OPNAME ## _pixels_y2,\
  365. OPNAME ## _pixels_xy2,\
  366. };\
  367. \
  368. void (*OPNAME ## _no_rnd_pixels_tab[4])(uint8_t *block, const uint8_t *pixels, int line_size, int h) = {\
  369. OPNAME ## _pixels,\
  370. OPNAME ## _no_rnd_pixels_x2,\
  371. OPNAME ## _no_rnd_pixels_y2,\
  372. OPNAME ## _no_rnd_pixels_xy2,\
  373. };
  374. #define op_avg(a, b) a = ( ((a)|(b)) - ((((a)^(b))&0xFEFEFEFEFEFEFEFEULL)>>1) )
  375. #else // 64 bit variant
  376. #define PIXOP2(OPNAME, OP) \
  377. void OPNAME ## _pixels(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  378. {\
  379. int i;\
  380. for(i=0; i<h; i++){\
  381. OP(*((uint32_t*)(block )), LD32(pixels ));\
  382. OP(*((uint32_t*)(block+4)), LD32(pixels+4));\
  383. pixels+=line_size;\
  384. block +=line_size;\
  385. }\
  386. }\
  387. \
  388. void OPNAME ## _no_rnd_pixels_x2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  389. {\
  390. int i;\
  391. for(i=0; i<h; i++){\
  392. int j;\
  393. for(j=0; j<2; j++){\
  394. const uint32_t a= LD32(pixels );\
  395. const uint32_t b= LD32(pixels+1);\
  396. OP(*((uint32_t*)block), (a&b) + (((a^b)&0xFEFEFEFEUL)>>1));\
  397. pixels+=4;\
  398. block +=4;\
  399. }\
  400. pixels+=line_size-8;\
  401. block +=line_size-8;\
  402. }\
  403. }\
  404. \
  405. void OPNAME ## _pixels_x2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  406. {\
  407. int i;\
  408. for(i=0; i<h; i++){\
  409. int j;\
  410. for(j=0; j<2; j++){\
  411. const uint32_t a= LD32(pixels );\
  412. const uint32_t b= LD32(pixels+1);\
  413. OP(*((uint32_t*)block), (a|b) - (((a^b)&0xFEFEFEFEUL)>>1));\
  414. pixels+=4;\
  415. block +=4;\
  416. }\
  417. pixels+=line_size-8;\
  418. block +=line_size-8;\
  419. }\
  420. }\
  421. \
  422. void OPNAME ## _no_rnd_pixels_y2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  423. {\
  424. int i;\
  425. for(i=0; i<h; i++){\
  426. int j;\
  427. for(j=0; j<2; j++){\
  428. const uint32_t a= LD32(pixels );\
  429. const uint32_t b= LD32(pixels+line_size);\
  430. OP(*((uint32_t*)block), (a&b) + (((a^b)&0xFEFEFEFEUL)>>1));\
  431. pixels+=4;\
  432. block +=4;\
  433. }\
  434. pixels+=line_size-8;\
  435. block +=line_size-8;\
  436. }\
  437. }\
  438. \
  439. void OPNAME ## _pixels_y2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  440. {\
  441. int i;\
  442. for(i=0; i<h; i++){\
  443. int j;\
  444. for(j=0; j<2; j++){\
  445. const uint32_t a= LD32(pixels );\
  446. const uint32_t b= LD32(pixels+line_size);\
  447. OP(*((uint32_t*)block), (a|b) - (((a^b)&0xFEFEFEFEUL)>>1));\
  448. pixels+=4;\
  449. block +=4;\
  450. }\
  451. pixels+=line_size-8;\
  452. block +=line_size-8;\
  453. }\
  454. }\
  455. \
  456. void OPNAME ## _pixels_xy2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  457. {\
  458. int j;\
  459. for(j=0; j<2; j++){\
  460. int i;\
  461. const uint32_t a= LD32(pixels );\
  462. const uint32_t b= LD32(pixels+1);\
  463. uint32_t l0= (a&0x03030303UL)\
  464. + (b&0x03030303UL)\
  465. + 0x02020202UL;\
  466. uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
  467. + ((b&0xFCFCFCFCUL)>>2);\
  468. uint32_t l1,h1;\
  469. \
  470. pixels+=line_size;\
  471. for(i=0; i<h; i+=2){\
  472. uint32_t a= LD32(pixels );\
  473. uint32_t b= LD32(pixels+1);\
  474. l1= (a&0x03030303UL)\
  475. + (b&0x03030303UL);\
  476. h1= ((a&0xFCFCFCFCUL)>>2)\
  477. + ((b&0xFCFCFCFCUL)>>2);\
  478. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  479. pixels+=line_size;\
  480. block +=line_size;\
  481. a= LD32(pixels );\
  482. b= LD32(pixels+1);\
  483. l0= (a&0x03030303UL)\
  484. + (b&0x03030303UL)\
  485. + 0x02020202UL;\
  486. h0= ((a&0xFCFCFCFCUL)>>2)\
  487. + ((b&0xFCFCFCFCUL)>>2);\
  488. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  489. pixels+=line_size;\
  490. block +=line_size;\
  491. }\
  492. pixels+=4-line_size*(h+1);\
  493. block +=4-line_size*h;\
  494. }\
  495. }\
  496. \
  497. void OPNAME ## _no_rnd_pixels_xy2(uint8_t *block, const uint8_t *pixels, int line_size, int h)\
  498. {\
  499. int j;\
  500. for(j=0; j<2; j++){\
  501. int i;\
  502. const uint32_t a= LD32(pixels );\
  503. const uint32_t b= LD32(pixels+1);\
  504. uint32_t l0= (a&0x03030303UL)\
  505. + (b&0x03030303UL)\
  506. + 0x01010101UL;\
  507. uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
  508. + ((b&0xFCFCFCFCUL)>>2);\
  509. uint32_t l1,h1;\
  510. \
  511. pixels+=line_size;\
  512. for(i=0; i<h; i+=2){\
  513. uint32_t a= LD32(pixels );\
  514. uint32_t b= LD32(pixels+1);\
  515. l1= (a&0x03030303UL)\
  516. + (b&0x03030303UL);\
  517. h1= ((a&0xFCFCFCFCUL)>>2)\
  518. + ((b&0xFCFCFCFCUL)>>2);\
  519. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  520. pixels+=line_size;\
  521. block +=line_size;\
  522. a= LD32(pixels );\
  523. b= LD32(pixels+1);\
  524. l0= (a&0x03030303UL)\
  525. + (b&0x03030303UL)\
  526. + 0x01010101UL;\
  527. h0= ((a&0xFCFCFCFCUL)>>2)\
  528. + ((b&0xFCFCFCFCUL)>>2);\
  529. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  530. pixels+=line_size;\
  531. block +=line_size;\
  532. }\
  533. pixels+=4-line_size*(h+1);\
  534. block +=4-line_size*h;\
  535. }\
  536. }\
  537. \
  538. void (*OPNAME ## _pixels_tab[4])(uint8_t *block, const uint8_t *pixels, int line_size, int h) = {\
  539. OPNAME ## _pixels,\
  540. OPNAME ## _pixels_x2,\
  541. OPNAME ## _pixels_y2,\
  542. OPNAME ## _pixels_xy2,\
  543. };\
  544. \
  545. void (*OPNAME ## _no_rnd_pixels_tab[4])(uint8_t *block, const uint8_t *pixels, int line_size, int h) = {\
  546. OPNAME ## _pixels,\
  547. OPNAME ## _no_rnd_pixels_x2,\
  548. OPNAME ## _no_rnd_pixels_y2,\
  549. OPNAME ## _no_rnd_pixels_xy2,\
  550. };
  551. #define op_avg(a, b) a = ( ((a)|(b)) - ((((a)^(b))&0xFEFEFEFEUL)>>1) )
  552. #endif
  553. #define op_put(a, b) a = b
  554. PIXOP2(avg, op_avg)
  555. PIXOP2(put, op_put)
  556. #undef op_avg
  557. #undef op_put
  558. #if 0
  559. /* FIXME this stuff could be removed as its ot really used anymore */
  560. #define PIXOP(BTYPE, OPNAME, OP, INCR) \
  561. \
  562. static void OPNAME ## _pixels(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
  563. { \
  564. BTYPE *p; \
  565. const UINT8 *pix; \
  566. \
  567. p = block; \
  568. pix = pixels; \
  569. do { \
  570. OP(p[0], pix[0]); \
  571. OP(p[1], pix[1]); \
  572. OP(p[2], pix[2]); \
  573. OP(p[3], pix[3]); \
  574. OP(p[4], pix[4]); \
  575. OP(p[5], pix[5]); \
  576. OP(p[6], pix[6]); \
  577. OP(p[7], pix[7]); \
  578. pix += line_size; \
  579. p += INCR; \
  580. } while (--h);; \
  581. } \
  582. \
  583. static void OPNAME ## _pixels_x2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
  584. { \
  585. BTYPE *p; \
  586. const UINT8 *pix; \
  587. \
  588. p = block; \
  589. pix = pixels; \
  590. do { \
  591. OP(p[0], avg2(pix[0], pix[1])); \
  592. OP(p[1], avg2(pix[1], pix[2])); \
  593. OP(p[2], avg2(pix[2], pix[3])); \
  594. OP(p[3], avg2(pix[3], pix[4])); \
  595. OP(p[4], avg2(pix[4], pix[5])); \
  596. OP(p[5], avg2(pix[5], pix[6])); \
  597. OP(p[6], avg2(pix[6], pix[7])); \
  598. OP(p[7], avg2(pix[7], pix[8])); \
  599. pix += line_size; \
  600. p += INCR; \
  601. } while (--h); \
  602. } \
  603. \
  604. static void OPNAME ## _pixels_y2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
  605. { \
  606. BTYPE *p; \
  607. const UINT8 *pix; \
  608. const UINT8 *pix1; \
  609. \
  610. p = block; \
  611. pix = pixels; \
  612. pix1 = pixels + line_size; \
  613. do { \
  614. OP(p[0], avg2(pix[0], pix1[0])); \
  615. OP(p[1], avg2(pix[1], pix1[1])); \
  616. OP(p[2], avg2(pix[2], pix1[2])); \
  617. OP(p[3], avg2(pix[3], pix1[3])); \
  618. OP(p[4], avg2(pix[4], pix1[4])); \
  619. OP(p[5], avg2(pix[5], pix1[5])); \
  620. OP(p[6], avg2(pix[6], pix1[6])); \
  621. OP(p[7], avg2(pix[7], pix1[7])); \
  622. pix += line_size; \
  623. pix1 += line_size; \
  624. p += INCR; \
  625. } while(--h); \
  626. } \
  627. \
  628. static void OPNAME ## _pixels_xy2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
  629. { \
  630. BTYPE *p; \
  631. const UINT8 *pix; \
  632. const UINT8 *pix1; \
  633. \
  634. p = block; \
  635. pix = pixels; \
  636. pix1 = pixels + line_size; \
  637. do { \
  638. OP(p[0], avg4(pix[0], pix[1], pix1[0], pix1[1])); \
  639. OP(p[1], avg4(pix[1], pix[2], pix1[1], pix1[2])); \
  640. OP(p[2], avg4(pix[2], pix[3], pix1[2], pix1[3])); \
  641. OP(p[3], avg4(pix[3], pix[4], pix1[3], pix1[4])); \
  642. OP(p[4], avg4(pix[4], pix[5], pix1[4], pix1[5])); \
  643. OP(p[5], avg4(pix[5], pix[6], pix1[5], pix1[6])); \
  644. OP(p[6], avg4(pix[6], pix[7], pix1[6], pix1[7])); \
  645. OP(p[7], avg4(pix[7], pix[8], pix1[7], pix1[8])); \
  646. pix += line_size; \
  647. pix1 += line_size; \
  648. p += INCR; \
  649. } while(--h); \
  650. } \
  651. \
  652. void (*OPNAME ## _pixels_tab[4])(BTYPE *block, const UINT8 *pixels, int line_size, int h) = { \
  653. OPNAME ## _pixels, \
  654. OPNAME ## _pixels_x2, \
  655. OPNAME ## _pixels_y2, \
  656. OPNAME ## _pixels_xy2, \
  657. };
  658. /* rounding primitives */
  659. #define avg2(a,b) ((a+b+1)>>1)
  660. #define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
  661. #define op_avg(a, b) a = avg2(a, b)
  662. #define op_sub(a, b) a -= b
  663. PIXOP(DCTELEM, sub, op_sub, 8)
  664. /* not rounding primitives */
  665. #undef avg2
  666. #undef avg4
  667. #define avg2(a,b) ((a+b)>>1)
  668. #define avg4(a,b,c,d) ((a+b+c+d+1)>>2)
  669. /* motion estimation */
  670. #undef avg2
  671. #undef avg4
  672. #endif
  673. #define avg2(a,b) ((a+b+1)>>1)
  674. #define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
  675. static void gmc1_c(UINT8 *dst, UINT8 *src, int srcStride, int h, int x16, int y16, int rounder)
  676. {
  677. const int A=(16-x16)*(16-y16);
  678. const int B=( x16)*(16-y16);
  679. const int C=(16-x16)*( y16);
  680. const int D=( x16)*( y16);
  681. int i;
  682. rounder= 128 - rounder;
  683. for(i=0; i<h; i++)
  684. {
  685. dst[0]= (A*src[0] + B*src[1] + C*src[srcStride+0] + D*src[srcStride+1] + rounder)>>8;
  686. dst[1]= (A*src[1] + B*src[2] + C*src[srcStride+1] + D*src[srcStride+2] + rounder)>>8;
  687. dst[2]= (A*src[2] + B*src[3] + C*src[srcStride+2] + D*src[srcStride+3] + rounder)>>8;
  688. dst[3]= (A*src[3] + B*src[4] + C*src[srcStride+3] + D*src[srcStride+4] + rounder)>>8;
  689. dst[4]= (A*src[4] + B*src[5] + C*src[srcStride+4] + D*src[srcStride+5] + rounder)>>8;
  690. dst[5]= (A*src[5] + B*src[6] + C*src[srcStride+5] + D*src[srcStride+6] + rounder)>>8;
  691. dst[6]= (A*src[6] + B*src[7] + C*src[srcStride+6] + D*src[srcStride+7] + rounder)>>8;
  692. dst[7]= (A*src[7] + B*src[8] + C*src[srcStride+7] + D*src[srcStride+8] + rounder)>>8;
  693. dst+= srcStride;
  694. src+= srcStride;
  695. }
  696. }
  697. static void qpel_h_lowpass(UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int h, int r)
  698. {
  699. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  700. int i;
  701. for(i=0; i<h; i++)
  702. {
  703. dst[0]= cm[(((src[0]+src[1])*20 - (src[0]+src[2])*6 + (src[1]+src[3])*3 - (src[2]+src[4]) + r)>>5)];
  704. dst[1]= cm[(((src[1]+src[2])*20 - (src[0]+src[3])*6 + (src[0]+src[4])*3 - (src[1]+src[5]) + r)>>5)];
  705. dst[2]= cm[(((src[2]+src[3])*20 - (src[1]+src[4])*6 + (src[0]+src[5])*3 - (src[0]+src[6]) + r)>>5)];
  706. dst[3]= cm[(((src[3]+src[4])*20 - (src[2]+src[5])*6 + (src[1]+src[6])*3 - (src[0]+src[7]) + r)>>5)];
  707. dst[4]= cm[(((src[4]+src[5])*20 - (src[3]+src[6])*6 + (src[2]+src[7])*3 - (src[1]+src[8]) + r)>>5)];
  708. dst[5]= cm[(((src[5]+src[6])*20 - (src[4]+src[7])*6 + (src[3]+src[8])*3 - (src[2]+src[8]) + r)>>5)];
  709. dst[6]= cm[(((src[6]+src[7])*20 - (src[5]+src[8])*6 + (src[4]+src[8])*3 - (src[3]+src[7]) + r)>>5)];
  710. dst[7]= cm[(((src[7]+src[8])*20 - (src[6]+src[8])*6 + (src[5]+src[7])*3 - (src[4]+src[6]) + r)>>5)];
  711. dst+=dstStride;
  712. src+=srcStride;
  713. }
  714. }
  715. static void qpel_v_lowpass(UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int w, int r)
  716. {
  717. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  718. int i;
  719. for(i=0; i<w; i++)
  720. {
  721. const int src0= src[0*srcStride];
  722. const int src1= src[1*srcStride];
  723. const int src2= src[2*srcStride];
  724. const int src3= src[3*srcStride];
  725. const int src4= src[4*srcStride];
  726. const int src5= src[5*srcStride];
  727. const int src6= src[6*srcStride];
  728. const int src7= src[7*srcStride];
  729. const int src8= src[8*srcStride];
  730. dst[0*dstStride]= cm[(((src0+src1)*20 - (src0+src2)*6 + (src1+src3)*3 - (src2+src4) + r)>>5)];
  731. dst[1*dstStride]= cm[(((src1+src2)*20 - (src0+src3)*6 + (src0+src4)*3 - (src1+src5) + r)>>5)];
  732. dst[2*dstStride]= cm[(((src2+src3)*20 - (src1+src4)*6 + (src0+src5)*3 - (src0+src6) + r)>>5)];
  733. dst[3*dstStride]= cm[(((src3+src4)*20 - (src2+src5)*6 + (src1+src6)*3 - (src0+src7) + r)>>5)];
  734. dst[4*dstStride]= cm[(((src4+src5)*20 - (src3+src6)*6 + (src2+src7)*3 - (src1+src8) + r)>>5)];
  735. dst[5*dstStride]= cm[(((src5+src6)*20 - (src4+src7)*6 + (src3+src8)*3 - (src2+src8) + r)>>5)];
  736. dst[6*dstStride]= cm[(((src6+src7)*20 - (src5+src8)*6 + (src4+src8)*3 - (src3+src7) + r)>>5)];
  737. dst[7*dstStride]= cm[(((src7+src8)*20 - (src6+src8)*6 + (src5+src7)*3 - (src4+src6) + r)>>5)];
  738. dst++;
  739. src++;
  740. }
  741. }
  742. static inline void put_block(UINT8 *dst, UINT8 *src, int dstStride, int srcStride)
  743. {
  744. int i;
  745. for(i=0; i<8; i++)
  746. {
  747. dst[0]= src[0];
  748. dst[1]= src[1];
  749. dst[2]= src[2];
  750. dst[3]= src[3];
  751. dst[4]= src[4];
  752. dst[5]= src[5];
  753. dst[6]= src[6];
  754. dst[7]= src[7];
  755. dst+=dstStride;
  756. src+=srcStride;
  757. }
  758. }
  759. static inline void avg2_block(UINT8 *dst, UINT8 *src1, UINT8 *src2, int dstStride, int srcStride, int r)
  760. {
  761. int i;
  762. for(i=0; i<8; i++)
  763. {
  764. dst[0]= (src1[0] + src2[0] + r)>>1;
  765. dst[1]= (src1[1] + src2[1] + r)>>1;
  766. dst[2]= (src1[2] + src2[2] + r)>>1;
  767. dst[3]= (src1[3] + src2[3] + r)>>1;
  768. dst[4]= (src1[4] + src2[4] + r)>>1;
  769. dst[5]= (src1[5] + src2[5] + r)>>1;
  770. dst[6]= (src1[6] + src2[6] + r)>>1;
  771. dst[7]= (src1[7] + src2[7] + r)>>1;
  772. dst+=dstStride;
  773. src1+=srcStride;
  774. src2+=8;
  775. }
  776. }
  777. static inline void avg4_block(UINT8 *dst, UINT8 *src1, UINT8 *src2, UINT8 *src3, UINT8 *src4, int dstStride, int srcStride, int r)
  778. {
  779. int i;
  780. for(i=0; i<8; i++)
  781. {
  782. dst[0]= (src1[0] + src2[0] + src3[0] + src4[0] + r)>>2;
  783. dst[1]= (src1[1] + src2[1] + src3[1] + src4[1] + r)>>2;
  784. dst[2]= (src1[2] + src2[2] + src3[2] + src4[2] + r)>>2;
  785. dst[3]= (src1[3] + src2[3] + src3[3] + src4[3] + r)>>2;
  786. dst[4]= (src1[4] + src2[4] + src3[4] + src4[4] + r)>>2;
  787. dst[5]= (src1[5] + src2[5] + src3[5] + src4[5] + r)>>2;
  788. dst[6]= (src1[6] + src2[6] + src3[6] + src4[6] + r)>>2;
  789. dst[7]= (src1[7] + src2[7] + src3[7] + src4[7] + r)>>2;
  790. dst+=dstStride;
  791. src1+=srcStride;
  792. src2+=8;
  793. src3+=8;
  794. src4+=8;
  795. }
  796. }
  797. #define QPEL_MC(r, name) \
  798. static void qpel_mc00_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  799. {\
  800. put_block(dst, src, dstStride, srcStride);\
  801. }\
  802. \
  803. static void qpel_mc10_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  804. {\
  805. UINT8 half[64];\
  806. qpel_h_lowpass(half, src, 8, srcStride, 8, 16-r);\
  807. avg2_block(dst, src, half, dstStride, srcStride, 1-r);\
  808. }\
  809. \
  810. static void qpel_mc20_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  811. {\
  812. qpel_h_lowpass(dst, src, dstStride, srcStride, 8, 16-r);\
  813. }\
  814. \
  815. static void qpel_mc30_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  816. {\
  817. UINT8 half[64];\
  818. qpel_h_lowpass(half, src, 8, srcStride, 8, 16-r);\
  819. avg2_block(dst, src+1, half, dstStride, srcStride, 1-r);\
  820. }\
  821. \
  822. static void qpel_mc01_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  823. {\
  824. UINT8 half[64];\
  825. qpel_v_lowpass(half, src, 8, srcStride, 8, 16-r);\
  826. avg2_block(dst, src, half, dstStride, srcStride, 1-r);\
  827. }\
  828. \
  829. static void qpel_mc02_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  830. {\
  831. qpel_v_lowpass(dst, src, dstStride, srcStride, 8, 16-r);\
  832. }\
  833. \
  834. static void qpel_mc03_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  835. {\
  836. UINT8 half[64];\
  837. qpel_v_lowpass(half, src, 8, srcStride, 8, 16-r);\
  838. avg2_block(dst, src+srcStride, half, dstStride, srcStride, 1-r);\
  839. }\
  840. static void qpel_mc11_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  841. {\
  842. UINT8 halfH[72];\
  843. UINT8 halfV[64];\
  844. UINT8 halfHV[64];\
  845. qpel_h_lowpass(halfH, src, 8, srcStride, 9, 16-r);\
  846. qpel_v_lowpass(halfV, src, 8, srcStride, 8, 16-r);\
  847. qpel_v_lowpass(halfHV, halfH, 8, 8, 8, 16-r);\
  848. avg4_block(dst, src, halfH, halfV, halfHV, dstStride, srcStride, 2-r);\
  849. }\
  850. static void qpel_mc31_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  851. {\
  852. UINT8 halfH[72];\
  853. UINT8 halfV[64];\
  854. UINT8 halfHV[64];\
  855. qpel_h_lowpass(halfH, src, 8, srcStride, 9, 16-r);\
  856. qpel_v_lowpass(halfV, src+1, 8, srcStride, 8, 16-r);\
  857. qpel_v_lowpass(halfHV, halfH, 8, 8, 8, 16-r);\
  858. avg4_block(dst, src+1, halfH, halfV, halfHV, dstStride, srcStride, 2-r);\
  859. }\
  860. static void qpel_mc13_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  861. {\
  862. UINT8 halfH[72];\
  863. UINT8 halfV[64];\
  864. UINT8 halfHV[64];\
  865. qpel_h_lowpass(halfH, src, 8, srcStride, 9, 16-r);\
  866. qpel_v_lowpass(halfV, src, 8, srcStride, 8, 16-r);\
  867. qpel_v_lowpass(halfHV, halfH, 8, 8, 8, 16-r);\
  868. avg4_block(dst, src+srcStride, halfH+8, halfV, halfHV, dstStride, srcStride, 2-r);\
  869. }\
  870. static void qpel_mc33_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  871. {\
  872. UINT8 halfH[72];\
  873. UINT8 halfV[64];\
  874. UINT8 halfHV[64];\
  875. qpel_h_lowpass(halfH, src, 8, srcStride, 9, 16-r);\
  876. qpel_v_lowpass(halfV, src+1, 8, srcStride, 8, 16-r);\
  877. qpel_v_lowpass(halfHV, halfH, 8, 8, 8, 16-r);\
  878. avg4_block(dst, src+srcStride+1, halfH+8, halfV, halfHV, dstStride, srcStride, 2-r);\
  879. }\
  880. static void qpel_mc21_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  881. {\
  882. UINT8 halfH[72];\
  883. UINT8 halfHV[64];\
  884. qpel_h_lowpass(halfH, src, 8, srcStride, 9, 16-r);\
  885. qpel_v_lowpass(halfHV, halfH, 8, 8, 8, 16-r);\
  886. avg2_block(dst, halfH, halfHV, dstStride, 8, 1-r);\
  887. }\
  888. static void qpel_mc23_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  889. {\
  890. UINT8 halfH[72];\
  891. UINT8 halfHV[64];\
  892. qpel_h_lowpass(halfH, src, 8, srcStride, 9, 16-r);\
  893. qpel_v_lowpass(halfHV, halfH, 8, 8, 8, 16-r);\
  894. avg2_block(dst, halfH+8, halfHV, dstStride, 8, 1-r);\
  895. }\
  896. static void qpel_mc12_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  897. {\
  898. UINT8 halfH[72];\
  899. UINT8 halfV[64];\
  900. UINT8 halfHV[64];\
  901. qpel_h_lowpass(halfH, src, 8, srcStride, 9, 16-r);\
  902. qpel_v_lowpass(halfV, src, 8, srcStride, 8, 16-r);\
  903. qpel_v_lowpass(halfHV, halfH, 8, 8, 8, 16-r);\
  904. avg2_block(dst, halfV, halfHV, dstStride, 8, 1-r);\
  905. }\
  906. static void qpel_mc32_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  907. {\
  908. UINT8 halfH[72];\
  909. UINT8 halfV[64];\
  910. UINT8 halfHV[64];\
  911. qpel_h_lowpass(halfH, src, 8, srcStride, 9, 16-r);\
  912. qpel_v_lowpass(halfV, src+1, 8, srcStride, 8, 16-r);\
  913. qpel_v_lowpass(halfHV, halfH, 8, 8, 8, 16-r);\
  914. avg2_block(dst, halfV, halfHV, dstStride, 8, 1-r);\
  915. }\
  916. static void qpel_mc22_c ## name (UINT8 *dst, UINT8 *src, int dstStride, int srcStride, int mx, int my)\
  917. {\
  918. UINT8 halfH[72];\
  919. qpel_h_lowpass(halfH, src, 8, srcStride, 9, 16-r);\
  920. qpel_v_lowpass(dst, halfH, dstStride, 8, 8, 16-r);\
  921. }\
  922. qpel_mc_func qpel_mc ## name ## _tab[16]={ \
  923. qpel_mc00_c ## name, \
  924. qpel_mc10_c ## name, \
  925. qpel_mc20_c ## name, \
  926. qpel_mc30_c ## name, \
  927. qpel_mc01_c ## name, \
  928. qpel_mc11_c ## name, \
  929. qpel_mc21_c ## name, \
  930. qpel_mc31_c ## name, \
  931. qpel_mc02_c ## name, \
  932. qpel_mc12_c ## name, \
  933. qpel_mc22_c ## name, \
  934. qpel_mc32_c ## name, \
  935. qpel_mc03_c ## name, \
  936. qpel_mc13_c ## name, \
  937. qpel_mc23_c ## name, \
  938. qpel_mc33_c ## name, \
  939. };
  940. QPEL_MC(0, _rnd)
  941. QPEL_MC(1, _no_rnd)
  942. int pix_abs16x16_c(UINT8 *pix1, UINT8 *pix2, int line_size)
  943. {
  944. int s, i;
  945. s = 0;
  946. for(i=0;i<16;i++) {
  947. s += abs(pix1[0] - pix2[0]);
  948. s += abs(pix1[1] - pix2[1]);
  949. s += abs(pix1[2] - pix2[2]);
  950. s += abs(pix1[3] - pix2[3]);
  951. s += abs(pix1[4] - pix2[4]);
  952. s += abs(pix1[5] - pix2[5]);
  953. s += abs(pix1[6] - pix2[6]);
  954. s += abs(pix1[7] - pix2[7]);
  955. s += abs(pix1[8] - pix2[8]);
  956. s += abs(pix1[9] - pix2[9]);
  957. s += abs(pix1[10] - pix2[10]);
  958. s += abs(pix1[11] - pix2[11]);
  959. s += abs(pix1[12] - pix2[12]);
  960. s += abs(pix1[13] - pix2[13]);
  961. s += abs(pix1[14] - pix2[14]);
  962. s += abs(pix1[15] - pix2[15]);
  963. pix1 += line_size;
  964. pix2 += line_size;
  965. }
  966. return s;
  967. }
  968. int pix_abs16x16_x2_c(UINT8 *pix1, UINT8 *pix2, int line_size)
  969. {
  970. int s, i;
  971. s = 0;
  972. for(i=0;i<16;i++) {
  973. s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
  974. s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
  975. s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
  976. s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
  977. s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
  978. s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
  979. s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
  980. s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
  981. s += abs(pix1[8] - avg2(pix2[8], pix2[9]));
  982. s += abs(pix1[9] - avg2(pix2[9], pix2[10]));
  983. s += abs(pix1[10] - avg2(pix2[10], pix2[11]));
  984. s += abs(pix1[11] - avg2(pix2[11], pix2[12]));
  985. s += abs(pix1[12] - avg2(pix2[12], pix2[13]));
  986. s += abs(pix1[13] - avg2(pix2[13], pix2[14]));
  987. s += abs(pix1[14] - avg2(pix2[14], pix2[15]));
  988. s += abs(pix1[15] - avg2(pix2[15], pix2[16]));
  989. pix1 += line_size;
  990. pix2 += line_size;
  991. }
  992. return s;
  993. }
  994. int pix_abs16x16_y2_c(UINT8 *pix1, UINT8 *pix2, int line_size)
  995. {
  996. int s, i;
  997. UINT8 *pix3 = pix2 + line_size;
  998. s = 0;
  999. for(i=0;i<16;i++) {
  1000. s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
  1001. s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
  1002. s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
  1003. s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
  1004. s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
  1005. s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
  1006. s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
  1007. s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
  1008. s += abs(pix1[8] - avg2(pix2[8], pix3[8]));
  1009. s += abs(pix1[9] - avg2(pix2[9], pix3[9]));
  1010. s += abs(pix1[10] - avg2(pix2[10], pix3[10]));
  1011. s += abs(pix1[11] - avg2(pix2[11], pix3[11]));
  1012. s += abs(pix1[12] - avg2(pix2[12], pix3[12]));
  1013. s += abs(pix1[13] - avg2(pix2[13], pix3[13]));
  1014. s += abs(pix1[14] - avg2(pix2[14], pix3[14]));
  1015. s += abs(pix1[15] - avg2(pix2[15], pix3[15]));
  1016. pix1 += line_size;
  1017. pix2 += line_size;
  1018. pix3 += line_size;
  1019. }
  1020. return s;
  1021. }
  1022. int pix_abs16x16_xy2_c(UINT8 *pix1, UINT8 *pix2, int line_size)
  1023. {
  1024. int s, i;
  1025. UINT8 *pix3 = pix2 + line_size;
  1026. s = 0;
  1027. for(i=0;i<16;i++) {
  1028. s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
  1029. s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
  1030. s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
  1031. s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
  1032. s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
  1033. s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
  1034. s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
  1035. s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
  1036. s += abs(pix1[8] - avg4(pix2[8], pix2[9], pix3[8], pix3[9]));
  1037. s += abs(pix1[9] - avg4(pix2[9], pix2[10], pix3[9], pix3[10]));
  1038. s += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
  1039. s += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
  1040. s += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
  1041. s += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
  1042. s += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
  1043. s += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
  1044. pix1 += line_size;
  1045. pix2 += line_size;
  1046. pix3 += line_size;
  1047. }
  1048. return s;
  1049. }
  1050. int pix_abs8x8_c(UINT8 *pix1, UINT8 *pix2, int line_size)
  1051. {
  1052. int s, i;
  1053. s = 0;
  1054. for(i=0;i<8;i++) {
  1055. s += abs(pix1[0] - pix2[0]);
  1056. s += abs(pix1[1] - pix2[1]);
  1057. s += abs(pix1[2] - pix2[2]);
  1058. s += abs(pix1[3] - pix2[3]);
  1059. s += abs(pix1[4] - pix2[4]);
  1060. s += abs(pix1[5] - pix2[5]);
  1061. s += abs(pix1[6] - pix2[6]);
  1062. s += abs(pix1[7] - pix2[7]);
  1063. pix1 += line_size;
  1064. pix2 += line_size;
  1065. }
  1066. return s;
  1067. }
  1068. int pix_abs8x8_x2_c(UINT8 *pix1, UINT8 *pix2, int line_size)
  1069. {
  1070. int s, i;
  1071. s = 0;
  1072. for(i=0;i<8;i++) {
  1073. s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
  1074. s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
  1075. s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
  1076. s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
  1077. s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
  1078. s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
  1079. s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
  1080. s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
  1081. pix1 += line_size;
  1082. pix2 += line_size;
  1083. }
  1084. return s;
  1085. }
  1086. int pix_abs8x8_y2_c(UINT8 *pix1, UINT8 *pix2, int line_size)
  1087. {
  1088. int s, i;
  1089. UINT8 *pix3 = pix2 + line_size;
  1090. s = 0;
  1091. for(i=0;i<8;i++) {
  1092. s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
  1093. s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
  1094. s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
  1095. s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
  1096. s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
  1097. s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
  1098. s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
  1099. s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
  1100. pix1 += line_size;
  1101. pix2 += line_size;
  1102. pix3 += line_size;
  1103. }
  1104. return s;
  1105. }
  1106. int pix_abs8x8_xy2_c(UINT8 *pix1, UINT8 *pix2, int line_size)
  1107. {
  1108. int s, i;
  1109. UINT8 *pix3 = pix2 + line_size;
  1110. s = 0;
  1111. for(i=0;i<8;i++) {
  1112. s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
  1113. s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
  1114. s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
  1115. s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
  1116. s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
  1117. s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
  1118. s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
  1119. s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
  1120. pix1 += line_size;
  1121. pix2 += line_size;
  1122. pix3 += line_size;
  1123. }
  1124. return s;
  1125. }
  1126. /* permute block according so that it corresponds to the MMX idct
  1127. order */
  1128. #ifdef SIMPLE_IDCT
  1129. /* general permutation, but perhaps slightly slower */
  1130. void block_permute(INT16 *block)
  1131. {
  1132. int i;
  1133. INT16 temp[64];
  1134. for(i=0; i<64; i++) temp[ block_permute_op(i) ] = block[i];
  1135. for(i=0; i<64; i++) block[i] = temp[i];
  1136. }
  1137. #else
  1138. void block_permute(INT16 *block)
  1139. {
  1140. int tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
  1141. int i;
  1142. for(i=0;i<8;i++) {
  1143. tmp1 = block[1];
  1144. tmp2 = block[2];
  1145. tmp3 = block[3];
  1146. tmp4 = block[4];
  1147. tmp5 = block[5];
  1148. tmp6 = block[6];
  1149. block[1] = tmp2;
  1150. block[2] = tmp4;
  1151. block[3] = tmp6;
  1152. block[4] = tmp1;
  1153. block[5] = tmp3;
  1154. block[6] = tmp5;
  1155. block += 8;
  1156. }
  1157. }
  1158. #endif
  1159. void clear_blocks_c(DCTELEM *blocks)
  1160. {
  1161. memset(blocks, 0, sizeof(DCTELEM)*6*64);
  1162. }
  1163. /* XXX: those functions should be suppressed ASAP when all IDCTs are
  1164. converted */
  1165. void gen_idct_put(UINT8 *dest, int line_size, DCTELEM *block)
  1166. {
  1167. ff_idct (block);
  1168. put_pixels_clamped(block, dest, line_size);
  1169. }
  1170. void gen_idct_add(UINT8 *dest, int line_size, DCTELEM *block)
  1171. {
  1172. ff_idct (block);
  1173. add_pixels_clamped(block, dest, line_size);
  1174. }
  1175. void dsputil_init(void)
  1176. {
  1177. int i, j;
  1178. int use_permuted_idct;
  1179. for(i=0;i<256;i++) cropTbl[i + MAX_NEG_CROP] = i;
  1180. for(i=0;i<MAX_NEG_CROP;i++) {
  1181. cropTbl[i] = 0;
  1182. cropTbl[i + MAX_NEG_CROP + 256] = 255;
  1183. }
  1184. for(i=0;i<512;i++) {
  1185. squareTbl[i] = (i - 256) * (i - 256);
  1186. }
  1187. #ifdef SIMPLE_IDCT
  1188. ff_idct = NULL;
  1189. #else
  1190. ff_idct = j_rev_dct;
  1191. #endif
  1192. get_pixels = get_pixels_c;
  1193. diff_pixels = diff_pixels_c;
  1194. put_pixels_clamped = put_pixels_clamped_c;
  1195. add_pixels_clamped = add_pixels_clamped_c;
  1196. gmc1= gmc1_c;
  1197. clear_blocks= clear_blocks_c;
  1198. pix_abs16x16 = pix_abs16x16_c;
  1199. pix_abs16x16_x2 = pix_abs16x16_x2_c;
  1200. pix_abs16x16_y2 = pix_abs16x16_y2_c;
  1201. pix_abs16x16_xy2 = pix_abs16x16_xy2_c;
  1202. pix_abs8x8 = pix_abs8x8_c;
  1203. pix_abs8x8_x2 = pix_abs8x8_x2_c;
  1204. pix_abs8x8_y2 = pix_abs8x8_y2_c;
  1205. pix_abs8x8_xy2 = pix_abs8x8_xy2_c;
  1206. av_fdct = fdct_ifast;
  1207. use_permuted_idct = 1;
  1208. #ifdef HAVE_MMX
  1209. dsputil_init_mmx();
  1210. #endif
  1211. #ifdef ARCH_ARMV4L
  1212. dsputil_init_armv4l();
  1213. #endif
  1214. #ifdef HAVE_MLIB
  1215. dsputil_init_mlib();
  1216. use_permuted_idct = 0;
  1217. #endif
  1218. #ifdef ARCH_ALPHA
  1219. dsputil_init_alpha();
  1220. use_permuted_idct = 0;
  1221. #endif
  1222. #ifdef SIMPLE_IDCT
  1223. if (ff_idct == NULL) {
  1224. ff_idct_put = simple_idct_put;
  1225. ff_idct_add = simple_idct_add;
  1226. use_permuted_idct=0;
  1227. } else {
  1228. ff_idct_put = gen_idct_put;
  1229. ff_idct_add = gen_idct_add;
  1230. }
  1231. #endif
  1232. if(use_permuted_idct)
  1233. #ifdef SIMPLE_IDCT
  1234. for(i=0; i<64; i++) permutation[i]= simple_mmx_permutation[i];
  1235. #else
  1236. for(i=0; i<64; i++) permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
  1237. #endif
  1238. else
  1239. for(i=0; i<64; i++) permutation[i]=i;
  1240. for(i=0; i<64; i++) inv_zigzag_direct16[zigzag_direct[i]]= i+1;
  1241. for(i=0; i<64; i++) zigzag_direct_noperm[i]= zigzag_direct[i];
  1242. if (use_permuted_idct) {
  1243. /* permute for IDCT */
  1244. for(i=0;i<64;i++) {
  1245. j = zigzag_direct[i];
  1246. zigzag_direct[i] = block_permute_op(j);
  1247. j = ff_alternate_horizontal_scan[i];
  1248. ff_alternate_horizontal_scan[i] = block_permute_op(j);
  1249. j = ff_alternate_vertical_scan[i];
  1250. ff_alternate_vertical_scan[i] = block_permute_op(j);
  1251. }
  1252. block_permute(default_intra_matrix);
  1253. block_permute(default_non_intra_matrix);
  1254. block_permute(ff_mpeg4_default_intra_matrix);
  1255. block_permute(ff_mpeg4_default_non_intra_matrix);
  1256. }
  1257. build_zigzag_end();
  1258. }
  1259. /* remove any non bit exact operation (testing purpose) */
  1260. void avcodec_set_bit_exact(void)
  1261. {
  1262. #ifdef HAVE_MMX
  1263. dsputil_set_bit_exact_mmx();
  1264. #endif
  1265. }
  1266. void get_psnr(UINT8 *orig_image[3], UINT8 *coded_image[3],
  1267. int orig_linesize[3], int coded_linesize,
  1268. AVCodecContext *avctx)
  1269. {
  1270. int quad, diff, x, y;
  1271. UINT8 *orig, *coded;
  1272. UINT32 *sq = squareTbl + 256;
  1273. quad = 0;
  1274. diff = 0;
  1275. /* Luminance */
  1276. orig = orig_image[0];
  1277. coded = coded_image[0];
  1278. for (y=0;y<avctx->height;y++) {
  1279. for (x=0;x<avctx->width;x++) {
  1280. diff = *(orig + x) - *(coded + x);
  1281. quad += sq[diff];
  1282. }
  1283. orig += orig_linesize[0];
  1284. coded += coded_linesize;
  1285. }
  1286. avctx->psnr_y = (float) quad / (float) (avctx->width * avctx->height);
  1287. if (avctx->psnr_y) {
  1288. avctx->psnr_y = (float) (255 * 255) / avctx->psnr_y;
  1289. avctx->psnr_y = 10 * (float) log10 (avctx->psnr_y);
  1290. } else
  1291. avctx->psnr_y = 99.99;
  1292. }