2 * Misc image conversion routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
24 * misc image conversion routines
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
36 #include "imgconvert.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/imgutils.h"
41 #if HAVE_MMX && HAVE_YASM
42 #include "x86/dsputil_mmx.h"
45 #define FF_COLOR_RGB 0 /**< RGB color space */
46 #define FF_COLOR_GRAY 1 /**< gray color space */
47 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
48 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
50 #if HAVE_MMX && HAVE_YASM
51 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
52 #define deinterlace_line ff_deinterlace_line_mmx
54 #define deinterlace_line_inplace deinterlace_line_inplace_c
55 #define deinterlace_line deinterlace_line_c
58 typedef struct PixFmtInfo {
59 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
60 uint8_t is_alpha : 1; /**< true if alpha can be specified */
61 uint8_t padded_size; /**< padded size in bits if different from the non-padded size */
64 /* this table gives more information about formats */
65 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
68 .color_type = FF_COLOR_YUV,
71 .color_type = FF_COLOR_YUV,
74 .color_type = FF_COLOR_YUV,
77 .color_type = FF_COLOR_YUV,
80 .color_type = FF_COLOR_YUV,
83 .color_type = FF_COLOR_YUV,
86 .color_type = FF_COLOR_YUV,
89 .color_type = FF_COLOR_YUV,
91 [PIX_FMT_YUV420P16LE] = {
92 .color_type = FF_COLOR_YUV,
94 [PIX_FMT_YUV422P16LE] = {
95 .color_type = FF_COLOR_YUV,
97 [PIX_FMT_YUV444P16LE] = {
98 .color_type = FF_COLOR_YUV,
100 [PIX_FMT_YUV420P16BE] = {
101 .color_type = FF_COLOR_YUV,
103 [PIX_FMT_YUV422P16BE] = {
104 .color_type = FF_COLOR_YUV,
106 [PIX_FMT_YUV444P16BE] = {
107 .color_type = FF_COLOR_YUV,
110 /* YUV formats with alpha plane */
111 [PIX_FMT_YUVA420P] = {
113 .color_type = FF_COLOR_YUV,
116 [PIX_FMT_YUVA444P] = {
118 .color_type = FF_COLOR_YUV,
122 [PIX_FMT_YUVJ420P] = {
123 .color_type = FF_COLOR_YUV_JPEG,
125 [PIX_FMT_YUVJ422P] = {
126 .color_type = FF_COLOR_YUV_JPEG,
128 [PIX_FMT_YUVJ444P] = {
129 .color_type = FF_COLOR_YUV_JPEG,
131 [PIX_FMT_YUVJ440P] = {
132 .color_type = FF_COLOR_YUV_JPEG,
137 .color_type = FF_COLOR_RGB,
140 .color_type = FF_COLOR_RGB,
144 .color_type = FF_COLOR_RGB,
146 [PIX_FMT_RGB48BE] = {
147 .color_type = FF_COLOR_RGB,
149 [PIX_FMT_RGB48LE] = {
150 .color_type = FF_COLOR_RGB,
152 [PIX_FMT_RGBA64BE] = {
154 .color_type = FF_COLOR_RGB,
156 [PIX_FMT_RGBA64LE] = {
158 .color_type = FF_COLOR_RGB,
160 [PIX_FMT_RGB565BE] = {
161 .color_type = FF_COLOR_RGB,
163 [PIX_FMT_RGB565LE] = {
164 .color_type = FF_COLOR_RGB,
166 [PIX_FMT_RGB555BE] = {
167 .color_type = FF_COLOR_RGB,
170 [PIX_FMT_RGB555LE] = {
171 .color_type = FF_COLOR_RGB,
174 [PIX_FMT_RGB444BE] = {
175 .color_type = FF_COLOR_RGB,
178 [PIX_FMT_RGB444LE] = {
179 .color_type = FF_COLOR_RGB,
183 /* gray / mono formats */
184 [PIX_FMT_GRAY16BE] = {
185 .color_type = FF_COLOR_GRAY,
187 [PIX_FMT_GRAY16LE] = {
188 .color_type = FF_COLOR_GRAY,
191 .color_type = FF_COLOR_GRAY,
195 .color_type = FF_COLOR_GRAY,
197 [PIX_FMT_MONOWHITE] = {
198 .color_type = FF_COLOR_GRAY,
200 [PIX_FMT_MONOBLACK] = {
201 .color_type = FF_COLOR_GRAY,
204 /* paletted formats */
207 .color_type = FF_COLOR_RGB,
209 [PIX_FMT_UYYVYY411] = {
210 .color_type = FF_COLOR_YUV,
214 .color_type = FF_COLOR_RGB,
216 [PIX_FMT_BGR48BE] = {
217 .color_type = FF_COLOR_RGB,
219 [PIX_FMT_BGR48LE] = {
220 .color_type = FF_COLOR_RGB,
222 [PIX_FMT_BGRA64BE] = {
224 .color_type = FF_COLOR_RGB,
226 [PIX_FMT_BGRA64LE] = {
228 .color_type = FF_COLOR_RGB,
230 [PIX_FMT_BGR565BE] = {
231 .color_type = FF_COLOR_RGB,
234 [PIX_FMT_BGR565LE] = {
235 .color_type = FF_COLOR_RGB,
238 [PIX_FMT_BGR555BE] = {
239 .color_type = FF_COLOR_RGB,
242 [PIX_FMT_BGR555LE] = {
243 .color_type = FF_COLOR_RGB,
246 [PIX_FMT_BGR444BE] = {
247 .color_type = FF_COLOR_RGB,
250 [PIX_FMT_BGR444LE] = {
251 .color_type = FF_COLOR_RGB,
255 .color_type = FF_COLOR_RGB,
258 .color_type = FF_COLOR_RGB,
260 [PIX_FMT_RGB4_BYTE] = {
261 .color_type = FF_COLOR_RGB,
265 .color_type = FF_COLOR_RGB,
268 .color_type = FF_COLOR_RGB,
270 [PIX_FMT_BGR4_BYTE] = {
271 .color_type = FF_COLOR_RGB,
275 .color_type = FF_COLOR_YUV,
278 .color_type = FF_COLOR_YUV,
283 .color_type = FF_COLOR_RGB,
287 .color_type = FF_COLOR_RGB,
291 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
293 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
294 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
297 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
299 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
302 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
303 enum PixelFormat pix_fmt, int width, int height)
307 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
310 if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
313 return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
316 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
317 unsigned char *dest, int dest_size)
319 int i, j, nb_planes = 0, linesizes[4];
320 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
321 int size = avpicture_get_size(pix_fmt, width, height);
323 if (size > dest_size || size < 0)
324 return AVERROR(EINVAL);
326 for (i = 0; i < desc->nb_components; i++)
327 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
330 av_image_fill_linesizes(linesizes, pix_fmt, width);
331 for (i = 0; i < nb_planes; i++) {
332 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
333 const unsigned char *s = src->data[i];
334 h = (height + (1 << shift) - 1) >> shift;
336 for (j = 0; j < h; j++) {
337 memcpy(dest, s, linesizes[i]);
338 dest += linesizes[i];
339 s += src->linesize[i];
346 case PIX_FMT_RGB4_BYTE:
347 case PIX_FMT_BGR4_BYTE:
349 // do not include palette for these pseudo-paletted formats
353 if (desc->flags & PIX_FMT_PAL)
354 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
359 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
361 AVPicture dummy_pict;
362 if(av_image_check_size(width, height, 0, NULL))
364 if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
365 // do not include palette for these pseudo-paletted formats
366 return width * height;
367 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
370 static int get_pix_fmt_depth(int *min, int *max, enum PixelFormat pix_fmt)
372 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
375 if (!desc->nb_components) {
377 return AVERROR(EINVAL);
380 *min = INT_MAX, *max = -INT_MAX;
381 for (i = 0; i < desc->nb_components; i++) {
382 *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
383 *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
388 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
391 const PixFmtInfo *pf, *ps;
392 const AVPixFmtDescriptor *src_desc;
393 const AVPixFmtDescriptor *dst_desc;
394 int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
397 if (dst_pix_fmt >= PIX_FMT_NB || dst_pix_fmt <= PIX_FMT_NONE)
400 src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
401 dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
402 ps = &pix_fmt_info[src_pix_fmt];
407 if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
409 if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
411 if (dst_min_depth < src_min_depth ||
412 dst_max_depth < src_max_depth)
413 loss |= FF_LOSS_DEPTH;
414 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
415 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
416 loss |= FF_LOSS_RESOLUTION;
418 pf = &pix_fmt_info[dst_pix_fmt];
419 switch(pf->color_type) {
421 if (ps->color_type != FF_COLOR_RGB &&
422 ps->color_type != FF_COLOR_GRAY)
423 loss |= FF_LOSS_COLORSPACE;
426 if (ps->color_type != FF_COLOR_GRAY)
427 loss |= FF_LOSS_COLORSPACE;
430 if (ps->color_type != FF_COLOR_YUV)
431 loss |= FF_LOSS_COLORSPACE;
433 case FF_COLOR_YUV_JPEG:
434 if (ps->color_type != FF_COLOR_YUV_JPEG &&
435 ps->color_type != FF_COLOR_YUV &&
436 ps->color_type != FF_COLOR_GRAY)
437 loss |= FF_LOSS_COLORSPACE;
441 if (ps->color_type != pf->color_type)
442 loss |= FF_LOSS_COLORSPACE;
445 if (pf->color_type == FF_COLOR_GRAY &&
446 ps->color_type != FF_COLOR_GRAY)
447 loss |= FF_LOSS_CHROMA;
448 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
449 loss |= FF_LOSS_ALPHA;
450 if (dst_pix_fmt == PIX_FMT_PAL8 &&
451 (src_pix_fmt != PIX_FMT_PAL8 && (ps->color_type != FF_COLOR_GRAY || (ps->is_alpha && has_alpha))))
452 loss |= FF_LOSS_COLORQUANT;
457 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
459 const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
460 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
462 return info->padded_size ?
463 info->padded_size : av_get_bits_per_pixel(desc);
466 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
467 int has_alpha, int *loss_ptr)
469 enum PixelFormat dst_pix_fmt;
472 if (loss_ptr) /* all losses count (for backward compatibility) */
475 dst_pix_fmt = PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
476 for(i = 0; i< FFMIN(PIX_FMT_NB, 64); i++){
477 if (pix_fmt_mask & (1ULL << i))
478 dst_pix_fmt = avcodec_find_best_pix_fmt2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
483 enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
484 enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
486 enum PixelFormat dst_pix_fmt;
487 int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
488 static const int loss_mask_order[] = {
489 ~0, /* no loss first */
492 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
495 ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
496 FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
497 0x80000, //non zero entry that combines all loss variants including future additions
501 loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
502 dst_pix_fmt = PIX_FMT_NONE;
503 loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
504 loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
506 /* try with successive loss */
507 for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
508 loss_order1 = loss1 & loss_mask_order[i];
509 loss_order2 = loss2 & loss_mask_order[i];
511 if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
512 dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
513 } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
514 dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
519 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
523 void av_picture_copy(AVPicture *dst, const AVPicture *src,
524 enum PixelFormat pix_fmt, int width, int height)
526 av_image_copy(dst->data, dst->linesize, src->data,
527 src->linesize, pix_fmt, width, height);
531 void ff_shrink22(uint8_t *dst, int dst_wrap,
532 const uint8_t *src, int src_wrap,
533 int width, int height)
536 const uint8_t *s1, *s2;
539 for(;height > 0; height--) {
543 for(w = width;w >= 4; w-=4) {
544 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
545 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
546 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
547 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
553 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
564 void ff_shrink44(uint8_t *dst, int dst_wrap,
565 const uint8_t *src, int src_wrap,
566 int width, int height)
569 const uint8_t *s1, *s2, *s3, *s4;
572 for(;height > 0; height--) {
578 for(w = width;w > 0; w--) {
579 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
580 s2[0] + s2[1] + s2[2] + s2[3] +
581 s3[0] + s3[1] + s3[2] + s3[3] +
582 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
595 void ff_shrink88(uint8_t *dst, int dst_wrap,
596 const uint8_t *src, int src_wrap,
597 int width, int height)
601 for(;height > 0; height--) {
602 for(w = width;w > 0; w--) {
605 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
608 *(dst++) = (tmp + 32)>>6;
609 src += 8 - 8*src_wrap;
611 src += 8*src_wrap - 8*width;
612 dst += dst_wrap - width;
617 int avpicture_alloc(AVPicture *picture,
618 enum PixelFormat pix_fmt, int width, int height)
622 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
623 memset(picture, 0, sizeof(AVPicture));
630 void avpicture_free(AVPicture *picture)
632 av_free(picture->data[0]);
635 /* return true if yuv planar */
636 static inline int is_yuv_planar(enum PixelFormat fmt)
638 const PixFmtInfo *info = &pix_fmt_info[fmt];
639 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
641 int planes[4] = { 0 };
643 if (info->color_type != FF_COLOR_YUV &&
644 info->color_type != FF_COLOR_YUV_JPEG)
647 /* set the used planes */
648 for (i = 0; i < desc->nb_components; i++)
649 planes[desc->comp[i].plane] = 1;
651 /* if there is an unused plane, the format is not planar */
652 for (i = 0; i < desc->nb_components; i++)
658 int av_picture_crop(AVPicture *dst, const AVPicture *src,
659 enum PixelFormat pix_fmt, int top_band, int left_band)
664 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
667 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
668 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
670 if (is_yuv_planar(pix_fmt)) {
671 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
672 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
673 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
675 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
677 if(left_band) //FIXME add support for this too
679 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
682 dst->linesize[0] = src->linesize[0];
683 dst->linesize[1] = src->linesize[1];
684 dst->linesize[2] = src->linesize[2];
688 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
689 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
698 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
699 !is_yuv_planar(pix_fmt)) return -1;
701 for (i = 0; i < 3; i++) {
702 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
703 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
705 if (padtop || padleft) {
706 memset(dst->data[i], color[i],
707 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
710 if (padleft || padright) {
711 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
712 (dst->linesize[i] - (padright >> x_shift));
713 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
714 for (y = 0; y < yheight; y++) {
715 memset(optr, color[i], (padleft + padright) >> x_shift);
716 optr += dst->linesize[i];
720 if (src) { /* first line */
721 uint8_t *iptr = src->data[i];
722 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
723 (padleft >> x_shift);
724 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
725 iptr += src->linesize[i];
726 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
727 (dst->linesize[i] - (padright >> x_shift));
728 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
729 for (y = 0; y < yheight; y++) {
730 memset(optr, color[i], (padleft + padright) >> x_shift);
731 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
732 (width - padleft - padright) >> x_shift);
733 iptr += src->linesize[i];
734 optr += dst->linesize[i];
738 if (padbottom || padright) {
739 optr = dst->data[i] + dst->linesize[i] *
740 ((height - padbottom) >> y_shift) - (padright >> x_shift);
741 memset(optr, color[i],dst->linesize[i] *
742 (padbottom >> y_shift) + (padright >> x_shift));
748 #if !(HAVE_MMX && HAVE_YASM)
749 /* filter parameters: [-1 4 2 4 -1] // 8 */
750 static void deinterlace_line_c(uint8_t *dst,
751 const uint8_t *lum_m4, const uint8_t *lum_m3,
752 const uint8_t *lum_m2, const uint8_t *lum_m1,
756 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
759 for(;size > 0;size--) {
761 sum += lum_m3[0] << 2;
762 sum += lum_m2[0] << 1;
763 sum += lum_m1[0] << 2;
765 dst[0] = cm[(sum + 4) >> 3];
775 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
776 uint8_t *lum_m2, uint8_t *lum_m1,
777 uint8_t *lum, int size)
779 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
782 for(;size > 0;size--) {
784 sum += lum_m3[0] << 2;
785 sum += lum_m2[0] << 1;
787 sum += lum_m1[0] << 2;
789 lum_m2[0] = cm[(sum + 4) >> 3];
799 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
800 top field is copied as is, but the bottom field is deinterlaced
801 against the top field. */
802 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
803 const uint8_t *src1, int src_wrap,
804 int width, int height)
806 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
811 src_0=&src_m1[src_wrap];
812 src_p1=&src_0[src_wrap];
813 src_p2=&src_p1[src_wrap];
814 for(y=0;y<(height-2);y+=2) {
815 memcpy(dst,src_m1,width);
817 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
821 src_p1 += 2*src_wrap;
822 src_p2 += 2*src_wrap;
825 memcpy(dst,src_m1,width);
828 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
831 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
832 int width, int height)
834 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
837 buf = av_malloc(width);
840 memcpy(buf,src_m1,width);
841 src_0=&src_m1[src_wrap];
842 src_p1=&src_0[src_wrap];
843 src_p2=&src_p1[src_wrap];
844 for(y=0;y<(height-2);y+=2) {
845 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
848 src_p1 += 2*src_wrap;
849 src_p2 += 2*src_wrap;
852 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
856 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
857 enum PixelFormat pix_fmt, int width, int height)
861 if (pix_fmt != PIX_FMT_YUV420P &&
862 pix_fmt != PIX_FMT_YUVJ420P &&
863 pix_fmt != PIX_FMT_YUV422P &&
864 pix_fmt != PIX_FMT_YUVJ422P &&
865 pix_fmt != PIX_FMT_YUV444P &&
866 pix_fmt != PIX_FMT_YUV411P &&
867 pix_fmt != PIX_FMT_GRAY8)
869 if ((width & 3) != 0 || (height & 3) != 0)
875 case PIX_FMT_YUVJ420P:
876 case PIX_FMT_YUV420P:
880 case PIX_FMT_YUV422P:
881 case PIX_FMT_YUVJ422P:
884 case PIX_FMT_YUV411P:
890 if (pix_fmt == PIX_FMT_GRAY8) {
895 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
898 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
899 src->data[i], src->linesize[i],