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 avpicture_fill(AVPicture *picture, uint8_t *ptr,
430 int pix_fmt, int width, int height)
432 int size, w2, h2, size2;
433 const PixFmtInfo *pinfo;
435 if(avcodec_check_dimensions(NULL, width, height))
438 pinfo = &pix_fmt_info[pix_fmt];
439 size = width * height;
441 case PIX_FMT_YUV420P:
442 case PIX_FMT_YUV422P:
443 case PIX_FMT_YUV444P:
444 case PIX_FMT_YUV410P:
445 case PIX_FMT_YUV411P:
446 case PIX_FMT_YUV440P:
447 case PIX_FMT_YUVJ420P:
448 case PIX_FMT_YUVJ422P:
449 case PIX_FMT_YUVJ444P:
450 case PIX_FMT_YUVJ440P:
451 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
452 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
454 picture->data[0] = ptr;
455 picture->data[1] = picture->data[0] + size;
456 picture->data[2] = picture->data[1] + size2;
457 picture->data[3] = NULL;
458 picture->linesize[0] = width;
459 picture->linesize[1] = w2;
460 picture->linesize[2] = w2;
461 picture->linesize[3] = 0;
462 return size + 2 * size2;
463 case PIX_FMT_YUVA420P:
464 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
465 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
467 picture->data[0] = ptr;
468 picture->data[1] = picture->data[0] + size;
469 picture->data[2] = picture->data[1] + size2;
470 picture->data[3] = picture->data[1] + size2 + size2;
471 picture->linesize[0] = width;
472 picture->linesize[1] = w2;
473 picture->linesize[2] = w2;
474 picture->linesize[3] = width;
475 return 2 * size + 2 * size2;
478 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
479 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
481 picture->data[0] = ptr;
482 picture->data[1] = picture->data[0] + size;
483 picture->data[2] = NULL;
484 picture->data[3] = NULL;
485 picture->linesize[0] = width;
486 picture->linesize[1] = w2;
487 picture->linesize[2] = 0;
488 picture->linesize[3] = 0;
489 return size + 2 * size2;
492 picture->data[0] = ptr;
493 picture->data[1] = NULL;
494 picture->data[2] = NULL;
495 picture->data[3] = NULL;
496 picture->linesize[0] = width * 3;
500 case PIX_FMT_RGB32_1:
501 case PIX_FMT_BGR32_1:
502 picture->data[0] = ptr;
503 picture->data[1] = NULL;
504 picture->data[2] = NULL;
505 picture->data[3] = NULL;
506 picture->linesize[0] = width * 4;
508 case PIX_FMT_GRAY16BE:
509 case PIX_FMT_GRAY16LE:
514 case PIX_FMT_YUYV422:
515 picture->data[0] = ptr;
516 picture->data[1] = NULL;
517 picture->data[2] = NULL;
518 picture->data[3] = NULL;
519 picture->linesize[0] = width * 2;
521 case PIX_FMT_UYVY422:
522 picture->data[0] = ptr;
523 picture->data[1] = NULL;
524 picture->data[2] = NULL;
525 picture->data[3] = NULL;
526 picture->linesize[0] = width * 2;
528 case PIX_FMT_UYYVYY411:
529 picture->data[0] = ptr;
530 picture->data[1] = NULL;
531 picture->data[2] = NULL;
532 picture->data[3] = NULL;
533 picture->linesize[0] = width + width/2;
534 return size + size/2;
537 case PIX_FMT_RGB4_BYTE:
538 case PIX_FMT_BGR4_BYTE:
540 picture->data[0] = ptr;
541 picture->data[1] = NULL;
542 picture->data[2] = NULL;
543 picture->data[3] = NULL;
544 picture->linesize[0] = width;
548 picture->data[0] = ptr;
549 picture->data[1] = NULL;
550 picture->data[2] = NULL;
551 picture->data[3] = NULL;
552 picture->linesize[0] = width / 2;
554 case PIX_FMT_MONOWHITE:
555 case PIX_FMT_MONOBLACK:
556 picture->data[0] = ptr;
557 picture->data[1] = NULL;
558 picture->data[2] = NULL;
559 picture->data[3] = NULL;
560 picture->linesize[0] = (width + 7) >> 3;
561 return picture->linesize[0] * height;
563 size2 = (size + 3) & ~3;
564 picture->data[0] = ptr;
565 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
566 picture->data[2] = NULL;
567 picture->data[3] = NULL;
568 picture->linesize[0] = width;
569 picture->linesize[1] = 4;
570 return size2 + 256 * 4;
573 picture->data[0] = NULL;
574 picture->data[1] = NULL;
575 picture->data[2] = NULL;
576 picture->data[3] = NULL;
581 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
582 unsigned char *dest, int dest_size)
584 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
585 int i, j, w, h, data_planes;
586 const unsigned char* s;
587 int size = avpicture_get_size(pix_fmt, width, height);
589 if (size > dest_size || size < 0)
592 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
593 if (pix_fmt == PIX_FMT_YUYV422 ||
594 pix_fmt == PIX_FMT_UYVY422 ||
595 pix_fmt == PIX_FMT_BGR565 ||
596 pix_fmt == PIX_FMT_BGR555 ||
597 pix_fmt == PIX_FMT_RGB565 ||
598 pix_fmt == PIX_FMT_RGB555)
600 else if (pix_fmt == PIX_FMT_UYYVYY411)
602 else if (pix_fmt == PIX_FMT_PAL8)
605 w = width * (pf->depth * pf->nb_channels / 8);
610 data_planes = pf->nb_channels;
611 w = (width*pf->depth + 7)/8;
615 for (i=0; i<data_planes; i++) {
617 w = width >> pf->x_chroma_shift;
618 h = height >> pf->y_chroma_shift;
624 s += src->linesize[i];
628 if (pf->pixel_type == FF_PIXEL_PALETTE)
629 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
634 int avpicture_get_size(int pix_fmt, int width, int height)
636 AVPicture dummy_pict;
637 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
640 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
643 const PixFmtInfo *pf, *ps;
646 ps = &pix_fmt_info[src_pix_fmt];
647 pf = &pix_fmt_info[dst_pix_fmt];
651 pf = &pix_fmt_info[dst_pix_fmt];
652 if (pf->depth < ps->depth ||
653 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
654 loss |= FF_LOSS_DEPTH;
655 if (pf->x_chroma_shift > ps->x_chroma_shift ||
656 pf->y_chroma_shift > ps->y_chroma_shift)
657 loss |= FF_LOSS_RESOLUTION;
658 switch(pf->color_type) {
660 if (ps->color_type != FF_COLOR_RGB &&
661 ps->color_type != FF_COLOR_GRAY)
662 loss |= FF_LOSS_COLORSPACE;
665 if (ps->color_type != FF_COLOR_GRAY)
666 loss |= FF_LOSS_COLORSPACE;
669 if (ps->color_type != FF_COLOR_YUV)
670 loss |= FF_LOSS_COLORSPACE;
672 case FF_COLOR_YUV_JPEG:
673 if (ps->color_type != FF_COLOR_YUV_JPEG &&
674 ps->color_type != FF_COLOR_YUV &&
675 ps->color_type != FF_COLOR_GRAY)
676 loss |= FF_LOSS_COLORSPACE;
680 if (ps->color_type != pf->color_type)
681 loss |= FF_LOSS_COLORSPACE;
684 if (pf->color_type == FF_COLOR_GRAY &&
685 ps->color_type != FF_COLOR_GRAY)
686 loss |= FF_LOSS_CHROMA;
687 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
688 loss |= FF_LOSS_ALPHA;
689 if (pf->pixel_type == FF_PIXEL_PALETTE &&
690 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
691 loss |= FF_LOSS_COLORQUANT;
695 static int avg_bits_per_pixel(int pix_fmt)
698 const PixFmtInfo *pf;
700 pf = &pix_fmt_info[pix_fmt];
701 switch(pf->pixel_type) {
702 case FF_PIXEL_PACKED:
704 case PIX_FMT_YUYV422:
705 case PIX_FMT_UYVY422:
712 case PIX_FMT_UYYVYY411:
716 bits = pf->depth * pf->nb_channels;
720 case FF_PIXEL_PLANAR:
721 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
722 bits = pf->depth * pf->nb_channels;
724 bits = pf->depth + ((2 * pf->depth) >>
725 (pf->x_chroma_shift + pf->y_chroma_shift));
728 case FF_PIXEL_PALETTE:
738 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
743 int dist, i, loss, min_dist, dst_pix_fmt;
745 /* find exact color match with smallest size */
747 min_dist = 0x7fffffff;
748 for(i = 0;i < PIX_FMT_NB; i++) {
749 if (pix_fmt_mask & (1 << i)) {
750 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
752 dist = avg_bits_per_pixel(i);
753 if (dist < min_dist) {
763 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
764 int has_alpha, int *loss_ptr)
766 int dst_pix_fmt, loss_mask, i;
767 static const int loss_mask_order[] = {
768 ~0, /* no loss first */
771 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
777 /* try with successive loss */
780 loss_mask = loss_mask_order[i++];
781 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
782 has_alpha, loss_mask);
783 if (dst_pix_fmt >= 0)
791 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
795 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
796 const uint8_t *src, int src_wrap,
797 int width, int height)
801 for(;height > 0; height--) {
802 memcpy(dst, src, width);
808 void av_picture_copy(AVPicture *dst, const AVPicture *src,
809 int pix_fmt, int width, int height)
812 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
814 pf = &pix_fmt_info[pix_fmt];
815 switch(pf->pixel_type) {
816 case FF_PIXEL_PACKED:
818 case PIX_FMT_YUYV422:
819 case PIX_FMT_UYVY422:
826 case PIX_FMT_UYYVYY411:
830 bits = pf->depth * pf->nb_channels;
833 bwidth = (width * bits + 7) >> 3;
834 ff_img_copy_plane(dst->data[0], dst->linesize[0],
835 src->data[0], src->linesize[0],
838 case FF_PIXEL_PLANAR:
839 for(i = 0; i < pf->nb_channels; i++) {
843 if (i == 1 || i == 2) {
844 w >>= pf->x_chroma_shift;
845 h >>= pf->y_chroma_shift;
847 bwidth = (w * pf->depth + 7) >> 3;
848 ff_img_copy_plane(dst->data[i], dst->linesize[i],
849 src->data[i], src->linesize[i],
853 case FF_PIXEL_PALETTE:
854 ff_img_copy_plane(dst->data[0], dst->linesize[0],
855 src->data[0], src->linesize[0],
857 /* copy the palette */
858 ff_img_copy_plane(dst->data[1], dst->linesize[1],
859 src->data[1], src->linesize[1],
865 /* XXX: totally non optimized */
867 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
868 int width, int height)
870 const uint8_t *p, *p1;
871 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
879 for(;height >= 1; height -= 2) {
884 for(w = width; w >= 2; w -= 2) {
901 p1 += src->linesize[0];
902 lum1 += dst->linesize[0];
906 for(w = width; w >= 2; w -= 2) {
915 p1 += src->linesize[0];
916 lum1 += dst->linesize[0];
918 cb1 += dst->linesize[1];
919 cr1 += dst->linesize[2];
923 static void uyvy422_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;
936 for(;height >= 1; height -= 2) {
941 for(w = width; w >= 2; w -= 2) {
958 p1 += src->linesize[0];
959 lum1 += dst->linesize[0];
963 for(w = width; w >= 2; w -= 2) {
972 p1 += src->linesize[0];
973 lum1 += dst->linesize[0];
975 cb1 += dst->linesize[1];
976 cr1 += dst->linesize[2];
981 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
982 int width, int height)
984 const uint8_t *p, *p1;
985 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
992 for(;height > 0; height--) {
997 for(w = width; w >= 2; w -= 2) {
1007 p1 += src->linesize[0];
1008 lum1 += dst->linesize[0];
1009 cb1 += dst->linesize[1];
1010 cr1 += dst->linesize[2];
1015 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1016 int width, int height)
1018 const uint8_t *p, *p1;
1019 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1023 lum1 = dst->data[0];
1026 for(;height > 0; height--) {
1031 for(w = width; w >= 2; w -= 2) {
1041 p1 += src->linesize[0];
1042 lum1 += dst->linesize[0];
1043 cb1 += dst->linesize[1];
1044 cr1 += dst->linesize[2];
1048 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1049 int width, int height)
1052 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1056 lum1 = src->data[0];
1059 for(;height > 0; height--) {
1064 for(w = width; w >= 2; w -= 2) {
1074 p1 += dst->linesize[0];
1075 lum1 += src->linesize[0];
1076 cb1 += src->linesize[1];
1077 cr1 += src->linesize[2];
1081 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1082 int width, int height)
1085 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1089 lum1 = src->data[0];
1092 for(;height > 0; height--) {
1097 for(w = width; w >= 2; w -= 2) {
1107 p1 += dst->linesize[0];
1108 lum1 += src->linesize[0];
1109 cb1 += src->linesize[1];
1110 cr1 += src->linesize[2];
1114 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1115 int width, int height)
1117 const uint8_t *p, *p1;
1118 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1122 lum1 = dst->data[0];
1125 for(;height > 0; height--) {
1130 for(w = width; w >= 4; w -= 4) {
1142 p1 += src->linesize[0];
1143 lum1 += dst->linesize[0];
1144 cb1 += dst->linesize[1];
1145 cr1 += dst->linesize[2];
1150 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1151 int width, int height)
1154 uint8_t *line1, *line2, *linesrc = dst->data[0];
1155 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1156 uint8_t *cb1, *cb2 = src->data[1];
1157 uint8_t *cr1, *cr2 = src->data[2];
1159 for(h = height / 2; h--;) {
1161 line2 = linesrc + dst->linesize[0];
1164 lum2 = lumsrc + src->linesize[0];
1169 for(w = width / 2; w--;) {
1170 *line1++ = *lum1++; *line2++ = *lum2++;
1171 *line1++ = *line2++ = *cb1++;
1172 *line1++ = *lum1++; *line2++ = *lum2++;
1173 *line1++ = *line2++ = *cr1++;
1176 linesrc += dst->linesize[0] * 2;
1177 lumsrc += src->linesize[0] * 2;
1178 cb2 += src->linesize[1];
1179 cr2 += src->linesize[2];
1183 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1184 int width, int height)
1187 uint8_t *line1, *line2, *linesrc = dst->data[0];
1188 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1189 uint8_t *cb1, *cb2 = src->data[1];
1190 uint8_t *cr1, *cr2 = src->data[2];
1192 for(h = height / 2; h--;) {
1194 line2 = linesrc + dst->linesize[0];
1197 lum2 = lumsrc + src->linesize[0];
1202 for(w = width / 2; w--;) {
1203 *line1++ = *line2++ = *cb1++;
1204 *line1++ = *lum1++; *line2++ = *lum2++;
1205 *line1++ = *line2++ = *cr1++;
1206 *line1++ = *lum1++; *line2++ = *lum2++;
1209 linesrc += dst->linesize[0] * 2;
1210 lumsrc += src->linesize[0] * 2;
1211 cb2 += src->linesize[1];
1212 cr2 += src->linesize[2];
1216 static uint8_t y_ccir_to_jpeg[256];
1217 static uint8_t y_jpeg_to_ccir[256];
1218 static uint8_t c_ccir_to_jpeg[256];
1219 static uint8_t c_jpeg_to_ccir[256];
1221 /* init various conversion tables */
1222 static void img_convert_init(void)
1225 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1227 for(i = 0;i < 256; i++) {
1228 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1229 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1230 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1231 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1235 /* apply to each pixel the given table */
1236 static void img_apply_table(uint8_t *dst, int dst_wrap,
1237 const uint8_t *src, int src_wrap,
1238 int width, int height, const uint8_t *table1)
1243 const uint8_t *table;
1246 for(;height > 0; height--) {
1270 /* XXX: use generic filter ? */
1271 /* XXX: in most cases, the sampling position is incorrect */
1274 static void shrink41(uint8_t *dst, int dst_wrap,
1275 const uint8_t *src, int src_wrap,
1276 int width, int height)
1282 for(;height > 0; height--) {
1285 for(w = width;w > 0; w--) {
1286 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1296 static void shrink21(uint8_t *dst, int dst_wrap,
1297 const uint8_t *src, int src_wrap,
1298 int width, int height)
1304 for(;height > 0; height--) {
1307 for(w = width;w > 0; w--) {
1308 d[0] = (s[0] + s[1]) >> 1;
1318 static void shrink12(uint8_t *dst, int dst_wrap,
1319 const uint8_t *src, int src_wrap,
1320 int width, int height)
1324 const uint8_t *s1, *s2;
1326 for(;height > 0; height--) {
1330 for(w = width;w >= 4; w-=4) {
1331 d[0] = (s1[0] + s2[0]) >> 1;
1332 d[1] = (s1[1] + s2[1]) >> 1;
1333 d[2] = (s1[2] + s2[2]) >> 1;
1334 d[3] = (s1[3] + s2[3]) >> 1;
1340 d[0] = (s1[0] + s2[0]) >> 1;
1345 src += 2 * src_wrap;
1351 void ff_shrink22(uint8_t *dst, int dst_wrap,
1352 const uint8_t *src, int src_wrap,
1353 int width, int height)
1356 const uint8_t *s1, *s2;
1359 for(;height > 0; height--) {
1363 for(w = width;w >= 4; w-=4) {
1364 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1365 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1366 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1367 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1373 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1378 src += 2 * src_wrap;
1384 void ff_shrink44(uint8_t *dst, int dst_wrap,
1385 const uint8_t *src, int src_wrap,
1386 int width, int height)
1389 const uint8_t *s1, *s2, *s3, *s4;
1392 for(;height > 0; height--) {
1398 for(w = width;w > 0; w--) {
1399 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1400 s2[0] + s2[1] + s2[2] + s2[3] +
1401 s3[0] + s3[1] + s3[2] + s3[3] +
1402 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1409 src += 4 * src_wrap;
1415 void ff_shrink88(uint8_t *dst, int dst_wrap,
1416 const uint8_t *src, int src_wrap,
1417 int width, int height)
1421 for(;height > 0; height--) {
1422 for(w = width;w > 0; w--) {
1425 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1428 *(dst++) = (tmp + 32)>>6;
1429 src += 8 - 8*src_wrap;
1431 src += 8*src_wrap - 8*width;
1432 dst += dst_wrap - width;
1436 static void grow21_line(uint8_t *dst, const uint8_t *src,
1445 for(w = width;w >= 4; w-=4) {
1446 d[1] = d[0] = s1[0];
1447 d[3] = d[2] = s1[1];
1451 for(;w >= 2; w -= 2) {
1452 d[1] = d[0] = s1[0];
1456 /* only needed if width is not a multiple of two */
1457 /* XXX: veryfy that */
1463 static void grow41_line(uint8_t *dst, const uint8_t *src,
1472 for(w = width;w >= 4; w-=4) {
1484 static void grow21(uint8_t *dst, int dst_wrap,
1485 const uint8_t *src, int src_wrap,
1486 int width, int height)
1488 for(;height > 0; height--) {
1489 grow21_line(dst, src, width);
1496 static void grow12(uint8_t *dst, int dst_wrap,
1497 const uint8_t *src, int src_wrap,
1498 int width, int height)
1500 for(;height > 0; height-=2) {
1501 memcpy(dst, src, width);
1503 memcpy(dst, src, width);
1510 static void grow22(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);
1523 static void grow41(uint8_t *dst, int dst_wrap,
1524 const uint8_t *src, int src_wrap,
1525 int width, int height)
1527 for(;height > 0; height--) {
1528 grow41_line(dst, src, width);
1535 static void grow44(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);
1541 if ((height & 3) == 1)
1548 static void conv411(uint8_t *dst, int dst_wrap,
1549 const uint8_t *src, int src_wrap,
1550 int width, int height)
1553 const uint8_t *s1, *s2;
1558 for(;height > 0; height--) {
1560 s2 = src + src_wrap;
1562 for(w = width;w > 0; w--) {
1563 c = (s1[0] + s2[0]) >> 1;
1570 src += src_wrap * 2;
1575 /* XXX: add jpeg quantize code */
1577 #define TRANSP_INDEX (6*6*6)
1579 /* this is maybe slow, but allows for extensions */
1580 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1582 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1585 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1588 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1591 pal = (uint32_t *)palette;
1593 for(r = 0; r < 6; r++) {
1594 for(g = 0; g < 6; g++) {
1595 for(b = 0; b < 6; b++) {
1596 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1597 (pal_value[g] << 8) | pal_value[b];
1604 pal[i++] = 0xff000000;
1607 /* copy bit n to bits 0 ... n - 1 */
1608 static inline unsigned int bitcopy_n(unsigned int a, int n)
1611 mask = (1 << n) - 1;
1612 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1615 /* rgb555 handling */
1617 #define RGB_NAME rgb555
1619 #define RGB_IN(r, g, b, s)\
1621 unsigned int v = ((const uint16_t *)(s))[0];\
1622 r = bitcopy_n(v >> (10 - 3), 3);\
1623 g = bitcopy_n(v >> (5 - 3), 3);\
1624 b = bitcopy_n(v << 3, 3);\
1628 #define RGB_OUT(d, r, g, b)\
1630 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1635 #include "imgconvert_template.h"
1637 /* rgb565 handling */
1639 #define RGB_NAME rgb565
1641 #define RGB_IN(r, g, b, s)\
1643 unsigned int v = ((const uint16_t *)(s))[0];\
1644 r = bitcopy_n(v >> (11 - 3), 3);\
1645 g = bitcopy_n(v >> (5 - 2), 2);\
1646 b = bitcopy_n(v << 3, 3);\
1649 #define RGB_OUT(d, r, g, b)\
1651 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1656 #include "imgconvert_template.h"
1658 /* bgr24 handling */
1660 #define RGB_NAME bgr24
1662 #define RGB_IN(r, g, b, s)\
1669 #define RGB_OUT(d, r, g, b)\
1678 #include "imgconvert_template.h"
1684 /* rgb24 handling */
1686 #define RGB_NAME rgb24
1689 #define RGB_IN(r, g, b, s)\
1696 #define RGB_OUT(d, r, g, b)\
1705 #include "imgconvert_template.h"
1707 /* rgb32 handling */
1709 #define RGB_NAME rgb32
1712 #define RGB_IN(r, g, b, s)\
1714 unsigned int v = ((const uint32_t *)(s))[0];\
1715 r = (v >> 16) & 0xff;\
1716 g = (v >> 8) & 0xff;\
1720 #define RGBA_IN(r, g, b, a, s)\
1722 unsigned int v = ((const uint32_t *)(s))[0];\
1723 a = (v >> 24) & 0xff;\
1724 r = (v >> 16) & 0xff;\
1725 g = (v >> 8) & 0xff;\
1729 #define RGBA_OUT(d, r, g, b, a)\
1731 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1736 #include "imgconvert_template.h"
1738 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1739 int width, int height, int xor_mask)
1741 const unsigned char *p;
1743 int v, dst_wrap, src_wrap;
1747 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1750 dst_wrap = dst->linesize[0] - width;
1751 for(y=0;y<height;y++) {
1754 v = *p++ ^ xor_mask;
1756 q[1] = -((v >> 6) & 1);
1757 q[2] = -((v >> 5) & 1);
1758 q[3] = -((v >> 4) & 1);
1759 q[4] = -((v >> 3) & 1);
1760 q[5] = -((v >> 2) & 1);
1761 q[6] = -((v >> 1) & 1);
1762 q[7] = -((v >> 0) & 1);
1767 v = *p++ ^ xor_mask;
1769 q[0] = -((v >> 7) & 1);
1779 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1780 int width, int height)
1782 mono_to_gray(dst, src, width, height, 0xff);
1785 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1786 int width, int height)
1788 mono_to_gray(dst, src, width, height, 0x00);
1791 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1792 int width, int height, int xor_mask)
1797 int j, b, v, n1, src_wrap, dst_wrap, y;
1800 src_wrap = src->linesize[0] - width;
1803 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1805 for(y=0;y<height;y++) {
1812 v = (v << 1) | (b >> 7);
1814 d[0] = v ^ xor_mask;
1824 v = (v << 1) | (b >> 7);
1827 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1835 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1836 int width, int height)
1838 gray_to_mono(dst, src, width, height, 0xff);
1841 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1842 int width, int height)
1844 gray_to_mono(dst, src, width, height, 0x00);
1847 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1848 int width, int height)
1850 int x, y, src_wrap, dst_wrap;
1853 src_wrap = src->linesize[0] - width;
1855 dst_wrap = dst->linesize[0] - width * 2;
1856 for(y=0; y<height; y++){
1857 for(x=0; x<width; x++){
1866 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1867 int width, int height)
1869 int x, y, src_wrap, dst_wrap;
1872 src_wrap = src->linesize[0] - width * 2;
1874 dst_wrap = dst->linesize[0] - width;
1875 for(y=0; y<height; y++){
1876 for(x=0; x<width; x++){
1885 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1886 int width, int height)
1888 gray16_to_gray(dst, src, width, height);
1891 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1892 int width, int height)
1894 AVPicture tmpsrc = *src;
1896 gray16_to_gray(dst, &tmpsrc, width, height);
1899 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1900 int width, int height)
1902 int x, y, src_wrap, dst_wrap;
1905 src_wrap = (src->linesize[0] - width * 2)/2;
1907 dst_wrap = (dst->linesize[0] - width * 2)/2;
1908 for(y=0; y<height; y++){
1909 for(x=0; x<width; x++){
1910 *d++ = bswap_16(*s++);
1918 typedef struct ConvertEntry {
1919 void (*convert)(AVPicture *dst,
1920 const AVPicture *src, int width, int height);
1923 /* Add each new conversion function in this table. In order to be able
1924 to convert from any format to any format, the following constraints
1927 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1929 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1931 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1933 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1936 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1938 The other conversion functions are just optimisations for common cases.
1940 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1941 [PIX_FMT_YUV420P] = {
1942 [PIX_FMT_YUYV422] = {
1943 .convert = yuv420p_to_yuyv422,
1945 [PIX_FMT_RGB555] = {
1946 .convert = yuv420p_to_rgb555
1948 [PIX_FMT_RGB565] = {
1949 .convert = yuv420p_to_rgb565
1952 .convert = yuv420p_to_bgr24
1955 .convert = yuv420p_to_rgb24
1958 .convert = yuv420p_to_rgb32
1960 [PIX_FMT_UYVY422] = {
1961 .convert = yuv420p_to_uyvy422,
1964 [PIX_FMT_YUV422P] = {
1965 [PIX_FMT_YUYV422] = {
1966 .convert = yuv422p_to_yuyv422,
1968 [PIX_FMT_UYVY422] = {
1969 .convert = yuv422p_to_uyvy422,
1972 [PIX_FMT_YUV444P] = {
1974 .convert = yuv444p_to_rgb24
1977 [PIX_FMT_YUVJ420P] = {
1978 [PIX_FMT_RGB555] = {
1979 .convert = yuvj420p_to_rgb555
1981 [PIX_FMT_RGB565] = {
1982 .convert = yuvj420p_to_rgb565
1985 .convert = yuvj420p_to_bgr24
1988 .convert = yuvj420p_to_rgb24
1991 .convert = yuvj420p_to_rgb32
1994 [PIX_FMT_YUVJ444P] = {
1996 .convert = yuvj444p_to_rgb24
1999 [PIX_FMT_YUYV422] = {
2000 [PIX_FMT_YUV420P] = {
2001 .convert = yuyv422_to_yuv420p,
2003 [PIX_FMT_YUV422P] = {
2004 .convert = yuyv422_to_yuv422p,
2007 [PIX_FMT_UYVY422] = {
2008 [PIX_FMT_YUV420P] = {
2009 .convert = uyvy422_to_yuv420p,
2011 [PIX_FMT_YUV422P] = {
2012 .convert = uyvy422_to_yuv422p,
2016 [PIX_FMT_YUV420P] = {
2017 .convert = rgb24_to_yuv420p
2019 [PIX_FMT_RGB565] = {
2020 .convert = rgb24_to_rgb565
2022 [PIX_FMT_RGB555] = {
2023 .convert = rgb24_to_rgb555
2026 .convert = rgb24_to_rgb32
2029 .convert = rgb24_to_bgr24
2032 .convert = rgb24_to_gray
2035 .convert = rgb24_to_pal8
2037 [PIX_FMT_YUV444P] = {
2038 .convert = rgb24_to_yuv444p
2040 [PIX_FMT_YUVJ420P] = {
2041 .convert = rgb24_to_yuvj420p
2043 [PIX_FMT_YUVJ444P] = {
2044 .convert = rgb24_to_yuvj444p
2049 .convert = rgb32_to_rgb24
2052 .convert = rgb32_to_bgr24
2054 [PIX_FMT_RGB565] = {
2055 .convert = rgb32_to_rgb565
2057 [PIX_FMT_RGB555] = {
2058 .convert = rgb32_to_rgb555
2061 .convert = rgb32_to_pal8
2063 [PIX_FMT_YUV420P] = {
2064 .convert = rgb32_to_yuv420p
2067 .convert = rgb32_to_gray
2072 .convert = bgr24_to_rgb32
2075 .convert = bgr24_to_rgb24
2077 [PIX_FMT_YUV420P] = {
2078 .convert = bgr24_to_yuv420p
2081 .convert = bgr24_to_gray
2084 [PIX_FMT_RGB555] = {
2086 .convert = rgb555_to_rgb24
2089 .convert = rgb555_to_rgb32
2091 [PIX_FMT_YUV420P] = {
2092 .convert = rgb555_to_yuv420p
2095 .convert = rgb555_to_gray
2098 [PIX_FMT_RGB565] = {
2100 .convert = rgb565_to_rgb32
2103 .convert = rgb565_to_rgb24
2105 [PIX_FMT_YUV420P] = {
2106 .convert = rgb565_to_yuv420p
2109 .convert = rgb565_to_gray
2112 [PIX_FMT_GRAY16BE] = {
2114 .convert = gray16be_to_gray
2116 [PIX_FMT_GRAY16LE] = {
2117 .convert = gray16_to_gray16
2120 [PIX_FMT_GRAY16LE] = {
2122 .convert = gray16le_to_gray
2124 [PIX_FMT_GRAY16BE] = {
2125 .convert = gray16_to_gray16
2129 [PIX_FMT_RGB555] = {
2130 .convert = gray_to_rgb555
2132 [PIX_FMT_RGB565] = {
2133 .convert = gray_to_rgb565
2136 .convert = gray_to_rgb24
2139 .convert = gray_to_bgr24
2142 .convert = gray_to_rgb32
2144 [PIX_FMT_MONOWHITE] = {
2145 .convert = gray_to_monowhite
2147 [PIX_FMT_MONOBLACK] = {
2148 .convert = gray_to_monoblack
2150 [PIX_FMT_GRAY16LE] = {
2151 .convert = gray_to_gray16
2153 [PIX_FMT_GRAY16BE] = {
2154 .convert = gray_to_gray16
2157 [PIX_FMT_MONOWHITE] = {
2159 .convert = monowhite_to_gray
2162 [PIX_FMT_MONOBLACK] = {
2164 .convert = monoblack_to_gray
2168 [PIX_FMT_RGB555] = {
2169 .convert = pal8_to_rgb555
2171 [PIX_FMT_RGB565] = {
2172 .convert = pal8_to_rgb565
2175 .convert = pal8_to_bgr24
2178 .convert = pal8_to_rgb24
2181 .convert = pal8_to_rgb32
2184 [PIX_FMT_UYYVYY411] = {
2185 [PIX_FMT_YUV411P] = {
2186 .convert = uyyvyy411_to_yuv411p,
2192 int avpicture_alloc(AVPicture *picture,
2193 int pix_fmt, int width, int height)
2198 size = avpicture_get_size(pix_fmt, width, height);
2201 ptr = av_malloc(size);
2204 avpicture_fill(picture, ptr, pix_fmt, width, height);
2207 memset(picture, 0, sizeof(AVPicture));
2211 void avpicture_free(AVPicture *picture)
2213 av_free(picture->data[0]);
2216 /* return true if yuv planar */
2217 static inline int is_yuv_planar(const PixFmtInfo *ps)
2219 return (ps->color_type == FF_COLOR_YUV ||
2220 ps->color_type == FF_COLOR_YUV_JPEG) &&
2221 ps->pixel_type == FF_PIXEL_PLANAR;
2224 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2225 int pix_fmt, int top_band, int left_band)
2230 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2233 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2234 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2236 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2237 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2238 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2240 dst->linesize[0] = src->linesize[0];
2241 dst->linesize[1] = src->linesize[1];
2242 dst->linesize[2] = src->linesize[2];
2246 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2247 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2256 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2257 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2259 for (i = 0; i < 3; i++) {
2260 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2261 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2263 if (padtop || padleft) {
2264 memset(dst->data[i], color[i],
2265 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2268 if (padleft || padright) {
2269 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2270 (dst->linesize[i] - (padright >> x_shift));
2271 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2272 for (y = 0; y < yheight; y++) {
2273 memset(optr, color[i], (padleft + padright) >> x_shift);
2274 optr += dst->linesize[i];
2278 if (src) { /* first line */
2279 uint8_t *iptr = src->data[i];
2280 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2281 (padleft >> x_shift);
2282 memcpy(optr, iptr, src->linesize[i]);
2283 iptr += src->linesize[i];
2284 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2285 (dst->linesize[i] - (padright >> x_shift));
2286 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2287 for (y = 0; y < yheight; y++) {
2288 memset(optr, color[i], (padleft + padright) >> x_shift);
2289 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2291 iptr += src->linesize[i];
2292 optr += dst->linesize[i];
2296 if (padbottom || padright) {
2297 optr = dst->data[i] + dst->linesize[i] *
2298 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2299 memset(optr, color[i],dst->linesize[i] *
2300 (padbottom >> y_shift) + (padright >> x_shift));
2306 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
2307 void img_copy(AVPicture *dst, const AVPicture *src,
2308 int pix_fmt, int width, int height)
2310 av_picture_copy(dst, src, pix_fmt, width, height);
2313 int img_crop(AVPicture *dst, const AVPicture *src,
2314 int pix_fmt, int top_band, int left_band)
2316 return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
2319 int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2320 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2323 return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
2327 #ifndef CONFIG_SWSCALER
2328 /* XXX: always use linesize. Return -1 if not supported */
2329 int img_convert(AVPicture *dst, int dst_pix_fmt,
2330 const AVPicture *src, int src_pix_fmt,
2331 int src_width, int src_height)
2334 int i, ret, dst_width, dst_height, int_pix_fmt;
2335 const PixFmtInfo *src_pix, *dst_pix;
2336 const ConvertEntry *ce;
2337 AVPicture tmp1, *tmp = &tmp1;
2339 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2340 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2342 if (src_width <= 0 || src_height <= 0)
2350 dst_width = src_width;
2351 dst_height = src_height;
2353 dst_pix = &pix_fmt_info[dst_pix_fmt];
2354 src_pix = &pix_fmt_info[src_pix_fmt];
2355 if (src_pix_fmt == dst_pix_fmt) {
2356 /* no conversion needed: just copy */
2357 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2361 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2363 /* specific conversion routine */
2364 ce->convert(dst, src, dst_width, dst_height);
2369 if (is_yuv_planar(dst_pix) &&
2370 src_pix_fmt == PIX_FMT_GRAY8) {
2374 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2375 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2376 src->data[0], src->linesize[0],
2377 dst_width, dst_height);
2379 img_apply_table(dst->data[0], dst->linesize[0],
2380 src->data[0], src->linesize[0],
2381 dst_width, dst_height,
2384 /* fill U and V with 128 */
2387 w >>= dst_pix->x_chroma_shift;
2388 h >>= dst_pix->y_chroma_shift;
2389 for(i = 1; i <= 2; i++) {
2391 for(y = 0; y< h; y++) {
2393 d += dst->linesize[i];
2400 if (is_yuv_planar(src_pix) &&
2401 dst_pix_fmt == PIX_FMT_GRAY8) {
2402 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2403 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2404 src->data[0], src->linesize[0],
2405 dst_width, dst_height);
2407 img_apply_table(dst->data[0], dst->linesize[0],
2408 src->data[0], src->linesize[0],
2409 dst_width, dst_height,
2415 /* YUV to YUV planar */
2416 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2417 int x_shift, y_shift, w, h, xy_shift;
2418 void (*resize_func)(uint8_t *dst, int dst_wrap,
2419 const uint8_t *src, int src_wrap,
2420 int width, int height);
2422 /* compute chroma size of the smallest dimensions */
2425 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2426 w >>= dst_pix->x_chroma_shift;
2428 w >>= src_pix->x_chroma_shift;
2429 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2430 h >>= dst_pix->y_chroma_shift;
2432 h >>= src_pix->y_chroma_shift;
2434 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2435 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2436 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2437 /* there must be filters for conversion at least from and to
2441 resize_func = ff_img_copy_plane;
2444 resize_func = shrink21;
2447 resize_func = shrink41;
2450 resize_func = shrink12;
2453 resize_func = ff_shrink22;
2456 resize_func = ff_shrink44;
2459 resize_func = grow21;
2462 resize_func = grow12;
2465 resize_func = grow41;
2468 resize_func = grow22;
2471 resize_func = grow44;
2474 resize_func = conv411;
2477 /* currently not handled */
2478 goto no_chroma_filter;
2481 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2482 src->data[0], src->linesize[0],
2483 dst_width, dst_height);
2485 for(i = 1;i <= 2; i++)
2486 resize_func(dst->data[i], dst->linesize[i],
2487 src->data[i], src->linesize[i],
2488 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2489 /* if yuv color space conversion is needed, we do it here on
2490 the destination image */
2491 if (dst_pix->color_type != src_pix->color_type) {
2492 const uint8_t *y_table, *c_table;
2493 if (dst_pix->color_type == FF_COLOR_YUV) {
2494 y_table = y_jpeg_to_ccir;
2495 c_table = c_jpeg_to_ccir;
2497 y_table = y_ccir_to_jpeg;
2498 c_table = c_ccir_to_jpeg;
2500 img_apply_table(dst->data[0], dst->linesize[0],
2501 dst->data[0], dst->linesize[0],
2502 dst_width, dst_height,
2505 for(i = 1;i <= 2; i++)
2506 img_apply_table(dst->data[i], dst->linesize[i],
2507 dst->data[i], dst->linesize[i],
2508 dst_width>>dst_pix->x_chroma_shift,
2509 dst_height>>dst_pix->y_chroma_shift,
2516 /* try to use an intermediate format */
2517 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2518 dst_pix_fmt == PIX_FMT_YUYV422) {
2519 /* specific case: convert to YUV422P first */
2520 int_pix_fmt = PIX_FMT_YUV422P;
2521 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2522 dst_pix_fmt == PIX_FMT_UYVY422) {
2523 /* specific case: convert to YUV422P first */
2524 int_pix_fmt = PIX_FMT_YUV422P;
2525 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2526 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2527 /* specific case: convert to YUV411P first */
2528 int_pix_fmt = PIX_FMT_YUV411P;
2529 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2530 src_pix_fmt != PIX_FMT_GRAY8) ||
2531 (dst_pix->color_type == FF_COLOR_GRAY &&
2532 dst_pix_fmt != PIX_FMT_GRAY8)) {
2533 /* gray8 is the normalized format */
2534 int_pix_fmt = PIX_FMT_GRAY8;
2535 } else if ((is_yuv_planar(src_pix) &&
2536 src_pix_fmt != PIX_FMT_YUV444P &&
2537 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2538 /* yuv444 is the normalized format */
2539 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2540 int_pix_fmt = PIX_FMT_YUVJ444P;
2542 int_pix_fmt = PIX_FMT_YUV444P;
2543 } else if ((is_yuv_planar(dst_pix) &&
2544 dst_pix_fmt != PIX_FMT_YUV444P &&
2545 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2546 /* yuv444 is the normalized format */
2547 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2548 int_pix_fmt = PIX_FMT_YUVJ444P;
2550 int_pix_fmt = PIX_FMT_YUV444P;
2552 /* the two formats are rgb or gray8 or yuv[j]444p */
2553 if (src_pix->is_alpha && dst_pix->is_alpha)
2554 int_pix_fmt = PIX_FMT_RGB32;
2556 int_pix_fmt = PIX_FMT_RGB24;
2558 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2561 if (img_convert(tmp, int_pix_fmt,
2562 src, src_pix_fmt, src_width, src_height) < 0)
2564 if (img_convert(dst, dst_pix_fmt,
2565 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2569 avpicture_free(tmp);
2574 /* NOTE: we scan all the pixels to have an exact information */
2575 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2577 const unsigned char *p;
2578 int src_wrap, ret, x, y;
2580 uint32_t *palette = (uint32_t *)src->data[1];
2583 src_wrap = src->linesize[0] - width;
2585 for(y=0;y<height;y++) {
2586 for(x=0;x<width;x++) {
2587 a = palette[p[0]] >> 24;
2589 ret |= FF_ALPHA_TRANSP;
2590 } else if (a != 0xff) {
2591 ret |= FF_ALPHA_SEMI_TRANSP;
2600 int img_get_alpha_info(const AVPicture *src,
2601 int pix_fmt, int width, int height)
2603 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2606 pf = &pix_fmt_info[pix_fmt];
2607 /* no alpha can be represented in format */
2612 ret = get_alpha_info_rgb32(src, width, height);
2615 ret = get_alpha_info_pal8(src, width, height);
2618 /* we do not know, so everything is indicated */
2619 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2626 #define DEINT_INPLACE_LINE_LUM \
2627 movd_m2r(lum_m4[0],mm0);\
2628 movd_m2r(lum_m3[0],mm1);\
2629 movd_m2r(lum_m2[0],mm2);\
2630 movd_m2r(lum_m1[0],mm3);\
2631 movd_m2r(lum[0],mm4);\
2632 punpcklbw_r2r(mm7,mm0);\
2633 movd_r2m(mm2,lum_m4[0]);\
2634 punpcklbw_r2r(mm7,mm1);\
2635 punpcklbw_r2r(mm7,mm2);\
2636 punpcklbw_r2r(mm7,mm3);\
2637 punpcklbw_r2r(mm7,mm4);\
2638 paddw_r2r(mm3,mm1);\
2640 paddw_r2r(mm4,mm0);\
2642 paddw_r2r(mm6,mm2);\
2643 paddw_r2r(mm2,mm1);\
2644 psubusw_r2r(mm0,mm1);\
2646 packuswb_r2r(mm7,mm1);\
2647 movd_r2m(mm1,lum_m2[0]);
2649 #define DEINT_LINE_LUM \
2650 movd_m2r(lum_m4[0],mm0);\
2651 movd_m2r(lum_m3[0],mm1);\
2652 movd_m2r(lum_m2[0],mm2);\
2653 movd_m2r(lum_m1[0],mm3);\
2654 movd_m2r(lum[0],mm4);\
2655 punpcklbw_r2r(mm7,mm0);\
2656 punpcklbw_r2r(mm7,mm1);\
2657 punpcklbw_r2r(mm7,mm2);\
2658 punpcklbw_r2r(mm7,mm3);\
2659 punpcklbw_r2r(mm7,mm4);\
2660 paddw_r2r(mm3,mm1);\
2662 paddw_r2r(mm4,mm0);\
2664 paddw_r2r(mm6,mm2);\
2665 paddw_r2r(mm2,mm1);\
2666 psubusw_r2r(mm0,mm1);\
2668 packuswb_r2r(mm7,mm1);\
2669 movd_r2m(mm1,dst[0]);
2672 /* filter parameters: [-1 4 2 4 -1] // 8 */
2673 static void deinterlace_line(uint8_t *dst,
2674 const uint8_t *lum_m4, const uint8_t *lum_m3,
2675 const uint8_t *lum_m2, const uint8_t *lum_m1,
2680 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2683 for(;size > 0;size--) {
2685 sum += lum_m3[0] << 2;
2686 sum += lum_m2[0] << 1;
2687 sum += lum_m1[0] << 2;
2689 dst[0] = cm[(sum + 4) >> 3];
2706 movq_m2r(rounder,mm6);
2708 for (;size > 3; size-=4) {
2719 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2723 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2726 for(;size > 0;size--) {
2728 sum += lum_m3[0] << 2;
2729 sum += lum_m2[0] << 1;
2730 lum_m4[0]=lum_m2[0];
2731 sum += lum_m1[0] << 2;
2733 lum_m2[0] = cm[(sum + 4) >> 3];
2749 movq_m2r(rounder,mm6);
2751 for (;size > 3; size-=4) {
2752 DEINT_INPLACE_LINE_LUM
2762 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2763 top field is copied as is, but the bottom field is deinterlaced
2764 against the top field. */
2765 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2766 const uint8_t *src1, int src_wrap,
2767 int width, int height)
2769 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2774 src_0=&src_m1[src_wrap];
2775 src_p1=&src_0[src_wrap];
2776 src_p2=&src_p1[src_wrap];
2777 for(y=0;y<(height-2);y+=2) {
2778 memcpy(dst,src_m1,width);
2780 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2784 src_p1 += 2*src_wrap;
2785 src_p2 += 2*src_wrap;
2788 memcpy(dst,src_m1,width);
2791 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2794 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2795 int width, int height)
2797 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2800 buf = (uint8_t*)av_malloc(width);
2803 memcpy(buf,src_m1,width);
2804 src_0=&src_m1[src_wrap];
2805 src_p1=&src_0[src_wrap];
2806 src_p2=&src_p1[src_wrap];
2807 for(y=0;y<(height-2);y+=2) {
2808 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2811 src_p1 += 2*src_wrap;
2812 src_p2 += 2*src_wrap;
2815 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2819 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2820 int pix_fmt, int width, int height)
2824 if (pix_fmt != PIX_FMT_YUV420P &&
2825 pix_fmt != PIX_FMT_YUV422P &&
2826 pix_fmt != PIX_FMT_YUV444P &&
2827 pix_fmt != PIX_FMT_YUV411P)
2829 if ((width & 3) != 0 || (height & 3) != 0)
2835 case PIX_FMT_YUV420P:
2839 case PIX_FMT_YUV422P:
2842 case PIX_FMT_YUV411P:
2850 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2853 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2854 src->data[i], src->linesize[i],