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 depth; /**< bit depth of the color components */
65 /* this table gives more information about formats */
66 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
71 .color_type = FF_COLOR_YUV,
72 .pixel_type = FF_PIXEL_PLANAR,
78 .color_type = FF_COLOR_YUV,
79 .pixel_type = FF_PIXEL_PLANAR,
85 .color_type = FF_COLOR_YUV,
86 .pixel_type = FF_PIXEL_PLANAR,
92 .color_type = FF_COLOR_YUV,
93 .pixel_type = FF_PIXEL_PACKED,
99 .color_type = FF_COLOR_YUV,
100 .pixel_type = FF_PIXEL_PACKED,
103 [PIX_FMT_YUV410P] = {
106 .color_type = FF_COLOR_YUV,
107 .pixel_type = FF_PIXEL_PLANAR,
110 [PIX_FMT_YUV411P] = {
113 .color_type = FF_COLOR_YUV,
114 .pixel_type = FF_PIXEL_PLANAR,
117 [PIX_FMT_YUV440P] = {
120 .color_type = FF_COLOR_YUV,
121 .pixel_type = FF_PIXEL_PLANAR,
124 [PIX_FMT_YUV420P16LE] = {
125 .name = "yuv420p16le",
127 .color_type = FF_COLOR_YUV,
128 .pixel_type = FF_PIXEL_PLANAR,
131 [PIX_FMT_YUV422P16LE] = {
132 .name = "yuv422p16le",
134 .color_type = FF_COLOR_YUV,
135 .pixel_type = FF_PIXEL_PLANAR,
138 [PIX_FMT_YUV444P16LE] = {
139 .name = "yuv444p16le",
141 .color_type = FF_COLOR_YUV,
142 .pixel_type = FF_PIXEL_PLANAR,
145 [PIX_FMT_YUV420P16BE] = {
146 .name = "yuv420p16be",
148 .color_type = FF_COLOR_YUV,
149 .pixel_type = FF_PIXEL_PLANAR,
152 [PIX_FMT_YUV422P16BE] = {
153 .name = "yuv422p16be",
155 .color_type = FF_COLOR_YUV,
156 .pixel_type = FF_PIXEL_PLANAR,
159 [PIX_FMT_YUV444P16BE] = {
160 .name = "yuv444p16be",
162 .color_type = FF_COLOR_YUV,
163 .pixel_type = FF_PIXEL_PLANAR,
168 /* YUV formats with alpha plane */
169 [PIX_FMT_YUVA420P] = {
172 .color_type = FF_COLOR_YUV,
173 .pixel_type = FF_PIXEL_PLANAR,
178 [PIX_FMT_YUVJ420P] = {
181 .color_type = FF_COLOR_YUV_JPEG,
182 .pixel_type = FF_PIXEL_PLANAR,
185 [PIX_FMT_YUVJ422P] = {
188 .color_type = FF_COLOR_YUV_JPEG,
189 .pixel_type = FF_PIXEL_PLANAR,
192 [PIX_FMT_YUVJ444P] = {
195 .color_type = FF_COLOR_YUV_JPEG,
196 .pixel_type = FF_PIXEL_PLANAR,
199 [PIX_FMT_YUVJ440P] = {
202 .color_type = FF_COLOR_YUV_JPEG,
203 .pixel_type = FF_PIXEL_PLANAR,
211 .color_type = FF_COLOR_RGB,
212 .pixel_type = FF_PIXEL_PACKED,
218 .color_type = FF_COLOR_RGB,
219 .pixel_type = FF_PIXEL_PACKED,
224 .nb_channels = 4, .is_alpha = 1,
225 .color_type = FF_COLOR_RGB,
226 .pixel_type = FF_PIXEL_PACKED,
229 [PIX_FMT_RGB48BE] = {
232 .color_type = FF_COLOR_RGB,
233 .pixel_type = FF_PIXEL_PACKED,
236 [PIX_FMT_RGB48LE] = {
239 .color_type = FF_COLOR_RGB,
240 .pixel_type = FF_PIXEL_PACKED,
243 [PIX_FMT_RGB565BE] = {
246 .color_type = FF_COLOR_RGB,
247 .pixel_type = FF_PIXEL_PACKED,
250 [PIX_FMT_RGB565LE] = {
253 .color_type = FF_COLOR_RGB,
254 .pixel_type = FF_PIXEL_PACKED,
257 [PIX_FMT_RGB555BE] = {
260 .color_type = FF_COLOR_RGB,
261 .pixel_type = FF_PIXEL_PACKED,
264 [PIX_FMT_RGB555LE] = {
267 .color_type = FF_COLOR_RGB,
268 .pixel_type = FF_PIXEL_PACKED,
272 /* gray / mono formats */
273 [PIX_FMT_GRAY16BE] = {
276 .color_type = FF_COLOR_GRAY,
277 .pixel_type = FF_PIXEL_PLANAR,
280 [PIX_FMT_GRAY16LE] = {
283 .color_type = FF_COLOR_GRAY,
284 .pixel_type = FF_PIXEL_PLANAR,
290 .color_type = FF_COLOR_GRAY,
291 .pixel_type = FF_PIXEL_PLANAR,
294 [PIX_FMT_MONOWHITE] = {
297 .color_type = FF_COLOR_GRAY,
298 .pixel_type = FF_PIXEL_PLANAR,
301 [PIX_FMT_MONOBLACK] = {
304 .color_type = FF_COLOR_GRAY,
305 .pixel_type = FF_PIXEL_PLANAR,
309 /* paletted formats */
312 .nb_channels = 4, .is_alpha = 1,
313 .color_type = FF_COLOR_RGB,
314 .pixel_type = FF_PIXEL_PALETTE,
317 [PIX_FMT_XVMC_MPEG2_MC] = {
321 [PIX_FMT_XVMC_MPEG2_IDCT] = {
325 [PIX_FMT_VDPAU_MPEG1] = {
326 .name = "vdpau_mpeg1",
329 [PIX_FMT_VDPAU_MPEG2] = {
330 .name = "vdpau_mpeg2",
333 [PIX_FMT_VDPAU_H264] = {
334 .name = "vdpau_h264",
337 [PIX_FMT_VDPAU_WMV3] = {
338 .name = "vdpau_wmv3",
341 [PIX_FMT_VDPAU_VC1] = {
345 [PIX_FMT_VDPAU_MPEG4] = {
346 .name = "vdpau_mpeg4",
349 [PIX_FMT_UYYVYY411] = {
352 .color_type = FF_COLOR_YUV,
353 .pixel_type = FF_PIXEL_PACKED,
358 .nb_channels = 4, .is_alpha = 1,
359 .color_type = FF_COLOR_RGB,
360 .pixel_type = FF_PIXEL_PACKED,
363 [PIX_FMT_BGR565BE] = {
366 .color_type = FF_COLOR_RGB,
367 .pixel_type = FF_PIXEL_PACKED,
370 [PIX_FMT_BGR565LE] = {
373 .color_type = FF_COLOR_RGB,
374 .pixel_type = FF_PIXEL_PACKED,
377 [PIX_FMT_BGR555BE] = {
380 .color_type = FF_COLOR_RGB,
381 .pixel_type = FF_PIXEL_PACKED,
384 [PIX_FMT_BGR555LE] = {
387 .color_type = FF_COLOR_RGB,
388 .pixel_type = FF_PIXEL_PACKED,
394 .color_type = FF_COLOR_RGB,
395 .pixel_type = FF_PIXEL_PACKED,
401 .color_type = FF_COLOR_RGB,
402 .pixel_type = FF_PIXEL_PACKED,
405 [PIX_FMT_RGB4_BYTE] = {
408 .color_type = FF_COLOR_RGB,
409 .pixel_type = FF_PIXEL_PACKED,
415 .color_type = FF_COLOR_RGB,
416 .pixel_type = FF_PIXEL_PACKED,
422 .color_type = FF_COLOR_RGB,
423 .pixel_type = FF_PIXEL_PACKED,
426 [PIX_FMT_BGR4_BYTE] = {
429 .color_type = FF_COLOR_RGB,
430 .pixel_type = FF_PIXEL_PACKED,
436 .color_type = FF_COLOR_YUV,
437 .pixel_type = FF_PIXEL_PLANAR,
443 .color_type = FF_COLOR_YUV,
444 .pixel_type = FF_PIXEL_PLANAR,
450 .nb_channels = 4, .is_alpha = 1,
451 .color_type = FF_COLOR_RGB,
452 .pixel_type = FF_PIXEL_PACKED,
457 .nb_channels = 4, .is_alpha = 1,
458 .color_type = FF_COLOR_RGB,
459 .pixel_type = FF_PIXEL_PACKED,
464 [PIX_FMT_VAAPI_MOCO] = {
465 .name = "vaapi_moco",
468 [PIX_FMT_VAAPI_IDCT] = {
469 .name = "vaapi_idct",
472 [PIX_FMT_VAAPI_VLD] = {
478 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
480 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
481 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
484 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
486 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
489 return pix_fmt_info[pix_fmt].name;
492 static enum PixelFormat avcodec_get_pix_fmt_internal(const char *name)
496 for (i=0; i < PIX_FMT_NB; i++)
497 if (pix_fmt_info[i].name && !strcmp(pix_fmt_info[i].name, name))
503 # define X_NE(be, le) be
505 # define X_NE(be, le) le
508 enum PixelFormat avcodec_get_pix_fmt(const char *name)
510 enum PixelFormat pix_fmt;
512 if (!strcmp(name, "rgb32"))
513 name = X_NE("argb", "bgra");
514 else if (!strcmp(name, "bgr32"))
515 name = X_NE("abgr", "rgba");
517 pix_fmt = avcodec_get_pix_fmt_internal(name);
518 if (pix_fmt == PIX_FMT_NONE) {
520 snprintf(name2, sizeof(name2), "%s%s", name, X_NE("be", "le"));
521 pix_fmt = avcodec_get_pix_fmt_internal(name2);
526 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
530 snprintf (buf, buf_size,
531 "name " " nb_channels" " depth" " is_alpha"
534 PixFmtInfo info= pix_fmt_info[pix_fmt];
536 char is_alpha_char= info.is_alpha ? 'y' : 'n';
538 snprintf (buf, buf_size,
548 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
550 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
553 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
556 for(i=0; i<256; i++){
570 case PIX_FMT_RGB4_BYTE:
575 case PIX_FMT_BGR4_BYTE:
586 pal[i] = b + (g<<8) + (r<<16);
592 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
595 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
597 memset(picture->linesize, 0, sizeof(picture->linesize));
600 case PIX_FMT_YUV420P:
601 case PIX_FMT_YUV422P:
602 case PIX_FMT_YUV444P:
603 case PIX_FMT_YUV410P:
604 case PIX_FMT_YUV411P:
605 case PIX_FMT_YUV440P:
606 case PIX_FMT_YUVJ420P:
607 case PIX_FMT_YUVJ422P:
608 case PIX_FMT_YUVJ444P:
609 case PIX_FMT_YUVJ440P:
610 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
611 picture->linesize[0] = width;
612 picture->linesize[1] = w2;
613 picture->linesize[2] = w2;
615 case PIX_FMT_YUV420P16LE:
616 case PIX_FMT_YUV422P16LE:
617 case PIX_FMT_YUV444P16LE:
618 case PIX_FMT_YUV420P16BE:
619 case PIX_FMT_YUV422P16BE:
620 case PIX_FMT_YUV444P16BE:
621 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
622 picture->linesize[0] = 2*width;
623 picture->linesize[1] = 2*w2;
624 picture->linesize[2] = 2*w2;
626 case PIX_FMT_YUVA420P:
627 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
628 picture->linesize[0] = width;
629 picture->linesize[1] = w2;
630 picture->linesize[2] = w2;
631 picture->linesize[3] = width;
635 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
636 picture->linesize[0] = width;
637 picture->linesize[1] = 2 * w2;
641 picture->linesize[0] = width * 3;
647 picture->linesize[0] = width * 4;
649 case PIX_FMT_RGB48BE:
650 case PIX_FMT_RGB48LE:
651 picture->linesize[0] = width * 6;
653 case PIX_FMT_GRAY16BE:
654 case PIX_FMT_GRAY16LE:
655 case PIX_FMT_BGR555BE:
656 case PIX_FMT_BGR555LE:
657 case PIX_FMT_BGR565BE:
658 case PIX_FMT_BGR565LE:
659 case PIX_FMT_RGB555BE:
660 case PIX_FMT_RGB555LE:
661 case PIX_FMT_RGB565BE:
662 case PIX_FMT_RGB565LE:
663 case PIX_FMT_YUYV422:
664 picture->linesize[0] = width * 2;
666 case PIX_FMT_UYVY422:
667 picture->linesize[0] = width * 2;
669 case PIX_FMT_UYYVYY411:
670 picture->linesize[0] = width + width/2;
674 picture->linesize[0] = width / 2;
676 case PIX_FMT_MONOWHITE:
677 case PIX_FMT_MONOBLACK:
678 picture->linesize[0] = (width + 7) >> 3;
683 case PIX_FMT_RGB4_BYTE:
684 case PIX_FMT_BGR4_BYTE:
686 picture->linesize[0] = width;
694 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
698 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
700 size = picture->linesize[0] * height;
702 case PIX_FMT_YUV420P:
703 case PIX_FMT_YUV422P:
704 case PIX_FMT_YUV444P:
705 case PIX_FMT_YUV410P:
706 case PIX_FMT_YUV411P:
707 case PIX_FMT_YUV440P:
708 case PIX_FMT_YUVJ420P:
709 case PIX_FMT_YUVJ422P:
710 case PIX_FMT_YUVJ444P:
711 case PIX_FMT_YUVJ440P:
712 case PIX_FMT_YUV420P16LE:
713 case PIX_FMT_YUV422P16LE:
714 case PIX_FMT_YUV444P16LE:
715 case PIX_FMT_YUV420P16BE:
716 case PIX_FMT_YUV422P16BE:
717 case PIX_FMT_YUV444P16BE:
718 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
719 size2 = picture->linesize[1] * h2;
720 picture->data[0] = ptr;
721 picture->data[1] = picture->data[0] + size;
722 picture->data[2] = picture->data[1] + size2;
723 picture->data[3] = NULL;
724 return size + 2 * size2;
725 case PIX_FMT_YUVA420P:
726 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
727 size2 = picture->linesize[1] * h2;
728 picture->data[0] = ptr;
729 picture->data[1] = picture->data[0] + size;
730 picture->data[2] = picture->data[1] + size2;
731 picture->data[3] = picture->data[1] + size2 + size2;
732 return 2 * size + 2 * size2;
735 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
736 size2 = picture->linesize[1] * h2;
737 picture->data[0] = ptr;
738 picture->data[1] = picture->data[0] + size;
739 picture->data[2] = NULL;
740 picture->data[3] = NULL;
748 case PIX_FMT_RGB48BE:
749 case PIX_FMT_RGB48LE:
750 case PIX_FMT_GRAY16BE:
751 case PIX_FMT_GRAY16LE:
752 case PIX_FMT_BGR555BE:
753 case PIX_FMT_BGR555LE:
754 case PIX_FMT_BGR565BE:
755 case PIX_FMT_BGR565LE:
756 case PIX_FMT_RGB555BE:
757 case PIX_FMT_RGB555LE:
758 case PIX_FMT_RGB565BE:
759 case PIX_FMT_RGB565LE:
760 case PIX_FMT_YUYV422:
761 case PIX_FMT_UYVY422:
762 case PIX_FMT_UYYVYY411:
765 case PIX_FMT_MONOWHITE:
766 case PIX_FMT_MONOBLACK:
767 picture->data[0] = ptr;
768 picture->data[1] = NULL;
769 picture->data[2] = NULL;
770 picture->data[3] = NULL;
775 case PIX_FMT_RGB4_BYTE:
776 case PIX_FMT_BGR4_BYTE:
778 size2 = (size + 3) & ~3;
779 picture->data[0] = ptr;
780 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
781 picture->data[2] = NULL;
782 picture->data[3] = NULL;
783 return size2 + 256 * 4;
785 picture->data[0] = NULL;
786 picture->data[1] = NULL;
787 picture->data[2] = NULL;
788 picture->data[3] = NULL;
793 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
794 enum PixelFormat pix_fmt, int width, int height)
797 if(avcodec_check_dimensions(NULL, width, height))
800 if (ff_fill_linesize(picture, pix_fmt, width))
803 return ff_fill_pointer(picture, ptr, pix_fmt, height);
806 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
807 unsigned char *dest, int dest_size)
809 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
810 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
811 int i, j, w, ow, h, oh, data_planes;
812 const unsigned char* s;
813 int size = avpicture_get_size(pix_fmt, width, height);
815 if (size > dest_size || size < 0)
818 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
819 if (pix_fmt == PIX_FMT_YUYV422 ||
820 pix_fmt == PIX_FMT_UYVY422 ||
821 pix_fmt == PIX_FMT_BGR565BE ||
822 pix_fmt == PIX_FMT_BGR565LE ||
823 pix_fmt == PIX_FMT_BGR555BE ||
824 pix_fmt == PIX_FMT_BGR555LE ||
825 pix_fmt == PIX_FMT_RGB565BE ||
826 pix_fmt == PIX_FMT_RGB565LE ||
827 pix_fmt == PIX_FMT_RGB555BE ||
828 pix_fmt == PIX_FMT_RGB555LE)
830 else if (pix_fmt == PIX_FMT_UYYVYY411)
832 else if (pix_fmt == PIX_FMT_PAL8)
835 w = width * (pf->depth * pf->nb_channels / 8);
840 data_planes = pf->nb_channels;
841 w = (width*pf->depth + 7)/8;
848 for (i=0; i<data_planes; i++) {
850 w = ((width >> desc->log2_chroma_w) * pf->depth + 7) / 8;
851 h = height >> desc->log2_chroma_h;
852 if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
862 s += src->linesize[i];
866 if (pf->pixel_type == FF_PIXEL_PALETTE)
867 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
872 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
874 AVPicture dummy_pict;
875 if(avcodec_check_dimensions(NULL, width, height))
880 case PIX_FMT_RGB4_BYTE:
881 case PIX_FMT_BGR4_BYTE:
883 // do not include palette for these pseudo-paletted formats
884 return width * height;
886 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
889 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
892 const PixFmtInfo *pf, *ps;
893 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
894 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
897 ps = &pix_fmt_info[src_pix_fmt];
901 pf = &pix_fmt_info[dst_pix_fmt];
902 if (pf->depth < ps->depth ||
903 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE) &&
904 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE)))
905 loss |= FF_LOSS_DEPTH;
906 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
907 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
908 loss |= FF_LOSS_RESOLUTION;
909 switch(pf->color_type) {
911 if (ps->color_type != FF_COLOR_RGB &&
912 ps->color_type != FF_COLOR_GRAY)
913 loss |= FF_LOSS_COLORSPACE;
916 if (ps->color_type != FF_COLOR_GRAY)
917 loss |= FF_LOSS_COLORSPACE;
920 if (ps->color_type != FF_COLOR_YUV)
921 loss |= FF_LOSS_COLORSPACE;
923 case FF_COLOR_YUV_JPEG:
924 if (ps->color_type != FF_COLOR_YUV_JPEG &&
925 ps->color_type != FF_COLOR_YUV &&
926 ps->color_type != FF_COLOR_GRAY)
927 loss |= FF_LOSS_COLORSPACE;
931 if (ps->color_type != pf->color_type)
932 loss |= FF_LOSS_COLORSPACE;
935 if (pf->color_type == FF_COLOR_GRAY &&
936 ps->color_type != FF_COLOR_GRAY)
937 loss |= FF_LOSS_CHROMA;
938 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
939 loss |= FF_LOSS_ALPHA;
940 if (pf->pixel_type == FF_PIXEL_PALETTE &&
941 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
942 loss |= FF_LOSS_COLORQUANT;
946 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
949 const PixFmtInfo *pf;
950 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
952 pf = &pix_fmt_info[pix_fmt];
953 switch(pf->pixel_type) {
954 case FF_PIXEL_PACKED:
956 case PIX_FMT_YUYV422:
957 case PIX_FMT_UYVY422:
958 case PIX_FMT_RGB565BE:
959 case PIX_FMT_RGB565LE:
960 case PIX_FMT_RGB555BE:
961 case PIX_FMT_RGB555LE:
962 case PIX_FMT_BGR565BE:
963 case PIX_FMT_BGR565LE:
964 case PIX_FMT_BGR555BE:
965 case PIX_FMT_BGR555LE:
968 case PIX_FMT_UYYVYY411:
972 bits = pf->depth * pf->nb_channels;
976 case FF_PIXEL_PLANAR:
977 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
978 bits = pf->depth * pf->nb_channels;
980 bits = pf->depth + ((2 * pf->depth) >>
981 (desc->log2_chroma_w + desc->log2_chroma_h));
984 case FF_PIXEL_PALETTE:
994 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
995 enum PixelFormat src_pix_fmt,
999 int dist, i, loss, min_dist;
1000 enum PixelFormat dst_pix_fmt;
1002 /* find exact color match with smallest size */
1004 min_dist = 0x7fffffff;
1005 for(i = 0;i < PIX_FMT_NB; i++) {
1006 if (pix_fmt_mask & (1ULL << i)) {
1007 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
1009 dist = avg_bits_per_pixel(i);
1010 if (dist < min_dist) {
1020 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
1021 int has_alpha, int *loss_ptr)
1023 enum PixelFormat dst_pix_fmt;
1025 static const int loss_mask_order[] = {
1026 ~0, /* no loss first */
1028 ~FF_LOSS_RESOLUTION,
1029 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
1030 ~FF_LOSS_COLORQUANT,
1035 /* try with successive loss */
1038 loss_mask = loss_mask_order[i++];
1039 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
1040 has_alpha, loss_mask);
1041 if (dst_pix_fmt >= 0)
1049 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
1053 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
1054 const uint8_t *src, int src_wrap,
1055 int width, int height)
1057 if((!dst) || (!src))
1059 for(;height > 0; height--) {
1060 memcpy(dst, src, width);
1066 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
1069 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1070 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1072 pf = &pix_fmt_info[pix_fmt];
1073 switch(pf->pixel_type) {
1074 case FF_PIXEL_PACKED:
1076 case PIX_FMT_YUYV422:
1077 case PIX_FMT_UYVY422:
1078 case PIX_FMT_RGB565BE:
1079 case PIX_FMT_RGB565LE:
1080 case PIX_FMT_RGB555BE:
1081 case PIX_FMT_RGB555LE:
1082 case PIX_FMT_BGR565BE:
1083 case PIX_FMT_BGR565LE:
1084 case PIX_FMT_BGR555BE:
1085 case PIX_FMT_BGR555LE:
1088 case PIX_FMT_UYYVYY411:
1092 bits = pf->depth * pf->nb_channels;
1095 return (width * bits + 7) >> 3;
1097 case FF_PIXEL_PLANAR:
1098 if (plane == 1 || plane == 2)
1099 width= -((-width)>>desc->log2_chroma_w);
1101 return (width * pf->depth + 7) >> 3;
1103 case FF_PIXEL_PALETTE:
1112 void av_picture_copy(AVPicture *dst, const AVPicture *src,
1113 enum PixelFormat pix_fmt, int width, int height)
1116 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1117 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1119 switch(pf->pixel_type) {
1120 case FF_PIXEL_PACKED:
1121 case FF_PIXEL_PLANAR:
1122 for(i = 0; i < pf->nb_channels; i++) {
1124 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
1126 if (i == 1 || i == 2) {
1127 h= -((-height)>>desc->log2_chroma_h);
1129 ff_img_copy_plane(dst->data[i], dst->linesize[i],
1130 src->data[i], src->linesize[i],
1134 case FF_PIXEL_PALETTE:
1135 ff_img_copy_plane(dst->data[0], dst->linesize[0],
1136 src->data[0], src->linesize[0],
1138 /* copy the palette */
1139 memcpy(dst->data[1], src->data[1], 4*256);
1145 void ff_shrink22(uint8_t *dst, int dst_wrap,
1146 const uint8_t *src, int src_wrap,
1147 int width, int height)
1150 const uint8_t *s1, *s2;
1153 for(;height > 0; height--) {
1157 for(w = width;w >= 4; w-=4) {
1158 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1159 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1160 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1161 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1167 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1172 src += 2 * src_wrap;
1178 void ff_shrink44(uint8_t *dst, int dst_wrap,
1179 const uint8_t *src, int src_wrap,
1180 int width, int height)
1183 const uint8_t *s1, *s2, *s3, *s4;
1186 for(;height > 0; height--) {
1192 for(w = width;w > 0; w--) {
1193 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1194 s2[0] + s2[1] + s2[2] + s2[3] +
1195 s3[0] + s3[1] + s3[2] + s3[3] +
1196 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1203 src += 4 * src_wrap;
1209 void ff_shrink88(uint8_t *dst, int dst_wrap,
1210 const uint8_t *src, int src_wrap,
1211 int width, int height)
1215 for(;height > 0; height--) {
1216 for(w = width;w > 0; w--) {
1219 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1222 *(dst++) = (tmp + 32)>>6;
1223 src += 8 - 8*src_wrap;
1225 src += 8*src_wrap - 8*width;
1226 dst += dst_wrap - width;
1231 int avpicture_alloc(AVPicture *picture,
1232 enum PixelFormat pix_fmt, int width, int height)
1237 size = avpicture_fill(picture, NULL, pix_fmt, width, height);
1240 ptr = av_malloc(size);
1243 avpicture_fill(picture, ptr, pix_fmt, width, height);
1244 if(picture->data[1] && !picture->data[2])
1245 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
1249 memset(picture, 0, sizeof(AVPicture));
1253 void avpicture_free(AVPicture *picture)
1255 av_free(picture->data[0]);
1258 /* return true if yuv planar */
1259 static inline int is_yuv_planar(const PixFmtInfo *ps)
1261 return (ps->color_type == FF_COLOR_YUV ||
1262 ps->color_type == FF_COLOR_YUV_JPEG) &&
1263 ps->pixel_type == FF_PIXEL_PLANAR;
1266 int av_picture_crop(AVPicture *dst, const AVPicture *src,
1267 enum PixelFormat pix_fmt, int top_band, int left_band)
1272 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1275 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
1276 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
1278 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1279 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1280 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1282 dst->linesize[0] = src->linesize[0];
1283 dst->linesize[1] = src->linesize[1];
1284 dst->linesize[2] = src->linesize[2];
1288 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
1289 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
1298 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1299 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
1301 for (i = 0; i < 3; i++) {
1302 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
1303 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
1305 if (padtop || padleft) {
1306 memset(dst->data[i], color[i],
1307 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
1310 if (padleft || padright) {
1311 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1312 (dst->linesize[i] - (padright >> x_shift));
1313 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1314 for (y = 0; y < yheight; y++) {
1315 memset(optr, color[i], (padleft + padright) >> x_shift);
1316 optr += dst->linesize[i];
1320 if (src) { /* first line */
1321 uint8_t *iptr = src->data[i];
1322 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1323 (padleft >> x_shift);
1324 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
1325 iptr += src->linesize[i];
1326 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1327 (dst->linesize[i] - (padright >> x_shift));
1328 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1329 for (y = 0; y < yheight; y++) {
1330 memset(optr, color[i], (padleft + padright) >> x_shift);
1331 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1332 (width - padleft - padright) >> x_shift);
1333 iptr += src->linesize[i];
1334 optr += dst->linesize[i];
1338 if (padbottom || padright) {
1339 optr = dst->data[i] + dst->linesize[i] *
1340 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1341 memset(optr, color[i],dst->linesize[i] *
1342 (padbottom >> y_shift) + (padright >> x_shift));
1348 /* NOTE: we scan all the pixels to have an exact information */
1349 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1351 const unsigned char *p;
1352 int src_wrap, ret, x, y;
1354 uint32_t *palette = (uint32_t *)src->data[1];
1357 src_wrap = src->linesize[0] - width;
1359 for(y=0;y<height;y++) {
1360 for(x=0;x<width;x++) {
1361 a = palette[p[0]] >> 24;
1363 ret |= FF_ALPHA_TRANSP;
1364 } else if (a != 0xff) {
1365 ret |= FF_ALPHA_SEMI_TRANSP;
1374 int img_get_alpha_info(const AVPicture *src,
1375 enum PixelFormat pix_fmt, int width, int height)
1377 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1380 /* no alpha can be represented in format */
1385 ret = get_alpha_info_pal8(src, width, height);
1388 /* we do not know, so everything is indicated */
1389 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1396 #define DEINT_INPLACE_LINE_LUM \
1397 movd_m2r(lum_m4[0],mm0);\
1398 movd_m2r(lum_m3[0],mm1);\
1399 movd_m2r(lum_m2[0],mm2);\
1400 movd_m2r(lum_m1[0],mm3);\
1401 movd_m2r(lum[0],mm4);\
1402 punpcklbw_r2r(mm7,mm0);\
1403 movd_r2m(mm2,lum_m4[0]);\
1404 punpcklbw_r2r(mm7,mm1);\
1405 punpcklbw_r2r(mm7,mm2);\
1406 punpcklbw_r2r(mm7,mm3);\
1407 punpcklbw_r2r(mm7,mm4);\
1408 paddw_r2r(mm3,mm1);\
1410 paddw_r2r(mm4,mm0);\
1412 paddw_r2r(mm6,mm2);\
1413 paddw_r2r(mm2,mm1);\
1414 psubusw_r2r(mm0,mm1);\
1416 packuswb_r2r(mm7,mm1);\
1417 movd_r2m(mm1,lum_m2[0]);
1419 #define DEINT_LINE_LUM \
1420 movd_m2r(lum_m4[0],mm0);\
1421 movd_m2r(lum_m3[0],mm1);\
1422 movd_m2r(lum_m2[0],mm2);\
1423 movd_m2r(lum_m1[0],mm3);\
1424 movd_m2r(lum[0],mm4);\
1425 punpcklbw_r2r(mm7,mm0);\
1426 punpcklbw_r2r(mm7,mm1);\
1427 punpcklbw_r2r(mm7,mm2);\
1428 punpcklbw_r2r(mm7,mm3);\
1429 punpcklbw_r2r(mm7,mm4);\
1430 paddw_r2r(mm3,mm1);\
1432 paddw_r2r(mm4,mm0);\
1434 paddw_r2r(mm6,mm2);\
1435 paddw_r2r(mm2,mm1);\
1436 psubusw_r2r(mm0,mm1);\
1438 packuswb_r2r(mm7,mm1);\
1439 movd_r2m(mm1,dst[0]);
1442 /* filter parameters: [-1 4 2 4 -1] // 8 */
1443 static void deinterlace_line(uint8_t *dst,
1444 const uint8_t *lum_m4, const uint8_t *lum_m3,
1445 const uint8_t *lum_m2, const uint8_t *lum_m1,
1450 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1453 for(;size > 0;size--) {
1455 sum += lum_m3[0] << 2;
1456 sum += lum_m2[0] << 1;
1457 sum += lum_m1[0] << 2;
1459 dst[0] = cm[(sum + 4) >> 3];
1471 movq_m2r(ff_pw_4,mm6);
1473 for (;size > 3; size-=4) {
1484 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1488 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1491 for(;size > 0;size--) {
1493 sum += lum_m3[0] << 2;
1494 sum += lum_m2[0] << 1;
1495 lum_m4[0]=lum_m2[0];
1496 sum += lum_m1[0] << 2;
1498 lum_m2[0] = cm[(sum + 4) >> 3];
1509 movq_m2r(ff_pw_4,mm6);
1511 for (;size > 3; size-=4) {
1512 DEINT_INPLACE_LINE_LUM
1522 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1523 top field is copied as is, but the bottom field is deinterlaced
1524 against the top field. */
1525 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1526 const uint8_t *src1, int src_wrap,
1527 int width, int height)
1529 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1534 src_0=&src_m1[src_wrap];
1535 src_p1=&src_0[src_wrap];
1536 src_p2=&src_p1[src_wrap];
1537 for(y=0;y<(height-2);y+=2) {
1538 memcpy(dst,src_m1,width);
1540 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1544 src_p1 += 2*src_wrap;
1545 src_p2 += 2*src_wrap;
1548 memcpy(dst,src_m1,width);
1551 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1554 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1555 int width, int height)
1557 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1560 buf = (uint8_t*)av_malloc(width);
1563 memcpy(buf,src_m1,width);
1564 src_0=&src_m1[src_wrap];
1565 src_p1=&src_0[src_wrap];
1566 src_p2=&src_p1[src_wrap];
1567 for(y=0;y<(height-2);y+=2) {
1568 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1571 src_p1 += 2*src_wrap;
1572 src_p2 += 2*src_wrap;
1575 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1579 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1580 enum PixelFormat pix_fmt, int width, int height)
1584 if (pix_fmt != PIX_FMT_YUV420P &&
1585 pix_fmt != PIX_FMT_YUV422P &&
1586 pix_fmt != PIX_FMT_YUV444P &&
1587 pix_fmt != PIX_FMT_YUV411P &&
1588 pix_fmt != PIX_FMT_GRAY8)
1590 if ((width & 3) != 0 || (height & 3) != 0)
1596 case PIX_FMT_YUV420P:
1600 case PIX_FMT_YUV422P:
1603 case PIX_FMT_YUV411P:
1609 if (pix_fmt == PIX_FMT_GRAY8) {
1614 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1617 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1618 src->data[i], src->linesize[i],