]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgconvert.c
Removes the internal avcodec_find_best_pix_fmt1()function previously used by avcodec_...
[ffmpeg] / libavcodec / imgconvert.c
1 /*
2  * Misc image conversion routines
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * misc image conversion routines
25  */
26
27 /* TODO:
28  * - write 'ffimg' program to test all the image related stuff
29  * - move all api to slice based system
30  * - integrate deinterlacing, postprocessing and scaling in the conversion process
31  */
32
33 #include "avcodec.h"
34 #include "dsputil.h"
35 #include "internal.h"
36 #include "imgconvert.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/pixdesc.h"
39 #include "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 #if HAVE_MMX && HAVE_YASM
51 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
52 #define deinterlace_line         ff_deinterlace_line_mmx
53 #else
54 #define deinterlace_line_inplace deinterlace_line_inplace_c
55 #define deinterlace_line         deinterlace_line_c
56 #endif
57
58 typedef struct PixFmtInfo {
59     uint8_t color_type;      /**< color type (see FF_COLOR_xxx constants) */
60     uint8_t is_alpha : 1;    /**< true if alpha can be specified */
61     uint8_t padded_size;     /**< padded size in bits if different from the non-padded size */
62 } PixFmtInfo;
63
64 /* this table gives more information about formats */
65 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
66     /* YUV formats */
67     [PIX_FMT_YUV420P] = {
68         .color_type = FF_COLOR_YUV,
69     },
70     [PIX_FMT_YUV422P] = {
71         .color_type = FF_COLOR_YUV,
72     },
73     [PIX_FMT_YUV444P] = {
74         .color_type = FF_COLOR_YUV,
75     },
76     [PIX_FMT_YUYV422] = {
77         .color_type = FF_COLOR_YUV,
78     },
79     [PIX_FMT_UYVY422] = {
80         .color_type = FF_COLOR_YUV,
81     },
82     [PIX_FMT_YUV410P] = {
83         .color_type = FF_COLOR_YUV,
84     },
85     [PIX_FMT_YUV411P] = {
86         .color_type = FF_COLOR_YUV,
87     },
88     [PIX_FMT_YUV440P] = {
89         .color_type = FF_COLOR_YUV,
90     },
91     [PIX_FMT_YUV420P16LE] = {
92         .color_type = FF_COLOR_YUV,
93     },
94     [PIX_FMT_YUV422P16LE] = {
95         .color_type = FF_COLOR_YUV,
96     },
97     [PIX_FMT_YUV444P16LE] = {
98         .color_type = FF_COLOR_YUV,
99     },
100     [PIX_FMT_YUV420P16BE] = {
101         .color_type = FF_COLOR_YUV,
102     },
103     [PIX_FMT_YUV422P16BE] = {
104         .color_type = FF_COLOR_YUV,
105     },
106     [PIX_FMT_YUV444P16BE] = {
107         .color_type = FF_COLOR_YUV,
108     },
109
110     /* YUV formats with alpha plane */
111     [PIX_FMT_YUVA420P] = {
112         .color_type = FF_COLOR_YUV,
113     },
114
115     /* JPEG YUV */
116     [PIX_FMT_YUVJ420P] = {
117         .color_type = FF_COLOR_YUV_JPEG,
118     },
119     [PIX_FMT_YUVJ422P] = {
120         .color_type = FF_COLOR_YUV_JPEG,
121     },
122     [PIX_FMT_YUVJ444P] = {
123         .color_type = FF_COLOR_YUV_JPEG,
124     },
125     [PIX_FMT_YUVJ440P] = {
126         .color_type = FF_COLOR_YUV_JPEG,
127     },
128
129     /* RGB formats */
130     [PIX_FMT_RGB24] = {
131         .color_type = FF_COLOR_RGB,
132     },
133     [PIX_FMT_BGR24] = {
134         .color_type = FF_COLOR_RGB,
135     },
136     [PIX_FMT_ARGB] = {
137         .is_alpha = 1,
138         .color_type = FF_COLOR_RGB,
139     },
140     [PIX_FMT_RGB48BE] = {
141         .color_type = FF_COLOR_RGB,
142     },
143     [PIX_FMT_RGB48LE] = {
144         .color_type = FF_COLOR_RGB,
145     },
146     [PIX_FMT_RGB565BE] = {
147         .color_type = FF_COLOR_RGB,
148     },
149     [PIX_FMT_RGB565LE] = {
150         .color_type = FF_COLOR_RGB,
151     },
152     [PIX_FMT_RGB555BE] = {
153         .color_type = FF_COLOR_RGB,
154         .padded_size = 16,
155     },
156     [PIX_FMT_RGB555LE] = {
157         .color_type = FF_COLOR_RGB,
158         .padded_size = 16,
159     },
160     [PIX_FMT_RGB444BE] = {
161         .color_type = FF_COLOR_RGB,
162         .padded_size = 16,
163     },
164     [PIX_FMT_RGB444LE] = {
165         .color_type = FF_COLOR_RGB,
166         .padded_size = 16,
167     },
168
169     /* gray / mono formats */
170     [PIX_FMT_GRAY16BE] = {
171         .color_type = FF_COLOR_GRAY,
172     },
173     [PIX_FMT_GRAY16LE] = {
174         .color_type = FF_COLOR_GRAY,
175     },
176     [PIX_FMT_GRAY8] = {
177         .color_type = FF_COLOR_GRAY,
178     },
179     [PIX_FMT_MONOWHITE] = {
180         .color_type = FF_COLOR_GRAY,
181     },
182     [PIX_FMT_MONOBLACK] = {
183         .color_type = FF_COLOR_GRAY,
184     },
185
186     /* paletted formats */
187     [PIX_FMT_PAL8] = {
188         .is_alpha = 1,
189         .color_type = FF_COLOR_RGB,
190     },
191     [PIX_FMT_UYYVYY411] = {
192         .color_type = FF_COLOR_YUV,
193     },
194     [PIX_FMT_ABGR] = {
195         .is_alpha = 1,
196         .color_type = FF_COLOR_RGB,
197     },
198     [PIX_FMT_BGR565BE] = {
199         .color_type = FF_COLOR_RGB,
200         .padded_size = 16,
201     },
202     [PIX_FMT_BGR565LE] = {
203         .color_type = FF_COLOR_RGB,
204         .padded_size = 16,
205     },
206     [PIX_FMT_BGR555BE] = {
207         .color_type = FF_COLOR_RGB,
208         .padded_size = 16,
209     },
210     [PIX_FMT_BGR555LE] = {
211         .color_type = FF_COLOR_RGB,
212         .padded_size = 16,
213     },
214     [PIX_FMT_BGR444BE] = {
215         .color_type = FF_COLOR_RGB,
216         .padded_size = 16,
217     },
218     [PIX_FMT_BGR444LE] = {
219         .color_type = FF_COLOR_RGB,
220         .padded_size = 16,
221     },
222     [PIX_FMT_RGB8] = {
223         .color_type = FF_COLOR_RGB,
224     },
225     [PIX_FMT_RGB4] = {
226         .color_type = FF_COLOR_RGB,
227     },
228     [PIX_FMT_RGB4_BYTE] = {
229         .color_type = FF_COLOR_RGB,
230         .padded_size = 8,
231     },
232     [PIX_FMT_BGR8] = {
233         .color_type = FF_COLOR_RGB,
234     },
235     [PIX_FMT_BGR4] = {
236         .color_type = FF_COLOR_RGB,
237     },
238     [PIX_FMT_BGR4_BYTE] = {
239         .color_type = FF_COLOR_RGB,
240         .padded_size = 8,
241     },
242     [PIX_FMT_NV12] = {
243         .color_type = FF_COLOR_YUV,
244     },
245     [PIX_FMT_NV21] = {
246         .color_type = FF_COLOR_YUV,
247     },
248
249     [PIX_FMT_BGRA] = {
250         .is_alpha = 1,
251         .color_type = FF_COLOR_RGB,
252     },
253     [PIX_FMT_RGBA] = {
254         .is_alpha = 1,
255         .color_type = FF_COLOR_RGB,
256     },
257 };
258
259 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
260 {
261     *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
262     *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
263 }
264
265 #if FF_API_GET_PIX_FMT_NAME
266 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
267 {
268     return av_get_pix_fmt_name(pix_fmt);
269 }
270 #endif
271
272 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
273 {
274     return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
275 }
276
277 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
278                    enum PixelFormat pix_fmt, int width, int height)
279 {
280     int ret;
281
282     if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
283         return ret;
284
285     if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
286         return ret;
287
288     return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
289 }
290
291 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
292                      unsigned char *dest, int dest_size)
293 {
294     int i, j, nb_planes = 0, linesizes[4];
295     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
296     int size = avpicture_get_size(pix_fmt, width, height);
297
298     if (size > dest_size || size < 0)
299         return AVERROR(EINVAL);
300
301     for (i = 0; i < desc->nb_components; i++)
302         nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
303     nb_planes++;
304
305     av_image_fill_linesizes(linesizes, pix_fmt, width);
306     for (i = 0; i < nb_planes; i++) {
307         int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
308         const unsigned char *s = src->data[i];
309         h = (height + (1 << shift) - 1) >> shift;
310
311         for (j = 0; j < h; j++) {
312             memcpy(dest, s, linesizes[i]);
313             dest += linesizes[i];
314             s += src->linesize[i];
315         }
316     }
317
318     if (desc->flags & PIX_FMT_PAL)
319         memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
320
321     return size;
322 }
323
324 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
325 {
326     AVPicture dummy_pict;
327     if(av_image_check_size(width, height, 0, NULL))
328         return -1;
329     switch (pix_fmt) {
330     case PIX_FMT_RGB8:
331     case PIX_FMT_BGR8:
332     case PIX_FMT_RGB4_BYTE:
333     case PIX_FMT_BGR4_BYTE:
334     case PIX_FMT_GRAY8:
335         // do not include palette for these pseudo-paletted formats
336         return width * height;
337     }
338     return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
339 }
340
341 static int get_pix_fmt_depth(int *min, int *max, enum PixelFormat pix_fmt)
342 {
343     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
344     int i;
345
346     if (!desc->nb_components) {
347         *min = *max = 0;
348         return AVERROR(EINVAL);
349     }
350
351     *min = INT_MAX, *max = -INT_MAX;
352     for (i = 0; i < desc->nb_components; i++) {
353         *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
354         *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
355     }
356     return 0;
357 }
358
359 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
360                              int has_alpha)
361 {
362     if (dst_pix_fmt>=PIX_FMT_NB || dst_pix_fmt<=PIX_FMT_NONE)
363         return ~0;
364
365     const PixFmtInfo *pf, *ps;
366     const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
367     const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
368     int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
369     int ret, loss;
370
371     ps = &pix_fmt_info[src_pix_fmt];
372
373     /* compute loss */
374     loss = 0;
375
376     if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
377         return ret;
378     if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
379         return ret;
380     if (dst_min_depth < src_min_depth ||
381         dst_max_depth < src_max_depth)
382         loss |= FF_LOSS_DEPTH;
383     if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
384         dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
385         loss |= FF_LOSS_RESOLUTION;
386
387     pf = &pix_fmt_info[dst_pix_fmt];
388     switch(pf->color_type) {
389     case FF_COLOR_RGB:
390         if (ps->color_type != FF_COLOR_RGB &&
391             ps->color_type != FF_COLOR_GRAY)
392             loss |= FF_LOSS_COLORSPACE;
393         break;
394     case FF_COLOR_GRAY:
395         if (ps->color_type != FF_COLOR_GRAY)
396             loss |= FF_LOSS_COLORSPACE;
397         break;
398     case FF_COLOR_YUV:
399         if (ps->color_type != FF_COLOR_YUV)
400             loss |= FF_LOSS_COLORSPACE;
401         break;
402     case FF_COLOR_YUV_JPEG:
403         if (ps->color_type != FF_COLOR_YUV_JPEG &&
404             ps->color_type != FF_COLOR_YUV &&
405             ps->color_type != FF_COLOR_GRAY)
406             loss |= FF_LOSS_COLORSPACE;
407         break;
408     default:
409         /* fail safe test */
410         if (ps->color_type != pf->color_type)
411             loss |= FF_LOSS_COLORSPACE;
412         break;
413     }
414     if (pf->color_type == FF_COLOR_GRAY &&
415         ps->color_type != FF_COLOR_GRAY)
416         loss |= FF_LOSS_CHROMA;
417     if (!pf->is_alpha && (ps->is_alpha && has_alpha))
418         loss |= FF_LOSS_ALPHA;
419     if (dst_pix_fmt == PIX_FMT_PAL8 &&
420         (src_pix_fmt != PIX_FMT_PAL8 && ps->color_type != FF_COLOR_GRAY))
421         loss |= FF_LOSS_COLORQUANT;
422     return loss;
423 }
424
425 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
426 {
427     const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
428     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
429
430     return info->padded_size ?
431         info->padded_size : av_get_bits_per_pixel(desc);
432 }
433
434 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
435                                             int has_alpha, int *loss_ptr)
436 {
437     enum PixelFormat dst_pix_fmt;
438     int i;
439
440     if (loss_ptr) /* all losses count (for backward compatibility) */
441         *loss_ptr = 0;
442
443     dst_pix_fmt = PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
444     for(i = 0; i< FFMIN(PIX_FMT_NB, 64); i++){
445         if (pix_fmt_mask & (1ULL << i))
446             dst_pix_fmt = avcodec_find_best_pix_fmt2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
447     }
448     return dst_pix_fmt;
449 }
450
451 enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
452                                             enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
453 {
454     enum PixelFormat dst_pix_fmt;
455     int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
456     static const int loss_mask_order[] = {
457         ~0, /* no loss first */
458         ~FF_LOSS_ALPHA,
459         ~FF_LOSS_RESOLUTION,
460         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
461         ~FF_LOSS_COLORQUANT,
462         ~FF_LOSS_DEPTH,
463         ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
464           FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
465         0x80000, //non zero entry that combines all loss variants including future additions
466         0,
467     };
468
469     loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
470     dst_pix_fmt = PIX_FMT_NONE;
471     loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
472     loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
473
474     /* try with successive loss */
475     for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
476         loss_order1 = loss1 & loss_mask_order[i];
477         loss_order2 = loss2 & loss_mask_order[i];
478
479         if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
480             dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
481         } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
482             dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
483         }
484     }
485
486     if (loss_ptr)
487         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
488     return dst_pix_fmt;
489 }
490
491 void av_picture_copy(AVPicture *dst, const AVPicture *src,
492                      enum PixelFormat pix_fmt, int width, int height)
493 {
494     av_image_copy(dst->data, dst->linesize, src->data,
495                   src->linesize, pix_fmt, width, height);
496 }
497
498 /* 2x2 -> 1x1 */
499 void ff_shrink22(uint8_t *dst, int dst_wrap,
500                      const uint8_t *src, int src_wrap,
501                      int width, int height)
502 {
503     int w;
504     const uint8_t *s1, *s2;
505     uint8_t *d;
506
507     for(;height > 0; height--) {
508         s1 = src;
509         s2 = s1 + src_wrap;
510         d = dst;
511         for(w = width;w >= 4; w-=4) {
512             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
513             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
514             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
515             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
516             s1 += 8;
517             s2 += 8;
518             d += 4;
519         }
520         for(;w > 0; w--) {
521             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
522             s1 += 2;
523             s2 += 2;
524             d++;
525         }
526         src += 2 * src_wrap;
527         dst += dst_wrap;
528     }
529 }
530
531 /* 4x4 -> 1x1 */
532 void ff_shrink44(uint8_t *dst, int dst_wrap,
533                      const uint8_t *src, int src_wrap,
534                      int width, int height)
535 {
536     int w;
537     const uint8_t *s1, *s2, *s3, *s4;
538     uint8_t *d;
539
540     for(;height > 0; height--) {
541         s1 = src;
542         s2 = s1 + src_wrap;
543         s3 = s2 + src_wrap;
544         s4 = s3 + src_wrap;
545         d = dst;
546         for(w = width;w > 0; w--) {
547             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
548                     s2[0] + s2[1] + s2[2] + s2[3] +
549                     s3[0] + s3[1] + s3[2] + s3[3] +
550                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
551             s1 += 4;
552             s2 += 4;
553             s3 += 4;
554             s4 += 4;
555             d++;
556         }
557         src += 4 * src_wrap;
558         dst += dst_wrap;
559     }
560 }
561
562 /* 8x8 -> 1x1 */
563 void ff_shrink88(uint8_t *dst, int dst_wrap,
564                      const uint8_t *src, int src_wrap,
565                      int width, int height)
566 {
567     int w, i;
568
569     for(;height > 0; height--) {
570         for(w = width;w > 0; w--) {
571             int tmp=0;
572             for(i=0; i<8; i++){
573                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
574                 src += src_wrap;
575             }
576             *(dst++) = (tmp + 32)>>6;
577             src += 8 - 8*src_wrap;
578         }
579         src += 8*src_wrap - 8*width;
580         dst += dst_wrap - width;
581     }
582 }
583
584
585 int avpicture_alloc(AVPicture *picture,
586                     enum PixelFormat pix_fmt, int width, int height)
587 {
588     int ret;
589
590     if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
591         memset(picture, 0, sizeof(AVPicture));
592         return ret;
593     }
594
595     return 0;
596 }
597
598 void avpicture_free(AVPicture *picture)
599 {
600     av_free(picture->data[0]);
601 }
602
603 /* return true if yuv planar */
604 static inline int is_yuv_planar(enum PixelFormat fmt)
605 {
606     const PixFmtInfo         *info = &pix_fmt_info[fmt];
607     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
608     int i;
609     int planes[4] = { 0 };
610
611     if (info->color_type != FF_COLOR_YUV &&
612         info->color_type != FF_COLOR_YUV_JPEG)
613         return 0;
614
615     /* set the used planes */
616     for (i = 0; i < desc->nb_components; i++)
617         planes[desc->comp[i].plane] = 1;
618
619     /* if there is an unused plane, the format is not planar */
620     for (i = 0; i < desc->nb_components; i++)
621         if (!planes[i])
622             return 0;
623     return 1;
624 }
625
626 int av_picture_crop(AVPicture *dst, const AVPicture *src,
627                     enum PixelFormat pix_fmt, int top_band, int left_band)
628 {
629     int y_shift;
630     int x_shift;
631
632     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
633         return -1;
634
635     y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
636     x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
637
638     if (is_yuv_planar(pix_fmt)) {
639     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
640     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
641     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
642     } else{
643         if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
644             return -1;
645         if(left_band) //FIXME add support for this too
646             return -1;
647         dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
648     }
649
650     dst->linesize[0] = src->linesize[0];
651     dst->linesize[1] = src->linesize[1];
652     dst->linesize[2] = src->linesize[2];
653     return 0;
654 }
655
656 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
657                    enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
658             int *color)
659 {
660     uint8_t *optr;
661     int y_shift;
662     int x_shift;
663     int yheight;
664     int i, y;
665
666     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
667         !is_yuv_planar(pix_fmt)) return -1;
668
669     for (i = 0; i < 3; i++) {
670         x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
671         y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
672
673         if (padtop || padleft) {
674             memset(dst->data[i], color[i],
675                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
676         }
677
678         if (padleft || padright) {
679             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
680                 (dst->linesize[i] - (padright >> x_shift));
681             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
682             for (y = 0; y < yheight; y++) {
683                 memset(optr, color[i], (padleft + padright) >> x_shift);
684                 optr += dst->linesize[i];
685             }
686         }
687
688         if (src) { /* first line */
689             uint8_t *iptr = src->data[i];
690             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
691                     (padleft >> x_shift);
692             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
693             iptr += src->linesize[i];
694             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
695                 (dst->linesize[i] - (padright >> x_shift));
696             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
697             for (y = 0; y < yheight; y++) {
698                 memset(optr, color[i], (padleft + padright) >> x_shift);
699                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
700                        (width - padleft - padright) >> x_shift);
701                 iptr += src->linesize[i];
702                 optr += dst->linesize[i];
703             }
704         }
705
706         if (padbottom || padright) {
707             optr = dst->data[i] + dst->linesize[i] *
708                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
709             memset(optr, color[i],dst->linesize[i] *
710                 (padbottom >> y_shift) + (padright >> x_shift));
711         }
712     }
713     return 0;
714 }
715
716 /* NOTE: we scan all the pixels to have an exact information */
717 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
718 {
719     const unsigned char *p;
720     int src_wrap, ret, x, y;
721     unsigned int a;
722     uint32_t *palette = (uint32_t *)src->data[1];
723
724     p = src->data[0];
725     src_wrap = src->linesize[0] - width;
726     ret = 0;
727     for(y=0;y<height;y++) {
728         for(x=0;x<width;x++) {
729             a = palette[p[0]] >> 24;
730             if (a == 0x00) {
731                 ret |= FF_ALPHA_TRANSP;
732             } else if (a != 0xff) {
733                 ret |= FF_ALPHA_SEMI_TRANSP;
734             }
735             p++;
736         }
737         p += src_wrap;
738     }
739     return ret;
740 }
741
742 int img_get_alpha_info(const AVPicture *src,
743                        enum PixelFormat pix_fmt, int width, int height)
744 {
745     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
746     int ret;
747
748     /* no alpha can be represented in format */
749     if (!pf->is_alpha)
750         return 0;
751     switch(pix_fmt) {
752     case PIX_FMT_PAL8:
753         ret = get_alpha_info_pal8(src, width, height);
754         break;
755     default:
756         /* we do not know, so everything is indicated */
757         ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
758         break;
759     }
760     return ret;
761 }
762
763 #if !(HAVE_MMX && HAVE_YASM)
764 /* filter parameters: [-1 4 2 4 -1] // 8 */
765 static void deinterlace_line_c(uint8_t *dst,
766                              const uint8_t *lum_m4, const uint8_t *lum_m3,
767                              const uint8_t *lum_m2, const uint8_t *lum_m1,
768                              const uint8_t *lum,
769                              int size)
770 {
771     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
772     int sum;
773
774     for(;size > 0;size--) {
775         sum = -lum_m4[0];
776         sum += lum_m3[0] << 2;
777         sum += lum_m2[0] << 1;
778         sum += lum_m1[0] << 2;
779         sum += -lum[0];
780         dst[0] = cm[(sum + 4) >> 3];
781         lum_m4++;
782         lum_m3++;
783         lum_m2++;
784         lum_m1++;
785         lum++;
786         dst++;
787     }
788 }
789
790 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
791                                        uint8_t *lum_m2, uint8_t *lum_m1,
792                                        uint8_t *lum, int size)
793 {
794     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
795     int sum;
796
797     for(;size > 0;size--) {
798         sum = -lum_m4[0];
799         sum += lum_m3[0] << 2;
800         sum += lum_m2[0] << 1;
801         lum_m4[0]=lum_m2[0];
802         sum += lum_m1[0] << 2;
803         sum += -lum[0];
804         lum_m2[0] = cm[(sum + 4) >> 3];
805         lum_m4++;
806         lum_m3++;
807         lum_m2++;
808         lum_m1++;
809         lum++;
810     }
811 }
812 #endif
813
814 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
815    top field is copied as is, but the bottom field is deinterlaced
816    against the top field. */
817 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
818                                     const uint8_t *src1, int src_wrap,
819                                     int width, int height)
820 {
821     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
822     int y;
823
824     src_m2 = src1;
825     src_m1 = src1;
826     src_0=&src_m1[src_wrap];
827     src_p1=&src_0[src_wrap];
828     src_p2=&src_p1[src_wrap];
829     for(y=0;y<(height-2);y+=2) {
830         memcpy(dst,src_m1,width);
831         dst += dst_wrap;
832         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
833         src_m2 = src_0;
834         src_m1 = src_p1;
835         src_0 = src_p2;
836         src_p1 += 2*src_wrap;
837         src_p2 += 2*src_wrap;
838         dst += dst_wrap;
839     }
840     memcpy(dst,src_m1,width);
841     dst += dst_wrap;
842     /* do last line */
843     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
844 }
845
846 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
847                                              int width, int height)
848 {
849     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
850     int y;
851     uint8_t *buf;
852     buf = (uint8_t*)av_malloc(width);
853
854     src_m1 = src1;
855     memcpy(buf,src_m1,width);
856     src_0=&src_m1[src_wrap];
857     src_p1=&src_0[src_wrap];
858     src_p2=&src_p1[src_wrap];
859     for(y=0;y<(height-2);y+=2) {
860         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
861         src_m1 = src_p1;
862         src_0 = src_p2;
863         src_p1 += 2*src_wrap;
864         src_p2 += 2*src_wrap;
865     }
866     /* do last line */
867     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
868     av_free(buf);
869 }
870
871 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
872                           enum PixelFormat pix_fmt, int width, int height)
873 {
874     int i;
875
876     if (pix_fmt != PIX_FMT_YUV420P &&
877         pix_fmt != PIX_FMT_YUVJ420P &&
878         pix_fmt != PIX_FMT_YUV422P &&
879         pix_fmt != PIX_FMT_YUVJ422P &&
880         pix_fmt != PIX_FMT_YUV444P &&
881         pix_fmt != PIX_FMT_YUV411P &&
882         pix_fmt != PIX_FMT_GRAY8)
883         return -1;
884     if ((width & 3) != 0 || (height & 3) != 0)
885         return -1;
886
887     for(i=0;i<3;i++) {
888         if (i == 1) {
889             switch(pix_fmt) {
890             case PIX_FMT_YUVJ420P:
891             case PIX_FMT_YUV420P:
892                 width >>= 1;
893                 height >>= 1;
894                 break;
895             case PIX_FMT_YUV422P:
896             case PIX_FMT_YUVJ422P:
897                 width >>= 1;
898                 break;
899             case PIX_FMT_YUV411P:
900                 width >>= 2;
901                 break;
902             default:
903                 break;
904             }
905             if (pix_fmt == PIX_FMT_GRAY8) {
906                 break;
907             }
908         }
909         if (src == dst) {
910             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
911                                  width, height);
912         } else {
913             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
914                                         src->data[i], src->linesize[i],
915                                         width, height);
916         }
917     }
918     emms_c();
919     return 0;
920 }
921