2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/imaging/common/webp-loading.h>
22 #ifdef DALI_WEBP_AVAILABLE
23 #include <webp/decode.h>
24 #include <webp/demux.h>
26 #if WEBP_DEMUX_ABI_VERSION > 0x0101
27 #define DALI_ANIMATED_WEBP_ENABLED 1
31 #include <dali/integration-api/debug.h>
32 #include <dali/public-api/images/pixel-data.h>
34 #include <dali/devel-api/threading/mutex.h>
35 #include <dali/internal/imaging/common/file-download.h>
36 #include <dali/internal/system/common/file-reader.h>
39 #include <sys/types.h>
44 #include <dali/devel-api/adaptor-framework/image-loading.h>
45 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
47 typedef uint8_t WebPByteType;
57 #if defined(DEBUG_ENABLED)
58 Debug::Filter* gWebPLoadingLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_GIF_LOADING");
61 static constexpr int32_t INITIAL_INDEX = -1;
62 static constexpr size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE = 50 * 1024 * 1024;
66 struct WebPLoading::Impl
69 Impl(const std::string& url, bool isLocalResource)
77 mLoadSucceeded(false),
78 mIsAnimatedImage(false),
79 mIsLocalResource(isLocalResource)
91 mLoadSucceeded(false),
92 mIsAnimatedImage(false),
93 mIsLocalResource(true)
97 bool LoadWebPInformation()
99 // Block to do not load this file again.
100 Mutex::ScopedLock lock(mMutex);
101 if(DALI_UNLIKELY(mLoadSucceeded))
103 return mLoadSucceeded;
106 #ifndef DALI_WEBP_AVAILABLE
107 // If the system doesn't support webp, loading will be failed.
109 mLoadSucceeded = false;
110 return mLoadSucceeded;
113 // mFrameCount will be 1 if the input image is non-animated image or animated image with single frame.
114 if(DALI_LIKELY(ReadWebPInformation()))
116 #ifdef DALI_WEBP_AVAILABLE
117 WebPDataInit(&mWebPData);
118 mWebPData.size = mBufferSize;
119 mWebPData.bytes = mBuffer;
121 WebPDemuxer* demuxer = WebPDemux(&mWebPData);
122 uint32_t flags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS);
123 if(flags & ANIMATION_FLAG)
125 mIsAnimatedImage = true;
127 WebPDemuxDelete(demuxer);
129 if(!mIsAnimatedImage)
131 int32_t imageWidth, imageHeight;
132 if(WebPGetInfo(mBuffer, mBufferSize, &imageWidth, &imageHeight))
134 mImageSize = ImageDimensions(imageWidth, imageHeight);
138 #ifdef DALI_ANIMATED_WEBP_ENABLED
141 WebPAnimDecoderOptions webPAnimDecoderOptions;
142 WebPAnimDecoderOptionsInit(&webPAnimDecoderOptions);
143 webPAnimDecoderOptions.color_mode = MODE_RGBA;
144 mWebPAnimDecoder = WebPAnimDecoderNew(&mWebPData, &webPAnimDecoderOptions);
145 WebPAnimDecoderGetInfo(mWebPAnimDecoder, &mWebPAnimInfo);
146 mTimeStamp.assign(mWebPAnimInfo.frame_count, 0);
147 mFrameCount = mWebPAnimInfo.frame_count;
148 mImageSize = ImageDimensions(mWebPAnimInfo.canvas_width, mWebPAnimInfo.canvas_height);
151 mLoadSucceeded = true;
156 mLoadSucceeded = false;
157 DALI_LOG_ERROR("Image loading failed for: \"%s\".\n", mUrl.c_str());
160 return mLoadSucceeded;
163 bool ReadWebPInformation()
168 std::unique_ptr<Internal::Platform::FileReader> fileReader;
169 Dali::Vector<uint8_t> dataBuffer;
174 fileReader = std::make_unique<Internal::Platform::FileReader>(mUrl);
179 if(DALI_LIKELY(TizenPlatform::Network::DownloadRemoteFileIntoMemory(mUrl, dataBuffer, dataSize, MAXIMUM_DOWNLOAD_IMAGE_SIZE)))
181 mBufferSize = dataBuffer.Size();
182 if(DALI_LIKELY(mBufferSize > 0U))
184 // Open a file handle on the memory buffer:
185 fileReader = std::make_unique<Internal::Platform::FileReader>(dataBuffer, mBufferSize);
192 fp = fileReader->GetFile();
196 if(DALI_LIKELY(fp != nullptr))
200 if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END) <= -1))
204 mBufferSize = ftell(fp);
207 if(DALI_LIKELY(!fseek(fp, 0, SEEK_SET)))
209 mBuffer = reinterpret_cast<WebPByteType*>(malloc(sizeof(WebPByteType) * mBufferSize));
210 mBufferSize = fread(mBuffer, sizeof(WebPByteType), mBufferSize, fp);
217 void ReleaseResource()
219 #ifdef DALI_WEBP_AVAILABLE
220 mWebPData.bytes = nullptr;
221 WebPDataInit(&mWebPData);
223 #ifdef DALI_ANIMATED_WEBP_ENABLED
226 if(mWebPAnimDecoder != nullptr)
228 WebPAnimDecoderDelete(mWebPAnimDecoder);
229 mWebPAnimDecoder = nullptr;
233 if(mBuffer != nullptr)
235 free((void*)mBuffer);
239 mLoadSucceeded = false;
242 // Moveable but not copyable
244 Impl(const Impl&) = delete;
245 Impl& operator=(const Impl&) = delete;
246 Impl(Impl&&) = default;
247 Impl& operator=(Impl&&) = default;
256 std::vector<uint32_t> mTimeStamp;
257 int32_t mLatestLoadedFrame{INITIAL_INDEX};
258 uint32_t mFrameCount;
260 // For the case the system doesn't support DALI_ANIMATED_WEBP_ENABLED
261 unsigned char* mBuffer;
262 uint32_t mBufferSize;
263 ImageDimensions mImageSize;
265 bool mIsAnimatedImage;
266 bool mIsLocalResource;
268 #ifdef DALI_WEBP_AVAILABLE
269 WebPData mWebPData{0};
272 #ifdef DALI_ANIMATED_WEBP_ENABLED
273 WebPAnimDecoder* mWebPAnimDecoder{nullptr};
274 WebPAnimInfo mWebPAnimInfo{0};
275 Dali::Devel::PixelBuffer mPreLoadedFrame{};
279 AnimatedImageLoadingPtr WebPLoading::New(const std::string& url, bool isLocalResource)
281 #ifndef DALI_ANIMATED_WEBP_ENABLED
282 DALI_LOG_ERROR("The system does not support Animated WebP format.\n");
284 return AnimatedImageLoadingPtr(new WebPLoading(url, isLocalResource));
287 AnimatedImageLoadingPtr WebPLoading::New(FILE* const fp)
289 #ifndef DALI_ANIMATED_WEBP_ENABLED
290 DALI_LOG_ERROR("The system does not support Animated WebP format.\n");
292 return AnimatedImageLoadingPtr(new WebPLoading(fp));
295 WebPLoading::WebPLoading(const std::string& url, bool isLocalResource)
296 : mImpl(new WebPLoading::Impl(url, isLocalResource))
300 WebPLoading::WebPLoading(FILE* const fp)
301 : mImpl(new WebPLoading::Impl(fp))
305 WebPLoading::~WebPLoading()
310 Dali::Devel::PixelBuffer WebPLoading::LoadFrame(uint32_t frameIndex, ImageDimensions desiredSize)
312 Dali::Devel::PixelBuffer pixelBuffer;
314 // If WebP file is still not loaded, Load the information.
315 if(DALI_UNLIKELY(!mImpl->mLoadSucceeded))
317 if(DALI_UNLIKELY(!mImpl->LoadWebPInformation()))
319 mImpl->ReleaseResource();
324 // WebPDecodeRGBA is faster than to use demux API for loading non-animated image.
325 // If frame count is 1, use WebPDecodeRGBA api.
326 #ifdef DALI_WEBP_AVAILABLE
327 if(!mImpl->mIsAnimatedImage)
329 int32_t width, height;
330 if(DALI_LIKELY(WebPGetInfo(mImpl->mBuffer, mImpl->mBufferSize, &width, &height)))
332 WebPBitstreamFeatures features;
333 if(DALI_LIKELY(VP8_STATUS_NOT_ENOUGH_DATA != WebPGetFeatures(mImpl->mBuffer, mImpl->mBufferSize, &features)))
335 uint32_t channelNumber = (features.has_alpha) ? 4 : 3;
336 uint8_t* frameBuffer = nullptr;
338 if(desiredSize.GetWidth() > 0 && desiredSize.GetHeight() > 0)
340 const int desiredWidth = desiredSize.GetWidth();
341 const int desiredHeight = desiredSize.GetHeight();
343 WebPDecoderConfig config;
344 if(!DALI_LIKELY(WebPInitDecoderConfig(&config)))
346 DALI_LOG_ERROR("WebPInitDecoderConfig is failed");
350 // Apply config for scaling
351 config.options.use_scaling = 1;
352 config.options.scaled_width = desiredWidth;
353 config.options.scaled_height = desiredHeight;
355 if(channelNumber == 4)
357 config.output.colorspace = MODE_RGBA;
361 config.output.colorspace = MODE_RGB;
364 if(WebPDecode(mImpl->mBuffer, mImpl->mBufferSize, &config) == VP8_STATUS_OK)
366 frameBuffer = config.output.u.RGBA.rgba;
370 DALI_LOG_ERROR("Webp Decoding with scaled size is failed \n");
373 if(frameBuffer != nullptr)
375 Pixel::Format pixelFormat = (channelNumber == 4) ? Pixel::RGBA8888 : Pixel::RGB888;
376 int32_t bufferSize = desiredWidth * desiredHeight * Dali::Pixel::GetBytesPerPixel(pixelFormat);
377 pixelBuffer = Dali::Devel::PixelBuffer::New(desiredWidth, desiredHeight, pixelFormat);
378 memcpy(pixelBuffer.GetBuffer(), frameBuffer, bufferSize);
381 WebPFreeDecBuffer(&config.output);
385 if(channelNumber == 4)
387 frameBuffer = WebPDecodeRGBA(mImpl->mBuffer, mImpl->mBufferSize, &width, &height);
391 frameBuffer = WebPDecodeRGB(mImpl->mBuffer, mImpl->mBufferSize, &width, &height);
394 if(frameBuffer != nullptr)
396 Pixel::Format pixelFormat = (channelNumber == 4) ? Pixel::RGBA8888 : Pixel::RGB888;
397 int32_t bufferSize = width * height * Dali::Pixel::GetBytesPerPixel(pixelFormat);
398 Internal::Adaptor::PixelBufferPtr internal =
399 Internal::Adaptor::PixelBuffer::New(frameBuffer, bufferSize, width, height, width, pixelFormat);
400 pixelBuffer = Devel::PixelBuffer(internal.Get());
405 // The single frame resource should be released after loading.
406 mImpl->ReleaseResource();
410 #ifdef DALI_ANIMATED_WEBP_ENABLED
411 if(mImpl->mIsAnimatedImage && mImpl->mBuffer != nullptr)
413 Mutex::ScopedLock lock(mImpl->mMutex);
414 if(DALI_LIKELY(frameIndex < mImpl->mWebPAnimInfo.frame_count && mImpl->mLoadSucceeded))
416 DALI_LOG_INFO(gWebPLoadingLogFilter, Debug::Concise, "LoadFrame( frameIndex:%d )\n", frameIndex);
418 if(mImpl->mPreLoadedFrame && mImpl->mLatestLoadedFrame == static_cast<int32_t>(frameIndex))
420 pixelBuffer = mImpl->mPreLoadedFrame;
424 pixelBuffer = DecodeFrame(frameIndex);
426 mImpl->mPreLoadedFrame.Reset();
428 // If time stamp of next frame is unknown, load a frame more to know it.
429 if(frameIndex + 1 < mImpl->mWebPAnimInfo.frame_count && mImpl->mTimeStamp[frameIndex + 1] == 0u)
431 mImpl->mPreLoadedFrame = DecodeFrame(frameIndex + 1);
436 mImpl->ReleaseResource();
443 Dali::Devel::PixelBuffer WebPLoading::DecodeFrame(uint32_t frameIndex)
445 Dali::Devel::PixelBuffer pixelBuffer;
446 #ifdef DALI_ANIMATED_WEBP_ENABLED
447 if(mImpl->mLatestLoadedFrame >= static_cast<int32_t>(frameIndex))
449 mImpl->mLatestLoadedFrame = INITIAL_INDEX;
450 WebPAnimDecoderReset(mImpl->mWebPAnimDecoder);
453 uint8_t* frameBuffer = nullptr;
454 int32_t timestamp = 0u;
455 for(; mImpl->mLatestLoadedFrame < static_cast<int32_t>(frameIndex);)
457 WebPAnimDecoderGetNext(mImpl->mWebPAnimDecoder, &frameBuffer, ×tamp);
458 mImpl->mTimeStamp[++mImpl->mLatestLoadedFrame] = timestamp;
461 if(frameBuffer != nullptr)
463 const int bufferSize = mImpl->mWebPAnimInfo.canvas_width * mImpl->mWebPAnimInfo.canvas_height * sizeof(uint32_t);
464 pixelBuffer = Dali::Devel::PixelBuffer::New(mImpl->mWebPAnimInfo.canvas_width, mImpl->mWebPAnimInfo.canvas_height, Dali::Pixel::RGBA8888);
465 memcpy(pixelBuffer.GetBuffer(), frameBuffer, bufferSize);
472 ImageDimensions WebPLoading::GetImageSize() const
474 if(DALI_UNLIKELY(!mImpl->mLoadSucceeded))
476 mImpl->LoadWebPInformation();
478 return mImpl->mImageSize;
481 uint32_t WebPLoading::GetImageCount() const
483 if(DALI_UNLIKELY(!mImpl->mLoadSucceeded))
485 mImpl->LoadWebPInformation();
487 return mImpl->mFrameCount;
490 uint32_t WebPLoading::GetFrameInterval(uint32_t frameIndex) const
492 if(DALI_UNLIKELY(!mImpl->mLoadSucceeded))
494 DALI_LOG_ERROR("WebP file is still not loaded, this frame interval could not be correct value.\n");
496 if(frameIndex >= GetImageCount())
498 DALI_LOG_ERROR("Input frameIndex exceeded frame count of the WebP.");
503 int32_t interval = 0u;
504 if(GetImageCount() == 1u)
508 else if(frameIndex + 1 == GetImageCount())
510 // For the interval between last frame and first frame, use last interval again.
511 interval = mImpl->mTimeStamp[frameIndex] - mImpl->mTimeStamp[frameIndex - 1];
515 interval = mImpl->mTimeStamp[frameIndex + 1] - mImpl->mTimeStamp[frameIndex];
518 if(DALI_UNLIKELY(interval < 0))
520 DALI_LOG_ERROR("This interval value is not correct, because the frame still hasn't ever been decoded.");
523 return static_cast<uint32_t>(interval);
527 std::string WebPLoading::GetUrl() const
532 bool WebPLoading::HasLoadingSucceeded() const
534 return mImpl->mLoadSucceeded;
537 } // namespace Adaptor
539 } // namespace Internal