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