2 * Misc image conversion routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * misc image conversion routines
28 * - write 'ffimg' program to test all the image related stuff
29 * - move all api to slice based system
30 * - integrate deinterlacing, postprocessing and scaling in the conversion process
35 #include "colorspace.h"
39 #include "x86/dsputil_mmx.h"
42 #define xglue(x, y) x ## y
43 #define glue(x, y) xglue(x, y)
45 #define FF_COLOR_RGB 0 /**< RGB color space */
46 #define FF_COLOR_GRAY 1 /**< gray color space */
47 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
48 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
50 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
51 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
52 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
54 typedef struct PixFmtInfo {
56 uint8_t nb_channels; /**< number of channels (including alpha) */
57 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
58 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
59 uint8_t is_alpha : 1; /**< true if alpha can be specified */
60 uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
61 uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
62 uint8_t depth; /**< bit depth of the color components */
65 /* this table gives more information about formats */
66 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
71 .color_type = FF_COLOR_YUV,
72 .pixel_type = FF_PIXEL_PLANAR,
74 .x_chroma_shift = 1, .y_chroma_shift = 1,
79 .color_type = FF_COLOR_YUV,
80 .pixel_type = FF_PIXEL_PLANAR,
82 .x_chroma_shift = 1, .y_chroma_shift = 0,
87 .color_type = FF_COLOR_YUV,
88 .pixel_type = FF_PIXEL_PLANAR,
90 .x_chroma_shift = 0, .y_chroma_shift = 0,
95 .color_type = FF_COLOR_YUV,
96 .pixel_type = FF_PIXEL_PACKED,
98 .x_chroma_shift = 1, .y_chroma_shift = 0,
100 [PIX_FMT_UYVY422] = {
103 .color_type = FF_COLOR_YUV,
104 .pixel_type = FF_PIXEL_PACKED,
106 .x_chroma_shift = 1, .y_chroma_shift = 0,
108 [PIX_FMT_YUV410P] = {
111 .color_type = FF_COLOR_YUV,
112 .pixel_type = FF_PIXEL_PLANAR,
114 .x_chroma_shift = 2, .y_chroma_shift = 2,
116 [PIX_FMT_YUV411P] = {
119 .color_type = FF_COLOR_YUV,
120 .pixel_type = FF_PIXEL_PLANAR,
122 .x_chroma_shift = 2, .y_chroma_shift = 0,
124 [PIX_FMT_YUV440P] = {
127 .color_type = FF_COLOR_YUV,
128 .pixel_type = FF_PIXEL_PLANAR,
130 .x_chroma_shift = 0, .y_chroma_shift = 1,
133 /* YUV formats with alpha plane */
134 [PIX_FMT_YUVA420P] = {
137 .color_type = FF_COLOR_YUV,
138 .pixel_type = FF_PIXEL_PLANAR,
140 .x_chroma_shift = 1, .y_chroma_shift = 1,
144 [PIX_FMT_YUVJ420P] = {
147 .color_type = FF_COLOR_YUV_JPEG,
148 .pixel_type = FF_PIXEL_PLANAR,
150 .x_chroma_shift = 1, .y_chroma_shift = 1,
152 [PIX_FMT_YUVJ422P] = {
155 .color_type = FF_COLOR_YUV_JPEG,
156 .pixel_type = FF_PIXEL_PLANAR,
158 .x_chroma_shift = 1, .y_chroma_shift = 0,
160 [PIX_FMT_YUVJ444P] = {
163 .color_type = FF_COLOR_YUV_JPEG,
164 .pixel_type = FF_PIXEL_PLANAR,
166 .x_chroma_shift = 0, .y_chroma_shift = 0,
168 [PIX_FMT_YUVJ440P] = {
171 .color_type = FF_COLOR_YUV_JPEG,
172 .pixel_type = FF_PIXEL_PLANAR,
174 .x_chroma_shift = 0, .y_chroma_shift = 1,
181 .color_type = FF_COLOR_RGB,
182 .pixel_type = FF_PIXEL_PACKED,
184 .x_chroma_shift = 0, .y_chroma_shift = 0,
189 .color_type = FF_COLOR_RGB,
190 .pixel_type = FF_PIXEL_PACKED,
192 .x_chroma_shift = 0, .y_chroma_shift = 0,
196 .nb_channels = 4, .is_alpha = 1,
197 .color_type = FF_COLOR_RGB,
198 .pixel_type = FF_PIXEL_PACKED,
200 .x_chroma_shift = 0, .y_chroma_shift = 0,
205 .color_type = FF_COLOR_RGB,
206 .pixel_type = FF_PIXEL_PACKED,
208 .x_chroma_shift = 0, .y_chroma_shift = 0,
213 .color_type = FF_COLOR_RGB,
214 .pixel_type = FF_PIXEL_PACKED,
216 .x_chroma_shift = 0, .y_chroma_shift = 0,
219 /* gray / mono formats */
220 [PIX_FMT_GRAY16BE] = {
223 .color_type = FF_COLOR_GRAY,
224 .pixel_type = FF_PIXEL_PLANAR,
227 [PIX_FMT_GRAY16LE] = {
230 .color_type = FF_COLOR_GRAY,
231 .pixel_type = FF_PIXEL_PLANAR,
237 .color_type = FF_COLOR_GRAY,
238 .pixel_type = FF_PIXEL_PLANAR,
241 [PIX_FMT_MONOWHITE] = {
244 .color_type = FF_COLOR_GRAY,
245 .pixel_type = FF_PIXEL_PLANAR,
248 [PIX_FMT_MONOBLACK] = {
251 .color_type = FF_COLOR_GRAY,
252 .pixel_type = FF_PIXEL_PLANAR,
256 /* paletted formats */
259 .nb_channels = 4, .is_alpha = 1,
260 .color_type = FF_COLOR_RGB,
261 .pixel_type = FF_PIXEL_PALETTE,
264 [PIX_FMT_XVMC_MPEG2_MC] = {
267 [PIX_FMT_XVMC_MPEG2_IDCT] = {
270 [PIX_FMT_VDPAU_H264] = {
271 .name = "vdpau_h264",
273 [PIX_FMT_UYYVYY411] = {
276 .color_type = FF_COLOR_YUV,
277 .pixel_type = FF_PIXEL_PACKED,
279 .x_chroma_shift = 2, .y_chroma_shift = 0,
283 .nb_channels = 4, .is_alpha = 1,
284 .color_type = FF_COLOR_RGB,
285 .pixel_type = FF_PIXEL_PACKED,
287 .x_chroma_shift = 0, .y_chroma_shift = 0,
292 .color_type = FF_COLOR_RGB,
293 .pixel_type = FF_PIXEL_PACKED,
295 .x_chroma_shift = 0, .y_chroma_shift = 0,
300 .color_type = FF_COLOR_RGB,
301 .pixel_type = FF_PIXEL_PACKED,
303 .x_chroma_shift = 0, .y_chroma_shift = 0,
308 .color_type = FF_COLOR_RGB,
309 .pixel_type = FF_PIXEL_PACKED,
311 .x_chroma_shift = 0, .y_chroma_shift = 0,
316 .color_type = FF_COLOR_RGB,
317 .pixel_type = FF_PIXEL_PACKED,
319 .x_chroma_shift = 0, .y_chroma_shift = 0,
321 [PIX_FMT_RGB4_BYTE] = {
324 .color_type = FF_COLOR_RGB,
325 .pixel_type = FF_PIXEL_PACKED,
327 .x_chroma_shift = 0, .y_chroma_shift = 0,
332 .color_type = FF_COLOR_RGB,
333 .pixel_type = FF_PIXEL_PACKED,
335 .x_chroma_shift = 0, .y_chroma_shift = 0,
340 .color_type = FF_COLOR_RGB,
341 .pixel_type = FF_PIXEL_PACKED,
343 .x_chroma_shift = 0, .y_chroma_shift = 0,
345 [PIX_FMT_BGR4_BYTE] = {
348 .color_type = FF_COLOR_RGB,
349 .pixel_type = FF_PIXEL_PACKED,
351 .x_chroma_shift = 0, .y_chroma_shift = 0,
356 .color_type = FF_COLOR_YUV,
357 .pixel_type = FF_PIXEL_PLANAR,
359 .x_chroma_shift = 1, .y_chroma_shift = 1,
364 .color_type = FF_COLOR_YUV,
365 .pixel_type = FF_PIXEL_PLANAR,
367 .x_chroma_shift = 1, .y_chroma_shift = 1,
370 [PIX_FMT_BGR32_1] = {
372 .nb_channels = 4, .is_alpha = 1,
373 .color_type = FF_COLOR_RGB,
374 .pixel_type = FF_PIXEL_PACKED,
376 .x_chroma_shift = 0, .y_chroma_shift = 0,
378 [PIX_FMT_RGB32_1] = {
380 .nb_channels = 4, .is_alpha = 1,
381 .color_type = FF_COLOR_RGB,
382 .pixel_type = FF_PIXEL_PACKED,
384 .x_chroma_shift = 0, .y_chroma_shift = 0,
388 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
390 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
391 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
394 const char *avcodec_get_pix_fmt_name(int pix_fmt)
396 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
399 return pix_fmt_info[pix_fmt].name;
402 enum PixelFormat avcodec_get_pix_fmt(const char* name)
406 for (i=0; i < PIX_FMT_NB; i++)
407 if (!strcmp(pix_fmt_info[i].name, name))
412 void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
416 snprintf (buf, buf_size,
417 "name " " nb_channels" " depth" " is_alpha"
420 PixFmtInfo info= pix_fmt_info[pix_fmt];
422 char is_alpha_char= info.is_alpha ? 'y' : 'n';
424 snprintf (buf, buf_size,
425 "%-10s" " %1d " " %2d " " %c ",
434 int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
437 const PixFmtInfo *pinfo;
439 memset(picture->linesize, 0, sizeof(picture->linesize));
441 pinfo = &pix_fmt_info[pix_fmt];
443 case PIX_FMT_YUV420P:
444 case PIX_FMT_YUV422P:
445 case PIX_FMT_YUV444P:
446 case PIX_FMT_YUV410P:
447 case PIX_FMT_YUV411P:
448 case PIX_FMT_YUV440P:
449 case PIX_FMT_YUVJ420P:
450 case PIX_FMT_YUVJ422P:
451 case PIX_FMT_YUVJ444P:
452 case PIX_FMT_YUVJ440P:
453 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
454 picture->linesize[0] = width;
455 picture->linesize[1] = w2;
456 picture->linesize[2] = w2;
458 case PIX_FMT_YUVA420P:
459 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
460 picture->linesize[0] = width;
461 picture->linesize[1] = w2;
462 picture->linesize[2] = w2;
463 picture->linesize[3] = width;
467 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
468 picture->linesize[0] = width;
469 picture->linesize[1] = w2;
473 picture->linesize[0] = width * 3;
477 case PIX_FMT_RGB32_1:
478 case PIX_FMT_BGR32_1:
479 picture->linesize[0] = width * 4;
481 case PIX_FMT_GRAY16BE:
482 case PIX_FMT_GRAY16LE:
487 case PIX_FMT_YUYV422:
488 picture->linesize[0] = width * 2;
490 case PIX_FMT_UYVY422:
491 picture->linesize[0] = width * 2;
493 case PIX_FMT_UYYVYY411:
494 picture->linesize[0] = width + width/2;
498 case PIX_FMT_RGB4_BYTE:
499 case PIX_FMT_BGR4_BYTE:
501 picture->linesize[0] = width;
505 picture->linesize[0] = width / 2;
507 case PIX_FMT_MONOWHITE:
508 case PIX_FMT_MONOBLACK:
509 picture->linesize[0] = (width + 7) >> 3;
512 picture->linesize[0] = width;
513 picture->linesize[1] = 4;
521 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
525 const PixFmtInfo *pinfo;
527 pinfo = &pix_fmt_info[pix_fmt];
528 size = picture->linesize[0] * height;
530 case PIX_FMT_YUV420P:
531 case PIX_FMT_YUV422P:
532 case PIX_FMT_YUV444P:
533 case PIX_FMT_YUV410P:
534 case PIX_FMT_YUV411P:
535 case PIX_FMT_YUV440P:
536 case PIX_FMT_YUVJ420P:
537 case PIX_FMT_YUVJ422P:
538 case PIX_FMT_YUVJ444P:
539 case PIX_FMT_YUVJ440P:
540 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
541 size2 = picture->linesize[1] * h2;
542 picture->data[0] = ptr;
543 picture->data[1] = picture->data[0] + size;
544 picture->data[2] = picture->data[1] + size2;
545 picture->data[3] = NULL;
546 return size + 2 * size2;
547 case PIX_FMT_YUVA420P:
548 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
549 size2 = picture->linesize[1] * h2;
550 picture->data[0] = ptr;
551 picture->data[1] = picture->data[0] + size;
552 picture->data[2] = picture->data[1] + size2;
553 picture->data[3] = picture->data[1] + size2 + size2;
554 return 2 * size + 2 * size2;
557 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
558 size2 = picture->linesize[1] * h2 * 2;
559 picture->data[0] = ptr;
560 picture->data[1] = picture->data[0] + size;
561 picture->data[2] = NULL;
562 picture->data[3] = NULL;
563 return size + 2 * size2;
568 case PIX_FMT_RGB32_1:
569 case PIX_FMT_BGR32_1:
570 case PIX_FMT_GRAY16BE:
571 case PIX_FMT_GRAY16LE:
576 case PIX_FMT_YUYV422:
577 case PIX_FMT_UYVY422:
578 case PIX_FMT_UYYVYY411:
581 case PIX_FMT_RGB4_BYTE:
582 case PIX_FMT_BGR4_BYTE:
586 case PIX_FMT_MONOWHITE:
587 case PIX_FMT_MONOBLACK:
588 picture->data[0] = ptr;
589 picture->data[1] = NULL;
590 picture->data[2] = NULL;
591 picture->data[3] = NULL;
594 size2 = (size + 3) & ~3;
595 picture->data[0] = ptr;
596 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
597 picture->data[2] = NULL;
598 picture->data[3] = NULL;
599 return size2 + 256 * 4;
601 picture->data[0] = NULL;
602 picture->data[1] = NULL;
603 picture->data[2] = NULL;
604 picture->data[3] = NULL;
609 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
610 int pix_fmt, int width, int height)
613 if(avcodec_check_dimensions(NULL, width, height))
616 if (ff_fill_linesize(picture, pix_fmt, width))
619 return ff_fill_pointer(picture, ptr, pix_fmt, height);
622 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
623 unsigned char *dest, int dest_size)
625 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
626 int i, j, w, h, data_planes;
627 const unsigned char* s;
628 int size = avpicture_get_size(pix_fmt, width, height);
630 if (size > dest_size || size < 0)
633 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
634 if (pix_fmt == PIX_FMT_YUYV422 ||
635 pix_fmt == PIX_FMT_UYVY422 ||
636 pix_fmt == PIX_FMT_BGR565 ||
637 pix_fmt == PIX_FMT_BGR555 ||
638 pix_fmt == PIX_FMT_RGB565 ||
639 pix_fmt == PIX_FMT_RGB555)
641 else if (pix_fmt == PIX_FMT_UYYVYY411)
643 else if (pix_fmt == PIX_FMT_PAL8)
646 w = width * (pf->depth * pf->nb_channels / 8);
651 data_planes = pf->nb_channels;
652 w = (width*pf->depth + 7)/8;
656 for (i=0; i<data_planes; i++) {
658 w = width >> pf->x_chroma_shift;
659 h = height >> pf->y_chroma_shift;
665 s += src->linesize[i];
669 if (pf->pixel_type == FF_PIXEL_PALETTE)
670 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
675 int avpicture_get_size(int pix_fmt, int width, int height)
677 AVPicture dummy_pict;
678 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
681 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
684 const PixFmtInfo *pf, *ps;
687 ps = &pix_fmt_info[src_pix_fmt];
688 pf = &pix_fmt_info[dst_pix_fmt];
692 pf = &pix_fmt_info[dst_pix_fmt];
693 if (pf->depth < ps->depth ||
694 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
695 loss |= FF_LOSS_DEPTH;
696 if (pf->x_chroma_shift > ps->x_chroma_shift ||
697 pf->y_chroma_shift > ps->y_chroma_shift)
698 loss |= FF_LOSS_RESOLUTION;
699 switch(pf->color_type) {
701 if (ps->color_type != FF_COLOR_RGB &&
702 ps->color_type != FF_COLOR_GRAY)
703 loss |= FF_LOSS_COLORSPACE;
706 if (ps->color_type != FF_COLOR_GRAY)
707 loss |= FF_LOSS_COLORSPACE;
710 if (ps->color_type != FF_COLOR_YUV)
711 loss |= FF_LOSS_COLORSPACE;
713 case FF_COLOR_YUV_JPEG:
714 if (ps->color_type != FF_COLOR_YUV_JPEG &&
715 ps->color_type != FF_COLOR_YUV &&
716 ps->color_type != FF_COLOR_GRAY)
717 loss |= FF_LOSS_COLORSPACE;
721 if (ps->color_type != pf->color_type)
722 loss |= FF_LOSS_COLORSPACE;
725 if (pf->color_type == FF_COLOR_GRAY &&
726 ps->color_type != FF_COLOR_GRAY)
727 loss |= FF_LOSS_CHROMA;
728 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
729 loss |= FF_LOSS_ALPHA;
730 if (pf->pixel_type == FF_PIXEL_PALETTE &&
731 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
732 loss |= FF_LOSS_COLORQUANT;
736 static int avg_bits_per_pixel(int pix_fmt)
739 const PixFmtInfo *pf;
741 pf = &pix_fmt_info[pix_fmt];
742 switch(pf->pixel_type) {
743 case FF_PIXEL_PACKED:
745 case PIX_FMT_YUYV422:
746 case PIX_FMT_UYVY422:
753 case PIX_FMT_UYYVYY411:
757 bits = pf->depth * pf->nb_channels;
761 case FF_PIXEL_PLANAR:
762 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
763 bits = pf->depth * pf->nb_channels;
765 bits = pf->depth + ((2 * pf->depth) >>
766 (pf->x_chroma_shift + pf->y_chroma_shift));
769 case FF_PIXEL_PALETTE:
779 static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
784 int dist, i, loss, min_dist, dst_pix_fmt;
786 /* find exact color match with smallest size */
788 min_dist = 0x7fffffff;
789 for(i = 0;i < PIX_FMT_NB; i++) {
790 if (pix_fmt_mask & (1ULL << i)) {
791 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
793 dist = avg_bits_per_pixel(i);
794 if (dist < min_dist) {
804 int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
805 int has_alpha, int *loss_ptr)
807 int dst_pix_fmt, loss_mask, i;
808 static const int loss_mask_order[] = {
809 ~0, /* no loss first */
812 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
818 /* try with successive loss */
821 loss_mask = loss_mask_order[i++];
822 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
823 has_alpha, loss_mask);
824 if (dst_pix_fmt >= 0)
832 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
836 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
837 const uint8_t *src, int src_wrap,
838 int width, int height)
842 for(;height > 0; height--) {
843 memcpy(dst, src, width);
849 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
852 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
854 pf = &pix_fmt_info[pix_fmt];
855 switch(pf->pixel_type) {
856 case FF_PIXEL_PACKED:
858 case PIX_FMT_YUYV422:
859 case PIX_FMT_UYVY422:
866 case PIX_FMT_UYYVYY411:
870 bits = pf->depth * pf->nb_channels;
873 return (width * bits + 7) >> 3;
875 case FF_PIXEL_PLANAR:
876 if (plane == 1 || plane == 2)
877 width= -((-width)>>pf->x_chroma_shift);
879 return (width * pf->depth + 7) >> 3;
881 case FF_PIXEL_PALETTE:
890 void av_picture_copy(AVPicture *dst, const AVPicture *src,
891 int pix_fmt, int width, int height)
894 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
896 pf = &pix_fmt_info[pix_fmt];
897 switch(pf->pixel_type) {
898 case FF_PIXEL_PACKED:
899 case FF_PIXEL_PLANAR:
900 for(i = 0; i < pf->nb_channels; i++) {
902 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
904 if (i == 1 || i == 2) {
905 h= -((-height)>>pf->y_chroma_shift);
907 ff_img_copy_plane(dst->data[i], dst->linesize[i],
908 src->data[i], src->linesize[i],
912 case FF_PIXEL_PALETTE:
913 ff_img_copy_plane(dst->data[0], dst->linesize[0],
914 src->data[0], src->linesize[0],
916 /* copy the palette */
917 ff_img_copy_plane(dst->data[1], dst->linesize[1],
918 src->data[1], src->linesize[1],
924 /* XXX: totally non optimized */
926 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
927 int width, int height)
929 const uint8_t *p, *p1;
930 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
938 for(;height >= 1; height -= 2) {
943 for(w = width; w >= 2; w -= 2) {
960 p1 += src->linesize[0];
961 lum1 += dst->linesize[0];
965 for(w = width; w >= 2; w -= 2) {
974 p1 += src->linesize[0];
975 lum1 += dst->linesize[0];
977 cb1 += dst->linesize[1];
978 cr1 += dst->linesize[2];
982 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
983 int width, int height)
985 const uint8_t *p, *p1;
986 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
995 for(;height >= 1; height -= 2) {
1000 for(w = width; w >= 2; w -= 2) {
1017 p1 += src->linesize[0];
1018 lum1 += dst->linesize[0];
1022 for(w = width; w >= 2; w -= 2) {
1031 p1 += src->linesize[0];
1032 lum1 += dst->linesize[0];
1034 cb1 += dst->linesize[1];
1035 cr1 += dst->linesize[2];
1040 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1041 int width, int height)
1043 const uint8_t *p, *p1;
1044 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1048 lum1 = dst->data[0];
1051 for(;height > 0; height--) {
1056 for(w = width; w >= 2; w -= 2) {
1066 p1 += src->linesize[0];
1067 lum1 += dst->linesize[0];
1068 cb1 += dst->linesize[1];
1069 cr1 += dst->linesize[2];
1074 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1075 int width, int height)
1077 const uint8_t *p, *p1;
1078 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1082 lum1 = dst->data[0];
1085 for(;height > 0; height--) {
1090 for(w = width; w >= 2; w -= 2) {
1100 p1 += src->linesize[0];
1101 lum1 += dst->linesize[0];
1102 cb1 += dst->linesize[1];
1103 cr1 += dst->linesize[2];
1107 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1108 int width, int height)
1111 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1115 lum1 = src->data[0];
1118 for(;height > 0; height--) {
1123 for(w = width; w >= 2; w -= 2) {
1133 p1 += dst->linesize[0];
1134 lum1 += src->linesize[0];
1135 cb1 += src->linesize[1];
1136 cr1 += src->linesize[2];
1140 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1141 int width, int height)
1144 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1148 lum1 = src->data[0];
1151 for(;height > 0; height--) {
1156 for(w = width; w >= 2; w -= 2) {
1166 p1 += dst->linesize[0];
1167 lum1 += src->linesize[0];
1168 cb1 += src->linesize[1];
1169 cr1 += src->linesize[2];
1173 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1174 int width, int height)
1176 const uint8_t *p, *p1;
1177 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1181 lum1 = dst->data[0];
1184 for(;height > 0; height--) {
1189 for(w = width; w >= 4; w -= 4) {
1201 p1 += src->linesize[0];
1202 lum1 += dst->linesize[0];
1203 cb1 += dst->linesize[1];
1204 cr1 += dst->linesize[2];
1209 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1210 int width, int height)
1213 uint8_t *line1, *line2, *linesrc = dst->data[0];
1214 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1215 uint8_t *cb1, *cb2 = src->data[1];
1216 uint8_t *cr1, *cr2 = src->data[2];
1218 for(h = height / 2; h--;) {
1220 line2 = linesrc + dst->linesize[0];
1223 lum2 = lumsrc + src->linesize[0];
1228 for(w = width / 2; w--;) {
1229 *line1++ = *lum1++; *line2++ = *lum2++;
1230 *line1++ = *line2++ = *cb1++;
1231 *line1++ = *lum1++; *line2++ = *lum2++;
1232 *line1++ = *line2++ = *cr1++;
1235 linesrc += dst->linesize[0] * 2;
1236 lumsrc += src->linesize[0] * 2;
1237 cb2 += src->linesize[1];
1238 cr2 += src->linesize[2];
1242 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1243 int width, int height)
1246 uint8_t *line1, *line2, *linesrc = dst->data[0];
1247 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1248 uint8_t *cb1, *cb2 = src->data[1];
1249 uint8_t *cr1, *cr2 = src->data[2];
1251 for(h = height / 2; h--;) {
1253 line2 = linesrc + dst->linesize[0];
1256 lum2 = lumsrc + src->linesize[0];
1261 for(w = width / 2; w--;) {
1262 *line1++ = *line2++ = *cb1++;
1263 *line1++ = *lum1++; *line2++ = *lum2++;
1264 *line1++ = *line2++ = *cr1++;
1265 *line1++ = *lum1++; *line2++ = *lum2++;
1268 linesrc += dst->linesize[0] * 2;
1269 lumsrc += src->linesize[0] * 2;
1270 cb2 += src->linesize[1];
1271 cr2 += src->linesize[2];
1276 void ff_shrink22(uint8_t *dst, int dst_wrap,
1277 const uint8_t *src, int src_wrap,
1278 int width, int height)
1281 const uint8_t *s1, *s2;
1284 for(;height > 0; height--) {
1288 for(w = width;w >= 4; w-=4) {
1289 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1290 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1291 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1292 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1298 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1303 src += 2 * src_wrap;
1309 void ff_shrink44(uint8_t *dst, int dst_wrap,
1310 const uint8_t *src, int src_wrap,
1311 int width, int height)
1314 const uint8_t *s1, *s2, *s3, *s4;
1317 for(;height > 0; height--) {
1323 for(w = width;w > 0; w--) {
1324 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1325 s2[0] + s2[1] + s2[2] + s2[3] +
1326 s3[0] + s3[1] + s3[2] + s3[3] +
1327 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1334 src += 4 * src_wrap;
1340 void ff_shrink88(uint8_t *dst, int dst_wrap,
1341 const uint8_t *src, int src_wrap,
1342 int width, int height)
1346 for(;height > 0; height--) {
1347 for(w = width;w > 0; w--) {
1350 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1353 *(dst++) = (tmp + 32)>>6;
1354 src += 8 - 8*src_wrap;
1356 src += 8*src_wrap - 8*width;
1357 dst += dst_wrap - width;
1361 /* XXX: add jpeg quantize code */
1363 #define TRANSP_INDEX (6*6*6)
1365 /* this is maybe slow, but allows for extensions */
1366 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1368 return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
1371 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1374 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1377 pal = (uint32_t *)palette;
1379 for(r = 0; r < 6; r++) {
1380 for(g = 0; g < 6; g++) {
1381 for(b = 0; b < 6; b++) {
1382 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1383 (pal_value[g] << 8) | pal_value[b];
1390 pal[i++] = 0xff000000;
1393 /* copy bit n to bits 0 ... n - 1 */
1394 static inline unsigned int bitcopy_n(unsigned int a, int n)
1397 mask = (1 << n) - 1;
1398 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1401 /* rgb555 handling */
1403 #define RGB_NAME rgb555
1405 #define RGB_IN(r, g, b, s)\
1407 unsigned int v = ((const uint16_t *)(s))[0];\
1408 r = bitcopy_n(v >> (10 - 3), 3);\
1409 g = bitcopy_n(v >> (5 - 3), 3);\
1410 b = bitcopy_n(v << 3, 3);\
1414 #define RGB_OUT(d, r, g, b)\
1416 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1421 #include "imgconvert_template.c"
1423 /* rgb565 handling */
1425 #define RGB_NAME rgb565
1427 #define RGB_IN(r, g, b, s)\
1429 unsigned int v = ((const uint16_t *)(s))[0];\
1430 r = bitcopy_n(v >> (11 - 3), 3);\
1431 g = bitcopy_n(v >> (5 - 2), 2);\
1432 b = bitcopy_n(v << 3, 3);\
1435 #define RGB_OUT(d, r, g, b)\
1437 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1442 #include "imgconvert_template.c"
1444 /* bgr24 handling */
1446 #define RGB_NAME bgr24
1448 #define RGB_IN(r, g, b, s)\
1455 #define RGB_OUT(d, r, g, b)\
1464 #include "imgconvert_template.c"
1470 /* rgb24 handling */
1472 #define RGB_NAME rgb24
1475 #define RGB_IN(r, g, b, s)\
1482 #define RGB_OUT(d, r, g, b)\
1491 #include "imgconvert_template.c"
1493 /* rgb32 handling */
1495 #define RGB_NAME rgb32
1498 #define RGB_IN(r, g, b, s)\
1500 unsigned int v = ((const uint32_t *)(s))[0];\
1501 r = (v >> 16) & 0xff;\
1502 g = (v >> 8) & 0xff;\
1506 #define RGBA_IN(r, g, b, a, s)\
1508 unsigned int v = ((const uint32_t *)(s))[0];\
1509 a = (v >> 24) & 0xff;\
1510 r = (v >> 16) & 0xff;\
1511 g = (v >> 8) & 0xff;\
1515 #define RGBA_OUT(d, r, g, b, a)\
1517 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1522 #include "imgconvert_template.c"
1524 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1525 int width, int height, int xor_mask)
1527 const unsigned char *p;
1529 int v, dst_wrap, src_wrap;
1533 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1536 dst_wrap = dst->linesize[0] - width;
1537 for(y=0;y<height;y++) {
1540 v = *p++ ^ xor_mask;
1542 q[1] = -((v >> 6) & 1);
1543 q[2] = -((v >> 5) & 1);
1544 q[3] = -((v >> 4) & 1);
1545 q[4] = -((v >> 3) & 1);
1546 q[5] = -((v >> 2) & 1);
1547 q[6] = -((v >> 1) & 1);
1548 q[7] = -((v >> 0) & 1);
1553 v = *p++ ^ xor_mask;
1555 q[0] = -((v >> 7) & 1);
1565 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1566 int width, int height)
1568 mono_to_gray(dst, src, width, height, 0xff);
1571 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1572 int width, int height)
1574 mono_to_gray(dst, src, width, height, 0x00);
1577 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1578 int width, int height, int xor_mask)
1583 int j, b, v, n1, src_wrap, dst_wrap, y;
1586 src_wrap = src->linesize[0] - width;
1589 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1591 for(y=0;y<height;y++) {
1598 v = (v << 1) | (b >> 7);
1600 d[0] = v ^ xor_mask;
1610 v = (v << 1) | (b >> 7);
1613 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1621 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1622 int width, int height)
1624 gray_to_mono(dst, src, width, height, 0xff);
1627 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1628 int width, int height)
1630 gray_to_mono(dst, src, width, height, 0x00);
1633 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1634 int width, int height)
1636 int x, y, src_wrap, dst_wrap;
1639 src_wrap = src->linesize[0] - width;
1641 dst_wrap = dst->linesize[0] - width * 2;
1642 for(y=0; y<height; y++){
1643 for(x=0; x<width; x++){
1652 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1653 int width, int height)
1655 int x, y, src_wrap, dst_wrap;
1658 src_wrap = src->linesize[0] - width * 2;
1660 dst_wrap = dst->linesize[0] - width;
1661 for(y=0; y<height; y++){
1662 for(x=0; x<width; x++){
1671 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1672 int width, int height)
1674 gray16_to_gray(dst, src, width, height);
1677 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1678 int width, int height)
1680 AVPicture tmpsrc = *src;
1682 gray16_to_gray(dst, &tmpsrc, width, height);
1685 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1686 int width, int height)
1688 int x, y, src_wrap, dst_wrap;
1690 s = (uint16_t*)src->data[0];
1691 src_wrap = (src->linesize[0] - width * 2)/2;
1692 d = (uint16_t*)dst->data[0];
1693 dst_wrap = (dst->linesize[0] - width * 2)/2;
1694 for(y=0; y<height; y++){
1695 for(x=0; x<width; x++){
1696 *d++ = bswap_16(*s++);
1704 typedef struct ConvertEntry {
1705 void (*convert)(AVPicture *dst,
1706 const AVPicture *src, int width, int height);
1709 /* Add each new conversion function in this table. In order to be able
1710 to convert from any format to any format, the following constraints
1713 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1715 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1717 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1719 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1722 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1724 The other conversion functions are just optimizations for common cases.
1726 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1727 [PIX_FMT_YUV420P] = {
1728 [PIX_FMT_YUYV422] = {
1729 .convert = yuv420p_to_yuyv422,
1731 [PIX_FMT_RGB555] = {
1732 .convert = yuv420p_to_rgb555
1734 [PIX_FMT_RGB565] = {
1735 .convert = yuv420p_to_rgb565
1738 .convert = yuv420p_to_bgr24
1741 .convert = yuv420p_to_rgb24
1744 .convert = yuv420p_to_rgb32
1746 [PIX_FMT_UYVY422] = {
1747 .convert = yuv420p_to_uyvy422,
1750 [PIX_FMT_YUV422P] = {
1751 [PIX_FMT_YUYV422] = {
1752 .convert = yuv422p_to_yuyv422,
1754 [PIX_FMT_UYVY422] = {
1755 .convert = yuv422p_to_uyvy422,
1758 [PIX_FMT_YUV444P] = {
1760 .convert = yuv444p_to_rgb24
1763 [PIX_FMT_YUVJ420P] = {
1764 [PIX_FMT_RGB555] = {
1765 .convert = yuvj420p_to_rgb555
1767 [PIX_FMT_RGB565] = {
1768 .convert = yuvj420p_to_rgb565
1771 .convert = yuvj420p_to_bgr24
1774 .convert = yuvj420p_to_rgb24
1777 .convert = yuvj420p_to_rgb32
1780 [PIX_FMT_YUVJ444P] = {
1782 .convert = yuvj444p_to_rgb24
1785 [PIX_FMT_YUYV422] = {
1786 [PIX_FMT_YUV420P] = {
1787 .convert = yuyv422_to_yuv420p,
1789 [PIX_FMT_YUV422P] = {
1790 .convert = yuyv422_to_yuv422p,
1793 [PIX_FMT_UYVY422] = {
1794 [PIX_FMT_YUV420P] = {
1795 .convert = uyvy422_to_yuv420p,
1797 [PIX_FMT_YUV422P] = {
1798 .convert = uyvy422_to_yuv422p,
1802 [PIX_FMT_YUV420P] = {
1803 .convert = rgb24_to_yuv420p
1805 [PIX_FMT_RGB565] = {
1806 .convert = rgb24_to_rgb565
1808 [PIX_FMT_RGB555] = {
1809 .convert = rgb24_to_rgb555
1812 .convert = rgb24_to_rgb32
1815 .convert = rgb24_to_bgr24
1818 .convert = rgb24_to_gray
1821 .convert = rgb24_to_pal8
1823 [PIX_FMT_YUV444P] = {
1824 .convert = rgb24_to_yuv444p
1826 [PIX_FMT_YUVJ420P] = {
1827 .convert = rgb24_to_yuvj420p
1829 [PIX_FMT_YUVJ444P] = {
1830 .convert = rgb24_to_yuvj444p
1835 .convert = rgb32_to_rgb24
1838 .convert = rgb32_to_bgr24
1840 [PIX_FMT_RGB565] = {
1841 .convert = rgb32_to_rgb565
1843 [PIX_FMT_RGB555] = {
1844 .convert = rgb32_to_rgb555
1847 .convert = rgb32_to_pal8
1849 [PIX_FMT_YUV420P] = {
1850 .convert = rgb32_to_yuv420p
1853 .convert = rgb32_to_gray
1858 .convert = bgr24_to_rgb32
1861 .convert = bgr24_to_rgb24
1863 [PIX_FMT_YUV420P] = {
1864 .convert = bgr24_to_yuv420p
1867 .convert = bgr24_to_gray
1870 [PIX_FMT_RGB555] = {
1872 .convert = rgb555_to_rgb24
1875 .convert = rgb555_to_rgb32
1877 [PIX_FMT_YUV420P] = {
1878 .convert = rgb555_to_yuv420p
1881 .convert = rgb555_to_gray
1884 [PIX_FMT_RGB565] = {
1886 .convert = rgb565_to_rgb32
1889 .convert = rgb565_to_rgb24
1891 [PIX_FMT_YUV420P] = {
1892 .convert = rgb565_to_yuv420p
1895 .convert = rgb565_to_gray
1898 [PIX_FMT_GRAY16BE] = {
1900 .convert = gray16be_to_gray
1902 [PIX_FMT_GRAY16LE] = {
1903 .convert = gray16_to_gray16
1906 [PIX_FMT_GRAY16LE] = {
1908 .convert = gray16le_to_gray
1910 [PIX_FMT_GRAY16BE] = {
1911 .convert = gray16_to_gray16
1915 [PIX_FMT_RGB555] = {
1916 .convert = gray_to_rgb555
1918 [PIX_FMT_RGB565] = {
1919 .convert = gray_to_rgb565
1922 .convert = gray_to_rgb24
1925 .convert = gray_to_bgr24
1928 .convert = gray_to_rgb32
1930 [PIX_FMT_MONOWHITE] = {
1931 .convert = gray_to_monowhite
1933 [PIX_FMT_MONOBLACK] = {
1934 .convert = gray_to_monoblack
1936 [PIX_FMT_GRAY16LE] = {
1937 .convert = gray_to_gray16
1939 [PIX_FMT_GRAY16BE] = {
1940 .convert = gray_to_gray16
1943 [PIX_FMT_MONOWHITE] = {
1945 .convert = monowhite_to_gray
1948 [PIX_FMT_MONOBLACK] = {
1950 .convert = monoblack_to_gray
1954 [PIX_FMT_RGB555] = {
1955 .convert = pal8_to_rgb555
1957 [PIX_FMT_RGB565] = {
1958 .convert = pal8_to_rgb565
1961 .convert = pal8_to_bgr24
1964 .convert = pal8_to_rgb24
1967 .convert = pal8_to_rgb32
1970 [PIX_FMT_UYYVYY411] = {
1971 [PIX_FMT_YUV411P] = {
1972 .convert = uyyvyy411_to_yuv411p,
1978 int avpicture_alloc(AVPicture *picture,
1979 int pix_fmt, int width, int height)
1984 size = avpicture_get_size(pix_fmt, width, height);
1987 ptr = av_malloc(size);
1990 avpicture_fill(picture, ptr, pix_fmt, width, height);
1993 memset(picture, 0, sizeof(AVPicture));
1997 void avpicture_free(AVPicture *picture)
1999 av_free(picture->data[0]);
2002 /* return true if yuv planar */
2003 static inline int is_yuv_planar(const PixFmtInfo *ps)
2005 return (ps->color_type == FF_COLOR_YUV ||
2006 ps->color_type == FF_COLOR_YUV_JPEG) &&
2007 ps->pixel_type == FF_PIXEL_PLANAR;
2010 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2011 int pix_fmt, int top_band, int left_band)
2016 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2019 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2020 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2022 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2023 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2024 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2026 dst->linesize[0] = src->linesize[0];
2027 dst->linesize[1] = src->linesize[1];
2028 dst->linesize[2] = src->linesize[2];
2032 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2033 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2042 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2043 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2045 for (i = 0; i < 3; i++) {
2046 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2047 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2049 if (padtop || padleft) {
2050 memset(dst->data[i], color[i],
2051 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2054 if (padleft || padright) {
2055 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2056 (dst->linesize[i] - (padright >> x_shift));
2057 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2058 for (y = 0; y < yheight; y++) {
2059 memset(optr, color[i], (padleft + padright) >> x_shift);
2060 optr += dst->linesize[i];
2064 if (src) { /* first line */
2065 uint8_t *iptr = src->data[i];
2066 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2067 (padleft >> x_shift);
2068 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
2069 iptr += src->linesize[i];
2070 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2071 (dst->linesize[i] - (padright >> x_shift));
2072 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2073 for (y = 0; y < yheight; y++) {
2074 memset(optr, color[i], (padleft + padright) >> x_shift);
2075 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2076 (width - padleft - padright) >> x_shift);
2077 iptr += src->linesize[i];
2078 optr += dst->linesize[i];
2082 if (padbottom || padright) {
2083 optr = dst->data[i] + dst->linesize[i] *
2084 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2085 memset(optr, color[i],dst->linesize[i] *
2086 (padbottom >> y_shift) + (padright >> x_shift));
2092 #ifndef CONFIG_SWSCALE
2093 static uint8_t y_ccir_to_jpeg[256];
2094 static uint8_t y_jpeg_to_ccir[256];
2095 static uint8_t c_ccir_to_jpeg[256];
2096 static uint8_t c_jpeg_to_ccir[256];
2098 /* init various conversion tables */
2099 static void img_convert_init(void)
2102 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2104 for(i = 0;i < 256; i++) {
2105 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
2106 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
2107 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
2108 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
2112 /* apply to each pixel the given table */
2113 static void img_apply_table(uint8_t *dst, int dst_wrap,
2114 const uint8_t *src, int src_wrap,
2115 int width, int height, const uint8_t *table1)
2120 const uint8_t *table;
2123 for(;height > 0; height--) {
2147 /* XXX: use generic filter ? */
2148 /* XXX: in most cases, the sampling position is incorrect */
2151 static void shrink41(uint8_t *dst, int dst_wrap,
2152 const uint8_t *src, int src_wrap,
2153 int width, int height)
2159 for(;height > 0; height--) {
2162 for(w = width;w > 0; w--) {
2163 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
2173 static void shrink21(uint8_t *dst, int dst_wrap,
2174 const uint8_t *src, int src_wrap,
2175 int width, int height)
2181 for(;height > 0; height--) {
2184 for(w = width;w > 0; w--) {
2185 d[0] = (s[0] + s[1]) >> 1;
2195 static void shrink12(uint8_t *dst, int dst_wrap,
2196 const uint8_t *src, int src_wrap,
2197 int width, int height)
2201 const uint8_t *s1, *s2;
2203 for(;height > 0; height--) {
2207 for(w = width;w >= 4; w-=4) {
2208 d[0] = (s1[0] + s2[0]) >> 1;
2209 d[1] = (s1[1] + s2[1]) >> 1;
2210 d[2] = (s1[2] + s2[2]) >> 1;
2211 d[3] = (s1[3] + s2[3]) >> 1;
2217 d[0] = (s1[0] + s2[0]) >> 1;
2222 src += 2 * src_wrap;
2227 static void grow21_line(uint8_t *dst, const uint8_t *src,
2236 for(w = width;w >= 4; w-=4) {
2237 d[1] = d[0] = s1[0];
2238 d[3] = d[2] = s1[1];
2242 for(;w >= 2; w -= 2) {
2243 d[1] = d[0] = s1[0];
2247 /* only needed if width is not a multiple of two */
2248 /* XXX: veryfy that */
2254 static void grow41_line(uint8_t *dst, const uint8_t *src,
2263 for(w = width;w >= 4; w-=4) {
2275 static void grow21(uint8_t *dst, int dst_wrap,
2276 const uint8_t *src, int src_wrap,
2277 int width, int height)
2279 for(;height > 0; height--) {
2280 grow21_line(dst, src, width);
2287 static void grow12(uint8_t *dst, int dst_wrap,
2288 const uint8_t *src, int src_wrap,
2289 int width, int height)
2291 for(;height > 0; height-=2) {
2292 memcpy(dst, src, width);
2294 memcpy(dst, src, width);
2301 static void grow22(uint8_t *dst, int dst_wrap,
2302 const uint8_t *src, int src_wrap,
2303 int width, int height)
2305 for(;height > 0; height--) {
2306 grow21_line(dst, src, width);
2314 static void grow41(uint8_t *dst, int dst_wrap,
2315 const uint8_t *src, int src_wrap,
2316 int width, int height)
2318 for(;height > 0; height--) {
2319 grow41_line(dst, src, width);
2326 static void grow44(uint8_t *dst, int dst_wrap,
2327 const uint8_t *src, int src_wrap,
2328 int width, int height)
2330 for(;height > 0; height--) {
2331 grow41_line(dst, src, width);
2332 if ((height & 3) == 1)
2339 static void conv411(uint8_t *dst, int dst_wrap,
2340 const uint8_t *src, int src_wrap,
2341 int width, int height)
2344 const uint8_t *s1, *s2;
2349 for(;height > 0; height--) {
2351 s2 = src + src_wrap;
2353 for(w = width;w > 0; w--) {
2354 c = (s1[0] + s2[0]) >> 1;
2361 src += src_wrap * 2;
2366 /* XXX: always use linesize. Return -1 if not supported */
2367 int img_convert(AVPicture *dst, int dst_pix_fmt,
2368 const AVPicture *src, int src_pix_fmt,
2369 int src_width, int src_height)
2371 static int initialized;
2372 int i, ret, dst_width, dst_height, int_pix_fmt;
2373 const PixFmtInfo *src_pix, *dst_pix;
2374 const ConvertEntry *ce;
2375 AVPicture tmp1, *tmp = &tmp1;
2377 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2378 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2380 if (src_width <= 0 || src_height <= 0)
2388 dst_width = src_width;
2389 dst_height = src_height;
2391 dst_pix = &pix_fmt_info[dst_pix_fmt];
2392 src_pix = &pix_fmt_info[src_pix_fmt];
2393 if (src_pix_fmt == dst_pix_fmt) {
2394 /* no conversion needed: just copy */
2395 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2399 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2401 /* specific conversion routine */
2402 ce->convert(dst, src, dst_width, dst_height);
2407 if (is_yuv_planar(dst_pix) &&
2408 src_pix_fmt == PIX_FMT_GRAY8) {
2412 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2413 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2414 src->data[0], src->linesize[0],
2415 dst_width, dst_height);
2417 img_apply_table(dst->data[0], dst->linesize[0],
2418 src->data[0], src->linesize[0],
2419 dst_width, dst_height,
2422 /* fill U and V with 128 */
2425 w >>= dst_pix->x_chroma_shift;
2426 h >>= dst_pix->y_chroma_shift;
2427 for(i = 1; i <= 2; i++) {
2429 for(y = 0; y< h; y++) {
2431 d += dst->linesize[i];
2438 if (is_yuv_planar(src_pix) &&
2439 dst_pix_fmt == PIX_FMT_GRAY8) {
2440 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2441 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2442 src->data[0], src->linesize[0],
2443 dst_width, dst_height);
2445 img_apply_table(dst->data[0], dst->linesize[0],
2446 src->data[0], src->linesize[0],
2447 dst_width, dst_height,
2453 /* YUV to YUV planar */
2454 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2455 int x_shift, y_shift, w, h, xy_shift;
2456 void (*resize_func)(uint8_t *dst, int dst_wrap,
2457 const uint8_t *src, int src_wrap,
2458 int width, int height);
2460 /* compute chroma size of the smallest dimensions */
2463 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2464 w >>= dst_pix->x_chroma_shift;
2466 w >>= src_pix->x_chroma_shift;
2467 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2468 h >>= dst_pix->y_chroma_shift;
2470 h >>= src_pix->y_chroma_shift;
2472 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2473 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2474 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2475 /* there must be filters for conversion at least from and to
2479 resize_func = ff_img_copy_plane;
2482 resize_func = shrink21;
2485 resize_func = shrink41;
2488 resize_func = shrink12;
2491 resize_func = ff_shrink22;
2494 resize_func = ff_shrink44;
2497 resize_func = grow21;
2500 resize_func = grow12;
2503 resize_func = grow41;
2506 resize_func = grow22;
2509 resize_func = grow44;
2512 resize_func = conv411;
2515 /* currently not handled */
2516 goto no_chroma_filter;
2519 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2520 src->data[0], src->linesize[0],
2521 dst_width, dst_height);
2523 for(i = 1;i <= 2; i++)
2524 resize_func(dst->data[i], dst->linesize[i],
2525 src->data[i], src->linesize[i],
2526 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2527 /* if yuv color space conversion is needed, we do it here on
2528 the destination image */
2529 if (dst_pix->color_type != src_pix->color_type) {
2530 const uint8_t *y_table, *c_table;
2531 if (dst_pix->color_type == FF_COLOR_YUV) {
2532 y_table = y_jpeg_to_ccir;
2533 c_table = c_jpeg_to_ccir;
2535 y_table = y_ccir_to_jpeg;
2536 c_table = c_ccir_to_jpeg;
2538 img_apply_table(dst->data[0], dst->linesize[0],
2539 dst->data[0], dst->linesize[0],
2540 dst_width, dst_height,
2543 for(i = 1;i <= 2; i++)
2544 img_apply_table(dst->data[i], dst->linesize[i],
2545 dst->data[i], dst->linesize[i],
2546 dst_width>>dst_pix->x_chroma_shift,
2547 dst_height>>dst_pix->y_chroma_shift,
2554 /* try to use an intermediate format */
2555 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2556 dst_pix_fmt == PIX_FMT_YUYV422) {
2557 /* specific case: convert to YUV422P first */
2558 int_pix_fmt = PIX_FMT_YUV422P;
2559 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2560 dst_pix_fmt == PIX_FMT_UYVY422) {
2561 /* specific case: convert to YUV422P first */
2562 int_pix_fmt = PIX_FMT_YUV422P;
2563 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2564 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2565 /* specific case: convert to YUV411P first */
2566 int_pix_fmt = PIX_FMT_YUV411P;
2567 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2568 src_pix_fmt != PIX_FMT_GRAY8) ||
2569 (dst_pix->color_type == FF_COLOR_GRAY &&
2570 dst_pix_fmt != PIX_FMT_GRAY8)) {
2571 /* gray8 is the normalized format */
2572 int_pix_fmt = PIX_FMT_GRAY8;
2573 } else if ((is_yuv_planar(src_pix) &&
2574 src_pix_fmt != PIX_FMT_YUV444P &&
2575 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2576 /* yuv444 is the normalized format */
2577 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2578 int_pix_fmt = PIX_FMT_YUVJ444P;
2580 int_pix_fmt = PIX_FMT_YUV444P;
2581 } else if ((is_yuv_planar(dst_pix) &&
2582 dst_pix_fmt != PIX_FMT_YUV444P &&
2583 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2584 /* yuv444 is the normalized format */
2585 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2586 int_pix_fmt = PIX_FMT_YUVJ444P;
2588 int_pix_fmt = PIX_FMT_YUV444P;
2590 /* the two formats are rgb or gray8 or yuv[j]444p */
2591 if (src_pix->is_alpha && dst_pix->is_alpha)
2592 int_pix_fmt = PIX_FMT_RGB32;
2594 int_pix_fmt = PIX_FMT_RGB24;
2596 if (src_pix_fmt == int_pix_fmt)
2598 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2601 if (img_convert(tmp, int_pix_fmt,
2602 src, src_pix_fmt, src_width, src_height) < 0)
2604 if (img_convert(dst, dst_pix_fmt,
2605 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2609 avpicture_free(tmp);
2614 /* NOTE: we scan all the pixels to have an exact information */
2615 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2617 const unsigned char *p;
2618 int src_wrap, ret, x, y;
2620 uint32_t *palette = (uint32_t *)src->data[1];
2623 src_wrap = src->linesize[0] - width;
2625 for(y=0;y<height;y++) {
2626 for(x=0;x<width;x++) {
2627 a = palette[p[0]] >> 24;
2629 ret |= FF_ALPHA_TRANSP;
2630 } else if (a != 0xff) {
2631 ret |= FF_ALPHA_SEMI_TRANSP;
2640 int img_get_alpha_info(const AVPicture *src,
2641 int pix_fmt, int width, int height)
2643 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2646 pf = &pix_fmt_info[pix_fmt];
2647 /* no alpha can be represented in format */
2652 ret = get_alpha_info_rgb32(src, width, height);
2655 ret = get_alpha_info_pal8(src, width, height);
2658 /* we do not know, so everything is indicated */
2659 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2666 #define DEINT_INPLACE_LINE_LUM \
2667 movd_m2r(lum_m4[0],mm0);\
2668 movd_m2r(lum_m3[0],mm1);\
2669 movd_m2r(lum_m2[0],mm2);\
2670 movd_m2r(lum_m1[0],mm3);\
2671 movd_m2r(lum[0],mm4);\
2672 punpcklbw_r2r(mm7,mm0);\
2673 movd_r2m(mm2,lum_m4[0]);\
2674 punpcklbw_r2r(mm7,mm1);\
2675 punpcklbw_r2r(mm7,mm2);\
2676 punpcklbw_r2r(mm7,mm3);\
2677 punpcklbw_r2r(mm7,mm4);\
2678 paddw_r2r(mm3,mm1);\
2680 paddw_r2r(mm4,mm0);\
2682 paddw_r2r(mm6,mm2);\
2683 paddw_r2r(mm2,mm1);\
2684 psubusw_r2r(mm0,mm1);\
2686 packuswb_r2r(mm7,mm1);\
2687 movd_r2m(mm1,lum_m2[0]);
2689 #define DEINT_LINE_LUM \
2690 movd_m2r(lum_m4[0],mm0);\
2691 movd_m2r(lum_m3[0],mm1);\
2692 movd_m2r(lum_m2[0],mm2);\
2693 movd_m2r(lum_m1[0],mm3);\
2694 movd_m2r(lum[0],mm4);\
2695 punpcklbw_r2r(mm7,mm0);\
2696 punpcklbw_r2r(mm7,mm1);\
2697 punpcklbw_r2r(mm7,mm2);\
2698 punpcklbw_r2r(mm7,mm3);\
2699 punpcklbw_r2r(mm7,mm4);\
2700 paddw_r2r(mm3,mm1);\
2702 paddw_r2r(mm4,mm0);\
2704 paddw_r2r(mm6,mm2);\
2705 paddw_r2r(mm2,mm1);\
2706 psubusw_r2r(mm0,mm1);\
2708 packuswb_r2r(mm7,mm1);\
2709 movd_r2m(mm1,dst[0]);
2712 /* filter parameters: [-1 4 2 4 -1] // 8 */
2713 static void deinterlace_line(uint8_t *dst,
2714 const uint8_t *lum_m4, const uint8_t *lum_m3,
2715 const uint8_t *lum_m2, const uint8_t *lum_m1,
2720 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2723 for(;size > 0;size--) {
2725 sum += lum_m3[0] << 2;
2726 sum += lum_m2[0] << 1;
2727 sum += lum_m1[0] << 2;
2729 dst[0] = cm[(sum + 4) >> 3];
2741 movq_m2r(ff_pw_4,mm6);
2743 for (;size > 3; size-=4) {
2754 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2758 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2761 for(;size > 0;size--) {
2763 sum += lum_m3[0] << 2;
2764 sum += lum_m2[0] << 1;
2765 lum_m4[0]=lum_m2[0];
2766 sum += lum_m1[0] << 2;
2768 lum_m2[0] = cm[(sum + 4) >> 3];
2779 movq_m2r(ff_pw_4,mm6);
2781 for (;size > 3; size-=4) {
2782 DEINT_INPLACE_LINE_LUM
2792 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2793 top field is copied as is, but the bottom field is deinterlaced
2794 against the top field. */
2795 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2796 const uint8_t *src1, int src_wrap,
2797 int width, int height)
2799 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2804 src_0=&src_m1[src_wrap];
2805 src_p1=&src_0[src_wrap];
2806 src_p2=&src_p1[src_wrap];
2807 for(y=0;y<(height-2);y+=2) {
2808 memcpy(dst,src_m1,width);
2810 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2814 src_p1 += 2*src_wrap;
2815 src_p2 += 2*src_wrap;
2818 memcpy(dst,src_m1,width);
2821 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2824 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2825 int width, int height)
2827 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2830 buf = (uint8_t*)av_malloc(width);
2833 memcpy(buf,src_m1,width);
2834 src_0=&src_m1[src_wrap];
2835 src_p1=&src_0[src_wrap];
2836 src_p2=&src_p1[src_wrap];
2837 for(y=0;y<(height-2);y+=2) {
2838 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2841 src_p1 += 2*src_wrap;
2842 src_p2 += 2*src_wrap;
2845 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2849 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2850 int pix_fmt, int width, int height)
2854 if (pix_fmt != PIX_FMT_YUV420P &&
2855 pix_fmt != PIX_FMT_YUV422P &&
2856 pix_fmt != PIX_FMT_YUV444P &&
2857 pix_fmt != PIX_FMT_YUV411P &&
2858 pix_fmt != PIX_FMT_GRAY8)
2860 if ((width & 3) != 0 || (height & 3) != 0)
2866 case PIX_FMT_YUV420P:
2870 case PIX_FMT_YUV422P:
2873 case PIX_FMT_YUV411P:
2879 if (pix_fmt == PIX_FMT_GRAY8) {
2884 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2887 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2888 src->data[i], src->linesize[i],