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