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