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