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"
37 #include "imgconvert.h"
38 #include "libavutil/pixdesc.h"
42 #include "x86/dsputil_mmx.h"
45 #define xglue(x, y) x ## y
46 #define glue(x, y) xglue(x, y)
48 #define FF_COLOR_RGB 0 /**< RGB color space */
49 #define FF_COLOR_GRAY 1 /**< gray color space */
50 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
51 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
53 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
54 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
55 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
57 typedef struct PixFmtInfo {
58 uint8_t nb_channels; /**< number of channels (including alpha) */
59 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
60 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
61 uint8_t is_alpha : 1; /**< true if alpha can be specified */
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] = {
70 .color_type = FF_COLOR_YUV,
71 .pixel_type = FF_PIXEL_PLANAR,
76 .color_type = FF_COLOR_YUV,
77 .pixel_type = FF_PIXEL_PLANAR,
82 .color_type = FF_COLOR_YUV,
83 .pixel_type = FF_PIXEL_PLANAR,
88 .color_type = FF_COLOR_YUV,
89 .pixel_type = FF_PIXEL_PACKED,
94 .color_type = FF_COLOR_YUV,
95 .pixel_type = FF_PIXEL_PACKED,
100 .color_type = FF_COLOR_YUV,
101 .pixel_type = FF_PIXEL_PLANAR,
104 [PIX_FMT_YUV411P] = {
106 .color_type = FF_COLOR_YUV,
107 .pixel_type = FF_PIXEL_PLANAR,
110 [PIX_FMT_YUV440P] = {
112 .color_type = FF_COLOR_YUV,
113 .pixel_type = FF_PIXEL_PLANAR,
116 [PIX_FMT_YUV420P16LE] = {
118 .color_type = FF_COLOR_YUV,
119 .pixel_type = FF_PIXEL_PLANAR,
122 [PIX_FMT_YUV422P16LE] = {
124 .color_type = FF_COLOR_YUV,
125 .pixel_type = FF_PIXEL_PLANAR,
128 [PIX_FMT_YUV444P16LE] = {
130 .color_type = FF_COLOR_YUV,
131 .pixel_type = FF_PIXEL_PLANAR,
134 [PIX_FMT_YUV420P16BE] = {
136 .color_type = FF_COLOR_YUV,
137 .pixel_type = FF_PIXEL_PLANAR,
140 [PIX_FMT_YUV422P16BE] = {
142 .color_type = FF_COLOR_YUV,
143 .pixel_type = FF_PIXEL_PLANAR,
146 [PIX_FMT_YUV444P16BE] = {
148 .color_type = FF_COLOR_YUV,
149 .pixel_type = FF_PIXEL_PLANAR,
154 /* YUV formats with alpha plane */
155 [PIX_FMT_YUVA420P] = {
157 .color_type = FF_COLOR_YUV,
158 .pixel_type = FF_PIXEL_PLANAR,
163 [PIX_FMT_YUVJ420P] = {
165 .color_type = FF_COLOR_YUV_JPEG,
166 .pixel_type = FF_PIXEL_PLANAR,
169 [PIX_FMT_YUVJ422P] = {
171 .color_type = FF_COLOR_YUV_JPEG,
172 .pixel_type = FF_PIXEL_PLANAR,
175 [PIX_FMT_YUVJ444P] = {
177 .color_type = FF_COLOR_YUV_JPEG,
178 .pixel_type = FF_PIXEL_PLANAR,
181 [PIX_FMT_YUVJ440P] = {
183 .color_type = FF_COLOR_YUV_JPEG,
184 .pixel_type = FF_PIXEL_PLANAR,
191 .color_type = FF_COLOR_RGB,
192 .pixel_type = FF_PIXEL_PACKED,
197 .color_type = FF_COLOR_RGB,
198 .pixel_type = FF_PIXEL_PACKED,
202 .nb_channels = 4, .is_alpha = 1,
203 .color_type = FF_COLOR_RGB,
204 .pixel_type = FF_PIXEL_PACKED,
207 [PIX_FMT_RGB48BE] = {
209 .color_type = FF_COLOR_RGB,
210 .pixel_type = FF_PIXEL_PACKED,
213 [PIX_FMT_RGB48LE] = {
215 .color_type = FF_COLOR_RGB,
216 .pixel_type = FF_PIXEL_PACKED,
219 [PIX_FMT_RGB565BE] = {
221 .color_type = FF_COLOR_RGB,
222 .pixel_type = FF_PIXEL_PACKED,
225 [PIX_FMT_RGB565LE] = {
227 .color_type = FF_COLOR_RGB,
228 .pixel_type = FF_PIXEL_PACKED,
231 [PIX_FMT_RGB555BE] = {
233 .color_type = FF_COLOR_RGB,
234 .pixel_type = FF_PIXEL_PACKED,
237 [PIX_FMT_RGB555LE] = {
239 .color_type = FF_COLOR_RGB,
240 .pixel_type = FF_PIXEL_PACKED,
244 /* gray / mono formats */
245 [PIX_FMT_GRAY16BE] = {
247 .color_type = FF_COLOR_GRAY,
248 .pixel_type = FF_PIXEL_PLANAR,
251 [PIX_FMT_GRAY16LE] = {
253 .color_type = FF_COLOR_GRAY,
254 .pixel_type = FF_PIXEL_PLANAR,
259 .color_type = FF_COLOR_GRAY,
260 .pixel_type = FF_PIXEL_PLANAR,
263 [PIX_FMT_MONOWHITE] = {
265 .color_type = FF_COLOR_GRAY,
266 .pixel_type = FF_PIXEL_PLANAR,
269 [PIX_FMT_MONOBLACK] = {
271 .color_type = FF_COLOR_GRAY,
272 .pixel_type = FF_PIXEL_PLANAR,
276 /* paletted formats */
278 .nb_channels = 4, .is_alpha = 1,
279 .color_type = FF_COLOR_RGB,
280 .pixel_type = FF_PIXEL_PALETTE,
283 [PIX_FMT_UYYVYY411] = {
285 .color_type = FF_COLOR_YUV,
286 .pixel_type = FF_PIXEL_PACKED,
290 .nb_channels = 4, .is_alpha = 1,
291 .color_type = FF_COLOR_RGB,
292 .pixel_type = FF_PIXEL_PACKED,
295 [PIX_FMT_BGR565BE] = {
297 .color_type = FF_COLOR_RGB,
298 .pixel_type = FF_PIXEL_PACKED,
301 [PIX_FMT_BGR565LE] = {
303 .color_type = FF_COLOR_RGB,
304 .pixel_type = FF_PIXEL_PACKED,
307 [PIX_FMT_BGR555BE] = {
309 .color_type = FF_COLOR_RGB,
310 .pixel_type = FF_PIXEL_PACKED,
313 [PIX_FMT_BGR555LE] = {
315 .color_type = FF_COLOR_RGB,
316 .pixel_type = FF_PIXEL_PACKED,
321 .color_type = FF_COLOR_RGB,
322 .pixel_type = FF_PIXEL_PACKED,
327 .color_type = FF_COLOR_RGB,
328 .pixel_type = FF_PIXEL_PACKED,
331 [PIX_FMT_RGB4_BYTE] = {
333 .color_type = FF_COLOR_RGB,
334 .pixel_type = FF_PIXEL_PACKED,
339 .color_type = FF_COLOR_RGB,
340 .pixel_type = FF_PIXEL_PACKED,
345 .color_type = FF_COLOR_RGB,
346 .pixel_type = FF_PIXEL_PACKED,
349 [PIX_FMT_BGR4_BYTE] = {
351 .color_type = FF_COLOR_RGB,
352 .pixel_type = FF_PIXEL_PACKED,
357 .color_type = FF_COLOR_YUV,
358 .pixel_type = FF_PIXEL_PLANAR,
363 .color_type = FF_COLOR_YUV,
364 .pixel_type = FF_PIXEL_PLANAR,
369 .nb_channels = 4, .is_alpha = 1,
370 .color_type = FF_COLOR_RGB,
371 .pixel_type = FF_PIXEL_PACKED,
375 .nb_channels = 4, .is_alpha = 1,
376 .color_type = FF_COLOR_RGB,
377 .pixel_type = FF_PIXEL_PACKED,
382 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
384 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
385 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
388 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
390 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
393 return av_pix_fmt_descriptors[pix_fmt].name;
396 #if LIBAVCODEC_VERSION_MAJOR < 53
397 enum PixelFormat avcodec_get_pix_fmt(const char *name)
399 return av_get_pix_fmt(name);
403 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
407 snprintf (buf, buf_size,
408 "name " " nb_channels" " depth" " is_alpha"
411 PixFmtInfo info= pix_fmt_info[pix_fmt];
413 char is_alpha_char= info.is_alpha ? 'y' : 'n';
415 snprintf (buf, buf_size,
417 av_pix_fmt_descriptors[pix_fmt].name,
425 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
427 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
430 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
433 for(i=0; i<256; i++){
447 case PIX_FMT_RGB4_BYTE:
452 case PIX_FMT_BGR4_BYTE:
463 pal[i] = b + (g<<8) + (r<<16);
469 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
472 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
474 memset(picture->linesize, 0, sizeof(picture->linesize));
477 case PIX_FMT_YUV420P:
478 case PIX_FMT_YUV422P:
479 case PIX_FMT_YUV444P:
480 case PIX_FMT_YUV410P:
481 case PIX_FMT_YUV411P:
482 case PIX_FMT_YUV440P:
483 case PIX_FMT_YUVJ420P:
484 case PIX_FMT_YUVJ422P:
485 case PIX_FMT_YUVJ444P:
486 case PIX_FMT_YUVJ440P:
487 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
488 picture->linesize[0] = width;
489 picture->linesize[1] = w2;
490 picture->linesize[2] = w2;
492 case PIX_FMT_YUV420P16LE:
493 case PIX_FMT_YUV422P16LE:
494 case PIX_FMT_YUV444P16LE:
495 case PIX_FMT_YUV420P16BE:
496 case PIX_FMT_YUV422P16BE:
497 case PIX_FMT_YUV444P16BE:
498 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
499 picture->linesize[0] = 2*width;
500 picture->linesize[1] = 2*w2;
501 picture->linesize[2] = 2*w2;
503 case PIX_FMT_YUVA420P:
504 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
505 picture->linesize[0] = width;
506 picture->linesize[1] = w2;
507 picture->linesize[2] = w2;
508 picture->linesize[3] = width;
512 w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
513 picture->linesize[0] = width;
514 picture->linesize[1] = 2 * w2;
518 picture->linesize[0] = width * 3;
524 picture->linesize[0] = width * 4;
526 case PIX_FMT_RGB48BE:
527 case PIX_FMT_RGB48LE:
528 picture->linesize[0] = width * 6;
530 case PIX_FMT_GRAY16BE:
531 case PIX_FMT_GRAY16LE:
532 case PIX_FMT_BGR555BE:
533 case PIX_FMT_BGR555LE:
534 case PIX_FMT_BGR565BE:
535 case PIX_FMT_BGR565LE:
536 case PIX_FMT_RGB555BE:
537 case PIX_FMT_RGB555LE:
538 case PIX_FMT_RGB565BE:
539 case PIX_FMT_RGB565LE:
540 case PIX_FMT_YUYV422:
541 picture->linesize[0] = width * 2;
543 case PIX_FMT_UYVY422:
544 picture->linesize[0] = width * 2;
546 case PIX_FMT_UYYVYY411:
547 picture->linesize[0] = width + width/2;
551 picture->linesize[0] = width / 2;
553 case PIX_FMT_MONOWHITE:
554 case PIX_FMT_MONOBLACK:
555 picture->linesize[0] = (width + 7) >> 3;
560 case PIX_FMT_RGB4_BYTE:
561 case PIX_FMT_BGR4_BYTE:
563 picture->linesize[0] = width;
571 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
575 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
577 size = picture->linesize[0] * height;
579 case PIX_FMT_YUV420P:
580 case PIX_FMT_YUV422P:
581 case PIX_FMT_YUV444P:
582 case PIX_FMT_YUV410P:
583 case PIX_FMT_YUV411P:
584 case PIX_FMT_YUV440P:
585 case PIX_FMT_YUVJ420P:
586 case PIX_FMT_YUVJ422P:
587 case PIX_FMT_YUVJ444P:
588 case PIX_FMT_YUVJ440P:
589 case PIX_FMT_YUV420P16LE:
590 case PIX_FMT_YUV422P16LE:
591 case PIX_FMT_YUV444P16LE:
592 case PIX_FMT_YUV420P16BE:
593 case PIX_FMT_YUV422P16BE:
594 case PIX_FMT_YUV444P16BE:
595 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
596 size2 = picture->linesize[1] * h2;
597 picture->data[0] = ptr;
598 picture->data[1] = picture->data[0] + size;
599 picture->data[2] = picture->data[1] + size2;
600 picture->data[3] = NULL;
601 return size + 2 * size2;
602 case PIX_FMT_YUVA420P:
603 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
604 size2 = picture->linesize[1] * h2;
605 picture->data[0] = ptr;
606 picture->data[1] = picture->data[0] + size;
607 picture->data[2] = picture->data[1] + size2;
608 picture->data[3] = picture->data[1] + size2 + size2;
609 return 2 * size + 2 * size2;
612 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
613 size2 = picture->linesize[1] * h2;
614 picture->data[0] = ptr;
615 picture->data[1] = picture->data[0] + size;
616 picture->data[2] = NULL;
617 picture->data[3] = NULL;
625 case PIX_FMT_RGB48BE:
626 case PIX_FMT_RGB48LE:
627 case PIX_FMT_GRAY16BE:
628 case PIX_FMT_GRAY16LE:
629 case PIX_FMT_BGR555BE:
630 case PIX_FMT_BGR555LE:
631 case PIX_FMT_BGR565BE:
632 case PIX_FMT_BGR565LE:
633 case PIX_FMT_RGB555BE:
634 case PIX_FMT_RGB555LE:
635 case PIX_FMT_RGB565BE:
636 case PIX_FMT_RGB565LE:
637 case PIX_FMT_YUYV422:
638 case PIX_FMT_UYVY422:
639 case PIX_FMT_UYYVYY411:
642 case PIX_FMT_MONOWHITE:
643 case PIX_FMT_MONOBLACK:
644 picture->data[0] = ptr;
645 picture->data[1] = NULL;
646 picture->data[2] = NULL;
647 picture->data[3] = NULL;
652 case PIX_FMT_RGB4_BYTE:
653 case PIX_FMT_BGR4_BYTE:
655 size2 = (size + 3) & ~3;
656 picture->data[0] = ptr;
657 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
658 picture->data[2] = NULL;
659 picture->data[3] = NULL;
660 return size2 + 256 * 4;
662 picture->data[0] = NULL;
663 picture->data[1] = NULL;
664 picture->data[2] = NULL;
665 picture->data[3] = NULL;
670 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
671 enum PixelFormat pix_fmt, int width, int height)
674 if(avcodec_check_dimensions(NULL, width, height))
677 if (ff_fill_linesize(picture, pix_fmt, width))
680 return ff_fill_pointer(picture, ptr, pix_fmt, height);
683 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
684 unsigned char *dest, int dest_size)
686 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
687 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
688 int i, j, w, ow, h, oh, data_planes;
689 const unsigned char* s;
690 int size = avpicture_get_size(pix_fmt, width, height);
692 if (size > dest_size || size < 0)
695 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
696 if (pix_fmt == PIX_FMT_YUYV422 ||
697 pix_fmt == PIX_FMT_UYVY422 ||
698 pix_fmt == PIX_FMT_BGR565BE ||
699 pix_fmt == PIX_FMT_BGR565LE ||
700 pix_fmt == PIX_FMT_BGR555BE ||
701 pix_fmt == PIX_FMT_BGR555LE ||
702 pix_fmt == PIX_FMT_RGB565BE ||
703 pix_fmt == PIX_FMT_RGB565LE ||
704 pix_fmt == PIX_FMT_RGB555BE ||
705 pix_fmt == PIX_FMT_RGB555LE)
707 else if (pix_fmt == PIX_FMT_UYYVYY411)
709 else if (pix_fmt == PIX_FMT_PAL8)
712 w = width * (pf->depth * pf->nb_channels / 8);
717 data_planes = pf->nb_channels;
718 w = (width*pf->depth + 7)/8;
725 for (i=0; i<data_planes; i++) {
727 w = ((width >> desc->log2_chroma_w) * pf->depth + 7) / 8;
728 h = height >> desc->log2_chroma_h;
729 if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
739 s += src->linesize[i];
743 if (pf->pixel_type == FF_PIXEL_PALETTE)
744 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
749 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
751 AVPicture dummy_pict;
752 if(avcodec_check_dimensions(NULL, width, height))
757 case PIX_FMT_RGB4_BYTE:
758 case PIX_FMT_BGR4_BYTE:
760 // do not include palette for these pseudo-paletted formats
761 return width * height;
763 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
766 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
769 const PixFmtInfo *pf, *ps;
770 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
771 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
774 ps = &pix_fmt_info[src_pix_fmt];
778 pf = &pix_fmt_info[dst_pix_fmt];
779 if (pf->depth < ps->depth ||
780 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE) &&
781 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE)))
782 loss |= FF_LOSS_DEPTH;
783 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
784 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
785 loss |= FF_LOSS_RESOLUTION;
786 switch(pf->color_type) {
788 if (ps->color_type != FF_COLOR_RGB &&
789 ps->color_type != FF_COLOR_GRAY)
790 loss |= FF_LOSS_COLORSPACE;
793 if (ps->color_type != FF_COLOR_GRAY)
794 loss |= FF_LOSS_COLORSPACE;
797 if (ps->color_type != FF_COLOR_YUV)
798 loss |= FF_LOSS_COLORSPACE;
800 case FF_COLOR_YUV_JPEG:
801 if (ps->color_type != FF_COLOR_YUV_JPEG &&
802 ps->color_type != FF_COLOR_YUV &&
803 ps->color_type != FF_COLOR_GRAY)
804 loss |= FF_LOSS_COLORSPACE;
808 if (ps->color_type != pf->color_type)
809 loss |= FF_LOSS_COLORSPACE;
812 if (pf->color_type == FF_COLOR_GRAY &&
813 ps->color_type != FF_COLOR_GRAY)
814 loss |= FF_LOSS_CHROMA;
815 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
816 loss |= FF_LOSS_ALPHA;
817 if (pf->pixel_type == FF_PIXEL_PALETTE &&
818 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
819 loss |= FF_LOSS_COLORQUANT;
823 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
826 const PixFmtInfo *pf;
827 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
829 pf = &pix_fmt_info[pix_fmt];
830 switch(pf->pixel_type) {
831 case FF_PIXEL_PACKED:
833 case PIX_FMT_YUYV422:
834 case PIX_FMT_UYVY422:
835 case PIX_FMT_RGB565BE:
836 case PIX_FMT_RGB565LE:
837 case PIX_FMT_RGB555BE:
838 case PIX_FMT_RGB555LE:
839 case PIX_FMT_BGR565BE:
840 case PIX_FMT_BGR565LE:
841 case PIX_FMT_BGR555BE:
842 case PIX_FMT_BGR555LE:
845 case PIX_FMT_UYYVYY411:
849 bits = pf->depth * pf->nb_channels;
853 case FF_PIXEL_PLANAR:
854 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
855 bits = pf->depth * pf->nb_channels;
857 bits = pf->depth + ((2 * pf->depth) >>
858 (desc->log2_chroma_w + desc->log2_chroma_h));
861 case FF_PIXEL_PALETTE:
871 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
872 enum PixelFormat src_pix_fmt,
876 int dist, i, loss, min_dist;
877 enum PixelFormat dst_pix_fmt;
879 /* find exact color match with smallest size */
880 dst_pix_fmt = PIX_FMT_NONE;
881 min_dist = 0x7fffffff;
882 for(i = 0;i < PIX_FMT_NB; i++) {
883 if (pix_fmt_mask & (1ULL << i)) {
884 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
886 dist = avg_bits_per_pixel(i);
887 if (dist < min_dist) {
897 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
898 int has_alpha, int *loss_ptr)
900 enum PixelFormat dst_pix_fmt;
902 static const int loss_mask_order[] = {
903 ~0, /* no loss first */
906 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
912 /* try with successive loss */
915 loss_mask = loss_mask_order[i++];
916 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
917 has_alpha, loss_mask);
918 if (dst_pix_fmt >= 0)
926 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
930 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
931 const uint8_t *src, int src_wrap,
932 int width, int height)
936 for(;height > 0; height--) {
937 memcpy(dst, src, width);
943 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
946 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
947 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
949 pf = &pix_fmt_info[pix_fmt];
950 switch(pf->pixel_type) {
951 case FF_PIXEL_PACKED:
953 case PIX_FMT_YUYV422:
954 case PIX_FMT_UYVY422:
955 case PIX_FMT_RGB565BE:
956 case PIX_FMT_RGB565LE:
957 case PIX_FMT_RGB555BE:
958 case PIX_FMT_RGB555LE:
959 case PIX_FMT_BGR565BE:
960 case PIX_FMT_BGR565LE:
961 case PIX_FMT_BGR555BE:
962 case PIX_FMT_BGR555LE:
965 case PIX_FMT_UYYVYY411:
969 bits = pf->depth * pf->nb_channels;
972 return (width * bits + 7) >> 3;
974 case FF_PIXEL_PLANAR:
975 if (plane == 1 || plane == 2)
976 width= -((-width)>>desc->log2_chroma_w);
978 return (width * pf->depth + 7) >> 3;
980 case FF_PIXEL_PALETTE:
989 void av_picture_copy(AVPicture *dst, const AVPicture *src,
990 enum PixelFormat pix_fmt, int width, int height)
993 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
994 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
996 switch(pf->pixel_type) {
997 case FF_PIXEL_PACKED:
998 case FF_PIXEL_PLANAR:
999 for(i = 0; i < pf->nb_channels; i++) {
1001 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
1003 if (i == 1 || i == 2) {
1004 h= -((-height)>>desc->log2_chroma_h);
1006 ff_img_copy_plane(dst->data[i], dst->linesize[i],
1007 src->data[i], src->linesize[i],
1011 case FF_PIXEL_PALETTE:
1012 ff_img_copy_plane(dst->data[0], dst->linesize[0],
1013 src->data[0], src->linesize[0],
1015 /* copy the palette */
1016 memcpy(dst->data[1], src->data[1], 4*256);
1022 void ff_shrink22(uint8_t *dst, int dst_wrap,
1023 const uint8_t *src, int src_wrap,
1024 int width, int height)
1027 const uint8_t *s1, *s2;
1030 for(;height > 0; height--) {
1034 for(w = width;w >= 4; w-=4) {
1035 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1036 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1037 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1038 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1044 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1049 src += 2 * src_wrap;
1055 void ff_shrink44(uint8_t *dst, int dst_wrap,
1056 const uint8_t *src, int src_wrap,
1057 int width, int height)
1060 const uint8_t *s1, *s2, *s3, *s4;
1063 for(;height > 0; height--) {
1069 for(w = width;w > 0; w--) {
1070 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1071 s2[0] + s2[1] + s2[2] + s2[3] +
1072 s3[0] + s3[1] + s3[2] + s3[3] +
1073 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1080 src += 4 * src_wrap;
1086 void ff_shrink88(uint8_t *dst, int dst_wrap,
1087 const uint8_t *src, int src_wrap,
1088 int width, int height)
1092 for(;height > 0; height--) {
1093 for(w = width;w > 0; w--) {
1096 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1099 *(dst++) = (tmp + 32)>>6;
1100 src += 8 - 8*src_wrap;
1102 src += 8*src_wrap - 8*width;
1103 dst += dst_wrap - width;
1108 int avpicture_alloc(AVPicture *picture,
1109 enum PixelFormat pix_fmt, int width, int height)
1114 size = avpicture_fill(picture, NULL, pix_fmt, width, height);
1117 ptr = av_malloc(size);
1120 avpicture_fill(picture, ptr, pix_fmt, width, height);
1121 if(picture->data[1] && !picture->data[2])
1122 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
1126 memset(picture, 0, sizeof(AVPicture));
1130 void avpicture_free(AVPicture *picture)
1132 av_free(picture->data[0]);
1135 /* return true if yuv planar */
1136 static inline int is_yuv_planar(const PixFmtInfo *ps)
1138 return (ps->color_type == FF_COLOR_YUV ||
1139 ps->color_type == FF_COLOR_YUV_JPEG) &&
1140 ps->pixel_type == FF_PIXEL_PLANAR;
1143 int av_picture_crop(AVPicture *dst, const AVPicture *src,
1144 enum PixelFormat pix_fmt, int top_band, int left_band)
1149 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1152 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
1153 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
1155 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1156 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1157 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1159 dst->linesize[0] = src->linesize[0];
1160 dst->linesize[1] = src->linesize[1];
1161 dst->linesize[2] = src->linesize[2];
1165 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
1166 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
1175 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1176 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
1178 for (i = 0; i < 3; i++) {
1179 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
1180 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
1182 if (padtop || padleft) {
1183 memset(dst->data[i], color[i],
1184 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
1187 if (padleft || padright) {
1188 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1189 (dst->linesize[i] - (padright >> x_shift));
1190 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1191 for (y = 0; y < yheight; y++) {
1192 memset(optr, color[i], (padleft + padright) >> x_shift);
1193 optr += dst->linesize[i];
1197 if (src) { /* first line */
1198 uint8_t *iptr = src->data[i];
1199 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1200 (padleft >> x_shift);
1201 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
1202 iptr += src->linesize[i];
1203 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1204 (dst->linesize[i] - (padright >> x_shift));
1205 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1206 for (y = 0; y < yheight; y++) {
1207 memset(optr, color[i], (padleft + padright) >> x_shift);
1208 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1209 (width - padleft - padright) >> x_shift);
1210 iptr += src->linesize[i];
1211 optr += dst->linesize[i];
1215 if (padbottom || padright) {
1216 optr = dst->data[i] + dst->linesize[i] *
1217 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1218 memset(optr, color[i],dst->linesize[i] *
1219 (padbottom >> y_shift) + (padright >> x_shift));
1225 /* NOTE: we scan all the pixels to have an exact information */
1226 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1228 const unsigned char *p;
1229 int src_wrap, ret, x, y;
1231 uint32_t *palette = (uint32_t *)src->data[1];
1234 src_wrap = src->linesize[0] - width;
1236 for(y=0;y<height;y++) {
1237 for(x=0;x<width;x++) {
1238 a = palette[p[0]] >> 24;
1240 ret |= FF_ALPHA_TRANSP;
1241 } else if (a != 0xff) {
1242 ret |= FF_ALPHA_SEMI_TRANSP;
1251 int img_get_alpha_info(const AVPicture *src,
1252 enum PixelFormat pix_fmt, int width, int height)
1254 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1257 /* no alpha can be represented in format */
1262 ret = get_alpha_info_pal8(src, width, height);
1265 /* we do not know, so everything is indicated */
1266 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1273 #define DEINT_INPLACE_LINE_LUM \
1274 movd_m2r(lum_m4[0],mm0);\
1275 movd_m2r(lum_m3[0],mm1);\
1276 movd_m2r(lum_m2[0],mm2);\
1277 movd_m2r(lum_m1[0],mm3);\
1278 movd_m2r(lum[0],mm4);\
1279 punpcklbw_r2r(mm7,mm0);\
1280 movd_r2m(mm2,lum_m4[0]);\
1281 punpcklbw_r2r(mm7,mm1);\
1282 punpcklbw_r2r(mm7,mm2);\
1283 punpcklbw_r2r(mm7,mm3);\
1284 punpcklbw_r2r(mm7,mm4);\
1285 paddw_r2r(mm3,mm1);\
1287 paddw_r2r(mm4,mm0);\
1289 paddw_r2r(mm6,mm2);\
1290 paddw_r2r(mm2,mm1);\
1291 psubusw_r2r(mm0,mm1);\
1293 packuswb_r2r(mm7,mm1);\
1294 movd_r2m(mm1,lum_m2[0]);
1296 #define DEINT_LINE_LUM \
1297 movd_m2r(lum_m4[0],mm0);\
1298 movd_m2r(lum_m3[0],mm1);\
1299 movd_m2r(lum_m2[0],mm2);\
1300 movd_m2r(lum_m1[0],mm3);\
1301 movd_m2r(lum[0],mm4);\
1302 punpcklbw_r2r(mm7,mm0);\
1303 punpcklbw_r2r(mm7,mm1);\
1304 punpcklbw_r2r(mm7,mm2);\
1305 punpcklbw_r2r(mm7,mm3);\
1306 punpcklbw_r2r(mm7,mm4);\
1307 paddw_r2r(mm3,mm1);\
1309 paddw_r2r(mm4,mm0);\
1311 paddw_r2r(mm6,mm2);\
1312 paddw_r2r(mm2,mm1);\
1313 psubusw_r2r(mm0,mm1);\
1315 packuswb_r2r(mm7,mm1);\
1316 movd_r2m(mm1,dst[0]);
1319 /* filter parameters: [-1 4 2 4 -1] // 8 */
1320 static void deinterlace_line(uint8_t *dst,
1321 const uint8_t *lum_m4, const uint8_t *lum_m3,
1322 const uint8_t *lum_m2, const uint8_t *lum_m1,
1327 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1330 for(;size > 0;size--) {
1332 sum += lum_m3[0] << 2;
1333 sum += lum_m2[0] << 1;
1334 sum += lum_m1[0] << 2;
1336 dst[0] = cm[(sum + 4) >> 3];
1348 movq_m2r(ff_pw_4,mm6);
1350 for (;size > 3; size-=4) {
1361 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1365 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1368 for(;size > 0;size--) {
1370 sum += lum_m3[0] << 2;
1371 sum += lum_m2[0] << 1;
1372 lum_m4[0]=lum_m2[0];
1373 sum += lum_m1[0] << 2;
1375 lum_m2[0] = cm[(sum + 4) >> 3];
1386 movq_m2r(ff_pw_4,mm6);
1388 for (;size > 3; size-=4) {
1389 DEINT_INPLACE_LINE_LUM
1399 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1400 top field is copied as is, but the bottom field is deinterlaced
1401 against the top field. */
1402 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1403 const uint8_t *src1, int src_wrap,
1404 int width, int height)
1406 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1411 src_0=&src_m1[src_wrap];
1412 src_p1=&src_0[src_wrap];
1413 src_p2=&src_p1[src_wrap];
1414 for(y=0;y<(height-2);y+=2) {
1415 memcpy(dst,src_m1,width);
1417 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1421 src_p1 += 2*src_wrap;
1422 src_p2 += 2*src_wrap;
1425 memcpy(dst,src_m1,width);
1428 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1431 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1432 int width, int height)
1434 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1437 buf = (uint8_t*)av_malloc(width);
1440 memcpy(buf,src_m1,width);
1441 src_0=&src_m1[src_wrap];
1442 src_p1=&src_0[src_wrap];
1443 src_p2=&src_p1[src_wrap];
1444 for(y=0;y<(height-2);y+=2) {
1445 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1448 src_p1 += 2*src_wrap;
1449 src_p2 += 2*src_wrap;
1452 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1456 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1457 enum PixelFormat pix_fmt, int width, int height)
1461 if (pix_fmt != PIX_FMT_YUV420P &&
1462 pix_fmt != PIX_FMT_YUV422P &&
1463 pix_fmt != PIX_FMT_YUV444P &&
1464 pix_fmt != PIX_FMT_YUV411P &&
1465 pix_fmt != PIX_FMT_GRAY8)
1467 if ((width & 3) != 0 || (height & 3) != 0)
1473 case PIX_FMT_YUV420P:
1477 case PIX_FMT_YUV422P:
1480 case PIX_FMT_YUV411P:
1486 if (pix_fmt == PIX_FMT_GRAY8) {
1491 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1494 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1495 src->data[i], src->linesize[i],