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_t nb_components; /* number of components in AVPicture array */
33 uint8_t is_yuv : 1; /* true if YUV instead of RGB color space */
34 uint8_t is_packed : 1; /* true if multiple components in same word */
35 uint8_t is_paletted : 1; /* true if paletted */
36 uint8_t is_alpha : 1; /* true if alpha can be specified */
37 uint8_t is_gray : 1; /* true if gray or monochrome format */
38 uint8_t x_chroma_shift; /* X chroma subsampling factor is 2 ^ shift */
39 uint8_t 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,
112 /* paletted formats */
115 .nb_components = 1, .is_packed = 1, .is_paletted = 1,
119 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
121 if (pix_fmt_info[pix_fmt].is_yuv) {
122 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
123 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
130 const char *avcodec_get_pix_fmt_name(int pix_fmt)
132 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
135 return pix_fmt_info[pix_fmt].name;
138 /* Picture field are filled with 'ptr' addresses. Also return size */
139 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
140 int pix_fmt, int width, int height)
142 int size, w2, h2, size2;
145 pinfo = &pix_fmt_info[pix_fmt];
146 size = width * height;
148 case PIX_FMT_YUV420P:
149 case PIX_FMT_YUV422P:
150 case PIX_FMT_YUV444P:
151 case PIX_FMT_YUV410P:
152 case PIX_FMT_YUV411P:
153 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
154 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
156 picture->data[0] = ptr;
157 picture->data[1] = picture->data[0] + size;
158 picture->data[2] = picture->data[1] + size2;
159 picture->linesize[0] = width;
160 picture->linesize[1] = w2;
161 picture->linesize[2] = w2;
162 return size + 2 * size2;
165 picture->data[0] = ptr;
166 picture->data[1] = NULL;
167 picture->data[2] = NULL;
168 picture->linesize[0] = width * 3;
171 picture->data[0] = ptr;
172 picture->data[1] = NULL;
173 picture->data[2] = NULL;
174 picture->linesize[0] = width * 4;
179 picture->data[0] = ptr;
180 picture->data[1] = NULL;
181 picture->data[2] = NULL;
182 picture->linesize[0] = width * 2;
185 picture->data[0] = ptr;
186 picture->data[1] = NULL;
187 picture->data[2] = NULL;
188 picture->linesize[0] = width;
190 case PIX_FMT_MONOWHITE:
191 case PIX_FMT_MONOBLACK:
192 picture->data[0] = ptr;
193 picture->data[1] = NULL;
194 picture->data[2] = NULL;
195 picture->linesize[0] = (width + 7) >> 3;
196 return picture->linesize[0] * height;
198 size2 = (size + 3) & ~3;
199 picture->data[0] = ptr;
200 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
201 picture->data[2] = NULL;
202 picture->linesize[0] = width;
203 picture->linesize[1] = 4;
204 return size2 + 256 * 4;
206 picture->data[0] = NULL;
207 picture->data[1] = NULL;
208 picture->data[2] = NULL;
209 picture->data[3] = NULL;
214 int avpicture_get_size(int pix_fmt, int width, int height)
216 AVPicture dummy_pict;
217 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
221 /* XXX: totally non optimized */
223 static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
224 int width, int height)
226 uint8_t *lum, *cb, *cr;
235 for(y=0;y<height;y+=2) {
236 for(x=0;x<width;x+=2) {
246 for(x=0;x<width;x+=2) {
256 #define ONE_HALF (1 << (SCALEBITS - 1))
257 #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
259 /* XXX: use generic filter ? */
261 static void shrink2(uint8_t *dst, int dst_wrap,
262 uint8_t *src, int src_wrap,
263 int width, int height)
266 uint8_t *s1, *s2, *d;
268 for(;height > 0; height--) {
272 for(w = width;w >= 4; w-=4) {
273 d[0] = (s1[0] + s2[0]) >> 1;
274 d[1] = (s1[1] + s2[1]) >> 1;
275 d[2] = (s1[2] + s2[2]) >> 1;
276 d[3] = (s1[3] + s2[3]) >> 1;
282 d[0] = (s1[0] + s2[0]) >> 1;
293 static void shrink22(uint8_t *dst, int dst_wrap,
294 uint8_t *src, int src_wrap,
295 int width, int height)
298 uint8_t *s1, *s2, *d;
300 for(;height > 0; height--) {
304 for(w = width;w >= 4; w-=4) {
305 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
306 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1;
307 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1;
308 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1;
314 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
325 static void grow22(uint8_t *dst, int dst_wrap,
326 uint8_t *src, int src_wrap,
327 int width, int height)
332 for(;height > 0; height--) {
335 for(w = width;w >= 4; w-=4) {
353 static void conv411(uint8_t *dst, int dst_wrap,
354 uint8_t *src, int src_wrap,
355 int width, int height)
358 uint8_t *s1, *s2, *d;
360 for(;height > 0; height--) {
364 for(w = width;w > 0; w--) {
365 c = (s1[0] + s2[0]) >> 1;
377 static void img_copy(uint8_t *dst, int dst_wrap,
378 uint8_t *src, int src_wrap,
379 int width, int height)
381 for(;height > 0; height--) {
382 memcpy(dst, src, width);
388 #define SCALE_BITS 10
390 #define C_Y (76309 >> (16 - SCALE_BITS))
391 #define C_RV (117504 >> (16 - SCALE_BITS))
392 #define C_BU (138453 >> (16 - SCALE_BITS))
393 #define C_GU (13954 >> (16 - SCALE_BITS))
394 #define C_GV (34903 >> (16 - SCALE_BITS))
396 #define YUV_TO_RGB2(r, g, b, y1)\
398 y = (y1 - 16) * C_Y;\
399 r = cm[(y + r_add) >> SCALE_BITS];\
400 g = cm[(y + g_add) >> SCALE_BITS];\
401 b = cm[(y + b_add) >> SCALE_BITS];\
404 /* XXX: no chroma interpolating is done */
405 #define RGB_FUNCTIONS(rgb_name) \
407 static void yuv420p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \
408 int width, int height) \
410 uint8_t *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2; \
411 int w, y, cb, cr, r_add, g_add, b_add, width2; \
412 uint8_t *cm = cropTbl + MAX_NEG_CROP; \
413 unsigned int r, g, b; \
416 y1_ptr = src->data[0]; \
417 cb_ptr = src->data[1]; \
418 cr_ptr = src->data[2]; \
419 width2 = (width + 1) >> 1; \
420 for(;height >= 2; height -= 2) { \
422 d2 = d + dst->linesize[0]; \
423 y2_ptr = y1_ptr + src->linesize[0]; \
424 for(w = width; w >= 2; w -= 2) { \
425 cb = cb_ptr[0] - 128; \
426 cr = cr_ptr[0] - 128; \
427 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
428 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
429 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
431 /* output 4 pixels */ \
432 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
433 RGB_OUT(d1, r, g, b); \
435 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
436 RGB_OUT(d1 + BPP, r, g, b); \
438 YUV_TO_RGB2(r, g, b, y2_ptr[0]); \
439 RGB_OUT(d2, r, g, b); \
441 YUV_TO_RGB2(r, g, b, y2_ptr[1]); \
442 RGB_OUT(d2 + BPP, r, g, b); \
452 /* handle odd width */ \
454 cb = cb_ptr[0] - 128; \
455 cr = cr_ptr[0] - 128; \
456 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
457 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
458 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
460 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
461 RGB_OUT(d1, r, g, b); \
463 YUV_TO_RGB2(r, g, b, y2_ptr[0]); \
464 RGB_OUT(d2, r, g, b); \
472 d += 2 * dst->linesize[0]; \
473 y1_ptr += 2 * src->linesize[0] - width; \
474 cb_ptr += src->linesize[1] - width2; \
475 cr_ptr += src->linesize[2] - width2; \
477 /* handle odd height */ \
480 for(w = width; w >= 2; w -= 2) { \
481 cb = cb_ptr[0] - 128; \
482 cr = cr_ptr[0] - 128; \
483 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
484 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
485 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
487 /* output 2 pixels */ \
488 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
489 RGB_OUT(d1, r, g, b); \
491 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
492 RGB_OUT(d1 + BPP, r, g, b); \
502 cb = cb_ptr[0] - 128; \
503 cr = cr_ptr[0] - 128; \
504 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
505 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
506 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
508 /* output 2 pixels */ \
509 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
510 RGB_OUT(d1, r, g, b); \
520 /* XXX: no chroma interpolating is done */ \
521 static void yuv422p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \
522 int width, int height) \
524 uint8_t *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1; \
525 int w, y, cb, cr, r_add, g_add, b_add, width2; \
526 uint8_t *cm = cropTbl + MAX_NEG_CROP; \
527 unsigned int r, g, b; \
530 y1_ptr = src->data[0]; \
531 cb_ptr = src->data[1]; \
532 cr_ptr = src->data[2]; \
533 width2 = (width + 1) >> 1; \
534 for(;height > 0; height --) { \
536 for(w = width; w >= 2; w -= 2) { \
537 cb = cb_ptr[0] - 128; \
538 cr = cr_ptr[0] - 128; \
539 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
540 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
541 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
543 /* output 2 pixels */ \
544 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
545 RGB_OUT(d1, r, g, b); \
547 YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
548 RGB_OUT(d1 + BPP, r, g, b); \
558 cb = cb_ptr[0] - 128; \
559 cr = cr_ptr[0] - 128; \
560 r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
561 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
562 b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
564 /* output 2 pixels */ \
565 YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
566 RGB_OUT(d1, r, g, b); \
573 d += dst->linesize[0]; \
574 y1_ptr += src->linesize[0] - width; \
575 cb_ptr += src->linesize[1] - width2; \
576 cr_ptr += src->linesize[2] - width2; \
580 static void rgb_name ## _to_yuv420p(AVPicture *dst, AVPicture *src, \
581 int width, int height) \
583 int wrap, wrap3, x, y; \
584 int r, g, b, r1, g1, b1; \
585 uint8_t *lum, *cb, *cr; \
588 lum = dst->data[0]; \
592 wrap = dst->linesize[0]; \
593 wrap3 = src->linesize[0]; \
595 for(y=0;y<height;y+=2) { \
596 for(x=0;x<width;x+=2) { \
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; \
603 RGB_IN(r, g, b, p + BPP); \
607 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \
608 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
612 RGB_IN(r, g, b, p); \
616 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
617 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
619 RGB_IN(r, g, b, p + BPP); \
623 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \
624 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
626 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
627 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> \
628 (SCALEBITS + 2)) + 128; \
629 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
630 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> \
631 (SCALEBITS + 2)) + 128; \
635 p += -wrap3 + 2 * BPP; \
638 p += wrap3 + (wrap3 - width * BPP); \
639 lum += wrap + (wrap - width); \
640 cb += dst->linesize[1] - width / 2; \
641 cr += dst->linesize[2] - width / 2; \
645 static void rgb_name ## _to_gray(AVPicture *dst, AVPicture *src, \
646 int width, int height) \
648 const unsigned char *p; \
650 int r, g, b, dst_wrap, src_wrap; \
654 src_wrap = src->linesize[0] - BPP * width; \
657 dst_wrap = dst->linesize[0] - width; \
659 for(y=0;y<height;y++) { \
660 for(x=0;x<width;x++) { \
661 RGB_IN(r, g, b, p); \
662 q[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
663 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
672 static void gray_to_ ## rgb_name(AVPicture *dst, AVPicture *src, \
673 int width, int height) \
675 const unsigned char *p; \
677 int r, dst_wrap, src_wrap; \
681 src_wrap = src->linesize[0] - width; \
684 dst_wrap = dst->linesize[0] - BPP * width; \
686 for(y=0;y<height;y++) { \
687 for(x=0;x<width;x++) { \
689 RGB_OUT(q, r, r, r); \
698 static void pal8_to_ ## rgb_name(AVPicture *dst, AVPicture *src, \
699 int width, int height) \
701 const unsigned char *p; \
703 int r, g, b, dst_wrap, src_wrap; \
706 const uint32_t *palette;\
709 src_wrap = src->linesize[0] - width; \
710 palette = (uint32_t *)src->data[1];\
713 dst_wrap = dst->linesize[0] - BPP * width; \
715 for(y=0;y<height;y++) { \
716 for(x=0;x<width;x++) { \
718 r = (v >> 16) & 0xff;\
719 g = (v >> 8) & 0xff;\
721 RGB_OUT(q, r, g, b); \
730 /* copy bit n to bits 0 ... n - 1 */
731 static inline unsigned int bitcopy_n(unsigned int a, int n)
735 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
738 /* rgb555 handling */
740 #define RGB_IN(r, g, b, s)\
742 unsigned int v = ((const uint16_t *)(s))[0];\
743 r = bitcopy_n(v >> (10 - 3), 3);\
744 g = bitcopy_n(v >> (5 - 3), 3);\
745 b = bitcopy_n(v << 3, 3);\
748 #define RGB_OUT(d, r, g, b)\
750 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;\
755 RGB_FUNCTIONS(rgb555)
761 /* rgb565 handling */
763 #define RGB_IN(r, g, b, s)\
765 unsigned int v = ((const uint16_t *)(s))[0];\
766 r = bitcopy_n(v >> (11 - 3), 3);\
767 g = bitcopy_n(v >> (5 - 2), 2);\
768 b = bitcopy_n(v << 3, 3);\
771 #define RGB_OUT(d, r, g, b)\
773 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
778 RGB_FUNCTIONS(rgb565)
786 #define RGB_IN(r, g, b, s)\
793 #define RGB_OUT(d, r, g, b)\
810 #define RGB_IN(r, g, b, s)\
817 #define RGB_OUT(d, r, g, b)\
832 /* rgba32 handling */
834 #define RGB_IN(r, g, b, s)\
836 unsigned int v = ((const uint32_t *)(s))[0];\
837 r = (v >> 16) & 0xff;\
838 g = (v >> 8) & 0xff;\
842 #define RGB_OUT(d, r, g, b)\
844 ((uint32_t *)(d))[0] = (0xff << 24) | (r << 16) | (g << 8) | b;\
849 RGB_FUNCTIONS(rgba32)
856 static void rgb24_to_rgb565(AVPicture *dst, AVPicture *src,
857 int width, int height)
859 const unsigned char *p;
861 int r, g, b, dst_wrap, src_wrap;
865 src_wrap = src->linesize[0] - 3 * width;
868 dst_wrap = dst->linesize[0] - 2 * width;
870 for(y=0;y<height;y++) {
871 for(x=0;x<width;x++) {
876 ((unsigned short *)q)[0] =
877 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
886 /* NOTE: we also add a dummy alpha bit */
887 static void rgb24_to_rgb555(AVPicture *dst, AVPicture *src,
888 int width, int height)
890 const unsigned char *p;
892 int r, g, b, dst_wrap, src_wrap;
896 src_wrap = src->linesize[0] - 3 * width;
899 dst_wrap = dst->linesize[0] - 2 * width;
901 for(y=0;y<height;y++) {
902 for(x=0;x<width;x++) {
907 ((unsigned short *)q)[0] =
908 ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;
917 static void mono_to_gray(AVPicture *dst, AVPicture *src,
918 int width, int height, int xor_mask)
920 const unsigned char *p;
922 int v, dst_wrap, src_wrap;
926 src_wrap = src->linesize[0] - ((width + 7) >> 3);
929 dst_wrap = dst->linesize[0] - width;
930 for(y=0;y<height;y++) {
935 q[1] = -((v >> 6) & 1);
936 q[2] = -((v >> 5) & 1);
937 q[3] = -((v >> 4) & 1);
938 q[4] = -((v >> 3) & 1);
939 q[5] = -((v >> 2) & 1);
940 q[6] = -((v >> 1) & 1);
941 q[7] = -((v >> 0) & 1);
948 q[0] = -((v >> 7) & 1);
958 static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
959 int width, int height)
961 mono_to_gray(dst, src, width, height, 0xff);
964 static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
965 int width, int height)
967 mono_to_gray(dst, src, width, height, 0x00);
970 static void gray_to_mono(AVPicture *dst, AVPicture *src,
971 int width, int height, int xor_mask)
976 int j, b, v, n1, src_wrap, dst_wrap, y;
979 src_wrap = src->linesize[0] - width;
982 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
983 printf("%d %d\n", width, height);
985 for(y=0;y<height;y++) {
992 v = (v << 1) | (b >> 7);
1004 v = (v << 1) | (b >> 7);
1007 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1015 static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
1016 int width, int height)
1018 gray_to_mono(dst, src, width, height, 0xff);
1021 static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
1022 int width, int height)
1024 gray_to_mono(dst, src, width, height, 0x00);
1027 /* this is maybe slow, but allows for extensions */
1028 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1030 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1033 /* XXX: put jpeg quantize code instead */
1034 static void rgb24_to_pal8(AVPicture *dst, AVPicture *src,
1035 int width, int height)
1037 const unsigned char *p;
1039 int r, g, b, dst_wrap, src_wrap;
1041 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1045 src_wrap = src->linesize[0] - 3 * width;
1048 dst_wrap = dst->linesize[0] - width;
1050 for(y=0;y<height;y++) {
1051 for(x=0;x<width;x++) {
1056 q[0] = gif_clut_index(r, g, b);
1065 pal = (uint32_t *)dst->data[1];
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];
1079 typedef struct ConvertEntry {
1080 void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
1083 /* add each new convertion function in this table */
1085 - all non YUV modes must convert at least to and from PIX_FMT_RGB24
1087 static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1088 [PIX_FMT_YUV420P] = {
1089 [PIX_FMT_RGB555] = {
1090 .convert = yuv420p_to_rgb555
1092 [PIX_FMT_RGB565] = {
1093 .convert = yuv420p_to_rgb565
1096 .convert = yuv420p_to_bgr24
1099 .convert = yuv420p_to_rgb24
1101 [PIX_FMT_RGBA32] = {
1102 .convert = yuv420p_to_rgba32
1105 [PIX_FMT_YUV422P] = {
1106 [PIX_FMT_RGB555] = {
1107 .convert = yuv422p_to_rgb555
1109 [PIX_FMT_RGB565] = {
1110 .convert = yuv422p_to_rgb565
1113 .convert = yuv422p_to_bgr24
1116 .convert = yuv422p_to_rgb24
1118 [PIX_FMT_RGBA32] = {
1119 .convert = yuv422p_to_rgba32
1122 [PIX_FMT_YUV422] = {
1123 [PIX_FMT_YUV420P] = {
1124 .convert = yuv422_to_yuv420p,
1129 [PIX_FMT_YUV420P] = {
1130 .convert = rgb24_to_yuv420p
1132 [PIX_FMT_RGB565] = {
1133 .convert = rgb24_to_rgb565
1135 [PIX_FMT_RGB555] = {
1136 .convert = rgb24_to_rgb555
1139 .convert = rgb24_to_gray
1142 .convert = rgb24_to_pal8
1145 [PIX_FMT_RGBA32] = {
1146 [PIX_FMT_YUV420P] = {
1147 .convert = rgba32_to_yuv420p
1150 .convert = rgba32_to_gray
1154 [PIX_FMT_YUV420P] = {
1155 .convert = bgr24_to_yuv420p
1158 .convert = bgr24_to_gray
1161 [PIX_FMT_RGB555] = {
1162 [PIX_FMT_YUV420P] = {
1163 .convert = rgb555_to_yuv420p
1166 .convert = rgb555_to_gray
1169 [PIX_FMT_RGB565] = {
1170 [PIX_FMT_YUV420P] = {
1171 .convert = rgb565_to_yuv420p
1174 .convert = rgb565_to_gray
1178 [PIX_FMT_RGB555] = {
1179 .convert = gray_to_rgb555
1181 [PIX_FMT_RGB565] = {
1182 .convert = gray_to_rgb565
1185 .convert = gray_to_rgb24
1188 .convert = gray_to_bgr24
1190 [PIX_FMT_RGBA32] = {
1191 .convert = gray_to_rgba32
1193 [PIX_FMT_MONOWHITE] = {
1194 .convert = gray_to_monowhite
1196 [PIX_FMT_MONOBLACK] = {
1197 .convert = gray_to_monoblack
1200 [PIX_FMT_MONOWHITE] = {
1202 .convert = monowhite_to_gray
1205 [PIX_FMT_MONOBLACK] = {
1207 .convert = monoblack_to_gray
1211 [PIX_FMT_RGB555] = {
1212 .convert = pal8_to_rgb555
1214 [PIX_FMT_RGB565] = {
1215 .convert = pal8_to_rgb565
1218 .convert = pal8_to_bgr24
1221 .convert = pal8_to_rgb24
1223 [PIX_FMT_RGBA32] = {
1224 .convert = pal8_to_rgba32
1229 static int avpicture_alloc(AVPicture *picture,
1230 int pix_fmt, int width, int height)
1235 size = avpicture_get_size(pix_fmt, width, height);
1238 ptr = av_malloc(size);
1241 avpicture_fill(picture, ptr, pix_fmt, width, height);
1244 memset(picture, 0, sizeof(AVPicture));
1248 static void avpicture_free(AVPicture *picture)
1250 av_free(picture->data[0]);
1253 /* XXX: always use linesize. Return -1 if not supported */
1254 int img_convert(AVPicture *dst, int dst_pix_fmt,
1255 AVPicture *src, int src_pix_fmt,
1256 int src_width, int src_height)
1258 int i, ret, dst_width, dst_height, int_pix_fmt;
1259 PixFmtInfo *src_pix, *dst_pix;
1261 AVPicture tmp1, *tmp = &tmp1;
1263 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1264 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1266 if (src_width <= 0 || src_height <= 0)
1269 dst_width = src_width;
1270 dst_height = src_height;
1272 dst_pix = &pix_fmt_info[dst_pix_fmt];
1273 src_pix = &pix_fmt_info[src_pix_fmt];
1274 if (src_pix_fmt == dst_pix_fmt) {
1275 /* XXX: incorrect */
1276 /* same format: just copy */
1277 for(i = 0; i < dst_pix->nb_components; i++) {
1281 if (dst_pix->is_yuv && (i == 1 || i == 2)) {
1282 w >>= dst_pix->x_chroma_shift;
1283 h >>= dst_pix->y_chroma_shift;
1285 img_copy(dst->data[i], dst->linesize[i],
1286 src->data[i], src->linesize[i],
1292 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1294 /* specific convertion routine */
1295 ce->convert(dst, src, dst_width, dst_height);
1300 if (dst_pix->is_yuv && src_pix_fmt == PIX_FMT_GRAY8) {
1304 img_copy(dst->data[0], dst->linesize[0],
1305 src->data[0], src->linesize[0],
1306 dst_width, dst_height);
1307 /* fill U and V with 128 */
1310 w >>= dst_pix->x_chroma_shift;
1311 h >>= dst_pix->y_chroma_shift;
1312 for(i = 1; i <= 2; i++) {
1314 for(y = 0; y< h; y++) {
1316 d += dst->linesize[i];
1323 if (src_pix->is_yuv && dst_pix_fmt == PIX_FMT_GRAY8) {
1324 img_copy(dst->data[0], dst->linesize[0],
1325 src->data[0], src->linesize[0],
1326 dst_width, dst_height);
1331 if (dst_pix->is_yuv && src_pix->is_yuv) {
1332 int x_shift, y_shift, w, h;
1333 void (*resize_func)(uint8_t *dst, int dst_wrap,
1334 uint8_t *src, int src_wrap,
1335 int width, int height);
1337 /* compute chroma size of the smallest dimensions */
1340 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1341 w >>= dst_pix->x_chroma_shift;
1343 w >>= src_pix->x_chroma_shift;
1344 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1345 h >>= dst_pix->y_chroma_shift;
1347 h >>= src_pix->y_chroma_shift;
1349 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1350 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
1351 if (x_shift == 0 && y_shift == 0) {
1352 resize_func = img_copy; /* should never happen */
1353 } else if (x_shift == 0 && y_shift == 1) {
1354 resize_func = shrink2;
1355 } else if (x_shift == 1 && y_shift == 1) {
1356 resize_func = shrink22;
1357 } else if (x_shift == -1 && y_shift == -1) {
1358 resize_func = grow22;
1359 } else if (x_shift == -1 && y_shift == 1) {
1360 resize_func = conv411;
1362 /* currently not handled */
1366 img_copy(dst->data[0], dst->linesize[0],
1367 src->data[0], src->linesize[0],
1368 dst_width, dst_height);
1370 for(i = 1;i <= 2; i++)
1371 resize_func(dst->data[i], dst->linesize[i],
1372 src->data[i], src->linesize[i],
1373 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
1377 /* try to use an intermediate format */
1378 if (src_pix_fmt == PIX_FMT_MONOWHITE ||
1379 src_pix_fmt == PIX_FMT_MONOBLACK ||
1380 dst_pix_fmt == PIX_FMT_MONOWHITE ||
1381 dst_pix_fmt == PIX_FMT_MONOBLACK) {
1382 int_pix_fmt = PIX_FMT_GRAY8;
1384 int_pix_fmt = PIX_FMT_RGB24;
1386 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
1389 if (img_convert(tmp, int_pix_fmt,
1390 src, src_pix_fmt, src_width, src_height) < 0)
1392 if (img_convert(dst, dst_pix_fmt,
1393 tmp, int_pix_fmt, dst_width, dst_height) < 0)
1397 avpicture_free(tmp);
1403 #define DEINT_INPLACE_LINE_LUM \
1404 movd_m2r(lum_m4[0],mm0);\
1405 movd_m2r(lum_m3[0],mm1);\
1406 movd_m2r(lum_m2[0],mm2);\
1407 movd_m2r(lum_m1[0],mm3);\
1408 movd_m2r(lum[0],mm4);\
1409 punpcklbw_r2r(mm7,mm0);\
1410 movd_r2m(mm2,lum_m4[0]);\
1411 punpcklbw_r2r(mm7,mm1);\
1412 punpcklbw_r2r(mm7,mm2);\
1413 punpcklbw_r2r(mm7,mm3);\
1414 punpcklbw_r2r(mm7,mm4);\
1415 paddw_r2r(mm3,mm1);\
1417 paddw_r2r(mm4,mm0);\
1419 paddw_r2r(mm6,mm2);\
1420 paddw_r2r(mm2,mm1);\
1421 psubusw_r2r(mm0,mm1);\
1423 packuswb_r2r(mm7,mm1);\
1424 movd_r2m(mm1,lum_m2[0]);
1426 #define DEINT_LINE_LUM \
1427 movd_m2r(lum_m4[0],mm0);\
1428 movd_m2r(lum_m3[0],mm1);\
1429 movd_m2r(lum_m2[0],mm2);\
1430 movd_m2r(lum_m1[0],mm3);\
1431 movd_m2r(lum[0],mm4);\
1432 punpcklbw_r2r(mm7,mm0);\
1433 punpcklbw_r2r(mm7,mm1);\
1434 punpcklbw_r2r(mm7,mm2);\
1435 punpcklbw_r2r(mm7,mm3);\
1436 punpcklbw_r2r(mm7,mm4);\
1437 paddw_r2r(mm3,mm1);\
1439 paddw_r2r(mm4,mm0);\
1441 paddw_r2r(mm6,mm2);\
1442 paddw_r2r(mm2,mm1);\
1443 psubusw_r2r(mm0,mm1);\
1445 packuswb_r2r(mm7,mm1);\
1446 movd_r2m(mm1,dst[0]);
1449 /* filter parameters: [-1 4 2 4 -1] // 8 */
1450 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,
1454 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1457 for(;size > 0;size--) {
1459 sum += lum_m3[0] << 2;
1460 sum += lum_m2[0] << 1;
1461 sum += lum_m1[0] << 2;
1463 dst[0] = cm[(sum + 4) >> 3];
1480 movq_m2r(rounder,mm6);
1482 for (;size > 3; size-=4) {
1493 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1497 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1500 for(;size > 0;size--) {
1502 sum += lum_m3[0] << 2;
1503 sum += lum_m2[0] << 1;
1504 lum_m4[0]=lum_m2[0];
1505 sum += lum_m1[0] << 2;
1507 lum_m2[0] = cm[(sum + 4) >> 3];
1523 movq_m2r(rounder,mm6);
1525 for (;size > 3; size-=4) {
1526 DEINT_INPLACE_LINE_LUM
1536 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1537 top field is copied as is, but the bottom field is deinterlaced
1538 against the top field. */
1539 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1540 uint8_t *src1, int src_wrap,
1541 int width, int height)
1543 uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1548 src_0=&src_m1[src_wrap];
1549 src_p1=&src_0[src_wrap];
1550 src_p2=&src_p1[src_wrap];
1551 for(y=0;y<(height-2);y+=2) {
1552 memcpy(dst,src_m1,width);
1554 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1558 src_p1 += 2*src_wrap;
1559 src_p2 += 2*src_wrap;
1562 memcpy(dst,src_m1,width);
1565 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1568 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1569 int width, int height)
1571 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1574 buf = (uint8_t*)av_malloc(width);
1577 memcpy(buf,src_m1,width);
1578 src_0=&src_m1[src_wrap];
1579 src_p1=&src_0[src_wrap];
1580 src_p2=&src_p1[src_wrap];
1581 for(y=0;y<(height-2);y+=2) {
1582 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1585 src_p1 += 2*src_wrap;
1586 src_p2 += 2*src_wrap;
1589 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1594 /* deinterlace - if not supported return -1 */
1595 int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
1596 int pix_fmt, int width, int height)
1600 if (pix_fmt != PIX_FMT_YUV420P &&
1601 pix_fmt != PIX_FMT_YUV422P &&
1602 pix_fmt != PIX_FMT_YUV444P)
1604 if ((width & 3) != 0 || (height & 3) != 0)
1610 case PIX_FMT_YUV420P:
1614 case PIX_FMT_YUV422P:
1622 deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
1625 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1626 src->data[i], src->linesize[i],