]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgconvert.c
avcodec: split avpicture from imgconvert
[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 "libavutil/colorspace.h"
37 #include "libavutil/common.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/imgutils.h"
40
41 #if HAVE_MMX_EXTERNAL
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_EXTERNAL
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[AV_PIX_FMT_NB] = {
72     /* YUV formats */
73     [AV_PIX_FMT_YUV420P] = {
74         .nb_channels = 3,
75         .color_type = FF_COLOR_YUV,
76         .pixel_type = FF_PIXEL_PLANAR,
77         .depth = 8,
78     },
79     [AV_PIX_FMT_YUV422P] = {
80         .nb_channels = 3,
81         .color_type = FF_COLOR_YUV,
82         .pixel_type = FF_PIXEL_PLANAR,
83         .depth = 8,
84     },
85     [AV_PIX_FMT_YUV444P] = {
86         .nb_channels = 3,
87         .color_type = FF_COLOR_YUV,
88         .pixel_type = FF_PIXEL_PLANAR,
89         .depth = 8,
90     },
91     [AV_PIX_FMT_YUYV422] = {
92         .nb_channels = 1,
93         .color_type = FF_COLOR_YUV,
94         .pixel_type = FF_PIXEL_PACKED,
95         .depth = 8,
96     },
97     [AV_PIX_FMT_UYVY422] = {
98         .nb_channels = 1,
99         .color_type = FF_COLOR_YUV,
100         .pixel_type = FF_PIXEL_PACKED,
101         .depth = 8,
102     },
103     [AV_PIX_FMT_YUV410P] = {
104         .nb_channels = 3,
105         .color_type = FF_COLOR_YUV,
106         .pixel_type = FF_PIXEL_PLANAR,
107         .depth = 8,
108     },
109     [AV_PIX_FMT_YUV411P] = {
110         .nb_channels = 3,
111         .color_type = FF_COLOR_YUV,
112         .pixel_type = FF_PIXEL_PLANAR,
113         .depth = 8,
114     },
115     [AV_PIX_FMT_YUV440P] = {
116         .nb_channels = 3,
117         .color_type = FF_COLOR_YUV,
118         .pixel_type = FF_PIXEL_PLANAR,
119         .depth = 8,
120     },
121     [AV_PIX_FMT_YUV420P16LE] = {
122         .nb_channels = 3,
123         .color_type = FF_COLOR_YUV,
124         .pixel_type = FF_PIXEL_PLANAR,
125         .depth = 16,
126     },
127     [AV_PIX_FMT_YUV422P16LE] = {
128         .nb_channels = 3,
129         .color_type = FF_COLOR_YUV,
130         .pixel_type = FF_PIXEL_PLANAR,
131         .depth = 16,
132     },
133     [AV_PIX_FMT_YUV444P16LE] = {
134         .nb_channels = 3,
135         .color_type = FF_COLOR_YUV,
136         .pixel_type = FF_PIXEL_PLANAR,
137         .depth = 16,
138     },
139     [AV_PIX_FMT_YUV420P16BE] = {
140         .nb_channels = 3,
141         .color_type = FF_COLOR_YUV,
142         .pixel_type = FF_PIXEL_PLANAR,
143         .depth = 16,
144     },
145     [AV_PIX_FMT_YUV422P16BE] = {
146         .nb_channels = 3,
147         .color_type = FF_COLOR_YUV,
148         .pixel_type = FF_PIXEL_PLANAR,
149         .depth = 16,
150     },
151     [AV_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     [AV_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     [AV_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     [AV_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     [AV_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     [AV_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     [AV_PIX_FMT_RGB24] = {
195         .nb_channels = 3,
196         .color_type = FF_COLOR_RGB,
197         .pixel_type = FF_PIXEL_PACKED,
198         .depth = 8,
199     },
200     [AV_PIX_FMT_BGR24] = {
201         .nb_channels = 3,
202         .color_type = FF_COLOR_RGB,
203         .pixel_type = FF_PIXEL_PACKED,
204         .depth = 8,
205     },
206     [AV_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     [AV_PIX_FMT_RGB48BE] = {
213         .nb_channels = 3,
214         .color_type = FF_COLOR_RGB,
215         .pixel_type = FF_PIXEL_PACKED,
216         .depth = 16,
217     },
218     [AV_PIX_FMT_RGB48LE] = {
219         .nb_channels = 3,
220         .color_type = FF_COLOR_RGB,
221         .pixel_type = FF_PIXEL_PACKED,
222         .depth = 16,
223     },
224     [AV_PIX_FMT_RGB565BE] = {
225         .nb_channels = 3,
226         .color_type = FF_COLOR_RGB,
227         .pixel_type = FF_PIXEL_PACKED,
228         .depth = 5,
229     },
230     [AV_PIX_FMT_RGB565LE] = {
231         .nb_channels = 3,
232         .color_type = FF_COLOR_RGB,
233         .pixel_type = FF_PIXEL_PACKED,
234         .depth = 5,
235     },
236     [AV_PIX_FMT_RGB555BE] = {
237         .nb_channels = 3,
238         .color_type = FF_COLOR_RGB,
239         .pixel_type = FF_PIXEL_PACKED,
240         .depth = 5,
241     },
242     [AV_PIX_FMT_RGB555LE] = {
243         .nb_channels = 3,
244         .color_type = FF_COLOR_RGB,
245         .pixel_type = FF_PIXEL_PACKED,
246         .depth = 5,
247     },
248     [AV_PIX_FMT_RGB444BE] = {
249         .nb_channels = 3,
250         .color_type = FF_COLOR_RGB,
251         .pixel_type = FF_PIXEL_PACKED,
252         .depth = 4,
253     },
254     [AV_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     [AV_PIX_FMT_GRAY16BE] = {
263         .nb_channels = 1,
264         .color_type = FF_COLOR_GRAY,
265         .pixel_type = FF_PIXEL_PLANAR,
266         .depth = 16,
267     },
268     [AV_PIX_FMT_GRAY16LE] = {
269         .nb_channels = 1,
270         .color_type = FF_COLOR_GRAY,
271         .pixel_type = FF_PIXEL_PLANAR,
272         .depth = 16,
273     },
274     [AV_PIX_FMT_GRAY8] = {
275         .nb_channels = 1,
276         .color_type = FF_COLOR_GRAY,
277         .pixel_type = FF_PIXEL_PLANAR,
278         .depth = 8,
279     },
280     [AV_PIX_FMT_MONOWHITE] = {
281         .nb_channels = 1,
282         .color_type = FF_COLOR_GRAY,
283         .pixel_type = FF_PIXEL_PLANAR,
284         .depth = 1,
285     },
286     [AV_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     [AV_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     [AV_PIX_FMT_UYYVYY411] = {
301         .nb_channels = 1,
302         .color_type = FF_COLOR_YUV,
303         .pixel_type = FF_PIXEL_PACKED,
304         .depth = 8,
305     },
306     [AV_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     [AV_PIX_FMT_BGR565BE] = {
313         .nb_channels = 3,
314         .color_type = FF_COLOR_RGB,
315         .pixel_type = FF_PIXEL_PACKED,
316         .depth = 5,
317     },
318     [AV_PIX_FMT_BGR565LE] = {
319         .nb_channels = 3,
320         .color_type = FF_COLOR_RGB,
321         .pixel_type = FF_PIXEL_PACKED,
322         .depth = 5,
323     },
324     [AV_PIX_FMT_BGR555BE] = {
325         .nb_channels = 3,
326         .color_type = FF_COLOR_RGB,
327         .pixel_type = FF_PIXEL_PACKED,
328         .depth = 5,
329     },
330     [AV_PIX_FMT_BGR555LE] = {
331         .nb_channels = 3,
332         .color_type = FF_COLOR_RGB,
333         .pixel_type = FF_PIXEL_PACKED,
334         .depth = 5,
335     },
336     [AV_PIX_FMT_BGR444BE] = {
337         .nb_channels = 3,
338         .color_type = FF_COLOR_RGB,
339         .pixel_type = FF_PIXEL_PACKED,
340         .depth = 4,
341     },
342     [AV_PIX_FMT_BGR444LE] = {
343         .nb_channels = 3,
344         .color_type = FF_COLOR_RGB,
345         .pixel_type = FF_PIXEL_PACKED,
346         .depth = 4,
347     },
348     [AV_PIX_FMT_RGB8] = {
349         .nb_channels = 1,
350         .color_type = FF_COLOR_RGB,
351         .pixel_type = FF_PIXEL_PACKED,
352         .depth = 8,
353     },
354     [AV_PIX_FMT_RGB4] = {
355         .nb_channels = 1,
356         .color_type = FF_COLOR_RGB,
357         .pixel_type = FF_PIXEL_PACKED,
358         .depth = 4,
359     },
360     [AV_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     [AV_PIX_FMT_BGR8] = {
367         .nb_channels = 1,
368         .color_type = FF_COLOR_RGB,
369         .pixel_type = FF_PIXEL_PACKED,
370         .depth = 8,
371     },
372     [AV_PIX_FMT_BGR4] = {
373         .nb_channels = 1,
374         .color_type = FF_COLOR_RGB,
375         .pixel_type = FF_PIXEL_PACKED,
376         .depth = 4,
377     },
378     [AV_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     [AV_PIX_FMT_NV12] = {
385         .nb_channels = 2,
386         .color_type = FF_COLOR_YUV,
387         .pixel_type = FF_PIXEL_PLANAR,
388         .depth = 8,
389     },
390     [AV_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     [AV_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     [AV_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 AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
412 {
413     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
414     *h_shift = desc->log2_chroma_w;
415     *v_shift = desc->log2_chroma_h;
416 }
417
418
419 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt,
420                              int has_alpha)
421 {
422     const PixFmtInfo *pf, *ps;
423     const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
424     const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
425     int loss;
426
427     ps = &pix_fmt_info[src_pix_fmt];
428
429     /* compute loss */
430     loss = 0;
431     pf = &pix_fmt_info[dst_pix_fmt];
432     if (pf->depth < ps->depth ||
433         ((dst_pix_fmt == AV_PIX_FMT_RGB555BE || dst_pix_fmt == AV_PIX_FMT_RGB555LE ||
434           dst_pix_fmt == AV_PIX_FMT_BGR555BE || dst_pix_fmt == AV_PIX_FMT_BGR555LE) &&
435          (src_pix_fmt == AV_PIX_FMT_RGB565BE || src_pix_fmt == AV_PIX_FMT_RGB565LE ||
436           src_pix_fmt == AV_PIX_FMT_BGR565BE || src_pix_fmt == AV_PIX_FMT_BGR565LE)))
437         loss |= FF_LOSS_DEPTH;
438     if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
439         dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
440         loss |= FF_LOSS_RESOLUTION;
441     switch(pf->color_type) {
442     case FF_COLOR_RGB:
443         if (ps->color_type != FF_COLOR_RGB &&
444             ps->color_type != FF_COLOR_GRAY)
445             loss |= FF_LOSS_COLORSPACE;
446         break;
447     case FF_COLOR_GRAY:
448         if (ps->color_type != FF_COLOR_GRAY)
449             loss |= FF_LOSS_COLORSPACE;
450         break;
451     case FF_COLOR_YUV:
452         if (ps->color_type != FF_COLOR_YUV)
453             loss |= FF_LOSS_COLORSPACE;
454         break;
455     case FF_COLOR_YUV_JPEG:
456         if (ps->color_type != FF_COLOR_YUV_JPEG &&
457             ps->color_type != FF_COLOR_YUV &&
458             ps->color_type != FF_COLOR_GRAY)
459             loss |= FF_LOSS_COLORSPACE;
460         break;
461     default:
462         /* fail safe test */
463         if (ps->color_type != pf->color_type)
464             loss |= FF_LOSS_COLORSPACE;
465         break;
466     }
467     if (pf->color_type == FF_COLOR_GRAY &&
468         ps->color_type != FF_COLOR_GRAY)
469         loss |= FF_LOSS_CHROMA;
470     if (!pf->is_alpha && (ps->is_alpha && has_alpha))
471         loss |= FF_LOSS_ALPHA;
472     if (pf->pixel_type == FF_PIXEL_PALETTE &&
473         (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
474         loss |= FF_LOSS_COLORQUANT;
475     return loss;
476 }
477
478 static int avg_bits_per_pixel(enum AVPixelFormat pix_fmt)
479 {
480     int bits;
481     const PixFmtInfo *pf;
482     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
483
484     pf = &pix_fmt_info[pix_fmt];
485     switch(pf->pixel_type) {
486     case FF_PIXEL_PACKED:
487         switch(pix_fmt) {
488         case AV_PIX_FMT_YUYV422:
489         case AV_PIX_FMT_UYVY422:
490         case AV_PIX_FMT_RGB565BE:
491         case AV_PIX_FMT_RGB565LE:
492         case AV_PIX_FMT_RGB555BE:
493         case AV_PIX_FMT_RGB555LE:
494         case AV_PIX_FMT_RGB444BE:
495         case AV_PIX_FMT_RGB444LE:
496         case AV_PIX_FMT_BGR565BE:
497         case AV_PIX_FMT_BGR565LE:
498         case AV_PIX_FMT_BGR555BE:
499         case AV_PIX_FMT_BGR555LE:
500         case AV_PIX_FMT_BGR444BE:
501         case AV_PIX_FMT_BGR444LE:
502             bits = 16;
503             break;
504         case AV_PIX_FMT_UYYVYY411:
505             bits = 12;
506             break;
507         default:
508             bits = pf->depth * pf->nb_channels;
509             break;
510         }
511         break;
512     case FF_PIXEL_PLANAR:
513         if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
514             bits = pf->depth * pf->nb_channels;
515         } else {
516             bits = pf->depth + ((2 * pf->depth) >>
517                                 (desc->log2_chroma_w + desc->log2_chroma_h));
518         }
519         break;
520     case FF_PIXEL_PALETTE:
521         bits = 8;
522         break;
523     default:
524         bits = -1;
525         break;
526     }
527     return bits;
528 }
529
530 static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
531                                       enum AVPixelFormat src_pix_fmt,
532                                       int has_alpha,
533                                       int loss_mask)
534 {
535     int dist, i, loss, min_dist;
536     enum AVPixelFormat dst_pix_fmt;
537
538     /* find exact color match with smallest size */
539     dst_pix_fmt = AV_PIX_FMT_NONE;
540     min_dist = 0x7fffffff;
541     i = 0;
542     while (pix_fmt_list[i] != AV_PIX_FMT_NONE) {
543         enum AVPixelFormat pix_fmt = pix_fmt_list[i];
544
545         if (i > AV_PIX_FMT_NB) {
546             av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
547                    "it is either not properly terminated or contains duplicates\n");
548             return AV_PIX_FMT_NONE;
549         }
550
551         loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
552         if (loss == 0) {
553             dist = avg_bits_per_pixel(pix_fmt);
554             if (dist < min_dist) {
555                 min_dist = dist;
556                 dst_pix_fmt = pix_fmt;
557             }
558         }
559         i++;
560     }
561     return dst_pix_fmt;
562 }
563
564 #if FF_API_FIND_BEST_PIX_FMT
565 enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
566                               int has_alpha, int *loss_ptr)
567 {
568     enum AVPixelFormat list[64];
569     int i, j = 0;
570
571     // test only the first 64 pixel formats to avoid undefined behaviour
572     for (i = 0; i < 64; i++) {
573         if (pix_fmt_mask & (1ULL << i))
574             list[j++] = i;
575     }
576     list[j] = AV_PIX_FMT_NONE;
577
578     return avcodec_find_best_pix_fmt2(list, src_pix_fmt, has_alpha, loss_ptr);
579 }
580 #endif /* FF_API_FIND_BEST_PIX_FMT */
581
582 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
583                                             enum AVPixelFormat src_pix_fmt,
584                                             int has_alpha, int *loss_ptr)
585 {
586     enum AVPixelFormat dst_pix_fmt;
587     int loss_mask, i;
588     static const int loss_mask_order[] = {
589         ~0, /* no loss first */
590         ~FF_LOSS_ALPHA,
591         ~FF_LOSS_RESOLUTION,
592         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
593         ~FF_LOSS_COLORQUANT,
594         ~FF_LOSS_DEPTH,
595         0,
596     };
597
598     /* try with successive loss */
599     i = 0;
600     for(;;) {
601         loss_mask = loss_mask_order[i++];
602         dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
603                                                  has_alpha, loss_mask);
604         if (dst_pix_fmt >= 0)
605             goto found;
606         if (loss_mask == 0)
607             break;
608     }
609     return AV_PIX_FMT_NONE;
610  found:
611     if (loss_ptr)
612         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
613     return dst_pix_fmt;
614 }
615
616 /* 2x2 -> 1x1 */
617 void ff_shrink22(uint8_t *dst, int dst_wrap,
618                      const uint8_t *src, int src_wrap,
619                      int width, int height)
620 {
621     int w;
622     const uint8_t *s1, *s2;
623     uint8_t *d;
624
625     for(;height > 0; height--) {
626         s1 = src;
627         s2 = s1 + src_wrap;
628         d = dst;
629         for(w = width;w >= 4; w-=4) {
630             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
631             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
632             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
633             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
634             s1 += 8;
635             s2 += 8;
636             d += 4;
637         }
638         for(;w > 0; w--) {
639             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
640             s1 += 2;
641             s2 += 2;
642             d++;
643         }
644         src += 2 * src_wrap;
645         dst += dst_wrap;
646     }
647 }
648
649 /* 4x4 -> 1x1 */
650 void ff_shrink44(uint8_t *dst, int dst_wrap,
651                      const uint8_t *src, int src_wrap,
652                      int width, int height)
653 {
654     int w;
655     const uint8_t *s1, *s2, *s3, *s4;
656     uint8_t *d;
657
658     for(;height > 0; height--) {
659         s1 = src;
660         s2 = s1 + src_wrap;
661         s3 = s2 + src_wrap;
662         s4 = s3 + src_wrap;
663         d = dst;
664         for(w = width;w > 0; w--) {
665             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
666                     s2[0] + s2[1] + s2[2] + s2[3] +
667                     s3[0] + s3[1] + s3[2] + s3[3] +
668                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
669             s1 += 4;
670             s2 += 4;
671             s3 += 4;
672             s4 += 4;
673             d++;
674         }
675         src += 4 * src_wrap;
676         dst += dst_wrap;
677     }
678 }
679
680 /* 8x8 -> 1x1 */
681 void ff_shrink88(uint8_t *dst, int dst_wrap,
682                      const uint8_t *src, int src_wrap,
683                      int width, int height)
684 {
685     int w, i;
686
687     for(;height > 0; height--) {
688         for(w = width;w > 0; w--) {
689             int tmp=0;
690             for(i=0; i<8; i++){
691                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
692                 src += src_wrap;
693             }
694             *(dst++) = (tmp + 32)>>6;
695             src += 8 - 8*src_wrap;
696         }
697         src += 8*src_wrap - 8*width;
698         dst += dst_wrap - width;
699     }
700 }
701
702 /* return true if yuv planar */
703 static inline int is_yuv_planar(const PixFmtInfo *ps)
704 {
705     return (ps->color_type == FF_COLOR_YUV ||
706             ps->color_type == FF_COLOR_YUV_JPEG) &&
707         ps->pixel_type == FF_PIXEL_PLANAR;
708 }
709
710 int av_picture_crop(AVPicture *dst, const AVPicture *src,
711                     enum AVPixelFormat pix_fmt, int top_band, int left_band)
712 {
713     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
714     int y_shift;
715     int x_shift;
716
717     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
718         return -1;
719
720     y_shift = desc->log2_chroma_h;
721     x_shift = desc->log2_chroma_w;
722
723     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
724     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
725     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
726
727     dst->linesize[0] = src->linesize[0];
728     dst->linesize[1] = src->linesize[1];
729     dst->linesize[2] = src->linesize[2];
730     return 0;
731 }
732
733 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
734                    enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
735             int *color)
736 {
737     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
738     uint8_t *optr;
739     int y_shift;
740     int x_shift;
741     int yheight;
742     int i, y;
743
744     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
745         !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
746
747     for (i = 0; i < 3; i++) {
748         x_shift = i ? desc->log2_chroma_w : 0;
749         y_shift = i ? desc->log2_chroma_h : 0;
750
751         if (padtop || padleft) {
752             memset(dst->data[i], color[i],
753                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
754         }
755
756         if (padleft || padright) {
757             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
758                 (dst->linesize[i] - (padright >> x_shift));
759             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
760             for (y = 0; y < yheight; y++) {
761                 memset(optr, color[i], (padleft + padright) >> x_shift);
762                 optr += dst->linesize[i];
763             }
764         }
765
766         if (src) { /* first line */
767             uint8_t *iptr = src->data[i];
768             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
769                     (padleft >> x_shift);
770             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
771             iptr += src->linesize[i];
772             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
773                 (dst->linesize[i] - (padright >> x_shift));
774             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
775             for (y = 0; y < yheight; y++) {
776                 memset(optr, color[i], (padleft + padright) >> x_shift);
777                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
778                        (width - padleft - padright) >> x_shift);
779                 iptr += src->linesize[i];
780                 optr += dst->linesize[i];
781             }
782         }
783
784         if (padbottom || padright) {
785             optr = dst->data[i] + dst->linesize[i] *
786                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
787             memset(optr, color[i],dst->linesize[i] *
788                 (padbottom >> y_shift) + (padright >> x_shift));
789         }
790     }
791     return 0;
792 }
793
794 #if !HAVE_MMX_EXTERNAL
795 /* filter parameters: [-1 4 2 4 -1] // 8 */
796 static void deinterlace_line_c(uint8_t *dst,
797                              const uint8_t *lum_m4, const uint8_t *lum_m3,
798                              const uint8_t *lum_m2, const uint8_t *lum_m1,
799                              const uint8_t *lum,
800                              int size)
801 {
802     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
803     int sum;
804
805     for(;size > 0;size--) {
806         sum = -lum_m4[0];
807         sum += lum_m3[0] << 2;
808         sum += lum_m2[0] << 1;
809         sum += lum_m1[0] << 2;
810         sum += -lum[0];
811         dst[0] = cm[(sum + 4) >> 3];
812         lum_m4++;
813         lum_m3++;
814         lum_m2++;
815         lum_m1++;
816         lum++;
817         dst++;
818     }
819 }
820
821 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
822                                        uint8_t *lum_m2, uint8_t *lum_m1,
823                                        uint8_t *lum, int size)
824 {
825     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
826     int sum;
827
828     for(;size > 0;size--) {
829         sum = -lum_m4[0];
830         sum += lum_m3[0] << 2;
831         sum += lum_m2[0] << 1;
832         lum_m4[0]=lum_m2[0];
833         sum += lum_m1[0] << 2;
834         sum += -lum[0];
835         lum_m2[0] = cm[(sum + 4) >> 3];
836         lum_m4++;
837         lum_m3++;
838         lum_m2++;
839         lum_m1++;
840         lum++;
841     }
842 }
843 #endif /* !HAVE_MMX_EXTERNAL */
844
845 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
846    top field is copied as is, but the bottom field is deinterlaced
847    against the top field. */
848 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
849                                     const uint8_t *src1, int src_wrap,
850                                     int width, int height)
851 {
852     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
853     int y;
854
855     src_m2 = src1;
856     src_m1 = src1;
857     src_0=&src_m1[src_wrap];
858     src_p1=&src_0[src_wrap];
859     src_p2=&src_p1[src_wrap];
860     for(y=0;y<(height-2);y+=2) {
861         memcpy(dst,src_m1,width);
862         dst += dst_wrap;
863         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
864         src_m2 = src_0;
865         src_m1 = src_p1;
866         src_0 = src_p2;
867         src_p1 += 2*src_wrap;
868         src_p2 += 2*src_wrap;
869         dst += dst_wrap;
870     }
871     memcpy(dst,src_m1,width);
872     dst += dst_wrap;
873     /* do last line */
874     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
875 }
876
877 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
878                                              int width, int height)
879 {
880     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
881     int y;
882     uint8_t *buf;
883     buf = av_malloc(width);
884
885     src_m1 = src1;
886     memcpy(buf,src_m1,width);
887     src_0=&src_m1[src_wrap];
888     src_p1=&src_0[src_wrap];
889     src_p2=&src_p1[src_wrap];
890     for(y=0;y<(height-2);y+=2) {
891         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
892         src_m1 = src_p1;
893         src_0 = src_p2;
894         src_p1 += 2*src_wrap;
895         src_p2 += 2*src_wrap;
896     }
897     /* do last line */
898     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
899     av_free(buf);
900 }
901
902 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
903                           enum AVPixelFormat pix_fmt, int width, int height)
904 {
905     int i;
906
907     if (pix_fmt != AV_PIX_FMT_YUV420P &&
908         pix_fmt != AV_PIX_FMT_YUVJ420P &&
909         pix_fmt != AV_PIX_FMT_YUV422P &&
910         pix_fmt != AV_PIX_FMT_YUVJ422P &&
911         pix_fmt != AV_PIX_FMT_YUV444P &&
912         pix_fmt != AV_PIX_FMT_YUV411P &&
913         pix_fmt != AV_PIX_FMT_GRAY8)
914         return -1;
915     if ((width & 3) != 0 || (height & 3) != 0)
916         return -1;
917
918     for(i=0;i<3;i++) {
919         if (i == 1) {
920             switch(pix_fmt) {
921             case AV_PIX_FMT_YUVJ420P:
922             case AV_PIX_FMT_YUV420P:
923                 width >>= 1;
924                 height >>= 1;
925                 break;
926             case AV_PIX_FMT_YUV422P:
927             case AV_PIX_FMT_YUVJ422P:
928                 width >>= 1;
929                 break;
930             case AV_PIX_FMT_YUV411P:
931                 width >>= 2;
932                 break;
933             default:
934                 break;
935             }
936             if (pix_fmt == AV_PIX_FMT_GRAY8) {
937                 break;
938             }
939         }
940         if (src == dst) {
941             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
942                                  width, height);
943         } else {
944             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
945                                         src->data[i], src->linesize[i],
946                                         width, height);
947         }
948     }
949     emms_c();
950     return 0;
951 }