xref: /aosp_15_r20/external/webp/src/demux/anim_decode.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2015 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li //  AnimDecoder implementation.
11*b2055c35SXin Li //
12*b2055c35SXin Li 
13*b2055c35SXin Li #ifdef HAVE_CONFIG_H
14*b2055c35SXin Li #include "src/webp/config.h"
15*b2055c35SXin Li #endif
16*b2055c35SXin Li 
17*b2055c35SXin Li #include <assert.h>
18*b2055c35SXin Li #include <string.h>
19*b2055c35SXin Li 
20*b2055c35SXin Li #include "src/utils/utils.h"
21*b2055c35SXin Li #include "src/webp/decode.h"
22*b2055c35SXin Li #include "src/webp/demux.h"
23*b2055c35SXin Li #include "src/webp/types.h"
24*b2055c35SXin Li 
25*b2055c35SXin Li #define NUM_CHANNELS 4
26*b2055c35SXin Li 
27*b2055c35SXin Li // Channel extraction from a uint32_t representation of a uint8_t RGBA/BGRA
28*b2055c35SXin Li // buffer.
29*b2055c35SXin Li #ifdef WORDS_BIGENDIAN
30*b2055c35SXin Li #define CHANNEL_SHIFT(i) (24 - (i) * 8)
31*b2055c35SXin Li #else
32*b2055c35SXin Li #define CHANNEL_SHIFT(i) ((i) * 8)
33*b2055c35SXin Li #endif
34*b2055c35SXin Li 
35*b2055c35SXin Li typedef void (*BlendRowFunc)(uint32_t* const, const uint32_t* const, int);
36*b2055c35SXin Li static void BlendPixelRowNonPremult(uint32_t* const src,
37*b2055c35SXin Li                                     const uint32_t* const dst, int num_pixels);
38*b2055c35SXin Li static void BlendPixelRowPremult(uint32_t* const src, const uint32_t* const dst,
39*b2055c35SXin Li                                  int num_pixels);
40*b2055c35SXin Li 
41*b2055c35SXin Li struct WebPAnimDecoder {
42*b2055c35SXin Li   WebPDemuxer* demux_;             // Demuxer created from given WebP bitstream.
43*b2055c35SXin Li   WebPDecoderConfig config_;       // Decoder config.
44*b2055c35SXin Li   // Note: we use a pointer to a function blending multiple pixels at a time to
45*b2055c35SXin Li   // allow possible inlining of per-pixel blending function.
46*b2055c35SXin Li   BlendRowFunc blend_func_;        // Pointer to the chose blend row function.
47*b2055c35SXin Li   WebPAnimInfo info_;              // Global info about the animation.
48*b2055c35SXin Li   uint8_t* curr_frame_;            // Current canvas (not disposed).
49*b2055c35SXin Li   uint8_t* prev_frame_disposed_;   // Previous canvas (properly disposed).
50*b2055c35SXin Li   int prev_frame_timestamp_;       // Previous frame timestamp (milliseconds).
51*b2055c35SXin Li   WebPIterator prev_iter_;         // Iterator object for previous frame.
52*b2055c35SXin Li   int prev_frame_was_keyframe_;    // True if previous frame was a keyframe.
53*b2055c35SXin Li   int next_frame_;                 // Index of the next frame to be decoded
54*b2055c35SXin Li                                    // (starting from 1).
55*b2055c35SXin Li };
56*b2055c35SXin Li 
DefaultDecoderOptions(WebPAnimDecoderOptions * const dec_options)57*b2055c35SXin Li static void DefaultDecoderOptions(WebPAnimDecoderOptions* const dec_options) {
58*b2055c35SXin Li   dec_options->color_mode = MODE_RGBA;
59*b2055c35SXin Li   dec_options->use_threads = 0;
60*b2055c35SXin Li }
61*b2055c35SXin Li 
WebPAnimDecoderOptionsInitInternal(WebPAnimDecoderOptions * dec_options,int abi_version)62*b2055c35SXin Li int WebPAnimDecoderOptionsInitInternal(WebPAnimDecoderOptions* dec_options,
63*b2055c35SXin Li                                        int abi_version) {
64*b2055c35SXin Li   if (dec_options == NULL ||
65*b2055c35SXin Li       WEBP_ABI_IS_INCOMPATIBLE(abi_version, WEBP_DEMUX_ABI_VERSION)) {
66*b2055c35SXin Li     return 0;
67*b2055c35SXin Li   }
68*b2055c35SXin Li   DefaultDecoderOptions(dec_options);
69*b2055c35SXin Li   return 1;
70*b2055c35SXin Li }
71*b2055c35SXin Li 
ApplyDecoderOptions(const WebPAnimDecoderOptions * const dec_options,WebPAnimDecoder * const dec)72*b2055c35SXin Li WEBP_NODISCARD static int ApplyDecoderOptions(
73*b2055c35SXin Li     const WebPAnimDecoderOptions* const dec_options,
74*b2055c35SXin Li     WebPAnimDecoder* const dec) {
75*b2055c35SXin Li   WEBP_CSP_MODE mode;
76*b2055c35SXin Li   WebPDecoderConfig* config = &dec->config_;
77*b2055c35SXin Li   assert(dec_options != NULL);
78*b2055c35SXin Li 
79*b2055c35SXin Li   mode = dec_options->color_mode;
80*b2055c35SXin Li   if (mode != MODE_RGBA && mode != MODE_BGRA &&
81*b2055c35SXin Li       mode != MODE_rgbA && mode != MODE_bgrA) {
82*b2055c35SXin Li     return 0;
83*b2055c35SXin Li   }
84*b2055c35SXin Li   dec->blend_func_ = (mode == MODE_RGBA || mode == MODE_BGRA)
85*b2055c35SXin Li                          ? &BlendPixelRowNonPremult
86*b2055c35SXin Li                          : &BlendPixelRowPremult;
87*b2055c35SXin Li   if (!WebPInitDecoderConfig(config)) {
88*b2055c35SXin Li     return 0;
89*b2055c35SXin Li   }
90*b2055c35SXin Li   config->output.colorspace = mode;
91*b2055c35SXin Li   config->output.is_external_memory = 1;
92*b2055c35SXin Li   config->options.use_threads = dec_options->use_threads;
93*b2055c35SXin Li   // Note: config->output.u.RGBA is set at the time of decoding each frame.
94*b2055c35SXin Li   return 1;
95*b2055c35SXin Li }
96*b2055c35SXin Li 
WebPAnimDecoderNewInternal(const WebPData * webp_data,const WebPAnimDecoderOptions * dec_options,int abi_version)97*b2055c35SXin Li WebPAnimDecoder* WebPAnimDecoderNewInternal(
98*b2055c35SXin Li     const WebPData* webp_data, const WebPAnimDecoderOptions* dec_options,
99*b2055c35SXin Li     int abi_version) {
100*b2055c35SXin Li   WebPAnimDecoderOptions options;
101*b2055c35SXin Li   WebPAnimDecoder* dec = NULL;
102*b2055c35SXin Li   WebPBitstreamFeatures features;
103*b2055c35SXin Li   if (webp_data == NULL ||
104*b2055c35SXin Li       WEBP_ABI_IS_INCOMPATIBLE(abi_version, WEBP_DEMUX_ABI_VERSION)) {
105*b2055c35SXin Li     return NULL;
106*b2055c35SXin Li   }
107*b2055c35SXin Li 
108*b2055c35SXin Li   // Validate the bitstream before doing expensive allocations. The demuxer may
109*b2055c35SXin Li   // be more tolerant than the decoder.
110*b2055c35SXin Li   if (WebPGetFeatures(webp_data->bytes, webp_data->size, &features) !=
111*b2055c35SXin Li       VP8_STATUS_OK) {
112*b2055c35SXin Li     return NULL;
113*b2055c35SXin Li   }
114*b2055c35SXin Li 
115*b2055c35SXin Li   // Note: calloc() so that the pointer members are initialized to NULL.
116*b2055c35SXin Li   dec = (WebPAnimDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
117*b2055c35SXin Li   if (dec == NULL) goto Error;
118*b2055c35SXin Li 
119*b2055c35SXin Li   if (dec_options != NULL) {
120*b2055c35SXin Li     options = *dec_options;
121*b2055c35SXin Li   } else {
122*b2055c35SXin Li     DefaultDecoderOptions(&options);
123*b2055c35SXin Li   }
124*b2055c35SXin Li   if (!ApplyDecoderOptions(&options, dec)) goto Error;
125*b2055c35SXin Li 
126*b2055c35SXin Li   dec->demux_ = WebPDemux(webp_data);
127*b2055c35SXin Li   if (dec->demux_ == NULL) goto Error;
128*b2055c35SXin Li 
129*b2055c35SXin Li   dec->info_.canvas_width = WebPDemuxGetI(dec->demux_, WEBP_FF_CANVAS_WIDTH);
130*b2055c35SXin Li   dec->info_.canvas_height = WebPDemuxGetI(dec->demux_, WEBP_FF_CANVAS_HEIGHT);
131*b2055c35SXin Li   dec->info_.loop_count = WebPDemuxGetI(dec->demux_, WEBP_FF_LOOP_COUNT);
132*b2055c35SXin Li   dec->info_.bgcolor = WebPDemuxGetI(dec->demux_, WEBP_FF_BACKGROUND_COLOR);
133*b2055c35SXin Li   dec->info_.frame_count = WebPDemuxGetI(dec->demux_, WEBP_FF_FRAME_COUNT);
134*b2055c35SXin Li 
135*b2055c35SXin Li   // Note: calloc() because we fill frame with zeroes as well.
136*b2055c35SXin Li   dec->curr_frame_ = (uint8_t*)WebPSafeCalloc(
137*b2055c35SXin Li       dec->info_.canvas_width * NUM_CHANNELS, dec->info_.canvas_height);
138*b2055c35SXin Li   if (dec->curr_frame_ == NULL) goto Error;
139*b2055c35SXin Li   dec->prev_frame_disposed_ = (uint8_t*)WebPSafeCalloc(
140*b2055c35SXin Li       dec->info_.canvas_width * NUM_CHANNELS, dec->info_.canvas_height);
141*b2055c35SXin Li   if (dec->prev_frame_disposed_ == NULL) goto Error;
142*b2055c35SXin Li 
143*b2055c35SXin Li   WebPAnimDecoderReset(dec);
144*b2055c35SXin Li   return dec;
145*b2055c35SXin Li 
146*b2055c35SXin Li  Error:
147*b2055c35SXin Li   WebPAnimDecoderDelete(dec);
148*b2055c35SXin Li   return NULL;
149*b2055c35SXin Li }
150*b2055c35SXin Li 
WebPAnimDecoderGetInfo(const WebPAnimDecoder * dec,WebPAnimInfo * info)151*b2055c35SXin Li int WebPAnimDecoderGetInfo(const WebPAnimDecoder* dec, WebPAnimInfo* info) {
152*b2055c35SXin Li   if (dec == NULL || info == NULL) return 0;
153*b2055c35SXin Li   *info = dec->info_;
154*b2055c35SXin Li   return 1;
155*b2055c35SXin Li }
156*b2055c35SXin Li 
157*b2055c35SXin Li // Returns true if the frame covers the full canvas.
IsFullFrame(int width,int height,int canvas_width,int canvas_height)158*b2055c35SXin Li static int IsFullFrame(int width, int height, int canvas_width,
159*b2055c35SXin Li                        int canvas_height) {
160*b2055c35SXin Li   return (width == canvas_width && height == canvas_height);
161*b2055c35SXin Li }
162*b2055c35SXin Li 
163*b2055c35SXin Li // Clear the canvas to transparent.
ZeroFillCanvas(uint8_t * buf,uint32_t canvas_width,uint32_t canvas_height)164*b2055c35SXin Li WEBP_NODISCARD static int ZeroFillCanvas(uint8_t* buf, uint32_t canvas_width,
165*b2055c35SXin Li                                          uint32_t canvas_height) {
166*b2055c35SXin Li   const uint64_t size =
167*b2055c35SXin Li       (uint64_t)canvas_width * canvas_height * NUM_CHANNELS * sizeof(*buf);
168*b2055c35SXin Li   if (!CheckSizeOverflow(size)) return 0;
169*b2055c35SXin Li   memset(buf, 0, (size_t)size);
170*b2055c35SXin Li   return 1;
171*b2055c35SXin Li }
172*b2055c35SXin Li 
173*b2055c35SXin Li // Clear given frame rectangle to transparent.
ZeroFillFrameRect(uint8_t * buf,int buf_stride,int x_offset,int y_offset,int width,int height)174*b2055c35SXin Li static void ZeroFillFrameRect(uint8_t* buf, int buf_stride, int x_offset,
175*b2055c35SXin Li                               int y_offset, int width, int height) {
176*b2055c35SXin Li   int j;
177*b2055c35SXin Li   assert(width * NUM_CHANNELS <= buf_stride);
178*b2055c35SXin Li   buf += y_offset * buf_stride + x_offset * NUM_CHANNELS;
179*b2055c35SXin Li   for (j = 0; j < height; ++j) {
180*b2055c35SXin Li     memset(buf, 0, width * NUM_CHANNELS);
181*b2055c35SXin Li     buf += buf_stride;
182*b2055c35SXin Li   }
183*b2055c35SXin Li }
184*b2055c35SXin Li 
185*b2055c35SXin Li // Copy width * height pixels from 'src' to 'dst'.
CopyCanvas(const uint8_t * src,uint8_t * dst,uint32_t width,uint32_t height)186*b2055c35SXin Li WEBP_NODISCARD static int CopyCanvas(const uint8_t* src, uint8_t* dst,
187*b2055c35SXin Li                                      uint32_t width, uint32_t height) {
188*b2055c35SXin Li   const uint64_t size = (uint64_t)width * height * NUM_CHANNELS;
189*b2055c35SXin Li   if (!CheckSizeOverflow(size)) return 0;
190*b2055c35SXin Li   assert(src != NULL && dst != NULL);
191*b2055c35SXin Li   memcpy(dst, src, (size_t)size);
192*b2055c35SXin Li   return 1;
193*b2055c35SXin Li }
194*b2055c35SXin Li 
195*b2055c35SXin Li // Returns true if the current frame is a key-frame.
IsKeyFrame(const WebPIterator * const curr,const WebPIterator * const prev,int prev_frame_was_key_frame,int canvas_width,int canvas_height)196*b2055c35SXin Li static int IsKeyFrame(const WebPIterator* const curr,
197*b2055c35SXin Li                       const WebPIterator* const prev,
198*b2055c35SXin Li                       int prev_frame_was_key_frame,
199*b2055c35SXin Li                       int canvas_width, int canvas_height) {
200*b2055c35SXin Li   if (curr->frame_num == 1) {
201*b2055c35SXin Li     return 1;
202*b2055c35SXin Li   } else if ((!curr->has_alpha || curr->blend_method == WEBP_MUX_NO_BLEND) &&
203*b2055c35SXin Li              IsFullFrame(curr->width, curr->height,
204*b2055c35SXin Li                          canvas_width, canvas_height)) {
205*b2055c35SXin Li     return 1;
206*b2055c35SXin Li   } else {
207*b2055c35SXin Li     return (prev->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) &&
208*b2055c35SXin Li            (IsFullFrame(prev->width, prev->height, canvas_width,
209*b2055c35SXin Li                         canvas_height) ||
210*b2055c35SXin Li             prev_frame_was_key_frame);
211*b2055c35SXin Li   }
212*b2055c35SXin Li }
213*b2055c35SXin Li 
214*b2055c35SXin Li 
215*b2055c35SXin Li // Blend a single channel of 'src' over 'dst', given their alpha channel values.
216*b2055c35SXin Li // 'src' and 'dst' are assumed to be NOT pre-multiplied by alpha.
BlendChannelNonPremult(uint32_t src,uint8_t src_a,uint32_t dst,uint8_t dst_a,uint32_t scale,int shift)217*b2055c35SXin Li static uint8_t BlendChannelNonPremult(uint32_t src, uint8_t src_a,
218*b2055c35SXin Li                                       uint32_t dst, uint8_t dst_a,
219*b2055c35SXin Li                                       uint32_t scale, int shift) {
220*b2055c35SXin Li   const uint8_t src_channel = (src >> shift) & 0xff;
221*b2055c35SXin Li   const uint8_t dst_channel = (dst >> shift) & 0xff;
222*b2055c35SXin Li   const uint32_t blend_unscaled = src_channel * src_a + dst_channel * dst_a;
223*b2055c35SXin Li   assert(blend_unscaled < (1ULL << 32) / scale);
224*b2055c35SXin Li   return (blend_unscaled * scale) >> CHANNEL_SHIFT(3);
225*b2055c35SXin Li }
226*b2055c35SXin Li 
227*b2055c35SXin Li // Blend 'src' over 'dst' assuming they are NOT pre-multiplied by alpha.
BlendPixelNonPremult(uint32_t src,uint32_t dst)228*b2055c35SXin Li static uint32_t BlendPixelNonPremult(uint32_t src, uint32_t dst) {
229*b2055c35SXin Li   const uint8_t src_a = (src >> CHANNEL_SHIFT(3)) & 0xff;
230*b2055c35SXin Li 
231*b2055c35SXin Li   if (src_a == 0) {
232*b2055c35SXin Li     return dst;
233*b2055c35SXin Li   } else {
234*b2055c35SXin Li     const uint8_t dst_a = (dst >> CHANNEL_SHIFT(3)) & 0xff;
235*b2055c35SXin Li     // This is the approximate integer arithmetic for the actual formula:
236*b2055c35SXin Li     // dst_factor_a = (dst_a * (255 - src_a)) / 255.
237*b2055c35SXin Li     const uint8_t dst_factor_a = (dst_a * (256 - src_a)) >> 8;
238*b2055c35SXin Li     const uint8_t blend_a = src_a + dst_factor_a;
239*b2055c35SXin Li     const uint32_t scale = (1UL << 24) / blend_a;
240*b2055c35SXin Li 
241*b2055c35SXin Li     const uint8_t blend_r = BlendChannelNonPremult(
242*b2055c35SXin Li         src, src_a, dst, dst_factor_a, scale, CHANNEL_SHIFT(0));
243*b2055c35SXin Li     const uint8_t blend_g = BlendChannelNonPremult(
244*b2055c35SXin Li         src, src_a, dst, dst_factor_a, scale, CHANNEL_SHIFT(1));
245*b2055c35SXin Li     const uint8_t blend_b = BlendChannelNonPremult(
246*b2055c35SXin Li         src, src_a, dst, dst_factor_a, scale, CHANNEL_SHIFT(2));
247*b2055c35SXin Li     assert(src_a + dst_factor_a < 256);
248*b2055c35SXin Li 
249*b2055c35SXin Li     return ((uint32_t)blend_r << CHANNEL_SHIFT(0)) |
250*b2055c35SXin Li            ((uint32_t)blend_g << CHANNEL_SHIFT(1)) |
251*b2055c35SXin Li            ((uint32_t)blend_b << CHANNEL_SHIFT(2)) |
252*b2055c35SXin Li            ((uint32_t)blend_a << CHANNEL_SHIFT(3));
253*b2055c35SXin Li   }
254*b2055c35SXin Li }
255*b2055c35SXin Li 
256*b2055c35SXin Li // Blend 'num_pixels' in 'src' over 'dst' assuming they are NOT pre-multiplied
257*b2055c35SXin Li // by alpha.
BlendPixelRowNonPremult(uint32_t * const src,const uint32_t * const dst,int num_pixels)258*b2055c35SXin Li static void BlendPixelRowNonPremult(uint32_t* const src,
259*b2055c35SXin Li                                     const uint32_t* const dst, int num_pixels) {
260*b2055c35SXin Li   int i;
261*b2055c35SXin Li   for (i = 0; i < num_pixels; ++i) {
262*b2055c35SXin Li     const uint8_t src_alpha = (src[i] >> CHANNEL_SHIFT(3)) & 0xff;
263*b2055c35SXin Li     if (src_alpha != 0xff) {
264*b2055c35SXin Li       src[i] = BlendPixelNonPremult(src[i], dst[i]);
265*b2055c35SXin Li     }
266*b2055c35SXin Li   }
267*b2055c35SXin Li }
268*b2055c35SXin Li 
269*b2055c35SXin Li // Individually multiply each channel in 'pix' by 'scale'.
ChannelwiseMultiply(uint32_t pix,uint32_t scale)270*b2055c35SXin Li static WEBP_INLINE uint32_t ChannelwiseMultiply(uint32_t pix, uint32_t scale) {
271*b2055c35SXin Li   uint32_t mask = 0x00FF00FF;
272*b2055c35SXin Li   uint32_t rb = ((pix & mask) * scale) >> 8;
273*b2055c35SXin Li   uint32_t ag = ((pix >> 8) & mask) * scale;
274*b2055c35SXin Li   return (rb & mask) | (ag & ~mask);
275*b2055c35SXin Li }
276*b2055c35SXin Li 
277*b2055c35SXin Li // Blend 'src' over 'dst' assuming they are pre-multiplied by alpha.
BlendPixelPremult(uint32_t src,uint32_t dst)278*b2055c35SXin Li static uint32_t BlendPixelPremult(uint32_t src, uint32_t dst) {
279*b2055c35SXin Li   const uint8_t src_a = (src >> CHANNEL_SHIFT(3)) & 0xff;
280*b2055c35SXin Li   return src + ChannelwiseMultiply(dst, 256 - src_a);
281*b2055c35SXin Li }
282*b2055c35SXin Li 
283*b2055c35SXin Li // Blend 'num_pixels' in 'src' over 'dst' assuming they are pre-multiplied by
284*b2055c35SXin Li // alpha.
BlendPixelRowPremult(uint32_t * const src,const uint32_t * const dst,int num_pixels)285*b2055c35SXin Li static void BlendPixelRowPremult(uint32_t* const src, const uint32_t* const dst,
286*b2055c35SXin Li                                  int num_pixels) {
287*b2055c35SXin Li   int i;
288*b2055c35SXin Li   for (i = 0; i < num_pixels; ++i) {
289*b2055c35SXin Li     const uint8_t src_alpha = (src[i] >> CHANNEL_SHIFT(3)) & 0xff;
290*b2055c35SXin Li     if (src_alpha != 0xff) {
291*b2055c35SXin Li       src[i] = BlendPixelPremult(src[i], dst[i]);
292*b2055c35SXin Li     }
293*b2055c35SXin Li   }
294*b2055c35SXin Li }
295*b2055c35SXin Li 
296*b2055c35SXin Li // Returns two ranges (<left, width> pairs) at row 'canvas_y', that belong to
297*b2055c35SXin Li // 'src' but not 'dst'. A point range is empty if the corresponding width is 0.
FindBlendRangeAtRow(const WebPIterator * const src,const WebPIterator * const dst,int canvas_y,int * const left1,int * const width1,int * const left2,int * const width2)298*b2055c35SXin Li static void FindBlendRangeAtRow(const WebPIterator* const src,
299*b2055c35SXin Li                                 const WebPIterator* const dst, int canvas_y,
300*b2055c35SXin Li                                 int* const left1, int* const width1,
301*b2055c35SXin Li                                 int* const left2, int* const width2) {
302*b2055c35SXin Li   const int src_max_x = src->x_offset + src->width;
303*b2055c35SXin Li   const int dst_max_x = dst->x_offset + dst->width;
304*b2055c35SXin Li   const int dst_max_y = dst->y_offset + dst->height;
305*b2055c35SXin Li   assert(canvas_y >= src->y_offset && canvas_y < (src->y_offset + src->height));
306*b2055c35SXin Li   *left1 = -1;
307*b2055c35SXin Li   *width1 = 0;
308*b2055c35SXin Li   *left2 = -1;
309*b2055c35SXin Li   *width2 = 0;
310*b2055c35SXin Li 
311*b2055c35SXin Li   if (canvas_y < dst->y_offset || canvas_y >= dst_max_y ||
312*b2055c35SXin Li       src->x_offset >= dst_max_x || src_max_x <= dst->x_offset) {
313*b2055c35SXin Li     *left1 = src->x_offset;
314*b2055c35SXin Li     *width1 = src->width;
315*b2055c35SXin Li     return;
316*b2055c35SXin Li   }
317*b2055c35SXin Li 
318*b2055c35SXin Li   if (src->x_offset < dst->x_offset) {
319*b2055c35SXin Li     *left1 = src->x_offset;
320*b2055c35SXin Li     *width1 = dst->x_offset - src->x_offset;
321*b2055c35SXin Li   }
322*b2055c35SXin Li 
323*b2055c35SXin Li   if (src_max_x > dst_max_x) {
324*b2055c35SXin Li     *left2 = dst_max_x;
325*b2055c35SXin Li     *width2 = src_max_x - dst_max_x;
326*b2055c35SXin Li   }
327*b2055c35SXin Li }
328*b2055c35SXin Li 
WebPAnimDecoderGetNext(WebPAnimDecoder * dec,uint8_t ** buf_ptr,int * timestamp_ptr)329*b2055c35SXin Li int WebPAnimDecoderGetNext(WebPAnimDecoder* dec,
330*b2055c35SXin Li                            uint8_t** buf_ptr, int* timestamp_ptr) {
331*b2055c35SXin Li   WebPIterator iter;
332*b2055c35SXin Li   uint32_t width;
333*b2055c35SXin Li   uint32_t height;
334*b2055c35SXin Li   int is_key_frame;
335*b2055c35SXin Li   int timestamp;
336*b2055c35SXin Li   BlendRowFunc blend_row;
337*b2055c35SXin Li 
338*b2055c35SXin Li   if (dec == NULL || buf_ptr == NULL || timestamp_ptr == NULL) return 0;
339*b2055c35SXin Li   if (!WebPAnimDecoderHasMoreFrames(dec)) return 0;
340*b2055c35SXin Li 
341*b2055c35SXin Li   width = dec->info_.canvas_width;
342*b2055c35SXin Li   height = dec->info_.canvas_height;
343*b2055c35SXin Li   blend_row = dec->blend_func_;
344*b2055c35SXin Li 
345*b2055c35SXin Li   // Get compressed frame.
346*b2055c35SXin Li   if (!WebPDemuxGetFrame(dec->demux_, dec->next_frame_, &iter)) {
347*b2055c35SXin Li     return 0;
348*b2055c35SXin Li   }
349*b2055c35SXin Li   timestamp = dec->prev_frame_timestamp_ + iter.duration;
350*b2055c35SXin Li 
351*b2055c35SXin Li   // Initialize.
352*b2055c35SXin Li   is_key_frame = IsKeyFrame(&iter, &dec->prev_iter_,
353*b2055c35SXin Li                             dec->prev_frame_was_keyframe_, width, height);
354*b2055c35SXin Li   if (is_key_frame) {
355*b2055c35SXin Li     if (!ZeroFillCanvas(dec->curr_frame_, width, height)) {
356*b2055c35SXin Li       goto Error;
357*b2055c35SXin Li     }
358*b2055c35SXin Li   } else {
359*b2055c35SXin Li     if (!CopyCanvas(dec->prev_frame_disposed_, dec->curr_frame_,
360*b2055c35SXin Li                     width, height)) {
361*b2055c35SXin Li       goto Error;
362*b2055c35SXin Li     }
363*b2055c35SXin Li   }
364*b2055c35SXin Li 
365*b2055c35SXin Li   // Decode.
366*b2055c35SXin Li   {
367*b2055c35SXin Li     const uint8_t* in = iter.fragment.bytes;
368*b2055c35SXin Li     const size_t in_size = iter.fragment.size;
369*b2055c35SXin Li     const uint32_t stride = width * NUM_CHANNELS;  // at most 25 + 2 bits
370*b2055c35SXin Li     const uint64_t out_offset = (uint64_t)iter.y_offset * stride +
371*b2055c35SXin Li                                 (uint64_t)iter.x_offset * NUM_CHANNELS;  // 53b
372*b2055c35SXin Li     const uint64_t size = (uint64_t)iter.height * stride;  // at most 25 + 27b
373*b2055c35SXin Li     WebPDecoderConfig* const config = &dec->config_;
374*b2055c35SXin Li     WebPRGBABuffer* const buf = &config->output.u.RGBA;
375*b2055c35SXin Li     if ((size_t)size != size) goto Error;
376*b2055c35SXin Li     buf->stride = (int)stride;
377*b2055c35SXin Li     buf->size = (size_t)size;
378*b2055c35SXin Li     buf->rgba = dec->curr_frame_ + out_offset;
379*b2055c35SXin Li 
380*b2055c35SXin Li     if (WebPDecode(in, in_size, config) != VP8_STATUS_OK) {
381*b2055c35SXin Li       goto Error;
382*b2055c35SXin Li     }
383*b2055c35SXin Li   }
384*b2055c35SXin Li 
385*b2055c35SXin Li   // During the decoding of current frame, we may have set some pixels to be
386*b2055c35SXin Li   // transparent (i.e. alpha < 255). However, the value of each of these
387*b2055c35SXin Li   // pixels should have been determined by blending it against the value of
388*b2055c35SXin Li   // that pixel in the previous frame if blending method of is WEBP_MUX_BLEND.
389*b2055c35SXin Li   if (iter.frame_num > 1 && iter.blend_method == WEBP_MUX_BLEND &&
390*b2055c35SXin Li       !is_key_frame) {
391*b2055c35SXin Li     if (dec->prev_iter_.dispose_method == WEBP_MUX_DISPOSE_NONE) {
392*b2055c35SXin Li       int y;
393*b2055c35SXin Li       // Blend transparent pixels with pixels in previous canvas.
394*b2055c35SXin Li       for (y = 0; y < iter.height; ++y) {
395*b2055c35SXin Li         const size_t offset =
396*b2055c35SXin Li             (iter.y_offset + y) * width + iter.x_offset;
397*b2055c35SXin Li         blend_row((uint32_t*)dec->curr_frame_ + offset,
398*b2055c35SXin Li                   (uint32_t*)dec->prev_frame_disposed_ + offset, iter.width);
399*b2055c35SXin Li       }
400*b2055c35SXin Li     } else {
401*b2055c35SXin Li       int y;
402*b2055c35SXin Li       assert(dec->prev_iter_.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND);
403*b2055c35SXin Li       // We need to blend a transparent pixel with its value just after
404*b2055c35SXin Li       // initialization. That is, blend it with:
405*b2055c35SXin Li       // * Fully transparent pixel if it belongs to prevRect <-- No-op.
406*b2055c35SXin Li       // * The pixel in the previous canvas otherwise <-- Need alpha-blending.
407*b2055c35SXin Li       for (y = 0; y < iter.height; ++y) {
408*b2055c35SXin Li         const int canvas_y = iter.y_offset + y;
409*b2055c35SXin Li         int left1, width1, left2, width2;
410*b2055c35SXin Li         FindBlendRangeAtRow(&iter, &dec->prev_iter_, canvas_y, &left1, &width1,
411*b2055c35SXin Li                             &left2, &width2);
412*b2055c35SXin Li         if (width1 > 0) {
413*b2055c35SXin Li           const size_t offset1 = canvas_y * width + left1;
414*b2055c35SXin Li           blend_row((uint32_t*)dec->curr_frame_ + offset1,
415*b2055c35SXin Li                     (uint32_t*)dec->prev_frame_disposed_ + offset1, width1);
416*b2055c35SXin Li         }
417*b2055c35SXin Li         if (width2 > 0) {
418*b2055c35SXin Li           const size_t offset2 = canvas_y * width + left2;
419*b2055c35SXin Li           blend_row((uint32_t*)dec->curr_frame_ + offset2,
420*b2055c35SXin Li                     (uint32_t*)dec->prev_frame_disposed_ + offset2, width2);
421*b2055c35SXin Li         }
422*b2055c35SXin Li       }
423*b2055c35SXin Li     }
424*b2055c35SXin Li   }
425*b2055c35SXin Li 
426*b2055c35SXin Li   // Update info of the previous frame and dispose it for the next iteration.
427*b2055c35SXin Li   dec->prev_frame_timestamp_ = timestamp;
428*b2055c35SXin Li   WebPDemuxReleaseIterator(&dec->prev_iter_);
429*b2055c35SXin Li   dec->prev_iter_ = iter;
430*b2055c35SXin Li   dec->prev_frame_was_keyframe_ = is_key_frame;
431*b2055c35SXin Li   if (!CopyCanvas(dec->curr_frame_, dec->prev_frame_disposed_, width, height)) {
432*b2055c35SXin Li     goto Error;
433*b2055c35SXin Li   }
434*b2055c35SXin Li   if (dec->prev_iter_.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {
435*b2055c35SXin Li     ZeroFillFrameRect(dec->prev_frame_disposed_, width * NUM_CHANNELS,
436*b2055c35SXin Li                       dec->prev_iter_.x_offset, dec->prev_iter_.y_offset,
437*b2055c35SXin Li                       dec->prev_iter_.width, dec->prev_iter_.height);
438*b2055c35SXin Li   }
439*b2055c35SXin Li   ++dec->next_frame_;
440*b2055c35SXin Li 
441*b2055c35SXin Li   // All OK, fill in the values.
442*b2055c35SXin Li   *buf_ptr = dec->curr_frame_;
443*b2055c35SXin Li   *timestamp_ptr = timestamp;
444*b2055c35SXin Li   return 1;
445*b2055c35SXin Li 
446*b2055c35SXin Li  Error:
447*b2055c35SXin Li   WebPDemuxReleaseIterator(&iter);
448*b2055c35SXin Li   return 0;
449*b2055c35SXin Li }
450*b2055c35SXin Li 
WebPAnimDecoderHasMoreFrames(const WebPAnimDecoder * dec)451*b2055c35SXin Li int WebPAnimDecoderHasMoreFrames(const WebPAnimDecoder* dec) {
452*b2055c35SXin Li   if (dec == NULL) return 0;
453*b2055c35SXin Li   return (dec->next_frame_ <= (int)dec->info_.frame_count);
454*b2055c35SXin Li }
455*b2055c35SXin Li 
WebPAnimDecoderReset(WebPAnimDecoder * dec)456*b2055c35SXin Li void WebPAnimDecoderReset(WebPAnimDecoder* dec) {
457*b2055c35SXin Li   if (dec != NULL) {
458*b2055c35SXin Li     dec->prev_frame_timestamp_ = 0;
459*b2055c35SXin Li     WebPDemuxReleaseIterator(&dec->prev_iter_);
460*b2055c35SXin Li     memset(&dec->prev_iter_, 0, sizeof(dec->prev_iter_));
461*b2055c35SXin Li     dec->prev_frame_was_keyframe_ = 0;
462*b2055c35SXin Li     dec->next_frame_ = 1;
463*b2055c35SXin Li   }
464*b2055c35SXin Li }
465*b2055c35SXin Li 
WebPAnimDecoderGetDemuxer(const WebPAnimDecoder * dec)466*b2055c35SXin Li const WebPDemuxer* WebPAnimDecoderGetDemuxer(const WebPAnimDecoder* dec) {
467*b2055c35SXin Li   if (dec == NULL) return NULL;
468*b2055c35SXin Li   return dec->demux_;
469*b2055c35SXin Li }
470*b2055c35SXin Li 
WebPAnimDecoderDelete(WebPAnimDecoder * dec)471*b2055c35SXin Li void WebPAnimDecoderDelete(WebPAnimDecoder* dec) {
472*b2055c35SXin Li   if (dec != NULL) {
473*b2055c35SXin Li     WebPDemuxReleaseIterator(&dec->prev_iter_);
474*b2055c35SXin Li     WebPDemuxDelete(dec->demux_);
475*b2055c35SXin Li     WebPSafeFree(dec->curr_frame_);
476*b2055c35SXin Li     WebPSafeFree(dec->prev_frame_disposed_);
477*b2055c35SXin Li     WebPSafeFree(dec);
478*b2055c35SXin Li   }
479*b2055c35SXin Li }
480