expand tabs
[platform/upstream/gstreamer.git] / gst / ffmpegcolorspace / imgconvert.c
1 /*
2  * Misc image convertion routines
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
4  *
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.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
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
18  */
19
20 /**
21  * @file imgconvert.c
22  * Misc image convertion routines.
23  */
24
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  */
30
31 #include "avcodec.h"
32 #include "dsputil.h"
33 #include "gstffmpegcodecmap.h"
34
35 #include <string.h>
36 #include <stdlib.h>
37
38 #define xglue(x, y) x ## y
39 #define glue(x, y) xglue(x, y)
40
41 /* this table gives more information about formats */
42 /* FIXME, this table is also in ffmpegcodecmap */
43 static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
44   /* YUV formats */
45   /* [PIX_FMT_YUV420P] = */ {
46         /* .format         = */ PIX_FMT_YUV420P,
47         /* .name           = */ "yuv420p",
48         /* .nb_channels    = */ 3,
49         /* .color_type     = */ FF_COLOR_YUV,
50         /* .pixel_type     = */ FF_PIXEL_PLANAR,
51         /* .is_alpha       = */ 0,
52         /* .x_chroma_shift = */ 1,
53         /* .y_chroma_shift = */ 1,
54         /* .depth          = */ 8,
55       },
56   /* [PIX_FMT_YVU420P] = */ {
57         /* .format         = */ PIX_FMT_YVU420P,
58         /* .name           = */ "yvu420p",
59         /* .nb_channels    = */ 3,
60         /* .color_type     = */ FF_COLOR_YUV,
61         /* .pixel_type     = */ FF_PIXEL_PLANAR,
62         /* .is_alpha       = */ 0,
63         /* .x_chroma_shift = */ 1,
64         /* .y_chroma_shift = */ 1,
65         /* .depth          = */ 8,
66       },
67   /* [PIX_FMT_YUV422P] = */ {
68         /* .format         = */ PIX_FMT_YUV422P,
69         /* .name           = */ "yuv422p",
70         /* .nb_channels    = */ 3,
71         /* .color_type     = */ FF_COLOR_YUV,
72         /* .pixel_type     = */ FF_PIXEL_PLANAR,
73         /* .is_alpha       = */ 0,
74         /* .x_chroma_shift = */ 1,
75         /* .y_chroma_shift = */ 0,
76         /* .depth          = */ 8,
77       },
78   /* [PIX_FMT_YUV444P] = */ {
79         /* .format         = */ PIX_FMT_YUV444P,
80         /* .name           = */ "yuv444p",
81         /* .nb_channels    = */ 3,
82         /* .color_type     = */ FF_COLOR_YUV,
83         /* .pixel_type     = */ FF_PIXEL_PLANAR,
84         /* .is_alpha       = */ 0,
85         /* .x_chroma_shift = */ 0,
86         /* .y_chroma_shift = */ 0,
87         /* .depth          = */ 8,
88       },
89   /* [PIX_FMT_YUV422] = */ {
90         /* .format         = */ PIX_FMT_YUV422,
91         /* .name           = */ "yuv422",
92         /* .nb_channels    = */ 1,
93         /* .color_type     = */ FF_COLOR_YUV,
94         /* .pixel_type     = */ FF_PIXEL_PACKED,
95         /* .is_alpha       = */ 0,
96         /* .x_chroma_shift = */ 1,
97         /* .y_chroma_shift = */ 0,
98         /* .depth          = */ 8,
99       },
100   /* [PIX_FMT_UYVY422] = */ {
101         /* .format         = */ PIX_FMT_UYVY422,
102         /* .name           = */ "uyvy422",
103         /* .nb_channels    = */ 1,
104         /* .color_type     = */ FF_COLOR_YUV,
105         /* .pixel_type     = */ FF_PIXEL_PACKED,
106         /* .is_alpha       = */ 0,
107         /* .x_chroma_shift = */ 1,
108         /* .y_chroma_shift = */ 0,
109         /* .depth          = */ 8,
110       },
111   /* [PIX_FMT_YUV410P] = */ {
112         /* .format         = */ PIX_FMT_YUV410P,
113         /* .name           = */ "yuv410p",
114         /* .nb_channels    = */ 3,
115         /* .color_type     = */ FF_COLOR_YUV,
116         /* .pixel_type     = */ FF_PIXEL_PLANAR,
117         /* .is_alpha       = */ 0,
118         /* .x_chroma_shift = */ 2,
119         /* .y_chroma_shift = */ 2,
120         /* .depth          = */ 8,
121       },
122   /* [PIX_FMT_YUV411P] = */ {
123         /* .format         = */ PIX_FMT_YUV411P,
124         /* .name           = */ "yuv411p",
125         /* .nb_channels    = */ 3,
126         /* .color_type     = */ FF_COLOR_YUV,
127         /* .pixel_type     = */ FF_PIXEL_PLANAR,
128         /* .is_alpha       = */ 0,
129         /* .x_chroma_shift = */ 2,
130         /* .y_chroma_shift = */ 0,
131         /* .depth          = */ 8,
132       },
133
134   /* JPEG YUV */
135   /* [PIX_FMT_YUVJ420P] = */ {
136         /* .format         = */ PIX_FMT_YUVJ420P,
137         /* .name           = */ "yuvj420p",
138         /* .nb_channels    = */ 3,
139         /* .color_type     = */ FF_COLOR_YUV_JPEG,
140         /* .pixel_type     = */ FF_PIXEL_PLANAR,
141         /* .is_alpha       = */ 0,
142         /* .x_chroma_shift = */ 1,
143         /* .y_chroma_shift = */ 1,
144         /* .depth          = */ 8,
145       },
146   /* [PIX_FMT_YUVJ422P] = */ {
147         /* .format         = */ PIX_FMT_YUVJ422P,
148         /* .name           = */ "yuvj422p",
149         /* .nb_channels    = */ 3,
150         /* .color_type     = */ FF_COLOR_YUV_JPEG,
151         /* .pixel_type     = */ FF_PIXEL_PLANAR,
152         /* .is_alpha       = */ 0,
153         /* .x_chroma_shift = */ 1,
154         /* .y_chroma_shift = */ 0,
155         /* .depth          = */ 8,
156       },
157   /* [PIX_FMT_YUVJ444P] = */ {
158         /* .format         = */ PIX_FMT_YUVJ444P,
159         /* .name           = */ "yuvj444p",
160         /* .nb_channels    = */ 3,
161         /* .color_type     = */ FF_COLOR_YUV_JPEG,
162         /* .pixel_type     = */ FF_PIXEL_PLANAR,
163         /* .is_alpha       = */ 0,
164         /* .x_chroma_shift = */ 0,
165         /* .y_chroma_shift = */ 0,
166         /* .depth          = */ 8,
167       },
168
169   /* RGB formats */
170   /* [PIX_FMT_RGB24] = */ {
171         /* .format         = */ PIX_FMT_RGB24,
172         /* .name           = */ "rgb24",
173         /* .nb_channels    = */ 3,
174         /* .color_type     = */ FF_COLOR_RGB,
175         /* .pixel_type     = */ FF_PIXEL_PACKED,
176         /* .is_alpha       = */ 0,
177         /* .x_chroma_shift = */ 0,
178         /* .y_chroma_shift = */ 0,
179         /* .depth          = */ 8,
180       },
181   /* [PIX_FMT_BGR24] = */ {
182         /* .format         = */ PIX_FMT_BGR24,
183         /* .name           = */ "bgr24",
184         /* .nb_channels    = */ 3,
185         /* .color_type     = */ FF_COLOR_RGB,
186         /* .pixel_type     = */ FF_PIXEL_PACKED,
187         /* .is_alpha       = */ 0,
188         /* .x_chroma_shift = */ 0,
189         /* .y_chroma_shift = */ 0,
190         /* .depth          = */ 8,
191       },
192   /* [PIX_FMT_RGB32] = */ {
193         /* .format         = */ PIX_FMT_RGB32,
194         /* .name           = */ "rgb32",
195         /* .nb_channels    = */ 4,
196         /* .color_type     = */ FF_COLOR_RGB,
197         /* .pixel_type     = */ FF_PIXEL_PACKED,
198         /* .is_alpha       = */ 0,
199         /* .x_chroma_shift = */ 0,
200         /* .y_chroma_shift = */ 0,
201         /* .depth          = */ 8,
202       },
203   /* [PIX_FMT_BGR32] = */ {
204         /* .format         = */ PIX_FMT_BGR32,
205         /* .name           = */ "bgr32",
206         /* .nb_channels    = */ 4,
207         /* .color_type     = */ FF_COLOR_RGB,
208         /* .pixel_type     = */ FF_PIXEL_PACKED,
209         /* .is_alpha       = */ 0,
210         /* .x_chroma_shift = */ 0,
211         /* .y_chroma_shift = */ 0,
212         /* .depth          = */ 8,
213       },
214   /* [PIX_FMT_RGBA32] = */ {
215         /* .format         = */ PIX_FMT_RGBA32,
216         /* .name           = */ "rgba32",
217         /* .nb_channels    = */ 4,
218         /* .color_type     = */ FF_COLOR_RGB,
219         /* .pixel_type     = */ FF_PIXEL_PACKED,
220         /* .is_alpha       = */ 1,
221         /* .x_chroma_shift = */ 0,
222         /* .y_chroma_shift = */ 0,
223         /* .depth          = */ 8,
224       },
225   /* [PIX_FMT_BGRA32] = */ {
226         /* .format         = */ PIX_FMT_BGRA32,
227         /* .name           = */ "bgra32",
228         /* .nb_channels    = */ 4,
229         /* .color_type     = */ FF_COLOR_RGB,
230         /* .pixel_type     = */ FF_PIXEL_PACKED,
231         /* .is_alpha       = */ 1,
232         /* .x_chroma_shift = */ 0,
233         /* .y_chroma_shift = */ 0,
234         /* .depth          = */ 8,
235       },
236   /* [PIX_FMT_RGB565] = */ {
237         /* .format         = */ PIX_FMT_RGB565,
238         /* .name           = */ "rgb565",
239         /* .nb_channels    = */ 3,
240         /* .color_type     = */ FF_COLOR_RGB,
241         /* .pixel_type     = */ FF_PIXEL_PACKED,
242         /* .is_alpha       = */ 0,
243         /* .x_chroma_shift = */ 0,
244         /* .y_chroma_shift = */ 0,
245         /* .depth          = */ 5,
246       },
247   /* [PIX_FMT_RGB555] = */ {
248         /* .format         = */ PIX_FMT_RGB555,
249         /* .name           = */ "rgb555",
250         /* .nb_channels    = */ 4,
251         /* .color_type     = */ FF_COLOR_RGB,
252         /* .pixel_type     = */ FF_PIXEL_PACKED,
253         /* .is_alpha       = */ 1,
254         /* .x_chroma_shift = */ 0,
255         /* .y_chroma_shift = */ 0,
256         /* .depth          = */ 5,
257       },
258
259   /* gray / mono formats */
260   /* [PIX_FMT_GRAY8] = */ {
261         /* .format         = */ PIX_FMT_GRAY8,
262         /* .name           = */ "gray",
263         /* .nb_channels    = */ 1,
264         /* .color_type     = */ FF_COLOR_GRAY,
265         /* .pixel_type     = */ FF_PIXEL_PLANAR,
266         /* .is_alpha       = */ 0,
267         /* .x_chroma_shift = */ 0,
268         /* .y_chroma_shift = */ 0,
269         /* .depth          = */ 8,
270       },
271   /* [PIX_FMT_MONOWHITE] = */ {
272         /* .format         = */ PIX_FMT_MONOWHITE,
273         /* .name           = */ "monow",
274         /* .nb_channels    = */ 1,
275         /* .color_type     = */ FF_COLOR_GRAY,
276         /* .pixel_type     = */ FF_PIXEL_PLANAR,
277         /* .is_alpha       = */ 0,
278         /* .x_chroma_shift = */ 0,
279         /* .y_chroma_shift = */ 0,
280         /* .depth          = */ 1,
281       },
282   /* [PIX_FMT_MONOBLACK] = */ {
283         /* .format         = */ PIX_FMT_MONOBLACK,
284         /* .name           = */ "monob",
285         /* .nb_channels    = */ 1,
286         /* .color_type     = */ FF_COLOR_GRAY,
287         /* .pixel_type     = */ FF_PIXEL_PLANAR,
288         /* .is_alpha       = */ 0,
289         /* .x_chroma_shift = */ 0,
290         /* .y_chroma_shift = */ 0,
291         /* .depth          = */ 1,
292       },
293
294   /* paletted formats */
295   /* [PIX_FMT_PAL8] = */ {
296         /* .format         = */ PIX_FMT_PAL8,
297         /* .name           = */ "pal8",
298         /* .nb_channels    = */ 4,
299         /* .color_type     = */ FF_COLOR_RGB,
300         /* .pixel_type     = */ FF_PIXEL_PALETTE,
301         /* .is_alpha       = */ 1,
302         /* .x_chroma_shift = */ 0,
303         /* .y_chroma_shift = */ 0,
304         /* .depth          = */ 8,
305       },
306   /* [PIX_FMT_XVMC_MPEG2_MC] = */ {
307         /* .format         = */ PIX_FMT_XVMC_MPEG2_MC,
308         /* .name           = */ "xvmcmc",
309       },
310   /* [PIX_FMT_XVMC_MPEG2_IDCT] = */ {
311         /* .format         = */ PIX_FMT_XVMC_MPEG2_IDCT,
312         /* .name           = */ "xvmcidct",
313       },
314   /* [PIX_FMT_UYVY411] = */ {
315         /* .format         = */ PIX_FMT_UYVY411,
316         /* .name           = */ "uyvy411",
317         /* .nb_channels    = */ 1,
318         /* .color_type     = */ FF_COLOR_YUV,
319         /* .pixel_type     = */ FF_PIXEL_PACKED,
320         /* .is_alpha       = */ 0,
321         /* .x_chroma_shift = */ 2,
322         /* .y_chroma_shift = */ 0,
323         /* .depth          = */ 8,
324       },
325   /* [PIX_FMT_AYUV4444] = */ {
326         /* .format         = */ PIX_FMT_AYUV4444,
327         /* .name           = */ "ayuv4444",
328         /* .nb_channels    = */ 1,
329         /* .color_type     = */ FF_COLOR_YUV,
330         /* .pixel_type     = */ FF_PIXEL_PACKED,
331         /* .is_alpha       = */ 1,
332         /* .x_chroma_shift = */ 0,
333         /* .y_chroma_shift = */ 0,
334         /*.depth = */ 8
335       }
336
337 };
338
339 /* returns NULL if not found */
340 /* undid static since this is also used in gstffmpegcodecmap.c */
341 PixFmtInfo *
342 get_pix_fmt_info (enum PixelFormat format)
343 {
344   int i;
345
346   for (i = 0; i < sizeof (pix_fmt_info) / sizeof (pix_fmt_info[0]); i++) {
347     if (pix_fmt_info[i].format == format) {
348       return pix_fmt_info + i;
349     }
350   }
351
352   /* since this doesn't get checked *anywhere*, we might as well warn
353      here if we return NULL so you have *some* idea what's going on */
354   g_warning
355       ("Could not find info for pixel format %d out of %d known pixel formats. One segfault coming up",
356       format, PIX_FMT_NB);
357   return NULL;
358 }
359
360 void
361 avcodec_get_chroma_sub_sample (int pix_fmt, int *h_shift, int *v_shift)
362 {
363   *h_shift = get_pix_fmt_info (pix_fmt)->x_chroma_shift;
364   *v_shift = get_pix_fmt_info (pix_fmt)->y_chroma_shift;
365 }
366
367 const char *
368 avcodec_get_pix_fmt_name (int pix_fmt)
369 {
370   if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
371     return "???";
372   else
373     return get_pix_fmt_info (pix_fmt)->name;
374 }
375
376 enum PixelFormat
377 avcodec_get_pix_fmt (const char *name)
378 {
379   int i;
380
381   for (i = 0; i < PIX_FMT_NB; i++)
382     if (!strcmp (pix_fmt_info[i].name, name))
383       break;
384   return pix_fmt_info[i].format;
385 }
386
387 int
388 avpicture_layout (const AVPicture * src, int pix_fmt, int width, int height,
389     unsigned char *dest, int dest_size)
390 {
391   PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
392   int i, j, w, h, data_planes;
393   const unsigned char *s;
394   int size = avpicture_get_size (pix_fmt, width, height);
395
396   if (size > dest_size)
397     return -1;
398
399   if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
400     if (pix_fmt == PIX_FMT_YUV422 ||
401         pix_fmt == PIX_FMT_UYVY422 ||
402         pix_fmt == PIX_FMT_RGB565 || pix_fmt == PIX_FMT_RGB555)
403       w = width * 2;
404     else if (pix_fmt == PIX_FMT_UYVY411)
405       w = width + width / 2;
406     else if (pix_fmt == PIX_FMT_PAL8)
407       w = width;
408     else
409       w = width * (pf->depth * pf->nb_channels / 8);
410
411     data_planes = 1;
412     h = height;
413   } else {
414     data_planes = pf->nb_channels;
415     w = (width * pf->depth + 7) / 8;
416     h = height;
417   }
418
419   for (i = 0; i < data_planes; i++) {
420     if (i == 1) {
421       w = width >> pf->x_chroma_shift;
422       h = height >> pf->y_chroma_shift;
423     }
424     s = src->data[i];
425     for (j = 0; j < h; j++) {
426       memcpy (dest, s, w);
427       dest += w;
428       s += src->linesize[i];
429     }
430   }
431
432   if (pf->pixel_type == FF_PIXEL_PALETTE)
433     memcpy ((unsigned char *) (((size_t) dest + 3) & ~3), src->data[1],
434         256 * 4);
435
436   return size;
437 }
438
439 int
440 avpicture_get_size (int pix_fmt, int width, int height)
441 {
442   AVPicture dummy_pict;
443
444   return gst_ffmpegcsp_avpicture_fill (&dummy_pict, NULL, pix_fmt, width,
445       height);
446 }
447
448 /**
449  * compute the loss when converting from a pixel format to another 
450  */
451 int
452 avcodec_get_pix_fmt_loss (int dst_pix_fmt, int src_pix_fmt, int has_alpha)
453 {
454   const PixFmtInfo *pf, *ps;
455   int loss;
456
457   ps = get_pix_fmt_info (src_pix_fmt);
458   pf = get_pix_fmt_info (dst_pix_fmt);
459
460   /* compute loss */
461   loss = 0;
462   if (pf->depth < ps->depth ||
463       (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
464     loss |= FF_LOSS_DEPTH;
465   if (pf->x_chroma_shift > ps->x_chroma_shift ||
466       pf->y_chroma_shift > ps->y_chroma_shift)
467     loss |= FF_LOSS_RESOLUTION;
468   switch (pf->color_type) {
469     case FF_COLOR_RGB:
470       if (ps->color_type != FF_COLOR_RGB && ps->color_type != FF_COLOR_GRAY)
471         loss |= FF_LOSS_COLORSPACE;
472       break;
473     case FF_COLOR_GRAY:
474       if (ps->color_type != FF_COLOR_GRAY)
475         loss |= FF_LOSS_COLORSPACE;
476       break;
477     case FF_COLOR_YUV:
478       if (ps->color_type != FF_COLOR_YUV)
479         loss |= FF_LOSS_COLORSPACE;
480       break;
481     case FF_COLOR_YUV_JPEG:
482       if (ps->color_type != FF_COLOR_YUV_JPEG &&
483           ps->color_type != FF_COLOR_YUV && ps->color_type != FF_COLOR_GRAY)
484         loss |= FF_LOSS_COLORSPACE;
485       break;
486     default:
487       /* fail safe test */
488       if (ps->color_type != pf->color_type)
489         loss |= FF_LOSS_COLORSPACE;
490       break;
491   }
492   if (pf->color_type == FF_COLOR_GRAY && ps->color_type != FF_COLOR_GRAY)
493     loss |= FF_LOSS_CHROMA;
494   if (!pf->is_alpha && (ps->is_alpha && has_alpha))
495     loss |= FF_LOSS_ALPHA;
496   if (pf->pixel_type == FF_PIXEL_PALETTE &&
497       (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
498     loss |= FF_LOSS_COLORQUANT;
499   return loss;
500 }
501
502 static int
503 avg_bits_per_pixel (int pix_fmt)
504 {
505   int bits;
506   const PixFmtInfo *pf;
507
508   pf = get_pix_fmt_info (pix_fmt);
509   switch (pf->pixel_type) {
510     case FF_PIXEL_PACKED:
511       switch (pix_fmt) {
512         case PIX_FMT_YUV422:
513         case PIX_FMT_UYVY422:
514         case PIX_FMT_RGB565:
515         case PIX_FMT_RGB555:
516           bits = 16;
517           break;
518         case PIX_FMT_UYVY411:
519           bits = 12;
520           break;
521         default:
522           bits = pf->depth * pf->nb_channels;
523           break;
524       }
525       break;
526     case FF_PIXEL_PLANAR:
527       if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
528         bits = pf->depth * pf->nb_channels;
529       } else {
530         bits = pf->depth + ((2 * pf->depth) >>
531             (pf->x_chroma_shift + pf->y_chroma_shift));
532       }
533       break;
534     case FF_PIXEL_PALETTE:
535       bits = 8;
536       break;
537     default:
538       bits = -1;
539       break;
540   }
541   return bits;
542 }
543
544 static int
545 avcodec_find_best_pix_fmt1 (int pix_fmt_mask,
546     int src_pix_fmt, int has_alpha, int loss_mask)
547 {
548   int dist, i, loss, min_dist, dst_pix_fmt;
549
550   /* find exact color match with smallest size */
551   dst_pix_fmt = -1;
552   min_dist = 0x7fffffff;
553   for (i = 0; i < PIX_FMT_NB; i++) {
554     if (pix_fmt_mask & (1 << i)) {
555       loss = avcodec_get_pix_fmt_loss (i, src_pix_fmt, has_alpha) & loss_mask;
556       if (loss == 0) {
557         dist = avg_bits_per_pixel (i);
558         if (dist < min_dist) {
559           min_dist = dist;
560           dst_pix_fmt = i;
561         }
562       }
563     }
564   }
565   return dst_pix_fmt;
566 }
567
568 /** 
569  * find best pixel format to convert to. Return -1 if none found 
570  */
571 int
572 avcodec_find_best_pix_fmt (int pix_fmt_mask, int src_pix_fmt,
573     int has_alpha, int *loss_ptr)
574 {
575   int dst_pix_fmt, loss_mask, i;
576   static const int loss_mask_order[] = {
577     ~0,                         /* no loss first */
578     ~FF_LOSS_ALPHA,
579     ~FF_LOSS_RESOLUTION,
580     ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
581     ~FF_LOSS_COLORQUANT,
582     ~FF_LOSS_DEPTH,
583     0,
584   };
585
586   /* try with successive loss */
587   i = 0;
588   for (;;) {
589     loss_mask = loss_mask_order[i++];
590     dst_pix_fmt = avcodec_find_best_pix_fmt1 (pix_fmt_mask, src_pix_fmt,
591         has_alpha, loss_mask);
592     if (dst_pix_fmt >= 0)
593       goto found;
594     if (loss_mask == 0)
595       break;
596   }
597   return -1;
598 found:
599   if (loss_ptr)
600     *loss_ptr = avcodec_get_pix_fmt_loss (dst_pix_fmt, src_pix_fmt, has_alpha);
601   return dst_pix_fmt;
602 }
603
604 static void
605 img_copy_plane (uint8_t * dst, int dst_wrap,
606     const uint8_t * src, int src_wrap, int width, int height)
607 {
608   for (; height > 0; height--) {
609     memcpy (dst, src, width);
610     dst += dst_wrap;
611     src += src_wrap;
612   }
613 }
614
615 /**
616  * Copy image 'src' to 'dst'.
617  */
618 void
619 img_copy (AVPicture * dst, const AVPicture * src,
620     int pix_fmt, int width, int height)
621 {
622   int bwidth, bits, i;
623   PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
624
625   pf = get_pix_fmt_info (pix_fmt);
626   switch (pf->pixel_type) {
627     case FF_PIXEL_PACKED:
628       switch (pix_fmt) {
629         case PIX_FMT_YUV422:
630         case PIX_FMT_UYVY422:
631         case PIX_FMT_RGB565:
632         case PIX_FMT_RGB555:
633           bits = 16;
634           break;
635         case PIX_FMT_UYVY411:
636           bits = 12;
637           break;
638         default:
639           bits = pf->depth * pf->nb_channels;
640           break;
641       }
642       bwidth = (width * bits + 7) >> 3;
643       img_copy_plane (dst->data[0], dst->linesize[0],
644           src->data[0], src->linesize[0], bwidth, height);
645       break;
646     case FF_PIXEL_PLANAR:
647       for (i = 0; i < pf->nb_channels; i++) {
648         int w, h;
649
650         w = width;
651         h = height;
652         if (i == 1 || i == 2) {
653           w >>= pf->x_chroma_shift;
654           h >>= pf->y_chroma_shift;
655         }
656         bwidth = (w * pf->depth + 7) >> 3;
657         img_copy_plane (dst->data[i], dst->linesize[i],
658             src->data[i], src->linesize[i], bwidth, h);
659       }
660       break;
661     case FF_PIXEL_PALETTE:
662       img_copy_plane (dst->data[0], dst->linesize[0],
663           src->data[0], src->linesize[0], width, height);
664       /* copy the palette */
665       img_copy_plane (dst->data[1], dst->linesize[1],
666           src->data[1], src->linesize[1], 4, 256);
667       break;
668   }
669 }
670
671 /* XXX: totally non optimized */
672
673 static void
674 yuv422_to_yuv420p (AVPicture * dst, const AVPicture * src,
675     int width, int height)
676 {
677   const uint8_t *p, *p1;
678   uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
679   int w;
680
681   p1 = src->data[0];
682   lum1 = dst->data[0];
683   cb1 = dst->data[1];
684   cr1 = dst->data[2];
685
686   for (; height >= 1; height -= 2) {
687     p = p1;
688     lum = lum1;
689     cb = cb1;
690     cr = cr1;
691     for (w = width; w >= 2; w -= 2) {
692       lum[0] = p[0];
693       cb[0] = p[1];
694       lum[1] = p[2];
695       cr[0] = p[3];
696       p += 4;
697       lum += 2;
698       cb++;
699       cr++;
700     }
701     if (w) {
702       lum[0] = p[0];
703       cb[0] = p[1];
704       cr[0] = p[3];
705       cb++;
706       cr++;
707     }
708     p1 += src->linesize[0];
709     lum1 += dst->linesize[0];
710     if (height > 1) {
711       p = p1;
712       lum = lum1;
713       for (w = width; w >= 2; w -= 2) {
714         lum[0] = p[0];
715         lum[1] = p[2];
716         p += 4;
717         lum += 2;
718       }
719       if (w) {
720         lum[0] = p[0];
721       }
722       p1 += src->linesize[0];
723       lum1 += dst->linesize[0];
724     }
725     cb1 += dst->linesize[1];
726     cr1 += dst->linesize[2];
727   }
728 }
729
730 static void
731 uyvy422_to_yuv420p (AVPicture * dst, const AVPicture * src,
732     int width, int height)
733 {
734   const uint8_t *p, *p1;
735   uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
736   int w;
737
738   p1 = src->data[0];
739
740   lum1 = dst->data[0];
741   cb1 = dst->data[1];
742   cr1 = dst->data[2];
743
744   for (; height >= 1; height -= 2) {
745     p = p1;
746     lum = lum1;
747     cb = cb1;
748     cr = cr1;
749     for (w = width; w >= 2; w -= 2) {
750       lum[0] = p[1];
751       cb[0] = p[0];
752       lum[1] = p[3];
753       cr[0] = p[2];
754       p += 4;
755       lum += 2;
756       cb++;
757       cr++;
758     }
759     if (w) {
760       lum[0] = p[1];
761       cb[0] = p[0];
762       cr[0] = p[2];
763       cb++;
764       cr++;
765     }
766     p1 += src->linesize[0];
767     lum1 += dst->linesize[0];
768     if (height > 1) {
769       p = p1;
770       lum = lum1;
771       for (w = width; w >= 2; w -= 2) {
772         lum[0] = p[1];
773         lum[1] = p[3];
774         p += 4;
775         lum += 2;
776       }
777       if (w) {
778         lum[0] = p[1];
779       }
780       p1 += src->linesize[0];
781       lum1 += dst->linesize[0];
782     }
783     cb1 += dst->linesize[1];
784     cr1 += dst->linesize[2];
785   }
786 }
787
788
789 static void
790 uyvy422_to_yuv422p (AVPicture * dst, const AVPicture * src,
791     int width, int height)
792 {
793   const uint8_t *p, *p1;
794   uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
795   int w;
796
797   p1 = src->data[0];
798   lum1 = dst->data[0];
799   cb1 = dst->data[1];
800   cr1 = dst->data[2];
801   for (; height > 0; height--) {
802     p = p1;
803     lum = lum1;
804     cb = cb1;
805     cr = cr1;
806     for (w = width; w >= 2; w -= 2) {
807       lum[0] = p[1];
808       cb[0] = p[0];
809       lum[1] = p[3];
810       cr[0] = p[2];
811       p += 4;
812       lum += 2;
813       cb++;
814       cr++;
815     }
816     p1 += src->linesize[0];
817     lum1 += dst->linesize[0];
818     cb1 += dst->linesize[1];
819     cr1 += dst->linesize[2];
820   }
821 }
822
823
824 static void
825 yuv422_to_yuv422p (AVPicture * dst, const AVPicture * src,
826     int width, int height)
827 {
828   const uint8_t *p, *p1;
829   uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
830   int w;
831
832   p1 = src->data[0];
833   lum1 = dst->data[0];
834   cb1 = dst->data[1];
835   cr1 = dst->data[2];
836   for (; height > 0; height--) {
837     p = p1;
838     lum = lum1;
839     cb = cb1;
840     cr = cr1;
841     for (w = width; w >= 2; w -= 2) {
842       lum[0] = p[0];
843       cb[0] = p[1];
844       lum[1] = p[2];
845       cr[0] = p[3];
846       p += 4;
847       lum += 2;
848       cb++;
849       cr++;
850     }
851     p1 += src->linesize[0];
852     lum1 += dst->linesize[0];
853     cb1 += dst->linesize[1];
854     cr1 += dst->linesize[2];
855   }
856 }
857
858 static void
859 yuv422p_to_yuv422 (AVPicture * dst, const AVPicture * src,
860     int width, int height)
861 {
862   uint8_t *p, *p1;
863   const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
864   int w;
865
866   p1 = dst->data[0];
867   lum1 = src->data[0];
868   cb1 = src->data[1];
869   cr1 = src->data[2];
870   for (; height > 0; height--) {
871     p = p1;
872     lum = lum1;
873     cb = cb1;
874     cr = cr1;
875     for (w = width; w >= 2; w -= 2) {
876       p[0] = lum[0];
877       p[1] = cb[0];
878       p[2] = lum[1];
879       p[3] = cr[0];
880       p += 4;
881       lum += 2;
882       cb++;
883       cr++;
884     }
885     if (w) {
886       p[0] = lum[0];
887       p[1] = cb[0];
888     }
889     p1 += dst->linesize[0];
890     lum1 += src->linesize[0];
891     cb1 += src->linesize[1];
892     cr1 += src->linesize[2];
893   }
894 }
895
896 static void
897 yuv422p_to_uyvy422 (AVPicture * dst, const AVPicture * src,
898     int width, int height)
899 {
900   uint8_t *p, *p1;
901   const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
902   int w;
903
904   p1 = dst->data[0];
905   lum1 = src->data[0];
906   cb1 = src->data[1];
907   cr1 = src->data[2];
908   for (; height > 0; height--) {
909     p = p1;
910     lum = lum1;
911     cb = cb1;
912     cr = cr1;
913     for (w = width; w >= 2; w -= 2) {
914       p[1] = lum[0];
915       p[0] = cb[0];
916       p[3] = lum[1];
917       p[2] = cr[0];
918       p += 4;
919       lum += 2;
920       cb++;
921       cr++;
922     }
923     p1 += dst->linesize[0];
924     lum1 += src->linesize[0];
925     cb1 += src->linesize[1];
926     cr1 += src->linesize[2];
927   }
928 }
929
930 static void
931 uyvy411_to_yuv411p (AVPicture * dst, const AVPicture * src,
932     int width, int height)
933 {
934   const uint8_t *p, *p1;
935   uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
936   int w;
937
938   p1 = src->data[0];
939   lum1 = dst->data[0];
940   cb1 = dst->data[1];
941   cr1 = dst->data[2];
942   for (; height > 0; height--) {
943     p = p1;
944     lum = lum1;
945     cb = cb1;
946     cr = cr1;
947     for (w = width; w >= 4; w -= 4) {
948       cb[0] = p[0];
949       lum[0] = p[1];
950       lum[1] = p[2];
951       cr[0] = p[3];
952       lum[2] = p[4];
953       lum[3] = p[5];
954       p += 6;
955       lum += 4;
956       cb++;
957       cr++;
958     }
959     p1 += src->linesize[0];
960     lum1 += dst->linesize[0];
961     cb1 += dst->linesize[1];
962     cr1 += dst->linesize[2];
963   }
964 }
965
966
967 static void
968 yuv420p_to_yuv422 (AVPicture * dst, const AVPicture * src,
969     int width, int height)
970 {
971   int w, h;
972   uint8_t *line1, *line2, *linesrc = dst->data[0];
973   uint8_t *lum1, *lum2, *lumsrc = src->data[0];
974   uint8_t *cb1, *cb2 = src->data[1];
975   uint8_t *cr1, *cr2 = src->data[2];
976
977   for (h = height / 2; h--;) {
978     line1 = linesrc;
979     line2 = linesrc + dst->linesize[0];
980
981     lum1 = lumsrc;
982     lum2 = lumsrc + src->linesize[0];
983
984     cb1 = cb2;
985     cr1 = cr2;
986
987     for (w = width / 2; w--;) {
988       *line1++ = *lum1++;
989       *line2++ = *lum2++;
990       *line1++ = *line2++ = *cb1++;
991       *line1++ = *lum1++;
992       *line2++ = *lum2++;
993       *line1++ = *line2++ = *cr1++;
994     }
995     /* odd width */
996     if (width % 2 != 0) {
997       *line1++ = *lum1++;
998       *line2++ = *lum2++;
999       *line1++ = *line2++ = *cb1++;
1000     }
1001
1002     linesrc += dst->linesize[0] * 2;
1003     lumsrc += src->linesize[0] * 2;
1004     cb2 += src->linesize[1];
1005     cr2 += src->linesize[2];
1006   }
1007   /* odd height */
1008   if (height % 2 != 0) {
1009     line1 = linesrc;
1010     lum1 = lumsrc;
1011     cb1 = cb2;
1012     cr1 = cr2;
1013
1014     for (w = width / 2; w--;) {
1015       *line1++ = *lum1++;
1016       *line1++ = *cb1++;
1017       *line1++ = *lum1++;
1018       *line1++ = *cr1++;
1019     }
1020     /* odd width */
1021     if (width % 2 != 0) {
1022       *line1++ = *lum1++;
1023       *line1++ = *cb1++;
1024     }
1025   }
1026 }
1027
1028 #define SCALEBITS 10
1029 #define ONE_HALF  (1 << (SCALEBITS - 1))
1030 #define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
1031
1032 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1033 {\
1034     cb = (cb1) - 128;\
1035     cr = (cr1) - 128;\
1036     r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1037     g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1038             ONE_HALF;\
1039     b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1040 }
1041
1042 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1043 {\
1044     y = ((y1) - 16) * FIX(255.0/219.0);\
1045     r = cm[(y + r_add) >> SCALEBITS];\
1046     g = cm[(y + g_add) >> SCALEBITS];\
1047     b = cm[(y + b_add) >> SCALEBITS];\
1048 }
1049
1050 #define YUV_TO_RGB1(cb1, cr1)\
1051 {\
1052     cb = (cb1) - 128;\
1053     cr = (cr1) - 128;\
1054     r_add = FIX(1.40200) * cr + ONE_HALF;\
1055     g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1056     b_add = FIX(1.77200) * cb + ONE_HALF;\
1057 }
1058
1059 #define YUV_TO_RGB2(r, g, b, y1)\
1060 {\
1061     y = (y1) << SCALEBITS;\
1062     r = cm[(y + r_add) >> SCALEBITS];\
1063     g = cm[(y + g_add) >> SCALEBITS];\
1064     b = cm[(y + b_add) >> SCALEBITS];\
1065 }
1066
1067 #define Y_CCIR_TO_JPEG(y)\
1068  cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1069
1070 #define Y_JPEG_TO_CCIR(y)\
1071  (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1072
1073 #define C_CCIR_TO_JPEG(y)\
1074  cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1075
1076 /* NOTE: the clamp is really necessary! */
1077 static inline int
1078 C_JPEG_TO_CCIR (int y)
1079 {
1080   y = (((y - 128) * FIX (112.0 / 127.0) + (ONE_HALF +
1081               (128 << SCALEBITS))) >> SCALEBITS);
1082   if (y < 16)
1083     y = 16;
1084   return y;
1085 }
1086
1087
1088 #define RGB_TO_Y(r, g, b) \
1089 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1090   FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1091
1092 #define RGB_TO_U(r1, g1, b1, shift)\
1093 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +         \
1094      FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1095
1096 #define RGB_TO_V(r1, g1, b1, shift)\
1097 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 -           \
1098    FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1099
1100 #define RGB_TO_Y_CCIR(r, g, b) \
1101 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1102   FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1103
1104 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1105 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 +         \
1106      FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1107
1108 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1109 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 -           \
1110    FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1111
1112 static uint8_t y_ccir_to_jpeg[256];
1113 static uint8_t y_jpeg_to_ccir[256];
1114 static uint8_t c_ccir_to_jpeg[256];
1115 static uint8_t c_jpeg_to_ccir[256];
1116
1117 /* init various conversion tables */
1118 static void
1119 img_convert_init (void)
1120 {
1121   int i;
1122   uint8_t *cm = cropTbl + MAX_NEG_CROP;
1123
1124   for (i = 0; i < 256; i++) {
1125     y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG (i);
1126     y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR (i);
1127     c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG (i);
1128     c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR (i);
1129   }
1130 }
1131
1132 /* apply to each pixel the given table */
1133 static void
1134 img_apply_table (uint8_t * dst, int dst_wrap,
1135     const uint8_t * src, int src_wrap,
1136     int width, int height, const uint8_t * table1)
1137 {
1138   int n;
1139   const uint8_t *s;
1140   uint8_t *d;
1141   const uint8_t *table;
1142
1143   table = table1;
1144   for (; height > 0; height--) {
1145     s = src;
1146     d = dst;
1147     n = width;
1148     while (n >= 4) {
1149       d[0] = table[s[0]];
1150       d[1] = table[s[1]];
1151       d[2] = table[s[2]];
1152       d[3] = table[s[3]];
1153       d += 4;
1154       s += 4;
1155       n -= 4;
1156     }
1157     while (n > 0) {
1158       d[0] = table[s[0]];
1159       d++;
1160       s++;
1161       n--;
1162     }
1163     dst += dst_wrap;
1164     src += src_wrap;
1165   }
1166 }
1167
1168 /* XXX: use generic filter ? */
1169 /* XXX: in most cases, the sampling position is incorrect */
1170
1171 /* 4x1 -> 1x1 */
1172 static void
1173 shrink41 (uint8_t * dst, int dst_wrap,
1174     const uint8_t * src, int src_wrap, int width, int height)
1175 {
1176   int w;
1177   const uint8_t *s;
1178   uint8_t *d;
1179
1180   for (; height > 0; height--) {
1181     s = src;
1182     d = dst;
1183     for (w = width; w > 0; w--) {
1184       d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1185       s += 4;
1186       d++;
1187     }
1188     src += src_wrap;
1189     dst += dst_wrap;
1190   }
1191 }
1192
1193 /* 2x1 -> 1x1 */
1194 static void
1195 shrink21 (uint8_t * dst, int dst_wrap,
1196     const uint8_t * src, int src_wrap, int width, int height)
1197 {
1198   int w;
1199   const uint8_t *s;
1200   uint8_t *d;
1201
1202   for (; height > 0; height--) {
1203     s = src;
1204     d = dst;
1205     for (w = width; w > 0; w--) {
1206       d[0] = (s[0] + s[1]) >> 1;
1207       s += 2;
1208       d++;
1209     }
1210     src += src_wrap;
1211     dst += dst_wrap;
1212   }
1213 }
1214
1215 /* 1x2 -> 1x1 */
1216 static void
1217 shrink12 (uint8_t * dst, int dst_wrap,
1218     const uint8_t * src, int src_wrap, int width, int height)
1219 {
1220   int w;
1221   uint8_t *d;
1222   const uint8_t *s1, *s2;
1223
1224   for (; height > 0; height--) {
1225     s1 = src;
1226     s2 = s1 + src_wrap;
1227     d = dst;
1228     for (w = width; w >= 4; w -= 4) {
1229       d[0] = (s1[0] + s2[0]) >> 1;
1230       d[1] = (s1[1] + s2[1]) >> 1;
1231       d[2] = (s1[2] + s2[2]) >> 1;
1232       d[3] = (s1[3] + s2[3]) >> 1;
1233       s1 += 4;
1234       s2 += 4;
1235       d += 4;
1236     }
1237     for (; w > 0; w--) {
1238       d[0] = (s1[0] + s2[0]) >> 1;
1239       s1++;
1240       s2++;
1241       d++;
1242     }
1243     src += 2 * src_wrap;
1244     dst += dst_wrap;
1245   }
1246 }
1247
1248 /* 2x2 -> 1x1 */
1249 static void
1250 shrink22 (uint8_t * dst, int dst_wrap,
1251     const uint8_t * src, int src_wrap, int width, int height)
1252 {
1253   int w;
1254   const uint8_t *s1, *s2;
1255   uint8_t *d;
1256
1257   for (; height > 0; height--) {
1258     s1 = src;
1259     s2 = s1 + src_wrap;
1260     d = dst;
1261     for (w = width; w >= 4; w -= 4) {
1262       d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1263       d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1264       d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1265       d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1266       s1 += 8;
1267       s2 += 8;
1268       d += 4;
1269     }
1270     for (; w > 0; w--) {
1271       d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1272       s1 += 2;
1273       s2 += 2;
1274       d++;
1275     }
1276     src += 2 * src_wrap;
1277     dst += dst_wrap;
1278   }
1279 }
1280
1281 /* 4x4 -> 1x1 */
1282 static void
1283 shrink44 (uint8_t * dst, int dst_wrap,
1284     const uint8_t * src, int src_wrap, int width, int height)
1285 {
1286   int w;
1287   const uint8_t *s1, *s2, *s3, *s4;
1288   uint8_t *d;
1289
1290   for (; height > 0; height--) {
1291     s1 = src;
1292     s2 = s1 + src_wrap;
1293     s3 = s2 + src_wrap;
1294     s4 = s3 + src_wrap;
1295     d = dst;
1296     for (w = width; w > 0; w--) {
1297       d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1298           s2[0] + s2[1] + s2[2] + s2[3] +
1299           s3[0] + s3[1] + s3[2] + s3[3] +
1300           s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1301       s1 += 4;
1302       s2 += 4;
1303       s3 += 4;
1304       s4 += 4;
1305       d++;
1306     }
1307     src += 4 * src_wrap;
1308     dst += dst_wrap;
1309   }
1310 }
1311
1312 static void
1313 grow21_line (uint8_t * dst, const uint8_t * src, int width)
1314 {
1315   int w;
1316   const uint8_t *s1;
1317   uint8_t *d;
1318
1319   s1 = src;
1320   d = dst;
1321   for (w = width; w >= 4; w -= 4) {
1322     d[1] = d[0] = s1[0];
1323     d[3] = d[2] = s1[1];
1324     s1 += 2;
1325     d += 4;
1326   }
1327   for (; w >= 2; w -= 2) {
1328     d[1] = d[0] = s1[0];
1329     s1++;
1330     d += 2;
1331   }
1332   /* only needed if width is not a multiple of two */
1333   /* XXX: veryfy that */
1334   if (w) {
1335     d[0] = s1[0];
1336   }
1337 }
1338
1339 static void
1340 grow41_line (uint8_t * dst, const uint8_t * src, int width)
1341 {
1342   int w, v;
1343   const uint8_t *s1;
1344   uint8_t *d;
1345
1346   s1 = src;
1347   d = dst;
1348   for (w = width; w >= 4; w -= 4) {
1349     v = s1[0];
1350     d[0] = v;
1351     d[1] = v;
1352     d[2] = v;
1353     d[3] = v;
1354     s1++;
1355     d += 4;
1356   }
1357 }
1358
1359 /* 1x1 -> 2x1 */
1360 static void
1361 grow21 (uint8_t * dst, int dst_wrap,
1362     const uint8_t * src, int src_wrap, int width, int height)
1363 {
1364   for (; height > 0; height--) {
1365     grow21_line (dst, src, width);
1366     src += src_wrap;
1367     dst += dst_wrap;
1368   }
1369 }
1370
1371 /* 1x1 -> 2x2 */
1372 static void
1373 grow22 (uint8_t * dst, int dst_wrap,
1374     const uint8_t * src, int src_wrap, int width, int height)
1375 {
1376   for (; height > 0; height--) {
1377     grow21_line (dst, src, width);
1378     if (height % 2)
1379       src += src_wrap;
1380     dst += dst_wrap;
1381   }
1382 }
1383
1384 /* 1x1 -> 4x1 */
1385 static void
1386 grow41 (uint8_t * dst, int dst_wrap,
1387     const uint8_t * src, int src_wrap, int width, int height)
1388 {
1389   for (; height > 0; height--) {
1390     grow41_line (dst, src, width);
1391     src += src_wrap;
1392     dst += dst_wrap;
1393   }
1394 }
1395
1396 /* 1x1 -> 4x4 */
1397 static void
1398 grow44 (uint8_t * dst, int dst_wrap,
1399     const uint8_t * src, int src_wrap, int width, int height)
1400 {
1401   for (; height > 0; height--) {
1402     grow41_line (dst, src, width);
1403     if ((height & 3) == 1)
1404       src += src_wrap;
1405     dst += dst_wrap;
1406   }
1407 }
1408
1409 /* 1x2 -> 2x1 */
1410 static void
1411 conv411 (uint8_t * dst, int dst_wrap,
1412     const uint8_t * src, int src_wrap, int width, int height)
1413 {
1414   int w, c;
1415   const uint8_t *s1, *s2;
1416   uint8_t *d;
1417
1418   width >>= 1;
1419
1420   for (; height > 0; height--) {
1421     s1 = src;
1422     s2 = src + src_wrap;
1423     d = dst;
1424     for (w = width; w > 0; w--) {
1425       c = (s1[0] + s2[0]) >> 1;
1426       d[0] = c;
1427       d[1] = c;
1428       s1++;
1429       s2++;
1430       d += 2;
1431     }
1432     src += src_wrap * 2;
1433     dst += dst_wrap;
1434   }
1435 }
1436
1437 /* XXX: add jpeg quantize code */
1438
1439 #define TRANSP_INDEX (6*6*6)
1440
1441 /* this is maybe slow, but allows for extensions */
1442 static inline unsigned char
1443 gif_clut_index (uint8_t r, uint8_t g, uint8_t b)
1444 {
1445   return ((((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6));
1446 }
1447
1448 static void
1449 build_rgb_palette (uint8_t * palette, int has_alpha)
1450 {
1451   uint32_t *pal;
1452   static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1453   int i, r, g, b;
1454
1455   pal = (uint32_t *) palette;
1456   i = 0;
1457   for (r = 0; r < 6; r++) {
1458     for (g = 0; g < 6; g++) {
1459       for (b = 0; b < 6; b++) {
1460         pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1461             (pal_value[g] << 8) | pal_value[b];
1462       }
1463     }
1464   }
1465   if (has_alpha)
1466     pal[i++] = 0;
1467   while (i < 256)
1468     pal[i++] = 0xff000000;
1469 }
1470
1471 /* copy bit n to bits 0 ... n - 1 */
1472 static inline unsigned int
1473 bitcopy_n (unsigned int a, int n)
1474 {
1475   int mask;
1476
1477   mask = (1 << n) - 1;
1478   return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1479 }
1480
1481 /* rgb555 handling */
1482
1483 #define RGB_NAME rgb555
1484
1485 #define RGB_IN(r, g, b, s)\
1486 {\
1487     unsigned int v = ((const uint16_t *)(s))[0];\
1488     r = bitcopy_n(v >> (10 - 3), 3);\
1489     g = bitcopy_n(v >> (5 - 3), 3);\
1490     b = bitcopy_n(v << 3, 3);\
1491 }
1492
1493 #define RGBA_IN(r, g, b, a, s)\
1494 {\
1495     unsigned int v = ((const uint16_t *)(s))[0];\
1496     r = bitcopy_n(v >> (10 - 3), 3);\
1497     g = bitcopy_n(v >> (5 - 3), 3);\
1498     b = bitcopy_n(v << 3, 3);\
1499     a = (-(v >> 15)) & 0xff;\
1500 }
1501
1502 #define RGBA_OUT(d, r, g, b, a)\
1503 {\
1504     ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1505                            ((a << 8) & 0x8000);\
1506 }
1507
1508 #define BPP 2
1509
1510 #include "imgconvert_template.h"
1511
1512 /* rgb565 handling */
1513
1514 #define RGB_NAME rgb565
1515
1516 #define RGB_IN(r, g, b, s)\
1517 {\
1518     unsigned int v = ((const uint16_t *)(s))[0];\
1519     r = bitcopy_n(v >> (11 - 3), 3);\
1520     g = bitcopy_n(v >> (5 - 2), 2);\
1521     b = bitcopy_n(v << 3, 3);\
1522 }
1523
1524 #define RGB_OUT(d, r, g, b)\
1525 {\
1526     ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1527 }
1528
1529 #define BPP 2
1530
1531 #include "imgconvert_template.h"
1532
1533 /* bgr24 handling */
1534
1535 #define RGB_NAME bgr24
1536
1537 #define RGB_IN(r, g, b, s)\
1538 {\
1539     b = (s)[0];\
1540     g = (s)[1];\
1541     r = (s)[2];\
1542 }
1543
1544 #define RGB_OUT(d, r, g, b)\
1545 {\
1546     (d)[0] = b;\
1547     (d)[1] = g;\
1548     (d)[2] = r;\
1549 }
1550
1551 #define BPP 3
1552
1553 #include "imgconvert_template.h"
1554
1555 #undef RGB_IN
1556 #undef RGB_OUT
1557 #undef BPP
1558
1559 /* rgb24 handling */
1560
1561 #define RGB_NAME rgb24
1562 #define FMT_RGB24
1563
1564 #define RGB_IN(r, g, b, s)\
1565 {\
1566     r = (s)[0];\
1567     g = (s)[1];\
1568     b = (s)[2];\
1569 }
1570
1571 #define RGB_OUT(d, r, g, b)\
1572 {\
1573     (d)[0] = r;\
1574     (d)[1] = g;\
1575     (d)[2] = b;\
1576 }
1577
1578 #define BPP 3
1579
1580 #include "imgconvert_template.h"
1581
1582 /* rgb32 handling */
1583
1584 #define RGB_NAME rgb32
1585 #define FMT_RGBA32
1586
1587 #define RGB_IN(r, g, b, s)\
1588 {\
1589     unsigned int v = ((const uint32_t *)(s))[0];\
1590     r = (v >> 16) & 0xff;\
1591     g = (v >> 8) & 0xff;\
1592     b = v & 0xff;\
1593 }
1594
1595 #define RGBA_IN(r, g, b, a, s)\
1596 {\
1597     unsigned int v = ((const uint32_t *)(s))[0];\
1598     a = 0xff;\
1599     r = (v >> 16) & 0xff;\
1600     g = (v >> 8) & 0xff;\
1601     b = v & 0xff;\
1602 }
1603
1604 #define RGBA_OUT(d, r, g, b, a)\
1605 {\
1606     ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1607 }
1608
1609 #define BPP 4
1610
1611 #include "imgconvert_template.h"
1612
1613 /* bgr32 handling */
1614
1615 #define RGB_NAME bgr32
1616
1617 #define RGB_IN(r, g, b, s)\
1618 {\
1619     unsigned int v = ((const uint32_t *)(s))[0];\
1620     r = (v >> 8) & 0xff;\
1621     g = (v >> 16) & 0xff;\
1622     b = (v >> 24) & 0xff;\
1623 }
1624
1625 #define RGBA_IN(r, g, b, a, s)\
1626 {\
1627     unsigned int v = ((const uint32_t *)(s))[0];\
1628     a = 0xff;\
1629     r = (v >> 8) & 0xff;\
1630     g = (v >> 16) & 0xff;\
1631     b = (v >> 24) & 0xff;\
1632 }
1633
1634 #define RGBA_OUT(d, r, g, b, a)\
1635 {\
1636     ((uint32_t *)(d))[0] = a | (r << 8) | (g << 16) | (b << 24);\
1637 }
1638
1639 #define BPP 4
1640
1641 #include "imgconvert_template.h"
1642
1643 /* rgba32 handling */
1644
1645 #define RGB_NAME rgba32
1646 #define FMT_RGBA32
1647
1648 #define RGB_IN(r, g, b, s)\
1649 {\
1650     unsigned int v = ((const uint32_t *)(s))[0];\
1651     r = (v >> 16) & 0xff;\
1652     g = (v >> 8) & 0xff;\
1653     b = v & 0xff;\
1654 }
1655
1656 #define RGBA_IN(r, g, b, a, s)\
1657 {\
1658     unsigned int v = ((const uint32_t *)(s))[0];\
1659     a = (v >> 24) & 0xff;\
1660     r = (v >> 16) & 0xff;\
1661     g = (v >> 8) & 0xff;\
1662     b = v & 0xff;\
1663 }
1664
1665 #define RGBA_OUT(d, r, g, b, a)\
1666 {\
1667     ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1668 }
1669
1670 #define BPP 4
1671
1672 #include "imgconvert_template.h"
1673
1674 /* bgra32 handling */
1675
1676 #define RGB_NAME bgra32
1677 #define FMT_BGRA32
1678
1679 #define RGB_IN(r, g, b, s)\
1680 {\
1681     unsigned int v = ((const uint32_t *)(s))[0];\
1682     r = (v >> 8) & 0xff;\
1683     g = (v >> 16) & 0xff;\
1684     b = (v >> 24) & 0xff;\
1685 }
1686
1687 #define RGBA_IN(r, g, b, a, s)\
1688 {\
1689     unsigned int v = ((const uint32_t *)(s))[0];\
1690     a = v & 0xff;\
1691     r = (v >> 8) & 0xff;\
1692     g = (v >> 16) & 0xff;\
1693     b = (v >> 24) & 0xff;\
1694 }
1695
1696 #define RGBA_OUT(d, r, g, b, a)\
1697 {\
1698     ((uint32_t *)(d))[0] = a | (r << 8) | (g << 16) | (b << 24 );\
1699 }
1700
1701 #define BPP 4
1702
1703 #include "imgconvert_template.h"
1704
1705 static void
1706 mono_to_gray (AVPicture * dst, const AVPicture * src,
1707     int width, int height, int xor_mask)
1708 {
1709   const unsigned char *p;
1710   unsigned char *q;
1711   int v, dst_wrap, src_wrap;
1712   int y, w;
1713
1714   p = src->data[0];
1715   src_wrap = src->linesize[0] - ((width + 7) >> 3);
1716
1717   q = dst->data[0];
1718   dst_wrap = dst->linesize[0] - width;
1719   for (y = 0; y < height; y++) {
1720     w = width;
1721     while (w >= 8) {
1722       v = *p++ ^ xor_mask;
1723       q[0] = -(v >> 7);
1724       q[1] = -((v >> 6) & 1);
1725       q[2] = -((v >> 5) & 1);
1726       q[3] = -((v >> 4) & 1);
1727       q[4] = -((v >> 3) & 1);
1728       q[5] = -((v >> 2) & 1);
1729       q[6] = -((v >> 1) & 1);
1730       q[7] = -((v >> 0) & 1);
1731       w -= 8;
1732       q += 8;
1733     }
1734     if (w > 0) {
1735       v = *p++ ^ xor_mask;
1736       do {
1737         q[0] = -((v >> 7) & 1);
1738         q++;
1739         v <<= 1;
1740       } while (--w);
1741     }
1742     p += src_wrap;
1743     q += dst_wrap;
1744   }
1745 }
1746
1747 static void
1748 monowhite_to_gray (AVPicture * dst, const AVPicture * src,
1749     int width, int height)
1750 {
1751   mono_to_gray (dst, src, width, height, 0xff);
1752 }
1753
1754 static void
1755 monoblack_to_gray (AVPicture * dst, const AVPicture * src,
1756     int width, int height)
1757 {
1758   mono_to_gray (dst, src, width, height, 0x00);
1759 }
1760
1761 static void
1762 gray_to_mono (AVPicture * dst, const AVPicture * src,
1763     int width, int height, int xor_mask)
1764 {
1765   int n;
1766   const uint8_t *s;
1767   uint8_t *d;
1768   int j, b, v, n1, src_wrap, dst_wrap, y;
1769
1770   s = src->data[0];
1771   src_wrap = src->linesize[0] - width;
1772
1773   d = dst->data[0];
1774   dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1775
1776   for (y = 0; y < height; y++) {
1777     n = width;
1778     while (n >= 8) {
1779       v = 0;
1780       for (j = 0; j < 8; j++) {
1781         b = s[0];
1782         s++;
1783         v = (v << 1) | (b >> 7);
1784       }
1785       d[0] = v ^ xor_mask;
1786       d++;
1787       n -= 8;
1788     }
1789     if (n > 0) {
1790       n1 = n;
1791       v = 0;
1792       while (n > 0) {
1793         b = s[0];
1794         s++;
1795         v = (v << 1) | (b >> 7);
1796         n--;
1797       }
1798       d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1799       d++;
1800     }
1801     s += src_wrap;
1802     d += dst_wrap;
1803   }
1804 }
1805
1806 static void
1807 gray_to_monowhite (AVPicture * dst, const AVPicture * src,
1808     int width, int height)
1809 {
1810   gray_to_mono (dst, src, width, height, 0xff);
1811 }
1812
1813 static void
1814 gray_to_monoblack (AVPicture * dst, const AVPicture * src,
1815     int width, int height)
1816 {
1817   gray_to_mono (dst, src, width, height, 0x00);
1818 }
1819
1820 typedef struct ConvertEntry
1821 {
1822   enum PixelFormat src;
1823   enum PixelFormat dest;
1824   void (*convert) (AVPicture * dst,
1825       const AVPicture * src, int width, int height);
1826 } ConvertEntry;
1827
1828 /* Add each new convertion function in this table. In order to be able
1829    to convert from any format to any format, the following constraints
1830    must be satisfied:
1831
1832    - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24 
1833
1834    - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1835
1836    - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1837
1838    - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1839      PIX_FMT_RGB24.
1840
1841    - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1842
1843    The other conversion functions are just optimisations for common cases.
1844 */
1845 static ConvertEntry convert_table[] = {
1846   {PIX_FMT_YUV420P, PIX_FMT_YUV422, yuv420p_to_yuv422},
1847   {PIX_FMT_YUV420P, PIX_FMT_RGB555, yuv420p_to_rgb555},
1848   {PIX_FMT_YUV420P, PIX_FMT_RGB565, yuv420p_to_rgb565},
1849   {PIX_FMT_YUV420P, PIX_FMT_BGR24, yuv420p_to_bgr24},
1850   {PIX_FMT_YUV420P, PIX_FMT_RGB24, yuv420p_to_rgb24},
1851   {PIX_FMT_YUV420P, PIX_FMT_RGB32, yuv420p_to_rgb32},
1852   {PIX_FMT_YUV420P, PIX_FMT_BGR32, yuv420p_to_bgr32},
1853   {PIX_FMT_YUV420P, PIX_FMT_RGBA32, yuv420p_to_rgba32},
1854   {PIX_FMT_YUV420P, PIX_FMT_BGRA32, yuv420p_to_bgra32},
1855
1856   {PIX_FMT_YUV422P, PIX_FMT_YUV422, yuv422p_to_yuv422},
1857   {PIX_FMT_YUV422P, PIX_FMT_UYVY422, yuv422p_to_uyvy422},
1858
1859   {PIX_FMT_YUV444P, PIX_FMT_RGB24, yuv444p_to_rgb24},
1860
1861   {PIX_FMT_YUVJ420P, PIX_FMT_RGB555, yuvj420p_to_rgb555},
1862   {PIX_FMT_YUVJ420P, PIX_FMT_RGB565, yuvj420p_to_rgb565},
1863   {PIX_FMT_YUVJ420P, PIX_FMT_BGR24, yuvj420p_to_bgr24},
1864   {PIX_FMT_YUVJ420P, PIX_FMT_RGB24, yuvj420p_to_rgb24},
1865   {PIX_FMT_YUVJ420P, PIX_FMT_RGB32, yuvj420p_to_rgb32},
1866   {PIX_FMT_YUVJ420P, PIX_FMT_BGR32, yuvj420p_to_bgr32},
1867   {PIX_FMT_YUVJ420P, PIX_FMT_RGBA32, yuvj420p_to_rgba32},
1868   {PIX_FMT_YUVJ420P, PIX_FMT_BGRA32, yuvj420p_to_bgra32},
1869
1870   {PIX_FMT_YUVJ444P, PIX_FMT_RGB24, yuvj444p_to_rgb24},
1871
1872   {PIX_FMT_YUV422, PIX_FMT_YUV420P, yuv422_to_yuv420p},
1873   {PIX_FMT_YUV422, PIX_FMT_YUV422P, yuv422_to_yuv422p},
1874
1875   {PIX_FMT_UYVY422, PIX_FMT_YUV420P, uyvy422_to_yuv420p},
1876   {PIX_FMT_UYVY422, PIX_FMT_YUV422P, uyvy422_to_yuv422p},
1877
1878   {PIX_FMT_RGB24, PIX_FMT_YUV420P, rgb24_to_yuv420p},
1879   {PIX_FMT_RGB24, PIX_FMT_RGB565, rgb24_to_rgb565},
1880   {PIX_FMT_RGB24, PIX_FMT_RGB555, rgb24_to_rgb555},
1881   {PIX_FMT_RGB24, PIX_FMT_RGB32, rgb24_to_rgb32},
1882   {PIX_FMT_RGB24, PIX_FMT_BGR32, rgb24_to_bgr32},
1883   {PIX_FMT_RGB24, PIX_FMT_RGBA32, rgb24_to_rgba32},
1884   {PIX_FMT_RGB24, PIX_FMT_BGR24, rgb24_to_bgr24},
1885   {PIX_FMT_RGB24, PIX_FMT_BGRA32, rgb24_to_bgra32},
1886   {PIX_FMT_RGB24, PIX_FMT_GRAY8, rgb24_to_gray},
1887   {PIX_FMT_RGB24, PIX_FMT_PAL8, rgb24_to_pal8},
1888   {PIX_FMT_RGB24, PIX_FMT_YUV444P, rgb24_to_yuv444p},
1889   {PIX_FMT_RGB24, PIX_FMT_YUVJ420P, rgb24_to_yuvj420p},
1890   {PIX_FMT_RGB24, PIX_FMT_YUVJ444P, rgb24_to_yuvj444p},
1891   {PIX_FMT_RGB24, PIX_FMT_AYUV4444, rgb24_to_ayuv4444},
1892
1893   {PIX_FMT_RGB32, PIX_FMT_RGB24, rgb32_to_rgb24},
1894   {PIX_FMT_RGB32, PIX_FMT_RGB555, rgba32_to_rgb555},
1895   {PIX_FMT_RGB32, PIX_FMT_PAL8, rgb32_to_pal8},
1896   {PIX_FMT_RGB32, PIX_FMT_YUV420P, rgb32_to_yuv420p},
1897   {PIX_FMT_RGB32, PIX_FMT_GRAY8, rgb32_to_gray},
1898
1899   {PIX_FMT_RGBA32, PIX_FMT_BGRA32, rgba32_to_bgra32},
1900   {PIX_FMT_RGBA32, PIX_FMT_BGR32, rgba32_to_bgr32},
1901   {PIX_FMT_RGBA32, PIX_FMT_RGB24, rgba32_to_rgb24},
1902   {PIX_FMT_RGBA32, PIX_FMT_RGB555, rgba32_to_rgb555},
1903   {PIX_FMT_RGBA32, PIX_FMT_PAL8, rgba32_to_pal8},
1904   {PIX_FMT_RGBA32, PIX_FMT_YUV420P, rgba32_to_yuv420p},
1905   {PIX_FMT_RGBA32, PIX_FMT_GRAY8, rgba32_to_gray},
1906   {PIX_FMT_RGBA32, PIX_FMT_AYUV4444, rgba32_to_ayuv4444},
1907   {PIX_FMT_BGRA32, PIX_FMT_AYUV4444, bgra32_to_ayuv4444},
1908
1909   {PIX_FMT_BGR24, PIX_FMT_RGB24, bgr24_to_rgb24},
1910   {PIX_FMT_BGR24, PIX_FMT_YUV420P, bgr24_to_yuv420p},
1911   {PIX_FMT_BGR24, PIX_FMT_GRAY8, bgr24_to_gray},
1912
1913   {PIX_FMT_BGR32, PIX_FMT_RGB24, bgr32_to_rgb24},
1914   {PIX_FMT_BGR32, PIX_FMT_RGBA32, bgr32_to_rgba32},
1915   {PIX_FMT_BGR32, PIX_FMT_YUV420P, bgr32_to_yuv420p},
1916   {PIX_FMT_BGR32, PIX_FMT_GRAY8, bgr32_to_gray},
1917
1918   {PIX_FMT_BGRA32, PIX_FMT_RGB24, bgra32_to_rgb24},
1919   {PIX_FMT_BGRA32, PIX_FMT_RGBA32, bgra32_to_rgba32},
1920   {PIX_FMT_BGRA32, PIX_FMT_YUV420P, bgra32_to_yuv420p},
1921   {PIX_FMT_BGRA32, PIX_FMT_GRAY8, bgra32_to_gray},
1922
1923   {PIX_FMT_RGB555, PIX_FMT_RGB24, rgb555_to_rgb24},
1924   {PIX_FMT_RGB555, PIX_FMT_RGB32, rgb555_to_rgba32},
1925   {PIX_FMT_RGB555, PIX_FMT_RGBA32, rgb555_to_rgba32},
1926   {PIX_FMT_RGB555, PIX_FMT_YUV420P, rgb555_to_yuv420p},
1927   {PIX_FMT_RGB555, PIX_FMT_GRAY8, rgb555_to_gray},
1928
1929   {PIX_FMT_RGB565, PIX_FMT_RGB24, rgb565_to_rgb24},
1930   {PIX_FMT_RGB565, PIX_FMT_YUV420P, rgb565_to_yuv420p},
1931   {PIX_FMT_RGB565, PIX_FMT_GRAY8, rgb565_to_gray},
1932
1933   {PIX_FMT_GRAY8, PIX_FMT_RGB555, gray_to_rgb555},
1934   {PIX_FMT_GRAY8, PIX_FMT_RGB565, gray_to_rgb565},
1935   {PIX_FMT_GRAY8, PIX_FMT_RGB24, gray_to_rgb24},
1936   {PIX_FMT_GRAY8, PIX_FMT_BGR24, gray_to_bgr24},
1937   {PIX_FMT_GRAY8, PIX_FMT_RGB32, gray_to_rgb32},
1938   {PIX_FMT_GRAY8, PIX_FMT_BGR32, gray_to_bgr32},
1939   {PIX_FMT_GRAY8, PIX_FMT_RGBA32, gray_to_rgba32},
1940   {PIX_FMT_GRAY8, PIX_FMT_BGRA32, gray_to_bgra32},
1941   {PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, gray_to_monowhite},
1942   {PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, gray_to_monoblack},
1943
1944   {PIX_FMT_MONOWHITE, PIX_FMT_GRAY8, monowhite_to_gray},
1945
1946   {PIX_FMT_MONOBLACK, PIX_FMT_GRAY8, monoblack_to_gray},
1947
1948   {PIX_FMT_PAL8, PIX_FMT_RGB555, pal8_to_rgb555},
1949   {PIX_FMT_PAL8, PIX_FMT_RGB565, pal8_to_rgb565},
1950   {PIX_FMT_PAL8, PIX_FMT_BGR24, pal8_to_bgr24},
1951   {PIX_FMT_PAL8, PIX_FMT_RGB24, pal8_to_rgb24},
1952   {PIX_FMT_PAL8, PIX_FMT_RGB32, pal8_to_rgb32},
1953   {PIX_FMT_PAL8, PIX_FMT_BGR32, pal8_to_bgr32},
1954   {PIX_FMT_PAL8, PIX_FMT_RGBA32, pal8_to_rgba32},
1955   {PIX_FMT_PAL8, PIX_FMT_BGRA32, pal8_to_bgra32},
1956
1957   {PIX_FMT_UYVY411, PIX_FMT_YUV411P, uyvy411_to_yuv411p},
1958
1959   {PIX_FMT_AYUV4444, PIX_FMT_RGBA32, ayuv4444_to_rgba32},
1960
1961   {PIX_FMT_AYUV4444, PIX_FMT_RGB24, ayuv4444_to_rgb24}
1962 };
1963
1964 static ConvertEntry *
1965 get_convert_table_entry (int src_pix_fmt, int dst_pix_fmt)
1966 {
1967   int i;
1968
1969   for (i = 0; i < sizeof (convert_table) / sizeof (convert_table[0]); i++) {
1970     if (convert_table[i].src == src_pix_fmt &&
1971         convert_table[i].dest == dst_pix_fmt) {
1972       return convert_table + i;
1973     }
1974   }
1975
1976   return NULL;
1977 }
1978
1979 int
1980 avpicture_alloc (AVPicture * picture, int pix_fmt, int width, int height)
1981 {
1982   unsigned int size;
1983   void *ptr;
1984
1985   size = avpicture_get_size (pix_fmt, width, height);
1986   ptr = av_malloc (size);
1987   if (!ptr)
1988     goto fail;
1989   gst_ffmpegcsp_avpicture_fill (picture, ptr, pix_fmt, width, height);
1990   return 0;
1991 fail:
1992   memset (picture, 0, sizeof (AVPicture));
1993   return -1;
1994 }
1995
1996 void
1997 avpicture_free (AVPicture * picture)
1998 {
1999   av_free (picture->data[0]);
2000 }
2001
2002 /* return true if yuv planar */
2003 static inline int
2004 is_yuv_planar (PixFmtInfo * ps)
2005 {
2006   return (ps->color_type == FF_COLOR_YUV ||
2007       ps->color_type == FF_COLOR_YUV_JPEG) && ps->pixel_type == FF_PIXEL_PLANAR;
2008 }
2009
2010 /* XXX: always use linesize. Return -1 if not supported */
2011 int
2012 img_convert (AVPicture * dst, int dst_pix_fmt,
2013     const AVPicture * src, int src_pix_fmt, int src_width, int src_height)
2014 {
2015   static int inited;
2016   int i, ret, dst_width, dst_height, int_pix_fmt;
2017   PixFmtInfo *src_pix, *dst_pix;
2018   ConvertEntry *ce;
2019   AVPicture tmp1, *tmp = &tmp1;
2020
2021   if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2022       dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2023     return -1;
2024   if (src_width <= 0 || src_height <= 0)
2025     return 0;
2026
2027   if (!inited) {
2028     inited = 1;
2029     img_convert_init ();
2030   }
2031
2032   dst_width = src_width;
2033   dst_height = src_height;
2034
2035   dst_pix = get_pix_fmt_info (dst_pix_fmt);
2036   src_pix = get_pix_fmt_info (src_pix_fmt);
2037   if (src_pix_fmt == dst_pix_fmt) {
2038     /* no conversion needed: just copy */
2039     img_copy (dst, src, dst_pix_fmt, dst_width, dst_height);
2040     return 0;
2041   }
2042
2043   ce = get_convert_table_entry (src_pix_fmt, dst_pix_fmt);
2044   if (ce && ce->convert) {
2045     /* specific conversion routine */
2046     ce->convert (dst, src, dst_width, dst_height);
2047     return 0;
2048   }
2049
2050   /* gray to YUV */
2051   if (is_yuv_planar (dst_pix) && src_pix_fmt == PIX_FMT_GRAY8) {
2052     int w, h, y;
2053     uint8_t *d;
2054
2055     if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2056       img_copy_plane (dst->data[0], dst->linesize[0],
2057           src->data[0], src->linesize[0], dst_width, dst_height);
2058     } else {
2059       img_apply_table (dst->data[0], dst->linesize[0],
2060           src->data[0], src->linesize[0],
2061           dst_width, dst_height, y_jpeg_to_ccir);
2062     }
2063     /* fill U and V with 128 */
2064     w = dst_width;
2065     h = dst_height;
2066     w >>= dst_pix->x_chroma_shift;
2067     h >>= dst_pix->y_chroma_shift;
2068     for (i = 1; i <= 2; i++) {
2069       d = dst->data[i];
2070       for (y = 0; y < h; y++) {
2071         memset (d, 128, w);
2072         d += dst->linesize[i];
2073       }
2074     }
2075     return 0;
2076   }
2077
2078   /* YUV to gray */
2079   if (is_yuv_planar (src_pix) && dst_pix_fmt == PIX_FMT_GRAY8) {
2080     if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2081       img_copy_plane (dst->data[0], dst->linesize[0],
2082           src->data[0], src->linesize[0], dst_width, dst_height);
2083     } else {
2084       img_apply_table (dst->data[0], dst->linesize[0],
2085           src->data[0], src->linesize[0],
2086           dst_width, dst_height, y_ccir_to_jpeg);
2087     }
2088     return 0;
2089   }
2090
2091   /* YUV to YUV planar */
2092   if (is_yuv_planar (dst_pix) && is_yuv_planar (src_pix)) {
2093     int x_shift, y_shift, w, h, xy_shift;
2094     void (*resize_func) (uint8_t * dst, int dst_wrap,
2095         const uint8_t * src, int src_wrap, int width, int height);
2096
2097     /* compute chroma size of the smallest dimensions */
2098     w = dst_width;
2099     h = dst_height;
2100     if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2101       w >>= dst_pix->x_chroma_shift;
2102     else
2103       w >>= src_pix->x_chroma_shift;
2104     if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2105       h >>= dst_pix->y_chroma_shift;
2106     else
2107       h >>= src_pix->y_chroma_shift;
2108
2109     x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2110     y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2111     xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2112     /* there must be filters for conversion at least from and to
2113        YUV444 format */
2114     switch (xy_shift) {
2115       case 0x00:
2116         resize_func = img_copy_plane;
2117         break;
2118       case 0x10:
2119         resize_func = shrink21;
2120         break;
2121       case 0x20:
2122         resize_func = shrink41;
2123         break;
2124       case 0x01:
2125         resize_func = shrink12;
2126         break;
2127       case 0x11:
2128         resize_func = shrink22;
2129         break;
2130       case 0x22:
2131         resize_func = shrink44;
2132         break;
2133       case 0xf0:
2134         resize_func = grow21;
2135         break;
2136       case 0xe0:
2137         resize_func = grow41;
2138         break;
2139       case 0xff:
2140         resize_func = grow22;
2141         break;
2142       case 0xee:
2143         resize_func = grow44;
2144         break;
2145       case 0xf1:
2146         resize_func = conv411;
2147         break;
2148       default:
2149         /* currently not handled */
2150         goto no_chroma_filter;
2151     }
2152
2153     img_copy_plane (dst->data[0], dst->linesize[0],
2154         src->data[0], src->linesize[0], dst_width, dst_height);
2155
2156 #define GEN_MASK(x) ((1<<(x))-1)
2157 #define DIV_ROUND_UP_X(v,x) (((v) + GEN_MASK(x)) >> (x))
2158
2159     for (i = 1; i <= 2; i++)
2160       resize_func (dst->data[i], dst->linesize[i],
2161           src->data[i], src->linesize[i],
2162           DIV_ROUND_UP_X (dst_width, dst_pix->x_chroma_shift),
2163           DIV_ROUND_UP_X (dst_height, dst_pix->y_chroma_shift));
2164     /* if yuv color space conversion is needed, we do it here on
2165        the destination image */
2166     if (dst_pix->color_type != src_pix->color_type) {
2167       const uint8_t *y_table, *c_table;
2168
2169       if (dst_pix->color_type == FF_COLOR_YUV) {
2170         y_table = y_jpeg_to_ccir;
2171         c_table = c_jpeg_to_ccir;
2172       } else {
2173         y_table = y_ccir_to_jpeg;
2174         c_table = c_ccir_to_jpeg;
2175       }
2176       img_apply_table (dst->data[0], dst->linesize[0],
2177           dst->data[0], dst->linesize[0], dst_width, dst_height, y_table);
2178
2179       for (i = 1; i <= 2; i++)
2180         img_apply_table (dst->data[i], dst->linesize[i],
2181             dst->data[i], dst->linesize[i],
2182             dst_width >> dst_pix->x_chroma_shift,
2183             dst_height >> dst_pix->y_chroma_shift, c_table);
2184     }
2185     return 0;
2186   }
2187 no_chroma_filter:
2188
2189   /* try to use an intermediate format */
2190   if (src_pix_fmt == PIX_FMT_YUV422 || dst_pix_fmt == PIX_FMT_YUV422) {
2191     /* specific case: convert to YUV422P first */
2192     int_pix_fmt = PIX_FMT_YUV422P;
2193   } else if (src_pix_fmt == PIX_FMT_UYVY422 || dst_pix_fmt == PIX_FMT_UYVY422) {
2194     /* specific case: convert to YUV422P first */
2195     int_pix_fmt = PIX_FMT_YUV422P;
2196   } else if (src_pix_fmt == PIX_FMT_UYVY411 || dst_pix_fmt == PIX_FMT_UYVY411) {
2197     /* specific case: convert to YUV411P first */
2198     int_pix_fmt = PIX_FMT_YUV411P;
2199   } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2200           src_pix_fmt != PIX_FMT_GRAY8) ||
2201       (dst_pix->color_type == FF_COLOR_GRAY && dst_pix_fmt != PIX_FMT_GRAY8)) {
2202     /* gray8 is the normalized format */
2203     int_pix_fmt = PIX_FMT_GRAY8;
2204   } else if ((is_yuv_planar (src_pix) &&
2205           src_pix_fmt != PIX_FMT_YUV444P && src_pix_fmt != PIX_FMT_YUVJ444P)) {
2206     /* yuv444 is the normalized format */
2207     if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2208       int_pix_fmt = PIX_FMT_YUVJ444P;
2209     else
2210       int_pix_fmt = PIX_FMT_YUV444P;
2211   } else if ((is_yuv_planar (dst_pix) &&
2212           dst_pix_fmt != PIX_FMT_YUV444P && dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2213     /* yuv444 is the normalized format */
2214     if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2215       int_pix_fmt = PIX_FMT_YUVJ444P;
2216     else
2217       int_pix_fmt = PIX_FMT_YUV444P;
2218   } else {
2219     /* the two formats are rgb or gray8 or yuv[j]444p */
2220     if (src_pix->is_alpha && dst_pix->is_alpha)
2221       int_pix_fmt = PIX_FMT_RGBA32;
2222     else
2223       int_pix_fmt = PIX_FMT_RGB24;
2224   }
2225   if (avpicture_alloc (tmp, int_pix_fmt, dst_width, dst_height) < 0)
2226     return -1;
2227   ret = -1;
2228   if (img_convert (tmp, int_pix_fmt,
2229           src, src_pix_fmt, src_width, src_height) < 0)
2230     goto fail1;
2231   if (img_convert (dst, dst_pix_fmt,
2232           tmp, int_pix_fmt, dst_width, dst_height) < 0)
2233     goto fail1;
2234   ret = 0;
2235 fail1:
2236   avpicture_free (tmp);
2237   return ret;
2238 }
2239
2240 /* NOTE: we scan all the pixels to have an exact information */
2241 static int
2242 get_alpha_info_pal8 (const AVPicture * src, int width, int height)
2243 {
2244   const unsigned char *p;
2245   int src_wrap, ret, x, y;
2246   unsigned int a;
2247   uint32_t *palette = (uint32_t *) src->data[1];
2248
2249   p = src->data[0];
2250   src_wrap = src->linesize[0] - width;
2251   ret = 0;
2252   for (y = 0; y < height; y++) {
2253     for (x = 0; x < width; x++) {
2254       a = palette[p[0]] >> 24;
2255       if (a == 0x00) {
2256         ret |= FF_ALPHA_TRANSP;
2257       } else if (a != 0xff) {
2258         ret |= FF_ALPHA_SEMI_TRANSP;
2259       }
2260       p++;
2261     }
2262     p += src_wrap;
2263   }
2264   return ret;
2265 }
2266
2267 /**
2268  * Tell if an image really has transparent alpha values.
2269  * @return ored mask of FF_ALPHA_xxx constants
2270  */
2271 int
2272 img_get_alpha_info (const AVPicture * src, int pix_fmt, int width, int height)
2273 {
2274   PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
2275   int ret;
2276
2277   pf = get_pix_fmt_info (pix_fmt);
2278   /* no alpha can be represented in format */
2279   if (!pf->is_alpha)
2280     return 0;
2281   switch (pix_fmt) {
2282     case PIX_FMT_RGB32:
2283       ret = get_alpha_info_rgb32 (src, width, height);
2284       break;
2285     case PIX_FMT_BGR32:
2286       ret = get_alpha_info_bgr32 (src, width, height);
2287       break;
2288     case PIX_FMT_RGBA32:
2289       ret = get_alpha_info_rgba32 (src, width, height);
2290       break;
2291     case PIX_FMT_BGRA32:
2292       ret = get_alpha_info_bgra32 (src, width, height);
2293       break;
2294     case PIX_FMT_RGB555:
2295       ret = get_alpha_info_rgb555 (src, width, height);
2296       break;
2297     case PIX_FMT_PAL8:
2298       ret = get_alpha_info_pal8 (src, width, height);
2299       break;
2300     default:
2301       /* we do not know, so everything is indicated */
2302       ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2303       break;
2304   }
2305   return ret;
2306 }
2307
2308 #ifdef HAVE_MMX
2309 #define DEINT_INPLACE_LINE_LUM \
2310                     movd_m2r(lum_m4[0],mm0);\
2311                     movd_m2r(lum_m3[0],mm1);\
2312                     movd_m2r(lum_m2[0],mm2);\
2313                     movd_m2r(lum_m1[0],mm3);\
2314                     movd_m2r(lum[0],mm4);\
2315                     punpcklbw_r2r(mm7,mm0);\
2316                     movd_r2m(mm2,lum_m4[0]);\
2317                     punpcklbw_r2r(mm7,mm1);\
2318                     punpcklbw_r2r(mm7,mm2);\
2319                     punpcklbw_r2r(mm7,mm3);\
2320                     punpcklbw_r2r(mm7,mm4);\
2321                     paddw_r2r(mm3,mm1);\
2322                     psllw_i2r(1,mm2);\
2323                     paddw_r2r(mm4,mm0);\
2324                     psllw_i2r(2,mm1);\
2325                     paddw_r2r(mm6,mm2);\
2326                     paddw_r2r(mm2,mm1);\
2327                     psubusw_r2r(mm0,mm1);\
2328                     psrlw_i2r(3,mm1);\
2329                     packuswb_r2r(mm7,mm1);\
2330                     movd_r2m(mm1,lum_m2[0]);
2331
2332 #define DEINT_LINE_LUM \
2333                     movd_m2r(lum_m4[0],mm0);\
2334                     movd_m2r(lum_m3[0],mm1);\
2335                     movd_m2r(lum_m2[0],mm2);\
2336                     movd_m2r(lum_m1[0],mm3);\
2337                     movd_m2r(lum[0],mm4);\
2338                     punpcklbw_r2r(mm7,mm0);\
2339                     punpcklbw_r2r(mm7,mm1);\
2340                     punpcklbw_r2r(mm7,mm2);\
2341                     punpcklbw_r2r(mm7,mm3);\
2342                     punpcklbw_r2r(mm7,mm4);\
2343                     paddw_r2r(mm3,mm1);\
2344                     psllw_i2r(1,mm2);\
2345                     paddw_r2r(mm4,mm0);\
2346                     psllw_i2r(2,mm1);\
2347                     paddw_r2r(mm6,mm2);\
2348                     paddw_r2r(mm2,mm1);\
2349                     psubusw_r2r(mm0,mm1);\
2350                     psrlw_i2r(3,mm1);\
2351                     packuswb_r2r(mm7,mm1);\
2352                     movd_r2m(mm1,dst[0]);
2353 #endif
2354
2355 /* filter parameters: [-1 4 2 4 -1] // 8 */
2356 static void
2357 deinterlace_line (uint8_t * dst,
2358     const uint8_t * lum_m4, const uint8_t * lum_m3,
2359     const uint8_t * lum_m2, const uint8_t * lum_m1,
2360     const uint8_t * lum, int size)
2361 {
2362 #ifndef HAVE_MMX
2363   uint8_t *cm = cropTbl + MAX_NEG_CROP;
2364   int sum;
2365
2366   for (; size > 0; size--) {
2367     sum = -lum_m4[0];
2368     sum += lum_m3[0] << 2;
2369     sum += lum_m2[0] << 1;
2370     sum += lum_m1[0] << 2;
2371     sum += -lum[0];
2372     dst[0] = cm[(sum + 4) >> 3];
2373     lum_m4++;
2374     lum_m3++;
2375     lum_m2++;
2376     lum_m1++;
2377     lum++;
2378     dst++;
2379   }
2380 #else
2381
2382   {
2383     mmx_t rounder;
2384
2385     rounder.uw[0] = 4;
2386     rounder.uw[1] = 4;
2387     rounder.uw[2] = 4;
2388     rounder.uw[3] = 4;
2389     pxor_r2r (mm7, mm7);
2390     movq_m2r (rounder, mm6);
2391   }
2392   for (; size > 3; size -= 4) {
2393     DEINT_LINE_LUM lum_m4 += 4;
2394
2395     lum_m3 += 4;
2396     lum_m2 += 4;
2397     lum_m1 += 4;
2398     lum += 4;
2399     dst += 4;
2400   }
2401 #endif
2402 }
2403 static void
2404 deinterlace_line_inplace (uint8_t * lum_m4, uint8_t * lum_m3, uint8_t * lum_m2,
2405     uint8_t * lum_m1, uint8_t * lum, int size)
2406 {
2407 #ifndef HAVE_MMX
2408   uint8_t *cm = cropTbl + MAX_NEG_CROP;
2409   int sum;
2410
2411   for (; size > 0; size--) {
2412     sum = -lum_m4[0];
2413     sum += lum_m3[0] << 2;
2414     sum += lum_m2[0] << 1;
2415     lum_m4[0] = lum_m2[0];
2416     sum += lum_m1[0] << 2;
2417     sum += -lum[0];
2418     lum_m2[0] = cm[(sum + 4) >> 3];
2419     lum_m4++;
2420     lum_m3++;
2421     lum_m2++;
2422     lum_m1++;
2423     lum++;
2424   }
2425 #else
2426
2427   {
2428     mmx_t rounder;
2429
2430     rounder.uw[0] = 4;
2431     rounder.uw[1] = 4;
2432     rounder.uw[2] = 4;
2433     rounder.uw[3] = 4;
2434     pxor_r2r (mm7, mm7);
2435     movq_m2r (rounder, mm6);
2436   }
2437   for (; size > 3; size -= 4) {
2438     DEINT_INPLACE_LINE_LUM lum_m4 += 4;
2439
2440     lum_m3 += 4;
2441     lum_m2 += 4;
2442     lum_m1 += 4;
2443     lum += 4;
2444   }
2445 #endif
2446 }
2447
2448 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2449    top field is copied as is, but the bottom field is deinterlaced
2450    against the top field. */
2451 static void
2452 deinterlace_bottom_field (uint8_t * dst, int dst_wrap,
2453     const uint8_t * src1, int src_wrap, int width, int height)
2454 {
2455   const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2456   int y;
2457
2458   src_m2 = src1;
2459   src_m1 = src1;
2460   src_0 = &src_m1[src_wrap];
2461   src_p1 = &src_0[src_wrap];
2462   src_p2 = &src_p1[src_wrap];
2463   for (y = 0; y < (height - 2); y += 2) {
2464     memcpy (dst, src_m1, width);
2465     dst += dst_wrap;
2466     deinterlace_line (dst, src_m2, src_m1, src_0, src_p1, src_p2, width);
2467     src_m2 = src_0;
2468     src_m1 = src_p1;
2469     src_0 = src_p2;
2470     src_p1 += 2 * src_wrap;
2471     src_p2 += 2 * src_wrap;
2472     dst += dst_wrap;
2473   }
2474   memcpy (dst, src_m1, width);
2475   dst += dst_wrap;
2476   /* do last line */
2477   deinterlace_line (dst, src_m2, src_m1, src_0, src_0, src_0, width);
2478 }
2479
2480 static void
2481 deinterlace_bottom_field_inplace (uint8_t * src1, int src_wrap,
2482     int width, int height)
2483 {
2484   uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2485   int y;
2486   uint8_t *buf;
2487
2488   buf = (uint8_t *) av_malloc (width);
2489
2490   src_m1 = src1;
2491   memcpy (buf, src_m1, width);
2492   src_0 = &src_m1[src_wrap];
2493   src_p1 = &src_0[src_wrap];
2494   src_p2 = &src_p1[src_wrap];
2495   for (y = 0; y < (height - 2); y += 2) {
2496     deinterlace_line_inplace (buf, src_m1, src_0, src_p1, src_p2, width);
2497     src_m1 = src_p1;
2498     src_0 = src_p2;
2499     src_p1 += 2 * src_wrap;
2500     src_p2 += 2 * src_wrap;
2501   }
2502   /* do last line */
2503   deinterlace_line_inplace (buf, src_m1, src_0, src_0, src_0, width);
2504   av_free (buf);
2505 }
2506
2507
2508 /* deinterlace - if not supported return -1 */
2509 int
2510 avpicture_deinterlace (AVPicture * dst, const AVPicture * src,
2511     int pix_fmt, int width, int height)
2512 {
2513   int i;
2514
2515   if (pix_fmt != PIX_FMT_YUV420P &&
2516       pix_fmt != PIX_FMT_YUV422P &&
2517       pix_fmt != PIX_FMT_YUV444P && pix_fmt != PIX_FMT_YUV411P)
2518     return -1;
2519   if ((width & 3) != 0 || (height & 3) != 0)
2520     return -1;
2521
2522   for (i = 0; i < 3; i++) {
2523     if (i == 1) {
2524       switch (pix_fmt) {
2525         case PIX_FMT_YUV420P:
2526           width >>= 1;
2527           height >>= 1;
2528           break;
2529         case PIX_FMT_YUV422P:
2530           width >>= 1;
2531           break;
2532         case PIX_FMT_YUV411P:
2533           width >>= 2;
2534           break;
2535         default:
2536           break;
2537       }
2538     }
2539     if (src == dst) {
2540       deinterlace_bottom_field_inplace (dst->data[i], dst->linesize[i],
2541           width, height);
2542     } else {
2543       deinterlace_bottom_field (dst->data[i], dst->linesize[i],
2544           src->data[i], src->linesize[i], width, height);
2545     }
2546   }
2547 #ifdef HAVE_MMX
2548   emms ();
2549 #endif
2550   return 0;
2551 }
2552
2553 #undef FIX