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_YUV410P] = {
103 .color_type = FF_COLOR_YUV,
104 .pixel_type = FF_PIXEL_PLANAR,
106 .x_chroma_shift = 2, .y_chroma_shift = 2,
108 [PIX_FMT_YUV411P] = {
111 .color_type = FF_COLOR_YUV,
112 .pixel_type = FF_PIXEL_PLANAR,
114 .x_chroma_shift = 2, .y_chroma_shift = 0,
118 [PIX_FMT_YUVJ420P] = {
121 .color_type = FF_COLOR_YUV_JPEG,
122 .pixel_type = FF_PIXEL_PLANAR,
124 .x_chroma_shift = 1, .y_chroma_shift = 1,
126 [PIX_FMT_YUVJ422P] = {
129 .color_type = FF_COLOR_YUV_JPEG,
130 .pixel_type = FF_PIXEL_PLANAR,
132 .x_chroma_shift = 1, .y_chroma_shift = 0,
134 [PIX_FMT_YUVJ444P] = {
137 .color_type = FF_COLOR_YUV_JPEG,
138 .pixel_type = FF_PIXEL_PLANAR,
140 .x_chroma_shift = 0, .y_chroma_shift = 0,
147 .color_type = FF_COLOR_RGB,
148 .pixel_type = FF_PIXEL_PACKED,
150 .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,
162 .nb_channels = 4, .is_alpha = 1,
163 .color_type = FF_COLOR_RGB,
164 .pixel_type = FF_PIXEL_PACKED,
166 .x_chroma_shift = 0, .y_chroma_shift = 0,
171 .color_type = FF_COLOR_RGB,
172 .pixel_type = FF_PIXEL_PACKED,
174 .x_chroma_shift = 0, .y_chroma_shift = 0,
178 .nb_channels = 4, .is_alpha = 1,
179 .color_type = FF_COLOR_RGB,
180 .pixel_type = FF_PIXEL_PACKED,
182 .x_chroma_shift = 0, .y_chroma_shift = 0,
185 /* gray / mono formats */
189 .color_type = FF_COLOR_GRAY,
190 .pixel_type = FF_PIXEL_PLANAR,
193 [PIX_FMT_MONOWHITE] = {
196 .color_type = FF_COLOR_GRAY,
197 .pixel_type = FF_PIXEL_PLANAR,
200 [PIX_FMT_MONOBLACK] = {
203 .color_type = FF_COLOR_GRAY,
204 .pixel_type = FF_PIXEL_PLANAR,
208 /* paletted formats */
211 .nb_channels = 4, .is_alpha = 1,
212 .color_type = FF_COLOR_RGB,
213 .pixel_type = FF_PIXEL_PALETTE,
218 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
220 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
221 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
224 const char *avcodec_get_pix_fmt_name(int pix_fmt)
226 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
229 return pix_fmt_info[pix_fmt].name;
232 enum PixelFormat avcodec_get_pix_fmt(const char* name)
236 for (i=0; i < PIX_FMT_NB; i++)
237 if (!strcmp(pix_fmt_info[i].name, name))
242 /* Picture field are filled with 'ptr' addresses. Also return size */
243 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
244 int pix_fmt, int width, int height)
246 int size, w2, h2, size2;
249 pinfo = &pix_fmt_info[pix_fmt];
250 size = width * height;
252 case PIX_FMT_YUV420P:
253 case PIX_FMT_YUV422P:
254 case PIX_FMT_YUV444P:
255 case PIX_FMT_YUV410P:
256 case PIX_FMT_YUV411P:
257 case PIX_FMT_YUVJ420P:
258 case PIX_FMT_YUVJ422P:
259 case PIX_FMT_YUVJ444P:
260 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
261 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
263 picture->data[0] = ptr;
264 picture->data[1] = picture->data[0] + size;
265 picture->data[2] = picture->data[1] + size2;
266 picture->linesize[0] = width;
267 picture->linesize[1] = w2;
268 picture->linesize[2] = w2;
269 return size + 2 * size2;
272 picture->data[0] = ptr;
273 picture->data[1] = NULL;
274 picture->data[2] = NULL;
275 picture->linesize[0] = width * 3;
278 picture->data[0] = ptr;
279 picture->data[1] = NULL;
280 picture->data[2] = NULL;
281 picture->linesize[0] = width * 4;
286 picture->data[0] = ptr;
287 picture->data[1] = NULL;
288 picture->data[2] = NULL;
289 picture->linesize[0] = width * 2;
292 picture->data[0] = ptr;
293 picture->data[1] = NULL;
294 picture->data[2] = NULL;
295 picture->linesize[0] = width;
297 case PIX_FMT_MONOWHITE:
298 case PIX_FMT_MONOBLACK:
299 picture->data[0] = ptr;
300 picture->data[1] = NULL;
301 picture->data[2] = NULL;
302 picture->linesize[0] = (width + 7) >> 3;
303 return picture->linesize[0] * height;
305 size2 = (size + 3) & ~3;
306 picture->data[0] = ptr;
307 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
308 picture->data[2] = NULL;
309 picture->linesize[0] = width;
310 picture->linesize[1] = 4;
311 return size2 + 256 * 4;
313 picture->data[0] = NULL;
314 picture->data[1] = NULL;
315 picture->data[2] = NULL;
316 picture->data[3] = NULL;
321 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
322 unsigned char *dest, int dest_size)
324 PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
325 int i, j, w, h, data_planes;
326 const unsigned char* s;
327 int size = avpicture_get_size(pix_fmt, width, height);
329 if (size > dest_size)
332 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
333 if (pix_fmt == PIX_FMT_YUV422 || pix_fmt == PIX_FMT_RGB565 ||
334 pix_fmt == PIX_FMT_RGB555)
336 else if (pix_fmt == PIX_FMT_PAL8)
339 w = width * (pf->depth * pf->nb_channels / 8);
344 data_planes = pf->nb_channels;
349 for (i=0; i<data_planes; i++) {
351 w = width >> pf->x_chroma_shift;
352 h = height >> pf->y_chroma_shift;
358 s += src->linesize[i];
362 if (pf->pixel_type == FF_PIXEL_PALETTE)
363 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
368 int avpicture_get_size(int pix_fmt, int width, int height)
370 AVPicture dummy_pict;
371 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
375 * compute the loss when converting from a pixel format to another
377 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
380 const PixFmtInfo *pf, *ps;
383 ps = &pix_fmt_info[src_pix_fmt];
384 pf = &pix_fmt_info[dst_pix_fmt];
388 pf = &pix_fmt_info[dst_pix_fmt];
389 if (pf->depth < ps->depth ||
390 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
391 loss |= FF_LOSS_DEPTH;
392 if (pf->x_chroma_shift > ps->x_chroma_shift ||
393 pf->y_chroma_shift > ps->y_chroma_shift)
394 loss |= FF_LOSS_RESOLUTION;
395 switch(pf->color_type) {
397 if (ps->color_type != FF_COLOR_RGB &&
398 ps->color_type != FF_COLOR_GRAY)
399 loss |= FF_LOSS_COLORSPACE;
402 if (ps->color_type != FF_COLOR_GRAY)
403 loss |= FF_LOSS_COLORSPACE;
406 if (ps->color_type != FF_COLOR_YUV)
407 loss |= FF_LOSS_COLORSPACE;
409 case FF_COLOR_YUV_JPEG:
410 if (ps->color_type != FF_COLOR_YUV_JPEG &&
411 ps->color_type != FF_COLOR_YUV &&
412 ps->color_type != FF_COLOR_GRAY)
413 loss |= FF_LOSS_COLORSPACE;
417 if (ps->color_type != pf->color_type)
418 loss |= FF_LOSS_COLORSPACE;
421 if (pf->color_type == FF_COLOR_GRAY &&
422 ps->color_type != FF_COLOR_GRAY)
423 loss |= FF_LOSS_CHROMA;
424 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
425 loss |= FF_LOSS_ALPHA;
426 if (pf->pixel_type == FF_PIXEL_PALETTE &&
427 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
428 loss |= FF_LOSS_COLORQUANT;
432 static int avg_bits_per_pixel(int pix_fmt)
435 const PixFmtInfo *pf;
437 pf = &pix_fmt_info[pix_fmt];
438 switch(pf->pixel_type) {
439 case FF_PIXEL_PACKED:
447 bits = pf->depth * pf->nb_channels;
451 case FF_PIXEL_PLANAR:
452 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
453 bits = pf->depth * pf->nb_channels;
455 bits = pf->depth + ((2 * pf->depth) >>
456 (pf->x_chroma_shift + pf->y_chroma_shift));
459 case FF_PIXEL_PALETTE:
469 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
474 int dist, i, loss, min_dist, dst_pix_fmt;
476 /* find exact color match with smallest size */
478 min_dist = 0x7fffffff;
479 for(i = 0;i < PIX_FMT_NB; i++) {
480 if (pix_fmt_mask & (1 << i)) {
481 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
483 dist = avg_bits_per_pixel(i);
484 if (dist < min_dist) {
495 * find best pixel format to convert to. Return -1 if none found
497 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
498 int has_alpha, int *loss_ptr)
500 int dst_pix_fmt, loss_mask, i;
501 static const int loss_mask_order[] = {
502 ~0, /* no loss first */
505 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
511 /* try with successive loss */
514 loss_mask = loss_mask_order[i++];
515 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
516 has_alpha, loss_mask);
517 if (dst_pix_fmt >= 0)
525 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
529 static void img_copy_plane(uint8_t *dst, int dst_wrap,
530 const uint8_t *src, int src_wrap,
531 int width, int height)
533 for(;height > 0; height--) {
534 memcpy(dst, src, width);
541 * Copy image 'src' to 'dst'.
543 void img_copy(AVPicture *dst, const AVPicture *src,
544 int pix_fmt, int width, int height)
547 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
549 pf = &pix_fmt_info[pix_fmt];
550 switch(pf->pixel_type) {
551 case FF_PIXEL_PACKED:
559 bits = pf->depth * pf->nb_channels;
562 bwidth = (width * bits + 7) >> 3;
563 img_copy_plane(dst->data[0], dst->linesize[0],
564 src->data[0], src->linesize[0],
567 case FF_PIXEL_PLANAR:
568 for(i = 0; i < pf->nb_channels; i++) {
572 if (i == 1 || i == 2) {
573 w >>= pf->x_chroma_shift;
574 h >>= pf->y_chroma_shift;
576 bwidth = (w * pf->depth + 7) >> 3;
577 img_copy_plane(dst->data[i], dst->linesize[i],
578 src->data[i], src->linesize[i],
582 case FF_PIXEL_PALETTE:
583 img_copy_plane(dst->data[0], dst->linesize[0],
584 src->data[0], src->linesize[0],
586 /* copy the palette */
587 img_copy_plane(dst->data[1], dst->linesize[1],
588 src->data[1], src->linesize[1],
594 /* XXX: totally non optimized */
596 static void yuv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
597 int width, int height)
599 const uint8_t *p, *p1;
600 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
608 for(;height >= 1; height -= 2) {
613 for(w = width; w >= 2; w -= 2) {
630 p1 += src->linesize[0];
631 lum1 += dst->linesize[0];
635 for(w = width; w >= 2; w -= 2) {
644 p1 += src->linesize[0];
645 lum1 += dst->linesize[0];
647 cb1 += dst->linesize[1];
648 cr1 += dst->linesize[2];
652 static void yuv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
653 int width, int height)
655 const uint8_t *p, *p1;
656 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
663 for(;height > 0; height--) {
668 for(w = width; w >= 2; w -= 2) {
678 p1 += src->linesize[0];
679 lum1 += dst->linesize[0];
680 cb1 += dst->linesize[1];
681 cr1 += dst->linesize[2];
685 static void yuv422p_to_yuv422(AVPicture *dst, const AVPicture *src,
686 int width, int height)
689 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
696 for(;height > 0; height--) {
701 for(w = width; w >= 2; w -= 2) {
711 p1 += dst->linesize[0];
712 lum1 += src->linesize[0];
713 cb1 += src->linesize[1];
714 cr1 += src->linesize[2];
719 #define ONE_HALF (1 << (SCALEBITS - 1))
720 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
722 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
726 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
727 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
729 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
732 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
734 y = ((y1) - 16) * FIX(255.0/219.0);\
735 r = cm[(y + r_add) >> SCALEBITS];\
736 g = cm[(y + g_add) >> SCALEBITS];\
737 b = cm[(y + b_add) >> SCALEBITS];\
740 #define YUV_TO_RGB1(cb1, cr1)\
744 r_add = FIX(1.40200) * cr + ONE_HALF;\
745 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
746 b_add = FIX(1.77200) * cb + ONE_HALF;\
749 #define YUV_TO_RGB2(r, g, b, y1)\
751 y = (y1) << SCALEBITS;\
752 r = cm[(y + r_add) >> SCALEBITS];\
753 g = cm[(y + g_add) >> SCALEBITS];\
754 b = cm[(y + b_add) >> SCALEBITS];\
757 #define Y_CCIR_TO_JPEG(y)\
758 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
760 #define Y_JPEG_TO_CCIR(y)\
761 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
763 #define C_CCIR_TO_JPEG(y)\
764 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
766 /* NOTE: the clamp is really necessary! */
767 static inline int C_JPEG_TO_CCIR(int y) {
768 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
775 #define RGB_TO_Y(r, g, b) \
776 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
777 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
779 #define RGB_TO_U(r1, g1, b1, shift)\
780 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
781 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
783 #define RGB_TO_V(r1, g1, b1, shift)\
784 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
785 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
787 #define RGB_TO_Y_CCIR(r, g, b) \
788 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
789 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
791 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
792 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
793 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
795 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
796 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
797 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
799 static uint8_t y_ccir_to_jpeg[256];
800 static uint8_t y_jpeg_to_ccir[256];
801 static uint8_t c_ccir_to_jpeg[256];
802 static uint8_t c_jpeg_to_ccir[256];
804 /* init various conversion tables */
805 static void img_convert_init(void)
808 uint8_t *cm = cropTbl + MAX_NEG_CROP;
810 for(i = 0;i < 256; i++) {
811 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
812 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
813 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
814 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
818 /* apply to each pixel the given table */
819 static void img_apply_table(uint8_t *dst, int dst_wrap,
820 const uint8_t *src, int src_wrap,
821 int width, int height, const uint8_t *table1)
826 const uint8_t *table;
829 for(;height > 0; height--) {
853 /* XXX: use generic filter ? */
854 /* XXX: in most cases, the sampling position is incorrect */
857 static void shrink41(uint8_t *dst, int dst_wrap,
858 const uint8_t *src, int src_wrap,
859 int width, int height)
865 for(;height > 0; height--) {
868 for(w = width;w > 0; w--) {
869 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
879 static void shrink21(uint8_t *dst, int dst_wrap,
880 const uint8_t *src, int src_wrap,
881 int width, int height)
887 for(;height > 0; height--) {
890 for(w = width;w > 0; w--) {
891 d[0] = (s[0] + s[1]) >> 1;
901 static void shrink12(uint8_t *dst, int dst_wrap,
902 const uint8_t *src, int src_wrap,
903 int width, int height)
907 const uint8_t *s1, *s2;
909 for(;height > 0; height--) {
913 for(w = width;w >= 4; w-=4) {
914 d[0] = (s1[0] + s2[0]) >> 1;
915 d[1] = (s1[1] + s2[1]) >> 1;
916 d[2] = (s1[2] + s2[2]) >> 1;
917 d[3] = (s1[3] + s2[3]) >> 1;
923 d[0] = (s1[0] + s2[0]) >> 1;
934 static void shrink22(uint8_t *dst, int dst_wrap,
935 const uint8_t *src, int src_wrap,
936 int width, int height)
939 const uint8_t *s1, *s2;
942 for(;height > 0; height--) {
946 for(w = width;w >= 4; w-=4) {
947 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
948 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
949 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
950 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
956 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
967 static void shrink44(uint8_t *dst, int dst_wrap,
968 const uint8_t *src, int src_wrap,
969 int width, int height)
972 const uint8_t *s1, *s2, *s3, *s4;
975 for(;height > 0; height--) {
981 for(w = width;w > 0; w--) {
982 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
983 s2[0] + s2[1] + s2[2] + s2[3] +
984 s3[0] + s3[1] + s3[2] + s3[3] +
985 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
997 static void grow21_line(uint8_t *dst, const uint8_t *src,
1006 for(w = width;w >= 4; w-=4) {
1007 d[1] = d[0] = s1[0];
1008 d[3] = d[2] = s1[1];
1012 for(;w >= 2; w -= 2) {
1013 d[1] = d[0] = s1[0];
1017 /* only needed if width is not a multiple of two */
1018 /* XXX: veryfy that */
1024 static void grow41_line(uint8_t *dst, const uint8_t *src,
1033 for(w = width;w >= 4; w-=4) {
1045 static void grow21(uint8_t *dst, int dst_wrap,
1046 const uint8_t *src, int src_wrap,
1047 int width, int height)
1049 for(;height > 0; height--) {
1050 grow21_line(dst, src, width);
1057 static void grow22(uint8_t *dst, int dst_wrap,
1058 const uint8_t *src, int src_wrap,
1059 int width, int height)
1061 for(;height > 0; height--) {
1062 grow21_line(dst, src, width);
1070 static void grow41(uint8_t *dst, int dst_wrap,
1071 const uint8_t *src, int src_wrap,
1072 int width, int height)
1074 for(;height > 0; height--) {
1075 grow41_line(dst, src, width);
1082 static void grow44(uint8_t *dst, int dst_wrap,
1083 const uint8_t *src, int src_wrap,
1084 int width, int height)
1086 for(;height > 0; height--) {
1087 grow41_line(dst, src, width);
1088 if ((height & 3) == 1)
1095 static void conv411(uint8_t *dst, int dst_wrap,
1096 const uint8_t *src, int src_wrap,
1097 int width, int height)
1100 const uint8_t *s1, *s2;
1105 for(;height > 0; height--) {
1107 s2 = src + src_wrap;
1109 for(w = width;w > 0; w--) {
1110 c = (s1[0] + s2[0]) >> 1;
1117 src += src_wrap * 2;
1122 /* XXX: add jpeg quantize code */
1124 #define TRANSP_INDEX (6*6*6)
1126 /* this is maybe slow, but allows for extensions */
1127 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1129 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1132 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1135 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1138 pal = (uint32_t *)palette;
1140 for(r = 0; r < 6; r++) {
1141 for(g = 0; g < 6; g++) {
1142 for(b = 0; b < 6; b++) {
1143 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1144 (pal_value[g] << 8) | pal_value[b];
1151 pal[i++] = 0xff000000;
1154 /* copy bit n to bits 0 ... n - 1 */
1155 static inline unsigned int bitcopy_n(unsigned int a, int n)
1158 mask = (1 << n) - 1;
1159 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1162 /* rgb555 handling */
1164 #define RGB_NAME rgb555
1166 #define RGB_IN(r, g, b, s)\
1168 unsigned int v = ((const uint16_t *)(s))[0];\
1169 r = bitcopy_n(v >> (10 - 3), 3);\
1170 g = bitcopy_n(v >> (5 - 3), 3);\
1171 b = bitcopy_n(v << 3, 3);\
1174 #define RGBA_IN(r, g, b, a, s)\
1176 unsigned int v = ((const uint16_t *)(s))[0];\
1177 r = bitcopy_n(v >> (10 - 3), 3);\
1178 g = bitcopy_n(v >> (5 - 3), 3);\
1179 b = bitcopy_n(v << 3, 3);\
1180 a = (-(v >> 15)) & 0xff;\
1183 #define RGBA_OUT(d, r, g, b, a)\
1185 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1186 ((a << 8) & 0x8000);\
1191 #include "imgconvert_template.h"
1193 /* rgb565 handling */
1195 #define RGB_NAME rgb565
1197 #define RGB_IN(r, g, b, s)\
1199 unsigned int v = ((const uint16_t *)(s))[0];\
1200 r = bitcopy_n(v >> (11 - 3), 3);\
1201 g = bitcopy_n(v >> (5 - 2), 2);\
1202 b = bitcopy_n(v << 3, 3);\
1205 #define RGB_OUT(d, r, g, b)\
1207 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1212 #include "imgconvert_template.h"
1214 /* bgr24 handling */
1216 #define RGB_NAME bgr24
1218 #define RGB_IN(r, g, b, s)\
1225 #define RGB_OUT(d, r, g, b)\
1234 #include "imgconvert_template.h"
1240 /* rgb24 handling */
1242 #define RGB_NAME rgb24
1245 #define RGB_IN(r, g, b, s)\
1252 #define RGB_OUT(d, r, g, b)\
1261 #include "imgconvert_template.h"
1263 /* rgba32 handling */
1265 #define RGB_NAME rgba32
1268 #define RGB_IN(r, g, b, s)\
1270 unsigned int v = ((const uint32_t *)(s))[0];\
1271 r = (v >> 16) & 0xff;\
1272 g = (v >> 8) & 0xff;\
1276 #define RGBA_IN(r, g, b, a, s)\
1278 unsigned int v = ((const uint32_t *)(s))[0];\
1279 a = (v >> 24) & 0xff;\
1280 r = (v >> 16) & 0xff;\
1281 g = (v >> 8) & 0xff;\
1285 #define RGBA_OUT(d, r, g, b, a)\
1287 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1292 #include "imgconvert_template.h"
1294 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1295 int width, int height, int xor_mask)
1297 const unsigned char *p;
1299 int v, dst_wrap, src_wrap;
1303 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1306 dst_wrap = dst->linesize[0] - width;
1307 for(y=0;y<height;y++) {
1310 v = *p++ ^ xor_mask;
1312 q[1] = -((v >> 6) & 1);
1313 q[2] = -((v >> 5) & 1);
1314 q[3] = -((v >> 4) & 1);
1315 q[4] = -((v >> 3) & 1);
1316 q[5] = -((v >> 2) & 1);
1317 q[6] = -((v >> 1) & 1);
1318 q[7] = -((v >> 0) & 1);
1323 v = *p++ ^ xor_mask;
1325 q[0] = -((v >> 7) & 1);
1335 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1336 int width, int height)
1338 mono_to_gray(dst, src, width, height, 0xff);
1341 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1342 int width, int height)
1344 mono_to_gray(dst, src, width, height, 0x00);
1347 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1348 int width, int height, int xor_mask)
1353 int j, b, v, n1, src_wrap, dst_wrap, y;
1356 src_wrap = src->linesize[0] - width;
1359 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1361 for(y=0;y<height;y++) {
1368 v = (v << 1) | (b >> 7);
1370 d[0] = v ^ xor_mask;
1380 v = (v << 1) | (b >> 7);
1383 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1391 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1392 int width, int height)
1394 gray_to_mono(dst, src, width, height, 0xff);
1397 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1398 int width, int height)
1400 gray_to_mono(dst, src, width, height, 0x00);
1403 typedef struct ConvertEntry {
1404 void (*convert)(AVPicture *dst,
1405 const AVPicture *src, int width, int height);
1408 /* Add each new convertion function in this table. In order to be able
1409 to convert from any format to any format, the following constraints
1412 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1414 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1416 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1418 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1421 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1423 The other conversion functions are just optimisations for common cases.
1425 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1426 [PIX_FMT_YUV420P] = {
1427 [PIX_FMT_RGB555] = {
1428 .convert = yuv420p_to_rgb555
1430 [PIX_FMT_RGB565] = {
1431 .convert = yuv420p_to_rgb565
1434 .convert = yuv420p_to_bgr24
1437 .convert = yuv420p_to_rgb24
1439 [PIX_FMT_RGBA32] = {
1440 .convert = yuv420p_to_rgba32
1443 [PIX_FMT_YUV422P] = {
1444 [PIX_FMT_YUV422] = {
1445 .convert = yuv422p_to_yuv422,
1448 [PIX_FMT_YUV444P] = {
1450 .convert = yuv444p_to_rgb24
1453 [PIX_FMT_YUVJ420P] = {
1454 [PIX_FMT_RGB555] = {
1455 .convert = yuvj420p_to_rgb555
1457 [PIX_FMT_RGB565] = {
1458 .convert = yuvj420p_to_rgb565
1461 .convert = yuvj420p_to_bgr24
1464 .convert = yuvj420p_to_rgb24
1466 [PIX_FMT_RGBA32] = {
1467 .convert = yuvj420p_to_rgba32
1470 [PIX_FMT_YUVJ444P] = {
1472 .convert = yuvj444p_to_rgb24
1475 [PIX_FMT_YUV422] = {
1476 [PIX_FMT_YUV420P] = {
1477 .convert = yuv422_to_yuv420p,
1479 [PIX_FMT_YUV422P] = {
1480 .convert = yuv422_to_yuv422p,
1485 [PIX_FMT_YUV420P] = {
1486 .convert = rgb24_to_yuv420p
1488 [PIX_FMT_RGB565] = {
1489 .convert = rgb24_to_rgb565
1491 [PIX_FMT_RGB555] = {
1492 .convert = rgb24_to_rgb555
1494 [PIX_FMT_RGBA32] = {
1495 .convert = rgb24_to_rgba32
1498 .convert = rgb24_to_bgr24
1501 .convert = rgb24_to_gray
1504 .convert = rgb24_to_pal8
1506 [PIX_FMT_YUV444P] = {
1507 .convert = rgb24_to_yuv444p
1509 [PIX_FMT_YUVJ420P] = {
1510 .convert = rgb24_to_yuvj420p
1512 [PIX_FMT_YUVJ444P] = {
1513 .convert = rgb24_to_yuvj444p
1516 [PIX_FMT_RGBA32] = {
1518 .convert = rgba32_to_rgb24
1520 [PIX_FMT_RGB555] = {
1521 .convert = rgba32_to_rgb555
1524 .convert = rgba32_to_pal8
1526 [PIX_FMT_YUV420P] = {
1527 .convert = rgba32_to_yuv420p
1530 .convert = rgba32_to_gray
1535 .convert = bgr24_to_rgb24
1537 [PIX_FMT_YUV420P] = {
1538 .convert = bgr24_to_yuv420p
1541 .convert = bgr24_to_gray
1544 [PIX_FMT_RGB555] = {
1546 .convert = rgb555_to_rgb24
1548 [PIX_FMT_RGBA32] = {
1549 .convert = rgb555_to_rgba32
1551 [PIX_FMT_YUV420P] = {
1552 .convert = rgb555_to_yuv420p
1555 .convert = rgb555_to_gray
1558 [PIX_FMT_RGB565] = {
1560 .convert = rgb565_to_rgb24
1562 [PIX_FMT_YUV420P] = {
1563 .convert = rgb565_to_yuv420p
1566 .convert = rgb565_to_gray
1570 [PIX_FMT_RGB555] = {
1571 .convert = gray_to_rgb555
1573 [PIX_FMT_RGB565] = {
1574 .convert = gray_to_rgb565
1577 .convert = gray_to_rgb24
1580 .convert = gray_to_bgr24
1582 [PIX_FMT_RGBA32] = {
1583 .convert = gray_to_rgba32
1585 [PIX_FMT_MONOWHITE] = {
1586 .convert = gray_to_monowhite
1588 [PIX_FMT_MONOBLACK] = {
1589 .convert = gray_to_monoblack
1592 [PIX_FMT_MONOWHITE] = {
1594 .convert = monowhite_to_gray
1597 [PIX_FMT_MONOBLACK] = {
1599 .convert = monoblack_to_gray
1603 [PIX_FMT_RGB555] = {
1604 .convert = pal8_to_rgb555
1606 [PIX_FMT_RGB565] = {
1607 .convert = pal8_to_rgb565
1610 .convert = pal8_to_bgr24
1613 .convert = pal8_to_rgb24
1615 [PIX_FMT_RGBA32] = {
1616 .convert = pal8_to_rgba32
1621 int avpicture_alloc(AVPicture *picture,
1622 int pix_fmt, int width, int height)
1627 size = avpicture_get_size(pix_fmt, width, height);
1628 ptr = av_malloc(size);
1631 avpicture_fill(picture, ptr, pix_fmt, width, height);
1634 memset(picture, 0, sizeof(AVPicture));
1638 void avpicture_free(AVPicture *picture)
1640 av_free(picture->data[0]);
1643 /* return true if yuv planar */
1644 static inline int is_yuv_planar(PixFmtInfo *ps)
1646 return (ps->color_type == FF_COLOR_YUV ||
1647 ps->color_type == FF_COLOR_YUV_JPEG) &&
1648 ps->pixel_type == FF_PIXEL_PLANAR;
1651 /* XXX: always use linesize. Return -1 if not supported */
1652 int img_convert(AVPicture *dst, int dst_pix_fmt,
1653 const AVPicture *src, int src_pix_fmt,
1654 int src_width, int src_height)
1657 int i, ret, dst_width, dst_height, int_pix_fmt;
1658 PixFmtInfo *src_pix, *dst_pix;
1660 AVPicture tmp1, *tmp = &tmp1;
1662 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1663 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1665 if (src_width <= 0 || src_height <= 0)
1673 dst_width = src_width;
1674 dst_height = src_height;
1676 dst_pix = &pix_fmt_info[dst_pix_fmt];
1677 src_pix = &pix_fmt_info[src_pix_fmt];
1678 if (src_pix_fmt == dst_pix_fmt) {
1679 /* no conversion needed: just copy */
1680 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
1684 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1686 /* specific convertion routine */
1687 ce->convert(dst, src, dst_width, dst_height);
1692 if (is_yuv_planar(dst_pix) &&
1693 src_pix_fmt == PIX_FMT_GRAY8) {
1697 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
1698 img_copy_plane(dst->data[0], dst->linesize[0],
1699 src->data[0], src->linesize[0],
1700 dst_width, dst_height);
1702 img_apply_table(dst->data[0], dst->linesize[0],
1703 src->data[0], src->linesize[0],
1704 dst_width, dst_height,
1707 /* fill U and V with 128 */
1710 w >>= dst_pix->x_chroma_shift;
1711 h >>= dst_pix->y_chroma_shift;
1712 for(i = 1; i <= 2; i++) {
1714 for(y = 0; y< h; y++) {
1716 d += dst->linesize[i];
1723 if (is_yuv_planar(src_pix) &&
1724 dst_pix_fmt == PIX_FMT_GRAY8) {
1725 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
1726 img_copy_plane(dst->data[0], dst->linesize[0],
1727 src->data[0], src->linesize[0],
1728 dst_width, dst_height);
1730 img_apply_table(dst->data[0], dst->linesize[0],
1731 src->data[0], src->linesize[0],
1732 dst_width, dst_height,
1738 /* YUV to YUV planar */
1739 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
1740 int x_shift, y_shift, w, h, xy_shift;
1741 void (*resize_func)(uint8_t *dst, int dst_wrap,
1742 const uint8_t *src, int src_wrap,
1743 int width, int height);
1745 /* compute chroma size of the smallest dimensions */
1748 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1749 w >>= dst_pix->x_chroma_shift;
1751 w >>= src_pix->x_chroma_shift;
1752 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1753 h >>= dst_pix->y_chroma_shift;
1755 h >>= src_pix->y_chroma_shift;
1757 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1758 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
1759 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
1760 /* there must be filters for conversion at least from and to
1764 resize_func = img_copy_plane;
1767 resize_func = shrink21;
1770 resize_func = shrink41;
1773 resize_func = shrink12;
1776 resize_func = shrink22;
1779 resize_func = shrink44;
1782 resize_func = grow21;
1785 resize_func = grow41;
1788 resize_func = grow22;
1791 resize_func = grow44;
1794 resize_func = conv411;
1797 /* currently not handled */
1798 goto no_chroma_filter;
1801 img_copy_plane(dst->data[0], dst->linesize[0],
1802 src->data[0], src->linesize[0],
1803 dst_width, dst_height);
1805 for(i = 1;i <= 2; i++)
1806 resize_func(dst->data[i], dst->linesize[i],
1807 src->data[i], src->linesize[i],
1808 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
1809 /* if yuv color space conversion is needed, we do it here on
1810 the destination image */
1811 if (dst_pix->color_type != src_pix->color_type) {
1812 const uint8_t *y_table, *c_table;
1813 if (dst_pix->color_type == FF_COLOR_YUV) {
1814 y_table = y_jpeg_to_ccir;
1815 c_table = c_jpeg_to_ccir;
1817 y_table = y_ccir_to_jpeg;
1818 c_table = c_ccir_to_jpeg;
1820 img_apply_table(dst->data[0], dst->linesize[0],
1821 dst->data[0], dst->linesize[0],
1822 dst_width, dst_height,
1825 for(i = 1;i <= 2; i++)
1826 img_apply_table(dst->data[i], dst->linesize[i],
1827 dst->data[i], dst->linesize[i],
1828 dst_width>>dst_pix->x_chroma_shift,
1829 dst_height>>dst_pix->y_chroma_shift,
1836 /* try to use an intermediate format */
1837 if (src_pix_fmt == PIX_FMT_YUV422 ||
1838 dst_pix_fmt == PIX_FMT_YUV422) {
1839 /* specific case: convert to YUV422P first */
1840 int_pix_fmt = PIX_FMT_YUV422P;
1841 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
1842 src_pix_fmt != PIX_FMT_GRAY8) ||
1843 (dst_pix->color_type == FF_COLOR_GRAY &&
1844 dst_pix_fmt != PIX_FMT_GRAY8)) {
1845 /* gray8 is the normalized format */
1846 int_pix_fmt = PIX_FMT_GRAY8;
1847 } else if ((is_yuv_planar(src_pix) &&
1848 src_pix_fmt != PIX_FMT_YUV444P &&
1849 src_pix_fmt != PIX_FMT_YUVJ444P)) {
1850 /* yuv444 is the normalized format */
1851 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
1852 int_pix_fmt = PIX_FMT_YUVJ444P;
1854 int_pix_fmt = PIX_FMT_YUV444P;
1855 } else if ((is_yuv_planar(dst_pix) &&
1856 dst_pix_fmt != PIX_FMT_YUV444P &&
1857 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
1858 /* yuv444 is the normalized format */
1859 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
1860 int_pix_fmt = PIX_FMT_YUVJ444P;
1862 int_pix_fmt = PIX_FMT_YUV444P;
1864 /* the two formats are rgb or gray8 or yuv[j]444p */
1865 if (src_pix->is_alpha && dst_pix->is_alpha)
1866 int_pix_fmt = PIX_FMT_RGBA32;
1868 int_pix_fmt = PIX_FMT_RGB24;
1870 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
1873 if (img_convert(tmp, int_pix_fmt,
1874 src, src_pix_fmt, src_width, src_height) < 0)
1876 if (img_convert(dst, dst_pix_fmt,
1877 tmp, int_pix_fmt, dst_width, dst_height) < 0)
1881 avpicture_free(tmp);
1885 /* NOTE: we scan all the pixels to have an exact information */
1886 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1888 const unsigned char *p;
1889 int src_wrap, ret, x, y;
1891 uint32_t *palette = (uint32_t *)src->data[1];
1894 src_wrap = src->linesize[0] - width;
1896 for(y=0;y<height;y++) {
1897 for(x=0;x<width;x++) {
1898 a = palette[p[0]] >> 24;
1900 ret |= FF_ALPHA_TRANSP;
1901 } else if (a != 0xff) {
1902 ret |= FF_ALPHA_SEMI_TRANSP;
1912 * Tell if an image really has transparent alpha values.
1913 * @return ored mask of FF_ALPHA_xxx constants
1915 int img_get_alpha_info(const AVPicture *src,
1916 int pix_fmt, int width, int height)
1918 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1921 pf = &pix_fmt_info[pix_fmt];
1922 /* no alpha can be represented in format */
1926 case PIX_FMT_RGBA32:
1927 ret = get_alpha_info_rgba32(src, width, height);
1929 case PIX_FMT_RGB555:
1930 ret = get_alpha_info_rgb555(src, width, height);
1933 ret = get_alpha_info_pal8(src, width, height);
1936 /* we do not know, so everything is indicated */
1937 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1944 #define DEINT_INPLACE_LINE_LUM \
1945 movd_m2r(lum_m4[0],mm0);\
1946 movd_m2r(lum_m3[0],mm1);\
1947 movd_m2r(lum_m2[0],mm2);\
1948 movd_m2r(lum_m1[0],mm3);\
1949 movd_m2r(lum[0],mm4);\
1950 punpcklbw_r2r(mm7,mm0);\
1951 movd_r2m(mm2,lum_m4[0]);\
1952 punpcklbw_r2r(mm7,mm1);\
1953 punpcklbw_r2r(mm7,mm2);\
1954 punpcklbw_r2r(mm7,mm3);\
1955 punpcklbw_r2r(mm7,mm4);\
1956 paddw_r2r(mm3,mm1);\
1958 paddw_r2r(mm4,mm0);\
1960 paddw_r2r(mm6,mm2);\
1961 paddw_r2r(mm2,mm1);\
1962 psubusw_r2r(mm0,mm1);\
1964 packuswb_r2r(mm7,mm1);\
1965 movd_r2m(mm1,lum_m2[0]);
1967 #define DEINT_LINE_LUM \
1968 movd_m2r(lum_m4[0],mm0);\
1969 movd_m2r(lum_m3[0],mm1);\
1970 movd_m2r(lum_m2[0],mm2);\
1971 movd_m2r(lum_m1[0],mm3);\
1972 movd_m2r(lum[0],mm4);\
1973 punpcklbw_r2r(mm7,mm0);\
1974 punpcklbw_r2r(mm7,mm1);\
1975 punpcklbw_r2r(mm7,mm2);\
1976 punpcklbw_r2r(mm7,mm3);\
1977 punpcklbw_r2r(mm7,mm4);\
1978 paddw_r2r(mm3,mm1);\
1980 paddw_r2r(mm4,mm0);\
1982 paddw_r2r(mm6,mm2);\
1983 paddw_r2r(mm2,mm1);\
1984 psubusw_r2r(mm0,mm1);\
1986 packuswb_r2r(mm7,mm1);\
1987 movd_r2m(mm1,dst[0]);
1990 /* filter parameters: [-1 4 2 4 -1] // 8 */
1991 static void deinterlace_line(uint8_t *dst,
1992 const uint8_t *lum_m4, const uint8_t *lum_m3,
1993 const uint8_t *lum_m2, const uint8_t *lum_m1,
1998 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2001 for(;size > 0;size--) {
2003 sum += lum_m3[0] << 2;
2004 sum += lum_m2[0] << 1;
2005 sum += lum_m1[0] << 2;
2007 dst[0] = cm[(sum + 4) >> 3];
2024 movq_m2r(rounder,mm6);
2026 for (;size > 3; size-=4) {
2037 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2041 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2044 for(;size > 0;size--) {
2046 sum += lum_m3[0] << 2;
2047 sum += lum_m2[0] << 1;
2048 lum_m4[0]=lum_m2[0];
2049 sum += lum_m1[0] << 2;
2051 lum_m2[0] = cm[(sum + 4) >> 3];
2067 movq_m2r(rounder,mm6);
2069 for (;size > 3; size-=4) {
2070 DEINT_INPLACE_LINE_LUM
2080 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2081 top field is copied as is, but the bottom field is deinterlaced
2082 against the top field. */
2083 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2084 const uint8_t *src1, int src_wrap,
2085 int width, int height)
2087 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2092 src_0=&src_m1[src_wrap];
2093 src_p1=&src_0[src_wrap];
2094 src_p2=&src_p1[src_wrap];
2095 for(y=0;y<(height-2);y+=2) {
2096 memcpy(dst,src_m1,width);
2098 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2102 src_p1 += 2*src_wrap;
2103 src_p2 += 2*src_wrap;
2106 memcpy(dst,src_m1,width);
2109 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2112 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2113 int width, int height)
2115 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2118 buf = (uint8_t*)av_malloc(width);
2121 memcpy(buf,src_m1,width);
2122 src_0=&src_m1[src_wrap];
2123 src_p1=&src_0[src_wrap];
2124 src_p2=&src_p1[src_wrap];
2125 for(y=0;y<(height-2);y+=2) {
2126 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2129 src_p1 += 2*src_wrap;
2130 src_p2 += 2*src_wrap;
2133 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2138 /* deinterlace - if not supported return -1 */
2139 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2140 int pix_fmt, int width, int height)
2144 if (pix_fmt != PIX_FMT_YUV420P &&
2145 pix_fmt != PIX_FMT_YUV422P &&
2146 pix_fmt != PIX_FMT_YUV444P &&
2147 pix_fmt != PIX_FMT_YUV411P)
2149 if ((width & 3) != 0 || (height & 3) != 0)
2155 case PIX_FMT_YUV420P:
2159 case PIX_FMT_YUV422P:
2162 case PIX_FMT_YUV411P:
2170 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2173 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2174 src->data[i], src->linesize[i],