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