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