Merge commit 'c166148409fe8f0dbccef2fe684286a40ba1e37d'
[ffmpeg.git] / libavcodec / dsputil.c
1 /*
2  * DSP utils
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * DSP utils
26  */
27
28 #include "libavutil/attributes.h"
29 #include "libavutil/internal.h"
30 #include "avcodec.h"
31 #include "copy_block.h"
32 #include "dct.h"
33 #include "dsputil.h"
34 #include "simple_idct.h"
35 #include "faandct.h"
36 #include "mpegvideo.h"
37 #include "config.h"
38
39 uint32_t ff_square_tab[512] = { 0, };
40
41 #define BIT_DEPTH 16
42 #include "dsputilenc_template.c"
43 #undef BIT_DEPTH
44
45 #define BIT_DEPTH 8
46 #include "dsputilenc_template.c"
47
48 static int sse4_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
49                   int line_size, int h)
50 {
51     int s = 0, i;
52     uint32_t *sq = ff_square_tab + 256;
53
54     for (i = 0; i < h; i++) {
55         s    += sq[pix1[0] - pix2[0]];
56         s    += sq[pix1[1] - pix2[1]];
57         s    += sq[pix1[2] - pix2[2]];
58         s    += sq[pix1[3] - pix2[3]];
59         pix1 += line_size;
60         pix2 += line_size;
61     }
62     return s;
63 }
64
65 static int sse8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
66                   int line_size, int h)
67 {
68     int s = 0, i;
69     uint32_t *sq = ff_square_tab + 256;
70
71     for (i = 0; i < h; i++) {
72         s    += sq[pix1[0] - pix2[0]];
73         s    += sq[pix1[1] - pix2[1]];
74         s    += sq[pix1[2] - pix2[2]];
75         s    += sq[pix1[3] - pix2[3]];
76         s    += sq[pix1[4] - pix2[4]];
77         s    += sq[pix1[5] - pix2[5]];
78         s    += sq[pix1[6] - pix2[6]];
79         s    += sq[pix1[7] - pix2[7]];
80         pix1 += line_size;
81         pix2 += line_size;
82     }
83     return s;
84 }
85
86 static int sse16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
87                    int line_size, int h)
88 {
89     int s = 0, i;
90     uint32_t *sq = ff_square_tab + 256;
91
92     for (i = 0; i < h; i++) {
93         s += sq[pix1[0]  - pix2[0]];
94         s += sq[pix1[1]  - pix2[1]];
95         s += sq[pix1[2]  - pix2[2]];
96         s += sq[pix1[3]  - pix2[3]];
97         s += sq[pix1[4]  - pix2[4]];
98         s += sq[pix1[5]  - pix2[5]];
99         s += sq[pix1[6]  - pix2[6]];
100         s += sq[pix1[7]  - pix2[7]];
101         s += sq[pix1[8]  - pix2[8]];
102         s += sq[pix1[9]  - pix2[9]];
103         s += sq[pix1[10] - pix2[10]];
104         s += sq[pix1[11] - pix2[11]];
105         s += sq[pix1[12] - pix2[12]];
106         s += sq[pix1[13] - pix2[13]];
107         s += sq[pix1[14] - pix2[14]];
108         s += sq[pix1[15] - pix2[15]];
109
110         pix1 += line_size;
111         pix2 += line_size;
112     }
113     return s;
114 }
115
116 static void diff_pixels_c(int16_t *av_restrict block, const uint8_t *s1,
117                           const uint8_t *s2, int stride)
118 {
119     int i;
120
121     /* read the pixels */
122     for (i = 0; i < 8; i++) {
123         block[0] = s1[0] - s2[0];
124         block[1] = s1[1] - s2[1];
125         block[2] = s1[2] - s2[2];
126         block[3] = s1[3] - s2[3];
127         block[4] = s1[4] - s2[4];
128         block[5] = s1[5] - s2[5];
129         block[6] = s1[6] - s2[6];
130         block[7] = s1[7] - s2[7];
131         s1      += stride;
132         s2      += stride;
133         block   += 8;
134     }
135 }
136
137 static int sum_abs_dctelem_c(int16_t *block)
138 {
139     int sum = 0, i;
140
141     for (i = 0; i < 64; i++)
142         sum += FFABS(block[i]);
143     return sum;
144 }
145
146 #define avg2(a, b) ((a + b + 1) >> 1)
147 #define avg4(a, b, c, d) ((a + b + c + d + 2) >> 2)
148
149 static inline int pix_abs16_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
150                               int line_size, int h)
151 {
152     int s = 0, i;
153
154     for (i = 0; i < h; i++) {
155         s    += abs(pix1[0]  - pix2[0]);
156         s    += abs(pix1[1]  - pix2[1]);
157         s    += abs(pix1[2]  - pix2[2]);
158         s    += abs(pix1[3]  - pix2[3]);
159         s    += abs(pix1[4]  - pix2[4]);
160         s    += abs(pix1[5]  - pix2[5]);
161         s    += abs(pix1[6]  - pix2[6]);
162         s    += abs(pix1[7]  - pix2[7]);
163         s    += abs(pix1[8]  - pix2[8]);
164         s    += abs(pix1[9]  - pix2[9]);
165         s    += abs(pix1[10] - pix2[10]);
166         s    += abs(pix1[11] - pix2[11]);
167         s    += abs(pix1[12] - pix2[12]);
168         s    += abs(pix1[13] - pix2[13]);
169         s    += abs(pix1[14] - pix2[14]);
170         s    += abs(pix1[15] - pix2[15]);
171         pix1 += line_size;
172         pix2 += line_size;
173     }
174     return s;
175 }
176
177 static int pix_abs16_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
178                           int line_size, int h)
179 {
180     int s = 0, i;
181
182     for (i = 0; i < h; i++) {
183         s    += abs(pix1[0]  - avg2(pix2[0],  pix2[1]));
184         s    += abs(pix1[1]  - avg2(pix2[1],  pix2[2]));
185         s    += abs(pix1[2]  - avg2(pix2[2],  pix2[3]));
186         s    += abs(pix1[3]  - avg2(pix2[3],  pix2[4]));
187         s    += abs(pix1[4]  - avg2(pix2[4],  pix2[5]));
188         s    += abs(pix1[5]  - avg2(pix2[5],  pix2[6]));
189         s    += abs(pix1[6]  - avg2(pix2[6],  pix2[7]));
190         s    += abs(pix1[7]  - avg2(pix2[7],  pix2[8]));
191         s    += abs(pix1[8]  - avg2(pix2[8],  pix2[9]));
192         s    += abs(pix1[9]  - avg2(pix2[9],  pix2[10]));
193         s    += abs(pix1[10] - avg2(pix2[10], pix2[11]));
194         s    += abs(pix1[11] - avg2(pix2[11], pix2[12]));
195         s    += abs(pix1[12] - avg2(pix2[12], pix2[13]));
196         s    += abs(pix1[13] - avg2(pix2[13], pix2[14]));
197         s    += abs(pix1[14] - avg2(pix2[14], pix2[15]));
198         s    += abs(pix1[15] - avg2(pix2[15], pix2[16]));
199         pix1 += line_size;
200         pix2 += line_size;
201     }
202     return s;
203 }
204
205 static int pix_abs16_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
206                           int line_size, int h)
207 {
208     int s = 0, i;
209     uint8_t *pix3 = pix2 + line_size;
210
211     for (i = 0; i < h; i++) {
212         s    += abs(pix1[0]  - avg2(pix2[0],  pix3[0]));
213         s    += abs(pix1[1]  - avg2(pix2[1],  pix3[1]));
214         s    += abs(pix1[2]  - avg2(pix2[2],  pix3[2]));
215         s    += abs(pix1[3]  - avg2(pix2[3],  pix3[3]));
216         s    += abs(pix1[4]  - avg2(pix2[4],  pix3[4]));
217         s    += abs(pix1[5]  - avg2(pix2[5],  pix3[5]));
218         s    += abs(pix1[6]  - avg2(pix2[6],  pix3[6]));
219         s    += abs(pix1[7]  - avg2(pix2[7],  pix3[7]));
220         s    += abs(pix1[8]  - avg2(pix2[8],  pix3[8]));
221         s    += abs(pix1[9]  - avg2(pix2[9],  pix3[9]));
222         s    += abs(pix1[10] - avg2(pix2[10], pix3[10]));
223         s    += abs(pix1[11] - avg2(pix2[11], pix3[11]));
224         s    += abs(pix1[12] - avg2(pix2[12], pix3[12]));
225         s    += abs(pix1[13] - avg2(pix2[13], pix3[13]));
226         s    += abs(pix1[14] - avg2(pix2[14], pix3[14]));
227         s    += abs(pix1[15] - avg2(pix2[15], pix3[15]));
228         pix1 += line_size;
229         pix2 += line_size;
230         pix3 += line_size;
231     }
232     return s;
233 }
234
235 static int pix_abs16_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
236                            int line_size, int h)
237 {
238     int s = 0, i;
239     uint8_t *pix3 = pix2 + line_size;
240
241     for (i = 0; i < h; i++) {
242         s    += abs(pix1[0]  - avg4(pix2[0],  pix2[1],  pix3[0],  pix3[1]));
243         s    += abs(pix1[1]  - avg4(pix2[1],  pix2[2],  pix3[1],  pix3[2]));
244         s    += abs(pix1[2]  - avg4(pix2[2],  pix2[3],  pix3[2],  pix3[3]));
245         s    += abs(pix1[3]  - avg4(pix2[3],  pix2[4],  pix3[3],  pix3[4]));
246         s    += abs(pix1[4]  - avg4(pix2[4],  pix2[5],  pix3[4],  pix3[5]));
247         s    += abs(pix1[5]  - avg4(pix2[5],  pix2[6],  pix3[5],  pix3[6]));
248         s    += abs(pix1[6]  - avg4(pix2[6],  pix2[7],  pix3[6],  pix3[7]));
249         s    += abs(pix1[7]  - avg4(pix2[7],  pix2[8],  pix3[7],  pix3[8]));
250         s    += abs(pix1[8]  - avg4(pix2[8],  pix2[9],  pix3[8],  pix3[9]));
251         s    += abs(pix1[9]  - avg4(pix2[9],  pix2[10], pix3[9],  pix3[10]));
252         s    += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
253         s    += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
254         s    += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
255         s    += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
256         s    += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
257         s    += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
258         pix1 += line_size;
259         pix2 += line_size;
260         pix3 += line_size;
261     }
262     return s;
263 }
264
265 static inline int pix_abs8_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
266                              int line_size, int h)
267 {
268     int s = 0, i;
269
270     for (i = 0; i < h; i++) {
271         s    += abs(pix1[0] - pix2[0]);
272         s    += abs(pix1[1] - pix2[1]);
273         s    += abs(pix1[2] - pix2[2]);
274         s    += abs(pix1[3] - pix2[3]);
275         s    += abs(pix1[4] - pix2[4]);
276         s    += abs(pix1[5] - pix2[5]);
277         s    += abs(pix1[6] - pix2[6]);
278         s    += abs(pix1[7] - pix2[7]);
279         pix1 += line_size;
280         pix2 += line_size;
281     }
282     return s;
283 }
284
285 static int pix_abs8_x2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
286                          int line_size, int h)
287 {
288     int s = 0, i;
289
290     for (i = 0; i < h; i++) {
291         s    += abs(pix1[0] - avg2(pix2[0], pix2[1]));
292         s    += abs(pix1[1] - avg2(pix2[1], pix2[2]));
293         s    += abs(pix1[2] - avg2(pix2[2], pix2[3]));
294         s    += abs(pix1[3] - avg2(pix2[3], pix2[4]));
295         s    += abs(pix1[4] - avg2(pix2[4], pix2[5]));
296         s    += abs(pix1[5] - avg2(pix2[5], pix2[6]));
297         s    += abs(pix1[6] - avg2(pix2[6], pix2[7]));
298         s    += abs(pix1[7] - avg2(pix2[7], pix2[8]));
299         pix1 += line_size;
300         pix2 += line_size;
301     }
302     return s;
303 }
304
305 static int pix_abs8_y2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
306                          int line_size, int h)
307 {
308     int s = 0, i;
309     uint8_t *pix3 = pix2 + line_size;
310
311     for (i = 0; i < h; i++) {
312         s    += abs(pix1[0] - avg2(pix2[0], pix3[0]));
313         s    += abs(pix1[1] - avg2(pix2[1], pix3[1]));
314         s    += abs(pix1[2] - avg2(pix2[2], pix3[2]));
315         s    += abs(pix1[3] - avg2(pix2[3], pix3[3]));
316         s    += abs(pix1[4] - avg2(pix2[4], pix3[4]));
317         s    += abs(pix1[5] - avg2(pix2[5], pix3[5]));
318         s    += abs(pix1[6] - avg2(pix2[6], pix3[6]));
319         s    += abs(pix1[7] - avg2(pix2[7], pix3[7]));
320         pix1 += line_size;
321         pix2 += line_size;
322         pix3 += line_size;
323     }
324     return s;
325 }
326
327 static int pix_abs8_xy2_c(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
328                           int line_size, int h)
329 {
330     int s = 0, i;
331     uint8_t *pix3 = pix2 + line_size;
332
333     for (i = 0; i < h; i++) {
334         s    += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
335         s    += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
336         s    += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
337         s    += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
338         s    += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
339         s    += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
340         s    += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
341         s    += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
342         pix1 += line_size;
343         pix2 += line_size;
344         pix3 += line_size;
345     }
346     return s;
347 }
348
349 static int nsse16_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
350 {
351     int score1 = 0, score2 = 0, x, y;
352
353     for (y = 0; y < h; y++) {
354         for (x = 0; x < 16; x++)
355             score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
356         if (y + 1 < h) {
357             for (x = 0; x < 15; x++)
358                 score2 += FFABS(s1[x]     - s1[x + stride] -
359                                 s1[x + 1] + s1[x + stride + 1]) -
360                           FFABS(s2[x]     - s2[x + stride] -
361                                 s2[x + 1] + s2[x + stride + 1]);
362         }
363         s1 += stride;
364         s2 += stride;
365     }
366
367     if (c)
368         return score1 + FFABS(score2) * c->avctx->nsse_weight;
369     else
370         return score1 + FFABS(score2) * 8;
371 }
372
373 static int nsse8_c(MpegEncContext *c, uint8_t *s1, uint8_t *s2, int stride, int h)
374 {
375     int score1 = 0, score2 = 0, x, y;
376
377     for (y = 0; y < h; y++) {
378         for (x = 0; x < 8; x++)
379             score1 += (s1[x] - s2[x]) * (s1[x] - s2[x]);
380         if (y + 1 < h) {
381             for (x = 0; x < 7; x++)
382                 score2 += FFABS(s1[x]     - s1[x + stride] -
383                                 s1[x + 1] + s1[x + stride + 1]) -
384                           FFABS(s2[x]     - s2[x + stride] -
385                                 s2[x + 1] + s2[x + stride + 1]);
386         }
387         s1 += stride;
388         s2 += stride;
389     }
390
391     if (c)
392         return score1 + FFABS(score2) * c->avctx->nsse_weight;
393     else
394         return score1 + FFABS(score2) * 8;
395 }
396
397 static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
398                     int stride, int h)
399 {
400     return 0;
401 }
402
403 void ff_set_cmp(DSPContext *c, me_cmp_func *cmp, int type)
404 {
405     int i;
406
407     memset(cmp, 0, sizeof(void *) * 6);
408
409     for (i = 0; i < 6; i++) {
410         switch (type & 0xFF) {
411         case FF_CMP_SAD:
412             cmp[i] = c->sad[i];
413             break;
414         case FF_CMP_SATD:
415             cmp[i] = c->hadamard8_diff[i];
416             break;
417         case FF_CMP_SSE:
418             cmp[i] = c->sse[i];
419             break;
420         case FF_CMP_DCT:
421             cmp[i] = c->dct_sad[i];
422             break;
423         case FF_CMP_DCT264:
424             cmp[i] = c->dct264_sad[i];
425             break;
426         case FF_CMP_DCTMAX:
427             cmp[i] = c->dct_max[i];
428             break;
429         case FF_CMP_PSNR:
430             cmp[i] = c->quant_psnr[i];
431             break;
432         case FF_CMP_BIT:
433             cmp[i] = c->bit[i];
434             break;
435         case FF_CMP_RD:
436             cmp[i] = c->rd[i];
437             break;
438         case FF_CMP_VSAD:
439             cmp[i] = c->vsad[i];
440             break;
441         case FF_CMP_VSSE:
442             cmp[i] = c->vsse[i];
443             break;
444         case FF_CMP_ZERO:
445             cmp[i] = zero_cmp;
446             break;
447         case FF_CMP_NSSE:
448             cmp[i] = c->nsse[i];
449             break;
450 #if CONFIG_DWT
451         case FF_CMP_W53:
452             cmp[i]= c->w53[i];
453             break;
454         case FF_CMP_W97:
455             cmp[i]= c->w97[i];
456             break;
457 #endif
458         default:
459             av_log(NULL, AV_LOG_ERROR,
460                    "internal error in cmp function selection\n");
461         }
462     }
463 }
464
465 #define BUTTERFLY2(o1, o2, i1, i2)              \
466     o1 = (i1) + (i2);                           \
467     o2 = (i1) - (i2);
468
469 #define BUTTERFLY1(x, y)                        \
470     {                                           \
471         int a, b;                               \
472         a = x;                                  \
473         b = y;                                  \
474         x = a + b;                              \
475         y = a - b;                              \
476     }
477
478 #define BUTTERFLYA(x, y) (FFABS((x) + (y)) + FFABS((x) - (y)))
479
480 static int hadamard8_diff8x8_c(MpegEncContext *s, uint8_t *dst,
481                                uint8_t *src, int stride, int h)
482 {
483     int i, temp[64], sum = 0;
484
485     av_assert2(h == 8);
486
487     for (i = 0; i < 8; i++) {
488         // FIXME: try pointer walks
489         BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
490                    src[stride * i + 0] - dst[stride * i + 0],
491                    src[stride * i + 1] - dst[stride * i + 1]);
492         BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
493                    src[stride * i + 2] - dst[stride * i + 2],
494                    src[stride * i + 3] - dst[stride * i + 3]);
495         BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
496                    src[stride * i + 4] - dst[stride * i + 4],
497                    src[stride * i + 5] - dst[stride * i + 5]);
498         BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
499                    src[stride * i + 6] - dst[stride * i + 6],
500                    src[stride * i + 7] - dst[stride * i + 7]);
501
502         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
503         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
504         BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
505         BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
506
507         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
508         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
509         BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
510         BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
511     }
512
513     for (i = 0; i < 8; i++) {
514         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
515         BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
516         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
517         BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
518
519         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
520         BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
521         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
522         BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
523
524         sum += BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i]) +
525                BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i]) +
526                BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i]) +
527                BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
528     }
529     return sum;
530 }
531
532 static int hadamard8_intra8x8_c(MpegEncContext *s, uint8_t *src,
533                                 uint8_t *dummy, int stride, int h)
534 {
535     int i, temp[64], sum = 0;
536
537     av_assert2(h == 8);
538
539     for (i = 0; i < 8; i++) {
540         // FIXME: try pointer walks
541         BUTTERFLY2(temp[8 * i + 0], temp[8 * i + 1],
542                    src[stride * i + 0], src[stride * i + 1]);
543         BUTTERFLY2(temp[8 * i + 2], temp[8 * i + 3],
544                    src[stride * i + 2], src[stride * i + 3]);
545         BUTTERFLY2(temp[8 * i + 4], temp[8 * i + 5],
546                    src[stride * i + 4], src[stride * i + 5]);
547         BUTTERFLY2(temp[8 * i + 6], temp[8 * i + 7],
548                    src[stride * i + 6], src[stride * i + 7]);
549
550         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 2]);
551         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 3]);
552         BUTTERFLY1(temp[8 * i + 4], temp[8 * i + 6]);
553         BUTTERFLY1(temp[8 * i + 5], temp[8 * i + 7]);
554
555         BUTTERFLY1(temp[8 * i + 0], temp[8 * i + 4]);
556         BUTTERFLY1(temp[8 * i + 1], temp[8 * i + 5]);
557         BUTTERFLY1(temp[8 * i + 2], temp[8 * i + 6]);
558         BUTTERFLY1(temp[8 * i + 3], temp[8 * i + 7]);
559     }
560
561     for (i = 0; i < 8; i++) {
562         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 1 + i]);
563         BUTTERFLY1(temp[8 * 2 + i], temp[8 * 3 + i]);
564         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 5 + i]);
565         BUTTERFLY1(temp[8 * 6 + i], temp[8 * 7 + i]);
566
567         BUTTERFLY1(temp[8 * 0 + i], temp[8 * 2 + i]);
568         BUTTERFLY1(temp[8 * 1 + i], temp[8 * 3 + i]);
569         BUTTERFLY1(temp[8 * 4 + i], temp[8 * 6 + i]);
570         BUTTERFLY1(temp[8 * 5 + i], temp[8 * 7 + i]);
571
572         sum +=
573             BUTTERFLYA(temp[8 * 0 + i], temp[8 * 4 + i])
574             + BUTTERFLYA(temp[8 * 1 + i], temp[8 * 5 + i])
575             + BUTTERFLYA(temp[8 * 2 + i], temp[8 * 6 + i])
576             + BUTTERFLYA(temp[8 * 3 + i], temp[8 * 7 + i]);
577     }
578
579     sum -= FFABS(temp[8 * 0] + temp[8 * 4]); // -mean
580
581     return sum;
582 }
583
584 static int dct_sad8x8_c(MpegEncContext *s, uint8_t *src1,
585                         uint8_t *src2, int stride, int h)
586 {
587     LOCAL_ALIGNED_16(int16_t, temp, [64]);
588
589     av_assert2(h == 8);
590
591     s->dsp.diff_pixels(temp, src1, src2, stride);
592     s->dsp.fdct(temp);
593     return s->dsp.sum_abs_dctelem(temp);
594 }
595
596 #if CONFIG_GPL
597 #define DCT8_1D                                         \
598     {                                                   \
599         const int s07 = SRC(0) + SRC(7);                \
600         const int s16 = SRC(1) + SRC(6);                \
601         const int s25 = SRC(2) + SRC(5);                \
602         const int s34 = SRC(3) + SRC(4);                \
603         const int a0  = s07 + s34;                      \
604         const int a1  = s16 + s25;                      \
605         const int a2  = s07 - s34;                      \
606         const int a3  = s16 - s25;                      \
607         const int d07 = SRC(0) - SRC(7);                \
608         const int d16 = SRC(1) - SRC(6);                \
609         const int d25 = SRC(2) - SRC(5);                \
610         const int d34 = SRC(3) - SRC(4);                \
611         const int a4  = d16 + d25 + (d07 + (d07 >> 1)); \
612         const int a5  = d07 - d34 - (d25 + (d25 >> 1)); \
613         const int a6  = d07 + d34 - (d16 + (d16 >> 1)); \
614         const int a7  = d16 - d25 + (d34 + (d34 >> 1)); \
615         DST(0, a0 + a1);                                \
616         DST(1, a4 + (a7 >> 2));                         \
617         DST(2, a2 + (a3 >> 1));                         \
618         DST(3, a5 + (a6 >> 2));                         \
619         DST(4, a0 - a1);                                \
620         DST(5, a6 - (a5 >> 2));                         \
621         DST(6, (a2 >> 1) - a3);                         \
622         DST(7, (a4 >> 2) - a7);                         \
623     }
624
625 static int dct264_sad8x8_c(MpegEncContext *s, uint8_t *src1,
626                            uint8_t *src2, int stride, int h)
627 {
628     int16_t dct[8][8];
629     int i, sum = 0;
630
631     s->dsp.diff_pixels(dct[0], src1, src2, stride);
632
633 #define SRC(x) dct[i][x]
634 #define DST(x, v) dct[i][x] = v
635     for (i = 0; i < 8; i++)
636         DCT8_1D
637 #undef SRC
638 #undef DST
639
640 #define SRC(x) dct[x][i]
641 #define DST(x, v) sum += FFABS(v)
642         for (i = 0; i < 8; i++)
643             DCT8_1D
644 #undef SRC
645 #undef DST
646             return sum;
647 }
648 #endif
649
650 static int dct_max8x8_c(MpegEncContext *s, uint8_t *src1,
651                         uint8_t *src2, int stride, int h)
652 {
653     LOCAL_ALIGNED_16(int16_t, temp, [64]);
654     int sum = 0, i;
655
656     av_assert2(h == 8);
657
658     s->dsp.diff_pixels(temp, src1, src2, stride);
659     s->dsp.fdct(temp);
660
661     for (i = 0; i < 64; i++)
662         sum = FFMAX(sum, FFABS(temp[i]));
663
664     return sum;
665 }
666
667 static int quant_psnr8x8_c(MpegEncContext *s, uint8_t *src1,
668                            uint8_t *src2, int stride, int h)
669 {
670     LOCAL_ALIGNED_16(int16_t, temp, [64 * 2]);
671     int16_t *const bak = temp + 64;
672     int sum = 0, i;
673
674     av_assert2(h == 8);
675     s->mb_intra = 0;
676
677     s->dsp.diff_pixels(temp, src1, src2, stride);
678
679     memcpy(bak, temp, 64 * sizeof(int16_t));
680
681     s->block_last_index[0 /* FIXME */] =
682         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
683     s->dct_unquantize_inter(s, temp, 0, s->qscale);
684     ff_simple_idct_8(temp); // FIXME
685
686     for (i = 0; i < 64; i++)
687         sum += (temp[i] - bak[i]) * (temp[i] - bak[i]);
688
689     return sum;
690 }
691
692 static int rd8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
693                    int stride, int h)
694 {
695     const uint8_t *scantable = s->intra_scantable.permutated;
696     LOCAL_ALIGNED_16(int16_t, temp, [64]);
697     LOCAL_ALIGNED_16(uint8_t, lsrc1, [64]);
698     LOCAL_ALIGNED_16(uint8_t, lsrc2, [64]);
699     int i, last, run, bits, level, distortion, start_i;
700     const int esc_length = s->ac_esc_length;
701     uint8_t *length, *last_length;
702
703     av_assert2(h == 8);
704
705     copy_block8(lsrc1, src1, 8, stride, 8);
706     copy_block8(lsrc2, src2, 8, stride, 8);
707
708     s->dsp.diff_pixels(temp, lsrc1, lsrc2, 8);
709
710     s->block_last_index[0 /* FIXME */] =
711     last                               =
712         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
713
714     bits = 0;
715
716     if (s->mb_intra) {
717         start_i     = 1;
718         length      = s->intra_ac_vlc_length;
719         last_length = s->intra_ac_vlc_last_length;
720         bits       += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
721     } else {
722         start_i     = 0;
723         length      = s->inter_ac_vlc_length;
724         last_length = s->inter_ac_vlc_last_length;
725     }
726
727     if (last >= start_i) {
728         run = 0;
729         for (i = start_i; i < last; i++) {
730             int j = scantable[i];
731             level = temp[j];
732
733             if (level) {
734                 level += 64;
735                 if ((level & (~127)) == 0)
736                     bits += length[UNI_AC_ENC_INDEX(run, level)];
737                 else
738                     bits += esc_length;
739                 run = 0;
740             } else
741                 run++;
742         }
743         i = scantable[last];
744
745         level = temp[i] + 64;
746
747         av_assert2(level - 64);
748
749         if ((level & (~127)) == 0) {
750             bits += last_length[UNI_AC_ENC_INDEX(run, level)];
751         } else
752             bits += esc_length;
753     }
754
755     if (last >= 0) {
756         if (s->mb_intra)
757             s->dct_unquantize_intra(s, temp, 0, s->qscale);
758         else
759             s->dct_unquantize_inter(s, temp, 0, s->qscale);
760     }
761
762     s->idsp.idct_add(lsrc2, 8, temp);
763
764     distortion = s->dsp.sse[1](NULL, lsrc2, lsrc1, 8, 8);
765
766     return distortion + ((bits * s->qscale * s->qscale * 109 + 64) >> 7);
767 }
768
769 static int bit8x8_c(MpegEncContext *s, uint8_t *src1, uint8_t *src2,
770                     int stride, int h)
771 {
772     const uint8_t *scantable = s->intra_scantable.permutated;
773     LOCAL_ALIGNED_16(int16_t, temp, [64]);
774     int i, last, run, bits, level, start_i;
775     const int esc_length = s->ac_esc_length;
776     uint8_t *length, *last_length;
777
778     av_assert2(h == 8);
779
780     s->dsp.diff_pixels(temp, src1, src2, stride);
781
782     s->block_last_index[0 /* FIXME */] =
783     last                               =
784         s->fast_dct_quantize(s, temp, 0 /* FIXME */, s->qscale, &i);
785
786     bits = 0;
787
788     if (s->mb_intra) {
789         start_i     = 1;
790         length      = s->intra_ac_vlc_length;
791         last_length = s->intra_ac_vlc_last_length;
792         bits       += s->luma_dc_vlc_length[temp[0] + 256]; // FIXME: chroma
793     } else {
794         start_i     = 0;
795         length      = s->inter_ac_vlc_length;
796         last_length = s->inter_ac_vlc_last_length;
797     }
798
799     if (last >= start_i) {
800         run = 0;
801         for (i = start_i; i < last; i++) {
802             int j = scantable[i];
803             level = temp[j];
804
805             if (level) {
806                 level += 64;
807                 if ((level & (~127)) == 0)
808                     bits += length[UNI_AC_ENC_INDEX(run, level)];
809                 else
810                     bits += esc_length;
811                 run = 0;
812             } else
813                 run++;
814         }
815         i = scantable[last];
816
817         level = temp[i] + 64;
818
819         av_assert2(level - 64);
820
821         if ((level & (~127)) == 0)
822             bits += last_length[UNI_AC_ENC_INDEX(run, level)];
823         else
824             bits += esc_length;
825     }
826
827     return bits;
828 }
829
830 #define VSAD_INTRA(size)                                                \
831 static int vsad_intra ## size ## _c(MpegEncContext *c,                  \
832                                     uint8_t *s, uint8_t *dummy,         \
833                                     int stride, int h)                  \
834 {                                                                       \
835     int score = 0, x, y;                                                \
836                                                                         \
837     for (y = 1; y < h; y++) {                                           \
838         for (x = 0; x < size; x += 4) {                                 \
839             score += FFABS(s[x]     - s[x + stride])     +              \
840                      FFABS(s[x + 1] - s[x + stride + 1]) +              \
841                      FFABS(s[x + 2] - s[x + 2 + stride]) +              \
842                      FFABS(s[x + 3] - s[x + 3 + stride]);               \
843         }                                                               \
844         s += stride;                                                    \
845     }                                                                   \
846                                                                         \
847     return score;                                                       \
848 }
849 VSAD_INTRA(8)
850 VSAD_INTRA(16)
851
852 #define VSAD(size)                                                             \
853 static int vsad ## size ## _c(MpegEncContext *c,                               \
854                               uint8_t *s1, uint8_t *s2,                        \
855                               int stride, int h)                               \
856 {                                                                              \
857     int score = 0, x, y;                                                       \
858                                                                                \
859     for (y = 1; y < h; y++) {                                                  \
860         for (x = 0; x < size; x++)                                             \
861             score += FFABS(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);   \
862         s1 += stride;                                                          \
863         s2 += stride;                                                          \
864     }                                                                          \
865                                                                                \
866     return score;                                                              \
867 }
868 VSAD(8)
869 VSAD(16)
870
871 #define SQ(a) ((a) * (a))
872 #define VSSE_INTRA(size)                                                \
873 static int vsse_intra ## size ## _c(MpegEncContext *c,                  \
874                                     uint8_t *s, uint8_t *dummy,         \
875                                     int stride, int h)                  \
876 {                                                                       \
877     int score = 0, x, y;                                                \
878                                                                         \
879     for (y = 1; y < h; y++) {                                           \
880         for (x = 0; x < size; x += 4) {                                 \
881             score += SQ(s[x]     - s[x + stride]) +                     \
882                      SQ(s[x + 1] - s[x + stride + 1]) +                 \
883                      SQ(s[x + 2] - s[x + stride + 2]) +                 \
884                      SQ(s[x + 3] - s[x + stride + 3]);                  \
885         }                                                               \
886         s += stride;                                                    \
887     }                                                                   \
888                                                                         \
889     return score;                                                       \
890 }
891 VSSE_INTRA(8)
892 VSSE_INTRA(16)
893
894 #define VSSE(size)                                                             \
895 static int vsse ## size ## _c(MpegEncContext *c, uint8_t *s1, uint8_t *s2,     \
896                     int stride, int h)                                         \
897 {                                                                              \
898     int score = 0, x, y;                                                       \
899                                                                                \
900     for (y = 1; y < h; y++) {                                                  \
901         for (x = 0; x < size; x++)                                             \
902             score += SQ(s1[x] - s2[x] - s1[x + stride] + s2[x + stride]);      \
903         s1 += stride;                                                          \
904         s2 += stride;                                                          \
905     }                                                                          \
906                                                                                \
907     return score;                                                              \
908 }
909 VSSE(8)
910 VSSE(16)
911
912 #define WRAPPER8_16_SQ(name8, name16)                                   \
913 static int name16(MpegEncContext *s, uint8_t *dst, uint8_t *src,        \
914                   int stride, int h)                                    \
915 {                                                                       \
916     int score = 0;                                                      \
917                                                                         \
918     score += name8(s, dst, src, stride, 8);                             \
919     score += name8(s, dst + 8, src + 8, stride, 8);                     \
920     if (h == 16) {                                                      \
921         dst   += 8 * stride;                                            \
922         src   += 8 * stride;                                            \
923         score += name8(s, dst, src, stride, 8);                         \
924         score += name8(s, dst + 8, src + 8, stride, 8);                 \
925     }                                                                   \
926     return score;                                                       \
927 }
928
929 WRAPPER8_16_SQ(hadamard8_diff8x8_c, hadamard8_diff16_c)
930 WRAPPER8_16_SQ(hadamard8_intra8x8_c, hadamard8_intra16_c)
931 WRAPPER8_16_SQ(dct_sad8x8_c, dct_sad16_c)
932 #if CONFIG_GPL
933 WRAPPER8_16_SQ(dct264_sad8x8_c, dct264_sad16_c)
934 #endif
935 WRAPPER8_16_SQ(dct_max8x8_c, dct_max16_c)
936 WRAPPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c)
937 WRAPPER8_16_SQ(rd8x8_c, rd16_c)
938 WRAPPER8_16_SQ(bit8x8_c, bit16_c)
939
940 /* draw the edges of width 'w' of an image of size width, height */
941 // FIXME: Check that this is OK for MPEG-4 interlaced.
942 static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
943                            int w, int h, int sides)
944 {
945     uint8_t *ptr = buf, *last_line;
946     int i;
947
948     /* left and right */
949     for (i = 0; i < height; i++) {
950         memset(ptr - w, ptr[0], w);
951         memset(ptr + width, ptr[width - 1], w);
952         ptr += wrap;
953     }
954
955     /* top and bottom + corners */
956     buf -= w;
957     last_line = buf + (height - 1) * wrap;
958     if (sides & EDGE_TOP)
959         for (i = 0; i < h; i++)
960             // top
961             memcpy(buf - (i + 1) * wrap, buf, width + w + w);
962     if (sides & EDGE_BOTTOM)
963         for (i = 0; i < h; i++)
964             // bottom
965             memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
966 }
967
968 /* init static data */
969 av_cold void ff_dsputil_static_init(void)
970 {
971     int i;
972
973     for (i = 0; i < 512; i++)
974         ff_square_tab[i] = (i - 256) * (i - 256);
975 }
976
977 int ff_check_alignment(void)
978 {
979     static int did_fail = 0;
980     LOCAL_ALIGNED_16(int, aligned, [4]);
981
982     if ((intptr_t)aligned & 15) {
983         if (!did_fail) {
984 #if HAVE_MMX || HAVE_ALTIVEC
985             av_log(NULL, AV_LOG_ERROR,
986                 "Compiler did not align stack variables. Libavcodec has been miscompiled\n"
987                 "and may be very slow or crash. This is not a bug in libavcodec,\n"
988                 "but in the compiler. You may try recompiling using gcc >= 4.2.\n"
989                 "Do not report crashes to FFmpeg developers.\n");
990 #endif
991             did_fail=1;
992         }
993         return -1;
994     }
995     return 0;
996 }
997
998 av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
999 {
1000     const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
1001
1002     ff_check_alignment();
1003
1004 #if CONFIG_ENCODERS
1005     if (avctx->bits_per_raw_sample == 10) {
1006         c->fdct    = ff_jpeg_fdct_islow_10;
1007         c->fdct248 = ff_fdct248_islow_10;
1008     } else {
1009         if (avctx->dct_algo == FF_DCT_FASTINT) {
1010             c->fdct    = ff_fdct_ifast;
1011             c->fdct248 = ff_fdct_ifast248;
1012         } else if (avctx->dct_algo == FF_DCT_FAAN) {
1013             c->fdct    = ff_faandct;
1014             c->fdct248 = ff_faandct248;
1015         } else {
1016             c->fdct    = ff_jpeg_fdct_islow_8; // slow/accurate/default
1017             c->fdct248 = ff_fdct248_islow_8;
1018         }
1019     }
1020 #endif /* CONFIG_ENCODERS */
1021
1022     c->diff_pixels = diff_pixels_c;
1023
1024     c->sum_abs_dctelem = sum_abs_dctelem_c;
1025
1026     /* TODO [0] 16  [1] 8 */
1027     c->pix_abs[0][0] = pix_abs16_c;
1028     c->pix_abs[0][1] = pix_abs16_x2_c;
1029     c->pix_abs[0][2] = pix_abs16_y2_c;
1030     c->pix_abs[0][3] = pix_abs16_xy2_c;
1031     c->pix_abs[1][0] = pix_abs8_c;
1032     c->pix_abs[1][1] = pix_abs8_x2_c;
1033     c->pix_abs[1][2] = pix_abs8_y2_c;
1034     c->pix_abs[1][3] = pix_abs8_xy2_c;
1035
1036 #define SET_CMP_FUNC(name)                      \
1037     c->name[0] = name ## 16_c;                  \
1038     c->name[1] = name ## 8x8_c;
1039
1040     SET_CMP_FUNC(hadamard8_diff)
1041     c->hadamard8_diff[4] = hadamard8_intra16_c;
1042     c->hadamard8_diff[5] = hadamard8_intra8x8_c;
1043     SET_CMP_FUNC(dct_sad)
1044     SET_CMP_FUNC(dct_max)
1045 #if CONFIG_GPL
1046     SET_CMP_FUNC(dct264_sad)
1047 #endif
1048     c->sad[0] = pix_abs16_c;
1049     c->sad[1] = pix_abs8_c;
1050     c->sse[0] = sse16_c;
1051     c->sse[1] = sse8_c;
1052     c->sse[2] = sse4_c;
1053     SET_CMP_FUNC(quant_psnr)
1054     SET_CMP_FUNC(rd)
1055     SET_CMP_FUNC(bit)
1056     c->vsad[0] = vsad16_c;
1057     c->vsad[1] = vsad8_c;
1058     c->vsad[4] = vsad_intra16_c;
1059     c->vsad[5] = vsad_intra8_c;
1060     c->vsse[0] = vsse16_c;
1061     c->vsse[1] = vsse8_c;
1062     c->vsse[4] = vsse_intra16_c;
1063     c->vsse[5] = vsse_intra8_c;
1064     c->nsse[0] = nsse16_c;
1065     c->nsse[1] = nsse8_c;
1066 #if CONFIG_SNOW_DECODER || CONFIG_SNOW_ENCODER
1067     ff_dsputil_init_dwt(c);
1068 #endif
1069
1070     c->draw_edges = draw_edges_8_c;
1071
1072     switch (avctx->bits_per_raw_sample) {
1073     case 9:
1074     case 10:
1075     case 12:
1076     case 14:
1077         c->get_pixels = get_pixels_16_c;
1078         break;
1079     default:
1080         if (avctx->bits_per_raw_sample<=8 || avctx->codec_type != AVMEDIA_TYPE_VIDEO) {
1081             c->get_pixels = get_pixels_8_c;
1082         }
1083         break;
1084     }
1085
1086
1087     if (ARCH_ALPHA)
1088         ff_dsputil_init_alpha(c, avctx);
1089     if (ARCH_ARM)
1090         ff_dsputil_init_arm(c, avctx, high_bit_depth);
1091     if (ARCH_PPC)
1092         ff_dsputil_init_ppc(c, avctx, high_bit_depth);
1093     if (ARCH_X86)
1094         ff_dsputil_init_x86(c, avctx, high_bit_depth);
1095 }
1096
1097 av_cold void dsputil_init(DSPContext* c, AVCodecContext *avctx)
1098 {
1099     ff_dsputil_init(c, avctx);
1100 }
1101
1102 av_cold void avpriv_dsputil_init(DSPContext *c, AVCodecContext *avctx)
1103 {
1104     ff_dsputil_init(c, avctx);
1105 }