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