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 {
56 uint8_t nb_channels; /**< number of channels (including alpha) */
57 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
58 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
59 uint8_t is_alpha : 1; /**< true if alpha can be specified */
60 uint8_t depth; /**< bit depth of the color components */
63 /* this table gives more information about formats */
64 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
68 .color_type = FF_COLOR_YUV,
69 .pixel_type = FF_PIXEL_PLANAR,
74 .color_type = FF_COLOR_YUV,
75 .pixel_type = FF_PIXEL_PLANAR,
80 .color_type = FF_COLOR_YUV,
81 .pixel_type = FF_PIXEL_PLANAR,
86 .color_type = FF_COLOR_YUV,
87 .pixel_type = FF_PIXEL_PACKED,
92 .color_type = FF_COLOR_YUV,
93 .pixel_type = FF_PIXEL_PACKED,
98 .color_type = FF_COLOR_YUV,
99 .pixel_type = FF_PIXEL_PLANAR,
102 [PIX_FMT_YUV411P] = {
104 .color_type = FF_COLOR_YUV,
105 .pixel_type = FF_PIXEL_PLANAR,
108 [PIX_FMT_YUV440P] = {
110 .color_type = FF_COLOR_YUV,
111 .pixel_type = FF_PIXEL_PLANAR,
114 [PIX_FMT_YUV420P16LE] = {
116 .color_type = FF_COLOR_YUV,
117 .pixel_type = FF_PIXEL_PLANAR,
120 [PIX_FMT_YUV422P16LE] = {
122 .color_type = FF_COLOR_YUV,
123 .pixel_type = FF_PIXEL_PLANAR,
126 [PIX_FMT_YUV444P16LE] = {
128 .color_type = FF_COLOR_YUV,
129 .pixel_type = FF_PIXEL_PLANAR,
132 [PIX_FMT_YUV420P16BE] = {
134 .color_type = FF_COLOR_YUV,
135 .pixel_type = FF_PIXEL_PLANAR,
138 [PIX_FMT_YUV422P16BE] = {
140 .color_type = FF_COLOR_YUV,
141 .pixel_type = FF_PIXEL_PLANAR,
144 [PIX_FMT_YUV444P16BE] = {
146 .color_type = FF_COLOR_YUV,
147 .pixel_type = FF_PIXEL_PLANAR,
152 /* YUV formats with alpha plane */
153 [PIX_FMT_YUVA420P] = {
155 .color_type = FF_COLOR_YUV,
156 .pixel_type = FF_PIXEL_PLANAR,
161 [PIX_FMT_YUVJ420P] = {
163 .color_type = FF_COLOR_YUV_JPEG,
164 .pixel_type = FF_PIXEL_PLANAR,
167 [PIX_FMT_YUVJ422P] = {
169 .color_type = FF_COLOR_YUV_JPEG,
170 .pixel_type = FF_PIXEL_PLANAR,
173 [PIX_FMT_YUVJ444P] = {
175 .color_type = FF_COLOR_YUV_JPEG,
176 .pixel_type = FF_PIXEL_PLANAR,
179 [PIX_FMT_YUVJ440P] = {
181 .color_type = FF_COLOR_YUV_JPEG,
182 .pixel_type = FF_PIXEL_PLANAR,
189 .color_type = FF_COLOR_RGB,
190 .pixel_type = FF_PIXEL_PACKED,
195 .color_type = FF_COLOR_RGB,
196 .pixel_type = FF_PIXEL_PACKED,
200 .nb_channels = 4, .is_alpha = 1,
201 .color_type = FF_COLOR_RGB,
202 .pixel_type = FF_PIXEL_PACKED,
205 [PIX_FMT_RGB48BE] = {
207 .color_type = FF_COLOR_RGB,
208 .pixel_type = FF_PIXEL_PACKED,
211 [PIX_FMT_RGB48LE] = {
213 .color_type = FF_COLOR_RGB,
214 .pixel_type = FF_PIXEL_PACKED,
217 [PIX_FMT_RGB565BE] = {
219 .color_type = FF_COLOR_RGB,
220 .pixel_type = FF_PIXEL_PACKED,
223 [PIX_FMT_RGB565LE] = {
225 .color_type = FF_COLOR_RGB,
226 .pixel_type = FF_PIXEL_PACKED,
229 [PIX_FMT_RGB555BE] = {
231 .color_type = FF_COLOR_RGB,
232 .pixel_type = FF_PIXEL_PACKED,
235 [PIX_FMT_RGB555LE] = {
237 .color_type = FF_COLOR_RGB,
238 .pixel_type = FF_PIXEL_PACKED,
242 /* gray / mono formats */
243 [PIX_FMT_GRAY16BE] = {
245 .color_type = FF_COLOR_GRAY,
246 .pixel_type = FF_PIXEL_PLANAR,
249 [PIX_FMT_GRAY16LE] = {
251 .color_type = FF_COLOR_GRAY,
252 .pixel_type = FF_PIXEL_PLANAR,
257 .color_type = FF_COLOR_GRAY,
258 .pixel_type = FF_PIXEL_PLANAR,
261 [PIX_FMT_MONOWHITE] = {
263 .color_type = FF_COLOR_GRAY,
264 .pixel_type = FF_PIXEL_PLANAR,
267 [PIX_FMT_MONOBLACK] = {
269 .color_type = FF_COLOR_GRAY,
270 .pixel_type = FF_PIXEL_PLANAR,
274 /* paletted formats */
276 .nb_channels = 4, .is_alpha = 1,
277 .color_type = FF_COLOR_RGB,
278 .pixel_type = FF_PIXEL_PALETTE,
281 [PIX_FMT_XVMC_MPEG2_MC] = {
283 [PIX_FMT_XVMC_MPEG2_IDCT] = {
285 [PIX_FMT_VDPAU_MPEG1] = {
287 [PIX_FMT_VDPAU_MPEG2] = {
289 [PIX_FMT_VDPAU_H264] = {
291 [PIX_FMT_VDPAU_WMV3] = {
293 [PIX_FMT_VDPAU_VC1] = {
295 [PIX_FMT_VDPAU_MPEG4] = {
297 [PIX_FMT_UYYVYY411] = {
299 .color_type = FF_COLOR_YUV,
300 .pixel_type = FF_PIXEL_PACKED,
304 .nb_channels = 4, .is_alpha = 1,
305 .color_type = FF_COLOR_RGB,
306 .pixel_type = FF_PIXEL_PACKED,
309 [PIX_FMT_BGR565BE] = {
311 .color_type = FF_COLOR_RGB,
312 .pixel_type = FF_PIXEL_PACKED,
315 [PIX_FMT_BGR565LE] = {
317 .color_type = FF_COLOR_RGB,
318 .pixel_type = FF_PIXEL_PACKED,
321 [PIX_FMT_BGR555BE] = {
323 .color_type = FF_COLOR_RGB,
324 .pixel_type = FF_PIXEL_PACKED,
327 [PIX_FMT_BGR555LE] = {
329 .color_type = FF_COLOR_RGB,
330 .pixel_type = FF_PIXEL_PACKED,
335 .color_type = FF_COLOR_RGB,
336 .pixel_type = FF_PIXEL_PACKED,
341 .color_type = FF_COLOR_RGB,
342 .pixel_type = FF_PIXEL_PACKED,
345 [PIX_FMT_RGB4_BYTE] = {
347 .color_type = FF_COLOR_RGB,
348 .pixel_type = FF_PIXEL_PACKED,
353 .color_type = FF_COLOR_RGB,
354 .pixel_type = FF_PIXEL_PACKED,
359 .color_type = FF_COLOR_RGB,
360 .pixel_type = FF_PIXEL_PACKED,
363 [PIX_FMT_BGR4_BYTE] = {
365 .color_type = FF_COLOR_RGB,
366 .pixel_type = FF_PIXEL_PACKED,
371 .color_type = FF_COLOR_YUV,
372 .pixel_type = FF_PIXEL_PLANAR,
377 .color_type = FF_COLOR_YUV,
378 .pixel_type = FF_PIXEL_PLANAR,
383 .nb_channels = 4, .is_alpha = 1,
384 .color_type = FF_COLOR_RGB,
385 .pixel_type = FF_PIXEL_PACKED,
389 .nb_channels = 4, .is_alpha = 1,
390 .color_type = FF_COLOR_RGB,
391 .pixel_type = FF_PIXEL_PACKED,
396 [PIX_FMT_VAAPI_MOCO] = {
398 [PIX_FMT_VAAPI_IDCT] = {
400 [PIX_FMT_VAAPI_VLD] = {
404 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
406 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
407 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
410 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
412 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
415 return av_pix_fmt_descriptors[pix_fmt].name;
418 static enum PixelFormat avcodec_get_pix_fmt_internal(const char *name)
422 for (i=0; i < PIX_FMT_NB; i++)
423 if (av_pix_fmt_descriptors[i].name && !strcmp(av_pix_fmt_descriptors[i].name, name))
429 # define X_NE(be, le) be
431 # define X_NE(be, le) le
434 enum PixelFormat avcodec_get_pix_fmt(const char *name)
436 enum PixelFormat pix_fmt;
438 if (!strcmp(name, "rgb32"))
439 name = X_NE("argb", "bgra");
440 else if (!strcmp(name, "bgr32"))
441 name = X_NE("abgr", "rgba");
443 pix_fmt = avcodec_get_pix_fmt_internal(name);
444 if (pix_fmt == PIX_FMT_NONE) {
446 snprintf(name2, sizeof(name2), "%s%s", name, X_NE("be", "le"));
447 pix_fmt = avcodec_get_pix_fmt_internal(name2);
452 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
456 snprintf (buf, buf_size,
457 "name " " nb_channels" " depth" " is_alpha"
460 PixFmtInfo info= pix_fmt_info[pix_fmt];
462 char is_alpha_char= info.is_alpha ? 'y' : 'n';
464 snprintf (buf, buf_size,
466 av_pix_fmt_descriptors[pix_fmt].name,
474 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
476 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
479 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
482 for(i=0; i<256; i++){
496 case PIX_FMT_RGB4_BYTE:
501 case PIX_FMT_BGR4_BYTE:
512 pal[i] = b + (g<<8) + (r<<16);
518 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
521 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
523 memset(picture->linesize, 0, sizeof(picture->linesize));
526 case PIX_FMT_YUV420P:
527 case PIX_FMT_YUV422P:
528 case PIX_FMT_YUV444P:
529 case PIX_FMT_YUV410P:
530 case PIX_FMT_YUV411P:
531 case PIX_FMT_YUV440P:
532 case PIX_FMT_YUVJ420P:
533 case PIX_FMT_YUVJ422P:
534 case PIX_FMT_YUVJ444P:
535 case PIX_FMT_YUVJ440P:
536 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
537 picture->linesize[0] = width;
538 picture->linesize[1] = w2;
539 picture->linesize[2] = w2;
541 case PIX_FMT_YUV420P16LE:
542 case PIX_FMT_YUV422P16LE:
543 case PIX_FMT_YUV444P16LE:
544 case PIX_FMT_YUV420P16BE:
545 case PIX_FMT_YUV422P16BE:
546 case PIX_FMT_YUV444P16BE:
547 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
548 picture->linesize[0] = 2*width;
549 picture->linesize[1] = 2*w2;
550 picture->linesize[2] = 2*w2;
552 case PIX_FMT_YUVA420P:
553 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
554 picture->linesize[0] = width;
555 picture->linesize[1] = w2;
556 picture->linesize[2] = w2;
557 picture->linesize[3] = width;
561 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
562 picture->linesize[0] = width;
563 picture->linesize[1] = 2 * w2;
567 picture->linesize[0] = width * 3;
573 picture->linesize[0] = width * 4;
575 case PIX_FMT_RGB48BE:
576 case PIX_FMT_RGB48LE:
577 picture->linesize[0] = width * 6;
579 case PIX_FMT_GRAY16BE:
580 case PIX_FMT_GRAY16LE:
581 case PIX_FMT_BGR555BE:
582 case PIX_FMT_BGR555LE:
583 case PIX_FMT_BGR565BE:
584 case PIX_FMT_BGR565LE:
585 case PIX_FMT_RGB555BE:
586 case PIX_FMT_RGB555LE:
587 case PIX_FMT_RGB565BE:
588 case PIX_FMT_RGB565LE:
589 case PIX_FMT_YUYV422:
590 picture->linesize[0] = width * 2;
592 case PIX_FMT_UYVY422:
593 picture->linesize[0] = width * 2;
595 case PIX_FMT_UYYVYY411:
596 picture->linesize[0] = width + width/2;
600 picture->linesize[0] = width / 2;
602 case PIX_FMT_MONOWHITE:
603 case PIX_FMT_MONOBLACK:
604 picture->linesize[0] = (width + 7) >> 3;
609 case PIX_FMT_RGB4_BYTE:
610 case PIX_FMT_BGR4_BYTE:
612 picture->linesize[0] = width;
620 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
624 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
626 size = picture->linesize[0] * height;
628 case PIX_FMT_YUV420P:
629 case PIX_FMT_YUV422P:
630 case PIX_FMT_YUV444P:
631 case PIX_FMT_YUV410P:
632 case PIX_FMT_YUV411P:
633 case PIX_FMT_YUV440P:
634 case PIX_FMT_YUVJ420P:
635 case PIX_FMT_YUVJ422P:
636 case PIX_FMT_YUVJ444P:
637 case PIX_FMT_YUVJ440P:
638 case PIX_FMT_YUV420P16LE:
639 case PIX_FMT_YUV422P16LE:
640 case PIX_FMT_YUV444P16LE:
641 case PIX_FMT_YUV420P16BE:
642 case PIX_FMT_YUV422P16BE:
643 case PIX_FMT_YUV444P16BE:
644 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
645 size2 = picture->linesize[1] * h2;
646 picture->data[0] = ptr;
647 picture->data[1] = picture->data[0] + size;
648 picture->data[2] = picture->data[1] + size2;
649 picture->data[3] = NULL;
650 return size + 2 * size2;
651 case PIX_FMT_YUVA420P:
652 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
653 size2 = picture->linesize[1] * h2;
654 picture->data[0] = ptr;
655 picture->data[1] = picture->data[0] + size;
656 picture->data[2] = picture->data[1] + size2;
657 picture->data[3] = picture->data[1] + size2 + size2;
658 return 2 * size + 2 * size2;
661 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
662 size2 = picture->linesize[1] * h2;
663 picture->data[0] = ptr;
664 picture->data[1] = picture->data[0] + size;
665 picture->data[2] = NULL;
666 picture->data[3] = NULL;
674 case PIX_FMT_RGB48BE:
675 case PIX_FMT_RGB48LE:
676 case PIX_FMT_GRAY16BE:
677 case PIX_FMT_GRAY16LE:
678 case PIX_FMT_BGR555BE:
679 case PIX_FMT_BGR555LE:
680 case PIX_FMT_BGR565BE:
681 case PIX_FMT_BGR565LE:
682 case PIX_FMT_RGB555BE:
683 case PIX_FMT_RGB555LE:
684 case PIX_FMT_RGB565BE:
685 case PIX_FMT_RGB565LE:
686 case PIX_FMT_YUYV422:
687 case PIX_FMT_UYVY422:
688 case PIX_FMT_UYYVYY411:
691 case PIX_FMT_MONOWHITE:
692 case PIX_FMT_MONOBLACK:
693 picture->data[0] = ptr;
694 picture->data[1] = NULL;
695 picture->data[2] = NULL;
696 picture->data[3] = NULL;
701 case PIX_FMT_RGB4_BYTE:
702 case PIX_FMT_BGR4_BYTE:
704 size2 = (size + 3) & ~3;
705 picture->data[0] = ptr;
706 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
707 picture->data[2] = NULL;
708 picture->data[3] = NULL;
709 return size2 + 256 * 4;
711 picture->data[0] = NULL;
712 picture->data[1] = NULL;
713 picture->data[2] = NULL;
714 picture->data[3] = NULL;
719 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
720 enum PixelFormat pix_fmt, int width, int height)
723 if(avcodec_check_dimensions(NULL, width, height))
726 if (ff_fill_linesize(picture, pix_fmt, width))
729 return ff_fill_pointer(picture, ptr, pix_fmt, height);
732 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
733 unsigned char *dest, int dest_size)
735 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
736 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
737 int i, j, w, ow, h, oh, data_planes;
738 const unsigned char* s;
739 int size = avpicture_get_size(pix_fmt, width, height);
741 if (size > dest_size || size < 0)
744 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
745 if (pix_fmt == PIX_FMT_YUYV422 ||
746 pix_fmt == PIX_FMT_UYVY422 ||
747 pix_fmt == PIX_FMT_BGR565BE ||
748 pix_fmt == PIX_FMT_BGR565LE ||
749 pix_fmt == PIX_FMT_BGR555BE ||
750 pix_fmt == PIX_FMT_BGR555LE ||
751 pix_fmt == PIX_FMT_RGB565BE ||
752 pix_fmt == PIX_FMT_RGB565LE ||
753 pix_fmt == PIX_FMT_RGB555BE ||
754 pix_fmt == PIX_FMT_RGB555LE)
756 else if (pix_fmt == PIX_FMT_UYYVYY411)
758 else if (pix_fmt == PIX_FMT_PAL8)
761 w = width * (pf->depth * pf->nb_channels / 8);
766 data_planes = pf->nb_channels;
767 w = (width*pf->depth + 7)/8;
774 for (i=0; i<data_planes; i++) {
776 w = ((width >> desc->log2_chroma_w) * pf->depth + 7) / 8;
777 h = height >> desc->log2_chroma_h;
778 if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
788 s += src->linesize[i];
792 if (pf->pixel_type == FF_PIXEL_PALETTE)
793 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
798 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
800 AVPicture dummy_pict;
801 if(avcodec_check_dimensions(NULL, width, height))
806 case PIX_FMT_RGB4_BYTE:
807 case PIX_FMT_BGR4_BYTE:
809 // do not include palette for these pseudo-paletted formats
810 return width * height;
812 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
815 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
818 const PixFmtInfo *pf, *ps;
819 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
820 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
823 ps = &pix_fmt_info[src_pix_fmt];
827 pf = &pix_fmt_info[dst_pix_fmt];
828 if (pf->depth < ps->depth ||
829 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE) &&
830 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE)))
831 loss |= FF_LOSS_DEPTH;
832 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
833 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
834 loss |= FF_LOSS_RESOLUTION;
835 switch(pf->color_type) {
837 if (ps->color_type != FF_COLOR_RGB &&
838 ps->color_type != FF_COLOR_GRAY)
839 loss |= FF_LOSS_COLORSPACE;
842 if (ps->color_type != FF_COLOR_GRAY)
843 loss |= FF_LOSS_COLORSPACE;
846 if (ps->color_type != FF_COLOR_YUV)
847 loss |= FF_LOSS_COLORSPACE;
849 case FF_COLOR_YUV_JPEG:
850 if (ps->color_type != FF_COLOR_YUV_JPEG &&
851 ps->color_type != FF_COLOR_YUV &&
852 ps->color_type != FF_COLOR_GRAY)
853 loss |= FF_LOSS_COLORSPACE;
857 if (ps->color_type != pf->color_type)
858 loss |= FF_LOSS_COLORSPACE;
861 if (pf->color_type == FF_COLOR_GRAY &&
862 ps->color_type != FF_COLOR_GRAY)
863 loss |= FF_LOSS_CHROMA;
864 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
865 loss |= FF_LOSS_ALPHA;
866 if (pf->pixel_type == FF_PIXEL_PALETTE &&
867 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
868 loss |= FF_LOSS_COLORQUANT;
872 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
875 const PixFmtInfo *pf;
876 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
878 pf = &pix_fmt_info[pix_fmt];
879 switch(pf->pixel_type) {
880 case FF_PIXEL_PACKED:
882 case PIX_FMT_YUYV422:
883 case PIX_FMT_UYVY422:
884 case PIX_FMT_RGB565BE:
885 case PIX_FMT_RGB565LE:
886 case PIX_FMT_RGB555BE:
887 case PIX_FMT_RGB555LE:
888 case PIX_FMT_BGR565BE:
889 case PIX_FMT_BGR565LE:
890 case PIX_FMT_BGR555BE:
891 case PIX_FMT_BGR555LE:
894 case PIX_FMT_UYYVYY411:
898 bits = pf->depth * pf->nb_channels;
902 case FF_PIXEL_PLANAR:
903 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
904 bits = pf->depth * pf->nb_channels;
906 bits = pf->depth + ((2 * pf->depth) >>
907 (desc->log2_chroma_w + desc->log2_chroma_h));
910 case FF_PIXEL_PALETTE:
920 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
921 enum PixelFormat src_pix_fmt,
925 int dist, i, loss, min_dist;
926 enum PixelFormat dst_pix_fmt;
928 /* find exact color match with smallest size */
930 min_dist = 0x7fffffff;
931 for(i = 0;i < PIX_FMT_NB; i++) {
932 if (pix_fmt_mask & (1ULL << i)) {
933 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
935 dist = avg_bits_per_pixel(i);
936 if (dist < min_dist) {
946 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
947 int has_alpha, int *loss_ptr)
949 enum PixelFormat dst_pix_fmt;
951 static const int loss_mask_order[] = {
952 ~0, /* no loss first */
955 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
961 /* try with successive loss */
964 loss_mask = loss_mask_order[i++];
965 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
966 has_alpha, loss_mask);
967 if (dst_pix_fmt >= 0)
975 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
979 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
980 const uint8_t *src, int src_wrap,
981 int width, int height)
985 for(;height > 0; height--) {
986 memcpy(dst, src, width);
992 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
995 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
996 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
998 pf = &pix_fmt_info[pix_fmt];
999 switch(pf->pixel_type) {
1000 case FF_PIXEL_PACKED:
1002 case PIX_FMT_YUYV422:
1003 case PIX_FMT_UYVY422:
1004 case PIX_FMT_RGB565BE:
1005 case PIX_FMT_RGB565LE:
1006 case PIX_FMT_RGB555BE:
1007 case PIX_FMT_RGB555LE:
1008 case PIX_FMT_BGR565BE:
1009 case PIX_FMT_BGR565LE:
1010 case PIX_FMT_BGR555BE:
1011 case PIX_FMT_BGR555LE:
1014 case PIX_FMT_UYYVYY411:
1018 bits = pf->depth * pf->nb_channels;
1021 return (width * bits + 7) >> 3;
1023 case FF_PIXEL_PLANAR:
1024 if (plane == 1 || plane == 2)
1025 width= -((-width)>>desc->log2_chroma_w);
1027 return (width * pf->depth + 7) >> 3;
1029 case FF_PIXEL_PALETTE:
1038 void av_picture_copy(AVPicture *dst, const AVPicture *src,
1039 enum PixelFormat pix_fmt, int width, int height)
1042 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1043 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1045 switch(pf->pixel_type) {
1046 case FF_PIXEL_PACKED:
1047 case FF_PIXEL_PLANAR:
1048 for(i = 0; i < pf->nb_channels; i++) {
1050 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
1052 if (i == 1 || i == 2) {
1053 h= -((-height)>>desc->log2_chroma_h);
1055 ff_img_copy_plane(dst->data[i], dst->linesize[i],
1056 src->data[i], src->linesize[i],
1060 case FF_PIXEL_PALETTE:
1061 ff_img_copy_plane(dst->data[0], dst->linesize[0],
1062 src->data[0], src->linesize[0],
1064 /* copy the palette */
1065 memcpy(dst->data[1], src->data[1], 4*256);
1071 void ff_shrink22(uint8_t *dst, int dst_wrap,
1072 const uint8_t *src, int src_wrap,
1073 int width, int height)
1076 const uint8_t *s1, *s2;
1079 for(;height > 0; height--) {
1083 for(w = width;w >= 4; w-=4) {
1084 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1085 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1086 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1087 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1093 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1098 src += 2 * src_wrap;
1104 void ff_shrink44(uint8_t *dst, int dst_wrap,
1105 const uint8_t *src, int src_wrap,
1106 int width, int height)
1109 const uint8_t *s1, *s2, *s3, *s4;
1112 for(;height > 0; height--) {
1118 for(w = width;w > 0; w--) {
1119 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1120 s2[0] + s2[1] + s2[2] + s2[3] +
1121 s3[0] + s3[1] + s3[2] + s3[3] +
1122 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1129 src += 4 * src_wrap;
1135 void ff_shrink88(uint8_t *dst, int dst_wrap,
1136 const uint8_t *src, int src_wrap,
1137 int width, int height)
1141 for(;height > 0; height--) {
1142 for(w = width;w > 0; w--) {
1145 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1148 *(dst++) = (tmp + 32)>>6;
1149 src += 8 - 8*src_wrap;
1151 src += 8*src_wrap - 8*width;
1152 dst += dst_wrap - width;
1157 int avpicture_alloc(AVPicture *picture,
1158 enum PixelFormat pix_fmt, int width, int height)
1163 size = avpicture_fill(picture, NULL, pix_fmt, width, height);
1166 ptr = av_malloc(size);
1169 avpicture_fill(picture, ptr, pix_fmt, width, height);
1170 if(picture->data[1] && !picture->data[2])
1171 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
1175 memset(picture, 0, sizeof(AVPicture));
1179 void avpicture_free(AVPicture *picture)
1181 av_free(picture->data[0]);
1184 /* return true if yuv planar */
1185 static inline int is_yuv_planar(const PixFmtInfo *ps)
1187 return (ps->color_type == FF_COLOR_YUV ||
1188 ps->color_type == FF_COLOR_YUV_JPEG) &&
1189 ps->pixel_type == FF_PIXEL_PLANAR;
1192 int av_picture_crop(AVPicture *dst, const AVPicture *src,
1193 enum PixelFormat pix_fmt, int top_band, int left_band)
1198 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1201 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
1202 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
1204 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1205 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1206 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1208 dst->linesize[0] = src->linesize[0];
1209 dst->linesize[1] = src->linesize[1];
1210 dst->linesize[2] = src->linesize[2];
1214 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
1215 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
1224 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1225 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
1227 for (i = 0; i < 3; i++) {
1228 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
1229 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
1231 if (padtop || padleft) {
1232 memset(dst->data[i], color[i],
1233 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
1236 if (padleft || padright) {
1237 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1238 (dst->linesize[i] - (padright >> x_shift));
1239 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1240 for (y = 0; y < yheight; y++) {
1241 memset(optr, color[i], (padleft + padright) >> x_shift);
1242 optr += dst->linesize[i];
1246 if (src) { /* first line */
1247 uint8_t *iptr = src->data[i];
1248 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1249 (padleft >> x_shift);
1250 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
1251 iptr += src->linesize[i];
1252 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1253 (dst->linesize[i] - (padright >> x_shift));
1254 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1255 for (y = 0; y < yheight; y++) {
1256 memset(optr, color[i], (padleft + padright) >> x_shift);
1257 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1258 (width - padleft - padright) >> x_shift);
1259 iptr += src->linesize[i];
1260 optr += dst->linesize[i];
1264 if (padbottom || padright) {
1265 optr = dst->data[i] + dst->linesize[i] *
1266 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1267 memset(optr, color[i],dst->linesize[i] *
1268 (padbottom >> y_shift) + (padright >> x_shift));
1274 /* NOTE: we scan all the pixels to have an exact information */
1275 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1277 const unsigned char *p;
1278 int src_wrap, ret, x, y;
1280 uint32_t *palette = (uint32_t *)src->data[1];
1283 src_wrap = src->linesize[0] - width;
1285 for(y=0;y<height;y++) {
1286 for(x=0;x<width;x++) {
1287 a = palette[p[0]] >> 24;
1289 ret |= FF_ALPHA_TRANSP;
1290 } else if (a != 0xff) {
1291 ret |= FF_ALPHA_SEMI_TRANSP;
1300 int img_get_alpha_info(const AVPicture *src,
1301 enum PixelFormat pix_fmt, int width, int height)
1303 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1306 /* no alpha can be represented in format */
1311 ret = get_alpha_info_pal8(src, width, height);
1314 /* we do not know, so everything is indicated */
1315 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1322 #define DEINT_INPLACE_LINE_LUM \
1323 movd_m2r(lum_m4[0],mm0);\
1324 movd_m2r(lum_m3[0],mm1);\
1325 movd_m2r(lum_m2[0],mm2);\
1326 movd_m2r(lum_m1[0],mm3);\
1327 movd_m2r(lum[0],mm4);\
1328 punpcklbw_r2r(mm7,mm0);\
1329 movd_r2m(mm2,lum_m4[0]);\
1330 punpcklbw_r2r(mm7,mm1);\
1331 punpcklbw_r2r(mm7,mm2);\
1332 punpcklbw_r2r(mm7,mm3);\
1333 punpcklbw_r2r(mm7,mm4);\
1334 paddw_r2r(mm3,mm1);\
1336 paddw_r2r(mm4,mm0);\
1338 paddw_r2r(mm6,mm2);\
1339 paddw_r2r(mm2,mm1);\
1340 psubusw_r2r(mm0,mm1);\
1342 packuswb_r2r(mm7,mm1);\
1343 movd_r2m(mm1,lum_m2[0]);
1345 #define DEINT_LINE_LUM \
1346 movd_m2r(lum_m4[0],mm0);\
1347 movd_m2r(lum_m3[0],mm1);\
1348 movd_m2r(lum_m2[0],mm2);\
1349 movd_m2r(lum_m1[0],mm3);\
1350 movd_m2r(lum[0],mm4);\
1351 punpcklbw_r2r(mm7,mm0);\
1352 punpcklbw_r2r(mm7,mm1);\
1353 punpcklbw_r2r(mm7,mm2);\
1354 punpcklbw_r2r(mm7,mm3);\
1355 punpcklbw_r2r(mm7,mm4);\
1356 paddw_r2r(mm3,mm1);\
1358 paddw_r2r(mm4,mm0);\
1360 paddw_r2r(mm6,mm2);\
1361 paddw_r2r(mm2,mm1);\
1362 psubusw_r2r(mm0,mm1);\
1364 packuswb_r2r(mm7,mm1);\
1365 movd_r2m(mm1,dst[0]);
1368 /* filter parameters: [-1 4 2 4 -1] // 8 */
1369 static void deinterlace_line(uint8_t *dst,
1370 const uint8_t *lum_m4, const uint8_t *lum_m3,
1371 const uint8_t *lum_m2, const uint8_t *lum_m1,
1376 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1379 for(;size > 0;size--) {
1381 sum += lum_m3[0] << 2;
1382 sum += lum_m2[0] << 1;
1383 sum += lum_m1[0] << 2;
1385 dst[0] = cm[(sum + 4) >> 3];
1397 movq_m2r(ff_pw_4,mm6);
1399 for (;size > 3; size-=4) {
1410 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1414 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1417 for(;size > 0;size--) {
1419 sum += lum_m3[0] << 2;
1420 sum += lum_m2[0] << 1;
1421 lum_m4[0]=lum_m2[0];
1422 sum += lum_m1[0] << 2;
1424 lum_m2[0] = cm[(sum + 4) >> 3];
1435 movq_m2r(ff_pw_4,mm6);
1437 for (;size > 3; size-=4) {
1438 DEINT_INPLACE_LINE_LUM
1448 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1449 top field is copied as is, but the bottom field is deinterlaced
1450 against the top field. */
1451 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1452 const uint8_t *src1, int src_wrap,
1453 int width, int height)
1455 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1460 src_0=&src_m1[src_wrap];
1461 src_p1=&src_0[src_wrap];
1462 src_p2=&src_p1[src_wrap];
1463 for(y=0;y<(height-2);y+=2) {
1464 memcpy(dst,src_m1,width);
1466 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1470 src_p1 += 2*src_wrap;
1471 src_p2 += 2*src_wrap;
1474 memcpy(dst,src_m1,width);
1477 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1480 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1481 int width, int height)
1483 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1486 buf = (uint8_t*)av_malloc(width);
1489 memcpy(buf,src_m1,width);
1490 src_0=&src_m1[src_wrap];
1491 src_p1=&src_0[src_wrap];
1492 src_p2=&src_p1[src_wrap];
1493 for(y=0;y<(height-2);y+=2) {
1494 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1497 src_p1 += 2*src_wrap;
1498 src_p2 += 2*src_wrap;
1501 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1505 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1506 enum PixelFormat pix_fmt, int width, int height)
1510 if (pix_fmt != PIX_FMT_YUV420P &&
1511 pix_fmt != PIX_FMT_YUV422P &&
1512 pix_fmt != PIX_FMT_YUV444P &&
1513 pix_fmt != PIX_FMT_YUV411P &&
1514 pix_fmt != PIX_FMT_GRAY8)
1516 if ((width & 3) != 0 || (height & 3) != 0)
1522 case PIX_FMT_YUV420P:
1526 case PIX_FMT_YUV422P:
1529 case PIX_FMT_YUV411P:
1535 if (pix_fmt == PIX_FMT_GRAY8) {
1540 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1543 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1544 src->data[i], src->linesize[i],