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 pinfo = &pix_fmt_info[pix_fmt];
272 size = width * height;
274 case PIX_FMT_YUV420P:
275 case PIX_FMT_YUV422P:
276 case PIX_FMT_YUV444P:
277 case PIX_FMT_YUV410P:
278 case PIX_FMT_YUV411P:
279 case PIX_FMT_YUVJ420P:
280 case PIX_FMT_YUVJ422P:
281 case PIX_FMT_YUVJ444P:
282 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
283 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
285 picture->data[0] = ptr;
286 picture->data[1] = picture->data[0] + size;
287 picture->data[2] = picture->data[1] + size2;
288 picture->linesize[0] = width;
289 picture->linesize[1] = w2;
290 picture->linesize[2] = w2;
291 return size + 2 * size2;
294 picture->data[0] = ptr;
295 picture->data[1] = NULL;
296 picture->data[2] = NULL;
297 picture->linesize[0] = width * 3;
300 picture->data[0] = ptr;
301 picture->data[1] = NULL;
302 picture->data[2] = NULL;
303 picture->linesize[0] = width * 4;
308 picture->data[0] = ptr;
309 picture->data[1] = NULL;
310 picture->data[2] = NULL;
311 picture->linesize[0] = width * 2;
313 case PIX_FMT_UYVY422:
314 picture->data[0] = ptr;
315 picture->data[1] = NULL;
316 picture->data[2] = NULL;
317 picture->linesize[0] = width * 2;
319 case PIX_FMT_UYVY411:
320 picture->data[0] = ptr;
321 picture->data[1] = NULL;
322 picture->data[2] = NULL;
323 picture->linesize[0] = width + width/2;
324 return size + size/2;
326 picture->data[0] = ptr;
327 picture->data[1] = NULL;
328 picture->data[2] = NULL;
329 picture->linesize[0] = width;
331 case PIX_FMT_MONOWHITE:
332 case PIX_FMT_MONOBLACK:
333 picture->data[0] = ptr;
334 picture->data[1] = NULL;
335 picture->data[2] = NULL;
336 picture->linesize[0] = (width + 7) >> 3;
337 return picture->linesize[0] * height;
339 size2 = (size + 3) & ~3;
340 picture->data[0] = ptr;
341 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
342 picture->data[2] = NULL;
343 picture->linesize[0] = width;
344 picture->linesize[1] = 4;
345 return size2 + 256 * 4;
347 picture->data[0] = NULL;
348 picture->data[1] = NULL;
349 picture->data[2] = NULL;
350 picture->data[3] = NULL;
355 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
356 unsigned char *dest, int dest_size)
358 PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
359 int i, j, w, h, data_planes;
360 const unsigned char* s;
361 int size = avpicture_get_size(pix_fmt, width, height);
363 if (size > dest_size)
366 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
367 if (pix_fmt == PIX_FMT_YUV422 ||
368 pix_fmt == PIX_FMT_UYVY422 ||
369 pix_fmt == PIX_FMT_RGB565 ||
370 pix_fmt == PIX_FMT_RGB555)
372 else if (pix_fmt == PIX_FMT_UYVY411)
374 else if (pix_fmt == PIX_FMT_PAL8)
377 w = width * (pf->depth * pf->nb_channels / 8);
382 data_planes = pf->nb_channels;
383 w = (width*pf->depth + 7)/8;
387 for (i=0; i<data_planes; i++) {
389 w = width >> pf->x_chroma_shift;
390 h = height >> pf->y_chroma_shift;
396 s += src->linesize[i];
400 if (pf->pixel_type == FF_PIXEL_PALETTE)
401 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
406 int avpicture_get_size(int pix_fmt, int width, int height)
408 AVPicture dummy_pict;
409 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
413 * compute the loss when converting from a pixel format to another
415 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
418 const PixFmtInfo *pf, *ps;
421 ps = &pix_fmt_info[src_pix_fmt];
422 pf = &pix_fmt_info[dst_pix_fmt];
426 pf = &pix_fmt_info[dst_pix_fmt];
427 if (pf->depth < ps->depth ||
428 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
429 loss |= FF_LOSS_DEPTH;
430 if (pf->x_chroma_shift > ps->x_chroma_shift ||
431 pf->y_chroma_shift > ps->y_chroma_shift)
432 loss |= FF_LOSS_RESOLUTION;
433 switch(pf->color_type) {
435 if (ps->color_type != FF_COLOR_RGB &&
436 ps->color_type != FF_COLOR_GRAY)
437 loss |= FF_LOSS_COLORSPACE;
440 if (ps->color_type != FF_COLOR_GRAY)
441 loss |= FF_LOSS_COLORSPACE;
444 if (ps->color_type != FF_COLOR_YUV)
445 loss |= FF_LOSS_COLORSPACE;
447 case FF_COLOR_YUV_JPEG:
448 if (ps->color_type != FF_COLOR_YUV_JPEG &&
449 ps->color_type != FF_COLOR_YUV &&
450 ps->color_type != FF_COLOR_GRAY)
451 loss |= FF_LOSS_COLORSPACE;
455 if (ps->color_type != pf->color_type)
456 loss |= FF_LOSS_COLORSPACE;
459 if (pf->color_type == FF_COLOR_GRAY &&
460 ps->color_type != FF_COLOR_GRAY)
461 loss |= FF_LOSS_CHROMA;
462 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
463 loss |= FF_LOSS_ALPHA;
464 if (pf->pixel_type == FF_PIXEL_PALETTE &&
465 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
466 loss |= FF_LOSS_COLORQUANT;
470 static int avg_bits_per_pixel(int pix_fmt)
473 const PixFmtInfo *pf;
475 pf = &pix_fmt_info[pix_fmt];
476 switch(pf->pixel_type) {
477 case FF_PIXEL_PACKED:
480 case PIX_FMT_UYVY422:
485 case PIX_FMT_UYVY411:
489 bits = pf->depth * pf->nb_channels;
493 case FF_PIXEL_PLANAR:
494 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
495 bits = pf->depth * pf->nb_channels;
497 bits = pf->depth + ((2 * pf->depth) >>
498 (pf->x_chroma_shift + pf->y_chroma_shift));
501 case FF_PIXEL_PALETTE:
511 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
516 int dist, i, loss, min_dist, dst_pix_fmt;
518 /* find exact color match with smallest size */
520 min_dist = 0x7fffffff;
521 for(i = 0;i < PIX_FMT_NB; i++) {
522 if (pix_fmt_mask & (1 << i)) {
523 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
525 dist = avg_bits_per_pixel(i);
526 if (dist < min_dist) {
537 * find best pixel format to convert to. Return -1 if none found
539 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
540 int has_alpha, int *loss_ptr)
542 int dst_pix_fmt, loss_mask, i;
543 static const int loss_mask_order[] = {
544 ~0, /* no loss first */
547 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
553 /* try with successive loss */
556 loss_mask = loss_mask_order[i++];
557 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
558 has_alpha, loss_mask);
559 if (dst_pix_fmt >= 0)
567 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
571 static void img_copy_plane(uint8_t *dst, int dst_wrap,
572 const uint8_t *src, int src_wrap,
573 int width, int height)
575 for(;height > 0; height--) {
576 memcpy(dst, src, width);
583 * Copy image 'src' to 'dst'.
585 void img_copy(AVPicture *dst, const AVPicture *src,
586 int pix_fmt, int width, int height)
589 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
591 pf = &pix_fmt_info[pix_fmt];
592 switch(pf->pixel_type) {
593 case FF_PIXEL_PACKED:
596 case PIX_FMT_UYVY422:
601 case PIX_FMT_UYVY411:
605 bits = pf->depth * pf->nb_channels;
608 bwidth = (width * bits + 7) >> 3;
609 img_copy_plane(dst->data[0], dst->linesize[0],
610 src->data[0], src->linesize[0],
613 case FF_PIXEL_PLANAR:
614 for(i = 0; i < pf->nb_channels; i++) {
618 if (i == 1 || i == 2) {
619 w >>= pf->x_chroma_shift;
620 h >>= pf->y_chroma_shift;
622 bwidth = (w * pf->depth + 7) >> 3;
623 img_copy_plane(dst->data[i], dst->linesize[i],
624 src->data[i], src->linesize[i],
628 case FF_PIXEL_PALETTE:
629 img_copy_plane(dst->data[0], dst->linesize[0],
630 src->data[0], src->linesize[0],
632 /* copy the palette */
633 img_copy_plane(dst->data[1], dst->linesize[1],
634 src->data[1], src->linesize[1],
640 /* XXX: totally non optimized */
642 static void yuv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
643 int width, int height)
645 const uint8_t *p, *p1;
646 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
654 for(;height >= 1; height -= 2) {
659 for(w = width; w >= 2; w -= 2) {
676 p1 += src->linesize[0];
677 lum1 += dst->linesize[0];
681 for(w = width; w >= 2; w -= 2) {
690 p1 += src->linesize[0];
691 lum1 += dst->linesize[0];
693 cb1 += dst->linesize[1];
694 cr1 += dst->linesize[2];
698 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
699 int width, int height)
701 const uint8_t *p, *p1;
702 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
711 for(;height >= 1; height -= 2) {
716 for(w = width; w >= 2; w -= 2) {
733 p1 += src->linesize[0];
734 lum1 += dst->linesize[0];
738 for(w = width; w >= 2; w -= 2) {
747 p1 += src->linesize[0];
748 lum1 += dst->linesize[0];
750 cb1 += dst->linesize[1];
751 cr1 += dst->linesize[2];
756 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
757 int width, int height)
759 const uint8_t *p, *p1;
760 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
767 for(;height > 0; height--) {
772 for(w = width; w >= 2; w -= 2) {
782 p1 += src->linesize[0];
783 lum1 += dst->linesize[0];
784 cb1 += dst->linesize[1];
785 cr1 += dst->linesize[2];
790 static void yuv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
791 int width, int height)
793 const uint8_t *p, *p1;
794 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
801 for(;height > 0; height--) {
806 for(w = width; w >= 2; w -= 2) {
816 p1 += src->linesize[0];
817 lum1 += dst->linesize[0];
818 cb1 += dst->linesize[1];
819 cr1 += dst->linesize[2];
823 static void yuv422p_to_yuv422(AVPicture *dst, const AVPicture *src,
824 int width, int height)
827 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
834 for(;height > 0; height--) {
839 for(w = width; w >= 2; w -= 2) {
849 p1 += dst->linesize[0];
850 lum1 += src->linesize[0];
851 cb1 += src->linesize[1];
852 cr1 += src->linesize[2];
856 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
857 int width, int height)
860 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
867 for(;height > 0; height--) {
872 for(w = width; w >= 2; w -= 2) {
882 p1 += dst->linesize[0];
883 lum1 += src->linesize[0];
884 cb1 += src->linesize[1];
885 cr1 += src->linesize[2];
889 static void uyvy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
890 int width, int height)
892 const uint8_t *p, *p1;
893 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
900 for(;height > 0; height--) {
905 for(w = width; w >= 4; w -= 4) {
917 p1 += src->linesize[0];
918 lum1 += dst->linesize[0];
919 cb1 += dst->linesize[1];
920 cr1 += dst->linesize[2];
925 static void yuv420p_to_yuv422(AVPicture *dst, const AVPicture *src,
926 int width, int height)
929 uint8_t *line1, *line2, *linesrc = dst->data[0];
930 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
931 uint8_t *cb1, *cb2 = src->data[1];
932 uint8_t *cr1, *cr2 = src->data[2];
934 for(h = height / 2; h--;) {
936 line2 = linesrc + dst->linesize[0];
939 lum2 = lumsrc + src->linesize[0];
944 for(w = width / 2; w--;) {
945 *line1++ = *lum1++; *line2++ = *lum2++;
946 *line1++ = *line2++ = *cb1++;
947 *line1++ = *lum1++; *line2++ = *lum2++;
948 *line1++ = *line2++ = *cr1++;
951 linesrc += dst->linesize[0] * 2;
952 lumsrc += src->linesize[0] * 2;
953 cb2 += src->linesize[1];
954 cr2 += src->linesize[2];
959 #define ONE_HALF (1 << (SCALEBITS - 1))
960 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
962 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
966 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
967 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
969 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
972 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
974 y = ((y1) - 16) * FIX(255.0/219.0);\
975 r = cm[(y + r_add) >> SCALEBITS];\
976 g = cm[(y + g_add) >> SCALEBITS];\
977 b = cm[(y + b_add) >> SCALEBITS];\
980 #define YUV_TO_RGB1(cb1, cr1)\
984 r_add = FIX(1.40200) * cr + ONE_HALF;\
985 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
986 b_add = FIX(1.77200) * cb + ONE_HALF;\
989 #define YUV_TO_RGB2(r, g, b, y1)\
991 y = (y1) << SCALEBITS;\
992 r = cm[(y + r_add) >> SCALEBITS];\
993 g = cm[(y + g_add) >> SCALEBITS];\
994 b = cm[(y + b_add) >> SCALEBITS];\
997 #define Y_CCIR_TO_JPEG(y)\
998 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1000 #define Y_JPEG_TO_CCIR(y)\
1001 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1003 #define C_CCIR_TO_JPEG(y)\
1004 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1006 /* NOTE: the clamp is really necessary! */
1007 static inline int C_JPEG_TO_CCIR(int y) {
1008 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
1015 #define RGB_TO_Y(r, g, b) \
1016 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1017 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1019 #define RGB_TO_U(r1, g1, b1, shift)\
1020 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1021 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1023 #define RGB_TO_V(r1, g1, b1, shift)\
1024 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1025 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1027 #define RGB_TO_Y_CCIR(r, g, b) \
1028 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1029 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1031 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1032 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1033 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1035 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1036 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1037 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1039 static uint8_t y_ccir_to_jpeg[256];
1040 static uint8_t y_jpeg_to_ccir[256];
1041 static uint8_t c_ccir_to_jpeg[256];
1042 static uint8_t c_jpeg_to_ccir[256];
1044 /* init various conversion tables */
1045 static void img_convert_init(void)
1048 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1050 for(i = 0;i < 256; i++) {
1051 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1052 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1053 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1054 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1058 /* apply to each pixel the given table */
1059 static void img_apply_table(uint8_t *dst, int dst_wrap,
1060 const uint8_t *src, int src_wrap,
1061 int width, int height, const uint8_t *table1)
1066 const uint8_t *table;
1069 for(;height > 0; height--) {
1093 /* XXX: use generic filter ? */
1094 /* XXX: in most cases, the sampling position is incorrect */
1097 static void shrink41(uint8_t *dst, int dst_wrap,
1098 const uint8_t *src, int src_wrap,
1099 int width, int height)
1105 for(;height > 0; height--) {
1108 for(w = width;w > 0; w--) {
1109 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1119 static void shrink21(uint8_t *dst, int dst_wrap,
1120 const uint8_t *src, int src_wrap,
1121 int width, int height)
1127 for(;height > 0; height--) {
1130 for(w = width;w > 0; w--) {
1131 d[0] = (s[0] + s[1]) >> 1;
1141 static void shrink12(uint8_t *dst, int dst_wrap,
1142 const uint8_t *src, int src_wrap,
1143 int width, int height)
1147 const uint8_t *s1, *s2;
1149 for(;height > 0; height--) {
1153 for(w = width;w >= 4; w-=4) {
1154 d[0] = (s1[0] + s2[0]) >> 1;
1155 d[1] = (s1[1] + s2[1]) >> 1;
1156 d[2] = (s1[2] + s2[2]) >> 1;
1157 d[3] = (s1[3] + s2[3]) >> 1;
1163 d[0] = (s1[0] + s2[0]) >> 1;
1168 src += 2 * src_wrap;
1174 static void shrink22(uint8_t *dst, int dst_wrap,
1175 const uint8_t *src, int src_wrap,
1176 int width, int height)
1179 const uint8_t *s1, *s2;
1182 for(;height > 0; height--) {
1186 for(w = width;w >= 4; w-=4) {
1187 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1188 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1189 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1190 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1196 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1201 src += 2 * src_wrap;
1207 static void shrink44(uint8_t *dst, int dst_wrap,
1208 const uint8_t *src, int src_wrap,
1209 int width, int height)
1212 const uint8_t *s1, *s2, *s3, *s4;
1215 for(;height > 0; height--) {
1221 for(w = width;w > 0; w--) {
1222 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1223 s2[0] + s2[1] + s2[2] + s2[3] +
1224 s3[0] + s3[1] + s3[2] + s3[3] +
1225 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1232 src += 4 * src_wrap;
1237 static void grow21_line(uint8_t *dst, const uint8_t *src,
1246 for(w = width;w >= 4; w-=4) {
1247 d[1] = d[0] = s1[0];
1248 d[3] = d[2] = s1[1];
1252 for(;w >= 2; w -= 2) {
1253 d[1] = d[0] = s1[0];
1257 /* only needed if width is not a multiple of two */
1258 /* XXX: veryfy that */
1264 static void grow41_line(uint8_t *dst, const uint8_t *src,
1273 for(w = width;w >= 4; w-=4) {
1285 static void grow21(uint8_t *dst, int dst_wrap,
1286 const uint8_t *src, int src_wrap,
1287 int width, int height)
1289 for(;height > 0; height--) {
1290 grow21_line(dst, src, width);
1297 static void grow22(uint8_t *dst, int dst_wrap,
1298 const uint8_t *src, int src_wrap,
1299 int width, int height)
1301 for(;height > 0; height--) {
1302 grow21_line(dst, src, width);
1310 static void grow41(uint8_t *dst, int dst_wrap,
1311 const uint8_t *src, int src_wrap,
1312 int width, int height)
1314 for(;height > 0; height--) {
1315 grow41_line(dst, src, width);
1322 static void grow44(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 grow41_line(dst, src, width);
1328 if ((height & 3) == 1)
1335 static void conv411(uint8_t *dst, int dst_wrap,
1336 const uint8_t *src, int src_wrap,
1337 int width, int height)
1340 const uint8_t *s1, *s2;
1345 for(;height > 0; height--) {
1347 s2 = src + src_wrap;
1349 for(w = width;w > 0; w--) {
1350 c = (s1[0] + s2[0]) >> 1;
1357 src += src_wrap * 2;
1362 /* XXX: add jpeg quantize code */
1364 #define TRANSP_INDEX (6*6*6)
1366 /* this is maybe slow, but allows for extensions */
1367 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1369 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1372 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1375 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1378 pal = (uint32_t *)palette;
1380 for(r = 0; r < 6; r++) {
1381 for(g = 0; g < 6; g++) {
1382 for(b = 0; b < 6; b++) {
1383 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1384 (pal_value[g] << 8) | pal_value[b];
1391 pal[i++] = 0xff000000;
1394 /* copy bit n to bits 0 ... n - 1 */
1395 static inline unsigned int bitcopy_n(unsigned int a, int n)
1398 mask = (1 << n) - 1;
1399 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1402 /* rgb555 handling */
1404 #define RGB_NAME rgb555
1406 #define RGB_IN(r, g, b, s)\
1408 unsigned int v = ((const uint16_t *)(s))[0];\
1409 r = bitcopy_n(v >> (10 - 3), 3);\
1410 g = bitcopy_n(v >> (5 - 3), 3);\
1411 b = bitcopy_n(v << 3, 3);\
1414 #define RGBA_IN(r, g, b, a, s)\
1416 unsigned int v = ((const uint16_t *)(s))[0];\
1417 r = bitcopy_n(v >> (10 - 3), 3);\
1418 g = bitcopy_n(v >> (5 - 3), 3);\
1419 b = bitcopy_n(v << 3, 3);\
1420 a = (-(v >> 15)) & 0xff;\
1423 #define RGBA_OUT(d, r, g, b, a)\
1425 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1426 ((a << 8) & 0x8000);\
1431 #include "imgconvert_template.h"
1433 /* rgb565 handling */
1435 #define RGB_NAME rgb565
1437 #define RGB_IN(r, g, b, s)\
1439 unsigned int v = ((const uint16_t *)(s))[0];\
1440 r = bitcopy_n(v >> (11 - 3), 3);\
1441 g = bitcopy_n(v >> (5 - 2), 2);\
1442 b = bitcopy_n(v << 3, 3);\
1445 #define RGB_OUT(d, r, g, b)\
1447 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1452 #include "imgconvert_template.h"
1454 /* bgr24 handling */
1456 #define RGB_NAME bgr24
1458 #define RGB_IN(r, g, b, s)\
1465 #define RGB_OUT(d, r, g, b)\
1474 #include "imgconvert_template.h"
1480 /* rgb24 handling */
1482 #define RGB_NAME rgb24
1485 #define RGB_IN(r, g, b, s)\
1492 #define RGB_OUT(d, r, g, b)\
1501 #include "imgconvert_template.h"
1503 /* rgba32 handling */
1505 #define RGB_NAME rgba32
1508 #define RGB_IN(r, g, b, s)\
1510 unsigned int v = ((const uint32_t *)(s))[0];\
1511 r = (v >> 16) & 0xff;\
1512 g = (v >> 8) & 0xff;\
1516 #define RGBA_IN(r, g, b, a, s)\
1518 unsigned int v = ((const uint32_t *)(s))[0];\
1519 a = (v >> 24) & 0xff;\
1520 r = (v >> 16) & 0xff;\
1521 g = (v >> 8) & 0xff;\
1525 #define RGBA_OUT(d, r, g, b, a)\
1527 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1532 #include "imgconvert_template.h"
1534 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1535 int width, int height, int xor_mask)
1537 const unsigned char *p;
1539 int v, dst_wrap, src_wrap;
1543 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1546 dst_wrap = dst->linesize[0] - width;
1547 for(y=0;y<height;y++) {
1550 v = *p++ ^ xor_mask;
1552 q[1] = -((v >> 6) & 1);
1553 q[2] = -((v >> 5) & 1);
1554 q[3] = -((v >> 4) & 1);
1555 q[4] = -((v >> 3) & 1);
1556 q[5] = -((v >> 2) & 1);
1557 q[6] = -((v >> 1) & 1);
1558 q[7] = -((v >> 0) & 1);
1563 v = *p++ ^ xor_mask;
1565 q[0] = -((v >> 7) & 1);
1575 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1576 int width, int height)
1578 mono_to_gray(dst, src, width, height, 0xff);
1581 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1582 int width, int height)
1584 mono_to_gray(dst, src, width, height, 0x00);
1587 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1588 int width, int height, int xor_mask)
1593 int j, b, v, n1, src_wrap, dst_wrap, y;
1596 src_wrap = src->linesize[0] - width;
1599 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1601 for(y=0;y<height;y++) {
1608 v = (v << 1) | (b >> 7);
1610 d[0] = v ^ xor_mask;
1620 v = (v << 1) | (b >> 7);
1623 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1631 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1632 int width, int height)
1634 gray_to_mono(dst, src, width, height, 0xff);
1637 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1638 int width, int height)
1640 gray_to_mono(dst, src, width, height, 0x00);
1643 typedef struct ConvertEntry {
1644 void (*convert)(AVPicture *dst,
1645 const AVPicture *src, int width, int height);
1648 /* Add each new convertion function in this table. In order to be able
1649 to convert from any format to any format, the following constraints
1652 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1654 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1656 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1658 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1661 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1663 The other conversion functions are just optimisations for common cases.
1665 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1666 [PIX_FMT_YUV420P] = {
1667 [PIX_FMT_YUV422] = {
1668 .convert = yuv420p_to_yuv422,
1670 [PIX_FMT_RGB555] = {
1671 .convert = yuv420p_to_rgb555
1673 [PIX_FMT_RGB565] = {
1674 .convert = yuv420p_to_rgb565
1677 .convert = yuv420p_to_bgr24
1680 .convert = yuv420p_to_rgb24
1682 [PIX_FMT_RGBA32] = {
1683 .convert = yuv420p_to_rgba32
1686 [PIX_FMT_YUV422P] = {
1687 [PIX_FMT_YUV422] = {
1688 .convert = yuv422p_to_yuv422,
1690 [PIX_FMT_UYVY422] = {
1691 .convert = yuv422p_to_uyvy422,
1694 [PIX_FMT_YUV444P] = {
1696 .convert = yuv444p_to_rgb24
1699 [PIX_FMT_YUVJ420P] = {
1700 [PIX_FMT_RGB555] = {
1701 .convert = yuvj420p_to_rgb555
1703 [PIX_FMT_RGB565] = {
1704 .convert = yuvj420p_to_rgb565
1707 .convert = yuvj420p_to_bgr24
1710 .convert = yuvj420p_to_rgb24
1712 [PIX_FMT_RGBA32] = {
1713 .convert = yuvj420p_to_rgba32
1716 [PIX_FMT_YUVJ444P] = {
1718 .convert = yuvj444p_to_rgb24
1721 [PIX_FMT_YUV422] = {
1722 [PIX_FMT_YUV420P] = {
1723 .convert = yuv422_to_yuv420p,
1725 [PIX_FMT_YUV422P] = {
1726 .convert = yuv422_to_yuv422p,
1729 [PIX_FMT_UYVY422] = {
1730 [PIX_FMT_YUV420P] = {
1731 .convert = uyvy422_to_yuv420p,
1733 [PIX_FMT_YUV422P] = {
1734 .convert = uyvy422_to_yuv422p,
1738 [PIX_FMT_YUV420P] = {
1739 .convert = rgb24_to_yuv420p
1741 [PIX_FMT_RGB565] = {
1742 .convert = rgb24_to_rgb565
1744 [PIX_FMT_RGB555] = {
1745 .convert = rgb24_to_rgb555
1747 [PIX_FMT_RGBA32] = {
1748 .convert = rgb24_to_rgba32
1751 .convert = rgb24_to_bgr24
1754 .convert = rgb24_to_gray
1757 .convert = rgb24_to_pal8
1759 [PIX_FMT_YUV444P] = {
1760 .convert = rgb24_to_yuv444p
1762 [PIX_FMT_YUVJ420P] = {
1763 .convert = rgb24_to_yuvj420p
1765 [PIX_FMT_YUVJ444P] = {
1766 .convert = rgb24_to_yuvj444p
1769 [PIX_FMT_RGBA32] = {
1771 .convert = rgba32_to_rgb24
1773 [PIX_FMT_RGB555] = {
1774 .convert = rgba32_to_rgb555
1777 .convert = rgba32_to_pal8
1779 [PIX_FMT_YUV420P] = {
1780 .convert = rgba32_to_yuv420p
1783 .convert = rgba32_to_gray
1788 .convert = bgr24_to_rgb24
1790 [PIX_FMT_YUV420P] = {
1791 .convert = bgr24_to_yuv420p
1794 .convert = bgr24_to_gray
1797 [PIX_FMT_RGB555] = {
1799 .convert = rgb555_to_rgb24
1801 [PIX_FMT_RGBA32] = {
1802 .convert = rgb555_to_rgba32
1804 [PIX_FMT_YUV420P] = {
1805 .convert = rgb555_to_yuv420p
1808 .convert = rgb555_to_gray
1811 [PIX_FMT_RGB565] = {
1813 .convert = rgb565_to_rgb24
1815 [PIX_FMT_YUV420P] = {
1816 .convert = rgb565_to_yuv420p
1819 .convert = rgb565_to_gray
1823 [PIX_FMT_RGB555] = {
1824 .convert = gray_to_rgb555
1826 [PIX_FMT_RGB565] = {
1827 .convert = gray_to_rgb565
1830 .convert = gray_to_rgb24
1833 .convert = gray_to_bgr24
1835 [PIX_FMT_RGBA32] = {
1836 .convert = gray_to_rgba32
1838 [PIX_FMT_MONOWHITE] = {
1839 .convert = gray_to_monowhite
1841 [PIX_FMT_MONOBLACK] = {
1842 .convert = gray_to_monoblack
1845 [PIX_FMT_MONOWHITE] = {
1847 .convert = monowhite_to_gray
1850 [PIX_FMT_MONOBLACK] = {
1852 .convert = monoblack_to_gray
1856 [PIX_FMT_RGB555] = {
1857 .convert = pal8_to_rgb555
1859 [PIX_FMT_RGB565] = {
1860 .convert = pal8_to_rgb565
1863 .convert = pal8_to_bgr24
1866 .convert = pal8_to_rgb24
1868 [PIX_FMT_RGBA32] = {
1869 .convert = pal8_to_rgba32
1872 [PIX_FMT_UYVY411] = {
1873 [PIX_FMT_YUV411P] = {
1874 .convert = uyvy411_to_yuv411p,
1880 int avpicture_alloc(AVPicture *picture,
1881 int pix_fmt, int width, int height)
1886 size = avpicture_get_size(pix_fmt, width, height);
1887 ptr = av_malloc(size);
1890 avpicture_fill(picture, ptr, pix_fmt, width, height);
1893 memset(picture, 0, sizeof(AVPicture));
1897 void avpicture_free(AVPicture *picture)
1899 av_free(picture->data[0]);
1902 /* return true if yuv planar */
1903 static inline int is_yuv_planar(PixFmtInfo *ps)
1905 return (ps->color_type == FF_COLOR_YUV ||
1906 ps->color_type == FF_COLOR_YUV_JPEG) &&
1907 ps->pixel_type == FF_PIXEL_PLANAR;
1910 /* XXX: always use linesize. Return -1 if not supported */
1911 int img_convert(AVPicture *dst, int dst_pix_fmt,
1912 const AVPicture *src, int src_pix_fmt,
1913 int src_width, int src_height)
1916 int i, ret, dst_width, dst_height, int_pix_fmt;
1917 PixFmtInfo *src_pix, *dst_pix;
1919 AVPicture tmp1, *tmp = &tmp1;
1921 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1922 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1924 if (src_width <= 0 || src_height <= 0)
1932 dst_width = src_width;
1933 dst_height = src_height;
1935 dst_pix = &pix_fmt_info[dst_pix_fmt];
1936 src_pix = &pix_fmt_info[src_pix_fmt];
1937 if (src_pix_fmt == dst_pix_fmt) {
1938 /* no conversion needed: just copy */
1939 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
1943 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1945 /* specific conversion routine */
1946 ce->convert(dst, src, dst_width, dst_height);
1951 if (is_yuv_planar(dst_pix) &&
1952 src_pix_fmt == PIX_FMT_GRAY8) {
1956 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
1957 img_copy_plane(dst->data[0], dst->linesize[0],
1958 src->data[0], src->linesize[0],
1959 dst_width, dst_height);
1961 img_apply_table(dst->data[0], dst->linesize[0],
1962 src->data[0], src->linesize[0],
1963 dst_width, dst_height,
1966 /* fill U and V with 128 */
1969 w >>= dst_pix->x_chroma_shift;
1970 h >>= dst_pix->y_chroma_shift;
1971 for(i = 1; i <= 2; i++) {
1973 for(y = 0; y< h; y++) {
1975 d += dst->linesize[i];
1982 if (is_yuv_planar(src_pix) &&
1983 dst_pix_fmt == PIX_FMT_GRAY8) {
1984 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
1985 img_copy_plane(dst->data[0], dst->linesize[0],
1986 src->data[0], src->linesize[0],
1987 dst_width, dst_height);
1989 img_apply_table(dst->data[0], dst->linesize[0],
1990 src->data[0], src->linesize[0],
1991 dst_width, dst_height,
1997 /* YUV to YUV planar */
1998 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
1999 int x_shift, y_shift, w, h, xy_shift;
2000 void (*resize_func)(uint8_t *dst, int dst_wrap,
2001 const uint8_t *src, int src_wrap,
2002 int width, int height);
2004 /* compute chroma size of the smallest dimensions */
2007 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2008 w >>= dst_pix->x_chroma_shift;
2010 w >>= src_pix->x_chroma_shift;
2011 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2012 h >>= dst_pix->y_chroma_shift;
2014 h >>= src_pix->y_chroma_shift;
2016 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2017 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2018 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2019 /* there must be filters for conversion at least from and to
2023 resize_func = img_copy_plane;
2026 resize_func = shrink21;
2029 resize_func = shrink41;
2032 resize_func = shrink12;
2035 resize_func = shrink22;
2038 resize_func = shrink44;
2041 resize_func = grow21;
2044 resize_func = grow41;
2047 resize_func = grow22;
2050 resize_func = grow44;
2053 resize_func = conv411;
2056 /* currently not handled */
2057 goto no_chroma_filter;
2060 img_copy_plane(dst->data[0], dst->linesize[0],
2061 src->data[0], src->linesize[0],
2062 dst_width, dst_height);
2064 for(i = 1;i <= 2; i++)
2065 resize_func(dst->data[i], dst->linesize[i],
2066 src->data[i], src->linesize[i],
2067 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2068 /* if yuv color space conversion is needed, we do it here on
2069 the destination image */
2070 if (dst_pix->color_type != src_pix->color_type) {
2071 const uint8_t *y_table, *c_table;
2072 if (dst_pix->color_type == FF_COLOR_YUV) {
2073 y_table = y_jpeg_to_ccir;
2074 c_table = c_jpeg_to_ccir;
2076 y_table = y_ccir_to_jpeg;
2077 c_table = c_ccir_to_jpeg;
2079 img_apply_table(dst->data[0], dst->linesize[0],
2080 dst->data[0], dst->linesize[0],
2081 dst_width, dst_height,
2084 for(i = 1;i <= 2; i++)
2085 img_apply_table(dst->data[i], dst->linesize[i],
2086 dst->data[i], dst->linesize[i],
2087 dst_width>>dst_pix->x_chroma_shift,
2088 dst_height>>dst_pix->y_chroma_shift,
2095 /* try to use an intermediate format */
2096 if (src_pix_fmt == PIX_FMT_YUV422 ||
2097 dst_pix_fmt == PIX_FMT_YUV422) {
2098 /* specific case: convert to YUV422P first */
2099 int_pix_fmt = PIX_FMT_YUV422P;
2100 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2101 dst_pix_fmt == PIX_FMT_UYVY422) {
2102 /* specific case: convert to YUV422P first */
2103 int_pix_fmt = PIX_FMT_YUV422P;
2104 } else if (src_pix_fmt == PIX_FMT_UYVY411 ||
2105 dst_pix_fmt == PIX_FMT_UYVY411) {
2106 /* specific case: convert to YUV411P first */
2107 int_pix_fmt = PIX_FMT_YUV411P;
2108 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2109 src_pix_fmt != PIX_FMT_GRAY8) ||
2110 (dst_pix->color_type == FF_COLOR_GRAY &&
2111 dst_pix_fmt != PIX_FMT_GRAY8)) {
2112 /* gray8 is the normalized format */
2113 int_pix_fmt = PIX_FMT_GRAY8;
2114 } else if ((is_yuv_planar(src_pix) &&
2115 src_pix_fmt != PIX_FMT_YUV444P &&
2116 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2117 /* yuv444 is the normalized format */
2118 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2119 int_pix_fmt = PIX_FMT_YUVJ444P;
2121 int_pix_fmt = PIX_FMT_YUV444P;
2122 } else if ((is_yuv_planar(dst_pix) &&
2123 dst_pix_fmt != PIX_FMT_YUV444P &&
2124 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2125 /* yuv444 is the normalized format */
2126 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2127 int_pix_fmt = PIX_FMT_YUVJ444P;
2129 int_pix_fmt = PIX_FMT_YUV444P;
2131 /* the two formats are rgb or gray8 or yuv[j]444p */
2132 if (src_pix->is_alpha && dst_pix->is_alpha)
2133 int_pix_fmt = PIX_FMT_RGBA32;
2135 int_pix_fmt = PIX_FMT_RGB24;
2137 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2140 if (img_convert(tmp, int_pix_fmt,
2141 src, src_pix_fmt, src_width, src_height) < 0)
2143 if (img_convert(dst, dst_pix_fmt,
2144 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2148 avpicture_free(tmp);
2152 /* NOTE: we scan all the pixels to have an exact information */
2153 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2155 const unsigned char *p;
2156 int src_wrap, ret, x, y;
2158 uint32_t *palette = (uint32_t *)src->data[1];
2161 src_wrap = src->linesize[0] - width;
2163 for(y=0;y<height;y++) {
2164 for(x=0;x<width;x++) {
2165 a = palette[p[0]] >> 24;
2167 ret |= FF_ALPHA_TRANSP;
2168 } else if (a != 0xff) {
2169 ret |= FF_ALPHA_SEMI_TRANSP;
2179 * Tell if an image really has transparent alpha values.
2180 * @return ored mask of FF_ALPHA_xxx constants
2182 int img_get_alpha_info(const AVPicture *src,
2183 int pix_fmt, int width, int height)
2185 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2188 pf = &pix_fmt_info[pix_fmt];
2189 /* no alpha can be represented in format */
2193 case PIX_FMT_RGBA32:
2194 ret = get_alpha_info_rgba32(src, width, height);
2196 case PIX_FMT_RGB555:
2197 ret = get_alpha_info_rgb555(src, width, height);
2200 ret = get_alpha_info_pal8(src, width, height);
2203 /* we do not know, so everything is indicated */
2204 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2211 #define DEINT_INPLACE_LINE_LUM \
2212 movd_m2r(lum_m4[0],mm0);\
2213 movd_m2r(lum_m3[0],mm1);\
2214 movd_m2r(lum_m2[0],mm2);\
2215 movd_m2r(lum_m1[0],mm3);\
2216 movd_m2r(lum[0],mm4);\
2217 punpcklbw_r2r(mm7,mm0);\
2218 movd_r2m(mm2,lum_m4[0]);\
2219 punpcklbw_r2r(mm7,mm1);\
2220 punpcklbw_r2r(mm7,mm2);\
2221 punpcklbw_r2r(mm7,mm3);\
2222 punpcklbw_r2r(mm7,mm4);\
2223 paddw_r2r(mm3,mm1);\
2225 paddw_r2r(mm4,mm0);\
2227 paddw_r2r(mm6,mm2);\
2228 paddw_r2r(mm2,mm1);\
2229 psubusw_r2r(mm0,mm1);\
2231 packuswb_r2r(mm7,mm1);\
2232 movd_r2m(mm1,lum_m2[0]);
2234 #define DEINT_LINE_LUM \
2235 movd_m2r(lum_m4[0],mm0);\
2236 movd_m2r(lum_m3[0],mm1);\
2237 movd_m2r(lum_m2[0],mm2);\
2238 movd_m2r(lum_m1[0],mm3);\
2239 movd_m2r(lum[0],mm4);\
2240 punpcklbw_r2r(mm7,mm0);\
2241 punpcklbw_r2r(mm7,mm1);\
2242 punpcklbw_r2r(mm7,mm2);\
2243 punpcklbw_r2r(mm7,mm3);\
2244 punpcklbw_r2r(mm7,mm4);\
2245 paddw_r2r(mm3,mm1);\
2247 paddw_r2r(mm4,mm0);\
2249 paddw_r2r(mm6,mm2);\
2250 paddw_r2r(mm2,mm1);\
2251 psubusw_r2r(mm0,mm1);\
2253 packuswb_r2r(mm7,mm1);\
2254 movd_r2m(mm1,dst[0]);
2257 /* filter parameters: [-1 4 2 4 -1] // 8 */
2258 static void deinterlace_line(uint8_t *dst,
2259 const uint8_t *lum_m4, const uint8_t *lum_m3,
2260 const uint8_t *lum_m2, const uint8_t *lum_m1,
2265 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2268 for(;size > 0;size--) {
2270 sum += lum_m3[0] << 2;
2271 sum += lum_m2[0] << 1;
2272 sum += lum_m1[0] << 2;
2274 dst[0] = cm[(sum + 4) >> 3];
2291 movq_m2r(rounder,mm6);
2293 for (;size > 3; size-=4) {
2304 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2308 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2311 for(;size > 0;size--) {
2313 sum += lum_m3[0] << 2;
2314 sum += lum_m2[0] << 1;
2315 lum_m4[0]=lum_m2[0];
2316 sum += lum_m1[0] << 2;
2318 lum_m2[0] = cm[(sum + 4) >> 3];
2334 movq_m2r(rounder,mm6);
2336 for (;size > 3; size-=4) {
2337 DEINT_INPLACE_LINE_LUM
2347 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2348 top field is copied as is, but the bottom field is deinterlaced
2349 against the top field. */
2350 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2351 const uint8_t *src1, int src_wrap,
2352 int width, int height)
2354 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2359 src_0=&src_m1[src_wrap];
2360 src_p1=&src_0[src_wrap];
2361 src_p2=&src_p1[src_wrap];
2362 for(y=0;y<(height-2);y+=2) {
2363 memcpy(dst,src_m1,width);
2365 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2369 src_p1 += 2*src_wrap;
2370 src_p2 += 2*src_wrap;
2373 memcpy(dst,src_m1,width);
2376 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2379 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2380 int width, int height)
2382 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2385 buf = (uint8_t*)av_malloc(width);
2388 memcpy(buf,src_m1,width);
2389 src_0=&src_m1[src_wrap];
2390 src_p1=&src_0[src_wrap];
2391 src_p2=&src_p1[src_wrap];
2392 for(y=0;y<(height-2);y+=2) {
2393 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2396 src_p1 += 2*src_wrap;
2397 src_p2 += 2*src_wrap;
2400 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2405 /* deinterlace - if not supported return -1 */
2406 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2407 int pix_fmt, int width, int height)
2411 if (pix_fmt != PIX_FMT_YUV420P &&
2412 pix_fmt != PIX_FMT_YUV422P &&
2413 pix_fmt != PIX_FMT_YUV444P &&
2414 pix_fmt != PIX_FMT_YUV411P)
2416 if ((width & 3) != 0 || (height & 3) != 0)
2422 case PIX_FMT_YUV420P:
2426 case PIX_FMT_YUV422P:
2429 case PIX_FMT_YUV411P:
2437 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2440 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2441 src->data[i], src->linesize[i],