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.

1432 lines
53KB

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