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.

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