b81ba47521ee8fa9124153a30d52648e33a8cf7e
[ffmpeg.git] / libavcodec / dsputil.c
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 Libav.
9  *
10  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 /**
26  * @file
27  * DSP utils
28  */
29
30 #include "libavutil/attributes.h"
31 #include "libavutil/imgutils.h"
32 #include "avcodec.h"
33 #include "copy_block.h"
34 #include "dct.h"
35 #include "dsputil.h"
36 #include "simple_idct.h"
37 #include "faandct.h"
38 #include "faanidct.h"
39 #include "imgconvert.h"
40 #include "mathops.h"
41 #include "mpegvideo.h"
42 #include "config.h"
43
44 uint32_t ff_square_tab[512] = { 0, };
45
46 #define BIT_DEPTH 16
47 #include "dsputil_template.c"
48 #undef BIT_DEPTH
49
50 #define BIT_DEPTH 8
51 #include "tpel_template.c"
52 #include "dsputil_template.c"
53
54 // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size
55 #define pb_7f (~0UL / 255 * 0x7f)
56 #define pb_80 (~0UL / 255 * 0x80)
57
58 /* Specific zigzag scan for 248 idct. NOTE that unlike the
59  * specification, we interleave the fields */
60 const uint8_t ff_zigzag248_direct[64] = {
61      0,  8,  1,  9, 16, 24,  2, 10,
62     17, 25, 32, 40, 48, 56, 33, 41,
63     18, 26,  3, 11,  4, 12, 19, 27,
64     34, 42, 49, 57, 50, 58, 35, 43,
65     20, 28,  5, 13,  6, 14, 21, 29,
66     36, 44, 51, 59, 52, 60, 37, 45,
67     22, 30,  7, 15, 23, 31, 38, 46,
68     53, 61, 54, 62, 39, 47, 55, 63,
69 };
70
71 const uint8_t ff_alternate_horizontal_scan[64] = {
72      0,  1,  2,  3,  8,  9, 16, 17,
73     10, 11,  4,  5,  6,  7, 15, 14,
74     13, 12, 19, 18, 24, 25, 32, 33,
75     26, 27, 20, 21, 22, 23, 28, 29,
76     30, 31, 34, 35, 40, 41, 48, 49,
77     42, 43, 36, 37, 38, 39, 44, 45,
78     46, 47, 50, 51, 56, 57, 58, 59,
79     52, 53, 54, 55, 60, 61, 62, 63,
80 };
81
82 const uint8_t ff_alternate_vertical_scan[64] = {
83      0,  8, 16, 24,  1,  9,  2, 10,
84     17, 25, 32, 40, 48, 56, 57, 49,
85     41, 33, 26, 18,  3, 11,  4, 12,
86     19, 27, 34, 42, 50, 58, 35, 43,
87     51, 59, 20, 28,  5, 13,  6, 14,
88     21, 29, 36, 44, 52, 60, 37, 45,
89     53, 61, 22, 30,  7, 15, 23, 31,
90     38, 46, 54, 62, 39, 47, 55, 63,
91 };
92
93 /* Input permutation for the simple_idct_mmx */
94 static const uint8_t simple_mmx_permutation[64] = {
95     0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
96     0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
97     0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
98     0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
99     0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
100     0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
101     0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
102     0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
103 };
104
105 static const uint8_t idct_sse2_row_perm[8] = { 0, 4, 1, 5, 2, 6, 3, 7 };
106
107 av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st,
108                                const uint8_t *src_scantable)
109 {
110     int i, end;
111
112     st->scantable = src_scantable;
113
114     for (i = 0; i < 64; i++) {
115         int j = src_scantable[i];
116         st->permutated[i] = permutation[j];
117     }
118
119     end = -1;
120     for (i = 0; i < 64; i++) {
121         int j = st->permutated[i];
122         if (j > end)
123             end = j;
124         st->raster_end[i] = end;
125     }
126 }
127
128 av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
129                                            int idct_permutation_type)
130 {
131     int i;
132
133     switch (idct_permutation_type) {
134     case FF_NO_IDCT_PERM:
135         for (i = 0; i < 64; i++)
136             idct_permutation[i] = i;
137         break;
138     case FF_LIBMPEG2_IDCT_PERM:
139         for (i = 0; i < 64; i++)
140             idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
141         break;
142     case FF_SIMPLE_IDCT_PERM:
143         for (i = 0; i < 64; i++)
144             idct_permutation[i] = simple_mmx_permutation[i];
145         break;
146     case FF_TRANSPOSE_IDCT_PERM:
147         for (i = 0; i < 64; i++)
148             idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
149         break;
150     case FF_PARTTRANS_IDCT_PERM:
151         for (i = 0; i < 64; i++)
152             idct_permutation[i] = (i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3);
153         break;
154     case FF_SSE2_IDCT_PERM:
155         for (i = 0; i < 64; i++)
156             idct_permutation[i] = (i & 0x38) | idct_sse2_row_perm[i & 7];
157         break;
158     default:
159         av_log(NULL, AV_LOG_ERROR,
160                "Internal error, IDCT permutation not set\n");
161     }
162 }
163
164 static int pix_sum_c(uint8_t *pix, int line_size)
165 {
166     int s = 0, i, j;
167
168     for (i = 0; i < 16; i++) {
169         for (j = 0; j < 16; j += 8) {
170             s   += pix[0];
171             s   += pix[1];
172             s   += pix[2];
173             s   += pix[3];
174             s   += pix[4];
175             s   += pix[5];
176             s   += pix[6];
177             s   += pix[7];
178             pix += 8;
179         }
180         pix += line_size - 16;
181     }
182     return s;
183 }
184
185 static int pix_norm1_c(uint8_t *pix, int line_size)
186 {
187     int s = 0, i, j;
188     uint32_t *sq = ff_square_tab + 256;
189
190     for (i = 0; i < 16; i++) {
191         for (j = 0; j < 16; j += 8) {
192 #if 0
193             s += sq[pix[0]];
194             s += sq[pix[1]];
195             s += sq[pix[2]];
196             s += sq[pix[3]];
197             s += sq[pix[4]];
198             s += sq[pix[5]];
199             s += sq[pix[6]];
200             s += sq[pix[7]];
201 #else
202 #if HAVE_FAST_64BIT
203             register uint64_t x = *(uint64_t *) pix;
204             s += sq[x         & 0xff];
205             s += sq[(x >>  8) & 0xff];
206             s += sq[(x >> 16) & 0xff];
207             s += sq[(x >> 24) & 0xff];
208             s += sq[(x >> 32) & 0xff];
209             s += sq[(x >> 40) & 0xff];
210             s += sq[(x >> 48) & 0xff];
211             s += sq[(x >> 56) & 0xff];
212 #else
213             register uint32_t x = *(uint32_t *) pix;
214             s += sq[x         & 0xff];
215             s += sq[(x >>  8) & 0xff];
216             s += sq[(x >> 16) & 0xff];
217             s += sq[(x >> 24) & 0xff];
218             x  = *(uint32_t *) (pix + 4);
219             s += sq[x         & 0xff];
220             s += sq[(x >>  8) & 0xff];
221             s += sq[(x >> 16) & 0xff];
222             s += sq[(x >> 24) & 0xff];
223 #endif
224 #endif
225             pix += 8;
226         }
227         pix += line_size - 16;
228     }
229     return s;
230 }
231
232 static void bswap_buf(uint32_t *dst, const uint32_t *src, int w)
233 {
234     int i;
235
236     for (i = 0; i + 8 <= w; i += 8) {
237         dst[i + 0] = av_bswap32(src[i + 0]);
238         dst[i + 1] = av_bswap32(src[i + 1]);
239         dst[i + 2] = av_bswap32(src[i + 2]);
240         dst[i + 3] = av_bswap32(src[i + 3]);
241         dst[i + 4] = av_bswap32(src[i + 4]);
242         dst[i + 5] = av_bswap32(src[i + 5]);
243         dst[i + 6] = av_bswap32(src[i + 6]);
244         dst[i + 7] = av_bswap32(src[i + 7]);
245     }
246     for (; i < w; i++)
247         dst[i + 0] = av_bswap32(src[i + 0]);
248 }
249
250 static void bswap16_buf(uint16_t *dst, const uint16_t *src, int len)
251 {
252     while (len--)
253         *dst++ = av_bswap16(*src++);
254 }
255
256 static int sse4_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
257                   int line_size, int h)
258 {
259     int s = 0, i;
260     uint32_t *sq = ff_square_tab + 256;
261
262     for (i = 0; i < h; i++) {
263         s    += sq[pix1[0] - pix2[0]];
264         s    += sq[pix1[1] - pix2[1]];
265         s    += sq[pix1[2] - pix2[2]];
266         s    += sq[pix1[3] - pix2[3]];
267         pix1 += line_size;
268         pix2 += line_size;
269     }
270     return s;
271 }
272
273 static int sse8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
274                   int line_size, int h)
275 {
276     int s = 0, i;
277     uint32_t *sq = ff_square_tab + 256;
278
279     for (i = 0; i < h; i++) {
280         s    += sq[pix1[0] - pix2[0]];
281         s    += sq[pix1[1] - pix2[1]];
282         s    += sq[pix1[2] - pix2[2]];
283         s    += sq[pix1[3] - pix2[3]];
284         s    += sq[pix1[4] - pix2[4]];
285         s    += sq[pix1[5] - pix2[5]];
286         s    += sq[pix1[6] - pix2[6]];
287         s    += sq[pix1[7] - pix2[7]];
288         pix1 += line_size;
289         pix2 += line_size;
290     }
291     return s;
292 }
293
294 static int sse16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
295                    int line_size, int h)
296 {
297     int s = 0, i;
298     uint32_t *sq = ff_square_tab + 256;
299
300     for (i = 0; i < h; i++) {
301         s += sq[pix1[0]  - pix2[0]];
302         s += sq[pix1[1]  - pix2[1]];
303         s += sq[pix1[2]  - pix2[2]];
304         s += sq[pix1[3]  - pix2[3]];
305         s += sq[pix1[4]  - pix2[4]];
306         s += sq[pix1[5]  - pix2[5]];
307         s += sq[pix1[6]  - pix2[6]];
308         s += sq[pix1[7]  - pix2[7]];
309         s += sq[pix1[8]  - pix2[8]];
310         s += sq[pix1[9]  - pix2[9]];
311         s += sq[pix1[10] - pix2[10]];
312         s += sq[pix1[11] - pix2[11]];
313         s += sq[pix1[12] - pix2[12]];
314         s += sq[pix1[13] - pix2[13]];
315         s += sq[pix1[14] - pix2[14]];
316         s += sq[pix1[15] - pix2[15]];
317
318         pix1 += line_size;
319         pix2 += line_size;
320     }
321     return s;
322 }
323
324 static void diff_pixels_c(int16_t *restrict block, const uint8_t *s1,
325                           const uint8_t *s2, int stride)
326 {
327     int i;
328
329     /* read the pixels */
330     for (i = 0; i < 8; i++) {
331         block[0] = s1[0] - s2[0];
332         block[1] = s1[1] - s2[1];
333         block[2] = s1[2] - s2[2];
334         block[3] = s1[3] - s2[3];
335         block[4] = s1[4] - s2[4];
336         block[5] = s1[5] - s2[5];
337         block[6] = s1[6] - s2[6];
338         block[7] = s1[7] - s2[7];
339         s1      += stride;
340         s2      += stride;
341         block   += 8;
342     }
343 }
344
345 static void put_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
346                                  int line_size)
347 {
348     int i;
349
350     /* read the pixels */
351     for (i = 0; i < 8; i++) {
352         pixels[0] = av_clip_uint8(block[0]);
353         pixels[1] = av_clip_uint8(block[1]);
354         pixels[2] = av_clip_uint8(block[2]);
355         pixels[3] = av_clip_uint8(block[3]);
356         pixels[4] = av_clip_uint8(block[4]);
357         pixels[5] = av_clip_uint8(block[5]);
358         pixels[6] = av_clip_uint8(block[6]);
359         pixels[7] = av_clip_uint8(block[7]);
360
361         pixels += line_size;
362         block  += 8;
363     }
364 }
365
366 static void put_signed_pixels_clamped_c(const int16_t *block,
367                                         uint8_t *restrict pixels,
368                                         int line_size)
369 {
370     int i, j;
371
372     for (i = 0; i < 8; i++) {
373         for (j = 0; j < 8; j++) {
374             if (*block < -128)
375                 *pixels = 0;
376             else if (*block > 127)
377                 *pixels = 255;
378             else
379                 *pixels = (uint8_t) (*block + 128);
380             block++;
381             pixels++;
382         }
383         pixels += (line_size - 8);
384     }
385 }
386
387 static void add_pixels8_c(uint8_t *restrict pixels, int16_t *block,
388                           int line_size)
389 {
390     int i;
391
392     for (i = 0; i < 8; i++) {
393         pixels[0] += block[0];
394         pixels[1] += block[1];
395         pixels[2] += block[2];
396         pixels[3] += block[3];
397         pixels[4] += block[4];
398         pixels[5] += block[5];
399         pixels[6] += block[6];
400         pixels[7] += block[7];
401         pixels    += line_size;
402         block     += 8;
403     }
404 }
405
406 static void add_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
407                                  int line_size)
408 {
409     int i;
410
411     /* read the pixels */
412     for (i = 0; i < 8; i++) {
413         pixels[0] = av_clip_uint8(pixels[0] + block[0]);
414         pixels[1] = av_clip_uint8(pixels[1] + block[1]);
415         pixels[2] = av_clip_uint8(pixels[2] + block[2]);
416         pixels[3] = av_clip_uint8(pixels[3] + block[3]);
417         pixels[4] = av_clip_uint8(pixels[4] + block[4]);
418         pixels[5] = av_clip_uint8(pixels[5] + block[5]);
419         pixels[6] = av_clip_uint8(pixels[6] + block[6]);
420         pixels[7] = av_clip_uint8(pixels[7] + block[7]);
421         pixels   += line_size;
422         block    += 8;
423     }
424 }
425
426 static int sum_abs_dctelem_c(int16_t *block)
427 {
428     int sum = 0, i;
429
430     for (i = 0; i < 64; i++)
431         sum += FFABS(block[i]);
432     return sum;
433 }
434
435 static void fill_block16_c(uint8_t *block, uint8_t value, int line_size, int h)
436 {
437     int i;
438
439     for (i = 0; i < h; i++) {
440         memset(block, value, 16);
441         block += line_size;
442     }
443 }
444
445 static void fill_block8_c(uint8_t *block, uint8_t value, int line_size, int h)
446 {
447     int i;
448
449     for (i = 0; i < h; i++) {
450         memset(block, value, 8);
451         block += line_size;
452     }
453 }
454
455 #define avg2(a, b) ((a + b + 1) >> 1)
456 #define avg4(a, b, c, d) ((a + b + c + d + 2) >> 2)
457
458 static void gmc1_c(uint8_t *dst, uint8_t *src, int stride, int h,
459                    int x16, int y16, int rounder)
460 {
461     const int A = (16 - x16) * (16 - y16);
462     const int B = (x16)      * (16 - y16);
463     const int C = (16 - x16) * (y16);
464     const int D = (x16)      * (y16);
465     int i;
466
467     for (i = 0; i < h; i++) {
468         dst[0] = (A * src[0] + B * src[1] + C * src[stride + 0] + D * src[stride + 1] + rounder) >> 8;
469         dst[1] = (A * src[1] + B * src[2] + C * src[stride + 1] + D * src[stride + 2] + rounder) >> 8;
470         dst[2] = (A * src[2] + B * src[3] + C * src[stride + 2] + D * src[stride + 3] + rounder) >> 8;
471         dst[3] = (A * src[3] + B * src[4] + C * src[stride + 3] + D * src[stride + 4] + rounder) >> 8;
472         dst[4] = (A * src[4] + B * src[5] + C * src[stride + 4] + D * src[stride + 5] + rounder) >> 8;
473         dst[5] = (A * src[5] + B * src[6] + C * src[stride + 5] + D * src[stride + 6] + rounder) >> 8;
474         dst[6] = (A * src[6] + B * src[7] + C * src[stride + 6] + D * src[stride + 7] + rounder) >> 8;
475         dst[7] = (A * src[7] + B * src[8] + C * src[stride + 7] + D * src[stride + 8] + rounder) >> 8;
476         dst   += stride;
477         src   += stride;
478     }
479 }
480
481 void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
482               int dxx, int dxy, int dyx, int dyy, int shift, int r,
483               int width, int height)
484 {
485     int y, vx, vy;
486     const int s = 1 << shift;
487
488     width--;
489     height--;
490
491     for (y = 0; y < h; y++) {
492         int x;
493
494         vx = ox;
495         vy = oy;
496         for (x = 0; x < 8; x++) { // FIXME: optimize
497             int index;
498             int src_x  = vx >> 16;
499             int src_y  = vy >> 16;
500             int frac_x = src_x & (s - 1);
501             int frac_y = src_y & (s - 1);
502
503             src_x >>= shift;
504             src_y >>= shift;
505
506             if ((unsigned) src_x < width) {
507                 if ((unsigned) src_y < height) {
508                     index = src_x + src_y * stride;
509                     dst[y * stride + x] =
510                         ((src[index]                        * (s - frac_x) +
511                           src[index + 1]          * frac_x) * (s - frac_y) +
512                          (src[index + stride]               * (s - frac_x) +
513                           src[index + stride + 1] * frac_x) *      frac_y  +
514                          r) >> (shift * 2);
515                 } else {
516                     index = src_x + av_clip(src_y, 0, height) * stride;
517                     dst[y * stride + x] =
518                         ((src[index]               * (s - frac_x) +
519                           src[index + 1] * frac_x) *  s           +
520                          r) >> (shift * 2);
521                 }
522             } else {
523                 if ((unsigned) src_y < height) {
524                     index = av_clip(src_x, 0, width) + src_y * stride;
525                     dst[y * stride + x] =
526                         ((src[index]                    * (s - frac_y) +
527                           src[index + stride] * frac_y) *  s           +
528                          r) >> (shift * 2);
529                 } else {
530                     index = av_clip(src_x, 0, width) +
531                             av_clip(src_y, 0, height) * stride;
532                     dst[y * stride + x] = src[index];
533                 }
534             }
535
536             vx += dxx;
537             vy += dyx;
538         }
539         ox += dxy;
540         oy += dyy;
541     }
542 }
543
544 #define QPEL_MC(r, OPNAME, RND, OP)                                           \
545 static void OPNAME ## mpeg4_qpel8_h_lowpass(uint8_t *dst, uint8_t *src,       \
546                                             int dstStride, int srcStride,     \
547                                             int h)                            \
548 {                                                                             \
549     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;                           \
550     int i;                                                                    \
551                                                                               \
552     for (i = 0; i < h; i++) {                                                 \
553         OP(dst[0], (src[0] + src[1]) * 20 - (src[0] + src[2]) * 6 + (src[1] + src[3]) * 3 - (src[2] + src[4])); \
554         OP(dst[1], (src[1] + src[2]) * 20 - (src[0] + src[3]) * 6 + (src[0] + src[4]) * 3 - (src[1] + src[5])); \
555         OP(dst[2], (src[2] + src[3]) * 20 - (src[1] + src[4]) * 6 + (src[0] + src[5]) * 3 - (src[0] + src[6])); \
556         OP(dst[3], (src[3] + src[4]) * 20 - (src[2] + src[5]) * 6 + (src[1] + src[6]) * 3 - (src[0] + src[7])); \
557         OP(dst[4], (src[4] + src[5]) * 20 - (src[3] + src[6]) * 6 + (src[2] + src[7]) * 3 - (src[1] + src[8])); \
558         OP(dst[5], (src[5] + src[6]) * 20 - (src[4] + src[7]) * 6 + (src[3] + src[8]) * 3 - (src[2] + src[8])); \
559         OP(dst[6], (src[6] + src[7]) * 20 - (src[5] + src[8]) * 6 + (src[4] + src[8]) * 3 - (src[3] + src[7])); \
560         OP(dst[7], (src[7] + src[8]) * 20 - (src[6] + src[8]) * 6 + (src[5] + src[7]) * 3 - (src[4] + src[6])); \
561         dst += dstStride;                                                     \
562         src += srcStride;                                                     \
563     }                                                                         \
564 }                                                                             \
565                                                                               \
566 static void OPNAME ## mpeg4_qpel8_v_lowpass(uint8_t *dst, uint8_t *src,       \
567                                             int dstStride, int srcStride)     \
568 {                                                                             \
569     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;                           \
570     const int w = 8;                                                          \
571     int i;                                                                    \
572                                                                               \
573     for (i = 0; i < w; i++) {                                                 \
574         const int src0 = src[0 * srcStride];                                  \
575         const int src1 = src[1 * srcStride];                                  \
576         const int src2 = src[2 * srcStride];                                  \
577         const int src3 = src[3 * srcStride];                                  \
578         const int src4 = src[4 * srcStride];                                  \
579         const int src5 = src[5 * srcStride];                                  \
580         const int src6 = src[6 * srcStride];                                  \
581         const int src7 = src[7 * srcStride];                                  \
582         const int src8 = src[8 * srcStride];                                  \
583         OP(dst[0 * dstStride], (src0 + src1) * 20 - (src0 + src2) * 6 + (src1 + src3) * 3 - (src2 + src4)); \
584         OP(dst[1 * dstStride], (src1 + src2) * 20 - (src0 + src3) * 6 + (src0 + src4) * 3 - (src1 + src5)); \
585         OP(dst[2 * dstStride], (src2 + src3) * 20 - (src1 + src4) * 6 + (src0 + src5) * 3 - (src0 + src6)); \
586         OP(dst[3 * dstStride], (src3 + src4) * 20 - (src2 + src5) * 6 + (src1 + src6) * 3 - (src0 + src7)); \
587         OP(dst[4 * dstStride], (src4 + src5) * 20 - (src3 + src6) * 6 + (src2 + src7) * 3 - (src1 + src8)); \
588         OP(dst[5 * dstStride], (src5 + src6) * 20 - (src4 + src7) * 6 + (src3 + src8) * 3 - (src2 + src8)); \
589         OP(dst[6 * dstStride], (src6 + src7) * 20 - (src5 + src8) * 6 + (src4 + src8) * 3 - (src3 + src7)); \
590         OP(dst[7 * dstStride], (src7 + src8) * 20 - (src6 + src8) * 6 + (src5 + src7) * 3 - (src4 + src6)); \
591         dst++;                                                                \
592         src++;                                                                \
593     }                                                                         \
594 }                                                                             \
595                                                                               \
596 static void OPNAME ## mpeg4_qpel16_h_lowpass(uint8_t *dst, uint8_t *src,      \
597                                              int dstStride, int srcStride,    \
598                                              int h)                           \
599 {                                                                             \
600     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;                           \
601     int i;                                                                    \
602                                                                               \
603     for (i = 0; i < h; i++) {                                                 \
604         OP(dst[0],  (src[0]  + src[1])  * 20 - (src[0]  + src[2])  * 6 + (src[1]  + src[3])  * 3 - (src[2]  + src[4]));  \
605         OP(dst[1],  (src[1]  + src[2])  * 20 - (src[0]  + src[3])  * 6 + (src[0]  + src[4])  * 3 - (src[1]  + src[5]));  \
606         OP(dst[2],  (src[2]  + src[3])  * 20 - (src[1]  + src[4])  * 6 + (src[0]  + src[5])  * 3 - (src[0]  + src[6]));  \
607         OP(dst[3],  (src[3]  + src[4])  * 20 - (src[2]  + src[5])  * 6 + (src[1]  + src[6])  * 3 - (src[0]  + src[7]));  \
608         OP(dst[4],  (src[4]  + src[5])  * 20 - (src[3]  + src[6])  * 6 + (src[2]  + src[7])  * 3 - (src[1]  + src[8]));  \
609         OP(dst[5],  (src[5]  + src[6])  * 20 - (src[4]  + src[7])  * 6 + (src[3]  + src[8])  * 3 - (src[2]  + src[9]));  \
610         OP(dst[6],  (src[6]  + src[7])  * 20 - (src[5]  + src[8])  * 6 + (src[4]  + src[9])  * 3 - (src[3]  + src[10])); \
611         OP(dst[7],  (src[7]  + src[8])  * 20 - (src[6]  + src[9])  * 6 + (src[5]  + src[10]) * 3 - (src[4]  + src[11])); \
612         OP(dst[8],  (src[8]  + src[9])  * 20 - (src[7]  + src[10]) * 6 + (src[6]  + src[11]) * 3 - (src[5]  + src[12])); \
613         OP(dst[9],  (src[9]  + src[10]) * 20 - (src[8]  + src[11]) * 6 + (src[7]  + src[12]) * 3 - (src[6]  + src[13])); \
614         OP(dst[10], (src[10] + src[11]) * 20 - (src[9]  + src[12]) * 6 + (src[8]  + src[13]) * 3 - (src[7]  + src[14])); \
615         OP(dst[11], (src[11] + src[12]) * 20 - (src[10] + src[13]) * 6 + (src[9]  + src[14]) * 3 - (src[8]  + src[15])); \
616         OP(dst[12], (src[12] + src[13]) * 20 - (src[11] + src[14]) * 6 + (src[10] + src[15]) * 3 - (src[9]  + src[16])); \
617         OP(dst[13], (src[13] + src[14]) * 20 - (src[12] + src[15]) * 6 + (src[11] + src[16]) * 3 - (src[10] + src[16])); \
618         OP(dst[14], (src[14] + src[15]) * 20 - (src[13] + src[16]) * 6 + (src[12] + src[16]) * 3 - (src[11] + src[15])); \
619         OP(dst[15], (src[15] + src[16]) * 20 - (src[14] + src[16]) * 6 + (src[13] + src[15]) * 3 - (src[12] + src[14])); \
620         dst += dstStride;                                                     \
621         src += srcStride;                                                     \
622     }                                                                         \
623 }                                                                             \
624                                                                               \
625 static void OPNAME ## mpeg4_qpel16_v_lowpass(uint8_t *dst, uint8_t *src,      \
626                                              int dstStride, int srcStride)    \
627 {                                                                             \
628     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;                           \
629     const int w = 16;                                                         \
630     int i;                                                                    \
631                                                                               \
632     for (i = 0; i < w; i++) {                                                 \
633         const int src0  = src[0  * srcStride];                                \
634         const int src1  = src[1  * srcStride];                                \
635         const int src2  = src[2  * srcStride];                                \
636         const int src3  = src[3  * srcStride];                                \
637         const int src4  = src[4  * srcStride];                                \
638         const int src5  = src[5  * srcStride];                                \
639         const int src6  = src[6  * srcStride];                                \
640         const int src7  = src[7  * srcStride];                                \
641         const int src8  = src[8  * srcStride];                                \
642         const int src9  = src[9  * srcStride];                                \
643         const int src10 = src[10 * srcStride];                                \
644         const int src11 = src[11 * srcStride];                                \
645         const int src12 = src[12 * srcStride];                                \
646         const int src13 = src[13 * srcStride];                                \
647         const int src14 = src[14 * srcStride];                                \
648         const int src15 = src[15 * srcStride];                                \
649         const int src16 = src[16 * srcStride];                                \
650         OP(dst[0  * dstStride], (src0  + src1)  * 20 - (src0  + src2)  * 6 + (src1  + src3)  * 3 - (src2  + src4));  \
651         OP(dst[1  * dstStride], (src1  + src2)  * 20 - (src0  + src3)  * 6 + (src0  + src4)  * 3 - (src1  + src5));  \
652         OP(dst[2  * dstStride], (src2  + src3)  * 20 - (src1  + src4)  * 6 + (src0  + src5)  * 3 - (src0  + src6));  \
653         OP(dst[3  * dstStride], (src3  + src4)  * 20 - (src2  + src5)  * 6 + (src1  + src6)  * 3 - (src0  + src7));  \
654         OP(dst[4  * dstStride], (src4  + src5)  * 20 - (src3  + src6)  * 6 + (src2  + src7)  * 3 - (src1  + src8));  \
655         OP(dst[5  * dstStride], (src5  + src6)  * 20 - (src4  + src7)  * 6 + (src3  + src8)  * 3 - (src2  + src9));  \
656         OP(dst[6  * dstStride], (src6  + src7)  * 20 - (src5  + src8)  * 6 + (src4  + src9)  * 3 - (src3  + src10)); \
657         OP(dst[7  * dstStride], (src7  + src8)  * 20 - (src6  + src9)  * 6 + (src5  + src10) * 3 - (src4  + src11)); \
658         OP(dst[8  * dstStride], (src8  + src9)  * 20 - (src7  + src10) * 6 + (src6  + src11) * 3 - (src5  + src12)); \
659         OP(dst[9  * dstStride], (src9  + src10) * 20 - (src8  + src11) * 6 + (src7  + src12) * 3 - (src6  + src13)); \
660         OP(dst[10 * dstStride], (src10 + src11) * 20 - (src9  + src12) * 6 + (src8  + src13) * 3 - (src7  + src14)); \
661         OP(dst[11 * dstStride], (src11 + src12) * 20 - (src10 + src13) * 6 + (src9  + src14) * 3 - (src8  + src15)); \
662         OP(dst[12 * dstStride], (src12 + src13) * 20 - (src11 + src14) * 6 + (src10 + src15) * 3 - (src9  + src16)); \
663         OP(dst[13 * dstStride], (src13 + src14) * 20 - (src12 + src15) * 6 + (src11 + src16) * 3 - (src10 + src16)); \
664         OP(dst[14 * dstStride], (src14 + src15) * 20 - (src13 + src16) * 6 + (src12 + src16) * 3 - (src11 + src15)); \
665         OP(dst[15 * dstStride], (src15 + src16) * 20 - (src14 + src16) * 6 + (src13 + src15) * 3 - (src12 + src14)); \
666         dst++;                                                                \
667         src++;                                                                \
668     }                                                                         \
669 }                                                                             \
670                                                                               \
671 static void OPNAME ## qpel8_mc10_c(uint8_t *dst, uint8_t *src,                \
672                                    ptrdiff_t stride)                          \
673 {                                                                             \
674     uint8_t half[64];                                                         \
675                                                                               \
676     put ## RND ## mpeg4_qpel8_h_lowpass(half, src, 8, stride, 8);             \
677     OPNAME ## pixels8_l2_8(dst, src, half, stride, stride, 8, 8);             \
678 }                                                                             \
679                                                                               \
680 static void OPNAME ## qpel8_mc20_c(uint8_t *dst, uint8_t *src,                \
681                                    ptrdiff_t stride)                          \
682 {                                                                             \
683     OPNAME ## mpeg4_qpel8_h_lowpass(dst, src, stride, stride, 8);             \
684 }                                                                             \
685                                                                               \
686 static void OPNAME ## qpel8_mc30_c(uint8_t *dst, uint8_t *src,                \
687                                    ptrdiff_t stride)                          \
688 {                                                                             \
689     uint8_t half[64];                                                         \
690                                                                               \
691     put ## RND ## mpeg4_qpel8_h_lowpass(half, src, 8, stride, 8);             \
692     OPNAME ## pixels8_l2_8(dst, src + 1, half, stride, stride, 8, 8);         \
693 }                                                                             \
694                                                                               \
695 static void OPNAME ## qpel8_mc01_c(uint8_t *dst, uint8_t *src,                \
696                                    ptrdiff_t stride)                          \
697 {                                                                             \
698     uint8_t full[16 * 9];                                                     \
699     uint8_t half[64];                                                         \
700                                                                               \
701     copy_block9(full, src, 16, stride, 9);                                    \
702     put ## RND ## mpeg4_qpel8_v_lowpass(half, full, 8, 16);                   \
703     OPNAME ## pixels8_l2_8(dst, full, half, stride, 16, 8, 8);                \
704 }                                                                             \
705                                                                               \
706 static void OPNAME ## qpel8_mc02_c(uint8_t *dst, uint8_t *src,                \
707                                    ptrdiff_t stride)                          \
708 {                                                                             \
709     uint8_t full[16 * 9];                                                     \
710                                                                               \
711     copy_block9(full, src, 16, stride, 9);                                    \
712     OPNAME ## mpeg4_qpel8_v_lowpass(dst, full, stride, 16);                   \
713 }                                                                             \
714                                                                               \
715 static void OPNAME ## qpel8_mc03_c(uint8_t *dst, uint8_t *src,                \
716                                    ptrdiff_t stride)                          \
717 {                                                                             \
718     uint8_t full[16 * 9];                                                     \
719     uint8_t half[64];                                                         \
720                                                                               \
721     copy_block9(full, src, 16, stride, 9);                                    \
722     put ## RND ## mpeg4_qpel8_v_lowpass(half, full, 8, 16);                   \
723     OPNAME ## pixels8_l2_8(dst, full + 16, half, stride, 16, 8, 8);           \
724 }                                                                             \
725                                                                               \
726 void ff_ ## OPNAME ## qpel8_mc11_old_c(uint8_t *dst, uint8_t *src,            \
727                                        ptrdiff_t stride)                      \
728 {                                                                             \
729     uint8_t full[16 * 9];                                                     \
730     uint8_t halfH[72];                                                        \
731     uint8_t halfV[64];                                                        \
732     uint8_t halfHV[64];                                                       \
733                                                                               \
734     copy_block9(full, src, 16, stride, 9);                                    \
735     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
736     put ## RND ## mpeg4_qpel8_v_lowpass(halfV, full, 8, 16);                  \
737     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
738     OPNAME ## pixels8_l4_8(dst, full, halfH, halfV, halfHV,                   \
739                            stride, 16, 8, 8, 8, 8);                           \
740 }                                                                             \
741                                                                               \
742 static void OPNAME ## qpel8_mc11_c(uint8_t *dst, uint8_t *src,                \
743                                    ptrdiff_t stride)                          \
744 {                                                                             \
745     uint8_t full[16 * 9];                                                     \
746     uint8_t halfH[72];                                                        \
747     uint8_t halfHV[64];                                                       \
748                                                                               \
749     copy_block9(full, src, 16, stride, 9);                                    \
750     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
751     put ## RND ## pixels8_l2_8(halfH, halfH, full, 8, 8, 16, 9);              \
752     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
753     OPNAME ## pixels8_l2_8(dst, halfH, halfHV, stride, 8, 8, 8);              \
754 }                                                                             \
755                                                                               \
756 void ff_ ## OPNAME ## qpel8_mc31_old_c(uint8_t *dst, uint8_t *src,            \
757                                        ptrdiff_t stride)                      \
758 {                                                                             \
759     uint8_t full[16 * 9];                                                     \
760     uint8_t halfH[72];                                                        \
761     uint8_t halfV[64];                                                        \
762     uint8_t halfHV[64];                                                       \
763                                                                               \
764     copy_block9(full, src, 16, stride, 9);                                    \
765     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
766     put ## RND ## mpeg4_qpel8_v_lowpass(halfV, full + 1, 8, 16);              \
767     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
768     OPNAME ## pixels8_l4_8(dst, full + 1, halfH, halfV, halfHV,               \
769                            stride, 16, 8, 8, 8, 8);                           \
770 }                                                                             \
771                                                                               \
772 static void OPNAME ## qpel8_mc31_c(uint8_t *dst, uint8_t *src,                \
773                                    ptrdiff_t stride)                          \
774 {                                                                             \
775     uint8_t full[16 * 9];                                                     \
776     uint8_t halfH[72];                                                        \
777     uint8_t halfHV[64];                                                       \
778                                                                               \
779     copy_block9(full, src, 16, stride, 9);                                    \
780     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
781     put ## RND ## pixels8_l2_8(halfH, halfH, full + 1, 8, 8, 16, 9);          \
782     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
783     OPNAME ## pixels8_l2_8(dst, halfH, halfHV, stride, 8, 8, 8);              \
784 }                                                                             \
785                                                                               \
786 void ff_ ## OPNAME ## qpel8_mc13_old_c(uint8_t *dst, uint8_t *src,            \
787                                        ptrdiff_t stride)                      \
788 {                                                                             \
789     uint8_t full[16 * 9];                                                     \
790     uint8_t halfH[72];                                                        \
791     uint8_t halfV[64];                                                        \
792     uint8_t halfHV[64];                                                       \
793                                                                               \
794     copy_block9(full, src, 16, stride, 9);                                    \
795     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
796     put ## RND ## mpeg4_qpel8_v_lowpass(halfV, full, 8, 16);                  \
797     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
798     OPNAME ## pixels8_l4_8(dst, full + 16, halfH + 8, halfV, halfHV,          \
799                            stride, 16, 8, 8, 8, 8);                           \
800 }                                                                             \
801                                                                               \
802 static void OPNAME ## qpel8_mc13_c(uint8_t *dst, uint8_t *src,                \
803                                    ptrdiff_t stride)                          \
804 {                                                                             \
805     uint8_t full[16 * 9];                                                     \
806     uint8_t halfH[72];                                                        \
807     uint8_t halfHV[64];                                                       \
808                                                                               \
809     copy_block9(full, src, 16, stride, 9);                                    \
810     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
811     put ## RND ## pixels8_l2_8(halfH, halfH, full, 8, 8, 16, 9);              \
812     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
813     OPNAME ## pixels8_l2_8(dst, halfH + 8, halfHV, stride, 8, 8, 8);          \
814 }                                                                             \
815                                                                               \
816 void ff_ ## OPNAME ## qpel8_mc33_old_c(uint8_t *dst, uint8_t *src,            \
817                                        ptrdiff_t stride)                      \
818 {                                                                             \
819     uint8_t full[16 * 9];                                                     \
820     uint8_t halfH[72];                                                        \
821     uint8_t halfV[64];                                                        \
822     uint8_t halfHV[64];                                                       \
823                                                                               \
824     copy_block9(full, src, 16, stride, 9);                                    \
825     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
826     put ## RND ## mpeg4_qpel8_v_lowpass(halfV, full + 1, 8, 16);              \
827     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
828     OPNAME ## pixels8_l4_8(dst, full + 17, halfH + 8, halfV, halfHV,          \
829                            stride, 16, 8, 8, 8, 8);                           \
830 }                                                                             \
831                                                                               \
832 static void OPNAME ## qpel8_mc33_c(uint8_t *dst, uint8_t *src,                \
833                                    ptrdiff_t stride)                          \
834 {                                                                             \
835     uint8_t full[16 * 9];                                                     \
836     uint8_t halfH[72];                                                        \
837     uint8_t halfHV[64];                                                       \
838                                                                               \
839     copy_block9(full, src, 16, stride, 9);                                    \
840     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
841     put ## RND ## pixels8_l2_8(halfH, halfH, full + 1, 8, 8, 16, 9);          \
842     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
843     OPNAME ## pixels8_l2_8(dst, halfH + 8, halfHV, stride, 8, 8, 8);          \
844 }                                                                             \
845                                                                               \
846 static void OPNAME ## qpel8_mc21_c(uint8_t *dst, uint8_t *src,                \
847                                    ptrdiff_t stride)                          \
848 {                                                                             \
849     uint8_t halfH[72];                                                        \
850     uint8_t halfHV[64];                                                       \
851                                                                               \
852     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);            \
853     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
854     OPNAME ## pixels8_l2_8(dst, halfH, halfHV, stride, 8, 8, 8);              \
855 }                                                                             \
856                                                                               \
857 static void OPNAME ## qpel8_mc23_c(uint8_t *dst, uint8_t *src,                \
858                                    ptrdiff_t stride)                          \
859 {                                                                             \
860     uint8_t halfH[72];                                                        \
861     uint8_t halfHV[64];                                                       \
862                                                                               \
863     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);            \
864     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
865     OPNAME ## pixels8_l2_8(dst, halfH + 8, halfHV, stride, 8, 8, 8);          \
866 }                                                                             \
867                                                                               \
868 void ff_ ## OPNAME ## qpel8_mc12_old_c(uint8_t *dst, uint8_t *src,            \
869                                        ptrdiff_t stride)                      \
870 {                                                                             \
871     uint8_t full[16 * 9];                                                     \
872     uint8_t halfH[72];                                                        \
873     uint8_t halfV[64];                                                        \
874     uint8_t halfHV[64];                                                       \
875                                                                               \
876     copy_block9(full, src, 16, stride, 9);                                    \
877     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
878     put ## RND ## mpeg4_qpel8_v_lowpass(halfV, full, 8, 16);                  \
879     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
880     OPNAME ## pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);              \
881 }                                                                             \
882                                                                               \
883 static void OPNAME ## qpel8_mc12_c(uint8_t *dst, uint8_t *src,                \
884                                    ptrdiff_t stride)                          \
885 {                                                                             \
886     uint8_t full[16 * 9];                                                     \
887     uint8_t halfH[72];                                                        \
888                                                                               \
889     copy_block9(full, src, 16, stride, 9);                                    \
890     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
891     put ## RND ## pixels8_l2_8(halfH, halfH, full, 8, 8, 16, 9);              \
892     OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);                   \
893 }                                                                             \
894                                                                               \
895 void ff_ ## OPNAME ## qpel8_mc32_old_c(uint8_t *dst, uint8_t *src,            \
896                                        ptrdiff_t stride)                      \
897 {                                                                             \
898     uint8_t full[16 * 9];                                                     \
899     uint8_t halfH[72];                                                        \
900     uint8_t halfV[64];                                                        \
901     uint8_t halfHV[64];                                                       \
902                                                                               \
903     copy_block9(full, src, 16, stride, 9);                                    \
904     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
905     put ## RND ## mpeg4_qpel8_v_lowpass(halfV, full + 1, 8, 16);              \
906     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);                 \
907     OPNAME ## pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);              \
908 }                                                                             \
909                                                                               \
910 static void OPNAME ## qpel8_mc32_c(uint8_t *dst, uint8_t *src,                \
911                                    ptrdiff_t stride)                          \
912 {                                                                             \
913     uint8_t full[16 * 9];                                                     \
914     uint8_t halfH[72];                                                        \
915                                                                               \
916     copy_block9(full, src, 16, stride, 9);                                    \
917     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);               \
918     put ## RND ## pixels8_l2_8(halfH, halfH, full + 1, 8, 8, 16, 9);          \
919     OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);                   \
920 }                                                                             \
921                                                                               \
922 static void OPNAME ## qpel8_mc22_c(uint8_t *dst, uint8_t *src,                \
923                                    ptrdiff_t stride)                          \
924 {                                                                             \
925     uint8_t halfH[72];                                                        \
926                                                                               \
927     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);            \
928     OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);                   \
929 }                                                                             \
930                                                                               \
931 static void OPNAME ## qpel16_mc10_c(uint8_t *dst, uint8_t *src,               \
932                                     ptrdiff_t stride)                         \
933 {                                                                             \
934     uint8_t half[256];                                                        \
935                                                                               \
936     put ## RND ## mpeg4_qpel16_h_lowpass(half, src, 16, stride, 16);          \
937     OPNAME ## pixels16_l2_8(dst, src, half, stride, stride, 16, 16);          \
938 }                                                                             \
939                                                                               \
940 static void OPNAME ## qpel16_mc20_c(uint8_t *dst, uint8_t *src,               \
941                                     ptrdiff_t stride)                         \
942 {                                                                             \
943     OPNAME ## mpeg4_qpel16_h_lowpass(dst, src, stride, stride, 16);           \
944 }                                                                             \
945                                                                               \
946 static void OPNAME ## qpel16_mc30_c(uint8_t *dst, uint8_t *src,               \
947                                     ptrdiff_t stride)                         \
948 {                                                                             \
949     uint8_t half[256];                                                        \
950                                                                               \
951     put ## RND ## mpeg4_qpel16_h_lowpass(half, src, 16, stride, 16);          \
952     OPNAME ## pixels16_l2_8(dst, src + 1, half, stride, stride, 16, 16);      \
953 }                                                                             \
954                                                                               \
955 static void OPNAME ## qpel16_mc01_c(uint8_t *dst, uint8_t *src,               \
956                                     ptrdiff_t stride)                         \
957 {                                                                             \
958     uint8_t full[24 * 17];                                                    \
959     uint8_t half[256];                                                        \
960                                                                               \
961     copy_block17(full, src, 24, stride, 17);                                  \
962     put ## RND ## mpeg4_qpel16_v_lowpass(half, full, 16, 24);                 \
963     OPNAME ## pixels16_l2_8(dst, full, half, stride, 24, 16, 16);             \
964 }                                                                             \
965                                                                               \
966 static void OPNAME ## qpel16_mc02_c(uint8_t *dst, uint8_t *src,               \
967                                     ptrdiff_t stride)                         \
968 {                                                                             \
969     uint8_t full[24 * 17];                                                    \
970                                                                               \
971     copy_block17(full, src, 24, stride, 17);                                  \
972     OPNAME ## mpeg4_qpel16_v_lowpass(dst, full, stride, 24);                  \
973 }                                                                             \
974                                                                               \
975 static void OPNAME ## qpel16_mc03_c(uint8_t *dst, uint8_t *src,               \
976                                     ptrdiff_t stride)                         \
977 {                                                                             \
978     uint8_t full[24 * 17];                                                    \
979     uint8_t half[256];                                                        \
980                                                                               \
981     copy_block17(full, src, 24, stride, 17);                                  \
982     put ## RND ## mpeg4_qpel16_v_lowpass(half, full, 16, 24);                 \
983     OPNAME ## pixels16_l2_8(dst, full + 24, half, stride, 24, 16, 16);        \
984 }                                                                             \
985                                                                               \
986 void ff_ ## OPNAME ## qpel16_mc11_old_c(uint8_t *dst, uint8_t *src,           \
987                                         ptrdiff_t stride)                     \
988 {                                                                             \
989     uint8_t full[24 * 17];                                                    \
990     uint8_t halfH[272];                                                       \
991     uint8_t halfV[256];                                                       \
992     uint8_t halfHV[256];                                                      \
993                                                                               \
994     copy_block17(full, src, 24, stride, 17);                                  \
995     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
996     put ## RND ## mpeg4_qpel16_v_lowpass(halfV, full, 16, 24);                \
997     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
998     OPNAME ## pixels16_l4_8(dst, full, halfH, halfV, halfHV,                  \
999                             stride, 24, 16, 16, 16, 16);                      \
1000 }                                                                             \
1001                                                                               \
1002 static void OPNAME ## qpel16_mc11_c(uint8_t *dst, uint8_t *src,               \
1003                                     ptrdiff_t stride)                         \
1004 {                                                                             \
1005     uint8_t full[24 * 17];                                                    \
1006     uint8_t halfH[272];                                                       \
1007     uint8_t halfHV[256];                                                      \
1008                                                                               \
1009     copy_block17(full, src, 24, stride, 17);                                  \
1010     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1011     put ## RND ## pixels16_l2_8(halfH, halfH, full, 16, 16, 24, 17);          \
1012     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1013     OPNAME ## pixels16_l2_8(dst, halfH, halfHV, stride, 16, 16, 16);          \
1014 }                                                                             \
1015                                                                               \
1016 void ff_ ## OPNAME ## qpel16_mc31_old_c(uint8_t *dst, uint8_t *src,           \
1017                                         ptrdiff_t stride)                     \
1018 {                                                                             \
1019     uint8_t full[24 * 17];                                                    \
1020     uint8_t halfH[272];                                                       \
1021     uint8_t halfV[256];                                                       \
1022     uint8_t halfHV[256];                                                      \
1023                                                                               \
1024     copy_block17(full, src, 24, stride, 17);                                  \
1025     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1026     put ## RND ## mpeg4_qpel16_v_lowpass(halfV, full + 1, 16, 24);            \
1027     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1028     OPNAME ## pixels16_l4_8(dst, full + 1, halfH, halfV, halfHV,              \
1029                             stride, 24, 16, 16, 16, 16);                      \
1030 }                                                                             \
1031                                                                               \
1032 static void OPNAME ## qpel16_mc31_c(uint8_t *dst, uint8_t *src,               \
1033                                     ptrdiff_t stride)                         \
1034 {                                                                             \
1035     uint8_t full[24 * 17];                                                    \
1036     uint8_t halfH[272];                                                       \
1037     uint8_t halfHV[256];                                                      \
1038                                                                               \
1039     copy_block17(full, src, 24, stride, 17);                                  \
1040     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1041     put ## RND ## pixels16_l2_8(halfH, halfH, full + 1, 16, 16, 24, 17);      \
1042     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1043     OPNAME ## pixels16_l2_8(dst, halfH, halfHV, stride, 16, 16, 16);          \
1044 }                                                                             \
1045                                                                               \
1046 void ff_ ## OPNAME ## qpel16_mc13_old_c(uint8_t *dst, uint8_t *src,           \
1047                                         ptrdiff_t stride)                     \
1048 {                                                                             \
1049     uint8_t full[24 * 17];                                                    \
1050     uint8_t halfH[272];                                                       \
1051     uint8_t halfV[256];                                                       \
1052     uint8_t halfHV[256];                                                      \
1053                                                                               \
1054     copy_block17(full, src, 24, stride, 17);                                  \
1055     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1056     put ## RND ## mpeg4_qpel16_v_lowpass(halfV, full, 16, 24);                \
1057     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1058     OPNAME ## pixels16_l4_8(dst, full + 24, halfH + 16, halfV, halfHV,        \
1059                             stride, 24, 16, 16, 16, 16);                      \
1060 }                                                                             \
1061                                                                               \
1062 static void OPNAME ## qpel16_mc13_c(uint8_t *dst, uint8_t *src,               \
1063                                     ptrdiff_t stride)                         \
1064 {                                                                             \
1065     uint8_t full[24 * 17];                                                    \
1066     uint8_t halfH[272];                                                       \
1067     uint8_t halfHV[256];                                                      \
1068                                                                               \
1069     copy_block17(full, src, 24, stride, 17);                                  \
1070     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1071     put ## RND ## pixels16_l2_8(halfH, halfH, full, 16, 16, 24, 17);          \
1072     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1073     OPNAME ## pixels16_l2_8(dst, halfH + 16, halfHV, stride, 16, 16, 16);     \
1074 }                                                                             \
1075                                                                               \
1076 void ff_ ## OPNAME ## qpel16_mc33_old_c(uint8_t *dst, uint8_t *src,           \
1077                                         ptrdiff_t stride)                     \
1078 {                                                                             \
1079     uint8_t full[24 * 17];                                                    \
1080     uint8_t halfH[272];                                                       \
1081     uint8_t halfV[256];                                                       \
1082     uint8_t halfHV[256];                                                      \
1083                                                                               \
1084     copy_block17(full, src, 24, stride, 17);                                  \
1085     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1086     put ## RND ## mpeg4_qpel16_v_lowpass(halfV, full + 1, 16, 24);            \
1087     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1088     OPNAME ## pixels16_l4_8(dst, full + 25, halfH + 16, halfV, halfHV,        \
1089                             stride, 24, 16, 16, 16, 16);                      \
1090 }                                                                             \
1091                                                                               \
1092 static void OPNAME ## qpel16_mc33_c(uint8_t *dst, uint8_t *src,               \
1093                                     ptrdiff_t stride)                         \
1094 {                                                                             \
1095     uint8_t full[24 * 17];                                                    \
1096     uint8_t halfH[272];                                                       \
1097     uint8_t halfHV[256];                                                      \
1098                                                                               \
1099     copy_block17(full, src, 24, stride, 17);                                  \
1100     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1101     put ## RND ## pixels16_l2_8(halfH, halfH, full + 1, 16, 16, 24, 17);      \
1102     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1103     OPNAME ## pixels16_l2_8(dst, halfH + 16, halfHV, stride, 16, 16, 16);     \
1104 }                                                                             \
1105                                                                               \
1106 static void OPNAME ## qpel16_mc21_c(uint8_t *dst, uint8_t *src,               \
1107                                     ptrdiff_t stride)                         \
1108 {                                                                             \
1109     uint8_t halfH[272];                                                       \
1110     uint8_t halfHV[256];                                                      \
1111                                                                               \
1112     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);         \
1113     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1114     OPNAME ## pixels16_l2_8(dst, halfH, halfHV, stride, 16, 16, 16);          \
1115 }                                                                             \
1116                                                                               \
1117 static void OPNAME ## qpel16_mc23_c(uint8_t *dst, uint8_t *src,               \
1118                                     ptrdiff_t stride)                         \
1119 {                                                                             \
1120     uint8_t halfH[272];                                                       \
1121     uint8_t halfHV[256];                                                      \
1122                                                                               \
1123     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);         \
1124     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1125     OPNAME ## pixels16_l2_8(dst, halfH + 16, halfHV, stride, 16, 16, 16);     \
1126 }                                                                             \
1127                                                                               \
1128 void ff_ ## OPNAME ## qpel16_mc12_old_c(uint8_t *dst, uint8_t *src,           \
1129                                         ptrdiff_t stride)                     \
1130 {                                                                             \
1131     uint8_t full[24 * 17];                                                    \
1132     uint8_t halfH[272];                                                       \
1133     uint8_t halfV[256];                                                       \
1134     uint8_t halfHV[256];                                                      \
1135                                                                               \
1136     copy_block17(full, src, 24, stride, 17);                                  \
1137     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1138     put ## RND ## mpeg4_qpel16_v_lowpass(halfV, full, 16, 24);                \
1139     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1140     OPNAME ## pixels16_l2_8(dst, halfV, halfHV, stride, 16, 16, 16);          \
1141 }                                                                             \
1142                                                                               \
1143 static void OPNAME ## qpel16_mc12_c(uint8_t *dst, uint8_t *src,               \
1144                                     ptrdiff_t stride)                         \
1145 {                                                                             \
1146     uint8_t full[24 * 17];                                                    \
1147     uint8_t halfH[272];                                                       \
1148                                                                               \
1149     copy_block17(full, src, 24, stride, 17);                                  \
1150     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1151     put ## RND ## pixels16_l2_8(halfH, halfH, full, 16, 16, 24, 17);          \
1152     OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);                 \
1153 }                                                                             \
1154                                                                               \
1155 void ff_ ## OPNAME ## qpel16_mc32_old_c(uint8_t *dst, uint8_t *src,           \
1156                                         ptrdiff_t stride)                     \
1157 {                                                                             \
1158     uint8_t full[24 * 17];                                                    \
1159     uint8_t halfH[272];                                                       \
1160     uint8_t halfV[256];                                                       \
1161     uint8_t halfHV[256];                                                      \
1162                                                                               \
1163     copy_block17(full, src, 24, stride, 17);                                  \
1164     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1165     put ## RND ## mpeg4_qpel16_v_lowpass(halfV, full + 1, 16, 24);            \
1166     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);              \
1167     OPNAME ## pixels16_l2_8(dst, halfV, halfHV, stride, 16, 16, 16);          \
1168 }                                                                             \
1169                                                                               \
1170 static void OPNAME ## qpel16_mc32_c(uint8_t *dst, uint8_t *src,               \
1171                                     ptrdiff_t stride)                         \
1172 {                                                                             \
1173     uint8_t full[24 * 17];                                                    \
1174     uint8_t halfH[272];                                                       \
1175                                                                               \
1176     copy_block17(full, src, 24, stride, 17);                                  \
1177     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);            \
1178     put ## RND ## pixels16_l2_8(halfH, halfH, full + 1, 16, 16, 24, 17);      \
1179     OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);                 \
1180 }                                                                             \
1181                                                                               \
1182 static void OPNAME ## qpel16_mc22_c(uint8_t *dst, uint8_t *src,               \
1183                                     ptrdiff_t stride)                         \
1184 {                                                                             \
1185     uint8_t halfH[272];                                                       \
1186                                                                               \
1187     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);         \
1188     OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);                 \
1189 }
1190
1191 #define op_avg(a, b)        a = (((a) + cm[((b) + 16) >> 5] + 1) >> 1)
1192 #define op_avg_no_rnd(a, b) a = (((a) + cm[((b) + 15) >> 5])     >> 1)
1193 #define op_put(a, b)        a = cm[((b) + 16) >> 5]
1194 #define op_put_no_rnd(a, b) a = cm[((b) + 15) >> 5]
1195
1196 QPEL_MC(0, put_, _, op_put)
1197 QPEL_MC(1, put_no_rnd_, _no_rnd_, op_put_no_rnd)
1198 QPEL_MC(0, avg_, _, op_avg)
1199
1200 #undef op_avg
1201 #undef op_put
1202 #undef op_put_no_rnd
1203
1204 void ff_put_pixels8x8_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1205 {
1206     put_pixels8_8_c(dst, src, stride, 8);
1207 }
1208
1209 void ff_avg_pixels8x8_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1210 {
1211     avg_pixels8_8_c(dst, src, stride, 8);
1212 }
1213
1214 void ff_put_pixels16x16_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1215 {
1216     put_pixels16_8_c(dst, src, stride, 16);
1217 }
1218
1219 void ff_avg_pixels16x16_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1220 {
1221     avg_pixels16_8_c(dst, src, stride, 16);
1222 }
1223
1224 #define put_qpel8_mc00_c         ff_put_pixels8x8_c
1225 #define avg_qpel8_mc00_c         ff_avg_pixels8x8_c
1226 #define put_qpel16_mc00_c        ff_put_pixels16x16_c
1227 #define avg_qpel16_mc00_c        ff_avg_pixels16x16_c
1228 #define put_no_rnd_qpel8_mc00_c  ff_put_pixels8x8_c
1229 #define put_no_rnd_qpel16_mc00_c ff_put_pixels16x16_c
1230
1231 static void wmv2_mspel8_h_lowpass(uint8_t *dst, uint8_t *src,
1232                                   int dstStride, int srcStride, int h)
1233 {
1234     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
1235     int i;
1236
1237     for (i = 0; i < h; i++) {
1238         dst[0] = cm[(9 * (src[0] + src[1]) - (src[-1] + src[2]) + 8) >> 4];
1239         dst[1] = cm[(9 * (src[1] + src[2]) - (src[0]  + src[3]) + 8) >> 4];
1240         dst[2] = cm[(9 * (src[2] + src[3]) - (src[1]  + src[4]) + 8) >> 4];
1241         dst[3] = cm[(9 * (src[3] + src[4]) - (src[2]  + src[5]) + 8) >> 4];
1242         dst[4] = cm[(9 * (src[4] + src[5]) - (src[3]  + src[6]) + 8) >> 4];
1243         dst[5] = cm[(9 * (src[5] + src[6]) - (src[4]  + src[7]) + 8) >> 4];
1244         dst[6] = cm[(9 * (src[6] + src[7]) - (src[5]  + src[8]) + 8) >> 4];
1245         dst[7] = cm[(9 * (src[7] + src[8]) - (src[6]  + src[9]) + 8) >> 4];
1246         dst   += dstStride;
1247         src   += srcStride;
1248     }
1249 }
1250
1251 #if CONFIG_RV40_DECODER
1252 void ff_put_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1253 {
1254     put_pixels16_xy2_8_c(dst, src, stride, 16);
1255 }
1256
1257 void ff_avg_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1258 {
1259     avg_pixels16_xy2_8_c(dst, src, stride, 16);
1260 }
1261
1262 void ff_put_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1263 {
1264     put_pixels8_xy2_8_c(dst, src, stride, 8);
1265 }
1266
1267 void ff_avg_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1268 {
1269     avg_pixels8_xy2_8_c(dst, src, stride, 8);
1270 }
1271 #endif /* CONFIG_RV40_DECODER */
1272
1273 static void wmv2_mspel8_v_lowpass(uint8_t *dst, uint8_t *src,
1274                                   int dstStride, int srcStride, int w)
1275 {
1276     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
1277     int i;
1278
1279     for (i = 0; i < w; i++) {
1280         const int src_1 = src[-srcStride];
1281         const int src0  = src[0];
1282         const int src1  = src[srcStride];
1283         const int src2  = src[2 * srcStride];
1284         const int src3  = src[3 * srcStride];
1285         const int src4  = src[4 * srcStride];
1286         const int src5  = src[5 * srcStride];
1287         const int src6  = src[6 * srcStride];
1288         const int src7  = src[7 * srcStride];
1289         const int src8  = src[8 * srcStride];
1290         const int src9  = src[9 * srcStride];
1291         dst[0 * dstStride] = cm[(9 * (src0 + src1) - (src_1 + src2) + 8) >> 4];
1292         dst[1 * dstStride] = cm[(9 * (src1 + src2) - (src0  + src3) + 8) >> 4];
1293         dst[2 * dstStride] = cm[(9 * (src2 + src3) - (src1  + src4) + 8) >> 4];
1294         dst[3 * dstStride] = cm[(9 * (src3 + src4) - (src2  + src5) + 8) >> 4];
1295         dst[4 * dstStride] = cm[(9 * (src4 + src5) - (src3  + src6) + 8) >> 4];
1296         dst[5 * dstStride] = cm[(9 * (src5 + src6) - (src4  + src7) + 8) >> 4];
1297         dst[6 * dstStride] = cm[(9 * (src6 + src7) - (src5  + src8) + 8) >> 4];
1298         dst[7 * dstStride] = cm[(9 * (src7 + src8) - (src6  + src9) + 8) >> 4];
1299         src++;
1300         dst++;
1301     }
1302 }
1303
1304 static void put_mspel8_mc10_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1305 {
1306     uint8_t half[64];
1307
1308     wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
1309     put_pixels8_l2_8(dst, src, half, stride, stride, 8, 8);
1310 }
1311
1312 static void put_mspel8_mc20_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1313 {
1314     wmv2_mspel8_h_lowpass(dst, src, stride, stride, 8);
1315 }
1316
1317 static void put_mspel8_mc30_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1318 {
1319     uint8_t half[64];
1320
1321     wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
1322     put_pixels8_l2_8(dst, src + 1, half, stride, stride, 8, 8);
1323 }
1324
1325 static void put_mspel8_mc02_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1326 {
1327     wmv2_mspel8_v_lowpass(dst, src, stride, stride, 8);
1328 }
1329
1330 static void put_mspel8_mc12_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1331 {
1332     uint8_t halfH[88];
1333     uint8_t halfV[64];
1334     uint8_t halfHV[64];
1335
1336     wmv2_mspel8_h_lowpass(halfH, src - stride, 8, stride, 11);
1337     wmv2_mspel8_v_lowpass(halfV, src, 8, stride, 8);
1338     wmv2_mspel8_v_lowpass(halfHV, halfH + 8, 8, 8, 8);
1339     put_pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);
1340 }
1341
1342 static void put_mspel8_mc32_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1343 {
1344     uint8_t halfH[88];
1345     uint8_t halfV[64];
1346     uint8_t halfHV[64];
1347
1348     wmv2_mspel8_h_lowpass(halfH, src - stride, 8, stride, 11);
1349     wmv2_mspel8_v_lowpass(halfV, src + 1, 8, stride, 8);
1350     wmv2_mspel8_v_lowpass(halfHV, halfH + 8, 8, 8, 8);
1351     put_pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);
1352 }
1353
1354 static void put_mspel8_mc22_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
1355 {
1356     uint8_t halfH[88];
1357
1358     wmv2_mspel8_h_lowpass(halfH, src - stride, 8, stride, 11);
1359     wmv2_mspel8_v_lowpass(dst, halfH + 8, stride, 8, 8);
1360 }
1361
1362 static inline int pix_abs16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
1363                               int line_size, int h)
1364 {
1365     int s = 0, i;
1366
1367     for (i = 0; i < h; i++) {
1368         s    += abs(pix1[0]  - pix2[0]);
1369         s    += abs(pix1[1]  - pix2[1]);
1370         s    += abs(pix1[2]  - pix2[2]);
1371         s    += abs(pix1[3]  - pix2[3]);
1372         s    += abs(pix1[4]  - pix2[4]);
1373         s    += abs(pix1[5]  - pix2[5]);
1374         s    += abs(pix1[6]  - pix2[6]);
1375         s    += abs(pix1[7]  - pix2[7]);
1376         s    += abs(pix1[8]  - pix2[8]);
1377         s    += abs(pix1[9]  - pix2[9]);
1378         s    += abs(pix1[10] - pix2[10]);
1379         s    += abs(pix1[11] - pix2[11]);
1380         s    += abs(pix1[12] - pix2[12]);
1381         s    += abs(pix1[13] - pix2[13]);
1382         s    += abs(pix1[14] - pix2[14]);
1383         s    += abs(pix1[15] - pix2[15]);
1384         pix1 += line_size;
1385         pix2 += line_size;
1386     }
1387     return s;
1388 }
1389
1390 static int pix_abs16_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
1391                           int line_size, int h)
1392 {
1393     int s = 0, i;
1394
1395     for (i = 0; i < h; i++) {
1396         s    += abs(pix1[0]  - avg2(pix2[0],  pix2[1]));
1397         s    += abs(pix1[1]  - avg2(pix2[1],  pix2[2]));
1398         s    += abs(pix1[2]  - avg2(pix2[2],  pix2[3]));
1399         s    += abs(pix1[3]  - avg2(pix2[3],  pix2[4]));
1400         s    += abs(pix1[4]  - avg2(pix2[4],  pix2[5]));
1401         s    += abs(pix1[5]  - avg2(pix2[5],  pix2[6]));
1402         s    += abs(pix1[6]  - avg2(pix2[6],  pix2[7]));
1403         s    += abs(pix1[7]  - avg2(pix2[7],  pix2[8]));
1404         s    += abs(pix1[8]  - avg2(pix2[8],  pix2[9]));
1405         s    += abs(pix1[9]  - avg2(pix2[9],  pix2[10]));
1406         s    += abs(pix1[10] - avg2(pix2[10], pix2[11]));
1407         s    += abs(pix1[11] - avg2(pix2[11], pix2[12]));
1408         s    += abs(pix1[12] - avg2(pix2[12], pix2[13]));
1409         s    += abs(pix1[13] - avg2(pix2[13], pix2[14]));
1410         s    += abs(pix1[14] - avg2(pix2[14], pix2[15]));
1411         s    += abs(pix1[15] - avg2(pix2[15], pix2[16]));
1412         pix1 += line_size;
1413         pix2 += line_size;
1414     }
1415     return s;
1416 }
1417
1418 static int pix_abs16_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
1419                           int line_size, int h)
1420 {
1421     int s = 0, i;
1422     uint8_t *pix3 = pix2 + line_size;
1423
1424     for (i = 0; i < h; i++) {
1425         s    += abs(pix1[0]  - avg2(pix2[0],  pix3[0]));
1426         s    += abs(pix1[1]  - avg2(pix2[1],  pix3[1]));
1427         s    += abs(pix1[2]  - avg2(pix2[2],  pix3[2]));
1428         s    += abs(pix1[3]  - avg2(pix2[3],  pix3[3]));
1429         s    += abs(pix1[4]  - avg2(pix2[4],  pix3[4]));
1430         s    += abs(pix1[5]  - avg2(pix2[5],  pix3[5]));
1431         s    += abs(pix1[6]  - avg2(pix2[6],  pix3[6]));
1432         s    += abs(pix1[7]  - avg2(pix2[7],  pix3[7]));
1433         s    += abs(pix1[8]  - avg2(pix2[8],  pix3[8]));
1434         s    += abs(pix1[9]  - avg2(pix2[9],  pix3[9]));
1435         s    += abs(pix1[10] - avg2(pix2[10], pix3[10]));
1436         s    += abs(pix1[11] - avg2(pix2[11], pix3[11]));
1437         s    += abs(pix1[12] - avg2(pix2[12], pix3[12]));
1438         s    += abs(pix1[13] - avg2(pix2[13], pix3[13]));
1439         s    += abs(pix1[14] - avg2(pix2[14], pix3[14]));
1440         s    += abs(pix1[15] - avg2(pix2[15], pix3[15]));
1441         pix1 += line_size;
1442         pix2 += line_size;
1443         pix3 += line_size;
1444     }
1445     return s;
1446 }
1447
1448 static int pix_abs16_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
1449                            int line_size, int h)
1450 {
1451     int s = 0, i;
1452     uint8_t *pix3 = pix2 + line_size;
1453
1454     for (i = 0; i < h; i++) {
1455         s    += abs(pix1[0]  - avg4(pix2[0],  pix2[1],  pix3[0],  pix3[1]));
1456         s    += abs(pix1[1]  - avg4(pix2[1],  pix2[2],  pix3[1],  pix3[2]));
1457         s    += abs(pix1[2]  - avg4(pix2[2],  pix2[3],  pix3[2],  pix3[3]));
1458         s    += abs(pix1[3]  - avg4(pix2[3],  pix2[4],  pix3[3],  pix3[4]));
1459         s    += abs(pix1[4]  - avg4(pix2[4],  pix2[5],  pix3[4],  pix3[5]));
1460         s    += abs(pix1[5]  - avg4(pix2[5],  pix2[6],  pix3[5],  pix3[6]));
1461         s    += abs(pix1[6]  - avg4(pix2[6],  pix2[7],  pix3[6],  pix3[7]));
1462         s    += abs(pix1[7]  - avg4(pix2[7],  pix2[8],  pix3[7],  pix3[8]));
1463         s    += abs(pix1[8]  - avg4(pix2[8],  pix2[9],  pix3[8],  pix3[9]));
1464         s    += abs(pix1[9]  - avg4(pix2[9],  pix2[10], pix3[9],  pix3[10]));
1465         s    += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
1466         s    += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
1467         s    += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
1468         s    += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
1469         s    += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
1470         s    += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
1471         pix1 += line_size;
1472         pix2 += line_size;
1473         pix3 += line_size;
1474     }
1475     return s;
1476 }
1477
1478 static inline int pix_abs8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
1479                              int line_size, int h)
1480 {
1481     int s = 0, i;
1482
1483     for (i = 0; i < h; i++) {
1484         s    += abs(pix1[0] - pix2[0]);
1485         s    += abs(pix1[1] - pix2[1]);
1486         s    += abs(pix1[2] - pix2[2]);
1487         s    += abs(pix1[3] - pix2[3]);
1488         s    += abs(pix1[4] - pix2[4]);
1489         s    += abs(pix1[5] - pix2[5]);
1490         s    += abs(pix1[6] - pix2[6]);
1491         s    += abs(pix1[7] - pix2[7]);
1492         pix1 += line_size;
1493         pix2 += line_size;
1494     }
1495     return s;
1496 }
1497
1498 static int pix_abs8_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
1499                          int line_size, int h)
1500 {
1501     int s = 0, i;
1502
1503     for (i = 0; i < h; i++) {
1504         s    += abs(pix1[0] - avg2(pix2[0], pix2[1]));
1505         s    += abs(pix1[1] - avg2(pix2[1], pix2[2]));
1506         s    += abs(pix1[2] - avg2(pix2[2], pix2[3]));
1507         s    += abs(pix1[3] - avg2(pix2[3], pix2[4]));
1508         s    += abs(pix1[4] - avg2(pix2[4], pix2[5]));
1509         s    += abs(pix1[5] - avg2(pix2[5], pix2[6]));
1510         s    += abs(pix1[6] - avg2(pix2[6], pix2[7]));
1511         s    += abs(pix1[7] - avg2(pix2[7], pix2[8]));
1512         pix1 += line_size;
1513         pix2 += line_size;
1514     }
1515     return s;
1516 }
1517
1518 static int pix_abs8_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
1519                          int line_size, int h)
1520 {
1521     int s = 0, i;
1522     uint8_t *pix3 = pix2 + line_size;
1523
1524     for (i = 0; i < h; i++) {
1525         s    += abs(pix1[0] - avg2(pix2[0], pix3[0]));
1526         s    += abs(pix1[1] - avg2(pix2[1], pix3[1]));
1527         s    += abs(pix1[2] - avg2(pix2[2], pix3[2]));
1528         s    += abs(pix1[3] - avg2(pix2[3], pix3[3]));
1529         s    += abs(pix1[4] - avg2(pix2[4], pix3[4]));
1530         s    += abs(pix1[5] - avg2(pix2[5], pix3[5]));
1531         s    += abs(pix1[6] - avg2(pix2[6], pix3[6]));
1532         s    += abs(pix1[7] - avg2(pix2[7], pix3[7]));
1533         pix1 += line_size;
1534         pix2 += line_size;
1535         pix3 += line_size;
1536     }
1537     return s;
1538 }
1539
1540 static int pix_abs8_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
1541                           int line_size, int h)
1542 {
1543     int s = 0, i;
1544     uint8_t *pix3 = pix2 + line_size;
1545
1546     for (i = 0; i < h; i++) {
1547         s    += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
1548         s    += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
1549         s    += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
1550         s    += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
1551         s    += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
1552         s    += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
1553         s    += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
1554         s    += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
1555         pix1 += line_size;
1556         pix2 += line_size;
1557         pix3 += line_size;
1558     }
1559     return s;
1560 }
1561
1562 static int nsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
1563 {
1564     int score1 = 0, score2 = 0, x, y;
1565
1566     for (y = 0; y < h; y++) {
1567         for (x = 0; x < 16; x++)
1568             score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
1569         if (y + 1 < h) {
1570             for (x = 0; x < 15; x++)
1571                 score2 += FFABS(s1[x]     - s1[x + stride] -
1572                                 s1[x + 1] + s1[x + stride + 1]) -
1573                           FFABS(s2[x]     - s2[x + stride] -
1574                                 s2[x + 1] + s2[x + stride + 1]);
1575         }
1576         s1 += stride;
1577         s2 += stride;
1578     }
1579
1580     if (c)
1581         return score1 + FFABS(score2) * c->avctx->nsse_weight;
1582     else
1583         return score1 + FFABS(score2) * 8;
1584 }
1585
1586 static int nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
1587 {
1588     int score1 = 0, score2 = 0, x, y;
1589
1590     for (y = 0; y < h; y++) {
1591         for (x = 0; x < 8; x++)
1592             score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
1593         if (y + 1 < h) {
1594             for (x = 0; x < 7; x++)
1595                 score2 += FFABS(s1[x]     - s1[x + stride] -
1596                                 s1[x + 1] + s1[x + stride + 1]) -
1597                           FFABS(s2[x]     - s2[x + stride] -
1598                                 s2[x + 1] + s2[x + stride + 1]);
1599         }
1600         s1 += stride;
1601         s2 += stride;
1602     }
1603
1604     if (c)
1605         return score1 + FFABS(score2) * c->avctx->nsse_weight;
1606     else
1607         return score1 + FFABS(score2) * 8;
1608 }
1609
1610 static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
1611                           int16_t basis[64], int scale)
1612 {
1613     int i;
1614     unsigned int sum = 0;
1615
1616     for (i = 0; i < 8 * 8; i++) {
1617         int b = rem[i] + ((basis[i] * scale +
1618                            (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
1619                           (BASIS_SHIFT - RECON_SHIFT));
1620         int w = weight[i];
1621         b >>= RECON_SHIFT;
1622         assert(-512 < b && b < 512);
1623
1624         sum += (w * b) * (w * b) >> 4;
1625     }
1626     return sum >> 2;
1627 }
1628
1629 static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
1630 {
1631     int i;
1632
1633     for (i = 0; i < 8 * 8; i++)
1634         rem[i] += (basis[i] * scale +
1635                    (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
1636                   (BASIS_SHIFT - RECON_SHIFT);
1637 }
1638
1639 static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
1640                     int stride, int h)
1641 {
1642     return 0;
1643 }
1644
1645 void ff_set_cmp(DSPContext *c, me_cmp_func *cmp, int type)
1646 {
1647     int i;
1648
1649     memset(cmp, 0, sizeof(void *) * 6);
1650
1651     for (i = 0; i < 6; i++) {
1652         switch (type & 0xFF) {
1653         case FF_CMP_SAD:
1654             cmp[i] = c->sad[i];
1655             break;
1656         case FF_CMP_SATD:
1657             cmp[i] = c->hadamard8_diff[i];
1658             break;
1659         case FF_CMP_SSE:
1660             cmp[i] = c->sse[i];
1661             break;
1662         case FF_CMP_DCT:
1663             cmp[i] = c->dct_sad[i];
1664             break;
1665         case FF_CMP_DCT264:
1666             cmp[i] = c->dct264_sad[i];
1667             break;
1668         case FF_CMP_DCTMAX:
1669             cmp[i] = c->dct_max[i];
1670             break;
1671         case FF_CMP_PSNR:
1672             cmp[i] = c->quant_psnr[i];
1673             break;
1674         case FF_CMP_BIT:
1675             cmp[i] = c->bit[i];
1676             break;
1677         case FF_CMP_RD:
1678             cmp[i] = c->rd[i];
1679             break;
1680         case FF_CMP_VSAD:
1681             cmp[i] = c->vsad[i];
1682             break;
1683         case FF_CMP_VSSE:
1684             cmp[i] = c->vsse[i];
1685             break;
1686         case FF_CMP_ZERO:
1687             cmp[i] = zero_cmp;
1688             break;
1689         case FF_CMP_NSSE:
1690             cmp[i] = c->nsse[i];
1691             break;
1692         default:
1693             av_log(NULL, AV_LOG_ERROR,
1694                    "internal error in cmp function selection\n");
1695         }
1696     }
1697 }
1698
1699 static void add_bytes_c(uint8_t *dst, uint8_t *src, int w)
1700 {
1701     long i;
1702
1703     for (i = 0; i <= w - (int) sizeof(long); i += sizeof(long)) {
1704         long a = *(long *) (src + i);
1705         long b = *(long *) (dst + i);
1706         *(long *) (dst + i) = ((a & pb_7f) + (b & pb_7f)) ^ ((a ^ b) & pb_80);
1707     }
1708     for (; i < w; i++)
1709         dst[i + 0] += src[i + 0];
1710 }
1711
1712 static void diff_bytes_c(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
1713 {
1714     long i;
1715
1716 #if !HAVE_FAST_UNALIGNED
1717     if ((long) src2 & (sizeof(long) - 1)) {
1718         for (i = 0; i + 7 < w; i += 8) {
1719             dst[i + 0] = src1[i + 0] - src2[i + 0];
1720             dst[i + 1] = src1[i + 1] - src2[i + 1];
1721             dst[i + 2] = src1[i + 2] - src2[i + 2];
1722             dst[i + 3] = src1[i + 3] - src2[i + 3];
1723             dst[i + 4] = src1[i + 4] - src2[i + 4];
1724             dst[i + 5] = src1[i + 5] - src2[i + 5];
1725             dst[i + 6] = src1[i + 6] - src2[i + 6];
1726             dst[i + 7] = src1[i + 7] - src2[i + 7];
1727         }
1728     } else
1729 #endif
1730     for (i = 0; i <= w - (int) sizeof(long); i += sizeof(long)) {
1731         long a = *(long *) (src1 + i);
1732         long b = *(long *) (src2 + i);
1733         *(long *) (dst + i) = ((a | pb_80) - (b & pb_7f)) ^
1734                               ((a ^ b ^ pb_80) & pb_80);
1735     }
1736     for (; i < w; i++)
1737         dst[i + 0] = src1[i + 0] - src2[i + 0];
1738 }
1739
1740 static void add_hfyu_median_prediction_c(uint8_t *dst, const uint8_t *src1,
1741                                          const uint8_t *diff, int w,
1742                                          int *left, int *left_top)
1743 {
1744     int i;
1745     uint8_t l, lt;
1746
1747     l  = *left;
1748     lt = *left_top;
1749
1750     for (i = 0; i < w; i++) {
1751         l      = mid_pred(l, src1[i], (l + src1[i] - lt) & 0xFF) + diff[i];
1752         lt     = src1[i];
1753         dst[i] = l;
1754     }
1755
1756     *left     = l;
1757     *left_top = lt;
1758 }
1759
1760 static void sub_hfyu_median_prediction_c(uint8_t *dst, const uint8_t *src1,
1761                                          const uint8_t *src2, int w,
1762                                          int *left, int *left_top)
1763 {
1764     int i;
1765     uint8_t l, lt;
1766
1767     l  = *left;
1768     lt = *left_top;
1769
1770     for (i = 0; i < w; i++) {
1771         const int pred = mid_pred(l, src1[i], (l + src1[i] - lt) & 0xFF);
1772         lt     = src1[i];
1773         l      = src2[i];
1774         dst[i] = l - pred;
1775     }
1776
1777     *left     = l;
1778     *left_top = lt;
1779 }
1780
1781 static int add_hfyu_left_prediction_c(uint8_t *dst, const uint8_t *src,
1782                                       int w, int acc)
1783 {
1784     int i;
1785
1786     for (i = 0; i < w - 1; i++) {
1787         acc   += src[i];
1788         dst[i] = acc;
1789         i++;
1790         acc   += src[i];
1791         dst[i] = acc;
1792     }
1793
1794     for (; i < w; i++) {
1795         acc   += src[i];
1796         dst[i] = acc;
1797     }
1798
1799     return acc;
1800 }
1801
1802 #if HAVE_BIGENDIAN
1803 #define B 3
1804 #define G 2
1805 #define R 1
1806 #define A 0
1807 #else
1808 #define B 0
1809 #define G 1
1810 #define R 2
1811 #define A 3
1812 #endif
1813 static void add_hfyu_left_prediction_bgr32_c(uint8_t *dst, const uint8_t *src,
1814                                              int w, int *red, int *green,
1815                                              int *blue, int *alpha)
1816 {
1817     int i, r = *red, g = *green, b = *blue, a = *alpha;
1818
1819     for (i = 0; i < w; i++) {
1820         b += src[4 * i + B];
1821         g += src[4 * i + G];
1822         r += src[4 * i + R];
1823         a += src[4 * i + A];
1824
1825         dst[4 * i + B] = b;
1826         dst[4 * i + G] = g;
1827         dst[4 * i + R] = r;
1828         dst[4 * i + A] = a;
1829     }
1830
1831     *red   = r;
1832     *green = g;
1833     *blue  = b;
1834     *alpha = a;
1835 }
1836 #undef B
1837 #undef G
1838 #undef R
1839 #undef A
1840
1841 #define BUTTERFLY2(o1, o2, i1, i2)              \
1842     o1 = (i1) + (i2);                           \
1843     o2 = (i1) - (i2);
1844
1845 #define BUTTERFLY1(x, y)                        \
1846     {                                           \
1847         int a, b;                               \
1848         a = x;                                  \
1849         b = y;                                  \
1850         x = a + b;                              \
1851         y = a - b;                              \
1852     }
1853
1854 #define BUTTERFLYA(x, y) (FFABS((x) + (y)) + FFABS((x) - (y)))
1855
1856 static int hadamard8_diff8x8_c(MpegEncContext *s, uint8_t *dst,
1857                                uint8_t *src, int stride, int h)
1858 {
1859     int i, temp[64], sum = 0;
1860
1861     assert(h == 8);
1862
1863     for (i = 0; i < 8; i++) {
1864         // FIXME: try pointer walks
1865         BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
1866                    src[stride * i + 0] - dst[stride * i + 0],
1867                    src[stride * i + 1] - dst[stride * i + 1]);
1868         BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
1869                    src[stride * i + 2] - dst[stride * i + 2],
1870                    src[stride * i + 3] - dst[stride * i + 3]);
1871         BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
1872                    src[stride * i + 4] - dst[stride * i + 4],
1873                    src[stride * i + 5] - dst[stride * i + 5]);
1874         BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
1875                    src[stride * i + 6] - dst[stride * i + 6],
1876                    src[stride * i + 7] - dst[stride * i + 7]);
1877
1878         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
1879         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
1880         BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
1881         BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
1882
1883         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
1884         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
1885         BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
1886         BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
1887     }
1888
1889     for (i = 0; i < 8; i++) {
1890         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
1891         BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
1892         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
1893         BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
1894
1895         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
1896         BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
1897         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
1898         BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
1899
1900         sum += BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i]) +
1901                BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i]) +
1902                BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i]) +
1903                BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
1904     }
1905     return sum;
1906 }
1907
1908 static int hadamard8_intra8x8_c(MpegEncContext *s, uint8_t *src,
1909                                 uint8_t *dummy, int stride, int h)
1910 {
1911     int i, temp[64], sum = 0;
1912
1913     assert(h == 8);
1914
1915     for (i = 0; i < 8; i++) {
1916         // FIXME: try pointer walks
1917         BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
1918                    src[stride * i + 0], src[stride * i + 1]);
1919         BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
1920                    src[stride * i + 2], src[stride * i + 3]);
1921         BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
1922                    src[stride * i + 4], src[stride * i + 5]);
1923         BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
1924                    src[stride * i + 6], src[stride * i + 7]);
1925
1926         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
1927         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
1928         BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
1929         BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
1930
1931         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
1932         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
1933         BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
1934         BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
1935     }
1936
1937     for (i = 0; i < 8; i++) {
1938         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
1939         BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
1940         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
1941         BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
1942
1943         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
1944         BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
1945         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
1946         BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
1947
1948         sum +=
1949             BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i])
1950             + BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i])
1951             + BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i])
1952             + BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
1953     }
1954
1955     sum -= FFABS(temp[8 * 0] + temp[8 * 4]); // -mean
1956
1957     return sum;
1958 }
1959
1960 static int dct_sad8x8_c(MpegEncContext *s, uint8_t *src1,
1961                         uint8_t *src2, int stride, int h)
1962 {
1963     LOCAL_ALIGNED_16(int16_t, temp, [64]);
1964
1965     assert(h == 8);
1966
1967     s->dsp.diff_pixels(temp, src1, src2, stride);
1968     s->dsp.fdct(temp);
1969     return s->dsp.sum_abs_dctelem(temp);
1970 }
1971
1972 #if CONFIG_GPL
1973 #define DCT8_1D                                         \
1974     {                                                   \
1975         const int s07 = SRC(0) + SRC(7);                \
1976         const int s16 = SRC(1) + SRC(6);                \
1977         const int s25 = SRC(2) + SRC(5);                \
1978         const int s34 = SRC(3) + SRC(4);                \
1979         const int a0  = s07 + s34;                      \
1980         const int a1  = s16 + s25;                      \
1981         const int a2  = s07 - s34;                      \
1982         const int a3  = s16 - s25;                      \
1983         const int d07 = SRC(0) - SRC(7);                \
1984         const int d16 = SRC(1) - SRC(6);                \
1985         const int d25 = SRC(2) - SRC(5);                \
1986         const int d34 = SRC(3) - SRC(4);                \
1987         const int a4  = d16 + d25 + (d07 + (d07 >> 1)); \
1988         const int a5  = d07 - d34 - (d25 + (d25 >> 1)); \
1989         const int a6  = d07 + d34 - (d16 + (d16 >> 1)); \
1990         const int a7  = d16 - d25 + (d34 + (d34 >> 1)); \
1991         DST(0, a0 + a1);                                \
1992         DST(1, a4 + (a7 >> 2));                         \
1993         DST(2, a2 + (a3 >> 1));                         \
1994         DST(3, a5 + (a6 >> 2));                         \
1995         DST(4, a0 - a1);                                \
1996         DST(5, a6 - (a5 >> 2));                         \
1997         DST(6, (a2 >> 1) - a3);                         \
1998         DST(7, (a4 >> 2) - a7);                         \
1999     }
2000
2001 static int dct264_sad8x8_c(MpegEncContext *s, uint8_t *src1,
2002                            uint8_t *src2, int stride, int h)
2003 {
2004     int16_t dct[8][8];
2005     int i, sum = 0;
2006
2007     s->dsp.diff_pixels(dct[0], src1, src2, stride);
2008
2009 #define SRC(x) dct[i][x]
2010 #define DST(x, v) dct[i][x] = v
2011     for (i = 0; i < 8; i++)
2012         DCT8_1D
2013 #undef SRC
2014 #undef DST
2015
2016 #define SRC(x) dct[x][i]
2017 #define DST(x, v) sum += FFABS(v)
2018         for (i = 0; i < 8; i++)
2019             DCT8_1D
2020 #undef SRC
2021 #undef DST
2022             return sum;
2023 }
2024 #endif
2025
2026 static int dct_max8x8_c(MpegEncContext *s, uint8_t *src1,
2027                         uint8_t *src2, int stride, int h)
2028 {
2029     LOCAL_ALIGNED_16(int16_t, temp, [64]);
2030     int sum = 0, i;
2031
2032     assert(h == 8);
2033
2034     s->dsp.diff_pixels(temp, src1, src2, stride);
2035     s->dsp.fdct(temp);
2036
2037     for (i = 0; i < 64; i++)
2038         sum = FFMAX(sum, FFABS(temp[i]));
2039
2040     return sum;
2041 }
2042
2043 static int quant_psnr8x8_c(MpegEncContext *s, uint8_t *src1,
2044                            uint8_t *src2, int stride, int h)
2045 {
2046     LOCAL_ALIGNED_16(int16_t, temp, [64 * 2]);
2047     int16_t *const bak = temp + 64;
2048     int sum = 0, i;
2049
2050     assert(h == 8);
2051     s->mb_intra = 0;
2052
2053     s->dsp.diff_pixels(temp, src1, src2, stride);
2054
2055     memcpy(bak, temp, 64 * sizeof(int16_t));
2056
2057     s->block_last_index[0 /* FIXME */] =
2058         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
2059     s->dct_unquantize_inter(s, temp, 0, s->qscale);
2060     ff_simple_idct_8(temp); // FIXME
2061
2062     for (i = 0; i < 64; i++)
2063         sum += (temp[i] - bak[i]) * (temp[i] - bak[i]);
2064
2065     return sum;
2066 }
2067
2068 static int rd8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
2069                    int stride, int h)
2070 {
2071     const uint8_t *scantable = s->intra_scantable.permutated;
2072     LOCAL_ALIGNED_16(int16_t, temp, [64]);
2073     LOCAL_ALIGNED_16(uint8_t, lsrc1, [64]);
2074     LOCAL_ALIGNED_16(uint8_t, lsrc2, [64]);
2075     int i, last, run, bits, level, distortion, start_i;
2076     const int esc_length = s->ac_esc_length;
2077     uint8_t *length, *last_length;
2078
2079     assert(h == 8);
2080
2081     copy_block8(lsrc1, src1, 8, stride, 8);
2082     copy_block8(lsrc2, src2, 8, stride, 8);
2083
2084     s->dsp.diff_pixels(temp, lsrc1, lsrc2, 8);
2085
2086     s->block_last_index[0 /* FIXME */] =
2087     last                               =
2088         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
2089
2090     bits = 0;
2091
2092     if (s->mb_intra) {
2093         start_i     = 1;
2094         length      = s->intra_ac_vlc_length;
2095         last_length = s->intra_ac_vlc_last_length;
2096         bits       += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
2097     } else {
2098         start_i     = 0;
2099         length      = s->inter_ac_vlc_length;
2100         last_length = s->inter_ac_vlc_last_length;
2101     }
2102
2103     if (last >= start_i) {
2104         run = 0;
2105         for (i = start_i; i < last; i++) {
2106             int j = scantable[i];
2107             level = temp[j];
2108
2109             if (level) {
2110                 level += 64;
2111                 if ((level & (~127)) == 0)
2112                     bits += length[UNI_AC_ENC_INDEX(run, level)];
2113                 else
2114                     bits += esc_length;
2115                 run = 0;
2116             } else
2117                 run++;
2118         }
2119         i = scantable[last];
2120
2121         level = temp[i] + 64;
2122
2123         assert(level - 64);
2124
2125         if ((level & (~127)) == 0) {
2126             bits += last_length[UNI_AC_ENC_INDEX(run, level)];
2127         } else
2128             bits += esc_length;
2129     }
2130
2131     if (last >= 0) {
2132         if (s->mb_intra)
2133             s->dct_unquantize_intra(s, temp, 0, s->qscale);
2134         else
2135             s->dct_unquantize_inter(s, temp, 0, s->qscale);
2136     }
2137
2138     s->dsp.idct_add(lsrc2, 8, temp);
2139
2140     distortion = s->dsp.sse[1](NULL, lsrc2, lsrc1, 8, 8);
2141
2142     return distortion + ((bits * s->qscale * s->qscale * 109 + 64) >> 7);
2143 }
2144
2145 static int bit8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
2146                     int stride, int h)
2147 {
2148     const uint8_t *scantable = s->intra_scantable.permutated;
2149     LOCAL_ALIGNED_16(int16_t, temp, [64]);
2150     int i, last, run, bits, level, start_i;
2151     const int esc_length = s->ac_esc_length;
2152     uint8_t *length, *last_length;
2153
2154     assert(h == 8);
2155
2156     s->dsp.diff_pixels(temp, src1, src2, stride);
2157
2158     s->block_last_index[0 /* FIXME */] =
2159     last                               =
2160         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
2161
2162     bits = 0;
2163
2164     if (s->mb_intra) {
2165         start_i     = 1;
2166         length      = s->intra_ac_vlc_length;
2167         last_length = s->intra_ac_vlc_last_length;
2168         bits       += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
2169     } else {
2170         start_i     = 0;
2171         length      = s->inter_ac_vlc_length;
2172         last_length = s->inter_ac_vlc_last_length;
2173     }
2174
2175     if (last >= start_i) {
2176         run = 0;
2177         for (i = start_i; i < last; i++) {
2178             int j = scantable[i];
2179             level = temp[j];
2180
2181             if (level) {
2182                 level += 64;
2183                 if ((level & (~127)) == 0)
2184                     bits += length[UNI_AC_ENC_INDEX(run, level)];
2185                 else
2186                     bits += esc_length;
2187                 run = 0;
2188             } else
2189                 run++;
2190         }
2191         i = scantable[last];
2192
2193         level = temp[i] + 64;
2194
2195         assert(level - 64);
2196
2197         if ((level & (~127)) == 0)
2198             bits += last_length[UNI_AC_ENC_INDEX(run, level)];
2199         else
2200             bits += esc_length;
2201     }
2202
2203     return bits;
2204 }
2205
2206 #define VSAD_INTRA(size)                                                \
2207 static int vsad_intra ## size ## _c(MpegEncContext *c,                  \
2208                                     uint8_t *s, uint8_t *dummy,         \
2209                                     int stride, int h)                  \
2210 {                                                                       \
2211     int score = 0, x, y;                                                \
2212                                                                         \
2213     for (y = 1; y < h; y++) {                                           \
2214         for (x = 0; x < size; x += 4) {                                 \
2215             score += FFABS(s[x]     - s[x + stride])     +              \
2216                      FFABS(s[x + 1] - s[x + stride + 1]) +              \
2217                      FFABS(s[x + 2] - s[x + 2 + stride]) +              \
2218                      FFABS(s[x + 3] - s[x + 3 + stride]);               \
2219         }                                                               \
2220         s += stride;                                                    \
2221     }                                                                   \
2222                                                                         \
2223     return score;                                                       \
2224 }
2225 VSAD_INTRA(8)
2226 VSAD_INTRA(16)
2227
2228 static int vsad16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,
2229                     int stride, int h)
2230 {
2231     int score = 0, x, y;
2232
2233     for (y = 1; y < h; y++) {
2234         for (x = 0; x < 16; x++)
2235             score += FFABS(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);
2236         s1 += stride;
2237         s2 += stride;
2238     }
2239
2240     return score;
2241 }
2242
2243 #define SQ(a) ((a) * (a))
2244 #define VSSE_INTRA(size)                                                \
2245 static int vsse_intra ## size ## _c(MpegEncContext *c,                  \
2246                                     uint8_t *s, uint8_t *dummy,         \
2247                                     int stride, int h)                  \
2248 {                                                                       \
2249     int score = 0, x, y;                                                \
2250                                                                         \
2251     for (y = 1; y < h; y++) {                                           \
2252         for (x = 0; x < size; x += 4) {                                 \
2253             score += SQ(s[x]     - s[x + stride]) +                     \
2254                      SQ(s[x + 1] - s[x + stride + 1]) +                 \
2255                      SQ(s[x + 2] - s[x + stride + 2]) +                 \
2256                      SQ(s[x + 3] - s[x + stride + 3]);                  \
2257         }                                                               \
2258         s += stride;                                                    \
2259     }                                                                   \
2260                                                                         \
2261     return score;                                                       \
2262 }
2263 VSSE_INTRA(8)
2264 VSSE_INTRA(16)
2265
2266 static int vsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,
2267                     int stride, int h)
2268 {
2269     int score = 0, x, y;
2270
2271     for (y = 1; y < h; y++) {
2272         for (x = 0; x < 16; x++)
2273             score += SQ(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);
2274         s1 += stride;
2275         s2 += stride;
2276     }
2277
2278     return score;
2279 }
2280
2281 static int ssd_int8_vs_int16_c(const int8_t *pix1, const int16_t *pix2,
2282                                int size)
2283 {
2284     int score = 0, i;
2285
2286     for (i = 0; i < size; i++)
2287         score += (pix1[i] - pix2[i]) * (pix1[i] - pix2[i]);
2288     return score;
2289 }
2290
2291 #define WRAPPER8_16_SQ(name8, name16)                                   \
2292 static int name16(MpegEncContext *s, uint8_t *dst, uint8_t *src,        \
2293                   int stride, int h)                                    \
2294 {                                                                       \
2295     int score = 0;                                                      \
2296                                                                         \
2297     score += name8(s, dst, src, stride, 8);                             \
2298     score += name8(s, dst + 8, src + 8, stride, 8);                     \
2299     if (h == 16) {                                                      \
2300         dst   += 8 * stride;                                            \
2301         src   += 8 * stride;                                            \
2302         score += name8(s, dst, src, stride, 8);                         \
2303         score += name8(s, dst + 8, src + 8, stride, 8);                 \
2304     }                                                                   \
2305     return score;                                                       \
2306 }
2307
2308 WRAPPER8_16_SQ(hadamard8_diff8x8_c, hadamard8_diff16_c)
2309 WRAPPER8_16_SQ(hadamard8_intra8x8_c, hadamard8_intra16_c)
2310 WRAPPER8_16_SQ(dct_sad8x8_c, dct_sad16_c)
2311 #if CONFIG_GPL
2312 WRAPPER8_16_SQ(dct264_sad8x8_c, dct264_sad16_c)
2313 #endif
2314 WRAPPER8_16_SQ(dct_max8x8_c, dct_max16_c)
2315 WRAPPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c)
2316 WRAPPER8_16_SQ(rd8x8_c, rd16_c)
2317 WRAPPER8_16_SQ(bit8x8_c, bit16_c)
2318
2319 static inline uint32_t clipf_c_one(uint32_t a, uint32_t mini,
2320                                    uint32_t maxi, uint32_t maxisign)
2321 {
2322     if (a > mini)
2323         return mini;
2324     else if ((a ^ (1U << 31)) > maxisign)
2325         return maxi;
2326     else
2327         return a;
2328 }
2329
2330 static void vector_clipf_c_opposite_sign(float *dst, const float *src,
2331                                          float *min, float *max, int len)
2332 {
2333     int i;
2334     uint32_t mini        = *(uint32_t *) min;
2335     uint32_t maxi        = *(uint32_t *) max;
2336     uint32_t maxisign    = maxi ^ (1U << 31);
2337     uint32_t *dsti       = (uint32_t *) dst;
2338     const uint32_t *srci = (const uint32_t *) src;
2339
2340     for (i = 0; i < len; i += 8) {
2341         dsti[i + 0] = clipf_c_one(srci[i + 0], mini, maxi, maxisign);
2342         dsti[i + 1] = clipf_c_one(srci[i + 1], mini, maxi, maxisign);
2343         dsti[i + 2] = clipf_c_one(srci[i + 2], mini, maxi, maxisign);
2344         dsti[i + 3] = clipf_c_one(srci[i + 3], mini, maxi, maxisign);
2345         dsti[i + 4] = clipf_c_one(srci[i + 4], mini, maxi, maxisign);
2346         dsti[i + 5] = clipf_c_one(srci[i + 5], mini, maxi, maxisign);
2347         dsti[i + 6] = clipf_c_one(srci[i + 6], mini, maxi, maxisign);
2348         dsti[i + 7] = clipf_c_one(srci[i + 7], mini, maxi, maxisign);
2349     }
2350 }
2351
2352 static void vector_clipf_c(float *dst, const float *src,
2353                            float min, float max, int len)
2354 {
2355     int i;
2356
2357     if (min < 0 && max > 0) {
2358         vector_clipf_c_opposite_sign(dst, src, &min, &max, len);
2359     } else {
2360         for (i = 0; i < len; i += 8) {
2361             dst[i]     = av_clipf(src[i], min, max);
2362             dst[i + 1] = av_clipf(src[i + 1], min, max);
2363             dst[i + 2] = av_clipf(src[i + 2], min, max);
2364             dst[i + 3] = av_clipf(src[i + 3], min, max);
2365             dst[i + 4] = av_clipf(src[i + 4], min, max);
2366             dst[i + 5] = av_clipf(src[i + 5], min, max);
2367             dst[i + 6] = av_clipf(src[i + 6], min, max);
2368             dst[i + 7] = av_clipf(src[i + 7], min, max);
2369         }
2370     }
2371 }
2372
2373 static int32_t scalarproduct_int16_c(const int16_t *v1, const int16_t *v2,
2374                                      int order)
2375 {
2376     int res = 0;
2377
2378     while (order--)
2379         res += *v1++ **v2++;
2380
2381     return res;
2382 }
2383
2384 static int32_t scalarproduct_and_madd_int16_c(int16_t *v1, const int16_t *v2,
2385                                               const int16_t *v3,
2386                                               int order, int mul)
2387 {
2388     int res = 0;
2389
2390     while (order--) {
2391         res   += *v1 * *v2++;
2392         *v1++ += mul * *v3++;
2393     }
2394     return res;
2395 }
2396
2397 static void vector_clip_int32_c(int32_t *dst, const int32_t *src, int32_t min,
2398                                 int32_t max, unsigned int len)
2399 {
2400     do {
2401         *dst++ = av_clip(*src++, min, max);
2402         *dst++ = av_clip(*src++, min, max);
2403         *dst++ = av_clip(*src++, min, max);
2404         *dst++ = av_clip(*src++, min, max);
2405         *dst++ = av_clip(*src++, min, max);
2406         *dst++ = av_clip(*src++, min, max);
2407         *dst++ = av_clip(*src++, min, max);
2408         *dst++ = av_clip(*src++, min, max);
2409         len   -= 8;
2410     } while (len > 0);
2411 }
2412
2413 static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
2414 {
2415     ff_j_rev_dct(block);
2416     put_pixels_clamped_c(block, dest, line_size);
2417 }
2418
2419 static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
2420 {
2421     ff_j_rev_dct(block);
2422     add_pixels_clamped_c(block, dest, line_size);
2423 }
2424
2425 /* init static data */
2426 av_cold void ff_dsputil_static_init(void)
2427 {
2428     int i;
2429
2430     for (i = 0; i < 512; i++)
2431         ff_square_tab[i] = (i - 256) * (i - 256);
2432 }
2433
2434 av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
2435 {
2436     const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
2437
2438 #if CONFIG_ENCODERS
2439     if (avctx->bits_per_raw_sample == 10) {
2440         c->fdct    = ff_jpeg_fdct_islow_10;
2441         c->fdct248 = ff_fdct248_islow_10;
2442     } else {
2443         if (avctx->dct_algo == FF_DCT_FASTINT) {
2444             c->fdct    = ff_fdct_ifast;
2445             c->fdct248 = ff_fdct_ifast248;
2446         } else if (avctx->dct_algo == FF_DCT_FAAN) {
2447             c->fdct    = ff_faandct;
2448             c->fdct248 = ff_faandct248;
2449         } else {
2450             c->fdct    = ff_jpeg_fdct_islow_8; // slow/accurate/default
2451             c->fdct248 = ff_fdct248_islow_8;
2452         }
2453     }
2454 #endif /* CONFIG_ENCODERS */
2455
2456     if (avctx->bits_per_raw_sample == 10) {
2457         c->idct_put              = ff_simple_idct_put_10;
2458         c->idct_add              = ff_simple_idct_add_10;
2459         c->idct                  = ff_simple_idct_10;
2460         c->idct_permutation_type = FF_NO_IDCT_PERM;
2461     } else {
2462         if (avctx->idct_algo == FF_IDCT_INT) {
2463             c->idct_put              = jref_idct_put;
2464             c->idct_add              = jref_idct_add;
2465             c->idct                  = ff_j_rev_dct;
2466             c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
2467         } else if (avctx->idct_algo == FF_IDCT_FAAN) {
2468             c->idct_put              = ff_faanidct_put;
2469             c->idct_add              = ff_faanidct_add;
2470             c->idct                  = ff_faanidct;
2471             c->idct_permutation_type = FF_NO_IDCT_PERM;
2472         } else { // accurate/default
2473             c->idct_put              = ff_simple_idct_put_8;
2474             c->idct_add              = ff_simple_idct_add_8;
2475             c->idct                  = ff_simple_idct_8;
2476             c->idct_permutation_type = FF_NO_IDCT_PERM;
2477         }
2478     }
2479
2480     c->diff_pixels = diff_pixels_c;
2481
2482     c->put_pixels_clamped        = put_pixels_clamped_c;
2483     c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
2484     c->add_pixels_clamped        = add_pixels_clamped_c;
2485
2486     c->sum_abs_dctelem = sum_abs_dctelem_c;
2487
2488     c->gmc1 = gmc1_c;
2489     c->gmc  = ff_gmc_c;
2490
2491     c->pix_sum   = pix_sum_c;
2492     c->pix_norm1 = pix_norm1_c;
2493
2494     c->fill_block_tab[0] = fill_block16_c;
2495     c->fill_block_tab[1] = fill_block8_c;
2496
2497     /* TODO [0] 16  [1] 8 */
2498     c->pix_abs[0][0] = pix_abs16_c;
2499     c->pix_abs[0][1] = pix_abs16_x2_c;
2500     c->pix_abs[0][2] = pix_abs16_y2_c;
2501     c->pix_abs[0][3] = pix_abs16_xy2_c;
2502     c->pix_abs[1][0] = pix_abs8_c;
2503     c->pix_abs[1][1] = pix_abs8_x2_c;
2504     c->pix_abs[1][2] = pix_abs8_y2_c;
2505     c->pix_abs[1][3] = pix_abs8_xy2_c;
2506
2507 #define dspfunc(PFX, IDX, NUM)                              \
2508     c->PFX ## _pixels_tab[IDX][0]  = PFX ## NUM ## _mc00_c; \
2509     c->PFX ## _pixels_tab[IDX][1]  = PFX ## NUM ## _mc10_c; \
2510     c->PFX ## _pixels_tab[IDX][2]  = PFX ## NUM ## _mc20_c; \
2511     c->PFX ## _pixels_tab[IDX][3]  = PFX ## NUM ## _mc30_c; \
2512     c->PFX ## _pixels_tab[IDX][4]  = PFX ## NUM ## _mc01_c; \
2513     c->PFX ## _pixels_tab[IDX][5]  = PFX ## NUM ## _mc11_c; \
2514     c->PFX ## _pixels_tab[IDX][6]  = PFX ## NUM ## _mc21_c; \
2515     c->PFX ## _pixels_tab[IDX][7]  = PFX ## NUM ## _mc31_c; \
2516     c->PFX ## _pixels_tab[IDX][8]  = PFX ## NUM ## _mc02_c; \
2517     c->PFX ## _pixels_tab[IDX][9]  = PFX ## NUM ## _mc12_c; \
2518     c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_c; \
2519     c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_c; \
2520     c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_c; \
2521     c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_c; \
2522     c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_c; \
2523     c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_c
2524
2525     dspfunc(put_qpel, 0, 16);
2526     dspfunc(put_qpel, 1, 8);
2527
2528     dspfunc(put_no_rnd_qpel, 0, 16);
2529     dspfunc(put_no_rnd_qpel, 1, 8);
2530
2531     dspfunc(avg_qpel, 0, 16);
2532     dspfunc(avg_qpel, 1, 8);
2533
2534 #undef dspfunc
2535
2536     c->put_mspel_pixels_tab[0] = ff_put_pixels8x8_c;
2537     c->put_mspel_pixels_tab[1] = put_mspel8_mc10_c;
2538     c->put_mspel_pixels_tab[2] = put_mspel8_mc20_c;
2539     c->put_mspel_pixels_tab[3] = put_mspel8_mc30_c;
2540     c->put_mspel_pixels_tab[4] = put_mspel8_mc02_c;
2541     c->put_mspel_pixels_tab[5] = put_mspel8_mc12_c;
2542     c->put_mspel_pixels_tab[6] = put_mspel8_mc22_c;
2543     c->put_mspel_pixels_tab[7] = put_mspel8_mc32_c;
2544
2545 #define SET_CMP_FUNC(name)                      \
2546     c->name[0] = name ## 16_c;                  \
2547     c->name[1] = name ## 8x8_c;
2548
2549     SET_CMP_FUNC(hadamard8_diff)
2550     c->hadamard8_diff[4] = hadamard8_intra16_c;
2551     c->hadamard8_diff[5] = hadamard8_intra8x8_c;
2552     SET_CMP_FUNC(dct_sad)
2553     SET_CMP_FUNC(dct_max)
2554 #if CONFIG_GPL
2555     SET_CMP_FUNC(dct264_sad)
2556 #endif
2557     c->sad[0] = pix_abs16_c;
2558     c->sad[1] = pix_abs8_c;
2559     c->sse[0] = sse16_c;
2560     c->sse[1] = sse8_c;
2561     c->sse[2] = sse4_c;
2562     SET_CMP_FUNC(quant_psnr)
2563     SET_CMP_FUNC(rd)
2564     SET_CMP_FUNC(bit)
2565     c->vsad[0] = vsad16_c;
2566     c->vsad[4] = vsad_intra16_c;
2567     c->vsad[5] = vsad_intra8_c;
2568     c->vsse[0] = vsse16_c;
2569     c->vsse[4] = vsse_intra16_c;
2570     c->vsse[5] = vsse_intra8_c;
2571     c->nsse[0] = nsse16_c;
2572     c->nsse[1] = nsse8_c;
2573
2574     c->ssd_int8_vs_int16 = ssd_int8_vs_int16_c;
2575
2576     c->add_bytes                      = add_bytes_c;
2577     c->add_hfyu_median_prediction     = add_hfyu_median_prediction_c;
2578     c->add_hfyu_left_prediction       = add_hfyu_left_prediction_c;
2579     c->add_hfyu_left_prediction_bgr32 = add_hfyu_left_prediction_bgr32_c;
2580
2581     c->diff_bytes                 = diff_bytes_c;
2582     c->sub_hfyu_median_prediction = sub_hfyu_median_prediction_c;
2583
2584     c->bswap_buf   = bswap_buf;
2585     c->bswap16_buf = bswap16_buf;
2586
2587     c->try_8x8basis = try_8x8basis_c;
2588     c->add_8x8basis = add_8x8basis_c;
2589
2590     c->scalarproduct_and_madd_int16 = scalarproduct_and_madd_int16_c;
2591
2592     c->scalarproduct_int16 = scalarproduct_int16_c;
2593     c->vector_clip_int32   = vector_clip_int32_c;
2594     c->vector_clipf        = vector_clipf_c;
2595
2596     c->shrink[0] = av_image_copy_plane;
2597     c->shrink[1] = ff_shrink22;
2598     c->shrink[2] = ff_shrink44;
2599     c->shrink[3] = ff_shrink88;
2600
2601     c->add_pixels8 = add_pixels8_c;
2602
2603 #undef FUNC
2604 #undef FUNCC
2605 #define FUNC(f,  depth) f ## _ ## depth
2606 #define FUNCC(f, depth) f ## _ ## depth ## _c
2607
2608     c->draw_edges = FUNCC(draw_edges, 8);
2609
2610     c->clear_block  = FUNCC(clear_block, 8);
2611     c->clear_blocks = FUNCC(clear_blocks, 8);
2612
2613 #define BIT_DEPTH_FUNCS(depth)                  \
2614     c->get_pixels = FUNCC(get_pixels, depth);
2615
2616     switch (avctx->bits_per_raw_sample) {
2617     case 9:
2618     case 10:
2619         BIT_DEPTH_FUNCS(16);
2620         break;
2621     default:
2622         BIT_DEPTH_FUNCS(8);
2623         break;
2624     }
2625
2626     if (ARCH_ARM)
2627         ff_dsputil_init_arm(c, avctx, high_bit_depth);
2628     if (ARCH_BFIN)
2629         ff_dsputil_init_bfin(c, avctx, high_bit_depth);
2630     if (ARCH_PPC)
2631         ff_dsputil_init_ppc(c, avctx, high_bit_depth);
2632     if (ARCH_X86)
2633         ff_dsputil_init_x86(c, avctx, high_bit_depth);
2634
2635     ff_init_scantable_permutation(c->idct_permutation,
2636                                   c->idct_permutation_type);
2637 }