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 |
Liz Kammer | 3ec5a0b | 2021-07-21 09:30:07 -0400 | [diff] [blame] | 18 | #include "include/HeifDecoderAPI.h" |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 19 | #define LOG_TAG "HeifDecoderImpl" |
| 20 | |
| 21 | #include "HeifDecoderImpl.h" |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | |
Marco Nelissen | dab79b3 | 2019-11-18 08:25:47 -0800 | [diff] [blame] | 25 | #include <android/IDataSource.h> |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 26 | #include <binder/IMemory.h> |
Dongwon Kang | 49ce671 | 2018-01-24 10:16:25 -0800 | [diff] [blame] | 27 | #include <binder/MemoryDealer.h> |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 28 | #include <drm/drm_framework_common.h> |
Ray Essick | e89e632 | 2022-02-02 13:33:50 -0800 | [diff] [blame] | 29 | #include <log/log.h> |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 30 | #include <media/mediametadataretriever.h> |
Marco Nelissen | 7291da6 | 2019-12-17 13:01:55 -0800 | [diff] [blame] | 31 | #include <media/stagefright/MediaSource.h> |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 32 | #include <media/stagefright/foundation/ADebug.h> |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 33 | #include <private/media/VideoFrame.h> |
| 34 | #include <utils/Log.h> |
| 35 | #include <utils/RefBase.h> |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 36 | #include <vector> |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 37 | |
| 38 | HeifDecoder* createHeifDecoder() { |
| 39 | return new android::HeifDecoderImpl(); |
| 40 | } |
| 41 | |
| 42 | namespace android { |
| 43 | |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 44 | void initFrameInfo(HeifFrameInfo *info, const VideoFrame *videoFrame) { |
| 45 | info->mWidth = videoFrame->mWidth; |
| 46 | info->mHeight = videoFrame->mHeight; |
| 47 | info->mRotationAngle = videoFrame->mRotationAngle; |
| 48 | info->mBytesPerPixel = videoFrame->mBytesPerPixel; |
Chong Zhang | 0af4db6 | 2019-08-23 15:34:58 -0700 | [diff] [blame] | 49 | info->mDurationUs = videoFrame->mDurationUs; |
Dichen Zhang | f406649 | 2022-02-17 17:32:53 -0800 | [diff] [blame] | 50 | info->mBitDepth = videoFrame->mBitDepth; |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 51 | if (videoFrame->mIccSize > 0) { |
| 52 | info->mIccData.assign( |
| 53 | videoFrame->getFlattenedIccData(), |
| 54 | videoFrame->getFlattenedIccData() + videoFrame->mIccSize); |
| 55 | } else { |
| 56 | // clear old Icc data if there is no Icc data. |
| 57 | info->mIccData.clear(); |
| 58 | } |
| 59 | } |
| 60 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 61 | /* |
| 62 | * HeifDataSource |
| 63 | * |
| 64 | * Proxies data requests over IDataSource interface from MediaMetadataRetriever |
| 65 | * to the HeifStream interface we received from the heif decoder client. |
| 66 | */ |
| 67 | class HeifDataSource : public BnDataSource { |
| 68 | public: |
| 69 | /* |
| 70 | * Constructs HeifDataSource; will take ownership of |stream|. |
| 71 | */ |
| 72 | HeifDataSource(HeifStream* stream) |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 73 | : mStream(stream), mEOS(false), |
| 74 | mCachedOffset(0), mCachedSize(0), mCacheBufferSize(0) {} |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 75 | |
| 76 | ~HeifDataSource() override {} |
| 77 | |
| 78 | /* |
| 79 | * Initializes internal resources. |
| 80 | */ |
| 81 | bool init(); |
| 82 | |
| 83 | sp<IMemory> getIMemory() override { return mMemory; } |
| 84 | ssize_t readAt(off64_t offset, size_t size) override; |
| 85 | status_t getSize(off64_t* size) override ; |
| 86 | void close() {} |
| 87 | uint32_t getFlags() override { return 0; } |
| 88 | String8 toString() override { return String8("HeifDataSource"); } |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 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) { |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 176 | memcpy(mMemory->unsecurePointer(), mCache.get() + offset - mCachedOffset, size); |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 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 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 274 | memcpy(mMemory->unsecurePointer(), mCache.get() + offset - mCachedOffset, size); |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 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 | |
Leon Scroggins III | 7ca678b | 2020-01-22 14:09:55 -0500 | [diff] [blame] | 340 | return reinit(frameInfo); |
| 341 | } |
| 342 | |
| 343 | bool HeifDecoderImpl::reinit(HeifFrameInfo* frameInfo) { |
| 344 | mFrameDecoded = false; |
| 345 | mFrameMemory.clear(); |
| 346 | |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 347 | sp<MediaMetadataRetriever> retriever = new MediaMetadataRetriever(); |
| 348 | status_t err = retriever->setDataSource(mDataSource, "image/heif"); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 349 | if (err != OK) { |
| 350 | ALOGE("failed to set data source!"); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 351 | mRetriever.clear(); |
| 352 | mDataSource.clear(); |
| 353 | return false; |
| 354 | } |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 355 | { |
| 356 | Mutex::Autolock _l(mRetrieverLock); |
| 357 | mRetriever = retriever; |
| 358 | } |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 359 | ALOGV("successfully set data source."); |
| 360 | |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 361 | const char* hasImage = retriever->extractMetadata(METADATA_KEY_HAS_IMAGE); |
| 362 | const char* hasVideo = retriever->extractMetadata(METADATA_KEY_HAS_VIDEO); |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 363 | |
| 364 | mHasImage = hasImage && !strcasecmp(hasImage, "yes"); |
| 365 | mHasVideo = hasVideo && !strcasecmp(hasVideo, "yes"); |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 366 | |
| 367 | HeifFrameInfo* defaultInfo = nullptr; |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 368 | if (mHasImage) { |
| 369 | // image index < 0 to retrieve primary image |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 370 | sp<IMemory> sharedMem = retriever->getImageAtIndex( |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 371 | -1, mOutputColor, true /*metaOnly*/); |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 372 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 373 | if (sharedMem == nullptr || sharedMem->unsecurePointer() == nullptr) { |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 374 | ALOGE("init: videoFrame is a nullptr"); |
| 375 | return false; |
| 376 | } |
| 377 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 378 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 379 | // (see declaration for details). |
| 380 | // Either document why it is safe in this case or address the |
| 381 | // issue (e.g. by copying). |
| 382 | VideoFrame* videoFrame = static_cast<VideoFrame*>(sharedMem->unsecurePointer()); |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 383 | |
Dichen Zhang | f406649 | 2022-02-17 17:32:53 -0800 | [diff] [blame] | 384 | ALOGV("Image dimension %dx%d, display %dx%d, angle %d, iccSize %d, bitDepth %d", |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 385 | videoFrame->mWidth, |
| 386 | videoFrame->mHeight, |
| 387 | videoFrame->mDisplayWidth, |
| 388 | videoFrame->mDisplayHeight, |
| 389 | videoFrame->mRotationAngle, |
Dichen Zhang | f406649 | 2022-02-17 17:32:53 -0800 | [diff] [blame] | 390 | videoFrame->mIccSize, |
| 391 | videoFrame->mBitDepth); |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 392 | |
| 393 | initFrameInfo(&mImageInfo, videoFrame); |
| 394 | |
| 395 | if (videoFrame->mTileHeight >= 512) { |
| 396 | // Try decoding in slices only if the image has tiles and is big enough. |
| 397 | mSliceHeight = videoFrame->mTileHeight; |
| 398 | ALOGV("mSliceHeight %u", mSliceHeight); |
| 399 | } |
| 400 | |
| 401 | defaultInfo = &mImageInfo; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 404 | if (mHasVideo) { |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 405 | sp<IMemory> sharedMem = retriever->getFrameAtTime(0, |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 406 | MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC, |
| 407 | mOutputColor, true /*metaOnly*/); |
| 408 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 409 | if (sharedMem == nullptr || sharedMem->unsecurePointer() == nullptr) { |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 410 | ALOGE("init: videoFrame is a nullptr"); |
| 411 | return false; |
| 412 | } |
| 413 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 414 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 415 | // (see declaration for details). |
| 416 | // Either document why it is safe in this case or address the |
| 417 | // issue (e.g. by copying). |
| 418 | VideoFrame* videoFrame = static_cast<VideoFrame*>( |
| 419 | sharedMem->unsecurePointer()); |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 420 | |
| 421 | ALOGV("Sequence dimension %dx%d, display %dx%d, angle %d, iccSize %d", |
| 422 | videoFrame->mWidth, |
| 423 | videoFrame->mHeight, |
| 424 | videoFrame->mDisplayWidth, |
| 425 | videoFrame->mDisplayHeight, |
| 426 | videoFrame->mRotationAngle, |
| 427 | videoFrame->mIccSize); |
| 428 | |
| 429 | initFrameInfo(&mSequenceInfo, videoFrame); |
| 430 | |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 431 | const char* frameCount = retriever->extractMetadata(METADATA_KEY_VIDEO_FRAME_COUNT); |
Ray Essick | e89e632 | 2022-02-02 13:33:50 -0800 | [diff] [blame] | 432 | if (frameCount == nullptr) { |
| 433 | android_errorWriteWithInfoLog(0x534e4554, "215002587", -1, NULL, 0); |
| 434 | ALOGD("No valid sequence information in metadata"); |
| 435 | return false; |
| 436 | } |
| 437 | mSequenceLength = atoi(frameCount); |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 438 | |
| 439 | if (defaultInfo == nullptr) { |
| 440 | defaultInfo = &mSequenceInfo; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | if (defaultInfo == nullptr) { |
| 445 | ALOGD("No valid image or sequence available"); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 446 | return false; |
| 447 | } |
| 448 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 449 | if (frameInfo != nullptr) { |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 450 | *frameInfo = *defaultInfo; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 451 | } |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 452 | |
| 453 | // default total scanline, this might change if decodeSequence() is used |
| 454 | mTotalScanline = defaultInfo->mHeight; |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | bool HeifDecoderImpl::getSequenceInfo( |
| 460 | HeifFrameInfo* frameInfo, size_t *frameCount) { |
| 461 | ALOGV("%s", __FUNCTION__); |
| 462 | if (!mHasVideo) { |
| 463 | return false; |
| 464 | } |
| 465 | if (frameInfo != nullptr) { |
| 466 | *frameInfo = mSequenceInfo; |
| 467 | } |
| 468 | if (frameCount != nullptr) { |
| 469 | *frameCount = mSequenceLength; |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 470 | } |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 471 | return true; |
| 472 | } |
| 473 | |
| 474 | bool HeifDecoderImpl::getEncodedColor(HeifEncodedColor* /*outColor*/) const { |
| 475 | ALOGW("getEncodedColor: not implemented!"); |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | bool HeifDecoderImpl::setOutputColor(HeifColorFormat heifColor) { |
Liz Kammer | 3ec5a0b | 2021-07-21 09:30:07 -0400 | [diff] [blame] | 480 | if (heifColor == (HeifColorFormat)mOutputColor) { |
Leon Scroggins III | 7ca678b | 2020-01-22 14:09:55 -0500 | [diff] [blame] | 481 | return true; |
| 482 | } |
| 483 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 484 | switch(heifColor) { |
| 485 | case kHeifColorFormat_RGB565: |
| 486 | { |
| 487 | mOutputColor = HAL_PIXEL_FORMAT_RGB_565; |
Leon Scroggins III | 7ca678b | 2020-01-22 14:09:55 -0500 | [diff] [blame] | 488 | break; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 489 | } |
| 490 | case kHeifColorFormat_RGBA_8888: |
| 491 | { |
| 492 | mOutputColor = HAL_PIXEL_FORMAT_RGBA_8888; |
Leon Scroggins III | 7ca678b | 2020-01-22 14:09:55 -0500 | [diff] [blame] | 493 | break; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 494 | } |
| 495 | case kHeifColorFormat_BGRA_8888: |
| 496 | { |
| 497 | mOutputColor = HAL_PIXEL_FORMAT_BGRA_8888; |
Leon Scroggins III | 7ca678b | 2020-01-22 14:09:55 -0500 | [diff] [blame] | 498 | break; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 499 | } |
Dichen Zhang | 1b3441a | 2021-11-17 11:54:43 -0800 | [diff] [blame] | 500 | case kHeifColorFormat_RGBA_1010102: |
| 501 | { |
| 502 | mOutputColor = HAL_PIXEL_FORMAT_RGBA_1010102; |
| 503 | break; |
| 504 | } |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 505 | default: |
Leon Scroggins III | 7ca678b | 2020-01-22 14:09:55 -0500 | [diff] [blame] | 506 | ALOGE("Unsupported output color format %d", heifColor); |
| 507 | return false; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 508 | } |
Leon Scroggins III | 7ca678b | 2020-01-22 14:09:55 -0500 | [diff] [blame] | 509 | |
| 510 | if (mFrameDecoded) { |
| 511 | return reinit(nullptr); |
| 512 | } |
| 513 | return true; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 516 | bool HeifDecoderImpl::decodeAsync() { |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 517 | wp<MediaMetadataRetriever> weakRetriever; |
| 518 | { |
| 519 | Mutex::Autolock _l(mRetrieverLock); |
| 520 | weakRetriever = mRetriever; |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 523 | for (size_t i = 1; i < mNumSlices; i++) { |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 524 | sp<MediaMetadataRetriever> retriever = weakRetriever.promote(); |
| 525 | if (retriever == nullptr) { |
| 526 | return false; |
| 527 | } |
| 528 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 529 | ALOGV("decodeAsync(): decoding slice %zu", i); |
| 530 | size_t top = i * mSliceHeight; |
| 531 | size_t bottom = (i + 1) * mSliceHeight; |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 532 | if (bottom > mImageInfo.mHeight) { |
| 533 | bottom = mImageInfo.mHeight; |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 534 | } |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 535 | |
| 536 | sp<IMemory> frameMemory = retriever->getImageRectAtIndex( |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 537 | -1, mOutputColor, 0, top, mImageInfo.mWidth, bottom); |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 538 | { |
| 539 | Mutex::Autolock autolock(mLock); |
| 540 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 541 | if (frameMemory == nullptr || frameMemory->unsecurePointer() == nullptr) { |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 542 | mAsyncDecodeDone = true; |
| 543 | mScanlineReady.signal(); |
| 544 | break; |
| 545 | } |
| 546 | mFrameMemory = frameMemory; |
| 547 | mAvailableLines = bottom; |
| 548 | ALOGV("decodeAsync(): available lines %zu", mAvailableLines); |
| 549 | mScanlineReady.signal(); |
| 550 | } |
| 551 | } |
Leon Scroggins III | 7ca678b | 2020-01-22 14:09:55 -0500 | [diff] [blame] | 552 | // Hold on to mDataSource in case the client wants to redecode. |
Dichen Zhang | f823003 | 2022-12-01 18:39:23 -0800 | [diff] [blame^] | 553 | |
| 554 | { |
| 555 | Mutex::Autolock _l(mRetrieverLock); |
| 556 | mRetriever.clear(); |
| 557 | } |
| 558 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 559 | return false; |
| 560 | } |
| 561 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 562 | bool HeifDecoderImpl::decode(HeifFrameInfo* frameInfo) { |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 563 | // reset scanline pointer |
| 564 | mCurScanline = 0; |
| 565 | |
| 566 | if (mFrameDecoded) { |
| 567 | return true; |
| 568 | } |
| 569 | |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 570 | sp<MediaMetadataRetriever> retriever; |
| 571 | { |
| 572 | Mutex::Autolock _l(mRetrieverLock); |
| 573 | if (mRetriever == nullptr) { |
| 574 | ALOGE("Failed to get MediaMetadataRetriever!"); |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | retriever = mRetriever; |
| 579 | } |
| 580 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 581 | // See if we want to decode in slices to allow client to start |
| 582 | // scanline processing in parallel with decode. If this fails |
| 583 | // we fallback to decoding the full frame. |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 584 | if (mHasImage) { |
| 585 | if (mSliceHeight >= 512 && |
| 586 | mImageInfo.mWidth >= 3000 && |
| 587 | mImageInfo.mHeight >= 2000 ) { |
| 588 | // Try decoding in slices only if the image has tiles and is big enough. |
| 589 | mNumSlices = (mImageInfo.mHeight + mSliceHeight - 1) / mSliceHeight; |
| 590 | ALOGV("mSliceHeight %u, mNumSlices %zu", mSliceHeight, mNumSlices); |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 593 | if (mNumSlices > 1) { |
| 594 | // get first slice and metadata |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 595 | sp<IMemory> frameMemory = retriever->getImageRectAtIndex( |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 596 | -1, mOutputColor, 0, 0, mImageInfo.mWidth, mSliceHeight); |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 597 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 598 | if (frameMemory == nullptr || frameMemory->unsecurePointer() == nullptr) { |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 599 | ALOGE("decode: metadata is a nullptr"); |
| 600 | return false; |
| 601 | } |
| 602 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 603 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 604 | // (see declaration for details). |
| 605 | // Either document why it is safe in this case or address the |
| 606 | // issue (e.g. by copying). |
| 607 | VideoFrame* videoFrame = static_cast<VideoFrame*>(frameMemory->unsecurePointer()); |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 608 | |
| 609 | if (frameInfo != nullptr) { |
| 610 | initFrameInfo(frameInfo, videoFrame); |
| 611 | } |
| 612 | mFrameMemory = frameMemory; |
| 613 | mAvailableLines = mSliceHeight; |
| 614 | mThread = new DecodeThread(this); |
| 615 | if (mThread->run("HeifDecode", ANDROID_PRIORITY_FOREGROUND) == OK) { |
| 616 | mFrameDecoded = true; |
| 617 | return true; |
| 618 | } |
| 619 | // Fallback to decode without slicing |
| 620 | mThread.clear(); |
| 621 | mNumSlices = 1; |
| 622 | mSliceHeight = 0; |
| 623 | mAvailableLines = 0; |
| 624 | mFrameMemory.clear(); |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 625 | } |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 628 | if (mHasImage) { |
| 629 | // image index < 0 to retrieve primary image |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 630 | mFrameMemory = retriever->getImageAtIndex(-1, mOutputColor); |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 631 | } else if (mHasVideo) { |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 632 | mFrameMemory = retriever->getFrameAtTime(0, |
Chong Zhang | d3e0d86 | 2017-10-03 13:17:13 -0700 | [diff] [blame] | 633 | MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC, mOutputColor); |
| 634 | } |
| 635 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 636 | if (mFrameMemory == nullptr || mFrameMemory->unsecurePointer() == nullptr) { |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 637 | ALOGE("decode: videoFrame is a nullptr"); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 638 | return false; |
| 639 | } |
| 640 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 641 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 642 | // (see declaration for details). |
| 643 | // Either document why it is safe in this case or address the |
| 644 | // issue (e.g. by copying). |
| 645 | VideoFrame* videoFrame = static_cast<VideoFrame*>(mFrameMemory->unsecurePointer()); |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 646 | if (videoFrame->mSize == 0 || |
| 647 | mFrameMemory->size() < videoFrame->getFlattenedSize()) { |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 648 | ALOGE("decode: videoFrame size is invalid"); |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 649 | return false; |
| 650 | } |
| 651 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 652 | ALOGV("Decoded dimension %dx%d, display %dx%d, angle %d, rowbytes %d, size %d", |
| 653 | videoFrame->mWidth, |
| 654 | videoFrame->mHeight, |
| 655 | videoFrame->mDisplayWidth, |
| 656 | videoFrame->mDisplayHeight, |
| 657 | videoFrame->mRotationAngle, |
| 658 | videoFrame->mRowBytes, |
| 659 | videoFrame->mSize); |
| 660 | |
| 661 | if (frameInfo != nullptr) { |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 662 | initFrameInfo(frameInfo, videoFrame); |
| 663 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 664 | } |
Chong Zhang | 3f3ffab | 2017-08-23 13:51:59 -0700 | [diff] [blame] | 665 | mFrameDecoded = true; |
Chong Zhang | 4c6398b | 2017-09-07 17:43:19 -0700 | [diff] [blame] | 666 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 667 | // Aggressively clear to avoid holding on to resources |
Dichen Zhang | f823003 | 2022-12-01 18:39:23 -0800 | [diff] [blame^] | 668 | { |
| 669 | Mutex::Autolock _l(mRetrieverLock); |
| 670 | mRetriever.clear(); |
| 671 | } |
Leon Scroggins III | 7ca678b | 2020-01-22 14:09:55 -0500 | [diff] [blame] | 672 | |
| 673 | // Hold on to mDataSource in case the client wants to redecode. |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 674 | return true; |
| 675 | } |
| 676 | |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 677 | bool HeifDecoderImpl::decodeSequence(int frameIndex, HeifFrameInfo* frameInfo) { |
| 678 | ALOGV("%s: frame index %d", __FUNCTION__, frameIndex); |
| 679 | if (!mHasVideo) { |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | if (frameIndex < 0 || frameIndex >= mSequenceLength) { |
| 684 | ALOGE("invalid frame index: %d, total frames %zu", frameIndex, mSequenceLength); |
| 685 | return false; |
| 686 | } |
| 687 | |
| 688 | mCurScanline = 0; |
| 689 | |
| 690 | // set total scanline to sequence height now |
| 691 | mTotalScanline = mSequenceInfo.mHeight; |
| 692 | |
Dichen Zhang | 92e5107 | 2022-06-08 18:26:37 -0700 | [diff] [blame] | 693 | sp<MediaMetadataRetriever> retriever; |
| 694 | { |
| 695 | Mutex::Autolock _l(mRetrieverLock); |
| 696 | retriever = mRetriever; |
| 697 | if (retriever == nullptr) { |
| 698 | ALOGE("failed to get MediaMetadataRetriever!"); |
| 699 | return false; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | mFrameMemory = retriever->getFrameAtIndex(frameIndex, mOutputColor); |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 704 | if (mFrameMemory == nullptr || mFrameMemory->unsecurePointer() == nullptr) { |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 705 | ALOGE("decode: videoFrame is a nullptr"); |
| 706 | return false; |
| 707 | } |
| 708 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 709 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 710 | // (see declaration for details). |
| 711 | // Either document why it is safe in this case or address the |
| 712 | // issue (e.g. by copying). |
| 713 | VideoFrame* videoFrame = static_cast<VideoFrame*>(mFrameMemory->unsecurePointer()); |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 714 | if (videoFrame->mSize == 0 || |
| 715 | mFrameMemory->size() < videoFrame->getFlattenedSize()) { |
| 716 | ALOGE("decode: videoFrame size is invalid"); |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | ALOGV("Decoded dimension %dx%d, display %dx%d, angle %d, rowbytes %d, size %d", |
| 721 | videoFrame->mWidth, |
| 722 | videoFrame->mHeight, |
| 723 | videoFrame->mDisplayWidth, |
| 724 | videoFrame->mDisplayHeight, |
| 725 | videoFrame->mRotationAngle, |
| 726 | videoFrame->mRowBytes, |
| 727 | videoFrame->mSize); |
| 728 | |
| 729 | if (frameInfo != nullptr) { |
| 730 | initFrameInfo(frameInfo, videoFrame); |
| 731 | } |
| 732 | return true; |
| 733 | } |
| 734 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 735 | bool HeifDecoderImpl::getScanlineInner(uint8_t* dst) { |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 736 | if (mFrameMemory == nullptr || mFrameMemory->unsecurePointer() == nullptr) { |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 737 | return false; |
| 738 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 739 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 740 | // (see declaration for details). |
| 741 | // Either document why it is safe in this case or address the |
| 742 | // issue (e.g. by copying). |
| 743 | VideoFrame* videoFrame = static_cast<VideoFrame*>(mFrameMemory->unsecurePointer()); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 744 | uint8_t* src = videoFrame->getFlattenedData() + videoFrame->mRowBytes * mCurScanline++; |
Chong Zhang | 70b0afd | 2018-02-27 12:43:06 -0800 | [diff] [blame] | 745 | memcpy(dst, src, videoFrame->mBytesPerPixel * videoFrame->mWidth); |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 746 | return true; |
| 747 | } |
| 748 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 749 | bool HeifDecoderImpl::getScanline(uint8_t* dst) { |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 750 | if (mCurScanline >= mTotalScanline) { |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 751 | ALOGE("no more scanline available"); |
| 752 | return false; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 753 | } |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 754 | |
Chong Zhang | 0c1407f | 2018-05-02 17:09:05 -0700 | [diff] [blame] | 755 | if (mNumSlices > 1) { |
| 756 | Mutex::Autolock autolock(mLock); |
| 757 | |
| 758 | while (!mAsyncDecodeDone && mCurScanline >= mAvailableLines) { |
| 759 | mScanlineReady.wait(mLock); |
| 760 | } |
| 761 | return (mCurScanline < mAvailableLines) ? getScanlineInner(dst) : false; |
| 762 | } |
| 763 | |
| 764 | return getScanlineInner(dst); |
| 765 | } |
| 766 | |
| 767 | size_t HeifDecoderImpl::skipScanlines(size_t count) { |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 768 | uint32_t oldScanline = mCurScanline; |
| 769 | mCurScanline += count; |
Chong Zhang | 8541d30 | 2019-07-12 11:19:47 -0700 | [diff] [blame] | 770 | if (mCurScanline > mTotalScanline) { |
| 771 | mCurScanline = mTotalScanline; |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 772 | } |
| 773 | return (mCurScanline > oldScanline) ? (mCurScanline - oldScanline) : 0; |
| 774 | } |
| 775 | |
Dichen Zhang | f406649 | 2022-02-17 17:32:53 -0800 | [diff] [blame] | 776 | uint32_t HeifDecoderImpl::getColorDepth() { |
| 777 | HeifFrameInfo* info = &mImageInfo; |
| 778 | if (info != nullptr) { |
| 779 | return mImageInfo.mBitDepth; |
| 780 | } else { |
| 781 | return 0; |
| 782 | } |
| 783 | } |
| 784 | |
Chong Zhang | ea280cb | 2017-08-02 11:10:10 -0700 | [diff] [blame] | 785 | } // namespace android |