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 "i386/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_UYYVYY411] = {
273 .color_type = FF_COLOR_YUV,
274 .pixel_type = FF_PIXEL_PACKED,
276 .x_chroma_shift = 2, .y_chroma_shift = 0,
280 .nb_channels = 4, .is_alpha = 1,
281 .color_type = FF_COLOR_RGB,
282 .pixel_type = FF_PIXEL_PACKED,
284 .x_chroma_shift = 0, .y_chroma_shift = 0,
289 .color_type = FF_COLOR_RGB,
290 .pixel_type = FF_PIXEL_PACKED,
292 .x_chroma_shift = 0, .y_chroma_shift = 0,
297 .color_type = FF_COLOR_RGB,
298 .pixel_type = FF_PIXEL_PACKED,
300 .x_chroma_shift = 0, .y_chroma_shift = 0,
305 .color_type = FF_COLOR_RGB,
306 .pixel_type = FF_PIXEL_PACKED,
308 .x_chroma_shift = 0, .y_chroma_shift = 0,
313 .color_type = FF_COLOR_RGB,
314 .pixel_type = FF_PIXEL_PACKED,
316 .x_chroma_shift = 0, .y_chroma_shift = 0,
318 [PIX_FMT_RGB4_BYTE] = {
321 .color_type = FF_COLOR_RGB,
322 .pixel_type = FF_PIXEL_PACKED,
324 .x_chroma_shift = 0, .y_chroma_shift = 0,
329 .color_type = FF_COLOR_RGB,
330 .pixel_type = FF_PIXEL_PACKED,
332 .x_chroma_shift = 0, .y_chroma_shift = 0,
337 .color_type = FF_COLOR_RGB,
338 .pixel_type = FF_PIXEL_PACKED,
340 .x_chroma_shift = 0, .y_chroma_shift = 0,
342 [PIX_FMT_BGR4_BYTE] = {
345 .color_type = FF_COLOR_RGB,
346 .pixel_type = FF_PIXEL_PACKED,
348 .x_chroma_shift = 0, .y_chroma_shift = 0,
353 .color_type = FF_COLOR_YUV,
354 .pixel_type = FF_PIXEL_PLANAR,
356 .x_chroma_shift = 1, .y_chroma_shift = 1,
361 .color_type = FF_COLOR_YUV,
362 .pixel_type = FF_PIXEL_PLANAR,
364 .x_chroma_shift = 1, .y_chroma_shift = 1,
367 [PIX_FMT_BGR32_1] = {
369 .nb_channels = 4, .is_alpha = 1,
370 .color_type = FF_COLOR_RGB,
371 .pixel_type = FF_PIXEL_PACKED,
373 .x_chroma_shift = 0, .y_chroma_shift = 0,
375 [PIX_FMT_RGB32_1] = {
377 .nb_channels = 4, .is_alpha = 1,
378 .color_type = FF_COLOR_RGB,
379 .pixel_type = FF_PIXEL_PACKED,
381 .x_chroma_shift = 0, .y_chroma_shift = 0,
385 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
387 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
388 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
391 const char *avcodec_get_pix_fmt_name(int pix_fmt)
393 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
396 return pix_fmt_info[pix_fmt].name;
399 enum PixelFormat avcodec_get_pix_fmt(const char* name)
403 for (i=0; i < PIX_FMT_NB; i++)
404 if (!strcmp(pix_fmt_info[i].name, name))
409 void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
413 snprintf (buf, buf_size,
414 "name " " nb_channels" " depth" " is_alpha"
417 PixFmtInfo info= pix_fmt_info[pix_fmt];
419 char is_alpha_char= info.is_alpha ? 'y' : 'n';
421 snprintf (buf, buf_size,
422 "%-10s" " %1d " " %2d " " %c ",
431 int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
434 const PixFmtInfo *pinfo;
436 memset(picture->linesize, 0, sizeof(picture->linesize));
438 pinfo = &pix_fmt_info[pix_fmt];
440 case PIX_FMT_YUV420P:
441 case PIX_FMT_YUV422P:
442 case PIX_FMT_YUV444P:
443 case PIX_FMT_YUV410P:
444 case PIX_FMT_YUV411P:
445 case PIX_FMT_YUV440P:
446 case PIX_FMT_YUVJ420P:
447 case PIX_FMT_YUVJ422P:
448 case PIX_FMT_YUVJ444P:
449 case PIX_FMT_YUVJ440P:
450 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
451 picture->linesize[0] = width;
452 picture->linesize[1] = w2;
453 picture->linesize[2] = w2;
455 case PIX_FMT_YUVA420P:
456 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
457 picture->linesize[0] = width;
458 picture->linesize[1] = w2;
459 picture->linesize[2] = w2;
460 picture->linesize[3] = width;
464 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
465 picture->linesize[0] = width;
466 picture->linesize[1] = w2;
470 picture->linesize[0] = width * 3;
474 case PIX_FMT_RGB32_1:
475 case PIX_FMT_BGR32_1:
476 picture->linesize[0] = width * 4;
478 case PIX_FMT_GRAY16BE:
479 case PIX_FMT_GRAY16LE:
484 case PIX_FMT_YUYV422:
485 picture->linesize[0] = width * 2;
487 case PIX_FMT_UYVY422:
488 picture->linesize[0] = width * 2;
490 case PIX_FMT_UYYVYY411:
491 picture->linesize[0] = width + width/2;
495 case PIX_FMT_RGB4_BYTE:
496 case PIX_FMT_BGR4_BYTE:
498 picture->linesize[0] = width;
502 picture->linesize[0] = width / 2;
504 case PIX_FMT_MONOWHITE:
505 case PIX_FMT_MONOBLACK:
506 picture->linesize[0] = (width + 7) >> 3;
509 picture->linesize[0] = width;
510 picture->linesize[1] = 4;
518 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
522 const PixFmtInfo *pinfo;
524 pinfo = &pix_fmt_info[pix_fmt];
525 size = picture->linesize[0] * height;
527 case PIX_FMT_YUV420P:
528 case PIX_FMT_YUV422P:
529 case PIX_FMT_YUV444P:
530 case PIX_FMT_YUV410P:
531 case PIX_FMT_YUV411P:
532 case PIX_FMT_YUV440P:
533 case PIX_FMT_YUVJ420P:
534 case PIX_FMT_YUVJ422P:
535 case PIX_FMT_YUVJ444P:
536 case PIX_FMT_YUVJ440P:
537 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
538 size2 = picture->linesize[1] * h2;
539 picture->data[0] = ptr;
540 picture->data[1] = picture->data[0] + size;
541 picture->data[2] = picture->data[1] + size2;
542 picture->data[3] = NULL;
543 return size + 2 * size2;
544 case PIX_FMT_YUVA420P:
545 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
546 size2 = picture->linesize[1] * h2;
547 picture->data[0] = ptr;
548 picture->data[1] = picture->data[0] + size;
549 picture->data[2] = picture->data[1] + size2;
550 picture->data[3] = picture->data[1] + size2 + size2;
551 return 2 * size + 2 * size2;
554 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
555 size2 = picture->linesize[1] * h2 * 2;
556 picture->data[0] = ptr;
557 picture->data[1] = picture->data[0] + size;
558 picture->data[2] = NULL;
559 picture->data[3] = NULL;
560 return size + 2 * size2;
565 case PIX_FMT_RGB32_1:
566 case PIX_FMT_BGR32_1:
567 case PIX_FMT_GRAY16BE:
568 case PIX_FMT_GRAY16LE:
573 case PIX_FMT_YUYV422:
574 case PIX_FMT_UYVY422:
575 case PIX_FMT_UYYVYY411:
578 case PIX_FMT_RGB4_BYTE:
579 case PIX_FMT_BGR4_BYTE:
583 case PIX_FMT_MONOWHITE:
584 case PIX_FMT_MONOBLACK:
585 picture->data[0] = ptr;
586 picture->data[1] = NULL;
587 picture->data[2] = NULL;
588 picture->data[3] = NULL;
591 size2 = (size + 3) & ~3;
592 picture->data[0] = ptr;
593 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
594 picture->data[2] = NULL;
595 picture->data[3] = NULL;
596 return size2 + 256 * 4;
598 picture->data[0] = NULL;
599 picture->data[1] = NULL;
600 picture->data[2] = NULL;
601 picture->data[3] = NULL;
606 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
607 int pix_fmt, int width, int height)
610 if(avcodec_check_dimensions(NULL, width, height))
613 if (ff_fill_linesize(picture, pix_fmt, width))
616 return ff_fill_pointer(picture, ptr, pix_fmt, height);
619 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
620 unsigned char *dest, int dest_size)
622 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
623 int i, j, w, h, data_planes;
624 const unsigned char* s;
625 int size = avpicture_get_size(pix_fmt, width, height);
627 if (size > dest_size || size < 0)
630 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
631 if (pix_fmt == PIX_FMT_YUYV422 ||
632 pix_fmt == PIX_FMT_UYVY422 ||
633 pix_fmt == PIX_FMT_BGR565 ||
634 pix_fmt == PIX_FMT_BGR555 ||
635 pix_fmt == PIX_FMT_RGB565 ||
636 pix_fmt == PIX_FMT_RGB555)
638 else if (pix_fmt == PIX_FMT_UYYVYY411)
640 else if (pix_fmt == PIX_FMT_PAL8)
643 w = width * (pf->depth * pf->nb_channels / 8);
648 data_planes = pf->nb_channels;
649 w = (width*pf->depth + 7)/8;
653 for (i=0; i<data_planes; i++) {
655 w = width >> pf->x_chroma_shift;
656 h = height >> pf->y_chroma_shift;
662 s += src->linesize[i];
666 if (pf->pixel_type == FF_PIXEL_PALETTE)
667 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
672 int avpicture_get_size(int pix_fmt, int width, int height)
674 AVPicture dummy_pict;
675 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
678 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
681 const PixFmtInfo *pf, *ps;
684 ps = &pix_fmt_info[src_pix_fmt];
685 pf = &pix_fmt_info[dst_pix_fmt];
689 pf = &pix_fmt_info[dst_pix_fmt];
690 if (pf->depth < ps->depth ||
691 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
692 loss |= FF_LOSS_DEPTH;
693 if (pf->x_chroma_shift > ps->x_chroma_shift ||
694 pf->y_chroma_shift > ps->y_chroma_shift)
695 loss |= FF_LOSS_RESOLUTION;
696 switch(pf->color_type) {
698 if (ps->color_type != FF_COLOR_RGB &&
699 ps->color_type != FF_COLOR_GRAY)
700 loss |= FF_LOSS_COLORSPACE;
703 if (ps->color_type != FF_COLOR_GRAY)
704 loss |= FF_LOSS_COLORSPACE;
707 if (ps->color_type != FF_COLOR_YUV)
708 loss |= FF_LOSS_COLORSPACE;
710 case FF_COLOR_YUV_JPEG:
711 if (ps->color_type != FF_COLOR_YUV_JPEG &&
712 ps->color_type != FF_COLOR_YUV &&
713 ps->color_type != FF_COLOR_GRAY)
714 loss |= FF_LOSS_COLORSPACE;
718 if (ps->color_type != pf->color_type)
719 loss |= FF_LOSS_COLORSPACE;
722 if (pf->color_type == FF_COLOR_GRAY &&
723 ps->color_type != FF_COLOR_GRAY)
724 loss |= FF_LOSS_CHROMA;
725 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
726 loss |= FF_LOSS_ALPHA;
727 if (pf->pixel_type == FF_PIXEL_PALETTE &&
728 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
729 loss |= FF_LOSS_COLORQUANT;
733 static int avg_bits_per_pixel(int pix_fmt)
736 const PixFmtInfo *pf;
738 pf = &pix_fmt_info[pix_fmt];
739 switch(pf->pixel_type) {
740 case FF_PIXEL_PACKED:
742 case PIX_FMT_YUYV422:
743 case PIX_FMT_UYVY422:
750 case PIX_FMT_UYYVYY411:
754 bits = pf->depth * pf->nb_channels;
758 case FF_PIXEL_PLANAR:
759 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
760 bits = pf->depth * pf->nb_channels;
762 bits = pf->depth + ((2 * pf->depth) >>
763 (pf->x_chroma_shift + pf->y_chroma_shift));
766 case FF_PIXEL_PALETTE:
776 static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
781 int dist, i, loss, min_dist, dst_pix_fmt;
783 /* find exact color match with smallest size */
785 min_dist = 0x7fffffff;
786 for(i = 0;i < PIX_FMT_NB; i++) {
787 if (pix_fmt_mask & (1ULL << i)) {
788 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
790 dist = avg_bits_per_pixel(i);
791 if (dist < min_dist) {
801 int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
802 int has_alpha, int *loss_ptr)
804 int dst_pix_fmt, loss_mask, i;
805 static const int loss_mask_order[] = {
806 ~0, /* no loss first */
809 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
815 /* try with successive loss */
818 loss_mask = loss_mask_order[i++];
819 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
820 has_alpha, loss_mask);
821 if (dst_pix_fmt >= 0)
829 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
833 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
834 const uint8_t *src, int src_wrap,
835 int width, int height)
839 for(;height > 0; height--) {
840 memcpy(dst, src, width);
846 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
849 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
851 pf = &pix_fmt_info[pix_fmt];
852 switch(pf->pixel_type) {
853 case FF_PIXEL_PACKED:
855 case PIX_FMT_YUYV422:
856 case PIX_FMT_UYVY422:
863 case PIX_FMT_UYYVYY411:
867 bits = pf->depth * pf->nb_channels;
870 return (width * bits + 7) >> 3;
872 case FF_PIXEL_PLANAR:
873 if (plane == 1 || plane == 2)
874 width= -((-width)>>pf->x_chroma_shift);
876 return (width * pf->depth + 7) >> 3;
878 case FF_PIXEL_PALETTE:
887 void av_picture_copy(AVPicture *dst, const AVPicture *src,
888 int pix_fmt, int width, int height)
891 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
893 pf = &pix_fmt_info[pix_fmt];
894 switch(pf->pixel_type) {
895 case FF_PIXEL_PACKED:
896 case FF_PIXEL_PLANAR:
897 for(i = 0; i < pf->nb_channels; i++) {
899 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
901 if (i == 1 || i == 2) {
902 h= -((-height)>>pf->y_chroma_shift);
904 ff_img_copy_plane(dst->data[i], dst->linesize[i],
905 src->data[i], src->linesize[i],
909 case FF_PIXEL_PALETTE:
910 ff_img_copy_plane(dst->data[0], dst->linesize[0],
911 src->data[0], src->linesize[0],
913 /* copy the palette */
914 ff_img_copy_plane(dst->data[1], dst->linesize[1],
915 src->data[1], src->linesize[1],
921 /* XXX: totally non optimized */
923 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
924 int width, int height)
926 const uint8_t *p, *p1;
927 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
935 for(;height >= 1; height -= 2) {
940 for(w = width; w >= 2; w -= 2) {
957 p1 += src->linesize[0];
958 lum1 += dst->linesize[0];
962 for(w = width; w >= 2; w -= 2) {
971 p1 += src->linesize[0];
972 lum1 += dst->linesize[0];
974 cb1 += dst->linesize[1];
975 cr1 += dst->linesize[2];
979 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
980 int width, int height)
982 const uint8_t *p, *p1;
983 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
992 for(;height >= 1; height -= 2) {
997 for(w = width; w >= 2; w -= 2) {
1014 p1 += src->linesize[0];
1015 lum1 += dst->linesize[0];
1019 for(w = width; w >= 2; w -= 2) {
1028 p1 += src->linesize[0];
1029 lum1 += dst->linesize[0];
1031 cb1 += dst->linesize[1];
1032 cr1 += dst->linesize[2];
1037 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1038 int width, int height)
1040 const uint8_t *p, *p1;
1041 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1045 lum1 = dst->data[0];
1048 for(;height > 0; height--) {
1053 for(w = width; w >= 2; w -= 2) {
1063 p1 += src->linesize[0];
1064 lum1 += dst->linesize[0];
1065 cb1 += dst->linesize[1];
1066 cr1 += dst->linesize[2];
1071 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1072 int width, int height)
1074 const uint8_t *p, *p1;
1075 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1079 lum1 = dst->data[0];
1082 for(;height > 0; height--) {
1087 for(w = width; w >= 2; w -= 2) {
1097 p1 += src->linesize[0];
1098 lum1 += dst->linesize[0];
1099 cb1 += dst->linesize[1];
1100 cr1 += dst->linesize[2];
1104 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1105 int width, int height)
1108 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1112 lum1 = src->data[0];
1115 for(;height > 0; height--) {
1120 for(w = width; w >= 2; w -= 2) {
1130 p1 += dst->linesize[0];
1131 lum1 += src->linesize[0];
1132 cb1 += src->linesize[1];
1133 cr1 += src->linesize[2];
1137 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1138 int width, int height)
1141 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1145 lum1 = src->data[0];
1148 for(;height > 0; height--) {
1153 for(w = width; w >= 2; w -= 2) {
1163 p1 += dst->linesize[0];
1164 lum1 += src->linesize[0];
1165 cb1 += src->linesize[1];
1166 cr1 += src->linesize[2];
1170 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1171 int width, int height)
1173 const uint8_t *p, *p1;
1174 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1178 lum1 = dst->data[0];
1181 for(;height > 0; height--) {
1186 for(w = width; w >= 4; w -= 4) {
1198 p1 += src->linesize[0];
1199 lum1 += dst->linesize[0];
1200 cb1 += dst->linesize[1];
1201 cr1 += dst->linesize[2];
1206 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1207 int width, int height)
1210 uint8_t *line1, *line2, *linesrc = dst->data[0];
1211 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1212 uint8_t *cb1, *cb2 = src->data[1];
1213 uint8_t *cr1, *cr2 = src->data[2];
1215 for(h = height / 2; h--;) {
1217 line2 = linesrc + dst->linesize[0];
1220 lum2 = lumsrc + src->linesize[0];
1225 for(w = width / 2; w--;) {
1226 *line1++ = *lum1++; *line2++ = *lum2++;
1227 *line1++ = *line2++ = *cb1++;
1228 *line1++ = *lum1++; *line2++ = *lum2++;
1229 *line1++ = *line2++ = *cr1++;
1232 linesrc += dst->linesize[0] * 2;
1233 lumsrc += src->linesize[0] * 2;
1234 cb2 += src->linesize[1];
1235 cr2 += src->linesize[2];
1239 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1240 int width, int height)
1243 uint8_t *line1, *line2, *linesrc = dst->data[0];
1244 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1245 uint8_t *cb1, *cb2 = src->data[1];
1246 uint8_t *cr1, *cr2 = src->data[2];
1248 for(h = height / 2; h--;) {
1250 line2 = linesrc + dst->linesize[0];
1253 lum2 = lumsrc + src->linesize[0];
1258 for(w = width / 2; w--;) {
1259 *line1++ = *line2++ = *cb1++;
1260 *line1++ = *lum1++; *line2++ = *lum2++;
1261 *line1++ = *line2++ = *cr1++;
1262 *line1++ = *lum1++; *line2++ = *lum2++;
1265 linesrc += dst->linesize[0] * 2;
1266 lumsrc += src->linesize[0] * 2;
1267 cb2 += src->linesize[1];
1268 cr2 += src->linesize[2];
1273 void ff_shrink22(uint8_t *dst, int dst_wrap,
1274 const uint8_t *src, int src_wrap,
1275 int width, int height)
1278 const uint8_t *s1, *s2;
1281 for(;height > 0; height--) {
1285 for(w = width;w >= 4; w-=4) {
1286 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1287 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1288 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1289 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1295 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1300 src += 2 * src_wrap;
1306 void ff_shrink44(uint8_t *dst, int dst_wrap,
1307 const uint8_t *src, int src_wrap,
1308 int width, int height)
1311 const uint8_t *s1, *s2, *s3, *s4;
1314 for(;height > 0; height--) {
1320 for(w = width;w > 0; w--) {
1321 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1322 s2[0] + s2[1] + s2[2] + s2[3] +
1323 s3[0] + s3[1] + s3[2] + s3[3] +
1324 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1331 src += 4 * src_wrap;
1337 void ff_shrink88(uint8_t *dst, int dst_wrap,
1338 const uint8_t *src, int src_wrap,
1339 int width, int height)
1343 for(;height > 0; height--) {
1344 for(w = width;w > 0; w--) {
1347 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1350 *(dst++) = (tmp + 32)>>6;
1351 src += 8 - 8*src_wrap;
1353 src += 8*src_wrap - 8*width;
1354 dst += dst_wrap - width;
1358 /* XXX: add jpeg quantize code */
1360 #define TRANSP_INDEX (6*6*6)
1362 /* this is maybe slow, but allows for extensions */
1363 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1365 return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
1368 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1371 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1374 pal = (uint32_t *)palette;
1376 for(r = 0; r < 6; r++) {
1377 for(g = 0; g < 6; g++) {
1378 for(b = 0; b < 6; b++) {
1379 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1380 (pal_value[g] << 8) | pal_value[b];
1387 pal[i++] = 0xff000000;
1390 /* copy bit n to bits 0 ... n - 1 */
1391 static inline unsigned int bitcopy_n(unsigned int a, int n)
1394 mask = (1 << n) - 1;
1395 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1398 /* rgb555 handling */
1400 #define RGB_NAME rgb555
1402 #define RGB_IN(r, g, b, s)\
1404 unsigned int v = ((const uint16_t *)(s))[0];\
1405 r = bitcopy_n(v >> (10 - 3), 3);\
1406 g = bitcopy_n(v >> (5 - 3), 3);\
1407 b = bitcopy_n(v << 3, 3);\
1411 #define RGB_OUT(d, r, g, b)\
1413 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1418 #include "imgconvert_template.c"
1420 /* rgb565 handling */
1422 #define RGB_NAME rgb565
1424 #define RGB_IN(r, g, b, s)\
1426 unsigned int v = ((const uint16_t *)(s))[0];\
1427 r = bitcopy_n(v >> (11 - 3), 3);\
1428 g = bitcopy_n(v >> (5 - 2), 2);\
1429 b = bitcopy_n(v << 3, 3);\
1432 #define RGB_OUT(d, r, g, b)\
1434 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1439 #include "imgconvert_template.c"
1441 /* bgr24 handling */
1443 #define RGB_NAME bgr24
1445 #define RGB_IN(r, g, b, s)\
1452 #define RGB_OUT(d, r, g, b)\
1461 #include "imgconvert_template.c"
1467 /* rgb24 handling */
1469 #define RGB_NAME rgb24
1472 #define RGB_IN(r, g, b, s)\
1479 #define RGB_OUT(d, r, g, b)\
1488 #include "imgconvert_template.c"
1490 /* rgb32 handling */
1492 #define RGB_NAME rgb32
1495 #define RGB_IN(r, g, b, s)\
1497 unsigned int v = ((const uint32_t *)(s))[0];\
1498 r = (v >> 16) & 0xff;\
1499 g = (v >> 8) & 0xff;\
1503 #define RGBA_IN(r, g, b, a, s)\
1505 unsigned int v = ((const uint32_t *)(s))[0];\
1506 a = (v >> 24) & 0xff;\
1507 r = (v >> 16) & 0xff;\
1508 g = (v >> 8) & 0xff;\
1512 #define RGBA_OUT(d, r, g, b, a)\
1514 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1519 #include "imgconvert_template.c"
1521 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1522 int width, int height, int xor_mask)
1524 const unsigned char *p;
1526 int v, dst_wrap, src_wrap;
1530 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1533 dst_wrap = dst->linesize[0] - width;
1534 for(y=0;y<height;y++) {
1537 v = *p++ ^ xor_mask;
1539 q[1] = -((v >> 6) & 1);
1540 q[2] = -((v >> 5) & 1);
1541 q[3] = -((v >> 4) & 1);
1542 q[4] = -((v >> 3) & 1);
1543 q[5] = -((v >> 2) & 1);
1544 q[6] = -((v >> 1) & 1);
1545 q[7] = -((v >> 0) & 1);
1550 v = *p++ ^ xor_mask;
1552 q[0] = -((v >> 7) & 1);
1562 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1563 int width, int height)
1565 mono_to_gray(dst, src, width, height, 0xff);
1568 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1569 int width, int height)
1571 mono_to_gray(dst, src, width, height, 0x00);
1574 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1575 int width, int height, int xor_mask)
1580 int j, b, v, n1, src_wrap, dst_wrap, y;
1583 src_wrap = src->linesize[0] - width;
1586 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1588 for(y=0;y<height;y++) {
1595 v = (v << 1) | (b >> 7);
1597 d[0] = v ^ xor_mask;
1607 v = (v << 1) | (b >> 7);
1610 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1618 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1619 int width, int height)
1621 gray_to_mono(dst, src, width, height, 0xff);
1624 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1625 int width, int height)
1627 gray_to_mono(dst, src, width, height, 0x00);
1630 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1631 int width, int height)
1633 int x, y, src_wrap, dst_wrap;
1636 src_wrap = src->linesize[0] - width;
1638 dst_wrap = dst->linesize[0] - width * 2;
1639 for(y=0; y<height; y++){
1640 for(x=0; x<width; x++){
1649 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1650 int width, int height)
1652 int x, y, src_wrap, dst_wrap;
1655 src_wrap = src->linesize[0] - width * 2;
1657 dst_wrap = dst->linesize[0] - width;
1658 for(y=0; y<height; y++){
1659 for(x=0; x<width; x++){
1668 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1669 int width, int height)
1671 gray16_to_gray(dst, src, width, height);
1674 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1675 int width, int height)
1677 AVPicture tmpsrc = *src;
1679 gray16_to_gray(dst, &tmpsrc, width, height);
1682 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1683 int width, int height)
1685 int x, y, src_wrap, dst_wrap;
1687 s = (uint16_t*)src->data[0];
1688 src_wrap = (src->linesize[0] - width * 2)/2;
1689 d = (uint16_t*)dst->data[0];
1690 dst_wrap = (dst->linesize[0] - width * 2)/2;
1691 for(y=0; y<height; y++){
1692 for(x=0; x<width; x++){
1693 *d++ = bswap_16(*s++);
1701 typedef struct ConvertEntry {
1702 void (*convert)(AVPicture *dst,
1703 const AVPicture *src, int width, int height);
1706 /* Add each new conversion function in this table. In order to be able
1707 to convert from any format to any format, the following constraints
1710 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1712 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1714 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1716 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1719 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1721 The other conversion functions are just optimizations for common cases.
1723 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1724 [PIX_FMT_YUV420P] = {
1725 [PIX_FMT_YUYV422] = {
1726 .convert = yuv420p_to_yuyv422,
1728 [PIX_FMT_RGB555] = {
1729 .convert = yuv420p_to_rgb555
1731 [PIX_FMT_RGB565] = {
1732 .convert = yuv420p_to_rgb565
1735 .convert = yuv420p_to_bgr24
1738 .convert = yuv420p_to_rgb24
1741 .convert = yuv420p_to_rgb32
1743 [PIX_FMT_UYVY422] = {
1744 .convert = yuv420p_to_uyvy422,
1747 [PIX_FMT_YUV422P] = {
1748 [PIX_FMT_YUYV422] = {
1749 .convert = yuv422p_to_yuyv422,
1751 [PIX_FMT_UYVY422] = {
1752 .convert = yuv422p_to_uyvy422,
1755 [PIX_FMT_YUV444P] = {
1757 .convert = yuv444p_to_rgb24
1760 [PIX_FMT_YUVJ420P] = {
1761 [PIX_FMT_RGB555] = {
1762 .convert = yuvj420p_to_rgb555
1764 [PIX_FMT_RGB565] = {
1765 .convert = yuvj420p_to_rgb565
1768 .convert = yuvj420p_to_bgr24
1771 .convert = yuvj420p_to_rgb24
1774 .convert = yuvj420p_to_rgb32
1777 [PIX_FMT_YUVJ444P] = {
1779 .convert = yuvj444p_to_rgb24
1782 [PIX_FMT_YUYV422] = {
1783 [PIX_FMT_YUV420P] = {
1784 .convert = yuyv422_to_yuv420p,
1786 [PIX_FMT_YUV422P] = {
1787 .convert = yuyv422_to_yuv422p,
1790 [PIX_FMT_UYVY422] = {
1791 [PIX_FMT_YUV420P] = {
1792 .convert = uyvy422_to_yuv420p,
1794 [PIX_FMT_YUV422P] = {
1795 .convert = uyvy422_to_yuv422p,
1799 [PIX_FMT_YUV420P] = {
1800 .convert = rgb24_to_yuv420p
1802 [PIX_FMT_RGB565] = {
1803 .convert = rgb24_to_rgb565
1805 [PIX_FMT_RGB555] = {
1806 .convert = rgb24_to_rgb555
1809 .convert = rgb24_to_rgb32
1812 .convert = rgb24_to_bgr24
1815 .convert = rgb24_to_gray
1818 .convert = rgb24_to_pal8
1820 [PIX_FMT_YUV444P] = {
1821 .convert = rgb24_to_yuv444p
1823 [PIX_FMT_YUVJ420P] = {
1824 .convert = rgb24_to_yuvj420p
1826 [PIX_FMT_YUVJ444P] = {
1827 .convert = rgb24_to_yuvj444p
1832 .convert = rgb32_to_rgb24
1835 .convert = rgb32_to_bgr24
1837 [PIX_FMT_RGB565] = {
1838 .convert = rgb32_to_rgb565
1840 [PIX_FMT_RGB555] = {
1841 .convert = rgb32_to_rgb555
1844 .convert = rgb32_to_pal8
1846 [PIX_FMT_YUV420P] = {
1847 .convert = rgb32_to_yuv420p
1850 .convert = rgb32_to_gray
1855 .convert = bgr24_to_rgb32
1858 .convert = bgr24_to_rgb24
1860 [PIX_FMT_YUV420P] = {
1861 .convert = bgr24_to_yuv420p
1864 .convert = bgr24_to_gray
1867 [PIX_FMT_RGB555] = {
1869 .convert = rgb555_to_rgb24
1872 .convert = rgb555_to_rgb32
1874 [PIX_FMT_YUV420P] = {
1875 .convert = rgb555_to_yuv420p
1878 .convert = rgb555_to_gray
1881 [PIX_FMT_RGB565] = {
1883 .convert = rgb565_to_rgb32
1886 .convert = rgb565_to_rgb24
1888 [PIX_FMT_YUV420P] = {
1889 .convert = rgb565_to_yuv420p
1892 .convert = rgb565_to_gray
1895 [PIX_FMT_GRAY16BE] = {
1897 .convert = gray16be_to_gray
1899 [PIX_FMT_GRAY16LE] = {
1900 .convert = gray16_to_gray16
1903 [PIX_FMT_GRAY16LE] = {
1905 .convert = gray16le_to_gray
1907 [PIX_FMT_GRAY16BE] = {
1908 .convert = gray16_to_gray16
1912 [PIX_FMT_RGB555] = {
1913 .convert = gray_to_rgb555
1915 [PIX_FMT_RGB565] = {
1916 .convert = gray_to_rgb565
1919 .convert = gray_to_rgb24
1922 .convert = gray_to_bgr24
1925 .convert = gray_to_rgb32
1927 [PIX_FMT_MONOWHITE] = {
1928 .convert = gray_to_monowhite
1930 [PIX_FMT_MONOBLACK] = {
1931 .convert = gray_to_monoblack
1933 [PIX_FMT_GRAY16LE] = {
1934 .convert = gray_to_gray16
1936 [PIX_FMT_GRAY16BE] = {
1937 .convert = gray_to_gray16
1940 [PIX_FMT_MONOWHITE] = {
1942 .convert = monowhite_to_gray
1945 [PIX_FMT_MONOBLACK] = {
1947 .convert = monoblack_to_gray
1951 [PIX_FMT_RGB555] = {
1952 .convert = pal8_to_rgb555
1954 [PIX_FMT_RGB565] = {
1955 .convert = pal8_to_rgb565
1958 .convert = pal8_to_bgr24
1961 .convert = pal8_to_rgb24
1964 .convert = pal8_to_rgb32
1967 [PIX_FMT_UYYVYY411] = {
1968 [PIX_FMT_YUV411P] = {
1969 .convert = uyyvyy411_to_yuv411p,
1975 int avpicture_alloc(AVPicture *picture,
1976 int pix_fmt, int width, int height)
1981 size = avpicture_get_size(pix_fmt, width, height);
1984 ptr = av_malloc(size);
1987 avpicture_fill(picture, ptr, pix_fmt, width, height);
1990 memset(picture, 0, sizeof(AVPicture));
1994 void avpicture_free(AVPicture *picture)
1996 av_free(picture->data[0]);
1999 /* return true if yuv planar */
2000 static inline int is_yuv_planar(const PixFmtInfo *ps)
2002 return (ps->color_type == FF_COLOR_YUV ||
2003 ps->color_type == FF_COLOR_YUV_JPEG) &&
2004 ps->pixel_type == FF_PIXEL_PLANAR;
2007 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2008 int pix_fmt, int top_band, int left_band)
2013 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2016 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2017 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2019 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2020 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2021 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2023 dst->linesize[0] = src->linesize[0];
2024 dst->linesize[1] = src->linesize[1];
2025 dst->linesize[2] = src->linesize[2];
2029 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2030 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2039 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2040 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2042 for (i = 0; i < 3; i++) {
2043 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2044 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2046 if (padtop || padleft) {
2047 memset(dst->data[i], color[i],
2048 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2051 if (padleft || padright) {
2052 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2053 (dst->linesize[i] - (padright >> x_shift));
2054 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2055 for (y = 0; y < yheight; y++) {
2056 memset(optr, color[i], (padleft + padright) >> x_shift);
2057 optr += dst->linesize[i];
2061 if (src) { /* first line */
2062 uint8_t *iptr = src->data[i];
2063 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2064 (padleft >> x_shift);
2065 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
2066 iptr += src->linesize[i];
2067 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2068 (dst->linesize[i] - (padright >> x_shift));
2069 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2070 for (y = 0; y < yheight; y++) {
2071 memset(optr, color[i], (padleft + padright) >> x_shift);
2072 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2073 (width - padleft - padright) >> x_shift);
2074 iptr += src->linesize[i];
2075 optr += dst->linesize[i];
2079 if (padbottom || padright) {
2080 optr = dst->data[i] + dst->linesize[i] *
2081 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2082 memset(optr, color[i],dst->linesize[i] *
2083 (padbottom >> y_shift) + (padright >> x_shift));
2089 #ifndef CONFIG_SWSCALE
2090 static uint8_t y_ccir_to_jpeg[256];
2091 static uint8_t y_jpeg_to_ccir[256];
2092 static uint8_t c_ccir_to_jpeg[256];
2093 static uint8_t c_jpeg_to_ccir[256];
2095 /* init various conversion tables */
2096 static void img_convert_init(void)
2099 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2101 for(i = 0;i < 256; i++) {
2102 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
2103 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
2104 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
2105 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
2109 /* apply to each pixel the given table */
2110 static void img_apply_table(uint8_t *dst, int dst_wrap,
2111 const uint8_t *src, int src_wrap,
2112 int width, int height, const uint8_t *table1)
2117 const uint8_t *table;
2120 for(;height > 0; height--) {
2144 /* XXX: use generic filter ? */
2145 /* XXX: in most cases, the sampling position is incorrect */
2148 static void shrink41(uint8_t *dst, int dst_wrap,
2149 const uint8_t *src, int src_wrap,
2150 int width, int height)
2156 for(;height > 0; height--) {
2159 for(w = width;w > 0; w--) {
2160 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
2170 static void shrink21(uint8_t *dst, int dst_wrap,
2171 const uint8_t *src, int src_wrap,
2172 int width, int height)
2178 for(;height > 0; height--) {
2181 for(w = width;w > 0; w--) {
2182 d[0] = (s[0] + s[1]) >> 1;
2192 static void shrink12(uint8_t *dst, int dst_wrap,
2193 const uint8_t *src, int src_wrap,
2194 int width, int height)
2198 const uint8_t *s1, *s2;
2200 for(;height > 0; height--) {
2204 for(w = width;w >= 4; w-=4) {
2205 d[0] = (s1[0] + s2[0]) >> 1;
2206 d[1] = (s1[1] + s2[1]) >> 1;
2207 d[2] = (s1[2] + s2[2]) >> 1;
2208 d[3] = (s1[3] + s2[3]) >> 1;
2214 d[0] = (s1[0] + s2[0]) >> 1;
2219 src += 2 * src_wrap;
2224 static void grow21_line(uint8_t *dst, const uint8_t *src,
2233 for(w = width;w >= 4; w-=4) {
2234 d[1] = d[0] = s1[0];
2235 d[3] = d[2] = s1[1];
2239 for(;w >= 2; w -= 2) {
2240 d[1] = d[0] = s1[0];
2244 /* only needed if width is not a multiple of two */
2245 /* XXX: veryfy that */
2251 static void grow41_line(uint8_t *dst, const uint8_t *src,
2260 for(w = width;w >= 4; w-=4) {
2272 static void grow21(uint8_t *dst, int dst_wrap,
2273 const uint8_t *src, int src_wrap,
2274 int width, int height)
2276 for(;height > 0; height--) {
2277 grow21_line(dst, src, width);
2284 static void grow12(uint8_t *dst, int dst_wrap,
2285 const uint8_t *src, int src_wrap,
2286 int width, int height)
2288 for(;height > 0; height-=2) {
2289 memcpy(dst, src, width);
2291 memcpy(dst, src, width);
2298 static void grow22(uint8_t *dst, int dst_wrap,
2299 const uint8_t *src, int src_wrap,
2300 int width, int height)
2302 for(;height > 0; height--) {
2303 grow21_line(dst, src, width);
2311 static void grow41(uint8_t *dst, int dst_wrap,
2312 const uint8_t *src, int src_wrap,
2313 int width, int height)
2315 for(;height > 0; height--) {
2316 grow41_line(dst, src, width);
2323 static void grow44(uint8_t *dst, int dst_wrap,
2324 const uint8_t *src, int src_wrap,
2325 int width, int height)
2327 for(;height > 0; height--) {
2328 grow41_line(dst, src, width);
2329 if ((height & 3) == 1)
2336 static void conv411(uint8_t *dst, int dst_wrap,
2337 const uint8_t *src, int src_wrap,
2338 int width, int height)
2341 const uint8_t *s1, *s2;
2346 for(;height > 0; height--) {
2348 s2 = src + src_wrap;
2350 for(w = width;w > 0; w--) {
2351 c = (s1[0] + s2[0]) >> 1;
2358 src += src_wrap * 2;
2363 /* XXX: always use linesize. Return -1 if not supported */
2364 int img_convert(AVPicture *dst, int dst_pix_fmt,
2365 const AVPicture *src, int src_pix_fmt,
2366 int src_width, int src_height)
2368 static int initialized;
2369 int i, ret, dst_width, dst_height, int_pix_fmt;
2370 const PixFmtInfo *src_pix, *dst_pix;
2371 const ConvertEntry *ce;
2372 AVPicture tmp1, *tmp = &tmp1;
2374 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2375 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2377 if (src_width <= 0 || src_height <= 0)
2385 dst_width = src_width;
2386 dst_height = src_height;
2388 dst_pix = &pix_fmt_info[dst_pix_fmt];
2389 src_pix = &pix_fmt_info[src_pix_fmt];
2390 if (src_pix_fmt == dst_pix_fmt) {
2391 /* no conversion needed: just copy */
2392 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2396 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2398 /* specific conversion routine */
2399 ce->convert(dst, src, dst_width, dst_height);
2404 if (is_yuv_planar(dst_pix) &&
2405 src_pix_fmt == PIX_FMT_GRAY8) {
2409 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2410 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2411 src->data[0], src->linesize[0],
2412 dst_width, dst_height);
2414 img_apply_table(dst->data[0], dst->linesize[0],
2415 src->data[0], src->linesize[0],
2416 dst_width, dst_height,
2419 /* fill U and V with 128 */
2422 w >>= dst_pix->x_chroma_shift;
2423 h >>= dst_pix->y_chroma_shift;
2424 for(i = 1; i <= 2; i++) {
2426 for(y = 0; y< h; y++) {
2428 d += dst->linesize[i];
2435 if (is_yuv_planar(src_pix) &&
2436 dst_pix_fmt == PIX_FMT_GRAY8) {
2437 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2438 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2439 src->data[0], src->linesize[0],
2440 dst_width, dst_height);
2442 img_apply_table(dst->data[0], dst->linesize[0],
2443 src->data[0], src->linesize[0],
2444 dst_width, dst_height,
2450 /* YUV to YUV planar */
2451 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2452 int x_shift, y_shift, w, h, xy_shift;
2453 void (*resize_func)(uint8_t *dst, int dst_wrap,
2454 const uint8_t *src, int src_wrap,
2455 int width, int height);
2457 /* compute chroma size of the smallest dimensions */
2460 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2461 w >>= dst_pix->x_chroma_shift;
2463 w >>= src_pix->x_chroma_shift;
2464 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2465 h >>= dst_pix->y_chroma_shift;
2467 h >>= src_pix->y_chroma_shift;
2469 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2470 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2471 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2472 /* there must be filters for conversion at least from and to
2476 resize_func = ff_img_copy_plane;
2479 resize_func = shrink21;
2482 resize_func = shrink41;
2485 resize_func = shrink12;
2488 resize_func = ff_shrink22;
2491 resize_func = ff_shrink44;
2494 resize_func = grow21;
2497 resize_func = grow12;
2500 resize_func = grow41;
2503 resize_func = grow22;
2506 resize_func = grow44;
2509 resize_func = conv411;
2512 /* currently not handled */
2513 goto no_chroma_filter;
2516 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2517 src->data[0], src->linesize[0],
2518 dst_width, dst_height);
2520 for(i = 1;i <= 2; i++)
2521 resize_func(dst->data[i], dst->linesize[i],
2522 src->data[i], src->linesize[i],
2523 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2524 /* if yuv color space conversion is needed, we do it here on
2525 the destination image */
2526 if (dst_pix->color_type != src_pix->color_type) {
2527 const uint8_t *y_table, *c_table;
2528 if (dst_pix->color_type == FF_COLOR_YUV) {
2529 y_table = y_jpeg_to_ccir;
2530 c_table = c_jpeg_to_ccir;
2532 y_table = y_ccir_to_jpeg;
2533 c_table = c_ccir_to_jpeg;
2535 img_apply_table(dst->data[0], dst->linesize[0],
2536 dst->data[0], dst->linesize[0],
2537 dst_width, dst_height,
2540 for(i = 1;i <= 2; i++)
2541 img_apply_table(dst->data[i], dst->linesize[i],
2542 dst->data[i], dst->linesize[i],
2543 dst_width>>dst_pix->x_chroma_shift,
2544 dst_height>>dst_pix->y_chroma_shift,
2551 /* try to use an intermediate format */
2552 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2553 dst_pix_fmt == PIX_FMT_YUYV422) {
2554 /* specific case: convert to YUV422P first */
2555 int_pix_fmt = PIX_FMT_YUV422P;
2556 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2557 dst_pix_fmt == PIX_FMT_UYVY422) {
2558 /* specific case: convert to YUV422P first */
2559 int_pix_fmt = PIX_FMT_YUV422P;
2560 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2561 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2562 /* specific case: convert to YUV411P first */
2563 int_pix_fmt = PIX_FMT_YUV411P;
2564 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2565 src_pix_fmt != PIX_FMT_GRAY8) ||
2566 (dst_pix->color_type == FF_COLOR_GRAY &&
2567 dst_pix_fmt != PIX_FMT_GRAY8)) {
2568 /* gray8 is the normalized format */
2569 int_pix_fmt = PIX_FMT_GRAY8;
2570 } else if ((is_yuv_planar(src_pix) &&
2571 src_pix_fmt != PIX_FMT_YUV444P &&
2572 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2573 /* yuv444 is the normalized format */
2574 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2575 int_pix_fmt = PIX_FMT_YUVJ444P;
2577 int_pix_fmt = PIX_FMT_YUV444P;
2578 } else if ((is_yuv_planar(dst_pix) &&
2579 dst_pix_fmt != PIX_FMT_YUV444P &&
2580 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2581 /* yuv444 is the normalized format */
2582 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2583 int_pix_fmt = PIX_FMT_YUVJ444P;
2585 int_pix_fmt = PIX_FMT_YUV444P;
2587 /* the two formats are rgb or gray8 or yuv[j]444p */
2588 if (src_pix->is_alpha && dst_pix->is_alpha)
2589 int_pix_fmt = PIX_FMT_RGB32;
2591 int_pix_fmt = PIX_FMT_RGB24;
2593 if (src_pix_fmt == int_pix_fmt)
2595 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2598 if (img_convert(tmp, int_pix_fmt,
2599 src, src_pix_fmt, src_width, src_height) < 0)
2601 if (img_convert(dst, dst_pix_fmt,
2602 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2606 avpicture_free(tmp);
2611 /* NOTE: we scan all the pixels to have an exact information */
2612 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2614 const unsigned char *p;
2615 int src_wrap, ret, x, y;
2617 uint32_t *palette = (uint32_t *)src->data[1];
2620 src_wrap = src->linesize[0] - width;
2622 for(y=0;y<height;y++) {
2623 for(x=0;x<width;x++) {
2624 a = palette[p[0]] >> 24;
2626 ret |= FF_ALPHA_TRANSP;
2627 } else if (a != 0xff) {
2628 ret |= FF_ALPHA_SEMI_TRANSP;
2637 int img_get_alpha_info(const AVPicture *src,
2638 int pix_fmt, int width, int height)
2640 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2643 pf = &pix_fmt_info[pix_fmt];
2644 /* no alpha can be represented in format */
2649 ret = get_alpha_info_rgb32(src, width, height);
2652 ret = get_alpha_info_pal8(src, width, height);
2655 /* we do not know, so everything is indicated */
2656 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2663 #define DEINT_INPLACE_LINE_LUM \
2664 movd_m2r(lum_m4[0],mm0);\
2665 movd_m2r(lum_m3[0],mm1);\
2666 movd_m2r(lum_m2[0],mm2);\
2667 movd_m2r(lum_m1[0],mm3);\
2668 movd_m2r(lum[0],mm4);\
2669 punpcklbw_r2r(mm7,mm0);\
2670 movd_r2m(mm2,lum_m4[0]);\
2671 punpcklbw_r2r(mm7,mm1);\
2672 punpcklbw_r2r(mm7,mm2);\
2673 punpcklbw_r2r(mm7,mm3);\
2674 punpcklbw_r2r(mm7,mm4);\
2675 paddw_r2r(mm3,mm1);\
2677 paddw_r2r(mm4,mm0);\
2679 paddw_r2r(mm6,mm2);\
2680 paddw_r2r(mm2,mm1);\
2681 psubusw_r2r(mm0,mm1);\
2683 packuswb_r2r(mm7,mm1);\
2684 movd_r2m(mm1,lum_m2[0]);
2686 #define DEINT_LINE_LUM \
2687 movd_m2r(lum_m4[0],mm0);\
2688 movd_m2r(lum_m3[0],mm1);\
2689 movd_m2r(lum_m2[0],mm2);\
2690 movd_m2r(lum_m1[0],mm3);\
2691 movd_m2r(lum[0],mm4);\
2692 punpcklbw_r2r(mm7,mm0);\
2693 punpcklbw_r2r(mm7,mm1);\
2694 punpcklbw_r2r(mm7,mm2);\
2695 punpcklbw_r2r(mm7,mm3);\
2696 punpcklbw_r2r(mm7,mm4);\
2697 paddw_r2r(mm3,mm1);\
2699 paddw_r2r(mm4,mm0);\
2701 paddw_r2r(mm6,mm2);\
2702 paddw_r2r(mm2,mm1);\
2703 psubusw_r2r(mm0,mm1);\
2705 packuswb_r2r(mm7,mm1);\
2706 movd_r2m(mm1,dst[0]);
2709 /* filter parameters: [-1 4 2 4 -1] // 8 */
2710 static void deinterlace_line(uint8_t *dst,
2711 const uint8_t *lum_m4, const uint8_t *lum_m3,
2712 const uint8_t *lum_m2, const uint8_t *lum_m1,
2717 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2720 for(;size > 0;size--) {
2722 sum += lum_m3[0] << 2;
2723 sum += lum_m2[0] << 1;
2724 sum += lum_m1[0] << 2;
2726 dst[0] = cm[(sum + 4) >> 3];
2738 movq_m2r(ff_pw_4,mm6);
2740 for (;size > 3; size-=4) {
2751 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2755 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2758 for(;size > 0;size--) {
2760 sum += lum_m3[0] << 2;
2761 sum += lum_m2[0] << 1;
2762 lum_m4[0]=lum_m2[0];
2763 sum += lum_m1[0] << 2;
2765 lum_m2[0] = cm[(sum + 4) >> 3];
2776 movq_m2r(ff_pw_4,mm6);
2778 for (;size > 3; size-=4) {
2779 DEINT_INPLACE_LINE_LUM
2789 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2790 top field is copied as is, but the bottom field is deinterlaced
2791 against the top field. */
2792 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2793 const uint8_t *src1, int src_wrap,
2794 int width, int height)
2796 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2801 src_0=&src_m1[src_wrap];
2802 src_p1=&src_0[src_wrap];
2803 src_p2=&src_p1[src_wrap];
2804 for(y=0;y<(height-2);y+=2) {
2805 memcpy(dst,src_m1,width);
2807 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2811 src_p1 += 2*src_wrap;
2812 src_p2 += 2*src_wrap;
2815 memcpy(dst,src_m1,width);
2818 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2821 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2822 int width, int height)
2824 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2827 buf = (uint8_t*)av_malloc(width);
2830 memcpy(buf,src_m1,width);
2831 src_0=&src_m1[src_wrap];
2832 src_p1=&src_0[src_wrap];
2833 src_p2=&src_p1[src_wrap];
2834 for(y=0;y<(height-2);y+=2) {
2835 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2838 src_p1 += 2*src_wrap;
2839 src_p2 += 2*src_wrap;
2842 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2846 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2847 int pix_fmt, int width, int height)
2851 if (pix_fmt != PIX_FMT_YUV420P &&
2852 pix_fmt != PIX_FMT_YUV422P &&
2853 pix_fmt != PIX_FMT_YUV444P &&
2854 pix_fmt != PIX_FMT_YUV411P &&
2855 pix_fmt != PIX_FMT_GRAY8)
2857 if ((width & 3) != 0 || (height & 3) != 0)
2863 case PIX_FMT_YUV420P:
2867 case PIX_FMT_YUV422P:
2870 case PIX_FMT_YUV411P:
2876 if (pix_fmt == PIX_FMT_GRAY8) {
2881 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2884 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2885 src->data[i], src->linesize[i],