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