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 static void fill_block16_c(uint8_t *block, uint8_t value, int line_size, int h)
443 for (i = 0; i < h; i++) {
444 memset(block, value, 16);
449 static void fill_block8_c(uint8_t *block, uint8_t value, int line_size, int h)
453 for (i = 0; i < h; i++) {
454 memset(block, value, 8);
459 #define avg2(a, b) ((a + b + 1) >> 1)
460 #define avg4(a, b, c, d) ((a + b + c + d + 2) >> 2)
462 static void gmc1_c(uint8_t *dst, uint8_t *src, int stride, int h,
463 int x16, int y16, int rounder)
465 const int A = (16 - x16) * (16 - y16);
466 const int B = (x16) * (16 - y16);
467 const int C = (16 - x16) * (y16);
468 const int D = (x16) * (y16);
471 for (i = 0; i < h; i++) {
472 dst[0] = (A * src[0] + B * src[1] + C * src[stride + 0] + D * src[stride + 1] + rounder) >> 8;
473 dst[1] = (A * src[1] + B * src[2] + C * src[stride + 1] + D * src[stride + 2] + rounder) >> 8;
474 dst[2] = (A * src[2] + B * src[3] + C * src[stride + 2] + D * src[stride + 3] + rounder) >> 8;
475 dst[3] = (A * src[3] + B * src[4] + C * src[stride + 3] + D * src[stride + 4] + rounder) >> 8;
476 dst[4] = (A * src[4] + B * src[5] + C * src[stride + 4] + D * src[stride + 5] + rounder) >> 8;
477 dst[5] = (A * src[5] + B * src[6] + C * src[stride + 5] + D * src[stride + 6] + rounder) >> 8;
478 dst[6] = (A * src[6] + B * src[7] + C * src[stride + 6] + D * src[stride + 7] + rounder) >> 8;
479 dst[7] = (A * src[7] + B * src[8] + C * src[stride + 7] + D * src[stride + 8] + rounder) >> 8;
485 void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
486 int dxx, int dxy, int dyx, int dyy, int shift, int r,
487 int width, int height)
490 const int s = 1 << shift;
495 for (y = 0; y < h; y++) {
500 for (x = 0; x < 8; x++) { // FIXME: optimize
502 int src_x = vx >> 16;
503 int src_y = vy >> 16;
504 int frac_x = src_x & (s - 1);
505 int frac_y = src_y & (s - 1);
510 if ((unsigned) src_x < width) {
511 if ((unsigned) src_y < height) {
512 index = src_x + src_y * stride;
513 dst[y * stride + x] =
514 ((src[index] * (s - frac_x) +
515 src[index + 1] * frac_x) * (s - frac_y) +
516 (src[index + stride] * (s - frac_x) +
517 src[index + stride + 1] * frac_x) * frac_y +
520 index = src_x + av_clip(src_y, 0, height) * stride;
521 dst[y * stride + x] =
522 ((src[index] * (s - frac_x) +
523 src[index + 1] * frac_x) * s +
527 if ((unsigned) src_y < height) {
528 index = av_clip(src_x, 0, width) + src_y * stride;
529 dst[y * stride + x] =
530 ((src[index] * (s - frac_y) +
531 src[index + stride] * frac_y) * s +
534 index = av_clip(src_x, 0, width) +
535 av_clip(src_y, 0, height) * stride;
536 dst[y * stride + x] = src[index];
548 static inline int pix_abs16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
549 int line_size, int h)
553 for (i = 0; i < h; i++) {
554 s += abs(pix1[0] - pix2[0]);
555 s += abs(pix1[1] - pix2[1]);
556 s += abs(pix1[2] - pix2[2]);
557 s += abs(pix1[3] - pix2[3]);
558 s += abs(pix1[4] - pix2[4]);
559 s += abs(pix1[5] - pix2[5]);
560 s += abs(pix1[6] - pix2[6]);
561 s += abs(pix1[7] - pix2[7]);
562 s += abs(pix1[8] - pix2[8]);
563 s += abs(pix1[9] - pix2[9]);
564 s += abs(pix1[10] - pix2[10]);
565 s += abs(pix1[11] - pix2[11]);
566 s += abs(pix1[12] - pix2[12]);
567 s += abs(pix1[13] - pix2[13]);
568 s += abs(pix1[14] - pix2[14]);
569 s += abs(pix1[15] - pix2[15]);
576 static int pix_abs16_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
577 int line_size, int h)
581 for (i = 0; i < h; i++) {
582 s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
583 s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
584 s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
585 s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
586 s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
587 s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
588 s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
589 s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
590 s += abs(pix1[8] - avg2(pix2[8], pix2[9]));
591 s += abs(pix1[9] - avg2(pix2[9], pix2[10]));
592 s += abs(pix1[10] - avg2(pix2[10], pix2[11]));
593 s += abs(pix1[11] - avg2(pix2[11], pix2[12]));
594 s += abs(pix1[12] - avg2(pix2[12], pix2[13]));
595 s += abs(pix1[13] - avg2(pix2[13], pix2[14]));
596 s += abs(pix1[14] - avg2(pix2[14], pix2[15]));
597 s += abs(pix1[15] - avg2(pix2[15], pix2[16]));
604 static int pix_abs16_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
605 int line_size, int h)
608 uint8_t *pix3 = pix2 + line_size;
610 for (i = 0; i < h; i++) {
611 s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
612 s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
613 s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
614 s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
615 s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
616 s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
617 s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
618 s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
619 s += abs(pix1[8] - avg2(pix2[8], pix3[8]));
620 s += abs(pix1[9] - avg2(pix2[9], pix3[9]));
621 s += abs(pix1[10] - avg2(pix2[10], pix3[10]));
622 s += abs(pix1[11] - avg2(pix2[11], pix3[11]));
623 s += abs(pix1[12] - avg2(pix2[12], pix3[12]));
624 s += abs(pix1[13] - avg2(pix2[13], pix3[13]));
625 s += abs(pix1[14] - avg2(pix2[14], pix3[14]));
626 s += abs(pix1[15] - avg2(pix2[15], pix3[15]));
634 static int pix_abs16_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
635 int line_size, int h)
638 uint8_t *pix3 = pix2 + line_size;
640 for (i = 0; i < h; i++) {
641 s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
642 s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
643 s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
644 s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
645 s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
646 s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
647 s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
648 s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
649 s += abs(pix1[8] - avg4(pix2[8], pix2[9], pix3[8], pix3[9]));
650 s += abs(pix1[9] - avg4(pix2[9], pix2[10], pix3[9], pix3[10]));
651 s += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
652 s += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
653 s += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
654 s += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
655 s += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
656 s += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
664 static inline int pix_abs8_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] - pix2[0]);
671 s += abs(pix1[1] - pix2[1]);
672 s += abs(pix1[2] - pix2[2]);
673 s += abs(pix1[3] - pix2[3]);
674 s += abs(pix1[4] - pix2[4]);
675 s += abs(pix1[5] - pix2[5]);
676 s += abs(pix1[6] - pix2[6]);
677 s += abs(pix1[7] - pix2[7]);
684 static int pix_abs8_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
685 int line_size, int h)
689 for (i = 0; i < h; i++) {
690 s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
691 s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
692 s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
693 s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
694 s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
695 s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
696 s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
697 s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
704 static int pix_abs8_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
705 int line_size, int h)
708 uint8_t *pix3 = pix2 + line_size;
710 for (i = 0; i < h; i++) {
711 s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
712 s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
713 s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
714 s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
715 s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
716 s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
717 s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
718 s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
726 static int pix_abs8_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
727 int line_size, int h)
730 uint8_t *pix3 = pix2 + line_size;
732 for (i = 0; i < h; i++) {
733 s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
734 s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
735 s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
736 s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
737 s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
738 s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
739 s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
740 s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
748 static int nsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
750 int score1 = 0, score2 = 0, x, y;
752 for (y = 0; y < h; y++) {
753 for (x = 0; x < 16; x++)
754 score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
756 for (x = 0; x < 15; x++)
757 score2 += FFABS(s1[x] - s1[x + stride] -
758 s1[x + 1] + s1[x + stride + 1]) -
759 FFABS(s2[x] - s2[x + stride] -
760 s2[x + 1] + s2[x + stride + 1]);
767 return score1 + FFABS(score2) * c->avctx->nsse_weight;
769 return score1 + FFABS(score2) * 8;
772 static int nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
774 int score1 = 0, score2 = 0, x, y;
776 for (y = 0; y < h; y++) {
777 for (x = 0; x < 8; x++)
778 score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
780 for (x = 0; x < 7; x++)
781 score2 += FFABS(s1[x] - s1[x + stride] -
782 s1[x + 1] + s1[x + stride + 1]) -
783 FFABS(s2[x] - s2[x + stride] -
784 s2[x + 1] + s2[x + stride + 1]);
791 return score1 + FFABS(score2) * c->avctx->nsse_weight;
793 return score1 + FFABS(score2) * 8;
796 static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
797 int16_t basis[64], int scale)
800 unsigned int sum = 0;
802 for (i = 0; i < 8 * 8; i++) {
803 int b = rem[i] + ((basis[i] * scale +
804 (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
805 (BASIS_SHIFT - RECON_SHIFT));
808 av_assert2(-512 < b && b < 512);
810 sum += (w * b) * (w * b) >> 4;
815 static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
819 for (i = 0; i < 8 * 8; i++)
820 rem[i] += (basis[i] * scale +
821 (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
822 (BASIS_SHIFT - RECON_SHIFT);
825 static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
831 void ff_set_cmp(DSPContext *c, me_cmp_func *cmp, int type)
835 memset(cmp, 0, sizeof(void *) * 6);
837 for (i = 0; i < 6; i++) {
838 switch (type & 0xFF) {
843 cmp[i] = c->hadamard8_diff[i];
849 cmp[i] = c->dct_sad[i];
852 cmp[i] = c->dct264_sad[i];
855 cmp[i] = c->dct_max[i];
858 cmp[i] = c->quant_psnr[i];
887 av_log(NULL, AV_LOG_ERROR,
888 "internal error in cmp function selection\n");
893 #define BUTTERFLY2(o1, o2, i1, i2) \
897 #define BUTTERFLY1(x, y) \
906 #define BUTTERFLYA(x, y) (FFABS((x) + (y)) + FFABS((x) - (y)))
908 static int hadamard8_diff8x8_c(MpegEncContext *s, uint8_t *dst,
909 uint8_t *src, int stride, int h)
911 int i, temp[64], sum = 0;
915 for (i = 0; i < 8; i++) {
916 // FIXME: try pointer walks
917 BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
918 src[stride * i + 0] - dst[stride * i + 0],
919 src[stride * i + 1] - dst[stride * i + 1]);
920 BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
921 src[stride * i + 2] - dst[stride * i + 2],
922 src[stride * i + 3] - dst[stride * i + 3]);
923 BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
924 src[stride * i + 4] - dst[stride * i + 4],
925 src[stride * i + 5] - dst[stride * i + 5]);
926 BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
927 src[stride * i + 6] - dst[stride * i + 6],
928 src[stride * i + 7] - dst[stride * i + 7]);
930 BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
931 BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
932 BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
933 BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
935 BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
936 BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
937 BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
938 BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
941 for (i = 0; i < 8; i++) {
942 BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
943 BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
944 BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
945 BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
947 BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
948 BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
949 BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
950 BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
952 sum += BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i]) +
953 BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i]) +
954 BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i]) +
955 BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
960 static int hadamard8_intra8x8_c(MpegEncContext *s, uint8_t *src,
961 uint8_t *dummy, int stride, int h)
963 int i, temp[64], sum = 0;
967 for (i = 0; i < 8; i++) {
968 // FIXME: try pointer walks
969 BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
970 src[stride * i + 0], src[stride * i + 1]);
971 BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
972 src[stride * i + 2], src[stride * i + 3]);
973 BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
974 src[stride * i + 4], src[stride * i + 5]);
975 BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
976 src[stride * i + 6], src[stride * i + 7]);
978 BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
979 BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
980 BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
981 BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
983 BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
984 BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
985 BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
986 BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
989 for (i = 0; i < 8; i++) {
990 BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
991 BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
992 BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
993 BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
995 BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
996 BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
997 BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
998 BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
1001 BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i])
1002 + BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i])
1003 + BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i])
1004 + BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
1007 sum -= FFABS(temp[8 * 0] + temp[8 * 4]); // -mean
1012 static int dct_sad8x8_c(MpegEncContext *s, uint8_t *src1,
1013 uint8_t *src2, int stride, int h)
1015 LOCAL_ALIGNED_16(int16_t, temp, [64]);
1019 s->dsp.diff_pixels(temp, src1, src2, stride);
1021 return s->dsp.sum_abs_dctelem(temp);
1027 const int s07 = SRC(0) + SRC(7); \
1028 const int s16 = SRC(1) + SRC(6); \
1029 const int s25 = SRC(2) + SRC(5); \
1030 const int s34 = SRC(3) + SRC(4); \
1031 const int a0 = s07 + s34; \
1032 const int a1 = s16 + s25; \
1033 const int a2 = s07 - s34; \
1034 const int a3 = s16 - s25; \
1035 const int d07 = SRC(0) - SRC(7); \
1036 const int d16 = SRC(1) - SRC(6); \
1037 const int d25 = SRC(2) - SRC(5); \
1038 const int d34 = SRC(3) - SRC(4); \
1039 const int a4 = d16 + d25 + (d07 + (d07 >> 1)); \
1040 const int a5 = d07 - d34 - (d25 + (d25 >> 1)); \
1041 const int a6 = d07 + d34 - (d16 + (d16 >> 1)); \
1042 const int a7 = d16 - d25 + (d34 + (d34 >> 1)); \
1044 DST(1, a4 + (a7 >> 2)); \
1045 DST(2, a2 + (a3 >> 1)); \
1046 DST(3, a5 + (a6 >> 2)); \
1048 DST(5, a6 - (a5 >> 2)); \
1049 DST(6, (a2 >> 1) - a3); \
1050 DST(7, (a4 >> 2) - a7); \
1053 static int dct264_sad8x8_c(MpegEncContext *s, uint8_t *src1,
1054 uint8_t *src2, int stride, int h)
1059 s->dsp.diff_pixels(dct[0], src1, src2, stride);
1061 #define SRC(x) dct[i][x]
1062 #define DST(x, v) dct[i][x] = v
1063 for (i = 0; i < 8; i++)
1068 #define SRC(x) dct[x][i]
1069 #define DST(x, v) sum += FFABS(v)
1070 for (i = 0; i < 8; i++)
1078 static int dct_max8x8_c(MpegEncContext *s, uint8_t *src1,
1079 uint8_t *src2, int stride, int h)
1081 LOCAL_ALIGNED_16(int16_t, temp, [64]);
1086 s->dsp.diff_pixels(temp, src1, src2, stride);
1089 for (i = 0; i < 64; i++)
1090 sum = FFMAX(sum, FFABS(temp[i]));
1095 static int quant_psnr8x8_c(MpegEncContext *s, uint8_t *src1,
1096 uint8_t *src2, int stride, int h)
1098 LOCAL_ALIGNED_16(int16_t, temp, [64 * 2]);
1099 int16_t *const bak = temp + 64;
1105 s->dsp.diff_pixels(temp, src1, src2, stride);
1107 memcpy(bak, temp, 64 * sizeof(int16_t));
1109 s->block_last_index[0 /* FIXME */] =
1110 s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1111 s->dct_unquantize_inter(s, temp, 0, s->qscale);
1112 ff_simple_idct_8(temp); // FIXME
1114 for (i = 0; i < 64; i++)
1115 sum += (temp[i] - bak[i]) * (temp[i] - bak[i]);
1120 static int rd8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
1123 const uint8_t *scantable = s->intra_scantable.permutated;
1124 LOCAL_ALIGNED_16(int16_t, temp, [64]);
1125 LOCAL_ALIGNED_16(uint8_t, lsrc1, [64]);
1126 LOCAL_ALIGNED_16(uint8_t, lsrc2, [64]);
1127 int i, last, run, bits, level, distortion, start_i;
1128 const int esc_length = s->ac_esc_length;
1129 uint8_t *length, *last_length;
1133 copy_block8(lsrc1, src1, 8, stride, 8);
1134 copy_block8(lsrc2, src2, 8, stride, 8);
1136 s->dsp.diff_pixels(temp, lsrc1, lsrc2, 8);
1138 s->block_last_index[0 /* FIXME */] =
1140 s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1146 length = s->intra_ac_vlc_length;
1147 last_length = s->intra_ac_vlc_last_length;
1148 bits += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
1151 length = s->inter_ac_vlc_length;
1152 last_length = s->inter_ac_vlc_last_length;
1155 if (last >= start_i) {
1157 for (i = start_i; i < last; i++) {
1158 int j = scantable[i];
1163 if ((level & (~127)) == 0)
1164 bits += length[UNI_AC_ENC_INDEX(run, level)];
1171 i = scantable[last];
1173 level = temp[i] + 64;
1175 av_assert2(level - 64);
1177 if ((level & (~127)) == 0) {
1178 bits += last_length[UNI_AC_ENC_INDEX(run, level)];
1185 s->dct_unquantize_intra(s, temp, 0, s->qscale);
1187 s->dct_unquantize_inter(s, temp, 0, s->qscale);
1190 s->dsp.idct_add(lsrc2, 8, temp);
1192 distortion = s->dsp.sse[1](NULL, lsrc2, lsrc1, 8, 8);
1194 return distortion + ((bits * s->qscale * s->qscale * 109 + 64) >> 7);
1197 static int bit8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
1200 const uint8_t *scantable = s->intra_scantable.permutated;
1201 LOCAL_ALIGNED_16(int16_t, temp, [64]);
1202 int i, last, run, bits, level, start_i;
1203 const int esc_length = s->ac_esc_length;
1204 uint8_t *length, *last_length;
1208 s->dsp.diff_pixels(temp, src1, src2, stride);
1210 s->block_last_index[0 /* FIXME */] =
1212 s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1218 length = s->intra_ac_vlc_length;
1219 last_length = s->intra_ac_vlc_last_length;
1220 bits += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
1223 length = s->inter_ac_vlc_length;
1224 last_length = s->inter_ac_vlc_last_length;
1227 if (last >= start_i) {
1229 for (i = start_i; i < last; i++) {
1230 int j = scantable[i];
1235 if ((level & (~127)) == 0)
1236 bits += length[UNI_AC_ENC_INDEX(run, level)];
1243 i = scantable[last];
1245 level = temp[i] + 64;
1247 av_assert2(level - 64);
1249 if ((level & (~127)) == 0)
1250 bits += last_length[UNI_AC_ENC_INDEX(run, level)];
1258 #define VSAD_INTRA(size) \
1259 static int vsad_intra ## size ## _c(MpegEncContext *c, \
1260 uint8_t *s, uint8_t *dummy, \
1261 int stride, int h) \
1263 int score = 0, x, y; \
1265 for (y = 1; y < h; y++) { \
1266 for (x = 0; x < size; x += 4) { \
1267 score += FFABS(s[x] - s[x + stride]) + \
1268 FFABS(s[x + 1] - s[x + stride + 1]) + \
1269 FFABS(s[x + 2] - s[x + 2 + stride]) + \
1270 FFABS(s[x + 3] - s[x + 3 + stride]); \
1280 #define VSAD(size) \
1281 static int vsad ## size ## _c(MpegEncContext *c, \
1282 uint8_t *s1, uint8_t *s2, \
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++) \
1289 score += FFABS(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]); \
1299 #define SQ(a) ((a) * (a))
1300 #define VSSE_INTRA(size) \
1301 static int vsse_intra ## size ## _c(MpegEncContext *c, \
1302 uint8_t *s, uint8_t *dummy, \
1303 int stride, int h) \
1305 int score = 0, x, y; \
1307 for (y = 1; y < h; y++) { \
1308 for (x = 0; x < size; x += 4) { \
1309 score += SQ(s[x] - s[x + stride]) + \
1310 SQ(s[x + 1] - s[x + stride + 1]) + \
1311 SQ(s[x + 2] - s[x + stride + 2]) + \
1312 SQ(s[x + 3] - s[x + stride + 3]); \
1322 #define VSSE(size) \
1323 static int vsse ## size ## _c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, \
1324 int stride, int h) \
1326 int score = 0, x, y; \
1328 for (y = 1; y < h; y++) { \
1329 for (x = 0; x < size; x++) \
1330 score += SQ(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]); \
1340 #define WRAPPER8_16_SQ(name8, name16) \
1341 static int name16(MpegEncContext *s, uint8_t *dst, uint8_t *src, \
1342 int stride, int h) \
1346 score += name8(s, dst, src, stride, 8); \
1347 score += name8(s, dst + 8, src + 8, stride, 8); \
1349 dst += 8 * stride; \
1350 src += 8 * stride; \
1351 score += name8(s, dst, src, stride, 8); \
1352 score += name8(s, dst + 8, src + 8, stride, 8); \
1357 WRAPPER8_16_SQ(hadamard8_diff8x8_c, hadamard8_diff16_c)
1358 WRAPPER8_16_SQ(hadamard8_intra8x8_c, hadamard8_intra16_c)
1359 WRAPPER8_16_SQ(dct_sad8x8_c, dct_sad16_c)
1361 WRAPPER8_16_SQ(dct264_sad8x8_c, dct264_sad16_c)
1363 WRAPPER8_16_SQ(dct_max8x8_c, dct_max16_c)
1364 WRAPPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c)
1365 WRAPPER8_16_SQ(rd8x8_c, rd16_c)
1366 WRAPPER8_16_SQ(bit8x8_c, bit16_c)
1368 static inline uint32_t clipf_c_one(uint32_t a, uint32_t mini,
1369 uint32_t maxi, uint32_t maxisign)
1373 else if ((a ^ (1U << 31)) > maxisign)
1379 static void vector_clipf_c_opposite_sign(float *dst, const float *src,
1380 float *min, float *max, int len)
1383 uint32_t mini = *(uint32_t *) min;
1384 uint32_t maxi = *(uint32_t *) max;
1385 uint32_t maxisign = maxi ^ (1U << 31);
1386 uint32_t *dsti = (uint32_t *) dst;
1387 const uint32_t *srci = (const uint32_t *) src;
1389 for (i = 0; i < len; i += 8) {
1390 dsti[i + 0] = clipf_c_one(srci[i + 0], mini, maxi, maxisign);
1391 dsti[i + 1] = clipf_c_one(srci[i + 1], mini, maxi, maxisign);
1392 dsti[i + 2] = clipf_c_one(srci[i + 2], mini, maxi, maxisign);
1393 dsti[i + 3] = clipf_c_one(srci[i + 3], mini, maxi, maxisign);
1394 dsti[i + 4] = clipf_c_one(srci[i + 4], mini, maxi, maxisign);
1395 dsti[i + 5] = clipf_c_one(srci[i + 5], mini, maxi, maxisign);
1396 dsti[i + 6] = clipf_c_one(srci[i + 6], mini, maxi, maxisign);
1397 dsti[i + 7] = clipf_c_one(srci[i + 7], mini, maxi, maxisign);
1401 static void vector_clipf_c(float *dst, const float *src,
1402 float min, float max, int len)
1406 if (min < 0 && max > 0) {
1407 vector_clipf_c_opposite_sign(dst, src, &min, &max, len);
1409 for (i = 0; i < len; i += 8) {
1410 dst[i] = av_clipf(src[i], min, max);
1411 dst[i + 1] = av_clipf(src[i + 1], min, max);
1412 dst[i + 2] = av_clipf(src[i + 2], min, max);
1413 dst[i + 3] = av_clipf(src[i + 3], min, max);
1414 dst[i + 4] = av_clipf(src[i + 4], min, max);
1415 dst[i + 5] = av_clipf(src[i + 5], min, max);
1416 dst[i + 6] = av_clipf(src[i + 6], min, max);
1417 dst[i + 7] = av_clipf(src[i + 7], min, max);
1422 static int32_t scalarproduct_int16_c(const int16_t *v1, const int16_t *v2,
1428 res += *v1++ **v2++;
1433 static void vector_clip_int32_c(int32_t *dst, const int32_t *src, int32_t min,
1434 int32_t max, unsigned int len)
1437 *dst++ = av_clip(*src++, min, max);
1438 *dst++ = av_clip(*src++, min, max);
1439 *dst++ = av_clip(*src++, min, max);
1440 *dst++ = av_clip(*src++, min, max);
1441 *dst++ = av_clip(*src++, min, max);
1442 *dst++ = av_clip(*src++, min, max);
1443 *dst++ = av_clip(*src++, min, max);
1444 *dst++ = av_clip(*src++, min, max);
1449 static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
1451 ff_j_rev_dct(block);
1452 put_pixels_clamped_c(block, dest, line_size);
1455 static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
1457 ff_j_rev_dct(block);
1458 add_pixels_clamped_c(block, dest, line_size);
1461 static void ff_jref_idct4_put(uint8_t *dest, int line_size, int16_t *block)
1463 ff_j_rev_dct4 (block);
1464 put_pixels_clamped4_c(block, dest, line_size);
1466 static void ff_jref_idct4_add(uint8_t *dest, int line_size, int16_t *block)
1468 ff_j_rev_dct4 (block);
1469 add_pixels_clamped4_c(block, dest, line_size);
1472 static void ff_jref_idct2_put(uint8_t *dest, int line_size, int16_t *block)
1474 ff_j_rev_dct2 (block);
1475 put_pixels_clamped2_c(block, dest, line_size);
1477 static void ff_jref_idct2_add(uint8_t *dest, int line_size, int16_t *block)
1479 ff_j_rev_dct2 (block);
1480 add_pixels_clamped2_c(block, dest, line_size);
1483 static void ff_jref_idct1_put(uint8_t *dest, int line_size, int16_t *block)
1485 dest[0] = av_clip_uint8((block[0] + 4)>>3);
1487 static void ff_jref_idct1_add(uint8_t *dest, int line_size, int16_t *block)
1489 dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
1492 /* draw the edges of width 'w' of an image of size width, height */
1493 // FIXME: Check that this is OK for MPEG-4 interlaced.
1494 static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
1495 int w, int h, int sides)
1497 uint8_t *ptr = buf, *last_line;
1500 /* left and right */
1501 for (i = 0; i < height; i++) {
1502 memset(ptr - w, ptr[0], w);
1503 memset(ptr + width, ptr[width - 1], w);
1507 /* top and bottom + corners */
1509 last_line = buf + (height - 1) * wrap;
1510 if (sides & EDGE_TOP)
1511 for (i = 0; i < h; i++)
1513 memcpy(buf - (i + 1) * wrap, buf, width + w + w);
1514 if (sides & EDGE_BOTTOM)
1515 for (i = 0; i < h; i++)
1517 memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
1520 static void clear_block_8_c(int16_t *block)
1522 memset(block, 0, sizeof(int16_t) * 64);
1525 static void clear_blocks_8_c(int16_t *blocks)
1527 memset(blocks, 0, sizeof(int16_t) * 6 * 64);
1530 /* init static data */
1531 av_cold void ff_dsputil_static_init(void)
1535 for (i = 0; i < 512; i++)
1536 ff_square_tab[i] = (i - 256) * (i - 256);
1539 int ff_check_alignment(void)
1541 static int did_fail = 0;
1542 LOCAL_ALIGNED_16(int, aligned, [4]);
1544 if ((intptr_t)aligned & 15) {
1546 #if HAVE_MMX || HAVE_ALTIVEC
1547 av_log(NULL, AV_LOG_ERROR,
1548 "Compiler did not align stack variables. Libavcodec has been miscompiled\n"
1549 "and may be very slow or crash. This is not a bug in libavcodec,\n"
1550 "but in the compiler. You may try recompiling using gcc >= 4.2.\n"
1551 "Do not report crashes to FFmpeg developers.\n");
1560 av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
1562 const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
1564 ff_check_alignment();
1567 if (avctx->bits_per_raw_sample == 10) {
1568 c->fdct = ff_jpeg_fdct_islow_10;
1569 c->fdct248 = ff_fdct248_islow_10;
1571 if (avctx->dct_algo == FF_DCT_FASTINT) {
1572 c->fdct = ff_fdct_ifast;
1573 c->fdct248 = ff_fdct_ifast248;
1574 } else if (avctx->dct_algo == FF_DCT_FAAN) {
1575 c->fdct = ff_faandct;
1576 c->fdct248 = ff_faandct248;
1578 c->fdct = ff_jpeg_fdct_islow_8; // slow/accurate/default
1579 c->fdct248 = ff_fdct248_islow_8;
1582 #endif /* CONFIG_ENCODERS */
1584 if (avctx->lowres==1) {
1585 c->idct_put = ff_jref_idct4_put;
1586 c->idct_add = ff_jref_idct4_add;
1587 c->idct = ff_j_rev_dct4;
1588 c->idct_permutation_type = FF_NO_IDCT_PERM;
1589 } else if (avctx->lowres==2) {
1590 c->idct_put = ff_jref_idct2_put;
1591 c->idct_add = ff_jref_idct2_add;
1592 c->idct = ff_j_rev_dct2;
1593 c->idct_permutation_type = FF_NO_IDCT_PERM;
1594 } else if (avctx->lowres==3) {
1595 c->idct_put = ff_jref_idct1_put;
1596 c->idct_add = ff_jref_idct1_add;
1597 c->idct = ff_j_rev_dct1;
1598 c->idct_permutation_type = FF_NO_IDCT_PERM;
1600 if (avctx->bits_per_raw_sample == 10) {
1601 c->idct_put = ff_simple_idct_put_10;
1602 c->idct_add = ff_simple_idct_add_10;
1603 c->idct = ff_simple_idct_10;
1604 c->idct_permutation_type = FF_NO_IDCT_PERM;
1605 } else if (avctx->bits_per_raw_sample == 12) {
1606 c->idct_put = ff_simple_idct_put_12;
1607 c->idct_add = ff_simple_idct_add_12;
1608 c->idct = ff_simple_idct_12;
1609 c->idct_permutation_type = FF_NO_IDCT_PERM;
1611 if (avctx->idct_algo == FF_IDCT_INT) {
1612 c->idct_put = jref_idct_put;
1613 c->idct_add = jref_idct_add;
1614 c->idct = ff_j_rev_dct;
1615 c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
1616 } else if (avctx->idct_algo == FF_IDCT_FAAN) {
1617 c->idct_put = ff_faanidct_put;
1618 c->idct_add = ff_faanidct_add;
1619 c->idct = ff_faanidct;
1620 c->idct_permutation_type = FF_NO_IDCT_PERM;
1621 } else { // accurate/default
1622 c->idct_put = ff_simple_idct_put_8;
1623 c->idct_add = ff_simple_idct_add_8;
1624 c->idct = ff_simple_idct_8;
1625 c->idct_permutation_type = FF_NO_IDCT_PERM;
1630 c->diff_pixels = diff_pixels_c;
1632 c->put_pixels_clamped = put_pixels_clamped_c;
1633 c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
1634 c->add_pixels_clamped = add_pixels_clamped_c;
1636 c->sum_abs_dctelem = sum_abs_dctelem_c;
1641 c->pix_sum = pix_sum_c;
1642 c->pix_norm1 = pix_norm1_c;
1644 c->fill_block_tab[0] = fill_block16_c;
1645 c->fill_block_tab[1] = fill_block8_c;
1647 /* TODO [0] 16 [1] 8 */
1648 c->pix_abs[0][0] = pix_abs16_c;
1649 c->pix_abs[0][1] = pix_abs16_x2_c;
1650 c->pix_abs[0][2] = pix_abs16_y2_c;
1651 c->pix_abs[0][3] = pix_abs16_xy2_c;
1652 c->pix_abs[1][0] = pix_abs8_c;
1653 c->pix_abs[1][1] = pix_abs8_x2_c;
1654 c->pix_abs[1][2] = pix_abs8_y2_c;
1655 c->pix_abs[1][3] = pix_abs8_xy2_c;
1657 #define SET_CMP_FUNC(name) \
1658 c->name[0] = name ## 16_c; \
1659 c->name[1] = name ## 8x8_c;
1661 SET_CMP_FUNC(hadamard8_diff)
1662 c->hadamard8_diff[4] = hadamard8_intra16_c;
1663 c->hadamard8_diff[5] = hadamard8_intra8x8_c;
1664 SET_CMP_FUNC(dct_sad)
1665 SET_CMP_FUNC(dct_max)
1667 SET_CMP_FUNC(dct264_sad)
1669 c->sad[0] = pix_abs16_c;
1670 c->sad[1] = pix_abs8_c;
1671 c->sse[0] = sse16_c;
1674 SET_CMP_FUNC(quant_psnr)
1677 c->vsad[0] = vsad16_c;
1678 c->vsad[1] = vsad8_c;
1679 c->vsad[4] = vsad_intra16_c;
1680 c->vsad[5] = vsad_intra8_c;
1681 c->vsse[0] = vsse16_c;
1682 c->vsse[1] = vsse8_c;
1683 c->vsse[4] = vsse_intra16_c;
1684 c->vsse[5] = vsse_intra8_c;
1685 c->nsse[0] = nsse16_c;
1686 c->nsse[1] = nsse8_c;
1687 #if CONFIG_SNOW_DECODER || CONFIG_SNOW_ENCODER
1688 ff_dsputil_init_dwt(c);
1691 c->bswap_buf = bswap_buf;
1692 c->bswap16_buf = bswap16_buf;
1694 c->try_8x8basis = try_8x8basis_c;
1695 c->add_8x8basis = add_8x8basis_c;
1697 c->scalarproduct_int16 = scalarproduct_int16_c;
1698 c->vector_clip_int32 = vector_clip_int32_c;
1699 c->vector_clipf = vector_clipf_c;
1701 c->shrink[0] = av_image_copy_plane;
1702 c->shrink[1] = ff_shrink22;
1703 c->shrink[2] = ff_shrink44;
1704 c->shrink[3] = ff_shrink88;
1706 c->draw_edges = draw_edges_8_c;
1708 c->clear_block = clear_block_8_c;
1709 c->clear_blocks = clear_blocks_8_c;
1711 switch (avctx->bits_per_raw_sample) {
1716 c->get_pixels = get_pixels_16_c;
1719 if (avctx->bits_per_raw_sample<=8 || avctx->codec_type != AVMEDIA_TYPE_VIDEO) {
1720 c->get_pixels = get_pixels_8_c;
1727 ff_dsputil_init_alpha(c, avctx);
1729 ff_dsputil_init_arm(c, avctx, high_bit_depth);
1731 ff_dsputil_init_ppc(c, avctx, high_bit_depth);
1733 ff_dsputil_init_x86(c, avctx, high_bit_depth);
1735 ff_init_scantable_permutation(c->idct_permutation,
1736 c->idct_permutation_type);
1739 av_cold void dsputil_init(DSPContext* c, AVCodecContext *avctx)
1741 ff_dsputil_init(c, avctx);
1744 av_cold void avpriv_dsputil_init(DSPContext *c, AVCodecContext *avctx)
1746 ff_dsputil_init(c, avctx);