]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgconvert.c
Use pixel formats names defined in av_pix_fmt_descriptors rather than
[ffmpeg] / libavcodec / imgconvert.c
1 /*
2  * Misc image conversion routines
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file libavcodec/imgconvert.c
24  * misc image conversion routines
25  */
26
27 /* TODO:
28  * - write 'ffimg' program to test all the image related stuff
29  * - move all api to slice based system
30  * - integrate deinterlacing, postprocessing and scaling in the conversion process
31  */
32
33 #include "avcodec.h"
34 #include "dsputil.h"
35 #include "colorspace.h"
36 #include "libavutil/pixdesc.h"
37
38 #if HAVE_MMX
39 #include "x86/mmx.h"
40 #include "x86/dsputil_mmx.h"
41 #endif
42
43 #define xglue(x, y) x ## y
44 #define glue(x, y) xglue(x, y)
45
46 #define FF_COLOR_RGB      0 /**< RGB color space */
47 #define FF_COLOR_GRAY     1 /**< gray color space */
48 #define FF_COLOR_YUV      2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
49 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
50
51 #define FF_PIXEL_PLANAR   0 /**< each channel has one component in AVPicture */
52 #define FF_PIXEL_PACKED   1 /**< only one components containing all the channels */
53 #define FF_PIXEL_PALETTE  2  /**< one components containing indexes for a palette */
54
55 typedef struct PixFmtInfo {
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 depth;           /**< bit depth of the color components */
61 } PixFmtInfo;
62
63 /* this table gives more information about formats */
64 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
65     /* YUV formats */
66     [PIX_FMT_YUV420P] = {
67         .nb_channels = 3,
68         .color_type = FF_COLOR_YUV,
69         .pixel_type = FF_PIXEL_PLANAR,
70         .depth = 8,
71     },
72     [PIX_FMT_YUV422P] = {
73         .nb_channels = 3,
74         .color_type = FF_COLOR_YUV,
75         .pixel_type = FF_PIXEL_PLANAR,
76         .depth = 8,
77     },
78     [PIX_FMT_YUV444P] = {
79         .nb_channels = 3,
80         .color_type = FF_COLOR_YUV,
81         .pixel_type = FF_PIXEL_PLANAR,
82         .depth = 8,
83     },
84     [PIX_FMT_YUYV422] = {
85         .nb_channels = 1,
86         .color_type = FF_COLOR_YUV,
87         .pixel_type = FF_PIXEL_PACKED,
88         .depth = 8,
89     },
90     [PIX_FMT_UYVY422] = {
91         .nb_channels = 1,
92         .color_type = FF_COLOR_YUV,
93         .pixel_type = FF_PIXEL_PACKED,
94         .depth = 8,
95     },
96     [PIX_FMT_YUV410P] = {
97         .nb_channels = 3,
98         .color_type = FF_COLOR_YUV,
99         .pixel_type = FF_PIXEL_PLANAR,
100         .depth = 8,
101     },
102     [PIX_FMT_YUV411P] = {
103         .nb_channels = 3,
104         .color_type = FF_COLOR_YUV,
105         .pixel_type = FF_PIXEL_PLANAR,
106         .depth = 8,
107     },
108     [PIX_FMT_YUV440P] = {
109         .nb_channels = 3,
110         .color_type = FF_COLOR_YUV,
111         .pixel_type = FF_PIXEL_PLANAR,
112         .depth = 8,
113     },
114     [PIX_FMT_YUV420P16LE] = {
115         .nb_channels = 3,
116         .color_type = FF_COLOR_YUV,
117         .pixel_type = FF_PIXEL_PLANAR,
118         .depth = 16,
119     },
120     [PIX_FMT_YUV422P16LE] = {
121         .nb_channels = 3,
122         .color_type = FF_COLOR_YUV,
123         .pixel_type = FF_PIXEL_PLANAR,
124         .depth = 16,
125     },
126     [PIX_FMT_YUV444P16LE] = {
127         .nb_channels = 3,
128         .color_type = FF_COLOR_YUV,
129         .pixel_type = FF_PIXEL_PLANAR,
130         .depth = 16,
131     },
132     [PIX_FMT_YUV420P16BE] = {
133         .nb_channels = 3,
134         .color_type = FF_COLOR_YUV,
135         .pixel_type = FF_PIXEL_PLANAR,
136         .depth = 16,
137     },
138     [PIX_FMT_YUV422P16BE] = {
139         .nb_channels = 3,
140         .color_type = FF_COLOR_YUV,
141         .pixel_type = FF_PIXEL_PLANAR,
142         .depth = 16,
143     },
144     [PIX_FMT_YUV444P16BE] = {
145         .nb_channels = 3,
146         .color_type = FF_COLOR_YUV,
147         .pixel_type = FF_PIXEL_PLANAR,
148         .depth = 16,
149     },
150
151
152     /* YUV formats with alpha plane */
153     [PIX_FMT_YUVA420P] = {
154         .nb_channels = 4,
155         .color_type = FF_COLOR_YUV,
156         .pixel_type = FF_PIXEL_PLANAR,
157         .depth = 8,
158     },
159
160     /* JPEG YUV */
161     [PIX_FMT_YUVJ420P] = {
162         .nb_channels = 3,
163         .color_type = FF_COLOR_YUV_JPEG,
164         .pixel_type = FF_PIXEL_PLANAR,
165         .depth = 8,
166     },
167     [PIX_FMT_YUVJ422P] = {
168         .nb_channels = 3,
169         .color_type = FF_COLOR_YUV_JPEG,
170         .pixel_type = FF_PIXEL_PLANAR,
171         .depth = 8,
172     },
173     [PIX_FMT_YUVJ444P] = {
174         .nb_channels = 3,
175         .color_type = FF_COLOR_YUV_JPEG,
176         .pixel_type = FF_PIXEL_PLANAR,
177         .depth = 8,
178     },
179     [PIX_FMT_YUVJ440P] = {
180         .nb_channels = 3,
181         .color_type = FF_COLOR_YUV_JPEG,
182         .pixel_type = FF_PIXEL_PLANAR,
183         .depth = 8,
184     },
185
186     /* RGB formats */
187     [PIX_FMT_RGB24] = {
188         .nb_channels = 3,
189         .color_type = FF_COLOR_RGB,
190         .pixel_type = FF_PIXEL_PACKED,
191         .depth = 8,
192     },
193     [PIX_FMT_BGR24] = {
194         .nb_channels = 3,
195         .color_type = FF_COLOR_RGB,
196         .pixel_type = FF_PIXEL_PACKED,
197         .depth = 8,
198     },
199     [PIX_FMT_ARGB] = {
200         .nb_channels = 4, .is_alpha = 1,
201         .color_type = FF_COLOR_RGB,
202         .pixel_type = FF_PIXEL_PACKED,
203         .depth = 8,
204     },
205     [PIX_FMT_RGB48BE] = {
206         .nb_channels = 3,
207         .color_type = FF_COLOR_RGB,
208         .pixel_type = FF_PIXEL_PACKED,
209         .depth = 16,
210     },
211     [PIX_FMT_RGB48LE] = {
212         .nb_channels = 3,
213         .color_type = FF_COLOR_RGB,
214         .pixel_type = FF_PIXEL_PACKED,
215         .depth = 16,
216     },
217     [PIX_FMT_RGB565BE] = {
218         .nb_channels = 3,
219         .color_type = FF_COLOR_RGB,
220         .pixel_type = FF_PIXEL_PACKED,
221         .depth = 5,
222     },
223     [PIX_FMT_RGB565LE] = {
224         .nb_channels = 3,
225         .color_type = FF_COLOR_RGB,
226         .pixel_type = FF_PIXEL_PACKED,
227         .depth = 5,
228     },
229     [PIX_FMT_RGB555BE] = {
230         .nb_channels = 3,
231         .color_type = FF_COLOR_RGB,
232         .pixel_type = FF_PIXEL_PACKED,
233         .depth = 5,
234     },
235     [PIX_FMT_RGB555LE] = {
236         .nb_channels = 3,
237         .color_type = FF_COLOR_RGB,
238         .pixel_type = FF_PIXEL_PACKED,
239         .depth = 5,
240     },
241
242     /* gray / mono formats */
243     [PIX_FMT_GRAY16BE] = {
244         .nb_channels = 1,
245         .color_type = FF_COLOR_GRAY,
246         .pixel_type = FF_PIXEL_PLANAR,
247         .depth = 16,
248     },
249     [PIX_FMT_GRAY16LE] = {
250         .nb_channels = 1,
251         .color_type = FF_COLOR_GRAY,
252         .pixel_type = FF_PIXEL_PLANAR,
253         .depth = 16,
254     },
255     [PIX_FMT_GRAY8] = {
256         .nb_channels = 1,
257         .color_type = FF_COLOR_GRAY,
258         .pixel_type = FF_PIXEL_PLANAR,
259         .depth = 8,
260     },
261     [PIX_FMT_MONOWHITE] = {
262         .nb_channels = 1,
263         .color_type = FF_COLOR_GRAY,
264         .pixel_type = FF_PIXEL_PLANAR,
265         .depth = 1,
266     },
267     [PIX_FMT_MONOBLACK] = {
268         .nb_channels = 1,
269         .color_type = FF_COLOR_GRAY,
270         .pixel_type = FF_PIXEL_PLANAR,
271         .depth = 1,
272     },
273
274     /* paletted formats */
275     [PIX_FMT_PAL8] = {
276         .nb_channels = 4, .is_alpha = 1,
277         .color_type = FF_COLOR_RGB,
278         .pixel_type = FF_PIXEL_PALETTE,
279         .depth = 8,
280     },
281     [PIX_FMT_XVMC_MPEG2_MC] = {
282     },
283     [PIX_FMT_XVMC_MPEG2_IDCT] = {
284     },
285     [PIX_FMT_VDPAU_MPEG1] = {
286     },
287     [PIX_FMT_VDPAU_MPEG2] = {
288     },
289     [PIX_FMT_VDPAU_H264] = {
290     },
291     [PIX_FMT_VDPAU_WMV3] = {
292     },
293     [PIX_FMT_VDPAU_VC1] = {
294     },
295     [PIX_FMT_VDPAU_MPEG4] = {
296     },
297     [PIX_FMT_UYYVYY411] = {
298         .nb_channels = 1,
299         .color_type = FF_COLOR_YUV,
300         .pixel_type = FF_PIXEL_PACKED,
301         .depth = 8,
302     },
303     [PIX_FMT_ABGR] = {
304         .nb_channels = 4, .is_alpha = 1,
305         .color_type = FF_COLOR_RGB,
306         .pixel_type = FF_PIXEL_PACKED,
307         .depth = 8,
308     },
309     [PIX_FMT_BGR565BE] = {
310         .nb_channels = 3,
311         .color_type = FF_COLOR_RGB,
312         .pixel_type = FF_PIXEL_PACKED,
313         .depth = 5,
314     },
315     [PIX_FMT_BGR565LE] = {
316         .nb_channels = 3,
317         .color_type = FF_COLOR_RGB,
318         .pixel_type = FF_PIXEL_PACKED,
319         .depth = 5,
320     },
321     [PIX_FMT_BGR555BE] = {
322         .nb_channels = 3,
323         .color_type = FF_COLOR_RGB,
324         .pixel_type = FF_PIXEL_PACKED,
325         .depth = 5,
326     },
327     [PIX_FMT_BGR555LE] = {
328         .nb_channels = 3,
329         .color_type = FF_COLOR_RGB,
330         .pixel_type = FF_PIXEL_PACKED,
331         .depth = 5,
332     },
333     [PIX_FMT_RGB8] = {
334         .nb_channels = 1,
335         .color_type = FF_COLOR_RGB,
336         .pixel_type = FF_PIXEL_PACKED,
337         .depth = 8,
338     },
339     [PIX_FMT_RGB4] = {
340         .nb_channels = 1,
341         .color_type = FF_COLOR_RGB,
342         .pixel_type = FF_PIXEL_PACKED,
343         .depth = 4,
344     },
345     [PIX_FMT_RGB4_BYTE] = {
346         .nb_channels = 1,
347         .color_type = FF_COLOR_RGB,
348         .pixel_type = FF_PIXEL_PACKED,
349         .depth = 8,
350     },
351     [PIX_FMT_BGR8] = {
352         .nb_channels = 1,
353         .color_type = FF_COLOR_RGB,
354         .pixel_type = FF_PIXEL_PACKED,
355         .depth = 8,
356     },
357     [PIX_FMT_BGR4] = {
358         .nb_channels = 1,
359         .color_type = FF_COLOR_RGB,
360         .pixel_type = FF_PIXEL_PACKED,
361         .depth = 4,
362     },
363     [PIX_FMT_BGR4_BYTE] = {
364         .nb_channels = 1,
365         .color_type = FF_COLOR_RGB,
366         .pixel_type = FF_PIXEL_PACKED,
367         .depth = 8,
368     },
369     [PIX_FMT_NV12] = {
370         .nb_channels = 2,
371         .color_type = FF_COLOR_YUV,
372         .pixel_type = FF_PIXEL_PLANAR,
373         .depth = 8,
374     },
375     [PIX_FMT_NV21] = {
376         .nb_channels = 2,
377         .color_type = FF_COLOR_YUV,
378         .pixel_type = FF_PIXEL_PLANAR,
379         .depth = 8,
380     },
381
382     [PIX_FMT_BGRA] = {
383         .nb_channels = 4, .is_alpha = 1,
384         .color_type = FF_COLOR_RGB,
385         .pixel_type = FF_PIXEL_PACKED,
386         .depth = 8,
387     },
388     [PIX_FMT_RGBA] = {
389         .nb_channels = 4, .is_alpha = 1,
390         .color_type = FF_COLOR_RGB,
391         .pixel_type = FF_PIXEL_PACKED,
392         .depth = 8,
393     },
394
395     /* VA API formats */
396     [PIX_FMT_VAAPI_MOCO] = {
397     },
398     [PIX_FMT_VAAPI_IDCT] = {
399     },
400     [PIX_FMT_VAAPI_VLD] = {
401     },
402 };
403
404 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
405 {
406     *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
407     *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
408 }
409
410 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
411 {
412     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
413         return NULL;
414     else
415         return av_pix_fmt_descriptors[pix_fmt].name;
416 }
417
418 static enum PixelFormat avcodec_get_pix_fmt_internal(const char *name)
419 {
420     int i;
421
422     for (i=0; i < PIX_FMT_NB; i++)
423         if (av_pix_fmt_descriptors[i].name && !strcmp(av_pix_fmt_descriptors[i].name, name))
424             return i;
425     return PIX_FMT_NONE;
426 }
427
428 #if HAVE_BIGENDIAN
429 #   define X_NE(be, le) be
430 #else
431 #   define X_NE(be, le) le
432 #endif
433
434 enum PixelFormat avcodec_get_pix_fmt(const char *name)
435 {
436     enum PixelFormat pix_fmt;
437
438     if (!strcmp(name, "rgb32"))
439         name = X_NE("argb", "bgra");
440     else if (!strcmp(name, "bgr32"))
441         name = X_NE("abgr", "rgba");
442
443     pix_fmt = avcodec_get_pix_fmt_internal(name);
444     if (pix_fmt == PIX_FMT_NONE) {
445         char name2[32];
446         snprintf(name2, sizeof(name2), "%s%s", name, X_NE("be", "le"));
447         pix_fmt = avcodec_get_pix_fmt_internal(name2);
448     }
449     return pix_fmt;
450 }
451
452 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
453 {
454     /* print header */
455     if (pix_fmt < 0)
456         snprintf (buf, buf_size,
457                   "name      " " nb_channels" " depth" " is_alpha"
458             );
459     else{
460         PixFmtInfo info= pix_fmt_info[pix_fmt];
461
462         char is_alpha_char= info.is_alpha ? 'y' : 'n';
463
464         snprintf (buf, buf_size,
465                   "%-11s %5d %9d %6c",
466                   av_pix_fmt_descriptors[pix_fmt].name,
467                   info.nb_channels,
468                   info.depth,
469                   is_alpha_char
470             );
471     }
472 }
473
474 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
475 {
476     return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
477 }
478
479 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
480     int i;
481
482     for(i=0; i<256; i++){
483         int r,g,b;
484
485         switch(pix_fmt) {
486         case PIX_FMT_RGB8:
487             r= (i>>5    )*36;
488             g= ((i>>2)&7)*36;
489             b= (i&3     )*85;
490             break;
491         case PIX_FMT_BGR8:
492             b= (i>>6    )*85;
493             g= ((i>>3)&7)*36;
494             r= (i&7     )*36;
495             break;
496         case PIX_FMT_RGB4_BYTE:
497             r= (i>>3    )*255;
498             g= ((i>>1)&3)*85;
499             b= (i&1     )*255;
500             break;
501         case PIX_FMT_BGR4_BYTE:
502             b= (i>>3    )*255;
503             g= ((i>>1)&3)*85;
504             r= (i&1     )*255;
505             break;
506         case PIX_FMT_GRAY8:
507             r=b=g= i;
508             break;
509         default:
510             return -1;
511         }
512         pal[i] =  b + (g<<8) + (r<<16);
513     }
514
515     return 0;
516 }
517
518 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
519 {
520     int w2;
521     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
522
523     memset(picture->linesize, 0, sizeof(picture->linesize));
524
525     switch(pix_fmt) {
526     case PIX_FMT_YUV420P:
527     case PIX_FMT_YUV422P:
528     case PIX_FMT_YUV444P:
529     case PIX_FMT_YUV410P:
530     case PIX_FMT_YUV411P:
531     case PIX_FMT_YUV440P:
532     case PIX_FMT_YUVJ420P:
533     case PIX_FMT_YUVJ422P:
534     case PIX_FMT_YUVJ444P:
535     case PIX_FMT_YUVJ440P:
536         w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
537         picture->linesize[0] = width;
538         picture->linesize[1] = w2;
539         picture->linesize[2] = w2;
540         break;
541     case PIX_FMT_YUV420P16LE:
542     case PIX_FMT_YUV422P16LE:
543     case PIX_FMT_YUV444P16LE:
544     case PIX_FMT_YUV420P16BE:
545     case PIX_FMT_YUV422P16BE:
546     case PIX_FMT_YUV444P16BE:
547         w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
548         picture->linesize[0] = 2*width;
549         picture->linesize[1] = 2*w2;
550         picture->linesize[2] = 2*w2;
551         break;
552     case PIX_FMT_YUVA420P:
553         w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
554         picture->linesize[0] = width;
555         picture->linesize[1] = w2;
556         picture->linesize[2] = w2;
557         picture->linesize[3] = width;
558         break;
559     case PIX_FMT_NV12:
560     case PIX_FMT_NV21:
561         w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
562         picture->linesize[0] = width;
563         picture->linesize[1] = 2 * w2;
564         break;
565     case PIX_FMT_RGB24:
566     case PIX_FMT_BGR24:
567         picture->linesize[0] = width * 3;
568         break;
569     case PIX_FMT_ARGB:
570     case PIX_FMT_ABGR:
571     case PIX_FMT_RGBA:
572     case PIX_FMT_BGRA:
573         picture->linesize[0] = width * 4;
574         break;
575     case PIX_FMT_RGB48BE:
576     case PIX_FMT_RGB48LE:
577         picture->linesize[0] = width * 6;
578         break;
579     case PIX_FMT_GRAY16BE:
580     case PIX_FMT_GRAY16LE:
581     case PIX_FMT_BGR555BE:
582     case PIX_FMT_BGR555LE:
583     case PIX_FMT_BGR565BE:
584     case PIX_FMT_BGR565LE:
585     case PIX_FMT_RGB555BE:
586     case PIX_FMT_RGB555LE:
587     case PIX_FMT_RGB565BE:
588     case PIX_FMT_RGB565LE:
589     case PIX_FMT_YUYV422:
590         picture->linesize[0] = width * 2;
591         break;
592     case PIX_FMT_UYVY422:
593         picture->linesize[0] = width * 2;
594         break;
595     case PIX_FMT_UYYVYY411:
596         picture->linesize[0] = width + width/2;
597         break;
598     case PIX_FMT_RGB4:
599     case PIX_FMT_BGR4:
600         picture->linesize[0] = width / 2;
601         break;
602     case PIX_FMT_MONOWHITE:
603     case PIX_FMT_MONOBLACK:
604         picture->linesize[0] = (width + 7) >> 3;
605         break;
606     case PIX_FMT_PAL8:
607     case PIX_FMT_RGB8:
608     case PIX_FMT_BGR8:
609     case PIX_FMT_RGB4_BYTE:
610     case PIX_FMT_BGR4_BYTE:
611     case PIX_FMT_GRAY8:
612         picture->linesize[0] = width;
613         break;
614     default:
615         return -1;
616     }
617     return 0;
618 }
619
620 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
621                     int height)
622 {
623     int size, h2, size2;
624     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
625
626     size = picture->linesize[0] * height;
627     switch(pix_fmt) {
628     case PIX_FMT_YUV420P:
629     case PIX_FMT_YUV422P:
630     case PIX_FMT_YUV444P:
631     case PIX_FMT_YUV410P:
632     case PIX_FMT_YUV411P:
633     case PIX_FMT_YUV440P:
634     case PIX_FMT_YUVJ420P:
635     case PIX_FMT_YUVJ422P:
636     case PIX_FMT_YUVJ444P:
637     case PIX_FMT_YUVJ440P:
638     case PIX_FMT_YUV420P16LE:
639     case PIX_FMT_YUV422P16LE:
640     case PIX_FMT_YUV444P16LE:
641     case PIX_FMT_YUV420P16BE:
642     case PIX_FMT_YUV422P16BE:
643     case PIX_FMT_YUV444P16BE:
644         h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
645         size2 = picture->linesize[1] * h2;
646         picture->data[0] = ptr;
647         picture->data[1] = picture->data[0] + size;
648         picture->data[2] = picture->data[1] + size2;
649         picture->data[3] = NULL;
650         return size + 2 * size2;
651     case PIX_FMT_YUVA420P:
652         h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
653         size2 = picture->linesize[1] * h2;
654         picture->data[0] = ptr;
655         picture->data[1] = picture->data[0] + size;
656         picture->data[2] = picture->data[1] + size2;
657         picture->data[3] = picture->data[1] + size2 + size2;
658         return 2 * size + 2 * size2;
659     case PIX_FMT_NV12:
660     case PIX_FMT_NV21:
661         h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
662         size2 = picture->linesize[1] * h2;
663         picture->data[0] = ptr;
664         picture->data[1] = picture->data[0] + size;
665         picture->data[2] = NULL;
666         picture->data[3] = NULL;
667         return size + size2;
668     case PIX_FMT_RGB24:
669     case PIX_FMT_BGR24:
670     case PIX_FMT_ARGB:
671     case PIX_FMT_ABGR:
672     case PIX_FMT_RGBA:
673     case PIX_FMT_BGRA:
674     case PIX_FMT_RGB48BE:
675     case PIX_FMT_RGB48LE:
676     case PIX_FMT_GRAY16BE:
677     case PIX_FMT_GRAY16LE:
678     case PIX_FMT_BGR555BE:
679     case PIX_FMT_BGR555LE:
680     case PIX_FMT_BGR565BE:
681     case PIX_FMT_BGR565LE:
682     case PIX_FMT_RGB555BE:
683     case PIX_FMT_RGB555LE:
684     case PIX_FMT_RGB565BE:
685     case PIX_FMT_RGB565LE:
686     case PIX_FMT_YUYV422:
687     case PIX_FMT_UYVY422:
688     case PIX_FMT_UYYVYY411:
689     case PIX_FMT_RGB4:
690     case PIX_FMT_BGR4:
691     case PIX_FMT_MONOWHITE:
692     case PIX_FMT_MONOBLACK:
693         picture->data[0] = ptr;
694         picture->data[1] = NULL;
695         picture->data[2] = NULL;
696         picture->data[3] = NULL;
697         return size;
698     case PIX_FMT_PAL8:
699     case PIX_FMT_RGB8:
700     case PIX_FMT_BGR8:
701     case PIX_FMT_RGB4_BYTE:
702     case PIX_FMT_BGR4_BYTE:
703     case PIX_FMT_GRAY8:
704         size2 = (size + 3) & ~3;
705         picture->data[0] = ptr;
706         picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
707         picture->data[2] = NULL;
708         picture->data[3] = NULL;
709         return size2 + 256 * 4;
710     default:
711         picture->data[0] = NULL;
712         picture->data[1] = NULL;
713         picture->data[2] = NULL;
714         picture->data[3] = NULL;
715         return -1;
716     }
717 }
718
719 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
720                    enum PixelFormat pix_fmt, int width, int height)
721 {
722
723     if(avcodec_check_dimensions(NULL, width, height))
724         return -1;
725
726     if (ff_fill_linesize(picture, pix_fmt, width))
727         return -1;
728
729     return ff_fill_pointer(picture, ptr, pix_fmt, height);
730 }
731
732 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
733                      unsigned char *dest, int dest_size)
734 {
735     const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
736     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
737     int i, j, w, ow, h, oh, data_planes;
738     const unsigned char* s;
739     int size = avpicture_get_size(pix_fmt, width, height);
740
741     if (size > dest_size || size < 0)
742         return -1;
743
744     if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
745         if (pix_fmt == PIX_FMT_YUYV422 ||
746             pix_fmt == PIX_FMT_UYVY422 ||
747             pix_fmt == PIX_FMT_BGR565BE ||
748             pix_fmt == PIX_FMT_BGR565LE ||
749             pix_fmt == PIX_FMT_BGR555BE ||
750             pix_fmt == PIX_FMT_BGR555LE ||
751             pix_fmt == PIX_FMT_RGB565BE ||
752             pix_fmt == PIX_FMT_RGB565LE ||
753             pix_fmt == PIX_FMT_RGB555BE ||
754             pix_fmt == PIX_FMT_RGB555LE)
755             w = width * 2;
756         else if (pix_fmt == PIX_FMT_UYYVYY411)
757             w = width + width/2;
758         else if (pix_fmt == PIX_FMT_PAL8)
759             w = width;
760         else
761             w = width * (pf->depth * pf->nb_channels / 8);
762
763         data_planes = 1;
764         h = height;
765     } else {
766         data_planes = pf->nb_channels;
767         w = (width*pf->depth + 7)/8;
768         h = height;
769     }
770
771     ow = w;
772     oh = h;
773
774     for (i=0; i<data_planes; i++) {
775         if (i == 1) {
776             w = ((width >> desc->log2_chroma_w) * pf->depth + 7) / 8;
777             h = height >> desc->log2_chroma_h;
778             if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
779                 w <<= 1;
780         } else if (i == 3) {
781             w = ow;
782             h = oh;
783         }
784         s = src->data[i];
785         for(j=0; j<h; j++) {
786             memcpy(dest, s, w);
787             dest += w;
788             s += src->linesize[i];
789         }
790     }
791
792     if (pf->pixel_type == FF_PIXEL_PALETTE)
793         memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
794
795     return size;
796 }
797
798 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
799 {
800     AVPicture dummy_pict;
801     if(avcodec_check_dimensions(NULL, width, height))
802         return -1;
803     switch (pix_fmt) {
804     case PIX_FMT_RGB8:
805     case PIX_FMT_BGR8:
806     case PIX_FMT_RGB4_BYTE:
807     case PIX_FMT_BGR4_BYTE:
808     case PIX_FMT_GRAY8:
809         // do not include palette for these pseudo-paletted formats
810         return width * height;
811     }
812     return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
813 }
814
815 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
816                              int has_alpha)
817 {
818     const PixFmtInfo *pf, *ps;
819     const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
820     const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
821     int loss;
822
823     ps = &pix_fmt_info[src_pix_fmt];
824
825     /* compute loss */
826     loss = 0;
827     pf = &pix_fmt_info[dst_pix_fmt];
828     if (pf->depth < ps->depth ||
829         ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE) &&
830          (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE)))
831         loss |= FF_LOSS_DEPTH;
832     if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
833         dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
834         loss |= FF_LOSS_RESOLUTION;
835     switch(pf->color_type) {
836     case FF_COLOR_RGB:
837         if (ps->color_type != FF_COLOR_RGB &&
838             ps->color_type != FF_COLOR_GRAY)
839             loss |= FF_LOSS_COLORSPACE;
840         break;
841     case FF_COLOR_GRAY:
842         if (ps->color_type != FF_COLOR_GRAY)
843             loss |= FF_LOSS_COLORSPACE;
844         break;
845     case FF_COLOR_YUV:
846         if (ps->color_type != FF_COLOR_YUV)
847             loss |= FF_LOSS_COLORSPACE;
848         break;
849     case FF_COLOR_YUV_JPEG:
850         if (ps->color_type != FF_COLOR_YUV_JPEG &&
851             ps->color_type != FF_COLOR_YUV &&
852             ps->color_type != FF_COLOR_GRAY)
853             loss |= FF_LOSS_COLORSPACE;
854         break;
855     default:
856         /* fail safe test */
857         if (ps->color_type != pf->color_type)
858             loss |= FF_LOSS_COLORSPACE;
859         break;
860     }
861     if (pf->color_type == FF_COLOR_GRAY &&
862         ps->color_type != FF_COLOR_GRAY)
863         loss |= FF_LOSS_CHROMA;
864     if (!pf->is_alpha && (ps->is_alpha && has_alpha))
865         loss |= FF_LOSS_ALPHA;
866     if (pf->pixel_type == FF_PIXEL_PALETTE &&
867         (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
868         loss |= FF_LOSS_COLORQUANT;
869     return loss;
870 }
871
872 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
873 {
874     int bits;
875     const PixFmtInfo *pf;
876     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
877
878     pf = &pix_fmt_info[pix_fmt];
879     switch(pf->pixel_type) {
880     case FF_PIXEL_PACKED:
881         switch(pix_fmt) {
882         case PIX_FMT_YUYV422:
883         case PIX_FMT_UYVY422:
884         case PIX_FMT_RGB565BE:
885         case PIX_FMT_RGB565LE:
886         case PIX_FMT_RGB555BE:
887         case PIX_FMT_RGB555LE:
888         case PIX_FMT_BGR565BE:
889         case PIX_FMT_BGR565LE:
890         case PIX_FMT_BGR555BE:
891         case PIX_FMT_BGR555LE:
892             bits = 16;
893             break;
894         case PIX_FMT_UYYVYY411:
895             bits = 12;
896             break;
897         default:
898             bits = pf->depth * pf->nb_channels;
899             break;
900         }
901         break;
902     case FF_PIXEL_PLANAR:
903         if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
904             bits = pf->depth * pf->nb_channels;
905         } else {
906             bits = pf->depth + ((2 * pf->depth) >>
907                                 (desc->log2_chroma_w + desc->log2_chroma_h));
908         }
909         break;
910     case FF_PIXEL_PALETTE:
911         bits = 8;
912         break;
913     default:
914         bits = -1;
915         break;
916     }
917     return bits;
918 }
919
920 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
921                                       enum PixelFormat src_pix_fmt,
922                                       int has_alpha,
923                                       int loss_mask)
924 {
925     int dist, i, loss, min_dist;
926     enum PixelFormat dst_pix_fmt;
927
928     /* find exact color match with smallest size */
929     dst_pix_fmt = -1;
930     min_dist = 0x7fffffff;
931     for(i = 0;i < PIX_FMT_NB; i++) {
932         if (pix_fmt_mask & (1ULL << i)) {
933             loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
934             if (loss == 0) {
935                 dist = avg_bits_per_pixel(i);
936                 if (dist < min_dist) {
937                     min_dist = dist;
938                     dst_pix_fmt = i;
939                 }
940             }
941         }
942     }
943     return dst_pix_fmt;
944 }
945
946 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
947                               int has_alpha, int *loss_ptr)
948 {
949     enum PixelFormat dst_pix_fmt;
950     int loss_mask, i;
951     static const int loss_mask_order[] = {
952         ~0, /* no loss first */
953         ~FF_LOSS_ALPHA,
954         ~FF_LOSS_RESOLUTION,
955         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
956         ~FF_LOSS_COLORQUANT,
957         ~FF_LOSS_DEPTH,
958         0,
959     };
960
961     /* try with successive loss */
962     i = 0;
963     for(;;) {
964         loss_mask = loss_mask_order[i++];
965         dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
966                                                  has_alpha, loss_mask);
967         if (dst_pix_fmt >= 0)
968             goto found;
969         if (loss_mask == 0)
970             break;
971     }
972     return -1;
973  found:
974     if (loss_ptr)
975         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
976     return dst_pix_fmt;
977 }
978
979 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
980                            const uint8_t *src, int src_wrap,
981                            int width, int height)
982 {
983     if((!dst) || (!src))
984         return;
985     for(;height > 0; height--) {
986         memcpy(dst, src, width);
987         dst += dst_wrap;
988         src += src_wrap;
989     }
990 }
991
992 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
993 {
994     int bits;
995     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
996     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
997
998     pf = &pix_fmt_info[pix_fmt];
999     switch(pf->pixel_type) {
1000     case FF_PIXEL_PACKED:
1001         switch(pix_fmt) {
1002         case PIX_FMT_YUYV422:
1003         case PIX_FMT_UYVY422:
1004         case PIX_FMT_RGB565BE:
1005         case PIX_FMT_RGB565LE:
1006         case PIX_FMT_RGB555BE:
1007         case PIX_FMT_RGB555LE:
1008         case PIX_FMT_BGR565BE:
1009         case PIX_FMT_BGR565LE:
1010         case PIX_FMT_BGR555BE:
1011         case PIX_FMT_BGR555LE:
1012             bits = 16;
1013             break;
1014         case PIX_FMT_UYYVYY411:
1015             bits = 12;
1016             break;
1017         default:
1018             bits = pf->depth * pf->nb_channels;
1019             break;
1020         }
1021         return (width * bits + 7) >> 3;
1022         break;
1023     case FF_PIXEL_PLANAR:
1024             if (plane == 1 || plane == 2)
1025                 width= -((-width)>>desc->log2_chroma_w);
1026
1027             return (width * pf->depth + 7) >> 3;
1028         break;
1029     case FF_PIXEL_PALETTE:
1030         if (plane == 0)
1031             return width;
1032         break;
1033     }
1034
1035     return -1;
1036 }
1037
1038 void av_picture_copy(AVPicture *dst, const AVPicture *src,
1039                      enum PixelFormat pix_fmt, int width, int height)
1040 {
1041     int i;
1042     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1043     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1044
1045     switch(pf->pixel_type) {
1046     case FF_PIXEL_PACKED:
1047     case FF_PIXEL_PLANAR:
1048         for(i = 0; i < pf->nb_channels; i++) {
1049             int h;
1050             int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
1051             h = height;
1052             if (i == 1 || i == 2) {
1053                 h= -((-height)>>desc->log2_chroma_h);
1054             }
1055             ff_img_copy_plane(dst->data[i], dst->linesize[i],
1056                            src->data[i], src->linesize[i],
1057                            bwidth, h);
1058         }
1059         break;
1060     case FF_PIXEL_PALETTE:
1061         ff_img_copy_plane(dst->data[0], dst->linesize[0],
1062                        src->data[0], src->linesize[0],
1063                        width, height);
1064         /* copy the palette */
1065         memcpy(dst->data[1], src->data[1], 4*256);
1066         break;
1067     }
1068 }
1069
1070 /* 2x2 -> 1x1 */
1071 void ff_shrink22(uint8_t *dst, int dst_wrap,
1072                      const uint8_t *src, int src_wrap,
1073                      int width, int height)
1074 {
1075     int w;
1076     const uint8_t *s1, *s2;
1077     uint8_t *d;
1078
1079     for(;height > 0; height--) {
1080         s1 = src;
1081         s2 = s1 + src_wrap;
1082         d = dst;
1083         for(w = width;w >= 4; w-=4) {
1084             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1085             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1086             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1087             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1088             s1 += 8;
1089             s2 += 8;
1090             d += 4;
1091         }
1092         for(;w > 0; w--) {
1093             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1094             s1 += 2;
1095             s2 += 2;
1096             d++;
1097         }
1098         src += 2 * src_wrap;
1099         dst += dst_wrap;
1100     }
1101 }
1102
1103 /* 4x4 -> 1x1 */
1104 void ff_shrink44(uint8_t *dst, int dst_wrap,
1105                      const uint8_t *src, int src_wrap,
1106                      int width, int height)
1107 {
1108     int w;
1109     const uint8_t *s1, *s2, *s3, *s4;
1110     uint8_t *d;
1111
1112     for(;height > 0; height--) {
1113         s1 = src;
1114         s2 = s1 + src_wrap;
1115         s3 = s2 + src_wrap;
1116         s4 = s3 + src_wrap;
1117         d = dst;
1118         for(w = width;w > 0; w--) {
1119             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1120                     s2[0] + s2[1] + s2[2] + s2[3] +
1121                     s3[0] + s3[1] + s3[2] + s3[3] +
1122                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1123             s1 += 4;
1124             s2 += 4;
1125             s3 += 4;
1126             s4 += 4;
1127             d++;
1128         }
1129         src += 4 * src_wrap;
1130         dst += dst_wrap;
1131     }
1132 }
1133
1134 /* 8x8 -> 1x1 */
1135 void ff_shrink88(uint8_t *dst, int dst_wrap,
1136                      const uint8_t *src, int src_wrap,
1137                      int width, int height)
1138 {
1139     int w, i;
1140
1141     for(;height > 0; height--) {
1142         for(w = width;w > 0; w--) {
1143             int tmp=0;
1144             for(i=0; i<8; i++){
1145                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1146                 src += src_wrap;
1147             }
1148             *(dst++) = (tmp + 32)>>6;
1149             src += 8 - 8*src_wrap;
1150         }
1151         src += 8*src_wrap - 8*width;
1152         dst += dst_wrap - width;
1153     }
1154 }
1155
1156
1157 int avpicture_alloc(AVPicture *picture,
1158                     enum PixelFormat pix_fmt, int width, int height)
1159 {
1160     int size;
1161     void *ptr;
1162
1163     size = avpicture_fill(picture, NULL, pix_fmt, width, height);
1164     if(size<0)
1165         goto fail;
1166     ptr = av_malloc(size);
1167     if (!ptr)
1168         goto fail;
1169     avpicture_fill(picture, ptr, pix_fmt, width, height);
1170     if(picture->data[1] && !picture->data[2])
1171         ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
1172
1173     return 0;
1174  fail:
1175     memset(picture, 0, sizeof(AVPicture));
1176     return -1;
1177 }
1178
1179 void avpicture_free(AVPicture *picture)
1180 {
1181     av_free(picture->data[0]);
1182 }
1183
1184 /* return true if yuv planar */
1185 static inline int is_yuv_planar(const PixFmtInfo *ps)
1186 {
1187     return (ps->color_type == FF_COLOR_YUV ||
1188             ps->color_type == FF_COLOR_YUV_JPEG) &&
1189         ps->pixel_type == FF_PIXEL_PLANAR;
1190 }
1191
1192 int av_picture_crop(AVPicture *dst, const AVPicture *src,
1193                     enum PixelFormat pix_fmt, int top_band, int left_band)
1194 {
1195     int y_shift;
1196     int x_shift;
1197
1198     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1199         return -1;
1200
1201     y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
1202     x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
1203
1204     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1205     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1206     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1207
1208     dst->linesize[0] = src->linesize[0];
1209     dst->linesize[1] = src->linesize[1];
1210     dst->linesize[2] = src->linesize[2];
1211     return 0;
1212 }
1213
1214 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
1215                    enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
1216             int *color)
1217 {
1218     uint8_t *optr;
1219     int y_shift;
1220     int x_shift;
1221     int yheight;
1222     int i, y;
1223
1224     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1225         !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
1226
1227     for (i = 0; i < 3; i++) {
1228         x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
1229         y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
1230
1231         if (padtop || padleft) {
1232             memset(dst->data[i], color[i],
1233                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
1234         }
1235
1236         if (padleft || padright) {
1237             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1238                 (dst->linesize[i] - (padright >> x_shift));
1239             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1240             for (y = 0; y < yheight; y++) {
1241                 memset(optr, color[i], (padleft + padright) >> x_shift);
1242                 optr += dst->linesize[i];
1243             }
1244         }
1245
1246         if (src) { /* first line */
1247             uint8_t *iptr = src->data[i];
1248             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1249                     (padleft >> x_shift);
1250             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
1251             iptr += src->linesize[i];
1252             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1253                 (dst->linesize[i] - (padright >> x_shift));
1254             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1255             for (y = 0; y < yheight; y++) {
1256                 memset(optr, color[i], (padleft + padright) >> x_shift);
1257                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1258                        (width - padleft - padright) >> x_shift);
1259                 iptr += src->linesize[i];
1260                 optr += dst->linesize[i];
1261             }
1262         }
1263
1264         if (padbottom || padright) {
1265             optr = dst->data[i] + dst->linesize[i] *
1266                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1267             memset(optr, color[i],dst->linesize[i] *
1268                 (padbottom >> y_shift) + (padright >> x_shift));
1269         }
1270     }
1271     return 0;
1272 }
1273
1274 /* NOTE: we scan all the pixels to have an exact information */
1275 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1276 {
1277     const unsigned char *p;
1278     int src_wrap, ret, x, y;
1279     unsigned int a;
1280     uint32_t *palette = (uint32_t *)src->data[1];
1281
1282     p = src->data[0];
1283     src_wrap = src->linesize[0] - width;
1284     ret = 0;
1285     for(y=0;y<height;y++) {
1286         for(x=0;x<width;x++) {
1287             a = palette[p[0]] >> 24;
1288             if (a == 0x00) {
1289                 ret |= FF_ALPHA_TRANSP;
1290             } else if (a != 0xff) {
1291                 ret |= FF_ALPHA_SEMI_TRANSP;
1292             }
1293             p++;
1294         }
1295         p += src_wrap;
1296     }
1297     return ret;
1298 }
1299
1300 int img_get_alpha_info(const AVPicture *src,
1301                        enum PixelFormat pix_fmt, int width, int height)
1302 {
1303     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1304     int ret;
1305
1306     /* no alpha can be represented in format */
1307     if (!pf->is_alpha)
1308         return 0;
1309     switch(pix_fmt) {
1310     case PIX_FMT_PAL8:
1311         ret = get_alpha_info_pal8(src, width, height);
1312         break;
1313     default:
1314         /* we do not know, so everything is indicated */
1315         ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1316         break;
1317     }
1318     return ret;
1319 }
1320
1321 #if HAVE_MMX
1322 #define DEINT_INPLACE_LINE_LUM \
1323                     movd_m2r(lum_m4[0],mm0);\
1324                     movd_m2r(lum_m3[0],mm1);\
1325                     movd_m2r(lum_m2[0],mm2);\
1326                     movd_m2r(lum_m1[0],mm3);\
1327                     movd_m2r(lum[0],mm4);\
1328                     punpcklbw_r2r(mm7,mm0);\
1329                     movd_r2m(mm2,lum_m4[0]);\
1330                     punpcklbw_r2r(mm7,mm1);\
1331                     punpcklbw_r2r(mm7,mm2);\
1332                     punpcklbw_r2r(mm7,mm3);\
1333                     punpcklbw_r2r(mm7,mm4);\
1334                     paddw_r2r(mm3,mm1);\
1335                     psllw_i2r(1,mm2);\
1336                     paddw_r2r(mm4,mm0);\
1337                     psllw_i2r(2,mm1);\
1338                     paddw_r2r(mm6,mm2);\
1339                     paddw_r2r(mm2,mm1);\
1340                     psubusw_r2r(mm0,mm1);\
1341                     psrlw_i2r(3,mm1);\
1342                     packuswb_r2r(mm7,mm1);\
1343                     movd_r2m(mm1,lum_m2[0]);
1344
1345 #define DEINT_LINE_LUM \
1346                     movd_m2r(lum_m4[0],mm0);\
1347                     movd_m2r(lum_m3[0],mm1);\
1348                     movd_m2r(lum_m2[0],mm2);\
1349                     movd_m2r(lum_m1[0],mm3);\
1350                     movd_m2r(lum[0],mm4);\
1351                     punpcklbw_r2r(mm7,mm0);\
1352                     punpcklbw_r2r(mm7,mm1);\
1353                     punpcklbw_r2r(mm7,mm2);\
1354                     punpcklbw_r2r(mm7,mm3);\
1355                     punpcklbw_r2r(mm7,mm4);\
1356                     paddw_r2r(mm3,mm1);\
1357                     psllw_i2r(1,mm2);\
1358                     paddw_r2r(mm4,mm0);\
1359                     psllw_i2r(2,mm1);\
1360                     paddw_r2r(mm6,mm2);\
1361                     paddw_r2r(mm2,mm1);\
1362                     psubusw_r2r(mm0,mm1);\
1363                     psrlw_i2r(3,mm1);\
1364                     packuswb_r2r(mm7,mm1);\
1365                     movd_r2m(mm1,dst[0]);
1366 #endif
1367
1368 /* filter parameters: [-1 4 2 4 -1] // 8 */
1369 static void deinterlace_line(uint8_t *dst,
1370                              const uint8_t *lum_m4, const uint8_t *lum_m3,
1371                              const uint8_t *lum_m2, const uint8_t *lum_m1,
1372                              const uint8_t *lum,
1373                              int size)
1374 {
1375 #if !HAVE_MMX
1376     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1377     int sum;
1378
1379     for(;size > 0;size--) {
1380         sum = -lum_m4[0];
1381         sum += lum_m3[0] << 2;
1382         sum += lum_m2[0] << 1;
1383         sum += lum_m1[0] << 2;
1384         sum += -lum[0];
1385         dst[0] = cm[(sum + 4) >> 3];
1386         lum_m4++;
1387         lum_m3++;
1388         lum_m2++;
1389         lum_m1++;
1390         lum++;
1391         dst++;
1392     }
1393 #else
1394
1395     {
1396         pxor_r2r(mm7,mm7);
1397         movq_m2r(ff_pw_4,mm6);
1398     }
1399     for (;size > 3; size-=4) {
1400         DEINT_LINE_LUM
1401         lum_m4+=4;
1402         lum_m3+=4;
1403         lum_m2+=4;
1404         lum_m1+=4;
1405         lum+=4;
1406         dst+=4;
1407     }
1408 #endif
1409 }
1410 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1411                              int size)
1412 {
1413 #if !HAVE_MMX
1414     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1415     int sum;
1416
1417     for(;size > 0;size--) {
1418         sum = -lum_m4[0];
1419         sum += lum_m3[0] << 2;
1420         sum += lum_m2[0] << 1;
1421         lum_m4[0]=lum_m2[0];
1422         sum += lum_m1[0] << 2;
1423         sum += -lum[0];
1424         lum_m2[0] = cm[(sum + 4) >> 3];
1425         lum_m4++;
1426         lum_m3++;
1427         lum_m2++;
1428         lum_m1++;
1429         lum++;
1430     }
1431 #else
1432
1433     {
1434         pxor_r2r(mm7,mm7);
1435         movq_m2r(ff_pw_4,mm6);
1436     }
1437     for (;size > 3; size-=4) {
1438         DEINT_INPLACE_LINE_LUM
1439         lum_m4+=4;
1440         lum_m3+=4;
1441         lum_m2+=4;
1442         lum_m1+=4;
1443         lum+=4;
1444     }
1445 #endif
1446 }
1447
1448 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1449    top field is copied as is, but the bottom field is deinterlaced
1450    against the top field. */
1451 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1452                                     const uint8_t *src1, int src_wrap,
1453                                     int width, int height)
1454 {
1455     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1456     int y;
1457
1458     src_m2 = src1;
1459     src_m1 = src1;
1460     src_0=&src_m1[src_wrap];
1461     src_p1=&src_0[src_wrap];
1462     src_p2=&src_p1[src_wrap];
1463     for(y=0;y<(height-2);y+=2) {
1464         memcpy(dst,src_m1,width);
1465         dst += dst_wrap;
1466         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1467         src_m2 = src_0;
1468         src_m1 = src_p1;
1469         src_0 = src_p2;
1470         src_p1 += 2*src_wrap;
1471         src_p2 += 2*src_wrap;
1472         dst += dst_wrap;
1473     }
1474     memcpy(dst,src_m1,width);
1475     dst += dst_wrap;
1476     /* do last line */
1477     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1478 }
1479
1480 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1481                                              int width, int height)
1482 {
1483     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1484     int y;
1485     uint8_t *buf;
1486     buf = (uint8_t*)av_malloc(width);
1487
1488     src_m1 = src1;
1489     memcpy(buf,src_m1,width);
1490     src_0=&src_m1[src_wrap];
1491     src_p1=&src_0[src_wrap];
1492     src_p2=&src_p1[src_wrap];
1493     for(y=0;y<(height-2);y+=2) {
1494         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1495         src_m1 = src_p1;
1496         src_0 = src_p2;
1497         src_p1 += 2*src_wrap;
1498         src_p2 += 2*src_wrap;
1499     }
1500     /* do last line */
1501     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1502     av_free(buf);
1503 }
1504
1505 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1506                           enum PixelFormat pix_fmt, int width, int height)
1507 {
1508     int i;
1509
1510     if (pix_fmt != PIX_FMT_YUV420P &&
1511         pix_fmt != PIX_FMT_YUV422P &&
1512         pix_fmt != PIX_FMT_YUV444P &&
1513         pix_fmt != PIX_FMT_YUV411P &&
1514         pix_fmt != PIX_FMT_GRAY8)
1515         return -1;
1516     if ((width & 3) != 0 || (height & 3) != 0)
1517         return -1;
1518
1519     for(i=0;i<3;i++) {
1520         if (i == 1) {
1521             switch(pix_fmt) {
1522             case PIX_FMT_YUV420P:
1523                 width >>= 1;
1524                 height >>= 1;
1525                 break;
1526             case PIX_FMT_YUV422P:
1527                 width >>= 1;
1528                 break;
1529             case PIX_FMT_YUV411P:
1530                 width >>= 2;
1531                 break;
1532             default:
1533                 break;
1534             }
1535             if (pix_fmt == PIX_FMT_GRAY8) {
1536                 break;
1537             }
1538         }
1539         if (src == dst) {
1540             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1541                                  width, height);
1542         } else {
1543             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1544                                         src->data[i], src->linesize[i],
1545                                         width, height);
1546         }
1547     }
1548     emms_c();
1549     return 0;
1550 }
1551