2 * Misc image convertion 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 convertion 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
40 #define xglue(x, y) x ## y
41 #define glue(x, y) xglue(x, y)
43 #define FF_COLOR_RGB 0 /**< RGB color space */
44 #define FF_COLOR_GRAY 1 /**< gray color space */
45 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
46 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
48 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
49 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
50 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
52 typedef struct PixFmtInfo {
54 uint8_t nb_channels; /**< number of channels (including alpha) */
55 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
56 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
57 uint8_t is_alpha : 1; /**< true if alpha can be specified */
58 uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
59 uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
60 uint8_t depth; /**< bit depth of the color components */
63 /* this table gives more information about formats */
64 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
69 .color_type = FF_COLOR_YUV,
70 .pixel_type = FF_PIXEL_PLANAR,
72 .x_chroma_shift = 1, .y_chroma_shift = 1,
77 .color_type = FF_COLOR_YUV,
78 .pixel_type = FF_PIXEL_PLANAR,
80 .x_chroma_shift = 1, .y_chroma_shift = 0,
85 .color_type = FF_COLOR_YUV,
86 .pixel_type = FF_PIXEL_PLANAR,
88 .x_chroma_shift = 0, .y_chroma_shift = 0,
93 .color_type = FF_COLOR_YUV,
94 .pixel_type = FF_PIXEL_PACKED,
96 .x_chroma_shift = 1, .y_chroma_shift = 0,
101 .color_type = FF_COLOR_YUV,
102 .pixel_type = FF_PIXEL_PACKED,
104 .x_chroma_shift = 1, .y_chroma_shift = 0,
106 [PIX_FMT_YUV410P] = {
109 .color_type = FF_COLOR_YUV,
110 .pixel_type = FF_PIXEL_PLANAR,
112 .x_chroma_shift = 2, .y_chroma_shift = 2,
114 [PIX_FMT_YUV411P] = {
117 .color_type = FF_COLOR_YUV,
118 .pixel_type = FF_PIXEL_PLANAR,
120 .x_chroma_shift = 2, .y_chroma_shift = 0,
124 [PIX_FMT_YUVJ420P] = {
127 .color_type = FF_COLOR_YUV_JPEG,
128 .pixel_type = FF_PIXEL_PLANAR,
130 .x_chroma_shift = 1, .y_chroma_shift = 1,
132 [PIX_FMT_YUVJ422P] = {
135 .color_type = FF_COLOR_YUV_JPEG,
136 .pixel_type = FF_PIXEL_PLANAR,
138 .x_chroma_shift = 1, .y_chroma_shift = 0,
140 [PIX_FMT_YUVJ444P] = {
143 .color_type = FF_COLOR_YUV_JPEG,
144 .pixel_type = FF_PIXEL_PLANAR,
146 .x_chroma_shift = 0, .y_chroma_shift = 0,
153 .color_type = FF_COLOR_RGB,
154 .pixel_type = FF_PIXEL_PACKED,
156 .x_chroma_shift = 0, .y_chroma_shift = 0,
161 .color_type = FF_COLOR_RGB,
162 .pixel_type = FF_PIXEL_PACKED,
164 .x_chroma_shift = 0, .y_chroma_shift = 0,
168 .nb_channels = 4, .is_alpha = 1,
169 .color_type = FF_COLOR_RGB,
170 .pixel_type = FF_PIXEL_PACKED,
172 .x_chroma_shift = 0, .y_chroma_shift = 0,
177 .color_type = FF_COLOR_RGB,
178 .pixel_type = FF_PIXEL_PACKED,
180 .x_chroma_shift = 0, .y_chroma_shift = 0,
185 .color_type = FF_COLOR_RGB,
186 .pixel_type = FF_PIXEL_PACKED,
188 .x_chroma_shift = 0, .y_chroma_shift = 0,
191 /* gray / mono formats */
192 [PIX_FMT_GRAY16BE] = {
195 .color_type = FF_COLOR_GRAY,
196 .pixel_type = FF_PIXEL_PLANAR,
199 [PIX_FMT_GRAY16LE] = {
202 .color_type = FF_COLOR_GRAY,
203 .pixel_type = FF_PIXEL_PLANAR,
209 .color_type = FF_COLOR_GRAY,
210 .pixel_type = FF_PIXEL_PLANAR,
213 [PIX_FMT_MONOWHITE] = {
216 .color_type = FF_COLOR_GRAY,
217 .pixel_type = FF_PIXEL_PLANAR,
220 [PIX_FMT_MONOBLACK] = {
223 .color_type = FF_COLOR_GRAY,
224 .pixel_type = FF_PIXEL_PLANAR,
228 /* paletted formats */
231 .nb_channels = 4, .is_alpha = 1,
232 .color_type = FF_COLOR_RGB,
233 .pixel_type = FF_PIXEL_PALETTE,
236 [PIX_FMT_XVMC_MPEG2_MC] = {
239 [PIX_FMT_XVMC_MPEG2_IDCT] = {
242 [PIX_FMT_UYYVYY411] = {
245 .color_type = FF_COLOR_YUV,
246 .pixel_type = FF_PIXEL_PACKED,
248 .x_chroma_shift = 2, .y_chroma_shift = 0,
252 .nb_channels = 4, .is_alpha = 1,
253 .color_type = FF_COLOR_RGB,
254 .pixel_type = FF_PIXEL_PACKED,
256 .x_chroma_shift = 0, .y_chroma_shift = 0,
261 .color_type = FF_COLOR_RGB,
262 .pixel_type = FF_PIXEL_PACKED,
264 .x_chroma_shift = 0, .y_chroma_shift = 0,
269 .color_type = FF_COLOR_RGB,
270 .pixel_type = FF_PIXEL_PACKED,
272 .x_chroma_shift = 0, .y_chroma_shift = 0,
277 .color_type = FF_COLOR_RGB,
278 .pixel_type = FF_PIXEL_PACKED,
280 .x_chroma_shift = 0, .y_chroma_shift = 0,
285 .color_type = FF_COLOR_RGB,
286 .pixel_type = FF_PIXEL_PACKED,
288 .x_chroma_shift = 0, .y_chroma_shift = 0,
290 [PIX_FMT_RGB4_BYTE] = {
293 .color_type = FF_COLOR_RGB,
294 .pixel_type = FF_PIXEL_PACKED,
296 .x_chroma_shift = 0, .y_chroma_shift = 0,
301 .color_type = FF_COLOR_RGB,
302 .pixel_type = FF_PIXEL_PACKED,
304 .x_chroma_shift = 0, .y_chroma_shift = 0,
309 .color_type = FF_COLOR_RGB,
310 .pixel_type = FF_PIXEL_PACKED,
312 .x_chroma_shift = 0, .y_chroma_shift = 0,
314 [PIX_FMT_BGR4_BYTE] = {
317 .color_type = FF_COLOR_RGB,
318 .pixel_type = FF_PIXEL_PACKED,
320 .x_chroma_shift = 0, .y_chroma_shift = 0,
325 .color_type = FF_COLOR_YUV,
326 .pixel_type = FF_PIXEL_PLANAR,
328 .x_chroma_shift = 1, .y_chroma_shift = 1,
333 .color_type = FF_COLOR_YUV,
334 .pixel_type = FF_PIXEL_PLANAR,
336 .x_chroma_shift = 1, .y_chroma_shift = 1,
339 [PIX_FMT_BGR32_1] = {
341 .nb_channels = 4, .is_alpha = 1,
342 .color_type = FF_COLOR_RGB,
343 .pixel_type = FF_PIXEL_PACKED,
345 .x_chroma_shift = 0, .y_chroma_shift = 0,
347 [PIX_FMT_RGB32_1] = {
349 .nb_channels = 4, .is_alpha = 1,
350 .color_type = FF_COLOR_RGB,
351 .pixel_type = FF_PIXEL_PACKED,
353 .x_chroma_shift = 0, .y_chroma_shift = 0,
357 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
359 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
360 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
363 const char *avcodec_get_pix_fmt_name(int pix_fmt)
365 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
368 return pix_fmt_info[pix_fmt].name;
371 enum PixelFormat avcodec_get_pix_fmt(const char* name)
375 for (i=0; i < PIX_FMT_NB; i++)
376 if (!strcmp(pix_fmt_info[i].name, name))
381 void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
383 PixFmtInfo info= pix_fmt_info[pix_fmt];
385 char is_alpha_char= info.is_alpha ? 'y' : 'n';
389 snprintf (buf, buf_size,
390 "name " " nb_channels" " depth" " is_alpha"
393 snprintf (buf, buf_size,
394 "%-10s" " %1d " " %2d " " %c ",
402 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
403 int pix_fmt, int width, int height)
405 int size, w2, h2, size2;
406 const PixFmtInfo *pinfo;
408 if(avcodec_check_dimensions(NULL, width, height))
411 pinfo = &pix_fmt_info[pix_fmt];
412 size = width * height;
414 case PIX_FMT_YUV420P:
415 case PIX_FMT_YUV422P:
416 case PIX_FMT_YUV444P:
417 case PIX_FMT_YUV410P:
418 case PIX_FMT_YUV411P:
419 case PIX_FMT_YUVJ420P:
420 case PIX_FMT_YUVJ422P:
421 case PIX_FMT_YUVJ444P:
422 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
423 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
425 picture->data[0] = ptr;
426 picture->data[1] = picture->data[0] + size;
427 picture->data[2] = picture->data[1] + size2;
428 picture->linesize[0] = width;
429 picture->linesize[1] = w2;
430 picture->linesize[2] = w2;
431 return size + 2 * size2;
434 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
435 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
437 picture->data[0] = ptr;
438 picture->data[1] = picture->data[0] + size;
439 picture->data[2] = NULL;
440 picture->linesize[0] = width;
441 picture->linesize[1] = w2;
442 picture->linesize[2] = 0;
443 return size + 2 * size2;
446 picture->data[0] = ptr;
447 picture->data[1] = NULL;
448 picture->data[2] = NULL;
449 picture->linesize[0] = width * 3;
453 case PIX_FMT_RGB32_1:
454 case PIX_FMT_BGR32_1:
455 picture->data[0] = ptr;
456 picture->data[1] = NULL;
457 picture->data[2] = NULL;
458 picture->linesize[0] = width * 4;
460 case PIX_FMT_GRAY16BE:
461 case PIX_FMT_GRAY16LE:
466 case PIX_FMT_YUYV422:
467 picture->data[0] = ptr;
468 picture->data[1] = NULL;
469 picture->data[2] = NULL;
470 picture->linesize[0] = width * 2;
472 case PIX_FMT_UYVY422:
473 picture->data[0] = ptr;
474 picture->data[1] = NULL;
475 picture->data[2] = NULL;
476 picture->linesize[0] = width * 2;
478 case PIX_FMT_UYYVYY411:
479 picture->data[0] = ptr;
480 picture->data[1] = NULL;
481 picture->data[2] = NULL;
482 picture->linesize[0] = width + width/2;
483 return size + size/2;
486 case PIX_FMT_RGB4_BYTE:
487 case PIX_FMT_BGR4_BYTE:
489 picture->data[0] = ptr;
490 picture->data[1] = NULL;
491 picture->data[2] = NULL;
492 picture->linesize[0] = width;
496 picture->data[0] = ptr;
497 picture->data[1] = NULL;
498 picture->data[2] = NULL;
499 picture->linesize[0] = width / 2;
501 case PIX_FMT_MONOWHITE:
502 case PIX_FMT_MONOBLACK:
503 picture->data[0] = ptr;
504 picture->data[1] = NULL;
505 picture->data[2] = NULL;
506 picture->linesize[0] = (width + 7) >> 3;
507 return picture->linesize[0] * height;
509 size2 = (size + 3) & ~3;
510 picture->data[0] = ptr;
511 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
512 picture->data[2] = NULL;
513 picture->linesize[0] = width;
514 picture->linesize[1] = 4;
515 return size2 + 256 * 4;
518 picture->data[0] = NULL;
519 picture->data[1] = NULL;
520 picture->data[2] = NULL;
521 picture->data[3] = NULL;
526 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
527 unsigned char *dest, int dest_size)
529 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
530 int i, j, w, h, data_planes;
531 const unsigned char* s;
532 int size = avpicture_get_size(pix_fmt, width, height);
534 if (size > dest_size || size < 0)
537 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
538 if (pix_fmt == PIX_FMT_YUYV422 ||
539 pix_fmt == PIX_FMT_UYVY422 ||
540 pix_fmt == PIX_FMT_BGR565 ||
541 pix_fmt == PIX_FMT_BGR555 ||
542 pix_fmt == PIX_FMT_RGB565 ||
543 pix_fmt == PIX_FMT_RGB555)
545 else if (pix_fmt == PIX_FMT_UYYVYY411)
547 else if (pix_fmt == PIX_FMT_PAL8)
550 w = width * (pf->depth * pf->nb_channels / 8);
555 data_planes = pf->nb_channels;
556 w = (width*pf->depth + 7)/8;
560 for (i=0; i<data_planes; i++) {
562 w = width >> pf->x_chroma_shift;
563 h = height >> pf->y_chroma_shift;
569 s += src->linesize[i];
573 if (pf->pixel_type == FF_PIXEL_PALETTE)
574 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
579 int avpicture_get_size(int pix_fmt, int width, int height)
581 AVPicture dummy_pict;
582 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
585 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
588 const PixFmtInfo *pf, *ps;
591 ps = &pix_fmt_info[src_pix_fmt];
592 pf = &pix_fmt_info[dst_pix_fmt];
596 pf = &pix_fmt_info[dst_pix_fmt];
597 if (pf->depth < ps->depth ||
598 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
599 loss |= FF_LOSS_DEPTH;
600 if (pf->x_chroma_shift > ps->x_chroma_shift ||
601 pf->y_chroma_shift > ps->y_chroma_shift)
602 loss |= FF_LOSS_RESOLUTION;
603 switch(pf->color_type) {
605 if (ps->color_type != FF_COLOR_RGB &&
606 ps->color_type != FF_COLOR_GRAY)
607 loss |= FF_LOSS_COLORSPACE;
610 if (ps->color_type != FF_COLOR_GRAY)
611 loss |= FF_LOSS_COLORSPACE;
614 if (ps->color_type != FF_COLOR_YUV)
615 loss |= FF_LOSS_COLORSPACE;
617 case FF_COLOR_YUV_JPEG:
618 if (ps->color_type != FF_COLOR_YUV_JPEG &&
619 ps->color_type != FF_COLOR_YUV &&
620 ps->color_type != FF_COLOR_GRAY)
621 loss |= FF_LOSS_COLORSPACE;
625 if (ps->color_type != pf->color_type)
626 loss |= FF_LOSS_COLORSPACE;
629 if (pf->color_type == FF_COLOR_GRAY &&
630 ps->color_type != FF_COLOR_GRAY)
631 loss |= FF_LOSS_CHROMA;
632 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
633 loss |= FF_LOSS_ALPHA;
634 if (pf->pixel_type == FF_PIXEL_PALETTE &&
635 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
636 loss |= FF_LOSS_COLORQUANT;
640 static int avg_bits_per_pixel(int pix_fmt)
643 const PixFmtInfo *pf;
645 pf = &pix_fmt_info[pix_fmt];
646 switch(pf->pixel_type) {
647 case FF_PIXEL_PACKED:
649 case PIX_FMT_YUYV422:
650 case PIX_FMT_UYVY422:
657 case PIX_FMT_UYYVYY411:
661 bits = pf->depth * pf->nb_channels;
665 case FF_PIXEL_PLANAR:
666 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
667 bits = pf->depth * pf->nb_channels;
669 bits = pf->depth + ((2 * pf->depth) >>
670 (pf->x_chroma_shift + pf->y_chroma_shift));
673 case FF_PIXEL_PALETTE:
683 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
688 int dist, i, loss, min_dist, dst_pix_fmt;
690 /* find exact color match with smallest size */
692 min_dist = 0x7fffffff;
693 for(i = 0;i < PIX_FMT_NB; i++) {
694 if (pix_fmt_mask & (1 << i)) {
695 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
697 dist = avg_bits_per_pixel(i);
698 if (dist < min_dist) {
708 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
709 int has_alpha, int *loss_ptr)
711 int dst_pix_fmt, loss_mask, i;
712 static const int loss_mask_order[] = {
713 ~0, /* no loss first */
716 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
722 /* try with successive loss */
725 loss_mask = loss_mask_order[i++];
726 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
727 has_alpha, loss_mask);
728 if (dst_pix_fmt >= 0)
736 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
740 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
741 const uint8_t *src, int src_wrap,
742 int width, int height)
746 for(;height > 0; height--) {
747 memcpy(dst, src, width);
753 void av_picture_copy(AVPicture *dst, const AVPicture *src,
754 int pix_fmt, int width, int height)
757 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
759 pf = &pix_fmt_info[pix_fmt];
760 switch(pf->pixel_type) {
761 case FF_PIXEL_PACKED:
763 case PIX_FMT_YUYV422:
764 case PIX_FMT_UYVY422:
771 case PIX_FMT_UYYVYY411:
775 bits = pf->depth * pf->nb_channels;
778 bwidth = (width * bits + 7) >> 3;
779 ff_img_copy_plane(dst->data[0], dst->linesize[0],
780 src->data[0], src->linesize[0],
783 case FF_PIXEL_PLANAR:
784 for(i = 0; i < pf->nb_channels; i++) {
788 if (i == 1 || i == 2) {
789 w >>= pf->x_chroma_shift;
790 h >>= pf->y_chroma_shift;
792 bwidth = (w * pf->depth + 7) >> 3;
793 ff_img_copy_plane(dst->data[i], dst->linesize[i],
794 src->data[i], src->linesize[i],
798 case FF_PIXEL_PALETTE:
799 ff_img_copy_plane(dst->data[0], dst->linesize[0],
800 src->data[0], src->linesize[0],
802 /* copy the palette */
803 ff_img_copy_plane(dst->data[1], dst->linesize[1],
804 src->data[1], src->linesize[1],
810 /* XXX: totally non optimized */
812 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
813 int width, int height)
815 const uint8_t *p, *p1;
816 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
824 for(;height >= 1; height -= 2) {
829 for(w = width; w >= 2; w -= 2) {
846 p1 += src->linesize[0];
847 lum1 += dst->linesize[0];
851 for(w = width; w >= 2; w -= 2) {
860 p1 += src->linesize[0];
861 lum1 += dst->linesize[0];
863 cb1 += dst->linesize[1];
864 cr1 += dst->linesize[2];
868 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
869 int width, int height)
871 const uint8_t *p, *p1;
872 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
881 for(;height >= 1; height -= 2) {
886 for(w = width; w >= 2; w -= 2) {
903 p1 += src->linesize[0];
904 lum1 += dst->linesize[0];
908 for(w = width; w >= 2; w -= 2) {
917 p1 += src->linesize[0];
918 lum1 += dst->linesize[0];
920 cb1 += dst->linesize[1];
921 cr1 += dst->linesize[2];
926 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
927 int width, int height)
929 const uint8_t *p, *p1;
930 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
937 for(;height > 0; height--) {
942 for(w = width; w >= 2; w -= 2) {
952 p1 += src->linesize[0];
953 lum1 += dst->linesize[0];
954 cb1 += dst->linesize[1];
955 cr1 += dst->linesize[2];
960 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
961 int width, int height)
963 const uint8_t *p, *p1;
964 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
971 for(;height > 0; height--) {
976 for(w = width; w >= 2; w -= 2) {
986 p1 += src->linesize[0];
987 lum1 += dst->linesize[0];
988 cb1 += dst->linesize[1];
989 cr1 += dst->linesize[2];
993 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
994 int width, int height)
997 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1001 lum1 = src->data[0];
1004 for(;height > 0; height--) {
1009 for(w = width; w >= 2; w -= 2) {
1019 p1 += dst->linesize[0];
1020 lum1 += src->linesize[0];
1021 cb1 += src->linesize[1];
1022 cr1 += src->linesize[2];
1026 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1027 int width, int height)
1030 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1034 lum1 = src->data[0];
1037 for(;height > 0; height--) {
1042 for(w = width; w >= 2; w -= 2) {
1052 p1 += dst->linesize[0];
1053 lum1 += src->linesize[0];
1054 cb1 += src->linesize[1];
1055 cr1 += src->linesize[2];
1059 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1060 int width, int height)
1062 const uint8_t *p, *p1;
1063 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1067 lum1 = dst->data[0];
1070 for(;height > 0; height--) {
1075 for(w = width; w >= 4; w -= 4) {
1087 p1 += src->linesize[0];
1088 lum1 += dst->linesize[0];
1089 cb1 += dst->linesize[1];
1090 cr1 += dst->linesize[2];
1095 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1096 int width, int height)
1099 uint8_t *line1, *line2, *linesrc = dst->data[0];
1100 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1101 uint8_t *cb1, *cb2 = src->data[1];
1102 uint8_t *cr1, *cr2 = src->data[2];
1104 for(h = height / 2; h--;) {
1106 line2 = linesrc + dst->linesize[0];
1109 lum2 = lumsrc + src->linesize[0];
1114 for(w = width / 2; w--;) {
1115 *line1++ = *lum1++; *line2++ = *lum2++;
1116 *line1++ = *line2++ = *cb1++;
1117 *line1++ = *lum1++; *line2++ = *lum2++;
1118 *line1++ = *line2++ = *cr1++;
1121 linesrc += dst->linesize[0] * 2;
1122 lumsrc += src->linesize[0] * 2;
1123 cb2 += src->linesize[1];
1124 cr2 += src->linesize[2];
1128 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1129 int width, int height)
1132 uint8_t *line1, *line2, *linesrc = dst->data[0];
1133 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1134 uint8_t *cb1, *cb2 = src->data[1];
1135 uint8_t *cr1, *cr2 = src->data[2];
1137 for(h = height / 2; h--;) {
1139 line2 = linesrc + dst->linesize[0];
1142 lum2 = lumsrc + src->linesize[0];
1147 for(w = width / 2; w--;) {
1148 *line1++ = *line2++ = *cb1++;
1149 *line1++ = *lum1++; *line2++ = *lum2++;
1150 *line1++ = *line2++ = *cr1++;
1151 *line1++ = *lum1++; *line2++ = *lum2++;
1154 linesrc += dst->linesize[0] * 2;
1155 lumsrc += src->linesize[0] * 2;
1156 cb2 += src->linesize[1];
1157 cr2 += src->linesize[2];
1161 #define SCALEBITS 10
1162 #define ONE_HALF (1 << (SCALEBITS - 1))
1163 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1165 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1169 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1170 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1172 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1175 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1177 y = ((y1) - 16) * FIX(255.0/219.0);\
1178 r = cm[(y + r_add) >> SCALEBITS];\
1179 g = cm[(y + g_add) >> SCALEBITS];\
1180 b = cm[(y + b_add) >> SCALEBITS];\
1183 #define YUV_TO_RGB1(cb1, cr1)\
1187 r_add = FIX(1.40200) * cr + ONE_HALF;\
1188 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1189 b_add = FIX(1.77200) * cb + ONE_HALF;\
1192 #define YUV_TO_RGB2(r, g, b, y1)\
1194 y = (y1) << SCALEBITS;\
1195 r = cm[(y + r_add) >> SCALEBITS];\
1196 g = cm[(y + g_add) >> SCALEBITS];\
1197 b = cm[(y + b_add) >> SCALEBITS];\
1200 #define Y_CCIR_TO_JPEG(y)\
1201 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1203 #define Y_JPEG_TO_CCIR(y)\
1204 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1206 #define C_CCIR_TO_JPEG(y)\
1207 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1209 /* NOTE: the clamp is really necessary! */
1210 static inline int C_JPEG_TO_CCIR(int y) {
1211 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
1218 #define RGB_TO_Y(r, g, b) \
1219 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1220 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1222 #define RGB_TO_U(r1, g1, b1, shift)\
1223 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1224 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1226 #define RGB_TO_V(r1, g1, b1, shift)\
1227 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1228 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1230 #define RGB_TO_Y_CCIR(r, g, b) \
1231 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1232 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1234 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1235 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1236 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1238 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1239 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1240 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1242 static uint8_t y_ccir_to_jpeg[256];
1243 static uint8_t y_jpeg_to_ccir[256];
1244 static uint8_t c_ccir_to_jpeg[256];
1245 static uint8_t c_jpeg_to_ccir[256];
1247 /* init various conversion tables */
1248 static void img_convert_init(void)
1251 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1253 for(i = 0;i < 256; i++) {
1254 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1255 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1256 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1257 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1261 /* apply to each pixel the given table */
1262 static void img_apply_table(uint8_t *dst, int dst_wrap,
1263 const uint8_t *src, int src_wrap,
1264 int width, int height, const uint8_t *table1)
1269 const uint8_t *table;
1272 for(;height > 0; height--) {
1296 /* XXX: use generic filter ? */
1297 /* XXX: in most cases, the sampling position is incorrect */
1300 static void shrink41(uint8_t *dst, int dst_wrap,
1301 const uint8_t *src, int src_wrap,
1302 int width, int height)
1308 for(;height > 0; height--) {
1311 for(w = width;w > 0; w--) {
1312 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1322 static void shrink21(uint8_t *dst, int dst_wrap,
1323 const uint8_t *src, int src_wrap,
1324 int width, int height)
1330 for(;height > 0; height--) {
1333 for(w = width;w > 0; w--) {
1334 d[0] = (s[0] + s[1]) >> 1;
1344 static void shrink12(uint8_t *dst, int dst_wrap,
1345 const uint8_t *src, int src_wrap,
1346 int width, int height)
1350 const uint8_t *s1, *s2;
1352 for(;height > 0; height--) {
1356 for(w = width;w >= 4; w-=4) {
1357 d[0] = (s1[0] + s2[0]) >> 1;
1358 d[1] = (s1[1] + s2[1]) >> 1;
1359 d[2] = (s1[2] + s2[2]) >> 1;
1360 d[3] = (s1[3] + s2[3]) >> 1;
1366 d[0] = (s1[0] + s2[0]) >> 1;
1371 src += 2 * src_wrap;
1377 void ff_shrink22(uint8_t *dst, int dst_wrap,
1378 const uint8_t *src, int src_wrap,
1379 int width, int height)
1382 const uint8_t *s1, *s2;
1385 for(;height > 0; height--) {
1389 for(w = width;w >= 4; w-=4) {
1390 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1391 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1392 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1393 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1399 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1404 src += 2 * src_wrap;
1410 void ff_shrink44(uint8_t *dst, int dst_wrap,
1411 const uint8_t *src, int src_wrap,
1412 int width, int height)
1415 const uint8_t *s1, *s2, *s3, *s4;
1418 for(;height > 0; height--) {
1424 for(w = width;w > 0; w--) {
1425 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1426 s2[0] + s2[1] + s2[2] + s2[3] +
1427 s3[0] + s3[1] + s3[2] + s3[3] +
1428 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1435 src += 4 * src_wrap;
1441 void ff_shrink88(uint8_t *dst, int dst_wrap,
1442 const uint8_t *src, int src_wrap,
1443 int width, int height)
1447 for(;height > 0; height--) {
1448 for(w = width;w > 0; w--) {
1451 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1454 *(dst++) = (tmp + 32)>>6;
1455 src += 8 - 8*src_wrap;
1457 src += 8*src_wrap - 8*width;
1458 dst += dst_wrap - width;
1462 static void grow21_line(uint8_t *dst, const uint8_t *src,
1471 for(w = width;w >= 4; w-=4) {
1472 d[1] = d[0] = s1[0];
1473 d[3] = d[2] = s1[1];
1477 for(;w >= 2; w -= 2) {
1478 d[1] = d[0] = s1[0];
1482 /* only needed if width is not a multiple of two */
1483 /* XXX: veryfy that */
1489 static void grow41_line(uint8_t *dst, const uint8_t *src,
1498 for(w = width;w >= 4; w-=4) {
1510 static void grow21(uint8_t *dst, int dst_wrap,
1511 const uint8_t *src, int src_wrap,
1512 int width, int height)
1514 for(;height > 0; height--) {
1515 grow21_line(dst, src, width);
1522 static void grow22(uint8_t *dst, int dst_wrap,
1523 const uint8_t *src, int src_wrap,
1524 int width, int height)
1526 for(;height > 0; height--) {
1527 grow21_line(dst, src, width);
1535 static void grow41(uint8_t *dst, int dst_wrap,
1536 const uint8_t *src, int src_wrap,
1537 int width, int height)
1539 for(;height > 0; height--) {
1540 grow41_line(dst, src, width);
1547 static void grow44(uint8_t *dst, int dst_wrap,
1548 const uint8_t *src, int src_wrap,
1549 int width, int height)
1551 for(;height > 0; height--) {
1552 grow41_line(dst, src, width);
1553 if ((height & 3) == 1)
1560 static void conv411(uint8_t *dst, int dst_wrap,
1561 const uint8_t *src, int src_wrap,
1562 int width, int height)
1565 const uint8_t *s1, *s2;
1570 for(;height > 0; height--) {
1572 s2 = src + src_wrap;
1574 for(w = width;w > 0; w--) {
1575 c = (s1[0] + s2[0]) >> 1;
1582 src += src_wrap * 2;
1587 /* XXX: add jpeg quantize code */
1589 #define TRANSP_INDEX (6*6*6)
1591 /* this is maybe slow, but allows for extensions */
1592 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1594 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1597 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1600 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1603 pal = (uint32_t *)palette;
1605 for(r = 0; r < 6; r++) {
1606 for(g = 0; g < 6; g++) {
1607 for(b = 0; b < 6; b++) {
1608 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1609 (pal_value[g] << 8) | pal_value[b];
1616 pal[i++] = 0xff000000;
1619 /* copy bit n to bits 0 ... n - 1 */
1620 static inline unsigned int bitcopy_n(unsigned int a, int n)
1623 mask = (1 << n) - 1;
1624 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1627 /* rgb555 handling */
1629 #define RGB_NAME rgb555
1631 #define RGB_IN(r, g, b, s)\
1633 unsigned int v = ((const uint16_t *)(s))[0];\
1634 r = bitcopy_n(v >> (10 - 3), 3);\
1635 g = bitcopy_n(v >> (5 - 3), 3);\
1636 b = bitcopy_n(v << 3, 3);\
1640 #define RGB_OUT(d, r, g, b)\
1642 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1647 #include "imgconvert_template.h"
1649 /* rgb565 handling */
1651 #define RGB_NAME rgb565
1653 #define RGB_IN(r, g, b, s)\
1655 unsigned int v = ((const uint16_t *)(s))[0];\
1656 r = bitcopy_n(v >> (11 - 3), 3);\
1657 g = bitcopy_n(v >> (5 - 2), 2);\
1658 b = bitcopy_n(v << 3, 3);\
1661 #define RGB_OUT(d, r, g, b)\
1663 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1668 #include "imgconvert_template.h"
1670 /* bgr24 handling */
1672 #define RGB_NAME bgr24
1674 #define RGB_IN(r, g, b, s)\
1681 #define RGB_OUT(d, r, g, b)\
1690 #include "imgconvert_template.h"
1696 /* rgb24 handling */
1698 #define RGB_NAME rgb24
1701 #define RGB_IN(r, g, b, s)\
1708 #define RGB_OUT(d, r, g, b)\
1717 #include "imgconvert_template.h"
1719 /* rgb32 handling */
1721 #define RGB_NAME rgb32
1724 #define RGB_IN(r, g, b, s)\
1726 unsigned int v = ((const uint32_t *)(s))[0];\
1727 r = (v >> 16) & 0xff;\
1728 g = (v >> 8) & 0xff;\
1732 #define RGBA_IN(r, g, b, a, s)\
1734 unsigned int v = ((const uint32_t *)(s))[0];\
1735 a = (v >> 24) & 0xff;\
1736 r = (v >> 16) & 0xff;\
1737 g = (v >> 8) & 0xff;\
1741 #define RGBA_OUT(d, r, g, b, a)\
1743 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1748 #include "imgconvert_template.h"
1750 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1751 int width, int height, int xor_mask)
1753 const unsigned char *p;
1755 int v, dst_wrap, src_wrap;
1759 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1762 dst_wrap = dst->linesize[0] - width;
1763 for(y=0;y<height;y++) {
1766 v = *p++ ^ xor_mask;
1768 q[1] = -((v >> 6) & 1);
1769 q[2] = -((v >> 5) & 1);
1770 q[3] = -((v >> 4) & 1);
1771 q[4] = -((v >> 3) & 1);
1772 q[5] = -((v >> 2) & 1);
1773 q[6] = -((v >> 1) & 1);
1774 q[7] = -((v >> 0) & 1);
1779 v = *p++ ^ xor_mask;
1781 q[0] = -((v >> 7) & 1);
1791 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1792 int width, int height)
1794 mono_to_gray(dst, src, width, height, 0xff);
1797 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1798 int width, int height)
1800 mono_to_gray(dst, src, width, height, 0x00);
1803 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1804 int width, int height, int xor_mask)
1809 int j, b, v, n1, src_wrap, dst_wrap, y;
1812 src_wrap = src->linesize[0] - width;
1815 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1817 for(y=0;y<height;y++) {
1824 v = (v << 1) | (b >> 7);
1826 d[0] = v ^ xor_mask;
1836 v = (v << 1) | (b >> 7);
1839 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1847 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1848 int width, int height)
1850 gray_to_mono(dst, src, width, height, 0xff);
1853 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1854 int width, int height)
1856 gray_to_mono(dst, src, width, height, 0x00);
1859 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1860 int width, int height)
1862 int x, y, src_wrap, dst_wrap;
1865 src_wrap = src->linesize[0] - width;
1867 dst_wrap = dst->linesize[0] - width * 2;
1868 for(y=0; y<height; y++){
1869 for(x=0; x<width; x++){
1878 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1879 int width, int height)
1881 int x, y, src_wrap, dst_wrap;
1884 src_wrap = src->linesize[0] - width * 2;
1886 dst_wrap = dst->linesize[0] - width;
1887 for(y=0; y<height; y++){
1888 for(x=0; x<width; x++){
1897 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1898 int width, int height)
1900 gray16_to_gray(dst, src, width, height);
1903 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1904 int width, int height)
1906 AVPicture tmpsrc = *src;
1908 gray16_to_gray(dst, &tmpsrc, width, height);
1911 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1912 int width, int height)
1914 int x, y, src_wrap, dst_wrap;
1917 src_wrap = (src->linesize[0] - width * 2)/2;
1919 dst_wrap = (dst->linesize[0] - width * 2)/2;
1920 for(y=0; y<height; y++){
1921 for(x=0; x<width; x++){
1922 *d++ = bswap_16(*s++);
1930 typedef struct ConvertEntry {
1931 void (*convert)(AVPicture *dst,
1932 const AVPicture *src, int width, int height);
1935 /* Add each new convertion function in this table. In order to be able
1936 to convert from any format to any format, the following constraints
1939 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1941 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1943 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1945 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1948 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1950 The other conversion functions are just optimisations for common cases.
1952 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1953 [PIX_FMT_YUV420P] = {
1954 [PIX_FMT_YUYV422] = {
1955 .convert = yuv420p_to_yuyv422,
1957 [PIX_FMT_RGB555] = {
1958 .convert = yuv420p_to_rgb555
1960 [PIX_FMT_RGB565] = {
1961 .convert = yuv420p_to_rgb565
1964 .convert = yuv420p_to_bgr24
1967 .convert = yuv420p_to_rgb24
1970 .convert = yuv420p_to_rgb32
1972 [PIX_FMT_UYVY422] = {
1973 .convert = yuv420p_to_uyvy422,
1976 [PIX_FMT_YUV422P] = {
1977 [PIX_FMT_YUYV422] = {
1978 .convert = yuv422p_to_yuyv422,
1980 [PIX_FMT_UYVY422] = {
1981 .convert = yuv422p_to_uyvy422,
1984 [PIX_FMT_YUV444P] = {
1986 .convert = yuv444p_to_rgb24
1989 [PIX_FMT_YUVJ420P] = {
1990 [PIX_FMT_RGB555] = {
1991 .convert = yuvj420p_to_rgb555
1993 [PIX_FMT_RGB565] = {
1994 .convert = yuvj420p_to_rgb565
1997 .convert = yuvj420p_to_bgr24
2000 .convert = yuvj420p_to_rgb24
2003 .convert = yuvj420p_to_rgb32
2006 [PIX_FMT_YUVJ444P] = {
2008 .convert = yuvj444p_to_rgb24
2011 [PIX_FMT_YUYV422] = {
2012 [PIX_FMT_YUV420P] = {
2013 .convert = yuyv422_to_yuv420p,
2015 [PIX_FMT_YUV422P] = {
2016 .convert = yuyv422_to_yuv422p,
2019 [PIX_FMT_UYVY422] = {
2020 [PIX_FMT_YUV420P] = {
2021 .convert = uyvy422_to_yuv420p,
2023 [PIX_FMT_YUV422P] = {
2024 .convert = uyvy422_to_yuv422p,
2028 [PIX_FMT_YUV420P] = {
2029 .convert = rgb24_to_yuv420p
2031 [PIX_FMT_RGB565] = {
2032 .convert = rgb24_to_rgb565
2034 [PIX_FMT_RGB555] = {
2035 .convert = rgb24_to_rgb555
2038 .convert = rgb24_to_rgb32
2041 .convert = rgb24_to_bgr24
2044 .convert = rgb24_to_gray
2047 .convert = rgb24_to_pal8
2049 [PIX_FMT_YUV444P] = {
2050 .convert = rgb24_to_yuv444p
2052 [PIX_FMT_YUVJ420P] = {
2053 .convert = rgb24_to_yuvj420p
2055 [PIX_FMT_YUVJ444P] = {
2056 .convert = rgb24_to_yuvj444p
2061 .convert = rgb32_to_rgb24
2064 .convert = rgb32_to_bgr24
2066 [PIX_FMT_RGB565] = {
2067 .convert = rgb32_to_rgb565
2069 [PIX_FMT_RGB555] = {
2070 .convert = rgb32_to_rgb555
2073 .convert = rgb32_to_pal8
2075 [PIX_FMT_YUV420P] = {
2076 .convert = rgb32_to_yuv420p
2079 .convert = rgb32_to_gray
2084 .convert = bgr24_to_rgb32
2087 .convert = bgr24_to_rgb24
2089 [PIX_FMT_YUV420P] = {
2090 .convert = bgr24_to_yuv420p
2093 .convert = bgr24_to_gray
2096 [PIX_FMT_RGB555] = {
2098 .convert = rgb555_to_rgb24
2101 .convert = rgb555_to_rgb32
2103 [PIX_FMT_YUV420P] = {
2104 .convert = rgb555_to_yuv420p
2107 .convert = rgb555_to_gray
2110 [PIX_FMT_RGB565] = {
2112 .convert = rgb565_to_rgb32
2115 .convert = rgb565_to_rgb24
2117 [PIX_FMT_YUV420P] = {
2118 .convert = rgb565_to_yuv420p
2121 .convert = rgb565_to_gray
2124 [PIX_FMT_GRAY16BE] = {
2126 .convert = gray16be_to_gray
2128 [PIX_FMT_GRAY16LE] = {
2129 .convert = gray16_to_gray16
2132 [PIX_FMT_GRAY16LE] = {
2134 .convert = gray16le_to_gray
2136 [PIX_FMT_GRAY16BE] = {
2137 .convert = gray16_to_gray16
2141 [PIX_FMT_RGB555] = {
2142 .convert = gray_to_rgb555
2144 [PIX_FMT_RGB565] = {
2145 .convert = gray_to_rgb565
2148 .convert = gray_to_rgb24
2151 .convert = gray_to_bgr24
2154 .convert = gray_to_rgb32
2156 [PIX_FMT_MONOWHITE] = {
2157 .convert = gray_to_monowhite
2159 [PIX_FMT_MONOBLACK] = {
2160 .convert = gray_to_monoblack
2162 [PIX_FMT_GRAY16LE] = {
2163 .convert = gray_to_gray16
2165 [PIX_FMT_GRAY16BE] = {
2166 .convert = gray_to_gray16
2169 [PIX_FMT_MONOWHITE] = {
2171 .convert = monowhite_to_gray
2174 [PIX_FMT_MONOBLACK] = {
2176 .convert = monoblack_to_gray
2180 [PIX_FMT_RGB555] = {
2181 .convert = pal8_to_rgb555
2183 [PIX_FMT_RGB565] = {
2184 .convert = pal8_to_rgb565
2187 .convert = pal8_to_bgr24
2190 .convert = pal8_to_rgb24
2193 .convert = pal8_to_rgb32
2196 [PIX_FMT_UYYVYY411] = {
2197 [PIX_FMT_YUV411P] = {
2198 .convert = uyyvyy411_to_yuv411p,
2204 int avpicture_alloc(AVPicture *picture,
2205 int pix_fmt, int width, int height)
2210 size = avpicture_get_size(pix_fmt, width, height);
2213 ptr = av_malloc(size);
2216 avpicture_fill(picture, ptr, pix_fmt, width, height);
2219 memset(picture, 0, sizeof(AVPicture));
2223 void avpicture_free(AVPicture *picture)
2225 av_free(picture->data[0]);
2228 /* return true if yuv planar */
2229 static inline int is_yuv_planar(const PixFmtInfo *ps)
2231 return (ps->color_type == FF_COLOR_YUV ||
2232 ps->color_type == FF_COLOR_YUV_JPEG) &&
2233 ps->pixel_type == FF_PIXEL_PLANAR;
2236 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2237 int pix_fmt, int top_band, int left_band)
2242 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2245 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2246 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2248 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2249 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2250 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2252 dst->linesize[0] = src->linesize[0];
2253 dst->linesize[1] = src->linesize[1];
2254 dst->linesize[2] = src->linesize[2];
2258 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2259 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2268 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2269 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2271 for (i = 0; i < 3; i++) {
2272 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2273 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2275 if (padtop || padleft) {
2276 memset(dst->data[i], color[i],
2277 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2280 if (padleft || padright) {
2281 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2282 (dst->linesize[i] - (padright >> x_shift));
2283 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2284 for (y = 0; y < yheight; y++) {
2285 memset(optr, color[i], (padleft + padright) >> x_shift);
2286 optr += dst->linesize[i];
2290 if (src) { /* first line */
2291 uint8_t *iptr = src->data[i];
2292 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2293 (padleft >> x_shift);
2294 memcpy(optr, iptr, src->linesize[i]);
2295 iptr += src->linesize[i];
2296 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2297 (dst->linesize[i] - (padright >> x_shift));
2298 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2299 for (y = 0; y < yheight; y++) {
2300 memset(optr, color[i], (padleft + padright) >> x_shift);
2301 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2303 iptr += src->linesize[i];
2304 optr += dst->linesize[i];
2308 if (padbottom || padright) {
2309 optr = dst->data[i] + dst->linesize[i] *
2310 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2311 memset(optr, color[i],dst->linesize[i] *
2312 (padbottom >> y_shift) + (padright >> x_shift));
2318 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
2319 void img_copy(AVPicture *dst, const AVPicture *src,
2320 int pix_fmt, int width, int height)
2322 av_picture_copy(dst, src, pix_fmt, width, height);
2325 int img_crop(AVPicture *dst, const AVPicture *src,
2326 int pix_fmt, int top_band, int left_band)
2328 return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
2331 int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2332 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2335 return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
2339 #ifndef CONFIG_SWSCALER
2340 /* XXX: always use linesize. Return -1 if not supported */
2341 int img_convert(AVPicture *dst, int dst_pix_fmt,
2342 const AVPicture *src, int src_pix_fmt,
2343 int src_width, int src_height)
2346 int i, ret, dst_width, dst_height, int_pix_fmt;
2347 const PixFmtInfo *src_pix, *dst_pix;
2348 const ConvertEntry *ce;
2349 AVPicture tmp1, *tmp = &tmp1;
2351 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2352 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2354 if (src_width <= 0 || src_height <= 0)
2362 dst_width = src_width;
2363 dst_height = src_height;
2365 dst_pix = &pix_fmt_info[dst_pix_fmt];
2366 src_pix = &pix_fmt_info[src_pix_fmt];
2367 if (src_pix_fmt == dst_pix_fmt) {
2368 /* no conversion needed: just copy */
2369 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2373 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2375 /* specific conversion routine */
2376 ce->convert(dst, src, dst_width, dst_height);
2381 if (is_yuv_planar(dst_pix) &&
2382 src_pix_fmt == PIX_FMT_GRAY8) {
2386 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2387 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2388 src->data[0], src->linesize[0],
2389 dst_width, dst_height);
2391 img_apply_table(dst->data[0], dst->linesize[0],
2392 src->data[0], src->linesize[0],
2393 dst_width, dst_height,
2396 /* fill U and V with 128 */
2399 w >>= dst_pix->x_chroma_shift;
2400 h >>= dst_pix->y_chroma_shift;
2401 for(i = 1; i <= 2; i++) {
2403 for(y = 0; y< h; y++) {
2405 d += dst->linesize[i];
2412 if (is_yuv_planar(src_pix) &&
2413 dst_pix_fmt == PIX_FMT_GRAY8) {
2414 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2415 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2416 src->data[0], src->linesize[0],
2417 dst_width, dst_height);
2419 img_apply_table(dst->data[0], dst->linesize[0],
2420 src->data[0], src->linesize[0],
2421 dst_width, dst_height,
2427 /* YUV to YUV planar */
2428 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2429 int x_shift, y_shift, w, h, xy_shift;
2430 void (*resize_func)(uint8_t *dst, int dst_wrap,
2431 const uint8_t *src, int src_wrap,
2432 int width, int height);
2434 /* compute chroma size of the smallest dimensions */
2437 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2438 w >>= dst_pix->x_chroma_shift;
2440 w >>= src_pix->x_chroma_shift;
2441 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2442 h >>= dst_pix->y_chroma_shift;
2444 h >>= src_pix->y_chroma_shift;
2446 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2447 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2448 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2449 /* there must be filters for conversion at least from and to
2453 resize_func = ff_img_copy_plane;
2456 resize_func = shrink21;
2459 resize_func = shrink41;
2462 resize_func = shrink12;
2465 resize_func = ff_shrink22;
2468 resize_func = ff_shrink44;
2471 resize_func = grow21;
2474 resize_func = grow41;
2477 resize_func = grow22;
2480 resize_func = grow44;
2483 resize_func = conv411;
2486 /* currently not handled */
2487 goto no_chroma_filter;
2490 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2491 src->data[0], src->linesize[0],
2492 dst_width, dst_height);
2494 for(i = 1;i <= 2; i++)
2495 resize_func(dst->data[i], dst->linesize[i],
2496 src->data[i], src->linesize[i],
2497 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2498 /* if yuv color space conversion is needed, we do it here on
2499 the destination image */
2500 if (dst_pix->color_type != src_pix->color_type) {
2501 const uint8_t *y_table, *c_table;
2502 if (dst_pix->color_type == FF_COLOR_YUV) {
2503 y_table = y_jpeg_to_ccir;
2504 c_table = c_jpeg_to_ccir;
2506 y_table = y_ccir_to_jpeg;
2507 c_table = c_ccir_to_jpeg;
2509 img_apply_table(dst->data[0], dst->linesize[0],
2510 dst->data[0], dst->linesize[0],
2511 dst_width, dst_height,
2514 for(i = 1;i <= 2; i++)
2515 img_apply_table(dst->data[i], dst->linesize[i],
2516 dst->data[i], dst->linesize[i],
2517 dst_width>>dst_pix->x_chroma_shift,
2518 dst_height>>dst_pix->y_chroma_shift,
2525 /* try to use an intermediate format */
2526 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2527 dst_pix_fmt == PIX_FMT_YUYV422) {
2528 /* specific case: convert to YUV422P first */
2529 int_pix_fmt = PIX_FMT_YUV422P;
2530 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2531 dst_pix_fmt == PIX_FMT_UYVY422) {
2532 /* specific case: convert to YUV422P first */
2533 int_pix_fmt = PIX_FMT_YUV422P;
2534 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2535 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2536 /* specific case: convert to YUV411P first */
2537 int_pix_fmt = PIX_FMT_YUV411P;
2538 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2539 src_pix_fmt != PIX_FMT_GRAY8) ||
2540 (dst_pix->color_type == FF_COLOR_GRAY &&
2541 dst_pix_fmt != PIX_FMT_GRAY8)) {
2542 /* gray8 is the normalized format */
2543 int_pix_fmt = PIX_FMT_GRAY8;
2544 } else if ((is_yuv_planar(src_pix) &&
2545 src_pix_fmt != PIX_FMT_YUV444P &&
2546 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2547 /* yuv444 is the normalized format */
2548 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2549 int_pix_fmt = PIX_FMT_YUVJ444P;
2551 int_pix_fmt = PIX_FMT_YUV444P;
2552 } else if ((is_yuv_planar(dst_pix) &&
2553 dst_pix_fmt != PIX_FMT_YUV444P &&
2554 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2555 /* yuv444 is the normalized format */
2556 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2557 int_pix_fmt = PIX_FMT_YUVJ444P;
2559 int_pix_fmt = PIX_FMT_YUV444P;
2561 /* the two formats are rgb or gray8 or yuv[j]444p */
2562 if (src_pix->is_alpha && dst_pix->is_alpha)
2563 int_pix_fmt = PIX_FMT_RGB32;
2565 int_pix_fmt = PIX_FMT_RGB24;
2567 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2570 if (img_convert(tmp, int_pix_fmt,
2571 src, src_pix_fmt, src_width, src_height) < 0)
2573 if (img_convert(dst, dst_pix_fmt,
2574 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2578 avpicture_free(tmp);
2583 /* NOTE: we scan all the pixels to have an exact information */
2584 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2586 const unsigned char *p;
2587 int src_wrap, ret, x, y;
2589 uint32_t *palette = (uint32_t *)src->data[1];
2592 src_wrap = src->linesize[0] - width;
2594 for(y=0;y<height;y++) {
2595 for(x=0;x<width;x++) {
2596 a = palette[p[0]] >> 24;
2598 ret |= FF_ALPHA_TRANSP;
2599 } else if (a != 0xff) {
2600 ret |= FF_ALPHA_SEMI_TRANSP;
2609 int img_get_alpha_info(const AVPicture *src,
2610 int pix_fmt, int width, int height)
2612 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2615 pf = &pix_fmt_info[pix_fmt];
2616 /* no alpha can be represented in format */
2621 ret = get_alpha_info_rgb32(src, width, height);
2624 ret = get_alpha_info_pal8(src, width, height);
2627 /* we do not know, so everything is indicated */
2628 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2635 #define DEINT_INPLACE_LINE_LUM \
2636 movd_m2r(lum_m4[0],mm0);\
2637 movd_m2r(lum_m3[0],mm1);\
2638 movd_m2r(lum_m2[0],mm2);\
2639 movd_m2r(lum_m1[0],mm3);\
2640 movd_m2r(lum[0],mm4);\
2641 punpcklbw_r2r(mm7,mm0);\
2642 movd_r2m(mm2,lum_m4[0]);\
2643 punpcklbw_r2r(mm7,mm1);\
2644 punpcklbw_r2r(mm7,mm2);\
2645 punpcklbw_r2r(mm7,mm3);\
2646 punpcklbw_r2r(mm7,mm4);\
2647 paddw_r2r(mm3,mm1);\
2649 paddw_r2r(mm4,mm0);\
2651 paddw_r2r(mm6,mm2);\
2652 paddw_r2r(mm2,mm1);\
2653 psubusw_r2r(mm0,mm1);\
2655 packuswb_r2r(mm7,mm1);\
2656 movd_r2m(mm1,lum_m2[0]);
2658 #define DEINT_LINE_LUM \
2659 movd_m2r(lum_m4[0],mm0);\
2660 movd_m2r(lum_m3[0],mm1);\
2661 movd_m2r(lum_m2[0],mm2);\
2662 movd_m2r(lum_m1[0],mm3);\
2663 movd_m2r(lum[0],mm4);\
2664 punpcklbw_r2r(mm7,mm0);\
2665 punpcklbw_r2r(mm7,mm1);\
2666 punpcklbw_r2r(mm7,mm2);\
2667 punpcklbw_r2r(mm7,mm3);\
2668 punpcklbw_r2r(mm7,mm4);\
2669 paddw_r2r(mm3,mm1);\
2671 paddw_r2r(mm4,mm0);\
2673 paddw_r2r(mm6,mm2);\
2674 paddw_r2r(mm2,mm1);\
2675 psubusw_r2r(mm0,mm1);\
2677 packuswb_r2r(mm7,mm1);\
2678 movd_r2m(mm1,dst[0]);
2681 /* filter parameters: [-1 4 2 4 -1] // 8 */
2682 static void deinterlace_line(uint8_t *dst,
2683 const uint8_t *lum_m4, const uint8_t *lum_m3,
2684 const uint8_t *lum_m2, const uint8_t *lum_m1,
2689 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2692 for(;size > 0;size--) {
2694 sum += lum_m3[0] << 2;
2695 sum += lum_m2[0] << 1;
2696 sum += lum_m1[0] << 2;
2698 dst[0] = cm[(sum + 4) >> 3];
2715 movq_m2r(rounder,mm6);
2717 for (;size > 3; size-=4) {
2728 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2732 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2735 for(;size > 0;size--) {
2737 sum += lum_m3[0] << 2;
2738 sum += lum_m2[0] << 1;
2739 lum_m4[0]=lum_m2[0];
2740 sum += lum_m1[0] << 2;
2742 lum_m2[0] = cm[(sum + 4) >> 3];
2758 movq_m2r(rounder,mm6);
2760 for (;size > 3; size-=4) {
2761 DEINT_INPLACE_LINE_LUM
2771 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2772 top field is copied as is, but the bottom field is deinterlaced
2773 against the top field. */
2774 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2775 const uint8_t *src1, int src_wrap,
2776 int width, int height)
2778 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2783 src_0=&src_m1[src_wrap];
2784 src_p1=&src_0[src_wrap];
2785 src_p2=&src_p1[src_wrap];
2786 for(y=0;y<(height-2);y+=2) {
2787 memcpy(dst,src_m1,width);
2789 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2793 src_p1 += 2*src_wrap;
2794 src_p2 += 2*src_wrap;
2797 memcpy(dst,src_m1,width);
2800 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2803 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2804 int width, int height)
2806 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2809 buf = (uint8_t*)av_malloc(width);
2812 memcpy(buf,src_m1,width);
2813 src_0=&src_m1[src_wrap];
2814 src_p1=&src_0[src_wrap];
2815 src_p2=&src_p1[src_wrap];
2816 for(y=0;y<(height-2);y+=2) {
2817 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2820 src_p1 += 2*src_wrap;
2821 src_p2 += 2*src_wrap;
2824 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2828 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2829 int pix_fmt, int width, int height)
2833 if (pix_fmt != PIX_FMT_YUV420P &&
2834 pix_fmt != PIX_FMT_YUV422P &&
2835 pix_fmt != PIX_FMT_YUV444P &&
2836 pix_fmt != PIX_FMT_YUV411P)
2838 if ((width & 3) != 0 || (height & 3) != 0)
2844 case PIX_FMT_YUV420P:
2848 case PIX_FMT_YUV422P:
2851 case PIX_FMT_YUV411P:
2859 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2862 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2863 src->data[i], src->linesize[i],