blob: 06a21f61838ec12d4908b67b832d010e30f66693 [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();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800715 Reply(msg, &err);
716 break;
717 }
718 case kWhatReset: {
719 thiz->onReset();
Sungtak Lee810eace2021-04-12 22:16:44 -0700720 thiz->mOutputBlockPool.reset();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800721 mRunning = false;
722 Reply(msg);
723 break;
724 }
725 case kWhatRelease: {
726 thiz->onRelease();
Sungtak Lee810eace2021-04-12 22:16:44 -0700727 thiz->mOutputBlockPool.reset();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800728 mRunning = false;
729 Reply(msg);
730 break;
731 }
732 default: {
733 ALOGD("Unrecognized msg: %d", msg->what());
734 break;
735 }
736 }
737}
738
Wonsik Kim0cb5a092019-01-03 16:38:22 -0800739class SimpleC2Component::BlockingBlockPool : public C2BlockPool {
740public:
741 BlockingBlockPool(const std::shared_ptr<C2BlockPool>& base): mBase{base} {}
742
743 virtual local_id_t getLocalId() const override {
744 return mBase->getLocalId();
745 }
746
747 virtual C2Allocator::id_t getAllocatorId() const override {
748 return mBase->getAllocatorId();
749 }
750
751 virtual c2_status_t fetchLinearBlock(
752 uint32_t capacity,
753 C2MemoryUsage usage,
754 std::shared_ptr<C2LinearBlock>* block) {
755 c2_status_t status;
756 do {
757 status = mBase->fetchLinearBlock(capacity, usage, block);
Sungtak Lee5f3fb6f2019-01-31 11:53:22 -0800758 } while (status == C2_BLOCKING);
Wonsik Kim0cb5a092019-01-03 16:38:22 -0800759 return status;
760 }
761
762 virtual c2_status_t fetchCircularBlock(
763 uint32_t capacity,
764 C2MemoryUsage usage,
765 std::shared_ptr<C2CircularBlock>* block) {
766 c2_status_t status;
767 do {
768 status = mBase->fetchCircularBlock(capacity, usage, block);
Sungtak Lee5f3fb6f2019-01-31 11:53:22 -0800769 } while (status == C2_BLOCKING);
Wonsik Kim0cb5a092019-01-03 16:38:22 -0800770 return status;
771 }
772
773 virtual c2_status_t fetchGraphicBlock(
774 uint32_t width, uint32_t height, uint32_t format,
775 C2MemoryUsage usage,
776 std::shared_ptr<C2GraphicBlock>* block) {
777 c2_status_t status;
778 do {
779 status = mBase->fetchGraphicBlock(width, height, format, usage,
780 block);
Sungtak Lee5f3fb6f2019-01-31 11:53:22 -0800781 } while (status == C2_BLOCKING);
Wonsik Kim0cb5a092019-01-03 16:38:22 -0800782 return status;
783 }
784
785private:
786 std::shared_ptr<C2BlockPool> mBase;
787};
788
Pawin Vongmasa36653902018-11-15 00:10:25 -0800789////////////////////////////////////////////////////////////////////////////////
790
791namespace {
792
793struct DummyReadView : public C2ReadView {
794 DummyReadView() : C2ReadView(C2_NO_INIT) {}
795};
796
797} // namespace
798
799SimpleC2Component::SimpleC2Component(
800 const std::shared_ptr<C2ComponentInterface> &intf)
801 : mDummyReadView(DummyReadView()),
802 mIntf(intf),
803 mLooper(new ALooper),
804 mHandler(new WorkHandler) {
805 mLooper->setName(intf->getName().c_str());
806 (void)mLooper->registerHandler(mHandler);
807 mLooper->start(false, false, ANDROID_PRIORITY_VIDEO);
808}
809
810SimpleC2Component::~SimpleC2Component() {
811 mLooper->unregisterHandler(mHandler->id());
812 (void)mLooper->stop();
813}
814
815c2_status_t SimpleC2Component::setListener_vb(
816 const std::shared_ptr<C2Component::Listener> &listener, c2_blocking_t mayBlock) {
817 mHandler->setComponent(shared_from_this());
818
819 Mutexed<ExecState>::Locked state(mExecState);
820 if (state->mState == RUNNING) {
821 if (listener) {
822 return C2_BAD_STATE;
823 } else if (!mayBlock) {
824 return C2_BLOCKING;
825 }
826 }
827 state->mListener = listener;
828 // TODO: wait for listener change to have taken place before returning
829 // (e.g. if there is an ongoing listener callback)
830 return C2_OK;
831}
832
833c2_status_t SimpleC2Component::queue_nb(std::list<std::unique_ptr<C2Work>> * const items) {
834 {
835 Mutexed<ExecState>::Locked state(mExecState);
836 if (state->mState != RUNNING) {
837 return C2_BAD_STATE;
838 }
839 }
840 bool queueWasEmpty = false;
841 {
842 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
843 queueWasEmpty = queue->empty();
844 while (!items->empty()) {
845 queue->push_back(std::move(items->front()));
846 items->pop_front();
847 }
848 }
849 if (queueWasEmpty) {
850 (new AMessage(WorkHandler::kWhatProcess, mHandler))->post();
851 }
852 return C2_OK;
853}
854
855c2_status_t SimpleC2Component::announce_nb(const std::vector<C2WorkOutline> &items) {
856 (void)items;
857 return C2_OMITTED;
858}
859
860c2_status_t SimpleC2Component::flush_sm(
861 flush_mode_t flushMode, std::list<std::unique_ptr<C2Work>>* const flushedWork) {
862 (void)flushMode;
863 {
864 Mutexed<ExecState>::Locked state(mExecState);
865 if (state->mState != RUNNING) {
866 return C2_BAD_STATE;
867 }
868 }
869 {
870 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
871 queue->incGeneration();
872 // TODO: queue->splicedBy(flushedWork, flushedWork->end());
873 while (!queue->empty()) {
874 std::unique_ptr<C2Work> work = queue->pop_front();
875 if (work) {
876 flushedWork->push_back(std::move(work));
877 }
878 }
Wonsik Kime1226f52019-04-04 15:24:41 -0700879 while (!queue->pending().empty()) {
880 flushedWork->push_back(std::move(queue->pending().begin()->second));
881 queue->pending().erase(queue->pending().begin());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800882 }
883 }
884
885 return C2_OK;
886}
887
888c2_status_t SimpleC2Component::drain_nb(drain_mode_t drainMode) {
889 if (drainMode == DRAIN_CHAIN) {
890 return C2_OMITTED;
891 }
892 {
893 Mutexed<ExecState>::Locked state(mExecState);
894 if (state->mState != RUNNING) {
895 return C2_BAD_STATE;
896 }
897 }
898 bool queueWasEmpty = false;
899 {
900 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
901 queueWasEmpty = queue->empty();
902 queue->markDrain(drainMode);
903 }
904 if (queueWasEmpty) {
905 (new AMessage(WorkHandler::kWhatProcess, mHandler))->post();
906 }
907
908 return C2_OK;
909}
910
911c2_status_t SimpleC2Component::start() {
912 Mutexed<ExecState>::Locked state(mExecState);
913 if (state->mState == RUNNING) {
914 return C2_BAD_STATE;
915 }
916 bool needsInit = (state->mState == UNINITIALIZED);
917 state.unlock();
918 if (needsInit) {
919 sp<AMessage> reply;
920 (new AMessage(WorkHandler::kWhatInit, mHandler))->postAndAwaitResponse(&reply);
921 int32_t err;
922 CHECK(reply->findInt32("err", &err));
923 if (err != C2_OK) {
924 return (c2_status_t)err;
925 }
926 } else {
927 (new AMessage(WorkHandler::kWhatStart, mHandler))->post();
928 }
929 state.lock();
930 state->mState = RUNNING;
931 return C2_OK;
932}
933
934c2_status_t SimpleC2Component::stop() {
935 ALOGV("stop");
936 {
937 Mutexed<ExecState>::Locked state(mExecState);
938 if (state->mState != RUNNING) {
939 return C2_BAD_STATE;
940 }
941 state->mState = STOPPED;
942 }
943 {
944 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
945 queue->clear();
Wonsik Kime1226f52019-04-04 15:24:41 -0700946 queue->pending().clear();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800947 }
948 sp<AMessage> reply;
949 (new AMessage(WorkHandler::kWhatStop, mHandler))->postAndAwaitResponse(&reply);
950 int32_t err;
951 CHECK(reply->findInt32("err", &err));
952 if (err != C2_OK) {
953 return (c2_status_t)err;
954 }
955 return C2_OK;
956}
957
958c2_status_t SimpleC2Component::reset() {
959 ALOGV("reset");
960 {
961 Mutexed<ExecState>::Locked state(mExecState);
962 state->mState = UNINITIALIZED;
963 }
964 {
965 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
966 queue->clear();
Wonsik Kime1226f52019-04-04 15:24:41 -0700967 queue->pending().clear();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800968 }
969 sp<AMessage> reply;
970 (new AMessage(WorkHandler::kWhatReset, mHandler))->postAndAwaitResponse(&reply);
971 return C2_OK;
972}
973
974c2_status_t SimpleC2Component::release() {
975 ALOGV("release");
976 sp<AMessage> reply;
977 (new AMessage(WorkHandler::kWhatRelease, mHandler))->postAndAwaitResponse(&reply);
978 return C2_OK;
979}
980
981std::shared_ptr<C2ComponentInterface> SimpleC2Component::intf() {
982 return mIntf;
983}
984
985namespace {
986
987std::list<std::unique_ptr<C2Work>> vec(std::unique_ptr<C2Work> &work) {
988 std::list<std::unique_ptr<C2Work>> ret;
989 ret.push_back(std::move(work));
990 return ret;
991}
992
993} // namespace
994
995void SimpleC2Component::finish(
996 uint64_t frameIndex, std::function<void(const std::unique_ptr<C2Work> &)> fillWork) {
997 std::unique_ptr<C2Work> work;
998 {
Wonsik Kime1226f52019-04-04 15:24:41 -0700999 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
1000 if (queue->pending().count(frameIndex) == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001001 ALOGW("unknown frame index: %" PRIu64, frameIndex);
1002 return;
1003 }
Wonsik Kime1226f52019-04-04 15:24:41 -07001004 work = std::move(queue->pending().at(frameIndex));
1005 queue->pending().erase(frameIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001006 }
1007 if (work) {
1008 fillWork(work);
1009 std::shared_ptr<C2Component::Listener> listener = mExecState.lock()->mListener;
1010 listener->onWorkDone_nb(shared_from_this(), vec(work));
1011 ALOGV("returning pending work");
1012 }
1013}
1014
1015void SimpleC2Component::cloneAndSend(
1016 uint64_t frameIndex,
1017 const std::unique_ptr<C2Work> &currentWork,
1018 std::function<void(const std::unique_ptr<C2Work> &)> fillWork) {
1019 std::unique_ptr<C2Work> work(new C2Work);
1020 if (currentWork->input.ordinal.frameIndex == frameIndex) {
1021 work->input.flags = currentWork->input.flags;
1022 work->input.ordinal = currentWork->input.ordinal;
1023 } else {
Wonsik Kime1226f52019-04-04 15:24:41 -07001024 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
1025 if (queue->pending().count(frameIndex) == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001026 ALOGW("unknown frame index: %" PRIu64, frameIndex);
1027 return;
1028 }
Wonsik Kime1226f52019-04-04 15:24:41 -07001029 work->input.flags = queue->pending().at(frameIndex)->input.flags;
1030 work->input.ordinal = queue->pending().at(frameIndex)->input.ordinal;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001031 }
1032 work->worklets.emplace_back(new C2Worklet);
1033 if (work) {
1034 fillWork(work);
1035 std::shared_ptr<C2Component::Listener> listener = mExecState.lock()->mListener;
1036 listener->onWorkDone_nb(shared_from_this(), vec(work));
1037 ALOGV("cloned and sending work");
1038 }
1039}
1040
1041bool SimpleC2Component::processQueue() {
1042 std::unique_ptr<C2Work> work;
1043 uint64_t generation;
1044 int32_t drainMode;
1045 bool isFlushPending = false;
1046 bool hasQueuedWork = false;
1047 {
1048 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
1049 if (queue->empty()) {
1050 return false;
1051 }
1052
1053 generation = queue->generation();
1054 drainMode = queue->drainMode();
1055 isFlushPending = queue->popPendingFlush();
1056 work = queue->pop_front();
1057 hasQueuedWork = !queue->empty();
1058 }
1059 if (isFlushPending) {
1060 ALOGV("processing pending flush");
1061 c2_status_t err = onFlush_sm();
1062 if (err != C2_OK) {
1063 ALOGD("flush err: %d", err);
1064 // TODO: error
1065 }
1066 }
1067
1068 if (!mOutputBlockPool) {
1069 c2_status_t err = [this] {
1070 // TODO: don't use query_vb
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001071 C2StreamBufferTypeSetting::output outputFormat(0u);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001072 std::vector<std::unique_ptr<C2Param>> params;
1073 c2_status_t err = intf()->query_vb(
1074 { &outputFormat },
1075 { C2PortBlockPoolsTuning::output::PARAM_TYPE },
1076 C2_DONT_BLOCK,
1077 &params);
1078 if (err != C2_OK && err != C2_BAD_INDEX) {
1079 ALOGD("query err = %d", err);
1080 return err;
1081 }
1082 C2BlockPool::local_id_t poolId =
Lajos Molnar3bb81cd2019-02-20 15:10:30 -08001083 outputFormat.value == C2BufferData::GRAPHIC
Pawin Vongmasa36653902018-11-15 00:10:25 -08001084 ? C2BlockPool::BASIC_GRAPHIC
1085 : C2BlockPool::BASIC_LINEAR;
1086 if (params.size()) {
1087 C2PortBlockPoolsTuning::output *outputPools =
1088 C2PortBlockPoolsTuning::output::From(params[0].get());
1089 if (outputPools && outputPools->flexCount() >= 1) {
1090 poolId = outputPools->m.values[0];
1091 }
1092 }
1093
Wonsik Kim0cb5a092019-01-03 16:38:22 -08001094 std::shared_ptr<C2BlockPool> blockPool;
1095 err = GetCodec2BlockPool(poolId, shared_from_this(), &blockPool);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001096 ALOGD("Using output block pool with poolID %llu => got %llu - %d",
1097 (unsigned long long)poolId,
1098 (unsigned long long)(
Wonsik Kim0cb5a092019-01-03 16:38:22 -08001099 blockPool ? blockPool->getLocalId() : 111000111),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001100 err);
Wonsik Kim0cb5a092019-01-03 16:38:22 -08001101 if (err == C2_OK) {
1102 mOutputBlockPool = std::make_shared<BlockingBlockPool>(blockPool);
1103 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001104 return err;
1105 }();
1106 if (err != C2_OK) {
1107 Mutexed<ExecState>::Locked state(mExecState);
1108 std::shared_ptr<C2Component::Listener> listener = state->mListener;
1109 state.unlock();
1110 listener->onError_nb(shared_from_this(), err);
1111 return hasQueuedWork;
1112 }
1113 }
1114
1115 if (!work) {
1116 c2_status_t err = drain(drainMode, mOutputBlockPool);
1117 if (err != C2_OK) {
1118 Mutexed<ExecState>::Locked state(mExecState);
1119 std::shared_ptr<C2Component::Listener> listener = state->mListener;
1120 state.unlock();
1121 listener->onError_nb(shared_from_this(), err);
1122 }
1123 return hasQueuedWork;
1124 }
1125
1126 {
1127 std::vector<C2Param *> updates;
1128 for (const std::unique_ptr<C2Param> &param: work->input.configUpdate) {
1129 if (param) {
1130 updates.emplace_back(param.get());
1131 }
1132 }
1133 if (!updates.empty()) {
1134 std::vector<std::unique_ptr<C2SettingResult>> failures;
1135 c2_status_t err = intf()->config_vb(updates, C2_MAY_BLOCK, &failures);
1136 ALOGD("applied %zu configUpdates => %s (%d)", updates.size(), asString(err), err);
1137 }
1138 }
1139
1140 ALOGV("start processing frame #%" PRIu64, work->input.ordinal.frameIndex.peeku());
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001141 // If input buffer list is not empty, it means we have some input to process on.
1142 // However, input could be a null buffer. In such case, clear the buffer list
1143 // before making call to process().
1144 if (!work->input.buffers.empty() && !work->input.buffers[0]) {
1145 ALOGD("Encountered null input buffer. Clearing the input buffer");
1146 work->input.buffers.clear();
1147 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001148 process(work, mOutputBlockPool);
1149 ALOGV("processed frame #%" PRIu64, work->input.ordinal.frameIndex.peeku());
Wonsik Kime1226f52019-04-04 15:24:41 -07001150 Mutexed<WorkQueue>::Locked queue(mWorkQueue);
1151 if (queue->generation() != generation) {
1152 ALOGD("work form old generation: was %" PRIu64 " now %" PRIu64,
1153 queue->generation(), generation);
1154 work->result = C2_NOT_FOUND;
1155 queue.unlock();
1156
1157 Mutexed<ExecState>::Locked state(mExecState);
1158 std::shared_ptr<C2Component::Listener> listener = state->mListener;
1159 state.unlock();
1160 listener->onWorkDone_nb(shared_from_this(), vec(work));
1161 return hasQueuedWork;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001162 }
1163 if (work->workletsProcessed != 0u) {
Wonsik Kime1226f52019-04-04 15:24:41 -07001164 queue.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001165 Mutexed<ExecState>::Locked state(mExecState);
1166 ALOGV("returning this work");
1167 std::shared_ptr<C2Component::Listener> listener = state->mListener;
1168 state.unlock();
1169 listener->onWorkDone_nb(shared_from_this(), vec(work));
1170 } else {
1171 ALOGV("queue pending work");
1172 work->input.buffers.clear();
1173 std::unique_ptr<C2Work> unexpected;
Wonsik Kime1226f52019-04-04 15:24:41 -07001174
1175 uint64_t frameIndex = work->input.ordinal.frameIndex.peeku();
1176 if (queue->pending().count(frameIndex) != 0) {
1177 unexpected = std::move(queue->pending().at(frameIndex));
1178 queue->pending().erase(frameIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001179 }
Wonsik Kime1226f52019-04-04 15:24:41 -07001180 (void)queue->pending().insert({ frameIndex, std::move(work) });
1181
1182 queue.unlock();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001183 if (unexpected) {
1184 ALOGD("unexpected pending work");
1185 unexpected->result = C2_CORRUPTED;
1186 Mutexed<ExecState>::Locked state(mExecState);
1187 std::shared_ptr<C2Component::Listener> listener = state->mListener;
1188 state.unlock();
1189 listener->onWorkDone_nb(shared_from_this(), vec(unexpected));
1190 }
1191 }
1192 return hasQueuedWork;
1193}
1194
Harish Mahendrakar749a74c2022-01-27 16:47:09 -08001195int SimpleC2Component::getHalPixelFormatForBitDepth10(bool allowRGBA1010102) {
1196 // Save supported hal pixel formats for bit depth of 10, the first time this is called
1197 if (!mBitDepth10HalPixelFormats.size()) {
1198 std::vector<int> halPixelFormats;
Harish Mahendrakarf5dec502022-04-13 15:53:55 -07001199 halPixelFormats.push_back(HAL_PIXEL_FORMAT_YCBCR_P010);
1200
Harish Mahendrakar749a74c2022-01-27 16:47:09 -08001201 // since allowRGBA1010102 can chance in each call, but mBitDepth10HalPixelFormats
1202 // is populated only once, allowRGBA1010102 is not considered at this stage.
1203 halPixelFormats.push_back(HAL_PIXEL_FORMAT_RGBA_1010102);
1204
1205 for (int halPixelFormat : halPixelFormats) {
Harish Mahendrakarf5dec502022-04-13 15:53:55 -07001206 if (isHalPixelFormatSupported((AHardwareBuffer_Format)halPixelFormat)) {
Harish Mahendrakar749a74c2022-01-27 16:47:09 -08001207 mBitDepth10HalPixelFormats.push_back(halPixelFormat);
1208 }
1209 }
1210 // Add YV12 in the end as a fall-back option
1211 mBitDepth10HalPixelFormats.push_back(HAL_PIXEL_FORMAT_YV12);
1212 }
Harish Mahendrakar5c467652022-04-28 19:47:28 -07001213 // From Android T onwards, HAL_PIXEL_FORMAT_RGBA_1010102 corresponds to true
1214 // RGBA 1010102 format unlike earlier versions where it was used to represent
1215 // YUVA 1010102 data
1216 if (!isAtLeastT()) {
1217 // When RGBA1010102 is not allowed and if the first supported hal pixel is format is
1218 // HAL_PIXEL_FORMAT_RGBA_1010102, then return HAL_PIXEL_FORMAT_YV12
1219 if (!allowRGBA1010102 && mBitDepth10HalPixelFormats[0] == HAL_PIXEL_FORMAT_RGBA_1010102) {
1220 return HAL_PIXEL_FORMAT_YV12;
1221 }
Harish Mahendrakar749a74c2022-01-27 16:47:09 -08001222 }
1223 // Return the first entry from supported formats
1224 return mBitDepth10HalPixelFormats[0];
1225}
Pawin Vongmasa36653902018-11-15 00:10:25 -08001226std::shared_ptr<C2Buffer> SimpleC2Component::createLinearBuffer(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001227 const std::shared_ptr<C2LinearBlock> &block, size_t offset, size_t size) {
1228 return C2Buffer::CreateLinearBuffer(block->share(offset, size, ::C2Fence()));
1229}
1230
1231std::shared_ptr<C2Buffer> SimpleC2Component::createGraphicBuffer(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001232 const std::shared_ptr<C2GraphicBlock> &block, const C2Rect &crop) {
1233 return C2Buffer::CreateGraphicBuffer(block->share(crop, ::C2Fence()));
1234}
1235
1236} // namespace android