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 is_hwaccel : 1; /**< true if this is an HW accelerated format */
62 uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
63 uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
64 uint8_t depth; /**< bit depth of the color components */
67 /* this table gives more information about formats */
68 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
73 .color_type = FF_COLOR_YUV,
74 .pixel_type = FF_PIXEL_PLANAR,
76 .x_chroma_shift = 1, .y_chroma_shift = 1,
81 .color_type = FF_COLOR_YUV,
82 .pixel_type = FF_PIXEL_PLANAR,
84 .x_chroma_shift = 1, .y_chroma_shift = 0,
89 .color_type = FF_COLOR_YUV,
90 .pixel_type = FF_PIXEL_PLANAR,
92 .x_chroma_shift = 0, .y_chroma_shift = 0,
97 .color_type = FF_COLOR_YUV,
98 .pixel_type = FF_PIXEL_PACKED,
100 .x_chroma_shift = 1, .y_chroma_shift = 0,
102 [PIX_FMT_UYVY422] = {
105 .color_type = FF_COLOR_YUV,
106 .pixel_type = FF_PIXEL_PACKED,
108 .x_chroma_shift = 1, .y_chroma_shift = 0,
110 [PIX_FMT_YUV410P] = {
113 .color_type = FF_COLOR_YUV,
114 .pixel_type = FF_PIXEL_PLANAR,
116 .x_chroma_shift = 2, .y_chroma_shift = 2,
118 [PIX_FMT_YUV411P] = {
121 .color_type = FF_COLOR_YUV,
122 .pixel_type = FF_PIXEL_PLANAR,
124 .x_chroma_shift = 2, .y_chroma_shift = 0,
126 [PIX_FMT_YUV440P] = {
129 .color_type = FF_COLOR_YUV,
130 .pixel_type = FF_PIXEL_PLANAR,
132 .x_chroma_shift = 0, .y_chroma_shift = 1,
134 [PIX_FMT_YUV420P16LE] = {
135 .name = "yuv420p16le",
137 .color_type = FF_COLOR_YUV,
138 .pixel_type = FF_PIXEL_PLANAR,
140 .x_chroma_shift = 1, .y_chroma_shift = 1,
142 [PIX_FMT_YUV422P16LE] = {
143 .name = "yuv422p16le",
145 .color_type = FF_COLOR_YUV,
146 .pixel_type = FF_PIXEL_PLANAR,
148 .x_chroma_shift = 1, .y_chroma_shift = 0,
150 [PIX_FMT_YUV444P16LE] = {
151 .name = "yuv444p16le",
153 .color_type = FF_COLOR_YUV,
154 .pixel_type = FF_PIXEL_PLANAR,
156 .x_chroma_shift = 0, .y_chroma_shift = 0,
158 [PIX_FMT_YUV420P16BE] = {
159 .name = "yuv420p16be",
161 .color_type = FF_COLOR_YUV,
162 .pixel_type = FF_PIXEL_PLANAR,
164 .x_chroma_shift = 1, .y_chroma_shift = 1,
166 [PIX_FMT_YUV422P16BE] = {
167 .name = "yuv422p16be",
169 .color_type = FF_COLOR_YUV,
170 .pixel_type = FF_PIXEL_PLANAR,
172 .x_chroma_shift = 1, .y_chroma_shift = 0,
174 [PIX_FMT_YUV444P16BE] = {
175 .name = "yuv444p16be",
177 .color_type = FF_COLOR_YUV,
178 .pixel_type = FF_PIXEL_PLANAR,
180 .x_chroma_shift = 0, .y_chroma_shift = 0,
184 /* YUV formats with alpha plane */
185 [PIX_FMT_YUVA420P] = {
188 .color_type = FF_COLOR_YUV,
189 .pixel_type = FF_PIXEL_PLANAR,
191 .x_chroma_shift = 1, .y_chroma_shift = 1,
195 [PIX_FMT_YUVJ420P] = {
198 .color_type = FF_COLOR_YUV_JPEG,
199 .pixel_type = FF_PIXEL_PLANAR,
201 .x_chroma_shift = 1, .y_chroma_shift = 1,
203 [PIX_FMT_YUVJ422P] = {
206 .color_type = FF_COLOR_YUV_JPEG,
207 .pixel_type = FF_PIXEL_PLANAR,
209 .x_chroma_shift = 1, .y_chroma_shift = 0,
211 [PIX_FMT_YUVJ444P] = {
214 .color_type = FF_COLOR_YUV_JPEG,
215 .pixel_type = FF_PIXEL_PLANAR,
217 .x_chroma_shift = 0, .y_chroma_shift = 0,
219 [PIX_FMT_YUVJ440P] = {
222 .color_type = FF_COLOR_YUV_JPEG,
223 .pixel_type = FF_PIXEL_PLANAR,
225 .x_chroma_shift = 0, .y_chroma_shift = 1,
232 .color_type = FF_COLOR_RGB,
233 .pixel_type = FF_PIXEL_PACKED,
235 .x_chroma_shift = 0, .y_chroma_shift = 0,
240 .color_type = FF_COLOR_RGB,
241 .pixel_type = FF_PIXEL_PACKED,
243 .x_chroma_shift = 0, .y_chroma_shift = 0,
247 .nb_channels = 4, .is_alpha = 1,
248 .color_type = FF_COLOR_RGB,
249 .pixel_type = FF_PIXEL_PACKED,
251 .x_chroma_shift = 0, .y_chroma_shift = 0,
253 [PIX_FMT_RGB48BE] = {
256 .color_type = FF_COLOR_RGB,
257 .pixel_type = FF_PIXEL_PACKED,
259 .x_chroma_shift = 0, .y_chroma_shift = 0,
261 [PIX_FMT_RGB48LE] = {
264 .color_type = FF_COLOR_RGB,
265 .pixel_type = FF_PIXEL_PACKED,
267 .x_chroma_shift = 0, .y_chroma_shift = 0,
269 [PIX_FMT_RGB565BE] = {
272 .color_type = FF_COLOR_RGB,
273 .pixel_type = FF_PIXEL_PACKED,
275 .x_chroma_shift = 0, .y_chroma_shift = 0,
277 [PIX_FMT_RGB565LE] = {
280 .color_type = FF_COLOR_RGB,
281 .pixel_type = FF_PIXEL_PACKED,
283 .x_chroma_shift = 0, .y_chroma_shift = 0,
285 [PIX_FMT_RGB555BE] = {
288 .color_type = FF_COLOR_RGB,
289 .pixel_type = FF_PIXEL_PACKED,
291 .x_chroma_shift = 0, .y_chroma_shift = 0,
293 [PIX_FMT_RGB555LE] = {
296 .color_type = FF_COLOR_RGB,
297 .pixel_type = FF_PIXEL_PACKED,
299 .x_chroma_shift = 0, .y_chroma_shift = 0,
302 /* gray / mono formats */
303 [PIX_FMT_GRAY16BE] = {
306 .color_type = FF_COLOR_GRAY,
307 .pixel_type = FF_PIXEL_PLANAR,
310 [PIX_FMT_GRAY16LE] = {
313 .color_type = FF_COLOR_GRAY,
314 .pixel_type = FF_PIXEL_PLANAR,
320 .color_type = FF_COLOR_GRAY,
321 .pixel_type = FF_PIXEL_PLANAR,
324 [PIX_FMT_MONOWHITE] = {
327 .color_type = FF_COLOR_GRAY,
328 .pixel_type = FF_PIXEL_PLANAR,
331 [PIX_FMT_MONOBLACK] = {
334 .color_type = FF_COLOR_GRAY,
335 .pixel_type = FF_PIXEL_PLANAR,
339 /* paletted formats */
342 .nb_channels = 4, .is_alpha = 1,
343 .color_type = FF_COLOR_RGB,
344 .pixel_type = FF_PIXEL_PALETTE,
347 [PIX_FMT_XVMC_MPEG2_MC] = {
351 [PIX_FMT_XVMC_MPEG2_IDCT] = {
355 [PIX_FMT_VDPAU_MPEG1] = {
356 .name = "vdpau_mpeg1",
358 .x_chroma_shift = 1, .y_chroma_shift = 1,
360 [PIX_FMT_VDPAU_MPEG2] = {
361 .name = "vdpau_mpeg2",
363 .x_chroma_shift = 1, .y_chroma_shift = 1,
365 [PIX_FMT_VDPAU_H264] = {
366 .name = "vdpau_h264",
368 .x_chroma_shift = 1, .y_chroma_shift = 1,
370 [PIX_FMT_VDPAU_WMV3] = {
371 .name = "vdpau_wmv3",
373 .x_chroma_shift = 1, .y_chroma_shift = 1,
375 [PIX_FMT_VDPAU_VC1] = {
378 .x_chroma_shift = 1, .y_chroma_shift = 1,
380 [PIX_FMT_VDPAU_MPEG4] = {
381 .name = "vdpau_mpeg4",
383 .x_chroma_shift = 1, .y_chroma_shift = 1,
385 [PIX_FMT_UYYVYY411] = {
388 .color_type = FF_COLOR_YUV,
389 .pixel_type = FF_PIXEL_PACKED,
391 .x_chroma_shift = 2, .y_chroma_shift = 0,
395 .nb_channels = 4, .is_alpha = 1,
396 .color_type = FF_COLOR_RGB,
397 .pixel_type = FF_PIXEL_PACKED,
399 .x_chroma_shift = 0, .y_chroma_shift = 0,
401 [PIX_FMT_BGR565BE] = {
404 .color_type = FF_COLOR_RGB,
405 .pixel_type = FF_PIXEL_PACKED,
407 .x_chroma_shift = 0, .y_chroma_shift = 0,
409 [PIX_FMT_BGR565LE] = {
412 .color_type = FF_COLOR_RGB,
413 .pixel_type = FF_PIXEL_PACKED,
415 .x_chroma_shift = 0, .y_chroma_shift = 0,
417 [PIX_FMT_BGR555BE] = {
420 .color_type = FF_COLOR_RGB,
421 .pixel_type = FF_PIXEL_PACKED,
423 .x_chroma_shift = 0, .y_chroma_shift = 0,
425 [PIX_FMT_BGR555LE] = {
428 .color_type = FF_COLOR_RGB,
429 .pixel_type = FF_PIXEL_PACKED,
431 .x_chroma_shift = 0, .y_chroma_shift = 0,
436 .color_type = FF_COLOR_RGB,
437 .pixel_type = FF_PIXEL_PACKED,
439 .x_chroma_shift = 0, .y_chroma_shift = 0,
444 .color_type = FF_COLOR_RGB,
445 .pixel_type = FF_PIXEL_PACKED,
447 .x_chroma_shift = 0, .y_chroma_shift = 0,
449 [PIX_FMT_RGB4_BYTE] = {
452 .color_type = FF_COLOR_RGB,
453 .pixel_type = FF_PIXEL_PACKED,
455 .x_chroma_shift = 0, .y_chroma_shift = 0,
460 .color_type = FF_COLOR_RGB,
461 .pixel_type = FF_PIXEL_PACKED,
463 .x_chroma_shift = 0, .y_chroma_shift = 0,
468 .color_type = FF_COLOR_RGB,
469 .pixel_type = FF_PIXEL_PACKED,
471 .x_chroma_shift = 0, .y_chroma_shift = 0,
473 [PIX_FMT_BGR4_BYTE] = {
476 .color_type = FF_COLOR_RGB,
477 .pixel_type = FF_PIXEL_PACKED,
479 .x_chroma_shift = 0, .y_chroma_shift = 0,
484 .color_type = FF_COLOR_YUV,
485 .pixel_type = FF_PIXEL_PLANAR,
487 .x_chroma_shift = 1, .y_chroma_shift = 1,
492 .color_type = FF_COLOR_YUV,
493 .pixel_type = FF_PIXEL_PLANAR,
495 .x_chroma_shift = 1, .y_chroma_shift = 1,
500 .nb_channels = 4, .is_alpha = 1,
501 .color_type = FF_COLOR_RGB,
502 .pixel_type = FF_PIXEL_PACKED,
504 .x_chroma_shift = 0, .y_chroma_shift = 0,
508 .nb_channels = 4, .is_alpha = 1,
509 .color_type = FF_COLOR_RGB,
510 .pixel_type = FF_PIXEL_PACKED,
512 .x_chroma_shift = 0, .y_chroma_shift = 0,
516 [PIX_FMT_VAAPI_MOCO] = {
517 .name = "vaapi_moco",
519 .x_chroma_shift = 1, .y_chroma_shift = 1,
521 [PIX_FMT_VAAPI_IDCT] = {
522 .name = "vaapi_idct",
524 .x_chroma_shift = 1, .y_chroma_shift = 1,
526 [PIX_FMT_VAAPI_VLD] = {
529 .x_chroma_shift = 1, .y_chroma_shift = 1,
533 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
535 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
536 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
539 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
541 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
544 return pix_fmt_info[pix_fmt].name;
547 static enum PixelFormat avcodec_get_pix_fmt_internal(const char *name)
551 for (i=0; i < PIX_FMT_NB; i++)
552 if (pix_fmt_info[i].name && !strcmp(pix_fmt_info[i].name, name))
558 # define X_NE(be, le) be
560 # define X_NE(be, le) le
563 enum PixelFormat avcodec_get_pix_fmt(const char *name)
565 enum PixelFormat pix_fmt;
567 if (!strcmp(name, "rgb32"))
568 name = X_NE("argb", "bgra");
569 else if (!strcmp(name, "bgr32"))
570 name = X_NE("abgr", "rgba");
572 pix_fmt = avcodec_get_pix_fmt_internal(name);
573 if (pix_fmt == PIX_FMT_NONE) {
575 snprintf(name2, sizeof(name2), "%s%s", name, X_NE("be", "le"));
576 pix_fmt = avcodec_get_pix_fmt_internal(name2);
581 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
585 snprintf (buf, buf_size,
586 "name " " nb_channels" " depth" " is_alpha"
589 PixFmtInfo info= pix_fmt_info[pix_fmt];
591 char is_alpha_char= info.is_alpha ? 'y' : 'n';
593 snprintf (buf, buf_size,
603 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
605 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
608 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
611 for(i=0; i<256; i++){
625 case PIX_FMT_RGB4_BYTE:
630 case PIX_FMT_BGR4_BYTE:
641 pal[i] = b + (g<<8) + (r<<16);
647 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
650 const PixFmtInfo *pinfo;
652 memset(picture->linesize, 0, sizeof(picture->linesize));
654 pinfo = &pix_fmt_info[pix_fmt];
656 case PIX_FMT_YUV420P:
657 case PIX_FMT_YUV422P:
658 case PIX_FMT_YUV444P:
659 case PIX_FMT_YUV410P:
660 case PIX_FMT_YUV411P:
661 case PIX_FMT_YUV440P:
662 case PIX_FMT_YUVJ420P:
663 case PIX_FMT_YUVJ422P:
664 case PIX_FMT_YUVJ444P:
665 case PIX_FMT_YUVJ440P:
666 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
667 picture->linesize[0] = width;
668 picture->linesize[1] = w2;
669 picture->linesize[2] = w2;
671 case PIX_FMT_YUV420P16LE:
672 case PIX_FMT_YUV422P16LE:
673 case PIX_FMT_YUV444P16LE:
674 case PIX_FMT_YUV420P16BE:
675 case PIX_FMT_YUV422P16BE:
676 case PIX_FMT_YUV444P16BE:
677 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
678 picture->linesize[0] = 2*width;
679 picture->linesize[1] = 2*w2;
680 picture->linesize[2] = 2*w2;
682 case PIX_FMT_YUVA420P:
683 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
684 picture->linesize[0] = width;
685 picture->linesize[1] = w2;
686 picture->linesize[2] = w2;
687 picture->linesize[3] = width;
691 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
692 picture->linesize[0] = width;
693 picture->linesize[1] = 2 * w2;
697 picture->linesize[0] = width * 3;
703 picture->linesize[0] = width * 4;
705 case PIX_FMT_RGB48BE:
706 case PIX_FMT_RGB48LE:
707 picture->linesize[0] = width * 6;
709 case PIX_FMT_GRAY16BE:
710 case PIX_FMT_GRAY16LE:
711 case PIX_FMT_BGR555BE:
712 case PIX_FMT_BGR555LE:
713 case PIX_FMT_BGR565BE:
714 case PIX_FMT_BGR565LE:
715 case PIX_FMT_RGB555BE:
716 case PIX_FMT_RGB555LE:
717 case PIX_FMT_RGB565BE:
718 case PIX_FMT_RGB565LE:
719 case PIX_FMT_YUYV422:
720 picture->linesize[0] = width * 2;
722 case PIX_FMT_UYVY422:
723 picture->linesize[0] = width * 2;
725 case PIX_FMT_UYYVYY411:
726 picture->linesize[0] = width + width/2;
730 picture->linesize[0] = width / 2;
732 case PIX_FMT_MONOWHITE:
733 case PIX_FMT_MONOBLACK:
734 picture->linesize[0] = (width + 7) >> 3;
739 case PIX_FMT_RGB4_BYTE:
740 case PIX_FMT_BGR4_BYTE:
742 picture->linesize[0] = width;
750 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
754 const PixFmtInfo *pinfo;
756 pinfo = &pix_fmt_info[pix_fmt];
757 size = picture->linesize[0] * height;
759 case PIX_FMT_YUV420P:
760 case PIX_FMT_YUV422P:
761 case PIX_FMT_YUV444P:
762 case PIX_FMT_YUV410P:
763 case PIX_FMT_YUV411P:
764 case PIX_FMT_YUV440P:
765 case PIX_FMT_YUVJ420P:
766 case PIX_FMT_YUVJ422P:
767 case PIX_FMT_YUVJ444P:
768 case PIX_FMT_YUVJ440P:
769 case PIX_FMT_YUV420P16LE:
770 case PIX_FMT_YUV422P16LE:
771 case PIX_FMT_YUV444P16LE:
772 case PIX_FMT_YUV420P16BE:
773 case PIX_FMT_YUV422P16BE:
774 case PIX_FMT_YUV444P16BE:
775 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
776 size2 = picture->linesize[1] * h2;
777 picture->data[0] = ptr;
778 picture->data[1] = picture->data[0] + size;
779 picture->data[2] = picture->data[1] + size2;
780 picture->data[3] = NULL;
781 return size + 2 * size2;
782 case PIX_FMT_YUVA420P:
783 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
784 size2 = picture->linesize[1] * h2;
785 picture->data[0] = ptr;
786 picture->data[1] = picture->data[0] + size;
787 picture->data[2] = picture->data[1] + size2;
788 picture->data[3] = picture->data[1] + size2 + size2;
789 return 2 * size + 2 * size2;
792 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
793 size2 = picture->linesize[1] * h2;
794 picture->data[0] = ptr;
795 picture->data[1] = picture->data[0] + size;
796 picture->data[2] = NULL;
797 picture->data[3] = NULL;
805 case PIX_FMT_RGB48BE:
806 case PIX_FMT_RGB48LE:
807 case PIX_FMT_GRAY16BE:
808 case PIX_FMT_GRAY16LE:
809 case PIX_FMT_BGR555BE:
810 case PIX_FMT_BGR555LE:
811 case PIX_FMT_BGR565BE:
812 case PIX_FMT_BGR565LE:
813 case PIX_FMT_RGB555BE:
814 case PIX_FMT_RGB555LE:
815 case PIX_FMT_RGB565BE:
816 case PIX_FMT_RGB565LE:
817 case PIX_FMT_YUYV422:
818 case PIX_FMT_UYVY422:
819 case PIX_FMT_UYYVYY411:
822 case PIX_FMT_MONOWHITE:
823 case PIX_FMT_MONOBLACK:
824 picture->data[0] = ptr;
825 picture->data[1] = NULL;
826 picture->data[2] = NULL;
827 picture->data[3] = NULL;
832 case PIX_FMT_RGB4_BYTE:
833 case PIX_FMT_BGR4_BYTE:
835 size2 = (size + 3) & ~3;
836 picture->data[0] = ptr;
837 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
838 picture->data[2] = NULL;
839 picture->data[3] = NULL;
840 return size2 + 256 * 4;
842 picture->data[0] = NULL;
843 picture->data[1] = NULL;
844 picture->data[2] = NULL;
845 picture->data[3] = NULL;
850 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
851 enum PixelFormat pix_fmt, int width, int height)
854 if(avcodec_check_dimensions(NULL, width, height))
857 if (ff_fill_linesize(picture, pix_fmt, width))
860 return ff_fill_pointer(picture, ptr, pix_fmt, height);
863 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
864 unsigned char *dest, int dest_size)
866 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
867 int i, j, w, ow, h, oh, data_planes;
868 const unsigned char* s;
869 int size = avpicture_get_size(pix_fmt, width, height);
871 if (size > dest_size || size < 0)
874 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
875 if (pix_fmt == PIX_FMT_YUYV422 ||
876 pix_fmt == PIX_FMT_UYVY422 ||
877 pix_fmt == PIX_FMT_BGR565BE ||
878 pix_fmt == PIX_FMT_BGR565LE ||
879 pix_fmt == PIX_FMT_BGR555BE ||
880 pix_fmt == PIX_FMT_BGR555LE ||
881 pix_fmt == PIX_FMT_RGB565BE ||
882 pix_fmt == PIX_FMT_RGB565LE ||
883 pix_fmt == PIX_FMT_RGB555BE ||
884 pix_fmt == PIX_FMT_RGB555LE)
886 else if (pix_fmt == PIX_FMT_UYYVYY411)
888 else if (pix_fmt == PIX_FMT_PAL8)
891 w = width * (pf->depth * pf->nb_channels / 8);
896 data_planes = pf->nb_channels;
897 w = (width*pf->depth + 7)/8;
904 for (i=0; i<data_planes; i++) {
906 w = ((width >> pf->x_chroma_shift) * pf->depth + 7) / 8;
907 h = height >> pf->y_chroma_shift;
908 if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
918 s += src->linesize[i];
922 if (pf->pixel_type == FF_PIXEL_PALETTE)
923 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
928 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
930 AVPicture dummy_pict;
931 if(avcodec_check_dimensions(NULL, width, height))
936 case PIX_FMT_RGB4_BYTE:
937 case PIX_FMT_BGR4_BYTE:
939 // do not include palette for these pseudo-paletted formats
940 return width * height;
942 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
945 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
948 const PixFmtInfo *pf, *ps;
951 ps = &pix_fmt_info[src_pix_fmt];
955 pf = &pix_fmt_info[dst_pix_fmt];
956 if (pf->depth < ps->depth ||
957 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE) &&
958 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE)))
959 loss |= FF_LOSS_DEPTH;
960 if (pf->x_chroma_shift > ps->x_chroma_shift ||
961 pf->y_chroma_shift > ps->y_chroma_shift)
962 loss |= FF_LOSS_RESOLUTION;
963 switch(pf->color_type) {
965 if (ps->color_type != FF_COLOR_RGB &&
966 ps->color_type != FF_COLOR_GRAY)
967 loss |= FF_LOSS_COLORSPACE;
970 if (ps->color_type != FF_COLOR_GRAY)
971 loss |= FF_LOSS_COLORSPACE;
974 if (ps->color_type != FF_COLOR_YUV)
975 loss |= FF_LOSS_COLORSPACE;
977 case FF_COLOR_YUV_JPEG:
978 if (ps->color_type != FF_COLOR_YUV_JPEG &&
979 ps->color_type != FF_COLOR_YUV &&
980 ps->color_type != FF_COLOR_GRAY)
981 loss |= FF_LOSS_COLORSPACE;
985 if (ps->color_type != pf->color_type)
986 loss |= FF_LOSS_COLORSPACE;
989 if (pf->color_type == FF_COLOR_GRAY &&
990 ps->color_type != FF_COLOR_GRAY)
991 loss |= FF_LOSS_CHROMA;
992 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
993 loss |= FF_LOSS_ALPHA;
994 if (pf->pixel_type == FF_PIXEL_PALETTE &&
995 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
996 loss |= FF_LOSS_COLORQUANT;
1000 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
1003 const PixFmtInfo *pf;
1005 pf = &pix_fmt_info[pix_fmt];
1006 switch(pf->pixel_type) {
1007 case FF_PIXEL_PACKED:
1009 case PIX_FMT_YUYV422:
1010 case PIX_FMT_UYVY422:
1011 case PIX_FMT_RGB565BE:
1012 case PIX_FMT_RGB565LE:
1013 case PIX_FMT_RGB555BE:
1014 case PIX_FMT_RGB555LE:
1015 case PIX_FMT_BGR565BE:
1016 case PIX_FMT_BGR565LE:
1017 case PIX_FMT_BGR555BE:
1018 case PIX_FMT_BGR555LE:
1021 case PIX_FMT_UYYVYY411:
1025 bits = pf->depth * pf->nb_channels;
1029 case FF_PIXEL_PLANAR:
1030 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
1031 bits = pf->depth * pf->nb_channels;
1033 bits = pf->depth + ((2 * pf->depth) >>
1034 (pf->x_chroma_shift + pf->y_chroma_shift));
1037 case FF_PIXEL_PALETTE:
1047 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
1048 enum PixelFormat src_pix_fmt,
1052 int dist, i, loss, min_dist;
1053 enum PixelFormat dst_pix_fmt;
1055 /* find exact color match with smallest size */
1057 min_dist = 0x7fffffff;
1058 for(i = 0;i < PIX_FMT_NB; i++) {
1059 if (pix_fmt_mask & (1ULL << i)) {
1060 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
1062 dist = avg_bits_per_pixel(i);
1063 if (dist < min_dist) {
1073 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
1074 int has_alpha, int *loss_ptr)
1076 enum PixelFormat dst_pix_fmt;
1078 static const int loss_mask_order[] = {
1079 ~0, /* no loss first */
1081 ~FF_LOSS_RESOLUTION,
1082 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
1083 ~FF_LOSS_COLORQUANT,
1088 /* try with successive loss */
1091 loss_mask = loss_mask_order[i++];
1092 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
1093 has_alpha, loss_mask);
1094 if (dst_pix_fmt >= 0)
1102 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
1106 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
1107 const uint8_t *src, int src_wrap,
1108 int width, int height)
1110 if((!dst) || (!src))
1112 for(;height > 0; height--) {
1113 memcpy(dst, src, width);
1119 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
1122 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1124 pf = &pix_fmt_info[pix_fmt];
1125 switch(pf->pixel_type) {
1126 case FF_PIXEL_PACKED:
1128 case PIX_FMT_YUYV422:
1129 case PIX_FMT_UYVY422:
1130 case PIX_FMT_RGB565BE:
1131 case PIX_FMT_RGB565LE:
1132 case PIX_FMT_RGB555BE:
1133 case PIX_FMT_RGB555LE:
1134 case PIX_FMT_BGR565BE:
1135 case PIX_FMT_BGR565LE:
1136 case PIX_FMT_BGR555BE:
1137 case PIX_FMT_BGR555LE:
1140 case PIX_FMT_UYYVYY411:
1144 bits = pf->depth * pf->nb_channels;
1147 return (width * bits + 7) >> 3;
1149 case FF_PIXEL_PLANAR:
1150 if (plane == 1 || plane == 2)
1151 width= -((-width)>>pf->x_chroma_shift);
1153 return (width * pf->depth + 7) >> 3;
1155 case FF_PIXEL_PALETTE:
1164 void av_picture_copy(AVPicture *dst, const AVPicture *src,
1165 enum PixelFormat pix_fmt, int width, int height)
1168 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1170 switch(pf->pixel_type) {
1171 case FF_PIXEL_PACKED:
1172 case FF_PIXEL_PLANAR:
1173 for(i = 0; i < pf->nb_channels; i++) {
1175 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
1177 if (i == 1 || i == 2) {
1178 h= -((-height)>>pf->y_chroma_shift);
1180 ff_img_copy_plane(dst->data[i], dst->linesize[i],
1181 src->data[i], src->linesize[i],
1185 case FF_PIXEL_PALETTE:
1186 ff_img_copy_plane(dst->data[0], dst->linesize[0],
1187 src->data[0], src->linesize[0],
1189 /* copy the palette */
1190 memcpy(dst->data[1], src->data[1], 4*256);
1196 void ff_shrink22(uint8_t *dst, int dst_wrap,
1197 const uint8_t *src, int src_wrap,
1198 int width, int height)
1201 const uint8_t *s1, *s2;
1204 for(;height > 0; height--) {
1208 for(w = width;w >= 4; w-=4) {
1209 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1210 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1211 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1212 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1218 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1223 src += 2 * src_wrap;
1229 void ff_shrink44(uint8_t *dst, int dst_wrap,
1230 const uint8_t *src, int src_wrap,
1231 int width, int height)
1234 const uint8_t *s1, *s2, *s3, *s4;
1237 for(;height > 0; height--) {
1243 for(w = width;w > 0; w--) {
1244 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1245 s2[0] + s2[1] + s2[2] + s2[3] +
1246 s3[0] + s3[1] + s3[2] + s3[3] +
1247 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1254 src += 4 * src_wrap;
1260 void ff_shrink88(uint8_t *dst, int dst_wrap,
1261 const uint8_t *src, int src_wrap,
1262 int width, int height)
1266 for(;height > 0; height--) {
1267 for(w = width;w > 0; w--) {
1270 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1273 *(dst++) = (tmp + 32)>>6;
1274 src += 8 - 8*src_wrap;
1276 src += 8*src_wrap - 8*width;
1277 dst += dst_wrap - width;
1282 int avpicture_alloc(AVPicture *picture,
1283 enum PixelFormat pix_fmt, int width, int height)
1288 size = avpicture_fill(picture, NULL, pix_fmt, width, height);
1291 ptr = av_malloc(size);
1294 avpicture_fill(picture, ptr, pix_fmt, width, height);
1295 if(picture->data[1] && !picture->data[2])
1296 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
1300 memset(picture, 0, sizeof(AVPicture));
1304 void avpicture_free(AVPicture *picture)
1306 av_free(picture->data[0]);
1309 /* return true if yuv planar */
1310 static inline int is_yuv_planar(const PixFmtInfo *ps)
1312 return (ps->color_type == FF_COLOR_YUV ||
1313 ps->color_type == FF_COLOR_YUV_JPEG) &&
1314 ps->pixel_type == FF_PIXEL_PLANAR;
1317 int av_picture_crop(AVPicture *dst, const AVPicture *src,
1318 enum PixelFormat pix_fmt, int top_band, int left_band)
1323 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1326 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
1327 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
1329 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1330 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1331 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1333 dst->linesize[0] = src->linesize[0];
1334 dst->linesize[1] = src->linesize[1];
1335 dst->linesize[2] = src->linesize[2];
1339 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
1340 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
1349 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1350 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
1352 for (i = 0; i < 3; i++) {
1353 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
1354 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
1356 if (padtop || padleft) {
1357 memset(dst->data[i], color[i],
1358 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
1361 if (padleft || padright) {
1362 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1363 (dst->linesize[i] - (padright >> x_shift));
1364 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1365 for (y = 0; y < yheight; y++) {
1366 memset(optr, color[i], (padleft + padright) >> x_shift);
1367 optr += dst->linesize[i];
1371 if (src) { /* first line */
1372 uint8_t *iptr = src->data[i];
1373 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1374 (padleft >> x_shift);
1375 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
1376 iptr += src->linesize[i];
1377 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1378 (dst->linesize[i] - (padright >> x_shift));
1379 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1380 for (y = 0; y < yheight; y++) {
1381 memset(optr, color[i], (padleft + padright) >> x_shift);
1382 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1383 (width - padleft - padright) >> x_shift);
1384 iptr += src->linesize[i];
1385 optr += dst->linesize[i];
1389 if (padbottom || padright) {
1390 optr = dst->data[i] + dst->linesize[i] *
1391 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1392 memset(optr, color[i],dst->linesize[i] *
1393 (padbottom >> y_shift) + (padright >> x_shift));
1399 /* NOTE: we scan all the pixels to have an exact information */
1400 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1402 const unsigned char *p;
1403 int src_wrap, ret, x, y;
1405 uint32_t *palette = (uint32_t *)src->data[1];
1408 src_wrap = src->linesize[0] - width;
1410 for(y=0;y<height;y++) {
1411 for(x=0;x<width;x++) {
1412 a = palette[p[0]] >> 24;
1414 ret |= FF_ALPHA_TRANSP;
1415 } else if (a != 0xff) {
1416 ret |= FF_ALPHA_SEMI_TRANSP;
1425 int img_get_alpha_info(const AVPicture *src,
1426 enum PixelFormat pix_fmt, int width, int height)
1428 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1431 /* no alpha can be represented in format */
1436 ret = get_alpha_info_pal8(src, width, height);
1439 /* we do not know, so everything is indicated */
1440 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1447 #define DEINT_INPLACE_LINE_LUM \
1448 movd_m2r(lum_m4[0],mm0);\
1449 movd_m2r(lum_m3[0],mm1);\
1450 movd_m2r(lum_m2[0],mm2);\
1451 movd_m2r(lum_m1[0],mm3);\
1452 movd_m2r(lum[0],mm4);\
1453 punpcklbw_r2r(mm7,mm0);\
1454 movd_r2m(mm2,lum_m4[0]);\
1455 punpcklbw_r2r(mm7,mm1);\
1456 punpcklbw_r2r(mm7,mm2);\
1457 punpcklbw_r2r(mm7,mm3);\
1458 punpcklbw_r2r(mm7,mm4);\
1459 paddw_r2r(mm3,mm1);\
1461 paddw_r2r(mm4,mm0);\
1463 paddw_r2r(mm6,mm2);\
1464 paddw_r2r(mm2,mm1);\
1465 psubusw_r2r(mm0,mm1);\
1467 packuswb_r2r(mm7,mm1);\
1468 movd_r2m(mm1,lum_m2[0]);
1470 #define DEINT_LINE_LUM \
1471 movd_m2r(lum_m4[0],mm0);\
1472 movd_m2r(lum_m3[0],mm1);\
1473 movd_m2r(lum_m2[0],mm2);\
1474 movd_m2r(lum_m1[0],mm3);\
1475 movd_m2r(lum[0],mm4);\
1476 punpcklbw_r2r(mm7,mm0);\
1477 punpcklbw_r2r(mm7,mm1);\
1478 punpcklbw_r2r(mm7,mm2);\
1479 punpcklbw_r2r(mm7,mm3);\
1480 punpcklbw_r2r(mm7,mm4);\
1481 paddw_r2r(mm3,mm1);\
1483 paddw_r2r(mm4,mm0);\
1485 paddw_r2r(mm6,mm2);\
1486 paddw_r2r(mm2,mm1);\
1487 psubusw_r2r(mm0,mm1);\
1489 packuswb_r2r(mm7,mm1);\
1490 movd_r2m(mm1,dst[0]);
1493 /* filter parameters: [-1 4 2 4 -1] // 8 */
1494 static void deinterlace_line(uint8_t *dst,
1495 const uint8_t *lum_m4, const uint8_t *lum_m3,
1496 const uint8_t *lum_m2, const uint8_t *lum_m1,
1501 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1504 for(;size > 0;size--) {
1506 sum += lum_m3[0] << 2;
1507 sum += lum_m2[0] << 1;
1508 sum += lum_m1[0] << 2;
1510 dst[0] = cm[(sum + 4) >> 3];
1522 movq_m2r(ff_pw_4,mm6);
1524 for (;size > 3; size-=4) {
1535 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1539 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1542 for(;size > 0;size--) {
1544 sum += lum_m3[0] << 2;
1545 sum += lum_m2[0] << 1;
1546 lum_m4[0]=lum_m2[0];
1547 sum += lum_m1[0] << 2;
1549 lum_m2[0] = cm[(sum + 4) >> 3];
1560 movq_m2r(ff_pw_4,mm6);
1562 for (;size > 3; size-=4) {
1563 DEINT_INPLACE_LINE_LUM
1573 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1574 top field is copied as is, but the bottom field is deinterlaced
1575 against the top field. */
1576 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1577 const uint8_t *src1, int src_wrap,
1578 int width, int height)
1580 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1585 src_0=&src_m1[src_wrap];
1586 src_p1=&src_0[src_wrap];
1587 src_p2=&src_p1[src_wrap];
1588 for(y=0;y<(height-2);y+=2) {
1589 memcpy(dst,src_m1,width);
1591 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1595 src_p1 += 2*src_wrap;
1596 src_p2 += 2*src_wrap;
1599 memcpy(dst,src_m1,width);
1602 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1605 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1606 int width, int height)
1608 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1611 buf = (uint8_t*)av_malloc(width);
1614 memcpy(buf,src_m1,width);
1615 src_0=&src_m1[src_wrap];
1616 src_p1=&src_0[src_wrap];
1617 src_p2=&src_p1[src_wrap];
1618 for(y=0;y<(height-2);y+=2) {
1619 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1622 src_p1 += 2*src_wrap;
1623 src_p2 += 2*src_wrap;
1626 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1630 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1631 enum PixelFormat pix_fmt, int width, int height)
1635 if (pix_fmt != PIX_FMT_YUV420P &&
1636 pix_fmt != PIX_FMT_YUV422P &&
1637 pix_fmt != PIX_FMT_YUV444P &&
1638 pix_fmt != PIX_FMT_YUV411P &&
1639 pix_fmt != PIX_FMT_GRAY8)
1641 if ((width & 3) != 0 || (height & 3) != 0)
1647 case PIX_FMT_YUV420P:
1651 case PIX_FMT_YUV422P:
1654 case PIX_FMT_YUV411P:
1660 if (pix_fmt == PIX_FMT_GRAY8) {
1665 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1668 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1669 src->data[i], src->linesize[i],