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 int av_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
811 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
813 pf = &pix_fmt_info[pix_fmt];
814 switch(pf->pixel_type) {
815 case FF_PIXEL_PACKED:
817 case PIX_FMT_YUYV422:
818 case PIX_FMT_UYVY422:
825 case PIX_FMT_UYYVYY411:
829 bits = pf->depth * pf->nb_channels;
832 return (width * bits + 7) >> 3;
834 case FF_PIXEL_PLANAR:
835 if (plane == 1 || plane == 2)
836 width >>= pf->x_chroma_shift;
838 return (width * pf->depth + 7) >> 3;
840 case FF_PIXEL_PALETTE:
849 void av_picture_copy(AVPicture *dst, const AVPicture *src,
850 int pix_fmt, int width, int height)
853 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
855 pf = &pix_fmt_info[pix_fmt];
856 switch(pf->pixel_type) {
857 case FF_PIXEL_PACKED:
858 case FF_PIXEL_PLANAR:
859 for(i = 0; i < pf->nb_channels; i++) {
861 int bwidth = av_get_plane_bytewidth(pix_fmt, width, i);
864 if (i == 1 || i == 2) {
865 w >>= pf->x_chroma_shift;
866 h >>= pf->y_chroma_shift;
868 ff_img_copy_plane(dst->data[i], dst->linesize[i],
869 src->data[i], src->linesize[i],
873 case FF_PIXEL_PALETTE:
874 ff_img_copy_plane(dst->data[0], dst->linesize[0],
875 src->data[0], src->linesize[0],
877 /* copy the palette */
878 ff_img_copy_plane(dst->data[1], dst->linesize[1],
879 src->data[1], src->linesize[1],
885 /* XXX: totally non optimized */
887 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
888 int width, int height)
890 const uint8_t *p, *p1;
891 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
899 for(;height >= 1; height -= 2) {
904 for(w = width; w >= 2; w -= 2) {
921 p1 += src->linesize[0];
922 lum1 += dst->linesize[0];
926 for(w = width; w >= 2; w -= 2) {
935 p1 += src->linesize[0];
936 lum1 += dst->linesize[0];
938 cb1 += dst->linesize[1];
939 cr1 += dst->linesize[2];
943 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
944 int width, int height)
946 const uint8_t *p, *p1;
947 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
956 for(;height >= 1; height -= 2) {
961 for(w = width; w >= 2; w -= 2) {
978 p1 += src->linesize[0];
979 lum1 += dst->linesize[0];
983 for(w = width; w >= 2; w -= 2) {
992 p1 += src->linesize[0];
993 lum1 += dst->linesize[0];
995 cb1 += dst->linesize[1];
996 cr1 += dst->linesize[2];
1001 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1002 int width, int height)
1004 const uint8_t *p, *p1;
1005 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1009 lum1 = dst->data[0];
1012 for(;height > 0; height--) {
1017 for(w = width; w >= 2; w -= 2) {
1027 p1 += src->linesize[0];
1028 lum1 += dst->linesize[0];
1029 cb1 += dst->linesize[1];
1030 cr1 += dst->linesize[2];
1035 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1036 int width, int height)
1038 const uint8_t *p, *p1;
1039 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1043 lum1 = dst->data[0];
1046 for(;height > 0; height--) {
1051 for(w = width; w >= 2; w -= 2) {
1061 p1 += src->linesize[0];
1062 lum1 += dst->linesize[0];
1063 cb1 += dst->linesize[1];
1064 cr1 += dst->linesize[2];
1068 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1069 int width, int height)
1072 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1076 lum1 = src->data[0];
1079 for(;height > 0; height--) {
1084 for(w = width; w >= 2; w -= 2) {
1094 p1 += dst->linesize[0];
1095 lum1 += src->linesize[0];
1096 cb1 += src->linesize[1];
1097 cr1 += src->linesize[2];
1101 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1102 int width, int height)
1105 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1109 lum1 = src->data[0];
1112 for(;height > 0; height--) {
1117 for(w = width; w >= 2; w -= 2) {
1127 p1 += dst->linesize[0];
1128 lum1 += src->linesize[0];
1129 cb1 += src->linesize[1];
1130 cr1 += src->linesize[2];
1134 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1135 int width, int height)
1137 const uint8_t *p, *p1;
1138 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1142 lum1 = dst->data[0];
1145 for(;height > 0; height--) {
1150 for(w = width; w >= 4; w -= 4) {
1162 p1 += src->linesize[0];
1163 lum1 += dst->linesize[0];
1164 cb1 += dst->linesize[1];
1165 cr1 += dst->linesize[2];
1170 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1171 int width, int height)
1174 uint8_t *line1, *line2, *linesrc = dst->data[0];
1175 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1176 uint8_t *cb1, *cb2 = src->data[1];
1177 uint8_t *cr1, *cr2 = src->data[2];
1179 for(h = height / 2; h--;) {
1181 line2 = linesrc + dst->linesize[0];
1184 lum2 = lumsrc + src->linesize[0];
1189 for(w = width / 2; w--;) {
1190 *line1++ = *lum1++; *line2++ = *lum2++;
1191 *line1++ = *line2++ = *cb1++;
1192 *line1++ = *lum1++; *line2++ = *lum2++;
1193 *line1++ = *line2++ = *cr1++;
1196 linesrc += dst->linesize[0] * 2;
1197 lumsrc += src->linesize[0] * 2;
1198 cb2 += src->linesize[1];
1199 cr2 += src->linesize[2];
1203 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1204 int width, int height)
1207 uint8_t *line1, *line2, *linesrc = dst->data[0];
1208 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1209 uint8_t *cb1, *cb2 = src->data[1];
1210 uint8_t *cr1, *cr2 = src->data[2];
1212 for(h = height / 2; h--;) {
1214 line2 = linesrc + dst->linesize[0];
1217 lum2 = lumsrc + src->linesize[0];
1222 for(w = width / 2; w--;) {
1223 *line1++ = *line2++ = *cb1++;
1224 *line1++ = *lum1++; *line2++ = *lum2++;
1225 *line1++ = *line2++ = *cr1++;
1226 *line1++ = *lum1++; *line2++ = *lum2++;
1229 linesrc += dst->linesize[0] * 2;
1230 lumsrc += src->linesize[0] * 2;
1231 cb2 += src->linesize[1];
1232 cr2 += src->linesize[2];
1236 static uint8_t y_ccir_to_jpeg[256];
1237 static uint8_t y_jpeg_to_ccir[256];
1238 static uint8_t c_ccir_to_jpeg[256];
1239 static uint8_t c_jpeg_to_ccir[256];
1241 /* init various conversion tables */
1242 static void img_convert_init(void)
1245 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1247 for(i = 0;i < 256; i++) {
1248 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1249 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1250 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1251 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1255 /* apply to each pixel the given table */
1256 static void img_apply_table(uint8_t *dst, int dst_wrap,
1257 const uint8_t *src, int src_wrap,
1258 int width, int height, const uint8_t *table1)
1263 const uint8_t *table;
1266 for(;height > 0; height--) {
1290 /* XXX: use generic filter ? */
1291 /* XXX: in most cases, the sampling position is incorrect */
1294 static void shrink41(uint8_t *dst, int dst_wrap,
1295 const uint8_t *src, int src_wrap,
1296 int width, int height)
1302 for(;height > 0; height--) {
1305 for(w = width;w > 0; w--) {
1306 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1316 static void shrink21(uint8_t *dst, int dst_wrap,
1317 const uint8_t *src, int src_wrap,
1318 int width, int height)
1324 for(;height > 0; height--) {
1327 for(w = width;w > 0; w--) {
1328 d[0] = (s[0] + s[1]) >> 1;
1338 static void shrink12(uint8_t *dst, int dst_wrap,
1339 const uint8_t *src, int src_wrap,
1340 int width, int height)
1344 const uint8_t *s1, *s2;
1346 for(;height > 0; height--) {
1350 for(w = width;w >= 4; w-=4) {
1351 d[0] = (s1[0] + s2[0]) >> 1;
1352 d[1] = (s1[1] + s2[1]) >> 1;
1353 d[2] = (s1[2] + s2[2]) >> 1;
1354 d[3] = (s1[3] + s2[3]) >> 1;
1360 d[0] = (s1[0] + s2[0]) >> 1;
1365 src += 2 * src_wrap;
1371 void ff_shrink22(uint8_t *dst, int dst_wrap,
1372 const uint8_t *src, int src_wrap,
1373 int width, int height)
1376 const uint8_t *s1, *s2;
1379 for(;height > 0; height--) {
1383 for(w = width;w >= 4; w-=4) {
1384 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1385 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1386 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1387 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1393 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1398 src += 2 * src_wrap;
1404 void ff_shrink44(uint8_t *dst, int dst_wrap,
1405 const uint8_t *src, int src_wrap,
1406 int width, int height)
1409 const uint8_t *s1, *s2, *s3, *s4;
1412 for(;height > 0; height--) {
1418 for(w = width;w > 0; w--) {
1419 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1420 s2[0] + s2[1] + s2[2] + s2[3] +
1421 s3[0] + s3[1] + s3[2] + s3[3] +
1422 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1429 src += 4 * src_wrap;
1435 void ff_shrink88(uint8_t *dst, int dst_wrap,
1436 const uint8_t *src, int src_wrap,
1437 int width, int height)
1441 for(;height > 0; height--) {
1442 for(w = width;w > 0; w--) {
1445 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1448 *(dst++) = (tmp + 32)>>6;
1449 src += 8 - 8*src_wrap;
1451 src += 8*src_wrap - 8*width;
1452 dst += dst_wrap - width;
1456 static void grow21_line(uint8_t *dst, const uint8_t *src,
1465 for(w = width;w >= 4; w-=4) {
1466 d[1] = d[0] = s1[0];
1467 d[3] = d[2] = s1[1];
1471 for(;w >= 2; w -= 2) {
1472 d[1] = d[0] = s1[0];
1476 /* only needed if width is not a multiple of two */
1477 /* XXX: veryfy that */
1483 static void grow41_line(uint8_t *dst, const uint8_t *src,
1492 for(w = width;w >= 4; w-=4) {
1504 static void grow21(uint8_t *dst, int dst_wrap,
1505 const uint8_t *src, int src_wrap,
1506 int width, int height)
1508 for(;height > 0; height--) {
1509 grow21_line(dst, src, width);
1516 static void grow12(uint8_t *dst, int dst_wrap,
1517 const uint8_t *src, int src_wrap,
1518 int width, int height)
1520 for(;height > 0; height-=2) {
1521 memcpy(dst, src, width);
1523 memcpy(dst, src, width);
1530 static void grow22(uint8_t *dst, int dst_wrap,
1531 const uint8_t *src, int src_wrap,
1532 int width, int height)
1534 for(;height > 0; height--) {
1535 grow21_line(dst, src, width);
1543 static void grow41(uint8_t *dst, int dst_wrap,
1544 const uint8_t *src, int src_wrap,
1545 int width, int height)
1547 for(;height > 0; height--) {
1548 grow41_line(dst, src, width);
1555 static void grow44(uint8_t *dst, int dst_wrap,
1556 const uint8_t *src, int src_wrap,
1557 int width, int height)
1559 for(;height > 0; height--) {
1560 grow41_line(dst, src, width);
1561 if ((height & 3) == 1)
1568 static void conv411(uint8_t *dst, int dst_wrap,
1569 const uint8_t *src, int src_wrap,
1570 int width, int height)
1573 const uint8_t *s1, *s2;
1578 for(;height > 0; height--) {
1580 s2 = src + src_wrap;
1582 for(w = width;w > 0; w--) {
1583 c = (s1[0] + s2[0]) >> 1;
1590 src += src_wrap * 2;
1595 /* XXX: add jpeg quantize code */
1597 #define TRANSP_INDEX (6*6*6)
1599 /* this is maybe slow, but allows for extensions */
1600 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1602 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1605 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1608 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1611 pal = (uint32_t *)palette;
1613 for(r = 0; r < 6; r++) {
1614 for(g = 0; g < 6; g++) {
1615 for(b = 0; b < 6; b++) {
1616 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1617 (pal_value[g] << 8) | pal_value[b];
1624 pal[i++] = 0xff000000;
1627 /* copy bit n to bits 0 ... n - 1 */
1628 static inline unsigned int bitcopy_n(unsigned int a, int n)
1631 mask = (1 << n) - 1;
1632 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1635 /* rgb555 handling */
1637 #define RGB_NAME rgb555
1639 #define RGB_IN(r, g, b, s)\
1641 unsigned int v = ((const uint16_t *)(s))[0];\
1642 r = bitcopy_n(v >> (10 - 3), 3);\
1643 g = bitcopy_n(v >> (5 - 3), 3);\
1644 b = bitcopy_n(v << 3, 3);\
1648 #define RGB_OUT(d, r, g, b)\
1650 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1655 #include "imgconvert_template.h"
1657 /* rgb565 handling */
1659 #define RGB_NAME rgb565
1661 #define RGB_IN(r, g, b, s)\
1663 unsigned int v = ((const uint16_t *)(s))[0];\
1664 r = bitcopy_n(v >> (11 - 3), 3);\
1665 g = bitcopy_n(v >> (5 - 2), 2);\
1666 b = bitcopy_n(v << 3, 3);\
1669 #define RGB_OUT(d, r, g, b)\
1671 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1676 #include "imgconvert_template.h"
1678 /* bgr24 handling */
1680 #define RGB_NAME bgr24
1682 #define RGB_IN(r, g, b, s)\
1689 #define RGB_OUT(d, r, g, b)\
1698 #include "imgconvert_template.h"
1704 /* rgb24 handling */
1706 #define RGB_NAME rgb24
1709 #define RGB_IN(r, g, b, s)\
1716 #define RGB_OUT(d, r, g, b)\
1725 #include "imgconvert_template.h"
1727 /* rgb32 handling */
1729 #define RGB_NAME rgb32
1732 #define RGB_IN(r, g, b, s)\
1734 unsigned int v = ((const uint32_t *)(s))[0];\
1735 r = (v >> 16) & 0xff;\
1736 g = (v >> 8) & 0xff;\
1740 #define RGBA_IN(r, g, b, a, s)\
1742 unsigned int v = ((const uint32_t *)(s))[0];\
1743 a = (v >> 24) & 0xff;\
1744 r = (v >> 16) & 0xff;\
1745 g = (v >> 8) & 0xff;\
1749 #define RGBA_OUT(d, r, g, b, a)\
1751 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1756 #include "imgconvert_template.h"
1758 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1759 int width, int height, int xor_mask)
1761 const unsigned char *p;
1763 int v, dst_wrap, src_wrap;
1767 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1770 dst_wrap = dst->linesize[0] - width;
1771 for(y=0;y<height;y++) {
1774 v = *p++ ^ xor_mask;
1776 q[1] = -((v >> 6) & 1);
1777 q[2] = -((v >> 5) & 1);
1778 q[3] = -((v >> 4) & 1);
1779 q[4] = -((v >> 3) & 1);
1780 q[5] = -((v >> 2) & 1);
1781 q[6] = -((v >> 1) & 1);
1782 q[7] = -((v >> 0) & 1);
1787 v = *p++ ^ xor_mask;
1789 q[0] = -((v >> 7) & 1);
1799 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1800 int width, int height)
1802 mono_to_gray(dst, src, width, height, 0xff);
1805 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1806 int width, int height)
1808 mono_to_gray(dst, src, width, height, 0x00);
1811 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1812 int width, int height, int xor_mask)
1817 int j, b, v, n1, src_wrap, dst_wrap, y;
1820 src_wrap = src->linesize[0] - width;
1823 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1825 for(y=0;y<height;y++) {
1832 v = (v << 1) | (b >> 7);
1834 d[0] = v ^ xor_mask;
1844 v = (v << 1) | (b >> 7);
1847 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1855 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1856 int width, int height)
1858 gray_to_mono(dst, src, width, height, 0xff);
1861 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1862 int width, int height)
1864 gray_to_mono(dst, src, width, height, 0x00);
1867 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1868 int width, int height)
1870 int x, y, src_wrap, dst_wrap;
1873 src_wrap = src->linesize[0] - width;
1875 dst_wrap = dst->linesize[0] - width * 2;
1876 for(y=0; y<height; y++){
1877 for(x=0; x<width; x++){
1886 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1887 int width, int height)
1889 int x, y, src_wrap, dst_wrap;
1892 src_wrap = src->linesize[0] - width * 2;
1894 dst_wrap = dst->linesize[0] - width;
1895 for(y=0; y<height; y++){
1896 for(x=0; x<width; x++){
1905 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1906 int width, int height)
1908 gray16_to_gray(dst, src, width, height);
1911 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1912 int width, int height)
1914 AVPicture tmpsrc = *src;
1916 gray16_to_gray(dst, &tmpsrc, width, height);
1919 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1920 int width, int height)
1922 int x, y, src_wrap, dst_wrap;
1924 s = (uint16_t*)src->data[0];
1925 src_wrap = (src->linesize[0] - width * 2)/2;
1926 d = (uint16_t*)dst->data[0];
1927 dst_wrap = (dst->linesize[0] - width * 2)/2;
1928 for(y=0; y<height; y++){
1929 for(x=0; x<width; x++){
1930 *d++ = bswap_16(*s++);
1938 typedef struct ConvertEntry {
1939 void (*convert)(AVPicture *dst,
1940 const AVPicture *src, int width, int height);
1943 /* Add each new conversion function in this table. In order to be able
1944 to convert from any format to any format, the following constraints
1947 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1949 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1951 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1953 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1956 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1958 The other conversion functions are just optimizations for common cases.
1960 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1961 [PIX_FMT_YUV420P] = {
1962 [PIX_FMT_YUYV422] = {
1963 .convert = yuv420p_to_yuyv422,
1965 [PIX_FMT_RGB555] = {
1966 .convert = yuv420p_to_rgb555
1968 [PIX_FMT_RGB565] = {
1969 .convert = yuv420p_to_rgb565
1972 .convert = yuv420p_to_bgr24
1975 .convert = yuv420p_to_rgb24
1978 .convert = yuv420p_to_rgb32
1980 [PIX_FMT_UYVY422] = {
1981 .convert = yuv420p_to_uyvy422,
1984 [PIX_FMT_YUV422P] = {
1985 [PIX_FMT_YUYV422] = {
1986 .convert = yuv422p_to_yuyv422,
1988 [PIX_FMT_UYVY422] = {
1989 .convert = yuv422p_to_uyvy422,
1992 [PIX_FMT_YUV444P] = {
1994 .convert = yuv444p_to_rgb24
1997 [PIX_FMT_YUVJ420P] = {
1998 [PIX_FMT_RGB555] = {
1999 .convert = yuvj420p_to_rgb555
2001 [PIX_FMT_RGB565] = {
2002 .convert = yuvj420p_to_rgb565
2005 .convert = yuvj420p_to_bgr24
2008 .convert = yuvj420p_to_rgb24
2011 .convert = yuvj420p_to_rgb32
2014 [PIX_FMT_YUVJ444P] = {
2016 .convert = yuvj444p_to_rgb24
2019 [PIX_FMT_YUYV422] = {
2020 [PIX_FMT_YUV420P] = {
2021 .convert = yuyv422_to_yuv420p,
2023 [PIX_FMT_YUV422P] = {
2024 .convert = yuyv422_to_yuv422p,
2027 [PIX_FMT_UYVY422] = {
2028 [PIX_FMT_YUV420P] = {
2029 .convert = uyvy422_to_yuv420p,
2031 [PIX_FMT_YUV422P] = {
2032 .convert = uyvy422_to_yuv422p,
2036 [PIX_FMT_YUV420P] = {
2037 .convert = rgb24_to_yuv420p
2039 [PIX_FMT_RGB565] = {
2040 .convert = rgb24_to_rgb565
2042 [PIX_FMT_RGB555] = {
2043 .convert = rgb24_to_rgb555
2046 .convert = rgb24_to_rgb32
2049 .convert = rgb24_to_bgr24
2052 .convert = rgb24_to_gray
2055 .convert = rgb24_to_pal8
2057 [PIX_FMT_YUV444P] = {
2058 .convert = rgb24_to_yuv444p
2060 [PIX_FMT_YUVJ420P] = {
2061 .convert = rgb24_to_yuvj420p
2063 [PIX_FMT_YUVJ444P] = {
2064 .convert = rgb24_to_yuvj444p
2069 .convert = rgb32_to_rgb24
2072 .convert = rgb32_to_bgr24
2074 [PIX_FMT_RGB565] = {
2075 .convert = rgb32_to_rgb565
2077 [PIX_FMT_RGB555] = {
2078 .convert = rgb32_to_rgb555
2081 .convert = rgb32_to_pal8
2083 [PIX_FMT_YUV420P] = {
2084 .convert = rgb32_to_yuv420p
2087 .convert = rgb32_to_gray
2092 .convert = bgr24_to_rgb32
2095 .convert = bgr24_to_rgb24
2097 [PIX_FMT_YUV420P] = {
2098 .convert = bgr24_to_yuv420p
2101 .convert = bgr24_to_gray
2104 [PIX_FMT_RGB555] = {
2106 .convert = rgb555_to_rgb24
2109 .convert = rgb555_to_rgb32
2111 [PIX_FMT_YUV420P] = {
2112 .convert = rgb555_to_yuv420p
2115 .convert = rgb555_to_gray
2118 [PIX_FMT_RGB565] = {
2120 .convert = rgb565_to_rgb32
2123 .convert = rgb565_to_rgb24
2125 [PIX_FMT_YUV420P] = {
2126 .convert = rgb565_to_yuv420p
2129 .convert = rgb565_to_gray
2132 [PIX_FMT_GRAY16BE] = {
2134 .convert = gray16be_to_gray
2136 [PIX_FMT_GRAY16LE] = {
2137 .convert = gray16_to_gray16
2140 [PIX_FMT_GRAY16LE] = {
2142 .convert = gray16le_to_gray
2144 [PIX_FMT_GRAY16BE] = {
2145 .convert = gray16_to_gray16
2149 [PIX_FMT_RGB555] = {
2150 .convert = gray_to_rgb555
2152 [PIX_FMT_RGB565] = {
2153 .convert = gray_to_rgb565
2156 .convert = gray_to_rgb24
2159 .convert = gray_to_bgr24
2162 .convert = gray_to_rgb32
2164 [PIX_FMT_MONOWHITE] = {
2165 .convert = gray_to_monowhite
2167 [PIX_FMT_MONOBLACK] = {
2168 .convert = gray_to_monoblack
2170 [PIX_FMT_GRAY16LE] = {
2171 .convert = gray_to_gray16
2173 [PIX_FMT_GRAY16BE] = {
2174 .convert = gray_to_gray16
2177 [PIX_FMT_MONOWHITE] = {
2179 .convert = monowhite_to_gray
2182 [PIX_FMT_MONOBLACK] = {
2184 .convert = monoblack_to_gray
2188 [PIX_FMT_RGB555] = {
2189 .convert = pal8_to_rgb555
2191 [PIX_FMT_RGB565] = {
2192 .convert = pal8_to_rgb565
2195 .convert = pal8_to_bgr24
2198 .convert = pal8_to_rgb24
2201 .convert = pal8_to_rgb32
2204 [PIX_FMT_UYYVYY411] = {
2205 [PIX_FMT_YUV411P] = {
2206 .convert = uyyvyy411_to_yuv411p,
2212 int avpicture_alloc(AVPicture *picture,
2213 int pix_fmt, int width, int height)
2218 size = avpicture_get_size(pix_fmt, width, height);
2221 ptr = av_malloc(size);
2224 avpicture_fill(picture, ptr, pix_fmt, width, height);
2227 memset(picture, 0, sizeof(AVPicture));
2231 void avpicture_free(AVPicture *picture)
2233 av_free(picture->data[0]);
2236 /* return true if yuv planar */
2237 static inline int is_yuv_planar(const PixFmtInfo *ps)
2239 return (ps->color_type == FF_COLOR_YUV ||
2240 ps->color_type == FF_COLOR_YUV_JPEG) &&
2241 ps->pixel_type == FF_PIXEL_PLANAR;
2244 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2245 int pix_fmt, int top_band, int left_band)
2250 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2253 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2254 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2256 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2257 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2258 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2260 dst->linesize[0] = src->linesize[0];
2261 dst->linesize[1] = src->linesize[1];
2262 dst->linesize[2] = src->linesize[2];
2266 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2267 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2276 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2277 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2279 for (i = 0; i < 3; i++) {
2280 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2281 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2283 if (padtop || padleft) {
2284 memset(dst->data[i], color[i],
2285 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2288 if (padleft || padright) {
2289 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2290 (dst->linesize[i] - (padright >> x_shift));
2291 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2292 for (y = 0; y < yheight; y++) {
2293 memset(optr, color[i], (padleft + padright) >> x_shift);
2294 optr += dst->linesize[i];
2298 if (src) { /* first line */
2299 uint8_t *iptr = src->data[i];
2300 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2301 (padleft >> x_shift);
2302 memcpy(optr, iptr, src->linesize[i]);
2303 iptr += src->linesize[i];
2304 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2305 (dst->linesize[i] - (padright >> x_shift));
2306 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2307 for (y = 0; y < yheight; y++) {
2308 memset(optr, color[i], (padleft + padright) >> x_shift);
2309 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2311 iptr += src->linesize[i];
2312 optr += dst->linesize[i];
2316 if (padbottom || padright) {
2317 optr = dst->data[i] + dst->linesize[i] *
2318 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2319 memset(optr, color[i],dst->linesize[i] *
2320 (padbottom >> y_shift) + (padright >> x_shift));
2326 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
2327 void img_copy(AVPicture *dst, const AVPicture *src,
2328 int pix_fmt, int width, int height)
2330 av_picture_copy(dst, src, pix_fmt, width, height);
2333 int img_crop(AVPicture *dst, const AVPicture *src,
2334 int pix_fmt, int top_band, int left_band)
2336 return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
2339 int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2340 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2343 return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
2347 #ifndef CONFIG_SWSCALER
2348 /* XXX: always use linesize. Return -1 if not supported */
2349 int img_convert(AVPicture *dst, int dst_pix_fmt,
2350 const AVPicture *src, int src_pix_fmt,
2351 int src_width, int src_height)
2354 int i, ret, dst_width, dst_height, int_pix_fmt;
2355 const PixFmtInfo *src_pix, *dst_pix;
2356 const ConvertEntry *ce;
2357 AVPicture tmp1, *tmp = &tmp1;
2359 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2360 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2362 if (src_width <= 0 || src_height <= 0)
2370 dst_width = src_width;
2371 dst_height = src_height;
2373 dst_pix = &pix_fmt_info[dst_pix_fmt];
2374 src_pix = &pix_fmt_info[src_pix_fmt];
2375 if (src_pix_fmt == dst_pix_fmt) {
2376 /* no conversion needed: just copy */
2377 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2381 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2383 /* specific conversion routine */
2384 ce->convert(dst, src, dst_width, dst_height);
2389 if (is_yuv_planar(dst_pix) &&
2390 src_pix_fmt == PIX_FMT_GRAY8) {
2394 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2395 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2396 src->data[0], src->linesize[0],
2397 dst_width, dst_height);
2399 img_apply_table(dst->data[0], dst->linesize[0],
2400 src->data[0], src->linesize[0],
2401 dst_width, dst_height,
2404 /* fill U and V with 128 */
2407 w >>= dst_pix->x_chroma_shift;
2408 h >>= dst_pix->y_chroma_shift;
2409 for(i = 1; i <= 2; i++) {
2411 for(y = 0; y< h; y++) {
2413 d += dst->linesize[i];
2420 if (is_yuv_planar(src_pix) &&
2421 dst_pix_fmt == PIX_FMT_GRAY8) {
2422 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2423 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2424 src->data[0], src->linesize[0],
2425 dst_width, dst_height);
2427 img_apply_table(dst->data[0], dst->linesize[0],
2428 src->data[0], src->linesize[0],
2429 dst_width, dst_height,
2435 /* YUV to YUV planar */
2436 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2437 int x_shift, y_shift, w, h, xy_shift;
2438 void (*resize_func)(uint8_t *dst, int dst_wrap,
2439 const uint8_t *src, int src_wrap,
2440 int width, int height);
2442 /* compute chroma size of the smallest dimensions */
2445 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2446 w >>= dst_pix->x_chroma_shift;
2448 w >>= src_pix->x_chroma_shift;
2449 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2450 h >>= dst_pix->y_chroma_shift;
2452 h >>= src_pix->y_chroma_shift;
2454 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2455 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2456 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2457 /* there must be filters for conversion at least from and to
2461 resize_func = ff_img_copy_plane;
2464 resize_func = shrink21;
2467 resize_func = shrink41;
2470 resize_func = shrink12;
2473 resize_func = ff_shrink22;
2476 resize_func = ff_shrink44;
2479 resize_func = grow21;
2482 resize_func = grow12;
2485 resize_func = grow41;
2488 resize_func = grow22;
2491 resize_func = grow44;
2494 resize_func = conv411;
2497 /* currently not handled */
2498 goto no_chroma_filter;
2501 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2502 src->data[0], src->linesize[0],
2503 dst_width, dst_height);
2505 for(i = 1;i <= 2; i++)
2506 resize_func(dst->data[i], dst->linesize[i],
2507 src->data[i], src->linesize[i],
2508 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2509 /* if yuv color space conversion is needed, we do it here on
2510 the destination image */
2511 if (dst_pix->color_type != src_pix->color_type) {
2512 const uint8_t *y_table, *c_table;
2513 if (dst_pix->color_type == FF_COLOR_YUV) {
2514 y_table = y_jpeg_to_ccir;
2515 c_table = c_jpeg_to_ccir;
2517 y_table = y_ccir_to_jpeg;
2518 c_table = c_ccir_to_jpeg;
2520 img_apply_table(dst->data[0], dst->linesize[0],
2521 dst->data[0], dst->linesize[0],
2522 dst_width, dst_height,
2525 for(i = 1;i <= 2; i++)
2526 img_apply_table(dst->data[i], dst->linesize[i],
2527 dst->data[i], dst->linesize[i],
2528 dst_width>>dst_pix->x_chroma_shift,
2529 dst_height>>dst_pix->y_chroma_shift,
2536 /* try to use an intermediate format */
2537 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2538 dst_pix_fmt == PIX_FMT_YUYV422) {
2539 /* specific case: convert to YUV422P first */
2540 int_pix_fmt = PIX_FMT_YUV422P;
2541 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2542 dst_pix_fmt == PIX_FMT_UYVY422) {
2543 /* specific case: convert to YUV422P first */
2544 int_pix_fmt = PIX_FMT_YUV422P;
2545 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2546 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2547 /* specific case: convert to YUV411P first */
2548 int_pix_fmt = PIX_FMT_YUV411P;
2549 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2550 src_pix_fmt != PIX_FMT_GRAY8) ||
2551 (dst_pix->color_type == FF_COLOR_GRAY &&
2552 dst_pix_fmt != PIX_FMT_GRAY8)) {
2553 /* gray8 is the normalized format */
2554 int_pix_fmt = PIX_FMT_GRAY8;
2555 } else if ((is_yuv_planar(src_pix) &&
2556 src_pix_fmt != PIX_FMT_YUV444P &&
2557 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2558 /* yuv444 is the normalized format */
2559 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2560 int_pix_fmt = PIX_FMT_YUVJ444P;
2562 int_pix_fmt = PIX_FMT_YUV444P;
2563 } else if ((is_yuv_planar(dst_pix) &&
2564 dst_pix_fmt != PIX_FMT_YUV444P &&
2565 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2566 /* yuv444 is the normalized format */
2567 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2568 int_pix_fmt = PIX_FMT_YUVJ444P;
2570 int_pix_fmt = PIX_FMT_YUV444P;
2572 /* the two formats are rgb or gray8 or yuv[j]444p */
2573 if (src_pix->is_alpha && dst_pix->is_alpha)
2574 int_pix_fmt = PIX_FMT_RGB32;
2576 int_pix_fmt = PIX_FMT_RGB24;
2578 if (src_pix_fmt == int_pix_fmt)
2580 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2583 if (img_convert(tmp, int_pix_fmt,
2584 src, src_pix_fmt, src_width, src_height) < 0)
2586 if (img_convert(dst, dst_pix_fmt,
2587 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2591 avpicture_free(tmp);
2596 /* NOTE: we scan all the pixels to have an exact information */
2597 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2599 const unsigned char *p;
2600 int src_wrap, ret, x, y;
2602 uint32_t *palette = (uint32_t *)src->data[1];
2605 src_wrap = src->linesize[0] - width;
2607 for(y=0;y<height;y++) {
2608 for(x=0;x<width;x++) {
2609 a = palette[p[0]] >> 24;
2611 ret |= FF_ALPHA_TRANSP;
2612 } else if (a != 0xff) {
2613 ret |= FF_ALPHA_SEMI_TRANSP;
2622 int img_get_alpha_info(const AVPicture *src,
2623 int pix_fmt, int width, int height)
2625 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2628 pf = &pix_fmt_info[pix_fmt];
2629 /* no alpha can be represented in format */
2634 ret = get_alpha_info_rgb32(src, width, height);
2637 ret = get_alpha_info_pal8(src, width, height);
2640 /* we do not know, so everything is indicated */
2641 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2648 #define DEINT_INPLACE_LINE_LUM \
2649 movd_m2r(lum_m4[0],mm0);\
2650 movd_m2r(lum_m3[0],mm1);\
2651 movd_m2r(lum_m2[0],mm2);\
2652 movd_m2r(lum_m1[0],mm3);\
2653 movd_m2r(lum[0],mm4);\
2654 punpcklbw_r2r(mm7,mm0);\
2655 movd_r2m(mm2,lum_m4[0]);\
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,lum_m2[0]);
2671 #define DEINT_LINE_LUM \
2672 movd_m2r(lum_m4[0],mm0);\
2673 movd_m2r(lum_m3[0],mm1);\
2674 movd_m2r(lum_m2[0],mm2);\
2675 movd_m2r(lum_m1[0],mm3);\
2676 movd_m2r(lum[0],mm4);\
2677 punpcklbw_r2r(mm7,mm0);\
2678 punpcklbw_r2r(mm7,mm1);\
2679 punpcklbw_r2r(mm7,mm2);\
2680 punpcklbw_r2r(mm7,mm3);\
2681 punpcklbw_r2r(mm7,mm4);\
2682 paddw_r2r(mm3,mm1);\
2684 paddw_r2r(mm4,mm0);\
2686 paddw_r2r(mm6,mm2);\
2687 paddw_r2r(mm2,mm1);\
2688 psubusw_r2r(mm0,mm1);\
2690 packuswb_r2r(mm7,mm1);\
2691 movd_r2m(mm1,dst[0]);
2694 /* filter parameters: [-1 4 2 4 -1] // 8 */
2695 static void deinterlace_line(uint8_t *dst,
2696 const uint8_t *lum_m4, const uint8_t *lum_m3,
2697 const uint8_t *lum_m2, const uint8_t *lum_m1,
2702 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2705 for(;size > 0;size--) {
2707 sum += lum_m3[0] << 2;
2708 sum += lum_m2[0] << 1;
2709 sum += lum_m1[0] << 2;
2711 dst[0] = cm[(sum + 4) >> 3];
2728 movq_m2r(rounder,mm6);
2730 for (;size > 3; size-=4) {
2741 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2745 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2748 for(;size > 0;size--) {
2750 sum += lum_m3[0] << 2;
2751 sum += lum_m2[0] << 1;
2752 lum_m4[0]=lum_m2[0];
2753 sum += lum_m1[0] << 2;
2755 lum_m2[0] = cm[(sum + 4) >> 3];
2771 movq_m2r(rounder,mm6);
2773 for (;size > 3; size-=4) {
2774 DEINT_INPLACE_LINE_LUM
2784 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2785 top field is copied as is, but the bottom field is deinterlaced
2786 against the top field. */
2787 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2788 const uint8_t *src1, int src_wrap,
2789 int width, int height)
2791 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2796 src_0=&src_m1[src_wrap];
2797 src_p1=&src_0[src_wrap];
2798 src_p2=&src_p1[src_wrap];
2799 for(y=0;y<(height-2);y+=2) {
2800 memcpy(dst,src_m1,width);
2802 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2806 src_p1 += 2*src_wrap;
2807 src_p2 += 2*src_wrap;
2810 memcpy(dst,src_m1,width);
2813 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2816 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2817 int width, int height)
2819 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2822 buf = (uint8_t*)av_malloc(width);
2825 memcpy(buf,src_m1,width);
2826 src_0=&src_m1[src_wrap];
2827 src_p1=&src_0[src_wrap];
2828 src_p2=&src_p1[src_wrap];
2829 for(y=0;y<(height-2);y+=2) {
2830 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2833 src_p1 += 2*src_wrap;
2834 src_p2 += 2*src_wrap;
2837 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2841 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2842 int pix_fmt, int width, int height)
2846 if (pix_fmt != PIX_FMT_YUV420P &&
2847 pix_fmt != PIX_FMT_YUV422P &&
2848 pix_fmt != PIX_FMT_YUV444P &&
2849 pix_fmt != PIX_FMT_YUV411P &&
2850 pix_fmt != PIX_FMT_GRAY8)
2852 if ((width & 3) != 0 || (height & 3) != 0)
2858 case PIX_FMT_YUV420P:
2862 case PIX_FMT_YUV422P:
2865 case PIX_FMT_YUV411P:
2871 if (pix_fmt == PIX_FMT_GRAY8) {
2876 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2879 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2880 src->data[i], src->linesize[i],