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 "libavcore/imgutils.h"
40 #include "libavcore/internal.h"
42 #if HAVE_MMX && HAVE_YASM
43 #include "x86/dsputil_mmx.h"
46 #define xglue(x, y) x ## y
47 #define glue(x, y) xglue(x, y)
49 #define FF_COLOR_RGB 0 /**< RGB color space */
50 #define FF_COLOR_GRAY 1 /**< gray color space */
51 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
52 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
54 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
55 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
56 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
58 #if HAVE_MMX && HAVE_YASM
59 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
60 #define deinterlace_line ff_deinterlace_line_mmx
62 #define deinterlace_line_inplace deinterlace_line_inplace_c
63 #define deinterlace_line deinterlace_line_c
66 typedef struct PixFmtInfo {
67 uint8_t nb_channels; /**< number of channels (including alpha) */
68 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
69 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
70 uint8_t is_alpha : 1; /**< true if alpha can be specified */
71 uint8_t depth; /**< bit depth of the color components */
74 /* this table gives more information about formats */
75 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
79 .color_type = FF_COLOR_YUV,
80 .pixel_type = FF_PIXEL_PLANAR,
85 .color_type = FF_COLOR_YUV,
86 .pixel_type = FF_PIXEL_PLANAR,
91 .color_type = FF_COLOR_YUV,
92 .pixel_type = FF_PIXEL_PLANAR,
97 .color_type = FF_COLOR_YUV,
98 .pixel_type = FF_PIXEL_PACKED,
101 [PIX_FMT_UYVY422] = {
103 .color_type = FF_COLOR_YUV,
104 .pixel_type = FF_PIXEL_PACKED,
107 [PIX_FMT_YUV410P] = {
109 .color_type = FF_COLOR_YUV,
110 .pixel_type = FF_PIXEL_PLANAR,
113 [PIX_FMT_YUV411P] = {
115 .color_type = FF_COLOR_YUV,
116 .pixel_type = FF_PIXEL_PLANAR,
119 [PIX_FMT_YUV440P] = {
121 .color_type = FF_COLOR_YUV,
122 .pixel_type = FF_PIXEL_PLANAR,
125 [PIX_FMT_YUV420P16LE] = {
127 .color_type = FF_COLOR_YUV,
128 .pixel_type = FF_PIXEL_PLANAR,
131 [PIX_FMT_YUV422P16LE] = {
133 .color_type = FF_COLOR_YUV,
134 .pixel_type = FF_PIXEL_PLANAR,
137 [PIX_FMT_YUV444P16LE] = {
139 .color_type = FF_COLOR_YUV,
140 .pixel_type = FF_PIXEL_PLANAR,
143 [PIX_FMT_YUV420P16BE] = {
145 .color_type = FF_COLOR_YUV,
146 .pixel_type = FF_PIXEL_PLANAR,
149 [PIX_FMT_YUV422P16BE] = {
151 .color_type = FF_COLOR_YUV,
152 .pixel_type = FF_PIXEL_PLANAR,
155 [PIX_FMT_YUV444P16BE] = {
157 .color_type = FF_COLOR_YUV,
158 .pixel_type = FF_PIXEL_PLANAR,
163 /* YUV formats with alpha plane */
164 [PIX_FMT_YUVA420P] = {
166 .color_type = FF_COLOR_YUV,
167 .pixel_type = FF_PIXEL_PLANAR,
172 [PIX_FMT_YUVJ420P] = {
174 .color_type = FF_COLOR_YUV_JPEG,
175 .pixel_type = FF_PIXEL_PLANAR,
178 [PIX_FMT_YUVJ422P] = {
180 .color_type = FF_COLOR_YUV_JPEG,
181 .pixel_type = FF_PIXEL_PLANAR,
184 [PIX_FMT_YUVJ444P] = {
186 .color_type = FF_COLOR_YUV_JPEG,
187 .pixel_type = FF_PIXEL_PLANAR,
190 [PIX_FMT_YUVJ440P] = {
192 .color_type = FF_COLOR_YUV_JPEG,
193 .pixel_type = FF_PIXEL_PLANAR,
200 .color_type = FF_COLOR_RGB,
201 .pixel_type = FF_PIXEL_PACKED,
206 .color_type = FF_COLOR_RGB,
207 .pixel_type = FF_PIXEL_PACKED,
211 .nb_channels = 4, .is_alpha = 1,
212 .color_type = FF_COLOR_RGB,
213 .pixel_type = FF_PIXEL_PACKED,
216 [PIX_FMT_RGB48BE] = {
218 .color_type = FF_COLOR_RGB,
219 .pixel_type = FF_PIXEL_PACKED,
222 [PIX_FMT_RGB48LE] = {
224 .color_type = FF_COLOR_RGB,
225 .pixel_type = FF_PIXEL_PACKED,
228 [PIX_FMT_RGB565BE] = {
230 .color_type = FF_COLOR_RGB,
231 .pixel_type = FF_PIXEL_PACKED,
234 [PIX_FMT_RGB565LE] = {
236 .color_type = FF_COLOR_RGB,
237 .pixel_type = FF_PIXEL_PACKED,
240 [PIX_FMT_RGB555BE] = {
242 .color_type = FF_COLOR_RGB,
243 .pixel_type = FF_PIXEL_PACKED,
246 [PIX_FMT_RGB555LE] = {
248 .color_type = FF_COLOR_RGB,
249 .pixel_type = FF_PIXEL_PACKED,
252 [PIX_FMT_RGB444BE] = {
254 .color_type = FF_COLOR_RGB,
255 .pixel_type = FF_PIXEL_PACKED,
258 [PIX_FMT_RGB444LE] = {
260 .color_type = FF_COLOR_RGB,
261 .pixel_type = FF_PIXEL_PACKED,
265 /* gray / mono formats */
266 [PIX_FMT_GRAY16BE] = {
268 .color_type = FF_COLOR_GRAY,
269 .pixel_type = FF_PIXEL_PLANAR,
272 [PIX_FMT_GRAY16LE] = {
274 .color_type = FF_COLOR_GRAY,
275 .pixel_type = FF_PIXEL_PLANAR,
280 .color_type = FF_COLOR_GRAY,
281 .pixel_type = FF_PIXEL_PLANAR,
284 [PIX_FMT_MONOWHITE] = {
286 .color_type = FF_COLOR_GRAY,
287 .pixel_type = FF_PIXEL_PLANAR,
290 [PIX_FMT_MONOBLACK] = {
292 .color_type = FF_COLOR_GRAY,
293 .pixel_type = FF_PIXEL_PLANAR,
297 /* paletted formats */
299 .nb_channels = 4, .is_alpha = 1,
300 .color_type = FF_COLOR_RGB,
301 .pixel_type = FF_PIXEL_PALETTE,
304 [PIX_FMT_UYYVYY411] = {
306 .color_type = FF_COLOR_YUV,
307 .pixel_type = FF_PIXEL_PACKED,
311 .nb_channels = 4, .is_alpha = 1,
312 .color_type = FF_COLOR_RGB,
313 .pixel_type = FF_PIXEL_PACKED,
316 [PIX_FMT_BGR565BE] = {
318 .color_type = FF_COLOR_RGB,
319 .pixel_type = FF_PIXEL_PACKED,
322 [PIX_FMT_BGR565LE] = {
324 .color_type = FF_COLOR_RGB,
325 .pixel_type = FF_PIXEL_PACKED,
328 [PIX_FMT_BGR555BE] = {
330 .color_type = FF_COLOR_RGB,
331 .pixel_type = FF_PIXEL_PACKED,
334 [PIX_FMT_BGR555LE] = {
336 .color_type = FF_COLOR_RGB,
337 .pixel_type = FF_PIXEL_PACKED,
340 [PIX_FMT_BGR444BE] = {
342 .color_type = FF_COLOR_RGB,
343 .pixel_type = FF_PIXEL_PACKED,
346 [PIX_FMT_BGR444LE] = {
348 .color_type = FF_COLOR_RGB,
349 .pixel_type = FF_PIXEL_PACKED,
354 .color_type = FF_COLOR_RGB,
355 .pixel_type = FF_PIXEL_PACKED,
360 .color_type = FF_COLOR_RGB,
361 .pixel_type = FF_PIXEL_PACKED,
364 [PIX_FMT_RGB4_BYTE] = {
366 .color_type = FF_COLOR_RGB,
367 .pixel_type = FF_PIXEL_PACKED,
372 .color_type = FF_COLOR_RGB,
373 .pixel_type = FF_PIXEL_PACKED,
378 .color_type = FF_COLOR_RGB,
379 .pixel_type = FF_PIXEL_PACKED,
382 [PIX_FMT_BGR4_BYTE] = {
384 .color_type = FF_COLOR_RGB,
385 .pixel_type = FF_PIXEL_PACKED,
390 .color_type = FF_COLOR_YUV,
391 .pixel_type = FF_PIXEL_PLANAR,
396 .color_type = FF_COLOR_YUV,
397 .pixel_type = FF_PIXEL_PLANAR,
402 .nb_channels = 4, .is_alpha = 1,
403 .color_type = FF_COLOR_RGB,
404 .pixel_type = FF_PIXEL_PACKED,
408 .nb_channels = 4, .is_alpha = 1,
409 .color_type = FF_COLOR_RGB,
410 .pixel_type = FF_PIXEL_PACKED,
415 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
417 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
418 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
421 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
423 if ((unsigned)pix_fmt >= PIX_FMT_NB)
426 return av_pix_fmt_descriptors[pix_fmt].name;
429 #if LIBAVCODEC_VERSION_MAJOR < 53
430 enum PixelFormat avcodec_get_pix_fmt(const char *name)
432 return av_get_pix_fmt(name);
435 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
437 av_get_pix_fmt_string(buf, buf_size, pix_fmt);
441 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
443 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
446 #if LIBAVCODEC_VERSION_MAJOR < 53
447 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
448 return ff_set_systematic_pal2(pal, pix_fmt);
451 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
453 return av_image_fill_linesizes(picture->linesize, pix_fmt, width);
456 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
459 return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
463 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
464 enum PixelFormat pix_fmt, int width, int height)
468 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
471 if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
474 return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
477 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
478 unsigned char *dest, int dest_size)
480 int i, j, nb_planes = 0, linesizes[4];
481 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
482 int size = avpicture_get_size(pix_fmt, width, height);
484 if (size > dest_size || size < 0)
485 return AVERROR(EINVAL);
487 for (i = 0; i < desc->nb_components; i++)
488 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
491 av_image_fill_linesizes(linesizes, pix_fmt, width);
492 for (i = 0; i < nb_planes; i++) {
493 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
494 const unsigned char *s = src->data[i];
495 h = (height + (1 << shift) - 1) >> shift;
497 for (j = 0; j < h; j++) {
498 memcpy(dest, s, linesizes[i]);
499 dest += linesizes[i];
500 s += src->linesize[i];
504 if (desc->flags & PIX_FMT_PAL)
505 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
510 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
512 AVPicture dummy_pict;
513 if(av_image_check_size(width, height, 0, NULL))
518 case PIX_FMT_RGB4_BYTE:
519 case PIX_FMT_BGR4_BYTE:
521 // do not include palette for these pseudo-paletted formats
522 return width * height;
524 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
527 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
530 const PixFmtInfo *pf, *ps;
531 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
532 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
535 ps = &pix_fmt_info[src_pix_fmt];
539 pf = &pix_fmt_info[dst_pix_fmt];
540 if (pf->depth < ps->depth ||
541 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
542 dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
543 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
544 src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
545 loss |= FF_LOSS_DEPTH;
546 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
547 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
548 loss |= FF_LOSS_RESOLUTION;
549 switch(pf->color_type) {
551 if (ps->color_type != FF_COLOR_RGB &&
552 ps->color_type != FF_COLOR_GRAY)
553 loss |= FF_LOSS_COLORSPACE;
556 if (ps->color_type != FF_COLOR_GRAY)
557 loss |= FF_LOSS_COLORSPACE;
560 if (ps->color_type != FF_COLOR_YUV)
561 loss |= FF_LOSS_COLORSPACE;
563 case FF_COLOR_YUV_JPEG:
564 if (ps->color_type != FF_COLOR_YUV_JPEG &&
565 ps->color_type != FF_COLOR_YUV &&
566 ps->color_type != FF_COLOR_GRAY)
567 loss |= FF_LOSS_COLORSPACE;
571 if (ps->color_type != pf->color_type)
572 loss |= FF_LOSS_COLORSPACE;
575 if (pf->color_type == FF_COLOR_GRAY &&
576 ps->color_type != FF_COLOR_GRAY)
577 loss |= FF_LOSS_CHROMA;
578 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
579 loss |= FF_LOSS_ALPHA;
580 if (pf->pixel_type == FF_PIXEL_PALETTE &&
581 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
582 loss |= FF_LOSS_COLORQUANT;
586 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
589 const PixFmtInfo *pf;
590 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
592 pf = &pix_fmt_info[pix_fmt];
593 switch(pf->pixel_type) {
594 case FF_PIXEL_PACKED:
596 case PIX_FMT_YUYV422:
597 case PIX_FMT_UYVY422:
598 case PIX_FMT_RGB565BE:
599 case PIX_FMT_RGB565LE:
600 case PIX_FMT_RGB555BE:
601 case PIX_FMT_RGB555LE:
602 case PIX_FMT_RGB444BE:
603 case PIX_FMT_RGB444LE:
604 case PIX_FMT_BGR565BE:
605 case PIX_FMT_BGR565LE:
606 case PIX_FMT_BGR555BE:
607 case PIX_FMT_BGR555LE:
608 case PIX_FMT_BGR444BE:
609 case PIX_FMT_BGR444LE:
612 case PIX_FMT_UYYVYY411:
616 bits = pf->depth * pf->nb_channels;
620 case FF_PIXEL_PLANAR:
621 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
622 bits = pf->depth * pf->nb_channels;
624 bits = pf->depth + ((2 * pf->depth) >>
625 (desc->log2_chroma_w + desc->log2_chroma_h));
628 case FF_PIXEL_PALETTE:
638 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
639 enum PixelFormat src_pix_fmt,
643 int dist, i, loss, min_dist;
644 enum PixelFormat dst_pix_fmt;
646 /* find exact color match with smallest size */
647 dst_pix_fmt = PIX_FMT_NONE;
648 min_dist = 0x7fffffff;
649 for(i = 0;i < PIX_FMT_NB; i++) {
650 if (pix_fmt_mask & (1ULL << i)) {
651 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
653 dist = avg_bits_per_pixel(i);
654 if (dist < min_dist) {
664 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
665 int has_alpha, int *loss_ptr)
667 enum PixelFormat dst_pix_fmt;
669 static const int loss_mask_order[] = {
670 ~0, /* no loss first */
673 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
679 /* try with successive loss */
682 loss_mask = loss_mask_order[i++];
683 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
684 has_alpha, loss_mask);
685 if (dst_pix_fmt >= 0)
693 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
697 #if LIBAVCODEC_VERSION_MAJOR < 53
698 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
699 const uint8_t *src, int src_wrap,
700 int width, int height)
702 av_image_copy_plane(dst, dst_wrap, src, src_wrap, width, height);
705 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
707 return av_image_get_linesize(pix_fmt, width, plane);
710 void av_picture_data_copy(uint8_t *dst_data[4], int dst_linesize[4],
711 uint8_t *src_data[4], int src_linesize[4],
712 enum PixelFormat pix_fmt, int width, int height)
714 av_image_copy(dst_data, dst_linesize, src_data, src_linesize,
715 pix_fmt, width, height);
719 void av_picture_copy(AVPicture *dst, const AVPicture *src,
720 enum PixelFormat pix_fmt, int width, int height)
722 av_image_copy(dst->data, dst->linesize, src->data,
723 src->linesize, pix_fmt, width, height);
727 void ff_shrink22(uint8_t *dst, int dst_wrap,
728 const uint8_t *src, int src_wrap,
729 int width, int height)
732 const uint8_t *s1, *s2;
735 for(;height > 0; height--) {
739 for(w = width;w >= 4; w-=4) {
740 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
741 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
742 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
743 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
749 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
760 void ff_shrink44(uint8_t *dst, int dst_wrap,
761 const uint8_t *src, int src_wrap,
762 int width, int height)
765 const uint8_t *s1, *s2, *s3, *s4;
768 for(;height > 0; height--) {
774 for(w = width;w > 0; w--) {
775 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
776 s2[0] + s2[1] + s2[2] + s2[3] +
777 s3[0] + s3[1] + s3[2] + s3[3] +
778 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
791 void ff_shrink88(uint8_t *dst, int dst_wrap,
792 const uint8_t *src, int src_wrap,
793 int width, int height)
797 for(;height > 0; height--) {
798 for(w = width;w > 0; w--) {
801 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
804 *(dst++) = (tmp + 32)>>6;
805 src += 8 - 8*src_wrap;
807 src += 8*src_wrap - 8*width;
808 dst += dst_wrap - width;
813 int avpicture_alloc(AVPicture *picture,
814 enum PixelFormat pix_fmt, int width, int height)
818 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
819 memset(picture, 0, sizeof(AVPicture));
826 void avpicture_free(AVPicture *picture)
828 av_free(picture->data[0]);
831 /* return true if yuv planar */
832 static inline int is_yuv_planar(const PixFmtInfo *ps)
834 return (ps->color_type == FF_COLOR_YUV ||
835 ps->color_type == FF_COLOR_YUV_JPEG) &&
836 ps->pixel_type == FF_PIXEL_PLANAR;
839 int av_picture_crop(AVPicture *dst, const AVPicture *src,
840 enum PixelFormat pix_fmt, int top_band, int left_band)
845 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
848 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
849 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
851 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
852 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
853 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
855 dst->linesize[0] = src->linesize[0];
856 dst->linesize[1] = src->linesize[1];
857 dst->linesize[2] = src->linesize[2];
861 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
862 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
871 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
872 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
874 for (i = 0; i < 3; i++) {
875 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
876 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
878 if (padtop || padleft) {
879 memset(dst->data[i], color[i],
880 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
883 if (padleft || padright) {
884 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
885 (dst->linesize[i] - (padright >> x_shift));
886 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
887 for (y = 0; y < yheight; y++) {
888 memset(optr, color[i], (padleft + padright) >> x_shift);
889 optr += dst->linesize[i];
893 if (src) { /* first line */
894 uint8_t *iptr = src->data[i];
895 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
896 (padleft >> x_shift);
897 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
898 iptr += src->linesize[i];
899 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
900 (dst->linesize[i] - (padright >> x_shift));
901 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
902 for (y = 0; y < yheight; y++) {
903 memset(optr, color[i], (padleft + padright) >> x_shift);
904 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
905 (width - padleft - padright) >> x_shift);
906 iptr += src->linesize[i];
907 optr += dst->linesize[i];
911 if (padbottom || padright) {
912 optr = dst->data[i] + dst->linesize[i] *
913 ((height - padbottom) >> y_shift) - (padright >> x_shift);
914 memset(optr, color[i],dst->linesize[i] *
915 (padbottom >> y_shift) + (padright >> x_shift));
921 /* NOTE: we scan all the pixels to have an exact information */
922 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
924 const unsigned char *p;
925 int src_wrap, ret, x, y;
927 uint32_t *palette = (uint32_t *)src->data[1];
930 src_wrap = src->linesize[0] - width;
932 for(y=0;y<height;y++) {
933 for(x=0;x<width;x++) {
934 a = palette[p[0]] >> 24;
936 ret |= FF_ALPHA_TRANSP;
937 } else if (a != 0xff) {
938 ret |= FF_ALPHA_SEMI_TRANSP;
947 int img_get_alpha_info(const AVPicture *src,
948 enum PixelFormat pix_fmt, int width, int height)
950 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
953 /* no alpha can be represented in format */
958 ret = get_alpha_info_pal8(src, width, height);
961 /* we do not know, so everything is indicated */
962 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
968 #if !(HAVE_MMX && HAVE_YASM)
969 /* filter parameters: [-1 4 2 4 -1] // 8 */
970 static void deinterlace_line_c(uint8_t *dst,
971 const uint8_t *lum_m4, const uint8_t *lum_m3,
972 const uint8_t *lum_m2, const uint8_t *lum_m1,
976 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
979 for(;size > 0;size--) {
981 sum += lum_m3[0] << 2;
982 sum += lum_m2[0] << 1;
983 sum += lum_m1[0] << 2;
985 dst[0] = cm[(sum + 4) >> 3];
995 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
996 uint8_t *lum_m2, uint8_t *lum_m1,
997 uint8_t *lum, int size)
999 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1002 for(;size > 0;size--) {
1004 sum += lum_m3[0] << 2;
1005 sum += lum_m2[0] << 1;
1006 lum_m4[0]=lum_m2[0];
1007 sum += lum_m1[0] << 2;
1009 lum_m2[0] = cm[(sum + 4) >> 3];
1019 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1020 top field is copied as is, but the bottom field is deinterlaced
1021 against the top field. */
1022 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1023 const uint8_t *src1, int src_wrap,
1024 int width, int height)
1026 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1031 src_0=&src_m1[src_wrap];
1032 src_p1=&src_0[src_wrap];
1033 src_p2=&src_p1[src_wrap];
1034 for(y=0;y<(height-2);y+=2) {
1035 memcpy(dst,src_m1,width);
1037 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1041 src_p1 += 2*src_wrap;
1042 src_p2 += 2*src_wrap;
1045 memcpy(dst,src_m1,width);
1048 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1051 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1052 int width, int height)
1054 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1057 buf = (uint8_t*)av_malloc(width);
1060 memcpy(buf,src_m1,width);
1061 src_0=&src_m1[src_wrap];
1062 src_p1=&src_0[src_wrap];
1063 src_p2=&src_p1[src_wrap];
1064 for(y=0;y<(height-2);y+=2) {
1065 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1068 src_p1 += 2*src_wrap;
1069 src_p2 += 2*src_wrap;
1072 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1076 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1077 enum PixelFormat pix_fmt, int width, int height)
1081 if (pix_fmt != PIX_FMT_YUV420P &&
1082 pix_fmt != PIX_FMT_YUVJ420P &&
1083 pix_fmt != PIX_FMT_YUV422P &&
1084 pix_fmt != PIX_FMT_YUVJ422P &&
1085 pix_fmt != PIX_FMT_YUV444P &&
1086 pix_fmt != PIX_FMT_YUV411P &&
1087 pix_fmt != PIX_FMT_GRAY8)
1089 if ((width & 3) != 0 || (height & 3) != 0)
1095 case PIX_FMT_YUVJ420P:
1096 case PIX_FMT_YUV420P:
1100 case PIX_FMT_YUV422P:
1101 case PIX_FMT_YUVJ422P:
1104 case PIX_FMT_YUV411P:
1110 if (pix_fmt == PIX_FMT_GRAY8) {
1115 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1118 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1119 src->data[i], src->linesize[i],