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