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