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)
581 for(;height > 0; height--) {
582 memcpy(dst, src, width);
589 * Copy image 'src' to 'dst'.
591 void img_copy(AVPicture *dst, const AVPicture *src,
592 int pix_fmt, int width, int height)
595 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
597 pf = &pix_fmt_info[pix_fmt];
598 switch(pf->pixel_type) {
599 case FF_PIXEL_PACKED:
602 case PIX_FMT_UYVY422:
607 case PIX_FMT_UYVY411:
611 bits = pf->depth * pf->nb_channels;
614 bwidth = (width * bits + 7) >> 3;
615 img_copy_plane(dst->data[0], dst->linesize[0],
616 src->data[0], src->linesize[0],
619 case FF_PIXEL_PLANAR:
620 for(i = 0; i < pf->nb_channels; i++) {
624 if (i == 1 || i == 2) {
625 w >>= pf->x_chroma_shift;
626 h >>= pf->y_chroma_shift;
628 bwidth = (w * pf->depth + 7) >> 3;
629 img_copy_plane(dst->data[i], dst->linesize[i],
630 src->data[i], src->linesize[i],
634 case FF_PIXEL_PALETTE:
635 img_copy_plane(dst->data[0], dst->linesize[0],
636 src->data[0], src->linesize[0],
638 /* copy the palette */
639 img_copy_plane(dst->data[1], dst->linesize[1],
640 src->data[1], src->linesize[1],
646 /* XXX: totally non optimized */
648 static void yuv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
649 int width, int height)
651 const uint8_t *p, *p1;
652 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
660 for(;height >= 1; height -= 2) {
665 for(w = width; w >= 2; w -= 2) {
682 p1 += src->linesize[0];
683 lum1 += dst->linesize[0];
687 for(w = width; w >= 2; w -= 2) {
696 p1 += src->linesize[0];
697 lum1 += dst->linesize[0];
699 cb1 += dst->linesize[1];
700 cr1 += dst->linesize[2];
704 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
705 int width, int height)
707 const uint8_t *p, *p1;
708 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
717 for(;height >= 1; height -= 2) {
722 for(w = width; w >= 2; w -= 2) {
739 p1 += src->linesize[0];
740 lum1 += dst->linesize[0];
744 for(w = width; w >= 2; w -= 2) {
753 p1 += src->linesize[0];
754 lum1 += dst->linesize[0];
756 cb1 += dst->linesize[1];
757 cr1 += dst->linesize[2];
762 static void uyvy422_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];
796 static void yuv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
797 int width, int height)
799 const uint8_t *p, *p1;
800 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
807 for(;height > 0; height--) {
812 for(w = width; w >= 2; w -= 2) {
822 p1 += src->linesize[0];
823 lum1 += dst->linesize[0];
824 cb1 += dst->linesize[1];
825 cr1 += dst->linesize[2];
829 static void yuv422p_to_yuv422(AVPicture *dst, const AVPicture *src,
830 int width, int height)
833 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
840 for(;height > 0; height--) {
845 for(w = width; w >= 2; w -= 2) {
855 p1 += dst->linesize[0];
856 lum1 += src->linesize[0];
857 cb1 += src->linesize[1];
858 cr1 += src->linesize[2];
862 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
863 int width, int height)
866 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
873 for(;height > 0; height--) {
878 for(w = width; w >= 2; w -= 2) {
888 p1 += dst->linesize[0];
889 lum1 += src->linesize[0];
890 cb1 += src->linesize[1];
891 cr1 += src->linesize[2];
895 static void uyvy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
896 int width, int height)
898 const uint8_t *p, *p1;
899 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
906 for(;height > 0; height--) {
911 for(w = width; w >= 4; w -= 4) {
923 p1 += src->linesize[0];
924 lum1 += dst->linesize[0];
925 cb1 += dst->linesize[1];
926 cr1 += dst->linesize[2];
931 static void yuv420p_to_yuv422(AVPicture *dst, const AVPicture *src,
932 int width, int height)
935 uint8_t *line1, *line2, *linesrc = dst->data[0];
936 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
937 uint8_t *cb1, *cb2 = src->data[1];
938 uint8_t *cr1, *cr2 = src->data[2];
940 for(h = height / 2; h--;) {
942 line2 = linesrc + dst->linesize[0];
945 lum2 = lumsrc + src->linesize[0];
950 for(w = width / 2; w--;) {
951 *line1++ = *lum1++; *line2++ = *lum2++;
952 *line1++ = *line2++ = *cb1++;
953 *line1++ = *lum1++; *line2++ = *lum2++;
954 *line1++ = *line2++ = *cr1++;
957 linesrc += dst->linesize[0] * 2;
958 lumsrc += src->linesize[0] * 2;
959 cb2 += src->linesize[1];
960 cr2 += src->linesize[2];
964 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
965 int width, int height)
968 uint8_t *line1, *line2, *linesrc = dst->data[0];
969 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
970 uint8_t *cb1, *cb2 = src->data[1];
971 uint8_t *cr1, *cr2 = src->data[2];
973 for(h = height / 2; h--;) {
975 line2 = linesrc + dst->linesize[0];
978 lum2 = lumsrc + src->linesize[0];
983 for(w = width / 2; w--;) {
984 *line1++ = *line2++ = *cb1++;
985 *line1++ = *lum1++; *line2++ = *lum2++;
986 *line1++ = *line2++ = *cr1++;
987 *line1++ = *lum1++; *line2++ = *lum2++;
990 linesrc += dst->linesize[0] * 2;
991 lumsrc += src->linesize[0] * 2;
992 cb2 += src->linesize[1];
993 cr2 += src->linesize[2];
998 #define ONE_HALF (1 << (SCALEBITS - 1))
999 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1001 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1005 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1006 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1008 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1011 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1013 y = ((y1) - 16) * FIX(255.0/219.0);\
1014 r = cm[(y + r_add) >> SCALEBITS];\
1015 g = cm[(y + g_add) >> SCALEBITS];\
1016 b = cm[(y + b_add) >> SCALEBITS];\
1019 #define YUV_TO_RGB1(cb1, cr1)\
1023 r_add = FIX(1.40200) * cr + ONE_HALF;\
1024 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1025 b_add = FIX(1.77200) * cb + ONE_HALF;\
1028 #define YUV_TO_RGB2(r, g, b, y1)\
1030 y = (y1) << SCALEBITS;\
1031 r = cm[(y + r_add) >> SCALEBITS];\
1032 g = cm[(y + g_add) >> SCALEBITS];\
1033 b = cm[(y + b_add) >> SCALEBITS];\
1036 #define Y_CCIR_TO_JPEG(y)\
1037 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1039 #define Y_JPEG_TO_CCIR(y)\
1040 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1042 #define C_CCIR_TO_JPEG(y)\
1043 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1045 /* NOTE: the clamp is really necessary! */
1046 static inline int C_JPEG_TO_CCIR(int y) {
1047 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
1054 #define RGB_TO_Y(r, g, b) \
1055 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1056 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1058 #define RGB_TO_U(r1, g1, b1, shift)\
1059 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1060 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1062 #define RGB_TO_V(r1, g1, b1, shift)\
1063 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1064 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1066 #define RGB_TO_Y_CCIR(r, g, b) \
1067 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1068 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1070 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1071 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1072 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1074 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1075 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1076 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1078 static uint8_t y_ccir_to_jpeg[256];
1079 static uint8_t y_jpeg_to_ccir[256];
1080 static uint8_t c_ccir_to_jpeg[256];
1081 static uint8_t c_jpeg_to_ccir[256];
1083 /* init various conversion tables */
1084 static void img_convert_init(void)
1087 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1089 for(i = 0;i < 256; i++) {
1090 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1091 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1092 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1093 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1097 /* apply to each pixel the given table */
1098 static void img_apply_table(uint8_t *dst, int dst_wrap,
1099 const uint8_t *src, int src_wrap,
1100 int width, int height, const uint8_t *table1)
1105 const uint8_t *table;
1108 for(;height > 0; height--) {
1132 /* XXX: use generic filter ? */
1133 /* XXX: in most cases, the sampling position is incorrect */
1136 static void shrink41(uint8_t *dst, int dst_wrap,
1137 const uint8_t *src, int src_wrap,
1138 int width, int height)
1144 for(;height > 0; height--) {
1147 for(w = width;w > 0; w--) {
1148 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1158 static void shrink21(uint8_t *dst, int dst_wrap,
1159 const uint8_t *src, int src_wrap,
1160 int width, int height)
1166 for(;height > 0; height--) {
1169 for(w = width;w > 0; w--) {
1170 d[0] = (s[0] + s[1]) >> 1;
1180 static void shrink12(uint8_t *dst, int dst_wrap,
1181 const uint8_t *src, int src_wrap,
1182 int width, int height)
1186 const uint8_t *s1, *s2;
1188 for(;height > 0; height--) {
1192 for(w = width;w >= 4; w-=4) {
1193 d[0] = (s1[0] + s2[0]) >> 1;
1194 d[1] = (s1[1] + s2[1]) >> 1;
1195 d[2] = (s1[2] + s2[2]) >> 1;
1196 d[3] = (s1[3] + s2[3]) >> 1;
1202 d[0] = (s1[0] + s2[0]) >> 1;
1207 src += 2 * src_wrap;
1213 static void shrink22(uint8_t *dst, int dst_wrap,
1214 const uint8_t *src, int src_wrap,
1215 int width, int height)
1218 const uint8_t *s1, *s2;
1221 for(;height > 0; height--) {
1225 for(w = width;w >= 4; w-=4) {
1226 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1227 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1228 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1229 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1235 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1240 src += 2 * src_wrap;
1246 static void shrink44(uint8_t *dst, int dst_wrap,
1247 const uint8_t *src, int src_wrap,
1248 int width, int height)
1251 const uint8_t *s1, *s2, *s3, *s4;
1254 for(;height > 0; height--) {
1260 for(w = width;w > 0; w--) {
1261 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1262 s2[0] + s2[1] + s2[2] + s2[3] +
1263 s3[0] + s3[1] + s3[2] + s3[3] +
1264 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1271 src += 4 * src_wrap;
1276 static void grow21_line(uint8_t *dst, const uint8_t *src,
1285 for(w = width;w >= 4; w-=4) {
1286 d[1] = d[0] = s1[0];
1287 d[3] = d[2] = s1[1];
1291 for(;w >= 2; w -= 2) {
1292 d[1] = d[0] = s1[0];
1296 /* only needed if width is not a multiple of two */
1297 /* XXX: veryfy that */
1303 static void grow41_line(uint8_t *dst, const uint8_t *src,
1312 for(w = width;w >= 4; w-=4) {
1324 static void grow21(uint8_t *dst, int dst_wrap,
1325 const uint8_t *src, int src_wrap,
1326 int width, int height)
1328 for(;height > 0; height--) {
1329 grow21_line(dst, src, width);
1336 static void grow22(uint8_t *dst, int dst_wrap,
1337 const uint8_t *src, int src_wrap,
1338 int width, int height)
1340 for(;height > 0; height--) {
1341 grow21_line(dst, src, width);
1349 static void grow41(uint8_t *dst, int dst_wrap,
1350 const uint8_t *src, int src_wrap,
1351 int width, int height)
1353 for(;height > 0; height--) {
1354 grow41_line(dst, src, width);
1361 static void grow44(uint8_t *dst, int dst_wrap,
1362 const uint8_t *src, int src_wrap,
1363 int width, int height)
1365 for(;height > 0; height--) {
1366 grow41_line(dst, src, width);
1367 if ((height & 3) == 1)
1374 static void conv411(uint8_t *dst, int dst_wrap,
1375 const uint8_t *src, int src_wrap,
1376 int width, int height)
1379 const uint8_t *s1, *s2;
1384 for(;height > 0; height--) {
1386 s2 = src + src_wrap;
1388 for(w = width;w > 0; w--) {
1389 c = (s1[0] + s2[0]) >> 1;
1396 src += src_wrap * 2;
1401 /* XXX: add jpeg quantize code */
1403 #define TRANSP_INDEX (6*6*6)
1405 /* this is maybe slow, but allows for extensions */
1406 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1408 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1411 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1414 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1417 pal = (uint32_t *)palette;
1419 for(r = 0; r < 6; r++) {
1420 for(g = 0; g < 6; g++) {
1421 for(b = 0; b < 6; b++) {
1422 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1423 (pal_value[g] << 8) | pal_value[b];
1430 pal[i++] = 0xff000000;
1433 /* copy bit n to bits 0 ... n - 1 */
1434 static inline unsigned int bitcopy_n(unsigned int a, int n)
1437 mask = (1 << n) - 1;
1438 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1441 /* rgb555 handling */
1443 #define RGB_NAME rgb555
1445 #define RGB_IN(r, g, b, s)\
1447 unsigned int v = ((const uint16_t *)(s))[0];\
1448 r = bitcopy_n(v >> (10 - 3), 3);\
1449 g = bitcopy_n(v >> (5 - 3), 3);\
1450 b = bitcopy_n(v << 3, 3);\
1453 #define RGBA_IN(r, g, b, a, s)\
1455 unsigned int v = ((const uint16_t *)(s))[0];\
1456 r = bitcopy_n(v >> (10 - 3), 3);\
1457 g = bitcopy_n(v >> (5 - 3), 3);\
1458 b = bitcopy_n(v << 3, 3);\
1459 a = (-(v >> 15)) & 0xff;\
1462 #define RGBA_OUT(d, r, g, b, a)\
1464 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1465 ((a << 8) & 0x8000);\
1470 #include "imgconvert_template.h"
1472 /* rgb565 handling */
1474 #define RGB_NAME rgb565
1476 #define RGB_IN(r, g, b, s)\
1478 unsigned int v = ((const uint16_t *)(s))[0];\
1479 r = bitcopy_n(v >> (11 - 3), 3);\
1480 g = bitcopy_n(v >> (5 - 2), 2);\
1481 b = bitcopy_n(v << 3, 3);\
1484 #define RGB_OUT(d, r, g, b)\
1486 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1491 #include "imgconvert_template.h"
1493 /* bgr24 handling */
1495 #define RGB_NAME bgr24
1497 #define RGB_IN(r, g, b, s)\
1504 #define RGB_OUT(d, r, g, b)\
1513 #include "imgconvert_template.h"
1519 /* rgb24 handling */
1521 #define RGB_NAME rgb24
1524 #define RGB_IN(r, g, b, s)\
1531 #define RGB_OUT(d, r, g, b)\
1540 #include "imgconvert_template.h"
1542 /* rgba32 handling */
1544 #define RGB_NAME rgba32
1547 #define RGB_IN(r, g, b, s)\
1549 unsigned int v = ((const uint32_t *)(s))[0];\
1550 r = (v >> 16) & 0xff;\
1551 g = (v >> 8) & 0xff;\
1555 #define RGBA_IN(r, g, b, a, s)\
1557 unsigned int v = ((const uint32_t *)(s))[0];\
1558 a = (v >> 24) & 0xff;\
1559 r = (v >> 16) & 0xff;\
1560 g = (v >> 8) & 0xff;\
1564 #define RGBA_OUT(d, r, g, b, a)\
1566 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1571 #include "imgconvert_template.h"
1573 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1574 int width, int height, int xor_mask)
1576 const unsigned char *p;
1578 int v, dst_wrap, src_wrap;
1582 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1585 dst_wrap = dst->linesize[0] - width;
1586 for(y=0;y<height;y++) {
1589 v = *p++ ^ xor_mask;
1591 q[1] = -((v >> 6) & 1);
1592 q[2] = -((v >> 5) & 1);
1593 q[3] = -((v >> 4) & 1);
1594 q[4] = -((v >> 3) & 1);
1595 q[5] = -((v >> 2) & 1);
1596 q[6] = -((v >> 1) & 1);
1597 q[7] = -((v >> 0) & 1);
1602 v = *p++ ^ xor_mask;
1604 q[0] = -((v >> 7) & 1);
1614 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1615 int width, int height)
1617 mono_to_gray(dst, src, width, height, 0xff);
1620 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1621 int width, int height)
1623 mono_to_gray(dst, src, width, height, 0x00);
1626 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1627 int width, int height, int xor_mask)
1632 int j, b, v, n1, src_wrap, dst_wrap, y;
1635 src_wrap = src->linesize[0] - width;
1638 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1640 for(y=0;y<height;y++) {
1647 v = (v << 1) | (b >> 7);
1649 d[0] = v ^ xor_mask;
1659 v = (v << 1) | (b >> 7);
1662 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1670 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1671 int width, int height)
1673 gray_to_mono(dst, src, width, height, 0xff);
1676 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1677 int width, int height)
1679 gray_to_mono(dst, src, width, height, 0x00);
1682 typedef struct ConvertEntry {
1683 void (*convert)(AVPicture *dst,
1684 const AVPicture *src, int width, int height);
1687 /* Add each new convertion function in this table. In order to be able
1688 to convert from any format to any format, the following constraints
1691 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1693 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1695 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1697 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1700 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1702 The other conversion functions are just optimisations for common cases.
1704 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1705 [PIX_FMT_YUV420P] = {
1706 [PIX_FMT_YUV422] = {
1707 .convert = yuv420p_to_yuv422,
1709 [PIX_FMT_RGB555] = {
1710 .convert = yuv420p_to_rgb555
1712 [PIX_FMT_RGB565] = {
1713 .convert = yuv420p_to_rgb565
1716 .convert = yuv420p_to_bgr24
1719 .convert = yuv420p_to_rgb24
1721 [PIX_FMT_RGBA32] = {
1722 .convert = yuv420p_to_rgba32
1724 [PIX_FMT_UYVY422] = {
1725 .convert = yuv420p_to_uyvy422,
1728 [PIX_FMT_YUV422P] = {
1729 [PIX_FMT_YUV422] = {
1730 .convert = yuv422p_to_yuv422,
1732 [PIX_FMT_UYVY422] = {
1733 .convert = yuv422p_to_uyvy422,
1736 [PIX_FMT_YUV444P] = {
1738 .convert = yuv444p_to_rgb24
1741 [PIX_FMT_YUVJ420P] = {
1742 [PIX_FMT_RGB555] = {
1743 .convert = yuvj420p_to_rgb555
1745 [PIX_FMT_RGB565] = {
1746 .convert = yuvj420p_to_rgb565
1749 .convert = yuvj420p_to_bgr24
1752 .convert = yuvj420p_to_rgb24
1754 [PIX_FMT_RGBA32] = {
1755 .convert = yuvj420p_to_rgba32
1758 [PIX_FMT_YUVJ444P] = {
1760 .convert = yuvj444p_to_rgb24
1763 [PIX_FMT_YUV422] = {
1764 [PIX_FMT_YUV420P] = {
1765 .convert = yuv422_to_yuv420p,
1767 [PIX_FMT_YUV422P] = {
1768 .convert = yuv422_to_yuv422p,
1771 [PIX_FMT_UYVY422] = {
1772 [PIX_FMT_YUV420P] = {
1773 .convert = uyvy422_to_yuv420p,
1775 [PIX_FMT_YUV422P] = {
1776 .convert = uyvy422_to_yuv422p,
1780 [PIX_FMT_YUV420P] = {
1781 .convert = rgb24_to_yuv420p
1783 [PIX_FMT_RGB565] = {
1784 .convert = rgb24_to_rgb565
1786 [PIX_FMT_RGB555] = {
1787 .convert = rgb24_to_rgb555
1789 [PIX_FMT_RGBA32] = {
1790 .convert = rgb24_to_rgba32
1793 .convert = rgb24_to_bgr24
1796 .convert = rgb24_to_gray
1799 .convert = rgb24_to_pal8
1801 [PIX_FMT_YUV444P] = {
1802 .convert = rgb24_to_yuv444p
1804 [PIX_FMT_YUVJ420P] = {
1805 .convert = rgb24_to_yuvj420p
1807 [PIX_FMT_YUVJ444P] = {
1808 .convert = rgb24_to_yuvj444p
1811 [PIX_FMT_RGBA32] = {
1813 .convert = rgba32_to_rgb24
1815 [PIX_FMT_RGB555] = {
1816 .convert = rgba32_to_rgb555
1819 .convert = rgba32_to_pal8
1821 [PIX_FMT_YUV420P] = {
1822 .convert = rgba32_to_yuv420p
1825 .convert = rgba32_to_gray
1830 .convert = bgr24_to_rgb24
1832 [PIX_FMT_YUV420P] = {
1833 .convert = bgr24_to_yuv420p
1836 .convert = bgr24_to_gray
1839 [PIX_FMT_RGB555] = {
1841 .convert = rgb555_to_rgb24
1843 [PIX_FMT_RGBA32] = {
1844 .convert = rgb555_to_rgba32
1846 [PIX_FMT_YUV420P] = {
1847 .convert = rgb555_to_yuv420p
1850 .convert = rgb555_to_gray
1853 [PIX_FMT_RGB565] = {
1855 .convert = rgb565_to_rgb24
1857 [PIX_FMT_YUV420P] = {
1858 .convert = rgb565_to_yuv420p
1861 .convert = rgb565_to_gray
1865 [PIX_FMT_RGB555] = {
1866 .convert = gray_to_rgb555
1868 [PIX_FMT_RGB565] = {
1869 .convert = gray_to_rgb565
1872 .convert = gray_to_rgb24
1875 .convert = gray_to_bgr24
1877 [PIX_FMT_RGBA32] = {
1878 .convert = gray_to_rgba32
1880 [PIX_FMT_MONOWHITE] = {
1881 .convert = gray_to_monowhite
1883 [PIX_FMT_MONOBLACK] = {
1884 .convert = gray_to_monoblack
1887 [PIX_FMT_MONOWHITE] = {
1889 .convert = monowhite_to_gray
1892 [PIX_FMT_MONOBLACK] = {
1894 .convert = monoblack_to_gray
1898 [PIX_FMT_RGB555] = {
1899 .convert = pal8_to_rgb555
1901 [PIX_FMT_RGB565] = {
1902 .convert = pal8_to_rgb565
1905 .convert = pal8_to_bgr24
1908 .convert = pal8_to_rgb24
1910 [PIX_FMT_RGBA32] = {
1911 .convert = pal8_to_rgba32
1914 [PIX_FMT_UYVY411] = {
1915 [PIX_FMT_YUV411P] = {
1916 .convert = uyvy411_to_yuv411p,
1922 int avpicture_alloc(AVPicture *picture,
1923 int pix_fmt, int width, int height)
1928 size = avpicture_get_size(pix_fmt, width, height);
1931 ptr = av_malloc(size);
1934 avpicture_fill(picture, ptr, pix_fmt, width, height);
1937 memset(picture, 0, sizeof(AVPicture));
1941 void avpicture_free(AVPicture *picture)
1943 av_free(picture->data[0]);
1946 /* return true if yuv planar */
1947 static inline int is_yuv_planar(PixFmtInfo *ps)
1949 return (ps->color_type == FF_COLOR_YUV ||
1950 ps->color_type == FF_COLOR_YUV_JPEG) &&
1951 ps->pixel_type == FF_PIXEL_PLANAR;
1954 /* XXX: always use linesize. Return -1 if not supported */
1955 int img_convert(AVPicture *dst, int dst_pix_fmt,
1956 const AVPicture *src, int src_pix_fmt,
1957 int src_width, int src_height)
1960 int i, ret, dst_width, dst_height, int_pix_fmt;
1961 PixFmtInfo *src_pix, *dst_pix;
1963 AVPicture tmp1, *tmp = &tmp1;
1965 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1966 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1968 if (src_width <= 0 || src_height <= 0)
1976 dst_width = src_width;
1977 dst_height = src_height;
1979 dst_pix = &pix_fmt_info[dst_pix_fmt];
1980 src_pix = &pix_fmt_info[src_pix_fmt];
1981 if (src_pix_fmt == dst_pix_fmt) {
1982 /* no conversion needed: just copy */
1983 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
1987 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1989 /* specific conversion routine */
1990 ce->convert(dst, src, dst_width, dst_height);
1995 if (is_yuv_planar(dst_pix) &&
1996 src_pix_fmt == PIX_FMT_GRAY8) {
2000 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2001 img_copy_plane(dst->data[0], dst->linesize[0],
2002 src->data[0], src->linesize[0],
2003 dst_width, dst_height);
2005 img_apply_table(dst->data[0], dst->linesize[0],
2006 src->data[0], src->linesize[0],
2007 dst_width, dst_height,
2010 /* fill U and V with 128 */
2013 w >>= dst_pix->x_chroma_shift;
2014 h >>= dst_pix->y_chroma_shift;
2015 for(i = 1; i <= 2; i++) {
2017 for(y = 0; y< h; y++) {
2019 d += dst->linesize[i];
2026 if (is_yuv_planar(src_pix) &&
2027 dst_pix_fmt == PIX_FMT_GRAY8) {
2028 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2029 img_copy_plane(dst->data[0], dst->linesize[0],
2030 src->data[0], src->linesize[0],
2031 dst_width, dst_height);
2033 img_apply_table(dst->data[0], dst->linesize[0],
2034 src->data[0], src->linesize[0],
2035 dst_width, dst_height,
2041 /* YUV to YUV planar */
2042 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2043 int x_shift, y_shift, w, h, xy_shift;
2044 void (*resize_func)(uint8_t *dst, int dst_wrap,
2045 const uint8_t *src, int src_wrap,
2046 int width, int height);
2048 /* compute chroma size of the smallest dimensions */
2051 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2052 w >>= dst_pix->x_chroma_shift;
2054 w >>= src_pix->x_chroma_shift;
2055 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2056 h >>= dst_pix->y_chroma_shift;
2058 h >>= src_pix->y_chroma_shift;
2060 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2061 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2062 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2063 /* there must be filters for conversion at least from and to
2067 resize_func = img_copy_plane;
2070 resize_func = shrink21;
2073 resize_func = shrink41;
2076 resize_func = shrink12;
2079 resize_func = shrink22;
2082 resize_func = shrink44;
2085 resize_func = grow21;
2088 resize_func = grow41;
2091 resize_func = grow22;
2094 resize_func = grow44;
2097 resize_func = conv411;
2100 /* currently not handled */
2101 goto no_chroma_filter;
2104 img_copy_plane(dst->data[0], dst->linesize[0],
2105 src->data[0], src->linesize[0],
2106 dst_width, dst_height);
2108 for(i = 1;i <= 2; i++)
2109 resize_func(dst->data[i], dst->linesize[i],
2110 src->data[i], src->linesize[i],
2111 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2112 /* if yuv color space conversion is needed, we do it here on
2113 the destination image */
2114 if (dst_pix->color_type != src_pix->color_type) {
2115 const uint8_t *y_table, *c_table;
2116 if (dst_pix->color_type == FF_COLOR_YUV) {
2117 y_table = y_jpeg_to_ccir;
2118 c_table = c_jpeg_to_ccir;
2120 y_table = y_ccir_to_jpeg;
2121 c_table = c_ccir_to_jpeg;
2123 img_apply_table(dst->data[0], dst->linesize[0],
2124 dst->data[0], dst->linesize[0],
2125 dst_width, dst_height,
2128 for(i = 1;i <= 2; i++)
2129 img_apply_table(dst->data[i], dst->linesize[i],
2130 dst->data[i], dst->linesize[i],
2131 dst_width>>dst_pix->x_chroma_shift,
2132 dst_height>>dst_pix->y_chroma_shift,
2139 /* try to use an intermediate format */
2140 if (src_pix_fmt == PIX_FMT_YUV422 ||
2141 dst_pix_fmt == PIX_FMT_YUV422) {
2142 /* specific case: convert to YUV422P first */
2143 int_pix_fmt = PIX_FMT_YUV422P;
2144 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2145 dst_pix_fmt == PIX_FMT_UYVY422) {
2146 /* specific case: convert to YUV422P first */
2147 int_pix_fmt = PIX_FMT_YUV422P;
2148 } else if (src_pix_fmt == PIX_FMT_UYVY411 ||
2149 dst_pix_fmt == PIX_FMT_UYVY411) {
2150 /* specific case: convert to YUV411P first */
2151 int_pix_fmt = PIX_FMT_YUV411P;
2152 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2153 src_pix_fmt != PIX_FMT_GRAY8) ||
2154 (dst_pix->color_type == FF_COLOR_GRAY &&
2155 dst_pix_fmt != PIX_FMT_GRAY8)) {
2156 /* gray8 is the normalized format */
2157 int_pix_fmt = PIX_FMT_GRAY8;
2158 } else if ((is_yuv_planar(src_pix) &&
2159 src_pix_fmt != PIX_FMT_YUV444P &&
2160 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2161 /* yuv444 is the normalized format */
2162 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2163 int_pix_fmt = PIX_FMT_YUVJ444P;
2165 int_pix_fmt = PIX_FMT_YUV444P;
2166 } else if ((is_yuv_planar(dst_pix) &&
2167 dst_pix_fmt != PIX_FMT_YUV444P &&
2168 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2169 /* yuv444 is the normalized format */
2170 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2171 int_pix_fmt = PIX_FMT_YUVJ444P;
2173 int_pix_fmt = PIX_FMT_YUV444P;
2175 /* the two formats are rgb or gray8 or yuv[j]444p */
2176 if (src_pix->is_alpha && dst_pix->is_alpha)
2177 int_pix_fmt = PIX_FMT_RGBA32;
2179 int_pix_fmt = PIX_FMT_RGB24;
2181 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2184 if (img_convert(tmp, int_pix_fmt,
2185 src, src_pix_fmt, src_width, src_height) < 0)
2187 if (img_convert(dst, dst_pix_fmt,
2188 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2192 avpicture_free(tmp);
2196 /* NOTE: we scan all the pixels to have an exact information */
2197 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2199 const unsigned char *p;
2200 int src_wrap, ret, x, y;
2202 uint32_t *palette = (uint32_t *)src->data[1];
2205 src_wrap = src->linesize[0] - width;
2207 for(y=0;y<height;y++) {
2208 for(x=0;x<width;x++) {
2209 a = palette[p[0]] >> 24;
2211 ret |= FF_ALPHA_TRANSP;
2212 } else if (a != 0xff) {
2213 ret |= FF_ALPHA_SEMI_TRANSP;
2223 * Tell if an image really has transparent alpha values.
2224 * @return ored mask of FF_ALPHA_xxx constants
2226 int img_get_alpha_info(const AVPicture *src,
2227 int pix_fmt, int width, int height)
2229 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2232 pf = &pix_fmt_info[pix_fmt];
2233 /* no alpha can be represented in format */
2237 case PIX_FMT_RGBA32:
2238 ret = get_alpha_info_rgba32(src, width, height);
2240 case PIX_FMT_RGB555:
2241 ret = get_alpha_info_rgb555(src, width, height);
2244 ret = get_alpha_info_pal8(src, width, height);
2247 /* we do not know, so everything is indicated */
2248 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2255 #define DEINT_INPLACE_LINE_LUM \
2256 movd_m2r(lum_m4[0],mm0);\
2257 movd_m2r(lum_m3[0],mm1);\
2258 movd_m2r(lum_m2[0],mm2);\
2259 movd_m2r(lum_m1[0],mm3);\
2260 movd_m2r(lum[0],mm4);\
2261 punpcklbw_r2r(mm7,mm0);\
2262 movd_r2m(mm2,lum_m4[0]);\
2263 punpcklbw_r2r(mm7,mm1);\
2264 punpcklbw_r2r(mm7,mm2);\
2265 punpcklbw_r2r(mm7,mm3);\
2266 punpcklbw_r2r(mm7,mm4);\
2267 paddw_r2r(mm3,mm1);\
2269 paddw_r2r(mm4,mm0);\
2271 paddw_r2r(mm6,mm2);\
2272 paddw_r2r(mm2,mm1);\
2273 psubusw_r2r(mm0,mm1);\
2275 packuswb_r2r(mm7,mm1);\
2276 movd_r2m(mm1,lum_m2[0]);
2278 #define DEINT_LINE_LUM \
2279 movd_m2r(lum_m4[0],mm0);\
2280 movd_m2r(lum_m3[0],mm1);\
2281 movd_m2r(lum_m2[0],mm2);\
2282 movd_m2r(lum_m1[0],mm3);\
2283 movd_m2r(lum[0],mm4);\
2284 punpcklbw_r2r(mm7,mm0);\
2285 punpcklbw_r2r(mm7,mm1);\
2286 punpcklbw_r2r(mm7,mm2);\
2287 punpcklbw_r2r(mm7,mm3);\
2288 punpcklbw_r2r(mm7,mm4);\
2289 paddw_r2r(mm3,mm1);\
2291 paddw_r2r(mm4,mm0);\
2293 paddw_r2r(mm6,mm2);\
2294 paddw_r2r(mm2,mm1);\
2295 psubusw_r2r(mm0,mm1);\
2297 packuswb_r2r(mm7,mm1);\
2298 movd_r2m(mm1,dst[0]);
2301 /* filter parameters: [-1 4 2 4 -1] // 8 */
2302 static void deinterlace_line(uint8_t *dst,
2303 const uint8_t *lum_m4, const uint8_t *lum_m3,
2304 const uint8_t *lum_m2, const uint8_t *lum_m1,
2309 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2312 for(;size > 0;size--) {
2314 sum += lum_m3[0] << 2;
2315 sum += lum_m2[0] << 1;
2316 sum += lum_m1[0] << 2;
2318 dst[0] = cm[(sum + 4) >> 3];
2335 movq_m2r(rounder,mm6);
2337 for (;size > 3; size-=4) {
2348 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2352 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2355 for(;size > 0;size--) {
2357 sum += lum_m3[0] << 2;
2358 sum += lum_m2[0] << 1;
2359 lum_m4[0]=lum_m2[0];
2360 sum += lum_m1[0] << 2;
2362 lum_m2[0] = cm[(sum + 4) >> 3];
2378 movq_m2r(rounder,mm6);
2380 for (;size > 3; size-=4) {
2381 DEINT_INPLACE_LINE_LUM
2391 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2392 top field is copied as is, but the bottom field is deinterlaced
2393 against the top field. */
2394 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2395 const uint8_t *src1, int src_wrap,
2396 int width, int height)
2398 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2403 src_0=&src_m1[src_wrap];
2404 src_p1=&src_0[src_wrap];
2405 src_p2=&src_p1[src_wrap];
2406 for(y=0;y<(height-2);y+=2) {
2407 memcpy(dst,src_m1,width);
2409 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2413 src_p1 += 2*src_wrap;
2414 src_p2 += 2*src_wrap;
2417 memcpy(dst,src_m1,width);
2420 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2423 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2424 int width, int height)
2426 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2429 buf = (uint8_t*)av_malloc(width);
2432 memcpy(buf,src_m1,width);
2433 src_0=&src_m1[src_wrap];
2434 src_p1=&src_0[src_wrap];
2435 src_p2=&src_p1[src_wrap];
2436 for(y=0;y<(height-2);y+=2) {
2437 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2440 src_p1 += 2*src_wrap;
2441 src_p2 += 2*src_wrap;
2444 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2449 /* deinterlace - if not supported return -1 */
2450 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2451 int pix_fmt, int width, int height)
2455 if (pix_fmt != PIX_FMT_YUV420P &&
2456 pix_fmt != PIX_FMT_YUV422P &&
2457 pix_fmt != PIX_FMT_YUV444P &&
2458 pix_fmt != PIX_FMT_YUV411P)
2460 if ((width & 3) != 0 || (height & 3) != 0)
2466 case PIX_FMT_YUV420P:
2470 case PIX_FMT_YUV422P:
2473 case PIX_FMT_YUV411P:
2481 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2484 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2485 src->data[i], src->linesize[i],