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 xglue(x, y) x ## y
46 #define glue(x, y) xglue(x, y)
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 */
53 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
54 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
55 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
57 #if HAVE_MMX && HAVE_YASM
58 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
59 #define deinterlace_line ff_deinterlace_line_mmx
61 #define deinterlace_line_inplace deinterlace_line_inplace_c
62 #define deinterlace_line deinterlace_line_c
65 typedef struct PixFmtInfo {
66 uint8_t nb_channels; /**< number of channels (including alpha) */
67 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
68 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
69 uint8_t is_alpha : 1; /**< true if alpha can be specified */
70 uint8_t depth; /**< bit depth of the color components */
73 /* this table gives more information about formats */
74 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
78 .color_type = FF_COLOR_YUV,
79 .pixel_type = FF_PIXEL_PLANAR,
84 .color_type = FF_COLOR_YUV,
85 .pixel_type = FF_PIXEL_PLANAR,
90 .color_type = FF_COLOR_YUV,
91 .pixel_type = FF_PIXEL_PLANAR,
96 .color_type = FF_COLOR_YUV,
97 .pixel_type = FF_PIXEL_PACKED,
100 [PIX_FMT_UYVY422] = {
102 .color_type = FF_COLOR_YUV,
103 .pixel_type = FF_PIXEL_PACKED,
106 [PIX_FMT_YUV410P] = {
108 .color_type = FF_COLOR_YUV,
109 .pixel_type = FF_PIXEL_PLANAR,
112 [PIX_FMT_YUV411P] = {
114 .color_type = FF_COLOR_YUV,
115 .pixel_type = FF_PIXEL_PLANAR,
118 [PIX_FMT_YUV440P] = {
120 .color_type = FF_COLOR_YUV,
121 .pixel_type = FF_PIXEL_PLANAR,
124 [PIX_FMT_YUV420P16LE] = {
126 .color_type = FF_COLOR_YUV,
127 .pixel_type = FF_PIXEL_PLANAR,
130 [PIX_FMT_YUV422P16LE] = {
132 .color_type = FF_COLOR_YUV,
133 .pixel_type = FF_PIXEL_PLANAR,
136 [PIX_FMT_YUV444P16LE] = {
138 .color_type = FF_COLOR_YUV,
139 .pixel_type = FF_PIXEL_PLANAR,
142 [PIX_FMT_YUV420P16BE] = {
144 .color_type = FF_COLOR_YUV,
145 .pixel_type = FF_PIXEL_PLANAR,
148 [PIX_FMT_YUV422P16BE] = {
150 .color_type = FF_COLOR_YUV,
151 .pixel_type = FF_PIXEL_PLANAR,
154 [PIX_FMT_YUV444P16BE] = {
156 .color_type = FF_COLOR_YUV,
157 .pixel_type = FF_PIXEL_PLANAR,
162 /* YUV formats with alpha plane */
163 [PIX_FMT_YUVA420P] = {
165 .color_type = FF_COLOR_YUV,
166 .pixel_type = FF_PIXEL_PLANAR,
171 [PIX_FMT_YUVJ420P] = {
173 .color_type = FF_COLOR_YUV_JPEG,
174 .pixel_type = FF_PIXEL_PLANAR,
177 [PIX_FMT_YUVJ422P] = {
179 .color_type = FF_COLOR_YUV_JPEG,
180 .pixel_type = FF_PIXEL_PLANAR,
183 [PIX_FMT_YUVJ444P] = {
185 .color_type = FF_COLOR_YUV_JPEG,
186 .pixel_type = FF_PIXEL_PLANAR,
189 [PIX_FMT_YUVJ440P] = {
191 .color_type = FF_COLOR_YUV_JPEG,
192 .pixel_type = FF_PIXEL_PLANAR,
199 .color_type = FF_COLOR_RGB,
200 .pixel_type = FF_PIXEL_PACKED,
205 .color_type = FF_COLOR_RGB,
206 .pixel_type = FF_PIXEL_PACKED,
210 .nb_channels = 4, .is_alpha = 1,
211 .color_type = FF_COLOR_RGB,
212 .pixel_type = FF_PIXEL_PACKED,
215 [PIX_FMT_RGB48BE] = {
217 .color_type = FF_COLOR_RGB,
218 .pixel_type = FF_PIXEL_PACKED,
221 [PIX_FMT_RGB48LE] = {
223 .color_type = FF_COLOR_RGB,
224 .pixel_type = FF_PIXEL_PACKED,
227 [PIX_FMT_RGB565BE] = {
229 .color_type = FF_COLOR_RGB,
230 .pixel_type = FF_PIXEL_PACKED,
233 [PIX_FMT_RGB565LE] = {
235 .color_type = FF_COLOR_RGB,
236 .pixel_type = FF_PIXEL_PACKED,
239 [PIX_FMT_RGB555BE] = {
241 .color_type = FF_COLOR_RGB,
242 .pixel_type = FF_PIXEL_PACKED,
245 [PIX_FMT_RGB555LE] = {
247 .color_type = FF_COLOR_RGB,
248 .pixel_type = FF_PIXEL_PACKED,
251 [PIX_FMT_RGB444BE] = {
253 .color_type = FF_COLOR_RGB,
254 .pixel_type = FF_PIXEL_PACKED,
257 [PIX_FMT_RGB444LE] = {
259 .color_type = FF_COLOR_RGB,
260 .pixel_type = FF_PIXEL_PACKED,
264 /* gray / mono formats */
265 [PIX_FMT_GRAY16BE] = {
267 .color_type = FF_COLOR_GRAY,
268 .pixel_type = FF_PIXEL_PLANAR,
271 [PIX_FMT_GRAY16LE] = {
273 .color_type = FF_COLOR_GRAY,
274 .pixel_type = FF_PIXEL_PLANAR,
279 .color_type = FF_COLOR_GRAY,
280 .pixel_type = FF_PIXEL_PLANAR,
283 [PIX_FMT_MONOWHITE] = {
285 .color_type = FF_COLOR_GRAY,
286 .pixel_type = FF_PIXEL_PLANAR,
289 [PIX_FMT_MONOBLACK] = {
291 .color_type = FF_COLOR_GRAY,
292 .pixel_type = FF_PIXEL_PLANAR,
296 /* paletted formats */
298 .nb_channels = 4, .is_alpha = 1,
299 .color_type = FF_COLOR_RGB,
300 .pixel_type = FF_PIXEL_PALETTE,
303 [PIX_FMT_UYYVYY411] = {
305 .color_type = FF_COLOR_YUV,
306 .pixel_type = FF_PIXEL_PACKED,
310 .nb_channels = 4, .is_alpha = 1,
311 .color_type = FF_COLOR_RGB,
312 .pixel_type = FF_PIXEL_PACKED,
315 [PIX_FMT_BGR565BE] = {
317 .color_type = FF_COLOR_RGB,
318 .pixel_type = FF_PIXEL_PACKED,
321 [PIX_FMT_BGR565LE] = {
323 .color_type = FF_COLOR_RGB,
324 .pixel_type = FF_PIXEL_PACKED,
327 [PIX_FMT_BGR555BE] = {
329 .color_type = FF_COLOR_RGB,
330 .pixel_type = FF_PIXEL_PACKED,
333 [PIX_FMT_BGR555LE] = {
335 .color_type = FF_COLOR_RGB,
336 .pixel_type = FF_PIXEL_PACKED,
339 [PIX_FMT_BGR444BE] = {
341 .color_type = FF_COLOR_RGB,
342 .pixel_type = FF_PIXEL_PACKED,
345 [PIX_FMT_BGR444LE] = {
347 .color_type = FF_COLOR_RGB,
348 .pixel_type = FF_PIXEL_PACKED,
353 .color_type = FF_COLOR_RGB,
354 .pixel_type = FF_PIXEL_PACKED,
359 .color_type = FF_COLOR_RGB,
360 .pixel_type = FF_PIXEL_PACKED,
363 [PIX_FMT_RGB4_BYTE] = {
365 .color_type = FF_COLOR_RGB,
366 .pixel_type = FF_PIXEL_PACKED,
371 .color_type = FF_COLOR_RGB,
372 .pixel_type = FF_PIXEL_PACKED,
377 .color_type = FF_COLOR_RGB,
378 .pixel_type = FF_PIXEL_PACKED,
381 [PIX_FMT_BGR4_BYTE] = {
383 .color_type = FF_COLOR_RGB,
384 .pixel_type = FF_PIXEL_PACKED,
389 .color_type = FF_COLOR_YUV,
390 .pixel_type = FF_PIXEL_PLANAR,
395 .color_type = FF_COLOR_YUV,
396 .pixel_type = FF_PIXEL_PLANAR,
401 .nb_channels = 4, .is_alpha = 1,
402 .color_type = FF_COLOR_RGB,
403 .pixel_type = FF_PIXEL_PACKED,
407 .nb_channels = 4, .is_alpha = 1,
408 .color_type = FF_COLOR_RGB,
409 .pixel_type = FF_PIXEL_PACKED,
414 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
416 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
417 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
420 #if FF_API_GET_PIX_FMT_NAME
421 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
423 return av_get_pix_fmt_name(pix_fmt);
427 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
429 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
432 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
433 enum PixelFormat pix_fmt, int width, int height)
437 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
440 if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
443 return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
446 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
447 unsigned char *dest, int dest_size)
449 int i, j, nb_planes = 0, linesizes[4];
450 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
451 int size = avpicture_get_size(pix_fmt, width, height);
453 if (size > dest_size || size < 0)
454 return AVERROR(EINVAL);
456 for (i = 0; i < desc->nb_components; i++)
457 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
460 av_image_fill_linesizes(linesizes, pix_fmt, width);
461 for (i = 0; i < nb_planes; i++) {
462 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
463 const unsigned char *s = src->data[i];
464 h = (height + (1 << shift) - 1) >> shift;
466 for (j = 0; j < h; j++) {
467 memcpy(dest, s, linesizes[i]);
468 dest += linesizes[i];
469 s += src->linesize[i];
473 if (desc->flags & PIX_FMT_PAL)
474 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
479 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
481 AVPicture dummy_pict;
482 if(av_image_check_size(width, height, 0, NULL))
487 case PIX_FMT_RGB4_BYTE:
488 case PIX_FMT_BGR4_BYTE:
490 // do not include palette for these pseudo-paletted formats
491 return width * height;
493 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
496 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
499 const PixFmtInfo *pf, *ps;
500 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
501 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
504 ps = &pix_fmt_info[src_pix_fmt];
508 pf = &pix_fmt_info[dst_pix_fmt];
509 if (pf->depth < ps->depth ||
510 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
511 dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
512 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
513 src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
514 loss |= FF_LOSS_DEPTH;
515 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
516 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
517 loss |= FF_LOSS_RESOLUTION;
518 switch(pf->color_type) {
520 if (ps->color_type != FF_COLOR_RGB &&
521 ps->color_type != FF_COLOR_GRAY)
522 loss |= FF_LOSS_COLORSPACE;
525 if (ps->color_type != FF_COLOR_GRAY)
526 loss |= FF_LOSS_COLORSPACE;
529 if (ps->color_type != FF_COLOR_YUV)
530 loss |= FF_LOSS_COLORSPACE;
532 case FF_COLOR_YUV_JPEG:
533 if (ps->color_type != FF_COLOR_YUV_JPEG &&
534 ps->color_type != FF_COLOR_YUV &&
535 ps->color_type != FF_COLOR_GRAY)
536 loss |= FF_LOSS_COLORSPACE;
540 if (ps->color_type != pf->color_type)
541 loss |= FF_LOSS_COLORSPACE;
544 if (pf->color_type == FF_COLOR_GRAY &&
545 ps->color_type != FF_COLOR_GRAY)
546 loss |= FF_LOSS_CHROMA;
547 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
548 loss |= FF_LOSS_ALPHA;
549 if (pf->pixel_type == FF_PIXEL_PALETTE &&
550 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
551 loss |= FF_LOSS_COLORQUANT;
555 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
558 const PixFmtInfo *pf;
559 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
561 pf = &pix_fmt_info[pix_fmt];
562 switch(pf->pixel_type) {
563 case FF_PIXEL_PACKED:
565 case PIX_FMT_YUYV422:
566 case PIX_FMT_UYVY422:
567 case PIX_FMT_RGB565BE:
568 case PIX_FMT_RGB565LE:
569 case PIX_FMT_RGB555BE:
570 case PIX_FMT_RGB555LE:
571 case PIX_FMT_RGB444BE:
572 case PIX_FMT_RGB444LE:
573 case PIX_FMT_BGR565BE:
574 case PIX_FMT_BGR565LE:
575 case PIX_FMT_BGR555BE:
576 case PIX_FMT_BGR555LE:
577 case PIX_FMT_BGR444BE:
578 case PIX_FMT_BGR444LE:
581 case PIX_FMT_UYYVYY411:
585 bits = pf->depth * pf->nb_channels;
589 case FF_PIXEL_PLANAR:
590 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
591 bits = pf->depth * pf->nb_channels;
593 bits = pf->depth + ((2 * pf->depth) >>
594 (desc->log2_chroma_w + desc->log2_chroma_h));
597 case FF_PIXEL_PALETTE:
607 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
608 enum PixelFormat src_pix_fmt,
612 int dist, i, loss, min_dist;
613 enum PixelFormat dst_pix_fmt;
615 /* find exact color match with smallest size */
616 dst_pix_fmt = PIX_FMT_NONE;
617 min_dist = 0x7fffffff;
618 for(i = 0;i < PIX_FMT_NB; i++) {
619 if (pix_fmt_mask & (1ULL << i)) {
620 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
622 dist = avg_bits_per_pixel(i);
623 if (dist < min_dist) {
633 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
634 int has_alpha, int *loss_ptr)
636 enum PixelFormat dst_pix_fmt;
638 static const int loss_mask_order[] = {
639 ~0, /* no loss first */
642 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
648 /* try with successive loss */
651 loss_mask = loss_mask_order[i++];
652 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
653 has_alpha, loss_mask);
654 if (dst_pix_fmt >= 0)
662 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
666 void av_picture_copy(AVPicture *dst, const AVPicture *src,
667 enum PixelFormat pix_fmt, int width, int height)
669 av_image_copy(dst->data, dst->linesize, src->data,
670 src->linesize, pix_fmt, width, height);
674 void ff_shrink22(uint8_t *dst, int dst_wrap,
675 const uint8_t *src, int src_wrap,
676 int width, int height)
679 const uint8_t *s1, *s2;
682 for(;height > 0; height--) {
686 for(w = width;w >= 4; w-=4) {
687 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
688 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
689 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
690 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
696 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
707 void ff_shrink44(uint8_t *dst, int dst_wrap,
708 const uint8_t *src, int src_wrap,
709 int width, int height)
712 const uint8_t *s1, *s2, *s3, *s4;
715 for(;height > 0; height--) {
721 for(w = width;w > 0; w--) {
722 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
723 s2[0] + s2[1] + s2[2] + s2[3] +
724 s3[0] + s3[1] + s3[2] + s3[3] +
725 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
738 void ff_shrink88(uint8_t *dst, int dst_wrap,
739 const uint8_t *src, int src_wrap,
740 int width, int height)
744 for(;height > 0; height--) {
745 for(w = width;w > 0; w--) {
748 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
751 *(dst++) = (tmp + 32)>>6;
752 src += 8 - 8*src_wrap;
754 src += 8*src_wrap - 8*width;
755 dst += dst_wrap - width;
760 int avpicture_alloc(AVPicture *picture,
761 enum PixelFormat pix_fmt, int width, int height)
765 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
766 memset(picture, 0, sizeof(AVPicture));
773 void avpicture_free(AVPicture *picture)
775 av_free(picture->data[0]);
778 /* return true if yuv planar */
779 static inline int is_yuv_planar(const PixFmtInfo *ps)
781 return (ps->color_type == FF_COLOR_YUV ||
782 ps->color_type == FF_COLOR_YUV_JPEG) &&
783 ps->pixel_type == FF_PIXEL_PLANAR;
786 int av_picture_crop(AVPicture *dst, const AVPicture *src,
787 enum PixelFormat pix_fmt, int top_band, int left_band)
792 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
795 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
796 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
798 if (is_yuv_planar(&pix_fmt_info[pix_fmt])) {
799 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
800 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
801 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
803 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
805 if(left_band) //FIXME add support for this too
807 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
810 dst->linesize[0] = src->linesize[0];
811 dst->linesize[1] = src->linesize[1];
812 dst->linesize[2] = src->linesize[2];
816 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
817 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
826 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
827 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
829 for (i = 0; i < 3; i++) {
830 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
831 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
833 if (padtop || padleft) {
834 memset(dst->data[i], color[i],
835 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
838 if (padleft || padright) {
839 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
840 (dst->linesize[i] - (padright >> x_shift));
841 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
842 for (y = 0; y < yheight; y++) {
843 memset(optr, color[i], (padleft + padright) >> x_shift);
844 optr += dst->linesize[i];
848 if (src) { /* first line */
849 uint8_t *iptr = src->data[i];
850 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
851 (padleft >> x_shift);
852 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
853 iptr += src->linesize[i];
854 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
855 (dst->linesize[i] - (padright >> x_shift));
856 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
857 for (y = 0; y < yheight; y++) {
858 memset(optr, color[i], (padleft + padright) >> x_shift);
859 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
860 (width - padleft - padright) >> x_shift);
861 iptr += src->linesize[i];
862 optr += dst->linesize[i];
866 if (padbottom || padright) {
867 optr = dst->data[i] + dst->linesize[i] *
868 ((height - padbottom) >> y_shift) - (padright >> x_shift);
869 memset(optr, color[i],dst->linesize[i] *
870 (padbottom >> y_shift) + (padright >> x_shift));
876 /* NOTE: we scan all the pixels to have an exact information */
877 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
879 const unsigned char *p;
880 int src_wrap, ret, x, y;
882 uint32_t *palette = (uint32_t *)src->data[1];
885 src_wrap = src->linesize[0] - width;
887 for(y=0;y<height;y++) {
888 for(x=0;x<width;x++) {
889 a = palette[p[0]] >> 24;
891 ret |= FF_ALPHA_TRANSP;
892 } else if (a != 0xff) {
893 ret |= FF_ALPHA_SEMI_TRANSP;
902 int img_get_alpha_info(const AVPicture *src,
903 enum PixelFormat pix_fmt, int width, int height)
905 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
908 /* no alpha can be represented in format */
913 ret = get_alpha_info_pal8(src, width, height);
916 /* we do not know, so everything is indicated */
917 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
923 #if !(HAVE_MMX && HAVE_YASM)
924 /* filter parameters: [-1 4 2 4 -1] // 8 */
925 static void deinterlace_line_c(uint8_t *dst,
926 const uint8_t *lum_m4, const uint8_t *lum_m3,
927 const uint8_t *lum_m2, const uint8_t *lum_m1,
931 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
934 for(;size > 0;size--) {
936 sum += lum_m3[0] << 2;
937 sum += lum_m2[0] << 1;
938 sum += lum_m1[0] << 2;
940 dst[0] = cm[(sum + 4) >> 3];
950 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
951 uint8_t *lum_m2, uint8_t *lum_m1,
952 uint8_t *lum, int size)
954 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
957 for(;size > 0;size--) {
959 sum += lum_m3[0] << 2;
960 sum += lum_m2[0] << 1;
962 sum += lum_m1[0] << 2;
964 lum_m2[0] = cm[(sum + 4) >> 3];
974 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
975 top field is copied as is, but the bottom field is deinterlaced
976 against the top field. */
977 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
978 const uint8_t *src1, int src_wrap,
979 int width, int height)
981 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
986 src_0=&src_m1[src_wrap];
987 src_p1=&src_0[src_wrap];
988 src_p2=&src_p1[src_wrap];
989 for(y=0;y<(height-2);y+=2) {
990 memcpy(dst,src_m1,width);
992 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
996 src_p1 += 2*src_wrap;
997 src_p2 += 2*src_wrap;
1000 memcpy(dst,src_m1,width);
1003 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1006 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1007 int width, int height)
1009 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1012 buf = (uint8_t*)av_malloc(width);
1015 memcpy(buf,src_m1,width);
1016 src_0=&src_m1[src_wrap];
1017 src_p1=&src_0[src_wrap];
1018 src_p2=&src_p1[src_wrap];
1019 for(y=0;y<(height-2);y+=2) {
1020 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1023 src_p1 += 2*src_wrap;
1024 src_p2 += 2*src_wrap;
1027 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1031 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1032 enum PixelFormat pix_fmt, int width, int height)
1036 if (pix_fmt != PIX_FMT_YUV420P &&
1037 pix_fmt != PIX_FMT_YUVJ420P &&
1038 pix_fmt != PIX_FMT_YUV422P &&
1039 pix_fmt != PIX_FMT_YUVJ422P &&
1040 pix_fmt != PIX_FMT_YUV444P &&
1041 pix_fmt != PIX_FMT_YUV411P &&
1042 pix_fmt != PIX_FMT_GRAY8)
1044 if ((width & 3) != 0 || (height & 3) != 0)
1050 case PIX_FMT_YUVJ420P:
1051 case PIX_FMT_YUV420P:
1055 case PIX_FMT_YUV422P:
1056 case PIX_FMT_YUVJ422P:
1059 case PIX_FMT_YUV411P:
1065 if (pix_fmt == PIX_FMT_GRAY8) {
1070 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1073 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1074 src->data[i], src->linesize[i],