2012-01-06 23:49:06 +01:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
2011-02-19 08:33:46 +01:00
|
|
|
//
|
2013-06-07 08:05:58 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2011-02-19 08:33:46 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Bit writing and boolean coder
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
2012-03-28 13:07:42 +02:00
|
|
|
// Vikas Arora (vikaas.arora@gmail.com)
|
2011-02-19 08:33:46 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
2011-09-07 11:26:35 +02:00
|
|
|
#include <string.h> // for memcpy()
|
2011-02-19 08:33:46 +01:00
|
|
|
#include <stdlib.h>
|
2014-03-27 23:27:32 +01:00
|
|
|
|
2011-09-07 11:26:35 +02:00
|
|
|
#include "./bit_writer.h"
|
2014-07-03 19:41:37 +02:00
|
|
|
#include "./endian_inl.h"
|
2014-03-27 23:27:32 +01:00
|
|
|
#include "./utils.h"
|
2011-02-19 08:33:46 +01:00
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-02-19 08:33:46 +01:00
|
|
|
// VP8BitWriter
|
|
|
|
|
|
|
|
static int BitWriterResize(VP8BitWriter* const bw, size_t extra_size) {
|
|
|
|
uint8_t* new_buf;
|
|
|
|
size_t new_size;
|
2012-08-01 09:37:24 +02:00
|
|
|
const uint64_t needed_size_64b = (uint64_t)bw->pos_ + extra_size;
|
|
|
|
const size_t needed_size = (size_t)needed_size_64b;
|
|
|
|
if (needed_size_64b != needed_size) {
|
|
|
|
bw->error_ = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-19 08:33:46 +01:00
|
|
|
if (needed_size <= bw->max_pos_) return 1;
|
2012-08-01 09:37:24 +02:00
|
|
|
// If the following line wraps over 32bit, the test just after will catch it.
|
2011-02-19 08:33:46 +01:00
|
|
|
new_size = 2 * bw->max_pos_;
|
2012-08-01 09:37:24 +02:00
|
|
|
if (new_size < needed_size) new_size = needed_size;
|
2011-02-19 08:33:46 +01:00
|
|
|
if (new_size < 1024) new_size = 1024;
|
2014-03-27 23:27:32 +01:00
|
|
|
new_buf = (uint8_t*)WebPSafeMalloc(1ULL, new_size);
|
2011-02-19 08:33:46 +01:00
|
|
|
if (new_buf == NULL) {
|
|
|
|
bw->error_ = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-12 09:32:28 +02:00
|
|
|
if (bw->pos_ > 0) {
|
|
|
|
assert(bw->buf_ != NULL);
|
|
|
|
memcpy(new_buf, bw->buf_, bw->pos_);
|
|
|
|
}
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(bw->buf_);
|
2011-02-19 08:33:46 +01:00
|
|
|
bw->buf_ = new_buf;
|
|
|
|
bw->max_pos_ = new_size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-03 03:42:16 +02:00
|
|
|
static void Flush(VP8BitWriter* const bw) {
|
2011-02-19 08:33:46 +01:00
|
|
|
const int s = 8 + bw->nb_bits_;
|
|
|
|
const int32_t bits = bw->value_ >> s;
|
|
|
|
assert(bw->nb_bits_ >= 0);
|
|
|
|
bw->value_ -= bits << s;
|
|
|
|
bw->nb_bits_ -= 8;
|
|
|
|
if ((bits & 0xff) != 0xff) {
|
|
|
|
size_t pos = bw->pos_;
|
2012-08-01 09:37:24 +02:00
|
|
|
if (!BitWriterResize(bw, bw->run_ + 1)) {
|
|
|
|
return;
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
if (bits & 0x100) { // overflow -> propagate carry over pending 0xff's
|
|
|
|
if (pos > 0) bw->buf_[pos - 1]++;
|
|
|
|
}
|
|
|
|
if (bw->run_ > 0) {
|
|
|
|
const int value = (bits & 0x100) ? 0x00 : 0xff;
|
|
|
|
for (; bw->run_ > 0; --bw->run_) bw->buf_[pos++] = value;
|
|
|
|
}
|
|
|
|
bw->buf_[pos++] = bits;
|
|
|
|
bw->pos_ = pos;
|
|
|
|
} else {
|
|
|
|
bw->run_++; // delay writing of bytes 0xff, pending eventual carry.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-02-19 08:33:46 +01:00
|
|
|
// renormalization
|
|
|
|
|
|
|
|
static const uint8_t kNorm[128] = { // renorm_sizes[i] = 8 - log2(i)
|
|
|
|
7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
// range = ((range + 1) << kVP8Log2Range[range]) - 1
|
2011-09-02 06:54:29 +02:00
|
|
|
static const uint8_t kNewRange[128] = {
|
2011-02-19 08:33:46 +01:00
|
|
|
127, 127, 191, 127, 159, 191, 223, 127, 143, 159, 175, 191, 207, 223, 239,
|
|
|
|
127, 135, 143, 151, 159, 167, 175, 183, 191, 199, 207, 215, 223, 231, 239,
|
|
|
|
247, 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179,
|
|
|
|
183, 187, 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239,
|
|
|
|
243, 247, 251, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149,
|
|
|
|
151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179,
|
|
|
|
181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209,
|
|
|
|
211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239,
|
|
|
|
241, 243, 245, 247, 249, 251, 253, 127
|
|
|
|
};
|
|
|
|
|
|
|
|
int VP8PutBit(VP8BitWriter* const bw, int bit, int prob) {
|
|
|
|
const int split = (bw->range_ * prob) >> 8;
|
|
|
|
if (bit) {
|
|
|
|
bw->value_ += split + 1;
|
|
|
|
bw->range_ -= split + 1;
|
|
|
|
} else {
|
|
|
|
bw->range_ = split;
|
|
|
|
}
|
|
|
|
if (bw->range_ < 127) { // emit 'shift' bits out and renormalize
|
|
|
|
const int shift = kNorm[bw->range_];
|
|
|
|
bw->range_ = kNewRange[bw->range_];
|
|
|
|
bw->value_ <<= shift;
|
|
|
|
bw->nb_bits_ += shift;
|
2014-08-03 03:42:16 +02:00
|
|
|
if (bw->nb_bits_ > 0) Flush(bw);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VP8PutBitUniform(VP8BitWriter* const bw, int bit) {
|
|
|
|
const int split = bw->range_ >> 1;
|
|
|
|
if (bit) {
|
|
|
|
bw->value_ += split + 1;
|
|
|
|
bw->range_ -= split + 1;
|
|
|
|
} else {
|
|
|
|
bw->range_ = split;
|
|
|
|
}
|
|
|
|
if (bw->range_ < 127) {
|
|
|
|
bw->range_ = kNewRange[bw->range_];
|
|
|
|
bw->value_ <<= 1;
|
|
|
|
bw->nb_bits_ += 1;
|
2014-08-03 03:42:16 +02:00
|
|
|
if (bw->nb_bits_ > 0) Flush(bw);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:35:16 +02:00
|
|
|
void VP8PutBits(VP8BitWriter* const bw, uint32_t value, int nb_bits) {
|
|
|
|
uint32_t mask;
|
|
|
|
assert(nb_bits > 0 && nb_bits < 32);
|
|
|
|
for (mask = 1u << (nb_bits - 1); mask; mask >>= 1)
|
2011-02-19 08:33:46 +01:00
|
|
|
VP8PutBitUniform(bw, value & mask);
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:35:16 +02:00
|
|
|
void VP8PutSignedBits(VP8BitWriter* const bw, int value, int nb_bits) {
|
2011-02-19 08:33:46 +01:00
|
|
|
if (!VP8PutBitUniform(bw, value != 0))
|
|
|
|
return;
|
|
|
|
if (value < 0) {
|
2014-10-23 15:35:16 +02:00
|
|
|
VP8PutBits(bw, ((-value) << 1) | 1, nb_bits + 1);
|
2011-02-19 08:33:46 +01:00
|
|
|
} else {
|
2014-10-23 15:35:16 +02:00
|
|
|
VP8PutBits(bw, value << 1, nb_bits + 1);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-02-19 08:33:46 +01:00
|
|
|
|
|
|
|
int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size) {
|
|
|
|
bw->range_ = 255 - 1;
|
|
|
|
bw->value_ = 0;
|
|
|
|
bw->run_ = 0;
|
|
|
|
bw->nb_bits_ = -8;
|
|
|
|
bw->pos_ = 0;
|
|
|
|
bw->max_pos_ = 0;
|
|
|
|
bw->error_ = 0;
|
|
|
|
bw->buf_ = NULL;
|
|
|
|
return (expected_size > 0) ? BitWriterResize(bw, expected_size) : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) {
|
2014-10-23 15:35:16 +02:00
|
|
|
VP8PutBits(bw, 0, 9 - bw->nb_bits_);
|
2011-02-19 08:33:46 +01:00
|
|
|
bw->nb_bits_ = 0; // pad with zeroes
|
2014-08-03 03:42:16 +02:00
|
|
|
Flush(bw);
|
2011-02-19 08:33:46 +01:00
|
|
|
return bw->buf_;
|
|
|
|
}
|
|
|
|
|
EXPERIMENTAL: add support for alpha channel
This is a (minor) bitstream change: if the 'color_space' bit is set to '1'
(which is normally an undefined/invalid behaviour), we add extra data at the
end of partition #0 (so-called 'extensions')
Namely, we add the size of the extension data as 3 bytes (little-endian),
followed by a set of bits telling which extensions we're incorporating.
The data then _preceeds_ this trailing tags.
This is all experimental, and you'll need to have
'#define WEBP_EXPERIMENTAL_FEATURES' in webp/types.h to enable this code
(at your own risk! :))
Still, this hack produces almost-valid WebP file for decoders that don't
check this color_space bit. In particular, previous 'dwebp' (and for instance
Chrome) will recognize this files and decode them, but without the alpha
of course. Other decoder will just see random extra stuff at the end of
partition #0.
To experiment with the alpha-channel, you need to compile on Unix platform
and use PNGs for input/output.
If 'alpha.png' is a source with alpha channel, then you can try (on Unix):
cwebp alpha.png -o alpha.webp
dwebp alpha.webp -o test.png
cwebp now has a '-noalpha' flag to ignore any alpha information from the
source, if present.
More hacking and experimenting welcome!
Change-Id: I3c7b1fd8411c9e7a9f77690e898479ad85c52f3e
2011-04-26 01:58:04 +02:00
|
|
|
int VP8BitWriterAppend(VP8BitWriter* const bw,
|
|
|
|
const uint8_t* data, size_t size) {
|
2014-03-27 23:27:32 +01:00
|
|
|
assert(data != NULL);
|
2014-08-03 03:42:16 +02:00
|
|
|
if (bw->nb_bits_ != -8) return 0; // Flush() must have been called
|
EXPERIMENTAL: add support for alpha channel
This is a (minor) bitstream change: if the 'color_space' bit is set to '1'
(which is normally an undefined/invalid behaviour), we add extra data at the
end of partition #0 (so-called 'extensions')
Namely, we add the size of the extension data as 3 bytes (little-endian),
followed by a set of bits telling which extensions we're incorporating.
The data then _preceeds_ this trailing tags.
This is all experimental, and you'll need to have
'#define WEBP_EXPERIMENTAL_FEATURES' in webp/types.h to enable this code
(at your own risk! :))
Still, this hack produces almost-valid WebP file for decoders that don't
check this color_space bit. In particular, previous 'dwebp' (and for instance
Chrome) will recognize this files and decode them, but without the alpha
of course. Other decoder will just see random extra stuff at the end of
partition #0.
To experiment with the alpha-channel, you need to compile on Unix platform
and use PNGs for input/output.
If 'alpha.png' is a source with alpha channel, then you can try (on Unix):
cwebp alpha.png -o alpha.webp
dwebp alpha.webp -o test.png
cwebp now has a '-noalpha' flag to ignore any alpha information from the
source, if present.
More hacking and experimenting welcome!
Change-Id: I3c7b1fd8411c9e7a9f77690e898479ad85c52f3e
2011-04-26 01:58:04 +02:00
|
|
|
if (!BitWriterResize(bw, size)) return 0;
|
|
|
|
memcpy(bw->buf_ + bw->pos_, data, size);
|
|
|
|
bw->pos_ += size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-12-01 11:24:50 +01:00
|
|
|
void VP8BitWriterWipeOut(VP8BitWriter* const bw) {
|
2014-03-27 23:27:32 +01:00
|
|
|
if (bw != NULL) {
|
|
|
|
WebPSafeFree(bw->buf_);
|
2011-12-01 11:24:50 +01:00
|
|
|
memset(bw, 0, sizeof(*bw));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:07:42 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// VP8LBitWriter
|
|
|
|
|
2014-02-11 18:12:45 +01:00
|
|
|
// This is the minimum amount of size the memory buffer is guaranteed to grow
|
|
|
|
// when extra space is needed.
|
|
|
|
#define MIN_EXTRA_SIZE (32768ULL)
|
|
|
|
|
|
|
|
#define VP8L_WRITER_BYTES ((int)sizeof(vp8l_wtype_t))
|
|
|
|
#define VP8L_WRITER_BITS (VP8L_WRITER_BYTES * 8)
|
2014-05-22 07:17:24 +02:00
|
|
|
#define VP8L_WRITER_MAX_BITS (8 * (int)sizeof(vp8l_atype_t))
|
2014-02-11 18:12:45 +01:00
|
|
|
|
2012-03-28 13:07:42 +02:00
|
|
|
// Returns 1 on success.
|
|
|
|
static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) {
|
|
|
|
uint8_t* allocated_buf;
|
|
|
|
size_t allocated_size;
|
2014-02-11 18:12:45 +01:00
|
|
|
const size_t max_bytes = bw->end_ - bw->buf_;
|
|
|
|
const size_t current_size = bw->cur_ - bw->buf_;
|
2012-08-01 09:37:24 +02:00
|
|
|
const uint64_t size_required_64b = (uint64_t)current_size + extra_size;
|
|
|
|
const size_t size_required = (size_t)size_required_64b;
|
|
|
|
if (size_required != size_required_64b) {
|
|
|
|
bw->error_ = 1;
|
|
|
|
return 0;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
2014-02-11 18:12:45 +01:00
|
|
|
if (max_bytes > 0 && size_required <= max_bytes) return 1;
|
|
|
|
allocated_size = (3 * max_bytes) >> 1;
|
2012-08-01 09:37:24 +02:00
|
|
|
if (allocated_size < size_required) allocated_size = size_required;
|
|
|
|
// make allocated size multiple of 1k
|
2012-03-28 13:07:42 +02:00
|
|
|
allocated_size = (((allocated_size >> 10) + 1) << 10);
|
2014-03-27 23:27:32 +01:00
|
|
|
allocated_buf = (uint8_t*)WebPSafeMalloc(1ULL, allocated_size);
|
2012-08-01 09:37:24 +02:00
|
|
|
if (allocated_buf == NULL) {
|
|
|
|
bw->error_ = 1;
|
|
|
|
return 0;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
2014-02-11 18:12:45 +01:00
|
|
|
if (current_size > 0) {
|
|
|
|
memcpy(allocated_buf, bw->buf_, current_size);
|
|
|
|
}
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(bw->buf_);
|
2012-03-28 13:07:42 +02:00
|
|
|
bw->buf_ = allocated_buf;
|
2014-02-11 18:12:45 +01:00
|
|
|
bw->cur_ = bw->buf_ + current_size;
|
|
|
|
bw->end_ = bw->buf_ + allocated_size;
|
2012-03-28 13:07:42 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size) {
|
|
|
|
memset(bw, 0, sizeof(*bw));
|
|
|
|
return VP8LBitWriterResize(bw, expected_size);
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:35:16 +02:00
|
|
|
void VP8LBitWriterWipeOut(VP8LBitWriter* const bw) {
|
2012-03-28 13:07:42 +02:00
|
|
|
if (bw != NULL) {
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(bw->buf_);
|
2012-03-28 13:07:42 +02:00
|
|
|
memset(bw, 0, sizeof(*bw));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:35:16 +02:00
|
|
|
void VP8LPutBits(VP8LBitWriter* const bw, uint32_t bits, int n_bits) {
|
2014-05-22 07:17:24 +02:00
|
|
|
assert(n_bits <= 32);
|
|
|
|
// That's the max we can handle:
|
|
|
|
assert(bw->used_ + n_bits <= 2 * VP8L_WRITER_MAX_BITS);
|
|
|
|
if (n_bits > 0) {
|
|
|
|
// Local field copy.
|
|
|
|
vp8l_atype_t lbits = bw->bits_;
|
|
|
|
int used = bw->used_;
|
|
|
|
// Special case of overflow handling for 32bit accumulator (2-steps flush).
|
|
|
|
if (VP8L_WRITER_BITS == 16) {
|
|
|
|
if (used + n_bits >= VP8L_WRITER_MAX_BITS) {
|
|
|
|
// Fill up all the VP8L_WRITER_MAX_BITS so it can be flushed out below.
|
|
|
|
const int shift = VP8L_WRITER_MAX_BITS - used;
|
|
|
|
lbits |= (vp8l_atype_t)bits << used;
|
|
|
|
used = VP8L_WRITER_MAX_BITS;
|
|
|
|
n_bits -= shift;
|
|
|
|
bits >>= shift;
|
|
|
|
assert(n_bits <= VP8L_WRITER_MAX_BITS);
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-22 07:17:24 +02:00
|
|
|
// If needed, make some room by flushing some bits out.
|
|
|
|
while (used >= VP8L_WRITER_BITS) {
|
|
|
|
if (bw->cur_ + VP8L_WRITER_BYTES > bw->end_) {
|
|
|
|
const uint64_t extra_size = (bw->end_ - bw->buf_) + MIN_EXTRA_SIZE;
|
|
|
|
if (extra_size != (size_t)extra_size ||
|
|
|
|
!VP8LBitWriterResize(bw, (size_t)extra_size)) {
|
|
|
|
bw->cur_ = bw->buf_;
|
|
|
|
bw->error_ = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*(vp8l_wtype_t*)bw->cur_ = (vp8l_wtype_t)WSWAP((vp8l_wtype_t)lbits);
|
|
|
|
bw->cur_ += VP8L_WRITER_BYTES;
|
|
|
|
lbits >>= VP8L_WRITER_BITS;
|
|
|
|
used -= VP8L_WRITER_BITS;
|
|
|
|
}
|
|
|
|
// Eventually, insert new bits.
|
|
|
|
bw->bits_ = lbits | ((vp8l_atype_t)bits << used);
|
|
|
|
bw->used_ = used + n_bits;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
2014-02-11 18:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw) {
|
|
|
|
// flush leftover bits
|
|
|
|
if (VP8LBitWriterResize(bw, (bw->used_ + 7) >> 3)) {
|
|
|
|
while (bw->used_ > 0) {
|
2014-02-12 06:17:35 +01:00
|
|
|
*bw->cur_++ = (uint8_t)bw->bits_;
|
2014-02-11 18:12:45 +01:00
|
|
|
bw->bits_ >>= 8;
|
|
|
|
bw->used_ -= 8;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
2014-02-11 18:12:45 +01:00
|
|
|
bw->used_ = 0;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
2014-02-11 18:12:45 +01:00
|
|
|
return bw->buf_;
|
2012-03-28 13:07:42 +02:00
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|