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
23 * @file libavcodec/imgconvert.c
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
35 #include "colorspace.h"
36 #include "libavutil/pixdesc.h"
40 #include "x86/dsputil_mmx.h"
43 #define xglue(x, y) x ## y
44 #define glue(x, y) xglue(x, y)
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 */
55 typedef struct PixFmtInfo {
57 uint8_t nb_channels; /**< number of channels (including alpha) */
58 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
59 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
60 uint8_t is_alpha : 1; /**< true if alpha can be specified */
61 uint8_t depth; /**< bit depth of the color components */
64 /* this table gives more information about formats */
65 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
70 .color_type = FF_COLOR_YUV,
71 .pixel_type = FF_PIXEL_PLANAR,
77 .color_type = FF_COLOR_YUV,
78 .pixel_type = FF_PIXEL_PLANAR,
84 .color_type = FF_COLOR_YUV,
85 .pixel_type = FF_PIXEL_PLANAR,
91 .color_type = FF_COLOR_YUV,
92 .pixel_type = FF_PIXEL_PACKED,
98 .color_type = FF_COLOR_YUV,
99 .pixel_type = FF_PIXEL_PACKED,
102 [PIX_FMT_YUV410P] = {
105 .color_type = FF_COLOR_YUV,
106 .pixel_type = FF_PIXEL_PLANAR,
109 [PIX_FMT_YUV411P] = {
112 .color_type = FF_COLOR_YUV,
113 .pixel_type = FF_PIXEL_PLANAR,
116 [PIX_FMT_YUV440P] = {
119 .color_type = FF_COLOR_YUV,
120 .pixel_type = FF_PIXEL_PLANAR,
123 [PIX_FMT_YUV420P16LE] = {
124 .name = "yuv420p16le",
126 .color_type = FF_COLOR_YUV,
127 .pixel_type = FF_PIXEL_PLANAR,
130 [PIX_FMT_YUV422P16LE] = {
131 .name = "yuv422p16le",
133 .color_type = FF_COLOR_YUV,
134 .pixel_type = FF_PIXEL_PLANAR,
137 [PIX_FMT_YUV444P16LE] = {
138 .name = "yuv444p16le",
140 .color_type = FF_COLOR_YUV,
141 .pixel_type = FF_PIXEL_PLANAR,
144 [PIX_FMT_YUV420P16BE] = {
145 .name = "yuv420p16be",
147 .color_type = FF_COLOR_YUV,
148 .pixel_type = FF_PIXEL_PLANAR,
151 [PIX_FMT_YUV422P16BE] = {
152 .name = "yuv422p16be",
154 .color_type = FF_COLOR_YUV,
155 .pixel_type = FF_PIXEL_PLANAR,
158 [PIX_FMT_YUV444P16BE] = {
159 .name = "yuv444p16be",
161 .color_type = FF_COLOR_YUV,
162 .pixel_type = FF_PIXEL_PLANAR,
167 /* YUV formats with alpha plane */
168 [PIX_FMT_YUVA420P] = {
171 .color_type = FF_COLOR_YUV,
172 .pixel_type = FF_PIXEL_PLANAR,
177 [PIX_FMT_YUVJ420P] = {
180 .color_type = FF_COLOR_YUV_JPEG,
181 .pixel_type = FF_PIXEL_PLANAR,
184 [PIX_FMT_YUVJ422P] = {
187 .color_type = FF_COLOR_YUV_JPEG,
188 .pixel_type = FF_PIXEL_PLANAR,
191 [PIX_FMT_YUVJ444P] = {
194 .color_type = FF_COLOR_YUV_JPEG,
195 .pixel_type = FF_PIXEL_PLANAR,
198 [PIX_FMT_YUVJ440P] = {
201 .color_type = FF_COLOR_YUV_JPEG,
202 .pixel_type = FF_PIXEL_PLANAR,
210 .color_type = FF_COLOR_RGB,
211 .pixel_type = FF_PIXEL_PACKED,
217 .color_type = FF_COLOR_RGB,
218 .pixel_type = FF_PIXEL_PACKED,
223 .nb_channels = 4, .is_alpha = 1,
224 .color_type = FF_COLOR_RGB,
225 .pixel_type = FF_PIXEL_PACKED,
228 [PIX_FMT_RGB48BE] = {
231 .color_type = FF_COLOR_RGB,
232 .pixel_type = FF_PIXEL_PACKED,
235 [PIX_FMT_RGB48LE] = {
238 .color_type = FF_COLOR_RGB,
239 .pixel_type = FF_PIXEL_PACKED,
242 [PIX_FMT_RGB565BE] = {
245 .color_type = FF_COLOR_RGB,
246 .pixel_type = FF_PIXEL_PACKED,
249 [PIX_FMT_RGB565LE] = {
252 .color_type = FF_COLOR_RGB,
253 .pixel_type = FF_PIXEL_PACKED,
256 [PIX_FMT_RGB555BE] = {
259 .color_type = FF_COLOR_RGB,
260 .pixel_type = FF_PIXEL_PACKED,
263 [PIX_FMT_RGB555LE] = {
266 .color_type = FF_COLOR_RGB,
267 .pixel_type = FF_PIXEL_PACKED,
271 /* gray / mono formats */
272 [PIX_FMT_GRAY16BE] = {
275 .color_type = FF_COLOR_GRAY,
276 .pixel_type = FF_PIXEL_PLANAR,
279 [PIX_FMT_GRAY16LE] = {
282 .color_type = FF_COLOR_GRAY,
283 .pixel_type = FF_PIXEL_PLANAR,
289 .color_type = FF_COLOR_GRAY,
290 .pixel_type = FF_PIXEL_PLANAR,
293 [PIX_FMT_MONOWHITE] = {
296 .color_type = FF_COLOR_GRAY,
297 .pixel_type = FF_PIXEL_PLANAR,
300 [PIX_FMT_MONOBLACK] = {
303 .color_type = FF_COLOR_GRAY,
304 .pixel_type = FF_PIXEL_PLANAR,
308 /* paletted formats */
311 .nb_channels = 4, .is_alpha = 1,
312 .color_type = FF_COLOR_RGB,
313 .pixel_type = FF_PIXEL_PALETTE,
316 [PIX_FMT_XVMC_MPEG2_MC] = {
319 [PIX_FMT_XVMC_MPEG2_IDCT] = {
322 [PIX_FMT_VDPAU_MPEG1] = {
323 .name = "vdpau_mpeg1",
325 [PIX_FMT_VDPAU_MPEG2] = {
326 .name = "vdpau_mpeg2",
328 [PIX_FMT_VDPAU_H264] = {
329 .name = "vdpau_h264",
331 [PIX_FMT_VDPAU_WMV3] = {
332 .name = "vdpau_wmv3",
334 [PIX_FMT_VDPAU_VC1] = {
337 [PIX_FMT_VDPAU_MPEG4] = {
338 .name = "vdpau_mpeg4",
340 [PIX_FMT_UYYVYY411] = {
343 .color_type = FF_COLOR_YUV,
344 .pixel_type = FF_PIXEL_PACKED,
349 .nb_channels = 4, .is_alpha = 1,
350 .color_type = FF_COLOR_RGB,
351 .pixel_type = FF_PIXEL_PACKED,
354 [PIX_FMT_BGR565BE] = {
357 .color_type = FF_COLOR_RGB,
358 .pixel_type = FF_PIXEL_PACKED,
361 [PIX_FMT_BGR565LE] = {
364 .color_type = FF_COLOR_RGB,
365 .pixel_type = FF_PIXEL_PACKED,
368 [PIX_FMT_BGR555BE] = {
371 .color_type = FF_COLOR_RGB,
372 .pixel_type = FF_PIXEL_PACKED,
375 [PIX_FMT_BGR555LE] = {
378 .color_type = FF_COLOR_RGB,
379 .pixel_type = FF_PIXEL_PACKED,
385 .color_type = FF_COLOR_RGB,
386 .pixel_type = FF_PIXEL_PACKED,
392 .color_type = FF_COLOR_RGB,
393 .pixel_type = FF_PIXEL_PACKED,
396 [PIX_FMT_RGB4_BYTE] = {
399 .color_type = FF_COLOR_RGB,
400 .pixel_type = FF_PIXEL_PACKED,
406 .color_type = FF_COLOR_RGB,
407 .pixel_type = FF_PIXEL_PACKED,
413 .color_type = FF_COLOR_RGB,
414 .pixel_type = FF_PIXEL_PACKED,
417 [PIX_FMT_BGR4_BYTE] = {
420 .color_type = FF_COLOR_RGB,
421 .pixel_type = FF_PIXEL_PACKED,
427 .color_type = FF_COLOR_YUV,
428 .pixel_type = FF_PIXEL_PLANAR,
434 .color_type = FF_COLOR_YUV,
435 .pixel_type = FF_PIXEL_PLANAR,
441 .nb_channels = 4, .is_alpha = 1,
442 .color_type = FF_COLOR_RGB,
443 .pixel_type = FF_PIXEL_PACKED,
448 .nb_channels = 4, .is_alpha = 1,
449 .color_type = FF_COLOR_RGB,
450 .pixel_type = FF_PIXEL_PACKED,
455 [PIX_FMT_VAAPI_MOCO] = {
456 .name = "vaapi_moco",
458 [PIX_FMT_VAAPI_IDCT] = {
459 .name = "vaapi_idct",
461 [PIX_FMT_VAAPI_VLD] = {
466 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
468 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
469 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
472 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
474 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
477 return av_pix_fmt_descriptors[pix_fmt].name;
480 static enum PixelFormat avcodec_get_pix_fmt_internal(const char *name)
484 for (i=0; i < PIX_FMT_NB; i++)
485 if (pix_fmt_info[i].name && !strcmp(pix_fmt_info[i].name, name))
491 # define X_NE(be, le) be
493 # define X_NE(be, le) le
496 enum PixelFormat avcodec_get_pix_fmt(const char *name)
498 enum PixelFormat pix_fmt;
500 if (!strcmp(name, "rgb32"))
501 name = X_NE("argb", "bgra");
502 else if (!strcmp(name, "bgr32"))
503 name = X_NE("abgr", "rgba");
505 pix_fmt = avcodec_get_pix_fmt_internal(name);
506 if (pix_fmt == PIX_FMT_NONE) {
508 snprintf(name2, sizeof(name2), "%s%s", name, X_NE("be", "le"));
509 pix_fmt = avcodec_get_pix_fmt_internal(name2);
514 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
518 snprintf (buf, buf_size,
519 "name " " nb_channels" " depth" " is_alpha"
522 PixFmtInfo info= pix_fmt_info[pix_fmt];
524 char is_alpha_char= info.is_alpha ? 'y' : 'n';
526 snprintf (buf, buf_size,
536 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
538 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
541 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
544 for(i=0; i<256; i++){
558 case PIX_FMT_RGB4_BYTE:
563 case PIX_FMT_BGR4_BYTE:
574 pal[i] = b + (g<<8) + (r<<16);
580 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
583 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
585 memset(picture->linesize, 0, sizeof(picture->linesize));
588 case PIX_FMT_YUV420P:
589 case PIX_FMT_YUV422P:
590 case PIX_FMT_YUV444P:
591 case PIX_FMT_YUV410P:
592 case PIX_FMT_YUV411P:
593 case PIX_FMT_YUV440P:
594 case PIX_FMT_YUVJ420P:
595 case PIX_FMT_YUVJ422P:
596 case PIX_FMT_YUVJ444P:
597 case PIX_FMT_YUVJ440P:
598 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
599 picture->linesize[0] = width;
600 picture->linesize[1] = w2;
601 picture->linesize[2] = w2;
603 case PIX_FMT_YUV420P16LE:
604 case PIX_FMT_YUV422P16LE:
605 case PIX_FMT_YUV444P16LE:
606 case PIX_FMT_YUV420P16BE:
607 case PIX_FMT_YUV422P16BE:
608 case PIX_FMT_YUV444P16BE:
609 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
610 picture->linesize[0] = 2*width;
611 picture->linesize[1] = 2*w2;
612 picture->linesize[2] = 2*w2;
614 case PIX_FMT_YUVA420P:
615 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
616 picture->linesize[0] = width;
617 picture->linesize[1] = w2;
618 picture->linesize[2] = w2;
619 picture->linesize[3] = width;
623 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
624 picture->linesize[0] = width;
625 picture->linesize[1] = 2 * w2;
629 picture->linesize[0] = width * 3;
635 picture->linesize[0] = width * 4;
637 case PIX_FMT_RGB48BE:
638 case PIX_FMT_RGB48LE:
639 picture->linesize[0] = width * 6;
641 case PIX_FMT_GRAY16BE:
642 case PIX_FMT_GRAY16LE:
643 case PIX_FMT_BGR555BE:
644 case PIX_FMT_BGR555LE:
645 case PIX_FMT_BGR565BE:
646 case PIX_FMT_BGR565LE:
647 case PIX_FMT_RGB555BE:
648 case PIX_FMT_RGB555LE:
649 case PIX_FMT_RGB565BE:
650 case PIX_FMT_RGB565LE:
651 case PIX_FMT_YUYV422:
652 picture->linesize[0] = width * 2;
654 case PIX_FMT_UYVY422:
655 picture->linesize[0] = width * 2;
657 case PIX_FMT_UYYVYY411:
658 picture->linesize[0] = width + width/2;
662 picture->linesize[0] = width / 2;
664 case PIX_FMT_MONOWHITE:
665 case PIX_FMT_MONOBLACK:
666 picture->linesize[0] = (width + 7) >> 3;
671 case PIX_FMT_RGB4_BYTE:
672 case PIX_FMT_BGR4_BYTE:
674 picture->linesize[0] = width;
682 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
686 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
688 size = picture->linesize[0] * height;
690 case PIX_FMT_YUV420P:
691 case PIX_FMT_YUV422P:
692 case PIX_FMT_YUV444P:
693 case PIX_FMT_YUV410P:
694 case PIX_FMT_YUV411P:
695 case PIX_FMT_YUV440P:
696 case PIX_FMT_YUVJ420P:
697 case PIX_FMT_YUVJ422P:
698 case PIX_FMT_YUVJ444P:
699 case PIX_FMT_YUVJ440P:
700 case PIX_FMT_YUV420P16LE:
701 case PIX_FMT_YUV422P16LE:
702 case PIX_FMT_YUV444P16LE:
703 case PIX_FMT_YUV420P16BE:
704 case PIX_FMT_YUV422P16BE:
705 case PIX_FMT_YUV444P16BE:
706 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
707 size2 = picture->linesize[1] * h2;
708 picture->data[0] = ptr;
709 picture->data[1] = picture->data[0] + size;
710 picture->data[2] = picture->data[1] + size2;
711 picture->data[3] = NULL;
712 return size + 2 * size2;
713 case PIX_FMT_YUVA420P:
714 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
715 size2 = picture->linesize[1] * h2;
716 picture->data[0] = ptr;
717 picture->data[1] = picture->data[0] + size;
718 picture->data[2] = picture->data[1] + size2;
719 picture->data[3] = picture->data[1] + size2 + size2;
720 return 2 * size + 2 * size2;
723 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
724 size2 = picture->linesize[1] * h2;
725 picture->data[0] = ptr;
726 picture->data[1] = picture->data[0] + size;
727 picture->data[2] = NULL;
728 picture->data[3] = NULL;
736 case PIX_FMT_RGB48BE:
737 case PIX_FMT_RGB48LE:
738 case PIX_FMT_GRAY16BE:
739 case PIX_FMT_GRAY16LE:
740 case PIX_FMT_BGR555BE:
741 case PIX_FMT_BGR555LE:
742 case PIX_FMT_BGR565BE:
743 case PIX_FMT_BGR565LE:
744 case PIX_FMT_RGB555BE:
745 case PIX_FMT_RGB555LE:
746 case PIX_FMT_RGB565BE:
747 case PIX_FMT_RGB565LE:
748 case PIX_FMT_YUYV422:
749 case PIX_FMT_UYVY422:
750 case PIX_FMT_UYYVYY411:
753 case PIX_FMT_MONOWHITE:
754 case PIX_FMT_MONOBLACK:
755 picture->data[0] = ptr;
756 picture->data[1] = NULL;
757 picture->data[2] = NULL;
758 picture->data[3] = NULL;
763 case PIX_FMT_RGB4_BYTE:
764 case PIX_FMT_BGR4_BYTE:
766 size2 = (size + 3) & ~3;
767 picture->data[0] = ptr;
768 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
769 picture->data[2] = NULL;
770 picture->data[3] = NULL;
771 return size2 + 256 * 4;
773 picture->data[0] = NULL;
774 picture->data[1] = NULL;
775 picture->data[2] = NULL;
776 picture->data[3] = NULL;
781 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
782 enum PixelFormat pix_fmt, int width, int height)
785 if(avcodec_check_dimensions(NULL, width, height))
788 if (ff_fill_linesize(picture, pix_fmt, width))
791 return ff_fill_pointer(picture, ptr, pix_fmt, height);
794 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
795 unsigned char *dest, int dest_size)
797 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
798 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
799 int i, j, w, ow, h, oh, data_planes;
800 const unsigned char* s;
801 int size = avpicture_get_size(pix_fmt, width, height);
803 if (size > dest_size || size < 0)
806 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
807 if (pix_fmt == PIX_FMT_YUYV422 ||
808 pix_fmt == PIX_FMT_UYVY422 ||
809 pix_fmt == PIX_FMT_BGR565BE ||
810 pix_fmt == PIX_FMT_BGR565LE ||
811 pix_fmt == PIX_FMT_BGR555BE ||
812 pix_fmt == PIX_FMT_BGR555LE ||
813 pix_fmt == PIX_FMT_RGB565BE ||
814 pix_fmt == PIX_FMT_RGB565LE ||
815 pix_fmt == PIX_FMT_RGB555BE ||
816 pix_fmt == PIX_FMT_RGB555LE)
818 else if (pix_fmt == PIX_FMT_UYYVYY411)
820 else if (pix_fmt == PIX_FMT_PAL8)
823 w = width * (pf->depth * pf->nb_channels / 8);
828 data_planes = pf->nb_channels;
829 w = (width*pf->depth + 7)/8;
836 for (i=0; i<data_planes; i++) {
838 w = ((width >> desc->log2_chroma_w) * pf->depth + 7) / 8;
839 h = height >> desc->log2_chroma_h;
840 if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
850 s += src->linesize[i];
854 if (pf->pixel_type == FF_PIXEL_PALETTE)
855 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
860 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
862 AVPicture dummy_pict;
863 if(avcodec_check_dimensions(NULL, width, height))
868 case PIX_FMT_RGB4_BYTE:
869 case PIX_FMT_BGR4_BYTE:
871 // do not include palette for these pseudo-paletted formats
872 return width * height;
874 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
877 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
880 const PixFmtInfo *pf, *ps;
881 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
882 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
885 ps = &pix_fmt_info[src_pix_fmt];
889 pf = &pix_fmt_info[dst_pix_fmt];
890 if (pf->depth < ps->depth ||
891 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE) &&
892 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE)))
893 loss |= FF_LOSS_DEPTH;
894 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
895 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
896 loss |= FF_LOSS_RESOLUTION;
897 switch(pf->color_type) {
899 if (ps->color_type != FF_COLOR_RGB &&
900 ps->color_type != FF_COLOR_GRAY)
901 loss |= FF_LOSS_COLORSPACE;
904 if (ps->color_type != FF_COLOR_GRAY)
905 loss |= FF_LOSS_COLORSPACE;
908 if (ps->color_type != FF_COLOR_YUV)
909 loss |= FF_LOSS_COLORSPACE;
911 case FF_COLOR_YUV_JPEG:
912 if (ps->color_type != FF_COLOR_YUV_JPEG &&
913 ps->color_type != FF_COLOR_YUV &&
914 ps->color_type != FF_COLOR_GRAY)
915 loss |= FF_LOSS_COLORSPACE;
919 if (ps->color_type != pf->color_type)
920 loss |= FF_LOSS_COLORSPACE;
923 if (pf->color_type == FF_COLOR_GRAY &&
924 ps->color_type != FF_COLOR_GRAY)
925 loss |= FF_LOSS_CHROMA;
926 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
927 loss |= FF_LOSS_ALPHA;
928 if (pf->pixel_type == FF_PIXEL_PALETTE &&
929 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
930 loss |= FF_LOSS_COLORQUANT;
934 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
937 const PixFmtInfo *pf;
938 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
940 pf = &pix_fmt_info[pix_fmt];
941 switch(pf->pixel_type) {
942 case FF_PIXEL_PACKED:
944 case PIX_FMT_YUYV422:
945 case PIX_FMT_UYVY422:
946 case PIX_FMT_RGB565BE:
947 case PIX_FMT_RGB565LE:
948 case PIX_FMT_RGB555BE:
949 case PIX_FMT_RGB555LE:
950 case PIX_FMT_BGR565BE:
951 case PIX_FMT_BGR565LE:
952 case PIX_FMT_BGR555BE:
953 case PIX_FMT_BGR555LE:
956 case PIX_FMT_UYYVYY411:
960 bits = pf->depth * pf->nb_channels;
964 case FF_PIXEL_PLANAR:
965 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
966 bits = pf->depth * pf->nb_channels;
968 bits = pf->depth + ((2 * pf->depth) >>
969 (desc->log2_chroma_w + desc->log2_chroma_h));
972 case FF_PIXEL_PALETTE:
982 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
983 enum PixelFormat src_pix_fmt,
987 int dist, i, loss, min_dist;
988 enum PixelFormat dst_pix_fmt;
990 /* find exact color match with smallest size */
992 min_dist = 0x7fffffff;
993 for(i = 0;i < PIX_FMT_NB; i++) {
994 if (pix_fmt_mask & (1ULL << i)) {
995 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
997 dist = avg_bits_per_pixel(i);
998 if (dist < min_dist) {
1008 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
1009 int has_alpha, int *loss_ptr)
1011 enum PixelFormat dst_pix_fmt;
1013 static const int loss_mask_order[] = {
1014 ~0, /* no loss first */
1016 ~FF_LOSS_RESOLUTION,
1017 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
1018 ~FF_LOSS_COLORQUANT,
1023 /* try with successive loss */
1026 loss_mask = loss_mask_order[i++];
1027 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
1028 has_alpha, loss_mask);
1029 if (dst_pix_fmt >= 0)
1037 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
1041 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
1042 const uint8_t *src, int src_wrap,
1043 int width, int height)
1045 if((!dst) || (!src))
1047 for(;height > 0; height--) {
1048 memcpy(dst, src, width);
1054 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
1057 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1058 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1060 pf = &pix_fmt_info[pix_fmt];
1061 switch(pf->pixel_type) {
1062 case FF_PIXEL_PACKED:
1064 case PIX_FMT_YUYV422:
1065 case PIX_FMT_UYVY422:
1066 case PIX_FMT_RGB565BE:
1067 case PIX_FMT_RGB565LE:
1068 case PIX_FMT_RGB555BE:
1069 case PIX_FMT_RGB555LE:
1070 case PIX_FMT_BGR565BE:
1071 case PIX_FMT_BGR565LE:
1072 case PIX_FMT_BGR555BE:
1073 case PIX_FMT_BGR555LE:
1076 case PIX_FMT_UYYVYY411:
1080 bits = pf->depth * pf->nb_channels;
1083 return (width * bits + 7) >> 3;
1085 case FF_PIXEL_PLANAR:
1086 if (plane == 1 || plane == 2)
1087 width= -((-width)>>desc->log2_chroma_w);
1089 return (width * pf->depth + 7) >> 3;
1091 case FF_PIXEL_PALETTE:
1100 void av_picture_copy(AVPicture *dst, const AVPicture *src,
1101 enum PixelFormat pix_fmt, int width, int height)
1104 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1105 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1107 switch(pf->pixel_type) {
1108 case FF_PIXEL_PACKED:
1109 case FF_PIXEL_PLANAR:
1110 for(i = 0; i < pf->nb_channels; i++) {
1112 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
1114 if (i == 1 || i == 2) {
1115 h= -((-height)>>desc->log2_chroma_h);
1117 ff_img_copy_plane(dst->data[i], dst->linesize[i],
1118 src->data[i], src->linesize[i],
1122 case FF_PIXEL_PALETTE:
1123 ff_img_copy_plane(dst->data[0], dst->linesize[0],
1124 src->data[0], src->linesize[0],
1126 /* copy the palette */
1127 memcpy(dst->data[1], src->data[1], 4*256);
1133 void ff_shrink22(uint8_t *dst, int dst_wrap,
1134 const uint8_t *src, int src_wrap,
1135 int width, int height)
1138 const uint8_t *s1, *s2;
1141 for(;height > 0; height--) {
1145 for(w = width;w >= 4; w-=4) {
1146 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1147 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1148 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1149 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1155 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1160 src += 2 * src_wrap;
1166 void ff_shrink44(uint8_t *dst, int dst_wrap,
1167 const uint8_t *src, int src_wrap,
1168 int width, int height)
1171 const uint8_t *s1, *s2, *s3, *s4;
1174 for(;height > 0; height--) {
1180 for(w = width;w > 0; w--) {
1181 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1182 s2[0] + s2[1] + s2[2] + s2[3] +
1183 s3[0] + s3[1] + s3[2] + s3[3] +
1184 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1191 src += 4 * src_wrap;
1197 void ff_shrink88(uint8_t *dst, int dst_wrap,
1198 const uint8_t *src, int src_wrap,
1199 int width, int height)
1203 for(;height > 0; height--) {
1204 for(w = width;w > 0; w--) {
1207 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1210 *(dst++) = (tmp + 32)>>6;
1211 src += 8 - 8*src_wrap;
1213 src += 8*src_wrap - 8*width;
1214 dst += dst_wrap - width;
1219 int avpicture_alloc(AVPicture *picture,
1220 enum PixelFormat pix_fmt, int width, int height)
1225 size = avpicture_fill(picture, NULL, pix_fmt, width, height);
1228 ptr = av_malloc(size);
1231 avpicture_fill(picture, ptr, pix_fmt, width, height);
1232 if(picture->data[1] && !picture->data[2])
1233 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
1237 memset(picture, 0, sizeof(AVPicture));
1241 void avpicture_free(AVPicture *picture)
1243 av_free(picture->data[0]);
1246 /* return true if yuv planar */
1247 static inline int is_yuv_planar(const PixFmtInfo *ps)
1249 return (ps->color_type == FF_COLOR_YUV ||
1250 ps->color_type == FF_COLOR_YUV_JPEG) &&
1251 ps->pixel_type == FF_PIXEL_PLANAR;
1254 int av_picture_crop(AVPicture *dst, const AVPicture *src,
1255 enum PixelFormat pix_fmt, int top_band, int left_band)
1260 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1263 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
1264 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
1266 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1267 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1268 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1270 dst->linesize[0] = src->linesize[0];
1271 dst->linesize[1] = src->linesize[1];
1272 dst->linesize[2] = src->linesize[2];
1276 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
1277 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
1286 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1287 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
1289 for (i = 0; i < 3; i++) {
1290 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
1291 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
1293 if (padtop || padleft) {
1294 memset(dst->data[i], color[i],
1295 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
1298 if (padleft || padright) {
1299 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1300 (dst->linesize[i] - (padright >> x_shift));
1301 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1302 for (y = 0; y < yheight; y++) {
1303 memset(optr, color[i], (padleft + padright) >> x_shift);
1304 optr += dst->linesize[i];
1308 if (src) { /* first line */
1309 uint8_t *iptr = src->data[i];
1310 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1311 (padleft >> x_shift);
1312 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
1313 iptr += src->linesize[i];
1314 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1315 (dst->linesize[i] - (padright >> x_shift));
1316 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1317 for (y = 0; y < yheight; y++) {
1318 memset(optr, color[i], (padleft + padright) >> x_shift);
1319 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1320 (width - padleft - padright) >> x_shift);
1321 iptr += src->linesize[i];
1322 optr += dst->linesize[i];
1326 if (padbottom || padright) {
1327 optr = dst->data[i] + dst->linesize[i] *
1328 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1329 memset(optr, color[i],dst->linesize[i] *
1330 (padbottom >> y_shift) + (padright >> x_shift));
1336 /* NOTE: we scan all the pixels to have an exact information */
1337 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1339 const unsigned char *p;
1340 int src_wrap, ret, x, y;
1342 uint32_t *palette = (uint32_t *)src->data[1];
1345 src_wrap = src->linesize[0] - width;
1347 for(y=0;y<height;y++) {
1348 for(x=0;x<width;x++) {
1349 a = palette[p[0]] >> 24;
1351 ret |= FF_ALPHA_TRANSP;
1352 } else if (a != 0xff) {
1353 ret |= FF_ALPHA_SEMI_TRANSP;
1362 int img_get_alpha_info(const AVPicture *src,
1363 enum PixelFormat pix_fmt, int width, int height)
1365 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1368 /* no alpha can be represented in format */
1373 ret = get_alpha_info_pal8(src, width, height);
1376 /* we do not know, so everything is indicated */
1377 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1384 #define DEINT_INPLACE_LINE_LUM \
1385 movd_m2r(lum_m4[0],mm0);\
1386 movd_m2r(lum_m3[0],mm1);\
1387 movd_m2r(lum_m2[0],mm2);\
1388 movd_m2r(lum_m1[0],mm3);\
1389 movd_m2r(lum[0],mm4);\
1390 punpcklbw_r2r(mm7,mm0);\
1391 movd_r2m(mm2,lum_m4[0]);\
1392 punpcklbw_r2r(mm7,mm1);\
1393 punpcklbw_r2r(mm7,mm2);\
1394 punpcklbw_r2r(mm7,mm3);\
1395 punpcklbw_r2r(mm7,mm4);\
1396 paddw_r2r(mm3,mm1);\
1398 paddw_r2r(mm4,mm0);\
1400 paddw_r2r(mm6,mm2);\
1401 paddw_r2r(mm2,mm1);\
1402 psubusw_r2r(mm0,mm1);\
1404 packuswb_r2r(mm7,mm1);\
1405 movd_r2m(mm1,lum_m2[0]);
1407 #define DEINT_LINE_LUM \
1408 movd_m2r(lum_m4[0],mm0);\
1409 movd_m2r(lum_m3[0],mm1);\
1410 movd_m2r(lum_m2[0],mm2);\
1411 movd_m2r(lum_m1[0],mm3);\
1412 movd_m2r(lum[0],mm4);\
1413 punpcklbw_r2r(mm7,mm0);\
1414 punpcklbw_r2r(mm7,mm1);\
1415 punpcklbw_r2r(mm7,mm2);\
1416 punpcklbw_r2r(mm7,mm3);\
1417 punpcklbw_r2r(mm7,mm4);\
1418 paddw_r2r(mm3,mm1);\
1420 paddw_r2r(mm4,mm0);\
1422 paddw_r2r(mm6,mm2);\
1423 paddw_r2r(mm2,mm1);\
1424 psubusw_r2r(mm0,mm1);\
1426 packuswb_r2r(mm7,mm1);\
1427 movd_r2m(mm1,dst[0]);
1430 /* filter parameters: [-1 4 2 4 -1] // 8 */
1431 static void deinterlace_line(uint8_t *dst,
1432 const uint8_t *lum_m4, const uint8_t *lum_m3,
1433 const uint8_t *lum_m2, const uint8_t *lum_m1,
1438 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1441 for(;size > 0;size--) {
1443 sum += lum_m3[0] << 2;
1444 sum += lum_m2[0] << 1;
1445 sum += lum_m1[0] << 2;
1447 dst[0] = cm[(sum + 4) >> 3];
1459 movq_m2r(ff_pw_4,mm6);
1461 for (;size > 3; size-=4) {
1472 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1476 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1479 for(;size > 0;size--) {
1481 sum += lum_m3[0] << 2;
1482 sum += lum_m2[0] << 1;
1483 lum_m4[0]=lum_m2[0];
1484 sum += lum_m1[0] << 2;
1486 lum_m2[0] = cm[(sum + 4) >> 3];
1497 movq_m2r(ff_pw_4,mm6);
1499 for (;size > 3; size-=4) {
1500 DEINT_INPLACE_LINE_LUM
1510 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1511 top field is copied as is, but the bottom field is deinterlaced
1512 against the top field. */
1513 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1514 const uint8_t *src1, int src_wrap,
1515 int width, int height)
1517 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1522 src_0=&src_m1[src_wrap];
1523 src_p1=&src_0[src_wrap];
1524 src_p2=&src_p1[src_wrap];
1525 for(y=0;y<(height-2);y+=2) {
1526 memcpy(dst,src_m1,width);
1528 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1532 src_p1 += 2*src_wrap;
1533 src_p2 += 2*src_wrap;
1536 memcpy(dst,src_m1,width);
1539 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1542 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1543 int width, int height)
1545 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1548 buf = (uint8_t*)av_malloc(width);
1551 memcpy(buf,src_m1,width);
1552 src_0=&src_m1[src_wrap];
1553 src_p1=&src_0[src_wrap];
1554 src_p2=&src_p1[src_wrap];
1555 for(y=0;y<(height-2);y+=2) {
1556 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1559 src_p1 += 2*src_wrap;
1560 src_p2 += 2*src_wrap;
1563 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1567 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1568 enum PixelFormat pix_fmt, int width, int height)
1572 if (pix_fmt != PIX_FMT_YUV420P &&
1573 pix_fmt != PIX_FMT_YUV422P &&
1574 pix_fmt != PIX_FMT_YUV444P &&
1575 pix_fmt != PIX_FMT_YUV411P &&
1576 pix_fmt != PIX_FMT_GRAY8)
1578 if ((width & 3) != 0 || (height & 3) != 0)
1584 case PIX_FMT_YUV420P:
1588 case PIX_FMT_YUV422P:
1591 case PIX_FMT_YUV411P:
1597 if (pix_fmt == PIX_FMT_GRAY8) {
1602 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1605 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1606 src->data[i], src->linesize[i],