3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * gmc & q-pel & 32/64 bit based MC by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of FFmpeg.
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.
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.
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
30 #include "libavutil/attributes.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/internal.h"
34 #include "copy_block.h"
37 #include "simple_idct.h"
40 #include "imgconvert.h"
42 #include "mpegvideo.h"
45 uint32_t ff_square_tab[512] = { 0, };
48 #include "dsputilenc_template.c"
52 #include "dsputilenc_template.c"
54 /* Input permutation for the simple_idct_mmx */
55 static const uint8_t simple_mmx_permutation[64] = {
56 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
57 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
58 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
59 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
60 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
61 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
62 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
63 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
66 static const uint8_t idct_sse2_row_perm[8] = { 0, 4, 1, 5, 2, 6, 3, 7 };
68 av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st,
69 const uint8_t *src_scantable)
73 st->scantable = src_scantable;
75 for (i = 0; i < 64; i++) {
76 int j = src_scantable[i];
77 st->permutated[i] = permutation[j];
81 for (i = 0; i < 64; i++) {
82 int j = st->permutated[i];
85 st->raster_end[i] = end;
89 av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
90 int idct_permutation_type)
94 switch (idct_permutation_type) {
96 for (i = 0; i < 64; i++)
97 idct_permutation[i] = i;
99 case FF_LIBMPEG2_IDCT_PERM:
100 for (i = 0; i < 64; i++)
101 idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
103 case FF_SIMPLE_IDCT_PERM:
104 for (i = 0; i < 64; i++)
105 idct_permutation[i] = simple_mmx_permutation[i];
107 case FF_TRANSPOSE_IDCT_PERM:
108 for (i = 0; i < 64; i++)
109 idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
111 case FF_PARTTRANS_IDCT_PERM:
112 for (i = 0; i < 64; i++)
113 idct_permutation[i] = (i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3);
115 case FF_SSE2_IDCT_PERM:
116 for (i = 0; i < 64; i++)
117 idct_permutation[i] = (i & 0x38) | idct_sse2_row_perm[i & 7];
120 av_log(NULL, AV_LOG_ERROR,
121 "Internal error, IDCT permutation not set\n");
125 static int pix_sum_c(uint8_t *pix, int line_size)
129 for (i = 0; i < 16; i++) {
130 for (j = 0; j < 16; j += 8) {
141 pix += line_size - 16;
146 static int pix_norm1_c(uint8_t *pix, int line_size)
149 uint32_t *sq = ff_square_tab + 256;
151 for (i = 0; i < 16; i++) {
152 for (j = 0; j < 16; j += 8) {
164 register uint64_t x = *(uint64_t *) pix;
166 s += sq[(x >> 8) & 0xff];
167 s += sq[(x >> 16) & 0xff];
168 s += sq[(x >> 24) & 0xff];
169 s += sq[(x >> 32) & 0xff];
170 s += sq[(x >> 40) & 0xff];
171 s += sq[(x >> 48) & 0xff];
172 s += sq[(x >> 56) & 0xff];
174 register uint32_t x = *(uint32_t *) pix;
176 s += sq[(x >> 8) & 0xff];
177 s += sq[(x >> 16) & 0xff];
178 s += sq[(x >> 24) & 0xff];
179 x = *(uint32_t *) (pix + 4);
181 s += sq[(x >> 8) & 0xff];
182 s += sq[(x >> 16) & 0xff];
183 s += sq[(x >> 24) & 0xff];
188 pix += line_size - 16;
193 static void bswap_buf(uint32_t *dst, const uint32_t *src, int w)
197 for (i = 0; i + 8 <= w; i += 8) {
198 dst[i + 0] = av_bswap32(src[i + 0]);
199 dst[i + 1] = av_bswap32(src[i + 1]);
200 dst[i + 2] = av_bswap32(src[i + 2]);
201 dst[i + 3] = av_bswap32(src[i + 3]);
202 dst[i + 4] = av_bswap32(src[i + 4]);
203 dst[i + 5] = av_bswap32(src[i + 5]);
204 dst[i + 6] = av_bswap32(src[i + 6]);
205 dst[i + 7] = av_bswap32(src[i + 7]);
208 dst[i + 0] = av_bswap32(src[i + 0]);
211 static void bswap16_buf(uint16_t *dst, const uint16_t *src, int len)
214 *dst++ = av_bswap16(*src++);
217 static int sse4_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
218 int line_size, int h)
221 uint32_t *sq = ff_square_tab + 256;
223 for (i = 0; i < h; i++) {
224 s += sq[pix1[0] - pix2[0]];
225 s += sq[pix1[1] - pix2[1]];
226 s += sq[pix1[2] - pix2[2]];
227 s += sq[pix1[3] - pix2[3]];
234 static int sse8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
235 int line_size, int h)
238 uint32_t *sq = ff_square_tab + 256;
240 for (i = 0; i < h; i++) {
241 s += sq[pix1[0] - pix2[0]];
242 s += sq[pix1[1] - pix2[1]];
243 s += sq[pix1[2] - pix2[2]];
244 s += sq[pix1[3] - pix2[3]];
245 s += sq[pix1[4] - pix2[4]];
246 s += sq[pix1[5] - pix2[5]];
247 s += sq[pix1[6] - pix2[6]];
248 s += sq[pix1[7] - pix2[7]];
255 static int sse16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
256 int line_size, int h)
259 uint32_t *sq = ff_square_tab + 256;
261 for (i = 0; i < h; i++) {
262 s += sq[pix1[0] - pix2[0]];
263 s += sq[pix1[1] - pix2[1]];
264 s += sq[pix1[2] - pix2[2]];
265 s += sq[pix1[3] - pix2[3]];
266 s += sq[pix1[4] - pix2[4]];
267 s += sq[pix1[5] - pix2[5]];
268 s += sq[pix1[6] - pix2[6]];
269 s += sq[pix1[7] - pix2[7]];
270 s += sq[pix1[8] - pix2[8]];
271 s += sq[pix1[9] - pix2[9]];
272 s += sq[pix1[10] - pix2[10]];
273 s += sq[pix1[11] - pix2[11]];
274 s += sq[pix1[12] - pix2[12]];
275 s += sq[pix1[13] - pix2[13]];
276 s += sq[pix1[14] - pix2[14]];
277 s += sq[pix1[15] - pix2[15]];
285 static void diff_pixels_c(int16_t *av_restrict block, const uint8_t *s1,
286 const uint8_t *s2, int stride)
290 /* read the pixels */
291 for (i = 0; i < 8; i++) {
292 block[0] = s1[0] - s2[0];
293 block[1] = s1[1] - s2[1];
294 block[2] = s1[2] - s2[2];
295 block[3] = s1[3] - s2[3];
296 block[4] = s1[4] - s2[4];
297 block[5] = s1[5] - s2[5];
298 block[6] = s1[6] - s2[6];
299 block[7] = s1[7] - s2[7];
306 static void put_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
311 /* read the pixels */
312 for (i = 0; i < 8; i++) {
313 pixels[0] = av_clip_uint8(block[0]);
314 pixels[1] = av_clip_uint8(block[1]);
315 pixels[2] = av_clip_uint8(block[2]);
316 pixels[3] = av_clip_uint8(block[3]);
317 pixels[4] = av_clip_uint8(block[4]);
318 pixels[5] = av_clip_uint8(block[5]);
319 pixels[6] = av_clip_uint8(block[6]);
320 pixels[7] = av_clip_uint8(block[7]);
327 static void put_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
332 /* read the pixels */
334 pixels[0] = av_clip_uint8(block[0]);
335 pixels[1] = av_clip_uint8(block[1]);
336 pixels[2] = av_clip_uint8(block[2]);
337 pixels[3] = av_clip_uint8(block[3]);
344 static void put_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
349 /* read the pixels */
351 pixels[0] = av_clip_uint8(block[0]);
352 pixels[1] = av_clip_uint8(block[1]);
359 static void put_signed_pixels_clamped_c(const int16_t *block,
360 uint8_t *av_restrict pixels,
365 for (i = 0; i < 8; i++) {
366 for (j = 0; j < 8; j++) {
369 else if (*block > 127)
372 *pixels = (uint8_t) (*block + 128);
376 pixels += (line_size - 8);
380 static void add_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
385 /* read the pixels */
386 for (i = 0; i < 8; i++) {
387 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
388 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
389 pixels[2] = av_clip_uint8(pixels[2] + block[2]);
390 pixels[3] = av_clip_uint8(pixels[3] + block[3]);
391 pixels[4] = av_clip_uint8(pixels[4] + block[4]);
392 pixels[5] = av_clip_uint8(pixels[5] + block[5]);
393 pixels[6] = av_clip_uint8(pixels[6] + block[6]);
394 pixels[7] = av_clip_uint8(pixels[7] + block[7]);
400 static void add_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
405 /* read the pixels */
407 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
408 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
409 pixels[2] = av_clip_uint8(pixels[2] + block[2]);
410 pixels[3] = av_clip_uint8(pixels[3] + block[3]);
416 static void add_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
421 /* read the pixels */
423 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
424 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
430 static int sum_abs_dctelem_c(int16_t *block)
434 for (i = 0; i < 64; i++)
435 sum += FFABS(block[i]);
439 #define avg2(a, b) ((a + b + 1) >> 1)
440 #define avg4(a, b, c, d) ((a + b + c + d + 2) >> 2)
442 static void gmc1_c(uint8_t *dst, uint8_t *src, int stride, int h,
443 int x16, int y16, int rounder)
445 const int A = (16 - x16) * (16 - y16);
446 const int B = (x16) * (16 - y16);
447 const int C = (16 - x16) * (y16);
448 const int D = (x16) * (y16);
451 for (i = 0; i < h; i++) {
452 dst[0] = (A * src[0] + B * src[1] + C * src[stride + 0] + D * src[stride + 1] + rounder) >> 8;
453 dst[1] = (A * src[1] + B * src[2] + C * src[stride + 1] + D * src[stride + 2] + rounder) >> 8;
454 dst[2] = (A * src[2] + B * src[3] + C * src[stride + 2] + D * src[stride + 3] + rounder) >> 8;
455 dst[3] = (A * src[3] + B * src[4] + C * src[stride + 3] + D * src[stride + 4] + rounder) >> 8;
456 dst[4] = (A * src[4] + B * src[5] + C * src[stride + 4] + D * src[stride + 5] + rounder) >> 8;
457 dst[5] = (A * src[5] + B * src[6] + C * src[stride + 5] + D * src[stride + 6] + rounder) >> 8;
458 dst[6] = (A * src[6] + B * src[7] + C * src[stride + 6] + D * src[stride + 7] + rounder) >> 8;
459 dst[7] = (A * src[7] + B * src[8] + C * src[stride + 7] + D * src[stride + 8] + rounder) >> 8;
465 void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
466 int dxx, int dxy, int dyx, int dyy, int shift, int r,
467 int width, int height)
470 const int s = 1 << shift;
475 for (y = 0; y < h; y++) {
480 for (x = 0; x < 8; x++) { // FIXME: optimize
482 int src_x = vx >> 16;
483 int src_y = vy >> 16;
484 int frac_x = src_x & (s - 1);
485 int frac_y = src_y & (s - 1);
490 if ((unsigned) src_x < width) {
491 if ((unsigned) src_y < height) {
492 index = src_x + src_y * stride;
493 dst[y * stride + x] =
494 ((src[index] * (s - frac_x) +
495 src[index + 1] * frac_x) * (s - frac_y) +
496 (src[index + stride] * (s - frac_x) +
497 src[index + stride + 1] * frac_x) * frac_y +
500 index = src_x + av_clip(src_y, 0, height) * stride;
501 dst[y * stride + x] =
502 ((src[index] * (s - frac_x) +
503 src[index + 1] * frac_x) * s +
507 if ((unsigned) src_y < height) {
508 index = av_clip(src_x, 0, width) + src_y * stride;
509 dst[y * stride + x] =
510 ((src[index] * (s - frac_y) +
511 src[index + stride] * frac_y) * s +
514 index = av_clip(src_x, 0, width) +
515 av_clip(src_y, 0, height) * stride;
516 dst[y * stride + x] = src[index];
528 static inline int pix_abs16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
529 int line_size, int h)
533 for (i = 0; i < h; i++) {
534 s += abs(pix1[0] - pix2[0]);
535 s += abs(pix1[1] - pix2[1]);
536 s += abs(pix1[2] - pix2[2]);
537 s += abs(pix1[3] - pix2[3]);
538 s += abs(pix1[4] - pix2[4]);
539 s += abs(pix1[5] - pix2[5]);
540 s += abs(pix1[6] - pix2[6]);
541 s += abs(pix1[7] - pix2[7]);
542 s += abs(pix1[8] - pix2[8]);
543 s += abs(pix1[9] - pix2[9]);
544 s += abs(pix1[10] - pix2[10]);
545 s += abs(pix1[11] - pix2[11]);
546 s += abs(pix1[12] - pix2[12]);
547 s += abs(pix1[13] - pix2[13]);
548 s += abs(pix1[14] - pix2[14]);
549 s += abs(pix1[15] - pix2[15]);
556 static int pix_abs16_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
557 int line_size, int h)
561 for (i = 0; i < h; i++) {
562 s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
563 s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
564 s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
565 s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
566 s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
567 s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
568 s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
569 s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
570 s += abs(pix1[8] - avg2(pix2[8], pix2[9]));
571 s += abs(pix1[9] - avg2(pix2[9], pix2[10]));
572 s += abs(pix1[10] - avg2(pix2[10], pix2[11]));
573 s += abs(pix1[11] - avg2(pix2[11], pix2[12]));
574 s += abs(pix1[12] - avg2(pix2[12], pix2[13]));
575 s += abs(pix1[13] - avg2(pix2[13], pix2[14]));
576 s += abs(pix1[14] - avg2(pix2[14], pix2[15]));
577 s += abs(pix1[15] - avg2(pix2[15], pix2[16]));
584 static int pix_abs16_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
585 int line_size, int h)
588 uint8_t *pix3 = pix2 + line_size;
590 for (i = 0; i < h; i++) {
591 s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
592 s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
593 s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
594 s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
595 s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
596 s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
597 s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
598 s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
599 s += abs(pix1[8] - avg2(pix2[8], pix3[8]));
600 s += abs(pix1[9] - avg2(pix2[9], pix3[9]));
601 s += abs(pix1[10] - avg2(pix2[10], pix3[10]));
602 s += abs(pix1[11] - avg2(pix2[11], pix3[11]));
603 s += abs(pix1[12] - avg2(pix2[12], pix3[12]));
604 s += abs(pix1[13] - avg2(pix2[13], pix3[13]));
605 s += abs(pix1[14] - avg2(pix2[14], pix3[14]));
606 s += abs(pix1[15] - avg2(pix2[15], pix3[15]));
614 static int pix_abs16_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
615 int line_size, int h)
618 uint8_t *pix3 = pix2 + line_size;
620 for (i = 0; i < h; i++) {
621 s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
622 s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
623 s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
624 s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
625 s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
626 s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
627 s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
628 s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
629 s += abs(pix1[8] - avg4(pix2[8], pix2[9], pix3[8], pix3[9]));
630 s += abs(pix1[9] - avg4(pix2[9], pix2[10], pix3[9], pix3[10]));
631 s += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
632 s += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
633 s += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
634 s += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
635 s += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
636 s += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
644 static inline int pix_abs8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
645 int line_size, int h)
649 for (i = 0; i < h; i++) {
650 s += abs(pix1[0] - pix2[0]);
651 s += abs(pix1[1] - pix2[1]);
652 s += abs(pix1[2] - pix2[2]);
653 s += abs(pix1[3] - pix2[3]);
654 s += abs(pix1[4] - pix2[4]);
655 s += abs(pix1[5] - pix2[5]);
656 s += abs(pix1[6] - pix2[6]);
657 s += abs(pix1[7] - pix2[7]);
664 static int pix_abs8_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
665 int line_size, int h)
669 for (i = 0; i < h; i++) {
670 s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
671 s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
672 s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
673 s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
674 s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
675 s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
676 s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
677 s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
684 static int pix_abs8_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
685 int line_size, int h)
688 uint8_t *pix3 = pix2 + line_size;
690 for (i = 0; i < h; i++) {
691 s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
692 s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
693 s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
694 s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
695 s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
696 s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
697 s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
698 s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
706 static int pix_abs8_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
707 int line_size, int h)
710 uint8_t *pix3 = pix2 + line_size;
712 for (i = 0; i < h; i++) {
713 s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
714 s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
715 s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
716 s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
717 s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
718 s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
719 s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
720 s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
728 static int nsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
730 int score1 = 0, score2 = 0, x, y;
732 for (y = 0; y < h; y++) {
733 for (x = 0; x < 16; x++)
734 score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
736 for (x = 0; x < 15; x++)
737 score2 += FFABS(s1[x] - s1[x + stride] -
738 s1[x + 1] + s1[x + stride + 1]) -
739 FFABS(s2[x] - s2[x + stride] -
740 s2[x + 1] + s2[x + stride + 1]);
747 return score1 + FFABS(score2) * c->avctx->nsse_weight;
749 return score1 + FFABS(score2) * 8;
752 static int nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
754 int score1 = 0, score2 = 0, x, y;
756 for (y = 0; y < h; y++) {
757 for (x = 0; x < 8; x++)
758 score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
760 for (x = 0; x < 7; x++)
761 score2 += FFABS(s1[x] - s1[x + stride] -
762 s1[x + 1] + s1[x + stride + 1]) -
763 FFABS(s2[x] - s2[x + stride] -
764 s2[x + 1] + s2[x + stride + 1]);
771 return score1 + FFABS(score2) * c->avctx->nsse_weight;
773 return score1 + FFABS(score2) * 8;
776 static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
777 int16_t basis[64], int scale)
780 unsigned int sum = 0;
782 for (i = 0; i < 8 * 8; i++) {
783 int b = rem[i] + ((basis[i] * scale +
784 (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
785 (BASIS_SHIFT - RECON_SHIFT));
788 av_assert2(-512 < b && b < 512);
790 sum += (w * b) * (w * b) >> 4;
795 static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
799 for (i = 0; i < 8 * 8; i++)
800 rem[i] += (basis[i] * scale +
801 (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
802 (BASIS_SHIFT - RECON_SHIFT);
805 static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
811 void ff_set_cmp(DSPContext *c, me_cmp_func *cmp, int type)
815 memset(cmp, 0, sizeof(void *) * 6);
817 for (i = 0; i < 6; i++) {
818 switch (type & 0xFF) {
823 cmp[i] = c->hadamard8_diff[i];
829 cmp[i] = c->dct_sad[i];
832 cmp[i] = c->dct264_sad[i];
835 cmp[i] = c->dct_max[i];
838 cmp[i] = c->quant_psnr[i];
867 av_log(NULL, AV_LOG_ERROR,
868 "internal error in cmp function selection\n");
873 #define BUTTERFLY2(o1, o2, i1, i2) \
877 #define BUTTERFLY1(x, y) \
886 #define BUTTERFLYA(x, y) (FFABS((x) + (y)) + FFABS((x) - (y)))
888 static int hadamard8_diff8x8_c(MpegEncContext *s, uint8_t *dst,
889 uint8_t *src, int stride, int h)
891 int i, temp[64], sum = 0;
895 for (i = 0; i < 8; i++) {
896 // FIXME: try pointer walks
897 BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
898 src[stride * i + 0] - dst[stride * i + 0],
899 src[stride * i + 1] - dst[stride * i + 1]);
900 BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
901 src[stride * i + 2] - dst[stride * i + 2],
902 src[stride * i + 3] - dst[stride * i + 3]);
903 BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
904 src[stride * i + 4] - dst[stride * i + 4],
905 src[stride * i + 5] - dst[stride * i + 5]);
906 BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
907 src[stride * i + 6] - dst[stride * i + 6],
908 src[stride * i + 7] - dst[stride * i + 7]);
910 BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
911 BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
912 BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
913 BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
915 BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
916 BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
917 BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
918 BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
921 for (i = 0; i < 8; i++) {
922 BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
923 BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
924 BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
925 BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
927 BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
928 BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
929 BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
930 BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
932 sum += BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i]) +
933 BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i]) +
934 BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i]) +
935 BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
940 static int hadamard8_intra8x8_c(MpegEncContext *s, uint8_t *src,
941 uint8_t *dummy, int stride, int h)
943 int i, temp[64], sum = 0;
947 for (i = 0; i < 8; i++) {
948 // FIXME: try pointer walks
949 BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
950 src[stride * i + 0], src[stride * i + 1]);
951 BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
952 src[stride * i + 2], src[stride * i + 3]);
953 BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
954 src[stride * i + 4], src[stride * i + 5]);
955 BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
956 src[stride * i + 6], src[stride * i + 7]);
958 BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
959 BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
960 BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
961 BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
963 BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
964 BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
965 BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
966 BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
969 for (i = 0; i < 8; i++) {
970 BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
971 BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
972 BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
973 BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
975 BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
976 BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
977 BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
978 BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
981 BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i])
982 + BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i])
983 + BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i])
984 + BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
987 sum -= FFABS(temp[8 * 0] + temp[8 * 4]); // -mean
992 static int dct_sad8x8_c(MpegEncContext *s, uint8_t *src1,
993 uint8_t *src2, int stride, int h)
995 LOCAL_ALIGNED_16(int16_t, temp, [64]);
999 s->dsp.diff_pixels(temp, src1, src2, stride);
1001 return s->dsp.sum_abs_dctelem(temp);
1007 const int s07 = SRC(0) + SRC(7); \
1008 const int s16 = SRC(1) + SRC(6); \
1009 const int s25 = SRC(2) + SRC(5); \
1010 const int s34 = SRC(3) + SRC(4); \
1011 const int a0 = s07 + s34; \
1012 const int a1 = s16 + s25; \
1013 const int a2 = s07 - s34; \
1014 const int a3 = s16 - s25; \
1015 const int d07 = SRC(0) - SRC(7); \
1016 const int d16 = SRC(1) - SRC(6); \
1017 const int d25 = SRC(2) - SRC(5); \
1018 const int d34 = SRC(3) - SRC(4); \
1019 const int a4 = d16 + d25 + (d07 + (d07 >> 1)); \
1020 const int a5 = d07 - d34 - (d25 + (d25 >> 1)); \
1021 const int a6 = d07 + d34 - (d16 + (d16 >> 1)); \
1022 const int a7 = d16 - d25 + (d34 + (d34 >> 1)); \
1024 DST(1, a4 + (a7 >> 2)); \
1025 DST(2, a2 + (a3 >> 1)); \
1026 DST(3, a5 + (a6 >> 2)); \
1028 DST(5, a6 - (a5 >> 2)); \
1029 DST(6, (a2 >> 1) - a3); \
1030 DST(7, (a4 >> 2) - a7); \
1033 static int dct264_sad8x8_c(MpegEncContext *s, uint8_t *src1,
1034 uint8_t *src2, int stride, int h)
1039 s->dsp.diff_pixels(dct[0], src1, src2, stride);
1041 #define SRC(x) dct[i][x]
1042 #define DST(x, v) dct[i][x] = v
1043 for (i = 0; i < 8; i++)
1048 #define SRC(x) dct[x][i]
1049 #define DST(x, v) sum += FFABS(v)
1050 for (i = 0; i < 8; i++)
1058 static int dct_max8x8_c(MpegEncContext *s, uint8_t *src1,
1059 uint8_t *src2, int stride, int h)
1061 LOCAL_ALIGNED_16(int16_t, temp, [64]);
1066 s->dsp.diff_pixels(temp, src1, src2, stride);
1069 for (i = 0; i < 64; i++)
1070 sum = FFMAX(sum, FFABS(temp[i]));
1075 static int quant_psnr8x8_c(MpegEncContext *s, uint8_t *src1,
1076 uint8_t *src2, int stride, int h)
1078 LOCAL_ALIGNED_16(int16_t, temp, [64 * 2]);
1079 int16_t *const bak = temp + 64;
1085 s->dsp.diff_pixels(temp, src1, src2, stride);
1087 memcpy(bak, temp, 64 * sizeof(int16_t));
1089 s->block_last_index[0 /* FIXME */] =
1090 s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1091 s->dct_unquantize_inter(s, temp, 0, s->qscale);
1092 ff_simple_idct_8(temp); // FIXME
1094 for (i = 0; i < 64; i++)
1095 sum += (temp[i] - bak[i]) * (temp[i] - bak[i]);
1100 static int rd8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
1103 const uint8_t *scantable = s->intra_scantable.permutated;
1104 LOCAL_ALIGNED_16(int16_t, temp, [64]);
1105 LOCAL_ALIGNED_16(uint8_t, lsrc1, [64]);
1106 LOCAL_ALIGNED_16(uint8_t, lsrc2, [64]);
1107 int i, last, run, bits, level, distortion, start_i;
1108 const int esc_length = s->ac_esc_length;
1109 uint8_t *length, *last_length;
1113 copy_block8(lsrc1, src1, 8, stride, 8);
1114 copy_block8(lsrc2, src2, 8, stride, 8);
1116 s->dsp.diff_pixels(temp, lsrc1, lsrc2, 8);
1118 s->block_last_index[0 /* FIXME */] =
1120 s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1126 length = s->intra_ac_vlc_length;
1127 last_length = s->intra_ac_vlc_last_length;
1128 bits += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
1131 length = s->inter_ac_vlc_length;
1132 last_length = s->inter_ac_vlc_last_length;
1135 if (last >= start_i) {
1137 for (i = start_i; i < last; i++) {
1138 int j = scantable[i];
1143 if ((level & (~127)) == 0)
1144 bits += length[UNI_AC_ENC_INDEX(run, level)];
1151 i = scantable[last];
1153 level = temp[i] + 64;
1155 av_assert2(level - 64);
1157 if ((level & (~127)) == 0) {
1158 bits += last_length[UNI_AC_ENC_INDEX(run, level)];
1165 s->dct_unquantize_intra(s, temp, 0, s->qscale);
1167 s->dct_unquantize_inter(s, temp, 0, s->qscale);
1170 s->dsp.idct_add(lsrc2, 8, temp);
1172 distortion = s->dsp.sse[1](NULL, lsrc2, lsrc1, 8, 8);
1174 return distortion + ((bits * s->qscale * s->qscale * 109 + 64) >> 7);
1177 static int bit8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
1180 const uint8_t *scantable = s->intra_scantable.permutated;
1181 LOCAL_ALIGNED_16(int16_t, temp, [64]);
1182 int i, last, run, bits, level, start_i;
1183 const int esc_length = s->ac_esc_length;
1184 uint8_t *length, *last_length;
1188 s->dsp.diff_pixels(temp, src1, src2, stride);
1190 s->block_last_index[0 /* FIXME */] =
1192 s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1198 length = s->intra_ac_vlc_length;
1199 last_length = s->intra_ac_vlc_last_length;
1200 bits += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
1203 length = s->inter_ac_vlc_length;
1204 last_length = s->inter_ac_vlc_last_length;
1207 if (last >= start_i) {
1209 for (i = start_i; i < last; i++) {
1210 int j = scantable[i];
1215 if ((level & (~127)) == 0)
1216 bits += length[UNI_AC_ENC_INDEX(run, level)];
1223 i = scantable[last];
1225 level = temp[i] + 64;
1227 av_assert2(level - 64);
1229 if ((level & (~127)) == 0)
1230 bits += last_length[UNI_AC_ENC_INDEX(run, level)];
1238 #define VSAD_INTRA(size) \
1239 static int vsad_intra ## size ## _c(MpegEncContext *c, \
1240 uint8_t *s, uint8_t *dummy, \
1241 int stride, int h) \
1243 int score = 0, x, y; \
1245 for (y = 1; y < h; y++) { \
1246 for (x = 0; x < size; x += 4) { \
1247 score += FFABS(s[x] - s[x + stride]) + \
1248 FFABS(s[x + 1] - s[x + stride + 1]) + \
1249 FFABS(s[x + 2] - s[x + 2 + stride]) + \
1250 FFABS(s[x + 3] - s[x + 3 + stride]); \
1260 #define VSAD(size) \
1261 static int vsad ## size ## _c(MpegEncContext *c, \
1262 uint8_t *s1, uint8_t *s2, \
1263 int stride, int h) \
1265 int score = 0, x, y; \
1267 for (y = 1; y < h; y++) { \
1268 for (x = 0; x < size; x++) \
1269 score += FFABS(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]); \
1279 #define SQ(a) ((a) * (a))
1280 #define VSSE_INTRA(size) \
1281 static int vsse_intra ## size ## _c(MpegEncContext *c, \
1282 uint8_t *s, uint8_t *dummy, \
1283 int stride, int h) \
1285 int score = 0, x, y; \
1287 for (y = 1; y < h; y++) { \
1288 for (x = 0; x < size; x += 4) { \
1289 score += SQ(s[x] - s[x + stride]) + \
1290 SQ(s[x + 1] - s[x + stride + 1]) + \
1291 SQ(s[x + 2] - s[x + stride + 2]) + \
1292 SQ(s[x + 3] - s[x + stride + 3]); \
1302 #define VSSE(size) \
1303 static int vsse ## size ## _c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, \
1304 int stride, int h) \
1306 int score = 0, x, y; \
1308 for (y = 1; y < h; y++) { \
1309 for (x = 0; x < size; x++) \
1310 score += SQ(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]); \
1320 #define WRAPPER8_16_SQ(name8, name16) \
1321 static int name16(MpegEncContext *s, uint8_t *dst, uint8_t *src, \
1322 int stride, int h) \
1326 score += name8(s, dst, src, stride, 8); \
1327 score += name8(s, dst + 8, src + 8, stride, 8); \
1329 dst += 8 * stride; \
1330 src += 8 * stride; \
1331 score += name8(s, dst, src, stride, 8); \
1332 score += name8(s, dst + 8, src + 8, stride, 8); \
1337 WRAPPER8_16_SQ(hadamard8_diff8x8_c, hadamard8_diff16_c)
1338 WRAPPER8_16_SQ(hadamard8_intra8x8_c, hadamard8_intra16_c)
1339 WRAPPER8_16_SQ(dct_sad8x8_c, dct_sad16_c)
1341 WRAPPER8_16_SQ(dct264_sad8x8_c, dct264_sad16_c)
1343 WRAPPER8_16_SQ(dct_max8x8_c, dct_max16_c)
1344 WRAPPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c)
1345 WRAPPER8_16_SQ(rd8x8_c, rd16_c)
1346 WRAPPER8_16_SQ(bit8x8_c, bit16_c)
1348 static inline uint32_t clipf_c_one(uint32_t a, uint32_t mini,
1349 uint32_t maxi, uint32_t maxisign)
1353 else if ((a ^ (1U << 31)) > maxisign)
1359 static void vector_clipf_c_opposite_sign(float *dst, const float *src,
1360 float *min, float *max, int len)
1363 uint32_t mini = *(uint32_t *) min;
1364 uint32_t maxi = *(uint32_t *) max;
1365 uint32_t maxisign = maxi ^ (1U << 31);
1366 uint32_t *dsti = (uint32_t *) dst;
1367 const uint32_t *srci = (const uint32_t *) src;
1369 for (i = 0; i < len; i += 8) {
1370 dsti[i + 0] = clipf_c_one(srci[i + 0], mini, maxi, maxisign);
1371 dsti[i + 1] = clipf_c_one(srci[i + 1], mini, maxi, maxisign);
1372 dsti[i + 2] = clipf_c_one(srci[i + 2], mini, maxi, maxisign);
1373 dsti[i + 3] = clipf_c_one(srci[i + 3], mini, maxi, maxisign);
1374 dsti[i + 4] = clipf_c_one(srci[i + 4], mini, maxi, maxisign);
1375 dsti[i + 5] = clipf_c_one(srci[i + 5], mini, maxi, maxisign);
1376 dsti[i + 6] = clipf_c_one(srci[i + 6], mini, maxi, maxisign);
1377 dsti[i + 7] = clipf_c_one(srci[i + 7], mini, maxi, maxisign);
1381 static void vector_clipf_c(float *dst, const float *src,
1382 float min, float max, int len)
1386 if (min < 0 && max > 0) {
1387 vector_clipf_c_opposite_sign(dst, src, &min, &max, len);
1389 for (i = 0; i < len; i += 8) {
1390 dst[i] = av_clipf(src[i], min, max);
1391 dst[i + 1] = av_clipf(src[i + 1], min, max);
1392 dst[i + 2] = av_clipf(src[i + 2], min, max);
1393 dst[i + 3] = av_clipf(src[i + 3], min, max);
1394 dst[i + 4] = av_clipf(src[i + 4], min, max);
1395 dst[i + 5] = av_clipf(src[i + 5], min, max);
1396 dst[i + 6] = av_clipf(src[i + 6], min, max);
1397 dst[i + 7] = av_clipf(src[i + 7], min, max);
1402 static int32_t scalarproduct_int16_c(const int16_t *v1, const int16_t *v2,
1408 res += *v1++ **v2++;
1413 static void vector_clip_int32_c(int32_t *dst, const int32_t *src, int32_t min,
1414 int32_t max, unsigned int len)
1417 *dst++ = av_clip(*src++, min, max);
1418 *dst++ = av_clip(*src++, min, max);
1419 *dst++ = av_clip(*src++, min, max);
1420 *dst++ = av_clip(*src++, min, max);
1421 *dst++ = av_clip(*src++, min, max);
1422 *dst++ = av_clip(*src++, min, max);
1423 *dst++ = av_clip(*src++, min, max);
1424 *dst++ = av_clip(*src++, min, max);
1429 static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
1431 ff_j_rev_dct(block);
1432 put_pixels_clamped_c(block, dest, line_size);
1435 static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
1437 ff_j_rev_dct(block);
1438 add_pixels_clamped_c(block, dest, line_size);
1441 static void ff_jref_idct4_put(uint8_t *dest, int line_size, int16_t *block)
1443 ff_j_rev_dct4 (block);
1444 put_pixels_clamped4_c(block, dest, line_size);
1446 static void ff_jref_idct4_add(uint8_t *dest, int line_size, int16_t *block)
1448 ff_j_rev_dct4 (block);
1449 add_pixels_clamped4_c(block, dest, line_size);
1452 static void ff_jref_idct2_put(uint8_t *dest, int line_size, int16_t *block)
1454 ff_j_rev_dct2 (block);
1455 put_pixels_clamped2_c(block, dest, line_size);
1457 static void ff_jref_idct2_add(uint8_t *dest, int line_size, int16_t *block)
1459 ff_j_rev_dct2 (block);
1460 add_pixels_clamped2_c(block, dest, line_size);
1463 static void ff_jref_idct1_put(uint8_t *dest, int line_size, int16_t *block)
1465 dest[0] = av_clip_uint8((block[0] + 4)>>3);
1467 static void ff_jref_idct1_add(uint8_t *dest, int line_size, int16_t *block)
1469 dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
1472 /* draw the edges of width 'w' of an image of size width, height */
1473 // FIXME: Check that this is OK for MPEG-4 interlaced.
1474 static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
1475 int w, int h, int sides)
1477 uint8_t *ptr = buf, *last_line;
1480 /* left and right */
1481 for (i = 0; i < height; i++) {
1482 memset(ptr - w, ptr[0], w);
1483 memset(ptr + width, ptr[width - 1], w);
1487 /* top and bottom + corners */
1489 last_line = buf + (height - 1) * wrap;
1490 if (sides & EDGE_TOP)
1491 for (i = 0; i < h; i++)
1493 memcpy(buf - (i + 1) * wrap, buf, width + w + w);
1494 if (sides & EDGE_BOTTOM)
1495 for (i = 0; i < h; i++)
1497 memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
1500 /* init static data */
1501 av_cold void ff_dsputil_static_init(void)
1505 for (i = 0; i < 512; i++)
1506 ff_square_tab[i] = (i - 256) * (i - 256);
1509 int ff_check_alignment(void)
1511 static int did_fail = 0;
1512 LOCAL_ALIGNED_16(int, aligned, [4]);
1514 if ((intptr_t)aligned & 15) {
1516 #if HAVE_MMX || HAVE_ALTIVEC
1517 av_log(NULL, AV_LOG_ERROR,
1518 "Compiler did not align stack variables. Libavcodec has been miscompiled\n"
1519 "and may be very slow or crash. This is not a bug in libavcodec,\n"
1520 "but in the compiler. You may try recompiling using gcc >= 4.2.\n"
1521 "Do not report crashes to FFmpeg developers.\n");
1530 av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
1532 const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
1534 ff_check_alignment();
1537 if (avctx->bits_per_raw_sample == 10) {
1538 c->fdct = ff_jpeg_fdct_islow_10;
1539 c->fdct248 = ff_fdct248_islow_10;
1541 if (avctx->dct_algo == FF_DCT_FASTINT) {
1542 c->fdct = ff_fdct_ifast;
1543 c->fdct248 = ff_fdct_ifast248;
1544 } else if (avctx->dct_algo == FF_DCT_FAAN) {
1545 c->fdct = ff_faandct;
1546 c->fdct248 = ff_faandct248;
1548 c->fdct = ff_jpeg_fdct_islow_8; // slow/accurate/default
1549 c->fdct248 = ff_fdct248_islow_8;
1552 #endif /* CONFIG_ENCODERS */
1554 if (avctx->lowres==1) {
1555 c->idct_put = ff_jref_idct4_put;
1556 c->idct_add = ff_jref_idct4_add;
1557 c->idct = ff_j_rev_dct4;
1558 c->idct_permutation_type = FF_NO_IDCT_PERM;
1559 } else if (avctx->lowres==2) {
1560 c->idct_put = ff_jref_idct2_put;
1561 c->idct_add = ff_jref_idct2_add;
1562 c->idct = ff_j_rev_dct2;
1563 c->idct_permutation_type = FF_NO_IDCT_PERM;
1564 } else if (avctx->lowres==3) {
1565 c->idct_put = ff_jref_idct1_put;
1566 c->idct_add = ff_jref_idct1_add;
1567 c->idct = ff_j_rev_dct1;
1568 c->idct_permutation_type = FF_NO_IDCT_PERM;
1570 if (avctx->bits_per_raw_sample == 10) {
1571 c->idct_put = ff_simple_idct_put_10;
1572 c->idct_add = ff_simple_idct_add_10;
1573 c->idct = ff_simple_idct_10;
1574 c->idct_permutation_type = FF_NO_IDCT_PERM;
1575 } else if (avctx->bits_per_raw_sample == 12) {
1576 c->idct_put = ff_simple_idct_put_12;
1577 c->idct_add = ff_simple_idct_add_12;
1578 c->idct = ff_simple_idct_12;
1579 c->idct_permutation_type = FF_NO_IDCT_PERM;
1581 if (avctx->idct_algo == FF_IDCT_INT) {
1582 c->idct_put = jref_idct_put;
1583 c->idct_add = jref_idct_add;
1584 c->idct = ff_j_rev_dct;
1585 c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
1586 } else if (avctx->idct_algo == FF_IDCT_FAAN) {
1587 c->idct_put = ff_faanidct_put;
1588 c->idct_add = ff_faanidct_add;
1589 c->idct = ff_faanidct;
1590 c->idct_permutation_type = FF_NO_IDCT_PERM;
1591 } else { // accurate/default
1592 c->idct_put = ff_simple_idct_put_8;
1593 c->idct_add = ff_simple_idct_add_8;
1594 c->idct = ff_simple_idct_8;
1595 c->idct_permutation_type = FF_NO_IDCT_PERM;
1600 c->diff_pixels = diff_pixels_c;
1602 c->put_pixels_clamped = put_pixels_clamped_c;
1603 c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
1604 c->add_pixels_clamped = add_pixels_clamped_c;
1606 c->sum_abs_dctelem = sum_abs_dctelem_c;
1611 c->pix_sum = pix_sum_c;
1612 c->pix_norm1 = pix_norm1_c;
1614 /* TODO [0] 16 [1] 8 */
1615 c->pix_abs[0][0] = pix_abs16_c;
1616 c->pix_abs[0][1] = pix_abs16_x2_c;
1617 c->pix_abs[0][2] = pix_abs16_y2_c;
1618 c->pix_abs[0][3] = pix_abs16_xy2_c;
1619 c->pix_abs[1][0] = pix_abs8_c;
1620 c->pix_abs[1][1] = pix_abs8_x2_c;
1621 c->pix_abs[1][2] = pix_abs8_y2_c;
1622 c->pix_abs[1][3] = pix_abs8_xy2_c;
1624 #define SET_CMP_FUNC(name) \
1625 c->name[0] = name ## 16_c; \
1626 c->name[1] = name ## 8x8_c;
1628 SET_CMP_FUNC(hadamard8_diff)
1629 c->hadamard8_diff[4] = hadamard8_intra16_c;
1630 c->hadamard8_diff[5] = hadamard8_intra8x8_c;
1631 SET_CMP_FUNC(dct_sad)
1632 SET_CMP_FUNC(dct_max)
1634 SET_CMP_FUNC(dct264_sad)
1636 c->sad[0] = pix_abs16_c;
1637 c->sad[1] = pix_abs8_c;
1638 c->sse[0] = sse16_c;
1641 SET_CMP_FUNC(quant_psnr)
1644 c->vsad[0] = vsad16_c;
1645 c->vsad[1] = vsad8_c;
1646 c->vsad[4] = vsad_intra16_c;
1647 c->vsad[5] = vsad_intra8_c;
1648 c->vsse[0] = vsse16_c;
1649 c->vsse[1] = vsse8_c;
1650 c->vsse[4] = vsse_intra16_c;
1651 c->vsse[5] = vsse_intra8_c;
1652 c->nsse[0] = nsse16_c;
1653 c->nsse[1] = nsse8_c;
1654 #if CONFIG_SNOW_DECODER || CONFIG_SNOW_ENCODER
1655 ff_dsputil_init_dwt(c);
1658 c->bswap_buf = bswap_buf;
1659 c->bswap16_buf = bswap16_buf;
1661 c->try_8x8basis = try_8x8basis_c;
1662 c->add_8x8basis = add_8x8basis_c;
1664 c->scalarproduct_int16 = scalarproduct_int16_c;
1665 c->vector_clip_int32 = vector_clip_int32_c;
1666 c->vector_clipf = vector_clipf_c;
1668 c->shrink[0] = av_image_copy_plane;
1669 c->shrink[1] = ff_shrink22;
1670 c->shrink[2] = ff_shrink44;
1671 c->shrink[3] = ff_shrink88;
1673 c->draw_edges = draw_edges_8_c;
1675 switch (avctx->bits_per_raw_sample) {
1680 c->get_pixels = get_pixels_16_c;
1683 if (avctx->bits_per_raw_sample<=8 || avctx->codec_type != AVMEDIA_TYPE_VIDEO) {
1684 c->get_pixels = get_pixels_8_c;
1691 ff_dsputil_init_alpha(c, avctx);
1693 ff_dsputil_init_arm(c, avctx, high_bit_depth);
1695 ff_dsputil_init_ppc(c, avctx, high_bit_depth);
1697 ff_dsputil_init_x86(c, avctx, high_bit_depth);
1699 ff_init_scantable_permutation(c->idct_permutation,
1700 c->idct_permutation_type);
1703 av_cold void dsputil_init(DSPContext* c, AVCodecContext *avctx)
1705 ff_dsputil_init(c, avctx);
1708 av_cold void avpriv_dsputil_init(DSPContext *c, AVCodecContext *avctx)
1710 ff_dsputil_init(c, avctx);