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