blob: aec6523e8a03413e6bf7baa4e5e8730ad36a5031 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
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_NDEBUG 0
18#define LOG_TAG "SimpleC2Component"
19#include <log/log.h>
20
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070021#include <android/hardware_buffer.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080022#include <cutils/properties.h>
23#include <media/stagefright/foundation/AMessage.h>
Harish Mahendrakar29351ea2023-08-24 17:18:56 +053024#include <media/stagefright/foundation/AUtils.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080025
26#include <inttypes.h>
Harish Mahendrakar29351ea2023-08-24 17:18:56 +053027#include <libyuv.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080028
29#include <C2Config.h>
30#include <C2Debug.h>
31#include <C2PlatformSupport.h>
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070032#include <Codec2BufferUtils.h>
33#include <Codec2CommonUtils.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080034#include <SimpleC2Component.h>
35
36namespace android {
Harish Mahendrakar29351ea2023-08-24 17:18:56 +053037
38// libyuv version required for I410ToAB30Matrix and I210ToAB30Matrix.
39#if LIBYUV_VERSION >= 1780
40#include <algorithm>
41#define HAVE_LIBYUV_I410_I210_TO_AB30 1
42#else
43#define HAVE_LIBYUV_I410_I210_TO_AB30 0
44#endif
45
Harish Mahendrakar1b1aef22021-12-30 19:12:51 -080046constexpr uint8_t kNeutralUVBitDepth8 = 128;
47constexpr uint16_t kNeutralUVBitDepth10 = 512;
Pawin Vongmasa36653902018-11-15 00:10:25 -080048
Harish Mahendrakar1b1aef22021-12-30 19:12:51 -080049void convertYUV420Planar8ToYV12(uint8_t *dstY, uint8_t *dstU, uint8_t *dstV, const uint8_t *srcY,
50 const uint8_t *srcU, const uint8_t *srcV, size_t srcYStride,
51 size_t srcUStride, size_t srcVStride, size_t dstYStride,
Vignesh Venkatasubramanian47b1d222023-01-12 21:45:40 +000052 size_t dstUStride, size_t dstVStride, uint32_t width,
53 uint32_t height, bool isMonochrome) {
Harish Mahendrakar1b1aef22021-12-30 19:12:51 -080054 for (size_t i = 0; i < height; ++i) {
55 memcpy(dstY, srcY, width);
56 srcY += srcYStride;
57 dstY += dstYStride;
58 }
59
60 if (isMonochrome) {
61 // Fill with neutral U/V values.
Vignesh Venkatasubramanianc4d385f2022-02-22 10:49:46 -080062 for (size_t i = 0; i < (height + 1) / 2; ++i) {
63 memset(dstV, kNeutralUVBitDepth8, (width + 1) / 2);
64 memset(dstU, kNeutralUVBitDepth8, (width + 1) / 2);
Vignesh Venkatasubramanian47b1d222023-01-12 21:45:40 +000065 dstV += dstVStride;
66 dstU += dstUStride;
Harish Mahendrakar1b1aef22021-12-30 19:12:51 -080067 }
68 return;
69 }
70
Vignesh Venkatasubramanianc4d385f2022-02-22 10:49:46 -080071 for (size_t i = 0; i < (height + 1) / 2; ++i) {
72 memcpy(dstV, srcV, (width + 1) / 2);
Harish Mahendrakar1b1aef22021-12-30 19:12:51 -080073 srcV += srcVStride;
Vignesh Venkatasubramanian47b1d222023-01-12 21:45:40 +000074 dstV += dstVStride;
Harish Mahendrakar1b1aef22021-12-30 19:12:51 -080075 }
76
Vignesh Venkatasubramanianc4d385f2022-02-22 10:49:46 -080077 for (size_t i = 0; i < (height + 1) / 2; ++i) {
78 memcpy(dstU, srcU, (width + 1) / 2);
Harish Mahendrakar1b1aef22021-12-30 19:12:51 -080079 srcU += srcUStride;
Vignesh Venkatasubramanian47b1d222023-01-12 21:45:40 +000080 dstU += dstUStride;
Harish Mahendrakar1b1aef22021-12-30 19:12:51 -080081 }
82}
83
84void convertYUV420Planar16ToY410(uint32_t *dst, const uint16_t *srcY, const uint16_t *srcU,
85 const uint16_t *srcV, size_t srcYStride, size_t srcUStride,
86 size_t srcVStride, size_t dstStride, size_t width, size_t height) {
87 // Converting two lines at a time, slightly faster
88 for (size_t y = 0; y < height; y += 2) {
89 uint32_t *dstTop = (uint32_t *)dst;
90 uint32_t *dstBot = (uint32_t *)(dst + dstStride);
91 uint16_t *ySrcTop = (uint16_t *)srcY;
92 uint16_t *ySrcBot = (uint16_t *)(srcY + srcYStride);
93 uint16_t *uSrc = (uint16_t *)srcU;
94 uint16_t *vSrc = (uint16_t *)srcV;
95
96 uint32_t u01, v01, y01, y23, y45, y67, uv0, uv1;
97 size_t x = 0;
98 for (; x < width - 3; x += 4) {
99 u01 = *((uint32_t *)uSrc);
100 uSrc += 2;
101 v01 = *((uint32_t *)vSrc);
102 vSrc += 2;
103
104 y01 = *((uint32_t *)ySrcTop);
105 ySrcTop += 2;
106 y23 = *((uint32_t *)ySrcTop);
107 ySrcTop += 2;
108 y45 = *((uint32_t *)ySrcBot);
109 ySrcBot += 2;
110 y67 = *((uint32_t *)ySrcBot);
111 ySrcBot += 2;
112
113 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
114 uv1 = (u01 >> 16) | ((v01 >> 16) << 20);
115
116 *dstTop++ = 3 << 30 | ((y01 & 0x3FF) << 10) | uv0;
117 *dstTop++ = 3 << 30 | ((y01 >> 16) << 10) | uv0;
118 *dstTop++ = 3 << 30 | ((y23 & 0x3FF) << 10) | uv1;
119 *dstTop++ = 3 << 30 | ((y23 >> 16) << 10) | uv1;
120
121 *dstBot++ = 3 << 30 | ((y45 & 0x3FF) << 10) | uv0;
122 *dstBot++ = 3 << 30 | ((y45 >> 16) << 10) | uv0;
123 *dstBot++ = 3 << 30 | ((y67 & 0x3FF) << 10) | uv1;
124 *dstBot++ = 3 << 30 | ((y67 >> 16) << 10) | uv1;
125 }
126
127 // There should be at most 2 more pixels to process. Note that we don't
128 // need to consider odd case as the buffer is always aligned to even.
129 if (x < width) {
130 u01 = *uSrc;
131 v01 = *vSrc;
132 y01 = *((uint32_t *)ySrcTop);
133 y45 = *((uint32_t *)ySrcBot);
134 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
135 *dstTop++ = ((y01 & 0x3FF) << 10) | uv0;
136 *dstTop++ = ((y01 >> 16) << 10) | uv0;
137 *dstBot++ = ((y45 & 0x3FF) << 10) | uv0;
138 *dstBot++ = ((y45 >> 16) << 10) | uv0;
139 }
140
141 srcY += srcYStride * 2;
142 srcU += srcUStride;
143 srcV += srcVStride;
144 dst += dstStride * 2;
145 }
146}
Lajos Molnarc971e962022-06-01 20:24:38 -0700147
148namespace {
149
150static C2ColorAspectsStruct FillMissingColorAspects(
151 std::shared_ptr<const C2ColorAspectsStruct> aspects,
152 int32_t width, int32_t height) {
153 C2ColorAspectsStruct _aspects;
154 if (aspects) {
155 _aspects = *aspects;
156 }
157
158 // use matrix for conversion
159 if (_aspects.matrix == C2Color::MATRIX_UNSPECIFIED) {
160 // if not specified, deduce matrix from primaries
161 if (_aspects.primaries == C2Color::PRIMARIES_UNSPECIFIED) {
162 // if those are also not specified, deduce primaries first from transfer, then from
163 // width and height
164 if (_aspects.transfer == C2Color::TRANSFER_ST2084
165 || _aspects.transfer == C2Color::TRANSFER_HLG) {
166 _aspects.primaries = C2Color::PRIMARIES_BT2020;
167 } else if (width >= 3840 || height >= 3840 || width * (int64_t)height >= 3840 * 1634) {
168 // TODO: stagefright defaults to BT.2020 for UHD, but perhaps we should default to
169 // BT.709 for non-HDR 10-bit UHD content
170 // (see media/libstagefright/foundation/ColorUtils.cpp)
171 _aspects.primaries = C2Color::PRIMARIES_BT2020;
172 } else if ((width <= 720 && height <= 576)
173 || (height <= 720 && width <= 576)) {
174 // note: it does not actually matter whether to use 525 or 625 here as the
175 // conversion is the same
176 _aspects.primaries = C2Color::PRIMARIES_BT601_625;
177 } else {
178 _aspects.primaries = C2Color::PRIMARIES_BT709;
179 }
180 }
181
182 switch (_aspects.primaries) {
183 case C2Color::PRIMARIES_BT601_525:
184 case C2Color::PRIMARIES_BT601_625:
185 _aspects.matrix = C2Color::MATRIX_BT601;
186 break;
187
188 case C2Color::PRIMARIES_BT709:
189 _aspects.matrix = C2Color::MATRIX_BT709;
190 break;
191
192 case C2Color::PRIMARIES_BT2020:
193 default:
194 _aspects.matrix = C2Color::MATRIX_BT2020;
195 }
196 }
197
198 return _aspects;
199}
200
201// matrix conversion coefficients
202// (see media/libstagefright/colorconverter/ColorConverter.cpp for more details)
203struct Coeffs {
Harish Mahendrakar36bd7942022-07-07 20:14:14 -0700204 int32_t _y, _r_v, _g_u, _g_v, _b_u, _c16;
Lajos Molnarc971e962022-06-01 20:24:38 -0700205};
206
207static const struct Coeffs GetCoeffsForAspects(const C2ColorAspectsStruct &aspects) {
208 bool isFullRange = aspects.range == C2Color::RANGE_FULL;
209
210 switch (aspects.matrix) {
211 case C2Color::MATRIX_BT601:
212 /**
213 * BT.601: K_R = 0.299; K_B = 0.114
214 */
215 if (isFullRange) {
216 return Coeffs { 1024, 1436, 352, 731, 1815, 0 };
217 } else {
218 return Coeffs { 1196, 1639, 402, 835, 2072, 64 };
219 }
220 break;
221
222 case C2Color::MATRIX_BT709:
223 /**
224 * BT.709: K_R = 0.2126; K_B = 0.0722
225 */
226 if (isFullRange) {
227 return Coeffs { 1024, 1613, 192, 479, 1900, 0 };
228 } else {
229 return Coeffs { 1196, 1841, 219, 547, 2169, 64 };
230 }
231 break;
232
233 case C2Color::MATRIX_BT2020:
234 default:
235 /**
236 * BT.2020: K_R = 0.2627; K_B = 0.0593
237 */
238 if (isFullRange) {
239 return Coeffs { 1024, 1510, 169, 585, 1927, 0 };
240 } else {
241 return Coeffs { 1196, 1724, 192, 668, 2200, 64 };
242 }
243 }
244}
245
246}
247
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700248#define CLIP3(min, v, max) (((v) < (min)) ? (min) : (((max) > (v)) ? (v) : (max)))
Lajos Molnarc971e962022-06-01 20:24:38 -0700249void convertYUV420Planar16ToRGBA1010102(
250 uint32_t *dst, const uint16_t *srcY, const uint16_t *srcU,
251 const uint16_t *srcV, size_t srcYStride, size_t srcUStride,
252 size_t srcVStride, size_t dstStride, size_t width,
253 size_t height,
254 std::shared_ptr<const C2ColorAspectsStruct> aspects) {
255
256 C2ColorAspectsStruct _aspects = FillMissingColorAspects(aspects, width, height);
257
258 struct Coeffs coeffs = GetCoeffsForAspects(_aspects);
259
260 int32_t _y = coeffs._y;
261 int32_t _b_u = coeffs._b_u;
262 int32_t _neg_g_u = -coeffs._g_u;
263 int32_t _neg_g_v = -coeffs._g_v;
264 int32_t _r_v = coeffs._r_v;
265 int32_t _c16 = coeffs._c16;
266
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700267 // Converting two lines at a time, slightly faster
268 for (size_t y = 0; y < height; y += 2) {
269 uint32_t *dstTop = (uint32_t *)dst;
270 uint32_t *dstBot = (uint32_t *)(dst + dstStride);
271 uint16_t *ySrcTop = (uint16_t *)srcY;
272 uint16_t *ySrcBot = (uint16_t *)(srcY + srcYStride);
273 uint16_t *uSrc = (uint16_t *)srcU;
274 uint16_t *vSrc = (uint16_t *)srcV;
Harish Mahendrakar1b1aef22021-12-30 19:12:51 -0800275
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700276 for (size_t x = 0; x < width; x += 2) {
277 int32_t u, v, y00, y01, y10, y11;
278 u = *uSrc - 512;
279 uSrc += 1;
280 v = *vSrc - 512;
281 vSrc += 1;
282
Lajos Molnarc971e962022-06-01 20:24:38 -0700283 y00 = *ySrcTop - _c16;
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700284 ySrcTop += 1;
Lajos Molnarc971e962022-06-01 20:24:38 -0700285 y01 = *ySrcTop - _c16;
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700286 ySrcTop += 1;
Lajos Molnarc971e962022-06-01 20:24:38 -0700287 y10 = *ySrcBot - _c16;
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700288 ySrcBot += 1;
Lajos Molnarc971e962022-06-01 20:24:38 -0700289 y11 = *ySrcBot - _c16;
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700290 ySrcBot += 1;
291
Lajos Molnarc971e962022-06-01 20:24:38 -0700292 int32_t u_b = u * _b_u;
293 int32_t u_g = u * _neg_g_u;
294 int32_t v_g = v * _neg_g_v;
295 int32_t v_r = v * _r_v;
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700296
297 int32_t yMult, b, g, r;
Lajos Molnarc971e962022-06-01 20:24:38 -0700298 yMult = y00 * _y + 512;
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700299 b = (yMult + u_b) / 1024;
300 g = (yMult + v_g + u_g) / 1024;
301 r = (yMult + v_r) / 1024;
302 b = CLIP3(0, b, 1023);
303 g = CLIP3(0, g, 1023);
304 r = CLIP3(0, r, 1023);
305 *dstTop++ = 3 << 30 | (b << 20) | (g << 10) | r;
306
Lajos Molnarc971e962022-06-01 20:24:38 -0700307 yMult = y01 * _y + 512;
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700308 b = (yMult + u_b) / 1024;
309 g = (yMult + v_g + u_g) / 1024;
310 r = (yMult + v_r) / 1024;
311 b = CLIP3(0, b, 1023);
312 g = CLIP3(0, g, 1023);
313 r = CLIP3(0, r, 1023);
314 *dstTop++ = 3 << 30 | (b << 20) | (g << 10) | r;
315
Lajos Molnarc971e962022-06-01 20:24:38 -0700316 yMult = y10 * _y + 512;
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700317 b = (yMult + u_b) / 1024;
318 g = (yMult + v_g + u_g) / 1024;
319 r = (yMult + v_r) / 1024;
320 b = CLIP3(0, b, 1023);
321 g = CLIP3(0, g, 1023);
322 r = CLIP3(0, r, 1023);
323 *dstBot++ = 3 << 30 | (b << 20) | (g << 10) | r;
324
Lajos Molnarc971e962022-06-01 20:24:38 -0700325 yMult = y11 * _y + 512;
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700326 b = (yMult + u_b) / 1024;
327 g = (yMult + v_g + u_g) / 1024;
328 r = (yMult + v_r) / 1024;
329 b = CLIP3(0, b, 1023);
330 g = CLIP3(0, g, 1023);
331 r = CLIP3(0, r, 1023);
332 *dstBot++ = 3 << 30 | (b << 20) | (g << 10) | r;
333 }
334
335 srcY += srcYStride * 2;
336 srcU += srcUStride;
337 srcV += srcVStride;
338 dst += dstStride * 2;
339 }
340}
341
Lajos Molnarc971e962022-06-01 20:24:38 -0700342void convertYUV420Planar16ToY410OrRGBA1010102(
343 uint32_t *dst, const uint16_t *srcY,
344 const uint16_t *srcU, const uint16_t *srcV,
345 size_t srcYStride, size_t srcUStride,
346 size_t srcVStride, size_t dstStride, size_t width, size_t height,
347 std::shared_ptr<const C2ColorAspectsStruct> aspects) {
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700348 if (isAtLeastT()) {
349 convertYUV420Planar16ToRGBA1010102(dst, srcY, srcU, srcV, srcYStride, srcUStride,
Lajos Molnarc971e962022-06-01 20:24:38 -0700350 srcVStride, dstStride, width, height, aspects);
Harish Mahendrakar5c467652022-04-28 19:47:28 -0700351 } else {
352 convertYUV420Planar16ToY410(dst, srcY, srcU, srcV, srcYStride, srcUStride, srcVStride,
353 dstStride, width, height);
354 }
355}
Lajos Molnarc971e962022-06-01 20:24:38 -0700356
Harish Mahendrakar1b1aef22021-12-30 19:12:51 -0800357void convertYUV420Planar16ToYV12(uint8_t *dstY, uint8_t *dstU, uint8_t *dstV, const uint16_t *srcY,
358 const uint16_t *srcU, const uint16_t *srcV, size_t srcYStride,
359 size_t srcUStride, size_t srcVStride, size_t dstYStride,
360 size_t dstUVStride, size_t width, size_t height,
361 bool isMonochrome) {
362 for (size_t y = 0; y < height; ++y) {
363 for (size_t x = 0; x < width; ++x) {
364 dstY[x] = (uint8_t)(srcY[x] >> 2);
365 }
366 srcY += srcYStride;
367 dstY += dstYStride;
368 }
369
370 if (isMonochrome) {
371 // Fill with neutral U/V values.
372 for (size_t y = 0; y < (height + 1) / 2; ++y) {
373 memset(dstV, kNeutralUVBitDepth8, (width + 1) / 2);
374 memset(dstU, kNeutralUVBitDepth8, (width + 1) / 2);
375 dstV += dstUVStride;
376 dstU += dstUVStride;
377 }
378 return;
379 }
380
381 for (size_t y = 0; y < (height + 1) / 2; ++y) {
382 for (size_t x = 0; x < (width + 1) / 2; ++x) {
383 dstU[x] = (uint8_t)(srcU[x] >> 2);
384 dstV[x] = (uint8_t)(srcV[x] >> 2);
385 }
386 srcU += srcUStride;
387 srcV += srcVStride;
388 dstU += dstUVStride;
389 dstV += dstUVStride;
390 }
391}
392
393void convertYUV420Planar16ToP010(uint16_t *dstY, uint16_t *dstUV, const uint16_t *srcY,
394 const uint16_t *srcU, const uint16_t *srcV, size_t srcYStride,
395 size_t srcUStride, size_t srcVStride, size_t dstYStride,
396 size_t dstUVStride, size_t width, size_t height,
397 bool isMonochrome) {
398 for (size_t y = 0; y < height; ++y) {
399 for (size_t x = 0; x < width; ++x) {
400 dstY[x] = srcY[x] << 6;
401 }
402 srcY += srcYStride;
403 dstY += dstYStride;
404 }
405
406 if (isMonochrome) {
407 // Fill with neutral U/V values.
408 for (size_t y = 0; y < (height + 1) / 2; ++y) {
409 for (size_t x = 0; x < (width + 1) / 2; ++x) {
410 dstUV[2 * x] = kNeutralUVBitDepth10 << 6;
411 dstUV[2 * x + 1] = kNeutralUVBitDepth10 << 6;
412 }
413 dstUV += dstUVStride;
414 }
415 return;
416 }
417
418 for (size_t y = 0; y < (height + 1) / 2; ++y) {
419 for (size_t x = 0; x < (width + 1) / 2; ++x) {
420 dstUV[2 * x] = srcU[x] << 6;
421 dstUV[2 * x + 1] = srcV[x] << 6;
422 }
423 srcU += srcUStride;
424 srcV += srcVStride;
425 dstUV += dstUVStride;
426 }
427}
Fyodor Kyslovd97e9912022-11-07 19:23:21 +0000428
429void convertP010ToYUV420Planar16(uint16_t *dstY, uint16_t *dstU, uint16_t *dstV,
430 const uint16_t *srcY, const uint16_t *srcUV,
431 size_t srcYStride, size_t srcUVStride, size_t dstYStride,
432 size_t dstUStride, size_t dstVStride, size_t width,
433 size_t height, bool isMonochrome) {
434 for (size_t y = 0; y < height; ++y) {
435 for (size_t x = 0; x < width; ++x) {
436 dstY[x] = srcY[x] >> 6;
437 }
438 srcY += srcYStride;
439 dstY += dstYStride;
440 }
441
442 if (isMonochrome) {
443 // Fill with neutral U/V values.
444 for (size_t y = 0; y < (height + 1) / 2; ++y) {
445 for (size_t x = 0; x < (width + 1) / 2; ++x) {
446 dstU[x] = kNeutralUVBitDepth10;
447 dstV[x] = kNeutralUVBitDepth10;
448 }
449 dstU += dstUStride;
450 dstV += dstVStride;
451 }
452 return;
453 }
454
455 for (size_t y = 0; y < (height + 1) / 2; ++y) {
456 for (size_t x = 0; x < (width + 1) / 2; ++x) {
457 dstU[x] = srcUV[2 * x] >> 6;
458 dstV[x] = srcUV[2 * x + 1] >> 6;
459 }
460 dstU += dstUStride;
461 dstV += dstVStride;
462 srcUV += srcUVStride;
463 }
464}
465
Aayush Soni5e82a6c2023-02-27 18:34:23 +0530466static const int16_t bt709Matrix_10bit[2][3][3] = {
467 { { 218, 732, 74 }, { -117, -395, 512 }, { 512, -465, -47 } }, /* RANGE_FULL */
468 { { 186, 627, 63 }, { -103, -345, 448 }, { 448, -407, -41 } }, /* RANGE_LIMITED */
469};
470
471static const int16_t bt2020Matrix_10bit[2][3][3] = {
472 { { 269, 694, 61 }, { -143, -369, 512 }, { 512, -471, -41 } }, /* RANGE_FULL */
473 { { 230, 594, 52 }, { -125, -323, 448 }, { 448, -412, -36 } }, /* RANGE_LIMITED */
474};
475
476void convertRGBA1010102ToYUV420Planar16(uint16_t* dstY, uint16_t* dstU, uint16_t* dstV,
477 const uint32_t* srcRGBA, size_t srcRGBStride, size_t width,
478 size_t height, C2Color::matrix_t colorMatrix,
479 C2Color::range_t colorRange) {
480 uint16_t r, g, b;
481 int32_t i32Y, i32U, i32V;
482 uint16_t zeroLvl = colorRange == C2Color::RANGE_FULL ? 0 : 64;
483 uint16_t maxLvlLuma = colorRange == C2Color::RANGE_FULL ? 1023 : 940;
484 uint16_t maxLvlChroma = colorRange == C2Color::RANGE_FULL ? 1023 : 960;
485 // set default range as limited
486 if (colorRange != C2Color::RANGE_FULL) {
487 colorRange = C2Color::RANGE_LIMITED;
488 }
489 const int16_t(*weights)[3] = (colorMatrix == C2Color::MATRIX_BT709)
490 ? bt709Matrix_10bit[colorRange - 1]
491 : bt2020Matrix_10bit[colorRange - 1];
492
493 for (size_t y = 0; y < height; ++y) {
494 for (size_t x = 0; x < width; ++x) {
495 b = (srcRGBA[x] >> 20) & 0x3FF;
496 g = (srcRGBA[x] >> 10) & 0x3FF;
497 r = srcRGBA[x] & 0x3FF;
498
499 i32Y = ((r * weights[0][0] + g * weights[0][1] + b * weights[0][2] + 512) >> 10) +
500 zeroLvl;
501 dstY[x] = CLIP3(zeroLvl, i32Y, maxLvlLuma);
502 if (y % 2 == 0 && x % 2 == 0) {
503 i32U = ((r * weights[1][0] + g * weights[1][1] + b * weights[1][2] + 512) >> 10) +
504 512;
505 i32V = ((r * weights[2][0] + g * weights[2][1] + b * weights[2][2] + 512) >> 10) +
506 512;
507 dstU[x >> 1] = CLIP3(zeroLvl, i32U, maxLvlChroma);
508 dstV[x >> 1] = CLIP3(zeroLvl, i32V, maxLvlChroma);
509 }
510 }
511 srcRGBA += srcRGBStride;
512 dstY += width;
513 if (y % 2 == 0) {
514 dstU += width / 2;
515 dstV += width / 2;
516 }
517 }
518}
519
Harish Mahendrakar29351ea2023-08-24 17:18:56 +0530520void convertPlanar16ToY410OrRGBA1010102(uint8_t* dst, const uint16_t* srcY, const uint16_t* srcU,
521 const uint16_t* srcV, size_t srcYStride, size_t srcUStride,
522 size_t srcVStride, size_t dstStride, size_t width,
523 size_t height,
524 std::shared_ptr<const C2ColorAspectsStruct> aspects,
525 CONV_FORMAT_T format) {
526 bool processed = false;
527#if HAVE_LIBYUV_I410_I210_TO_AB30
528 if (format == CONV_FORMAT_I444) {
529 libyuv::I410ToAB30Matrix(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dst,
530 dstStride, &libyuv::kYuvV2020Constants, width, height);
531 processed = true;
532 } else if (format == CONV_FORMAT_I422) {
533 libyuv::I210ToAB30Matrix(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dst,
534 dstStride, &libyuv::kYuvV2020Constants, width, height);
535 processed = true;
536 }
537#endif // HAVE_LIBYUV_I410_I210_TO_AB30
538 if (!processed) {
539 convertYUV420Planar16ToY410OrRGBA1010102(
540 (uint32_t*)dst, srcY, srcU, srcV, srcYStride, srcUStride, srcVStride,
541 dstStride / sizeof(uint32_t), width, height,
542 std::static_pointer_cast<const C2ColorAspectsStruct>(aspects));
543 }
544}
545
546void convertPlanar16ToP010(uint16_t* dstY, uint16_t* dstUV, const uint16_t* srcY,
547 const uint16_t* srcU, const uint16_t* srcV, size_t srcYStride,
548 size_t srcUStride, size_t srcVStride, size_t dstYStride,
549 size_t dstUStride, size_t dstVStride, size_t width, size_t height,
550 bool isMonochrome, CONV_FORMAT_T format, uint16_t* tmpFrameBuffer,
551 size_t tmpFrameBufferSize) {
552#if LIBYUV_VERSION >= 1779
553 if ((format == CONV_FORMAT_I444) || (format == CONV_FORMAT_I422)) {
554 // TODO(https://crbug.com/libyuv/952): replace this block with libyuv::I410ToP010
555 // and libyuv::I210ToP010 when they are available. Note it may be safe to alias dstY
556 // in I010ToP010, but the libyuv API doesn't make any guarantees.
557 const size_t tmpSize = dstYStride * height + dstUStride * align(height, 2);
558 CHECK(tmpSize <= tmpFrameBufferSize);
559
560 uint16_t* const tmpY = tmpFrameBuffer;
561 uint16_t* const tmpU = tmpY + dstYStride * height;
562 uint16_t* const tmpV = tmpU + dstUStride * align(height, 2) / 2;
563 if (format == CONV_FORMAT_I444) {
564 libyuv::I410ToI010(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, tmpY,
565 dstYStride, tmpU, dstUStride, tmpV, dstUStride, width, height);
566 } else {
567 libyuv::I210ToI010(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, tmpY,
568 dstYStride, tmpU, dstUStride, tmpV, dstUStride, width, height);
569 }
570 libyuv::I010ToP010(tmpY, dstYStride, tmpU, dstUStride, tmpV, dstVStride, dstY, dstYStride,
571 dstUV, dstUStride, width, height);
572 } else {
573 convertYUV420Planar16ToP010(dstY, dstUV, srcY, srcU, srcV, srcYStride, srcUStride,
574 srcVStride, dstYStride, dstUStride, width, height,
575 isMonochrome);
576 }
577#else // LIBYUV_VERSION < 1779
578 convertYUV420Planar16ToP010(dstY, dstUV, srcY, srcU, srcV, srcYStride, srcUStride, srcVStride,
579 dstYStride, dstUStride, width, height, isMonochrome);
580#endif // LIBYUV_VERSION >= 1779
581}
582
583void convertPlanar16ToYV12(uint8_t* dstY, uint8_t* dstU, uint8_t* dstV, const uint16_t* srcY,
584 const uint16_t* srcU, const uint16_t* srcV, size_t srcYStride,
585 size_t srcUStride, size_t srcVStride, size_t dstYStride,
586 size_t dstUStride, size_t dstVStride, size_t width, size_t height,
587 bool isMonochrome, CONV_FORMAT_T format, uint16_t* tmpFrameBuffer,
588 size_t tmpFrameBufferSize) {
589#if LIBYUV_VERSION >= 1779
590 if (format == CONV_FORMAT_I444) {
591 // TODO(https://crbug.com/libyuv/950): replace this block with libyuv::I410ToI420
592 // when it's available.
593 const size_t tmpSize = dstYStride * height + dstUStride * align(height, 2);
594 CHECK(tmpSize <= tmpFrameBufferSize);
595
596 uint16_t* const tmpY = tmpFrameBuffer;
597 uint16_t* const tmpU = tmpY + dstYStride * height;
598 uint16_t* const tmpV = tmpU + dstUStride * align(height, 2) / 2;
599 libyuv::I410ToI010(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, tmpY, dstYStride,
600 tmpU, dstUStride, tmpV, dstVStride, width, height);
601 libyuv::I010ToI420(tmpY, dstYStride, tmpU, dstUStride, tmpV, dstUStride, dstY, dstYStride,
602 dstU, dstUStride, dstV, dstVStride, width, height);
603 } else if (format == CONV_FORMAT_I422) {
604 libyuv::I210ToI420(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dstY, dstYStride,
605 dstU, dstUStride, dstV, dstVStride, width, height);
606 } else {
607 convertYUV420Planar16ToYV12(dstY, dstU, dstV, srcY, srcU, srcV, srcYStride, srcUStride,
608 srcVStride, dstYStride, dstUStride, width, height,
609 isMonochrome);
610 }
611#else // LIBYUV_VERSION < 1779
612 convertYUV420Planar16ToYV12(dstY, dstU, dstV, srcY, srcU, srcV, srcYStride, srcUStride,
613 srcVStride, dstYStride, dstUStride, width, height, isMonochrome);
614#endif // LIBYUV_VERSION >= 1779
615}
616
617void convertPlanar8ToYV12(uint8_t* dstY, uint8_t* dstU, uint8_t* dstV, const uint8_t* srcY,
618 const uint8_t* srcU, const uint8_t* srcV, size_t srcYStride,
619 size_t srcUStride, size_t srcVStride, size_t dstYStride,
620 size_t dstUStride, size_t dstVStride, uint32_t width, uint32_t height,
621 bool isMonochrome, CONV_FORMAT_T format) {
622 if (format == CONV_FORMAT_I444) {
623 libyuv::I444ToI420(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dstY, dstYStride,
624 dstU, dstUStride, dstV, dstVStride, width, height);
625 } else if (format == CONV_FORMAT_I422) {
626 libyuv::I422ToI420(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dstY, dstYStride,
627 dstU, dstUStride, dstV, dstVStride, width, height);
628 } else {
629 convertYUV420Planar8ToYV12(dstY, dstU, dstV, srcY, srcU, srcV, srcYStride, srcUStride,
630 srcVStride, dstYStride, dstUStride, dstVStride, width, height,
631 isMonochrome);
632 }
633}
Pawin Vongmasa36653902018-11-15 00:10:25 -0800634std::unique_ptr<C2Work> SimpleC2Component::WorkQueue::pop_front() {
635 std::unique_ptr<C2Work> work = std::move(mQueue.front().work);
636 mQueue.pop_front();
637 return work;
638}
639
640void SimpleC2Component::WorkQueue::push_back(std::unique_ptr<C2Work> work) {
641 mQueue.push_back({ std::move(work), NO_DRAIN });
642}
643
644bool SimpleC2Component::WorkQueue::empty() const {
645 return mQueue.empty();
646}
647
648void SimpleC2Component::WorkQueue::clear() {
649 mQueue.clear();
650}
651
652uint32_t SimpleC2Component::WorkQueue::drainMode() const {
653 return mQueue.front().drainMode;
654}
655
656void SimpleC2Component::WorkQueue::markDrain(uint32_t drainMode) {
657 mQueue.push_back({ nullptr, drainMode });
658}
659
660////////////////////////////////////////////////////////////////////////////////
661
662SimpleC2Component::WorkHandler::WorkHandler() : mRunning(false) {}
663
664void SimpleC2Component::WorkHandler::setComponent(
665 const std::shared_ptr<SimpleC2Component> &thiz) {
666 mThiz = thiz;
667}
668
669static void Reply(const sp<AMessage> &msg, int32_t *err = nullptr) {
670 sp<AReplyToken> replyId;
671 CHECK(msg->senderAwaitsResponse(&replyId));
672 sp<AMessage> reply = new AMessage;
673 if (err) {
674 reply->setInt32("err", *err);
675 }
676 reply->postReply(replyId);
677}
678
679void SimpleC2Component::WorkHandler::onMessageReceived(const sp<AMessage> &msg) {
680 std::shared_ptr<SimpleC2Component> thiz = mThiz.lock();
681 if (!thiz) {
682 ALOGD("component not yet set; msg = %s", msg->debugString().c_str());
683 sp<AReplyToken> replyId;
684 if (msg->senderAwaitsResponse(&replyId)) {
685 sp<AMessage> reply = new AMessage;
686 reply->setInt32("err", C2_CORRUPTED);
687 reply->postReply(replyId);
688 }
689 return;
690 }
691
692 switch (msg->what()) {
693 case kWhatProcess: {
694 if (mRunning) {
695 if (thiz->processQueue()) {
696 (new AMessage(kWhatProcess, this))->post();
697 }
698 } else {
699 ALOGV("Ignore process message as we're not running");
700 }
701 break;
702 }
703 case kWhatInit: {
704 int32_t err = thiz->onInit();
705 Reply(msg, &err);
706 [[fallthrough]];
707 }
708 case kWhatStart: {
709 mRunning = true;
710 break;
711 }
712 case kWhatStop: {
713 int32_t err = thiz->onStop();
Sungtak Lee810eace2021-04-12 22:16:44 -0700714 thiz->mOutputBlockPool.reset();
Sungtak Lee8175aea2024-05-07 22:32:38 +0000715 mRunning = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800716 Reply(msg, &err);
717 break;
718 }
719 case kWhatReset: {
720 thiz->onReset();
Sungtak Lee810eace2021-04-12 22:16:44 -0700721 thiz->mOutputBlockPool.reset();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800722 mRunning = false;
723 Reply(msg);
724 break;
725 }
726 case kWhatRelease: {
727 thiz->onRelease();
Sungtak Lee810eace2021-04-12 22:16:44 -0700728 thiz->mOutputBlockPool.reset();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800729 mRunning = false;
730 Reply(msg);
731 break;
732 }
733 default: {
734 ALOGD("Unrecognized msg: %d", msg->what());
735 break;
736 }
737 }
738}
739
Wonsik Kim0cb5a092019-01-03 16:38:22 -0800740class SimpleC2Component::BlockingBlockPool : public C2BlockPool {
741public:
742 BlockingBlockPool(const std::shared_ptr<C2BlockPool>& base): mBase{base} {}
743
744 virtual local_id_t getLocalId() const override {
745 return mBase->getLocalId();
746 }
747
748 virtual C2Allocator::id_t getAllocatorId() const override {
749 return mBase->getAllocatorId();
750 }
751
752 virtual c2_status_t fetchLinearBlock(
753 uint32_t capacity,
754 C2MemoryUsage usage,
755 std::shared_ptr<C2LinearBlock>* block) {
756 c2_status_t status;
757 do {
758 status = mBase->fetchLinearBlock(capacity, usage, block);
Sungtak Lee5f3fb6f2019-01-31 11:53:22 -0800759 } while (status == C2_BLOCKING);
Wonsik Kim0cb5a092019-01-03 16:38:22 -0800760 return status;
761 }
762
763 virtual c2_status_t fetchCircularBlock(
764 uint32_t capacity,
765 C2MemoryUsage usage,
766 std::shared_ptr<C2CircularBlock>* block) {
767 c2_status_t status;
768 do {
769 status = mBase->fetchCircularBlock(capacity, usage, block);
Sungtak Lee5f3fb6f2019-01-31 11:53:22 -0800770 } while (status == C2_BLOCKING);
Wonsik Kim0cb5a092019-01-03 16:38:22 -0800771 return status;
772 }
773
774 virtual c2_status_t fetchGraphicBlock(
775 uint32_t width, uint32_t height, uint32_t format,
776 C2MemoryUsage usage,
777 std::shared_ptr<C2GraphicBlock>* block) {
778 c2_status_t status;
779 do {
780 status = mBase->fetchGraphicBlock(width, height, format, usage,
781 block);
Sungtak Lee5f3fb6f2019-01-31 11:53:22 -0800782 } while (status == C2_BLOCKING);
Wonsik Kim0cb5a092019-01-03 16:38:22 -0800783 return status;
784 }
785
786private:
787 std::shared_ptr<C2BlockPool> mBase;
788};
789
Pawin Vongmasa36653902018-11-15 00:10:25 -0800790////////////////////////////////////////////////////////////////////////////////
791
792namespace {
793
794struct DummyReadView : public C2ReadView {
795 DummyReadView() : C2ReadView(C2_NO_INIT) {}
796};
797
798} // namespace
799
800SimpleC2Component::SimpleC2Component(
801 const std::shared_ptr<C2ComponentInterface> &intf)
802 : mDummyReadView(DummyReadView()),
803 mIntf(intf),
804 mLooper(new ALooper),
805 mHandler(new WorkHandler) {
806 mLooper->setName(intf->getName().c_str());
807 (void)mLooper->registerHandler(mHandler);
808 mLooper->start(false, false, ANDROID_PRIORITY_VIDEO);
809}
810
811SimpleC2Component::~SimpleC2Component() {
812 mLooper->unregisterHandler(mHandler->id());
813 (void)mLooper->stop();
814}
815
816c2_status_t SimpleC2Component::setListener_vb(
817 const std::shared_ptr<C2Component::Listener> &listener, c2_blocking_t mayBlock) {
818 mHandler->setComponent(shared_from_this());
819
820 Mutexed<ExecState>::Locked state(mExecState);
821 if (state->mState == RUNNING) {
822 if (listener) {
823 return C2_BAD_STATE;
824 } else if (!mayBlock) {
825 return C2_BLOCKING;
826 }
827 }
828 state->mListener = listener;
829 // TODO: wait for listener change to have taken place before returning
830 // (e.g. if there is an ongoing listener callback)
831 return C2_OK;
832}
833
834c2_status_t SimpleC2Component::queue_nb(std::list<std::unique_ptr<C2Work>> * const items) {
835 {
836 Mutexed<ExecState>::Locked state(mExecState);
837 if (state->mState != RUNNING) {
838 return C2_BAD_STATE;
839 }
840 }
841 bool queueWasEmpty = false;
842 {
843 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
844 queueWasEmpty = queue->empty();
845 while (!items->empty()) {
846 queue->push_back(std::move(items->front()));
847 items->pop_front();
848 }
849 }
850 if (queueWasEmpty) {
851 (new AMessage(WorkHandler::kWhatProcess, mHandler))->post();
852 }
853 return C2_OK;
854}
855
856c2_status_t SimpleC2Component::announce_nb(const std::vector<C2WorkOutline> &items) {
857 (void)items;
858 return C2_OMITTED;
859}
860
861c2_status_t SimpleC2Component::flush_sm(
862 flush_mode_t flushMode, std::list<std::unique_ptr<C2Work>>* const flushedWork) {
863 (void)flushMode;
864 {
865 Mutexed<ExecState>::Locked state(mExecState);
866 if (state->mState != RUNNING) {
867 return C2_BAD_STATE;
868 }
869 }
870 {
871 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
872 queue->incGeneration();
873 // TODO: queue->splicedBy(flushedWork, flushedWork->end());
874 while (!queue->empty()) {
875 std::unique_ptr<C2Work> work = queue->pop_front();
876 if (work) {
877 flushedWork->push_back(std::move(work));
878 }
879 }
Wonsik Kime1226f52019-04-04 15:24:41 -0700880 while (!queue->pending().empty()) {
881 flushedWork->push_back(std::move(queue->pending().begin()->second));
882 queue->pending().erase(queue->pending().begin());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800883 }
884 }
885
886 return C2_OK;
887}
888
889c2_status_t SimpleC2Component::drain_nb(drain_mode_t drainMode) {
890 if (drainMode == DRAIN_CHAIN) {
891 return C2_OMITTED;
892 }
893 {
894 Mutexed<ExecState>::Locked state(mExecState);
895 if (state->mState != RUNNING) {
896 return C2_BAD_STATE;
897 }
898 }
899 bool queueWasEmpty = false;
900 {
901 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
902 queueWasEmpty = queue->empty();
903 queue->markDrain(drainMode);
904 }
905 if (queueWasEmpty) {
906 (new AMessage(WorkHandler::kWhatProcess, mHandler))->post();
907 }
908
909 return C2_OK;
910}
911
912c2_status_t SimpleC2Component::start() {
913 Mutexed<ExecState>::Locked state(mExecState);
914 if (state->mState == RUNNING) {
915 return C2_BAD_STATE;
916 }
917 bool needsInit = (state->mState == UNINITIALIZED);
918 state.unlock();
919 if (needsInit) {
920 sp<AMessage> reply;
921 (new AMessage(WorkHandler::kWhatInit, mHandler))->postAndAwaitResponse(&reply);
922 int32_t err;
923 CHECK(reply->findInt32("err", &err));
924 if (err != C2_OK) {
925 return (c2_status_t)err;
926 }
927 } else {
928 (new AMessage(WorkHandler::kWhatStart, mHandler))->post();
929 }
930 state.lock();
931 state->mState = RUNNING;
932 return C2_OK;
933}
934
935c2_status_t SimpleC2Component::stop() {
936 ALOGV("stop");
937 {
938 Mutexed<ExecState>::Locked state(mExecState);
939 if (state->mState != RUNNING) {
940 return C2_BAD_STATE;
941 }
942 state->mState = STOPPED;
943 }
944 {
945 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
946 queue->clear();
Wonsik Kime1226f52019-04-04 15:24:41 -0700947 queue->pending().clear();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800948 }
949 sp<AMessage> reply;
950 (new AMessage(WorkHandler::kWhatStop, mHandler))->postAndAwaitResponse(&reply);
951 int32_t err;
952 CHECK(reply->findInt32("err", &err));
953 if (err != C2_OK) {
954 return (c2_status_t)err;
955 }
956 return C2_OK;
957}
958
959c2_status_t SimpleC2Component::reset() {
960 ALOGV("reset");
961 {
962 Mutexed<ExecState>::Locked state(mExecState);
963 state->mState = UNINITIALIZED;
964 }
965 {
966 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
967 queue->clear();
Wonsik Kime1226f52019-04-04 15:24:41 -0700968 queue->pending().clear();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800969 }
970 sp<AMessage> reply;
971 (new AMessage(WorkHandler::kWhatReset, mHandler))->postAndAwaitResponse(&reply);
972 return C2_OK;
973}
974
975c2_status_t SimpleC2Component::release() {
976 ALOGV("release");
977 sp<AMessage> reply;
978 (new AMessage(WorkHandler::kWhatRelease, mHandler))->postAndAwaitResponse(&reply);
979 return C2_OK;
980}
981
982std::shared_ptr<C2ComponentInterface> SimpleC2Component::intf() {
983 return mIntf;
984}
985
986namespace {
987
988std::list<std::unique_ptr<C2Work>> vec(std::unique_ptr<C2Work> &work) {
989 std::list<std::unique_ptr<C2Work>> ret;
990 ret.push_back(std::move(work));
991 return ret;
992}
993
994} // namespace
995
996void SimpleC2Component::finish(
997 uint64_t frameIndex, std::function<void(const std::unique_ptr<C2Work> &)> fillWork) {
998 std::unique_ptr<C2Work> work;
999 {
Wonsik Kime1226f52019-04-04 15:24:41 -07001000 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
1001 if (queue->pending().count(frameIndex) == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001002 ALOGW("unknown frame index: %" PRIu64, frameIndex);
1003 return;
1004 }
Wonsik Kime1226f52019-04-04 15:24:41 -07001005 work = std::move(queue->pending().at(frameIndex));
1006 queue->pending().erase(frameIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001007 }
1008 if (work) {
1009 fillWork(work);
1010 std::shared_ptr<C2Component::Listener> listener = mExecState.lock()->mListener;
1011 listener->onWorkDone_nb(shared_from_this(), vec(work));
1012 ALOGV("returning pending work");
1013 }
1014}
1015
1016void SimpleC2Component::cloneAndSend(
1017 uint64_t frameIndex,
1018 const std::unique_ptr<C2Work> &currentWork,
1019 std::function<void(const std::unique_ptr<C2Work> &)> fillWork) {
1020 std::unique_ptr<C2Work> work(new C2Work);
1021 if (currentWork->input.ordinal.frameIndex == frameIndex) {
1022 work->input.flags = currentWork->input.flags;
1023 work->input.ordinal = currentWork->input.ordinal;
1024 } else {
Wonsik Kime1226f52019-04-04 15:24:41 -07001025 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
1026 if (queue->pending().count(frameIndex) == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001027 ALOGW("unknown frame index: %" PRIu64, frameIndex);
1028 return;
1029 }
Wonsik Kime1226f52019-04-04 15:24:41 -07001030 work->input.flags = queue->pending().at(frameIndex)->input.flags;
1031 work->input.ordinal = queue->pending().at(frameIndex)->input.ordinal;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001032 }
1033 work->worklets.emplace_back(new C2Worklet);
1034 if (work) {
1035 fillWork(work);
1036 std::shared_ptr<C2Component::Listener> listener = mExecState.lock()->mListener;
1037 listener->onWorkDone_nb(shared_from_this(), vec(work));
1038 ALOGV("cloned and sending work");
1039 }
1040}
1041
1042bool SimpleC2Component::processQueue() {
1043 std::unique_ptr<C2Work> work;
1044 uint64_t generation;
1045 int32_t drainMode;
1046 bool isFlushPending = false;
1047 bool hasQueuedWork = false;
1048 {
1049 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
1050 if (queue->empty()) {
1051 return false;
1052 }
1053
1054 generation = queue->generation();
1055 drainMode = queue->drainMode();
1056 isFlushPending = queue->popPendingFlush();
1057 work = queue->pop_front();
1058 hasQueuedWork = !queue->empty();
1059 }
1060 if (isFlushPending) {
1061 ALOGV("processing pending flush");
1062 c2_status_t err = onFlush_sm();
1063 if (err != C2_OK) {
1064 ALOGD("flush err: %d", err);
1065 // TODO: error
1066 }
1067 }
1068
1069 if (!mOutputBlockPool) {
1070 c2_status_t err = [this] {
1071 // TODO: don't use query_vb
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001072 C2StreamBufferTypeSetting::output outputFormat(0u);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001073 std::vector<std::unique_ptr<C2Param>> params;
1074 c2_status_t err = intf()->query_vb(
1075 { &outputFormat },
1076 { C2PortBlockPoolsTuning::output::PARAM_TYPE },
1077 C2_DONT_BLOCK,
1078 &params);
1079 if (err != C2_OK && err != C2_BAD_INDEX) {
1080 ALOGD("query err = %d", err);
1081 return err;
1082 }
1083 C2BlockPool::local_id_t poolId =
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001084 outputFormat.value == C2BufferData::GRAPHIC
Pawin Vongmasa36653902018-11-15 00:10:25 -08001085 ? C2BlockPool::BASIC_GRAPHIC
1086 : C2BlockPool::BASIC_LINEAR;
1087 if (params.size()) {
1088 C2PortBlockPoolsTuning::output *outputPools =
1089 C2PortBlockPoolsTuning::output::From(params[0].get());
1090 if (outputPools && outputPools->flexCount() >= 1) {
1091 poolId = outputPools->m.values[0];
1092 }
1093 }
1094
Wonsik Kim0cb5a092019-01-03 16:38:22 -08001095 std::shared_ptr<C2BlockPool> blockPool;
1096 err = GetCodec2BlockPool(poolId, shared_from_this(), &blockPool);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001097 ALOGD("Using output block pool with poolID %llu => got %llu - %d",
1098 (unsigned long long)poolId,
1099 (unsigned long long)(
Wonsik Kim0cb5a092019-01-03 16:38:22 -08001100 blockPool ? blockPool->getLocalId() : 111000111),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001101 err);
Wonsik Kim0cb5a092019-01-03 16:38:22 -08001102 if (err == C2_OK) {
1103 mOutputBlockPool = std::make_shared<BlockingBlockPool>(blockPool);
1104 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001105 return err;
1106 }();
1107 if (err != C2_OK) {
1108 Mutexed<ExecState>::Locked state(mExecState);
1109 std::shared_ptr<C2Component::Listener> listener = state->mListener;
1110 state.unlock();
1111 listener->onError_nb(shared_from_this(), err);
1112 return hasQueuedWork;
1113 }
1114 }
1115
1116 if (!work) {
1117 c2_status_t err = drain(drainMode, mOutputBlockPool);
1118 if (err != C2_OK) {
1119 Mutexed<ExecState>::Locked state(mExecState);
1120 std::shared_ptr<C2Component::Listener> listener = state->mListener;
1121 state.unlock();
1122 listener->onError_nb(shared_from_this(), err);
1123 }
1124 return hasQueuedWork;
1125 }
1126
1127 {
1128 std::vector<C2Param *> updates;
1129 for (const std::unique_ptr<C2Param> &param: work->input.configUpdate) {
1130 if (param) {
1131 updates.emplace_back(param.get());
1132 }
1133 }
1134 if (!updates.empty()) {
1135 std::vector<std::unique_ptr<C2SettingResult>> failures;
1136 c2_status_t err = intf()->config_vb(updates, C2_MAY_BLOCK, &failures);
1137 ALOGD("applied %zu configUpdates => %s (%d)", updates.size(), asString(err), err);
1138 }
1139 }
1140
1141 ALOGV("start processing frame #%" PRIu64, work->input.ordinal.frameIndex.peeku());
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001142 // If input buffer list is not empty, it means we have some input to process on.
1143 // However, input could be a null buffer. In such case, clear the buffer list
1144 // before making call to process().
1145 if (!work->input.buffers.empty() && !work->input.buffers[0]) {
1146 ALOGD("Encountered null input buffer. Clearing the input buffer");
1147 work->input.buffers.clear();
1148 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001149 process(work, mOutputBlockPool);
1150 ALOGV("processed frame #%" PRIu64, work->input.ordinal.frameIndex.peeku());
Wonsik Kime1226f52019-04-04 15:24:41 -07001151 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
1152 if (queue->generation() != generation) {
1153 ALOGD("work form old generation: was %" PRIu64 " now %" PRIu64,
1154 queue->generation(), generation);
1155 work->result = C2_NOT_FOUND;
1156 queue.unlock();
1157
1158 Mutexed<ExecState>::Locked state(mExecState);
1159 std::shared_ptr<C2Component::Listener> listener = state->mListener;
1160 state.unlock();
1161 listener->onWorkDone_nb(shared_from_this(), vec(work));
1162 return hasQueuedWork;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001163 }
1164 if (work->workletsProcessed != 0u) {
Wonsik Kime1226f52019-04-04 15:24:41 -07001165 queue.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001166 Mutexed<ExecState>::Locked state(mExecState);
1167 ALOGV("returning this work");
1168 std::shared_ptr<C2Component::Listener> listener = state->mListener;
1169 state.unlock();
1170 listener->onWorkDone_nb(shared_from_this(), vec(work));
1171 } else {
1172 ALOGV("queue pending work");
1173 work->input.buffers.clear();
1174 std::unique_ptr<C2Work> unexpected;
Wonsik Kime1226f52019-04-04 15:24:41 -07001175
1176 uint64_t frameIndex = work->input.ordinal.frameIndex.peeku();
1177 if (queue->pending().count(frameIndex) != 0) {
1178 unexpected = std::move(queue->pending().at(frameIndex));
1179 queue->pending().erase(frameIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001180 }
Wonsik Kime1226f52019-04-04 15:24:41 -07001181 (void)queue->pending().insert({ frameIndex, std::move(work) });
1182
1183 queue.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001184 if (unexpected) {
1185 ALOGD("unexpected pending work");
1186 unexpected->result = C2_CORRUPTED;
1187 Mutexed<ExecState>::Locked state(mExecState);
1188 std::shared_ptr<C2Component::Listener> listener = state->mListener;
1189 state.unlock();
1190 listener->onWorkDone_nb(shared_from_this(), vec(unexpected));
1191 }
1192 }
1193 return hasQueuedWork;
1194}
1195
Harish Mahendrakar749a74c2022-01-27 16:47:09 -08001196int SimpleC2Component::getHalPixelFormatForBitDepth10(bool allowRGBA1010102) {
1197 // Save supported hal pixel formats for bit depth of 10, the first time this is called
1198 if (!mBitDepth10HalPixelFormats.size()) {
1199 std::vector<int> halPixelFormats;
Harish Mahendrakarf5dec502022-04-13 15:53:55 -07001200 halPixelFormats.push_back(HAL_PIXEL_FORMAT_YCBCR_P010);
1201
Harish Mahendrakar749a74c2022-01-27 16:47:09 -08001202 // since allowRGBA1010102 can chance in each call, but mBitDepth10HalPixelFormats
1203 // is populated only once, allowRGBA1010102 is not considered at this stage.
1204 halPixelFormats.push_back(HAL_PIXEL_FORMAT_RGBA_1010102);
1205
1206 for (int halPixelFormat : halPixelFormats) {
Harish Mahendrakarf5dec502022-04-13 15:53:55 -07001207 if (isHalPixelFormatSupported((AHardwareBuffer_Format)halPixelFormat)) {
Harish Mahendrakar749a74c2022-01-27 16:47:09 -08001208 mBitDepth10HalPixelFormats.push_back(halPixelFormat);
1209 }
1210 }
1211 // Add YV12 in the end as a fall-back option
1212 mBitDepth10HalPixelFormats.push_back(HAL_PIXEL_FORMAT_YV12);
1213 }
Harish Mahendrakar5c467652022-04-28 19:47:28 -07001214 // From Android T onwards, HAL_PIXEL_FORMAT_RGBA_1010102 corresponds to true
1215 // RGBA 1010102 format unlike earlier versions where it was used to represent
1216 // YUVA 1010102 data
1217 if (!isAtLeastT()) {
1218 // When RGBA1010102 is not allowed and if the first supported hal pixel is format is
1219 // HAL_PIXEL_FORMAT_RGBA_1010102, then return HAL_PIXEL_FORMAT_YV12
1220 if (!allowRGBA1010102 && mBitDepth10HalPixelFormats[0] == HAL_PIXEL_FORMAT_RGBA_1010102) {
1221 return HAL_PIXEL_FORMAT_YV12;
1222 }
Harish Mahendrakar749a74c2022-01-27 16:47:09 -08001223 }
1224 // Return the first entry from supported formats
1225 return mBitDepth10HalPixelFormats[0];
1226}
Pawin Vongmasa36653902018-11-15 00:10:25 -08001227std::shared_ptr<C2Buffer> SimpleC2Component::createLinearBuffer(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001228 const std::shared_ptr<C2LinearBlock> &block, size_t offset, size_t size) {
1229 return C2Buffer::CreateLinearBuffer(block->share(offset, size, ::C2Fence()));
1230}
1231
1232std::shared_ptr<C2Buffer> SimpleC2Component::createGraphicBuffer(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001233 const std::shared_ptr<C2GraphicBlock> &block, const C2Rect &crop) {
1234 return C2Buffer::CreateGraphicBuffer(block->share(crop, ::C2Fence()));
1235}
1236
1237} // namespace android