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