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_VDPAU_WMV3] = {
280 .name = "vdpau_wmv3",
282 [PIX_FMT_VDPAU_VC1] = {
285 [PIX_FMT_UYYVYY411] = {
288 .color_type = FF_COLOR_YUV,
289 .pixel_type = FF_PIXEL_PACKED,
291 .x_chroma_shift = 2, .y_chroma_shift = 0,
295 .nb_channels = 4, .is_alpha = 1,
296 .color_type = FF_COLOR_RGB,
297 .pixel_type = FF_PIXEL_PACKED,
299 .x_chroma_shift = 0, .y_chroma_shift = 0,
304 .color_type = FF_COLOR_RGB,
305 .pixel_type = FF_PIXEL_PACKED,
307 .x_chroma_shift = 0, .y_chroma_shift = 0,
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,
333 [PIX_FMT_RGB4_BYTE] = {
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,
352 .color_type = FF_COLOR_RGB,
353 .pixel_type = FF_PIXEL_PACKED,
355 .x_chroma_shift = 0, .y_chroma_shift = 0,
357 [PIX_FMT_BGR4_BYTE] = {
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_YUV,
369 .pixel_type = FF_PIXEL_PLANAR,
371 .x_chroma_shift = 1, .y_chroma_shift = 1,
376 .color_type = FF_COLOR_YUV,
377 .pixel_type = FF_PIXEL_PLANAR,
379 .x_chroma_shift = 1, .y_chroma_shift = 1,
382 [PIX_FMT_BGR32_1] = {
384 .nb_channels = 4, .is_alpha = 1,
385 .color_type = FF_COLOR_RGB,
386 .pixel_type = FF_PIXEL_PACKED,
388 .x_chroma_shift = 0, .y_chroma_shift = 0,
390 [PIX_FMT_RGB32_1] = {
392 .nb_channels = 4, .is_alpha = 1,
393 .color_type = FF_COLOR_RGB,
394 .pixel_type = FF_PIXEL_PACKED,
396 .x_chroma_shift = 0, .y_chroma_shift = 0,
400 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
402 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
403 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
406 const char *avcodec_get_pix_fmt_name(int pix_fmt)
408 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
411 return pix_fmt_info[pix_fmt].name;
414 enum PixelFormat avcodec_get_pix_fmt(const char* name)
418 for (i=0; i < PIX_FMT_NB; i++)
419 if (!strcmp(pix_fmt_info[i].name, name))
424 void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
428 snprintf (buf, buf_size,
429 "name " " nb_channels" " depth" " is_alpha"
432 PixFmtInfo info= pix_fmt_info[pix_fmt];
434 char is_alpha_char= info.is_alpha ? 'y' : 'n';
436 snprintf (buf, buf_size,
437 "%-10s" " %1d " " %2d " " %c ",
446 int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
449 const PixFmtInfo *pinfo;
451 memset(picture->linesize, 0, sizeof(picture->linesize));
453 pinfo = &pix_fmt_info[pix_fmt];
455 case PIX_FMT_YUV420P:
456 case PIX_FMT_YUV422P:
457 case PIX_FMT_YUV444P:
458 case PIX_FMT_YUV410P:
459 case PIX_FMT_YUV411P:
460 case PIX_FMT_YUV440P:
461 case PIX_FMT_YUVJ420P:
462 case PIX_FMT_YUVJ422P:
463 case PIX_FMT_YUVJ444P:
464 case PIX_FMT_YUVJ440P:
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;
470 case PIX_FMT_YUVA420P:
471 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
472 picture->linesize[0] = width;
473 picture->linesize[1] = w2;
474 picture->linesize[2] = w2;
475 picture->linesize[3] = width;
479 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
480 picture->linesize[0] = width;
481 picture->linesize[1] = w2;
485 picture->linesize[0] = width * 3;
489 case PIX_FMT_RGB32_1:
490 case PIX_FMT_BGR32_1:
491 picture->linesize[0] = width * 4;
493 case PIX_FMT_GRAY16BE:
494 case PIX_FMT_GRAY16LE:
499 case PIX_FMT_YUYV422:
500 picture->linesize[0] = width * 2;
502 case PIX_FMT_UYVY422:
503 picture->linesize[0] = width * 2;
505 case PIX_FMT_UYYVYY411:
506 picture->linesize[0] = width + width/2;
510 case PIX_FMT_RGB4_BYTE:
511 case PIX_FMT_BGR4_BYTE:
513 picture->linesize[0] = width;
517 picture->linesize[0] = width / 2;
519 case PIX_FMT_MONOWHITE:
520 case PIX_FMT_MONOBLACK:
521 picture->linesize[0] = (width + 7) >> 3;
524 picture->linesize[0] = width;
525 picture->linesize[1] = 4;
533 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
537 const PixFmtInfo *pinfo;
539 pinfo = &pix_fmt_info[pix_fmt];
540 size = picture->linesize[0] * height;
542 case PIX_FMT_YUV420P:
543 case PIX_FMT_YUV422P:
544 case PIX_FMT_YUV444P:
545 case PIX_FMT_YUV410P:
546 case PIX_FMT_YUV411P:
547 case PIX_FMT_YUV440P:
548 case PIX_FMT_YUVJ420P:
549 case PIX_FMT_YUVJ422P:
550 case PIX_FMT_YUVJ444P:
551 case PIX_FMT_YUVJ440P:
552 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
553 size2 = picture->linesize[1] * h2;
554 picture->data[0] = ptr;
555 picture->data[1] = picture->data[0] + size;
556 picture->data[2] = picture->data[1] + size2;
557 picture->data[3] = NULL;
558 return size + 2 * size2;
559 case PIX_FMT_YUVA420P:
560 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
561 size2 = picture->linesize[1] * h2;
562 picture->data[0] = ptr;
563 picture->data[1] = picture->data[0] + size;
564 picture->data[2] = picture->data[1] + size2;
565 picture->data[3] = picture->data[1] + size2 + size2;
566 return 2 * size + 2 * size2;
569 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
570 size2 = picture->linesize[1] * h2 * 2;
571 picture->data[0] = ptr;
572 picture->data[1] = picture->data[0] + size;
573 picture->data[2] = NULL;
574 picture->data[3] = NULL;
575 return size + 2 * size2;
580 case PIX_FMT_RGB32_1:
581 case PIX_FMT_BGR32_1:
582 case PIX_FMT_GRAY16BE:
583 case PIX_FMT_GRAY16LE:
588 case PIX_FMT_YUYV422:
589 case PIX_FMT_UYVY422:
590 case PIX_FMT_UYYVYY411:
593 case PIX_FMT_RGB4_BYTE:
594 case PIX_FMT_BGR4_BYTE:
598 case PIX_FMT_MONOWHITE:
599 case PIX_FMT_MONOBLACK:
600 picture->data[0] = ptr;
601 picture->data[1] = NULL;
602 picture->data[2] = NULL;
603 picture->data[3] = NULL;
606 size2 = (size + 3) & ~3;
607 picture->data[0] = ptr;
608 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
609 picture->data[2] = NULL;
610 picture->data[3] = NULL;
611 return size2 + 256 * 4;
613 picture->data[0] = NULL;
614 picture->data[1] = NULL;
615 picture->data[2] = NULL;
616 picture->data[3] = NULL;
621 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
622 int pix_fmt, int width, int height)
625 if(avcodec_check_dimensions(NULL, width, height))
628 if (ff_fill_linesize(picture, pix_fmt, width))
631 return ff_fill_pointer(picture, ptr, pix_fmt, height);
634 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
635 unsigned char *dest, int dest_size)
637 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
638 int i, j, w, h, data_planes;
639 const unsigned char* s;
640 int size = avpicture_get_size(pix_fmt, width, height);
642 if (size > dest_size || size < 0)
645 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
646 if (pix_fmt == PIX_FMT_YUYV422 ||
647 pix_fmt == PIX_FMT_UYVY422 ||
648 pix_fmt == PIX_FMT_BGR565 ||
649 pix_fmt == PIX_FMT_BGR555 ||
650 pix_fmt == PIX_FMT_RGB565 ||
651 pix_fmt == PIX_FMT_RGB555)
653 else if (pix_fmt == PIX_FMT_UYYVYY411)
655 else if (pix_fmt == PIX_FMT_PAL8)
658 w = width * (pf->depth * pf->nb_channels / 8);
663 data_planes = pf->nb_channels;
664 w = (width*pf->depth + 7)/8;
668 for (i=0; i<data_planes; i++) {
670 w = width >> pf->x_chroma_shift;
671 h = height >> pf->y_chroma_shift;
677 s += src->linesize[i];
681 if (pf->pixel_type == FF_PIXEL_PALETTE)
682 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
687 int avpicture_get_size(int pix_fmt, int width, int height)
689 AVPicture dummy_pict;
690 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
693 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
696 const PixFmtInfo *pf, *ps;
699 ps = &pix_fmt_info[src_pix_fmt];
700 pf = &pix_fmt_info[dst_pix_fmt];
704 pf = &pix_fmt_info[dst_pix_fmt];
705 if (pf->depth < ps->depth ||
706 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
707 loss |= FF_LOSS_DEPTH;
708 if (pf->x_chroma_shift > ps->x_chroma_shift ||
709 pf->y_chroma_shift > ps->y_chroma_shift)
710 loss |= FF_LOSS_RESOLUTION;
711 switch(pf->color_type) {
713 if (ps->color_type != FF_COLOR_RGB &&
714 ps->color_type != FF_COLOR_GRAY)
715 loss |= FF_LOSS_COLORSPACE;
718 if (ps->color_type != FF_COLOR_GRAY)
719 loss |= FF_LOSS_COLORSPACE;
722 if (ps->color_type != FF_COLOR_YUV)
723 loss |= FF_LOSS_COLORSPACE;
725 case FF_COLOR_YUV_JPEG:
726 if (ps->color_type != FF_COLOR_YUV_JPEG &&
727 ps->color_type != FF_COLOR_YUV &&
728 ps->color_type != FF_COLOR_GRAY)
729 loss |= FF_LOSS_COLORSPACE;
733 if (ps->color_type != pf->color_type)
734 loss |= FF_LOSS_COLORSPACE;
737 if (pf->color_type == FF_COLOR_GRAY &&
738 ps->color_type != FF_COLOR_GRAY)
739 loss |= FF_LOSS_CHROMA;
740 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
741 loss |= FF_LOSS_ALPHA;
742 if (pf->pixel_type == FF_PIXEL_PALETTE &&
743 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
744 loss |= FF_LOSS_COLORQUANT;
748 static int avg_bits_per_pixel(int pix_fmt)
751 const PixFmtInfo *pf;
753 pf = &pix_fmt_info[pix_fmt];
754 switch(pf->pixel_type) {
755 case FF_PIXEL_PACKED:
757 case PIX_FMT_YUYV422:
758 case PIX_FMT_UYVY422:
765 case PIX_FMT_UYYVYY411:
769 bits = pf->depth * pf->nb_channels;
773 case FF_PIXEL_PLANAR:
774 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
775 bits = pf->depth * pf->nb_channels;
777 bits = pf->depth + ((2 * pf->depth) >>
778 (pf->x_chroma_shift + pf->y_chroma_shift));
781 case FF_PIXEL_PALETTE:
791 static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
796 int dist, i, loss, min_dist, dst_pix_fmt;
798 /* find exact color match with smallest size */
800 min_dist = 0x7fffffff;
801 for(i = 0;i < PIX_FMT_NB; i++) {
802 if (pix_fmt_mask & (1ULL << i)) {
803 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
805 dist = avg_bits_per_pixel(i);
806 if (dist < min_dist) {
816 int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
817 int has_alpha, int *loss_ptr)
819 int dst_pix_fmt, loss_mask, i;
820 static const int loss_mask_order[] = {
821 ~0, /* no loss first */
824 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
830 /* try with successive loss */
833 loss_mask = loss_mask_order[i++];
834 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
835 has_alpha, loss_mask);
836 if (dst_pix_fmt >= 0)
844 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
848 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
849 const uint8_t *src, int src_wrap,
850 int width, int height)
854 for(;height > 0; height--) {
855 memcpy(dst, src, width);
861 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
864 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
866 pf = &pix_fmt_info[pix_fmt];
867 switch(pf->pixel_type) {
868 case FF_PIXEL_PACKED:
870 case PIX_FMT_YUYV422:
871 case PIX_FMT_UYVY422:
878 case PIX_FMT_UYYVYY411:
882 bits = pf->depth * pf->nb_channels;
885 return (width * bits + 7) >> 3;
887 case FF_PIXEL_PLANAR:
888 if (plane == 1 || plane == 2)
889 width= -((-width)>>pf->x_chroma_shift);
891 return (width * pf->depth + 7) >> 3;
893 case FF_PIXEL_PALETTE:
902 void av_picture_copy(AVPicture *dst, const AVPicture *src,
903 int pix_fmt, int width, int height)
906 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
908 pf = &pix_fmt_info[pix_fmt];
909 switch(pf->pixel_type) {
910 case FF_PIXEL_PACKED:
911 case FF_PIXEL_PLANAR:
912 for(i = 0; i < pf->nb_channels; i++) {
914 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
916 if (i == 1 || i == 2) {
917 h= -((-height)>>pf->y_chroma_shift);
919 ff_img_copy_plane(dst->data[i], dst->linesize[i],
920 src->data[i], src->linesize[i],
924 case FF_PIXEL_PALETTE:
925 ff_img_copy_plane(dst->data[0], dst->linesize[0],
926 src->data[0], src->linesize[0],
928 /* copy the palette */
929 ff_img_copy_plane(dst->data[1], dst->linesize[1],
930 src->data[1], src->linesize[1],
936 /* XXX: totally non optimized */
938 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
939 int width, int height)
941 const uint8_t *p, *p1;
942 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
950 for(;height >= 1; height -= 2) {
955 for(w = width; w >= 2; w -= 2) {
972 p1 += src->linesize[0];
973 lum1 += dst->linesize[0];
977 for(w = width; w >= 2; w -= 2) {
986 p1 += src->linesize[0];
987 lum1 += dst->linesize[0];
989 cb1 += dst->linesize[1];
990 cr1 += dst->linesize[2];
994 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
995 int width, int height)
997 const uint8_t *p, *p1;
998 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1003 lum1 = dst->data[0];
1007 for(;height >= 1; height -= 2) {
1012 for(w = width; w >= 2; w -= 2) {
1029 p1 += src->linesize[0];
1030 lum1 += dst->linesize[0];
1034 for(w = width; w >= 2; w -= 2) {
1043 p1 += src->linesize[0];
1044 lum1 += dst->linesize[0];
1046 cb1 += dst->linesize[1];
1047 cr1 += dst->linesize[2];
1052 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1053 int width, int height)
1055 const uint8_t *p, *p1;
1056 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1060 lum1 = dst->data[0];
1063 for(;height > 0; height--) {
1068 for(w = width; w >= 2; w -= 2) {
1078 p1 += src->linesize[0];
1079 lum1 += dst->linesize[0];
1080 cb1 += dst->linesize[1];
1081 cr1 += dst->linesize[2];
1086 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1087 int width, int height)
1089 const uint8_t *p, *p1;
1090 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1094 lum1 = dst->data[0];
1097 for(;height > 0; height--) {
1102 for(w = width; w >= 2; w -= 2) {
1112 p1 += src->linesize[0];
1113 lum1 += dst->linesize[0];
1114 cb1 += dst->linesize[1];
1115 cr1 += dst->linesize[2];
1119 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1120 int width, int height)
1123 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1127 lum1 = src->data[0];
1130 for(;height > 0; height--) {
1135 for(w = width; w >= 2; w -= 2) {
1145 p1 += dst->linesize[0];
1146 lum1 += src->linesize[0];
1147 cb1 += src->linesize[1];
1148 cr1 += src->linesize[2];
1152 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1153 int width, int height)
1156 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1160 lum1 = src->data[0];
1163 for(;height > 0; height--) {
1168 for(w = width; w >= 2; w -= 2) {
1178 p1 += dst->linesize[0];
1179 lum1 += src->linesize[0];
1180 cb1 += src->linesize[1];
1181 cr1 += src->linesize[2];
1185 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1186 int width, int height)
1188 const uint8_t *p, *p1;
1189 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1193 lum1 = dst->data[0];
1196 for(;height > 0; height--) {
1201 for(w = width; w >= 4; w -= 4) {
1213 p1 += src->linesize[0];
1214 lum1 += dst->linesize[0];
1215 cb1 += dst->linesize[1];
1216 cr1 += dst->linesize[2];
1221 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1222 int width, int height)
1225 uint8_t *line1, *line2, *linesrc = dst->data[0];
1226 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1227 uint8_t *cb1, *cb2 = src->data[1];
1228 uint8_t *cr1, *cr2 = src->data[2];
1230 for(h = height / 2; h--;) {
1232 line2 = linesrc + dst->linesize[0];
1235 lum2 = lumsrc + src->linesize[0];
1240 for(w = width / 2; w--;) {
1241 *line1++ = *lum1++; *line2++ = *lum2++;
1242 *line1++ = *line2++ = *cb1++;
1243 *line1++ = *lum1++; *line2++ = *lum2++;
1244 *line1++ = *line2++ = *cr1++;
1247 linesrc += dst->linesize[0] * 2;
1248 lumsrc += src->linesize[0] * 2;
1249 cb2 += src->linesize[1];
1250 cr2 += src->linesize[2];
1254 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1255 int width, int height)
1258 uint8_t *line1, *line2, *linesrc = dst->data[0];
1259 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1260 uint8_t *cb1, *cb2 = src->data[1];
1261 uint8_t *cr1, *cr2 = src->data[2];
1263 for(h = height / 2; h--;) {
1265 line2 = linesrc + dst->linesize[0];
1268 lum2 = lumsrc + src->linesize[0];
1273 for(w = width / 2; w--;) {
1274 *line1++ = *line2++ = *cb1++;
1275 *line1++ = *lum1++; *line2++ = *lum2++;
1276 *line1++ = *line2++ = *cr1++;
1277 *line1++ = *lum1++; *line2++ = *lum2++;
1280 linesrc += dst->linesize[0] * 2;
1281 lumsrc += src->linesize[0] * 2;
1282 cb2 += src->linesize[1];
1283 cr2 += src->linesize[2];
1288 void ff_shrink22(uint8_t *dst, int dst_wrap,
1289 const uint8_t *src, int src_wrap,
1290 int width, int height)
1293 const uint8_t *s1, *s2;
1296 for(;height > 0; height--) {
1300 for(w = width;w >= 4; w-=4) {
1301 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1302 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1303 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1304 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1310 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1315 src += 2 * src_wrap;
1321 void ff_shrink44(uint8_t *dst, int dst_wrap,
1322 const uint8_t *src, int src_wrap,
1323 int width, int height)
1326 const uint8_t *s1, *s2, *s3, *s4;
1329 for(;height > 0; height--) {
1335 for(w = width;w > 0; w--) {
1336 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1337 s2[0] + s2[1] + s2[2] + s2[3] +
1338 s3[0] + s3[1] + s3[2] + s3[3] +
1339 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1346 src += 4 * src_wrap;
1352 void ff_shrink88(uint8_t *dst, int dst_wrap,
1353 const uint8_t *src, int src_wrap,
1354 int width, int height)
1358 for(;height > 0; height--) {
1359 for(w = width;w > 0; w--) {
1362 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1365 *(dst++) = (tmp + 32)>>6;
1366 src += 8 - 8*src_wrap;
1368 src += 8*src_wrap - 8*width;
1369 dst += dst_wrap - width;
1373 /* XXX: add jpeg quantize code */
1375 #define TRANSP_INDEX (6*6*6)
1377 /* this is maybe slow, but allows for extensions */
1378 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1380 return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
1383 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1386 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1389 pal = (uint32_t *)palette;
1391 for(r = 0; r < 6; r++) {
1392 for(g = 0; g < 6; g++) {
1393 for(b = 0; b < 6; b++) {
1394 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1395 (pal_value[g] << 8) | pal_value[b];
1402 pal[i++] = 0xff000000;
1405 /* copy bit n to bits 0 ... n - 1 */
1406 static inline unsigned int bitcopy_n(unsigned int a, int n)
1409 mask = (1 << n) - 1;
1410 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1413 /* rgb555 handling */
1415 #define RGB_NAME rgb555
1417 #define RGB_IN(r, g, b, s)\
1419 unsigned int v = ((const uint16_t *)(s))[0];\
1420 r = bitcopy_n(v >> (10 - 3), 3);\
1421 g = bitcopy_n(v >> (5 - 3), 3);\
1422 b = bitcopy_n(v << 3, 3);\
1426 #define RGB_OUT(d, r, g, b)\
1428 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1433 #include "imgconvert_template.c"
1435 /* rgb565 handling */
1437 #define RGB_NAME rgb565
1439 #define RGB_IN(r, g, b, s)\
1441 unsigned int v = ((const uint16_t *)(s))[0];\
1442 r = bitcopy_n(v >> (11 - 3), 3);\
1443 g = bitcopy_n(v >> (5 - 2), 2);\
1444 b = bitcopy_n(v << 3, 3);\
1447 #define RGB_OUT(d, r, g, b)\
1449 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1454 #include "imgconvert_template.c"
1456 /* bgr24 handling */
1458 #define RGB_NAME bgr24
1460 #define RGB_IN(r, g, b, s)\
1467 #define RGB_OUT(d, r, g, b)\
1476 #include "imgconvert_template.c"
1482 /* rgb24 handling */
1484 #define RGB_NAME rgb24
1487 #define RGB_IN(r, g, b, s)\
1494 #define RGB_OUT(d, r, g, b)\
1503 #include "imgconvert_template.c"
1505 /* rgb32 handling */
1507 #define RGB_NAME rgb32
1510 #define RGB_IN(r, g, b, s)\
1512 unsigned int v = ((const uint32_t *)(s))[0];\
1513 r = (v >> 16) & 0xff;\
1514 g = (v >> 8) & 0xff;\
1518 #define RGBA_IN(r, g, b, a, s)\
1520 unsigned int v = ((const uint32_t *)(s))[0];\
1521 a = (v >> 24) & 0xff;\
1522 r = (v >> 16) & 0xff;\
1523 g = (v >> 8) & 0xff;\
1527 #define RGBA_OUT(d, r, g, b, a)\
1529 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1534 #include "imgconvert_template.c"
1536 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1537 int width, int height, int xor_mask)
1539 const unsigned char *p;
1541 int v, dst_wrap, src_wrap;
1545 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1548 dst_wrap = dst->linesize[0] - width;
1549 for(y=0;y<height;y++) {
1552 v = *p++ ^ xor_mask;
1554 q[1] = -((v >> 6) & 1);
1555 q[2] = -((v >> 5) & 1);
1556 q[3] = -((v >> 4) & 1);
1557 q[4] = -((v >> 3) & 1);
1558 q[5] = -((v >> 2) & 1);
1559 q[6] = -((v >> 1) & 1);
1560 q[7] = -((v >> 0) & 1);
1565 v = *p++ ^ xor_mask;
1567 q[0] = -((v >> 7) & 1);
1577 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1578 int width, int height)
1580 mono_to_gray(dst, src, width, height, 0xff);
1583 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1584 int width, int height)
1586 mono_to_gray(dst, src, width, height, 0x00);
1589 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1590 int width, int height, int xor_mask)
1595 int j, b, v, n1, src_wrap, dst_wrap, y;
1598 src_wrap = src->linesize[0] - width;
1601 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1603 for(y=0;y<height;y++) {
1610 v = (v << 1) | (b >> 7);
1612 d[0] = v ^ xor_mask;
1622 v = (v << 1) | (b >> 7);
1625 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1633 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1634 int width, int height)
1636 gray_to_mono(dst, src, width, height, 0xff);
1639 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1640 int width, int height)
1642 gray_to_mono(dst, src, width, height, 0x00);
1645 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1646 int width, int height)
1648 int x, y, src_wrap, dst_wrap;
1651 src_wrap = src->linesize[0] - width;
1653 dst_wrap = dst->linesize[0] - width * 2;
1654 for(y=0; y<height; y++){
1655 for(x=0; x<width; x++){
1664 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1665 int width, int height)
1667 int x, y, src_wrap, dst_wrap;
1670 src_wrap = src->linesize[0] - width * 2;
1672 dst_wrap = dst->linesize[0] - width;
1673 for(y=0; y<height; y++){
1674 for(x=0; x<width; x++){
1683 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1684 int width, int height)
1686 gray16_to_gray(dst, src, width, height);
1689 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1690 int width, int height)
1692 AVPicture tmpsrc = *src;
1694 gray16_to_gray(dst, &tmpsrc, width, height);
1697 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1698 int width, int height)
1700 int x, y, src_wrap, dst_wrap;
1702 s = (uint16_t*)src->data[0];
1703 src_wrap = (src->linesize[0] - width * 2)/2;
1704 d = (uint16_t*)dst->data[0];
1705 dst_wrap = (dst->linesize[0] - width * 2)/2;
1706 for(y=0; y<height; y++){
1707 for(x=0; x<width; x++){
1708 *d++ = bswap_16(*s++);
1716 typedef struct ConvertEntry {
1717 void (*convert)(AVPicture *dst,
1718 const AVPicture *src, int width, int height);
1721 /* Add each new conversion function in this table. In order to be able
1722 to convert from any format to any format, the following constraints
1725 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1727 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1729 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1731 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1734 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1736 The other conversion functions are just optimizations for common cases.
1738 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1739 [PIX_FMT_YUV420P] = {
1740 [PIX_FMT_YUYV422] = {
1741 .convert = yuv420p_to_yuyv422,
1743 [PIX_FMT_RGB555] = {
1744 .convert = yuv420p_to_rgb555
1746 [PIX_FMT_RGB565] = {
1747 .convert = yuv420p_to_rgb565
1750 .convert = yuv420p_to_bgr24
1753 .convert = yuv420p_to_rgb24
1756 .convert = yuv420p_to_rgb32
1758 [PIX_FMT_UYVY422] = {
1759 .convert = yuv420p_to_uyvy422,
1762 [PIX_FMT_YUV422P] = {
1763 [PIX_FMT_YUYV422] = {
1764 .convert = yuv422p_to_yuyv422,
1766 [PIX_FMT_UYVY422] = {
1767 .convert = yuv422p_to_uyvy422,
1770 [PIX_FMT_YUV444P] = {
1772 .convert = yuv444p_to_rgb24
1775 [PIX_FMT_YUVJ420P] = {
1776 [PIX_FMT_RGB555] = {
1777 .convert = yuvj420p_to_rgb555
1779 [PIX_FMT_RGB565] = {
1780 .convert = yuvj420p_to_rgb565
1783 .convert = yuvj420p_to_bgr24
1786 .convert = yuvj420p_to_rgb24
1789 .convert = yuvj420p_to_rgb32
1792 [PIX_FMT_YUVJ444P] = {
1794 .convert = yuvj444p_to_rgb24
1797 [PIX_FMT_YUYV422] = {
1798 [PIX_FMT_YUV420P] = {
1799 .convert = yuyv422_to_yuv420p,
1801 [PIX_FMT_YUV422P] = {
1802 .convert = yuyv422_to_yuv422p,
1805 [PIX_FMT_UYVY422] = {
1806 [PIX_FMT_YUV420P] = {
1807 .convert = uyvy422_to_yuv420p,
1809 [PIX_FMT_YUV422P] = {
1810 .convert = uyvy422_to_yuv422p,
1814 [PIX_FMT_YUV420P] = {
1815 .convert = rgb24_to_yuv420p
1817 [PIX_FMT_RGB565] = {
1818 .convert = rgb24_to_rgb565
1820 [PIX_FMT_RGB555] = {
1821 .convert = rgb24_to_rgb555
1824 .convert = rgb24_to_rgb32
1827 .convert = rgb24_to_bgr24
1830 .convert = rgb24_to_gray
1833 .convert = rgb24_to_pal8
1835 [PIX_FMT_YUV444P] = {
1836 .convert = rgb24_to_yuv444p
1838 [PIX_FMT_YUVJ420P] = {
1839 .convert = rgb24_to_yuvj420p
1841 [PIX_FMT_YUVJ444P] = {
1842 .convert = rgb24_to_yuvj444p
1847 .convert = rgb32_to_rgb24
1850 .convert = rgb32_to_bgr24
1852 [PIX_FMT_RGB565] = {
1853 .convert = rgb32_to_rgb565
1855 [PIX_FMT_RGB555] = {
1856 .convert = rgb32_to_rgb555
1859 .convert = rgb32_to_pal8
1861 [PIX_FMT_YUV420P] = {
1862 .convert = rgb32_to_yuv420p
1865 .convert = rgb32_to_gray
1870 .convert = bgr24_to_rgb32
1873 .convert = bgr24_to_rgb24
1875 [PIX_FMT_YUV420P] = {
1876 .convert = bgr24_to_yuv420p
1879 .convert = bgr24_to_gray
1882 [PIX_FMT_RGB555] = {
1884 .convert = rgb555_to_rgb24
1887 .convert = rgb555_to_rgb32
1889 [PIX_FMT_YUV420P] = {
1890 .convert = rgb555_to_yuv420p
1893 .convert = rgb555_to_gray
1896 [PIX_FMT_RGB565] = {
1898 .convert = rgb565_to_rgb32
1901 .convert = rgb565_to_rgb24
1903 [PIX_FMT_YUV420P] = {
1904 .convert = rgb565_to_yuv420p
1907 .convert = rgb565_to_gray
1910 [PIX_FMT_GRAY16BE] = {
1912 .convert = gray16be_to_gray
1914 [PIX_FMT_GRAY16LE] = {
1915 .convert = gray16_to_gray16
1918 [PIX_FMT_GRAY16LE] = {
1920 .convert = gray16le_to_gray
1922 [PIX_FMT_GRAY16BE] = {
1923 .convert = gray16_to_gray16
1927 [PIX_FMT_RGB555] = {
1928 .convert = gray_to_rgb555
1930 [PIX_FMT_RGB565] = {
1931 .convert = gray_to_rgb565
1934 .convert = gray_to_rgb24
1937 .convert = gray_to_bgr24
1940 .convert = gray_to_rgb32
1942 [PIX_FMT_MONOWHITE] = {
1943 .convert = gray_to_monowhite
1945 [PIX_FMT_MONOBLACK] = {
1946 .convert = gray_to_monoblack
1948 [PIX_FMT_GRAY16LE] = {
1949 .convert = gray_to_gray16
1951 [PIX_FMT_GRAY16BE] = {
1952 .convert = gray_to_gray16
1955 [PIX_FMT_MONOWHITE] = {
1957 .convert = monowhite_to_gray
1960 [PIX_FMT_MONOBLACK] = {
1962 .convert = monoblack_to_gray
1966 [PIX_FMT_RGB555] = {
1967 .convert = pal8_to_rgb555
1969 [PIX_FMT_RGB565] = {
1970 .convert = pal8_to_rgb565
1973 .convert = pal8_to_bgr24
1976 .convert = pal8_to_rgb24
1979 .convert = pal8_to_rgb32
1982 [PIX_FMT_UYYVYY411] = {
1983 [PIX_FMT_YUV411P] = {
1984 .convert = uyyvyy411_to_yuv411p,
1990 int avpicture_alloc(AVPicture *picture,
1991 int pix_fmt, int width, int height)
1996 size = avpicture_get_size(pix_fmt, width, height);
1999 ptr = av_malloc(size);
2002 avpicture_fill(picture, ptr, pix_fmt, width, height);
2005 memset(picture, 0, sizeof(AVPicture));
2009 void avpicture_free(AVPicture *picture)
2011 av_free(picture->data[0]);
2014 /* return true if yuv planar */
2015 static inline int is_yuv_planar(const PixFmtInfo *ps)
2017 return (ps->color_type == FF_COLOR_YUV ||
2018 ps->color_type == FF_COLOR_YUV_JPEG) &&
2019 ps->pixel_type == FF_PIXEL_PLANAR;
2022 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2023 int pix_fmt, int top_band, int left_band)
2028 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2031 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2032 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2034 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2035 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2036 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2038 dst->linesize[0] = src->linesize[0];
2039 dst->linesize[1] = src->linesize[1];
2040 dst->linesize[2] = src->linesize[2];
2044 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2045 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2054 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2055 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2057 for (i = 0; i < 3; i++) {
2058 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2059 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2061 if (padtop || padleft) {
2062 memset(dst->data[i], color[i],
2063 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2066 if (padleft || padright) {
2067 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2068 (dst->linesize[i] - (padright >> x_shift));
2069 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2070 for (y = 0; y < yheight; y++) {
2071 memset(optr, color[i], (padleft + padright) >> x_shift);
2072 optr += dst->linesize[i];
2076 if (src) { /* first line */
2077 uint8_t *iptr = src->data[i];
2078 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2079 (padleft >> x_shift);
2080 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
2081 iptr += src->linesize[i];
2082 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2083 (dst->linesize[i] - (padright >> x_shift));
2084 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2085 for (y = 0; y < yheight; y++) {
2086 memset(optr, color[i], (padleft + padright) >> x_shift);
2087 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2088 (width - padleft - padright) >> x_shift);
2089 iptr += src->linesize[i];
2090 optr += dst->linesize[i];
2094 if (padbottom || padright) {
2095 optr = dst->data[i] + dst->linesize[i] *
2096 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2097 memset(optr, color[i],dst->linesize[i] *
2098 (padbottom >> y_shift) + (padright >> x_shift));
2105 static uint8_t y_ccir_to_jpeg[256];
2106 static uint8_t y_jpeg_to_ccir[256];
2107 static uint8_t c_ccir_to_jpeg[256];
2108 static uint8_t c_jpeg_to_ccir[256];
2110 /* init various conversion tables */
2111 static void img_convert_init(void)
2114 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2116 for(i = 0;i < 256; i++) {
2117 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
2118 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
2119 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
2120 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
2124 /* apply to each pixel the given table */
2125 static void img_apply_table(uint8_t *dst, int dst_wrap,
2126 const uint8_t *src, int src_wrap,
2127 int width, int height, const uint8_t *table1)
2132 const uint8_t *table;
2135 for(;height > 0; height--) {
2159 /* XXX: use generic filter ? */
2160 /* XXX: in most cases, the sampling position is incorrect */
2163 static void shrink41(uint8_t *dst, int dst_wrap,
2164 const uint8_t *src, int src_wrap,
2165 int width, int height)
2171 for(;height > 0; height--) {
2174 for(w = width;w > 0; w--) {
2175 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
2185 static void shrink21(uint8_t *dst, int dst_wrap,
2186 const uint8_t *src, int src_wrap,
2187 int width, int height)
2193 for(;height > 0; height--) {
2196 for(w = width;w > 0; w--) {
2197 d[0] = (s[0] + s[1]) >> 1;
2207 static void shrink12(uint8_t *dst, int dst_wrap,
2208 const uint8_t *src, int src_wrap,
2209 int width, int height)
2213 const uint8_t *s1, *s2;
2215 for(;height > 0; height--) {
2219 for(w = width;w >= 4; w-=4) {
2220 d[0] = (s1[0] + s2[0]) >> 1;
2221 d[1] = (s1[1] + s2[1]) >> 1;
2222 d[2] = (s1[2] + s2[2]) >> 1;
2223 d[3] = (s1[3] + s2[3]) >> 1;
2229 d[0] = (s1[0] + s2[0]) >> 1;
2234 src += 2 * src_wrap;
2239 static void grow21_line(uint8_t *dst, const uint8_t *src,
2248 for(w = width;w >= 4; w-=4) {
2249 d[1] = d[0] = s1[0];
2250 d[3] = d[2] = s1[1];
2254 for(;w >= 2; w -= 2) {
2255 d[1] = d[0] = s1[0];
2259 /* only needed if width is not a multiple of two */
2260 /* XXX: veryfy that */
2266 static void grow41_line(uint8_t *dst, const uint8_t *src,
2275 for(w = width;w >= 4; w-=4) {
2287 static void grow21(uint8_t *dst, int dst_wrap,
2288 const uint8_t *src, int src_wrap,
2289 int width, int height)
2291 for(;height > 0; height--) {
2292 grow21_line(dst, src, width);
2299 static void grow12(uint8_t *dst, int dst_wrap,
2300 const uint8_t *src, int src_wrap,
2301 int width, int height)
2303 for(;height > 0; height-=2) {
2304 memcpy(dst, src, width);
2306 memcpy(dst, src, width);
2313 static void grow22(uint8_t *dst, int dst_wrap,
2314 const uint8_t *src, int src_wrap,
2315 int width, int height)
2317 for(;height > 0; height--) {
2318 grow21_line(dst, src, width);
2326 static void grow41(uint8_t *dst, int dst_wrap,
2327 const uint8_t *src, int src_wrap,
2328 int width, int height)
2330 for(;height > 0; height--) {
2331 grow41_line(dst, src, width);
2338 static void grow44(uint8_t *dst, int dst_wrap,
2339 const uint8_t *src, int src_wrap,
2340 int width, int height)
2342 for(;height > 0; height--) {
2343 grow41_line(dst, src, width);
2344 if ((height & 3) == 1)
2351 static void conv411(uint8_t *dst, int dst_wrap,
2352 const uint8_t *src, int src_wrap,
2353 int width, int height)
2356 const uint8_t *s1, *s2;
2361 for(;height > 0; height--) {
2363 s2 = src + src_wrap;
2365 for(w = width;w > 0; w--) {
2366 c = (s1[0] + s2[0]) >> 1;
2373 src += src_wrap * 2;
2378 /* XXX: always use linesize. Return -1 if not supported */
2379 int img_convert(AVPicture *dst, int dst_pix_fmt,
2380 const AVPicture *src, int src_pix_fmt,
2381 int src_width, int src_height)
2383 static int initialized;
2384 int i, ret, dst_width, dst_height, int_pix_fmt;
2385 const PixFmtInfo *src_pix, *dst_pix;
2386 const ConvertEntry *ce;
2387 AVPicture tmp1, *tmp = &tmp1;
2389 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2390 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2392 if (src_width <= 0 || src_height <= 0)
2400 dst_width = src_width;
2401 dst_height = src_height;
2403 dst_pix = &pix_fmt_info[dst_pix_fmt];
2404 src_pix = &pix_fmt_info[src_pix_fmt];
2405 if (src_pix_fmt == dst_pix_fmt) {
2406 /* no conversion needed: just copy */
2407 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2411 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2413 /* specific conversion routine */
2414 ce->convert(dst, src, dst_width, dst_height);
2419 if (is_yuv_planar(dst_pix) &&
2420 src_pix_fmt == PIX_FMT_GRAY8) {
2424 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2425 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2426 src->data[0], src->linesize[0],
2427 dst_width, dst_height);
2429 img_apply_table(dst->data[0], dst->linesize[0],
2430 src->data[0], src->linesize[0],
2431 dst_width, dst_height,
2434 /* fill U and V with 128 */
2437 w >>= dst_pix->x_chroma_shift;
2438 h >>= dst_pix->y_chroma_shift;
2439 for(i = 1; i <= 2; i++) {
2441 for(y = 0; y< h; y++) {
2443 d += dst->linesize[i];
2450 if (is_yuv_planar(src_pix) &&
2451 dst_pix_fmt == PIX_FMT_GRAY8) {
2452 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2453 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2454 src->data[0], src->linesize[0],
2455 dst_width, dst_height);
2457 img_apply_table(dst->data[0], dst->linesize[0],
2458 src->data[0], src->linesize[0],
2459 dst_width, dst_height,
2465 /* YUV to YUV planar */
2466 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2467 int x_shift, y_shift, w, h, xy_shift;
2468 void (*resize_func)(uint8_t *dst, int dst_wrap,
2469 const uint8_t *src, int src_wrap,
2470 int width, int height);
2472 /* compute chroma size of the smallest dimensions */
2475 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2476 w >>= dst_pix->x_chroma_shift;
2478 w >>= src_pix->x_chroma_shift;
2479 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2480 h >>= dst_pix->y_chroma_shift;
2482 h >>= src_pix->y_chroma_shift;
2484 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2485 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2486 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2487 /* there must be filters for conversion at least from and to
2491 resize_func = ff_img_copy_plane;
2494 resize_func = shrink21;
2497 resize_func = shrink41;
2500 resize_func = shrink12;
2503 resize_func = ff_shrink22;
2506 resize_func = ff_shrink44;
2509 resize_func = grow21;
2512 resize_func = grow12;
2515 resize_func = grow41;
2518 resize_func = grow22;
2521 resize_func = grow44;
2524 resize_func = conv411;
2527 /* currently not handled */
2528 goto no_chroma_filter;
2531 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2532 src->data[0], src->linesize[0],
2533 dst_width, dst_height);
2535 for(i = 1;i <= 2; i++)
2536 resize_func(dst->data[i], dst->linesize[i],
2537 src->data[i], src->linesize[i],
2538 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2539 /* if yuv color space conversion is needed, we do it here on
2540 the destination image */
2541 if (dst_pix->color_type != src_pix->color_type) {
2542 const uint8_t *y_table, *c_table;
2543 if (dst_pix->color_type == FF_COLOR_YUV) {
2544 y_table = y_jpeg_to_ccir;
2545 c_table = c_jpeg_to_ccir;
2547 y_table = y_ccir_to_jpeg;
2548 c_table = c_ccir_to_jpeg;
2550 img_apply_table(dst->data[0], dst->linesize[0],
2551 dst->data[0], dst->linesize[0],
2552 dst_width, dst_height,
2555 for(i = 1;i <= 2; i++)
2556 img_apply_table(dst->data[i], dst->linesize[i],
2557 dst->data[i], dst->linesize[i],
2558 dst_width>>dst_pix->x_chroma_shift,
2559 dst_height>>dst_pix->y_chroma_shift,
2566 /* try to use an intermediate format */
2567 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2568 dst_pix_fmt == PIX_FMT_YUYV422) {
2569 /* specific case: convert to YUV422P first */
2570 int_pix_fmt = PIX_FMT_YUV422P;
2571 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2572 dst_pix_fmt == PIX_FMT_UYVY422) {
2573 /* specific case: convert to YUV422P first */
2574 int_pix_fmt = PIX_FMT_YUV422P;
2575 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2576 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2577 /* specific case: convert to YUV411P first */
2578 int_pix_fmt = PIX_FMT_YUV411P;
2579 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2580 src_pix_fmt != PIX_FMT_GRAY8) ||
2581 (dst_pix->color_type == FF_COLOR_GRAY &&
2582 dst_pix_fmt != PIX_FMT_GRAY8)) {
2583 /* gray8 is the normalized format */
2584 int_pix_fmt = PIX_FMT_GRAY8;
2585 } else if ((is_yuv_planar(src_pix) &&
2586 src_pix_fmt != PIX_FMT_YUV444P &&
2587 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2588 /* yuv444 is the normalized format */
2589 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2590 int_pix_fmt = PIX_FMT_YUVJ444P;
2592 int_pix_fmt = PIX_FMT_YUV444P;
2593 } else if ((is_yuv_planar(dst_pix) &&
2594 dst_pix_fmt != PIX_FMT_YUV444P &&
2595 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2596 /* yuv444 is the normalized format */
2597 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2598 int_pix_fmt = PIX_FMT_YUVJ444P;
2600 int_pix_fmt = PIX_FMT_YUV444P;
2602 /* the two formats are rgb or gray8 or yuv[j]444p */
2603 if (src_pix->is_alpha && dst_pix->is_alpha)
2604 int_pix_fmt = PIX_FMT_RGB32;
2606 int_pix_fmt = PIX_FMT_RGB24;
2608 if (src_pix_fmt == int_pix_fmt)
2610 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2613 if (img_convert(tmp, int_pix_fmt,
2614 src, src_pix_fmt, src_width, src_height) < 0)
2616 if (img_convert(dst, dst_pix_fmt,
2617 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2621 avpicture_free(tmp);
2626 /* NOTE: we scan all the pixels to have an exact information */
2627 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2629 const unsigned char *p;
2630 int src_wrap, ret, x, y;
2632 uint32_t *palette = (uint32_t *)src->data[1];
2635 src_wrap = src->linesize[0] - width;
2637 for(y=0;y<height;y++) {
2638 for(x=0;x<width;x++) {
2639 a = palette[p[0]] >> 24;
2641 ret |= FF_ALPHA_TRANSP;
2642 } else if (a != 0xff) {
2643 ret |= FF_ALPHA_SEMI_TRANSP;
2652 int img_get_alpha_info(const AVPicture *src,
2653 int pix_fmt, int width, int height)
2655 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2658 pf = &pix_fmt_info[pix_fmt];
2659 /* no alpha can be represented in format */
2664 ret = get_alpha_info_rgb32(src, width, height);
2667 ret = get_alpha_info_pal8(src, width, height);
2670 /* we do not know, so everything is indicated */
2671 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2678 #define DEINT_INPLACE_LINE_LUM \
2679 movd_m2r(lum_m4[0],mm0);\
2680 movd_m2r(lum_m3[0],mm1);\
2681 movd_m2r(lum_m2[0],mm2);\
2682 movd_m2r(lum_m1[0],mm3);\
2683 movd_m2r(lum[0],mm4);\
2684 punpcklbw_r2r(mm7,mm0);\
2685 movd_r2m(mm2,lum_m4[0]);\
2686 punpcklbw_r2r(mm7,mm1);\
2687 punpcklbw_r2r(mm7,mm2);\
2688 punpcklbw_r2r(mm7,mm3);\
2689 punpcklbw_r2r(mm7,mm4);\
2690 paddw_r2r(mm3,mm1);\
2692 paddw_r2r(mm4,mm0);\
2694 paddw_r2r(mm6,mm2);\
2695 paddw_r2r(mm2,mm1);\
2696 psubusw_r2r(mm0,mm1);\
2698 packuswb_r2r(mm7,mm1);\
2699 movd_r2m(mm1,lum_m2[0]);
2701 #define DEINT_LINE_LUM \
2702 movd_m2r(lum_m4[0],mm0);\
2703 movd_m2r(lum_m3[0],mm1);\
2704 movd_m2r(lum_m2[0],mm2);\
2705 movd_m2r(lum_m1[0],mm3);\
2706 movd_m2r(lum[0],mm4);\
2707 punpcklbw_r2r(mm7,mm0);\
2708 punpcklbw_r2r(mm7,mm1);\
2709 punpcklbw_r2r(mm7,mm2);\
2710 punpcklbw_r2r(mm7,mm3);\
2711 punpcklbw_r2r(mm7,mm4);\
2712 paddw_r2r(mm3,mm1);\
2714 paddw_r2r(mm4,mm0);\
2716 paddw_r2r(mm6,mm2);\
2717 paddw_r2r(mm2,mm1);\
2718 psubusw_r2r(mm0,mm1);\
2720 packuswb_r2r(mm7,mm1);\
2721 movd_r2m(mm1,dst[0]);
2724 /* filter parameters: [-1 4 2 4 -1] // 8 */
2725 static void deinterlace_line(uint8_t *dst,
2726 const uint8_t *lum_m4, const uint8_t *lum_m3,
2727 const uint8_t *lum_m2, const uint8_t *lum_m1,
2732 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2735 for(;size > 0;size--) {
2737 sum += lum_m3[0] << 2;
2738 sum += lum_m2[0] << 1;
2739 sum += lum_m1[0] << 2;
2741 dst[0] = cm[(sum + 4) >> 3];
2753 movq_m2r(ff_pw_4,mm6);
2755 for (;size > 3; size-=4) {
2766 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2770 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2773 for(;size > 0;size--) {
2775 sum += lum_m3[0] << 2;
2776 sum += lum_m2[0] << 1;
2777 lum_m4[0]=lum_m2[0];
2778 sum += lum_m1[0] << 2;
2780 lum_m2[0] = cm[(sum + 4) >> 3];
2791 movq_m2r(ff_pw_4,mm6);
2793 for (;size > 3; size-=4) {
2794 DEINT_INPLACE_LINE_LUM
2804 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2805 top field is copied as is, but the bottom field is deinterlaced
2806 against the top field. */
2807 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2808 const uint8_t *src1, int src_wrap,
2809 int width, int height)
2811 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2816 src_0=&src_m1[src_wrap];
2817 src_p1=&src_0[src_wrap];
2818 src_p2=&src_p1[src_wrap];
2819 for(y=0;y<(height-2);y+=2) {
2820 memcpy(dst,src_m1,width);
2822 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2826 src_p1 += 2*src_wrap;
2827 src_p2 += 2*src_wrap;
2830 memcpy(dst,src_m1,width);
2833 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2836 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2837 int width, int height)
2839 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2842 buf = (uint8_t*)av_malloc(width);
2845 memcpy(buf,src_m1,width);
2846 src_0=&src_m1[src_wrap];
2847 src_p1=&src_0[src_wrap];
2848 src_p2=&src_p1[src_wrap];
2849 for(y=0;y<(height-2);y+=2) {
2850 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2853 src_p1 += 2*src_wrap;
2854 src_p2 += 2*src_wrap;
2857 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2861 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2862 int pix_fmt, int width, int height)
2866 if (pix_fmt != PIX_FMT_YUV420P &&
2867 pix_fmt != PIX_FMT_YUV422P &&
2868 pix_fmt != PIX_FMT_YUV444P &&
2869 pix_fmt != PIX_FMT_YUV411P &&
2870 pix_fmt != PIX_FMT_GRAY8)
2872 if ((width & 3) != 0 || (height & 3) != 0)
2878 case PIX_FMT_YUV420P:
2882 case PIX_FMT_YUV422P:
2885 case PIX_FMT_YUV411P:
2891 if (pix_fmt == PIX_FMT_GRAY8) {
2896 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2899 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2900 src->data[i], src->linesize[i],