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);
197 // Set the flag so that webp information can be reloaded when visual is re-attached to scene.
198 mLoadSucceeded = false;
201 // Moveable but not copyable
203 Impl(const Impl&) = delete;
204 Impl& operator=(const Impl&) = delete;
205 Impl(Impl&&) = default;
206 Impl& operator=(Impl&&) = default;
214 std::vector<uint32_t> mTimeStamp;
215 int32_t mLatestLoadedFrame{INITIAL_INDEX};
216 uint32_t mFrameCount;
218 // For the case the system doesn't support DALI_ANIMATED_WEBP_ENABLED
219 unsigned char* mBuffer;
220 uint32_t mBufferSize;
221 ImageDimensions mImageSize;
223 bool mIsLocalResource;
225 #ifdef DALI_ANIMATED_WEBP_ENABLED
226 WebPData mWebPData{0};
227 WebPAnimDecoder* mWebPAnimDecoder{nullptr};
228 WebPAnimInfo mWebPAnimInfo{0};
229 Dali::Devel::PixelBuffer mPreLoadedFrame{};
233 AnimatedImageLoadingPtr WebPLoading::New(const std::string& url, bool isLocalResource)
235 #ifndef DALI_ANIMATED_WEBP_ENABLED
236 DALI_LOG_ERROR("The system does not support Animated WebP format.\n");
238 return AnimatedImageLoadingPtr(new WebPLoading(url, isLocalResource));
241 WebPLoading::WebPLoading(const std::string& url, bool isLocalResource)
242 : mImpl(new WebPLoading::Impl(url, isLocalResource))
246 WebPLoading::~WebPLoading()
251 bool WebPLoading::LoadNextNFrames(uint32_t frameStartIndex, int count, std::vector<Dali::PixelData>& pixelData)
253 for(int i = 0; i < count; ++i)
255 Dali::Devel::PixelBuffer pixelBuffer = LoadFrame((frameStartIndex + i) % mImpl->mFrameCount);
261 Dali::PixelData imageData = Devel::PixelBuffer::Convert(pixelBuffer);
262 pixelData.push_back(imageData);
264 if(pixelData.size() != static_cast<uint32_t>(count))
271 Dali::Devel::PixelBuffer WebPLoading::LoadFrame(uint32_t frameIndex)
273 Dali::Devel::PixelBuffer pixelBuffer;
275 // If WebP file is still not loaded, Load the information.
276 if(!mImpl->mLoadSucceeded)
278 if(!mImpl->LoadWebPInformation())
284 // WebPDecodeRGBA is faster than to use demux API for loading non-animated image.
285 // If frame count is 1, use WebPDecodeRGBA api.
286 #ifdef DALI_WEBP_AVAILABLE
287 if(mImpl->mFrameCount == 1)
289 int32_t width, height;
290 if(!WebPGetInfo(mImpl->mBuffer, mImpl->mBufferSize, &width, &height))
295 WebPBitstreamFeatures features;
296 if(VP8_STATUS_NOT_ENOUGH_DATA == WebPGetFeatures(mImpl->mBuffer, mImpl->mBufferSize, &features))
301 uint32_t channelNumber = (features.has_alpha) ? 4 : 3;
302 Pixel::Format pixelFormat = (channelNumber == 4) ? Pixel::RGBA8888 : Pixel::RGB888;
303 pixelBuffer = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
304 uint8_t* frameBuffer = nullptr;
305 if(channelNumber == 4)
307 frameBuffer = WebPDecodeRGBA(mImpl->mBuffer, mImpl->mBufferSize, &width, &height);
311 frameBuffer = WebPDecodeRGB(mImpl->mBuffer, mImpl->mBufferSize, &width, &height);
314 if(frameBuffer != nullptr)
316 const int32_t imageBufferSize = width * height * sizeof(uint8_t) * channelNumber;
317 memcpy(pixelBuffer.GetBuffer(), frameBuffer, imageBufferSize);
318 free((void*)frameBuffer);
320 mImpl->ReleaseResource();
326 #ifdef DALI_ANIMATED_WEBP_ENABLED
327 Mutex::ScopedLock lock(mImpl->mMutex);
328 if(frameIndex >= mImpl->mWebPAnimInfo.frame_count || !mImpl->mLoadSucceeded)
333 DALI_LOG_INFO(gWebPLoadingLogFilter, Debug::Concise, "LoadFrame( frameIndex:%d )\n", frameIndex);
335 if(mImpl->mPreLoadedFrame && mImpl->mLatestLoadedFrame == static_cast<int32_t>(frameIndex))
337 pixelBuffer = mImpl->mPreLoadedFrame;
341 pixelBuffer = DecodeFrame(frameIndex);
343 mImpl->mPreLoadedFrame.Reset();
345 // If time stamp of next frame is unknown, load a frame more to know it.
346 if(frameIndex + 1 < mImpl->mWebPAnimInfo.frame_count && mImpl->mTimeStamp[frameIndex + 1] == 0u)
348 mImpl->mPreLoadedFrame = DecodeFrame(frameIndex + 1);
355 Dali::Devel::PixelBuffer WebPLoading::DecodeFrame(uint32_t frameIndex)
357 Dali::Devel::PixelBuffer pixelBuffer;
358 #ifdef DALI_ANIMATED_WEBP_ENABLED
359 if(mImpl->mLatestLoadedFrame >= static_cast<int32_t>(frameIndex))
361 mImpl->mLatestLoadedFrame = INITIAL_INDEX;
362 WebPAnimDecoderReset(mImpl->mWebPAnimDecoder);
366 uint8_t* frameBuffer = nullptr;
367 for(; mImpl->mLatestLoadedFrame < static_cast<int32_t>(frameIndex);)
369 WebPAnimDecoderGetNext(mImpl->mWebPAnimDecoder, &frameBuffer, ×tamp);
370 mImpl->mTimeStamp[++mImpl->mLatestLoadedFrame] = timestamp;
373 if(frameBuffer != nullptr)
375 const int bufferSize = mImpl->mWebPAnimInfo.canvas_width * mImpl->mWebPAnimInfo.canvas_height * sizeof(uint32_t);
376 pixelBuffer = Dali::Devel::PixelBuffer::New(mImpl->mWebPAnimInfo.canvas_width, mImpl->mWebPAnimInfo.canvas_height, Dali::Pixel::RGBA8888);
377 memcpy(pixelBuffer.GetBuffer(), frameBuffer, bufferSize);
384 ImageDimensions WebPLoading::GetImageSize() const
386 if(!mImpl->mLoadSucceeded)
388 DALI_LOG_ERROR("WebP file is still not loaded, this image size could not be correct value.\n");
390 return mImpl->mImageSize;
393 uint32_t WebPLoading::GetImageCount() const
395 if(!mImpl->mLoadSucceeded)
397 DALI_LOG_ERROR("WebP file is still not loaded, this image count could not be correct value.\n");
399 return mImpl->mFrameCount;
402 uint32_t WebPLoading::GetFrameInterval(uint32_t frameIndex) const
404 if(!mImpl->mLoadSucceeded)
406 DALI_LOG_ERROR("WebP file is still not loaded, this frame interval could not be correct value.\n");
408 if(frameIndex >= GetImageCount())
410 DALI_LOG_ERROR("Input frameIndex exceeded frame count of the WebP.");
415 int32_t interval = 0u;
416 if(GetImageCount() == 1u)
420 else if(frameIndex + 1 == GetImageCount())
422 // For the interval between last frame and first frame, use last interval again.
423 interval = mImpl->mTimeStamp[frameIndex] - mImpl->mTimeStamp[frameIndex - 1];
427 interval = mImpl->mTimeStamp[frameIndex + 1] - mImpl->mTimeStamp[frameIndex];
434 return static_cast<uint32_t>(interval);
438 std::string WebPLoading::GetUrl() const
443 bool WebPLoading::HasLoadingSucceeded() const
445 return mImpl->mLoadSucceeded;
448 } // namespace Adaptor
450 } // namespace Internal