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 #define pixdesc_has_alpha(pixdesc) \
59 ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & PIX_FMT_PAL)
61 typedef struct PixFmtInfo {
62 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
63 uint8_t padded_size; /**< padded size in bits if different from the non-padded size */
66 /* this table gives more information about formats */
67 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
70 .color_type = FF_COLOR_YUV,
73 .color_type = FF_COLOR_YUV,
76 .color_type = FF_COLOR_YUV,
79 .color_type = FF_COLOR_YUV,
82 .color_type = FF_COLOR_YUV,
85 .color_type = FF_COLOR_YUV,
88 .color_type = FF_COLOR_YUV,
91 .color_type = FF_COLOR_YUV,
93 [PIX_FMT_YUV420P16LE] = {
94 .color_type = FF_COLOR_YUV,
96 [PIX_FMT_YUV422P16LE] = {
97 .color_type = FF_COLOR_YUV,
99 [PIX_FMT_YUV444P16LE] = {
100 .color_type = FF_COLOR_YUV,
102 [PIX_FMT_YUV420P16BE] = {
103 .color_type = FF_COLOR_YUV,
105 [PIX_FMT_YUV422P16BE] = {
106 .color_type = FF_COLOR_YUV,
108 [PIX_FMT_YUV444P16BE] = {
109 .color_type = FF_COLOR_YUV,
112 /* YUV formats with alpha plane */
113 [PIX_FMT_YUVA420P] = {
114 .color_type = FF_COLOR_YUV,
117 [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,
143 .color_type = FF_COLOR_RGB,
145 [PIX_FMT_RGB48BE] = {
146 .color_type = FF_COLOR_RGB,
148 [PIX_FMT_RGB48LE] = {
149 .color_type = FF_COLOR_RGB,
151 [PIX_FMT_RGBA64BE] = {
152 .color_type = FF_COLOR_RGB,
154 [PIX_FMT_RGBA64LE] = {
155 .color_type = FF_COLOR_RGB,
157 [PIX_FMT_RGB565BE] = {
158 .color_type = FF_COLOR_RGB,
160 [PIX_FMT_RGB565LE] = {
161 .color_type = FF_COLOR_RGB,
163 [PIX_FMT_RGB555BE] = {
164 .color_type = FF_COLOR_RGB,
167 [PIX_FMT_RGB555LE] = {
168 .color_type = FF_COLOR_RGB,
171 [PIX_FMT_RGB444BE] = {
172 .color_type = FF_COLOR_RGB,
175 [PIX_FMT_RGB444LE] = {
176 .color_type = FF_COLOR_RGB,
180 /* gray / mono formats */
181 [PIX_FMT_GRAY16BE] = {
182 .color_type = FF_COLOR_GRAY,
184 [PIX_FMT_GRAY16LE] = {
185 .color_type = FF_COLOR_GRAY,
188 .color_type = FF_COLOR_GRAY,
191 .color_type = FF_COLOR_GRAY,
193 [PIX_FMT_MONOWHITE] = {
194 .color_type = FF_COLOR_GRAY,
196 [PIX_FMT_MONOBLACK] = {
197 .color_type = FF_COLOR_GRAY,
200 /* paletted formats */
202 .color_type = FF_COLOR_RGB,
204 [PIX_FMT_UYYVYY411] = {
205 .color_type = FF_COLOR_YUV,
208 .color_type = FF_COLOR_RGB,
210 [PIX_FMT_BGR48BE] = {
211 .color_type = FF_COLOR_RGB,
213 [PIX_FMT_BGR48LE] = {
214 .color_type = FF_COLOR_RGB,
216 [PIX_FMT_BGRA64BE] = {
217 .color_type = FF_COLOR_RGB,
219 [PIX_FMT_BGRA64LE] = {
220 .color_type = FF_COLOR_RGB,
222 [PIX_FMT_BGR565BE] = {
223 .color_type = FF_COLOR_RGB,
226 [PIX_FMT_BGR565LE] = {
227 .color_type = FF_COLOR_RGB,
230 [PIX_FMT_BGR555BE] = {
231 .color_type = FF_COLOR_RGB,
234 [PIX_FMT_BGR555LE] = {
235 .color_type = FF_COLOR_RGB,
238 [PIX_FMT_BGR444BE] = {
239 .color_type = FF_COLOR_RGB,
242 [PIX_FMT_BGR444LE] = {
243 .color_type = FF_COLOR_RGB,
247 .color_type = FF_COLOR_RGB,
250 .color_type = FF_COLOR_RGB,
252 [PIX_FMT_RGB4_BYTE] = {
253 .color_type = FF_COLOR_RGB,
257 .color_type = FF_COLOR_RGB,
260 .color_type = FF_COLOR_RGB,
262 [PIX_FMT_BGR4_BYTE] = {
263 .color_type = FF_COLOR_RGB,
267 .color_type = FF_COLOR_YUV,
270 .color_type = FF_COLOR_YUV,
274 .color_type = FF_COLOR_RGB,
277 .color_type = FF_COLOR_RGB,
281 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
283 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
284 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
287 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
289 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
292 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
293 enum PixelFormat pix_fmt, int width, int height)
297 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
300 if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
303 return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
306 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
307 unsigned char *dest, int dest_size)
309 int i, j, nb_planes = 0, linesizes[4];
310 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
311 int size = avpicture_get_size(pix_fmt, width, height);
313 if (size > dest_size || size < 0)
314 return AVERROR(EINVAL);
316 for (i = 0; i < desc->nb_components; i++)
317 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
320 av_image_fill_linesizes(linesizes, pix_fmt, width);
321 for (i = 0; i < nb_planes; i++) {
322 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
323 const unsigned char *s = src->data[i];
324 h = (height + (1 << shift) - 1) >> shift;
326 for (j = 0; j < h; j++) {
327 memcpy(dest, s, linesizes[i]);
328 dest += linesizes[i];
329 s += src->linesize[i];
336 case PIX_FMT_RGB4_BYTE:
337 case PIX_FMT_BGR4_BYTE:
339 // do not include palette for these pseudo-paletted formats
343 if (desc->flags & PIX_FMT_PAL) {
344 uint32_t *d32 = (unsigned char *)(((size_t)dest + 3) & ~3);
345 for (i = 0; i<256; i++)
346 AV_WL32(d32 + i, AV_RN32(src->data[1] + 4*i));
352 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
354 AVPicture dummy_pict;
355 if(av_image_check_size(width, height, 0, NULL))
357 if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
358 // do not include palette for these pseudo-paletted formats
359 return width * height;
360 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
363 static int get_pix_fmt_depth(int *min, int *max, enum PixelFormat pix_fmt)
365 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
368 if (!desc->nb_components) {
370 return AVERROR(EINVAL);
373 *min = INT_MAX, *max = -INT_MAX;
374 for (i = 0; i < desc->nb_components; i++) {
375 *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
376 *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
381 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
384 const PixFmtInfo *pf, *ps;
385 const AVPixFmtDescriptor *src_desc;
386 const AVPixFmtDescriptor *dst_desc;
387 int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
390 if (dst_pix_fmt >= PIX_FMT_NB || dst_pix_fmt <= PIX_FMT_NONE)
393 src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
394 dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
395 ps = &pix_fmt_info[src_pix_fmt];
400 if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
402 if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
404 if (dst_min_depth < src_min_depth ||
405 dst_max_depth < src_max_depth)
406 loss |= FF_LOSS_DEPTH;
407 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
408 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
409 loss |= FF_LOSS_RESOLUTION;
411 pf = &pix_fmt_info[dst_pix_fmt];
412 switch(pf->color_type) {
414 if (ps->color_type != FF_COLOR_RGB &&
415 ps->color_type != FF_COLOR_GRAY)
416 loss |= FF_LOSS_COLORSPACE;
419 if (ps->color_type != FF_COLOR_GRAY)
420 loss |= FF_LOSS_COLORSPACE;
423 if (ps->color_type != FF_COLOR_YUV)
424 loss |= FF_LOSS_COLORSPACE;
426 case FF_COLOR_YUV_JPEG:
427 if (ps->color_type != FF_COLOR_YUV_JPEG &&
428 ps->color_type != FF_COLOR_YUV &&
429 ps->color_type != FF_COLOR_GRAY)
430 loss |= FF_LOSS_COLORSPACE;
434 if (ps->color_type != pf->color_type)
435 loss |= FF_LOSS_COLORSPACE;
438 if (pf->color_type == FF_COLOR_GRAY &&
439 ps->color_type != FF_COLOR_GRAY)
440 loss |= FF_LOSS_CHROMA;
441 if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
442 loss |= FF_LOSS_ALPHA;
443 if (dst_pix_fmt == PIX_FMT_PAL8 &&
444 (src_pix_fmt != PIX_FMT_PAL8 && (ps->color_type != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
445 loss |= FF_LOSS_COLORQUANT;
450 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
452 const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
453 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
455 return info->padded_size ?
456 info->padded_size : av_get_bits_per_pixel(desc);
459 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
460 int has_alpha, int *loss_ptr)
462 enum PixelFormat dst_pix_fmt;
465 if (loss_ptr) /* all losses count (for backward compatibility) */
468 dst_pix_fmt = PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
469 for(i = 0; i< FFMIN(PIX_FMT_NB, 64); i++){
470 if (pix_fmt_mask & (1ULL << i))
471 dst_pix_fmt = avcodec_find_best_pix_fmt2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
476 enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
477 enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
479 enum PixelFormat dst_pix_fmt;
480 int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
481 static const int loss_mask_order[] = {
482 ~0, /* no loss first */
485 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
488 ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
489 FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
490 0x80000, //non zero entry that combines all loss variants including future additions
494 loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
495 dst_pix_fmt = PIX_FMT_NONE;
496 loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
497 loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
499 /* try with successive loss */
500 for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
501 loss_order1 = loss1 & loss_mask_order[i];
502 loss_order2 = loss2 & loss_mask_order[i];
504 if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
505 dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
506 } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
507 dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
512 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
516 void av_picture_copy(AVPicture *dst, const AVPicture *src,
517 enum PixelFormat pix_fmt, int width, int height)
519 av_image_copy(dst->data, dst->linesize, src->data,
520 src->linesize, pix_fmt, width, height);
524 void ff_shrink22(uint8_t *dst, int dst_wrap,
525 const uint8_t *src, int src_wrap,
526 int width, int height)
529 const uint8_t *s1, *s2;
532 for(;height > 0; height--) {
536 for(w = width;w >= 4; w-=4) {
537 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
538 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
539 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
540 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
546 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
557 void ff_shrink44(uint8_t *dst, int dst_wrap,
558 const uint8_t *src, int src_wrap,
559 int width, int height)
562 const uint8_t *s1, *s2, *s3, *s4;
565 for(;height > 0; height--) {
571 for(w = width;w > 0; w--) {
572 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
573 s2[0] + s2[1] + s2[2] + s2[3] +
574 s3[0] + s3[1] + s3[2] + s3[3] +
575 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
588 void ff_shrink88(uint8_t *dst, int dst_wrap,
589 const uint8_t *src, int src_wrap,
590 int width, int height)
594 for(;height > 0; height--) {
595 for(w = width;w > 0; w--) {
598 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
601 *(dst++) = (tmp + 32)>>6;
602 src += 8 - 8*src_wrap;
604 src += 8*src_wrap - 8*width;
605 dst += dst_wrap - width;
610 int avpicture_alloc(AVPicture *picture,
611 enum PixelFormat pix_fmt, int width, int height)
615 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
616 memset(picture, 0, sizeof(AVPicture));
623 void avpicture_free(AVPicture *picture)
625 av_free(picture->data[0]);
628 /* return true if yuv planar */
629 static inline int is_yuv_planar(enum PixelFormat fmt)
631 const PixFmtInfo *info = &pix_fmt_info[fmt];
632 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
634 int planes[4] = { 0 };
636 if (info->color_type != FF_COLOR_YUV &&
637 info->color_type != FF_COLOR_YUV_JPEG)
640 /* set the used planes */
641 for (i = 0; i < desc->nb_components; i++)
642 planes[desc->comp[i].plane] = 1;
644 /* if there is an unused plane, the format is not planar */
645 for (i = 0; i < desc->nb_components; i++)
651 int av_picture_crop(AVPicture *dst, const AVPicture *src,
652 enum PixelFormat pix_fmt, int top_band, int left_band)
657 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
660 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
661 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
663 if (is_yuv_planar(pix_fmt)) {
664 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
665 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
666 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
668 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
670 if(left_band) //FIXME add support for this too
672 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
675 dst->linesize[0] = src->linesize[0];
676 dst->linesize[1] = src->linesize[1];
677 dst->linesize[2] = src->linesize[2];
681 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
682 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
691 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
692 !is_yuv_planar(pix_fmt)) return -1;
694 for (i = 0; i < 3; i++) {
695 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
696 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
698 if (padtop || padleft) {
699 memset(dst->data[i], color[i],
700 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
703 if (padleft || padright) {
704 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
705 (dst->linesize[i] - (padright >> x_shift));
706 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
707 for (y = 0; y < yheight; y++) {
708 memset(optr, color[i], (padleft + padright) >> x_shift);
709 optr += dst->linesize[i];
713 if (src) { /* first line */
714 uint8_t *iptr = src->data[i];
715 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
716 (padleft >> x_shift);
717 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
718 iptr += src->linesize[i];
719 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
720 (dst->linesize[i] - (padright >> x_shift));
721 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
722 for (y = 0; y < yheight; y++) {
723 memset(optr, color[i], (padleft + padright) >> x_shift);
724 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
725 (width - padleft - padright) >> x_shift);
726 iptr += src->linesize[i];
727 optr += dst->linesize[i];
731 if (padbottom || padright) {
732 optr = dst->data[i] + dst->linesize[i] *
733 ((height - padbottom) >> y_shift) - (padright >> x_shift);
734 memset(optr, color[i],dst->linesize[i] *
735 (padbottom >> y_shift) + (padright >> x_shift));
741 #if !(HAVE_MMX && HAVE_YASM)
742 /* filter parameters: [-1 4 2 4 -1] // 8 */
743 static void deinterlace_line_c(uint8_t *dst,
744 const uint8_t *lum_m4, const uint8_t *lum_m3,
745 const uint8_t *lum_m2, const uint8_t *lum_m1,
749 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
752 for(;size > 0;size--) {
754 sum += lum_m3[0] << 2;
755 sum += lum_m2[0] << 1;
756 sum += lum_m1[0] << 2;
758 dst[0] = cm[(sum + 4) >> 3];
768 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
769 uint8_t *lum_m2, uint8_t *lum_m1,
770 uint8_t *lum, int size)
772 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
775 for(;size > 0;size--) {
777 sum += lum_m3[0] << 2;
778 sum += lum_m2[0] << 1;
780 sum += lum_m1[0] << 2;
782 lum_m2[0] = cm[(sum + 4) >> 3];
792 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
793 top field is copied as is, but the bottom field is deinterlaced
794 against the top field. */
795 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
796 const uint8_t *src1, int src_wrap,
797 int width, int height)
799 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
804 src_0=&src_m1[src_wrap];
805 src_p1=&src_0[src_wrap];
806 src_p2=&src_p1[src_wrap];
807 for(y=0;y<(height-2);y+=2) {
808 memcpy(dst,src_m1,width);
810 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
814 src_p1 += 2*src_wrap;
815 src_p2 += 2*src_wrap;
818 memcpy(dst,src_m1,width);
821 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
824 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
825 int width, int height)
827 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
830 buf = av_malloc(width);
833 memcpy(buf,src_m1,width);
834 src_0=&src_m1[src_wrap];
835 src_p1=&src_0[src_wrap];
836 src_p2=&src_p1[src_wrap];
837 for(y=0;y<(height-2);y+=2) {
838 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
841 src_p1 += 2*src_wrap;
842 src_p2 += 2*src_wrap;
845 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
849 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
850 enum PixelFormat pix_fmt, int width, int height)
854 if (pix_fmt != PIX_FMT_YUV420P &&
855 pix_fmt != PIX_FMT_YUVJ420P &&
856 pix_fmt != PIX_FMT_YUV422P &&
857 pix_fmt != PIX_FMT_YUVJ422P &&
858 pix_fmt != PIX_FMT_YUV444P &&
859 pix_fmt != PIX_FMT_YUV411P &&
860 pix_fmt != PIX_FMT_GRAY8)
862 if ((width & 3) != 0 || (height & 3) != 0)
868 case PIX_FMT_YUVJ420P:
869 case PIX_FMT_YUV420P:
873 case PIX_FMT_YUV422P:
874 case PIX_FMT_YUVJ422P:
877 case PIX_FMT_YUV411P:
883 if (pix_fmt == PIX_FMT_GRAY8) {
888 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
891 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
892 src->data[i], src->linesize[i],