M4VIFI_xVSS_RGB565toYUV420.c revision 3b25fdc4a33b53cfcf67315c2d42ad699b8cefe2
1/*
2 * Copyright (C) 2004-2011 NXP Software
3 * Copyright (C) 2011 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17/**
18 ******************************************************************************
19 * @file     M4VIFI_RGB565toYUV420.c
20 * @brief    Contain video library function
21 * @note     Color Conversion Filter
22 *           -# Contains the format conversion filters from RGB565 to YUV420
23 ******************************************************************************
24*/
25
26/* Prototypes of functions, and type definitions */
27#include    "M4VIFI_FiltersAPI.h"
28/* Macro definitions */
29#include    "M4VIFI_Defines.h"
30/* Clip table declaration */
31#include    "M4VIFI_Clip.h"
32
33
34/**
35 ******************************************************************************
36 * M4VIFI_UInt8 M4VIFI_RGB565toYUV420 (void *pUserData,
37 *                                     M4VIFI_ImagePlane *pPlaneIn,
38 *                                   M4VIFI_ImagePlane *pPlaneOut)
39 * @brief   transform RGB565 image to a YUV420 image.
40 * @note    Convert RGB565 to YUV420,
41 *          Loop on each row ( 2 rows by 2 rows )
42 *              Loop on each column ( 2 col by 2 col )
43 *                  Get 4 RGB samples from input data and build 4 output Y samples
44 *                  and each single U & V data
45 *              end loop on col
46 *          end loop on row
47 * @param   pUserData: (IN) User Specific Data
48 * @param   pPlaneIn: (IN) Pointer to RGB565 Plane
49 * @param   pPlaneOut: (OUT) Pointer to  YUV420 buffer Plane
50 * @return  M4VIFI_OK: there is no error
51 * @return  M4VIFI_ILLEGAL_FRAME_HEIGHT: YUV Plane height is ODD
52 * @return  M4VIFI_ILLEGAL_FRAME_WIDTH:  YUV Plane width is ODD
53 ******************************************************************************
54*/
55M4VIFI_UInt8    M4VIFI_xVSS_RGB565toYUV420(void *pUserData, M4VIFI_ImagePlane *pPlaneIn,
56                                                      M4VIFI_ImagePlane *pPlaneOut)
57{
58    M4VIFI_UInt32   u32_width, u32_height;
59    M4VIFI_UInt32   u32_stride_Y, u32_stride2_Y, u32_stride_U, u32_stride_V;
60    M4VIFI_UInt32   u32_stride_rgb, u32_stride_2rgb;
61    M4VIFI_UInt32   u32_col, u32_row;
62
63    M4VIFI_Int32    i32_r00, i32_r01, i32_r10, i32_r11;
64    M4VIFI_Int32    i32_g00, i32_g01, i32_g10, i32_g11;
65    M4VIFI_Int32    i32_b00, i32_b01, i32_b10, i32_b11;
66    M4VIFI_Int32    i32_y00, i32_y01, i32_y10, i32_y11;
67    M4VIFI_Int32    i32_u00, i32_u01, i32_u10, i32_u11;
68    M4VIFI_Int32    i32_v00, i32_v01, i32_v10, i32_v11;
69    M4VIFI_UInt8    *pu8_yn, *pu8_ys, *pu8_u, *pu8_v;
70    M4VIFI_UInt8    *pu8_y_data, *pu8_u_data, *pu8_v_data;
71    M4VIFI_UInt8    *pu8_rgbn_data, *pu8_rgbn;
72    M4VIFI_UInt16   u16_pix1, u16_pix2, u16_pix3, u16_pix4;
73    M4VIFI_UInt8 count_null=0;
74
75    /* Check planes height are appropriate */
76    if( (pPlaneIn->u_height != pPlaneOut[0].u_height)           ||
77        (pPlaneOut[0].u_height != (pPlaneOut[1].u_height<<1))   ||
78        (pPlaneOut[0].u_height != (pPlaneOut[2].u_height<<1)))
79    {
80        return M4VIFI_ILLEGAL_FRAME_HEIGHT;
81    }
82
83    /* Check planes width are appropriate */
84    if( (pPlaneIn->u_width != pPlaneOut[0].u_width)         ||
85        (pPlaneOut[0].u_width != (pPlaneOut[1].u_width<<1)) ||
86        (pPlaneOut[0].u_width != (pPlaneOut[2].u_width<<1)))
87    {
88        return M4VIFI_ILLEGAL_FRAME_WIDTH;
89    }
90
91    /* Set the pointer to the beginning of the output data buffers */
92    pu8_y_data = pPlaneOut[0].pac_data + pPlaneOut[0].u_topleft;
93    pu8_u_data = pPlaneOut[1].pac_data + pPlaneOut[1].u_topleft;
94    pu8_v_data = pPlaneOut[2].pac_data + pPlaneOut[2].u_topleft;
95
96    /* Set the pointer to the beginning of the input data buffers */
97    pu8_rgbn_data   = pPlaneIn->pac_data + pPlaneIn->u_topleft;
98
99    /* Get the size of the output image */
100    u32_width = pPlaneOut[0].u_width;
101    u32_height = pPlaneOut[0].u_height;
102
103    /* Set the size of the memory jumps corresponding to row jump in each output plane */
104    u32_stride_Y = pPlaneOut[0].u_stride;
105    u32_stride2_Y = u32_stride_Y << 1;
106    u32_stride_U = pPlaneOut[1].u_stride;
107    u32_stride_V = pPlaneOut[2].u_stride;
108
109    /* Set the size of the memory jumps corresponding to row jump in input plane */
110    u32_stride_rgb = pPlaneIn->u_stride;
111    u32_stride_2rgb = u32_stride_rgb << 1;
112
113
114    /* Loop on each row of the output image, input coordinates are estimated from output ones */
115    /* Two YUV rows are computed at each pass */
116    for (u32_row = u32_height ;u32_row != 0; u32_row -=2)
117    {
118        /* Current Y plane row pointers */
119        pu8_yn = pu8_y_data;
120        /* Next Y plane row pointers */
121        pu8_ys = pu8_yn + u32_stride_Y;
122        /* Current U plane row pointer */
123        pu8_u = pu8_u_data;
124        /* Current V plane row pointer */
125        pu8_v = pu8_v_data;
126
127        pu8_rgbn = pu8_rgbn_data;
128
129        /* Loop on each column of the output image */
130        for (u32_col = u32_width; u32_col != 0 ; u32_col -=2)
131        {
132            /* Get four RGB 565 samples from input data */
133            u16_pix1 = *( (M4VIFI_UInt16 *) pu8_rgbn);
134            u16_pix2 = *( (M4VIFI_UInt16 *) (pu8_rgbn + CST_RGB_16_SIZE));
135            u16_pix3 = *( (M4VIFI_UInt16 *) (pu8_rgbn + u32_stride_rgb));
136            u16_pix4 = *( (M4VIFI_UInt16 *) (pu8_rgbn + u32_stride_rgb + CST_RGB_16_SIZE));
137
138            /* Unpack RGB565 to 8bit R, G, B */
139            /* (x,y) */
140            GET_RGB565(i32_b00,i32_g00,i32_r00,u16_pix1);
141            /* (x+1,y) */
142            GET_RGB565(i32_b10,i32_g10,i32_r10,u16_pix2);
143            /* (x,y+1) */
144            GET_RGB565(i32_b01,i32_g01,i32_r01,u16_pix3);
145            /* (x+1,y+1) */
146            GET_RGB565(i32_b11,i32_g11,i32_r11,u16_pix4);
147            /* If RGB is transparent color (0, 63, 0), we transform it to white (31,63,31) */
148            if(i32_b00 == 0 && i32_g00 == 63 && i32_r00 == 0)
149            {
150                i32_b00 = 31;
151                i32_r00 = 31;
152            }
153            if(i32_b10 == 0 && i32_g10 == 63 && i32_r10 == 0)
154            {
155                i32_b10 = 31;
156                i32_r10 = 31;
157            }
158            if(i32_b01 == 0 && i32_g01 == 63 && i32_r01 == 0)
159            {
160                i32_b01 = 31;
161                i32_r01 = 31;
162            }
163            if(i32_b11 == 0 && i32_g11 == 63 && i32_r11 == 0)
164            {
165                i32_b11 = 31;
166                i32_r11 = 31;
167            }
168            /* Convert RGB value to YUV */
169            i32_u00 = U16(i32_r00, i32_g00, i32_b00);
170            i32_v00 = V16(i32_r00, i32_g00, i32_b00);
171            /* luminance value */
172            i32_y00 = Y16(i32_r00, i32_g00, i32_b00);
173
174            i32_u10 = U16(i32_r10, i32_g10, i32_b10);
175            i32_v10 = V16(i32_r10, i32_g10, i32_b10);
176            /* luminance value */
177            i32_y10 = Y16(i32_r10, i32_g10, i32_b10);
178
179            i32_u01 = U16(i32_r01, i32_g01, i32_b01);
180            i32_v01 = V16(i32_r01, i32_g01, i32_b01);
181            /* luminance value */
182            i32_y01 = Y16(i32_r01, i32_g01, i32_b01);
183
184            i32_u11 = U16(i32_r11, i32_g11, i32_b11);
185            i32_v11 = V16(i32_r11, i32_g11, i32_b11);
186            /* luminance value */
187            i32_y11 = Y16(i32_r11, i32_g11, i32_b11);
188
189            /* Store luminance data */
190            pu8_yn[0] = (M4VIFI_UInt8)i32_y00;
191            pu8_yn[1] = (M4VIFI_UInt8)i32_y10;
192            pu8_ys[0] = (M4VIFI_UInt8)i32_y01;
193            pu8_ys[1] = (M4VIFI_UInt8)i32_y11;
194            *pu8_u = (M4VIFI_UInt8)((i32_u00 + i32_u01 + i32_u10 + i32_u11 + 2) >> 2);
195            *pu8_v = (M4VIFI_UInt8)((i32_v00 + i32_v01 + i32_v10 + i32_v11 + 2) >> 2);
196            /* Prepare for next column */
197            pu8_rgbn += (CST_RGB_16_SIZE<<1);
198            /* Update current Y plane line pointer*/
199            pu8_yn += 2;
200            /* Update next Y plane line pointer*/
201            pu8_ys += 2;
202            /* Update U plane line pointer*/
203            pu8_u ++;
204            /* Update V plane line pointer*/
205            pu8_v ++;
206        } /* End of horizontal scanning */
207
208        /* Prepare pointers for the next row */
209        pu8_y_data += u32_stride2_Y;
210        pu8_u_data += u32_stride_U;
211        pu8_v_data += u32_stride_V;
212        pu8_rgbn_data += u32_stride_2rgb;
213
214
215    } /* End of vertical scanning */
216
217    return M4VIFI_OK;
218}
219/* End of file M4VIFI_RGB565toYUV420.c */
220
221