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