2 * Misc image conversion routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @file libavcodec/imgconvert.c
24 * misc image conversion routines
28 * - write 'ffimg' program to test all the image related stuff
29 * - move all api to slice based system
30 * - integrate deinterlacing, postprocessing and scaling in the conversion process
35 #include "colorspace.h"
39 #include "x86/dsputil_mmx.h"
42 #define xglue(x, y) x ## y
43 #define glue(x, y) xglue(x, y)
45 #define FF_COLOR_RGB 0 /**< RGB color space */
46 #define FF_COLOR_GRAY 1 /**< gray color space */
47 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
48 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
50 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
51 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
52 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
54 typedef struct PixFmtInfo {
56 uint8_t nb_channels; /**< number of channels (including alpha) */
57 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
58 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
59 uint8_t is_alpha : 1; /**< true if alpha can be specified */
60 uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
61 uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
62 uint8_t depth; /**< bit depth of the color components */
65 /* this table gives more information about formats */
66 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
71 .color_type = FF_COLOR_YUV,
72 .pixel_type = FF_PIXEL_PLANAR,
74 .x_chroma_shift = 1, .y_chroma_shift = 1,
79 .color_type = FF_COLOR_YUV,
80 .pixel_type = FF_PIXEL_PLANAR,
82 .x_chroma_shift = 1, .y_chroma_shift = 0,
87 .color_type = FF_COLOR_YUV,
88 .pixel_type = FF_PIXEL_PLANAR,
90 .x_chroma_shift = 0, .y_chroma_shift = 0,
95 .color_type = FF_COLOR_YUV,
96 .pixel_type = FF_PIXEL_PACKED,
98 .x_chroma_shift = 1, .y_chroma_shift = 0,
100 [PIX_FMT_UYVY422] = {
103 .color_type = FF_COLOR_YUV,
104 .pixel_type = FF_PIXEL_PACKED,
106 .x_chroma_shift = 1, .y_chroma_shift = 0,
108 [PIX_FMT_YUV410P] = {
111 .color_type = FF_COLOR_YUV,
112 .pixel_type = FF_PIXEL_PLANAR,
114 .x_chroma_shift = 2, .y_chroma_shift = 2,
116 [PIX_FMT_YUV411P] = {
119 .color_type = FF_COLOR_YUV,
120 .pixel_type = FF_PIXEL_PLANAR,
122 .x_chroma_shift = 2, .y_chroma_shift = 0,
124 [PIX_FMT_YUV440P] = {
127 .color_type = FF_COLOR_YUV,
128 .pixel_type = FF_PIXEL_PLANAR,
130 .x_chroma_shift = 0, .y_chroma_shift = 1,
133 /* YUV formats with alpha plane */
134 [PIX_FMT_YUVA420P] = {
137 .color_type = FF_COLOR_YUV,
138 .pixel_type = FF_PIXEL_PLANAR,
140 .x_chroma_shift = 1, .y_chroma_shift = 1,
144 [PIX_FMT_YUVJ420P] = {
147 .color_type = FF_COLOR_YUV_JPEG,
148 .pixel_type = FF_PIXEL_PLANAR,
150 .x_chroma_shift = 1, .y_chroma_shift = 1,
152 [PIX_FMT_YUVJ422P] = {
155 .color_type = FF_COLOR_YUV_JPEG,
156 .pixel_type = FF_PIXEL_PLANAR,
158 .x_chroma_shift = 1, .y_chroma_shift = 0,
160 [PIX_FMT_YUVJ444P] = {
163 .color_type = FF_COLOR_YUV_JPEG,
164 .pixel_type = FF_PIXEL_PLANAR,
166 .x_chroma_shift = 0, .y_chroma_shift = 0,
168 [PIX_FMT_YUVJ440P] = {
171 .color_type = FF_COLOR_YUV_JPEG,
172 .pixel_type = FF_PIXEL_PLANAR,
174 .x_chroma_shift = 0, .y_chroma_shift = 1,
181 .color_type = FF_COLOR_RGB,
182 .pixel_type = FF_PIXEL_PACKED,
184 .x_chroma_shift = 0, .y_chroma_shift = 0,
189 .color_type = FF_COLOR_RGB,
190 .pixel_type = FF_PIXEL_PACKED,
192 .x_chroma_shift = 0, .y_chroma_shift = 0,
196 .nb_channels = 4, .is_alpha = 1,
197 .color_type = FF_COLOR_RGB,
198 .pixel_type = FF_PIXEL_PACKED,
200 .x_chroma_shift = 0, .y_chroma_shift = 0,
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_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
449 for(i=0; i<256; i++){
463 case PIX_FMT_RGB4_BYTE:
468 case PIX_FMT_BGR4_BYTE:
477 pal[i] = b + (g<<8) + (r<<16);
483 int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
486 const PixFmtInfo *pinfo;
488 memset(picture->linesize, 0, sizeof(picture->linesize));
490 pinfo = &pix_fmt_info[pix_fmt];
492 case PIX_FMT_YUV420P:
493 case PIX_FMT_YUV422P:
494 case PIX_FMT_YUV444P:
495 case PIX_FMT_YUV410P:
496 case PIX_FMT_YUV411P:
497 case PIX_FMT_YUV440P:
498 case PIX_FMT_YUVJ420P:
499 case PIX_FMT_YUVJ422P:
500 case PIX_FMT_YUVJ444P:
501 case PIX_FMT_YUVJ440P:
502 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
503 picture->linesize[0] = width;
504 picture->linesize[1] = w2;
505 picture->linesize[2] = w2;
507 case PIX_FMT_YUVA420P:
508 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
509 picture->linesize[0] = width;
510 picture->linesize[1] = w2;
511 picture->linesize[2] = w2;
512 picture->linesize[3] = width;
516 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
517 picture->linesize[0] = width;
518 picture->linesize[1] = w2;
522 picture->linesize[0] = width * 3;
526 case PIX_FMT_RGB32_1:
527 case PIX_FMT_BGR32_1:
528 picture->linesize[0] = width * 4;
530 case PIX_FMT_GRAY16BE:
531 case PIX_FMT_GRAY16LE:
536 case PIX_FMT_YUYV422:
537 picture->linesize[0] = width * 2;
539 case PIX_FMT_UYVY422:
540 picture->linesize[0] = width * 2;
542 case PIX_FMT_UYYVYY411:
543 picture->linesize[0] = width + width/2;
547 picture->linesize[0] = width / 2;
549 case PIX_FMT_MONOWHITE:
550 case PIX_FMT_MONOBLACK:
551 picture->linesize[0] = (width + 7) >> 3;
556 case PIX_FMT_RGB4_BYTE:
557 case PIX_FMT_BGR4_BYTE:
559 picture->linesize[0] = width;
560 picture->linesize[1] = 4;
568 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
572 const PixFmtInfo *pinfo;
574 pinfo = &pix_fmt_info[pix_fmt];
575 size = picture->linesize[0] * height;
577 case PIX_FMT_YUV420P:
578 case PIX_FMT_YUV422P:
579 case PIX_FMT_YUV444P:
580 case PIX_FMT_YUV410P:
581 case PIX_FMT_YUV411P:
582 case PIX_FMT_YUV440P:
583 case PIX_FMT_YUVJ420P:
584 case PIX_FMT_YUVJ422P:
585 case PIX_FMT_YUVJ444P:
586 case PIX_FMT_YUVJ440P:
587 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
588 size2 = picture->linesize[1] * h2;
589 picture->data[0] = ptr;
590 picture->data[1] = picture->data[0] + size;
591 picture->data[2] = picture->data[1] + size2;
592 picture->data[3] = NULL;
593 return size + 2 * size2;
594 case PIX_FMT_YUVA420P:
595 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
596 size2 = picture->linesize[1] * h2;
597 picture->data[0] = ptr;
598 picture->data[1] = picture->data[0] + size;
599 picture->data[2] = picture->data[1] + size2;
600 picture->data[3] = picture->data[1] + size2 + size2;
601 return 2 * size + 2 * size2;
604 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
605 size2 = picture->linesize[1] * h2 * 2;
606 picture->data[0] = ptr;
607 picture->data[1] = picture->data[0] + size;
608 picture->data[2] = NULL;
609 picture->data[3] = NULL;
610 return size + 2 * size2;
615 case PIX_FMT_RGB32_1:
616 case PIX_FMT_BGR32_1:
617 case PIX_FMT_GRAY16BE:
618 case PIX_FMT_GRAY16LE:
623 case PIX_FMT_YUYV422:
624 case PIX_FMT_UYVY422:
625 case PIX_FMT_UYYVYY411:
628 case PIX_FMT_MONOWHITE:
629 case PIX_FMT_MONOBLACK:
630 picture->data[0] = ptr;
631 picture->data[1] = NULL;
632 picture->data[2] = NULL;
633 picture->data[3] = NULL;
638 case PIX_FMT_RGB4_BYTE:
639 case PIX_FMT_BGR4_BYTE:
641 size2 = (size + 3) & ~3;
642 picture->data[0] = ptr;
643 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
644 picture->data[2] = NULL;
645 picture->data[3] = NULL;
646 return size2 + 256 * 4;
648 picture->data[0] = NULL;
649 picture->data[1] = NULL;
650 picture->data[2] = NULL;
651 picture->data[3] = NULL;
656 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
657 int pix_fmt, int width, int height)
660 if(avcodec_check_dimensions(NULL, width, height))
663 if (ff_fill_linesize(picture, pix_fmt, width))
666 return ff_fill_pointer(picture, ptr, pix_fmt, height);
669 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
670 unsigned char *dest, int dest_size)
672 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
673 int i, j, w, h, data_planes;
674 const unsigned char* s;
675 int size = avpicture_get_size(pix_fmt, width, height);
677 if (size > dest_size || size < 0)
680 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
681 if (pix_fmt == PIX_FMT_YUYV422 ||
682 pix_fmt == PIX_FMT_UYVY422 ||
683 pix_fmt == PIX_FMT_BGR565 ||
684 pix_fmt == PIX_FMT_BGR555 ||
685 pix_fmt == PIX_FMT_RGB565 ||
686 pix_fmt == PIX_FMT_RGB555)
688 else if (pix_fmt == PIX_FMT_UYYVYY411)
690 else if (pix_fmt == PIX_FMT_PAL8)
693 w = width * (pf->depth * pf->nb_channels / 8);
698 data_planes = pf->nb_channels;
699 w = (width*pf->depth + 7)/8;
703 for (i=0; i<data_planes; i++) {
705 w = width >> pf->x_chroma_shift;
706 h = height >> pf->y_chroma_shift;
712 s += src->linesize[i];
716 if (pf->pixel_type == FF_PIXEL_PALETTE)
717 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
722 int avpicture_get_size(int pix_fmt, int width, int height)
724 AVPicture dummy_pict;
725 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
728 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
731 const PixFmtInfo *pf, *ps;
734 ps = &pix_fmt_info[src_pix_fmt];
735 pf = &pix_fmt_info[dst_pix_fmt];
739 pf = &pix_fmt_info[dst_pix_fmt];
740 if (pf->depth < ps->depth ||
741 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
742 loss |= FF_LOSS_DEPTH;
743 if (pf->x_chroma_shift > ps->x_chroma_shift ||
744 pf->y_chroma_shift > ps->y_chroma_shift)
745 loss |= FF_LOSS_RESOLUTION;
746 switch(pf->color_type) {
748 if (ps->color_type != FF_COLOR_RGB &&
749 ps->color_type != FF_COLOR_GRAY)
750 loss |= FF_LOSS_COLORSPACE;
753 if (ps->color_type != FF_COLOR_GRAY)
754 loss |= FF_LOSS_COLORSPACE;
757 if (ps->color_type != FF_COLOR_YUV)
758 loss |= FF_LOSS_COLORSPACE;
760 case FF_COLOR_YUV_JPEG:
761 if (ps->color_type != FF_COLOR_YUV_JPEG &&
762 ps->color_type != FF_COLOR_YUV &&
763 ps->color_type != FF_COLOR_GRAY)
764 loss |= FF_LOSS_COLORSPACE;
768 if (ps->color_type != pf->color_type)
769 loss |= FF_LOSS_COLORSPACE;
772 if (pf->color_type == FF_COLOR_GRAY &&
773 ps->color_type != FF_COLOR_GRAY)
774 loss |= FF_LOSS_CHROMA;
775 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
776 loss |= FF_LOSS_ALPHA;
777 if (pf->pixel_type == FF_PIXEL_PALETTE &&
778 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
779 loss |= FF_LOSS_COLORQUANT;
783 static int avg_bits_per_pixel(int pix_fmt)
786 const PixFmtInfo *pf;
788 pf = &pix_fmt_info[pix_fmt];
789 switch(pf->pixel_type) {
790 case FF_PIXEL_PACKED:
792 case PIX_FMT_YUYV422:
793 case PIX_FMT_UYVY422:
800 case PIX_FMT_UYYVYY411:
804 bits = pf->depth * pf->nb_channels;
808 case FF_PIXEL_PLANAR:
809 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
810 bits = pf->depth * pf->nb_channels;
812 bits = pf->depth + ((2 * pf->depth) >>
813 (pf->x_chroma_shift + pf->y_chroma_shift));
816 case FF_PIXEL_PALETTE:
826 static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
831 int dist, i, loss, min_dist, dst_pix_fmt;
833 /* find exact color match with smallest size */
835 min_dist = 0x7fffffff;
836 for(i = 0;i < PIX_FMT_NB; i++) {
837 if (pix_fmt_mask & (1ULL << i)) {
838 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
840 dist = avg_bits_per_pixel(i);
841 if (dist < min_dist) {
851 int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
852 int has_alpha, int *loss_ptr)
854 int dst_pix_fmt, loss_mask, i;
855 static const int loss_mask_order[] = {
856 ~0, /* no loss first */
859 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
865 /* try with successive loss */
868 loss_mask = loss_mask_order[i++];
869 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
870 has_alpha, loss_mask);
871 if (dst_pix_fmt >= 0)
879 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
883 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
884 const uint8_t *src, int src_wrap,
885 int width, int height)
889 for(;height > 0; height--) {
890 memcpy(dst, src, width);
896 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
899 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
901 pf = &pix_fmt_info[pix_fmt];
902 switch(pf->pixel_type) {
903 case FF_PIXEL_PACKED:
905 case PIX_FMT_YUYV422:
906 case PIX_FMT_UYVY422:
913 case PIX_FMT_UYYVYY411:
917 bits = pf->depth * pf->nb_channels;
920 return (width * bits + 7) >> 3;
922 case FF_PIXEL_PLANAR:
923 if (plane == 1 || plane == 2)
924 width= -((-width)>>pf->x_chroma_shift);
926 return (width * pf->depth + 7) >> 3;
928 case FF_PIXEL_PALETTE:
937 void av_picture_copy(AVPicture *dst, const AVPicture *src,
938 int pix_fmt, int width, int height)
941 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
943 pf = &pix_fmt_info[pix_fmt];
944 switch(pf->pixel_type) {
945 case FF_PIXEL_PACKED:
946 case FF_PIXEL_PLANAR:
947 for(i = 0; i < pf->nb_channels; i++) {
949 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
951 if (i == 1 || i == 2) {
952 h= -((-height)>>pf->y_chroma_shift);
954 ff_img_copy_plane(dst->data[i], dst->linesize[i],
955 src->data[i], src->linesize[i],
959 case FF_PIXEL_PALETTE:
960 ff_img_copy_plane(dst->data[0], dst->linesize[0],
961 src->data[0], src->linesize[0],
963 /* copy the palette */
964 ff_img_copy_plane(dst->data[1], dst->linesize[1],
965 src->data[1], src->linesize[1],
971 /* XXX: totally non optimized */
973 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
974 int width, int height)
976 const uint8_t *p, *p1;
977 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
985 for(;height >= 1; height -= 2) {
990 for(w = width; w >= 2; w -= 2) {
1007 p1 += src->linesize[0];
1008 lum1 += dst->linesize[0];
1012 for(w = width; w >= 2; w -= 2) {
1021 p1 += src->linesize[0];
1022 lum1 += dst->linesize[0];
1024 cb1 += dst->linesize[1];
1025 cr1 += dst->linesize[2];
1029 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
1030 int width, int height)
1032 const uint8_t *p, *p1;
1033 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1038 lum1 = dst->data[0];
1042 for(;height >= 1; height -= 2) {
1047 for(w = width; w >= 2; w -= 2) {
1064 p1 += src->linesize[0];
1065 lum1 += dst->linesize[0];
1069 for(w = width; w >= 2; w -= 2) {
1078 p1 += src->linesize[0];
1079 lum1 += dst->linesize[0];
1081 cb1 += dst->linesize[1];
1082 cr1 += dst->linesize[2];
1087 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1088 int width, int height)
1090 const uint8_t *p, *p1;
1091 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1095 lum1 = dst->data[0];
1098 for(;height > 0; height--) {
1103 for(w = width; w >= 2; w -= 2) {
1113 p1 += src->linesize[0];
1114 lum1 += dst->linesize[0];
1115 cb1 += dst->linesize[1];
1116 cr1 += dst->linesize[2];
1121 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1122 int width, int height)
1124 const uint8_t *p, *p1;
1125 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1129 lum1 = dst->data[0];
1132 for(;height > 0; height--) {
1137 for(w = width; w >= 2; w -= 2) {
1147 p1 += src->linesize[0];
1148 lum1 += dst->linesize[0];
1149 cb1 += dst->linesize[1];
1150 cr1 += dst->linesize[2];
1154 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1155 int width, int height)
1158 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1162 lum1 = src->data[0];
1165 for(;height > 0; height--) {
1170 for(w = width; w >= 2; w -= 2) {
1180 p1 += dst->linesize[0];
1181 lum1 += src->linesize[0];
1182 cb1 += src->linesize[1];
1183 cr1 += src->linesize[2];
1187 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1188 int width, int height)
1191 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1195 lum1 = src->data[0];
1198 for(;height > 0; height--) {
1203 for(w = width; w >= 2; w -= 2) {
1213 p1 += dst->linesize[0];
1214 lum1 += src->linesize[0];
1215 cb1 += src->linesize[1];
1216 cr1 += src->linesize[2];
1220 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1221 int width, int height)
1223 const uint8_t *p, *p1;
1224 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1228 lum1 = dst->data[0];
1231 for(;height > 0; height--) {
1236 for(w = width; w >= 4; w -= 4) {
1248 p1 += src->linesize[0];
1249 lum1 += dst->linesize[0];
1250 cb1 += dst->linesize[1];
1251 cr1 += dst->linesize[2];
1256 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1257 int width, int height)
1260 uint8_t *line1, *line2, *linesrc = dst->data[0];
1261 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1262 uint8_t *cb1, *cb2 = src->data[1];
1263 uint8_t *cr1, *cr2 = src->data[2];
1265 for(h = height / 2; h--;) {
1267 line2 = linesrc + dst->linesize[0];
1270 lum2 = lumsrc + src->linesize[0];
1275 for(w = width / 2; w--;) {
1276 *line1++ = *lum1++; *line2++ = *lum2++;
1277 *line1++ = *line2++ = *cb1++;
1278 *line1++ = *lum1++; *line2++ = *lum2++;
1279 *line1++ = *line2++ = *cr1++;
1282 linesrc += dst->linesize[0] * 2;
1283 lumsrc += src->linesize[0] * 2;
1284 cb2 += src->linesize[1];
1285 cr2 += src->linesize[2];
1289 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1290 int width, int height)
1293 uint8_t *line1, *line2, *linesrc = dst->data[0];
1294 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1295 uint8_t *cb1, *cb2 = src->data[1];
1296 uint8_t *cr1, *cr2 = src->data[2];
1298 for(h = height / 2; h--;) {
1300 line2 = linesrc + dst->linesize[0];
1303 lum2 = lumsrc + src->linesize[0];
1308 for(w = width / 2; w--;) {
1309 *line1++ = *line2++ = *cb1++;
1310 *line1++ = *lum1++; *line2++ = *lum2++;
1311 *line1++ = *line2++ = *cr1++;
1312 *line1++ = *lum1++; *line2++ = *lum2++;
1315 linesrc += dst->linesize[0] * 2;
1316 lumsrc += src->linesize[0] * 2;
1317 cb2 += src->linesize[1];
1318 cr2 += src->linesize[2];
1323 void ff_shrink22(uint8_t *dst, int dst_wrap,
1324 const uint8_t *src, int src_wrap,
1325 int width, int height)
1328 const uint8_t *s1, *s2;
1331 for(;height > 0; height--) {
1335 for(w = width;w >= 4; w-=4) {
1336 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1337 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1338 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1339 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1345 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1350 src += 2 * src_wrap;
1356 void ff_shrink44(uint8_t *dst, int dst_wrap,
1357 const uint8_t *src, int src_wrap,
1358 int width, int height)
1361 const uint8_t *s1, *s2, *s3, *s4;
1364 for(;height > 0; height--) {
1370 for(w = width;w > 0; w--) {
1371 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1372 s2[0] + s2[1] + s2[2] + s2[3] +
1373 s3[0] + s3[1] + s3[2] + s3[3] +
1374 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1381 src += 4 * src_wrap;
1387 void ff_shrink88(uint8_t *dst, int dst_wrap,
1388 const uint8_t *src, int src_wrap,
1389 int width, int height)
1393 for(;height > 0; height--) {
1394 for(w = width;w > 0; w--) {
1397 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1400 *(dst++) = (tmp + 32)>>6;
1401 src += 8 - 8*src_wrap;
1403 src += 8*src_wrap - 8*width;
1404 dst += dst_wrap - width;
1408 /* XXX: add jpeg quantize code */
1410 #define TRANSP_INDEX (6*6*6)
1412 /* this is maybe slow, but allows for extensions */
1413 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1415 return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
1418 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1421 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1424 pal = (uint32_t *)palette;
1426 for(r = 0; r < 6; r++) {
1427 for(g = 0; g < 6; g++) {
1428 for(b = 0; b < 6; b++) {
1429 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1430 (pal_value[g] << 8) | pal_value[b];
1437 pal[i++] = 0xff000000;
1440 /* copy bit n to bits 0 ... n - 1 */
1441 static inline unsigned int bitcopy_n(unsigned int a, int n)
1444 mask = (1 << n) - 1;
1445 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1448 /* rgb555 handling */
1450 #define RGB_NAME rgb555
1452 #define RGB_IN(r, g, b, s)\
1454 unsigned int v = ((const uint16_t *)(s))[0];\
1455 r = bitcopy_n(v >> (10 - 3), 3);\
1456 g = bitcopy_n(v >> (5 - 3), 3);\
1457 b = bitcopy_n(v << 3, 3);\
1461 #define RGB_OUT(d, r, g, b)\
1463 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1468 #include "imgconvert_template.c"
1470 /* rgb565 handling */
1472 #define RGB_NAME rgb565
1474 #define RGB_IN(r, g, b, s)\
1476 unsigned int v = ((const uint16_t *)(s))[0];\
1477 r = bitcopy_n(v >> (11 - 3), 3);\
1478 g = bitcopy_n(v >> (5 - 2), 2);\
1479 b = bitcopy_n(v << 3, 3);\
1482 #define RGB_OUT(d, r, g, b)\
1484 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1489 #include "imgconvert_template.c"
1491 /* bgr24 handling */
1493 #define RGB_NAME bgr24
1495 #define RGB_IN(r, g, b, s)\
1502 #define RGB_OUT(d, r, g, b)\
1511 #include "imgconvert_template.c"
1517 /* rgb24 handling */
1519 #define RGB_NAME rgb24
1522 #define RGB_IN(r, g, b, s)\
1529 #define RGB_OUT(d, r, g, b)\
1538 #include "imgconvert_template.c"
1540 /* rgb32 handling */
1542 #define RGB_NAME rgb32
1545 #define RGB_IN(r, g, b, s)\
1547 unsigned int v = ((const uint32_t *)(s))[0];\
1548 r = (v >> 16) & 0xff;\
1549 g = (v >> 8) & 0xff;\
1553 #define RGBA_IN(r, g, b, a, s)\
1555 unsigned int v = ((const uint32_t *)(s))[0];\
1556 a = (v >> 24) & 0xff;\
1557 r = (v >> 16) & 0xff;\
1558 g = (v >> 8) & 0xff;\
1562 #define RGBA_OUT(d, r, g, b, a)\
1564 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1569 #include "imgconvert_template.c"
1571 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1572 int width, int height, int xor_mask)
1574 const unsigned char *p;
1576 int v, dst_wrap, src_wrap;
1580 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1583 dst_wrap = dst->linesize[0] - width;
1584 for(y=0;y<height;y++) {
1587 v = *p++ ^ xor_mask;
1589 q[1] = -((v >> 6) & 1);
1590 q[2] = -((v >> 5) & 1);
1591 q[3] = -((v >> 4) & 1);
1592 q[4] = -((v >> 3) & 1);
1593 q[5] = -((v >> 2) & 1);
1594 q[6] = -((v >> 1) & 1);
1595 q[7] = -((v >> 0) & 1);
1600 v = *p++ ^ xor_mask;
1602 q[0] = -((v >> 7) & 1);
1612 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1613 int width, int height)
1615 mono_to_gray(dst, src, width, height, 0xff);
1618 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1619 int width, int height)
1621 mono_to_gray(dst, src, width, height, 0x00);
1624 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1625 int width, int height, int xor_mask)
1630 int j, b, v, n1, src_wrap, dst_wrap, y;
1633 src_wrap = src->linesize[0] - width;
1636 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1638 for(y=0;y<height;y++) {
1645 v = (v << 1) | (b >> 7);
1647 d[0] = v ^ xor_mask;
1657 v = (v << 1) | (b >> 7);
1660 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1668 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1669 int width, int height)
1671 gray_to_mono(dst, src, width, height, 0xff);
1674 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1675 int width, int height)
1677 gray_to_mono(dst, src, width, height, 0x00);
1680 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1681 int width, int height)
1683 int x, y, src_wrap, dst_wrap;
1686 src_wrap = src->linesize[0] - width;
1688 dst_wrap = dst->linesize[0] - width * 2;
1689 for(y=0; y<height; y++){
1690 for(x=0; x<width; x++){
1699 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1700 int width, int height)
1702 int x, y, src_wrap, dst_wrap;
1705 src_wrap = src->linesize[0] - width * 2;
1707 dst_wrap = dst->linesize[0] - width;
1708 for(y=0; y<height; y++){
1709 for(x=0; x<width; x++){
1718 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1719 int width, int height)
1721 gray16_to_gray(dst, src, width, height);
1724 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1725 int width, int height)
1727 AVPicture tmpsrc = *src;
1729 gray16_to_gray(dst, &tmpsrc, width, height);
1732 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1733 int width, int height)
1735 int x, y, src_wrap, dst_wrap;
1737 s = (uint16_t*)src->data[0];
1738 src_wrap = (src->linesize[0] - width * 2)/2;
1739 d = (uint16_t*)dst->data[0];
1740 dst_wrap = (dst->linesize[0] - width * 2)/2;
1741 for(y=0; y<height; y++){
1742 for(x=0; x<width; x++){
1743 *d++ = bswap_16(*s++);
1751 typedef struct ConvertEntry {
1752 void (*convert)(AVPicture *dst,
1753 const AVPicture *src, int width, int height);
1756 /* Add each new conversion function in this table. In order to be able
1757 to convert from any format to any format, the following constraints
1760 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1762 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1764 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1766 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1769 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1771 The other conversion functions are just optimizations for common cases.
1773 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1774 [PIX_FMT_YUV420P] = {
1775 [PIX_FMT_YUYV422] = {
1776 .convert = yuv420p_to_yuyv422,
1778 [PIX_FMT_RGB555] = {
1779 .convert = yuv420p_to_rgb555
1781 [PIX_FMT_RGB565] = {
1782 .convert = yuv420p_to_rgb565
1785 .convert = yuv420p_to_bgr24
1788 .convert = yuv420p_to_rgb24
1791 .convert = yuv420p_to_rgb32
1793 [PIX_FMT_UYVY422] = {
1794 .convert = yuv420p_to_uyvy422,
1797 [PIX_FMT_YUV422P] = {
1798 [PIX_FMT_YUYV422] = {
1799 .convert = yuv422p_to_yuyv422,
1801 [PIX_FMT_UYVY422] = {
1802 .convert = yuv422p_to_uyvy422,
1805 [PIX_FMT_YUV444P] = {
1807 .convert = yuv444p_to_rgb24
1810 [PIX_FMT_YUVJ420P] = {
1811 [PIX_FMT_RGB555] = {
1812 .convert = yuvj420p_to_rgb555
1814 [PIX_FMT_RGB565] = {
1815 .convert = yuvj420p_to_rgb565
1818 .convert = yuvj420p_to_bgr24
1821 .convert = yuvj420p_to_rgb24
1824 .convert = yuvj420p_to_rgb32
1827 [PIX_FMT_YUVJ444P] = {
1829 .convert = yuvj444p_to_rgb24
1832 [PIX_FMT_YUYV422] = {
1833 [PIX_FMT_YUV420P] = {
1834 .convert = yuyv422_to_yuv420p,
1836 [PIX_FMT_YUV422P] = {
1837 .convert = yuyv422_to_yuv422p,
1840 [PIX_FMT_UYVY422] = {
1841 [PIX_FMT_YUV420P] = {
1842 .convert = uyvy422_to_yuv420p,
1844 [PIX_FMT_YUV422P] = {
1845 .convert = uyvy422_to_yuv422p,
1849 [PIX_FMT_YUV420P] = {
1850 .convert = rgb24_to_yuv420p
1852 [PIX_FMT_RGB565] = {
1853 .convert = rgb24_to_rgb565
1855 [PIX_FMT_RGB555] = {
1856 .convert = rgb24_to_rgb555
1859 .convert = rgb24_to_rgb32
1862 .convert = rgb24_to_bgr24
1865 .convert = rgb24_to_gray
1868 .convert = rgb24_to_pal8
1870 [PIX_FMT_YUV444P] = {
1871 .convert = rgb24_to_yuv444p
1873 [PIX_FMT_YUVJ420P] = {
1874 .convert = rgb24_to_yuvj420p
1876 [PIX_FMT_YUVJ444P] = {
1877 .convert = rgb24_to_yuvj444p
1882 .convert = rgb32_to_rgb24
1885 .convert = rgb32_to_bgr24
1887 [PIX_FMT_RGB565] = {
1888 .convert = rgb32_to_rgb565
1890 [PIX_FMT_RGB555] = {
1891 .convert = rgb32_to_rgb555
1894 .convert = rgb32_to_pal8
1896 [PIX_FMT_YUV420P] = {
1897 .convert = rgb32_to_yuv420p
1900 .convert = rgb32_to_gray
1905 .convert = bgr24_to_rgb32
1908 .convert = bgr24_to_rgb24
1910 [PIX_FMT_YUV420P] = {
1911 .convert = bgr24_to_yuv420p
1914 .convert = bgr24_to_gray
1917 [PIX_FMT_RGB555] = {
1919 .convert = rgb555_to_rgb24
1922 .convert = rgb555_to_rgb32
1924 [PIX_FMT_YUV420P] = {
1925 .convert = rgb555_to_yuv420p
1928 .convert = rgb555_to_gray
1931 [PIX_FMT_RGB565] = {
1933 .convert = rgb565_to_rgb32
1936 .convert = rgb565_to_rgb24
1938 [PIX_FMT_YUV420P] = {
1939 .convert = rgb565_to_yuv420p
1942 .convert = rgb565_to_gray
1945 [PIX_FMT_GRAY16BE] = {
1947 .convert = gray16be_to_gray
1949 [PIX_FMT_GRAY16LE] = {
1950 .convert = gray16_to_gray16
1953 [PIX_FMT_GRAY16LE] = {
1955 .convert = gray16le_to_gray
1957 [PIX_FMT_GRAY16BE] = {
1958 .convert = gray16_to_gray16
1962 [PIX_FMT_RGB555] = {
1963 .convert = gray_to_rgb555
1965 [PIX_FMT_RGB565] = {
1966 .convert = gray_to_rgb565
1969 .convert = gray_to_rgb24
1972 .convert = gray_to_bgr24
1975 .convert = gray_to_rgb32
1977 [PIX_FMT_MONOWHITE] = {
1978 .convert = gray_to_monowhite
1980 [PIX_FMT_MONOBLACK] = {
1981 .convert = gray_to_monoblack
1983 [PIX_FMT_GRAY16LE] = {
1984 .convert = gray_to_gray16
1986 [PIX_FMT_GRAY16BE] = {
1987 .convert = gray_to_gray16
1990 [PIX_FMT_MONOWHITE] = {
1992 .convert = monowhite_to_gray
1995 [PIX_FMT_MONOBLACK] = {
1997 .convert = monoblack_to_gray
2001 [PIX_FMT_RGB555] = {
2002 .convert = pal8_to_rgb555
2004 [PIX_FMT_RGB565] = {
2005 .convert = pal8_to_rgb565
2008 .convert = pal8_to_bgr24
2011 .convert = pal8_to_rgb24
2014 .convert = pal8_to_rgb32
2017 [PIX_FMT_UYYVYY411] = {
2018 [PIX_FMT_YUV411P] = {
2019 .convert = uyyvyy411_to_yuv411p,
2025 int avpicture_alloc(AVPicture *picture,
2026 int pix_fmt, int width, int height)
2031 size = avpicture_get_size(pix_fmt, width, height);
2034 ptr = av_malloc(size);
2037 avpicture_fill(picture, ptr, pix_fmt, width, height);
2038 if(picture->data[1] && !picture->data[2])
2039 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
2043 memset(picture, 0, sizeof(AVPicture));
2047 void avpicture_free(AVPicture *picture)
2049 av_free(picture->data[0]);
2052 /* return true if yuv planar */
2053 static inline int is_yuv_planar(const PixFmtInfo *ps)
2055 return (ps->color_type == FF_COLOR_YUV ||
2056 ps->color_type == FF_COLOR_YUV_JPEG) &&
2057 ps->pixel_type == FF_PIXEL_PLANAR;
2060 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2061 int pix_fmt, int top_band, int left_band)
2066 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2069 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2070 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2072 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2073 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2074 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2076 dst->linesize[0] = src->linesize[0];
2077 dst->linesize[1] = src->linesize[1];
2078 dst->linesize[2] = src->linesize[2];
2082 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2083 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2092 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2093 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2095 for (i = 0; i < 3; i++) {
2096 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2097 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2099 if (padtop || padleft) {
2100 memset(dst->data[i], color[i],
2101 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2104 if (padleft || padright) {
2105 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2106 (dst->linesize[i] - (padright >> x_shift));
2107 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2108 for (y = 0; y < yheight; y++) {
2109 memset(optr, color[i], (padleft + padright) >> x_shift);
2110 optr += dst->linesize[i];
2114 if (src) { /* first line */
2115 uint8_t *iptr = src->data[i];
2116 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2117 (padleft >> x_shift);
2118 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
2119 iptr += src->linesize[i];
2120 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2121 (dst->linesize[i] - (padright >> x_shift));
2122 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2123 for (y = 0; y < yheight; y++) {
2124 memset(optr, color[i], (padleft + padright) >> x_shift);
2125 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2126 (width - padleft - padright) >> x_shift);
2127 iptr += src->linesize[i];
2128 optr += dst->linesize[i];
2132 if (padbottom || padright) {
2133 optr = dst->data[i] + dst->linesize[i] *
2134 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2135 memset(optr, color[i],dst->linesize[i] *
2136 (padbottom >> y_shift) + (padright >> x_shift));
2143 static uint8_t y_ccir_to_jpeg[256];
2144 static uint8_t y_jpeg_to_ccir[256];
2145 static uint8_t c_ccir_to_jpeg[256];
2146 static uint8_t c_jpeg_to_ccir[256];
2148 /* init various conversion tables */
2149 static void img_convert_init(void)
2152 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2154 for(i = 0;i < 256; i++) {
2155 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
2156 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
2157 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
2158 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
2162 /* apply to each pixel the given table */
2163 static void img_apply_table(uint8_t *dst, int dst_wrap,
2164 const uint8_t *src, int src_wrap,
2165 int width, int height, const uint8_t *table1)
2170 const uint8_t *table;
2173 for(;height > 0; height--) {
2197 /* XXX: use generic filter ? */
2198 /* XXX: in most cases, the sampling position is incorrect */
2201 static void shrink41(uint8_t *dst, int dst_wrap,
2202 const uint8_t *src, int src_wrap,
2203 int width, int height)
2209 for(;height > 0; height--) {
2212 for(w = width;w > 0; w--) {
2213 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
2223 static void shrink21(uint8_t *dst, int dst_wrap,
2224 const uint8_t *src, int src_wrap,
2225 int width, int height)
2231 for(;height > 0; height--) {
2234 for(w = width;w > 0; w--) {
2235 d[0] = (s[0] + s[1]) >> 1;
2245 static void shrink12(uint8_t *dst, int dst_wrap,
2246 const uint8_t *src, int src_wrap,
2247 int width, int height)
2251 const uint8_t *s1, *s2;
2253 for(;height > 0; height--) {
2257 for(w = width;w >= 4; w-=4) {
2258 d[0] = (s1[0] + s2[0]) >> 1;
2259 d[1] = (s1[1] + s2[1]) >> 1;
2260 d[2] = (s1[2] + s2[2]) >> 1;
2261 d[3] = (s1[3] + s2[3]) >> 1;
2267 d[0] = (s1[0] + s2[0]) >> 1;
2272 src += 2 * src_wrap;
2277 static void grow21_line(uint8_t *dst, const uint8_t *src,
2286 for(w = width;w >= 4; w-=4) {
2287 d[1] = d[0] = s1[0];
2288 d[3] = d[2] = s1[1];
2292 for(;w >= 2; w -= 2) {
2293 d[1] = d[0] = s1[0];
2297 /* only needed if width is not a multiple of two */
2298 /* XXX: veryfy that */
2304 static void grow41_line(uint8_t *dst, const uint8_t *src,
2313 for(w = width;w >= 4; w-=4) {
2325 static void grow21(uint8_t *dst, int dst_wrap,
2326 const uint8_t *src, int src_wrap,
2327 int width, int height)
2329 for(;height > 0; height--) {
2330 grow21_line(dst, src, width);
2337 static void grow12(uint8_t *dst, int dst_wrap,
2338 const uint8_t *src, int src_wrap,
2339 int width, int height)
2341 for(;height > 0; height-=2) {
2342 memcpy(dst, src, width);
2344 memcpy(dst, src, width);
2351 static void grow22(uint8_t *dst, int dst_wrap,
2352 const uint8_t *src, int src_wrap,
2353 int width, int height)
2355 for(;height > 0; height--) {
2356 grow21_line(dst, src, width);
2364 static void grow41(uint8_t *dst, int dst_wrap,
2365 const uint8_t *src, int src_wrap,
2366 int width, int height)
2368 for(;height > 0; height--) {
2369 grow41_line(dst, src, width);
2376 static void grow44(uint8_t *dst, int dst_wrap,
2377 const uint8_t *src, int src_wrap,
2378 int width, int height)
2380 for(;height > 0; height--) {
2381 grow41_line(dst, src, width);
2382 if ((height & 3) == 1)
2389 static void conv411(uint8_t *dst, int dst_wrap,
2390 const uint8_t *src, int src_wrap,
2391 int width, int height)
2394 const uint8_t *s1, *s2;
2399 for(;height > 0; height--) {
2401 s2 = src + src_wrap;
2403 for(w = width;w > 0; w--) {
2404 c = (s1[0] + s2[0]) >> 1;
2411 src += src_wrap * 2;
2416 /* XXX: always use linesize. Return -1 if not supported */
2417 int img_convert(AVPicture *dst, int dst_pix_fmt,
2418 const AVPicture *src, int src_pix_fmt,
2419 int src_width, int src_height)
2421 static int initialized;
2422 int i, ret, dst_width, dst_height, int_pix_fmt;
2423 const PixFmtInfo *src_pix, *dst_pix;
2424 const ConvertEntry *ce;
2425 AVPicture tmp1, *tmp = &tmp1;
2427 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2428 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2430 if (src_width <= 0 || src_height <= 0)
2438 dst_width = src_width;
2439 dst_height = src_height;
2441 dst_pix = &pix_fmt_info[dst_pix_fmt];
2442 src_pix = &pix_fmt_info[src_pix_fmt];
2443 if (src_pix_fmt == dst_pix_fmt) {
2444 /* no conversion needed: just copy */
2445 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2449 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2451 /* specific conversion routine */
2452 ce->convert(dst, src, dst_width, dst_height);
2457 if (is_yuv_planar(dst_pix) &&
2458 src_pix_fmt == PIX_FMT_GRAY8) {
2462 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2463 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2464 src->data[0], src->linesize[0],
2465 dst_width, dst_height);
2467 img_apply_table(dst->data[0], dst->linesize[0],
2468 src->data[0], src->linesize[0],
2469 dst_width, dst_height,
2472 /* fill U and V with 128 */
2475 w >>= dst_pix->x_chroma_shift;
2476 h >>= dst_pix->y_chroma_shift;
2477 for(i = 1; i <= 2; i++) {
2479 for(y = 0; y< h; y++) {
2481 d += dst->linesize[i];
2488 if (is_yuv_planar(src_pix) &&
2489 dst_pix_fmt == PIX_FMT_GRAY8) {
2490 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2491 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2492 src->data[0], src->linesize[0],
2493 dst_width, dst_height);
2495 img_apply_table(dst->data[0], dst->linesize[0],
2496 src->data[0], src->linesize[0],
2497 dst_width, dst_height,
2503 /* YUV to YUV planar */
2504 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2505 int x_shift, y_shift, w, h, xy_shift;
2506 void (*resize_func)(uint8_t *dst, int dst_wrap,
2507 const uint8_t *src, int src_wrap,
2508 int width, int height);
2510 /* compute chroma size of the smallest dimensions */
2513 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2514 w >>= dst_pix->x_chroma_shift;
2516 w >>= src_pix->x_chroma_shift;
2517 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2518 h >>= dst_pix->y_chroma_shift;
2520 h >>= src_pix->y_chroma_shift;
2522 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2523 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2524 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2525 /* there must be filters for conversion at least from and to
2529 resize_func = ff_img_copy_plane;
2532 resize_func = shrink21;
2535 resize_func = shrink41;
2538 resize_func = shrink12;
2541 resize_func = ff_shrink22;
2544 resize_func = ff_shrink44;
2547 resize_func = grow21;
2550 resize_func = grow12;
2553 resize_func = grow41;
2556 resize_func = grow22;
2559 resize_func = grow44;
2562 resize_func = conv411;
2565 /* currently not handled */
2566 goto no_chroma_filter;
2569 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2570 src->data[0], src->linesize[0],
2571 dst_width, dst_height);
2573 for(i = 1;i <= 2; i++)
2574 resize_func(dst->data[i], dst->linesize[i],
2575 src->data[i], src->linesize[i],
2576 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2577 /* if yuv color space conversion is needed, we do it here on
2578 the destination image */
2579 if (dst_pix->color_type != src_pix->color_type) {
2580 const uint8_t *y_table, *c_table;
2581 if (dst_pix->color_type == FF_COLOR_YUV) {
2582 y_table = y_jpeg_to_ccir;
2583 c_table = c_jpeg_to_ccir;
2585 y_table = y_ccir_to_jpeg;
2586 c_table = c_ccir_to_jpeg;
2588 img_apply_table(dst->data[0], dst->linesize[0],
2589 dst->data[0], dst->linesize[0],
2590 dst_width, dst_height,
2593 for(i = 1;i <= 2; i++)
2594 img_apply_table(dst->data[i], dst->linesize[i],
2595 dst->data[i], dst->linesize[i],
2596 dst_width>>dst_pix->x_chroma_shift,
2597 dst_height>>dst_pix->y_chroma_shift,
2604 /* try to use an intermediate format */
2605 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2606 dst_pix_fmt == PIX_FMT_YUYV422) {
2607 /* specific case: convert to YUV422P first */
2608 int_pix_fmt = PIX_FMT_YUV422P;
2609 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2610 dst_pix_fmt == PIX_FMT_UYVY422) {
2611 /* specific case: convert to YUV422P first */
2612 int_pix_fmt = PIX_FMT_YUV422P;
2613 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2614 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2615 /* specific case: convert to YUV411P first */
2616 int_pix_fmt = PIX_FMT_YUV411P;
2617 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2618 src_pix_fmt != PIX_FMT_GRAY8) ||
2619 (dst_pix->color_type == FF_COLOR_GRAY &&
2620 dst_pix_fmt != PIX_FMT_GRAY8)) {
2621 /* gray8 is the normalized format */
2622 int_pix_fmt = PIX_FMT_GRAY8;
2623 } else if ((is_yuv_planar(src_pix) &&
2624 src_pix_fmt != PIX_FMT_YUV444P &&
2625 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2626 /* yuv444 is the normalized format */
2627 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2628 int_pix_fmt = PIX_FMT_YUVJ444P;
2630 int_pix_fmt = PIX_FMT_YUV444P;
2631 } else if ((is_yuv_planar(dst_pix) &&
2632 dst_pix_fmt != PIX_FMT_YUV444P &&
2633 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2634 /* yuv444 is the normalized format */
2635 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2636 int_pix_fmt = PIX_FMT_YUVJ444P;
2638 int_pix_fmt = PIX_FMT_YUV444P;
2640 /* the two formats are rgb or gray8 or yuv[j]444p */
2641 if (src_pix->is_alpha && dst_pix->is_alpha)
2642 int_pix_fmt = PIX_FMT_RGB32;
2644 int_pix_fmt = PIX_FMT_RGB24;
2646 if (src_pix_fmt == int_pix_fmt)
2648 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2651 if (img_convert(tmp, int_pix_fmt,
2652 src, src_pix_fmt, src_width, src_height) < 0)
2654 if (img_convert(dst, dst_pix_fmt,
2655 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2659 avpicture_free(tmp);
2664 /* NOTE: we scan all the pixels to have an exact information */
2665 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2667 const unsigned char *p;
2668 int src_wrap, ret, x, y;
2670 uint32_t *palette = (uint32_t *)src->data[1];
2673 src_wrap = src->linesize[0] - width;
2675 for(y=0;y<height;y++) {
2676 for(x=0;x<width;x++) {
2677 a = palette[p[0]] >> 24;
2679 ret |= FF_ALPHA_TRANSP;
2680 } else if (a != 0xff) {
2681 ret |= FF_ALPHA_SEMI_TRANSP;
2690 int img_get_alpha_info(const AVPicture *src,
2691 int pix_fmt, int width, int height)
2693 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2696 pf = &pix_fmt_info[pix_fmt];
2697 /* no alpha can be represented in format */
2702 ret = get_alpha_info_rgb32(src, width, height);
2705 ret = get_alpha_info_pal8(src, width, height);
2708 /* we do not know, so everything is indicated */
2709 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2716 #define DEINT_INPLACE_LINE_LUM \
2717 movd_m2r(lum_m4[0],mm0);\
2718 movd_m2r(lum_m3[0],mm1);\
2719 movd_m2r(lum_m2[0],mm2);\
2720 movd_m2r(lum_m1[0],mm3);\
2721 movd_m2r(lum[0],mm4);\
2722 punpcklbw_r2r(mm7,mm0);\
2723 movd_r2m(mm2,lum_m4[0]);\
2724 punpcklbw_r2r(mm7,mm1);\
2725 punpcklbw_r2r(mm7,mm2);\
2726 punpcklbw_r2r(mm7,mm3);\
2727 punpcklbw_r2r(mm7,mm4);\
2728 paddw_r2r(mm3,mm1);\
2730 paddw_r2r(mm4,mm0);\
2732 paddw_r2r(mm6,mm2);\
2733 paddw_r2r(mm2,mm1);\
2734 psubusw_r2r(mm0,mm1);\
2736 packuswb_r2r(mm7,mm1);\
2737 movd_r2m(mm1,lum_m2[0]);
2739 #define DEINT_LINE_LUM \
2740 movd_m2r(lum_m4[0],mm0);\
2741 movd_m2r(lum_m3[0],mm1);\
2742 movd_m2r(lum_m2[0],mm2);\
2743 movd_m2r(lum_m1[0],mm3);\
2744 movd_m2r(lum[0],mm4);\
2745 punpcklbw_r2r(mm7,mm0);\
2746 punpcklbw_r2r(mm7,mm1);\
2747 punpcklbw_r2r(mm7,mm2);\
2748 punpcklbw_r2r(mm7,mm3);\
2749 punpcklbw_r2r(mm7,mm4);\
2750 paddw_r2r(mm3,mm1);\
2752 paddw_r2r(mm4,mm0);\
2754 paddw_r2r(mm6,mm2);\
2755 paddw_r2r(mm2,mm1);\
2756 psubusw_r2r(mm0,mm1);\
2758 packuswb_r2r(mm7,mm1);\
2759 movd_r2m(mm1,dst[0]);
2762 /* filter parameters: [-1 4 2 4 -1] // 8 */
2763 static void deinterlace_line(uint8_t *dst,
2764 const uint8_t *lum_m4, const uint8_t *lum_m3,
2765 const uint8_t *lum_m2, const uint8_t *lum_m1,
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 sum += lum_m1[0] << 2;
2779 dst[0] = cm[(sum + 4) >> 3];
2791 movq_m2r(ff_pw_4,mm6);
2793 for (;size > 3; size-=4) {
2804 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2808 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2811 for(;size > 0;size--) {
2813 sum += lum_m3[0] << 2;
2814 sum += lum_m2[0] << 1;
2815 lum_m4[0]=lum_m2[0];
2816 sum += lum_m1[0] << 2;
2818 lum_m2[0] = cm[(sum + 4) >> 3];
2829 movq_m2r(ff_pw_4,mm6);
2831 for (;size > 3; size-=4) {
2832 DEINT_INPLACE_LINE_LUM
2842 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2843 top field is copied as is, but the bottom field is deinterlaced
2844 against the top field. */
2845 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2846 const uint8_t *src1, int src_wrap,
2847 int width, int height)
2849 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2854 src_0=&src_m1[src_wrap];
2855 src_p1=&src_0[src_wrap];
2856 src_p2=&src_p1[src_wrap];
2857 for(y=0;y<(height-2);y+=2) {
2858 memcpy(dst,src_m1,width);
2860 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2864 src_p1 += 2*src_wrap;
2865 src_p2 += 2*src_wrap;
2868 memcpy(dst,src_m1,width);
2871 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2874 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2875 int width, int height)
2877 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2880 buf = (uint8_t*)av_malloc(width);
2883 memcpy(buf,src_m1,width);
2884 src_0=&src_m1[src_wrap];
2885 src_p1=&src_0[src_wrap];
2886 src_p2=&src_p1[src_wrap];
2887 for(y=0;y<(height-2);y+=2) {
2888 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2891 src_p1 += 2*src_wrap;
2892 src_p2 += 2*src_wrap;
2895 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2899 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2900 int pix_fmt, int width, int height)
2904 if (pix_fmt != PIX_FMT_YUV420P &&
2905 pix_fmt != PIX_FMT_YUV422P &&
2906 pix_fmt != PIX_FMT_YUV444P &&
2907 pix_fmt != PIX_FMT_YUV411P &&
2908 pix_fmt != PIX_FMT_GRAY8)
2910 if ((width & 3) != 0 || (height & 3) != 0)
2916 case PIX_FMT_YUV420P:
2920 case PIX_FMT_YUV422P:
2923 case PIX_FMT_YUV411P:
2929 if (pix_fmt == PIX_FMT_GRAY8) {
2934 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2937 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2938 src->data[i], src->linesize[i],