2 * Misc image conversion routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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/common.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
43 #include "x86/dsputil_mmx.h"
46 #define FF_COLOR_RGB 0 /**< RGB color space */
47 #define FF_COLOR_GRAY 1 /**< gray color space */
48 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
49 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
51 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
52 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
53 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
56 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
57 #define deinterlace_line ff_deinterlace_line_mmx
59 #define deinterlace_line_inplace deinterlace_line_inplace_c
60 #define deinterlace_line deinterlace_line_c
63 typedef struct PixFmtInfo {
64 uint8_t nb_channels; /**< number of channels (including alpha) */
65 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
66 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
67 uint8_t is_alpha : 1; /**< true if alpha can be specified */
68 uint8_t depth; /**< bit depth of the color components */
71 /* this table gives more information about formats */
72 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
76 .color_type = FF_COLOR_YUV,
77 .pixel_type = FF_PIXEL_PLANAR,
82 .color_type = FF_COLOR_YUV,
83 .pixel_type = FF_PIXEL_PLANAR,
88 .color_type = FF_COLOR_YUV,
89 .pixel_type = FF_PIXEL_PLANAR,
94 .color_type = FF_COLOR_YUV,
95 .pixel_type = FF_PIXEL_PACKED,
100 .color_type = FF_COLOR_YUV,
101 .pixel_type = FF_PIXEL_PACKED,
104 [PIX_FMT_YUV410P] = {
106 .color_type = FF_COLOR_YUV,
107 .pixel_type = FF_PIXEL_PLANAR,
110 [PIX_FMT_YUV411P] = {
112 .color_type = FF_COLOR_YUV,
113 .pixel_type = FF_PIXEL_PLANAR,
116 [PIX_FMT_YUV440P] = {
118 .color_type = FF_COLOR_YUV,
119 .pixel_type = FF_PIXEL_PLANAR,
122 [PIX_FMT_YUV420P16LE] = {
124 .color_type = FF_COLOR_YUV,
125 .pixel_type = FF_PIXEL_PLANAR,
128 [PIX_FMT_YUV422P16LE] = {
130 .color_type = FF_COLOR_YUV,
131 .pixel_type = FF_PIXEL_PLANAR,
134 [PIX_FMT_YUV444P16LE] = {
136 .color_type = FF_COLOR_YUV,
137 .pixel_type = FF_PIXEL_PLANAR,
140 [PIX_FMT_YUV420P16BE] = {
142 .color_type = FF_COLOR_YUV,
143 .pixel_type = FF_PIXEL_PLANAR,
146 [PIX_FMT_YUV422P16BE] = {
148 .color_type = FF_COLOR_YUV,
149 .pixel_type = FF_PIXEL_PLANAR,
152 [PIX_FMT_YUV444P16BE] = {
154 .color_type = FF_COLOR_YUV,
155 .pixel_type = FF_PIXEL_PLANAR,
160 /* YUV formats with alpha plane */
161 [PIX_FMT_YUVA420P] = {
163 .color_type = FF_COLOR_YUV,
164 .pixel_type = FF_PIXEL_PLANAR,
169 [PIX_FMT_YUVJ420P] = {
171 .color_type = FF_COLOR_YUV_JPEG,
172 .pixel_type = FF_PIXEL_PLANAR,
175 [PIX_FMT_YUVJ422P] = {
177 .color_type = FF_COLOR_YUV_JPEG,
178 .pixel_type = FF_PIXEL_PLANAR,
181 [PIX_FMT_YUVJ444P] = {
183 .color_type = FF_COLOR_YUV_JPEG,
184 .pixel_type = FF_PIXEL_PLANAR,
187 [PIX_FMT_YUVJ440P] = {
189 .color_type = FF_COLOR_YUV_JPEG,
190 .pixel_type = FF_PIXEL_PLANAR,
197 .color_type = FF_COLOR_RGB,
198 .pixel_type = FF_PIXEL_PACKED,
203 .color_type = FF_COLOR_RGB,
204 .pixel_type = FF_PIXEL_PACKED,
208 .nb_channels = 4, .is_alpha = 1,
209 .color_type = FF_COLOR_RGB,
210 .pixel_type = FF_PIXEL_PACKED,
213 [PIX_FMT_RGB48BE] = {
215 .color_type = FF_COLOR_RGB,
216 .pixel_type = FF_PIXEL_PACKED,
219 [PIX_FMT_RGB48LE] = {
221 .color_type = FF_COLOR_RGB,
222 .pixel_type = FF_PIXEL_PACKED,
225 [PIX_FMT_RGB565BE] = {
227 .color_type = FF_COLOR_RGB,
228 .pixel_type = FF_PIXEL_PACKED,
231 [PIX_FMT_RGB565LE] = {
233 .color_type = FF_COLOR_RGB,
234 .pixel_type = FF_PIXEL_PACKED,
237 [PIX_FMT_RGB555BE] = {
239 .color_type = FF_COLOR_RGB,
240 .pixel_type = FF_PIXEL_PACKED,
243 [PIX_FMT_RGB555LE] = {
245 .color_type = FF_COLOR_RGB,
246 .pixel_type = FF_PIXEL_PACKED,
249 [PIX_FMT_RGB444BE] = {
251 .color_type = FF_COLOR_RGB,
252 .pixel_type = FF_PIXEL_PACKED,
255 [PIX_FMT_RGB444LE] = {
257 .color_type = FF_COLOR_RGB,
258 .pixel_type = FF_PIXEL_PACKED,
262 /* gray / mono formats */
263 [PIX_FMT_GRAY16BE] = {
265 .color_type = FF_COLOR_GRAY,
266 .pixel_type = FF_PIXEL_PLANAR,
269 [PIX_FMT_GRAY16LE] = {
271 .color_type = FF_COLOR_GRAY,
272 .pixel_type = FF_PIXEL_PLANAR,
277 .color_type = FF_COLOR_GRAY,
278 .pixel_type = FF_PIXEL_PLANAR,
281 [PIX_FMT_MONOWHITE] = {
283 .color_type = FF_COLOR_GRAY,
284 .pixel_type = FF_PIXEL_PLANAR,
287 [PIX_FMT_MONOBLACK] = {
289 .color_type = FF_COLOR_GRAY,
290 .pixel_type = FF_PIXEL_PLANAR,
294 /* paletted formats */
296 .nb_channels = 4, .is_alpha = 1,
297 .color_type = FF_COLOR_RGB,
298 .pixel_type = FF_PIXEL_PALETTE,
301 [PIX_FMT_UYYVYY411] = {
303 .color_type = FF_COLOR_YUV,
304 .pixel_type = FF_PIXEL_PACKED,
308 .nb_channels = 4, .is_alpha = 1,
309 .color_type = FF_COLOR_RGB,
310 .pixel_type = FF_PIXEL_PACKED,
313 [PIX_FMT_BGR565BE] = {
315 .color_type = FF_COLOR_RGB,
316 .pixel_type = FF_PIXEL_PACKED,
319 [PIX_FMT_BGR565LE] = {
321 .color_type = FF_COLOR_RGB,
322 .pixel_type = FF_PIXEL_PACKED,
325 [PIX_FMT_BGR555BE] = {
327 .color_type = FF_COLOR_RGB,
328 .pixel_type = FF_PIXEL_PACKED,
331 [PIX_FMT_BGR555LE] = {
333 .color_type = FF_COLOR_RGB,
334 .pixel_type = FF_PIXEL_PACKED,
337 [PIX_FMT_BGR444BE] = {
339 .color_type = FF_COLOR_RGB,
340 .pixel_type = FF_PIXEL_PACKED,
343 [PIX_FMT_BGR444LE] = {
345 .color_type = FF_COLOR_RGB,
346 .pixel_type = FF_PIXEL_PACKED,
351 .color_type = FF_COLOR_RGB,
352 .pixel_type = FF_PIXEL_PACKED,
357 .color_type = FF_COLOR_RGB,
358 .pixel_type = FF_PIXEL_PACKED,
361 [PIX_FMT_RGB4_BYTE] = {
363 .color_type = FF_COLOR_RGB,
364 .pixel_type = FF_PIXEL_PACKED,
369 .color_type = FF_COLOR_RGB,
370 .pixel_type = FF_PIXEL_PACKED,
375 .color_type = FF_COLOR_RGB,
376 .pixel_type = FF_PIXEL_PACKED,
379 [PIX_FMT_BGR4_BYTE] = {
381 .color_type = FF_COLOR_RGB,
382 .pixel_type = FF_PIXEL_PACKED,
387 .color_type = FF_COLOR_YUV,
388 .pixel_type = FF_PIXEL_PLANAR,
393 .color_type = FF_COLOR_YUV,
394 .pixel_type = FF_PIXEL_PLANAR,
399 .nb_channels = 4, .is_alpha = 1,
400 .color_type = FF_COLOR_RGB,
401 .pixel_type = FF_PIXEL_PACKED,
405 .nb_channels = 4, .is_alpha = 1,
406 .color_type = FF_COLOR_RGB,
407 .pixel_type = FF_PIXEL_PACKED,
412 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
414 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
415 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
418 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
420 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
423 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
424 enum PixelFormat pix_fmt, int width, int height)
428 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
431 if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
434 return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
437 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
438 unsigned char *dest, int dest_size)
440 int i, j, nb_planes = 0, linesizes[4];
441 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
442 int size = avpicture_get_size(pix_fmt, width, height);
444 if (size > dest_size || size < 0)
445 return AVERROR(EINVAL);
447 for (i = 0; i < desc->nb_components; i++)
448 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
451 av_image_fill_linesizes(linesizes, pix_fmt, width);
452 for (i = 0; i < nb_planes; i++) {
453 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
454 const unsigned char *s = src->data[i];
455 h = (height + (1 << shift) - 1) >> shift;
457 for (j = 0; j < h; j++) {
458 memcpy(dest, s, linesizes[i]);
459 dest += linesizes[i];
460 s += src->linesize[i];
464 if (desc->flags & PIX_FMT_PAL)
465 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
470 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
472 AVPicture dummy_pict;
473 if(av_image_check_size(width, height, 0, NULL))
475 if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
476 // do not include palette for these pseudo-paletted formats
477 return width * height;
478 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
481 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
484 const PixFmtInfo *pf, *ps;
485 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
486 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
489 ps = &pix_fmt_info[src_pix_fmt];
493 pf = &pix_fmt_info[dst_pix_fmt];
494 if (pf->depth < ps->depth ||
495 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
496 dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
497 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
498 src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
499 loss |= FF_LOSS_DEPTH;
500 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
501 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
502 loss |= FF_LOSS_RESOLUTION;
503 switch(pf->color_type) {
505 if (ps->color_type != FF_COLOR_RGB &&
506 ps->color_type != FF_COLOR_GRAY)
507 loss |= FF_LOSS_COLORSPACE;
510 if (ps->color_type != FF_COLOR_GRAY)
511 loss |= FF_LOSS_COLORSPACE;
514 if (ps->color_type != FF_COLOR_YUV)
515 loss |= FF_LOSS_COLORSPACE;
517 case FF_COLOR_YUV_JPEG:
518 if (ps->color_type != FF_COLOR_YUV_JPEG &&
519 ps->color_type != FF_COLOR_YUV &&
520 ps->color_type != FF_COLOR_GRAY)
521 loss |= FF_LOSS_COLORSPACE;
525 if (ps->color_type != pf->color_type)
526 loss |= FF_LOSS_COLORSPACE;
529 if (pf->color_type == FF_COLOR_GRAY &&
530 ps->color_type != FF_COLOR_GRAY)
531 loss |= FF_LOSS_CHROMA;
532 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
533 loss |= FF_LOSS_ALPHA;
534 if (pf->pixel_type == FF_PIXEL_PALETTE &&
535 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
536 loss |= FF_LOSS_COLORQUANT;
540 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
543 const PixFmtInfo *pf;
544 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
546 pf = &pix_fmt_info[pix_fmt];
547 switch(pf->pixel_type) {
548 case FF_PIXEL_PACKED:
550 case PIX_FMT_YUYV422:
551 case PIX_FMT_UYVY422:
552 case PIX_FMT_RGB565BE:
553 case PIX_FMT_RGB565LE:
554 case PIX_FMT_RGB555BE:
555 case PIX_FMT_RGB555LE:
556 case PIX_FMT_RGB444BE:
557 case PIX_FMT_RGB444LE:
558 case PIX_FMT_BGR565BE:
559 case PIX_FMT_BGR565LE:
560 case PIX_FMT_BGR555BE:
561 case PIX_FMT_BGR555LE:
562 case PIX_FMT_BGR444BE:
563 case PIX_FMT_BGR444LE:
566 case PIX_FMT_UYYVYY411:
570 bits = pf->depth * pf->nb_channels;
574 case FF_PIXEL_PLANAR:
575 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
576 bits = pf->depth * pf->nb_channels;
578 bits = pf->depth + ((2 * pf->depth) >>
579 (desc->log2_chroma_w + desc->log2_chroma_h));
582 case FF_PIXEL_PALETTE:
592 static enum PixelFormat avcodec_find_best_pix_fmt1(enum PixelFormat *pix_fmt_list,
593 enum PixelFormat src_pix_fmt,
597 int dist, i, loss, min_dist;
598 enum PixelFormat dst_pix_fmt;
600 /* find exact color match with smallest size */
601 dst_pix_fmt = PIX_FMT_NONE;
602 min_dist = 0x7fffffff;
604 while (pix_fmt_list[i] != PIX_FMT_NONE) {
605 enum PixelFormat pix_fmt = pix_fmt_list[i];
607 if (i > PIX_FMT_NB) {
608 av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
609 "it is either not properly terminated or contains duplicates\n");
613 loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
615 dist = avg_bits_per_pixel(pix_fmt);
616 if (dist < min_dist) {
618 dst_pix_fmt = pix_fmt;
626 #if FF_API_FIND_BEST_PIX_FMT
627 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
628 int has_alpha, int *loss_ptr)
630 enum PixelFormat list[64];
633 // test only the first 64 pixel formats to avoid undefined behaviour
634 for (i = 0; i < 64; i++) {
635 if (pix_fmt_mask & (1ULL << i))
638 list[j] = PIX_FMT_NONE;
640 return avcodec_find_best_pix_fmt2(list, src_pix_fmt, has_alpha, loss_ptr);
642 #endif /* FF_API_FIND_BEST_PIX_FMT */
644 enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat *pix_fmt_list,
645 enum PixelFormat src_pix_fmt,
646 int has_alpha, int *loss_ptr)
648 enum PixelFormat dst_pix_fmt;
650 static const int loss_mask_order[] = {
651 ~0, /* no loss first */
654 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
660 /* try with successive loss */
663 loss_mask = loss_mask_order[i++];
664 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
665 has_alpha, loss_mask);
666 if (dst_pix_fmt >= 0)
674 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
678 void av_picture_copy(AVPicture *dst, const AVPicture *src,
679 enum PixelFormat pix_fmt, int width, int height)
681 av_image_copy(dst->data, dst->linesize, src->data,
682 src->linesize, pix_fmt, width, height);
686 void ff_shrink22(uint8_t *dst, int dst_wrap,
687 const uint8_t *src, int src_wrap,
688 int width, int height)
691 const uint8_t *s1, *s2;
694 for(;height > 0; height--) {
698 for(w = width;w >= 4; w-=4) {
699 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
700 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
701 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
702 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
708 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
719 void ff_shrink44(uint8_t *dst, int dst_wrap,
720 const uint8_t *src, int src_wrap,
721 int width, int height)
724 const uint8_t *s1, *s2, *s3, *s4;
727 for(;height > 0; height--) {
733 for(w = width;w > 0; w--) {
734 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
735 s2[0] + s2[1] + s2[2] + s2[3] +
736 s3[0] + s3[1] + s3[2] + s3[3] +
737 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
750 void ff_shrink88(uint8_t *dst, int dst_wrap,
751 const uint8_t *src, int src_wrap,
752 int width, int height)
756 for(;height > 0; height--) {
757 for(w = width;w > 0; w--) {
760 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
763 *(dst++) = (tmp + 32)>>6;
764 src += 8 - 8*src_wrap;
766 src += 8*src_wrap - 8*width;
767 dst += dst_wrap - width;
772 int avpicture_alloc(AVPicture *picture,
773 enum PixelFormat pix_fmt, int width, int height)
777 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
778 memset(picture, 0, sizeof(AVPicture));
785 void avpicture_free(AVPicture *picture)
787 av_free(picture->data[0]);
790 /* return true if yuv planar */
791 static inline int is_yuv_planar(const PixFmtInfo *ps)
793 return (ps->color_type == FF_COLOR_YUV ||
794 ps->color_type == FF_COLOR_YUV_JPEG) &&
795 ps->pixel_type == FF_PIXEL_PLANAR;
798 int av_picture_crop(AVPicture *dst, const AVPicture *src,
799 enum PixelFormat pix_fmt, int top_band, int left_band)
804 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
807 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
808 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
810 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
811 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
812 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
814 dst->linesize[0] = src->linesize[0];
815 dst->linesize[1] = src->linesize[1];
816 dst->linesize[2] = src->linesize[2];
820 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
821 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
830 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
831 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
833 for (i = 0; i < 3; i++) {
834 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
835 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
837 if (padtop || padleft) {
838 memset(dst->data[i], color[i],
839 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
842 if (padleft || padright) {
843 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
844 (dst->linesize[i] - (padright >> x_shift));
845 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
846 for (y = 0; y < yheight; y++) {
847 memset(optr, color[i], (padleft + padright) >> x_shift);
848 optr += dst->linesize[i];
852 if (src) { /* first line */
853 uint8_t *iptr = src->data[i];
854 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
855 (padleft >> x_shift);
856 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
857 iptr += src->linesize[i];
858 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
859 (dst->linesize[i] - (padright >> x_shift));
860 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
861 for (y = 0; y < yheight; y++) {
862 memset(optr, color[i], (padleft + padright) >> x_shift);
863 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
864 (width - padleft - padright) >> x_shift);
865 iptr += src->linesize[i];
866 optr += dst->linesize[i];
870 if (padbottom || padright) {
871 optr = dst->data[i] + dst->linesize[i] *
872 ((height - padbottom) >> y_shift) - (padright >> x_shift);
873 memset(optr, color[i],dst->linesize[i] *
874 (padbottom >> y_shift) + (padright >> x_shift));
880 #if !HAVE_MMX_EXTERNAL
881 /* filter parameters: [-1 4 2 4 -1] // 8 */
882 static void deinterlace_line_c(uint8_t *dst,
883 const uint8_t *lum_m4, const uint8_t *lum_m3,
884 const uint8_t *lum_m2, const uint8_t *lum_m1,
888 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
891 for(;size > 0;size--) {
893 sum += lum_m3[0] << 2;
894 sum += lum_m2[0] << 1;
895 sum += lum_m1[0] << 2;
897 dst[0] = cm[(sum + 4) >> 3];
907 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
908 uint8_t *lum_m2, uint8_t *lum_m1,
909 uint8_t *lum, int size)
911 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
914 for(;size > 0;size--) {
916 sum += lum_m3[0] << 2;
917 sum += lum_m2[0] << 1;
919 sum += lum_m1[0] << 2;
921 lum_m2[0] = cm[(sum + 4) >> 3];
929 #endif /* !HAVE_MMX_EXTERNAL */
931 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
932 top field is copied as is, but the bottom field is deinterlaced
933 against the top field. */
934 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
935 const uint8_t *src1, int src_wrap,
936 int width, int height)
938 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
943 src_0=&src_m1[src_wrap];
944 src_p1=&src_0[src_wrap];
945 src_p2=&src_p1[src_wrap];
946 for(y=0;y<(height-2);y+=2) {
947 memcpy(dst,src_m1,width);
949 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
953 src_p1 += 2*src_wrap;
954 src_p2 += 2*src_wrap;
957 memcpy(dst,src_m1,width);
960 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
963 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
964 int width, int height)
966 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
969 buf = av_malloc(width);
972 memcpy(buf,src_m1,width);
973 src_0=&src_m1[src_wrap];
974 src_p1=&src_0[src_wrap];
975 src_p2=&src_p1[src_wrap];
976 for(y=0;y<(height-2);y+=2) {
977 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
980 src_p1 += 2*src_wrap;
981 src_p2 += 2*src_wrap;
984 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
988 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
989 enum PixelFormat pix_fmt, int width, int height)
993 if (pix_fmt != PIX_FMT_YUV420P &&
994 pix_fmt != PIX_FMT_YUVJ420P &&
995 pix_fmt != PIX_FMT_YUV422P &&
996 pix_fmt != PIX_FMT_YUVJ422P &&
997 pix_fmt != PIX_FMT_YUV444P &&
998 pix_fmt != PIX_FMT_YUV411P &&
999 pix_fmt != PIX_FMT_GRAY8)
1001 if ((width & 3) != 0 || (height & 3) != 0)
1007 case PIX_FMT_YUVJ420P:
1008 case PIX_FMT_YUV420P:
1012 case PIX_FMT_YUV422P:
1013 case PIX_FMT_YUVJ422P:
1016 case PIX_FMT_YUV411P:
1022 if (pix_fmt == PIX_FMT_GRAY8) {
1027 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1030 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1031 src->data[i], src->linesize[i],