9811df73e3782d42b3702dea34f14bc2cd74489d
[ffmpeg.git] / libavcodec / dsputil.c
1 /*
2  * DSP utils
3  * Copyright (c) 2000, 2001 Gerard Lantau.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include "avcodec.h"
22 #include "dsputil.h"
23 #include "simple_idct.h"
24
25 void (*ff_idct)(DCTELEM *block);
26 void (*get_pixels)(DCTELEM *block, const UINT8 *pixels, int line_size);
27 void (*put_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
28 void (*add_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
29
30 op_pixels_abs_func pix_abs16x16;
31 op_pixels_abs_func pix_abs16x16_x2;
32 op_pixels_abs_func pix_abs16x16_y2;
33 op_pixels_abs_func pix_abs16x16_xy2;
34
35 UINT8 cropTbl[256 + 2 * MAX_NEG_CROP];
36 UINT32 squareTbl[512];
37
38 extern UINT16 default_intra_matrix[64];
39 extern UINT16 default_non_intra_matrix[64];
40
41 UINT8 zigzag_direct[64] = {
42     0, 1, 8, 16, 9, 2, 3, 10,
43     17, 24, 32, 25, 18, 11, 4, 5,
44     12, 19, 26, 33, 40, 48, 41, 34,
45     27, 20, 13, 6, 7, 14, 21, 28,
46     35, 42, 49, 56, 57, 50, 43, 36,
47     29, 22, 15, 23, 30, 37, 44, 51,
48     58, 59, 52, 45, 38, 31, 39, 46,
49     53, 60, 61, 54, 47, 55, 62, 63
50 };
51
52 UINT8 ff_alternate_horizontal_scan[64] = {
53     0,  1,  2,  3,  8,  9, 16, 17, 
54     10, 11,  4,  5,  6,  7, 15, 14,
55     13, 12, 19, 18, 24, 25, 32, 33, 
56     26, 27, 20, 21, 22, 23, 28, 29,
57     30, 31, 34, 35, 40, 41, 48, 49, 
58     42, 43, 36, 37, 38, 39, 44, 45,
59     46, 47, 50, 51, 56, 57, 58, 59, 
60     52, 53, 54, 55, 60, 61, 62, 63,
61 };
62
63 UINT8 ff_alternate_vertical_scan[64] = {
64     0,  8, 16, 24,  1,  9,  2, 10, 
65     17, 25, 32, 40, 48, 56, 57, 49,
66     41, 33, 26, 18,  3, 11,  4, 12, 
67     19, 27, 34, 42, 50, 58, 35, 43,
68     51, 59, 20, 28,  5, 13,  6, 14, 
69     21, 29, 36, 44, 52, 60, 37, 45,
70     53, 61, 22, 30,  7, 15, 23, 31, 
71     38, 46, 54, 62, 39, 47, 55, 63,
72 };
73
74
75 static UINT8 simple_mmx_permutation[64]={
76         0x00, 0x08, 0x01, 0x09, 0x04, 0x0C, 0x05, 0x0D,
77         0x10, 0x18, 0x11, 0x19, 0x14, 0x1C, 0x15, 0x1D,
78         0x02, 0x0A, 0x03, 0x0B, 0x06, 0x0E, 0x07, 0x0F,
79         0x12, 0x1A, 0x13, 0x1B, 0x16, 0x1E, 0x17, 0x1F,
80         0x20, 0x28, 0x21, 0x29, 0x24, 0x2C, 0x25, 0x2D,
81         0x30, 0x38, 0x31, 0x39, 0x34, 0x3C, 0x35, 0x3D,
82         0x22, 0x2A, 0x23, 0x2B, 0x26, 0x2E, 0x27, 0x2F,
83         0x32, 0x3A, 0x33, 0x3B, 0x36, 0x3E, 0x37, 0x3F,
84 };
85
86 UINT8 permutation[64];
87 //UINT8 invPermutation[64];
88
89 void get_pixels_c(DCTELEM *block, const UINT8 *pixels, int line_size)
90 {
91     DCTELEM *p;
92     const UINT8 *pix;
93     int i;
94
95     /* read the pixels */
96     p = block;
97     pix = pixels;
98     for(i=0;i<8;i++) {
99         p[0] = pix[0];
100         p[1] = pix[1];
101         p[2] = pix[2];
102         p[3] = pix[3];
103         p[4] = pix[4];
104         p[5] = pix[5];
105         p[6] = pix[6];
106         p[7] = pix[7];
107         pix += line_size;
108         p += 8;
109     }
110 }
111
112 void put_pixels_clamped_c(const DCTELEM *block, UINT8 *pixels, int line_size)
113 {
114     const DCTELEM *p;
115     UINT8 *pix;
116     int i;
117     UINT8 *cm = cropTbl + MAX_NEG_CROP;
118     
119     /* read the pixels */
120     p = block;
121     pix = pixels;
122     for(i=0;i<8;i++) {
123         pix[0] = cm[p[0]];
124         pix[1] = cm[p[1]];
125         pix[2] = cm[p[2]];
126         pix[3] = cm[p[3]];
127         pix[4] = cm[p[4]];
128         pix[5] = cm[p[5]];
129         pix[6] = cm[p[6]];
130         pix[7] = cm[p[7]];
131         pix += line_size;
132         p += 8;
133     }
134 }
135
136 void add_pixels_clamped_c(const DCTELEM *block, UINT8 *pixels, int line_size)
137 {
138     const DCTELEM *p;
139     UINT8 *pix;
140     int i;
141     UINT8 *cm = cropTbl + MAX_NEG_CROP;
142     
143     /* read the pixels */
144     p = block;
145     pix = pixels;
146     for(i=0;i<8;i++) {
147         pix[0] = cm[pix[0] + p[0]];
148         pix[1] = cm[pix[1] + p[1]];
149         pix[2] = cm[pix[2] + p[2]];
150         pix[3] = cm[pix[3] + p[3]];
151         pix[4] = cm[pix[4] + p[4]];
152         pix[5] = cm[pix[5] + p[5]];
153         pix[6] = cm[pix[6] + p[6]];
154         pix[7] = cm[pix[7] + p[7]];
155         pix += line_size;
156         p += 8;
157     }
158 }
159
160 #define PIXOP(BTYPE, OPNAME, OP, INCR)                                                   \
161                                                                                          \
162 static void OPNAME ## _pixels(BTYPE *block, const UINT8 *pixels, int line_size, int h)    \
163 {                                                                                        \
164     BTYPE *p;                                                                            \
165     const UINT8 *pix;                                                                    \
166                                                                                          \
167     p = block;                                                                           \
168     pix = pixels;                                                                        \
169     do {                                                                                 \
170         OP(p[0], pix[0]);                                                                  \
171         OP(p[1], pix[1]);                                                                  \
172         OP(p[2], pix[2]);                                                                  \
173         OP(p[3], pix[3]);                                                                  \
174         OP(p[4], pix[4]);                                                                  \
175         OP(p[5], pix[5]);                                                                  \
176         OP(p[6], pix[6]);                                                                  \
177         OP(p[7], pix[7]);                                                                  \
178         pix += line_size;                                                                \
179         p += INCR;                                                                       \
180     } while (--h);;                                                                       \
181 }                                                                                        \
182                                                                                          \
183 static void OPNAME ## _pixels_x2(BTYPE *block, const UINT8 *pixels, int line_size, int h)     \
184 {                                                                                        \
185     BTYPE *p;                                                                          \
186     const UINT8 *pix;                                                                    \
187                                                                                          \
188     p = block;                                                                           \
189     pix = pixels;                                                                        \
190     do {                                                                   \
191         OP(p[0], avg2(pix[0], pix[1]));                                                    \
192         OP(p[1], avg2(pix[1], pix[2]));                                                    \
193         OP(p[2], avg2(pix[2], pix[3]));                                                    \
194         OP(p[3], avg2(pix[3], pix[4]));                                                    \
195         OP(p[4], avg2(pix[4], pix[5]));                                                    \
196         OP(p[5], avg2(pix[5], pix[6]));                                                    \
197         OP(p[6], avg2(pix[6], pix[7]));                                                    \
198         OP(p[7], avg2(pix[7], pix[8]));                                                    \
199         pix += line_size;                                                                \
200         p += INCR;                                                                       \
201     } while (--h);                                                                        \
202 }                                                                                        \
203                                                                                          \
204 static void OPNAME ## _pixels_y2(BTYPE *block, const UINT8 *pixels, int line_size, int h)     \
205 {                                                                                        \
206     BTYPE *p;                                                                          \
207     const UINT8 *pix;                                                                    \
208     const UINT8 *pix1;                                                                   \
209                                                                                          \
210     p = block;                                                                           \
211     pix = pixels;                                                                        \
212     pix1 = pixels + line_size;                                                           \
213     do {                                                                                 \
214         OP(p[0], avg2(pix[0], pix1[0]));                                                   \
215         OP(p[1], avg2(pix[1], pix1[1]));                                                   \
216         OP(p[2], avg2(pix[2], pix1[2]));                                                   \
217         OP(p[3], avg2(pix[3], pix1[3]));                                                   \
218         OP(p[4], avg2(pix[4], pix1[4]));                                                   \
219         OP(p[5], avg2(pix[5], pix1[5]));                                                   \
220         OP(p[6], avg2(pix[6], pix1[6]));                                                   \
221         OP(p[7], avg2(pix[7], pix1[7]));                                                   \
222         pix += line_size;                                                                \
223         pix1 += line_size;                                                               \
224         p += INCR;                                                                       \
225     } while(--h);                                                                         \
226 }                                                                                        \
227                                                                                          \
228 static void OPNAME ## _pixels_xy2(BTYPE *block, const UINT8 *pixels, int line_size, int h)    \
229 {                                                                                        \
230     BTYPE *p;                                                                          \
231     const UINT8 *pix;                                                                    \
232     const UINT8 *pix1;                                                                   \
233                                                                                          \
234     p = block;                                                                           \
235     pix = pixels;                                                                        \
236     pix1 = pixels + line_size;                                                           \
237     do {                                                                   \
238         OP(p[0], avg4(pix[0], pix[1], pix1[0], pix1[1]));                                  \
239         OP(p[1], avg4(pix[1], pix[2], pix1[1], pix1[2]));                                  \
240         OP(p[2], avg4(pix[2], pix[3], pix1[2], pix1[3]));                                  \
241         OP(p[3], avg4(pix[3], pix[4], pix1[3], pix1[4]));                                  \
242         OP(p[4], avg4(pix[4], pix[5], pix1[4], pix1[5]));                                  \
243         OP(p[5], avg4(pix[5], pix[6], pix1[5], pix1[6]));                                  \
244         OP(p[6], avg4(pix[6], pix[7], pix1[6], pix1[7]));                                  \
245         OP(p[7], avg4(pix[7], pix[8], pix1[7], pix1[8]));                                  \
246         pix += line_size;                                                                \
247         pix1 += line_size;                                                               \
248         p += INCR;                                                                       \
249     } while(--h);                                                                         \
250 }                                                                                        \
251                                                                                          \
252 void (*OPNAME ## _pixels_tab[4])(BTYPE *block, const UINT8 *pixels, int line_size, int h) = { \
253     OPNAME ## _pixels,                                                                   \
254     OPNAME ## _pixels_x2,                                                                \
255     OPNAME ## _pixels_y2,                                                                \
256     OPNAME ## _pixels_xy2,                                                               \
257 };
258
259
260 /* rounding primitives */
261 #define avg2(a,b) ((a+b+1)>>1)
262 #define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
263
264 #define op_put(a, b) a = b
265 #define op_avg(a, b) a = avg2(a, b)
266 #define op_sub(a, b) a -= b
267
268 PIXOP(UINT8, put, op_put, line_size)
269 PIXOP(UINT8, avg, op_avg, line_size)
270
271 PIXOP(DCTELEM, sub, op_sub, 8)
272
273 /* not rounding primitives */
274 #undef avg2
275 #undef avg4
276 #define avg2(a,b) ((a+b)>>1)
277 #define avg4(a,b,c,d) ((a+b+c+d+1)>>2)
278
279 PIXOP(UINT8, put_no_rnd, op_put, line_size)
280 PIXOP(UINT8, avg_no_rnd, op_avg, line_size)
281
282 /* motion estimation */
283
284 #undef avg2
285 #undef avg4
286 #define avg2(a,b) ((a+b+1)>>1)
287 #define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
288
289 int pix_abs16x16_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
290 {
291     int s, i;
292
293     s = 0;
294     for(i=0;i<h;i++) {
295         s += abs(pix1[0] - pix2[0]);
296         s += abs(pix1[1] - pix2[1]);
297         s += abs(pix1[2] - pix2[2]);
298         s += abs(pix1[3] - pix2[3]);
299         s += abs(pix1[4] - pix2[4]);
300         s += abs(pix1[5] - pix2[5]);
301         s += abs(pix1[6] - pix2[6]);
302         s += abs(pix1[7] - pix2[7]);
303         s += abs(pix1[8] - pix2[8]);
304         s += abs(pix1[9] - pix2[9]);
305         s += abs(pix1[10] - pix2[10]);
306         s += abs(pix1[11] - pix2[11]);
307         s += abs(pix1[12] - pix2[12]);
308         s += abs(pix1[13] - pix2[13]);
309         s += abs(pix1[14] - pix2[14]);
310         s += abs(pix1[15] - pix2[15]);
311         pix1 += line_size;
312         pix2 += line_size;
313     }
314     return s;
315 }
316
317 int pix_abs16x16_x2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
318 {
319     int s, i;
320
321     s = 0;
322     for(i=0;i<h;i++) {
323         s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
324         s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
325         s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
326         s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
327         s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
328         s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
329         s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
330         s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
331         s += abs(pix1[8] - avg2(pix2[8], pix2[9]));
332         s += abs(pix1[9] - avg2(pix2[9], pix2[10]));
333         s += abs(pix1[10] - avg2(pix2[10], pix2[11]));
334         s += abs(pix1[11] - avg2(pix2[11], pix2[12]));
335         s += abs(pix1[12] - avg2(pix2[12], pix2[13]));
336         s += abs(pix1[13] - avg2(pix2[13], pix2[14]));
337         s += abs(pix1[14] - avg2(pix2[14], pix2[15]));
338         s += abs(pix1[15] - avg2(pix2[15], pix2[16]));
339         pix1 += line_size;
340         pix2 += line_size;
341     }
342     return s;
343 }
344
345 int pix_abs16x16_y2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
346 {
347     int s, i;
348     UINT8 *pix3 = pix2 + line_size;
349
350     s = 0;
351     for(i=0;i<h;i++) {
352         s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
353         s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
354         s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
355         s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
356         s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
357         s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
358         s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
359         s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
360         s += abs(pix1[8] - avg2(pix2[8], pix3[8]));
361         s += abs(pix1[9] - avg2(pix2[9], pix3[9]));
362         s += abs(pix1[10] - avg2(pix2[10], pix3[10]));
363         s += abs(pix1[11] - avg2(pix2[11], pix3[11]));
364         s += abs(pix1[12] - avg2(pix2[12], pix3[12]));
365         s += abs(pix1[13] - avg2(pix2[13], pix3[13]));
366         s += abs(pix1[14] - avg2(pix2[14], pix3[14]));
367         s += abs(pix1[15] - avg2(pix2[15], pix3[15]));
368         pix1 += line_size;
369         pix2 += line_size;
370         pix3 += line_size;
371     }
372     return s;
373 }
374
375 int pix_abs16x16_xy2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
376 {
377     int s, i;
378     UINT8 *pix3 = pix2 + line_size;
379
380     s = 0;
381     for(i=0;i<h;i++) {
382         s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
383         s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
384         s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
385         s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
386         s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
387         s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
388         s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
389         s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
390         s += abs(pix1[8] - avg4(pix2[8], pix2[9], pix3[8], pix3[9]));
391         s += abs(pix1[9] - avg4(pix2[9], pix2[10], pix3[9], pix3[10]));
392         s += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
393         s += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
394         s += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
395         s += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
396         s += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
397         s += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
398         pix1 += line_size;
399         pix2 += line_size;
400         pix3 += line_size;
401     }
402     return s;
403 }
404
405 /* permute block according so that it corresponds to the MMX idct
406    order */
407 #ifdef SIMPLE_IDCT
408  /* general permutation, but perhaps slightly slower */
409 void block_permute(INT16 *block)
410 {
411         int i;
412         INT16 temp[64];
413
414         for(i=0; i<64; i++) temp[ block_permute_op(i) ] = block[i];
415
416         for(i=0; i<64; i++) block[i] = temp[i];
417 }
418 #else
419
420 void block_permute(INT16 *block)
421 {
422     int tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
423     int i;
424
425     for(i=0;i<8;i++) {
426         tmp1 = block[1];
427         tmp2 = block[2];
428         tmp3 = block[3];
429         tmp4 = block[4];
430         tmp5 = block[5];
431         tmp6 = block[6];
432         block[1] = tmp2;
433         block[2] = tmp4;
434         block[3] = tmp6;
435         block[4] = tmp1;
436         block[5] = tmp3;
437         block[6] = tmp5;
438         block += 8;
439     }
440 }
441 #endif
442
443 void dsputil_init(void)
444 {
445     int i, j;
446     int use_permuted_idct;
447
448     for(i=0;i<256;i++) cropTbl[i + MAX_NEG_CROP] = i;
449     for(i=0;i<MAX_NEG_CROP;i++) {
450         cropTbl[i] = 0;
451         cropTbl[i + MAX_NEG_CROP + 256] = 255;
452     }
453
454     for(i=0;i<512;i++) {
455         squareTbl[i] = (i - 256) * (i - 256);
456     }
457
458 #ifdef SIMPLE_IDCT
459     ff_idct = simple_idct;
460 #else
461     ff_idct = j_rev_dct;
462 #endif
463     get_pixels = get_pixels_c;
464     put_pixels_clamped = put_pixels_clamped_c;
465     add_pixels_clamped = add_pixels_clamped_c;
466
467     pix_abs16x16 = pix_abs16x16_c;
468     pix_abs16x16_x2 = pix_abs16x16_x2_c;
469     pix_abs16x16_y2 = pix_abs16x16_y2_c;
470     pix_abs16x16_xy2 = pix_abs16x16_xy2_c;
471     av_fdct = jpeg_fdct_ifast;
472
473     use_permuted_idct = 1;
474
475 #ifdef HAVE_MMX
476     dsputil_init_mmx();
477 #endif
478 #ifdef ARCH_ARMV4L
479     dsputil_init_armv4l();
480 #endif
481 #ifdef HAVE_MLIB
482     dsputil_init_mlib();
483     use_permuted_idct = 0;
484 #endif
485
486 #ifdef SIMPLE_IDCT
487     if(ff_idct == simple_idct) use_permuted_idct=0;
488 #endif
489
490     if(use_permuted_idct)
491 #ifdef SIMPLE_IDCT
492         for(i=0; i<64; i++) permutation[i]= simple_mmx_permutation[i];
493 #else
494         for(i=0; i<64; i++) permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
495 #endif
496     else
497         for(i=0; i<64; i++) permutation[i]=i;
498
499     if (use_permuted_idct) {
500         /* permute for IDCT */
501         for(i=0;i<64;i++) {
502             j = zigzag_direct[i];
503             zigzag_direct[i] = block_permute_op(j);
504             j = ff_alternate_horizontal_scan[i];
505             ff_alternate_horizontal_scan[i] = block_permute_op(j);
506             j = ff_alternate_vertical_scan[i];
507             ff_alternate_vertical_scan[i] = block_permute_op(j);
508         }
509         block_permute(default_intra_matrix);
510         block_permute(default_non_intra_matrix);
511     }
512 }