]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgconvert.c
Silicon Graphics Movie (.mv) demuxer
[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 "libavutil/avassert.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/common.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
41
42 #if HAVE_MMX_EXTERNAL
43 #include "x86/dsputil_mmx.h"
44 #endif
45
46 #define FF_COLOR_NA      -1
47 #define FF_COLOR_RGB      0 /**< RGB color space */
48 #define FF_COLOR_GRAY     1 /**< gray color space */
49 #define FF_COLOR_YUV      2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
50 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
51
52 #if HAVE_MMX_EXTERNAL
53 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
54 #define deinterlace_line         ff_deinterlace_line_mmx
55 #else
56 #define deinterlace_line_inplace deinterlace_line_inplace_c
57 #define deinterlace_line         deinterlace_line_c
58 #endif
59
60 #define pixdesc_has_alpha(pixdesc) \
61     ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & PIX_FMT_PAL)
62
63
64 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
65 {
66     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
67     av_assert0(desc);
68     *h_shift = desc->log2_chroma_w;
69     *v_shift = desc->log2_chroma_h;
70 }
71
72 static get_color_type(AVPixFmtDescriptor *desc) {
73     if(desc->nb_components == 1 || desc->nb_components == 2)
74         return FF_COLOR_GRAY;
75
76     if(desc->name && !strncmp(desc->name, "yuvj", 4))
77         return FF_COLOR_YUV_JPEG;
78
79     if(desc->flags & PIX_FMT_RGB)
80         return  FF_COLOR_RGB;
81
82     if(desc->nb_components == 0)
83         return FF_COLOR_NA;
84
85     return FF_COLOR_YUV;
86 }
87
88 static int get_pix_fmt_depth(int *min, int *max, enum AVPixelFormat pix_fmt)
89 {
90     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
91     int i;
92
93     if (!desc || !desc->nb_components) {
94         *min = *max = 0;
95         return AVERROR(EINVAL);
96     }
97
98     *min = INT_MAX, *max = -INT_MAX;
99     for (i = 0; i < desc->nb_components; i++) {
100         *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
101         *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
102     }
103     return 0;
104 }
105
106 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
107                              enum AVPixelFormat src_pix_fmt,
108                              int has_alpha)
109 {
110     const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
111     const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
112     int src_color, dst_color;
113     int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
114     int ret, loss, i, nb_components;
115
116     if (dst_pix_fmt >= AV_PIX_FMT_NB || dst_pix_fmt <= AV_PIX_FMT_NONE)
117         return ~0;
118
119     /* compute loss */
120     loss = 0;
121
122     if (dst_pix_fmt == src_pix_fmt)
123         return 0;
124
125     if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
126         return ret;
127     if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
128         return ret;
129
130     src_color = get_color_type(src_desc);
131     dst_color = get_color_type(dst_desc);
132     nb_components = FFMIN(src_desc->nb_components, dst_desc->nb_components);
133
134     for (i = 0; i < nb_components; i++)
135         if (src_desc->comp[i].depth_minus1 > dst_desc->comp[i].depth_minus1)
136             loss |= FF_LOSS_DEPTH;
137
138     if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
139         dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
140         loss |= FF_LOSS_RESOLUTION;
141
142     switch(dst_color) {
143     case FF_COLOR_RGB:
144         if (src_color != FF_COLOR_RGB &&
145             src_color != FF_COLOR_GRAY)
146             loss |= FF_LOSS_COLORSPACE;
147         break;
148     case FF_COLOR_GRAY:
149         if (src_color != FF_COLOR_GRAY)
150             loss |= FF_LOSS_COLORSPACE;
151         break;
152     case FF_COLOR_YUV:
153         if (src_color != FF_COLOR_YUV)
154             loss |= FF_LOSS_COLORSPACE;
155         break;
156     case FF_COLOR_YUV_JPEG:
157         if (src_color != FF_COLOR_YUV_JPEG &&
158             src_color != FF_COLOR_YUV &&
159             src_color != FF_COLOR_GRAY)
160             loss |= FF_LOSS_COLORSPACE;
161         break;
162     default:
163         /* fail safe test */
164         if (src_color != dst_color)
165             loss |= FF_LOSS_COLORSPACE;
166         break;
167     }
168     if (dst_color == FF_COLOR_GRAY &&
169         src_color != FF_COLOR_GRAY)
170         loss |= FF_LOSS_CHROMA;
171     if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
172         loss |= FF_LOSS_ALPHA;
173     if (dst_pix_fmt == AV_PIX_FMT_PAL8 &&
174         (src_pix_fmt != AV_PIX_FMT_PAL8 && (src_color != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
175         loss |= FF_LOSS_COLORQUANT;
176
177     return loss;
178 }
179
180 #if FF_API_FIND_BEST_PIX_FMT
181 enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
182                                             int has_alpha, int *loss_ptr)
183 {
184     enum AVPixelFormat dst_pix_fmt;
185     int i;
186
187     if (loss_ptr) /* all losses count (for backward compatibility) */
188         *loss_ptr = 0;
189
190     dst_pix_fmt = AV_PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
191     for(i = 0; i< FFMIN(AV_PIX_FMT_NB, 64); i++){
192         if (pix_fmt_mask & (1ULL << i))
193             dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
194     }
195     return dst_pix_fmt;
196 }
197 #endif /* FF_API_FIND_BEST_PIX_FMT */
198
199 enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
200                                             enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
201 {
202     enum AVPixelFormat dst_pix_fmt;
203     int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
204     const AVPixFmtDescriptor *desc1 = av_pix_fmt_desc_get(dst_pix_fmt1);
205     const AVPixFmtDescriptor *desc2 = av_pix_fmt_desc_get(dst_pix_fmt2);
206     static const int loss_mask_order[] = {
207         ~0, /* no loss first */
208         ~FF_LOSS_ALPHA,
209         ~FF_LOSS_RESOLUTION,
210         ~FF_LOSS_COLORSPACE,
211         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
212         ~FF_LOSS_COLORQUANT,
213         ~FF_LOSS_DEPTH,
214         ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
215         ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
216           FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
217         0x80000, //non zero entry that combines all loss variants including future additions
218         0,
219     };
220
221     loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
222     dst_pix_fmt = AV_PIX_FMT_NONE;
223     loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
224     loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
225
226     /* try with successive loss */
227     for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == AV_PIX_FMT_NONE;i++) {
228         loss_order1 = loss1 & loss_mask_order[i];
229         loss_order2 = loss2 & loss_mask_order[i];
230
231         if (loss_order1 == 0 && loss_order2 == 0 && dst_pix_fmt2 != AV_PIX_FMT_NONE && dst_pix_fmt1 != AV_PIX_FMT_NONE){ /* use format with smallest depth */
232             if(av_get_padded_bits_per_pixel(desc2) != av_get_padded_bits_per_pixel(desc1)) {
233                 dst_pix_fmt = av_get_padded_bits_per_pixel(desc2) < av_get_padded_bits_per_pixel(desc1) ? dst_pix_fmt2 : dst_pix_fmt1;
234             } else {
235                 dst_pix_fmt = desc2->nb_components < desc1->nb_components ? dst_pix_fmt2 : dst_pix_fmt1;
236             }
237         } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
238             dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
239         }
240     }
241
242     if (loss_ptr)
243         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
244     return dst_pix_fmt;
245 }
246
247 #if AV_HAVE_INCOMPATIBLE_FORK_ABI
248 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
249                                             enum AVPixelFormat src_pix_fmt,
250                                             int has_alpha, int *loss_ptr){
251     return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
252 }
253 #else
254 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
255                                             enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
256 {
257     return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
258 }
259 #endif
260
261 enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(enum AVPixelFormat *pix_fmt_list,
262                                             enum AVPixelFormat src_pix_fmt,
263                                             int has_alpha, int *loss_ptr){
264     int i;
265
266     enum AVPixelFormat best = AV_PIX_FMT_NONE;
267
268     for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
269         best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
270
271     return best;
272 }
273
274 /* 2x2 -> 1x1 */
275 void ff_shrink22(uint8_t *dst, int dst_wrap,
276                      const uint8_t *src, int src_wrap,
277                      int width, int height)
278 {
279     int w;
280     const uint8_t *s1, *s2;
281     uint8_t *d;
282
283     for(;height > 0; height--) {
284         s1 = src;
285         s2 = s1 + src_wrap;
286         d = dst;
287         for(w = width;w >= 4; w-=4) {
288             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
289             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
290             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
291             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
292             s1 += 8;
293             s2 += 8;
294             d += 4;
295         }
296         for(;w > 0; w--) {
297             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
298             s1 += 2;
299             s2 += 2;
300             d++;
301         }
302         src += 2 * src_wrap;
303         dst += dst_wrap;
304     }
305 }
306
307 /* 4x4 -> 1x1 */
308 void ff_shrink44(uint8_t *dst, int dst_wrap,
309                      const uint8_t *src, int src_wrap,
310                      int width, int height)
311 {
312     int w;
313     const uint8_t *s1, *s2, *s3, *s4;
314     uint8_t *d;
315
316     for(;height > 0; height--) {
317         s1 = src;
318         s2 = s1 + src_wrap;
319         s3 = s2 + src_wrap;
320         s4 = s3 + src_wrap;
321         d = dst;
322         for(w = width;w > 0; w--) {
323             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
324                     s2[0] + s2[1] + s2[2] + s2[3] +
325                     s3[0] + s3[1] + s3[2] + s3[3] +
326                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
327             s1 += 4;
328             s2 += 4;
329             s3 += 4;
330             s4 += 4;
331             d++;
332         }
333         src += 4 * src_wrap;
334         dst += dst_wrap;
335     }
336 }
337
338 /* 8x8 -> 1x1 */
339 void ff_shrink88(uint8_t *dst, int dst_wrap,
340                      const uint8_t *src, int src_wrap,
341                      int width, int height)
342 {
343     int w, i;
344
345     for(;height > 0; height--) {
346         for(w = width;w > 0; w--) {
347             int tmp=0;
348             for(i=0; i<8; i++){
349                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
350                 src += src_wrap;
351             }
352             *(dst++) = (tmp + 32)>>6;
353             src += 8 - 8*src_wrap;
354         }
355         src += 8*src_wrap - 8*width;
356         dst += dst_wrap - width;
357     }
358 }
359
360 /* return true if yuv planar */
361 static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
362 {
363     int i;
364     int planes[4] = { 0 };
365
366     if (     desc->flags & PIX_FMT_RGB
367         || !(desc->flags & PIX_FMT_PLANAR))
368         return 0;
369
370     /* set the used planes */
371     for (i = 0; i < desc->nb_components; i++)
372         planes[desc->comp[i].plane] = 1;
373
374     /* if there is an unused plane, the format is not planar */
375     for (i = 0; i < desc->nb_components; i++)
376         if (!planes[i])
377             return 0;
378     return 1;
379 }
380
381 int av_picture_crop(AVPicture *dst, const AVPicture *src,
382                     enum AVPixelFormat pix_fmt, int top_band, int left_band)
383 {
384     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
385     int y_shift;
386     int x_shift;
387
388     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
389         return -1;
390
391     y_shift = desc->log2_chroma_h;
392     x_shift = desc->log2_chroma_w;
393
394     if (is_yuv_planar(desc)) {
395     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
396     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
397     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
398     } else{
399         if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
400             return -1;
401         if(left_band) //FIXME add support for this too
402             return -1;
403         dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
404     }
405
406     dst->linesize[0] = src->linesize[0];
407     dst->linesize[1] = src->linesize[1];
408     dst->linesize[2] = src->linesize[2];
409     return 0;
410 }
411
412 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
413                    enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
414             int *color)
415 {
416     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
417     uint8_t *optr;
418     int y_shift;
419     int x_shift;
420     int yheight;
421     int i, y;
422
423     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
424         !is_yuv_planar(desc)) return -1;
425
426     for (i = 0; i < 3; i++) {
427         x_shift = i ? desc->log2_chroma_w : 0;
428         y_shift = i ? desc->log2_chroma_h : 0;
429
430         if (padtop || padleft) {
431             memset(dst->data[i], color[i],
432                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
433         }
434
435         if (padleft || padright) {
436             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
437                 (dst->linesize[i] - (padright >> x_shift));
438             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
439             for (y = 0; y < yheight; y++) {
440                 memset(optr, color[i], (padleft + padright) >> x_shift);
441                 optr += dst->linesize[i];
442             }
443         }
444
445         if (src) { /* first line */
446             uint8_t *iptr = src->data[i];
447             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
448                     (padleft >> x_shift);
449             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
450             iptr += src->linesize[i];
451             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
452                 (dst->linesize[i] - (padright >> x_shift));
453             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
454             for (y = 0; y < yheight; y++) {
455                 memset(optr, color[i], (padleft + padright) >> x_shift);
456                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
457                        (width - padleft - padright) >> x_shift);
458                 iptr += src->linesize[i];
459                 optr += dst->linesize[i];
460             }
461         }
462
463         if (padbottom || padright) {
464             optr = dst->data[i] + dst->linesize[i] *
465                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
466             memset(optr, color[i],dst->linesize[i] *
467                 (padbottom >> y_shift) + (padright >> x_shift));
468         }
469     }
470     return 0;
471 }
472
473 #if !HAVE_MMX_EXTERNAL
474 /* filter parameters: [-1 4 2 4 -1] // 8 */
475 static void deinterlace_line_c(uint8_t *dst,
476                              const uint8_t *lum_m4, const uint8_t *lum_m3,
477                              const uint8_t *lum_m2, const uint8_t *lum_m1,
478                              const uint8_t *lum,
479                              int size)
480 {
481     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
482     int sum;
483
484     for(;size > 0;size--) {
485         sum = -lum_m4[0];
486         sum += lum_m3[0] << 2;
487         sum += lum_m2[0] << 1;
488         sum += lum_m1[0] << 2;
489         sum += -lum[0];
490         dst[0] = cm[(sum + 4) >> 3];
491         lum_m4++;
492         lum_m3++;
493         lum_m2++;
494         lum_m1++;
495         lum++;
496         dst++;
497     }
498 }
499
500 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
501                                        uint8_t *lum_m2, uint8_t *lum_m1,
502                                        uint8_t *lum, int size)
503 {
504     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
505     int sum;
506
507     for(;size > 0;size--) {
508         sum = -lum_m4[0];
509         sum += lum_m3[0] << 2;
510         sum += lum_m2[0] << 1;
511         lum_m4[0]=lum_m2[0];
512         sum += lum_m1[0] << 2;
513         sum += -lum[0];
514         lum_m2[0] = cm[(sum + 4) >> 3];
515         lum_m4++;
516         lum_m3++;
517         lum_m2++;
518         lum_m1++;
519         lum++;
520     }
521 }
522 #endif /* !HAVE_MMX_EXTERNAL */
523
524 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
525    top field is copied as is, but the bottom field is deinterlaced
526    against the top field. */
527 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
528                                     const uint8_t *src1, int src_wrap,
529                                     int width, int height)
530 {
531     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
532     int y;
533
534     src_m2 = src1;
535     src_m1 = src1;
536     src_0=&src_m1[src_wrap];
537     src_p1=&src_0[src_wrap];
538     src_p2=&src_p1[src_wrap];
539     for(y=0;y<(height-2);y+=2) {
540         memcpy(dst,src_m1,width);
541         dst += dst_wrap;
542         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
543         src_m2 = src_0;
544         src_m1 = src_p1;
545         src_0 = src_p2;
546         src_p1 += 2*src_wrap;
547         src_p2 += 2*src_wrap;
548         dst += dst_wrap;
549     }
550     memcpy(dst,src_m1,width);
551     dst += dst_wrap;
552     /* do last line */
553     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
554 }
555
556 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
557                                              int width, int height)
558 {
559     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
560     int y;
561     uint8_t *buf;
562     buf = av_malloc(width);
563
564     src_m1 = src1;
565     memcpy(buf,src_m1,width);
566     src_0=&src_m1[src_wrap];
567     src_p1=&src_0[src_wrap];
568     src_p2=&src_p1[src_wrap];
569     for(y=0;y<(height-2);y+=2) {
570         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
571         src_m1 = src_p1;
572         src_0 = src_p2;
573         src_p1 += 2*src_wrap;
574         src_p2 += 2*src_wrap;
575     }
576     /* do last line */
577     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
578     av_free(buf);
579 }
580
581 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
582                           enum AVPixelFormat pix_fmt, int width, int height)
583 {
584     int i;
585
586     if (pix_fmt != AV_PIX_FMT_YUV420P &&
587         pix_fmt != AV_PIX_FMT_YUVJ420P &&
588         pix_fmt != AV_PIX_FMT_YUV422P &&
589         pix_fmt != AV_PIX_FMT_YUVJ422P &&
590         pix_fmt != AV_PIX_FMT_YUV444P &&
591         pix_fmt != AV_PIX_FMT_YUV411P &&
592         pix_fmt != AV_PIX_FMT_GRAY8)
593         return -1;
594     if ((width & 3) != 0 || (height & 3) != 0)
595         return -1;
596
597     for(i=0;i<3;i++) {
598         if (i == 1) {
599             switch(pix_fmt) {
600             case AV_PIX_FMT_YUVJ420P:
601             case AV_PIX_FMT_YUV420P:
602                 width >>= 1;
603                 height >>= 1;
604                 break;
605             case AV_PIX_FMT_YUV422P:
606             case AV_PIX_FMT_YUVJ422P:
607                 width >>= 1;
608                 break;
609             case AV_PIX_FMT_YUV411P:
610                 width >>= 2;
611                 break;
612             default:
613                 break;
614             }
615             if (pix_fmt == AV_PIX_FMT_GRAY8) {
616                 break;
617             }
618         }
619         if (src == dst) {
620             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
621                                  width, height);
622         } else {
623             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
624                                         src->data[i], src->linesize[i],
625                                         width, height);
626         }
627     }
628     emms_c();
629     return 0;
630 }
631
632 #ifdef TEST
633
634 int main(void){
635     int i;
636     int err=0;
637     int skip = 0;
638
639     for (i=0; i<AV_PIX_FMT_NB*2; i++) {
640         AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
641         if(!desc || !desc->name) {
642             skip ++;
643             continue;
644         }
645         if (skip) {
646             av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
647             skip = 0;
648         }
649         av_log(NULL, AV_LOG_INFO, "pix fmt %s yuv_plan:%d avg_bpp:%d colortype:%d\n", desc->name, is_yuv_planar(desc), av_get_padded_bits_per_pixel(desc), get_color_type(desc));
650         if ((!(desc->flags & PIX_FMT_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
651             av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");
652             err = 1;
653         }
654     }
655     return err;
656 }
657
658 #endif