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