From b9df35f7143c6e08f64819a5271c5c4972bccb2c Mon Sep 17 00:00:00 2001 From: Urvang Joshi Date: Tue, 3 Feb 2015 11:42:13 -0800 Subject: [PATCH] AnimEncode API: kmax=0 should imply all keyframes. Earlier, it wasn't adding any keyframes at all. Change-Id: If3824fc8e57548b8610a52e875fb9279f862fa57 --- man/gif2webp.1 | 3 ++- src/mux/anim_encode.c | 15 ++++++++++----- src/webp/mux.h | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/man/gif2webp.1 b/man/gif2webp.1 index ccc61726..1acff850 100644 --- a/man/gif2webp.1 +++ b/man/gif2webp.1 @@ -68,7 +68,8 @@ Specify the minimum and maximum distance between consecutive key frames some key frames into the output animation as needed so that this criteria is satisfied. .br -A 'kmin' value of 0 will turn off insertion of key frames. +A 'kmin' value of 0 will turn off insertion of key frames. A 'kmax' value of 0 +will result in all frames being key frames. Typical values are in the range 3 to 30. Default values are kmin = 9, kmax = 17 for lossless compression and kmin = 3, kmax = 5 for lossy compression. .br diff --git a/src/mux/anim_encode.c b/src/mux/anim_encode.c index d2898f74..3b42510d 100644 --- a/src/mux/anim_encode.c +++ b/src/mux/anim_encode.c @@ -111,9 +111,9 @@ static void SanitizeEncoderOptions(WebPAnimEncoderOptions* const enc_options) { DisableKeyframes(enc_options); print_warning = 0; } - if (enc_options->kmax == 0) { - enc_options->kmax = ~0; - print_warning = 0; + if (enc_options->kmax == 0) { // All frames will be key-frames. + enc_options->kmin = 0; + return; } if (enc_options->kmin >= enc_options->kmax) { @@ -144,7 +144,7 @@ static void SanitizeEncoderOptions(WebPAnimEncoderOptions* const enc_options) { (int)enc_options->kmin, MAX_CACHED_FRAMES); } } - assert(enc_options->kmin <= enc_options->kmax); + assert(enc_options->kmin < enc_options->kmax); } #undef MAX_CACHED_FRAMES @@ -242,6 +242,9 @@ WebPAnimEncoder* WebPAnimEncoderNewInternal( ResetCounters(enc); // Note: one extra storage is for the previous frame. enc->size_ = enc->options_.kmax - enc->options_.kmin + 1; + // We need space for at least 2 frames. But when kmin, kmax are both zero, + // enc->size_ will be 1. So we handle that special case below. + if (enc->size_ < 2) enc->size_ = 2; enc->encoded_frames_ = (EncodedFrame*)WebPSafeCalloc(enc->size_, sizeof(*enc->encoded_frames_)); if (enc->encoded_frames_ == NULL) goto Err; @@ -972,7 +975,9 @@ static int CacheFrame(WebPAnimEncoder* const enc, int duration, } else { encoded_frame->is_key_frame_ = 0; } - if (enc->count_since_key_frame_ == enc->options_.kmax) { + // Note: We need '>=' below because when kmin and kmax are both zero, + // count_since_key_frame will always be > kmax. + if (enc->count_since_key_frame_ >= enc->options_.kmax) { enc->flush_count_ = enc->count_ - 1; enc->count_since_key_frame_ = 0; enc->keyframe_ = KEYFRAME_NONE; diff --git a/src/webp/mux.h b/src/webp/mux.h index 5126ad5d..28492956 100644 --- a/src/webp/mux.h +++ b/src/webp/mux.h @@ -427,7 +427,8 @@ typedef struct { // frames as needed to satisfy this criteria. // Note that these conditions should hold: kmax > kmin // and kmin >= kmax / 2 + 1. Also, if kmin == 0, then - // key-frame insertion is disabled. + // key-frame insertion is disabled; and if kmax == 0, + // then all frames will be key-frames. int allow_mixed; // If true, use mixed compression mode; may choose // either lossy and lossless for each frame.