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