blob: 9ba23624e1c41fc4768389e4f9d62562be698ec6 [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,
218 C2P<C2VideoSizeStreamInfo::output> &me) {
219 (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
562static void copyOutputBufferToYV12Frame(uint8_t *dst,
563 const uint8_t *srcY, const uint8_t *srcU, const uint8_t *srcV,
564 size_t srcYStride, size_t srcUStride, size_t srcVStride,
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700565 uint32_t width, uint32_t height) {
566 size_t dstYStride = align(width, 16);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800567 size_t dstUVStride = align(dstYStride / 2, 16);
568 uint8_t *dstStart = dst;
569
570 for (size_t i = 0; i < height; ++i) {
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700571 memcpy(dst, srcY, width);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800572 srcY += srcYStride;
573 dst += dstYStride;
574 }
575
576 dst = dstStart + dstYStride * height;
577 for (size_t i = 0; i < height / 2; ++i) {
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700578 memcpy(dst, srcV, width / 2);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800579 srcV += srcVStride;
580 dst += dstUVStride;
581 }
582
583 dst = dstStart + (dstYStride * height) + (dstUVStride * height / 2);
584 for (size_t i = 0; i < height / 2; ++i) {
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700585 memcpy(dst, srcU, width / 2);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800586 srcU += srcUStride;
587 dst += dstUVStride;
588 }
589}
590
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700591static void convertYUV420Planar16ToY410(uint32_t *dst,
592 const uint16_t *srcY, const uint16_t *srcU, const uint16_t *srcV,
593 size_t srcYStride, size_t srcUStride, size_t srcVStride,
594 size_t dstStride, size_t width, size_t height) {
595
596 // Converting two lines at a time, slightly faster
597 for (size_t y = 0; y < height; y += 2) {
598 uint32_t *dstTop = (uint32_t *) dst;
599 uint32_t *dstBot = (uint32_t *) (dst + dstStride);
600 uint16_t *ySrcTop = (uint16_t*) srcY;
601 uint16_t *ySrcBot = (uint16_t*) (srcY + srcYStride);
602 uint16_t *uSrc = (uint16_t*) srcU;
603 uint16_t *vSrc = (uint16_t*) srcV;
604
605 uint32_t u01, v01, y01, y23, y45, y67, uv0, uv1;
606 size_t x = 0;
607 for (; x < width - 3; x += 4) {
608
609 u01 = *((uint32_t*)uSrc); uSrc += 2;
610 v01 = *((uint32_t*)vSrc); vSrc += 2;
611
612 y01 = *((uint32_t*)ySrcTop); ySrcTop += 2;
613 y23 = *((uint32_t*)ySrcTop); ySrcTop += 2;
614 y45 = *((uint32_t*)ySrcBot); ySrcBot += 2;
615 y67 = *((uint32_t*)ySrcBot); ySrcBot += 2;
616
617 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
618 uv1 = (u01 >> 16) | ((v01 >> 16) << 20);
619
620 *dstTop++ = 3 << 30 | ((y01 & 0x3FF) << 10) | uv0;
621 *dstTop++ = 3 << 30 | ((y01 >> 16) << 10) | uv0;
622 *dstTop++ = 3 << 30 | ((y23 & 0x3FF) << 10) | uv1;
623 *dstTop++ = 3 << 30 | ((y23 >> 16) << 10) | uv1;
624
625 *dstBot++ = 3 << 30 | ((y45 & 0x3FF) << 10) | uv0;
626 *dstBot++ = 3 << 30 | ((y45 >> 16) << 10) | uv0;
627 *dstBot++ = 3 << 30 | ((y67 & 0x3FF) << 10) | uv1;
628 *dstBot++ = 3 << 30 | ((y67 >> 16) << 10) | uv1;
629 }
630
631 // There should be at most 2 more pixels to process. Note that we don't
632 // need to consider odd case as the buffer is always aligned to even.
633 if (x < width) {
634 u01 = *uSrc;
635 v01 = *vSrc;
636 y01 = *((uint32_t*)ySrcTop);
637 y45 = *((uint32_t*)ySrcBot);
638 uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
639 *dstTop++ = ((y01 & 0x3FF) << 10) | uv0;
640 *dstTop++ = ((y01 >> 16) << 10) | uv0;
641 *dstBot++ = ((y45 & 0x3FF) << 10) | uv0;
642 *dstBot++ = ((y45 >> 16) << 10) | uv0;
643 }
644
645 srcY += srcYStride * 2;
646 srcU += srcUStride;
647 srcV += srcVStride;
648 dst += dstStride * 2;
649 }
650
651 return;
652}
653
654static void convertYUV420Planar16ToYUV420Planar(uint8_t *dst,
655 const uint16_t *srcY, const uint16_t *srcU, const uint16_t *srcV,
656 size_t srcYStride, size_t srcUStride, size_t srcVStride,
657 size_t dstStride, size_t width, size_t height) {
658
659 uint8_t *dstY = (uint8_t *)dst;
660 size_t dstYSize = dstStride * height;
661 size_t dstUVStride = align(dstStride / 2, 16);
662 size_t dstUVSize = dstUVStride * height / 2;
663 uint8_t *dstV = dstY + dstYSize;
664 uint8_t *dstU = dstV + dstUVSize;
665
666 for (size_t y = 0; y < height; ++y) {
667 for (size_t x = 0; x < width; ++x) {
668 dstY[x] = (uint8_t)(srcY[x] >> 2);
669 }
670
671 srcY += srcYStride;
672 dstY += dstStride;
673 }
674
675 for (size_t y = 0; y < (height + 1) / 2; ++y) {
676 for (size_t x = 0; x < (width + 1) / 2; ++x) {
677 dstU[x] = (uint8_t)(srcU[x] >> 2);
678 dstV[x] = (uint8_t)(srcV[x] >> 2);
679 }
680
681 srcU += srcUStride;
682 srcV += srcVStride;
683 dstU += dstUVStride;
684 dstV += dstUVStride;
685 }
686 return;
687}
Pawin Vongmasa36653902018-11-15 00:10:25 -0800688bool C2SoftVpxDec::outputBuffer(
689 const std::shared_ptr<C2BlockPool> &pool,
690 const std::unique_ptr<C2Work> &work)
691{
692 if (!(work && pool)) return false;
693
694 vpx_codec_iter_t iter = nullptr;
695 vpx_image_t *img = vpx_codec_get_frame(mCodecCtx, &iter);
696
697 if (!img) return false;
698
699 if (img->d_w != mWidth || img->d_h != mHeight) {
700 mWidth = img->d_w;
701 mHeight = img->d_h;
702
703 C2VideoSizeStreamInfo::output size(0u, mWidth, mHeight);
704 std::vector<std::unique_ptr<C2SettingResult>> failures;
705 c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures);
706 if (err == C2_OK) {
707 work->worklets.front()->output.configUpdate.push_back(
708 C2Param::Copy(size));
709 } else {
710 ALOGE("Config update size failed");
711 mSignalledError = true;
712 work->workletsProcessed = 1u;
713 work->result = C2_CORRUPTED;
714 return false;
715 }
716
717 }
718 CHECK(img->fmt == VPX_IMG_FMT_I420 || img->fmt == VPX_IMG_FMT_I42016);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800719
720 std::shared_ptr<C2GraphicBlock> block;
721 uint32_t format = HAL_PIXEL_FORMAT_YV12;
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700722 if (img->fmt == VPX_IMG_FMT_I42016) {
723 IntfImpl::Lock lock = mIntf->lock();
724 std::shared_ptr<C2StreamColorAspectsTuning::output> defaultColorAspects = mIntf->getDefaultColorAspects_l();
725
726 if (defaultColorAspects->primaries == C2Color::PRIMARIES_BT2020 &&
727 defaultColorAspects->matrix == C2Color::MATRIX_BT2020 &&
728 defaultColorAspects->transfer == C2Color::TRANSFER_ST2084) {
729 format = HAL_PIXEL_FORMAT_RGBA_1010102;
730 }
731 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800732 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700733 c2_status_t err = pool->fetchGraphicBlock(align(mWidth, 16), mHeight, format, usage, &block);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800734 if (err != C2_OK) {
735 ALOGE("fetchGraphicBlock for Output failed with status %d", err);
736 work->result = err;
737 return false;
738 }
739
740 C2GraphicView wView = block->map().get();
741 if (wView.error()) {
742 ALOGE("graphic view map failed %d", wView.error());
743 work->result = C2_CORRUPTED;
744 return false;
745 }
746
747 ALOGV("provided (%dx%d) required (%dx%d), out frameindex %d",
748 block->width(), block->height(), mWidth, mHeight, (int)*(int64_t *)img->user_priv);
749
750 uint8_t *dst = const_cast<uint8_t *>(wView.data()[C2PlanarLayout::PLANE_Y]);
751 size_t srcYStride = img->stride[VPX_PLANE_Y];
752 size_t srcUStride = img->stride[VPX_PLANE_U];
753 size_t srcVStride = img->stride[VPX_PLANE_V];
Pawin Vongmasa36653902018-11-15 00:10:25 -0800754
Harish Mahendrakared4e0cd2018-10-11 18:11:49 -0700755 if (img->fmt == VPX_IMG_FMT_I42016) {
756 const uint16_t *srcY = (const uint16_t *)img->planes[VPX_PLANE_Y];
757 const uint16_t *srcU = (const uint16_t *)img->planes[VPX_PLANE_U];
758 const uint16_t *srcV = (const uint16_t *)img->planes[VPX_PLANE_V];
759
760 if (format == HAL_PIXEL_FORMAT_RGBA_1010102) {
761 convertYUV420Planar16ToY410((uint32_t *)dst, srcY, srcU, srcV, srcYStride / 2,
762 srcUStride / 2, srcVStride / 2,
763 align(mWidth, 16),
764 mWidth, mHeight);
765 } else {
766 convertYUV420Planar16ToYUV420Planar(dst, srcY, srcU, srcV, srcYStride / 2,
767 srcUStride / 2, srcVStride / 2,
768 align(mWidth, 16),
769 mWidth, mHeight);
770 }
771 } else {
772 const uint8_t *srcY = (const uint8_t *)img->planes[VPX_PLANE_Y];
773 const uint8_t *srcU = (const uint8_t *)img->planes[VPX_PLANE_U];
774 const uint8_t *srcV = (const uint8_t *)img->planes[VPX_PLANE_V];
775 copyOutputBufferToYV12Frame(dst, srcY, srcU, srcV,
776 srcYStride, srcUStride, srcVStride, mWidth, mHeight);
777 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800778 finishWork(*(int64_t *)img->user_priv, work, std::move(block));
779 return true;
780}
781
782c2_status_t C2SoftVpxDec::drainInternal(
783 uint32_t drainMode,
784 const std::shared_ptr<C2BlockPool> &pool,
785 const std::unique_ptr<C2Work> &work) {
786 if (drainMode == NO_DRAIN) {
787 ALOGW("drain with NO_DRAIN: no-op");
788 return C2_OK;
789 }
790 if (drainMode == DRAIN_CHAIN) {
791 ALOGW("DRAIN_CHAIN not supported");
792 return C2_OMITTED;
793 }
794
795 while ((outputBuffer(pool, work))) {
796 }
797
798 if (drainMode == DRAIN_COMPONENT_WITH_EOS &&
799 work && work->workletsProcessed == 0u) {
800 fillEmptyWork(work);
801 }
802
803 return C2_OK;
804}
805c2_status_t C2SoftVpxDec::drain(
806 uint32_t drainMode,
807 const std::shared_ptr<C2BlockPool> &pool) {
808 return drainInternal(drainMode, pool, nullptr);
809}
810
811class C2SoftVpxFactory : public C2ComponentFactory {
812public:
813 C2SoftVpxFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
814 GetCodec2PlatformComponentStore()->getParamReflector())) {
815 }
816
817 virtual c2_status_t createComponent(
818 c2_node_id_t id,
819 std::shared_ptr<C2Component>* const component,
820 std::function<void(C2Component*)> deleter) override {
821 *component = std::shared_ptr<C2Component>(
822 new C2SoftVpxDec(COMPONENT_NAME, id,
823 std::make_shared<C2SoftVpxDec::IntfImpl>(mHelper)),
824 deleter);
825 return C2_OK;
826 }
827
828 virtual c2_status_t createInterface(
829 c2_node_id_t id,
830 std::shared_ptr<C2ComponentInterface>* const interface,
831 std::function<void(C2ComponentInterface*)> deleter) override {
832 *interface = std::shared_ptr<C2ComponentInterface>(
833 new SimpleInterface<C2SoftVpxDec::IntfImpl>(
834 COMPONENT_NAME, id,
835 std::make_shared<C2SoftVpxDec::IntfImpl>(mHelper)),
836 deleter);
837 return C2_OK;
838 }
839
840 virtual ~C2SoftVpxFactory() override = default;
841
842private:
843 std::shared_ptr<C2ReflectorHelper> mHelper;
844};
845
846} // namespace android
847
848extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
849 ALOGV("in %s", __func__);
850 return new ::android::C2SoftVpxFactory();
851}
852
853extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
854 ALOGV("in %s", __func__);
855 delete factory;
856}