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