summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/libwebp/src/mux/anim_encode.c
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@qt.io>2017-03-07 13:05:21 +0100
committerLiang Qi <liang.qi@qt.io>2017-03-13 10:47:45 +0000
commitb7ec9e78633d8f2c75a8b02e17e169497bb103e2 (patch)
treee4be04af4dbcf8cd635715efdf4e769281183746 /src/3rdparty/libwebp/src/mux/anim_encode.c
parentf2dbc67c2b032a5f27d0224e020fb6dfcd3fd142 (diff)
Bundled libwebp updated to version 0.6.0
This commit imports libwebp 0.6.0, including AUTHORS, COPYING, ChangeLog, NEWS, PATENTS, README and src directories. In src, only includes header and source files. Upstream changes since 0.5.1 have been merged in. Also updated version in qt_attribution.json. Conflicts: src/3rdparty/libwebp.pri src/3rdparty/libwebp/qt_attribution.json src/3rdparty/libwebp/src/webp/config.h Change-Id: I001aa7a3fabf0130b54f9005c23aa822bc1d0ec1 Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Diffstat (limited to 'src/3rdparty/libwebp/src/mux/anim_encode.c')
-rw-r--r--src/3rdparty/libwebp/src/mux/anim_encode.c91
1 files changed, 68 insertions, 23 deletions
diff --git a/src/3rdparty/libwebp/src/mux/anim_encode.c b/src/3rdparty/libwebp/src/mux/anim_encode.c
index 53e2906..6066388 100644
--- a/src/3rdparty/libwebp/src/mux/anim_encode.c
+++ b/src/3rdparty/libwebp/src/mux/anim_encode.c
@@ -16,6 +16,7 @@
#include <stdio.h>
#include <stdlib.h> // for abs()
+#include "../mux/animi.h"
#include "../utils/utils.h"
#include "../webp/decode.h"
#include "../webp/encode.h"
@@ -128,14 +129,13 @@ static void SanitizeEncoderOptions(WebPAnimEncoderOptions* const enc_options) {
DisableKeyframes(enc_options);
}
- if (enc_options->kmin <= 0) {
- DisableKeyframes(enc_options);
- print_warning = 0;
- }
- if (enc_options->kmax <= 0) { // All frames will be key-frames.
+ if (enc_options->kmax == 1) { // All frames will be key-frames.
enc_options->kmin = 0;
enc_options->kmax = 0;
return;
+ } else if (enc_options->kmax <= 0) {
+ DisableKeyframes(enc_options);
+ print_warning = 0;
}
if (enc_options->kmin >= enc_options->kmax) {
@@ -378,10 +378,10 @@ static WEBP_INLINE int PixelsAreSimilar(uint32_t src, uint32_t dst,
const int dst_g = (dst >> 8) & 0xff;
const int dst_b = (dst >> 0) & 0xff;
- return (abs(src_r * src_a - dst_r * dst_a) <= (max_allowed_diff * 255)) &&
- (abs(src_g * src_a - dst_g * dst_a) <= (max_allowed_diff * 255)) &&
- (abs(src_b * src_a - dst_b * dst_a) <= (max_allowed_diff * 255)) &&
- (abs(src_a - dst_a) <= max_allowed_diff);
+ return (src_a == dst_a) &&
+ (abs(src_r - dst_r) * dst_a <= (max_allowed_diff * 255)) &&
+ (abs(src_g - dst_g) * dst_a <= (max_allowed_diff * 255)) &&
+ (abs(src_b - dst_b) * dst_a <= (max_allowed_diff * 255));
}
// Returns true if 'length' number of pixels in 'src' and 'dst' are within an
@@ -586,6 +586,39 @@ static int GetSubRects(const WebPPicture* const prev_canvas,
&params->rect_lossy_, &params->sub_frame_lossy_);
}
+static WEBP_INLINE int clip(int v, int min_v, int max_v) {
+ return (v < min_v) ? min_v : (v > max_v) ? max_v : v;
+}
+
+int WebPAnimEncoderRefineRect(
+ const WebPPicture* const prev_canvas, const WebPPicture* const curr_canvas,
+ int is_lossless, float quality, int* const x_offset, int* const y_offset,
+ int* const width, int* const height) {
+ FrameRect rect;
+ const int right = clip(*x_offset + *width, 0, curr_canvas->width);
+ const int left = clip(*x_offset, 0, curr_canvas->width - 1);
+ const int bottom = clip(*y_offset + *height, 0, curr_canvas->height);
+ const int top = clip(*y_offset, 0, curr_canvas->height - 1);
+ if (prev_canvas == NULL || curr_canvas == NULL ||
+ prev_canvas->width != curr_canvas->width ||
+ prev_canvas->height != curr_canvas->height ||
+ !prev_canvas->use_argb || !curr_canvas->use_argb) {
+ return 0;
+ }
+ rect.x_offset_ = left;
+ rect.y_offset_ = top;
+ rect.width_ = clip(right - left, 0, curr_canvas->width - rect.x_offset_);
+ rect.height_ = clip(bottom - top, 0, curr_canvas->height - rect.y_offset_);
+ MinimizeChangeRectangle(prev_canvas, curr_canvas, &rect, is_lossless,
+ quality);
+ SnapToEvenOffsets(&rect);
+ *x_offset = rect.x_offset_;
+ *y_offset = rect.y_offset_;
+ *width = rect.width_;
+ *height = rect.height_;
+ return 1;
+}
+
static void DisposeFrameRectangle(int dispose_method,
const FrameRect* const rect,
WebPPicture* const curr_canvas) {
@@ -829,8 +862,8 @@ static WebPEncodingError GenerateCandidates(
WebPPicture* const curr_canvas = &enc->curr_canvas_copy_;
const WebPPicture* const prev_canvas =
is_dispose_none ? &enc->prev_canvas_ : &enc->prev_canvas_disposed_;
- int use_blending_ll;
- int use_blending_lossy;
+ int use_blending_ll, use_blending_lossy;
+ int evaluate_ll, evaluate_lossy;
CopyCurrentCanvas(enc);
use_blending_ll =
@@ -843,16 +876,19 @@ static WebPEncodingError GenerateCandidates(
// Pick candidates to be tried.
if (!enc->options_.allow_mixed) {
- candidate_ll->evaluate_ = is_lossless;
- candidate_lossy->evaluate_ = !is_lossless;
+ evaluate_ll = is_lossless;
+ evaluate_lossy = !is_lossless;
+ } else if (enc->options_.minimize_size) {
+ evaluate_ll = 1;
+ evaluate_lossy = 1;
} else { // Use a heuristic for trying lossless and/or lossy compression.
const int num_colors = WebPGetColorPalette(&params->sub_frame_ll_, NULL);
- candidate_ll->evaluate_ = (num_colors < MAX_COLORS_LOSSLESS);
- candidate_lossy->evaluate_ = (num_colors >= MIN_COLORS_LOSSY);
+ evaluate_ll = (num_colors < MAX_COLORS_LOSSLESS);
+ evaluate_lossy = (num_colors >= MIN_COLORS_LOSSY);
}
// Generate candidates.
- if (candidate_ll->evaluate_) {
+ if (evaluate_ll) {
CopyCurrentCanvas(enc);
if (use_blending_ll) {
enc->curr_canvas_copy_modified_ =
@@ -862,7 +898,7 @@ static WebPEncodingError GenerateCandidates(
config_ll, use_blending_ll, candidate_ll);
if (error_code != VP8_ENC_OK) return error_code;
}
- if (candidate_lossy->evaluate_) {
+ if (evaluate_lossy) {
CopyCurrentCanvas(enc);
if (use_blending_lossy) {
enc->curr_canvas_copy_modified_ =
@@ -1029,6 +1065,8 @@ static WebPEncodingError SetFrame(WebPAnimEncoder* const enc,
const WebPPicture* const prev_canvas = &enc->prev_canvas_;
Candidate candidates[CANDIDATE_COUNT];
const int is_lossless = config->lossless;
+ const int consider_lossless = is_lossless || enc->options_.allow_mixed;
+ const int consider_lossy = !is_lossless || enc->options_.allow_mixed;
const int is_first_frame = enc->is_first_frame_;
// First frame cannot be skipped as there is no 'previous frame' to merge it
@@ -1066,9 +1104,7 @@ static WebPEncodingError SetFrame(WebPAnimEncoder* const enc,
return VP8_ENC_ERROR_INVALID_CONFIGURATION;
}
- for (i = 0; i < CANDIDATE_COUNT; ++i) {
- candidates[i].evaluate_ = 0;
- }
+ memset(candidates, 0, sizeof(candidates));
// Change-rectangle assuming previous frame was DISPOSE_NONE.
if (!GetSubRects(prev_canvas, curr_canvas, is_key_frame, is_first_frame,
@@ -1077,8 +1113,8 @@ static WebPEncodingError SetFrame(WebPAnimEncoder* const enc,
goto Err;
}
- if ((is_lossless && IsEmptyRect(&dispose_none_params.rect_ll_)) ||
- (!is_lossless && IsEmptyRect(&dispose_none_params.rect_lossy_))) {
+ if ((consider_lossless && IsEmptyRect(&dispose_none_params.rect_ll_)) ||
+ (consider_lossy && IsEmptyRect(&dispose_none_params.rect_lossy_))) {
// Don't encode the frame at all. Instead, the duration of the previous
// frame will be increased later.
assert(empty_rect_allowed_none);
@@ -1187,16 +1223,20 @@ static int CacheFrame(WebPAnimEncoder* const enc,
enc->prev_candidate_undecided_ = 0;
} else {
int64_t curr_delta;
+ FrameRect prev_rect_key, prev_rect_sub;
// Add this as a frame rectangle to enc.
error_code = SetFrame(enc, config, 0, encoded_frame, &frame_skipped);
if (error_code != VP8_ENC_OK) goto End;
if (frame_skipped) goto Skip;
+ prev_rect_sub = enc->prev_rect_;
+
// Add this as a key-frame to enc, too.
error_code = SetFrame(enc, config, 1, encoded_frame, &frame_skipped);
if (error_code != VP8_ENC_OK) goto End;
assert(frame_skipped == 0); // Key-frame cannot be an empty rectangle.
+ prev_rect_key = enc->prev_rect_;
// Analyze size difference of the two variants.
curr_delta = KeyFramePenalty(encoded_frame);
@@ -1207,11 +1247,13 @@ static int CacheFrame(WebPAnimEncoder* const enc,
old_keyframe->is_key_frame_ = 0;
}
encoded_frame->is_key_frame_ = 1;
+ enc->prev_candidate_undecided_ = 1;
enc->keyframe_ = (int)position;
enc->best_delta_ = curr_delta;
enc->flush_count_ = enc->count_ - 1; // We can flush previous frames.
} else {
encoded_frame->is_key_frame_ = 0;
+ enc->prev_candidate_undecided_ = 0;
}
// Note: We need '>=' below because when kmin and kmax are both zero,
// count_since_key_frame will always be > kmax.
@@ -1221,7 +1263,10 @@ static int CacheFrame(WebPAnimEncoder* const enc,
enc->keyframe_ = KEYFRAME_NONE;
enc->best_delta_ = DELTA_INFINITY;
}
- enc->prev_candidate_undecided_ = 1;
+ if (!enc->prev_candidate_undecided_) {
+ enc->prev_rect_ =
+ encoded_frame->is_key_frame_ ? prev_rect_key : prev_rect_sub;
+ }
}
}