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
23 #include "fastmemcpy.h"
30 typedef struct PixFmtInfo {
32 UINT8 nb_components; /* number of components in AVPicture array */
33 UINT8 is_yuv : 1; /* true if YUV instead of RGB color space */
34 UINT8 is_packed : 1; /* true if multiple components in same word */
35 UINT8 is_paletted : 1; /* true if paletted */
36 UINT8 is_alpha : 1; /* true if alpha can be specified */
37 UINT8 is_gray : 1; /* true if gray or monochrome format */
38 UINT8 x_chroma_shift; /* X chroma subsampling factor is 2 ^ shift */
39 UINT8 y_chroma_shift; /* Y chroma subsampling factor is 2 ^ shift */
42 /* this table gives more information about formats */
43 static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
47 .nb_components = 3, .is_yuv = 1,
48 .x_chroma_shift = 1, .y_chroma_shift = 1,
52 .nb_components = 3, .is_yuv = 1,
53 .x_chroma_shift = 1, .y_chroma_shift = 0,
57 .nb_components = 3, .is_yuv = 1,
58 .x_chroma_shift = 0, .y_chroma_shift = 0,
62 .nb_components = 1, .is_yuv = 1, .is_packed = 1,
63 .x_chroma_shift = 1, .y_chroma_shift = 0,
67 .nb_components = 3, .is_yuv = 1,
68 .x_chroma_shift = 2, .y_chroma_shift = 2,
72 .nb_components = 3, .is_yuv = 1,
73 .x_chroma_shift = 2, .y_chroma_shift = 0,
79 .nb_components = 1, .is_packed = 1,
83 .nb_components = 1, .is_packed = 1,
87 .nb_components = 1, .is_packed = 1, .is_alpha = 1,
91 .nb_components = 1, .is_packed = 1,
95 .nb_components = 1, .is_packed = 1, .is_alpha = 1,
98 /* gray / mono formats */
101 .nb_components = 1, .is_gray = 1,
103 [PIX_FMT_MONOWHITE] = {
105 .nb_components = 1, .is_packed = 1, .is_gray = 1,
107 [PIX_FMT_MONOBLACK] = {
109 .nb_components = 1, .is_packed = 1, .is_gray = 1,
113 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
115 if (pix_fmt_info[pix_fmt].is_yuv) {
116 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
117 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
124 const char *avcodec_get_pix_fmt_name(int pix_fmt)
126 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
129 return pix_fmt_info[pix_fmt].name;
132 /* Picture field are filled with 'ptr' addresses. Also return size */
133 int avpicture_fill(AVPicture *picture, UINT8 *ptr,
134 int pix_fmt, int width, int height)
138 size = width * height;
140 case PIX_FMT_YUV420P:
141 picture->data[0] = ptr;
142 picture->data[1] = picture->data[0] + size;
143 picture->data[2] = picture->data[1] + size / 4;
144 picture->linesize[0] = width;
145 picture->linesize[1] = width / 2;
146 picture->linesize[2] = width / 2;
147 return (size * 3) / 2;
150 picture->data[0] = ptr;
151 picture->data[1] = NULL;
152 picture->data[2] = NULL;
153 picture->linesize[0] = width * 3;
155 case PIX_FMT_YUV422P:
156 picture->data[0] = ptr;
157 picture->data[1] = picture->data[0] + size;
158 picture->data[2] = picture->data[1] + size / 2;
159 picture->linesize[0] = width;
160 picture->linesize[1] = width / 2;
161 picture->linesize[2] = width / 2;
163 case PIX_FMT_YUV444P:
164 picture->data[0] = ptr;
165 picture->data[1] = picture->data[0] + size;
166 picture->data[2] = picture->data[1] + size;
167 picture->linesize[0] = width;
168 picture->linesize[1] = width;
169 picture->linesize[2] = width;
172 picture->data[0] = ptr;
173 picture->data[1] = NULL;
174 picture->data[2] = NULL;
175 picture->linesize[0] = width * 4;
177 case PIX_FMT_YUV410P:
178 picture->data[0] = ptr;
179 picture->data[1] = picture->data[0] + size;
180 picture->data[2] = picture->data[1] + size / 16;
181 picture->linesize[0] = width;
182 picture->linesize[1] = width / 4;
183 picture->linesize[2] = width / 4;
184 return size + (size / 8);
185 case PIX_FMT_YUV411P:
186 picture->data[0] = ptr;
187 picture->data[1] = picture->data[0] + size;
188 picture->data[2] = picture->data[1] + size / 4;
189 picture->linesize[0] = width;
190 picture->linesize[1] = width / 4;
191 picture->linesize[2] = width / 4;
192 return size + (size / 2);
196 picture->data[0] = ptr;
197 picture->data[1] = NULL;
198 picture->data[2] = NULL;
199 picture->linesize[0] = width * 2;
202 picture->data[0] = ptr;
203 picture->data[1] = NULL;
204 picture->data[2] = NULL;
205 picture->linesize[0] = width;
207 case PIX_FMT_MONOWHITE:
208 case PIX_FMT_MONOBLACK:
209 picture->data[0] = ptr;
210 picture->data[1] = NULL;
211 picture->data[2] = NULL;
212 picture->linesize[0] = (width + 7) >> 3;
213 return picture->linesize[0] * height;
215 picture->data[0] = NULL;
216 picture->data[1] = NULL;
217 picture->data[2] = NULL;
222 int avpicture_get_size(int pix_fmt, int width, int height)
224 AVPicture dummy_pict;
225 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
229 /* XXX: totally non optimized */
231 static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
232 int width, int height)
234 UINT8 *lum, *cb, *cr;
243 for(y=0;y<height;y+=2) {
244 for(x=0;x<width;x+=2) {
254 for(x=0;x<width;x+=2) {
264 #define ONE_HALF (1 << (SCALEBITS - 1))
265 #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
267 /* XXX: use generic filter ? */
269 static void shrink2(UINT8 *dst, int dst_wrap,
270 UINT8 *src, int src_wrap,
271 int width, int height)
276 for(;height > 0; height--) {
280 for(w = width;w >= 4; w-=4) {
281 d[0] = (s1[0] + s2[0]) >> 1;
282 d[1] = (s1[1] + s2[1]) >> 1;
283 d[2] = (s1[2] + s2[2]) >> 1;
284 d[3] = (s1[3] + s2[3]) >> 1;
290 d[0] = (s1[0] + s2[0]) >> 1;
301 static void shrink22(UINT8 *dst, int dst_wrap,
302 UINT8 *src, int src_wrap,
303 int width, int height)
308 for(;height > 0; height--) {
312 for(w = width;w >= 4; w-=4) {
313 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
314 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1;
315 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1;
316 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1;
322 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
333 static void grow22(UINT8 *dst, int dst_wrap,
334 UINT8 *src, int src_wrap,
335 int width, int height)
340 for(;height > 0; height--) {
343 for(w = width;w >= 4; w-=4) {
361 static void conv411(UINT8 *dst, int dst_wrap,
362 UINT8 *src, int src_wrap,
363 int width, int height)
368 for(;height > 0; height--) {
372 for(w = width;w > 0; w--) {
373 c = (s1[0] + s2[0]) >> 1;
385 static void img_copy(UINT8 *dst, int dst_wrap,
386 UINT8 *src, int src_wrap,
387 int width, int height)
389 for(;height > 0; height--) {
390 memcpy(dst, src, width);
396 #define SCALE_BITS 10
398 #define C_Y (76309 >> (16 - SCALE_BITS))
399 #define C_RV (117504 >> (16 - SCALE_BITS))
400 #define C_BU (138453 >> (16 - SCALE_BITS))
401 #define C_GU (13954 >> (16 - SCALE_BITS))
402 #define C_GV (34903 >> (16 - SCALE_BITS))
404 #define YUV_TO_RGB2(r, g, b, y1)\
406 y = (y1 - 16) * C_Y;\
407 r = cm[(y + r_add) >> SCALE_BITS];\
408 g = cm[(y + g_add) >> SCALE_BITS];\
409 b = cm[(y + b_add) >> SCALE_BITS];\
412 /* XXX: no chroma interpolating is done */
413 #define RGB_FUNCTIONS(rgb_name) \
415 static void yuv420p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \
416 int width, int height) \
418 UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2; \
419 int w, y, cb, cr, r_add, g_add, b_add, width2; \
420 UINT8 *cm = cropTbl + MAX_NEG_CROP; \
421 unsigned int r, g, b; \
424 y1_ptr = src->data[0]; \
425 cb_ptr = src->data[1]; \
426 cr_ptr = src->data[2]; \
427 width2 = width >> 1; \
428 for(;height > 0; height -= 2) { \
430 d2 = d + dst->linesize[0]; \
431 y2_ptr = y1_ptr + src->linesize[0]; \
432 for(w = width2; w > 0; w --) { \
433 cb = cb_ptr[0] - 128; \
434 cr = cr_ptr[0] - 128; \
435 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
436 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
437 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
439 /* output 4 pixels */ \
440 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
441 RGB_OUT(d1, r, g, b); \
443 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
444 RGB_OUT(d1 + BPP, r, g, b); \
446 YUV_TO_RGB2(r, g, b, y2_ptr[0]); \
447 RGB_OUT(d2, r, g, b); \
449 YUV_TO_RGB2(r, g, b, y2_ptr[1]); \
450 RGB_OUT(d2 + BPP, r, g, b); \
460 d += 2 * dst->linesize[0]; \
461 y1_ptr += 2 * src->linesize[0] - width; \
462 cb_ptr += src->linesize[1] - width2; \
463 cr_ptr += src->linesize[2] - width2; \
467 /* XXX: no chroma interpolating is done */ \
468 static void yuv422p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \
469 int width, int height) \
471 UINT8 *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1; \
472 int w, y, cb, cr, r_add, g_add, b_add, width2; \
473 UINT8 *cm = cropTbl + MAX_NEG_CROP; \
474 unsigned int r, g, b; \
477 y1_ptr = src->data[0]; \
478 cb_ptr = src->data[1]; \
479 cr_ptr = src->data[2]; \
480 width2 = width >> 1; \
481 for(;height > 0; height --) { \
483 for(w = width2; w > 0; w --) { \
484 cb = cb_ptr[0] - 128; \
485 cr = cr_ptr[0] - 128; \
486 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
487 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
488 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
490 /* output 2 pixels */ \
491 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
492 RGB_OUT(d, r, g, b); \
494 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
495 RGB_OUT(d + BPP, r, g, b); \
503 d += dst->linesize[0]; \
504 y1_ptr += src->linesize[0] - width; \
505 cb_ptr += src->linesize[1] - width2; \
506 cr_ptr += src->linesize[2] - width2; \
510 static void rgb_name ## _to_yuv420p(AVPicture *dst, AVPicture *src, \
511 int width, int height) \
513 int wrap, wrap3, x, y; \
514 int r, g, b, r1, g1, b1; \
515 UINT8 *lum, *cb, *cr; \
518 lum = dst->data[0]; \
525 for(y=0;y<height;y+=2) { \
526 for(x=0;x<width;x+=2) { \
527 RGB_IN(r, g, b, p); \
531 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
532 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
533 RGB_IN(r, g, b, p + BPP); \
537 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \
538 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
542 RGB_IN(r, g, b, p); \
546 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
547 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
549 RGB_IN(r, g, b, p + BPP); \
553 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \
554 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
556 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
557 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> \
558 (SCALEBITS + 2)) + 128; \
559 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
560 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> \
561 (SCALEBITS + 2)) + 128; \
565 p += -wrap3 + 2 * 3; \
573 /* copy bit n to bits 0 ... n - 1 */
574 static inline unsigned int bitcopy_n(unsigned int a, int n)
578 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
581 /* rgb555 handling */
583 #define RGB_IN(r, g, b, s)\
585 unsigned int v = ((UINT16 *)(s))[0];\
586 r = bitcopy_n(v >> (10 - 3), 3);\
587 g = bitcopy_n(v >> (5 - 3), 3);\
588 b = bitcopy_n(v << 3, 3);\
591 #define RGB_OUT(d, r, g, b)\
593 ((UINT16 *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;\
598 RGB_FUNCTIONS(rgb555)
604 /* rgb565 handling */
606 #define RGB_IN(r, g, b, s)\
608 unsigned int v = ((UINT16 *)(s))[0];\
609 r = bitcopy_n(v >> (11 - 3), 3);\
610 g = bitcopy_n(v >> (5 - 2), 2);\
611 b = bitcopy_n(v << 3, 3);\
614 #define RGB_OUT(d, r, g, b)\
616 ((UINT16 *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
621 RGB_FUNCTIONS(rgb565)
629 #define RGB_IN(r, g, b, s)\
636 #define RGB_OUT(d, r, g, b)\
653 #define RGB_IN(r, g, b, s)\
660 #define RGB_OUT(d, r, g, b)\
675 /* rgba32 handling */
677 #define RGB_IN(r, g, b, s)\
679 unsigned int v = ((UINT32 *)(s))[0];\
680 r = (v >> 16) & 0xff;\
681 g = (v >> 8) & 0xff;\
685 #define RGB_OUT(d, r, g, b)\
687 ((UINT32 *)(d))[0] = (0xff << 24) | (r << 16) | (g << 8) | b;\
692 RGB_FUNCTIONS(rgba32)
699 static void rgb24_to_rgb565(AVPicture *dst, AVPicture *src,
700 int width, int height)
702 const unsigned char *p;
704 int r, g, b, dst_wrap, src_wrap;
708 src_wrap = src->linesize[0] - 3 * width;
711 dst_wrap = dst->linesize[0] - 2 * width;
713 for(y=0;y<height;y++) {
714 for(x=0;x<width;x++) {
719 ((unsigned short *)q)[0] =
720 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
729 /* NOTE: we also add a dummy alpha bit */
730 static void rgb24_to_rgb555(AVPicture *dst, AVPicture *src,
731 int width, int height)
733 const unsigned char *p;
735 int r, g, b, dst_wrap, src_wrap;
739 src_wrap = src->linesize[0] - 3 * width;
742 dst_wrap = dst->linesize[0] - 2 * width;
744 for(y=0;y<height;y++) {
745 for(x=0;x<width;x++) {
750 ((unsigned short *)q)[0] =
751 ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;
760 static void rgb24_to_gray(AVPicture *dst, AVPicture *src,
761 int width, int height)
763 const unsigned char *p;
765 int r, g, b, dst_wrap, src_wrap;
769 src_wrap = src->linesize[0] - 3 * width;
772 dst_wrap = dst->linesize[0] - width;
774 for(y=0;y<height;y++) {
775 for(x=0;x<width;x++) {
780 q[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
781 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
790 static void gray_to_rgb24(AVPicture *dst, AVPicture *src,
791 int width, int height)
793 const unsigned char *p;
795 int r, dst_wrap, src_wrap;
799 src_wrap = src->linesize[0] - width;
802 dst_wrap = dst->linesize[0] - 3 * width;
804 for(y=0;y<height;y++) {
805 for(x=0;x<width;x++) {
820 static void mono_to_gray(AVPicture *dst, AVPicture *src,
821 int width, int height, int xor_mask)
823 const unsigned char *p;
825 int v, dst_wrap, src_wrap;
829 src_wrap = src->linesize[0] - ((width + 7) >> 3);
832 dst_wrap = dst->linesize[0] - width;
833 for(y=0;y<height;y++) {
838 q[1] = -((v >> 6) & 1);
839 q[2] = -((v >> 5) & 1);
840 q[3] = -((v >> 4) & 1);
841 q[4] = -((v >> 3) & 1);
842 q[5] = -((v >> 2) & 1);
843 q[6] = -((v >> 1) & 1);
844 q[7] = -((v >> 0) & 1);
851 q[0] = -((v >> 7) & 1);
861 static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
862 int width, int height)
864 mono_to_gray(dst, src, width, height, 0xff);
867 static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
868 int width, int height)
870 mono_to_gray(dst, src, width, height, 0x00);
873 static void gray_to_mono(AVPicture *dst, AVPicture *src,
874 int width, int height, int xor_mask)
879 int j, b, v, n1, src_wrap, dst_wrap, y;
882 src_wrap = src->linesize[0] - width;
885 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
886 printf("%d %d\n", width, height);
888 for(y=0;y<height;y++) {
895 v = (v << 1) | (b >> 7);
907 v = (v << 1) | (b >> 7);
910 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
918 static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
919 int width, int height)
921 gray_to_mono(dst, src, width, height, 0xff);
924 static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
925 int width, int height)
927 gray_to_mono(dst, src, width, height, 0x00);
930 typedef struct ConvertEntry {
931 void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
934 /* add each new convertion function in this table */
936 - all non YUV modes must convert at least to and from PIX_FMT_RGB24
938 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
939 [PIX_FMT_YUV420P] = {
941 .convert = yuv420p_to_rgb555
944 .convert = yuv420p_to_rgb565
947 .convert = yuv420p_to_bgr24
950 .convert = yuv420p_to_rgb24
953 .convert = yuv420p_to_rgba32
956 [PIX_FMT_YUV422P] = {
958 .convert = yuv422p_to_rgb555
961 .convert = yuv422p_to_rgb565
964 .convert = yuv422p_to_bgr24
967 .convert = yuv422p_to_rgb24
970 .convert = yuv422p_to_rgba32
974 [PIX_FMT_YUV420P] = {
975 .convert = yuv422_to_yuv420p,
980 [PIX_FMT_YUV420P] = {
981 .convert = rgb24_to_yuv420p
984 .convert = rgb24_to_rgb565
987 .convert = rgb24_to_rgb555
990 .convert = rgb24_to_gray
994 [PIX_FMT_YUV420P] = {
995 .convert = rgba32_to_yuv420p
999 [PIX_FMT_YUV420P] = {
1000 .convert = bgr24_to_yuv420p
1003 [PIX_FMT_RGB555] = {
1004 [PIX_FMT_YUV420P] = {
1005 .convert = rgb555_to_yuv420p
1008 [PIX_FMT_RGB565] = {
1009 [PIX_FMT_YUV420P] = {
1010 .convert = rgb565_to_yuv420p
1015 .convert = gray_to_rgb24
1017 [PIX_FMT_MONOWHITE] = {
1018 .convert = gray_to_monowhite
1020 [PIX_FMT_MONOBLACK] = {
1021 .convert = gray_to_monoblack
1024 [PIX_FMT_MONOWHITE] = {
1026 .convert = monowhite_to_gray
1029 [PIX_FMT_MONOBLACK] = {
1031 .convert = monoblack_to_gray
1036 static int avpicture_alloc(AVPicture *picture,
1037 int pix_fmt, int width, int height)
1042 size = avpicture_get_size(pix_fmt, width, height);
1045 ptr = av_malloc(size);
1048 avpicture_fill(picture, ptr, pix_fmt, width, height);
1051 memset(picture, 0, sizeof(AVPicture));
1055 static void avpicture_free(AVPicture *picture)
1057 free(picture->data[0]);
1060 /* XXX: always use linesize. Return -1 if not supported */
1061 int img_convert(AVPicture *dst, int dst_pix_fmt,
1062 AVPicture *src, int src_pix_fmt,
1063 int src_width, int src_height)
1065 int i, ret, dst_width, dst_height, int_pix_fmt;
1066 PixFmtInfo *src_pix, *dst_pix;
1068 AVPicture tmp1, *tmp = &tmp1;
1070 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1071 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1073 if (src_width <= 0 || src_height <= 0)
1076 dst_width = src_width;
1077 dst_height = src_height;
1079 dst_pix = &pix_fmt_info[dst_pix_fmt];
1080 src_pix = &pix_fmt_info[src_pix_fmt];
1081 if (src_pix_fmt == dst_pix_fmt) {
1082 /* XXX: incorrect */
1083 /* same format: just copy */
1084 for(i = 0; i < dst_pix->nb_components; i++) {
1088 if (dst_pix->is_yuv && (i == 1 || i == 2)) {
1089 w >>= dst_pix->x_chroma_shift;
1090 h >>= dst_pix->y_chroma_shift;
1092 img_copy(dst->data[i], dst->linesize[i],
1093 src->data[i], src->linesize[i],
1099 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1101 /* specific convertion routine */
1102 ce->convert(dst, src, dst_width, dst_height);
1107 if (dst_pix->is_yuv && src_pix_fmt == PIX_FMT_GRAY8) {
1111 img_copy(dst->data[0], dst->linesize[0],
1112 src->data[0], src->linesize[0],
1113 dst_width, dst_height);
1114 /* fill U and V with 128 */
1117 w >>= dst_pix->x_chroma_shift;
1118 h >>= dst_pix->y_chroma_shift;
1119 for(i = 1; i <= 2; i++) {
1121 for(y = 0; y< h; y++) {
1123 d += dst->linesize[i];
1130 if (src_pix->is_yuv && dst_pix_fmt == PIX_FMT_GRAY8) {
1131 img_copy(dst->data[0], dst->linesize[0],
1132 src->data[0], src->linesize[0],
1133 dst_width, dst_height);
1138 if (dst_pix->is_yuv && src_pix->is_yuv) {
1139 int x_shift, y_shift, w, h;
1140 void (*resize_func)(UINT8 *dst, int dst_wrap,
1141 UINT8 *src, int src_wrap,
1142 int width, int height);
1144 /* compute chroma size of the smallest dimensions */
1147 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1148 w >>= dst_pix->x_chroma_shift;
1150 w >>= src_pix->x_chroma_shift;
1151 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1152 h >>= dst_pix->y_chroma_shift;
1154 h >>= src_pix->y_chroma_shift;
1156 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1157 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
1158 if (x_shift == 0 && y_shift == 0) {
1159 resize_func = img_copy; /* should never happen */
1160 } else if (x_shift == 0 && y_shift == 1) {
1161 resize_func = shrink2;
1162 } else if (x_shift == 1 && y_shift == 1) {
1163 resize_func = shrink22;
1164 } else if (x_shift == -1 && y_shift == -1) {
1165 resize_func = grow22;
1166 } else if (x_shift == -1 && y_shift == 1) {
1167 resize_func = conv411;
1169 /* currently not handled */
1173 img_copy(dst->data[0], dst->linesize[0],
1174 src->data[0], src->linesize[0],
1175 dst_width, dst_height);
1176 for(i = 1;i <= 2; i++)
1177 resize_func(dst->data[1], dst->linesize[1],
1178 src->data[1], src->linesize[1],
1182 /* try to use an intermediate format */
1183 if (src_pix_fmt == PIX_FMT_MONOWHITE ||
1184 src_pix_fmt == PIX_FMT_MONOBLACK ||
1185 dst_pix_fmt == PIX_FMT_MONOWHITE ||
1186 dst_pix_fmt == PIX_FMT_MONOBLACK) {
1187 int_pix_fmt = PIX_FMT_GRAY8;
1189 int_pix_fmt = PIX_FMT_RGB24;
1191 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
1194 if (img_convert(tmp, int_pix_fmt,
1195 src, src_pix_fmt, src_width, src_height) < 0)
1197 if (img_convert(dst, dst_pix_fmt,
1198 tmp, int_pix_fmt, dst_width, dst_height) < 0)
1202 avpicture_free(tmp);
1208 #define DEINT_INPLACE_LINE_LUM \
1209 movd_m2r(lum_m4[0],mm0);\
1210 movd_m2r(lum_m3[0],mm1);\
1211 movd_m2r(lum_m2[0],mm2);\
1212 movd_m2r(lum_m1[0],mm3);\
1213 movd_m2r(lum[0],mm4);\
1214 punpcklbw_r2r(mm7,mm0);\
1215 movd_r2m(mm2,lum_m4[0]);\
1216 punpcklbw_r2r(mm7,mm1);\
1217 punpcklbw_r2r(mm7,mm2);\
1218 punpcklbw_r2r(mm7,mm3);\
1219 punpcklbw_r2r(mm7,mm4);\
1220 paddw_r2r(mm3,mm1);\
1222 paddw_r2r(mm4,mm0);\
1224 paddw_r2r(mm6,mm2);\
1225 paddw_r2r(mm2,mm1);\
1226 psubusw_r2r(mm0,mm1);\
1228 packuswb_r2r(mm7,mm1);\
1229 movd_r2m(mm1,lum_m2[0]);
1231 #define DEINT_LINE_LUM \
1232 movd_m2r(lum_m4[0],mm0);\
1233 movd_m2r(lum_m3[0],mm1);\
1234 movd_m2r(lum_m2[0],mm2);\
1235 movd_m2r(lum_m1[0],mm3);\
1236 movd_m2r(lum[0],mm4);\
1237 punpcklbw_r2r(mm7,mm0);\
1238 punpcklbw_r2r(mm7,mm1);\
1239 punpcklbw_r2r(mm7,mm2);\
1240 punpcklbw_r2r(mm7,mm3);\
1241 punpcklbw_r2r(mm7,mm4);\
1242 paddw_r2r(mm3,mm1);\
1244 paddw_r2r(mm4,mm0);\
1246 paddw_r2r(mm6,mm2);\
1247 paddw_r2r(mm2,mm1);\
1248 psubusw_r2r(mm0,mm1);\
1250 packuswb_r2r(mm7,mm1);\
1251 movd_r2m(mm1,dst[0]);
1254 /* filter parameters: [-1 4 2 4 -1] // 8 */
1255 static void deinterlace_line(UINT8 *dst, UINT8 *lum_m4, UINT8 *lum_m3, UINT8 *lum_m2, UINT8 *lum_m1, UINT8 *lum,
1259 UINT8 *cm = cropTbl + MAX_NEG_CROP;
1262 for(;size > 0;size--) {
1264 sum += lum_m3[0] << 2;
1265 sum += lum_m2[0] << 1;
1266 sum += lum_m1[0] << 2;
1268 dst[0] = cm[(sum + 4) >> 3];
1278 for (;size > 3; size-=4) {
1289 static void deinterlace_line_inplace(UINT8 *lum_m4, UINT8 *lum_m3, UINT8 *lum_m2, UINT8 *lum_m1, UINT8 *lum,
1293 UINT8 *cm = cropTbl + MAX_NEG_CROP;
1296 for(;size > 0;size--) {
1298 sum += lum_m3[0] << 2;
1299 sum += lum_m2[0] << 1;
1300 lum_m4[0]=lum_m2[0];
1301 sum += lum_m1[0] << 2;
1303 lum_m2[0] = cm[(sum + 4) >> 3];
1312 for (;size > 3; size-=4) {
1313 DEINT_INPLACE_LINE_LUM
1323 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1324 top field is copied as is, but the bottom field is deinterlaced
1325 against the top field. */
1326 static void deinterlace_bottom_field(UINT8 *dst, int dst_wrap,
1327 UINT8 *src1, int src_wrap,
1328 int width, int height)
1330 UINT8 *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1335 src_0=&src_m1[src_wrap];
1336 src_p1=&src_0[src_wrap];
1337 src_p2=&src_p1[src_wrap];
1338 for(y=0;y<(height-2);y+=2) {
1339 memcpy(dst,src_m1,width);
1341 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1345 src_p1 += 2*src_wrap;
1346 src_p2 += 2*src_wrap;
1349 memcpy(dst,src_m1,width);
1352 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1355 static void deinterlace_bottom_field_inplace(UINT8 *src1, int src_wrap,
1356 int width, int height)
1358 UINT8 *src_m1, *src_0, *src_p1, *src_p2;
1361 buf = (UINT8*)av_malloc(width);
1364 memcpy(buf,src_m1,width);
1365 src_0=&src_m1[src_wrap];
1366 src_p1=&src_0[src_wrap];
1367 src_p2=&src_p1[src_wrap];
1368 for(y=0;y<(height-2);y+=2) {
1369 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1372 src_p1 += 2*src_wrap;
1373 src_p2 += 2*src_wrap;
1376 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1381 /* deinterlace - if not supported return -1 */
1382 int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
1383 int pix_fmt, int width, int height)
1387 if (pix_fmt != PIX_FMT_YUV420P &&
1388 pix_fmt != PIX_FMT_YUV422P &&
1389 pix_fmt != PIX_FMT_YUV444P)
1391 if ((width & 3) != 0 || (height & 3) != 0)
1402 movq_m2r(rounder,mm6);
1410 case PIX_FMT_YUV420P:
1414 case PIX_FMT_YUV422P:
1422 deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
1425 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1426 src->data[i], src->linesize[i],