]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgconvert.c
Remove alpha information from avcodec_pix_fmt_string(), as that
[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
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 (pix_fmt < 0 || 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 #endif
434
435 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
436 {
437     /* print header */
438     if (pix_fmt < 0)
439         snprintf (buf, buf_size,
440                   "name      " " nb_channels" " depth"
441             );
442     else{
443         PixFmtInfo info= pix_fmt_info[pix_fmt];
444
445         snprintf (buf, buf_size,
446                   "%-11s %5d %9d",
447                   av_pix_fmt_descriptors[pix_fmt].name,
448                   info.nb_channels,
449                   info.depth
450             );
451     }
452 }
453
454 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
455 {
456     return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
457 }
458
459 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
460     int i;
461
462     for(i=0; i<256; i++){
463         int r,g,b;
464
465         switch(pix_fmt) {
466         case PIX_FMT_RGB8:
467             r= (i>>5    )*36;
468             g= ((i>>2)&7)*36;
469             b= (i&3     )*85;
470             break;
471         case PIX_FMT_BGR8:
472             b= (i>>6    )*85;
473             g= ((i>>3)&7)*36;
474             r= (i&7     )*36;
475             break;
476         case PIX_FMT_RGB4_BYTE:
477             r= (i>>3    )*255;
478             g= ((i>>1)&3)*85;
479             b= (i&1     )*255;
480             break;
481         case PIX_FMT_BGR4_BYTE:
482             b= (i>>3    )*255;
483             g= ((i>>1)&3)*85;
484             r= (i&1     )*255;
485             break;
486         case PIX_FMT_GRAY8:
487             r=b=g= i;
488             break;
489         default:
490             return -1;
491         }
492         pal[i] =  b + (g<<8) + (r<<16);
493     }
494
495     return 0;
496 }
497
498 #if LIBAVCODEC_VERSION_MAJOR < 53
499 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
500 {
501     return av_image_fill_linesizes(picture->linesize, pix_fmt, width);
502 }
503
504 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
505                     int height)
506 {
507     return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
508 }
509 #endif
510
511 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
512                    enum PixelFormat pix_fmt, int width, int height)
513 {
514
515     if(av_image_check_size(width, height, 0, NULL))
516         return -1;
517
518     if (av_image_fill_linesizes(picture->linesize, pix_fmt, width))
519         return -1;
520
521     return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
522 }
523
524 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
525                      unsigned char *dest, int dest_size)
526 {
527     const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
528     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
529     int i, j, w, ow, h, oh, data_planes;
530     const unsigned char* s;
531     int size = avpicture_get_size(pix_fmt, width, height);
532
533     if (size > dest_size || size < 0)
534         return -1;
535
536     if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
537         if (pix_fmt == PIX_FMT_YUYV422 ||
538             pix_fmt == PIX_FMT_UYVY422 ||
539             pix_fmt == PIX_FMT_BGR565BE ||
540             pix_fmt == PIX_FMT_BGR565LE ||
541             pix_fmt == PIX_FMT_BGR555BE ||
542             pix_fmt == PIX_FMT_BGR555LE ||
543             pix_fmt == PIX_FMT_BGR444BE ||
544             pix_fmt == PIX_FMT_BGR444LE ||
545             pix_fmt == PIX_FMT_RGB565BE ||
546             pix_fmt == PIX_FMT_RGB565LE ||
547             pix_fmt == PIX_FMT_RGB555BE ||
548             pix_fmt == PIX_FMT_RGB555LE ||
549             pix_fmt == PIX_FMT_RGB444BE ||
550             pix_fmt == PIX_FMT_RGB444LE)
551             w = width * 2;
552         else if (pix_fmt == PIX_FMT_UYYVYY411)
553             w = width + width/2;
554         else if (pix_fmt == PIX_FMT_PAL8)
555             w = width;
556         else
557             w = width * (pf->depth * pf->nb_channels / 8);
558
559         data_planes = 1;
560         h = height;
561     } else {
562         data_planes = pf->nb_channels;
563         w = (width*pf->depth + 7)/8;
564         h = height;
565     }
566
567     ow = w;
568     oh = h;
569
570     for (i=0; i<data_planes; i++) {
571         if (i == 1) {
572             w = (- ((-width) >> desc->log2_chroma_w) * pf->depth + 7) / 8;
573             h = -((-height) >> desc->log2_chroma_h);
574             if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
575                 w <<= 1;
576         } else if (i == 3) {
577             w = ow;
578             h = oh;
579         }
580         s = src->data[i];
581         for(j=0; j<h; j++) {
582             memcpy(dest, s, w);
583             dest += w;
584             s += src->linesize[i];
585         }
586     }
587
588     if (pf->pixel_type == FF_PIXEL_PALETTE)
589         memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
590
591     return size;
592 }
593
594 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
595 {
596     AVPicture dummy_pict;
597     if(av_image_check_size(width, height, 0, NULL))
598         return -1;
599     switch (pix_fmt) {
600     case PIX_FMT_RGB8:
601     case PIX_FMT_BGR8:
602     case PIX_FMT_RGB4_BYTE:
603     case PIX_FMT_BGR4_BYTE:
604     case PIX_FMT_GRAY8:
605         // do not include palette for these pseudo-paletted formats
606         return width * height;
607     }
608     return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
609 }
610
611 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
612                              int has_alpha)
613 {
614     const PixFmtInfo *pf, *ps;
615     const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
616     const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
617     int loss;
618
619     ps = &pix_fmt_info[src_pix_fmt];
620
621     /* compute loss */
622     loss = 0;
623     pf = &pix_fmt_info[dst_pix_fmt];
624     if (pf->depth < ps->depth ||
625         ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
626           dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
627          (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
628           src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
629         loss |= FF_LOSS_DEPTH;
630     if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
631         dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
632         loss |= FF_LOSS_RESOLUTION;
633     switch(pf->color_type) {
634     case FF_COLOR_RGB:
635         if (ps->color_type != FF_COLOR_RGB &&
636             ps->color_type != FF_COLOR_GRAY)
637             loss |= FF_LOSS_COLORSPACE;
638         break;
639     case FF_COLOR_GRAY:
640         if (ps->color_type != FF_COLOR_GRAY)
641             loss |= FF_LOSS_COLORSPACE;
642         break;
643     case FF_COLOR_YUV:
644         if (ps->color_type != FF_COLOR_YUV)
645             loss |= FF_LOSS_COLORSPACE;
646         break;
647     case FF_COLOR_YUV_JPEG:
648         if (ps->color_type != FF_COLOR_YUV_JPEG &&
649             ps->color_type != FF_COLOR_YUV &&
650             ps->color_type != FF_COLOR_GRAY)
651             loss |= FF_LOSS_COLORSPACE;
652         break;
653     default:
654         /* fail safe test */
655         if (ps->color_type != pf->color_type)
656             loss |= FF_LOSS_COLORSPACE;
657         break;
658     }
659     if (pf->color_type == FF_COLOR_GRAY &&
660         ps->color_type != FF_COLOR_GRAY)
661         loss |= FF_LOSS_CHROMA;
662     if (!pf->is_alpha && (ps->is_alpha && has_alpha))
663         loss |= FF_LOSS_ALPHA;
664     if (pf->pixel_type == FF_PIXEL_PALETTE &&
665         (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
666         loss |= FF_LOSS_COLORQUANT;
667     return loss;
668 }
669
670 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
671 {
672     int bits;
673     const PixFmtInfo *pf;
674     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
675
676     pf = &pix_fmt_info[pix_fmt];
677     switch(pf->pixel_type) {
678     case FF_PIXEL_PACKED:
679         switch(pix_fmt) {
680         case PIX_FMT_YUYV422:
681         case PIX_FMT_UYVY422:
682         case PIX_FMT_RGB565BE:
683         case PIX_FMT_RGB565LE:
684         case PIX_FMT_RGB555BE:
685         case PIX_FMT_RGB555LE:
686         case PIX_FMT_RGB444BE:
687         case PIX_FMT_RGB444LE:
688         case PIX_FMT_BGR565BE:
689         case PIX_FMT_BGR565LE:
690         case PIX_FMT_BGR555BE:
691         case PIX_FMT_BGR555LE:
692         case PIX_FMT_BGR444BE:
693         case PIX_FMT_BGR444LE:
694             bits = 16;
695             break;
696         case PIX_FMT_UYYVYY411:
697             bits = 12;
698             break;
699         default:
700             bits = pf->depth * pf->nb_channels;
701             break;
702         }
703         break;
704     case FF_PIXEL_PLANAR:
705         if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
706             bits = pf->depth * pf->nb_channels;
707         } else {
708             bits = pf->depth + ((2 * pf->depth) >>
709                                 (desc->log2_chroma_w + desc->log2_chroma_h));
710         }
711         break;
712     case FF_PIXEL_PALETTE:
713         bits = 8;
714         break;
715     default:
716         bits = -1;
717         break;
718     }
719     return bits;
720 }
721
722 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
723                                       enum PixelFormat src_pix_fmt,
724                                       int has_alpha,
725                                       int loss_mask)
726 {
727     int dist, i, loss, min_dist;
728     enum PixelFormat dst_pix_fmt;
729
730     /* find exact color match with smallest size */
731     dst_pix_fmt = PIX_FMT_NONE;
732     min_dist = 0x7fffffff;
733     for(i = 0;i < PIX_FMT_NB; i++) {
734         if (pix_fmt_mask & (1ULL << i)) {
735             loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
736             if (loss == 0) {
737                 dist = avg_bits_per_pixel(i);
738                 if (dist < min_dist) {
739                     min_dist = dist;
740                     dst_pix_fmt = i;
741                 }
742             }
743         }
744     }
745     return dst_pix_fmt;
746 }
747
748 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
749                               int has_alpha, int *loss_ptr)
750 {
751     enum PixelFormat dst_pix_fmt;
752     int loss_mask, i;
753     static const int loss_mask_order[] = {
754         ~0, /* no loss first */
755         ~FF_LOSS_ALPHA,
756         ~FF_LOSS_RESOLUTION,
757         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
758         ~FF_LOSS_COLORQUANT,
759         ~FF_LOSS_DEPTH,
760         0,
761     };
762
763     /* try with successive loss */
764     i = 0;
765     for(;;) {
766         loss_mask = loss_mask_order[i++];
767         dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
768                                                  has_alpha, loss_mask);
769         if (dst_pix_fmt >= 0)
770             goto found;
771         if (loss_mask == 0)
772             break;
773     }
774     return PIX_FMT_NONE;
775  found:
776     if (loss_ptr)
777         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
778     return dst_pix_fmt;
779 }
780
781 #if LIBAVCODEC_VERSION_MAJOR < 53
782 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
783                            const uint8_t *src, int src_wrap,
784                            int width, int height)
785 {
786     av_image_copy_plane(dst, dst_wrap, src, src_wrap, width, height);
787 }
788
789 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
790 {
791     return av_image_get_linesize(pix_fmt, width, plane);
792 }
793
794 void av_picture_data_copy(uint8_t *dst_data[4], int dst_linesize[4],
795                           uint8_t *src_data[4], int src_linesize[4],
796                           enum PixelFormat pix_fmt, int width, int height)
797 {
798     av_image_copy(dst_data, dst_linesize, src_data, src_linesize,
799                   pix_fmt, width, height);
800 }
801 #endif
802
803 void av_picture_copy(AVPicture *dst, const AVPicture *src,
804                      enum PixelFormat pix_fmt, int width, int height)
805 {
806     av_image_copy(dst->data, dst->linesize, src->data,
807                   src->linesize, pix_fmt, width, height);
808 }
809
810 /* 2x2 -> 1x1 */
811 void ff_shrink22(uint8_t *dst, int dst_wrap,
812                      const uint8_t *src, int src_wrap,
813                      int width, int height)
814 {
815     int w;
816     const uint8_t *s1, *s2;
817     uint8_t *d;
818
819     for(;height > 0; height--) {
820         s1 = src;
821         s2 = s1 + src_wrap;
822         d = dst;
823         for(w = width;w >= 4; w-=4) {
824             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
825             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
826             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
827             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
828             s1 += 8;
829             s2 += 8;
830             d += 4;
831         }
832         for(;w > 0; w--) {
833             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
834             s1 += 2;
835             s2 += 2;
836             d++;
837         }
838         src += 2 * src_wrap;
839         dst += dst_wrap;
840     }
841 }
842
843 /* 4x4 -> 1x1 */
844 void ff_shrink44(uint8_t *dst, int dst_wrap,
845                      const uint8_t *src, int src_wrap,
846                      int width, int height)
847 {
848     int w;
849     const uint8_t *s1, *s2, *s3, *s4;
850     uint8_t *d;
851
852     for(;height > 0; height--) {
853         s1 = src;
854         s2 = s1 + src_wrap;
855         s3 = s2 + src_wrap;
856         s4 = s3 + src_wrap;
857         d = dst;
858         for(w = width;w > 0; w--) {
859             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
860                     s2[0] + s2[1] + s2[2] + s2[3] +
861                     s3[0] + s3[1] + s3[2] + s3[3] +
862                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
863             s1 += 4;
864             s2 += 4;
865             s3 += 4;
866             s4 += 4;
867             d++;
868         }
869         src += 4 * src_wrap;
870         dst += dst_wrap;
871     }
872 }
873
874 /* 8x8 -> 1x1 */
875 void ff_shrink88(uint8_t *dst, int dst_wrap,
876                      const uint8_t *src, int src_wrap,
877                      int width, int height)
878 {
879     int w, i;
880
881     for(;height > 0; height--) {
882         for(w = width;w > 0; w--) {
883             int tmp=0;
884             for(i=0; i<8; i++){
885                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
886                 src += src_wrap;
887             }
888             *(dst++) = (tmp + 32)>>6;
889             src += 8 - 8*src_wrap;
890         }
891         src += 8*src_wrap - 8*width;
892         dst += dst_wrap - width;
893     }
894 }
895
896
897 int avpicture_alloc(AVPicture *picture,
898                     enum PixelFormat pix_fmt, int width, int height)
899 {
900     int size;
901     void *ptr;
902
903     size = avpicture_fill(picture, NULL, pix_fmt, width, height);
904     if(size<0)
905         goto fail;
906     ptr = av_malloc(size);
907     if (!ptr)
908         goto fail;
909     avpicture_fill(picture, ptr, pix_fmt, width, height);
910     if(picture->data[1] && !picture->data[2])
911         ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
912
913     return 0;
914  fail:
915     memset(picture, 0, sizeof(AVPicture));
916     return -1;
917 }
918
919 void avpicture_free(AVPicture *picture)
920 {
921     av_free(picture->data[0]);
922 }
923
924 /* return true if yuv planar */
925 static inline int is_yuv_planar(const PixFmtInfo *ps)
926 {
927     return (ps->color_type == FF_COLOR_YUV ||
928             ps->color_type == FF_COLOR_YUV_JPEG) &&
929         ps->pixel_type == FF_PIXEL_PLANAR;
930 }
931
932 int av_picture_crop(AVPicture *dst, const AVPicture *src,
933                     enum PixelFormat pix_fmt, int top_band, int left_band)
934 {
935     int y_shift;
936     int x_shift;
937
938     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
939         return -1;
940
941     y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
942     x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
943
944     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
945     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
946     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
947
948     dst->linesize[0] = src->linesize[0];
949     dst->linesize[1] = src->linesize[1];
950     dst->linesize[2] = src->linesize[2];
951     return 0;
952 }
953
954 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
955                    enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
956             int *color)
957 {
958     uint8_t *optr;
959     int y_shift;
960     int x_shift;
961     int yheight;
962     int i, y;
963
964     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
965         !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
966
967     for (i = 0; i < 3; i++) {
968         x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
969         y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
970
971         if (padtop || padleft) {
972             memset(dst->data[i], color[i],
973                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
974         }
975
976         if (padleft || padright) {
977             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
978                 (dst->linesize[i] - (padright >> x_shift));
979             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
980             for (y = 0; y < yheight; y++) {
981                 memset(optr, color[i], (padleft + padright) >> x_shift);
982                 optr += dst->linesize[i];
983             }
984         }
985
986         if (src) { /* first line */
987             uint8_t *iptr = src->data[i];
988             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
989                     (padleft >> x_shift);
990             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
991             iptr += src->linesize[i];
992             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
993                 (dst->linesize[i] - (padright >> x_shift));
994             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
995             for (y = 0; y < yheight; y++) {
996                 memset(optr, color[i], (padleft + padright) >> x_shift);
997                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
998                        (width - padleft - padright) >> x_shift);
999                 iptr += src->linesize[i];
1000                 optr += dst->linesize[i];
1001             }
1002         }
1003
1004         if (padbottom || padright) {
1005             optr = dst->data[i] + dst->linesize[i] *
1006                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1007             memset(optr, color[i],dst->linesize[i] *
1008                 (padbottom >> y_shift) + (padright >> x_shift));
1009         }
1010     }
1011     return 0;
1012 }
1013
1014 /* NOTE: we scan all the pixels to have an exact information */
1015 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1016 {
1017     const unsigned char *p;
1018     int src_wrap, ret, x, y;
1019     unsigned int a;
1020     uint32_t *palette = (uint32_t *)src->data[1];
1021
1022     p = src->data[0];
1023     src_wrap = src->linesize[0] - width;
1024     ret = 0;
1025     for(y=0;y<height;y++) {
1026         for(x=0;x<width;x++) {
1027             a = palette[p[0]] >> 24;
1028             if (a == 0x00) {
1029                 ret |= FF_ALPHA_TRANSP;
1030             } else if (a != 0xff) {
1031                 ret |= FF_ALPHA_SEMI_TRANSP;
1032             }
1033             p++;
1034         }
1035         p += src_wrap;
1036     }
1037     return ret;
1038 }
1039
1040 int img_get_alpha_info(const AVPicture *src,
1041                        enum PixelFormat pix_fmt, int width, int height)
1042 {
1043     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1044     int ret;
1045
1046     /* no alpha can be represented in format */
1047     if (!pf->is_alpha)
1048         return 0;
1049     switch(pix_fmt) {
1050     case PIX_FMT_PAL8:
1051         ret = get_alpha_info_pal8(src, width, height);
1052         break;
1053     default:
1054         /* we do not know, so everything is indicated */
1055         ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1056         break;
1057     }
1058     return ret;
1059 }
1060
1061 #if !(HAVE_MMX && HAVE_YASM)
1062 /* filter parameters: [-1 4 2 4 -1] // 8 */
1063 static void deinterlace_line_c(uint8_t *dst,
1064                              const uint8_t *lum_m4, const uint8_t *lum_m3,
1065                              const uint8_t *lum_m2, const uint8_t *lum_m1,
1066                              const uint8_t *lum,
1067                              int size)
1068 {
1069     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1070     int sum;
1071
1072     for(;size > 0;size--) {
1073         sum = -lum_m4[0];
1074         sum += lum_m3[0] << 2;
1075         sum += lum_m2[0] << 1;
1076         sum += lum_m1[0] << 2;
1077         sum += -lum[0];
1078         dst[0] = cm[(sum + 4) >> 3];
1079         lum_m4++;
1080         lum_m3++;
1081         lum_m2++;
1082         lum_m1++;
1083         lum++;
1084         dst++;
1085     }
1086 }
1087
1088 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
1089                                        uint8_t *lum_m2, uint8_t *lum_m1,
1090                                        uint8_t *lum, int size)
1091 {
1092     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1093     int sum;
1094
1095     for(;size > 0;size--) {
1096         sum = -lum_m4[0];
1097         sum += lum_m3[0] << 2;
1098         sum += lum_m2[0] << 1;
1099         lum_m4[0]=lum_m2[0];
1100         sum += lum_m1[0] << 2;
1101         sum += -lum[0];
1102         lum_m2[0] = cm[(sum + 4) >> 3];
1103         lum_m4++;
1104         lum_m3++;
1105         lum_m2++;
1106         lum_m1++;
1107         lum++;
1108     }
1109 }
1110 #endif
1111
1112 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1113    top field is copied as is, but the bottom field is deinterlaced
1114    against the top field. */
1115 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1116                                     const uint8_t *src1, int src_wrap,
1117                                     int width, int height)
1118 {
1119     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1120     int y;
1121
1122     src_m2 = src1;
1123     src_m1 = src1;
1124     src_0=&src_m1[src_wrap];
1125     src_p1=&src_0[src_wrap];
1126     src_p2=&src_p1[src_wrap];
1127     for(y=0;y<(height-2);y+=2) {
1128         memcpy(dst,src_m1,width);
1129         dst += dst_wrap;
1130         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1131         src_m2 = src_0;
1132         src_m1 = src_p1;
1133         src_0 = src_p2;
1134         src_p1 += 2*src_wrap;
1135         src_p2 += 2*src_wrap;
1136         dst += dst_wrap;
1137     }
1138     memcpy(dst,src_m1,width);
1139     dst += dst_wrap;
1140     /* do last line */
1141     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1142 }
1143
1144 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1145                                              int width, int height)
1146 {
1147     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1148     int y;
1149     uint8_t *buf;
1150     buf = (uint8_t*)av_malloc(width);
1151
1152     src_m1 = src1;
1153     memcpy(buf,src_m1,width);
1154     src_0=&src_m1[src_wrap];
1155     src_p1=&src_0[src_wrap];
1156     src_p2=&src_p1[src_wrap];
1157     for(y=0;y<(height-2);y+=2) {
1158         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1159         src_m1 = src_p1;
1160         src_0 = src_p2;
1161         src_p1 += 2*src_wrap;
1162         src_p2 += 2*src_wrap;
1163     }
1164     /* do last line */
1165     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1166     av_free(buf);
1167 }
1168
1169 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1170                           enum PixelFormat pix_fmt, int width, int height)
1171 {
1172     int i;
1173
1174     if (pix_fmt != PIX_FMT_YUV420P &&
1175         pix_fmt != PIX_FMT_YUVJ420P &&
1176         pix_fmt != PIX_FMT_YUV422P &&
1177         pix_fmt != PIX_FMT_YUVJ422P &&
1178         pix_fmt != PIX_FMT_YUV444P &&
1179         pix_fmt != PIX_FMT_YUV411P &&
1180         pix_fmt != PIX_FMT_GRAY8)
1181         return -1;
1182     if ((width & 3) != 0 || (height & 3) != 0)
1183         return -1;
1184
1185     for(i=0;i<3;i++) {
1186         if (i == 1) {
1187             switch(pix_fmt) {
1188             case PIX_FMT_YUVJ420P:
1189             case PIX_FMT_YUV420P:
1190                 width >>= 1;
1191                 height >>= 1;
1192                 break;
1193             case PIX_FMT_YUV422P:
1194             case PIX_FMT_YUVJ422P:
1195                 width >>= 1;
1196                 break;
1197             case PIX_FMT_YUV411P:
1198                 width >>= 2;
1199                 break;
1200             default:
1201                 break;
1202             }
1203             if (pix_fmt == PIX_FMT_GRAY8) {
1204                 break;
1205             }
1206         }
1207         if (src == dst) {
1208             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1209                                  width, height);
1210         } else {
1211             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1212                                         src->data[i], src->linesize[i],
1213                                         width, height);
1214         }
1215     }
1216     emms_c();
1217     return 0;
1218 }
1219