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"
41 #define xglue(x, y) x ## y
42 #define glue(x, y) xglue(x, y)
44 #define FF_COLOR_RGB 0 /**< RGB color space */
45 #define FF_COLOR_GRAY 1 /**< gray color space */
46 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
47 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
49 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
50 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
51 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
53 typedef struct PixFmtInfo {
55 uint8_t nb_channels; /**< number of channels (including alpha) */
56 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
57 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
58 uint8_t is_alpha : 1; /**< true if alpha can be specified */
59 uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
60 uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
61 uint8_t depth; /**< bit depth of the color components */
64 /* this table gives more information about formats */
65 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
70 .color_type = FF_COLOR_YUV,
71 .pixel_type = FF_PIXEL_PLANAR,
73 .x_chroma_shift = 1, .y_chroma_shift = 1,
78 .color_type = FF_COLOR_YUV,
79 .pixel_type = FF_PIXEL_PLANAR,
81 .x_chroma_shift = 1, .y_chroma_shift = 0,
86 .color_type = FF_COLOR_YUV,
87 .pixel_type = FF_PIXEL_PLANAR,
89 .x_chroma_shift = 0, .y_chroma_shift = 0,
94 .color_type = FF_COLOR_YUV,
95 .pixel_type = FF_PIXEL_PACKED,
97 .x_chroma_shift = 1, .y_chroma_shift = 0,
102 .color_type = FF_COLOR_YUV,
103 .pixel_type = FF_PIXEL_PACKED,
105 .x_chroma_shift = 1, .y_chroma_shift = 0,
107 [PIX_FMT_YUV410P] = {
110 .color_type = FF_COLOR_YUV,
111 .pixel_type = FF_PIXEL_PLANAR,
113 .x_chroma_shift = 2, .y_chroma_shift = 2,
115 [PIX_FMT_YUV411P] = {
118 .color_type = FF_COLOR_YUV,
119 .pixel_type = FF_PIXEL_PLANAR,
121 .x_chroma_shift = 2, .y_chroma_shift = 0,
123 [PIX_FMT_YUV440P] = {
126 .color_type = FF_COLOR_YUV,
127 .pixel_type = FF_PIXEL_PLANAR,
129 .x_chroma_shift = 0, .y_chroma_shift = 1,
132 /* YUV formats with alpha plane */
133 [PIX_FMT_YUVA420P] = {
136 .color_type = FF_COLOR_YUV,
137 .pixel_type = FF_PIXEL_PLANAR,
139 .x_chroma_shift = 1, .y_chroma_shift = 1,
143 [PIX_FMT_YUVJ420P] = {
146 .color_type = FF_COLOR_YUV_JPEG,
147 .pixel_type = FF_PIXEL_PLANAR,
149 .x_chroma_shift = 1, .y_chroma_shift = 1,
151 [PIX_FMT_YUVJ422P] = {
154 .color_type = FF_COLOR_YUV_JPEG,
155 .pixel_type = FF_PIXEL_PLANAR,
157 .x_chroma_shift = 1, .y_chroma_shift = 0,
159 [PIX_FMT_YUVJ444P] = {
162 .color_type = FF_COLOR_YUV_JPEG,
163 .pixel_type = FF_PIXEL_PLANAR,
165 .x_chroma_shift = 0, .y_chroma_shift = 0,
167 [PIX_FMT_YUVJ440P] = {
170 .color_type = FF_COLOR_YUV_JPEG,
171 .pixel_type = FF_PIXEL_PLANAR,
173 .x_chroma_shift = 0, .y_chroma_shift = 1,
180 .color_type = FF_COLOR_RGB,
181 .pixel_type = FF_PIXEL_PACKED,
183 .x_chroma_shift = 0, .y_chroma_shift = 0,
188 .color_type = FF_COLOR_RGB,
189 .pixel_type = FF_PIXEL_PACKED,
191 .x_chroma_shift = 0, .y_chroma_shift = 0,
195 .nb_channels = 4, .is_alpha = 1,
196 .color_type = FF_COLOR_RGB,
197 .pixel_type = FF_PIXEL_PACKED,
199 .x_chroma_shift = 0, .y_chroma_shift = 0,
204 .color_type = FF_COLOR_RGB,
205 .pixel_type = FF_PIXEL_PACKED,
207 .x_chroma_shift = 0, .y_chroma_shift = 0,
212 .color_type = FF_COLOR_RGB,
213 .pixel_type = FF_PIXEL_PACKED,
215 .x_chroma_shift = 0, .y_chroma_shift = 0,
218 /* gray / mono formats */
219 [PIX_FMT_GRAY16BE] = {
222 .color_type = FF_COLOR_GRAY,
223 .pixel_type = FF_PIXEL_PLANAR,
226 [PIX_FMT_GRAY16LE] = {
229 .color_type = FF_COLOR_GRAY,
230 .pixel_type = FF_PIXEL_PLANAR,
236 .color_type = FF_COLOR_GRAY,
237 .pixel_type = FF_PIXEL_PLANAR,
240 [PIX_FMT_MONOWHITE] = {
243 .color_type = FF_COLOR_GRAY,
244 .pixel_type = FF_PIXEL_PLANAR,
247 [PIX_FMT_MONOBLACK] = {
250 .color_type = FF_COLOR_GRAY,
251 .pixel_type = FF_PIXEL_PLANAR,
255 /* paletted formats */
258 .nb_channels = 4, .is_alpha = 1,
259 .color_type = FF_COLOR_RGB,
260 .pixel_type = FF_PIXEL_PALETTE,
263 [PIX_FMT_XVMC_MPEG2_MC] = {
266 [PIX_FMT_XVMC_MPEG2_IDCT] = {
269 [PIX_FMT_UYYVYY411] = {
272 .color_type = FF_COLOR_YUV,
273 .pixel_type = FF_PIXEL_PACKED,
275 .x_chroma_shift = 2, .y_chroma_shift = 0,
279 .nb_channels = 4, .is_alpha = 1,
280 .color_type = FF_COLOR_RGB,
281 .pixel_type = FF_PIXEL_PACKED,
283 .x_chroma_shift = 0, .y_chroma_shift = 0,
288 .color_type = FF_COLOR_RGB,
289 .pixel_type = FF_PIXEL_PACKED,
291 .x_chroma_shift = 0, .y_chroma_shift = 0,
296 .color_type = FF_COLOR_RGB,
297 .pixel_type = FF_PIXEL_PACKED,
299 .x_chroma_shift = 0, .y_chroma_shift = 0,
304 .color_type = FF_COLOR_RGB,
305 .pixel_type = FF_PIXEL_PACKED,
307 .x_chroma_shift = 0, .y_chroma_shift = 0,
312 .color_type = FF_COLOR_RGB,
313 .pixel_type = FF_PIXEL_PACKED,
315 .x_chroma_shift = 0, .y_chroma_shift = 0,
317 [PIX_FMT_RGB4_BYTE] = {
320 .color_type = FF_COLOR_RGB,
321 .pixel_type = FF_PIXEL_PACKED,
323 .x_chroma_shift = 0, .y_chroma_shift = 0,
328 .color_type = FF_COLOR_RGB,
329 .pixel_type = FF_PIXEL_PACKED,
331 .x_chroma_shift = 0, .y_chroma_shift = 0,
336 .color_type = FF_COLOR_RGB,
337 .pixel_type = FF_PIXEL_PACKED,
339 .x_chroma_shift = 0, .y_chroma_shift = 0,
341 [PIX_FMT_BGR4_BYTE] = {
344 .color_type = FF_COLOR_RGB,
345 .pixel_type = FF_PIXEL_PACKED,
347 .x_chroma_shift = 0, .y_chroma_shift = 0,
352 .color_type = FF_COLOR_YUV,
353 .pixel_type = FF_PIXEL_PLANAR,
355 .x_chroma_shift = 1, .y_chroma_shift = 1,
360 .color_type = FF_COLOR_YUV,
361 .pixel_type = FF_PIXEL_PLANAR,
363 .x_chroma_shift = 1, .y_chroma_shift = 1,
366 [PIX_FMT_BGR32_1] = {
368 .nb_channels = 4, .is_alpha = 1,
369 .color_type = FF_COLOR_RGB,
370 .pixel_type = FF_PIXEL_PACKED,
372 .x_chroma_shift = 0, .y_chroma_shift = 0,
374 [PIX_FMT_RGB32_1] = {
376 .nb_channels = 4, .is_alpha = 1,
377 .color_type = FF_COLOR_RGB,
378 .pixel_type = FF_PIXEL_PACKED,
380 .x_chroma_shift = 0, .y_chroma_shift = 0,
384 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
386 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
387 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
390 const char *avcodec_get_pix_fmt_name(int pix_fmt)
392 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
395 return pix_fmt_info[pix_fmt].name;
398 enum PixelFormat avcodec_get_pix_fmt(const char* name)
402 for (i=0; i < PIX_FMT_NB; i++)
403 if (!strcmp(pix_fmt_info[i].name, name))
408 void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
410 PixFmtInfo info= pix_fmt_info[pix_fmt];
412 char is_alpha_char= info.is_alpha ? 'y' : 'n';
416 snprintf (buf, buf_size,
417 "name " " nb_channels" " depth" " is_alpha"
420 snprintf (buf, buf_size,
421 "%-10s" " %1d " " %2d " " %c ",
429 int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
432 const PixFmtInfo *pinfo;
434 memset(picture->linesize, 0, sizeof(picture->linesize));
436 pinfo = &pix_fmt_info[pix_fmt];
438 case PIX_FMT_YUV420P:
439 case PIX_FMT_YUV422P:
440 case PIX_FMT_YUV444P:
441 case PIX_FMT_YUV410P:
442 case PIX_FMT_YUV411P:
443 case PIX_FMT_YUV440P:
444 case PIX_FMT_YUVJ420P:
445 case PIX_FMT_YUVJ422P:
446 case PIX_FMT_YUVJ444P:
447 case PIX_FMT_YUVJ440P:
448 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
449 picture->linesize[0] = width;
450 picture->linesize[1] = w2;
451 picture->linesize[2] = w2;
453 case PIX_FMT_YUVA420P:
454 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
455 picture->linesize[0] = width;
456 picture->linesize[1] = w2;
457 picture->linesize[2] = w2;
458 picture->linesize[3] = width;
462 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
463 picture->linesize[0] = width;
464 picture->linesize[1] = w2;
468 picture->linesize[0] = width * 3;
472 case PIX_FMT_RGB32_1:
473 case PIX_FMT_BGR32_1:
474 picture->linesize[0] = width * 4;
476 case PIX_FMT_GRAY16BE:
477 case PIX_FMT_GRAY16LE:
482 case PIX_FMT_YUYV422:
483 picture->linesize[0] = width * 2;
485 case PIX_FMT_UYVY422:
486 picture->linesize[0] = width * 2;
488 case PIX_FMT_UYYVYY411:
489 picture->linesize[0] = width + width/2;
493 case PIX_FMT_RGB4_BYTE:
494 case PIX_FMT_BGR4_BYTE:
496 picture->linesize[0] = width;
500 picture->linesize[0] = width / 2;
502 case PIX_FMT_MONOWHITE:
503 case PIX_FMT_MONOBLACK:
504 picture->linesize[0] = (width + 7) >> 3;
507 picture->linesize[0] = width;
508 picture->linesize[1] = 4;
516 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
520 const PixFmtInfo *pinfo;
522 pinfo = &pix_fmt_info[pix_fmt];
523 size = picture->linesize[0] * height;
525 case PIX_FMT_YUV420P:
526 case PIX_FMT_YUV422P:
527 case PIX_FMT_YUV444P:
528 case PIX_FMT_YUV410P:
529 case PIX_FMT_YUV411P:
530 case PIX_FMT_YUV440P:
531 case PIX_FMT_YUVJ420P:
532 case PIX_FMT_YUVJ422P:
533 case PIX_FMT_YUVJ444P:
534 case PIX_FMT_YUVJ440P:
535 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
536 size2 = picture->linesize[1] * h2;
537 picture->data[0] = ptr;
538 picture->data[1] = picture->data[0] + size;
539 picture->data[2] = picture->data[1] + size2;
540 picture->data[3] = NULL;
541 return size + 2 * size2;
542 case PIX_FMT_YUVA420P:
543 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
544 size2 = picture->linesize[1] * h2;
545 picture->data[0] = ptr;
546 picture->data[1] = picture->data[0] + size;
547 picture->data[2] = picture->data[1] + size2;
548 picture->data[3] = picture->data[1] + size2 + size2;
549 return 2 * size + 2 * size2;
552 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
553 size2 = picture->linesize[1] * h2 * 2;
554 picture->data[0] = ptr;
555 picture->data[1] = picture->data[0] + size;
556 picture->data[2] = NULL;
557 picture->data[3] = NULL;
558 return size + 2 * size2;
563 case PIX_FMT_RGB32_1:
564 case PIX_FMT_BGR32_1:
565 case PIX_FMT_GRAY16BE:
566 case PIX_FMT_GRAY16LE:
571 case PIX_FMT_YUYV422:
572 case PIX_FMT_UYVY422:
573 case PIX_FMT_UYYVYY411:
576 case PIX_FMT_RGB4_BYTE:
577 case PIX_FMT_BGR4_BYTE:
581 case PIX_FMT_MONOWHITE:
582 case PIX_FMT_MONOBLACK:
583 picture->data[0] = ptr;
584 picture->data[1] = NULL;
585 picture->data[2] = NULL;
586 picture->data[3] = NULL;
589 size2 = (size + 3) & ~3;
590 picture->data[0] = ptr;
591 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
592 picture->data[2] = NULL;
593 picture->data[3] = NULL;
594 return size2 + 256 * 4;
596 picture->data[0] = NULL;
597 picture->data[1] = NULL;
598 picture->data[2] = NULL;
599 picture->data[3] = NULL;
604 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
605 int pix_fmt, int width, int height)
608 if(avcodec_check_dimensions(NULL, width, height))
611 if (ff_fill_linesize(picture, pix_fmt, width))
614 return ff_fill_pointer(picture, ptr, pix_fmt, height);
617 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
618 unsigned char *dest, int dest_size)
620 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
621 int i, j, w, h, data_planes;
622 const unsigned char* s;
623 int size = avpicture_get_size(pix_fmt, width, height);
625 if (size > dest_size || size < 0)
628 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
629 if (pix_fmt == PIX_FMT_YUYV422 ||
630 pix_fmt == PIX_FMT_UYVY422 ||
631 pix_fmt == PIX_FMT_BGR565 ||
632 pix_fmt == PIX_FMT_BGR555 ||
633 pix_fmt == PIX_FMT_RGB565 ||
634 pix_fmt == PIX_FMT_RGB555)
636 else if (pix_fmt == PIX_FMT_UYYVYY411)
638 else if (pix_fmt == PIX_FMT_PAL8)
641 w = width * (pf->depth * pf->nb_channels / 8);
646 data_planes = pf->nb_channels;
647 w = (width*pf->depth + 7)/8;
651 for (i=0; i<data_planes; i++) {
653 w = width >> pf->x_chroma_shift;
654 h = height >> pf->y_chroma_shift;
660 s += src->linesize[i];
664 if (pf->pixel_type == FF_PIXEL_PALETTE)
665 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
670 int avpicture_get_size(int pix_fmt, int width, int height)
672 AVPicture dummy_pict;
673 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
676 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
679 const PixFmtInfo *pf, *ps;
682 ps = &pix_fmt_info[src_pix_fmt];
683 pf = &pix_fmt_info[dst_pix_fmt];
687 pf = &pix_fmt_info[dst_pix_fmt];
688 if (pf->depth < ps->depth ||
689 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
690 loss |= FF_LOSS_DEPTH;
691 if (pf->x_chroma_shift > ps->x_chroma_shift ||
692 pf->y_chroma_shift > ps->y_chroma_shift)
693 loss |= FF_LOSS_RESOLUTION;
694 switch(pf->color_type) {
696 if (ps->color_type != FF_COLOR_RGB &&
697 ps->color_type != FF_COLOR_GRAY)
698 loss |= FF_LOSS_COLORSPACE;
701 if (ps->color_type != FF_COLOR_GRAY)
702 loss |= FF_LOSS_COLORSPACE;
705 if (ps->color_type != FF_COLOR_YUV)
706 loss |= FF_LOSS_COLORSPACE;
708 case FF_COLOR_YUV_JPEG:
709 if (ps->color_type != FF_COLOR_YUV_JPEG &&
710 ps->color_type != FF_COLOR_YUV &&
711 ps->color_type != FF_COLOR_GRAY)
712 loss |= FF_LOSS_COLORSPACE;
716 if (ps->color_type != pf->color_type)
717 loss |= FF_LOSS_COLORSPACE;
720 if (pf->color_type == FF_COLOR_GRAY &&
721 ps->color_type != FF_COLOR_GRAY)
722 loss |= FF_LOSS_CHROMA;
723 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
724 loss |= FF_LOSS_ALPHA;
725 if (pf->pixel_type == FF_PIXEL_PALETTE &&
726 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
727 loss |= FF_LOSS_COLORQUANT;
731 static int avg_bits_per_pixel(int pix_fmt)
734 const PixFmtInfo *pf;
736 pf = &pix_fmt_info[pix_fmt];
737 switch(pf->pixel_type) {
738 case FF_PIXEL_PACKED:
740 case PIX_FMT_YUYV422:
741 case PIX_FMT_UYVY422:
748 case PIX_FMT_UYYVYY411:
752 bits = pf->depth * pf->nb_channels;
756 case FF_PIXEL_PLANAR:
757 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
758 bits = pf->depth * pf->nb_channels;
760 bits = pf->depth + ((2 * pf->depth) >>
761 (pf->x_chroma_shift + pf->y_chroma_shift));
764 case FF_PIXEL_PALETTE:
774 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
779 int dist, i, loss, min_dist, dst_pix_fmt;
781 /* find exact color match with smallest size */
783 min_dist = 0x7fffffff;
784 for(i = 0;i < PIX_FMT_NB; i++) {
785 if (pix_fmt_mask & (1 << i)) {
786 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
788 dist = avg_bits_per_pixel(i);
789 if (dist < min_dist) {
799 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
800 int has_alpha, int *loss_ptr)
802 int dst_pix_fmt, loss_mask, i;
803 static const int loss_mask_order[] = {
804 ~0, /* no loss first */
807 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
813 /* try with successive loss */
816 loss_mask = loss_mask_order[i++];
817 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
818 has_alpha, loss_mask);
819 if (dst_pix_fmt >= 0)
827 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
831 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
832 const uint8_t *src, int src_wrap,
833 int width, int height)
837 for(;height > 0; height--) {
838 memcpy(dst, src, width);
844 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
847 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
849 pf = &pix_fmt_info[pix_fmt];
850 switch(pf->pixel_type) {
851 case FF_PIXEL_PACKED:
853 case PIX_FMT_YUYV422:
854 case PIX_FMT_UYVY422:
861 case PIX_FMT_UYYVYY411:
865 bits = pf->depth * pf->nb_channels;
868 return (width * bits + 7) >> 3;
870 case FF_PIXEL_PLANAR:
871 if (plane == 1 || plane == 2)
872 width >>= pf->x_chroma_shift;
874 return (width * pf->depth + 7) >> 3;
876 case FF_PIXEL_PALETTE:
885 void av_picture_copy(AVPicture *dst, const AVPicture *src,
886 int pix_fmt, int width, int height)
889 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
891 pf = &pix_fmt_info[pix_fmt];
892 switch(pf->pixel_type) {
893 case FF_PIXEL_PACKED:
894 case FF_PIXEL_PLANAR:
895 for(i = 0; i < pf->nb_channels; i++) {
897 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
900 if (i == 1 || i == 2) {
901 w >>= pf->x_chroma_shift;
902 h >>= 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];
1272 static uint8_t y_ccir_to_jpeg[256];
1273 static uint8_t y_jpeg_to_ccir[256];
1274 static uint8_t c_ccir_to_jpeg[256];
1275 static uint8_t c_jpeg_to_ccir[256];
1277 /* init various conversion tables */
1278 static void img_convert_init(void)
1281 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1283 for(i = 0;i < 256; i++) {
1284 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1285 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1286 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1287 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1291 /* apply to each pixel the given table */
1292 static void img_apply_table(uint8_t *dst, int dst_wrap,
1293 const uint8_t *src, int src_wrap,
1294 int width, int height, const uint8_t *table1)
1299 const uint8_t *table;
1302 for(;height > 0; height--) {
1326 /* XXX: use generic filter ? */
1327 /* XXX: in most cases, the sampling position is incorrect */
1330 static void shrink41(uint8_t *dst, int dst_wrap,
1331 const uint8_t *src, int src_wrap,
1332 int width, int height)
1338 for(;height > 0; height--) {
1341 for(w = width;w > 0; w--) {
1342 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1352 static void shrink21(uint8_t *dst, int dst_wrap,
1353 const uint8_t *src, int src_wrap,
1354 int width, int height)
1360 for(;height > 0; height--) {
1363 for(w = width;w > 0; w--) {
1364 d[0] = (s[0] + s[1]) >> 1;
1374 static void shrink12(uint8_t *dst, int dst_wrap,
1375 const uint8_t *src, int src_wrap,
1376 int width, int height)
1380 const uint8_t *s1, *s2;
1382 for(;height > 0; height--) {
1386 for(w = width;w >= 4; w-=4) {
1387 d[0] = (s1[0] + s2[0]) >> 1;
1388 d[1] = (s1[1] + s2[1]) >> 1;
1389 d[2] = (s1[2] + s2[2]) >> 1;
1390 d[3] = (s1[3] + s2[3]) >> 1;
1396 d[0] = (s1[0] + s2[0]) >> 1;
1401 src += 2 * src_wrap;
1407 void ff_shrink22(uint8_t *dst, int dst_wrap,
1408 const uint8_t *src, int src_wrap,
1409 int width, int height)
1412 const uint8_t *s1, *s2;
1415 for(;height > 0; height--) {
1419 for(w = width;w >= 4; w-=4) {
1420 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1421 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1422 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1423 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1429 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1434 src += 2 * src_wrap;
1440 void ff_shrink44(uint8_t *dst, int dst_wrap,
1441 const uint8_t *src, int src_wrap,
1442 int width, int height)
1445 const uint8_t *s1, *s2, *s3, *s4;
1448 for(;height > 0; height--) {
1454 for(w = width;w > 0; w--) {
1455 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1456 s2[0] + s2[1] + s2[2] + s2[3] +
1457 s3[0] + s3[1] + s3[2] + s3[3] +
1458 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1465 src += 4 * src_wrap;
1471 void ff_shrink88(uint8_t *dst, int dst_wrap,
1472 const uint8_t *src, int src_wrap,
1473 int width, int height)
1477 for(;height > 0; height--) {
1478 for(w = width;w > 0; w--) {
1481 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1484 *(dst++) = (tmp + 32)>>6;
1485 src += 8 - 8*src_wrap;
1487 src += 8*src_wrap - 8*width;
1488 dst += dst_wrap - width;
1492 static void grow21_line(uint8_t *dst, const uint8_t *src,
1501 for(w = width;w >= 4; w-=4) {
1502 d[1] = d[0] = s1[0];
1503 d[3] = d[2] = s1[1];
1507 for(;w >= 2; w -= 2) {
1508 d[1] = d[0] = s1[0];
1512 /* only needed if width is not a multiple of two */
1513 /* XXX: veryfy that */
1519 static void grow41_line(uint8_t *dst, const uint8_t *src,
1528 for(w = width;w >= 4; w-=4) {
1540 static void grow21(uint8_t *dst, int dst_wrap,
1541 const uint8_t *src, int src_wrap,
1542 int width, int height)
1544 for(;height > 0; height--) {
1545 grow21_line(dst, src, width);
1552 static void grow12(uint8_t *dst, int dst_wrap,
1553 const uint8_t *src, int src_wrap,
1554 int width, int height)
1556 for(;height > 0; height-=2) {
1557 memcpy(dst, src, width);
1559 memcpy(dst, src, width);
1566 static void grow22(uint8_t *dst, int dst_wrap,
1567 const uint8_t *src, int src_wrap,
1568 int width, int height)
1570 for(;height > 0; height--) {
1571 grow21_line(dst, src, width);
1579 static void grow41(uint8_t *dst, int dst_wrap,
1580 const uint8_t *src, int src_wrap,
1581 int width, int height)
1583 for(;height > 0; height--) {
1584 grow41_line(dst, src, width);
1591 static void grow44(uint8_t *dst, int dst_wrap,
1592 const uint8_t *src, int src_wrap,
1593 int width, int height)
1595 for(;height > 0; height--) {
1596 grow41_line(dst, src, width);
1597 if ((height & 3) == 1)
1604 static void conv411(uint8_t *dst, int dst_wrap,
1605 const uint8_t *src, int src_wrap,
1606 int width, int height)
1609 const uint8_t *s1, *s2;
1614 for(;height > 0; height--) {
1616 s2 = src + src_wrap;
1618 for(w = width;w > 0; w--) {
1619 c = (s1[0] + s2[0]) >> 1;
1626 src += src_wrap * 2;
1631 /* XXX: add jpeg quantize code */
1633 #define TRANSP_INDEX (6*6*6)
1635 /* this is maybe slow, but allows for extensions */
1636 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1638 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1641 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1644 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1647 pal = (uint32_t *)palette;
1649 for(r = 0; r < 6; r++) {
1650 for(g = 0; g < 6; g++) {
1651 for(b = 0; b < 6; b++) {
1652 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1653 (pal_value[g] << 8) | pal_value[b];
1660 pal[i++] = 0xff000000;
1663 /* copy bit n to bits 0 ... n - 1 */
1664 static inline unsigned int bitcopy_n(unsigned int a, int n)
1667 mask = (1 << n) - 1;
1668 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1671 /* rgb555 handling */
1673 #define RGB_NAME rgb555
1675 #define RGB_IN(r, g, b, s)\
1677 unsigned int v = ((const uint16_t *)(s))[0];\
1678 r = bitcopy_n(v >> (10 - 3), 3);\
1679 g = bitcopy_n(v >> (5 - 3), 3);\
1680 b = bitcopy_n(v << 3, 3);\
1684 #define RGB_OUT(d, r, g, b)\
1686 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1691 #include "imgconvert_template.h"
1693 /* rgb565 handling */
1695 #define RGB_NAME rgb565
1697 #define RGB_IN(r, g, b, s)\
1699 unsigned int v = ((const uint16_t *)(s))[0];\
1700 r = bitcopy_n(v >> (11 - 3), 3);\
1701 g = bitcopy_n(v >> (5 - 2), 2);\
1702 b = bitcopy_n(v << 3, 3);\
1705 #define RGB_OUT(d, r, g, b)\
1707 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1712 #include "imgconvert_template.h"
1714 /* bgr24 handling */
1716 #define RGB_NAME bgr24
1718 #define RGB_IN(r, g, b, s)\
1725 #define RGB_OUT(d, r, g, b)\
1734 #include "imgconvert_template.h"
1740 /* rgb24 handling */
1742 #define RGB_NAME rgb24
1745 #define RGB_IN(r, g, b, s)\
1752 #define RGB_OUT(d, r, g, b)\
1761 #include "imgconvert_template.h"
1763 /* rgb32 handling */
1765 #define RGB_NAME rgb32
1768 #define RGB_IN(r, g, b, s)\
1770 unsigned int v = ((const uint32_t *)(s))[0];\
1771 r = (v >> 16) & 0xff;\
1772 g = (v >> 8) & 0xff;\
1776 #define RGBA_IN(r, g, b, a, s)\
1778 unsigned int v = ((const uint32_t *)(s))[0];\
1779 a = (v >> 24) & 0xff;\
1780 r = (v >> 16) & 0xff;\
1781 g = (v >> 8) & 0xff;\
1785 #define RGBA_OUT(d, r, g, b, a)\
1787 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1792 #include "imgconvert_template.h"
1794 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1795 int width, int height, int xor_mask)
1797 const unsigned char *p;
1799 int v, dst_wrap, src_wrap;
1803 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1806 dst_wrap = dst->linesize[0] - width;
1807 for(y=0;y<height;y++) {
1810 v = *p++ ^ xor_mask;
1812 q[1] = -((v >> 6) & 1);
1813 q[2] = -((v >> 5) & 1);
1814 q[3] = -((v >> 4) & 1);
1815 q[4] = -((v >> 3) & 1);
1816 q[5] = -((v >> 2) & 1);
1817 q[6] = -((v >> 1) & 1);
1818 q[7] = -((v >> 0) & 1);
1823 v = *p++ ^ xor_mask;
1825 q[0] = -((v >> 7) & 1);
1835 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1836 int width, int height)
1838 mono_to_gray(dst, src, width, height, 0xff);
1841 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1842 int width, int height)
1844 mono_to_gray(dst, src, width, height, 0x00);
1847 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1848 int width, int height, int xor_mask)
1853 int j, b, v, n1, src_wrap, dst_wrap, y;
1856 src_wrap = src->linesize[0] - width;
1859 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1861 for(y=0;y<height;y++) {
1868 v = (v << 1) | (b >> 7);
1870 d[0] = v ^ xor_mask;
1880 v = (v << 1) | (b >> 7);
1883 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1891 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1892 int width, int height)
1894 gray_to_mono(dst, src, width, height, 0xff);
1897 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1898 int width, int height)
1900 gray_to_mono(dst, src, width, height, 0x00);
1903 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1904 int width, int height)
1906 int x, y, src_wrap, dst_wrap;
1909 src_wrap = src->linesize[0] - width;
1911 dst_wrap = dst->linesize[0] - width * 2;
1912 for(y=0; y<height; y++){
1913 for(x=0; x<width; x++){
1922 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1923 int width, int height)
1925 int x, y, src_wrap, dst_wrap;
1928 src_wrap = src->linesize[0] - width * 2;
1930 dst_wrap = dst->linesize[0] - width;
1931 for(y=0; y<height; y++){
1932 for(x=0; x<width; x++){
1941 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1942 int width, int height)
1944 gray16_to_gray(dst, src, width, height);
1947 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1948 int width, int height)
1950 AVPicture tmpsrc = *src;
1952 gray16_to_gray(dst, &tmpsrc, width, height);
1955 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1956 int width, int height)
1958 int x, y, src_wrap, dst_wrap;
1960 s = (uint16_t*)src->data[0];
1961 src_wrap = (src->linesize[0] - width * 2)/2;
1962 d = (uint16_t*)dst->data[0];
1963 dst_wrap = (dst->linesize[0] - width * 2)/2;
1964 for(y=0; y<height; y++){
1965 for(x=0; x<width; x++){
1966 *d++ = bswap_16(*s++);
1974 typedef struct ConvertEntry {
1975 void (*convert)(AVPicture *dst,
1976 const AVPicture *src, int width, int height);
1979 /* Add each new conversion function in this table. In order to be able
1980 to convert from any format to any format, the following constraints
1983 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1985 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1987 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1989 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1992 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1994 The other conversion functions are just optimizations for common cases.
1996 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1997 [PIX_FMT_YUV420P] = {
1998 [PIX_FMT_YUYV422] = {
1999 .convert = yuv420p_to_yuyv422,
2001 [PIX_FMT_RGB555] = {
2002 .convert = yuv420p_to_rgb555
2004 [PIX_FMT_RGB565] = {
2005 .convert = yuv420p_to_rgb565
2008 .convert = yuv420p_to_bgr24
2011 .convert = yuv420p_to_rgb24
2014 .convert = yuv420p_to_rgb32
2016 [PIX_FMT_UYVY422] = {
2017 .convert = yuv420p_to_uyvy422,
2020 [PIX_FMT_YUV422P] = {
2021 [PIX_FMT_YUYV422] = {
2022 .convert = yuv422p_to_yuyv422,
2024 [PIX_FMT_UYVY422] = {
2025 .convert = yuv422p_to_uyvy422,
2028 [PIX_FMT_YUV444P] = {
2030 .convert = yuv444p_to_rgb24
2033 [PIX_FMT_YUVJ420P] = {
2034 [PIX_FMT_RGB555] = {
2035 .convert = yuvj420p_to_rgb555
2037 [PIX_FMT_RGB565] = {
2038 .convert = yuvj420p_to_rgb565
2041 .convert = yuvj420p_to_bgr24
2044 .convert = yuvj420p_to_rgb24
2047 .convert = yuvj420p_to_rgb32
2050 [PIX_FMT_YUVJ444P] = {
2052 .convert = yuvj444p_to_rgb24
2055 [PIX_FMT_YUYV422] = {
2056 [PIX_FMT_YUV420P] = {
2057 .convert = yuyv422_to_yuv420p,
2059 [PIX_FMT_YUV422P] = {
2060 .convert = yuyv422_to_yuv422p,
2063 [PIX_FMT_UYVY422] = {
2064 [PIX_FMT_YUV420P] = {
2065 .convert = uyvy422_to_yuv420p,
2067 [PIX_FMT_YUV422P] = {
2068 .convert = uyvy422_to_yuv422p,
2072 [PIX_FMT_YUV420P] = {
2073 .convert = rgb24_to_yuv420p
2075 [PIX_FMT_RGB565] = {
2076 .convert = rgb24_to_rgb565
2078 [PIX_FMT_RGB555] = {
2079 .convert = rgb24_to_rgb555
2082 .convert = rgb24_to_rgb32
2085 .convert = rgb24_to_bgr24
2088 .convert = rgb24_to_gray
2091 .convert = rgb24_to_pal8
2093 [PIX_FMT_YUV444P] = {
2094 .convert = rgb24_to_yuv444p
2096 [PIX_FMT_YUVJ420P] = {
2097 .convert = rgb24_to_yuvj420p
2099 [PIX_FMT_YUVJ444P] = {
2100 .convert = rgb24_to_yuvj444p
2105 .convert = rgb32_to_rgb24
2108 .convert = rgb32_to_bgr24
2110 [PIX_FMT_RGB565] = {
2111 .convert = rgb32_to_rgb565
2113 [PIX_FMT_RGB555] = {
2114 .convert = rgb32_to_rgb555
2117 .convert = rgb32_to_pal8
2119 [PIX_FMT_YUV420P] = {
2120 .convert = rgb32_to_yuv420p
2123 .convert = rgb32_to_gray
2128 .convert = bgr24_to_rgb32
2131 .convert = bgr24_to_rgb24
2133 [PIX_FMT_YUV420P] = {
2134 .convert = bgr24_to_yuv420p
2137 .convert = bgr24_to_gray
2140 [PIX_FMT_RGB555] = {
2142 .convert = rgb555_to_rgb24
2145 .convert = rgb555_to_rgb32
2147 [PIX_FMT_YUV420P] = {
2148 .convert = rgb555_to_yuv420p
2151 .convert = rgb555_to_gray
2154 [PIX_FMT_RGB565] = {
2156 .convert = rgb565_to_rgb32
2159 .convert = rgb565_to_rgb24
2161 [PIX_FMT_YUV420P] = {
2162 .convert = rgb565_to_yuv420p
2165 .convert = rgb565_to_gray
2168 [PIX_FMT_GRAY16BE] = {
2170 .convert = gray16be_to_gray
2172 [PIX_FMT_GRAY16LE] = {
2173 .convert = gray16_to_gray16
2176 [PIX_FMT_GRAY16LE] = {
2178 .convert = gray16le_to_gray
2180 [PIX_FMT_GRAY16BE] = {
2181 .convert = gray16_to_gray16
2185 [PIX_FMT_RGB555] = {
2186 .convert = gray_to_rgb555
2188 [PIX_FMT_RGB565] = {
2189 .convert = gray_to_rgb565
2192 .convert = gray_to_rgb24
2195 .convert = gray_to_bgr24
2198 .convert = gray_to_rgb32
2200 [PIX_FMT_MONOWHITE] = {
2201 .convert = gray_to_monowhite
2203 [PIX_FMT_MONOBLACK] = {
2204 .convert = gray_to_monoblack
2206 [PIX_FMT_GRAY16LE] = {
2207 .convert = gray_to_gray16
2209 [PIX_FMT_GRAY16BE] = {
2210 .convert = gray_to_gray16
2213 [PIX_FMT_MONOWHITE] = {
2215 .convert = monowhite_to_gray
2218 [PIX_FMT_MONOBLACK] = {
2220 .convert = monoblack_to_gray
2224 [PIX_FMT_RGB555] = {
2225 .convert = pal8_to_rgb555
2227 [PIX_FMT_RGB565] = {
2228 .convert = pal8_to_rgb565
2231 .convert = pal8_to_bgr24
2234 .convert = pal8_to_rgb24
2237 .convert = pal8_to_rgb32
2240 [PIX_FMT_UYYVYY411] = {
2241 [PIX_FMT_YUV411P] = {
2242 .convert = uyyvyy411_to_yuv411p,
2248 int avpicture_alloc(AVPicture *picture,
2249 int pix_fmt, int width, int height)
2254 size = avpicture_get_size(pix_fmt, width, height);
2257 ptr = av_malloc(size);
2260 avpicture_fill(picture, ptr, pix_fmt, width, height);
2263 memset(picture, 0, sizeof(AVPicture));
2267 void avpicture_free(AVPicture *picture)
2269 av_free(picture->data[0]);
2272 /* return true if yuv planar */
2273 static inline int is_yuv_planar(const PixFmtInfo *ps)
2275 return (ps->color_type == FF_COLOR_YUV ||
2276 ps->color_type == FF_COLOR_YUV_JPEG) &&
2277 ps->pixel_type == FF_PIXEL_PLANAR;
2280 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2281 int pix_fmt, int top_band, int left_band)
2286 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2289 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2290 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2292 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2293 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2294 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2296 dst->linesize[0] = src->linesize[0];
2297 dst->linesize[1] = src->linesize[1];
2298 dst->linesize[2] = src->linesize[2];
2302 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2303 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2312 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2313 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2315 for (i = 0; i < 3; i++) {
2316 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2317 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2319 if (padtop || padleft) {
2320 memset(dst->data[i], color[i],
2321 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2324 if (padleft || padright) {
2325 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2326 (dst->linesize[i] - (padright >> x_shift));
2327 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2328 for (y = 0; y < yheight; y++) {
2329 memset(optr, color[i], (padleft + padright) >> x_shift);
2330 optr += dst->linesize[i];
2334 if (src) { /* first line */
2335 uint8_t *iptr = src->data[i];
2336 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2337 (padleft >> x_shift);
2338 memcpy(optr, iptr, src->linesize[i]);
2339 iptr += src->linesize[i];
2340 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2341 (dst->linesize[i] - (padright >> x_shift));
2342 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2343 for (y = 0; y < yheight; y++) {
2344 memset(optr, color[i], (padleft + padright) >> x_shift);
2345 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2347 iptr += src->linesize[i];
2348 optr += dst->linesize[i];
2352 if (padbottom || padright) {
2353 optr = dst->data[i] + dst->linesize[i] *
2354 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2355 memset(optr, color[i],dst->linesize[i] *
2356 (padbottom >> y_shift) + (padright >> x_shift));
2362 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
2363 void img_copy(AVPicture *dst, const AVPicture *src,
2364 int pix_fmt, int width, int height)
2366 av_picture_copy(dst, src, pix_fmt, width, height);
2369 int img_crop(AVPicture *dst, const AVPicture *src,
2370 int pix_fmt, int top_band, int left_band)
2372 return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
2375 int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2376 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2379 return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
2383 #ifndef CONFIG_SWSCALER
2384 /* XXX: always use linesize. Return -1 if not supported */
2385 int img_convert(AVPicture *dst, int dst_pix_fmt,
2386 const AVPicture *src, int src_pix_fmt,
2387 int src_width, int src_height)
2389 static int initialized;
2390 int i, ret, dst_width, dst_height, int_pix_fmt;
2391 const PixFmtInfo *src_pix, *dst_pix;
2392 const ConvertEntry *ce;
2393 AVPicture tmp1, *tmp = &tmp1;
2395 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2396 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2398 if (src_width <= 0 || src_height <= 0)
2406 dst_width = src_width;
2407 dst_height = src_height;
2409 dst_pix = &pix_fmt_info[dst_pix_fmt];
2410 src_pix = &pix_fmt_info[src_pix_fmt];
2411 if (src_pix_fmt == dst_pix_fmt) {
2412 /* no conversion needed: just copy */
2413 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2417 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2419 /* specific conversion routine */
2420 ce->convert(dst, src, dst_width, dst_height);
2425 if (is_yuv_planar(dst_pix) &&
2426 src_pix_fmt == PIX_FMT_GRAY8) {
2430 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2431 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2432 src->data[0], src->linesize[0],
2433 dst_width, dst_height);
2435 img_apply_table(dst->data[0], dst->linesize[0],
2436 src->data[0], src->linesize[0],
2437 dst_width, dst_height,
2440 /* fill U and V with 128 */
2443 w >>= dst_pix->x_chroma_shift;
2444 h >>= dst_pix->y_chroma_shift;
2445 for(i = 1; i <= 2; i++) {
2447 for(y = 0; y< h; y++) {
2449 d += dst->linesize[i];
2456 if (is_yuv_planar(src_pix) &&
2457 dst_pix_fmt == PIX_FMT_GRAY8) {
2458 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2459 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2460 src->data[0], src->linesize[0],
2461 dst_width, dst_height);
2463 img_apply_table(dst->data[0], dst->linesize[0],
2464 src->data[0], src->linesize[0],
2465 dst_width, dst_height,
2471 /* YUV to YUV planar */
2472 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2473 int x_shift, y_shift, w, h, xy_shift;
2474 void (*resize_func)(uint8_t *dst, int dst_wrap,
2475 const uint8_t *src, int src_wrap,
2476 int width, int height);
2478 /* compute chroma size of the smallest dimensions */
2481 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2482 w >>= dst_pix->x_chroma_shift;
2484 w >>= src_pix->x_chroma_shift;
2485 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2486 h >>= dst_pix->y_chroma_shift;
2488 h >>= src_pix->y_chroma_shift;
2490 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2491 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2492 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2493 /* there must be filters for conversion at least from and to
2497 resize_func = ff_img_copy_plane;
2500 resize_func = shrink21;
2503 resize_func = shrink41;
2506 resize_func = shrink12;
2509 resize_func = ff_shrink22;
2512 resize_func = ff_shrink44;
2515 resize_func = grow21;
2518 resize_func = grow12;
2521 resize_func = grow41;
2524 resize_func = grow22;
2527 resize_func = grow44;
2530 resize_func = conv411;
2533 /* currently not handled */
2534 goto no_chroma_filter;
2537 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2538 src->data[0], src->linesize[0],
2539 dst_width, dst_height);
2541 for(i = 1;i <= 2; i++)
2542 resize_func(dst->data[i], dst->linesize[i],
2543 src->data[i], src->linesize[i],
2544 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2545 /* if yuv color space conversion is needed, we do it here on
2546 the destination image */
2547 if (dst_pix->color_type != src_pix->color_type) {
2548 const uint8_t *y_table, *c_table;
2549 if (dst_pix->color_type == FF_COLOR_YUV) {
2550 y_table = y_jpeg_to_ccir;
2551 c_table = c_jpeg_to_ccir;
2553 y_table = y_ccir_to_jpeg;
2554 c_table = c_ccir_to_jpeg;
2556 img_apply_table(dst->data[0], dst->linesize[0],
2557 dst->data[0], dst->linesize[0],
2558 dst_width, dst_height,
2561 for(i = 1;i <= 2; i++)
2562 img_apply_table(dst->data[i], dst->linesize[i],
2563 dst->data[i], dst->linesize[i],
2564 dst_width>>dst_pix->x_chroma_shift,
2565 dst_height>>dst_pix->y_chroma_shift,
2572 /* try to use an intermediate format */
2573 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2574 dst_pix_fmt == PIX_FMT_YUYV422) {
2575 /* specific case: convert to YUV422P first */
2576 int_pix_fmt = PIX_FMT_YUV422P;
2577 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2578 dst_pix_fmt == PIX_FMT_UYVY422) {
2579 /* specific case: convert to YUV422P first */
2580 int_pix_fmt = PIX_FMT_YUV422P;
2581 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2582 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2583 /* specific case: convert to YUV411P first */
2584 int_pix_fmt = PIX_FMT_YUV411P;
2585 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2586 src_pix_fmt != PIX_FMT_GRAY8) ||
2587 (dst_pix->color_type == FF_COLOR_GRAY &&
2588 dst_pix_fmt != PIX_FMT_GRAY8)) {
2589 /* gray8 is the normalized format */
2590 int_pix_fmt = PIX_FMT_GRAY8;
2591 } else if ((is_yuv_planar(src_pix) &&
2592 src_pix_fmt != PIX_FMT_YUV444P &&
2593 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2594 /* yuv444 is the normalized format */
2595 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2596 int_pix_fmt = PIX_FMT_YUVJ444P;
2598 int_pix_fmt = PIX_FMT_YUV444P;
2599 } else if ((is_yuv_planar(dst_pix) &&
2600 dst_pix_fmt != PIX_FMT_YUV444P &&
2601 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2602 /* yuv444 is the normalized format */
2603 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2604 int_pix_fmt = PIX_FMT_YUVJ444P;
2606 int_pix_fmt = PIX_FMT_YUV444P;
2608 /* the two formats are rgb or gray8 or yuv[j]444p */
2609 if (src_pix->is_alpha && dst_pix->is_alpha)
2610 int_pix_fmt = PIX_FMT_RGB32;
2612 int_pix_fmt = PIX_FMT_RGB24;
2614 if (src_pix_fmt == int_pix_fmt)
2616 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2619 if (img_convert(tmp, int_pix_fmt,
2620 src, src_pix_fmt, src_width, src_height) < 0)
2622 if (img_convert(dst, dst_pix_fmt,
2623 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2627 avpicture_free(tmp);
2632 /* NOTE: we scan all the pixels to have an exact information */
2633 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2635 const unsigned char *p;
2636 int src_wrap, ret, x, y;
2638 uint32_t *palette = (uint32_t *)src->data[1];
2641 src_wrap = src->linesize[0] - width;
2643 for(y=0;y<height;y++) {
2644 for(x=0;x<width;x++) {
2645 a = palette[p[0]] >> 24;
2647 ret |= FF_ALPHA_TRANSP;
2648 } else if (a != 0xff) {
2649 ret |= FF_ALPHA_SEMI_TRANSP;
2658 int img_get_alpha_info(const AVPicture *src,
2659 int pix_fmt, int width, int height)
2661 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2664 pf = &pix_fmt_info[pix_fmt];
2665 /* no alpha can be represented in format */
2670 ret = get_alpha_info_rgb32(src, width, height);
2673 ret = get_alpha_info_pal8(src, width, height);
2676 /* we do not know, so everything is indicated */
2677 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2684 #define DEINT_INPLACE_LINE_LUM \
2685 movd_m2r(lum_m4[0],mm0);\
2686 movd_m2r(lum_m3[0],mm1);\
2687 movd_m2r(lum_m2[0],mm2);\
2688 movd_m2r(lum_m1[0],mm3);\
2689 movd_m2r(lum[0],mm4);\
2690 punpcklbw_r2r(mm7,mm0);\
2691 movd_r2m(mm2,lum_m4[0]);\
2692 punpcklbw_r2r(mm7,mm1);\
2693 punpcklbw_r2r(mm7,mm2);\
2694 punpcklbw_r2r(mm7,mm3);\
2695 punpcklbw_r2r(mm7,mm4);\
2696 paddw_r2r(mm3,mm1);\
2698 paddw_r2r(mm4,mm0);\
2700 paddw_r2r(mm6,mm2);\
2701 paddw_r2r(mm2,mm1);\
2702 psubusw_r2r(mm0,mm1);\
2704 packuswb_r2r(mm7,mm1);\
2705 movd_r2m(mm1,lum_m2[0]);
2707 #define DEINT_LINE_LUM \
2708 movd_m2r(lum_m4[0],mm0);\
2709 movd_m2r(lum_m3[0],mm1);\
2710 movd_m2r(lum_m2[0],mm2);\
2711 movd_m2r(lum_m1[0],mm3);\
2712 movd_m2r(lum[0],mm4);\
2713 punpcklbw_r2r(mm7,mm0);\
2714 punpcklbw_r2r(mm7,mm1);\
2715 punpcklbw_r2r(mm7,mm2);\
2716 punpcklbw_r2r(mm7,mm3);\
2717 punpcklbw_r2r(mm7,mm4);\
2718 paddw_r2r(mm3,mm1);\
2720 paddw_r2r(mm4,mm0);\
2722 paddw_r2r(mm6,mm2);\
2723 paddw_r2r(mm2,mm1);\
2724 psubusw_r2r(mm0,mm1);\
2726 packuswb_r2r(mm7,mm1);\
2727 movd_r2m(mm1,dst[0]);
2730 /* filter parameters: [-1 4 2 4 -1] // 8 */
2731 static void deinterlace_line(uint8_t *dst,
2732 const uint8_t *lum_m4, const uint8_t *lum_m3,
2733 const uint8_t *lum_m2, const uint8_t *lum_m1,
2738 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2741 for(;size > 0;size--) {
2743 sum += lum_m3[0] << 2;
2744 sum += lum_m2[0] << 1;
2745 sum += lum_m1[0] << 2;
2747 dst[0] = cm[(sum + 4) >> 3];
2764 movq_m2r(rounder,mm6);
2766 for (;size > 3; size-=4) {
2777 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2781 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2784 for(;size > 0;size--) {
2786 sum += lum_m3[0] << 2;
2787 sum += lum_m2[0] << 1;
2788 lum_m4[0]=lum_m2[0];
2789 sum += lum_m1[0] << 2;
2791 lum_m2[0] = cm[(sum + 4) >> 3];
2807 movq_m2r(rounder,mm6);
2809 for (;size > 3; size-=4) {
2810 DEINT_INPLACE_LINE_LUM
2820 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2821 top field is copied as is, but the bottom field is deinterlaced
2822 against the top field. */
2823 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2824 const uint8_t *src1, int src_wrap,
2825 int width, int height)
2827 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2832 src_0=&src_m1[src_wrap];
2833 src_p1=&src_0[src_wrap];
2834 src_p2=&src_p1[src_wrap];
2835 for(y=0;y<(height-2);y+=2) {
2836 memcpy(dst,src_m1,width);
2838 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2842 src_p1 += 2*src_wrap;
2843 src_p2 += 2*src_wrap;
2846 memcpy(dst,src_m1,width);
2849 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2852 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2853 int width, int height)
2855 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2858 buf = (uint8_t*)av_malloc(width);
2861 memcpy(buf,src_m1,width);
2862 src_0=&src_m1[src_wrap];
2863 src_p1=&src_0[src_wrap];
2864 src_p2=&src_p1[src_wrap];
2865 for(y=0;y<(height-2);y+=2) {
2866 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2869 src_p1 += 2*src_wrap;
2870 src_p2 += 2*src_wrap;
2873 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2877 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2878 int pix_fmt, int width, int height)
2882 if (pix_fmt != PIX_FMT_YUV420P &&
2883 pix_fmt != PIX_FMT_YUV422P &&
2884 pix_fmt != PIX_FMT_YUV444P &&
2885 pix_fmt != PIX_FMT_YUV411P &&
2886 pix_fmt != PIX_FMT_GRAY8)
2888 if ((width & 3) != 0 || (height & 3) != 0)
2894 case PIX_FMT_YUV420P:
2898 case PIX_FMT_YUV422P:
2901 case PIX_FMT_YUV411P:
2907 if (pix_fmt == PIX_FMT_GRAY8) {
2912 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2915 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2916 src->data[i], src->linesize[i],