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.

1492 lines
49KB

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