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