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