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