OSDN Git Service

document introduction of side data in APIchanges
[coroid/ffmpeg_saccubus.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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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
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 "internal.h"
36 #include "imgconvert.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/imgutils.h"
40
41 #if HAVE_MMX && HAVE_YASM
42 #include "x86/dsputil_mmx.h"
43 #endif
44
45 #define xglue(x, y) x ## y
46 #define glue(x, y) xglue(x, y)
47
48 #define FF_COLOR_RGB      0 /**< RGB color space */
49 #define FF_COLOR_GRAY     1 /**< gray color space */
50 #define FF_COLOR_YUV      2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
51 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
52
53 #define FF_PIXEL_PLANAR   0 /**< each channel has one component in AVPicture */
54 #define FF_PIXEL_PACKED   1 /**< only one components containing all the channels */
55 #define FF_PIXEL_PALETTE  2  /**< one components containing indexes for a palette */
56
57 #if HAVE_MMX && HAVE_YASM
58 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
59 #define deinterlace_line         ff_deinterlace_line_mmx
60 #else
61 #define deinterlace_line_inplace deinterlace_line_inplace_c
62 #define deinterlace_line         deinterlace_line_c
63 #endif
64
65 typedef struct PixFmtInfo {
66     uint8_t nb_channels;     /**< number of channels (including alpha) */
67     uint8_t color_type;      /**< color type (see FF_COLOR_xxx constants) */
68     uint8_t pixel_type;      /**< pixel storage type (see FF_PIXEL_xxx constants) */
69     uint8_t is_alpha : 1;    /**< true if alpha can be specified */
70     uint8_t depth;           /**< bit depth of the color components */
71 } PixFmtInfo;
72
73 /* this table gives more information about formats */
74 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
75     /* YUV formats */
76     [PIX_FMT_YUV420P] = {
77         .nb_channels = 3,
78         .color_type = FF_COLOR_YUV,
79         .pixel_type = FF_PIXEL_PLANAR,
80         .depth = 8,
81     },
82     [PIX_FMT_YUV422P] = {
83         .nb_channels = 3,
84         .color_type = FF_COLOR_YUV,
85         .pixel_type = FF_PIXEL_PLANAR,
86         .depth = 8,
87     },
88     [PIX_FMT_YUV444P] = {
89         .nb_channels = 3,
90         .color_type = FF_COLOR_YUV,
91         .pixel_type = FF_PIXEL_PLANAR,
92         .depth = 8,
93     },
94     [PIX_FMT_YUYV422] = {
95         .nb_channels = 1,
96         .color_type = FF_COLOR_YUV,
97         .pixel_type = FF_PIXEL_PACKED,
98         .depth = 8,
99     },
100     [PIX_FMT_UYVY422] = {
101         .nb_channels = 1,
102         .color_type = FF_COLOR_YUV,
103         .pixel_type = FF_PIXEL_PACKED,
104         .depth = 8,
105     },
106     [PIX_FMT_YUV410P] = {
107         .nb_channels = 3,
108         .color_type = FF_COLOR_YUV,
109         .pixel_type = FF_PIXEL_PLANAR,
110         .depth = 8,
111     },
112     [PIX_FMT_YUV411P] = {
113         .nb_channels = 3,
114         .color_type = FF_COLOR_YUV,
115         .pixel_type = FF_PIXEL_PLANAR,
116         .depth = 8,
117     },
118     [PIX_FMT_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         .nb_channels = 3,
126         .color_type = FF_COLOR_YUV,
127         .pixel_type = FF_PIXEL_PLANAR,
128         .depth = 16,
129     },
130     [PIX_FMT_YUV422P16LE] = {
131         .nb_channels = 3,
132         .color_type = FF_COLOR_YUV,
133         .pixel_type = FF_PIXEL_PLANAR,
134         .depth = 16,
135     },
136     [PIX_FMT_YUV444P16LE] = {
137         .nb_channels = 3,
138         .color_type = FF_COLOR_YUV,
139         .pixel_type = FF_PIXEL_PLANAR,
140         .depth = 16,
141     },
142     [PIX_FMT_YUV420P16BE] = {
143         .nb_channels = 3,
144         .color_type = FF_COLOR_YUV,
145         .pixel_type = FF_PIXEL_PLANAR,
146         .depth = 16,
147     },
148     [PIX_FMT_YUV422P16BE] = {
149         .nb_channels = 3,
150         .color_type = FF_COLOR_YUV,
151         .pixel_type = FF_PIXEL_PLANAR,
152         .depth = 16,
153     },
154     [PIX_FMT_YUV444P16BE] = {
155         .nb_channels = 3,
156         .color_type = FF_COLOR_YUV,
157         .pixel_type = FF_PIXEL_PLANAR,
158         .depth = 16,
159     },
160
161
162     /* YUV formats with alpha plane */
163     [PIX_FMT_YUVA420P] = {
164         .nb_channels = 4,
165         .color_type = FF_COLOR_YUV,
166         .pixel_type = FF_PIXEL_PLANAR,
167         .depth = 8,
168     },
169
170     /* JPEG YUV */
171     [PIX_FMT_YUVJ420P] = {
172         .nb_channels = 3,
173         .color_type = FF_COLOR_YUV_JPEG,
174         .pixel_type = FF_PIXEL_PLANAR,
175         .depth = 8,
176     },
177     [PIX_FMT_YUVJ422P] = {
178         .nb_channels = 3,
179         .color_type = FF_COLOR_YUV_JPEG,
180         .pixel_type = FF_PIXEL_PLANAR,
181         .depth = 8,
182     },
183     [PIX_FMT_YUVJ444P] = {
184         .nb_channels = 3,
185         .color_type = FF_COLOR_YUV_JPEG,
186         .pixel_type = FF_PIXEL_PLANAR,
187         .depth = 8,
188     },
189     [PIX_FMT_YUVJ440P] = {
190         .nb_channels = 3,
191         .color_type = FF_COLOR_YUV_JPEG,
192         .pixel_type = FF_PIXEL_PLANAR,
193         .depth = 8,
194     },
195
196     /* RGB formats */
197     [PIX_FMT_RGB24] = {
198         .nb_channels = 3,
199         .color_type = FF_COLOR_RGB,
200         .pixel_type = FF_PIXEL_PACKED,
201         .depth = 8,
202     },
203     [PIX_FMT_BGR24] = {
204         .nb_channels = 3,
205         .color_type = FF_COLOR_RGB,
206         .pixel_type = FF_PIXEL_PACKED,
207         .depth = 8,
208     },
209     [PIX_FMT_ARGB] = {
210         .nb_channels = 4, .is_alpha = 1,
211         .color_type = FF_COLOR_RGB,
212         .pixel_type = FF_PIXEL_PACKED,
213         .depth = 8,
214     },
215     [PIX_FMT_RGB48BE] = {
216         .nb_channels = 3,
217         .color_type = FF_COLOR_RGB,
218         .pixel_type = FF_PIXEL_PACKED,
219         .depth = 16,
220     },
221     [PIX_FMT_RGB48LE] = {
222         .nb_channels = 3,
223         .color_type = FF_COLOR_RGB,
224         .pixel_type = FF_PIXEL_PACKED,
225         .depth = 16,
226     },
227     [PIX_FMT_RGB565BE] = {
228         .nb_channels = 3,
229         .color_type = FF_COLOR_RGB,
230         .pixel_type = FF_PIXEL_PACKED,
231         .depth = 5,
232     },
233     [PIX_FMT_RGB565LE] = {
234         .nb_channels = 3,
235         .color_type = FF_COLOR_RGB,
236         .pixel_type = FF_PIXEL_PACKED,
237         .depth = 5,
238     },
239     [PIX_FMT_RGB555BE] = {
240         .nb_channels = 3,
241         .color_type = FF_COLOR_RGB,
242         .pixel_type = FF_PIXEL_PACKED,
243         .depth = 5,
244     },
245     [PIX_FMT_RGB555LE] = {
246         .nb_channels = 3,
247         .color_type = FF_COLOR_RGB,
248         .pixel_type = FF_PIXEL_PACKED,
249         .depth = 5,
250     },
251     [PIX_FMT_RGB444BE] = {
252         .nb_channels = 3,
253         .color_type = FF_COLOR_RGB,
254         .pixel_type = FF_PIXEL_PACKED,
255         .depth = 4,
256     },
257     [PIX_FMT_RGB444LE] = {
258         .nb_channels = 3,
259         .color_type = FF_COLOR_RGB,
260         .pixel_type = FF_PIXEL_PACKED,
261         .depth = 4,
262     },
263
264     /* gray / mono formats */
265     [PIX_FMT_GRAY16BE] = {
266         .nb_channels = 1,
267         .color_type = FF_COLOR_GRAY,
268         .pixel_type = FF_PIXEL_PLANAR,
269         .depth = 16,
270     },
271     [PIX_FMT_GRAY16LE] = {
272         .nb_channels = 1,
273         .color_type = FF_COLOR_GRAY,
274         .pixel_type = FF_PIXEL_PLANAR,
275         .depth = 16,
276     },
277     [PIX_FMT_GRAY8] = {
278         .nb_channels = 1,
279         .color_type = FF_COLOR_GRAY,
280         .pixel_type = FF_PIXEL_PLANAR,
281         .depth = 8,
282     },
283     [PIX_FMT_MONOWHITE] = {
284         .nb_channels = 1,
285         .color_type = FF_COLOR_GRAY,
286         .pixel_type = FF_PIXEL_PLANAR,
287         .depth = 1,
288     },
289     [PIX_FMT_MONOBLACK] = {
290         .nb_channels = 1,
291         .color_type = FF_COLOR_GRAY,
292         .pixel_type = FF_PIXEL_PLANAR,
293         .depth = 1,
294     },
295
296     /* paletted formats */
297     [PIX_FMT_PAL8] = {
298         .nb_channels = 4, .is_alpha = 1,
299         .color_type = FF_COLOR_RGB,
300         .pixel_type = FF_PIXEL_PALETTE,
301         .depth = 8,
302     },
303     [PIX_FMT_UYYVYY411] = {
304         .nb_channels = 1,
305         .color_type = FF_COLOR_YUV,
306         .pixel_type = FF_PIXEL_PACKED,
307         .depth = 8,
308     },
309     [PIX_FMT_ABGR] = {
310         .nb_channels = 4, .is_alpha = 1,
311         .color_type = FF_COLOR_RGB,
312         .pixel_type = FF_PIXEL_PACKED,
313         .depth = 8,
314     },
315     [PIX_FMT_BGR565BE] = {
316         .nb_channels = 3,
317         .color_type = FF_COLOR_RGB,
318         .pixel_type = FF_PIXEL_PACKED,
319         .depth = 5,
320     },
321     [PIX_FMT_BGR565LE] = {
322         .nb_channels = 3,
323         .color_type = FF_COLOR_RGB,
324         .pixel_type = FF_PIXEL_PACKED,
325         .depth = 5,
326     },
327     [PIX_FMT_BGR555BE] = {
328         .nb_channels = 3,
329         .color_type = FF_COLOR_RGB,
330         .pixel_type = FF_PIXEL_PACKED,
331         .depth = 5,
332     },
333     [PIX_FMT_BGR555LE] = {
334         .nb_channels = 3,
335         .color_type = FF_COLOR_RGB,
336         .pixel_type = FF_PIXEL_PACKED,
337         .depth = 5,
338     },
339     [PIX_FMT_BGR444BE] = {
340         .nb_channels = 3,
341         .color_type = FF_COLOR_RGB,
342         .pixel_type = FF_PIXEL_PACKED,
343         .depth = 4,
344     },
345     [PIX_FMT_BGR444LE] = {
346         .nb_channels = 3,
347         .color_type = FF_COLOR_RGB,
348         .pixel_type = FF_PIXEL_PACKED,
349         .depth = 4,
350     },
351     [PIX_FMT_RGB8] = {
352         .nb_channels = 1,
353         .color_type = FF_COLOR_RGB,
354         .pixel_type = FF_PIXEL_PACKED,
355         .depth = 8,
356     },
357     [PIX_FMT_RGB4] = {
358         .nb_channels = 1,
359         .color_type = FF_COLOR_RGB,
360         .pixel_type = FF_PIXEL_PACKED,
361         .depth = 4,
362     },
363     [PIX_FMT_RGB4_BYTE] = {
364         .nb_channels = 1,
365         .color_type = FF_COLOR_RGB,
366         .pixel_type = FF_PIXEL_PACKED,
367         .depth = 8,
368     },
369     [PIX_FMT_BGR8] = {
370         .nb_channels = 1,
371         .color_type = FF_COLOR_RGB,
372         .pixel_type = FF_PIXEL_PACKED,
373         .depth = 8,
374     },
375     [PIX_FMT_BGR4] = {
376         .nb_channels = 1,
377         .color_type = FF_COLOR_RGB,
378         .pixel_type = FF_PIXEL_PACKED,
379         .depth = 4,
380     },
381     [PIX_FMT_BGR4_BYTE] = {
382         .nb_channels = 1,
383         .color_type = FF_COLOR_RGB,
384         .pixel_type = FF_PIXEL_PACKED,
385         .depth = 8,
386     },
387     [PIX_FMT_NV12] = {
388         .nb_channels = 2,
389         .color_type = FF_COLOR_YUV,
390         .pixel_type = FF_PIXEL_PLANAR,
391         .depth = 8,
392     },
393     [PIX_FMT_NV21] = {
394         .nb_channels = 2,
395         .color_type = FF_COLOR_YUV,
396         .pixel_type = FF_PIXEL_PLANAR,
397         .depth = 8,
398     },
399
400     [PIX_FMT_BGRA] = {
401         .nb_channels = 4, .is_alpha = 1,
402         .color_type = FF_COLOR_RGB,
403         .pixel_type = FF_PIXEL_PACKED,
404         .depth = 8,
405     },
406     [PIX_FMT_RGBA] = {
407         .nb_channels = 4, .is_alpha = 1,
408         .color_type = FF_COLOR_RGB,
409         .pixel_type = FF_PIXEL_PACKED,
410         .depth = 8,
411     },
412 };
413
414 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
415 {
416     *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
417     *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
418 }
419
420 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
421 {
422     if ((unsigned)pix_fmt >= PIX_FMT_NB)
423         return NULL;
424     else
425         return av_pix_fmt_descriptors[pix_fmt].name;
426 }
427
428 #if LIBAVCODEC_VERSION_MAJOR < 53
429 enum PixelFormat avcodec_get_pix_fmt(const char *name)
430 {
431     return av_get_pix_fmt(name);
432 }
433
434 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
435 {
436     av_get_pix_fmt_string(buf, buf_size, pix_fmt);
437 }
438 #endif
439
440 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
441 {
442     return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
443 }
444
445 #if LIBAVCODEC_VERSION_MAJOR < 53
446 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
447     return ff_set_systematic_pal2(pal, pix_fmt);
448 }
449
450 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
451 {
452     return av_image_fill_linesizes(picture->linesize, pix_fmt, width);
453 }
454
455 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
456                     int height)
457 {
458     return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
459 }
460 #endif
461
462 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
463                    enum PixelFormat pix_fmt, int width, int height)
464 {
465     int ret;
466
467     if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
468         return ret;
469
470     if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
471         return ret;
472
473     return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
474 }
475
476 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
477                      unsigned char *dest, int dest_size)
478 {
479     int i, j, nb_planes = 0, linesizes[4];
480     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
481     int size = avpicture_get_size(pix_fmt, width, height);
482
483     if (size > dest_size || size < 0)
484         return AVERROR(EINVAL);
485
486     for (i = 0; i < desc->nb_components; i++)
487         nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
488     nb_planes++;
489
490     av_image_fill_linesizes(linesizes, pix_fmt, width);
491     for (i = 0; i < nb_planes; i++) {
492         int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
493         const unsigned char *s = src->data[i];
494         h = (height + (1 << shift) - 1) >> shift;
495
496         for (j = 0; j < h; j++) {
497             memcpy(dest, s, linesizes[i]);
498             dest += linesizes[i];
499             s += src->linesize[i];
500         }
501     }
502
503     if (desc->flags & PIX_FMT_PAL)
504         memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
505
506     return size;
507 }
508
509 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
510 {
511     AVPicture dummy_pict;
512     if(av_image_check_size(width, height, 0, NULL))
513         return -1;
514     switch (pix_fmt) {
515     case PIX_FMT_RGB8:
516     case PIX_FMT_BGR8:
517     case PIX_FMT_RGB4_BYTE:
518     case PIX_FMT_BGR4_BYTE:
519     case PIX_FMT_GRAY8:
520         // do not include palette for these pseudo-paletted formats
521         return width * height;
522     }
523     return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
524 }
525
526 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
527                              int has_alpha)
528 {
529     const PixFmtInfo *pf, *ps;
530     const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
531     const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
532     int loss;
533
534     ps = &pix_fmt_info[src_pix_fmt];
535
536     /* compute loss */
537     loss = 0;
538     pf = &pix_fmt_info[dst_pix_fmt];
539     if (pf->depth < ps->depth ||
540         ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
541           dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
542          (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
543           src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
544         loss |= FF_LOSS_DEPTH;
545     if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
546         dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
547         loss |= FF_LOSS_RESOLUTION;
548     switch(pf->color_type) {
549     case FF_COLOR_RGB:
550         if (ps->color_type != FF_COLOR_RGB &&
551             ps->color_type != FF_COLOR_GRAY)
552             loss |= FF_LOSS_COLORSPACE;
553         break;
554     case FF_COLOR_GRAY:
555         if (ps->color_type != FF_COLOR_GRAY)
556             loss |= FF_LOSS_COLORSPACE;
557         break;
558     case FF_COLOR_YUV:
559         if (ps->color_type != FF_COLOR_YUV)
560             loss |= FF_LOSS_COLORSPACE;
561         break;
562     case FF_COLOR_YUV_JPEG:
563         if (ps->color_type != FF_COLOR_YUV_JPEG &&
564             ps->color_type != FF_COLOR_YUV &&
565             ps->color_type != FF_COLOR_GRAY)
566             loss |= FF_LOSS_COLORSPACE;
567         break;
568     default:
569         /* fail safe test */
570         if (ps->color_type != pf->color_type)
571             loss |= FF_LOSS_COLORSPACE;
572         break;
573     }
574     if (pf->color_type == FF_COLOR_GRAY &&
575         ps->color_type != FF_COLOR_GRAY)
576         loss |= FF_LOSS_CHROMA;
577     if (!pf->is_alpha && (ps->is_alpha && has_alpha))
578         loss |= FF_LOSS_ALPHA;
579     if (pf->pixel_type == FF_PIXEL_PALETTE &&
580         (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
581         loss |= FF_LOSS_COLORQUANT;
582     return loss;
583 }
584
585 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
586 {
587     int bits;
588     const PixFmtInfo *pf;
589     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
590
591     pf = &pix_fmt_info[pix_fmt];
592     switch(pf->pixel_type) {
593     case FF_PIXEL_PACKED:
594         switch(pix_fmt) {
595         case PIX_FMT_YUYV422:
596         case PIX_FMT_UYVY422:
597         case PIX_FMT_RGB565BE:
598         case PIX_FMT_RGB565LE:
599         case PIX_FMT_RGB555BE:
600         case PIX_FMT_RGB555LE:
601         case PIX_FMT_RGB444BE:
602         case PIX_FMT_RGB444LE:
603         case PIX_FMT_BGR565BE:
604         case PIX_FMT_BGR565LE:
605         case PIX_FMT_BGR555BE:
606         case PIX_FMT_BGR555LE:
607         case PIX_FMT_BGR444BE:
608         case PIX_FMT_BGR444LE:
609             bits = 16;
610             break;
611         case PIX_FMT_UYYVYY411:
612             bits = 12;
613             break;
614         default:
615             bits = pf->depth * pf->nb_channels;
616             break;
617         }
618         break;
619     case FF_PIXEL_PLANAR:
620         if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
621             bits = pf->depth * pf->nb_channels;
622         } else {
623             bits = pf->depth + ((2 * pf->depth) >>
624                                 (desc->log2_chroma_w + desc->log2_chroma_h));
625         }
626         break;
627     case FF_PIXEL_PALETTE:
628         bits = 8;
629         break;
630     default:
631         bits = -1;
632         break;
633     }
634     return bits;
635 }
636
637 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
638                                       enum PixelFormat src_pix_fmt,
639                                       int has_alpha,
640                                       int loss_mask)
641 {
642     int dist, i, loss, min_dist;
643     enum PixelFormat dst_pix_fmt;
644
645     /* find exact color match with smallest size */
646     dst_pix_fmt = PIX_FMT_NONE;
647     min_dist = 0x7fffffff;
648     for(i = 0;i < PIX_FMT_NB; i++) {
649         if (pix_fmt_mask & (1ULL << i)) {
650             loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
651             if (loss == 0) {
652                 dist = avg_bits_per_pixel(i);
653                 if (dist < min_dist) {
654                     min_dist = dist;
655                     dst_pix_fmt = i;
656                 }
657             }
658         }
659     }
660     return dst_pix_fmt;
661 }
662
663 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
664                               int has_alpha, int *loss_ptr)
665 {
666     enum PixelFormat dst_pix_fmt;
667     int loss_mask, i;
668     static const int loss_mask_order[] = {
669         ~0, /* no loss first */
670         ~FF_LOSS_ALPHA,
671         ~FF_LOSS_RESOLUTION,
672         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
673         ~FF_LOSS_COLORQUANT,
674         ~FF_LOSS_DEPTH,
675         0,
676     };
677
678     /* try with successive loss */
679     i = 0;
680     for(;;) {
681         loss_mask = loss_mask_order[i++];
682         dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
683                                                  has_alpha, loss_mask);
684         if (dst_pix_fmt >= 0)
685             goto found;
686         if (loss_mask == 0)
687             break;
688     }
689     return PIX_FMT_NONE;
690  found:
691     if (loss_ptr)
692         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
693     return dst_pix_fmt;
694 }
695
696 #if LIBAVCODEC_VERSION_MAJOR < 53
697 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
698                            const uint8_t *src, int src_wrap,
699                            int width, int height)
700 {
701     av_image_copy_plane(dst, dst_wrap, src, src_wrap, width, height);
702 }
703
704 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
705 {
706     return av_image_get_linesize(pix_fmt, width, plane);
707 }
708
709 void av_picture_data_copy(uint8_t *dst_data[4], int dst_linesize[4],
710                           uint8_t *src_data[4], int src_linesize[4],
711                           enum PixelFormat pix_fmt, int width, int height)
712 {
713     av_image_copy(dst_data, dst_linesize, src_data, src_linesize,
714                   pix_fmt, width, height);
715 }
716 #endif
717
718 void av_picture_copy(AVPicture *dst, const AVPicture *src,
719                      enum PixelFormat pix_fmt, int width, int height)
720 {
721     av_image_copy(dst->data, dst->linesize, src->data,
722                   src->linesize, pix_fmt, width, height);
723 }
724
725 /* 2x2 -> 1x1 */
726 void ff_shrink22(uint8_t *dst, int dst_wrap,
727                      const uint8_t *src, int src_wrap,
728                      int width, int height)
729 {
730     int w;
731     const uint8_t *s1, *s2;
732     uint8_t *d;
733
734     for(;height > 0; height--) {
735         s1 = src;
736         s2 = s1 + src_wrap;
737         d = dst;
738         for(w = width;w >= 4; w-=4) {
739             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
740             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
741             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
742             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
743             s1 += 8;
744             s2 += 8;
745             d += 4;
746         }
747         for(;w > 0; w--) {
748             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
749             s1 += 2;
750             s2 += 2;
751             d++;
752         }
753         src += 2 * src_wrap;
754         dst += dst_wrap;
755     }
756 }
757
758 /* 4x4 -> 1x1 */
759 void ff_shrink44(uint8_t *dst, int dst_wrap,
760                      const uint8_t *src, int src_wrap,
761                      int width, int height)
762 {
763     int w;
764     const uint8_t *s1, *s2, *s3, *s4;
765     uint8_t *d;
766
767     for(;height > 0; height--) {
768         s1 = src;
769         s2 = s1 + src_wrap;
770         s3 = s2 + src_wrap;
771         s4 = s3 + src_wrap;
772         d = dst;
773         for(w = width;w > 0; w--) {
774             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
775                     s2[0] + s2[1] + s2[2] + s2[3] +
776                     s3[0] + s3[1] + s3[2] + s3[3] +
777                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
778             s1 += 4;
779             s2 += 4;
780             s3 += 4;
781             s4 += 4;
782             d++;
783         }
784         src += 4 * src_wrap;
785         dst += dst_wrap;
786     }
787 }
788
789 /* 8x8 -> 1x1 */
790 void ff_shrink88(uint8_t *dst, int dst_wrap,
791                      const uint8_t *src, int src_wrap,
792                      int width, int height)
793 {
794     int w, i;
795
796     for(;height > 0; height--) {
797         for(w = width;w > 0; w--) {
798             int tmp=0;
799             for(i=0; i<8; i++){
800                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
801                 src += src_wrap;
802             }
803             *(dst++) = (tmp + 32)>>6;
804             src += 8 - 8*src_wrap;
805         }
806         src += 8*src_wrap - 8*width;
807         dst += dst_wrap - width;
808     }
809 }
810
811
812 int avpicture_alloc(AVPicture *picture,
813                     enum PixelFormat pix_fmt, int width, int height)
814 {
815     int ret;
816
817     if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
818         memset(picture, 0, sizeof(AVPicture));
819         return ret;
820     }
821
822     return 0;
823 }
824
825 void avpicture_free(AVPicture *picture)
826 {
827     av_free(picture->data[0]);
828 }
829
830 /* return true if yuv planar */
831 static inline int is_yuv_planar(const PixFmtInfo *ps)
832 {
833     return (ps->color_type == FF_COLOR_YUV ||
834             ps->color_type == FF_COLOR_YUV_JPEG) &&
835         ps->pixel_type == FF_PIXEL_PLANAR;
836 }
837
838 int av_picture_crop(AVPicture *dst, const AVPicture *src,
839                     enum PixelFormat pix_fmt, int top_band, int left_band)
840 {
841     int y_shift;
842     int x_shift;
843
844     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
845         return -1;
846
847     y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
848     x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
849
850     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
851     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
852     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
853
854     dst->linesize[0] = src->linesize[0];
855     dst->linesize[1] = src->linesize[1];
856     dst->linesize[2] = src->linesize[2];
857     return 0;
858 }
859
860 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
861                    enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
862             int *color)
863 {
864     uint8_t *optr;
865     int y_shift;
866     int x_shift;
867     int yheight;
868     int i, y;
869
870     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
871         !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
872
873     for (i = 0; i < 3; i++) {
874         x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
875         y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
876
877         if (padtop || padleft) {
878             memset(dst->data[i], color[i],
879                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
880         }
881
882         if (padleft || padright) {
883             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
884                 (dst->linesize[i] - (padright >> x_shift));
885             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
886             for (y = 0; y < yheight; y++) {
887                 memset(optr, color[i], (padleft + padright) >> x_shift);
888                 optr += dst->linesize[i];
889             }
890         }
891
892         if (src) { /* first line */
893             uint8_t *iptr = src->data[i];
894             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
895                     (padleft >> x_shift);
896             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
897             iptr += src->linesize[i];
898             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
899                 (dst->linesize[i] - (padright >> x_shift));
900             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
901             for (y = 0; y < yheight; y++) {
902                 memset(optr, color[i], (padleft + padright) >> x_shift);
903                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
904                        (width - padleft - padright) >> x_shift);
905                 iptr += src->linesize[i];
906                 optr += dst->linesize[i];
907             }
908         }
909
910         if (padbottom || padright) {
911             optr = dst->data[i] + dst->linesize[i] *
912                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
913             memset(optr, color[i],dst->linesize[i] *
914                 (padbottom >> y_shift) + (padright >> x_shift));
915         }
916     }
917     return 0;
918 }
919
920 /* NOTE: we scan all the pixels to have an exact information */
921 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
922 {
923     const unsigned char *p;
924     int src_wrap, ret, x, y;
925     unsigned int a;
926     uint32_t *palette = (uint32_t *)src->data[1];
927
928     p = src->data[0];
929     src_wrap = src->linesize[0] - width;
930     ret = 0;
931     for(y=0;y<height;y++) {
932         for(x=0;x<width;x++) {
933             a = palette[p[0]] >> 24;
934             if (a == 0x00) {
935                 ret |= FF_ALPHA_TRANSP;
936             } else if (a != 0xff) {
937                 ret |= FF_ALPHA_SEMI_TRANSP;
938             }
939             p++;
940         }
941         p += src_wrap;
942     }
943     return ret;
944 }
945
946 int img_get_alpha_info(const AVPicture *src,
947                        enum PixelFormat pix_fmt, int width, int height)
948 {
949     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
950     int ret;
951
952     /* no alpha can be represented in format */
953     if (!pf->is_alpha)
954         return 0;
955     switch(pix_fmt) {
956     case PIX_FMT_PAL8:
957         ret = get_alpha_info_pal8(src, width, height);
958         break;
959     default:
960         /* we do not know, so everything is indicated */
961         ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
962         break;
963     }
964     return ret;
965 }
966
967 #if !(HAVE_MMX && HAVE_YASM)
968 /* filter parameters: [-1 4 2 4 -1] // 8 */
969 static void deinterlace_line_c(uint8_t *dst,
970                              const uint8_t *lum_m4, const uint8_t *lum_m3,
971                              const uint8_t *lum_m2, const uint8_t *lum_m1,
972                              const uint8_t *lum,
973                              int size)
974 {
975     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
976     int sum;
977
978     for(;size > 0;size--) {
979         sum = -lum_m4[0];
980         sum += lum_m3[0] << 2;
981         sum += lum_m2[0] << 1;
982         sum += lum_m1[0] << 2;
983         sum += -lum[0];
984         dst[0] = cm[(sum + 4) >> 3];
985         lum_m4++;
986         lum_m3++;
987         lum_m2++;
988         lum_m1++;
989         lum++;
990         dst++;
991     }
992 }
993
994 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
995                                        uint8_t *lum_m2, uint8_t *lum_m1,
996                                        uint8_t *lum, int size)
997 {
998     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
999     int sum;
1000
1001     for(;size > 0;size--) {
1002         sum = -lum_m4[0];
1003         sum += lum_m3[0] << 2;
1004         sum += lum_m2[0] << 1;
1005         lum_m4[0]=lum_m2[0];
1006         sum += lum_m1[0] << 2;
1007         sum += -lum[0];
1008         lum_m2[0] = cm[(sum + 4) >> 3];
1009         lum_m4++;
1010         lum_m3++;
1011         lum_m2++;
1012         lum_m1++;
1013         lum++;
1014     }
1015 }
1016 #endif
1017
1018 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1019    top field is copied as is, but the bottom field is deinterlaced
1020    against the top field. */
1021 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1022                                     const uint8_t *src1, int src_wrap,
1023                                     int width, int height)
1024 {
1025     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1026     int y;
1027
1028     src_m2 = src1;
1029     src_m1 = src1;
1030     src_0=&src_m1[src_wrap];
1031     src_p1=&src_0[src_wrap];
1032     src_p2=&src_p1[src_wrap];
1033     for(y=0;y<(height-2);y+=2) {
1034         memcpy(dst,src_m1,width);
1035         dst += dst_wrap;
1036         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1037         src_m2 = src_0;
1038         src_m1 = src_p1;
1039         src_0 = src_p2;
1040         src_p1 += 2*src_wrap;
1041         src_p2 += 2*src_wrap;
1042         dst += dst_wrap;
1043     }
1044     memcpy(dst,src_m1,width);
1045     dst += dst_wrap;
1046     /* do last line */
1047     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1048 }
1049
1050 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1051                                              int width, int height)
1052 {
1053     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1054     int y;
1055     uint8_t *buf;
1056     buf = (uint8_t*)av_malloc(width);
1057
1058     src_m1 = src1;
1059     memcpy(buf,src_m1,width);
1060     src_0=&src_m1[src_wrap];
1061     src_p1=&src_0[src_wrap];
1062     src_p2=&src_p1[src_wrap];
1063     for(y=0;y<(height-2);y+=2) {
1064         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1065         src_m1 = src_p1;
1066         src_0 = src_p2;
1067         src_p1 += 2*src_wrap;
1068         src_p2 += 2*src_wrap;
1069     }
1070     /* do last line */
1071     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1072     av_free(buf);
1073 }
1074
1075 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1076                           enum PixelFormat pix_fmt, int width, int height)
1077 {
1078     int i;
1079
1080     if (pix_fmt != PIX_FMT_YUV420P &&
1081         pix_fmt != PIX_FMT_YUVJ420P &&
1082         pix_fmt != PIX_FMT_YUV422P &&
1083         pix_fmt != PIX_FMT_YUVJ422P &&
1084         pix_fmt != PIX_FMT_YUV444P &&
1085         pix_fmt != PIX_FMT_YUV411P &&
1086         pix_fmt != PIX_FMT_GRAY8)
1087         return -1;
1088     if ((width & 3) != 0 || (height & 3) != 0)
1089         return -1;
1090
1091     for(i=0;i<3;i++) {
1092         if (i == 1) {
1093             switch(pix_fmt) {
1094             case PIX_FMT_YUVJ420P:
1095             case PIX_FMT_YUV420P:
1096                 width >>= 1;
1097                 height >>= 1;
1098                 break;
1099             case PIX_FMT_YUV422P:
1100             case PIX_FMT_YUVJ422P:
1101                 width >>= 1;
1102                 break;
1103             case PIX_FMT_YUV411P:
1104                 width >>= 2;
1105                 break;
1106             default:
1107                 break;
1108             }
1109             if (pix_fmt == PIX_FMT_GRAY8) {
1110                 break;
1111             }
1112         }
1113         if (src == dst) {
1114             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1115                                  width, height);
1116         } else {
1117             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1118                                         src->data[i], src->linesize[i],
1119                                         width, height);
1120         }
1121     }
1122     emms_c();
1123     return 0;
1124 }
1125