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.

1333 lines
43KB

  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 "libavutil/imgutils.h"
  28. #include "avcodec.h"
  29. #include "copy_block.h"
  30. #include "dct.h"
  31. #include "dsputil.h"
  32. #include "simple_idct.h"
  33. #include "faandct.h"
  34. #include "faanidct.h"
  35. #include "imgconvert.h"
  36. #include "mathops.h"
  37. #include "mpegvideo.h"
  38. #include "config.h"
  39. uint32_t ff_square_tab[512] = { 0, };
  40. #define BIT_DEPTH 16
  41. #include "dsputilenc_template.c"
  42. #undef BIT_DEPTH
  43. #define BIT_DEPTH 8
  44. #include "dsputilenc_template.c"
  45. /* Input permutation for the simple_idct_mmx */
  46. static const uint8_t simple_mmx_permutation[64] = {
  47. 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
  48. 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
  49. 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
  50. 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
  51. 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
  52. 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
  53. 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
  54. 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
  55. };
  56. static const uint8_t idct_sse2_row_perm[8] = { 0, 4, 1, 5, 2, 6, 3, 7 };
  57. av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st,
  58. const uint8_t *src_scantable)
  59. {
  60. int i, end;
  61. st->scantable = src_scantable;
  62. for (i = 0; i < 64; i++) {
  63. int j = src_scantable[i];
  64. st->permutated[i] = permutation[j];
  65. }
  66. end = -1;
  67. for (i = 0; i < 64; i++) {
  68. int j = st->permutated[i];
  69. if (j > end)
  70. end = j;
  71. st->raster_end[i] = end;
  72. }
  73. }
  74. av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
  75. int idct_permutation_type)
  76. {
  77. int i;
  78. switch (idct_permutation_type) {
  79. case FF_NO_IDCT_PERM:
  80. for (i = 0; i < 64; i++)
  81. idct_permutation[i] = i;
  82. break;
  83. case FF_LIBMPEG2_IDCT_PERM:
  84. for (i = 0; i < 64; i++)
  85. idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
  86. break;
  87. case FF_SIMPLE_IDCT_PERM:
  88. for (i = 0; i < 64; i++)
  89. idct_permutation[i] = simple_mmx_permutation[i];
  90. break;
  91. case FF_TRANSPOSE_IDCT_PERM:
  92. for (i = 0; i < 64; i++)
  93. idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
  94. break;
  95. case FF_PARTTRANS_IDCT_PERM:
  96. for (i = 0; i < 64; i++)
  97. idct_permutation[i] = (i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3);
  98. break;
  99. case FF_SSE2_IDCT_PERM:
  100. for (i = 0; i < 64; i++)
  101. idct_permutation[i] = (i & 0x38) | idct_sse2_row_perm[i & 7];
  102. break;
  103. default:
  104. av_log(NULL, AV_LOG_ERROR,
  105. "Internal error, IDCT permutation not set\n");
  106. }
  107. }
  108. static int pix_sum_c(uint8_t *pix, int line_size)
  109. {
  110. int s = 0, i, j;
  111. for (i = 0; i < 16; i++) {
  112. for (j = 0; j < 16; j += 8) {
  113. s += pix[0];
  114. s += pix[1];
  115. s += pix[2];
  116. s += pix[3];
  117. s += pix[4];
  118. s += pix[5];
  119. s += pix[6];
  120. s += pix[7];
  121. pix += 8;
  122. }
  123. pix += line_size - 16;
  124. }
  125. return s;
  126. }
  127. static int pix_norm1_c(uint8_t *pix, int line_size)
  128. {
  129. int s = 0, i, j;
  130. uint32_t *sq = ff_square_tab + 256;
  131. for (i = 0; i < 16; i++) {
  132. for (j = 0; j < 16; j += 8) {
  133. #if 0
  134. s += sq[pix[0]];
  135. s += sq[pix[1]];
  136. s += sq[pix[2]];
  137. s += sq[pix[3]];
  138. s += sq[pix[4]];
  139. s += sq[pix[5]];
  140. s += sq[pix[6]];
  141. s += sq[pix[7]];
  142. #else
  143. #if HAVE_FAST_64BIT
  144. register uint64_t x = *(uint64_t *) pix;
  145. s += sq[x & 0xff];
  146. s += sq[(x >> 8) & 0xff];
  147. s += sq[(x >> 16) & 0xff];
  148. s += sq[(x >> 24) & 0xff];
  149. s += sq[(x >> 32) & 0xff];
  150. s += sq[(x >> 40) & 0xff];
  151. s += sq[(x >> 48) & 0xff];
  152. s += sq[(x >> 56) & 0xff];
  153. #else
  154. register uint32_t x = *(uint32_t *) pix;
  155. s += sq[x & 0xff];
  156. s += sq[(x >> 8) & 0xff];
  157. s += sq[(x >> 16) & 0xff];
  158. s += sq[(x >> 24) & 0xff];
  159. x = *(uint32_t *) (pix + 4);
  160. s += sq[x & 0xff];
  161. s += sq[(x >> 8) & 0xff];
  162. s += sq[(x >> 16) & 0xff];
  163. s += sq[(x >> 24) & 0xff];
  164. #endif
  165. #endif
  166. pix += 8;
  167. }
  168. pix += line_size - 16;
  169. }
  170. return s;
  171. }
  172. static int sse4_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  173. int line_size, int h)
  174. {
  175. int s = 0, i;
  176. uint32_t *sq = ff_square_tab + 256;
  177. for (i = 0; i < h; i++) {
  178. s += sq[pix1[0] - pix2[0]];
  179. s += sq[pix1[1] - pix2[1]];
  180. s += sq[pix1[2] - pix2[2]];
  181. s += sq[pix1[3] - pix2[3]];
  182. pix1 += line_size;
  183. pix2 += line_size;
  184. }
  185. return s;
  186. }
  187. static int sse8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  188. int line_size, int h)
  189. {
  190. int s = 0, i;
  191. uint32_t *sq = ff_square_tab + 256;
  192. for (i = 0; i < h; i++) {
  193. s += sq[pix1[0] - pix2[0]];
  194. s += sq[pix1[1] - pix2[1]];
  195. s += sq[pix1[2] - pix2[2]];
  196. s += sq[pix1[3] - pix2[3]];
  197. s += sq[pix1[4] - pix2[4]];
  198. s += sq[pix1[5] - pix2[5]];
  199. s += sq[pix1[6] - pix2[6]];
  200. s += sq[pix1[7] - pix2[7]];
  201. pix1 += line_size;
  202. pix2 += line_size;
  203. }
  204. return s;
  205. }
  206. static int sse16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  207. int line_size, int h)
  208. {
  209. int s = 0, i;
  210. uint32_t *sq = ff_square_tab + 256;
  211. for (i = 0; i < h; i++) {
  212. s += sq[pix1[0] - pix2[0]];
  213. s += sq[pix1[1] - pix2[1]];
  214. s += sq[pix1[2] - pix2[2]];
  215. s += sq[pix1[3] - pix2[3]];
  216. s += sq[pix1[4] - pix2[4]];
  217. s += sq[pix1[5] - pix2[5]];
  218. s += sq[pix1[6] - pix2[6]];
  219. s += sq[pix1[7] - pix2[7]];
  220. s += sq[pix1[8] - pix2[8]];
  221. s += sq[pix1[9] - pix2[9]];
  222. s += sq[pix1[10] - pix2[10]];
  223. s += sq[pix1[11] - pix2[11]];
  224. s += sq[pix1[12] - pix2[12]];
  225. s += sq[pix1[13] - pix2[13]];
  226. s += sq[pix1[14] - pix2[14]];
  227. s += sq[pix1[15] - pix2[15]];
  228. pix1 += line_size;
  229. pix2 += line_size;
  230. }
  231. return s;
  232. }
  233. static void diff_pixels_c(int16_t *restrict block, const uint8_t *s1,
  234. const uint8_t *s2, int stride)
  235. {
  236. int i;
  237. /* read the pixels */
  238. for (i = 0; i < 8; i++) {
  239. block[0] = s1[0] - s2[0];
  240. block[1] = s1[1] - s2[1];
  241. block[2] = s1[2] - s2[2];
  242. block[3] = s1[3] - s2[3];
  243. block[4] = s1[4] - s2[4];
  244. block[5] = s1[5] - s2[5];
  245. block[6] = s1[6] - s2[6];
  246. block[7] = s1[7] - s2[7];
  247. s1 += stride;
  248. s2 += stride;
  249. block += 8;
  250. }
  251. }
  252. static void put_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
  253. int line_size)
  254. {
  255. int i;
  256. /* read the pixels */
  257. for (i = 0; i < 8; i++) {
  258. pixels[0] = av_clip_uint8(block[0]);
  259. pixels[1] = av_clip_uint8(block[1]);
  260. pixels[2] = av_clip_uint8(block[2]);
  261. pixels[3] = av_clip_uint8(block[3]);
  262. pixels[4] = av_clip_uint8(block[4]);
  263. pixels[5] = av_clip_uint8(block[5]);
  264. pixels[6] = av_clip_uint8(block[6]);
  265. pixels[7] = av_clip_uint8(block[7]);
  266. pixels += line_size;
  267. block += 8;
  268. }
  269. }
  270. static void put_signed_pixels_clamped_c(const int16_t *block,
  271. uint8_t *restrict pixels,
  272. int line_size)
  273. {
  274. int i, j;
  275. for (i = 0; i < 8; i++) {
  276. for (j = 0; j < 8; j++) {
  277. if (*block < -128)
  278. *pixels = 0;
  279. else if (*block > 127)
  280. *pixels = 255;
  281. else
  282. *pixels = (uint8_t) (*block + 128);
  283. block++;
  284. pixels++;
  285. }
  286. pixels += (line_size - 8);
  287. }
  288. }
  289. static void add_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
  290. int line_size)
  291. {
  292. int i;
  293. /* read the pixels */
  294. for (i = 0; i < 8; i++) {
  295. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  296. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  297. pixels[2] = av_clip_uint8(pixels[2] + block[2]);
  298. pixels[3] = av_clip_uint8(pixels[3] + block[3]);
  299. pixels[4] = av_clip_uint8(pixels[4] + block[4]);
  300. pixels[5] = av_clip_uint8(pixels[5] + block[5]);
  301. pixels[6] = av_clip_uint8(pixels[6] + block[6]);
  302. pixels[7] = av_clip_uint8(pixels[7] + block[7]);
  303. pixels += line_size;
  304. block += 8;
  305. }
  306. }
  307. static int sum_abs_dctelem_c(int16_t *block)
  308. {
  309. int sum = 0, i;
  310. for (i = 0; i < 64; i++)
  311. sum += FFABS(block[i]);
  312. return sum;
  313. }
  314. #define avg2(a, b) ((a + b + 1) >> 1)
  315. #define avg4(a, b, c, d) ((a + b + c + d + 2) >> 2)
  316. static inline int pix_abs16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  317. int line_size, int h)
  318. {
  319. int s = 0, i;
  320. for (i = 0; i < h; i++) {
  321. s += abs(pix1[0] - pix2[0]);
  322. s += abs(pix1[1] - pix2[1]);
  323. s += abs(pix1[2] - pix2[2]);
  324. s += abs(pix1[3] - pix2[3]);
  325. s += abs(pix1[4] - pix2[4]);
  326. s += abs(pix1[5] - pix2[5]);
  327. s += abs(pix1[6] - pix2[6]);
  328. s += abs(pix1[7] - pix2[7]);
  329. s += abs(pix1[8] - pix2[8]);
  330. s += abs(pix1[9] - pix2[9]);
  331. s += abs(pix1[10] - pix2[10]);
  332. s += abs(pix1[11] - pix2[11]);
  333. s += abs(pix1[12] - pix2[12]);
  334. s += abs(pix1[13] - pix2[13]);
  335. s += abs(pix1[14] - pix2[14]);
  336. s += abs(pix1[15] - pix2[15]);
  337. pix1 += line_size;
  338. pix2 += line_size;
  339. }
  340. return s;
  341. }
  342. static int pix_abs16_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  343. int line_size, int h)
  344. {
  345. int s = 0, i;
  346. for (i = 0; i < h; i++) {
  347. s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
  348. s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
  349. s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
  350. s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
  351. s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
  352. s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
  353. s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
  354. s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
  355. s += abs(pix1[8] - avg2(pix2[8], pix2[9]));
  356. s += abs(pix1[9] - avg2(pix2[9], pix2[10]));
  357. s += abs(pix1[10] - avg2(pix2[10], pix2[11]));
  358. s += abs(pix1[11] - avg2(pix2[11], pix2[12]));
  359. s += abs(pix1[12] - avg2(pix2[12], pix2[13]));
  360. s += abs(pix1[13] - avg2(pix2[13], pix2[14]));
  361. s += abs(pix1[14] - avg2(pix2[14], pix2[15]));
  362. s += abs(pix1[15] - avg2(pix2[15], pix2[16]));
  363. pix1 += line_size;
  364. pix2 += line_size;
  365. }
  366. return s;
  367. }
  368. static int pix_abs16_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  369. int line_size, int h)
  370. {
  371. int s = 0, i;
  372. uint8_t *pix3 = pix2 + line_size;
  373. for (i = 0; i < h; i++) {
  374. s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
  375. s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
  376. s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
  377. s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
  378. s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
  379. s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
  380. s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
  381. s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
  382. s += abs(pix1[8] - avg2(pix2[8], pix3[8]));
  383. s += abs(pix1[9] - avg2(pix2[9], pix3[9]));
  384. s += abs(pix1[10] - avg2(pix2[10], pix3[10]));
  385. s += abs(pix1[11] - avg2(pix2[11], pix3[11]));
  386. s += abs(pix1[12] - avg2(pix2[12], pix3[12]));
  387. s += abs(pix1[13] - avg2(pix2[13], pix3[13]));
  388. s += abs(pix1[14] - avg2(pix2[14], pix3[14]));
  389. s += abs(pix1[15] - avg2(pix2[15], pix3[15]));
  390. pix1 += line_size;
  391. pix2 += line_size;
  392. pix3 += line_size;
  393. }
  394. return s;
  395. }
  396. static int pix_abs16_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  397. int line_size, int h)
  398. {
  399. int s = 0, i;
  400. uint8_t *pix3 = pix2 + line_size;
  401. for (i = 0; i < h; i++) {
  402. s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
  403. s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
  404. s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
  405. s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
  406. s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
  407. s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
  408. s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
  409. s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
  410. s += abs(pix1[8] - avg4(pix2[8], pix2[9], pix3[8], pix3[9]));
  411. s += abs(pix1[9] - avg4(pix2[9], pix2[10], pix3[9], pix3[10]));
  412. s += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
  413. s += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
  414. s += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
  415. s += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
  416. s += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
  417. s += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
  418. pix1 += line_size;
  419. pix2 += line_size;
  420. pix3 += line_size;
  421. }
  422. return s;
  423. }
  424. static inline int pix_abs8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  425. int line_size, int h)
  426. {
  427. int s = 0, i;
  428. for (i = 0; i < h; i++) {
  429. s += abs(pix1[0] - pix2[0]);
  430. s += abs(pix1[1] - pix2[1]);
  431. s += abs(pix1[2] - pix2[2]);
  432. s += abs(pix1[3] - pix2[3]);
  433. s += abs(pix1[4] - pix2[4]);
  434. s += abs(pix1[5] - pix2[5]);
  435. s += abs(pix1[6] - pix2[6]);
  436. s += abs(pix1[7] - pix2[7]);
  437. pix1 += line_size;
  438. pix2 += line_size;
  439. }
  440. return s;
  441. }
  442. static int pix_abs8_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  443. int line_size, int h)
  444. {
  445. int s = 0, i;
  446. for (i = 0; i < h; i++) {
  447. s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
  448. s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
  449. s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
  450. s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
  451. s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
  452. s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
  453. s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
  454. s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
  455. pix1 += line_size;
  456. pix2 += line_size;
  457. }
  458. return s;
  459. }
  460. static int pix_abs8_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  461. int line_size, int h)
  462. {
  463. int s = 0, i;
  464. uint8_t *pix3 = pix2 + line_size;
  465. for (i = 0; i < h; i++) {
  466. s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
  467. s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
  468. s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
  469. s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
  470. s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
  471. s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
  472. s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
  473. s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
  474. pix1 += line_size;
  475. pix2 += line_size;
  476. pix3 += line_size;
  477. }
  478. return s;
  479. }
  480. static int pix_abs8_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  481. int line_size, int h)
  482. {
  483. int s = 0, i;
  484. uint8_t *pix3 = pix2 + line_size;
  485. for (i = 0; i < h; i++) {
  486. s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
  487. s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
  488. s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
  489. s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
  490. s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
  491. s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
  492. s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
  493. s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
  494. pix1 += line_size;
  495. pix2 += line_size;
  496. pix3 += line_size;
  497. }
  498. return s;
  499. }
  500. static int nsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
  501. {
  502. int score1 = 0, score2 = 0, x, y;
  503. for (y = 0; y < h; y++) {
  504. for (x = 0; x < 16; x++)
  505. score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
  506. if (y + 1 < h) {
  507. for (x = 0; x < 15; x++)
  508. score2 += FFABS(s1[x] - s1[x + stride] -
  509. s1[x + 1] + s1[x + stride + 1]) -
  510. FFABS(s2[x] - s2[x + stride] -
  511. s2[x + 1] + s2[x + stride + 1]);
  512. }
  513. s1 += stride;
  514. s2 += stride;
  515. }
  516. if (c)
  517. return score1 + FFABS(score2) * c->avctx->nsse_weight;
  518. else
  519. return score1 + FFABS(score2) * 8;
  520. }
  521. static int nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
  522. {
  523. int score1 = 0, score2 = 0, x, y;
  524. for (y = 0; y < h; y++) {
  525. for (x = 0; x < 8; x++)
  526. score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
  527. if (y + 1 < h) {
  528. for (x = 0; x < 7; x++)
  529. score2 += FFABS(s1[x] - s1[x + stride] -
  530. s1[x + 1] + s1[x + stride + 1]) -
  531. FFABS(s2[x] - s2[x + stride] -
  532. s2[x + 1] + s2[x + stride + 1]);
  533. }
  534. s1 += stride;
  535. s2 += stride;
  536. }
  537. if (c)
  538. return score1 + FFABS(score2) * c->avctx->nsse_weight;
  539. else
  540. return score1 + FFABS(score2) * 8;
  541. }
  542. static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
  543. int16_t basis[64], int scale)
  544. {
  545. int i;
  546. unsigned int sum = 0;
  547. for (i = 0; i < 8 * 8; i++) {
  548. int b = rem[i] + ((basis[i] * scale +
  549. (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
  550. (BASIS_SHIFT - RECON_SHIFT));
  551. int w = weight[i];
  552. b >>= RECON_SHIFT;
  553. assert(-512 < b && b < 512);
  554. sum += (w * b) * (w * b) >> 4;
  555. }
  556. return sum >> 2;
  557. }
  558. static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
  559. {
  560. int i;
  561. for (i = 0; i < 8 * 8; i++)
  562. rem[i] += (basis[i] * scale +
  563. (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
  564. (BASIS_SHIFT - RECON_SHIFT);
  565. }
  566. static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
  567. int stride, int h)
  568. {
  569. return 0;
  570. }
  571. void ff_set_cmp(DSPContext *c, me_cmp_func *cmp, int type)
  572. {
  573. int i;
  574. memset(cmp, 0, sizeof(void *) * 6);
  575. for (i = 0; i < 6; i++) {
  576. switch (type & 0xFF) {
  577. case FF_CMP_SAD:
  578. cmp[i] = c->sad[i];
  579. break;
  580. case FF_CMP_SATD:
  581. cmp[i] = c->hadamard8_diff[i];
  582. break;
  583. case FF_CMP_SSE:
  584. cmp[i] = c->sse[i];
  585. break;
  586. case FF_CMP_DCT:
  587. cmp[i] = c->dct_sad[i];
  588. break;
  589. case FF_CMP_DCT264:
  590. cmp[i] = c->dct264_sad[i];
  591. break;
  592. case FF_CMP_DCTMAX:
  593. cmp[i] = c->dct_max[i];
  594. break;
  595. case FF_CMP_PSNR:
  596. cmp[i] = c->quant_psnr[i];
  597. break;
  598. case FF_CMP_BIT:
  599. cmp[i] = c->bit[i];
  600. break;
  601. case FF_CMP_RD:
  602. cmp[i] = c->rd[i];
  603. break;
  604. case FF_CMP_VSAD:
  605. cmp[i] = c->vsad[i];
  606. break;
  607. case FF_CMP_VSSE:
  608. cmp[i] = c->vsse[i];
  609. break;
  610. case FF_CMP_ZERO:
  611. cmp[i] = zero_cmp;
  612. break;
  613. case FF_CMP_NSSE:
  614. cmp[i] = c->nsse[i];
  615. break;
  616. default:
  617. av_log(NULL, AV_LOG_ERROR,
  618. "internal error in cmp function selection\n");
  619. }
  620. }
  621. }
  622. #define BUTTERFLY2(o1, o2, i1, i2) \
  623. o1 = (i1) + (i2); \
  624. o2 = (i1) - (i2);
  625. #define BUTTERFLY1(x, y) \
  626. { \
  627. int a, b; \
  628. a = x; \
  629. b = y; \
  630. x = a + b; \
  631. y = a - b; \
  632. }
  633. #define BUTTERFLYA(x, y) (FFABS((x) + (y)) + FFABS((x) - (y)))
  634. static int hadamard8_diff8x8_c(MpegEncContext *s, uint8_t *dst,
  635. uint8_t *src, int stride, int h)
  636. {
  637. int i, temp[64], sum = 0;
  638. assert(h == 8);
  639. for (i = 0; i < 8; i++) {
  640. // FIXME: try pointer walks
  641. BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
  642. src[stride * i + 0] - dst[stride * i + 0],
  643. src[stride * i + 1] - dst[stride * i + 1]);
  644. BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
  645. src[stride * i + 2] - dst[stride * i + 2],
  646. src[stride * i + 3] - dst[stride * i + 3]);
  647. BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
  648. src[stride * i + 4] - dst[stride * i + 4],
  649. src[stride * i + 5] - dst[stride * i + 5]);
  650. BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
  651. src[stride * i + 6] - dst[stride * i + 6],
  652. src[stride * i + 7] - dst[stride * i + 7]);
  653. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
  654. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
  655. BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
  656. BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
  657. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
  658. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
  659. BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
  660. BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
  661. }
  662. for (i = 0; i < 8; i++) {
  663. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
  664. BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
  665. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
  666. BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
  667. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
  668. BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
  669. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
  670. BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
  671. sum += BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i]) +
  672. BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i]) +
  673. BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i]) +
  674. BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
  675. }
  676. return sum;
  677. }
  678. static int hadamard8_intra8x8_c(MpegEncContext *s, uint8_t *src,
  679. uint8_t *dummy, int stride, int h)
  680. {
  681. int i, temp[64], sum = 0;
  682. assert(h == 8);
  683. for (i = 0; i < 8; i++) {
  684. // FIXME: try pointer walks
  685. BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
  686. src[stride * i + 0], src[stride * i + 1]);
  687. BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
  688. src[stride * i + 2], src[stride * i + 3]);
  689. BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
  690. src[stride * i + 4], src[stride * i + 5]);
  691. BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
  692. src[stride * i + 6], src[stride * i + 7]);
  693. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
  694. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
  695. BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
  696. BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
  697. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
  698. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
  699. BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
  700. BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
  701. }
  702. for (i = 0; i < 8; i++) {
  703. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
  704. BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
  705. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
  706. BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
  707. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
  708. BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
  709. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
  710. BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
  711. sum +=
  712. BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i])
  713. + BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i])
  714. + BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i])
  715. + BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
  716. }
  717. sum -= FFABS(temp[8 * 0] + temp[8 * 4]); // -mean
  718. return sum;
  719. }
  720. static int dct_sad8x8_c(MpegEncContext *s, uint8_t *src1,
  721. uint8_t *src2, int stride, int h)
  722. {
  723. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  724. assert(h == 8);
  725. s->dsp.diff_pixels(temp, src1, src2, stride);
  726. s->dsp.fdct(temp);
  727. return s->dsp.sum_abs_dctelem(temp);
  728. }
  729. #if CONFIG_GPL
  730. #define DCT8_1D \
  731. { \
  732. const int s07 = SRC(0) + SRC(7); \
  733. const int s16 = SRC(1) + SRC(6); \
  734. const int s25 = SRC(2) + SRC(5); \
  735. const int s34 = SRC(3) + SRC(4); \
  736. const int a0 = s07 + s34; \
  737. const int a1 = s16 + s25; \
  738. const int a2 = s07 - s34; \
  739. const int a3 = s16 - s25; \
  740. const int d07 = SRC(0) - SRC(7); \
  741. const int d16 = SRC(1) - SRC(6); \
  742. const int d25 = SRC(2) - SRC(5); \
  743. const int d34 = SRC(3) - SRC(4); \
  744. const int a4 = d16 + d25 + (d07 + (d07 >> 1)); \
  745. const int a5 = d07 - d34 - (d25 + (d25 >> 1)); \
  746. const int a6 = d07 + d34 - (d16 + (d16 >> 1)); \
  747. const int a7 = d16 - d25 + (d34 + (d34 >> 1)); \
  748. DST(0, a0 + a1); \
  749. DST(1, a4 + (a7 >> 2)); \
  750. DST(2, a2 + (a3 >> 1)); \
  751. DST(3, a5 + (a6 >> 2)); \
  752. DST(4, a0 - a1); \
  753. DST(5, a6 - (a5 >> 2)); \
  754. DST(6, (a2 >> 1) - a3); \
  755. DST(7, (a4 >> 2) - a7); \
  756. }
  757. static int dct264_sad8x8_c(MpegEncContext *s, uint8_t *src1,
  758. uint8_t *src2, int stride, int h)
  759. {
  760. int16_t dct[8][8];
  761. int i, sum = 0;
  762. s->dsp.diff_pixels(dct[0], src1, src2, stride);
  763. #define SRC(x) dct[i][x]
  764. #define DST(x, v) dct[i][x] = v
  765. for (i = 0; i < 8; i++)
  766. DCT8_1D
  767. #undef SRC
  768. #undef DST
  769. #define SRC(x) dct[x][i]
  770. #define DST(x, v) sum += FFABS(v)
  771. for (i = 0; i < 8; i++)
  772. DCT8_1D
  773. #undef SRC
  774. #undef DST
  775. return sum;
  776. }
  777. #endif
  778. static int dct_max8x8_c(MpegEncContext *s, uint8_t *src1,
  779. uint8_t *src2, int stride, int h)
  780. {
  781. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  782. int sum = 0, i;
  783. assert(h == 8);
  784. s->dsp.diff_pixels(temp, src1, src2, stride);
  785. s->dsp.fdct(temp);
  786. for (i = 0; i < 64; i++)
  787. sum = FFMAX(sum, FFABS(temp[i]));
  788. return sum;
  789. }
  790. static int quant_psnr8x8_c(MpegEncContext *s, uint8_t *src1,
  791. uint8_t *src2, int stride, int h)
  792. {
  793. LOCAL_ALIGNED_16(int16_t, temp, [64 * 2]);
  794. int16_t *const bak = temp + 64;
  795. int sum = 0, i;
  796. assert(h == 8);
  797. s->mb_intra = 0;
  798. s->dsp.diff_pixels(temp, src1, src2, stride);
  799. memcpy(bak, temp, 64 * sizeof(int16_t));
  800. s->block_last_index[0 /* FIXME */] =
  801. s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
  802. s->dct_unquantize_inter(s, temp, 0, s->qscale);
  803. ff_simple_idct_8(temp); // FIXME
  804. for (i = 0; i < 64; i++)
  805. sum += (temp[i] - bak[i]) * (temp[i] - bak[i]);
  806. return sum;
  807. }
  808. static int rd8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
  809. int stride, int h)
  810. {
  811. const uint8_t *scantable = s->intra_scantable.permutated;
  812. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  813. LOCAL_ALIGNED_16(uint8_t, lsrc1, [64]);
  814. LOCAL_ALIGNED_16(uint8_t, lsrc2, [64]);
  815. int i, last, run, bits, level, distortion, start_i;
  816. const int esc_length = s->ac_esc_length;
  817. uint8_t *length, *last_length;
  818. assert(h == 8);
  819. copy_block8(lsrc1, src1, 8, stride, 8);
  820. copy_block8(lsrc2, src2, 8, stride, 8);
  821. s->dsp.diff_pixels(temp, lsrc1, lsrc2, 8);
  822. s->block_last_index[0 /* FIXME */] =
  823. last =
  824. s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
  825. bits = 0;
  826. if (s->mb_intra) {
  827. start_i = 1;
  828. length = s->intra_ac_vlc_length;
  829. last_length = s->intra_ac_vlc_last_length;
  830. bits += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
  831. } else {
  832. start_i = 0;
  833. length = s->inter_ac_vlc_length;
  834. last_length = s->inter_ac_vlc_last_length;
  835. }
  836. if (last >= start_i) {
  837. run = 0;
  838. for (i = start_i; i < last; i++) {
  839. int j = scantable[i];
  840. level = temp[j];
  841. if (level) {
  842. level += 64;
  843. if ((level & (~127)) == 0)
  844. bits += length[UNI_AC_ENC_INDEX(run, level)];
  845. else
  846. bits += esc_length;
  847. run = 0;
  848. } else
  849. run++;
  850. }
  851. i = scantable[last];
  852. level = temp[i] + 64;
  853. assert(level - 64);
  854. if ((level & (~127)) == 0) {
  855. bits += last_length[UNI_AC_ENC_INDEX(run, level)];
  856. } else
  857. bits += esc_length;
  858. }
  859. if (last >= 0) {
  860. if (s->mb_intra)
  861. s->dct_unquantize_intra(s, temp, 0, s->qscale);
  862. else
  863. s->dct_unquantize_inter(s, temp, 0, s->qscale);
  864. }
  865. s->dsp.idct_add(lsrc2, 8, temp);
  866. distortion = s->dsp.sse[1](NULL, lsrc2, lsrc1, 8, 8);
  867. return distortion + ((bits * s->qscale * s->qscale * 109 + 64) >> 7);
  868. }
  869. static int bit8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
  870. int stride, int h)
  871. {
  872. const uint8_t *scantable = s->intra_scantable.permutated;
  873. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  874. int i, last, run, bits, level, start_i;
  875. const int esc_length = s->ac_esc_length;
  876. uint8_t *length, *last_length;
  877. assert(h == 8);
  878. s->dsp.diff_pixels(temp, src1, src2, stride);
  879. s->block_last_index[0 /* FIXME */] =
  880. last =
  881. s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
  882. bits = 0;
  883. if (s->mb_intra) {
  884. start_i = 1;
  885. length = s->intra_ac_vlc_length;
  886. last_length = s->intra_ac_vlc_last_length;
  887. bits += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
  888. } else {
  889. start_i = 0;
  890. length = s->inter_ac_vlc_length;
  891. last_length = s->inter_ac_vlc_last_length;
  892. }
  893. if (last >= start_i) {
  894. run = 0;
  895. for (i = start_i; i < last; i++) {
  896. int j = scantable[i];
  897. level = temp[j];
  898. if (level) {
  899. level += 64;
  900. if ((level & (~127)) == 0)
  901. bits += length[UNI_AC_ENC_INDEX(run, level)];
  902. else
  903. bits += esc_length;
  904. run = 0;
  905. } else
  906. run++;
  907. }
  908. i = scantable[last];
  909. level = temp[i] + 64;
  910. assert(level - 64);
  911. if ((level & (~127)) == 0)
  912. bits += last_length[UNI_AC_ENC_INDEX(run, level)];
  913. else
  914. bits += esc_length;
  915. }
  916. return bits;
  917. }
  918. #define VSAD_INTRA(size) \
  919. static int vsad_intra ## size ## _c(MpegEncContext *c, \
  920. uint8_t *s, uint8_t *dummy, \
  921. int stride, int h) \
  922. { \
  923. int score = 0, x, y; \
  924. \
  925. for (y = 1; y < h; y++) { \
  926. for (x = 0; x < size; x += 4) { \
  927. score += FFABS(s[x] - s[x + stride]) + \
  928. FFABS(s[x + 1] - s[x + stride + 1]) + \
  929. FFABS(s[x + 2] - s[x + 2 + stride]) + \
  930. FFABS(s[x + 3] - s[x + 3 + stride]); \
  931. } \
  932. s += stride; \
  933. } \
  934. \
  935. return score; \
  936. }
  937. VSAD_INTRA(8)
  938. VSAD_INTRA(16)
  939. static int vsad16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,
  940. int stride, int h)
  941. {
  942. int score = 0, x, y;
  943. for (y = 1; y < h; y++) {
  944. for (x = 0; x < 16; x++)
  945. score += FFABS(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);
  946. s1 += stride;
  947. s2 += stride;
  948. }
  949. return score;
  950. }
  951. #define SQ(a) ((a) * (a))
  952. #define VSSE_INTRA(size) \
  953. static int vsse_intra ## size ## _c(MpegEncContext *c, \
  954. uint8_t *s, uint8_t *dummy, \
  955. int stride, int h) \
  956. { \
  957. int score = 0, x, y; \
  958. \
  959. for (y = 1; y < h; y++) { \
  960. for (x = 0; x < size; x += 4) { \
  961. score += SQ(s[x] - s[x + stride]) + \
  962. SQ(s[x + 1] - s[x + stride + 1]) + \
  963. SQ(s[x + 2] - s[x + stride + 2]) + \
  964. SQ(s[x + 3] - s[x + stride + 3]); \
  965. } \
  966. s += stride; \
  967. } \
  968. \
  969. return score; \
  970. }
  971. VSSE_INTRA(8)
  972. VSSE_INTRA(16)
  973. static int vsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,
  974. int stride, int h)
  975. {
  976. int score = 0, x, y;
  977. for (y = 1; y < h; y++) {
  978. for (x = 0; x < 16; x++)
  979. score += SQ(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);
  980. s1 += stride;
  981. s2 += stride;
  982. }
  983. return score;
  984. }
  985. #define WRAPPER8_16_SQ(name8, name16) \
  986. static int name16(MpegEncContext *s, uint8_t *dst, uint8_t *src, \
  987. int stride, int h) \
  988. { \
  989. int score = 0; \
  990. \
  991. score += name8(s, dst, src, stride, 8); \
  992. score += name8(s, dst + 8, src + 8, stride, 8); \
  993. if (h == 16) { \
  994. dst += 8 * stride; \
  995. src += 8 * stride; \
  996. score += name8(s, dst, src, stride, 8); \
  997. score += name8(s, dst + 8, src + 8, stride, 8); \
  998. } \
  999. return score; \
  1000. }
  1001. WRAPPER8_16_SQ(hadamard8_diff8x8_c, hadamard8_diff16_c)
  1002. WRAPPER8_16_SQ(hadamard8_intra8x8_c, hadamard8_intra16_c)
  1003. WRAPPER8_16_SQ(dct_sad8x8_c, dct_sad16_c)
  1004. #if CONFIG_GPL
  1005. WRAPPER8_16_SQ(dct264_sad8x8_c, dct264_sad16_c)
  1006. #endif
  1007. WRAPPER8_16_SQ(dct_max8x8_c, dct_max16_c)
  1008. WRAPPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c)
  1009. WRAPPER8_16_SQ(rd8x8_c, rd16_c)
  1010. WRAPPER8_16_SQ(bit8x8_c, bit16_c)
  1011. static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
  1012. {
  1013. ff_j_rev_dct(block);
  1014. put_pixels_clamped_c(block, dest, line_size);
  1015. }
  1016. static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
  1017. {
  1018. ff_j_rev_dct(block);
  1019. add_pixels_clamped_c(block, dest, line_size);
  1020. }
  1021. /* draw the edges of width 'w' of an image of size width, height */
  1022. // FIXME: Check that this is OK for MPEG-4 interlaced.
  1023. static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
  1024. int w, int h, int sides)
  1025. {
  1026. uint8_t *ptr = buf, *last_line;
  1027. int i;
  1028. /* left and right */
  1029. for (i = 0; i < height; i++) {
  1030. memset(ptr - w, ptr[0], w);
  1031. memset(ptr + width, ptr[width - 1], w);
  1032. ptr += wrap;
  1033. }
  1034. /* top and bottom + corners */
  1035. buf -= w;
  1036. last_line = buf + (height - 1) * wrap;
  1037. if (sides & EDGE_TOP)
  1038. for (i = 0; i < h; i++)
  1039. // top
  1040. memcpy(buf - (i + 1) * wrap, buf, width + w + w);
  1041. if (sides & EDGE_BOTTOM)
  1042. for (i = 0; i < h; i++)
  1043. // bottom
  1044. memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
  1045. }
  1046. /* init static data */
  1047. av_cold void ff_dsputil_static_init(void)
  1048. {
  1049. int i;
  1050. for (i = 0; i < 512; i++)
  1051. ff_square_tab[i] = (i - 256) * (i - 256);
  1052. }
  1053. av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
  1054. {
  1055. const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
  1056. #if CONFIG_ENCODERS
  1057. if (avctx->bits_per_raw_sample == 10) {
  1058. c->fdct = ff_jpeg_fdct_islow_10;
  1059. c->fdct248 = ff_fdct248_islow_10;
  1060. } else {
  1061. if (avctx->dct_algo == FF_DCT_FASTINT) {
  1062. c->fdct = ff_fdct_ifast;
  1063. c->fdct248 = ff_fdct_ifast248;
  1064. } else if (avctx->dct_algo == FF_DCT_FAAN) {
  1065. c->fdct = ff_faandct;
  1066. c->fdct248 = ff_faandct248;
  1067. } else {
  1068. c->fdct = ff_jpeg_fdct_islow_8; // slow/accurate/default
  1069. c->fdct248 = ff_fdct248_islow_8;
  1070. }
  1071. }
  1072. #endif /* CONFIG_ENCODERS */
  1073. if (avctx->bits_per_raw_sample == 10) {
  1074. c->idct_put = ff_simple_idct_put_10;
  1075. c->idct_add = ff_simple_idct_add_10;
  1076. c->idct = ff_simple_idct_10;
  1077. c->idct_permutation_type = FF_NO_IDCT_PERM;
  1078. } else {
  1079. if (avctx->idct_algo == FF_IDCT_INT) {
  1080. c->idct_put = jref_idct_put;
  1081. c->idct_add = jref_idct_add;
  1082. c->idct = ff_j_rev_dct;
  1083. c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
  1084. } else if (avctx->idct_algo == FF_IDCT_FAAN) {
  1085. c->idct_put = ff_faanidct_put;
  1086. c->idct_add = ff_faanidct_add;
  1087. c->idct = ff_faanidct;
  1088. c->idct_permutation_type = FF_NO_IDCT_PERM;
  1089. } else { // accurate/default
  1090. c->idct_put = ff_simple_idct_put_8;
  1091. c->idct_add = ff_simple_idct_add_8;
  1092. c->idct = ff_simple_idct_8;
  1093. c->idct_permutation_type = FF_NO_IDCT_PERM;
  1094. }
  1095. }
  1096. c->diff_pixels = diff_pixels_c;
  1097. c->put_pixels_clamped = put_pixels_clamped_c;
  1098. c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
  1099. c->add_pixels_clamped = add_pixels_clamped_c;
  1100. c->sum_abs_dctelem = sum_abs_dctelem_c;
  1101. c->pix_sum = pix_sum_c;
  1102. c->pix_norm1 = pix_norm1_c;
  1103. /* TODO [0] 16 [1] 8 */
  1104. c->pix_abs[0][0] = pix_abs16_c;
  1105. c->pix_abs[0][1] = pix_abs16_x2_c;
  1106. c->pix_abs[0][2] = pix_abs16_y2_c;
  1107. c->pix_abs[0][3] = pix_abs16_xy2_c;
  1108. c->pix_abs[1][0] = pix_abs8_c;
  1109. c->pix_abs[1][1] = pix_abs8_x2_c;
  1110. c->pix_abs[1][2] = pix_abs8_y2_c;
  1111. c->pix_abs[1][3] = pix_abs8_xy2_c;
  1112. #define SET_CMP_FUNC(name) \
  1113. c->name[0] = name ## 16_c; \
  1114. c->name[1] = name ## 8x8_c;
  1115. SET_CMP_FUNC(hadamard8_diff)
  1116. c->hadamard8_diff[4] = hadamard8_intra16_c;
  1117. c->hadamard8_diff[5] = hadamard8_intra8x8_c;
  1118. SET_CMP_FUNC(dct_sad)
  1119. SET_CMP_FUNC(dct_max)
  1120. #if CONFIG_GPL
  1121. SET_CMP_FUNC(dct264_sad)
  1122. #endif
  1123. c->sad[0] = pix_abs16_c;
  1124. c->sad[1] = pix_abs8_c;
  1125. c->sse[0] = sse16_c;
  1126. c->sse[1] = sse8_c;
  1127. c->sse[2] = sse4_c;
  1128. SET_CMP_FUNC(quant_psnr)
  1129. SET_CMP_FUNC(rd)
  1130. SET_CMP_FUNC(bit)
  1131. c->vsad[0] = vsad16_c;
  1132. c->vsad[4] = vsad_intra16_c;
  1133. c->vsad[5] = vsad_intra8_c;
  1134. c->vsse[0] = vsse16_c;
  1135. c->vsse[4] = vsse_intra16_c;
  1136. c->vsse[5] = vsse_intra8_c;
  1137. c->nsse[0] = nsse16_c;
  1138. c->nsse[1] = nsse8_c;
  1139. c->try_8x8basis = try_8x8basis_c;
  1140. c->add_8x8basis = add_8x8basis_c;
  1141. c->shrink[0] = av_image_copy_plane;
  1142. c->shrink[1] = ff_shrink22;
  1143. c->shrink[2] = ff_shrink44;
  1144. c->shrink[3] = ff_shrink88;
  1145. c->draw_edges = draw_edges_8_c;
  1146. switch (avctx->bits_per_raw_sample) {
  1147. case 9:
  1148. case 10:
  1149. c->get_pixels = get_pixels_16_c;
  1150. break;
  1151. default:
  1152. c->get_pixels = get_pixels_8_c;
  1153. break;
  1154. }
  1155. if (ARCH_ARM)
  1156. ff_dsputil_init_arm(c, avctx, high_bit_depth);
  1157. if (ARCH_PPC)
  1158. ff_dsputil_init_ppc(c, avctx, high_bit_depth);
  1159. if (ARCH_X86)
  1160. ff_dsputil_init_x86(c, avctx, high_bit_depth);
  1161. ff_init_scantable_permutation(c->idct_permutation,
  1162. c->idct_permutation_type);
  1163. }