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