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