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