blob: 4bcc2c669eafa731b5ec4a494c20d9bc5ce8a07f [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
81 addParameter(DefineParam(mMaxSize, C2_PARAMKEY_MAX_PICTURE_SIZE)
82 .withDefault(new C2StreamMaxPictureSizeTuning::output(
83 0u, 320, 240))
84 .withFields({
85 C2F(mSize, width).inRange(2, 2048, 2),
86 C2F(mSize, height).inRange(2, 2048, 2),
87 })
88 .withSetter(MaxPictureSizeSetter, mSize)
89 .build());
90
91 addParameter(
92 DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
93 .withDefault(
94 new C2StreamMaxBufferSizeInfo::input(0u, 320 * 240 * 3 / 4))
95 .withFields({
96 C2F(mMaxInputSize, value).any(),
97 })
98 .calculatedAs(MaxInputSizeSetter, mMaxSize)
99 .build());
100
101 C2ChromaOffsetStruct locations[1] = {
102 C2ChromaOffsetStruct::ITU_YUV_420_0()};
103 std::shared_ptr<C2StreamColorInfo::output> defaultColorInfo =
104 C2StreamColorInfo::output::AllocShared(1u, 0u, 8u /* bitDepth */,
105 C2Color::YUV_420);
106 memcpy(defaultColorInfo->m.locations, locations, sizeof(locations));
107
108 defaultColorInfo = C2StreamColorInfo::output::AllocShared(
109 {C2ChromaOffsetStruct::ITU_YUV_420_0()}, 0u, 8u /* bitDepth */,
110 C2Color::YUV_420);
111 helper->addStructDescriptors<C2ChromaOffsetStruct>();
112
113 addParameter(DefineParam(mColorInfo, C2_PARAMKEY_CODED_COLOR_INFO)
114 .withConstValue(defaultColorInfo)
115 .build());
116
117 addParameter(
118 DefineParam(mDefaultColorAspects, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS)
119 .withDefault(new C2StreamColorAspectsTuning::output(
120 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
121 C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
122 .withFields({
123 C2F(mDefaultColorAspects, range).inRange(
124 C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
125 C2F(mDefaultColorAspects, primaries).inRange(
126 C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER),
127 C2F(mDefaultColorAspects, transfer).inRange(
128 C2Color::TRANSFER_UNSPECIFIED, C2Color::TRANSFER_OTHER),
129 C2F(mDefaultColorAspects, matrix).inRange(
130 C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)
131 })
132 .withSetter(DefaultColorAspectsSetter)
133 .build());
134
135 // TODO: support more formats?
136 addParameter(DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT)
137 .withConstValue(new C2StreamPixelFormatInfo::output(
138 0u, HAL_PIXEL_FORMAT_YCBCR_420_888))
139 .build());
140 }
141
142 static C2R SizeSetter(bool mayBlock,
143 const C2P<C2StreamPictureSizeInfo::output>& oldMe,
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800144 C2P<C2StreamPictureSizeInfo::output>& me) {
Ray Essick707c1462018-12-05 15:21:35 -0800145 (void)mayBlock;
146 C2R res = C2R::Ok();
147 if (!me.F(me.v.width).supportsAtAll(me.v.width)) {
148 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width)));
149 me.set().width = oldMe.v.width;
150 }
151 if (!me.F(me.v.height).supportsAtAll(me.v.height)) {
152 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height)));
153 me.set().height = oldMe.v.height;
154 }
155 return res;
156 }
157
158 static C2R MaxPictureSizeSetter(
159 bool mayBlock, C2P<C2StreamMaxPictureSizeTuning::output>& me,
160 const C2P<C2StreamPictureSizeInfo::output>& size) {
161 (void)mayBlock;
162 // TODO: get max width/height from the size's field helpers vs.
163 // hardcoding
164 me.set().width = c2_min(c2_max(me.v.width, size.v.width), 4096u);
165 me.set().height = c2_min(c2_max(me.v.height, size.v.height), 4096u);
166 return C2R::Ok();
167 }
168
169 static C2R MaxInputSizeSetter(
170 bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input>& me,
171 const C2P<C2StreamMaxPictureSizeTuning::output>& maxSize) {
172 (void)mayBlock;
173 // assume compression ratio of 2
174 me.set().value = (((maxSize.v.width + 63) / 64) *
175 ((maxSize.v.height + 63) / 64) * 3072);
176 return C2R::Ok();
177 }
178 static C2R DefaultColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsTuning::output> &me) {
179 (void)mayBlock;
180 if (me.v.range > C2Color::RANGE_OTHER) {
181 me.set().range = C2Color::RANGE_OTHER;
182 }
183 if (me.v.primaries > C2Color::PRIMARIES_OTHER) {
184 me.set().primaries = C2Color::PRIMARIES_OTHER;
185 }
186 if (me.v.transfer > C2Color::TRANSFER_OTHER) {
187 me.set().transfer = C2Color::TRANSFER_OTHER;
188 }
189 if (me.v.matrix > C2Color::MATRIX_OTHER) {
190 me.set().matrix = C2Color::MATRIX_OTHER;
191 }
192 return C2R::Ok();
193 }
194
195 static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me,
196 const C2P<C2StreamPictureSizeInfo::output> &size) {
197 (void)mayBlock;
198 (void)size;
199 (void)me; // TODO: validate
200 return C2R::Ok();
201 }
202 std::shared_ptr<C2StreamColorAspectsTuning::output> getDefaultColorAspects_l() {
203 return mDefaultColorAspects;
204 }
205
206 private:
207 std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
208 std::shared_ptr<C2StreamPictureSizeInfo::output> mSize;
209 std::shared_ptr<C2StreamMaxPictureSizeTuning::output> mMaxSize;
210 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize;
211 std::shared_ptr<C2StreamColorInfo::output> mColorInfo;
212 std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormat;
213 std::shared_ptr<C2StreamColorAspectsTuning::output> mDefaultColorAspects;
214};
215
216C2SoftAomDec::C2SoftAomDec(const char* name, c2_node_id_t id,
217 const std::shared_ptr<IntfImpl>& intfImpl)
218 : SimpleC2Component(
219 std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
220 mIntf(intfImpl),
221 mCodecCtx(nullptr){
222
223 GENERATE_FILE_NAMES();
224 CREATE_DUMP_FILE(mInFile);
225 CREATE_DUMP_FILE(mOutFile);
226
227 gettimeofday(&mTimeStart, nullptr);
228 gettimeofday(&mTimeEnd, nullptr);
229}
230
231C2SoftAomDec::~C2SoftAomDec() {
232 onRelease();
233}
234
235c2_status_t C2SoftAomDec::onInit() {
236 status_t err = initDecoder();
237 return err == OK ? C2_OK : C2_CORRUPTED;
238}
239
240c2_status_t C2SoftAomDec::onStop() {
241 mSignalledError = false;
242 mSignalledOutputEos = false;
243 return C2_OK;
244}
245
246void C2SoftAomDec::onReset() {
247 (void)onStop();
248 c2_status_t err = onFlush_sm();
249 if (err != C2_OK) {
250 ALOGW("Failed to flush decoder. Try to hard reset decoder.");
251 destroyDecoder();
252 (void)initDecoder();
253 }
254}
255
256void C2SoftAomDec::onRelease() {
257 destroyDecoder();
258}
259
260c2_status_t C2SoftAomDec::onFlush_sm() {
261 if (aom_codec_decode(mCodecCtx, nullptr, 0, nullptr)) {
262 ALOGE("Failed to flush av1 decoder.");
263 return C2_CORRUPTED;
264 }
265
266 aom_codec_iter_t iter = nullptr;
267 while (aom_codec_get_frame(mCodecCtx, &iter)) {
268 }
269
270 mSignalledError = false;
271 mSignalledOutputEos = false;
272
273 return C2_OK;
274}
275
276static int GetCPUCoreCount() {
277 int cpuCoreCount = 1;
278#if defined(_SC_NPROCESSORS_ONLN)
279 cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
280#else
281 // _SC_NPROC_ONLN must be defined...
282 cpuCoreCount = sysconf(_SC_NPROC_ONLN);
283#endif
284 CHECK(cpuCoreCount >= 1);
285 ALOGV("Number of CPU cores: %d", cpuCoreCount);
286 return cpuCoreCount;
287}
288
289status_t C2SoftAomDec::initDecoder() {
290 mSignalledError = false;
291 mSignalledOutputEos = false;
292 if (!mCodecCtx) {
293 mCodecCtx = new aom_codec_ctx_t;
294 }
295
296 if (!mCodecCtx) {
297 ALOGE("mCodecCtx is null");
298 return NO_MEMORY;
299 }
300
301 aom_codec_dec_cfg_t cfg;
302 memset(&cfg, 0, sizeof(aom_codec_dec_cfg_t));
303 cfg.threads = GetCPUCoreCount();
304 cfg.allow_lowbitdepth = 1;
305
306 aom_codec_flags_t flags;
307 memset(&flags, 0, sizeof(aom_codec_flags_t));
308
309 aom_codec_err_t err;
310 if ((err = aom_codec_dec_init(mCodecCtx, aom_codec_av1_dx(), &cfg, 0))) {
311 ALOGE("av1 decoder failed to initialize. (%d)", err);
312 return UNKNOWN_ERROR;
313 }
314
315 return OK;
316}
317
318status_t C2SoftAomDec::destroyDecoder() {
319 if (mCodecCtx) {
320 aom_codec_destroy(mCodecCtx);
321 delete mCodecCtx;
322 mCodecCtx = nullptr;
323 }
324 return OK;
325}
326
327void fillEmptyWork(const std::unique_ptr<C2Work>& work) {
328 uint32_t flags = 0;
329 if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
330 flags |= C2FrameData::FLAG_END_OF_STREAM;
331 ALOGV("signalling eos");
332 }
333 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
334 work->worklets.front()->output.buffers.clear();
335 work->worklets.front()->output.ordinal = work->input.ordinal;
336 work->workletsProcessed = 1u;
337}
338
339void C2SoftAomDec::finishWork(uint64_t index,
340 const std::unique_ptr<C2Work>& work,
341 const std::shared_ptr<C2GraphicBlock>& block) {
342 std::shared_ptr<C2Buffer> buffer =
343 createGraphicBuffer(block, C2Rect(mWidth, mHeight));
344 auto fillWork = [buffer, index](const std::unique_ptr<C2Work>& work) {
345 uint32_t flags = 0;
346 if ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) &&
347 (c2_cntr64_t(index) == work->input.ordinal.frameIndex)) {
348 flags |= C2FrameData::FLAG_END_OF_STREAM;
349 ALOGV("signalling eos");
350 }
351 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
352 work->worklets.front()->output.buffers.clear();
353 work->worklets.front()->output.buffers.push_back(buffer);
354 work->worklets.front()->output.ordinal = work->input.ordinal;
355 work->workletsProcessed = 1u;
356 };
357 if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) {
358 fillWork(work);
359 } else {
360 finish(index, fillWork);
361 }
362}
363
364void C2SoftAomDec::process(const std::unique_ptr<C2Work>& work,
365 const std::shared_ptr<C2BlockPool>& pool) {
366 work->result = C2_OK;
367 work->workletsProcessed = 0u;
368 work->worklets.front()->output.configUpdate.clear();
369 work->worklets.front()->output.flags = work->input.flags;
370 if (mSignalledError || mSignalledOutputEos) {
371 work->result = C2_BAD_VALUE;
372 return;
373 }
374
375 size_t inOffset = 0u;
376 size_t inSize = 0u;
377 C2ReadView rView = mDummyReadView;
378 if (!work->input.buffers.empty()) {
379 rView =
380 work->input.buffers[0]->data().linearBlocks().front().map().get();
381 inSize = rView.capacity();
382 if (inSize && rView.error()) {
383 ALOGE("read view map failed %d", rView.error());
384 work->result = C2_CORRUPTED;
385 return;
386 }
387 }
388
389 bool codecConfig =
390 ((work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) != 0);
391 bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
392
393 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
394 inSize, (int)work->input.ordinal.timestamp.peeku(),
395 (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
396
397 if (codecConfig) {
398 fillEmptyWork(work);
399 return;
400 }
401
402 int64_t frameIndex = work->input.ordinal.frameIndex.peekll();
403 if (inSize) {
404 uint8_t* bitstream = const_cast<uint8_t*>(rView.data() + inOffset);
405 int32_t decodeTime = 0;
406 int32_t delay = 0;
407
408 DUMP_TO_FILE(mOutFile, bitstream, inSize);
409 GETTIME(&mTimeStart, nullptr);
410 TIME_DIFF(mTimeEnd, mTimeStart, delay);
411
412 aom_codec_err_t err =
413 aom_codec_decode(mCodecCtx, bitstream, inSize, &frameIndex);
414
415 GETTIME(&mTimeEnd, nullptr);
416 TIME_DIFF(mTimeStart, mTimeEnd, decodeTime);
417 ALOGV("decodeTime=%4d delay=%4d\n", decodeTime, delay);
418
419 if (err != AOM_CODEC_OK) {
420 ALOGE("av1 decoder failed to decode frame err: %d", err);
421 work->result = C2_CORRUPTED;
422 work->workletsProcessed = 1u;
423 mSignalledError = true;
424 return;
425 }
426
427 } else {
428 if (aom_codec_decode(mCodecCtx, nullptr, 0, nullptr)) {
429 ALOGE("Failed to flush av1 decoder.");
430 work->result = C2_CORRUPTED;
431 work->workletsProcessed = 1u;
432 mSignalledError = true;
433 return;
434 }
435 }
436
437 (void)outputBuffer(pool, work);
438
439 if (eos) {
440 drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work);
441 mSignalledOutputEos = true;
442 } else if (!inSize) {
443 fillEmptyWork(work);
444 }
445}
446
447static void copyOutputBufferToYV12Frame(uint8_t *dst,
448 const uint8_t *srcY, const uint8_t *srcU, const uint8_t *srcV,
449 size_t srcYStride, size_t srcUStride, size_t srcVStride,
450 uint32_t width, uint32_t height) {
451 size_t dstYStride = align(width, 16);
452 size_t dstUVStride = align(dstYStride / 2, 16);
453 uint8_t* dstStart = dst;
454
455
456 for (size_t i = 0; i < height; ++i) {
457 memcpy(dst, srcY, width);
458 srcY += srcYStride;
459 dst += dstYStride;
460 }
461
462 dst = dstStart + dstYStride * height;
463 for (size_t i = 0; i < height / 2; ++i) {
464 memcpy(dst, srcV, width / 2);
465 srcV += srcVStride;
466 dst += dstUVStride;
467 }
468
469 dst = dstStart + (dstYStride * height) + (dstUVStride * height / 2);
470 for (size_t i = 0; i < height / 2; ++i) {
471 memcpy(dst, srcU, width / 2);
472 srcU += srcUStride;
473 dst += dstUVStride;
474 }
475}
476
477static void convertYUV420Planar16ToY410(uint32_t *dst,
478 const uint16_t *srcY, const uint16_t *srcU, const uint16_t *srcV,
479 size_t srcYStride, size_t srcUStride, size_t srcVStride,
480 size_t dstStride, size_t width, size_t height) {
481
482 // Converting two lines at a time, slightly faster
483 for (size_t y = 0; y < height; y += 2) {
484 uint32_t *dstTop = (uint32_t *) dst;
485 uint32_t *dstBot = (uint32_t *) (dst + dstStride);
486 uint16_t *ySrcTop = (uint16_t*) srcY;
487 uint16_t *ySrcBot = (uint16_t*) (srcY + srcYStride);
488 uint16_t *uSrc = (uint16_t*) srcU;
489 uint16_t *vSrc = (uint16_t*) srcV;
490
491 uint32_t u01, v01, y01, y23, y45, y67, uv0, uv1;
492 size_t x = 0;
493 for (; x < width - 3; x += 4) {
494
495 u01 = *((uint32_t*)uSrc); uSrc += 2;
496 v01 = *((uint32_t*)vSrc); vSrc += 2;
497
498 y01 = *((uint32_t*)ySrcTop); ySrcTop += 2;
499 y23 = *((uint32_t*)ySrcTop); ySrcTop += 2;
500 y45 = *((uint32_t*)ySrcBot); ySrcBot += 2;
501 y67 = *((uint32_t*)ySrcBot); ySrcBot += 2;
502
503 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
504 uv1 = (u01 >> 16) | ((v01 >> 16) << 20);
505
506 *dstTop++ = 3 << 30 | ((y01 & 0x3FF) << 10) | uv0;
507 *dstTop++ = 3 << 30 | ((y01 >> 16) << 10) | uv0;
508 *dstTop++ = 3 << 30 | ((y23 & 0x3FF) << 10) | uv1;
509 *dstTop++ = 3 << 30 | ((y23 >> 16) << 10) | uv1;
510
511 *dstBot++ = 3 << 30 | ((y45 & 0x3FF) << 10) | uv0;
512 *dstBot++ = 3 << 30 | ((y45 >> 16) << 10) | uv0;
513 *dstBot++ = 3 << 30 | ((y67 & 0x3FF) << 10) | uv1;
514 *dstBot++ = 3 << 30 | ((y67 >> 16) << 10) | uv1;
515 }
516
517 // There should be at most 2 more pixels to process. Note that we don't
518 // need to consider odd case as the buffer is always aligned to even.
519 if (x < width) {
520 u01 = *uSrc;
521 v01 = *vSrc;
522 y01 = *((uint32_t*)ySrcTop);
523 y45 = *((uint32_t*)ySrcBot);
524 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
525 *dstTop++ = ((y01 & 0x3FF) << 10) | uv0;
526 *dstTop++ = ((y01 >> 16) << 10) | uv0;
527 *dstBot++ = ((y45 & 0x3FF) << 10) | uv0;
528 *dstBot++ = ((y45 >> 16) << 10) | uv0;
529 }
530
531 srcY += srcYStride * 2;
532 srcU += srcUStride;
533 srcV += srcVStride;
534 dst += dstStride * 2;
535 }
536
537 return;
538}
539
540static void convertYUV420Planar16ToYUV420Planar(uint8_t *dst,
541 const uint16_t *srcY, const uint16_t *srcU, const uint16_t *srcV,
542 size_t srcYStride, size_t srcUStride, size_t srcVStride,
543 size_t dstStride, size_t width, size_t height) {
544
545 uint8_t *dstY = (uint8_t *)dst;
546 size_t dstYSize = dstStride * height;
547 size_t dstUVStride = align(dstStride / 2, 16);
548 size_t dstUVSize = dstUVStride * height / 2;
549 uint8_t *dstV = dstY + dstYSize;
550 uint8_t *dstU = dstV + dstUVSize;
551
552 for (size_t y = 0; y < height; ++y) {
553 for (size_t x = 0; x < width; ++x) {
554 dstY[x] = (uint8_t)(srcY[x] >> 2);
555 }
556
557 srcY += srcYStride;
558 dstY += dstStride;
559 }
560
561 for (size_t y = 0; y < (height + 1) / 2; ++y) {
562 for (size_t x = 0; x < (width + 1) / 2; ++x) {
563 dstU[x] = (uint8_t)(srcU[x] >> 2);
564 dstV[x] = (uint8_t)(srcV[x] >> 2);
565 }
566
567 srcU += srcUStride;
568 srcV += srcVStride;
569 dstU += dstUVStride;
570 dstV += dstUVStride;
571 }
572 return;
573}
574bool C2SoftAomDec::outputBuffer(
575 const std::shared_ptr<C2BlockPool> &pool,
576 const std::unique_ptr<C2Work> &work)
577{
578 if (!(work && pool)) return false;
579
580 aom_codec_iter_t iter = nullptr;
581 aom_image_t* img = aom_codec_get_frame(mCodecCtx, &iter);
582
583 if (!img) return false;
584
585 if (img->d_w != mWidth || img->d_h != mHeight) {
586 mWidth = img->d_w;
587 mHeight = img->d_h;
588
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800589 C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight);
Ray Essick707c1462018-12-05 15:21:35 -0800590 std::vector<std::unique_ptr<C2SettingResult>> failures;
591 c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures);
592 if (err == C2_OK) {
593 work->worklets.front()->output.configUpdate.push_back(
594 C2Param::Copy(size));
595 } else {
596 ALOGE("Config update size failed");
597 mSignalledError = true;
598 work->result = C2_CORRUPTED;
599 work->workletsProcessed = 1u;
600 return false;
601 }
602 }
603
604 CHECK(img->fmt == AOM_IMG_FMT_I420 || img->fmt == AOM_IMG_FMT_I42016);
605
606 std::shared_ptr<C2GraphicBlock> block;
607 uint32_t format = HAL_PIXEL_FORMAT_YV12;
608 if (img->fmt == AOM_IMG_FMT_I42016) {
609 IntfImpl::Lock lock = mIntf->lock();
610 std::shared_ptr<C2StreamColorAspectsTuning::output> defaultColorAspects = mIntf->getDefaultColorAspects_l();
611
612 if (defaultColorAspects->primaries == C2Color::PRIMARIES_BT2020 &&
613 defaultColorAspects->matrix == C2Color::MATRIX_BT2020 &&
614 defaultColorAspects->transfer == C2Color::TRANSFER_ST2084) {
615 format = HAL_PIXEL_FORMAT_RGBA_1010102;
616 }
617 }
618 C2MemoryUsage usage = {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
619
620 c2_status_t err = pool->fetchGraphicBlock(align(mWidth, 16), mHeight,
621 format, usage, &block);
622
623 if (err != C2_OK) {
624 ALOGE("fetchGraphicBlock for Output failed with status %d", err);
625 work->result = err;
626 return false;
627 }
628
629 C2GraphicView wView = block->map().get();
630
631 if (wView.error()) {
632 ALOGE("graphic view map failed %d", wView.error());
633 work->result = C2_CORRUPTED;
634 return false;
635 }
636
637 ALOGV("provided (%dx%d) required (%dx%d), out frameindex %d",
638 block->width(), block->height(), mWidth, mHeight,
639 (int)*(int64_t*)img->user_priv);
640
641 uint8_t* dst = const_cast<uint8_t*>(wView.data()[C2PlanarLayout::PLANE_Y]);
642 size_t srcYStride = img->stride[AOM_PLANE_Y];
643 size_t srcUStride = img->stride[AOM_PLANE_U];
644 size_t srcVStride = img->stride[AOM_PLANE_V];
645
646 if (img->fmt == AOM_IMG_FMT_I42016) {
647 const uint16_t *srcY = (const uint16_t *)img->planes[AOM_PLANE_Y];
648 const uint16_t *srcU = (const uint16_t *)img->planes[AOM_PLANE_U];
649 const uint16_t *srcV = (const uint16_t *)img->planes[AOM_PLANE_V];
650
651 if (format == HAL_PIXEL_FORMAT_RGBA_1010102) {
652 convertYUV420Planar16ToY410((uint32_t *)dst, srcY, srcU, srcV, srcYStride / 2,
653 srcUStride / 2, srcVStride / 2,
654 align(mWidth, 16),
655 mWidth, mHeight);
656 } else {
657 convertYUV420Planar16ToYUV420Planar(dst, srcY, srcU, srcV, srcYStride / 2,
658 srcUStride / 2, srcVStride / 2,
659 align(mWidth, 16),
660 mWidth, mHeight);
661 }
662 } else {
663 const uint8_t *srcY = (const uint8_t *)img->planes[AOM_PLANE_Y];
664 const uint8_t *srcU = (const uint8_t *)img->planes[AOM_PLANE_U];
665 const uint8_t *srcV = (const uint8_t *)img->planes[AOM_PLANE_V];
666 copyOutputBufferToYV12Frame(dst, srcY, srcU, srcV,
667 srcYStride, srcUStride, srcVStride, mWidth, mHeight);
668 }
669 finishWork(*(int64_t*)img->user_priv, work, std::move(block));
670 block = nullptr;
671 return true;
672}
673
674c2_status_t C2SoftAomDec::drainInternal(
675 uint32_t drainMode, const std::shared_ptr<C2BlockPool>& pool,
676 const std::unique_ptr<C2Work>& work) {
677 if (drainMode == NO_DRAIN) {
678 ALOGW("drain with NO_DRAIN: no-op");
679 return C2_OK;
680 }
681 if (drainMode == DRAIN_CHAIN) {
682 ALOGW("DRAIN_CHAIN not supported");
683 return C2_OMITTED;
684 }
685
686 if (aom_codec_decode(mCodecCtx, nullptr, 0, nullptr)) {
687 ALOGE("Failed to flush av1 decoder.");
688 return C2_CORRUPTED;
689 }
690
691 while ((outputBuffer(pool, work))) {
692 }
693
694 if (drainMode == DRAIN_COMPONENT_WITH_EOS && work &&
695 work->workletsProcessed == 0u) {
696 fillEmptyWork(work);
697 }
698
699 return C2_OK;
700}
701
702c2_status_t C2SoftAomDec::drain(uint32_t drainMode,
703 const std::shared_ptr<C2BlockPool>& pool) {
704 return drainInternal(drainMode, pool, nullptr);
705}
706
707class C2SoftAomFactory : public C2ComponentFactory {
708 public:
709 C2SoftAomFactory()
710 : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
711 GetCodec2PlatformComponentStore()->getParamReflector())) {}
712
713 virtual c2_status_t createComponent(
714 c2_node_id_t id, std::shared_ptr<C2Component>* const component,
715 std::function<void(C2Component*)> deleter) override {
716 *component = std::shared_ptr<C2Component>(
717 new C2SoftAomDec(COMPONENT_NAME, id,
718 std::make_shared<C2SoftAomDec::IntfImpl>(mHelper)),
719 deleter);
720 return C2_OK;
721 }
722
723 virtual c2_status_t createInterface(
724 c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
725 std::function<void(C2ComponentInterface*)> deleter) override {
726 *interface = std::shared_ptr<C2ComponentInterface>(
727 new SimpleInterface<C2SoftAomDec::IntfImpl>(
728 COMPONENT_NAME, id,
729 std::make_shared<C2SoftAomDec::IntfImpl>(mHelper)),
730 deleter);
731 return C2_OK;
732 }
733
734 virtual ~C2SoftAomFactory() override = default;
735
736 private:
737 std::shared_ptr<C2ReflectorHelper> mHelper;
738};
739
740} // namespace android
741
742extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
743 ALOGV("in %s", __func__);
744 return new ::android::C2SoftAomFactory();
745}
746
747extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
748 ALOGV("in %s", __func__);
749 delete factory;
750}