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 <android/hardware_buffer.h>
18 #include "FormatConvert.h"
19
20
21 // Round up to the nearest multiple of the given alignment value
22 template<unsigned alignment>
align(int value)23 int align(int value) {
24 static_assert((alignment && !(alignment & (alignment - 1))),
25 "alignment must be a power of 2");
26
27 unsigned mask = alignment - 1;
28 return (value + mask) & ~mask;
29 }
30
31
32 // Limit the given value to the provided range. :)
clamp(float v,float min,float max)33 static inline float clamp(float v, float min, float max) {
34 if (v < min) return min;
35 if (v > max) return max;
36 return v;
37 }
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 ) |
55 (G << 8) |
56 (B << 16) |
57 0xFF000000; // Fill the alpha channel with ones
58 }
59
60
copyNV21toRGB32(unsigned width,unsigned height,uint8_t * src,uint32_t * dst,unsigned dstStridePixels)61 void copyNV21toRGB32(unsigned width, unsigned height,
62 uint8_t* src,
63 uint32_t* dst, unsigned dstStridePixels)
64 {
65 // The NV21 format provides a Y array of 8bit values, followed by a 1/2 x 1/2 interleaved
66 // U/V array. It assumes an even width and height for the overall image, and a horizontal
67 // stride that is an even multiple of 16 bytes for both the Y and UV arrays.
68 unsigned strideLum = align<16>(width);
69 unsigned sizeY = strideLum * height;
70 unsigned strideColor = strideLum; // 1/2 the samples, but two interleaved channels
71 unsigned offsetUV = sizeY;
72
73 uint8_t* srcY = src;
74 uint8_t* srcUV = src+offsetUV;
75
76 for (unsigned r = 0; r < height; r++) {
77 // Note that we're walking the same UV row twice for even/odd luminance rows
78 uint8_t* rowY = srcY + r*strideLum;
79 uint8_t* rowUV = srcUV + (r/2 * strideColor);
80
81 uint32_t* rowDest = dst + r*dstStridePixels;
82
83 for (unsigned c = 0; c < width; c++) {
84 unsigned uCol = (c & ~1); // uCol is always even and repeats 1:2 with Y values
85 unsigned vCol = uCol | 1; // vCol is always odd
86 rowDest[c] = yuvToRgbx(rowY[c], rowUV[uCol], rowUV[vCol]);
87 }
88 }
89 }
90
91
copyYV12toRGB32(unsigned width,unsigned height,uint8_t * src,uint32_t * dst,unsigned dstStridePixels)92 void copyYV12toRGB32(unsigned width, unsigned height,
93 uint8_t* src,
94 uint32_t* dst, unsigned dstStridePixels)
95 {
96 // The YV12 format provides a Y array of 8bit values, followed by a 1/2 x 1/2 U array, followed
97 // by another 1/2 x 1/2 V array. It assumes an even width and height for the overall image,
98 // and a horizontal stride that is an even multiple of 16 bytes for each of the Y, U,
99 // and V arrays.
100 unsigned strideLum = align<16>(width);
101 unsigned sizeY = strideLum * height;
102 unsigned strideColor = align<16>(strideLum/2);
103 unsigned sizeColor = strideColor * height/2;
104 unsigned offsetU = sizeY;
105 unsigned offsetV = sizeY + sizeColor;
106
107 uint8_t* srcY = src;
108 uint8_t* srcU = src+offsetU;
109 uint8_t* srcV = src+offsetV;
110
111 for (unsigned r = 0; r < height; r++) {
112 // Note that we're walking the same U and V rows twice for even/odd luminance rows
113 uint8_t* rowY = srcY + r*strideLum;
114 uint8_t* rowU = srcU + (r/2 * strideColor);
115 uint8_t* rowV = srcV + (r/2 * strideColor);
116
117 uint32_t* rowDest = dst + r*dstStridePixels;
118
119 for (unsigned c = 0; c < width; c++) {
120 rowDest[c] = yuvToRgbx(rowY[c], rowU[c], rowV[c]);
121 }
122 }
123 }
124
125
copyYUYVtoRGB32(unsigned width,unsigned height,uint8_t * src,unsigned srcStridePixels,uint32_t * dst,unsigned dstStridePixels)126 void copyYUYVtoRGB32(unsigned width, unsigned height,
127 uint8_t* src, unsigned srcStridePixels,
128 uint32_t* dst, unsigned dstStridePixels)
129 {
130 uint32_t* srcWords = (uint32_t*)src;
131
132 const int srcRowPadding32 = srcStridePixels/2 - width/2; // 2 bytes per pixel, 4 bytes per word
133 const int dstRowPadding32 = dstStridePixels - width; // 4 bytes per pixel, 4 bytes per word
134
135 for (unsigned r = 0; r < height; r++) {
136 for (unsigned c = 0; c < width/2; c++) {
137 // Note: we're walking two pixels at a time here (even/odd)
138 uint32_t srcPixel = *srcWords++;
139
140 uint8_t Y1 = (srcPixel) & 0xFF;
141 uint8_t U = (srcPixel >> 8) & 0xFF;
142 uint8_t Y2 = (srcPixel >> 16) & 0xFF;
143 uint8_t V = (srcPixel >> 24) & 0xFF;
144
145 // On the RGB output, we're writing one pixel at a time
146 *(dst+0) = yuvToRgbx(Y1, U, V);
147 *(dst+1) = yuvToRgbx(Y2, U, V);
148 dst += 2;
149 }
150
151 // Skip over any extra data or end of row alignment padding
152 srcWords += srcRowPadding32;
153 dst += dstRowPadding32;
154 }
155 }
156
157
copyMatchedInterleavedFormats(unsigned width,unsigned height,void * src,unsigned srcStridePixels,void * dst,unsigned dstStridePixels,unsigned pixelSize)158 void copyMatchedInterleavedFormats(unsigned width, unsigned height,
159 void* src, unsigned srcStridePixels,
160 void* dst, unsigned dstStridePixels,
161 unsigned pixelSize) {
162 for (unsigned row = 0; row < height; row++) {
163 // Copy the entire row of pixel data
164 memcpy(dst, src, width * pixelSize);
165
166 // Advance to the next row (keeping in mind that stride here is in units of pixels)
167 src = (uint8_t*)src + srcStridePixels * pixelSize;
168 dst = (uint8_t*)dst + dstStridePixels * pixelSize;
169 }
170 }
171
172
convertBufferDesc(const BufferDesc_1_0 & src)173 BufferDesc_1_1 convertBufferDesc(const BufferDesc_1_0& src) {
174 BufferDesc_1_1 dst = {};
175 AHardwareBuffer_Desc* pDesc =
176 reinterpret_cast<AHardwareBuffer_Desc *>(&dst.buffer.description);
177 pDesc->width = src.width;
178 pDesc->height = src.height;
179 pDesc->layers = 1;
180 pDesc->format = src.format;
181 pDesc->usage = static_cast<uint64_t>(src.usage);
182 pDesc->stride = src.stride;
183
184 dst.buffer.nativeHandle = src.memHandle;
185 dst.pixelSize = src.pixelSize;
186 dst.bufferId = src.bufferId;
187
188 return dst;
189 }
190