updated dct-test to test IDCTs too
[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
34static UINT8 cropTbl[256 + 2 * MAX_NEG_CROP];
35UINT32 squareTbl[512];
36
37void get_pixels_c(DCTELEM *block, const UINT8 *pixels, int line_size)
38{
39 DCTELEM *p;
40 const UINT8 *pix;
41 int i;
42
43 /* read the pixels */
44 p = block;
45 pix = pixels;
46 for(i=0;i<8;i++) {
47 p[0] = pix[0];
48 p[1] = pix[1];
49 p[2] = pix[2];
50 p[3] = pix[3];
51 p[4] = pix[4];
52 p[5] = pix[5];
53 p[6] = pix[6];
54 p[7] = pix[7];
55 pix += line_size;
56 p += 8;
57 }
58}
59
60void put_pixels_clamped_c(const DCTELEM *block, UINT8 *pixels, int line_size)
61{
62 const DCTELEM *p;
63 UINT8 *pix;
64 int i;
65 UINT8 *cm = cropTbl + MAX_NEG_CROP;
66
67 /* read the pixels */
68 p = block;
69 pix = pixels;
70 for(i=0;i<8;i++) {
71 pix[0] = cm[p[0]];
72 pix[1] = cm[p[1]];
73 pix[2] = cm[p[2]];
74 pix[3] = cm[p[3]];
75 pix[4] = cm[p[4]];
76 pix[5] = cm[p[5]];
77 pix[6] = cm[p[6]];
78 pix[7] = cm[p[7]];
79 pix += line_size;
80 p += 8;
81 }
82}
83
84void add_pixels_clamped_c(const DCTELEM *block, UINT8 *pixels, int line_size)
85{
86 const DCTELEM *p;
87 UINT8 *pix;
88 int i;
89 UINT8 *cm = cropTbl + MAX_NEG_CROP;
90
91 /* read the pixels */
92 p = block;
93 pix = pixels;
94 for(i=0;i<8;i++) {
95 pix[0] = cm[pix[0] + p[0]];
96 pix[1] = cm[pix[1] + p[1]];
97 pix[2] = cm[pix[2] + p[2]];
98 pix[3] = cm[pix[3] + p[3]];
99 pix[4] = cm[pix[4] + p[4]];
100 pix[5] = cm[pix[5] + p[5]];
101 pix[6] = cm[pix[6] + p[6]];
102 pix[7] = cm[pix[7] + p[7]];
103 pix += line_size;
104 p += 8;
105 }
106}
107
108#define PIXOP(BTYPE, OPNAME, OP, INCR) \
109 \
110static void OPNAME ## _pixels(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
111{ \
112 BTYPE *p; \
113 const UINT8 *pix; \
114 \
115 p = block; \
116 pix = pixels; \
117 do { \
118 OP(p[0], pix[0]); \
119 OP(p[1], pix[1]); \
120 OP(p[2], pix[2]); \
121 OP(p[3], pix[3]); \
122 OP(p[4], pix[4]); \
123 OP(p[5], pix[5]); \
124 OP(p[6], pix[6]); \
125 OP(p[7], pix[7]); \
126 pix += line_size; \
127 p += INCR; \
128 } while (--h);; \
129} \
130 \
131static void OPNAME ## _pixels_x2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
132{ \
133 BTYPE *p; \
134 const UINT8 *pix; \
135 \
136 p = block; \
137 pix = pixels; \
138 do { \
139 OP(p[0], avg2(pix[0], pix[1])); \
140 OP(p[1], avg2(pix[1], pix[2])); \
141 OP(p[2], avg2(pix[2], pix[3])); \
142 OP(p[3], avg2(pix[3], pix[4])); \
143 OP(p[4], avg2(pix[4], pix[5])); \
144 OP(p[5], avg2(pix[5], pix[6])); \
145 OP(p[6], avg2(pix[6], pix[7])); \
146 OP(p[7], avg2(pix[7], pix[8])); \
147 pix += line_size; \
148 p += INCR; \
149 } while (--h); \
150} \
151 \
152static void OPNAME ## _pixels_y2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
153{ \
154 BTYPE *p; \
155 const UINT8 *pix; \
156 const UINT8 *pix1; \
157 \
158 p = block; \
159 pix = pixels; \
160 pix1 = pixels + line_size; \
161 do { \
162 OP(p[0], avg2(pix[0], pix1[0])); \
163 OP(p[1], avg2(pix[1], pix1[1])); \
164 OP(p[2], avg2(pix[2], pix1[2])); \
165 OP(p[3], avg2(pix[3], pix1[3])); \
166 OP(p[4], avg2(pix[4], pix1[4])); \
167 OP(p[5], avg2(pix[5], pix1[5])); \
168 OP(p[6], avg2(pix[6], pix1[6])); \
169 OP(p[7], avg2(pix[7], pix1[7])); \
170 pix += line_size; \
171 pix1 += line_size; \
172 p += INCR; \
173 } while(--h); \
174} \
175 \
176static void OPNAME ## _pixels_xy2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
177{ \
178 BTYPE *p; \
179 const UINT8 *pix; \
180 const UINT8 *pix1; \
181 \
182 p = block; \
183 pix = pixels; \
184 pix1 = pixels + line_size; \
185 do { \
186 OP(p[0], avg4(pix[0], pix[1], pix1[0], pix1[1])); \
187 OP(p[1], avg4(pix[1], pix[2], pix1[1], pix1[2])); \
188 OP(p[2], avg4(pix[2], pix[3], pix1[2], pix1[3])); \
189 OP(p[3], avg4(pix[3], pix[4], pix1[3], pix1[4])); \
190 OP(p[4], avg4(pix[4], pix[5], pix1[4], pix1[5])); \
191 OP(p[5], avg4(pix[5], pix[6], pix1[5], pix1[6])); \
192 OP(p[6], avg4(pix[6], pix[7], pix1[6], pix1[7])); \
193 OP(p[7], avg4(pix[7], pix[8], pix1[7], pix1[8])); \
194 pix += line_size; \
195 pix1 += line_size; \
196 p += INCR; \
197 } while(--h); \
198} \
199 \
200void (*OPNAME ## _pixels_tab[4])(BTYPE *block, const UINT8 *pixels, int line_size, int h) = { \
201 OPNAME ## _pixels, \
202 OPNAME ## _pixels_x2, \
203 OPNAME ## _pixels_y2, \
204 OPNAME ## _pixels_xy2, \
205};
206
207
208/* rounding primitives */
209#define avg2(a,b) ((a+b+1)>>1)
210#define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
211
212#define op_put(a, b) a = b
213#define op_avg(a, b) a = avg2(a, b)
214#define op_sub(a, b) a -= b
215
216PIXOP(UINT8, put, op_put, line_size)
217PIXOP(UINT8, avg, op_avg, line_size)
218
219PIXOP(DCTELEM, sub, op_sub, 8)
220
221/* not rounding primitives */
222#undef avg2
223#undef avg4
224#define avg2(a,b) ((a+b)>>1)
225#define avg4(a,b,c,d) ((a+b+c+d+1)>>2)
226
227PIXOP(UINT8, put_no_rnd, op_put, line_size)
228PIXOP(UINT8, avg_no_rnd, op_avg, line_size)
229
230/* motion estimation */
231
232#undef avg2
233#undef avg4
234#define avg2(a,b) ((a+b+1)>>1)
235#define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
236
237int pix_abs16x16_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
238{
239 int s, i;
240
241 s = 0;
242 for(i=0;i<h;i++) {
243 s += abs(pix1[0] - pix2[0]);
244 s += abs(pix1[1] - pix2[1]);
245 s += abs(pix1[2] - pix2[2]);
246 s += abs(pix1[3] - pix2[3]);
247 s += abs(pix1[4] - pix2[4]);
248 s += abs(pix1[5] - pix2[5]);
249 s += abs(pix1[6] - pix2[6]);
250 s += abs(pix1[7] - pix2[7]);
251 s += abs(pix1[8] - pix2[8]);
252 s += abs(pix1[9] - pix2[9]);
253 s += abs(pix1[10] - pix2[10]);
254 s += abs(pix1[11] - pix2[11]);
255 s += abs(pix1[12] - pix2[12]);
256 s += abs(pix1[13] - pix2[13]);
257 s += abs(pix1[14] - pix2[14]);
258 s += abs(pix1[15] - pix2[15]);
259 pix1 += line_size;
260 pix2 += line_size;
261 }
262 return s;
263}
264
265int pix_abs16x16_x2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
266{
267 int s, i;
268
269 s = 0;
270 for(i=0;i<h;i++) {
271 s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
272 s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
273 s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
274 s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
275 s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
276 s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
277 s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
278 s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
279 s += abs(pix1[8] - avg2(pix2[8], pix2[9]));
280 s += abs(pix1[9] - avg2(pix2[9], pix2[10]));
281 s += abs(pix1[10] - avg2(pix2[10], pix2[11]));
282 s += abs(pix1[11] - avg2(pix2[11], pix2[12]));
283 s += abs(pix1[12] - avg2(pix2[12], pix2[13]));
284 s += abs(pix1[13] - avg2(pix2[13], pix2[14]));
285 s += abs(pix1[14] - avg2(pix2[14], pix2[15]));
286 s += abs(pix1[15] - avg2(pix2[15], pix2[16]));
287 pix1 += line_size;
288 pix2 += line_size;
289 }
290 return s;
291}
292
293int pix_abs16x16_y2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
294{
295 int s, i;
296 UINT8 *pix3 = pix2 + line_size;
297
298 s = 0;
299 for(i=0;i<h;i++) {
300 s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
301 s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
302 s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
303 s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
304 s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
305 s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
306 s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
307 s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
308 s += abs(pix1[8] - avg2(pix2[8], pix3[8]));
309 s += abs(pix1[9] - avg2(pix2[9], pix3[9]));
310 s += abs(pix1[10] - avg2(pix2[10], pix3[10]));
311 s += abs(pix1[11] - avg2(pix2[11], pix3[11]));
312 s += abs(pix1[12] - avg2(pix2[12], pix3[12]));
313 s += abs(pix1[13] - avg2(pix2[13], pix3[13]));
314 s += abs(pix1[14] - avg2(pix2[14], pix3[14]));
315 s += abs(pix1[15] - avg2(pix2[15], pix3[15]));
316 pix1 += line_size;
317 pix2 += line_size;
318 pix3 += line_size;
319 }
320 return s;
321}
322
323int pix_abs16x16_xy2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
324{
325 int s, i;
326 UINT8 *pix3 = pix2 + line_size;
327
328 s = 0;
329 for(i=0;i<h;i++) {
330 s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
331 s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
332 s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
333 s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
334 s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
335 s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
336 s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
337 s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
338 s += abs(pix1[8] - avg4(pix2[8], pix2[9], pix3[8], pix3[9]));
339 s += abs(pix1[9] - avg4(pix2[9], pix2[10], pix3[9], pix3[10]));
340 s += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
341 s += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
342 s += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
343 s += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
344 s += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
345 s += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
346 pix1 += line_size;
347 pix2 += line_size;
348 pix3 += line_size;
349 }
350 return s;
351}
352
353void dsputil_init(void)
354{
355 int i;
356
357 for(i=0;i<256;i++) cropTbl[i + MAX_NEG_CROP] = i;
358 for(i=0;i<MAX_NEG_CROP;i++) {
359 cropTbl[i] = 0;
360 cropTbl[i + MAX_NEG_CROP + 256] = 255;
361 }
362
363 for(i=0;i<512;i++) {
364 squareTbl[i] = (i - 256) * (i - 256);
365 }
366
4af7bcc1 367 ff_idct = j_rev_dct;
de6d9b64
FB
368 get_pixels = get_pixels_c;
369 put_pixels_clamped = put_pixels_clamped_c;
370 add_pixels_clamped = add_pixels_clamped_c;
371
372 pix_abs16x16 = pix_abs16x16_c;
373 pix_abs16x16_x2 = pix_abs16x16_x2_c;
374 pix_abs16x16_y2 = pix_abs16x16_y2_c;
375 pix_abs16x16_xy2 = pix_abs16x16_xy2_c;
376 av_fdct = jpeg_fdct_ifast;
377
980fc7b8 378#ifdef HAVE_MMX
de6d9b64
FB
379 dsputil_init_mmx();
380#endif
381}