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] = {
112 .color_type = FF_COLOR_YUV,
116 [PIX_FMT_YUVJ420P] = {
117 .color_type = FF_COLOR_YUV_JPEG,
119 [PIX_FMT_YUVJ422P] = {
120 .color_type = FF_COLOR_YUV_JPEG,
122 [PIX_FMT_YUVJ444P] = {
123 .color_type = FF_COLOR_YUV_JPEG,
125 [PIX_FMT_YUVJ440P] = {
126 .color_type = FF_COLOR_YUV_JPEG,
131 .color_type = FF_COLOR_RGB,
134 .color_type = FF_COLOR_RGB,
138 .color_type = FF_COLOR_RGB,
140 [PIX_FMT_RGB48BE] = {
141 .color_type = FF_COLOR_RGB,
143 [PIX_FMT_RGB48LE] = {
144 .color_type = FF_COLOR_RGB,
146 [PIX_FMT_RGB565BE] = {
147 .color_type = FF_COLOR_RGB,
149 [PIX_FMT_RGB565LE] = {
150 .color_type = FF_COLOR_RGB,
152 [PIX_FMT_RGB555BE] = {
153 .color_type = FF_COLOR_RGB,
156 [PIX_FMT_RGB555LE] = {
157 .color_type = FF_COLOR_RGB,
160 [PIX_FMT_RGB444BE] = {
161 .color_type = FF_COLOR_RGB,
164 [PIX_FMT_RGB444LE] = {
165 .color_type = FF_COLOR_RGB,
169 /* gray / mono formats */
170 [PIX_FMT_GRAY16BE] = {
171 .color_type = FF_COLOR_GRAY,
173 [PIX_FMT_GRAY16LE] = {
174 .color_type = FF_COLOR_GRAY,
177 .color_type = FF_COLOR_GRAY,
179 [PIX_FMT_MONOWHITE] = {
180 .color_type = FF_COLOR_GRAY,
182 [PIX_FMT_MONOBLACK] = {
183 .color_type = FF_COLOR_GRAY,
186 /* paletted formats */
189 .color_type = FF_COLOR_RGB,
191 [PIX_FMT_UYYVYY411] = {
192 .color_type = FF_COLOR_YUV,
196 .color_type = FF_COLOR_RGB,
198 [PIX_FMT_BGR565BE] = {
199 .color_type = FF_COLOR_RGB,
202 [PIX_FMT_BGR565LE] = {
203 .color_type = FF_COLOR_RGB,
206 [PIX_FMT_BGR555BE] = {
207 .color_type = FF_COLOR_RGB,
210 [PIX_FMT_BGR555LE] = {
211 .color_type = FF_COLOR_RGB,
214 [PIX_FMT_BGR444BE] = {
215 .color_type = FF_COLOR_RGB,
218 [PIX_FMT_BGR444LE] = {
219 .color_type = FF_COLOR_RGB,
223 .color_type = FF_COLOR_RGB,
226 .color_type = FF_COLOR_RGB,
228 [PIX_FMT_RGB4_BYTE] = {
229 .color_type = FF_COLOR_RGB,
233 .color_type = FF_COLOR_RGB,
236 .color_type = FF_COLOR_RGB,
238 [PIX_FMT_BGR4_BYTE] = {
239 .color_type = FF_COLOR_RGB,
243 .color_type = FF_COLOR_YUV,
246 .color_type = FF_COLOR_YUV,
251 .color_type = FF_COLOR_RGB,
255 .color_type = FF_COLOR_RGB,
259 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
261 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
262 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
265 #if FF_API_GET_PIX_FMT_NAME
266 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
268 return av_get_pix_fmt_name(pix_fmt);
272 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
274 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
277 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
278 enum PixelFormat pix_fmt, int width, int height)
282 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
285 if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
288 return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
291 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
292 unsigned char *dest, int dest_size)
294 int i, j, nb_planes = 0, linesizes[4];
295 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
296 int size = avpicture_get_size(pix_fmt, width, height);
298 if (size > dest_size || size < 0)
299 return AVERROR(EINVAL);
301 for (i = 0; i < desc->nb_components; i++)
302 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
305 av_image_fill_linesizes(linesizes, pix_fmt, width);
306 for (i = 0; i < nb_planes; i++) {
307 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
308 const unsigned char *s = src->data[i];
309 h = (height + (1 << shift) - 1) >> shift;
311 for (j = 0; j < h; j++) {
312 memcpy(dest, s, linesizes[i]);
313 dest += linesizes[i];
314 s += src->linesize[i];
318 if (desc->flags & PIX_FMT_PAL)
319 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
324 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
326 AVPicture dummy_pict;
327 if(av_image_check_size(width, height, 0, NULL))
332 case PIX_FMT_RGB4_BYTE:
333 case PIX_FMT_BGR4_BYTE:
335 // do not include palette for these pseudo-paletted formats
336 return width * height;
338 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
341 static int get_pix_fmt_depth(int *min, int *max, enum PixelFormat pix_fmt)
343 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
346 if (!desc->nb_components) {
348 return AVERROR(EINVAL);
351 *min = INT_MAX, *max = -INT_MAX;
352 for (i = 0; i < desc->nb_components; i++) {
353 *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
354 *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
359 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
362 const PixFmtInfo *pf, *ps;
363 const AVPixFmtDescriptor *src_desc;
364 const AVPixFmtDescriptor *dst_desc;
365 int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
368 if (dst_pix_fmt >= PIX_FMT_NB || dst_pix_fmt <= PIX_FMT_NONE)
371 src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
372 dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
373 ps = &pix_fmt_info[src_pix_fmt];
378 if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
380 if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
382 if (dst_min_depth < src_min_depth ||
383 dst_max_depth < src_max_depth)
384 loss |= FF_LOSS_DEPTH;
385 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
386 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
387 loss |= FF_LOSS_RESOLUTION;
389 pf = &pix_fmt_info[dst_pix_fmt];
390 switch(pf->color_type) {
392 if (ps->color_type != FF_COLOR_RGB &&
393 ps->color_type != FF_COLOR_GRAY)
394 loss |= FF_LOSS_COLORSPACE;
397 if (ps->color_type != FF_COLOR_GRAY)
398 loss |= FF_LOSS_COLORSPACE;
401 if (ps->color_type != FF_COLOR_YUV)
402 loss |= FF_LOSS_COLORSPACE;
404 case FF_COLOR_YUV_JPEG:
405 if (ps->color_type != FF_COLOR_YUV_JPEG &&
406 ps->color_type != FF_COLOR_YUV &&
407 ps->color_type != FF_COLOR_GRAY)
408 loss |= FF_LOSS_COLORSPACE;
412 if (ps->color_type != pf->color_type)
413 loss |= FF_LOSS_COLORSPACE;
416 if (pf->color_type == FF_COLOR_GRAY &&
417 ps->color_type != FF_COLOR_GRAY)
418 loss |= FF_LOSS_CHROMA;
419 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
420 loss |= FF_LOSS_ALPHA;
421 if (dst_pix_fmt == PIX_FMT_PAL8 &&
422 (src_pix_fmt != PIX_FMT_PAL8 && ps->color_type != FF_COLOR_GRAY))
423 loss |= FF_LOSS_COLORQUANT;
427 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
429 const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
430 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
432 return info->padded_size ?
433 info->padded_size : av_get_bits_per_pixel(desc);
436 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
437 int has_alpha, int *loss_ptr)
439 enum PixelFormat dst_pix_fmt;
442 if (loss_ptr) /* all losses count (for backward compatibility) */
445 dst_pix_fmt = PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
446 for(i = 0; i< FFMIN(PIX_FMT_NB, 64); i++){
447 if (pix_fmt_mask & (1ULL << i))
448 dst_pix_fmt = avcodec_find_best_pix_fmt2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
453 enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
454 enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
456 enum PixelFormat dst_pix_fmt;
457 int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
458 static const int loss_mask_order[] = {
459 ~0, /* no loss first */
462 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
465 ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
466 FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
467 0x80000, //non zero entry that combines all loss variants including future additions
471 loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
472 dst_pix_fmt = PIX_FMT_NONE;
473 loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
474 loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
476 /* try with successive loss */
477 for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
478 loss_order1 = loss1 & loss_mask_order[i];
479 loss_order2 = loss2 & loss_mask_order[i];
481 if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
482 dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
483 } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
484 dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
489 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
493 void av_picture_copy(AVPicture *dst, const AVPicture *src,
494 enum PixelFormat pix_fmt, int width, int height)
496 av_image_copy(dst->data, dst->linesize, src->data,
497 src->linesize, pix_fmt, width, height);
501 void ff_shrink22(uint8_t *dst, int dst_wrap,
502 const uint8_t *src, int src_wrap,
503 int width, int height)
506 const uint8_t *s1, *s2;
509 for(;height > 0; height--) {
513 for(w = width;w >= 4; w-=4) {
514 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
515 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
516 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
517 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
523 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
534 void ff_shrink44(uint8_t *dst, int dst_wrap,
535 const uint8_t *src, int src_wrap,
536 int width, int height)
539 const uint8_t *s1, *s2, *s3, *s4;
542 for(;height > 0; height--) {
548 for(w = width;w > 0; w--) {
549 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
550 s2[0] + s2[1] + s2[2] + s2[3] +
551 s3[0] + s3[1] + s3[2] + s3[3] +
552 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
565 void ff_shrink88(uint8_t *dst, int dst_wrap,
566 const uint8_t *src, int src_wrap,
567 int width, int height)
571 for(;height > 0; height--) {
572 for(w = width;w > 0; w--) {
575 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
578 *(dst++) = (tmp + 32)>>6;
579 src += 8 - 8*src_wrap;
581 src += 8*src_wrap - 8*width;
582 dst += dst_wrap - width;
587 int avpicture_alloc(AVPicture *picture,
588 enum PixelFormat pix_fmt, int width, int height)
592 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
593 memset(picture, 0, sizeof(AVPicture));
600 void avpicture_free(AVPicture *picture)
602 av_free(picture->data[0]);
605 /* return true if yuv planar */
606 static inline int is_yuv_planar(enum PixelFormat fmt)
608 const PixFmtInfo *info = &pix_fmt_info[fmt];
609 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
611 int planes[4] = { 0 };
613 if (info->color_type != FF_COLOR_YUV &&
614 info->color_type != FF_COLOR_YUV_JPEG)
617 /* set the used planes */
618 for (i = 0; i < desc->nb_components; i++)
619 planes[desc->comp[i].plane] = 1;
621 /* if there is an unused plane, the format is not planar */
622 for (i = 0; i < desc->nb_components; i++)
628 int av_picture_crop(AVPicture *dst, const AVPicture *src,
629 enum PixelFormat pix_fmt, int top_band, int left_band)
634 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
637 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
638 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
640 if (is_yuv_planar(pix_fmt)) {
641 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
642 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
643 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
645 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
647 if(left_band) //FIXME add support for this too
649 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
652 dst->linesize[0] = src->linesize[0];
653 dst->linesize[1] = src->linesize[1];
654 dst->linesize[2] = src->linesize[2];
658 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
659 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
668 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
669 !is_yuv_planar(pix_fmt)) return -1;
671 for (i = 0; i < 3; i++) {
672 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
673 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
675 if (padtop || padleft) {
676 memset(dst->data[i], color[i],
677 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
680 if (padleft || padright) {
681 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
682 (dst->linesize[i] - (padright >> x_shift));
683 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
684 for (y = 0; y < yheight; y++) {
685 memset(optr, color[i], (padleft + padright) >> x_shift);
686 optr += dst->linesize[i];
690 if (src) { /* first line */
691 uint8_t *iptr = src->data[i];
692 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
693 (padleft >> x_shift);
694 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
695 iptr += src->linesize[i];
696 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
697 (dst->linesize[i] - (padright >> x_shift));
698 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
699 for (y = 0; y < yheight; y++) {
700 memset(optr, color[i], (padleft + padright) >> x_shift);
701 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
702 (width - padleft - padright) >> x_shift);
703 iptr += src->linesize[i];
704 optr += dst->linesize[i];
708 if (padbottom || padright) {
709 optr = dst->data[i] + dst->linesize[i] *
710 ((height - padbottom) >> y_shift) - (padright >> x_shift);
711 memset(optr, color[i],dst->linesize[i] *
712 (padbottom >> y_shift) + (padright >> x_shift));
718 /* NOTE: we scan all the pixels to have an exact information */
719 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
721 const unsigned char *p;
722 int src_wrap, ret, x, y;
724 uint32_t *palette = (uint32_t *)src->data[1];
727 src_wrap = src->linesize[0] - width;
729 for(y=0;y<height;y++) {
730 for(x=0;x<width;x++) {
731 a = palette[p[0]] >> 24;
733 ret |= FF_ALPHA_TRANSP;
734 } else if (a != 0xff) {
735 ret |= FF_ALPHA_SEMI_TRANSP;
744 int img_get_alpha_info(const AVPicture *src,
745 enum PixelFormat pix_fmt, int width, int height)
747 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
750 /* no alpha can be represented in format */
755 ret = get_alpha_info_pal8(src, width, height);
758 /* we do not know, so everything is indicated */
759 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
765 #if !(HAVE_MMX && HAVE_YASM)
766 /* filter parameters: [-1 4 2 4 -1] // 8 */
767 static void deinterlace_line_c(uint8_t *dst,
768 const uint8_t *lum_m4, const uint8_t *lum_m3,
769 const uint8_t *lum_m2, const uint8_t *lum_m1,
773 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
776 for(;size > 0;size--) {
778 sum += lum_m3[0] << 2;
779 sum += lum_m2[0] << 1;
780 sum += lum_m1[0] << 2;
782 dst[0] = cm[(sum + 4) >> 3];
792 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
793 uint8_t *lum_m2, uint8_t *lum_m1,
794 uint8_t *lum, int size)
796 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
799 for(;size > 0;size--) {
801 sum += lum_m3[0] << 2;
802 sum += lum_m2[0] << 1;
804 sum += lum_m1[0] << 2;
806 lum_m2[0] = cm[(sum + 4) >> 3];
816 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
817 top field is copied as is, but the bottom field is deinterlaced
818 against the top field. */
819 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
820 const uint8_t *src1, int src_wrap,
821 int width, int height)
823 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
828 src_0=&src_m1[src_wrap];
829 src_p1=&src_0[src_wrap];
830 src_p2=&src_p1[src_wrap];
831 for(y=0;y<(height-2);y+=2) {
832 memcpy(dst,src_m1,width);
834 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
838 src_p1 += 2*src_wrap;
839 src_p2 += 2*src_wrap;
842 memcpy(dst,src_m1,width);
845 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
848 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
849 int width, int height)
851 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
854 buf = (uint8_t*)av_malloc(width);
857 memcpy(buf,src_m1,width);
858 src_0=&src_m1[src_wrap];
859 src_p1=&src_0[src_wrap];
860 src_p2=&src_p1[src_wrap];
861 for(y=0;y<(height-2);y+=2) {
862 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
865 src_p1 += 2*src_wrap;
866 src_p2 += 2*src_wrap;
869 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
873 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
874 enum PixelFormat pix_fmt, int width, int height)
878 if (pix_fmt != PIX_FMT_YUV420P &&
879 pix_fmt != PIX_FMT_YUVJ420P &&
880 pix_fmt != PIX_FMT_YUV422P &&
881 pix_fmt != PIX_FMT_YUVJ422P &&
882 pix_fmt != PIX_FMT_YUV444P &&
883 pix_fmt != PIX_FMT_YUV411P &&
884 pix_fmt != PIX_FMT_GRAY8)
886 if ((width & 3) != 0 || (height & 3) != 0)
892 case PIX_FMT_YUVJ420P:
893 case PIX_FMT_YUV420P:
897 case PIX_FMT_YUV422P:
898 case PIX_FMT_YUVJ422P:
901 case PIX_FMT_YUV411P:
907 if (pix_fmt == PIX_FMT_GRAY8) {
912 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
915 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
916 src->data[i], src->linesize[i],