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