2011-02-19 08:33:46 +01:00
|
|
|
// Copyright 2011 Google Inc.
|
|
|
|
//
|
|
|
|
// This code is licensed under the same terms as WebM:
|
|
|
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
|
|
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Header syntax writing
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2011-12-01 07:44:15 +01:00
|
|
|
#include "./vp8enci.h"
|
|
|
|
#include "../dec/webpi.h" // For chunk-size constants.
|
|
|
|
#include "../webp/mux.h" // For 'ALPHA_FLAG' constant.
|
2011-02-19 08:33:46 +01:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define KSIGNATURE 0x9d012a
|
|
|
|
#define MAX_PARTITION0_SIZE (1 << 19) // max size of mode partition
|
|
|
|
#define MAX_PARTITION_SIZE (1 << 24) // max size for token partition
|
|
|
|
|
|
|
|
|
2011-12-01 07:44:15 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Helper functions
|
2011-02-19 08:33:46 +01:00
|
|
|
|
|
|
|
static void PutLE32(uint8_t* const data, uint32_t val) {
|
|
|
|
data[0] = (val >> 0) & 0xff;
|
|
|
|
data[1] = (val >> 8) & 0xff;
|
|
|
|
data[2] = (val >> 16) & 0xff;
|
|
|
|
data[3] = (val >> 24) & 0xff;
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:44:15 +01:00
|
|
|
static int IsVP8XNeeded(const VP8Encoder* const enc) {
|
|
|
|
return !!enc->has_alpha_; // Currently the only case when VP8X is needed.
|
|
|
|
// This could change in the future.
|
|
|
|
}
|
|
|
|
|
|
|
|
static int PutPaddingByte(const WebPPicture* const pic) {
|
|
|
|
|
|
|
|
const uint8_t pad_byte[1] = { 0 };
|
|
|
|
return !!pic->writer(pad_byte, 1, pic);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Writers for header's various pieces (in order of appearance)
|
|
|
|
|
|
|
|
static WebPEncodingError PutRIFFHeader(const VP8Encoder* const enc,
|
|
|
|
size_t riff_size) {
|
|
|
|
const WebPPicture* const pic = enc->pic_;
|
|
|
|
uint8_t riff[RIFF_HEADER_SIZE] = {
|
|
|
|
'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'E', 'B', 'P'
|
2011-02-19 08:33:46 +01:00
|
|
|
};
|
2011-12-01 07:44:15 +01:00
|
|
|
PutLE32(riff + TAG_SIZE, riff_size);
|
|
|
|
if (!pic->writer(riff, sizeof(riff), pic)) {
|
|
|
|
return VP8_ENC_ERROR_BAD_WRITE;
|
|
|
|
}
|
|
|
|
return VP8_ENC_OK;
|
|
|
|
}
|
2011-02-19 08:33:46 +01:00
|
|
|
|
2011-12-01 07:44:15 +01:00
|
|
|
static WebPEncodingError PutVP8XHeader(const VP8Encoder* const enc) {
|
|
|
|
const WebPPicture* const pic = enc->pic_;
|
|
|
|
uint8_t vp8x[CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE] = {
|
2011-12-01 22:05:31 +01:00
|
|
|
'V', 'P', '8', 'X'
|
2011-12-01 07:44:15 +01:00
|
|
|
};
|
|
|
|
uint32_t flags = 0;
|
|
|
|
|
|
|
|
assert(IsVP8XNeeded(enc));
|
|
|
|
|
|
|
|
if (enc->has_alpha_) {
|
|
|
|
flags |= ALPHA_FLAG;
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
2011-12-01 22:05:31 +01:00
|
|
|
PutLE32(vp8x + TAG_SIZE, VP8X_CHUNK_SIZE);
|
|
|
|
PutLE32(vp8x + CHUNK_HEADER_SIZE, flags);
|
|
|
|
PutLE32(vp8x + CHUNK_HEADER_SIZE + 4, pic->width);
|
|
|
|
PutLE32(vp8x + CHUNK_HEADER_SIZE + 8, pic->height);
|
2011-12-01 07:44:15 +01:00
|
|
|
if(!pic->writer(vp8x, sizeof(vp8x), pic)) {
|
|
|
|
return VP8_ENC_ERROR_BAD_WRITE;
|
2011-06-28 16:02:56 +02:00
|
|
|
}
|
2011-12-01 07:44:15 +01:00
|
|
|
return VP8_ENC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WebPEncodingError PutAlphaChunk(const VP8Encoder* const enc) {
|
|
|
|
const WebPPicture* const pic = enc->pic_;
|
|
|
|
uint8_t alpha_chunk_hdr[CHUNK_HEADER_SIZE] = {
|
2011-12-01 22:05:31 +01:00
|
|
|
'A', 'L', 'P', 'H'
|
2011-12-01 07:44:15 +01:00
|
|
|
};
|
2011-06-28 16:02:56 +02:00
|
|
|
|
2011-12-01 07:44:15 +01:00
|
|
|
assert(enc->has_alpha_);
|
|
|
|
|
|
|
|
// Alpha chunk header.
|
|
|
|
PutLE32(alpha_chunk_hdr + TAG_SIZE, enc->alpha_data_size_);
|
|
|
|
if (!pic->writer(alpha_chunk_hdr, sizeof(alpha_chunk_hdr), pic)) {
|
|
|
|
return VP8_ENC_ERROR_BAD_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alpha chunk data.
|
|
|
|
if (!pic->writer(enc->alpha_data_, enc->alpha_data_size_, pic)) {
|
|
|
|
return VP8_ENC_ERROR_BAD_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Padding.
|
|
|
|
if ((enc->alpha_data_size_ & 1) && !PutPaddingByte(pic)) {
|
|
|
|
return VP8_ENC_ERROR_BAD_WRITE;
|
|
|
|
}
|
|
|
|
return VP8_ENC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WebPEncodingError PutVP8Header(const WebPPicture* const pic,
|
|
|
|
size_t vp8_size) {
|
|
|
|
uint8_t vp8_chunk_hdr[CHUNK_HEADER_SIZE] = {
|
2011-12-01 22:05:31 +01:00
|
|
|
'V', 'P', '8', ' '
|
2011-12-01 07:44:15 +01:00
|
|
|
};
|
|
|
|
PutLE32(vp8_chunk_hdr + TAG_SIZE, vp8_size);
|
|
|
|
if (!pic->writer(vp8_chunk_hdr, sizeof(vp8_chunk_hdr), pic)) {
|
|
|
|
return VP8_ENC_ERROR_BAD_WRITE;
|
|
|
|
}
|
|
|
|
return VP8_ENC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WebPEncodingError PutVP8FrameHeader(const WebPPicture* const pic,
|
|
|
|
int profile, size_t size0) {
|
|
|
|
uint8_t vp8_frm_hdr[VP8_FRAME_HEADER_SIZE];
|
|
|
|
uint32_t bits;
|
|
|
|
|
|
|
|
if (size0 >= MAX_PARTITION0_SIZE) { // partition #0 is too big to fit
|
|
|
|
return VP8_ENC_ERROR_PARTITION0_OVERFLOW;
|
2011-06-02 15:55:03 +02:00
|
|
|
}
|
2011-02-19 08:33:46 +01:00
|
|
|
|
2011-12-01 22:05:31 +01:00
|
|
|
// Paragraph 9.1.
|
2011-06-28 16:02:56 +02:00
|
|
|
bits = 0 // keyframe (1b)
|
|
|
|
| (profile << 1) // profile (3b)
|
|
|
|
| (1 << 4) // visible (1b)
|
2011-12-01 22:05:31 +01:00
|
|
|
| ((uint32_t)size0 << 5); // partition length (19b)
|
2011-12-01 07:44:15 +01:00
|
|
|
vp8_frm_hdr[0] = bits & 0xff;
|
|
|
|
vp8_frm_hdr[1] = (bits >> 8) & 0xff;
|
|
|
|
vp8_frm_hdr[2] = (bits >> 16) & 0xff;
|
2011-02-19 08:33:46 +01:00
|
|
|
// signature
|
2011-12-01 07:44:15 +01:00
|
|
|
vp8_frm_hdr[3] = (KSIGNATURE >> 16) & 0xff;
|
|
|
|
vp8_frm_hdr[4] = (KSIGNATURE >> 8) & 0xff;
|
|
|
|
vp8_frm_hdr[5] = (KSIGNATURE >> 0) & 0xff;
|
2011-02-19 08:33:46 +01:00
|
|
|
// dimensions
|
2011-12-01 07:44:15 +01:00
|
|
|
vp8_frm_hdr[6] = pic->width & 0xff;
|
|
|
|
vp8_frm_hdr[7] = pic->width >> 8;
|
|
|
|
vp8_frm_hdr[8] = pic->height & 0xff;
|
|
|
|
vp8_frm_hdr[9] = pic->height >> 8;
|
2011-02-19 08:33:46 +01:00
|
|
|
|
2011-12-01 07:44:15 +01:00
|
|
|
if (!pic->writer(vp8_frm_hdr, sizeof(vp8_frm_hdr), pic)) {
|
|
|
|
return VP8_ENC_ERROR_BAD_WRITE;
|
|
|
|
}
|
|
|
|
return VP8_ENC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebP Headers.
|
|
|
|
static int PutWebPHeaders(const VP8Encoder* const enc, size_t size0,
|
|
|
|
size_t vp8_size, size_t riff_size) {
|
|
|
|
WebPPicture* const pic = enc->pic_;
|
|
|
|
WebPEncodingError err = VP8_ENC_OK;
|
|
|
|
|
|
|
|
// RIFF header.
|
|
|
|
err = PutRIFFHeader(enc, riff_size);
|
|
|
|
if (err != VP8_ENC_OK) goto Error;
|
|
|
|
|
|
|
|
// VP8X.
|
|
|
|
if (IsVP8XNeeded(enc)) {
|
|
|
|
err = PutVP8XHeader(enc);
|
|
|
|
if (err != VP8_ENC_OK) goto Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alpha.
|
|
|
|
if (enc->has_alpha_) {
|
|
|
|
err = PutAlphaChunk(enc);
|
|
|
|
if (err != VP8_ENC_OK) goto Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// VP8 header.
|
|
|
|
err = PutVP8Header(pic, vp8_size);
|
|
|
|
if (err != VP8_ENC_OK) goto Error;
|
|
|
|
|
|
|
|
// VP8 frame header.
|
|
|
|
err = PutVP8FrameHeader(pic, enc->profile_, size0);
|
|
|
|
if (err != VP8_ENC_OK) goto Error;
|
|
|
|
|
|
|
|
// All OK.
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Error.
|
|
|
|
Error:
|
|
|
|
return WebPEncodingSetError(pic, err);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Segmentation header
|
|
|
|
static void PutSegmentHeader(VP8BitWriter* const bw,
|
|
|
|
const VP8Encoder* const enc) {
|
|
|
|
const VP8SegmentHeader* const hdr = &enc->segment_hdr_;
|
|
|
|
const VP8Proba* const proba = &enc->proba_;
|
|
|
|
if (VP8PutBitUniform(bw, (hdr->num_segments_ > 1))) {
|
|
|
|
// We always 'update' the quant and filter strength values
|
|
|
|
const int update_data = 1;
|
|
|
|
int s;
|
|
|
|
VP8PutBitUniform(bw, hdr->update_map_);
|
|
|
|
if (VP8PutBitUniform(bw, update_data)) {
|
2011-02-27 19:22:54 +01:00
|
|
|
// we always use absolute values, not relative ones
|
|
|
|
VP8PutBitUniform(bw, 1); // (segment_feature_mode = 1. Paragraph 9.3.)
|
2011-02-19 08:33:46 +01:00
|
|
|
for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
|
2011-02-27 19:22:54 +01:00
|
|
|
VP8PutSignedValue(bw, enc->dqm_[s].quant_, 7);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
|
2011-02-27 19:22:54 +01:00
|
|
|
VP8PutSignedValue(bw, enc->dqm_[s].fstrength_, 6);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hdr->update_map_) {
|
|
|
|
for (s = 0; s < 3; ++s) {
|
|
|
|
if (VP8PutBitUniform(bw, (proba->segments_[s] != 255u))) {
|
|
|
|
VP8PutValue(bw, proba->segments_[s], 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filtering parameters header
|
|
|
|
static void PutFilterHeader(VP8BitWriter* const bw,
|
|
|
|
const VP8FilterHeader* const hdr) {
|
|
|
|
const int use_lf_delta = (hdr->i4x4_lf_delta_ != 0);
|
|
|
|
VP8PutBitUniform(bw, hdr->simple_);
|
|
|
|
VP8PutValue(bw, hdr->level_, 6);
|
|
|
|
VP8PutValue(bw, hdr->sharpness_, 3);
|
|
|
|
if (VP8PutBitUniform(bw, use_lf_delta)) {
|
|
|
|
// '0' is the default value for i4x4_lf_delta_ at frame #0.
|
|
|
|
const int need_update = (hdr->i4x4_lf_delta_ != 0);
|
|
|
|
if (VP8PutBitUniform(bw, need_update)) {
|
|
|
|
// we don't use ref_lf_delta => emit four 0 bits
|
|
|
|
VP8PutValue(bw, 0, 4);
|
|
|
|
// we use mode_lf_delta for i4x4
|
|
|
|
VP8PutSignedValue(bw, hdr->i4x4_lf_delta_, 6);
|
|
|
|
VP8PutValue(bw, 0, 3); // all others unused
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nominal quantization parameters
|
|
|
|
static void PutQuant(VP8BitWriter* const bw,
|
|
|
|
const VP8Encoder* const enc) {
|
|
|
|
VP8PutValue(bw, enc->base_quant_, 7);
|
|
|
|
VP8PutSignedValue(bw, enc->dq_y1_dc_, 4);
|
|
|
|
VP8PutSignedValue(bw, enc->dq_y2_dc_, 4);
|
|
|
|
VP8PutSignedValue(bw, enc->dq_y2_ac_, 4);
|
|
|
|
VP8PutSignedValue(bw, enc->dq_uv_dc_, 4);
|
|
|
|
VP8PutSignedValue(bw, enc->dq_uv_ac_, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Partition sizes
|
|
|
|
static int EmitPartitionsSize(const VP8Encoder* const enc,
|
2011-06-02 15:55:03 +02:00
|
|
|
WebPPicture* const pic) {
|
2011-02-19 08:33:46 +01:00
|
|
|
uint8_t buf[3 * (MAX_NUM_PARTITIONS - 1)];
|
|
|
|
int p;
|
|
|
|
for (p = 0; p < enc->num_parts_ - 1; ++p) {
|
|
|
|
const size_t part_size = VP8BitWriterSize(enc->parts_ + p);
|
|
|
|
if (part_size >= MAX_PARTITION_SIZE) {
|
2011-06-02 15:55:03 +02:00
|
|
|
return WebPEncodingSetError(pic, VP8_ENC_ERROR_PARTITION_OVERFLOW);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
buf[3 * p + 0] = (part_size >> 0) & 0xff;
|
|
|
|
buf[3 * p + 1] = (part_size >> 8) & 0xff;
|
|
|
|
buf[3 * p + 2] = (part_size >> 16) & 0xff;
|
|
|
|
}
|
|
|
|
return p ? pic->writer(buf, 3 * p, pic) : 1;
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-02-19 08:33:46 +01:00
|
|
|
|
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
|
|
|
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
2011-05-03 02:19:00 +02:00
|
|
|
|
2011-06-16 22:33:07 +02:00
|
|
|
#define KTRAILER_SIZE 8
|
|
|
|
|
2011-05-03 02:19:00 +02:00
|
|
|
static void PutLE24(uint8_t* buf, size_t value) {
|
|
|
|
buf[0] = (value >> 0) & 0xff;
|
|
|
|
buf[1] = (value >> 8) & 0xff;
|
|
|
|
buf[2] = (value >> 16) & 0xff;
|
|
|
|
}
|
|
|
|
|
2011-04-29 01:03:34 +02:00
|
|
|
static int WriteExtensions(VP8Encoder* const enc) {
|
2011-06-16 22:33:07 +02:00
|
|
|
uint8_t buffer[KTRAILER_SIZE];
|
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
|
|
|
VP8BitWriter* const bw = &enc->bw_;
|
2011-06-02 15:55:03 +02:00
|
|
|
WebPPicture* const pic = enc->pic_;
|
2011-05-03 02:19:00 +02:00
|
|
|
|
|
|
|
// Layer (bytes 0..3)
|
|
|
|
PutLE24(buffer + 0, enc->layer_data_size_);
|
|
|
|
buffer[3] = enc->pic_->colorspace & WEBP_CSP_UV_MASK;
|
|
|
|
if (enc->layer_data_size_ > 0) {
|
|
|
|
assert(enc->use_layer_);
|
|
|
|
// append layer data to last partition
|
|
|
|
if (!VP8BitWriterAppend(&enc->parts_[enc->num_parts_ - 1],
|
|
|
|
enc->layer_data_, enc->layer_data_size_)) {
|
2011-06-02 15:55:03 +02:00
|
|
|
return WebPEncodingSetError(pic, VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY);
|
2011-05-03 02:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-16 22:33:07 +02:00
|
|
|
buffer[KTRAILER_SIZE - 1] = 0x01; // marker
|
|
|
|
if (!VP8BitWriterAppend(bw, buffer, KTRAILER_SIZE)) {
|
2011-06-02 15:55:03 +02:00
|
|
|
return WebPEncodingSetError(pic, VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY);
|
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
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2011-06-02 15:55:03 +02:00
|
|
|
|
|
|
|
#endif /* WEBP_EXPERIMENTAL_FEATURES */
|
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
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
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
|
|
|
|
2011-02-19 08:33:46 +01:00
|
|
|
static size_t GeneratePartition0(VP8Encoder* const enc) {
|
|
|
|
VP8BitWriter* const bw = &enc->bw_;
|
|
|
|
const int mb_size = enc->mb_w_ * enc->mb_h_;
|
|
|
|
uint64_t pos1, pos2, pos3;
|
2011-04-29 01:31:14 +02:00
|
|
|
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
2011-12-01 07:44:15 +01:00
|
|
|
const int need_extensions = enc->use_layer_;
|
2011-04-29 01:31:14 +02:00
|
|
|
#endif
|
2011-02-19 08:33:46 +01:00
|
|
|
|
|
|
|
pos1 = VP8BitWriterPos(bw);
|
|
|
|
VP8BitWriterInit(bw, mb_size * 7 / 8); // ~7 bits per macroblock
|
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
|
|
|
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
|
|
|
VP8PutBitUniform(bw, need_extensions); // extensions
|
|
|
|
#else
|
2011-02-19 08:33:46 +01:00
|
|
|
VP8PutBitUniform(bw, 0); // colorspace
|
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
|
|
|
#endif
|
2011-02-19 08:33:46 +01:00
|
|
|
VP8PutBitUniform(bw, 0); // clamp type
|
|
|
|
|
|
|
|
PutSegmentHeader(bw, enc);
|
|
|
|
PutFilterHeader(bw, &enc->filter_hdr_);
|
|
|
|
VP8PutValue(bw, enc->config_->partitions, 2);
|
|
|
|
PutQuant(bw, enc);
|
|
|
|
VP8PutBitUniform(bw, 0); // no proba update
|
|
|
|
VP8WriteProbas(bw, &enc->proba_);
|
|
|
|
pos2 = VP8BitWriterPos(bw);
|
|
|
|
VP8CodeIntraModes(enc);
|
|
|
|
VP8BitWriterFinish(bw);
|
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
|
|
|
|
2011-04-29 01:03:34 +02:00
|
|
|
#ifdef WEBP_EXPERIMENTAL_FEATURES
|
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 (need_extensions && !WriteExtensions(enc)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-04-29 01:03:34 +02:00
|
|
|
#endif
|
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
|
|
|
|
2011-02-19 08:33:46 +01:00
|
|
|
pos3 = VP8BitWriterPos(bw);
|
|
|
|
|
|
|
|
if (enc->pic_->stats) {
|
|
|
|
enc->pic_->stats->header_bytes[0] = (int)((pos2 - pos1 + 7) >> 3);
|
|
|
|
enc->pic_->stats->header_bytes[1] = (int)((pos3 - pos2 + 7) >> 3);
|
2011-06-28 16:02:56 +02:00
|
|
|
enc->pic_->stats->alpha_data_size = (int)enc->alpha_data_size_;
|
|
|
|
enc->pic_->stats->layer_data_size = (int)enc->layer_data_size_;
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
return !bw->error_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VP8EncWrite(VP8Encoder* const enc) {
|
|
|
|
WebPPicture* const pic = enc->pic_;
|
|
|
|
VP8BitWriter* const bw = &enc->bw_;
|
|
|
|
int ok = 0;
|
2011-12-01 07:44:15 +01:00
|
|
|
size_t vp8_size, pad, riff_size;
|
2011-02-19 08:33:46 +01:00
|
|
|
int p;
|
|
|
|
|
|
|
|
// Partition #0 with header and partition sizes
|
2011-06-28 16:02:56 +02:00
|
|
|
ok = !!GeneratePartition0(enc);
|
2011-02-19 08:33:46 +01:00
|
|
|
|
2011-12-01 07:44:15 +01:00
|
|
|
// Compute VP8 size
|
|
|
|
vp8_size = VP8_FRAME_HEADER_SIZE +
|
|
|
|
VP8BitWriterSize(bw) +
|
|
|
|
3 * (enc->num_parts_ - 1);
|
2011-02-19 08:33:46 +01:00
|
|
|
for (p = 0; p < enc->num_parts_; ++p) {
|
2011-12-01 07:44:15 +01:00
|
|
|
vp8_size += VP8BitWriterSize(enc->parts_ + p);
|
|
|
|
}
|
|
|
|
pad = vp8_size & 1;
|
|
|
|
vp8_size += pad;
|
|
|
|
|
2011-12-01 22:05:31 +01:00
|
|
|
// Compute RIFF size
|
2011-12-01 07:44:15 +01:00
|
|
|
// At the minimum it is: "WEBPVP8 nnnn" + VP8 data size.
|
|
|
|
riff_size = TAG_SIZE + CHUNK_HEADER_SIZE + vp8_size;
|
|
|
|
if (IsVP8XNeeded(enc)) { // Add size for: VP8X header + data.
|
|
|
|
riff_size += CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
|
|
|
|
}
|
|
|
|
if (enc->has_alpha_) { // Add size for: ALPH header + data.
|
|
|
|
const uint32_t padded_alpha_size = enc->alpha_data_size_ +
|
|
|
|
(enc->alpha_data_size_ & 1);
|
|
|
|
riff_size += CHUNK_HEADER_SIZE + padded_alpha_size;
|
|
|
|
}
|
|
|
|
// Sanity check.
|
|
|
|
if (riff_size > 0xfffffffeU) {
|
|
|
|
return WebPEncodingSetError(pic, VP8_ENC_ERROR_FILE_TOO_BIG);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit headers and partition #0
|
|
|
|
{
|
|
|
|
const uint8_t* const part0 = VP8BitWriterBuf(bw);
|
|
|
|
const size_t size0 = VP8BitWriterSize(bw);
|
2011-12-01 07:44:15 +01:00
|
|
|
ok = ok && PutWebPHeaders(enc, size0, vp8_size, riff_size)
|
2011-02-19 08:33:46 +01:00
|
|
|
&& pic->writer(part0, size0, pic)
|
|
|
|
&& EmitPartitionsSize(enc, pic);
|
|
|
|
free((void*)part0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token partitions
|
|
|
|
for (p = 0; p < enc->num_parts_; ++p) {
|
|
|
|
const uint8_t* const buf = VP8BitWriterBuf(enc->parts_ + p);
|
|
|
|
const size_t size = VP8BitWriterSize(enc->parts_ + p);
|
|
|
|
if (size)
|
|
|
|
ok = ok && pic->writer(buf, size, pic);
|
|
|
|
free((void*)buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Padding byte
|
|
|
|
if (ok && pad) {
|
2011-12-01 07:44:15 +01:00
|
|
|
ok = PutPaddingByte(pic);
|
2011-02-19 08:33:46 +01:00
|
|
|
}
|
|
|
|
|
2011-12-01 07:44:15 +01:00
|
|
|
enc->coded_size_ = (int)(CHUNK_HEADER_SIZE + riff_size);
|
2011-02-19 08:33:46 +01:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-02-19 08:33:46 +01:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
2011-06-21 00:47:35 +02:00
|
|
|
#endif
|