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) {
328 if (pix_fmt == PIX_FMT_YUV422 || pix_fmt == PIX_FMT_RGB565 ||
329 pix_fmt == PIX_FMT_RGB555)
332 w = width * (pf->depth * pf->nb_channels / 8);
336 data_planes = pf->nb_channels;
341 for (i=0; i<data_planes; i++) {
343 w = width >> pf->x_chroma_shift;
344 h = height >> pf->y_chroma_shift;
350 s += src->linesize[i];
357 int avpicture_get_size(int pix_fmt, int width, int height)
359 AVPicture dummy_pict;
360 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
364 * compute the loss when converting from a pixel format to another
366 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
369 const PixFmtInfo *pf, *ps;
372 ps = &pix_fmt_info[src_pix_fmt];
373 pf = &pix_fmt_info[dst_pix_fmt];
377 pf = &pix_fmt_info[dst_pix_fmt];
378 if (pf->depth < ps->depth ||
379 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
380 loss |= FF_LOSS_DEPTH;
381 if (pf->x_chroma_shift > ps->x_chroma_shift ||
382 pf->y_chroma_shift > ps->y_chroma_shift)
383 loss |= FF_LOSS_RESOLUTION;
384 switch(pf->color_type) {
386 if (ps->color_type != FF_COLOR_RGB &&
387 ps->color_type != FF_COLOR_GRAY)
388 loss |= FF_LOSS_COLORSPACE;
391 if (ps->color_type != FF_COLOR_GRAY)
392 loss |= FF_LOSS_COLORSPACE;
395 if (ps->color_type != FF_COLOR_YUV)
396 loss |= FF_LOSS_COLORSPACE;
398 case FF_COLOR_YUV_JPEG:
399 if (ps->color_type != FF_COLOR_YUV_JPEG &&
400 ps->color_type != FF_COLOR_YUV &&
401 ps->color_type != FF_COLOR_GRAY)
402 loss |= FF_LOSS_COLORSPACE;
406 if (ps->color_type != pf->color_type)
407 loss |= FF_LOSS_COLORSPACE;
410 if (pf->color_type == FF_COLOR_GRAY &&
411 ps->color_type != FF_COLOR_GRAY)
412 loss |= FF_LOSS_CHROMA;
413 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
414 loss |= FF_LOSS_ALPHA;
415 if (pf->pixel_type == FF_PIXEL_PALETTE &&
416 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
417 loss |= FF_LOSS_COLORQUANT;
421 static int avg_bits_per_pixel(int pix_fmt)
424 const PixFmtInfo *pf;
426 pf = &pix_fmt_info[pix_fmt];
427 switch(pf->pixel_type) {
428 case FF_PIXEL_PACKED:
436 bits = pf->depth * pf->nb_channels;
440 case FF_PIXEL_PLANAR:
441 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
442 bits = pf->depth * pf->nb_channels;
444 bits = pf->depth + ((2 * pf->depth) >>
445 (pf->x_chroma_shift + pf->y_chroma_shift));
448 case FF_PIXEL_PALETTE:
458 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
463 int dist, i, loss, min_dist, dst_pix_fmt;
465 /* find exact color match with smallest size */
467 min_dist = 0x7fffffff;
468 for(i = 0;i < PIX_FMT_NB; i++) {
469 if (pix_fmt_mask & (1 << i)) {
470 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
472 dist = avg_bits_per_pixel(i);
473 if (dist < min_dist) {
484 * find best pixel format to convert to. Return -1 if none found
486 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
487 int has_alpha, int *loss_ptr)
489 int dst_pix_fmt, loss_mask, i;
490 static const int loss_mask_order[] = {
491 ~0, /* no loss first */
494 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
500 /* try with successive loss */
503 loss_mask = loss_mask_order[i++];
504 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
505 has_alpha, loss_mask);
506 if (dst_pix_fmt >= 0)
514 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
518 static void img_copy_plane(uint8_t *dst, int dst_wrap,
519 const uint8_t *src, int src_wrap,
520 int width, int height)
522 for(;height > 0; height--) {
523 memcpy(dst, src, width);
530 * Copy image 'src' to 'dst'.
532 void img_copy(AVPicture *dst, AVPicture *src,
533 int pix_fmt, int width, int height)
536 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
538 pf = &pix_fmt_info[pix_fmt];
539 switch(pf->pixel_type) {
540 case FF_PIXEL_PACKED:
548 bits = pf->depth * pf->nb_channels;
551 bwidth = (width * bits + 7) >> 3;
552 img_copy_plane(dst->data[0], dst->linesize[0],
553 src->data[0], src->linesize[0],
556 case FF_PIXEL_PLANAR:
557 for(i = 0; i < pf->nb_channels; i++) {
561 if (i == 1 || i == 2) {
562 w >>= pf->x_chroma_shift;
563 h >>= pf->y_chroma_shift;
565 bwidth = (w * pf->depth + 7) >> 3;
566 img_copy_plane(dst->data[i], dst->linesize[i],
567 src->data[i], src->linesize[i],
571 case FF_PIXEL_PALETTE:
572 img_copy_plane(dst->data[0], dst->linesize[0],
573 src->data[0], src->linesize[0],
575 /* copy the palette */
576 img_copy_plane(dst->data[1], dst->linesize[1],
577 src->data[1], src->linesize[1],
583 /* XXX: totally non optimized */
585 static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
586 int width, int height)
588 const uint8_t *p, *p1;
589 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
597 for(;height >= 2; height -= 2) {
602 for(x=0;x<width;x+=2) {
612 p1 += src->linesize[0];
613 lum1 += dst->linesize[0];
616 for(x=0;x<width;x+=2) {
622 p1 += src->linesize[0];
623 lum1 += dst->linesize[0];
624 cb1 += dst->linesize[1];
625 cr1 += dst->linesize[2];
629 static void yuv422_to_yuv422p(AVPicture *dst, AVPicture *src,
630 int width, int height)
632 const uint8_t *p, *p1;
633 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
640 for(;height > 0; height--) {
645 for(w = width; w >= 2; w -= 2) {
655 p1 += src->linesize[0];
656 lum1 += dst->linesize[0];
657 cb1 += dst->linesize[1];
658 cr1 += dst->linesize[2];
662 static void yuv422p_to_yuv422(AVPicture *dst, AVPicture *src,
663 int width, int height)
666 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
673 for(;height > 0; height--) {
678 for(w = width; w >= 2; w -= 2) {
688 p1 += dst->linesize[0];
689 lum1 += src->linesize[0];
690 cb1 += src->linesize[1];
691 cr1 += src->linesize[2];
696 #define ONE_HALF (1 << (SCALEBITS - 1))
697 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
699 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
703 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
704 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
706 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
709 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
711 y = ((y1) - 16) * FIX(255.0/219.0);\
712 r = cm[(y + r_add) >> SCALEBITS];\
713 g = cm[(y + g_add) >> SCALEBITS];\
714 b = cm[(y + b_add) >> SCALEBITS];\
717 #define YUV_TO_RGB1(cb1, cr1)\
721 r_add = FIX(1.40200) * cr + ONE_HALF;\
722 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
723 b_add = FIX(1.77200) * cb + ONE_HALF;\
726 #define YUV_TO_RGB2(r, g, b, y1)\
728 y = (y1) << SCALEBITS;\
729 r = cm[(y + r_add) >> SCALEBITS];\
730 g = cm[(y + g_add) >> SCALEBITS];\
731 b = cm[(y + b_add) >> SCALEBITS];\
734 #define Y_CCIR_TO_JPEG(y)\
735 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
737 #define Y_JPEG_TO_CCIR(y)\
738 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
740 #define C_CCIR_TO_JPEG(y)\
741 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
743 /* NOTE: the clamp is really necessary! */
744 #define C_JPEG_TO_CCIR(y)\
747 __y = ((((y) - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);\
753 #define RGB_TO_Y(r, g, b) \
754 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
755 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
757 #define RGB_TO_U(r1, g1, b1, shift)\
758 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
759 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
761 #define RGB_TO_V(r1, g1, b1, shift)\
762 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
763 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
765 #define RGB_TO_Y_CCIR(r, g, b) \
766 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
767 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
769 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
770 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
771 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
773 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
774 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
775 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
777 static uint8_t y_ccir_to_jpeg[256];
778 static uint8_t y_jpeg_to_ccir[256];
779 static uint8_t c_ccir_to_jpeg[256];
780 static uint8_t c_jpeg_to_ccir[256];
782 /* init various conversion tables */
783 static void img_convert_init(void)
786 uint8_t *cm = cropTbl + MAX_NEG_CROP;
788 for(i = 0;i < 256; i++) {
789 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
790 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
791 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
792 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
796 /* apply to each pixel the given table */
797 static void img_apply_table(uint8_t *dst, int dst_wrap,
798 const uint8_t *src, int src_wrap,
799 int width, int height, const uint8_t *table1)
804 const uint8_t *table;
807 for(;height > 0; height--) {
831 /* XXX: use generic filter ? */
832 /* XXX: in most cases, the sampling position is incorrect */
835 static void shrink41(uint8_t *dst, int dst_wrap,
836 const uint8_t *src, int src_wrap,
837 int width, int height)
843 for(;height > 0; height--) {
846 for(w = width;w > 0; w--) {
847 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
857 static void shrink21(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]) >> 1;
879 static void shrink12(uint8_t *dst, int dst_wrap,
880 const uint8_t *src, int src_wrap,
881 int width, int height)
885 const uint8_t *s1, *s2;
887 for(;height > 0; height--) {
891 for(w = width;w >= 4; w-=4) {
892 d[0] = (s1[0] + s2[0]) >> 1;
893 d[1] = (s1[1] + s2[1]) >> 1;
894 d[2] = (s1[2] + s2[2]) >> 1;
895 d[3] = (s1[3] + s2[3]) >> 1;
901 d[0] = (s1[0] + s2[0]) >> 1;
912 static void shrink22(uint8_t *dst, int dst_wrap,
913 const uint8_t *src, int src_wrap,
914 int width, int height)
917 const uint8_t *s1, *s2;
920 for(;height > 0; height--) {
924 for(w = width;w >= 4; w-=4) {
925 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
926 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
927 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
928 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
934 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
945 static void shrink44(uint8_t *dst, int dst_wrap,
946 const uint8_t *src, int src_wrap,
947 int width, int height)
950 const uint8_t *s1, *s2, *s3, *s4;
953 for(;height > 0; height--) {
959 for(w = width;w > 0; w--) {
960 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
961 s2[0] + s2[1] + s2[2] + s2[3] +
962 s3[0] + s3[1] + s3[2] + s3[3] +
963 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
975 static void grow21_line(uint8_t *dst, const uint8_t *src,
984 for(w = width;w >= 4; w-=4) {
990 for(;w >= 2; w -= 2) {
995 /* only needed if width is not a multiple of two */
996 /* XXX: veryfy that */
1002 static void grow41_line(uint8_t *dst, const uint8_t *src,
1011 for(w = width;w >= 4; w-=4) {
1023 static void grow21(uint8_t *dst, int dst_wrap,
1024 const uint8_t *src, int src_wrap,
1025 int width, int height)
1027 for(;height > 0; height--) {
1028 grow21_line(dst, src, width);
1035 static void grow22(uint8_t *dst, int dst_wrap,
1036 const uint8_t *src, int src_wrap,
1037 int width, int height)
1039 for(;height > 0; height--) {
1040 grow21_line(dst, src, width);
1048 static void grow41(uint8_t *dst, int dst_wrap,
1049 const uint8_t *src, int src_wrap,
1050 int width, int height)
1052 for(;height > 0; height--) {
1053 grow41_line(dst, src, width);
1060 static void grow44(uint8_t *dst, int dst_wrap,
1061 const uint8_t *src, int src_wrap,
1062 int width, int height)
1064 for(;height > 0; height--) {
1065 grow41_line(dst, src, width);
1066 if ((height & 3) == 1)
1073 static void conv411(uint8_t *dst, int dst_wrap,
1074 const uint8_t *src, int src_wrap,
1075 int width, int height)
1078 const uint8_t *s1, *s2;
1083 for(;height > 0; height--) {
1085 s2 = src + src_wrap;
1087 for(w = width;w > 0; w--) {
1088 c = (s1[0] + s2[0]) >> 1;
1095 src += src_wrap * 2;
1100 /* XXX: add jpeg quantize code */
1102 #define TRANSP_INDEX (6*6*6)
1104 /* this is maybe slow, but allows for extensions */
1105 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1107 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1110 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1113 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1116 pal = (uint32_t *)palette;
1118 for(r = 0; r < 6; r++) {
1119 for(g = 0; g < 6; g++) {
1120 for(b = 0; b < 6; b++) {
1121 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1122 (pal_value[g] << 8) | pal_value[b];
1129 pal[i++] = 0xff000000;
1132 /* copy bit n to bits 0 ... n - 1 */
1133 static inline unsigned int bitcopy_n(unsigned int a, int n)
1136 mask = (1 << n) - 1;
1137 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1140 /* rgb555 handling */
1142 #define RGB_NAME rgb555
1144 #define RGB_IN(r, g, b, s)\
1146 unsigned int v = ((const uint16_t *)(s))[0];\
1147 r = bitcopy_n(v >> (10 - 3), 3);\
1148 g = bitcopy_n(v >> (5 - 3), 3);\
1149 b = bitcopy_n(v << 3, 3);\
1152 #define RGBA_IN(r, g, b, a, s)\
1154 unsigned int v = ((const uint16_t *)(s))[0];\
1155 r = bitcopy_n(v >> (10 - 3), 3);\
1156 g = bitcopy_n(v >> (5 - 3), 3);\
1157 b = bitcopy_n(v << 3, 3);\
1158 a = (-(v >> 15)) & 0xff;\
1161 #define RGBA_OUT(d, r, g, b, a)\
1163 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1164 ((a << 8) & 0x8000);\
1169 #include "imgconvert_template.h"
1171 /* rgb565 handling */
1173 #define RGB_NAME rgb565
1175 #define RGB_IN(r, g, b, s)\
1177 unsigned int v = ((const uint16_t *)(s))[0];\
1178 r = bitcopy_n(v >> (11 - 3), 3);\
1179 g = bitcopy_n(v >> (5 - 2), 2);\
1180 b = bitcopy_n(v << 3, 3);\
1183 #define RGB_OUT(d, r, g, b)\
1185 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1190 #include "imgconvert_template.h"
1192 /* bgr24 handling */
1194 #define RGB_NAME bgr24
1196 #define RGB_IN(r, g, b, s)\
1203 #define RGB_OUT(d, r, g, b)\
1212 #include "imgconvert_template.h"
1218 /* rgb24 handling */
1220 #define RGB_NAME rgb24
1223 #define RGB_IN(r, g, b, s)\
1230 #define RGB_OUT(d, r, g, b)\
1239 #include "imgconvert_template.h"
1241 /* rgba32 handling */
1243 #define RGB_NAME rgba32
1246 #define RGB_IN(r, g, b, s)\
1248 unsigned int v = ((const uint32_t *)(s))[0];\
1249 r = (v >> 16) & 0xff;\
1250 g = (v >> 8) & 0xff;\
1254 #define RGBA_IN(r, g, b, a, s)\
1256 unsigned int v = ((const uint32_t *)(s))[0];\
1257 a = (v >> 24) & 0xff;\
1258 r = (v >> 16) & 0xff;\
1259 g = (v >> 8) & 0xff;\
1263 #define RGBA_OUT(d, r, g, b, a)\
1265 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1270 #include "imgconvert_template.h"
1272 static void mono_to_gray(AVPicture *dst, AVPicture *src,
1273 int width, int height, int xor_mask)
1275 const unsigned char *p;
1277 int v, dst_wrap, src_wrap;
1281 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1284 dst_wrap = dst->linesize[0] - width;
1285 for(y=0;y<height;y++) {
1288 v = *p++ ^ xor_mask;
1290 q[1] = -((v >> 6) & 1);
1291 q[2] = -((v >> 5) & 1);
1292 q[3] = -((v >> 4) & 1);
1293 q[4] = -((v >> 3) & 1);
1294 q[5] = -((v >> 2) & 1);
1295 q[6] = -((v >> 1) & 1);
1296 q[7] = -((v >> 0) & 1);
1301 v = *p++ ^ xor_mask;
1303 q[0] = -((v >> 7) & 1);
1313 static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
1314 int width, int height)
1316 mono_to_gray(dst, src, width, height, 0xff);
1319 static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
1320 int width, int height)
1322 mono_to_gray(dst, src, width, height, 0x00);
1325 static void gray_to_mono(AVPicture *dst, AVPicture *src,
1326 int width, int height, int xor_mask)
1331 int j, b, v, n1, src_wrap, dst_wrap, y;
1334 src_wrap = src->linesize[0] - width;
1337 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1339 for(y=0;y<height;y++) {
1346 v = (v << 1) | (b >> 7);
1348 d[0] = v ^ xor_mask;
1358 v = (v << 1) | (b >> 7);
1361 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1369 static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
1370 int width, int height)
1372 gray_to_mono(dst, src, width, height, 0xff);
1375 static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
1376 int width, int height)
1378 gray_to_mono(dst, src, width, height, 0x00);
1381 typedef struct ConvertEntry {
1382 void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
1385 /* Add each new convertion function in this table. In order to be able
1386 to convert from any format to any format, the following constraints
1389 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1391 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1393 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1395 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1398 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1400 The other conversion functions are just optimisations for common cases.
1402 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1403 [PIX_FMT_YUV420P] = {
1404 [PIX_FMT_RGB555] = {
1405 .convert = yuv420p_to_rgb555
1407 [PIX_FMT_RGB565] = {
1408 .convert = yuv420p_to_rgb565
1411 .convert = yuv420p_to_bgr24
1414 .convert = yuv420p_to_rgb24
1416 [PIX_FMT_RGBA32] = {
1417 .convert = yuv420p_to_rgba32
1420 [PIX_FMT_YUV422P] = {
1421 [PIX_FMT_YUV422] = {
1422 .convert = yuv422p_to_yuv422,
1425 [PIX_FMT_YUV444P] = {
1427 .convert = yuv444p_to_rgb24
1430 [PIX_FMT_YUVJ420P] = {
1431 [PIX_FMT_RGB555] = {
1432 .convert = yuvj420p_to_rgb555
1434 [PIX_FMT_RGB565] = {
1435 .convert = yuvj420p_to_rgb565
1438 .convert = yuvj420p_to_bgr24
1441 .convert = yuvj420p_to_rgb24
1443 [PIX_FMT_RGBA32] = {
1444 .convert = yuvj420p_to_rgba32
1447 [PIX_FMT_YUVJ444P] = {
1449 .convert = yuvj444p_to_rgb24
1452 [PIX_FMT_YUV422] = {
1453 [PIX_FMT_YUV420P] = {
1454 .convert = yuv422_to_yuv420p,
1456 [PIX_FMT_YUV422P] = {
1457 .convert = yuv422_to_yuv422p,
1462 [PIX_FMT_YUV420P] = {
1463 .convert = rgb24_to_yuv420p
1465 [PIX_FMT_RGB565] = {
1466 .convert = rgb24_to_rgb565
1468 [PIX_FMT_RGB555] = {
1469 .convert = rgb24_to_rgb555
1471 [PIX_FMT_RGBA32] = {
1472 .convert = rgb24_to_rgba32
1475 .convert = rgb24_to_bgr24
1478 .convert = rgb24_to_gray
1481 .convert = rgb24_to_pal8
1483 [PIX_FMT_YUV444P] = {
1484 .convert = rgb24_to_yuv444p
1486 [PIX_FMT_YUVJ420P] = {
1487 .convert = rgb24_to_yuvj420p
1489 [PIX_FMT_YUVJ444P] = {
1490 .convert = rgb24_to_yuvj444p
1493 [PIX_FMT_RGBA32] = {
1495 .convert = rgba32_to_rgb24
1497 [PIX_FMT_RGB555] = {
1498 .convert = rgba32_to_rgb555
1501 .convert = rgba32_to_pal8
1503 [PIX_FMT_YUV420P] = {
1504 .convert = rgba32_to_yuv420p
1507 .convert = rgba32_to_gray
1512 .convert = bgr24_to_rgb24
1514 [PIX_FMT_YUV420P] = {
1515 .convert = bgr24_to_yuv420p
1518 .convert = bgr24_to_gray
1521 [PIX_FMT_RGB555] = {
1523 .convert = rgb555_to_rgb24
1525 [PIX_FMT_RGBA32] = {
1526 .convert = rgb555_to_rgba32
1528 [PIX_FMT_YUV420P] = {
1529 .convert = rgb555_to_yuv420p
1532 .convert = rgb555_to_gray
1535 [PIX_FMT_RGB565] = {
1537 .convert = rgb565_to_rgb24
1539 [PIX_FMT_YUV420P] = {
1540 .convert = rgb565_to_yuv420p
1543 .convert = rgb565_to_gray
1547 [PIX_FMT_RGB555] = {
1548 .convert = gray_to_rgb555
1550 [PIX_FMT_RGB565] = {
1551 .convert = gray_to_rgb565
1554 .convert = gray_to_rgb24
1557 .convert = gray_to_bgr24
1559 [PIX_FMT_RGBA32] = {
1560 .convert = gray_to_rgba32
1562 [PIX_FMT_MONOWHITE] = {
1563 .convert = gray_to_monowhite
1565 [PIX_FMT_MONOBLACK] = {
1566 .convert = gray_to_monoblack
1569 [PIX_FMT_MONOWHITE] = {
1571 .convert = monowhite_to_gray
1574 [PIX_FMT_MONOBLACK] = {
1576 .convert = monoblack_to_gray
1580 [PIX_FMT_RGB555] = {
1581 .convert = pal8_to_rgb555
1583 [PIX_FMT_RGB565] = {
1584 .convert = pal8_to_rgb565
1587 .convert = pal8_to_bgr24
1590 .convert = pal8_to_rgb24
1592 [PIX_FMT_RGBA32] = {
1593 .convert = pal8_to_rgba32
1598 static int avpicture_alloc(AVPicture *picture,
1599 int pix_fmt, int width, int height)
1604 size = avpicture_get_size(pix_fmt, width, height);
1607 ptr = av_malloc(size);
1610 avpicture_fill(picture, ptr, pix_fmt, width, height);
1613 memset(picture, 0, sizeof(AVPicture));
1617 static void avpicture_free(AVPicture *picture)
1619 av_free(picture->data[0]);
1622 /* return true if yuv planar */
1623 static inline int is_yuv_planar(PixFmtInfo *ps)
1625 return (ps->color_type == FF_COLOR_YUV ||
1626 ps->color_type == FF_COLOR_YUV_JPEG) &&
1627 ps->pixel_type == FF_PIXEL_PLANAR;
1630 /* XXX: always use linesize. Return -1 if not supported */
1631 int img_convert(AVPicture *dst, int dst_pix_fmt,
1632 AVPicture *src, int src_pix_fmt,
1633 int src_width, int src_height)
1636 int i, ret, dst_width, dst_height, int_pix_fmt;
1637 PixFmtInfo *src_pix, *dst_pix;
1639 AVPicture tmp1, *tmp = &tmp1;
1641 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1642 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1644 if (src_width <= 0 || src_height <= 0)
1652 dst_width = src_width;
1653 dst_height = src_height;
1655 dst_pix = &pix_fmt_info[dst_pix_fmt];
1656 src_pix = &pix_fmt_info[src_pix_fmt];
1657 if (src_pix_fmt == dst_pix_fmt) {
1658 /* no conversion needed: just copy */
1659 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
1663 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1665 /* specific convertion routine */
1666 ce->convert(dst, src, dst_width, dst_height);
1671 if (is_yuv_planar(dst_pix) &&
1672 src_pix_fmt == PIX_FMT_GRAY8) {
1676 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
1677 img_copy_plane(dst->data[0], dst->linesize[0],
1678 src->data[0], src->linesize[0],
1679 dst_width, dst_height);
1681 img_apply_table(dst->data[0], dst->linesize[0],
1682 src->data[0], src->linesize[0],
1683 dst_width, dst_height,
1686 /* fill U and V with 128 */
1689 w >>= dst_pix->x_chroma_shift;
1690 h >>= dst_pix->y_chroma_shift;
1691 for(i = 1; i <= 2; i++) {
1693 for(y = 0; y< h; y++) {
1695 d += dst->linesize[i];
1702 if (is_yuv_planar(src_pix) &&
1703 dst_pix_fmt == PIX_FMT_GRAY8) {
1704 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
1705 img_copy_plane(dst->data[0], dst->linesize[0],
1706 src->data[0], src->linesize[0],
1707 dst_width, dst_height);
1709 img_apply_table(dst->data[0], dst->linesize[0],
1710 src->data[0], src->linesize[0],
1711 dst_width, dst_height,
1717 /* YUV to YUV planar */
1718 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
1719 int x_shift, y_shift, w, h, xy_shift;
1720 void (*resize_func)(uint8_t *dst, int dst_wrap,
1721 const uint8_t *src, int src_wrap,
1722 int width, int height);
1724 /* compute chroma size of the smallest dimensions */
1727 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1728 w >>= dst_pix->x_chroma_shift;
1730 w >>= src_pix->x_chroma_shift;
1731 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1732 h >>= dst_pix->y_chroma_shift;
1734 h >>= src_pix->y_chroma_shift;
1736 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1737 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
1738 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
1739 /* there must be filters for conversion at least from and to
1743 resize_func = img_copy_plane;
1746 resize_func = shrink21;
1749 resize_func = shrink41;
1752 resize_func = shrink12;
1755 resize_func = shrink22;
1758 resize_func = shrink44;
1761 resize_func = grow21;
1764 resize_func = grow41;
1767 resize_func = grow22;
1770 resize_func = grow44;
1773 resize_func = conv411;
1776 /* currently not handled */
1777 goto no_chroma_filter;
1780 img_copy_plane(dst->data[0], dst->linesize[0],
1781 src->data[0], src->linesize[0],
1782 dst_width, dst_height);
1784 for(i = 1;i <= 2; i++)
1785 resize_func(dst->data[i], dst->linesize[i],
1786 src->data[i], src->linesize[i],
1787 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
1788 /* if yuv color space conversion is needed, we do it here on
1789 the destination image */
1790 if (dst_pix->color_type != src_pix->color_type) {
1791 const uint8_t *y_table, *c_table;
1792 if (dst_pix->color_type == FF_COLOR_YUV) {
1793 y_table = y_jpeg_to_ccir;
1794 c_table = c_jpeg_to_ccir;
1796 y_table = y_ccir_to_jpeg;
1797 c_table = c_ccir_to_jpeg;
1799 img_apply_table(dst->data[0], dst->linesize[0],
1800 dst->data[0], dst->linesize[0],
1801 dst_width, dst_height,
1804 for(i = 1;i <= 2; i++)
1805 img_apply_table(dst->data[i], dst->linesize[i],
1806 dst->data[i], dst->linesize[i],
1807 dst_width>>dst_pix->x_chroma_shift,
1808 dst_height>>dst_pix->y_chroma_shift,
1815 /* try to use an intermediate format */
1816 if (src_pix_fmt == PIX_FMT_YUV422 ||
1817 dst_pix_fmt == PIX_FMT_YUV422) {
1818 /* specific case: convert to YUV422P first */
1819 int_pix_fmt = PIX_FMT_YUV422P;
1820 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
1821 src_pix_fmt != PIX_FMT_GRAY8) ||
1822 (dst_pix->color_type == FF_COLOR_GRAY &&
1823 dst_pix_fmt != PIX_FMT_GRAY8)) {
1824 /* gray8 is the normalized format */
1825 int_pix_fmt = PIX_FMT_GRAY8;
1826 } else if ((is_yuv_planar(src_pix) &&
1827 src_pix_fmt != PIX_FMT_YUV444P &&
1828 src_pix_fmt != PIX_FMT_YUVJ444P)) {
1829 /* yuv444 is the normalized format */
1830 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
1831 int_pix_fmt = PIX_FMT_YUVJ444P;
1833 int_pix_fmt = PIX_FMT_YUV444P;
1834 } else if ((is_yuv_planar(dst_pix) &&
1835 dst_pix_fmt != PIX_FMT_YUV444P &&
1836 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
1837 /* yuv444 is the normalized format */
1838 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
1839 int_pix_fmt = PIX_FMT_YUVJ444P;
1841 int_pix_fmt = PIX_FMT_YUV444P;
1843 /* the two formats are rgb or gray8 or yuv[j]444p */
1844 if (src_pix->is_alpha && dst_pix->is_alpha)
1845 int_pix_fmt = PIX_FMT_RGBA32;
1847 int_pix_fmt = PIX_FMT_RGB24;
1849 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
1852 if (img_convert(tmp, int_pix_fmt,
1853 src, src_pix_fmt, src_width, src_height) < 0)
1855 if (img_convert(dst, dst_pix_fmt,
1856 tmp, int_pix_fmt, dst_width, dst_height) < 0)
1860 avpicture_free(tmp);
1864 /* NOTE: we scan all the pixels to have an exact information */
1865 static int get_alpha_info_pal8(AVPicture *src, int width, int height)
1867 const unsigned char *p;
1868 int src_wrap, ret, x, y;
1870 uint32_t *palette = (uint32_t *)src->data[1];
1873 src_wrap = src->linesize[0] - width;
1875 for(y=0;y<height;y++) {
1876 for(x=0;x<width;x++) {
1877 a = palette[p[0]] >> 24;
1879 ret |= FF_ALPHA_TRANSP;
1880 } else if (a != 0xff) {
1881 ret |= FF_ALPHA_SEMI_TRANSP;
1891 * Tell if an image really has transparent alpha values.
1892 * @return ored mask of FF_ALPHA_xxx constants
1894 int img_get_alpha_info(AVPicture *src, int pix_fmt, int width, int height)
1896 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1899 pf = &pix_fmt_info[pix_fmt];
1900 /* no alpha can be represented in format */
1904 case PIX_FMT_RGBA32:
1905 ret = get_alpha_info_rgba32(src, width, height);
1907 case PIX_FMT_RGB555:
1908 ret = get_alpha_info_rgb555(src, width, height);
1911 ret = get_alpha_info_pal8(src, width, height);
1914 /* we do not know, so everything is indicated */
1915 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1922 #define DEINT_INPLACE_LINE_LUM \
1923 movd_m2r(lum_m4[0],mm0);\
1924 movd_m2r(lum_m3[0],mm1);\
1925 movd_m2r(lum_m2[0],mm2);\
1926 movd_m2r(lum_m1[0],mm3);\
1927 movd_m2r(lum[0],mm4);\
1928 punpcklbw_r2r(mm7,mm0);\
1929 movd_r2m(mm2,lum_m4[0]);\
1930 punpcklbw_r2r(mm7,mm1);\
1931 punpcklbw_r2r(mm7,mm2);\
1932 punpcklbw_r2r(mm7,mm3);\
1933 punpcklbw_r2r(mm7,mm4);\
1934 paddw_r2r(mm3,mm1);\
1936 paddw_r2r(mm4,mm0);\
1938 paddw_r2r(mm6,mm2);\
1939 paddw_r2r(mm2,mm1);\
1940 psubusw_r2r(mm0,mm1);\
1942 packuswb_r2r(mm7,mm1);\
1943 movd_r2m(mm1,lum_m2[0]);
1945 #define DEINT_LINE_LUM \
1946 movd_m2r(lum_m4[0],mm0);\
1947 movd_m2r(lum_m3[0],mm1);\
1948 movd_m2r(lum_m2[0],mm2);\
1949 movd_m2r(lum_m1[0],mm3);\
1950 movd_m2r(lum[0],mm4);\
1951 punpcklbw_r2r(mm7,mm0);\
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,dst[0]);
1968 /* filter parameters: [-1 4 2 4 -1] // 8 */
1969 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,
1973 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1976 for(;size > 0;size--) {
1978 sum += lum_m3[0] << 2;
1979 sum += lum_m2[0] << 1;
1980 sum += lum_m1[0] << 2;
1982 dst[0] = cm[(sum + 4) >> 3];
1999 movq_m2r(rounder,mm6);
2001 for (;size > 3; size-=4) {
2012 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2016 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2019 for(;size > 0;size--) {
2021 sum += lum_m3[0] << 2;
2022 sum += lum_m2[0] << 1;
2023 lum_m4[0]=lum_m2[0];
2024 sum += lum_m1[0] << 2;
2026 lum_m2[0] = cm[(sum + 4) >> 3];
2042 movq_m2r(rounder,mm6);
2044 for (;size > 3; size-=4) {
2045 DEINT_INPLACE_LINE_LUM
2055 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2056 top field is copied as is, but the bottom field is deinterlaced
2057 against the top field. */
2058 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2059 uint8_t *src1, int src_wrap,
2060 int width, int height)
2062 uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2067 src_0=&src_m1[src_wrap];
2068 src_p1=&src_0[src_wrap];
2069 src_p2=&src_p1[src_wrap];
2070 for(y=0;y<(height-2);y+=2) {
2071 memcpy(dst,src_m1,width);
2073 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2077 src_p1 += 2*src_wrap;
2078 src_p2 += 2*src_wrap;
2081 memcpy(dst,src_m1,width);
2084 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2087 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2088 int width, int height)
2090 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2093 buf = (uint8_t*)av_malloc(width);
2096 memcpy(buf,src_m1,width);
2097 src_0=&src_m1[src_wrap];
2098 src_p1=&src_0[src_wrap];
2099 src_p2=&src_p1[src_wrap];
2100 for(y=0;y<(height-2);y+=2) {
2101 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2104 src_p1 += 2*src_wrap;
2105 src_p2 += 2*src_wrap;
2108 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2113 /* deinterlace - if not supported return -1 */
2114 int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
2115 int pix_fmt, int width, int height)
2119 if (pix_fmt != PIX_FMT_YUV420P &&
2120 pix_fmt != PIX_FMT_YUV422P &&
2121 pix_fmt != PIX_FMT_YUV444P)
2123 if ((width & 3) != 0 || (height & 3) != 0)
2129 case PIX_FMT_YUV420P:
2133 case PIX_FMT_YUV422P:
2141 deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
2144 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2145 src->data[i], src->linesize[i],