Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * Misc image convertion routines | |
524c6b63 | 3 | * Copyright (c) 2001, 2002, 2003 Fabrice Bellard. |
de6d9b64 | 4 | * |
ff4ec49e FB |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2 of the License, or (at your option) any later version. | |
de6d9b64 | 9 | * |
ff4ec49e | 10 | * This library is distributed in the hope that it will be useful, |
de6d9b64 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ff4ec49e FB |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. | |
de6d9b64 | 14 | * |
ff4ec49e FB |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
de6d9b64 | 18 | */ |
de6d9b64 | 19 | #include "avcodec.h" |
85c242d8 | 20 | #include "dsputil.h" |
de6d9b64 | 21 | |
54329dd5 NK |
22 | #ifdef USE_FASTMEMCPY |
23 | #include "fastmemcpy.h" | |
24 | #endif | |
5981f4e6 F |
25 | |
26 | #ifdef HAVE_MMX | |
27 | #include "i386/mmx.h" | |
28 | #endif | |
524c6b63 FB |
29 | |
30 | typedef struct PixFmtInfo { | |
31 | const char *name; | |
32 | UINT8 nb_components; /* number of components in AVPicture array */ | |
33 | UINT8 is_yuv : 1; /* true if YUV instead of RGB color space */ | |
34 | UINT8 is_packed : 1; /* true if multiple components in same word */ | |
35 | UINT8 is_paletted : 1; /* true if paletted */ | |
36 | UINT8 is_alpha : 1; /* true if alpha can be specified */ | |
37 | UINT8 is_gray : 1; /* true if gray or monochrome format */ | |
38 | UINT8 x_chroma_shift; /* X chroma subsampling factor is 2 ^ shift */ | |
39 | UINT8 y_chroma_shift; /* Y chroma subsampling factor is 2 ^ shift */ | |
40 | } PixFmtInfo; | |
41 | ||
42 | /* this table gives more information about formats */ | |
43 | static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = { | |
44 | /* YUV formats */ | |
45 | [PIX_FMT_YUV420P] = { | |
46 | name: "yuv420p", | |
47 | nb_components: 3, is_yuv: 1, | |
48 | x_chroma_shift: 1, y_chroma_shift: 1, | |
49 | }, | |
50 | [PIX_FMT_YUV422P] = { | |
51 | name: "yuv422p", | |
52 | nb_components: 3, is_yuv: 1, | |
53 | x_chroma_shift: 1, y_chroma_shift: 0, | |
54 | }, | |
55 | [PIX_FMT_YUV444P] = { | |
56 | name: "yuv444p", | |
57 | nb_components: 3, is_yuv: 1, | |
58 | x_chroma_shift: 0, y_chroma_shift: 0, | |
59 | }, | |
60 | [PIX_FMT_YUV422] = { | |
61 | name: "yuv422", | |
62 | nb_components: 1, is_yuv: 1, is_packed: 1, | |
63 | x_chroma_shift: 1, y_chroma_shift: 0, | |
64 | }, | |
65 | [PIX_FMT_YUV410P] = { | |
66 | name: "yuv410p", | |
67 | nb_components: 3, is_yuv: 1, | |
68 | x_chroma_shift: 2, y_chroma_shift: 2, | |
69 | }, | |
70 | [PIX_FMT_YUV411P] = { | |
71 | name: "yuv411p", | |
72 | nb_components: 3, is_yuv: 1, | |
73 | x_chroma_shift: 2, y_chroma_shift: 0, | |
74 | }, | |
75 | ||
76 | /* RGB formats */ | |
77 | [PIX_FMT_RGB24] = { | |
78 | name: "rgb24", | |
79 | nb_components: 1, is_packed: 1, | |
80 | }, | |
81 | [PIX_FMT_BGR24] = { | |
82 | name: "bgr24", | |
83 | nb_components: 1, is_packed: 1, | |
84 | }, | |
85 | [PIX_FMT_RGBA32] = { | |
86 | name: "rgba32", | |
87 | nb_components: 1, is_packed: 1, is_alpha: 1, | |
88 | }, | |
89 | [PIX_FMT_RGB565] = { | |
90 | name: "rgb565", | |
91 | nb_components: 1, is_packed: 1, | |
92 | }, | |
93 | [PIX_FMT_RGB555] = { | |
94 | name: "rgb555", | |
95 | nb_components: 1, is_packed: 1, is_alpha : 1, | |
96 | }, | |
97 | ||
98 | /* gray / mono formats */ | |
99 | [PIX_FMT_GRAY8] = { | |
100 | name: "gray", | |
101 | nb_components: 1, is_gray: 1, | |
102 | }, | |
103 | [PIX_FMT_MONOWHITE] = { | |
104 | name: "monow", | |
105 | nb_components: 1, is_packed: 1, is_gray: 1, | |
106 | }, | |
107 | [PIX_FMT_MONOBLACK] = { | |
108 | name: "monob", | |
109 | nb_components: 1, is_packed: 1, is_gray: 1, | |
110 | }, | |
111 | }; | |
112 | ||
113 | void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift) | |
114 | { | |
115 | if (pix_fmt_info[pix_fmt].is_yuv) { | |
116 | *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift; | |
117 | *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift; | |
118 | } else { | |
119 | *h_shift=0; | |
120 | *v_shift=0; | |
121 | } | |
122 | } | |
123 | ||
124 | const char *avcodec_get_pix_fmt_name(int pix_fmt) | |
125 | { | |
126 | if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB) | |
127 | return "???"; | |
128 | else | |
129 | return pix_fmt_info[pix_fmt].name; | |
130 | } | |
131 | ||
de6d9b64 FB |
132 | /* XXX: totally non optimized */ |
133 | ||
524c6b63 FB |
134 | static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src, |
135 | int width, int height) | |
de6d9b64 | 136 | { |
524c6b63 | 137 | UINT8 *lum, *cb, *cr; |
de6d9b64 | 138 | int x, y; |
524c6b63 FB |
139 | const UINT8 *p; |
140 | ||
141 | lum = dst->data[0]; | |
142 | cb = dst->data[1]; | |
143 | cr = dst->data[2]; | |
144 | p = src->data[0]; | |
145 | ||
de6d9b64 FB |
146 | for(y=0;y<height;y+=2) { |
147 | for(x=0;x<width;x+=2) { | |
148 | lum[0] = p[0]; | |
149 | cb[0] = p[1]; | |
150 | lum[1] = p[2]; | |
151 | cr[0] = p[3]; | |
152 | p += 4; | |
153 | lum += 2; | |
154 | cb++; | |
155 | cr++; | |
156 | } | |
157 | for(x=0;x<width;x+=2) { | |
158 | lum[0] = p[0]; | |
159 | lum[1] = p[2]; | |
160 | p += 4; | |
161 | lum += 2; | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | #define SCALEBITS 8 | |
167 | #define ONE_HALF (1 << (SCALEBITS - 1)) | |
168 | #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5)) | |
169 | ||
85c242d8 FB |
170 | /* XXX: use generic filter ? */ |
171 | /* 1x2 -> 1x1 */ | |
172 | static void shrink2(UINT8 *dst, int dst_wrap, | |
173 | UINT8 *src, int src_wrap, | |
174 | int width, int height) | |
175 | { | |
176 | int w; | |
177 | UINT8 *s1, *s2, *d; | |
178 | ||
179 | for(;height > 0; height--) { | |
180 | s1 = src; | |
181 | s2 = s1 + src_wrap; | |
182 | d = dst; | |
183 | for(w = width;w >= 4; w-=4) { | |
184 | d[0] = (s1[0] + s2[0]) >> 1; | |
185 | d[1] = (s1[1] + s2[1]) >> 1; | |
186 | d[2] = (s1[2] + s2[2]) >> 1; | |
187 | d[3] = (s1[3] + s2[3]) >> 1; | |
188 | s1 += 4; | |
189 | s2 += 4; | |
190 | d += 4; | |
191 | } | |
192 | for(;w > 0; w--) { | |
193 | d[0] = (s1[0] + s2[0]) >> 1; | |
194 | s1++; | |
195 | s2++; | |
196 | d++; | |
197 | } | |
198 | src += 2 * src_wrap; | |
199 | dst += dst_wrap; | |
200 | } | |
201 | } | |
202 | ||
203 | /* 2x2 -> 1x1 */ | |
204 | static void shrink22(UINT8 *dst, int dst_wrap, | |
205 | UINT8 *src, int src_wrap, | |
206 | int width, int height) | |
207 | { | |
208 | int w; | |
209 | UINT8 *s1, *s2, *d; | |
210 | ||
211 | for(;height > 0; height--) { | |
212 | s1 = src; | |
213 | s2 = s1 + src_wrap; | |
214 | d = dst; | |
215 | for(w = width;w >= 4; w-=4) { | |
216 | d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1; | |
217 | d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1; | |
218 | d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1; | |
219 | d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1; | |
220 | s1 += 8; | |
221 | s2 += 8; | |
222 | d += 4; | |
223 | } | |
224 | for(;w > 0; w--) { | |
225 | d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1; | |
226 | s1 += 2; | |
227 | s2 += 2; | |
228 | d++; | |
229 | } | |
230 | src += 2 * src_wrap; | |
231 | dst += dst_wrap; | |
232 | } | |
233 | } | |
234 | ||
6742d95d FR |
235 | /* 1x1 -> 2x2 */ |
236 | static void grow22(UINT8 *dst, int dst_wrap, | |
237 | UINT8 *src, int src_wrap, | |
238 | int width, int height) | |
239 | { | |
240 | int w; | |
241 | UINT8 *s1, *d; | |
242 | ||
243 | for(;height > 0; height--) { | |
244 | s1 = src; | |
245 | d = dst; | |
246 | for(w = width;w >= 4; w-=4) { | |
247 | d[1] = d[0] = s1[0]; | |
248 | d[3] = d[2] = s1[1]; | |
249 | s1 += 2; | |
250 | d += 4; | |
251 | } | |
252 | for(;w > 0; w--) { | |
253 | d[0] = s1[0]; | |
254 | s1 ++; | |
255 | d++; | |
256 | } | |
257 | if (height%2) | |
258 | src += src_wrap; | |
259 | dst += dst_wrap; | |
260 | } | |
261 | } | |
262 | ||
524c6b63 | 263 | /* 1x2 -> 2x1 */ |
789587d5 FB |
264 | static void conv411(UINT8 *dst, int dst_wrap, |
265 | UINT8 *src, int src_wrap, | |
266 | int width, int height) | |
267 | { | |
268 | int w, c; | |
269 | UINT8 *s1, *s2, *d; | |
270 | ||
524c6b63 | 271 | for(;height > 0; height--) { |
789587d5 FB |
272 | s1 = src; |
273 | s2 = src + src_wrap; | |
274 | d = dst; | |
275 | for(w = width;w > 0; w--) { | |
276 | c = (s1[0] + s2[0]) >> 1; | |
277 | d[0] = c; | |
278 | d[1] = c; | |
279 | s1++; | |
280 | s2++; | |
281 | d += 2; | |
282 | } | |
283 | src += src_wrap * 2; | |
284 | dst += dst_wrap; | |
285 | } | |
286 | } | |
287 | ||
85c242d8 FB |
288 | static void img_copy(UINT8 *dst, int dst_wrap, |
289 | UINT8 *src, int src_wrap, | |
290 | int width, int height) | |
291 | { | |
292 | for(;height > 0; height--) { | |
293 | memcpy(dst, src, width); | |
294 | dst += dst_wrap; | |
295 | src += src_wrap; | |
296 | } | |
297 | } | |
298 | ||
299 | #define SCALE_BITS 10 | |
300 | ||
301 | #define C_Y (76309 >> (16 - SCALE_BITS)) | |
302 | #define C_RV (117504 >> (16 - SCALE_BITS)) | |
303 | #define C_BU (138453 >> (16 - SCALE_BITS)) | |
304 | #define C_GU (13954 >> (16 - SCALE_BITS)) | |
305 | #define C_GV (34903 >> (16 - SCALE_BITS)) | |
306 | ||
524c6b63 | 307 | #define YUV_TO_RGB2(r, g, b, y1)\ |
85c242d8 FB |
308 | {\ |
309 | y = (y1 - 16) * C_Y;\ | |
310 | r = cm[(y + r_add) >> SCALE_BITS];\ | |
311 | g = cm[(y + g_add) >> SCALE_BITS];\ | |
312 | b = cm[(y + b_add) >> SCALE_BITS];\ | |
313 | } | |
314 | ||
315 | /* XXX: no chroma interpolating is done */ | |
524c6b63 FB |
316 | #define RGB_FUNCTIONS(rgb_name) \ |
317 | \ | |
318 | static void yuv420p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \ | |
319 | int width, int height) \ | |
320 | { \ | |
321 | UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2; \ | |
322 | int w, y, cb, cr, r_add, g_add, b_add, width2; \ | |
323 | UINT8 *cm = cropTbl + MAX_NEG_CROP; \ | |
324 | unsigned int r, g, b; \ | |
325 | \ | |
326 | d = dst->data[0]; \ | |
327 | y1_ptr = src->data[0]; \ | |
328 | cb_ptr = src->data[1]; \ | |
329 | cr_ptr = src->data[2]; \ | |
330 | width2 = width >> 1; \ | |
331 | for(;height > 0; height -= 2) { \ | |
332 | d1 = d; \ | |
333 | d2 = d + dst->linesize[0]; \ | |
334 | y2_ptr = y1_ptr + src->linesize[0]; \ | |
335 | for(w = width2; w > 0; w --) { \ | |
336 | cb = cb_ptr[0] - 128; \ | |
337 | cr = cr_ptr[0] - 128; \ | |
338 | r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \ | |
339 | g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \ | |
340 | b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \ | |
341 | \ | |
342 | /* output 4 pixels */ \ | |
343 | YUV_TO_RGB2(r, g, b, y1_ptr[0]); \ | |
344 | RGB_OUT(d1, r, g, b); \ | |
345 | \ | |
346 | YUV_TO_RGB2(r, g, b, y1_ptr[1]); \ | |
347 | RGB_OUT(d1 + BPP, r, g, b); \ | |
348 | \ | |
349 | YUV_TO_RGB2(r, g, b, y2_ptr[0]); \ | |
350 | RGB_OUT(d2, r, g, b); \ | |
351 | \ | |
352 | YUV_TO_RGB2(r, g, b, y2_ptr[1]); \ | |
353 | RGB_OUT(d2 + BPP, r, g, b); \ | |
354 | \ | |
355 | d1 += 2 * BPP; \ | |
356 | d2 += 2 * BPP; \ | |
357 | \ | |
358 | y1_ptr += 2; \ | |
359 | y2_ptr += 2; \ | |
360 | cb_ptr++; \ | |
361 | cr_ptr++; \ | |
362 | } \ | |
363 | d += 2 * dst->linesize[0]; \ | |
364 | y1_ptr += 2 * src->linesize[0] - width; \ | |
365 | cb_ptr += src->linesize[1] - width2; \ | |
366 | cr_ptr += src->linesize[2] - width2; \ | |
367 | } \ | |
368 | } \ | |
369 | \ | |
370 | /* XXX: no chroma interpolating is done */ \ | |
371 | static void yuv422p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \ | |
372 | int width, int height) \ | |
373 | { \ | |
374 | UINT8 *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1; \ | |
375 | int w, y, cb, cr, r_add, g_add, b_add, width2; \ | |
376 | UINT8 *cm = cropTbl + MAX_NEG_CROP; \ | |
377 | unsigned int r, g, b; \ | |
378 | \ | |
379 | d = dst->data[0]; \ | |
380 | y1_ptr = src->data[0]; \ | |
381 | cb_ptr = src->data[1]; \ | |
382 | cr_ptr = src->data[2]; \ | |
383 | width2 = width >> 1; \ | |
384 | for(;height > 0; height --) { \ | |
385 | d1 = d; \ | |
386 | for(w = width2; w > 0; w --) { \ | |
387 | cb = cb_ptr[0] - 128; \ | |
388 | cr = cr_ptr[0] - 128; \ | |
389 | r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \ | |
390 | g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \ | |
391 | b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \ | |
392 | \ | |
393 | /* output 2 pixels */ \ | |
394 | YUV_TO_RGB2(r, g, b, y1_ptr[0]); \ | |
395 | RGB_OUT(d, r, g, b); \ | |
396 | \ | |
397 | YUV_TO_RGB2(r, g, b, y1_ptr[1]); \ | |
398 | RGB_OUT(d + BPP, r, g, b); \ | |
399 | \ | |
400 | d += 2 * BPP; \ | |
401 | \ | |
402 | y1_ptr += 2; \ | |
403 | cb_ptr++; \ | |
404 | cr_ptr++; \ | |
405 | } \ | |
406 | d += dst->linesize[0]; \ | |
407 | y1_ptr += src->linesize[0] - width; \ | |
408 | cb_ptr += src->linesize[1] - width2; \ | |
409 | cr_ptr += src->linesize[2] - width2; \ | |
410 | } \ | |
411 | } \ | |
412 | \ | |
413 | static void rgb_name ## _to_yuv420p(AVPicture *dst, AVPicture *src, \ | |
414 | int width, int height) \ | |
415 | { \ | |
416 | int wrap, wrap3, x, y; \ | |
417 | int r, g, b, r1, g1, b1; \ | |
418 | UINT8 *lum, *cb, *cr; \ | |
419 | const UINT8 *p; \ | |
420 | \ | |
421 | lum = dst->data[0]; \ | |
422 | cb = dst->data[1]; \ | |
423 | cr = dst->data[2]; \ | |
424 | \ | |
425 | wrap = width; \ | |
426 | wrap3 = width * 3; \ | |
427 | p = src->data[0]; \ | |
428 | for(y=0;y<height;y+=2) { \ | |
429 | for(x=0;x<width;x+=2) { \ | |
430 | RGB_IN(r, g, b, p); \ | |
431 | r1 = r; \ | |
432 | g1 = g; \ | |
433 | b1 = b; \ | |
434 | lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \ | |
435 | FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \ | |
436 | RGB_IN(r, g, b, p + BPP); \ | |
437 | r1 += r; \ | |
438 | g1 += g; \ | |
439 | b1 += b; \ | |
440 | lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \ | |
441 | FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \ | |
442 | p += wrap3; \ | |
443 | lum += wrap; \ | |
444 | \ | |
445 | RGB_IN(r, g, b, p); \ | |
446 | r1 += r; \ | |
447 | g1 += g; \ | |
448 | b1 += b; \ | |
449 | lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \ | |
450 | FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \ | |
451 | \ | |
452 | RGB_IN(r, g, b, p + BPP); \ | |
453 | r1 += r; \ | |
454 | g1 += g; \ | |
455 | b1 += b; \ | |
456 | lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \ | |
457 | FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \ | |
458 | \ | |
459 | cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \ | |
460 | FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> \ | |
461 | (SCALEBITS + 2)) + 128; \ | |
462 | cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \ | |
463 | FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> \ | |
464 | (SCALEBITS + 2)) + 128; \ | |
465 | \ | |
466 | cb++; \ | |
467 | cr++; \ | |
468 | p += -wrap3 + 2 * 3; \ | |
469 | lum += -wrap + 2; \ | |
470 | } \ | |
471 | p += wrap3; \ | |
472 | lum += wrap; \ | |
473 | } \ | |
474 | } | |
475 | ||
476 | /* copy bit n to bits 0 ... n - 1 */ | |
477 | static inline unsigned int bitcopy_n(unsigned int a, int n) | |
b71472eb | 478 | { |
524c6b63 FB |
479 | int mask; |
480 | mask = (1 << n) - 1; | |
481 | return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask); | |
482 | } | |
483 | ||
484 | /* rgb555 handling */ | |
485 | ||
486 | #define RGB_IN(r, g, b, s)\ | |
487 | {\ | |
488 | unsigned int v = ((UINT16 *)(s))[0];\ | |
489 | r = bitcopy_n(v >> (10 - 3), 3);\ | |
490 | g = bitcopy_n(v >> (5 - 3), 3);\ | |
491 | b = bitcopy_n(v << 3, 3);\ | |
492 | } | |
493 | ||
494 | #define RGB_OUT(d, r, g, b)\ | |
495 | {\ | |
496 | ((UINT16 *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;\ | |
497 | } | |
498 | ||
499 | #define BPP 2 | |
500 | ||
501 | RGB_FUNCTIONS(rgb555) | |
502 | ||
503 | #undef RGB_IN | |
504 | #undef RGB_OUT | |
505 | #undef BPP | |
506 | ||
507 | /* rgb565 handling */ | |
508 | ||
509 | #define RGB_IN(r, g, b, s)\ | |
510 | {\ | |
511 | unsigned int v = ((UINT16 *)(s))[0];\ | |
512 | r = bitcopy_n(v >> (11 - 3), 3);\ | |
513 | g = bitcopy_n(v >> (5 - 2), 2);\ | |
514 | b = bitcopy_n(v << 3, 3);\ | |
515 | } | |
516 | ||
517 | #define RGB_OUT(d, r, g, b)\ | |
518 | {\ | |
519 | ((UINT16 *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\ | |
520 | } | |
521 | ||
522 | #define BPP 2 | |
523 | ||
524 | RGB_FUNCTIONS(rgb565) | |
525 | ||
526 | #undef RGB_IN | |
527 | #undef RGB_OUT | |
528 | #undef BPP | |
529 | ||
530 | /* bgr24 handling */ | |
531 | ||
532 | #define RGB_IN(r, g, b, s)\ | |
533 | {\ | |
534 | b = (s)[0];\ | |
535 | g = (s)[1];\ | |
536 | r = (s)[2];\ | |
537 | } | |
538 | ||
539 | #define RGB_OUT(d, r, g, b)\ | |
540 | {\ | |
541 | (d)[0] = b;\ | |
542 | (d)[1] = g;\ | |
543 | (d)[2] = r;\ | |
544 | } | |
545 | ||
546 | #define BPP 3 | |
547 | ||
548 | RGB_FUNCTIONS(bgr24) | |
549 | ||
550 | #undef RGB_IN | |
551 | #undef RGB_OUT | |
552 | #undef BPP | |
553 | ||
554 | /* rgb24 handling */ | |
555 | ||
556 | #define RGB_IN(r, g, b, s)\ | |
557 | {\ | |
558 | r = (s)[0];\ | |
559 | g = (s)[1];\ | |
560 | b = (s)[2];\ | |
561 | } | |
562 | ||
563 | #define RGB_OUT(d, r, g, b)\ | |
564 | {\ | |
565 | (d)[0] = r;\ | |
566 | (d)[1] = g;\ | |
567 | (d)[2] = b;\ | |
568 | } | |
569 | ||
570 | #define BPP 3 | |
571 | ||
572 | RGB_FUNCTIONS(rgb24) | |
573 | ||
574 | #undef RGB_IN | |
575 | #undef RGB_OUT | |
576 | #undef BPP | |
577 | ||
578 | /* rgba32 handling */ | |
579 | ||
580 | #define RGB_IN(r, g, b, s)\ | |
581 | {\ | |
582 | unsigned int v = ((UINT32 *)(s))[0];\ | |
583 | r = (v >> 16) & 0xff;\ | |
584 | g = (v >> 8) & 0xff;\ | |
585 | b = v & 0xff;\ | |
586 | } | |
587 | ||
588 | #define RGB_OUT(d, r, g, b)\ | |
589 | {\ | |
590 | ((UINT32 *)(d))[0] = (0xff << 24) | (r << 16) | (g << 8) | b;\ | |
591 | } | |
592 | ||
593 | #define BPP 4 | |
594 | ||
595 | RGB_FUNCTIONS(rgba32) | |
596 | ||
597 | #undef RGB_IN | |
598 | #undef RGB_OUT | |
599 | #undef BPP | |
600 | ||
601 | ||
602 | static void rgb24_to_rgb565(AVPicture *dst, AVPicture *src, | |
603 | int width, int height) | |
604 | { | |
605 | const unsigned char *p; | |
606 | unsigned char *q; | |
607 | int r, g, b, dst_wrap, src_wrap; | |
608 | int x, y; | |
609 | ||
610 | p = src->data[0]; | |
611 | src_wrap = src->linesize[0] - 3 * width; | |
612 | ||
613 | q = dst->data[0]; | |
614 | dst_wrap = dst->linesize[0] - 2 * width; | |
615 | ||
616 | for(y=0;y<height;y++) { | |
617 | for(x=0;x<width;x++) { | |
618 | r = p[0]; | |
619 | g = p[1]; | |
620 | b = p[2]; | |
621 | ||
622 | ((unsigned short *)q)[0] = | |
623 | ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3); | |
624 | q += 2; | |
625 | p += 3; | |
b71472eb | 626 | } |
524c6b63 FB |
627 | p += src_wrap; |
628 | q += dst_wrap; | |
b71472eb PG |
629 | } |
630 | } | |
631 | ||
524c6b63 FB |
632 | /* NOTE: we also add a dummy alpha bit */ |
633 | static void rgb24_to_rgb555(AVPicture *dst, AVPicture *src, | |
634 | int width, int height) | |
b71472eb | 635 | { |
524c6b63 FB |
636 | const unsigned char *p; |
637 | unsigned char *q; | |
638 | int r, g, b, dst_wrap, src_wrap; | |
639 | int x, y; | |
640 | ||
641 | p = src->data[0]; | |
642 | src_wrap = src->linesize[0] - 3 * width; | |
643 | ||
644 | q = dst->data[0]; | |
645 | dst_wrap = dst->linesize[0] - 2 * width; | |
646 | ||
647 | for(y=0;y<height;y++) { | |
648 | for(x=0;x<width;x++) { | |
649 | r = p[0]; | |
650 | g = p[1]; | |
651 | b = p[2]; | |
652 | ||
653 | ((unsigned short *)q)[0] = | |
654 | ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000; | |
655 | q += 2; | |
656 | p += 3; | |
b71472eb | 657 | } |
524c6b63 FB |
658 | p += src_wrap; |
659 | q += dst_wrap; | |
b71472eb PG |
660 | } |
661 | } | |
662 | ||
524c6b63 FB |
663 | static void rgb24_to_gray(AVPicture *dst, AVPicture *src, |
664 | int width, int height) | |
85c242d8 | 665 | { |
524c6b63 FB |
666 | const unsigned char *p; |
667 | unsigned char *q; | |
668 | int r, g, b, dst_wrap, src_wrap; | |
669 | int x, y; | |
670 | ||
671 | p = src->data[0]; | |
672 | src_wrap = src->linesize[0] - 3 * width; | |
673 | ||
674 | q = dst->data[0]; | |
675 | dst_wrap = dst->linesize[0] - width; | |
676 | ||
677 | for(y=0;y<height;y++) { | |
678 | for(x=0;x<width;x++) { | |
679 | r = p[0]; | |
680 | g = p[1]; | |
681 | b = p[2]; | |
682 | ||
683 | q[0] = (FIX(0.29900) * r + FIX(0.58700) * g + | |
684 | FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; | |
685 | q++; | |
686 | p += 3; | |
85c242d8 | 687 | } |
524c6b63 FB |
688 | p += src_wrap; |
689 | q += dst_wrap; | |
85c242d8 FB |
690 | } |
691 | } | |
692 | ||
524c6b63 FB |
693 | static void gray_to_rgb24(AVPicture *dst, AVPicture *src, |
694 | int width, int height) | |
85c242d8 | 695 | { |
524c6b63 FB |
696 | const unsigned char *p; |
697 | unsigned char *q; | |
698 | int r, dst_wrap, src_wrap; | |
699 | int x, y; | |
700 | ||
701 | p = src->data[0]; | |
702 | src_wrap = src->linesize[0] - width; | |
703 | ||
704 | q = dst->data[0]; | |
705 | dst_wrap = dst->linesize[0] - 3 * width; | |
706 | ||
707 | for(y=0;y<height;y++) { | |
708 | for(x=0;x<width;x++) { | |
709 | r = p[0]; | |
710 | ||
711 | q[0] = r; | |
712 | q[1] = r; | |
713 | q[2] = r; | |
85c242d8 | 714 | |
524c6b63 FB |
715 | q += 3; |
716 | p ++; | |
717 | } | |
718 | p += src_wrap; | |
719 | q += dst_wrap; | |
720 | } | |
721 | } | |
722 | ||
723 | static void monowhite_to_rgb24(AVPicture *dst, AVPicture *src, | |
724 | int width, int height) | |
725 | { | |
726 | const unsigned char *p; | |
727 | unsigned char *q; | |
728 | int v, dst_wrap, src_wrap; | |
729 | int y, w; | |
730 | ||
731 | p = src->data[0]; | |
732 | src_wrap = src->linesize[0] - ((width + 7) >> 3); | |
733 | ||
734 | q = dst->data[0]; | |
735 | dst_wrap = dst->linesize[0] - 3 * width; | |
736 | ||
737 | for(y=0;y<height;y++) { | |
738 | w = width; | |
739 | while (w >= 8) { | |
740 | v = *p++ ^ 0xff; | |
741 | q[0] = q[1] = q[2] = -(v >> 7); q += 3; | |
742 | q[0] = q[1] = q[2] = -((v >> 6) & 1); q += 3; | |
743 | q[0] = q[1] = q[2] = -((v >> 5) & 1); q += 3; | |
744 | q[0] = q[1] = q[2] = -((v >> 4) & 1); q += 3; | |
745 | q[0] = q[1] = q[2] = -((v >> 3) & 1); q += 3; | |
746 | q[0] = q[1] = q[2] = -((v >> 2) & 1); q += 3; | |
747 | q[0] = q[1] = q[2] = -((v >> 1) & 1); q += 3; | |
748 | q[0] = q[1] = q[2] = -((v >> 0) & 1); q += 3; | |
749 | w -= 8; | |
750 | } | |
751 | if (w > 0) { | |
752 | v = *p++ ^ 0xff; | |
753 | do { | |
754 | q[0] = q[1] = q[2] = -((v >> 7) & 1); q += 3; | |
755 | v <<= 1; | |
756 | } while (--w); | |
85c242d8 | 757 | } |
524c6b63 FB |
758 | p += src_wrap; |
759 | q += dst_wrap; | |
85c242d8 FB |
760 | } |
761 | } | |
762 | ||
524c6b63 FB |
763 | static void monoblack_to_rgb24(AVPicture *dst, AVPicture *src, |
764 | int width, int height) | |
765 | { | |
766 | const unsigned char *p; | |
767 | unsigned char *q; | |
768 | int v, dst_wrap, src_wrap; | |
769 | int y, w; | |
770 | ||
771 | p = src->data[0]; | |
772 | src_wrap = src->linesize[0] - ((width + 7) >> 3); | |
773 | ||
774 | q = dst->data[0]; | |
775 | dst_wrap = dst->linesize[0] - 3 * width; | |
776 | ||
777 | for(y=0;y<height;y++) { | |
778 | w = width; | |
779 | while (w >= 8) { | |
780 | v = *p++; | |
781 | q[0] = q[1] = q[2] = -(v >> 7); q += 3; | |
782 | q[0] = q[1] = q[2] = -((v >> 6) & 1); q += 3; | |
783 | q[0] = q[1] = q[2] = -((v >> 5) & 1); q += 3; | |
784 | q[0] = q[1] = q[2] = -((v >> 4) & 1); q += 3; | |
785 | q[0] = q[1] = q[2] = -((v >> 3) & 1); q += 3; | |
786 | q[0] = q[1] = q[2] = -((v >> 2) & 1); q += 3; | |
787 | q[0] = q[1] = q[2] = -((v >> 1) & 1); q += 3; | |
788 | q[0] = q[1] = q[2] = -((v >> 0) & 1); q += 3; | |
789 | w -= 8; | |
790 | } | |
791 | if (w > 0) { | |
792 | v = *p++; | |
793 | do { | |
794 | q[0] = q[1] = q[2] = -((v >> 7) & 1); q += 3; | |
795 | v <<= 1; | |
796 | } while (--w); | |
797 | } | |
798 | p += src_wrap; | |
799 | q += dst_wrap; | |
800 | } | |
801 | } | |
802 | ||
803 | typedef struct ConvertEntry { | |
804 | void (*convert)(AVPicture *dst, AVPicture *src, int width, int height); | |
805 | } ConvertEntry; | |
806 | ||
807 | /* add each new convertion function in this table */ | |
808 | /* constraints; | |
809 | - all non YUV modes must convert at least to and from PIX_FMT_RGB24 | |
810 | */ | |
811 | static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = { | |
812 | [PIX_FMT_YUV420P] = { | |
813 | [PIX_FMT_RGB555] = { | |
814 | convert: yuv420p_to_rgb555 | |
815 | }, | |
816 | [PIX_FMT_RGB565] = { | |
817 | convert: yuv420p_to_rgb565 | |
818 | }, | |
819 | [PIX_FMT_BGR24] = { | |
820 | convert: yuv420p_to_bgr24 | |
821 | }, | |
822 | [PIX_FMT_RGB24] = { | |
823 | convert: yuv420p_to_rgb24 | |
824 | }, | |
825 | [PIX_FMT_RGBA32] = { | |
826 | convert: yuv420p_to_rgba32 | |
827 | }, | |
828 | }, | |
829 | [PIX_FMT_YUV422P] = { | |
830 | [PIX_FMT_RGB555] = { | |
831 | convert: yuv422p_to_rgb555 | |
832 | }, | |
833 | [PIX_FMT_RGB565] = { | |
834 | convert: yuv422p_to_rgb565 | |
835 | }, | |
836 | [PIX_FMT_BGR24] = { | |
837 | convert: yuv422p_to_bgr24 | |
838 | }, | |
839 | [PIX_FMT_RGB24] = { | |
840 | convert: yuv422p_to_rgb24 | |
841 | }, | |
842 | [PIX_FMT_RGBA32] = { | |
843 | convert: yuv422p_to_rgba32 | |
844 | }, | |
845 | }, | |
846 | [PIX_FMT_YUV422] = { | |
847 | [PIX_FMT_YUV420P] = { | |
848 | convert: yuv422_to_yuv420p, | |
849 | }, | |
850 | }, | |
851 | ||
852 | [PIX_FMT_RGB24] = { | |
853 | [PIX_FMT_YUV420P] = { | |
854 | convert: rgb24_to_yuv420p | |
855 | }, | |
856 | [PIX_FMT_RGB565] = { | |
857 | convert: rgb24_to_rgb565 | |
858 | }, | |
859 | [PIX_FMT_RGB555] = { | |
860 | convert: rgb24_to_rgb555 | |
861 | }, | |
862 | [PIX_FMT_GRAY8] = { | |
863 | convert: rgb24_to_gray | |
864 | }, | |
865 | }, | |
866 | [PIX_FMT_RGBA32] = { | |
867 | [PIX_FMT_YUV420P] = { | |
868 | convert: rgba32_to_yuv420p | |
869 | }, | |
870 | }, | |
871 | [PIX_FMT_BGR24] = { | |
872 | [PIX_FMT_YUV420P] = { | |
873 | convert: bgr24_to_yuv420p | |
874 | }, | |
875 | }, | |
876 | [PIX_FMT_RGB555] = { | |
877 | [PIX_FMT_YUV420P] = { | |
878 | convert: rgb555_to_yuv420p | |
879 | }, | |
880 | }, | |
881 | [PIX_FMT_RGB565] = { | |
882 | [PIX_FMT_YUV420P] = { | |
883 | convert: rgb565_to_yuv420p | |
884 | }, | |
885 | }, | |
886 | [PIX_FMT_GRAY8] = { | |
887 | [PIX_FMT_RGB24] = { | |
888 | convert: gray_to_rgb24 | |
889 | }, | |
890 | }, | |
891 | [PIX_FMT_MONOWHITE] = { | |
892 | [PIX_FMT_RGB24] = { | |
893 | convert: monowhite_to_rgb24 | |
894 | }, | |
895 | }, | |
896 | [PIX_FMT_MONOBLACK] = { | |
897 | [PIX_FMT_RGB24] = { | |
898 | convert: monoblack_to_rgb24 | |
899 | }, | |
900 | }, | |
901 | }; | |
902 | ||
903 | static int avpicture_alloc(AVPicture *picture, | |
904 | int pix_fmt, int width, int height) | |
905 | { | |
906 | int size; | |
907 | void *ptr; | |
908 | ||
909 | size = avpicture_get_size(pix_fmt, width, height); | |
910 | if (size < 0) | |
911 | goto fail; | |
912 | ptr = av_malloc(size); | |
913 | if (!ptr) | |
914 | goto fail; | |
915 | avpicture_fill(picture, ptr, pix_fmt, width, height); | |
916 | return 0; | |
917 | fail: | |
918 | memset(picture, 0, sizeof(AVPicture)); | |
919 | return -1; | |
920 | } | |
921 | ||
922 | static void avpicture_free(AVPicture *picture) | |
923 | { | |
924 | free(picture->data[0]); | |
925 | } | |
926 | ||
85c242d8 FB |
927 | /* XXX: always use linesize. Return -1 if not supported */ |
928 | int img_convert(AVPicture *dst, int dst_pix_fmt, | |
524c6b63 FB |
929 | AVPicture *src, int src_pix_fmt, |
930 | int src_width, int src_height) | |
85c242d8 | 931 | { |
524c6b63 FB |
932 | int i, ret, dst_width, dst_height; |
933 | PixFmtInfo *src_pix, *dst_pix; | |
934 | ConvertEntry *ce; | |
935 | AVPicture tmp1, *tmp = &tmp1; | |
936 | ||
937 | if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB || | |
938 | dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB) | |
939 | return -1; | |
940 | if (src_width <= 0 || src_height <= 0) | |
941 | return 0; | |
942 | ||
943 | dst_width = src_width; | |
944 | dst_height = src_height; | |
945 | ||
946 | dst_pix = &pix_fmt_info[dst_pix_fmt]; | |
947 | src_pix = &pix_fmt_info[src_pix_fmt]; | |
948 | if (src_pix_fmt == dst_pix_fmt) { | |
949 | /* same format: just copy */ | |
950 | for(i = 0; i < dst_pix->nb_components; i++) { | |
951 | int w, h; | |
952 | w = dst_width; | |
953 | h = dst_height; | |
954 | if (dst_pix->is_yuv && (i == 1 || i == 2)) { | |
955 | w >>= dst_pix->x_chroma_shift; | |
956 | h >>= dst_pix->y_chroma_shift; | |
85c242d8 | 957 | } |
524c6b63 FB |
958 | img_copy(dst->data[i], dst->linesize[i], |
959 | src->data[i], src->linesize[i], | |
960 | w, h); | |
85c242d8 | 961 | } |
524c6b63 FB |
962 | return 0; |
963 | } | |
964 | ||
965 | ce = &convert_table[src_pix_fmt][dst_pix_fmt]; | |
966 | if (ce->convert) { | |
967 | /* specific convertion routine */ | |
968 | ce->convert(dst, src, dst_width, dst_height); | |
969 | return 0; | |
970 | } | |
971 | ||
972 | /* if both format are not YUV, try to use RGB24 as common | |
973 | format */ | |
974 | if (!dst_pix->is_yuv && !src_pix->is_yuv) { | |
975 | if (avpicture_alloc(tmp, PIX_FMT_RGB24, dst_width, dst_height) < 0) | |
b71472eb | 976 | return -1; |
524c6b63 FB |
977 | ret = -1; |
978 | if (img_convert(tmp, PIX_FMT_RGB24, | |
979 | src, src_pix_fmt, src_width, src_height) < 0) | |
980 | goto fail1; | |
981 | if (img_convert(dst, dst_pix_fmt, | |
982 | tmp, PIX_FMT_RGB24, dst_width, dst_height) < 0) | |
983 | goto fail1; | |
984 | ret = 0; | |
985 | fail1: | |
986 | avpicture_free(tmp); | |
987 | return ret; | |
988 | } | |
989 | ||
990 | /* gray to YUV */ | |
991 | if (dst_pix->is_yuv && src_pix_fmt == PIX_FMT_GRAY8) { | |
992 | int w, h, y; | |
993 | uint8_t *d; | |
994 | ||
995 | img_copy(dst->data[0], dst->linesize[0], | |
996 | src->data[0], src->linesize[0], | |
997 | dst_width, dst_height); | |
998 | /* fill U and V with 128 */ | |
999 | w = dst_width; | |
1000 | h = dst_height; | |
1001 | w >>= dst_pix->x_chroma_shift; | |
1002 | h >>= dst_pix->y_chroma_shift; | |
1003 | for(i = 1; i <= 2; i++) { | |
1004 | d = dst->data[i]; | |
1005 | for(y = 0; y<h; y++) { | |
1006 | memset(d, 128, 0); | |
1007 | d += dst->linesize[i]; | |
1008 | } | |
b71472eb | 1009 | } |
524c6b63 FB |
1010 | return 0; |
1011 | } | |
1012 | ||
1013 | /* YUV to gray */ | |
1014 | if (src_pix->is_yuv && dst_pix_fmt == PIX_FMT_GRAY8) { | |
1015 | img_copy(dst->data[0], dst->linesize[0], | |
1016 | src->data[0], src->linesize[0], | |
1017 | dst_width, dst_height); | |
1018 | return 0; | |
1019 | } | |
1020 | ||
1021 | /* YUV to YUV */ | |
1022 | if (dst_pix->is_yuv && src_pix->is_yuv) { | |
1023 | int x_shift, y_shift, w, h; | |
1024 | void (*resize_func)(UINT8 *dst, int dst_wrap, | |
1025 | UINT8 *src, int src_wrap, | |
1026 | int width, int height); | |
1027 | ||
1028 | /* compute chroma size of the smallest dimensions */ | |
1029 | w = dst_width; | |
1030 | h = dst_height; | |
1031 | if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift) | |
1032 | w >>= dst_pix->x_chroma_shift; | |
1033 | else | |
1034 | w >>= src_pix->x_chroma_shift; | |
1035 | if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift) | |
1036 | h >>= dst_pix->y_chroma_shift; | |
1037 | else | |
1038 | h >>= src_pix->y_chroma_shift; | |
1039 | ||
1040 | x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift); | |
1041 | y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift); | |
1042 | if (x_shift == 0 && y_shift == 0) { | |
1043 | resize_func = img_copy; /* should never happen */ | |
1044 | } else if (x_shift == 0 && y_shift == 1) { | |
1045 | resize_func = shrink2; | |
1046 | } else if (x_shift == 1 && y_shift == 1) { | |
1047 | resize_func = shrink22; | |
1048 | } else if (x_shift == -1 && y_shift == -1) { | |
1049 | resize_func = grow22; | |
1050 | } else if (x_shift == -1 && y_shift == 1) { | |
1051 | resize_func = conv411; | |
1052 | } else { | |
1053 | /* currently not handled */ | |
85c242d8 FB |
1054 | return -1; |
1055 | } | |
524c6b63 FB |
1056 | |
1057 | img_copy(dst->data[0], dst->linesize[0], | |
1058 | src->data[0], src->linesize[0], | |
1059 | dst_width, dst_height); | |
1060 | for(i = 1;i <= 2; i++) | |
1061 | resize_func(dst->data[1], dst->linesize[1], | |
1062 | src->data[1], src->linesize[1], | |
1063 | w, h); | |
85c242d8 | 1064 | } |
524c6b63 FB |
1065 | |
1066 | /* cannot convert yet */ | |
1067 | ||
1068 | return -1; | |
85c242d8 FB |
1069 | } |
1070 | ||
5981f4e6 F |
1071 | |
1072 | #ifdef HAVE_MMX | |
1073 | #define DEINT_INPLACE_LINE_LUM \ | |
1074 | movd_m2r(lum_m4[0],mm0);\ | |
1075 | movd_m2r(lum_m3[0],mm1);\ | |
1076 | movd_m2r(lum_m2[0],mm2);\ | |
1077 | movd_m2r(lum_m1[0],mm3);\ | |
1078 | movd_m2r(lum[0],mm4);\ | |
1079 | punpcklbw_r2r(mm7,mm0);\ | |
1080 | movd_r2m(mm2,lum_m4[0]);\ | |
1081 | punpcklbw_r2r(mm7,mm1);\ | |
1082 | punpcklbw_r2r(mm7,mm2);\ | |
1083 | punpcklbw_r2r(mm7,mm3);\ | |
1084 | punpcklbw_r2r(mm7,mm4);\ | |
1085 | paddw_r2r(mm3,mm1);\ | |
1086 | psllw_i2r(1,mm2);\ | |
1087 | paddw_r2r(mm4,mm0);\ | |
1088 | psllw_i2r(2,mm1);\ | |
1089 | paddw_r2r(mm6,mm2);\ | |
1090 | paddw_r2r(mm2,mm1);\ | |
1091 | psubusw_r2r(mm0,mm1);\ | |
1092 | psrlw_i2r(3,mm1);\ | |
1093 | packuswb_r2r(mm7,mm1);\ | |
1094 | movd_r2m(mm1,lum_m2[0]); | |
1095 | ||
1096 | #define DEINT_LINE_LUM \ | |
1097 | movd_m2r(lum_m4[0],mm0);\ | |
1098 | movd_m2r(lum_m3[0],mm1);\ | |
1099 | movd_m2r(lum_m2[0],mm2);\ | |
1100 | movd_m2r(lum_m1[0],mm3);\ | |
1101 | movd_m2r(lum[0],mm4);\ | |
1102 | punpcklbw_r2r(mm7,mm0);\ | |
1103 | punpcklbw_r2r(mm7,mm1);\ | |
1104 | punpcklbw_r2r(mm7,mm2);\ | |
1105 | punpcklbw_r2r(mm7,mm3);\ | |
1106 | punpcklbw_r2r(mm7,mm4);\ | |
1107 | paddw_r2r(mm3,mm1);\ | |
1108 | psllw_i2r(1,mm2);\ | |
1109 | paddw_r2r(mm4,mm0);\ | |
1110 | psllw_i2r(2,mm1);\ | |
1111 | paddw_r2r(mm6,mm2);\ | |
1112 | paddw_r2r(mm2,mm1);\ | |
1113 | psubusw_r2r(mm0,mm1);\ | |
1114 | psrlw_i2r(3,mm1);\ | |
1115 | packuswb_r2r(mm7,mm1);\ | |
1116 | movd_r2m(mm1,dst[0]); | |
1117 | #endif | |
1118 | ||
85c242d8 | 1119 | /* filter parameters: [-1 4 2 4 -1] // 8 */ |
5981f4e6 F |
1120 | static void deinterlace_line(UINT8 *dst, UINT8 *lum_m4, UINT8 *lum_m3, UINT8 *lum_m2, UINT8 *lum_m1, UINT8 *lum, |
1121 | int size) | |
85c242d8 | 1122 | { |
5981f4e6 | 1123 | #ifndef HAVE_MMX |
85c242d8 FB |
1124 | UINT8 *cm = cropTbl + MAX_NEG_CROP; |
1125 | int sum; | |
85c242d8 FB |
1126 | |
1127 | for(;size > 0;size--) { | |
5981f4e6 F |
1128 | sum = -lum_m4[0]; |
1129 | sum += lum_m3[0] << 2; | |
1130 | sum += lum_m2[0] << 1; | |
1131 | sum += lum_m1[0] << 2; | |
1132 | sum += -lum[0]; | |
85c242d8 | 1133 | dst[0] = cm[(sum + 4) >> 3]; |
5981f4e6 F |
1134 | lum_m4++; |
1135 | lum_m3++; | |
1136 | lum_m2++; | |
1137 | lum_m1++; | |
1138 | lum++; | |
85c242d8 | 1139 | dst++; |
85c242d8 | 1140 | } |
5981f4e6 F |
1141 | #else |
1142 | ||
1143 | for (;size > 3; size-=4) { | |
1144 | DEINT_LINE_LUM | |
1145 | lum_m4+=4; | |
1146 | lum_m3+=4; | |
1147 | lum_m2+=4; | |
1148 | lum_m1+=4; | |
1149 | lum+=4; | |
1150 | dst+=4; | |
1151 | } | |
1152 | #endif | |
1153 | } | |
1154 | static void deinterlace_line_inplace(UINT8 *lum_m4, UINT8 *lum_m3, UINT8 *lum_m2, UINT8 *lum_m1, UINT8 *lum, | |
1155 | int size) | |
1156 | { | |
1157 | #ifndef HAVE_MMX | |
1158 | UINT8 *cm = cropTbl + MAX_NEG_CROP; | |
1159 | int sum; | |
1160 | ||
1161 | for(;size > 0;size--) { | |
1162 | sum = -lum_m4[0]; | |
1163 | sum += lum_m3[0] << 2; | |
1164 | sum += lum_m2[0] << 1; | |
1165 | lum_m4[0]=lum_m2[0]; | |
1166 | sum += lum_m1[0] << 2; | |
1167 | sum += -lum[0]; | |
1168 | lum_m2[0] = cm[(sum + 4) >> 3]; | |
1169 | lum_m4++; | |
1170 | lum_m3++; | |
1171 | lum_m2++; | |
1172 | lum_m1++; | |
1173 | lum++; | |
1174 | } | |
1175 | #else | |
1176 | ||
1177 | for (;size > 3; size-=4) { | |
1178 | DEINT_INPLACE_LINE_LUM | |
1179 | lum_m4+=4; | |
1180 | lum_m3+=4; | |
1181 | lum_m2+=4; | |
1182 | lum_m1+=4; | |
1183 | lum+=4; | |
1184 | } | |
1185 | #endif | |
85c242d8 FB |
1186 | } |
1187 | ||
1188 | /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The | |
1189 | top field is copied as is, but the bottom field is deinterlaced | |
1190 | against the top field. */ | |
1191 | static void deinterlace_bottom_field(UINT8 *dst, int dst_wrap, | |
5981f4e6 F |
1192 | UINT8 *src1, int src_wrap, |
1193 | int width, int height) | |
85c242d8 | 1194 | { |
5981f4e6 F |
1195 | UINT8 *src_m2, *src_m1, *src_0, *src_p1, *src_p2; |
1196 | int y; | |
1197 | ||
1198 | src_m2 = src1; | |
1199 | src_m1 = src1; | |
1200 | src_0=&src_m1[src_wrap]; | |
1201 | src_p1=&src_0[src_wrap]; | |
1202 | src_p2=&src_p1[src_wrap]; | |
1203 | for(y=0;y<(height-2);y+=2) { | |
1204 | memcpy(dst,src_m1,width); | |
85c242d8 | 1205 | dst += dst_wrap; |
5981f4e6 F |
1206 | deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width); |
1207 | src_m2 = src_0; | |
1208 | src_m1 = src_p1; | |
1209 | src_0 = src_p2; | |
1210 | src_p1 += 2*src_wrap; | |
1211 | src_p2 += 2*src_wrap; | |
85c242d8 | 1212 | dst += dst_wrap; |
85c242d8 | 1213 | } |
5981f4e6 F |
1214 | memcpy(dst,src_m1,width); |
1215 | dst += dst_wrap; | |
1216 | /* do last line */ | |
1217 | deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width); | |
1218 | } | |
1219 | ||
1220 | static void deinterlace_bottom_field_inplace(UINT8 *src1, int src_wrap, | |
1221 | int width, int height) | |
1222 | { | |
1223 | UINT8 *src_m1, *src_0, *src_p1, *src_p2; | |
1224 | int y; | |
1225 | UINT8 *buf; | |
1226 | buf = (UINT8*)av_malloc(width); | |
1227 | ||
1228 | src_m1 = src1; | |
1229 | memcpy(buf,src_m1,width); | |
1230 | src_0=&src_m1[src_wrap]; | |
1231 | src_p1=&src_0[src_wrap]; | |
1232 | src_p2=&src_p1[src_wrap]; | |
1233 | for(y=0;y<(height-2);y+=2) { | |
1234 | deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width); | |
1235 | src_m1 = src_p1; | |
1236 | src_0 = src_p2; | |
1237 | src_p1 += 2*src_wrap; | |
1238 | src_p2 += 2*src_wrap; | |
1239 | } | |
1240 | /* do last line */ | |
1241 | deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width); | |
6000abfa | 1242 | av_free(buf); |
85c242d8 FB |
1243 | } |
1244 | ||
1245 | ||
5981f4e6 | 1246 | /* deinterlace - if not supported return -1 */ |
85c242d8 | 1247 | int avpicture_deinterlace(AVPicture *dst, AVPicture *src, |
de6d9b64 FB |
1248 | int pix_fmt, int width, int height) |
1249 | { | |
85c242d8 FB |
1250 | int i; |
1251 | ||
1252 | if (pix_fmt != PIX_FMT_YUV420P && | |
1253 | pix_fmt != PIX_FMT_YUV422P && | |
1254 | pix_fmt != PIX_FMT_YUV444P) | |
1255 | return -1; | |
5981f4e6 | 1256 | if ((width & 3) != 0 || (height & 3) != 0) |
85c242d8 | 1257 | return -1; |
5981f4e6 F |
1258 | |
1259 | #ifdef HAVE_MMX | |
1260 | { | |
1261 | mmx_t rounder; | |
1262 | rounder.uw[0]=4; | |
1263 | rounder.uw[1]=4; | |
1264 | rounder.uw[2]=4; | |
1265 | rounder.uw[3]=4; | |
1266 | pxor_r2r(mm7,mm7); | |
1267 | movq_m2r(rounder,mm6); | |
1268 | } | |
1269 | #endif | |
1270 | ||
85c242d8 FB |
1271 | |
1272 | for(i=0;i<3;i++) { | |
1273 | if (i == 1) { | |
1274 | switch(pix_fmt) { | |
1275 | case PIX_FMT_YUV420P: | |
1276 | width >>= 1; | |
1277 | height >>= 1; | |
1278 | break; | |
1279 | case PIX_FMT_YUV422P: | |
1280 | width >>= 1; | |
1281 | break; | |
1282 | default: | |
1283 | break; | |
1284 | } | |
1285 | } | |
5981f4e6 F |
1286 | if (src == dst) { |
1287 | deinterlace_bottom_field_inplace(src->data[i], src->linesize[i], | |
85c242d8 | 1288 | width, height); |
5981f4e6 F |
1289 | } else { |
1290 | deinterlace_bottom_field(dst->data[i],dst->linesize[i], | |
1291 | src->data[i], src->linesize[i], | |
1292 | width, height); | |
1293 | } | |
de6d9b64 | 1294 | } |
5981f4e6 F |
1295 | #ifdef HAVE_MMX |
1296 | emms(); | |
1297 | #endif | |
85c242d8 | 1298 | return 0; |
de6d9b64 | 1299 | } |
cd4af68a ZK |
1300 | |
1301 | #undef FIX |