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