Path: blob/master/3rdparty/libwebp/src/demux/anim_decode.c
16345 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"2223#define NUM_CHANNELS 42425typedef void (*BlendRowFunc)(uint32_t* const, const uint32_t* const, int);26static void BlendPixelRowNonPremult(uint32_t* const src,27const uint32_t* const dst, int num_pixels);28static void BlendPixelRowPremult(uint32_t* const src, const uint32_t* const dst,29int num_pixels);3031struct WebPAnimDecoder {32WebPDemuxer* demux_; // Demuxer created from given WebP bitstream.33WebPDecoderConfig config_; // Decoder config.34// Note: we use a pointer to a function blending multiple pixels at a time to35// allow possible inlining of per-pixel blending function.36BlendRowFunc blend_func_; // Pointer to the chose blend row function.37WebPAnimInfo info_; // Global info about the animation.38uint8_t* curr_frame_; // Current canvas (not disposed).39uint8_t* prev_frame_disposed_; // Previous canvas (properly disposed).40int prev_frame_timestamp_; // Previous frame timestamp (milliseconds).41WebPIterator prev_iter_; // Iterator object for previous frame.42int prev_frame_was_keyframe_; // True if previous frame was a keyframe.43int next_frame_; // Index of the next frame to be decoded44// (starting from 1).45};4647static void DefaultDecoderOptions(WebPAnimDecoderOptions* const dec_options) {48dec_options->color_mode = MODE_RGBA;49dec_options->use_threads = 0;50}5152int WebPAnimDecoderOptionsInitInternal(WebPAnimDecoderOptions* dec_options,53int abi_version) {54if (dec_options == NULL ||55WEBP_ABI_IS_INCOMPATIBLE(abi_version, WEBP_DEMUX_ABI_VERSION)) {56return 0;57}58DefaultDecoderOptions(dec_options);59return 1;60}6162static int ApplyDecoderOptions(const WebPAnimDecoderOptions* const dec_options,63WebPAnimDecoder* const dec) {64WEBP_CSP_MODE mode;65WebPDecoderConfig* config = &dec->config_;66assert(dec_options != NULL);6768mode = dec_options->color_mode;69if (mode != MODE_RGBA && mode != MODE_BGRA &&70mode != MODE_rgbA && mode != MODE_bgrA) {71return 0;72}73dec->blend_func_ = (mode == MODE_RGBA || mode == MODE_BGRA)74? &BlendPixelRowNonPremult75: &BlendPixelRowPremult;76WebPInitDecoderConfig(config);77config->output.colorspace = mode;78config->output.is_external_memory = 1;79config->options.use_threads = dec_options->use_threads;80// Note: config->output.u.RGBA is set at the time of decoding each frame.81return 1;82}8384WebPAnimDecoder* WebPAnimDecoderNewInternal(85const WebPData* webp_data, const WebPAnimDecoderOptions* dec_options,86int abi_version) {87WebPAnimDecoderOptions options;88WebPAnimDecoder* dec = NULL;89if (webp_data == NULL ||90WEBP_ABI_IS_INCOMPATIBLE(abi_version, WEBP_DEMUX_ABI_VERSION)) {91return NULL;92}9394// Note: calloc() so that the pointer members are initialized to NULL.95dec = (WebPAnimDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec));96if (dec == NULL) goto Error;9798if (dec_options != NULL) {99options = *dec_options;100} else {101DefaultDecoderOptions(&options);102}103if (!ApplyDecoderOptions(&options, dec)) goto Error;104105dec->demux_ = WebPDemux(webp_data);106if (dec->demux_ == NULL) goto Error;107108dec->info_.canvas_width = WebPDemuxGetI(dec->demux_, WEBP_FF_CANVAS_WIDTH);109dec->info_.canvas_height = WebPDemuxGetI(dec->demux_, WEBP_FF_CANVAS_HEIGHT);110dec->info_.loop_count = WebPDemuxGetI(dec->demux_, WEBP_FF_LOOP_COUNT);111dec->info_.bgcolor = WebPDemuxGetI(dec->demux_, WEBP_FF_BACKGROUND_COLOR);112dec->info_.frame_count = WebPDemuxGetI(dec->demux_, WEBP_FF_FRAME_COUNT);113114// Note: calloc() because we fill frame with zeroes as well.115dec->curr_frame_ = (uint8_t*)WebPSafeCalloc(116dec->info_.canvas_width * NUM_CHANNELS, dec->info_.canvas_height);117if (dec->curr_frame_ == NULL) goto Error;118dec->prev_frame_disposed_ = (uint8_t*)WebPSafeCalloc(119dec->info_.canvas_width * NUM_CHANNELS, dec->info_.canvas_height);120if (dec->prev_frame_disposed_ == NULL) goto Error;121122WebPAnimDecoderReset(dec);123return dec;124125Error:126WebPAnimDecoderDelete(dec);127return NULL;128}129130int WebPAnimDecoderGetInfo(const WebPAnimDecoder* dec, WebPAnimInfo* info) {131if (dec == NULL || info == NULL) return 0;132*info = dec->info_;133return 1;134}135136// Returns true if the frame covers the full canvas.137static int IsFullFrame(int width, int height, int canvas_width,138int canvas_height) {139return (width == canvas_width && height == canvas_height);140}141142// Clear the canvas to transparent.143static int ZeroFillCanvas(uint8_t* buf, uint32_t canvas_width,144uint32_t canvas_height) {145const uint64_t size =146(uint64_t)canvas_width * canvas_height * NUM_CHANNELS * sizeof(*buf);147if (size != (size_t)size) return 0;148memset(buf, 0, (size_t)size);149return 1;150}151152// Clear given frame rectangle to transparent.153static void ZeroFillFrameRect(uint8_t* buf, int buf_stride, int x_offset,154int y_offset, int width, int height) {155int j;156assert(width * NUM_CHANNELS <= buf_stride);157buf += y_offset * buf_stride + x_offset * NUM_CHANNELS;158for (j = 0; j < height; ++j) {159memset(buf, 0, width * NUM_CHANNELS);160buf += buf_stride;161}162}163164// Copy width * height pixels from 'src' to 'dst'.165static int CopyCanvas(const uint8_t* src, uint8_t* dst,166uint32_t width, uint32_t height) {167const uint64_t size = (uint64_t)width * height * NUM_CHANNELS;168if (size != (size_t)size) return 0;169assert(src != NULL && dst != NULL);170memcpy(dst, src, (size_t)size);171return 1;172}173174// Returns true if the current frame is a key-frame.175static int IsKeyFrame(const WebPIterator* const curr,176const WebPIterator* const prev,177int prev_frame_was_key_frame,178int canvas_width, int canvas_height) {179if (curr->frame_num == 1) {180return 1;181} else if ((!curr->has_alpha || curr->blend_method == WEBP_MUX_NO_BLEND) &&182IsFullFrame(curr->width, curr->height,183canvas_width, canvas_height)) {184return 1;185} else {186return (prev->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) &&187(IsFullFrame(prev->width, prev->height, canvas_width,188canvas_height) ||189prev_frame_was_key_frame);190}191}192193194// Blend a single channel of 'src' over 'dst', given their alpha channel values.195// 'src' and 'dst' are assumed to be NOT pre-multiplied by alpha.196static uint8_t BlendChannelNonPremult(uint32_t src, uint8_t src_a,197uint32_t dst, uint8_t dst_a,198uint32_t scale, int shift) {199const uint8_t src_channel = (src >> shift) & 0xff;200const uint8_t dst_channel = (dst >> shift) & 0xff;201const uint32_t blend_unscaled = src_channel * src_a + dst_channel * dst_a;202assert(blend_unscaled < (1ULL << 32) / scale);203return (blend_unscaled * scale) >> 24;204}205206// Blend 'src' over 'dst' assuming they are NOT pre-multiplied by alpha.207static uint32_t BlendPixelNonPremult(uint32_t src, uint32_t dst) {208const uint8_t src_a = (src >> 24) & 0xff;209210if (src_a == 0) {211return dst;212} else {213const uint8_t dst_a = (dst >> 24) & 0xff;214// This is the approximate integer arithmetic for the actual formula:215// dst_factor_a = (dst_a * (255 - src_a)) / 255.216const uint8_t dst_factor_a = (dst_a * (256 - src_a)) >> 8;217const uint8_t blend_a = src_a + dst_factor_a;218const uint32_t scale = (1UL << 24) / blend_a;219220const uint8_t blend_r =221BlendChannelNonPremult(src, src_a, dst, dst_factor_a, scale, 0);222const uint8_t blend_g =223BlendChannelNonPremult(src, src_a, dst, dst_factor_a, scale, 8);224const uint8_t blend_b =225BlendChannelNonPremult(src, src_a, dst, dst_factor_a, scale, 16);226assert(src_a + dst_factor_a < 256);227228return (blend_r << 0) |229(blend_g << 8) |230(blend_b << 16) |231((uint32_t)blend_a << 24);232}233}234235// Blend 'num_pixels' in 'src' over 'dst' assuming they are NOT pre-multiplied236// by alpha.237static void BlendPixelRowNonPremult(uint32_t* const src,238const uint32_t* const dst, int num_pixels) {239int i;240for (i = 0; i < num_pixels; ++i) {241const uint8_t src_alpha = (src[i] >> 24) & 0xff;242if (src_alpha != 0xff) {243src[i] = BlendPixelNonPremult(src[i], dst[i]);244}245}246}247248// Individually multiply each channel in 'pix' by 'scale'.249static WEBP_INLINE uint32_t ChannelwiseMultiply(uint32_t pix, uint32_t scale) {250uint32_t mask = 0x00FF00FF;251uint32_t rb = ((pix & mask) * scale) >> 8;252uint32_t ag = ((pix >> 8) & mask) * scale;253return (rb & mask) | (ag & ~mask);254}255256// Blend 'src' over 'dst' assuming they are pre-multiplied by alpha.257static uint32_t BlendPixelPremult(uint32_t src, uint32_t dst) {258const uint8_t src_a = (src >> 24) & 0xff;259return src + ChannelwiseMultiply(dst, 256 - src_a);260}261262// Blend 'num_pixels' in 'src' over 'dst' assuming they are pre-multiplied by263// alpha.264static void BlendPixelRowPremult(uint32_t* const src, const uint32_t* const dst,265int num_pixels) {266int i;267for (i = 0; i < num_pixels; ++i) {268const uint8_t src_alpha = (src[i] >> 24) & 0xff;269if (src_alpha != 0xff) {270src[i] = BlendPixelPremult(src[i], dst[i]);271}272}273}274275// Returns two ranges (<left, width> pairs) at row 'canvas_y', that belong to276// 'src' but not 'dst'. A point range is empty if the corresponding width is 0.277static void FindBlendRangeAtRow(const WebPIterator* const src,278const WebPIterator* const dst, int canvas_y,279int* const left1, int* const width1,280int* const left2, int* const width2) {281const int src_max_x = src->x_offset + src->width;282const int dst_max_x = dst->x_offset + dst->width;283const int dst_max_y = dst->y_offset + dst->height;284assert(canvas_y >= src->y_offset && canvas_y < (src->y_offset + src->height));285*left1 = -1;286*width1 = 0;287*left2 = -1;288*width2 = 0;289290if (canvas_y < dst->y_offset || canvas_y >= dst_max_y ||291src->x_offset >= dst_max_x || src_max_x <= dst->x_offset) {292*left1 = src->x_offset;293*width1 = src->width;294return;295}296297if (src->x_offset < dst->x_offset) {298*left1 = src->x_offset;299*width1 = dst->x_offset - src->x_offset;300}301302if (src_max_x > dst_max_x) {303*left2 = dst_max_x;304*width2 = src_max_x - dst_max_x;305}306}307308int WebPAnimDecoderGetNext(WebPAnimDecoder* dec,309uint8_t** buf_ptr, int* timestamp_ptr) {310WebPIterator iter;311uint32_t width;312uint32_t height;313int is_key_frame;314int timestamp;315BlendRowFunc blend_row;316317if (dec == NULL || buf_ptr == NULL || timestamp_ptr == NULL) return 0;318if (!WebPAnimDecoderHasMoreFrames(dec)) return 0;319320width = dec->info_.canvas_width;321height = dec->info_.canvas_height;322blend_row = dec->blend_func_;323324// Get compressed frame.325if (!WebPDemuxGetFrame(dec->demux_, dec->next_frame_, &iter)) {326return 0;327}328timestamp = dec->prev_frame_timestamp_ + iter.duration;329330// Initialize.331is_key_frame = IsKeyFrame(&iter, &dec->prev_iter_,332dec->prev_frame_was_keyframe_, width, height);333if (is_key_frame) {334if (!ZeroFillCanvas(dec->curr_frame_, width, height)) {335goto Error;336}337} else {338if (!CopyCanvas(dec->prev_frame_disposed_, dec->curr_frame_,339width, height)) {340goto Error;341}342}343344// Decode.345{346const uint8_t* in = iter.fragment.bytes;347const size_t in_size = iter.fragment.size;348const size_t out_offset =349(iter.y_offset * width + iter.x_offset) * NUM_CHANNELS;350WebPDecoderConfig* const config = &dec->config_;351WebPRGBABuffer* const buf = &config->output.u.RGBA;352buf->stride = NUM_CHANNELS * width;353buf->size = buf->stride * iter.height;354buf->rgba = dec->curr_frame_ + out_offset;355356if (WebPDecode(in, in_size, config) != VP8_STATUS_OK) {357goto Error;358}359}360361// During the decoding of current frame, we may have set some pixels to be362// transparent (i.e. alpha < 255). However, the value of each of these363// pixels should have been determined by blending it against the value of364// that pixel in the previous frame if blending method of is WEBP_MUX_BLEND.365if (iter.frame_num > 1 && iter.blend_method == WEBP_MUX_BLEND &&366!is_key_frame) {367if (dec->prev_iter_.dispose_method == WEBP_MUX_DISPOSE_NONE) {368int y;369// Blend transparent pixels with pixels in previous canvas.370for (y = 0; y < iter.height; ++y) {371const size_t offset =372(iter.y_offset + y) * width + iter.x_offset;373blend_row((uint32_t*)dec->curr_frame_ + offset,374(uint32_t*)dec->prev_frame_disposed_ + offset, iter.width);375}376} else {377int y;378assert(dec->prev_iter_.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND);379// We need to blend a transparent pixel with its value just after380// initialization. That is, blend it with:381// * Fully transparent pixel if it belongs to prevRect <-- No-op.382// * The pixel in the previous canvas otherwise <-- Need alpha-blending.383for (y = 0; y < iter.height; ++y) {384const int canvas_y = iter.y_offset + y;385int left1, width1, left2, width2;386FindBlendRangeAtRow(&iter, &dec->prev_iter_, canvas_y, &left1, &width1,387&left2, &width2);388if (width1 > 0) {389const size_t offset1 = canvas_y * width + left1;390blend_row((uint32_t*)dec->curr_frame_ + offset1,391(uint32_t*)dec->prev_frame_disposed_ + offset1, width1);392}393if (width2 > 0) {394const size_t offset2 = canvas_y * width + left2;395blend_row((uint32_t*)dec->curr_frame_ + offset2,396(uint32_t*)dec->prev_frame_disposed_ + offset2, width2);397}398}399}400}401402// Update info of the previous frame and dispose it for the next iteration.403dec->prev_frame_timestamp_ = timestamp;404WebPDemuxReleaseIterator(&dec->prev_iter_);405dec->prev_iter_ = iter;406dec->prev_frame_was_keyframe_ = is_key_frame;407CopyCanvas(dec->curr_frame_, dec->prev_frame_disposed_, width, height);408if (dec->prev_iter_.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {409ZeroFillFrameRect(dec->prev_frame_disposed_, width * NUM_CHANNELS,410dec->prev_iter_.x_offset, dec->prev_iter_.y_offset,411dec->prev_iter_.width, dec->prev_iter_.height);412}413++dec->next_frame_;414415// All OK, fill in the values.416*buf_ptr = dec->curr_frame_;417*timestamp_ptr = timestamp;418return 1;419420Error:421WebPDemuxReleaseIterator(&iter);422return 0;423}424425int WebPAnimDecoderHasMoreFrames(const WebPAnimDecoder* dec) {426if (dec == NULL) return 0;427return (dec->next_frame_ <= (int)dec->info_.frame_count);428}429430void WebPAnimDecoderReset(WebPAnimDecoder* dec) {431if (dec != NULL) {432dec->prev_frame_timestamp_ = 0;433WebPDemuxReleaseIterator(&dec->prev_iter_);434memset(&dec->prev_iter_, 0, sizeof(dec->prev_iter_));435dec->prev_frame_was_keyframe_ = 0;436dec->next_frame_ = 1;437}438}439440const WebPDemuxer* WebPAnimDecoderGetDemuxer(const WebPAnimDecoder* dec) {441if (dec == NULL) return NULL;442return dec->demux_;443}444445void WebPAnimDecoderDelete(WebPAnimDecoder* dec) {446if (dec != NULL) {447WebPDemuxReleaseIterator(&dec->prev_iter_);448WebPDemuxDelete(dec->demux_);449WebPSafeFree(dec->curr_frame_);450WebPSafeFree(dec->prev_frame_disposed_);451WebPSafeFree(dec);452}453}454455456