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 /* Picture field are filled with 'ptr' addresses. Also return size */
228 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
229 int pix_fmt, int width, int height)
231 int size, w2, h2, size2;
234 pinfo = &pix_fmt_info[pix_fmt];
235 size = width * height;
237 case PIX_FMT_YUV420P:
238 case PIX_FMT_YUV422P:
239 case PIX_FMT_YUV444P:
240 case PIX_FMT_YUV410P:
241 case PIX_FMT_YUV411P:
242 case PIX_FMT_YUVJ420P:
243 case PIX_FMT_YUVJ422P:
244 case PIX_FMT_YUVJ444P:
245 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
246 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
248 picture->data[0] = ptr;
249 picture->data[1] = picture->data[0] + size;
250 picture->data[2] = picture->data[1] + size2;
251 picture->linesize[0] = width;
252 picture->linesize[1] = w2;
253 picture->linesize[2] = w2;
254 return size + 2 * size2;
257 picture->data[0] = ptr;
258 picture->data[1] = NULL;
259 picture->data[2] = NULL;
260 picture->linesize[0] = width * 3;
263 picture->data[0] = ptr;
264 picture->data[1] = NULL;
265 picture->data[2] = NULL;
266 picture->linesize[0] = width * 4;
271 picture->data[0] = ptr;
272 picture->data[1] = NULL;
273 picture->data[2] = NULL;
274 picture->linesize[0] = width * 2;
277 picture->data[0] = ptr;
278 picture->data[1] = NULL;
279 picture->data[2] = NULL;
280 picture->linesize[0] = width;
282 case PIX_FMT_MONOWHITE:
283 case PIX_FMT_MONOBLACK:
284 picture->data[0] = ptr;
285 picture->data[1] = NULL;
286 picture->data[2] = NULL;
287 picture->linesize[0] = (width + 7) >> 3;
288 return picture->linesize[0] * height;
290 size2 = (size + 3) & ~3;
291 picture->data[0] = ptr;
292 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
293 picture->data[2] = NULL;
294 picture->linesize[0] = width;
295 picture->linesize[1] = 4;
296 return size2 + 256 * 4;
298 picture->data[0] = NULL;
299 picture->data[1] = NULL;
300 picture->data[2] = NULL;
301 picture->data[3] = NULL;
306 int avpicture_get_size(int pix_fmt, int width, int height)
308 AVPicture dummy_pict;
309 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
313 * compute the loss when converting from a pixel format to another
315 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
318 const PixFmtInfo *pf, *ps;
321 ps = &pix_fmt_info[src_pix_fmt];
322 pf = &pix_fmt_info[dst_pix_fmt];
326 pf = &pix_fmt_info[dst_pix_fmt];
327 if (pf->depth < ps->depth ||
328 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
329 loss |= FF_LOSS_DEPTH;
330 if (pf->x_chroma_shift > ps->x_chroma_shift ||
331 pf->y_chroma_shift > ps->y_chroma_shift)
332 loss |= FF_LOSS_RESOLUTION;
333 switch(pf->color_type) {
335 if (ps->color_type != FF_COLOR_RGB &&
336 ps->color_type != FF_COLOR_GRAY)
337 loss |= FF_LOSS_COLORSPACE;
340 if (ps->color_type != FF_COLOR_GRAY)
341 loss |= FF_LOSS_COLORSPACE;
344 if (ps->color_type != FF_COLOR_YUV)
345 loss |= FF_LOSS_COLORSPACE;
347 case FF_COLOR_YUV_JPEG:
348 if (ps->color_type != FF_COLOR_YUV_JPEG &&
349 ps->color_type != FF_COLOR_YUV &&
350 ps->color_type != FF_COLOR_GRAY)
351 loss |= FF_LOSS_COLORSPACE;
355 if (ps->color_type != pf->color_type)
356 loss |= FF_LOSS_COLORSPACE;
359 if (pf->color_type == FF_COLOR_GRAY &&
360 ps->color_type != FF_COLOR_GRAY)
361 loss |= FF_LOSS_CHROMA;
362 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
363 loss |= FF_LOSS_ALPHA;
364 if (pf->pixel_type == FF_PIXEL_PALETTE &&
365 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
366 loss |= FF_LOSS_COLORQUANT;
370 static int avg_bits_per_pixel(int pix_fmt)
373 const PixFmtInfo *pf;
375 pf = &pix_fmt_info[pix_fmt];
376 switch(pf->pixel_type) {
377 case FF_PIXEL_PACKED:
385 bits = pf->depth * pf->nb_channels;
389 case FF_PIXEL_PLANAR:
390 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
391 bits = pf->depth * pf->nb_channels;
393 bits = pf->depth + ((2 * pf->depth) >>
394 (pf->x_chroma_shift + pf->y_chroma_shift));
397 case FF_PIXEL_PALETTE:
407 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
412 int dist, i, loss, min_dist, dst_pix_fmt;
414 /* find exact color match with smallest size */
416 min_dist = 0x7fffffff;
417 for(i = 0;i < PIX_FMT_NB; i++) {
418 if (pix_fmt_mask & (1 << i)) {
419 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
421 dist = avg_bits_per_pixel(i);
422 if (dist < min_dist) {
433 * find best pixel format to convert to. Return -1 if none found
435 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
436 int has_alpha, int *loss_ptr)
438 int dst_pix_fmt, loss_mask, i;
439 static const int loss_mask_order[] = {
440 ~0, /* no loss first */
443 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
449 /* try with successive loss */
452 loss_mask = loss_mask_order[i++];
453 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
454 has_alpha, loss_mask);
455 if (dst_pix_fmt >= 0)
463 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
467 static void img_copy_plane(uint8_t *dst, int dst_wrap,
468 const uint8_t *src, int src_wrap,
469 int width, int height)
471 for(;height > 0; height--) {
472 memcpy(dst, src, width);
479 * Copy image 'src' to 'dst'.
481 void img_copy(AVPicture *dst, AVPicture *src,
482 int pix_fmt, int width, int height)
485 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
487 pf = &pix_fmt_info[pix_fmt];
488 switch(pf->pixel_type) {
489 case FF_PIXEL_PACKED:
497 bits = pf->depth * pf->nb_channels;
500 bwidth = (width * bits + 7) >> 3;
501 img_copy_plane(dst->data[0], dst->linesize[0],
502 src->data[0], src->linesize[0],
505 case FF_PIXEL_PLANAR:
506 for(i = 0; i < pf->nb_channels; i++) {
510 if (i == 1 || i == 2) {
511 w >>= pf->x_chroma_shift;
512 h >>= pf->y_chroma_shift;
514 bwidth = (w * pf->depth + 7) >> 3;
515 img_copy_plane(dst->data[i], dst->linesize[i],
516 src->data[i], src->linesize[i],
520 case FF_PIXEL_PALETTE:
521 img_copy_plane(dst->data[0], dst->linesize[0],
522 src->data[0], src->linesize[0],
524 /* copy the palette */
525 img_copy_plane(dst->data[1], dst->linesize[1],
526 src->data[1], src->linesize[1],
532 /* XXX: totally non optimized */
534 static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
535 int width, int height)
537 const uint8_t *p, *p1;
538 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
546 for(;height >= 2; height -= 2) {
551 for(x=0;x<width;x+=2) {
561 p1 += src->linesize[0];
562 lum1 += dst->linesize[0];
565 for(x=0;x<width;x+=2) {
571 p1 += src->linesize[0];
572 lum1 += dst->linesize[0];
573 cb1 += dst->linesize[1];
574 cr1 += dst->linesize[2];
578 static void yuv422_to_yuv422p(AVPicture *dst, AVPicture *src,
579 int width, int height)
581 const uint8_t *p, *p1;
582 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
589 for(;height > 0; height--) {
594 for(w = width; w >= 2; w -= 2) {
604 p1 += src->linesize[0];
605 lum1 += dst->linesize[0];
606 cb1 += dst->linesize[1];
607 cr1 += dst->linesize[2];
611 static void yuv422p_to_yuv422(AVPicture *dst, AVPicture *src,
612 int width, int height)
615 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
622 for(;height > 0; height--) {
627 for(w = width; w >= 2; w -= 2) {
637 p1 += dst->linesize[0];
638 lum1 += src->linesize[0];
639 cb1 += src->linesize[1];
640 cr1 += src->linesize[2];
645 #define ONE_HALF (1 << (SCALEBITS - 1))
646 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
648 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
652 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
653 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
655 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
658 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
660 y = ((y1) - 16) * FIX(255.0/219.0);\
661 r = cm[(y + r_add) >> SCALEBITS];\
662 g = cm[(y + g_add) >> SCALEBITS];\
663 b = cm[(y + b_add) >> SCALEBITS];\
666 #define YUV_TO_RGB1(cb1, cr1)\
670 r_add = FIX(1.40200) * cr + ONE_HALF;\
671 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
672 b_add = FIX(1.77200) * cb + ONE_HALF;\
675 #define YUV_TO_RGB2(r, g, b, y1)\
677 y = (y1) << SCALEBITS;\
678 r = cm[(y + r_add) >> SCALEBITS];\
679 g = cm[(y + g_add) >> SCALEBITS];\
680 b = cm[(y + b_add) >> SCALEBITS];\
683 #define Y_CCIR_TO_JPEG(y)\
684 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
686 #define Y_JPEG_TO_CCIR(y)\
687 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
689 #define C_CCIR_TO_JPEG(y)\
690 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
692 /* NOTE: the clamp is really necessary! */
693 #define C_JPEG_TO_CCIR(y)\
696 __y = ((((y) - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);\
702 #define RGB_TO_Y(r, g, b) \
703 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
704 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
706 #define RGB_TO_U(r1, g1, b1, shift)\
707 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
708 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
710 #define RGB_TO_V(r1, g1, b1, shift)\
711 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
712 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
714 #define RGB_TO_Y_CCIR(r, g, b) \
715 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
716 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
718 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
719 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
720 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
722 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
723 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
724 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
726 static uint8_t y_ccir_to_jpeg[256];
727 static uint8_t y_jpeg_to_ccir[256];
728 static uint8_t c_ccir_to_jpeg[256];
729 static uint8_t c_jpeg_to_ccir[256];
731 /* init various conversion tables */
732 static void img_convert_init(void)
735 uint8_t *cm = cropTbl + MAX_NEG_CROP;
737 for(i = 0;i < 256; i++) {
738 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
739 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
740 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
741 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
745 /* apply to each pixel the given table */
746 static void img_apply_table(uint8_t *dst, int dst_wrap,
747 const uint8_t *src, int src_wrap,
748 int width, int height, const uint8_t *table1)
753 const uint8_t *table;
756 for(;height > 0; height--) {
780 /* XXX: use generic filter ? */
781 /* XXX: in most cases, the sampling position is incorrect */
784 static void shrink41(uint8_t *dst, int dst_wrap,
785 const uint8_t *src, int src_wrap,
786 int width, int height)
792 for(;height > 0; height--) {
795 for(w = width;w > 0; w--) {
796 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
806 static void shrink21(uint8_t *dst, int dst_wrap,
807 const uint8_t *src, int src_wrap,
808 int width, int height)
814 for(;height > 0; height--) {
817 for(w = width;w > 0; w--) {
818 d[0] = (s[0] + s[1]) >> 1;
828 static void shrink12(uint8_t *dst, int dst_wrap,
829 const uint8_t *src, int src_wrap,
830 int width, int height)
834 const uint8_t *s1, *s2;
836 for(;height > 0; height--) {
840 for(w = width;w >= 4; w-=4) {
841 d[0] = (s1[0] + s2[0]) >> 1;
842 d[1] = (s1[1] + s2[1]) >> 1;
843 d[2] = (s1[2] + s2[2]) >> 1;
844 d[3] = (s1[3] + s2[3]) >> 1;
850 d[0] = (s1[0] + s2[0]) >> 1;
861 static void shrink22(uint8_t *dst, int dst_wrap,
862 const uint8_t *src, int src_wrap,
863 int width, int height)
866 const uint8_t *s1, *s2;
869 for(;height > 0; height--) {
873 for(w = width;w >= 4; w-=4) {
874 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
875 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
876 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
877 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
883 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
894 static void shrink44(uint8_t *dst, int dst_wrap,
895 const uint8_t *src, int src_wrap,
896 int width, int height)
899 const uint8_t *s1, *s2, *s3, *s4;
902 for(;height > 0; height--) {
908 for(w = width;w > 0; w--) {
909 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
910 s2[0] + s2[1] + s2[2] + s2[3] +
911 s3[0] + s3[1] + s3[2] + s3[3] +
912 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
924 static void grow21_line(uint8_t *dst, const uint8_t *src,
933 for(w = width;w >= 4; w-=4) {
939 for(;w >= 2; w -= 2) {
944 /* only needed if width is not a multiple of two */
945 /* XXX: veryfy that */
951 static void grow41_line(uint8_t *dst, const uint8_t *src,
960 for(w = width;w >= 4; w-=4) {
972 static void grow21(uint8_t *dst, int dst_wrap,
973 const uint8_t *src, int src_wrap,
974 int width, int height)
976 for(;height > 0; height--) {
977 grow21_line(dst, src, width);
984 static void grow22(uint8_t *dst, int dst_wrap,
985 const uint8_t *src, int src_wrap,
986 int width, int height)
988 for(;height > 0; height--) {
989 grow21_line(dst, src, width);
997 static void grow41(uint8_t *dst, int dst_wrap,
998 const uint8_t *src, int src_wrap,
999 int width, int height)
1001 for(;height > 0; height--) {
1002 grow41_line(dst, src, width);
1009 static void grow44(uint8_t *dst, int dst_wrap,
1010 const uint8_t *src, int src_wrap,
1011 int width, int height)
1013 for(;height > 0; height--) {
1014 grow41_line(dst, src, width);
1015 if ((height & 3) == 1)
1022 static void conv411(uint8_t *dst, int dst_wrap,
1023 const uint8_t *src, int src_wrap,
1024 int width, int height)
1027 const uint8_t *s1, *s2;
1032 for(;height > 0; height--) {
1034 s2 = src + src_wrap;
1036 for(w = width;w > 0; w--) {
1037 c = (s1[0] + s2[0]) >> 1;
1044 src += src_wrap * 2;
1049 /* XXX: add jpeg quantize code */
1051 #define TRANSP_INDEX (6*6*6)
1053 /* this is maybe slow, but allows for extensions */
1054 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1056 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1059 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1062 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1065 pal = (uint32_t *)palette;
1067 for(r = 0; r < 6; r++) {
1068 for(g = 0; g < 6; g++) {
1069 for(b = 0; b < 6; b++) {
1070 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1071 (pal_value[g] << 8) | pal_value[b];
1078 pal[i++] = 0xff000000;
1081 /* copy bit n to bits 0 ... n - 1 */
1082 static inline unsigned int bitcopy_n(unsigned int a, int n)
1085 mask = (1 << n) - 1;
1086 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1089 /* rgb555 handling */
1091 #define RGB_NAME rgb555
1093 #define RGB_IN(r, g, b, s)\
1095 unsigned int v = ((const uint16_t *)(s))[0];\
1096 r = bitcopy_n(v >> (10 - 3), 3);\
1097 g = bitcopy_n(v >> (5 - 3), 3);\
1098 b = bitcopy_n(v << 3, 3);\
1101 #define RGBA_IN(r, g, b, a, s)\
1103 unsigned int v = ((const uint16_t *)(s))[0];\
1104 r = bitcopy_n(v >> (10 - 3), 3);\
1105 g = bitcopy_n(v >> (5 - 3), 3);\
1106 b = bitcopy_n(v << 3, 3);\
1107 a = (-(v >> 15)) & 0xff;\
1110 #define RGBA_OUT(d, r, g, b, a)\
1112 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1113 ((a << 8) & 0x8000);\
1118 #include "imgconvert_template.h"
1120 /* rgb565 handling */
1122 #define RGB_NAME rgb565
1124 #define RGB_IN(r, g, b, s)\
1126 unsigned int v = ((const uint16_t *)(s))[0];\
1127 r = bitcopy_n(v >> (11 - 3), 3);\
1128 g = bitcopy_n(v >> (5 - 2), 2);\
1129 b = bitcopy_n(v << 3, 3);\
1132 #define RGB_OUT(d, r, g, b)\
1134 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1139 #include "imgconvert_template.h"
1141 /* bgr24 handling */
1143 #define RGB_NAME bgr24
1145 #define RGB_IN(r, g, b, s)\
1152 #define RGB_OUT(d, r, g, b)\
1161 #include "imgconvert_template.h"
1167 /* rgb24 handling */
1169 #define RGB_NAME rgb24
1172 #define RGB_IN(r, g, b, s)\
1179 #define RGB_OUT(d, r, g, b)\
1188 #include "imgconvert_template.h"
1190 /* rgba32 handling */
1192 #define RGB_NAME rgba32
1195 #define RGB_IN(r, g, b, s)\
1197 unsigned int v = ((const uint32_t *)(s))[0];\
1198 r = (v >> 16) & 0xff;\
1199 g = (v >> 8) & 0xff;\
1203 #define RGBA_IN(r, g, b, a, s)\
1205 unsigned int v = ((const uint32_t *)(s))[0];\
1206 a = (v >> 24) & 0xff;\
1207 r = (v >> 16) & 0xff;\
1208 g = (v >> 8) & 0xff;\
1212 #define RGBA_OUT(d, r, g, b, a)\
1214 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1219 #include "imgconvert_template.h"
1221 static void mono_to_gray(AVPicture *dst, AVPicture *src,
1222 int width, int height, int xor_mask)
1224 const unsigned char *p;
1226 int v, dst_wrap, src_wrap;
1230 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1233 dst_wrap = dst->linesize[0] - width;
1234 for(y=0;y<height;y++) {
1237 v = *p++ ^ xor_mask;
1239 q[1] = -((v >> 6) & 1);
1240 q[2] = -((v >> 5) & 1);
1241 q[3] = -((v >> 4) & 1);
1242 q[4] = -((v >> 3) & 1);
1243 q[5] = -((v >> 2) & 1);
1244 q[6] = -((v >> 1) & 1);
1245 q[7] = -((v >> 0) & 1);
1250 v = *p++ ^ xor_mask;
1252 q[0] = -((v >> 7) & 1);
1262 static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
1263 int width, int height)
1265 mono_to_gray(dst, src, width, height, 0xff);
1268 static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
1269 int width, int height)
1271 mono_to_gray(dst, src, width, height, 0x00);
1274 static void gray_to_mono(AVPicture *dst, AVPicture *src,
1275 int width, int height, int xor_mask)
1280 int j, b, v, n1, src_wrap, dst_wrap, y;
1283 src_wrap = src->linesize[0] - width;
1286 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1288 for(y=0;y<height;y++) {
1295 v = (v << 1) | (b >> 7);
1297 d[0] = v ^ xor_mask;
1307 v = (v << 1) | (b >> 7);
1310 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1318 static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
1319 int width, int height)
1321 gray_to_mono(dst, src, width, height, 0xff);
1324 static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
1325 int width, int height)
1327 gray_to_mono(dst, src, width, height, 0x00);
1330 typedef struct ConvertEntry {
1331 void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
1334 /* Add each new convertion function in this table. In order to be able
1335 to convert from any format to any format, the following constraints
1338 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1340 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1342 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1344 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1347 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1349 The other conversion functions are just optimisations for common cases.
1351 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1352 [PIX_FMT_YUV420P] = {
1353 [PIX_FMT_RGB555] = {
1354 .convert = yuv420p_to_rgb555
1356 [PIX_FMT_RGB565] = {
1357 .convert = yuv420p_to_rgb565
1360 .convert = yuv420p_to_bgr24
1363 .convert = yuv420p_to_rgb24
1365 [PIX_FMT_RGBA32] = {
1366 .convert = yuv420p_to_rgba32
1369 [PIX_FMT_YUV422P] = {
1370 [PIX_FMT_YUV422] = {
1371 .convert = yuv422p_to_yuv422,
1374 [PIX_FMT_YUV444P] = {
1376 .convert = yuv444p_to_rgb24
1379 [PIX_FMT_YUVJ420P] = {
1380 [PIX_FMT_RGB555] = {
1381 .convert = yuvj420p_to_rgb555
1383 [PIX_FMT_RGB565] = {
1384 .convert = yuvj420p_to_rgb565
1387 .convert = yuvj420p_to_bgr24
1390 .convert = yuvj420p_to_rgb24
1392 [PIX_FMT_RGBA32] = {
1393 .convert = yuvj420p_to_rgba32
1396 [PIX_FMT_YUVJ444P] = {
1398 .convert = yuvj444p_to_rgb24
1401 [PIX_FMT_YUV422] = {
1402 [PIX_FMT_YUV420P] = {
1403 .convert = yuv422_to_yuv420p,
1405 [PIX_FMT_YUV422P] = {
1406 .convert = yuv422_to_yuv422p,
1411 [PIX_FMT_YUV420P] = {
1412 .convert = rgb24_to_yuv420p
1414 [PIX_FMT_RGB565] = {
1415 .convert = rgb24_to_rgb565
1417 [PIX_FMT_RGB555] = {
1418 .convert = rgb24_to_rgb555
1420 [PIX_FMT_RGBA32] = {
1421 .convert = rgb24_to_rgba32
1424 .convert = rgb24_to_bgr24
1427 .convert = rgb24_to_gray
1430 .convert = rgb24_to_pal8
1432 [PIX_FMT_YUV444P] = {
1433 .convert = rgb24_to_yuv444p
1435 [PIX_FMT_YUVJ420P] = {
1436 .convert = rgb24_to_yuvj420p
1438 [PIX_FMT_YUVJ444P] = {
1439 .convert = rgb24_to_yuvj444p
1442 [PIX_FMT_RGBA32] = {
1444 .convert = rgba32_to_rgb24
1446 [PIX_FMT_RGB555] = {
1447 .convert = rgba32_to_rgb555
1450 .convert = rgba32_to_pal8
1452 [PIX_FMT_YUV420P] = {
1453 .convert = rgba32_to_yuv420p
1456 .convert = rgba32_to_gray
1461 .convert = bgr24_to_rgb24
1463 [PIX_FMT_YUV420P] = {
1464 .convert = bgr24_to_yuv420p
1467 .convert = bgr24_to_gray
1470 [PIX_FMT_RGB555] = {
1472 .convert = rgb555_to_rgb24
1474 [PIX_FMT_RGBA32] = {
1475 .convert = rgb555_to_rgba32
1477 [PIX_FMT_YUV420P] = {
1478 .convert = rgb555_to_yuv420p
1481 .convert = rgb555_to_gray
1484 [PIX_FMT_RGB565] = {
1486 .convert = rgb565_to_rgb24
1488 [PIX_FMT_YUV420P] = {
1489 .convert = rgb565_to_yuv420p
1492 .convert = rgb565_to_gray
1496 [PIX_FMT_RGB555] = {
1497 .convert = gray_to_rgb555
1499 [PIX_FMT_RGB565] = {
1500 .convert = gray_to_rgb565
1503 .convert = gray_to_rgb24
1506 .convert = gray_to_bgr24
1508 [PIX_FMT_RGBA32] = {
1509 .convert = gray_to_rgba32
1511 [PIX_FMT_MONOWHITE] = {
1512 .convert = gray_to_monowhite
1514 [PIX_FMT_MONOBLACK] = {
1515 .convert = gray_to_monoblack
1518 [PIX_FMT_MONOWHITE] = {
1520 .convert = monowhite_to_gray
1523 [PIX_FMT_MONOBLACK] = {
1525 .convert = monoblack_to_gray
1529 [PIX_FMT_RGB555] = {
1530 .convert = pal8_to_rgb555
1532 [PIX_FMT_RGB565] = {
1533 .convert = pal8_to_rgb565
1536 .convert = pal8_to_bgr24
1539 .convert = pal8_to_rgb24
1541 [PIX_FMT_RGBA32] = {
1542 .convert = pal8_to_rgba32
1547 static int avpicture_alloc(AVPicture *picture,
1548 int pix_fmt, int width, int height)
1553 size = avpicture_get_size(pix_fmt, width, height);
1556 ptr = av_malloc(size);
1559 avpicture_fill(picture, ptr, pix_fmt, width, height);
1562 memset(picture, 0, sizeof(AVPicture));
1566 static void avpicture_free(AVPicture *picture)
1568 av_free(picture->data[0]);
1571 /* return true if yuv planar */
1572 static inline int is_yuv_planar(PixFmtInfo *ps)
1574 return (ps->color_type == FF_COLOR_YUV ||
1575 ps->color_type == FF_COLOR_YUV_JPEG) &&
1576 ps->pixel_type == FF_PIXEL_PLANAR;
1579 /* XXX: always use linesize. Return -1 if not supported */
1580 int img_convert(AVPicture *dst, int dst_pix_fmt,
1581 AVPicture *src, int src_pix_fmt,
1582 int src_width, int src_height)
1585 int i, ret, dst_width, dst_height, int_pix_fmt;
1586 PixFmtInfo *src_pix, *dst_pix;
1588 AVPicture tmp1, *tmp = &tmp1;
1590 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1591 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1593 if (src_width <= 0 || src_height <= 0)
1601 dst_width = src_width;
1602 dst_height = src_height;
1604 dst_pix = &pix_fmt_info[dst_pix_fmt];
1605 src_pix = &pix_fmt_info[src_pix_fmt];
1606 if (src_pix_fmt == dst_pix_fmt) {
1607 /* no conversion needed: just copy */
1608 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
1612 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1614 /* specific convertion routine */
1615 ce->convert(dst, src, dst_width, dst_height);
1620 if (is_yuv_planar(dst_pix) &&
1621 src_pix_fmt == PIX_FMT_GRAY8) {
1625 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
1626 img_copy_plane(dst->data[0], dst->linesize[0],
1627 src->data[0], src->linesize[0],
1628 dst_width, dst_height);
1630 img_apply_table(dst->data[0], dst->linesize[0],
1631 src->data[0], src->linesize[0],
1632 dst_width, dst_height,
1635 /* fill U and V with 128 */
1638 w >>= dst_pix->x_chroma_shift;
1639 h >>= dst_pix->y_chroma_shift;
1640 for(i = 1; i <= 2; i++) {
1642 for(y = 0; y< h; y++) {
1644 d += dst->linesize[i];
1651 if (is_yuv_planar(src_pix) &&
1652 dst_pix_fmt == PIX_FMT_GRAY8) {
1653 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
1654 img_copy_plane(dst->data[0], dst->linesize[0],
1655 src->data[0], src->linesize[0],
1656 dst_width, dst_height);
1658 img_apply_table(dst->data[0], dst->linesize[0],
1659 src->data[0], src->linesize[0],
1660 dst_width, dst_height,
1666 /* YUV to YUV planar */
1667 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
1668 int x_shift, y_shift, w, h, xy_shift;
1669 void (*resize_func)(uint8_t *dst, int dst_wrap,
1670 const uint8_t *src, int src_wrap,
1671 int width, int height);
1673 /* compute chroma size of the smallest dimensions */
1676 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1677 w >>= dst_pix->x_chroma_shift;
1679 w >>= src_pix->x_chroma_shift;
1680 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1681 h >>= dst_pix->y_chroma_shift;
1683 h >>= src_pix->y_chroma_shift;
1685 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1686 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
1687 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
1688 /* there must be filters for conversion at least from and to
1692 resize_func = img_copy_plane;
1695 resize_func = shrink21;
1698 resize_func = shrink41;
1701 resize_func = shrink12;
1704 resize_func = shrink22;
1707 resize_func = shrink44;
1710 resize_func = grow21;
1713 resize_func = grow41;
1716 resize_func = grow22;
1719 resize_func = grow44;
1722 resize_func = conv411;
1725 /* currently not handled */
1726 goto no_chroma_filter;
1729 img_copy_plane(dst->data[0], dst->linesize[0],
1730 src->data[0], src->linesize[0],
1731 dst_width, dst_height);
1733 for(i = 1;i <= 2; i++)
1734 resize_func(dst->data[i], dst->linesize[i],
1735 src->data[i], src->linesize[i],
1736 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
1737 /* if yuv color space conversion is needed, we do it here on
1738 the destination image */
1739 if (dst_pix->color_type != src_pix->color_type) {
1740 const uint8_t *y_table, *c_table;
1741 if (dst_pix->color_type == FF_COLOR_YUV) {
1742 y_table = y_jpeg_to_ccir;
1743 c_table = c_jpeg_to_ccir;
1745 y_table = y_ccir_to_jpeg;
1746 c_table = c_ccir_to_jpeg;
1748 img_apply_table(dst->data[0], dst->linesize[0],
1749 dst->data[0], dst->linesize[0],
1750 dst_width, dst_height,
1753 for(i = 1;i <= 2; i++)
1754 img_apply_table(dst->data[i], dst->linesize[i],
1755 dst->data[i], dst->linesize[i],
1756 dst_width>>dst_pix->x_chroma_shift,
1757 dst_height>>dst_pix->y_chroma_shift,
1764 /* try to use an intermediate format */
1765 if (src_pix_fmt == PIX_FMT_YUV422 ||
1766 dst_pix_fmt == PIX_FMT_YUV422) {
1767 /* specific case: convert to YUV422P first */
1768 int_pix_fmt = PIX_FMT_YUV422P;
1769 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
1770 src_pix_fmt != PIX_FMT_GRAY8) ||
1771 (dst_pix->color_type == FF_COLOR_GRAY &&
1772 dst_pix_fmt != PIX_FMT_GRAY8)) {
1773 /* gray8 is the normalized format */
1774 int_pix_fmt = PIX_FMT_GRAY8;
1775 } else if ((is_yuv_planar(src_pix) &&
1776 src_pix_fmt != PIX_FMT_YUV444P &&
1777 src_pix_fmt != PIX_FMT_YUVJ444P)) {
1778 /* yuv444 is the normalized format */
1779 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
1780 int_pix_fmt = PIX_FMT_YUVJ444P;
1782 int_pix_fmt = PIX_FMT_YUV444P;
1783 } else if ((is_yuv_planar(dst_pix) &&
1784 dst_pix_fmt != PIX_FMT_YUV444P &&
1785 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
1786 /* yuv444 is the normalized format */
1787 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
1788 int_pix_fmt = PIX_FMT_YUVJ444P;
1790 int_pix_fmt = PIX_FMT_YUV444P;
1792 /* the two formats are rgb or gray8 or yuv[j]444p */
1793 if (src_pix->is_alpha && dst_pix->is_alpha)
1794 int_pix_fmt = PIX_FMT_RGBA32;
1796 int_pix_fmt = PIX_FMT_RGB24;
1798 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
1801 if (img_convert(tmp, int_pix_fmt,
1802 src, src_pix_fmt, src_width, src_height) < 0)
1804 if (img_convert(dst, dst_pix_fmt,
1805 tmp, int_pix_fmt, dst_width, dst_height) < 0)
1809 avpicture_free(tmp);
1813 /* NOTE: we scan all the pixels to have an exact information */
1814 static int get_alpha_info_pal8(AVPicture *src, int width, int height)
1816 const unsigned char *p;
1817 int src_wrap, ret, x, y;
1819 uint32_t *palette = (uint32_t *)src->data[1];
1822 src_wrap = src->linesize[0] - width;
1824 for(y=0;y<height;y++) {
1825 for(x=0;x<width;x++) {
1826 a = palette[p[0]] >> 24;
1828 ret |= FF_ALPHA_TRANSP;
1829 } else if (a != 0xff) {
1830 ret |= FF_ALPHA_SEMI_TRANSP;
1840 * Tell if an image really has transparent alpha values.
1841 * @return ored mask of FF_ALPHA_xxx constants
1843 int img_get_alpha_info(AVPicture *src, int pix_fmt, int width, int height)
1845 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1848 pf = &pix_fmt_info[pix_fmt];
1849 /* no alpha can be represented in format */
1853 case PIX_FMT_RGBA32:
1854 ret = get_alpha_info_rgba32(src, width, height);
1856 case PIX_FMT_RGB555:
1857 ret = get_alpha_info_rgb555(src, width, height);
1860 ret = get_alpha_info_pal8(src, width, height);
1863 /* we do not know, so everything is indicated */
1864 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1871 #define DEINT_INPLACE_LINE_LUM \
1872 movd_m2r(lum_m4[0],mm0);\
1873 movd_m2r(lum_m3[0],mm1);\
1874 movd_m2r(lum_m2[0],mm2);\
1875 movd_m2r(lum_m1[0],mm3);\
1876 movd_m2r(lum[0],mm4);\
1877 punpcklbw_r2r(mm7,mm0);\
1878 movd_r2m(mm2,lum_m4[0]);\
1879 punpcklbw_r2r(mm7,mm1);\
1880 punpcklbw_r2r(mm7,mm2);\
1881 punpcklbw_r2r(mm7,mm3);\
1882 punpcklbw_r2r(mm7,mm4);\
1883 paddw_r2r(mm3,mm1);\
1885 paddw_r2r(mm4,mm0);\
1887 paddw_r2r(mm6,mm2);\
1888 paddw_r2r(mm2,mm1);\
1889 psubusw_r2r(mm0,mm1);\
1891 packuswb_r2r(mm7,mm1);\
1892 movd_r2m(mm1,lum_m2[0]);
1894 #define DEINT_LINE_LUM \
1895 movd_m2r(lum_m4[0],mm0);\
1896 movd_m2r(lum_m3[0],mm1);\
1897 movd_m2r(lum_m2[0],mm2);\
1898 movd_m2r(lum_m1[0],mm3);\
1899 movd_m2r(lum[0],mm4);\
1900 punpcklbw_r2r(mm7,mm0);\
1901 punpcklbw_r2r(mm7,mm1);\
1902 punpcklbw_r2r(mm7,mm2);\
1903 punpcklbw_r2r(mm7,mm3);\
1904 punpcklbw_r2r(mm7,mm4);\
1905 paddw_r2r(mm3,mm1);\
1907 paddw_r2r(mm4,mm0);\
1909 paddw_r2r(mm6,mm2);\
1910 paddw_r2r(mm2,mm1);\
1911 psubusw_r2r(mm0,mm1);\
1913 packuswb_r2r(mm7,mm1);\
1914 movd_r2m(mm1,dst[0]);
1917 /* filter parameters: [-1 4 2 4 -1] // 8 */
1918 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,
1922 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1925 for(;size > 0;size--) {
1927 sum += lum_m3[0] << 2;
1928 sum += lum_m2[0] << 1;
1929 sum += lum_m1[0] << 2;
1931 dst[0] = cm[(sum + 4) >> 3];
1948 movq_m2r(rounder,mm6);
1950 for (;size > 3; size-=4) {
1961 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1965 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1968 for(;size > 0;size--) {
1970 sum += lum_m3[0] << 2;
1971 sum += lum_m2[0] << 1;
1972 lum_m4[0]=lum_m2[0];
1973 sum += lum_m1[0] << 2;
1975 lum_m2[0] = cm[(sum + 4) >> 3];
1991 movq_m2r(rounder,mm6);
1993 for (;size > 3; size-=4) {
1994 DEINT_INPLACE_LINE_LUM
2004 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2005 top field is copied as is, but the bottom field is deinterlaced
2006 against the top field. */
2007 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2008 uint8_t *src1, int src_wrap,
2009 int width, int height)
2011 uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2016 src_0=&src_m1[src_wrap];
2017 src_p1=&src_0[src_wrap];
2018 src_p2=&src_p1[src_wrap];
2019 for(y=0;y<(height-2);y+=2) {
2020 memcpy(dst,src_m1,width);
2022 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2026 src_p1 += 2*src_wrap;
2027 src_p2 += 2*src_wrap;
2030 memcpy(dst,src_m1,width);
2033 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2036 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2037 int width, int height)
2039 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2042 buf = (uint8_t*)av_malloc(width);
2045 memcpy(buf,src_m1,width);
2046 src_0=&src_m1[src_wrap];
2047 src_p1=&src_0[src_wrap];
2048 src_p2=&src_p1[src_wrap];
2049 for(y=0;y<(height-2);y+=2) {
2050 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2053 src_p1 += 2*src_wrap;
2054 src_p2 += 2*src_wrap;
2057 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2062 /* deinterlace - if not supported return -1 */
2063 int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
2064 int pix_fmt, int width, int height)
2068 if (pix_fmt != PIX_FMT_YUV420P &&
2069 pix_fmt != PIX_FMT_YUV422P &&
2070 pix_fmt != PIX_FMT_YUV444P)
2072 if ((width & 3) != 0 || (height & 3) != 0)
2078 case PIX_FMT_YUV420P:
2082 case PIX_FMT_YUV422P:
2090 deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
2093 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2094 src->data[i], src->linesize[i],