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)
136 int size, w2, h2, size2;
139 pinfo = &pix_fmt_info[pix_fmt];
140 size = width * height;
142 case PIX_FMT_YUV420P:
143 case PIX_FMT_YUV422P:
144 case PIX_FMT_YUV444P:
145 case PIX_FMT_YUV410P:
146 case PIX_FMT_YUV411P:
147 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
148 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
150 picture->data[0] = ptr;
151 picture->data[1] = picture->data[0] + size;
152 picture->data[2] = picture->data[1] + size2;
153 picture->linesize[0] = width;
154 picture->linesize[1] = w2;
155 picture->linesize[2] = w2;
156 return size + 2 * size2;
159 picture->data[0] = ptr;
160 picture->data[1] = NULL;
161 picture->data[2] = NULL;
162 picture->linesize[0] = width * 3;
165 picture->data[0] = ptr;
166 picture->data[1] = NULL;
167 picture->data[2] = NULL;
168 picture->linesize[0] = width * 4;
173 picture->data[0] = ptr;
174 picture->data[1] = NULL;
175 picture->data[2] = NULL;
176 picture->linesize[0] = width * 2;
179 picture->data[0] = ptr;
180 picture->data[1] = NULL;
181 picture->data[2] = NULL;
182 picture->linesize[0] = width;
184 case PIX_FMT_MONOWHITE:
185 case PIX_FMT_MONOBLACK:
186 picture->data[0] = ptr;
187 picture->data[1] = NULL;
188 picture->data[2] = NULL;
189 picture->linesize[0] = (width + 7) >> 3;
190 return picture->linesize[0] * height;
192 picture->data[0] = NULL;
193 picture->data[1] = NULL;
194 picture->data[2] = NULL;
199 int avpicture_get_size(int pix_fmt, int width, int height)
201 AVPicture dummy_pict;
202 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
206 /* XXX: totally non optimized */
208 static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
209 int width, int height)
211 UINT8 *lum, *cb, *cr;
220 for(y=0;y<height;y+=2) {
221 for(x=0;x<width;x+=2) {
231 for(x=0;x<width;x+=2) {
241 #define ONE_HALF (1 << (SCALEBITS - 1))
242 #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
244 /* XXX: use generic filter ? */
246 static void shrink2(UINT8 *dst, int dst_wrap,
247 UINT8 *src, int src_wrap,
248 int width, int height)
253 for(;height > 0; height--) {
257 for(w = width;w >= 4; w-=4) {
258 d[0] = (s1[0] + s2[0]) >> 1;
259 d[1] = (s1[1] + s2[1]) >> 1;
260 d[2] = (s1[2] + s2[2]) >> 1;
261 d[3] = (s1[3] + s2[3]) >> 1;
267 d[0] = (s1[0] + s2[0]) >> 1;
278 static void shrink22(UINT8 *dst, int dst_wrap,
279 UINT8 *src, int src_wrap,
280 int width, int height)
285 for(;height > 0; height--) {
289 for(w = width;w >= 4; w-=4) {
290 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
291 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1;
292 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1;
293 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1;
299 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
310 static void grow22(UINT8 *dst, int dst_wrap,
311 UINT8 *src, int src_wrap,
312 int width, int height)
317 for(;height > 0; height--) {
320 for(w = width;w >= 4; w-=4) {
338 static void conv411(UINT8 *dst, int dst_wrap,
339 UINT8 *src, int src_wrap,
340 int width, int height)
345 for(;height > 0; height--) {
349 for(w = width;w > 0; w--) {
350 c = (s1[0] + s2[0]) >> 1;
362 static void img_copy(UINT8 *dst, int dst_wrap,
363 UINT8 *src, int src_wrap,
364 int width, int height)
366 for(;height > 0; height--) {
367 memcpy(dst, src, width);
373 #define SCALE_BITS 10
375 #define C_Y (76309 >> (16 - SCALE_BITS))
376 #define C_RV (117504 >> (16 - SCALE_BITS))
377 #define C_BU (138453 >> (16 - SCALE_BITS))
378 #define C_GU (13954 >> (16 - SCALE_BITS))
379 #define C_GV (34903 >> (16 - SCALE_BITS))
381 #define YUV_TO_RGB2(r, g, b, y1)\
383 y = (y1 - 16) * C_Y;\
384 r = cm[(y + r_add) >> SCALE_BITS];\
385 g = cm[(y + g_add) >> SCALE_BITS];\
386 b = cm[(y + b_add) >> SCALE_BITS];\
389 /* XXX: no chroma interpolating is done */
390 #define RGB_FUNCTIONS(rgb_name) \
392 static void yuv420p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \
393 int width, int height) \
395 UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2; \
396 int w, y, cb, cr, r_add, g_add, b_add, width2; \
397 UINT8 *cm = cropTbl + MAX_NEG_CROP; \
398 unsigned int r, g, b; \
401 y1_ptr = src->data[0]; \
402 cb_ptr = src->data[1]; \
403 cr_ptr = src->data[2]; \
404 width2 = (width + 1) >> 1; \
405 for(;height >= 2; height -= 2) { \
407 d2 = d + dst->linesize[0]; \
408 y2_ptr = y1_ptr + src->linesize[0]; \
409 for(w = width; w >= 2; w -= 2) { \
410 cb = cb_ptr[0] - 128; \
411 cr = cr_ptr[0] - 128; \
412 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
413 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
414 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
416 /* output 4 pixels */ \
417 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
418 RGB_OUT(d1, r, g, b); \
420 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
421 RGB_OUT(d1 + BPP, r, g, b); \
423 YUV_TO_RGB2(r, g, b, y2_ptr[0]); \
424 RGB_OUT(d2, r, g, b); \
426 YUV_TO_RGB2(r, g, b, y2_ptr[1]); \
427 RGB_OUT(d2 + BPP, r, g, b); \
437 /* handle odd width */ \
439 cb = cb_ptr[0] - 128; \
440 cr = cr_ptr[0] - 128; \
441 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
442 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
443 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
445 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
446 RGB_OUT(d1, r, g, b); \
448 YUV_TO_RGB2(r, g, b, y2_ptr[0]); \
449 RGB_OUT(d2, r, g, b); \
457 d += 2 * dst->linesize[0]; \
458 y1_ptr += 2 * src->linesize[0] - width; \
459 cb_ptr += src->linesize[1] - width2; \
460 cr_ptr += src->linesize[2] - width2; \
462 /* handle odd height */ \
465 for(w = width; w >= 2; w -= 2) { \
466 cb = cb_ptr[0] - 128; \
467 cr = cr_ptr[0] - 128; \
468 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
469 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
470 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
472 /* output 2 pixels */ \
473 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
474 RGB_OUT(d1, r, g, b); \
476 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
477 RGB_OUT(d1 + BPP, r, g, b); \
487 cb = cb_ptr[0] - 128; \
488 cr = cr_ptr[0] - 128; \
489 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
490 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
491 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
493 /* output 2 pixels */ \
494 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
495 RGB_OUT(d1, r, g, b); \
505 /* XXX: no chroma interpolating is done */ \
506 static void yuv422p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \
507 int width, int height) \
509 UINT8 *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1; \
510 int w, y, cb, cr, r_add, g_add, b_add, width2; \
511 UINT8 *cm = cropTbl + MAX_NEG_CROP; \
512 unsigned int r, g, b; \
515 y1_ptr = src->data[0]; \
516 cb_ptr = src->data[1]; \
517 cr_ptr = src->data[2]; \
518 width2 = (width + 1) >> 1; \
519 for(;height > 0; height --) { \
521 for(w = width; w >= 2; w -= 2) { \
522 cb = cb_ptr[0] - 128; \
523 cr = cr_ptr[0] - 128; \
524 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
525 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
526 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
528 /* output 2 pixels */ \
529 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
530 RGB_OUT(d1, r, g, b); \
532 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
533 RGB_OUT(d1 + BPP, r, g, b); \
543 cb = cb_ptr[0] - 128; \
544 cr = cr_ptr[0] - 128; \
545 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
546 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
547 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
549 /* output 2 pixels */ \
550 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
551 RGB_OUT(d1, r, g, b); \
558 d += dst->linesize[0]; \
559 y1_ptr += src->linesize[0] - width; \
560 cb_ptr += src->linesize[1] - width2; \
561 cr_ptr += src->linesize[2] - width2; \
565 static void rgb_name ## _to_yuv420p(AVPicture *dst, AVPicture *src, \
566 int width, int height) \
568 int wrap, wrap3, x, y; \
569 int r, g, b, r1, g1, b1; \
570 UINT8 *lum, *cb, *cr; \
573 lum = dst->data[0]; \
577 wrap = dst->linesize[0]; \
578 wrap3 = src->linesize[0]; \
580 for(y=0;y<height;y+=2) { \
581 for(x=0;x<width;x+=2) { \
582 RGB_IN(r, g, b, p); \
586 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
587 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
588 RGB_IN(r, g, b, p + BPP); \
592 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \
593 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
597 RGB_IN(r, g, b, p); \
601 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
602 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
604 RGB_IN(r, g, b, p + BPP); \
608 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \
609 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
611 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
612 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> \
613 (SCALEBITS + 2)) + 128; \
614 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
615 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> \
616 (SCALEBITS + 2)) + 128; \
620 p += -wrap3 + 2 * BPP; \
623 p += wrap3 + (wrap3 - width * BPP); \
624 lum += wrap + (wrap - width); \
625 cb += dst->linesize[1] - width / 2; \
626 cr += dst->linesize[2] - width / 2; \
630 static void rgb_name ## _to_gray(AVPicture *dst, AVPicture *src, \
631 int width, int height) \
633 const unsigned char *p; \
635 int r, g, b, dst_wrap, src_wrap; \
639 src_wrap = src->linesize[0] - BPP * width; \
642 dst_wrap = dst->linesize[0] - width; \
644 for(y=0;y<height;y++) { \
645 for(x=0;x<width;x++) { \
646 RGB_IN(r, g, b, p); \
647 q[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
648 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
657 static void gray_to_ ## rgb_name(AVPicture *dst, AVPicture *src, \
658 int width, int height) \
660 const unsigned char *p; \
662 int r, dst_wrap, src_wrap; \
666 src_wrap = src->linesize[0] - width; \
669 dst_wrap = dst->linesize[0] - BPP * width; \
671 for(y=0;y<height;y++) { \
672 for(x=0;x<width;x++) { \
674 RGB_OUT(q, r, r, r); \
683 /* copy bit n to bits 0 ... n - 1 */
684 static inline unsigned int bitcopy_n(unsigned int a, int n)
688 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
691 /* rgb555 handling */
693 #define RGB_IN(r, g, b, s)\
695 unsigned int v = ((UINT16 *)(s))[0];\
696 r = bitcopy_n(v >> (10 - 3), 3);\
697 g = bitcopy_n(v >> (5 - 3), 3);\
698 b = bitcopy_n(v << 3, 3);\
701 #define RGB_OUT(d, r, g, b)\
703 ((UINT16 *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;\
708 RGB_FUNCTIONS(rgb555)
714 /* rgb565 handling */
716 #define RGB_IN(r, g, b, s)\
718 unsigned int v = ((UINT16 *)(s))[0];\
719 r = bitcopy_n(v >> (11 - 3), 3);\
720 g = bitcopy_n(v >> (5 - 2), 2);\
721 b = bitcopy_n(v << 3, 3);\
724 #define RGB_OUT(d, r, g, b)\
726 ((UINT16 *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
731 RGB_FUNCTIONS(rgb565)
739 #define RGB_IN(r, g, b, s)\
746 #define RGB_OUT(d, r, g, b)\
763 #define RGB_IN(r, g, b, s)\
770 #define RGB_OUT(d, r, g, b)\
785 /* rgba32 handling */
787 #define RGB_IN(r, g, b, s)\
789 unsigned int v = ((UINT32 *)(s))[0];\
790 r = (v >> 16) & 0xff;\
791 g = (v >> 8) & 0xff;\
795 #define RGB_OUT(d, r, g, b)\
797 ((UINT32 *)(d))[0] = (0xff << 24) | (r << 16) | (g << 8) | b;\
802 RGB_FUNCTIONS(rgba32)
809 static void rgb24_to_rgb565(AVPicture *dst, AVPicture *src,
810 int width, int height)
812 const unsigned char *p;
814 int r, g, b, dst_wrap, src_wrap;
818 src_wrap = src->linesize[0] - 3 * width;
821 dst_wrap = dst->linesize[0] - 2 * width;
823 for(y=0;y<height;y++) {
824 for(x=0;x<width;x++) {
829 ((unsigned short *)q)[0] =
830 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
839 /* NOTE: we also add a dummy alpha bit */
840 static void rgb24_to_rgb555(AVPicture *dst, AVPicture *src,
841 int width, int height)
843 const unsigned char *p;
845 int r, g, b, dst_wrap, src_wrap;
849 src_wrap = src->linesize[0] - 3 * width;
852 dst_wrap = dst->linesize[0] - 2 * width;
854 for(y=0;y<height;y++) {
855 for(x=0;x<width;x++) {
860 ((unsigned short *)q)[0] =
861 ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;
870 static void mono_to_gray(AVPicture *dst, AVPicture *src,
871 int width, int height, int xor_mask)
873 const unsigned char *p;
875 int v, dst_wrap, src_wrap;
879 src_wrap = src->linesize[0] - ((width + 7) >> 3);
882 dst_wrap = dst->linesize[0] - width;
883 for(y=0;y<height;y++) {
888 q[1] = -((v >> 6) & 1);
889 q[2] = -((v >> 5) & 1);
890 q[3] = -((v >> 4) & 1);
891 q[4] = -((v >> 3) & 1);
892 q[5] = -((v >> 2) & 1);
893 q[6] = -((v >> 1) & 1);
894 q[7] = -((v >> 0) & 1);
901 q[0] = -((v >> 7) & 1);
911 static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
912 int width, int height)
914 mono_to_gray(dst, src, width, height, 0xff);
917 static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
918 int width, int height)
920 mono_to_gray(dst, src, width, height, 0x00);
923 static void gray_to_mono(AVPicture *dst, AVPicture *src,
924 int width, int height, int xor_mask)
929 int j, b, v, n1, src_wrap, dst_wrap, y;
932 src_wrap = src->linesize[0] - width;
935 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
936 printf("%d %d\n", width, height);
938 for(y=0;y<height;y++) {
945 v = (v << 1) | (b >> 7);
957 v = (v << 1) | (b >> 7);
960 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
968 static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
969 int width, int height)
971 gray_to_mono(dst, src, width, height, 0xff);
974 static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
975 int width, int height)
977 gray_to_mono(dst, src, width, height, 0x00);
980 typedef struct ConvertEntry {
981 void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
984 /* add each new convertion function in this table */
986 - all non YUV modes must convert at least to and from PIX_FMT_RGB24
988 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
989 [PIX_FMT_YUV420P] = {
991 .convert = yuv420p_to_rgb555
994 .convert = yuv420p_to_rgb565
997 .convert = yuv420p_to_bgr24
1000 .convert = yuv420p_to_rgb24
1002 [PIX_FMT_RGBA32] = {
1003 .convert = yuv420p_to_rgba32
1006 [PIX_FMT_YUV422P] = {
1007 [PIX_FMT_RGB555] = {
1008 .convert = yuv422p_to_rgb555
1010 [PIX_FMT_RGB565] = {
1011 .convert = yuv422p_to_rgb565
1014 .convert = yuv422p_to_bgr24
1017 .convert = yuv422p_to_rgb24
1019 [PIX_FMT_RGBA32] = {
1020 .convert = yuv422p_to_rgba32
1023 [PIX_FMT_YUV422] = {
1024 [PIX_FMT_YUV420P] = {
1025 .convert = yuv422_to_yuv420p,
1030 [PIX_FMT_YUV420P] = {
1031 .convert = rgb24_to_yuv420p
1033 [PIX_FMT_RGB565] = {
1034 .convert = rgb24_to_rgb565
1036 [PIX_FMT_RGB555] = {
1037 .convert = rgb24_to_rgb555
1040 .convert = rgb24_to_gray
1043 [PIX_FMT_RGBA32] = {
1044 [PIX_FMT_YUV420P] = {
1045 .convert = rgba32_to_yuv420p
1048 .convert = rgba32_to_gray
1052 [PIX_FMT_YUV420P] = {
1053 .convert = bgr24_to_yuv420p
1056 .convert = bgr24_to_gray
1059 [PIX_FMT_RGB555] = {
1060 [PIX_FMT_YUV420P] = {
1061 .convert = rgb555_to_yuv420p
1064 .convert = rgb555_to_gray
1067 [PIX_FMT_RGB565] = {
1068 [PIX_FMT_YUV420P] = {
1069 .convert = rgb565_to_yuv420p
1072 .convert = rgb565_to_gray
1076 [PIX_FMT_RGB555] = {
1077 .convert = gray_to_rgb555
1079 [PIX_FMT_RGB565] = {
1080 .convert = gray_to_rgb565
1083 .convert = gray_to_rgb24
1086 .convert = gray_to_bgr24
1088 [PIX_FMT_RGBA32] = {
1089 .convert = gray_to_rgba32
1091 [PIX_FMT_MONOWHITE] = {
1092 .convert = gray_to_monowhite
1094 [PIX_FMT_MONOBLACK] = {
1095 .convert = gray_to_monoblack
1098 [PIX_FMT_MONOWHITE] = {
1100 .convert = monowhite_to_gray
1103 [PIX_FMT_MONOBLACK] = {
1105 .convert = monoblack_to_gray
1110 static int avpicture_alloc(AVPicture *picture,
1111 int pix_fmt, int width, int height)
1116 size = avpicture_get_size(pix_fmt, width, height);
1119 ptr = av_malloc(size);
1122 avpicture_fill(picture, ptr, pix_fmt, width, height);
1125 memset(picture, 0, sizeof(AVPicture));
1129 static void avpicture_free(AVPicture *picture)
1131 av_free(picture->data[0]);
1134 /* XXX: always use linesize. Return -1 if not supported */
1135 int img_convert(AVPicture *dst, int dst_pix_fmt,
1136 AVPicture *src, int src_pix_fmt,
1137 int src_width, int src_height)
1139 int i, ret, dst_width, dst_height, int_pix_fmt;
1140 PixFmtInfo *src_pix, *dst_pix;
1142 AVPicture tmp1, *tmp = &tmp1;
1144 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1145 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1147 if (src_width <= 0 || src_height <= 0)
1150 dst_width = src_width;
1151 dst_height = src_height;
1153 dst_pix = &pix_fmt_info[dst_pix_fmt];
1154 src_pix = &pix_fmt_info[src_pix_fmt];
1155 if (src_pix_fmt == dst_pix_fmt) {
1156 /* XXX: incorrect */
1157 /* same format: just copy */
1158 for(i = 0; i < dst_pix->nb_components; i++) {
1162 if (dst_pix->is_yuv && (i == 1 || i == 2)) {
1163 w >>= dst_pix->x_chroma_shift;
1164 h >>= dst_pix->y_chroma_shift;
1166 img_copy(dst->data[i], dst->linesize[i],
1167 src->data[i], src->linesize[i],
1173 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1175 /* specific convertion routine */
1176 ce->convert(dst, src, dst_width, dst_height);
1181 if (dst_pix->is_yuv && src_pix_fmt == PIX_FMT_GRAY8) {
1185 img_copy(dst->data[0], dst->linesize[0],
1186 src->data[0], src->linesize[0],
1187 dst_width, dst_height);
1188 /* fill U and V with 128 */
1191 w >>= dst_pix->x_chroma_shift;
1192 h >>= dst_pix->y_chroma_shift;
1193 for(i = 1; i <= 2; i++) {
1195 for(y = 0; y< h; y++) {
1197 d += dst->linesize[i];
1204 if (src_pix->is_yuv && dst_pix_fmt == PIX_FMT_GRAY8) {
1205 img_copy(dst->data[0], dst->linesize[0],
1206 src->data[0], src->linesize[0],
1207 dst_width, dst_height);
1212 if (dst_pix->is_yuv && src_pix->is_yuv) {
1213 int x_shift, y_shift, w, h;
1214 void (*resize_func)(UINT8 *dst, int dst_wrap,
1215 UINT8 *src, int src_wrap,
1216 int width, int height);
1218 /* compute chroma size of the smallest dimensions */
1221 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1222 w >>= dst_pix->x_chroma_shift;
1224 w >>= src_pix->x_chroma_shift;
1225 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1226 h >>= dst_pix->y_chroma_shift;
1228 h >>= src_pix->y_chroma_shift;
1230 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1231 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
1232 if (x_shift == 0 && y_shift == 0) {
1233 resize_func = img_copy; /* should never happen */
1234 } else if (x_shift == 0 && y_shift == 1) {
1235 resize_func = shrink2;
1236 } else if (x_shift == 1 && y_shift == 1) {
1237 resize_func = shrink22;
1238 } else if (x_shift == -1 && y_shift == -1) {
1239 resize_func = grow22;
1240 } else if (x_shift == -1 && y_shift == 1) {
1241 resize_func = conv411;
1243 /* currently not handled */
1247 img_copy(dst->data[0], dst->linesize[0],
1248 src->data[0], src->linesize[0],
1249 dst_width, dst_height);
1251 for(i = 1;i <= 2; i++)
1252 resize_func(dst->data[i], dst->linesize[i],
1253 src->data[i], src->linesize[i],
1258 /* try to use an intermediate format */
1259 if (src_pix_fmt == PIX_FMT_MONOWHITE ||
1260 src_pix_fmt == PIX_FMT_MONOBLACK ||
1261 dst_pix_fmt == PIX_FMT_MONOWHITE ||
1262 dst_pix_fmt == PIX_FMT_MONOBLACK) {
1263 int_pix_fmt = PIX_FMT_GRAY8;
1265 int_pix_fmt = PIX_FMT_RGB24;
1267 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
1270 if (img_convert(tmp, int_pix_fmt,
1271 src, src_pix_fmt, src_width, src_height) < 0)
1273 if (img_convert(dst, dst_pix_fmt,
1274 tmp, int_pix_fmt, dst_width, dst_height) < 0)
1278 avpicture_free(tmp);
1284 #define DEINT_INPLACE_LINE_LUM \
1285 movd_m2r(lum_m4[0],mm0);\
1286 movd_m2r(lum_m3[0],mm1);\
1287 movd_m2r(lum_m2[0],mm2);\
1288 movd_m2r(lum_m1[0],mm3);\
1289 movd_m2r(lum[0],mm4);\
1290 punpcklbw_r2r(mm7,mm0);\
1291 movd_r2m(mm2,lum_m4[0]);\
1292 punpcklbw_r2r(mm7,mm1);\
1293 punpcklbw_r2r(mm7,mm2);\
1294 punpcklbw_r2r(mm7,mm3);\
1295 punpcklbw_r2r(mm7,mm4);\
1296 paddw_r2r(mm3,mm1);\
1298 paddw_r2r(mm4,mm0);\
1300 paddw_r2r(mm6,mm2);\
1301 paddw_r2r(mm2,mm1);\
1302 psubusw_r2r(mm0,mm1);\
1304 packuswb_r2r(mm7,mm1);\
1305 movd_r2m(mm1,lum_m2[0]);
1307 #define DEINT_LINE_LUM \
1308 movd_m2r(lum_m4[0],mm0);\
1309 movd_m2r(lum_m3[0],mm1);\
1310 movd_m2r(lum_m2[0],mm2);\
1311 movd_m2r(lum_m1[0],mm3);\
1312 movd_m2r(lum[0],mm4);\
1313 punpcklbw_r2r(mm7,mm0);\
1314 punpcklbw_r2r(mm7,mm1);\
1315 punpcklbw_r2r(mm7,mm2);\
1316 punpcklbw_r2r(mm7,mm3);\
1317 punpcklbw_r2r(mm7,mm4);\
1318 paddw_r2r(mm3,mm1);\
1320 paddw_r2r(mm4,mm0);\
1322 paddw_r2r(mm6,mm2);\
1323 paddw_r2r(mm2,mm1);\
1324 psubusw_r2r(mm0,mm1);\
1326 packuswb_r2r(mm7,mm1);\
1327 movd_r2m(mm1,dst[0]);
1330 /* filter parameters: [-1 4 2 4 -1] // 8 */
1331 static void deinterlace_line(UINT8 *dst, UINT8 *lum_m4, UINT8 *lum_m3, UINT8 *lum_m2, UINT8 *lum_m1, UINT8 *lum,
1335 UINT8 *cm = cropTbl + MAX_NEG_CROP;
1338 for(;size > 0;size--) {
1340 sum += lum_m3[0] << 2;
1341 sum += lum_m2[0] << 1;
1342 sum += lum_m1[0] << 2;
1344 dst[0] = cm[(sum + 4) >> 3];
1361 movq_m2r(rounder,mm6);
1363 for (;size > 3; size-=4) {
1374 static void deinterlace_line_inplace(UINT8 *lum_m4, UINT8 *lum_m3, UINT8 *lum_m2, UINT8 *lum_m1, UINT8 *lum,
1378 UINT8 *cm = cropTbl + MAX_NEG_CROP;
1381 for(;size > 0;size--) {
1383 sum += lum_m3[0] << 2;
1384 sum += lum_m2[0] << 1;
1385 lum_m4[0]=lum_m2[0];
1386 sum += lum_m1[0] << 2;
1388 lum_m2[0] = cm[(sum + 4) >> 3];
1404 movq_m2r(rounder,mm6);
1406 for (;size > 3; size-=4) {
1407 DEINT_INPLACE_LINE_LUM
1417 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1418 top field is copied as is, but the bottom field is deinterlaced
1419 against the top field. */
1420 static void deinterlace_bottom_field(UINT8 *dst, int dst_wrap,
1421 UINT8 *src1, int src_wrap,
1422 int width, int height)
1424 UINT8 *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1429 src_0=&src_m1[src_wrap];
1430 src_p1=&src_0[src_wrap];
1431 src_p2=&src_p1[src_wrap];
1432 for(y=0;y<(height-2);y+=2) {
1433 memcpy(dst,src_m1,width);
1435 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1439 src_p1 += 2*src_wrap;
1440 src_p2 += 2*src_wrap;
1443 memcpy(dst,src_m1,width);
1446 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1449 static void deinterlace_bottom_field_inplace(UINT8 *src1, int src_wrap,
1450 int width, int height)
1452 UINT8 *src_m1, *src_0, *src_p1, *src_p2;
1455 buf = (UINT8*)av_malloc(width);
1458 memcpy(buf,src_m1,width);
1459 src_0=&src_m1[src_wrap];
1460 src_p1=&src_0[src_wrap];
1461 src_p2=&src_p1[src_wrap];
1462 for(y=0;y<(height-2);y+=2) {
1463 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1466 src_p1 += 2*src_wrap;
1467 src_p2 += 2*src_wrap;
1470 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1475 /* deinterlace - if not supported return -1 */
1476 int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
1477 int pix_fmt, int width, int height)
1481 if (pix_fmt != PIX_FMT_YUV420P &&
1482 pix_fmt != PIX_FMT_YUV422P &&
1483 pix_fmt != PIX_FMT_YUV444P)
1485 if ((width & 3) != 0 || (height & 3) != 0)
1491 case PIX_FMT_YUV420P:
1495 case PIX_FMT_YUV422P:
1503 deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
1506 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1507 src->data[i], src->linesize[i],