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 #include <android-base/logging.h>
20 #include <libyuv.h>
21
22 namespace {
23
24 inline constexpr size_t kYuv422BytesPerPixel = 2;
25 inline constexpr size_t kRgbaBytesPerPixel = 4;
26
27 }; // anonymous namespace
28
29
30 namespace android {
31 namespace hardware {
32 namespace automotive {
33 namespace evs {
34 namespace V1_1 {
35 namespace implementation {
36
37
38 // Round up to the nearest multiple of the given alignment value
39 template<unsigned alignment>
align(int value)40 int align(int value) {
41 static_assert((alignment && !(alignment & (alignment - 1))),
42 "alignment must be a power of 2");
43
44 unsigned mask = alignment - 1;
45 return (value + mask) & ~mask;
46 }
47
48
fillNV21FromNV21(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,void *,unsigned)49 void fillNV21FromNV21(
50 const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, void*, unsigned) {
51 // The NV21 format provides a Y array of 8bit values, followed by a 1/2 x 1/2 interleave U/V array.
52 // It assumes an even width and height for the overall image, and a horizontal stride that is
53 // an even multiple of 16 bytes for both the Y and UV arrays.
54
55 // Target and source image layout properties (They match since the formats match!)
56 const AHardwareBuffer_Desc* pDesc =
57 reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
58 const unsigned strideLum = align<16>(pDesc->width);
59 const unsigned sizeY = strideLum * pDesc->height;
60 const unsigned strideColor = strideLum; // 1/2 the samples, but two interleaved channels
61 const unsigned sizeColor = strideColor * pDesc->height/2;
62 const unsigned totalBytes = sizeY + sizeColor;
63
64 // Simply copy the data byte for byte
65 memcpy(tgt, imgData, totalBytes);
66 }
67
68
fillNV21FromYUYV(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,void *,unsigned imgStride)69 void fillNV21FromYUYV(
70 const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, void*, unsigned imgStride) {
71 // The YUYV format provides an interleaved array of pixel values with U and V subsampled in
72 // the horizontal direction only. Also known as interleaved 422 format. A 4 byte
73 // "macro pixel" provides the Y value for two adjacent pixels and the U and V values shared
74 // between those two pixels. The width of the image must be an even number.
75 // We need to down sample the UV values and collect them together after all the packed Y values
76 // to construct the NV21 format.
77 // NV21 requires even width and height, so we assume that is the case for the incomming image
78 // as well.
79 uint32_t *srcDataYUYV = (uint32_t*)imgData;
80 struct YUYVpixel {
81 uint8_t Y1;
82 uint8_t U;
83 uint8_t Y2;
84 uint8_t V;
85 };
86
87 // Target image layout properties
88 const AHardwareBuffer_Desc* pDesc =
89 reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
90 const unsigned strideLum = align<16>(pDesc->width);
91 const unsigned sizeY = strideLum * pDesc->height;
92 const unsigned strideColor = strideLum; // 1/2 the samples, but two interleaved channels
93
94 // Source image layout properties
95 const unsigned srcRowPixels = imgStride/4; // imgStride is in units of bytes
96 const unsigned srcRowDoubleStep = srcRowPixels * 2;
97 uint32_t* topSrcRow = srcDataYUYV;
98 uint32_t* botSrcRow = srcDataYUYV + srcRowPixels;
99
100 // We're going to work on one 2x2 cell in the output image at at time
101 for (unsigned cellRow = 0; cellRow < pDesc->height/2; cellRow++) {
102
103 // Set up the output pointers
104 uint8_t* yTopRow = tgt + (cellRow*2) * strideLum;
105 uint8_t* yBotRow = yTopRow + strideLum;
106 uint8_t* uvRow = (tgt + sizeY) + cellRow * strideColor;
107
108 for (unsigned cellCol = 0; cellCol < pDesc->width/2; cellCol++) {
109 // Collect the values from the YUYV interleaved data
110 const YUYVpixel* pTopMacroPixel = (YUYVpixel*)&topSrcRow[cellCol];
111 const YUYVpixel* pBotMacroPixel = (YUYVpixel*)&botSrcRow[cellCol];
112
113 // Down sample the U/V values by linear average between rows
114 const uint8_t uValue = (pTopMacroPixel->U + pBotMacroPixel->U) >> 1;
115 const uint8_t vValue = (pTopMacroPixel->V + pBotMacroPixel->V) >> 1;
116
117 // Store the values into the NV21 layout
118 yTopRow[cellCol*2] = pTopMacroPixel->Y1;
119 yTopRow[cellCol*2+1] = pTopMacroPixel->Y2;
120 yBotRow[cellCol*2] = pBotMacroPixel->Y1;
121 yBotRow[cellCol*2+1] = pBotMacroPixel->Y2;
122 uvRow[cellCol*2] = uValue;
123 uvRow[cellCol*2+1] = vValue;
124 }
125
126 // Skipping two rows to get to the next set of two source rows
127 topSrcRow += srcRowDoubleStep;
128 botSrcRow += srcRowDoubleStep;
129 }
130 }
131
132
fillRGBAFromYUYV(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,void * buf,unsigned imgStride)133 void fillRGBAFromYUYV(
134 const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, void* buf, unsigned imgStride) {
135 const AHardwareBuffer_Desc* pDesc =
136 reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
137 // Converts YUY2ToARGB (little endian). Please note that libyuv uses the
138 // little endian while we're using the big endian in RGB format names.
139 const auto srcStrideInBytes = imgStride * kYuv422BytesPerPixel;
140 const auto dstStrideInBytes = pDesc->stride * kRgbaBytesPerPixel;
141 auto result = libyuv::YUY2ToARGB((const uint8_t*)imgData,
142 srcStrideInBytes, // input stride in bytes
143 (uint8_t*)buf,
144 dstStrideInBytes, // output stride in bytes
145 pDesc->width,
146 pDesc->height);
147 if (result) {
148 LOG(ERROR) << "Failed to convert YUYV to BGRA.";
149 return;
150 }
151
152 // Swaps R and B pixels to convert BGRA to RGBA
153 result = libyuv::ABGRToARGB((uint8_t*)buf, dstStrideInBytes, tgt, dstStrideInBytes,
154 pDesc->width, pDesc->height);
155 if (result) {
156 LOG(ERROR) << "Failed to convert BGRA to RGBA.";
157 }
158 }
159
160
fillRGBAFromUYVY(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,void * buf,unsigned imgStride)161 void fillRGBAFromUYVY(
162 const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, void* buf, unsigned imgStride) {
163 const AHardwareBuffer_Desc* pDesc =
164 reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
165 // Converts UYVYToARGB (little endian). Please note that libyuv uses the
166 // little endian while we're using the big endian in RGB format names.
167 const auto srcStrideInBytes = imgStride * kYuv422BytesPerPixel;
168 const auto dstStrideInBytes = pDesc->stride * kRgbaBytesPerPixel;
169 auto result = libyuv::UYVYToARGB(static_cast<const uint8_t*>(imgData),
170 srcStrideInBytes, // input stride in bytes
171 static_cast<uint8_t*>(buf),
172 dstStrideInBytes, // output stride in bytes
173 pDesc->width,
174 pDesc->height);
175 if (result) {
176 LOG(ERROR) << "Failed to convert UYVY to BGRA.";
177 return;
178 }
179
180 // Swaps R and B pixels to convert BGRA to RGBA
181 result = libyuv::ABGRToARGB(static_cast<uint8_t*>(buf), dstStrideInBytes, tgt,
182 dstStrideInBytes, pDesc->width, pDesc->height);
183 if (result) {
184 LOG(WARNING) << "Failed to convert BGRA to RGBA.";
185 }
186 }
187
188
fillYUYVFromYUYV(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,void *,unsigned imgStride)189 void fillYUYVFromYUYV(
190 const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, void *, unsigned imgStride) {
191 const AHardwareBuffer_Desc* pDesc =
192 reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
193 const auto height = pDesc->height;
194 uint8_t* src = (uint8_t*)imgData;
195 uint8_t* dst = (uint8_t*)tgt;
196 const auto srcStrideBytes = imgStride * kYuv422BytesPerPixel;
197 const auto dstStrideBytes = pDesc->stride * kYuv422BytesPerPixel;
198
199 for (unsigned r=0; r<height; r++) {
200 // Copy a pixel row at a time (2 bytes per pixel, averaged over a YUYV macro pixel)
201 memcpy(dst+r*dstStrideBytes, src+r*srcStrideBytes, srcStrideBytes);
202 }
203 }
204
205
fillYUYVFromUYVY(const BufferDesc & tgtBuff,uint8_t * tgt,void * imgData,void *,unsigned imgStride)206 void fillYUYVFromUYVY(
207 const BufferDesc& tgtBuff, uint8_t* tgt, void* imgData, void *, unsigned imgStride) {
208 const AHardwareBuffer_Desc* pDesc =
209 reinterpret_cast<const AHardwareBuffer_Desc*>(&tgtBuff.buffer.description);
210 unsigned width = pDesc->width;
211 unsigned height = pDesc->height;
212 uint32_t* src = (uint32_t*)imgData;
213 uint32_t* dst = (uint32_t*)tgt;
214 unsigned srcStridePixels = imgStride;
215 unsigned dstStridePixels = pDesc->stride;
216
217 const int srcRowPadding32 = srcStridePixels/2 - width/2; // 2 bytes per pixel, 4 bytes per word
218 const int dstRowPadding32 = dstStridePixels/2 - width/2; // 2 bytes per pixel, 4 bytes per word
219
220 for (unsigned r=0; r<height; r++) {
221 for (unsigned c=0; c<width/2; c++) {
222 // Note: we're walking two pixels at a time here (even/odd)
223 uint32_t srcPixel = *src++;
224
225 uint8_t Y1 = (srcPixel) & 0xFF;
226 uint8_t U = (srcPixel >> 8) & 0xFF;
227 uint8_t Y2 = (srcPixel >> 16) & 0xFF;
228 uint8_t V = (srcPixel >> 24) & 0xFF;
229
230 // Now we write back the pair of pixels with the components swizzled
231 *dst++ = (U) |
232 (Y1 << 8) |
233 (V << 16) |
234 (Y2 << 24);
235 }
236
237 // Skip over any extra data or end of row alignment padding
238 src += srcRowPadding32;
239 dst += dstRowPadding32;
240 }
241 }
242
243
244 } // namespace implementation
245 } // namespace V1_1
246 } // namespace evs
247 } // namespace automotive
248 } // namespace hardware
249 } // namespace android
250