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
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,
205 .color_type = FF_COLOR_RGB,
206 .pixel_type = FF_PIXEL_PACKED,
208 .x_chroma_shift = 0, .y_chroma_shift = 0,
213 .color_type = FF_COLOR_RGB,
214 .pixel_type = FF_PIXEL_PACKED,
216 .x_chroma_shift = 0, .y_chroma_shift = 0,
219 /* gray / mono formats */
220 [PIX_FMT_GRAY16BE] = {
223 .color_type = FF_COLOR_GRAY,
224 .pixel_type = FF_PIXEL_PLANAR,
227 [PIX_FMT_GRAY16LE] = {
230 .color_type = FF_COLOR_GRAY,
231 .pixel_type = FF_PIXEL_PLANAR,
237 .color_type = FF_COLOR_GRAY,
238 .pixel_type = FF_PIXEL_PLANAR,
241 [PIX_FMT_MONOWHITE] = {
244 .color_type = FF_COLOR_GRAY,
245 .pixel_type = FF_PIXEL_PLANAR,
248 [PIX_FMT_MONOBLACK] = {
251 .color_type = FF_COLOR_GRAY,
252 .pixel_type = FF_PIXEL_PLANAR,
256 /* paletted formats */
259 .nb_channels = 4, .is_alpha = 1,
260 .color_type = FF_COLOR_RGB,
261 .pixel_type = FF_PIXEL_PALETTE,
264 [PIX_FMT_XVMC_MPEG2_MC] = {
267 [PIX_FMT_XVMC_MPEG2_IDCT] = {
270 [PIX_FMT_VDPAU_MPEG1] = {
271 .name = "vdpau_mpeg1",
273 [PIX_FMT_VDPAU_MPEG2] = {
274 .name = "vdpau_mpeg2",
276 [PIX_FMT_VDPAU_H264] = {
277 .name = "vdpau_h264",
279 [PIX_FMT_UYYVYY411] = {
282 .color_type = FF_COLOR_YUV,
283 .pixel_type = FF_PIXEL_PACKED,
285 .x_chroma_shift = 2, .y_chroma_shift = 0,
289 .nb_channels = 4, .is_alpha = 1,
290 .color_type = FF_COLOR_RGB,
291 .pixel_type = FF_PIXEL_PACKED,
293 .x_chroma_shift = 0, .y_chroma_shift = 0,
298 .color_type = FF_COLOR_RGB,
299 .pixel_type = FF_PIXEL_PACKED,
301 .x_chroma_shift = 0, .y_chroma_shift = 0,
306 .color_type = FF_COLOR_RGB,
307 .pixel_type = FF_PIXEL_PACKED,
309 .x_chroma_shift = 0, .y_chroma_shift = 0,
314 .color_type = FF_COLOR_RGB,
315 .pixel_type = FF_PIXEL_PACKED,
317 .x_chroma_shift = 0, .y_chroma_shift = 0,
322 .color_type = FF_COLOR_RGB,
323 .pixel_type = FF_PIXEL_PACKED,
325 .x_chroma_shift = 0, .y_chroma_shift = 0,
327 [PIX_FMT_RGB4_BYTE] = {
330 .color_type = FF_COLOR_RGB,
331 .pixel_type = FF_PIXEL_PACKED,
333 .x_chroma_shift = 0, .y_chroma_shift = 0,
338 .color_type = FF_COLOR_RGB,
339 .pixel_type = FF_PIXEL_PACKED,
341 .x_chroma_shift = 0, .y_chroma_shift = 0,
346 .color_type = FF_COLOR_RGB,
347 .pixel_type = FF_PIXEL_PACKED,
349 .x_chroma_shift = 0, .y_chroma_shift = 0,
351 [PIX_FMT_BGR4_BYTE] = {
354 .color_type = FF_COLOR_RGB,
355 .pixel_type = FF_PIXEL_PACKED,
357 .x_chroma_shift = 0, .y_chroma_shift = 0,
362 .color_type = FF_COLOR_YUV,
363 .pixel_type = FF_PIXEL_PLANAR,
365 .x_chroma_shift = 1, .y_chroma_shift = 1,
370 .color_type = FF_COLOR_YUV,
371 .pixel_type = FF_PIXEL_PLANAR,
373 .x_chroma_shift = 1, .y_chroma_shift = 1,
376 [PIX_FMT_BGR32_1] = {
378 .nb_channels = 4, .is_alpha = 1,
379 .color_type = FF_COLOR_RGB,
380 .pixel_type = FF_PIXEL_PACKED,
382 .x_chroma_shift = 0, .y_chroma_shift = 0,
384 [PIX_FMT_RGB32_1] = {
386 .nb_channels = 4, .is_alpha = 1,
387 .color_type = FF_COLOR_RGB,
388 .pixel_type = FF_PIXEL_PACKED,
390 .x_chroma_shift = 0, .y_chroma_shift = 0,
394 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
396 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
397 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
400 const char *avcodec_get_pix_fmt_name(int pix_fmt)
402 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
405 return pix_fmt_info[pix_fmt].name;
408 enum PixelFormat avcodec_get_pix_fmt(const char* name)
412 for (i=0; i < PIX_FMT_NB; i++)
413 if (!strcmp(pix_fmt_info[i].name, name))
418 void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
422 snprintf (buf, buf_size,
423 "name " " nb_channels" " depth" " is_alpha"
426 PixFmtInfo info= pix_fmt_info[pix_fmt];
428 char is_alpha_char= info.is_alpha ? 'y' : 'n';
430 snprintf (buf, buf_size,
431 "%-10s" " %1d " " %2d " " %c ",
440 int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
443 const PixFmtInfo *pinfo;
445 memset(picture->linesize, 0, sizeof(picture->linesize));
447 pinfo = &pix_fmt_info[pix_fmt];
449 case PIX_FMT_YUV420P:
450 case PIX_FMT_YUV422P:
451 case PIX_FMT_YUV444P:
452 case PIX_FMT_YUV410P:
453 case PIX_FMT_YUV411P:
454 case PIX_FMT_YUV440P:
455 case PIX_FMT_YUVJ420P:
456 case PIX_FMT_YUVJ422P:
457 case PIX_FMT_YUVJ444P:
458 case PIX_FMT_YUVJ440P:
459 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
460 picture->linesize[0] = width;
461 picture->linesize[1] = w2;
462 picture->linesize[2] = w2;
464 case PIX_FMT_YUVA420P:
465 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
466 picture->linesize[0] = width;
467 picture->linesize[1] = w2;
468 picture->linesize[2] = w2;
469 picture->linesize[3] = width;
473 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
474 picture->linesize[0] = width;
475 picture->linesize[1] = w2;
479 picture->linesize[0] = width * 3;
483 case PIX_FMT_RGB32_1:
484 case PIX_FMT_BGR32_1:
485 picture->linesize[0] = width * 4;
487 case PIX_FMT_GRAY16BE:
488 case PIX_FMT_GRAY16LE:
493 case PIX_FMT_YUYV422:
494 picture->linesize[0] = width * 2;
496 case PIX_FMT_UYVY422:
497 picture->linesize[0] = width * 2;
499 case PIX_FMT_UYYVYY411:
500 picture->linesize[0] = width + width/2;
504 case PIX_FMT_RGB4_BYTE:
505 case PIX_FMT_BGR4_BYTE:
507 picture->linesize[0] = width;
511 picture->linesize[0] = width / 2;
513 case PIX_FMT_MONOWHITE:
514 case PIX_FMT_MONOBLACK:
515 picture->linesize[0] = (width + 7) >> 3;
518 picture->linesize[0] = width;
519 picture->linesize[1] = 4;
527 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
531 const PixFmtInfo *pinfo;
533 pinfo = &pix_fmt_info[pix_fmt];
534 size = picture->linesize[0] * height;
536 case PIX_FMT_YUV420P:
537 case PIX_FMT_YUV422P:
538 case PIX_FMT_YUV444P:
539 case PIX_FMT_YUV410P:
540 case PIX_FMT_YUV411P:
541 case PIX_FMT_YUV440P:
542 case PIX_FMT_YUVJ420P:
543 case PIX_FMT_YUVJ422P:
544 case PIX_FMT_YUVJ444P:
545 case PIX_FMT_YUVJ440P:
546 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
547 size2 = picture->linesize[1] * h2;
548 picture->data[0] = ptr;
549 picture->data[1] = picture->data[0] + size;
550 picture->data[2] = picture->data[1] + size2;
551 picture->data[3] = NULL;
552 return size + 2 * size2;
553 case PIX_FMT_YUVA420P:
554 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
555 size2 = picture->linesize[1] * h2;
556 picture->data[0] = ptr;
557 picture->data[1] = picture->data[0] + size;
558 picture->data[2] = picture->data[1] + size2;
559 picture->data[3] = picture->data[1] + size2 + size2;
560 return 2 * size + 2 * size2;
563 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
564 size2 = picture->linesize[1] * h2 * 2;
565 picture->data[0] = ptr;
566 picture->data[1] = picture->data[0] + size;
567 picture->data[2] = NULL;
568 picture->data[3] = NULL;
569 return size + 2 * size2;
574 case PIX_FMT_RGB32_1:
575 case PIX_FMT_BGR32_1:
576 case PIX_FMT_GRAY16BE:
577 case PIX_FMT_GRAY16LE:
582 case PIX_FMT_YUYV422:
583 case PIX_FMT_UYVY422:
584 case PIX_FMT_UYYVYY411:
587 case PIX_FMT_RGB4_BYTE:
588 case PIX_FMT_BGR4_BYTE:
592 case PIX_FMT_MONOWHITE:
593 case PIX_FMT_MONOBLACK:
594 picture->data[0] = ptr;
595 picture->data[1] = NULL;
596 picture->data[2] = NULL;
597 picture->data[3] = NULL;
600 size2 = (size + 3) & ~3;
601 picture->data[0] = ptr;
602 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
603 picture->data[2] = NULL;
604 picture->data[3] = NULL;
605 return size2 + 256 * 4;
607 picture->data[0] = NULL;
608 picture->data[1] = NULL;
609 picture->data[2] = NULL;
610 picture->data[3] = NULL;
615 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
616 int pix_fmt, int width, int height)
619 if(avcodec_check_dimensions(NULL, width, height))
622 if (ff_fill_linesize(picture, pix_fmt, width))
625 return ff_fill_pointer(picture, ptr, pix_fmt, height);
628 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
629 unsigned char *dest, int dest_size)
631 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
632 int i, j, w, h, data_planes;
633 const unsigned char* s;
634 int size = avpicture_get_size(pix_fmt, width, height);
636 if (size > dest_size || size < 0)
639 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
640 if (pix_fmt == PIX_FMT_YUYV422 ||
641 pix_fmt == PIX_FMT_UYVY422 ||
642 pix_fmt == PIX_FMT_BGR565 ||
643 pix_fmt == PIX_FMT_BGR555 ||
644 pix_fmt == PIX_FMT_RGB565 ||
645 pix_fmt == PIX_FMT_RGB555)
647 else if (pix_fmt == PIX_FMT_UYYVYY411)
649 else if (pix_fmt == PIX_FMT_PAL8)
652 w = width * (pf->depth * pf->nb_channels / 8);
657 data_planes = pf->nb_channels;
658 w = (width*pf->depth + 7)/8;
662 for (i=0; i<data_planes; i++) {
664 w = width >> pf->x_chroma_shift;
665 h = height >> pf->y_chroma_shift;
671 s += src->linesize[i];
675 if (pf->pixel_type == FF_PIXEL_PALETTE)
676 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
681 int avpicture_get_size(int pix_fmt, int width, int height)
683 AVPicture dummy_pict;
684 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
687 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
690 const PixFmtInfo *pf, *ps;
693 ps = &pix_fmt_info[src_pix_fmt];
694 pf = &pix_fmt_info[dst_pix_fmt];
698 pf = &pix_fmt_info[dst_pix_fmt];
699 if (pf->depth < ps->depth ||
700 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
701 loss |= FF_LOSS_DEPTH;
702 if (pf->x_chroma_shift > ps->x_chroma_shift ||
703 pf->y_chroma_shift > ps->y_chroma_shift)
704 loss |= FF_LOSS_RESOLUTION;
705 switch(pf->color_type) {
707 if (ps->color_type != FF_COLOR_RGB &&
708 ps->color_type != FF_COLOR_GRAY)
709 loss |= FF_LOSS_COLORSPACE;
712 if (ps->color_type != FF_COLOR_GRAY)
713 loss |= FF_LOSS_COLORSPACE;
716 if (ps->color_type != FF_COLOR_YUV)
717 loss |= FF_LOSS_COLORSPACE;
719 case FF_COLOR_YUV_JPEG:
720 if (ps->color_type != FF_COLOR_YUV_JPEG &&
721 ps->color_type != FF_COLOR_YUV &&
722 ps->color_type != FF_COLOR_GRAY)
723 loss |= FF_LOSS_COLORSPACE;
727 if (ps->color_type != pf->color_type)
728 loss |= FF_LOSS_COLORSPACE;
731 if (pf->color_type == FF_COLOR_GRAY &&
732 ps->color_type != FF_COLOR_GRAY)
733 loss |= FF_LOSS_CHROMA;
734 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
735 loss |= FF_LOSS_ALPHA;
736 if (pf->pixel_type == FF_PIXEL_PALETTE &&
737 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
738 loss |= FF_LOSS_COLORQUANT;
742 static int avg_bits_per_pixel(int pix_fmt)
745 const PixFmtInfo *pf;
747 pf = &pix_fmt_info[pix_fmt];
748 switch(pf->pixel_type) {
749 case FF_PIXEL_PACKED:
751 case PIX_FMT_YUYV422:
752 case PIX_FMT_UYVY422:
759 case PIX_FMT_UYYVYY411:
763 bits = pf->depth * pf->nb_channels;
767 case FF_PIXEL_PLANAR:
768 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
769 bits = pf->depth * pf->nb_channels;
771 bits = pf->depth + ((2 * pf->depth) >>
772 (pf->x_chroma_shift + pf->y_chroma_shift));
775 case FF_PIXEL_PALETTE:
785 static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
790 int dist, i, loss, min_dist, dst_pix_fmt;
792 /* find exact color match with smallest size */
794 min_dist = 0x7fffffff;
795 for(i = 0;i < PIX_FMT_NB; i++) {
796 if (pix_fmt_mask & (1ULL << i)) {
797 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
799 dist = avg_bits_per_pixel(i);
800 if (dist < min_dist) {
810 int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
811 int has_alpha, int *loss_ptr)
813 int dst_pix_fmt, loss_mask, i;
814 static const int loss_mask_order[] = {
815 ~0, /* no loss first */
818 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
824 /* try with successive loss */
827 loss_mask = loss_mask_order[i++];
828 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
829 has_alpha, loss_mask);
830 if (dst_pix_fmt >= 0)
838 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
842 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
843 const uint8_t *src, int src_wrap,
844 int width, int height)
848 for(;height > 0; height--) {
849 memcpy(dst, src, width);
855 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
858 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
860 pf = &pix_fmt_info[pix_fmt];
861 switch(pf->pixel_type) {
862 case FF_PIXEL_PACKED:
864 case PIX_FMT_YUYV422:
865 case PIX_FMT_UYVY422:
872 case PIX_FMT_UYYVYY411:
876 bits = pf->depth * pf->nb_channels;
879 return (width * bits + 7) >> 3;
881 case FF_PIXEL_PLANAR:
882 if (plane == 1 || plane == 2)
883 width= -((-width)>>pf->x_chroma_shift);
885 return (width * pf->depth + 7) >> 3;
887 case FF_PIXEL_PALETTE:
896 void av_picture_copy(AVPicture *dst, const AVPicture *src,
897 int pix_fmt, int width, int height)
900 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
902 pf = &pix_fmt_info[pix_fmt];
903 switch(pf->pixel_type) {
904 case FF_PIXEL_PACKED:
905 case FF_PIXEL_PLANAR:
906 for(i = 0; i < pf->nb_channels; i++) {
908 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
910 if (i == 1 || i == 2) {
911 h= -((-height)>>pf->y_chroma_shift);
913 ff_img_copy_plane(dst->data[i], dst->linesize[i],
914 src->data[i], src->linesize[i],
918 case FF_PIXEL_PALETTE:
919 ff_img_copy_plane(dst->data[0], dst->linesize[0],
920 src->data[0], src->linesize[0],
922 /* copy the palette */
923 ff_img_copy_plane(dst->data[1], dst->linesize[1],
924 src->data[1], src->linesize[1],
930 /* XXX: totally non optimized */
932 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
933 int width, int height)
935 const uint8_t *p, *p1;
936 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
944 for(;height >= 1; height -= 2) {
949 for(w = width; w >= 2; w -= 2) {
966 p1 += src->linesize[0];
967 lum1 += dst->linesize[0];
971 for(w = width; w >= 2; w -= 2) {
980 p1 += src->linesize[0];
981 lum1 += dst->linesize[0];
983 cb1 += dst->linesize[1];
984 cr1 += dst->linesize[2];
988 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
989 int width, int height)
991 const uint8_t *p, *p1;
992 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1001 for(;height >= 1; height -= 2) {
1006 for(w = width; w >= 2; w -= 2) {
1023 p1 += src->linesize[0];
1024 lum1 += dst->linesize[0];
1028 for(w = width; w >= 2; w -= 2) {
1037 p1 += src->linesize[0];
1038 lum1 += dst->linesize[0];
1040 cb1 += dst->linesize[1];
1041 cr1 += dst->linesize[2];
1046 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1047 int width, int height)
1049 const uint8_t *p, *p1;
1050 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1054 lum1 = dst->data[0];
1057 for(;height > 0; height--) {
1062 for(w = width; w >= 2; w -= 2) {
1072 p1 += src->linesize[0];
1073 lum1 += dst->linesize[0];
1074 cb1 += dst->linesize[1];
1075 cr1 += dst->linesize[2];
1080 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1081 int width, int height)
1083 const uint8_t *p, *p1;
1084 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1088 lum1 = dst->data[0];
1091 for(;height > 0; height--) {
1096 for(w = width; w >= 2; w -= 2) {
1106 p1 += src->linesize[0];
1107 lum1 += dst->linesize[0];
1108 cb1 += dst->linesize[1];
1109 cr1 += dst->linesize[2];
1113 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1114 int width, int height)
1117 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1121 lum1 = src->data[0];
1124 for(;height > 0; height--) {
1129 for(w = width; w >= 2; w -= 2) {
1139 p1 += dst->linesize[0];
1140 lum1 += src->linesize[0];
1141 cb1 += src->linesize[1];
1142 cr1 += src->linesize[2];
1146 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1147 int width, int height)
1150 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1154 lum1 = src->data[0];
1157 for(;height > 0; height--) {
1162 for(w = width; w >= 2; w -= 2) {
1172 p1 += dst->linesize[0];
1173 lum1 += src->linesize[0];
1174 cb1 += src->linesize[1];
1175 cr1 += src->linesize[2];
1179 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1180 int width, int height)
1182 const uint8_t *p, *p1;
1183 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1187 lum1 = dst->data[0];
1190 for(;height > 0; height--) {
1195 for(w = width; w >= 4; w -= 4) {
1207 p1 += src->linesize[0];
1208 lum1 += dst->linesize[0];
1209 cb1 += dst->linesize[1];
1210 cr1 += dst->linesize[2];
1215 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1216 int width, int height)
1219 uint8_t *line1, *line2, *linesrc = dst->data[0];
1220 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1221 uint8_t *cb1, *cb2 = src->data[1];
1222 uint8_t *cr1, *cr2 = src->data[2];
1224 for(h = height / 2; h--;) {
1226 line2 = linesrc + dst->linesize[0];
1229 lum2 = lumsrc + src->linesize[0];
1234 for(w = width / 2; w--;) {
1235 *line1++ = *lum1++; *line2++ = *lum2++;
1236 *line1++ = *line2++ = *cb1++;
1237 *line1++ = *lum1++; *line2++ = *lum2++;
1238 *line1++ = *line2++ = *cr1++;
1241 linesrc += dst->linesize[0] * 2;
1242 lumsrc += src->linesize[0] * 2;
1243 cb2 += src->linesize[1];
1244 cr2 += src->linesize[2];
1248 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1249 int width, int height)
1252 uint8_t *line1, *line2, *linesrc = dst->data[0];
1253 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1254 uint8_t *cb1, *cb2 = src->data[1];
1255 uint8_t *cr1, *cr2 = src->data[2];
1257 for(h = height / 2; h--;) {
1259 line2 = linesrc + dst->linesize[0];
1262 lum2 = lumsrc + src->linesize[0];
1267 for(w = width / 2; w--;) {
1268 *line1++ = *line2++ = *cb1++;
1269 *line1++ = *lum1++; *line2++ = *lum2++;
1270 *line1++ = *line2++ = *cr1++;
1271 *line1++ = *lum1++; *line2++ = *lum2++;
1274 linesrc += dst->linesize[0] * 2;
1275 lumsrc += src->linesize[0] * 2;
1276 cb2 += src->linesize[1];
1277 cr2 += src->linesize[2];
1282 void ff_shrink22(uint8_t *dst, int dst_wrap,
1283 const uint8_t *src, int src_wrap,
1284 int width, int height)
1287 const uint8_t *s1, *s2;
1290 for(;height > 0; height--) {
1294 for(w = width;w >= 4; w-=4) {
1295 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1296 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1297 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1298 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1304 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1309 src += 2 * src_wrap;
1315 void ff_shrink44(uint8_t *dst, int dst_wrap,
1316 const uint8_t *src, int src_wrap,
1317 int width, int height)
1320 const uint8_t *s1, *s2, *s3, *s4;
1323 for(;height > 0; height--) {
1329 for(w = width;w > 0; w--) {
1330 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1331 s2[0] + s2[1] + s2[2] + s2[3] +
1332 s3[0] + s3[1] + s3[2] + s3[3] +
1333 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1340 src += 4 * src_wrap;
1346 void ff_shrink88(uint8_t *dst, int dst_wrap,
1347 const uint8_t *src, int src_wrap,
1348 int width, int height)
1352 for(;height > 0; height--) {
1353 for(w = width;w > 0; w--) {
1356 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1359 *(dst++) = (tmp + 32)>>6;
1360 src += 8 - 8*src_wrap;
1362 src += 8*src_wrap - 8*width;
1363 dst += dst_wrap - width;
1367 /* XXX: add jpeg quantize code */
1369 #define TRANSP_INDEX (6*6*6)
1371 /* this is maybe slow, but allows for extensions */
1372 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1374 return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
1377 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1380 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1383 pal = (uint32_t *)palette;
1385 for(r = 0; r < 6; r++) {
1386 for(g = 0; g < 6; g++) {
1387 for(b = 0; b < 6; b++) {
1388 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1389 (pal_value[g] << 8) | pal_value[b];
1396 pal[i++] = 0xff000000;
1399 /* copy bit n to bits 0 ... n - 1 */
1400 static inline unsigned int bitcopy_n(unsigned int a, int n)
1403 mask = (1 << n) - 1;
1404 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1407 /* rgb555 handling */
1409 #define RGB_NAME rgb555
1411 #define RGB_IN(r, g, b, s)\
1413 unsigned int v = ((const uint16_t *)(s))[0];\
1414 r = bitcopy_n(v >> (10 - 3), 3);\
1415 g = bitcopy_n(v >> (5 - 3), 3);\
1416 b = bitcopy_n(v << 3, 3);\
1420 #define RGB_OUT(d, r, g, b)\
1422 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1427 #include "imgconvert_template.c"
1429 /* rgb565 handling */
1431 #define RGB_NAME rgb565
1433 #define RGB_IN(r, g, b, s)\
1435 unsigned int v = ((const uint16_t *)(s))[0];\
1436 r = bitcopy_n(v >> (11 - 3), 3);\
1437 g = bitcopy_n(v >> (5 - 2), 2);\
1438 b = bitcopy_n(v << 3, 3);\
1441 #define RGB_OUT(d, r, g, b)\
1443 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1448 #include "imgconvert_template.c"
1450 /* bgr24 handling */
1452 #define RGB_NAME bgr24
1454 #define RGB_IN(r, g, b, s)\
1461 #define RGB_OUT(d, r, g, b)\
1470 #include "imgconvert_template.c"
1476 /* rgb24 handling */
1478 #define RGB_NAME rgb24
1481 #define RGB_IN(r, g, b, s)\
1488 #define RGB_OUT(d, r, g, b)\
1497 #include "imgconvert_template.c"
1499 /* rgb32 handling */
1501 #define RGB_NAME rgb32
1504 #define RGB_IN(r, g, b, s)\
1506 unsigned int v = ((const uint32_t *)(s))[0];\
1507 r = (v >> 16) & 0xff;\
1508 g = (v >> 8) & 0xff;\
1512 #define RGBA_IN(r, g, b, a, s)\
1514 unsigned int v = ((const uint32_t *)(s))[0];\
1515 a = (v >> 24) & 0xff;\
1516 r = (v >> 16) & 0xff;\
1517 g = (v >> 8) & 0xff;\
1521 #define RGBA_OUT(d, r, g, b, a)\
1523 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1528 #include "imgconvert_template.c"
1530 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1531 int width, int height, int xor_mask)
1533 const unsigned char *p;
1535 int v, dst_wrap, src_wrap;
1539 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1542 dst_wrap = dst->linesize[0] - width;
1543 for(y=0;y<height;y++) {
1546 v = *p++ ^ xor_mask;
1548 q[1] = -((v >> 6) & 1);
1549 q[2] = -((v >> 5) & 1);
1550 q[3] = -((v >> 4) & 1);
1551 q[4] = -((v >> 3) & 1);
1552 q[5] = -((v >> 2) & 1);
1553 q[6] = -((v >> 1) & 1);
1554 q[7] = -((v >> 0) & 1);
1559 v = *p++ ^ xor_mask;
1561 q[0] = -((v >> 7) & 1);
1571 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1572 int width, int height)
1574 mono_to_gray(dst, src, width, height, 0xff);
1577 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1578 int width, int height)
1580 mono_to_gray(dst, src, width, height, 0x00);
1583 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1584 int width, int height, int xor_mask)
1589 int j, b, v, n1, src_wrap, dst_wrap, y;
1592 src_wrap = src->linesize[0] - width;
1595 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1597 for(y=0;y<height;y++) {
1604 v = (v << 1) | (b >> 7);
1606 d[0] = v ^ xor_mask;
1616 v = (v << 1) | (b >> 7);
1619 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1627 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1628 int width, int height)
1630 gray_to_mono(dst, src, width, height, 0xff);
1633 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1634 int width, int height)
1636 gray_to_mono(dst, src, width, height, 0x00);
1639 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1640 int width, int height)
1642 int x, y, src_wrap, dst_wrap;
1645 src_wrap = src->linesize[0] - width;
1647 dst_wrap = dst->linesize[0] - width * 2;
1648 for(y=0; y<height; y++){
1649 for(x=0; x<width; x++){
1658 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1659 int width, int height)
1661 int x, y, src_wrap, dst_wrap;
1664 src_wrap = src->linesize[0] - width * 2;
1666 dst_wrap = dst->linesize[0] - width;
1667 for(y=0; y<height; y++){
1668 for(x=0; x<width; x++){
1677 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1678 int width, int height)
1680 gray16_to_gray(dst, src, width, height);
1683 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1684 int width, int height)
1686 AVPicture tmpsrc = *src;
1688 gray16_to_gray(dst, &tmpsrc, width, height);
1691 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1692 int width, int height)
1694 int x, y, src_wrap, dst_wrap;
1696 s = (uint16_t*)src->data[0];
1697 src_wrap = (src->linesize[0] - width * 2)/2;
1698 d = (uint16_t*)dst->data[0];
1699 dst_wrap = (dst->linesize[0] - width * 2)/2;
1700 for(y=0; y<height; y++){
1701 for(x=0; x<width; x++){
1702 *d++ = bswap_16(*s++);
1710 typedef struct ConvertEntry {
1711 void (*convert)(AVPicture *dst,
1712 const AVPicture *src, int width, int height);
1715 /* Add each new conversion function in this table. In order to be able
1716 to convert from any format to any format, the following constraints
1719 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1721 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1723 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1725 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1728 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1730 The other conversion functions are just optimizations for common cases.
1732 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1733 [PIX_FMT_YUV420P] = {
1734 [PIX_FMT_YUYV422] = {
1735 .convert = yuv420p_to_yuyv422,
1737 [PIX_FMT_RGB555] = {
1738 .convert = yuv420p_to_rgb555
1740 [PIX_FMT_RGB565] = {
1741 .convert = yuv420p_to_rgb565
1744 .convert = yuv420p_to_bgr24
1747 .convert = yuv420p_to_rgb24
1750 .convert = yuv420p_to_rgb32
1752 [PIX_FMT_UYVY422] = {
1753 .convert = yuv420p_to_uyvy422,
1756 [PIX_FMT_YUV422P] = {
1757 [PIX_FMT_YUYV422] = {
1758 .convert = yuv422p_to_yuyv422,
1760 [PIX_FMT_UYVY422] = {
1761 .convert = yuv422p_to_uyvy422,
1764 [PIX_FMT_YUV444P] = {
1766 .convert = yuv444p_to_rgb24
1769 [PIX_FMT_YUVJ420P] = {
1770 [PIX_FMT_RGB555] = {
1771 .convert = yuvj420p_to_rgb555
1773 [PIX_FMT_RGB565] = {
1774 .convert = yuvj420p_to_rgb565
1777 .convert = yuvj420p_to_bgr24
1780 .convert = yuvj420p_to_rgb24
1783 .convert = yuvj420p_to_rgb32
1786 [PIX_FMT_YUVJ444P] = {
1788 .convert = yuvj444p_to_rgb24
1791 [PIX_FMT_YUYV422] = {
1792 [PIX_FMT_YUV420P] = {
1793 .convert = yuyv422_to_yuv420p,
1795 [PIX_FMT_YUV422P] = {
1796 .convert = yuyv422_to_yuv422p,
1799 [PIX_FMT_UYVY422] = {
1800 [PIX_FMT_YUV420P] = {
1801 .convert = uyvy422_to_yuv420p,
1803 [PIX_FMT_YUV422P] = {
1804 .convert = uyvy422_to_yuv422p,
1808 [PIX_FMT_YUV420P] = {
1809 .convert = rgb24_to_yuv420p
1811 [PIX_FMT_RGB565] = {
1812 .convert = rgb24_to_rgb565
1814 [PIX_FMT_RGB555] = {
1815 .convert = rgb24_to_rgb555
1818 .convert = rgb24_to_rgb32
1821 .convert = rgb24_to_bgr24
1824 .convert = rgb24_to_gray
1827 .convert = rgb24_to_pal8
1829 [PIX_FMT_YUV444P] = {
1830 .convert = rgb24_to_yuv444p
1832 [PIX_FMT_YUVJ420P] = {
1833 .convert = rgb24_to_yuvj420p
1835 [PIX_FMT_YUVJ444P] = {
1836 .convert = rgb24_to_yuvj444p
1841 .convert = rgb32_to_rgb24
1844 .convert = rgb32_to_bgr24
1846 [PIX_FMT_RGB565] = {
1847 .convert = rgb32_to_rgb565
1849 [PIX_FMT_RGB555] = {
1850 .convert = rgb32_to_rgb555
1853 .convert = rgb32_to_pal8
1855 [PIX_FMT_YUV420P] = {
1856 .convert = rgb32_to_yuv420p
1859 .convert = rgb32_to_gray
1864 .convert = bgr24_to_rgb32
1867 .convert = bgr24_to_rgb24
1869 [PIX_FMT_YUV420P] = {
1870 .convert = bgr24_to_yuv420p
1873 .convert = bgr24_to_gray
1876 [PIX_FMT_RGB555] = {
1878 .convert = rgb555_to_rgb24
1881 .convert = rgb555_to_rgb32
1883 [PIX_FMT_YUV420P] = {
1884 .convert = rgb555_to_yuv420p
1887 .convert = rgb555_to_gray
1890 [PIX_FMT_RGB565] = {
1892 .convert = rgb565_to_rgb32
1895 .convert = rgb565_to_rgb24
1897 [PIX_FMT_YUV420P] = {
1898 .convert = rgb565_to_yuv420p
1901 .convert = rgb565_to_gray
1904 [PIX_FMT_GRAY16BE] = {
1906 .convert = gray16be_to_gray
1908 [PIX_FMT_GRAY16LE] = {
1909 .convert = gray16_to_gray16
1912 [PIX_FMT_GRAY16LE] = {
1914 .convert = gray16le_to_gray
1916 [PIX_FMT_GRAY16BE] = {
1917 .convert = gray16_to_gray16
1921 [PIX_FMT_RGB555] = {
1922 .convert = gray_to_rgb555
1924 [PIX_FMT_RGB565] = {
1925 .convert = gray_to_rgb565
1928 .convert = gray_to_rgb24
1931 .convert = gray_to_bgr24
1934 .convert = gray_to_rgb32
1936 [PIX_FMT_MONOWHITE] = {
1937 .convert = gray_to_monowhite
1939 [PIX_FMT_MONOBLACK] = {
1940 .convert = gray_to_monoblack
1942 [PIX_FMT_GRAY16LE] = {
1943 .convert = gray_to_gray16
1945 [PIX_FMT_GRAY16BE] = {
1946 .convert = gray_to_gray16
1949 [PIX_FMT_MONOWHITE] = {
1951 .convert = monowhite_to_gray
1954 [PIX_FMT_MONOBLACK] = {
1956 .convert = monoblack_to_gray
1960 [PIX_FMT_RGB555] = {
1961 .convert = pal8_to_rgb555
1963 [PIX_FMT_RGB565] = {
1964 .convert = pal8_to_rgb565
1967 .convert = pal8_to_bgr24
1970 .convert = pal8_to_rgb24
1973 .convert = pal8_to_rgb32
1976 [PIX_FMT_UYYVYY411] = {
1977 [PIX_FMT_YUV411P] = {
1978 .convert = uyyvyy411_to_yuv411p,
1984 int avpicture_alloc(AVPicture *picture,
1985 int pix_fmt, int width, int height)
1990 size = avpicture_get_size(pix_fmt, width, height);
1993 ptr = av_malloc(size);
1996 avpicture_fill(picture, ptr, pix_fmt, width, height);
1999 memset(picture, 0, sizeof(AVPicture));
2003 void avpicture_free(AVPicture *picture)
2005 av_free(picture->data[0]);
2008 /* return true if yuv planar */
2009 static inline int is_yuv_planar(const PixFmtInfo *ps)
2011 return (ps->color_type == FF_COLOR_YUV ||
2012 ps->color_type == FF_COLOR_YUV_JPEG) &&
2013 ps->pixel_type == FF_PIXEL_PLANAR;
2016 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2017 int pix_fmt, int top_band, int left_band)
2022 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2025 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2026 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2028 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2029 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2030 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2032 dst->linesize[0] = src->linesize[0];
2033 dst->linesize[1] = src->linesize[1];
2034 dst->linesize[2] = src->linesize[2];
2038 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2039 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2048 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2049 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2051 for (i = 0; i < 3; i++) {
2052 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2053 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2055 if (padtop || padleft) {
2056 memset(dst->data[i], color[i],
2057 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2060 if (padleft || padright) {
2061 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2062 (dst->linesize[i] - (padright >> x_shift));
2063 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2064 for (y = 0; y < yheight; y++) {
2065 memset(optr, color[i], (padleft + padright) >> x_shift);
2066 optr += dst->linesize[i];
2070 if (src) { /* first line */
2071 uint8_t *iptr = src->data[i];
2072 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2073 (padleft >> x_shift);
2074 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
2075 iptr += src->linesize[i];
2076 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2077 (dst->linesize[i] - (padright >> x_shift));
2078 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2079 for (y = 0; y < yheight; y++) {
2080 memset(optr, color[i], (padleft + padright) >> x_shift);
2081 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2082 (width - padleft - padright) >> x_shift);
2083 iptr += src->linesize[i];
2084 optr += dst->linesize[i];
2088 if (padbottom || padright) {
2089 optr = dst->data[i] + dst->linesize[i] *
2090 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2091 memset(optr, color[i],dst->linesize[i] *
2092 (padbottom >> y_shift) + (padright >> x_shift));
2099 static uint8_t y_ccir_to_jpeg[256];
2100 static uint8_t y_jpeg_to_ccir[256];
2101 static uint8_t c_ccir_to_jpeg[256];
2102 static uint8_t c_jpeg_to_ccir[256];
2104 /* init various conversion tables */
2105 static void img_convert_init(void)
2108 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2110 for(i = 0;i < 256; i++) {
2111 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
2112 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
2113 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
2114 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
2118 /* apply to each pixel the given table */
2119 static void img_apply_table(uint8_t *dst, int dst_wrap,
2120 const uint8_t *src, int src_wrap,
2121 int width, int height, const uint8_t *table1)
2126 const uint8_t *table;
2129 for(;height > 0; height--) {
2153 /* XXX: use generic filter ? */
2154 /* XXX: in most cases, the sampling position is incorrect */
2157 static void shrink41(uint8_t *dst, int dst_wrap,
2158 const uint8_t *src, int src_wrap,
2159 int width, int height)
2165 for(;height > 0; height--) {
2168 for(w = width;w > 0; w--) {
2169 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
2179 static void shrink21(uint8_t *dst, int dst_wrap,
2180 const uint8_t *src, int src_wrap,
2181 int width, int height)
2187 for(;height > 0; height--) {
2190 for(w = width;w > 0; w--) {
2191 d[0] = (s[0] + s[1]) >> 1;
2201 static void shrink12(uint8_t *dst, int dst_wrap,
2202 const uint8_t *src, int src_wrap,
2203 int width, int height)
2207 const uint8_t *s1, *s2;
2209 for(;height > 0; height--) {
2213 for(w = width;w >= 4; w-=4) {
2214 d[0] = (s1[0] + s2[0]) >> 1;
2215 d[1] = (s1[1] + s2[1]) >> 1;
2216 d[2] = (s1[2] + s2[2]) >> 1;
2217 d[3] = (s1[3] + s2[3]) >> 1;
2223 d[0] = (s1[0] + s2[0]) >> 1;
2228 src += 2 * src_wrap;
2233 static void grow21_line(uint8_t *dst, const uint8_t *src,
2242 for(w = width;w >= 4; w-=4) {
2243 d[1] = d[0] = s1[0];
2244 d[3] = d[2] = s1[1];
2248 for(;w >= 2; w -= 2) {
2249 d[1] = d[0] = s1[0];
2253 /* only needed if width is not a multiple of two */
2254 /* XXX: veryfy that */
2260 static void grow41_line(uint8_t *dst, const uint8_t *src,
2269 for(w = width;w >= 4; w-=4) {
2281 static void grow21(uint8_t *dst, int dst_wrap,
2282 const uint8_t *src, int src_wrap,
2283 int width, int height)
2285 for(;height > 0; height--) {
2286 grow21_line(dst, src, width);
2293 static void grow12(uint8_t *dst, int dst_wrap,
2294 const uint8_t *src, int src_wrap,
2295 int width, int height)
2297 for(;height > 0; height-=2) {
2298 memcpy(dst, src, width);
2300 memcpy(dst, src, width);
2307 static void grow22(uint8_t *dst, int dst_wrap,
2308 const uint8_t *src, int src_wrap,
2309 int width, int height)
2311 for(;height > 0; height--) {
2312 grow21_line(dst, src, width);
2320 static void grow41(uint8_t *dst, int dst_wrap,
2321 const uint8_t *src, int src_wrap,
2322 int width, int height)
2324 for(;height > 0; height--) {
2325 grow41_line(dst, src, width);
2332 static void grow44(uint8_t *dst, int dst_wrap,
2333 const uint8_t *src, int src_wrap,
2334 int width, int height)
2336 for(;height > 0; height--) {
2337 grow41_line(dst, src, width);
2338 if ((height & 3) == 1)
2345 static void conv411(uint8_t *dst, int dst_wrap,
2346 const uint8_t *src, int src_wrap,
2347 int width, int height)
2350 const uint8_t *s1, *s2;
2355 for(;height > 0; height--) {
2357 s2 = src + src_wrap;
2359 for(w = width;w > 0; w--) {
2360 c = (s1[0] + s2[0]) >> 1;
2367 src += src_wrap * 2;
2372 /* XXX: always use linesize. Return -1 if not supported */
2373 int img_convert(AVPicture *dst, int dst_pix_fmt,
2374 const AVPicture *src, int src_pix_fmt,
2375 int src_width, int src_height)
2377 static int initialized;
2378 int i, ret, dst_width, dst_height, int_pix_fmt;
2379 const PixFmtInfo *src_pix, *dst_pix;
2380 const ConvertEntry *ce;
2381 AVPicture tmp1, *tmp = &tmp1;
2383 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2384 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2386 if (src_width <= 0 || src_height <= 0)
2394 dst_width = src_width;
2395 dst_height = src_height;
2397 dst_pix = &pix_fmt_info[dst_pix_fmt];
2398 src_pix = &pix_fmt_info[src_pix_fmt];
2399 if (src_pix_fmt == dst_pix_fmt) {
2400 /* no conversion needed: just copy */
2401 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2405 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2407 /* specific conversion routine */
2408 ce->convert(dst, src, dst_width, dst_height);
2413 if (is_yuv_planar(dst_pix) &&
2414 src_pix_fmt == PIX_FMT_GRAY8) {
2418 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2419 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2420 src->data[0], src->linesize[0],
2421 dst_width, dst_height);
2423 img_apply_table(dst->data[0], dst->linesize[0],
2424 src->data[0], src->linesize[0],
2425 dst_width, dst_height,
2428 /* fill U and V with 128 */
2431 w >>= dst_pix->x_chroma_shift;
2432 h >>= dst_pix->y_chroma_shift;
2433 for(i = 1; i <= 2; i++) {
2435 for(y = 0; y< h; y++) {
2437 d += dst->linesize[i];
2444 if (is_yuv_planar(src_pix) &&
2445 dst_pix_fmt == PIX_FMT_GRAY8) {
2446 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2447 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2448 src->data[0], src->linesize[0],
2449 dst_width, dst_height);
2451 img_apply_table(dst->data[0], dst->linesize[0],
2452 src->data[0], src->linesize[0],
2453 dst_width, dst_height,
2459 /* YUV to YUV planar */
2460 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2461 int x_shift, y_shift, w, h, xy_shift;
2462 void (*resize_func)(uint8_t *dst, int dst_wrap,
2463 const uint8_t *src, int src_wrap,
2464 int width, int height);
2466 /* compute chroma size of the smallest dimensions */
2469 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2470 w >>= dst_pix->x_chroma_shift;
2472 w >>= src_pix->x_chroma_shift;
2473 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2474 h >>= dst_pix->y_chroma_shift;
2476 h >>= src_pix->y_chroma_shift;
2478 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2479 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2480 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2481 /* there must be filters for conversion at least from and to
2485 resize_func = ff_img_copy_plane;
2488 resize_func = shrink21;
2491 resize_func = shrink41;
2494 resize_func = shrink12;
2497 resize_func = ff_shrink22;
2500 resize_func = ff_shrink44;
2503 resize_func = grow21;
2506 resize_func = grow12;
2509 resize_func = grow41;
2512 resize_func = grow22;
2515 resize_func = grow44;
2518 resize_func = conv411;
2521 /* currently not handled */
2522 goto no_chroma_filter;
2525 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2526 src->data[0], src->linesize[0],
2527 dst_width, dst_height);
2529 for(i = 1;i <= 2; i++)
2530 resize_func(dst->data[i], dst->linesize[i],
2531 src->data[i], src->linesize[i],
2532 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2533 /* if yuv color space conversion is needed, we do it here on
2534 the destination image */
2535 if (dst_pix->color_type != src_pix->color_type) {
2536 const uint8_t *y_table, *c_table;
2537 if (dst_pix->color_type == FF_COLOR_YUV) {
2538 y_table = y_jpeg_to_ccir;
2539 c_table = c_jpeg_to_ccir;
2541 y_table = y_ccir_to_jpeg;
2542 c_table = c_ccir_to_jpeg;
2544 img_apply_table(dst->data[0], dst->linesize[0],
2545 dst->data[0], dst->linesize[0],
2546 dst_width, dst_height,
2549 for(i = 1;i <= 2; i++)
2550 img_apply_table(dst->data[i], dst->linesize[i],
2551 dst->data[i], dst->linesize[i],
2552 dst_width>>dst_pix->x_chroma_shift,
2553 dst_height>>dst_pix->y_chroma_shift,
2560 /* try to use an intermediate format */
2561 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2562 dst_pix_fmt == PIX_FMT_YUYV422) {
2563 /* specific case: convert to YUV422P first */
2564 int_pix_fmt = PIX_FMT_YUV422P;
2565 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2566 dst_pix_fmt == PIX_FMT_UYVY422) {
2567 /* specific case: convert to YUV422P first */
2568 int_pix_fmt = PIX_FMT_YUV422P;
2569 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2570 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2571 /* specific case: convert to YUV411P first */
2572 int_pix_fmt = PIX_FMT_YUV411P;
2573 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2574 src_pix_fmt != PIX_FMT_GRAY8) ||
2575 (dst_pix->color_type == FF_COLOR_GRAY &&
2576 dst_pix_fmt != PIX_FMT_GRAY8)) {
2577 /* gray8 is the normalized format */
2578 int_pix_fmt = PIX_FMT_GRAY8;
2579 } else if ((is_yuv_planar(src_pix) &&
2580 src_pix_fmt != PIX_FMT_YUV444P &&
2581 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2582 /* yuv444 is the normalized format */
2583 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2584 int_pix_fmt = PIX_FMT_YUVJ444P;
2586 int_pix_fmt = PIX_FMT_YUV444P;
2587 } else if ((is_yuv_planar(dst_pix) &&
2588 dst_pix_fmt != PIX_FMT_YUV444P &&
2589 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2590 /* yuv444 is the normalized format */
2591 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2592 int_pix_fmt = PIX_FMT_YUVJ444P;
2594 int_pix_fmt = PIX_FMT_YUV444P;
2596 /* the two formats are rgb or gray8 or yuv[j]444p */
2597 if (src_pix->is_alpha && dst_pix->is_alpha)
2598 int_pix_fmt = PIX_FMT_RGB32;
2600 int_pix_fmt = PIX_FMT_RGB24;
2602 if (src_pix_fmt == int_pix_fmt)
2604 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2607 if (img_convert(tmp, int_pix_fmt,
2608 src, src_pix_fmt, src_width, src_height) < 0)
2610 if (img_convert(dst, dst_pix_fmt,
2611 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2615 avpicture_free(tmp);
2620 /* NOTE: we scan all the pixels to have an exact information */
2621 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2623 const unsigned char *p;
2624 int src_wrap, ret, x, y;
2626 uint32_t *palette = (uint32_t *)src->data[1];
2629 src_wrap = src->linesize[0] - width;
2631 for(y=0;y<height;y++) {
2632 for(x=0;x<width;x++) {
2633 a = palette[p[0]] >> 24;
2635 ret |= FF_ALPHA_TRANSP;
2636 } else if (a != 0xff) {
2637 ret |= FF_ALPHA_SEMI_TRANSP;
2646 int img_get_alpha_info(const AVPicture *src,
2647 int pix_fmt, int width, int height)
2649 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2652 pf = &pix_fmt_info[pix_fmt];
2653 /* no alpha can be represented in format */
2658 ret = get_alpha_info_rgb32(src, width, height);
2661 ret = get_alpha_info_pal8(src, width, height);
2664 /* we do not know, so everything is indicated */
2665 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2672 #define DEINT_INPLACE_LINE_LUM \
2673 movd_m2r(lum_m4[0],mm0);\
2674 movd_m2r(lum_m3[0],mm1);\
2675 movd_m2r(lum_m2[0],mm2);\
2676 movd_m2r(lum_m1[0],mm3);\
2677 movd_m2r(lum[0],mm4);\
2678 punpcklbw_r2r(mm7,mm0);\
2679 movd_r2m(mm2,lum_m4[0]);\
2680 punpcklbw_r2r(mm7,mm1);\
2681 punpcklbw_r2r(mm7,mm2);\
2682 punpcklbw_r2r(mm7,mm3);\
2683 punpcklbw_r2r(mm7,mm4);\
2684 paddw_r2r(mm3,mm1);\
2686 paddw_r2r(mm4,mm0);\
2688 paddw_r2r(mm6,mm2);\
2689 paddw_r2r(mm2,mm1);\
2690 psubusw_r2r(mm0,mm1);\
2692 packuswb_r2r(mm7,mm1);\
2693 movd_r2m(mm1,lum_m2[0]);
2695 #define DEINT_LINE_LUM \
2696 movd_m2r(lum_m4[0],mm0);\
2697 movd_m2r(lum_m3[0],mm1);\
2698 movd_m2r(lum_m2[0],mm2);\
2699 movd_m2r(lum_m1[0],mm3);\
2700 movd_m2r(lum[0],mm4);\
2701 punpcklbw_r2r(mm7,mm0);\
2702 punpcklbw_r2r(mm7,mm1);\
2703 punpcklbw_r2r(mm7,mm2);\
2704 punpcklbw_r2r(mm7,mm3);\
2705 punpcklbw_r2r(mm7,mm4);\
2706 paddw_r2r(mm3,mm1);\
2708 paddw_r2r(mm4,mm0);\
2710 paddw_r2r(mm6,mm2);\
2711 paddw_r2r(mm2,mm1);\
2712 psubusw_r2r(mm0,mm1);\
2714 packuswb_r2r(mm7,mm1);\
2715 movd_r2m(mm1,dst[0]);
2718 /* filter parameters: [-1 4 2 4 -1] // 8 */
2719 static void deinterlace_line(uint8_t *dst,
2720 const uint8_t *lum_m4, const uint8_t *lum_m3,
2721 const uint8_t *lum_m2, const uint8_t *lum_m1,
2726 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2729 for(;size > 0;size--) {
2731 sum += lum_m3[0] << 2;
2732 sum += lum_m2[0] << 1;
2733 sum += lum_m1[0] << 2;
2735 dst[0] = cm[(sum + 4) >> 3];
2747 movq_m2r(ff_pw_4,mm6);
2749 for (;size > 3; size-=4) {
2760 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2764 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2767 for(;size > 0;size--) {
2769 sum += lum_m3[0] << 2;
2770 sum += lum_m2[0] << 1;
2771 lum_m4[0]=lum_m2[0];
2772 sum += lum_m1[0] << 2;
2774 lum_m2[0] = cm[(sum + 4) >> 3];
2785 movq_m2r(ff_pw_4,mm6);
2787 for (;size > 3; size-=4) {
2788 DEINT_INPLACE_LINE_LUM
2798 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2799 top field is copied as is, but the bottom field is deinterlaced
2800 against the top field. */
2801 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2802 const uint8_t *src1, int src_wrap,
2803 int width, int height)
2805 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2810 src_0=&src_m1[src_wrap];
2811 src_p1=&src_0[src_wrap];
2812 src_p2=&src_p1[src_wrap];
2813 for(y=0;y<(height-2);y+=2) {
2814 memcpy(dst,src_m1,width);
2816 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2820 src_p1 += 2*src_wrap;
2821 src_p2 += 2*src_wrap;
2824 memcpy(dst,src_m1,width);
2827 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2830 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2831 int width, int height)
2833 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2836 buf = (uint8_t*)av_malloc(width);
2839 memcpy(buf,src_m1,width);
2840 src_0=&src_m1[src_wrap];
2841 src_p1=&src_0[src_wrap];
2842 src_p2=&src_p1[src_wrap];
2843 for(y=0;y<(height-2);y+=2) {
2844 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2847 src_p1 += 2*src_wrap;
2848 src_p2 += 2*src_wrap;
2851 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2855 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2856 int pix_fmt, int width, int height)
2860 if (pix_fmt != PIX_FMT_YUV420P &&
2861 pix_fmt != PIX_FMT_YUV422P &&
2862 pix_fmt != PIX_FMT_YUV444P &&
2863 pix_fmt != PIX_FMT_YUV411P &&
2864 pix_fmt != PIX_FMT_GRAY8)
2866 if ((width & 3) != 0 || (height & 3) != 0)
2872 case PIX_FMT_YUV420P:
2876 case PIX_FMT_YUV422P:
2879 case PIX_FMT_YUV411P:
2885 if (pix_fmt == PIX_FMT_GRAY8) {
2890 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2893 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2894 src->data[i], src->linesize[i],