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