1 /*
2  * Copyright (C) 2017 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 #define LOG_TAG "VtsHalEvsTest"
18 
19 #include "FormatConvert.h"
20 
21 namespace android {
22 namespace hardware {
23 namespace automotive {
24 namespace evs {
25 namespace common {
26 
27 // Round up to the nearest multiple of the given alignment value
28 template<unsigned alignment>
align(int value)29 int Utils::align(int value) {
30     static_assert((alignment && !(alignment & (alignment - 1))),
31                   "alignment must be a power of 2");
32 
33     unsigned mask = alignment - 1;
34     return (value + mask) & ~mask;
35 }
36 
37 
38 // Limit the given value to the provided range.  :)
clamp(float v,float min,float max)39 inline float Utils::clamp(float v, float min, float max) {
40     if (v < min) return min;
41     if (v > max) return max;
42     return v;
43 }
44 
45 
yuvToRgbx(const unsigned char Y,const unsigned char Uin,const unsigned char Vin,bool bgrxFormat)46 uint32_t Utils::yuvToRgbx(const unsigned char Y,
47                           const unsigned char Uin,
48                           const unsigned char Vin,
49                           bool bgrxFormat) {
50     // Don't use this if you want to see the best performance.  :)
51     // Better to do this in a pixel shader if we really have to, but on actual
52     // embedded hardware we expect to be able to texture directly from the YUV data
53     float U = Uin - 128.0f;
54     float V = Vin - 128.0f;
55 
56     float Rf = Y + 1.140f*V;
57     float Gf = Y - 0.395f*U - 0.581f*V;
58     float Bf = Y + 2.032f*U;
59     unsigned char R = (unsigned char)clamp(Rf, 0.0f, 255.0f);
60     unsigned char G = (unsigned char)clamp(Gf, 0.0f, 255.0f);
61     unsigned char B = (unsigned char)clamp(Bf, 0.0f, 255.0f);
62 
63     if (!bgrxFormat) {
64         return (R      ) |
65                (G <<  8) |
66                (B << 16) |
67                0xFF000000;  // Fill the alpha channel with ones
68     } else {
69         return (R << 16) |
70                (G <<  8) |
71                (B      ) |
72                0xFF000000;  // Fill the alpha channel with ones
73     }
74 }
75 
76 
copyNV21toRGB32(unsigned width,unsigned height,uint8_t * src,uint32_t * dst,unsigned dstStridePixels,bool bgrxFormat)77 void Utils::copyNV21toRGB32(unsigned width, unsigned height,
78                             uint8_t* src,
79                             uint32_t* dst, unsigned dstStridePixels,
80                             bool bgrxFormat)
81 {
82     // The NV21 format provides a Y array of 8bit values, followed by a 1/2 x 1/2 interleaved
83     // U/V array.  It assumes an even width and height for the overall image, and a horizontal
84     // stride that is an even multiple of 16 bytes for both the Y and UV arrays.
85     unsigned strideLum = align<16>(width);
86     unsigned sizeY = strideLum * height;
87     unsigned strideColor = strideLum;   // 1/2 the samples, but two interleaved channels
88     unsigned offsetUV = sizeY;
89 
90     uint8_t* srcY = src;
91     uint8_t* srcUV = src+offsetUV;
92 
93     for (unsigned r = 0; r < height; r++) {
94         // Note that we're walking the same UV row twice for even/odd luminance rows
95         uint8_t* rowY  = srcY  + r*strideLum;
96         uint8_t* rowUV = srcUV + (r/2 * strideColor);
97 
98         uint32_t* rowDest = dst + r*dstStridePixels;
99 
100         for (unsigned c = 0; c < width; c++) {
101             unsigned uCol = (c & ~1);   // uCol is always even and repeats 1:2 with Y values
102             unsigned vCol = uCol | 1;   // vCol is always odd
103             rowDest[c] = yuvToRgbx(rowY[c], rowUV[uCol], rowUV[vCol], bgrxFormat);
104         }
105     }
106 }
107 
108 
copyYV12toRGB32(unsigned width,unsigned height,uint8_t * src,uint32_t * dst,unsigned dstStridePixels,bool bgrxFormat)109 void Utils::copyYV12toRGB32(unsigned width, unsigned height,
110                             uint8_t* src,
111                             uint32_t* dst, unsigned dstStridePixels,
112                             bool bgrxFormat)
113 {
114     // The YV12 format provides a Y array of 8bit values, followed by a 1/2 x 1/2 U array, followed
115     // by another 1/2 x 1/2 V array.  It assumes an even width and height for the overall image,
116     // and a horizontal stride that is an even multiple of 16 bytes for each of the Y, U,
117     // and V arrays.
118     unsigned strideLum = align<16>(width);
119     unsigned sizeY = strideLum * height;
120     unsigned strideColor = align<16>(strideLum/2);
121     unsigned sizeColor = strideColor * height/2;
122     unsigned offsetU = sizeY;
123     unsigned offsetV = sizeY + sizeColor;
124 
125     uint8_t* srcY = src;
126     uint8_t* srcU = src+offsetU;
127     uint8_t* srcV = src+offsetV;
128 
129     for (unsigned r = 0; r < height; r++) {
130         // Note that we're walking the same U and V rows twice for even/odd luminance rows
131         uint8_t* rowY = srcY + r*strideLum;
132         uint8_t* rowU = srcU + (r/2 * strideColor);
133         uint8_t* rowV = srcV + (r/2 * strideColor);
134 
135         uint32_t* rowDest = dst + r*dstStridePixels;
136 
137         for (unsigned c = 0; c < width; c++) {
138             rowDest[c] = yuvToRgbx(rowY[c], rowU[c], rowV[c], bgrxFormat);
139         }
140     }
141 }
142 
143 
copyYUYVtoRGB32(unsigned width,unsigned height,uint8_t * src,unsigned srcStridePixels,uint32_t * dst,unsigned dstStridePixels,bool bgrxFormat)144 void Utils::copyYUYVtoRGB32(unsigned width, unsigned height,
145                             uint8_t* src, unsigned srcStridePixels,
146                             uint32_t* dst, unsigned dstStridePixels,
147                             bool bgrxFormat)
148 {
149     uint32_t* srcWords = (uint32_t*)src;
150 
151     const int srcRowPadding32 = srcStridePixels/2 - width/2;  // 2 bytes per pixel, 4 bytes per word
152     const int dstRowPadding32 = dstStridePixels   - width;    // 4 bytes per pixel, 4 bytes per word
153 
154     for (unsigned r = 0; r < height; r++) {
155         for (unsigned c = 0; c < width/2; c++) {
156             // Note:  we're walking two pixels at a time here (even/odd)
157             uint32_t srcPixel = *srcWords++;
158 
159             uint8_t Y1 = (srcPixel)       & 0xFF;
160             uint8_t U  = (srcPixel >> 8)  & 0xFF;
161             uint8_t Y2 = (srcPixel >> 16) & 0xFF;
162             uint8_t V  = (srcPixel >> 24) & 0xFF;
163 
164             // On the RGB output, we're writing one pixel at a time
165             *(dst+0) = yuvToRgbx(Y1, U, V, bgrxFormat);
166             *(dst+1) = yuvToRgbx(Y2, U, V, bgrxFormat);
167             dst += 2;
168         }
169 
170         // Skip over any extra data or end of row alignment padding
171         srcWords += srcRowPadding32;
172         dst += dstRowPadding32;
173     }
174 }
175 
176 
copyNV21toBGR32(unsigned width,unsigned height,uint8_t * src,uint32_t * dst,unsigned dstStridePixels)177 void Utils::copyNV21toBGR32(unsigned width, unsigned height,
178                             uint8_t* src,
179                             uint32_t* dst, unsigned dstStridePixels)
180 {
181     return copyNV21toRGB32(width, height, src, dst, dstStridePixels, true);
182 }
183 
184 
copyYV12toBGR32(unsigned width,unsigned height,uint8_t * src,uint32_t * dst,unsigned dstStridePixels)185 void Utils::copyYV12toBGR32(unsigned width, unsigned height,
186                             uint8_t* src,
187                             uint32_t* dst, unsigned dstStridePixels)
188 {
189     return copyYV12toRGB32(width, height, src, dst, dstStridePixels, true);
190 }
191 
192 
copyYUYVtoBGR32(unsigned width,unsigned height,uint8_t * src,unsigned srcStridePixels,uint32_t * dst,unsigned dstStridePixels)193 void Utils::copyYUYVtoBGR32(unsigned width, unsigned height,
194                             uint8_t* src, unsigned srcStridePixels,
195                             uint32_t* dst, unsigned dstStridePixels)
196 {
197     return copyYUYVtoRGB32(width, height, src, srcStridePixels, dst, dstStridePixels, true);
198 }
199 
200 
copyMatchedInterleavedFormats(unsigned width,unsigned height,void * src,unsigned srcStridePixels,void * dst,unsigned dstStridePixels,unsigned pixelSize)201 void Utils::copyMatchedInterleavedFormats(unsigned width, unsigned height,
202                                           void* src, unsigned srcStridePixels,
203                                           void* dst, unsigned dstStridePixels,
204                                           unsigned pixelSize) {
205     for (unsigned row = 0; row < height; row++) {
206         // Copy the entire row of pixel data
207         memcpy(dst, src, width * pixelSize);
208 
209         // Advance to the next row (keeping in mind that stride here is in units of pixels)
210         src = (uint8_t*)src + srcStridePixels * pixelSize;
211         dst = (uint8_t*)dst + dstStridePixels * pixelSize;
212     }
213 }
214 
215 } // namespace common
216 } // namespace evs
217 } // namespace automotive
218 } // namespace hardware
219 } // namespace android
220 
221