Path: blob/master/thirdparty/libwebp/src/demux/anim_decode.c
20783 views
// Copyright 2015 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// AnimDecoder implementation.10//1112#ifdef HAVE_CONFIG_H13#include "src/webp/config.h"14#endif1516#include <assert.h>17#include <string.h>1819#include "src/utils/utils.h"20#include "src/webp/decode.h"21#include "src/webp/demux.h"22#include "src/webp/mux.h"23#include "src/webp/mux_types.h"24#include "src/webp/types.h"2526#define NUM_CHANNELS 42728// Channel extraction from a uint32_t representation of a uint8_t RGBA/BGRA29// buffer.30#ifdef WORDS_BIGENDIAN31#define CHANNEL_SHIFT(i) (24 - (i) * 8)32#else33#define CHANNEL_SHIFT(i) ((i) * 8)34#endif3536typedef void (*BlendRowFunc)(uint32_t* const, const uint32_t* const, int);37static void BlendPixelRowNonPremult(uint32_t* const src,38const uint32_t* const dst, int num_pixels);39static void BlendPixelRowPremult(uint32_t* const src, const uint32_t* const dst,40int num_pixels);4142struct WebPAnimDecoder {43WebPDemuxer* demux; // Demuxer created from given WebP bitstream.44WebPDecoderConfig config; // Decoder config.45// Note: we use a pointer to a function blending multiple pixels at a time to46// allow possible inlining of per-pixel blending function.47BlendRowFunc blend_func; // Pointer to the chose blend row function.48WebPAnimInfo info; // Global info about the animation.49uint8_t* curr_frame; // Current canvas (not disposed).50uint8_t* prev_frame_disposed; // Previous canvas (properly disposed).51int prev_frame_timestamp; // Previous frame timestamp (milliseconds).52WebPIterator prev_iter; // Iterator object for previous frame.53int prev_frame_was_keyframe; // True if previous frame was a keyframe.54int next_frame; // Index of the next frame to be decoded55// (starting from 1).56};5758static void DefaultDecoderOptions(WebPAnimDecoderOptions* const dec_options) {59dec_options->color_mode = MODE_RGBA;60dec_options->use_threads = 0;61}6263int WebPAnimDecoderOptionsInitInternal(WebPAnimDecoderOptions* dec_options,64int abi_version) {65if (dec_options == NULL ||66WEBP_ABI_IS_INCOMPATIBLE(abi_version, WEBP_DEMUX_ABI_VERSION)) {67return 0;68}69DefaultDecoderOptions(dec_options);70return 1;71}7273WEBP_NODISCARD static int ApplyDecoderOptions(74const WebPAnimDecoderOptions* const dec_options,75WebPAnimDecoder* const dec) {76WEBP_CSP_MODE mode;77WebPDecoderConfig* config = &dec->config;78assert(dec_options != NULL);7980mode = dec_options->color_mode;81if (mode != MODE_RGBA && mode != MODE_BGRA &&82mode != MODE_rgbA && mode != MODE_bgrA) {83return 0;84}85dec->blend_func = (mode == MODE_RGBA || mode == MODE_BGRA)86? &BlendPixelRowNonPremult87: &BlendPixelRowPremult;88if (!WebPInitDecoderConfig(config)) {89return 0;90}91config->output.colorspace = mode;92config->output.is_external_memory = 1;93config->options.use_threads = dec_options->use_threads;94// Note: config->output.u.RGBA is set at the time of decoding each frame.95return 1;96}9798WebPAnimDecoder* WebPAnimDecoderNewInternal(99const WebPData* webp_data, const WebPAnimDecoderOptions* dec_options,100int abi_version) {101WebPAnimDecoderOptions options;102WebPAnimDecoder* dec = NULL;103WebPBitstreamFeatures features;104if (webp_data == NULL ||105WEBP_ABI_IS_INCOMPATIBLE(abi_version, WEBP_DEMUX_ABI_VERSION)) {106return NULL;107}108109// Validate the bitstream before doing expensive allocations. The demuxer may110// be more tolerant than the decoder.111if (WebPGetFeatures(webp_data->bytes, webp_data->size, &features) !=112VP8_STATUS_OK) {113return NULL;114}115116// Note: calloc() so that the pointer members are initialized to NULL.117dec = (WebPAnimDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec));118if (dec == NULL) goto Error;119120if (dec_options != NULL) {121options = *dec_options;122} else {123DefaultDecoderOptions(&options);124}125if (!ApplyDecoderOptions(&options, dec)) goto Error;126127dec->demux = WebPDemux(webp_data);128if (dec->demux == NULL) goto Error;129130dec->info.canvas_width = WebPDemuxGetI(dec->demux, WEBP_FF_CANVAS_WIDTH);131dec->info.canvas_height = WebPDemuxGetI(dec->demux, WEBP_FF_CANVAS_HEIGHT);132dec->info.loop_count = WebPDemuxGetI(dec->demux, WEBP_FF_LOOP_COUNT);133dec->info.bgcolor = WebPDemuxGetI(dec->demux, WEBP_FF_BACKGROUND_COLOR);134dec->info.frame_count = WebPDemuxGetI(dec->demux, WEBP_FF_FRAME_COUNT);135136// Note: calloc() because we fill frame with zeroes as well.137dec->curr_frame = (uint8_t*)WebPSafeCalloc(138dec->info.canvas_width * NUM_CHANNELS, dec->info.canvas_height);139if (dec->curr_frame == NULL) goto Error;140dec->prev_frame_disposed = (uint8_t*)WebPSafeCalloc(141dec->info.canvas_width * NUM_CHANNELS, dec->info.canvas_height);142if (dec->prev_frame_disposed == NULL) goto Error;143144WebPAnimDecoderReset(dec);145return dec;146147Error:148WebPAnimDecoderDelete(dec);149return NULL;150}151152int WebPAnimDecoderGetInfo(const WebPAnimDecoder* dec, WebPAnimInfo* info) {153if (dec == NULL || info == NULL) return 0;154*info = dec->info;155return 1;156}157158// Returns true if the frame covers the full canvas.159static int IsFullFrame(int width, int height, int canvas_width,160int canvas_height) {161return (width == canvas_width && height == canvas_height);162}163164// Clear the canvas to transparent.165WEBP_NODISCARD static int ZeroFillCanvas(uint8_t* buf, uint32_t canvas_width,166uint32_t canvas_height) {167const uint64_t size =168(uint64_t)canvas_width * canvas_height * NUM_CHANNELS * sizeof(*buf);169if (!CheckSizeOverflow(size)) return 0;170memset(buf, 0, (size_t)size);171return 1;172}173174// Clear given frame rectangle to transparent.175static void ZeroFillFrameRect(uint8_t* buf, int buf_stride, int x_offset,176int y_offset, int width, int height) {177int j;178assert(width * NUM_CHANNELS <= buf_stride);179buf += y_offset * buf_stride + x_offset * NUM_CHANNELS;180for (j = 0; j < height; ++j) {181memset(buf, 0, width * NUM_CHANNELS);182buf += buf_stride;183}184}185186// Copy width * height pixels from 'src' to 'dst'.187WEBP_NODISCARD static int CopyCanvas(const uint8_t* src, uint8_t* dst,188uint32_t width, uint32_t height) {189const uint64_t size = (uint64_t)width * height * NUM_CHANNELS;190if (!CheckSizeOverflow(size)) return 0;191assert(src != NULL && dst != NULL);192memcpy(dst, src, (size_t)size);193return 1;194}195196// Returns true if the current frame is a key-frame.197static int IsKeyFrame(const WebPIterator* const curr,198const WebPIterator* const prev,199int prev_frame_was_key_frame,200int canvas_width, int canvas_height) {201if (curr->frame_num == 1) {202return 1;203} else if ((!curr->has_alpha || curr->blend_method == WEBP_MUX_NO_BLEND) &&204IsFullFrame(curr->width, curr->height,205canvas_width, canvas_height)) {206return 1;207} else {208return (prev->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) &&209(IsFullFrame(prev->width, prev->height, canvas_width,210canvas_height) ||211prev_frame_was_key_frame);212}213}214215216// Blend a single channel of 'src' over 'dst', given their alpha channel values.217// 'src' and 'dst' are assumed to be NOT pre-multiplied by alpha.218static uint8_t BlendChannelNonPremult(uint32_t src, uint8_t src_a,219uint32_t dst, uint8_t dst_a,220uint32_t scale, int shift) {221const uint8_t src_channel = (src >> shift) & 0xff;222const uint8_t dst_channel = (dst >> shift) & 0xff;223const uint32_t blend_unscaled = src_channel * src_a + dst_channel * dst_a;224assert(blend_unscaled < (1ULL << 32) / scale);225return (blend_unscaled * scale) >> CHANNEL_SHIFT(3);226}227228// Blend 'src' over 'dst' assuming they are NOT pre-multiplied by alpha.229static uint32_t BlendPixelNonPremult(uint32_t src, uint32_t dst) {230const uint8_t src_a = (src >> CHANNEL_SHIFT(3)) & 0xff;231232if (src_a == 0) {233return dst;234} else {235const uint8_t dst_a = (dst >> CHANNEL_SHIFT(3)) & 0xff;236// This is the approximate integer arithmetic for the actual formula:237// dst_factor_a = (dst_a * (255 - src_a)) / 255.238const uint8_t dst_factor_a = (dst_a * (256 - src_a)) >> 8;239const uint8_t blend_a = src_a + dst_factor_a;240const uint32_t scale = (1UL << 24) / blend_a;241242const uint8_t blend_r = BlendChannelNonPremult(243src, src_a, dst, dst_factor_a, scale, CHANNEL_SHIFT(0));244const uint8_t blend_g = BlendChannelNonPremult(245src, src_a, dst, dst_factor_a, scale, CHANNEL_SHIFT(1));246const uint8_t blend_b = BlendChannelNonPremult(247src, src_a, dst, dst_factor_a, scale, CHANNEL_SHIFT(2));248assert(src_a + dst_factor_a < 256);249250return ((uint32_t)blend_r << CHANNEL_SHIFT(0)) |251((uint32_t)blend_g << CHANNEL_SHIFT(1)) |252((uint32_t)blend_b << CHANNEL_SHIFT(2)) |253((uint32_t)blend_a << CHANNEL_SHIFT(3));254}255}256257// Blend 'num_pixels' in 'src' over 'dst' assuming they are NOT pre-multiplied258// by alpha.259static void BlendPixelRowNonPremult(uint32_t* const src,260const uint32_t* const dst, int num_pixels) {261int i;262for (i = 0; i < num_pixels; ++i) {263const uint8_t src_alpha = (src[i] >> CHANNEL_SHIFT(3)) & 0xff;264if (src_alpha != 0xff) {265src[i] = BlendPixelNonPremult(src[i], dst[i]);266}267}268}269270// Individually multiply each channel in 'pix' by 'scale'.271static WEBP_INLINE uint32_t ChannelwiseMultiply(uint32_t pix, uint32_t scale) {272uint32_t mask = 0x00FF00FF;273uint32_t rb = ((pix & mask) * scale) >> 8;274uint32_t ag = ((pix >> 8) & mask) * scale;275return (rb & mask) | (ag & ~mask);276}277278// Blend 'src' over 'dst' assuming they are pre-multiplied by alpha.279static uint32_t BlendPixelPremult(uint32_t src, uint32_t dst) {280const uint8_t src_a = (src >> CHANNEL_SHIFT(3)) & 0xff;281return src + ChannelwiseMultiply(dst, 256 - src_a);282}283284// Blend 'num_pixels' in 'src' over 'dst' assuming they are pre-multiplied by285// alpha.286static void BlendPixelRowPremult(uint32_t* const src, const uint32_t* const dst,287int num_pixels) {288int i;289for (i = 0; i < num_pixels; ++i) {290const uint8_t src_alpha = (src[i] >> CHANNEL_SHIFT(3)) & 0xff;291if (src_alpha != 0xff) {292src[i] = BlendPixelPremult(src[i], dst[i]);293}294}295}296297// Returns two ranges (<left, width> pairs) at row 'canvas_y', that belong to298// 'src' but not 'dst'. A point range is empty if the corresponding width is 0.299static void FindBlendRangeAtRow(const WebPIterator* const src,300const WebPIterator* const dst, int canvas_y,301int* const left1, int* const width1,302int* const left2, int* const width2) {303const int src_max_x = src->x_offset + src->width;304const int dst_max_x = dst->x_offset + dst->width;305const int dst_max_y = dst->y_offset + dst->height;306assert(canvas_y >= src->y_offset && canvas_y < (src->y_offset + src->height));307*left1 = -1;308*width1 = 0;309*left2 = -1;310*width2 = 0;311312if (canvas_y < dst->y_offset || canvas_y >= dst_max_y ||313src->x_offset >= dst_max_x || src_max_x <= dst->x_offset) {314*left1 = src->x_offset;315*width1 = src->width;316return;317}318319if (src->x_offset < dst->x_offset) {320*left1 = src->x_offset;321*width1 = dst->x_offset - src->x_offset;322}323324if (src_max_x > dst_max_x) {325*left2 = dst_max_x;326*width2 = src_max_x - dst_max_x;327}328}329330int WebPAnimDecoderGetNext(WebPAnimDecoder* dec,331uint8_t** buf_ptr, int* timestamp_ptr) {332WebPIterator iter;333uint32_t width;334uint32_t height;335int is_key_frame;336int timestamp;337BlendRowFunc blend_row;338339if (dec == NULL || buf_ptr == NULL || timestamp_ptr == NULL) return 0;340if (!WebPAnimDecoderHasMoreFrames(dec)) return 0;341342width = dec->info.canvas_width;343height = dec->info.canvas_height;344blend_row = dec->blend_func;345346// Get compressed frame.347if (!WebPDemuxGetFrame(dec->demux, dec->next_frame, &iter)) {348return 0;349}350timestamp = dec->prev_frame_timestamp + iter.duration;351352// Initialize.353is_key_frame = IsKeyFrame(&iter, &dec->prev_iter,354dec->prev_frame_was_keyframe, width, height);355if (is_key_frame) {356if (!ZeroFillCanvas(dec->curr_frame, width, height)) {357goto Error;358}359} else {360if (!CopyCanvas(dec->prev_frame_disposed, dec->curr_frame,361width, height)) {362goto Error;363}364}365366// Decode.367{368const uint8_t* in = iter.fragment.bytes;369const size_t in_size = iter.fragment.size;370const uint32_t stride = width * NUM_CHANNELS; // at most 25 + 2 bits371const uint64_t out_offset = (uint64_t)iter.y_offset * stride +372(uint64_t)iter.x_offset * NUM_CHANNELS; // 53b373const uint64_t size = (uint64_t)iter.height * stride; // at most 25 + 27b374WebPDecoderConfig* const config = &dec->config;375WebPRGBABuffer* const buf = &config->output.u.RGBA;376if ((size_t)size != size) goto Error;377buf->stride = (int)stride;378buf->size = (size_t)size;379buf->rgba = dec->curr_frame + out_offset;380381if (WebPDecode(in, in_size, config) != VP8_STATUS_OK) {382goto Error;383}384}385386// During the decoding of current frame, we may have set some pixels to be387// transparent (i.e. alpha < 255). However, the value of each of these388// pixels should have been determined by blending it against the value of389// that pixel in the previous frame if blending method of is WEBP_MUX_BLEND.390if (iter.frame_num > 1 && iter.blend_method == WEBP_MUX_BLEND &&391!is_key_frame) {392if (dec->prev_iter.dispose_method == WEBP_MUX_DISPOSE_NONE) {393int y;394// Blend transparent pixels with pixels in previous canvas.395for (y = 0; y < iter.height; ++y) {396const size_t offset =397(iter.y_offset + y) * width + iter.x_offset;398blend_row((uint32_t*)dec->curr_frame + offset,399(uint32_t*)dec->prev_frame_disposed + offset, iter.width);400}401} else {402int y;403assert(dec->prev_iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND);404// We need to blend a transparent pixel with its value just after405// initialization. That is, blend it with:406// * Fully transparent pixel if it belongs to prevRect <-- No-op.407// * The pixel in the previous canvas otherwise <-- Need alpha-blending.408for (y = 0; y < iter.height; ++y) {409const int canvas_y = iter.y_offset + y;410int left1, width1, left2, width2;411FindBlendRangeAtRow(&iter, &dec->prev_iter, canvas_y, &left1, &width1,412&left2, &width2);413if (width1 > 0) {414const size_t offset1 = canvas_y * width + left1;415blend_row((uint32_t*)dec->curr_frame + offset1,416(uint32_t*)dec->prev_frame_disposed + offset1, width1);417}418if (width2 > 0) {419const size_t offset2 = canvas_y * width + left2;420blend_row((uint32_t*)dec->curr_frame + offset2,421(uint32_t*)dec->prev_frame_disposed + offset2, width2);422}423}424}425}426427// Update info of the previous frame and dispose it for the next iteration.428dec->prev_frame_timestamp = timestamp;429WebPDemuxReleaseIterator(&dec->prev_iter);430dec->prev_iter = iter;431dec->prev_frame_was_keyframe = is_key_frame;432if (!CopyCanvas(dec->curr_frame, dec->prev_frame_disposed, width, height)) {433goto Error;434}435if (dec->prev_iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {436ZeroFillFrameRect(dec->prev_frame_disposed, width * NUM_CHANNELS,437dec->prev_iter.x_offset, dec->prev_iter.y_offset,438dec->prev_iter.width, dec->prev_iter.height);439}440++dec->next_frame;441442// All OK, fill in the values.443*buf_ptr = dec->curr_frame;444*timestamp_ptr = timestamp;445return 1;446447Error:448WebPDemuxReleaseIterator(&iter);449return 0;450}451452int WebPAnimDecoderHasMoreFrames(const WebPAnimDecoder* dec) {453if (dec == NULL) return 0;454return (dec->next_frame <= (int)dec->info.frame_count);455}456457void WebPAnimDecoderReset(WebPAnimDecoder* dec) {458if (dec != NULL) {459dec->prev_frame_timestamp = 0;460WebPDemuxReleaseIterator(&dec->prev_iter);461memset(&dec->prev_iter, 0, sizeof(dec->prev_iter));462dec->prev_frame_was_keyframe = 0;463dec->next_frame = 1;464}465}466467const WebPDemuxer* WebPAnimDecoderGetDemuxer(const WebPAnimDecoder* dec) {468if (dec == NULL) return NULL;469return dec->demux;470}471472void WebPAnimDecoderDelete(WebPAnimDecoder* dec) {473if (dec != NULL) {474WebPDemuxReleaseIterator(&dec->prev_iter);475WebPDemuxDelete(dec->demux);476WebPSafeFree(dec->curr_frame);477WebPSafeFree(dec->prev_frame_disposed);478WebPSafeFree(dec);479}480}481482483