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