blob: 2fa4f2557e9d6a9a965427a546513f3dc0e8d16a [file] [log] [blame]
Vignesh Venkatasubramanianb6d383d2019-06-10 15:11:58 -07001/*
2 * Copyright (C) 2019 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 "C2SoftGav1Dec"
19#include "C2SoftGav1Dec.h"
20
21#include <C2Debug.h>
22#include <C2PlatformSupport.h>
23#include <SimpleC2Interface.h>
24#include <log/log.h>
25#include <media/stagefright/foundation/AUtils.h>
26#include <media/stagefright/foundation/MediaDefs.h>
27
28namespace android {
29
Ray Essickc2cc4372019-08-21 14:02:28 -070030// codecname set and passed in as a compile flag from Android.bp
31constexpr char COMPONENT_NAME[] = CODECNAME;
Vignesh Venkatasubramanianb6d383d2019-06-10 15:11:58 -070032
33class C2SoftGav1Dec::IntfImpl : public SimpleInterface<void>::BaseParams {
34 public:
35 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
36 : SimpleInterface<void>::BaseParams(
37 helper, COMPONENT_NAME, C2Component::KIND_DECODER,
38 C2Component::DOMAIN_VIDEO, MEDIA_MIMETYPE_VIDEO_AV1) {
39 noPrivateBuffers(); // TODO: account for our buffers here.
40 noInputReferences();
41 noOutputReferences();
42 noInputLatency();
43 noTimeStretch();
44
45 addParameter(DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
46 .withConstValue(new C2ComponentAttributesSetting(
47 C2Component::ATTRIB_IS_TEMPORAL))
48 .build());
49
50 addParameter(
51 DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE)
52 .withDefault(new C2StreamPictureSizeInfo::output(0u, 320, 240))
53 .withFields({
54 C2F(mSize, width).inRange(2, 2048, 2),
55 C2F(mSize, height).inRange(2, 2048, 2),
56 })
57 .withSetter(SizeSetter)
58 .build());
59
60 addParameter(DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
61 .withDefault(new C2StreamProfileLevelInfo::input(
62 0u, C2Config::PROFILE_AV1_0, C2Config::LEVEL_AV1_2_1))
63 .withFields({C2F(mProfileLevel, profile)
64 .oneOf({C2Config::PROFILE_AV1_0,
65 C2Config::PROFILE_AV1_1}),
66 C2F(mProfileLevel, level)
67 .oneOf({
Harish Mahendrakar1ad8c3b2021-06-04 15:42:31 -070068 C2Config::LEVEL_AV1_2, C2Config::LEVEL_AV1_2_1,
69 C2Config::LEVEL_AV1_2_2, C2Config::LEVEL_AV1_2_3,
70 C2Config::LEVEL_AV1_3, C2Config::LEVEL_AV1_3_1,
71 C2Config::LEVEL_AV1_3_2, C2Config::LEVEL_AV1_3_3,
72 C2Config::LEVEL_AV1_4, C2Config::LEVEL_AV1_4_1,
73 C2Config::LEVEL_AV1_4_2, C2Config::LEVEL_AV1_4_3,
74 C2Config::LEVEL_AV1_5, C2Config::LEVEL_AV1_5_1,
75 C2Config::LEVEL_AV1_5_2, C2Config::LEVEL_AV1_5_3,
Vignesh Venkatasubramanianb6d383d2019-06-10 15:11:58 -070076 })})
77 .withSetter(ProfileLevelSetter, mSize)
78 .build());
79
80 mHdr10PlusInfoInput = C2StreamHdr10PlusInfo::input::AllocShared(0);
81 addParameter(
82 DefineParam(mHdr10PlusInfoInput, C2_PARAMKEY_INPUT_HDR10_PLUS_INFO)
83 .withDefault(mHdr10PlusInfoInput)
84 .withFields({
85 C2F(mHdr10PlusInfoInput, m.value).any(),
86 })
87 .withSetter(Hdr10PlusInfoInputSetter)
88 .build());
89
90 mHdr10PlusInfoOutput = C2StreamHdr10PlusInfo::output::AllocShared(0);
91 addParameter(
92 DefineParam(mHdr10PlusInfoOutput, C2_PARAMKEY_OUTPUT_HDR10_PLUS_INFO)
93 .withDefault(mHdr10PlusInfoOutput)
94 .withFields({
95 C2F(mHdr10PlusInfoOutput, m.value).any(),
96 })
97 .withSetter(Hdr10PlusInfoOutputSetter)
98 .build());
99
100 addParameter(
101 DefineParam(mMaxSize, C2_PARAMKEY_MAX_PICTURE_SIZE)
102 .withDefault(new C2StreamMaxPictureSizeTuning::output(0u, 320, 240))
103 .withFields({
104 C2F(mSize, width).inRange(2, 2048, 2),
105 C2F(mSize, height).inRange(2, 2048, 2),
106 })
107 .withSetter(MaxPictureSizeSetter, mSize)
108 .build());
109
110 addParameter(DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
111 .withDefault(new C2StreamMaxBufferSizeInfo::input(
112 0u, 320 * 240 * 3 / 4))
113 .withFields({
114 C2F(mMaxInputSize, value).any(),
115 })
116 .calculatedAs(MaxInputSizeSetter, mMaxSize)
117 .build());
118
119 C2ChromaOffsetStruct locations[1] = {C2ChromaOffsetStruct::ITU_YUV_420_0()};
120 std::shared_ptr<C2StreamColorInfo::output> defaultColorInfo =
121 C2StreamColorInfo::output::AllocShared(1u, 0u, 8u /* bitDepth */,
122 C2Color::YUV_420);
123 memcpy(defaultColorInfo->m.locations, locations, sizeof(locations));
124
125 defaultColorInfo = C2StreamColorInfo::output::AllocShared(
126 {C2ChromaOffsetStruct::ITU_YUV_420_0()}, 0u, 8u /* bitDepth */,
127 C2Color::YUV_420);
128 helper->addStructDescriptors<C2ChromaOffsetStruct>();
129
130 addParameter(DefineParam(mColorInfo, C2_PARAMKEY_CODED_COLOR_INFO)
131 .withConstValue(defaultColorInfo)
132 .build());
133
134 addParameter(
135 DefineParam(mDefaultColorAspects, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS)
136 .withDefault(new C2StreamColorAspectsTuning::output(
137 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
138 C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
139 .withFields(
140 {C2F(mDefaultColorAspects, range)
141 .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
142 C2F(mDefaultColorAspects, primaries)
143 .inRange(C2Color::PRIMARIES_UNSPECIFIED,
144 C2Color::PRIMARIES_OTHER),
145 C2F(mDefaultColorAspects, transfer)
146 .inRange(C2Color::TRANSFER_UNSPECIFIED,
147 C2Color::TRANSFER_OTHER),
148 C2F(mDefaultColorAspects, matrix)
149 .inRange(C2Color::MATRIX_UNSPECIFIED,
150 C2Color::MATRIX_OTHER)})
151 .withSetter(DefaultColorAspectsSetter)
152 .build());
153
154 // TODO: support more formats?
155 addParameter(DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT)
156 .withConstValue(new C2StreamPixelFormatInfo::output(
157 0u, HAL_PIXEL_FORMAT_YCBCR_420_888))
158 .build());
159 }
160
161 static C2R SizeSetter(bool mayBlock,
162 const C2P<C2StreamPictureSizeInfo::output> &oldMe,
163 C2P<C2StreamPictureSizeInfo::output> &me) {
164 (void)mayBlock;
165 C2R res = C2R::Ok();
166 if (!me.F(me.v.width).supportsAtAll(me.v.width)) {
167 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width)));
168 me.set().width = oldMe.v.width;
169 }
170 if (!me.F(me.v.height).supportsAtAll(me.v.height)) {
171 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height)));
172 me.set().height = oldMe.v.height;
173 }
174 return res;
175 }
176
177 static C2R MaxPictureSizeSetter(
178 bool mayBlock, C2P<C2StreamMaxPictureSizeTuning::output> &me,
179 const C2P<C2StreamPictureSizeInfo::output> &size) {
180 (void)mayBlock;
181 // TODO: get max width/height from the size's field helpers vs.
182 // hardcoding
183 me.set().width = c2_min(c2_max(me.v.width, size.v.width), 4096u);
184 me.set().height = c2_min(c2_max(me.v.height, size.v.height), 4096u);
185 return C2R::Ok();
186 }
187
188 static C2R MaxInputSizeSetter(
189 bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input> &me,
190 const C2P<C2StreamMaxPictureSizeTuning::output> &maxSize) {
191 (void)mayBlock;
192 // assume compression ratio of 2
193 me.set().value =
194 (((maxSize.v.width + 63) / 64) * ((maxSize.v.height + 63) / 64) * 3072);
195 return C2R::Ok();
196 }
197
198 static C2R DefaultColorAspectsSetter(
199 bool mayBlock, C2P<C2StreamColorAspectsTuning::output> &me) {
200 (void)mayBlock;
201 if (me.v.range > C2Color::RANGE_OTHER) {
202 me.set().range = C2Color::RANGE_OTHER;
203 }
204 if (me.v.primaries > C2Color::PRIMARIES_OTHER) {
205 me.set().primaries = C2Color::PRIMARIES_OTHER;
206 }
207 if (me.v.transfer > C2Color::TRANSFER_OTHER) {
208 me.set().transfer = C2Color::TRANSFER_OTHER;
209 }
210 if (me.v.matrix > C2Color::MATRIX_OTHER) {
211 me.set().matrix = C2Color::MATRIX_OTHER;
212 }
213 return C2R::Ok();
214 }
215
216 static C2R ProfileLevelSetter(
217 bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me,
218 const C2P<C2StreamPictureSizeInfo::output> &size) {
219 (void)mayBlock;
220 (void)size;
221 (void)me; // TODO: validate
222 return C2R::Ok();
223 }
224
225 std::shared_ptr<C2StreamColorAspectsTuning::output>
226 getDefaultColorAspects_l() {
227 return mDefaultColorAspects;
228 }
229
230 static C2R Hdr10PlusInfoInputSetter(bool mayBlock,
231 C2P<C2StreamHdr10PlusInfo::input> &me) {
232 (void)mayBlock;
233 (void)me; // TODO: validate
234 return C2R::Ok();
235 }
236
237 static C2R Hdr10PlusInfoOutputSetter(bool mayBlock,
238 C2P<C2StreamHdr10PlusInfo::output> &me) {
239 (void)mayBlock;
240 (void)me; // TODO: validate
241 return C2R::Ok();
242 }
243
244 private:
245 std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
246 std::shared_ptr<C2StreamPictureSizeInfo::output> mSize;
247 std::shared_ptr<C2StreamMaxPictureSizeTuning::output> mMaxSize;
248 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize;
249 std::shared_ptr<C2StreamColorInfo::output> mColorInfo;
250 std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormat;
251 std::shared_ptr<C2StreamColorAspectsTuning::output> mDefaultColorAspects;
252 std::shared_ptr<C2StreamHdr10PlusInfo::input> mHdr10PlusInfoInput;
253 std::shared_ptr<C2StreamHdr10PlusInfo::output> mHdr10PlusInfoOutput;
254};
255
256C2SoftGav1Dec::C2SoftGav1Dec(const char *name, c2_node_id_t id,
257 const std::shared_ptr<IntfImpl> &intfImpl)
258 : SimpleC2Component(
259 std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700260 mIntf(intfImpl),
261 mCodecCtx(nullptr) {
262 gettimeofday(&mTimeStart, nullptr);
263 gettimeofday(&mTimeEnd, nullptr);
264}
Vignesh Venkatasubramanianb6d383d2019-06-10 15:11:58 -0700265
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700266C2SoftGav1Dec::~C2SoftGav1Dec() { onRelease(); }
267
268c2_status_t C2SoftGav1Dec::onInit() {
269 return initDecoder() ? C2_OK : C2_CORRUPTED;
270}
271
272c2_status_t C2SoftGav1Dec::onStop() {
273 mSignalledError = false;
274 mSignalledOutputEos = false;
Vignesh Venkatasubramanianb6d383d2019-06-10 15:11:58 -0700275 return C2_OK;
276}
277
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700278void C2SoftGav1Dec::onReset() {
279 (void)onStop();
280 c2_status_t err = onFlush_sm();
281 if (err != C2_OK) {
282 ALOGW("Failed to flush the av1 decoder. Trying to hard reset.");
283 destroyDecoder();
284 if (!initDecoder()) {
285 ALOGE("Hard reset failed.");
286 }
287 }
288}
289
290void C2SoftGav1Dec::onRelease() { destroyDecoder(); }
291
292c2_status_t C2SoftGav1Dec::onFlush_sm() {
James Zernb7aee6e2020-06-26 13:49:53 -0700293 Libgav1StatusCode status = mCodecCtx->SignalEOS();
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700294 if (status != kLibgav1StatusOk) {
295 ALOGE("Failed to flush av1 decoder. status: %d.", status);
296 return C2_CORRUPTED;
297 }
298
299 // Dequeue frame (if any) that was enqueued previously.
300 const libgav1::DecoderBuffer *buffer;
301 status = mCodecCtx->DequeueFrame(&buffer);
James Zernb7aee6e2020-06-26 13:49:53 -0700302 if (status != kLibgav1StatusOk && status != kLibgav1StatusNothingToDequeue) {
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700303 ALOGE("Failed to dequeue frame after flushing the av1 decoder. status: %d",
304 status);
305 return C2_CORRUPTED;
306 }
307
308 mSignalledError = false;
309 mSignalledOutputEos = false;
310
311 return C2_OK;
312}
313
314static int GetCPUCoreCount() {
315 int cpuCoreCount = 1;
316#if defined(_SC_NPROCESSORS_ONLN)
317 cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
318#else
319 // _SC_NPROC_ONLN must be defined...
320 cpuCoreCount = sysconf(_SC_NPROC_ONLN);
321#endif
322 CHECK(cpuCoreCount >= 1);
323 ALOGV("Number of CPU cores: %d", cpuCoreCount);
324 return cpuCoreCount;
325}
326
327bool C2SoftGav1Dec::initDecoder() {
328 mSignalledError = false;
329 mSignalledOutputEos = false;
330 mCodecCtx.reset(new libgav1::Decoder());
331
332 if (mCodecCtx == nullptr) {
333 ALOGE("mCodecCtx is null");
334 return false;
335 }
336
337 libgav1::DecoderSettings settings = {};
338 settings.threads = GetCPUCoreCount();
339
Vignesh Venkatasubramanian61ba2cf2019-06-24 10:04:00 -0700340 ALOGV("Using libgav1 AV1 software decoder.");
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700341 Libgav1StatusCode status = mCodecCtx->Init(&settings);
342 if (status != kLibgav1StatusOk) {
343 ALOGE("av1 decoder failed to initialize. status: %d.", status);
344 return false;
345 }
346
347 return true;
348}
349
350void C2SoftGav1Dec::destroyDecoder() { mCodecCtx = nullptr; }
351
352void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
353 uint32_t flags = 0;
354 if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
355 flags |= C2FrameData::FLAG_END_OF_STREAM;
356 ALOGV("signalling eos");
357 }
358 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
359 work->worklets.front()->output.buffers.clear();
360 work->worklets.front()->output.ordinal = work->input.ordinal;
361 work->workletsProcessed = 1u;
362}
363
364void C2SoftGav1Dec::finishWork(uint64_t index,
365 const std::unique_ptr<C2Work> &work,
366 const std::shared_ptr<C2GraphicBlock> &block) {
367 std::shared_ptr<C2Buffer> buffer =
368 createGraphicBuffer(block, C2Rect(mWidth, mHeight));
369 auto fillWork = [buffer, index](const std::unique_ptr<C2Work> &work) {
370 uint32_t flags = 0;
371 if ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) &&
372 (c2_cntr64_t(index) == work->input.ordinal.frameIndex)) {
373 flags |= C2FrameData::FLAG_END_OF_STREAM;
374 ALOGV("signalling eos");
375 }
376 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
377 work->worklets.front()->output.buffers.clear();
378 work->worklets.front()->output.buffers.push_back(buffer);
379 work->worklets.front()->output.ordinal = work->input.ordinal;
380 work->workletsProcessed = 1u;
381 };
382 if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) {
383 fillWork(work);
384 } else {
385 finish(index, fillWork);
386 }
387}
388
389void C2SoftGav1Dec::process(const std::unique_ptr<C2Work> &work,
390 const std::shared_ptr<C2BlockPool> &pool) {
391 work->result = C2_OK;
392 work->workletsProcessed = 0u;
393 work->worklets.front()->output.configUpdate.clear();
394 work->worklets.front()->output.flags = work->input.flags;
395 if (mSignalledError || mSignalledOutputEos) {
396 work->result = C2_BAD_VALUE;
397 return;
398 }
399
400 size_t inOffset = 0u;
401 size_t inSize = 0u;
402 C2ReadView rView = mDummyReadView;
403 if (!work->input.buffers.empty()) {
404 rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
405 inSize = rView.capacity();
406 if (inSize && rView.error()) {
407 ALOGE("read view map failed %d", rView.error());
408 work->result = C2_CORRUPTED;
409 return;
410 }
411 }
412
413 bool codecConfig =
414 ((work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) != 0);
415 bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
416
417 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x", inSize,
418 (int)work->input.ordinal.timestamp.peeku(),
419 (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
420
421 if (codecConfig) {
422 fillEmptyWork(work);
423 return;
424 }
425
426 int64_t frameIndex = work->input.ordinal.frameIndex.peekll();
427 if (inSize) {
428 uint8_t *bitstream = const_cast<uint8_t *>(rView.data() + inOffset);
429 int32_t decodeTime = 0;
430 int32_t delay = 0;
431
432 GETTIME(&mTimeStart, nullptr);
433 TIME_DIFF(mTimeEnd, mTimeStart, delay);
434
435 const Libgav1StatusCode status =
James Zernb7aee6e2020-06-26 13:49:53 -0700436 mCodecCtx->EnqueueFrame(bitstream, inSize, frameIndex,
437 /*buffer_private_data=*/nullptr);
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700438
439 GETTIME(&mTimeEnd, nullptr);
440 TIME_DIFF(mTimeStart, mTimeEnd, decodeTime);
441 ALOGV("decodeTime=%4d delay=%4d\n", decodeTime, delay);
442
443 if (status != kLibgav1StatusOk) {
444 ALOGE("av1 decoder failed to decode frame. status: %d.", status);
445 work->result = C2_CORRUPTED;
446 work->workletsProcessed = 1u;
447 mSignalledError = true;
448 return;
449 }
450
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700451 }
452
453 (void)outputBuffer(pool, work);
454
455 if (eos) {
456 drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work);
457 mSignalledOutputEos = true;
458 } else if (!inSize) {
459 fillEmptyWork(work);
460 }
461}
462
ming.zhouac19c3d2019-10-11 11:14:07 +0800463static void copyOutputBufferToYV12Frame(uint8_t *dstY, uint8_t *dstU, uint8_t *dstV,
464 const uint8_t *srcY, const uint8_t *srcU, const uint8_t *srcV,
465 size_t srcYStride, size_t srcUStride, size_t srcVStride,
466 size_t dstYStride, size_t dstUVStride,
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700467 uint32_t width, uint32_t height) {
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700468
469 for (size_t i = 0; i < height; ++i) {
ming.zhouac19c3d2019-10-11 11:14:07 +0800470 memcpy(dstY, srcY, width);
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700471 srcY += srcYStride;
ming.zhouac19c3d2019-10-11 11:14:07 +0800472 dstY += dstYStride;
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700473 }
474
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700475 for (size_t i = 0; i < height / 2; ++i) {
ming.zhouac19c3d2019-10-11 11:14:07 +0800476 memcpy(dstV, srcV, width / 2);
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700477 srcV += srcVStride;
ming.zhouac19c3d2019-10-11 11:14:07 +0800478 dstV += dstUVStride;
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700479 }
480
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700481 for (size_t i = 0; i < height / 2; ++i) {
ming.zhouac19c3d2019-10-11 11:14:07 +0800482 memcpy(dstU, srcU, width / 2);
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700483 srcU += srcUStride;
ming.zhouac19c3d2019-10-11 11:14:07 +0800484 dstU += dstUVStride;
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700485 }
486}
487
488static void convertYUV420Planar16ToY410(uint32_t *dst, const uint16_t *srcY,
489 const uint16_t *srcU,
490 const uint16_t *srcV, size_t srcYStride,
491 size_t srcUStride, size_t srcVStride,
492 size_t dstStride, size_t width,
493 size_t height) {
494 // Converting two lines at a time, slightly faster
495 for (size_t y = 0; y < height; y += 2) {
496 uint32_t *dstTop = (uint32_t *)dst;
497 uint32_t *dstBot = (uint32_t *)(dst + dstStride);
498 uint16_t *ySrcTop = (uint16_t *)srcY;
499 uint16_t *ySrcBot = (uint16_t *)(srcY + srcYStride);
500 uint16_t *uSrc = (uint16_t *)srcU;
501 uint16_t *vSrc = (uint16_t *)srcV;
502
503 uint32_t u01, v01, y01, y23, y45, y67, uv0, uv1;
504 size_t x = 0;
505 for (; x < width - 3; x += 4) {
506 u01 = *((uint32_t *)uSrc);
507 uSrc += 2;
508 v01 = *((uint32_t *)vSrc);
509 vSrc += 2;
510
511 y01 = *((uint32_t *)ySrcTop);
512 ySrcTop += 2;
513 y23 = *((uint32_t *)ySrcTop);
514 ySrcTop += 2;
515 y45 = *((uint32_t *)ySrcBot);
516 ySrcBot += 2;
517 y67 = *((uint32_t *)ySrcBot);
518 ySrcBot += 2;
519
520 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
521 uv1 = (u01 >> 16) | ((v01 >> 16) << 20);
522
523 *dstTop++ = 3 << 30 | ((y01 & 0x3FF) << 10) | uv0;
524 *dstTop++ = 3 << 30 | ((y01 >> 16) << 10) | uv0;
525 *dstTop++ = 3 << 30 | ((y23 & 0x3FF) << 10) | uv1;
526 *dstTop++ = 3 << 30 | ((y23 >> 16) << 10) | uv1;
527
528 *dstBot++ = 3 << 30 | ((y45 & 0x3FF) << 10) | uv0;
529 *dstBot++ = 3 << 30 | ((y45 >> 16) << 10) | uv0;
530 *dstBot++ = 3 << 30 | ((y67 & 0x3FF) << 10) | uv1;
531 *dstBot++ = 3 << 30 | ((y67 >> 16) << 10) | uv1;
532 }
533
534 // There should be at most 2 more pixels to process. Note that we don't
535 // need to consider odd case as the buffer is always aligned to even.
536 if (x < width) {
537 u01 = *uSrc;
538 v01 = *vSrc;
539 y01 = *((uint32_t *)ySrcTop);
540 y45 = *((uint32_t *)ySrcBot);
541 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
542 *dstTop++ = ((y01 & 0x3FF) << 10) | uv0;
543 *dstTop++ = ((y01 >> 16) << 10) | uv0;
544 *dstBot++ = ((y45 & 0x3FF) << 10) | uv0;
545 *dstBot++ = ((y45 >> 16) << 10) | uv0;
546 }
547
548 srcY += srcYStride * 2;
549 srcU += srcUStride;
550 srcV += srcVStride;
551 dst += dstStride * 2;
552 }
553}
554
555static void convertYUV420Planar16ToYUV420Planar(
ming.zhouac19c3d2019-10-11 11:14:07 +0800556 uint8_t *dstY, uint8_t *dstU, uint8_t *dstV,
557 const uint16_t *srcY, const uint16_t *srcU, const uint16_t *srcV,
558 size_t srcYStride, size_t srcUStride, size_t srcVStride,
559 size_t dstYStride, size_t dstUVStride,
560 size_t width, size_t height) {
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700561
562 for (size_t y = 0; y < height; ++y) {
563 for (size_t x = 0; x < width; ++x) {
564 dstY[x] = (uint8_t)(srcY[x] >> 2);
565 }
566
567 srcY += srcYStride;
ming.zhouac19c3d2019-10-11 11:14:07 +0800568 dstY += dstYStride;
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700569 }
570
571 for (size_t y = 0; y < (height + 1) / 2; ++y) {
572 for (size_t x = 0; x < (width + 1) / 2; ++x) {
573 dstU[x] = (uint8_t)(srcU[x] >> 2);
574 dstV[x] = (uint8_t)(srcV[x] >> 2);
575 }
576
577 srcU += srcUStride;
578 srcV += srcVStride;
579 dstU += dstUVStride;
580 dstV += dstUVStride;
581 }
582}
583
584bool C2SoftGav1Dec::outputBuffer(const std::shared_ptr<C2BlockPool> &pool,
585 const std::unique_ptr<C2Work> &work) {
586 if (!(work && pool)) return false;
587
588 const libgav1::DecoderBuffer *buffer;
589 const Libgav1StatusCode status = mCodecCtx->DequeueFrame(&buffer);
590
James Zernb7aee6e2020-06-26 13:49:53 -0700591 if (status != kLibgav1StatusOk && status != kLibgav1StatusNothingToDequeue) {
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700592 ALOGE("av1 decoder DequeueFrame failed. status: %d.", status);
593 return false;
594 }
595
James Zernb7aee6e2020-06-26 13:49:53 -0700596 // |buffer| can be NULL if status was equal to kLibgav1StatusOk or
597 // kLibgav1StatusNothingToDequeue. This is not an error. This could mean one
598 // of two things:
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700599 // - The EnqueueFrame() call was either a flush (called with nullptr).
600 // - The enqueued frame did not have any displayable frames.
601 if (!buffer) {
602 return false;
603 }
604
605 const int width = buffer->displayed_width[0];
606 const int height = buffer->displayed_height[0];
607 if (width != mWidth || height != mHeight) {
608 mWidth = width;
609 mHeight = height;
610
611 C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight);
612 std::vector<std::unique_ptr<C2SettingResult>> failures;
613 c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures);
614 if (err == C2_OK) {
615 work->worklets.front()->output.configUpdate.push_back(
616 C2Param::Copy(size));
617 } else {
618 ALOGE("Config update size failed");
619 mSignalledError = true;
620 work->result = C2_CORRUPTED;
621 work->workletsProcessed = 1u;
622 return false;
623 }
624 }
625
626 // TODO(vigneshv): Add support for monochrome videos since AV1 supports it.
627 CHECK(buffer->image_format == libgav1::kImageFormatYuv420);
628
629 std::shared_ptr<C2GraphicBlock> block;
630 uint32_t format = HAL_PIXEL_FORMAT_YV12;
631 if (buffer->bitdepth == 10) {
632 IntfImpl::Lock lock = mIntf->lock();
633 std::shared_ptr<C2StreamColorAspectsTuning::output> defaultColorAspects =
634 mIntf->getDefaultColorAspects_l();
635
636 if (defaultColorAspects->primaries == C2Color::PRIMARIES_BT2020 &&
637 defaultColorAspects->matrix == C2Color::MATRIX_BT2020 &&
638 defaultColorAspects->transfer == C2Color::TRANSFER_ST2084) {
639 format = HAL_PIXEL_FORMAT_RGBA_1010102;
640 }
641 }
642 C2MemoryUsage usage = {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
643
644 c2_status_t err = pool->fetchGraphicBlock(align(mWidth, 16), mHeight, format,
645 usage, &block);
646
647 if (err != C2_OK) {
648 ALOGE("fetchGraphicBlock for Output failed with status %d", err);
649 work->result = err;
650 return false;
651 }
652
653 C2GraphicView wView = block->map().get();
654
655 if (wView.error()) {
656 ALOGE("graphic view map failed %d", wView.error());
657 work->result = C2_CORRUPTED;
658 return false;
659 }
660
661 ALOGV("provided (%dx%d) required (%dx%d), out frameindex %d", block->width(),
662 block->height(), mWidth, mHeight, (int)buffer->user_private_data);
663
ming.zhouac19c3d2019-10-11 11:14:07 +0800664 uint8_t *dstY = const_cast<uint8_t *>(wView.data()[C2PlanarLayout::PLANE_Y]);
665 uint8_t *dstU = const_cast<uint8_t *>(wView.data()[C2PlanarLayout::PLANE_U]);
666 uint8_t *dstV = const_cast<uint8_t *>(wView.data()[C2PlanarLayout::PLANE_V]);
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700667 size_t srcYStride = buffer->stride[0];
668 size_t srcUStride = buffer->stride[1];
669 size_t srcVStride = buffer->stride[2];
670
ming.zhouac19c3d2019-10-11 11:14:07 +0800671 C2PlanarLayout layout = wView.layout();
672 size_t dstYStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
673 size_t dstUVStride = layout.planes[C2PlanarLayout::PLANE_U].rowInc;
674
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700675 if (buffer->bitdepth == 10) {
676 const uint16_t *srcY = (const uint16_t *)buffer->plane[0];
677 const uint16_t *srcU = (const uint16_t *)buffer->plane[1];
678 const uint16_t *srcV = (const uint16_t *)buffer->plane[2];
679
680 if (format == HAL_PIXEL_FORMAT_RGBA_1010102) {
681 convertYUV420Planar16ToY410(
ming.zhouac19c3d2019-10-11 11:14:07 +0800682 (uint32_t *)dstY, srcY, srcU, srcV, srcYStride / 2, srcUStride / 2,
James Zern8dfb3f22020-08-07 18:49:51 -0700683 srcVStride / 2, dstYStride / sizeof(uint32_t), mWidth, mHeight);
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700684 } else {
ming.zhouac19c3d2019-10-11 11:14:07 +0800685 convertYUV420Planar16ToYUV420Planar(dstY, dstU, dstV,
686 srcY, srcU, srcV,
687 srcYStride / 2, srcUStride / 2, srcVStride / 2,
688 dstYStride, dstUVStride,
689 mWidth, mHeight);
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700690 }
691 } else {
692 const uint8_t *srcY = (const uint8_t *)buffer->plane[0];
693 const uint8_t *srcU = (const uint8_t *)buffer->plane[1];
694 const uint8_t *srcV = (const uint8_t *)buffer->plane[2];
ming.zhouac19c3d2019-10-11 11:14:07 +0800695 copyOutputBufferToYV12Frame(dstY, dstU, dstV,
696 srcY, srcU, srcV,
697 srcYStride, srcUStride, srcVStride,
698 dstYStride, dstUVStride,
699 mWidth, mHeight);
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700700 }
701 finishWork(buffer->user_private_data, work, std::move(block));
702 block = nullptr;
703 return true;
704}
705
706c2_status_t C2SoftGav1Dec::drainInternal(
707 uint32_t drainMode, const std::shared_ptr<C2BlockPool> &pool,
708 const std::unique_ptr<C2Work> &work) {
709 if (drainMode == NO_DRAIN) {
710 ALOGW("drain with NO_DRAIN: no-op");
711 return C2_OK;
712 }
713 if (drainMode == DRAIN_CHAIN) {
714 ALOGW("DRAIN_CHAIN not supported");
715 return C2_OMITTED;
716 }
717
James Zernb7aee6e2020-06-26 13:49:53 -0700718 const Libgav1StatusCode status = mCodecCtx->SignalEOS();
Vignesh Venkatasubramanian0f3e7422019-06-17 16:21:36 -0700719 if (status != kLibgav1StatusOk) {
720 ALOGE("Failed to flush av1 decoder. status: %d.", status);
721 return C2_CORRUPTED;
722 }
723
724 while (outputBuffer(pool, work)) {
725 }
726
727 if (drainMode == DRAIN_COMPONENT_WITH_EOS && work &&
728 work->workletsProcessed == 0u) {
729 fillEmptyWork(work);
730 }
731
732 return C2_OK;
733}
734
735c2_status_t C2SoftGav1Dec::drain(uint32_t drainMode,
736 const std::shared_ptr<C2BlockPool> &pool) {
737 return drainInternal(drainMode, pool, nullptr);
738}
739
Vignesh Venkatasubramanianb6d383d2019-06-10 15:11:58 -0700740class C2SoftGav1Factory : public C2ComponentFactory {
741 public:
742 C2SoftGav1Factory()
743 : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
744 GetCodec2PlatformComponentStore()->getParamReflector())) {}
745
746 virtual c2_status_t createComponent(
747 c2_node_id_t id, std::shared_ptr<C2Component> *const component,
748 std::function<void(C2Component *)> deleter) override {
749 *component = std::shared_ptr<C2Component>(
750 new C2SoftGav1Dec(COMPONENT_NAME, id,
751 std::make_shared<C2SoftGav1Dec::IntfImpl>(mHelper)),
752 deleter);
753 return C2_OK;
754 }
755
756 virtual c2_status_t createInterface(
757 c2_node_id_t id, std::shared_ptr<C2ComponentInterface> *const interface,
758 std::function<void(C2ComponentInterface *)> deleter) override {
759 *interface = std::shared_ptr<C2ComponentInterface>(
760 new SimpleInterface<C2SoftGav1Dec::IntfImpl>(
761 COMPONENT_NAME, id,
762 std::make_shared<C2SoftGav1Dec::IntfImpl>(mHelper)),
763 deleter);
764 return C2_OK;
765 }
766
767 virtual ~C2SoftGav1Factory() override = default;
768
769 private:
770 std::shared_ptr<C2ReflectorHelper> mHelper;
771};
772
773} // namespace android
774
Cindy Zhouf6c0c3c2020-12-02 10:53:40 -0800775__attribute__((cfi_canonical_jump_table))
Vignesh Venkatasubramanianb6d383d2019-06-10 15:11:58 -0700776extern "C" ::C2ComponentFactory *CreateCodec2Factory() {
777 ALOGV("in %s", __func__);
778 return new ::android::C2SoftGav1Factory();
779}
780
Cindy Zhouf6c0c3c2020-12-02 10:53:40 -0800781__attribute__((cfi_canonical_jump_table))
Vignesh Venkatasubramanianb6d383d2019-06-10 15:11:58 -0700782extern "C" void DestroyCodec2Factory(::C2ComponentFactory *factory) {
783 ALOGV("in %s", __func__);
784 delete factory;
785}