]> git.sesse.net Git - ffmpeg/blob - libavcodec/dsputil.c
Merge commit '9e500efdbe0deeff1602500ebc229a0a6b6bb1a2'
[ffmpeg] / 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 FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 /**
26  * @file
27  * DSP utils
28  */
29
30 #include "libavutil/attributes.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/internal.h"
33 #include "avcodec.h"
34 #include "copy_block.h"
35 #include "dct.h"
36 #include "dsputil.h"
37 #include "simple_idct.h"
38 #include "faandct.h"
39 #include "faanidct.h"
40 #include "imgconvert.h"
41 #include "mathops.h"
42 #include "mpegvideo.h"
43 #include "config.h"
44
45 uint32_t ff_square_tab[512] = { 0, };
46
47 #define BIT_DEPTH 16
48 #include "dsputilenc_template.c"
49 #undef BIT_DEPTH
50
51 #define BIT_DEPTH 8
52 #include "dsputilenc_template.c"
53
54 /* Input permutation for the simple_idct_mmx */
55 static const uint8_t simple_mmx_permutation[64] = {
56     0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
57     0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
58     0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
59     0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
60     0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
61     0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
62     0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
63     0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
64 };
65
66 static const uint8_t idct_sse2_row_perm[8] = { 0, 4, 1, 5, 2, 6, 3, 7 };
67
68 av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st,
69                                const uint8_t *src_scantable)
70 {
71     int i, end;
72
73     st->scantable = src_scantable;
74
75     for (i = 0; i < 64; i++) {
76         int j = src_scantable[i];
77         st->permutated[i] = permutation[j];
78     }
79
80     end = -1;
81     for (i = 0; i < 64; i++) {
82         int j = st->permutated[i];
83         if (j > end)
84             end = j;
85         st->raster_end[i] = end;
86     }
87 }
88
89 av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
90                                            int idct_permutation_type)
91 {
92     int i;
93
94     switch (idct_permutation_type) {
95     case FF_NO_IDCT_PERM:
96         for (i = 0; i < 64; i++)
97             idct_permutation[i] = i;
98         break;
99     case FF_LIBMPEG2_IDCT_PERM:
100         for (i = 0; i < 64; i++)
101             idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
102         break;
103     case FF_SIMPLE_IDCT_PERM:
104         for (i = 0; i < 64; i++)
105             idct_permutation[i] = simple_mmx_permutation[i];
106         break;
107     case FF_TRANSPOSE_IDCT_PERM:
108         for (i = 0; i < 64; i++)
109             idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
110         break;
111     case FF_PARTTRANS_IDCT_PERM:
112         for (i = 0; i < 64; i++)
113             idct_permutation[i] = (i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3);
114         break;
115     case FF_SSE2_IDCT_PERM:
116         for (i = 0; i < 64; i++)
117             idct_permutation[i] = (i & 0x38) | idct_sse2_row_perm[i & 7];
118         break;
119     default:
120         av_log(NULL, AV_LOG_ERROR,
121                "Internal error, IDCT permutation not set\n");
122     }
123 }
124
125 static int pix_sum_c(uint8_t *pix, int line_size)
126 {
127     int s = 0, i, j;
128
129     for (i = 0; i < 16; i++) {
130         for (j = 0; j < 16; j += 8) {
131             s   += pix[0];
132             s   += pix[1];
133             s   += pix[2];
134             s   += pix[3];
135             s   += pix[4];
136             s   += pix[5];
137             s   += pix[6];
138             s   += pix[7];
139             pix += 8;
140         }
141         pix += line_size - 16;
142     }
143     return s;
144 }
145
146 static int pix_norm1_c(uint8_t *pix, int line_size)
147 {
148     int s = 0, i, j;
149     uint32_t *sq = ff_square_tab + 256;
150
151     for (i = 0; i < 16; i++) {
152         for (j = 0; j < 16; j += 8) {
153 #if 0
154             s += sq[pix[0]];
155             s += sq[pix[1]];
156             s += sq[pix[2]];
157             s += sq[pix[3]];
158             s += sq[pix[4]];
159             s += sq[pix[5]];
160             s += sq[pix[6]];
161             s += sq[pix[7]];
162 #else
163 #if HAVE_FAST_64BIT
164             register uint64_t x = *(uint64_t *) pix;
165             s += sq[x         & 0xff];
166             s += sq[(x >>  8) & 0xff];
167             s += sq[(x >> 16) & 0xff];
168             s += sq[(x >> 24) & 0xff];
169             s += sq[(x >> 32) & 0xff];
170             s += sq[(x >> 40) & 0xff];
171             s += sq[(x >> 48) & 0xff];
172             s += sq[(x >> 56) & 0xff];
173 #else
174             register uint32_t x = *(uint32_t *) pix;
175             s += sq[x         & 0xff];
176             s += sq[(x >>  8) & 0xff];
177             s += sq[(x >> 16) & 0xff];
178             s += sq[(x >> 24) & 0xff];
179             x  = *(uint32_t *) (pix + 4);
180             s += sq[x         & 0xff];
181             s += sq[(x >>  8) & 0xff];
182             s += sq[(x >> 16) & 0xff];
183             s += sq[(x >> 24) & 0xff];
184 #endif
185 #endif
186             pix += 8;
187         }
188         pix += line_size - 16;
189     }
190     return s;
191 }
192
193 static void bswap_buf(uint32_t *dst, const uint32_t *src, int w)
194 {
195     int i;
196
197     for (i = 0; i + 8 <= w; i += 8) {
198         dst[i + 0] = av_bswap32(src[i + 0]);
199         dst[i + 1] = av_bswap32(src[i + 1]);
200         dst[i + 2] = av_bswap32(src[i + 2]);
201         dst[i + 3] = av_bswap32(src[i + 3]);
202         dst[i + 4] = av_bswap32(src[i + 4]);
203         dst[i + 5] = av_bswap32(src[i + 5]);
204         dst[i + 6] = av_bswap32(src[i + 6]);
205         dst[i + 7] = av_bswap32(src[i + 7]);
206     }
207     for (; i < w; i++)
208         dst[i + 0] = av_bswap32(src[i + 0]);
209 }
210
211 static void bswap16_buf(uint16_t *dst, const uint16_t *src, int len)
212 {
213     while (len--)
214         *dst++ = av_bswap16(*src++);
215 }
216
217 static int sse4_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
218                   int line_size, int h)
219 {
220     int s = 0, i;
221     uint32_t *sq = ff_square_tab + 256;
222
223     for (i = 0; i < h; i++) {
224         s    += sq[pix1[0] - pix2[0]];
225         s    += sq[pix1[1] - pix2[1]];
226         s    += sq[pix1[2] - pix2[2]];
227         s    += sq[pix1[3] - pix2[3]];
228         pix1 += line_size;
229         pix2 += line_size;
230     }
231     return s;
232 }
233
234 static int sse8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
235                   int line_size, int h)
236 {
237     int s = 0, i;
238     uint32_t *sq = ff_square_tab + 256;
239
240     for (i = 0; i < h; i++) {
241         s    += sq[pix1[0] - pix2[0]];
242         s    += sq[pix1[1] - pix2[1]];
243         s    += sq[pix1[2] - pix2[2]];
244         s    += sq[pix1[3] - pix2[3]];
245         s    += sq[pix1[4] - pix2[4]];
246         s    += sq[pix1[5] - pix2[5]];
247         s    += sq[pix1[6] - pix2[6]];
248         s    += sq[pix1[7] - pix2[7]];
249         pix1 += line_size;
250         pix2 += line_size;
251     }
252     return s;
253 }
254
255 static int sse16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
256                    int line_size, int h)
257 {
258     int s = 0, i;
259     uint32_t *sq = ff_square_tab + 256;
260
261     for (i = 0; i < h; i++) {
262         s += sq[pix1[0]  - pix2[0]];
263         s += sq[pix1[1]  - pix2[1]];
264         s += sq[pix1[2]  - pix2[2]];
265         s += sq[pix1[3]  - pix2[3]];
266         s += sq[pix1[4]  - pix2[4]];
267         s += sq[pix1[5]  - pix2[5]];
268         s += sq[pix1[6]  - pix2[6]];
269         s += sq[pix1[7]  - pix2[7]];
270         s += sq[pix1[8]  - pix2[8]];
271         s += sq[pix1[9]  - pix2[9]];
272         s += sq[pix1[10] - pix2[10]];
273         s += sq[pix1[11] - pix2[11]];
274         s += sq[pix1[12] - pix2[12]];
275         s += sq[pix1[13] - pix2[13]];
276         s += sq[pix1[14] - pix2[14]];
277         s += sq[pix1[15] - pix2[15]];
278
279         pix1 += line_size;
280         pix2 += line_size;
281     }
282     return s;
283 }
284
285 static void diff_pixels_c(int16_t *av_restrict block, const uint8_t *s1,
286                           const uint8_t *s2, int stride)
287 {
288     int i;
289
290     /* read the pixels */
291     for (i = 0; i < 8; i++) {
292         block[0] = s1[0] - s2[0];
293         block[1] = s1[1] - s2[1];
294         block[2] = s1[2] - s2[2];
295         block[3] = s1[3] - s2[3];
296         block[4] = s1[4] - s2[4];
297         block[5] = s1[5] - s2[5];
298         block[6] = s1[6] - s2[6];
299         block[7] = s1[7] - s2[7];
300         s1      += stride;
301         s2      += stride;
302         block   += 8;
303     }
304 }
305
306 static void put_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
307                                  int line_size)
308 {
309     int i;
310
311     /* read the pixels */
312     for (i = 0; i < 8; i++) {
313         pixels[0] = av_clip_uint8(block[0]);
314         pixels[1] = av_clip_uint8(block[1]);
315         pixels[2] = av_clip_uint8(block[2]);
316         pixels[3] = av_clip_uint8(block[3]);
317         pixels[4] = av_clip_uint8(block[4]);
318         pixels[5] = av_clip_uint8(block[5]);
319         pixels[6] = av_clip_uint8(block[6]);
320         pixels[7] = av_clip_uint8(block[7]);
321
322         pixels += line_size;
323         block  += 8;
324     }
325 }
326
327 static void put_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
328                                  int line_size)
329 {
330     int i;
331
332     /* read the pixels */
333     for(i=0;i<4;i++) {
334         pixels[0] = av_clip_uint8(block[0]);
335         pixels[1] = av_clip_uint8(block[1]);
336         pixels[2] = av_clip_uint8(block[2]);
337         pixels[3] = av_clip_uint8(block[3]);
338
339         pixels += line_size;
340         block += 8;
341     }
342 }
343
344 static void put_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
345                                  int line_size)
346 {
347     int i;
348
349     /* read the pixels */
350     for(i=0;i<2;i++) {
351         pixels[0] = av_clip_uint8(block[0]);
352         pixels[1] = av_clip_uint8(block[1]);
353
354         pixels += line_size;
355         block += 8;
356     }
357 }
358
359 static void put_signed_pixels_clamped_c(const int16_t *block,
360                                         uint8_t *av_restrict pixels,
361                                         int line_size)
362 {
363     int i, j;
364
365     for (i = 0; i < 8; i++) {
366         for (j = 0; j < 8; j++) {
367             if (*block < -128)
368                 *pixels = 0;
369             else if (*block > 127)
370                 *pixels = 255;
371             else
372                 *pixels = (uint8_t) (*block + 128);
373             block++;
374             pixels++;
375         }
376         pixels += (line_size - 8);
377     }
378 }
379
380 static void add_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
381                                  int line_size)
382 {
383     int i;
384
385     /* read the pixels */
386     for (i = 0; i < 8; i++) {
387         pixels[0] = av_clip_uint8(pixels[0] + block[0]);
388         pixels[1] = av_clip_uint8(pixels[1] + block[1]);
389         pixels[2] = av_clip_uint8(pixels[2] + block[2]);
390         pixels[3] = av_clip_uint8(pixels[3] + block[3]);
391         pixels[4] = av_clip_uint8(pixels[4] + block[4]);
392         pixels[5] = av_clip_uint8(pixels[5] + block[5]);
393         pixels[6] = av_clip_uint8(pixels[6] + block[6]);
394         pixels[7] = av_clip_uint8(pixels[7] + block[7]);
395         pixels   += line_size;
396         block    += 8;
397     }
398 }
399
400 static void add_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
401                           int line_size)
402 {
403     int i;
404
405     /* read the pixels */
406     for(i=0;i<4;i++) {
407         pixels[0] = av_clip_uint8(pixels[0] + block[0]);
408         pixels[1] = av_clip_uint8(pixels[1] + block[1]);
409         pixels[2] = av_clip_uint8(pixels[2] + block[2]);
410         pixels[3] = av_clip_uint8(pixels[3] + block[3]);
411         pixels += line_size;
412         block += 8;
413     }
414 }
415
416 static void add_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
417                           int line_size)
418 {
419     int i;
420
421     /* read the pixels */
422     for(i=0;i<2;i++) {
423         pixels[0] = av_clip_uint8(pixels[0] + block[0]);
424         pixels[1] = av_clip_uint8(pixels[1] + block[1]);
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 #define avg2(a, b) ((a + b + 1) >> 1)
440 #define avg4(a, b, c, d) ((a + b + c + d + 2) >> 2)
441
442 static void gmc1_c(uint8_t *dst, uint8_t *src, int stride, int h,
443                    int x16, int y16, int rounder)
444 {
445     const int A = (16 - x16) * (16 - y16);
446     const int B = (x16)      * (16 - y16);
447     const int C = (16 - x16) * (y16);
448     const int D = (x16)      * (y16);
449     int i;
450
451     for (i = 0; i < h; i++) {
452         dst[0] = (A * src[0] + B * src[1] + C * src[stride + 0] + D * src[stride + 1] + rounder) >> 8;
453         dst[1] = (A * src[1] + B * src[2] + C * src[stride + 1] + D * src[stride + 2] + rounder) >> 8;
454         dst[2] = (A * src[2] + B * src[3] + C * src[stride + 2] + D * src[stride + 3] + rounder) >> 8;
455         dst[3] = (A * src[3] + B * src[4] + C * src[stride + 3] + D * src[stride + 4] + rounder) >> 8;
456         dst[4] = (A * src[4] + B * src[5] + C * src[stride + 4] + D * src[stride + 5] + rounder) >> 8;
457         dst[5] = (A * src[5] + B * src[6] + C * src[stride + 5] + D * src[stride + 6] + rounder) >> 8;
458         dst[6] = (A * src[6] + B * src[7] + C * src[stride + 6] + D * src[stride + 7] + rounder) >> 8;
459         dst[7] = (A * src[7] + B * src[8] + C * src[stride + 7] + D * src[stride + 8] + rounder) >> 8;
460         dst   += stride;
461         src   += stride;
462     }
463 }
464
465 void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
466               int dxx, int dxy, int dyx, int dyy, int shift, int r,
467               int width, int height)
468 {
469     int y, vx, vy;
470     const int s = 1 << shift;
471
472     width--;
473     height--;
474
475     for (y = 0; y < h; y++) {
476         int x;
477
478         vx = ox;
479         vy = oy;
480         for (x = 0; x < 8; x++) { // FIXME: optimize
481             int index;
482             int src_x  = vx >> 16;
483             int src_y  = vy >> 16;
484             int frac_x = src_x & (s - 1);
485             int frac_y = src_y & (s - 1);
486
487             src_x >>= shift;
488             src_y >>= shift;
489
490             if ((unsigned) src_x < width) {
491                 if ((unsigned) src_y < height) {
492                     index = src_x + src_y * stride;
493                     dst[y * stride + x] =
494                         ((src[index]                        * (s - frac_x) +
495                           src[index + 1]          * frac_x) * (s - frac_y) +
496                          (src[index + stride]               * (s - frac_x) +
497                           src[index + stride + 1] * frac_x) *      frac_y  +
498                          r) >> (shift * 2);
499                 } else {
500                     index = src_x + av_clip(src_y, 0, height) * stride;
501                     dst[y * stride + x] =
502                         ((src[index]               * (s - frac_x) +
503                           src[index + 1] * frac_x) *  s           +
504                          r) >> (shift * 2);
505                 }
506             } else {
507                 if ((unsigned) src_y < height) {
508                     index = av_clip(src_x, 0, width) + src_y * stride;
509                     dst[y * stride + x] =
510                         ((src[index]                    * (s - frac_y) +
511                           src[index + stride] * frac_y) *  s           +
512                          r) >> (shift * 2);
513                 } else {
514                     index = av_clip(src_x, 0, width) +
515                             av_clip(src_y, 0, height) * stride;
516                     dst[y * stride + x] = src[index];
517                 }
518             }
519
520             vx += dxx;
521             vy += dyx;
522         }
523         ox += dxy;
524         oy += dyy;
525     }
526 }
527
528 static inline int pix_abs16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
529                               int line_size, int h)
530 {
531     int s = 0, i;
532
533     for (i = 0; i < h; i++) {
534         s    += abs(pix1[0]  - pix2[0]);
535         s    += abs(pix1[1]  - pix2[1]);
536         s    += abs(pix1[2]  - pix2[2]);
537         s    += abs(pix1[3]  - pix2[3]);
538         s    += abs(pix1[4]  - pix2[4]);
539         s    += abs(pix1[5]  - pix2[5]);
540         s    += abs(pix1[6]  - pix2[6]);
541         s    += abs(pix1[7]  - pix2[7]);
542         s    += abs(pix1[8]  - pix2[8]);
543         s    += abs(pix1[9]  - pix2[9]);
544         s    += abs(pix1[10] - pix2[10]);
545         s    += abs(pix1[11] - pix2[11]);
546         s    += abs(pix1[12] - pix2[12]);
547         s    += abs(pix1[13] - pix2[13]);
548         s    += abs(pix1[14] - pix2[14]);
549         s    += abs(pix1[15] - pix2[15]);
550         pix1 += line_size;
551         pix2 += line_size;
552     }
553     return s;
554 }
555
556 static int pix_abs16_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
557                           int line_size, int h)
558 {
559     int s = 0, i;
560
561     for (i = 0; i < h; i++) {
562         s    += abs(pix1[0]  - avg2(pix2[0],  pix2[1]));
563         s    += abs(pix1[1]  - avg2(pix2[1],  pix2[2]));
564         s    += abs(pix1[2]  - avg2(pix2[2],  pix2[3]));
565         s    += abs(pix1[3]  - avg2(pix2[3],  pix2[4]));
566         s    += abs(pix1[4]  - avg2(pix2[4],  pix2[5]));
567         s    += abs(pix1[5]  - avg2(pix2[5],  pix2[6]));
568         s    += abs(pix1[6]  - avg2(pix2[6],  pix2[7]));
569         s    += abs(pix1[7]  - avg2(pix2[7],  pix2[8]));
570         s    += abs(pix1[8]  - avg2(pix2[8],  pix2[9]));
571         s    += abs(pix1[9]  - avg2(pix2[9],  pix2[10]));
572         s    += abs(pix1[10] - avg2(pix2[10], pix2[11]));
573         s    += abs(pix1[11] - avg2(pix2[11], pix2[12]));
574         s    += abs(pix1[12] - avg2(pix2[12], pix2[13]));
575         s    += abs(pix1[13] - avg2(pix2[13], pix2[14]));
576         s    += abs(pix1[14] - avg2(pix2[14], pix2[15]));
577         s    += abs(pix1[15] - avg2(pix2[15], pix2[16]));
578         pix1 += line_size;
579         pix2 += line_size;
580     }
581     return s;
582 }
583
584 static int pix_abs16_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
585                           int line_size, int h)
586 {
587     int s = 0, i;
588     uint8_t *pix3 = pix2 + line_size;
589
590     for (i = 0; i < h; i++) {
591         s    += abs(pix1[0]  - avg2(pix2[0],  pix3[0]));
592         s    += abs(pix1[1]  - avg2(pix2[1],  pix3[1]));
593         s    += abs(pix1[2]  - avg2(pix2[2],  pix3[2]));
594         s    += abs(pix1[3]  - avg2(pix2[3],  pix3[3]));
595         s    += abs(pix1[4]  - avg2(pix2[4],  pix3[4]));
596         s    += abs(pix1[5]  - avg2(pix2[5],  pix3[5]));
597         s    += abs(pix1[6]  - avg2(pix2[6],  pix3[6]));
598         s    += abs(pix1[7]  - avg2(pix2[7],  pix3[7]));
599         s    += abs(pix1[8]  - avg2(pix2[8],  pix3[8]));
600         s    += abs(pix1[9]  - avg2(pix2[9],  pix3[9]));
601         s    += abs(pix1[10] - avg2(pix2[10], pix3[10]));
602         s    += abs(pix1[11] - avg2(pix2[11], pix3[11]));
603         s    += abs(pix1[12] - avg2(pix2[12], pix3[12]));
604         s    += abs(pix1[13] - avg2(pix2[13], pix3[13]));
605         s    += abs(pix1[14] - avg2(pix2[14], pix3[14]));
606         s    += abs(pix1[15] - avg2(pix2[15], pix3[15]));
607         pix1 += line_size;
608         pix2 += line_size;
609         pix3 += line_size;
610     }
611     return s;
612 }
613
614 static int pix_abs16_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
615                            int line_size, int h)
616 {
617     int s = 0, i;
618     uint8_t *pix3 = pix2 + line_size;
619
620     for (i = 0; i < h; i++) {
621         s    += abs(pix1[0]  - avg4(pix2[0],  pix2[1],  pix3[0],  pix3[1]));
622         s    += abs(pix1[1]  - avg4(pix2[1],  pix2[2],  pix3[1],  pix3[2]));
623         s    += abs(pix1[2]  - avg4(pix2[2],  pix2[3],  pix3[2],  pix3[3]));
624         s    += abs(pix1[3]  - avg4(pix2[3],  pix2[4],  pix3[3],  pix3[4]));
625         s    += abs(pix1[4]  - avg4(pix2[4],  pix2[5],  pix3[4],  pix3[5]));
626         s    += abs(pix1[5]  - avg4(pix2[5],  pix2[6],  pix3[5],  pix3[6]));
627         s    += abs(pix1[6]  - avg4(pix2[6],  pix2[7],  pix3[6],  pix3[7]));
628         s    += abs(pix1[7]  - avg4(pix2[7],  pix2[8],  pix3[7],  pix3[8]));
629         s    += abs(pix1[8]  - avg4(pix2[8],  pix2[9],  pix3[8],  pix3[9]));
630         s    += abs(pix1[9]  - avg4(pix2[9],  pix2[10], pix3[9],  pix3[10]));
631         s    += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
632         s    += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
633         s    += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
634         s    += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
635         s    += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
636         s    += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
637         pix1 += line_size;
638         pix2 += line_size;
639         pix3 += line_size;
640     }
641     return s;
642 }
643
644 static inline int pix_abs8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
645                              int line_size, int h)
646 {
647     int s = 0, i;
648
649     for (i = 0; i < h; i++) {
650         s    += abs(pix1[0] - pix2[0]);
651         s    += abs(pix1[1] - pix2[1]);
652         s    += abs(pix1[2] - pix2[2]);
653         s    += abs(pix1[3] - pix2[3]);
654         s    += abs(pix1[4] - pix2[4]);
655         s    += abs(pix1[5] - pix2[5]);
656         s    += abs(pix1[6] - pix2[6]);
657         s    += abs(pix1[7] - pix2[7]);
658         pix1 += line_size;
659         pix2 += line_size;
660     }
661     return s;
662 }
663
664 static int pix_abs8_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
665                          int line_size, int h)
666 {
667     int s = 0, i;
668
669     for (i = 0; i < h; i++) {
670         s    += abs(pix1[0] - avg2(pix2[0], pix2[1]));
671         s    += abs(pix1[1] - avg2(pix2[1], pix2[2]));
672         s    += abs(pix1[2] - avg2(pix2[2], pix2[3]));
673         s    += abs(pix1[3] - avg2(pix2[3], pix2[4]));
674         s    += abs(pix1[4] - avg2(pix2[4], pix2[5]));
675         s    += abs(pix1[5] - avg2(pix2[5], pix2[6]));
676         s    += abs(pix1[6] - avg2(pix2[6], pix2[7]));
677         s    += abs(pix1[7] - avg2(pix2[7], pix2[8]));
678         pix1 += line_size;
679         pix2 += line_size;
680     }
681     return s;
682 }
683
684 static int pix_abs8_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
685                          int line_size, int h)
686 {
687     int s = 0, i;
688     uint8_t *pix3 = pix2 + line_size;
689
690     for (i = 0; i < h; i++) {
691         s    += abs(pix1[0] - avg2(pix2[0], pix3[0]));
692         s    += abs(pix1[1] - avg2(pix2[1], pix3[1]));
693         s    += abs(pix1[2] - avg2(pix2[2], pix3[2]));
694         s    += abs(pix1[3] - avg2(pix2[3], pix3[3]));
695         s    += abs(pix1[4] - avg2(pix2[4], pix3[4]));
696         s    += abs(pix1[5] - avg2(pix2[5], pix3[5]));
697         s    += abs(pix1[6] - avg2(pix2[6], pix3[6]));
698         s    += abs(pix1[7] - avg2(pix2[7], pix3[7]));
699         pix1 += line_size;
700         pix2 += line_size;
701         pix3 += line_size;
702     }
703     return s;
704 }
705
706 static int pix_abs8_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
707                           int line_size, int h)
708 {
709     int s = 0, i;
710     uint8_t *pix3 = pix2 + line_size;
711
712     for (i = 0; i < h; i++) {
713         s    += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
714         s    += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
715         s    += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
716         s    += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
717         s    += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
718         s    += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
719         s    += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
720         s    += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
721         pix1 += line_size;
722         pix2 += line_size;
723         pix3 += line_size;
724     }
725     return s;
726 }
727
728 static int nsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
729 {
730     int score1 = 0, score2 = 0, x, y;
731
732     for (y = 0; y < h; y++) {
733         for (x = 0; x < 16; x++)
734             score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
735         if (y + 1 < h) {
736             for (x = 0; x < 15; x++)
737                 score2 += FFABS(s1[x]     - s1[x + stride] -
738                                 s1[x + 1] + s1[x + stride + 1]) -
739                           FFABS(s2[x]     - s2[x + stride] -
740                                 s2[x + 1] + s2[x + stride + 1]);
741         }
742         s1 += stride;
743         s2 += stride;
744     }
745
746     if (c)
747         return score1 + FFABS(score2) * c->avctx->nsse_weight;
748     else
749         return score1 + FFABS(score2) * 8;
750 }
751
752 static int nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
753 {
754     int score1 = 0, score2 = 0, x, y;
755
756     for (y = 0; y < h; y++) {
757         for (x = 0; x < 8; x++)
758             score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
759         if (y + 1 < h) {
760             for (x = 0; x < 7; x++)
761                 score2 += FFABS(s1[x]     - s1[x + stride] -
762                                 s1[x + 1] + s1[x + stride + 1]) -
763                           FFABS(s2[x]     - s2[x + stride] -
764                                 s2[x + 1] + s2[x + stride + 1]);
765         }
766         s1 += stride;
767         s2 += stride;
768     }
769
770     if (c)
771         return score1 + FFABS(score2) * c->avctx->nsse_weight;
772     else
773         return score1 + FFABS(score2) * 8;
774 }
775
776 static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
777                           int16_t basis[64], int scale)
778 {
779     int i;
780     unsigned int sum = 0;
781
782     for (i = 0; i < 8 * 8; i++) {
783         int b = rem[i] + ((basis[i] * scale +
784                            (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
785                           (BASIS_SHIFT - RECON_SHIFT));
786         int w = weight[i];
787         b >>= RECON_SHIFT;
788         av_assert2(-512 < b && b < 512);
789
790         sum += (w * b) * (w * b) >> 4;
791     }
792     return sum >> 2;
793 }
794
795 static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
796 {
797     int i;
798
799     for (i = 0; i < 8 * 8; i++)
800         rem[i] += (basis[i] * scale +
801                    (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
802                   (BASIS_SHIFT - RECON_SHIFT);
803 }
804
805 static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
806                     int stride, int h)
807 {
808     return 0;
809 }
810
811 void ff_set_cmp(DSPContext *c, me_cmp_func *cmp, int type)
812 {
813     int i;
814
815     memset(cmp, 0, sizeof(void *) * 6);
816
817     for (i = 0; i < 6; i++) {
818         switch (type & 0xFF) {
819         case FF_CMP_SAD:
820             cmp[i] = c->sad[i];
821             break;
822         case FF_CMP_SATD:
823             cmp[i] = c->hadamard8_diff[i];
824             break;
825         case FF_CMP_SSE:
826             cmp[i] = c->sse[i];
827             break;
828         case FF_CMP_DCT:
829             cmp[i] = c->dct_sad[i];
830             break;
831         case FF_CMP_DCT264:
832             cmp[i] = c->dct264_sad[i];
833             break;
834         case FF_CMP_DCTMAX:
835             cmp[i] = c->dct_max[i];
836             break;
837         case FF_CMP_PSNR:
838             cmp[i] = c->quant_psnr[i];
839             break;
840         case FF_CMP_BIT:
841             cmp[i] = c->bit[i];
842             break;
843         case FF_CMP_RD:
844             cmp[i] = c->rd[i];
845             break;
846         case FF_CMP_VSAD:
847             cmp[i] = c->vsad[i];
848             break;
849         case FF_CMP_VSSE:
850             cmp[i] = c->vsse[i];
851             break;
852         case FF_CMP_ZERO:
853             cmp[i] = zero_cmp;
854             break;
855         case FF_CMP_NSSE:
856             cmp[i] = c->nsse[i];
857             break;
858 #if CONFIG_DWT
859         case FF_CMP_W53:
860             cmp[i]= c->w53[i];
861             break;
862         case FF_CMP_W97:
863             cmp[i]= c->w97[i];
864             break;
865 #endif
866         default:
867             av_log(NULL, AV_LOG_ERROR,
868                    "internal error in cmp function selection\n");
869         }
870     }
871 }
872
873 #define BUTTERFLY2(o1, o2, i1, i2)              \
874     o1 = (i1) + (i2);                           \
875     o2 = (i1) - (i2);
876
877 #define BUTTERFLY1(x, y)                        \
878     {                                           \
879         int a, b;                               \
880         a = x;                                  \
881         b = y;                                  \
882         x = a + b;                              \
883         y = a - b;                              \
884     }
885
886 #define BUTTERFLYA(x, y) (FFABS((x) + (y)) + FFABS((x) - (y)))
887
888 static int hadamard8_diff8x8_c(MpegEncContext *s, uint8_t *dst,
889                                uint8_t *src, int stride, int h)
890 {
891     int i, temp[64], sum = 0;
892
893     av_assert2(h == 8);
894
895     for (i = 0; i < 8; i++) {
896         // FIXME: try pointer walks
897         BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
898                    src[stride * i + 0] - dst[stride * i + 0],
899                    src[stride * i + 1] - dst[stride * i + 1]);
900         BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
901                    src[stride * i + 2] - dst[stride * i + 2],
902                    src[stride * i + 3] - dst[stride * i + 3]);
903         BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
904                    src[stride * i + 4] - dst[stride * i + 4],
905                    src[stride * i + 5] - dst[stride * i + 5]);
906         BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
907                    src[stride * i + 6] - dst[stride * i + 6],
908                    src[stride * i + 7] - dst[stride * i + 7]);
909
910         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
911         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
912         BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
913         BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
914
915         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
916         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
917         BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
918         BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
919     }
920
921     for (i = 0; i < 8; i++) {
922         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
923         BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
924         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
925         BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
926
927         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
928         BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
929         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
930         BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
931
932         sum += BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i]) +
933                BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i]) +
934                BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i]) +
935                BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
936     }
937     return sum;
938 }
939
940 static int hadamard8_intra8x8_c(MpegEncContext *s, uint8_t *src,
941                                 uint8_t *dummy, int stride, int h)
942 {
943     int i, temp[64], sum = 0;
944
945     av_assert2(h == 8);
946
947     for (i = 0; i < 8; i++) {
948         // FIXME: try pointer walks
949         BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
950                    src[stride * i + 0], src[stride * i + 1]);
951         BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
952                    src[stride * i + 2], src[stride * i + 3]);
953         BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
954                    src[stride * i + 4], src[stride * i + 5]);
955         BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
956                    src[stride * i + 6], src[stride * i + 7]);
957
958         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
959         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
960         BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
961         BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
962
963         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
964         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
965         BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
966         BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
967     }
968
969     for (i = 0; i < 8; i++) {
970         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
971         BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
972         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
973         BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
974
975         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
976         BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
977         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
978         BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
979
980         sum +=
981             BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i])
982             + BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i])
983             + BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i])
984             + BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
985     }
986
987     sum -= FFABS(temp[8 * 0] + temp[8 * 4]); // -mean
988
989     return sum;
990 }
991
992 static int dct_sad8x8_c(MpegEncContext *s, uint8_t *src1,
993                         uint8_t *src2, int stride, int h)
994 {
995     LOCAL_ALIGNED_16(int16_t, temp, [64]);
996
997     av_assert2(h == 8);
998
999     s->dsp.diff_pixels(temp, src1, src2, stride);
1000     s->dsp.fdct(temp);
1001     return s->dsp.sum_abs_dctelem(temp);
1002 }
1003
1004 #if CONFIG_GPL
1005 #define DCT8_1D                                         \
1006     {                                                   \
1007         const int s07 = SRC(0) + SRC(7);                \
1008         const int s16 = SRC(1) + SRC(6);                \
1009         const int s25 = SRC(2) + SRC(5);                \
1010         const int s34 = SRC(3) + SRC(4);                \
1011         const int a0  = s07 + s34;                      \
1012         const int a1  = s16 + s25;                      \
1013         const int a2  = s07 - s34;                      \
1014         const int a3  = s16 - s25;                      \
1015         const int d07 = SRC(0) - SRC(7);                \
1016         const int d16 = SRC(1) - SRC(6);                \
1017         const int d25 = SRC(2) - SRC(5);                \
1018         const int d34 = SRC(3) - SRC(4);                \
1019         const int a4  = d16 + d25 + (d07 + (d07 >> 1)); \
1020         const int a5  = d07 - d34 - (d25 + (d25 >> 1)); \
1021         const int a6  = d07 + d34 - (d16 + (d16 >> 1)); \
1022         const int a7  = d16 - d25 + (d34 + (d34 >> 1)); \
1023         DST(0, a0 + a1);                                \
1024         DST(1, a4 + (a7 >> 2));                         \
1025         DST(2, a2 + (a3 >> 1));                         \
1026         DST(3, a5 + (a6 >> 2));                         \
1027         DST(4, a0 - a1);                                \
1028         DST(5, a6 - (a5 >> 2));                         \
1029         DST(6, (a2 >> 1) - a3);                         \
1030         DST(7, (a4 >> 2) - a7);                         \
1031     }
1032
1033 static int dct264_sad8x8_c(MpegEncContext *s, uint8_t *src1,
1034                            uint8_t *src2, int stride, int h)
1035 {
1036     int16_t dct[8][8];
1037     int i, sum = 0;
1038
1039     s->dsp.diff_pixels(dct[0], src1, src2, stride);
1040
1041 #define SRC(x) dct[i][x]
1042 #define DST(x, v) dct[i][x] = v
1043     for (i = 0; i < 8; i++)
1044         DCT8_1D
1045 #undef SRC
1046 #undef DST
1047
1048 #define SRC(x) dct[x][i]
1049 #define DST(x, v) sum += FFABS(v)
1050         for (i = 0; i < 8; i++)
1051             DCT8_1D
1052 #undef SRC
1053 #undef DST
1054             return sum;
1055 }
1056 #endif
1057
1058 static int dct_max8x8_c(MpegEncContext *s, uint8_t *src1,
1059                         uint8_t *src2, int stride, int h)
1060 {
1061     LOCAL_ALIGNED_16(int16_t, temp, [64]);
1062     int sum = 0, i;
1063
1064     av_assert2(h == 8);
1065
1066     s->dsp.diff_pixels(temp, src1, src2, stride);
1067     s->dsp.fdct(temp);
1068
1069     for (i = 0; i < 64; i++)
1070         sum = FFMAX(sum, FFABS(temp[i]));
1071
1072     return sum;
1073 }
1074
1075 static int quant_psnr8x8_c(MpegEncContext *s, uint8_t *src1,
1076                            uint8_t *src2, int stride, int h)
1077 {
1078     LOCAL_ALIGNED_16(int16_t, temp, [64 * 2]);
1079     int16_t *const bak = temp + 64;
1080     int sum = 0, i;
1081
1082     av_assert2(h == 8);
1083     s->mb_intra = 0;
1084
1085     s->dsp.diff_pixels(temp, src1, src2, stride);
1086
1087     memcpy(bak, temp, 64 * sizeof(int16_t));
1088
1089     s->block_last_index[0 /* FIXME */] =
1090         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1091     s->dct_unquantize_inter(s, temp, 0, s->qscale);
1092     ff_simple_idct_8(temp); // FIXME
1093
1094     for (i = 0; i < 64; i++)
1095         sum += (temp[i] - bak[i]) * (temp[i] - bak[i]);
1096
1097     return sum;
1098 }
1099
1100 static int rd8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
1101                    int stride, int h)
1102 {
1103     const uint8_t *scantable = s->intra_scantable.permutated;
1104     LOCAL_ALIGNED_16(int16_t, temp, [64]);
1105     LOCAL_ALIGNED_16(uint8_t, lsrc1, [64]);
1106     LOCAL_ALIGNED_16(uint8_t, lsrc2, [64]);
1107     int i, last, run, bits, level, distortion, start_i;
1108     const int esc_length = s->ac_esc_length;
1109     uint8_t *length, *last_length;
1110
1111     av_assert2(h == 8);
1112
1113     copy_block8(lsrc1, src1, 8, stride, 8);
1114     copy_block8(lsrc2, src2, 8, stride, 8);
1115
1116     s->dsp.diff_pixels(temp, lsrc1, lsrc2, 8);
1117
1118     s->block_last_index[0 /* FIXME */] =
1119     last                               =
1120         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1121
1122     bits = 0;
1123
1124     if (s->mb_intra) {
1125         start_i     = 1;
1126         length      = s->intra_ac_vlc_length;
1127         last_length = s->intra_ac_vlc_last_length;
1128         bits       += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
1129     } else {
1130         start_i     = 0;
1131         length      = s->inter_ac_vlc_length;
1132         last_length = s->inter_ac_vlc_last_length;
1133     }
1134
1135     if (last >= start_i) {
1136         run = 0;
1137         for (i = start_i; i < last; i++) {
1138             int j = scantable[i];
1139             level = temp[j];
1140
1141             if (level) {
1142                 level += 64;
1143                 if ((level & (~127)) == 0)
1144                     bits += length[UNI_AC_ENC_INDEX(run, level)];
1145                 else
1146                     bits += esc_length;
1147                 run = 0;
1148             } else
1149                 run++;
1150         }
1151         i = scantable[last];
1152
1153         level = temp[i] + 64;
1154
1155         av_assert2(level - 64);
1156
1157         if ((level & (~127)) == 0) {
1158             bits += last_length[UNI_AC_ENC_INDEX(run, level)];
1159         } else
1160             bits += esc_length;
1161     }
1162
1163     if (last >= 0) {
1164         if (s->mb_intra)
1165             s->dct_unquantize_intra(s, temp, 0, s->qscale);
1166         else
1167             s->dct_unquantize_inter(s, temp, 0, s->qscale);
1168     }
1169
1170     s->dsp.idct_add(lsrc2, 8, temp);
1171
1172     distortion = s->dsp.sse[1](NULL, lsrc2, lsrc1, 8, 8);
1173
1174     return distortion + ((bits * s->qscale * s->qscale * 109 + 64) >> 7);
1175 }
1176
1177 static int bit8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
1178                     int stride, int h)
1179 {
1180     const uint8_t *scantable = s->intra_scantable.permutated;
1181     LOCAL_ALIGNED_16(int16_t, temp, [64]);
1182     int i, last, run, bits, level, start_i;
1183     const int esc_length = s->ac_esc_length;
1184     uint8_t *length, *last_length;
1185
1186     av_assert2(h == 8);
1187
1188     s->dsp.diff_pixels(temp, src1, src2, stride);
1189
1190     s->block_last_index[0 /* FIXME */] =
1191     last                               =
1192         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1193
1194     bits = 0;
1195
1196     if (s->mb_intra) {
1197         start_i     = 1;
1198         length      = s->intra_ac_vlc_length;
1199         last_length = s->intra_ac_vlc_last_length;
1200         bits       += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
1201     } else {
1202         start_i     = 0;
1203         length      = s->inter_ac_vlc_length;
1204         last_length = s->inter_ac_vlc_last_length;
1205     }
1206
1207     if (last >= start_i) {
1208         run = 0;
1209         for (i = start_i; i < last; i++) {
1210             int j = scantable[i];
1211             level = temp[j];
1212
1213             if (level) {
1214                 level += 64;
1215                 if ((level & (~127)) == 0)
1216                     bits += length[UNI_AC_ENC_INDEX(run, level)];
1217                 else
1218                     bits += esc_length;
1219                 run = 0;
1220             } else
1221                 run++;
1222         }
1223         i = scantable[last];
1224
1225         level = temp[i] + 64;
1226
1227         av_assert2(level - 64);
1228
1229         if ((level & (~127)) == 0)
1230             bits += last_length[UNI_AC_ENC_INDEX(run, level)];
1231         else
1232             bits += esc_length;
1233     }
1234
1235     return bits;
1236 }
1237
1238 #define VSAD_INTRA(size)                                                \
1239 static int vsad_intra ## size ## _c(MpegEncContext *c,                  \
1240                                     uint8_t *s, uint8_t *dummy,         \
1241                                     int stride, int h)                  \
1242 {                                                                       \
1243     int score = 0, x, y;                                                \
1244                                                                         \
1245     for (y = 1; y < h; y++) {                                           \
1246         for (x = 0; x < size; x += 4) {                                 \
1247             score += FFABS(s[x]     - s[x + stride])     +              \
1248                      FFABS(s[x + 1] - s[x + stride + 1]) +              \
1249                      FFABS(s[x + 2] - s[x + 2 + stride]) +              \
1250                      FFABS(s[x + 3] - s[x + 3 + stride]);               \
1251         }                                                               \
1252         s += stride;                                                    \
1253     }                                                                   \
1254                                                                         \
1255     return score;                                                       \
1256 }
1257 VSAD_INTRA(8)
1258 VSAD_INTRA(16)
1259
1260 #define VSAD(size)                                                             \
1261 static int vsad ## size ## _c(MpegEncContext *c,                               \
1262                               uint8_t *s1, uint8_t *s2,                        \
1263                               int stride, int h)                               \
1264 {                                                                              \
1265     int score = 0, x, y;                                                       \
1266                                                                                \
1267     for (y = 1; y < h; y++) {                                                  \
1268         for (x = 0; x < size; x++)                                             \
1269             score += FFABS(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);   \
1270         s1 += stride;                                                          \
1271         s2 += stride;                                                          \
1272     }                                                                          \
1273                                                                                \
1274     return score;                                                              \
1275 }
1276 VSAD(8)
1277 VSAD(16)
1278
1279 #define SQ(a) ((a) * (a))
1280 #define VSSE_INTRA(size)                                                \
1281 static int vsse_intra ## size ## _c(MpegEncContext *c,                  \
1282                                     uint8_t *s, uint8_t *dummy,         \
1283                                     int stride, int h)                  \
1284 {                                                                       \
1285     int score = 0, x, y;                                                \
1286                                                                         \
1287     for (y = 1; y < h; y++) {                                           \
1288         for (x = 0; x < size; x += 4) {                                 \
1289             score += SQ(s[x]     - s[x + stride]) +                     \
1290                      SQ(s[x + 1] - s[x + stride + 1]) +                 \
1291                      SQ(s[x + 2] - s[x + stride + 2]) +                 \
1292                      SQ(s[x + 3] - s[x + stride + 3]);                  \
1293         }                                                               \
1294         s += stride;                                                    \
1295     }                                                                   \
1296                                                                         \
1297     return score;                                                       \
1298 }
1299 VSSE_INTRA(8)
1300 VSSE_INTRA(16)
1301
1302 #define VSSE(size)                                                             \
1303 static int vsse ## size ## _c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,     \
1304                     int stride, int h)                                         \
1305 {                                                                              \
1306     int score = 0, x, y;                                                       \
1307                                                                                \
1308     for (y = 1; y < h; y++) {                                                  \
1309         for (x = 0; x < size; x++)                                             \
1310             score += SQ(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);      \
1311         s1 += stride;                                                          \
1312         s2 += stride;                                                          \
1313     }                                                                          \
1314                                                                                \
1315     return score;                                                              \
1316 }
1317 VSSE(8)
1318 VSSE(16)
1319
1320 #define WRAPPER8_16_SQ(name8, name16)                                   \
1321 static int name16(MpegEncContext *s, uint8_t *dst, uint8_t *src,        \
1322                   int stride, int h)                                    \
1323 {                                                                       \
1324     int score = 0;                                                      \
1325                                                                         \
1326     score += name8(s, dst, src, stride, 8);                             \
1327     score += name8(s, dst + 8, src + 8, stride, 8);                     \
1328     if (h == 16) {                                                      \
1329         dst   += 8 * stride;                                            \
1330         src   += 8 * stride;                                            \
1331         score += name8(s, dst, src, stride, 8);                         \
1332         score += name8(s, dst + 8, src + 8, stride, 8);                 \
1333     }                                                                   \
1334     return score;                                                       \
1335 }
1336
1337 WRAPPER8_16_SQ(hadamard8_diff8x8_c, hadamard8_diff16_c)
1338 WRAPPER8_16_SQ(hadamard8_intra8x8_c, hadamard8_intra16_c)
1339 WRAPPER8_16_SQ(dct_sad8x8_c, dct_sad16_c)
1340 #if CONFIG_GPL
1341 WRAPPER8_16_SQ(dct264_sad8x8_c, dct264_sad16_c)
1342 #endif
1343 WRAPPER8_16_SQ(dct_max8x8_c, dct_max16_c)
1344 WRAPPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c)
1345 WRAPPER8_16_SQ(rd8x8_c, rd16_c)
1346 WRAPPER8_16_SQ(bit8x8_c, bit16_c)
1347
1348 static inline uint32_t clipf_c_one(uint32_t a, uint32_t mini,
1349                                    uint32_t maxi, uint32_t maxisign)
1350 {
1351     if (a > mini)
1352         return mini;
1353     else if ((a ^ (1U << 31)) > maxisign)
1354         return maxi;
1355     else
1356         return a;
1357 }
1358
1359 static void vector_clipf_c_opposite_sign(float *dst, const float *src,
1360                                          float *min, float *max, int len)
1361 {
1362     int i;
1363     uint32_t mini        = *(uint32_t *) min;
1364     uint32_t maxi        = *(uint32_t *) max;
1365     uint32_t maxisign    = maxi ^ (1U << 31);
1366     uint32_t *dsti       = (uint32_t *) dst;
1367     const uint32_t *srci = (const uint32_t *) src;
1368
1369     for (i = 0; i < len; i += 8) {
1370         dsti[i + 0] = clipf_c_one(srci[i + 0], mini, maxi, maxisign);
1371         dsti[i + 1] = clipf_c_one(srci[i + 1], mini, maxi, maxisign);
1372         dsti[i + 2] = clipf_c_one(srci[i + 2], mini, maxi, maxisign);
1373         dsti[i + 3] = clipf_c_one(srci[i + 3], mini, maxi, maxisign);
1374         dsti[i + 4] = clipf_c_one(srci[i + 4], mini, maxi, maxisign);
1375         dsti[i + 5] = clipf_c_one(srci[i + 5], mini, maxi, maxisign);
1376         dsti[i + 6] = clipf_c_one(srci[i + 6], mini, maxi, maxisign);
1377         dsti[i + 7] = clipf_c_one(srci[i + 7], mini, maxi, maxisign);
1378     }
1379 }
1380
1381 static void vector_clipf_c(float *dst, const float *src,
1382                            float min, float max, int len)
1383 {
1384     int i;
1385
1386     if (min < 0 && max > 0) {
1387         vector_clipf_c_opposite_sign(dst, src, &min, &max, len);
1388     } else {
1389         for (i = 0; i < len; i += 8) {
1390             dst[i]     = av_clipf(src[i], min, max);
1391             dst[i + 1] = av_clipf(src[i + 1], min, max);
1392             dst[i + 2] = av_clipf(src[i + 2], min, max);
1393             dst[i + 3] = av_clipf(src[i + 3], min, max);
1394             dst[i + 4] = av_clipf(src[i + 4], min, max);
1395             dst[i + 5] = av_clipf(src[i + 5], min, max);
1396             dst[i + 6] = av_clipf(src[i + 6], min, max);
1397             dst[i + 7] = av_clipf(src[i + 7], min, max);
1398         }
1399     }
1400 }
1401
1402 static int32_t scalarproduct_int16_c(const int16_t *v1, const int16_t *v2,
1403                                      int order)
1404 {
1405     int res = 0;
1406
1407     while (order--)
1408         res += *v1++ **v2++;
1409
1410     return res;
1411 }
1412
1413 static void vector_clip_int32_c(int32_t *dst, const int32_t *src, int32_t min,
1414                                 int32_t max, unsigned int len)
1415 {
1416     do {
1417         *dst++ = av_clip(*src++, min, max);
1418         *dst++ = av_clip(*src++, min, max);
1419         *dst++ = av_clip(*src++, min, max);
1420         *dst++ = av_clip(*src++, min, max);
1421         *dst++ = av_clip(*src++, min, max);
1422         *dst++ = av_clip(*src++, min, max);
1423         *dst++ = av_clip(*src++, min, max);
1424         *dst++ = av_clip(*src++, min, max);
1425         len   -= 8;
1426     } while (len > 0);
1427 }
1428
1429 static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
1430 {
1431     ff_j_rev_dct(block);
1432     put_pixels_clamped_c(block, dest, line_size);
1433 }
1434
1435 static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
1436 {
1437     ff_j_rev_dct(block);
1438     add_pixels_clamped_c(block, dest, line_size);
1439 }
1440
1441 static void ff_jref_idct4_put(uint8_t *dest, int line_size, int16_t *block)
1442 {
1443     ff_j_rev_dct4 (block);
1444     put_pixels_clamped4_c(block, dest, line_size);
1445 }
1446 static void ff_jref_idct4_add(uint8_t *dest, int line_size, int16_t *block)
1447 {
1448     ff_j_rev_dct4 (block);
1449     add_pixels_clamped4_c(block, dest, line_size);
1450 }
1451
1452 static void ff_jref_idct2_put(uint8_t *dest, int line_size, int16_t *block)
1453 {
1454     ff_j_rev_dct2 (block);
1455     put_pixels_clamped2_c(block, dest, line_size);
1456 }
1457 static void ff_jref_idct2_add(uint8_t *dest, int line_size, int16_t *block)
1458 {
1459     ff_j_rev_dct2 (block);
1460     add_pixels_clamped2_c(block, dest, line_size);
1461 }
1462
1463 static void ff_jref_idct1_put(uint8_t *dest, int line_size, int16_t *block)
1464 {
1465     dest[0] = av_clip_uint8((block[0] + 4)>>3);
1466 }
1467 static void ff_jref_idct1_add(uint8_t *dest, int line_size, int16_t *block)
1468 {
1469     dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
1470 }
1471
1472 /* draw the edges of width 'w' of an image of size width, height */
1473 // FIXME: Check that this is OK for MPEG-4 interlaced.
1474 static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
1475                            int w, int h, int sides)
1476 {
1477     uint8_t *ptr = buf, *last_line;
1478     int i;
1479
1480     /* left and right */
1481     for (i = 0; i < height; i++) {
1482         memset(ptr - w, ptr[0], w);
1483         memset(ptr + width, ptr[width - 1], w);
1484         ptr += wrap;
1485     }
1486
1487     /* top and bottom + corners */
1488     buf -= w;
1489     last_line = buf + (height - 1) * wrap;
1490     if (sides & EDGE_TOP)
1491         for (i = 0; i < h; i++)
1492             // top
1493             memcpy(buf - (i + 1) * wrap, buf, width + w + w);
1494     if (sides & EDGE_BOTTOM)
1495         for (i = 0; i < h; i++)
1496             // bottom
1497             memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
1498 }
1499
1500 /* init static data */
1501 av_cold void ff_dsputil_static_init(void)
1502 {
1503     int i;
1504
1505     for (i = 0; i < 512; i++)
1506         ff_square_tab[i] = (i - 256) * (i - 256);
1507 }
1508
1509 int ff_check_alignment(void)
1510 {
1511     static int did_fail = 0;
1512     LOCAL_ALIGNED_16(int, aligned, [4]);
1513
1514     if ((intptr_t)aligned & 15) {
1515         if (!did_fail) {
1516 #if HAVE_MMX || HAVE_ALTIVEC
1517             av_log(NULL, AV_LOG_ERROR,
1518                 "Compiler did not align stack variables. Libavcodec has been miscompiled\n"
1519                 "and may be very slow or crash. This is not a bug in libavcodec,\n"
1520                 "but in the compiler. You may try recompiling using gcc >= 4.2.\n"
1521                 "Do not report crashes to FFmpeg developers.\n");
1522 #endif
1523             did_fail=1;
1524         }
1525         return -1;
1526     }
1527     return 0;
1528 }
1529
1530 av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
1531 {
1532     const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
1533
1534     ff_check_alignment();
1535
1536 #if CONFIG_ENCODERS
1537     if (avctx->bits_per_raw_sample == 10) {
1538         c->fdct    = ff_jpeg_fdct_islow_10;
1539         c->fdct248 = ff_fdct248_islow_10;
1540     } else {
1541         if (avctx->dct_algo == FF_DCT_FASTINT) {
1542             c->fdct    = ff_fdct_ifast;
1543             c->fdct248 = ff_fdct_ifast248;
1544         } else if (avctx->dct_algo == FF_DCT_FAAN) {
1545             c->fdct    = ff_faandct;
1546             c->fdct248 = ff_faandct248;
1547         } else {
1548             c->fdct    = ff_jpeg_fdct_islow_8; // slow/accurate/default
1549             c->fdct248 = ff_fdct248_islow_8;
1550         }
1551     }
1552 #endif /* CONFIG_ENCODERS */
1553
1554     if (avctx->lowres==1) {
1555         c->idct_put              = ff_jref_idct4_put;
1556         c->idct_add              = ff_jref_idct4_add;
1557         c->idct                  = ff_j_rev_dct4;
1558         c->idct_permutation_type = FF_NO_IDCT_PERM;
1559     } else if (avctx->lowres==2) {
1560         c->idct_put              =  ff_jref_idct2_put;
1561         c->idct_add              =  ff_jref_idct2_add;
1562         c->idct                  =  ff_j_rev_dct2;
1563         c->idct_permutation_type = FF_NO_IDCT_PERM;
1564     } else if (avctx->lowres==3) {
1565         c->idct_put              =  ff_jref_idct1_put;
1566         c->idct_add              =  ff_jref_idct1_add;
1567         c->idct                  =  ff_j_rev_dct1;
1568         c->idct_permutation_type = FF_NO_IDCT_PERM;
1569     } else {
1570         if (avctx->bits_per_raw_sample == 10) {
1571             c->idct_put              = ff_simple_idct_put_10;
1572             c->idct_add              = ff_simple_idct_add_10;
1573             c->idct                  = ff_simple_idct_10;
1574             c->idct_permutation_type = FF_NO_IDCT_PERM;
1575         } else if (avctx->bits_per_raw_sample == 12) {
1576             c->idct_put              = ff_simple_idct_put_12;
1577             c->idct_add              = ff_simple_idct_add_12;
1578             c->idct                  = ff_simple_idct_12;
1579             c->idct_permutation_type = FF_NO_IDCT_PERM;
1580         } else {
1581         if (avctx->idct_algo == FF_IDCT_INT) {
1582             c->idct_put              = jref_idct_put;
1583             c->idct_add              = jref_idct_add;
1584             c->idct                  = ff_j_rev_dct;
1585             c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
1586         } else if (avctx->idct_algo == FF_IDCT_FAAN) {
1587             c->idct_put              = ff_faanidct_put;
1588             c->idct_add              = ff_faanidct_add;
1589             c->idct                  = ff_faanidct;
1590             c->idct_permutation_type = FF_NO_IDCT_PERM;
1591         } else { // accurate/default
1592             c->idct_put              = ff_simple_idct_put_8;
1593             c->idct_add              = ff_simple_idct_add_8;
1594             c->idct                  = ff_simple_idct_8;
1595             c->idct_permutation_type = FF_NO_IDCT_PERM;
1596         }
1597         }
1598     }
1599
1600     c->diff_pixels = diff_pixels_c;
1601
1602     c->put_pixels_clamped        = put_pixels_clamped_c;
1603     c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
1604     c->add_pixels_clamped        = add_pixels_clamped_c;
1605
1606     c->sum_abs_dctelem = sum_abs_dctelem_c;
1607
1608     c->gmc1 = gmc1_c;
1609     c->gmc  = ff_gmc_c;
1610
1611     c->pix_sum   = pix_sum_c;
1612     c->pix_norm1 = pix_norm1_c;
1613
1614     /* TODO [0] 16  [1] 8 */
1615     c->pix_abs[0][0] = pix_abs16_c;
1616     c->pix_abs[0][1] = pix_abs16_x2_c;
1617     c->pix_abs[0][2] = pix_abs16_y2_c;
1618     c->pix_abs[0][3] = pix_abs16_xy2_c;
1619     c->pix_abs[1][0] = pix_abs8_c;
1620     c->pix_abs[1][1] = pix_abs8_x2_c;
1621     c->pix_abs[1][2] = pix_abs8_y2_c;
1622     c->pix_abs[1][3] = pix_abs8_xy2_c;
1623
1624 #define SET_CMP_FUNC(name)                      \
1625     c->name[0] = name ## 16_c;                  \
1626     c->name[1] = name ## 8x8_c;
1627
1628     SET_CMP_FUNC(hadamard8_diff)
1629     c->hadamard8_diff[4] = hadamard8_intra16_c;
1630     c->hadamard8_diff[5] = hadamard8_intra8x8_c;
1631     SET_CMP_FUNC(dct_sad)
1632     SET_CMP_FUNC(dct_max)
1633 #if CONFIG_GPL
1634     SET_CMP_FUNC(dct264_sad)
1635 #endif
1636     c->sad[0] = pix_abs16_c;
1637     c->sad[1] = pix_abs8_c;
1638     c->sse[0] = sse16_c;
1639     c->sse[1] = sse8_c;
1640     c->sse[2] = sse4_c;
1641     SET_CMP_FUNC(quant_psnr)
1642     SET_CMP_FUNC(rd)
1643     SET_CMP_FUNC(bit)
1644     c->vsad[0] = vsad16_c;
1645     c->vsad[1] = vsad8_c;
1646     c->vsad[4] = vsad_intra16_c;
1647     c->vsad[5] = vsad_intra8_c;
1648     c->vsse[0] = vsse16_c;
1649     c->vsse[1] = vsse8_c;
1650     c->vsse[4] = vsse_intra16_c;
1651     c->vsse[5] = vsse_intra8_c;
1652     c->nsse[0] = nsse16_c;
1653     c->nsse[1] = nsse8_c;
1654 #if CONFIG_SNOW_DECODER || CONFIG_SNOW_ENCODER
1655     ff_dsputil_init_dwt(c);
1656 #endif
1657
1658     c->bswap_buf   = bswap_buf;
1659     c->bswap16_buf = bswap16_buf;
1660
1661     c->try_8x8basis = try_8x8basis_c;
1662     c->add_8x8basis = add_8x8basis_c;
1663
1664     c->scalarproduct_int16 = scalarproduct_int16_c;
1665     c->vector_clip_int32   = vector_clip_int32_c;
1666     c->vector_clipf        = vector_clipf_c;
1667
1668     c->shrink[0] = av_image_copy_plane;
1669     c->shrink[1] = ff_shrink22;
1670     c->shrink[2] = ff_shrink44;
1671     c->shrink[3] = ff_shrink88;
1672
1673     c->draw_edges = draw_edges_8_c;
1674
1675     switch (avctx->bits_per_raw_sample) {
1676     case 9:
1677     case 10:
1678     case 12:
1679     case 14:
1680         c->get_pixels = get_pixels_16_c;
1681         break;
1682     default:
1683         if (avctx->bits_per_raw_sample<=8 || avctx->codec_type != AVMEDIA_TYPE_VIDEO) {
1684             c->get_pixels = get_pixels_8_c;
1685         }
1686         break;
1687     }
1688
1689
1690     if (ARCH_ALPHA)
1691         ff_dsputil_init_alpha(c, avctx);
1692     if (ARCH_ARM)
1693         ff_dsputil_init_arm(c, avctx, high_bit_depth);
1694     if (ARCH_PPC)
1695         ff_dsputil_init_ppc(c, avctx, high_bit_depth);
1696     if (ARCH_X86)
1697         ff_dsputil_init_x86(c, avctx, high_bit_depth);
1698
1699     ff_init_scantable_permutation(c->idct_permutation,
1700                                   c->idct_permutation_type);
1701 }
1702
1703 av_cold void dsputil_init(DSPContext* c, AVCodecContext *avctx)
1704 {
1705     ff_dsputil_init(c, avctx);
1706 }
1707
1708 av_cold void avpriv_dsputil_init(DSPContext *c, AVCodecContext *avctx)
1709 {
1710     ff_dsputil_init(c, avctx);
1711 }