[svn] / branches / dev-api-3 / xvidcore / src / image / colorspace.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/image/colorspace.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 630, Wed Nov 6 21:05:30 2002 UTC revision 631, Thu Nov 7 10:31:03 2002 UTC
# Line 39  Line 39 
39  // function pointers  // function pointers
40    
41  /* input */  /* input */
42  color_inputFuncPtr rgb555_to_yv12;  packedFuncPtr rgb555_to_yv12;
43  color_inputFuncPtr rgb565_to_yv12;  packedFuncPtr rgb565_to_yv12;
44  color_inputFuncPtr rgb24_to_yv12;  packedFuncPtr bgr_to_yv12;
45  color_inputFuncPtr rgb32_to_yv12;  packedFuncPtr bgra_to_yv12;
46  color_inputFuncPtr yuv_to_yv12;  packedFuncPtr abgr_to_yv12;
47  color_inputFuncPtr yuyv_to_yv12;  packedFuncPtr rgba_to_yv12;
48  color_inputFuncPtr uyvy_to_yv12;  packedFuncPtr yuv_to_yv12;
49    packedFuncPtr yuyv_to_yv12;
50    packedFuncPtr uyvy_to_yv12;
51    
52    packedFuncPtr rgb555i_to_yv12;
53    packedFuncPtr rgb565i_to_yv12;
54    packedFuncPtr bgri_to_yv12;
55    packedFuncPtr bgrai_to_yv12;
56    packedFuncPtr abgri_to_yv12;
57    packedFuncPtr rgbai_to_yv12;
58    packedFuncPtr yuyvi_to_yv12;
59    packedFuncPtr uyvyi_to_yv12;
60    
61  /* output */  /* output */
62  color_outputFuncPtr yv12_to_rgb555;  packedFuncPtr yv12_to_rgb555;
63  color_outputFuncPtr yv12_to_rgb565;  packedFuncPtr yv12_to_rgb565;
64  color_outputFuncPtr yv12_to_rgb24;  packedFuncPtr yv12_to_bgr;
65  color_outputFuncPtr yv12_to_rgb32;  packedFuncPtr yv12_to_bgra;
66  color_outputFuncPtr yv12_to_abgr;  packedFuncPtr yv12_to_abgr;
67  color_outputFuncPtr yv12_to_rgba;  packedFuncPtr yv12_to_rgba;
68  color_outputFuncPtr yv12_to_yuv;  packedFuncPtr yv12_to_yuv;
69  color_outputFuncPtr yv12_to_yuyv;  packedFuncPtr yv12_to_yuyv;
70  color_outputFuncPtr yv12_to_uyvy;  packedFuncPtr yv12_to_uyvy;
71    
72    packedFuncPtr yv12_to_rgb555i;
73    packedFuncPtr yv12_to_rgb565i;
74    packedFuncPtr yv12_to_bgri;
75    packedFuncPtr yv12_to_bgrai;
76    packedFuncPtr yv12_to_abgri;
77    packedFuncPtr yv12_to_rgbai;
78    packedFuncPtr yv12_to_yuyvi;
79    packedFuncPtr yv12_to_uyvyi;
80    
81    planarFuncPtr yv12_to_yv12;
82    
83    
84    int32_t RGB_Y_tab[256];
85    int32_t B_U_tab[256];
86    int32_t G_U_tab[256];
87    int32_t G_V_tab[256];
88    int32_t R_V_tab[256];
89    
90    
91  #define MIN(A,B)        ((A)<(B)?(A):(B))  #define MIN(A,B)        ((A)<(B)?(A):(B))
92  #define MAX(A,B)        ((A)>(B)?(A):(B))  #define MAX(A,B)        ((A)>(B)?(A):(B))
93    
94    
95    /********** generic colorspace macro **********/
96    
97    
98    #define MAKE_COLORSPACE(NAME,SIZE,PIXELS,VPIXELS,FUNC,C1,C2,C3,C4) \
99    void    \
100    NAME(uint8_t * x_ptr, int x_stride,     \
101                                     uint8_t * y_ptr, uint8_t * u_ptr, uint8_t * v_ptr,     \
102                                     int y_stride, int uv_stride,   \
103                                     int width, int height, int vflip)      \
104    {       \
105            int fixed_width = (width + 1) & ~1;                             \
106            int x_dif = x_stride - (SIZE)*fixed_width;              \
107            int y_dif = y_stride - fixed_width;                             \
108            int uv_dif = uv_stride - (fixed_width / 2);             \
109            int x, y;                                                                               \
110            if (vflip) {                                                            \
111                    x_ptr += (height - 1) * x_stride;                       \
112                    x_dif = -(SIZE)*fixed_width - x_stride;         \
113                    x_stride = -x_stride;                                           \
114            }                                                                                               \
115            for (y = 0; y < height; y+=(VPIXELS)) {                 \
116                    FUNC##_ROW(SIZE,C1,C2,C3,C4);                           \
117                    for (x = 0; x < fixed_width; x+=(PIXELS)) {     \
118                            FUNC(SIZE,C1,C2,C3,C4);                         \
119                            x_ptr += (PIXELS)*(SIZE);                               \
120                            y_ptr += (PIXELS);                                              \
121                            u_ptr += (PIXELS)/2;                                    \
122                            v_ptr += (PIXELS)/2;                                    \
123                    }                                                                                       \
124                    x_ptr += x_dif + (VPIXELS-1)*x_stride;          \
125                    y_ptr += y_dif + (VPIXELS-1)*y_stride;          \
126                    u_ptr += uv_dif + ((VPIXELS/2)-1)*uv_stride;    \
127                    v_ptr += uv_dif + ((VPIXELS/2)-1)*uv_stride;    \
128            }                                                                                               \
129    }
130    
131    
132    
133    /********** colorspace input (xxx_to_yv12) functions **********/
134    
135  /*      rgb -> yuv def's  /*      rgb -> yuv def's
136    
137          this following constants are "official spec"          this following constants are "official spec"
# Line 90  Line 161 
161  #define FIX_IN(x)               ((uint16_t) ((x) * (1L<<SCALEBITS_IN) + 0.5))  #define FIX_IN(x)               ((uint16_t) ((x) * (1L<<SCALEBITS_IN) + 0.5))
162    
163    
164  int32_t RGB_Y_tab[256];  /* rgb16/rgb16i input */
 int32_t B_U_tab[256];  
 int32_t G_U_tab[256];  
 int32_t G_V_tab[256];  
 int32_t R_V_tab[256];  
   
   
 /* rgb555 -> yuv 4:2:0 planar */  
 void  
 rgb555_to_yv12_c(uint8_t * y_out,  
                                  uint8_t * u_out,  
                                  uint8_t * v_out,  
                                  uint8_t * src,  
                                  int width,  
                                  int height,  
                                  int y_stride)  
 {  
         int32_t src_stride = width * 2;  
         uint32_t y_dif = y_stride - width;  
         uint32_t uv_dif = (y_stride - width) / 2;  
         uint32_t x, y;  
   
         if (height < 0) {  
                 height = -height;  
                 src += (height - 1) * src_stride;  
                 src_stride = -src_stride;  
         }  
   
   
         for (y = height / 2; y; y--) {  
                 // process one 2x2 block per iteration  
                 for (x = 0; x < (uint32_t) width; x += 2) {  
                         int rgb, r, g, b, r4, g4, b4;  
   
                         rgb = *(uint16_t *) (src + x * 2);  
                         b4 = b = (rgb << 3) & 0xf8;  
                         g4 = g = (rgb >> 2) & 0xf8;  
                         r4 = r = (rgb >> 7) & 0xf8;  
                         y_out[0] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         rgb = *(uint16_t *) (src + x * 2 + src_stride);  
                         b4 += b = (rgb << 3) & 0xf8;  
                         g4 += g = (rgb >> 2) & 0xf8;  
                         r4 += r = (rgb >> 7) & 0xf8;  
                         y_out[y_stride] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         rgb = *(uint16_t *) (src + x * 2 + 2);  
                         b4 += b = (rgb << 3) & 0xf8;  
                         g4 += g = (rgb >> 2) & 0xf8;  
                         r4 += r = (rgb >> 7) & 0xf8;  
                         y_out[1] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         rgb = *(uint16_t *) (src + x * 2 + src_stride + 2);  
                         b4 += b = (rgb << 3) & 0xf8;  
                         g4 += g = (rgb >> 2) & 0xf8;  
                         r4 += r = (rgb >> 7) & 0xf8;  
                         y_out[y_stride + 1] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         *u_out++ =  
                                 (uint8_t) ((-FIX_IN(U_R_IN) * r4 - FIX_IN(U_G_IN) * g4 +  
                                                         FIX_IN(U_B_IN) * b4) >> (SCALEBITS_IN + 2)) +  
                                 U_ADD_IN;  
   
   
                         *v_out++ =  
                                 (uint8_t) ((FIX_IN(V_R_IN) * r4 - FIX_IN(V_G_IN) * g4 -  
                                                         FIX_IN(V_B_IN) * b4) >> (SCALEBITS_IN + 2)) +  
                                 V_ADD_IN;  
   
                         y_out += 2;  
                 }  
                 src += src_stride * 2;  
                 y_out += y_dif + y_stride;  
                 u_out += uv_dif;  
                 v_out += uv_dif;  
         }  
 }  
   
   
   
 /* rgb565_to_yuv_c  
         NOTE:   identical to rgb555 except for shift/mask  
                         not tested */  
   
 void  
 rgb565_to_yv12_c(uint8_t * y_out,  
                                  uint8_t * u_out,  
                                  uint8_t * v_out,  
                                  uint8_t * src,  
                                  int width,  
                                  int height,  
                                  int y_stride)  
 {  
         int32_t src_stride = width * 2;  
   
         uint32_t y_dif = y_stride - width;  
         uint32_t uv_dif = (y_stride - width) / 2;  
         uint32_t x, y;  
   
         if (height < 0) {  
                 height = -height;  
                 src += (height - 1) * src_stride;  
                 src_stride = -src_stride;  
         }  
   
   
         for (y = height / 2; y; y--) {  
                 // process one 2x2 block per iteration  
                 for (x = 0; x < (uint32_t) width; x += 2) {  
                         int rgb, r, g, b, r4, g4, b4;  
   
                         rgb = *(uint16_t *) (src + x * 2);  
                         b4 = b = (rgb << 3) & 0xf8;  
                         g4 = g = (rgb >> 3) & 0xfc;  
                         r4 = r = (rgb >> 8) & 0xf8;  
                         y_out[0] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         rgb = *(uint16_t *) (src + x * 2 + src_stride);  
                         b4 += b = (rgb << 3) & 0xf8;  
                         g4 += g = (rgb >> 3) & 0xfc;  
                         r4 += r = (rgb >> 8) & 0xf8;  
                         y_out[y_stride] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         rgb = *(uint16_t *) (src + x * 2 + 2);  
                         b4 += b = (rgb << 3) & 0xf8;  
                         g4 += g = (rgb >> 3) & 0xfc;  
                         r4 += r = (rgb >> 8) & 0xf8;  
                         y_out[1] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         rgb = *(uint16_t *) (src + x * 2 + src_stride + 2);  
                         b4 += b = (rgb << 3) & 0xf8;  
                         g4 += g = (rgb >> 3) & 0xfc;  
                         r4 += r = (rgb >> 8) & 0xf8;  
                         y_out[y_stride + 1] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         *u_out++ =  
                                 (uint8_t) ((-FIX_IN(U_R_IN) * r4 - FIX_IN(U_G_IN) * g4 +  
                                                         FIX_IN(U_B_IN) * b4) >> (SCALEBITS_IN + 2)) +  
                                 U_ADD_IN;  
   
   
                         *v_out++ =  
                                 (uint8_t) ((FIX_IN(V_R_IN) * r4 - FIX_IN(V_G_IN) * g4 -  
                                                         FIX_IN(V_B_IN) * b4) >> (SCALEBITS_IN + 2)) +  
                                 V_ADD_IN;  
   
                         y_out += 2;  
                 }  
                 src += src_stride * 2;  
                 y_out += y_dif + y_stride;  
                 u_out += uv_dif;  
                 v_out += uv_dif;  
         }  
 }  
   
   
   
   
 /*      rgb24 -> yuv 4:2:0 planar  
   
         NOTE: always flips.  
 */  
   
 void  
 rgb24_to_yv12_c(uint8_t * y_out,  
                                 uint8_t * u_out,  
                                 uint8_t * v_out,  
                                 uint8_t * src,  
                                 int width,  
                                 int height,  
                                 int stride)  
 {  
         uint32_t width3 = (width << 1) + width; /* width * 3 */  
         uint32_t src_dif = (width << 3) + width;        /* width3 * 3 */  
         uint32_t y_dif = (stride << 1) - width;  
         uint32_t uv_dif = (stride - width) >> 1;  
         uint32_t x, y;  
   
         src += (height - 2) * width3;  
   
   
         for (y = height >> 1; y; y--) {  
                 for (x = width >> 1; x; x--) {  
                         uint32_t r, g, b, r4, g4, b4;  
   
                         b4 = b = src[0];  
                         g4 = g = src[1];  
                         r4 = r = src[2];  
                         y_out[stride + 0] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         b4 += (b = src[3]);  
                         g4 += (g = src[4]);  
                         r4 += (r = src[5]);  
                         y_out[stride + 1] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         b4 += (b = src[width3 + 0]);  
                         g4 += (g = src[width3 + 1]);  
                         r4 += (r = src[width3 + 2]);  
                         y_out[0] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         b4 += (b = src[width3 + 3]);  
                         g4 += (g = src[width3 + 4]);  
                         r4 += (r = src[width3 + 5]);  
                         y_out[1] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         *u_out++ =  
                                 (uint8_t) ((-FIX_IN(U_R_IN) * r4 - FIX_IN(U_G_IN) * g4 +  
                                                         FIX_IN(U_B_IN) * b4) >> (SCALEBITS_IN + 2)) +  
                                 U_ADD_IN;  
   
   
                         *v_out++ =  
                                 (uint8_t) ((FIX_IN(V_R_IN) * r4 - FIX_IN(V_G_IN) * g4 -  
                                                         FIX_IN(V_B_IN) * b4) >> (SCALEBITS_IN + 2)) +  
                                 V_ADD_IN;  
   
   
                         src += 6;  
                         y_out += 2;  
                 }  
                 src -= src_dif;  
                 y_out += y_dif;  
                 u_out += uv_dif;  
                 v_out += uv_dif;  
         }  
 }  
   
   
 /*      rgb32 -> yuv 4:2:0 planar  
   
         NOTE: always flips  
 */  
   
 void  
 rgb32_to_yv12_c(uint8_t * y_out,  
                                 uint8_t * u_out,  
                                 uint8_t * v_out,  
                                 uint8_t * src,  
                                 int width,  
                                 int height,  
                                 int stride)  
 {  
         uint32_t width4 = (width << 2); /* width * 4 */  
         uint32_t src_dif = 3 * width4;  
         uint32_t y_dif = (stride << 1) - width;  
         uint32_t uv_dif = (stride - width) >> 1;  
         uint32_t x, y;  
   
         src += (height - 2) * width4;  
   
         for (y = height >> 1; y; y--) {  
                 for (x = width >> 1; x; x--) {  
                         uint32_t r, g, b, r4, g4, b4;  
   
                         b4 = b = src[0];  
                         g4 = g = src[1];  
                         r4 = r = src[2];  
                         y_out[stride + 0] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         b4 += (b = src[4]);  
                         g4 += (g = src[5]);  
                         r4 += (r = src[6]);  
                         y_out[stride + 1] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         b4 += (b = src[width4 + 0]);  
                         g4 += (g = src[width4 + 1]);  
                         r4 += (r = src[width4 + 2]);  
   
                         y_out[0] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         b4 += (b = src[width4 + 4]);  
                         g4 += (g = src[width4 + 5]);  
                         r4 += (r = src[width4 + 6]);  
                         y_out[1] =  
                                 (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +  
                                                         FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;  
   
                         *u_out++ =  
                                 (uint8_t) ((-FIX_IN(U_R_IN) * r4 - FIX_IN(U_G_IN) * g4 +  
                                                         FIX_IN(U_B_IN) * b4) >> (SCALEBITS_IN + 2)) +  
                                 U_ADD_IN;  
   
                         *v_out++ =  
                                 (uint8_t) ((FIX_IN(V_R_IN) * r4 - FIX_IN(V_G_IN) * g4 -  
                                                         FIX_IN(V_B_IN) * b4) >> (SCALEBITS_IN + 2)) +  
                                 V_ADD_IN;  
   
                         src += 8;  
                         y_out += 2;  
                 }  
                 src -= src_dif;  
                 y_out += y_dif;  
                 u_out += uv_dif;  
                 v_out += uv_dif;  
         }  
 }  
   
 /*      yuv planar -> yuv 4:2:0 planar  
   
         NOTE: does not flip */  
   
 void  
 yuv_to_yv12_c(uint8_t * y_out,  
                           uint8_t * u_out,  
                           uint8_t * v_out,  
                           uint8_t * src,  
                           int width,  
                           int height,  
                           int stride)  
 {  
         uint32_t stride2 = stride >> 1;  
         uint32_t width2 = width >> 1;  
         uint32_t y;  
   
         for (y = height; y; y--) {  
                 memcpy(y_out, src, width);  
                 src += width;  
                 y_out += stride;  
         }  
   
         for (y = height >> 1; y; y--) {  
                 memcpy(u_out, src, width2);  
                 src += width2;  
                 u_out += stride2;  
         }  
   
         for (y = height >> 1; y; y--) {  
                 memcpy(v_out, src, width2);  
                 src += width2;  
                 v_out += stride2;  
         }  
 }  
   
   
   
 /* yuyv (yuv2) packed -> yuv 4:2:0 planar  
   
    NOTE: does not flip */  
   
 void  
 yuyv_to_yv12_c(uint8_t * y_out,  
                            uint8_t * u_out,  
                            uint8_t * v_out,  
                            uint8_t * src,  
                            int width,  
                            int height,  
                            int stride)  
 {  
         uint32_t width2 = width + width;  
         uint32_t y_dif = stride - width;  
         uint32_t uv_dif = y_dif >> 1;  
         uint32_t x, y;  
   
         for (y = height >> 1; y; y--) {  
   
                 for (x = width >> 1; x; x--) {  
                         *y_out++ = *src++;  
                         //*u_out++ = *src++;  
                         *u_out++ = (*(src + width2) + *src) >> 1;  
                         src++;  
                         *y_out++ = *src++;  
                         //*v_out++ = *src++;  
                         *v_out++ = (*(src + width2) + *src) >> 1;  
                         src++;  
   
                 }  
   
                 y_out += y_dif;  
                 u_out += uv_dif;  
                 v_out += uv_dif;  
   
                 for (x = width >> 1; x; x--) {  
                         *y_out++ = *src++;  
                         src++;  
                         *y_out++ = *src++;  
                         src++;  
                 }  
   
                 y_out += y_dif;  
   
         }  
   
 }  
   
   
165    
166  /* uyvy packed -> yuv 4:2:0 planar  #define MK_RGB555_B(RGB)  ((RGB) << 3) & 0xf8
167    #define MK_RGB555_G(RGB)  ((RGB) >> 2) & 0xf8
168    #define MK_RGB555_R(RGB)  ((RGB) >> 7) & 0xf8
169    
170    #define MK_RGB565_B(RGB)  ((RGB) << 3) & 0xf8
171    #define MK_RGB565_G(RGB)  ((RGB) >> 3) & 0xfc
172    #define MK_RGB565_R(RGB)  ((RGB) >> 8) & 0xf8
173    
174    
175    #define READ_RGB16_Y(ROW, UVID, C1,C2,C3,C4)    \
176            rgb = *(uint16_t *) (x_ptr + ((ROW)*x_stride) + 0);     \
177            b##UVID += b = C1##_B(rgb);                             \
178            g##UVID += g = C1##_G(rgb);                             \
179            r##UVID += r = C1##_R(rgb);                             \
180            y_ptr[(ROW)*y_stride+0] =                               \
181                    (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +   \
182                                            FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;        \
183            rgb = *(uint16_t *) (x_ptr + ((ROW)*x_stride) + 2);     \
184            b##UVID += b = C1##_B(rgb);                             \
185            g##UVID += g = C1##_G(rgb);                             \
186            r##UVID += r = C1##_R(rgb);                             \
187            y_ptr[(ROW)*y_stride+1] =                               \
188                    (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +                   \
189                                            FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;
190    
191    #define READ_RGB16_UV(UV_ROW,UVID)      \
192            u_ptr[(UV_ROW)*uv_stride] =                                                                                                             \
193                    (uint8_t) ((-FIX_IN(U_R_IN) * r##UVID - FIX_IN(U_G_IN) * g##UVID +                      \
194                                            FIX_IN(U_B_IN) * b##UVID) >> (SCALEBITS_IN + 2)) + U_ADD_IN;    \
195            v_ptr[(UV_ROW)*uv_stride] =                                                                                                             \
196                    (uint8_t) ((FIX_IN(V_R_IN) * r##UVID - FIX_IN(V_G_IN) * g##UVID -                       \
197                                            FIX_IN(V_B_IN) * b##UVID) >> (SCALEBITS_IN + 2)) + V_ADD_IN;
198    
199    #define RGB16_TO_YV12_ROW(SIZE,C1,C2,C3,C4) \
200            /* nothing */
201    #define RGB16_TO_YV12(SIZE,C1,C2,C3,C4) \
202            uint32_t rgb, r, g, b, r0, g0, b0;      \
203            r0 = g0 = b0 = 0;                                       \
204            READ_RGB16_Y (0, 0, C1,C2,C3,C4)        \
205            READ_RGB16_Y (1, 0, C1,C2,C3,C4)        \
206            READ_RGB16_UV(0, 0)
207    
208    
209    #define RGB16I_TO_YV12_ROW(SIZE,C1,C2,C3,C4) \
210            /* nothing */
211    #define RGB16I_TO_YV12(SIZE,C1,C2,C3,C4)        \
212            uint32_t rgb, r, g, b, r0, g0, b0, r1, g1, b1;  \
213            r0 = g0 = b0 = r1 = g1 = b1 = 0;        \
214            READ_RGB16_Y (0, 0, C1,C2,C3,C4)        \
215            READ_RGB16_Y (1, 1, C1,C2,C3,C4)        \
216            READ_RGB16_Y (2, 0, C1,C2,C3,C4)        \
217            READ_RGB16_Y (3, 1, C1,C2,C3,C4)        \
218            READ_RGB16_UV(0, 0)                                     \
219            READ_RGB16_UV(1, 1)
220    
221    
222    /* rgb/rgbi input */
223    
224    #define READ_RGB_Y(SIZE, ROW, UVID, C1,C2,C3,C4)        \
225            r##UVID += r = x_ptr[(ROW)*x_stride+(C1)];                                              \
226            g##UVID += g = x_ptr[(ROW)*x_stride+(C2)];                                              \
227            b##UVID += b = x_ptr[(ROW)*x_stride+(C3)];                                              \
228            y_ptr[(ROW)*y_stride+0] =                                                                       \
229                    (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +   \
230                                            FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;        \
231            r##UVID += r = x_ptr[(ROW)*x_stride+(SIZE)+(C1)];                               \
232            g##UVID += g = x_ptr[(ROW)*x_stride+(SIZE)+(C2)];                               \
233            b##UVID += b = x_ptr[(ROW)*x_stride+(SIZE)+(C3)];                               \
234            y_ptr[(ROW)*y_stride+1] =                                                                       \
235                    (uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g +   \
236                                            FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;
237    
238    #define READ_RGB_UV(UV_ROW,UVID)        \
239            u_ptr[(UV_ROW)*uv_stride] =                                                                                                             \
240                    (uint8_t) ((-FIX_IN(U_R_IN) * r##UVID - FIX_IN(U_G_IN) * g##UVID +                      \
241                                            FIX_IN(U_B_IN) * b##UVID) >> (SCALEBITS_IN + 2)) + U_ADD_IN;    \
242            v_ptr[(UV_ROW)*uv_stride] =                                                                                                             \
243                    (uint8_t) ((FIX_IN(V_R_IN) * r##UVID - FIX_IN(V_G_IN) * g##UVID -                       \
244                                            FIX_IN(V_B_IN) * b##UVID) >> (SCALEBITS_IN + 2)) + V_ADD_IN;
245    
246    
247    #define RGB_TO_YV12_ROW(SIZE,C1,C2,C3,C4) \
248            /* nothing */
249    #define RGB_TO_YV12(SIZE,C1,C2,C3,C4)   \
250            uint32_t r, g, b, r0, g0, b0;           \
251            r0 = g0 = b0 = 0;                                       \
252            READ_RGB_Y(SIZE, 0, 0, C1,C2,C3,C4)     \
253            READ_RGB_Y(SIZE, 1, 0, C1,C2,C3,C4)     \
254            READ_RGB_UV(     0, 0)
255    
256    #define RGBI_TO_YV12_ROW(SIZE,C1,C2,C3,C4) \
257            /* nothing */
258    #define RGBI_TO_YV12(SIZE,C1,C2,C3,C4)  \
259            uint32_t r, g, b, r0, g0, b0, r1, g1, b1;       \
260            r0 = g0 = b0 = r1 = g1 = b1 = 0;        \
261            READ_RGB_Y(SIZE, 0, 0, C1,C2,C3,C4)     \
262            READ_RGB_Y(SIZE, 1, 1, C1,C2,C3,C4)     \
263            READ_RGB_Y(SIZE, 2, 0, C1,C2,C3,C4)     \
264            READ_RGB_Y(SIZE, 3, 1, C1,C2,C3,C4)     \
265            READ_RGB_UV(     0, 0)                          \
266            READ_RGB_UV(     1, 1)
267    
268    
269    /* yuyv/yuyvi input */
270    
271    #define READ_YUYV_Y(ROW,C1,C2,C3,C4)    \
272            y_ptr[(ROW)*y_stride+0] = x_ptr[(ROW)*x_stride+(C1)];   \
273            y_ptr[(ROW)*y_stride+1] = x_ptr[(ROW)*x_stride+(C3)];
274    #define READ_YUYV_UV(UV_ROW,ROW1,ROW2,C1,C2,C3,C4) \
275            u_ptr[(UV_ROW)*uv_stride] = (x_ptr[(ROW1)*x_stride+(C2)] + x_ptr[(ROW2)*x_stride+(C2)] + 1) / 2;        \
276            v_ptr[(UV_ROW)*uv_stride] = (x_ptr[(ROW1)*x_stride+(C4)] + x_ptr[(ROW2)*x_stride+(C4)] + 1) / 2;
277    
278    #define YUYV_TO_YV12_ROW(SIZE,C1,C2,C3,C4) \
279            /* nothing */
280    #define YUYV_TO_YV12(SIZE,C1,C2,C3,C4)  \
281            READ_YUYV_Y (0,      C1,C2,C3,C4)       \
282            READ_YUYV_Y (1,      C1,C2,C3,C4)       \
283            READ_YUYV_UV(0, 0,1, C1,C2,C3,C4)
284    
285    #define YUYVI_TO_YV12_ROW(SIZE,C1,C2,C3,C4) \
286            /* nothing */
287    #define YUYVI_TO_YV12(SIZE,C1,C2,C3,C4) \
288            READ_YUYV_Y (0, C1,C2,C3,C4)    \
289            READ_YUYV_Y (1, C1,C2,C3,C4)    \
290            READ_YUYV_Y (2, C1,C2,C3,C4)    \
291            READ_YUYV_Y (3, C1,C2,C3,C4)    \
292            READ_YUYV_UV(0, 0,2, C1,C2,C3,C4)       \
293            READ_YUYV_UV(1, 1,3, C1,C2,C3,C4)
294    
295    
296    MAKE_COLORSPACE(rgb555_to_yv12_c,  2,2,2, RGB16_TO_YV12,  MK_RGB555, 0,0,0)
297    MAKE_COLORSPACE(rgb565_to_yv12_c,  2,2,2, RGB16_TO_YV12,  MK_RGB565, 0,0,0)
298    MAKE_COLORSPACE(bgr_to_yv12_c,     3,2,2, RGB_TO_YV12,    2,1,0, 0)
299    MAKE_COLORSPACE(bgra_to_yv12_c,    4,2,2, RGB_TO_YV12,    2,1,0, 0)
300    MAKE_COLORSPACE(abgr_to_yv12_c,    4,2,2, RGB_TO_YV12,    3,2,1, 0)
301    MAKE_COLORSPACE(rgba_to_yv12_c,    4,2,2, RGB_TO_YV12,    0,1,2, 0)
302    MAKE_COLORSPACE(yuyv_to_yv12_c,    2,2,2, YUYV_TO_YV12,   0,1,2,3)
303    MAKE_COLORSPACE(uyvy_to_yv12_c,    2,2,2, YUYV_TO_YV12,   1,0,3,2)
304    
305    MAKE_COLORSPACE(rgb555i_to_yv12_c, 2,2,4, RGB16I_TO_YV12, MK_RGB555, 0,0,0)
306    MAKE_COLORSPACE(rgb565i_to_yv12_c, 2,2,4, RGB16I_TO_YV12, MK_RGB565, 0,0,0)
307    MAKE_COLORSPACE(bgri_to_yv12_c,    3,2,4, RGBI_TO_YV12,   2,1,0, 0)
308    MAKE_COLORSPACE(bgrai_to_yv12_c,   4,2,4, RGBI_TO_YV12,   2,1,0, 0)
309    MAKE_COLORSPACE(abgri_to_yv12_c,   4,2,4, RGBI_TO_YV12,   3,2,1, 0)
310    MAKE_COLORSPACE(rgbai_to_yv12_c,   4,2,4, RGBI_TO_YV12,   0,1,2, 0)
311    MAKE_COLORSPACE(yuyvi_to_yv12_c,   2,2,4, YUYVI_TO_YV12,  0,1,2,3)
312    MAKE_COLORSPACE(uyvyi_to_yv12_c,   2,2,4, YUYVI_TO_YV12,  1,0,3,2)
313    
    NOTE: does not flip */  
   
   
 void  
 uyvy_to_yv12_c(uint8_t * y_out,  
                            uint8_t * u_out,  
                            uint8_t * v_out,  
                            uint8_t * src,  
                            int width,  
                            int height,  
                            int stride)  
 {  
         uint32_t width2 = width + width;  
         uint32_t y_dif = stride - width;  
         uint32_t uv_dif = y_dif >> 1;  
         uint32_t x, y;  
   
         for (y = height >> 1; y; y--) {  
   
                 for (x = width >> 1; x; x--) {  
                         *u_out++ = *src++;  
                         // *u_out++ = (*(src+width2) + *src++) >> 1;  
                         *y_out++ = *src++;  
                         //*v_out++ = *src++;  
                         *v_out++ = (*(src + width2) + *src) >> 1;  
                         src++;  
                         *y_out++ = *src++;  
                 }  
   
                 y_out += y_dif;  
                 u_out += uv_dif;;  
                 v_out += uv_dif;;  
   
                 for (x = width >> 1; x; x--) {  
                         src++;  
                         *y_out++ = *src++;  
                         src++;  
                         *y_out++ = *src++;  
                 }  
   
                 y_out += y_dif;  
         }  
 }  
314    
315    /********** colorspace output (yv12_to_xxx) functions **********/
316    
317  /*      yuv -> rgb def's */  /*      yuv -> rgb def's */
318    
# Line 566  Line 327 
327  #define R_V_OUT                 1.596  #define R_V_OUT                 1.596
328  #define V_ADD_OUT               128  #define V_ADD_OUT               128
329    
   
330  #define SCALEBITS_OUT   13  #define SCALEBITS_OUT   13
331  #define FIX_OUT(x)              ((uint16_t) ((x) * (1L<<SCALEBITS_OUT) + 0.5))  #define FIX_OUT(x)              ((uint16_t) ((x) * (1L<<SCALEBITS_OUT) + 0.5))
332    
333    
334  /* initialize rgb lookup tables */  /* rgb16/rgb16i output */
335    
336  void  #define MK_RGB555(R,G,B)        \
337  colorspace_init(void)          ((MAX(0,MIN(255, R)) << 7) & 0x7c00) | \
 {  
         int32_t i;  
   
         for (i = 0; i < 256; i++) {  
                 RGB_Y_tab[i] = FIX_OUT(RGB_Y_OUT) * (i - Y_ADD_OUT);  
                 B_U_tab[i] = FIX_OUT(B_U_OUT) * (i - U_ADD_OUT);  
                 G_U_tab[i] = FIX_OUT(G_U_OUT) * (i - U_ADD_OUT);  
                 G_V_tab[i] = FIX_OUT(G_V_OUT) * (i - V_ADD_OUT);  
                 R_V_tab[i] = FIX_OUT(R_V_OUT) * (i - V_ADD_OUT);  
         }  
 }  
   
 /* yuv 4:2:0 planar -> rgb555 + very simple error diffusion  
 */  
   
 #define MK_RGB555(R,G,B)        ((MAX(0,MIN(255, R)) << 7) & 0x7c00) | \  
338                                                          ((MAX(0,MIN(255, G)) << 2) & 0x03e0) | \                                                          ((MAX(0,MIN(255, G)) << 2) & 0x03e0) | \
339                                                          ((MAX(0,MIN(255, B)) >> 3) & 0x001f)                                                          ((MAX(0,MIN(255, B)) >> 3) & 0x001f)
340    
341    #define MK_RGB565(R,G,B)        \
342  void          ((MAX(0,MIN(255, R)) << 8) & 0xf800) | \
 yv12_to_rgb555_c(uint8_t * dst,  
                                  int dst_stride,  
                                  uint8_t * y_src,  
                                  uint8_t * u_src,  
                                  uint8_t * v_src,  
                                  int y_stride,  
                                  int uv_stride,  
                                  int width,  
                                  int height)  
 {  
         const uint32_t dst_dif = 2 * dst_stride - 2 * width;  
         int32_t y_dif = 2 * y_stride - width;  
   
         uint8_t *dst2 = dst + dst_stride;  
         uint8_t *y_src2 = y_src + y_stride;  
         uint32_t x, y;  
   
         if (height < 0) {  
                 height = -height;  
                 y_src += (height - 1) * y_stride;  
                 y_src2 = y_src - y_stride;  
                 u_src += (height / 2 - 1) * uv_stride;  
                 v_src += (height / 2 - 1) * uv_stride;  
                 y_dif = -width - 2 * y_stride;  
                 uv_stride = -uv_stride;  
         }  
   
         for (y = height / 2; y; y--) {  
                 int r, g, b;  
                 int r2, g2, b2;  
   
                 r = g = b = 0;  
                 r2 = g2 = b2 = 0;  
   
                 // process one 2x2 block per iteration  
                 for (x = 0; x < (uint32_t) width / 2; x++) {  
                         int u, v;  
                         int b_u, g_uv, r_v, rgb_y;  
   
                         u = u_src[x];  
                         v = v_src[x];  
   
                         b_u = B_U_tab[u];  
                         g_uv = G_U_tab[u] + G_V_tab[v];  
                         r_v = R_V_tab[v];  
   
                         rgb_y = RGB_Y_tab[*y_src];  
                         b = (b & 0x7) + ((rgb_y + b_u) >> SCALEBITS_OUT);  
                         g = (g & 0x7) + ((rgb_y - g_uv) >> SCALEBITS_OUT);  
                         r = (r & 0x7) + ((rgb_y + r_v) >> SCALEBITS_OUT);  
                         *(uint16_t *) dst = MK_RGB555(r, g, b);  
   
                         y_src++;  
                         rgb_y = RGB_Y_tab[*y_src];  
                         b = (b & 0x7) + ((rgb_y + b_u) >> SCALEBITS_OUT);  
                         g = (g & 0x7) + ((rgb_y - g_uv) >> SCALEBITS_OUT);  
                         r = (r & 0x7) + ((rgb_y + r_v) >> SCALEBITS_OUT);  
                         *(uint16_t *) (dst + 2) = MK_RGB555(r, g, b);  
                         y_src++;  
   
                         rgb_y = RGB_Y_tab[*y_src2];  
                         b2 = (b2 & 0x7) + ((rgb_y + b_u) >> SCALEBITS_OUT);  
                         g2 = (g2 & 0x7) + ((rgb_y - g_uv) >> SCALEBITS_OUT);  
                         r2 = (r2 & 0x7) + ((rgb_y + r_v) >> SCALEBITS_OUT);  
                         *(uint16_t *) (dst2) = MK_RGB555(r2, g2, b2);  
                         y_src2++;  
   
                         rgb_y = RGB_Y_tab[*y_src2];  
                         b2 = (b2 & 0x7) + ((rgb_y + b_u) >> SCALEBITS_OUT);  
                         g2 = (g2 & 0x7) + ((rgb_y - g_uv) >> SCALEBITS_OUT);  
                         r2 = (r2 & 0x7) + ((rgb_y + r_v) >> SCALEBITS_OUT);  
                         *(uint16_t *) (dst2 + 2) = MK_RGB555(r2, g2, b2);  
                         y_src2++;  
   
                         dst += 4;  
                         dst2 += 4;  
                 }  
   
                 dst += dst_dif;  
                 dst2 += dst_dif;  
   
                 y_src += y_dif;  
                 y_src2 += y_dif;  
   
                 u_src += uv_stride;  
                 v_src += uv_stride;  
         }  
 }  
   
   
 /* yuv 4:2:0 planar -> rgb565 + very simple error diffusion  
         NOTE:   identical to rgb555 except for shift/mask  */  
   
   
 #define MK_RGB565(R,G,B)        ((MAX(0,MIN(255, R)) << 8) & 0xf800) | \  
343                                                          ((MAX(0,MIN(255, G)) << 3) & 0x07e0) | \                                                          ((MAX(0,MIN(255, G)) << 3) & 0x07e0) | \
344                                                          ((MAX(0,MIN(255, B)) >> 3) & 0x001f)                                                          ((MAX(0,MIN(255, B)) >> 3) & 0x001f)
345    
346  void  #define WRITE_RGB16(ROW,UV_ROW,C1)      \
347  yv12_to_rgb565_c(uint8_t * dst,          rgb_y = RGB_Y_tab[ y_ptr[y_stride + 0] ];                       \
348                                   int dst_stride,          b[ROW] = (b[ROW] & 0x7) + ((rgb_y + b_u##UV_ROW) >> SCALEBITS_OUT);     \
349                                   uint8_t * y_src,          g[ROW] = (g[ROW] & 0x7) + ((rgb_y - g_uv##UV_ROW) >> SCALEBITS_OUT);    \
350                                   uint8_t * u_src,          r[ROW] = (r[ROW] & 0x7) + ((rgb_y + r_v##UV_ROW) >> SCALEBITS_OUT);             \
351                                   uint8_t * v_src,          *(uint16_t *) (x_ptr+((ROW)*x_stride)+0) = C1(r[ROW], g[ROW], b[ROW]);  \
352                                   int y_stride,          rgb_y = RGB_Y_tab[ y_ptr[y_stride + 1] ];                               \
353                                   int uv_stride,          b[ROW] = (b[ROW] & 0x7) + ((rgb_y + b_u##UV_ROW) >> SCALEBITS_OUT);             \
354                                   int width,          g[ROW] = (g[ROW] & 0x7) + ((rgb_y - g_uv##UV_ROW) >> SCALEBITS_OUT);    \
355                                   int height)          r[ROW] = (r[ROW] & 0x7) + ((rgb_y + r_v##UV_ROW) >> SCALEBITS_OUT);             \
356  {          *(uint16_t *) (x_ptr+((ROW)*x_stride)+2) = C1(r[ROW], g[ROW], b[ROW]);
357          const uint32_t dst_dif = 2 * dst_stride - 2 * width;  
358          int32_t y_dif = 2 * y_stride - width;  #define YV12_TO_RGB16_ROW(SIZE,C1,C2,C3,C4) \
359            int r[2], g[2], b[2];                                   \
360          uint8_t *dst2 = dst + dst_stride;          r[0] = r[1] = g[0] = g[1] = b[0] = b[1] = 0;
361          uint8_t *y_src2 = y_src + y_stride;  #define YV12_TO_RGB16(SIZE,C1,C2,C3,C4)         \
362          uint32_t x, y;          int rgb_y;                                                                                              \
363            int b_u0 = B_U_tab[ u_ptr[0] ];                                                         \
364          if (height < 0) {                       // flip image?          int g_uv0 = G_U_tab[ u_ptr[0] ] + G_V_tab[ v_ptr[0] ];          \
365                  height = -height;          int r_v0 = R_V_tab[ v_ptr[0] ];                                                         \
366                  y_src += (height - 1) * y_stride;          WRITE_RGB16(0, 0, C1)                                                                           \
367                  y_src2 = y_src - y_stride;          WRITE_RGB16(1, 0, C1)
368                  u_src += (height / 2 - 1) * uv_stride;  
369                  v_src += (height / 2 - 1) * uv_stride;  #define YV12_TO_RGB16I_ROW(SIZE,C1,C2,C3,C4) \
370                  y_dif = -width - 2 * y_stride;          int r[4], g[4], b[4];                                   \
371                  uv_stride = -uv_stride;          r[0] = r[1] = r[2] = r[3] = 0;                  \
372          }          g[0] = g[1] = g[2] = g[3] = 0;                  \
373            b[0] = b[1] = b[2] = b[3] = 0;
374          for (y = height / 2; y; y--) {  #define YV12_TO_RGB16I(SIZE,C1,C2,C3,C4)                \
375                  int r, g, b;          int rgb_y;                                                                                                      \
376                  int r2, g2, b2;          int b_u0 = B_U_tab[ u_ptr[0] ];                                                         \
377            int g_uv0 = G_U_tab[ u_ptr[0] ] + G_V_tab[ v_ptr[0] ];          \
378                  r = g = b = 0;          int r_v0 = R_V_tab[ v_ptr[0] ];                                                         \
379                  r2 = g2 = b2 = 0;      int b_u1 = B_U_tab[ u_ptr[uv_stride] ];                                             \
380            int g_uv1 = G_U_tab[ u_ptr[uv_stride] ] + G_V_tab[ v_ptr[uv_stride] ];  \
381                  // process one 2x2 block per iteration          int r_v1 = R_V_tab[ v_ptr[uv_stride] ];                                         \
382                  for (x = 0; x < (uint32_t) width / 2; x++) {      WRITE_RGB16(0, 0, C1)                                                                               \
383                          int u, v;          WRITE_RGB16(1, 1, C1)                                                                           \
384                          int b_u, g_uv, r_v, rgb_y;      WRITE_RGB16(2, 0, C1)                                                                               \
385            WRITE_RGB16(3, 1, C1)                                                                           \
386                          u = u_src[x];  
387                          v = v_src[x];  
388    /* rgb/rgbi output */
389                          b_u = B_U_tab[u];  
390                          g_uv = G_U_tab[u] + G_V_tab[v];  #define WRITE_RGB(SIZE,ROW,UV_ROW,C1,C2,C3,C4)  \
391                          r_v = R_V_tab[v];          rgb_y = RGB_Y_tab[ y_ptr[(ROW)*y_stride + 0] ];                                         \
392            x_ptr[(ROW)*x_stride+(C3)] = MAX(0, MIN(255, (rgb_y + b_u##UV_ROW) >> SCALEBITS_OUT));  \
393                          rgb_y = RGB_Y_tab[*y_src];          x_ptr[(ROW)*x_stride+(C2)] = MAX(0, MIN(255, (rgb_y - g_uv##UV_ROW) >> SCALEBITS_OUT)); \
394                          b = (b & 0x7) + ((rgb_y + b_u) >> SCALEBITS_OUT);          x_ptr[(ROW)*x_stride+(C1)] = MAX(0, MIN(255, (rgb_y + r_v##UV_ROW) >> SCALEBITS_OUT));  \
395                          g = (g & 0x7) + ((rgb_y - g_uv) >> SCALEBITS_OUT);          if ((SIZE)>3) x_ptr[(ROW)*x_stride+(C4)] = 0;                                                                   \
396                          r = (r & 0x7) + ((rgb_y + r_v) >> SCALEBITS_OUT);          rgb_y = RGB_Y_tab[ y_ptr[(ROW)*y_stride + 1] ];                                                                 \
397                          *(uint16_t *) dst = MK_RGB565(r, g, b);          x_ptr[(ROW)*x_stride+(SIZE)+(C3)] = MAX(0, MIN(255, (rgb_y + b_u##UV_ROW) >> SCALEBITS_OUT));   \
398            x_ptr[(ROW)*x_stride+(SIZE)+(C2)] = MAX(0, MIN(255, (rgb_y - g_uv##UV_ROW) >> SCALEBITS_OUT));  \
399                          y_src++;          x_ptr[(ROW)*x_stride+(SIZE)+(C1)] = MAX(0, MIN(255, (rgb_y + r_v##UV_ROW) >> SCALEBITS_OUT));   \
400                          rgb_y = RGB_Y_tab[*y_src];          if ((SIZE)>3) x_ptr[(ROW)*x_stride+(SIZE)+(C4)] = 0;
401                          b = (b & 0x7) + ((rgb_y + b_u) >> SCALEBITS_OUT);  
402                          g = (g & 0x7) + ((rgb_y - g_uv) >> SCALEBITS_OUT);  
403                          r = (r & 0x7) + ((rgb_y + r_v) >> SCALEBITS_OUT);  #define YV12_TO_RGB_ROW(SIZE,C1,C2,C3,C4)       /* nothing */
404                          *(uint16_t *) (dst + 2) = MK_RGB565(r, g, b);  #define YV12_TO_RGB(SIZE,C1,C2,C3,C4)                           \
405                          y_src++;          int rgb_y;                                                                                              \
406            int b_u0 = B_U_tab[ u_ptr[0] ];                                                 \
407                          rgb_y = RGB_Y_tab[*y_src2];          int g_uv0 = G_U_tab[ u_ptr[0] ] + G_V_tab[ v_ptr[0] ];  \
408                          b2 = (b2 & 0x7) + ((rgb_y + b_u) >> SCALEBITS_OUT);          int r_v0 = R_V_tab[ v_ptr[0] ];                                                 \
409                          g2 = (g2 & 0x7) + ((rgb_y - g_uv) >> SCALEBITS_OUT);          WRITE_RGB(SIZE, 0, 0, C1,C2,C3,C4)                                              \
410                          r2 = (r2 & 0x7) + ((rgb_y + r_v) >> SCALEBITS_OUT);          WRITE_RGB(SIZE, 1, 0, C1,C2,C3,C4)
411                          *(uint16_t *) (dst2) = MK_RGB565(r2, g2, b2);  
412                          y_src2++;  #define YV12_TO_RGBI_ROW(SIZE,C1,C2,C3,C4)      /* nothing */
413    #define YV12_TO_RGBI(SIZE,C1,C2,C3,C4)                          \
414                          rgb_y = RGB_Y_tab[*y_src2];          int rgb_y;                                                                                              \
415                          b2 = (b2 & 0x7) + ((rgb_y + b_u) >> SCALEBITS_OUT);          int b_u0 = B_U_tab[ u_ptr[0] ];                                                 \
416                          g2 = (g2 & 0x7) + ((rgb_y - g_uv) >> SCALEBITS_OUT);          int g_uv0 = G_U_tab[ u_ptr[0] ] + G_V_tab[ v_ptr[0] ];  \
417                          r2 = (r2 & 0x7) + ((rgb_y + r_v) >> SCALEBITS_OUT);          int r_v0 = R_V_tab[ v_ptr[0] ];                                                 \
418                          *(uint16_t *) (dst2 + 2) = MK_RGB565(r2, g2, b2);      int b_u1 = B_U_tab[ u_ptr[uv_stride] ];                                     \
419                          y_src2++;          int g_uv1 = G_U_tab[ u_ptr[uv_stride] ] + G_V_tab[ v_ptr[uv_stride] ];  \
420            int r_v1 = R_V_tab[ v_ptr[uv_stride] ];                                 \
421                          dst += 4;          WRITE_RGB(SIZE, 0, 0, C1,C2,C3,C4)              \
422                          dst2 += 4;          WRITE_RGB(SIZE, 1, 1, C1,C2,C3,C4)              \
423                  }          WRITE_RGB(SIZE, 2, 0, C1,C2,C3,C4)              \
424            WRITE_RGB(SIZE, 3, 1, C1,C2,C3,C4)
425                  dst += dst_dif;  
426                  dst2 += dst_dif;  
427    /* yuyv/yuyvi output */
428                  y_src += y_dif;  
429                  y_src2 += y_dif;  #define WRITE_YUYV(ROW,UV_ROW,C1,C2,C3,C4)      \
430            x_ptr[(ROW)*x_stride+(C1)] = y_ptr[   (ROW)*y_stride +0];       \
431                  u_src += uv_stride;          x_ptr[(ROW)*x_stride+(C2)] = u_ptr[(UV_ROW)*uv_stride+0];       \
432                  v_src += uv_stride;          x_ptr[(ROW)*x_stride+(C3)] = y_ptr[   (ROW)*y_stride +1];       \
433          }          x_ptr[(ROW)*x_stride+(C4)] = v_ptr[(UV_ROW)*uv_stride+0];       \
434  }  
435    #define YV12_TO_YUYV_ROW(SIZE,C1,C2,C3,C4)      /* nothing */
436    #define YV12_TO_YUYV(SIZE,C1,C2,C3,C4)  \
437  #define MAKE_YV12_TO_RGB_C(NAME,SIZE,C1,C2,C3,C4) \          WRITE_YUYV(0, 0, C1,C2,C3,C4)           \
438  void yv12_to_##NAME##_c(uint8_t * dst,          \          WRITE_YUYV(1, 0, C1,C2,C3,C4)
439                                  int dst_stride,         \  
440                                  uint8_t * y_src,        \  #define YV12_TO_YUYVI_ROW(SIZE,C1,C2,C3,C4) /* nothing */
441                                  uint8_t * u_src,        \  #define YV12_TO_YUYVI(SIZE,C1,C2,C3,C4) \
442                                  uint8_t * v_src,        \          WRITE_YUYV(0, 0, C1,C2,C3,C4)           \
443                                  int y_stride,           \          WRITE_YUYV(1, 1, C1,C2,C3,C4)           \
444                                  int uv_stride,          \          WRITE_YUYV(2, 0, C1,C2,C3,C4)           \
445                                  int width,                      \          WRITE_YUYV(3, 1, C1,C2,C3,C4)
446                                  int height)                     \  
447  {       \  
448          const uint8_t a = 0;            /* alpha = 0 */         \  MAKE_COLORSPACE(yv12_to_rgb555_c,  2,2,2, YV12_TO_RGB16,  MK_RGB555, 0,0,0)
449          const uint32_t dst_dif = 2 * dst_stride - (SIZE) * width;       \  MAKE_COLORSPACE(yv12_to_rgb565_c,  2,2,2, YV12_TO_RGB16,  MK_RGB565, 0,0,0)
450          int32_t y_dif = 2 * y_stride - width;   \  MAKE_COLORSPACE(yv12_to_bgr_c,     3,2,2, YV12_TO_RGB,    2,1,0, 0)
451          uint8_t *dst2 = dst + dst_stride;       \  MAKE_COLORSPACE(yv12_to_bgra_c,    4,2,2, YV12_TO_RGB,    2,1,0,3)
452          uint8_t *y_src2 = y_src + y_stride;     \  MAKE_COLORSPACE(yv12_to_abgr_c,    4,2,2, YV12_TO_RGB,    3,2,1,0)
453          uint32_t x, y;  \  MAKE_COLORSPACE(yv12_to_rgba_c,    4,2,2, YV12_TO_RGB,    0,1,2,3)
454  \  MAKE_COLORSPACE(yv12_to_yuyv_c,    2,2,2, YV12_TO_YUYV,   0,1,2,3)
455          if (height < 0) {                       /* flip image? */       \  MAKE_COLORSPACE(yv12_to_uyvy_c,    2,2,2, YV12_TO_YUYV,   1,0,3,2)
456                  height = -height;                                                       \  
457                  y_src += (height - 1) * y_stride;                       \  MAKE_COLORSPACE(yv12_to_rgb555i_c, 2,2,4, YV12_TO_RGB16I, MK_RGB555, 0,0,0)
458                  y_src2 = y_src - y_stride;                                      \  MAKE_COLORSPACE(yv12_to_rgb565i_c, 2,2,4, YV12_TO_RGB16I, MK_RGB565, 0,0,0)
459                  u_src += (height / 2 - 1) * uv_stride;          \  MAKE_COLORSPACE(yv12_to_bgri_c,    3,2,4, YV12_TO_RGBI,   2,1,0, 0)
460                  v_src += (height / 2 - 1) * uv_stride;          \  MAKE_COLORSPACE(yv12_to_bgrai_c,   4,2,4, YV12_TO_RGBI,   2,1,0,3)
461                  y_dif = -width - 2 * y_stride;                          \  MAKE_COLORSPACE(yv12_to_abgri_c,   4,2,4, YV12_TO_RGBI,   3,2,1,0)
462                  uv_stride = -uv_stride;                                         \  MAKE_COLORSPACE(yv12_to_rgbai_c,   4,2,4, YV12_TO_RGBI,   0,1,2,3)
463          }       \  MAKE_COLORSPACE(yv12_to_yuyvi_c,   2,2,4, YV12_TO_YUYVI,  0,1,2,3)
464          /* process one 2x2 block per iteration */               \  MAKE_COLORSPACE(yv12_to_uyvyi_c,   2,2,4, YV12_TO_YUYVI,  1,0,3,2)
465          for (y = height/2; y; y--) {                                    \  
466                                                                                                          \  
467                  for (x = 0; x < (uint32_t)width/2; x++) {       \  
468                          int u, v;                                                               \  /* yv12 to yv12 copy function */
469                          int b_u, g_uv, r_v, rgb_y;                              \  
470                          int r, g, b;                                                    \  void
471  \  yv12_to_yv12_c(uint8_t * y_dst, uint8_t * u_dst, uint8_t * v_dst,
472                          u = u_src[x];                                                   \                                  int y_dst_stride, int uv_dst_stride,
473                          v = v_src[x];                                                   \                                  uint8_t * y_src, uint8_t * u_src, uint8_t * v_src,
474                          b_u = B_U_tab[u];                                               \                                  int y_src_stride, int uv_src_stride,
475                          g_uv = G_U_tab[u] + G_V_tab[v];                 \                                  int width, int height, int vflip)
476                          r_v = R_V_tab[v];                                               \  {
477  \          int width2 = width / 2;
478                          rgb_y = RGB_Y_tab[*y_src];                              \          int height2 = height / 2;
479                          b = MAX(0, MIN(255, (rgb_y + b_u) >> SCALEBITS_OUT));   \          int y;
480                          g = MAX(0, MIN(255, (rgb_y - g_uv) >> SCALEBITS_OUT));  \  
481                          r = MAX(0, MIN(255, (rgb_y + r_v) >> SCALEBITS_OUT));   \          if (vflip) {
482                          dst[0] = (C1);                                                  \                  y_src += (height - 1) * y_src_stride;
483                          dst[1] = (C2);                                                  \                  u_src += (height2 - 1) * uv_src_stride;
484                          dst[2] = (C3);                                                  \                  v_src += (height2 - 1) * uv_src_stride;
485                          if ((SIZE)>3) dst[3] = (C4);                    \                  y_src_stride = -y_src_stride;
486                          y_src++;                                                                \                  uv_src_stride = -uv_src_stride;
 \  
                         rgb_y = RGB_Y_tab[*y_src];                              \  
                         b = MAX(0, MIN(255, (rgb_y + b_u) >> SCALEBITS_OUT));   \  
                         g = MAX(0, MIN(255, (rgb_y - g_uv) >> SCALEBITS_OUT));  \  
                         r = MAX(0, MIN(255, (rgb_y + r_v) >> SCALEBITS_OUT));   \  
                         dst[(SIZE)+0] = (C1);                                                   \  
                         dst[(SIZE)+1] = (C2);                                                   \  
                         dst[(SIZE)+2] = (C3);                                                   \  
                         if ((SIZE)>3) dst[(SIZE)+3] = (C4);                     \  
                         y_src++;                                                                \  
 \  
                         rgb_y = RGB_Y_tab[*y_src2];                             \  
                         b = MAX(0, MIN(255, (rgb_y + b_u) >> SCALEBITS_OUT));   \  
                         g = MAX(0, MIN(255, (rgb_y - g_uv) >> SCALEBITS_OUT));  \  
                         r = MAX(0, MIN(255, (rgb_y + r_v) >> SCALEBITS_OUT));   \  
                         dst2[0] = (C1);                                                 \  
                         dst2[1] = (C2);                                                 \  
                         dst2[2] = (C3);                                                 \  
                         if ((SIZE)>3) dst2[3] = (C4);                   \  
                         y_src2++;                                                               \  
 \  
                         rgb_y = RGB_Y_tab[*y_src2];                             \  
                         b = MAX(0, MIN(255, (rgb_y + b_u) >> SCALEBITS_OUT));   \  
                         g = MAX(0, MIN(255, (rgb_y - g_uv) >> SCALEBITS_OUT));  \  
                         r = MAX(0, MIN(255, (rgb_y + r_v) >> SCALEBITS_OUT));   \  
                         dst2[(SIZE)+0] = (C1);                                                  \  
                         dst2[(SIZE)+1] = (C2);                                                  \  
                         dst2[(SIZE)+2] = (C3);                                                  \  
                         if ((SIZE)>3) dst2[(SIZE)+3] = (C4);                    \  
                         y_src2++;                                                               \  
 \  
                         dst += 2*(SIZE);                        \  
                         dst2 += 2*(SIZE);                       \  
                 }                                               \  
                 dst += dst_dif;                                 \  
                 dst2 += dst_dif;                                \  
                 y_src += y_dif;                                 \  
                 y_src2 += y_dif;                                \  
                 u_src += uv_stride;                             \  
                 v_src += uv_stride;                             \  
         }               \  
 }  
   
 MAKE_YV12_TO_RGB_C(rgb24,3, b,g,r,0)            /* yuv420 -> bgr */  
 MAKE_YV12_TO_RGB_C(rgb32,4, b,g,r,a)            /* yuv420 -> bgra */  
 MAKE_YV12_TO_RGB_C(abgr, 4, a,b,g,r)            /* yuv420 -> abgr */  
 MAKE_YV12_TO_RGB_C(rgba, 4, r,g,b,a)            /* yuv420 -> rgba */  
   
   
   
 /*      yuv 4:2:0 planar -> yuv planar */  
   
 void  
 yv12_to_yuv_c(uint8_t * dst,  
                           int dst_stride,  
                           uint8_t * y_src,  
                           uint8_t * u_src,  
                           uint8_t * v_src,  
                           int y_stride,  
                           int uv_stride,  
                           int width,  
                           int height)  
 {  
         uint32_t dst_stride2 = dst_stride >> 1;  
         uint32_t width2 = width >> 1;  
         uint32_t y;  
   
         if (height < 0) {  
                 height = -height;  
                 y_src += (height - 1) * y_stride;  
                 u_src += (height / 2 - 1) * uv_stride;  
                 v_src += (height / 2 - 1) * uv_stride;  
                 y_stride = -y_stride;  
                 uv_stride = -uv_stride;  
487          }          }
488    
489          for (y = height; y; y--) {          for (y = height; y; y--) {
490                  memcpy(dst, y_src, width);                  memcpy(y_dst, y_src, width);
491                  dst += dst_stride;                  y_src += y_src_stride;
492                  y_src += y_stride;                  y_dst += y_dst_stride;
         }  
   
         for (y = height >> 1; y; y--) {  
                 memcpy(dst, u_src, width2);  
                 dst += dst_stride2;  
                 u_src += uv_stride;  
         }  
   
         for (y = height >> 1; y; y--) {  
                 memcpy(dst, v_src, width2);  
                 dst += dst_stride2;  
                 v_src += uv_stride;  
         }  
 }  
   
   
   
   
 /* yuv 4:2:0 planar -> yuyv (yuv2) packed */  
   
 void  
 yv12_to_yuyv_c(uint8_t * dst,  
                            int dst_stride,  
                            uint8_t * y_src,  
                            uint8_t * u_src,  
                            uint8_t * v_src,  
                            int y_stride,  
                            int uv_stride,  
                            int width,  
                            int height)  
 {  
         const uint32_t dst_dif = dst_stride - (2 * width);  
         uint32_t x, y;  
   
         if (height < 0) {  
                 height = -height;  
                 y_src += (height - 1) * y_stride;  
                 u_src += (height / 2 - 1) * uv_stride;  
                 v_src += (height / 2 - 1) * uv_stride;  
                 y_stride = -y_stride;  
                 uv_stride = -uv_stride;  
         }  
   
         for (y = 0; y < (uint32_t) height; y++) {  
                 for (x = 0; x < (uint32_t) width / 2; x++) {  
                         dst[0] = y_src[2 * x];  
                         dst[1] = u_src[x];  
                         dst[2] = y_src[2 * x + 1];  
                         dst[3] = v_src[x];  
                         dst += 4;  
                 }  
                 dst += dst_dif;  
                 y_src += y_stride;  
                 if (y & 1) {  
                         u_src += uv_stride;  
                         v_src += uv_stride;  
493                  }                  }
         }  
 }  
   
   
   
 /* yuv 4:2:0 planar -> uyvy packed */  
   
 void  
 yv12_to_uyvy_c(uint8_t * dst,  
                            int dst_stride,  
                            uint8_t * y_src,  
                            uint8_t * u_src,  
                            uint8_t * v_src,  
                            int y_stride,  
                            int uv_stride,  
                            int width,  
                            int height)  
 {  
         const uint32_t dst_dif = dst_stride - (2 * width);  
         uint32_t x, y;  
494    
495          if (height < 0) {          for (y = height2; y; y--) {
496                  height = -height;                  memcpy(u_dst, u_src, width2);
497                  y_src += (height - 1) * y_stride;                  u_src += uv_src_stride;
498                  u_src += (height / 2 - 1) * uv_stride;                  u_dst += uv_dst_stride;
                 v_src += (height / 2 - 1) * uv_stride;  
                 y_stride = -y_stride;  
                 uv_stride = -uv_stride;  
499          }          }
500    
501          for (y = 0; y < (uint32_t) height; y++) {          for (y = height2; y; y--) {
502                  for (x = 0; x < (uint32_t) width / 2; x++) {                  memcpy(v_dst, v_src, width2);
503                          dst[0] = u_src[x];                  v_src += uv_src_stride;
504                          dst[1] = y_src[2 * x];                  v_dst += uv_dst_stride;
                         dst[2] = v_src[x];  
                         dst[3] = y_src[2 * x + 1];  
                         dst += 4;  
                 }  
                 dst += dst_dif;  
                 y_src += y_stride;  
                 if (y & 1) {  
                         u_src += uv_stride;  
                         v_src += uv_stride;  
                 }  
505          }          }
506  }  }
507    
508    
 /*      user yuv planar -> yuv 4:2:0 planar  
509    
510          NOTE: does not flip */  /* initialize rgb lookup tables */
511    
512  void  void
513  user_to_yuv_c(uint8_t * y_out,  colorspace_init(void)
                           uint8_t * u_out,  
                           uint8_t * v_out,  
                           int stride,  
                           DEC_PICTURE * picture,  
                           int width,  
                           int height)  
514  {  {
515          uint32_t stride2 = stride >> 1;          int32_t i;
         uint32_t width2 = width >> 1;  
         uint32_t y;  
         uint8_t *src;  
   
         src = picture->y;  
         for (y = height; y; y--) {  
                 memcpy(y_out, src, width);  
                 src += picture->stride_y;  
                 y_out += stride;  
         }  
   
         src = picture->u;  
         for (y = height >> 1; y; y--) {  
                 memcpy(u_out, src, width2);  
                 src += picture->stride_uv;  
                 u_out += stride2;  
         }  
516    
517          src = picture->v;          for (i = 0; i < 256; i++) {
518          for (y = height >> 1; y; y--) {                  RGB_Y_tab[i] = FIX_OUT(RGB_Y_OUT) * (i - Y_ADD_OUT);
519                  memcpy(v_out, src, width2);                  B_U_tab[i] = FIX_OUT(B_U_OUT) * (i - U_ADD_OUT);
520                  src += picture->stride_uv;                  G_U_tab[i] = FIX_OUT(G_U_OUT) * (i - U_ADD_OUT);
521                  v_out += stride2;                  G_V_tab[i] = FIX_OUT(G_V_OUT) * (i - V_ADD_OUT);
522                    R_V_tab[i] = FIX_OUT(R_V_OUT) * (i - V_ADD_OUT);
523          }          }
524  }  }

Legend:
Removed from v.630  
changed lines
  Added in v.631

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4