blob: b80f4b45723374980e0d2450aed7cb4e02ceade3 [file] [log] [blame]
Chong Zhangea280cb2017-08-02 11:10:10 -07001/*
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 Kang49ce6712018-01-24 10:16:25 -080025#include <binder/MemoryDealer.h>
Chong Zhangea280cb2017-08-02 11:10:10 -070026#include <drm/drm_framework_common.h>
27#include <media/IDataSource.h>
28#include <media/mediametadataretriever.h>
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070029#include <media/MediaSource.h>
Chong Zhangd3e0d862017-10-03 13:17:13 -070030#include <media/stagefright/foundation/ADebug.h>
Chong Zhangea280cb2017-08-02 11:10:10 -070031#include <private/media/VideoFrame.h>
32#include <utils/Log.h>
33#include <utils/RefBase.h>
Chong Zhang8541d302019-07-12 11:19:47 -070034#include <vector>
Chong Zhangea280cb2017-08-02 11:10:10 -070035
36HeifDecoder* createHeifDecoder() {
37 return new android::HeifDecoderImpl();
38}
39
40namespace android {
41
Chong Zhang8541d302019-07-12 11:19:47 -070042void 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 Zhang0af4db62019-08-23 15:34:58 -070047 info->mDurationUs = videoFrame->mDurationUs;
Chong Zhang8541d302019-07-12 11:19:47 -070048 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 Zhangea280cb2017-08-02 11:10:10 -070058/*
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 */
64class HeifDataSource : public BnDataSource {
65public:
66 /*
67 * Constructs HeifDataSource; will take ownership of |stream|.
68 */
69 HeifDataSource(HeifStream* stream)
Chong Zhang3f3ffab2017-08-23 13:51:59 -070070 : mStream(stream), mEOS(false),
71 mCachedOffset(0), mCachedSize(0), mCacheBufferSize(0) {}
Chong Zhangea280cb2017-08-02 11:10:10 -070072
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 Zhangea280cb2017-08-02 11:10:10 -070086
87private:
Chong Zhangea280cb2017-08-02 11:10:10 -070088 enum {
Chong Zhang3f3ffab2017-08-23 13:51:59 -070089 /*
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 Zhangea280cb2017-08-02 11:10:10 -070093 kBufferSize = 64 * 1024,
Chong Zhang3f3ffab2017-08-23 13:51:59 -070094 /*
95 * Initial and max cache buffer size.
96 */
97 kInitialCacheBufferSize = 4 * 1024 * 1024,
98 kMaxCacheBufferSize = 64 * 1024 * 1024,
Chong Zhangea280cb2017-08-02 11:10:10 -070099 };
100 sp<IMemory> mMemory;
101 std::unique_ptr<HeifStream> mStream;
Chong Zhangea280cb2017-08-02 11:10:10 -0700102 bool mEOS;
Chong Zhangcd031922018-08-24 13:03:19 -0700103 std::unique_ptr<uint8_t[]> mCache;
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700104 off64_t mCachedOffset;
105 size_t mCachedSize;
106 size_t mCacheBufferSize;
Chong Zhangea280cb2017-08-02 11:10:10 -0700107};
108
109bool 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 Zhang3f3ffab2017-08-23 13:51:59 -0700117 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 Zhangea280cb2017-08-02 11:10:10 -0700123 return true;
124}
125
126ssize_t HeifDataSource::readAt(off64_t offset, size_t size) {
127 ALOGV("readAt: offset=%lld, size=%zu", (long long)offset, size);
128
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700129 if (offset < mCachedOffset) {
Chong Zhangea280cb2017-08-02 11:10:10 -0700130 // 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 Zhang3f3ffab2017-08-23 13:51:59 -0700133 mCachedOffset = offset;
134 mCachedSize = 0;
Chong Zhangea280cb2017-08-02 11:10:10 -0700135 mEOS = false;
136 } else if (mStream->rewind()) {
137 ALOGV("readAt: rewind to offset=0");
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700138 mCachedOffset = 0;
139 mCachedSize = 0;
Chong Zhangea280cb2017-08-02 11:10:10 -0700140 mEOS = false;
141 } else {
142 ALOGE("readAt: couldn't seek or rewind!");
143 mEOS = true;
144 }
145 }
146
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700147 if (mEOS && (offset < mCachedOffset ||
148 offset >= (off64_t)(mCachedOffset + mCachedSize))) {
Chong Zhangea280cb2017-08-02 11:10:10 -0700149 ALOGV("readAt: EOS");
150 return ERROR_END_OF_STREAM;
151 }
152
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700153 // at this point, offset must be >= mCachedOffset, other cases should
154 // have been caught above.
155 CHECK(offset >= mCachedOffset);
156
Sungtak Lee237f9032018-03-05 15:21:33 -0800157 off64_t resultOffset;
158 if (__builtin_add_overflow(offset, size, &resultOffset)) {
159 return ERROR_IO;
160 }
161
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700162 if (size == 0) {
163 return 0;
Chong Zhangea280cb2017-08-02 11:10:10 -0700164 }
165
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700166 // Can only read max of kBufferSize
Chong Zhangea280cb2017-08-02 11:10:10 -0700167 if (size > kBufferSize) {
168 size = kBufferSize;
169 }
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700170
171 // copy from cache if the request falls entirely in cache
172 if (offset + size <= mCachedOffset + mCachedSize) {
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700173 memcpy(mMemory->unsecurePointer(), mCache.get() + offset - mCachedOffset, size);
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700174 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 Zhangcd031922018-08-24 13:03:19 -0700182 std::unique_ptr<uint8_t[]> newCache;
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700183 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 Zhangea280cb2017-08-02 11:10:10 -0700252 // bytesRead is invalid
253 mEOS = true;
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700254 bytesRead = 0;
255 } else if (bytesRead < bytesToRead) {
256 // read some bytes but not all, set EOS
Chong Zhangea280cb2017-08-02 11:10:10 -0700257 mEOS = true;
258 }
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700259 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-Tsvi7dd39722019-09-05 15:14:30 -0700271 memcpy(mMemory->unsecurePointer(), mCache.get() + offset - mCachedOffset, size);
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700272 return size;
Chong Zhangea280cb2017-08-02 11:10:10 -0700273}
274
275status_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 Zhang0c1407f2018-05-02 17:09:05 -0700288struct HeifDecoderImpl::DecodeThread : public Thread {
289 explicit DecodeThread(HeifDecoderImpl *decoder) : mDecoder(decoder) {}
290
291private:
292 HeifDecoderImpl* mDecoder;
293
294 bool threadLoop();
295
296 DISALLOW_EVIL_CONSTRUCTORS(DecodeThread);
297};
298
299bool HeifDecoderImpl::DecodeThread::threadLoop() {
300 return mDecoder->decodeAsync();
301}
302
303/////////////////////////////////////////////////////////////////////////
304
Chong Zhangea280cb2017-08-02 11:10:10 -0700305HeifDecoderImpl::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 Zhang3f3ffab2017-08-23 13:51:59 -0700309 mCurScanline(0),
Chong Zhang8541d302019-07-12 11:19:47 -0700310 mTotalScanline(0),
Chong Zhangd3e0d862017-10-03 13:17:13 -0700311 mFrameDecoded(false),
312 mHasImage(false),
Chong Zhang0c1407f2018-05-02 17:09:05 -0700313 mHasVideo(false),
Chong Zhang8541d302019-07-12 11:19:47 -0700314 mSequenceLength(0),
Chong Zhang0c1407f2018-05-02 17:09:05 -0700315 mAvailableLines(0),
316 mNumSlices(1),
317 mSliceHeight(0),
318 mAsyncDecodeDone(false) {
Chong Zhangea280cb2017-08-02 11:10:10 -0700319}
320
321HeifDecoderImpl::~HeifDecoderImpl() {
Chong Zhang0c1407f2018-05-02 17:09:05 -0700322 if (mThread != nullptr) {
323 mThread->join();
324 }
Chong Zhangea280cb2017-08-02 11:10:10 -0700325}
326
327bool HeifDecoderImpl::init(HeifStream* stream, HeifFrameInfo* frameInfo) {
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700328 mFrameDecoded = false;
Chong Zhangd3e0d862017-10-03 13:17:13 -0700329 mFrameMemory.clear();
330
Chong Zhangea280cb2017-08-02 11:10:10 -0700331 sp<HeifDataSource> dataSource = new HeifDataSource(stream);
332 if (!dataSource->init()) {
333 return false;
334 }
335 mDataSource = dataSource;
336
337 mRetriever = new MediaMetadataRetriever();
Chong Zhangd3e0d862017-10-03 13:17:13 -0700338 status_t err = mRetriever->setDataSource(mDataSource, "image/heif");
Chong Zhangea280cb2017-08-02 11:10:10 -0700339 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 Zhangd3e0d862017-10-03 13:17:13 -0700348 const char* hasImage = mRetriever->extractMetadata(METADATA_KEY_HAS_IMAGE);
Chong Zhangea280cb2017-08-02 11:10:10 -0700349 const char* hasVideo = mRetriever->extractMetadata(METADATA_KEY_HAS_VIDEO);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700350
351 mHasImage = hasImage && !strcasecmp(hasImage, "yes");
352 mHasVideo = hasVideo && !strcasecmp(hasVideo, "yes");
Chong Zhang8541d302019-07-12 11:19:47 -0700353
354 HeifFrameInfo* defaultInfo = nullptr;
Chong Zhangd3e0d862017-10-03 13:17:13 -0700355 if (mHasImage) {
356 // image index < 0 to retrieve primary image
Chong Zhang8541d302019-07-12 11:19:47 -0700357 sp<IMemory> sharedMem = mRetriever->getImageAtIndex(
Chong Zhangd3e0d862017-10-03 13:17:13 -0700358 -1, mOutputColor, true /*metaOnly*/);
Chong Zhang8541d302019-07-12 11:19:47 -0700359
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700360 if (sharedMem == nullptr || sharedMem->unsecurePointer() == nullptr) {
Chong Zhang8541d302019-07-12 11:19:47 -0700361 ALOGE("init: videoFrame is a nullptr");
362 return false;
363 }
364
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700365 // 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 Zhang8541d302019-07-12 11:19:47 -0700370
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 Zhangea280cb2017-08-02 11:10:10 -0700388 }
389
Chong Zhang8541d302019-07-12 11:19:47 -0700390 if (mHasVideo) {
391 sp<IMemory> sharedMem = mRetriever->getFrameAtTime(0,
392 MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC,
393 mOutputColor, true /*metaOnly*/);
394
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700395 if (sharedMem == nullptr || sharedMem->unsecurePointer() == nullptr) {
Chong Zhang8541d302019-07-12 11:19:47 -0700396 ALOGE("init: videoFrame is a nullptr");
397 return false;
398 }
399
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700400 // 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 Zhang8541d302019-07-12 11:19:47 -0700406
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 Zhangea280cb2017-08-02 11:10:10 -0700426 return false;
427 }
428
Chong Zhangea280cb2017-08-02 11:10:10 -0700429 if (frameInfo != nullptr) {
Chong Zhang8541d302019-07-12 11:19:47 -0700430 *frameInfo = *defaultInfo;
Chong Zhangea280cb2017-08-02 11:10:10 -0700431 }
Chong Zhang8541d302019-07-12 11:19:47 -0700432
433 // default total scanline, this might change if decodeSequence() is used
434 mTotalScanline = defaultInfo->mHeight;
435
436 return true;
437}
438
439bool 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 Zhang0c1407f2018-05-02 17:09:05 -0700450 }
Chong Zhangea280cb2017-08-02 11:10:10 -0700451 return true;
452}
453
454bool HeifDecoderImpl::getEncodedColor(HeifEncodedColor* /*outColor*/) const {
455 ALOGW("getEncodedColor: not implemented!");
456 return false;
457}
458
459bool 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 Zhang0c1407f2018-05-02 17:09:05 -0700483bool 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 Zhang8541d302019-07-12 11:19:47 -0700488 if (bottom > mImageInfo.mHeight) {
489 bottom = mImageInfo.mHeight;
Chong Zhang0c1407f2018-05-02 17:09:05 -0700490 }
491 sp<IMemory> frameMemory = mRetriever->getImageRectAtIndex(
Chong Zhang8541d302019-07-12 11:19:47 -0700492 -1, mOutputColor, 0, top, mImageInfo.mWidth, bottom);
Chong Zhang0c1407f2018-05-02 17:09:05 -0700493 {
494 Mutex::Autolock autolock(mLock);
495
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700496 if (frameMemory == nullptr || frameMemory->unsecurePointer() == nullptr) {
Chong Zhang0c1407f2018-05-02 17:09:05 -0700497 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 Zhangea280cb2017-08-02 11:10:10 -0700513bool HeifDecoderImpl::decode(HeifFrameInfo* frameInfo) {
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700514 // reset scanline pointer
515 mCurScanline = 0;
516
517 if (mFrameDecoded) {
518 return true;
519 }
520
Chong Zhang0c1407f2018-05-02 17:09:05 -0700521 // 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 Zhang8541d302019-07-12 11:19:47 -0700524 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 Zhang0c1407f2018-05-02 17:09:05 -0700531 }
532
Chong Zhang8541d302019-07-12 11:19:47 -0700533 if (mNumSlices > 1) {
534 // get first slice and metadata
535 sp<IMemory> frameMemory = mRetriever->getImageRectAtIndex(
536 -1, mOutputColor, 0, 0, mImageInfo.mWidth, mSliceHeight);
Chong Zhang0c1407f2018-05-02 17:09:05 -0700537
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700538 if (frameMemory == nullptr || frameMemory->unsecurePointer() == nullptr) {
Chong Zhang8541d302019-07-12 11:19:47 -0700539 ALOGE("decode: metadata is a nullptr");
540 return false;
541 }
542
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700543 // 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 Zhang8541d302019-07-12 11:19:47 -0700548
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 Zhang0c1407f2018-05-02 17:09:05 -0700565 }
Chong Zhang0c1407f2018-05-02 17:09:05 -0700566 }
567
Chong Zhangd3e0d862017-10-03 13:17:13 -0700568 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-Tsvi7dd39722019-09-05 15:14:30 -0700576 if (mFrameMemory == nullptr || mFrameMemory->unsecurePointer() == nullptr) {
Chong Zhang0c1407f2018-05-02 17:09:05 -0700577 ALOGE("decode: videoFrame is a nullptr");
Chong Zhangea280cb2017-08-02 11:10:10 -0700578 return false;
579 }
580
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700581 // 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 Zhang3f3ffab2017-08-23 13:51:59 -0700586 if (videoFrame->mSize == 0 ||
587 mFrameMemory->size() < videoFrame->getFlattenedSize()) {
Chong Zhang0c1407f2018-05-02 17:09:05 -0700588 ALOGE("decode: videoFrame size is invalid");
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700589 return false;
590 }
591
Chong Zhangea280cb2017-08-02 11:10:10 -0700592 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 Zhang8541d302019-07-12 11:19:47 -0700602 initFrameInfo(frameInfo, videoFrame);
603
Chong Zhangea280cb2017-08-02 11:10:10 -0700604 }
Chong Zhang3f3ffab2017-08-23 13:51:59 -0700605 mFrameDecoded = true;
Chong Zhang4c6398b2017-09-07 17:43:19 -0700606
Chong Zhang0c1407f2018-05-02 17:09:05 -0700607 // Aggressively clear to avoid holding on to resources
Chong Zhang4c6398b2017-09-07 17:43:19 -0700608 mRetriever.clear();
609 mDataSource.clear();
Chong Zhangea280cb2017-08-02 11:10:10 -0700610 return true;
611}
612
Chong Zhang8541d302019-07-12 11:19:47 -0700613bool 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-Tsvi7dd39722019-09-05 15:14:30 -0700630 if (mFrameMemory == nullptr || mFrameMemory->unsecurePointer() == nullptr) {
Chong Zhang8541d302019-07-12 11:19:47 -0700631 ALOGE("decode: videoFrame is a nullptr");
632 return false;
633 }
634
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700635 // 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 Zhang8541d302019-07-12 11:19:47 -0700640 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 Zhang0c1407f2018-05-02 17:09:05 -0700661bool HeifDecoderImpl::getScanlineInner(uint8_t* dst) {
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700662 if (mFrameMemory == nullptr || mFrameMemory->unsecurePointer() == nullptr) {
Chong Zhangea280cb2017-08-02 11:10:10 -0700663 return false;
664 }
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700665 // 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 Zhangea280cb2017-08-02 11:10:10 -0700670 uint8_t* src = videoFrame->getFlattenedData() + videoFrame->mRowBytes * mCurScanline++;
Chong Zhang70b0afd2018-02-27 12:43:06 -0800671 memcpy(dst, src, videoFrame->mBytesPerPixel * videoFrame->mWidth);
Chong Zhangea280cb2017-08-02 11:10:10 -0700672 return true;
673}
674
Chong Zhang0c1407f2018-05-02 17:09:05 -0700675bool HeifDecoderImpl::getScanline(uint8_t* dst) {
Chong Zhang8541d302019-07-12 11:19:47 -0700676 if (mCurScanline >= mTotalScanline) {
Chong Zhang0c1407f2018-05-02 17:09:05 -0700677 ALOGE("no more scanline available");
678 return false;
Chong Zhangea280cb2017-08-02 11:10:10 -0700679 }
Chong Zhangea280cb2017-08-02 11:10:10 -0700680
Chong Zhang0c1407f2018-05-02 17:09:05 -0700681 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
693size_t HeifDecoderImpl::skipScanlines(size_t count) {
Chong Zhangea280cb2017-08-02 11:10:10 -0700694 uint32_t oldScanline = mCurScanline;
695 mCurScanline += count;
Chong Zhang8541d302019-07-12 11:19:47 -0700696 if (mCurScanline > mTotalScanline) {
697 mCurScanline = mTotalScanline;
Chong Zhangea280cb2017-08-02 11:10:10 -0700698 }
699 return (mCurScanline > oldScanline) ? (mCurScanline - oldScanline) : 0;
700}
701
702} // namespace android