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