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