1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "bufferCopy.h"
18 
19 
20 namespace android {
21 namespace hardware {
22 namespace automotive {
23 namespace evs {
24 namespace V1_1 {
25 namespace implementation {
26 
27 
28 // Round up to the nearest multiple of the given alignment value
29 template<unsigned alignment>
align(int value)30 int align(int value) {
31     static_assert((alignment && !(alignment & (alignment - 1))),
32                   "alignment must be a power of 2");
33 
34     unsigned mask = alignment - 1;
35     return (value + mask) & ~mask;
36 }
37 
38 
39 // Limit the given value to the provided range.  :)
clamp(float v,float min,float max)40 static inline float clamp(float v, float min, float max) {
41     if (v < min) return min;
42     if (v > max) return max;
43     return v;
44 }
45 
46 
yuvToRgbx(const unsigned char Y,const unsigned char Uin,const unsigned char Vin)47 static uint32_t yuvToRgbx(const unsigned char Y, const unsigned char Uin, const unsigned char Vin) {
48     // Don't use this if you want to see the best performance.  :)
49     // Better to do this in a pixel shader if we really have to, but on actual
50     // embedded hardware we expect to be able to texture directly from the YUV data
51     float U = Uin - 128.0f;
52     float V = Vin - 128.0f;
53 
54     float Rf = Y + 1.140f*V;
55     float Gf = Y - 0.395f*U - 0.581f*V;
56     float Bf = Y + 2.032f*U;
57     unsigned char R = (unsigned char)clamp(Rf, 0.0f, 255.0f);
58     unsigned char G = (unsigned char)clamp(Gf, 0.0f, 255.0f);
59     unsigned char B = (unsigned char)clamp(Bf, 0.0f, 255.0f);
60 
61     return ((R & 0xFF))       |
62            ((G & 0xFF) << 8)  |
63            ((B & 0xFF) << 16) |
64            0xFF000000;  // Fill the alpha channel with ones
65 }
66 
67 
fillNV21FromNV21(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,unsigned)68 void fillNV21FromNV21(const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, unsigned) {
69     // The NV21 format provides a Y array of 8bit values, followed by a 1/2 x 1/2 interleave U/V array.
70     // It assumes an even width and height for the overall image, and a horizontal stride that is
71     // an even multiple of 16 bytes for both the Y and UV arrays.
72 
73     // Target  and source image layout properties (They match since the formats match!)
74     const AHardwareBuffer_Desc* pDesc =
75         reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
76     const unsigned strideLum = align<16>(pDesc->width);
77     const unsigned sizeY = strideLum * pDesc->height;
78     const unsigned strideColor = strideLum;   // 1/2 the samples, but two interleaved channels
79     const unsigned sizeColor = strideColor * pDesc->height/2;
80     const unsigned totalBytes = sizeY + sizeColor;
81 
82     // Simply copy the data byte for byte
83     memcpy(tgt, imgData, totalBytes);
84 }
85 
86 
fillNV21FromYUYV(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,unsigned imgStride)87 void fillNV21FromYUYV(const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, unsigned imgStride) {
88     // The YUYV format provides an interleaved array of pixel values with U and V subsampled in
89     // the horizontal direction only.  Also known as interleaved 422 format.  A 4 byte
90     // "macro pixel" provides the Y value for two adjacent pixels and the U and V values shared
91     // between those two pixels.  The width of the image must be an even number.
92     // We need to down sample the UV values and collect them together after all the packed Y values
93     // to construct the NV21 format.
94     // NV21 requires even width and height, so we assume that is the case for the incomming image
95     // as well.
96     uint32_t *srcDataYUYV = (uint32_t*)imgData;
97     struct YUYVpixel {
98         uint8_t Y1;
99         uint8_t U;
100         uint8_t Y2;
101         uint8_t V;
102     };
103 
104     // Target image layout properties
105     const AHardwareBuffer_Desc* pDesc =
106         reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
107     const unsigned strideLum = align<16>(pDesc->width);
108     const unsigned sizeY = strideLum * pDesc->height;
109     const unsigned strideColor = strideLum;   // 1/2 the samples, but two interleaved channels
110 
111     // Source image layout properties
112     const unsigned srcRowPixels = imgStride/4;  // imgStride is in units of bytes
113     const unsigned srcRowDoubleStep = srcRowPixels * 2;
114     uint32_t* topSrcRow =  srcDataYUYV;
115     uint32_t* botSrcRow =  srcDataYUYV + srcRowPixels;
116 
117     // We're going to work on one 2x2 cell in the output image at at time
118     for (unsigned cellRow = 0; cellRow < pDesc->height/2; cellRow++) {
119 
120         // Set up the output pointers
121         uint8_t* yTopRow = tgt + (cellRow*2) * strideLum;
122         uint8_t* yBotRow = yTopRow + strideLum;
123         uint8_t* uvRow   = (tgt + sizeY) + cellRow * strideColor;
124 
125         for (unsigned cellCol = 0; cellCol < pDesc->width/2; cellCol++) {
126             // Collect the values from the YUYV interleaved data
127             const YUYVpixel* pTopMacroPixel = (YUYVpixel*)&topSrcRow[cellCol];
128             const YUYVpixel* pBotMacroPixel = (YUYVpixel*)&botSrcRow[cellCol];
129 
130             // Down sample the U/V values by linear average between rows
131             const uint8_t uValue = (pTopMacroPixel->U + pBotMacroPixel->U) >> 1;
132             const uint8_t vValue = (pTopMacroPixel->V + pBotMacroPixel->V) >> 1;
133 
134             // Store the values into the NV21 layout
135             yTopRow[cellCol*2]   = pTopMacroPixel->Y1;
136             yTopRow[cellCol*2+1] = pTopMacroPixel->Y2;
137             yBotRow[cellCol*2]   = pBotMacroPixel->Y1;
138             yBotRow[cellCol*2+1] = pBotMacroPixel->Y2;
139             uvRow[cellCol*2]     = uValue;
140             uvRow[cellCol*2+1]   = vValue;
141         }
142 
143         // Skipping two rows to get to the next set of two source rows
144         topSrcRow += srcRowDoubleStep;
145         botSrcRow += srcRowDoubleStep;
146     }
147 }
148 
149 
fillRGBAFromYUYV(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,unsigned imgStride)150 void fillRGBAFromYUYV(const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, unsigned imgStride) {
151     const AHardwareBuffer_Desc* pDesc =
152         reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
153     unsigned width = pDesc->width;
154     unsigned height = pDesc->height;
155     uint32_t* src = (uint32_t*)imgData;
156     uint32_t* dst = (uint32_t*)tgt;
157     unsigned srcStridePixels = imgStride / 2;
158     unsigned dstStridePixels = pDesc->stride;
159 
160     const int srcRowPadding32 = srcStridePixels/2 - width/2;  // 2 bytes per pixel, 4 bytes per word
161     const int dstRowPadding32 = dstStridePixels   - width;    // 4 bytes per pixel, 4 bytes per word
162 
163     for (unsigned r=0; r<height; r++) {
164         for (unsigned c=0; c<width/2; c++) {
165             // Note:  we're walking two pixels at a time here (even/odd)
166             uint32_t srcPixel = *src++;
167 
168             uint8_t Y1 = (srcPixel)       & 0xFF;
169             uint8_t U  = (srcPixel >> 8)  & 0xFF;
170             uint8_t Y2 = (srcPixel >> 16) & 0xFF;
171             uint8_t V  = (srcPixel >> 24) & 0xFF;
172 
173             // On the RGB output, we're writing one pixel at a time
174             *(dst+0) = yuvToRgbx(Y1, U, V);
175             *(dst+1) = yuvToRgbx(Y2, U, V);
176             dst += 2;
177         }
178 
179         // Skip over any extra data or end of row alignment padding
180         src += srcRowPadding32;
181         dst += dstRowPadding32;
182     }
183 }
184 
185 
fillYUYVFromYUYV(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,unsigned imgStride)186 void fillYUYVFromYUYV(const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, unsigned imgStride) {
187     const AHardwareBuffer_Desc* pDesc =
188         reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
189     unsigned width = pDesc->width;
190     unsigned height = pDesc->height;
191     uint8_t* src = (uint8_t*)imgData;
192     uint8_t* dst = (uint8_t*)tgt;
193     unsigned srcStrideBytes = imgStride;
194     unsigned dstStrideBytes = pDesc->stride * 2;
195 
196     for (unsigned r=0; r<height; r++) {
197         // Copy a pixel row at a time (2 bytes per pixel, averaged over a YUYV macro pixel)
198         memcpy(dst+r*dstStrideBytes, src+r*srcStrideBytes, width*2);
199     }
200 }
201 
202 
fillYUYVFromUYVY(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,unsigned imgStride)203 void fillYUYVFromUYVY(const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, unsigned imgStride) {
204     const AHardwareBuffer_Desc* pDesc =
205         reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
206     unsigned width = pDesc->width;
207     unsigned height = pDesc->height;
208     uint32_t* src = (uint32_t*)imgData;
209     uint32_t* dst = (uint32_t*)tgt;
210     unsigned srcStridePixels = imgStride / 2;
211     unsigned dstStridePixels = pDesc->stride;
212 
213     const int srcRowPadding32 = srcStridePixels/2 - width/2;  // 2 bytes per pixel, 4 bytes per word
214     const int dstRowPadding32 = dstStridePixels/2 - width/2;  // 2 bytes per pixel, 4 bytes per word
215 
216     for (unsigned r=0; r<height; r++) {
217         for (unsigned c=0; c<width/2; c++) {
218             // Note:  we're walking two pixels at a time here (even/odd)
219             uint32_t srcPixel = *src++;
220 
221             uint8_t Y1 = (srcPixel)       & 0xFF;
222             uint8_t U  = (srcPixel >> 8)  & 0xFF;
223             uint8_t Y2 = (srcPixel >> 16) & 0xFF;
224             uint8_t V  = (srcPixel >> 24) & 0xFF;
225 
226             // Now we write back the pair of pixels with the components swizzled
227             *dst++ = (U)        |
228                      (Y1 << 8)  |
229                      (V  << 16) |
230                      (Y2 << 24);
231         }
232 
233         // Skip over any extra data or end of row alignment padding
234         src += srcRowPadding32;
235         dst += dstRowPadding32;
236     }
237 }
238 
239 
240 } // namespace implementation
241 } // namespace V1_1
242 } // namespace evs
243 } // namespace automotive
244 } // namespace hardware
245 } // namespace android
246