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:
479 pal[i] = b + (g<<8) + (r<<16);
485 int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
488 const PixFmtInfo *pinfo;
490 memset(picture->linesize, 0, sizeof(picture->linesize));
492 pinfo = &pix_fmt_info[pix_fmt];
494 case PIX_FMT_YUV420P:
495 case PIX_FMT_YUV422P:
496 case PIX_FMT_YUV444P:
497 case PIX_FMT_YUV410P:
498 case PIX_FMT_YUV411P:
499 case PIX_FMT_YUV440P:
500 case PIX_FMT_YUVJ420P:
501 case PIX_FMT_YUVJ422P:
502 case PIX_FMT_YUVJ444P:
503 case PIX_FMT_YUVJ440P:
504 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
505 picture->linesize[0] = width;
506 picture->linesize[1] = w2;
507 picture->linesize[2] = w2;
509 case PIX_FMT_YUVA420P:
510 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
511 picture->linesize[0] = width;
512 picture->linesize[1] = w2;
513 picture->linesize[2] = w2;
514 picture->linesize[3] = width;
518 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
519 picture->linesize[0] = width;
520 picture->linesize[1] = w2;
524 picture->linesize[0] = width * 3;
528 case PIX_FMT_RGB32_1:
529 case PIX_FMT_BGR32_1:
530 picture->linesize[0] = width * 4;
532 case PIX_FMT_GRAY16BE:
533 case PIX_FMT_GRAY16LE:
538 case PIX_FMT_YUYV422:
539 picture->linesize[0] = width * 2;
541 case PIX_FMT_UYVY422:
542 picture->linesize[0] = width * 2;
544 case PIX_FMT_UYYVYY411:
545 picture->linesize[0] = width + width/2;
549 picture->linesize[0] = width / 2;
551 case PIX_FMT_MONOWHITE:
552 case PIX_FMT_MONOBLACK:
553 picture->linesize[0] = (width + 7) >> 3;
558 case PIX_FMT_RGB4_BYTE:
559 case PIX_FMT_BGR4_BYTE:
561 picture->linesize[0] = width;
562 picture->linesize[1] = 4;
570 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
574 const PixFmtInfo *pinfo;
576 pinfo = &pix_fmt_info[pix_fmt];
577 size = picture->linesize[0] * height;
579 case PIX_FMT_YUV420P:
580 case PIX_FMT_YUV422P:
581 case PIX_FMT_YUV444P:
582 case PIX_FMT_YUV410P:
583 case PIX_FMT_YUV411P:
584 case PIX_FMT_YUV440P:
585 case PIX_FMT_YUVJ420P:
586 case PIX_FMT_YUVJ422P:
587 case PIX_FMT_YUVJ444P:
588 case PIX_FMT_YUVJ440P:
589 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
590 size2 = picture->linesize[1] * h2;
591 picture->data[0] = ptr;
592 picture->data[1] = picture->data[0] + size;
593 picture->data[2] = picture->data[1] + size2;
594 picture->data[3] = NULL;
595 return size + 2 * size2;
596 case PIX_FMT_YUVA420P:
597 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
598 size2 = picture->linesize[1] * h2;
599 picture->data[0] = ptr;
600 picture->data[1] = picture->data[0] + size;
601 picture->data[2] = picture->data[1] + size2;
602 picture->data[3] = picture->data[1] + size2 + size2;
603 return 2 * size + 2 * size2;
606 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
607 size2 = picture->linesize[1] * h2 * 2;
608 picture->data[0] = ptr;
609 picture->data[1] = picture->data[0] + size;
610 picture->data[2] = NULL;
611 picture->data[3] = NULL;
612 return size + 2 * size2;
617 case PIX_FMT_RGB32_1:
618 case PIX_FMT_BGR32_1:
619 case PIX_FMT_GRAY16BE:
620 case PIX_FMT_GRAY16LE:
625 case PIX_FMT_YUYV422:
626 case PIX_FMT_UYVY422:
627 case PIX_FMT_UYYVYY411:
630 case PIX_FMT_MONOWHITE:
631 case PIX_FMT_MONOBLACK:
632 picture->data[0] = ptr;
633 picture->data[1] = NULL;
634 picture->data[2] = NULL;
635 picture->data[3] = NULL;
640 case PIX_FMT_RGB4_BYTE:
641 case PIX_FMT_BGR4_BYTE:
643 size2 = (size + 3) & ~3;
644 picture->data[0] = ptr;
645 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
646 picture->data[2] = NULL;
647 picture->data[3] = NULL;
648 return size2 + 256 * 4;
650 picture->data[0] = NULL;
651 picture->data[1] = NULL;
652 picture->data[2] = NULL;
653 picture->data[3] = NULL;
658 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
659 int pix_fmt, int width, int height)
662 if(avcodec_check_dimensions(NULL, width, height))
665 if (ff_fill_linesize(picture, pix_fmt, width))
668 return ff_fill_pointer(picture, ptr, pix_fmt, height);
671 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
672 unsigned char *dest, int dest_size)
674 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
675 int i, j, w, h, data_planes;
676 const unsigned char* s;
677 int size = avpicture_get_size(pix_fmt, width, height);
679 if (size > dest_size || size < 0)
682 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
683 if (pix_fmt == PIX_FMT_YUYV422 ||
684 pix_fmt == PIX_FMT_UYVY422 ||
685 pix_fmt == PIX_FMT_BGR565 ||
686 pix_fmt == PIX_FMT_BGR555 ||
687 pix_fmt == PIX_FMT_RGB565 ||
688 pix_fmt == PIX_FMT_RGB555)
690 else if (pix_fmt == PIX_FMT_UYYVYY411)
692 else if (pix_fmt == PIX_FMT_PAL8)
695 w = width * (pf->depth * pf->nb_channels / 8);
700 data_planes = pf->nb_channels;
701 w = (width*pf->depth + 7)/8;
705 for (i=0; i<data_planes; i++) {
707 w = width >> pf->x_chroma_shift;
708 h = height >> pf->y_chroma_shift;
714 s += src->linesize[i];
718 if (pf->pixel_type == FF_PIXEL_PALETTE)
719 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
724 int avpicture_get_size(int pix_fmt, int width, int height)
726 AVPicture dummy_pict;
727 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
730 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
733 const PixFmtInfo *pf, *ps;
736 ps = &pix_fmt_info[src_pix_fmt];
737 pf = &pix_fmt_info[dst_pix_fmt];
741 pf = &pix_fmt_info[dst_pix_fmt];
742 if (pf->depth < ps->depth ||
743 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
744 loss |= FF_LOSS_DEPTH;
745 if (pf->x_chroma_shift > ps->x_chroma_shift ||
746 pf->y_chroma_shift > ps->y_chroma_shift)
747 loss |= FF_LOSS_RESOLUTION;
748 switch(pf->color_type) {
750 if (ps->color_type != FF_COLOR_RGB &&
751 ps->color_type != FF_COLOR_GRAY)
752 loss |= FF_LOSS_COLORSPACE;
755 if (ps->color_type != FF_COLOR_GRAY)
756 loss |= FF_LOSS_COLORSPACE;
759 if (ps->color_type != FF_COLOR_YUV)
760 loss |= FF_LOSS_COLORSPACE;
762 case FF_COLOR_YUV_JPEG:
763 if (ps->color_type != FF_COLOR_YUV_JPEG &&
764 ps->color_type != FF_COLOR_YUV &&
765 ps->color_type != FF_COLOR_GRAY)
766 loss |= FF_LOSS_COLORSPACE;
770 if (ps->color_type != pf->color_type)
771 loss |= FF_LOSS_COLORSPACE;
774 if (pf->color_type == FF_COLOR_GRAY &&
775 ps->color_type != FF_COLOR_GRAY)
776 loss |= FF_LOSS_CHROMA;
777 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
778 loss |= FF_LOSS_ALPHA;
779 if (pf->pixel_type == FF_PIXEL_PALETTE &&
780 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
781 loss |= FF_LOSS_COLORQUANT;
785 static int avg_bits_per_pixel(int pix_fmt)
788 const PixFmtInfo *pf;
790 pf = &pix_fmt_info[pix_fmt];
791 switch(pf->pixel_type) {
792 case FF_PIXEL_PACKED:
794 case PIX_FMT_YUYV422:
795 case PIX_FMT_UYVY422:
802 case PIX_FMT_UYYVYY411:
806 bits = pf->depth * pf->nb_channels;
810 case FF_PIXEL_PLANAR:
811 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
812 bits = pf->depth * pf->nb_channels;
814 bits = pf->depth + ((2 * pf->depth) >>
815 (pf->x_chroma_shift + pf->y_chroma_shift));
818 case FF_PIXEL_PALETTE:
828 static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
833 int dist, i, loss, min_dist, dst_pix_fmt;
835 /* find exact color match with smallest size */
837 min_dist = 0x7fffffff;
838 for(i = 0;i < PIX_FMT_NB; i++) {
839 if (pix_fmt_mask & (1ULL << i)) {
840 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
842 dist = avg_bits_per_pixel(i);
843 if (dist < min_dist) {
853 int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
854 int has_alpha, int *loss_ptr)
856 int dst_pix_fmt, loss_mask, i;
857 static const int loss_mask_order[] = {
858 ~0, /* no loss first */
861 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
867 /* try with successive loss */
870 loss_mask = loss_mask_order[i++];
871 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
872 has_alpha, loss_mask);
873 if (dst_pix_fmt >= 0)
881 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
885 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
886 const uint8_t *src, int src_wrap,
887 int width, int height)
891 for(;height > 0; height--) {
892 memcpy(dst, src, width);
898 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
901 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
903 pf = &pix_fmt_info[pix_fmt];
904 switch(pf->pixel_type) {
905 case FF_PIXEL_PACKED:
907 case PIX_FMT_YUYV422:
908 case PIX_FMT_UYVY422:
915 case PIX_FMT_UYYVYY411:
919 bits = pf->depth * pf->nb_channels;
922 return (width * bits + 7) >> 3;
924 case FF_PIXEL_PLANAR:
925 if (plane == 1 || plane == 2)
926 width= -((-width)>>pf->x_chroma_shift);
928 return (width * pf->depth + 7) >> 3;
930 case FF_PIXEL_PALETTE:
939 void av_picture_copy(AVPicture *dst, const AVPicture *src,
940 int pix_fmt, int width, int height)
943 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
945 pf = &pix_fmt_info[pix_fmt];
946 switch(pf->pixel_type) {
947 case FF_PIXEL_PACKED:
948 case FF_PIXEL_PLANAR:
949 for(i = 0; i < pf->nb_channels; i++) {
951 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
953 if (i == 1 || i == 2) {
954 h= -((-height)>>pf->y_chroma_shift);
956 ff_img_copy_plane(dst->data[i], dst->linesize[i],
957 src->data[i], src->linesize[i],
961 case FF_PIXEL_PALETTE:
962 ff_img_copy_plane(dst->data[0], dst->linesize[0],
963 src->data[0], src->linesize[0],
965 /* copy the palette */
966 ff_img_copy_plane(dst->data[1], dst->linesize[1],
967 src->data[1], src->linesize[1],
973 /* XXX: totally non optimized */
975 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
976 int width, int height)
978 const uint8_t *p, *p1;
979 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
987 for(;height >= 1; height -= 2) {
992 for(w = width; w >= 2; w -= 2) {
1009 p1 += src->linesize[0];
1010 lum1 += dst->linesize[0];
1014 for(w = width; w >= 2; w -= 2) {
1023 p1 += src->linesize[0];
1024 lum1 += dst->linesize[0];
1026 cb1 += dst->linesize[1];
1027 cr1 += dst->linesize[2];
1031 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
1032 int width, int height)
1034 const uint8_t *p, *p1;
1035 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1040 lum1 = dst->data[0];
1044 for(;height >= 1; height -= 2) {
1049 for(w = width; w >= 2; w -= 2) {
1066 p1 += src->linesize[0];
1067 lum1 += dst->linesize[0];
1071 for(w = width; w >= 2; w -= 2) {
1080 p1 += src->linesize[0];
1081 lum1 += dst->linesize[0];
1083 cb1 += dst->linesize[1];
1084 cr1 += dst->linesize[2];
1089 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1090 int width, int height)
1092 const uint8_t *p, *p1;
1093 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1097 lum1 = dst->data[0];
1100 for(;height > 0; height--) {
1105 for(w = width; w >= 2; w -= 2) {
1115 p1 += src->linesize[0];
1116 lum1 += dst->linesize[0];
1117 cb1 += dst->linesize[1];
1118 cr1 += dst->linesize[2];
1123 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1124 int width, int height)
1126 const uint8_t *p, *p1;
1127 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1131 lum1 = dst->data[0];
1134 for(;height > 0; height--) {
1139 for(w = width; w >= 2; w -= 2) {
1149 p1 += src->linesize[0];
1150 lum1 += dst->linesize[0];
1151 cb1 += dst->linesize[1];
1152 cr1 += dst->linesize[2];
1156 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1157 int width, int height)
1160 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1164 lum1 = src->data[0];
1167 for(;height > 0; height--) {
1172 for(w = width; w >= 2; w -= 2) {
1182 p1 += dst->linesize[0];
1183 lum1 += src->linesize[0];
1184 cb1 += src->linesize[1];
1185 cr1 += src->linesize[2];
1189 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1190 int width, int height)
1193 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1197 lum1 = src->data[0];
1200 for(;height > 0; height--) {
1205 for(w = width; w >= 2; w -= 2) {
1215 p1 += dst->linesize[0];
1216 lum1 += src->linesize[0];
1217 cb1 += src->linesize[1];
1218 cr1 += src->linesize[2];
1222 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1223 int width, int height)
1225 const uint8_t *p, *p1;
1226 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1230 lum1 = dst->data[0];
1233 for(;height > 0; height--) {
1238 for(w = width; w >= 4; w -= 4) {
1250 p1 += src->linesize[0];
1251 lum1 += dst->linesize[0];
1252 cb1 += dst->linesize[1];
1253 cr1 += dst->linesize[2];
1258 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1259 int width, int height)
1262 uint8_t *line1, *line2, *linesrc = dst->data[0];
1263 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1264 uint8_t *cb1, *cb2 = src->data[1];
1265 uint8_t *cr1, *cr2 = src->data[2];
1267 for(h = height / 2; h--;) {
1269 line2 = linesrc + dst->linesize[0];
1272 lum2 = lumsrc + src->linesize[0];
1277 for(w = width / 2; w--;) {
1278 *line1++ = *lum1++; *line2++ = *lum2++;
1279 *line1++ = *line2++ = *cb1++;
1280 *line1++ = *lum1++; *line2++ = *lum2++;
1281 *line1++ = *line2++ = *cr1++;
1284 linesrc += dst->linesize[0] * 2;
1285 lumsrc += src->linesize[0] * 2;
1286 cb2 += src->linesize[1];
1287 cr2 += src->linesize[2];
1291 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1292 int width, int height)
1295 uint8_t *line1, *line2, *linesrc = dst->data[0];
1296 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1297 uint8_t *cb1, *cb2 = src->data[1];
1298 uint8_t *cr1, *cr2 = src->data[2];
1300 for(h = height / 2; h--;) {
1302 line2 = linesrc + dst->linesize[0];
1305 lum2 = lumsrc + src->linesize[0];
1310 for(w = width / 2; w--;) {
1311 *line1++ = *line2++ = *cb1++;
1312 *line1++ = *lum1++; *line2++ = *lum2++;
1313 *line1++ = *line2++ = *cr1++;
1314 *line1++ = *lum1++; *line2++ = *lum2++;
1317 linesrc += dst->linesize[0] * 2;
1318 lumsrc += src->linesize[0] * 2;
1319 cb2 += src->linesize[1];
1320 cr2 += src->linesize[2];
1325 void ff_shrink22(uint8_t *dst, int dst_wrap,
1326 const uint8_t *src, int src_wrap,
1327 int width, int height)
1330 const uint8_t *s1, *s2;
1333 for(;height > 0; height--) {
1337 for(w = width;w >= 4; w-=4) {
1338 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1339 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1340 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1341 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1347 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1352 src += 2 * src_wrap;
1358 void ff_shrink44(uint8_t *dst, int dst_wrap,
1359 const uint8_t *src, int src_wrap,
1360 int width, int height)
1363 const uint8_t *s1, *s2, *s3, *s4;
1366 for(;height > 0; height--) {
1372 for(w = width;w > 0; w--) {
1373 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1374 s2[0] + s2[1] + s2[2] + s2[3] +
1375 s3[0] + s3[1] + s3[2] + s3[3] +
1376 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1383 src += 4 * src_wrap;
1389 void ff_shrink88(uint8_t *dst, int dst_wrap,
1390 const uint8_t *src, int src_wrap,
1391 int width, int height)
1395 for(;height > 0; height--) {
1396 for(w = width;w > 0; w--) {
1399 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1402 *(dst++) = (tmp + 32)>>6;
1403 src += 8 - 8*src_wrap;
1405 src += 8*src_wrap - 8*width;
1406 dst += dst_wrap - width;
1410 /* XXX: add jpeg quantize code */
1412 #define TRANSP_INDEX (6*6*6)
1414 /* this is maybe slow, but allows for extensions */
1415 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1417 return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
1420 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1423 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1426 pal = (uint32_t *)palette;
1428 for(r = 0; r < 6; r++) {
1429 for(g = 0; g < 6; g++) {
1430 for(b = 0; b < 6; b++) {
1431 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1432 (pal_value[g] << 8) | pal_value[b];
1439 pal[i++] = 0xff000000;
1442 /* copy bit n to bits 0 ... n - 1 */
1443 static inline unsigned int bitcopy_n(unsigned int a, int n)
1446 mask = (1 << n) - 1;
1447 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1450 /* rgb555 handling */
1452 #define RGB_NAME rgb555
1454 #define RGB_IN(r, g, b, s)\
1456 unsigned int v = ((const uint16_t *)(s))[0];\
1457 r = bitcopy_n(v >> (10 - 3), 3);\
1458 g = bitcopy_n(v >> (5 - 3), 3);\
1459 b = bitcopy_n(v << 3, 3);\
1463 #define RGB_OUT(d, r, g, b)\
1465 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1470 #include "imgconvert_template.c"
1472 /* rgb565 handling */
1474 #define RGB_NAME rgb565
1476 #define RGB_IN(r, g, b, s)\
1478 unsigned int v = ((const uint16_t *)(s))[0];\
1479 r = bitcopy_n(v >> (11 - 3), 3);\
1480 g = bitcopy_n(v >> (5 - 2), 2);\
1481 b = bitcopy_n(v << 3, 3);\
1484 #define RGB_OUT(d, r, g, b)\
1486 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1491 #include "imgconvert_template.c"
1493 /* bgr24 handling */
1495 #define RGB_NAME bgr24
1497 #define RGB_IN(r, g, b, s)\
1504 #define RGB_OUT(d, r, g, b)\
1513 #include "imgconvert_template.c"
1519 /* rgb24 handling */
1521 #define RGB_NAME rgb24
1524 #define RGB_IN(r, g, b, s)\
1531 #define RGB_OUT(d, r, g, b)\
1540 #include "imgconvert_template.c"
1542 /* rgb32 handling */
1544 #define RGB_NAME rgb32
1547 #define RGB_IN(r, g, b, s)\
1549 unsigned int v = ((const uint32_t *)(s))[0];\
1550 r = (v >> 16) & 0xff;\
1551 g = (v >> 8) & 0xff;\
1555 #define RGBA_IN(r, g, b, a, s)\
1557 unsigned int v = ((const uint32_t *)(s))[0];\
1558 a = (v >> 24) & 0xff;\
1559 r = (v >> 16) & 0xff;\
1560 g = (v >> 8) & 0xff;\
1564 #define RGBA_OUT(d, r, g, b, a)\
1566 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1571 #include "imgconvert_template.c"
1573 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1574 int width, int height, int xor_mask)
1576 const unsigned char *p;
1578 int v, dst_wrap, src_wrap;
1582 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1585 dst_wrap = dst->linesize[0] - width;
1586 for(y=0;y<height;y++) {
1589 v = *p++ ^ xor_mask;
1591 q[1] = -((v >> 6) & 1);
1592 q[2] = -((v >> 5) & 1);
1593 q[3] = -((v >> 4) & 1);
1594 q[4] = -((v >> 3) & 1);
1595 q[5] = -((v >> 2) & 1);
1596 q[6] = -((v >> 1) & 1);
1597 q[7] = -((v >> 0) & 1);
1602 v = *p++ ^ xor_mask;
1604 q[0] = -((v >> 7) & 1);
1614 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1615 int width, int height)
1617 mono_to_gray(dst, src, width, height, 0xff);
1620 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1621 int width, int height)
1623 mono_to_gray(dst, src, width, height, 0x00);
1626 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1627 int width, int height, int xor_mask)
1632 int j, b, v, n1, src_wrap, dst_wrap, y;
1635 src_wrap = src->linesize[0] - width;
1638 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1640 for(y=0;y<height;y++) {
1647 v = (v << 1) | (b >> 7);
1649 d[0] = v ^ xor_mask;
1659 v = (v << 1) | (b >> 7);
1662 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1670 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1671 int width, int height)
1673 gray_to_mono(dst, src, width, height, 0xff);
1676 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1677 int width, int height)
1679 gray_to_mono(dst, src, width, height, 0x00);
1682 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1683 int width, int height)
1685 int x, y, src_wrap, dst_wrap;
1688 src_wrap = src->linesize[0] - width;
1690 dst_wrap = dst->linesize[0] - width * 2;
1691 for(y=0; y<height; y++){
1692 for(x=0; x<width; x++){
1701 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1702 int width, int height)
1704 int x, y, src_wrap, dst_wrap;
1707 src_wrap = src->linesize[0] - width * 2;
1709 dst_wrap = dst->linesize[0] - width;
1710 for(y=0; y<height; y++){
1711 for(x=0; x<width; x++){
1720 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1721 int width, int height)
1723 gray16_to_gray(dst, src, width, height);
1726 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1727 int width, int height)
1729 AVPicture tmpsrc = *src;
1731 gray16_to_gray(dst, &tmpsrc, width, height);
1734 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1735 int width, int height)
1737 int x, y, src_wrap, dst_wrap;
1739 s = (uint16_t*)src->data[0];
1740 src_wrap = (src->linesize[0] - width * 2)/2;
1741 d = (uint16_t*)dst->data[0];
1742 dst_wrap = (dst->linesize[0] - width * 2)/2;
1743 for(y=0; y<height; y++){
1744 for(x=0; x<width; x++){
1745 *d++ = bswap_16(*s++);
1753 typedef struct ConvertEntry {
1754 void (*convert)(AVPicture *dst,
1755 const AVPicture *src, int width, int height);
1758 /* Add each new conversion function in this table. In order to be able
1759 to convert from any format to any format, the following constraints
1762 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1764 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1766 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1768 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1771 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1773 The other conversion functions are just optimizations for common cases.
1775 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1776 [PIX_FMT_YUV420P] = {
1777 [PIX_FMT_YUYV422] = {
1778 .convert = yuv420p_to_yuyv422,
1780 [PIX_FMT_RGB555] = {
1781 .convert = yuv420p_to_rgb555
1783 [PIX_FMT_RGB565] = {
1784 .convert = yuv420p_to_rgb565
1787 .convert = yuv420p_to_bgr24
1790 .convert = yuv420p_to_rgb24
1793 .convert = yuv420p_to_rgb32
1795 [PIX_FMT_UYVY422] = {
1796 .convert = yuv420p_to_uyvy422,
1799 [PIX_FMT_YUV422P] = {
1800 [PIX_FMT_YUYV422] = {
1801 .convert = yuv422p_to_yuyv422,
1803 [PIX_FMT_UYVY422] = {
1804 .convert = yuv422p_to_uyvy422,
1807 [PIX_FMT_YUV444P] = {
1809 .convert = yuv444p_to_rgb24
1812 [PIX_FMT_YUVJ420P] = {
1813 [PIX_FMT_RGB555] = {
1814 .convert = yuvj420p_to_rgb555
1816 [PIX_FMT_RGB565] = {
1817 .convert = yuvj420p_to_rgb565
1820 .convert = yuvj420p_to_bgr24
1823 .convert = yuvj420p_to_rgb24
1826 .convert = yuvj420p_to_rgb32
1829 [PIX_FMT_YUVJ444P] = {
1831 .convert = yuvj444p_to_rgb24
1834 [PIX_FMT_YUYV422] = {
1835 [PIX_FMT_YUV420P] = {
1836 .convert = yuyv422_to_yuv420p,
1838 [PIX_FMT_YUV422P] = {
1839 .convert = yuyv422_to_yuv422p,
1842 [PIX_FMT_UYVY422] = {
1843 [PIX_FMT_YUV420P] = {
1844 .convert = uyvy422_to_yuv420p,
1846 [PIX_FMT_YUV422P] = {
1847 .convert = uyvy422_to_yuv422p,
1851 [PIX_FMT_YUV420P] = {
1852 .convert = rgb24_to_yuv420p
1854 [PIX_FMT_RGB565] = {
1855 .convert = rgb24_to_rgb565
1857 [PIX_FMT_RGB555] = {
1858 .convert = rgb24_to_rgb555
1861 .convert = rgb24_to_rgb32
1864 .convert = rgb24_to_bgr24
1867 .convert = rgb24_to_gray
1870 .convert = rgb24_to_pal8
1872 [PIX_FMT_YUV444P] = {
1873 .convert = rgb24_to_yuv444p
1875 [PIX_FMT_YUVJ420P] = {
1876 .convert = rgb24_to_yuvj420p
1878 [PIX_FMT_YUVJ444P] = {
1879 .convert = rgb24_to_yuvj444p
1884 .convert = rgb32_to_rgb24
1887 .convert = rgb32_to_bgr24
1889 [PIX_FMT_RGB565] = {
1890 .convert = rgb32_to_rgb565
1892 [PIX_FMT_RGB555] = {
1893 .convert = rgb32_to_rgb555
1896 .convert = rgb32_to_pal8
1898 [PIX_FMT_YUV420P] = {
1899 .convert = rgb32_to_yuv420p
1902 .convert = rgb32_to_gray
1907 .convert = bgr24_to_rgb32
1910 .convert = bgr24_to_rgb24
1912 [PIX_FMT_YUV420P] = {
1913 .convert = bgr24_to_yuv420p
1916 .convert = bgr24_to_gray
1919 [PIX_FMT_RGB555] = {
1921 .convert = rgb555_to_rgb24
1924 .convert = rgb555_to_rgb32
1926 [PIX_FMT_YUV420P] = {
1927 .convert = rgb555_to_yuv420p
1930 .convert = rgb555_to_gray
1933 [PIX_FMT_RGB565] = {
1935 .convert = rgb565_to_rgb32
1938 .convert = rgb565_to_rgb24
1940 [PIX_FMT_YUV420P] = {
1941 .convert = rgb565_to_yuv420p
1944 .convert = rgb565_to_gray
1947 [PIX_FMT_GRAY16BE] = {
1949 .convert = gray16be_to_gray
1951 [PIX_FMT_GRAY16LE] = {
1952 .convert = gray16_to_gray16
1955 [PIX_FMT_GRAY16LE] = {
1957 .convert = gray16le_to_gray
1959 [PIX_FMT_GRAY16BE] = {
1960 .convert = gray16_to_gray16
1964 [PIX_FMT_RGB555] = {
1965 .convert = gray_to_rgb555
1967 [PIX_FMT_RGB565] = {
1968 .convert = gray_to_rgb565
1971 .convert = gray_to_rgb24
1974 .convert = gray_to_bgr24
1977 .convert = gray_to_rgb32
1979 [PIX_FMT_MONOWHITE] = {
1980 .convert = gray_to_monowhite
1982 [PIX_FMT_MONOBLACK] = {
1983 .convert = gray_to_monoblack
1985 [PIX_FMT_GRAY16LE] = {
1986 .convert = gray_to_gray16
1988 [PIX_FMT_GRAY16BE] = {
1989 .convert = gray_to_gray16
1992 [PIX_FMT_MONOWHITE] = {
1994 .convert = monowhite_to_gray
1997 [PIX_FMT_MONOBLACK] = {
1999 .convert = monoblack_to_gray
2003 [PIX_FMT_RGB555] = {
2004 .convert = pal8_to_rgb555
2006 [PIX_FMT_RGB565] = {
2007 .convert = pal8_to_rgb565
2010 .convert = pal8_to_bgr24
2013 .convert = pal8_to_rgb24
2016 .convert = pal8_to_rgb32
2019 [PIX_FMT_UYYVYY411] = {
2020 [PIX_FMT_YUV411P] = {
2021 .convert = uyyvyy411_to_yuv411p,
2027 int avpicture_alloc(AVPicture *picture,
2028 int pix_fmt, int width, int height)
2033 size = avpicture_get_size(pix_fmt, width, height);
2036 ptr = av_malloc(size);
2039 avpicture_fill(picture, ptr, pix_fmt, width, height);
2040 if(picture->data[1] && !picture->data[2])
2041 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
2045 memset(picture, 0, sizeof(AVPicture));
2049 void avpicture_free(AVPicture *picture)
2051 av_free(picture->data[0]);
2054 /* return true if yuv planar */
2055 static inline int is_yuv_planar(const PixFmtInfo *ps)
2057 return (ps->color_type == FF_COLOR_YUV ||
2058 ps->color_type == FF_COLOR_YUV_JPEG) &&
2059 ps->pixel_type == FF_PIXEL_PLANAR;
2062 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2063 int pix_fmt, int top_band, int left_band)
2068 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2071 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2072 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2074 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2075 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2076 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2078 dst->linesize[0] = src->linesize[0];
2079 dst->linesize[1] = src->linesize[1];
2080 dst->linesize[2] = src->linesize[2];
2084 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2085 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2094 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2095 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2097 for (i = 0; i < 3; i++) {
2098 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2099 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2101 if (padtop || padleft) {
2102 memset(dst->data[i], color[i],
2103 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2106 if (padleft || padright) {
2107 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2108 (dst->linesize[i] - (padright >> x_shift));
2109 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2110 for (y = 0; y < yheight; y++) {
2111 memset(optr, color[i], (padleft + padright) >> x_shift);
2112 optr += dst->linesize[i];
2116 if (src) { /* first line */
2117 uint8_t *iptr = src->data[i];
2118 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2119 (padleft >> x_shift);
2120 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
2121 iptr += src->linesize[i];
2122 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2123 (dst->linesize[i] - (padright >> x_shift));
2124 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2125 for (y = 0; y < yheight; y++) {
2126 memset(optr, color[i], (padleft + padright) >> x_shift);
2127 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2128 (width - padleft - padright) >> x_shift);
2129 iptr += src->linesize[i];
2130 optr += dst->linesize[i];
2134 if (padbottom || padright) {
2135 optr = dst->data[i] + dst->linesize[i] *
2136 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2137 memset(optr, color[i],dst->linesize[i] *
2138 (padbottom >> y_shift) + (padright >> x_shift));
2145 static uint8_t y_ccir_to_jpeg[256];
2146 static uint8_t y_jpeg_to_ccir[256];
2147 static uint8_t c_ccir_to_jpeg[256];
2148 static uint8_t c_jpeg_to_ccir[256];
2150 /* init various conversion tables */
2151 static void img_convert_init(void)
2154 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2156 for(i = 0;i < 256; i++) {
2157 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
2158 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
2159 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
2160 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
2164 /* apply to each pixel the given table */
2165 static void img_apply_table(uint8_t *dst, int dst_wrap,
2166 const uint8_t *src, int src_wrap,
2167 int width, int height, const uint8_t *table1)
2172 const uint8_t *table;
2175 for(;height > 0; height--) {
2199 /* XXX: use generic filter ? */
2200 /* XXX: in most cases, the sampling position is incorrect */
2203 static void shrink41(uint8_t *dst, int dst_wrap,
2204 const uint8_t *src, int src_wrap,
2205 int width, int height)
2211 for(;height > 0; height--) {
2214 for(w = width;w > 0; w--) {
2215 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
2225 static void shrink21(uint8_t *dst, int dst_wrap,
2226 const uint8_t *src, int src_wrap,
2227 int width, int height)
2233 for(;height > 0; height--) {
2236 for(w = width;w > 0; w--) {
2237 d[0] = (s[0] + s[1]) >> 1;
2247 static void shrink12(uint8_t *dst, int dst_wrap,
2248 const uint8_t *src, int src_wrap,
2249 int width, int height)
2253 const uint8_t *s1, *s2;
2255 for(;height > 0; height--) {
2259 for(w = width;w >= 4; w-=4) {
2260 d[0] = (s1[0] + s2[0]) >> 1;
2261 d[1] = (s1[1] + s2[1]) >> 1;
2262 d[2] = (s1[2] + s2[2]) >> 1;
2263 d[3] = (s1[3] + s2[3]) >> 1;
2269 d[0] = (s1[0] + s2[0]) >> 1;
2274 src += 2 * src_wrap;
2279 static void grow21_line(uint8_t *dst, const uint8_t *src,
2288 for(w = width;w >= 4; w-=4) {
2289 d[1] = d[0] = s1[0];
2290 d[3] = d[2] = s1[1];
2294 for(;w >= 2; w -= 2) {
2295 d[1] = d[0] = s1[0];
2299 /* only needed if width is not a multiple of two */
2300 /* XXX: veryfy that */
2306 static void grow41_line(uint8_t *dst, const uint8_t *src,
2315 for(w = width;w >= 4; w-=4) {
2327 static void grow21(uint8_t *dst, int dst_wrap,
2328 const uint8_t *src, int src_wrap,
2329 int width, int height)
2331 for(;height > 0; height--) {
2332 grow21_line(dst, src, width);
2339 static void grow12(uint8_t *dst, int dst_wrap,
2340 const uint8_t *src, int src_wrap,
2341 int width, int height)
2343 for(;height > 0; height-=2) {
2344 memcpy(dst, src, width);
2346 memcpy(dst, src, width);
2353 static void grow22(uint8_t *dst, int dst_wrap,
2354 const uint8_t *src, int src_wrap,
2355 int width, int height)
2357 for(;height > 0; height--) {
2358 grow21_line(dst, src, width);
2366 static void grow41(uint8_t *dst, int dst_wrap,
2367 const uint8_t *src, int src_wrap,
2368 int width, int height)
2370 for(;height > 0; height--) {
2371 grow41_line(dst, src, width);
2378 static void grow44(uint8_t *dst, int dst_wrap,
2379 const uint8_t *src, int src_wrap,
2380 int width, int height)
2382 for(;height > 0; height--) {
2383 grow41_line(dst, src, width);
2384 if ((height & 3) == 1)
2391 static void conv411(uint8_t *dst, int dst_wrap,
2392 const uint8_t *src, int src_wrap,
2393 int width, int height)
2396 const uint8_t *s1, *s2;
2401 for(;height > 0; height--) {
2403 s2 = src + src_wrap;
2405 for(w = width;w > 0; w--) {
2406 c = (s1[0] + s2[0]) >> 1;
2413 src += src_wrap * 2;
2418 /* XXX: always use linesize. Return -1 if not supported */
2419 int img_convert(AVPicture *dst, int dst_pix_fmt,
2420 const AVPicture *src, int src_pix_fmt,
2421 int src_width, int src_height)
2423 static int initialized;
2424 int i, ret, dst_width, dst_height, int_pix_fmt;
2425 const PixFmtInfo *src_pix, *dst_pix;
2426 const ConvertEntry *ce;
2427 AVPicture tmp1, *tmp = &tmp1;
2429 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2430 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2432 if (src_width <= 0 || src_height <= 0)
2440 dst_width = src_width;
2441 dst_height = src_height;
2443 dst_pix = &pix_fmt_info[dst_pix_fmt];
2444 src_pix = &pix_fmt_info[src_pix_fmt];
2445 if (src_pix_fmt == dst_pix_fmt) {
2446 /* no conversion needed: just copy */
2447 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2451 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2453 /* specific conversion routine */
2454 ce->convert(dst, src, dst_width, dst_height);
2459 if (is_yuv_planar(dst_pix) &&
2460 src_pix_fmt == PIX_FMT_GRAY8) {
2464 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2465 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2466 src->data[0], src->linesize[0],
2467 dst_width, dst_height);
2469 img_apply_table(dst->data[0], dst->linesize[0],
2470 src->data[0], src->linesize[0],
2471 dst_width, dst_height,
2474 /* fill U and V with 128 */
2477 w >>= dst_pix->x_chroma_shift;
2478 h >>= dst_pix->y_chroma_shift;
2479 for(i = 1; i <= 2; i++) {
2481 for(y = 0; y< h; y++) {
2483 d += dst->linesize[i];
2490 if (is_yuv_planar(src_pix) &&
2491 dst_pix_fmt == PIX_FMT_GRAY8) {
2492 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2493 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2494 src->data[0], src->linesize[0],
2495 dst_width, dst_height);
2497 img_apply_table(dst->data[0], dst->linesize[0],
2498 src->data[0], src->linesize[0],
2499 dst_width, dst_height,
2505 /* YUV to YUV planar */
2506 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2507 int x_shift, y_shift, w, h, xy_shift;
2508 void (*resize_func)(uint8_t *dst, int dst_wrap,
2509 const uint8_t *src, int src_wrap,
2510 int width, int height);
2512 /* compute chroma size of the smallest dimensions */
2515 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2516 w >>= dst_pix->x_chroma_shift;
2518 w >>= src_pix->x_chroma_shift;
2519 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2520 h >>= dst_pix->y_chroma_shift;
2522 h >>= src_pix->y_chroma_shift;
2524 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2525 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2526 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2527 /* there must be filters for conversion at least from and to
2531 resize_func = ff_img_copy_plane;
2534 resize_func = shrink21;
2537 resize_func = shrink41;
2540 resize_func = shrink12;
2543 resize_func = ff_shrink22;
2546 resize_func = ff_shrink44;
2549 resize_func = grow21;
2552 resize_func = grow12;
2555 resize_func = grow41;
2558 resize_func = grow22;
2561 resize_func = grow44;
2564 resize_func = conv411;
2567 /* currently not handled */
2568 goto no_chroma_filter;
2571 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2572 src->data[0], src->linesize[0],
2573 dst_width, dst_height);
2575 for(i = 1;i <= 2; i++)
2576 resize_func(dst->data[i], dst->linesize[i],
2577 src->data[i], src->linesize[i],
2578 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2579 /* if yuv color space conversion is needed, we do it here on
2580 the destination image */
2581 if (dst_pix->color_type != src_pix->color_type) {
2582 const uint8_t *y_table, *c_table;
2583 if (dst_pix->color_type == FF_COLOR_YUV) {
2584 y_table = y_jpeg_to_ccir;
2585 c_table = c_jpeg_to_ccir;
2587 y_table = y_ccir_to_jpeg;
2588 c_table = c_ccir_to_jpeg;
2590 img_apply_table(dst->data[0], dst->linesize[0],
2591 dst->data[0], dst->linesize[0],
2592 dst_width, dst_height,
2595 for(i = 1;i <= 2; i++)
2596 img_apply_table(dst->data[i], dst->linesize[i],
2597 dst->data[i], dst->linesize[i],
2598 dst_width>>dst_pix->x_chroma_shift,
2599 dst_height>>dst_pix->y_chroma_shift,
2606 /* try to use an intermediate format */
2607 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2608 dst_pix_fmt == PIX_FMT_YUYV422) {
2609 /* specific case: convert to YUV422P first */
2610 int_pix_fmt = PIX_FMT_YUV422P;
2611 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2612 dst_pix_fmt == PIX_FMT_UYVY422) {
2613 /* specific case: convert to YUV422P first */
2614 int_pix_fmt = PIX_FMT_YUV422P;
2615 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2616 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2617 /* specific case: convert to YUV411P first */
2618 int_pix_fmt = PIX_FMT_YUV411P;
2619 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2620 src_pix_fmt != PIX_FMT_GRAY8) ||
2621 (dst_pix->color_type == FF_COLOR_GRAY &&
2622 dst_pix_fmt != PIX_FMT_GRAY8)) {
2623 /* gray8 is the normalized format */
2624 int_pix_fmt = PIX_FMT_GRAY8;
2625 } else if ((is_yuv_planar(src_pix) &&
2626 src_pix_fmt != PIX_FMT_YUV444P &&
2627 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2628 /* yuv444 is the normalized format */
2629 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2630 int_pix_fmt = PIX_FMT_YUVJ444P;
2632 int_pix_fmt = PIX_FMT_YUV444P;
2633 } else if ((is_yuv_planar(dst_pix) &&
2634 dst_pix_fmt != PIX_FMT_YUV444P &&
2635 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2636 /* yuv444 is the normalized format */
2637 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2638 int_pix_fmt = PIX_FMT_YUVJ444P;
2640 int_pix_fmt = PIX_FMT_YUV444P;
2642 /* the two formats are rgb or gray8 or yuv[j]444p */
2643 if (src_pix->is_alpha && dst_pix->is_alpha)
2644 int_pix_fmt = PIX_FMT_RGB32;
2646 int_pix_fmt = PIX_FMT_RGB24;
2648 if (src_pix_fmt == int_pix_fmt)
2650 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2653 if (img_convert(tmp, int_pix_fmt,
2654 src, src_pix_fmt, src_width, src_height) < 0)
2656 if (img_convert(dst, dst_pix_fmt,
2657 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2661 avpicture_free(tmp);
2666 /* NOTE: we scan all the pixels to have an exact information */
2667 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2669 const unsigned char *p;
2670 int src_wrap, ret, x, y;
2672 uint32_t *palette = (uint32_t *)src->data[1];
2675 src_wrap = src->linesize[0] - width;
2677 for(y=0;y<height;y++) {
2678 for(x=0;x<width;x++) {
2679 a = palette[p[0]] >> 24;
2681 ret |= FF_ALPHA_TRANSP;
2682 } else if (a != 0xff) {
2683 ret |= FF_ALPHA_SEMI_TRANSP;
2692 int img_get_alpha_info(const AVPicture *src,
2693 int pix_fmt, int width, int height)
2695 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2698 pf = &pix_fmt_info[pix_fmt];
2699 /* no alpha can be represented in format */
2704 ret = get_alpha_info_rgb32(src, width, height);
2707 ret = get_alpha_info_pal8(src, width, height);
2710 /* we do not know, so everything is indicated */
2711 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2718 #define DEINT_INPLACE_LINE_LUM \
2719 movd_m2r(lum_m4[0],mm0);\
2720 movd_m2r(lum_m3[0],mm1);\
2721 movd_m2r(lum_m2[0],mm2);\
2722 movd_m2r(lum_m1[0],mm3);\
2723 movd_m2r(lum[0],mm4);\
2724 punpcklbw_r2r(mm7,mm0);\
2725 movd_r2m(mm2,lum_m4[0]);\
2726 punpcklbw_r2r(mm7,mm1);\
2727 punpcklbw_r2r(mm7,mm2);\
2728 punpcklbw_r2r(mm7,mm3);\
2729 punpcklbw_r2r(mm7,mm4);\
2730 paddw_r2r(mm3,mm1);\
2732 paddw_r2r(mm4,mm0);\
2734 paddw_r2r(mm6,mm2);\
2735 paddw_r2r(mm2,mm1);\
2736 psubusw_r2r(mm0,mm1);\
2738 packuswb_r2r(mm7,mm1);\
2739 movd_r2m(mm1,lum_m2[0]);
2741 #define DEINT_LINE_LUM \
2742 movd_m2r(lum_m4[0],mm0);\
2743 movd_m2r(lum_m3[0],mm1);\
2744 movd_m2r(lum_m2[0],mm2);\
2745 movd_m2r(lum_m1[0],mm3);\
2746 movd_m2r(lum[0],mm4);\
2747 punpcklbw_r2r(mm7,mm0);\
2748 punpcklbw_r2r(mm7,mm1);\
2749 punpcklbw_r2r(mm7,mm2);\
2750 punpcklbw_r2r(mm7,mm3);\
2751 punpcklbw_r2r(mm7,mm4);\
2752 paddw_r2r(mm3,mm1);\
2754 paddw_r2r(mm4,mm0);\
2756 paddw_r2r(mm6,mm2);\
2757 paddw_r2r(mm2,mm1);\
2758 psubusw_r2r(mm0,mm1);\
2760 packuswb_r2r(mm7,mm1);\
2761 movd_r2m(mm1,dst[0]);
2764 /* filter parameters: [-1 4 2 4 -1] // 8 */
2765 static void deinterlace_line(uint8_t *dst,
2766 const uint8_t *lum_m4, const uint8_t *lum_m3,
2767 const uint8_t *lum_m2, const uint8_t *lum_m1,
2772 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2775 for(;size > 0;size--) {
2777 sum += lum_m3[0] << 2;
2778 sum += lum_m2[0] << 1;
2779 sum += lum_m1[0] << 2;
2781 dst[0] = cm[(sum + 4) >> 3];
2793 movq_m2r(ff_pw_4,mm6);
2795 for (;size > 3; size-=4) {
2806 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2810 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2813 for(;size > 0;size--) {
2815 sum += lum_m3[0] << 2;
2816 sum += lum_m2[0] << 1;
2817 lum_m4[0]=lum_m2[0];
2818 sum += lum_m1[0] << 2;
2820 lum_m2[0] = cm[(sum + 4) >> 3];
2831 movq_m2r(ff_pw_4,mm6);
2833 for (;size > 3; size-=4) {
2834 DEINT_INPLACE_LINE_LUM
2844 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2845 top field is copied as is, but the bottom field is deinterlaced
2846 against the top field. */
2847 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2848 const uint8_t *src1, int src_wrap,
2849 int width, int height)
2851 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2856 src_0=&src_m1[src_wrap];
2857 src_p1=&src_0[src_wrap];
2858 src_p2=&src_p1[src_wrap];
2859 for(y=0;y<(height-2);y+=2) {
2860 memcpy(dst,src_m1,width);
2862 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2866 src_p1 += 2*src_wrap;
2867 src_p2 += 2*src_wrap;
2870 memcpy(dst,src_m1,width);
2873 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2876 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2877 int width, int height)
2879 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2882 buf = (uint8_t*)av_malloc(width);
2885 memcpy(buf,src_m1,width);
2886 src_0=&src_m1[src_wrap];
2887 src_p1=&src_0[src_wrap];
2888 src_p2=&src_p1[src_wrap];
2889 for(y=0;y<(height-2);y+=2) {
2890 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2893 src_p1 += 2*src_wrap;
2894 src_p2 += 2*src_wrap;
2897 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2901 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2902 int pix_fmt, int width, int height)
2906 if (pix_fmt != PIX_FMT_YUV420P &&
2907 pix_fmt != PIX_FMT_YUV422P &&
2908 pix_fmt != PIX_FMT_YUV444P &&
2909 pix_fmt != PIX_FMT_YUV411P &&
2910 pix_fmt != PIX_FMT_GRAY8)
2912 if ((width & 3) != 0 || (height & 3) != 0)
2918 case PIX_FMT_YUV420P:
2922 case PIX_FMT_YUV422P:
2925 case PIX_FMT_YUV411P:
2931 if (pix_fmt == PIX_FMT_GRAY8) {
2936 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2939 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2940 src->data[i], src->linesize[i],