fix different UMV handling for mpeg4 vs. h263 - patch by Michael Niedermayer <michael...
[libav.git] / libavcodec / dsputil.c
CommitLineData
de6d9b64
FB
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"
d962f6fd 23#include "simple_idct.h"
de6d9b64 24
4af7bcc1 25void (*ff_idct)(DCTELEM *block);
de6d9b64
FB
26void (*get_pixels)(DCTELEM *block, const UINT8 *pixels, int line_size);
27void (*put_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
28void (*add_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
29
30op_pixels_abs_func pix_abs16x16;
31op_pixels_abs_func pix_abs16x16_x2;
32op_pixels_abs_func pix_abs16x16_y2;
33op_pixels_abs_func pix_abs16x16_xy2;
34
0cfa9713 35UINT8 cropTbl[256 + 2 * MAX_NEG_CROP];
de6d9b64
FB
36UINT32 squareTbl[512];
37
e0eac44e
FB
38extern UINT16 default_intra_matrix[64];
39extern UINT16 default_non_intra_matrix[64];
40
41UINT8 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
52UINT8 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
63UINT8 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
de6d9b64
FB
74void get_pixels_c(DCTELEM *block, const UINT8 *pixels, int line_size)
75{
76 DCTELEM *p;
77 const UINT8 *pix;
78 int i;
79
80 /* read the pixels */
81 p = block;
82 pix = pixels;
83 for(i=0;i<8;i++) {
84 p[0] = pix[0];
85 p[1] = pix[1];
86 p[2] = pix[2];
87 p[3] = pix[3];
88 p[4] = pix[4];
89 p[5] = pix[5];
90 p[6] = pix[6];
91 p[7] = pix[7];
92 pix += line_size;
93 p += 8;
94 }
95}
96
97void put_pixels_clamped_c(const DCTELEM *block, UINT8 *pixels, int line_size)
98{
99 const DCTELEM *p;
100 UINT8 *pix;
101 int i;
102 UINT8 *cm = cropTbl + MAX_NEG_CROP;
103
104 /* read the pixels */
105 p = block;
106 pix = pixels;
107 for(i=0;i<8;i++) {
108 pix[0] = cm[p[0]];
109 pix[1] = cm[p[1]];
110 pix[2] = cm[p[2]];
111 pix[3] = cm[p[3]];
112 pix[4] = cm[p[4]];
113 pix[5] = cm[p[5]];
114 pix[6] = cm[p[6]];
115 pix[7] = cm[p[7]];
116 pix += line_size;
117 p += 8;
118 }
119}
120
121void add_pixels_clamped_c(const DCTELEM *block, UINT8 *pixels, int line_size)
122{
123 const DCTELEM *p;
124 UINT8 *pix;
125 int i;
126 UINT8 *cm = cropTbl + MAX_NEG_CROP;
127
128 /* read the pixels */
129 p = block;
130 pix = pixels;
131 for(i=0;i<8;i++) {
132 pix[0] = cm[pix[0] + p[0]];
133 pix[1] = cm[pix[1] + p[1]];
134 pix[2] = cm[pix[2] + p[2]];
135 pix[3] = cm[pix[3] + p[3]];
136 pix[4] = cm[pix[4] + p[4]];
137 pix[5] = cm[pix[5] + p[5]];
138 pix[6] = cm[pix[6] + p[6]];
139 pix[7] = cm[pix[7] + p[7]];
140 pix += line_size;
141 p += 8;
142 }
143}
144
145#define PIXOP(BTYPE, OPNAME, OP, INCR) \
146 \
147static void OPNAME ## _pixels(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
148{ \
149 BTYPE *p; \
150 const UINT8 *pix; \
151 \
152 p = block; \
153 pix = pixels; \
154 do { \
155 OP(p[0], pix[0]); \
156 OP(p[1], pix[1]); \
157 OP(p[2], pix[2]); \
158 OP(p[3], pix[3]); \
159 OP(p[4], pix[4]); \
160 OP(p[5], pix[5]); \
161 OP(p[6], pix[6]); \
162 OP(p[7], pix[7]); \
163 pix += line_size; \
164 p += INCR; \
165 } while (--h);; \
166} \
167 \
168static void OPNAME ## _pixels_x2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
169{ \
170 BTYPE *p; \
171 const UINT8 *pix; \
172 \
173 p = block; \
174 pix = pixels; \
175 do { \
176 OP(p[0], avg2(pix[0], pix[1])); \
177 OP(p[1], avg2(pix[1], pix[2])); \
178 OP(p[2], avg2(pix[2], pix[3])); \
179 OP(p[3], avg2(pix[3], pix[4])); \
180 OP(p[4], avg2(pix[4], pix[5])); \
181 OP(p[5], avg2(pix[5], pix[6])); \
182 OP(p[6], avg2(pix[6], pix[7])); \
183 OP(p[7], avg2(pix[7], pix[8])); \
184 pix += line_size; \
185 p += INCR; \
186 } while (--h); \
187} \
188 \
189static void OPNAME ## _pixels_y2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
190{ \
191 BTYPE *p; \
192 const UINT8 *pix; \
193 const UINT8 *pix1; \
194 \
195 p = block; \
196 pix = pixels; \
197 pix1 = pixels + line_size; \
198 do { \
199 OP(p[0], avg2(pix[0], pix1[0])); \
200 OP(p[1], avg2(pix[1], pix1[1])); \
201 OP(p[2], avg2(pix[2], pix1[2])); \
202 OP(p[3], avg2(pix[3], pix1[3])); \
203 OP(p[4], avg2(pix[4], pix1[4])); \
204 OP(p[5], avg2(pix[5], pix1[5])); \
205 OP(p[6], avg2(pix[6], pix1[6])); \
206 OP(p[7], avg2(pix[7], pix1[7])); \
207 pix += line_size; \
208 pix1 += line_size; \
209 p += INCR; \
210 } while(--h); \
211} \
212 \
213static void OPNAME ## _pixels_xy2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
214{ \
215 BTYPE *p; \
216 const UINT8 *pix; \
217 const UINT8 *pix1; \
218 \
219 p = block; \
220 pix = pixels; \
221 pix1 = pixels + line_size; \
222 do { \
223 OP(p[0], avg4(pix[0], pix[1], pix1[0], pix1[1])); \
224 OP(p[1], avg4(pix[1], pix[2], pix1[1], pix1[2])); \
225 OP(p[2], avg4(pix[2], pix[3], pix1[2], pix1[3])); \
226 OP(p[3], avg4(pix[3], pix[4], pix1[3], pix1[4])); \
227 OP(p[4], avg4(pix[4], pix[5], pix1[4], pix1[5])); \
228 OP(p[5], avg4(pix[5], pix[6], pix1[5], pix1[6])); \
229 OP(p[6], avg4(pix[6], pix[7], pix1[6], pix1[7])); \
230 OP(p[7], avg4(pix[7], pix[8], pix1[7], pix1[8])); \
231 pix += line_size; \
232 pix1 += line_size; \
233 p += INCR; \
234 } while(--h); \
235} \
236 \
237void (*OPNAME ## _pixels_tab[4])(BTYPE *block, const UINT8 *pixels, int line_size, int h) = { \
238 OPNAME ## _pixels, \
239 OPNAME ## _pixels_x2, \
240 OPNAME ## _pixels_y2, \
241 OPNAME ## _pixels_xy2, \
242};
243
244
245/* rounding primitives */
246#define avg2(a,b) ((a+b+1)>>1)
247#define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
248
249#define op_put(a, b) a = b
250#define op_avg(a, b) a = avg2(a, b)
251#define op_sub(a, b) a -= b
252
253PIXOP(UINT8, put, op_put, line_size)
254PIXOP(UINT8, avg, op_avg, line_size)
255
256PIXOP(DCTELEM, sub, op_sub, 8)
257
258/* not rounding primitives */
259#undef avg2
260#undef avg4
261#define avg2(a,b) ((a+b)>>1)
262#define avg4(a,b,c,d) ((a+b+c+d+1)>>2)
263
264PIXOP(UINT8, put_no_rnd, op_put, line_size)
265PIXOP(UINT8, avg_no_rnd, op_avg, line_size)
266
267/* motion estimation */
268
269#undef avg2
270#undef avg4
271#define avg2(a,b) ((a+b+1)>>1)
272#define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
273
274int pix_abs16x16_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
275{
276 int s, i;
277
278 s = 0;
279 for(i=0;i<h;i++) {
280 s += abs(pix1[0] - pix2[0]);
281 s += abs(pix1[1] - pix2[1]);
282 s += abs(pix1[2] - pix2[2]);
283 s += abs(pix1[3] - pix2[3]);
284 s += abs(pix1[4] - pix2[4]);
285 s += abs(pix1[5] - pix2[5]);
286 s += abs(pix1[6] - pix2[6]);
287 s += abs(pix1[7] - pix2[7]);
288 s += abs(pix1[8] - pix2[8]);
289 s += abs(pix1[9] - pix2[9]);
290 s += abs(pix1[10] - pix2[10]);
291 s += abs(pix1[11] - pix2[11]);
292 s += abs(pix1[12] - pix2[12]);
293 s += abs(pix1[13] - pix2[13]);
294 s += abs(pix1[14] - pix2[14]);
295 s += abs(pix1[15] - pix2[15]);
296 pix1 += line_size;
297 pix2 += line_size;
298 }
299 return s;
300}
301
302int pix_abs16x16_x2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
303{
304 int s, i;
305
306 s = 0;
307 for(i=0;i<h;i++) {
308 s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
309 s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
310 s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
311 s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
312 s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
313 s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
314 s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
315 s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
316 s += abs(pix1[8] - avg2(pix2[8], pix2[9]));
317 s += abs(pix1[9] - avg2(pix2[9], pix2[10]));
318 s += abs(pix1[10] - avg2(pix2[10], pix2[11]));
319 s += abs(pix1[11] - avg2(pix2[11], pix2[12]));
320 s += abs(pix1[12] - avg2(pix2[12], pix2[13]));
321 s += abs(pix1[13] - avg2(pix2[13], pix2[14]));
322 s += abs(pix1[14] - avg2(pix2[14], pix2[15]));
323 s += abs(pix1[15] - avg2(pix2[15], pix2[16]));
324 pix1 += line_size;
325 pix2 += line_size;
326 }
327 return s;
328}
329
330int pix_abs16x16_y2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
331{
332 int s, i;
333 UINT8 *pix3 = pix2 + line_size;
334
335 s = 0;
336 for(i=0;i<h;i++) {
337 s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
338 s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
339 s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
340 s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
341 s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
342 s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
343 s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
344 s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
345 s += abs(pix1[8] - avg2(pix2[8], pix3[8]));
346 s += abs(pix1[9] - avg2(pix2[9], pix3[9]));
347 s += abs(pix1[10] - avg2(pix2[10], pix3[10]));
348 s += abs(pix1[11] - avg2(pix2[11], pix3[11]));
349 s += abs(pix1[12] - avg2(pix2[12], pix3[12]));
350 s += abs(pix1[13] - avg2(pix2[13], pix3[13]));
351 s += abs(pix1[14] - avg2(pix2[14], pix3[14]));
352 s += abs(pix1[15] - avg2(pix2[15], pix3[15]));
353 pix1 += line_size;
354 pix2 += line_size;
355 pix3 += line_size;
356 }
357 return s;
358}
359
360int pix_abs16x16_xy2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
361{
362 int s, i;
363 UINT8 *pix3 = pix2 + line_size;
364
365 s = 0;
366 for(i=0;i<h;i++) {
367 s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
368 s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
369 s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
370 s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
371 s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
372 s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
373 s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
374 s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
375 s += abs(pix1[8] - avg4(pix2[8], pix2[9], pix3[8], pix3[9]));
376 s += abs(pix1[9] - avg4(pix2[9], pix2[10], pix3[9], pix3[10]));
377 s += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
378 s += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
379 s += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
380 s += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
381 s += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
382 s += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
383 pix1 += line_size;
384 pix2 += line_size;
385 pix3 += line_size;
386 }
387 return s;
388}
389
e0eac44e
FB
390/* permute block according so that it corresponds to the MMX idct
391 order */
d962f6fd
A
392#ifdef SIMPLE_IDCT
393void block_permute(INT16 *block)
394{
395 int i;
396 INT16 temp[64];
397
398// for(i=0; i<64; i++) temp[i] = block[ block_permute_op(i) ];
399 for(i=0; i<64; i++) temp[ block_permute_op(i) ] = block[i];
400
401 for(i=0; i<64; i++) block[i] = temp[i];
402/*
403 for(i=0; i<64; i++)
404 {
405 if((i&7)==0) printf("\n");
406 printf("%2d ", block[i]);
407 }
408*/
409}
410
411#else
412
e0eac44e 413void block_permute(INT16 *block)
de6d9b64 414{
e0eac44e 415 int tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
de6d9b64
FB
416 int i;
417
e0eac44e
FB
418 for(i=0;i<8;i++) {
419 tmp1 = block[1];
420 tmp2 = block[2];
421 tmp3 = block[3];
422 tmp4 = block[4];
423 tmp5 = block[5];
424 tmp6 = block[6];
425 block[1] = tmp2;
426 block[2] = tmp4;
427 block[3] = tmp6;
428 block[4] = tmp1;
429 block[5] = tmp3;
430 block[6] = tmp5;
431 block += 8;
432 }
433}
d962f6fd 434#endif
e0eac44e
FB
435
436void dsputil_init(void)
437{
438 int i, j;
c34270f5 439 int use_permuted_idct;
e0eac44e 440
de6d9b64
FB
441 for(i=0;i<256;i++) cropTbl[i + MAX_NEG_CROP] = i;
442 for(i=0;i<MAX_NEG_CROP;i++) {
443 cropTbl[i] = 0;
444 cropTbl[i + MAX_NEG_CROP + 256] = 255;
445 }
446
447 for(i=0;i<512;i++) {
448 squareTbl[i] = (i - 256) * (i - 256);
449 }
450
d962f6fd
A
451#ifdef SIMPLE_IDCT
452 ff_idct = simple_idct;
453#else
4af7bcc1 454 ff_idct = j_rev_dct;
d962f6fd 455#endif
de6d9b64
FB
456 get_pixels = get_pixels_c;
457 put_pixels_clamped = put_pixels_clamped_c;
458 add_pixels_clamped = add_pixels_clamped_c;
459
460 pix_abs16x16 = pix_abs16x16_c;
461 pix_abs16x16_x2 = pix_abs16x16_x2_c;
462 pix_abs16x16_y2 = pix_abs16x16_y2_c;
463 pix_abs16x16_xy2 = pix_abs16x16_xy2_c;
464 av_fdct = jpeg_fdct_ifast;
465
c34270f5 466 use_permuted_idct = 1;
e0eac44e 467
980fc7b8 468#ifdef HAVE_MMX
de6d9b64
FB
469 dsputil_init_mmx();
470#endif
3d03c0a2
FB
471#ifdef ARCH_ARMV4L
472 dsputil_init_armv4l();
473#endif
c34270f5
FB
474#ifdef HAVE_MLIB
475 dsputil_init_mlib();
476 use_permuted_idct = 0;
477#endif
478
d962f6fd
A
479#ifdef SIMPLE_IDCT
480 if(ff_idct == simple_idct) use_permuted_idct=0;
481#endif
482
c34270f5
FB
483 if (use_permuted_idct) {
484 /* permute for IDCT */
485 for(i=0;i<64;i++) {
486 j = zigzag_direct[i];
487 zigzag_direct[i] = block_permute_op(j);
488 j = ff_alternate_horizontal_scan[i];
489 ff_alternate_horizontal_scan[i] = block_permute_op(j);
490 j = ff_alternate_vertical_scan[i];
491 ff_alternate_vertical_scan[i] = block_permute_op(j);
492 }
493 block_permute(default_intra_matrix);
494 block_permute(default_non_intra_matrix);
495 }
de6d9b64 496}