]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgconvert.c
Remove deprecation of avcodec_get_chroma_sub_sample.
[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 "libavutil/avassert.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/common.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
41
42 #if HAVE_MMX_EXTERNAL
43 #include "x86/dsputil_mmx.h"
44 #endif
45
46 #define FF_COLOR_RGB      0 /**< RGB color space */
47 #define FF_COLOR_GRAY     1 /**< gray color space */
48 #define FF_COLOR_YUV      2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
49 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
50
51 #if HAVE_MMX_EXTERNAL
52 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
53 #define deinterlace_line         ff_deinterlace_line_mmx
54 #else
55 #define deinterlace_line_inplace deinterlace_line_inplace_c
56 #define deinterlace_line         deinterlace_line_c
57 #endif
58
59 #define pixdesc_has_alpha(pixdesc) \
60     ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & PIX_FMT_PAL)
61
62 typedef struct PixFmtInfo {
63     uint8_t color_type;      /**< color type (see FF_COLOR_xxx constants) */
64     uint8_t padded_size;     /**< padded size in bits if different from the non-padded size */
65 } PixFmtInfo;
66
67 /* this table gives more information about formats */
68 static const PixFmtInfo pix_fmt_info[AV_PIX_FMT_NB] = {
69     /* YUV formats */
70     [AV_PIX_FMT_YUV420P] = {
71         .color_type = FF_COLOR_YUV,
72     },
73     [AV_PIX_FMT_YUV422P] = {
74         .color_type = FF_COLOR_YUV,
75     },
76     [AV_PIX_FMT_YUV444P] = {
77         .color_type = FF_COLOR_YUV,
78     },
79     [AV_PIX_FMT_YUYV422] = {
80         .color_type = FF_COLOR_YUV,
81     },
82     [AV_PIX_FMT_UYVY422] = {
83         .color_type = FF_COLOR_YUV,
84     },
85     [AV_PIX_FMT_YUV410P] = {
86         .color_type = FF_COLOR_YUV,
87     },
88     [AV_PIX_FMT_YUV411P] = {
89         .color_type = FF_COLOR_YUV,
90     },
91     [AV_PIX_FMT_YUV440P] = {
92         .color_type = FF_COLOR_YUV,
93     },
94     [AV_PIX_FMT_YUV420P9LE] = {
95         .color_type = FF_COLOR_YUV,
96     },
97     [AV_PIX_FMT_YUV422P9LE] = {
98         .color_type = FF_COLOR_YUV,
99     },
100     [AV_PIX_FMT_YUV444P9LE] = {
101         .color_type = FF_COLOR_YUV,
102     },
103     [AV_PIX_FMT_YUV420P9BE] = {
104         .color_type = FF_COLOR_YUV,
105     },
106     [AV_PIX_FMT_YUV422P9BE] = {
107         .color_type = FF_COLOR_YUV,
108     },
109     [AV_PIX_FMT_YUV444P9BE] = {
110         .color_type = FF_COLOR_YUV,
111     },
112     [AV_PIX_FMT_YUV420P10LE] = {
113         .color_type = FF_COLOR_YUV,
114     },
115     [AV_PIX_FMT_YUV422P10LE] = {
116         .color_type = FF_COLOR_YUV,
117     },
118     [AV_PIX_FMT_YUV444P10LE] = {
119         .color_type = FF_COLOR_YUV,
120     },
121     [AV_PIX_FMT_YUV420P10BE] = {
122         .color_type = FF_COLOR_YUV,
123     },
124     [AV_PIX_FMT_YUV422P10BE] = {
125         .color_type = FF_COLOR_YUV,
126     },
127     [AV_PIX_FMT_YUV444P10BE] = {
128         .color_type = FF_COLOR_YUV,
129     },
130     [AV_PIX_FMT_YUV420P12LE] = {
131         .color_type = FF_COLOR_YUV,
132     },
133     [AV_PIX_FMT_YUV422P12LE] = {
134         .color_type = FF_COLOR_YUV,
135     },
136     [AV_PIX_FMT_YUV444P12LE] = {
137         .color_type = FF_COLOR_YUV,
138     },
139     [AV_PIX_FMT_YUV420P12BE] = {
140         .color_type = FF_COLOR_YUV,
141     },
142     [AV_PIX_FMT_YUV422P12BE] = {
143         .color_type = FF_COLOR_YUV,
144     },
145     [AV_PIX_FMT_YUV444P12BE] = {
146         .color_type = FF_COLOR_YUV,
147     },
148     [AV_PIX_FMT_YUV420P14LE] = {
149         .color_type = FF_COLOR_YUV,
150     },
151     [AV_PIX_FMT_YUV422P14LE] = {
152         .color_type = FF_COLOR_YUV,
153     },
154     [AV_PIX_FMT_YUV444P14LE] = {
155         .color_type = FF_COLOR_YUV,
156     },
157     [AV_PIX_FMT_YUV420P14BE] = {
158         .color_type = FF_COLOR_YUV,
159     },
160     [AV_PIX_FMT_YUV422P14BE] = {
161         .color_type = FF_COLOR_YUV,
162     },
163     [AV_PIX_FMT_YUV444P14BE] = {
164         .color_type = FF_COLOR_YUV,
165     },
166     [AV_PIX_FMT_YUV420P16LE] = {
167         .color_type = FF_COLOR_YUV,
168     },
169     [AV_PIX_FMT_YUV422P16LE] = {
170         .color_type = FF_COLOR_YUV,
171     },
172     [AV_PIX_FMT_YUV444P16LE] = {
173         .color_type = FF_COLOR_YUV,
174     },
175     [AV_PIX_FMT_YUV420P16BE] = {
176         .color_type = FF_COLOR_YUV,
177     },
178     [AV_PIX_FMT_YUV422P16BE] = {
179         .color_type = FF_COLOR_YUV,
180     },
181     [AV_PIX_FMT_YUV444P16BE] = {
182         .color_type = FF_COLOR_YUV,
183     },
184
185     /* YUV formats with alpha plane */
186     [AV_PIX_FMT_YUVA420P] = {
187         .color_type = FF_COLOR_YUV,
188     },
189
190     [AV_PIX_FMT_YUVA422P] = {
191         .color_type = FF_COLOR_YUV,
192     },
193
194     [AV_PIX_FMT_YUVA444P] = {
195         .color_type = FF_COLOR_YUV,
196     },
197     [AV_PIX_FMT_YUVA420P9LE] = {
198         .color_type = FF_COLOR_YUV,
199     },
200     [AV_PIX_FMT_YUVA422P9LE] = {
201         .color_type = FF_COLOR_YUV,
202     },
203     [AV_PIX_FMT_YUVA444P9LE] = {
204         .color_type = FF_COLOR_YUV,
205     },
206     [AV_PIX_FMT_YUVA420P9BE] = {
207         .color_type = FF_COLOR_YUV,
208     },
209     [AV_PIX_FMT_YUVA422P9BE] = {
210         .color_type = FF_COLOR_YUV,
211     },
212     [AV_PIX_FMT_YUVA444P9BE] = {
213         .color_type = FF_COLOR_YUV,
214     },
215     [AV_PIX_FMT_YUVA420P10LE] = {
216         .color_type = FF_COLOR_YUV,
217     },
218     [AV_PIX_FMT_YUVA422P10LE] = {
219         .color_type = FF_COLOR_YUV,
220     },
221     [AV_PIX_FMT_YUVA444P10LE] = {
222         .color_type = FF_COLOR_YUV,
223     },
224     [AV_PIX_FMT_YUVA420P10BE] = {
225         .color_type = FF_COLOR_YUV,
226     },
227     [AV_PIX_FMT_YUVA422P10BE] = {
228         .color_type = FF_COLOR_YUV,
229     },
230     [AV_PIX_FMT_YUVA444P10BE] = {
231         .color_type = FF_COLOR_YUV,
232     },
233
234     /* JPEG YUV */
235     [AV_PIX_FMT_YUVJ420P] = {
236         .color_type = FF_COLOR_YUV_JPEG,
237     },
238     [AV_PIX_FMT_YUVJ422P] = {
239         .color_type = FF_COLOR_YUV_JPEG,
240     },
241     [AV_PIX_FMT_YUVJ444P] = {
242         .color_type = FF_COLOR_YUV_JPEG,
243     },
244     [AV_PIX_FMT_YUVJ440P] = {
245         .color_type = FF_COLOR_YUV_JPEG,
246     },
247
248     /* RGB formats */
249     [AV_PIX_FMT_RGB24] = {
250         .color_type = FF_COLOR_RGB,
251     },
252     [AV_PIX_FMT_BGR24] = {
253         .color_type = FF_COLOR_RGB,
254     },
255     [AV_PIX_FMT_ARGB] = {
256         .color_type = FF_COLOR_RGB,
257     },
258     [AV_PIX_FMT_RGB48BE] = {
259         .color_type = FF_COLOR_RGB,
260     },
261     [AV_PIX_FMT_RGB48LE] = {
262         .color_type = FF_COLOR_RGB,
263     },
264     [AV_PIX_FMT_RGBA64BE] = {
265         .color_type = FF_COLOR_RGB,
266     },
267     [AV_PIX_FMT_RGBA64LE] = {
268         .color_type = FF_COLOR_RGB,
269     },
270     [AV_PIX_FMT_RGB565BE] = {
271         .color_type = FF_COLOR_RGB,
272     },
273     [AV_PIX_FMT_RGB565LE] = {
274         .color_type = FF_COLOR_RGB,
275     },
276     [AV_PIX_FMT_RGB555BE] = {
277         .color_type = FF_COLOR_RGB,
278         .padded_size = 16,
279     },
280     [AV_PIX_FMT_RGB555LE] = {
281         .color_type = FF_COLOR_RGB,
282         .padded_size = 16,
283     },
284     [AV_PIX_FMT_RGB444BE] = {
285         .color_type = FF_COLOR_RGB,
286         .padded_size = 16,
287     },
288     [AV_PIX_FMT_RGB444LE] = {
289         .color_type = FF_COLOR_RGB,
290         .padded_size = 16,
291     },
292
293     /* gray / mono formats */
294     [AV_PIX_FMT_GRAY16BE] = {
295         .color_type = FF_COLOR_GRAY,
296     },
297     [AV_PIX_FMT_GRAY16LE] = {
298         .color_type = FF_COLOR_GRAY,
299     },
300     [AV_PIX_FMT_GRAY8] = {
301         .color_type = FF_COLOR_GRAY,
302     },
303     [AV_PIX_FMT_GRAY8A] = {
304         .color_type = FF_COLOR_GRAY,
305     },
306     [AV_PIX_FMT_MONOWHITE] = {
307         .color_type = FF_COLOR_GRAY,
308     },
309     [AV_PIX_FMT_MONOBLACK] = {
310         .color_type = FF_COLOR_GRAY,
311     },
312
313     /* paletted formats */
314     [AV_PIX_FMT_PAL8] = {
315         .color_type = FF_COLOR_RGB,
316     },
317     [AV_PIX_FMT_UYYVYY411] = {
318         .color_type = FF_COLOR_YUV,
319     },
320     [AV_PIX_FMT_ABGR] = {
321         .color_type = FF_COLOR_RGB,
322     },
323     [AV_PIX_FMT_BGR48BE] = {
324         .color_type = FF_COLOR_RGB,
325     },
326     [AV_PIX_FMT_BGR48LE] = {
327         .color_type = FF_COLOR_RGB,
328     },
329     [AV_PIX_FMT_BGRA64BE] = {
330         .color_type = FF_COLOR_RGB,
331     },
332     [AV_PIX_FMT_BGRA64LE] = {
333         .color_type = FF_COLOR_RGB,
334     },
335     [AV_PIX_FMT_BGR565BE] = {
336         .color_type = FF_COLOR_RGB,
337         .padded_size = 16,
338     },
339     [AV_PIX_FMT_BGR565LE] = {
340         .color_type = FF_COLOR_RGB,
341         .padded_size = 16,
342     },
343     [AV_PIX_FMT_BGR555BE] = {
344         .color_type = FF_COLOR_RGB,
345         .padded_size = 16,
346     },
347     [AV_PIX_FMT_BGR555LE] = {
348         .color_type = FF_COLOR_RGB,
349         .padded_size = 16,
350     },
351     [AV_PIX_FMT_BGR444BE] = {
352         .color_type = FF_COLOR_RGB,
353         .padded_size = 16,
354     },
355     [AV_PIX_FMT_BGR444LE] = {
356         .color_type = FF_COLOR_RGB,
357         .padded_size = 16,
358     },
359     [AV_PIX_FMT_RGB8] = {
360         .color_type = FF_COLOR_RGB,
361     },
362     [AV_PIX_FMT_RGB4] = {
363         .color_type = FF_COLOR_RGB,
364     },
365     [AV_PIX_FMT_RGB4_BYTE] = {
366         .color_type = FF_COLOR_RGB,
367         .padded_size = 8,
368     },
369     [AV_PIX_FMT_BGR8] = {
370         .color_type = FF_COLOR_RGB,
371     },
372     [AV_PIX_FMT_BGR4] = {
373         .color_type = FF_COLOR_RGB,
374     },
375     [AV_PIX_FMT_BGR4_BYTE] = {
376         .color_type = FF_COLOR_RGB,
377         .padded_size = 8,
378     },
379     [AV_PIX_FMT_NV12] = {
380         .color_type = FF_COLOR_YUV,
381     },
382     [AV_PIX_FMT_NV21] = {
383         .color_type = FF_COLOR_YUV,
384     },
385
386     [AV_PIX_FMT_BGRA] = {
387         .color_type = FF_COLOR_RGB,
388     },
389     [AV_PIX_FMT_RGBA] = {
390         .color_type = FF_COLOR_RGB,
391     },
392
393     [AV_PIX_FMT_GBRP] = {
394         .color_type = FF_COLOR_RGB,
395     },
396     [AV_PIX_FMT_GBRP9BE] = {
397         .color_type = FF_COLOR_RGB,
398     },
399     [AV_PIX_FMT_GBRP9LE] = {
400         .color_type = FF_COLOR_RGB,
401     },
402     [AV_PIX_FMT_GBRP10BE] = {
403         .color_type = FF_COLOR_RGB,
404     },
405     [AV_PIX_FMT_GBRP10LE] = {
406         .color_type = FF_COLOR_RGB,
407     },
408     [AV_PIX_FMT_GBRP12BE] = {
409         .color_type = FF_COLOR_RGB,
410     },
411     [AV_PIX_FMT_GBRP12LE] = {
412         .color_type = FF_COLOR_RGB,
413     },
414     [AV_PIX_FMT_GBRP14BE] = {
415         .color_type = FF_COLOR_RGB,
416     },
417     [AV_PIX_FMT_GBRP14LE] = {
418         .color_type = FF_COLOR_RGB,
419     },
420     [AV_PIX_FMT_GBRP16BE] = {
421         .color_type = FF_COLOR_RGB,
422     },
423     [AV_PIX_FMT_GBRP16LE] = {
424         .color_type = FF_COLOR_RGB,
425     },
426 };
427
428 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
429 {
430     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
431     av_assert0(desc);
432     *h_shift = desc->log2_chroma_w;
433     *v_shift = desc->log2_chroma_h;
434 }
435
436 int avpicture_fill(AVPicture *picture, const uint8_t *ptr,
437                    enum AVPixelFormat pix_fmt, int width, int height)
438 {
439     return av_image_fill_arrays(picture->data, picture->linesize,
440                                 ptr, pix_fmt, width, height, 1);
441 }
442
443 int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt, int width, int height,
444                      unsigned char *dest, int dest_size)
445 {
446     return av_image_copy_to_buffer(dest, dest_size,
447                                    (const uint8_t * const*)src->data, src->linesize,
448                                    pix_fmt, width, height, 1);
449 }
450
451 int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
452 {
453     return av_image_get_buffer_size(pix_fmt, width, height, 1);
454 }
455
456 static int get_pix_fmt_depth(int *min, int *max, enum AVPixelFormat pix_fmt)
457 {
458     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
459     int i;
460
461     if (!desc || !desc->nb_components) {
462         *min = *max = 0;
463         return AVERROR(EINVAL);
464     }
465
466     *min = INT_MAX, *max = -INT_MAX;
467     for (i = 0; i < desc->nb_components; i++) {
468         *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
469         *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
470     }
471     return 0;
472 }
473
474 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt,
475                              int has_alpha)
476 {
477     const PixFmtInfo *pf, *ps;
478     const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
479     const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
480     int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
481     int ret, loss;
482
483     if (dst_pix_fmt >= AV_PIX_FMT_NB || dst_pix_fmt <= AV_PIX_FMT_NONE)
484         return ~0;
485
486     ps = &pix_fmt_info[src_pix_fmt];
487
488     /* compute loss */
489     loss = 0;
490
491     if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
492         return ret;
493     if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
494         return ret;
495     if (dst_min_depth < src_min_depth ||
496         dst_max_depth < src_max_depth)
497         loss |= FF_LOSS_DEPTH;
498     if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
499         dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
500         loss |= FF_LOSS_RESOLUTION;
501
502     pf = &pix_fmt_info[dst_pix_fmt];
503     switch(pf->color_type) {
504     case FF_COLOR_RGB:
505         if (ps->color_type != FF_COLOR_RGB &&
506             ps->color_type != FF_COLOR_GRAY)
507             loss |= FF_LOSS_COLORSPACE;
508         break;
509     case FF_COLOR_GRAY:
510         if (ps->color_type != FF_COLOR_GRAY)
511             loss |= FF_LOSS_COLORSPACE;
512         break;
513     case FF_COLOR_YUV:
514         if (ps->color_type != FF_COLOR_YUV)
515             loss |= FF_LOSS_COLORSPACE;
516         break;
517     case FF_COLOR_YUV_JPEG:
518         if (ps->color_type != FF_COLOR_YUV_JPEG &&
519             ps->color_type != FF_COLOR_YUV &&
520             ps->color_type != FF_COLOR_GRAY)
521             loss |= FF_LOSS_COLORSPACE;
522         break;
523     default:
524         /* fail safe test */
525         if (ps->color_type != pf->color_type)
526             loss |= FF_LOSS_COLORSPACE;
527         break;
528     }
529     if (pf->color_type == FF_COLOR_GRAY &&
530         ps->color_type != FF_COLOR_GRAY)
531         loss |= FF_LOSS_CHROMA;
532     if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
533         loss |= FF_LOSS_ALPHA;
534     if (dst_pix_fmt == AV_PIX_FMT_PAL8 &&
535         (src_pix_fmt != AV_PIX_FMT_PAL8 && (ps->color_type != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
536         loss |= FF_LOSS_COLORQUANT;
537
538     return loss;
539 }
540
541 static int avg_bits_per_pixel(enum AVPixelFormat pix_fmt)
542 {
543     const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
544     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
545
546     return info->padded_size ?
547         info->padded_size : av_get_bits_per_pixel(desc);
548 }
549
550 #if FF_API_FIND_BEST_PIX_FMT
551 enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
552                                             int has_alpha, int *loss_ptr)
553 {
554     enum AVPixelFormat dst_pix_fmt;
555     int i;
556
557     if (loss_ptr) /* all losses count (for backward compatibility) */
558         *loss_ptr = 0;
559
560     dst_pix_fmt = AV_PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
561     for(i = 0; i< FFMIN(AV_PIX_FMT_NB, 64); i++){
562         if (pix_fmt_mask & (1ULL << i))
563             dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
564     }
565     return dst_pix_fmt;
566 }
567 #endif /* FF_API_FIND_BEST_PIX_FMT */
568
569 enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
570                                             enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
571 {
572     enum AVPixelFormat dst_pix_fmt;
573     int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
574     static const int loss_mask_order[] = {
575         ~0, /* no loss first */
576         ~FF_LOSS_ALPHA,
577         ~FF_LOSS_RESOLUTION,
578         ~FF_LOSS_COLORSPACE,
579         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
580         ~FF_LOSS_COLORQUANT,
581         ~FF_LOSS_DEPTH,
582         ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
583         ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
584           FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
585         0x80000, //non zero entry that combines all loss variants including future additions
586         0,
587     };
588
589     loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
590     dst_pix_fmt = AV_PIX_FMT_NONE;
591     loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
592     loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
593
594     /* try with successive loss */
595     for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == AV_PIX_FMT_NONE;i++) {
596         loss_order1 = loss1 & loss_mask_order[i];
597         loss_order2 = loss2 & loss_mask_order[i];
598
599         if (loss_order1 == 0 && loss_order2 == 0 && dst_pix_fmt2 != AV_PIX_FMT_NONE && dst_pix_fmt1 != AV_PIX_FMT_NONE){ /* use format with smallest depth */
600             dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
601         } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
602             dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
603         }
604     }
605
606     if (loss_ptr)
607         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
608     return dst_pix_fmt;
609 }
610
611 #if AV_HAVE_INCOMPATIBLE_FORK_ABI
612 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
613                                             enum AVPixelFormat src_pix_fmt,
614                                             int has_alpha, int *loss_ptr){
615     return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
616 }
617 #else
618 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
619                                             enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
620 {
621     return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
622 }
623 #endif
624
625 enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(enum AVPixelFormat *pix_fmt_list,
626                                             enum AVPixelFormat src_pix_fmt,
627                                             int has_alpha, int *loss_ptr){
628     int i;
629
630     enum AVPixelFormat best = AV_PIX_FMT_NONE;
631
632     for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
633         best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
634
635     return best;
636 }
637
638 void av_picture_copy(AVPicture *dst, const AVPicture *src,
639                      enum AVPixelFormat pix_fmt, int width, int height)
640 {
641     av_image_copy(dst->data, dst->linesize, (const uint8_t **)src->data,
642                   src->linesize, pix_fmt, width, height);
643 }
644
645 /* 2x2 -> 1x1 */
646 void ff_shrink22(uint8_t *dst, int dst_wrap,
647                      const uint8_t *src, int src_wrap,
648                      int width, int height)
649 {
650     int w;
651     const uint8_t *s1, *s2;
652     uint8_t *d;
653
654     for(;height > 0; height--) {
655         s1 = src;
656         s2 = s1 + src_wrap;
657         d = dst;
658         for(w = width;w >= 4; w-=4) {
659             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
660             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
661             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
662             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
663             s1 += 8;
664             s2 += 8;
665             d += 4;
666         }
667         for(;w > 0; w--) {
668             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
669             s1 += 2;
670             s2 += 2;
671             d++;
672         }
673         src += 2 * src_wrap;
674         dst += dst_wrap;
675     }
676 }
677
678 /* 4x4 -> 1x1 */
679 void ff_shrink44(uint8_t *dst, int dst_wrap,
680                      const uint8_t *src, int src_wrap,
681                      int width, int height)
682 {
683     int w;
684     const uint8_t *s1, *s2, *s3, *s4;
685     uint8_t *d;
686
687     for(;height > 0; height--) {
688         s1 = src;
689         s2 = s1 + src_wrap;
690         s3 = s2 + src_wrap;
691         s4 = s3 + src_wrap;
692         d = dst;
693         for(w = width;w > 0; w--) {
694             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
695                     s2[0] + s2[1] + s2[2] + s2[3] +
696                     s3[0] + s3[1] + s3[2] + s3[3] +
697                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
698             s1 += 4;
699             s2 += 4;
700             s3 += 4;
701             s4 += 4;
702             d++;
703         }
704         src += 4 * src_wrap;
705         dst += dst_wrap;
706     }
707 }
708
709 /* 8x8 -> 1x1 */
710 void ff_shrink88(uint8_t *dst, int dst_wrap,
711                      const uint8_t *src, int src_wrap,
712                      int width, int height)
713 {
714     int w, i;
715
716     for(;height > 0; height--) {
717         for(w = width;w > 0; w--) {
718             int tmp=0;
719             for(i=0; i<8; i++){
720                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
721                 src += src_wrap;
722             }
723             *(dst++) = (tmp + 32)>>6;
724             src += 8 - 8*src_wrap;
725         }
726         src += 8*src_wrap - 8*width;
727         dst += dst_wrap - width;
728     }
729 }
730
731
732 int avpicture_alloc(AVPicture *picture,
733                     enum AVPixelFormat pix_fmt, int width, int height)
734 {
735     int ret;
736
737     if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
738         memset(picture, 0, sizeof(AVPicture));
739         return ret;
740     }
741
742     return 0;
743 }
744
745 void avpicture_free(AVPicture *picture)
746 {
747     av_free(picture->data[0]);
748 }
749
750 /* return true if yuv planar */
751 static inline int is_yuv_planar(enum AVPixelFormat fmt)
752 {
753     const PixFmtInfo         *info = &pix_fmt_info[fmt];
754     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
755     int i;
756     int planes[4] = { 0 };
757
758     if (info->color_type != FF_COLOR_YUV &&
759         info->color_type != FF_COLOR_YUV_JPEG)
760         return 0;
761
762     /* set the used planes */
763     for (i = 0; i < desc->nb_components; i++)
764         planes[desc->comp[i].plane] = 1;
765
766     /* if there is an unused plane, the format is not planar */
767     for (i = 0; i < desc->nb_components; i++)
768         if (!planes[i])
769             return 0;
770     return 1;
771 }
772
773 int av_picture_crop(AVPicture *dst, const AVPicture *src,
774                     enum AVPixelFormat pix_fmt, int top_band, int left_band)
775 {
776     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
777     int y_shift;
778     int x_shift;
779
780     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
781         return -1;
782
783     y_shift = desc->log2_chroma_h;
784     x_shift = desc->log2_chroma_w;
785
786     if (is_yuv_planar(pix_fmt)) {
787     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
788     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
789     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
790     } else{
791         if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
792             return -1;
793         if(left_band) //FIXME add support for this too
794             return -1;
795         dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
796     }
797
798     dst->linesize[0] = src->linesize[0];
799     dst->linesize[1] = src->linesize[1];
800     dst->linesize[2] = src->linesize[2];
801     return 0;
802 }
803
804 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
805                    enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
806             int *color)
807 {
808     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
809     uint8_t *optr;
810     int y_shift;
811     int x_shift;
812     int yheight;
813     int i, y;
814
815     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
816         !is_yuv_planar(pix_fmt)) return -1;
817
818     for (i = 0; i < 3; i++) {
819         x_shift = i ? desc->log2_chroma_w : 0;
820         y_shift = i ? desc->log2_chroma_h : 0;
821
822         if (padtop || padleft) {
823             memset(dst->data[i], color[i],
824                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
825         }
826
827         if (padleft || padright) {
828             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
829                 (dst->linesize[i] - (padright >> x_shift));
830             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
831             for (y = 0; y < yheight; y++) {
832                 memset(optr, color[i], (padleft + padright) >> x_shift);
833                 optr += dst->linesize[i];
834             }
835         }
836
837         if (src) { /* first line */
838             uint8_t *iptr = src->data[i];
839             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
840                     (padleft >> x_shift);
841             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
842             iptr += src->linesize[i];
843             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
844                 (dst->linesize[i] - (padright >> x_shift));
845             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
846             for (y = 0; y < yheight; y++) {
847                 memset(optr, color[i], (padleft + padright) >> x_shift);
848                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
849                        (width - padleft - padright) >> x_shift);
850                 iptr += src->linesize[i];
851                 optr += dst->linesize[i];
852             }
853         }
854
855         if (padbottom || padright) {
856             optr = dst->data[i] + dst->linesize[i] *
857                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
858             memset(optr, color[i],dst->linesize[i] *
859                 (padbottom >> y_shift) + (padright >> x_shift));
860         }
861     }
862     return 0;
863 }
864
865 #if !HAVE_MMX_EXTERNAL
866 /* filter parameters: [-1 4 2 4 -1] // 8 */
867 static void deinterlace_line_c(uint8_t *dst,
868                              const uint8_t *lum_m4, const uint8_t *lum_m3,
869                              const uint8_t *lum_m2, const uint8_t *lum_m1,
870                              const uint8_t *lum,
871                              int size)
872 {
873     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
874     int sum;
875
876     for(;size > 0;size--) {
877         sum = -lum_m4[0];
878         sum += lum_m3[0] << 2;
879         sum += lum_m2[0] << 1;
880         sum += lum_m1[0] << 2;
881         sum += -lum[0];
882         dst[0] = cm[(sum + 4) >> 3];
883         lum_m4++;
884         lum_m3++;
885         lum_m2++;
886         lum_m1++;
887         lum++;
888         dst++;
889     }
890 }
891
892 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
893                                        uint8_t *lum_m2, uint8_t *lum_m1,
894                                        uint8_t *lum, int size)
895 {
896     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
897     int sum;
898
899     for(;size > 0;size--) {
900         sum = -lum_m4[0];
901         sum += lum_m3[0] << 2;
902         sum += lum_m2[0] << 1;
903         lum_m4[0]=lum_m2[0];
904         sum += lum_m1[0] << 2;
905         sum += -lum[0];
906         lum_m2[0] = cm[(sum + 4) >> 3];
907         lum_m4++;
908         lum_m3++;
909         lum_m2++;
910         lum_m1++;
911         lum++;
912     }
913 }
914 #endif /* !HAVE_MMX_EXTERNAL */
915
916 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
917    top field is copied as is, but the bottom field is deinterlaced
918    against the top field. */
919 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
920                                     const uint8_t *src1, int src_wrap,
921                                     int width, int height)
922 {
923     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
924     int y;
925
926     src_m2 = src1;
927     src_m1 = src1;
928     src_0=&src_m1[src_wrap];
929     src_p1=&src_0[src_wrap];
930     src_p2=&src_p1[src_wrap];
931     for(y=0;y<(height-2);y+=2) {
932         memcpy(dst,src_m1,width);
933         dst += dst_wrap;
934         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
935         src_m2 = src_0;
936         src_m1 = src_p1;
937         src_0 = src_p2;
938         src_p1 += 2*src_wrap;
939         src_p2 += 2*src_wrap;
940         dst += dst_wrap;
941     }
942     memcpy(dst,src_m1,width);
943     dst += dst_wrap;
944     /* do last line */
945     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
946 }
947
948 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
949                                              int width, int height)
950 {
951     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
952     int y;
953     uint8_t *buf;
954     buf = av_malloc(width);
955
956     src_m1 = src1;
957     memcpy(buf,src_m1,width);
958     src_0=&src_m1[src_wrap];
959     src_p1=&src_0[src_wrap];
960     src_p2=&src_p1[src_wrap];
961     for(y=0;y<(height-2);y+=2) {
962         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
963         src_m1 = src_p1;
964         src_0 = src_p2;
965         src_p1 += 2*src_wrap;
966         src_p2 += 2*src_wrap;
967     }
968     /* do last line */
969     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
970     av_free(buf);
971 }
972
973 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
974                           enum AVPixelFormat pix_fmt, int width, int height)
975 {
976     int i;
977
978     if (pix_fmt != AV_PIX_FMT_YUV420P &&
979         pix_fmt != AV_PIX_FMT_YUVJ420P &&
980         pix_fmt != AV_PIX_FMT_YUV422P &&
981         pix_fmt != AV_PIX_FMT_YUVJ422P &&
982         pix_fmt != AV_PIX_FMT_YUV444P &&
983         pix_fmt != AV_PIX_FMT_YUV411P &&
984         pix_fmt != AV_PIX_FMT_GRAY8)
985         return -1;
986     if ((width & 3) != 0 || (height & 3) != 0)
987         return -1;
988
989     for(i=0;i<3;i++) {
990         if (i == 1) {
991             switch(pix_fmt) {
992             case AV_PIX_FMT_YUVJ420P:
993             case AV_PIX_FMT_YUV420P:
994                 width >>= 1;
995                 height >>= 1;
996                 break;
997             case AV_PIX_FMT_YUV422P:
998             case AV_PIX_FMT_YUVJ422P:
999                 width >>= 1;
1000                 break;
1001             case AV_PIX_FMT_YUV411P:
1002                 width >>= 2;
1003                 break;
1004             default:
1005                 break;
1006             }
1007             if (pix_fmt == AV_PIX_FMT_GRAY8) {
1008                 break;
1009             }
1010         }
1011         if (src == dst) {
1012             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1013                                  width, height);
1014         } else {
1015             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1016                                         src->data[i], src->linesize[i],
1017                                         width, height);
1018         }
1019     }
1020     emms_c();
1021     return 0;
1022 }