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"
39 #include "x86/dsputil_mmx.h"
42 #define xglue(x, y) x ## y
43 #define glue(x, y) xglue(x, y)
45 #define FF_COLOR_RGB 0 /**< RGB color space */
46 #define FF_COLOR_GRAY 1 /**< gray color space */
47 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
48 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
50 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
51 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
52 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
54 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 x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
61 uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
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,
74 .x_chroma_shift = 1, .y_chroma_shift = 1,
79 .color_type = FF_COLOR_YUV,
80 .pixel_type = FF_PIXEL_PLANAR,
82 .x_chroma_shift = 1, .y_chroma_shift = 0,
87 .color_type = FF_COLOR_YUV,
88 .pixel_type = FF_PIXEL_PLANAR,
90 .x_chroma_shift = 0, .y_chroma_shift = 0,
95 .color_type = FF_COLOR_YUV,
96 .pixel_type = FF_PIXEL_PACKED,
98 .x_chroma_shift = 1, .y_chroma_shift = 0,
100 [PIX_FMT_UYVY422] = {
103 .color_type = FF_COLOR_YUV,
104 .pixel_type = FF_PIXEL_PACKED,
106 .x_chroma_shift = 1, .y_chroma_shift = 0,
108 [PIX_FMT_YUV410P] = {
111 .color_type = FF_COLOR_YUV,
112 .pixel_type = FF_PIXEL_PLANAR,
114 .x_chroma_shift = 2, .y_chroma_shift = 2,
116 [PIX_FMT_YUV411P] = {
119 .color_type = FF_COLOR_YUV,
120 .pixel_type = FF_PIXEL_PLANAR,
122 .x_chroma_shift = 2, .y_chroma_shift = 0,
124 [PIX_FMT_YUV440P] = {
127 .color_type = FF_COLOR_YUV,
128 .pixel_type = FF_PIXEL_PLANAR,
130 .x_chroma_shift = 0, .y_chroma_shift = 1,
133 /* YUV formats with alpha plane */
134 [PIX_FMT_YUVA420P] = {
137 .color_type = FF_COLOR_YUV,
138 .pixel_type = FF_PIXEL_PLANAR,
140 .x_chroma_shift = 1, .y_chroma_shift = 1,
144 [PIX_FMT_YUVJ420P] = {
147 .color_type = FF_COLOR_YUV_JPEG,
148 .pixel_type = FF_PIXEL_PLANAR,
150 .x_chroma_shift = 1, .y_chroma_shift = 1,
152 [PIX_FMT_YUVJ422P] = {
155 .color_type = FF_COLOR_YUV_JPEG,
156 .pixel_type = FF_PIXEL_PLANAR,
158 .x_chroma_shift = 1, .y_chroma_shift = 0,
160 [PIX_FMT_YUVJ444P] = {
163 .color_type = FF_COLOR_YUV_JPEG,
164 .pixel_type = FF_PIXEL_PLANAR,
166 .x_chroma_shift = 0, .y_chroma_shift = 0,
168 [PIX_FMT_YUVJ440P] = {
171 .color_type = FF_COLOR_YUV_JPEG,
172 .pixel_type = FF_PIXEL_PLANAR,
174 .x_chroma_shift = 0, .y_chroma_shift = 1,
181 .color_type = FF_COLOR_RGB,
182 .pixel_type = FF_PIXEL_PACKED,
184 .x_chroma_shift = 0, .y_chroma_shift = 0,
189 .color_type = FF_COLOR_RGB,
190 .pixel_type = FF_PIXEL_PACKED,
192 .x_chroma_shift = 0, .y_chroma_shift = 0,
196 .nb_channels = 4, .is_alpha = 1,
197 .color_type = FF_COLOR_RGB,
198 .pixel_type = FF_PIXEL_PACKED,
200 .x_chroma_shift = 0, .y_chroma_shift = 0,
202 [PIX_FMT_RGB48BE] = {
205 .color_type = FF_COLOR_RGB,
206 .pixel_type = FF_PIXEL_PACKED,
208 .x_chroma_shift = 0, .y_chroma_shift = 0,
210 [PIX_FMT_RGB48LE] = {
213 .color_type = FF_COLOR_RGB,
214 .pixel_type = FF_PIXEL_PACKED,
216 .x_chroma_shift = 0, .y_chroma_shift = 0,
221 .color_type = FF_COLOR_RGB,
222 .pixel_type = FF_PIXEL_PACKED,
224 .x_chroma_shift = 0, .y_chroma_shift = 0,
229 .color_type = FF_COLOR_RGB,
230 .pixel_type = FF_PIXEL_PACKED,
232 .x_chroma_shift = 0, .y_chroma_shift = 0,
235 /* gray / mono formats */
236 [PIX_FMT_GRAY16BE] = {
239 .color_type = FF_COLOR_GRAY,
240 .pixel_type = FF_PIXEL_PLANAR,
243 [PIX_FMT_GRAY16LE] = {
246 .color_type = FF_COLOR_GRAY,
247 .pixel_type = FF_PIXEL_PLANAR,
253 .color_type = FF_COLOR_GRAY,
254 .pixel_type = FF_PIXEL_PLANAR,
257 [PIX_FMT_MONOWHITE] = {
260 .color_type = FF_COLOR_GRAY,
261 .pixel_type = FF_PIXEL_PLANAR,
264 [PIX_FMT_MONOBLACK] = {
267 .color_type = FF_COLOR_GRAY,
268 .pixel_type = FF_PIXEL_PLANAR,
272 /* paletted formats */
275 .nb_channels = 4, .is_alpha = 1,
276 .color_type = FF_COLOR_RGB,
277 .pixel_type = FF_PIXEL_PALETTE,
280 [PIX_FMT_XVMC_MPEG2_MC] = {
283 [PIX_FMT_XVMC_MPEG2_IDCT] = {
286 [PIX_FMT_VDPAU_MPEG1] = {
287 .name = "vdpau_mpeg1",
289 [PIX_FMT_VDPAU_MPEG2] = {
290 .name = "vdpau_mpeg2",
292 [PIX_FMT_VDPAU_H264] = {
293 .name = "vdpau_h264",
295 [PIX_FMT_VDPAU_WMV3] = {
296 .name = "vdpau_wmv3",
298 [PIX_FMT_VDPAU_VC1] = {
301 [PIX_FMT_UYYVYY411] = {
304 .color_type = FF_COLOR_YUV,
305 .pixel_type = FF_PIXEL_PACKED,
307 .x_chroma_shift = 2, .y_chroma_shift = 0,
311 .nb_channels = 4, .is_alpha = 1,
312 .color_type = FF_COLOR_RGB,
313 .pixel_type = FF_PIXEL_PACKED,
315 .x_chroma_shift = 0, .y_chroma_shift = 0,
320 .color_type = FF_COLOR_RGB,
321 .pixel_type = FF_PIXEL_PACKED,
323 .x_chroma_shift = 0, .y_chroma_shift = 0,
328 .color_type = FF_COLOR_RGB,
329 .pixel_type = FF_PIXEL_PACKED,
331 .x_chroma_shift = 0, .y_chroma_shift = 0,
336 .color_type = FF_COLOR_RGB,
337 .pixel_type = FF_PIXEL_PACKED,
339 .x_chroma_shift = 0, .y_chroma_shift = 0,
344 .color_type = FF_COLOR_RGB,
345 .pixel_type = FF_PIXEL_PACKED,
347 .x_chroma_shift = 0, .y_chroma_shift = 0,
349 [PIX_FMT_RGB4_BYTE] = {
352 .color_type = FF_COLOR_RGB,
353 .pixel_type = FF_PIXEL_PACKED,
355 .x_chroma_shift = 0, .y_chroma_shift = 0,
360 .color_type = FF_COLOR_RGB,
361 .pixel_type = FF_PIXEL_PACKED,
363 .x_chroma_shift = 0, .y_chroma_shift = 0,
368 .color_type = FF_COLOR_RGB,
369 .pixel_type = FF_PIXEL_PACKED,
371 .x_chroma_shift = 0, .y_chroma_shift = 0,
373 [PIX_FMT_BGR4_BYTE] = {
376 .color_type = FF_COLOR_RGB,
377 .pixel_type = FF_PIXEL_PACKED,
379 .x_chroma_shift = 0, .y_chroma_shift = 0,
384 .color_type = FF_COLOR_YUV,
385 .pixel_type = FF_PIXEL_PLANAR,
387 .x_chroma_shift = 1, .y_chroma_shift = 1,
392 .color_type = FF_COLOR_YUV,
393 .pixel_type = FF_PIXEL_PLANAR,
395 .x_chroma_shift = 1, .y_chroma_shift = 1,
398 [PIX_FMT_BGR32_1] = {
400 .nb_channels = 4, .is_alpha = 1,
401 .color_type = FF_COLOR_RGB,
402 .pixel_type = FF_PIXEL_PACKED,
404 .x_chroma_shift = 0, .y_chroma_shift = 0,
406 [PIX_FMT_RGB32_1] = {
408 .nb_channels = 4, .is_alpha = 1,
409 .color_type = FF_COLOR_RGB,
410 .pixel_type = FF_PIXEL_PACKED,
412 .x_chroma_shift = 0, .y_chroma_shift = 0,
416 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
418 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
419 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
422 const char *avcodec_get_pix_fmt_name(int pix_fmt)
424 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
427 return pix_fmt_info[pix_fmt].name;
430 enum PixelFormat avcodec_get_pix_fmt(const char* name)
434 for (i=0; i < PIX_FMT_NB; i++)
435 if (!strcmp(pix_fmt_info[i].name, name))
440 void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
444 snprintf (buf, buf_size,
445 "name " " nb_channels" " depth" " is_alpha"
448 PixFmtInfo info= pix_fmt_info[pix_fmt];
450 char is_alpha_char= info.is_alpha ? 'y' : 'n';
452 snprintf (buf, buf_size,
453 "%-10s" " %1d " " %2d " " %c ",
462 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
465 for(i=0; i<256; i++){
479 case PIX_FMT_RGB4_BYTE:
484 case PIX_FMT_BGR4_BYTE:
495 pal[i] = b + (g<<8) + (r<<16);
501 int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
504 const PixFmtInfo *pinfo;
506 memset(picture->linesize, 0, sizeof(picture->linesize));
508 pinfo = &pix_fmt_info[pix_fmt];
510 case PIX_FMT_YUV420P:
511 case PIX_FMT_YUV422P:
512 case PIX_FMT_YUV444P:
513 case PIX_FMT_YUV410P:
514 case PIX_FMT_YUV411P:
515 case PIX_FMT_YUV440P:
516 case PIX_FMT_YUVJ420P:
517 case PIX_FMT_YUVJ422P:
518 case PIX_FMT_YUVJ444P:
519 case PIX_FMT_YUVJ440P:
520 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
521 picture->linesize[0] = width;
522 picture->linesize[1] = w2;
523 picture->linesize[2] = w2;
525 case PIX_FMT_YUVA420P:
526 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
527 picture->linesize[0] = width;
528 picture->linesize[1] = w2;
529 picture->linesize[2] = w2;
530 picture->linesize[3] = width;
534 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
535 picture->linesize[0] = width;
536 picture->linesize[1] = w2;
540 picture->linesize[0] = width * 3;
544 case PIX_FMT_RGB32_1:
545 case PIX_FMT_BGR32_1:
546 picture->linesize[0] = width * 4;
548 case PIX_FMT_RGB48BE:
549 case PIX_FMT_RGB48LE:
550 picture->linesize[0] = width * 6;
552 case PIX_FMT_GRAY16BE:
553 case PIX_FMT_GRAY16LE:
558 case PIX_FMT_YUYV422:
559 picture->linesize[0] = width * 2;
561 case PIX_FMT_UYVY422:
562 picture->linesize[0] = width * 2;
564 case PIX_FMT_UYYVYY411:
565 picture->linesize[0] = width + width/2;
569 picture->linesize[0] = width / 2;
571 case PIX_FMT_MONOWHITE:
572 case PIX_FMT_MONOBLACK:
573 picture->linesize[0] = (width + 7) >> 3;
578 case PIX_FMT_RGB4_BYTE:
579 case PIX_FMT_BGR4_BYTE:
581 picture->linesize[0] = width;
582 picture->linesize[1] = 4;
590 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
594 const PixFmtInfo *pinfo;
596 pinfo = &pix_fmt_info[pix_fmt];
597 size = picture->linesize[0] * height;
599 case PIX_FMT_YUV420P:
600 case PIX_FMT_YUV422P:
601 case PIX_FMT_YUV444P:
602 case PIX_FMT_YUV410P:
603 case PIX_FMT_YUV411P:
604 case PIX_FMT_YUV440P:
605 case PIX_FMT_YUVJ420P:
606 case PIX_FMT_YUVJ422P:
607 case PIX_FMT_YUVJ444P:
608 case PIX_FMT_YUVJ440P:
609 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
610 size2 = picture->linesize[1] * h2;
611 picture->data[0] = ptr;
612 picture->data[1] = picture->data[0] + size;
613 picture->data[2] = picture->data[1] + size2;
614 picture->data[3] = NULL;
615 return size + 2 * size2;
616 case PIX_FMT_YUVA420P:
617 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
618 size2 = picture->linesize[1] * h2;
619 picture->data[0] = ptr;
620 picture->data[1] = picture->data[0] + size;
621 picture->data[2] = picture->data[1] + size2;
622 picture->data[3] = picture->data[1] + size2 + size2;
623 return 2 * size + 2 * size2;
626 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
627 size2 = picture->linesize[1] * h2 * 2;
628 picture->data[0] = ptr;
629 picture->data[1] = picture->data[0] + size;
630 picture->data[2] = NULL;
631 picture->data[3] = NULL;
632 return size + 2 * size2;
637 case PIX_FMT_RGB32_1:
638 case PIX_FMT_BGR32_1:
639 case PIX_FMT_RGB48BE:
640 case PIX_FMT_RGB48LE:
641 case PIX_FMT_GRAY16BE:
642 case PIX_FMT_GRAY16LE:
647 case PIX_FMT_YUYV422:
648 case PIX_FMT_UYVY422:
649 case PIX_FMT_UYYVYY411:
652 case PIX_FMT_MONOWHITE:
653 case PIX_FMT_MONOBLACK:
654 picture->data[0] = ptr;
655 picture->data[1] = NULL;
656 picture->data[2] = NULL;
657 picture->data[3] = NULL;
662 case PIX_FMT_RGB4_BYTE:
663 case PIX_FMT_BGR4_BYTE:
665 size2 = (size + 3) & ~3;
666 picture->data[0] = ptr;
667 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
668 picture->data[2] = NULL;
669 picture->data[3] = NULL;
670 return size2 + 256 * 4;
672 picture->data[0] = NULL;
673 picture->data[1] = NULL;
674 picture->data[2] = NULL;
675 picture->data[3] = NULL;
680 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
681 int pix_fmt, int width, int height)
684 if(avcodec_check_dimensions(NULL, width, height))
687 if (ff_fill_linesize(picture, pix_fmt, width))
690 return ff_fill_pointer(picture, ptr, pix_fmt, height);
693 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
694 unsigned char *dest, int dest_size)
696 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
697 int i, j, w, h, data_planes;
698 const unsigned char* s;
699 int size = avpicture_get_size(pix_fmt, width, height);
701 if (size > dest_size || size < 0)
704 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
705 if (pix_fmt == PIX_FMT_YUYV422 ||
706 pix_fmt == PIX_FMT_UYVY422 ||
707 pix_fmt == PIX_FMT_BGR565 ||
708 pix_fmt == PIX_FMT_BGR555 ||
709 pix_fmt == PIX_FMT_RGB565 ||
710 pix_fmt == PIX_FMT_RGB555)
712 else if (pix_fmt == PIX_FMT_UYYVYY411)
714 else if (pix_fmt == PIX_FMT_PAL8)
717 w = width * (pf->depth * pf->nb_channels / 8);
722 data_planes = pf->nb_channels;
723 w = (width*pf->depth + 7)/8;
727 for (i=0; i<data_planes; i++) {
729 w = width >> pf->x_chroma_shift;
730 h = height >> pf->y_chroma_shift;
736 s += src->linesize[i];
740 if (pf->pixel_type == FF_PIXEL_PALETTE)
741 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
746 int avpicture_get_size(int pix_fmt, int width, int height)
748 AVPicture dummy_pict;
749 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
752 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
755 const PixFmtInfo *pf, *ps;
758 ps = &pix_fmt_info[src_pix_fmt];
759 pf = &pix_fmt_info[dst_pix_fmt];
763 pf = &pix_fmt_info[dst_pix_fmt];
764 if (pf->depth < ps->depth ||
765 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
766 loss |= FF_LOSS_DEPTH;
767 if (pf->x_chroma_shift > ps->x_chroma_shift ||
768 pf->y_chroma_shift > ps->y_chroma_shift)
769 loss |= FF_LOSS_RESOLUTION;
770 switch(pf->color_type) {
772 if (ps->color_type != FF_COLOR_RGB &&
773 ps->color_type != FF_COLOR_GRAY)
774 loss |= FF_LOSS_COLORSPACE;
777 if (ps->color_type != FF_COLOR_GRAY)
778 loss |= FF_LOSS_COLORSPACE;
781 if (ps->color_type != FF_COLOR_YUV)
782 loss |= FF_LOSS_COLORSPACE;
784 case FF_COLOR_YUV_JPEG:
785 if (ps->color_type != FF_COLOR_YUV_JPEG &&
786 ps->color_type != FF_COLOR_YUV &&
787 ps->color_type != FF_COLOR_GRAY)
788 loss |= FF_LOSS_COLORSPACE;
792 if (ps->color_type != pf->color_type)
793 loss |= FF_LOSS_COLORSPACE;
796 if (pf->color_type == FF_COLOR_GRAY &&
797 ps->color_type != FF_COLOR_GRAY)
798 loss |= FF_LOSS_CHROMA;
799 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
800 loss |= FF_LOSS_ALPHA;
801 if (pf->pixel_type == FF_PIXEL_PALETTE &&
802 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
803 loss |= FF_LOSS_COLORQUANT;
807 static int avg_bits_per_pixel(int pix_fmt)
810 const PixFmtInfo *pf;
812 pf = &pix_fmt_info[pix_fmt];
813 switch(pf->pixel_type) {
814 case FF_PIXEL_PACKED:
816 case PIX_FMT_YUYV422:
817 case PIX_FMT_UYVY422:
824 case PIX_FMT_UYYVYY411:
828 bits = pf->depth * pf->nb_channels;
832 case FF_PIXEL_PLANAR:
833 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
834 bits = pf->depth * pf->nb_channels;
836 bits = pf->depth + ((2 * pf->depth) >>
837 (pf->x_chroma_shift + pf->y_chroma_shift));
840 case FF_PIXEL_PALETTE:
850 static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
855 int dist, i, loss, min_dist, dst_pix_fmt;
857 /* find exact color match with smallest size */
859 min_dist = 0x7fffffff;
860 for(i = 0;i < PIX_FMT_NB; i++) {
861 if (pix_fmt_mask & (1ULL << i)) {
862 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
864 dist = avg_bits_per_pixel(i);
865 if (dist < min_dist) {
875 int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
876 int has_alpha, int *loss_ptr)
878 int dst_pix_fmt, loss_mask, i;
879 static const int loss_mask_order[] = {
880 ~0, /* no loss first */
883 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
889 /* try with successive loss */
892 loss_mask = loss_mask_order[i++];
893 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
894 has_alpha, loss_mask);
895 if (dst_pix_fmt >= 0)
903 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
907 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
908 const uint8_t *src, int src_wrap,
909 int width, int height)
913 for(;height > 0; height--) {
914 memcpy(dst, src, width);
920 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
923 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
925 pf = &pix_fmt_info[pix_fmt];
926 switch(pf->pixel_type) {
927 case FF_PIXEL_PACKED:
929 case PIX_FMT_YUYV422:
930 case PIX_FMT_UYVY422:
937 case PIX_FMT_UYYVYY411:
941 bits = pf->depth * pf->nb_channels;
944 return (width * bits + 7) >> 3;
946 case FF_PIXEL_PLANAR:
947 if (plane == 1 || plane == 2)
948 width= -((-width)>>pf->x_chroma_shift);
950 return (width * pf->depth + 7) >> 3;
952 case FF_PIXEL_PALETTE:
961 void av_picture_copy(AVPicture *dst, const AVPicture *src,
962 int pix_fmt, int width, int height)
965 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
967 pf = &pix_fmt_info[pix_fmt];
968 switch(pf->pixel_type) {
969 case FF_PIXEL_PACKED:
970 case FF_PIXEL_PLANAR:
971 for(i = 0; i < pf->nb_channels; i++) {
973 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
975 if (i == 1 || i == 2) {
976 h= -((-height)>>pf->y_chroma_shift);
978 ff_img_copy_plane(dst->data[i], dst->linesize[i],
979 src->data[i], src->linesize[i],
983 case FF_PIXEL_PALETTE:
984 ff_img_copy_plane(dst->data[0], dst->linesize[0],
985 src->data[0], src->linesize[0],
987 /* copy the palette */
988 ff_img_copy_plane(dst->data[1], dst->linesize[1],
989 src->data[1], src->linesize[1],
995 /* XXX: totally non optimized */
997 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
998 int width, int height)
1000 const uint8_t *p, *p1;
1001 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1005 lum1 = dst->data[0];
1009 for(;height >= 1; height -= 2) {
1014 for(w = width; w >= 2; w -= 2) {
1031 p1 += src->linesize[0];
1032 lum1 += dst->linesize[0];
1036 for(w = width; w >= 2; w -= 2) {
1045 p1 += src->linesize[0];
1046 lum1 += dst->linesize[0];
1048 cb1 += dst->linesize[1];
1049 cr1 += dst->linesize[2];
1053 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
1054 int width, int height)
1056 const uint8_t *p, *p1;
1057 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1062 lum1 = dst->data[0];
1066 for(;height >= 1; height -= 2) {
1071 for(w = width; w >= 2; w -= 2) {
1088 p1 += src->linesize[0];
1089 lum1 += dst->linesize[0];
1093 for(w = width; w >= 2; w -= 2) {
1102 p1 += src->linesize[0];
1103 lum1 += dst->linesize[0];
1105 cb1 += dst->linesize[1];
1106 cr1 += dst->linesize[2];
1111 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1112 int width, int height)
1114 const uint8_t *p, *p1;
1115 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1119 lum1 = dst->data[0];
1122 for(;height > 0; height--) {
1127 for(w = width; w >= 2; w -= 2) {
1137 p1 += src->linesize[0];
1138 lum1 += dst->linesize[0];
1139 cb1 += dst->linesize[1];
1140 cr1 += dst->linesize[2];
1145 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1146 int width, int height)
1148 const uint8_t *p, *p1;
1149 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1153 lum1 = dst->data[0];
1156 for(;height > 0; height--) {
1161 for(w = width; w >= 2; w -= 2) {
1171 p1 += src->linesize[0];
1172 lum1 += dst->linesize[0];
1173 cb1 += dst->linesize[1];
1174 cr1 += dst->linesize[2];
1178 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1179 int width, int height)
1182 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1186 lum1 = src->data[0];
1189 for(;height > 0; height--) {
1194 for(w = width; w >= 2; w -= 2) {
1204 p1 += dst->linesize[0];
1205 lum1 += src->linesize[0];
1206 cb1 += src->linesize[1];
1207 cr1 += src->linesize[2];
1211 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1212 int width, int height)
1215 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1219 lum1 = src->data[0];
1222 for(;height > 0; height--) {
1227 for(w = width; w >= 2; w -= 2) {
1237 p1 += dst->linesize[0];
1238 lum1 += src->linesize[0];
1239 cb1 += src->linesize[1];
1240 cr1 += src->linesize[2];
1244 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1245 int width, int height)
1247 const uint8_t *p, *p1;
1248 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1252 lum1 = dst->data[0];
1255 for(;height > 0; height--) {
1260 for(w = width; w >= 4; w -= 4) {
1272 p1 += src->linesize[0];
1273 lum1 += dst->linesize[0];
1274 cb1 += dst->linesize[1];
1275 cr1 += dst->linesize[2];
1280 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1281 int width, int height)
1284 uint8_t *line1, *line2, *linesrc = dst->data[0];
1285 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1286 uint8_t *cb1, *cb2 = src->data[1];
1287 uint8_t *cr1, *cr2 = src->data[2];
1289 for(h = height / 2; h--;) {
1291 line2 = linesrc + dst->linesize[0];
1294 lum2 = lumsrc + src->linesize[0];
1299 for(w = width / 2; w--;) {
1300 *line1++ = *lum1++; *line2++ = *lum2++;
1301 *line1++ = *line2++ = *cb1++;
1302 *line1++ = *lum1++; *line2++ = *lum2++;
1303 *line1++ = *line2++ = *cr1++;
1306 linesrc += dst->linesize[0] * 2;
1307 lumsrc += src->linesize[0] * 2;
1308 cb2 += src->linesize[1];
1309 cr2 += src->linesize[2];
1313 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1314 int width, int height)
1317 uint8_t *line1, *line2, *linesrc = dst->data[0];
1318 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1319 uint8_t *cb1, *cb2 = src->data[1];
1320 uint8_t *cr1, *cr2 = src->data[2];
1322 for(h = height / 2; h--;) {
1324 line2 = linesrc + dst->linesize[0];
1327 lum2 = lumsrc + src->linesize[0];
1332 for(w = width / 2; w--;) {
1333 *line1++ = *line2++ = *cb1++;
1334 *line1++ = *lum1++; *line2++ = *lum2++;
1335 *line1++ = *line2++ = *cr1++;
1336 *line1++ = *lum1++; *line2++ = *lum2++;
1339 linesrc += dst->linesize[0] * 2;
1340 lumsrc += src->linesize[0] * 2;
1341 cb2 += src->linesize[1];
1342 cr2 += src->linesize[2];
1347 void ff_shrink22(uint8_t *dst, int dst_wrap,
1348 const uint8_t *src, int src_wrap,
1349 int width, int height)
1352 const uint8_t *s1, *s2;
1355 for(;height > 0; height--) {
1359 for(w = width;w >= 4; w-=4) {
1360 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1361 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1362 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1363 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1369 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1374 src += 2 * src_wrap;
1380 void ff_shrink44(uint8_t *dst, int dst_wrap,
1381 const uint8_t *src, int src_wrap,
1382 int width, int height)
1385 const uint8_t *s1, *s2, *s3, *s4;
1388 for(;height > 0; height--) {
1394 for(w = width;w > 0; w--) {
1395 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1396 s2[0] + s2[1] + s2[2] + s2[3] +
1397 s3[0] + s3[1] + s3[2] + s3[3] +
1398 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1405 src += 4 * src_wrap;
1411 void ff_shrink88(uint8_t *dst, int dst_wrap,
1412 const uint8_t *src, int src_wrap,
1413 int width, int height)
1417 for(;height > 0; height--) {
1418 for(w = width;w > 0; w--) {
1421 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1424 *(dst++) = (tmp + 32)>>6;
1425 src += 8 - 8*src_wrap;
1427 src += 8*src_wrap - 8*width;
1428 dst += dst_wrap - width;
1432 /* XXX: add jpeg quantize code */
1434 #define TRANSP_INDEX (6*6*6)
1436 /* this is maybe slow, but allows for extensions */
1437 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1439 return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
1442 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1445 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1448 pal = (uint32_t *)palette;
1450 for(r = 0; r < 6; r++) {
1451 for(g = 0; g < 6; g++) {
1452 for(b = 0; b < 6; b++) {
1453 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1454 (pal_value[g] << 8) | pal_value[b];
1461 pal[i++] = 0xff000000;
1464 /* copy bit n to bits 0 ... n - 1 */
1465 static inline unsigned int bitcopy_n(unsigned int a, int n)
1468 mask = (1 << n) - 1;
1469 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1472 /* rgb555 handling */
1474 #define RGB_NAME rgb555
1476 #define RGB_IN(r, g, b, s)\
1478 unsigned int v = ((const uint16_t *)(s))[0];\
1479 r = bitcopy_n(v >> (10 - 3), 3);\
1480 g = bitcopy_n(v >> (5 - 3), 3);\
1481 b = bitcopy_n(v << 3, 3);\
1485 #define RGB_OUT(d, r, g, b)\
1487 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1492 #include "imgconvert_template.c"
1494 /* rgb565 handling */
1496 #define RGB_NAME rgb565
1498 #define RGB_IN(r, g, b, s)\
1500 unsigned int v = ((const uint16_t *)(s))[0];\
1501 r = bitcopy_n(v >> (11 - 3), 3);\
1502 g = bitcopy_n(v >> (5 - 2), 2);\
1503 b = bitcopy_n(v << 3, 3);\
1506 #define RGB_OUT(d, r, g, b)\
1508 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1513 #include "imgconvert_template.c"
1515 /* bgr24 handling */
1517 #define RGB_NAME bgr24
1519 #define RGB_IN(r, g, b, s)\
1526 #define RGB_OUT(d, r, g, b)\
1535 #include "imgconvert_template.c"
1541 /* rgb24 handling */
1543 #define RGB_NAME rgb24
1546 #define RGB_IN(r, g, b, s)\
1553 #define RGB_OUT(d, r, g, b)\
1562 #include "imgconvert_template.c"
1564 /* rgb32 handling */
1566 #define RGB_NAME rgb32
1569 #define RGB_IN(r, g, b, s)\
1571 unsigned int v = ((const uint32_t *)(s))[0];\
1572 r = (v >> 16) & 0xff;\
1573 g = (v >> 8) & 0xff;\
1577 #define RGBA_IN(r, g, b, a, s)\
1579 unsigned int v = ((const uint32_t *)(s))[0];\
1580 a = (v >> 24) & 0xff;\
1581 r = (v >> 16) & 0xff;\
1582 g = (v >> 8) & 0xff;\
1586 #define RGBA_OUT(d, r, g, b, a)\
1588 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1593 #include "imgconvert_template.c"
1595 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1596 int width, int height, int xor_mask)
1598 const unsigned char *p;
1600 int v, dst_wrap, src_wrap;
1604 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1607 dst_wrap = dst->linesize[0] - width;
1608 for(y=0;y<height;y++) {
1611 v = *p++ ^ xor_mask;
1613 q[1] = -((v >> 6) & 1);
1614 q[2] = -((v >> 5) & 1);
1615 q[3] = -((v >> 4) & 1);
1616 q[4] = -((v >> 3) & 1);
1617 q[5] = -((v >> 2) & 1);
1618 q[6] = -((v >> 1) & 1);
1619 q[7] = -((v >> 0) & 1);
1624 v = *p++ ^ xor_mask;
1626 q[0] = -((v >> 7) & 1);
1636 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1637 int width, int height)
1639 mono_to_gray(dst, src, width, height, 0xff);
1642 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1643 int width, int height)
1645 mono_to_gray(dst, src, width, height, 0x00);
1648 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1649 int width, int height, int xor_mask)
1654 int j, b, v, n1, src_wrap, dst_wrap, y;
1657 src_wrap = src->linesize[0] - width;
1660 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1662 for(y=0;y<height;y++) {
1669 v = (v << 1) | (b >> 7);
1671 d[0] = v ^ xor_mask;
1681 v = (v << 1) | (b >> 7);
1684 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1692 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1693 int width, int height)
1695 gray_to_mono(dst, src, width, height, 0xff);
1698 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1699 int width, int height)
1701 gray_to_mono(dst, src, width, height, 0x00);
1704 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1705 int width, int height)
1707 int x, y, src_wrap, dst_wrap;
1710 src_wrap = src->linesize[0] - width;
1712 dst_wrap = dst->linesize[0] - width * 2;
1713 for(y=0; y<height; y++){
1714 for(x=0; x<width; x++){
1723 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1724 int width, int height)
1726 int x, y, src_wrap, dst_wrap;
1729 src_wrap = src->linesize[0] - width * 2;
1731 dst_wrap = dst->linesize[0] - width;
1732 for(y=0; y<height; y++){
1733 for(x=0; x<width; x++){
1742 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1743 int width, int height)
1745 gray16_to_gray(dst, src, width, height);
1748 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1749 int width, int height)
1751 AVPicture tmpsrc = *src;
1753 gray16_to_gray(dst, &tmpsrc, width, height);
1756 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1757 int width, int height)
1759 int x, y, src_wrap, dst_wrap;
1761 s = (uint16_t*)src->data[0];
1762 src_wrap = (src->linesize[0] - width * 2)/2;
1763 d = (uint16_t*)dst->data[0];
1764 dst_wrap = (dst->linesize[0] - width * 2)/2;
1765 for(y=0; y<height; y++){
1766 for(x=0; x<width; x++){
1767 *d++ = bswap_16(*s++);
1775 typedef struct ConvertEntry {
1776 void (*convert)(AVPicture *dst,
1777 const AVPicture *src, int width, int height);
1780 /* Add each new conversion function in this table. In order to be able
1781 to convert from any format to any format, the following constraints
1784 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1786 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1788 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1790 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1793 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1795 The other conversion functions are just optimizations for common cases.
1797 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1798 [PIX_FMT_YUV420P] = {
1799 [PIX_FMT_YUYV422] = {
1800 .convert = yuv420p_to_yuyv422,
1802 [PIX_FMT_RGB555] = {
1803 .convert = yuv420p_to_rgb555
1805 [PIX_FMT_RGB565] = {
1806 .convert = yuv420p_to_rgb565
1809 .convert = yuv420p_to_bgr24
1812 .convert = yuv420p_to_rgb24
1815 .convert = yuv420p_to_rgb32
1817 [PIX_FMT_UYVY422] = {
1818 .convert = yuv420p_to_uyvy422,
1821 [PIX_FMT_YUV422P] = {
1822 [PIX_FMT_YUYV422] = {
1823 .convert = yuv422p_to_yuyv422,
1825 [PIX_FMT_UYVY422] = {
1826 .convert = yuv422p_to_uyvy422,
1829 [PIX_FMT_YUV444P] = {
1831 .convert = yuv444p_to_rgb24
1834 [PIX_FMT_YUVJ420P] = {
1835 [PIX_FMT_RGB555] = {
1836 .convert = yuvj420p_to_rgb555
1838 [PIX_FMT_RGB565] = {
1839 .convert = yuvj420p_to_rgb565
1842 .convert = yuvj420p_to_bgr24
1845 .convert = yuvj420p_to_rgb24
1848 .convert = yuvj420p_to_rgb32
1851 [PIX_FMT_YUVJ444P] = {
1853 .convert = yuvj444p_to_rgb24
1856 [PIX_FMT_YUYV422] = {
1857 [PIX_FMT_YUV420P] = {
1858 .convert = yuyv422_to_yuv420p,
1860 [PIX_FMT_YUV422P] = {
1861 .convert = yuyv422_to_yuv422p,
1864 [PIX_FMT_UYVY422] = {
1865 [PIX_FMT_YUV420P] = {
1866 .convert = uyvy422_to_yuv420p,
1868 [PIX_FMT_YUV422P] = {
1869 .convert = uyvy422_to_yuv422p,
1873 [PIX_FMT_YUV420P] = {
1874 .convert = rgb24_to_yuv420p
1876 [PIX_FMT_RGB565] = {
1877 .convert = rgb24_to_rgb565
1879 [PIX_FMT_RGB555] = {
1880 .convert = rgb24_to_rgb555
1883 .convert = rgb24_to_rgb32
1886 .convert = rgb24_to_bgr24
1889 .convert = rgb24_to_gray
1892 .convert = rgb24_to_pal8
1894 [PIX_FMT_YUV444P] = {
1895 .convert = rgb24_to_yuv444p
1897 [PIX_FMT_YUVJ420P] = {
1898 .convert = rgb24_to_yuvj420p
1900 [PIX_FMT_YUVJ444P] = {
1901 .convert = rgb24_to_yuvj444p
1906 .convert = rgb32_to_rgb24
1909 .convert = rgb32_to_bgr24
1911 [PIX_FMT_RGB565] = {
1912 .convert = rgb32_to_rgb565
1914 [PIX_FMT_RGB555] = {
1915 .convert = rgb32_to_rgb555
1918 .convert = rgb32_to_pal8
1920 [PIX_FMT_YUV420P] = {
1921 .convert = rgb32_to_yuv420p
1924 .convert = rgb32_to_gray
1929 .convert = bgr24_to_rgb32
1932 .convert = bgr24_to_rgb24
1934 [PIX_FMT_YUV420P] = {
1935 .convert = bgr24_to_yuv420p
1938 .convert = bgr24_to_gray
1941 [PIX_FMT_RGB555] = {
1943 .convert = rgb555_to_rgb24
1946 .convert = rgb555_to_rgb32
1948 [PIX_FMT_YUV420P] = {
1949 .convert = rgb555_to_yuv420p
1952 .convert = rgb555_to_gray
1955 [PIX_FMT_RGB565] = {
1957 .convert = rgb565_to_rgb32
1960 .convert = rgb565_to_rgb24
1962 [PIX_FMT_YUV420P] = {
1963 .convert = rgb565_to_yuv420p
1966 .convert = rgb565_to_gray
1969 [PIX_FMT_GRAY16BE] = {
1971 .convert = gray16be_to_gray
1973 [PIX_FMT_GRAY16LE] = {
1974 .convert = gray16_to_gray16
1977 [PIX_FMT_GRAY16LE] = {
1979 .convert = gray16le_to_gray
1981 [PIX_FMT_GRAY16BE] = {
1982 .convert = gray16_to_gray16
1986 [PIX_FMT_RGB555] = {
1987 .convert = gray_to_rgb555
1989 [PIX_FMT_RGB565] = {
1990 .convert = gray_to_rgb565
1993 .convert = gray_to_rgb24
1996 .convert = gray_to_bgr24
1999 .convert = gray_to_rgb32
2001 [PIX_FMT_MONOWHITE] = {
2002 .convert = gray_to_monowhite
2004 [PIX_FMT_MONOBLACK] = {
2005 .convert = gray_to_monoblack
2007 [PIX_FMT_GRAY16LE] = {
2008 .convert = gray_to_gray16
2010 [PIX_FMT_GRAY16BE] = {
2011 .convert = gray_to_gray16
2014 [PIX_FMT_MONOWHITE] = {
2016 .convert = monowhite_to_gray
2019 [PIX_FMT_MONOBLACK] = {
2021 .convert = monoblack_to_gray
2025 [PIX_FMT_RGB555] = {
2026 .convert = pal8_to_rgb555
2028 [PIX_FMT_RGB565] = {
2029 .convert = pal8_to_rgb565
2032 .convert = pal8_to_bgr24
2035 .convert = pal8_to_rgb24
2038 .convert = pal8_to_rgb32
2041 [PIX_FMT_UYYVYY411] = {
2042 [PIX_FMT_YUV411P] = {
2043 .convert = uyyvyy411_to_yuv411p,
2049 int avpicture_alloc(AVPicture *picture,
2050 int pix_fmt, int width, int height)
2055 size = avpicture_get_size(pix_fmt, width, height);
2058 ptr = av_malloc(size);
2061 avpicture_fill(picture, ptr, pix_fmt, width, height);
2062 if(picture->data[1] && !picture->data[2])
2063 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
2067 memset(picture, 0, sizeof(AVPicture));
2071 void avpicture_free(AVPicture *picture)
2073 av_free(picture->data[0]);
2076 /* return true if yuv planar */
2077 static inline int is_yuv_planar(const PixFmtInfo *ps)
2079 return (ps->color_type == FF_COLOR_YUV ||
2080 ps->color_type == FF_COLOR_YUV_JPEG) &&
2081 ps->pixel_type == FF_PIXEL_PLANAR;
2084 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2085 int pix_fmt, int top_band, int left_band)
2090 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2093 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2094 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2096 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2097 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2098 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2100 dst->linesize[0] = src->linesize[0];
2101 dst->linesize[1] = src->linesize[1];
2102 dst->linesize[2] = src->linesize[2];
2106 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2107 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2116 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2117 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2119 for (i = 0; i < 3; i++) {
2120 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2121 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2123 if (padtop || padleft) {
2124 memset(dst->data[i], color[i],
2125 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2128 if (padleft || padright) {
2129 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2130 (dst->linesize[i] - (padright >> x_shift));
2131 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2132 for (y = 0; y < yheight; y++) {
2133 memset(optr, color[i], (padleft + padright) >> x_shift);
2134 optr += dst->linesize[i];
2138 if (src) { /* first line */
2139 uint8_t *iptr = src->data[i];
2140 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2141 (padleft >> x_shift);
2142 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
2143 iptr += src->linesize[i];
2144 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2145 (dst->linesize[i] - (padright >> x_shift));
2146 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2147 for (y = 0; y < yheight; y++) {
2148 memset(optr, color[i], (padleft + padright) >> x_shift);
2149 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2150 (width - padleft - padright) >> x_shift);
2151 iptr += src->linesize[i];
2152 optr += dst->linesize[i];
2156 if (padbottom || padright) {
2157 optr = dst->data[i] + dst->linesize[i] *
2158 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2159 memset(optr, color[i],dst->linesize[i] *
2160 (padbottom >> y_shift) + (padright >> x_shift));
2167 static uint8_t y_ccir_to_jpeg[256];
2168 static uint8_t y_jpeg_to_ccir[256];
2169 static uint8_t c_ccir_to_jpeg[256];
2170 static uint8_t c_jpeg_to_ccir[256];
2172 /* init various conversion tables */
2173 static av_cold void img_convert_init(void)
2176 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2178 for(i = 0;i < 256; i++) {
2179 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
2180 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
2181 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
2182 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
2186 /* apply to each pixel the given table */
2187 static void img_apply_table(uint8_t *dst, int dst_wrap,
2188 const uint8_t *src, int src_wrap,
2189 int width, int height, const uint8_t *table1)
2194 const uint8_t *table;
2197 for(;height > 0; height--) {
2221 /* XXX: use generic filter ? */
2222 /* XXX: in most cases, the sampling position is incorrect */
2225 static void shrink41(uint8_t *dst, int dst_wrap,
2226 const uint8_t *src, int src_wrap,
2227 int width, int height)
2233 for(;height > 0; height--) {
2236 for(w = width;w > 0; w--) {
2237 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
2247 static void shrink21(uint8_t *dst, int dst_wrap,
2248 const uint8_t *src, int src_wrap,
2249 int width, int height)
2255 for(;height > 0; height--) {
2258 for(w = width;w > 0; w--) {
2259 d[0] = (s[0] + s[1]) >> 1;
2269 static void shrink12(uint8_t *dst, int dst_wrap,
2270 const uint8_t *src, int src_wrap,
2271 int width, int height)
2275 const uint8_t *s1, *s2;
2277 for(;height > 0; height--) {
2281 for(w = width;w >= 4; w-=4) {
2282 d[0] = (s1[0] + s2[0]) >> 1;
2283 d[1] = (s1[1] + s2[1]) >> 1;
2284 d[2] = (s1[2] + s2[2]) >> 1;
2285 d[3] = (s1[3] + s2[3]) >> 1;
2291 d[0] = (s1[0] + s2[0]) >> 1;
2296 src += 2 * src_wrap;
2301 static void grow21_line(uint8_t *dst, const uint8_t *src,
2310 for(w = width;w >= 4; w-=4) {
2311 d[1] = d[0] = s1[0];
2312 d[3] = d[2] = s1[1];
2316 for(;w >= 2; w -= 2) {
2317 d[1] = d[0] = s1[0];
2321 /* only needed if width is not a multiple of two */
2322 /* XXX: veryfy that */
2328 static void grow41_line(uint8_t *dst, const uint8_t *src,
2337 for(w = width;w >= 4; w-=4) {
2349 static void grow21(uint8_t *dst, int dst_wrap,
2350 const uint8_t *src, int src_wrap,
2351 int width, int height)
2353 for(;height > 0; height--) {
2354 grow21_line(dst, src, width);
2361 static void grow12(uint8_t *dst, int dst_wrap,
2362 const uint8_t *src, int src_wrap,
2363 int width, int height)
2365 for(;height > 0; height-=2) {
2366 memcpy(dst, src, width);
2368 memcpy(dst, src, width);
2375 static void grow22(uint8_t *dst, int dst_wrap,
2376 const uint8_t *src, int src_wrap,
2377 int width, int height)
2379 for(;height > 0; height--) {
2380 grow21_line(dst, src, width);
2388 static void grow41(uint8_t *dst, int dst_wrap,
2389 const uint8_t *src, int src_wrap,
2390 int width, int height)
2392 for(;height > 0; height--) {
2393 grow41_line(dst, src, width);
2400 static void grow44(uint8_t *dst, int dst_wrap,
2401 const uint8_t *src, int src_wrap,
2402 int width, int height)
2404 for(;height > 0; height--) {
2405 grow41_line(dst, src, width);
2406 if ((height & 3) == 1)
2413 static void conv411(uint8_t *dst, int dst_wrap,
2414 const uint8_t *src, int src_wrap,
2415 int width, int height)
2418 const uint8_t *s1, *s2;
2423 for(;height > 0; height--) {
2425 s2 = src + src_wrap;
2427 for(w = width;w > 0; w--) {
2428 c = (s1[0] + s2[0]) >> 1;
2435 src += src_wrap * 2;
2440 /* XXX: always use linesize. Return -1 if not supported */
2441 int img_convert(AVPicture *dst, int dst_pix_fmt,
2442 const AVPicture *src, int src_pix_fmt,
2443 int src_width, int src_height)
2445 static int initialized;
2446 int i, ret, dst_width, dst_height, int_pix_fmt;
2447 const PixFmtInfo *src_pix, *dst_pix;
2448 const ConvertEntry *ce;
2449 AVPicture tmp1, *tmp = &tmp1;
2451 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2452 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2454 if (src_width <= 0 || src_height <= 0)
2462 dst_width = src_width;
2463 dst_height = src_height;
2465 dst_pix = &pix_fmt_info[dst_pix_fmt];
2466 src_pix = &pix_fmt_info[src_pix_fmt];
2467 if (src_pix_fmt == dst_pix_fmt) {
2468 /* no conversion needed: just copy */
2469 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2473 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2475 /* specific conversion routine */
2476 ce->convert(dst, src, dst_width, dst_height);
2481 if (is_yuv_planar(dst_pix) &&
2482 src_pix_fmt == PIX_FMT_GRAY8) {
2486 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2487 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2488 src->data[0], src->linesize[0],
2489 dst_width, dst_height);
2491 img_apply_table(dst->data[0], dst->linesize[0],
2492 src->data[0], src->linesize[0],
2493 dst_width, dst_height,
2496 /* fill U and V with 128 */
2499 w >>= dst_pix->x_chroma_shift;
2500 h >>= dst_pix->y_chroma_shift;
2501 for(i = 1; i <= 2; i++) {
2503 for(y = 0; y< h; y++) {
2505 d += dst->linesize[i];
2512 if (is_yuv_planar(src_pix) &&
2513 dst_pix_fmt == PIX_FMT_GRAY8) {
2514 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2515 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2516 src->data[0], src->linesize[0],
2517 dst_width, dst_height);
2519 img_apply_table(dst->data[0], dst->linesize[0],
2520 src->data[0], src->linesize[0],
2521 dst_width, dst_height,
2527 /* YUV to YUV planar */
2528 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2529 int x_shift, y_shift, w, h, xy_shift;
2530 void (*resize_func)(uint8_t *dst, int dst_wrap,
2531 const uint8_t *src, int src_wrap,
2532 int width, int height);
2534 /* compute chroma size of the smallest dimensions */
2537 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2538 w >>= dst_pix->x_chroma_shift;
2540 w >>= src_pix->x_chroma_shift;
2541 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2542 h >>= dst_pix->y_chroma_shift;
2544 h >>= src_pix->y_chroma_shift;
2546 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2547 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2548 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2549 /* there must be filters for conversion at least from and to
2553 resize_func = ff_img_copy_plane;
2556 resize_func = shrink21;
2559 resize_func = shrink41;
2562 resize_func = shrink12;
2565 resize_func = ff_shrink22;
2568 resize_func = ff_shrink44;
2571 resize_func = grow21;
2574 resize_func = grow12;
2577 resize_func = grow41;
2580 resize_func = grow22;
2583 resize_func = grow44;
2586 resize_func = conv411;
2589 /* currently not handled */
2590 goto no_chroma_filter;
2593 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2594 src->data[0], src->linesize[0],
2595 dst_width, dst_height);
2597 for(i = 1;i <= 2; i++)
2598 resize_func(dst->data[i], dst->linesize[i],
2599 src->data[i], src->linesize[i],
2600 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2601 /* if yuv color space conversion is needed, we do it here on
2602 the destination image */
2603 if (dst_pix->color_type != src_pix->color_type) {
2604 const uint8_t *y_table, *c_table;
2605 if (dst_pix->color_type == FF_COLOR_YUV) {
2606 y_table = y_jpeg_to_ccir;
2607 c_table = c_jpeg_to_ccir;
2609 y_table = y_ccir_to_jpeg;
2610 c_table = c_ccir_to_jpeg;
2612 img_apply_table(dst->data[0], dst->linesize[0],
2613 dst->data[0], dst->linesize[0],
2614 dst_width, dst_height,
2617 for(i = 1;i <= 2; i++)
2618 img_apply_table(dst->data[i], dst->linesize[i],
2619 dst->data[i], dst->linesize[i],
2620 dst_width>>dst_pix->x_chroma_shift,
2621 dst_height>>dst_pix->y_chroma_shift,
2628 /* try to use an intermediate format */
2629 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2630 dst_pix_fmt == PIX_FMT_YUYV422) {
2631 /* specific case: convert to YUV422P first */
2632 int_pix_fmt = PIX_FMT_YUV422P;
2633 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2634 dst_pix_fmt == PIX_FMT_UYVY422) {
2635 /* specific case: convert to YUV422P first */
2636 int_pix_fmt = PIX_FMT_YUV422P;
2637 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2638 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2639 /* specific case: convert to YUV411P first */
2640 int_pix_fmt = PIX_FMT_YUV411P;
2641 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2642 src_pix_fmt != PIX_FMT_GRAY8) ||
2643 (dst_pix->color_type == FF_COLOR_GRAY &&
2644 dst_pix_fmt != PIX_FMT_GRAY8)) {
2645 /* gray8 is the normalized format */
2646 int_pix_fmt = PIX_FMT_GRAY8;
2647 } else if ((is_yuv_planar(src_pix) &&
2648 src_pix_fmt != PIX_FMT_YUV444P &&
2649 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2650 /* yuv444 is the normalized format */
2651 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2652 int_pix_fmt = PIX_FMT_YUVJ444P;
2654 int_pix_fmt = PIX_FMT_YUV444P;
2655 } else if ((is_yuv_planar(dst_pix) &&
2656 dst_pix_fmt != PIX_FMT_YUV444P &&
2657 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2658 /* yuv444 is the normalized format */
2659 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2660 int_pix_fmt = PIX_FMT_YUVJ444P;
2662 int_pix_fmt = PIX_FMT_YUV444P;
2664 /* the two formats are rgb or gray8 or yuv[j]444p */
2665 if (src_pix->is_alpha && dst_pix->is_alpha)
2666 int_pix_fmt = PIX_FMT_RGB32;
2668 int_pix_fmt = PIX_FMT_RGB24;
2670 if (src_pix_fmt == int_pix_fmt)
2672 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2675 if (img_convert(tmp, int_pix_fmt,
2676 src, src_pix_fmt, src_width, src_height) < 0)
2678 if (img_convert(dst, dst_pix_fmt,
2679 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2683 avpicture_free(tmp);
2688 /* NOTE: we scan all the pixels to have an exact information */
2689 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2691 const unsigned char *p;
2692 int src_wrap, ret, x, y;
2694 uint32_t *palette = (uint32_t *)src->data[1];
2697 src_wrap = src->linesize[0] - width;
2699 for(y=0;y<height;y++) {
2700 for(x=0;x<width;x++) {
2701 a = palette[p[0]] >> 24;
2703 ret |= FF_ALPHA_TRANSP;
2704 } else if (a != 0xff) {
2705 ret |= FF_ALPHA_SEMI_TRANSP;
2714 int img_get_alpha_info(const AVPicture *src,
2715 int pix_fmt, int width, int height)
2717 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2720 pf = &pix_fmt_info[pix_fmt];
2721 /* no alpha can be represented in format */
2726 ret = get_alpha_info_rgb32(src, width, height);
2729 ret = get_alpha_info_pal8(src, width, height);
2732 /* we do not know, so everything is indicated */
2733 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2740 #define DEINT_INPLACE_LINE_LUM \
2741 movd_m2r(lum_m4[0],mm0);\
2742 movd_m2r(lum_m3[0],mm1);\
2743 movd_m2r(lum_m2[0],mm2);\
2744 movd_m2r(lum_m1[0],mm3);\
2745 movd_m2r(lum[0],mm4);\
2746 punpcklbw_r2r(mm7,mm0);\
2747 movd_r2m(mm2,lum_m4[0]);\
2748 punpcklbw_r2r(mm7,mm1);\
2749 punpcklbw_r2r(mm7,mm2);\
2750 punpcklbw_r2r(mm7,mm3);\
2751 punpcklbw_r2r(mm7,mm4);\
2752 paddw_r2r(mm3,mm1);\
2754 paddw_r2r(mm4,mm0);\
2756 paddw_r2r(mm6,mm2);\
2757 paddw_r2r(mm2,mm1);\
2758 psubusw_r2r(mm0,mm1);\
2760 packuswb_r2r(mm7,mm1);\
2761 movd_r2m(mm1,lum_m2[0]);
2763 #define DEINT_LINE_LUM \
2764 movd_m2r(lum_m4[0],mm0);\
2765 movd_m2r(lum_m3[0],mm1);\
2766 movd_m2r(lum_m2[0],mm2);\
2767 movd_m2r(lum_m1[0],mm3);\
2768 movd_m2r(lum[0],mm4);\
2769 punpcklbw_r2r(mm7,mm0);\
2770 punpcklbw_r2r(mm7,mm1);\
2771 punpcklbw_r2r(mm7,mm2);\
2772 punpcklbw_r2r(mm7,mm3);\
2773 punpcklbw_r2r(mm7,mm4);\
2774 paddw_r2r(mm3,mm1);\
2776 paddw_r2r(mm4,mm0);\
2778 paddw_r2r(mm6,mm2);\
2779 paddw_r2r(mm2,mm1);\
2780 psubusw_r2r(mm0,mm1);\
2782 packuswb_r2r(mm7,mm1);\
2783 movd_r2m(mm1,dst[0]);
2786 /* filter parameters: [-1 4 2 4 -1] // 8 */
2787 static void deinterlace_line(uint8_t *dst,
2788 const uint8_t *lum_m4, const uint8_t *lum_m3,
2789 const uint8_t *lum_m2, const uint8_t *lum_m1,
2794 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2797 for(;size > 0;size--) {
2799 sum += lum_m3[0] << 2;
2800 sum += lum_m2[0] << 1;
2801 sum += lum_m1[0] << 2;
2803 dst[0] = cm[(sum + 4) >> 3];
2815 movq_m2r(ff_pw_4,mm6);
2817 for (;size > 3; size-=4) {
2828 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2832 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2835 for(;size > 0;size--) {
2837 sum += lum_m3[0] << 2;
2838 sum += lum_m2[0] << 1;
2839 lum_m4[0]=lum_m2[0];
2840 sum += lum_m1[0] << 2;
2842 lum_m2[0] = cm[(sum + 4) >> 3];
2853 movq_m2r(ff_pw_4,mm6);
2855 for (;size > 3; size-=4) {
2856 DEINT_INPLACE_LINE_LUM
2866 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2867 top field is copied as is, but the bottom field is deinterlaced
2868 against the top field. */
2869 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2870 const uint8_t *src1, int src_wrap,
2871 int width, int height)
2873 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2878 src_0=&src_m1[src_wrap];
2879 src_p1=&src_0[src_wrap];
2880 src_p2=&src_p1[src_wrap];
2881 for(y=0;y<(height-2);y+=2) {
2882 memcpy(dst,src_m1,width);
2884 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2888 src_p1 += 2*src_wrap;
2889 src_p2 += 2*src_wrap;
2892 memcpy(dst,src_m1,width);
2895 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2898 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2899 int width, int height)
2901 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2904 buf = (uint8_t*)av_malloc(width);
2907 memcpy(buf,src_m1,width);
2908 src_0=&src_m1[src_wrap];
2909 src_p1=&src_0[src_wrap];
2910 src_p2=&src_p1[src_wrap];
2911 for(y=0;y<(height-2);y+=2) {
2912 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2915 src_p1 += 2*src_wrap;
2916 src_p2 += 2*src_wrap;
2919 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2923 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2924 int pix_fmt, int width, int height)
2928 if (pix_fmt != PIX_FMT_YUV420P &&
2929 pix_fmt != PIX_FMT_YUV422P &&
2930 pix_fmt != PIX_FMT_YUV444P &&
2931 pix_fmt != PIX_FMT_YUV411P &&
2932 pix_fmt != PIX_FMT_GRAY8)
2934 if ((width & 3) != 0 || (height & 3) != 0)
2940 case PIX_FMT_YUV420P:
2944 case PIX_FMT_YUV422P:
2947 case PIX_FMT_YUV411P:
2953 if (pix_fmt == PIX_FMT_GRAY8) {
2958 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2961 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2962 src->data[i], src->linesize[i],