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
This commit is contained in:
Pascal Massimino
2011-04-25 16:58:04 -07:00
parent cfbf88a6c4
commit 2ab4b72f53
27 changed files with 477 additions and 59 deletions

View File

@ -1,7 +1,7 @@
AM_CPPFLAGS = -I$(top_srcdir)/src
libwebpdecode_la_SOURCES = bits.h vp8i.h yuv.h bits.c dsp.c frame.c \
quant.c tree.c vp8.c webp.c yuv.c idec.c
quant.c tree.c vp8.c webp.c yuv.c idec.c alpha.c
libwebpdecode_la_LDFLAGS = -version-info 0:0:0
libwebpdecodeinclude_HEADERS = ../webp/decode.h ../webp/decode_vp8.h ../webp/types.h
libwebpdecodeincludedir = $(includedir)/webp

69
src/dec/alpha.c Normal file
View File

@ -0,0 +1,69 @@
// 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/
// -----------------------------------------------------------------------------
//
// Alpha-plane decompression.
//
// Author: Skal (pascal.massimino@gmail.com)
#include <stdlib.h>
#include "vp8i.h"
#ifdef WEBP_EXPERIMENTAL_FEATURES
#include "zlib.h"
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
//-----------------------------------------------------------------------------
const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
int row, int num_rows) {
uint8_t* output = dec->alpha_plane_;
const int stride = dec->pic_hdr_.width_;
if (row < 0 || row + num_rows > dec->pic_hdr_.height_) {
return NULL; // sanity check
}
if (row == 0) {
// TODO(skal): for now, we just decompress everything during the first call.
// Later, we'll decode progressively, but we need to store the
// z_stream state.
const uint8_t* data = dec->alpha_data_;
size_t data_size = dec->alpha_data_size_;
const size_t output_size = stride * dec->pic_hdr_.height_;
int ret = Z_OK;
z_stream strm;
memset(&strm, 0, sizeof(strm));
if (inflateInit(&strm) != Z_OK) {
return 0;
}
strm.avail_in = data_size;
strm.next_in = (unsigned char*)data;
do {
strm.avail_out = output_size;
strm.next_out = output;
ret = inflate(&strm, Z_NO_FLUSH);
if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
break;
}
} while (strm.avail_out == 0);
inflateEnd(&strm);
if (ret != Z_STREAM_END) {
return NULL; // error
}
}
return output + row * stride;
}
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif
#endif // WEBP_EXPERIMENTAL_FEATURES

View File

@ -33,10 +33,12 @@ int VP8InitFrame(VP8Decoder* const dec, VP8Io* io) {
const int coeffs_size = 384 * sizeof(*dec->coeffs_);
const int cache_height = (16 + kFilterExtraRows[dec->filter_type_]) * 3 / 2;
const int cache_size = top_size * cache_height;
const int alpha_size =
dec->alpha_data_ ? (dec->pic_hdr_.width_ * dec->pic_hdr_.height_) : 0;
const int needed = intra_pred_mode_size
+ top_size + info_size
+ yuv_size + coeffs_size
+ cache_size + ALIGN_MASK;
+ cache_size + alpha_size + ALIGN_MASK;
uint8_t* mem;
if (needed > dec->mem_size_) {
@ -84,6 +86,10 @@ int VP8InitFrame(VP8Decoder* const dec, VP8Io* io) {
}
mem += cache_size;
// alpha plane
dec->alpha_plane_ = alpha_size ? (uint8_t*)mem : NULL;
mem += alpha_size;
// note: left-info is initialized once for all.
memset(dec->mb_info_ - 1, 0, (mb_w + 1) * sizeof(*dec->mb_info_));
@ -100,6 +106,7 @@ int VP8InitFrame(VP8Decoder* const dec, VP8Io* io) {
io->y_stride = dec->cache_y_stride_;
io->uv_stride = dec->cache_uv_stride_;
io->fancy_upscaling = 0; // default
io->a = NULL;
// Init critical function pointers and look-up tables.
VP8DspInitTables();
@ -250,6 +257,16 @@ int VP8FinishRow(VP8Decoder* const dec, VP8Io* io) {
}
io->mb_y = y_start;
io->mb_h = y_end - y_start;
io->a = NULL;
#ifdef WEBP_EXPERIMENTAL_FEATURES
if (dec->alpha_data_) {
io->a = VP8DecompressAlphaRows(dec, y_start, y_end - y_start);
if (io->a == NULL) {
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
"Could not decode alpha data.");
}
}
#endif
if (!io->put(io)) {
return 0;
}

View File

@ -305,6 +305,10 @@ int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
"bad partition length");
}
dec->alpha_data_ = NULL;
dec->alpha_data_size_ = 0;
br = &dec->br_;
VP8InitBitReader(br, buf, buf + frm_hdr->partition_length_);
buf += frm_hdr->partition_length_;
@ -323,6 +327,7 @@ int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
"cannot parse filter header");
}
status = ParsePartitions(dec, buf, buf_size);
if (status != VP8_STATUS_OK) {
return VP8SetError(dec, status, "cannot parse partitions");
@ -368,6 +373,32 @@ int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
VP8ParseProba(br, dec);
#ifdef WEBP_EXPERIMENTAL_FEATURES
// Extensions
if (dec->pic_hdr_.colorspace_) {
const uint32_t EXT_SIZE = 4;
uint32_t ext_size;
uint8_t ext_bits;
const uint8_t* ext_bytes_end = buf - EXT_SIZE;
if (frm_hdr->partition_length_ <= EXT_SIZE) {
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
"RIFF: Inconsistent extra information.");
}
ext_size = (ext_bytes_end[0] << 16) | (ext_bytes_end[1] << 8)
| (ext_bytes_end[2]);
ext_bits = ext_bytes_end[3];
ext_bytes_end -= ext_size;
if (!(ext_bits & 0x01) || (ext_size + EXT_SIZE > frm_hdr->partition_length_)) {
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
"RIFF: Inconsistent extra information.");
}
if (!!(ext_bits & 0x02)) { // has alpha data
dec->alpha_data_size_ = ext_size;
dec->alpha_data_ = ext_bytes_end;
}
}
#endif
// sanitized state
dec->ready_ = 1;
return 1;

View File

@ -246,6 +246,11 @@ struct VP8Decoder {
// Filtering side-info
int filter_type_; // 0=off, 1=simple, 2=complex
uint8_t filter_levels_[NUM_MB_SEGMENTS]; // precalculated per-segment
// extensions
const uint8_t* alpha_data_; // compressed alpha data (if present)
size_t alpha_data_size_;
uint8_t* alpha_plane_; // output
};
//-----------------------------------------------------------------------------
@ -274,6 +279,10 @@ int VP8FinishRow(VP8Decoder* const dec, VP8Io* io);
// Decode one macroblock. Returns false if there is not enough data.
int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br);
// in alpha.c
const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
int row, int num_rows);
// in dsp.c
typedef void (*VP8Idct)(const int16_t* coeffs, uint8_t* dst);
extern VP8Idct VP8Transform;

View File

@ -139,10 +139,10 @@ static inline void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \
}
// All variants implemented.
UPSCALE_FUNC(UpscaleRgbLinePair, VP8YuvToRgb, 3)
UPSCALE_FUNC(UpscaleBgrLinePair, VP8YuvToBgr, 3)
UPSCALE_FUNC(UpscaleRgbaLinePair, VP8YuvToRgba, 4)
UPSCALE_FUNC(UpscaleBgraLinePair, VP8YuvToBgra, 4)
UPSCALE_FUNC(UpscaleRgbLinePair, VP8YuvToRgb, 3)
UPSCALE_FUNC(UpscaleBgrLinePair, VP8YuvToBgr, 3)
UPSCALE_FUNC(UpscaleRgbaLinePair, VP8YuvToRgb, 4)
UPSCALE_FUNC(UpscaleBgraLinePair, VP8YuvToBgr, 4)
// Main driver function.
static inline
@ -266,15 +266,42 @@ static int CustomPut(const VP8Io* io) {
} else if (p->mode == MODE_BGR) {
VP8YuvToBgr(y, u, v, dst + i * 3);
} else if (p->mode == MODE_RGBA) {
VP8YuvToRgba(y, u, v, dst + i * 4);
VP8YuvToRgb(y, u, v, dst + i * 4);
} else {
VP8YuvToBgra(y, u, v, dst + i * 4);
VP8YuvToBgr(y, u, v, dst + i * 4);
}
}
dst += p->stride;
}
}
}
// Alpha handling
if (p->mode == MODE_RGBA || p->mode == MODE_BGRA) {
int i, j;
uint8_t* dst = p->output + io->mb_y * p->stride + 3;
const uint8_t* alpha = io->a;
const int has_alpha = (alpha != NULL);
#ifdef WEBP_EXPERIMENTAL_FEATURES
if (has_alpha) {
for (j = 0; j < mb_h; ++j) {
for (i = 0; i < w; ++i) {
dst[4 * i] = alpha[i];
}
alpha += io->width;
dst += p->stride;
}
}
#endif
if (!has_alpha) { // fill-in with 0xFFs
for (j = 0; j < mb_h; ++j) {
for (i = 0; i < w; ++i) {
dst[4 * i] = 0xff;
}
dst += p->stride;
}
}
}
return 1;
}

View File

@ -36,11 +36,6 @@ inline static void VP8YuvToRgb(uint8_t y, uint8_t u, uint8_t v,
rgb[2] = VP8kClip[y + b_off - YUV_RANGE_MIN];
}
inline static void VP8YuvToRgba(int y, int u, int v, uint8_t* const rgba) {
VP8YuvToRgb(y, u, v, rgba);
rgba[3] = 0xff;
}
inline static void VP8YuvToBgr(uint8_t y, uint8_t u, uint8_t v,
uint8_t* const bgr) {
const int r_off = VP8kVToR[v];