1 /*
2 * Copyright (C) 2023 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 <libyuv/convert.h>
18 #include "converters.h"
19 #include "debug.h"
20
21 namespace android {
22 namespace hardware {
23 namespace camera {
24 namespace provider {
25 namespace implementation {
26 namespace conv {
27
28 // https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion
29 constexpr size_t kFixedPointShift = 16;
30 constexpr size_t kFixedPointMul = size_t(1) << kFixedPointShift;
31 constexpr int32_t kY_R = 0.299 * kFixedPointMul;
32 constexpr int32_t kY_G = 0.587 * kFixedPointMul;
33 constexpr int32_t kY_B = 0.114 * kFixedPointMul;
34 constexpr int32_t kY_Add = 0 << kFixedPointShift;
35 constexpr int32_t kY_Shift = kFixedPointShift;
36 constexpr int32_t kY_Clamp = 255 << kY_Shift;
37 constexpr int32_t kCB_R = -0.168736 * kFixedPointMul;
38 constexpr int32_t kCB_G = -0.331264 * kFixedPointMul;
39 constexpr int32_t kCB_B = 0.5 * kFixedPointMul;
40 constexpr int32_t kCR_R = 0.5 * kFixedPointMul;
41 constexpr int32_t kCR_G = -0.418688 * kFixedPointMul;
42 constexpr int32_t kCR_B = -0.081312 * kFixedPointMul;
43 constexpr int32_t kCx_Shift = kFixedPointShift + 2;
44 constexpr int32_t kCx_Add = 128 << kCx_Shift;
45 constexpr int32_t kCx_Clamp = 255 << kCx_Shift;
46
47 #define CLAMP_SHIFT(X, MIN, MAX, S) \
48 (((X) - (((X) > (MAX)) * ((X) - (MAX))) - (((X) < (MIN)) * ((MIN) - (X)))) >> (S))
49
50 #define RGB2Y(R, G, B) (kY_R * (R) + kY_G * (G) + kY_B * (B) + kY_Add)
51 #define RGB2CB(R, G, B) (kCB_R * (R) + kCB_G * (G) + kCB_B * (B) + kCx_Add)
52 #define RGB2CR(R, G, B) (kCR_R * (R) + kCR_G * (G) + kCR_B * (B) + kCx_Add)
53
rgba2yuv(const size_t width,size_t height,const uint32_t * rgba,const android_ycbcr & ycbcr)54 bool rgba2yuv(const size_t width, size_t height,
55 const uint32_t* rgba, const android_ycbcr& ycbcr) {
56 if ((width & 1) || (height & 1)) {
57 return FAILURE(false);
58 }
59
60 if (ycbcr.chroma_step == 1) {
61 return (libyuv::ABGRToI420(
62 reinterpret_cast<const uint8_t*>(rgba), (width * sizeof(*rgba)),
63 static_cast<uint8_t*>(ycbcr.y), ycbcr.ystride,
64 static_cast<uint8_t*>(ycbcr.cb), ycbcr.cstride,
65 static_cast<uint8_t*>(ycbcr.cr), ycbcr.cstride,
66 width, height) == 0) ? true : FAILURE(false);
67 }
68
69 const size_t width2 = width + width;
70 const size_t ystride = ycbcr.ystride;
71 const size_t ystride2 = ystride + ystride;
72 const size_t cstride = ycbcr.cstride;
73 const size_t chromaStep = ycbcr.chroma_step;
74 uint8_t* y = static_cast<uint8_t*>(ycbcr.y);
75 uint8_t* cb = static_cast<uint8_t*>(ycbcr.cb);
76 uint8_t* cr = static_cast<uint8_t*>(ycbcr.cr);
77
78 // The loops below go through the RGBA image 2rows X 2columns at once.
79 // Each four RGBA pixels produce four Y values and one {Cb, Cr} pair.
80 // R, G and B components of those 4 pixels are averaged (this is why
81 // they are called R4, G4 and B4) before converting to the {Cb, Cr} pair.
82 // The code does not have a separate divizion by 4 to average the color
83 // components, instead `2` is added to the Cx shift.
84 for (; height > 0; height -= 2, rgba += width2, y += ystride2,
85 cb += cstride, cr += cstride) {
86 const uint32_t* r0 = rgba;
87 const uint32_t* r1 = rgba + width;
88 uint8_t* y0 = y;
89 uint8_t* y1 = y + ystride;
90 uint8_t* cb0 = cb;
91 uint8_t* cr0 = cr;
92
93 for (size_t col = width; col > 0; col -= 2, r0 += 2, r1 += 2, y0 += 2, y1 += 2,
94 cb0 += chromaStep, cr0 += chromaStep) {
95 int32_t r4;
96 int32_t g4;
97 int32_t b4;
98 int32_t tmp0;
99 int32_t tmp1;
100
101 {
102 const uint32_t p00 = r0[0];
103 const uint32_t p01 = r0[1];
104 const int32_t r00 = p00 & 0xFF;
105 const int32_t r01 = p01 & 0xFF;
106 const int32_t g00 = (p00 >> 8) & 0xFF;
107 const int32_t g01 = (p01 >> 8) & 0xFF;
108 const int32_t b00 = (p00 >> 16) & 0xFF;
109 const int32_t b01 = (p01 >> 16) & 0xFF;
110 r4 = r00 + r01;
111 g4 = g00 + g01;
112 b4 = b00 + b01;
113 tmp0 = RGB2Y(r00, g00, b00);
114 tmp1 = RGB2Y(r01, g01, b01);
115 tmp0 = CLAMP_SHIFT(tmp0, 0, kY_Clamp, kY_Shift);
116 tmp1 = CLAMP_SHIFT(tmp1, 0, kY_Clamp, kY_Shift);
117 y0[0] = tmp0;
118 y1[0] = tmp1;
119 }
120 {
121 const uint32_t p10 = r1[0];
122 const uint32_t p11 = r1[1];
123 const int32_t r10 = p10 & 0xFF;
124 const int32_t r11 = p11 & 0xFF;
125 const int32_t g10 = (p10 >> 8) & 0xFF;
126 const int32_t g11 = (p11 >> 8) & 0xFF;
127 const int32_t b10 = (p10 >> 16) & 0xFF;
128 const int32_t b11 = (p11 >> 16) & 0xFF;
129 r4 += (r10 + r11);
130 g4 += (g10 + g11);
131 b4 += (b10 + b11);
132 tmp0 = RGB2Y(r10, g10, b10);
133 tmp1 = RGB2Y(r11, g11, b11);
134 tmp0 = CLAMP_SHIFT(tmp0, 0, kY_Clamp, kY_Shift);
135 tmp1 = CLAMP_SHIFT(tmp1, 0, kY_Clamp, kY_Shift);
136 y1[0] = tmp0;
137 y1[1] = tmp1;
138 }
139
140 tmp0 = RGB2CB(r4, g4, b4);
141 tmp1 = RGB2CR(r4, g4, b4);
142 tmp0 = CLAMP_SHIFT(tmp0, 0, kCx_Clamp, kCx_Shift);
143 tmp1 = CLAMP_SHIFT(tmp1, 0, kCx_Clamp, kCx_Shift);
144 *cb0 = tmp0;
145 *cr0 = tmp1;
146 }
147 }
148
149 return true;
150 }
151
152 } // namespace conv
153 } // namespace implementation
154 } // namespace provider
155 } // namespace camera
156 } // namespace hardware
157 } // namespace android
158