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,
154 .color_type = FF_COLOR_RGB,
155 .pixel_type = FF_PIXEL_PACKED,
160 .nb_channels = 4, .is_alpha = 1,
161 .color_type = FF_COLOR_RGB,
162 .pixel_type = FF_PIXEL_PACKED,
168 .color_type = FF_COLOR_RGB,
169 .pixel_type = FF_PIXEL_PACKED,
174 .nb_channels = 4, .is_alpha = 1,
175 .color_type = FF_COLOR_RGB,
176 .pixel_type = FF_PIXEL_PACKED,
180 /* gray / mono formats */
184 .color_type = FF_COLOR_GRAY,
185 .pixel_type = FF_PIXEL_PLANAR,
188 [PIX_FMT_MONOWHITE] = {
191 .color_type = FF_COLOR_GRAY,
192 .pixel_type = FF_PIXEL_PLANAR,
195 [PIX_FMT_MONOBLACK] = {
198 .color_type = FF_COLOR_GRAY,
199 .pixel_type = FF_PIXEL_PLANAR,
203 /* paletted formats */
206 .nb_channels = 4, .is_alpha = 1,
207 .color_type = FF_COLOR_RGB,
208 .pixel_type = FF_PIXEL_PALETTE,
213 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
215 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
216 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
219 const char *avcodec_get_pix_fmt_name(int pix_fmt)
221 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
224 return pix_fmt_info[pix_fmt].name;
227 enum PixelFormat avcodec_get_pix_fmt(const char* name)
231 for (i=0; i < PIX_FMT_NB; i++)
232 if (!strcmp(pix_fmt_info[i].name, name))
237 /* Picture field are filled with 'ptr' addresses. Also return size */
238 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
239 int pix_fmt, int width, int height)
241 int size, w2, h2, size2;
244 pinfo = &pix_fmt_info[pix_fmt];
245 size = width * height;
247 case PIX_FMT_YUV420P:
248 case PIX_FMT_YUV422P:
249 case PIX_FMT_YUV444P:
250 case PIX_FMT_YUV410P:
251 case PIX_FMT_YUV411P:
252 case PIX_FMT_YUVJ420P:
253 case PIX_FMT_YUVJ422P:
254 case PIX_FMT_YUVJ444P:
255 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
256 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
258 picture->data[0] = ptr;
259 picture->data[1] = picture->data[0] + size;
260 picture->data[2] = picture->data[1] + size2;
261 picture->linesize[0] = width;
262 picture->linesize[1] = w2;
263 picture->linesize[2] = w2;
264 return size + 2 * size2;
267 picture->data[0] = ptr;
268 picture->data[1] = NULL;
269 picture->data[2] = NULL;
270 picture->linesize[0] = width * 3;
273 picture->data[0] = ptr;
274 picture->data[1] = NULL;
275 picture->data[2] = NULL;
276 picture->linesize[0] = width * 4;
281 picture->data[0] = ptr;
282 picture->data[1] = NULL;
283 picture->data[2] = NULL;
284 picture->linesize[0] = width * 2;
287 picture->data[0] = ptr;
288 picture->data[1] = NULL;
289 picture->data[2] = NULL;
290 picture->linesize[0] = width;
292 case PIX_FMT_MONOWHITE:
293 case PIX_FMT_MONOBLACK:
294 picture->data[0] = ptr;
295 picture->data[1] = NULL;
296 picture->data[2] = NULL;
297 picture->linesize[0] = (width + 7) >> 3;
298 return picture->linesize[0] * height;
300 size2 = (size + 3) & ~3;
301 picture->data[0] = ptr;
302 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
303 picture->data[2] = NULL;
304 picture->linesize[0] = width;
305 picture->linesize[1] = 4;
306 return size2 + 256 * 4;
308 picture->data[0] = NULL;
309 picture->data[1] = NULL;
310 picture->data[2] = NULL;
311 picture->data[3] = NULL;
316 int avpicture_layout(AVPicture* src, int pix_fmt, int width, int height,
317 unsigned char *dest, int dest_size)
319 PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
320 int i, j, w, h, data_planes;
322 int size = avpicture_get_size(pix_fmt, width, height);
324 if (size > dest_size)
327 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
328 if (pix_fmt == PIX_FMT_YUV422 || pix_fmt == PIX_FMT_RGB565 ||
329 pix_fmt == PIX_FMT_RGB555)
331 else if (pix_fmt == PIX_FMT_PAL8)
334 w = width * (pf->depth * pf->nb_channels / 8);
339 data_planes = pf->nb_channels;
344 for (i=0; i<data_planes; i++) {
346 w = width >> pf->x_chroma_shift;
347 h = height >> pf->y_chroma_shift;
353 s += src->linesize[i];
357 if (pf->pixel_type == FF_PIXEL_PALETTE)
358 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
363 int avpicture_get_size(int pix_fmt, int width, int height)
365 AVPicture dummy_pict;
366 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
370 * compute the loss when converting from a pixel format to another
372 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
375 const PixFmtInfo *pf, *ps;
378 ps = &pix_fmt_info[src_pix_fmt];
379 pf = &pix_fmt_info[dst_pix_fmt];
383 pf = &pix_fmt_info[dst_pix_fmt];
384 if (pf->depth < ps->depth ||
385 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
386 loss |= FF_LOSS_DEPTH;
387 if (pf->x_chroma_shift > ps->x_chroma_shift ||
388 pf->y_chroma_shift > ps->y_chroma_shift)
389 loss |= FF_LOSS_RESOLUTION;
390 switch(pf->color_type) {
392 if (ps->color_type != FF_COLOR_RGB &&
393 ps->color_type != FF_COLOR_GRAY)
394 loss |= FF_LOSS_COLORSPACE;
397 if (ps->color_type != FF_COLOR_GRAY)
398 loss |= FF_LOSS_COLORSPACE;
401 if (ps->color_type != FF_COLOR_YUV)
402 loss |= FF_LOSS_COLORSPACE;
404 case FF_COLOR_YUV_JPEG:
405 if (ps->color_type != FF_COLOR_YUV_JPEG &&
406 ps->color_type != FF_COLOR_YUV &&
407 ps->color_type != FF_COLOR_GRAY)
408 loss |= FF_LOSS_COLORSPACE;
412 if (ps->color_type != pf->color_type)
413 loss |= FF_LOSS_COLORSPACE;
416 if (pf->color_type == FF_COLOR_GRAY &&
417 ps->color_type != FF_COLOR_GRAY)
418 loss |= FF_LOSS_CHROMA;
419 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
420 loss |= FF_LOSS_ALPHA;
421 if (pf->pixel_type == FF_PIXEL_PALETTE &&
422 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
423 loss |= FF_LOSS_COLORQUANT;
427 static int avg_bits_per_pixel(int pix_fmt)
430 const PixFmtInfo *pf;
432 pf = &pix_fmt_info[pix_fmt];
433 switch(pf->pixel_type) {
434 case FF_PIXEL_PACKED:
442 bits = pf->depth * pf->nb_channels;
446 case FF_PIXEL_PLANAR:
447 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
448 bits = pf->depth * pf->nb_channels;
450 bits = pf->depth + ((2 * pf->depth) >>
451 (pf->x_chroma_shift + pf->y_chroma_shift));
454 case FF_PIXEL_PALETTE:
464 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
469 int dist, i, loss, min_dist, dst_pix_fmt;
471 /* find exact color match with smallest size */
473 min_dist = 0x7fffffff;
474 for(i = 0;i < PIX_FMT_NB; i++) {
475 if (pix_fmt_mask & (1 << i)) {
476 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
478 dist = avg_bits_per_pixel(i);
479 if (dist < min_dist) {
490 * find best pixel format to convert to. Return -1 if none found
492 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
493 int has_alpha, int *loss_ptr)
495 int dst_pix_fmt, loss_mask, i;
496 static const int loss_mask_order[] = {
497 ~0, /* no loss first */
500 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
506 /* try with successive loss */
509 loss_mask = loss_mask_order[i++];
510 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
511 has_alpha, loss_mask);
512 if (dst_pix_fmt >= 0)
520 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
524 static void img_copy_plane(uint8_t *dst, int dst_wrap,
525 const uint8_t *src, int src_wrap,
526 int width, int height)
528 for(;height > 0; height--) {
529 memcpy(dst, src, width);
536 * Copy image 'src' to 'dst'.
538 void img_copy(AVPicture *dst, AVPicture *src,
539 int pix_fmt, int width, int height)
542 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
544 pf = &pix_fmt_info[pix_fmt];
545 switch(pf->pixel_type) {
546 case FF_PIXEL_PACKED:
554 bits = pf->depth * pf->nb_channels;
557 bwidth = (width * bits + 7) >> 3;
558 img_copy_plane(dst->data[0], dst->linesize[0],
559 src->data[0], src->linesize[0],
562 case FF_PIXEL_PLANAR:
563 for(i = 0; i < pf->nb_channels; i++) {
567 if (i == 1 || i == 2) {
568 w >>= pf->x_chroma_shift;
569 h >>= pf->y_chroma_shift;
571 bwidth = (w * pf->depth + 7) >> 3;
572 img_copy_plane(dst->data[i], dst->linesize[i],
573 src->data[i], src->linesize[i],
577 case FF_PIXEL_PALETTE:
578 img_copy_plane(dst->data[0], dst->linesize[0],
579 src->data[0], src->linesize[0],
581 /* copy the palette */
582 img_copy_plane(dst->data[1], dst->linesize[1],
583 src->data[1], src->linesize[1],
589 /* XXX: totally non optimized */
591 static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
592 int width, int height)
594 const uint8_t *p, *p1;
595 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
603 for(;height >= 1; height -= 2) {
608 for(w = width; w >= 2; w -= 2) {
625 p1 += src->linesize[0];
626 lum1 += dst->linesize[0];
630 for(w = width; w >= 2; w -= 2) {
639 p1 += src->linesize[0];
640 lum1 += dst->linesize[0];
642 cb1 += dst->linesize[1];
643 cr1 += dst->linesize[2];
647 static void yuv422_to_yuv422p(AVPicture *dst, AVPicture *src,
648 int width, int height)
650 const uint8_t *p, *p1;
651 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
658 for(;height > 0; height--) {
663 for(w = width; w >= 2; w -= 2) {
673 p1 += src->linesize[0];
674 lum1 += dst->linesize[0];
675 cb1 += dst->linesize[1];
676 cr1 += dst->linesize[2];
680 static void yuv422p_to_yuv422(AVPicture *dst, AVPicture *src,
681 int width, int height)
684 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
691 for(;height > 0; height--) {
696 for(w = width; w >= 2; w -= 2) {
706 p1 += dst->linesize[0];
707 lum1 += src->linesize[0];
708 cb1 += src->linesize[1];
709 cr1 += src->linesize[2];
714 #define ONE_HALF (1 << (SCALEBITS - 1))
715 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
717 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
721 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
722 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
724 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
727 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
729 y = ((y1) - 16) * FIX(255.0/219.0);\
730 r = cm[(y + r_add) >> SCALEBITS];\
731 g = cm[(y + g_add) >> SCALEBITS];\
732 b = cm[(y + b_add) >> SCALEBITS];\
735 #define YUV_TO_RGB1(cb1, cr1)\
739 r_add = FIX(1.40200) * cr + ONE_HALF;\
740 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
741 b_add = FIX(1.77200) * cb + ONE_HALF;\
744 #define YUV_TO_RGB2(r, g, b, y1)\
746 y = (y1) << SCALEBITS;\
747 r = cm[(y + r_add) >> SCALEBITS];\
748 g = cm[(y + g_add) >> SCALEBITS];\
749 b = cm[(y + b_add) >> SCALEBITS];\
752 #define Y_CCIR_TO_JPEG(y)\
753 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
755 #define Y_JPEG_TO_CCIR(y)\
756 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
758 #define C_CCIR_TO_JPEG(y)\
759 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
761 /* NOTE: the clamp is really necessary! */
762 static inline int C_JPEG_TO_CCIR(int y) {
763 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
770 #define RGB_TO_Y(r, g, b) \
771 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
772 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
774 #define RGB_TO_U(r1, g1, b1, shift)\
775 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
776 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
778 #define RGB_TO_V(r1, g1, b1, shift)\
779 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
780 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
782 #define RGB_TO_Y_CCIR(r, g, b) \
783 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
784 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
786 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
787 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
788 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
790 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
791 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
792 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
794 static uint8_t y_ccir_to_jpeg[256];
795 static uint8_t y_jpeg_to_ccir[256];
796 static uint8_t c_ccir_to_jpeg[256];
797 static uint8_t c_jpeg_to_ccir[256];
799 /* init various conversion tables */
800 static void img_convert_init(void)
803 uint8_t *cm = cropTbl + MAX_NEG_CROP;
805 for(i = 0;i < 256; i++) {
806 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
807 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
808 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
809 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
813 /* apply to each pixel the given table */
814 static void img_apply_table(uint8_t *dst, int dst_wrap,
815 const uint8_t *src, int src_wrap,
816 int width, int height, const uint8_t *table1)
821 const uint8_t *table;
824 for(;height > 0; height--) {
848 /* XXX: use generic filter ? */
849 /* XXX: in most cases, the sampling position is incorrect */
852 static void shrink41(uint8_t *dst, int dst_wrap,
853 const uint8_t *src, int src_wrap,
854 int width, int height)
860 for(;height > 0; height--) {
863 for(w = width;w > 0; w--) {
864 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
874 static void shrink21(uint8_t *dst, int dst_wrap,
875 const uint8_t *src, int src_wrap,
876 int width, int height)
882 for(;height > 0; height--) {
885 for(w = width;w > 0; w--) {
886 d[0] = (s[0] + s[1]) >> 1;
896 static void shrink12(uint8_t *dst, int dst_wrap,
897 const uint8_t *src, int src_wrap,
898 int width, int height)
902 const uint8_t *s1, *s2;
904 for(;height > 0; height--) {
908 for(w = width;w >= 4; w-=4) {
909 d[0] = (s1[0] + s2[0]) >> 1;
910 d[1] = (s1[1] + s2[1]) >> 1;
911 d[2] = (s1[2] + s2[2]) >> 1;
912 d[3] = (s1[3] + s2[3]) >> 1;
918 d[0] = (s1[0] + s2[0]) >> 1;
929 static void shrink22(uint8_t *dst, int dst_wrap,
930 const uint8_t *src, int src_wrap,
931 int width, int height)
934 const uint8_t *s1, *s2;
937 for(;height > 0; height--) {
941 for(w = width;w >= 4; w-=4) {
942 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
943 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
944 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
945 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
951 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
962 static void shrink44(uint8_t *dst, int dst_wrap,
963 const uint8_t *src, int src_wrap,
964 int width, int height)
967 const uint8_t *s1, *s2, *s3, *s4;
970 for(;height > 0; height--) {
976 for(w = width;w > 0; w--) {
977 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
978 s2[0] + s2[1] + s2[2] + s2[3] +
979 s3[0] + s3[1] + s3[2] + s3[3] +
980 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
992 static void grow21_line(uint8_t *dst, const uint8_t *src,
1001 for(w = width;w >= 4; w-=4) {
1002 d[1] = d[0] = s1[0];
1003 d[3] = d[2] = s1[1];
1007 for(;w >= 2; w -= 2) {
1008 d[1] = d[0] = s1[0];
1012 /* only needed if width is not a multiple of two */
1013 /* XXX: veryfy that */
1019 static void grow41_line(uint8_t *dst, const uint8_t *src,
1028 for(w = width;w >= 4; w-=4) {
1040 static void grow21(uint8_t *dst, int dst_wrap,
1041 const uint8_t *src, int src_wrap,
1042 int width, int height)
1044 for(;height > 0; height--) {
1045 grow21_line(dst, src, width);
1052 static void grow22(uint8_t *dst, int dst_wrap,
1053 const uint8_t *src, int src_wrap,
1054 int width, int height)
1056 for(;height > 0; height--) {
1057 grow21_line(dst, src, width);
1065 static void grow41(uint8_t *dst, int dst_wrap,
1066 const uint8_t *src, int src_wrap,
1067 int width, int height)
1069 for(;height > 0; height--) {
1070 grow41_line(dst, src, width);
1077 static void grow44(uint8_t *dst, int dst_wrap,
1078 const uint8_t *src, int src_wrap,
1079 int width, int height)
1081 for(;height > 0; height--) {
1082 grow41_line(dst, src, width);
1083 if ((height & 3) == 1)
1090 static void conv411(uint8_t *dst, int dst_wrap,
1091 const uint8_t *src, int src_wrap,
1092 int width, int height)
1095 const uint8_t *s1, *s2;
1100 for(;height > 0; height--) {
1102 s2 = src + src_wrap;
1104 for(w = width;w > 0; w--) {
1105 c = (s1[0] + s2[0]) >> 1;
1112 src += src_wrap * 2;
1117 /* XXX: add jpeg quantize code */
1119 #define TRANSP_INDEX (6*6*6)
1121 /* this is maybe slow, but allows for extensions */
1122 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1124 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1127 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1130 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1133 pal = (uint32_t *)palette;
1135 for(r = 0; r < 6; r++) {
1136 for(g = 0; g < 6; g++) {
1137 for(b = 0; b < 6; b++) {
1138 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1139 (pal_value[g] << 8) | pal_value[b];
1146 pal[i++] = 0xff000000;
1149 /* copy bit n to bits 0 ... n - 1 */
1150 static inline unsigned int bitcopy_n(unsigned int a, int n)
1153 mask = (1 << n) - 1;
1154 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1157 /* rgb555 handling */
1159 #define RGB_NAME rgb555
1161 #define RGB_IN(r, g, b, s)\
1163 unsigned int v = ((const uint16_t *)(s))[0];\
1164 r = bitcopy_n(v >> (10 - 3), 3);\
1165 g = bitcopy_n(v >> (5 - 3), 3);\
1166 b = bitcopy_n(v << 3, 3);\
1169 #define RGBA_IN(r, g, b, a, s)\
1171 unsigned int v = ((const uint16_t *)(s))[0];\
1172 r = bitcopy_n(v >> (10 - 3), 3);\
1173 g = bitcopy_n(v >> (5 - 3), 3);\
1174 b = bitcopy_n(v << 3, 3);\
1175 a = (-(v >> 15)) & 0xff;\
1178 #define RGBA_OUT(d, r, g, b, a)\
1180 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1181 ((a << 8) & 0x8000);\
1186 #include "imgconvert_template.h"
1188 /* rgb565 handling */
1190 #define RGB_NAME rgb565
1192 #define RGB_IN(r, g, b, s)\
1194 unsigned int v = ((const uint16_t *)(s))[0];\
1195 r = bitcopy_n(v >> (11 - 3), 3);\
1196 g = bitcopy_n(v >> (5 - 2), 2);\
1197 b = bitcopy_n(v << 3, 3);\
1200 #define RGB_OUT(d, r, g, b)\
1202 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1207 #include "imgconvert_template.h"
1209 /* bgr24 handling */
1211 #define RGB_NAME bgr24
1213 #define RGB_IN(r, g, b, s)\
1220 #define RGB_OUT(d, r, g, b)\
1229 #include "imgconvert_template.h"
1235 /* rgb24 handling */
1237 #define RGB_NAME rgb24
1240 #define RGB_IN(r, g, b, s)\
1247 #define RGB_OUT(d, r, g, b)\
1256 #include "imgconvert_template.h"
1258 /* rgba32 handling */
1260 #define RGB_NAME rgba32
1263 #define RGB_IN(r, g, b, s)\
1265 unsigned int v = ((const uint32_t *)(s))[0];\
1266 r = (v >> 16) & 0xff;\
1267 g = (v >> 8) & 0xff;\
1271 #define RGBA_IN(r, g, b, a, s)\
1273 unsigned int v = ((const uint32_t *)(s))[0];\
1274 a = (v >> 24) & 0xff;\
1275 r = (v >> 16) & 0xff;\
1276 g = (v >> 8) & 0xff;\
1280 #define RGBA_OUT(d, r, g, b, a)\
1282 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1287 #include "imgconvert_template.h"
1289 static void mono_to_gray(AVPicture *dst, AVPicture *src,
1290 int width, int height, int xor_mask)
1292 const unsigned char *p;
1294 int v, dst_wrap, src_wrap;
1298 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1301 dst_wrap = dst->linesize[0] - width;
1302 for(y=0;y<height;y++) {
1305 v = *p++ ^ xor_mask;
1307 q[1] = -((v >> 6) & 1);
1308 q[2] = -((v >> 5) & 1);
1309 q[3] = -((v >> 4) & 1);
1310 q[4] = -((v >> 3) & 1);
1311 q[5] = -((v >> 2) & 1);
1312 q[6] = -((v >> 1) & 1);
1313 q[7] = -((v >> 0) & 1);
1318 v = *p++ ^ xor_mask;
1320 q[0] = -((v >> 7) & 1);
1330 static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
1331 int width, int height)
1333 mono_to_gray(dst, src, width, height, 0xff);
1336 static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
1337 int width, int height)
1339 mono_to_gray(dst, src, width, height, 0x00);
1342 static void gray_to_mono(AVPicture *dst, AVPicture *src,
1343 int width, int height, int xor_mask)
1348 int j, b, v, n1, src_wrap, dst_wrap, y;
1351 src_wrap = src->linesize[0] - width;
1354 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1356 for(y=0;y<height;y++) {
1363 v = (v << 1) | (b >> 7);
1365 d[0] = v ^ xor_mask;
1375 v = (v << 1) | (b >> 7);
1378 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1386 static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
1387 int width, int height)
1389 gray_to_mono(dst, src, width, height, 0xff);
1392 static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
1393 int width, int height)
1395 gray_to_mono(dst, src, width, height, 0x00);
1398 typedef struct ConvertEntry {
1399 void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
1402 /* Add each new convertion function in this table. In order to be able
1403 to convert from any format to any format, the following constraints
1406 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1408 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1410 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1412 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1415 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1417 The other conversion functions are just optimisations for common cases.
1419 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1420 [PIX_FMT_YUV420P] = {
1421 [PIX_FMT_RGB555] = {
1422 .convert = yuv420p_to_rgb555
1424 [PIX_FMT_RGB565] = {
1425 .convert = yuv420p_to_rgb565
1428 .convert = yuv420p_to_bgr24
1431 .convert = yuv420p_to_rgb24
1433 [PIX_FMT_RGBA32] = {
1434 .convert = yuv420p_to_rgba32
1437 [PIX_FMT_YUV422P] = {
1438 [PIX_FMT_YUV422] = {
1439 .convert = yuv422p_to_yuv422,
1442 [PIX_FMT_YUV444P] = {
1444 .convert = yuv444p_to_rgb24
1447 [PIX_FMT_YUVJ420P] = {
1448 [PIX_FMT_RGB555] = {
1449 .convert = yuvj420p_to_rgb555
1451 [PIX_FMT_RGB565] = {
1452 .convert = yuvj420p_to_rgb565
1455 .convert = yuvj420p_to_bgr24
1458 .convert = yuvj420p_to_rgb24
1460 [PIX_FMT_RGBA32] = {
1461 .convert = yuvj420p_to_rgba32
1464 [PIX_FMT_YUVJ444P] = {
1466 .convert = yuvj444p_to_rgb24
1469 [PIX_FMT_YUV422] = {
1470 [PIX_FMT_YUV420P] = {
1471 .convert = yuv422_to_yuv420p,
1473 [PIX_FMT_YUV422P] = {
1474 .convert = yuv422_to_yuv422p,
1479 [PIX_FMT_YUV420P] = {
1480 .convert = rgb24_to_yuv420p
1482 [PIX_FMT_RGB565] = {
1483 .convert = rgb24_to_rgb565
1485 [PIX_FMT_RGB555] = {
1486 .convert = rgb24_to_rgb555
1488 [PIX_FMT_RGBA32] = {
1489 .convert = rgb24_to_rgba32
1492 .convert = rgb24_to_bgr24
1495 .convert = rgb24_to_gray
1498 .convert = rgb24_to_pal8
1500 [PIX_FMT_YUV444P] = {
1501 .convert = rgb24_to_yuv444p
1503 [PIX_FMT_YUVJ420P] = {
1504 .convert = rgb24_to_yuvj420p
1506 [PIX_FMT_YUVJ444P] = {
1507 .convert = rgb24_to_yuvj444p
1510 [PIX_FMT_RGBA32] = {
1512 .convert = rgba32_to_rgb24
1514 [PIX_FMT_RGB555] = {
1515 .convert = rgba32_to_rgb555
1518 .convert = rgba32_to_pal8
1520 [PIX_FMT_YUV420P] = {
1521 .convert = rgba32_to_yuv420p
1524 .convert = rgba32_to_gray
1529 .convert = bgr24_to_rgb24
1531 [PIX_FMT_YUV420P] = {
1532 .convert = bgr24_to_yuv420p
1535 .convert = bgr24_to_gray
1538 [PIX_FMT_RGB555] = {
1540 .convert = rgb555_to_rgb24
1542 [PIX_FMT_RGBA32] = {
1543 .convert = rgb555_to_rgba32
1545 [PIX_FMT_YUV420P] = {
1546 .convert = rgb555_to_yuv420p
1549 .convert = rgb555_to_gray
1552 [PIX_FMT_RGB565] = {
1554 .convert = rgb565_to_rgb24
1556 [PIX_FMT_YUV420P] = {
1557 .convert = rgb565_to_yuv420p
1560 .convert = rgb565_to_gray
1564 [PIX_FMT_RGB555] = {
1565 .convert = gray_to_rgb555
1567 [PIX_FMT_RGB565] = {
1568 .convert = gray_to_rgb565
1571 .convert = gray_to_rgb24
1574 .convert = gray_to_bgr24
1576 [PIX_FMT_RGBA32] = {
1577 .convert = gray_to_rgba32
1579 [PIX_FMT_MONOWHITE] = {
1580 .convert = gray_to_monowhite
1582 [PIX_FMT_MONOBLACK] = {
1583 .convert = gray_to_monoblack
1586 [PIX_FMT_MONOWHITE] = {
1588 .convert = monowhite_to_gray
1591 [PIX_FMT_MONOBLACK] = {
1593 .convert = monoblack_to_gray
1597 [PIX_FMT_RGB555] = {
1598 .convert = pal8_to_rgb555
1600 [PIX_FMT_RGB565] = {
1601 .convert = pal8_to_rgb565
1604 .convert = pal8_to_bgr24
1607 .convert = pal8_to_rgb24
1609 [PIX_FMT_RGBA32] = {
1610 .convert = pal8_to_rgba32
1615 static int avpicture_alloc(AVPicture *picture,
1616 int pix_fmt, int width, int height)
1621 size = avpicture_get_size(pix_fmt, width, height);
1622 ptr = av_malloc(size);
1625 avpicture_fill(picture, ptr, pix_fmt, width, height);
1628 memset(picture, 0, sizeof(AVPicture));
1632 static void avpicture_free(AVPicture *picture)
1634 av_free(picture->data[0]);
1637 /* return true if yuv planar */
1638 static inline int is_yuv_planar(PixFmtInfo *ps)
1640 return (ps->color_type == FF_COLOR_YUV ||
1641 ps->color_type == FF_COLOR_YUV_JPEG) &&
1642 ps->pixel_type == FF_PIXEL_PLANAR;
1645 /* XXX: always use linesize. Return -1 if not supported */
1646 int img_convert(AVPicture *dst, int dst_pix_fmt,
1647 AVPicture *src, int src_pix_fmt,
1648 int src_width, int src_height)
1651 int i, ret, dst_width, dst_height, int_pix_fmt;
1652 PixFmtInfo *src_pix, *dst_pix;
1654 AVPicture tmp1, *tmp = &tmp1;
1656 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1657 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1659 if (src_width <= 0 || src_height <= 0)
1667 dst_width = src_width;
1668 dst_height = src_height;
1670 dst_pix = &pix_fmt_info[dst_pix_fmt];
1671 src_pix = &pix_fmt_info[src_pix_fmt];
1672 if (src_pix_fmt == dst_pix_fmt) {
1673 /* no conversion needed: just copy */
1674 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
1678 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1680 /* specific convertion routine */
1681 ce->convert(dst, src, dst_width, dst_height);
1686 if (is_yuv_planar(dst_pix) &&
1687 src_pix_fmt == PIX_FMT_GRAY8) {
1691 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
1692 img_copy_plane(dst->data[0], dst->linesize[0],
1693 src->data[0], src->linesize[0],
1694 dst_width, dst_height);
1696 img_apply_table(dst->data[0], dst->linesize[0],
1697 src->data[0], src->linesize[0],
1698 dst_width, dst_height,
1701 /* fill U and V with 128 */
1704 w >>= dst_pix->x_chroma_shift;
1705 h >>= dst_pix->y_chroma_shift;
1706 for(i = 1; i <= 2; i++) {
1708 for(y = 0; y< h; y++) {
1710 d += dst->linesize[i];
1717 if (is_yuv_planar(src_pix) &&
1718 dst_pix_fmt == PIX_FMT_GRAY8) {
1719 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
1720 img_copy_plane(dst->data[0], dst->linesize[0],
1721 src->data[0], src->linesize[0],
1722 dst_width, dst_height);
1724 img_apply_table(dst->data[0], dst->linesize[0],
1725 src->data[0], src->linesize[0],
1726 dst_width, dst_height,
1732 /* YUV to YUV planar */
1733 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
1734 int x_shift, y_shift, w, h, xy_shift;
1735 void (*resize_func)(uint8_t *dst, int dst_wrap,
1736 const uint8_t *src, int src_wrap,
1737 int width, int height);
1739 /* compute chroma size of the smallest dimensions */
1742 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1743 w >>= dst_pix->x_chroma_shift;
1745 w >>= src_pix->x_chroma_shift;
1746 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1747 h >>= dst_pix->y_chroma_shift;
1749 h >>= src_pix->y_chroma_shift;
1751 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1752 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
1753 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
1754 /* there must be filters for conversion at least from and to
1758 resize_func = img_copy_plane;
1761 resize_func = shrink21;
1764 resize_func = shrink41;
1767 resize_func = shrink12;
1770 resize_func = shrink22;
1773 resize_func = shrink44;
1776 resize_func = grow21;
1779 resize_func = grow41;
1782 resize_func = grow22;
1785 resize_func = grow44;
1788 resize_func = conv411;
1791 /* currently not handled */
1792 goto no_chroma_filter;
1795 img_copy_plane(dst->data[0], dst->linesize[0],
1796 src->data[0], src->linesize[0],
1797 dst_width, dst_height);
1799 for(i = 1;i <= 2; i++)
1800 resize_func(dst->data[i], dst->linesize[i],
1801 src->data[i], src->linesize[i],
1802 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
1803 /* if yuv color space conversion is needed, we do it here on
1804 the destination image */
1805 if (dst_pix->color_type != src_pix->color_type) {
1806 const uint8_t *y_table, *c_table;
1807 if (dst_pix->color_type == FF_COLOR_YUV) {
1808 y_table = y_jpeg_to_ccir;
1809 c_table = c_jpeg_to_ccir;
1811 y_table = y_ccir_to_jpeg;
1812 c_table = c_ccir_to_jpeg;
1814 img_apply_table(dst->data[0], dst->linesize[0],
1815 dst->data[0], dst->linesize[0],
1816 dst_width, dst_height,
1819 for(i = 1;i <= 2; i++)
1820 img_apply_table(dst->data[i], dst->linesize[i],
1821 dst->data[i], dst->linesize[i],
1822 dst_width>>dst_pix->x_chroma_shift,
1823 dst_height>>dst_pix->y_chroma_shift,
1830 /* try to use an intermediate format */
1831 if (src_pix_fmt == PIX_FMT_YUV422 ||
1832 dst_pix_fmt == PIX_FMT_YUV422) {
1833 /* specific case: convert to YUV422P first */
1834 int_pix_fmt = PIX_FMT_YUV422P;
1835 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
1836 src_pix_fmt != PIX_FMT_GRAY8) ||
1837 (dst_pix->color_type == FF_COLOR_GRAY &&
1838 dst_pix_fmt != PIX_FMT_GRAY8)) {
1839 /* gray8 is the normalized format */
1840 int_pix_fmt = PIX_FMT_GRAY8;
1841 } else if ((is_yuv_planar(src_pix) &&
1842 src_pix_fmt != PIX_FMT_YUV444P &&
1843 src_pix_fmt != PIX_FMT_YUVJ444P)) {
1844 /* yuv444 is the normalized format */
1845 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
1846 int_pix_fmt = PIX_FMT_YUVJ444P;
1848 int_pix_fmt = PIX_FMT_YUV444P;
1849 } else if ((is_yuv_planar(dst_pix) &&
1850 dst_pix_fmt != PIX_FMT_YUV444P &&
1851 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
1852 /* yuv444 is the normalized format */
1853 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
1854 int_pix_fmt = PIX_FMT_YUVJ444P;
1856 int_pix_fmt = PIX_FMT_YUV444P;
1858 /* the two formats are rgb or gray8 or yuv[j]444p */
1859 if (src_pix->is_alpha && dst_pix->is_alpha)
1860 int_pix_fmt = PIX_FMT_RGBA32;
1862 int_pix_fmt = PIX_FMT_RGB24;
1864 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
1867 if (img_convert(tmp, int_pix_fmt,
1868 src, src_pix_fmt, src_width, src_height) < 0)
1870 if (img_convert(dst, dst_pix_fmt,
1871 tmp, int_pix_fmt, dst_width, dst_height) < 0)
1875 avpicture_free(tmp);
1879 /* NOTE: we scan all the pixels to have an exact information */
1880 static int get_alpha_info_pal8(AVPicture *src, int width, int height)
1882 const unsigned char *p;
1883 int src_wrap, ret, x, y;
1885 uint32_t *palette = (uint32_t *)src->data[1];
1888 src_wrap = src->linesize[0] - width;
1890 for(y=0;y<height;y++) {
1891 for(x=0;x<width;x++) {
1892 a = palette[p[0]] >> 24;
1894 ret |= FF_ALPHA_TRANSP;
1895 } else if (a != 0xff) {
1896 ret |= FF_ALPHA_SEMI_TRANSP;
1906 * Tell if an image really has transparent alpha values.
1907 * @return ored mask of FF_ALPHA_xxx constants
1909 int img_get_alpha_info(AVPicture *src, int pix_fmt, int width, int height)
1911 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1914 pf = &pix_fmt_info[pix_fmt];
1915 /* no alpha can be represented in format */
1919 case PIX_FMT_RGBA32:
1920 ret = get_alpha_info_rgba32(src, width, height);
1922 case PIX_FMT_RGB555:
1923 ret = get_alpha_info_rgb555(src, width, height);
1926 ret = get_alpha_info_pal8(src, width, height);
1929 /* we do not know, so everything is indicated */
1930 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1937 #define DEINT_INPLACE_LINE_LUM \
1938 movd_m2r(lum_m4[0],mm0);\
1939 movd_m2r(lum_m3[0],mm1);\
1940 movd_m2r(lum_m2[0],mm2);\
1941 movd_m2r(lum_m1[0],mm3);\
1942 movd_m2r(lum[0],mm4);\
1943 punpcklbw_r2r(mm7,mm0);\
1944 movd_r2m(mm2,lum_m4[0]);\
1945 punpcklbw_r2r(mm7,mm1);\
1946 punpcklbw_r2r(mm7,mm2);\
1947 punpcklbw_r2r(mm7,mm3);\
1948 punpcklbw_r2r(mm7,mm4);\
1949 paddw_r2r(mm3,mm1);\
1951 paddw_r2r(mm4,mm0);\
1953 paddw_r2r(mm6,mm2);\
1954 paddw_r2r(mm2,mm1);\
1955 psubusw_r2r(mm0,mm1);\
1957 packuswb_r2r(mm7,mm1);\
1958 movd_r2m(mm1,lum_m2[0]);
1960 #define DEINT_LINE_LUM \
1961 movd_m2r(lum_m4[0],mm0);\
1962 movd_m2r(lum_m3[0],mm1);\
1963 movd_m2r(lum_m2[0],mm2);\
1964 movd_m2r(lum_m1[0],mm3);\
1965 movd_m2r(lum[0],mm4);\
1966 punpcklbw_r2r(mm7,mm0);\
1967 punpcklbw_r2r(mm7,mm1);\
1968 punpcklbw_r2r(mm7,mm2);\
1969 punpcklbw_r2r(mm7,mm3);\
1970 punpcklbw_r2r(mm7,mm4);\
1971 paddw_r2r(mm3,mm1);\
1973 paddw_r2r(mm4,mm0);\
1975 paddw_r2r(mm6,mm2);\
1976 paddw_r2r(mm2,mm1);\
1977 psubusw_r2r(mm0,mm1);\
1979 packuswb_r2r(mm7,mm1);\
1980 movd_r2m(mm1,dst[0]);
1983 /* filter parameters: [-1 4 2 4 -1] // 8 */
1984 static void deinterlace_line(uint8_t *dst, uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1988 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1991 for(;size > 0;size--) {
1993 sum += lum_m3[0] << 2;
1994 sum += lum_m2[0] << 1;
1995 sum += lum_m1[0] << 2;
1997 dst[0] = cm[(sum + 4) >> 3];
2014 movq_m2r(rounder,mm6);
2016 for (;size > 3; size-=4) {
2027 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2031 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2034 for(;size > 0;size--) {
2036 sum += lum_m3[0] << 2;
2037 sum += lum_m2[0] << 1;
2038 lum_m4[0]=lum_m2[0];
2039 sum += lum_m1[0] << 2;
2041 lum_m2[0] = cm[(sum + 4) >> 3];
2057 movq_m2r(rounder,mm6);
2059 for (;size > 3; size-=4) {
2060 DEINT_INPLACE_LINE_LUM
2070 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2071 top field is copied as is, but the bottom field is deinterlaced
2072 against the top field. */
2073 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2074 uint8_t *src1, int src_wrap,
2075 int width, int height)
2077 uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2082 src_0=&src_m1[src_wrap];
2083 src_p1=&src_0[src_wrap];
2084 src_p2=&src_p1[src_wrap];
2085 for(y=0;y<(height-2);y+=2) {
2086 memcpy(dst,src_m1,width);
2088 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2092 src_p1 += 2*src_wrap;
2093 src_p2 += 2*src_wrap;
2096 memcpy(dst,src_m1,width);
2099 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2102 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2103 int width, int height)
2105 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2108 buf = (uint8_t*)av_malloc(width);
2111 memcpy(buf,src_m1,width);
2112 src_0=&src_m1[src_wrap];
2113 src_p1=&src_0[src_wrap];
2114 src_p2=&src_p1[src_wrap];
2115 for(y=0;y<(height-2);y+=2) {
2116 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2119 src_p1 += 2*src_wrap;
2120 src_p2 += 2*src_wrap;
2123 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2128 /* deinterlace - if not supported return -1 */
2129 int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
2130 int pix_fmt, int width, int height)
2134 if (pix_fmt != PIX_FMT_YUV420P &&
2135 pix_fmt != PIX_FMT_YUV422P &&
2136 pix_fmt != PIX_FMT_YUV444P &&
2137 pix_fmt != PIX_FMT_YUV411P)
2139 if ((width & 3) != 0 || (height & 3) != 0)
2145 case PIX_FMT_YUV420P:
2149 case PIX_FMT_YUV422P:
2152 case PIX_FMT_YUV411P:
2160 deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
2163 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2164 src->data[i], src->linesize[i],