Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "HeifDecoderImpl" |
| 19 | |
| 20 | #include "HeifDecoderImpl.h" |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include <binder/IMemory.h> |
Dongwon Kang | 49ce671 | 2018-01-24 10:16:25 -0800 | [diff] [blame] | 25 | #include <binder/MemoryDealer.h> |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 26 | #include <drm/drm_framework_common.h> |
| 27 | #include <media/IDataSource.h> |
| 28 | #include <media/mediametadataretriever.h> |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 29 | #include <media/MediaSource.h> |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 30 | #include <media/stagefright/foundation/ADebug.h> |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 31 | #include <private/media/VideoFrame.h> |
| 32 | #include <utils/Log.h> |
| 33 | #include <utils/RefBase.h> |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 34 | #include <vector> |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 35 | |
| 36 | HeifDecoder* createHeifDecoder() { |
| 37 | return new android::HeifDecoderImpl(); |
| 38 | } |
| 39 | |
| 40 | namespace android { |
| 41 | |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 42 | void initFrameInfo(HeifFrameInfo *info, const VideoFrame *videoFrame) { |
| 43 | info->mWidth = videoFrame->mWidth; |
| 44 | info->mHeight = videoFrame->mHeight; |
| 45 | info->mRotationAngle = videoFrame->mRotationAngle; |
| 46 | info->mBytesPerPixel = videoFrame->mBytesPerPixel; |
Chong Zhang | 0af4db6 | 2019-08-23 15:34:58 -0700 | [diff] [blame^] | 47 | info->mDurationUs = videoFrame->mDurationUs; |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 48 | if (videoFrame->mIccSize > 0) { |
| 49 | info->mIccData.assign( |
| 50 | videoFrame->getFlattenedIccData(), |
| 51 | videoFrame->getFlattenedIccData() + videoFrame->mIccSize); |
| 52 | } else { |
| 53 | // clear old Icc data if there is no Icc data. |
| 54 | info->mIccData.clear(); |
| 55 | } |
| 56 | } |
| 57 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 58 | /* |
| 59 | * HeifDataSource |
| 60 | * |
| 61 | * Proxies data requests over IDataSource interface from MediaMetadataRetriever |
| 62 | * to the HeifStream interface we received from the heif decoder client. |
| 63 | */ |
| 64 | class HeifDataSource : public BnDataSource { |
| 65 | public: |
| 66 | /* |
| 67 | * Constructs HeifDataSource; will take ownership of |stream|. |
| 68 | */ |
| 69 | HeifDataSource(HeifStream* stream) |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 70 | : mStream(stream), mEOS(false), |
| 71 | mCachedOffset(0), mCachedSize(0), mCacheBufferSize(0) {} |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 72 | |
| 73 | ~HeifDataSource() override {} |
| 74 | |
| 75 | /* |
| 76 | * Initializes internal resources. |
| 77 | */ |
| 78 | bool init(); |
| 79 | |
| 80 | sp<IMemory> getIMemory() override { return mMemory; } |
| 81 | ssize_t readAt(off64_t offset, size_t size) override; |
| 82 | status_t getSize(off64_t* size) override ; |
| 83 | void close() {} |
| 84 | uint32_t getFlags() override { return 0; } |
| 85 | String8 toString() override { return String8("HeifDataSource"); } |
| 86 | sp<DecryptHandle> DrmInitialization(const char*) override { |
| 87 | return nullptr; |
| 88 | } |
| 89 | |
| 90 | private: |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 91 | enum { |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 92 | /* |
| 93 | * Buffer size for passing the read data to mediaserver. Set to 64K |
| 94 | * (which is what MediaDataSource Java API's jni implementation uses). |
| 95 | */ |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 96 | kBufferSize = 64 * 1024, |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 97 | /* |
| 98 | * Initial and max cache buffer size. |
| 99 | */ |
| 100 | kInitialCacheBufferSize = 4 * 1024 * 1024, |
| 101 | kMaxCacheBufferSize = 64 * 1024 * 1024, |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 102 | }; |
| 103 | sp<IMemory> mMemory; |
| 104 | std::unique_ptr<HeifStream> mStream; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 105 | bool mEOS; |
Chong Zhang | cd03192 | 2018-08-24 13:03:19 -0700 | [diff] [blame] | 106 | std::unique_ptr<uint8_t[]> mCache; |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 107 | off64_t mCachedOffset; |
| 108 | size_t mCachedSize; |
| 109 | size_t mCacheBufferSize; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | bool HeifDataSource::init() { |
| 113 | sp<MemoryDealer> memoryDealer = |
| 114 | new MemoryDealer(kBufferSize, "HeifDataSource"); |
| 115 | mMemory = memoryDealer->allocate(kBufferSize); |
| 116 | if (mMemory == nullptr) { |
| 117 | ALOGE("Failed to allocate shared memory!"); |
| 118 | return false; |
| 119 | } |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 120 | mCache.reset(new uint8_t[kInitialCacheBufferSize]); |
| 121 | if (mCache.get() == nullptr) { |
| 122 | ALOGE("mFailed to allocate cache!"); |
| 123 | return false; |
| 124 | } |
| 125 | mCacheBufferSize = kInitialCacheBufferSize; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
| 129 | ssize_t HeifDataSource::readAt(off64_t offset, size_t size) { |
| 130 | ALOGV("readAt: offset=%lld, size=%zu", (long long)offset, size); |
| 131 | |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 132 | if (offset < mCachedOffset) { |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 133 | // try seek, then rewind/skip, fail if none worked |
| 134 | if (mStream->seek(offset)) { |
| 135 | ALOGV("readAt: seek to offset=%lld", (long long)offset); |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 136 | mCachedOffset = offset; |
| 137 | mCachedSize = 0; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 138 | mEOS = false; |
| 139 | } else if (mStream->rewind()) { |
| 140 | ALOGV("readAt: rewind to offset=0"); |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 141 | mCachedOffset = 0; |
| 142 | mCachedSize = 0; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 143 | mEOS = false; |
| 144 | } else { |
| 145 | ALOGE("readAt: couldn't seek or rewind!"); |
| 146 | mEOS = true; |
| 147 | } |
| 148 | } |
| 149 | |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 150 | if (mEOS && (offset < mCachedOffset || |
| 151 | offset >= (off64_t)(mCachedOffset + mCachedSize))) { |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 152 | ALOGV("readAt: EOS"); |
| 153 | return ERROR_END_OF_STREAM; |
| 154 | } |
| 155 | |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 156 | // at this point, offset must be >= mCachedOffset, other cases should |
| 157 | // have been caught above. |
| 158 | CHECK(offset >= mCachedOffset); |
| 159 | |
Sungtak Lee | 237f903 | 2018-03-05 15:21:33 -0800 | [diff] [blame] | 160 | off64_t resultOffset; |
| 161 | if (__builtin_add_overflow(offset, size, &resultOffset)) { |
| 162 | return ERROR_IO; |
| 163 | } |
| 164 | |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 165 | if (size == 0) { |
| 166 | return 0; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 169 | // Can only read max of kBufferSize |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 170 | if (size > kBufferSize) { |
| 171 | size = kBufferSize; |
| 172 | } |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 173 | |
| 174 | // copy from cache if the request falls entirely in cache |
| 175 | if (offset + size <= mCachedOffset + mCachedSize) { |
| 176 | memcpy(mMemory->pointer(), mCache.get() + offset - mCachedOffset, size); |
| 177 | return size; |
| 178 | } |
| 179 | |
| 180 | // need to fetch more, check if we need to expand the cache buffer. |
| 181 | if ((off64_t)(offset + size) > mCachedOffset + kMaxCacheBufferSize) { |
| 182 | // it's reaching max cache buffer size, need to roll window, and possibly |
| 183 | // expand the cache buffer. |
| 184 | size_t newCacheBufferSize = mCacheBufferSize; |
Chong Zhang | cd03192 | 2018-08-24 13:03:19 -0700 | [diff] [blame] | 185 | std::unique_ptr<uint8_t[]> newCache; |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 186 | uint8_t* dst = mCache.get(); |
| 187 | if (newCacheBufferSize < kMaxCacheBufferSize) { |
| 188 | newCacheBufferSize = kMaxCacheBufferSize; |
| 189 | newCache.reset(new uint8_t[newCacheBufferSize]); |
| 190 | dst = newCache.get(); |
| 191 | } |
| 192 | |
| 193 | // when rolling the cache window, try to keep about half the old bytes |
| 194 | // in case that the client goes back. |
| 195 | off64_t newCachedOffset = offset - (off64_t)(newCacheBufferSize / 2); |
| 196 | if (newCachedOffset < mCachedOffset) { |
| 197 | newCachedOffset = mCachedOffset; |
| 198 | } |
| 199 | |
| 200 | int64_t newCachedSize = (int64_t)(mCachedOffset + mCachedSize) - newCachedOffset; |
| 201 | if (newCachedSize > 0) { |
| 202 | // in this case, the new cache region partially overlop the old cache, |
| 203 | // move the portion of the cache we want to save to the beginning of |
| 204 | // the cache buffer. |
| 205 | memcpy(dst, mCache.get() + newCachedOffset - mCachedOffset, newCachedSize); |
| 206 | } else if (newCachedSize < 0){ |
| 207 | // in this case, the new cache region is entirely out of the old cache, |
| 208 | // in order to guarantee sequential read, we need to skip a number of |
| 209 | // bytes before reading. |
| 210 | size_t bytesToSkip = -newCachedSize; |
| 211 | size_t bytesSkipped = mStream->read(nullptr, bytesToSkip); |
| 212 | if (bytesSkipped != bytesToSkip) { |
| 213 | // bytesSkipped is invalid, there is not enough bytes to reach |
| 214 | // the requested offset. |
| 215 | ALOGE("readAt: skip failed, EOS"); |
| 216 | |
| 217 | mEOS = true; |
| 218 | mCachedOffset = newCachedOffset; |
| 219 | mCachedSize = 0; |
| 220 | return ERROR_END_OF_STREAM; |
| 221 | } |
| 222 | // set cache size to 0, since we're not keeping any old cache |
| 223 | newCachedSize = 0; |
| 224 | } |
| 225 | |
| 226 | if (newCache.get() != nullptr) { |
| 227 | mCache.reset(newCache.release()); |
| 228 | mCacheBufferSize = newCacheBufferSize; |
| 229 | } |
| 230 | mCachedOffset = newCachedOffset; |
| 231 | mCachedSize = newCachedSize; |
| 232 | |
| 233 | ALOGV("readAt: rolling cache window to (%lld, %zu), cache buffer size %zu", |
| 234 | (long long)mCachedOffset, mCachedSize, mCacheBufferSize); |
| 235 | } else { |
| 236 | // expand cache buffer, but no need to roll the window |
| 237 | size_t newCacheBufferSize = mCacheBufferSize; |
| 238 | while (offset + size > mCachedOffset + newCacheBufferSize) { |
| 239 | newCacheBufferSize *= 2; |
| 240 | } |
| 241 | CHECK(newCacheBufferSize <= kMaxCacheBufferSize); |
| 242 | if (mCacheBufferSize < newCacheBufferSize) { |
| 243 | uint8_t* newCache = new uint8_t[newCacheBufferSize]; |
| 244 | memcpy(newCache, mCache.get(), mCachedSize); |
| 245 | mCache.reset(newCache); |
| 246 | mCacheBufferSize = newCacheBufferSize; |
| 247 | |
| 248 | ALOGV("readAt: current cache window (%lld, %zu), new cache buffer size %zu", |
| 249 | (long long) mCachedOffset, mCachedSize, mCacheBufferSize); |
| 250 | } |
| 251 | } |
| 252 | size_t bytesToRead = offset + size - mCachedOffset - mCachedSize; |
| 253 | size_t bytesRead = mStream->read(mCache.get() + mCachedSize, bytesToRead); |
| 254 | if (bytesRead > bytesToRead || bytesRead == 0) { |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 255 | // bytesRead is invalid |
| 256 | mEOS = true; |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 257 | bytesRead = 0; |
| 258 | } else if (bytesRead < bytesToRead) { |
| 259 | // read some bytes but not all, set EOS |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 260 | mEOS = true; |
| 261 | } |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 262 | mCachedSize += bytesRead; |
| 263 | ALOGV("readAt: current cache window (%lld, %zu)", |
| 264 | (long long) mCachedOffset, mCachedSize); |
| 265 | |
| 266 | // here bytesAvailable could be negative if offset jumped past EOS. |
| 267 | int64_t bytesAvailable = mCachedOffset + mCachedSize - offset; |
| 268 | if (bytesAvailable <= 0) { |
| 269 | return ERROR_END_OF_STREAM; |
| 270 | } |
| 271 | if (bytesAvailable < (int64_t)size) { |
| 272 | size = bytesAvailable; |
| 273 | } |
| 274 | memcpy(mMemory->pointer(), mCache.get() + offset - mCachedOffset, size); |
| 275 | return size; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | status_t HeifDataSource::getSize(off64_t* size) { |
| 279 | if (!mStream->hasLength()) { |
| 280 | *size = -1; |
| 281 | ALOGE("getSize: not supported!"); |
| 282 | return ERROR_UNSUPPORTED; |
| 283 | } |
| 284 | *size = mStream->getLength(); |
| 285 | ALOGV("getSize: size=%lld", (long long)*size); |
| 286 | return OK; |
| 287 | } |
| 288 | |
| 289 | ///////////////////////////////////////////////////////////////////////// |
| 290 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 291 | struct HeifDecoderImpl::DecodeThread : public Thread { |
| 292 | explicit DecodeThread(HeifDecoderImpl *decoder) : mDecoder(decoder) {} |
| 293 | |
| 294 | private: |
| 295 | HeifDecoderImpl* mDecoder; |
| 296 | |
| 297 | bool threadLoop(); |
| 298 | |
| 299 | DISALLOW_EVIL_CONSTRUCTORS(DecodeThread); |
| 300 | }; |
| 301 | |
| 302 | bool HeifDecoderImpl::DecodeThread::threadLoop() { |
| 303 | return mDecoder->decodeAsync(); |
| 304 | } |
| 305 | |
| 306 | ///////////////////////////////////////////////////////////////////////// |
| 307 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 308 | HeifDecoderImpl::HeifDecoderImpl() : |
| 309 | // output color format should always be set via setOutputColor(), in case |
| 310 | // it's not, default to HAL_PIXEL_FORMAT_RGB_565. |
| 311 | mOutputColor(HAL_PIXEL_FORMAT_RGB_565), |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 312 | mCurScanline(0), |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 313 | mTotalScanline(0), |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 314 | mFrameDecoded(false), |
| 315 | mHasImage(false), |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 316 | mHasVideo(false), |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 317 | mSequenceLength(0), |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 318 | mAvailableLines(0), |
| 319 | mNumSlices(1), |
| 320 | mSliceHeight(0), |
| 321 | mAsyncDecodeDone(false) { |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | HeifDecoderImpl::~HeifDecoderImpl() { |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 325 | if (mThread != nullptr) { |
| 326 | mThread->join(); |
| 327 | } |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | bool HeifDecoderImpl::init(HeifStream* stream, HeifFrameInfo* frameInfo) { |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 331 | mFrameDecoded = false; |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 332 | mFrameMemory.clear(); |
| 333 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 334 | sp<HeifDataSource> dataSource = new HeifDataSource(stream); |
| 335 | if (!dataSource->init()) { |
| 336 | return false; |
| 337 | } |
| 338 | mDataSource = dataSource; |
| 339 | |
| 340 | mRetriever = new MediaMetadataRetriever(); |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 341 | status_t err = mRetriever->setDataSource(mDataSource, "image/heif"); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 342 | if (err != OK) { |
| 343 | ALOGE("failed to set data source!"); |
| 344 | |
| 345 | mRetriever.clear(); |
| 346 | mDataSource.clear(); |
| 347 | return false; |
| 348 | } |
| 349 | ALOGV("successfully set data source."); |
| 350 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 351 | const char* hasImage = mRetriever->extractMetadata(METADATA_KEY_HAS_IMAGE); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 352 | const char* hasVideo = mRetriever->extractMetadata(METADATA_KEY_HAS_VIDEO); |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 353 | |
| 354 | mHasImage = hasImage && !strcasecmp(hasImage, "yes"); |
| 355 | mHasVideo = hasVideo && !strcasecmp(hasVideo, "yes"); |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 356 | |
| 357 | HeifFrameInfo* defaultInfo = nullptr; |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 358 | if (mHasImage) { |
| 359 | // image index < 0 to retrieve primary image |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 360 | sp<IMemory> sharedMem = mRetriever->getImageAtIndex( |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 361 | -1, mOutputColor, true /*metaOnly*/); |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 362 | |
| 363 | if (sharedMem == nullptr || sharedMem->pointer() == nullptr) { |
| 364 | ALOGE("init: videoFrame is a nullptr"); |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | VideoFrame* videoFrame = static_cast<VideoFrame*>(sharedMem->pointer()); |
| 369 | |
| 370 | ALOGV("Image dimension %dx%d, display %dx%d, angle %d, iccSize %d", |
| 371 | videoFrame->mWidth, |
| 372 | videoFrame->mHeight, |
| 373 | videoFrame->mDisplayWidth, |
| 374 | videoFrame->mDisplayHeight, |
| 375 | videoFrame->mRotationAngle, |
| 376 | videoFrame->mIccSize); |
| 377 | |
| 378 | initFrameInfo(&mImageInfo, videoFrame); |
| 379 | |
| 380 | if (videoFrame->mTileHeight >= 512) { |
| 381 | // Try decoding in slices only if the image has tiles and is big enough. |
| 382 | mSliceHeight = videoFrame->mTileHeight; |
| 383 | ALOGV("mSliceHeight %u", mSliceHeight); |
| 384 | } |
| 385 | |
| 386 | defaultInfo = &mImageInfo; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 389 | if (mHasVideo) { |
| 390 | sp<IMemory> sharedMem = mRetriever->getFrameAtTime(0, |
| 391 | MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC, |
| 392 | mOutputColor, true /*metaOnly*/); |
| 393 | |
| 394 | if (sharedMem == nullptr || sharedMem->pointer() == nullptr) { |
| 395 | ALOGE("init: videoFrame is a nullptr"); |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | VideoFrame* videoFrame = static_cast<VideoFrame*>(sharedMem->pointer()); |
| 400 | |
| 401 | ALOGV("Sequence dimension %dx%d, display %dx%d, angle %d, iccSize %d", |
| 402 | videoFrame->mWidth, |
| 403 | videoFrame->mHeight, |
| 404 | videoFrame->mDisplayWidth, |
| 405 | videoFrame->mDisplayHeight, |
| 406 | videoFrame->mRotationAngle, |
| 407 | videoFrame->mIccSize); |
| 408 | |
| 409 | initFrameInfo(&mSequenceInfo, videoFrame); |
| 410 | |
| 411 | mSequenceLength = atoi(mRetriever->extractMetadata(METADATA_KEY_VIDEO_FRAME_COUNT)); |
| 412 | |
| 413 | if (defaultInfo == nullptr) { |
| 414 | defaultInfo = &mSequenceInfo; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (defaultInfo == nullptr) { |
| 419 | ALOGD("No valid image or sequence available"); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 420 | return false; |
| 421 | } |
| 422 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 423 | if (frameInfo != nullptr) { |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 424 | *frameInfo = *defaultInfo; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 425 | } |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 426 | |
| 427 | // default total scanline, this might change if decodeSequence() is used |
| 428 | mTotalScanline = defaultInfo->mHeight; |
| 429 | |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | bool HeifDecoderImpl::getSequenceInfo( |
| 434 | HeifFrameInfo* frameInfo, size_t *frameCount) { |
| 435 | ALOGV("%s", __FUNCTION__); |
| 436 | if (!mHasVideo) { |
| 437 | return false; |
| 438 | } |
| 439 | if (frameInfo != nullptr) { |
| 440 | *frameInfo = mSequenceInfo; |
| 441 | } |
| 442 | if (frameCount != nullptr) { |
| 443 | *frameCount = mSequenceLength; |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 444 | } |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 445 | return true; |
| 446 | } |
| 447 | |
| 448 | bool HeifDecoderImpl::getEncodedColor(HeifEncodedColor* /*outColor*/) const { |
| 449 | ALOGW("getEncodedColor: not implemented!"); |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | bool HeifDecoderImpl::setOutputColor(HeifColorFormat heifColor) { |
| 454 | switch(heifColor) { |
| 455 | case kHeifColorFormat_RGB565: |
| 456 | { |
| 457 | mOutputColor = HAL_PIXEL_FORMAT_RGB_565; |
| 458 | return true; |
| 459 | } |
| 460 | case kHeifColorFormat_RGBA_8888: |
| 461 | { |
| 462 | mOutputColor = HAL_PIXEL_FORMAT_RGBA_8888; |
| 463 | return true; |
| 464 | } |
| 465 | case kHeifColorFormat_BGRA_8888: |
| 466 | { |
| 467 | mOutputColor = HAL_PIXEL_FORMAT_BGRA_8888; |
| 468 | return true; |
| 469 | } |
| 470 | default: |
| 471 | break; |
| 472 | } |
| 473 | ALOGE("Unsupported output color format %d", heifColor); |
| 474 | return false; |
| 475 | } |
| 476 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 477 | bool HeifDecoderImpl::decodeAsync() { |
| 478 | for (size_t i = 1; i < mNumSlices; i++) { |
| 479 | ALOGV("decodeAsync(): decoding slice %zu", i); |
| 480 | size_t top = i * mSliceHeight; |
| 481 | size_t bottom = (i + 1) * mSliceHeight; |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 482 | if (bottom > mImageInfo.mHeight) { |
| 483 | bottom = mImageInfo.mHeight; |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 484 | } |
| 485 | sp<IMemory> frameMemory = mRetriever->getImageRectAtIndex( |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 486 | -1, mOutputColor, 0, top, mImageInfo.mWidth, bottom); |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 487 | { |
| 488 | Mutex::Autolock autolock(mLock); |
| 489 | |
| 490 | if (frameMemory == nullptr || frameMemory->pointer() == nullptr) { |
| 491 | mAsyncDecodeDone = true; |
| 492 | mScanlineReady.signal(); |
| 493 | break; |
| 494 | } |
| 495 | mFrameMemory = frameMemory; |
| 496 | mAvailableLines = bottom; |
| 497 | ALOGV("decodeAsync(): available lines %zu", mAvailableLines); |
| 498 | mScanlineReady.signal(); |
| 499 | } |
| 500 | } |
| 501 | // Aggressive clear to avoid holding on to resources |
| 502 | mRetriever.clear(); |
| 503 | mDataSource.clear(); |
| 504 | return false; |
| 505 | } |
| 506 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 507 | bool HeifDecoderImpl::decode(HeifFrameInfo* frameInfo) { |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 508 | // reset scanline pointer |
| 509 | mCurScanline = 0; |
| 510 | |
| 511 | if (mFrameDecoded) { |
| 512 | return true; |
| 513 | } |
| 514 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 515 | // See if we want to decode in slices to allow client to start |
| 516 | // scanline processing in parallel with decode. If this fails |
| 517 | // we fallback to decoding the full frame. |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 518 | if (mHasImage) { |
| 519 | if (mSliceHeight >= 512 && |
| 520 | mImageInfo.mWidth >= 3000 && |
| 521 | mImageInfo.mHeight >= 2000 ) { |
| 522 | // Try decoding in slices only if the image has tiles and is big enough. |
| 523 | mNumSlices = (mImageInfo.mHeight + mSliceHeight - 1) / mSliceHeight; |
| 524 | ALOGV("mSliceHeight %u, mNumSlices %zu", mSliceHeight, mNumSlices); |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 527 | if (mNumSlices > 1) { |
| 528 | // get first slice and metadata |
| 529 | sp<IMemory> frameMemory = mRetriever->getImageRectAtIndex( |
| 530 | -1, mOutputColor, 0, 0, mImageInfo.mWidth, mSliceHeight); |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 531 | |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 532 | if (frameMemory == nullptr || frameMemory->pointer() == nullptr) { |
| 533 | ALOGE("decode: metadata is a nullptr"); |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | VideoFrame* videoFrame = static_cast<VideoFrame*>(frameMemory->pointer()); |
| 538 | |
| 539 | if (frameInfo != nullptr) { |
| 540 | initFrameInfo(frameInfo, videoFrame); |
| 541 | } |
| 542 | mFrameMemory = frameMemory; |
| 543 | mAvailableLines = mSliceHeight; |
| 544 | mThread = new DecodeThread(this); |
| 545 | if (mThread->run("HeifDecode", ANDROID_PRIORITY_FOREGROUND) == OK) { |
| 546 | mFrameDecoded = true; |
| 547 | return true; |
| 548 | } |
| 549 | // Fallback to decode without slicing |
| 550 | mThread.clear(); |
| 551 | mNumSlices = 1; |
| 552 | mSliceHeight = 0; |
| 553 | mAvailableLines = 0; |
| 554 | mFrameMemory.clear(); |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 555 | } |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 558 | if (mHasImage) { |
| 559 | // image index < 0 to retrieve primary image |
| 560 | mFrameMemory = mRetriever->getImageAtIndex(-1, mOutputColor); |
| 561 | } else if (mHasVideo) { |
| 562 | mFrameMemory = mRetriever->getFrameAtTime(0, |
| 563 | MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC, mOutputColor); |
| 564 | } |
| 565 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 566 | if (mFrameMemory == nullptr || mFrameMemory->pointer() == nullptr) { |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 567 | ALOGE("decode: videoFrame is a nullptr"); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 568 | return false; |
| 569 | } |
| 570 | |
| 571 | VideoFrame* videoFrame = static_cast<VideoFrame*>(mFrameMemory->pointer()); |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 572 | if (videoFrame->mSize == 0 || |
| 573 | mFrameMemory->size() < videoFrame->getFlattenedSize()) { |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 574 | ALOGE("decode: videoFrame size is invalid"); |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 578 | ALOGV("Decoded dimension %dx%d, display %dx%d, angle %d, rowbytes %d, size %d", |
| 579 | videoFrame->mWidth, |
| 580 | videoFrame->mHeight, |
| 581 | videoFrame->mDisplayWidth, |
| 582 | videoFrame->mDisplayHeight, |
| 583 | videoFrame->mRotationAngle, |
| 584 | videoFrame->mRowBytes, |
| 585 | videoFrame->mSize); |
| 586 | |
| 587 | if (frameInfo != nullptr) { |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 588 | initFrameInfo(frameInfo, videoFrame); |
| 589 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 590 | } |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 591 | mFrameDecoded = true; |
Chong Zhang | 4c6398b | 2017-09-07 17:43:19 -0700 | [diff] [blame] | 592 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 593 | // Aggressively clear to avoid holding on to resources |
Chong Zhang | 4c6398b | 2017-09-07 17:43:19 -0700 | [diff] [blame] | 594 | mRetriever.clear(); |
| 595 | mDataSource.clear(); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 596 | return true; |
| 597 | } |
| 598 | |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 599 | bool HeifDecoderImpl::decodeSequence(int frameIndex, HeifFrameInfo* frameInfo) { |
| 600 | ALOGV("%s: frame index %d", __FUNCTION__, frameIndex); |
| 601 | if (!mHasVideo) { |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | if (frameIndex < 0 || frameIndex >= mSequenceLength) { |
| 606 | ALOGE("invalid frame index: %d, total frames %zu", frameIndex, mSequenceLength); |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | mCurScanline = 0; |
| 611 | |
| 612 | // set total scanline to sequence height now |
| 613 | mTotalScanline = mSequenceInfo.mHeight; |
| 614 | |
| 615 | mFrameMemory = mRetriever->getFrameAtIndex(frameIndex, mOutputColor); |
| 616 | if (mFrameMemory == nullptr || mFrameMemory->pointer() == nullptr) { |
| 617 | ALOGE("decode: videoFrame is a nullptr"); |
| 618 | return false; |
| 619 | } |
| 620 | |
| 621 | VideoFrame* videoFrame = static_cast<VideoFrame*>(mFrameMemory->pointer()); |
| 622 | if (videoFrame->mSize == 0 || |
| 623 | mFrameMemory->size() < videoFrame->getFlattenedSize()) { |
| 624 | ALOGE("decode: videoFrame size is invalid"); |
| 625 | return false; |
| 626 | } |
| 627 | |
| 628 | ALOGV("Decoded dimension %dx%d, display %dx%d, angle %d, rowbytes %d, size %d", |
| 629 | videoFrame->mWidth, |
| 630 | videoFrame->mHeight, |
| 631 | videoFrame->mDisplayWidth, |
| 632 | videoFrame->mDisplayHeight, |
| 633 | videoFrame->mRotationAngle, |
| 634 | videoFrame->mRowBytes, |
| 635 | videoFrame->mSize); |
| 636 | |
| 637 | if (frameInfo != nullptr) { |
| 638 | initFrameInfo(frameInfo, videoFrame); |
| 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 643 | bool HeifDecoderImpl::getScanlineInner(uint8_t* dst) { |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 644 | if (mFrameMemory == nullptr || mFrameMemory->pointer() == nullptr) { |
| 645 | return false; |
| 646 | } |
| 647 | VideoFrame* videoFrame = static_cast<VideoFrame*>(mFrameMemory->pointer()); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 648 | uint8_t* src = videoFrame->getFlattenedData() + videoFrame->mRowBytes * mCurScanline++; |
Chong Zhang | 70b0afd | 2018-02-27 12:43:06 -0800 | [diff] [blame] | 649 | memcpy(dst, src, videoFrame->mBytesPerPixel * videoFrame->mWidth); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 650 | return true; |
| 651 | } |
| 652 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 653 | bool HeifDecoderImpl::getScanline(uint8_t* dst) { |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 654 | if (mCurScanline >= mTotalScanline) { |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 655 | ALOGE("no more scanline available"); |
| 656 | return false; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 657 | } |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 658 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 659 | if (mNumSlices > 1) { |
| 660 | Mutex::Autolock autolock(mLock); |
| 661 | |
| 662 | while (!mAsyncDecodeDone && mCurScanline >= mAvailableLines) { |
| 663 | mScanlineReady.wait(mLock); |
| 664 | } |
| 665 | return (mCurScanline < mAvailableLines) ? getScanlineInner(dst) : false; |
| 666 | } |
| 667 | |
| 668 | return getScanlineInner(dst); |
| 669 | } |
| 670 | |
| 671 | size_t HeifDecoderImpl::skipScanlines(size_t count) { |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 672 | uint32_t oldScanline = mCurScanline; |
| 673 | mCurScanline += count; |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 674 | if (mCurScanline > mTotalScanline) { |
| 675 | mCurScanline = mTotalScanline; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 676 | } |
| 677 | return (mCurScanline > oldScanline) ? (mCurScanline - oldScanline) : 0; |
| 678 | } |
| 679 | |
| 680 | } // namespace android |