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