blob: 0cf277ff88268eb62998372c1c9e0f2b5e5d706d [file] [log] [blame]
Ray Essick707c1462018-12-05 15:21:35 -08001/*
2 * Copyright (C) 2018 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 "C2SoftAomDec"
19#include <log/log.h>
20
21#include <media/stagefright/foundation/AUtils.h>
22#include <media/stagefright/foundation/MediaDefs.h>
23
24#include <C2Debug.h>
25#include <C2PlatformSupport.h>
26#include <SimpleC2Interface.h>
27
28#include "C2SoftAomDec.h"
29
30namespace android {
31
32constexpr char COMPONENT_NAME[] = "c2.android.av1.decoder";
33
34class C2SoftAomDec::IntfImpl : public SimpleInterface<void>::BaseParams {
35 public:
36 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper>& helper)
37 : SimpleInterface<void>::BaseParams(
38 helper, COMPONENT_NAME, C2Component::KIND_DECODER,
39 C2Component::DOMAIN_VIDEO, MEDIA_MIMETYPE_VIDEO_AV1) {
40 noPrivateBuffers(); // TODO: account for our buffers here
41 noInputReferences();
42 noOutputReferences();
43 noInputLatency();
44 noTimeStretch();
45
46 addParameter(DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
47 .withConstValue(new C2ComponentAttributesSetting(
48 C2Component::ATTRIB_IS_TEMPORAL))
49 .build());
50
51 addParameter(
52 DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE)
53 .withDefault(new C2StreamPictureSizeInfo::output(0u, 320, 240))
54 .withFields({
55 C2F(mSize, width).inRange(2, 2048, 2),
56 C2F(mSize, height).inRange(2, 2048, 2),
57 })
58 .withSetter(SizeSetter)
59 .build());
60
61 addParameter(
62 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
63 .withDefault(new C2StreamProfileLevelInfo::input(0u,
64 C2Config::PROFILE_AV1_0, C2Config::LEVEL_AV1_2_1))
65 .withFields({
66 C2F(mProfileLevel, profile).oneOf({
67 C2Config::PROFILE_AV1_0,
68 C2Config::PROFILE_AV1_1}),
69 C2F(mProfileLevel, level).oneOf({
70 C2Config::LEVEL_AV1_2,
71 C2Config::LEVEL_AV1_2_1,
72 C2Config::LEVEL_AV1_2_2,
73 C2Config::LEVEL_AV1_3,
74 C2Config::LEVEL_AV1_3_1,
75 C2Config::LEVEL_AV1_3_2,
76 })
77 })
78 .withSetter(ProfileLevelSetter, mSize)
79 .build());
80
Rakesh Kumarf93a4542019-03-26 11:01:00 +053081 mHdr10PlusInfoInput = C2StreamHdr10PlusInfo::input::AllocShared(0);
82 addParameter(
83 DefineParam(mHdr10PlusInfoInput, C2_PARAMKEY_INPUT_HDR10_PLUS_INFO)
84 .withDefault(mHdr10PlusInfoInput)
85 .withFields({
86 C2F(mHdr10PlusInfoInput, m.value).any(),
87 })
88 .withSetter(Hdr10PlusInfoInputSetter)
89 .build());
90
91 mHdr10PlusInfoOutput = C2StreamHdr10PlusInfo::output::AllocShared(0);
92 addParameter(
93 DefineParam(mHdr10PlusInfoOutput, C2_PARAMKEY_OUTPUT_HDR10_PLUS_INFO)
94 .withDefault(mHdr10PlusInfoOutput)
95 .withFields({
96 C2F(mHdr10PlusInfoOutput, m.value).any(),
97 })
98 .withSetter(Hdr10PlusInfoOutputSetter)
99 .build());
100
Ray Essick707c1462018-12-05 15:21:35 -0800101 addParameter(DefineParam(mMaxSize, C2_PARAMKEY_MAX_PICTURE_SIZE)
102 .withDefault(new C2StreamMaxPictureSizeTuning::output(
103 0u, 320, 240))
104 .withFields({
105 C2F(mSize, width).inRange(2, 2048, 2),
106 C2F(mSize, height).inRange(2, 2048, 2),
107 })
108 .withSetter(MaxPictureSizeSetter, mSize)
109 .build());
110
111 addParameter(
112 DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
113 .withDefault(
114 new C2StreamMaxBufferSizeInfo::input(0u, 320 * 240 * 3 / 4))
115 .withFields({
116 C2F(mMaxInputSize, value).any(),
117 })
118 .calculatedAs(MaxInputSizeSetter, mMaxSize)
119 .build());
120
121 C2ChromaOffsetStruct locations[1] = {
122 C2ChromaOffsetStruct::ITU_YUV_420_0()};
123 std::shared_ptr<C2StreamColorInfo::output> defaultColorInfo =
124 C2StreamColorInfo::output::AllocShared(1u, 0u, 8u /* bitDepth */,
125 C2Color::YUV_420);
126 memcpy(defaultColorInfo->m.locations, locations, sizeof(locations));
127
128 defaultColorInfo = C2StreamColorInfo::output::AllocShared(
129 {C2ChromaOffsetStruct::ITU_YUV_420_0()}, 0u, 8u /* bitDepth */,
130 C2Color::YUV_420);
131 helper->addStructDescriptors<C2ChromaOffsetStruct>();
132
133 addParameter(DefineParam(mColorInfo, C2_PARAMKEY_CODED_COLOR_INFO)
134 .withConstValue(defaultColorInfo)
135 .build());
136
137 addParameter(
138 DefineParam(mDefaultColorAspects, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS)
139 .withDefault(new C2StreamColorAspectsTuning::output(
140 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
141 C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
142 .withFields({
143 C2F(mDefaultColorAspects, range).inRange(
144 C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
145 C2F(mDefaultColorAspects, primaries).inRange(
146 C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER),
147 C2F(mDefaultColorAspects, transfer).inRange(
148 C2Color::TRANSFER_UNSPECIFIED, C2Color::TRANSFER_OTHER),
149 C2F(mDefaultColorAspects, matrix).inRange(
150 C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)
151 })
152 .withSetter(DefaultColorAspectsSetter)
153 .build());
154
155 // TODO: support more formats?
156 addParameter(DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT)
157 .withConstValue(new C2StreamPixelFormatInfo::output(
158 0u, HAL_PIXEL_FORMAT_YCBCR_420_888))
159 .build());
160 }
161
162 static C2R SizeSetter(bool mayBlock,
163 const C2P<C2StreamPictureSizeInfo::output>& oldMe,
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800164 C2P<C2StreamPictureSizeInfo::output>& me) {
Ray Essick707c1462018-12-05 15:21:35 -0800165 (void)mayBlock;
166 C2R res = C2R::Ok();
167 if (!me.F(me.v.width).supportsAtAll(me.v.width)) {
168 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width)));
169 me.set().width = oldMe.v.width;
170 }
171 if (!me.F(me.v.height).supportsAtAll(me.v.height)) {
172 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height)));
173 me.set().height = oldMe.v.height;
174 }
175 return res;
176 }
177
178 static C2R MaxPictureSizeSetter(
179 bool mayBlock, C2P<C2StreamMaxPictureSizeTuning::output>& me,
180 const C2P<C2StreamPictureSizeInfo::output>& size) {
181 (void)mayBlock;
182 // TODO: get max width/height from the size's field helpers vs.
183 // hardcoding
184 me.set().width = c2_min(c2_max(me.v.width, size.v.width), 4096u);
185 me.set().height = c2_min(c2_max(me.v.height, size.v.height), 4096u);
186 return C2R::Ok();
187 }
188
189 static C2R MaxInputSizeSetter(
190 bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input>& me,
191 const C2P<C2StreamMaxPictureSizeTuning::output>& maxSize) {
192 (void)mayBlock;
193 // assume compression ratio of 2
194 me.set().value = (((maxSize.v.width + 63) / 64) *
195 ((maxSize.v.height + 63) / 64) * 3072);
196 return C2R::Ok();
197 }
198 static C2R DefaultColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsTuning::output> &me) {
199 (void)mayBlock;
200 if (me.v.range > C2Color::RANGE_OTHER) {
201 me.set().range = C2Color::RANGE_OTHER;
202 }
203 if (me.v.primaries > C2Color::PRIMARIES_OTHER) {
204 me.set().primaries = C2Color::PRIMARIES_OTHER;
205 }
206 if (me.v.transfer > C2Color::TRANSFER_OTHER) {
207 me.set().transfer = C2Color::TRANSFER_OTHER;
208 }
209 if (me.v.matrix > C2Color::MATRIX_OTHER) {
210 me.set().matrix = C2Color::MATRIX_OTHER;
211 }
212 return C2R::Ok();
213 }
214
215 static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me,
216 const C2P<C2StreamPictureSizeInfo::output> &size) {
217 (void)mayBlock;
218 (void)size;
219 (void)me; // TODO: validate
220 return C2R::Ok();
221 }
222 std::shared_ptr<C2StreamColorAspectsTuning::output> getDefaultColorAspects_l() {
223 return mDefaultColorAspects;
224 }
225
Rakesh Kumarf93a4542019-03-26 11:01:00 +0530226 static C2R Hdr10PlusInfoInputSetter(bool mayBlock, C2P<C2StreamHdr10PlusInfo::input> &me) {
227 (void)mayBlock;
228 (void)me; // TODO: validate
229 return C2R::Ok();
230 }
231
232 static C2R Hdr10PlusInfoOutputSetter(bool mayBlock, C2P<C2StreamHdr10PlusInfo::output> &me) {
233 (void)mayBlock;
234 (void)me; // TODO: validate
235 return C2R::Ok();
236 }
237
Ray Essick707c1462018-12-05 15:21:35 -0800238 private:
239 std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
240 std::shared_ptr<C2StreamPictureSizeInfo::output> mSize;
241 std::shared_ptr<C2StreamMaxPictureSizeTuning::output> mMaxSize;
242 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize;
243 std::shared_ptr<C2StreamColorInfo::output> mColorInfo;
244 std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormat;
245 std::shared_ptr<C2StreamColorAspectsTuning::output> mDefaultColorAspects;
Rakesh Kumarf93a4542019-03-26 11:01:00 +0530246 std::shared_ptr<C2StreamHdr10PlusInfo::input> mHdr10PlusInfoInput;
247 std::shared_ptr<C2StreamHdr10PlusInfo::output> mHdr10PlusInfoOutput;
Ray Essick707c1462018-12-05 15:21:35 -0800248};
249
250C2SoftAomDec::C2SoftAomDec(const char* name, c2_node_id_t id,
251 const std::shared_ptr<IntfImpl>& intfImpl)
252 : SimpleC2Component(
253 std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
254 mIntf(intfImpl),
255 mCodecCtx(nullptr){
256
257 GENERATE_FILE_NAMES();
258 CREATE_DUMP_FILE(mInFile);
259 CREATE_DUMP_FILE(mOutFile);
260
261 gettimeofday(&mTimeStart, nullptr);
262 gettimeofday(&mTimeEnd, nullptr);
263}
264
265C2SoftAomDec::~C2SoftAomDec() {
266 onRelease();
267}
268
269c2_status_t C2SoftAomDec::onInit() {
270 status_t err = initDecoder();
271 return err == OK ? C2_OK : C2_CORRUPTED;
272}
273
274c2_status_t C2SoftAomDec::onStop() {
275 mSignalledError = false;
276 mSignalledOutputEos = false;
277 return C2_OK;
278}
279
280void C2SoftAomDec::onReset() {
281 (void)onStop();
282 c2_status_t err = onFlush_sm();
283 if (err != C2_OK) {
284 ALOGW("Failed to flush decoder. Try to hard reset decoder.");
285 destroyDecoder();
286 (void)initDecoder();
287 }
288}
289
290void C2SoftAomDec::onRelease() {
291 destroyDecoder();
292}
293
294c2_status_t C2SoftAomDec::onFlush_sm() {
295 if (aom_codec_decode(mCodecCtx, nullptr, 0, nullptr)) {
296 ALOGE("Failed to flush av1 decoder.");
297 return C2_CORRUPTED;
298 }
299
300 aom_codec_iter_t iter = nullptr;
301 while (aom_codec_get_frame(mCodecCtx, &iter)) {
302 }
303
304 mSignalledError = false;
305 mSignalledOutputEos = false;
306
307 return C2_OK;
308}
309
310static int GetCPUCoreCount() {
311 int cpuCoreCount = 1;
312#if defined(_SC_NPROCESSORS_ONLN)
313 cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
314#else
315 // _SC_NPROC_ONLN must be defined...
316 cpuCoreCount = sysconf(_SC_NPROC_ONLN);
317#endif
318 CHECK(cpuCoreCount >= 1);
319 ALOGV("Number of CPU cores: %d", cpuCoreCount);
320 return cpuCoreCount;
321}
322
323status_t C2SoftAomDec::initDecoder() {
324 mSignalledError = false;
325 mSignalledOutputEos = false;
326 if (!mCodecCtx) {
327 mCodecCtx = new aom_codec_ctx_t;
328 }
329
330 if (!mCodecCtx) {
331 ALOGE("mCodecCtx is null");
332 return NO_MEMORY;
333 }
334
335 aom_codec_dec_cfg_t cfg;
336 memset(&cfg, 0, sizeof(aom_codec_dec_cfg_t));
337 cfg.threads = GetCPUCoreCount();
338 cfg.allow_lowbitdepth = 1;
339
340 aom_codec_flags_t flags;
341 memset(&flags, 0, sizeof(aom_codec_flags_t));
342
Vignesh Venkatasubramanian61ba2cf2019-06-24 10:04:00 -0700343 ALOGV("Using libaom AV1 software decoder.");
Ray Essick707c1462018-12-05 15:21:35 -0800344 aom_codec_err_t err;
345 if ((err = aom_codec_dec_init(mCodecCtx, aom_codec_av1_dx(), &cfg, 0))) {
346 ALOGE("av1 decoder failed to initialize. (%d)", err);
347 return UNKNOWN_ERROR;
348 }
349
350 return OK;
351}
352
353status_t C2SoftAomDec::destroyDecoder() {
354 if (mCodecCtx) {
355 aom_codec_destroy(mCodecCtx);
356 delete mCodecCtx;
357 mCodecCtx = nullptr;
358 }
359 return OK;
360}
361
362void fillEmptyWork(const std::unique_ptr<C2Work>& work) {
363 uint32_t flags = 0;
364 if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
365 flags |= C2FrameData::FLAG_END_OF_STREAM;
366 ALOGV("signalling eos");
367 }
368 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
369 work->worklets.front()->output.buffers.clear();
370 work->worklets.front()->output.ordinal = work->input.ordinal;
371 work->workletsProcessed = 1u;
372}
373
374void C2SoftAomDec::finishWork(uint64_t index,
375 const std::unique_ptr<C2Work>& work,
376 const std::shared_ptr<C2GraphicBlock>& block) {
377 std::shared_ptr<C2Buffer> buffer =
378 createGraphicBuffer(block, C2Rect(mWidth, mHeight));
Rakesh Kumarf93a4542019-03-26 11:01:00 +0530379 auto fillWork = [buffer, index, intf = this->mIntf](
380 const std::unique_ptr<C2Work>& work) {
Ray Essick707c1462018-12-05 15:21:35 -0800381 uint32_t flags = 0;
382 if ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) &&
383 (c2_cntr64_t(index) == work->input.ordinal.frameIndex)) {
384 flags |= C2FrameData::FLAG_END_OF_STREAM;
385 ALOGV("signalling eos");
386 }
387 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
388 work->worklets.front()->output.buffers.clear();
389 work->worklets.front()->output.buffers.push_back(buffer);
390 work->worklets.front()->output.ordinal = work->input.ordinal;
391 work->workletsProcessed = 1u;
Rakesh Kumarf93a4542019-03-26 11:01:00 +0530392
393 for (const std::unique_ptr<C2Param> &param: work->input.configUpdate) {
394 if (param) {
395 C2StreamHdr10PlusInfo::input *hdr10PlusInfo =
396 C2StreamHdr10PlusInfo::input::From(param.get());
397
398 if (hdr10PlusInfo != nullptr) {
399 std::vector<std::unique_ptr<C2SettingResult>> failures;
400 std::unique_ptr<C2Param> outParam = C2Param::CopyAsStream(
401 *param.get(), true /*output*/, param->stream());
402 c2_status_t err = intf->config(
403 { outParam.get() }, C2_MAY_BLOCK, &failures);
404 if (err == C2_OK) {
405 work->worklets.front()->output.configUpdate.push_back(
406 C2Param::Copy(*outParam.get()));
407 } else {
408 ALOGE("finishWork: Config update size failed");
409 }
410 break;
411 }
412 }
413 }
Ray Essick707c1462018-12-05 15:21:35 -0800414 };
415 if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) {
416 fillWork(work);
417 } else {
418 finish(index, fillWork);
419 }
420}
421
422void C2SoftAomDec::process(const std::unique_ptr<C2Work>& work,
423 const std::shared_ptr<C2BlockPool>& pool) {
424 work->result = C2_OK;
425 work->workletsProcessed = 0u;
426 work->worklets.front()->output.configUpdate.clear();
427 work->worklets.front()->output.flags = work->input.flags;
428 if (mSignalledError || mSignalledOutputEos) {
429 work->result = C2_BAD_VALUE;
430 return;
431 }
432
433 size_t inOffset = 0u;
434 size_t inSize = 0u;
435 C2ReadView rView = mDummyReadView;
436 if (!work->input.buffers.empty()) {
437 rView =
438 work->input.buffers[0]->data().linearBlocks().front().map().get();
439 inSize = rView.capacity();
440 if (inSize && rView.error()) {
441 ALOGE("read view map failed %d", rView.error());
442 work->result = C2_CORRUPTED;
443 return;
444 }
445 }
446
447 bool codecConfig =
448 ((work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) != 0);
449 bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
450
451 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
452 inSize, (int)work->input.ordinal.timestamp.peeku(),
453 (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
454
455 if (codecConfig) {
456 fillEmptyWork(work);
457 return;
458 }
459
460 int64_t frameIndex = work->input.ordinal.frameIndex.peekll();
461 if (inSize) {
462 uint8_t* bitstream = const_cast<uint8_t*>(rView.data() + inOffset);
463 int32_t decodeTime = 0;
464 int32_t delay = 0;
465
466 DUMP_TO_FILE(mOutFile, bitstream, inSize);
467 GETTIME(&mTimeStart, nullptr);
468 TIME_DIFF(mTimeEnd, mTimeStart, delay);
469
470 aom_codec_err_t err =
471 aom_codec_decode(mCodecCtx, bitstream, inSize, &frameIndex);
472
473 GETTIME(&mTimeEnd, nullptr);
474 TIME_DIFF(mTimeStart, mTimeEnd, decodeTime);
475 ALOGV("decodeTime=%4d delay=%4d\n", decodeTime, delay);
476
477 if (err != AOM_CODEC_OK) {
478 ALOGE("av1 decoder failed to decode frame err: %d", err);
479 work->result = C2_CORRUPTED;
480 work->workletsProcessed = 1u;
481 mSignalledError = true;
482 return;
483 }
484
485 } else {
486 if (aom_codec_decode(mCodecCtx, nullptr, 0, nullptr)) {
487 ALOGE("Failed to flush av1 decoder.");
488 work->result = C2_CORRUPTED;
489 work->workletsProcessed = 1u;
490 mSignalledError = true;
491 return;
492 }
493 }
494
495 (void)outputBuffer(pool, work);
496
497 if (eos) {
498 drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work);
499 mSignalledOutputEos = true;
500 } else if (!inSize) {
501 fillEmptyWork(work);
502 }
503}
504
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700505static void copyOutputBufferToYuvPlanarFrame(
506 uint8_t *dst, const uint8_t *srcY, const uint8_t *srcU, const uint8_t *srcV,
Ray Essick707c1462018-12-05 15:21:35 -0800507 size_t srcYStride, size_t srcUStride, size_t srcVStride,
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700508 size_t dstYStride, size_t dstUVStride,
Ray Essick707c1462018-12-05 15:21:35 -0800509 uint32_t width, uint32_t height) {
Ray Essick707c1462018-12-05 15:21:35 -0800510 uint8_t* dstStart = dst;
511
Ray Essick707c1462018-12-05 15:21:35 -0800512 for (size_t i = 0; i < height; ++i) {
513 memcpy(dst, srcY, width);
514 srcY += srcYStride;
515 dst += dstYStride;
516 }
517
518 dst = dstStart + dstYStride * height;
519 for (size_t i = 0; i < height / 2; ++i) {
520 memcpy(dst, srcV, width / 2);
521 srcV += srcVStride;
522 dst += dstUVStride;
523 }
524
525 dst = dstStart + (dstYStride * height) + (dstUVStride * height / 2);
526 for (size_t i = 0; i < height / 2; ++i) {
527 memcpy(dst, srcU, width / 2);
528 srcU += srcUStride;
529 dst += dstUVStride;
530 }
531}
532
533static void convertYUV420Planar16ToY410(uint32_t *dst,
534 const uint16_t *srcY, const uint16_t *srcU, const uint16_t *srcV,
535 size_t srcYStride, size_t srcUStride, size_t srcVStride,
536 size_t dstStride, size_t width, size_t height) {
537
538 // Converting two lines at a time, slightly faster
539 for (size_t y = 0; y < height; y += 2) {
540 uint32_t *dstTop = (uint32_t *) dst;
541 uint32_t *dstBot = (uint32_t *) (dst + dstStride);
542 uint16_t *ySrcTop = (uint16_t*) srcY;
543 uint16_t *ySrcBot = (uint16_t*) (srcY + srcYStride);
544 uint16_t *uSrc = (uint16_t*) srcU;
545 uint16_t *vSrc = (uint16_t*) srcV;
546
547 uint32_t u01, v01, y01, y23, y45, y67, uv0, uv1;
548 size_t x = 0;
549 for (; x < width - 3; x += 4) {
550
551 u01 = *((uint32_t*)uSrc); uSrc += 2;
552 v01 = *((uint32_t*)vSrc); vSrc += 2;
553
554 y01 = *((uint32_t*)ySrcTop); ySrcTop += 2;
555 y23 = *((uint32_t*)ySrcTop); ySrcTop += 2;
556 y45 = *((uint32_t*)ySrcBot); ySrcBot += 2;
557 y67 = *((uint32_t*)ySrcBot); ySrcBot += 2;
558
559 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
560 uv1 = (u01 >> 16) | ((v01 >> 16) << 20);
561
562 *dstTop++ = 3 << 30 | ((y01 & 0x3FF) << 10) | uv0;
563 *dstTop++ = 3 << 30 | ((y01 >> 16) << 10) | uv0;
564 *dstTop++ = 3 << 30 | ((y23 & 0x3FF) << 10) | uv1;
565 *dstTop++ = 3 << 30 | ((y23 >> 16) << 10) | uv1;
566
567 *dstBot++ = 3 << 30 | ((y45 & 0x3FF) << 10) | uv0;
568 *dstBot++ = 3 << 30 | ((y45 >> 16) << 10) | uv0;
569 *dstBot++ = 3 << 30 | ((y67 & 0x3FF) << 10) | uv1;
570 *dstBot++ = 3 << 30 | ((y67 >> 16) << 10) | uv1;
571 }
572
573 // There should be at most 2 more pixels to process. Note that we don't
574 // need to consider odd case as the buffer is always aligned to even.
575 if (x < width) {
576 u01 = *uSrc;
577 v01 = *vSrc;
578 y01 = *((uint32_t*)ySrcTop);
579 y45 = *((uint32_t*)ySrcBot);
580 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
581 *dstTop++ = ((y01 & 0x3FF) << 10) | uv0;
582 *dstTop++ = ((y01 >> 16) << 10) | uv0;
583 *dstBot++ = ((y45 & 0x3FF) << 10) | uv0;
584 *dstBot++ = ((y45 >> 16) << 10) | uv0;
585 }
586
587 srcY += srcYStride * 2;
588 srcU += srcUStride;
589 srcV += srcVStride;
590 dst += dstStride * 2;
591 }
592
593 return;
594}
595
596static void convertYUV420Planar16ToYUV420Planar(uint8_t *dst,
597 const uint16_t *srcY, const uint16_t *srcU, const uint16_t *srcV,
598 size_t srcYStride, size_t srcUStride, size_t srcVStride,
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700599 size_t dstYStride, size_t dstUVStride, size_t width, size_t height) {
Ray Essick707c1462018-12-05 15:21:35 -0800600
601 uint8_t *dstY = (uint8_t *)dst;
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700602 size_t dstYSize = dstYStride * height;
Ray Essick707c1462018-12-05 15:21:35 -0800603 size_t dstUVSize = dstUVStride * height / 2;
604 uint8_t *dstV = dstY + dstYSize;
605 uint8_t *dstU = dstV + dstUVSize;
606
607 for (size_t y = 0; y < height; ++y) {
608 for (size_t x = 0; x < width; ++x) {
609 dstY[x] = (uint8_t)(srcY[x] >> 2);
610 }
611
612 srcY += srcYStride;
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700613 dstY += dstYStride;
Ray Essick707c1462018-12-05 15:21:35 -0800614 }
615
616 for (size_t y = 0; y < (height + 1) / 2; ++y) {
617 for (size_t x = 0; x < (width + 1) / 2; ++x) {
618 dstU[x] = (uint8_t)(srcU[x] >> 2);
619 dstV[x] = (uint8_t)(srcV[x] >> 2);
620 }
621
622 srcU += srcUStride;
623 srcV += srcVStride;
624 dstU += dstUVStride;
625 dstV += dstUVStride;
626 }
627 return;
628}
629bool C2SoftAomDec::outputBuffer(
630 const std::shared_ptr<C2BlockPool> &pool,
631 const std::unique_ptr<C2Work> &work)
632{
633 if (!(work && pool)) return false;
634
635 aom_codec_iter_t iter = nullptr;
636 aom_image_t* img = aom_codec_get_frame(mCodecCtx, &iter);
637
638 if (!img) return false;
639
640 if (img->d_w != mWidth || img->d_h != mHeight) {
641 mWidth = img->d_w;
642 mHeight = img->d_h;
643
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800644 C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight);
Ray Essick707c1462018-12-05 15:21:35 -0800645 std::vector<std::unique_ptr<C2SettingResult>> failures;
646 c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures);
647 if (err == C2_OK) {
648 work->worklets.front()->output.configUpdate.push_back(
649 C2Param::Copy(size));
650 } else {
651 ALOGE("Config update size failed");
652 mSignalledError = true;
653 work->result = C2_CORRUPTED;
654 work->workletsProcessed = 1u;
655 return false;
656 }
657 }
658
659 CHECK(img->fmt == AOM_IMG_FMT_I420 || img->fmt == AOM_IMG_FMT_I42016);
660
661 std::shared_ptr<C2GraphicBlock> block;
662 uint32_t format = HAL_PIXEL_FORMAT_YV12;
663 if (img->fmt == AOM_IMG_FMT_I42016) {
664 IntfImpl::Lock lock = mIntf->lock();
665 std::shared_ptr<C2StreamColorAspectsTuning::output> defaultColorAspects = mIntf->getDefaultColorAspects_l();
666
667 if (defaultColorAspects->primaries == C2Color::PRIMARIES_BT2020 &&
668 defaultColorAspects->matrix == C2Color::MATRIX_BT2020 &&
669 defaultColorAspects->transfer == C2Color::TRANSFER_ST2084) {
670 format = HAL_PIXEL_FORMAT_RGBA_1010102;
671 }
672 }
673 C2MemoryUsage usage = {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
674
675 c2_status_t err = pool->fetchGraphicBlock(align(mWidth, 16), mHeight,
676 format, usage, &block);
677
678 if (err != C2_OK) {
679 ALOGE("fetchGraphicBlock for Output failed with status %d", err);
680 work->result = err;
681 return false;
682 }
683
684 C2GraphicView wView = block->map().get();
685
686 if (wView.error()) {
687 ALOGE("graphic view map failed %d", wView.error());
688 work->result = C2_CORRUPTED;
689 return false;
690 }
691
692 ALOGV("provided (%dx%d) required (%dx%d), out frameindex %d",
693 block->width(), block->height(), mWidth, mHeight,
694 (int)*(int64_t*)img->user_priv);
695
696 uint8_t* dst = const_cast<uint8_t*>(wView.data()[C2PlanarLayout::PLANE_Y]);
697 size_t srcYStride = img->stride[AOM_PLANE_Y];
698 size_t srcUStride = img->stride[AOM_PLANE_U];
699 size_t srcVStride = img->stride[AOM_PLANE_V];
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700700 C2PlanarLayout layout = wView.layout();
701 size_t dstYStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
702 size_t dstUVStride = layout.planes[C2PlanarLayout::PLANE_U].rowInc;
Ray Essick707c1462018-12-05 15:21:35 -0800703
704 if (img->fmt == AOM_IMG_FMT_I42016) {
705 const uint16_t *srcY = (const uint16_t *)img->planes[AOM_PLANE_Y];
706 const uint16_t *srcU = (const uint16_t *)img->planes[AOM_PLANE_U];
707 const uint16_t *srcV = (const uint16_t *)img->planes[AOM_PLANE_V];
708
709 if (format == HAL_PIXEL_FORMAT_RGBA_1010102) {
710 convertYUV420Planar16ToY410((uint32_t *)dst, srcY, srcU, srcV, srcYStride / 2,
711 srcUStride / 2, srcVStride / 2,
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700712 dstYStride / sizeof(uint32_t),
Ray Essick707c1462018-12-05 15:21:35 -0800713 mWidth, mHeight);
714 } else {
715 convertYUV420Planar16ToYUV420Planar(dst, srcY, srcU, srcV, srcYStride / 2,
716 srcUStride / 2, srcVStride / 2,
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700717 dstYStride, dstUVStride,
Ray Essick707c1462018-12-05 15:21:35 -0800718 mWidth, mHeight);
719 }
720 } else {
721 const uint8_t *srcY = (const uint8_t *)img->planes[AOM_PLANE_Y];
722 const uint8_t *srcU = (const uint8_t *)img->planes[AOM_PLANE_U];
723 const uint8_t *srcV = (const uint8_t *)img->planes[AOM_PLANE_V];
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700724 copyOutputBufferToYuvPlanarFrame(
725 dst, srcY, srcU, srcV,
726 srcYStride, srcUStride, srcVStride,
727 dstYStride, dstUVStride,
728 mWidth, mHeight);
Ray Essick707c1462018-12-05 15:21:35 -0800729 }
730 finishWork(*(int64_t*)img->user_priv, work, std::move(block));
731 block = nullptr;
732 return true;
733}
734
735c2_status_t C2SoftAomDec::drainInternal(
736 uint32_t drainMode, const std::shared_ptr<C2BlockPool>& pool,
737 const std::unique_ptr<C2Work>& work) {
738 if (drainMode == NO_DRAIN) {
739 ALOGW("drain with NO_DRAIN: no-op");
740 return C2_OK;
741 }
742 if (drainMode == DRAIN_CHAIN) {
743 ALOGW("DRAIN_CHAIN not supported");
744 return C2_OMITTED;
745 }
746
747 if (aom_codec_decode(mCodecCtx, nullptr, 0, nullptr)) {
748 ALOGE("Failed to flush av1 decoder.");
749 return C2_CORRUPTED;
750 }
751
752 while ((outputBuffer(pool, work))) {
753 }
754
755 if (drainMode == DRAIN_COMPONENT_WITH_EOS && work &&
756 work->workletsProcessed == 0u) {
757 fillEmptyWork(work);
758 }
759
760 return C2_OK;
761}
762
763c2_status_t C2SoftAomDec::drain(uint32_t drainMode,
764 const std::shared_ptr<C2BlockPool>& pool) {
765 return drainInternal(drainMode, pool, nullptr);
766}
767
768class C2SoftAomFactory : public C2ComponentFactory {
769 public:
770 C2SoftAomFactory()
771 : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
772 GetCodec2PlatformComponentStore()->getParamReflector())) {}
773
774 virtual c2_status_t createComponent(
775 c2_node_id_t id, std::shared_ptr<C2Component>* const component,
776 std::function<void(C2Component*)> deleter) override {
777 *component = std::shared_ptr<C2Component>(
778 new C2SoftAomDec(COMPONENT_NAME, id,
779 std::make_shared<C2SoftAomDec::IntfImpl>(mHelper)),
780 deleter);
781 return C2_OK;
782 }
783
784 virtual c2_status_t createInterface(
785 c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
786 std::function<void(C2ComponentInterface*)> deleter) override {
787 *interface = std::shared_ptr<C2ComponentInterface>(
788 new SimpleInterface<C2SoftAomDec::IntfImpl>(
789 COMPONENT_NAME, id,
790 std::make_shared<C2SoftAomDec::IntfImpl>(mHelper)),
791 deleter);
792 return C2_OK;
793 }
794
795 virtual ~C2SoftAomFactory() override = default;
796
797 private:
798 std::shared_ptr<C2ReflectorHelper> mHelper;
799};
800
801} // namespace android
802
803extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
804 ALOGV("in %s", __func__);
805 return new ::android::C2SoftAomFactory();
806}
807
808extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
809 ALOGV("in %s", __func__);
810 delete factory;
811}