2 * Misc image conversion routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * misc image conversion routines
28 * - write 'ffimg' program to test all the image related stuff
29 * - move all api to slice based system
30 * - integrate deinterlacing, postprocessing and scaling in the conversion process
35 #include "colorspace.h"
41 #define xglue(x, y) x ## y
42 #define glue(x, y) xglue(x, y)
44 #define FF_COLOR_RGB 0 /**< RGB color space */
45 #define FF_COLOR_GRAY 1 /**< gray color space */
46 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
47 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
49 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
50 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
51 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
53 typedef struct PixFmtInfo {
55 uint8_t nb_channels; /**< number of channels (including alpha) */
56 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
57 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
58 uint8_t is_alpha : 1; /**< true if alpha can be specified */
59 uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
60 uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
61 uint8_t depth; /**< bit depth of the color components */
64 /* this table gives more information about formats */
65 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
70 .color_type = FF_COLOR_YUV,
71 .pixel_type = FF_PIXEL_PLANAR,
73 .x_chroma_shift = 1, .y_chroma_shift = 1,
78 .color_type = FF_COLOR_YUV,
79 .pixel_type = FF_PIXEL_PLANAR,
81 .x_chroma_shift = 1, .y_chroma_shift = 0,
86 .color_type = FF_COLOR_YUV,
87 .pixel_type = FF_PIXEL_PLANAR,
89 .x_chroma_shift = 0, .y_chroma_shift = 0,
94 .color_type = FF_COLOR_YUV,
95 .pixel_type = FF_PIXEL_PACKED,
97 .x_chroma_shift = 1, .y_chroma_shift = 0,
102 .color_type = FF_COLOR_YUV,
103 .pixel_type = FF_PIXEL_PACKED,
105 .x_chroma_shift = 1, .y_chroma_shift = 0,
107 [PIX_FMT_YUV410P] = {
110 .color_type = FF_COLOR_YUV,
111 .pixel_type = FF_PIXEL_PLANAR,
113 .x_chroma_shift = 2, .y_chroma_shift = 2,
115 [PIX_FMT_YUV411P] = {
118 .color_type = FF_COLOR_YUV,
119 .pixel_type = FF_PIXEL_PLANAR,
121 .x_chroma_shift = 2, .y_chroma_shift = 0,
123 [PIX_FMT_YUV440P] = {
126 .color_type = FF_COLOR_YUV,
127 .pixel_type = FF_PIXEL_PLANAR,
129 .x_chroma_shift = 0, .y_chroma_shift = 1,
132 /* YUV formats with alpha plane */
133 [PIX_FMT_YUVA420P] = {
136 .color_type = FF_COLOR_YUV,
137 .pixel_type = FF_PIXEL_PLANAR,
139 .x_chroma_shift = 1, .y_chroma_shift = 1,
143 [PIX_FMT_YUVJ420P] = {
146 .color_type = FF_COLOR_YUV_JPEG,
147 .pixel_type = FF_PIXEL_PLANAR,
149 .x_chroma_shift = 1, .y_chroma_shift = 1,
151 [PIX_FMT_YUVJ422P] = {
154 .color_type = FF_COLOR_YUV_JPEG,
155 .pixel_type = FF_PIXEL_PLANAR,
157 .x_chroma_shift = 1, .y_chroma_shift = 0,
159 [PIX_FMT_YUVJ444P] = {
162 .color_type = FF_COLOR_YUV_JPEG,
163 .pixel_type = FF_PIXEL_PLANAR,
165 .x_chroma_shift = 0, .y_chroma_shift = 0,
167 [PIX_FMT_YUVJ440P] = {
170 .color_type = FF_COLOR_YUV_JPEG,
171 .pixel_type = FF_PIXEL_PLANAR,
173 .x_chroma_shift = 0, .y_chroma_shift = 1,
180 .color_type = FF_COLOR_RGB,
181 .pixel_type = FF_PIXEL_PACKED,
183 .x_chroma_shift = 0, .y_chroma_shift = 0,
188 .color_type = FF_COLOR_RGB,
189 .pixel_type = FF_PIXEL_PACKED,
191 .x_chroma_shift = 0, .y_chroma_shift = 0,
195 .nb_channels = 4, .is_alpha = 1,
196 .color_type = FF_COLOR_RGB,
197 .pixel_type = FF_PIXEL_PACKED,
199 .x_chroma_shift = 0, .y_chroma_shift = 0,
204 .color_type = FF_COLOR_RGB,
205 .pixel_type = FF_PIXEL_PACKED,
207 .x_chroma_shift = 0, .y_chroma_shift = 0,
212 .color_type = FF_COLOR_RGB,
213 .pixel_type = FF_PIXEL_PACKED,
215 .x_chroma_shift = 0, .y_chroma_shift = 0,
218 /* gray / mono formats */
219 [PIX_FMT_GRAY16BE] = {
222 .color_type = FF_COLOR_GRAY,
223 .pixel_type = FF_PIXEL_PLANAR,
226 [PIX_FMT_GRAY16LE] = {
229 .color_type = FF_COLOR_GRAY,
230 .pixel_type = FF_PIXEL_PLANAR,
236 .color_type = FF_COLOR_GRAY,
237 .pixel_type = FF_PIXEL_PLANAR,
240 [PIX_FMT_MONOWHITE] = {
243 .color_type = FF_COLOR_GRAY,
244 .pixel_type = FF_PIXEL_PLANAR,
247 [PIX_FMT_MONOBLACK] = {
250 .color_type = FF_COLOR_GRAY,
251 .pixel_type = FF_PIXEL_PLANAR,
255 /* paletted formats */
258 .nb_channels = 4, .is_alpha = 1,
259 .color_type = FF_COLOR_RGB,
260 .pixel_type = FF_PIXEL_PALETTE,
263 [PIX_FMT_XVMC_MPEG2_MC] = {
266 [PIX_FMT_XVMC_MPEG2_IDCT] = {
269 [PIX_FMT_UYYVYY411] = {
272 .color_type = FF_COLOR_YUV,
273 .pixel_type = FF_PIXEL_PACKED,
275 .x_chroma_shift = 2, .y_chroma_shift = 0,
279 .nb_channels = 4, .is_alpha = 1,
280 .color_type = FF_COLOR_RGB,
281 .pixel_type = FF_PIXEL_PACKED,
283 .x_chroma_shift = 0, .y_chroma_shift = 0,
288 .color_type = FF_COLOR_RGB,
289 .pixel_type = FF_PIXEL_PACKED,
291 .x_chroma_shift = 0, .y_chroma_shift = 0,
296 .color_type = FF_COLOR_RGB,
297 .pixel_type = FF_PIXEL_PACKED,
299 .x_chroma_shift = 0, .y_chroma_shift = 0,
304 .color_type = FF_COLOR_RGB,
305 .pixel_type = FF_PIXEL_PACKED,
307 .x_chroma_shift = 0, .y_chroma_shift = 0,
312 .color_type = FF_COLOR_RGB,
313 .pixel_type = FF_PIXEL_PACKED,
315 .x_chroma_shift = 0, .y_chroma_shift = 0,
317 [PIX_FMT_RGB4_BYTE] = {
320 .color_type = FF_COLOR_RGB,
321 .pixel_type = FF_PIXEL_PACKED,
323 .x_chroma_shift = 0, .y_chroma_shift = 0,
328 .color_type = FF_COLOR_RGB,
329 .pixel_type = FF_PIXEL_PACKED,
331 .x_chroma_shift = 0, .y_chroma_shift = 0,
336 .color_type = FF_COLOR_RGB,
337 .pixel_type = FF_PIXEL_PACKED,
339 .x_chroma_shift = 0, .y_chroma_shift = 0,
341 [PIX_FMT_BGR4_BYTE] = {
344 .color_type = FF_COLOR_RGB,
345 .pixel_type = FF_PIXEL_PACKED,
347 .x_chroma_shift = 0, .y_chroma_shift = 0,
352 .color_type = FF_COLOR_YUV,
353 .pixel_type = FF_PIXEL_PLANAR,
355 .x_chroma_shift = 1, .y_chroma_shift = 1,
360 .color_type = FF_COLOR_YUV,
361 .pixel_type = FF_PIXEL_PLANAR,
363 .x_chroma_shift = 1, .y_chroma_shift = 1,
366 [PIX_FMT_BGR32_1] = {
368 .nb_channels = 4, .is_alpha = 1,
369 .color_type = FF_COLOR_RGB,
370 .pixel_type = FF_PIXEL_PACKED,
372 .x_chroma_shift = 0, .y_chroma_shift = 0,
374 [PIX_FMT_RGB32_1] = {
376 .nb_channels = 4, .is_alpha = 1,
377 .color_type = FF_COLOR_RGB,
378 .pixel_type = FF_PIXEL_PACKED,
380 .x_chroma_shift = 0, .y_chroma_shift = 0,
384 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
386 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
387 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
390 const char *avcodec_get_pix_fmt_name(int pix_fmt)
392 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
395 return pix_fmt_info[pix_fmt].name;
398 enum PixelFormat avcodec_get_pix_fmt(const char* name)
402 for (i=0; i < PIX_FMT_NB; i++)
403 if (!strcmp(pix_fmt_info[i].name, name))
408 void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
410 PixFmtInfo info= pix_fmt_info[pix_fmt];
412 char is_alpha_char= info.is_alpha ? 'y' : 'n';
416 snprintf (buf, buf_size,
417 "name " " nb_channels" " depth" " is_alpha"
420 snprintf (buf, buf_size,
421 "%-10s" " %1d " " %2d " " %c ",
429 int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
432 const PixFmtInfo *pinfo;
434 memset(picture->linesize, 0, sizeof(picture->linesize));
436 pinfo = &pix_fmt_info[pix_fmt];
438 case PIX_FMT_YUV420P:
439 case PIX_FMT_YUV422P:
440 case PIX_FMT_YUV444P:
441 case PIX_FMT_YUV410P:
442 case PIX_FMT_YUV411P:
443 case PIX_FMT_YUV440P:
444 case PIX_FMT_YUVJ420P:
445 case PIX_FMT_YUVJ422P:
446 case PIX_FMT_YUVJ444P:
447 case PIX_FMT_YUVJ440P:
448 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
449 picture->linesize[0] = width;
450 picture->linesize[1] = w2;
451 picture->linesize[2] = w2;
453 case PIX_FMT_YUVA420P:
454 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
455 picture->linesize[0] = width;
456 picture->linesize[1] = w2;
457 picture->linesize[2] = w2;
458 picture->linesize[3] = width;
462 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
463 picture->linesize[0] = width;
464 picture->linesize[1] = w2;
468 picture->linesize[0] = width * 3;
472 case PIX_FMT_RGB32_1:
473 case PIX_FMT_BGR32_1:
474 picture->linesize[0] = width * 4;
476 case PIX_FMT_GRAY16BE:
477 case PIX_FMT_GRAY16LE:
482 case PIX_FMT_YUYV422:
483 picture->linesize[0] = width * 2;
485 case PIX_FMT_UYVY422:
486 picture->linesize[0] = width * 2;
488 case PIX_FMT_UYYVYY411:
489 picture->linesize[0] = width + width/2;
493 case PIX_FMT_RGB4_BYTE:
494 case PIX_FMT_BGR4_BYTE:
496 picture->linesize[0] = width;
500 picture->linesize[0] = width / 2;
502 case PIX_FMT_MONOWHITE:
503 case PIX_FMT_MONOBLACK:
504 picture->linesize[0] = (width + 7) >> 3;
507 picture->linesize[0] = width;
508 picture->linesize[1] = 4;
516 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
520 const PixFmtInfo *pinfo;
522 pinfo = &pix_fmt_info[pix_fmt];
523 size = picture->linesize[0] * height;
525 case PIX_FMT_YUV420P:
526 case PIX_FMT_YUV422P:
527 case PIX_FMT_YUV444P:
528 case PIX_FMT_YUV410P:
529 case PIX_FMT_YUV411P:
530 case PIX_FMT_YUV440P:
531 case PIX_FMT_YUVJ420P:
532 case PIX_FMT_YUVJ422P:
533 case PIX_FMT_YUVJ444P:
534 case PIX_FMT_YUVJ440P:
535 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
536 size2 = picture->linesize[1] * h2;
537 picture->data[0] = ptr;
538 picture->data[1] = picture->data[0] + size;
539 picture->data[2] = picture->data[1] + size2;
540 picture->data[3] = NULL;
541 return size + 2 * size2;
542 case PIX_FMT_YUVA420P:
543 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
544 size2 = picture->linesize[1] * h2;
545 picture->data[0] = ptr;
546 picture->data[1] = picture->data[0] + size;
547 picture->data[2] = picture->data[1] + size2;
548 picture->data[3] = picture->data[1] + size2 + size2;
549 return 2 * size + 2 * size2;
552 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
553 size2 = picture->linesize[1] * h2 * 2;
554 picture->data[0] = ptr;
555 picture->data[1] = picture->data[0] + size;
556 picture->data[2] = NULL;
557 picture->data[3] = NULL;
558 return size + 2 * size2;
563 case PIX_FMT_RGB32_1:
564 case PIX_FMT_BGR32_1:
565 case PIX_FMT_GRAY16BE:
566 case PIX_FMT_GRAY16LE:
571 case PIX_FMT_YUYV422:
572 case PIX_FMT_UYVY422:
573 case PIX_FMT_UYYVYY411:
576 case PIX_FMT_RGB4_BYTE:
577 case PIX_FMT_BGR4_BYTE:
581 case PIX_FMT_MONOWHITE:
582 case PIX_FMT_MONOBLACK:
583 picture->data[0] = ptr;
584 picture->data[1] = NULL;
585 picture->data[2] = NULL;
586 picture->data[3] = NULL;
589 size2 = (size + 3) & ~3;
590 picture->data[0] = ptr;
591 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
592 picture->data[2] = NULL;
593 picture->data[3] = NULL;
594 return size2 + 256 * 4;
596 picture->data[0] = NULL;
597 picture->data[1] = NULL;
598 picture->data[2] = NULL;
599 picture->data[3] = NULL;
604 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
605 int pix_fmt, int width, int height)
608 if(avcodec_check_dimensions(NULL, width, height))
611 if (ff_fill_linesize(picture, pix_fmt, width))
614 return ff_fill_pointer(picture, ptr, pix_fmt, height);
617 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
618 unsigned char *dest, int dest_size)
620 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
621 int i, j, w, h, data_planes;
622 const unsigned char* s;
623 int size = avpicture_get_size(pix_fmt, width, height);
625 if (size > dest_size || size < 0)
628 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
629 if (pix_fmt == PIX_FMT_YUYV422 ||
630 pix_fmt == PIX_FMT_UYVY422 ||
631 pix_fmt == PIX_FMT_BGR565 ||
632 pix_fmt == PIX_FMT_BGR555 ||
633 pix_fmt == PIX_FMT_RGB565 ||
634 pix_fmt == PIX_FMT_RGB555)
636 else if (pix_fmt == PIX_FMT_UYYVYY411)
638 else if (pix_fmt == PIX_FMT_PAL8)
641 w = width * (pf->depth * pf->nb_channels / 8);
646 data_planes = pf->nb_channels;
647 w = (width*pf->depth + 7)/8;
651 for (i=0; i<data_planes; i++) {
653 w = width >> pf->x_chroma_shift;
654 h = height >> pf->y_chroma_shift;
660 s += src->linesize[i];
664 if (pf->pixel_type == FF_PIXEL_PALETTE)
665 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
670 int avpicture_get_size(int pix_fmt, int width, int height)
672 AVPicture dummy_pict;
673 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
676 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
679 const PixFmtInfo *pf, *ps;
682 ps = &pix_fmt_info[src_pix_fmt];
683 pf = &pix_fmt_info[dst_pix_fmt];
687 pf = &pix_fmt_info[dst_pix_fmt];
688 if (pf->depth < ps->depth ||
689 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
690 loss |= FF_LOSS_DEPTH;
691 if (pf->x_chroma_shift > ps->x_chroma_shift ||
692 pf->y_chroma_shift > ps->y_chroma_shift)
693 loss |= FF_LOSS_RESOLUTION;
694 switch(pf->color_type) {
696 if (ps->color_type != FF_COLOR_RGB &&
697 ps->color_type != FF_COLOR_GRAY)
698 loss |= FF_LOSS_COLORSPACE;
701 if (ps->color_type != FF_COLOR_GRAY)
702 loss |= FF_LOSS_COLORSPACE;
705 if (ps->color_type != FF_COLOR_YUV)
706 loss |= FF_LOSS_COLORSPACE;
708 case FF_COLOR_YUV_JPEG:
709 if (ps->color_type != FF_COLOR_YUV_JPEG &&
710 ps->color_type != FF_COLOR_YUV &&
711 ps->color_type != FF_COLOR_GRAY)
712 loss |= FF_LOSS_COLORSPACE;
716 if (ps->color_type != pf->color_type)
717 loss |= FF_LOSS_COLORSPACE;
720 if (pf->color_type == FF_COLOR_GRAY &&
721 ps->color_type != FF_COLOR_GRAY)
722 loss |= FF_LOSS_CHROMA;
723 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
724 loss |= FF_LOSS_ALPHA;
725 if (pf->pixel_type == FF_PIXEL_PALETTE &&
726 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
727 loss |= FF_LOSS_COLORQUANT;
731 static int avg_bits_per_pixel(int pix_fmt)
734 const PixFmtInfo *pf;
736 pf = &pix_fmt_info[pix_fmt];
737 switch(pf->pixel_type) {
738 case FF_PIXEL_PACKED:
740 case PIX_FMT_YUYV422:
741 case PIX_FMT_UYVY422:
748 case PIX_FMT_UYYVYY411:
752 bits = pf->depth * pf->nb_channels;
756 case FF_PIXEL_PLANAR:
757 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
758 bits = pf->depth * pf->nb_channels;
760 bits = pf->depth + ((2 * pf->depth) >>
761 (pf->x_chroma_shift + pf->y_chroma_shift));
764 case FF_PIXEL_PALETTE:
774 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
779 int dist, i, loss, min_dist, dst_pix_fmt;
781 /* find exact color match with smallest size */
783 min_dist = 0x7fffffff;
784 for(i = 0;i < PIX_FMT_NB; i++) {
785 if (pix_fmt_mask & (1 << i)) {
786 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
788 dist = avg_bits_per_pixel(i);
789 if (dist < min_dist) {
799 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
800 int has_alpha, int *loss_ptr)
802 int dst_pix_fmt, loss_mask, i;
803 static const int loss_mask_order[] = {
804 ~0, /* no loss first */
807 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
813 /* try with successive loss */
816 loss_mask = loss_mask_order[i++];
817 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
818 has_alpha, loss_mask);
819 if (dst_pix_fmt >= 0)
827 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
831 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
832 const uint8_t *src, int src_wrap,
833 int width, int height)
837 for(;height > 0; height--) {
838 memcpy(dst, src, width);
844 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
847 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
849 pf = &pix_fmt_info[pix_fmt];
850 switch(pf->pixel_type) {
851 case FF_PIXEL_PACKED:
853 case PIX_FMT_YUYV422:
854 case PIX_FMT_UYVY422:
861 case PIX_FMT_UYYVYY411:
865 bits = pf->depth * pf->nb_channels;
868 return (width * bits + 7) >> 3;
870 case FF_PIXEL_PLANAR:
871 if (plane == 1 || plane == 2)
872 width >>= pf->x_chroma_shift;
874 return (width * pf->depth + 7) >> 3;
876 case FF_PIXEL_PALETTE:
885 void av_picture_copy(AVPicture *dst, const AVPicture *src,
886 int pix_fmt, int width, int height)
889 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
891 pf = &pix_fmt_info[pix_fmt];
892 switch(pf->pixel_type) {
893 case FF_PIXEL_PACKED:
894 case FF_PIXEL_PLANAR:
895 for(i = 0; i < pf->nb_channels; i++) {
897 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
900 if (i == 1 || i == 2) {
901 w >>= pf->x_chroma_shift;
902 h >>= pf->y_chroma_shift;
904 ff_img_copy_plane(dst->data[i], dst->linesize[i],
905 src->data[i], src->linesize[i],
909 case FF_PIXEL_PALETTE:
910 ff_img_copy_plane(dst->data[0], dst->linesize[0],
911 src->data[0], src->linesize[0],
913 /* copy the palette */
914 ff_img_copy_plane(dst->data[1], dst->linesize[1],
915 src->data[1], src->linesize[1],
921 /* XXX: totally non optimized */
923 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
924 int width, int height)
926 const uint8_t *p, *p1;
927 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
935 for(;height >= 1; height -= 2) {
940 for(w = width; w >= 2; w -= 2) {
957 p1 += src->linesize[0];
958 lum1 += dst->linesize[0];
962 for(w = width; w >= 2; w -= 2) {
971 p1 += src->linesize[0];
972 lum1 += dst->linesize[0];
974 cb1 += dst->linesize[1];
975 cr1 += dst->linesize[2];
979 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
980 int width, int height)
982 const uint8_t *p, *p1;
983 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
992 for(;height >= 1; height -= 2) {
997 for(w = width; w >= 2; w -= 2) {
1014 p1 += src->linesize[0];
1015 lum1 += dst->linesize[0];
1019 for(w = width; w >= 2; w -= 2) {
1028 p1 += src->linesize[0];
1029 lum1 += dst->linesize[0];
1031 cb1 += dst->linesize[1];
1032 cr1 += dst->linesize[2];
1037 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1038 int width, int height)
1040 const uint8_t *p, *p1;
1041 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1045 lum1 = dst->data[0];
1048 for(;height > 0; height--) {
1053 for(w = width; w >= 2; w -= 2) {
1063 p1 += src->linesize[0];
1064 lum1 += dst->linesize[0];
1065 cb1 += dst->linesize[1];
1066 cr1 += dst->linesize[2];
1071 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1072 int width, int height)
1074 const uint8_t *p, *p1;
1075 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1079 lum1 = dst->data[0];
1082 for(;height > 0; height--) {
1087 for(w = width; w >= 2; w -= 2) {
1097 p1 += src->linesize[0];
1098 lum1 += dst->linesize[0];
1099 cb1 += dst->linesize[1];
1100 cr1 += dst->linesize[2];
1104 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1105 int width, int height)
1108 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1112 lum1 = src->data[0];
1115 for(;height > 0; height--) {
1120 for(w = width; w >= 2; w -= 2) {
1130 p1 += dst->linesize[0];
1131 lum1 += src->linesize[0];
1132 cb1 += src->linesize[1];
1133 cr1 += src->linesize[2];
1137 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1138 int width, int height)
1141 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1145 lum1 = src->data[0];
1148 for(;height > 0; height--) {
1153 for(w = width; w >= 2; w -= 2) {
1163 p1 += dst->linesize[0];
1164 lum1 += src->linesize[0];
1165 cb1 += src->linesize[1];
1166 cr1 += src->linesize[2];
1170 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1171 int width, int height)
1173 const uint8_t *p, *p1;
1174 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1178 lum1 = dst->data[0];
1181 for(;height > 0; height--) {
1186 for(w = width; w >= 4; w -= 4) {
1198 p1 += src->linesize[0];
1199 lum1 += dst->linesize[0];
1200 cb1 += dst->linesize[1];
1201 cr1 += dst->linesize[2];
1206 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1207 int width, int height)
1210 uint8_t *line1, *line2, *linesrc = dst->data[0];
1211 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1212 uint8_t *cb1, *cb2 = src->data[1];
1213 uint8_t *cr1, *cr2 = src->data[2];
1215 for(h = height / 2; h--;) {
1217 line2 = linesrc + dst->linesize[0];
1220 lum2 = lumsrc + src->linesize[0];
1225 for(w = width / 2; w--;) {
1226 *line1++ = *lum1++; *line2++ = *lum2++;
1227 *line1++ = *line2++ = *cb1++;
1228 *line1++ = *lum1++; *line2++ = *lum2++;
1229 *line1++ = *line2++ = *cr1++;
1232 linesrc += dst->linesize[0] * 2;
1233 lumsrc += src->linesize[0] * 2;
1234 cb2 += src->linesize[1];
1235 cr2 += src->linesize[2];
1239 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1240 int width, int height)
1243 uint8_t *line1, *line2, *linesrc = dst->data[0];
1244 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1245 uint8_t *cb1, *cb2 = src->data[1];
1246 uint8_t *cr1, *cr2 = src->data[2];
1248 for(h = height / 2; h--;) {
1250 line2 = linesrc + dst->linesize[0];
1253 lum2 = lumsrc + src->linesize[0];
1258 for(w = width / 2; w--;) {
1259 *line1++ = *line2++ = *cb1++;
1260 *line1++ = *lum1++; *line2++ = *lum2++;
1261 *line1++ = *line2++ = *cr1++;
1262 *line1++ = *lum1++; *line2++ = *lum2++;
1265 linesrc += dst->linesize[0] * 2;
1266 lumsrc += src->linesize[0] * 2;
1267 cb2 += src->linesize[1];
1268 cr2 += src->linesize[2];
1273 void ff_shrink22(uint8_t *dst, int dst_wrap,
1274 const uint8_t *src, int src_wrap,
1275 int width, int height)
1278 const uint8_t *s1, *s2;
1281 for(;height > 0; height--) {
1285 for(w = width;w >= 4; w-=4) {
1286 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1287 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1288 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1289 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1295 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1300 src += 2 * src_wrap;
1306 void ff_shrink44(uint8_t *dst, int dst_wrap,
1307 const uint8_t *src, int src_wrap,
1308 int width, int height)
1311 const uint8_t *s1, *s2, *s3, *s4;
1314 for(;height > 0; height--) {
1320 for(w = width;w > 0; w--) {
1321 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1322 s2[0] + s2[1] + s2[2] + s2[3] +
1323 s3[0] + s3[1] + s3[2] + s3[3] +
1324 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1331 src += 4 * src_wrap;
1337 void ff_shrink88(uint8_t *dst, int dst_wrap,
1338 const uint8_t *src, int src_wrap,
1339 int width, int height)
1343 for(;height > 0; height--) {
1344 for(w = width;w > 0; w--) {
1347 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1350 *(dst++) = (tmp + 32)>>6;
1351 src += 8 - 8*src_wrap;
1353 src += 8*src_wrap - 8*width;
1354 dst += dst_wrap - width;
1358 /* XXX: add jpeg quantize code */
1360 #define TRANSP_INDEX (6*6*6)
1362 /* this is maybe slow, but allows for extensions */
1363 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1365 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1368 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1371 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1374 pal = (uint32_t *)palette;
1376 for(r = 0; r < 6; r++) {
1377 for(g = 0; g < 6; g++) {
1378 for(b = 0; b < 6; b++) {
1379 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1380 (pal_value[g] << 8) | pal_value[b];
1387 pal[i++] = 0xff000000;
1390 /* copy bit n to bits 0 ... n - 1 */
1391 static inline unsigned int bitcopy_n(unsigned int a, int n)
1394 mask = (1 << n) - 1;
1395 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1398 /* rgb555 handling */
1400 #define RGB_NAME rgb555
1402 #define RGB_IN(r, g, b, s)\
1404 unsigned int v = ((const uint16_t *)(s))[0];\
1405 r = bitcopy_n(v >> (10 - 3), 3);\
1406 g = bitcopy_n(v >> (5 - 3), 3);\
1407 b = bitcopy_n(v << 3, 3);\
1411 #define RGB_OUT(d, r, g, b)\
1413 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1418 #include "imgconvert_template.h"
1420 /* rgb565 handling */
1422 #define RGB_NAME rgb565
1424 #define RGB_IN(r, g, b, s)\
1426 unsigned int v = ((const uint16_t *)(s))[0];\
1427 r = bitcopy_n(v >> (11 - 3), 3);\
1428 g = bitcopy_n(v >> (5 - 2), 2);\
1429 b = bitcopy_n(v << 3, 3);\
1432 #define RGB_OUT(d, r, g, b)\
1434 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1439 #include "imgconvert_template.h"
1441 /* bgr24 handling */
1443 #define RGB_NAME bgr24
1445 #define RGB_IN(r, g, b, s)\
1452 #define RGB_OUT(d, r, g, b)\
1461 #include "imgconvert_template.h"
1467 /* rgb24 handling */
1469 #define RGB_NAME rgb24
1472 #define RGB_IN(r, g, b, s)\
1479 #define RGB_OUT(d, r, g, b)\
1488 #include "imgconvert_template.h"
1490 /* rgb32 handling */
1492 #define RGB_NAME rgb32
1495 #define RGB_IN(r, g, b, s)\
1497 unsigned int v = ((const uint32_t *)(s))[0];\
1498 r = (v >> 16) & 0xff;\
1499 g = (v >> 8) & 0xff;\
1503 #define RGBA_IN(r, g, b, a, s)\
1505 unsigned int v = ((const uint32_t *)(s))[0];\
1506 a = (v >> 24) & 0xff;\
1507 r = (v >> 16) & 0xff;\
1508 g = (v >> 8) & 0xff;\
1512 #define RGBA_OUT(d, r, g, b, a)\
1514 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1519 #include "imgconvert_template.h"
1521 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1522 int width, int height, int xor_mask)
1524 const unsigned char *p;
1526 int v, dst_wrap, src_wrap;
1530 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1533 dst_wrap = dst->linesize[0] - width;
1534 for(y=0;y<height;y++) {
1537 v = *p++ ^ xor_mask;
1539 q[1] = -((v >> 6) & 1);
1540 q[2] = -((v >> 5) & 1);
1541 q[3] = -((v >> 4) & 1);
1542 q[4] = -((v >> 3) & 1);
1543 q[5] = -((v >> 2) & 1);
1544 q[6] = -((v >> 1) & 1);
1545 q[7] = -((v >> 0) & 1);
1550 v = *p++ ^ xor_mask;
1552 q[0] = -((v >> 7) & 1);
1562 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1563 int width, int height)
1565 mono_to_gray(dst, src, width, height, 0xff);
1568 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1569 int width, int height)
1571 mono_to_gray(dst, src, width, height, 0x00);
1574 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1575 int width, int height, int xor_mask)
1580 int j, b, v, n1, src_wrap, dst_wrap, y;
1583 src_wrap = src->linesize[0] - width;
1586 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1588 for(y=0;y<height;y++) {
1595 v = (v << 1) | (b >> 7);
1597 d[0] = v ^ xor_mask;
1607 v = (v << 1) | (b >> 7);
1610 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1618 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1619 int width, int height)
1621 gray_to_mono(dst, src, width, height, 0xff);
1624 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1625 int width, int height)
1627 gray_to_mono(dst, src, width, height, 0x00);
1630 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1631 int width, int height)
1633 int x, y, src_wrap, dst_wrap;
1636 src_wrap = src->linesize[0] - width;
1638 dst_wrap = dst->linesize[0] - width * 2;
1639 for(y=0; y<height; y++){
1640 for(x=0; x<width; x++){
1649 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1650 int width, int height)
1652 int x, y, src_wrap, dst_wrap;
1655 src_wrap = src->linesize[0] - width * 2;
1657 dst_wrap = dst->linesize[0] - width;
1658 for(y=0; y<height; y++){
1659 for(x=0; x<width; x++){
1668 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1669 int width, int height)
1671 gray16_to_gray(dst, src, width, height);
1674 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1675 int width, int height)
1677 AVPicture tmpsrc = *src;
1679 gray16_to_gray(dst, &tmpsrc, width, height);
1682 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1683 int width, int height)
1685 int x, y, src_wrap, dst_wrap;
1687 s = (uint16_t*)src->data[0];
1688 src_wrap = (src->linesize[0] - width * 2)/2;
1689 d = (uint16_t*)dst->data[0];
1690 dst_wrap = (dst->linesize[0] - width * 2)/2;
1691 for(y=0; y<height; y++){
1692 for(x=0; x<width; x++){
1693 *d++ = bswap_16(*s++);
1701 typedef struct ConvertEntry {
1702 void (*convert)(AVPicture *dst,
1703 const AVPicture *src, int width, int height);
1706 /* Add each new conversion function in this table. In order to be able
1707 to convert from any format to any format, the following constraints
1710 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1712 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1714 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1716 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1719 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1721 The other conversion functions are just optimizations for common cases.
1723 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1724 [PIX_FMT_YUV420P] = {
1725 [PIX_FMT_YUYV422] = {
1726 .convert = yuv420p_to_yuyv422,
1728 [PIX_FMT_RGB555] = {
1729 .convert = yuv420p_to_rgb555
1731 [PIX_FMT_RGB565] = {
1732 .convert = yuv420p_to_rgb565
1735 .convert = yuv420p_to_bgr24
1738 .convert = yuv420p_to_rgb24
1741 .convert = yuv420p_to_rgb32
1743 [PIX_FMT_UYVY422] = {
1744 .convert = yuv420p_to_uyvy422,
1747 [PIX_FMT_YUV422P] = {
1748 [PIX_FMT_YUYV422] = {
1749 .convert = yuv422p_to_yuyv422,
1751 [PIX_FMT_UYVY422] = {
1752 .convert = yuv422p_to_uyvy422,
1755 [PIX_FMT_YUV444P] = {
1757 .convert = yuv444p_to_rgb24
1760 [PIX_FMT_YUVJ420P] = {
1761 [PIX_FMT_RGB555] = {
1762 .convert = yuvj420p_to_rgb555
1764 [PIX_FMT_RGB565] = {
1765 .convert = yuvj420p_to_rgb565
1768 .convert = yuvj420p_to_bgr24
1771 .convert = yuvj420p_to_rgb24
1774 .convert = yuvj420p_to_rgb32
1777 [PIX_FMT_YUVJ444P] = {
1779 .convert = yuvj444p_to_rgb24
1782 [PIX_FMT_YUYV422] = {
1783 [PIX_FMT_YUV420P] = {
1784 .convert = yuyv422_to_yuv420p,
1786 [PIX_FMT_YUV422P] = {
1787 .convert = yuyv422_to_yuv422p,
1790 [PIX_FMT_UYVY422] = {
1791 [PIX_FMT_YUV420P] = {
1792 .convert = uyvy422_to_yuv420p,
1794 [PIX_FMT_YUV422P] = {
1795 .convert = uyvy422_to_yuv422p,
1799 [PIX_FMT_YUV420P] = {
1800 .convert = rgb24_to_yuv420p
1802 [PIX_FMT_RGB565] = {
1803 .convert = rgb24_to_rgb565
1805 [PIX_FMT_RGB555] = {
1806 .convert = rgb24_to_rgb555
1809 .convert = rgb24_to_rgb32
1812 .convert = rgb24_to_bgr24
1815 .convert = rgb24_to_gray
1818 .convert = rgb24_to_pal8
1820 [PIX_FMT_YUV444P] = {
1821 .convert = rgb24_to_yuv444p
1823 [PIX_FMT_YUVJ420P] = {
1824 .convert = rgb24_to_yuvj420p
1826 [PIX_FMT_YUVJ444P] = {
1827 .convert = rgb24_to_yuvj444p
1832 .convert = rgb32_to_rgb24
1835 .convert = rgb32_to_bgr24
1837 [PIX_FMT_RGB565] = {
1838 .convert = rgb32_to_rgb565
1840 [PIX_FMT_RGB555] = {
1841 .convert = rgb32_to_rgb555
1844 .convert = rgb32_to_pal8
1846 [PIX_FMT_YUV420P] = {
1847 .convert = rgb32_to_yuv420p
1850 .convert = rgb32_to_gray
1855 .convert = bgr24_to_rgb32
1858 .convert = bgr24_to_rgb24
1860 [PIX_FMT_YUV420P] = {
1861 .convert = bgr24_to_yuv420p
1864 .convert = bgr24_to_gray
1867 [PIX_FMT_RGB555] = {
1869 .convert = rgb555_to_rgb24
1872 .convert = rgb555_to_rgb32
1874 [PIX_FMT_YUV420P] = {
1875 .convert = rgb555_to_yuv420p
1878 .convert = rgb555_to_gray
1881 [PIX_FMT_RGB565] = {
1883 .convert = rgb565_to_rgb32
1886 .convert = rgb565_to_rgb24
1888 [PIX_FMT_YUV420P] = {
1889 .convert = rgb565_to_yuv420p
1892 .convert = rgb565_to_gray
1895 [PIX_FMT_GRAY16BE] = {
1897 .convert = gray16be_to_gray
1899 [PIX_FMT_GRAY16LE] = {
1900 .convert = gray16_to_gray16
1903 [PIX_FMT_GRAY16LE] = {
1905 .convert = gray16le_to_gray
1907 [PIX_FMT_GRAY16BE] = {
1908 .convert = gray16_to_gray16
1912 [PIX_FMT_RGB555] = {
1913 .convert = gray_to_rgb555
1915 [PIX_FMT_RGB565] = {
1916 .convert = gray_to_rgb565
1919 .convert = gray_to_rgb24
1922 .convert = gray_to_bgr24
1925 .convert = gray_to_rgb32
1927 [PIX_FMT_MONOWHITE] = {
1928 .convert = gray_to_monowhite
1930 [PIX_FMT_MONOBLACK] = {
1931 .convert = gray_to_monoblack
1933 [PIX_FMT_GRAY16LE] = {
1934 .convert = gray_to_gray16
1936 [PIX_FMT_GRAY16BE] = {
1937 .convert = gray_to_gray16
1940 [PIX_FMT_MONOWHITE] = {
1942 .convert = monowhite_to_gray
1945 [PIX_FMT_MONOBLACK] = {
1947 .convert = monoblack_to_gray
1951 [PIX_FMT_RGB555] = {
1952 .convert = pal8_to_rgb555
1954 [PIX_FMT_RGB565] = {
1955 .convert = pal8_to_rgb565
1958 .convert = pal8_to_bgr24
1961 .convert = pal8_to_rgb24
1964 .convert = pal8_to_rgb32
1967 [PIX_FMT_UYYVYY411] = {
1968 [PIX_FMT_YUV411P] = {
1969 .convert = uyyvyy411_to_yuv411p,
1975 int avpicture_alloc(AVPicture *picture,
1976 int pix_fmt, int width, int height)
1981 size = avpicture_get_size(pix_fmt, width, height);
1984 ptr = av_malloc(size);
1987 avpicture_fill(picture, ptr, pix_fmt, width, height);
1990 memset(picture, 0, sizeof(AVPicture));
1994 void avpicture_free(AVPicture *picture)
1996 av_free(picture->data[0]);
1999 /* return true if yuv planar */
2000 static inline int is_yuv_planar(const PixFmtInfo *ps)
2002 return (ps->color_type == FF_COLOR_YUV ||
2003 ps->color_type == FF_COLOR_YUV_JPEG) &&
2004 ps->pixel_type == FF_PIXEL_PLANAR;
2007 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2008 int pix_fmt, int top_band, int left_band)
2013 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2016 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2017 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2019 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2020 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2021 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2023 dst->linesize[0] = src->linesize[0];
2024 dst->linesize[1] = src->linesize[1];
2025 dst->linesize[2] = src->linesize[2];
2029 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2030 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2039 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2040 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2042 for (i = 0; i < 3; i++) {
2043 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2044 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2046 if (padtop || padleft) {
2047 memset(dst->data[i], color[i],
2048 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2051 if (padleft || padright) {
2052 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2053 (dst->linesize[i] - (padright >> x_shift));
2054 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2055 for (y = 0; y < yheight; y++) {
2056 memset(optr, color[i], (padleft + padright) >> x_shift);
2057 optr += dst->linesize[i];
2061 if (src) { /* first line */
2062 uint8_t *iptr = src->data[i];
2063 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2064 (padleft >> x_shift);
2065 memcpy(optr, iptr, src->linesize[i]);
2066 iptr += src->linesize[i];
2067 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2068 (dst->linesize[i] - (padright >> x_shift));
2069 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2070 for (y = 0; y < yheight; y++) {
2071 memset(optr, color[i], (padleft + padright) >> x_shift);
2072 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2074 iptr += src->linesize[i];
2075 optr += dst->linesize[i];
2079 if (padbottom || padright) {
2080 optr = dst->data[i] + dst->linesize[i] *
2081 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2082 memset(optr, color[i],dst->linesize[i] *
2083 (padbottom >> y_shift) + (padright >> x_shift));
2089 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
2090 void img_copy(AVPicture *dst, const AVPicture *src,
2091 int pix_fmt, int width, int height)
2093 av_picture_copy(dst, src, pix_fmt, width, height);
2096 int img_crop(AVPicture *dst, const AVPicture *src,
2097 int pix_fmt, int top_band, int left_band)
2099 return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
2102 int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2103 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2106 return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
2110 #ifndef CONFIG_SWSCALE
2111 static uint8_t y_ccir_to_jpeg[256];
2112 static uint8_t y_jpeg_to_ccir[256];
2113 static uint8_t c_ccir_to_jpeg[256];
2114 static uint8_t c_jpeg_to_ccir[256];
2116 /* init various conversion tables */
2117 static void img_convert_init(void)
2120 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2122 for(i = 0;i < 256; i++) {
2123 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
2124 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
2125 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
2126 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
2130 /* apply to each pixel the given table */
2131 static void img_apply_table(uint8_t *dst, int dst_wrap,
2132 const uint8_t *src, int src_wrap,
2133 int width, int height, const uint8_t *table1)
2138 const uint8_t *table;
2141 for(;height > 0; height--) {
2165 /* XXX: use generic filter ? */
2166 /* XXX: in most cases, the sampling position is incorrect */
2169 static void shrink41(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] + s[2] + s[3] + 2) >> 2;
2191 static void shrink21(uint8_t *dst, int dst_wrap,
2192 const uint8_t *src, int src_wrap,
2193 int width, int height)
2199 for(;height > 0; height--) {
2202 for(w = width;w > 0; w--) {
2203 d[0] = (s[0] + s[1]) >> 1;
2213 static void shrink12(uint8_t *dst, int dst_wrap,
2214 const uint8_t *src, int src_wrap,
2215 int width, int height)
2219 const uint8_t *s1, *s2;
2221 for(;height > 0; height--) {
2225 for(w = width;w >= 4; w-=4) {
2226 d[0] = (s1[0] + s2[0]) >> 1;
2227 d[1] = (s1[1] + s2[1]) >> 1;
2228 d[2] = (s1[2] + s2[2]) >> 1;
2229 d[3] = (s1[3] + s2[3]) >> 1;
2235 d[0] = (s1[0] + s2[0]) >> 1;
2240 src += 2 * src_wrap;
2245 static void grow21_line(uint8_t *dst, const uint8_t *src,
2254 for(w = width;w >= 4; w-=4) {
2255 d[1] = d[0] = s1[0];
2256 d[3] = d[2] = s1[1];
2260 for(;w >= 2; w -= 2) {
2261 d[1] = d[0] = s1[0];
2265 /* only needed if width is not a multiple of two */
2266 /* XXX: veryfy that */
2272 static void grow41_line(uint8_t *dst, const uint8_t *src,
2281 for(w = width;w >= 4; w-=4) {
2293 static void grow21(uint8_t *dst, int dst_wrap,
2294 const uint8_t *src, int src_wrap,
2295 int width, int height)
2297 for(;height > 0; height--) {
2298 grow21_line(dst, src, width);
2305 static void grow12(uint8_t *dst, int dst_wrap,
2306 const uint8_t *src, int src_wrap,
2307 int width, int height)
2309 for(;height > 0; height-=2) {
2310 memcpy(dst, src, width);
2312 memcpy(dst, src, width);
2319 static void grow22(uint8_t *dst, int dst_wrap,
2320 const uint8_t *src, int src_wrap,
2321 int width, int height)
2323 for(;height > 0; height--) {
2324 grow21_line(dst, src, width);
2332 static void grow41(uint8_t *dst, int dst_wrap,
2333 const uint8_t *src, int src_wrap,
2334 int width, int height)
2336 for(;height > 0; height--) {
2337 grow41_line(dst, src, width);
2344 static void grow44(uint8_t *dst, int dst_wrap,
2345 const uint8_t *src, int src_wrap,
2346 int width, int height)
2348 for(;height > 0; height--) {
2349 grow41_line(dst, src, width);
2350 if ((height & 3) == 1)
2357 static void conv411(uint8_t *dst, int dst_wrap,
2358 const uint8_t *src, int src_wrap,
2359 int width, int height)
2362 const uint8_t *s1, *s2;
2367 for(;height > 0; height--) {
2369 s2 = src + src_wrap;
2371 for(w = width;w > 0; w--) {
2372 c = (s1[0] + s2[0]) >> 1;
2379 src += src_wrap * 2;
2384 /* XXX: always use linesize. Return -1 if not supported */
2385 int img_convert(AVPicture *dst, int dst_pix_fmt,
2386 const AVPicture *src, int src_pix_fmt,
2387 int src_width, int src_height)
2389 static int initialized;
2390 int i, ret, dst_width, dst_height, int_pix_fmt;
2391 const PixFmtInfo *src_pix, *dst_pix;
2392 const ConvertEntry *ce;
2393 AVPicture tmp1, *tmp = &tmp1;
2395 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2396 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2398 if (src_width <= 0 || src_height <= 0)
2406 dst_width = src_width;
2407 dst_height = src_height;
2409 dst_pix = &pix_fmt_info[dst_pix_fmt];
2410 src_pix = &pix_fmt_info[src_pix_fmt];
2411 if (src_pix_fmt == dst_pix_fmt) {
2412 /* no conversion needed: just copy */
2413 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2417 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2419 /* specific conversion routine */
2420 ce->convert(dst, src, dst_width, dst_height);
2425 if (is_yuv_planar(dst_pix) &&
2426 src_pix_fmt == PIX_FMT_GRAY8) {
2430 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2431 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2432 src->data[0], src->linesize[0],
2433 dst_width, dst_height);
2435 img_apply_table(dst->data[0], dst->linesize[0],
2436 src->data[0], src->linesize[0],
2437 dst_width, dst_height,
2440 /* fill U and V with 128 */
2443 w >>= dst_pix->x_chroma_shift;
2444 h >>= dst_pix->y_chroma_shift;
2445 for(i = 1; i <= 2; i++) {
2447 for(y = 0; y< h; y++) {
2449 d += dst->linesize[i];
2456 if (is_yuv_planar(src_pix) &&
2457 dst_pix_fmt == PIX_FMT_GRAY8) {
2458 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2459 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2460 src->data[0], src->linesize[0],
2461 dst_width, dst_height);
2463 img_apply_table(dst->data[0], dst->linesize[0],
2464 src->data[0], src->linesize[0],
2465 dst_width, dst_height,
2471 /* YUV to YUV planar */
2472 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2473 int x_shift, y_shift, w, h, xy_shift;
2474 void (*resize_func)(uint8_t *dst, int dst_wrap,
2475 const uint8_t *src, int src_wrap,
2476 int width, int height);
2478 /* compute chroma size of the smallest dimensions */
2481 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2482 w >>= dst_pix->x_chroma_shift;
2484 w >>= src_pix->x_chroma_shift;
2485 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2486 h >>= dst_pix->y_chroma_shift;
2488 h >>= src_pix->y_chroma_shift;
2490 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2491 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2492 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2493 /* there must be filters for conversion at least from and to
2497 resize_func = ff_img_copy_plane;
2500 resize_func = shrink21;
2503 resize_func = shrink41;
2506 resize_func = shrink12;
2509 resize_func = ff_shrink22;
2512 resize_func = ff_shrink44;
2515 resize_func = grow21;
2518 resize_func = grow12;
2521 resize_func = grow41;
2524 resize_func = grow22;
2527 resize_func = grow44;
2530 resize_func = conv411;
2533 /* currently not handled */
2534 goto no_chroma_filter;
2537 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2538 src->data[0], src->linesize[0],
2539 dst_width, dst_height);
2541 for(i = 1;i <= 2; i++)
2542 resize_func(dst->data[i], dst->linesize[i],
2543 src->data[i], src->linesize[i],
2544 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2545 /* if yuv color space conversion is needed, we do it here on
2546 the destination image */
2547 if (dst_pix->color_type != src_pix->color_type) {
2548 const uint8_t *y_table, *c_table;
2549 if (dst_pix->color_type == FF_COLOR_YUV) {
2550 y_table = y_jpeg_to_ccir;
2551 c_table = c_jpeg_to_ccir;
2553 y_table = y_ccir_to_jpeg;
2554 c_table = c_ccir_to_jpeg;
2556 img_apply_table(dst->data[0], dst->linesize[0],
2557 dst->data[0], dst->linesize[0],
2558 dst_width, dst_height,
2561 for(i = 1;i <= 2; i++)
2562 img_apply_table(dst->data[i], dst->linesize[i],
2563 dst->data[i], dst->linesize[i],
2564 dst_width>>dst_pix->x_chroma_shift,
2565 dst_height>>dst_pix->y_chroma_shift,
2572 /* try to use an intermediate format */
2573 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2574 dst_pix_fmt == PIX_FMT_YUYV422) {
2575 /* specific case: convert to YUV422P first */
2576 int_pix_fmt = PIX_FMT_YUV422P;
2577 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2578 dst_pix_fmt == PIX_FMT_UYVY422) {
2579 /* specific case: convert to YUV422P first */
2580 int_pix_fmt = PIX_FMT_YUV422P;
2581 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2582 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2583 /* specific case: convert to YUV411P first */
2584 int_pix_fmt = PIX_FMT_YUV411P;
2585 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2586 src_pix_fmt != PIX_FMT_GRAY8) ||
2587 (dst_pix->color_type == FF_COLOR_GRAY &&
2588 dst_pix_fmt != PIX_FMT_GRAY8)) {
2589 /* gray8 is the normalized format */
2590 int_pix_fmt = PIX_FMT_GRAY8;
2591 } else if ((is_yuv_planar(src_pix) &&
2592 src_pix_fmt != PIX_FMT_YUV444P &&
2593 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2594 /* yuv444 is the normalized format */
2595 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2596 int_pix_fmt = PIX_FMT_YUVJ444P;
2598 int_pix_fmt = PIX_FMT_YUV444P;
2599 } else if ((is_yuv_planar(dst_pix) &&
2600 dst_pix_fmt != PIX_FMT_YUV444P &&
2601 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2602 /* yuv444 is the normalized format */
2603 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2604 int_pix_fmt = PIX_FMT_YUVJ444P;
2606 int_pix_fmt = PIX_FMT_YUV444P;
2608 /* the two formats are rgb or gray8 or yuv[j]444p */
2609 if (src_pix->is_alpha && dst_pix->is_alpha)
2610 int_pix_fmt = PIX_FMT_RGB32;
2612 int_pix_fmt = PIX_FMT_RGB24;
2614 if (src_pix_fmt == int_pix_fmt)
2616 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2619 if (img_convert(tmp, int_pix_fmt,
2620 src, src_pix_fmt, src_width, src_height) < 0)
2622 if (img_convert(dst, dst_pix_fmt,
2623 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2627 avpicture_free(tmp);
2632 /* NOTE: we scan all the pixels to have an exact information */
2633 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2635 const unsigned char *p;
2636 int src_wrap, ret, x, y;
2638 uint32_t *palette = (uint32_t *)src->data[1];
2641 src_wrap = src->linesize[0] - width;
2643 for(y=0;y<height;y++) {
2644 for(x=0;x<width;x++) {
2645 a = palette[p[0]] >> 24;
2647 ret |= FF_ALPHA_TRANSP;
2648 } else if (a != 0xff) {
2649 ret |= FF_ALPHA_SEMI_TRANSP;
2658 int img_get_alpha_info(const AVPicture *src,
2659 int pix_fmt, int width, int height)
2661 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2664 pf = &pix_fmt_info[pix_fmt];
2665 /* no alpha can be represented in format */
2670 ret = get_alpha_info_rgb32(src, width, height);
2673 ret = get_alpha_info_pal8(src, width, height);
2676 /* we do not know, so everything is indicated */
2677 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2684 #define DEINT_INPLACE_LINE_LUM \
2685 movd_m2r(lum_m4[0],mm0);\
2686 movd_m2r(lum_m3[0],mm1);\
2687 movd_m2r(lum_m2[0],mm2);\
2688 movd_m2r(lum_m1[0],mm3);\
2689 movd_m2r(lum[0],mm4);\
2690 punpcklbw_r2r(mm7,mm0);\
2691 movd_r2m(mm2,lum_m4[0]);\
2692 punpcklbw_r2r(mm7,mm1);\
2693 punpcklbw_r2r(mm7,mm2);\
2694 punpcklbw_r2r(mm7,mm3);\
2695 punpcklbw_r2r(mm7,mm4);\
2696 paddw_r2r(mm3,mm1);\
2698 paddw_r2r(mm4,mm0);\
2700 paddw_r2r(mm6,mm2);\
2701 paddw_r2r(mm2,mm1);\
2702 psubusw_r2r(mm0,mm1);\
2704 packuswb_r2r(mm7,mm1);\
2705 movd_r2m(mm1,lum_m2[0]);
2707 #define DEINT_LINE_LUM \
2708 movd_m2r(lum_m4[0],mm0);\
2709 movd_m2r(lum_m3[0],mm1);\
2710 movd_m2r(lum_m2[0],mm2);\
2711 movd_m2r(lum_m1[0],mm3);\
2712 movd_m2r(lum[0],mm4);\
2713 punpcklbw_r2r(mm7,mm0);\
2714 punpcklbw_r2r(mm7,mm1);\
2715 punpcklbw_r2r(mm7,mm2);\
2716 punpcklbw_r2r(mm7,mm3);\
2717 punpcklbw_r2r(mm7,mm4);\
2718 paddw_r2r(mm3,mm1);\
2720 paddw_r2r(mm4,mm0);\
2722 paddw_r2r(mm6,mm2);\
2723 paddw_r2r(mm2,mm1);\
2724 psubusw_r2r(mm0,mm1);\
2726 packuswb_r2r(mm7,mm1);\
2727 movd_r2m(mm1,dst[0]);
2730 /* filter parameters: [-1 4 2 4 -1] // 8 */
2731 static void deinterlace_line(uint8_t *dst,
2732 const uint8_t *lum_m4, const uint8_t *lum_m3,
2733 const uint8_t *lum_m2, const uint8_t *lum_m1,
2738 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2741 for(;size > 0;size--) {
2743 sum += lum_m3[0] << 2;
2744 sum += lum_m2[0] << 1;
2745 sum += lum_m1[0] << 2;
2747 dst[0] = cm[(sum + 4) >> 3];
2764 movq_m2r(rounder,mm6);
2766 for (;size > 3; size-=4) {
2777 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2781 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2784 for(;size > 0;size--) {
2786 sum += lum_m3[0] << 2;
2787 sum += lum_m2[0] << 1;
2788 lum_m4[0]=lum_m2[0];
2789 sum += lum_m1[0] << 2;
2791 lum_m2[0] = cm[(sum + 4) >> 3];
2807 movq_m2r(rounder,mm6);
2809 for (;size > 3; size-=4) {
2810 DEINT_INPLACE_LINE_LUM
2820 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2821 top field is copied as is, but the bottom field is deinterlaced
2822 against the top field. */
2823 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2824 const uint8_t *src1, int src_wrap,
2825 int width, int height)
2827 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2832 src_0=&src_m1[src_wrap];
2833 src_p1=&src_0[src_wrap];
2834 src_p2=&src_p1[src_wrap];
2835 for(y=0;y<(height-2);y+=2) {
2836 memcpy(dst,src_m1,width);
2838 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2842 src_p1 += 2*src_wrap;
2843 src_p2 += 2*src_wrap;
2846 memcpy(dst,src_m1,width);
2849 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2852 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2853 int width, int height)
2855 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2858 buf = (uint8_t*)av_malloc(width);
2861 memcpy(buf,src_m1,width);
2862 src_0=&src_m1[src_wrap];
2863 src_p1=&src_0[src_wrap];
2864 src_p2=&src_p1[src_wrap];
2865 for(y=0;y<(height-2);y+=2) {
2866 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2869 src_p1 += 2*src_wrap;
2870 src_p2 += 2*src_wrap;
2873 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2877 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2878 int pix_fmt, int width, int height)
2882 if (pix_fmt != PIX_FMT_YUV420P &&
2883 pix_fmt != PIX_FMT_YUV422P &&
2884 pix_fmt != PIX_FMT_YUV444P &&
2885 pix_fmt != PIX_FMT_YUV411P &&
2886 pix_fmt != PIX_FMT_GRAY8)
2888 if ((width & 3) != 0 || (height & 3) != 0)
2894 case PIX_FMT_YUV420P:
2898 case PIX_FMT_YUV422P:
2901 case PIX_FMT_YUV411P:
2907 if (pix_fmt == PIX_FMT_GRAY8) {
2912 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2915 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2916 src->data[i], src->linesize[i],