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