2 * Misc image convertion routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * Misc image convertion routines.
26 * - write 'ffimg' program to test all the image related stuff
27 * - move all api to slice based system
28 * - integrate deinterlacing, postprocessing and scaling in the conversion process
35 #include "libvo/fastmemcpy.h"
42 #define xglue(x, y) x ## y
43 #define glue(x, y) xglue(x, y)
45 #define FF_COLOR_RGB 0 /* RGB color space */
46 #define FF_COLOR_GRAY 1 /* gray color space */
47 #define FF_COLOR_YUV 2 /* YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
48 #define FF_COLOR_YUV_JPEG 3 /* YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
50 #define FF_PIXEL_PLANAR 0 /* each channel has one component in AVPicture */
51 #define FF_PIXEL_PACKED 1 /* only one components containing all the channels */
52 #define FF_PIXEL_PALETTE 2 /* one components containing indexes for a palette */
54 typedef struct PixFmtInfo {
56 uint8_t nb_channels; /* number of channels (including alpha) */
57 uint8_t color_type; /* color type (see FF_COLOR_xxx constants) */
58 uint8_t pixel_type; /* pixel storage type (see FF_PIXEL_xxx constants) */
59 uint8_t is_alpha : 1; /* true if alpha can be specified */
60 uint8_t x_chroma_shift; /* X chroma subsampling factor is 2 ^ shift */
61 uint8_t y_chroma_shift; /* Y chroma subsampling factor is 2 ^ shift */
62 uint8_t depth; /* bit depth of the color components */
65 /* this table gives more information about formats */
66 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
71 .color_type = FF_COLOR_YUV,
72 .pixel_type = FF_PIXEL_PLANAR,
74 .x_chroma_shift = 1, .y_chroma_shift = 1,
79 .color_type = FF_COLOR_YUV,
80 .pixel_type = FF_PIXEL_PLANAR,
82 .x_chroma_shift = 1, .y_chroma_shift = 0,
87 .color_type = FF_COLOR_YUV,
88 .pixel_type = FF_PIXEL_PLANAR,
90 .x_chroma_shift = 0, .y_chroma_shift = 0,
95 .color_type = FF_COLOR_YUV,
96 .pixel_type = FF_PIXEL_PACKED,
98 .x_chroma_shift = 1, .y_chroma_shift = 0,
100 [PIX_FMT_UYVY422] = {
103 .color_type = FF_COLOR_YUV,
104 .pixel_type = FF_PIXEL_PACKED,
106 .x_chroma_shift = 1, .y_chroma_shift = 0,
108 [PIX_FMT_YUV410P] = {
111 .color_type = FF_COLOR_YUV,
112 .pixel_type = FF_PIXEL_PLANAR,
114 .x_chroma_shift = 2, .y_chroma_shift = 2,
116 [PIX_FMT_YUV411P] = {
119 .color_type = FF_COLOR_YUV,
120 .pixel_type = FF_PIXEL_PLANAR,
122 .x_chroma_shift = 2, .y_chroma_shift = 0,
126 [PIX_FMT_YUVJ420P] = {
129 .color_type = FF_COLOR_YUV_JPEG,
130 .pixel_type = FF_PIXEL_PLANAR,
132 .x_chroma_shift = 1, .y_chroma_shift = 1,
134 [PIX_FMT_YUVJ422P] = {
137 .color_type = FF_COLOR_YUV_JPEG,
138 .pixel_type = FF_PIXEL_PLANAR,
140 .x_chroma_shift = 1, .y_chroma_shift = 0,
142 [PIX_FMT_YUVJ444P] = {
145 .color_type = FF_COLOR_YUV_JPEG,
146 .pixel_type = FF_PIXEL_PLANAR,
148 .x_chroma_shift = 0, .y_chroma_shift = 0,
155 .color_type = FF_COLOR_RGB,
156 .pixel_type = FF_PIXEL_PACKED,
158 .x_chroma_shift = 0, .y_chroma_shift = 0,
163 .color_type = FF_COLOR_RGB,
164 .pixel_type = FF_PIXEL_PACKED,
166 .x_chroma_shift = 0, .y_chroma_shift = 0,
170 .nb_channels = 4, .is_alpha = 1,
171 .color_type = FF_COLOR_RGB,
172 .pixel_type = FF_PIXEL_PACKED,
174 .x_chroma_shift = 0, .y_chroma_shift = 0,
179 .color_type = FF_COLOR_RGB,
180 .pixel_type = FF_PIXEL_PACKED,
182 .x_chroma_shift = 0, .y_chroma_shift = 0,
186 .nb_channels = 4, .is_alpha = 1,
187 .color_type = FF_COLOR_RGB,
188 .pixel_type = FF_PIXEL_PACKED,
190 .x_chroma_shift = 0, .y_chroma_shift = 0,
193 /* gray / mono formats */
197 .color_type = FF_COLOR_GRAY,
198 .pixel_type = FF_PIXEL_PLANAR,
201 [PIX_FMT_MONOWHITE] = {
204 .color_type = FF_COLOR_GRAY,
205 .pixel_type = FF_PIXEL_PLANAR,
208 [PIX_FMT_MONOBLACK] = {
211 .color_type = FF_COLOR_GRAY,
212 .pixel_type = FF_PIXEL_PLANAR,
216 /* paletted formats */
219 .nb_channels = 4, .is_alpha = 1,
220 .color_type = FF_COLOR_RGB,
221 .pixel_type = FF_PIXEL_PALETTE,
224 [PIX_FMT_XVMC_MPEG2_MC] = {
227 [PIX_FMT_XVMC_MPEG2_IDCT] = {
230 [PIX_FMT_UYVY411] = {
233 .color_type = FF_COLOR_YUV,
234 .pixel_type = FF_PIXEL_PACKED,
236 .x_chroma_shift = 2, .y_chroma_shift = 0,
240 .nb_channels = 4, .is_alpha = 1,
241 .color_type = FF_COLOR_RGB,
242 .pixel_type = FF_PIXEL_PACKED,
244 .x_chroma_shift = 0, .y_chroma_shift = 0,
249 .color_type = FF_COLOR_RGB,
250 .pixel_type = FF_PIXEL_PACKED,
252 .x_chroma_shift = 0, .y_chroma_shift = 0,
256 .nb_channels = 4, .is_alpha = 1,
257 .color_type = FF_COLOR_RGB,
258 .pixel_type = FF_PIXEL_PACKED,
260 .x_chroma_shift = 0, .y_chroma_shift = 0,
265 .color_type = FF_COLOR_RGB,
266 .pixel_type = FF_PIXEL_PACKED,
268 .x_chroma_shift = 0, .y_chroma_shift = 0,
273 .color_type = FF_COLOR_RGB,
274 .pixel_type = FF_PIXEL_PACKED,
276 .x_chroma_shift = 0, .y_chroma_shift = 0,
278 [PIX_FMT_RGB4_BYTE] = {
281 .color_type = FF_COLOR_RGB,
282 .pixel_type = FF_PIXEL_PACKED,
284 .x_chroma_shift = 0, .y_chroma_shift = 0,
289 .color_type = FF_COLOR_RGB,
290 .pixel_type = FF_PIXEL_PACKED,
292 .x_chroma_shift = 0, .y_chroma_shift = 0,
297 .color_type = FF_COLOR_RGB,
298 .pixel_type = FF_PIXEL_PACKED,
300 .x_chroma_shift = 0, .y_chroma_shift = 0,
302 [PIX_FMT_BGR4_BYTE] = {
305 .color_type = FF_COLOR_RGB,
306 .pixel_type = FF_PIXEL_PACKED,
308 .x_chroma_shift = 0, .y_chroma_shift = 0,
313 .color_type = FF_COLOR_YUV,
314 .pixel_type = FF_PIXEL_PLANAR,
316 .x_chroma_shift = 1, .y_chroma_shift = 1,
321 .color_type = FF_COLOR_YUV,
322 .pixel_type = FF_PIXEL_PLANAR,
324 .x_chroma_shift = 1, .y_chroma_shift = 1,
327 [PIX_FMT_BGR32_1] = {
329 .nb_channels = 4, .is_alpha = 1,
330 .color_type = FF_COLOR_RGB,
331 .pixel_type = FF_PIXEL_PACKED,
333 .x_chroma_shift = 0, .y_chroma_shift = 0,
335 [PIX_FMT_RGB32_1] = {
337 .nb_channels = 4, .is_alpha = 1,
338 .color_type = FF_COLOR_RGB,
339 .pixel_type = FF_PIXEL_PACKED,
341 .x_chroma_shift = 0, .y_chroma_shift = 0,
345 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
347 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
348 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
351 const char *avcodec_get_pix_fmt_name(int pix_fmt)
353 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
356 return pix_fmt_info[pix_fmt].name;
359 enum PixelFormat avcodec_get_pix_fmt(const char* name)
363 for (i=0; i < PIX_FMT_NB; i++)
364 if (!strcmp(pix_fmt_info[i].name, name))
369 /* Picture field are filled with 'ptr' addresses. Also return size */
370 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
371 int pix_fmt, int width, int height)
373 int size, w2, h2, size2;
374 const PixFmtInfo *pinfo;
376 if(avcodec_check_dimensions(NULL, width, height))
379 pinfo = &pix_fmt_info[pix_fmt];
380 size = width * height;
382 case PIX_FMT_YUV420P:
383 case PIX_FMT_YUV422P:
384 case PIX_FMT_YUV444P:
385 case PIX_FMT_YUV410P:
386 case PIX_FMT_YUV411P:
387 case PIX_FMT_YUVJ420P:
388 case PIX_FMT_YUVJ422P:
389 case PIX_FMT_YUVJ444P:
390 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
391 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
393 picture->data[0] = ptr;
394 picture->data[1] = picture->data[0] + size;
395 picture->data[2] = picture->data[1] + size2;
396 picture->linesize[0] = width;
397 picture->linesize[1] = w2;
398 picture->linesize[2] = w2;
399 return size + 2 * size2;
402 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
403 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
405 picture->data[0] = ptr;
406 picture->data[1] = picture->data[0] + size;
407 picture->data[2] = NULL;
408 picture->linesize[0] = width;
409 picture->linesize[1] = w2;
410 picture->linesize[2] = 0;
411 return size + 2 * size2;
414 picture->data[0] = ptr;
415 picture->data[1] = NULL;
416 picture->data[2] = NULL;
417 picture->linesize[0] = width * 3;
421 case PIX_FMT_RGB32_1:
422 case PIX_FMT_BGR32_1:
423 picture->data[0] = ptr;
424 picture->data[1] = NULL;
425 picture->data[2] = NULL;
426 picture->linesize[0] = width * 4;
433 picture->data[0] = ptr;
434 picture->data[1] = NULL;
435 picture->data[2] = NULL;
436 picture->linesize[0] = width * 2;
438 case PIX_FMT_UYVY422:
439 picture->data[0] = ptr;
440 picture->data[1] = NULL;
441 picture->data[2] = NULL;
442 picture->linesize[0] = width * 2;
444 case PIX_FMT_UYVY411:
445 picture->data[0] = ptr;
446 picture->data[1] = NULL;
447 picture->data[2] = NULL;
448 picture->linesize[0] = width + width/2;
449 return size + size/2;
452 case PIX_FMT_RGB4_BYTE:
453 case PIX_FMT_BGR4_BYTE:
455 picture->data[0] = ptr;
456 picture->data[1] = NULL;
457 picture->data[2] = NULL;
458 picture->linesize[0] = width;
462 picture->data[0] = ptr;
463 picture->data[1] = NULL;
464 picture->data[2] = NULL;
465 picture->linesize[0] = width / 2;
467 case PIX_FMT_MONOWHITE:
468 case PIX_FMT_MONOBLACK:
469 picture->data[0] = ptr;
470 picture->data[1] = NULL;
471 picture->data[2] = NULL;
472 picture->linesize[0] = (width + 7) >> 3;
473 return picture->linesize[0] * height;
475 size2 = (size + 3) & ~3;
476 picture->data[0] = ptr;
477 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
478 picture->data[2] = NULL;
479 picture->linesize[0] = width;
480 picture->linesize[1] = 4;
481 return size2 + 256 * 4;
484 picture->data[0] = NULL;
485 picture->data[1] = NULL;
486 picture->data[2] = NULL;
487 picture->data[3] = NULL;
492 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
493 unsigned char *dest, int dest_size)
495 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
496 int i, j, w, h, data_planes;
497 const unsigned char* s;
498 int size = avpicture_get_size(pix_fmt, width, height);
500 if (size > dest_size || size < 0)
503 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
504 if (pix_fmt == PIX_FMT_YUV422 ||
505 pix_fmt == PIX_FMT_UYVY422 ||
506 pix_fmt == PIX_FMT_BGR565 ||
507 pix_fmt == PIX_FMT_BGR565 ||
508 pix_fmt == PIX_FMT_RGB565 ||
509 pix_fmt == PIX_FMT_RGB555)
511 else if (pix_fmt == PIX_FMT_UYVY411)
513 else if (pix_fmt == PIX_FMT_PAL8)
516 w = width * (pf->depth * pf->nb_channels / 8);
521 data_planes = pf->nb_channels;
522 w = (width*pf->depth + 7)/8;
526 for (i=0; i<data_planes; i++) {
528 w = width >> pf->x_chroma_shift;
529 h = height >> pf->y_chroma_shift;
535 s += src->linesize[i];
539 if (pf->pixel_type == FF_PIXEL_PALETTE)
540 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
545 int avpicture_get_size(int pix_fmt, int width, int height)
547 AVPicture dummy_pict;
548 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
552 * compute the loss when converting from a pixel format to another
554 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
557 const PixFmtInfo *pf, *ps;
560 ps = &pix_fmt_info[src_pix_fmt];
561 pf = &pix_fmt_info[dst_pix_fmt];
565 pf = &pix_fmt_info[dst_pix_fmt];
566 if (pf->depth < ps->depth ||
567 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
568 loss |= FF_LOSS_DEPTH;
569 if (pf->x_chroma_shift > ps->x_chroma_shift ||
570 pf->y_chroma_shift > ps->y_chroma_shift)
571 loss |= FF_LOSS_RESOLUTION;
572 switch(pf->color_type) {
574 if (ps->color_type != FF_COLOR_RGB &&
575 ps->color_type != FF_COLOR_GRAY)
576 loss |= FF_LOSS_COLORSPACE;
579 if (ps->color_type != FF_COLOR_GRAY)
580 loss |= FF_LOSS_COLORSPACE;
583 if (ps->color_type != FF_COLOR_YUV)
584 loss |= FF_LOSS_COLORSPACE;
586 case FF_COLOR_YUV_JPEG:
587 if (ps->color_type != FF_COLOR_YUV_JPEG &&
588 ps->color_type != FF_COLOR_YUV &&
589 ps->color_type != FF_COLOR_GRAY)
590 loss |= FF_LOSS_COLORSPACE;
594 if (ps->color_type != pf->color_type)
595 loss |= FF_LOSS_COLORSPACE;
598 if (pf->color_type == FF_COLOR_GRAY &&
599 ps->color_type != FF_COLOR_GRAY)
600 loss |= FF_LOSS_CHROMA;
601 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
602 loss |= FF_LOSS_ALPHA;
603 if (pf->pixel_type == FF_PIXEL_PALETTE &&
604 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
605 loss |= FF_LOSS_COLORQUANT;
609 static int avg_bits_per_pixel(int pix_fmt)
612 const PixFmtInfo *pf;
614 pf = &pix_fmt_info[pix_fmt];
615 switch(pf->pixel_type) {
616 case FF_PIXEL_PACKED:
619 case PIX_FMT_UYVY422:
626 case PIX_FMT_UYVY411:
630 bits = pf->depth * pf->nb_channels;
634 case FF_PIXEL_PLANAR:
635 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
636 bits = pf->depth * pf->nb_channels;
638 bits = pf->depth + ((2 * pf->depth) >>
639 (pf->x_chroma_shift + pf->y_chroma_shift));
642 case FF_PIXEL_PALETTE:
652 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
657 int dist, i, loss, min_dist, dst_pix_fmt;
659 /* find exact color match with smallest size */
661 min_dist = 0x7fffffff;
662 for(i = 0;i < PIX_FMT_NB; i++) {
663 if (pix_fmt_mask & (1 << i)) {
664 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
666 dist = avg_bits_per_pixel(i);
667 if (dist < min_dist) {
678 * find best pixel format to convert to. Return -1 if none found
680 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
681 int has_alpha, int *loss_ptr)
683 int dst_pix_fmt, loss_mask, i;
684 static const int loss_mask_order[] = {
685 ~0, /* no loss first */
688 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
694 /* try with successive loss */
697 loss_mask = loss_mask_order[i++];
698 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
699 has_alpha, loss_mask);
700 if (dst_pix_fmt >= 0)
708 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
712 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
713 const uint8_t *src, int src_wrap,
714 int width, int height)
718 for(;height > 0; height--) {
719 memcpy(dst, src, width);
726 * Copy image 'src' to 'dst'.
728 void img_copy(AVPicture *dst, const AVPicture *src,
729 int pix_fmt, int width, int height)
732 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
734 pf = &pix_fmt_info[pix_fmt];
735 switch(pf->pixel_type) {
736 case FF_PIXEL_PACKED:
739 case PIX_FMT_UYVY422:
746 case PIX_FMT_UYVY411:
750 bits = pf->depth * pf->nb_channels;
753 bwidth = (width * bits + 7) >> 3;
754 ff_img_copy_plane(dst->data[0], dst->linesize[0],
755 src->data[0], src->linesize[0],
758 case FF_PIXEL_PLANAR:
759 for(i = 0; i < pf->nb_channels; i++) {
763 if (i == 1 || i == 2) {
764 w >>= pf->x_chroma_shift;
765 h >>= pf->y_chroma_shift;
767 bwidth = (w * pf->depth + 7) >> 3;
768 ff_img_copy_plane(dst->data[i], dst->linesize[i],
769 src->data[i], src->linesize[i],
773 case FF_PIXEL_PALETTE:
774 ff_img_copy_plane(dst->data[0], dst->linesize[0],
775 src->data[0], src->linesize[0],
777 /* copy the palette */
778 ff_img_copy_plane(dst->data[1], dst->linesize[1],
779 src->data[1], src->linesize[1],
785 /* XXX: totally non optimized */
787 static void yuv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
788 int width, int height)
790 const uint8_t *p, *p1;
791 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
799 for(;height >= 1; height -= 2) {
804 for(w = width; w >= 2; w -= 2) {
821 p1 += src->linesize[0];
822 lum1 += dst->linesize[0];
826 for(w = width; w >= 2; w -= 2) {
835 p1 += src->linesize[0];
836 lum1 += dst->linesize[0];
838 cb1 += dst->linesize[1];
839 cr1 += dst->linesize[2];
843 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
844 int width, int height)
846 const uint8_t *p, *p1;
847 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
856 for(;height >= 1; height -= 2) {
861 for(w = width; w >= 2; w -= 2) {
878 p1 += src->linesize[0];
879 lum1 += dst->linesize[0];
883 for(w = width; w >= 2; w -= 2) {
892 p1 += src->linesize[0];
893 lum1 += dst->linesize[0];
895 cb1 += dst->linesize[1];
896 cr1 += dst->linesize[2];
901 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
902 int width, int height)
904 const uint8_t *p, *p1;
905 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
912 for(;height > 0; height--) {
917 for(w = width; w >= 2; w -= 2) {
927 p1 += src->linesize[0];
928 lum1 += dst->linesize[0];
929 cb1 += dst->linesize[1];
930 cr1 += dst->linesize[2];
935 static void yuv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
936 int width, int height)
938 const uint8_t *p, *p1;
939 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
946 for(;height > 0; height--) {
951 for(w = width; w >= 2; w -= 2) {
961 p1 += src->linesize[0];
962 lum1 += dst->linesize[0];
963 cb1 += dst->linesize[1];
964 cr1 += dst->linesize[2];
968 static void yuv422p_to_yuv422(AVPicture *dst, const AVPicture *src,
969 int width, int height)
972 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
979 for(;height > 0; height--) {
984 for(w = width; w >= 2; w -= 2) {
994 p1 += dst->linesize[0];
995 lum1 += src->linesize[0];
996 cb1 += src->linesize[1];
997 cr1 += src->linesize[2];
1001 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1002 int width, int height)
1005 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1009 lum1 = src->data[0];
1012 for(;height > 0; height--) {
1017 for(w = width; w >= 2; w -= 2) {
1027 p1 += dst->linesize[0];
1028 lum1 += src->linesize[0];
1029 cb1 += src->linesize[1];
1030 cr1 += src->linesize[2];
1034 static void uyvy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1035 int width, int height)
1037 const uint8_t *p, *p1;
1038 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1042 lum1 = dst->data[0];
1045 for(;height > 0; height--) {
1050 for(w = width; w >= 4; w -= 4) {
1062 p1 += src->linesize[0];
1063 lum1 += dst->linesize[0];
1064 cb1 += dst->linesize[1];
1065 cr1 += dst->linesize[2];
1070 static void yuv420p_to_yuv422(AVPicture *dst, const AVPicture *src,
1071 int width, int height)
1074 uint8_t *line1, *line2, *linesrc = dst->data[0];
1075 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1076 uint8_t *cb1, *cb2 = src->data[1];
1077 uint8_t *cr1, *cr2 = src->data[2];
1079 for(h = height / 2; h--;) {
1081 line2 = linesrc + dst->linesize[0];
1084 lum2 = lumsrc + src->linesize[0];
1089 for(w = width / 2; w--;) {
1090 *line1++ = *lum1++; *line2++ = *lum2++;
1091 *line1++ = *line2++ = *cb1++;
1092 *line1++ = *lum1++; *line2++ = *lum2++;
1093 *line1++ = *line2++ = *cr1++;
1096 linesrc += dst->linesize[0] * 2;
1097 lumsrc += src->linesize[0] * 2;
1098 cb2 += src->linesize[1];
1099 cr2 += src->linesize[2];
1103 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1104 int width, int height)
1107 uint8_t *line1, *line2, *linesrc = dst->data[0];
1108 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1109 uint8_t *cb1, *cb2 = src->data[1];
1110 uint8_t *cr1, *cr2 = src->data[2];
1112 for(h = height / 2; h--;) {
1114 line2 = linesrc + dst->linesize[0];
1117 lum2 = lumsrc + src->linesize[0];
1122 for(w = width / 2; w--;) {
1123 *line1++ = *line2++ = *cb1++;
1124 *line1++ = *lum1++; *line2++ = *lum2++;
1125 *line1++ = *line2++ = *cr1++;
1126 *line1++ = *lum1++; *line2++ = *lum2++;
1129 linesrc += dst->linesize[0] * 2;
1130 lumsrc += src->linesize[0] * 2;
1131 cb2 += src->linesize[1];
1132 cr2 += src->linesize[2];
1136 #define SCALEBITS 10
1137 #define ONE_HALF (1 << (SCALEBITS - 1))
1138 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1140 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1144 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1145 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1147 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1150 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1152 y = ((y1) - 16) * FIX(255.0/219.0);\
1153 r = cm[(y + r_add) >> SCALEBITS];\
1154 g = cm[(y + g_add) >> SCALEBITS];\
1155 b = cm[(y + b_add) >> SCALEBITS];\
1158 #define YUV_TO_RGB1(cb1, cr1)\
1162 r_add = FIX(1.40200) * cr + ONE_HALF;\
1163 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1164 b_add = FIX(1.77200) * cb + ONE_HALF;\
1167 #define YUV_TO_RGB2(r, g, b, y1)\
1169 y = (y1) << SCALEBITS;\
1170 r = cm[(y + r_add) >> SCALEBITS];\
1171 g = cm[(y + g_add) >> SCALEBITS];\
1172 b = cm[(y + b_add) >> SCALEBITS];\
1175 #define Y_CCIR_TO_JPEG(y)\
1176 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1178 #define Y_JPEG_TO_CCIR(y)\
1179 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1181 #define C_CCIR_TO_JPEG(y)\
1182 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1184 /* NOTE: the clamp is really necessary! */
1185 static inline int C_JPEG_TO_CCIR(int y) {
1186 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
1193 #define RGB_TO_Y(r, g, b) \
1194 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1195 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1197 #define RGB_TO_U(r1, g1, b1, shift)\
1198 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1199 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1201 #define RGB_TO_V(r1, g1, b1, shift)\
1202 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1203 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1205 #define RGB_TO_Y_CCIR(r, g, b) \
1206 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1207 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1209 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1210 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1211 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1213 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1214 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1215 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1217 static uint8_t y_ccir_to_jpeg[256];
1218 static uint8_t y_jpeg_to_ccir[256];
1219 static uint8_t c_ccir_to_jpeg[256];
1220 static uint8_t c_jpeg_to_ccir[256];
1222 /* init various conversion tables */
1223 static void img_convert_init(void)
1226 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1228 for(i = 0;i < 256; i++) {
1229 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1230 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1231 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1232 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1236 /* apply to each pixel the given table */
1237 static void img_apply_table(uint8_t *dst, int dst_wrap,
1238 const uint8_t *src, int src_wrap,
1239 int width, int height, const uint8_t *table1)
1244 const uint8_t *table;
1247 for(;height > 0; height--) {
1271 /* XXX: use generic filter ? */
1272 /* XXX: in most cases, the sampling position is incorrect */
1275 static void shrink41(uint8_t *dst, int dst_wrap,
1276 const uint8_t *src, int src_wrap,
1277 int width, int height)
1283 for(;height > 0; height--) {
1286 for(w = width;w > 0; w--) {
1287 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1297 static void shrink21(uint8_t *dst, int dst_wrap,
1298 const uint8_t *src, int src_wrap,
1299 int width, int height)
1305 for(;height > 0; height--) {
1308 for(w = width;w > 0; w--) {
1309 d[0] = (s[0] + s[1]) >> 1;
1319 static void shrink12(uint8_t *dst, int dst_wrap,
1320 const uint8_t *src, int src_wrap,
1321 int width, int height)
1325 const uint8_t *s1, *s2;
1327 for(;height > 0; height--) {
1331 for(w = width;w >= 4; w-=4) {
1332 d[0] = (s1[0] + s2[0]) >> 1;
1333 d[1] = (s1[1] + s2[1]) >> 1;
1334 d[2] = (s1[2] + s2[2]) >> 1;
1335 d[3] = (s1[3] + s2[3]) >> 1;
1341 d[0] = (s1[0] + s2[0]) >> 1;
1346 src += 2 * src_wrap;
1352 void ff_shrink22(uint8_t *dst, int dst_wrap,
1353 const uint8_t *src, int src_wrap,
1354 int width, int height)
1357 const uint8_t *s1, *s2;
1360 for(;height > 0; height--) {
1364 for(w = width;w >= 4; w-=4) {
1365 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1366 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1367 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1368 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1374 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1379 src += 2 * src_wrap;
1385 void ff_shrink44(uint8_t *dst, int dst_wrap,
1386 const uint8_t *src, int src_wrap,
1387 int width, int height)
1390 const uint8_t *s1, *s2, *s3, *s4;
1393 for(;height > 0; height--) {
1399 for(w = width;w > 0; w--) {
1400 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1401 s2[0] + s2[1] + s2[2] + s2[3] +
1402 s3[0] + s3[1] + s3[2] + s3[3] +
1403 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1410 src += 4 * src_wrap;
1416 void ff_shrink88(uint8_t *dst, int dst_wrap,
1417 const uint8_t *src, int src_wrap,
1418 int width, int height)
1422 for(;height > 0; height--) {
1423 for(w = width;w > 0; w--) {
1426 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1429 *(dst++) = (tmp + 32)>>6;
1430 src += 8 - 8*src_wrap;
1432 src += 8*src_wrap - 8*width;
1433 dst += dst_wrap - width;
1437 static void grow21_line(uint8_t *dst, const uint8_t *src,
1446 for(w = width;w >= 4; w-=4) {
1447 d[1] = d[0] = s1[0];
1448 d[3] = d[2] = s1[1];
1452 for(;w >= 2; w -= 2) {
1453 d[1] = d[0] = s1[0];
1457 /* only needed if width is not a multiple of two */
1458 /* XXX: veryfy that */
1464 static void grow41_line(uint8_t *dst, const uint8_t *src,
1473 for(w = width;w >= 4; w-=4) {
1485 static void grow21(uint8_t *dst, int dst_wrap,
1486 const uint8_t *src, int src_wrap,
1487 int width, int height)
1489 for(;height > 0; height--) {
1490 grow21_line(dst, src, width);
1497 static void grow22(uint8_t *dst, int dst_wrap,
1498 const uint8_t *src, int src_wrap,
1499 int width, int height)
1501 for(;height > 0; height--) {
1502 grow21_line(dst, src, width);
1510 static void grow41(uint8_t *dst, int dst_wrap,
1511 const uint8_t *src, int src_wrap,
1512 int width, int height)
1514 for(;height > 0; height--) {
1515 grow41_line(dst, src, width);
1522 static void grow44(uint8_t *dst, int dst_wrap,
1523 const uint8_t *src, int src_wrap,
1524 int width, int height)
1526 for(;height > 0; height--) {
1527 grow41_line(dst, src, width);
1528 if ((height & 3) == 1)
1535 static void conv411(uint8_t *dst, int dst_wrap,
1536 const uint8_t *src, int src_wrap,
1537 int width, int height)
1540 const uint8_t *s1, *s2;
1545 for(;height > 0; height--) {
1547 s2 = src + src_wrap;
1549 for(w = width;w > 0; w--) {
1550 c = (s1[0] + s2[0]) >> 1;
1557 src += src_wrap * 2;
1562 /* XXX: add jpeg quantize code */
1564 #define TRANSP_INDEX (6*6*6)
1566 /* this is maybe slow, but allows for extensions */
1567 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1569 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1572 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1575 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1578 pal = (uint32_t *)palette;
1580 for(r = 0; r < 6; r++) {
1581 for(g = 0; g < 6; g++) {
1582 for(b = 0; b < 6; b++) {
1583 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1584 (pal_value[g] << 8) | pal_value[b];
1591 pal[i++] = 0xff000000;
1594 /* copy bit n to bits 0 ... n - 1 */
1595 static inline unsigned int bitcopy_n(unsigned int a, int n)
1598 mask = (1 << n) - 1;
1599 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1602 /* rgb555 handling */
1604 #define RGB_NAME rgb555
1606 #define RGB_IN(r, g, b, s)\
1608 unsigned int v = ((const uint16_t *)(s))[0];\
1609 r = bitcopy_n(v >> (10 - 3), 3);\
1610 g = bitcopy_n(v >> (5 - 3), 3);\
1611 b = bitcopy_n(v << 3, 3);\
1614 #define RGBA_IN(r, g, b, a, s)\
1616 unsigned int v = ((const uint16_t *)(s))[0];\
1617 r = bitcopy_n(v >> (10 - 3), 3);\
1618 g = bitcopy_n(v >> (5 - 3), 3);\
1619 b = bitcopy_n(v << 3, 3);\
1620 a = (-(v >> 15)) & 0xff;\
1623 #define RGBA_OUT(d, r, g, b, a)\
1625 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1626 ((a << 8) & 0x8000);\
1631 #include "imgconvert_template.h"
1633 /* rgb565 handling */
1635 #define RGB_NAME rgb565
1637 #define RGB_IN(r, g, b, s)\
1639 unsigned int v = ((const uint16_t *)(s))[0];\
1640 r = bitcopy_n(v >> (11 - 3), 3);\
1641 g = bitcopy_n(v >> (5 - 2), 2);\
1642 b = bitcopy_n(v << 3, 3);\
1645 #define RGB_OUT(d, r, g, b)\
1647 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1652 #include "imgconvert_template.h"
1654 /* bgr24 handling */
1656 #define RGB_NAME bgr24
1658 #define RGB_IN(r, g, b, s)\
1665 #define RGB_OUT(d, r, g, b)\
1674 #include "imgconvert_template.h"
1680 /* rgb24 handling */
1682 #define RGB_NAME rgb24
1685 #define RGB_IN(r, g, b, s)\
1692 #define RGB_OUT(d, r, g, b)\
1701 #include "imgconvert_template.h"
1703 /* rgba32 handling */
1705 #define RGB_NAME rgba32
1708 #define RGB_IN(r, g, b, s)\
1710 unsigned int v = ((const uint32_t *)(s))[0];\
1711 r = (v >> 16) & 0xff;\
1712 g = (v >> 8) & 0xff;\
1716 #define RGBA_IN(r, g, b, a, s)\
1718 unsigned int v = ((const uint32_t *)(s))[0];\
1719 a = (v >> 24) & 0xff;\
1720 r = (v >> 16) & 0xff;\
1721 g = (v >> 8) & 0xff;\
1725 #define RGBA_OUT(d, r, g, b, a)\
1727 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1732 #include "imgconvert_template.h"
1734 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1735 int width, int height, int xor_mask)
1737 const unsigned char *p;
1739 int v, dst_wrap, src_wrap;
1743 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1746 dst_wrap = dst->linesize[0] - width;
1747 for(y=0;y<height;y++) {
1750 v = *p++ ^ xor_mask;
1752 q[1] = -((v >> 6) & 1);
1753 q[2] = -((v >> 5) & 1);
1754 q[3] = -((v >> 4) & 1);
1755 q[4] = -((v >> 3) & 1);
1756 q[5] = -((v >> 2) & 1);
1757 q[6] = -((v >> 1) & 1);
1758 q[7] = -((v >> 0) & 1);
1763 v = *p++ ^ xor_mask;
1765 q[0] = -((v >> 7) & 1);
1775 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1776 int width, int height)
1778 mono_to_gray(dst, src, width, height, 0xff);
1781 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1782 int width, int height)
1784 mono_to_gray(dst, src, width, height, 0x00);
1787 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1788 int width, int height, int xor_mask)
1793 int j, b, v, n1, src_wrap, dst_wrap, y;
1796 src_wrap = src->linesize[0] - width;
1799 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1801 for(y=0;y<height;y++) {
1808 v = (v << 1) | (b >> 7);
1810 d[0] = v ^ xor_mask;
1820 v = (v << 1) | (b >> 7);
1823 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1831 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1832 int width, int height)
1834 gray_to_mono(dst, src, width, height, 0xff);
1837 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1838 int width, int height)
1840 gray_to_mono(dst, src, width, height, 0x00);
1843 typedef struct ConvertEntry {
1844 void (*convert)(AVPicture *dst,
1845 const AVPicture *src, int width, int height);
1848 /* Add each new convertion function in this table. In order to be able
1849 to convert from any format to any format, the following constraints
1852 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1854 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1856 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1858 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1861 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1863 The other conversion functions are just optimisations for common cases.
1865 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1866 [PIX_FMT_YUV420P] = {
1867 [PIX_FMT_YUV422] = {
1868 .convert = yuv420p_to_yuv422,
1870 [PIX_FMT_RGB555] = {
1871 .convert = yuv420p_to_rgb555
1873 [PIX_FMT_RGB565] = {
1874 .convert = yuv420p_to_rgb565
1877 .convert = yuv420p_to_bgr24
1880 .convert = yuv420p_to_rgb24
1882 [PIX_FMT_RGBA32] = {
1883 .convert = yuv420p_to_rgba32
1885 [PIX_FMT_UYVY422] = {
1886 .convert = yuv420p_to_uyvy422,
1889 [PIX_FMT_YUV422P] = {
1890 [PIX_FMT_YUV422] = {
1891 .convert = yuv422p_to_yuv422,
1893 [PIX_FMT_UYVY422] = {
1894 .convert = yuv422p_to_uyvy422,
1897 [PIX_FMT_YUV444P] = {
1899 .convert = yuv444p_to_rgb24
1902 [PIX_FMT_YUVJ420P] = {
1903 [PIX_FMT_RGB555] = {
1904 .convert = yuvj420p_to_rgb555
1906 [PIX_FMT_RGB565] = {
1907 .convert = yuvj420p_to_rgb565
1910 .convert = yuvj420p_to_bgr24
1913 .convert = yuvj420p_to_rgb24
1915 [PIX_FMT_RGBA32] = {
1916 .convert = yuvj420p_to_rgba32
1919 [PIX_FMT_YUVJ444P] = {
1921 .convert = yuvj444p_to_rgb24
1924 [PIX_FMT_YUV422] = {
1925 [PIX_FMT_YUV420P] = {
1926 .convert = yuv422_to_yuv420p,
1928 [PIX_FMT_YUV422P] = {
1929 .convert = yuv422_to_yuv422p,
1932 [PIX_FMT_UYVY422] = {
1933 [PIX_FMT_YUV420P] = {
1934 .convert = uyvy422_to_yuv420p,
1936 [PIX_FMT_YUV422P] = {
1937 .convert = uyvy422_to_yuv422p,
1941 [PIX_FMT_YUV420P] = {
1942 .convert = rgb24_to_yuv420p
1944 [PIX_FMT_RGB565] = {
1945 .convert = rgb24_to_rgb565
1947 [PIX_FMT_RGB555] = {
1948 .convert = rgb24_to_rgb555
1950 [PIX_FMT_RGBA32] = {
1951 .convert = rgb24_to_rgba32
1954 .convert = rgb24_to_bgr24
1957 .convert = rgb24_to_gray
1960 .convert = rgb24_to_pal8
1962 [PIX_FMT_YUV444P] = {
1963 .convert = rgb24_to_yuv444p
1965 [PIX_FMT_YUVJ420P] = {
1966 .convert = rgb24_to_yuvj420p
1968 [PIX_FMT_YUVJ444P] = {
1969 .convert = rgb24_to_yuvj444p
1972 [PIX_FMT_RGBA32] = {
1974 .convert = rgba32_to_rgb24
1976 [PIX_FMT_RGB555] = {
1977 .convert = rgba32_to_rgb555
1980 .convert = rgba32_to_pal8
1982 [PIX_FMT_YUV420P] = {
1983 .convert = rgba32_to_yuv420p
1986 .convert = rgba32_to_gray
1991 .convert = bgr24_to_rgb24
1993 [PIX_FMT_YUV420P] = {
1994 .convert = bgr24_to_yuv420p
1997 .convert = bgr24_to_gray
2000 [PIX_FMT_RGB555] = {
2002 .convert = rgb555_to_rgb24
2004 [PIX_FMT_RGBA32] = {
2005 .convert = rgb555_to_rgba32
2007 [PIX_FMT_YUV420P] = {
2008 .convert = rgb555_to_yuv420p
2011 .convert = rgb555_to_gray
2014 [PIX_FMT_RGB565] = {
2016 .convert = rgb565_to_rgb24
2018 [PIX_FMT_YUV420P] = {
2019 .convert = rgb565_to_yuv420p
2022 .convert = rgb565_to_gray
2026 [PIX_FMT_RGB555] = {
2027 .convert = gray_to_rgb555
2029 [PIX_FMT_RGB565] = {
2030 .convert = gray_to_rgb565
2033 .convert = gray_to_rgb24
2036 .convert = gray_to_bgr24
2038 [PIX_FMT_RGBA32] = {
2039 .convert = gray_to_rgba32
2041 [PIX_FMT_MONOWHITE] = {
2042 .convert = gray_to_monowhite
2044 [PIX_FMT_MONOBLACK] = {
2045 .convert = gray_to_monoblack
2048 [PIX_FMT_MONOWHITE] = {
2050 .convert = monowhite_to_gray
2053 [PIX_FMT_MONOBLACK] = {
2055 .convert = monoblack_to_gray
2059 [PIX_FMT_RGB555] = {
2060 .convert = pal8_to_rgb555
2062 [PIX_FMT_RGB565] = {
2063 .convert = pal8_to_rgb565
2066 .convert = pal8_to_bgr24
2069 .convert = pal8_to_rgb24
2071 [PIX_FMT_RGBA32] = {
2072 .convert = pal8_to_rgba32
2075 [PIX_FMT_UYVY411] = {
2076 [PIX_FMT_YUV411P] = {
2077 .convert = uyvy411_to_yuv411p,
2083 int avpicture_alloc(AVPicture *picture,
2084 int pix_fmt, int width, int height)
2089 size = avpicture_get_size(pix_fmt, width, height);
2092 ptr = av_malloc(size);
2095 avpicture_fill(picture, ptr, pix_fmt, width, height);
2098 memset(picture, 0, sizeof(AVPicture));
2102 void avpicture_free(AVPicture *picture)
2104 av_free(picture->data[0]);
2107 /* return true if yuv planar */
2108 static inline int is_yuv_planar(const PixFmtInfo *ps)
2110 return (ps->color_type == FF_COLOR_YUV ||
2111 ps->color_type == FF_COLOR_YUV_JPEG) &&
2112 ps->pixel_type == FF_PIXEL_PLANAR;
2116 * Crop image top and left side
2118 int img_crop(AVPicture *dst, const AVPicture *src,
2119 int pix_fmt, int top_band, int left_band)
2124 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2127 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2128 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2130 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2131 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2132 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2134 dst->linesize[0] = src->linesize[0];
2135 dst->linesize[1] = src->linesize[1];
2136 dst->linesize[2] = src->linesize[2];
2143 int img_pad(AVPicture *dst, const AVPicture *src, int height, int width, int pix_fmt,
2144 int padtop, int padbottom, int padleft, int padright, int *color)
2146 uint8_t *optr, *iptr;
2152 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2155 for (i = 0; i < 3; i++) {
2156 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2157 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2159 if (padtop || padleft) {
2160 memset(dst->data[i], color[i], dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2163 if (padleft || padright || src) {
2164 if (src) { /* first line */
2165 iptr = src->data[i];
2166 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift);
2167 memcpy(optr, iptr, src->linesize[i]);
2168 iptr += src->linesize[i];
2170 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) + (dst->linesize[i] - (padright >> x_shift));
2171 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2172 for (y = 0; y < yheight; y++) {
2173 memset(optr, color[i], (padleft + padright) >> x_shift);
2175 memcpy(optr + ((padleft + padright) >> x_shift), iptr, src->linesize[i]);
2176 iptr += src->linesize[i];
2178 optr += dst->linesize[i];
2182 if (padbottom || padright) {
2183 optr = dst->data[i] + dst->linesize[i] * ((height - padbottom) >> y_shift) - (padright >> x_shift);
2184 memset(optr, color[i], dst->linesize[i] * (padbottom >> y_shift) + (padright >> x_shift));
2190 #ifndef CONFIG_SWSCALER
2191 /* XXX: always use linesize. Return -1 if not supported */
2192 int img_convert(AVPicture *dst, int dst_pix_fmt,
2193 const AVPicture *src, int src_pix_fmt,
2194 int src_width, int src_height)
2197 int i, ret, dst_width, dst_height, int_pix_fmt;
2198 const PixFmtInfo *src_pix, *dst_pix;
2199 const ConvertEntry *ce;
2200 AVPicture tmp1, *tmp = &tmp1;
2202 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2203 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2205 if (src_width <= 0 || src_height <= 0)
2213 dst_width = src_width;
2214 dst_height = src_height;
2216 dst_pix = &pix_fmt_info[dst_pix_fmt];
2217 src_pix = &pix_fmt_info[src_pix_fmt];
2218 if (src_pix_fmt == dst_pix_fmt) {
2219 /* no conversion needed: just copy */
2220 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2224 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2226 /* specific conversion routine */
2227 ce->convert(dst, src, dst_width, dst_height);
2232 if (is_yuv_planar(dst_pix) &&
2233 src_pix_fmt == PIX_FMT_GRAY8) {
2237 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2238 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2239 src->data[0], src->linesize[0],
2240 dst_width, dst_height);
2242 img_apply_table(dst->data[0], dst->linesize[0],
2243 src->data[0], src->linesize[0],
2244 dst_width, dst_height,
2247 /* fill U and V with 128 */
2250 w >>= dst_pix->x_chroma_shift;
2251 h >>= dst_pix->y_chroma_shift;
2252 for(i = 1; i <= 2; i++) {
2254 for(y = 0; y< h; y++) {
2256 d += dst->linesize[i];
2263 if (is_yuv_planar(src_pix) &&
2264 dst_pix_fmt == PIX_FMT_GRAY8) {
2265 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2266 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2267 src->data[0], src->linesize[0],
2268 dst_width, dst_height);
2270 img_apply_table(dst->data[0], dst->linesize[0],
2271 src->data[0], src->linesize[0],
2272 dst_width, dst_height,
2278 /* YUV to YUV planar */
2279 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2280 int x_shift, y_shift, w, h, xy_shift;
2281 void (*resize_func)(uint8_t *dst, int dst_wrap,
2282 const uint8_t *src, int src_wrap,
2283 int width, int height);
2285 /* compute chroma size of the smallest dimensions */
2288 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2289 w >>= dst_pix->x_chroma_shift;
2291 w >>= src_pix->x_chroma_shift;
2292 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2293 h >>= dst_pix->y_chroma_shift;
2295 h >>= src_pix->y_chroma_shift;
2297 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2298 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2299 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2300 /* there must be filters for conversion at least from and to
2304 resize_func = ff_img_copy_plane;
2307 resize_func = shrink21;
2310 resize_func = shrink41;
2313 resize_func = shrink12;
2316 resize_func = ff_shrink22;
2319 resize_func = ff_shrink44;
2322 resize_func = grow21;
2325 resize_func = grow41;
2328 resize_func = grow22;
2331 resize_func = grow44;
2334 resize_func = conv411;
2337 /* currently not handled */
2338 goto no_chroma_filter;
2341 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2342 src->data[0], src->linesize[0],
2343 dst_width, dst_height);
2345 for(i = 1;i <= 2; i++)
2346 resize_func(dst->data[i], dst->linesize[i],
2347 src->data[i], src->linesize[i],
2348 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2349 /* if yuv color space conversion is needed, we do it here on
2350 the destination image */
2351 if (dst_pix->color_type != src_pix->color_type) {
2352 const uint8_t *y_table, *c_table;
2353 if (dst_pix->color_type == FF_COLOR_YUV) {
2354 y_table = y_jpeg_to_ccir;
2355 c_table = c_jpeg_to_ccir;
2357 y_table = y_ccir_to_jpeg;
2358 c_table = c_ccir_to_jpeg;
2360 img_apply_table(dst->data[0], dst->linesize[0],
2361 dst->data[0], dst->linesize[0],
2362 dst_width, dst_height,
2365 for(i = 1;i <= 2; i++)
2366 img_apply_table(dst->data[i], dst->linesize[i],
2367 dst->data[i], dst->linesize[i],
2368 dst_width>>dst_pix->x_chroma_shift,
2369 dst_height>>dst_pix->y_chroma_shift,
2376 /* try to use an intermediate format */
2377 if (src_pix_fmt == PIX_FMT_YUV422 ||
2378 dst_pix_fmt == PIX_FMT_YUV422) {
2379 /* specific case: convert to YUV422P first */
2380 int_pix_fmt = PIX_FMT_YUV422P;
2381 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2382 dst_pix_fmt == PIX_FMT_UYVY422) {
2383 /* specific case: convert to YUV422P first */
2384 int_pix_fmt = PIX_FMT_YUV422P;
2385 } else if (src_pix_fmt == PIX_FMT_UYVY411 ||
2386 dst_pix_fmt == PIX_FMT_UYVY411) {
2387 /* specific case: convert to YUV411P first */
2388 int_pix_fmt = PIX_FMT_YUV411P;
2389 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2390 src_pix_fmt != PIX_FMT_GRAY8) ||
2391 (dst_pix->color_type == FF_COLOR_GRAY &&
2392 dst_pix_fmt != PIX_FMT_GRAY8)) {
2393 /* gray8 is the normalized format */
2394 int_pix_fmt = PIX_FMT_GRAY8;
2395 } else if ((is_yuv_planar(src_pix) &&
2396 src_pix_fmt != PIX_FMT_YUV444P &&
2397 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2398 /* yuv444 is the normalized format */
2399 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2400 int_pix_fmt = PIX_FMT_YUVJ444P;
2402 int_pix_fmt = PIX_FMT_YUV444P;
2403 } else if ((is_yuv_planar(dst_pix) &&
2404 dst_pix_fmt != PIX_FMT_YUV444P &&
2405 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2406 /* yuv444 is the normalized format */
2407 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2408 int_pix_fmt = PIX_FMT_YUVJ444P;
2410 int_pix_fmt = PIX_FMT_YUV444P;
2412 /* the two formats are rgb or gray8 or yuv[j]444p */
2413 if (src_pix->is_alpha && dst_pix->is_alpha)
2414 int_pix_fmt = PIX_FMT_RGBA32;
2416 int_pix_fmt = PIX_FMT_RGB24;
2418 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2421 if (img_convert(tmp, int_pix_fmt,
2422 src, src_pix_fmt, src_width, src_height) < 0)
2424 if (img_convert(dst, dst_pix_fmt,
2425 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2429 avpicture_free(tmp);
2434 /* NOTE: we scan all the pixels to have an exact information */
2435 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2437 const unsigned char *p;
2438 int src_wrap, ret, x, y;
2440 uint32_t *palette = (uint32_t *)src->data[1];
2443 src_wrap = src->linesize[0] - width;
2445 for(y=0;y<height;y++) {
2446 for(x=0;x<width;x++) {
2447 a = palette[p[0]] >> 24;
2449 ret |= FF_ALPHA_TRANSP;
2450 } else if (a != 0xff) {
2451 ret |= FF_ALPHA_SEMI_TRANSP;
2461 * Tell if an image really has transparent alpha values.
2462 * @return ored mask of FF_ALPHA_xxx constants
2464 int img_get_alpha_info(const AVPicture *src,
2465 int pix_fmt, int width, int height)
2467 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2470 pf = &pix_fmt_info[pix_fmt];
2471 /* no alpha can be represented in format */
2475 case PIX_FMT_RGBA32:
2476 ret = get_alpha_info_rgba32(src, width, height);
2478 case PIX_FMT_RGB555:
2479 ret = get_alpha_info_rgb555(src, width, height);
2482 ret = get_alpha_info_pal8(src, width, height);
2485 /* we do not know, so everything is indicated */
2486 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2493 #define DEINT_INPLACE_LINE_LUM \
2494 movd_m2r(lum_m4[0],mm0);\
2495 movd_m2r(lum_m3[0],mm1);\
2496 movd_m2r(lum_m2[0],mm2);\
2497 movd_m2r(lum_m1[0],mm3);\
2498 movd_m2r(lum[0],mm4);\
2499 punpcklbw_r2r(mm7,mm0);\
2500 movd_r2m(mm2,lum_m4[0]);\
2501 punpcklbw_r2r(mm7,mm1);\
2502 punpcklbw_r2r(mm7,mm2);\
2503 punpcklbw_r2r(mm7,mm3);\
2504 punpcklbw_r2r(mm7,mm4);\
2505 paddw_r2r(mm3,mm1);\
2507 paddw_r2r(mm4,mm0);\
2509 paddw_r2r(mm6,mm2);\
2510 paddw_r2r(mm2,mm1);\
2511 psubusw_r2r(mm0,mm1);\
2513 packuswb_r2r(mm7,mm1);\
2514 movd_r2m(mm1,lum_m2[0]);
2516 #define DEINT_LINE_LUM \
2517 movd_m2r(lum_m4[0],mm0);\
2518 movd_m2r(lum_m3[0],mm1);\
2519 movd_m2r(lum_m2[0],mm2);\
2520 movd_m2r(lum_m1[0],mm3);\
2521 movd_m2r(lum[0],mm4);\
2522 punpcklbw_r2r(mm7,mm0);\
2523 punpcklbw_r2r(mm7,mm1);\
2524 punpcklbw_r2r(mm7,mm2);\
2525 punpcklbw_r2r(mm7,mm3);\
2526 punpcklbw_r2r(mm7,mm4);\
2527 paddw_r2r(mm3,mm1);\
2529 paddw_r2r(mm4,mm0);\
2531 paddw_r2r(mm6,mm2);\
2532 paddw_r2r(mm2,mm1);\
2533 psubusw_r2r(mm0,mm1);\
2535 packuswb_r2r(mm7,mm1);\
2536 movd_r2m(mm1,dst[0]);
2539 /* filter parameters: [-1 4 2 4 -1] // 8 */
2540 static void deinterlace_line(uint8_t *dst,
2541 const uint8_t *lum_m4, const uint8_t *lum_m3,
2542 const uint8_t *lum_m2, const uint8_t *lum_m1,
2547 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2550 for(;size > 0;size--) {
2552 sum += lum_m3[0] << 2;
2553 sum += lum_m2[0] << 1;
2554 sum += lum_m1[0] << 2;
2556 dst[0] = cm[(sum + 4) >> 3];
2573 movq_m2r(rounder,mm6);
2575 for (;size > 3; size-=4) {
2586 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2590 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2593 for(;size > 0;size--) {
2595 sum += lum_m3[0] << 2;
2596 sum += lum_m2[0] << 1;
2597 lum_m4[0]=lum_m2[0];
2598 sum += lum_m1[0] << 2;
2600 lum_m2[0] = cm[(sum + 4) >> 3];
2616 movq_m2r(rounder,mm6);
2618 for (;size > 3; size-=4) {
2619 DEINT_INPLACE_LINE_LUM
2629 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2630 top field is copied as is, but the bottom field is deinterlaced
2631 against the top field. */
2632 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2633 const uint8_t *src1, int src_wrap,
2634 int width, int height)
2636 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2641 src_0=&src_m1[src_wrap];
2642 src_p1=&src_0[src_wrap];
2643 src_p2=&src_p1[src_wrap];
2644 for(y=0;y<(height-2);y+=2) {
2645 memcpy(dst,src_m1,width);
2647 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2651 src_p1 += 2*src_wrap;
2652 src_p2 += 2*src_wrap;
2655 memcpy(dst,src_m1,width);
2658 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2661 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2662 int width, int height)
2664 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2667 buf = (uint8_t*)av_malloc(width);
2670 memcpy(buf,src_m1,width);
2671 src_0=&src_m1[src_wrap];
2672 src_p1=&src_0[src_wrap];
2673 src_p2=&src_p1[src_wrap];
2674 for(y=0;y<(height-2);y+=2) {
2675 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2678 src_p1 += 2*src_wrap;
2679 src_p2 += 2*src_wrap;
2682 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2687 /* deinterlace - if not supported return -1 */
2688 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2689 int pix_fmt, int width, int height)
2693 if (pix_fmt != PIX_FMT_YUV420P &&
2694 pix_fmt != PIX_FMT_YUV422P &&
2695 pix_fmt != PIX_FMT_YUV444P &&
2696 pix_fmt != PIX_FMT_YUV411P)
2698 if ((width & 3) != 0 || (height & 3) != 0)
2704 case PIX_FMT_YUV420P:
2708 case PIX_FMT_YUV422P:
2711 case PIX_FMT_YUV411P:
2719 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2722 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2723 src->data[i], src->linesize[i],