blob: 42f507f317178639bd71648d32b280504a56998a [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -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 "C2SoftVpxDec"
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 "C2SoftVpxDec.h"
29
30namespace android {
31
32#ifdef VP9
33constexpr char COMPONENT_NAME[] = "c2.android.vp9.decoder";
34#else
35constexpr char COMPONENT_NAME[] = "c2.android.vp8.decoder";
36#endif
37
38class C2SoftVpxDec::IntfImpl : public SimpleInterface<void>::BaseParams {
39public:
40 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
41 : SimpleInterface<void>::BaseParams(
42 helper,
43 COMPONENT_NAME,
44 C2Component::KIND_DECODER,
45 C2Component::DOMAIN_VIDEO,
46#ifdef VP9
47 MEDIA_MIMETYPE_VIDEO_VP9
48#else
49 MEDIA_MIMETYPE_VIDEO_VP8
50#endif
51 ) {
52 noPrivateBuffers(); // TODO: account for our buffers here
53 noInputReferences();
54 noOutputReferences();
55 noInputLatency();
56 noTimeStretch();
57
58 // TODO: output latency and reordering
59
60 addParameter(
61 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
62 .withConstValue(new C2ComponentAttributesSetting(C2Component::ATTRIB_IS_TEMPORAL))
63 .build());
64
65 addParameter(
66 DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE)
67 .withDefault(new C2StreamPictureSizeInfo::output(0u, 320, 240))
68 .withFields({
69 C2F(mSize, width).inRange(2, 2048, 2),
70 C2F(mSize, height).inRange(2, 2048, 2),
71 })
72 .withSetter(SizeSetter)
73 .build());
74
75#ifdef VP9
76 // TODO: Add C2Config::PROFILE_VP9_2HDR ??
77 addParameter(
78 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
79 .withDefault(new C2StreamProfileLevelInfo::input(0u,
80 C2Config::PROFILE_VP9_0, C2Config::LEVEL_VP9_5))
81 .withFields({
82 C2F(mProfileLevel, profile).oneOf({
83 C2Config::PROFILE_VP9_0,
84 C2Config::PROFILE_VP9_2}),
85 C2F(mProfileLevel, level).oneOf({
86 C2Config::LEVEL_VP9_1,
87 C2Config::LEVEL_VP9_1_1,
88 C2Config::LEVEL_VP9_2,
89 C2Config::LEVEL_VP9_2_1,
90 C2Config::LEVEL_VP9_3,
91 C2Config::LEVEL_VP9_3_1,
92 C2Config::LEVEL_VP9_4,
93 C2Config::LEVEL_VP9_4_1,
94 C2Config::LEVEL_VP9_5,
95 })
96 })
97 .withSetter(ProfileLevelSetter, mSize)
98 .build());
99
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800100 mHdr10PlusInfoInput = C2StreamHdr10PlusInfo::input::AllocShared(0);
101 addParameter(
102 DefineParam(mHdr10PlusInfoInput, C2_PARAMKEY_INPUT_HDR10_PLUS_INFO)
103 .withDefault(mHdr10PlusInfoInput)
104 .withFields({
105 C2F(mHdr10PlusInfoInput, m.value).any(),
106 })
107 .withSetter(Hdr10PlusInfoInputSetter)
108 .build());
109
110 mHdr10PlusInfoOutput = C2StreamHdr10PlusInfo::output::AllocShared(0);
111 addParameter(
112 DefineParam(mHdr10PlusInfoOutput, C2_PARAMKEY_OUTPUT_HDR10_PLUS_INFO)
113 .withDefault(mHdr10PlusInfoOutput)
114 .withFields({
115 C2F(mHdr10PlusInfoOutput, m.value).any(),
116 })
117 .withSetter(Hdr10PlusInfoOutputSetter)
118 .build());
119
Pawin Vongmasa36653902018-11-15 00:10:25 -0800120#if 0
121 // sample BT.2020 static info
122 mHdrStaticInfo = std::make_shared<C2StreamHdrStaticInfo::output>();
123 mHdrStaticInfo->mastering = {
124 .red = { .x = 0.708, .y = 0.292 },
125 .green = { .x = 0.170, .y = 0.797 },
126 .blue = { .x = 0.131, .y = 0.046 },
127 .white = { .x = 0.3127, .y = 0.3290 },
128 .maxLuminance = 1000,
129 .minLuminance = 0.1,
130 };
131 mHdrStaticInfo->maxCll = 1000;
132 mHdrStaticInfo->maxFall = 120;
133
134 mHdrStaticInfo->maxLuminance = 0; // disable static info
135
136 helper->addStructDescriptors<C2MasteringDisplayColorVolumeStruct, C2ColorXyStruct>();
137 addParameter(
138 DefineParam(mHdrStaticInfo, C2_PARAMKEY_HDR_STATIC_INFO)
139 .withDefault(mHdrStaticInfo)
140 .withFields({
141 C2F(mHdrStaticInfo, mastering.red.x).inRange(0, 1),
142 // TODO
143 })
144 .withSetter(HdrStaticInfoSetter)
145 .build());
146#endif
147#else
148 addParameter(
149 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
150 .withConstValue(new C2StreamProfileLevelInfo::input(0u,
151 C2Config::PROFILE_UNUSED, C2Config::LEVEL_UNUSED))
152 .build());
153#endif
154
155 addParameter(
156 DefineParam(mMaxSize, C2_PARAMKEY_MAX_PICTURE_SIZE)
157 .withDefault(new C2StreamMaxPictureSizeTuning::output(0u, 320, 240))
158 .withFields({
159 C2F(mSize, width).inRange(2, 2048, 2),
160 C2F(mSize, height).inRange(2, 2048, 2),
161 })
162 .withSetter(MaxPictureSizeSetter, mSize)
163 .build());
164
165 addParameter(
166 DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
167 .withDefault(new C2StreamMaxBufferSizeInfo::input(0u, 320 * 240 * 3 / 4))
168 .withFields({
169 C2F(mMaxInputSize, value).any(),
170 })
171 .calculatedAs(MaxInputSizeSetter, mMaxSize)
172 .build());
173
174 C2ChromaOffsetStruct locations[1] = { C2ChromaOffsetStruct::ITU_YUV_420_0() };
175 std::shared_ptr<C2StreamColorInfo::output> defaultColorInfo =
176 C2StreamColorInfo::output::AllocShared(
177 1u, 0u, 8u /* bitDepth */, C2Color::YUV_420);
178 memcpy(defaultColorInfo->m.locations, locations, sizeof(locations));
179
180 defaultColorInfo =
181 C2StreamColorInfo::output::AllocShared(
182 { C2ChromaOffsetStruct::ITU_YUV_420_0() },
183 0u, 8u /* bitDepth */, C2Color::YUV_420);
184 helper->addStructDescriptors<C2ChromaOffsetStruct>();
185
186 addParameter(
187 DefineParam(mColorInfo, C2_PARAMKEY_CODED_COLOR_INFO)
188 .withConstValue(defaultColorInfo)
189 .build());
190
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700191 addParameter(
192 DefineParam(mDefaultColorAspects, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS)
193 .withDefault(new C2StreamColorAspectsTuning::output(
194 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
195 C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
196 .withFields({
197 C2F(mDefaultColorAspects, range).inRange(
198 C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
199 C2F(mDefaultColorAspects, primaries).inRange(
200 C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER),
201 C2F(mDefaultColorAspects, transfer).inRange(
202 C2Color::TRANSFER_UNSPECIFIED, C2Color::TRANSFER_OTHER),
203 C2F(mDefaultColorAspects, matrix).inRange(
204 C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)
205 })
206 .withSetter(DefaultColorAspectsSetter)
207 .build());
208
Pawin Vongmasa36653902018-11-15 00:10:25 -0800209 // TODO: support more formats?
210 addParameter(
211 DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT)
212 .withConstValue(new C2StreamPixelFormatInfo::output(
213 0u, HAL_PIXEL_FORMAT_YCBCR_420_888))
214 .build());
215 }
216
217 static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::output> &oldMe,
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800218 C2P<C2StreamPictureSizeInfo::output> &me) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800219 (void)mayBlock;
220 C2R res = C2R::Ok();
221 if (!me.F(me.v.width).supportsAtAll(me.v.width)) {
222 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width)));
223 me.set().width = oldMe.v.width;
224 }
225 if (!me.F(me.v.height).supportsAtAll(me.v.height)) {
226 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height)));
227 me.set().height = oldMe.v.height;
228 }
229 return res;
230 }
231
232 static C2R MaxPictureSizeSetter(bool mayBlock, C2P<C2StreamMaxPictureSizeTuning::output> &me,
233 const C2P<C2StreamPictureSizeInfo::output> &size) {
234 (void)mayBlock;
235 // TODO: get max width/height from the size's field helpers vs. hardcoding
236 me.set().width = c2_min(c2_max(me.v.width, size.v.width), 2048u);
237 me.set().height = c2_min(c2_max(me.v.height, size.v.height), 2048u);
238 return C2R::Ok();
239 }
240
241 static C2R MaxInputSizeSetter(bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input> &me,
242 const C2P<C2StreamMaxPictureSizeTuning::output> &maxSize) {
243 (void)mayBlock;
244 // assume compression ratio of 2
245 me.set().value = (((maxSize.v.width + 63) / 64) * ((maxSize.v.height + 63) / 64) * 3072);
246 return C2R::Ok();
247 }
248
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700249 static C2R DefaultColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsTuning::output> &me) {
250 (void)mayBlock;
251 if (me.v.range > C2Color::RANGE_OTHER) {
252 me.set().range = C2Color::RANGE_OTHER;
253 }
254 if (me.v.primaries > C2Color::PRIMARIES_OTHER) {
255 me.set().primaries = C2Color::PRIMARIES_OTHER;
256 }
257 if (me.v.transfer > C2Color::TRANSFER_OTHER) {
258 me.set().transfer = C2Color::TRANSFER_OTHER;
259 }
260 if (me.v.matrix > C2Color::MATRIX_OTHER) {
261 me.set().matrix = C2Color::MATRIX_OTHER;
262 }
263 return C2R::Ok();
264 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800265
266 static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me,
267 const C2P<C2StreamPictureSizeInfo::output> &size) {
268 (void)mayBlock;
269 (void)size;
270 (void)me; // TODO: validate
271 return C2R::Ok();
272 }
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700273 std::shared_ptr<C2StreamColorAspectsTuning::output> getDefaultColorAspects_l() {
274 return mDefaultColorAspects;
275 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800276
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800277 static C2R Hdr10PlusInfoInputSetter(bool mayBlock, C2P<C2StreamHdr10PlusInfo::input> &me) {
278 (void)mayBlock;
279 (void)me; // TODO: validate
280 return C2R::Ok();
281 }
282
283 static C2R Hdr10PlusInfoOutputSetter(bool mayBlock, C2P<C2StreamHdr10PlusInfo::output> &me) {
284 (void)mayBlock;
285 (void)me; // TODO: validate
286 return C2R::Ok();
287 }
288
Pawin Vongmasa36653902018-11-15 00:10:25 -0800289private:
290 std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
291 std::shared_ptr<C2StreamPictureSizeInfo::output> mSize;
292 std::shared_ptr<C2StreamMaxPictureSizeTuning::output> mMaxSize;
293 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize;
294 std::shared_ptr<C2StreamColorInfo::output> mColorInfo;
295 std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormat;
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700296 std::shared_ptr<C2StreamColorAspectsTuning::output> mDefaultColorAspects;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800297#ifdef VP9
298#if 0
299 std::shared_ptr<C2StreamHdrStaticInfo::output> mHdrStaticInfo;
300#endif
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800301 std::shared_ptr<C2StreamHdr10PlusInfo::input> mHdr10PlusInfoInput;
302 std::shared_ptr<C2StreamHdr10PlusInfo::output> mHdr10PlusInfoOutput;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800303#endif
304};
305
306C2SoftVpxDec::C2SoftVpxDec(
307 const char *name,
308 c2_node_id_t id,
309 const std::shared_ptr<IntfImpl> &intfImpl)
310 : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
311 mIntf(intfImpl),
312 mCodecCtx(nullptr) {
313}
314
315C2SoftVpxDec::~C2SoftVpxDec() {
316 onRelease();
317}
318
319c2_status_t C2SoftVpxDec::onInit() {
320 status_t err = initDecoder();
321 return err == OK ? C2_OK : C2_CORRUPTED;
322}
323
324c2_status_t C2SoftVpxDec::onStop() {
325 mSignalledError = false;
326 mSignalledOutputEos = false;
327
328 return C2_OK;
329}
330
331void C2SoftVpxDec::onReset() {
332 (void)onStop();
333 c2_status_t err = onFlush_sm();
334 if (err != C2_OK)
335 {
336 ALOGW("Failed to flush decoder. Try to hard reset decoder");
337 destroyDecoder();
338 (void)initDecoder();
339 }
340}
341
342void C2SoftVpxDec::onRelease() {
343 destroyDecoder();
344}
345
346c2_status_t C2SoftVpxDec::onFlush_sm() {
347 if (mFrameParallelMode) {
348 // Flush decoder by passing nullptr data ptr and 0 size.
349 // Ideally, this should never fail.
350 if (vpx_codec_decode(mCodecCtx, nullptr, 0, nullptr, 0)) {
351 ALOGE("Failed to flush on2 decoder.");
352 return C2_CORRUPTED;
353 }
354 }
355
356 // Drop all the decoded frames in decoder.
357 vpx_codec_iter_t iter = nullptr;
358 while (vpx_codec_get_frame(mCodecCtx, &iter)) {
359 }
360
361 mSignalledError = false;
362 mSignalledOutputEos = false;
363 return C2_OK;
364}
365
366static int GetCPUCoreCount() {
367 int cpuCoreCount = 1;
368#if defined(_SC_NPROCESSORS_ONLN)
369 cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
370#else
371 // _SC_NPROC_ONLN must be defined...
372 cpuCoreCount = sysconf(_SC_NPROC_ONLN);
373#endif
374 CHECK(cpuCoreCount >= 1);
375 ALOGV("Number of CPU cores: %d", cpuCoreCount);
376 return cpuCoreCount;
377}
378
379status_t C2SoftVpxDec::initDecoder() {
380#ifdef VP9
381 mMode = MODE_VP9;
382#else
383 mMode = MODE_VP8;
384#endif
385
386 mWidth = 320;
387 mHeight = 240;
388 mFrameParallelMode = false;
389 mSignalledOutputEos = false;
390 mSignalledError = false;
391
392 if (!mCodecCtx) {
393 mCodecCtx = new vpx_codec_ctx_t;
394 }
395 if (!mCodecCtx) {
396 ALOGE("mCodecCtx is null");
397 return NO_MEMORY;
398 }
399
400 vpx_codec_dec_cfg_t cfg;
401 memset(&cfg, 0, sizeof(vpx_codec_dec_cfg_t));
402 cfg.threads = GetCPUCoreCount();
403
404 vpx_codec_flags_t flags;
405 memset(&flags, 0, sizeof(vpx_codec_flags_t));
406 if (mFrameParallelMode) flags |= VPX_CODEC_USE_FRAME_THREADING;
407
408 vpx_codec_err_t vpx_err;
409 if ((vpx_err = vpx_codec_dec_init(
410 mCodecCtx, mMode == MODE_VP8 ? &vpx_codec_vp8_dx_algo : &vpx_codec_vp9_dx_algo,
411 &cfg, flags))) {
412 ALOGE("on2 decoder failed to initialize. (%d)", vpx_err);
413 return UNKNOWN_ERROR;
414 }
415
416 return OK;
417}
418
419status_t C2SoftVpxDec::destroyDecoder() {
420 if (mCodecCtx) {
421 vpx_codec_destroy(mCodecCtx);
422 delete mCodecCtx;
423 mCodecCtx = nullptr;
424 }
425
426 return OK;
427}
428
429void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
430 uint32_t flags = 0;
431 if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
432 flags |= C2FrameData::FLAG_END_OF_STREAM;
433 ALOGV("signalling eos");
434 }
435 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
436 work->worklets.front()->output.buffers.clear();
437 work->worklets.front()->output.ordinal = work->input.ordinal;
438 work->workletsProcessed = 1u;
439}
440
441void C2SoftVpxDec::finishWork(uint64_t index, const std::unique_ptr<C2Work> &work,
442 const std::shared_ptr<C2GraphicBlock> &block) {
443 std::shared_ptr<C2Buffer> buffer = createGraphicBuffer(block,
444 C2Rect(mWidth, mHeight));
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800445 auto fillWork = [buffer, index, intf = this->mIntf](
446 const std::unique_ptr<C2Work> &work) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800447 uint32_t flags = 0;
448 if ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) &&
449 (c2_cntr64_t(index) == work->input.ordinal.frameIndex)) {
450 flags |= C2FrameData::FLAG_END_OF_STREAM;
451 ALOGV("signalling eos");
452 }
453 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
454 work->worklets.front()->output.buffers.clear();
455 work->worklets.front()->output.buffers.push_back(buffer);
456 work->worklets.front()->output.ordinal = work->input.ordinal;
457 work->workletsProcessed = 1u;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800458
459 for (const std::unique_ptr<C2Param> &param: work->input.configUpdate) {
460 if (param) {
461 C2StreamHdr10PlusInfo::input *hdr10PlusInfo =
462 C2StreamHdr10PlusInfo::input::From(param.get());
463
464 if (hdr10PlusInfo != nullptr) {
465 std::vector<std::unique_ptr<C2SettingResult>> failures;
466 std::unique_ptr<C2Param> outParam = C2Param::CopyAsStream(
467 *param.get(), true /*output*/, param->stream());
468 c2_status_t err = intf->config(
469 { outParam.get() }, C2_MAY_BLOCK, &failures);
470 if (err == C2_OK) {
471 work->worklets.front()->output.configUpdate.push_back(
472 C2Param::Copy(*outParam.get()));
473 } else {
474 ALOGE("finishWork: Config update size failed");
475 }
476 break;
477 }
478 }
479 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800480 };
481 if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) {
482 fillWork(work);
483 } else {
484 finish(index, fillWork);
485 }
486}
487
488void C2SoftVpxDec::process(
489 const std::unique_ptr<C2Work> &work,
490 const std::shared_ptr<C2BlockPool> &pool) {
491 // Initialize output work
492 work->result = C2_OK;
493 work->workletsProcessed = 0u;
494 work->worklets.front()->output.configUpdate.clear();
495 work->worklets.front()->output.flags = work->input.flags;
496
497 if (mSignalledError || mSignalledOutputEos) {
498 work->result = C2_BAD_VALUE;
499 return;
500 }
501
502 size_t inOffset = 0u;
503 size_t inSize = 0u;
504 C2ReadView rView = mDummyReadView;
505 if (!work->input.buffers.empty()) {
506 rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
507 inSize = rView.capacity();
508 if (inSize && rView.error()) {
509 ALOGE("read view map failed %d", rView.error());
510 work->result = C2_CORRUPTED;
511 return;
512 }
513 }
514
515 bool codecConfig = ((work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) !=0);
516 bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
517
518 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
519 inSize, (int)work->input.ordinal.timestamp.peeku(),
520 (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
521
522 // Software VP9 Decoder does not need the Codec Specific Data (CSD)
523 // (specified in http://www.webmproject.org/vp9/profiles/). Ignore it if
524 // it was passed.
525 if (codecConfig) {
526 // Ignore CSD buffer for VP9.
527 if (mMode == MODE_VP9) {
528 fillEmptyWork(work);
529 return;
530 } else {
531 // Tolerate the CSD buffer for VP8. This is a workaround
532 // for b/28689536. continue
533 ALOGW("WARNING: Got CSD buffer for VP8. Continue");
534 }
535 }
536
537 int64_t frameIndex = work->input.ordinal.frameIndex.peekll();
538
539 if (inSize) {
540 uint8_t *bitstream = const_cast<uint8_t *>(rView.data() + inOffset);
541 vpx_codec_err_t err = vpx_codec_decode(
542 mCodecCtx, bitstream, inSize, &frameIndex, 0);
543 if (err != VPX_CODEC_OK) {
544 ALOGE("on2 decoder failed to decode frame. err: %d", err);
545 mSignalledError = true;
546 work->workletsProcessed = 1u;
547 work->result = C2_CORRUPTED;
548 return;
549 }
550 }
551
552 (void)outputBuffer(pool, work);
553
554 if (eos) {
555 drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work);
556 mSignalledOutputEos = true;
557 } else if (!inSize) {
558 fillEmptyWork(work);
559 }
560}
561
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700562static void copyOutputBufferToYuvPlanarFrame(
563 uint8_t *dst, const uint8_t *srcY, const uint8_t *srcU, const uint8_t *srcV,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800564 size_t srcYStride, size_t srcUStride, size_t srcVStride,
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700565 size_t dstYStride, size_t dstUVStride,
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700566 uint32_t width, uint32_t height) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800567 uint8_t *dstStart = dst;
568
569 for (size_t i = 0; i < height; ++i) {
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700570 memcpy(dst, srcY, width);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800571 srcY += srcYStride;
572 dst += dstYStride;
573 }
574
575 dst = dstStart + dstYStride * height;
576 for (size_t i = 0; i < height / 2; ++i) {
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700577 memcpy(dst, srcV, width / 2);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800578 srcV += srcVStride;
579 dst += dstUVStride;
580 }
581
582 dst = dstStart + (dstYStride * height) + (dstUVStride * height / 2);
583 for (size_t i = 0; i < height / 2; ++i) {
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700584 memcpy(dst, srcU, width / 2);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800585 srcU += srcUStride;
586 dst += dstUVStride;
587 }
588}
589
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700590static void convertYUV420Planar16ToY410(uint32_t *dst,
591 const uint16_t *srcY, const uint16_t *srcU, const uint16_t *srcV,
592 size_t srcYStride, size_t srcUStride, size_t srcVStride,
593 size_t dstStride, size_t width, size_t height) {
594
595 // Converting two lines at a time, slightly faster
596 for (size_t y = 0; y < height; y += 2) {
597 uint32_t *dstTop = (uint32_t *) dst;
598 uint32_t *dstBot = (uint32_t *) (dst + dstStride);
599 uint16_t *ySrcTop = (uint16_t*) srcY;
600 uint16_t *ySrcBot = (uint16_t*) (srcY + srcYStride);
601 uint16_t *uSrc = (uint16_t*) srcU;
602 uint16_t *vSrc = (uint16_t*) srcV;
603
604 uint32_t u01, v01, y01, y23, y45, y67, uv0, uv1;
605 size_t x = 0;
606 for (; x < width - 3; x += 4) {
607
608 u01 = *((uint32_t*)uSrc); uSrc += 2;
609 v01 = *((uint32_t*)vSrc); vSrc += 2;
610
611 y01 = *((uint32_t*)ySrcTop); ySrcTop += 2;
612 y23 = *((uint32_t*)ySrcTop); ySrcTop += 2;
613 y45 = *((uint32_t*)ySrcBot); ySrcBot += 2;
614 y67 = *((uint32_t*)ySrcBot); ySrcBot += 2;
615
616 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
617 uv1 = (u01 >> 16) | ((v01 >> 16) << 20);
618
619 *dstTop++ = 3 << 30 | ((y01 & 0x3FF) << 10) | uv0;
620 *dstTop++ = 3 << 30 | ((y01 >> 16) << 10) | uv0;
621 *dstTop++ = 3 << 30 | ((y23 & 0x3FF) << 10) | uv1;
622 *dstTop++ = 3 << 30 | ((y23 >> 16) << 10) | uv1;
623
624 *dstBot++ = 3 << 30 | ((y45 & 0x3FF) << 10) | uv0;
625 *dstBot++ = 3 << 30 | ((y45 >> 16) << 10) | uv0;
626 *dstBot++ = 3 << 30 | ((y67 & 0x3FF) << 10) | uv1;
627 *dstBot++ = 3 << 30 | ((y67 >> 16) << 10) | uv1;
628 }
629
630 // There should be at most 2 more pixels to process. Note that we don't
631 // need to consider odd case as the buffer is always aligned to even.
632 if (x < width) {
633 u01 = *uSrc;
634 v01 = *vSrc;
635 y01 = *((uint32_t*)ySrcTop);
636 y45 = *((uint32_t*)ySrcBot);
637 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
638 *dstTop++ = ((y01 & 0x3FF) << 10) | uv0;
639 *dstTop++ = ((y01 >> 16) << 10) | uv0;
640 *dstBot++ = ((y45 & 0x3FF) << 10) | uv0;
641 *dstBot++ = ((y45 >> 16) << 10) | uv0;
642 }
643
644 srcY += srcYStride * 2;
645 srcU += srcUStride;
646 srcV += srcVStride;
647 dst += dstStride * 2;
648 }
649
650 return;
651}
652
653static void convertYUV420Planar16ToYUV420Planar(uint8_t *dst,
654 const uint16_t *srcY, const uint16_t *srcU, const uint16_t *srcV,
655 size_t srcYStride, size_t srcUStride, size_t srcVStride,
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700656 size_t dstYStride, size_t dstUVStride, size_t width, size_t height) {
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700657
658 uint8_t *dstY = (uint8_t *)dst;
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700659 size_t dstYSize = dstYStride * height;
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700660 size_t dstUVSize = dstUVStride * height / 2;
661 uint8_t *dstV = dstY + dstYSize;
662 uint8_t *dstU = dstV + dstUVSize;
663
664 for (size_t y = 0; y < height; ++y) {
665 for (size_t x = 0; x < width; ++x) {
666 dstY[x] = (uint8_t)(srcY[x] >> 2);
667 }
668
669 srcY += srcYStride;
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700670 dstY += dstYStride;
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700671 }
672
673 for (size_t y = 0; y < (height + 1) / 2; ++y) {
674 for (size_t x = 0; x < (width + 1) / 2; ++x) {
675 dstU[x] = (uint8_t)(srcU[x] >> 2);
676 dstV[x] = (uint8_t)(srcV[x] >> 2);
677 }
678
679 srcU += srcUStride;
680 srcV += srcVStride;
681 dstU += dstUVStride;
682 dstV += dstUVStride;
683 }
684 return;
685}
Pawin Vongmasa36653902018-11-15 00:10:25 -0800686bool C2SoftVpxDec::outputBuffer(
687 const std::shared_ptr<C2BlockPool> &pool,
688 const std::unique_ptr<C2Work> &work)
689{
690 if (!(work && pool)) return false;
691
692 vpx_codec_iter_t iter = nullptr;
693 vpx_image_t *img = vpx_codec_get_frame(mCodecCtx, &iter);
694
695 if (!img) return false;
696
697 if (img->d_w != mWidth || img->d_h != mHeight) {
698 mWidth = img->d_w;
699 mHeight = img->d_h;
700
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800701 C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800702 std::vector<std::unique_ptr<C2SettingResult>> failures;
703 c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures);
704 if (err == C2_OK) {
705 work->worklets.front()->output.configUpdate.push_back(
706 C2Param::Copy(size));
707 } else {
708 ALOGE("Config update size failed");
709 mSignalledError = true;
710 work->workletsProcessed = 1u;
711 work->result = C2_CORRUPTED;
712 return false;
713 }
714
715 }
716 CHECK(img->fmt == VPX_IMG_FMT_I420 || img->fmt == VPX_IMG_FMT_I42016);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800717
718 std::shared_ptr<C2GraphicBlock> block;
719 uint32_t format = HAL_PIXEL_FORMAT_YV12;
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700720 if (img->fmt == VPX_IMG_FMT_I42016) {
721 IntfImpl::Lock lock = mIntf->lock();
722 std::shared_ptr<C2StreamColorAspectsTuning::output> defaultColorAspects = mIntf->getDefaultColorAspects_l();
723
724 if (defaultColorAspects->primaries == C2Color::PRIMARIES_BT2020 &&
725 defaultColorAspects->matrix == C2Color::MATRIX_BT2020 &&
726 defaultColorAspects->transfer == C2Color::TRANSFER_ST2084) {
727 format = HAL_PIXEL_FORMAT_RGBA_1010102;
728 }
729 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800730 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700731 c2_status_t err = pool->fetchGraphicBlock(align(mWidth, 16), mHeight, format, usage, &block);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800732 if (err != C2_OK) {
733 ALOGE("fetchGraphicBlock for Output failed with status %d", err);
734 work->result = err;
735 return false;
736 }
737
738 C2GraphicView wView = block->map().get();
739 if (wView.error()) {
740 ALOGE("graphic view map failed %d", wView.error());
741 work->result = C2_CORRUPTED;
742 return false;
743 }
744
745 ALOGV("provided (%dx%d) required (%dx%d), out frameindex %d",
746 block->width(), block->height(), mWidth, mHeight, (int)*(int64_t *)img->user_priv);
747
748 uint8_t *dst = const_cast<uint8_t *>(wView.data()[C2PlanarLayout::PLANE_Y]);
749 size_t srcYStride = img->stride[VPX_PLANE_Y];
750 size_t srcUStride = img->stride[VPX_PLANE_U];
751 size_t srcVStride = img->stride[VPX_PLANE_V];
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700752 C2PlanarLayout layout = wView.layout();
753 size_t dstYStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
754 size_t dstUVStride = layout.planes[C2PlanarLayout::PLANE_U].rowInc;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800755
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700756 if (img->fmt == VPX_IMG_FMT_I42016) {
757 const uint16_t *srcY = (const uint16_t *)img->planes[VPX_PLANE_Y];
758 const uint16_t *srcU = (const uint16_t *)img->planes[VPX_PLANE_U];
759 const uint16_t *srcV = (const uint16_t *)img->planes[VPX_PLANE_V];
760
761 if (format == HAL_PIXEL_FORMAT_RGBA_1010102) {
762 convertYUV420Planar16ToY410((uint32_t *)dst, srcY, srcU, srcV, srcYStride / 2,
763 srcUStride / 2, srcVStride / 2,
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700764 dstYStride / sizeof(uint32_t),
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700765 mWidth, mHeight);
766 } else {
767 convertYUV420Planar16ToYUV420Planar(dst, srcY, srcU, srcV, srcYStride / 2,
768 srcUStride / 2, srcVStride / 2,
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700769 dstYStride, dstUVStride,
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700770 mWidth, mHeight);
771 }
772 } else {
773 const uint8_t *srcY = (const uint8_t *)img->planes[VPX_PLANE_Y];
774 const uint8_t *srcU = (const uint8_t *)img->planes[VPX_PLANE_U];
775 const uint8_t *srcV = (const uint8_t *)img->planes[VPX_PLANE_V];
Harish Mahendrakardfa3f5a2019-05-02 12:10:24 -0700776 copyOutputBufferToYuvPlanarFrame(
777 dst, srcY, srcU, srcV,
778 srcYStride, srcUStride, srcVStride,
779 dstYStride, dstUVStride,
780 mWidth, mHeight);
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700781 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800782 finishWork(*(int64_t *)img->user_priv, work, std::move(block));
783 return true;
784}
785
786c2_status_t C2SoftVpxDec::drainInternal(
787 uint32_t drainMode,
788 const std::shared_ptr<C2BlockPool> &pool,
789 const std::unique_ptr<C2Work> &work) {
790 if (drainMode == NO_DRAIN) {
791 ALOGW("drain with NO_DRAIN: no-op");
792 return C2_OK;
793 }
794 if (drainMode == DRAIN_CHAIN) {
795 ALOGW("DRAIN_CHAIN not supported");
796 return C2_OMITTED;
797 }
798
799 while ((outputBuffer(pool, work))) {
800 }
801
802 if (drainMode == DRAIN_COMPONENT_WITH_EOS &&
803 work && work->workletsProcessed == 0u) {
804 fillEmptyWork(work);
805 }
806
807 return C2_OK;
808}
809c2_status_t C2SoftVpxDec::drain(
810 uint32_t drainMode,
811 const std::shared_ptr<C2BlockPool> &pool) {
812 return drainInternal(drainMode, pool, nullptr);
813}
814
815class C2SoftVpxFactory : public C2ComponentFactory {
816public:
817 C2SoftVpxFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
818 GetCodec2PlatformComponentStore()->getParamReflector())) {
819 }
820
821 virtual c2_status_t createComponent(
822 c2_node_id_t id,
823 std::shared_ptr<C2Component>* const component,
824 std::function<void(C2Component*)> deleter) override {
825 *component = std::shared_ptr<C2Component>(
826 new C2SoftVpxDec(COMPONENT_NAME, id,
827 std::make_shared<C2SoftVpxDec::IntfImpl>(mHelper)),
828 deleter);
829 return C2_OK;
830 }
831
832 virtual c2_status_t createInterface(
833 c2_node_id_t id,
834 std::shared_ptr<C2ComponentInterface>* const interface,
835 std::function<void(C2ComponentInterface*)> deleter) override {
836 *interface = std::shared_ptr<C2ComponentInterface>(
837 new SimpleInterface<C2SoftVpxDec::IntfImpl>(
838 COMPONENT_NAME, id,
839 std::make_shared<C2SoftVpxDec::IntfImpl>(mHelper)),
840 deleter);
841 return C2_OK;
842 }
843
844 virtual ~C2SoftVpxFactory() override = default;
845
846private:
847 std::shared_ptr<C2ReflectorHelper> mHelper;
848};
849
850} // namespace android
851
852extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
853 ALOGV("in %s", __func__);
854 return new ::android::C2SoftVpxFactory();
855}
856
857extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
858 ALOGV("in %s", __func__);
859 delete factory;
860}