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] = {
232 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
234 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
235 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
238 const char *avcodec_get_pix_fmt_name(int pix_fmt)
240 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
243 return pix_fmt_info[pix_fmt].name;
246 enum PixelFormat avcodec_get_pix_fmt(const char* name)
250 for (i=0; i < PIX_FMT_NB; i++)
251 if (!strcmp(pix_fmt_info[i].name, name))
256 /* Picture field are filled with 'ptr' addresses. Also return size */
257 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
258 int pix_fmt, int width, int height)
260 int size, w2, h2, size2;
263 pinfo = &pix_fmt_info[pix_fmt];
264 size = width * height;
266 case PIX_FMT_YUV420P:
267 case PIX_FMT_YUV422P:
268 case PIX_FMT_YUV444P:
269 case PIX_FMT_YUV410P:
270 case PIX_FMT_YUV411P:
271 case PIX_FMT_YUVJ420P:
272 case PIX_FMT_YUVJ422P:
273 case PIX_FMT_YUVJ444P:
274 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
275 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
277 picture->data[0] = ptr;
278 picture->data[1] = picture->data[0] + size;
279 picture->data[2] = picture->data[1] + size2;
280 picture->linesize[0] = width;
281 picture->linesize[1] = w2;
282 picture->linesize[2] = w2;
283 return size + 2 * size2;
286 picture->data[0] = ptr;
287 picture->data[1] = NULL;
288 picture->data[2] = NULL;
289 picture->linesize[0] = width * 3;
292 picture->data[0] = ptr;
293 picture->data[1] = NULL;
294 picture->data[2] = NULL;
295 picture->linesize[0] = width * 4;
300 picture->data[0] = ptr;
301 picture->data[1] = NULL;
302 picture->data[2] = NULL;
303 picture->linesize[0] = width * 2;
305 case PIX_FMT_UYVY422:
306 picture->data[0] = ptr;
307 picture->data[1] = NULL;
308 picture->data[2] = NULL;
309 picture->linesize[0] = width * 2;
312 picture->data[0] = ptr;
313 picture->data[1] = NULL;
314 picture->data[2] = NULL;
315 picture->linesize[0] = width;
317 case PIX_FMT_MONOWHITE:
318 case PIX_FMT_MONOBLACK:
319 picture->data[0] = ptr;
320 picture->data[1] = NULL;
321 picture->data[2] = NULL;
322 picture->linesize[0] = (width + 7) >> 3;
323 return picture->linesize[0] * height;
325 size2 = (size + 3) & ~3;
326 picture->data[0] = ptr;
327 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
328 picture->data[2] = NULL;
329 picture->linesize[0] = width;
330 picture->linesize[1] = 4;
331 return size2 + 256 * 4;
333 picture->data[0] = NULL;
334 picture->data[1] = NULL;
335 picture->data[2] = NULL;
336 picture->data[3] = NULL;
341 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
342 unsigned char *dest, int dest_size)
344 PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
345 int i, j, w, h, data_planes;
346 const unsigned char* s;
347 int size = avpicture_get_size(pix_fmt, width, height);
349 if (size > dest_size)
352 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
353 if (pix_fmt == PIX_FMT_YUV422 ||
354 pix_fmt == PIX_FMT_UYVY422 ||
355 pix_fmt == PIX_FMT_RGB565 ||
356 pix_fmt == PIX_FMT_RGB555)
358 else if (pix_fmt == PIX_FMT_PAL8)
361 w = width * (pf->depth * pf->nb_channels / 8);
366 data_planes = pf->nb_channels;
371 for (i=0; i<data_planes; i++) {
373 w = width >> pf->x_chroma_shift;
374 h = height >> pf->y_chroma_shift;
380 s += src->linesize[i];
384 if (pf->pixel_type == FF_PIXEL_PALETTE)
385 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
390 int avpicture_get_size(int pix_fmt, int width, int height)
392 AVPicture dummy_pict;
393 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
397 * compute the loss when converting from a pixel format to another
399 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
402 const PixFmtInfo *pf, *ps;
405 ps = &pix_fmt_info[src_pix_fmt];
406 pf = &pix_fmt_info[dst_pix_fmt];
410 pf = &pix_fmt_info[dst_pix_fmt];
411 if (pf->depth < ps->depth ||
412 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
413 loss |= FF_LOSS_DEPTH;
414 if (pf->x_chroma_shift > ps->x_chroma_shift ||
415 pf->y_chroma_shift > ps->y_chroma_shift)
416 loss |= FF_LOSS_RESOLUTION;
417 switch(pf->color_type) {
419 if (ps->color_type != FF_COLOR_RGB &&
420 ps->color_type != FF_COLOR_GRAY)
421 loss |= FF_LOSS_COLORSPACE;
424 if (ps->color_type != FF_COLOR_GRAY)
425 loss |= FF_LOSS_COLORSPACE;
428 if (ps->color_type != FF_COLOR_YUV)
429 loss |= FF_LOSS_COLORSPACE;
431 case FF_COLOR_YUV_JPEG:
432 if (ps->color_type != FF_COLOR_YUV_JPEG &&
433 ps->color_type != FF_COLOR_YUV &&
434 ps->color_type != FF_COLOR_GRAY)
435 loss |= FF_LOSS_COLORSPACE;
439 if (ps->color_type != pf->color_type)
440 loss |= FF_LOSS_COLORSPACE;
443 if (pf->color_type == FF_COLOR_GRAY &&
444 ps->color_type != FF_COLOR_GRAY)
445 loss |= FF_LOSS_CHROMA;
446 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
447 loss |= FF_LOSS_ALPHA;
448 if (pf->pixel_type == FF_PIXEL_PALETTE &&
449 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
450 loss |= FF_LOSS_COLORQUANT;
454 static int avg_bits_per_pixel(int pix_fmt)
457 const PixFmtInfo *pf;
459 pf = &pix_fmt_info[pix_fmt];
460 switch(pf->pixel_type) {
461 case FF_PIXEL_PACKED:
464 case PIX_FMT_UYVY422:
470 bits = pf->depth * pf->nb_channels;
474 case FF_PIXEL_PLANAR:
475 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
476 bits = pf->depth * pf->nb_channels;
478 bits = pf->depth + ((2 * pf->depth) >>
479 (pf->x_chroma_shift + pf->y_chroma_shift));
482 case FF_PIXEL_PALETTE:
492 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
497 int dist, i, loss, min_dist, dst_pix_fmt;
499 /* find exact color match with smallest size */
501 min_dist = 0x7fffffff;
502 for(i = 0;i < PIX_FMT_NB; i++) {
503 if (pix_fmt_mask & (1 << i)) {
504 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
506 dist = avg_bits_per_pixel(i);
507 if (dist < min_dist) {
518 * find best pixel format to convert to. Return -1 if none found
520 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
521 int has_alpha, int *loss_ptr)
523 int dst_pix_fmt, loss_mask, i;
524 static const int loss_mask_order[] = {
525 ~0, /* no loss first */
528 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
534 /* try with successive loss */
537 loss_mask = loss_mask_order[i++];
538 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
539 has_alpha, loss_mask);
540 if (dst_pix_fmt >= 0)
548 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
552 static void img_copy_plane(uint8_t *dst, int dst_wrap,
553 const uint8_t *src, int src_wrap,
554 int width, int height)
556 for(;height > 0; height--) {
557 memcpy(dst, src, width);
564 * Copy image 'src' to 'dst'.
566 void img_copy(AVPicture *dst, const AVPicture *src,
567 int pix_fmt, int width, int height)
570 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
572 pf = &pix_fmt_info[pix_fmt];
573 switch(pf->pixel_type) {
574 case FF_PIXEL_PACKED:
577 case PIX_FMT_UYVY422:
583 bits = pf->depth * pf->nb_channels;
586 bwidth = (width * bits + 7) >> 3;
587 img_copy_plane(dst->data[0], dst->linesize[0],
588 src->data[0], src->linesize[0],
591 case FF_PIXEL_PLANAR:
592 for(i = 0; i < pf->nb_channels; i++) {
596 if (i == 1 || i == 2) {
597 w >>= pf->x_chroma_shift;
598 h >>= pf->y_chroma_shift;
600 bwidth = (w * pf->depth + 7) >> 3;
601 img_copy_plane(dst->data[i], dst->linesize[i],
602 src->data[i], src->linesize[i],
606 case FF_PIXEL_PALETTE:
607 img_copy_plane(dst->data[0], dst->linesize[0],
608 src->data[0], src->linesize[0],
610 /* copy the palette */
611 img_copy_plane(dst->data[1], dst->linesize[1],
612 src->data[1], src->linesize[1],
618 /* XXX: totally non optimized */
620 static void yuv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
621 int width, int height)
623 const uint8_t *p, *p1;
624 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
632 for(;height >= 1; height -= 2) {
637 for(w = width; w >= 2; w -= 2) {
654 p1 += src->linesize[0];
655 lum1 += dst->linesize[0];
659 for(w = width; w >= 2; w -= 2) {
668 p1 += src->linesize[0];
669 lum1 += dst->linesize[0];
671 cb1 += dst->linesize[1];
672 cr1 += dst->linesize[2];
676 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
677 int width, int height)
679 const uint8_t *p, *p1;
680 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
689 for(;height >= 1; height -= 2) {
694 for(w = width; w >= 2; w -= 2) {
711 p1 += src->linesize[0];
712 lum1 += dst->linesize[0];
716 for(w = width; w >= 2; w -= 2) {
725 p1 += src->linesize[0];
726 lum1 += dst->linesize[0];
728 cb1 += dst->linesize[1];
729 cr1 += dst->linesize[2];
734 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
735 int width, int height)
737 const uint8_t *p, *p1;
738 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
745 for(;height > 0; height--) {
750 for(w = width; w >= 2; w -= 2) {
760 p1 += src->linesize[0];
761 lum1 += dst->linesize[0];
762 cb1 += dst->linesize[1];
763 cr1 += dst->linesize[2];
768 static void yuv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
769 int width, int height)
771 const uint8_t *p, *p1;
772 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
779 for(;height > 0; height--) {
784 for(w = width; w >= 2; w -= 2) {
794 p1 += src->linesize[0];
795 lum1 += dst->linesize[0];
796 cb1 += dst->linesize[1];
797 cr1 += dst->linesize[2];
801 static void yuv422p_to_yuv422(AVPicture *dst, const AVPicture *src,
802 int width, int height)
805 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
812 for(;height > 0; height--) {
817 for(w = width; w >= 2; w -= 2) {
827 p1 += dst->linesize[0];
828 lum1 += src->linesize[0];
829 cb1 += src->linesize[1];
830 cr1 += src->linesize[2];
834 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
835 int width, int height)
838 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
845 for(;height > 0; height--) {
850 for(w = width; w >= 2; w -= 2) {
860 p1 += dst->linesize[0];
861 lum1 += src->linesize[0];
862 cb1 += src->linesize[1];
863 cr1 += src->linesize[2];
870 #define ONE_HALF (1 << (SCALEBITS - 1))
871 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
873 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
877 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
878 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
880 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
883 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
885 y = ((y1) - 16) * FIX(255.0/219.0);\
886 r = cm[(y + r_add) >> SCALEBITS];\
887 g = cm[(y + g_add) >> SCALEBITS];\
888 b = cm[(y + b_add) >> SCALEBITS];\
891 #define YUV_TO_RGB1(cb1, cr1)\
895 r_add = FIX(1.40200) * cr + ONE_HALF;\
896 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
897 b_add = FIX(1.77200) * cb + ONE_HALF;\
900 #define YUV_TO_RGB2(r, g, b, y1)\
902 y = (y1) << SCALEBITS;\
903 r = cm[(y + r_add) >> SCALEBITS];\
904 g = cm[(y + g_add) >> SCALEBITS];\
905 b = cm[(y + b_add) >> SCALEBITS];\
908 #define Y_CCIR_TO_JPEG(y)\
909 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
911 #define Y_JPEG_TO_CCIR(y)\
912 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
914 #define C_CCIR_TO_JPEG(y)\
915 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
917 /* NOTE: the clamp is really necessary! */
918 static inline int C_JPEG_TO_CCIR(int y) {
919 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
926 #define RGB_TO_Y(r, g, b) \
927 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
928 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
930 #define RGB_TO_U(r1, g1, b1, shift)\
931 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
932 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
934 #define RGB_TO_V(r1, g1, b1, shift)\
935 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
936 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
938 #define RGB_TO_Y_CCIR(r, g, b) \
939 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
940 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
942 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
943 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
944 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
946 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
947 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
948 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
950 static uint8_t y_ccir_to_jpeg[256];
951 static uint8_t y_jpeg_to_ccir[256];
952 static uint8_t c_ccir_to_jpeg[256];
953 static uint8_t c_jpeg_to_ccir[256];
955 /* init various conversion tables */
956 static void img_convert_init(void)
959 uint8_t *cm = cropTbl + MAX_NEG_CROP;
961 for(i = 0;i < 256; i++) {
962 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
963 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
964 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
965 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
969 /* apply to each pixel the given table */
970 static void img_apply_table(uint8_t *dst, int dst_wrap,
971 const uint8_t *src, int src_wrap,
972 int width, int height, const uint8_t *table1)
977 const uint8_t *table;
980 for(;height > 0; height--) {
1004 /* XXX: use generic filter ? */
1005 /* XXX: in most cases, the sampling position is incorrect */
1008 static void shrink41(uint8_t *dst, int dst_wrap,
1009 const uint8_t *src, int src_wrap,
1010 int width, int height)
1016 for(;height > 0; height--) {
1019 for(w = width;w > 0; w--) {
1020 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1030 static void shrink21(uint8_t *dst, int dst_wrap,
1031 const uint8_t *src, int src_wrap,
1032 int width, int height)
1038 for(;height > 0; height--) {
1041 for(w = width;w > 0; w--) {
1042 d[0] = (s[0] + s[1]) >> 1;
1052 static void shrink12(uint8_t *dst, int dst_wrap,
1053 const uint8_t *src, int src_wrap,
1054 int width, int height)
1058 const uint8_t *s1, *s2;
1060 for(;height > 0; height--) {
1064 for(w = width;w >= 4; w-=4) {
1065 d[0] = (s1[0] + s2[0]) >> 1;
1066 d[1] = (s1[1] + s2[1]) >> 1;
1067 d[2] = (s1[2] + s2[2]) >> 1;
1068 d[3] = (s1[3] + s2[3]) >> 1;
1074 d[0] = (s1[0] + s2[0]) >> 1;
1079 src += 2 * src_wrap;
1085 static void shrink22(uint8_t *dst, int dst_wrap,
1086 const uint8_t *src, int src_wrap,
1087 int width, int height)
1090 const uint8_t *s1, *s2;
1093 for(;height > 0; height--) {
1097 for(w = width;w >= 4; w-=4) {
1098 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1099 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1100 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1101 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1107 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1112 src += 2 * src_wrap;
1118 static void shrink44(uint8_t *dst, int dst_wrap,
1119 const uint8_t *src, int src_wrap,
1120 int width, int height)
1123 const uint8_t *s1, *s2, *s3, *s4;
1126 for(;height > 0; height--) {
1132 for(w = width;w > 0; w--) {
1133 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1134 s2[0] + s2[1] + s2[2] + s2[3] +
1135 s3[0] + s3[1] + s3[2] + s3[3] +
1136 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1143 src += 4 * src_wrap;
1148 static void grow21_line(uint8_t *dst, const uint8_t *src,
1157 for(w = width;w >= 4; w-=4) {
1158 d[1] = d[0] = s1[0];
1159 d[3] = d[2] = s1[1];
1163 for(;w >= 2; w -= 2) {
1164 d[1] = d[0] = s1[0];
1168 /* only needed if width is not a multiple of two */
1169 /* XXX: veryfy that */
1175 static void grow41_line(uint8_t *dst, const uint8_t *src,
1184 for(w = width;w >= 4; w-=4) {
1196 static void grow21(uint8_t *dst, int dst_wrap,
1197 const uint8_t *src, int src_wrap,
1198 int width, int height)
1200 for(;height > 0; height--) {
1201 grow21_line(dst, src, width);
1208 static void grow22(uint8_t *dst, int dst_wrap,
1209 const uint8_t *src, int src_wrap,
1210 int width, int height)
1212 for(;height > 0; height--) {
1213 grow21_line(dst, src, width);
1221 static void grow41(uint8_t *dst, int dst_wrap,
1222 const uint8_t *src, int src_wrap,
1223 int width, int height)
1225 for(;height > 0; height--) {
1226 grow41_line(dst, src, width);
1233 static void grow44(uint8_t *dst, int dst_wrap,
1234 const uint8_t *src, int src_wrap,
1235 int width, int height)
1237 for(;height > 0; height--) {
1238 grow41_line(dst, src, width);
1239 if ((height & 3) == 1)
1246 static void conv411(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;
1256 for(;height > 0; height--) {
1258 s2 = src + src_wrap;
1260 for(w = width;w > 0; w--) {
1261 c = (s1[0] + s2[0]) >> 1;
1268 src += src_wrap * 2;
1273 /* XXX: add jpeg quantize code */
1275 #define TRANSP_INDEX (6*6*6)
1277 /* this is maybe slow, but allows for extensions */
1278 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1280 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1283 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1286 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1289 pal = (uint32_t *)palette;
1291 for(r = 0; r < 6; r++) {
1292 for(g = 0; g < 6; g++) {
1293 for(b = 0; b < 6; b++) {
1294 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1295 (pal_value[g] << 8) | pal_value[b];
1302 pal[i++] = 0xff000000;
1305 /* copy bit n to bits 0 ... n - 1 */
1306 static inline unsigned int bitcopy_n(unsigned int a, int n)
1309 mask = (1 << n) - 1;
1310 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1313 /* rgb555 handling */
1315 #define RGB_NAME rgb555
1317 #define RGB_IN(r, g, b, s)\
1319 unsigned int v = ((const uint16_t *)(s))[0];\
1320 r = bitcopy_n(v >> (10 - 3), 3);\
1321 g = bitcopy_n(v >> (5 - 3), 3);\
1322 b = bitcopy_n(v << 3, 3);\
1325 #define RGBA_IN(r, g, b, a, s)\
1327 unsigned int v = ((const uint16_t *)(s))[0];\
1328 r = bitcopy_n(v >> (10 - 3), 3);\
1329 g = bitcopy_n(v >> (5 - 3), 3);\
1330 b = bitcopy_n(v << 3, 3);\
1331 a = (-(v >> 15)) & 0xff;\
1334 #define RGBA_OUT(d, r, g, b, a)\
1336 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1337 ((a << 8) & 0x8000);\
1342 #include "imgconvert_template.h"
1344 /* rgb565 handling */
1346 #define RGB_NAME rgb565
1348 #define RGB_IN(r, g, b, s)\
1350 unsigned int v = ((const uint16_t *)(s))[0];\
1351 r = bitcopy_n(v >> (11 - 3), 3);\
1352 g = bitcopy_n(v >> (5 - 2), 2);\
1353 b = bitcopy_n(v << 3, 3);\
1356 #define RGB_OUT(d, r, g, b)\
1358 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1363 #include "imgconvert_template.h"
1365 /* bgr24 handling */
1367 #define RGB_NAME bgr24
1369 #define RGB_IN(r, g, b, s)\
1376 #define RGB_OUT(d, r, g, b)\
1385 #include "imgconvert_template.h"
1391 /* rgb24 handling */
1393 #define RGB_NAME rgb24
1396 #define RGB_IN(r, g, b, s)\
1403 #define RGB_OUT(d, r, g, b)\
1412 #include "imgconvert_template.h"
1414 /* rgba32 handling */
1416 #define RGB_NAME rgba32
1419 #define RGB_IN(r, g, b, s)\
1421 unsigned int v = ((const uint32_t *)(s))[0];\
1422 r = (v >> 16) & 0xff;\
1423 g = (v >> 8) & 0xff;\
1427 #define RGBA_IN(r, g, b, a, s)\
1429 unsigned int v = ((const uint32_t *)(s))[0];\
1430 a = (v >> 24) & 0xff;\
1431 r = (v >> 16) & 0xff;\
1432 g = (v >> 8) & 0xff;\
1436 #define RGBA_OUT(d, r, g, b, a)\
1438 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1443 #include "imgconvert_template.h"
1445 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1446 int width, int height, int xor_mask)
1448 const unsigned char *p;
1450 int v, dst_wrap, src_wrap;
1454 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1457 dst_wrap = dst->linesize[0] - width;
1458 for(y=0;y<height;y++) {
1461 v = *p++ ^ xor_mask;
1463 q[1] = -((v >> 6) & 1);
1464 q[2] = -((v >> 5) & 1);
1465 q[3] = -((v >> 4) & 1);
1466 q[4] = -((v >> 3) & 1);
1467 q[5] = -((v >> 2) & 1);
1468 q[6] = -((v >> 1) & 1);
1469 q[7] = -((v >> 0) & 1);
1474 v = *p++ ^ xor_mask;
1476 q[0] = -((v >> 7) & 1);
1486 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1487 int width, int height)
1489 mono_to_gray(dst, src, width, height, 0xff);
1492 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1493 int width, int height)
1495 mono_to_gray(dst, src, width, height, 0x00);
1498 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1499 int width, int height, int xor_mask)
1504 int j, b, v, n1, src_wrap, dst_wrap, y;
1507 src_wrap = src->linesize[0] - width;
1510 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1512 for(y=0;y<height;y++) {
1519 v = (v << 1) | (b >> 7);
1521 d[0] = v ^ xor_mask;
1531 v = (v << 1) | (b >> 7);
1534 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1542 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1543 int width, int height)
1545 gray_to_mono(dst, src, width, height, 0xff);
1548 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1549 int width, int height)
1551 gray_to_mono(dst, src, width, height, 0x00);
1554 typedef struct ConvertEntry {
1555 void (*convert)(AVPicture *dst,
1556 const AVPicture *src, int width, int height);
1559 /* Add each new convertion function in this table. In order to be able
1560 to convert from any format to any format, the following constraints
1563 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1565 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1567 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1569 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1572 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1574 The other conversion functions are just optimisations for common cases.
1576 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1577 [PIX_FMT_YUV420P] = {
1578 [PIX_FMT_RGB555] = {
1579 .convert = yuv420p_to_rgb555
1581 [PIX_FMT_RGB565] = {
1582 .convert = yuv420p_to_rgb565
1585 .convert = yuv420p_to_bgr24
1588 .convert = yuv420p_to_rgb24
1590 [PIX_FMT_RGBA32] = {
1591 .convert = yuv420p_to_rgba32
1594 [PIX_FMT_YUV422P] = {
1595 [PIX_FMT_YUV422] = {
1596 .convert = yuv422p_to_yuv422,
1598 [PIX_FMT_UYVY422] = {
1599 .convert = yuv422p_to_uyvy422,
1602 [PIX_FMT_YUV444P] = {
1604 .convert = yuv444p_to_rgb24
1607 [PIX_FMT_YUVJ420P] = {
1608 [PIX_FMT_RGB555] = {
1609 .convert = yuvj420p_to_rgb555
1611 [PIX_FMT_RGB565] = {
1612 .convert = yuvj420p_to_rgb565
1615 .convert = yuvj420p_to_bgr24
1618 .convert = yuvj420p_to_rgb24
1620 [PIX_FMT_RGBA32] = {
1621 .convert = yuvj420p_to_rgba32
1624 [PIX_FMT_YUVJ444P] = {
1626 .convert = yuvj444p_to_rgb24
1629 [PIX_FMT_YUV422] = {
1630 [PIX_FMT_YUV420P] = {
1631 .convert = yuv422_to_yuv420p,
1633 [PIX_FMT_YUV422P] = {
1634 .convert = yuv422_to_yuv422p,
1637 [PIX_FMT_UYVY422] = {
1638 [PIX_FMT_YUV420P] = {
1639 .convert = uyvy422_to_yuv420p,
1641 [PIX_FMT_YUV422P] = {
1642 .convert = uyvy422_to_yuv422p,
1646 [PIX_FMT_YUV420P] = {
1647 .convert = rgb24_to_yuv420p
1649 [PIX_FMT_RGB565] = {
1650 .convert = rgb24_to_rgb565
1652 [PIX_FMT_RGB555] = {
1653 .convert = rgb24_to_rgb555
1655 [PIX_FMT_RGBA32] = {
1656 .convert = rgb24_to_rgba32
1659 .convert = rgb24_to_bgr24
1662 .convert = rgb24_to_gray
1665 .convert = rgb24_to_pal8
1667 [PIX_FMT_YUV444P] = {
1668 .convert = rgb24_to_yuv444p
1670 [PIX_FMT_YUVJ420P] = {
1671 .convert = rgb24_to_yuvj420p
1673 [PIX_FMT_YUVJ444P] = {
1674 .convert = rgb24_to_yuvj444p
1677 [PIX_FMT_RGBA32] = {
1679 .convert = rgba32_to_rgb24
1681 [PIX_FMT_RGB555] = {
1682 .convert = rgba32_to_rgb555
1685 .convert = rgba32_to_pal8
1687 [PIX_FMT_YUV420P] = {
1688 .convert = rgba32_to_yuv420p
1691 .convert = rgba32_to_gray
1696 .convert = bgr24_to_rgb24
1698 [PIX_FMT_YUV420P] = {
1699 .convert = bgr24_to_yuv420p
1702 .convert = bgr24_to_gray
1705 [PIX_FMT_RGB555] = {
1707 .convert = rgb555_to_rgb24
1709 [PIX_FMT_RGBA32] = {
1710 .convert = rgb555_to_rgba32
1712 [PIX_FMT_YUV420P] = {
1713 .convert = rgb555_to_yuv420p
1716 .convert = rgb555_to_gray
1719 [PIX_FMT_RGB565] = {
1721 .convert = rgb565_to_rgb24
1723 [PIX_FMT_YUV420P] = {
1724 .convert = rgb565_to_yuv420p
1727 .convert = rgb565_to_gray
1731 [PIX_FMT_RGB555] = {
1732 .convert = gray_to_rgb555
1734 [PIX_FMT_RGB565] = {
1735 .convert = gray_to_rgb565
1738 .convert = gray_to_rgb24
1741 .convert = gray_to_bgr24
1743 [PIX_FMT_RGBA32] = {
1744 .convert = gray_to_rgba32
1746 [PIX_FMT_MONOWHITE] = {
1747 .convert = gray_to_monowhite
1749 [PIX_FMT_MONOBLACK] = {
1750 .convert = gray_to_monoblack
1753 [PIX_FMT_MONOWHITE] = {
1755 .convert = monowhite_to_gray
1758 [PIX_FMT_MONOBLACK] = {
1760 .convert = monoblack_to_gray
1764 [PIX_FMT_RGB555] = {
1765 .convert = pal8_to_rgb555
1767 [PIX_FMT_RGB565] = {
1768 .convert = pal8_to_rgb565
1771 .convert = pal8_to_bgr24
1774 .convert = pal8_to_rgb24
1776 [PIX_FMT_RGBA32] = {
1777 .convert = pal8_to_rgba32
1782 int avpicture_alloc(AVPicture *picture,
1783 int pix_fmt, int width, int height)
1788 size = avpicture_get_size(pix_fmt, width, height);
1789 ptr = av_malloc(size);
1792 avpicture_fill(picture, ptr, pix_fmt, width, height);
1795 memset(picture, 0, sizeof(AVPicture));
1799 void avpicture_free(AVPicture *picture)
1801 av_free(picture->data[0]);
1804 /* return true if yuv planar */
1805 static inline int is_yuv_planar(PixFmtInfo *ps)
1807 return (ps->color_type == FF_COLOR_YUV ||
1808 ps->color_type == FF_COLOR_YUV_JPEG) &&
1809 ps->pixel_type == FF_PIXEL_PLANAR;
1812 /* XXX: always use linesize. Return -1 if not supported */
1813 int img_convert(AVPicture *dst, int dst_pix_fmt,
1814 const AVPicture *src, int src_pix_fmt,
1815 int src_width, int src_height)
1818 int i, ret, dst_width, dst_height, int_pix_fmt;
1819 PixFmtInfo *src_pix, *dst_pix;
1821 AVPicture tmp1, *tmp = &tmp1;
1823 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1824 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1826 if (src_width <= 0 || src_height <= 0)
1834 dst_width = src_width;
1835 dst_height = src_height;
1837 dst_pix = &pix_fmt_info[dst_pix_fmt];
1838 src_pix = &pix_fmt_info[src_pix_fmt];
1839 if (src_pix_fmt == dst_pix_fmt) {
1840 /* no conversion needed: just copy */
1841 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
1845 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1847 /* specific conversion routine */
1848 ce->convert(dst, src, dst_width, dst_height);
1853 if (is_yuv_planar(dst_pix) &&
1854 src_pix_fmt == PIX_FMT_GRAY8) {
1858 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
1859 img_copy_plane(dst->data[0], dst->linesize[0],
1860 src->data[0], src->linesize[0],
1861 dst_width, dst_height);
1863 img_apply_table(dst->data[0], dst->linesize[0],
1864 src->data[0], src->linesize[0],
1865 dst_width, dst_height,
1868 /* fill U and V with 128 */
1871 w >>= dst_pix->x_chroma_shift;
1872 h >>= dst_pix->y_chroma_shift;
1873 for(i = 1; i <= 2; i++) {
1875 for(y = 0; y< h; y++) {
1877 d += dst->linesize[i];
1884 if (is_yuv_planar(src_pix) &&
1885 dst_pix_fmt == PIX_FMT_GRAY8) {
1886 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
1887 img_copy_plane(dst->data[0], dst->linesize[0],
1888 src->data[0], src->linesize[0],
1889 dst_width, dst_height);
1891 img_apply_table(dst->data[0], dst->linesize[0],
1892 src->data[0], src->linesize[0],
1893 dst_width, dst_height,
1899 /* YUV to YUV planar */
1900 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
1901 int x_shift, y_shift, w, h, xy_shift;
1902 void (*resize_func)(uint8_t *dst, int dst_wrap,
1903 const uint8_t *src, int src_wrap,
1904 int width, int height);
1906 /* compute chroma size of the smallest dimensions */
1909 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1910 w >>= dst_pix->x_chroma_shift;
1912 w >>= src_pix->x_chroma_shift;
1913 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1914 h >>= dst_pix->y_chroma_shift;
1916 h >>= src_pix->y_chroma_shift;
1918 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1919 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
1920 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
1921 /* there must be filters for conversion at least from and to
1925 resize_func = img_copy_plane;
1928 resize_func = shrink21;
1931 resize_func = shrink41;
1934 resize_func = shrink12;
1937 resize_func = shrink22;
1940 resize_func = shrink44;
1943 resize_func = grow21;
1946 resize_func = grow41;
1949 resize_func = grow22;
1952 resize_func = grow44;
1955 resize_func = conv411;
1958 /* currently not handled */
1959 goto no_chroma_filter;
1962 img_copy_plane(dst->data[0], dst->linesize[0],
1963 src->data[0], src->linesize[0],
1964 dst_width, dst_height);
1966 for(i = 1;i <= 2; i++)
1967 resize_func(dst->data[i], dst->linesize[i],
1968 src->data[i], src->linesize[i],
1969 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
1970 /* if yuv color space conversion is needed, we do it here on
1971 the destination image */
1972 if (dst_pix->color_type != src_pix->color_type) {
1973 const uint8_t *y_table, *c_table;
1974 if (dst_pix->color_type == FF_COLOR_YUV) {
1975 y_table = y_jpeg_to_ccir;
1976 c_table = c_jpeg_to_ccir;
1978 y_table = y_ccir_to_jpeg;
1979 c_table = c_ccir_to_jpeg;
1981 img_apply_table(dst->data[0], dst->linesize[0],
1982 dst->data[0], dst->linesize[0],
1983 dst_width, dst_height,
1986 for(i = 1;i <= 2; i++)
1987 img_apply_table(dst->data[i], dst->linesize[i],
1988 dst->data[i], dst->linesize[i],
1989 dst_width>>dst_pix->x_chroma_shift,
1990 dst_height>>dst_pix->y_chroma_shift,
1997 /* try to use an intermediate format */
1998 if (src_pix_fmt == PIX_FMT_YUV422 ||
1999 dst_pix_fmt == PIX_FMT_YUV422) {
2000 /* specific case: convert to YUV422P first */
2001 int_pix_fmt = PIX_FMT_YUV422P;
2002 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2003 dst_pix_fmt == PIX_FMT_UYVY422) {
2004 /* specific case: convert to YUV422P first */
2005 int_pix_fmt = PIX_FMT_YUV422P;
2006 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2007 src_pix_fmt != PIX_FMT_GRAY8) ||
2008 (dst_pix->color_type == FF_COLOR_GRAY &&
2009 dst_pix_fmt != PIX_FMT_GRAY8)) {
2010 /* gray8 is the normalized format */
2011 int_pix_fmt = PIX_FMT_GRAY8;
2012 } else if ((is_yuv_planar(src_pix) &&
2013 src_pix_fmt != PIX_FMT_YUV444P &&
2014 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2015 /* yuv444 is the normalized format */
2016 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2017 int_pix_fmt = PIX_FMT_YUVJ444P;
2019 int_pix_fmt = PIX_FMT_YUV444P;
2020 } else if ((is_yuv_planar(dst_pix) &&
2021 dst_pix_fmt != PIX_FMT_YUV444P &&
2022 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2023 /* yuv444 is the normalized format */
2024 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2025 int_pix_fmt = PIX_FMT_YUVJ444P;
2027 int_pix_fmt = PIX_FMT_YUV444P;
2029 /* the two formats are rgb or gray8 or yuv[j]444p */
2030 if (src_pix->is_alpha && dst_pix->is_alpha)
2031 int_pix_fmt = PIX_FMT_RGBA32;
2033 int_pix_fmt = PIX_FMT_RGB24;
2035 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2038 if (img_convert(tmp, int_pix_fmt,
2039 src, src_pix_fmt, src_width, src_height) < 0)
2041 if (img_convert(dst, dst_pix_fmt,
2042 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2046 avpicture_free(tmp);
2050 /* NOTE: we scan all the pixels to have an exact information */
2051 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2053 const unsigned char *p;
2054 int src_wrap, ret, x, y;
2056 uint32_t *palette = (uint32_t *)src->data[1];
2059 src_wrap = src->linesize[0] - width;
2061 for(y=0;y<height;y++) {
2062 for(x=0;x<width;x++) {
2063 a = palette[p[0]] >> 24;
2065 ret |= FF_ALPHA_TRANSP;
2066 } else if (a != 0xff) {
2067 ret |= FF_ALPHA_SEMI_TRANSP;
2077 * Tell if an image really has transparent alpha values.
2078 * @return ored mask of FF_ALPHA_xxx constants
2080 int img_get_alpha_info(const AVPicture *src,
2081 int pix_fmt, int width, int height)
2083 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2086 pf = &pix_fmt_info[pix_fmt];
2087 /* no alpha can be represented in format */
2091 case PIX_FMT_RGBA32:
2092 ret = get_alpha_info_rgba32(src, width, height);
2094 case PIX_FMT_RGB555:
2095 ret = get_alpha_info_rgb555(src, width, height);
2098 ret = get_alpha_info_pal8(src, width, height);
2101 /* we do not know, so everything is indicated */
2102 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2109 #define DEINT_INPLACE_LINE_LUM \
2110 movd_m2r(lum_m4[0],mm0);\
2111 movd_m2r(lum_m3[0],mm1);\
2112 movd_m2r(lum_m2[0],mm2);\
2113 movd_m2r(lum_m1[0],mm3);\
2114 movd_m2r(lum[0],mm4);\
2115 punpcklbw_r2r(mm7,mm0);\
2116 movd_r2m(mm2,lum_m4[0]);\
2117 punpcklbw_r2r(mm7,mm1);\
2118 punpcklbw_r2r(mm7,mm2);\
2119 punpcklbw_r2r(mm7,mm3);\
2120 punpcklbw_r2r(mm7,mm4);\
2121 paddw_r2r(mm3,mm1);\
2123 paddw_r2r(mm4,mm0);\
2125 paddw_r2r(mm6,mm2);\
2126 paddw_r2r(mm2,mm1);\
2127 psubusw_r2r(mm0,mm1);\
2129 packuswb_r2r(mm7,mm1);\
2130 movd_r2m(mm1,lum_m2[0]);
2132 #define DEINT_LINE_LUM \
2133 movd_m2r(lum_m4[0],mm0);\
2134 movd_m2r(lum_m3[0],mm1);\
2135 movd_m2r(lum_m2[0],mm2);\
2136 movd_m2r(lum_m1[0],mm3);\
2137 movd_m2r(lum[0],mm4);\
2138 punpcklbw_r2r(mm7,mm0);\
2139 punpcklbw_r2r(mm7,mm1);\
2140 punpcklbw_r2r(mm7,mm2);\
2141 punpcklbw_r2r(mm7,mm3);\
2142 punpcklbw_r2r(mm7,mm4);\
2143 paddw_r2r(mm3,mm1);\
2145 paddw_r2r(mm4,mm0);\
2147 paddw_r2r(mm6,mm2);\
2148 paddw_r2r(mm2,mm1);\
2149 psubusw_r2r(mm0,mm1);\
2151 packuswb_r2r(mm7,mm1);\
2152 movd_r2m(mm1,dst[0]);
2155 /* filter parameters: [-1 4 2 4 -1] // 8 */
2156 static void deinterlace_line(uint8_t *dst,
2157 const uint8_t *lum_m4, const uint8_t *lum_m3,
2158 const uint8_t *lum_m2, const uint8_t *lum_m1,
2163 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2166 for(;size > 0;size--) {
2168 sum += lum_m3[0] << 2;
2169 sum += lum_m2[0] << 1;
2170 sum += lum_m1[0] << 2;
2172 dst[0] = cm[(sum + 4) >> 3];
2189 movq_m2r(rounder,mm6);
2191 for (;size > 3; size-=4) {
2202 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2206 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2209 for(;size > 0;size--) {
2211 sum += lum_m3[0] << 2;
2212 sum += lum_m2[0] << 1;
2213 lum_m4[0]=lum_m2[0];
2214 sum += lum_m1[0] << 2;
2216 lum_m2[0] = cm[(sum + 4) >> 3];
2232 movq_m2r(rounder,mm6);
2234 for (;size > 3; size-=4) {
2235 DEINT_INPLACE_LINE_LUM
2245 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2246 top field is copied as is, but the bottom field is deinterlaced
2247 against the top field. */
2248 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2249 const uint8_t *src1, int src_wrap,
2250 int width, int height)
2252 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2257 src_0=&src_m1[src_wrap];
2258 src_p1=&src_0[src_wrap];
2259 src_p2=&src_p1[src_wrap];
2260 for(y=0;y<(height-2);y+=2) {
2261 memcpy(dst,src_m1,width);
2263 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2267 src_p1 += 2*src_wrap;
2268 src_p2 += 2*src_wrap;
2271 memcpy(dst,src_m1,width);
2274 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2277 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2278 int width, int height)
2280 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2283 buf = (uint8_t*)av_malloc(width);
2286 memcpy(buf,src_m1,width);
2287 src_0=&src_m1[src_wrap];
2288 src_p1=&src_0[src_wrap];
2289 src_p2=&src_p1[src_wrap];
2290 for(y=0;y<(height-2);y+=2) {
2291 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2294 src_p1 += 2*src_wrap;
2295 src_p2 += 2*src_wrap;
2298 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2303 /* deinterlace - if not supported return -1 */
2304 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2305 int pix_fmt, int width, int height)
2309 if (pix_fmt != PIX_FMT_YUV420P &&
2310 pix_fmt != PIX_FMT_YUV422P &&
2311 pix_fmt != PIX_FMT_YUV444P &&
2312 pix_fmt != PIX_FMT_YUV411P)
2314 if ((width & 3) != 0 || (height & 3) != 0)
2320 case PIX_FMT_YUV420P:
2324 case PIX_FMT_YUV422P:
2327 case PIX_FMT_YUV411P:
2335 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2338 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2339 src->data[i], src->linesize[i],