Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | namespace android { |
| 31 | |
| 32 | #ifdef VP9 |
| 33 | constexpr char COMPONENT_NAME[] = "c2.android.vp9.decoder"; |
| 34 | #else |
| 35 | constexpr char COMPONENT_NAME[] = "c2.android.vp8.decoder"; |
| 36 | #endif |
| 37 | |
| 38 | class C2SoftVpxDec::IntfImpl : public SimpleInterface<void>::BaseParams { |
| 39 | public: |
| 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 | |
| 100 | #if 0 |
| 101 | // sample BT.2020 static info |
| 102 | mHdrStaticInfo = std::make_shared<C2StreamHdrStaticInfo::output>(); |
| 103 | mHdrStaticInfo->mastering = { |
| 104 | .red = { .x = 0.708, .y = 0.292 }, |
| 105 | .green = { .x = 0.170, .y = 0.797 }, |
| 106 | .blue = { .x = 0.131, .y = 0.046 }, |
| 107 | .white = { .x = 0.3127, .y = 0.3290 }, |
| 108 | .maxLuminance = 1000, |
| 109 | .minLuminance = 0.1, |
| 110 | }; |
| 111 | mHdrStaticInfo->maxCll = 1000; |
| 112 | mHdrStaticInfo->maxFall = 120; |
| 113 | |
| 114 | mHdrStaticInfo->maxLuminance = 0; // disable static info |
| 115 | |
| 116 | helper->addStructDescriptors<C2MasteringDisplayColorVolumeStruct, C2ColorXyStruct>(); |
| 117 | addParameter( |
| 118 | DefineParam(mHdrStaticInfo, C2_PARAMKEY_HDR_STATIC_INFO) |
| 119 | .withDefault(mHdrStaticInfo) |
| 120 | .withFields({ |
| 121 | C2F(mHdrStaticInfo, mastering.red.x).inRange(0, 1), |
| 122 | // TODO |
| 123 | }) |
| 124 | .withSetter(HdrStaticInfoSetter) |
| 125 | .build()); |
| 126 | #endif |
| 127 | #else |
| 128 | addParameter( |
| 129 | DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL) |
| 130 | .withConstValue(new C2StreamProfileLevelInfo::input(0u, |
| 131 | C2Config::PROFILE_UNUSED, C2Config::LEVEL_UNUSED)) |
| 132 | .build()); |
| 133 | #endif |
| 134 | |
| 135 | addParameter( |
| 136 | DefineParam(mMaxSize, C2_PARAMKEY_MAX_PICTURE_SIZE) |
| 137 | .withDefault(new C2StreamMaxPictureSizeTuning::output(0u, 320, 240)) |
| 138 | .withFields({ |
| 139 | C2F(mSize, width).inRange(2, 2048, 2), |
| 140 | C2F(mSize, height).inRange(2, 2048, 2), |
| 141 | }) |
| 142 | .withSetter(MaxPictureSizeSetter, mSize) |
| 143 | .build()); |
| 144 | |
| 145 | addParameter( |
| 146 | DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE) |
| 147 | .withDefault(new C2StreamMaxBufferSizeInfo::input(0u, 320 * 240 * 3 / 4)) |
| 148 | .withFields({ |
| 149 | C2F(mMaxInputSize, value).any(), |
| 150 | }) |
| 151 | .calculatedAs(MaxInputSizeSetter, mMaxSize) |
| 152 | .build()); |
| 153 | |
| 154 | C2ChromaOffsetStruct locations[1] = { C2ChromaOffsetStruct::ITU_YUV_420_0() }; |
| 155 | std::shared_ptr<C2StreamColorInfo::output> defaultColorInfo = |
| 156 | C2StreamColorInfo::output::AllocShared( |
| 157 | 1u, 0u, 8u /* bitDepth */, C2Color::YUV_420); |
| 158 | memcpy(defaultColorInfo->m.locations, locations, sizeof(locations)); |
| 159 | |
| 160 | defaultColorInfo = |
| 161 | C2StreamColorInfo::output::AllocShared( |
| 162 | { C2ChromaOffsetStruct::ITU_YUV_420_0() }, |
| 163 | 0u, 8u /* bitDepth */, C2Color::YUV_420); |
| 164 | helper->addStructDescriptors<C2ChromaOffsetStruct>(); |
| 165 | |
| 166 | addParameter( |
| 167 | DefineParam(mColorInfo, C2_PARAMKEY_CODED_COLOR_INFO) |
| 168 | .withConstValue(defaultColorInfo) |
| 169 | .build()); |
| 170 | |
| 171 | // TODO: support more formats? |
| 172 | addParameter( |
| 173 | DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT) |
| 174 | .withConstValue(new C2StreamPixelFormatInfo::output( |
| 175 | 0u, HAL_PIXEL_FORMAT_YCBCR_420_888)) |
| 176 | .build()); |
| 177 | } |
| 178 | |
| 179 | static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::output> &oldMe, |
| 180 | C2P<C2VideoSizeStreamInfo::output> &me) { |
| 181 | (void)mayBlock; |
| 182 | C2R res = C2R::Ok(); |
| 183 | if (!me.F(me.v.width).supportsAtAll(me.v.width)) { |
| 184 | res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width))); |
| 185 | me.set().width = oldMe.v.width; |
| 186 | } |
| 187 | if (!me.F(me.v.height).supportsAtAll(me.v.height)) { |
| 188 | res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height))); |
| 189 | me.set().height = oldMe.v.height; |
| 190 | } |
| 191 | return res; |
| 192 | } |
| 193 | |
| 194 | static C2R MaxPictureSizeSetter(bool mayBlock, C2P<C2StreamMaxPictureSizeTuning::output> &me, |
| 195 | const C2P<C2StreamPictureSizeInfo::output> &size) { |
| 196 | (void)mayBlock; |
| 197 | // TODO: get max width/height from the size's field helpers vs. hardcoding |
| 198 | me.set().width = c2_min(c2_max(me.v.width, size.v.width), 2048u); |
| 199 | me.set().height = c2_min(c2_max(me.v.height, size.v.height), 2048u); |
| 200 | return C2R::Ok(); |
| 201 | } |
| 202 | |
| 203 | static C2R MaxInputSizeSetter(bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input> &me, |
| 204 | const C2P<C2StreamMaxPictureSizeTuning::output> &maxSize) { |
| 205 | (void)mayBlock; |
| 206 | // assume compression ratio of 2 |
| 207 | me.set().value = (((maxSize.v.width + 63) / 64) * ((maxSize.v.height + 63) / 64) * 3072); |
| 208 | return C2R::Ok(); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me, |
| 213 | const C2P<C2StreamPictureSizeInfo::output> &size) { |
| 214 | (void)mayBlock; |
| 215 | (void)size; |
| 216 | (void)me; // TODO: validate |
| 217 | return C2R::Ok(); |
| 218 | } |
| 219 | |
| 220 | private: |
| 221 | std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel; |
| 222 | std::shared_ptr<C2StreamPictureSizeInfo::output> mSize; |
| 223 | std::shared_ptr<C2StreamMaxPictureSizeTuning::output> mMaxSize; |
| 224 | std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize; |
| 225 | std::shared_ptr<C2StreamColorInfo::output> mColorInfo; |
| 226 | std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormat; |
| 227 | #ifdef VP9 |
| 228 | #if 0 |
| 229 | std::shared_ptr<C2StreamHdrStaticInfo::output> mHdrStaticInfo; |
| 230 | #endif |
| 231 | #endif |
| 232 | }; |
| 233 | |
| 234 | C2SoftVpxDec::C2SoftVpxDec( |
| 235 | const char *name, |
| 236 | c2_node_id_t id, |
| 237 | const std::shared_ptr<IntfImpl> &intfImpl) |
| 238 | : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)), |
| 239 | mIntf(intfImpl), |
| 240 | mCodecCtx(nullptr) { |
| 241 | } |
| 242 | |
| 243 | C2SoftVpxDec::~C2SoftVpxDec() { |
| 244 | onRelease(); |
| 245 | } |
| 246 | |
| 247 | c2_status_t C2SoftVpxDec::onInit() { |
| 248 | status_t err = initDecoder(); |
| 249 | return err == OK ? C2_OK : C2_CORRUPTED; |
| 250 | } |
| 251 | |
| 252 | c2_status_t C2SoftVpxDec::onStop() { |
| 253 | mSignalledError = false; |
| 254 | mSignalledOutputEos = false; |
| 255 | |
| 256 | return C2_OK; |
| 257 | } |
| 258 | |
| 259 | void C2SoftVpxDec::onReset() { |
| 260 | (void)onStop(); |
| 261 | c2_status_t err = onFlush_sm(); |
| 262 | if (err != C2_OK) |
| 263 | { |
| 264 | ALOGW("Failed to flush decoder. Try to hard reset decoder"); |
| 265 | destroyDecoder(); |
| 266 | (void)initDecoder(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void C2SoftVpxDec::onRelease() { |
| 271 | destroyDecoder(); |
| 272 | } |
| 273 | |
| 274 | c2_status_t C2SoftVpxDec::onFlush_sm() { |
| 275 | if (mFrameParallelMode) { |
| 276 | // Flush decoder by passing nullptr data ptr and 0 size. |
| 277 | // Ideally, this should never fail. |
| 278 | if (vpx_codec_decode(mCodecCtx, nullptr, 0, nullptr, 0)) { |
| 279 | ALOGE("Failed to flush on2 decoder."); |
| 280 | return C2_CORRUPTED; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Drop all the decoded frames in decoder. |
| 285 | vpx_codec_iter_t iter = nullptr; |
| 286 | while (vpx_codec_get_frame(mCodecCtx, &iter)) { |
| 287 | } |
| 288 | |
| 289 | mSignalledError = false; |
| 290 | mSignalledOutputEos = false; |
| 291 | return C2_OK; |
| 292 | } |
| 293 | |
| 294 | static int GetCPUCoreCount() { |
| 295 | int cpuCoreCount = 1; |
| 296 | #if defined(_SC_NPROCESSORS_ONLN) |
| 297 | cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN); |
| 298 | #else |
| 299 | // _SC_NPROC_ONLN must be defined... |
| 300 | cpuCoreCount = sysconf(_SC_NPROC_ONLN); |
| 301 | #endif |
| 302 | CHECK(cpuCoreCount >= 1); |
| 303 | ALOGV("Number of CPU cores: %d", cpuCoreCount); |
| 304 | return cpuCoreCount; |
| 305 | } |
| 306 | |
| 307 | status_t C2SoftVpxDec::initDecoder() { |
| 308 | #ifdef VP9 |
| 309 | mMode = MODE_VP9; |
| 310 | #else |
| 311 | mMode = MODE_VP8; |
| 312 | #endif |
| 313 | |
| 314 | mWidth = 320; |
| 315 | mHeight = 240; |
| 316 | mFrameParallelMode = false; |
| 317 | mSignalledOutputEos = false; |
| 318 | mSignalledError = false; |
| 319 | |
| 320 | if (!mCodecCtx) { |
| 321 | mCodecCtx = new vpx_codec_ctx_t; |
| 322 | } |
| 323 | if (!mCodecCtx) { |
| 324 | ALOGE("mCodecCtx is null"); |
| 325 | return NO_MEMORY; |
| 326 | } |
| 327 | |
| 328 | vpx_codec_dec_cfg_t cfg; |
| 329 | memset(&cfg, 0, sizeof(vpx_codec_dec_cfg_t)); |
| 330 | cfg.threads = GetCPUCoreCount(); |
| 331 | |
| 332 | vpx_codec_flags_t flags; |
| 333 | memset(&flags, 0, sizeof(vpx_codec_flags_t)); |
| 334 | if (mFrameParallelMode) flags |= VPX_CODEC_USE_FRAME_THREADING; |
| 335 | |
| 336 | vpx_codec_err_t vpx_err; |
| 337 | if ((vpx_err = vpx_codec_dec_init( |
| 338 | mCodecCtx, mMode == MODE_VP8 ? &vpx_codec_vp8_dx_algo : &vpx_codec_vp9_dx_algo, |
| 339 | &cfg, flags))) { |
| 340 | ALOGE("on2 decoder failed to initialize. (%d)", vpx_err); |
| 341 | return UNKNOWN_ERROR; |
| 342 | } |
| 343 | |
| 344 | return OK; |
| 345 | } |
| 346 | |
| 347 | status_t C2SoftVpxDec::destroyDecoder() { |
| 348 | if (mCodecCtx) { |
| 349 | vpx_codec_destroy(mCodecCtx); |
| 350 | delete mCodecCtx; |
| 351 | mCodecCtx = nullptr; |
| 352 | } |
| 353 | |
| 354 | return OK; |
| 355 | } |
| 356 | |
| 357 | void fillEmptyWork(const std::unique_ptr<C2Work> &work) { |
| 358 | uint32_t flags = 0; |
| 359 | if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) { |
| 360 | flags |= C2FrameData::FLAG_END_OF_STREAM; |
| 361 | ALOGV("signalling eos"); |
| 362 | } |
| 363 | work->worklets.front()->output.flags = (C2FrameData::flags_t)flags; |
| 364 | work->worklets.front()->output.buffers.clear(); |
| 365 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 366 | work->workletsProcessed = 1u; |
| 367 | } |
| 368 | |
| 369 | void C2SoftVpxDec::finishWork(uint64_t index, const std::unique_ptr<C2Work> &work, |
| 370 | const std::shared_ptr<C2GraphicBlock> &block) { |
| 371 | std::shared_ptr<C2Buffer> buffer = createGraphicBuffer(block, |
| 372 | C2Rect(mWidth, mHeight)); |
| 373 | auto fillWork = [buffer, index](const std::unique_ptr<C2Work> &work) { |
| 374 | uint32_t flags = 0; |
| 375 | if ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) && |
| 376 | (c2_cntr64_t(index) == work->input.ordinal.frameIndex)) { |
| 377 | flags |= C2FrameData::FLAG_END_OF_STREAM; |
| 378 | ALOGV("signalling eos"); |
| 379 | } |
| 380 | work->worklets.front()->output.flags = (C2FrameData::flags_t)flags; |
| 381 | work->worklets.front()->output.buffers.clear(); |
| 382 | work->worklets.front()->output.buffers.push_back(buffer); |
| 383 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 384 | work->workletsProcessed = 1u; |
| 385 | }; |
| 386 | if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) { |
| 387 | fillWork(work); |
| 388 | } else { |
| 389 | finish(index, fillWork); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | void C2SoftVpxDec::process( |
| 394 | const std::unique_ptr<C2Work> &work, |
| 395 | const std::shared_ptr<C2BlockPool> &pool) { |
| 396 | // Initialize output work |
| 397 | work->result = C2_OK; |
| 398 | work->workletsProcessed = 0u; |
| 399 | work->worklets.front()->output.configUpdate.clear(); |
| 400 | work->worklets.front()->output.flags = work->input.flags; |
| 401 | |
| 402 | if (mSignalledError || mSignalledOutputEos) { |
| 403 | work->result = C2_BAD_VALUE; |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | size_t inOffset = 0u; |
| 408 | size_t inSize = 0u; |
| 409 | C2ReadView rView = mDummyReadView; |
| 410 | if (!work->input.buffers.empty()) { |
| 411 | rView = work->input.buffers[0]->data().linearBlocks().front().map().get(); |
| 412 | inSize = rView.capacity(); |
| 413 | if (inSize && rView.error()) { |
| 414 | ALOGE("read view map failed %d", rView.error()); |
| 415 | work->result = C2_CORRUPTED; |
| 416 | return; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | bool codecConfig = ((work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) !=0); |
| 421 | bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0); |
| 422 | |
| 423 | ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x", |
| 424 | inSize, (int)work->input.ordinal.timestamp.peeku(), |
| 425 | (int)work->input.ordinal.frameIndex.peeku(), work->input.flags); |
| 426 | |
| 427 | // Software VP9 Decoder does not need the Codec Specific Data (CSD) |
| 428 | // (specified in http://www.webmproject.org/vp9/profiles/). Ignore it if |
| 429 | // it was passed. |
| 430 | if (codecConfig) { |
| 431 | // Ignore CSD buffer for VP9. |
| 432 | if (mMode == MODE_VP9) { |
| 433 | fillEmptyWork(work); |
| 434 | return; |
| 435 | } else { |
| 436 | // Tolerate the CSD buffer for VP8. This is a workaround |
| 437 | // for b/28689536. continue |
| 438 | ALOGW("WARNING: Got CSD buffer for VP8. Continue"); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | int64_t frameIndex = work->input.ordinal.frameIndex.peekll(); |
| 443 | |
| 444 | if (inSize) { |
| 445 | uint8_t *bitstream = const_cast<uint8_t *>(rView.data() + inOffset); |
| 446 | vpx_codec_err_t err = vpx_codec_decode( |
| 447 | mCodecCtx, bitstream, inSize, &frameIndex, 0); |
| 448 | if (err != VPX_CODEC_OK) { |
| 449 | ALOGE("on2 decoder failed to decode frame. err: %d", err); |
| 450 | mSignalledError = true; |
| 451 | work->workletsProcessed = 1u; |
| 452 | work->result = C2_CORRUPTED; |
| 453 | return; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | (void)outputBuffer(pool, work); |
| 458 | |
| 459 | if (eos) { |
| 460 | drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work); |
| 461 | mSignalledOutputEos = true; |
| 462 | } else if (!inSize) { |
| 463 | fillEmptyWork(work); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | static void copyOutputBufferToYV12Frame(uint8_t *dst, |
| 468 | const uint8_t *srcY, const uint8_t *srcU, const uint8_t *srcV, |
| 469 | size_t srcYStride, size_t srcUStride, size_t srcVStride, |
| 470 | uint32_t width, uint32_t height, int32_t bpp) { |
| 471 | size_t dstYStride = align(width, 16) * bpp ; |
| 472 | size_t dstUVStride = align(dstYStride / 2, 16); |
| 473 | uint8_t *dstStart = dst; |
| 474 | |
| 475 | for (size_t i = 0; i < height; ++i) { |
| 476 | memcpy(dst, srcY, width * bpp); |
| 477 | srcY += srcYStride; |
| 478 | dst += dstYStride; |
| 479 | } |
| 480 | |
| 481 | dst = dstStart + dstYStride * height; |
| 482 | for (size_t i = 0; i < height / 2; ++i) { |
| 483 | memcpy(dst, srcV, width / 2 * bpp); |
| 484 | srcV += srcVStride; |
| 485 | dst += dstUVStride; |
| 486 | } |
| 487 | |
| 488 | dst = dstStart + (dstYStride * height) + (dstUVStride * height / 2); |
| 489 | for (size_t i = 0; i < height / 2; ++i) { |
| 490 | memcpy(dst, srcU, width / 2 * bpp); |
| 491 | srcU += srcUStride; |
| 492 | dst += dstUVStride; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | bool C2SoftVpxDec::outputBuffer( |
| 497 | const std::shared_ptr<C2BlockPool> &pool, |
| 498 | const std::unique_ptr<C2Work> &work) |
| 499 | { |
| 500 | if (!(work && pool)) return false; |
| 501 | |
| 502 | vpx_codec_iter_t iter = nullptr; |
| 503 | vpx_image_t *img = vpx_codec_get_frame(mCodecCtx, &iter); |
| 504 | |
| 505 | if (!img) return false; |
| 506 | |
| 507 | if (img->d_w != mWidth || img->d_h != mHeight) { |
| 508 | mWidth = img->d_w; |
| 509 | mHeight = img->d_h; |
| 510 | |
| 511 | C2VideoSizeStreamInfo::output size(0u, mWidth, mHeight); |
| 512 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 513 | c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures); |
| 514 | if (err == C2_OK) { |
| 515 | work->worklets.front()->output.configUpdate.push_back( |
| 516 | C2Param::Copy(size)); |
| 517 | } else { |
| 518 | ALOGE("Config update size failed"); |
| 519 | mSignalledError = true; |
| 520 | work->workletsProcessed = 1u; |
| 521 | work->result = C2_CORRUPTED; |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | } |
| 526 | CHECK(img->fmt == VPX_IMG_FMT_I420 || img->fmt == VPX_IMG_FMT_I42016); |
| 527 | int32_t bpp = 1; |
| 528 | if (img->fmt == VPX_IMG_FMT_I42016) { |
| 529 | bpp = 2; |
| 530 | } |
| 531 | |
| 532 | std::shared_ptr<C2GraphicBlock> block; |
| 533 | uint32_t format = HAL_PIXEL_FORMAT_YV12; |
| 534 | C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }; |
| 535 | c2_status_t err = pool->fetchGraphicBlock(align(mWidth, 16) * bpp, mHeight, format, usage, &block); |
| 536 | if (err != C2_OK) { |
| 537 | ALOGE("fetchGraphicBlock for Output failed with status %d", err); |
| 538 | work->result = err; |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | C2GraphicView wView = block->map().get(); |
| 543 | if (wView.error()) { |
| 544 | ALOGE("graphic view map failed %d", wView.error()); |
| 545 | work->result = C2_CORRUPTED; |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | ALOGV("provided (%dx%d) required (%dx%d), out frameindex %d", |
| 550 | block->width(), block->height(), mWidth, mHeight, (int)*(int64_t *)img->user_priv); |
| 551 | |
| 552 | uint8_t *dst = const_cast<uint8_t *>(wView.data()[C2PlanarLayout::PLANE_Y]); |
| 553 | size_t srcYStride = img->stride[VPX_PLANE_Y]; |
| 554 | size_t srcUStride = img->stride[VPX_PLANE_U]; |
| 555 | size_t srcVStride = img->stride[VPX_PLANE_V]; |
| 556 | const uint8_t *srcY = (const uint8_t *)img->planes[VPX_PLANE_Y]; |
| 557 | const uint8_t *srcU = (const uint8_t *)img->planes[VPX_PLANE_U]; |
| 558 | const uint8_t *srcV = (const uint8_t *)img->planes[VPX_PLANE_V]; |
| 559 | copyOutputBufferToYV12Frame(dst, srcY, srcU, srcV, |
| 560 | srcYStride, srcUStride, srcVStride, mWidth, mHeight, bpp); |
| 561 | |
| 562 | finishWork(*(int64_t *)img->user_priv, work, std::move(block)); |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | c2_status_t C2SoftVpxDec::drainInternal( |
| 567 | uint32_t drainMode, |
| 568 | const std::shared_ptr<C2BlockPool> &pool, |
| 569 | const std::unique_ptr<C2Work> &work) { |
| 570 | if (drainMode == NO_DRAIN) { |
| 571 | ALOGW("drain with NO_DRAIN: no-op"); |
| 572 | return C2_OK; |
| 573 | } |
| 574 | if (drainMode == DRAIN_CHAIN) { |
| 575 | ALOGW("DRAIN_CHAIN not supported"); |
| 576 | return C2_OMITTED; |
| 577 | } |
| 578 | |
| 579 | while ((outputBuffer(pool, work))) { |
| 580 | } |
| 581 | |
| 582 | if (drainMode == DRAIN_COMPONENT_WITH_EOS && |
| 583 | work && work->workletsProcessed == 0u) { |
| 584 | fillEmptyWork(work); |
| 585 | } |
| 586 | |
| 587 | return C2_OK; |
| 588 | } |
| 589 | c2_status_t C2SoftVpxDec::drain( |
| 590 | uint32_t drainMode, |
| 591 | const std::shared_ptr<C2BlockPool> &pool) { |
| 592 | return drainInternal(drainMode, pool, nullptr); |
| 593 | } |
| 594 | |
| 595 | class C2SoftVpxFactory : public C2ComponentFactory { |
| 596 | public: |
| 597 | C2SoftVpxFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>( |
| 598 | GetCodec2PlatformComponentStore()->getParamReflector())) { |
| 599 | } |
| 600 | |
| 601 | virtual c2_status_t createComponent( |
| 602 | c2_node_id_t id, |
| 603 | std::shared_ptr<C2Component>* const component, |
| 604 | std::function<void(C2Component*)> deleter) override { |
| 605 | *component = std::shared_ptr<C2Component>( |
| 606 | new C2SoftVpxDec(COMPONENT_NAME, id, |
| 607 | std::make_shared<C2SoftVpxDec::IntfImpl>(mHelper)), |
| 608 | deleter); |
| 609 | return C2_OK; |
| 610 | } |
| 611 | |
| 612 | virtual c2_status_t createInterface( |
| 613 | c2_node_id_t id, |
| 614 | std::shared_ptr<C2ComponentInterface>* const interface, |
| 615 | std::function<void(C2ComponentInterface*)> deleter) override { |
| 616 | *interface = std::shared_ptr<C2ComponentInterface>( |
| 617 | new SimpleInterface<C2SoftVpxDec::IntfImpl>( |
| 618 | COMPONENT_NAME, id, |
| 619 | std::make_shared<C2SoftVpxDec::IntfImpl>(mHelper)), |
| 620 | deleter); |
| 621 | return C2_OK; |
| 622 | } |
| 623 | |
| 624 | virtual ~C2SoftVpxFactory() override = default; |
| 625 | |
| 626 | private: |
| 627 | std::shared_ptr<C2ReflectorHelper> mHelper; |
| 628 | }; |
| 629 | |
| 630 | } // namespace android |
| 631 | |
| 632 | extern "C" ::C2ComponentFactory* CreateCodec2Factory() { |
| 633 | ALOGV("in %s", __func__); |
| 634 | return new ::android::C2SoftVpxFactory(); |
| 635 | } |
| 636 | |
| 637 | extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) { |
| 638 | ALOGV("in %s", __func__); |
| 639 | delete factory; |
| 640 | } |