almost exhaustive image conversion support
[libav.git] / libavcodec / imgconvert.c
CommitLineData
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 */
983e3246
MN
19
20/**
1ab3d669 21 * @file imgconvert.c
983e3246
MN
22 * Misc image convertion routines.
23 */
24
c50c0bc8
FB
25/* TODO:
26 * - write 'ffimg' program to test all the image related stuff
27 * - move all api to slice based system
28 * - integrate deinterlacing, postprocessing and scaling in the conversion process
29 */
983e3246 30
de6d9b64 31#include "avcodec.h"
85c242d8 32#include "dsputil.h"
de6d9b64 33
54329dd5
NK
34#ifdef USE_FASTMEMCPY
35#include "fastmemcpy.h"
36#endif
5981f4e6
F
37
38#ifdef HAVE_MMX
39#include "i386/mmx.h"
40#endif
524c6b63 41
7e7e5940
FB
42#define xglue(x, y) x ## y
43#define glue(x, y) xglue(x, y)
44
b6147995
FB
45#define FF_COLOR_RGB 0 /* RGB color space */
46#define FF_COLOR_GRAY 1 /* gray color space */
47#define FF_COLOR_YUV 2 /* YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
48#define FF_COLOR_YUV_JPEG 3 /* YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
49
7e7e5940
FB
50#define FF_PIXEL_PLANAR 0 /* each channel has one component in AVPicture */
51#define FF_PIXEL_PACKED 1 /* only one components containing all the channels */
52#define FF_PIXEL_PALETTE 2 /* one components containing indexes for a palette */
53
524c6b63
FB
54typedef struct PixFmtInfo {
55 const char *name;
7e7e5940
FB
56 uint8_t nb_channels; /* number of channels (including alpha) */
57 uint8_t color_type; /* color type (see FF_COLOR_xxx constants) */
58 uint8_t pixel_type; /* pixel storage type (see FF_PIXEL_xxx constants) */
0c1a9eda 59 uint8_t is_alpha : 1; /* true if alpha can be specified */
7e7e5940
FB
60 uint8_t x_chroma_shift; /* X chroma subsampling factor is 2 ^ shift */
61 uint8_t y_chroma_shift; /* Y chroma subsampling factor is 2 ^ shift */
62 uint8_t depth; /* bit depth of the color components */
524c6b63
FB
63} PixFmtInfo;
64
65/* this table gives more information about formats */
66static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
67 /* YUV formats */
68 [PIX_FMT_YUV420P] = {
ef9f7306 69 .name = "yuv420p",
7e7e5940 70 .nb_channels = 3,
b6147995 71 .color_type = FF_COLOR_YUV,
7e7e5940 72 .pixel_type = FF_PIXEL_PLANAR,
b6147995 73 .depth = 8,
ef9f7306 74 .x_chroma_shift = 1, .y_chroma_shift = 1,
524c6b63
FB
75 },
76 [PIX_FMT_YUV422P] = {
ef9f7306 77 .name = "yuv422p",
7e7e5940 78 .nb_channels = 3,
b6147995 79 .color_type = FF_COLOR_YUV,
7e7e5940 80 .pixel_type = FF_PIXEL_PLANAR,
b6147995 81 .depth = 8,
ef9f7306 82 .x_chroma_shift = 1, .y_chroma_shift = 0,
524c6b63
FB
83 },
84 [PIX_FMT_YUV444P] = {
ef9f7306 85 .name = "yuv444p",
7e7e5940 86 .nb_channels = 3,
b6147995 87 .color_type = FF_COLOR_YUV,
7e7e5940 88 .pixel_type = FF_PIXEL_PLANAR,
b6147995 89 .depth = 8,
ef9f7306 90 .x_chroma_shift = 0, .y_chroma_shift = 0,
524c6b63
FB
91 },
92 [PIX_FMT_YUV422] = {
ef9f7306 93 .name = "yuv422",
7e7e5940 94 .nb_channels = 1,
b6147995 95 .color_type = FF_COLOR_YUV,
7e7e5940 96 .pixel_type = FF_PIXEL_PACKED,
b6147995 97 .depth = 8,
ef9f7306 98 .x_chroma_shift = 1, .y_chroma_shift = 0,
524c6b63
FB
99 },
100 [PIX_FMT_YUV410P] = {
ef9f7306 101 .name = "yuv410p",
7e7e5940 102 .nb_channels = 3,
b6147995 103 .color_type = FF_COLOR_YUV,
7e7e5940 104 .pixel_type = FF_PIXEL_PLANAR,
b6147995 105 .depth = 8,
ef9f7306 106 .x_chroma_shift = 2, .y_chroma_shift = 2,
524c6b63
FB
107 },
108 [PIX_FMT_YUV411P] = {
ef9f7306 109 .name = "yuv411p",
7e7e5940 110 .nb_channels = 3,
b6147995 111 .color_type = FF_COLOR_YUV,
7e7e5940 112 .pixel_type = FF_PIXEL_PLANAR,
b6147995 113 .depth = 8,
ef9f7306 114 .x_chroma_shift = 2, .y_chroma_shift = 0,
524c6b63
FB
115 },
116
b6147995
FB
117 /* JPEG YUV */
118 [PIX_FMT_YUVJ420P] = {
119 .name = "yuvj420p",
7e7e5940 120 .nb_channels = 3,
b6147995 121 .color_type = FF_COLOR_YUV_JPEG,
7e7e5940 122 .pixel_type = FF_PIXEL_PLANAR,
b6147995
FB
123 .depth = 8,
124 .x_chroma_shift = 1, .y_chroma_shift = 1,
125 },
126 [PIX_FMT_YUVJ422P] = {
127 .name = "yuvj422p",
7e7e5940 128 .nb_channels = 3,
b6147995 129 .color_type = FF_COLOR_YUV_JPEG,
7e7e5940 130 .pixel_type = FF_PIXEL_PLANAR,
b6147995
FB
131 .depth = 8,
132 .x_chroma_shift = 1, .y_chroma_shift = 0,
133 },
134 [PIX_FMT_YUVJ444P] = {
135 .name = "yuvj444p",
7e7e5940 136 .nb_channels = 3,
b6147995 137 .color_type = FF_COLOR_YUV_JPEG,
7e7e5940 138 .pixel_type = FF_PIXEL_PLANAR,
b6147995
FB
139 .depth = 8,
140 .x_chroma_shift = 0, .y_chroma_shift = 0,
141 },
142
524c6b63
FB
143 /* RGB formats */
144 [PIX_FMT_RGB24] = {
ef9f7306 145 .name = "rgb24",
7e7e5940 146 .nb_channels = 3,
b6147995 147 .color_type = FF_COLOR_RGB,
7e7e5940 148 .pixel_type = FF_PIXEL_PACKED,
b6147995 149 .depth = 8,
524c6b63
FB
150 },
151 [PIX_FMT_BGR24] = {
ef9f7306 152 .name = "bgr24",
7e7e5940 153 .nb_channels = 3,
b6147995 154 .color_type = FF_COLOR_RGB,
7e7e5940 155 .pixel_type = FF_PIXEL_PACKED,
b6147995 156 .depth = 8,
524c6b63
FB
157 },
158 [PIX_FMT_RGBA32] = {
ef9f7306 159 .name = "rgba32",
7e7e5940 160 .nb_channels = 4, .is_alpha = 1,
b6147995 161 .color_type = FF_COLOR_RGB,
7e7e5940 162 .pixel_type = FF_PIXEL_PACKED,
b6147995 163 .depth = 8,
524c6b63
FB
164 },
165 [PIX_FMT_RGB565] = {
ef9f7306 166 .name = "rgb565",
7e7e5940 167 .nb_channels = 3,
b6147995 168 .color_type = FF_COLOR_RGB,
7e7e5940 169 .pixel_type = FF_PIXEL_PACKED,
b6147995 170 .depth = 5,
524c6b63
FB
171 },
172 [PIX_FMT_RGB555] = {
ef9f7306 173 .name = "rgb555",
7e7e5940 174 .nb_channels = 4, .is_alpha = 1,
b6147995 175 .color_type = FF_COLOR_RGB,
7e7e5940 176 .pixel_type = FF_PIXEL_PACKED,
b6147995 177 .depth = 5,
524c6b63
FB
178 },
179
180 /* gray / mono formats */
181 [PIX_FMT_GRAY8] = {
ef9f7306 182 .name = "gray",
7e7e5940 183 .nb_channels = 1,
b6147995 184 .color_type = FF_COLOR_GRAY,
7e7e5940 185 .pixel_type = FF_PIXEL_PLANAR,
b6147995 186 .depth = 8,
524c6b63
FB
187 },
188 [PIX_FMT_MONOWHITE] = {
ef9f7306 189 .name = "monow",
7e7e5940 190 .nb_channels = 1,
b6147995 191 .color_type = FF_COLOR_GRAY,
7e7e5940 192 .pixel_type = FF_PIXEL_PLANAR,
b6147995 193 .depth = 1,
524c6b63
FB
194 },
195 [PIX_FMT_MONOBLACK] = {
ef9f7306 196 .name = "monob",
7e7e5940 197 .nb_channels = 1,
b6147995 198 .color_type = FF_COLOR_GRAY,
7e7e5940 199 .pixel_type = FF_PIXEL_PLANAR,
b6147995 200 .depth = 1,
524c6b63 201 },
7e6d70d0
FB
202
203 /* paletted formats */
204 [PIX_FMT_PAL8] = {
205 .name = "pal8",
7e7e5940 206 .nb_channels = 4, .is_alpha = 1,
b6147995 207 .color_type = FF_COLOR_RGB,
7e7e5940 208 .pixel_type = FF_PIXEL_PALETTE,
b6147995 209 .depth = 8,
7e6d70d0 210 },
524c6b63
FB
211};
212
213void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
214{
b6147995
FB
215 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
216 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
524c6b63
FB
217}
218
219const char *avcodec_get_pix_fmt_name(int pix_fmt)
220{
221 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
222 return "???";
223 else
224 return pix_fmt_info[pix_fmt].name;
225}
226
2a877875 227/* Picture field are filled with 'ptr' addresses. Also return size */
0c1a9eda 228int avpicture_fill(AVPicture *picture, uint8_t *ptr,
2a877875
FB
229 int pix_fmt, int width, int height)
230{
4c7e8619
FB
231 int size, w2, h2, size2;
232 PixFmtInfo *pinfo;
233
234 pinfo = &pix_fmt_info[pix_fmt];
2a877875
FB
235 size = width * height;
236 switch(pix_fmt) {
237 case PIX_FMT_YUV420P:
4c7e8619
FB
238 case PIX_FMT_YUV422P:
239 case PIX_FMT_YUV444P:
240 case PIX_FMT_YUV410P:
241 case PIX_FMT_YUV411P:
c50c0bc8
FB
242 case PIX_FMT_YUVJ420P:
243 case PIX_FMT_YUVJ422P:
244 case PIX_FMT_YUVJ444P:
4c7e8619
FB
245 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
246 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
247 size2 = w2 * h2;
2a877875
FB
248 picture->data[0] = ptr;
249 picture->data[1] = picture->data[0] + size;
4c7e8619 250 picture->data[2] = picture->data[1] + size2;
2a877875 251 picture->linesize[0] = width;
4c7e8619
FB
252 picture->linesize[1] = w2;
253 picture->linesize[2] = w2;
254 return size + 2 * size2;
2a877875
FB
255 case PIX_FMT_RGB24:
256 case PIX_FMT_BGR24:
257 picture->data[0] = ptr;
258 picture->data[1] = NULL;
259 picture->data[2] = NULL;
260 picture->linesize[0] = width * 3;
261 return size * 3;
2a877875
FB
262 case PIX_FMT_RGBA32:
263 picture->data[0] = ptr;
264 picture->data[1] = NULL;
265 picture->data[2] = NULL;
266 picture->linesize[0] = width * 4;
267 return size * 4;
2a877875
FB
268 case PIX_FMT_RGB555:
269 case PIX_FMT_RGB565:
270 case PIX_FMT_YUV422:
271 picture->data[0] = ptr;
272 picture->data[1] = NULL;
273 picture->data[2] = NULL;
274 picture->linesize[0] = width * 2;
275 return size * 2;
276 case PIX_FMT_GRAY8:
277 picture->data[0] = ptr;
278 picture->data[1] = NULL;
279 picture->data[2] = NULL;
280 picture->linesize[0] = width;
281 return size;
282 case PIX_FMT_MONOWHITE:
283 case PIX_FMT_MONOBLACK:
284 picture->data[0] = ptr;
285 picture->data[1] = NULL;
286 picture->data[2] = NULL;
287 picture->linesize[0] = (width + 7) >> 3;
288 return picture->linesize[0] * height;
7e6d70d0
FB
289 case PIX_FMT_PAL8:
290 size2 = (size + 3) & ~3;
291 picture->data[0] = ptr;
292 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
293 picture->data[2] = NULL;
294 picture->linesize[0] = width;
295 picture->linesize[1] = 4;
296 return size2 + 256 * 4;
2a877875
FB
297 default:
298 picture->data[0] = NULL;
299 picture->data[1] = NULL;
300 picture->data[2] = NULL;
7e6d70d0 301 picture->data[3] = NULL;
2a877875
FB
302 return -1;
303 }
304}
305
306int avpicture_get_size(int pix_fmt, int width, int height)
307{
308 AVPicture dummy_pict;
309 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
310}
311
b6147995
FB
312/**
313 * compute the loss when converting from a pixel format to another
314 */
315int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
316 int has_alpha)
317{
318 const PixFmtInfo *pf, *ps;
319 int loss;
320
321 ps = &pix_fmt_info[src_pix_fmt];
322 pf = &pix_fmt_info[dst_pix_fmt];
323
324 /* compute loss */
325 loss = 0;
326 pf = &pix_fmt_info[dst_pix_fmt];
327 if (pf->depth < ps->depth)
328 loss |= FF_LOSS_DEPTH;
329 if (pf->x_chroma_shift >= ps->x_chroma_shift ||
330 pf->y_chroma_shift >= ps->y_chroma_shift)
331 loss |= FF_LOSS_RESOLUTION;
332 switch(pf->color_type) {
333 case FF_COLOR_RGB:
334 if (ps->color_type != FF_COLOR_RGB &&
335 ps->color_type != FF_COLOR_GRAY)
336 loss |= FF_LOSS_COLORSPACE;
337 break;
338 case FF_COLOR_GRAY:
339 if (ps->color_type != FF_COLOR_GRAY)
340 loss |= FF_LOSS_COLORSPACE;
341 break;
342 case FF_COLOR_YUV:
343 if (ps->color_type != FF_COLOR_YUV)
344 loss |= FF_LOSS_COLORSPACE;
345 break;
346 case FF_COLOR_YUV_JPEG:
347 if (ps->color_type != FF_COLOR_YUV_JPEG &&
348 ps->color_type != FF_COLOR_YUV)
349 loss |= FF_LOSS_COLORSPACE;
350 break;
351 default:
352 /* fail safe test */
353 if (ps->color_type != pf->color_type)
354 loss |= FF_LOSS_COLORSPACE;
355 break;
356 }
357 if (pf->color_type == FF_COLOR_GRAY &&
358 ps->color_type != FF_COLOR_GRAY)
359 loss |= FF_LOSS_CHROMA;
360 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
361 loss |= FF_LOSS_ALPHA;
7e7e5940
FB
362 if (pf->pixel_type == FF_PIXEL_PALETTE &&
363 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
b6147995
FB
364 loss |= FF_LOSS_COLORQUANT;
365 return loss;
366}
367
368static int avg_bits_per_pixel(int pix_fmt)
369{
370 int bits;
371 const PixFmtInfo *pf;
372
373 pf = &pix_fmt_info[pix_fmt];
7e7e5940
FB
374 switch(pf->pixel_type) {
375 case FF_PIXEL_PACKED:
b6147995 376 switch(pix_fmt) {
7e7e5940 377 case PIX_FMT_YUV422:
b6147995
FB
378 case PIX_FMT_RGB565:
379 case PIX_FMT_RGB555:
380 bits = 16;
b6147995
FB
381 break;
382 default:
7e7e5940 383 bits = pf->depth * pf->nb_channels;
b6147995
FB
384 break;
385 }
7e7e5940
FB
386 break;
387 case FF_PIXEL_PLANAR:
388 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
389 bits = pf->depth * pf->nb_channels;
390 } else {
391 bits = pf->depth + ((2 * pf->depth) >>
392 (pf->x_chroma_shift + pf->y_chroma_shift));
393 }
394 break;
395 case FF_PIXEL_PALETTE:
396 bits = 8;
397 break;
398 default:
399 bits = -1;
400 break;
b6147995
FB
401 }
402 return bits;
403}
404
405static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
406 int src_pix_fmt,
407 int has_alpha,
408 int loss_mask)
409{
410 int dist, i, loss, min_dist, dst_pix_fmt;
411
412 /* find exact color match with smallest size */
413 dst_pix_fmt = -1;
414 min_dist = 0x7fffffff;
415 for(i = 0;i < PIX_FMT_NB; i++) {
416 if (pix_fmt_mask & (1 << i)) {
417 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
418 if (loss == 0) {
419 dist = avg_bits_per_pixel(i);
420 if (dist < min_dist) {
421 min_dist = dist;
422 dst_pix_fmt = i;
423 }
424 }
425 }
426 }
427 return dst_pix_fmt;
428}
429
430/**
431 * find best pixel format to convert to. Return -1 if none found
432 */
433int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
434 int has_alpha, int *loss_ptr)
435{
436 int dst_pix_fmt, loss_mask, i;
437 static const int loss_mask_order[] = {
438 ~0, /* no loss first */
439 ~FF_LOSS_ALPHA,
440 ~FF_LOSS_RESOLUTION,
441 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
442 ~FF_LOSS_COLORQUANT,
443 ~FF_LOSS_DEPTH,
444 0,
445 };
446
447 /* try with successive loss */
448 i = 0;
449 for(;;) {
450 loss_mask = loss_mask_order[i++];
451 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
452 has_alpha, loss_mask);
453 if (dst_pix_fmt >= 0)
454 goto found;
455 if (loss_mask == 0)
456 break;
457 }
458 return -1;
459 found:
460 if (loss_ptr)
461 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
462 return dst_pix_fmt;
463}
464
7e7e5940
FB
465static void img_copy_plane(uint8_t *dst, int dst_wrap,
466 uint8_t *src, int src_wrap,
467 int width, int height)
468{
469 for(;height > 0; height--) {
470 memcpy(dst, src, width);
471 dst += dst_wrap;
472 src += src_wrap;
473 }
474}
475
476/* copy image 'src' to 'dst' */
477void img_copy(AVPicture *dst, AVPicture *src,
478 int pix_fmt, int width, int height)
479{
480 int bwidth, bits, i;
481 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
482
483 pf = &pix_fmt_info[pix_fmt];
484 switch(pf->pixel_type) {
485 case FF_PIXEL_PACKED:
486 switch(pix_fmt) {
487 case PIX_FMT_YUV422:
488 case PIX_FMT_RGB565:
489 case PIX_FMT_RGB555:
490 bits = 16;
491 break;
492 default:
493 bits = pf->depth * pf->nb_channels;
494 break;
495 }
496 bwidth = (width * bits + 7) >> 3;
497 img_copy_plane(dst->data[0], dst->linesize[0],
498 src->data[0], src->linesize[0],
499 bwidth, height);
500 break;
501 case FF_PIXEL_PLANAR:
502 for(i = 0; i < pf->nb_channels; i++) {
503 int w, h;
504 w = width;
505 h = height;
506 if (i == 1 || i == 2) {
507 w >>= pf->x_chroma_shift;
508 h >>= pf->y_chroma_shift;
509 }
510 bwidth = (w * pf->depth + 7) >> 3;
511 img_copy_plane(dst->data[i], dst->linesize[i],
512 src->data[i], src->linesize[i],
513 bwidth, h);
514 }
515 break;
516 case FF_PIXEL_PALETTE:
517 img_copy_plane(dst->data[0], dst->linesize[0],
518 src->data[0], src->linesize[0],
519 width, height);
520 /* copy the palette */
521 img_copy_plane(dst->data[1], dst->linesize[1],
522 src->data[1], src->linesize[1],
523 4, 256);
524 break;
525 }
526}
2a877875 527
de6d9b64
FB
528/* XXX: totally non optimized */
529
524c6b63
FB
530static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
531 int width, int height)
de6d9b64 532{
c50c0bc8
FB
533 const uint8_t *p, *p1;
534 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
535 int x;
524c6b63 536
c50c0bc8
FB
537 p1 = src->data[0];
538 lum1 = dst->data[0];
539 cb1 = dst->data[0];
540 cr1 = dst->data[0];
541
542 for(;height >= 2; height -= 2) {
543 p = p1;
544 lum = lum1;
545 cb = cb1;
546 cr = cr1;
de6d9b64 547 for(x=0;x<width;x+=2) {
e78df699
MN
548 lum[0] = p[0];
549 cb[0] = p[1];
550 lum[1] = p[2];
551 cr[0] = p[3];
de6d9b64
FB
552 p += 4;
553 lum += 2;
554 cb++;
555 cr++;
556 }
c50c0bc8
FB
557 p1 += src->linesize[0];
558 lum1 += dst->linesize[0];
559 p = p1;
560 lum = lum1;
de6d9b64 561 for(x=0;x<width;x+=2) {
e78df699
MN
562 lum[0] = p[0];
563 lum[1] = p[2];
de6d9b64
FB
564 p += 4;
565 lum += 2;
566 }
c50c0bc8
FB
567 p1 += src->linesize[0];
568 lum1 += dst->linesize[0];
569 cb1 += dst->linesize[1];
570 cr1 += dst->linesize[2];
571 }
572}
573
574static void yuv422_to_yuv422p(AVPicture *dst, AVPicture *src,
575 int width, int height)
576{
577 const uint8_t *p, *p1;
578 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
579 int w;
580
581 p1 = src->data[0];
582 lum1 = dst->data[0];
583 cb1 = dst->data[0];
584 cr1 = dst->data[0];
585 for(;height >= 2; height -= 2) {
586 p = p1;
587 lum = lum1;
588 cb = cb1;
589 cr = cr1;
590 for(w = width; w >= 2; w -= 2) {
591 lum[0] = p[0];
592 cb[0] = p[1];
593 lum[1] = p[2];
594 cr[0] = p[3];
595 p += 4;
596 lum += 2;
597 cb++;
598 cr++;
599 }
600 p1 += src->linesize[0];
601 lum1 += dst->linesize[0];
602 cb1 += dst->linesize[1];
603 cr1 += dst->linesize[2];
de6d9b64
FB
604 }
605}
606
c50c0bc8
FB
607static void yuv422p_to_yuv422(AVPicture *dst, AVPicture *src,
608 int width, int height)
609{
610 uint8_t *p, *p1;
611 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
612 int w;
613
614 p1 = dst->data[0];
615 lum1 = src->data[0];
616 cb1 = src->data[0];
617 cr1 = src->data[0];
618 for(;height >= 2; height -= 2) {
619 p = p1;
620 lum = lum1;
621 cb = cb1;
622 cr = cr1;
623 for(w = width; w >= 2; w -= 2) {
624 p[0] = lum[0];
625 p[1] = cb[0];
626 p[2] = lum[1];
627 p[3] = cr[0];
628 p += 4;
629 lum += 2;
630 cb++;
631 cr++;
632 }
633 p1 += src->linesize[0];
634 lum1 += dst->linesize[0];
635 cb1 += dst->linesize[1];
636 cr1 += dst->linesize[2];
637 }
638}
639
640#define SCALEBITS 10
de6d9b64 641#define ONE_HALF (1 << (SCALEBITS - 1))
c50c0bc8 642#define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
de6d9b64 643
c50c0bc8
FB
644#define YUV_TO_RGB1_CCIR(cb1, cr1)\
645{\
646 cb = (cb1) - 128;\
647 cr = (cr1) - 128;\
648 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
649 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
650 ONE_HALF;\
651 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
652}
b6147995 653
c50c0bc8
FB
654#define YUV_TO_RGB2_CCIR(r, g, b, y1)\
655{\
656 y = ((y1) - 16) * FIX(255.0/219.0);\
657 r = cm[(y + r_add) >> SCALEBITS];\
658 g = cm[(y + g_add) >> SCALEBITS];\
659 b = cm[(y + b_add) >> SCALEBITS];\
660}
661
662#define YUV_TO_RGB1(cb1, cr1)\
663{\
664 cb = (cb1) - 128;\
665 cr = (cr1) - 128;\
666 r_add = FIX(1.40200) * cr + ONE_HALF;\
667 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
668 b_add = FIX(1.77200) * cb + ONE_HALF;\
669}
b6147995
FB
670
671#define YUV_TO_RGB2(r, g, b, y1)\
672{\
c50c0bc8
FB
673 y = (y1) << SCALEBITS;\
674 r = cm[(y + r_add) >> SCALEBITS];\
675 g = cm[(y + g_add) >> SCALEBITS];\
676 b = cm[(y + b_add) >> SCALEBITS];\
b6147995
FB
677}
678
c50c0bc8 679#define Y_CCIR_TO_JPEG(y)\
b6147995
FB
680 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
681
c50c0bc8 682#define Y_JPEG_TO_CCIR(y)\
b6147995
FB
683 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
684
c50c0bc8
FB
685#define C_CCIR_TO_JPEG(y)\
686 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
687
688/* NOTE: the clamp is really necessary! */
689#define C_JPEG_TO_CCIR(y)\
690({\
691 int __y;\
692 __y = ((((y) - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);\
693 if (__y < 16)\
694 __y = 16;\
695 __y;\
696})
697
b6147995
FB
698#define RGB_TO_Y(r, g, b) \
699((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
700 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
701
c50c0bc8 702#define RGB_TO_U(r1, g1, b1, shift)\
b6147995 703(((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
c50c0bc8 704 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
b6147995 705
c50c0bc8 706#define RGB_TO_V(r1, g1, b1, shift)\
b6147995 707(((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
c50c0bc8 708 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
b6147995
FB
709
710#define RGB_TO_Y_CCIR(r, g, b) \
711((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
712 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
713
c50c0bc8 714#define RGB_TO_U_CCIR(r1, g1, b1, shift)\
b6147995 715(((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
c50c0bc8 716 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
b6147995 717
c50c0bc8 718#define RGB_TO_V_CCIR(r1, g1, b1, shift)\
b6147995 719(((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
c50c0bc8 720 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
b6147995 721
c50c0bc8
FB
722static uint8_t y_ccir_to_jpeg[256];
723static uint8_t y_jpeg_to_ccir[256];
724static uint8_t c_ccir_to_jpeg[256];
725static uint8_t c_jpeg_to_ccir[256];
726
727/* init various conversion tables */
728static void img_convert_init(void)
b6147995 729{
c50c0bc8 730 int i;
b6147995
FB
731 uint8_t *cm = cropTbl + MAX_NEG_CROP;
732
c50c0bc8
FB
733 for(i = 0;i < 256; i++) {
734 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
735 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
736 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
737 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
b6147995
FB
738 }
739}
740
c50c0bc8
FB
741/* apply to each pixel the given table */
742static void img_apply_table(uint8_t *dst, int dst_wrap,
743 const uint8_t *src, int src_wrap,
744 int width, int height, const uint8_t *table1)
b6147995
FB
745{
746 int n;
747 const uint8_t *s;
748 uint8_t *d;
c50c0bc8 749 const uint8_t *table;
b6147995 750
c50c0bc8 751 table = table1;
b6147995
FB
752 for(;height > 0; height--) {
753 s = src;
754 d = dst;
755 n = width;
756 while (n >= 4) {
c50c0bc8
FB
757 d[0] = table[s[0]];
758 d[1] = table[s[1]];
759 d[2] = table[s[2]];
760 d[3] = table[s[3]];
b6147995
FB
761 d += 4;
762 s += 4;
763 n -= 4;
764 }
765 while (n > 0) {
c50c0bc8 766 d[0] = table[s[0]];
b6147995
FB
767 d++;
768 s++;
769 n--;
770 }
771 dst += dst_wrap;
772 src += src_wrap;
773 }
774}
775
85c242d8
FB
776/* XXX: use generic filter ? */
777/* 1x2 -> 1x1 */
0c1a9eda
ZK
778static void shrink2(uint8_t *dst, int dst_wrap,
779 uint8_t *src, int src_wrap,
85c242d8
FB
780 int width, int height)
781{
782 int w;
0c1a9eda 783 uint8_t *s1, *s2, *d;
85c242d8
FB
784
785 for(;height > 0; height--) {
786 s1 = src;
787 s2 = s1 + src_wrap;
788 d = dst;
789 for(w = width;w >= 4; w-=4) {
790 d[0] = (s1[0] + s2[0]) >> 1;
791 d[1] = (s1[1] + s2[1]) >> 1;
792 d[2] = (s1[2] + s2[2]) >> 1;
793 d[3] = (s1[3] + s2[3]) >> 1;
794 s1 += 4;
795 s2 += 4;
796 d += 4;
797 }
798 for(;w > 0; w--) {
799 d[0] = (s1[0] + s2[0]) >> 1;
800 s1++;
801 s2++;
802 d++;
803 }
804 src += 2 * src_wrap;
805 dst += dst_wrap;
806 }
807}
808
809/* 2x2 -> 1x1 */
0c1a9eda
ZK
810static void shrink22(uint8_t *dst, int dst_wrap,
811 uint8_t *src, int src_wrap,
85c242d8
FB
812 int width, int height)
813{
814 int w;
0c1a9eda 815 uint8_t *s1, *s2, *d;
85c242d8
FB
816
817 for(;height > 0; height--) {
818 s1 = src;
819 s2 = s1 + src_wrap;
820 d = dst;
821 for(w = width;w >= 4; w-=4) {
822 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
823 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1;
824 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1;
825 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1;
826 s1 += 8;
827 s2 += 8;
828 d += 4;
829 }
830 for(;w > 0; w--) {
831 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
832 s1 += 2;
833 s2 += 2;
834 d++;
835 }
836 src += 2 * src_wrap;
837 dst += dst_wrap;
838 }
839}
840
6742d95d 841/* 1x1 -> 2x2 */
0c1a9eda
ZK
842static void grow22(uint8_t *dst, int dst_wrap,
843 uint8_t *src, int src_wrap,
6742d95d
FR
844 int width, int height)
845{
846 int w;
0c1a9eda 847 uint8_t *s1, *d;
6742d95d
FR
848
849 for(;height > 0; height--) {
850 s1 = src;
851 d = dst;
852 for(w = width;w >= 4; w-=4) {
853 d[1] = d[0] = s1[0];
854 d[3] = d[2] = s1[1];
855 s1 += 2;
856 d += 4;
857 }
858 for(;w > 0; w--) {
859 d[0] = s1[0];
860 s1 ++;
861 d++;
862 }
863 if (height%2)
864 src += src_wrap;
865 dst += dst_wrap;
866 }
867}
868
524c6b63 869/* 1x2 -> 2x1 */
0c1a9eda
ZK
870static void conv411(uint8_t *dst, int dst_wrap,
871 uint8_t *src, int src_wrap,
789587d5
FB
872 int width, int height)
873{
874 int w, c;
0c1a9eda 875 uint8_t *s1, *s2, *d;
789587d5 876
50643575
MN
877 width>>=1;
878
524c6b63 879 for(;height > 0; height--) {
789587d5
FB
880 s1 = src;
881 s2 = src + src_wrap;
882 d = dst;
883 for(w = width;w > 0; w--) {
884 c = (s1[0] + s2[0]) >> 1;
885 d[0] = c;
886 d[1] = c;
887 s1++;
888 s2++;
889 d += 2;
890 }
891 src += src_wrap * 2;
892 dst += dst_wrap;
893 }
894}
895
7e7e5940
FB
896/* XXX: add jpeg quantize code */
897
898#define TRANSP_INDEX (6*6*6)
899
900/* this is maybe slow, but allows for extensions */
901static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
85c242d8 902{
7e7e5940 903 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
85c242d8
FB
904}
905
7e7e5940
FB
906static void build_rgb_palette(uint8_t *palette, int has_alpha)
907{
908 uint32_t *pal;
909 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
910 int i, r, g, b;
911
912 pal = (uint32_t *)palette;
913 i = 0;
914 for(r = 0; r < 6; r++) {
915 for(g = 0; g < 6; g++) {
916 for(b = 0; b < 6; b++) {
917 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
918 (pal_value[g] << 8) | pal_value[b];
919 }
920 }
921 }
922 if (has_alpha)
923 pal[i++] = 0;
924 while (i < 256)
925 pal[i++] = 0xff000000;
524c6b63
FB
926}
927
928/* copy bit n to bits 0 ... n - 1 */
929static inline unsigned int bitcopy_n(unsigned int a, int n)
b71472eb 930{
524c6b63
FB
931 int mask;
932 mask = (1 << n) - 1;
933 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
934}
935
936/* rgb555 handling */
937
7e7e5940
FB
938#define RGB_NAME rgb555
939
524c6b63
FB
940#define RGB_IN(r, g, b, s)\
941{\
0c1a9eda 942 unsigned int v = ((const uint16_t *)(s))[0];\
524c6b63
FB
943 r = bitcopy_n(v >> (10 - 3), 3);\
944 g = bitcopy_n(v >> (5 - 3), 3);\
945 b = bitcopy_n(v << 3, 3);\
946}
947
7e7e5940 948#define RGBA_IN(r, g, b, a, s)\
524c6b63 949{\
7e7e5940
FB
950 unsigned int v = ((const uint16_t *)(s))[0];\
951 r = bitcopy_n(v >> (10 - 3), 3);\
952 g = bitcopy_n(v >> (5 - 3), 3);\
953 b = bitcopy_n(v << 3, 3);\
954 a = bitcopy_n(v >> 15, 7);\
955}
956
957#define RGBA_OUT(d, r, g, b, a)\
958{\
959 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
960 ((a << 8) & 0x8000);\
524c6b63
FB
961}
962
963#define BPP 2
964
7e7e5940 965#include "imgconvert_template.h"
524c6b63
FB
966
967/* rgb565 handling */
968
7e7e5940
FB
969#define RGB_NAME rgb565
970
524c6b63
FB
971#define RGB_IN(r, g, b, s)\
972{\
0c1a9eda 973 unsigned int v = ((const uint16_t *)(s))[0];\
524c6b63
FB
974 r = bitcopy_n(v >> (11 - 3), 3);\
975 g = bitcopy_n(v >> (5 - 2), 2);\
976 b = bitcopy_n(v << 3, 3);\
977}
978
979#define RGB_OUT(d, r, g, b)\
980{\
0c1a9eda 981 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
524c6b63
FB
982}
983
984#define BPP 2
985
7e7e5940 986#include "imgconvert_template.h"
524c6b63
FB
987
988/* bgr24 handling */
989
7e7e5940
FB
990#define RGB_NAME bgr24
991
524c6b63
FB
992#define RGB_IN(r, g, b, s)\
993{\
994 b = (s)[0];\
995 g = (s)[1];\
996 r = (s)[2];\
997}
998
999#define RGB_OUT(d, r, g, b)\
1000{\
1001 (d)[0] = b;\
1002 (d)[1] = g;\
1003 (d)[2] = r;\
1004}
1005
1006#define BPP 3
1007
7e7e5940 1008#include "imgconvert_template.h"
524c6b63
FB
1009
1010#undef RGB_IN
1011#undef RGB_OUT
1012#undef BPP
1013
1014/* rgb24 handling */
1015
7e7e5940
FB
1016#define RGB_NAME rgb24
1017#define FMT_RGB24
1018
524c6b63
FB
1019#define RGB_IN(r, g, b, s)\
1020{\
1021 r = (s)[0];\
1022 g = (s)[1];\
1023 b = (s)[2];\
1024}
1025
1026#define RGB_OUT(d, r, g, b)\
1027{\
1028 (d)[0] = r;\
1029 (d)[1] = g;\
1030 (d)[2] = b;\
1031}
1032
1033#define BPP 3
1034
7e7e5940 1035#include "imgconvert_template.h"
524c6b63
FB
1036
1037/* rgba32 handling */
1038
7e7e5940
FB
1039#define RGB_NAME rgba32
1040#define FMT_RGBA32
1041
524c6b63
FB
1042#define RGB_IN(r, g, b, s)\
1043{\
0c1a9eda 1044 unsigned int v = ((const uint32_t *)(s))[0];\
524c6b63
FB
1045 r = (v >> 16) & 0xff;\
1046 g = (v >> 8) & 0xff;\
1047 b = v & 0xff;\
1048}
1049
7e7e5940 1050#define RGBA_IN(r, g, b, a, s)\
524c6b63 1051{\
7e7e5940
FB
1052 unsigned int v = ((const uint32_t *)(s))[0];\
1053 a = (v >> 24) & 0xff;\
1054 r = (v >> 16) & 0xff;\
1055 g = (v >> 8) & 0xff;\
1056 b = v & 0xff;\
524c6b63
FB
1057}
1058
7e7e5940
FB
1059#define RGBA_OUT(d, r, g, b, a)\
1060{\
1061 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
b71472eb
PG
1062}
1063
7e7e5940 1064#define BPP 4
524c6b63 1065
7e7e5940 1066#include "imgconvert_template.h"
b71472eb 1067
2a877875
FB
1068static void mono_to_gray(AVPicture *dst, AVPicture *src,
1069 int width, int height, int xor_mask)
524c6b63
FB
1070{
1071 const unsigned char *p;
1072 unsigned char *q;
1073 int v, dst_wrap, src_wrap;
1074 int y, w;
1075
1076 p = src->data[0];
1077 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1078
1079 q = dst->data[0];
2a877875 1080 dst_wrap = dst->linesize[0] - width;
524c6b63
FB
1081 for(y=0;y<height;y++) {
1082 w = width;
1083 while (w >= 8) {
2a877875
FB
1084 v = *p++ ^ xor_mask;
1085 q[0] = -(v >> 7);
1086 q[1] = -((v >> 6) & 1);
1087 q[2] = -((v >> 5) & 1);
1088 q[3] = -((v >> 4) & 1);
1089 q[4] = -((v >> 3) & 1);
1090 q[5] = -((v >> 2) & 1);
1091 q[6] = -((v >> 1) & 1);
1092 q[7] = -((v >> 0) & 1);
524c6b63 1093 w -= 8;
2a877875 1094 q += 8;
524c6b63
FB
1095 }
1096 if (w > 0) {
2a877875 1097 v = *p++ ^ xor_mask;
524c6b63 1098 do {
2a877875
FB
1099 q[0] = -((v >> 7) & 1);
1100 q++;
524c6b63
FB
1101 v <<= 1;
1102 } while (--w);
85c242d8 1103 }
524c6b63
FB
1104 p += src_wrap;
1105 q += dst_wrap;
85c242d8
FB
1106 }
1107}
1108
2a877875 1109static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
524c6b63
FB
1110 int width, int height)
1111{
2a877875
FB
1112 mono_to_gray(dst, src, width, height, 0xff);
1113}
524c6b63 1114
2a877875
FB
1115static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
1116 int width, int height)
1117{
1118 mono_to_gray(dst, src, width, height, 0x00);
1119}
524c6b63 1120
2a877875
FB
1121static void gray_to_mono(AVPicture *dst, AVPicture *src,
1122 int width, int height, int xor_mask)
1123{
1124 int n;
0c1a9eda
ZK
1125 const uint8_t *s;
1126 uint8_t *d;
2a877875
FB
1127 int j, b, v, n1, src_wrap, dst_wrap, y;
1128
1129 s = src->data[0];
1130 src_wrap = src->linesize[0] - width;
1131
1132 d = dst->data[0];
1133 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
524c6b63
FB
1134
1135 for(y=0;y<height;y++) {
2a877875
FB
1136 n = width;
1137 while (n >= 8) {
1138 v = 0;
1139 for(j=0;j<8;j++) {
1140 b = s[0];
1141 s++;
1142 v = (v << 1) | (b >> 7);
1143 }
1144 d[0] = v ^ xor_mask;
1145 d++;
1146 n -= 8;
524c6b63 1147 }
2a877875
FB
1148 if (n > 0) {
1149 n1 = n;
1150 v = 0;
1151 while (n > 0) {
1152 b = s[0];
1153 s++;
1154 v = (v << 1) | (b >> 7);
1155 n--;
1156 }
1157 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1158 d++;
524c6b63 1159 }
2a877875
FB
1160 s += src_wrap;
1161 d += dst_wrap;
524c6b63
FB
1162 }
1163}
1164
2a877875
FB
1165static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
1166 int width, int height)
1167{
1168 gray_to_mono(dst, src, width, height, 0xff);
1169}
1170
1171static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
1172 int width, int height)
1173{
1174 gray_to_mono(dst, src, width, height, 0x00);
1175}
1176
524c6b63
FB
1177typedef struct ConvertEntry {
1178 void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
1179} ConvertEntry;
1180
c50c0bc8
FB
1181/* Add each new convertion function in this table. In order to be able
1182 to convert from any format to any format, the following constraints
1183 must be satisfied:
1184
1185 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1186
1187 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1188
1189 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1190
1191 - all PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1192 PIX_FMT_RGB24.
1193
1194 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
524c6b63
FB
1195*/
1196static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1197 [PIX_FMT_YUV420P] = {
1198 [PIX_FMT_RGB555] = {
ef9f7306 1199 .convert = yuv420p_to_rgb555
524c6b63
FB
1200 },
1201 [PIX_FMT_RGB565] = {
ef9f7306 1202 .convert = yuv420p_to_rgb565
524c6b63
FB
1203 },
1204 [PIX_FMT_BGR24] = {
ef9f7306 1205 .convert = yuv420p_to_bgr24
524c6b63
FB
1206 },
1207 [PIX_FMT_RGB24] = {
ef9f7306 1208 .convert = yuv420p_to_rgb24
524c6b63
FB
1209 },
1210 [PIX_FMT_RGBA32] = {
ef9f7306 1211 .convert = yuv420p_to_rgba32
524c6b63
FB
1212 },
1213 },
c50c0bc8
FB
1214 [PIX_FMT_YUV422P] = {
1215 [PIX_FMT_YUV422] = {
1216 .convert = yuv422p_to_yuv422,
1217 },
1218 },
1219 [PIX_FMT_YUV444P] = {
1220 [PIX_FMT_RGB24] = {
1221 .convert = yuv444p_to_rgb24
1222 },
1223 },
1224 [PIX_FMT_YUVJ420P] = {
524c6b63 1225 [PIX_FMT_RGB555] = {
c50c0bc8 1226 .convert = yuvj420p_to_rgb555
524c6b63
FB
1227 },
1228 [PIX_FMT_RGB565] = {
c50c0bc8 1229 .convert = yuvj420p_to_rgb565
524c6b63
FB
1230 },
1231 [PIX_FMT_BGR24] = {
c50c0bc8 1232 .convert = yuvj420p_to_bgr24
524c6b63
FB
1233 },
1234 [PIX_FMT_RGB24] = {
c50c0bc8 1235 .convert = yuvj420p_to_rgb24
524c6b63
FB
1236 },
1237 [PIX_FMT_RGBA32] = {
c50c0bc8
FB
1238 .convert = yuvj420p_to_rgba32
1239 },
1240 },
1241 [PIX_FMT_YUVJ444P] = {
1242 [PIX_FMT_RGB24] = {
1243 .convert = yuvj444p_to_rgb24
524c6b63
FB
1244 },
1245 },
1246 [PIX_FMT_YUV422] = {
1247 [PIX_FMT_YUV420P] = {
ef9f7306 1248 .convert = yuv422_to_yuv420p,
524c6b63 1249 },
c50c0bc8
FB
1250 [PIX_FMT_YUV422P] = {
1251 .convert = yuv422_to_yuv422p,
1252 },
524c6b63
FB
1253 },
1254
1255 [PIX_FMT_RGB24] = {
1256 [PIX_FMT_YUV420P] = {
ef9f7306 1257 .convert = rgb24_to_yuv420p
524c6b63
FB
1258 },
1259 [PIX_FMT_RGB565] = {
ef9f7306 1260 .convert = rgb24_to_rgb565
524c6b63
FB
1261 },
1262 [PIX_FMT_RGB555] = {
ef9f7306 1263 .convert = rgb24_to_rgb555
524c6b63 1264 },
7e7e5940
FB
1265 [PIX_FMT_RGBA32] = {
1266 .convert = rgb24_to_rgba32
1267 },
1268 [PIX_FMT_BGR24] = {
1269 .convert = rgb24_to_bgr24
1270 },
524c6b63 1271 [PIX_FMT_GRAY8] = {
ef9f7306 1272 .convert = rgb24_to_gray
524c6b63 1273 },
7e7e5940 1274 [PIX_FMT_PAL8] = {
7e6d70d0
FB
1275 .convert = rgb24_to_pal8
1276 },
c50c0bc8
FB
1277 [PIX_FMT_YUV444P] = {
1278 .convert = rgb24_to_yuv444p
1279 },
1280 [PIX_FMT_YUVJ420P] = {
1281 .convert = rgb24_to_yuvj420p
1282 },
1283 [PIX_FMT_YUVJ444P] = {
1284 .convert = rgb24_to_yuvj444p
1285 },
524c6b63
FB
1286 },
1287 [PIX_FMT_RGBA32] = {
7e7e5940
FB
1288 [PIX_FMT_RGB24] = {
1289 .convert = rgba32_to_rgb24
1290 },
1291 [PIX_FMT_RGB555] = {
1292 .convert = rgba32_to_rgb555
1293 },
1294 [PIX_FMT_PAL8] = {
1295 .convert = rgba32_to_pal8
1296 },
524c6b63 1297 [PIX_FMT_YUV420P] = {
ef9f7306 1298 .convert = rgba32_to_yuv420p
524c6b63 1299 },
69572401
FB
1300 [PIX_FMT_GRAY8] = {
1301 .convert = rgba32_to_gray
1302 },
524c6b63
FB
1303 },
1304 [PIX_FMT_BGR24] = {
7e7e5940
FB
1305 [PIX_FMT_RGB24] = {
1306 .convert = bgr24_to_rgb24
1307 },
524c6b63 1308 [PIX_FMT_YUV420P] = {
ef9f7306 1309 .convert = bgr24_to_yuv420p
524c6b63 1310 },
69572401
FB
1311 [PIX_FMT_GRAY8] = {
1312 .convert = bgr24_to_gray
1313 },
524c6b63
FB
1314 },
1315 [PIX_FMT_RGB555] = {
7e7e5940
FB
1316 [PIX_FMT_RGB24] = {
1317 .convert = rgb555_to_rgb24
1318 },
1319 [PIX_FMT_RGBA32] = {
1320 .convert = rgb555_to_rgba32
1321 },
524c6b63 1322 [PIX_FMT_YUV420P] = {
ef9f7306 1323 .convert = rgb555_to_yuv420p
524c6b63 1324 },
69572401
FB
1325 [PIX_FMT_GRAY8] = {
1326 .convert = rgb555_to_gray
1327 },
524c6b63
FB
1328 },
1329 [PIX_FMT_RGB565] = {
7e7e5940
FB
1330 [PIX_FMT_RGB24] = {
1331 .convert = rgb565_to_rgb24
1332 },
524c6b63 1333 [PIX_FMT_YUV420P] = {
ef9f7306 1334 .convert = rgb565_to_yuv420p
524c6b63 1335 },
69572401
FB
1336 [PIX_FMT_GRAY8] = {
1337 .convert = rgb565_to_gray
1338 },
524c6b63
FB
1339 },
1340 [PIX_FMT_GRAY8] = {
69572401
FB
1341 [PIX_FMT_RGB555] = {
1342 .convert = gray_to_rgb555
1343 },
1344 [PIX_FMT_RGB565] = {
1345 .convert = gray_to_rgb565
1346 },
524c6b63 1347 [PIX_FMT_RGB24] = {
ef9f7306 1348 .convert = gray_to_rgb24
524c6b63 1349 },
69572401
FB
1350 [PIX_FMT_BGR24] = {
1351 .convert = gray_to_bgr24
1352 },
1353 [PIX_FMT_RGBA32] = {
1354 .convert = gray_to_rgba32
1355 },
2a877875 1356 [PIX_FMT_MONOWHITE] = {
ef9f7306 1357 .convert = gray_to_monowhite
2a877875
FB
1358 },
1359 [PIX_FMT_MONOBLACK] = {
ef9f7306 1360 .convert = gray_to_monoblack
2a877875 1361 },
524c6b63
FB
1362 },
1363 [PIX_FMT_MONOWHITE] = {
2a877875 1364 [PIX_FMT_GRAY8] = {
ef9f7306 1365 .convert = monowhite_to_gray
524c6b63
FB
1366 },
1367 },
1368 [PIX_FMT_MONOBLACK] = {
2a877875 1369 [PIX_FMT_GRAY8] = {
ef9f7306 1370 .convert = monoblack_to_gray
524c6b63
FB
1371 },
1372 },
7e6d70d0
FB
1373 [PIX_FMT_PAL8] = {
1374 [PIX_FMT_RGB555] = {
1375 .convert = pal8_to_rgb555
1376 },
1377 [PIX_FMT_RGB565] = {
1378 .convert = pal8_to_rgb565
1379 },
1380 [PIX_FMT_BGR24] = {
1381 .convert = pal8_to_bgr24
1382 },
1383 [PIX_FMT_RGB24] = {
1384 .convert = pal8_to_rgb24
1385 },
1386 [PIX_FMT_RGBA32] = {
1387 .convert = pal8_to_rgba32
1388 },
1389 },
524c6b63
FB
1390};
1391
1392static int avpicture_alloc(AVPicture *picture,
1393 int pix_fmt, int width, int height)
1394{
5c91a675 1395 unsigned int size;
524c6b63
FB
1396 void *ptr;
1397
1398 size = avpicture_get_size(pix_fmt, width, height);
1399 if (size < 0)
1400 goto fail;
1401 ptr = av_malloc(size);
1402 if (!ptr)
1403 goto fail;
1404 avpicture_fill(picture, ptr, pix_fmt, width, height);
1405 return 0;
1406 fail:
1407 memset(picture, 0, sizeof(AVPicture));
1408 return -1;
1409}
1410
1411static void avpicture_free(AVPicture *picture)
1412{
8e1e6f31 1413 av_free(picture->data[0]);
524c6b63
FB
1414}
1415
c50c0bc8
FB
1416/* return true if yuv planar */
1417static inline int is_yuv_planar(PixFmtInfo *ps)
1418{
1419 return (ps->color_type == FF_COLOR_YUV ||
7e7e5940
FB
1420 ps->color_type == FF_COLOR_YUV_JPEG) &&
1421 ps->pixel_type == FF_PIXEL_PLANAR;
c50c0bc8
FB
1422}
1423
85c242d8
FB
1424/* XXX: always use linesize. Return -1 if not supported */
1425int img_convert(AVPicture *dst, int dst_pix_fmt,
524c6b63
FB
1426 AVPicture *src, int src_pix_fmt,
1427 int src_width, int src_height)
85c242d8 1428{
c50c0bc8 1429 static int inited;
2a877875 1430 int i, ret, dst_width, dst_height, int_pix_fmt;
524c6b63
FB
1431 PixFmtInfo *src_pix, *dst_pix;
1432 ConvertEntry *ce;
1433 AVPicture tmp1, *tmp = &tmp1;
1434
1435 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1436 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1437 return -1;
1438 if (src_width <= 0 || src_height <= 0)
1439 return 0;
69572401 1440
c50c0bc8
FB
1441 if (!inited) {
1442 inited = 1;
1443 img_convert_init();
1444 }
1445
524c6b63
FB
1446 dst_width = src_width;
1447 dst_height = src_height;
69572401 1448
524c6b63
FB
1449 dst_pix = &pix_fmt_info[dst_pix_fmt];
1450 src_pix = &pix_fmt_info[src_pix_fmt];
1451 if (src_pix_fmt == dst_pix_fmt) {
7e7e5940
FB
1452 /* no conversion needed: just copy */
1453 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
524c6b63
FB
1454 return 0;
1455 }
1456
1457 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1458 if (ce->convert) {
1459 /* specific convertion routine */
1460 ce->convert(dst, src, dst_width, dst_height);
1461 return 0;
1462 }
1463
524c6b63 1464 /* gray to YUV */
c50c0bc8 1465 if (is_yuv_planar(dst_pix) &&
b6147995 1466 src_pix_fmt == PIX_FMT_GRAY8) {
524c6b63
FB
1467 int w, h, y;
1468 uint8_t *d;
1469
b6147995 1470 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
7e7e5940 1471 img_copy_plane(dst->data[0], dst->linesize[0],
b6147995
FB
1472 src->data[0], src->linesize[0],
1473 dst_width, dst_height);
1474 } else {
c50c0bc8
FB
1475 img_apply_table(dst->data[0], dst->linesize[0],
1476 src->data[0], src->linesize[0],
1477 dst_width, dst_height,
1478 y_jpeg_to_ccir);
b6147995 1479 }
524c6b63
FB
1480 /* fill U and V with 128 */
1481 w = dst_width;
1482 h = dst_height;
1483 w >>= dst_pix->x_chroma_shift;
1484 h >>= dst_pix->y_chroma_shift;
1485 for(i = 1; i <= 2; i++) {
1486 d = dst->data[i];
2a877875
FB
1487 for(y = 0; y< h; y++) {
1488 memset(d, 128, w);
524c6b63
FB
1489 d += dst->linesize[i];
1490 }
b71472eb 1491 }
524c6b63
FB
1492 return 0;
1493 }
1494
1495 /* YUV to gray */
c50c0bc8 1496 if (is_yuv_planar(src_pix) &&
b6147995
FB
1497 dst_pix_fmt == PIX_FMT_GRAY8) {
1498 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
7e7e5940 1499 img_copy_plane(dst->data[0], dst->linesize[0],
b6147995
FB
1500 src->data[0], src->linesize[0],
1501 dst_width, dst_height);
1502 } else {
c50c0bc8
FB
1503 img_apply_table(dst->data[0], dst->linesize[0],
1504 src->data[0], src->linesize[0],
1505 dst_width, dst_height,
1506 y_ccir_to_jpeg);
b6147995 1507 }
524c6b63
FB
1508 return 0;
1509 }
1510
c50c0bc8
FB
1511 /* YUV to YUV planar */
1512 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
524c6b63 1513 int x_shift, y_shift, w, h;
0c1a9eda
ZK
1514 void (*resize_func)(uint8_t *dst, int dst_wrap,
1515 uint8_t *src, int src_wrap,
524c6b63
FB
1516 int width, int height);
1517
1518 /* compute chroma size of the smallest dimensions */
1519 w = dst_width;
1520 h = dst_height;
1521 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1522 w >>= dst_pix->x_chroma_shift;
1523 else
1524 w >>= src_pix->x_chroma_shift;
1525 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1526 h >>= dst_pix->y_chroma_shift;
1527 else
1528 h >>= src_pix->y_chroma_shift;
1529
1530 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1531 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
c50c0bc8 1532
524c6b63 1533 if (x_shift == 0 && y_shift == 0) {
7e7e5940 1534 resize_func = img_copy_plane;
524c6b63
FB
1535 } else if (x_shift == 0 && y_shift == 1) {
1536 resize_func = shrink2;
1537 } else if (x_shift == 1 && y_shift == 1) {
1538 resize_func = shrink22;
1539 } else if (x_shift == -1 && y_shift == -1) {
1540 resize_func = grow22;
1541 } else if (x_shift == -1 && y_shift == 1) {
1542 resize_func = conv411;
1543 } else {
1544 /* currently not handled */
85c242d8
FB
1545 return -1;
1546 }
524c6b63 1547
7e7e5940
FB
1548 img_copy_plane(dst->data[0], dst->linesize[0],
1549 src->data[0], src->linesize[0],
1550 dst_width, dst_height);
566986ee 1551
524c6b63 1552 for(i = 1;i <= 2; i++)
566986ee
MK
1553 resize_func(dst->data[i], dst->linesize[i],
1554 src->data[i], src->linesize[i],
185fdc54 1555 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
c50c0bc8
FB
1556 /* if yuv color space conversion is needed, we do it here on
1557 the destination image */
1558 if (dst_pix->color_type != src_pix->color_type) {
1559 const uint8_t *y_table, *c_table;
1560 if (dst_pix->color_type == FF_COLOR_YUV) {
1561 y_table = y_jpeg_to_ccir;
1562 c_table = c_jpeg_to_ccir;
1563 } else {
1564 y_table = y_ccir_to_jpeg;
1565 c_table = c_ccir_to_jpeg;
1566 }
1567 img_apply_table(dst->data[0], dst->linesize[0],
1568 dst->data[0], dst->linesize[0],
1569 dst_width, dst_height,
1570 y_table);
1571
1572 for(i = 1;i <= 2; i++)
1573 img_apply_table(dst->data[i], dst->linesize[i],
1574 dst->data[i], dst->linesize[i],
1575 dst_width>>dst_pix->x_chroma_shift,
1576 dst_height>>dst_pix->y_chroma_shift,
1577 c_table);
1578 }
1579 return 0;
85c242d8 1580 }
524c6b63 1581
2a877875 1582 /* try to use an intermediate format */
c50c0bc8
FB
1583 if (src_pix_fmt == PIX_FMT_YUV422 ||
1584 dst_pix_fmt == PIX_FMT_YUV422) {
1585 /* specific case: convert to YUV422P first */
1586 int_pix_fmt = PIX_FMT_YUV422P;
1587 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
1588 src_pix_fmt != PIX_FMT_GRAY8) ||
1589 (dst_pix->color_type == FF_COLOR_GRAY &&
1590 dst_pix_fmt != PIX_FMT_GRAY8)) {
1591 /* gray8 is the normalized format */
2a877875 1592 int_pix_fmt = PIX_FMT_GRAY8;
c50c0bc8
FB
1593 } else if ((is_yuv_planar(src_pix) &&
1594 src_pix_fmt != PIX_FMT_YUV444P &&
1595 src_pix_fmt != PIX_FMT_YUVJ444P)) {
1596 /* yuv444 is the normalized format */
1597 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
1598 int_pix_fmt = PIX_FMT_YUVJ444P;
1599 else
1600 int_pix_fmt = PIX_FMT_YUV444P;
1601 } else if ((is_yuv_planar(dst_pix) &&
1602 dst_pix_fmt != PIX_FMT_YUV444P &&
1603 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
1604 /* yuv444 is the normalized format */
1605 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
1606 int_pix_fmt = PIX_FMT_YUVJ444P;
1607 else
1608 int_pix_fmt = PIX_FMT_YUV444P;
2a877875 1609 } else {
c50c0bc8
FB
1610 /* the two formats are rgb or gray8 or yuv[j]444p */
1611 if (src_pix->is_alpha && dst_pix->is_alpha)
1612 int_pix_fmt = PIX_FMT_RGBA32;
1613 else
1614 int_pix_fmt = PIX_FMT_RGB24;
2a877875
FB
1615 }
1616 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
1617 return -1;
1618 ret = -1;
1619 if (img_convert(tmp, int_pix_fmt,
1620 src, src_pix_fmt, src_width, src_height) < 0)
1621 goto fail1;
1622 if (img_convert(dst, dst_pix_fmt,
1623 tmp, int_pix_fmt, dst_width, dst_height) < 0)
1624 goto fail1;
1625 ret = 0;
1626 fail1:
1627 avpicture_free(tmp);
1628 return ret;
85c242d8
FB
1629}
1630
5981f4e6
F
1631
1632#ifdef HAVE_MMX
1633#define DEINT_INPLACE_LINE_LUM \
1634 movd_m2r(lum_m4[0],mm0);\
1635 movd_m2r(lum_m3[0],mm1);\
1636 movd_m2r(lum_m2[0],mm2);\
1637 movd_m2r(lum_m1[0],mm3);\
1638 movd_m2r(lum[0],mm4);\
1639 punpcklbw_r2r(mm7,mm0);\
1640 movd_r2m(mm2,lum_m4[0]);\
1641 punpcklbw_r2r(mm7,mm1);\
1642 punpcklbw_r2r(mm7,mm2);\
1643 punpcklbw_r2r(mm7,mm3);\
1644 punpcklbw_r2r(mm7,mm4);\
1645 paddw_r2r(mm3,mm1);\
1646 psllw_i2r(1,mm2);\
1647 paddw_r2r(mm4,mm0);\
1648 psllw_i2r(2,mm1);\
1649 paddw_r2r(mm6,mm2);\
1650 paddw_r2r(mm2,mm1);\
1651 psubusw_r2r(mm0,mm1);\
1652 psrlw_i2r(3,mm1);\
1653 packuswb_r2r(mm7,mm1);\
1654 movd_r2m(mm1,lum_m2[0]);
1655
1656#define DEINT_LINE_LUM \
1657 movd_m2r(lum_m4[0],mm0);\
1658 movd_m2r(lum_m3[0],mm1);\
1659 movd_m2r(lum_m2[0],mm2);\
1660 movd_m2r(lum_m1[0],mm3);\
1661 movd_m2r(lum[0],mm4);\
1662 punpcklbw_r2r(mm7,mm0);\
1663 punpcklbw_r2r(mm7,mm1);\
1664 punpcklbw_r2r(mm7,mm2);\
1665 punpcklbw_r2r(mm7,mm3);\
1666 punpcklbw_r2r(mm7,mm4);\
1667 paddw_r2r(mm3,mm1);\
1668 psllw_i2r(1,mm2);\
1669 paddw_r2r(mm4,mm0);\
1670 psllw_i2r(2,mm1);\
1671 paddw_r2r(mm6,mm2);\
1672 paddw_r2r(mm2,mm1);\
1673 psubusw_r2r(mm0,mm1);\
1674 psrlw_i2r(3,mm1);\
1675 packuswb_r2r(mm7,mm1);\
1676 movd_r2m(mm1,dst[0]);
1677#endif
1678
85c242d8 1679/* filter parameters: [-1 4 2 4 -1] // 8 */
0c1a9eda 1680static void deinterlace_line(uint8_t *dst, uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
5981f4e6 1681 int size)
85c242d8 1682{
5981f4e6 1683#ifndef HAVE_MMX
0c1a9eda 1684 uint8_t *cm = cropTbl + MAX_NEG_CROP;
85c242d8 1685 int sum;
85c242d8
FB
1686
1687 for(;size > 0;size--) {
5981f4e6
F
1688 sum = -lum_m4[0];
1689 sum += lum_m3[0] << 2;
1690 sum += lum_m2[0] << 1;
1691 sum += lum_m1[0] << 2;
1692 sum += -lum[0];
85c242d8 1693 dst[0] = cm[(sum + 4) >> 3];
5981f4e6
F
1694 lum_m4++;
1695 lum_m3++;
1696 lum_m2++;
1697 lum_m1++;
1698 lum++;
85c242d8 1699 dst++;
85c242d8 1700 }
5981f4e6
F
1701#else
1702
782c5984
MN
1703 {
1704 mmx_t rounder;
1705 rounder.uw[0]=4;
1706 rounder.uw[1]=4;
1707 rounder.uw[2]=4;
1708 rounder.uw[3]=4;
1709 pxor_r2r(mm7,mm7);
1710 movq_m2r(rounder,mm6);
1711 }
5981f4e6
F
1712 for (;size > 3; size-=4) {
1713 DEINT_LINE_LUM
1714 lum_m4+=4;
1715 lum_m3+=4;
1716 lum_m2+=4;
1717 lum_m1+=4;
1718 lum+=4;
1719 dst+=4;
1720 }
1721#endif
1722}
0c1a9eda 1723static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
5981f4e6
F
1724 int size)
1725{
1726#ifndef HAVE_MMX
0c1a9eda 1727 uint8_t *cm = cropTbl + MAX_NEG_CROP;
5981f4e6
F
1728 int sum;
1729
1730 for(;size > 0;size--) {
1731 sum = -lum_m4[0];
1732 sum += lum_m3[0] << 2;
1733 sum += lum_m2[0] << 1;
1734 lum_m4[0]=lum_m2[0];
1735 sum += lum_m1[0] << 2;
1736 sum += -lum[0];
1737 lum_m2[0] = cm[(sum + 4) >> 3];
1738 lum_m4++;
1739 lum_m3++;
1740 lum_m2++;
1741 lum_m1++;
1742 lum++;
1743 }
1744#else
1745
782c5984
MN
1746 {
1747 mmx_t rounder;
1748 rounder.uw[0]=4;
1749 rounder.uw[1]=4;
1750 rounder.uw[2]=4;
1751 rounder.uw[3]=4;
1752 pxor_r2r(mm7,mm7);
1753 movq_m2r(rounder,mm6);
1754 }
5981f4e6
F
1755 for (;size > 3; size-=4) {
1756 DEINT_INPLACE_LINE_LUM
1757 lum_m4+=4;
1758 lum_m3+=4;
1759 lum_m2+=4;
1760 lum_m1+=4;
1761 lum+=4;
1762 }
1763#endif
85c242d8
FB
1764}
1765
1766/* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1767 top field is copied as is, but the bottom field is deinterlaced
1768 against the top field. */
0c1a9eda
ZK
1769static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1770 uint8_t *src1, int src_wrap,
5981f4e6 1771 int width, int height)
85c242d8 1772{
0c1a9eda 1773 uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
5981f4e6
F
1774 int y;
1775
1776 src_m2 = src1;
1777 src_m1 = src1;
1778 src_0=&src_m1[src_wrap];
1779 src_p1=&src_0[src_wrap];
1780 src_p2=&src_p1[src_wrap];
1781 for(y=0;y<(height-2);y+=2) {
1782 memcpy(dst,src_m1,width);
85c242d8 1783 dst += dst_wrap;
5981f4e6
F
1784 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1785 src_m2 = src_0;
1786 src_m1 = src_p1;
1787 src_0 = src_p2;
1788 src_p1 += 2*src_wrap;
1789 src_p2 += 2*src_wrap;
85c242d8 1790 dst += dst_wrap;
85c242d8 1791 }
5981f4e6
F
1792 memcpy(dst,src_m1,width);
1793 dst += dst_wrap;
1794 /* do last line */
1795 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1796}
1797
0c1a9eda 1798static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
5981f4e6
F
1799 int width, int height)
1800{
0c1a9eda 1801 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
5981f4e6 1802 int y;
0c1a9eda
ZK
1803 uint8_t *buf;
1804 buf = (uint8_t*)av_malloc(width);
5981f4e6
F
1805
1806 src_m1 = src1;
1807 memcpy(buf,src_m1,width);
1808 src_0=&src_m1[src_wrap];
1809 src_p1=&src_0[src_wrap];
1810 src_p2=&src_p1[src_wrap];
1811 for(y=0;y<(height-2);y+=2) {
1812 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1813 src_m1 = src_p1;
1814 src_0 = src_p2;
1815 src_p1 += 2*src_wrap;
1816 src_p2 += 2*src_wrap;
1817 }
1818 /* do last line */
1819 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
6000abfa 1820 av_free(buf);
85c242d8
FB
1821}
1822
1823
5981f4e6 1824/* deinterlace - if not supported return -1 */
85c242d8 1825int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
de6d9b64
FB
1826 int pix_fmt, int width, int height)
1827{
85c242d8
FB
1828 int i;
1829
1830 if (pix_fmt != PIX_FMT_YUV420P &&
1831 pix_fmt != PIX_FMT_YUV422P &&
1832 pix_fmt != PIX_FMT_YUV444P)
1833 return -1;
5981f4e6 1834 if ((width & 3) != 0 || (height & 3) != 0)
85c242d8 1835 return -1;
5981f4e6 1836
85c242d8
FB
1837 for(i=0;i<3;i++) {
1838 if (i == 1) {
1839 switch(pix_fmt) {
1840 case PIX_FMT_YUV420P:
1841 width >>= 1;
1842 height >>= 1;
1843 break;
1844 case PIX_FMT_YUV422P:
1845 width >>= 1;
1846 break;
1847 default:
1848 break;
1849 }
1850 }
5981f4e6
F
1851 if (src == dst) {
1852 deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
85c242d8 1853 width, height);
5981f4e6
F
1854 } else {
1855 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1856 src->data[i], src->linesize[i],
1857 width, height);
1858 }
de6d9b64 1859 }
5981f4e6
F
1860#ifdef HAVE_MMX
1861 emms();
1862#endif
85c242d8 1863 return 0;
de6d9b64 1864}
cd4af68a
ZK
1865
1866#undef FIX