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