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,
226 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
228 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
229 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
232 const char *avcodec_get_pix_fmt_name(int pix_fmt)
234 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
237 return pix_fmt_info[pix_fmt].name;
240 enum PixelFormat avcodec_get_pix_fmt(const char* name)
244 for (i=0; i < PIX_FMT_NB; i++)
245 if (!strcmp(pix_fmt_info[i].name, name))
250 /* Picture field are filled with 'ptr' addresses. Also return size */
251 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
252 int pix_fmt, int width, int height)
254 int size, w2, h2, size2;
257 pinfo = &pix_fmt_info[pix_fmt];
258 size = width * height;
260 case PIX_FMT_YUV420P:
261 case PIX_FMT_YUV422P:
262 case PIX_FMT_YUV444P:
263 case PIX_FMT_YUV410P:
264 case PIX_FMT_YUV411P:
265 case PIX_FMT_YUVJ420P:
266 case PIX_FMT_YUVJ422P:
267 case PIX_FMT_YUVJ444P:
268 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
269 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
271 picture->data[0] = ptr;
272 picture->data[1] = picture->data[0] + size;
273 picture->data[2] = picture->data[1] + size2;
274 picture->linesize[0] = width;
275 picture->linesize[1] = w2;
276 picture->linesize[2] = w2;
277 return size + 2 * size2;
280 picture->data[0] = ptr;
281 picture->data[1] = NULL;
282 picture->data[2] = NULL;
283 picture->linesize[0] = width * 3;
286 picture->data[0] = ptr;
287 picture->data[1] = NULL;
288 picture->data[2] = NULL;
289 picture->linesize[0] = width * 4;
294 picture->data[0] = ptr;
295 picture->data[1] = NULL;
296 picture->data[2] = NULL;
297 picture->linesize[0] = width * 2;
299 case PIX_FMT_UYVY422:
300 picture->data[0] = ptr;
301 picture->data[1] = NULL;
302 picture->data[2] = NULL;
303 picture->linesize[0] = width * 2;
306 picture->data[0] = ptr;
307 picture->data[1] = NULL;
308 picture->data[2] = NULL;
309 picture->linesize[0] = width;
311 case PIX_FMT_MONOWHITE:
312 case PIX_FMT_MONOBLACK:
313 picture->data[0] = ptr;
314 picture->data[1] = NULL;
315 picture->data[2] = NULL;
316 picture->linesize[0] = (width + 7) >> 3;
317 return picture->linesize[0] * height;
319 size2 = (size + 3) & ~3;
320 picture->data[0] = ptr;
321 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
322 picture->data[2] = NULL;
323 picture->linesize[0] = width;
324 picture->linesize[1] = 4;
325 return size2 + 256 * 4;
327 picture->data[0] = NULL;
328 picture->data[1] = NULL;
329 picture->data[2] = NULL;
330 picture->data[3] = NULL;
335 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
336 unsigned char *dest, int dest_size)
338 PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
339 int i, j, w, h, data_planes;
340 const unsigned char* s;
341 int size = avpicture_get_size(pix_fmt, width, height);
343 if (size > dest_size)
346 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
347 if (pix_fmt == PIX_FMT_YUV422 ||
348 pix_fmt == PIX_FMT_UYVY422 ||
349 pix_fmt == PIX_FMT_RGB565 ||
350 pix_fmt == PIX_FMT_RGB555)
352 else if (pix_fmt == PIX_FMT_PAL8)
355 w = width * (pf->depth * pf->nb_channels / 8);
360 data_planes = pf->nb_channels;
365 for (i=0; i<data_planes; i++) {
367 w = width >> pf->x_chroma_shift;
368 h = height >> pf->y_chroma_shift;
374 s += src->linesize[i];
378 if (pf->pixel_type == FF_PIXEL_PALETTE)
379 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
384 int avpicture_get_size(int pix_fmt, int width, int height)
386 AVPicture dummy_pict;
387 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
391 * compute the loss when converting from a pixel format to another
393 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
396 const PixFmtInfo *pf, *ps;
399 ps = &pix_fmt_info[src_pix_fmt];
400 pf = &pix_fmt_info[dst_pix_fmt];
404 pf = &pix_fmt_info[dst_pix_fmt];
405 if (pf->depth < ps->depth ||
406 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
407 loss |= FF_LOSS_DEPTH;
408 if (pf->x_chroma_shift > ps->x_chroma_shift ||
409 pf->y_chroma_shift > ps->y_chroma_shift)
410 loss |= FF_LOSS_RESOLUTION;
411 switch(pf->color_type) {
413 if (ps->color_type != FF_COLOR_RGB &&
414 ps->color_type != FF_COLOR_GRAY)
415 loss |= FF_LOSS_COLORSPACE;
418 if (ps->color_type != FF_COLOR_GRAY)
419 loss |= FF_LOSS_COLORSPACE;
422 if (ps->color_type != FF_COLOR_YUV)
423 loss |= FF_LOSS_COLORSPACE;
425 case FF_COLOR_YUV_JPEG:
426 if (ps->color_type != FF_COLOR_YUV_JPEG &&
427 ps->color_type != FF_COLOR_YUV &&
428 ps->color_type != FF_COLOR_GRAY)
429 loss |= FF_LOSS_COLORSPACE;
433 if (ps->color_type != pf->color_type)
434 loss |= FF_LOSS_COLORSPACE;
437 if (pf->color_type == FF_COLOR_GRAY &&
438 ps->color_type != FF_COLOR_GRAY)
439 loss |= FF_LOSS_CHROMA;
440 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
441 loss |= FF_LOSS_ALPHA;
442 if (pf->pixel_type == FF_PIXEL_PALETTE &&
443 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
444 loss |= FF_LOSS_COLORQUANT;
448 static int avg_bits_per_pixel(int pix_fmt)
451 const PixFmtInfo *pf;
453 pf = &pix_fmt_info[pix_fmt];
454 switch(pf->pixel_type) {
455 case FF_PIXEL_PACKED:
458 case PIX_FMT_UYVY422:
464 bits = pf->depth * pf->nb_channels;
468 case FF_PIXEL_PLANAR:
469 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
470 bits = pf->depth * pf->nb_channels;
472 bits = pf->depth + ((2 * pf->depth) >>
473 (pf->x_chroma_shift + pf->y_chroma_shift));
476 case FF_PIXEL_PALETTE:
486 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
491 int dist, i, loss, min_dist, dst_pix_fmt;
493 /* find exact color match with smallest size */
495 min_dist = 0x7fffffff;
496 for(i = 0;i < PIX_FMT_NB; i++) {
497 if (pix_fmt_mask & (1 << i)) {
498 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
500 dist = avg_bits_per_pixel(i);
501 if (dist < min_dist) {
512 * find best pixel format to convert to. Return -1 if none found
514 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
515 int has_alpha, int *loss_ptr)
517 int dst_pix_fmt, loss_mask, i;
518 static const int loss_mask_order[] = {
519 ~0, /* no loss first */
522 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
528 /* try with successive loss */
531 loss_mask = loss_mask_order[i++];
532 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
533 has_alpha, loss_mask);
534 if (dst_pix_fmt >= 0)
542 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
546 static void img_copy_plane(uint8_t *dst, int dst_wrap,
547 const uint8_t *src, int src_wrap,
548 int width, int height)
550 for(;height > 0; height--) {
551 memcpy(dst, src, width);
558 * Copy image 'src' to 'dst'.
560 void img_copy(AVPicture *dst, const AVPicture *src,
561 int pix_fmt, int width, int height)
564 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
566 pf = &pix_fmt_info[pix_fmt];
567 switch(pf->pixel_type) {
568 case FF_PIXEL_PACKED:
571 case PIX_FMT_UYVY422:
577 bits = pf->depth * pf->nb_channels;
580 bwidth = (width * bits + 7) >> 3;
581 img_copy_plane(dst->data[0], dst->linesize[0],
582 src->data[0], src->linesize[0],
585 case FF_PIXEL_PLANAR:
586 for(i = 0; i < pf->nb_channels; i++) {
590 if (i == 1 || i == 2) {
591 w >>= pf->x_chroma_shift;
592 h >>= pf->y_chroma_shift;
594 bwidth = (w * pf->depth + 7) >> 3;
595 img_copy_plane(dst->data[i], dst->linesize[i],
596 src->data[i], src->linesize[i],
600 case FF_PIXEL_PALETTE:
601 img_copy_plane(dst->data[0], dst->linesize[0],
602 src->data[0], src->linesize[0],
604 /* copy the palette */
605 img_copy_plane(dst->data[1], dst->linesize[1],
606 src->data[1], src->linesize[1],
612 /* XXX: totally non optimized */
614 static void yuv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
615 int width, int height)
617 const uint8_t *p, *p1;
618 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
626 for(;height >= 1; height -= 2) {
631 for(w = width; w >= 2; w -= 2) {
648 p1 += src->linesize[0];
649 lum1 += dst->linesize[0];
653 for(w = width; w >= 2; w -= 2) {
662 p1 += src->linesize[0];
663 lum1 += dst->linesize[0];
665 cb1 += dst->linesize[1];
666 cr1 += dst->linesize[2];
670 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
671 int width, int height)
673 const uint8_t *p, *p1;
674 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
683 for(;height >= 1; height -= 2) {
688 for(w = width; w >= 2; w -= 2) {
705 p1 += src->linesize[0];
706 lum1 += dst->linesize[0];
710 for(w = width; w >= 2; w -= 2) {
719 p1 += src->linesize[0];
720 lum1 += dst->linesize[0];
722 cb1 += dst->linesize[1];
723 cr1 += dst->linesize[2];
728 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
729 int width, int height)
731 const uint8_t *p, *p1;
732 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
739 for(;height > 0; height--) {
744 for(w = width; w >= 2; w -= 2) {
754 p1 += src->linesize[0];
755 lum1 += dst->linesize[0];
756 cb1 += dst->linesize[1];
757 cr1 += dst->linesize[2];
762 static void yuv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
763 int width, int height)
765 const uint8_t *p, *p1;
766 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
773 for(;height > 0; height--) {
778 for(w = width; w >= 2; w -= 2) {
788 p1 += src->linesize[0];
789 lum1 += dst->linesize[0];
790 cb1 += dst->linesize[1];
791 cr1 += dst->linesize[2];
795 static void yuv422p_to_yuv422(AVPicture *dst, const AVPicture *src,
796 int width, int height)
799 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
806 for(;height > 0; height--) {
811 for(w = width; w >= 2; w -= 2) {
821 p1 += dst->linesize[0];
822 lum1 += src->linesize[0];
823 cb1 += src->linesize[1];
824 cr1 += src->linesize[2];
828 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
829 int width, int height)
832 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
839 for(;height > 0; height--) {
844 for(w = width; w >= 2; w -= 2) {
854 p1 += dst->linesize[0];
855 lum1 += src->linesize[0];
856 cb1 += src->linesize[1];
857 cr1 += src->linesize[2];
864 #define ONE_HALF (1 << (SCALEBITS - 1))
865 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
867 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
871 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
872 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
874 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
877 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
879 y = ((y1) - 16) * FIX(255.0/219.0);\
880 r = cm[(y + r_add) >> SCALEBITS];\
881 g = cm[(y + g_add) >> SCALEBITS];\
882 b = cm[(y + b_add) >> SCALEBITS];\
885 #define YUV_TO_RGB1(cb1, cr1)\
889 r_add = FIX(1.40200) * cr + ONE_HALF;\
890 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
891 b_add = FIX(1.77200) * cb + ONE_HALF;\
894 #define YUV_TO_RGB2(r, g, b, y1)\
896 y = (y1) << SCALEBITS;\
897 r = cm[(y + r_add) >> SCALEBITS];\
898 g = cm[(y + g_add) >> SCALEBITS];\
899 b = cm[(y + b_add) >> SCALEBITS];\
902 #define Y_CCIR_TO_JPEG(y)\
903 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
905 #define Y_JPEG_TO_CCIR(y)\
906 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
908 #define C_CCIR_TO_JPEG(y)\
909 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
911 /* NOTE: the clamp is really necessary! */
912 static inline int C_JPEG_TO_CCIR(int y) {
913 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
920 #define RGB_TO_Y(r, g, b) \
921 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
922 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
924 #define RGB_TO_U(r1, g1, b1, shift)\
925 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
926 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
928 #define RGB_TO_V(r1, g1, b1, shift)\
929 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
930 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
932 #define RGB_TO_Y_CCIR(r, g, b) \
933 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
934 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
936 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
937 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
938 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
940 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
941 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
942 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
944 static uint8_t y_ccir_to_jpeg[256];
945 static uint8_t y_jpeg_to_ccir[256];
946 static uint8_t c_ccir_to_jpeg[256];
947 static uint8_t c_jpeg_to_ccir[256];
949 /* init various conversion tables */
950 static void img_convert_init(void)
953 uint8_t *cm = cropTbl + MAX_NEG_CROP;
955 for(i = 0;i < 256; i++) {
956 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
957 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
958 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
959 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
963 /* apply to each pixel the given table */
964 static void img_apply_table(uint8_t *dst, int dst_wrap,
965 const uint8_t *src, int src_wrap,
966 int width, int height, const uint8_t *table1)
971 const uint8_t *table;
974 for(;height > 0; height--) {
998 /* XXX: use generic filter ? */
999 /* XXX: in most cases, the sampling position is incorrect */
1002 static void shrink41(uint8_t *dst, int dst_wrap,
1003 const uint8_t *src, int src_wrap,
1004 int width, int height)
1010 for(;height > 0; height--) {
1013 for(w = width;w > 0; w--) {
1014 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1024 static void shrink21(uint8_t *dst, int dst_wrap,
1025 const uint8_t *src, int src_wrap,
1026 int width, int height)
1032 for(;height > 0; height--) {
1035 for(w = width;w > 0; w--) {
1036 d[0] = (s[0] + s[1]) >> 1;
1046 static void shrink12(uint8_t *dst, int dst_wrap,
1047 const uint8_t *src, int src_wrap,
1048 int width, int height)
1052 const uint8_t *s1, *s2;
1054 for(;height > 0; height--) {
1058 for(w = width;w >= 4; w-=4) {
1059 d[0] = (s1[0] + s2[0]) >> 1;
1060 d[1] = (s1[1] + s2[1]) >> 1;
1061 d[2] = (s1[2] + s2[2]) >> 1;
1062 d[3] = (s1[3] + s2[3]) >> 1;
1068 d[0] = (s1[0] + s2[0]) >> 1;
1073 src += 2 * src_wrap;
1079 static void shrink22(uint8_t *dst, int dst_wrap,
1080 const uint8_t *src, int src_wrap,
1081 int width, int height)
1084 const uint8_t *s1, *s2;
1087 for(;height > 0; height--) {
1091 for(w = width;w >= 4; w-=4) {
1092 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1093 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1094 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1095 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1101 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1106 src += 2 * src_wrap;
1112 static void shrink44(uint8_t *dst, int dst_wrap,
1113 const uint8_t *src, int src_wrap,
1114 int width, int height)
1117 const uint8_t *s1, *s2, *s3, *s4;
1120 for(;height > 0; height--) {
1126 for(w = width;w > 0; w--) {
1127 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1128 s2[0] + s2[1] + s2[2] + s2[3] +
1129 s3[0] + s3[1] + s3[2] + s3[3] +
1130 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1137 src += 4 * src_wrap;
1142 static void grow21_line(uint8_t *dst, const uint8_t *src,
1151 for(w = width;w >= 4; w-=4) {
1152 d[1] = d[0] = s1[0];
1153 d[3] = d[2] = s1[1];
1157 for(;w >= 2; w -= 2) {
1158 d[1] = d[0] = s1[0];
1162 /* only needed if width is not a multiple of two */
1163 /* XXX: veryfy that */
1169 static void grow41_line(uint8_t *dst, const uint8_t *src,
1178 for(w = width;w >= 4; w-=4) {
1190 static void grow21(uint8_t *dst, int dst_wrap,
1191 const uint8_t *src, int src_wrap,
1192 int width, int height)
1194 for(;height > 0; height--) {
1195 grow21_line(dst, src, width);
1202 static void grow22(uint8_t *dst, int dst_wrap,
1203 const uint8_t *src, int src_wrap,
1204 int width, int height)
1206 for(;height > 0; height--) {
1207 grow21_line(dst, src, width);
1215 static void grow41(uint8_t *dst, int dst_wrap,
1216 const uint8_t *src, int src_wrap,
1217 int width, int height)
1219 for(;height > 0; height--) {
1220 grow41_line(dst, src, width);
1227 static void grow44(uint8_t *dst, int dst_wrap,
1228 const uint8_t *src, int src_wrap,
1229 int width, int height)
1231 for(;height > 0; height--) {
1232 grow41_line(dst, src, width);
1233 if ((height & 3) == 1)
1240 static void conv411(uint8_t *dst, int dst_wrap,
1241 const uint8_t *src, int src_wrap,
1242 int width, int height)
1245 const uint8_t *s1, *s2;
1250 for(;height > 0; height--) {
1252 s2 = src + src_wrap;
1254 for(w = width;w > 0; w--) {
1255 c = (s1[0] + s2[0]) >> 1;
1262 src += src_wrap * 2;
1267 /* XXX: add jpeg quantize code */
1269 #define TRANSP_INDEX (6*6*6)
1271 /* this is maybe slow, but allows for extensions */
1272 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1274 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1277 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1280 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1283 pal = (uint32_t *)palette;
1285 for(r = 0; r < 6; r++) {
1286 for(g = 0; g < 6; g++) {
1287 for(b = 0; b < 6; b++) {
1288 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1289 (pal_value[g] << 8) | pal_value[b];
1296 pal[i++] = 0xff000000;
1299 /* copy bit n to bits 0 ... n - 1 */
1300 static inline unsigned int bitcopy_n(unsigned int a, int n)
1303 mask = (1 << n) - 1;
1304 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1307 /* rgb555 handling */
1309 #define RGB_NAME rgb555
1311 #define RGB_IN(r, g, b, s)\
1313 unsigned int v = ((const uint16_t *)(s))[0];\
1314 r = bitcopy_n(v >> (10 - 3), 3);\
1315 g = bitcopy_n(v >> (5 - 3), 3);\
1316 b = bitcopy_n(v << 3, 3);\
1319 #define RGBA_IN(r, g, b, a, s)\
1321 unsigned int v = ((const uint16_t *)(s))[0];\
1322 r = bitcopy_n(v >> (10 - 3), 3);\
1323 g = bitcopy_n(v >> (5 - 3), 3);\
1324 b = bitcopy_n(v << 3, 3);\
1325 a = (-(v >> 15)) & 0xff;\
1328 #define RGBA_OUT(d, r, g, b, a)\
1330 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1331 ((a << 8) & 0x8000);\
1336 #include "imgconvert_template.h"
1338 /* rgb565 handling */
1340 #define RGB_NAME rgb565
1342 #define RGB_IN(r, g, b, s)\
1344 unsigned int v = ((const uint16_t *)(s))[0];\
1345 r = bitcopy_n(v >> (11 - 3), 3);\
1346 g = bitcopy_n(v >> (5 - 2), 2);\
1347 b = bitcopy_n(v << 3, 3);\
1350 #define RGB_OUT(d, r, g, b)\
1352 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1357 #include "imgconvert_template.h"
1359 /* bgr24 handling */
1361 #define RGB_NAME bgr24
1363 #define RGB_IN(r, g, b, s)\
1370 #define RGB_OUT(d, r, g, b)\
1379 #include "imgconvert_template.h"
1385 /* rgb24 handling */
1387 #define RGB_NAME rgb24
1390 #define RGB_IN(r, g, b, s)\
1397 #define RGB_OUT(d, r, g, b)\
1406 #include "imgconvert_template.h"
1408 /* rgba32 handling */
1410 #define RGB_NAME rgba32
1413 #define RGB_IN(r, g, b, s)\
1415 unsigned int v = ((const uint32_t *)(s))[0];\
1416 r = (v >> 16) & 0xff;\
1417 g = (v >> 8) & 0xff;\
1421 #define RGBA_IN(r, g, b, a, s)\
1423 unsigned int v = ((const uint32_t *)(s))[0];\
1424 a = (v >> 24) & 0xff;\
1425 r = (v >> 16) & 0xff;\
1426 g = (v >> 8) & 0xff;\
1430 #define RGBA_OUT(d, r, g, b, a)\
1432 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1437 #include "imgconvert_template.h"
1439 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1440 int width, int height, int xor_mask)
1442 const unsigned char *p;
1444 int v, dst_wrap, src_wrap;
1448 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1451 dst_wrap = dst->linesize[0] - width;
1452 for(y=0;y<height;y++) {
1455 v = *p++ ^ xor_mask;
1457 q[1] = -((v >> 6) & 1);
1458 q[2] = -((v >> 5) & 1);
1459 q[3] = -((v >> 4) & 1);
1460 q[4] = -((v >> 3) & 1);
1461 q[5] = -((v >> 2) & 1);
1462 q[6] = -((v >> 1) & 1);
1463 q[7] = -((v >> 0) & 1);
1468 v = *p++ ^ xor_mask;
1470 q[0] = -((v >> 7) & 1);
1480 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1481 int width, int height)
1483 mono_to_gray(dst, src, width, height, 0xff);
1486 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1487 int width, int height)
1489 mono_to_gray(dst, src, width, height, 0x00);
1492 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1493 int width, int height, int xor_mask)
1498 int j, b, v, n1, src_wrap, dst_wrap, y;
1501 src_wrap = src->linesize[0] - width;
1504 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1506 for(y=0;y<height;y++) {
1513 v = (v << 1) | (b >> 7);
1515 d[0] = v ^ xor_mask;
1525 v = (v << 1) | (b >> 7);
1528 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1536 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1537 int width, int height)
1539 gray_to_mono(dst, src, width, height, 0xff);
1542 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1543 int width, int height)
1545 gray_to_mono(dst, src, width, height, 0x00);
1548 typedef struct ConvertEntry {
1549 void (*convert)(AVPicture *dst,
1550 const AVPicture *src, int width, int height);
1553 /* Add each new convertion function in this table. In order to be able
1554 to convert from any format to any format, the following constraints
1557 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1559 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1561 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1563 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1566 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1568 The other conversion functions are just optimisations for common cases.
1570 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1571 [PIX_FMT_YUV420P] = {
1572 [PIX_FMT_RGB555] = {
1573 .convert = yuv420p_to_rgb555
1575 [PIX_FMT_RGB565] = {
1576 .convert = yuv420p_to_rgb565
1579 .convert = yuv420p_to_bgr24
1582 .convert = yuv420p_to_rgb24
1584 [PIX_FMT_RGBA32] = {
1585 .convert = yuv420p_to_rgba32
1588 [PIX_FMT_YUV422P] = {
1589 [PIX_FMT_YUV422] = {
1590 .convert = yuv422p_to_yuv422,
1592 [PIX_FMT_UYVY422] = {
1593 .convert = yuv422p_to_uyvy422,
1596 [PIX_FMT_YUV444P] = {
1598 .convert = yuv444p_to_rgb24
1601 [PIX_FMT_YUVJ420P] = {
1602 [PIX_FMT_RGB555] = {
1603 .convert = yuvj420p_to_rgb555
1605 [PIX_FMT_RGB565] = {
1606 .convert = yuvj420p_to_rgb565
1609 .convert = yuvj420p_to_bgr24
1612 .convert = yuvj420p_to_rgb24
1614 [PIX_FMT_RGBA32] = {
1615 .convert = yuvj420p_to_rgba32
1618 [PIX_FMT_YUVJ444P] = {
1620 .convert = yuvj444p_to_rgb24
1623 [PIX_FMT_YUV422] = {
1624 [PIX_FMT_YUV420P] = {
1625 .convert = yuv422_to_yuv420p,
1627 [PIX_FMT_YUV422P] = {
1628 .convert = yuv422_to_yuv422p,
1631 [PIX_FMT_UYVY422] = {
1632 [PIX_FMT_YUV420P] = {
1633 .convert = uyvy422_to_yuv420p,
1635 [PIX_FMT_YUV422P] = {
1636 .convert = uyvy422_to_yuv422p,
1640 [PIX_FMT_YUV420P] = {
1641 .convert = rgb24_to_yuv420p
1643 [PIX_FMT_RGB565] = {
1644 .convert = rgb24_to_rgb565
1646 [PIX_FMT_RGB555] = {
1647 .convert = rgb24_to_rgb555
1649 [PIX_FMT_RGBA32] = {
1650 .convert = rgb24_to_rgba32
1653 .convert = rgb24_to_bgr24
1656 .convert = rgb24_to_gray
1659 .convert = rgb24_to_pal8
1661 [PIX_FMT_YUV444P] = {
1662 .convert = rgb24_to_yuv444p
1664 [PIX_FMT_YUVJ420P] = {
1665 .convert = rgb24_to_yuvj420p
1667 [PIX_FMT_YUVJ444P] = {
1668 .convert = rgb24_to_yuvj444p
1671 [PIX_FMT_RGBA32] = {
1673 .convert = rgba32_to_rgb24
1675 [PIX_FMT_RGB555] = {
1676 .convert = rgba32_to_rgb555
1679 .convert = rgba32_to_pal8
1681 [PIX_FMT_YUV420P] = {
1682 .convert = rgba32_to_yuv420p
1685 .convert = rgba32_to_gray
1690 .convert = bgr24_to_rgb24
1692 [PIX_FMT_YUV420P] = {
1693 .convert = bgr24_to_yuv420p
1696 .convert = bgr24_to_gray
1699 [PIX_FMT_RGB555] = {
1701 .convert = rgb555_to_rgb24
1703 [PIX_FMT_RGBA32] = {
1704 .convert = rgb555_to_rgba32
1706 [PIX_FMT_YUV420P] = {
1707 .convert = rgb555_to_yuv420p
1710 .convert = rgb555_to_gray
1713 [PIX_FMT_RGB565] = {
1715 .convert = rgb565_to_rgb24
1717 [PIX_FMT_YUV420P] = {
1718 .convert = rgb565_to_yuv420p
1721 .convert = rgb565_to_gray
1725 [PIX_FMT_RGB555] = {
1726 .convert = gray_to_rgb555
1728 [PIX_FMT_RGB565] = {
1729 .convert = gray_to_rgb565
1732 .convert = gray_to_rgb24
1735 .convert = gray_to_bgr24
1737 [PIX_FMT_RGBA32] = {
1738 .convert = gray_to_rgba32
1740 [PIX_FMT_MONOWHITE] = {
1741 .convert = gray_to_monowhite
1743 [PIX_FMT_MONOBLACK] = {
1744 .convert = gray_to_monoblack
1747 [PIX_FMT_MONOWHITE] = {
1749 .convert = monowhite_to_gray
1752 [PIX_FMT_MONOBLACK] = {
1754 .convert = monoblack_to_gray
1758 [PIX_FMT_RGB555] = {
1759 .convert = pal8_to_rgb555
1761 [PIX_FMT_RGB565] = {
1762 .convert = pal8_to_rgb565
1765 .convert = pal8_to_bgr24
1768 .convert = pal8_to_rgb24
1770 [PIX_FMT_RGBA32] = {
1771 .convert = pal8_to_rgba32
1776 int avpicture_alloc(AVPicture *picture,
1777 int pix_fmt, int width, int height)
1782 size = avpicture_get_size(pix_fmt, width, height);
1783 ptr = av_malloc(size);
1786 avpicture_fill(picture, ptr, pix_fmt, width, height);
1789 memset(picture, 0, sizeof(AVPicture));
1793 void avpicture_free(AVPicture *picture)
1795 av_free(picture->data[0]);
1798 /* return true if yuv planar */
1799 static inline int is_yuv_planar(PixFmtInfo *ps)
1801 return (ps->color_type == FF_COLOR_YUV ||
1802 ps->color_type == FF_COLOR_YUV_JPEG) &&
1803 ps->pixel_type == FF_PIXEL_PLANAR;
1806 /* XXX: always use linesize. Return -1 if not supported */
1807 int img_convert(AVPicture *dst, int dst_pix_fmt,
1808 const AVPicture *src, int src_pix_fmt,
1809 int src_width, int src_height)
1812 int i, ret, dst_width, dst_height, int_pix_fmt;
1813 PixFmtInfo *src_pix, *dst_pix;
1815 AVPicture tmp1, *tmp = &tmp1;
1817 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1818 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1820 if (src_width <= 0 || src_height <= 0)
1828 dst_width = src_width;
1829 dst_height = src_height;
1831 dst_pix = &pix_fmt_info[dst_pix_fmt];
1832 src_pix = &pix_fmt_info[src_pix_fmt];
1833 if (src_pix_fmt == dst_pix_fmt) {
1834 /* no conversion needed: just copy */
1835 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
1839 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1841 /* specific conversion routine */
1842 ce->convert(dst, src, dst_width, dst_height);
1847 if (is_yuv_planar(dst_pix) &&
1848 src_pix_fmt == PIX_FMT_GRAY8) {
1852 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
1853 img_copy_plane(dst->data[0], dst->linesize[0],
1854 src->data[0], src->linesize[0],
1855 dst_width, dst_height);
1857 img_apply_table(dst->data[0], dst->linesize[0],
1858 src->data[0], src->linesize[0],
1859 dst_width, dst_height,
1862 /* fill U and V with 128 */
1865 w >>= dst_pix->x_chroma_shift;
1866 h >>= dst_pix->y_chroma_shift;
1867 for(i = 1; i <= 2; i++) {
1869 for(y = 0; y< h; y++) {
1871 d += dst->linesize[i];
1878 if (is_yuv_planar(src_pix) &&
1879 dst_pix_fmt == PIX_FMT_GRAY8) {
1880 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
1881 img_copy_plane(dst->data[0], dst->linesize[0],
1882 src->data[0], src->linesize[0],
1883 dst_width, dst_height);
1885 img_apply_table(dst->data[0], dst->linesize[0],
1886 src->data[0], src->linesize[0],
1887 dst_width, dst_height,
1893 /* YUV to YUV planar */
1894 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
1895 int x_shift, y_shift, w, h, xy_shift;
1896 void (*resize_func)(uint8_t *dst, int dst_wrap,
1897 const uint8_t *src, int src_wrap,
1898 int width, int height);
1900 /* compute chroma size of the smallest dimensions */
1903 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1904 w >>= dst_pix->x_chroma_shift;
1906 w >>= src_pix->x_chroma_shift;
1907 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1908 h >>= dst_pix->y_chroma_shift;
1910 h >>= src_pix->y_chroma_shift;
1912 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1913 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
1914 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
1915 /* there must be filters for conversion at least from and to
1919 resize_func = img_copy_plane;
1922 resize_func = shrink21;
1925 resize_func = shrink41;
1928 resize_func = shrink12;
1931 resize_func = shrink22;
1934 resize_func = shrink44;
1937 resize_func = grow21;
1940 resize_func = grow41;
1943 resize_func = grow22;
1946 resize_func = grow44;
1949 resize_func = conv411;
1952 /* currently not handled */
1953 goto no_chroma_filter;
1956 img_copy_plane(dst->data[0], dst->linesize[0],
1957 src->data[0], src->linesize[0],
1958 dst_width, dst_height);
1960 for(i = 1;i <= 2; i++)
1961 resize_func(dst->data[i], dst->linesize[i],
1962 src->data[i], src->linesize[i],
1963 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
1964 /* if yuv color space conversion is needed, we do it here on
1965 the destination image */
1966 if (dst_pix->color_type != src_pix->color_type) {
1967 const uint8_t *y_table, *c_table;
1968 if (dst_pix->color_type == FF_COLOR_YUV) {
1969 y_table = y_jpeg_to_ccir;
1970 c_table = c_jpeg_to_ccir;
1972 y_table = y_ccir_to_jpeg;
1973 c_table = c_ccir_to_jpeg;
1975 img_apply_table(dst->data[0], dst->linesize[0],
1976 dst->data[0], dst->linesize[0],
1977 dst_width, dst_height,
1980 for(i = 1;i <= 2; i++)
1981 img_apply_table(dst->data[i], dst->linesize[i],
1982 dst->data[i], dst->linesize[i],
1983 dst_width>>dst_pix->x_chroma_shift,
1984 dst_height>>dst_pix->y_chroma_shift,
1991 /* try to use an intermediate format */
1992 if (src_pix_fmt == PIX_FMT_YUV422 ||
1993 dst_pix_fmt == PIX_FMT_YUV422) {
1994 /* specific case: convert to YUV422P first */
1995 int_pix_fmt = PIX_FMT_YUV422P;
1996 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
1997 dst_pix_fmt == PIX_FMT_UYVY422) {
1998 /* specific case: convert to YUV422P first */
1999 int_pix_fmt = PIX_FMT_YUV422P;
2000 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2001 src_pix_fmt != PIX_FMT_GRAY8) ||
2002 (dst_pix->color_type == FF_COLOR_GRAY &&
2003 dst_pix_fmt != PIX_FMT_GRAY8)) {
2004 /* gray8 is the normalized format */
2005 int_pix_fmt = PIX_FMT_GRAY8;
2006 } else if ((is_yuv_planar(src_pix) &&
2007 src_pix_fmt != PIX_FMT_YUV444P &&
2008 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2009 /* yuv444 is the normalized format */
2010 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2011 int_pix_fmt = PIX_FMT_YUVJ444P;
2013 int_pix_fmt = PIX_FMT_YUV444P;
2014 } else if ((is_yuv_planar(dst_pix) &&
2015 dst_pix_fmt != PIX_FMT_YUV444P &&
2016 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2017 /* yuv444 is the normalized format */
2018 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2019 int_pix_fmt = PIX_FMT_YUVJ444P;
2021 int_pix_fmt = PIX_FMT_YUV444P;
2023 /* the two formats are rgb or gray8 or yuv[j]444p */
2024 if (src_pix->is_alpha && dst_pix->is_alpha)
2025 int_pix_fmt = PIX_FMT_RGBA32;
2027 int_pix_fmt = PIX_FMT_RGB24;
2029 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2032 if (img_convert(tmp, int_pix_fmt,
2033 src, src_pix_fmt, src_width, src_height) < 0)
2035 if (img_convert(dst, dst_pix_fmt,
2036 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2040 avpicture_free(tmp);
2044 /* NOTE: we scan all the pixels to have an exact information */
2045 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2047 const unsigned char *p;
2048 int src_wrap, ret, x, y;
2050 uint32_t *palette = (uint32_t *)src->data[1];
2053 src_wrap = src->linesize[0] - width;
2055 for(y=0;y<height;y++) {
2056 for(x=0;x<width;x++) {
2057 a = palette[p[0]] >> 24;
2059 ret |= FF_ALPHA_TRANSP;
2060 } else if (a != 0xff) {
2061 ret |= FF_ALPHA_SEMI_TRANSP;
2071 * Tell if an image really has transparent alpha values.
2072 * @return ored mask of FF_ALPHA_xxx constants
2074 int img_get_alpha_info(const AVPicture *src,
2075 int pix_fmt, int width, int height)
2077 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2080 pf = &pix_fmt_info[pix_fmt];
2081 /* no alpha can be represented in format */
2085 case PIX_FMT_RGBA32:
2086 ret = get_alpha_info_rgba32(src, width, height);
2088 case PIX_FMT_RGB555:
2089 ret = get_alpha_info_rgb555(src, width, height);
2092 ret = get_alpha_info_pal8(src, width, height);
2095 /* we do not know, so everything is indicated */
2096 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2103 #define DEINT_INPLACE_LINE_LUM \
2104 movd_m2r(lum_m4[0],mm0);\
2105 movd_m2r(lum_m3[0],mm1);\
2106 movd_m2r(lum_m2[0],mm2);\
2107 movd_m2r(lum_m1[0],mm3);\
2108 movd_m2r(lum[0],mm4);\
2109 punpcklbw_r2r(mm7,mm0);\
2110 movd_r2m(mm2,lum_m4[0]);\
2111 punpcklbw_r2r(mm7,mm1);\
2112 punpcklbw_r2r(mm7,mm2);\
2113 punpcklbw_r2r(mm7,mm3);\
2114 punpcklbw_r2r(mm7,mm4);\
2115 paddw_r2r(mm3,mm1);\
2117 paddw_r2r(mm4,mm0);\
2119 paddw_r2r(mm6,mm2);\
2120 paddw_r2r(mm2,mm1);\
2121 psubusw_r2r(mm0,mm1);\
2123 packuswb_r2r(mm7,mm1);\
2124 movd_r2m(mm1,lum_m2[0]);
2126 #define DEINT_LINE_LUM \
2127 movd_m2r(lum_m4[0],mm0);\
2128 movd_m2r(lum_m3[0],mm1);\
2129 movd_m2r(lum_m2[0],mm2);\
2130 movd_m2r(lum_m1[0],mm3);\
2131 movd_m2r(lum[0],mm4);\
2132 punpcklbw_r2r(mm7,mm0);\
2133 punpcklbw_r2r(mm7,mm1);\
2134 punpcklbw_r2r(mm7,mm2);\
2135 punpcklbw_r2r(mm7,mm3);\
2136 punpcklbw_r2r(mm7,mm4);\
2137 paddw_r2r(mm3,mm1);\
2139 paddw_r2r(mm4,mm0);\
2141 paddw_r2r(mm6,mm2);\
2142 paddw_r2r(mm2,mm1);\
2143 psubusw_r2r(mm0,mm1);\
2145 packuswb_r2r(mm7,mm1);\
2146 movd_r2m(mm1,dst[0]);
2149 /* filter parameters: [-1 4 2 4 -1] // 8 */
2150 static void deinterlace_line(uint8_t *dst,
2151 const uint8_t *lum_m4, const uint8_t *lum_m3,
2152 const uint8_t *lum_m2, const uint8_t *lum_m1,
2157 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2160 for(;size > 0;size--) {
2162 sum += lum_m3[0] << 2;
2163 sum += lum_m2[0] << 1;
2164 sum += lum_m1[0] << 2;
2166 dst[0] = cm[(sum + 4) >> 3];
2183 movq_m2r(rounder,mm6);
2185 for (;size > 3; size-=4) {
2196 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2200 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2203 for(;size > 0;size--) {
2205 sum += lum_m3[0] << 2;
2206 sum += lum_m2[0] << 1;
2207 lum_m4[0]=lum_m2[0];
2208 sum += lum_m1[0] << 2;
2210 lum_m2[0] = cm[(sum + 4) >> 3];
2226 movq_m2r(rounder,mm6);
2228 for (;size > 3; size-=4) {
2229 DEINT_INPLACE_LINE_LUM
2239 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2240 top field is copied as is, but the bottom field is deinterlaced
2241 against the top field. */
2242 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2243 const uint8_t *src1, int src_wrap,
2244 int width, int height)
2246 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2251 src_0=&src_m1[src_wrap];
2252 src_p1=&src_0[src_wrap];
2253 src_p2=&src_p1[src_wrap];
2254 for(y=0;y<(height-2);y+=2) {
2255 memcpy(dst,src_m1,width);
2257 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2261 src_p1 += 2*src_wrap;
2262 src_p2 += 2*src_wrap;
2265 memcpy(dst,src_m1,width);
2268 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2271 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2272 int width, int height)
2274 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2277 buf = (uint8_t*)av_malloc(width);
2280 memcpy(buf,src_m1,width);
2281 src_0=&src_m1[src_wrap];
2282 src_p1=&src_0[src_wrap];
2283 src_p2=&src_p1[src_wrap];
2284 for(y=0;y<(height-2);y+=2) {
2285 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2288 src_p1 += 2*src_wrap;
2289 src_p2 += 2*src_wrap;
2292 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2297 /* deinterlace - if not supported return -1 */
2298 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2299 int pix_fmt, int width, int height)
2303 if (pix_fmt != PIX_FMT_YUV420P &&
2304 pix_fmt != PIX_FMT_YUV422P &&
2305 pix_fmt != PIX_FMT_YUV444P &&
2306 pix_fmt != PIX_FMT_YUV411P)
2308 if ((width & 3) != 0 || (height & 3) != 0)
2314 case PIX_FMT_YUV420P:
2318 case PIX_FMT_YUV422P:
2321 case PIX_FMT_YUV411P:
2329 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2332 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2333 src->data[i], src->linesize[i],