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