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.

1600 lines
53KB

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