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