From 8c7cd722f639ef31d571f5c41588b07ad05177a2 Mon Sep 17 00:00:00 2001 From: Urvang Joshi Date: Tue, 22 Apr 2014 15:13:14 -0700 Subject: [PATCH] Bugfix: Incremental decode of lossy-alpha When remapping buffer, br->eos_ was wrongly being set to true for certain images. Also, refactored the end-of-stream detection as a function. Reported in http://crbug.com/364830 Change-Id: I716ce082ef2b505fe24246b9c14912d8e97b5d84 --- src/utils/bit_reader.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/utils/bit_reader.c b/src/utils/bit_reader.c index bfa4d7d2..79f64d36 100644 --- a/src/utils/bit_reader.c +++ b/src/utils/bit_reader.c @@ -141,14 +141,24 @@ void VP8LInitBitReader(VP8LBitReader* const br, } } +// Special version that assumes br->pos_ <= br_len_. +static int IsEndOfStreamSpecial(const VP8LBitReader* const br) { + assert(br->pos_ <= br->len_); + return br->pos_ == br->len_ && br->bit_pos_ >= LBITS; +} + +static int IsEndOfStream(const VP8LBitReader* const br) { + return (br->pos_ > br->len_) || IsEndOfStreamSpecial(br); +} + void VP8LBitReaderSetBuffer(VP8LBitReader* const br, const uint8_t* const buf, size_t len) { assert(br != NULL); assert(buf != NULL); assert(len < 0xfffffff8u); // can't happen with a RIFF chunk. - br->eos_ = (br->pos_ >= len); br->buf_ = buf; br->len_ = len; + br->eos_ = IsEndOfStream(br); } // If not at EOS, reload up to LBITS byte-by-byte @@ -175,9 +185,7 @@ void VP8LFillBitWindow(VP8LBitReader* const br) { } #endif ShiftBytes(br); // Slow path. - if (br->pos_ == br->len_ && br->bit_pos_ >= LBITS) { - br->eos_ = 1; - } + br->eos_ = IsEndOfStreamSpecial(br); } } @@ -190,11 +198,7 @@ uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) { const int new_bits = br->bit_pos_ + n_bits; br->bit_pos_ = new_bits; // If this read is going to cross the read buffer, set the eos flag. - if (br->pos_ == br->len_) { - if (new_bits >= LBITS) { - br->eos_ = 1; - } - } + br->eos_ = IsEndOfStreamSpecial(br); ShiftBytes(br); return val; } else {