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