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