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.

1748 lines
57KB

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