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.
30 #include "fastmemcpy.h"
37 typedef struct PixFmtInfo {
39 uint8_t nb_components; /* number of components in AVPicture array */
40 uint8_t is_yuv : 1; /* true if YUV instead of RGB color space */
41 uint8_t is_packed : 1; /* true if multiple components in same word */
42 uint8_t is_paletted : 1; /* true if paletted */
43 uint8_t is_alpha : 1; /* true if alpha can be specified */
44 uint8_t is_gray : 1; /* true if gray or monochrome format */
45 uint8_t x_chroma_shift; /* X chroma subsampling factor is 2 ^ shift */
46 uint8_t y_chroma_shift; /* Y chroma subsampling factor is 2 ^ shift */
49 /* this table gives more information about formats */
50 static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
54 .nb_components = 3, .is_yuv = 1,
55 .x_chroma_shift = 1, .y_chroma_shift = 1,
59 .nb_components = 3, .is_yuv = 1,
60 .x_chroma_shift = 1, .y_chroma_shift = 0,
64 .nb_components = 3, .is_yuv = 1,
65 .x_chroma_shift = 0, .y_chroma_shift = 0,
69 .nb_components = 1, .is_yuv = 1, .is_packed = 1,
70 .x_chroma_shift = 1, .y_chroma_shift = 0,
74 .nb_components = 3, .is_yuv = 1,
75 .x_chroma_shift = 2, .y_chroma_shift = 2,
79 .nb_components = 3, .is_yuv = 1,
80 .x_chroma_shift = 2, .y_chroma_shift = 0,
86 .nb_components = 1, .is_packed = 1,
90 .nb_components = 1, .is_packed = 1,
94 .nb_components = 1, .is_packed = 1, .is_alpha = 1,
98 .nb_components = 1, .is_packed = 1,
102 .nb_components = 1, .is_packed = 1, .is_alpha = 1,
105 /* gray / mono formats */
108 .nb_components = 1, .is_gray = 1,
110 [PIX_FMT_MONOWHITE] = {
112 .nb_components = 1, .is_packed = 1, .is_gray = 1,
114 [PIX_FMT_MONOBLACK] = {
116 .nb_components = 1, .is_packed = 1, .is_gray = 1,
119 /* paletted formats */
122 .nb_components = 1, .is_packed = 1, .is_paletted = 1,
126 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
128 if (pix_fmt_info[pix_fmt].is_yuv) {
129 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
130 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
137 const char *avcodec_get_pix_fmt_name(int pix_fmt)
139 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
142 return pix_fmt_info[pix_fmt].name;
145 /* Picture field are filled with 'ptr' addresses. Also return size */
146 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
147 int pix_fmt, int width, int height)
149 int size, w2, h2, size2;
152 pinfo = &pix_fmt_info[pix_fmt];
153 size = width * height;
155 case PIX_FMT_YUV420P:
156 case PIX_FMT_YUV422P:
157 case PIX_FMT_YUV444P:
158 case PIX_FMT_YUV410P:
159 case PIX_FMT_YUV411P:
160 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
161 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
163 picture->data[0] = ptr;
164 picture->data[1] = picture->data[0] + size;
165 picture->data[2] = picture->data[1] + size2;
166 picture->linesize[0] = width;
167 picture->linesize[1] = w2;
168 picture->linesize[2] = w2;
169 return size + 2 * size2;
172 picture->data[0] = ptr;
173 picture->data[1] = NULL;
174 picture->data[2] = NULL;
175 picture->linesize[0] = width * 3;
178 picture->data[0] = ptr;
179 picture->data[1] = NULL;
180 picture->data[2] = NULL;
181 picture->linesize[0] = width * 4;
186 picture->data[0] = ptr;
187 picture->data[1] = NULL;
188 picture->data[2] = NULL;
189 picture->linesize[0] = width * 2;
192 picture->data[0] = ptr;
193 picture->data[1] = NULL;
194 picture->data[2] = NULL;
195 picture->linesize[0] = width;
197 case PIX_FMT_MONOWHITE:
198 case PIX_FMT_MONOBLACK:
199 picture->data[0] = ptr;
200 picture->data[1] = NULL;
201 picture->data[2] = NULL;
202 picture->linesize[0] = (width + 7) >> 3;
203 return picture->linesize[0] * height;
205 size2 = (size + 3) & ~3;
206 picture->data[0] = ptr;
207 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
208 picture->data[2] = NULL;
209 picture->linesize[0] = width;
210 picture->linesize[1] = 4;
211 return size2 + 256 * 4;
213 picture->data[0] = NULL;
214 picture->data[1] = NULL;
215 picture->data[2] = NULL;
216 picture->data[3] = NULL;
221 int avpicture_get_size(int pix_fmt, int width, int height)
223 AVPicture dummy_pict;
224 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
228 /* XXX: totally non optimized */
230 static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
231 int width, int height)
233 uint8_t *lum, *cb, *cr;
242 for(y=0;y<height;y+=2) {
243 for(x=0;x<width;x+=2) {
253 for(x=0;x<width;x+=2) {
263 #define ONE_HALF (1 << (SCALEBITS - 1))
264 #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
266 /* XXX: use generic filter ? */
268 static void shrink2(uint8_t *dst, int dst_wrap,
269 uint8_t *src, int src_wrap,
270 int width, int height)
273 uint8_t *s1, *s2, *d;
275 for(;height > 0; height--) {
279 for(w = width;w >= 4; w-=4) {
280 d[0] = (s1[0] + s2[0]) >> 1;
281 d[1] = (s1[1] + s2[1]) >> 1;
282 d[2] = (s1[2] + s2[2]) >> 1;
283 d[3] = (s1[3] + s2[3]) >> 1;
289 d[0] = (s1[0] + s2[0]) >> 1;
300 static void shrink22(uint8_t *dst, int dst_wrap,
301 uint8_t *src, int src_wrap,
302 int width, int height)
305 uint8_t *s1, *s2, *d;
307 for(;height > 0; height--) {
311 for(w = width;w >= 4; w-=4) {
312 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
313 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1;
314 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1;
315 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1;
321 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
332 static void grow22(uint8_t *dst, int dst_wrap,
333 uint8_t *src, int src_wrap,
334 int width, int height)
339 for(;height > 0; height--) {
342 for(w = width;w >= 4; w-=4) {
360 static void conv411(uint8_t *dst, int dst_wrap,
361 uint8_t *src, int src_wrap,
362 int width, int height)
365 uint8_t *s1, *s2, *d;
369 for(;height > 0; height--) {
373 for(w = width;w > 0; w--) {
374 c = (s1[0] + s2[0]) >> 1;
386 static void img_copy(uint8_t *dst, int dst_wrap,
387 uint8_t *src, int src_wrap,
388 int width, int height)
390 for(;height > 0; height--) {
391 memcpy(dst, src, width);
397 #define SCALE_BITS 10
399 #define C_Y (76309 >> (16 - SCALE_BITS))
400 #define C_RV (117504 >> (16 - SCALE_BITS))
401 #define C_BU (138453 >> (16 - SCALE_BITS))
402 #define C_GU (13954 >> (16 - SCALE_BITS))
403 #define C_GV (34903 >> (16 - SCALE_BITS))
405 #define YUV_TO_RGB2(r, g, b, y1)\
407 y = (y1 - 16) * C_Y;\
408 r = cm[(y + r_add) >> SCALE_BITS];\
409 g = cm[(y + g_add) >> SCALE_BITS];\
410 b = cm[(y + b_add) >> SCALE_BITS];\
413 /* XXX: no chroma interpolating is done */
414 #define RGB_FUNCTIONS(rgb_name) \
416 static void yuv420p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \
417 int width, int height) \
419 uint8_t *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2; \
420 int w, y, cb, cr, r_add, g_add, b_add, width2; \
421 uint8_t *cm = cropTbl + MAX_NEG_CROP; \
422 unsigned int r, g, b; \
425 y1_ptr = src->data[0]; \
426 cb_ptr = src->data[1]; \
427 cr_ptr = src->data[2]; \
428 width2 = (width + 1) >> 1; \
429 for(;height >= 2; height -= 2) { \
431 d2 = d + dst->linesize[0]; \
432 y2_ptr = y1_ptr + src->linesize[0]; \
433 for(w = width; w >= 2; w -= 2) { \
434 cb = cb_ptr[0] - 128; \
435 cr = cr_ptr[0] - 128; \
436 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
437 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
438 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
440 /* output 4 pixels */ \
441 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
442 RGB_OUT(d1, r, g, b); \
444 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
445 RGB_OUT(d1 + BPP, r, g, b); \
447 YUV_TO_RGB2(r, g, b, y2_ptr[0]); \
448 RGB_OUT(d2, r, g, b); \
450 YUV_TO_RGB2(r, g, b, y2_ptr[1]); \
451 RGB_OUT(d2 + BPP, r, g, b); \
461 /* handle odd width */ \
463 cb = cb_ptr[0] - 128; \
464 cr = cr_ptr[0] - 128; \
465 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
466 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
467 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
469 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
470 RGB_OUT(d1, r, g, b); \
472 YUV_TO_RGB2(r, g, b, y2_ptr[0]); \
473 RGB_OUT(d2, r, g, b); \
481 d += 2 * dst->linesize[0]; \
482 y1_ptr += 2 * src->linesize[0] - width; \
483 cb_ptr += src->linesize[1] - width2; \
484 cr_ptr += src->linesize[2] - width2; \
486 /* handle odd height */ \
489 for(w = width; w >= 2; w -= 2) { \
490 cb = cb_ptr[0] - 128; \
491 cr = cr_ptr[0] - 128; \
492 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
493 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
494 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
496 /* output 2 pixels */ \
497 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
498 RGB_OUT(d1, r, g, b); \
500 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
501 RGB_OUT(d1 + BPP, r, g, b); \
511 cb = cb_ptr[0] - 128; \
512 cr = cr_ptr[0] - 128; \
513 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
514 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
515 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
517 /* output 2 pixels */ \
518 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
519 RGB_OUT(d1, r, g, b); \
529 /* XXX: no chroma interpolating is done */ \
530 static void yuv422p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \
531 int width, int height) \
533 uint8_t *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1; \
534 int w, y, cb, cr, r_add, g_add, b_add, width2; \
535 uint8_t *cm = cropTbl + MAX_NEG_CROP; \
536 unsigned int r, g, b; \
539 y1_ptr = src->data[0]; \
540 cb_ptr = src->data[1]; \
541 cr_ptr = src->data[2]; \
542 width2 = (width + 1) >> 1; \
543 for(;height > 0; height --) { \
545 for(w = width; w >= 2; w -= 2) { \
546 cb = cb_ptr[0] - 128; \
547 cr = cr_ptr[0] - 128; \
548 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
549 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
550 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
552 /* output 2 pixels */ \
553 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
554 RGB_OUT(d1, r, g, b); \
556 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
557 RGB_OUT(d1 + BPP, r, g, b); \
567 cb = cb_ptr[0] - 128; \
568 cr = cr_ptr[0] - 128; \
569 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
570 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
571 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
573 /* output 2 pixels */ \
574 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
575 RGB_OUT(d1, r, g, b); \
582 d += dst->linesize[0]; \
583 y1_ptr += src->linesize[0] - width; \
584 cb_ptr += src->linesize[1] - width2; \
585 cr_ptr += src->linesize[2] - width2; \
589 static void rgb_name ## _to_yuv420p(AVPicture *dst, AVPicture *src, \
590 int width, int height) \
592 int wrap, wrap3, x, y; \
593 int r, g, b, r1, g1, b1; \
594 uint8_t *lum, *cb, *cr; \
597 lum = dst->data[0]; \
601 wrap = dst->linesize[0]; \
602 wrap3 = src->linesize[0]; \
604 for(y=0;y<height;y+=2) { \
605 for(x=0;x<width;x+=2) { \
606 RGB_IN(r, g, b, p); \
610 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
611 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
612 RGB_IN(r, g, b, p + BPP); \
616 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \
617 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
621 RGB_IN(r, g, b, p); \
625 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
626 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
628 RGB_IN(r, g, b, p + BPP); \
632 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \
633 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
635 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
636 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> \
637 (SCALEBITS + 2)) + 128; \
638 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
639 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> \
640 (SCALEBITS + 2)) + 128; \
644 p += -wrap3 + 2 * BPP; \
647 p += wrap3 + (wrap3 - width * BPP); \
648 lum += wrap + (wrap - width); \
649 cb += dst->linesize[1] - width / 2; \
650 cr += dst->linesize[2] - width / 2; \
654 static void rgb_name ## _to_gray(AVPicture *dst, AVPicture *src, \
655 int width, int height) \
657 const unsigned char *p; \
659 int r, g, b, dst_wrap, src_wrap; \
663 src_wrap = src->linesize[0] - BPP * width; \
666 dst_wrap = dst->linesize[0] - width; \
668 for(y=0;y<height;y++) { \
669 for(x=0;x<width;x++) { \
670 RGB_IN(r, g, b, p); \
671 q[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
672 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
681 static void gray_to_ ## rgb_name(AVPicture *dst, AVPicture *src, \
682 int width, int height) \
684 const unsigned char *p; \
686 int r, dst_wrap, src_wrap; \
690 src_wrap = src->linesize[0] - width; \
693 dst_wrap = dst->linesize[0] - BPP * width; \
695 for(y=0;y<height;y++) { \
696 for(x=0;x<width;x++) { \
698 RGB_OUT(q, r, r, r); \
707 static void pal8_to_ ## rgb_name(AVPicture *dst, AVPicture *src, \
708 int width, int height) \
710 const unsigned char *p; \
712 int r, g, b, dst_wrap, src_wrap; \
715 const uint32_t *palette;\
718 src_wrap = src->linesize[0] - width; \
719 palette = (uint32_t *)src->data[1];\
722 dst_wrap = dst->linesize[0] - BPP * width; \
724 for(y=0;y<height;y++) { \
725 for(x=0;x<width;x++) { \
727 r = (v >> 16) & 0xff;\
728 g = (v >> 8) & 0xff;\
730 RGB_OUT(q, r, g, b); \
739 /* copy bit n to bits 0 ... n - 1 */
740 static inline unsigned int bitcopy_n(unsigned int a, int n)
744 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
747 /* rgb555 handling */
749 #define RGB_IN(r, g, b, s)\
751 unsigned int v = ((const uint16_t *)(s))[0];\
752 r = bitcopy_n(v >> (10 - 3), 3);\
753 g = bitcopy_n(v >> (5 - 3), 3);\
754 b = bitcopy_n(v << 3, 3);\
757 #define RGB_OUT(d, r, g, b)\
759 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;\
764 RGB_FUNCTIONS(rgb555)
770 /* rgb565 handling */
772 #define RGB_IN(r, g, b, s)\
774 unsigned int v = ((const uint16_t *)(s))[0];\
775 r = bitcopy_n(v >> (11 - 3), 3);\
776 g = bitcopy_n(v >> (5 - 2), 2);\
777 b = bitcopy_n(v << 3, 3);\
780 #define RGB_OUT(d, r, g, b)\
782 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
787 RGB_FUNCTIONS(rgb565)
795 #define RGB_IN(r, g, b, s)\
802 #define RGB_OUT(d, r, g, b)\
819 #define RGB_IN(r, g, b, s)\
826 #define RGB_OUT(d, r, g, b)\
841 /* rgba32 handling */
843 #define RGB_IN(r, g, b, s)\
845 unsigned int v = ((const uint32_t *)(s))[0];\
846 r = (v >> 16) & 0xff;\
847 g = (v >> 8) & 0xff;\
851 #define RGB_OUT(d, r, g, b)\
853 ((uint32_t *)(d))[0] = (0xff << 24) | (r << 16) | (g << 8) | b;\
858 RGB_FUNCTIONS(rgba32)
865 static void rgb24_to_rgb565(AVPicture *dst, AVPicture *src,
866 int width, int height)
868 const unsigned char *p;
870 int r, g, b, dst_wrap, src_wrap;
874 src_wrap = src->linesize[0] - 3 * width;
877 dst_wrap = dst->linesize[0] - 2 * width;
879 for(y=0;y<height;y++) {
880 for(x=0;x<width;x++) {
885 ((unsigned short *)q)[0] =
886 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
895 /* NOTE: we also add a dummy alpha bit */
896 static void rgb24_to_rgb555(AVPicture *dst, AVPicture *src,
897 int width, int height)
899 const unsigned char *p;
901 int r, g, b, dst_wrap, src_wrap;
905 src_wrap = src->linesize[0] - 3 * width;
908 dst_wrap = dst->linesize[0] - 2 * width;
910 for(y=0;y<height;y++) {
911 for(x=0;x<width;x++) {
916 ((unsigned short *)q)[0] =
917 ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;
926 static void mono_to_gray(AVPicture *dst, AVPicture *src,
927 int width, int height, int xor_mask)
929 const unsigned char *p;
931 int v, dst_wrap, src_wrap;
935 src_wrap = src->linesize[0] - ((width + 7) >> 3);
938 dst_wrap = dst->linesize[0] - width;
939 for(y=0;y<height;y++) {
944 q[1] = -((v >> 6) & 1);
945 q[2] = -((v >> 5) & 1);
946 q[3] = -((v >> 4) & 1);
947 q[4] = -((v >> 3) & 1);
948 q[5] = -((v >> 2) & 1);
949 q[6] = -((v >> 1) & 1);
950 q[7] = -((v >> 0) & 1);
957 q[0] = -((v >> 7) & 1);
967 static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
968 int width, int height)
970 mono_to_gray(dst, src, width, height, 0xff);
973 static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
974 int width, int height)
976 mono_to_gray(dst, src, width, height, 0x00);
979 static void gray_to_mono(AVPicture *dst, AVPicture *src,
980 int width, int height, int xor_mask)
985 int j, b, v, n1, src_wrap, dst_wrap, y;
988 src_wrap = src->linesize[0] - width;
991 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
992 printf("%d %d\n", width, height);
994 for(y=0;y<height;y++) {
1001 v = (v << 1) | (b >> 7);
1003 d[0] = v ^ xor_mask;
1013 v = (v << 1) | (b >> 7);
1016 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1024 static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
1025 int width, int height)
1027 gray_to_mono(dst, src, width, height, 0xff);
1030 static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
1031 int width, int height)
1033 gray_to_mono(dst, src, width, height, 0x00);
1036 /* this is maybe slow, but allows for extensions */
1037 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1039 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1042 /* XXX: put jpeg quantize code instead */
1043 static void rgb24_to_pal8(AVPicture *dst, AVPicture *src,
1044 int width, int height)
1046 const unsigned char *p;
1048 int r, g, b, dst_wrap, src_wrap;
1050 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1054 src_wrap = src->linesize[0] - 3 * width;
1057 dst_wrap = dst->linesize[0] - width;
1059 for(y=0;y<height;y++) {
1060 for(x=0;x<width;x++) {
1065 q[0] = gif_clut_index(r, g, b);
1074 pal = (uint32_t *)dst->data[1];
1076 for(r = 0; r < 6; r++) {
1077 for(g = 0; g < 6; g++) {
1078 for(b = 0; b < 6; b++) {
1079 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1080 (pal_value[g] << 8) | pal_value[b];
1088 typedef struct ConvertEntry {
1089 void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
1092 /* add each new convertion function in this table */
1094 - all non YUV modes must convert at least to and from PIX_FMT_RGB24
1096 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1097 [PIX_FMT_YUV420P] = {
1098 [PIX_FMT_RGB555] = {
1099 .convert = yuv420p_to_rgb555
1101 [PIX_FMT_RGB565] = {
1102 .convert = yuv420p_to_rgb565
1105 .convert = yuv420p_to_bgr24
1108 .convert = yuv420p_to_rgb24
1110 [PIX_FMT_RGBA32] = {
1111 .convert = yuv420p_to_rgba32
1114 [PIX_FMT_YUV422P] = {
1115 [PIX_FMT_RGB555] = {
1116 .convert = yuv422p_to_rgb555
1118 [PIX_FMT_RGB565] = {
1119 .convert = yuv422p_to_rgb565
1122 .convert = yuv422p_to_bgr24
1125 .convert = yuv422p_to_rgb24
1127 [PIX_FMT_RGBA32] = {
1128 .convert = yuv422p_to_rgba32
1131 [PIX_FMT_YUV422] = {
1132 [PIX_FMT_YUV420P] = {
1133 .convert = yuv422_to_yuv420p,
1138 [PIX_FMT_YUV420P] = {
1139 .convert = rgb24_to_yuv420p
1141 [PIX_FMT_RGB565] = {
1142 .convert = rgb24_to_rgb565
1144 [PIX_FMT_RGB555] = {
1145 .convert = rgb24_to_rgb555
1148 .convert = rgb24_to_gray
1151 .convert = rgb24_to_pal8
1154 [PIX_FMT_RGBA32] = {
1155 [PIX_FMT_YUV420P] = {
1156 .convert = rgba32_to_yuv420p
1159 .convert = rgba32_to_gray
1163 [PIX_FMT_YUV420P] = {
1164 .convert = bgr24_to_yuv420p
1167 .convert = bgr24_to_gray
1170 [PIX_FMT_RGB555] = {
1171 [PIX_FMT_YUV420P] = {
1172 .convert = rgb555_to_yuv420p
1175 .convert = rgb555_to_gray
1178 [PIX_FMT_RGB565] = {
1179 [PIX_FMT_YUV420P] = {
1180 .convert = rgb565_to_yuv420p
1183 .convert = rgb565_to_gray
1187 [PIX_FMT_RGB555] = {
1188 .convert = gray_to_rgb555
1190 [PIX_FMT_RGB565] = {
1191 .convert = gray_to_rgb565
1194 .convert = gray_to_rgb24
1197 .convert = gray_to_bgr24
1199 [PIX_FMT_RGBA32] = {
1200 .convert = gray_to_rgba32
1202 [PIX_FMT_MONOWHITE] = {
1203 .convert = gray_to_monowhite
1205 [PIX_FMT_MONOBLACK] = {
1206 .convert = gray_to_monoblack
1209 [PIX_FMT_MONOWHITE] = {
1211 .convert = monowhite_to_gray
1214 [PIX_FMT_MONOBLACK] = {
1216 .convert = monoblack_to_gray
1220 [PIX_FMT_RGB555] = {
1221 .convert = pal8_to_rgb555
1223 [PIX_FMT_RGB565] = {
1224 .convert = pal8_to_rgb565
1227 .convert = pal8_to_bgr24
1230 .convert = pal8_to_rgb24
1232 [PIX_FMT_RGBA32] = {
1233 .convert = pal8_to_rgba32
1238 static int avpicture_alloc(AVPicture *picture,
1239 int pix_fmt, int width, int height)
1244 size = avpicture_get_size(pix_fmt, width, height);
1247 ptr = av_malloc(size);
1250 avpicture_fill(picture, ptr, pix_fmt, width, height);
1253 memset(picture, 0, sizeof(AVPicture));
1257 static void avpicture_free(AVPicture *picture)
1259 av_free(picture->data[0]);
1262 /* XXX: always use linesize. Return -1 if not supported */
1263 int img_convert(AVPicture *dst, int dst_pix_fmt,
1264 AVPicture *src, int src_pix_fmt,
1265 int src_width, int src_height)
1267 int i, ret, dst_width, dst_height, int_pix_fmt;
1268 PixFmtInfo *src_pix, *dst_pix;
1270 AVPicture tmp1, *tmp = &tmp1;
1272 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1273 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1275 if (src_width <= 0 || src_height <= 0)
1278 dst_width = src_width;
1279 dst_height = src_height;
1281 dst_pix = &pix_fmt_info[dst_pix_fmt];
1282 src_pix = &pix_fmt_info[src_pix_fmt];
1283 if (src_pix_fmt == dst_pix_fmt) {
1284 /* XXX: incorrect */
1285 /* same format: just copy */
1286 for(i = 0; i < dst_pix->nb_components; i++) {
1290 if (dst_pix->is_yuv && (i == 1 || i == 2)) {
1291 w >>= dst_pix->x_chroma_shift;
1292 h >>= dst_pix->y_chroma_shift;
1294 img_copy(dst->data[i], dst->linesize[i],
1295 src->data[i], src->linesize[i],
1301 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1303 /* specific convertion routine */
1304 ce->convert(dst, src, dst_width, dst_height);
1309 if (dst_pix->is_yuv && src_pix_fmt == PIX_FMT_GRAY8) {
1313 img_copy(dst->data[0], dst->linesize[0],
1314 src->data[0], src->linesize[0],
1315 dst_width, dst_height);
1316 /* fill U and V with 128 */
1319 w >>= dst_pix->x_chroma_shift;
1320 h >>= dst_pix->y_chroma_shift;
1321 for(i = 1; i <= 2; i++) {
1323 for(y = 0; y< h; y++) {
1325 d += dst->linesize[i];
1332 if (src_pix->is_yuv && dst_pix_fmt == PIX_FMT_GRAY8) {
1333 img_copy(dst->data[0], dst->linesize[0],
1334 src->data[0], src->linesize[0],
1335 dst_width, dst_height);
1340 if (dst_pix->is_yuv && src_pix->is_yuv) {
1341 int x_shift, y_shift, w, h;
1342 void (*resize_func)(uint8_t *dst, int dst_wrap,
1343 uint8_t *src, int src_wrap,
1344 int width, int height);
1346 /* compute chroma size of the smallest dimensions */
1349 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1350 w >>= dst_pix->x_chroma_shift;
1352 w >>= src_pix->x_chroma_shift;
1353 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1354 h >>= dst_pix->y_chroma_shift;
1356 h >>= src_pix->y_chroma_shift;
1358 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1359 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
1360 if (x_shift == 0 && y_shift == 0) {
1361 resize_func = img_copy; /* should never happen */
1362 } else if (x_shift == 0 && y_shift == 1) {
1363 resize_func = shrink2;
1364 } else if (x_shift == 1 && y_shift == 1) {
1365 resize_func = shrink22;
1366 } else if (x_shift == -1 && y_shift == -1) {
1367 resize_func = grow22;
1368 } else if (x_shift == -1 && y_shift == 1) {
1369 resize_func = conv411;
1371 /* currently not handled */
1375 img_copy(dst->data[0], dst->linesize[0],
1376 src->data[0], src->linesize[0],
1377 dst_width, dst_height);
1379 for(i = 1;i <= 2; i++)
1380 resize_func(dst->data[i], dst->linesize[i],
1381 src->data[i], src->linesize[i],
1382 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
1386 /* try to use an intermediate format */
1387 if (src_pix_fmt == PIX_FMT_MONOWHITE ||
1388 src_pix_fmt == PIX_FMT_MONOBLACK ||
1389 dst_pix_fmt == PIX_FMT_MONOWHITE ||
1390 dst_pix_fmt == PIX_FMT_MONOBLACK) {
1391 int_pix_fmt = PIX_FMT_GRAY8;
1393 int_pix_fmt = PIX_FMT_RGB24;
1395 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
1398 if (img_convert(tmp, int_pix_fmt,
1399 src, src_pix_fmt, src_width, src_height) < 0)
1401 if (img_convert(dst, dst_pix_fmt,
1402 tmp, int_pix_fmt, dst_width, dst_height) < 0)
1406 avpicture_free(tmp);
1412 #define DEINT_INPLACE_LINE_LUM \
1413 movd_m2r(lum_m4[0],mm0);\
1414 movd_m2r(lum_m3[0],mm1);\
1415 movd_m2r(lum_m2[0],mm2);\
1416 movd_m2r(lum_m1[0],mm3);\
1417 movd_m2r(lum[0],mm4);\
1418 punpcklbw_r2r(mm7,mm0);\
1419 movd_r2m(mm2,lum_m4[0]);\
1420 punpcklbw_r2r(mm7,mm1);\
1421 punpcklbw_r2r(mm7,mm2);\
1422 punpcklbw_r2r(mm7,mm3);\
1423 punpcklbw_r2r(mm7,mm4);\
1424 paddw_r2r(mm3,mm1);\
1426 paddw_r2r(mm4,mm0);\
1428 paddw_r2r(mm6,mm2);\
1429 paddw_r2r(mm2,mm1);\
1430 psubusw_r2r(mm0,mm1);\
1432 packuswb_r2r(mm7,mm1);\
1433 movd_r2m(mm1,lum_m2[0]);
1435 #define DEINT_LINE_LUM \
1436 movd_m2r(lum_m4[0],mm0);\
1437 movd_m2r(lum_m3[0],mm1);\
1438 movd_m2r(lum_m2[0],mm2);\
1439 movd_m2r(lum_m1[0],mm3);\
1440 movd_m2r(lum[0],mm4);\
1441 punpcklbw_r2r(mm7,mm0);\
1442 punpcklbw_r2r(mm7,mm1);\
1443 punpcklbw_r2r(mm7,mm2);\
1444 punpcklbw_r2r(mm7,mm3);\
1445 punpcklbw_r2r(mm7,mm4);\
1446 paddw_r2r(mm3,mm1);\
1448 paddw_r2r(mm4,mm0);\
1450 paddw_r2r(mm6,mm2);\
1451 paddw_r2r(mm2,mm1);\
1452 psubusw_r2r(mm0,mm1);\
1454 packuswb_r2r(mm7,mm1);\
1455 movd_r2m(mm1,dst[0]);
1458 /* filter parameters: [-1 4 2 4 -1] // 8 */
1459 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,
1463 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1466 for(;size > 0;size--) {
1468 sum += lum_m3[0] << 2;
1469 sum += lum_m2[0] << 1;
1470 sum += lum_m1[0] << 2;
1472 dst[0] = cm[(sum + 4) >> 3];
1489 movq_m2r(rounder,mm6);
1491 for (;size > 3; size-=4) {
1502 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1506 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1509 for(;size > 0;size--) {
1511 sum += lum_m3[0] << 2;
1512 sum += lum_m2[0] << 1;
1513 lum_m4[0]=lum_m2[0];
1514 sum += lum_m1[0] << 2;
1516 lum_m2[0] = cm[(sum + 4) >> 3];
1532 movq_m2r(rounder,mm6);
1534 for (;size > 3; size-=4) {
1535 DEINT_INPLACE_LINE_LUM
1545 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1546 top field is copied as is, but the bottom field is deinterlaced
1547 against the top field. */
1548 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1549 uint8_t *src1, int src_wrap,
1550 int width, int height)
1552 uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1557 src_0=&src_m1[src_wrap];
1558 src_p1=&src_0[src_wrap];
1559 src_p2=&src_p1[src_wrap];
1560 for(y=0;y<(height-2);y+=2) {
1561 memcpy(dst,src_m1,width);
1563 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1567 src_p1 += 2*src_wrap;
1568 src_p2 += 2*src_wrap;
1571 memcpy(dst,src_m1,width);
1574 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1577 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1578 int width, int height)
1580 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1583 buf = (uint8_t*)av_malloc(width);
1586 memcpy(buf,src_m1,width);
1587 src_0=&src_m1[src_wrap];
1588 src_p1=&src_0[src_wrap];
1589 src_p2=&src_p1[src_wrap];
1590 for(y=0;y<(height-2);y+=2) {
1591 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1594 src_p1 += 2*src_wrap;
1595 src_p2 += 2*src_wrap;
1598 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1603 /* deinterlace - if not supported return -1 */
1604 int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
1605 int pix_fmt, int width, int height)
1609 if (pix_fmt != PIX_FMT_YUV420P &&
1610 pix_fmt != PIX_FMT_YUV422P &&
1611 pix_fmt != PIX_FMT_YUV444P)
1613 if ((width & 3) != 0 || (height & 3) != 0)
1619 case PIX_FMT_YUV420P:
1623 case PIX_FMT_YUV422P:
1631 deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
1634 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1635 src->data[i], src->linesize[i],