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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "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 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 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
242 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
243 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
246 const char *avcodec_get_pix_fmt_name(int pix_fmt)
248 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
251 return pix_fmt_info[pix_fmt].name;
254 enum PixelFormat avcodec_get_pix_fmt(const char* name)
258 for (i=0; i < PIX_FMT_NB; i++)
259 if (!strcmp(pix_fmt_info[i].name, name))
264 /* Picture field are filled with 'ptr' addresses. Also return size */
265 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
266 int pix_fmt, int width, int height)
268 int size, w2, h2, size2;
271 if(avcodec_check_dimensions(NULL, width, height))
274 pinfo = &pix_fmt_info[pix_fmt];
275 size = width * height;
277 case PIX_FMT_YUV420P:
278 case PIX_FMT_YUV422P:
279 case PIX_FMT_YUV444P:
280 case PIX_FMT_YUV410P:
281 case PIX_FMT_YUV411P:
282 case PIX_FMT_YUVJ420P:
283 case PIX_FMT_YUVJ422P:
284 case PIX_FMT_YUVJ444P:
285 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
286 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
288 picture->data[0] = ptr;
289 picture->data[1] = picture->data[0] + size;
290 picture->data[2] = picture->data[1] + size2;
291 picture->linesize[0] = width;
292 picture->linesize[1] = w2;
293 picture->linesize[2] = w2;
294 return size + 2 * size2;
297 picture->data[0] = ptr;
298 picture->data[1] = NULL;
299 picture->data[2] = NULL;
300 picture->linesize[0] = width * 3;
303 picture->data[0] = ptr;
304 picture->data[1] = NULL;
305 picture->data[2] = NULL;
306 picture->linesize[0] = width * 4;
311 picture->data[0] = ptr;
312 picture->data[1] = NULL;
313 picture->data[2] = NULL;
314 picture->linesize[0] = width * 2;
316 case PIX_FMT_UYVY422:
317 picture->data[0] = ptr;
318 picture->data[1] = NULL;
319 picture->data[2] = NULL;
320 picture->linesize[0] = width * 2;
322 case PIX_FMT_UYVY411:
323 picture->data[0] = ptr;
324 picture->data[1] = NULL;
325 picture->data[2] = NULL;
326 picture->linesize[0] = width + width/2;
327 return size + size/2;
329 picture->data[0] = ptr;
330 picture->data[1] = NULL;
331 picture->data[2] = NULL;
332 picture->linesize[0] = width;
334 case PIX_FMT_MONOWHITE:
335 case PIX_FMT_MONOBLACK:
336 picture->data[0] = ptr;
337 picture->data[1] = NULL;
338 picture->data[2] = NULL;
339 picture->linesize[0] = (width + 7) >> 3;
340 return picture->linesize[0] * height;
342 size2 = (size + 3) & ~3;
343 picture->data[0] = ptr;
344 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
345 picture->data[2] = NULL;
346 picture->linesize[0] = width;
347 picture->linesize[1] = 4;
348 return size2 + 256 * 4;
351 picture->data[0] = NULL;
352 picture->data[1] = NULL;
353 picture->data[2] = NULL;
354 picture->data[3] = NULL;
359 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
360 unsigned char *dest, int dest_size)
362 PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
363 int i, j, w, h, data_planes;
364 const unsigned char* s;
365 int size = avpicture_get_size(pix_fmt, width, height);
367 if (size > dest_size || size < 0)
370 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
371 if (pix_fmt == PIX_FMT_YUV422 ||
372 pix_fmt == PIX_FMT_UYVY422 ||
373 pix_fmt == PIX_FMT_RGB565 ||
374 pix_fmt == PIX_FMT_RGB555)
376 else if (pix_fmt == PIX_FMT_UYVY411)
378 else if (pix_fmt == PIX_FMT_PAL8)
381 w = width * (pf->depth * pf->nb_channels / 8);
386 data_planes = pf->nb_channels;
387 w = (width*pf->depth + 7)/8;
391 for (i=0; i<data_planes; i++) {
393 w = width >> pf->x_chroma_shift;
394 h = height >> pf->y_chroma_shift;
400 s += src->linesize[i];
404 if (pf->pixel_type == FF_PIXEL_PALETTE)
405 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
410 int avpicture_get_size(int pix_fmt, int width, int height)
412 AVPicture dummy_pict;
413 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
417 * compute the loss when converting from a pixel format to another
419 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
422 const PixFmtInfo *pf, *ps;
425 ps = &pix_fmt_info[src_pix_fmt];
426 pf = &pix_fmt_info[dst_pix_fmt];
430 pf = &pix_fmt_info[dst_pix_fmt];
431 if (pf->depth < ps->depth ||
432 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
433 loss |= FF_LOSS_DEPTH;
434 if (pf->x_chroma_shift > ps->x_chroma_shift ||
435 pf->y_chroma_shift > ps->y_chroma_shift)
436 loss |= FF_LOSS_RESOLUTION;
437 switch(pf->color_type) {
439 if (ps->color_type != FF_COLOR_RGB &&
440 ps->color_type != FF_COLOR_GRAY)
441 loss |= FF_LOSS_COLORSPACE;
444 if (ps->color_type != FF_COLOR_GRAY)
445 loss |= FF_LOSS_COLORSPACE;
448 if (ps->color_type != FF_COLOR_YUV)
449 loss |= FF_LOSS_COLORSPACE;
451 case FF_COLOR_YUV_JPEG:
452 if (ps->color_type != FF_COLOR_YUV_JPEG &&
453 ps->color_type != FF_COLOR_YUV &&
454 ps->color_type != FF_COLOR_GRAY)
455 loss |= FF_LOSS_COLORSPACE;
459 if (ps->color_type != pf->color_type)
460 loss |= FF_LOSS_COLORSPACE;
463 if (pf->color_type == FF_COLOR_GRAY &&
464 ps->color_type != FF_COLOR_GRAY)
465 loss |= FF_LOSS_CHROMA;
466 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
467 loss |= FF_LOSS_ALPHA;
468 if (pf->pixel_type == FF_PIXEL_PALETTE &&
469 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
470 loss |= FF_LOSS_COLORQUANT;
474 static int avg_bits_per_pixel(int pix_fmt)
477 const PixFmtInfo *pf;
479 pf = &pix_fmt_info[pix_fmt];
480 switch(pf->pixel_type) {
481 case FF_PIXEL_PACKED:
484 case PIX_FMT_UYVY422:
489 case PIX_FMT_UYVY411:
493 bits = pf->depth * pf->nb_channels;
497 case FF_PIXEL_PLANAR:
498 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
499 bits = pf->depth * pf->nb_channels;
501 bits = pf->depth + ((2 * pf->depth) >>
502 (pf->x_chroma_shift + pf->y_chroma_shift));
505 case FF_PIXEL_PALETTE:
515 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
520 int dist, i, loss, min_dist, dst_pix_fmt;
522 /* find exact color match with smallest size */
524 min_dist = 0x7fffffff;
525 for(i = 0;i < PIX_FMT_NB; i++) {
526 if (pix_fmt_mask & (1 << i)) {
527 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
529 dist = avg_bits_per_pixel(i);
530 if (dist < min_dist) {
541 * find best pixel format to convert to. Return -1 if none found
543 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
544 int has_alpha, int *loss_ptr)
546 int dst_pix_fmt, loss_mask, i;
547 static const int loss_mask_order[] = {
548 ~0, /* no loss first */
551 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
557 /* try with successive loss */
560 loss_mask = loss_mask_order[i++];
561 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
562 has_alpha, loss_mask);
563 if (dst_pix_fmt >= 0)
571 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
575 static void img_copy_plane(uint8_t *dst, int dst_wrap,
576 const uint8_t *src, int src_wrap,
577 int width, int height)
579 for(;height > 0; height--) {
580 memcpy(dst, src, width);
587 * Copy image 'src' to 'dst'.
589 void img_copy(AVPicture *dst, const AVPicture *src,
590 int pix_fmt, int width, int height)
593 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
595 pf = &pix_fmt_info[pix_fmt];
596 switch(pf->pixel_type) {
597 case FF_PIXEL_PACKED:
600 case PIX_FMT_UYVY422:
605 case PIX_FMT_UYVY411:
609 bits = pf->depth * pf->nb_channels;
612 bwidth = (width * bits + 7) >> 3;
613 img_copy_plane(dst->data[0], dst->linesize[0],
614 src->data[0], src->linesize[0],
617 case FF_PIXEL_PLANAR:
618 for(i = 0; i < pf->nb_channels; i++) {
622 if (i == 1 || i == 2) {
623 w >>= pf->x_chroma_shift;
624 h >>= pf->y_chroma_shift;
626 bwidth = (w * pf->depth + 7) >> 3;
627 img_copy_plane(dst->data[i], dst->linesize[i],
628 src->data[i], src->linesize[i],
632 case FF_PIXEL_PALETTE:
633 img_copy_plane(dst->data[0], dst->linesize[0],
634 src->data[0], src->linesize[0],
636 /* copy the palette */
637 img_copy_plane(dst->data[1], dst->linesize[1],
638 src->data[1], src->linesize[1],
644 /* XXX: totally non optimized */
646 static void yuv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
647 int width, int height)
649 const uint8_t *p, *p1;
650 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
658 for(;height >= 1; height -= 2) {
663 for(w = width; w >= 2; w -= 2) {
680 p1 += src->linesize[0];
681 lum1 += dst->linesize[0];
685 for(w = width; w >= 2; w -= 2) {
694 p1 += src->linesize[0];
695 lum1 += dst->linesize[0];
697 cb1 += dst->linesize[1];
698 cr1 += dst->linesize[2];
702 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
703 int width, int height)
705 const uint8_t *p, *p1;
706 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
715 for(;height >= 1; height -= 2) {
720 for(w = width; w >= 2; w -= 2) {
737 p1 += src->linesize[0];
738 lum1 += dst->linesize[0];
742 for(w = width; w >= 2; w -= 2) {
751 p1 += src->linesize[0];
752 lum1 += dst->linesize[0];
754 cb1 += dst->linesize[1];
755 cr1 += dst->linesize[2];
760 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
761 int width, int height)
763 const uint8_t *p, *p1;
764 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
771 for(;height > 0; height--) {
776 for(w = width; w >= 2; w -= 2) {
786 p1 += src->linesize[0];
787 lum1 += dst->linesize[0];
788 cb1 += dst->linesize[1];
789 cr1 += dst->linesize[2];
794 static void yuv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
795 int width, int height)
797 const uint8_t *p, *p1;
798 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
805 for(;height > 0; height--) {
810 for(w = width; w >= 2; w -= 2) {
820 p1 += src->linesize[0];
821 lum1 += dst->linesize[0];
822 cb1 += dst->linesize[1];
823 cr1 += dst->linesize[2];
827 static void yuv422p_to_yuv422(AVPicture *dst, const AVPicture *src,
828 int width, int height)
831 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
838 for(;height > 0; height--) {
843 for(w = width; w >= 2; w -= 2) {
853 p1 += dst->linesize[0];
854 lum1 += src->linesize[0];
855 cb1 += src->linesize[1];
856 cr1 += src->linesize[2];
860 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
861 int width, int height)
864 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
871 for(;height > 0; height--) {
876 for(w = width; w >= 2; w -= 2) {
886 p1 += dst->linesize[0];
887 lum1 += src->linesize[0];
888 cb1 += src->linesize[1];
889 cr1 += src->linesize[2];
893 static void uyvy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
894 int width, int height)
896 const uint8_t *p, *p1;
897 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
904 for(;height > 0; height--) {
909 for(w = width; w >= 4; w -= 4) {
921 p1 += src->linesize[0];
922 lum1 += dst->linesize[0];
923 cb1 += dst->linesize[1];
924 cr1 += dst->linesize[2];
929 static void yuv420p_to_yuv422(AVPicture *dst, const AVPicture *src,
930 int width, int height)
933 uint8_t *line1, *line2, *linesrc = dst->data[0];
934 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
935 uint8_t *cb1, *cb2 = src->data[1];
936 uint8_t *cr1, *cr2 = src->data[2];
938 for(h = height / 2; h--;) {
940 line2 = linesrc + dst->linesize[0];
943 lum2 = lumsrc + src->linesize[0];
948 for(w = width / 2; w--;) {
949 *line1++ = *lum1++; *line2++ = *lum2++;
950 *line1++ = *line2++ = *cb1++;
951 *line1++ = *lum1++; *line2++ = *lum2++;
952 *line1++ = *line2++ = *cr1++;
955 linesrc += dst->linesize[0] * 2;
956 lumsrc += src->linesize[0] * 2;
957 cb2 += src->linesize[1];
958 cr2 += src->linesize[2];
962 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
963 int width, int height)
966 uint8_t *line1, *line2, *linesrc = dst->data[0];
967 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
968 uint8_t *cb1, *cb2 = src->data[1];
969 uint8_t *cr1, *cr2 = src->data[2];
971 for(h = height / 2; h--;) {
973 line2 = linesrc + dst->linesize[0];
976 lum2 = lumsrc + src->linesize[0];
981 for(w = width / 2; w--;) {
982 *line1++ = *line2++ = *cb1++;
983 *line1++ = *lum1++; *line2++ = *lum2++;
984 *line1++ = *line2++ = *cr1++;
985 *line1++ = *lum1++; *line2++ = *lum2++;
988 linesrc += dst->linesize[0] * 2;
989 lumsrc += src->linesize[0] * 2;
990 cb2 += src->linesize[1];
991 cr2 += src->linesize[2];
996 #define ONE_HALF (1 << (SCALEBITS - 1))
997 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
999 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1003 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1004 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1006 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1009 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1011 y = ((y1) - 16) * FIX(255.0/219.0);\
1012 r = cm[(y + r_add) >> SCALEBITS];\
1013 g = cm[(y + g_add) >> SCALEBITS];\
1014 b = cm[(y + b_add) >> SCALEBITS];\
1017 #define YUV_TO_RGB1(cb1, cr1)\
1021 r_add = FIX(1.40200) * cr + ONE_HALF;\
1022 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1023 b_add = FIX(1.77200) * cb + ONE_HALF;\
1026 #define YUV_TO_RGB2(r, g, b, y1)\
1028 y = (y1) << SCALEBITS;\
1029 r = cm[(y + r_add) >> SCALEBITS];\
1030 g = cm[(y + g_add) >> SCALEBITS];\
1031 b = cm[(y + b_add) >> SCALEBITS];\
1034 #define Y_CCIR_TO_JPEG(y)\
1035 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1037 #define Y_JPEG_TO_CCIR(y)\
1038 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1040 #define C_CCIR_TO_JPEG(y)\
1041 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1043 /* NOTE: the clamp is really necessary! */
1044 static inline int C_JPEG_TO_CCIR(int y) {
1045 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
1052 #define RGB_TO_Y(r, g, b) \
1053 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1054 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1056 #define RGB_TO_U(r1, g1, b1, shift)\
1057 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1058 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1060 #define RGB_TO_V(r1, g1, b1, shift)\
1061 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1062 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1064 #define RGB_TO_Y_CCIR(r, g, b) \
1065 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1066 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1068 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1069 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1070 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1072 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1073 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1074 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1076 static uint8_t y_ccir_to_jpeg[256];
1077 static uint8_t y_jpeg_to_ccir[256];
1078 static uint8_t c_ccir_to_jpeg[256];
1079 static uint8_t c_jpeg_to_ccir[256];
1081 /* init various conversion tables */
1082 static void img_convert_init(void)
1085 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1087 for(i = 0;i < 256; i++) {
1088 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1089 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1090 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1091 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1095 /* apply to each pixel the given table */
1096 static void img_apply_table(uint8_t *dst, int dst_wrap,
1097 const uint8_t *src, int src_wrap,
1098 int width, int height, const uint8_t *table1)
1103 const uint8_t *table;
1106 for(;height > 0; height--) {
1130 /* XXX: use generic filter ? */
1131 /* XXX: in most cases, the sampling position is incorrect */
1134 static void shrink41(uint8_t *dst, int dst_wrap,
1135 const uint8_t *src, int src_wrap,
1136 int width, int height)
1142 for(;height > 0; height--) {
1145 for(w = width;w > 0; w--) {
1146 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1156 static void shrink21(uint8_t *dst, int dst_wrap,
1157 const uint8_t *src, int src_wrap,
1158 int width, int height)
1164 for(;height > 0; height--) {
1167 for(w = width;w > 0; w--) {
1168 d[0] = (s[0] + s[1]) >> 1;
1178 static void shrink12(uint8_t *dst, int dst_wrap,
1179 const uint8_t *src, int src_wrap,
1180 int width, int height)
1184 const uint8_t *s1, *s2;
1186 for(;height > 0; height--) {
1190 for(w = width;w >= 4; w-=4) {
1191 d[0] = (s1[0] + s2[0]) >> 1;
1192 d[1] = (s1[1] + s2[1]) >> 1;
1193 d[2] = (s1[2] + s2[2]) >> 1;
1194 d[3] = (s1[3] + s2[3]) >> 1;
1200 d[0] = (s1[0] + s2[0]) >> 1;
1205 src += 2 * src_wrap;
1211 static void shrink22(uint8_t *dst, int dst_wrap,
1212 const uint8_t *src, int src_wrap,
1213 int width, int height)
1216 const uint8_t *s1, *s2;
1219 for(;height > 0; height--) {
1223 for(w = width;w >= 4; w-=4) {
1224 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1225 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1226 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1227 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1233 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1238 src += 2 * src_wrap;
1244 static void shrink44(uint8_t *dst, int dst_wrap,
1245 const uint8_t *src, int src_wrap,
1246 int width, int height)
1249 const uint8_t *s1, *s2, *s3, *s4;
1252 for(;height > 0; height--) {
1258 for(w = width;w > 0; w--) {
1259 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1260 s2[0] + s2[1] + s2[2] + s2[3] +
1261 s3[0] + s3[1] + s3[2] + s3[3] +
1262 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1269 src += 4 * src_wrap;
1274 static void grow21_line(uint8_t *dst, const uint8_t *src,
1283 for(w = width;w >= 4; w-=4) {
1284 d[1] = d[0] = s1[0];
1285 d[3] = d[2] = s1[1];
1289 for(;w >= 2; w -= 2) {
1290 d[1] = d[0] = s1[0];
1294 /* only needed if width is not a multiple of two */
1295 /* XXX: veryfy that */
1301 static void grow41_line(uint8_t *dst, const uint8_t *src,
1310 for(w = width;w >= 4; w-=4) {
1322 static void grow21(uint8_t *dst, int dst_wrap,
1323 const uint8_t *src, int src_wrap,
1324 int width, int height)
1326 for(;height > 0; height--) {
1327 grow21_line(dst, src, width);
1334 static void grow22(uint8_t *dst, int dst_wrap,
1335 const uint8_t *src, int src_wrap,
1336 int width, int height)
1338 for(;height > 0; height--) {
1339 grow21_line(dst, src, width);
1347 static void grow41(uint8_t *dst, int dst_wrap,
1348 const uint8_t *src, int src_wrap,
1349 int width, int height)
1351 for(;height > 0; height--) {
1352 grow41_line(dst, src, width);
1359 static void grow44(uint8_t *dst, int dst_wrap,
1360 const uint8_t *src, int src_wrap,
1361 int width, int height)
1363 for(;height > 0; height--) {
1364 grow41_line(dst, src, width);
1365 if ((height & 3) == 1)
1372 static void conv411(uint8_t *dst, int dst_wrap,
1373 const uint8_t *src, int src_wrap,
1374 int width, int height)
1377 const uint8_t *s1, *s2;
1382 for(;height > 0; height--) {
1384 s2 = src + src_wrap;
1386 for(w = width;w > 0; w--) {
1387 c = (s1[0] + s2[0]) >> 1;
1394 src += src_wrap * 2;
1399 /* XXX: add jpeg quantize code */
1401 #define TRANSP_INDEX (6*6*6)
1403 /* this is maybe slow, but allows for extensions */
1404 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1406 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1409 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1412 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1415 pal = (uint32_t *)palette;
1417 for(r = 0; r < 6; r++) {
1418 for(g = 0; g < 6; g++) {
1419 for(b = 0; b < 6; b++) {
1420 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1421 (pal_value[g] << 8) | pal_value[b];
1428 pal[i++] = 0xff000000;
1431 /* copy bit n to bits 0 ... n - 1 */
1432 static inline unsigned int bitcopy_n(unsigned int a, int n)
1435 mask = (1 << n) - 1;
1436 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1439 /* rgb555 handling */
1441 #define RGB_NAME rgb555
1443 #define RGB_IN(r, g, b, s)\
1445 unsigned int v = ((const uint16_t *)(s))[0];\
1446 r = bitcopy_n(v >> (10 - 3), 3);\
1447 g = bitcopy_n(v >> (5 - 3), 3);\
1448 b = bitcopy_n(v << 3, 3);\
1451 #define RGBA_IN(r, g, b, a, s)\
1453 unsigned int v = ((const uint16_t *)(s))[0];\
1454 r = bitcopy_n(v >> (10 - 3), 3);\
1455 g = bitcopy_n(v >> (5 - 3), 3);\
1456 b = bitcopy_n(v << 3, 3);\
1457 a = (-(v >> 15)) & 0xff;\
1460 #define RGBA_OUT(d, r, g, b, a)\
1462 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1463 ((a << 8) & 0x8000);\
1468 #include "imgconvert_template.h"
1470 /* rgb565 handling */
1472 #define RGB_NAME rgb565
1474 #define RGB_IN(r, g, b, s)\
1476 unsigned int v = ((const uint16_t *)(s))[0];\
1477 r = bitcopy_n(v >> (11 - 3), 3);\
1478 g = bitcopy_n(v >> (5 - 2), 2);\
1479 b = bitcopy_n(v << 3, 3);\
1482 #define RGB_OUT(d, r, g, b)\
1484 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1489 #include "imgconvert_template.h"
1491 /* bgr24 handling */
1493 #define RGB_NAME bgr24
1495 #define RGB_IN(r, g, b, s)\
1502 #define RGB_OUT(d, r, g, b)\
1511 #include "imgconvert_template.h"
1517 /* rgb24 handling */
1519 #define RGB_NAME rgb24
1522 #define RGB_IN(r, g, b, s)\
1529 #define RGB_OUT(d, r, g, b)\
1538 #include "imgconvert_template.h"
1540 /* rgba32 handling */
1542 #define RGB_NAME rgba32
1545 #define RGB_IN(r, g, b, s)\
1547 unsigned int v = ((const uint32_t *)(s))[0];\
1548 r = (v >> 16) & 0xff;\
1549 g = (v >> 8) & 0xff;\
1553 #define RGBA_IN(r, g, b, a, s)\
1555 unsigned int v = ((const uint32_t *)(s))[0];\
1556 a = (v >> 24) & 0xff;\
1557 r = (v >> 16) & 0xff;\
1558 g = (v >> 8) & 0xff;\
1562 #define RGBA_OUT(d, r, g, b, a)\
1564 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1569 #include "imgconvert_template.h"
1571 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1572 int width, int height, int xor_mask)
1574 const unsigned char *p;
1576 int v, dst_wrap, src_wrap;
1580 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1583 dst_wrap = dst->linesize[0] - width;
1584 for(y=0;y<height;y++) {
1587 v = *p++ ^ xor_mask;
1589 q[1] = -((v >> 6) & 1);
1590 q[2] = -((v >> 5) & 1);
1591 q[3] = -((v >> 4) & 1);
1592 q[4] = -((v >> 3) & 1);
1593 q[5] = -((v >> 2) & 1);
1594 q[6] = -((v >> 1) & 1);
1595 q[7] = -((v >> 0) & 1);
1600 v = *p++ ^ xor_mask;
1602 q[0] = -((v >> 7) & 1);
1612 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1613 int width, int height)
1615 mono_to_gray(dst, src, width, height, 0xff);
1618 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1619 int width, int height)
1621 mono_to_gray(dst, src, width, height, 0x00);
1624 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1625 int width, int height, int xor_mask)
1630 int j, b, v, n1, src_wrap, dst_wrap, y;
1633 src_wrap = src->linesize[0] - width;
1636 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1638 for(y=0;y<height;y++) {
1645 v = (v << 1) | (b >> 7);
1647 d[0] = v ^ xor_mask;
1657 v = (v << 1) | (b >> 7);
1660 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1668 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1669 int width, int height)
1671 gray_to_mono(dst, src, width, height, 0xff);
1674 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1675 int width, int height)
1677 gray_to_mono(dst, src, width, height, 0x00);
1680 typedef struct ConvertEntry {
1681 void (*convert)(AVPicture *dst,
1682 const AVPicture *src, int width, int height);
1685 /* Add each new convertion function in this table. In order to be able
1686 to convert from any format to any format, the following constraints
1689 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1691 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1693 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1695 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1698 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1700 The other conversion functions are just optimisations for common cases.
1702 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1703 [PIX_FMT_YUV420P] = {
1704 [PIX_FMT_YUV422] = {
1705 .convert = yuv420p_to_yuv422,
1707 [PIX_FMT_RGB555] = {
1708 .convert = yuv420p_to_rgb555
1710 [PIX_FMT_RGB565] = {
1711 .convert = yuv420p_to_rgb565
1714 .convert = yuv420p_to_bgr24
1717 .convert = yuv420p_to_rgb24
1719 [PIX_FMT_RGBA32] = {
1720 .convert = yuv420p_to_rgba32
1722 [PIX_FMT_UYVY422] = {
1723 .convert = yuv420p_to_uyvy422,
1726 [PIX_FMT_YUV422P] = {
1727 [PIX_FMT_YUV422] = {
1728 .convert = yuv422p_to_yuv422,
1730 [PIX_FMT_UYVY422] = {
1731 .convert = yuv422p_to_uyvy422,
1734 [PIX_FMT_YUV444P] = {
1736 .convert = yuv444p_to_rgb24
1739 [PIX_FMT_YUVJ420P] = {
1740 [PIX_FMT_RGB555] = {
1741 .convert = yuvj420p_to_rgb555
1743 [PIX_FMT_RGB565] = {
1744 .convert = yuvj420p_to_rgb565
1747 .convert = yuvj420p_to_bgr24
1750 .convert = yuvj420p_to_rgb24
1752 [PIX_FMT_RGBA32] = {
1753 .convert = yuvj420p_to_rgba32
1756 [PIX_FMT_YUVJ444P] = {
1758 .convert = yuvj444p_to_rgb24
1761 [PIX_FMT_YUV422] = {
1762 [PIX_FMT_YUV420P] = {
1763 .convert = yuv422_to_yuv420p,
1765 [PIX_FMT_YUV422P] = {
1766 .convert = yuv422_to_yuv422p,
1769 [PIX_FMT_UYVY422] = {
1770 [PIX_FMT_YUV420P] = {
1771 .convert = uyvy422_to_yuv420p,
1773 [PIX_FMT_YUV422P] = {
1774 .convert = uyvy422_to_yuv422p,
1778 [PIX_FMT_YUV420P] = {
1779 .convert = rgb24_to_yuv420p
1781 [PIX_FMT_RGB565] = {
1782 .convert = rgb24_to_rgb565
1784 [PIX_FMT_RGB555] = {
1785 .convert = rgb24_to_rgb555
1787 [PIX_FMT_RGBA32] = {
1788 .convert = rgb24_to_rgba32
1791 .convert = rgb24_to_bgr24
1794 .convert = rgb24_to_gray
1797 .convert = rgb24_to_pal8
1799 [PIX_FMT_YUV444P] = {
1800 .convert = rgb24_to_yuv444p
1802 [PIX_FMT_YUVJ420P] = {
1803 .convert = rgb24_to_yuvj420p
1805 [PIX_FMT_YUVJ444P] = {
1806 .convert = rgb24_to_yuvj444p
1809 [PIX_FMT_RGBA32] = {
1811 .convert = rgba32_to_rgb24
1813 [PIX_FMT_RGB555] = {
1814 .convert = rgba32_to_rgb555
1817 .convert = rgba32_to_pal8
1819 [PIX_FMT_YUV420P] = {
1820 .convert = rgba32_to_yuv420p
1823 .convert = rgba32_to_gray
1828 .convert = bgr24_to_rgb24
1830 [PIX_FMT_YUV420P] = {
1831 .convert = bgr24_to_yuv420p
1834 .convert = bgr24_to_gray
1837 [PIX_FMT_RGB555] = {
1839 .convert = rgb555_to_rgb24
1841 [PIX_FMT_RGBA32] = {
1842 .convert = rgb555_to_rgba32
1844 [PIX_FMT_YUV420P] = {
1845 .convert = rgb555_to_yuv420p
1848 .convert = rgb555_to_gray
1851 [PIX_FMT_RGB565] = {
1853 .convert = rgb565_to_rgb24
1855 [PIX_FMT_YUV420P] = {
1856 .convert = rgb565_to_yuv420p
1859 .convert = rgb565_to_gray
1863 [PIX_FMT_RGB555] = {
1864 .convert = gray_to_rgb555
1866 [PIX_FMT_RGB565] = {
1867 .convert = gray_to_rgb565
1870 .convert = gray_to_rgb24
1873 .convert = gray_to_bgr24
1875 [PIX_FMT_RGBA32] = {
1876 .convert = gray_to_rgba32
1878 [PIX_FMT_MONOWHITE] = {
1879 .convert = gray_to_monowhite
1881 [PIX_FMT_MONOBLACK] = {
1882 .convert = gray_to_monoblack
1885 [PIX_FMT_MONOWHITE] = {
1887 .convert = monowhite_to_gray
1890 [PIX_FMT_MONOBLACK] = {
1892 .convert = monoblack_to_gray
1896 [PIX_FMT_RGB555] = {
1897 .convert = pal8_to_rgb555
1899 [PIX_FMT_RGB565] = {
1900 .convert = pal8_to_rgb565
1903 .convert = pal8_to_bgr24
1906 .convert = pal8_to_rgb24
1908 [PIX_FMT_RGBA32] = {
1909 .convert = pal8_to_rgba32
1912 [PIX_FMT_UYVY411] = {
1913 [PIX_FMT_YUV411P] = {
1914 .convert = uyvy411_to_yuv411p,
1920 int avpicture_alloc(AVPicture *picture,
1921 int pix_fmt, int width, int height)
1926 size = avpicture_get_size(pix_fmt, width, height);
1929 ptr = av_malloc(size);
1932 avpicture_fill(picture, ptr, pix_fmt, width, height);
1935 memset(picture, 0, sizeof(AVPicture));
1939 void avpicture_free(AVPicture *picture)
1941 av_free(picture->data[0]);
1944 /* return true if yuv planar */
1945 static inline int is_yuv_planar(PixFmtInfo *ps)
1947 return (ps->color_type == FF_COLOR_YUV ||
1948 ps->color_type == FF_COLOR_YUV_JPEG) &&
1949 ps->pixel_type == FF_PIXEL_PLANAR;
1952 /* XXX: always use linesize. Return -1 if not supported */
1953 int img_convert(AVPicture *dst, int dst_pix_fmt,
1954 const AVPicture *src, int src_pix_fmt,
1955 int src_width, int src_height)
1958 int i, ret, dst_width, dst_height, int_pix_fmt;
1959 PixFmtInfo *src_pix, *dst_pix;
1961 AVPicture tmp1, *tmp = &tmp1;
1963 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1964 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1966 if (src_width <= 0 || src_height <= 0)
1974 dst_width = src_width;
1975 dst_height = src_height;
1977 dst_pix = &pix_fmt_info[dst_pix_fmt];
1978 src_pix = &pix_fmt_info[src_pix_fmt];
1979 if (src_pix_fmt == dst_pix_fmt) {
1980 /* no conversion needed: just copy */
1981 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
1985 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1987 /* specific conversion routine */
1988 ce->convert(dst, src, dst_width, dst_height);
1993 if (is_yuv_planar(dst_pix) &&
1994 src_pix_fmt == PIX_FMT_GRAY8) {
1998 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
1999 img_copy_plane(dst->data[0], dst->linesize[0],
2000 src->data[0], src->linesize[0],
2001 dst_width, dst_height);
2003 img_apply_table(dst->data[0], dst->linesize[0],
2004 src->data[0], src->linesize[0],
2005 dst_width, dst_height,
2008 /* fill U and V with 128 */
2011 w >>= dst_pix->x_chroma_shift;
2012 h >>= dst_pix->y_chroma_shift;
2013 for(i = 1; i <= 2; i++) {
2015 for(y = 0; y< h; y++) {
2017 d += dst->linesize[i];
2024 if (is_yuv_planar(src_pix) &&
2025 dst_pix_fmt == PIX_FMT_GRAY8) {
2026 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2027 img_copy_plane(dst->data[0], dst->linesize[0],
2028 src->data[0], src->linesize[0],
2029 dst_width, dst_height);
2031 img_apply_table(dst->data[0], dst->linesize[0],
2032 src->data[0], src->linesize[0],
2033 dst_width, dst_height,
2039 /* YUV to YUV planar */
2040 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2041 int x_shift, y_shift, w, h, xy_shift;
2042 void (*resize_func)(uint8_t *dst, int dst_wrap,
2043 const uint8_t *src, int src_wrap,
2044 int width, int height);
2046 /* compute chroma size of the smallest dimensions */
2049 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2050 w >>= dst_pix->x_chroma_shift;
2052 w >>= src_pix->x_chroma_shift;
2053 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2054 h >>= dst_pix->y_chroma_shift;
2056 h >>= src_pix->y_chroma_shift;
2058 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2059 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2060 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2061 /* there must be filters for conversion at least from and to
2065 resize_func = img_copy_plane;
2068 resize_func = shrink21;
2071 resize_func = shrink41;
2074 resize_func = shrink12;
2077 resize_func = shrink22;
2080 resize_func = shrink44;
2083 resize_func = grow21;
2086 resize_func = grow41;
2089 resize_func = grow22;
2092 resize_func = grow44;
2095 resize_func = conv411;
2098 /* currently not handled */
2099 goto no_chroma_filter;
2102 img_copy_plane(dst->data[0], dst->linesize[0],
2103 src->data[0], src->linesize[0],
2104 dst_width, dst_height);
2106 for(i = 1;i <= 2; i++)
2107 resize_func(dst->data[i], dst->linesize[i],
2108 src->data[i], src->linesize[i],
2109 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2110 /* if yuv color space conversion is needed, we do it here on
2111 the destination image */
2112 if (dst_pix->color_type != src_pix->color_type) {
2113 const uint8_t *y_table, *c_table;
2114 if (dst_pix->color_type == FF_COLOR_YUV) {
2115 y_table = y_jpeg_to_ccir;
2116 c_table = c_jpeg_to_ccir;
2118 y_table = y_ccir_to_jpeg;
2119 c_table = c_ccir_to_jpeg;
2121 img_apply_table(dst->data[0], dst->linesize[0],
2122 dst->data[0], dst->linesize[0],
2123 dst_width, dst_height,
2126 for(i = 1;i <= 2; i++)
2127 img_apply_table(dst->data[i], dst->linesize[i],
2128 dst->data[i], dst->linesize[i],
2129 dst_width>>dst_pix->x_chroma_shift,
2130 dst_height>>dst_pix->y_chroma_shift,
2137 /* try to use an intermediate format */
2138 if (src_pix_fmt == PIX_FMT_YUV422 ||
2139 dst_pix_fmt == PIX_FMT_YUV422) {
2140 /* specific case: convert to YUV422P first */
2141 int_pix_fmt = PIX_FMT_YUV422P;
2142 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2143 dst_pix_fmt == PIX_FMT_UYVY422) {
2144 /* specific case: convert to YUV422P first */
2145 int_pix_fmt = PIX_FMT_YUV422P;
2146 } else if (src_pix_fmt == PIX_FMT_UYVY411 ||
2147 dst_pix_fmt == PIX_FMT_UYVY411) {
2148 /* specific case: convert to YUV411P first */
2149 int_pix_fmt = PIX_FMT_YUV411P;
2150 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2151 src_pix_fmt != PIX_FMT_GRAY8) ||
2152 (dst_pix->color_type == FF_COLOR_GRAY &&
2153 dst_pix_fmt != PIX_FMT_GRAY8)) {
2154 /* gray8 is the normalized format */
2155 int_pix_fmt = PIX_FMT_GRAY8;
2156 } else if ((is_yuv_planar(src_pix) &&
2157 src_pix_fmt != PIX_FMT_YUV444P &&
2158 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2159 /* yuv444 is the normalized format */
2160 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2161 int_pix_fmt = PIX_FMT_YUVJ444P;
2163 int_pix_fmt = PIX_FMT_YUV444P;
2164 } else if ((is_yuv_planar(dst_pix) &&
2165 dst_pix_fmt != PIX_FMT_YUV444P &&
2166 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2167 /* yuv444 is the normalized format */
2168 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2169 int_pix_fmt = PIX_FMT_YUVJ444P;
2171 int_pix_fmt = PIX_FMT_YUV444P;
2173 /* the two formats are rgb or gray8 or yuv[j]444p */
2174 if (src_pix->is_alpha && dst_pix->is_alpha)
2175 int_pix_fmt = PIX_FMT_RGBA32;
2177 int_pix_fmt = PIX_FMT_RGB24;
2179 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2182 if (img_convert(tmp, int_pix_fmt,
2183 src, src_pix_fmt, src_width, src_height) < 0)
2185 if (img_convert(dst, dst_pix_fmt,
2186 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2190 avpicture_free(tmp);
2194 /* NOTE: we scan all the pixels to have an exact information */
2195 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2197 const unsigned char *p;
2198 int src_wrap, ret, x, y;
2200 uint32_t *palette = (uint32_t *)src->data[1];
2203 src_wrap = src->linesize[0] - width;
2205 for(y=0;y<height;y++) {
2206 for(x=0;x<width;x++) {
2207 a = palette[p[0]] >> 24;
2209 ret |= FF_ALPHA_TRANSP;
2210 } else if (a != 0xff) {
2211 ret |= FF_ALPHA_SEMI_TRANSP;
2221 * Tell if an image really has transparent alpha values.
2222 * @return ored mask of FF_ALPHA_xxx constants
2224 int img_get_alpha_info(const AVPicture *src,
2225 int pix_fmt, int width, int height)
2227 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2230 pf = &pix_fmt_info[pix_fmt];
2231 /* no alpha can be represented in format */
2235 case PIX_FMT_RGBA32:
2236 ret = get_alpha_info_rgba32(src, width, height);
2238 case PIX_FMT_RGB555:
2239 ret = get_alpha_info_rgb555(src, width, height);
2242 ret = get_alpha_info_pal8(src, width, height);
2245 /* we do not know, so everything is indicated */
2246 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2253 #define DEINT_INPLACE_LINE_LUM \
2254 movd_m2r(lum_m4[0],mm0);\
2255 movd_m2r(lum_m3[0],mm1);\
2256 movd_m2r(lum_m2[0],mm2);\
2257 movd_m2r(lum_m1[0],mm3);\
2258 movd_m2r(lum[0],mm4);\
2259 punpcklbw_r2r(mm7,mm0);\
2260 movd_r2m(mm2,lum_m4[0]);\
2261 punpcklbw_r2r(mm7,mm1);\
2262 punpcklbw_r2r(mm7,mm2);\
2263 punpcklbw_r2r(mm7,mm3);\
2264 punpcklbw_r2r(mm7,mm4);\
2265 paddw_r2r(mm3,mm1);\
2267 paddw_r2r(mm4,mm0);\
2269 paddw_r2r(mm6,mm2);\
2270 paddw_r2r(mm2,mm1);\
2271 psubusw_r2r(mm0,mm1);\
2273 packuswb_r2r(mm7,mm1);\
2274 movd_r2m(mm1,lum_m2[0]);
2276 #define DEINT_LINE_LUM \
2277 movd_m2r(lum_m4[0],mm0);\
2278 movd_m2r(lum_m3[0],mm1);\
2279 movd_m2r(lum_m2[0],mm2);\
2280 movd_m2r(lum_m1[0],mm3);\
2281 movd_m2r(lum[0],mm4);\
2282 punpcklbw_r2r(mm7,mm0);\
2283 punpcklbw_r2r(mm7,mm1);\
2284 punpcklbw_r2r(mm7,mm2);\
2285 punpcklbw_r2r(mm7,mm3);\
2286 punpcklbw_r2r(mm7,mm4);\
2287 paddw_r2r(mm3,mm1);\
2289 paddw_r2r(mm4,mm0);\
2291 paddw_r2r(mm6,mm2);\
2292 paddw_r2r(mm2,mm1);\
2293 psubusw_r2r(mm0,mm1);\
2295 packuswb_r2r(mm7,mm1);\
2296 movd_r2m(mm1,dst[0]);
2299 /* filter parameters: [-1 4 2 4 -1] // 8 */
2300 static void deinterlace_line(uint8_t *dst,
2301 const uint8_t *lum_m4, const uint8_t *lum_m3,
2302 const uint8_t *lum_m2, const uint8_t *lum_m1,
2307 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2310 for(;size > 0;size--) {
2312 sum += lum_m3[0] << 2;
2313 sum += lum_m2[0] << 1;
2314 sum += lum_m1[0] << 2;
2316 dst[0] = cm[(sum + 4) >> 3];
2333 movq_m2r(rounder,mm6);
2335 for (;size > 3; size-=4) {
2346 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2350 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2353 for(;size > 0;size--) {
2355 sum += lum_m3[0] << 2;
2356 sum += lum_m2[0] << 1;
2357 lum_m4[0]=lum_m2[0];
2358 sum += lum_m1[0] << 2;
2360 lum_m2[0] = cm[(sum + 4) >> 3];
2376 movq_m2r(rounder,mm6);
2378 for (;size > 3; size-=4) {
2379 DEINT_INPLACE_LINE_LUM
2389 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2390 top field is copied as is, but the bottom field is deinterlaced
2391 against the top field. */
2392 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2393 const uint8_t *src1, int src_wrap,
2394 int width, int height)
2396 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2401 src_0=&src_m1[src_wrap];
2402 src_p1=&src_0[src_wrap];
2403 src_p2=&src_p1[src_wrap];
2404 for(y=0;y<(height-2);y+=2) {
2405 memcpy(dst,src_m1,width);
2407 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2411 src_p1 += 2*src_wrap;
2412 src_p2 += 2*src_wrap;
2415 memcpy(dst,src_m1,width);
2418 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2421 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2422 int width, int height)
2424 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2427 buf = (uint8_t*)av_malloc(width);
2430 memcpy(buf,src_m1,width);
2431 src_0=&src_m1[src_wrap];
2432 src_p1=&src_0[src_wrap];
2433 src_p2=&src_p1[src_wrap];
2434 for(y=0;y<(height-2);y+=2) {
2435 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2438 src_p1 += 2*src_wrap;
2439 src_p2 += 2*src_wrap;
2442 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2447 /* deinterlace - if not supported return -1 */
2448 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2449 int pix_fmt, int width, int height)
2453 if (pix_fmt != PIX_FMT_YUV420P &&
2454 pix_fmt != PIX_FMT_YUV422P &&
2455 pix_fmt != PIX_FMT_YUV444P &&
2456 pix_fmt != PIX_FMT_YUV411P)
2458 if ((width & 3) != 0 || (height & 3) != 0)
2464 case PIX_FMT_YUV420P:
2468 case PIX_FMT_YUV422P:
2471 case PIX_FMT_YUV411P:
2479 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2482 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2483 src->data[i], src->linesize[i],