M4VIFI_RGB888toYUV420.c revision 7c9d8018755adf1857571125ba1b3598c96ea506
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#include    "M4VIFI_FiltersAPI.h"
18
19#include    "M4VIFI_Defines.h"
20
21#include    "M4VIFI_Clip.h"
22
23/***************************************************************************
24Proto:
25M4VIFI_UInt8    M4VIFI_RGB888toYUV420(void *pUserData, M4VIFI_ImagePlane *PlaneIn,
26                                     M4VIFI_ImagePlane PlaneOut[3]);
27Purpose:    filling of the YUV420 plane from a BGR24 plane
28Abstract:    Loop on each row ( 2 rows by 2 rows )
29                Loop on each column ( 2 col by 2 col )
30                    Get 4 BGR samples from input data and build 4 output Y samples and
31                    each single U & V data
32                end loop on col
33            end loop on row
34
35In:            RGB24 plane
36InOut:        none
37Out:        array of 3 M4VIFI_ImagePlane structures
38Modified:    ML: RGB function modified to BGR.
39***************************************************************************/
40M4VIFI_UInt8 M4VIFI_RGB888toYUV420(void *pUserData, M4VIFI_ImagePlane *PlaneIn,
41                                    M4VIFI_ImagePlane PlaneOut[3])
42{
43    M4VIFI_UInt32    u32_width, u32_height;
44    M4VIFI_UInt32    u32_stride_Y, u32_stride2_Y, u32_stride_U, u32_stride_V, u32_stride_rgb,\
45                     u32_stride_2rgb;
46    M4VIFI_UInt32    u32_col, u32_row;
47
48    M4VIFI_Int32    i32_r00, i32_r01, i32_r10, i32_r11;
49    M4VIFI_Int32    i32_g00, i32_g01, i32_g10, i32_g11;
50    M4VIFI_Int32    i32_b00, i32_b01, i32_b10, i32_b11;
51    M4VIFI_Int32    i32_y00, i32_y01, i32_y10, i32_y11;
52    M4VIFI_Int32    i32_u00, i32_u01, i32_u10, i32_u11;
53    M4VIFI_Int32    i32_v00, i32_v01, i32_v10, i32_v11;
54    M4VIFI_UInt8    *pu8_yn, *pu8_ys, *pu8_u, *pu8_v;
55    M4VIFI_UInt8    *pu8_y_data, *pu8_u_data, *pu8_v_data;
56    M4VIFI_UInt8    *pu8_rgbn_data, *pu8_rgbn;
57
58    /* check sizes */
59    if( (PlaneIn->u_height != PlaneOut[0].u_height)            ||
60        (PlaneOut[0].u_height != (PlaneOut[1].u_height<<1))    ||
61        (PlaneOut[0].u_height != (PlaneOut[2].u_height<<1)))
62        return M4VIFI_ILLEGAL_FRAME_HEIGHT;
63
64    if( (PlaneIn->u_width != PlaneOut[0].u_width)        ||
65        (PlaneOut[0].u_width != (PlaneOut[1].u_width<<1))    ||
66        (PlaneOut[0].u_width != (PlaneOut[2].u_width<<1)))
67        return M4VIFI_ILLEGAL_FRAME_WIDTH;
68
69
70    /* set the pointer to the beginning of the output data buffers */
71    pu8_y_data    = PlaneOut[0].pac_data + PlaneOut[0].u_topleft;
72    pu8_u_data    = PlaneOut[1].pac_data + PlaneOut[1].u_topleft;
73    pu8_v_data    = PlaneOut[2].pac_data + PlaneOut[2].u_topleft;
74
75    /* idem for input buffer */
76    pu8_rgbn_data    = PlaneIn->pac_data + PlaneIn->u_topleft;
77
78    /* get the size of the output image */
79    u32_width    = PlaneOut[0].u_width;
80    u32_height    = PlaneOut[0].u_height;
81
82    /* set the size of the memory jumps corresponding to row jump in each output plane */
83    u32_stride_Y = PlaneOut[0].u_stride;
84    u32_stride2_Y= u32_stride_Y << 1;
85    u32_stride_U = PlaneOut[1].u_stride;
86    u32_stride_V = PlaneOut[2].u_stride;
87
88    /* idem for input plane */
89    u32_stride_rgb = PlaneIn->u_stride;
90    u32_stride_2rgb = u32_stride_rgb << 1;
91
92    /* loop on each row of the output image, input coordinates are estimated from output ones */
93    /* two YUV rows are computed at each pass */
94    for    (u32_row = u32_height ;u32_row != 0; u32_row -=2)
95    {
96        /* update working pointers */
97        pu8_yn    = pu8_y_data;
98        pu8_ys    = pu8_yn + u32_stride_Y;
99
100        pu8_u    = pu8_u_data;
101        pu8_v    = pu8_v_data;
102
103        pu8_rgbn= pu8_rgbn_data;
104
105        /* loop on each column of the output image*/
106        for    (u32_col = u32_width; u32_col != 0 ; u32_col -=2)
107        {
108            /* get RGB samples of 4 pixels */
109            GET_RGB24(i32_r00, i32_g00, i32_b00, pu8_rgbn, 0);
110            GET_RGB24(i32_r10, i32_g10, i32_b10, pu8_rgbn, CST_RGB_24_SIZE);
111            GET_RGB24(i32_r01, i32_g01, i32_b01, pu8_rgbn, u32_stride_rgb);
112            GET_RGB24(i32_r11, i32_g11, i32_b11, pu8_rgbn, u32_stride_rgb + CST_RGB_24_SIZE);
113
114            i32_u00    = U24(i32_r00, i32_g00, i32_b00);
115            i32_v00    = V24(i32_r00, i32_g00, i32_b00);
116            i32_y00    = Y24(i32_r00, i32_g00, i32_b00);        /* matrix luminance */
117            pu8_yn[0]= (M4VIFI_UInt8)i32_y00;
118
119            i32_u10    = U24(i32_r10, i32_g10, i32_b10);
120            i32_v10    = V24(i32_r10, i32_g10, i32_b10);
121            i32_y10    = Y24(i32_r10, i32_g10, i32_b10);
122            pu8_yn[1]= (M4VIFI_UInt8)i32_y10;
123
124            i32_u01    = U24(i32_r01, i32_g01, i32_b01);
125            i32_v01    = V24(i32_r01, i32_g01, i32_b01);
126            i32_y01    = Y24(i32_r01, i32_g01, i32_b01);
127            pu8_ys[0]= (M4VIFI_UInt8)i32_y01;
128
129            i32_u11    = U24(i32_r11, i32_g11, i32_b11);
130            i32_v11    = V24(i32_r11, i32_g11, i32_b11);
131            i32_y11    = Y24(i32_r11, i32_g11, i32_b11);
132            pu8_ys[1] = (M4VIFI_UInt8)i32_y11;
133
134            *pu8_u    = (M4VIFI_UInt8)((i32_u00 + i32_u01 + i32_u10 + i32_u11 + 2) >> 2);
135            *pu8_v    = (M4VIFI_UInt8)((i32_v00 + i32_v01 + i32_v10 + i32_v11 + 2) >> 2);
136
137            pu8_rgbn    +=  (CST_RGB_24_SIZE<<1);
138            pu8_yn        += 2;
139            pu8_ys        += 2;
140
141            pu8_u ++;
142            pu8_v ++;
143        } /* end of horizontal scanning */
144
145        pu8_y_data        += u32_stride2_Y;
146        pu8_u_data        += u32_stride_U;
147        pu8_v_data        += u32_stride_V;
148        pu8_rgbn_data    += u32_stride_2rgb;
149
150
151    } /* End of vertical scanning */
152
153    return M4VIFI_OK;
154}
155