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 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
422 if ((unsigned)pix_fmt >= PIX_FMT_NB)
425 return av_pix_fmt_descriptors[pix_fmt].name;
428 #if LIBAVCODEC_VERSION_MAJOR < 53
429 enum PixelFormat avcodec_get_pix_fmt(const char *name)
431 return av_get_pix_fmt(name);
434 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
436 av_get_pix_fmt_string(buf, buf_size, pix_fmt);
440 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
442 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
445 #if LIBAVCODEC_VERSION_MAJOR < 53
446 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
447 return ff_set_systematic_pal2(pal, pix_fmt);
450 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
452 return av_image_fill_linesizes(picture->linesize, pix_fmt, width);
455 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
458 return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
462 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
463 enum PixelFormat pix_fmt, int width, int height)
467 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
470 if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
473 return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
476 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
477 unsigned char *dest, int dest_size)
479 int i, j, nb_planes = 0, linesizes[4];
480 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
481 int size = avpicture_get_size(pix_fmt, width, height);
483 if (size > dest_size || size < 0)
484 return AVERROR(EINVAL);
486 for (i = 0; i < desc->nb_components; i++)
487 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
490 av_image_fill_linesizes(linesizes, pix_fmt, width);
491 for (i = 0; i < nb_planes; i++) {
492 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
493 const unsigned char *s = src->data[i];
494 h = (height + (1 << shift) - 1) >> shift;
496 for (j = 0; j < h; j++) {
497 memcpy(dest, s, linesizes[i]);
498 dest += linesizes[i];
499 s += src->linesize[i];
503 if (desc->flags & PIX_FMT_PAL)
504 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
509 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
511 AVPicture dummy_pict;
512 if(av_image_check_size(width, height, 0, NULL))
517 case PIX_FMT_RGB4_BYTE:
518 case PIX_FMT_BGR4_BYTE:
520 // do not include palette for these pseudo-paletted formats
521 return width * height;
523 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
526 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
529 const PixFmtInfo *pf, *ps;
530 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
531 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
534 ps = &pix_fmt_info[src_pix_fmt];
538 pf = &pix_fmt_info[dst_pix_fmt];
539 if (pf->depth < ps->depth ||
540 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
541 dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
542 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
543 src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
544 loss |= FF_LOSS_DEPTH;
545 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
546 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
547 loss |= FF_LOSS_RESOLUTION;
548 switch(pf->color_type) {
550 if (ps->color_type != FF_COLOR_RGB &&
551 ps->color_type != FF_COLOR_GRAY)
552 loss |= FF_LOSS_COLORSPACE;
555 if (ps->color_type != FF_COLOR_GRAY)
556 loss |= FF_LOSS_COLORSPACE;
559 if (ps->color_type != FF_COLOR_YUV)
560 loss |= FF_LOSS_COLORSPACE;
562 case FF_COLOR_YUV_JPEG:
563 if (ps->color_type != FF_COLOR_YUV_JPEG &&
564 ps->color_type != FF_COLOR_YUV &&
565 ps->color_type != FF_COLOR_GRAY)
566 loss |= FF_LOSS_COLORSPACE;
570 if (ps->color_type != pf->color_type)
571 loss |= FF_LOSS_COLORSPACE;
574 if (pf->color_type == FF_COLOR_GRAY &&
575 ps->color_type != FF_COLOR_GRAY)
576 loss |= FF_LOSS_CHROMA;
577 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
578 loss |= FF_LOSS_ALPHA;
579 if (pf->pixel_type == FF_PIXEL_PALETTE &&
580 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
581 loss |= FF_LOSS_COLORQUANT;
585 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
588 const PixFmtInfo *pf;
589 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
591 pf = &pix_fmt_info[pix_fmt];
592 switch(pf->pixel_type) {
593 case FF_PIXEL_PACKED:
595 case PIX_FMT_YUYV422:
596 case PIX_FMT_UYVY422:
597 case PIX_FMT_RGB565BE:
598 case PIX_FMT_RGB565LE:
599 case PIX_FMT_RGB555BE:
600 case PIX_FMT_RGB555LE:
601 case PIX_FMT_RGB444BE:
602 case PIX_FMT_RGB444LE:
603 case PIX_FMT_BGR565BE:
604 case PIX_FMT_BGR565LE:
605 case PIX_FMT_BGR555BE:
606 case PIX_FMT_BGR555LE:
607 case PIX_FMT_BGR444BE:
608 case PIX_FMT_BGR444LE:
611 case PIX_FMT_UYYVYY411:
615 bits = pf->depth * pf->nb_channels;
619 case FF_PIXEL_PLANAR:
620 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
621 bits = pf->depth * pf->nb_channels;
623 bits = pf->depth + ((2 * pf->depth) >>
624 (desc->log2_chroma_w + desc->log2_chroma_h));
627 case FF_PIXEL_PALETTE:
637 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
638 enum PixelFormat src_pix_fmt,
642 int dist, i, loss, min_dist;
643 enum PixelFormat dst_pix_fmt;
645 /* find exact color match with smallest size */
646 dst_pix_fmt = PIX_FMT_NONE;
647 min_dist = 0x7fffffff;
648 for(i = 0;i < PIX_FMT_NB; i++) {
649 if (pix_fmt_mask & (1ULL << i)) {
650 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
652 dist = avg_bits_per_pixel(i);
653 if (dist < min_dist) {
663 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
664 int has_alpha, int *loss_ptr)
666 enum PixelFormat dst_pix_fmt;
668 static const int loss_mask_order[] = {
669 ~0, /* no loss first */
672 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
678 /* try with successive loss */
681 loss_mask = loss_mask_order[i++];
682 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
683 has_alpha, loss_mask);
684 if (dst_pix_fmt >= 0)
692 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
696 #if LIBAVCODEC_VERSION_MAJOR < 53
697 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
698 const uint8_t *src, int src_wrap,
699 int width, int height)
701 av_image_copy_plane(dst, dst_wrap, src, src_wrap, width, height);
704 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
706 return av_image_get_linesize(pix_fmt, width, plane);
709 void av_picture_data_copy(uint8_t *dst_data[4], int dst_linesize[4],
710 uint8_t *src_data[4], int src_linesize[4],
711 enum PixelFormat pix_fmt, int width, int height)
713 av_image_copy(dst_data, dst_linesize, src_data, src_linesize,
714 pix_fmt, width, height);
718 void av_picture_copy(AVPicture *dst, const AVPicture *src,
719 enum PixelFormat pix_fmt, int width, int height)
721 av_image_copy(dst->data, dst->linesize, src->data,
722 src->linesize, pix_fmt, width, height);
726 void ff_shrink22(uint8_t *dst, int dst_wrap,
727 const uint8_t *src, int src_wrap,
728 int width, int height)
731 const uint8_t *s1, *s2;
734 for(;height > 0; height--) {
738 for(w = width;w >= 4; w-=4) {
739 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
740 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
741 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
742 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
748 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
759 void ff_shrink44(uint8_t *dst, int dst_wrap,
760 const uint8_t *src, int src_wrap,
761 int width, int height)
764 const uint8_t *s1, *s2, *s3, *s4;
767 for(;height > 0; height--) {
773 for(w = width;w > 0; w--) {
774 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
775 s2[0] + s2[1] + s2[2] + s2[3] +
776 s3[0] + s3[1] + s3[2] + s3[3] +
777 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
790 void ff_shrink88(uint8_t *dst, int dst_wrap,
791 const uint8_t *src, int src_wrap,
792 int width, int height)
796 for(;height > 0; height--) {
797 for(w = width;w > 0; w--) {
800 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
803 *(dst++) = (tmp + 32)>>6;
804 src += 8 - 8*src_wrap;
806 src += 8*src_wrap - 8*width;
807 dst += dst_wrap - width;
812 int avpicture_alloc(AVPicture *picture,
813 enum PixelFormat pix_fmt, int width, int height)
817 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
818 memset(picture, 0, sizeof(AVPicture));
825 void avpicture_free(AVPicture *picture)
827 av_free(picture->data[0]);
830 /* return true if yuv planar */
831 static inline int is_yuv_planar(const PixFmtInfo *ps)
833 return (ps->color_type == FF_COLOR_YUV ||
834 ps->color_type == FF_COLOR_YUV_JPEG) &&
835 ps->pixel_type == FF_PIXEL_PLANAR;
838 int av_picture_crop(AVPicture *dst, const AVPicture *src,
839 enum PixelFormat pix_fmt, int top_band, int left_band)
844 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
847 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
848 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
850 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
851 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
852 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
854 dst->linesize[0] = src->linesize[0];
855 dst->linesize[1] = src->linesize[1];
856 dst->linesize[2] = src->linesize[2];
860 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
861 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
870 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
871 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
873 for (i = 0; i < 3; i++) {
874 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
875 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
877 if (padtop || padleft) {
878 memset(dst->data[i], color[i],
879 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
882 if (padleft || padright) {
883 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
884 (dst->linesize[i] - (padright >> x_shift));
885 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
886 for (y = 0; y < yheight; y++) {
887 memset(optr, color[i], (padleft + padright) >> x_shift);
888 optr += dst->linesize[i];
892 if (src) { /* first line */
893 uint8_t *iptr = src->data[i];
894 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
895 (padleft >> x_shift);
896 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
897 iptr += src->linesize[i];
898 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
899 (dst->linesize[i] - (padright >> x_shift));
900 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
901 for (y = 0; y < yheight; y++) {
902 memset(optr, color[i], (padleft + padright) >> x_shift);
903 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
904 (width - padleft - padright) >> x_shift);
905 iptr += src->linesize[i];
906 optr += dst->linesize[i];
910 if (padbottom || padright) {
911 optr = dst->data[i] + dst->linesize[i] *
912 ((height - padbottom) >> y_shift) - (padright >> x_shift);
913 memset(optr, color[i],dst->linesize[i] *
914 (padbottom >> y_shift) + (padright >> x_shift));
920 /* NOTE: we scan all the pixels to have an exact information */
921 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
923 const unsigned char *p;
924 int src_wrap, ret, x, y;
926 uint32_t *palette = (uint32_t *)src->data[1];
929 src_wrap = src->linesize[0] - width;
931 for(y=0;y<height;y++) {
932 for(x=0;x<width;x++) {
933 a = palette[p[0]] >> 24;
935 ret |= FF_ALPHA_TRANSP;
936 } else if (a != 0xff) {
937 ret |= FF_ALPHA_SEMI_TRANSP;
946 int img_get_alpha_info(const AVPicture *src,
947 enum PixelFormat pix_fmt, int width, int height)
949 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
952 /* no alpha can be represented in format */
957 ret = get_alpha_info_pal8(src, width, height);
960 /* we do not know, so everything is indicated */
961 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
967 #if !(HAVE_MMX && HAVE_YASM)
968 /* filter parameters: [-1 4 2 4 -1] // 8 */
969 static void deinterlace_line_c(uint8_t *dst,
970 const uint8_t *lum_m4, const uint8_t *lum_m3,
971 const uint8_t *lum_m2, const uint8_t *lum_m1,
975 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
978 for(;size > 0;size--) {
980 sum += lum_m3[0] << 2;
981 sum += lum_m2[0] << 1;
982 sum += lum_m1[0] << 2;
984 dst[0] = cm[(sum + 4) >> 3];
994 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
995 uint8_t *lum_m2, uint8_t *lum_m1,
996 uint8_t *lum, int size)
998 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1001 for(;size > 0;size--) {
1003 sum += lum_m3[0] << 2;
1004 sum += lum_m2[0] << 1;
1005 lum_m4[0]=lum_m2[0];
1006 sum += lum_m1[0] << 2;
1008 lum_m2[0] = cm[(sum + 4) >> 3];
1018 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1019 top field is copied as is, but the bottom field is deinterlaced
1020 against the top field. */
1021 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1022 const uint8_t *src1, int src_wrap,
1023 int width, int height)
1025 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1030 src_0=&src_m1[src_wrap];
1031 src_p1=&src_0[src_wrap];
1032 src_p2=&src_p1[src_wrap];
1033 for(y=0;y<(height-2);y+=2) {
1034 memcpy(dst,src_m1,width);
1036 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1040 src_p1 += 2*src_wrap;
1041 src_p2 += 2*src_wrap;
1044 memcpy(dst,src_m1,width);
1047 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1050 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1051 int width, int height)
1053 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1056 buf = (uint8_t*)av_malloc(width);
1059 memcpy(buf,src_m1,width);
1060 src_0=&src_m1[src_wrap];
1061 src_p1=&src_0[src_wrap];
1062 src_p2=&src_p1[src_wrap];
1063 for(y=0;y<(height-2);y+=2) {
1064 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1067 src_p1 += 2*src_wrap;
1068 src_p2 += 2*src_wrap;
1071 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1075 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1076 enum PixelFormat pix_fmt, int width, int height)
1080 if (pix_fmt != PIX_FMT_YUV420P &&
1081 pix_fmt != PIX_FMT_YUVJ420P &&
1082 pix_fmt != PIX_FMT_YUV422P &&
1083 pix_fmt != PIX_FMT_YUVJ422P &&
1084 pix_fmt != PIX_FMT_YUV444P &&
1085 pix_fmt != PIX_FMT_YUV411P &&
1086 pix_fmt != PIX_FMT_GRAY8)
1088 if ((width & 3) != 0 || (height & 3) != 0)
1094 case PIX_FMT_YUVJ420P:
1095 case PIX_FMT_YUV420P:
1099 case PIX_FMT_YUV422P:
1100 case PIX_FMT_YUVJ422P:
1103 case PIX_FMT_YUV411P:
1109 if (pix_fmt == PIX_FMT_GRAY8) {
1114 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1117 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1118 src->data[i], src->linesize[i],