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.

1045 lines
34KB

  1. /*
  2. * DSP utils
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * DSP utils
  25. */
  26. #include "libavutil/attributes.h"
  27. #include "avcodec.h"
  28. #include "copy_block.h"
  29. #include "dct.h"
  30. #include "dsputil.h"
  31. #include "simple_idct.h"
  32. #include "faandct.h"
  33. #include "mpegvideo.h"
  34. #include "config.h"
  35. uint32_t ff_square_tab[512] = { 0, };
  36. #define BIT_DEPTH 16
  37. #include "dsputilenc_template.c"
  38. #undef BIT_DEPTH
  39. #define BIT_DEPTH 8
  40. #include "dsputilenc_template.c"
  41. static int sse4_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  42. int line_size, int h)
  43. {
  44. int s = 0, i;
  45. uint32_t *sq = ff_square_tab + 256;
  46. for (i = 0; i < h; i++) {
  47. s += sq[pix1[0] - pix2[0]];
  48. s += sq[pix1[1] - pix2[1]];
  49. s += sq[pix1[2] - pix2[2]];
  50. s += sq[pix1[3] - pix2[3]];
  51. pix1 += line_size;
  52. pix2 += line_size;
  53. }
  54. return s;
  55. }
  56. static int sse8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  57. int line_size, int h)
  58. {
  59. int s = 0, i;
  60. uint32_t *sq = ff_square_tab + 256;
  61. for (i = 0; i < h; i++) {
  62. s += sq[pix1[0] - pix2[0]];
  63. s += sq[pix1[1] - pix2[1]];
  64. s += sq[pix1[2] - pix2[2]];
  65. s += sq[pix1[3] - pix2[3]];
  66. s += sq[pix1[4] - pix2[4]];
  67. s += sq[pix1[5] - pix2[5]];
  68. s += sq[pix1[6] - pix2[6]];
  69. s += sq[pix1[7] - pix2[7]];
  70. pix1 += line_size;
  71. pix2 += line_size;
  72. }
  73. return s;
  74. }
  75. static int sse16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  76. int line_size, int h)
  77. {
  78. int s = 0, i;
  79. uint32_t *sq = ff_square_tab + 256;
  80. for (i = 0; i < h; i++) {
  81. s += sq[pix1[0] - pix2[0]];
  82. s += sq[pix1[1] - pix2[1]];
  83. s += sq[pix1[2] - pix2[2]];
  84. s += sq[pix1[3] - pix2[3]];
  85. s += sq[pix1[4] - pix2[4]];
  86. s += sq[pix1[5] - pix2[5]];
  87. s += sq[pix1[6] - pix2[6]];
  88. s += sq[pix1[7] - pix2[7]];
  89. s += sq[pix1[8] - pix2[8]];
  90. s += sq[pix1[9] - pix2[9]];
  91. s += sq[pix1[10] - pix2[10]];
  92. s += sq[pix1[11] - pix2[11]];
  93. s += sq[pix1[12] - pix2[12]];
  94. s += sq[pix1[13] - pix2[13]];
  95. s += sq[pix1[14] - pix2[14]];
  96. s += sq[pix1[15] - pix2[15]];
  97. pix1 += line_size;
  98. pix2 += line_size;
  99. }
  100. return s;
  101. }
  102. static void diff_pixels_c(int16_t *restrict block, const uint8_t *s1,
  103. const uint8_t *s2, int stride)
  104. {
  105. int i;
  106. /* read the pixels */
  107. for (i = 0; i < 8; i++) {
  108. block[0] = s1[0] - s2[0];
  109. block[1] = s1[1] - s2[1];
  110. block[2] = s1[2] - s2[2];
  111. block[3] = s1[3] - s2[3];
  112. block[4] = s1[4] - s2[4];
  113. block[5] = s1[5] - s2[5];
  114. block[6] = s1[6] - s2[6];
  115. block[7] = s1[7] - s2[7];
  116. s1 += stride;
  117. s2 += stride;
  118. block += 8;
  119. }
  120. }
  121. static int sum_abs_dctelem_c(int16_t *block)
  122. {
  123. int sum = 0, i;
  124. for (i = 0; i < 64; i++)
  125. sum += FFABS(block[i]);
  126. return sum;
  127. }
  128. #define avg2(a, b) ((a + b + 1) >> 1)
  129. #define avg4(a, b, c, d) ((a + b + c + d + 2) >> 2)
  130. static inline int pix_abs16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  131. int line_size, int h)
  132. {
  133. int s = 0, i;
  134. for (i = 0; i < h; i++) {
  135. s += abs(pix1[0] - pix2[0]);
  136. s += abs(pix1[1] - pix2[1]);
  137. s += abs(pix1[2] - pix2[2]);
  138. s += abs(pix1[3] - pix2[3]);
  139. s += abs(pix1[4] - pix2[4]);
  140. s += abs(pix1[5] - pix2[5]);
  141. s += abs(pix1[6] - pix2[6]);
  142. s += abs(pix1[7] - pix2[7]);
  143. s += abs(pix1[8] - pix2[8]);
  144. s += abs(pix1[9] - pix2[9]);
  145. s += abs(pix1[10] - pix2[10]);
  146. s += abs(pix1[11] - pix2[11]);
  147. s += abs(pix1[12] - pix2[12]);
  148. s += abs(pix1[13] - pix2[13]);
  149. s += abs(pix1[14] - pix2[14]);
  150. s += abs(pix1[15] - pix2[15]);
  151. pix1 += line_size;
  152. pix2 += line_size;
  153. }
  154. return s;
  155. }
  156. static int pix_abs16_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  157. int line_size, int h)
  158. {
  159. int s = 0, i;
  160. for (i = 0; i < h; i++) {
  161. s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
  162. s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
  163. s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
  164. s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
  165. s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
  166. s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
  167. s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
  168. s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
  169. s += abs(pix1[8] - avg2(pix2[8], pix2[9]));
  170. s += abs(pix1[9] - avg2(pix2[9], pix2[10]));
  171. s += abs(pix1[10] - avg2(pix2[10], pix2[11]));
  172. s += abs(pix1[11] - avg2(pix2[11], pix2[12]));
  173. s += abs(pix1[12] - avg2(pix2[12], pix2[13]));
  174. s += abs(pix1[13] - avg2(pix2[13], pix2[14]));
  175. s += abs(pix1[14] - avg2(pix2[14], pix2[15]));
  176. s += abs(pix1[15] - avg2(pix2[15], pix2[16]));
  177. pix1 += line_size;
  178. pix2 += line_size;
  179. }
  180. return s;
  181. }
  182. static int pix_abs16_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  183. int line_size, int h)
  184. {
  185. int s = 0, i;
  186. uint8_t *pix3 = pix2 + line_size;
  187. for (i = 0; i < h; i++) {
  188. s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
  189. s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
  190. s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
  191. s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
  192. s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
  193. s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
  194. s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
  195. s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
  196. s += abs(pix1[8] - avg2(pix2[8], pix3[8]));
  197. s += abs(pix1[9] - avg2(pix2[9], pix3[9]));
  198. s += abs(pix1[10] - avg2(pix2[10], pix3[10]));
  199. s += abs(pix1[11] - avg2(pix2[11], pix3[11]));
  200. s += abs(pix1[12] - avg2(pix2[12], pix3[12]));
  201. s += abs(pix1[13] - avg2(pix2[13], pix3[13]));
  202. s += abs(pix1[14] - avg2(pix2[14], pix3[14]));
  203. s += abs(pix1[15] - avg2(pix2[15], pix3[15]));
  204. pix1 += line_size;
  205. pix2 += line_size;
  206. pix3 += line_size;
  207. }
  208. return s;
  209. }
  210. static int pix_abs16_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  211. int line_size, int h)
  212. {
  213. int s = 0, i;
  214. uint8_t *pix3 = pix2 + line_size;
  215. for (i = 0; i < h; i++) {
  216. s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
  217. s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
  218. s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
  219. s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
  220. s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
  221. s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
  222. s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
  223. s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
  224. s += abs(pix1[8] - avg4(pix2[8], pix2[9], pix3[8], pix3[9]));
  225. s += abs(pix1[9] - avg4(pix2[9], pix2[10], pix3[9], pix3[10]));
  226. s += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
  227. s += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
  228. s += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
  229. s += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
  230. s += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
  231. s += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
  232. pix1 += line_size;
  233. pix2 += line_size;
  234. pix3 += line_size;
  235. }
  236. return s;
  237. }
  238. static inline int pix_abs8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  239. int line_size, int h)
  240. {
  241. int s = 0, i;
  242. for (i = 0; i < h; i++) {
  243. s += abs(pix1[0] - pix2[0]);
  244. s += abs(pix1[1] - pix2[1]);
  245. s += abs(pix1[2] - pix2[2]);
  246. s += abs(pix1[3] - pix2[3]);
  247. s += abs(pix1[4] - pix2[4]);
  248. s += abs(pix1[5] - pix2[5]);
  249. s += abs(pix1[6] - pix2[6]);
  250. s += abs(pix1[7] - pix2[7]);
  251. pix1 += line_size;
  252. pix2 += line_size;
  253. }
  254. return s;
  255. }
  256. static int pix_abs8_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  257. int line_size, int h)
  258. {
  259. int s = 0, i;
  260. for (i = 0; i < h; i++) {
  261. s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
  262. s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
  263. s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
  264. s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
  265. s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
  266. s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
  267. s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
  268. s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
  269. pix1 += line_size;
  270. pix2 += line_size;
  271. }
  272. return s;
  273. }
  274. static int pix_abs8_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  275. int line_size, int h)
  276. {
  277. int s = 0, i;
  278. uint8_t *pix3 = pix2 + line_size;
  279. for (i = 0; i < h; i++) {
  280. s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
  281. s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
  282. s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
  283. s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
  284. s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
  285. s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
  286. s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
  287. s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
  288. pix1 += line_size;
  289. pix2 += line_size;
  290. pix3 += line_size;
  291. }
  292. return s;
  293. }
  294. static int pix_abs8_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  295. int line_size, int h)
  296. {
  297. int s = 0, i;
  298. uint8_t *pix3 = pix2 + line_size;
  299. for (i = 0; i < h; i++) {
  300. s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
  301. s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
  302. s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
  303. s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
  304. s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
  305. s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
  306. s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
  307. s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
  308. pix1 += line_size;
  309. pix2 += line_size;
  310. pix3 += line_size;
  311. }
  312. return s;
  313. }
  314. static int nsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
  315. {
  316. int score1 = 0, score2 = 0, x, y;
  317. for (y = 0; y < h; y++) {
  318. for (x = 0; x < 16; x++)
  319. score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
  320. if (y + 1 < h) {
  321. for (x = 0; x < 15; x++)
  322. score2 += FFABS(s1[x] - s1[x + stride] -
  323. s1[x + 1] + s1[x + stride + 1]) -
  324. FFABS(s2[x] - s2[x + stride] -
  325. s2[x + 1] + s2[x + stride + 1]);
  326. }
  327. s1 += stride;
  328. s2 += stride;
  329. }
  330. if (c)
  331. return score1 + FFABS(score2) * c->avctx->nsse_weight;
  332. else
  333. return score1 + FFABS(score2) * 8;
  334. }
  335. static int nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
  336. {
  337. int score1 = 0, score2 = 0, x, y;
  338. for (y = 0; y < h; y++) {
  339. for (x = 0; x < 8; x++)
  340. score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
  341. if (y + 1 < h) {
  342. for (x = 0; x < 7; x++)
  343. score2 += FFABS(s1[x] - s1[x + stride] -
  344. s1[x + 1] + s1[x + stride + 1]) -
  345. FFABS(s2[x] - s2[x + stride] -
  346. s2[x + 1] + s2[x + stride + 1]);
  347. }
  348. s1 += stride;
  349. s2 += stride;
  350. }
  351. if (c)
  352. return score1 + FFABS(score2) * c->avctx->nsse_weight;
  353. else
  354. return score1 + FFABS(score2) * 8;
  355. }
  356. static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
  357. int stride, int h)
  358. {
  359. return 0;
  360. }
  361. void ff_set_cmp(DSPContext *c, me_cmp_func *cmp, int type)
  362. {
  363. int i;
  364. memset(cmp, 0, sizeof(void *) * 6);
  365. for (i = 0; i < 6; i++) {
  366. switch (type & 0xFF) {
  367. case FF_CMP_SAD:
  368. cmp[i] = c->sad[i];
  369. break;
  370. case FF_CMP_SATD:
  371. cmp[i] = c->hadamard8_diff[i];
  372. break;
  373. case FF_CMP_SSE:
  374. cmp[i] = c->sse[i];
  375. break;
  376. case FF_CMP_DCT:
  377. cmp[i] = c->dct_sad[i];
  378. break;
  379. case FF_CMP_DCT264:
  380. cmp[i] = c->dct264_sad[i];
  381. break;
  382. case FF_CMP_DCTMAX:
  383. cmp[i] = c->dct_max[i];
  384. break;
  385. case FF_CMP_PSNR:
  386. cmp[i] = c->quant_psnr[i];
  387. break;
  388. case FF_CMP_BIT:
  389. cmp[i] = c->bit[i];
  390. break;
  391. case FF_CMP_RD:
  392. cmp[i] = c->rd[i];
  393. break;
  394. case FF_CMP_VSAD:
  395. cmp[i] = c->vsad[i];
  396. break;
  397. case FF_CMP_VSSE:
  398. cmp[i] = c->vsse[i];
  399. break;
  400. case FF_CMP_ZERO:
  401. cmp[i] = zero_cmp;
  402. break;
  403. case FF_CMP_NSSE:
  404. cmp[i] = c->nsse[i];
  405. break;
  406. default:
  407. av_log(NULL, AV_LOG_ERROR,
  408. "internal error in cmp function selection\n");
  409. }
  410. }
  411. }
  412. #define BUTTERFLY2(o1, o2, i1, i2) \
  413. o1 = (i1) + (i2); \
  414. o2 = (i1) - (i2);
  415. #define BUTTERFLY1(x, y) \
  416. { \
  417. int a, b; \
  418. a = x; \
  419. b = y; \
  420. x = a + b; \
  421. y = a - b; \
  422. }
  423. #define BUTTERFLYA(x, y) (FFABS((x) + (y)) + FFABS((x) - (y)))
  424. static int hadamard8_diff8x8_c(MpegEncContext *s, uint8_t *dst,
  425. uint8_t *src, int stride, int h)
  426. {
  427. int i, temp[64], sum = 0;
  428. assert(h == 8);
  429. for (i = 0; i < 8; i++) {
  430. // FIXME: try pointer walks
  431. BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
  432. src[stride * i + 0] - dst[stride * i + 0],
  433. src[stride * i + 1] - dst[stride * i + 1]);
  434. BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
  435. src[stride * i + 2] - dst[stride * i + 2],
  436. src[stride * i + 3] - dst[stride * i + 3]);
  437. BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
  438. src[stride * i + 4] - dst[stride * i + 4],
  439. src[stride * i + 5] - dst[stride * i + 5]);
  440. BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
  441. src[stride * i + 6] - dst[stride * i + 6],
  442. src[stride * i + 7] - dst[stride * i + 7]);
  443. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
  444. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
  445. BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
  446. BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
  447. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
  448. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
  449. BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
  450. BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
  451. }
  452. for (i = 0; i < 8; i++) {
  453. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
  454. BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
  455. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
  456. BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
  457. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
  458. BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
  459. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
  460. BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
  461. sum += BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i]) +
  462. BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i]) +
  463. BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i]) +
  464. BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
  465. }
  466. return sum;
  467. }
  468. static int hadamard8_intra8x8_c(MpegEncContext *s, uint8_t *src,
  469. uint8_t *dummy, int stride, int h)
  470. {
  471. int i, temp[64], sum = 0;
  472. assert(h == 8);
  473. for (i = 0; i < 8; i++) {
  474. // FIXME: try pointer walks
  475. BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
  476. src[stride * i + 0], src[stride * i + 1]);
  477. BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
  478. src[stride * i + 2], src[stride * i + 3]);
  479. BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
  480. src[stride * i + 4], src[stride * i + 5]);
  481. BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
  482. src[stride * i + 6], src[stride * i + 7]);
  483. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
  484. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
  485. BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
  486. BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
  487. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
  488. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
  489. BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
  490. BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
  491. }
  492. for (i = 0; i < 8; i++) {
  493. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
  494. BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
  495. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
  496. BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
  497. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
  498. BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
  499. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
  500. BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
  501. sum +=
  502. BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i])
  503. + BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i])
  504. + BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i])
  505. + BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
  506. }
  507. sum -= FFABS(temp[8 * 0] + temp[8 * 4]); // -mean
  508. return sum;
  509. }
  510. static int dct_sad8x8_c(MpegEncContext *s, uint8_t *src1,
  511. uint8_t *src2, int stride, int h)
  512. {
  513. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  514. assert(h == 8);
  515. s->dsp.diff_pixels(temp, src1, src2, stride);
  516. s->dsp.fdct(temp);
  517. return s->dsp.sum_abs_dctelem(temp);
  518. }
  519. #if CONFIG_GPL
  520. #define DCT8_1D \
  521. { \
  522. const int s07 = SRC(0) + SRC(7); \
  523. const int s16 = SRC(1) + SRC(6); \
  524. const int s25 = SRC(2) + SRC(5); \
  525. const int s34 = SRC(3) + SRC(4); \
  526. const int a0 = s07 + s34; \
  527. const int a1 = s16 + s25; \
  528. const int a2 = s07 - s34; \
  529. const int a3 = s16 - s25; \
  530. const int d07 = SRC(0) - SRC(7); \
  531. const int d16 = SRC(1) - SRC(6); \
  532. const int d25 = SRC(2) - SRC(5); \
  533. const int d34 = SRC(3) - SRC(4); \
  534. const int a4 = d16 + d25 + (d07 + (d07 >> 1)); \
  535. const int a5 = d07 - d34 - (d25 + (d25 >> 1)); \
  536. const int a6 = d07 + d34 - (d16 + (d16 >> 1)); \
  537. const int a7 = d16 - d25 + (d34 + (d34 >> 1)); \
  538. DST(0, a0 + a1); \
  539. DST(1, a4 + (a7 >> 2)); \
  540. DST(2, a2 + (a3 >> 1)); \
  541. DST(3, a5 + (a6 >> 2)); \
  542. DST(4, a0 - a1); \
  543. DST(5, a6 - (a5 >> 2)); \
  544. DST(6, (a2 >> 1) - a3); \
  545. DST(7, (a4 >> 2) - a7); \
  546. }
  547. static int dct264_sad8x8_c(MpegEncContext *s, uint8_t *src1,
  548. uint8_t *src2, int stride, int h)
  549. {
  550. int16_t dct[8][8];
  551. int i, sum = 0;
  552. s->dsp.diff_pixels(dct[0], src1, src2, stride);
  553. #define SRC(x) dct[i][x]
  554. #define DST(x, v) dct[i][x] = v
  555. for (i = 0; i < 8; i++)
  556. DCT8_1D
  557. #undef SRC
  558. #undef DST
  559. #define SRC(x) dct[x][i]
  560. #define DST(x, v) sum += FFABS(v)
  561. for (i = 0; i < 8; i++)
  562. DCT8_1D
  563. #undef SRC
  564. #undef DST
  565. return sum;
  566. }
  567. #endif
  568. static int dct_max8x8_c(MpegEncContext *s, uint8_t *src1,
  569. uint8_t *src2, int stride, int h)
  570. {
  571. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  572. int sum = 0, i;
  573. assert(h == 8);
  574. s->dsp.diff_pixels(temp, src1, src2, stride);
  575. s->dsp.fdct(temp);
  576. for (i = 0; i < 64; i++)
  577. sum = FFMAX(sum, FFABS(temp[i]));
  578. return sum;
  579. }
  580. static int quant_psnr8x8_c(MpegEncContext *s, uint8_t *src1,
  581. uint8_t *src2, int stride, int h)
  582. {
  583. LOCAL_ALIGNED_16(int16_t, temp, [64 * 2]);
  584. int16_t *const bak = temp + 64;
  585. int sum = 0, i;
  586. assert(h == 8);
  587. s->mb_intra = 0;
  588. s->dsp.diff_pixels(temp, src1, src2, stride);
  589. memcpy(bak, temp, 64 * sizeof(int16_t));
  590. s->block_last_index[0 /* FIXME */] =
  591. s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
  592. s->dct_unquantize_inter(s, temp, 0, s->qscale);
  593. ff_simple_idct_8(temp); // FIXME
  594. for (i = 0; i < 64; i++)
  595. sum += (temp[i] - bak[i]) * (temp[i] - bak[i]);
  596. return sum;
  597. }
  598. static int rd8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
  599. int stride, int h)
  600. {
  601. const uint8_t *scantable = s->intra_scantable.permutated;
  602. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  603. LOCAL_ALIGNED_16(uint8_t, lsrc1, [64]);
  604. LOCAL_ALIGNED_16(uint8_t, lsrc2, [64]);
  605. int i, last, run, bits, level, distortion, start_i;
  606. const int esc_length = s->ac_esc_length;
  607. uint8_t *length, *last_length;
  608. assert(h == 8);
  609. copy_block8(lsrc1, src1, 8, stride, 8);
  610. copy_block8(lsrc2, src2, 8, stride, 8);
  611. s->dsp.diff_pixels(temp, lsrc1, lsrc2, 8);
  612. s->block_last_index[0 /* FIXME */] =
  613. last =
  614. s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
  615. bits = 0;
  616. if (s->mb_intra) {
  617. start_i = 1;
  618. length = s->intra_ac_vlc_length;
  619. last_length = s->intra_ac_vlc_last_length;
  620. bits += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
  621. } else {
  622. start_i = 0;
  623. length = s->inter_ac_vlc_length;
  624. last_length = s->inter_ac_vlc_last_length;
  625. }
  626. if (last >= start_i) {
  627. run = 0;
  628. for (i = start_i; i < last; i++) {
  629. int j = scantable[i];
  630. level = temp[j];
  631. if (level) {
  632. level += 64;
  633. if ((level & (~127)) == 0)
  634. bits += length[UNI_AC_ENC_INDEX(run, level)];
  635. else
  636. bits += esc_length;
  637. run = 0;
  638. } else
  639. run++;
  640. }
  641. i = scantable[last];
  642. level = temp[i] + 64;
  643. assert(level - 64);
  644. if ((level & (~127)) == 0) {
  645. bits += last_length[UNI_AC_ENC_INDEX(run, level)];
  646. } else
  647. bits += esc_length;
  648. }
  649. if (last >= 0) {
  650. if (s->mb_intra)
  651. s->dct_unquantize_intra(s, temp, 0, s->qscale);
  652. else
  653. s->dct_unquantize_inter(s, temp, 0, s->qscale);
  654. }
  655. s->idsp.idct_add(lsrc2, 8, temp);
  656. distortion = s->dsp.sse[1](NULL, lsrc2, lsrc1, 8, 8);
  657. return distortion + ((bits * s->qscale * s->qscale * 109 + 64) >> 7);
  658. }
  659. static int bit8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
  660. int stride, int h)
  661. {
  662. const uint8_t *scantable = s->intra_scantable.permutated;
  663. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  664. int i, last, run, bits, level, start_i;
  665. const int esc_length = s->ac_esc_length;
  666. uint8_t *length, *last_length;
  667. assert(h == 8);
  668. s->dsp.diff_pixels(temp, src1, src2, stride);
  669. s->block_last_index[0 /* FIXME */] =
  670. last =
  671. s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
  672. bits = 0;
  673. if (s->mb_intra) {
  674. start_i = 1;
  675. length = s->intra_ac_vlc_length;
  676. last_length = s->intra_ac_vlc_last_length;
  677. bits += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
  678. } else {
  679. start_i = 0;
  680. length = s->inter_ac_vlc_length;
  681. last_length = s->inter_ac_vlc_last_length;
  682. }
  683. if (last >= start_i) {
  684. run = 0;
  685. for (i = start_i; i < last; i++) {
  686. int j = scantable[i];
  687. level = temp[j];
  688. if (level) {
  689. level += 64;
  690. if ((level & (~127)) == 0)
  691. bits += length[UNI_AC_ENC_INDEX(run, level)];
  692. else
  693. bits += esc_length;
  694. run = 0;
  695. } else
  696. run++;
  697. }
  698. i = scantable[last];
  699. level = temp[i] + 64;
  700. assert(level - 64);
  701. if ((level & (~127)) == 0)
  702. bits += last_length[UNI_AC_ENC_INDEX(run, level)];
  703. else
  704. bits += esc_length;
  705. }
  706. return bits;
  707. }
  708. #define VSAD_INTRA(size) \
  709. static int vsad_intra ## size ## _c(MpegEncContext *c, \
  710. uint8_t *s, uint8_t *dummy, \
  711. int stride, int h) \
  712. { \
  713. int score = 0, x, y; \
  714. \
  715. for (y = 1; y < h; y++) { \
  716. for (x = 0; x < size; x += 4) { \
  717. score += FFABS(s[x] - s[x + stride]) + \
  718. FFABS(s[x + 1] - s[x + stride + 1]) + \
  719. FFABS(s[x + 2] - s[x + 2 + stride]) + \
  720. FFABS(s[x + 3] - s[x + 3 + stride]); \
  721. } \
  722. s += stride; \
  723. } \
  724. \
  725. return score; \
  726. }
  727. VSAD_INTRA(8)
  728. VSAD_INTRA(16)
  729. static int vsad16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,
  730. int stride, int h)
  731. {
  732. int score = 0, x, y;
  733. for (y = 1; y < h; y++) {
  734. for (x = 0; x < 16; x++)
  735. score += FFABS(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);
  736. s1 += stride;
  737. s2 += stride;
  738. }
  739. return score;
  740. }
  741. #define SQ(a) ((a) * (a))
  742. #define VSSE_INTRA(size) \
  743. static int vsse_intra ## size ## _c(MpegEncContext *c, \
  744. uint8_t *s, uint8_t *dummy, \
  745. int stride, int h) \
  746. { \
  747. int score = 0, x, y; \
  748. \
  749. for (y = 1; y < h; y++) { \
  750. for (x = 0; x < size; x += 4) { \
  751. score += SQ(s[x] - s[x + stride]) + \
  752. SQ(s[x + 1] - s[x + stride + 1]) + \
  753. SQ(s[x + 2] - s[x + stride + 2]) + \
  754. SQ(s[x + 3] - s[x + stride + 3]); \
  755. } \
  756. s += stride; \
  757. } \
  758. \
  759. return score; \
  760. }
  761. VSSE_INTRA(8)
  762. VSSE_INTRA(16)
  763. static int vsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,
  764. int stride, int h)
  765. {
  766. int score = 0, x, y;
  767. for (y = 1; y < h; y++) {
  768. for (x = 0; x < 16; x++)
  769. score += SQ(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);
  770. s1 += stride;
  771. s2 += stride;
  772. }
  773. return score;
  774. }
  775. #define WRAPPER8_16_SQ(name8, name16) \
  776. static int name16(MpegEncContext *s, uint8_t *dst, uint8_t *src, \
  777. int stride, int h) \
  778. { \
  779. int score = 0; \
  780. \
  781. score += name8(s, dst, src, stride, 8); \
  782. score += name8(s, dst + 8, src + 8, stride, 8); \
  783. if (h == 16) { \
  784. dst += 8 * stride; \
  785. src += 8 * stride; \
  786. score += name8(s, dst, src, stride, 8); \
  787. score += name8(s, dst + 8, src + 8, stride, 8); \
  788. } \
  789. return score; \
  790. }
  791. WRAPPER8_16_SQ(hadamard8_diff8x8_c, hadamard8_diff16_c)
  792. WRAPPER8_16_SQ(hadamard8_intra8x8_c, hadamard8_intra16_c)
  793. WRAPPER8_16_SQ(dct_sad8x8_c, dct_sad16_c)
  794. #if CONFIG_GPL
  795. WRAPPER8_16_SQ(dct264_sad8x8_c, dct264_sad16_c)
  796. #endif
  797. WRAPPER8_16_SQ(dct_max8x8_c, dct_max16_c)
  798. WRAPPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c)
  799. WRAPPER8_16_SQ(rd8x8_c, rd16_c)
  800. WRAPPER8_16_SQ(bit8x8_c, bit16_c)
  801. /* draw the edges of width 'w' of an image of size width, height */
  802. // FIXME: Check that this is OK for MPEG-4 interlaced.
  803. static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
  804. int w, int h, int sides)
  805. {
  806. uint8_t *ptr = buf, *last_line;
  807. int i;
  808. /* left and right */
  809. for (i = 0; i < height; i++) {
  810. memset(ptr - w, ptr[0], w);
  811. memset(ptr + width, ptr[width - 1], w);
  812. ptr += wrap;
  813. }
  814. /* top and bottom + corners */
  815. buf -= w;
  816. last_line = buf + (height - 1) * wrap;
  817. if (sides & EDGE_TOP)
  818. for (i = 0; i < h; i++)
  819. // top
  820. memcpy(buf - (i + 1) * wrap, buf, width + w + w);
  821. if (sides & EDGE_BOTTOM)
  822. for (i = 0; i < h; i++)
  823. // bottom
  824. memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
  825. }
  826. /* init static data */
  827. av_cold void ff_dsputil_static_init(void)
  828. {
  829. int i;
  830. for (i = 0; i < 512; i++)
  831. ff_square_tab[i] = (i - 256) * (i - 256);
  832. }
  833. av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
  834. {
  835. const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
  836. #if CONFIG_ENCODERS
  837. if (avctx->bits_per_raw_sample == 10) {
  838. c->fdct = ff_jpeg_fdct_islow_10;
  839. c->fdct248 = ff_fdct248_islow_10;
  840. } else {
  841. if (avctx->dct_algo == FF_DCT_FASTINT) {
  842. c->fdct = ff_fdct_ifast;
  843. c->fdct248 = ff_fdct_ifast248;
  844. } else if (avctx->dct_algo == FF_DCT_FAAN) {
  845. c->fdct = ff_faandct;
  846. c->fdct248 = ff_faandct248;
  847. } else {
  848. c->fdct = ff_jpeg_fdct_islow_8; // slow/accurate/default
  849. c->fdct248 = ff_fdct248_islow_8;
  850. }
  851. }
  852. #endif /* CONFIG_ENCODERS */
  853. c->diff_pixels = diff_pixels_c;
  854. c->sum_abs_dctelem = sum_abs_dctelem_c;
  855. /* TODO [0] 16 [1] 8 */
  856. c->pix_abs[0][0] = pix_abs16_c;
  857. c->pix_abs[0][1] = pix_abs16_x2_c;
  858. c->pix_abs[0][2] = pix_abs16_y2_c;
  859. c->pix_abs[0][3] = pix_abs16_xy2_c;
  860. c->pix_abs[1][0] = pix_abs8_c;
  861. c->pix_abs[1][1] = pix_abs8_x2_c;
  862. c->pix_abs[1][2] = pix_abs8_y2_c;
  863. c->pix_abs[1][3] = pix_abs8_xy2_c;
  864. #define SET_CMP_FUNC(name) \
  865. c->name[0] = name ## 16_c; \
  866. c->name[1] = name ## 8x8_c;
  867. SET_CMP_FUNC(hadamard8_diff)
  868. c->hadamard8_diff[4] = hadamard8_intra16_c;
  869. c->hadamard8_diff[5] = hadamard8_intra8x8_c;
  870. SET_CMP_FUNC(dct_sad)
  871. SET_CMP_FUNC(dct_max)
  872. #if CONFIG_GPL
  873. SET_CMP_FUNC(dct264_sad)
  874. #endif
  875. c->sad[0] = pix_abs16_c;
  876. c->sad[1] = pix_abs8_c;
  877. c->sse[0] = sse16_c;
  878. c->sse[1] = sse8_c;
  879. c->sse[2] = sse4_c;
  880. SET_CMP_FUNC(quant_psnr)
  881. SET_CMP_FUNC(rd)
  882. SET_CMP_FUNC(bit)
  883. c->vsad[0] = vsad16_c;
  884. c->vsad[4] = vsad_intra16_c;
  885. c->vsad[5] = vsad_intra8_c;
  886. c->vsse[0] = vsse16_c;
  887. c->vsse[4] = vsse_intra16_c;
  888. c->vsse[5] = vsse_intra8_c;
  889. c->nsse[0] = nsse16_c;
  890. c->nsse[1] = nsse8_c;
  891. c->draw_edges = draw_edges_8_c;
  892. switch (avctx->bits_per_raw_sample) {
  893. case 9:
  894. case 10:
  895. c->get_pixels = get_pixels_16_c;
  896. break;
  897. default:
  898. c->get_pixels = get_pixels_8_c;
  899. break;
  900. }
  901. if (ARCH_ARM)
  902. ff_dsputil_init_arm(c, avctx, high_bit_depth);
  903. if (ARCH_PPC)
  904. ff_dsputil_init_ppc(c, avctx, high_bit_depth);
  905. if (ARCH_X86)
  906. ff_dsputil_init_x86(c, avctx, high_bit_depth);
  907. }