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.

1712 lines
56KB

  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 void bswap_buf(uint32_t *dst, const uint32_t *src, int w)
  176. {
  177. int i;
  178. for (i = 0; i + 8 <= w; i += 8) {
  179. dst[i + 0] = av_bswap32(src[i + 0]);
  180. dst[i + 1] = av_bswap32(src[i + 1]);
  181. dst[i + 2] = av_bswap32(src[i + 2]);
  182. dst[i + 3] = av_bswap32(src[i + 3]);
  183. dst[i + 4] = av_bswap32(src[i + 4]);
  184. dst[i + 5] = av_bswap32(src[i + 5]);
  185. dst[i + 6] = av_bswap32(src[i + 6]);
  186. dst[i + 7] = av_bswap32(src[i + 7]);
  187. }
  188. for (; i < w; i++)
  189. dst[i + 0] = av_bswap32(src[i + 0]);
  190. }
  191. static void bswap16_buf(uint16_t *dst, const uint16_t *src, int len)
  192. {
  193. while (len--)
  194. *dst++ = av_bswap16(*src++);
  195. }
  196. static int sse4_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  197. int line_size, int h)
  198. {
  199. int s = 0, i;
  200. uint32_t *sq = ff_square_tab + 256;
  201. for (i = 0; i < h; i++) {
  202. s += sq[pix1[0] - pix2[0]];
  203. s += sq[pix1[1] - pix2[1]];
  204. s += sq[pix1[2] - pix2[2]];
  205. s += sq[pix1[3] - pix2[3]];
  206. pix1 += line_size;
  207. pix2 += line_size;
  208. }
  209. return s;
  210. }
  211. static int sse8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  212. int line_size, int h)
  213. {
  214. int s = 0, i;
  215. uint32_t *sq = ff_square_tab + 256;
  216. for (i = 0; i < h; i++) {
  217. s += sq[pix1[0] - pix2[0]];
  218. s += sq[pix1[1] - pix2[1]];
  219. s += sq[pix1[2] - pix2[2]];
  220. s += sq[pix1[3] - pix2[3]];
  221. s += sq[pix1[4] - pix2[4]];
  222. s += sq[pix1[5] - pix2[5]];
  223. s += sq[pix1[6] - pix2[6]];
  224. s += sq[pix1[7] - pix2[7]];
  225. pix1 += line_size;
  226. pix2 += line_size;
  227. }
  228. return s;
  229. }
  230. static int sse16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  231. int line_size, int h)
  232. {
  233. int s = 0, i;
  234. uint32_t *sq = ff_square_tab + 256;
  235. for (i = 0; i < h; i++) {
  236. s += sq[pix1[0] - pix2[0]];
  237. s += sq[pix1[1] - pix2[1]];
  238. s += sq[pix1[2] - pix2[2]];
  239. s += sq[pix1[3] - pix2[3]];
  240. s += sq[pix1[4] - pix2[4]];
  241. s += sq[pix1[5] - pix2[5]];
  242. s += sq[pix1[6] - pix2[6]];
  243. s += sq[pix1[7] - pix2[7]];
  244. s += sq[pix1[8] - pix2[8]];
  245. s += sq[pix1[9] - pix2[9]];
  246. s += sq[pix1[10] - pix2[10]];
  247. s += sq[pix1[11] - pix2[11]];
  248. s += sq[pix1[12] - pix2[12]];
  249. s += sq[pix1[13] - pix2[13]];
  250. s += sq[pix1[14] - pix2[14]];
  251. s += sq[pix1[15] - pix2[15]];
  252. pix1 += line_size;
  253. pix2 += line_size;
  254. }
  255. return s;
  256. }
  257. static void diff_pixels_c(int16_t *av_restrict block, const uint8_t *s1,
  258. const uint8_t *s2, int stride)
  259. {
  260. int i;
  261. /* read the pixels */
  262. for (i = 0; i < 8; i++) {
  263. block[0] = s1[0] - s2[0];
  264. block[1] = s1[1] - s2[1];
  265. block[2] = s1[2] - s2[2];
  266. block[3] = s1[3] - s2[3];
  267. block[4] = s1[4] - s2[4];
  268. block[5] = s1[5] - s2[5];
  269. block[6] = s1[6] - s2[6];
  270. block[7] = s1[7] - s2[7];
  271. s1 += stride;
  272. s2 += stride;
  273. block += 8;
  274. }
  275. }
  276. static void put_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
  277. int line_size)
  278. {
  279. int i;
  280. /* read the pixels */
  281. for (i = 0; i < 8; i++) {
  282. pixels[0] = av_clip_uint8(block[0]);
  283. pixels[1] = av_clip_uint8(block[1]);
  284. pixels[2] = av_clip_uint8(block[2]);
  285. pixels[3] = av_clip_uint8(block[3]);
  286. pixels[4] = av_clip_uint8(block[4]);
  287. pixels[5] = av_clip_uint8(block[5]);
  288. pixels[6] = av_clip_uint8(block[6]);
  289. pixels[7] = av_clip_uint8(block[7]);
  290. pixels += line_size;
  291. block += 8;
  292. }
  293. }
  294. static void put_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
  295. int line_size)
  296. {
  297. int i;
  298. /* read the pixels */
  299. for(i=0;i<4;i++) {
  300. pixels[0] = av_clip_uint8(block[0]);
  301. pixels[1] = av_clip_uint8(block[1]);
  302. pixels[2] = av_clip_uint8(block[2]);
  303. pixels[3] = av_clip_uint8(block[3]);
  304. pixels += line_size;
  305. block += 8;
  306. }
  307. }
  308. static void put_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
  309. int line_size)
  310. {
  311. int i;
  312. /* read the pixels */
  313. for(i=0;i<2;i++) {
  314. pixels[0] = av_clip_uint8(block[0]);
  315. pixels[1] = av_clip_uint8(block[1]);
  316. pixels += line_size;
  317. block += 8;
  318. }
  319. }
  320. static void put_signed_pixels_clamped_c(const int16_t *block,
  321. uint8_t *av_restrict pixels,
  322. int line_size)
  323. {
  324. int i, j;
  325. for (i = 0; i < 8; i++) {
  326. for (j = 0; j < 8; j++) {
  327. if (*block < -128)
  328. *pixels = 0;
  329. else if (*block > 127)
  330. *pixels = 255;
  331. else
  332. *pixels = (uint8_t) (*block + 128);
  333. block++;
  334. pixels++;
  335. }
  336. pixels += (line_size - 8);
  337. }
  338. }
  339. static void add_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
  340. int line_size)
  341. {
  342. int i;
  343. /* read the pixels */
  344. for (i = 0; i < 8; i++) {
  345. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  346. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  347. pixels[2] = av_clip_uint8(pixels[2] + block[2]);
  348. pixels[3] = av_clip_uint8(pixels[3] + block[3]);
  349. pixels[4] = av_clip_uint8(pixels[4] + block[4]);
  350. pixels[5] = av_clip_uint8(pixels[5] + block[5]);
  351. pixels[6] = av_clip_uint8(pixels[6] + block[6]);
  352. pixels[7] = av_clip_uint8(pixels[7] + block[7]);
  353. pixels += line_size;
  354. block += 8;
  355. }
  356. }
  357. static void add_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
  358. int line_size)
  359. {
  360. int i;
  361. /* read the pixels */
  362. for(i=0;i<4;i++) {
  363. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  364. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  365. pixels[2] = av_clip_uint8(pixels[2] + block[2]);
  366. pixels[3] = av_clip_uint8(pixels[3] + block[3]);
  367. pixels += line_size;
  368. block += 8;
  369. }
  370. }
  371. static void add_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
  372. int line_size)
  373. {
  374. int i;
  375. /* read the pixels */
  376. for(i=0;i<2;i++) {
  377. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  378. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  379. pixels += line_size;
  380. block += 8;
  381. }
  382. }
  383. static int sum_abs_dctelem_c(int16_t *block)
  384. {
  385. int sum = 0, i;
  386. for (i = 0; i < 64; i++)
  387. sum += FFABS(block[i]);
  388. return sum;
  389. }
  390. #define avg2(a, b) ((a + b + 1) >> 1)
  391. #define avg4(a, b, c, d) ((a + b + c + d + 2) >> 2)
  392. static void gmc1_c(uint8_t *dst, uint8_t *src, int stride, int h,
  393. int x16, int y16, int rounder)
  394. {
  395. const int A = (16 - x16) * (16 - y16);
  396. const int B = (x16) * (16 - y16);
  397. const int C = (16 - x16) * (y16);
  398. const int D = (x16) * (y16);
  399. int i;
  400. for (i = 0; i < h; i++) {
  401. dst[0] = (A * src[0] + B * src[1] + C * src[stride + 0] + D * src[stride + 1] + rounder) >> 8;
  402. dst[1] = (A * src[1] + B * src[2] + C * src[stride + 1] + D * src[stride + 2] + rounder) >> 8;
  403. dst[2] = (A * src[2] + B * src[3] + C * src[stride + 2] + D * src[stride + 3] + rounder) >> 8;
  404. dst[3] = (A * src[3] + B * src[4] + C * src[stride + 3] + D * src[stride + 4] + rounder) >> 8;
  405. dst[4] = (A * src[4] + B * src[5] + C * src[stride + 4] + D * src[stride + 5] + rounder) >> 8;
  406. dst[5] = (A * src[5] + B * src[6] + C * src[stride + 5] + D * src[stride + 6] + rounder) >> 8;
  407. dst[6] = (A * src[6] + B * src[7] + C * src[stride + 6] + D * src[stride + 7] + rounder) >> 8;
  408. dst[7] = (A * src[7] + B * src[8] + C * src[stride + 7] + D * src[stride + 8] + rounder) >> 8;
  409. dst += stride;
  410. src += stride;
  411. }
  412. }
  413. void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
  414. int dxx, int dxy, int dyx, int dyy, int shift, int r,
  415. int width, int height)
  416. {
  417. int y, vx, vy;
  418. const int s = 1 << shift;
  419. width--;
  420. height--;
  421. for (y = 0; y < h; y++) {
  422. int x;
  423. vx = ox;
  424. vy = oy;
  425. for (x = 0; x < 8; x++) { // FIXME: optimize
  426. int index;
  427. int src_x = vx >> 16;
  428. int src_y = vy >> 16;
  429. int frac_x = src_x & (s - 1);
  430. int frac_y = src_y & (s - 1);
  431. src_x >>= shift;
  432. src_y >>= shift;
  433. if ((unsigned) src_x < width) {
  434. if ((unsigned) src_y < height) {
  435. index = src_x + src_y * stride;
  436. dst[y * stride + x] =
  437. ((src[index] * (s - frac_x) +
  438. src[index + 1] * frac_x) * (s - frac_y) +
  439. (src[index + stride] * (s - frac_x) +
  440. src[index + stride + 1] * frac_x) * frac_y +
  441. r) >> (shift * 2);
  442. } else {
  443. index = src_x + av_clip(src_y, 0, height) * stride;
  444. dst[y * stride + x] =
  445. ((src[index] * (s - frac_x) +
  446. src[index + 1] * frac_x) * s +
  447. r) >> (shift * 2);
  448. }
  449. } else {
  450. if ((unsigned) src_y < height) {
  451. index = av_clip(src_x, 0, width) + src_y * stride;
  452. dst[y * stride + x] =
  453. ((src[index] * (s - frac_y) +
  454. src[index + stride] * frac_y) * s +
  455. r) >> (shift * 2);
  456. } else {
  457. index = av_clip(src_x, 0, width) +
  458. av_clip(src_y, 0, height) * stride;
  459. dst[y * stride + x] = src[index];
  460. }
  461. }
  462. vx += dxx;
  463. vy += dyx;
  464. }
  465. ox += dxy;
  466. oy += dyy;
  467. }
  468. }
  469. static inline int pix_abs16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  470. int line_size, int h)
  471. {
  472. int s = 0, i;
  473. for (i = 0; i < h; i++) {
  474. s += abs(pix1[0] - pix2[0]);
  475. s += abs(pix1[1] - pix2[1]);
  476. s += abs(pix1[2] - pix2[2]);
  477. s += abs(pix1[3] - pix2[3]);
  478. s += abs(pix1[4] - pix2[4]);
  479. s += abs(pix1[5] - pix2[5]);
  480. s += abs(pix1[6] - pix2[6]);
  481. s += abs(pix1[7] - pix2[7]);
  482. s += abs(pix1[8] - pix2[8]);
  483. s += abs(pix1[9] - pix2[9]);
  484. s += abs(pix1[10] - pix2[10]);
  485. s += abs(pix1[11] - pix2[11]);
  486. s += abs(pix1[12] - pix2[12]);
  487. s += abs(pix1[13] - pix2[13]);
  488. s += abs(pix1[14] - pix2[14]);
  489. s += abs(pix1[15] - pix2[15]);
  490. pix1 += line_size;
  491. pix2 += line_size;
  492. }
  493. return s;
  494. }
  495. static int pix_abs16_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  496. int line_size, int h)
  497. {
  498. int s = 0, i;
  499. for (i = 0; i < h; i++) {
  500. s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
  501. s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
  502. s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
  503. s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
  504. s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
  505. s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
  506. s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
  507. s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
  508. s += abs(pix1[8] - avg2(pix2[8], pix2[9]));
  509. s += abs(pix1[9] - avg2(pix2[9], pix2[10]));
  510. s += abs(pix1[10] - avg2(pix2[10], pix2[11]));
  511. s += abs(pix1[11] - avg2(pix2[11], pix2[12]));
  512. s += abs(pix1[12] - avg2(pix2[12], pix2[13]));
  513. s += abs(pix1[13] - avg2(pix2[13], pix2[14]));
  514. s += abs(pix1[14] - avg2(pix2[14], pix2[15]));
  515. s += abs(pix1[15] - avg2(pix2[15], pix2[16]));
  516. pix1 += line_size;
  517. pix2 += line_size;
  518. }
  519. return s;
  520. }
  521. static int pix_abs16_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  522. int line_size, int h)
  523. {
  524. int s = 0, i;
  525. uint8_t *pix3 = pix2 + line_size;
  526. for (i = 0; i < h; i++) {
  527. s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
  528. s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
  529. s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
  530. s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
  531. s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
  532. s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
  533. s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
  534. s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
  535. s += abs(pix1[8] - avg2(pix2[8], pix3[8]));
  536. s += abs(pix1[9] - avg2(pix2[9], pix3[9]));
  537. s += abs(pix1[10] - avg2(pix2[10], pix3[10]));
  538. s += abs(pix1[11] - avg2(pix2[11], pix3[11]));
  539. s += abs(pix1[12] - avg2(pix2[12], pix3[12]));
  540. s += abs(pix1[13] - avg2(pix2[13], pix3[13]));
  541. s += abs(pix1[14] - avg2(pix2[14], pix3[14]));
  542. s += abs(pix1[15] - avg2(pix2[15], pix3[15]));
  543. pix1 += line_size;
  544. pix2 += line_size;
  545. pix3 += line_size;
  546. }
  547. return s;
  548. }
  549. static int pix_abs16_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  550. int line_size, int h)
  551. {
  552. int s = 0, i;
  553. uint8_t *pix3 = pix2 + line_size;
  554. for (i = 0; i < h; i++) {
  555. s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
  556. s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
  557. s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
  558. s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
  559. s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
  560. s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
  561. s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
  562. s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
  563. s += abs(pix1[8] - avg4(pix2[8], pix2[9], pix3[8], pix3[9]));
  564. s += abs(pix1[9] - avg4(pix2[9], pix2[10], pix3[9], pix3[10]));
  565. s += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
  566. s += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
  567. s += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
  568. s += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
  569. s += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
  570. s += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
  571. pix1 += line_size;
  572. pix2 += line_size;
  573. pix3 += line_size;
  574. }
  575. return s;
  576. }
  577. static inline int pix_abs8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  578. int line_size, int h)
  579. {
  580. int s = 0, i;
  581. for (i = 0; i < h; i++) {
  582. s += abs(pix1[0] - pix2[0]);
  583. s += abs(pix1[1] - pix2[1]);
  584. s += abs(pix1[2] - pix2[2]);
  585. s += abs(pix1[3] - pix2[3]);
  586. s += abs(pix1[4] - pix2[4]);
  587. s += abs(pix1[5] - pix2[5]);
  588. s += abs(pix1[6] - pix2[6]);
  589. s += abs(pix1[7] - pix2[7]);
  590. pix1 += line_size;
  591. pix2 += line_size;
  592. }
  593. return s;
  594. }
  595. static int pix_abs8_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  596. int line_size, int h)
  597. {
  598. int s = 0, i;
  599. for (i = 0; i < h; i++) {
  600. s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
  601. s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
  602. s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
  603. s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
  604. s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
  605. s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
  606. s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
  607. s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
  608. pix1 += line_size;
  609. pix2 += line_size;
  610. }
  611. return s;
  612. }
  613. static int pix_abs8_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  614. int line_size, int h)
  615. {
  616. int s = 0, i;
  617. uint8_t *pix3 = pix2 + line_size;
  618. for (i = 0; i < h; i++) {
  619. s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
  620. s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
  621. s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
  622. s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
  623. s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
  624. s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
  625. s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
  626. s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
  627. pix1 += line_size;
  628. pix2 += line_size;
  629. pix3 += line_size;
  630. }
  631. return s;
  632. }
  633. static int pix_abs8_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  634. int line_size, int h)
  635. {
  636. int s = 0, i;
  637. uint8_t *pix3 = pix2 + line_size;
  638. for (i = 0; i < h; i++) {
  639. s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
  640. s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
  641. s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
  642. s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
  643. s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
  644. s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
  645. s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
  646. s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
  647. pix1 += line_size;
  648. pix2 += line_size;
  649. pix3 += line_size;
  650. }
  651. return s;
  652. }
  653. static int nsse16_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 < 16; x++)
  658. score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
  659. if (y + 1 < h) {
  660. for (x = 0; x < 15; 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 nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
  675. {
  676. int score1 = 0, score2 = 0, x, y;
  677. for (y = 0; y < h; y++) {
  678. for (x = 0; x < 8; x++)
  679. score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
  680. if (y + 1 < h) {
  681. for (x = 0; x < 7; x++)
  682. score2 += FFABS(s1[x] - s1[x + stride] -
  683. s1[x + 1] + s1[x + stride + 1]) -
  684. FFABS(s2[x] - s2[x + stride] -
  685. s2[x + 1] + s2[x + stride + 1]);
  686. }
  687. s1 += stride;
  688. s2 += stride;
  689. }
  690. if (c)
  691. return score1 + FFABS(score2) * c->avctx->nsse_weight;
  692. else
  693. return score1 + FFABS(score2) * 8;
  694. }
  695. static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
  696. int16_t basis[64], int scale)
  697. {
  698. int i;
  699. unsigned int sum = 0;
  700. for (i = 0; i < 8 * 8; i++) {
  701. int b = rem[i] + ((basis[i] * scale +
  702. (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
  703. (BASIS_SHIFT - RECON_SHIFT));
  704. int w = weight[i];
  705. b >>= RECON_SHIFT;
  706. av_assert2(-512 < b && b < 512);
  707. sum += (w * b) * (w * b) >> 4;
  708. }
  709. return sum >> 2;
  710. }
  711. static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
  712. {
  713. int i;
  714. for (i = 0; i < 8 * 8; i++)
  715. rem[i] += (basis[i] * scale +
  716. (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
  717. (BASIS_SHIFT - RECON_SHIFT);
  718. }
  719. static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
  720. int stride, int h)
  721. {
  722. return 0;
  723. }
  724. void ff_set_cmp(DSPContext *c, me_cmp_func *cmp, int type)
  725. {
  726. int i;
  727. memset(cmp, 0, sizeof(void *) * 6);
  728. for (i = 0; i < 6; i++) {
  729. switch (type & 0xFF) {
  730. case FF_CMP_SAD:
  731. cmp[i] = c->sad[i];
  732. break;
  733. case FF_CMP_SATD:
  734. cmp[i] = c->hadamard8_diff[i];
  735. break;
  736. case FF_CMP_SSE:
  737. cmp[i] = c->sse[i];
  738. break;
  739. case FF_CMP_DCT:
  740. cmp[i] = c->dct_sad[i];
  741. break;
  742. case FF_CMP_DCT264:
  743. cmp[i] = c->dct264_sad[i];
  744. break;
  745. case FF_CMP_DCTMAX:
  746. cmp[i] = c->dct_max[i];
  747. break;
  748. case FF_CMP_PSNR:
  749. cmp[i] = c->quant_psnr[i];
  750. break;
  751. case FF_CMP_BIT:
  752. cmp[i] = c->bit[i];
  753. break;
  754. case FF_CMP_RD:
  755. cmp[i] = c->rd[i];
  756. break;
  757. case FF_CMP_VSAD:
  758. cmp[i] = c->vsad[i];
  759. break;
  760. case FF_CMP_VSSE:
  761. cmp[i] = c->vsse[i];
  762. break;
  763. case FF_CMP_ZERO:
  764. cmp[i] = zero_cmp;
  765. break;
  766. case FF_CMP_NSSE:
  767. cmp[i] = c->nsse[i];
  768. break;
  769. #if CONFIG_DWT
  770. case FF_CMP_W53:
  771. cmp[i]= c->w53[i];
  772. break;
  773. case FF_CMP_W97:
  774. cmp[i]= c->w97[i];
  775. break;
  776. #endif
  777. default:
  778. av_log(NULL, AV_LOG_ERROR,
  779. "internal error in cmp function selection\n");
  780. }
  781. }
  782. }
  783. #define BUTTERFLY2(o1, o2, i1, i2) \
  784. o1 = (i1) + (i2); \
  785. o2 = (i1) - (i2);
  786. #define BUTTERFLY1(x, y) \
  787. { \
  788. int a, b; \
  789. a = x; \
  790. b = y; \
  791. x = a + b; \
  792. y = a - b; \
  793. }
  794. #define BUTTERFLYA(x, y) (FFABS((x) + (y)) + FFABS((x) - (y)))
  795. static int hadamard8_diff8x8_c(MpegEncContext *s, uint8_t *dst,
  796. uint8_t *src, int stride, int h)
  797. {
  798. int i, temp[64], sum = 0;
  799. av_assert2(h == 8);
  800. for (i = 0; i < 8; i++) {
  801. // FIXME: try pointer walks
  802. BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
  803. src[stride * i + 0] - dst[stride * i + 0],
  804. src[stride * i + 1] - dst[stride * i + 1]);
  805. BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
  806. src[stride * i + 2] - dst[stride * i + 2],
  807. src[stride * i + 3] - dst[stride * i + 3]);
  808. BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
  809. src[stride * i + 4] - dst[stride * i + 4],
  810. src[stride * i + 5] - dst[stride * i + 5]);
  811. BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
  812. src[stride * i + 6] - dst[stride * i + 6],
  813. src[stride * i + 7] - dst[stride * i + 7]);
  814. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
  815. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
  816. BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
  817. BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
  818. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
  819. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
  820. BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
  821. BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
  822. }
  823. for (i = 0; i < 8; i++) {
  824. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
  825. BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
  826. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
  827. BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
  828. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
  829. BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
  830. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
  831. BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
  832. sum += BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i]) +
  833. BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i]) +
  834. BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i]) +
  835. BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
  836. }
  837. return sum;
  838. }
  839. static int hadamard8_intra8x8_c(MpegEncContext *s, uint8_t *src,
  840. uint8_t *dummy, int stride, int h)
  841. {
  842. int i, temp[64], sum = 0;
  843. av_assert2(h == 8);
  844. for (i = 0; i < 8; i++) {
  845. // FIXME: try pointer walks
  846. BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
  847. src[stride * i + 0], src[stride * i + 1]);
  848. BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
  849. src[stride * i + 2], src[stride * i + 3]);
  850. BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
  851. src[stride * i + 4], src[stride * i + 5]);
  852. BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
  853. src[stride * i + 6], src[stride * i + 7]);
  854. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
  855. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
  856. BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
  857. BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
  858. BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
  859. BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
  860. BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
  861. BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
  862. }
  863. for (i = 0; i < 8; i++) {
  864. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
  865. BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
  866. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
  867. BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
  868. BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
  869. BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
  870. BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
  871. BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
  872. sum +=
  873. BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i])
  874. + BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i])
  875. + BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i])
  876. + BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
  877. }
  878. sum -= FFABS(temp[8 * 0] + temp[8 * 4]); // -mean
  879. return sum;
  880. }
  881. static int dct_sad8x8_c(MpegEncContext *s, uint8_t *src1,
  882. uint8_t *src2, int stride, int h)
  883. {
  884. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  885. av_assert2(h == 8);
  886. s->dsp.diff_pixels(temp, src1, src2, stride);
  887. s->dsp.fdct(temp);
  888. return s->dsp.sum_abs_dctelem(temp);
  889. }
  890. #if CONFIG_GPL
  891. #define DCT8_1D \
  892. { \
  893. const int s07 = SRC(0) + SRC(7); \
  894. const int s16 = SRC(1) + SRC(6); \
  895. const int s25 = SRC(2) + SRC(5); \
  896. const int s34 = SRC(3) + SRC(4); \
  897. const int a0 = s07 + s34; \
  898. const int a1 = s16 + s25; \
  899. const int a2 = s07 - s34; \
  900. const int a3 = s16 - s25; \
  901. const int d07 = SRC(0) - SRC(7); \
  902. const int d16 = SRC(1) - SRC(6); \
  903. const int d25 = SRC(2) - SRC(5); \
  904. const int d34 = SRC(3) - SRC(4); \
  905. const int a4 = d16 + d25 + (d07 + (d07 >> 1)); \
  906. const int a5 = d07 - d34 - (d25 + (d25 >> 1)); \
  907. const int a6 = d07 + d34 - (d16 + (d16 >> 1)); \
  908. const int a7 = d16 - d25 + (d34 + (d34 >> 1)); \
  909. DST(0, a0 + a1); \
  910. DST(1, a4 + (a7 >> 2)); \
  911. DST(2, a2 + (a3 >> 1)); \
  912. DST(3, a5 + (a6 >> 2)); \
  913. DST(4, a0 - a1); \
  914. DST(5, a6 - (a5 >> 2)); \
  915. DST(6, (a2 >> 1) - a3); \
  916. DST(7, (a4 >> 2) - a7); \
  917. }
  918. static int dct264_sad8x8_c(MpegEncContext *s, uint8_t *src1,
  919. uint8_t *src2, int stride, int h)
  920. {
  921. int16_t dct[8][8];
  922. int i, sum = 0;
  923. s->dsp.diff_pixels(dct[0], src1, src2, stride);
  924. #define SRC(x) dct[i][x]
  925. #define DST(x, v) dct[i][x] = v
  926. for (i = 0; i < 8; i++)
  927. DCT8_1D
  928. #undef SRC
  929. #undef DST
  930. #define SRC(x) dct[x][i]
  931. #define DST(x, v) sum += FFABS(v)
  932. for (i = 0; i < 8; i++)
  933. DCT8_1D
  934. #undef SRC
  935. #undef DST
  936. return sum;
  937. }
  938. #endif
  939. static int dct_max8x8_c(MpegEncContext *s, uint8_t *src1,
  940. uint8_t *src2, int stride, int h)
  941. {
  942. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  943. int sum = 0, i;
  944. av_assert2(h == 8);
  945. s->dsp.diff_pixels(temp, src1, src2, stride);
  946. s->dsp.fdct(temp);
  947. for (i = 0; i < 64; i++)
  948. sum = FFMAX(sum, FFABS(temp[i]));
  949. return sum;
  950. }
  951. static int quant_psnr8x8_c(MpegEncContext *s, uint8_t *src1,
  952. uint8_t *src2, int stride, int h)
  953. {
  954. LOCAL_ALIGNED_16(int16_t, temp, [64 * 2]);
  955. int16_t *const bak = temp + 64;
  956. int sum = 0, i;
  957. av_assert2(h == 8);
  958. s->mb_intra = 0;
  959. s->dsp.diff_pixels(temp, src1, src2, stride);
  960. memcpy(bak, temp, 64 * sizeof(int16_t));
  961. s->block_last_index[0 /* FIXME */] =
  962. s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
  963. s->dct_unquantize_inter(s, temp, 0, s->qscale);
  964. ff_simple_idct_8(temp); // FIXME
  965. for (i = 0; i < 64; i++)
  966. sum += (temp[i] - bak[i]) * (temp[i] - bak[i]);
  967. return sum;
  968. }
  969. static int rd8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
  970. int stride, int h)
  971. {
  972. const uint8_t *scantable = s->intra_scantable.permutated;
  973. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  974. LOCAL_ALIGNED_16(uint8_t, lsrc1, [64]);
  975. LOCAL_ALIGNED_16(uint8_t, lsrc2, [64]);
  976. int i, last, run, bits, level, distortion, start_i;
  977. const int esc_length = s->ac_esc_length;
  978. uint8_t *length, *last_length;
  979. av_assert2(h == 8);
  980. copy_block8(lsrc1, src1, 8, stride, 8);
  981. copy_block8(lsrc2, src2, 8, stride, 8);
  982. s->dsp.diff_pixels(temp, lsrc1, lsrc2, 8);
  983. s->block_last_index[0 /* FIXME */] =
  984. last =
  985. s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
  986. bits = 0;
  987. if (s->mb_intra) {
  988. start_i = 1;
  989. length = s->intra_ac_vlc_length;
  990. last_length = s->intra_ac_vlc_last_length;
  991. bits += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
  992. } else {
  993. start_i = 0;
  994. length = s->inter_ac_vlc_length;
  995. last_length = s->inter_ac_vlc_last_length;
  996. }
  997. if (last >= start_i) {
  998. run = 0;
  999. for (i = start_i; i < last; i++) {
  1000. int j = scantable[i];
  1001. level = temp[j];
  1002. if (level) {
  1003. level += 64;
  1004. if ((level & (~127)) == 0)
  1005. bits += length[UNI_AC_ENC_INDEX(run, level)];
  1006. else
  1007. bits += esc_length;
  1008. run = 0;
  1009. } else
  1010. run++;
  1011. }
  1012. i = scantable[last];
  1013. level = temp[i] + 64;
  1014. av_assert2(level - 64);
  1015. if ((level & (~127)) == 0) {
  1016. bits += last_length[UNI_AC_ENC_INDEX(run, level)];
  1017. } else
  1018. bits += esc_length;
  1019. }
  1020. if (last >= 0) {
  1021. if (s->mb_intra)
  1022. s->dct_unquantize_intra(s, temp, 0, s->qscale);
  1023. else
  1024. s->dct_unquantize_inter(s, temp, 0, s->qscale);
  1025. }
  1026. s->dsp.idct_add(lsrc2, 8, temp);
  1027. distortion = s->dsp.sse[1](NULL, lsrc2, lsrc1, 8, 8);
  1028. return distortion + ((bits * s->qscale * s->qscale * 109 + 64) >> 7);
  1029. }
  1030. static int bit8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
  1031. int stride, int h)
  1032. {
  1033. const uint8_t *scantable = s->intra_scantable.permutated;
  1034. LOCAL_ALIGNED_16(int16_t, temp, [64]);
  1035. int i, last, run, bits, level, start_i;
  1036. const int esc_length = s->ac_esc_length;
  1037. uint8_t *length, *last_length;
  1038. av_assert2(h == 8);
  1039. s->dsp.diff_pixels(temp, src1, src2, stride);
  1040. s->block_last_index[0 /* FIXME */] =
  1041. last =
  1042. s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
  1043. bits = 0;
  1044. if (s->mb_intra) {
  1045. start_i = 1;
  1046. length = s->intra_ac_vlc_length;
  1047. last_length = s->intra_ac_vlc_last_length;
  1048. bits += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
  1049. } else {
  1050. start_i = 0;
  1051. length = s->inter_ac_vlc_length;
  1052. last_length = s->inter_ac_vlc_last_length;
  1053. }
  1054. if (last >= start_i) {
  1055. run = 0;
  1056. for (i = start_i; i < last; i++) {
  1057. int j = scantable[i];
  1058. level = temp[j];
  1059. if (level) {
  1060. level += 64;
  1061. if ((level & (~127)) == 0)
  1062. bits += length[UNI_AC_ENC_INDEX(run, level)];
  1063. else
  1064. bits += esc_length;
  1065. run = 0;
  1066. } else
  1067. run++;
  1068. }
  1069. i = scantable[last];
  1070. level = temp[i] + 64;
  1071. av_assert2(level - 64);
  1072. if ((level & (~127)) == 0)
  1073. bits += last_length[UNI_AC_ENC_INDEX(run, level)];
  1074. else
  1075. bits += esc_length;
  1076. }
  1077. return bits;
  1078. }
  1079. #define VSAD_INTRA(size) \
  1080. static int vsad_intra ## size ## _c(MpegEncContext *c, \
  1081. uint8_t *s, uint8_t *dummy, \
  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 += 4) { \
  1088. score += FFABS(s[x] - s[x + stride]) + \
  1089. FFABS(s[x + 1] - s[x + stride + 1]) + \
  1090. FFABS(s[x + 2] - s[x + 2 + stride]) + \
  1091. FFABS(s[x + 3] - s[x + 3 + stride]); \
  1092. } \
  1093. s += stride; \
  1094. } \
  1095. \
  1096. return score; \
  1097. }
  1098. VSAD_INTRA(8)
  1099. VSAD_INTRA(16)
  1100. #define VSAD(size) \
  1101. static int vsad ## size ## _c(MpegEncContext *c, \
  1102. uint8_t *s1, uint8_t *s2, \
  1103. int stride, int h) \
  1104. { \
  1105. int score = 0, x, y; \
  1106. \
  1107. for (y = 1; y < h; y++) { \
  1108. for (x = 0; x < size; x++) \
  1109. score += FFABS(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]); \
  1110. s1 += stride; \
  1111. s2 += stride; \
  1112. } \
  1113. \
  1114. return score; \
  1115. }
  1116. VSAD(8)
  1117. VSAD(16)
  1118. #define SQ(a) ((a) * (a))
  1119. #define VSSE_INTRA(size) \
  1120. static int vsse_intra ## size ## _c(MpegEncContext *c, \
  1121. uint8_t *s, uint8_t *dummy, \
  1122. int stride, int h) \
  1123. { \
  1124. int score = 0, x, y; \
  1125. \
  1126. for (y = 1; y < h; y++) { \
  1127. for (x = 0; x < size; x += 4) { \
  1128. score += SQ(s[x] - s[x + stride]) + \
  1129. SQ(s[x + 1] - s[x + stride + 1]) + \
  1130. SQ(s[x + 2] - s[x + stride + 2]) + \
  1131. SQ(s[x + 3] - s[x + stride + 3]); \
  1132. } \
  1133. s += stride; \
  1134. } \
  1135. \
  1136. return score; \
  1137. }
  1138. VSSE_INTRA(8)
  1139. VSSE_INTRA(16)
  1140. #define VSSE(size) \
  1141. static int vsse ## size ## _c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, \
  1142. int stride, int h) \
  1143. { \
  1144. int score = 0, x, y; \
  1145. \
  1146. for (y = 1; y < h; y++) { \
  1147. for (x = 0; x < size; x++) \
  1148. score += SQ(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]); \
  1149. s1 += stride; \
  1150. s2 += stride; \
  1151. } \
  1152. \
  1153. return score; \
  1154. }
  1155. VSSE(8)
  1156. VSSE(16)
  1157. #define WRAPPER8_16_SQ(name8, name16) \
  1158. static int name16(MpegEncContext *s, uint8_t *dst, uint8_t *src, \
  1159. int stride, int h) \
  1160. { \
  1161. int score = 0; \
  1162. \
  1163. score += name8(s, dst, src, stride, 8); \
  1164. score += name8(s, dst + 8, src + 8, stride, 8); \
  1165. if (h == 16) { \
  1166. dst += 8 * stride; \
  1167. src += 8 * stride; \
  1168. score += name8(s, dst, src, stride, 8); \
  1169. score += name8(s, dst + 8, src + 8, stride, 8); \
  1170. } \
  1171. return score; \
  1172. }
  1173. WRAPPER8_16_SQ(hadamard8_diff8x8_c, hadamard8_diff16_c)
  1174. WRAPPER8_16_SQ(hadamard8_intra8x8_c, hadamard8_intra16_c)
  1175. WRAPPER8_16_SQ(dct_sad8x8_c, dct_sad16_c)
  1176. #if CONFIG_GPL
  1177. WRAPPER8_16_SQ(dct264_sad8x8_c, dct264_sad16_c)
  1178. #endif
  1179. WRAPPER8_16_SQ(dct_max8x8_c, dct_max16_c)
  1180. WRAPPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c)
  1181. WRAPPER8_16_SQ(rd8x8_c, rd16_c)
  1182. WRAPPER8_16_SQ(bit8x8_c, bit16_c)
  1183. static inline uint32_t clipf_c_one(uint32_t a, uint32_t mini,
  1184. uint32_t maxi, uint32_t maxisign)
  1185. {
  1186. if (a > mini)
  1187. return mini;
  1188. else if ((a ^ (1U << 31)) > maxisign)
  1189. return maxi;
  1190. else
  1191. return a;
  1192. }
  1193. static void vector_clipf_c_opposite_sign(float *dst, const float *src,
  1194. float *min, float *max, int len)
  1195. {
  1196. int i;
  1197. uint32_t mini = *(uint32_t *) min;
  1198. uint32_t maxi = *(uint32_t *) max;
  1199. uint32_t maxisign = maxi ^ (1U << 31);
  1200. uint32_t *dsti = (uint32_t *) dst;
  1201. const uint32_t *srci = (const uint32_t *) src;
  1202. for (i = 0; i < len; i += 8) {
  1203. dsti[i + 0] = clipf_c_one(srci[i + 0], mini, maxi, maxisign);
  1204. dsti[i + 1] = clipf_c_one(srci[i + 1], mini, maxi, maxisign);
  1205. dsti[i + 2] = clipf_c_one(srci[i + 2], mini, maxi, maxisign);
  1206. dsti[i + 3] = clipf_c_one(srci[i + 3], mini, maxi, maxisign);
  1207. dsti[i + 4] = clipf_c_one(srci[i + 4], mini, maxi, maxisign);
  1208. dsti[i + 5] = clipf_c_one(srci[i + 5], mini, maxi, maxisign);
  1209. dsti[i + 6] = clipf_c_one(srci[i + 6], mini, maxi, maxisign);
  1210. dsti[i + 7] = clipf_c_one(srci[i + 7], mini, maxi, maxisign);
  1211. }
  1212. }
  1213. static void vector_clipf_c(float *dst, const float *src,
  1214. float min, float max, int len)
  1215. {
  1216. int i;
  1217. if (min < 0 && max > 0) {
  1218. vector_clipf_c_opposite_sign(dst, src, &min, &max, len);
  1219. } else {
  1220. for (i = 0; i < len; i += 8) {
  1221. dst[i] = av_clipf(src[i], min, max);
  1222. dst[i + 1] = av_clipf(src[i + 1], min, max);
  1223. dst[i + 2] = av_clipf(src[i + 2], min, max);
  1224. dst[i + 3] = av_clipf(src[i + 3], min, max);
  1225. dst[i + 4] = av_clipf(src[i + 4], min, max);
  1226. dst[i + 5] = av_clipf(src[i + 5], min, max);
  1227. dst[i + 6] = av_clipf(src[i + 6], min, max);
  1228. dst[i + 7] = av_clipf(src[i + 7], min, max);
  1229. }
  1230. }
  1231. }
  1232. static int32_t scalarproduct_int16_c(const int16_t *v1, const int16_t *v2,
  1233. int order)
  1234. {
  1235. int res = 0;
  1236. while (order--)
  1237. res += *v1++ **v2++;
  1238. return res;
  1239. }
  1240. static void vector_clip_int32_c(int32_t *dst, const int32_t *src, int32_t min,
  1241. int32_t max, unsigned int len)
  1242. {
  1243. do {
  1244. *dst++ = av_clip(*src++, min, max);
  1245. *dst++ = av_clip(*src++, min, max);
  1246. *dst++ = av_clip(*src++, min, max);
  1247. *dst++ = av_clip(*src++, min, max);
  1248. *dst++ = av_clip(*src++, min, max);
  1249. *dst++ = av_clip(*src++, min, max);
  1250. *dst++ = av_clip(*src++, min, max);
  1251. *dst++ = av_clip(*src++, min, max);
  1252. len -= 8;
  1253. } while (len > 0);
  1254. }
  1255. static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
  1256. {
  1257. ff_j_rev_dct(block);
  1258. put_pixels_clamped_c(block, dest, line_size);
  1259. }
  1260. static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
  1261. {
  1262. ff_j_rev_dct(block);
  1263. add_pixels_clamped_c(block, dest, line_size);
  1264. }
  1265. static void ff_jref_idct4_put(uint8_t *dest, int line_size, int16_t *block)
  1266. {
  1267. ff_j_rev_dct4 (block);
  1268. put_pixels_clamped4_c(block, dest, line_size);
  1269. }
  1270. static void ff_jref_idct4_add(uint8_t *dest, int line_size, int16_t *block)
  1271. {
  1272. ff_j_rev_dct4 (block);
  1273. add_pixels_clamped4_c(block, dest, line_size);
  1274. }
  1275. static void ff_jref_idct2_put(uint8_t *dest, int line_size, int16_t *block)
  1276. {
  1277. ff_j_rev_dct2 (block);
  1278. put_pixels_clamped2_c(block, dest, line_size);
  1279. }
  1280. static void ff_jref_idct2_add(uint8_t *dest, int line_size, int16_t *block)
  1281. {
  1282. ff_j_rev_dct2 (block);
  1283. add_pixels_clamped2_c(block, dest, line_size);
  1284. }
  1285. static void ff_jref_idct1_put(uint8_t *dest, int line_size, int16_t *block)
  1286. {
  1287. dest[0] = av_clip_uint8((block[0] + 4)>>3);
  1288. }
  1289. static void ff_jref_idct1_add(uint8_t *dest, int line_size, int16_t *block)
  1290. {
  1291. dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
  1292. }
  1293. /* draw the edges of width 'w' of an image of size width, height */
  1294. // FIXME: Check that this is OK for MPEG-4 interlaced.
  1295. static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
  1296. int w, int h, int sides)
  1297. {
  1298. uint8_t *ptr = buf, *last_line;
  1299. int i;
  1300. /* left and right */
  1301. for (i = 0; i < height; i++) {
  1302. memset(ptr - w, ptr[0], w);
  1303. memset(ptr + width, ptr[width - 1], w);
  1304. ptr += wrap;
  1305. }
  1306. /* top and bottom + corners */
  1307. buf -= w;
  1308. last_line = buf + (height - 1) * wrap;
  1309. if (sides & EDGE_TOP)
  1310. for (i = 0; i < h; i++)
  1311. // top
  1312. memcpy(buf - (i + 1) * wrap, buf, width + w + w);
  1313. if (sides & EDGE_BOTTOM)
  1314. for (i = 0; i < h; i++)
  1315. // bottom
  1316. memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
  1317. }
  1318. /* init static data */
  1319. av_cold void ff_dsputil_static_init(void)
  1320. {
  1321. int i;
  1322. for (i = 0; i < 512; i++)
  1323. ff_square_tab[i] = (i - 256) * (i - 256);
  1324. }
  1325. int ff_check_alignment(void)
  1326. {
  1327. static int did_fail = 0;
  1328. LOCAL_ALIGNED_16(int, aligned, [4]);
  1329. if ((intptr_t)aligned & 15) {
  1330. if (!did_fail) {
  1331. #if HAVE_MMX || HAVE_ALTIVEC
  1332. av_log(NULL, AV_LOG_ERROR,
  1333. "Compiler did not align stack variables. Libavcodec has been miscompiled\n"
  1334. "and may be very slow or crash. This is not a bug in libavcodec,\n"
  1335. "but in the compiler. You may try recompiling using gcc >= 4.2.\n"
  1336. "Do not report crashes to FFmpeg developers.\n");
  1337. #endif
  1338. did_fail=1;
  1339. }
  1340. return -1;
  1341. }
  1342. return 0;
  1343. }
  1344. av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
  1345. {
  1346. const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
  1347. ff_check_alignment();
  1348. #if CONFIG_ENCODERS
  1349. if (avctx->bits_per_raw_sample == 10) {
  1350. c->fdct = ff_jpeg_fdct_islow_10;
  1351. c->fdct248 = ff_fdct248_islow_10;
  1352. } else {
  1353. if (avctx->dct_algo == FF_DCT_FASTINT) {
  1354. c->fdct = ff_fdct_ifast;
  1355. c->fdct248 = ff_fdct_ifast248;
  1356. } else if (avctx->dct_algo == FF_DCT_FAAN) {
  1357. c->fdct = ff_faandct;
  1358. c->fdct248 = ff_faandct248;
  1359. } else {
  1360. c->fdct = ff_jpeg_fdct_islow_8; // slow/accurate/default
  1361. c->fdct248 = ff_fdct248_islow_8;
  1362. }
  1363. }
  1364. #endif /* CONFIG_ENCODERS */
  1365. if (avctx->lowres==1) {
  1366. c->idct_put = ff_jref_idct4_put;
  1367. c->idct_add = ff_jref_idct4_add;
  1368. c->idct = ff_j_rev_dct4;
  1369. c->idct_permutation_type = FF_NO_IDCT_PERM;
  1370. } else if (avctx->lowres==2) {
  1371. c->idct_put = ff_jref_idct2_put;
  1372. c->idct_add = ff_jref_idct2_add;
  1373. c->idct = ff_j_rev_dct2;
  1374. c->idct_permutation_type = FF_NO_IDCT_PERM;
  1375. } else if (avctx->lowres==3) {
  1376. c->idct_put = ff_jref_idct1_put;
  1377. c->idct_add = ff_jref_idct1_add;
  1378. c->idct = ff_j_rev_dct1;
  1379. c->idct_permutation_type = FF_NO_IDCT_PERM;
  1380. } else {
  1381. if (avctx->bits_per_raw_sample == 10) {
  1382. c->idct_put = ff_simple_idct_put_10;
  1383. c->idct_add = ff_simple_idct_add_10;
  1384. c->idct = ff_simple_idct_10;
  1385. c->idct_permutation_type = FF_NO_IDCT_PERM;
  1386. } else if (avctx->bits_per_raw_sample == 12) {
  1387. c->idct_put = ff_simple_idct_put_12;
  1388. c->idct_add = ff_simple_idct_add_12;
  1389. c->idct = ff_simple_idct_12;
  1390. c->idct_permutation_type = FF_NO_IDCT_PERM;
  1391. } else {
  1392. if (avctx->idct_algo == FF_IDCT_INT) {
  1393. c->idct_put = jref_idct_put;
  1394. c->idct_add = jref_idct_add;
  1395. c->idct = ff_j_rev_dct;
  1396. c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
  1397. } else if (avctx->idct_algo == FF_IDCT_FAAN) {
  1398. c->idct_put = ff_faanidct_put;
  1399. c->idct_add = ff_faanidct_add;
  1400. c->idct = ff_faanidct;
  1401. c->idct_permutation_type = FF_NO_IDCT_PERM;
  1402. } else { // accurate/default
  1403. c->idct_put = ff_simple_idct_put_8;
  1404. c->idct_add = ff_simple_idct_add_8;
  1405. c->idct = ff_simple_idct_8;
  1406. c->idct_permutation_type = FF_NO_IDCT_PERM;
  1407. }
  1408. }
  1409. }
  1410. c->diff_pixels = diff_pixels_c;
  1411. c->put_pixels_clamped = put_pixels_clamped_c;
  1412. c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
  1413. c->add_pixels_clamped = add_pixels_clamped_c;
  1414. c->sum_abs_dctelem = sum_abs_dctelem_c;
  1415. c->gmc1 = gmc1_c;
  1416. c->gmc = ff_gmc_c;
  1417. c->pix_sum = pix_sum_c;
  1418. c->pix_norm1 = pix_norm1_c;
  1419. /* TODO [0] 16 [1] 8 */
  1420. c->pix_abs[0][0] = pix_abs16_c;
  1421. c->pix_abs[0][1] = pix_abs16_x2_c;
  1422. c->pix_abs[0][2] = pix_abs16_y2_c;
  1423. c->pix_abs[0][3] = pix_abs16_xy2_c;
  1424. c->pix_abs[1][0] = pix_abs8_c;
  1425. c->pix_abs[1][1] = pix_abs8_x2_c;
  1426. c->pix_abs[1][2] = pix_abs8_y2_c;
  1427. c->pix_abs[1][3] = pix_abs8_xy2_c;
  1428. #define SET_CMP_FUNC(name) \
  1429. c->name[0] = name ## 16_c; \
  1430. c->name[1] = name ## 8x8_c;
  1431. SET_CMP_FUNC(hadamard8_diff)
  1432. c->hadamard8_diff[4] = hadamard8_intra16_c;
  1433. c->hadamard8_diff[5] = hadamard8_intra8x8_c;
  1434. SET_CMP_FUNC(dct_sad)
  1435. SET_CMP_FUNC(dct_max)
  1436. #if CONFIG_GPL
  1437. SET_CMP_FUNC(dct264_sad)
  1438. #endif
  1439. c->sad[0] = pix_abs16_c;
  1440. c->sad[1] = pix_abs8_c;
  1441. c->sse[0] = sse16_c;
  1442. c->sse[1] = sse8_c;
  1443. c->sse[2] = sse4_c;
  1444. SET_CMP_FUNC(quant_psnr)
  1445. SET_CMP_FUNC(rd)
  1446. SET_CMP_FUNC(bit)
  1447. c->vsad[0] = vsad16_c;
  1448. c->vsad[1] = vsad8_c;
  1449. c->vsad[4] = vsad_intra16_c;
  1450. c->vsad[5] = vsad_intra8_c;
  1451. c->vsse[0] = vsse16_c;
  1452. c->vsse[1] = vsse8_c;
  1453. c->vsse[4] = vsse_intra16_c;
  1454. c->vsse[5] = vsse_intra8_c;
  1455. c->nsse[0] = nsse16_c;
  1456. c->nsse[1] = nsse8_c;
  1457. #if CONFIG_SNOW_DECODER || CONFIG_SNOW_ENCODER
  1458. ff_dsputil_init_dwt(c);
  1459. #endif
  1460. c->bswap_buf = bswap_buf;
  1461. c->bswap16_buf = bswap16_buf;
  1462. c->try_8x8basis = try_8x8basis_c;
  1463. c->add_8x8basis = add_8x8basis_c;
  1464. c->scalarproduct_int16 = scalarproduct_int16_c;
  1465. c->vector_clip_int32 = vector_clip_int32_c;
  1466. c->vector_clipf = vector_clipf_c;
  1467. c->shrink[0] = av_image_copy_plane;
  1468. c->shrink[1] = ff_shrink22;
  1469. c->shrink[2] = ff_shrink44;
  1470. c->shrink[3] = ff_shrink88;
  1471. c->draw_edges = draw_edges_8_c;
  1472. switch (avctx->bits_per_raw_sample) {
  1473. case 9:
  1474. case 10:
  1475. case 12:
  1476. case 14:
  1477. c->get_pixels = get_pixels_16_c;
  1478. break;
  1479. default:
  1480. if (avctx->bits_per_raw_sample<=8 || avctx->codec_type != AVMEDIA_TYPE_VIDEO) {
  1481. c->get_pixels = get_pixels_8_c;
  1482. }
  1483. break;
  1484. }
  1485. if (ARCH_ALPHA)
  1486. ff_dsputil_init_alpha(c, avctx);
  1487. if (ARCH_ARM)
  1488. ff_dsputil_init_arm(c, avctx, high_bit_depth);
  1489. if (ARCH_PPC)
  1490. ff_dsputil_init_ppc(c, avctx, high_bit_depth);
  1491. if (ARCH_X86)
  1492. ff_dsputil_init_x86(c, avctx, high_bit_depth);
  1493. ff_init_scantable_permutation(c->idct_permutation,
  1494. c->idct_permutation_type);
  1495. }
  1496. av_cold void dsputil_init(DSPContext* c, AVCodecContext *avctx)
  1497. {
  1498. ff_dsputil_init(c, avctx);
  1499. }
  1500. av_cold void avpriv_dsputil_init(DSPContext *c, AVCodecContext *avctx)
  1501. {
  1502. ff_dsputil_init(c, avctx);
  1503. }