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)
412 snprintf (buf, buf_size,
413 "name " " nb_channels" " depth" " is_alpha"
416 PixFmtInfo info= pix_fmt_info[pix_fmt];
418 char is_alpha_char= info.is_alpha ? 'y' : 'n';
420 snprintf (buf, buf_size,
421 "%-10s" " %1d " " %2d " " %c ",
430 int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
433 const PixFmtInfo *pinfo;
435 memset(picture->linesize, 0, sizeof(picture->linesize));
437 pinfo = &pix_fmt_info[pix_fmt];
439 case PIX_FMT_YUV420P:
440 case PIX_FMT_YUV422P:
441 case PIX_FMT_YUV444P:
442 case PIX_FMT_YUV410P:
443 case PIX_FMT_YUV411P:
444 case PIX_FMT_YUV440P:
445 case PIX_FMT_YUVJ420P:
446 case PIX_FMT_YUVJ422P:
447 case PIX_FMT_YUVJ444P:
448 case PIX_FMT_YUVJ440P:
449 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
450 picture->linesize[0] = width;
451 picture->linesize[1] = w2;
452 picture->linesize[2] = w2;
454 case PIX_FMT_YUVA420P:
455 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
456 picture->linesize[0] = width;
457 picture->linesize[1] = w2;
458 picture->linesize[2] = w2;
459 picture->linesize[3] = width;
463 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
464 picture->linesize[0] = width;
465 picture->linesize[1] = w2;
469 picture->linesize[0] = width * 3;
473 case PIX_FMT_RGB32_1:
474 case PIX_FMT_BGR32_1:
475 picture->linesize[0] = width * 4;
477 case PIX_FMT_GRAY16BE:
478 case PIX_FMT_GRAY16LE:
483 case PIX_FMT_YUYV422:
484 picture->linesize[0] = width * 2;
486 case PIX_FMT_UYVY422:
487 picture->linesize[0] = width * 2;
489 case PIX_FMT_UYYVYY411:
490 picture->linesize[0] = width + width/2;
494 case PIX_FMT_RGB4_BYTE:
495 case PIX_FMT_BGR4_BYTE:
497 picture->linesize[0] = width;
501 picture->linesize[0] = width / 2;
503 case PIX_FMT_MONOWHITE:
504 case PIX_FMT_MONOBLACK:
505 picture->linesize[0] = (width + 7) >> 3;
508 picture->linesize[0] = width;
509 picture->linesize[1] = 4;
517 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
521 const PixFmtInfo *pinfo;
523 pinfo = &pix_fmt_info[pix_fmt];
524 size = picture->linesize[0] * height;
526 case PIX_FMT_YUV420P:
527 case PIX_FMT_YUV422P:
528 case PIX_FMT_YUV444P:
529 case PIX_FMT_YUV410P:
530 case PIX_FMT_YUV411P:
531 case PIX_FMT_YUV440P:
532 case PIX_FMT_YUVJ420P:
533 case PIX_FMT_YUVJ422P:
534 case PIX_FMT_YUVJ444P:
535 case PIX_FMT_YUVJ440P:
536 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
537 size2 = picture->linesize[1] * h2;
538 picture->data[0] = ptr;
539 picture->data[1] = picture->data[0] + size;
540 picture->data[2] = picture->data[1] + size2;
541 picture->data[3] = NULL;
542 return size + 2 * size2;
543 case PIX_FMT_YUVA420P:
544 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
545 size2 = picture->linesize[1] * h2;
546 picture->data[0] = ptr;
547 picture->data[1] = picture->data[0] + size;
548 picture->data[2] = picture->data[1] + size2;
549 picture->data[3] = picture->data[1] + size2 + size2;
550 return 2 * size + 2 * size2;
553 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
554 size2 = picture->linesize[1] * h2 * 2;
555 picture->data[0] = ptr;
556 picture->data[1] = picture->data[0] + size;
557 picture->data[2] = NULL;
558 picture->data[3] = NULL;
559 return size + 2 * size2;
564 case PIX_FMT_RGB32_1:
565 case PIX_FMT_BGR32_1:
566 case PIX_FMT_GRAY16BE:
567 case PIX_FMT_GRAY16LE:
572 case PIX_FMT_YUYV422:
573 case PIX_FMT_UYVY422:
574 case PIX_FMT_UYYVYY411:
577 case PIX_FMT_RGB4_BYTE:
578 case PIX_FMT_BGR4_BYTE:
582 case PIX_FMT_MONOWHITE:
583 case PIX_FMT_MONOBLACK:
584 picture->data[0] = ptr;
585 picture->data[1] = NULL;
586 picture->data[2] = NULL;
587 picture->data[3] = NULL;
590 size2 = (size + 3) & ~3;
591 picture->data[0] = ptr;
592 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
593 picture->data[2] = NULL;
594 picture->data[3] = NULL;
595 return size2 + 256 * 4;
597 picture->data[0] = NULL;
598 picture->data[1] = NULL;
599 picture->data[2] = NULL;
600 picture->data[3] = NULL;
605 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
606 int pix_fmt, int width, int height)
609 if(avcodec_check_dimensions(NULL, width, height))
612 if (ff_fill_linesize(picture, pix_fmt, width))
615 return ff_fill_pointer(picture, ptr, pix_fmt, height);
618 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
619 unsigned char *dest, int dest_size)
621 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
622 int i, j, w, h, data_planes;
623 const unsigned char* s;
624 int size = avpicture_get_size(pix_fmt, width, height);
626 if (size > dest_size || size < 0)
629 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
630 if (pix_fmt == PIX_FMT_YUYV422 ||
631 pix_fmt == PIX_FMT_UYVY422 ||
632 pix_fmt == PIX_FMT_BGR565 ||
633 pix_fmt == PIX_FMT_BGR555 ||
634 pix_fmt == PIX_FMT_RGB565 ||
635 pix_fmt == PIX_FMT_RGB555)
637 else if (pix_fmt == PIX_FMT_UYYVYY411)
639 else if (pix_fmt == PIX_FMT_PAL8)
642 w = width * (pf->depth * pf->nb_channels / 8);
647 data_planes = pf->nb_channels;
648 w = (width*pf->depth + 7)/8;
652 for (i=0; i<data_planes; i++) {
654 w = width >> pf->x_chroma_shift;
655 h = height >> pf->y_chroma_shift;
661 s += src->linesize[i];
665 if (pf->pixel_type == FF_PIXEL_PALETTE)
666 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
671 int avpicture_get_size(int pix_fmt, int width, int height)
673 AVPicture dummy_pict;
674 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
677 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
680 const PixFmtInfo *pf, *ps;
683 ps = &pix_fmt_info[src_pix_fmt];
684 pf = &pix_fmt_info[dst_pix_fmt];
688 pf = &pix_fmt_info[dst_pix_fmt];
689 if (pf->depth < ps->depth ||
690 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
691 loss |= FF_LOSS_DEPTH;
692 if (pf->x_chroma_shift > ps->x_chroma_shift ||
693 pf->y_chroma_shift > ps->y_chroma_shift)
694 loss |= FF_LOSS_RESOLUTION;
695 switch(pf->color_type) {
697 if (ps->color_type != FF_COLOR_RGB &&
698 ps->color_type != FF_COLOR_GRAY)
699 loss |= FF_LOSS_COLORSPACE;
702 if (ps->color_type != FF_COLOR_GRAY)
703 loss |= FF_LOSS_COLORSPACE;
706 if (ps->color_type != FF_COLOR_YUV)
707 loss |= FF_LOSS_COLORSPACE;
709 case FF_COLOR_YUV_JPEG:
710 if (ps->color_type != FF_COLOR_YUV_JPEG &&
711 ps->color_type != FF_COLOR_YUV &&
712 ps->color_type != FF_COLOR_GRAY)
713 loss |= FF_LOSS_COLORSPACE;
717 if (ps->color_type != pf->color_type)
718 loss |= FF_LOSS_COLORSPACE;
721 if (pf->color_type == FF_COLOR_GRAY &&
722 ps->color_type != FF_COLOR_GRAY)
723 loss |= FF_LOSS_CHROMA;
724 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
725 loss |= FF_LOSS_ALPHA;
726 if (pf->pixel_type == FF_PIXEL_PALETTE &&
727 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
728 loss |= FF_LOSS_COLORQUANT;
732 static int avg_bits_per_pixel(int pix_fmt)
735 const PixFmtInfo *pf;
737 pf = &pix_fmt_info[pix_fmt];
738 switch(pf->pixel_type) {
739 case FF_PIXEL_PACKED:
741 case PIX_FMT_YUYV422:
742 case PIX_FMT_UYVY422:
749 case PIX_FMT_UYYVYY411:
753 bits = pf->depth * pf->nb_channels;
757 case FF_PIXEL_PLANAR:
758 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
759 bits = pf->depth * pf->nb_channels;
761 bits = pf->depth + ((2 * pf->depth) >>
762 (pf->x_chroma_shift + pf->y_chroma_shift));
765 case FF_PIXEL_PALETTE:
775 static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
780 int dist, i, loss, min_dist, dst_pix_fmt;
782 /* find exact color match with smallest size */
784 min_dist = 0x7fffffff;
785 for(i = 0;i < PIX_FMT_NB; i++) {
786 if (pix_fmt_mask & (1 << i)) {
787 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
789 dist = avg_bits_per_pixel(i);
790 if (dist < min_dist) {
800 int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
801 int has_alpha, int *loss_ptr)
803 int dst_pix_fmt, loss_mask, i;
804 static const int loss_mask_order[] = {
805 ~0, /* no loss first */
808 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
814 /* try with successive loss */
817 loss_mask = loss_mask_order[i++];
818 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
819 has_alpha, loss_mask);
820 if (dst_pix_fmt >= 0)
828 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
832 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
833 const uint8_t *src, int src_wrap,
834 int width, int height)
838 for(;height > 0; height--) {
839 memcpy(dst, src, width);
845 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
848 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
850 pf = &pix_fmt_info[pix_fmt];
851 switch(pf->pixel_type) {
852 case FF_PIXEL_PACKED:
854 case PIX_FMT_YUYV422:
855 case PIX_FMT_UYVY422:
862 case PIX_FMT_UYYVYY411:
866 bits = pf->depth * pf->nb_channels;
869 return (width * bits + 7) >> 3;
871 case FF_PIXEL_PLANAR:
872 if (plane == 1 || plane == 2)
873 width= -((-width)>>pf->x_chroma_shift);
875 return (width * pf->depth + 7) >> 3;
877 case FF_PIXEL_PALETTE:
886 void av_picture_copy(AVPicture *dst, const AVPicture *src,
887 int pix_fmt, int width, int height)
890 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
892 pf = &pix_fmt_info[pix_fmt];
893 switch(pf->pixel_type) {
894 case FF_PIXEL_PACKED:
895 case FF_PIXEL_PLANAR:
896 for(i = 0; i < pf->nb_channels; i++) {
898 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
900 if (i == 1 || i == 2) {
901 h= -((-height)>>pf->y_chroma_shift);
903 ff_img_copy_plane(dst->data[i], dst->linesize[i],
904 src->data[i], src->linesize[i],
908 case FF_PIXEL_PALETTE:
909 ff_img_copy_plane(dst->data[0], dst->linesize[0],
910 src->data[0], src->linesize[0],
912 /* copy the palette */
913 ff_img_copy_plane(dst->data[1], dst->linesize[1],
914 src->data[1], src->linesize[1],
920 /* XXX: totally non optimized */
922 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
923 int width, int height)
925 const uint8_t *p, *p1;
926 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
934 for(;height >= 1; height -= 2) {
939 for(w = width; w >= 2; w -= 2) {
956 p1 += src->linesize[0];
957 lum1 += dst->linesize[0];
961 for(w = width; w >= 2; w -= 2) {
970 p1 += src->linesize[0];
971 lum1 += dst->linesize[0];
973 cb1 += dst->linesize[1];
974 cr1 += dst->linesize[2];
978 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
979 int width, int height)
981 const uint8_t *p, *p1;
982 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
991 for(;height >= 1; height -= 2) {
996 for(w = width; w >= 2; w -= 2) {
1013 p1 += src->linesize[0];
1014 lum1 += dst->linesize[0];
1018 for(w = width; w >= 2; w -= 2) {
1027 p1 += src->linesize[0];
1028 lum1 += dst->linesize[0];
1030 cb1 += dst->linesize[1];
1031 cr1 += dst->linesize[2];
1036 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1037 int width, int height)
1039 const uint8_t *p, *p1;
1040 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1044 lum1 = dst->data[0];
1047 for(;height > 0; height--) {
1052 for(w = width; w >= 2; w -= 2) {
1062 p1 += src->linesize[0];
1063 lum1 += dst->linesize[0];
1064 cb1 += dst->linesize[1];
1065 cr1 += dst->linesize[2];
1070 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1071 int width, int height)
1073 const uint8_t *p, *p1;
1074 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1078 lum1 = dst->data[0];
1081 for(;height > 0; height--) {
1086 for(w = width; w >= 2; w -= 2) {
1096 p1 += src->linesize[0];
1097 lum1 += dst->linesize[0];
1098 cb1 += dst->linesize[1];
1099 cr1 += dst->linesize[2];
1103 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1104 int width, int height)
1107 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1111 lum1 = src->data[0];
1114 for(;height > 0; height--) {
1119 for(w = width; w >= 2; w -= 2) {
1129 p1 += dst->linesize[0];
1130 lum1 += src->linesize[0];
1131 cb1 += src->linesize[1];
1132 cr1 += src->linesize[2];
1136 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1137 int width, int height)
1140 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1144 lum1 = src->data[0];
1147 for(;height > 0; height--) {
1152 for(w = width; w >= 2; w -= 2) {
1162 p1 += dst->linesize[0];
1163 lum1 += src->linesize[0];
1164 cb1 += src->linesize[1];
1165 cr1 += src->linesize[2];
1169 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1170 int width, int height)
1172 const uint8_t *p, *p1;
1173 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1177 lum1 = dst->data[0];
1180 for(;height > 0; height--) {
1185 for(w = width; w >= 4; w -= 4) {
1197 p1 += src->linesize[0];
1198 lum1 += dst->linesize[0];
1199 cb1 += dst->linesize[1];
1200 cr1 += dst->linesize[2];
1205 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1206 int width, int height)
1209 uint8_t *line1, *line2, *linesrc = dst->data[0];
1210 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1211 uint8_t *cb1, *cb2 = src->data[1];
1212 uint8_t *cr1, *cr2 = src->data[2];
1214 for(h = height / 2; h--;) {
1216 line2 = linesrc + dst->linesize[0];
1219 lum2 = lumsrc + src->linesize[0];
1224 for(w = width / 2; w--;) {
1225 *line1++ = *lum1++; *line2++ = *lum2++;
1226 *line1++ = *line2++ = *cb1++;
1227 *line1++ = *lum1++; *line2++ = *lum2++;
1228 *line1++ = *line2++ = *cr1++;
1231 linesrc += dst->linesize[0] * 2;
1232 lumsrc += src->linesize[0] * 2;
1233 cb2 += src->linesize[1];
1234 cr2 += src->linesize[2];
1238 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1239 int width, int height)
1242 uint8_t *line1, *line2, *linesrc = dst->data[0];
1243 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1244 uint8_t *cb1, *cb2 = src->data[1];
1245 uint8_t *cr1, *cr2 = src->data[2];
1247 for(h = height / 2; h--;) {
1249 line2 = linesrc + dst->linesize[0];
1252 lum2 = lumsrc + src->linesize[0];
1257 for(w = width / 2; w--;) {
1258 *line1++ = *line2++ = *cb1++;
1259 *line1++ = *lum1++; *line2++ = *lum2++;
1260 *line1++ = *line2++ = *cr1++;
1261 *line1++ = *lum1++; *line2++ = *lum2++;
1264 linesrc += dst->linesize[0] * 2;
1265 lumsrc += src->linesize[0] * 2;
1266 cb2 += src->linesize[1];
1267 cr2 += src->linesize[2];
1272 void ff_shrink22(uint8_t *dst, int dst_wrap,
1273 const uint8_t *src, int src_wrap,
1274 int width, int height)
1277 const uint8_t *s1, *s2;
1280 for(;height > 0; height--) {
1284 for(w = width;w >= 4; w-=4) {
1285 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1286 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1287 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1288 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1294 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1299 src += 2 * src_wrap;
1305 void ff_shrink44(uint8_t *dst, int dst_wrap,
1306 const uint8_t *src, int src_wrap,
1307 int width, int height)
1310 const uint8_t *s1, *s2, *s3, *s4;
1313 for(;height > 0; height--) {
1319 for(w = width;w > 0; w--) {
1320 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1321 s2[0] + s2[1] + s2[2] + s2[3] +
1322 s3[0] + s3[1] + s3[2] + s3[3] +
1323 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1330 src += 4 * src_wrap;
1336 void ff_shrink88(uint8_t *dst, int dst_wrap,
1337 const uint8_t *src, int src_wrap,
1338 int width, int height)
1342 for(;height > 0; height--) {
1343 for(w = width;w > 0; w--) {
1346 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1349 *(dst++) = (tmp + 32)>>6;
1350 src += 8 - 8*src_wrap;
1352 src += 8*src_wrap - 8*width;
1353 dst += dst_wrap - width;
1357 /* XXX: add jpeg quantize code */
1359 #define TRANSP_INDEX (6*6*6)
1361 /* this is maybe slow, but allows for extensions */
1362 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1364 return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
1367 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1370 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1373 pal = (uint32_t *)palette;
1375 for(r = 0; r < 6; r++) {
1376 for(g = 0; g < 6; g++) {
1377 for(b = 0; b < 6; b++) {
1378 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1379 (pal_value[g] << 8) | pal_value[b];
1386 pal[i++] = 0xff000000;
1389 /* copy bit n to bits 0 ... n - 1 */
1390 static inline unsigned int bitcopy_n(unsigned int a, int n)
1393 mask = (1 << n) - 1;
1394 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1397 /* rgb555 handling */
1399 #define RGB_NAME rgb555
1401 #define RGB_IN(r, g, b, s)\
1403 unsigned int v = ((const uint16_t *)(s))[0];\
1404 r = bitcopy_n(v >> (10 - 3), 3);\
1405 g = bitcopy_n(v >> (5 - 3), 3);\
1406 b = bitcopy_n(v << 3, 3);\
1410 #define RGB_OUT(d, r, g, b)\
1412 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1417 #include "imgconvert_template.c"
1419 /* rgb565 handling */
1421 #define RGB_NAME rgb565
1423 #define RGB_IN(r, g, b, s)\
1425 unsigned int v = ((const uint16_t *)(s))[0];\
1426 r = bitcopy_n(v >> (11 - 3), 3);\
1427 g = bitcopy_n(v >> (5 - 2), 2);\
1428 b = bitcopy_n(v << 3, 3);\
1431 #define RGB_OUT(d, r, g, b)\
1433 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1438 #include "imgconvert_template.c"
1440 /* bgr24 handling */
1442 #define RGB_NAME bgr24
1444 #define RGB_IN(r, g, b, s)\
1451 #define RGB_OUT(d, r, g, b)\
1460 #include "imgconvert_template.c"
1466 /* rgb24 handling */
1468 #define RGB_NAME rgb24
1471 #define RGB_IN(r, g, b, s)\
1478 #define RGB_OUT(d, r, g, b)\
1487 #include "imgconvert_template.c"
1489 /* rgb32 handling */
1491 #define RGB_NAME rgb32
1494 #define RGB_IN(r, g, b, s)\
1496 unsigned int v = ((const uint32_t *)(s))[0];\
1497 r = (v >> 16) & 0xff;\
1498 g = (v >> 8) & 0xff;\
1502 #define RGBA_IN(r, g, b, a, s)\
1504 unsigned int v = ((const uint32_t *)(s))[0];\
1505 a = (v >> 24) & 0xff;\
1506 r = (v >> 16) & 0xff;\
1507 g = (v >> 8) & 0xff;\
1511 #define RGBA_OUT(d, r, g, b, a)\
1513 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1518 #include "imgconvert_template.c"
1520 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1521 int width, int height, int xor_mask)
1523 const unsigned char *p;
1525 int v, dst_wrap, src_wrap;
1529 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1532 dst_wrap = dst->linesize[0] - width;
1533 for(y=0;y<height;y++) {
1536 v = *p++ ^ xor_mask;
1538 q[1] = -((v >> 6) & 1);
1539 q[2] = -((v >> 5) & 1);
1540 q[3] = -((v >> 4) & 1);
1541 q[4] = -((v >> 3) & 1);
1542 q[5] = -((v >> 2) & 1);
1543 q[6] = -((v >> 1) & 1);
1544 q[7] = -((v >> 0) & 1);
1549 v = *p++ ^ xor_mask;
1551 q[0] = -((v >> 7) & 1);
1561 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1562 int width, int height)
1564 mono_to_gray(dst, src, width, height, 0xff);
1567 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1568 int width, int height)
1570 mono_to_gray(dst, src, width, height, 0x00);
1573 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1574 int width, int height, int xor_mask)
1579 int j, b, v, n1, src_wrap, dst_wrap, y;
1582 src_wrap = src->linesize[0] - width;
1585 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1587 for(y=0;y<height;y++) {
1594 v = (v << 1) | (b >> 7);
1596 d[0] = v ^ xor_mask;
1606 v = (v << 1) | (b >> 7);
1609 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1617 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1618 int width, int height)
1620 gray_to_mono(dst, src, width, height, 0xff);
1623 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1624 int width, int height)
1626 gray_to_mono(dst, src, width, height, 0x00);
1629 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1630 int width, int height)
1632 int x, y, src_wrap, dst_wrap;
1635 src_wrap = src->linesize[0] - width;
1637 dst_wrap = dst->linesize[0] - width * 2;
1638 for(y=0; y<height; y++){
1639 for(x=0; x<width; x++){
1648 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1649 int width, int height)
1651 int x, y, src_wrap, dst_wrap;
1654 src_wrap = src->linesize[0] - width * 2;
1656 dst_wrap = dst->linesize[0] - width;
1657 for(y=0; y<height; y++){
1658 for(x=0; x<width; x++){
1667 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1668 int width, int height)
1670 gray16_to_gray(dst, src, width, height);
1673 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1674 int width, int height)
1676 AVPicture tmpsrc = *src;
1678 gray16_to_gray(dst, &tmpsrc, width, height);
1681 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1682 int width, int height)
1684 int x, y, src_wrap, dst_wrap;
1686 s = (uint16_t*)src->data[0];
1687 src_wrap = (src->linesize[0] - width * 2)/2;
1688 d = (uint16_t*)dst->data[0];
1689 dst_wrap = (dst->linesize[0] - width * 2)/2;
1690 for(y=0; y<height; y++){
1691 for(x=0; x<width; x++){
1692 *d++ = bswap_16(*s++);
1700 typedef struct ConvertEntry {
1701 void (*convert)(AVPicture *dst,
1702 const AVPicture *src, int width, int height);
1705 /* Add each new conversion function in this table. In order to be able
1706 to convert from any format to any format, the following constraints
1709 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1711 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1713 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1715 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1718 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1720 The other conversion functions are just optimizations for common cases.
1722 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1723 [PIX_FMT_YUV420P] = {
1724 [PIX_FMT_YUYV422] = {
1725 .convert = yuv420p_to_yuyv422,
1727 [PIX_FMT_RGB555] = {
1728 .convert = yuv420p_to_rgb555
1730 [PIX_FMT_RGB565] = {
1731 .convert = yuv420p_to_rgb565
1734 .convert = yuv420p_to_bgr24
1737 .convert = yuv420p_to_rgb24
1740 .convert = yuv420p_to_rgb32
1742 [PIX_FMT_UYVY422] = {
1743 .convert = yuv420p_to_uyvy422,
1746 [PIX_FMT_YUV422P] = {
1747 [PIX_FMT_YUYV422] = {
1748 .convert = yuv422p_to_yuyv422,
1750 [PIX_FMT_UYVY422] = {
1751 .convert = yuv422p_to_uyvy422,
1754 [PIX_FMT_YUV444P] = {
1756 .convert = yuv444p_to_rgb24
1759 [PIX_FMT_YUVJ420P] = {
1760 [PIX_FMT_RGB555] = {
1761 .convert = yuvj420p_to_rgb555
1763 [PIX_FMT_RGB565] = {
1764 .convert = yuvj420p_to_rgb565
1767 .convert = yuvj420p_to_bgr24
1770 .convert = yuvj420p_to_rgb24
1773 .convert = yuvj420p_to_rgb32
1776 [PIX_FMT_YUVJ444P] = {
1778 .convert = yuvj444p_to_rgb24
1781 [PIX_FMT_YUYV422] = {
1782 [PIX_FMT_YUV420P] = {
1783 .convert = yuyv422_to_yuv420p,
1785 [PIX_FMT_YUV422P] = {
1786 .convert = yuyv422_to_yuv422p,
1789 [PIX_FMT_UYVY422] = {
1790 [PIX_FMT_YUV420P] = {
1791 .convert = uyvy422_to_yuv420p,
1793 [PIX_FMT_YUV422P] = {
1794 .convert = uyvy422_to_yuv422p,
1798 [PIX_FMT_YUV420P] = {
1799 .convert = rgb24_to_yuv420p
1801 [PIX_FMT_RGB565] = {
1802 .convert = rgb24_to_rgb565
1804 [PIX_FMT_RGB555] = {
1805 .convert = rgb24_to_rgb555
1808 .convert = rgb24_to_rgb32
1811 .convert = rgb24_to_bgr24
1814 .convert = rgb24_to_gray
1817 .convert = rgb24_to_pal8
1819 [PIX_FMT_YUV444P] = {
1820 .convert = rgb24_to_yuv444p
1822 [PIX_FMT_YUVJ420P] = {
1823 .convert = rgb24_to_yuvj420p
1825 [PIX_FMT_YUVJ444P] = {
1826 .convert = rgb24_to_yuvj444p
1831 .convert = rgb32_to_rgb24
1834 .convert = rgb32_to_bgr24
1836 [PIX_FMT_RGB565] = {
1837 .convert = rgb32_to_rgb565
1839 [PIX_FMT_RGB555] = {
1840 .convert = rgb32_to_rgb555
1843 .convert = rgb32_to_pal8
1845 [PIX_FMT_YUV420P] = {
1846 .convert = rgb32_to_yuv420p
1849 .convert = rgb32_to_gray
1854 .convert = bgr24_to_rgb32
1857 .convert = bgr24_to_rgb24
1859 [PIX_FMT_YUV420P] = {
1860 .convert = bgr24_to_yuv420p
1863 .convert = bgr24_to_gray
1866 [PIX_FMT_RGB555] = {
1868 .convert = rgb555_to_rgb24
1871 .convert = rgb555_to_rgb32
1873 [PIX_FMT_YUV420P] = {
1874 .convert = rgb555_to_yuv420p
1877 .convert = rgb555_to_gray
1880 [PIX_FMT_RGB565] = {
1882 .convert = rgb565_to_rgb32
1885 .convert = rgb565_to_rgb24
1887 [PIX_FMT_YUV420P] = {
1888 .convert = rgb565_to_yuv420p
1891 .convert = rgb565_to_gray
1894 [PIX_FMT_GRAY16BE] = {
1896 .convert = gray16be_to_gray
1898 [PIX_FMT_GRAY16LE] = {
1899 .convert = gray16_to_gray16
1902 [PIX_FMT_GRAY16LE] = {
1904 .convert = gray16le_to_gray
1906 [PIX_FMT_GRAY16BE] = {
1907 .convert = gray16_to_gray16
1911 [PIX_FMT_RGB555] = {
1912 .convert = gray_to_rgb555
1914 [PIX_FMT_RGB565] = {
1915 .convert = gray_to_rgb565
1918 .convert = gray_to_rgb24
1921 .convert = gray_to_bgr24
1924 .convert = gray_to_rgb32
1926 [PIX_FMT_MONOWHITE] = {
1927 .convert = gray_to_monowhite
1929 [PIX_FMT_MONOBLACK] = {
1930 .convert = gray_to_monoblack
1932 [PIX_FMT_GRAY16LE] = {
1933 .convert = gray_to_gray16
1935 [PIX_FMT_GRAY16BE] = {
1936 .convert = gray_to_gray16
1939 [PIX_FMT_MONOWHITE] = {
1941 .convert = monowhite_to_gray
1944 [PIX_FMT_MONOBLACK] = {
1946 .convert = monoblack_to_gray
1950 [PIX_FMT_RGB555] = {
1951 .convert = pal8_to_rgb555
1953 [PIX_FMT_RGB565] = {
1954 .convert = pal8_to_rgb565
1957 .convert = pal8_to_bgr24
1960 .convert = pal8_to_rgb24
1963 .convert = pal8_to_rgb32
1966 [PIX_FMT_UYYVYY411] = {
1967 [PIX_FMT_YUV411P] = {
1968 .convert = uyyvyy411_to_yuv411p,
1974 int avpicture_alloc(AVPicture *picture,
1975 int pix_fmt, int width, int height)
1980 size = avpicture_get_size(pix_fmt, width, height);
1983 ptr = av_malloc(size);
1986 avpicture_fill(picture, ptr, pix_fmt, width, height);
1989 memset(picture, 0, sizeof(AVPicture));
1993 void avpicture_free(AVPicture *picture)
1995 av_free(picture->data[0]);
1998 /* return true if yuv planar */
1999 static inline int is_yuv_planar(const PixFmtInfo *ps)
2001 return (ps->color_type == FF_COLOR_YUV ||
2002 ps->color_type == FF_COLOR_YUV_JPEG) &&
2003 ps->pixel_type == FF_PIXEL_PLANAR;
2006 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2007 int pix_fmt, int top_band, int left_band)
2012 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2015 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2016 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2018 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2019 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2020 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2022 dst->linesize[0] = src->linesize[0];
2023 dst->linesize[1] = src->linesize[1];
2024 dst->linesize[2] = src->linesize[2];
2028 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2029 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2038 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2039 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2041 for (i = 0; i < 3; i++) {
2042 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2043 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2045 if (padtop || padleft) {
2046 memset(dst->data[i], color[i],
2047 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2050 if (padleft || padright) {
2051 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2052 (dst->linesize[i] - (padright >> x_shift));
2053 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2054 for (y = 0; y < yheight; y++) {
2055 memset(optr, color[i], (padleft + padright) >> x_shift);
2056 optr += dst->linesize[i];
2060 if (src) { /* first line */
2061 uint8_t *iptr = src->data[i];
2062 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2063 (padleft >> x_shift);
2064 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
2065 iptr += src->linesize[i];
2066 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2067 (dst->linesize[i] - (padright >> x_shift));
2068 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2069 for (y = 0; y < yheight; y++) {
2070 memset(optr, color[i], (padleft + padright) >> x_shift);
2071 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2072 (width - padleft - padright) >> x_shift);
2073 iptr += src->linesize[i];
2074 optr += dst->linesize[i];
2078 if (padbottom || padright) {
2079 optr = dst->data[i] + dst->linesize[i] *
2080 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2081 memset(optr, color[i],dst->linesize[i] *
2082 (padbottom >> y_shift) + (padright >> x_shift));
2088 #ifndef CONFIG_SWSCALE
2089 static uint8_t y_ccir_to_jpeg[256];
2090 static uint8_t y_jpeg_to_ccir[256];
2091 static uint8_t c_ccir_to_jpeg[256];
2092 static uint8_t c_jpeg_to_ccir[256];
2094 /* init various conversion tables */
2095 static void img_convert_init(void)
2098 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2100 for(i = 0;i < 256; i++) {
2101 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
2102 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
2103 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
2104 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
2108 /* apply to each pixel the given table */
2109 static void img_apply_table(uint8_t *dst, int dst_wrap,
2110 const uint8_t *src, int src_wrap,
2111 int width, int height, const uint8_t *table1)
2116 const uint8_t *table;
2119 for(;height > 0; height--) {
2143 /* XXX: use generic filter ? */
2144 /* XXX: in most cases, the sampling position is incorrect */
2147 static void shrink41(uint8_t *dst, int dst_wrap,
2148 const uint8_t *src, int src_wrap,
2149 int width, int height)
2155 for(;height > 0; height--) {
2158 for(w = width;w > 0; w--) {
2159 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
2169 static void shrink21(uint8_t *dst, int dst_wrap,
2170 const uint8_t *src, int src_wrap,
2171 int width, int height)
2177 for(;height > 0; height--) {
2180 for(w = width;w > 0; w--) {
2181 d[0] = (s[0] + s[1]) >> 1;
2191 static void shrink12(uint8_t *dst, int dst_wrap,
2192 const uint8_t *src, int src_wrap,
2193 int width, int height)
2197 const uint8_t *s1, *s2;
2199 for(;height > 0; height--) {
2203 for(w = width;w >= 4; w-=4) {
2204 d[0] = (s1[0] + s2[0]) >> 1;
2205 d[1] = (s1[1] + s2[1]) >> 1;
2206 d[2] = (s1[2] + s2[2]) >> 1;
2207 d[3] = (s1[3] + s2[3]) >> 1;
2213 d[0] = (s1[0] + s2[0]) >> 1;
2218 src += 2 * src_wrap;
2223 static void grow21_line(uint8_t *dst, const uint8_t *src,
2232 for(w = width;w >= 4; w-=4) {
2233 d[1] = d[0] = s1[0];
2234 d[3] = d[2] = s1[1];
2238 for(;w >= 2; w -= 2) {
2239 d[1] = d[0] = s1[0];
2243 /* only needed if width is not a multiple of two */
2244 /* XXX: veryfy that */
2250 static void grow41_line(uint8_t *dst, const uint8_t *src,
2259 for(w = width;w >= 4; w-=4) {
2271 static void grow21(uint8_t *dst, int dst_wrap,
2272 const uint8_t *src, int src_wrap,
2273 int width, int height)
2275 for(;height > 0; height--) {
2276 grow21_line(dst, src, width);
2283 static void grow12(uint8_t *dst, int dst_wrap,
2284 const uint8_t *src, int src_wrap,
2285 int width, int height)
2287 for(;height > 0; height-=2) {
2288 memcpy(dst, src, width);
2290 memcpy(dst, src, width);
2297 static void grow22(uint8_t *dst, int dst_wrap,
2298 const uint8_t *src, int src_wrap,
2299 int width, int height)
2301 for(;height > 0; height--) {
2302 grow21_line(dst, src, width);
2310 static void grow41(uint8_t *dst, int dst_wrap,
2311 const uint8_t *src, int src_wrap,
2312 int width, int height)
2314 for(;height > 0; height--) {
2315 grow41_line(dst, src, width);
2322 static void grow44(uint8_t *dst, int dst_wrap,
2323 const uint8_t *src, int src_wrap,
2324 int width, int height)
2326 for(;height > 0; height--) {
2327 grow41_line(dst, src, width);
2328 if ((height & 3) == 1)
2335 static void conv411(uint8_t *dst, int dst_wrap,
2336 const uint8_t *src, int src_wrap,
2337 int width, int height)
2340 const uint8_t *s1, *s2;
2345 for(;height > 0; height--) {
2347 s2 = src + src_wrap;
2349 for(w = width;w > 0; w--) {
2350 c = (s1[0] + s2[0]) >> 1;
2357 src += src_wrap * 2;
2362 /* XXX: always use linesize. Return -1 if not supported */
2363 int img_convert(AVPicture *dst, int dst_pix_fmt,
2364 const AVPicture *src, int src_pix_fmt,
2365 int src_width, int src_height)
2367 static int initialized;
2368 int i, ret, dst_width, dst_height, int_pix_fmt;
2369 const PixFmtInfo *src_pix, *dst_pix;
2370 const ConvertEntry *ce;
2371 AVPicture tmp1, *tmp = &tmp1;
2373 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2374 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2376 if (src_width <= 0 || src_height <= 0)
2384 dst_width = src_width;
2385 dst_height = src_height;
2387 dst_pix = &pix_fmt_info[dst_pix_fmt];
2388 src_pix = &pix_fmt_info[src_pix_fmt];
2389 if (src_pix_fmt == dst_pix_fmt) {
2390 /* no conversion needed: just copy */
2391 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2395 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2397 /* specific conversion routine */
2398 ce->convert(dst, src, dst_width, dst_height);
2403 if (is_yuv_planar(dst_pix) &&
2404 src_pix_fmt == PIX_FMT_GRAY8) {
2408 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2409 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2410 src->data[0], src->linesize[0],
2411 dst_width, dst_height);
2413 img_apply_table(dst->data[0], dst->linesize[0],
2414 src->data[0], src->linesize[0],
2415 dst_width, dst_height,
2418 /* fill U and V with 128 */
2421 w >>= dst_pix->x_chroma_shift;
2422 h >>= dst_pix->y_chroma_shift;
2423 for(i = 1; i <= 2; i++) {
2425 for(y = 0; y< h; y++) {
2427 d += dst->linesize[i];
2434 if (is_yuv_planar(src_pix) &&
2435 dst_pix_fmt == PIX_FMT_GRAY8) {
2436 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2437 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2438 src->data[0], src->linesize[0],
2439 dst_width, dst_height);
2441 img_apply_table(dst->data[0], dst->linesize[0],
2442 src->data[0], src->linesize[0],
2443 dst_width, dst_height,
2449 /* YUV to YUV planar */
2450 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2451 int x_shift, y_shift, w, h, xy_shift;
2452 void (*resize_func)(uint8_t *dst, int dst_wrap,
2453 const uint8_t *src, int src_wrap,
2454 int width, int height);
2456 /* compute chroma size of the smallest dimensions */
2459 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2460 w >>= dst_pix->x_chroma_shift;
2462 w >>= src_pix->x_chroma_shift;
2463 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2464 h >>= dst_pix->y_chroma_shift;
2466 h >>= src_pix->y_chroma_shift;
2468 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2469 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2470 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2471 /* there must be filters for conversion at least from and to
2475 resize_func = ff_img_copy_plane;
2478 resize_func = shrink21;
2481 resize_func = shrink41;
2484 resize_func = shrink12;
2487 resize_func = ff_shrink22;
2490 resize_func = ff_shrink44;
2493 resize_func = grow21;
2496 resize_func = grow12;
2499 resize_func = grow41;
2502 resize_func = grow22;
2505 resize_func = grow44;
2508 resize_func = conv411;
2511 /* currently not handled */
2512 goto no_chroma_filter;
2515 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2516 src->data[0], src->linesize[0],
2517 dst_width, dst_height);
2519 for(i = 1;i <= 2; i++)
2520 resize_func(dst->data[i], dst->linesize[i],
2521 src->data[i], src->linesize[i],
2522 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2523 /* if yuv color space conversion is needed, we do it here on
2524 the destination image */
2525 if (dst_pix->color_type != src_pix->color_type) {
2526 const uint8_t *y_table, *c_table;
2527 if (dst_pix->color_type == FF_COLOR_YUV) {
2528 y_table = y_jpeg_to_ccir;
2529 c_table = c_jpeg_to_ccir;
2531 y_table = y_ccir_to_jpeg;
2532 c_table = c_ccir_to_jpeg;
2534 img_apply_table(dst->data[0], dst->linesize[0],
2535 dst->data[0], dst->linesize[0],
2536 dst_width, dst_height,
2539 for(i = 1;i <= 2; i++)
2540 img_apply_table(dst->data[i], dst->linesize[i],
2541 dst->data[i], dst->linesize[i],
2542 dst_width>>dst_pix->x_chroma_shift,
2543 dst_height>>dst_pix->y_chroma_shift,
2550 /* try to use an intermediate format */
2551 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2552 dst_pix_fmt == PIX_FMT_YUYV422) {
2553 /* specific case: convert to YUV422P first */
2554 int_pix_fmt = PIX_FMT_YUV422P;
2555 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2556 dst_pix_fmt == PIX_FMT_UYVY422) {
2557 /* specific case: convert to YUV422P first */
2558 int_pix_fmt = PIX_FMT_YUV422P;
2559 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2560 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2561 /* specific case: convert to YUV411P first */
2562 int_pix_fmt = PIX_FMT_YUV411P;
2563 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2564 src_pix_fmt != PIX_FMT_GRAY8) ||
2565 (dst_pix->color_type == FF_COLOR_GRAY &&
2566 dst_pix_fmt != PIX_FMT_GRAY8)) {
2567 /* gray8 is the normalized format */
2568 int_pix_fmt = PIX_FMT_GRAY8;
2569 } else if ((is_yuv_planar(src_pix) &&
2570 src_pix_fmt != PIX_FMT_YUV444P &&
2571 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2572 /* yuv444 is the normalized format */
2573 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2574 int_pix_fmt = PIX_FMT_YUVJ444P;
2576 int_pix_fmt = PIX_FMT_YUV444P;
2577 } else if ((is_yuv_planar(dst_pix) &&
2578 dst_pix_fmt != PIX_FMT_YUV444P &&
2579 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2580 /* yuv444 is the normalized format */
2581 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2582 int_pix_fmt = PIX_FMT_YUVJ444P;
2584 int_pix_fmt = PIX_FMT_YUV444P;
2586 /* the two formats are rgb or gray8 or yuv[j]444p */
2587 if (src_pix->is_alpha && dst_pix->is_alpha)
2588 int_pix_fmt = PIX_FMT_RGB32;
2590 int_pix_fmt = PIX_FMT_RGB24;
2592 if (src_pix_fmt == int_pix_fmt)
2594 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2597 if (img_convert(tmp, int_pix_fmt,
2598 src, src_pix_fmt, src_width, src_height) < 0)
2600 if (img_convert(dst, dst_pix_fmt,
2601 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2605 avpicture_free(tmp);
2610 /* NOTE: we scan all the pixels to have an exact information */
2611 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2613 const unsigned char *p;
2614 int src_wrap, ret, x, y;
2616 uint32_t *palette = (uint32_t *)src->data[1];
2619 src_wrap = src->linesize[0] - width;
2621 for(y=0;y<height;y++) {
2622 for(x=0;x<width;x++) {
2623 a = palette[p[0]] >> 24;
2625 ret |= FF_ALPHA_TRANSP;
2626 } else if (a != 0xff) {
2627 ret |= FF_ALPHA_SEMI_TRANSP;
2636 int img_get_alpha_info(const AVPicture *src,
2637 int pix_fmt, int width, int height)
2639 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2642 pf = &pix_fmt_info[pix_fmt];
2643 /* no alpha can be represented in format */
2648 ret = get_alpha_info_rgb32(src, width, height);
2651 ret = get_alpha_info_pal8(src, width, height);
2654 /* we do not know, so everything is indicated */
2655 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2662 #define DEINT_INPLACE_LINE_LUM \
2663 movd_m2r(lum_m4[0],mm0);\
2664 movd_m2r(lum_m3[0],mm1);\
2665 movd_m2r(lum_m2[0],mm2);\
2666 movd_m2r(lum_m1[0],mm3);\
2667 movd_m2r(lum[0],mm4);\
2668 punpcklbw_r2r(mm7,mm0);\
2669 movd_r2m(mm2,lum_m4[0]);\
2670 punpcklbw_r2r(mm7,mm1);\
2671 punpcklbw_r2r(mm7,mm2);\
2672 punpcklbw_r2r(mm7,mm3);\
2673 punpcklbw_r2r(mm7,mm4);\
2674 paddw_r2r(mm3,mm1);\
2676 paddw_r2r(mm4,mm0);\
2678 paddw_r2r(mm6,mm2);\
2679 paddw_r2r(mm2,mm1);\
2680 psubusw_r2r(mm0,mm1);\
2682 packuswb_r2r(mm7,mm1);\
2683 movd_r2m(mm1,lum_m2[0]);
2685 #define DEINT_LINE_LUM \
2686 movd_m2r(lum_m4[0],mm0);\
2687 movd_m2r(lum_m3[0],mm1);\
2688 movd_m2r(lum_m2[0],mm2);\
2689 movd_m2r(lum_m1[0],mm3);\
2690 movd_m2r(lum[0],mm4);\
2691 punpcklbw_r2r(mm7,mm0);\
2692 punpcklbw_r2r(mm7,mm1);\
2693 punpcklbw_r2r(mm7,mm2);\
2694 punpcklbw_r2r(mm7,mm3);\
2695 punpcklbw_r2r(mm7,mm4);\
2696 paddw_r2r(mm3,mm1);\
2698 paddw_r2r(mm4,mm0);\
2700 paddw_r2r(mm6,mm2);\
2701 paddw_r2r(mm2,mm1);\
2702 psubusw_r2r(mm0,mm1);\
2704 packuswb_r2r(mm7,mm1);\
2705 movd_r2m(mm1,dst[0]);
2708 /* filter parameters: [-1 4 2 4 -1] // 8 */
2709 static void deinterlace_line(uint8_t *dst,
2710 const uint8_t *lum_m4, const uint8_t *lum_m3,
2711 const uint8_t *lum_m2, const uint8_t *lum_m1,
2716 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2719 for(;size > 0;size--) {
2721 sum += lum_m3[0] << 2;
2722 sum += lum_m2[0] << 1;
2723 sum += lum_m1[0] << 2;
2725 dst[0] = cm[(sum + 4) >> 3];
2742 movq_m2r(rounder,mm6);
2744 for (;size > 3; size-=4) {
2755 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2759 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2762 for(;size > 0;size--) {
2764 sum += lum_m3[0] << 2;
2765 sum += lum_m2[0] << 1;
2766 lum_m4[0]=lum_m2[0];
2767 sum += lum_m1[0] << 2;
2769 lum_m2[0] = cm[(sum + 4) >> 3];
2785 movq_m2r(rounder,mm6);
2787 for (;size > 3; size-=4) {
2788 DEINT_INPLACE_LINE_LUM
2798 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2799 top field is copied as is, but the bottom field is deinterlaced
2800 against the top field. */
2801 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2802 const uint8_t *src1, int src_wrap,
2803 int width, int height)
2805 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2810 src_0=&src_m1[src_wrap];
2811 src_p1=&src_0[src_wrap];
2812 src_p2=&src_p1[src_wrap];
2813 for(y=0;y<(height-2);y+=2) {
2814 memcpy(dst,src_m1,width);
2816 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2820 src_p1 += 2*src_wrap;
2821 src_p2 += 2*src_wrap;
2824 memcpy(dst,src_m1,width);
2827 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2830 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2831 int width, int height)
2833 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2836 buf = (uint8_t*)av_malloc(width);
2839 memcpy(buf,src_m1,width);
2840 src_0=&src_m1[src_wrap];
2841 src_p1=&src_0[src_wrap];
2842 src_p2=&src_p1[src_wrap];
2843 for(y=0;y<(height-2);y+=2) {
2844 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2847 src_p1 += 2*src_wrap;
2848 src_p2 += 2*src_wrap;
2851 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2855 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2856 int pix_fmt, int width, int height)
2860 if (pix_fmt != PIX_FMT_YUV420P &&
2861 pix_fmt != PIX_FMT_YUV422P &&
2862 pix_fmt != PIX_FMT_YUV444P &&
2863 pix_fmt != PIX_FMT_YUV411P &&
2864 pix_fmt != PIX_FMT_GRAY8)
2866 if ((width & 3) != 0 || (height & 3) != 0)
2872 case PIX_FMT_YUV420P:
2876 case PIX_FMT_YUV422P:
2879 case PIX_FMT_YUV411P:
2885 if (pix_fmt == PIX_FMT_GRAY8) {
2890 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2893 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2894 src->data[i], src->linesize[i],