]> git.sesse.net Git - ffmpeg/blob - libavcodec/dsputil.c
Merge commit '83aa4fc3feec7389ac781fece1e994f2dfd7ebdb'
[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 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 index;
502             int src_x  = vx >> 16;
503             int src_y  = vy >> 16;
504             int frac_x = src_x & (s - 1);
505             int frac_y = src_y & (s - 1);
506
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 int pix_abs16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
549                               int line_size, int h)
550 {
551     int s = 0, i;
552
553     for (i = 0; i < h; i++) {
554         s    += abs(pix1[0]  - pix2[0]);
555         s    += abs(pix1[1]  - pix2[1]);
556         s    += abs(pix1[2]  - pix2[2]);
557         s    += abs(pix1[3]  - pix2[3]);
558         s    += abs(pix1[4]  - pix2[4]);
559         s    += abs(pix1[5]  - pix2[5]);
560         s    += abs(pix1[6]  - pix2[6]);
561         s    += abs(pix1[7]  - pix2[7]);
562         s    += abs(pix1[8]  - pix2[8]);
563         s    += abs(pix1[9]  - pix2[9]);
564         s    += abs(pix1[10] - pix2[10]);
565         s    += abs(pix1[11] - pix2[11]);
566         s    += abs(pix1[12] - pix2[12]);
567         s    += abs(pix1[13] - pix2[13]);
568         s    += abs(pix1[14] - pix2[14]);
569         s    += abs(pix1[15] - pix2[15]);
570         pix1 += line_size;
571         pix2 += line_size;
572     }
573     return s;
574 }
575
576 static int pix_abs16_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
577                           int line_size, int h)
578 {
579     int s = 0, i;
580
581     for (i = 0; i < h; i++) {
582         s    += abs(pix1[0]  - avg2(pix2[0],  pix2[1]));
583         s    += abs(pix1[1]  - avg2(pix2[1],  pix2[2]));
584         s    += abs(pix1[2]  - avg2(pix2[2],  pix2[3]));
585         s    += abs(pix1[3]  - avg2(pix2[3],  pix2[4]));
586         s    += abs(pix1[4]  - avg2(pix2[4],  pix2[5]));
587         s    += abs(pix1[5]  - avg2(pix2[5],  pix2[6]));
588         s    += abs(pix1[6]  - avg2(pix2[6],  pix2[7]));
589         s    += abs(pix1[7]  - avg2(pix2[7],  pix2[8]));
590         s    += abs(pix1[8]  - avg2(pix2[8],  pix2[9]));
591         s    += abs(pix1[9]  - avg2(pix2[9],  pix2[10]));
592         s    += abs(pix1[10] - avg2(pix2[10], pix2[11]));
593         s    += abs(pix1[11] - avg2(pix2[11], pix2[12]));
594         s    += abs(pix1[12] - avg2(pix2[12], pix2[13]));
595         s    += abs(pix1[13] - avg2(pix2[13], pix2[14]));
596         s    += abs(pix1[14] - avg2(pix2[14], pix2[15]));
597         s    += abs(pix1[15] - avg2(pix2[15], pix2[16]));
598         pix1 += line_size;
599         pix2 += line_size;
600     }
601     return s;
602 }
603
604 static int pix_abs16_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
605                           int line_size, int h)
606 {
607     int s = 0, i;
608     uint8_t *pix3 = pix2 + line_size;
609
610     for (i = 0; i < h; i++) {
611         s    += abs(pix1[0]  - avg2(pix2[0],  pix3[0]));
612         s    += abs(pix1[1]  - avg2(pix2[1],  pix3[1]));
613         s    += abs(pix1[2]  - avg2(pix2[2],  pix3[2]));
614         s    += abs(pix1[3]  - avg2(pix2[3],  pix3[3]));
615         s    += abs(pix1[4]  - avg2(pix2[4],  pix3[4]));
616         s    += abs(pix1[5]  - avg2(pix2[5],  pix3[5]));
617         s    += abs(pix1[6]  - avg2(pix2[6],  pix3[6]));
618         s    += abs(pix1[7]  - avg2(pix2[7],  pix3[7]));
619         s    += abs(pix1[8]  - avg2(pix2[8],  pix3[8]));
620         s    += abs(pix1[9]  - avg2(pix2[9],  pix3[9]));
621         s    += abs(pix1[10] - avg2(pix2[10], pix3[10]));
622         s    += abs(pix1[11] - avg2(pix2[11], pix3[11]));
623         s    += abs(pix1[12] - avg2(pix2[12], pix3[12]));
624         s    += abs(pix1[13] - avg2(pix2[13], pix3[13]));
625         s    += abs(pix1[14] - avg2(pix2[14], pix3[14]));
626         s    += abs(pix1[15] - avg2(pix2[15], pix3[15]));
627         pix1 += line_size;
628         pix2 += line_size;
629         pix3 += line_size;
630     }
631     return s;
632 }
633
634 static int pix_abs16_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
635                            int line_size, int h)
636 {
637     int s = 0, i;
638     uint8_t *pix3 = pix2 + line_size;
639
640     for (i = 0; i < h; i++) {
641         s    += abs(pix1[0]  - avg4(pix2[0],  pix2[1],  pix3[0],  pix3[1]));
642         s    += abs(pix1[1]  - avg4(pix2[1],  pix2[2],  pix3[1],  pix3[2]));
643         s    += abs(pix1[2]  - avg4(pix2[2],  pix2[3],  pix3[2],  pix3[3]));
644         s    += abs(pix1[3]  - avg4(pix2[3],  pix2[4],  pix3[3],  pix3[4]));
645         s    += abs(pix1[4]  - avg4(pix2[4],  pix2[5],  pix3[4],  pix3[5]));
646         s    += abs(pix1[5]  - avg4(pix2[5],  pix2[6],  pix3[5],  pix3[6]));
647         s    += abs(pix1[6]  - avg4(pix2[6],  pix2[7],  pix3[6],  pix3[7]));
648         s    += abs(pix1[7]  - avg4(pix2[7],  pix2[8],  pix3[7],  pix3[8]));
649         s    += abs(pix1[8]  - avg4(pix2[8],  pix2[9],  pix3[8],  pix3[9]));
650         s    += abs(pix1[9]  - avg4(pix2[9],  pix2[10], pix3[9],  pix3[10]));
651         s    += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
652         s    += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
653         s    += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
654         s    += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
655         s    += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
656         s    += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
657         pix1 += line_size;
658         pix2 += line_size;
659         pix3 += line_size;
660     }
661     return s;
662 }
663
664 static inline int pix_abs8_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] - pix2[0]);
671         s    += abs(pix1[1] - pix2[1]);
672         s    += abs(pix1[2] - pix2[2]);
673         s    += abs(pix1[3] - pix2[3]);
674         s    += abs(pix1[4] - pix2[4]);
675         s    += abs(pix1[5] - pix2[5]);
676         s    += abs(pix1[6] - pix2[6]);
677         s    += abs(pix1[7] - pix2[7]);
678         pix1 += line_size;
679         pix2 += line_size;
680     }
681     return s;
682 }
683
684 static int pix_abs8_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
685                          int line_size, int h)
686 {
687     int s = 0, i;
688
689     for (i = 0; i < h; i++) {
690         s    += abs(pix1[0] - avg2(pix2[0], pix2[1]));
691         s    += abs(pix1[1] - avg2(pix2[1], pix2[2]));
692         s    += abs(pix1[2] - avg2(pix2[2], pix2[3]));
693         s    += abs(pix1[3] - avg2(pix2[3], pix2[4]));
694         s    += abs(pix1[4] - avg2(pix2[4], pix2[5]));
695         s    += abs(pix1[5] - avg2(pix2[5], pix2[6]));
696         s    += abs(pix1[6] - avg2(pix2[6], pix2[7]));
697         s    += abs(pix1[7] - avg2(pix2[7], pix2[8]));
698         pix1 += line_size;
699         pix2 += line_size;
700     }
701     return s;
702 }
703
704 static int pix_abs8_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
705                          int line_size, int h)
706 {
707     int s = 0, i;
708     uint8_t *pix3 = pix2 + line_size;
709
710     for (i = 0; i < h; i++) {
711         s    += abs(pix1[0] - avg2(pix2[0], pix3[0]));
712         s    += abs(pix1[1] - avg2(pix2[1], pix3[1]));
713         s    += abs(pix1[2] - avg2(pix2[2], pix3[2]));
714         s    += abs(pix1[3] - avg2(pix2[3], pix3[3]));
715         s    += abs(pix1[4] - avg2(pix2[4], pix3[4]));
716         s    += abs(pix1[5] - avg2(pix2[5], pix3[5]));
717         s    += abs(pix1[6] - avg2(pix2[6], pix3[6]));
718         s    += abs(pix1[7] - avg2(pix2[7], pix3[7]));
719         pix1 += line_size;
720         pix2 += line_size;
721         pix3 += line_size;
722     }
723     return s;
724 }
725
726 static int pix_abs8_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
727                           int line_size, int h)
728 {
729     int s = 0, i;
730     uint8_t *pix3 = pix2 + line_size;
731
732     for (i = 0; i < h; i++) {
733         s    += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
734         s    += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
735         s    += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
736         s    += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
737         s    += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
738         s    += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
739         s    += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
740         s    += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
741         pix1 += line_size;
742         pix2 += line_size;
743         pix3 += line_size;
744     }
745     return s;
746 }
747
748 static int nsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
749 {
750     int score1 = 0, score2 = 0, x, y;
751
752     for (y = 0; y < h; y++) {
753         for (x = 0; x < 16; x++)
754             score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
755         if (y + 1 < h) {
756             for (x = 0; x < 15; x++)
757                 score2 += FFABS(s1[x]     - s1[x + stride] -
758                                 s1[x + 1] + s1[x + stride + 1]) -
759                           FFABS(s2[x]     - s2[x + stride] -
760                                 s2[x + 1] + s2[x + stride + 1]);
761         }
762         s1 += stride;
763         s2 += stride;
764     }
765
766     if (c)
767         return score1 + FFABS(score2) * c->avctx->nsse_weight;
768     else
769         return score1 + FFABS(score2) * 8;
770 }
771
772 static int nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
773 {
774     int score1 = 0, score2 = 0, x, y;
775
776     for (y = 0; y < h; y++) {
777         for (x = 0; x < 8; x++)
778             score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
779         if (y + 1 < h) {
780             for (x = 0; x < 7; x++)
781                 score2 += FFABS(s1[x]     - s1[x + stride] -
782                                 s1[x + 1] + s1[x + stride + 1]) -
783                           FFABS(s2[x]     - s2[x + stride] -
784                                 s2[x + 1] + s2[x + stride + 1]);
785         }
786         s1 += stride;
787         s2 += stride;
788     }
789
790     if (c)
791         return score1 + FFABS(score2) * c->avctx->nsse_weight;
792     else
793         return score1 + FFABS(score2) * 8;
794 }
795
796 static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
797                           int16_t basis[64], int scale)
798 {
799     int i;
800     unsigned int sum = 0;
801
802     for (i = 0; i < 8 * 8; i++) {
803         int b = rem[i] + ((basis[i] * scale +
804                            (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
805                           (BASIS_SHIFT - RECON_SHIFT));
806         int w = weight[i];
807         b >>= RECON_SHIFT;
808         av_assert2(-512 < b && b < 512);
809
810         sum += (w * b) * (w * b) >> 4;
811     }
812     return sum >> 2;
813 }
814
815 static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
816 {
817     int i;
818
819     for (i = 0; i < 8 * 8; i++)
820         rem[i] += (basis[i] * scale +
821                    (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
822                   (BASIS_SHIFT - RECON_SHIFT);
823 }
824
825 static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
826                     int stride, int h)
827 {
828     return 0;
829 }
830
831 void ff_set_cmp(DSPContext *c, me_cmp_func *cmp, int type)
832 {
833     int i;
834
835     memset(cmp, 0, sizeof(void *) * 6);
836
837     for (i = 0; i < 6; i++) {
838         switch (type & 0xFF) {
839         case FF_CMP_SAD:
840             cmp[i] = c->sad[i];
841             break;
842         case FF_CMP_SATD:
843             cmp[i] = c->hadamard8_diff[i];
844             break;
845         case FF_CMP_SSE:
846             cmp[i] = c->sse[i];
847             break;
848         case FF_CMP_DCT:
849             cmp[i] = c->dct_sad[i];
850             break;
851         case FF_CMP_DCT264:
852             cmp[i] = c->dct264_sad[i];
853             break;
854         case FF_CMP_DCTMAX:
855             cmp[i] = c->dct_max[i];
856             break;
857         case FF_CMP_PSNR:
858             cmp[i] = c->quant_psnr[i];
859             break;
860         case FF_CMP_BIT:
861             cmp[i] = c->bit[i];
862             break;
863         case FF_CMP_RD:
864             cmp[i] = c->rd[i];
865             break;
866         case FF_CMP_VSAD:
867             cmp[i] = c->vsad[i];
868             break;
869         case FF_CMP_VSSE:
870             cmp[i] = c->vsse[i];
871             break;
872         case FF_CMP_ZERO:
873             cmp[i] = zero_cmp;
874             break;
875         case FF_CMP_NSSE:
876             cmp[i] = c->nsse[i];
877             break;
878 #if CONFIG_DWT
879         case FF_CMP_W53:
880             cmp[i]= c->w53[i];
881             break;
882         case FF_CMP_W97:
883             cmp[i]= c->w97[i];
884             break;
885 #endif
886         default:
887             av_log(NULL, AV_LOG_ERROR,
888                    "internal error in cmp function selection\n");
889         }
890     }
891 }
892
893 #define BUTTERFLY2(o1, o2, i1, i2)              \
894     o1 = (i1) + (i2);                           \
895     o2 = (i1) - (i2);
896
897 #define BUTTERFLY1(x, y)                        \
898     {                                           \
899         int a, b;                               \
900         a = x;                                  \
901         b = y;                                  \
902         x = a + b;                              \
903         y = a - b;                              \
904     }
905
906 #define BUTTERFLYA(x, y) (FFABS((x) + (y)) + FFABS((x) - (y)))
907
908 static int hadamard8_diff8x8_c(MpegEncContext *s, uint8_t *dst,
909                                uint8_t *src, int stride, int h)
910 {
911     int i, temp[64], sum = 0;
912
913     av_assert2(h == 8);
914
915     for (i = 0; i < 8; i++) {
916         // FIXME: try pointer walks
917         BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
918                    src[stride * i + 0] - dst[stride * i + 0],
919                    src[stride * i + 1] - dst[stride * i + 1]);
920         BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
921                    src[stride * i + 2] - dst[stride * i + 2],
922                    src[stride * i + 3] - dst[stride * i + 3]);
923         BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
924                    src[stride * i + 4] - dst[stride * i + 4],
925                    src[stride * i + 5] - dst[stride * i + 5]);
926         BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
927                    src[stride * i + 6] - dst[stride * i + 6],
928                    src[stride * i + 7] - dst[stride * i + 7]);
929
930         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
931         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
932         BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
933         BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
934
935         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
936         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
937         BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
938         BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
939     }
940
941     for (i = 0; i < 8; i++) {
942         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
943         BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
944         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
945         BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
946
947         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
948         BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
949         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
950         BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
951
952         sum += BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i]) +
953                BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i]) +
954                BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i]) +
955                BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
956     }
957     return sum;
958 }
959
960 static int hadamard8_intra8x8_c(MpegEncContext *s, uint8_t *src,
961                                 uint8_t *dummy, int stride, int h)
962 {
963     int i, temp[64], sum = 0;
964
965     av_assert2(h == 8);
966
967     for (i = 0; i < 8; i++) {
968         // FIXME: try pointer walks
969         BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
970                    src[stride * i + 0], src[stride * i + 1]);
971         BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
972                    src[stride * i + 2], src[stride * i + 3]);
973         BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
974                    src[stride * i + 4], src[stride * i + 5]);
975         BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
976                    src[stride * i + 6], src[stride * i + 7]);
977
978         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
979         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
980         BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
981         BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
982
983         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
984         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
985         BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
986         BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
987     }
988
989     for (i = 0; i < 8; i++) {
990         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
991         BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
992         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
993         BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
994
995         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
996         BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
997         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
998         BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
999
1000         sum +=
1001             BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i])
1002             + BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i])
1003             + BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i])
1004             + BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
1005     }
1006
1007     sum -= FFABS(temp[8 * 0] + temp[8 * 4]); // -mean
1008
1009     return sum;
1010 }
1011
1012 static int dct_sad8x8_c(MpegEncContext *s, uint8_t *src1,
1013                         uint8_t *src2, int stride, int h)
1014 {
1015     LOCAL_ALIGNED_16(int16_t, temp, [64]);
1016
1017     av_assert2(h == 8);
1018
1019     s->dsp.diff_pixels(temp, src1, src2, stride);
1020     s->dsp.fdct(temp);
1021     return s->dsp.sum_abs_dctelem(temp);
1022 }
1023
1024 #if CONFIG_GPL
1025 #define DCT8_1D                                         \
1026     {                                                   \
1027         const int s07 = SRC(0) + SRC(7);                \
1028         const int s16 = SRC(1) + SRC(6);                \
1029         const int s25 = SRC(2) + SRC(5);                \
1030         const int s34 = SRC(3) + SRC(4);                \
1031         const int a0  = s07 + s34;                      \
1032         const int a1  = s16 + s25;                      \
1033         const int a2  = s07 - s34;                      \
1034         const int a3  = s16 - s25;                      \
1035         const int d07 = SRC(0) - SRC(7);                \
1036         const int d16 = SRC(1) - SRC(6);                \
1037         const int d25 = SRC(2) - SRC(5);                \
1038         const int d34 = SRC(3) - SRC(4);                \
1039         const int a4  = d16 + d25 + (d07 + (d07 >> 1)); \
1040         const int a5  = d07 - d34 - (d25 + (d25 >> 1)); \
1041         const int a6  = d07 + d34 - (d16 + (d16 >> 1)); \
1042         const int a7  = d16 - d25 + (d34 + (d34 >> 1)); \
1043         DST(0, a0 + a1);                                \
1044         DST(1, a4 + (a7 >> 2));                         \
1045         DST(2, a2 + (a3 >> 1));                         \
1046         DST(3, a5 + (a6 >> 2));                         \
1047         DST(4, a0 - a1);                                \
1048         DST(5, a6 - (a5 >> 2));                         \
1049         DST(6, (a2 >> 1) - a3);                         \
1050         DST(7, (a4 >> 2) - a7);                         \
1051     }
1052
1053 static int dct264_sad8x8_c(MpegEncContext *s, uint8_t *src1,
1054                            uint8_t *src2, int stride, int h)
1055 {
1056     int16_t dct[8][8];
1057     int i, sum = 0;
1058
1059     s->dsp.diff_pixels(dct[0], src1, src2, stride);
1060
1061 #define SRC(x) dct[i][x]
1062 #define DST(x, v) dct[i][x] = v
1063     for (i = 0; i < 8; i++)
1064         DCT8_1D
1065 #undef SRC
1066 #undef DST
1067
1068 #define SRC(x) dct[x][i]
1069 #define DST(x, v) sum += FFABS(v)
1070         for (i = 0; i < 8; i++)
1071             DCT8_1D
1072 #undef SRC
1073 #undef DST
1074             return sum;
1075 }
1076 #endif
1077
1078 static int dct_max8x8_c(MpegEncContext *s, uint8_t *src1,
1079                         uint8_t *src2, int stride, int h)
1080 {
1081     LOCAL_ALIGNED_16(int16_t, temp, [64]);
1082     int sum = 0, i;
1083
1084     av_assert2(h == 8);
1085
1086     s->dsp.diff_pixels(temp, src1, src2, stride);
1087     s->dsp.fdct(temp);
1088
1089     for (i = 0; i < 64; i++)
1090         sum = FFMAX(sum, FFABS(temp[i]));
1091
1092     return sum;
1093 }
1094
1095 static int quant_psnr8x8_c(MpegEncContext *s, uint8_t *src1,
1096                            uint8_t *src2, int stride, int h)
1097 {
1098     LOCAL_ALIGNED_16(int16_t, temp, [64 * 2]);
1099     int16_t *const bak = temp + 64;
1100     int sum = 0, i;
1101
1102     av_assert2(h == 8);
1103     s->mb_intra = 0;
1104
1105     s->dsp.diff_pixels(temp, src1, src2, stride);
1106
1107     memcpy(bak, temp, 64 * sizeof(int16_t));
1108
1109     s->block_last_index[0 /* FIXME */] =
1110         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1111     s->dct_unquantize_inter(s, temp, 0, s->qscale);
1112     ff_simple_idct_8(temp); // FIXME
1113
1114     for (i = 0; i < 64; i++)
1115         sum += (temp[i] - bak[i]) * (temp[i] - bak[i]);
1116
1117     return sum;
1118 }
1119
1120 static int rd8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
1121                    int stride, int h)
1122 {
1123     const uint8_t *scantable = s->intra_scantable.permutated;
1124     LOCAL_ALIGNED_16(int16_t, temp, [64]);
1125     LOCAL_ALIGNED_16(uint8_t, lsrc1, [64]);
1126     LOCAL_ALIGNED_16(uint8_t, lsrc2, [64]);
1127     int i, last, run, bits, level, distortion, start_i;
1128     const int esc_length = s->ac_esc_length;
1129     uint8_t *length, *last_length;
1130
1131     av_assert2(h == 8);
1132
1133     copy_block8(lsrc1, src1, 8, stride, 8);
1134     copy_block8(lsrc2, src2, 8, stride, 8);
1135
1136     s->dsp.diff_pixels(temp, lsrc1, lsrc2, 8);
1137
1138     s->block_last_index[0 /* FIXME */] =
1139     last                               =
1140         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1141
1142     bits = 0;
1143
1144     if (s->mb_intra) {
1145         start_i     = 1;
1146         length      = s->intra_ac_vlc_length;
1147         last_length = s->intra_ac_vlc_last_length;
1148         bits       += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
1149     } else {
1150         start_i     = 0;
1151         length      = s->inter_ac_vlc_length;
1152         last_length = s->inter_ac_vlc_last_length;
1153     }
1154
1155     if (last >= start_i) {
1156         run = 0;
1157         for (i = start_i; i < last; i++) {
1158             int j = scantable[i];
1159             level = temp[j];
1160
1161             if (level) {
1162                 level += 64;
1163                 if ((level & (~127)) == 0)
1164                     bits += length[UNI_AC_ENC_INDEX(run, level)];
1165                 else
1166                     bits += esc_length;
1167                 run = 0;
1168             } else
1169                 run++;
1170         }
1171         i = scantable[last];
1172
1173         level = temp[i] + 64;
1174
1175         av_assert2(level - 64);
1176
1177         if ((level & (~127)) == 0) {
1178             bits += last_length[UNI_AC_ENC_INDEX(run, level)];
1179         } else
1180             bits += esc_length;
1181     }
1182
1183     if (last >= 0) {
1184         if (s->mb_intra)
1185             s->dct_unquantize_intra(s, temp, 0, s->qscale);
1186         else
1187             s->dct_unquantize_inter(s, temp, 0, s->qscale);
1188     }
1189
1190     s->dsp.idct_add(lsrc2, 8, temp);
1191
1192     distortion = s->dsp.sse[1](NULL, lsrc2, lsrc1, 8, 8);
1193
1194     return distortion + ((bits * s->qscale * s->qscale * 109 + 64) >> 7);
1195 }
1196
1197 static int bit8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
1198                     int stride, int h)
1199 {
1200     const uint8_t *scantable = s->intra_scantable.permutated;
1201     LOCAL_ALIGNED_16(int16_t, temp, [64]);
1202     int i, last, run, bits, level, start_i;
1203     const int esc_length = s->ac_esc_length;
1204     uint8_t *length, *last_length;
1205
1206     av_assert2(h == 8);
1207
1208     s->dsp.diff_pixels(temp, src1, src2, stride);
1209
1210     s->block_last_index[0 /* FIXME */] =
1211     last                               =
1212         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
1213
1214     bits = 0;
1215
1216     if (s->mb_intra) {
1217         start_i     = 1;
1218         length      = s->intra_ac_vlc_length;
1219         last_length = s->intra_ac_vlc_last_length;
1220         bits       += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
1221     } else {
1222         start_i     = 0;
1223         length      = s->inter_ac_vlc_length;
1224         last_length = s->inter_ac_vlc_last_length;
1225     }
1226
1227     if (last >= start_i) {
1228         run = 0;
1229         for (i = start_i; i < last; i++) {
1230             int j = scantable[i];
1231             level = temp[j];
1232
1233             if (level) {
1234                 level += 64;
1235                 if ((level & (~127)) == 0)
1236                     bits += length[UNI_AC_ENC_INDEX(run, level)];
1237                 else
1238                     bits += esc_length;
1239                 run = 0;
1240             } else
1241                 run++;
1242         }
1243         i = scantable[last];
1244
1245         level = temp[i] + 64;
1246
1247         av_assert2(level - 64);
1248
1249         if ((level & (~127)) == 0)
1250             bits += last_length[UNI_AC_ENC_INDEX(run, level)];
1251         else
1252             bits += esc_length;
1253     }
1254
1255     return bits;
1256 }
1257
1258 #define VSAD_INTRA(size)                                                \
1259 static int vsad_intra ## size ## _c(MpegEncContext *c,                  \
1260                                     uint8_t *s, uint8_t *dummy,         \
1261                                     int stride, int h)                  \
1262 {                                                                       \
1263     int score = 0, x, y;                                                \
1264                                                                         \
1265     for (y = 1; y < h; y++) {                                           \
1266         for (x = 0; x < size; x += 4) {                                 \
1267             score += FFABS(s[x]     - s[x + stride])     +              \
1268                      FFABS(s[x + 1] - s[x + stride + 1]) +              \
1269                      FFABS(s[x + 2] - s[x + 2 + stride]) +              \
1270                      FFABS(s[x + 3] - s[x + 3 + stride]);               \
1271         }                                                               \
1272         s += stride;                                                    \
1273     }                                                                   \
1274                                                                         \
1275     return score;                                                       \
1276 }
1277 VSAD_INTRA(8)
1278 VSAD_INTRA(16)
1279
1280 #define VSAD(size)                                                             \
1281 static int vsad ## size ## _c(MpegEncContext *c,                               \
1282                               uint8_t *s1, uint8_t *s2,                        \
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++)                                             \
1289             score += FFABS(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);   \
1290         s1 += stride;                                                          \
1291         s2 += stride;                                                          \
1292     }                                                                          \
1293                                                                                \
1294     return score;                                                              \
1295 }
1296 VSAD(8)
1297 VSAD(16)
1298
1299 #define SQ(a) ((a) * (a))
1300 #define VSSE_INTRA(size)                                                \
1301 static int vsse_intra ## size ## _c(MpegEncContext *c,                  \
1302                                     uint8_t *s, uint8_t *dummy,         \
1303                                     int stride, int h)                  \
1304 {                                                                       \
1305     int score = 0, x, y;                                                \
1306                                                                         \
1307     for (y = 1; y < h; y++) {                                           \
1308         for (x = 0; x < size; x += 4) {                                 \
1309             score += SQ(s[x]     - s[x + stride]) +                     \
1310                      SQ(s[x + 1] - s[x + stride + 1]) +                 \
1311                      SQ(s[x + 2] - s[x + stride + 2]) +                 \
1312                      SQ(s[x + 3] - s[x + stride + 3]);                  \
1313         }                                                               \
1314         s += stride;                                                    \
1315     }                                                                   \
1316                                                                         \
1317     return score;                                                       \
1318 }
1319 VSSE_INTRA(8)
1320 VSSE_INTRA(16)
1321
1322 #define VSSE(size)                                                             \
1323 static int vsse ## size ## _c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,     \
1324                     int stride, int h)                                         \
1325 {                                                                              \
1326     int score = 0, x, y;                                                       \
1327                                                                                \
1328     for (y = 1; y < h; y++) {                                                  \
1329         for (x = 0; x < size; x++)                                             \
1330             score += SQ(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);      \
1331         s1 += stride;                                                          \
1332         s2 += stride;                                                          \
1333     }                                                                          \
1334                                                                                \
1335     return score;                                                              \
1336 }
1337 VSSE(8)
1338 VSSE(16)
1339
1340 #define WRAPPER8_16_SQ(name8, name16)                                   \
1341 static int name16(MpegEncContext *s, uint8_t *dst, uint8_t *src,        \
1342                   int stride, int h)                                    \
1343 {                                                                       \
1344     int score = 0;                                                      \
1345                                                                         \
1346     score += name8(s, dst, src, stride, 8);                             \
1347     score += name8(s, dst + 8, src + 8, stride, 8);                     \
1348     if (h == 16) {                                                      \
1349         dst   += 8 * stride;                                            \
1350         src   += 8 * stride;                                            \
1351         score += name8(s, dst, src, stride, 8);                         \
1352         score += name8(s, dst + 8, src + 8, stride, 8);                 \
1353     }                                                                   \
1354     return score;                                                       \
1355 }
1356
1357 WRAPPER8_16_SQ(hadamard8_diff8x8_c, hadamard8_diff16_c)
1358 WRAPPER8_16_SQ(hadamard8_intra8x8_c, hadamard8_intra16_c)
1359 WRAPPER8_16_SQ(dct_sad8x8_c, dct_sad16_c)
1360 #if CONFIG_GPL
1361 WRAPPER8_16_SQ(dct264_sad8x8_c, dct264_sad16_c)
1362 #endif
1363 WRAPPER8_16_SQ(dct_max8x8_c, dct_max16_c)
1364 WRAPPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c)
1365 WRAPPER8_16_SQ(rd8x8_c, rd16_c)
1366 WRAPPER8_16_SQ(bit8x8_c, bit16_c)
1367
1368 static inline uint32_t clipf_c_one(uint32_t a, uint32_t mini,
1369                                    uint32_t maxi, uint32_t maxisign)
1370 {
1371     if (a > mini)
1372         return mini;
1373     else if ((a ^ (1U << 31)) > maxisign)
1374         return maxi;
1375     else
1376         return a;
1377 }
1378
1379 static void vector_clipf_c_opposite_sign(float *dst, const float *src,
1380                                          float *min, float *max, int len)
1381 {
1382     int i;
1383     uint32_t mini        = *(uint32_t *) min;
1384     uint32_t maxi        = *(uint32_t *) max;
1385     uint32_t maxisign    = maxi ^ (1U << 31);
1386     uint32_t *dsti       = (uint32_t *) dst;
1387     const uint32_t *srci = (const uint32_t *) src;
1388
1389     for (i = 0; i < len; i += 8) {
1390         dsti[i + 0] = clipf_c_one(srci[i + 0], mini, maxi, maxisign);
1391         dsti[i + 1] = clipf_c_one(srci[i + 1], mini, maxi, maxisign);
1392         dsti[i + 2] = clipf_c_one(srci[i + 2], mini, maxi, maxisign);
1393         dsti[i + 3] = clipf_c_one(srci[i + 3], mini, maxi, maxisign);
1394         dsti[i + 4] = clipf_c_one(srci[i + 4], mini, maxi, maxisign);
1395         dsti[i + 5] = clipf_c_one(srci[i + 5], mini, maxi, maxisign);
1396         dsti[i + 6] = clipf_c_one(srci[i + 6], mini, maxi, maxisign);
1397         dsti[i + 7] = clipf_c_one(srci[i + 7], mini, maxi, maxisign);
1398     }
1399 }
1400
1401 static void vector_clipf_c(float *dst, const float *src,
1402                            float min, float max, int len)
1403 {
1404     int i;
1405
1406     if (min < 0 && max > 0) {
1407         vector_clipf_c_opposite_sign(dst, src, &min, &max, len);
1408     } else {
1409         for (i = 0; i < len; i += 8) {
1410             dst[i]     = av_clipf(src[i], min, max);
1411             dst[i + 1] = av_clipf(src[i + 1], min, max);
1412             dst[i + 2] = av_clipf(src[i + 2], min, max);
1413             dst[i + 3] = av_clipf(src[i + 3], min, max);
1414             dst[i + 4] = av_clipf(src[i + 4], min, max);
1415             dst[i + 5] = av_clipf(src[i + 5], min, max);
1416             dst[i + 6] = av_clipf(src[i + 6], min, max);
1417             dst[i + 7] = av_clipf(src[i + 7], min, max);
1418         }
1419     }
1420 }
1421
1422 static int32_t scalarproduct_int16_c(const int16_t *v1, const int16_t *v2,
1423                                      int order)
1424 {
1425     int res = 0;
1426
1427     while (order--)
1428         res += *v1++ **v2++;
1429
1430     return res;
1431 }
1432
1433 static void vector_clip_int32_c(int32_t *dst, const int32_t *src, int32_t min,
1434                                 int32_t max, unsigned int len)
1435 {
1436     do {
1437         *dst++ = av_clip(*src++, min, max);
1438         *dst++ = av_clip(*src++, min, max);
1439         *dst++ = av_clip(*src++, min, max);
1440         *dst++ = av_clip(*src++, min, max);
1441         *dst++ = av_clip(*src++, min, max);
1442         *dst++ = av_clip(*src++, min, max);
1443         *dst++ = av_clip(*src++, min, max);
1444         *dst++ = av_clip(*src++, min, max);
1445         len   -= 8;
1446     } while (len > 0);
1447 }
1448
1449 static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
1450 {
1451     ff_j_rev_dct(block);
1452     put_pixels_clamped_c(block, dest, line_size);
1453 }
1454
1455 static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
1456 {
1457     ff_j_rev_dct(block);
1458     add_pixels_clamped_c(block, dest, line_size);
1459 }
1460
1461 static void ff_jref_idct4_put(uint8_t *dest, int line_size, int16_t *block)
1462 {
1463     ff_j_rev_dct4 (block);
1464     put_pixels_clamped4_c(block, dest, line_size);
1465 }
1466 static void ff_jref_idct4_add(uint8_t *dest, int line_size, int16_t *block)
1467 {
1468     ff_j_rev_dct4 (block);
1469     add_pixels_clamped4_c(block, dest, line_size);
1470 }
1471
1472 static void ff_jref_idct2_put(uint8_t *dest, int line_size, int16_t *block)
1473 {
1474     ff_j_rev_dct2 (block);
1475     put_pixels_clamped2_c(block, dest, line_size);
1476 }
1477 static void ff_jref_idct2_add(uint8_t *dest, int line_size, int16_t *block)
1478 {
1479     ff_j_rev_dct2 (block);
1480     add_pixels_clamped2_c(block, dest, line_size);
1481 }
1482
1483 static void ff_jref_idct1_put(uint8_t *dest, int line_size, int16_t *block)
1484 {
1485     dest[0] = av_clip_uint8((block[0] + 4)>>3);
1486 }
1487 static void ff_jref_idct1_add(uint8_t *dest, int line_size, int16_t *block)
1488 {
1489     dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
1490 }
1491
1492 /* draw the edges of width 'w' of an image of size width, height */
1493 // FIXME: Check that this is OK for MPEG-4 interlaced.
1494 static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
1495                            int w, int h, int sides)
1496 {
1497     uint8_t *ptr = buf, *last_line;
1498     int i;
1499
1500     /* left and right */
1501     for (i = 0; i < height; i++) {
1502         memset(ptr - w, ptr[0], w);
1503         memset(ptr + width, ptr[width - 1], w);
1504         ptr += wrap;
1505     }
1506
1507     /* top and bottom + corners */
1508     buf -= w;
1509     last_line = buf + (height - 1) * wrap;
1510     if (sides & EDGE_TOP)
1511         for (i = 0; i < h; i++)
1512             // top
1513             memcpy(buf - (i + 1) * wrap, buf, width + w + w);
1514     if (sides & EDGE_BOTTOM)
1515         for (i = 0; i < h; i++)
1516             // bottom
1517             memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
1518 }
1519
1520 static void clear_block_8_c(int16_t *block)
1521 {
1522     memset(block, 0, sizeof(int16_t) * 64);
1523 }
1524
1525 static void clear_blocks_8_c(int16_t *blocks)
1526 {
1527     memset(blocks, 0, sizeof(int16_t) * 6 * 64);
1528 }
1529
1530 /* init static data */
1531 av_cold void ff_dsputil_static_init(void)
1532 {
1533     int i;
1534
1535     for (i = 0; i < 512; i++)
1536         ff_square_tab[i] = (i - 256) * (i - 256);
1537 }
1538
1539 int ff_check_alignment(void)
1540 {
1541     static int did_fail = 0;
1542     LOCAL_ALIGNED_16(int, aligned, [4]);
1543
1544     if ((intptr_t)aligned & 15) {
1545         if (!did_fail) {
1546 #if HAVE_MMX || HAVE_ALTIVEC
1547             av_log(NULL, AV_LOG_ERROR,
1548                 "Compiler did not align stack variables. Libavcodec has been miscompiled\n"
1549                 "and may be very slow or crash. This is not a bug in libavcodec,\n"
1550                 "but in the compiler. You may try recompiling using gcc >= 4.2.\n"
1551                 "Do not report crashes to FFmpeg developers.\n");
1552 #endif
1553             did_fail=1;
1554         }
1555         return -1;
1556     }
1557     return 0;
1558 }
1559
1560 av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
1561 {
1562     const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
1563
1564     ff_check_alignment();
1565
1566 #if CONFIG_ENCODERS
1567     if (avctx->bits_per_raw_sample == 10) {
1568         c->fdct    = ff_jpeg_fdct_islow_10;
1569         c->fdct248 = ff_fdct248_islow_10;
1570     } else {
1571         if (avctx->dct_algo == FF_DCT_FASTINT) {
1572             c->fdct    = ff_fdct_ifast;
1573             c->fdct248 = ff_fdct_ifast248;
1574         } else if (avctx->dct_algo == FF_DCT_FAAN) {
1575             c->fdct    = ff_faandct;
1576             c->fdct248 = ff_faandct248;
1577         } else {
1578             c->fdct    = ff_jpeg_fdct_islow_8; // slow/accurate/default
1579             c->fdct248 = ff_fdct248_islow_8;
1580         }
1581     }
1582 #endif /* CONFIG_ENCODERS */
1583
1584     if (avctx->lowres==1) {
1585         c->idct_put              = ff_jref_idct4_put;
1586         c->idct_add              = ff_jref_idct4_add;
1587         c->idct                  = ff_j_rev_dct4;
1588         c->idct_permutation_type = FF_NO_IDCT_PERM;
1589     } else if (avctx->lowres==2) {
1590         c->idct_put              =  ff_jref_idct2_put;
1591         c->idct_add              =  ff_jref_idct2_add;
1592         c->idct                  =  ff_j_rev_dct2;
1593         c->idct_permutation_type = FF_NO_IDCT_PERM;
1594     } else if (avctx->lowres==3) {
1595         c->idct_put              =  ff_jref_idct1_put;
1596         c->idct_add              =  ff_jref_idct1_add;
1597         c->idct                  =  ff_j_rev_dct1;
1598         c->idct_permutation_type = FF_NO_IDCT_PERM;
1599     } else {
1600         if (avctx->bits_per_raw_sample == 10) {
1601             c->idct_put              = ff_simple_idct_put_10;
1602             c->idct_add              = ff_simple_idct_add_10;
1603             c->idct                  = ff_simple_idct_10;
1604             c->idct_permutation_type = FF_NO_IDCT_PERM;
1605         } else if (avctx->bits_per_raw_sample == 12) {
1606             c->idct_put              = ff_simple_idct_put_12;
1607             c->idct_add              = ff_simple_idct_add_12;
1608             c->idct                  = ff_simple_idct_12;
1609             c->idct_permutation_type = FF_NO_IDCT_PERM;
1610         } else {
1611         if (avctx->idct_algo == FF_IDCT_INT) {
1612             c->idct_put              = jref_idct_put;
1613             c->idct_add              = jref_idct_add;
1614             c->idct                  = ff_j_rev_dct;
1615             c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
1616         } else if (avctx->idct_algo == FF_IDCT_FAAN) {
1617             c->idct_put              = ff_faanidct_put;
1618             c->idct_add              = ff_faanidct_add;
1619             c->idct                  = ff_faanidct;
1620             c->idct_permutation_type = FF_NO_IDCT_PERM;
1621         } else { // accurate/default
1622             c->idct_put              = ff_simple_idct_put_8;
1623             c->idct_add              = ff_simple_idct_add_8;
1624             c->idct                  = ff_simple_idct_8;
1625             c->idct_permutation_type = FF_NO_IDCT_PERM;
1626         }
1627         }
1628     }
1629
1630     c->diff_pixels = diff_pixels_c;
1631
1632     c->put_pixels_clamped        = put_pixels_clamped_c;
1633     c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
1634     c->add_pixels_clamped        = add_pixels_clamped_c;
1635
1636     c->sum_abs_dctelem = sum_abs_dctelem_c;
1637
1638     c->gmc1 = gmc1_c;
1639     c->gmc  = ff_gmc_c;
1640
1641     c->pix_sum   = pix_sum_c;
1642     c->pix_norm1 = pix_norm1_c;
1643
1644     c->fill_block_tab[0] = fill_block16_c;
1645     c->fill_block_tab[1] = fill_block8_c;
1646
1647     /* TODO [0] 16  [1] 8 */
1648     c->pix_abs[0][0] = pix_abs16_c;
1649     c->pix_abs[0][1] = pix_abs16_x2_c;
1650     c->pix_abs[0][2] = pix_abs16_y2_c;
1651     c->pix_abs[0][3] = pix_abs16_xy2_c;
1652     c->pix_abs[1][0] = pix_abs8_c;
1653     c->pix_abs[1][1] = pix_abs8_x2_c;
1654     c->pix_abs[1][2] = pix_abs8_y2_c;
1655     c->pix_abs[1][3] = pix_abs8_xy2_c;
1656
1657 #define SET_CMP_FUNC(name)                      \
1658     c->name[0] = name ## 16_c;                  \
1659     c->name[1] = name ## 8x8_c;
1660
1661     SET_CMP_FUNC(hadamard8_diff)
1662     c->hadamard8_diff[4] = hadamard8_intra16_c;
1663     c->hadamard8_diff[5] = hadamard8_intra8x8_c;
1664     SET_CMP_FUNC(dct_sad)
1665     SET_CMP_FUNC(dct_max)
1666 #if CONFIG_GPL
1667     SET_CMP_FUNC(dct264_sad)
1668 #endif
1669     c->sad[0] = pix_abs16_c;
1670     c->sad[1] = pix_abs8_c;
1671     c->sse[0] = sse16_c;
1672     c->sse[1] = sse8_c;
1673     c->sse[2] = sse4_c;
1674     SET_CMP_FUNC(quant_psnr)
1675     SET_CMP_FUNC(rd)
1676     SET_CMP_FUNC(bit)
1677     c->vsad[0] = vsad16_c;
1678     c->vsad[1] = vsad8_c;
1679     c->vsad[4] = vsad_intra16_c;
1680     c->vsad[5] = vsad_intra8_c;
1681     c->vsse[0] = vsse16_c;
1682     c->vsse[1] = vsse8_c;
1683     c->vsse[4] = vsse_intra16_c;
1684     c->vsse[5] = vsse_intra8_c;
1685     c->nsse[0] = nsse16_c;
1686     c->nsse[1] = nsse8_c;
1687 #if CONFIG_SNOW_DECODER || CONFIG_SNOW_ENCODER
1688     ff_dsputil_init_dwt(c);
1689 #endif
1690
1691     c->bswap_buf   = bswap_buf;
1692     c->bswap16_buf = bswap16_buf;
1693
1694     c->try_8x8basis = try_8x8basis_c;
1695     c->add_8x8basis = add_8x8basis_c;
1696
1697     c->scalarproduct_int16 = scalarproduct_int16_c;
1698     c->vector_clip_int32   = vector_clip_int32_c;
1699     c->vector_clipf        = vector_clipf_c;
1700
1701     c->shrink[0] = av_image_copy_plane;
1702     c->shrink[1] = ff_shrink22;
1703     c->shrink[2] = ff_shrink44;
1704     c->shrink[3] = ff_shrink88;
1705
1706     c->draw_edges = draw_edges_8_c;
1707
1708     c->clear_block  = clear_block_8_c;
1709     c->clear_blocks = clear_blocks_8_c;
1710
1711     switch (avctx->bits_per_raw_sample) {
1712     case 9:
1713     case 10:
1714     case 12:
1715     case 14:
1716         c->get_pixels = get_pixels_16_c;
1717         break;
1718     default:
1719         if (avctx->bits_per_raw_sample<=8 || avctx->codec_type != AVMEDIA_TYPE_VIDEO) {
1720             c->get_pixels = get_pixels_8_c;
1721         }
1722         break;
1723     }
1724
1725
1726     if (ARCH_ALPHA)
1727         ff_dsputil_init_alpha(c, avctx);
1728     if (ARCH_ARM)
1729         ff_dsputil_init_arm(c, avctx, high_bit_depth);
1730     if (ARCH_PPC)
1731         ff_dsputil_init_ppc(c, avctx, high_bit_depth);
1732     if (ARCH_X86)
1733         ff_dsputil_init_x86(c, avctx, high_bit_depth);
1734
1735     ff_init_scantable_permutation(c->idct_permutation,
1736                                   c->idct_permutation_type);
1737 }
1738
1739 av_cold void dsputil_init(DSPContext* c, AVCodecContext *avctx)
1740 {
1741     ff_dsputil_init(c, avctx);
1742 }
1743
1744 av_cold void avpriv_dsputil_init(DSPContext *c, AVCodecContext *avctx)
1745 {
1746     ff_dsputil_init(c, avctx);
1747 }