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 "C2SoftMpeg2Dec" |
| 19 | #include <log/log.h> |
| 20 | |
| 21 | #include <media/stagefright/foundation/MediaDefs.h> |
| 22 | |
| 23 | #include <C2Debug.h> |
| 24 | #include <C2PlatformSupport.h> |
| 25 | #include <Codec2Mapper.h> |
| 26 | #include <SimpleC2Interface.h> |
| 27 | |
| 28 | #include "C2SoftMpeg2Dec.h" |
| 29 | #include "impeg2d.h" |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | constexpr char COMPONENT_NAME[] = "c2.android.mpeg2.decoder"; |
| 34 | |
| 35 | class C2SoftMpeg2Dec::IntfImpl : public SimpleInterface<void>::BaseParams { |
| 36 | public: |
| 37 | explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper) |
| 38 | : SimpleInterface<void>::BaseParams( |
| 39 | helper, |
| 40 | COMPONENT_NAME, |
| 41 | C2Component::KIND_DECODER, |
| 42 | C2Component::DOMAIN_VIDEO, |
| 43 | MEDIA_MIMETYPE_VIDEO_MPEG2) { |
| 44 | noPrivateBuffers(); // TODO: account for our buffers here |
| 45 | noInputReferences(); |
| 46 | noOutputReferences(); |
| 47 | noInputLatency(); |
| 48 | noTimeStretch(); |
| 49 | |
| 50 | // TODO: output latency and reordering |
| 51 | |
| 52 | addParameter( |
| 53 | DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES) |
| 54 | .withConstValue(new C2ComponentAttributesSetting(C2Component::ATTRIB_IS_TEMPORAL)) |
| 55 | .build()); |
| 56 | |
| 57 | addParameter( |
| 58 | DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE) |
| 59 | .withDefault(new C2StreamPictureSizeInfo::output(0u, 320, 240)) |
| 60 | .withFields({ |
| 61 | C2F(mSize, width).inRange(16, 1920, 4), |
| 62 | C2F(mSize, height).inRange(16, 1088, 4), |
| 63 | }) |
| 64 | .withSetter(SizeSetter) |
| 65 | .build()); |
| 66 | |
| 67 | addParameter( |
| 68 | DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL) |
| 69 | .withDefault(new C2StreamProfileLevelInfo::input(0u, |
| 70 | C2Config::PROFILE_MP2V_SIMPLE, C2Config::LEVEL_MP2V_HIGH)) |
| 71 | .withFields({ |
| 72 | C2F(mProfileLevel, profile).oneOf({ |
| 73 | C2Config::PROFILE_MP2V_SIMPLE, |
| 74 | C2Config::PROFILE_MP2V_MAIN}), |
| 75 | C2F(mProfileLevel, level).oneOf({ |
| 76 | C2Config::LEVEL_MP2V_LOW, |
| 77 | C2Config::LEVEL_MP2V_MAIN, |
| 78 | C2Config::LEVEL_MP2V_HIGH_1440, |
| 79 | C2Config::LEVEL_MP2V_HIGH}) |
| 80 | }) |
| 81 | .withSetter(ProfileLevelSetter, mSize) |
| 82 | .build()); |
| 83 | |
| 84 | addParameter( |
| 85 | DefineParam(mMaxSize, C2_PARAMKEY_MAX_PICTURE_SIZE) |
| 86 | .withDefault(new C2StreamMaxPictureSizeTuning::output(0u, 320, 240)) |
| 87 | .withFields({ |
| 88 | C2F(mSize, width).inRange(2, 1920, 2), |
| 89 | C2F(mSize, height).inRange(2, 1088, 2), |
| 90 | }) |
| 91 | .withSetter(MaxPictureSizeSetter, mSize) |
| 92 | .build()); |
| 93 | |
| 94 | addParameter( |
| 95 | DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE) |
| 96 | .withDefault(new C2StreamMaxBufferSizeInfo::input(0u, 320 * 240 * 3 / 2)) |
| 97 | .withFields({ |
| 98 | C2F(mMaxInputSize, value).any(), |
| 99 | }) |
| 100 | .calculatedAs(MaxInputSizeSetter, mMaxSize) |
| 101 | .build()); |
| 102 | |
| 103 | C2ChromaOffsetStruct locations[1] = { C2ChromaOffsetStruct::ITU_YUV_420_0() }; |
| 104 | std::shared_ptr<C2StreamColorInfo::output> defaultColorInfo = |
| 105 | C2StreamColorInfo::output::AllocShared( |
| 106 | 1u, 0u, 8u /* bitDepth */, C2Color::YUV_420); |
| 107 | memcpy(defaultColorInfo->m.locations, locations, sizeof(locations)); |
| 108 | |
| 109 | defaultColorInfo = |
| 110 | C2StreamColorInfo::output::AllocShared( |
| 111 | { C2ChromaOffsetStruct::ITU_YUV_420_0() }, |
| 112 | 0u, 8u /* bitDepth */, C2Color::YUV_420); |
| 113 | helper->addStructDescriptors<C2ChromaOffsetStruct>(); |
| 114 | |
| 115 | addParameter( |
| 116 | DefineParam(mColorInfo, C2_PARAMKEY_CODED_COLOR_INFO) |
| 117 | .withConstValue(defaultColorInfo) |
| 118 | .build()); |
| 119 | |
| 120 | addParameter( |
| 121 | DefineParam(mDefaultColorAspects, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS) |
| 122 | .withDefault(new C2StreamColorAspectsTuning::output( |
| 123 | 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED, |
| 124 | C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED)) |
| 125 | .withFields({ |
| 126 | C2F(mDefaultColorAspects, range).inRange( |
| 127 | C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER), |
| 128 | C2F(mDefaultColorAspects, primaries).inRange( |
| 129 | C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER), |
| 130 | C2F(mDefaultColorAspects, transfer).inRange( |
| 131 | C2Color::TRANSFER_UNSPECIFIED, C2Color::TRANSFER_OTHER), |
| 132 | C2F(mDefaultColorAspects, matrix).inRange( |
| 133 | C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER) |
| 134 | }) |
| 135 | .withSetter(DefaultColorAspectsSetter) |
| 136 | .build()); |
| 137 | |
| 138 | addParameter( |
| 139 | DefineParam(mCodedColorAspects, C2_PARAMKEY_VUI_COLOR_ASPECTS) |
| 140 | .withDefault(new C2StreamColorAspectsInfo::input( |
| 141 | 0u, C2Color::RANGE_LIMITED, C2Color::PRIMARIES_UNSPECIFIED, |
| 142 | C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED)) |
| 143 | .withFields({ |
| 144 | C2F(mCodedColorAspects, range).inRange( |
| 145 | C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER), |
| 146 | C2F(mCodedColorAspects, primaries).inRange( |
| 147 | C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER), |
| 148 | C2F(mCodedColorAspects, transfer).inRange( |
| 149 | C2Color::TRANSFER_UNSPECIFIED, C2Color::TRANSFER_OTHER), |
| 150 | C2F(mCodedColorAspects, matrix).inRange( |
| 151 | C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER) |
| 152 | }) |
| 153 | .withSetter(CodedColorAspectsSetter) |
| 154 | .build()); |
| 155 | |
| 156 | addParameter( |
| 157 | DefineParam(mColorAspects, C2_PARAMKEY_COLOR_ASPECTS) |
| 158 | .withDefault(new C2StreamColorAspectsInfo::output( |
| 159 | 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED, |
| 160 | C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED)) |
| 161 | .withFields({ |
| 162 | C2F(mColorAspects, range).inRange( |
| 163 | C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER), |
| 164 | C2F(mColorAspects, primaries).inRange( |
| 165 | C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER), |
| 166 | C2F(mColorAspects, transfer).inRange( |
| 167 | C2Color::TRANSFER_UNSPECIFIED, C2Color::TRANSFER_OTHER), |
| 168 | C2F(mColorAspects, matrix).inRange( |
| 169 | C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER) |
| 170 | }) |
| 171 | .withSetter(ColorAspectsSetter, mDefaultColorAspects, mCodedColorAspects) |
| 172 | .build()); |
| 173 | |
| 174 | // TODO: support more formats? |
| 175 | addParameter( |
| 176 | DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT) |
| 177 | .withConstValue(new C2StreamPixelFormatInfo::output( |
| 178 | 0u, HAL_PIXEL_FORMAT_YCBCR_420_888)) |
| 179 | .build()); |
| 180 | } |
| 181 | |
| 182 | static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::output> &oldMe, |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 183 | C2P<C2StreamPictureSizeInfo::output> &me) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 184 | (void)mayBlock; |
| 185 | C2R res = C2R::Ok(); |
| 186 | if (!me.F(me.v.width).supportsAtAll(me.v.width)) { |
| 187 | res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width))); |
| 188 | me.set().width = oldMe.v.width; |
| 189 | } |
| 190 | if (!me.F(me.v.height).supportsAtAll(me.v.height)) { |
| 191 | res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height))); |
| 192 | me.set().height = oldMe.v.height; |
| 193 | } |
| 194 | return res; |
| 195 | } |
| 196 | |
| 197 | static C2R MaxPictureSizeSetter(bool mayBlock, C2P<C2StreamMaxPictureSizeTuning::output> &me, |
| 198 | const C2P<C2StreamPictureSizeInfo::output> &size) { |
| 199 | (void)mayBlock; |
| 200 | // TODO: get max width/height from the size's field helpers vs. hardcoding |
| 201 | me.set().width = c2_min(c2_max(me.v.width, size.v.width), 1920u); |
| 202 | me.set().height = c2_min(c2_max(me.v.height, size.v.height), 1088u); |
| 203 | return C2R::Ok(); |
| 204 | } |
| 205 | |
| 206 | static C2R MaxInputSizeSetter(bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input> &me, |
| 207 | const C2P<C2StreamMaxPictureSizeTuning::output> &maxSize) { |
| 208 | (void)mayBlock; |
| 209 | // assume compression ratio of 1 |
| 210 | me.set().value = (((maxSize.v.width + 15) / 16) * ((maxSize.v.height + 15) / 16) * 384); |
| 211 | return C2R::Ok(); |
| 212 | } |
| 213 | |
| 214 | static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me, |
| 215 | const C2P<C2StreamPictureSizeInfo::output> &size) { |
| 216 | (void)mayBlock; |
| 217 | (void)size; |
| 218 | (void)me; // TODO: validate |
| 219 | return C2R::Ok(); |
| 220 | } |
| 221 | |
| 222 | static C2R DefaultColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsTuning::output> &me) { |
| 223 | (void)mayBlock; |
| 224 | if (me.v.range > C2Color::RANGE_OTHER) { |
| 225 | me.set().range = C2Color::RANGE_OTHER; |
| 226 | } |
| 227 | if (me.v.primaries > C2Color::PRIMARIES_OTHER) { |
| 228 | me.set().primaries = C2Color::PRIMARIES_OTHER; |
| 229 | } |
| 230 | if (me.v.transfer > C2Color::TRANSFER_OTHER) { |
| 231 | me.set().transfer = C2Color::TRANSFER_OTHER; |
| 232 | } |
| 233 | if (me.v.matrix > C2Color::MATRIX_OTHER) { |
| 234 | me.set().matrix = C2Color::MATRIX_OTHER; |
| 235 | } |
| 236 | return C2R::Ok(); |
| 237 | } |
| 238 | |
| 239 | static C2R CodedColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::input> &me) { |
| 240 | (void)mayBlock; |
| 241 | if (me.v.range > C2Color::RANGE_OTHER) { |
| 242 | me.set().range = C2Color::RANGE_OTHER; |
| 243 | } |
| 244 | if (me.v.primaries > C2Color::PRIMARIES_OTHER) { |
| 245 | me.set().primaries = C2Color::PRIMARIES_OTHER; |
| 246 | } |
| 247 | if (me.v.transfer > C2Color::TRANSFER_OTHER) { |
| 248 | me.set().transfer = C2Color::TRANSFER_OTHER; |
| 249 | } |
| 250 | if (me.v.matrix > C2Color::MATRIX_OTHER) { |
| 251 | me.set().matrix = C2Color::MATRIX_OTHER; |
| 252 | } |
| 253 | return C2R::Ok(); |
| 254 | } |
| 255 | |
| 256 | static C2R ColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::output> &me, |
| 257 | const C2P<C2StreamColorAspectsTuning::output> &def, |
| 258 | const C2P<C2StreamColorAspectsInfo::input> &coded) { |
| 259 | (void)mayBlock; |
| 260 | // take default values for all unspecified fields, and coded values for specified ones |
| 261 | me.set().range = coded.v.range == RANGE_UNSPECIFIED ? def.v.range : coded.v.range; |
| 262 | me.set().primaries = coded.v.primaries == PRIMARIES_UNSPECIFIED |
| 263 | ? def.v.primaries : coded.v.primaries; |
| 264 | me.set().transfer = coded.v.transfer == TRANSFER_UNSPECIFIED |
| 265 | ? def.v.transfer : coded.v.transfer; |
| 266 | me.set().matrix = coded.v.matrix == MATRIX_UNSPECIFIED ? def.v.matrix : coded.v.matrix; |
| 267 | return C2R::Ok(); |
| 268 | } |
| 269 | |
| 270 | std::shared_ptr<C2StreamColorAspectsInfo::output> getColorAspects_l() { |
| 271 | return mColorAspects; |
| 272 | } |
| 273 | |
| 274 | private: |
| 275 | std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel; |
| 276 | std::shared_ptr<C2StreamPictureSizeInfo::output> mSize; |
| 277 | std::shared_ptr<C2StreamMaxPictureSizeTuning::output> mMaxSize; |
| 278 | std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize; |
| 279 | std::shared_ptr<C2StreamColorInfo::output> mColorInfo; |
| 280 | std::shared_ptr<C2StreamColorAspectsInfo::input> mCodedColorAspects; |
| 281 | std::shared_ptr<C2StreamColorAspectsTuning::output> mDefaultColorAspects; |
| 282 | std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects; |
| 283 | std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormat; |
| 284 | }; |
| 285 | |
| 286 | static size_t getCpuCoreCount() { |
| 287 | long cpuCoreCount = 1; |
| 288 | #if defined(_SC_NPROCESSORS_ONLN) |
| 289 | cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN); |
| 290 | #else |
| 291 | // _SC_NPROC_ONLN must be defined... |
| 292 | cpuCoreCount = sysconf(_SC_NPROC_ONLN); |
| 293 | #endif |
| 294 | CHECK(cpuCoreCount >= 1); |
| 295 | ALOGV("Number of CPU cores: %ld", cpuCoreCount); |
| 296 | return (size_t)cpuCoreCount; |
| 297 | } |
| 298 | |
| 299 | static void *ivd_aligned_malloc(WORD32 alignment, WORD32 size) { |
| 300 | return memalign(alignment, size); |
| 301 | } |
| 302 | |
| 303 | static void ivd_aligned_free(void *mem) { |
| 304 | free(mem); |
| 305 | } |
| 306 | |
| 307 | C2SoftMpeg2Dec::C2SoftMpeg2Dec( |
| 308 | const char *name, |
| 309 | c2_node_id_t id, |
| 310 | const std::shared_ptr<IntfImpl> &intfImpl) |
| 311 | : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)), |
| 312 | mIntf(intfImpl), |
| 313 | mDecHandle(nullptr), |
| 314 | mMemRecords(nullptr), |
| 315 | mOutBufferDrain(nullptr), |
| 316 | mIvColorformat(IV_YUV_420P), |
| 317 | mWidth(320), |
Rakesh Kumar | d91bc48 | 2019-02-18 10:42:41 +0530 | [diff] [blame] | 318 | mHeight(240), |
| 319 | mOutIndex(0u) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 320 | // If input dump is enabled, then open create an empty file |
| 321 | GENERATE_FILE_NAMES(); |
| 322 | CREATE_DUMP_FILE(mInFile); |
| 323 | } |
| 324 | |
| 325 | C2SoftMpeg2Dec::~C2SoftMpeg2Dec() { |
| 326 | onRelease(); |
| 327 | } |
| 328 | |
| 329 | c2_status_t C2SoftMpeg2Dec::onInit() { |
| 330 | status_t err = initDecoder(); |
| 331 | return err == OK ? C2_OK : C2_CORRUPTED; |
| 332 | } |
| 333 | |
| 334 | c2_status_t C2SoftMpeg2Dec::onStop() { |
| 335 | if (OK != resetDecoder()) return C2_CORRUPTED; |
| 336 | resetPlugin(); |
| 337 | return C2_OK; |
| 338 | } |
| 339 | |
| 340 | void C2SoftMpeg2Dec::onReset() { |
| 341 | (void) onStop(); |
| 342 | } |
| 343 | |
| 344 | void C2SoftMpeg2Dec::onRelease() { |
| 345 | (void) deleteDecoder(); |
| 346 | if (mOutBufferDrain) { |
| 347 | ivd_aligned_free(mOutBufferDrain); |
| 348 | mOutBufferDrain = nullptr; |
| 349 | } |
| 350 | if (mOutBlock) { |
| 351 | mOutBlock.reset(); |
| 352 | } |
| 353 | if (mMemRecords) { |
| 354 | ivd_aligned_free(mMemRecords); |
| 355 | mMemRecords = nullptr; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | c2_status_t C2SoftMpeg2Dec::onFlush_sm() { |
| 360 | if (OK != setFlushMode()) return C2_CORRUPTED; |
| 361 | |
| 362 | uint32_t displayStride = mStride; |
| 363 | uint32_t displayHeight = mHeight; |
| 364 | uint32_t bufferSize = displayStride * displayHeight * 3 / 2; |
| 365 | mOutBufferDrain = (uint8_t *)ivd_aligned_malloc(128, bufferSize); |
| 366 | if (!mOutBufferDrain) { |
| 367 | ALOGE("could not allocate tmp output buffer (for flush) of size %u ", bufferSize); |
| 368 | return C2_NO_MEMORY; |
| 369 | } |
| 370 | |
| 371 | while (true) { |
| 372 | ivd_video_decode_ip_t s_decode_ip; |
| 373 | ivd_video_decode_op_t s_decode_op; |
| 374 | |
| 375 | setDecodeArgs(&s_decode_ip, &s_decode_op, nullptr, nullptr, 0, 0, 0); |
| 376 | (void) ivdec_api_function(mDecHandle, &s_decode_ip, &s_decode_op); |
| 377 | if (0 == s_decode_op.u4_output_present) { |
| 378 | resetPlugin(); |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if (mOutBufferDrain) { |
| 384 | ivd_aligned_free(mOutBufferDrain); |
| 385 | mOutBufferDrain = nullptr; |
| 386 | } |
| 387 | |
| 388 | return C2_OK; |
| 389 | } |
| 390 | |
| 391 | status_t C2SoftMpeg2Dec::getNumMemRecords() { |
| 392 | iv_num_mem_rec_ip_t s_num_mem_rec_ip; |
| 393 | iv_num_mem_rec_op_t s_num_mem_rec_op; |
| 394 | |
| 395 | s_num_mem_rec_ip.u4_size = sizeof(s_num_mem_rec_ip); |
| 396 | s_num_mem_rec_ip.e_cmd = IV_CMD_GET_NUM_MEM_REC; |
| 397 | s_num_mem_rec_op.u4_size = sizeof(s_num_mem_rec_op); |
| 398 | |
| 399 | IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle, |
| 400 | &s_num_mem_rec_ip, |
| 401 | &s_num_mem_rec_op); |
| 402 | if (IV_SUCCESS != status) { |
| 403 | ALOGE("Error in getting mem records: 0x%x", s_num_mem_rec_op.u4_error_code); |
| 404 | return UNKNOWN_ERROR; |
| 405 | } |
| 406 | mNumMemRecords = s_num_mem_rec_op.u4_num_mem_rec; |
| 407 | |
| 408 | return OK; |
| 409 | } |
| 410 | |
| 411 | status_t C2SoftMpeg2Dec::fillMemRecords() { |
| 412 | iv_mem_rec_t *ps_mem_rec = (iv_mem_rec_t *) ivd_aligned_malloc( |
| 413 | 128, mNumMemRecords * sizeof(iv_mem_rec_t)); |
| 414 | if (!ps_mem_rec) { |
| 415 | ALOGE("Allocation failure"); |
| 416 | return NO_MEMORY; |
| 417 | } |
| 418 | memset(ps_mem_rec, 0, mNumMemRecords * sizeof(iv_mem_rec_t)); |
| 419 | for (size_t i = 0; i < mNumMemRecords; i++) |
| 420 | ps_mem_rec[i].u4_size = sizeof(iv_mem_rec_t); |
| 421 | mMemRecords = ps_mem_rec; |
| 422 | |
| 423 | ivdext_fill_mem_rec_ip_t s_fill_mem_ip; |
| 424 | ivdext_fill_mem_rec_op_t s_fill_mem_op; |
| 425 | |
| 426 | s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_size = sizeof(ivdext_fill_mem_rec_ip_t); |
| 427 | s_fill_mem_ip.u4_share_disp_buf = 0; |
| 428 | s_fill_mem_ip.e_output_format = mIvColorformat; |
| 429 | s_fill_mem_ip.u4_deinterlace = 1; |
| 430 | s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.e_cmd = IV_CMD_FILL_NUM_MEM_REC; |
| 431 | s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location = mMemRecords; |
| 432 | s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_wd = mWidth; |
| 433 | s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_ht = mHeight; |
| 434 | s_fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_size = sizeof(ivdext_fill_mem_rec_op_t); |
| 435 | IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle, |
| 436 | &s_fill_mem_ip, |
| 437 | &s_fill_mem_op); |
| 438 | if (IV_SUCCESS != status) { |
| 439 | ALOGE("Error in filling mem records: 0x%x", |
| 440 | s_fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_error_code); |
| 441 | return UNKNOWN_ERROR; |
| 442 | } |
| 443 | |
| 444 | CHECK_EQ(mNumMemRecords, s_fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_num_mem_rec_filled); |
| 445 | for (size_t i = 0; i < mNumMemRecords; i++, ps_mem_rec++) { |
| 446 | ps_mem_rec->pv_base = ivd_aligned_malloc( |
| 447 | ps_mem_rec->u4_mem_alignment, ps_mem_rec->u4_mem_size); |
| 448 | if (!ps_mem_rec->pv_base) { |
| 449 | ALOGE("Allocation failure for memory record #%zu of size %u", |
| 450 | i, ps_mem_rec->u4_mem_size); |
| 451 | return NO_MEMORY; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return OK; |
| 456 | } |
| 457 | |
| 458 | status_t C2SoftMpeg2Dec::createDecoder() { |
| 459 | ivdext_init_ip_t s_init_ip; |
| 460 | ivdext_init_op_t s_init_op; |
| 461 | |
| 462 | s_init_ip.s_ivd_init_ip_t.u4_size = sizeof(ivdext_init_ip_t); |
| 463 | s_init_ip.s_ivd_init_ip_t.e_cmd = (IVD_API_COMMAND_TYPE_T)IV_CMD_INIT; |
| 464 | s_init_ip.s_ivd_init_ip_t.pv_mem_rec_location = mMemRecords; |
| 465 | s_init_ip.s_ivd_init_ip_t.u4_frm_max_wd = mWidth; |
| 466 | s_init_ip.s_ivd_init_ip_t.u4_frm_max_ht = mHeight; |
| 467 | s_init_ip.u4_share_disp_buf = 0; |
| 468 | s_init_ip.u4_deinterlace = 1; |
| 469 | s_init_ip.s_ivd_init_ip_t.u4_num_mem_rec = mNumMemRecords; |
| 470 | s_init_ip.s_ivd_init_ip_t.e_output_format = mIvColorformat; |
| 471 | s_init_op.s_ivd_init_op_t.u4_size = sizeof(ivdext_init_op_t); |
| 472 | |
| 473 | mDecHandle = (iv_obj_t *)mMemRecords[0].pv_base; |
| 474 | mDecHandle->pv_fxns = (void *)ivdec_api_function; |
| 475 | mDecHandle->u4_size = sizeof(iv_obj_t); |
| 476 | |
| 477 | IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle, |
| 478 | &s_init_ip, |
| 479 | &s_init_op); |
| 480 | if (status != IV_SUCCESS) { |
| 481 | ALOGE("error in %s: 0x%x", __func__, |
| 482 | s_init_op.s_ivd_init_op_t.u4_error_code); |
| 483 | return UNKNOWN_ERROR; |
| 484 | } |
| 485 | |
| 486 | return OK; |
| 487 | } |
| 488 | |
| 489 | status_t C2SoftMpeg2Dec::setNumCores() { |
| 490 | ivdext_ctl_set_num_cores_ip_t s_set_num_cores_ip; |
| 491 | ivdext_ctl_set_num_cores_op_t s_set_num_cores_op; |
| 492 | |
| 493 | s_set_num_cores_ip.u4_size = sizeof(ivdext_ctl_set_num_cores_ip_t); |
| 494 | s_set_num_cores_ip.e_cmd = IVD_CMD_VIDEO_CTL; |
| 495 | s_set_num_cores_ip.e_sub_cmd = IVDEXT_CMD_CTL_SET_NUM_CORES; |
| 496 | s_set_num_cores_ip.u4_num_cores = mNumCores; |
| 497 | s_set_num_cores_op.u4_size = sizeof(ivdext_ctl_set_num_cores_op_t); |
| 498 | IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle, |
| 499 | &s_set_num_cores_ip, |
| 500 | &s_set_num_cores_op); |
| 501 | if (status != IV_SUCCESS) { |
| 502 | ALOGD("error in %s: 0x%x", __func__, s_set_num_cores_op.u4_error_code); |
| 503 | return UNKNOWN_ERROR; |
| 504 | } |
| 505 | |
| 506 | return OK; |
| 507 | } |
| 508 | |
| 509 | status_t C2SoftMpeg2Dec::setParams(size_t stride) { |
| 510 | ivd_ctl_set_config_ip_t s_set_dyn_params_ip; |
| 511 | ivd_ctl_set_config_op_t s_set_dyn_params_op; |
| 512 | |
| 513 | s_set_dyn_params_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t); |
| 514 | s_set_dyn_params_ip.e_cmd = IVD_CMD_VIDEO_CTL; |
| 515 | s_set_dyn_params_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS; |
| 516 | s_set_dyn_params_ip.u4_disp_wd = (UWORD32) stride; |
| 517 | s_set_dyn_params_ip.e_frm_skip_mode = IVD_SKIP_NONE; |
| 518 | s_set_dyn_params_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT; |
| 519 | s_set_dyn_params_ip.e_vid_dec_mode = IVD_DECODE_FRAME; |
| 520 | s_set_dyn_params_op.u4_size = sizeof(ivd_ctl_set_config_op_t); |
| 521 | IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle, |
| 522 | &s_set_dyn_params_ip, |
| 523 | &s_set_dyn_params_op); |
| 524 | if (status != IV_SUCCESS) { |
| 525 | ALOGE("error in %s: 0x%x", __func__, s_set_dyn_params_op.u4_error_code); |
| 526 | return UNKNOWN_ERROR; |
| 527 | } |
| 528 | |
| 529 | return OK; |
| 530 | } |
| 531 | |
| 532 | status_t C2SoftMpeg2Dec::getVersion() { |
| 533 | ivd_ctl_getversioninfo_ip_t s_get_versioninfo_ip; |
| 534 | ivd_ctl_getversioninfo_op_t s_get_versioninfo_op; |
| 535 | UWORD8 au1_buf[512]; |
| 536 | |
| 537 | s_get_versioninfo_ip.u4_size = sizeof(ivd_ctl_getversioninfo_ip_t); |
| 538 | s_get_versioninfo_ip.e_cmd = IVD_CMD_VIDEO_CTL; |
| 539 | s_get_versioninfo_ip.e_sub_cmd = IVD_CMD_CTL_GETVERSION; |
| 540 | s_get_versioninfo_ip.pv_version_buffer = au1_buf; |
| 541 | s_get_versioninfo_ip.u4_version_buffer_size = sizeof(au1_buf); |
| 542 | s_get_versioninfo_op.u4_size = sizeof(ivd_ctl_getversioninfo_op_t); |
| 543 | IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle, |
| 544 | &s_get_versioninfo_ip, |
| 545 | &s_get_versioninfo_op); |
| 546 | if (status != IV_SUCCESS) { |
| 547 | ALOGD("error in %s: 0x%x", __func__, |
| 548 | s_get_versioninfo_op.u4_error_code); |
| 549 | } else { |
| 550 | ALOGV("ittiam decoder version number: %s", |
| 551 | (char *) s_get_versioninfo_ip.pv_version_buffer); |
| 552 | } |
| 553 | |
| 554 | return OK; |
| 555 | } |
| 556 | |
| 557 | status_t C2SoftMpeg2Dec::initDecoder() { |
| 558 | status_t ret = getNumMemRecords(); |
| 559 | if (OK != ret) return ret; |
| 560 | |
| 561 | ret = fillMemRecords(); |
| 562 | if (OK != ret) return ret; |
| 563 | |
| 564 | if (OK != createDecoder()) return UNKNOWN_ERROR; |
| 565 | |
| 566 | mNumCores = MIN(getCpuCoreCount(), MAX_NUM_CORES); |
| 567 | mStride = ALIGN64(mWidth); |
| 568 | mSignalledError = false; |
| 569 | resetPlugin(); |
| 570 | (void) setNumCores(); |
| 571 | if (OK != setParams(mStride)) return UNKNOWN_ERROR; |
| 572 | (void) getVersion(); |
| 573 | |
| 574 | return OK; |
| 575 | } |
| 576 | |
| 577 | bool C2SoftMpeg2Dec::setDecodeArgs(ivd_video_decode_ip_t *ps_decode_ip, |
| 578 | ivd_video_decode_op_t *ps_decode_op, |
| 579 | C2ReadView *inBuffer, |
| 580 | C2GraphicView *outBuffer, |
| 581 | size_t inOffset, |
| 582 | size_t inSize, |
| 583 | uint32_t tsMarker) { |
| 584 | uint32_t displayStride = mStride; |
| 585 | uint32_t displayHeight = mHeight; |
| 586 | size_t lumaSize = displayStride * displayHeight; |
| 587 | size_t chromaSize = lumaSize >> 2; |
| 588 | |
| 589 | ps_decode_ip->u4_size = sizeof(ivd_video_decode_ip_t); |
| 590 | ps_decode_ip->e_cmd = IVD_CMD_VIDEO_DECODE; |
| 591 | if (inBuffer) { |
| 592 | ps_decode_ip->u4_ts = tsMarker; |
| 593 | ps_decode_ip->pv_stream_buffer = const_cast<uint8_t *>(inBuffer->data() + inOffset); |
| 594 | ps_decode_ip->u4_num_Bytes = inSize; |
| 595 | } else { |
| 596 | ps_decode_ip->u4_ts = 0; |
| 597 | ps_decode_ip->pv_stream_buffer = nullptr; |
| 598 | ps_decode_ip->u4_num_Bytes = 0; |
| 599 | } |
| 600 | ps_decode_ip->s_out_buffer.u4_min_out_buf_size[0] = lumaSize; |
| 601 | ps_decode_ip->s_out_buffer.u4_min_out_buf_size[1] = chromaSize; |
| 602 | ps_decode_ip->s_out_buffer.u4_min_out_buf_size[2] = chromaSize; |
| 603 | if (outBuffer) { |
| 604 | if (outBuffer->width() < displayStride || outBuffer->height() < displayHeight) { |
| 605 | ALOGE("Output buffer too small: provided (%dx%d) required (%ux%u)", |
| 606 | outBuffer->width(), outBuffer->height(), displayStride, displayHeight); |
| 607 | return false; |
| 608 | } |
| 609 | ps_decode_ip->s_out_buffer.pu1_bufs[0] = outBuffer->data()[C2PlanarLayout::PLANE_Y]; |
| 610 | ps_decode_ip->s_out_buffer.pu1_bufs[1] = outBuffer->data()[C2PlanarLayout::PLANE_U]; |
| 611 | ps_decode_ip->s_out_buffer.pu1_bufs[2] = outBuffer->data()[C2PlanarLayout::PLANE_V]; |
| 612 | } else { |
| 613 | ps_decode_ip->s_out_buffer.pu1_bufs[0] = mOutBufferDrain; |
| 614 | ps_decode_ip->s_out_buffer.pu1_bufs[1] = mOutBufferDrain + lumaSize; |
| 615 | ps_decode_ip->s_out_buffer.pu1_bufs[2] = mOutBufferDrain + lumaSize + chromaSize; |
| 616 | } |
| 617 | ps_decode_ip->s_out_buffer.u4_num_bufs = 3; |
| 618 | ps_decode_op->u4_size = sizeof(ivd_video_decode_op_t); |
| 619 | |
| 620 | return true; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | bool C2SoftMpeg2Dec::getSeqInfo() { |
| 625 | ivdext_ctl_get_seq_info_ip_t s_ctl_get_seq_info_ip; |
| 626 | ivdext_ctl_get_seq_info_op_t s_ctl_get_seq_info_op; |
| 627 | |
| 628 | s_ctl_get_seq_info_ip.u4_size = sizeof(ivdext_ctl_get_seq_info_ip_t); |
| 629 | s_ctl_get_seq_info_ip.e_cmd = IVD_CMD_VIDEO_CTL; |
| 630 | s_ctl_get_seq_info_ip.e_sub_cmd = |
| 631 | (IVD_CONTROL_API_COMMAND_TYPE_T)IMPEG2D_CMD_CTL_GET_SEQ_INFO; |
| 632 | s_ctl_get_seq_info_op.u4_size = sizeof(ivdext_ctl_get_seq_info_op_t); |
| 633 | IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle, |
| 634 | &s_ctl_get_seq_info_ip, |
| 635 | &s_ctl_get_seq_info_op); |
| 636 | if (status != IV_SUCCESS) { |
| 637 | ALOGW("Error in getting Sequence info: 0x%x", s_ctl_get_seq_info_op.u4_error_code); |
| 638 | return false; |
| 639 | } |
| 640 | |
| 641 | VuiColorAspects vuiColorAspects; |
| 642 | vuiColorAspects.primaries = s_ctl_get_seq_info_op.u1_colour_primaries; |
| 643 | vuiColorAspects.transfer = s_ctl_get_seq_info_op.u1_transfer_characteristics; |
| 644 | vuiColorAspects.coeffs = s_ctl_get_seq_info_op.u1_matrix_coefficients; |
| 645 | vuiColorAspects.fullRange = false; // mpeg2 video has limited range. |
| 646 | |
| 647 | // convert vui aspects to C2 values if changed |
| 648 | if (!(vuiColorAspects == mBitstreamColorAspects)) { |
| 649 | mBitstreamColorAspects = vuiColorAspects; |
| 650 | ColorAspects sfAspects; |
| 651 | C2StreamColorAspectsInfo::input codedAspects = { 0u }; |
| 652 | ColorUtils::convertIsoColorAspectsToCodecAspects( |
| 653 | vuiColorAspects.primaries, vuiColorAspects.transfer, vuiColorAspects.coeffs, |
| 654 | vuiColorAspects.fullRange, sfAspects); |
| 655 | if (!C2Mapper::map(sfAspects.mPrimaries, &codedAspects.primaries)) { |
| 656 | codedAspects.primaries = C2Color::PRIMARIES_UNSPECIFIED; |
| 657 | } |
| 658 | if (!C2Mapper::map(sfAspects.mRange, &codedAspects.range)) { |
| 659 | codedAspects.range = C2Color::RANGE_UNSPECIFIED; |
| 660 | } |
| 661 | if (!C2Mapper::map(sfAspects.mMatrixCoeffs, &codedAspects.matrix)) { |
| 662 | codedAspects.matrix = C2Color::MATRIX_UNSPECIFIED; |
| 663 | } |
| 664 | if (!C2Mapper::map(sfAspects.mTransfer, &codedAspects.transfer)) { |
| 665 | codedAspects.transfer = C2Color::TRANSFER_UNSPECIFIED; |
| 666 | } |
| 667 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 668 | (void)mIntf->config({&codedAspects}, C2_MAY_BLOCK, &failures); |
| 669 | } |
| 670 | return true; |
| 671 | } |
| 672 | |
| 673 | status_t C2SoftMpeg2Dec::setFlushMode() { |
| 674 | ivd_ctl_flush_ip_t s_set_flush_ip; |
| 675 | ivd_ctl_flush_op_t s_set_flush_op; |
| 676 | |
| 677 | s_set_flush_ip.u4_size = sizeof(ivd_ctl_flush_ip_t); |
| 678 | s_set_flush_ip.e_cmd = IVD_CMD_VIDEO_CTL; |
| 679 | s_set_flush_ip.e_sub_cmd = IVD_CMD_CTL_FLUSH; |
| 680 | s_set_flush_op.u4_size = sizeof(ivd_ctl_flush_op_t); |
| 681 | IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle, |
| 682 | &s_set_flush_ip, |
| 683 | &s_set_flush_op); |
| 684 | if (status != IV_SUCCESS) { |
| 685 | ALOGE("error in %s: 0x%x", __func__, s_set_flush_op.u4_error_code); |
| 686 | return UNKNOWN_ERROR; |
| 687 | } |
| 688 | |
| 689 | return OK; |
| 690 | } |
| 691 | |
| 692 | status_t C2SoftMpeg2Dec::resetDecoder() { |
| 693 | ivd_ctl_reset_ip_t s_reset_ip; |
| 694 | ivd_ctl_reset_op_t s_reset_op; |
| 695 | |
| 696 | s_reset_ip.u4_size = sizeof(ivd_ctl_reset_ip_t); |
| 697 | s_reset_ip.e_cmd = IVD_CMD_VIDEO_CTL; |
| 698 | s_reset_ip.e_sub_cmd = IVD_CMD_CTL_RESET; |
| 699 | s_reset_op.u4_size = sizeof(ivd_ctl_reset_op_t); |
| 700 | IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle, |
| 701 | &s_reset_ip, |
| 702 | &s_reset_op); |
| 703 | if (IV_SUCCESS != status) { |
| 704 | ALOGE("error in %s: 0x%x", __func__, s_reset_op.u4_error_code); |
| 705 | return UNKNOWN_ERROR; |
| 706 | } |
| 707 | (void) setNumCores(); |
| 708 | mStride = 0; |
| 709 | mSignalledError = false; |
| 710 | |
| 711 | return OK; |
| 712 | } |
| 713 | |
| 714 | void C2SoftMpeg2Dec::resetPlugin() { |
| 715 | mSignalledOutputEos = false; |
| 716 | gettimeofday(&mTimeStart, nullptr); |
| 717 | gettimeofday(&mTimeEnd, nullptr); |
| 718 | } |
| 719 | |
| 720 | status_t C2SoftMpeg2Dec::deleteDecoder() { |
| 721 | if (mMemRecords) { |
| 722 | iv_mem_rec_t *ps_mem_rec = mMemRecords; |
| 723 | |
| 724 | for (size_t i = 0; i < mNumMemRecords; i++, ps_mem_rec++) { |
| 725 | if (ps_mem_rec->pv_base) { |
| 726 | ivd_aligned_free(ps_mem_rec->pv_base); |
| 727 | } |
| 728 | } |
| 729 | ivd_aligned_free(mMemRecords); |
| 730 | mMemRecords = nullptr; |
| 731 | } |
| 732 | mDecHandle = nullptr; |
| 733 | |
| 734 | return OK; |
| 735 | } |
| 736 | |
| 737 | status_t C2SoftMpeg2Dec::reInitDecoder() { |
| 738 | deleteDecoder(); |
| 739 | |
| 740 | status_t ret = initDecoder(); |
| 741 | if (OK != ret) { |
| 742 | ALOGE("Failed to initialize decoder"); |
| 743 | deleteDecoder(); |
| 744 | return ret; |
| 745 | } |
| 746 | return OK; |
| 747 | } |
| 748 | |
| 749 | void fillEmptyWork(const std::unique_ptr<C2Work> &work) { |
| 750 | uint32_t flags = 0; |
| 751 | if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) { |
| 752 | flags |= C2FrameData::FLAG_END_OF_STREAM; |
| 753 | ALOGV("signalling eos"); |
| 754 | } |
| 755 | work->worklets.front()->output.flags = (C2FrameData::flags_t)flags; |
| 756 | work->worklets.front()->output.buffers.clear(); |
| 757 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 758 | work->workletsProcessed = 1u; |
| 759 | } |
| 760 | |
| 761 | void C2SoftMpeg2Dec::finishWork(uint64_t index, const std::unique_ptr<C2Work> &work) { |
| 762 | std::shared_ptr<C2Buffer> buffer = createGraphicBuffer(std::move(mOutBlock), |
| 763 | C2Rect(mWidth, mHeight)); |
| 764 | mOutBlock = nullptr; |
| 765 | { |
| 766 | IntfImpl::Lock lock = mIntf->lock(); |
| 767 | buffer->setInfo(mIntf->getColorAspects_l()); |
| 768 | } |
| 769 | |
Rakesh Kumar | d91bc48 | 2019-02-18 10:42:41 +0530 | [diff] [blame] | 770 | class FillWork { |
| 771 | public: |
| 772 | FillWork(uint32_t flags, C2WorkOrdinalStruct ordinal, |
| 773 | const std::shared_ptr<C2Buffer>& buffer) |
| 774 | : mFlags(flags), mOrdinal(ordinal), mBuffer(buffer) {} |
| 775 | ~FillWork() = default; |
| 776 | |
| 777 | void operator()(const std::unique_ptr<C2Work>& work) { |
| 778 | work->worklets.front()->output.flags = (C2FrameData::flags_t)mFlags; |
| 779 | work->worklets.front()->output.buffers.clear(); |
| 780 | work->worklets.front()->output.ordinal = mOrdinal; |
| 781 | work->workletsProcessed = 1u; |
| 782 | work->result = C2_OK; |
| 783 | if (mBuffer) { |
| 784 | work->worklets.front()->output.buffers.push_back(mBuffer); |
| 785 | } |
| 786 | ALOGV("timestamp = %lld, index = %lld, w/%s buffer", |
| 787 | mOrdinal.timestamp.peekll(), mOrdinal.frameIndex.peekll(), |
| 788 | mBuffer ? "" : "o"); |
| 789 | } |
| 790 | |
| 791 | private: |
| 792 | const uint32_t mFlags; |
| 793 | const C2WorkOrdinalStruct mOrdinal; |
| 794 | const std::shared_ptr<C2Buffer> mBuffer; |
| 795 | }; |
| 796 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 797 | auto fillWork = [buffer](const std::unique_ptr<C2Work> &work) { |
| 798 | work->worklets.front()->output.flags = (C2FrameData::flags_t)0; |
| 799 | work->worklets.front()->output.buffers.clear(); |
| 800 | work->worklets.front()->output.buffers.push_back(buffer); |
| 801 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 802 | work->workletsProcessed = 1u; |
| 803 | }; |
| 804 | if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) { |
Rakesh Kumar | d91bc48 | 2019-02-18 10:42:41 +0530 | [diff] [blame] | 805 | bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0); |
| 806 | // TODO: Check if cloneAndSend can be avoided by tracking number of frames remaining |
| 807 | if (eos) { |
| 808 | if (buffer) { |
| 809 | mOutIndex = index; |
| 810 | C2WorkOrdinalStruct outOrdinal = work->input.ordinal; |
| 811 | cloneAndSend( |
| 812 | mOutIndex, work, |
| 813 | FillWork(C2FrameData::FLAG_INCOMPLETE, outOrdinal, buffer)); |
| 814 | buffer.reset(); |
| 815 | } |
| 816 | } else { |
| 817 | fillWork(work); |
| 818 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 819 | } else { |
| 820 | finish(index, fillWork); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | c2_status_t C2SoftMpeg2Dec::ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool) { |
| 825 | if (!mDecHandle) { |
| 826 | ALOGE("not supposed to be here, invalid decoder context"); |
| 827 | return C2_CORRUPTED; |
| 828 | } |
| 829 | if (mStride != ALIGN64(mWidth)) { |
| 830 | mStride = ALIGN64(mWidth); |
| 831 | if (OK != setParams(mStride)) return C2_CORRUPTED; |
| 832 | } |
| 833 | if (mOutBlock && |
| 834 | (mOutBlock->width() != mStride || mOutBlock->height() != mHeight)) { |
| 835 | mOutBlock.reset(); |
| 836 | } |
| 837 | if (!mOutBlock) { |
| 838 | uint32_t format = HAL_PIXEL_FORMAT_YV12; |
| 839 | C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }; |
| 840 | c2_status_t err = pool->fetchGraphicBlock(mStride, mHeight, format, usage, &mOutBlock); |
| 841 | if (err != C2_OK) { |
| 842 | ALOGE("fetchGraphicBlock for Output failed with status %d", err); |
| 843 | return err; |
| 844 | } |
| 845 | ALOGV("provided (%dx%d) required (%dx%d)", |
| 846 | mOutBlock->width(), mOutBlock->height(), mStride, mHeight); |
| 847 | } |
| 848 | |
| 849 | return C2_OK; |
| 850 | } |
| 851 | |
| 852 | // TODO: can overall error checking be improved? |
| 853 | // TODO: allow configuration of color format and usage for graphic buffers instead |
| 854 | // of hard coding them to HAL_PIXEL_FORMAT_YV12 |
| 855 | // TODO: pass coloraspects information to surface |
| 856 | // TODO: test support for dynamic change in resolution |
| 857 | // TODO: verify if the decoder sent back all frames |
| 858 | void C2SoftMpeg2Dec::process( |
| 859 | const std::unique_ptr<C2Work> &work, |
| 860 | const std::shared_ptr<C2BlockPool> &pool) { |
| 861 | // Initialize output work |
| 862 | work->result = C2_OK; |
| 863 | work->workletsProcessed = 0u; |
| 864 | work->worklets.front()->output.configUpdate.clear(); |
| 865 | work->worklets.front()->output.flags = work->input.flags; |
| 866 | |
| 867 | if (mSignalledError || mSignalledOutputEos) { |
| 868 | work->result = C2_BAD_VALUE; |
| 869 | return; |
| 870 | } |
| 871 | |
| 872 | size_t inOffset = 0u; |
| 873 | size_t inSize = 0u; |
| 874 | uint32_t workIndex = work->input.ordinal.frameIndex.peeku() & 0xFFFFFFFF; |
| 875 | C2ReadView rView = mDummyReadView; |
| 876 | if (!work->input.buffers.empty()) { |
| 877 | rView = work->input.buffers[0]->data().linearBlocks().front().map().get(); |
| 878 | inSize = rView.capacity(); |
| 879 | if (inSize && rView.error()) { |
| 880 | ALOGE("read view map failed %d", rView.error()); |
| 881 | work->result = C2_CORRUPTED; |
| 882 | return; |
| 883 | } |
| 884 | } |
| 885 | bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0); |
| 886 | bool hasPicture = false; |
| 887 | |
| 888 | ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x", |
| 889 | inSize, (int)work->input.ordinal.timestamp.peeku(), |
| 890 | (int)work->input.ordinal.frameIndex.peeku(), work->input.flags); |
| 891 | size_t inPos = 0; |
| 892 | while (inPos < inSize) { |
| 893 | if (C2_OK != ensureDecoderState(pool)) { |
| 894 | mSignalledError = true; |
| 895 | work->workletsProcessed = 1u; |
| 896 | work->result = C2_CORRUPTED; |
| 897 | return; |
| 898 | } |
| 899 | C2GraphicView wView = mOutBlock->map().get(); |
| 900 | if (wView.error()) { |
| 901 | ALOGE("graphic view map failed %d", wView.error()); |
| 902 | work->result = C2_CORRUPTED; |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | ivd_video_decode_ip_t s_decode_ip; |
| 907 | ivd_video_decode_op_t s_decode_op; |
| 908 | if (!setDecodeArgs(&s_decode_ip, &s_decode_op, &rView, &wView, |
| 909 | inOffset + inPos, inSize - inPos, workIndex)) { |
| 910 | mSignalledError = true; |
| 911 | work->workletsProcessed = 1u; |
| 912 | work->result = C2_CORRUPTED; |
| 913 | return; |
| 914 | } |
| 915 | // If input dump is enabled, then write to file |
| 916 | DUMP_TO_FILE(mInFile, s_decode_ip.pv_stream_buffer, s_decode_ip.u4_num_Bytes); |
| 917 | WORD32 delay; |
| 918 | GETTIME(&mTimeStart, nullptr); |
| 919 | TIME_DIFF(mTimeEnd, mTimeStart, delay); |
| 920 | (void) ivdec_api_function(mDecHandle, &s_decode_ip, &s_decode_op); |
| 921 | WORD32 decodeTime; |
| 922 | GETTIME(&mTimeEnd, nullptr); |
| 923 | TIME_DIFF(mTimeStart, mTimeEnd, decodeTime); |
| 924 | ALOGV("decodeTime=%6d delay=%6d numBytes=%6d ", decodeTime, delay, |
| 925 | s_decode_op.u4_num_bytes_consumed); |
| 926 | if (IMPEG2D_UNSUPPORTED_DIMENSIONS == s_decode_op.u4_error_code) { |
| 927 | ALOGV("unsupported resolution : %dx%d", s_decode_op.u4_pic_wd, s_decode_op.u4_pic_ht); |
| 928 | drainInternal(DRAIN_COMPONENT_NO_EOS, pool, work); |
| 929 | resetPlugin(); |
| 930 | work->workletsProcessed = 0u; |
| 931 | mWidth = s_decode_op.u4_pic_wd; |
| 932 | mHeight = s_decode_op.u4_pic_ht; |
| 933 | |
| 934 | ALOGI("Configuring decoder: mWidth %d , mHeight %d ", |
| 935 | mWidth, mHeight); |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 936 | C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 937 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 938 | c2_status_t err = |
| 939 | mIntf->config({&size}, C2_MAY_BLOCK, &failures); |
| 940 | if (err == OK) { |
| 941 | work->worklets.front()->output.configUpdate.push_back( |
| 942 | C2Param::Copy(size)); |
| 943 | } else { |
| 944 | ALOGE("Cannot set width and height"); |
| 945 | mSignalledError = true; |
| 946 | work->workletsProcessed = 1u; |
| 947 | work->result = C2_CORRUPTED; |
| 948 | return; |
| 949 | } |
| 950 | |
| 951 | if (OK != reInitDecoder()) { |
| 952 | ALOGE("Failed to reinitialize decoder"); |
| 953 | mSignalledError = true; |
| 954 | work->workletsProcessed = 1u; |
| 955 | work->result = C2_CORRUPTED; |
| 956 | return; |
| 957 | } |
| 958 | continue; |
| 959 | } else if (IVD_RES_CHANGED == (s_decode_op.u4_error_code & 0xFF)) { |
| 960 | ALOGV("resolution changed"); |
| 961 | drainInternal(DRAIN_COMPONENT_NO_EOS, pool, work); |
| 962 | resetDecoder(); |
| 963 | resetPlugin(); |
| 964 | work->workletsProcessed = 0u; |
| 965 | continue; |
| 966 | } |
| 967 | if (0 < s_decode_op.u4_pic_wd && 0 < s_decode_op.u4_pic_ht) { |
| 968 | if (s_decode_op.u4_pic_wd != mWidth || s_decode_op.u4_pic_ht != mHeight) { |
| 969 | mWidth = s_decode_op.u4_pic_wd; |
| 970 | mHeight = s_decode_op.u4_pic_ht; |
| 971 | CHECK_EQ(0u, s_decode_op.u4_output_present); |
| 972 | |
| 973 | ALOGI("Configuring decoder out: mWidth %d , mHeight %d ", |
| 974 | mWidth, mHeight); |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 975 | C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 976 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 977 | c2_status_t err = |
| 978 | mIntf->config({&size}, C2_MAY_BLOCK, &failures); |
| 979 | if (err == OK) { |
| 980 | work->worklets.front()->output.configUpdate.push_back( |
| 981 | C2Param::Copy(size)); |
| 982 | } else { |
| 983 | ALOGE("Cannot set width and height"); |
| 984 | mSignalledError = true; |
| 985 | work->workletsProcessed = 1u; |
| 986 | work->result = C2_CORRUPTED; |
| 987 | return; |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | (void) getSeqInfo(); |
| 993 | hasPicture |= (1 == s_decode_op.u4_frame_decoded_flag); |
| 994 | if (s_decode_op.u4_output_present) { |
| 995 | finishWork(s_decode_op.u4_ts, work); |
| 996 | } |
| 997 | |
| 998 | inPos += s_decode_op.u4_num_bytes_consumed; |
| 999 | if (hasPicture && (inSize - inPos) != 0) { |
| 1000 | ALOGD("decoded frame in current access nal, ignoring further trailing bytes %d", |
| 1001 | (int)inSize - (int)inPos); |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (eos) { |
| 1007 | drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work); |
| 1008 | mSignalledOutputEos = true; |
| 1009 | } else if (!hasPicture) { |
| 1010 | fillEmptyWork(work); |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | c2_status_t C2SoftMpeg2Dec::drainInternal( |
| 1015 | uint32_t drainMode, |
| 1016 | const std::shared_ptr<C2BlockPool> &pool, |
| 1017 | const std::unique_ptr<C2Work> &work) { |
| 1018 | if (drainMode == NO_DRAIN) { |
| 1019 | ALOGW("drain with NO_DRAIN: no-op"); |
| 1020 | return C2_OK; |
| 1021 | } |
| 1022 | if (drainMode == DRAIN_CHAIN) { |
| 1023 | ALOGW("DRAIN_CHAIN not supported"); |
| 1024 | return C2_OMITTED; |
| 1025 | } |
| 1026 | |
| 1027 | if (OK != setFlushMode()) return C2_CORRUPTED; |
| 1028 | while (true) { |
| 1029 | if (C2_OK != ensureDecoderState(pool)) { |
| 1030 | mSignalledError = true; |
| 1031 | work->workletsProcessed = 1u; |
| 1032 | work->result = C2_CORRUPTED; |
| 1033 | return C2_CORRUPTED; |
| 1034 | } |
| 1035 | C2GraphicView wView = mOutBlock->map().get(); |
| 1036 | if (wView.error()) { |
| 1037 | ALOGE("graphic view map failed %d", wView.error()); |
| 1038 | return C2_CORRUPTED; |
| 1039 | } |
| 1040 | ivd_video_decode_ip_t s_decode_ip; |
| 1041 | ivd_video_decode_op_t s_decode_op; |
| 1042 | if (!setDecodeArgs(&s_decode_ip, &s_decode_op, nullptr, &wView, 0, 0, 0)) { |
| 1043 | mSignalledError = true; |
| 1044 | work->workletsProcessed = 1u; |
| 1045 | return C2_CORRUPTED; |
| 1046 | } |
| 1047 | (void) ivdec_api_function(mDecHandle, &s_decode_ip, &s_decode_op); |
| 1048 | if (s_decode_op.u4_output_present) { |
| 1049 | finishWork(s_decode_op.u4_ts, work); |
| 1050 | } else { |
| 1051 | fillEmptyWork(work); |
| 1052 | break; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | return C2_OK; |
| 1057 | } |
| 1058 | |
| 1059 | c2_status_t C2SoftMpeg2Dec::drain( |
| 1060 | uint32_t drainMode, |
| 1061 | const std::shared_ptr<C2BlockPool> &pool) { |
| 1062 | return drainInternal(drainMode, pool, nullptr); |
| 1063 | } |
| 1064 | |
| 1065 | class C2SoftMpeg2DecFactory : public C2ComponentFactory { |
| 1066 | public: |
| 1067 | C2SoftMpeg2DecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>( |
| 1068 | GetCodec2PlatformComponentStore()->getParamReflector())) { |
| 1069 | } |
| 1070 | |
| 1071 | virtual c2_status_t createComponent( |
| 1072 | c2_node_id_t id, |
| 1073 | std::shared_ptr<C2Component>* const component, |
| 1074 | std::function<void(C2Component*)> deleter) override { |
| 1075 | *component = std::shared_ptr<C2Component>( |
| 1076 | new C2SoftMpeg2Dec(COMPONENT_NAME, |
| 1077 | id, |
| 1078 | std::make_shared<C2SoftMpeg2Dec::IntfImpl>(mHelper)), |
| 1079 | deleter); |
| 1080 | return C2_OK; |
| 1081 | } |
| 1082 | |
| 1083 | virtual c2_status_t createInterface( |
| 1084 | c2_node_id_t id, |
| 1085 | std::shared_ptr<C2ComponentInterface>* const interface, |
| 1086 | std::function<void(C2ComponentInterface*)> deleter) override { |
| 1087 | *interface = std::shared_ptr<C2ComponentInterface>( |
| 1088 | new SimpleInterface<C2SoftMpeg2Dec::IntfImpl>( |
| 1089 | COMPONENT_NAME, id, std::make_shared<C2SoftMpeg2Dec::IntfImpl>(mHelper)), |
| 1090 | deleter); |
| 1091 | return C2_OK; |
| 1092 | } |
| 1093 | |
| 1094 | virtual ~C2SoftMpeg2DecFactory() override = default; |
| 1095 | |
| 1096 | private: |
| 1097 | std::shared_ptr<C2ReflectorHelper> mHelper; |
| 1098 | }; |
| 1099 | |
| 1100 | } // namespace android |
| 1101 | |
| 1102 | extern "C" ::C2ComponentFactory* CreateCodec2Factory() { |
| 1103 | ALOGV("in %s", __func__); |
| 1104 | return new ::android::C2SoftMpeg2DecFactory(); |
| 1105 | } |
| 1106 | |
| 1107 | extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) { |
| 1108 | ALOGV("in %s", __func__); |
| 1109 | delete factory; |
| 1110 | } |