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