2 * Copyright (c) 2021 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>
46 typedef unsigned char WebPByteType;
56 #if defined(DEBUG_ENABLED)
57 Debug::Filter* gWebPLoadingLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_GIF_LOADING");
60 static constexpr int32_t INITIAL_INDEX = -1;
61 static constexpr size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE = 50 * 1024 * 1024;
65 struct WebPLoading::Impl
68 Impl(const std::string& url, bool isLocalResource)
75 mLoadSucceeded(false),
76 mIsLocalResource(isLocalResource)
80 bool LoadWebPInformation()
82 // mFrameCount will be 1 if the input image is non-animated image or animated image with single frame.
83 if(ReadWebPInformation())
85 mLoadSucceeded = true;
86 #ifdef DALI_ANIMATED_WEBP_ENABLED
87 WebPDataInit(&mWebPData);
88 mWebPData.size = mBufferSize;
89 mWebPData.bytes = mBuffer;
90 WebPAnimDecoderOptions webPAnimDecoderOptions;
91 WebPAnimDecoderOptionsInit(&webPAnimDecoderOptions);
92 webPAnimDecoderOptions.color_mode = MODE_RGBA;
93 mWebPAnimDecoder = WebPAnimDecoderNew(&mWebPData, &webPAnimDecoderOptions);
94 WebPAnimDecoderGetInfo(mWebPAnimDecoder, &mWebPAnimInfo);
95 mTimeStamp.assign(mWebPAnimInfo.frame_count, 0);
96 mFrameCount = mWebPAnimInfo.frame_count;
97 mImageSize = ImageDimensions(mWebPAnimInfo.canvas_width, mWebPAnimInfo.canvas_height);
98 #elif DALI_WEBP_AVAILABLE
99 int32_t imageWidth, imageHeight;
100 if(WebPGetInfo(mBuffer, mBufferSize, &imageWidth, &imageHeight))
102 mImageSize = ImageDimensions(imageWidth, imageHeight);
105 #ifndef DALI_WEBP_AVAILABLE
106 // If the system doesn't support webp, loading will be failed.
108 mLoadSucceeded = false;
114 mLoadSucceeded = false;
115 DALI_LOG_ERROR("Image loading failed for: \"%s\".\n", mUrl.c_str());
118 return mLoadSucceeded;
121 bool ReadWebPInformation()
126 Internal::Platform::FileReader fileReader(mUrl);
127 fp = fileReader.GetFile();
133 if(fseek(fp, 0, SEEK_END) <= -1)
138 mBufferSize = ftell(fp);
139 if(!fseek(fp, 0, SEEK_SET))
141 mBuffer = reinterpret_cast<WebPByteType*>(malloc(sizeof(WebPByteType) * mBufferSize));
142 mBufferSize = fread(mBuffer, sizeof(WebPByteType), mBufferSize, fp);
150 Dali::Vector<uint8_t> dataBuffer;
153 succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory(mUrl, dataBuffer, dataSize, MAXIMUM_DOWNLOAD_IMAGE_SIZE);
156 mBufferSize = dataBuffer.Size();
159 // Open a file handle on the memory buffer:
160 Internal::Platform::FileReader fileReader(dataBuffer, mBufferSize);
161 fp = fileReader.GetFile();
164 if(!fseek(fp, 0, SEEK_SET))
166 mBuffer = reinterpret_cast<WebPByteType*>(malloc(sizeof(WebPByteType) * mBufferSize));
167 mBufferSize = fread(mBuffer, sizeof(WebPByteType), mBufferSize, fp);
177 void ReleaseResource()
179 #ifdef DALI_ANIMATED_WEBP_ENABLED
180 if(&mWebPData != nullptr)
182 mWebPData.bytes = nullptr;
183 WebPDataInit(&mWebPData);
185 if(mWebPAnimDecoder != nullptr)
187 WebPAnimDecoderDelete(mWebPAnimDecoder);
188 mWebPAnimDecoder = nullptr;
191 if(mBuffer != nullptr)
193 free((void*)mBuffer);
198 // Moveable but not copyable
200 Impl(const Impl&) = delete;
201 Impl& operator=(const Impl&) = delete;
202 Impl(Impl&&) = default;
203 Impl& operator=(Impl&&) = default;
211 std::vector<uint32_t> mTimeStamp;
212 int32_t mLatestLoadedFrame{INITIAL_INDEX};
213 uint32_t mFrameCount;
215 // For the case the system doesn't support DALI_ANIMATED_WEBP_ENABLED
216 unsigned char* mBuffer;
217 uint32_t mBufferSize;
218 ImageDimensions mImageSize;
220 bool mIsLocalResource;
222 #ifdef DALI_ANIMATED_WEBP_ENABLED
223 WebPData mWebPData{0};
224 WebPAnimDecoder* mWebPAnimDecoder{nullptr};
225 WebPAnimInfo mWebPAnimInfo{0};
226 Dali::Devel::PixelBuffer mPreLoadedFrame{};
230 AnimatedImageLoadingPtr WebPLoading::New(const std::string& url, bool isLocalResource)
232 #ifndef DALI_ANIMATED_WEBP_ENABLED
233 DALI_LOG_ERROR("The system does not support Animated WebP format.\n");
235 return AnimatedImageLoadingPtr(new WebPLoading(url, isLocalResource));
238 WebPLoading::WebPLoading(const std::string& url, bool isLocalResource)
239 : mImpl(new WebPLoading::Impl(url, isLocalResource))
243 WebPLoading::~WebPLoading()
248 bool WebPLoading::LoadNextNFrames(uint32_t frameStartIndex, int count, std::vector<Dali::PixelData>& pixelData)
250 for(int i = 0; i < count; ++i)
252 Dali::Devel::PixelBuffer pixelBuffer = LoadFrame((frameStartIndex + i) % mImpl->mFrameCount);
258 Dali::PixelData imageData = Devel::PixelBuffer::Convert(pixelBuffer);
259 pixelData.push_back(imageData);
261 if(pixelData.size() != static_cast<uint32_t>(count))
268 Dali::Devel::PixelBuffer WebPLoading::LoadFrame(uint32_t frameIndex)
270 Dali::Devel::PixelBuffer pixelBuffer;
272 // If WebP file is still not loaded, Load the information.
273 if(!mImpl->mLoadSucceeded)
275 if(!mImpl->LoadWebPInformation())
281 // WebPDecodeRGBA is faster than to use demux API for loading non-animated image.
282 // If frame count is 1, use WebPDecodeRGBA api.
283 #ifdef DALI_WEBP_AVAILABLE
284 if(mImpl->mFrameCount == 1)
286 int32_t width, height;
287 if(!WebPGetInfo(mImpl->mBuffer, mImpl->mBufferSize, &width, &height))
292 WebPBitstreamFeatures features;
293 if(VP8_STATUS_NOT_ENOUGH_DATA == WebPGetFeatures(mImpl->mBuffer, mImpl->mBufferSize, &features))
298 uint32_t channelNumber = (features.has_alpha) ? 4 : 3;
299 Pixel::Format pixelFormat = (channelNumber == 4) ? Pixel::RGBA8888 : Pixel::RGB888;
300 pixelBuffer = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
301 uint8_t* frameBuffer = nullptr;
302 if(channelNumber == 4)
304 frameBuffer = WebPDecodeRGBA(mImpl->mBuffer, mImpl->mBufferSize, &width, &height);
308 frameBuffer = WebPDecodeRGB(mImpl->mBuffer, mImpl->mBufferSize, &width, &height);
311 if(frameBuffer != nullptr)
313 const int32_t imageBufferSize = width * height * sizeof(uint8_t) * channelNumber;
314 memcpy(pixelBuffer.GetBuffer(), frameBuffer, imageBufferSize);
315 free((void*)frameBuffer);
317 mImpl->ReleaseResource();
322 #ifdef DALI_ANIMATED_WEBP_ENABLED
323 Mutex::ScopedLock lock(mImpl->mMutex);
324 if(frameIndex >= mImpl->mWebPAnimInfo.frame_count || !mImpl->mLoadSucceeded)
329 DALI_LOG_INFO(gWebPLoadingLogFilter, Debug::Concise, "LoadFrame( frameIndex:%d )\n", frameIndex);
331 if(mImpl->mPreLoadedFrame && mImpl->mLatestLoadedFrame == static_cast<int32_t>(frameIndex))
333 pixelBuffer = mImpl->mPreLoadedFrame;
337 pixelBuffer = DecodeFrame(frameIndex);
339 mImpl->mPreLoadedFrame.Reset();
341 // If time stamp of next frame is unknown, load a frame more to know it.
342 if(frameIndex + 1 < mImpl->mWebPAnimInfo.frame_count && mImpl->mTimeStamp[frameIndex + 1] == 0u)
344 mImpl->mPreLoadedFrame = DecodeFrame(frameIndex + 1);
351 Dali::Devel::PixelBuffer WebPLoading::DecodeFrame(uint32_t frameIndex)
353 Dali::Devel::PixelBuffer pixelBuffer;
354 #ifdef DALI_ANIMATED_WEBP_ENABLED
355 if(mImpl->mLatestLoadedFrame > static_cast<int32_t>(frameIndex))
357 mImpl->mLatestLoadedFrame = INITIAL_INDEX;
358 WebPAnimDecoderReset(mImpl->mWebPAnimDecoder);
361 uint8_t* frameBuffer;
363 for(; mImpl->mLatestLoadedFrame < static_cast<int32_t>(frameIndex);)
365 WebPAnimDecoderGetNext(mImpl->mWebPAnimDecoder, &frameBuffer, ×tamp);
366 mImpl->mTimeStamp[++mImpl->mLatestLoadedFrame] = timestamp;
368 const int bufferSize = mImpl->mWebPAnimInfo.canvas_width * mImpl->mWebPAnimInfo.canvas_height * sizeof(uint32_t);
369 pixelBuffer = Dali::Devel::PixelBuffer::New(mImpl->mWebPAnimInfo.canvas_width, mImpl->mWebPAnimInfo.canvas_height, Dali::Pixel::RGBA8888);
370 memcpy(pixelBuffer.GetBuffer(), frameBuffer, bufferSize);
376 ImageDimensions WebPLoading::GetImageSize() const
378 if(!mImpl->mLoadSucceeded)
380 DALI_LOG_ERROR("WebP file is still not loaded, this image size could not be correct value.\n");
382 return mImpl->mImageSize;
385 uint32_t WebPLoading::GetImageCount() const
387 if(!mImpl->mLoadSucceeded)
389 DALI_LOG_ERROR("WebP file is still not loaded, this image count could not be correct value.\n");
391 return mImpl->mFrameCount;
394 uint32_t WebPLoading::GetFrameInterval(uint32_t frameIndex) const
396 if(!mImpl->mLoadSucceeded)
398 DALI_LOG_ERROR("WebP file is still not loaded, this frame interval could not be correct value.\n");
400 if(frameIndex >= GetImageCount())
402 DALI_LOG_ERROR("Input frameIndex exceeded frame count of the WebP.");
407 int32_t interval = 0u;
408 if(GetImageCount() == 1u)
412 else if(frameIndex + 1 == GetImageCount())
414 // For the interval between last frame and first frame, use last interval again.
415 interval = mImpl->mTimeStamp[frameIndex] - mImpl->mTimeStamp[frameIndex - 1];
419 interval = mImpl->mTimeStamp[frameIndex + 1] - mImpl->mTimeStamp[frameIndex];
426 return static_cast<uint32_t>(interval);
430 std::string WebPLoading::GetUrl() const
435 bool WebPLoading::HasLoadingSucceeded() const
437 return mImpl->mLoadSucceeded;
440 } // namespace Adaptor
442 } // namespace Internal