mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-14 21:09:55 +02:00
enable lossless decoder
import changes from experimental 5529a2e^ and enable build in autoconf and makefile.unix; windows will be treated separately. Change-Id: Ie2e177a99db63190b4cd647b3edee3b4e13719e9
This commit is contained in:
264
src/dec/webp.c
264
src/dec/webp.c
@ -12,6 +12,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "./vp8i.h"
|
||||
#include "./vp8li.h"
|
||||
#include "./webpi.h"
|
||||
#include "../mux/muxi.h" // For MAX_CHUNK_PAYLOAD.
|
||||
#include "../webp/mux.h" // For 'ALPHA_FLAG'.
|
||||
@ -31,6 +32,10 @@ extern "C" {
|
||||
// 16..19 size of the raw VP8 image data, starting at offset 20
|
||||
// 20.... the VP8 bytes
|
||||
// Or,
|
||||
// 12..15 "VP8L": 4-bytes tags, signaling the use of VP8L lossless format
|
||||
// 16..19 size of the raw VP8L image data, starting at offset 20
|
||||
// 20.... the VP8L bytes
|
||||
// Or,
|
||||
// 12..15 "VP8X": 4-bytes tags, describing the extended-VP8 chunk.
|
||||
// 16..19 size of the VP8X chunk starting at offset 20.
|
||||
// 20..23 VP8X flags bit-map corresponding to the chunk-types present.
|
||||
@ -45,41 +50,54 @@ static WEBP_INLINE uint32_t get_le32(const uint8_t* const data) {
|
||||
return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
|
||||
}
|
||||
|
||||
VP8StatusCode WebPParseRIFF(const uint8_t** data, uint32_t* data_size,
|
||||
uint32_t* riff_size) {
|
||||
// Validates the RIFF container (if detected) and skips over it.
|
||||
// If a RIFF container is detected,
|
||||
// Returns VP8_STATUS_BITSTREAM_ERROR for invalid header, and
|
||||
// VP8_STATUS_OK otherwise.
|
||||
// In case there are not enough bytes (partial RIFF container), return 0 for
|
||||
// riff_size. Else return the riff_size extracted from the header.
|
||||
static VP8StatusCode ParseRIFF(const uint8_t** data, uint32_t* data_size,
|
||||
uint32_t* riff_size) {
|
||||
assert(data);
|
||||
assert(data_size);
|
||||
assert(riff_size);
|
||||
|
||||
*riff_size = 0; // Default: no RIFF present.
|
||||
if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) {
|
||||
if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {
|
||||
return VP8_STATUS_BITSTREAM_ERROR; // Wrong image file signature.
|
||||
} else {
|
||||
*riff_size = get_le32(*data + TAG_SIZE);
|
||||
const uint32_t size = get_le32(*data + TAG_SIZE);
|
||||
// Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").
|
||||
if (*riff_size < TAG_SIZE + CHUNK_HEADER_SIZE) {
|
||||
if (size < TAG_SIZE + CHUNK_HEADER_SIZE) {
|
||||
return VP8_STATUS_BITSTREAM_ERROR;
|
||||
}
|
||||
// We have a RIFF container. Skip it.
|
||||
*riff_size = size;
|
||||
*data += RIFF_HEADER_SIZE;
|
||||
*data_size -= RIFF_HEADER_SIZE;
|
||||
}
|
||||
} else {
|
||||
*riff_size = 0; // Did not get full RIFF header.
|
||||
}
|
||||
return VP8_STATUS_OK;
|
||||
}
|
||||
|
||||
VP8StatusCode WebPParseVP8X(const uint8_t** data, uint32_t* data_size,
|
||||
uint32_t* bytes_skipped,
|
||||
int* width, int* height, uint32_t* flags) {
|
||||
// Validates the VP8X header and skips over it.
|
||||
// Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header,
|
||||
// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
|
||||
// VP8_STATUS_OK otherwise.
|
||||
// If a VP8X chunk is found, found_vp8x is set to true and *width, *height and
|
||||
// *flags are set to the corresponding values extracted from the VP8X chunk.
|
||||
static VP8StatusCode ParseVP8X(const uint8_t** data, uint32_t* data_size,
|
||||
int* found_vp8x,
|
||||
int* width, int* height, uint32_t* flags) {
|
||||
const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
|
||||
assert(data);
|
||||
assert(data_size);
|
||||
assert(bytes_skipped);
|
||||
assert(found_vp8x);
|
||||
|
||||
*bytes_skipped = 0;
|
||||
*found_vp8x = 0;
|
||||
|
||||
if (*data_size < CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE) {
|
||||
if (*data_size < vp8x_size) {
|
||||
return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
|
||||
}
|
||||
|
||||
@ -98,39 +116,43 @@ VP8StatusCode WebPParseVP8X(const uint8_t** data, uint32_t* data_size,
|
||||
*height = get_le32(*data + 16);
|
||||
}
|
||||
// Skip over VP8X header bytes.
|
||||
*bytes_skipped = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
|
||||
*data += *bytes_skipped;
|
||||
*data_size -= *bytes_skipped;
|
||||
*data += vp8x_size;
|
||||
*data_size -= vp8x_size;
|
||||
*found_vp8x = 1;
|
||||
}
|
||||
return VP8_STATUS_OK;
|
||||
}
|
||||
|
||||
VP8StatusCode WebPParseOptionalChunks(const uint8_t** data, uint32_t* data_size,
|
||||
uint32_t riff_size,
|
||||
uint32_t* bytes_skipped,
|
||||
const uint8_t** alpha_data,
|
||||
uint32_t* alpha_size) {
|
||||
// Skips to the next VP8 chunk header in the data given the size of the RIFF
|
||||
// chunk 'riff_size'.
|
||||
// Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered,
|
||||
// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
|
||||
// VP8_STATUS_OK otherwise.
|
||||
// If an alpha chunk is found, alpha_data and alpha_size are set appropriately.
|
||||
static VP8StatusCode ParseOptionalChunks(const uint8_t** data,
|
||||
uint32_t* data_size,
|
||||
uint32_t riff_size,
|
||||
const uint8_t** alpha_data,
|
||||
uint32_t* alpha_size) {
|
||||
const uint8_t* buf;
|
||||
uint32_t buf_size;
|
||||
|
||||
uint32_t total_size = TAG_SIZE + // "WEBP".
|
||||
CHUNK_HEADER_SIZE + // "VP8Xnnnn".
|
||||
VP8X_CHUNK_SIZE; // data.
|
||||
assert(data);
|
||||
assert(data_size);
|
||||
assert(bytes_skipped);
|
||||
assert(alpha_data);
|
||||
assert(alpha_size);
|
||||
|
||||
buf = *data;
|
||||
buf_size = *data_size;
|
||||
*bytes_skipped = 0;
|
||||
|
||||
assert(alpha_data);
|
||||
assert(alpha_size);
|
||||
*alpha_data = NULL;
|
||||
*alpha_size = 0;
|
||||
|
||||
while (1) {
|
||||
uint32_t chunk_size;
|
||||
uint32_t cur_skip_size;
|
||||
const uint32_t bytes_skipped_header = TAG_SIZE + // "WEBP".
|
||||
CHUNK_HEADER_SIZE + // "VP8Xnnnn".
|
||||
VP8X_CHUNK_SIZE; // data.
|
||||
uint32_t disk_chunk_size; // chunk_size with padding
|
||||
|
||||
*data = buf;
|
||||
*data_size = buf_size;
|
||||
|
||||
@ -140,20 +162,19 @@ VP8StatusCode WebPParseOptionalChunks(const uint8_t** data, uint32_t* data_size,
|
||||
|
||||
chunk_size = get_le32(buf + TAG_SIZE);
|
||||
// For odd-sized chunk-payload, there's one byte padding at the end.
|
||||
cur_skip_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1;
|
||||
disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1;
|
||||
total_size += disk_chunk_size;
|
||||
|
||||
// Check that total bytes skipped along with current chunk size
|
||||
// does not exceed riff_size.
|
||||
if (riff_size > 0 &&
|
||||
(bytes_skipped_header + *bytes_skipped + cur_skip_size > riff_size)) {
|
||||
return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.
|
||||
// Check that total bytes skipped so far does not exceed riff_size.
|
||||
if (riff_size > 0 && (total_size > riff_size)) {
|
||||
return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.
|
||||
}
|
||||
|
||||
if (buf_size < cur_skip_size) { // Insufficient data.
|
||||
if (buf_size < disk_chunk_size) { // Insufficient data.
|
||||
return VP8_STATUS_NOT_ENOUGH_DATA;
|
||||
}
|
||||
|
||||
if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header.
|
||||
if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header.
|
||||
*alpha_data = buf + CHUNK_HEADER_SIZE;
|
||||
*alpha_size = chunk_size;
|
||||
} else if (!memcmp(buf, "VP8 ", TAG_SIZE)) { // A valid VP8 header.
|
||||
@ -161,108 +182,104 @@ VP8StatusCode WebPParseOptionalChunks(const uint8_t** data, uint32_t* data_size,
|
||||
}
|
||||
|
||||
// We have a full and valid chunk; skip it.
|
||||
buf += cur_skip_size;
|
||||
buf_size -= cur_skip_size;
|
||||
*bytes_skipped += cur_skip_size;
|
||||
buf += disk_chunk_size;
|
||||
buf_size -= disk_chunk_size;
|
||||
}
|
||||
}
|
||||
|
||||
VP8StatusCode WebPParseVP8Header(const uint8_t** data, uint32_t* data_size,
|
||||
uint32_t riff_size, uint32_t* bytes_skipped,
|
||||
uint32_t* vp8_chunk_size) {
|
||||
// Validates the VP8 Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.
|
||||
// Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than
|
||||
// riff_size) VP8 header,
|
||||
// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
|
||||
// VP8_STATUS_OK otherwise.
|
||||
// If a VP8/VP8L chunk is found, chunk_size is set to the total number of bytes
|
||||
// extracted from the VP8/VP8L chunk header.
|
||||
// The flag 'is_lossless' is set to 1 in case of VP8L chunk.
|
||||
static VP8StatusCode ParseVP8Header(const uint8_t** data, uint32_t* data_size,
|
||||
uint32_t riff_size,
|
||||
uint32_t* chunk_size, int* is_lossless) {
|
||||
const int is_vp8 = !memcmp(*data, "VP8 ", TAG_SIZE);
|
||||
const int is_vp8l = !memcmp(*data, "VP8L", TAG_SIZE);
|
||||
const uint32_t minimal_size =
|
||||
TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn"
|
||||
assert(data);
|
||||
assert(data_size);
|
||||
assert(bytes_skipped);
|
||||
assert(vp8_chunk_size);
|
||||
assert(chunk_size);
|
||||
assert(is_lossless);
|
||||
|
||||
*bytes_skipped = 0;
|
||||
*vp8_chunk_size = 0;
|
||||
*chunk_size = *data_size; // default: raw data
|
||||
|
||||
if (*data_size < CHUNK_HEADER_SIZE) {
|
||||
return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
|
||||
}
|
||||
|
||||
if (!memcmp(*data, "VP8 ", TAG_SIZE)) {
|
||||
*vp8_chunk_size = get_le32(*data + TAG_SIZE);
|
||||
if ((riff_size >= TAG_SIZE + CHUNK_HEADER_SIZE) && // "WEBP" + "VP8 nnnn".
|
||||
(*vp8_chunk_size > riff_size - (TAG_SIZE + CHUNK_HEADER_SIZE))) {
|
||||
if (is_vp8 || is_vp8l) {
|
||||
const uint32_t size = get_le32(*data + TAG_SIZE);
|
||||
if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) {
|
||||
return VP8_STATUS_BITSTREAM_ERROR; // Inconsistent size information.
|
||||
}
|
||||
// Skip over CHUNK_HEADER_SIZE bytes from VP8 Header.
|
||||
*bytes_skipped = CHUNK_HEADER_SIZE;
|
||||
*data += *bytes_skipped;
|
||||
*data_size -= *bytes_skipped;
|
||||
*chunk_size = size;
|
||||
*data += CHUNK_HEADER_SIZE;
|
||||
*data_size -= CHUNK_HEADER_SIZE;
|
||||
*is_lossless = is_vp8l;
|
||||
}
|
||||
return VP8_STATUS_OK;
|
||||
}
|
||||
|
||||
VP8StatusCode WebPParseHeaders(const uint8_t** data, uint32_t* data_size,
|
||||
uint32_t* vp8_size, uint32_t* bytes_skipped,
|
||||
const uint8_t** alpha_data,
|
||||
uint32_t* alpha_size) {
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {
|
||||
const uint8_t* buf;
|
||||
uint32_t buf_size;
|
||||
uint32_t riff_size;
|
||||
uint32_t vp8_size_tmp;
|
||||
uint32_t optional_data_size;
|
||||
uint32_t vp8x_skip_size;
|
||||
uint32_t vp8_skip_size;
|
||||
int found_vp8x;
|
||||
VP8StatusCode status;
|
||||
|
||||
assert(data);
|
||||
assert(data_size);
|
||||
assert(vp8_size);
|
||||
assert(bytes_skipped);
|
||||
assert(alpha_data);
|
||||
assert(alpha_size);
|
||||
assert(headers);
|
||||
|
||||
buf = *data;
|
||||
buf_size = *data_size;
|
||||
|
||||
*vp8_size = 0;
|
||||
*bytes_skipped = 0;
|
||||
*alpha_data = NULL;
|
||||
*alpha_size = 0;
|
||||
buf = headers->data;
|
||||
buf_size = headers->data_size;
|
||||
headers->alpha_data = NULL;
|
||||
headers->alpha_data_size = 0;
|
||||
headers->vp8_size = 0;
|
||||
headers->is_lossless = 0;
|
||||
headers->offset = 0;
|
||||
headers->riff_size = 0;
|
||||
|
||||
if (buf == NULL || buf_size < RIFF_HEADER_SIZE) {
|
||||
return VP8_STATUS_NOT_ENOUGH_DATA;
|
||||
}
|
||||
|
||||
// Skip over RIFF header.
|
||||
if (WebPParseRIFF(&buf, &buf_size, &riff_size) != VP8_STATUS_OK) {
|
||||
if (ParseRIFF(&buf, &buf_size, &headers->riff_size) != VP8_STATUS_OK) {
|
||||
return VP8_STATUS_BITSTREAM_ERROR; // Wrong RIFF header.
|
||||
}
|
||||
|
||||
// Skip over VP8X header.
|
||||
status = WebPParseVP8X(&buf, &buf_size, &vp8x_skip_size, NULL, NULL, NULL);
|
||||
status = ParseVP8X(&buf, &buf_size, &found_vp8x, NULL, NULL, NULL);
|
||||
if (status != VP8_STATUS_OK) {
|
||||
return status; // Wrong VP8X chunk / insufficient data.
|
||||
}
|
||||
if (vp8x_skip_size > 0) {
|
||||
if (found_vp8x) {
|
||||
// Skip over optional chunks.
|
||||
status = WebPParseOptionalChunks(&buf, &buf_size, riff_size,
|
||||
&optional_data_size,
|
||||
alpha_data, alpha_size);
|
||||
status = ParseOptionalChunks(&buf, &buf_size, headers->riff_size,
|
||||
&headers->alpha_data,
|
||||
&headers->alpha_data_size);
|
||||
if (status != VP8_STATUS_OK) {
|
||||
return status; // Found an invalid chunk size / insufficient data.
|
||||
}
|
||||
}
|
||||
|
||||
// Skip over VP8 chunk header.
|
||||
status = WebPParseVP8Header(&buf, &buf_size, riff_size, &vp8_skip_size,
|
||||
&vp8_size_tmp);
|
||||
status = ParseVP8Header(&buf, &buf_size, headers->riff_size,
|
||||
&headers->vp8_size, &headers->is_lossless);
|
||||
if (status != VP8_STATUS_OK) {
|
||||
return status; // Invalid VP8 header / insufficient data.
|
||||
}
|
||||
if (vp8_skip_size > 0) {
|
||||
*vp8_size = vp8_size_tmp;
|
||||
}
|
||||
|
||||
*bytes_skipped = (uint32_t)(buf - *data);
|
||||
assert(buf - *data < MAX_CHUNK_PAYLOAD);
|
||||
assert(*bytes_skipped == *data_size - buf_size);
|
||||
*data = buf;
|
||||
*data_size = buf_size;
|
||||
headers->offset = (uint32_t)(buf - headers->data);
|
||||
assert((uint64_t)(buf - headers->data) < MAX_CHUNK_PAYLOAD);
|
||||
assert(headers->offset == headers->data_size - buf_size);
|
||||
return VP8_STATUS_OK;
|
||||
}
|
||||
|
||||
@ -310,9 +327,18 @@ static VP8StatusCode DecodeInto(const uint8_t* data, uint32_t data_size,
|
||||
params->output);
|
||||
if (status == VP8_STATUS_OK) {
|
||||
// Decode
|
||||
if (!VP8Decode(dec, &io)) {
|
||||
status = dec->status_;
|
||||
if (!dec->is_lossless_) {
|
||||
if (!VP8Decode(dec, &io)) {
|
||||
status = dec->status_;
|
||||
}
|
||||
} else {
|
||||
VP8LDecoder* const vp8l_decoder = &dec->vp8l_decoder_;
|
||||
if (!VP8LDecodeImage(vp8l_decoder)) {
|
||||
status = VP8_STATUS_BITSTREAM_ERROR;
|
||||
}
|
||||
}
|
||||
} else if (dec->is_lossless_) { // Clear lossless decoder on error.
|
||||
VP8LClear(&dec->vp8l_decoder_);
|
||||
}
|
||||
}
|
||||
VP8Delete(dec);
|
||||
@ -479,13 +505,12 @@ static void DefaultFeatures(WebPBitstreamFeatures* const features) {
|
||||
|
||||
static VP8StatusCode GetFeatures(const uint8_t* data, uint32_t data_size,
|
||||
WebPBitstreamFeatures* const features) {
|
||||
uint32_t vp8_chunk_size = 0;
|
||||
uint32_t chunk_size = 0;
|
||||
uint32_t riff_size = 0;
|
||||
uint32_t flags = 0;
|
||||
uint32_t vp8x_skip_size = 0;
|
||||
uint32_t vp8_skip_size = 0;
|
||||
int* const width = &features->width;
|
||||
int* const height = &features->height;
|
||||
int found_vp8x;
|
||||
int is_lossless = 0;
|
||||
VP8StatusCode status;
|
||||
|
||||
if (features == NULL || data == NULL) {
|
||||
@ -494,35 +519,42 @@ static VP8StatusCode GetFeatures(const uint8_t* data, uint32_t data_size,
|
||||
DefaultFeatures(features);
|
||||
|
||||
// Skip over RIFF header.
|
||||
status = WebPParseRIFF(&data, &data_size, &riff_size);
|
||||
status = ParseRIFF(&data, &data_size, &riff_size);
|
||||
if (status != VP8_STATUS_OK) {
|
||||
return status; // Wrong RIFF header / insufficient data.
|
||||
}
|
||||
|
||||
// Skip over VP8X.
|
||||
status = WebPParseVP8X(&data, &data_size, &vp8x_skip_size,
|
||||
width, height, &flags);
|
||||
if (status != VP8_STATUS_OK) {
|
||||
return status; // Wrong VP8X / insufficient data.
|
||||
}
|
||||
features->has_alpha = !!(flags & ALPHA_FLAG);
|
||||
if (vp8x_skip_size > 0) {
|
||||
return VP8_STATUS_OK; // Return features from VP8X header.
|
||||
{
|
||||
uint32_t flags = 0;
|
||||
status = ParseVP8X(&data, &data_size, &found_vp8x, width, height, &flags);
|
||||
if (status != VP8_STATUS_OK) {
|
||||
return status; // Wrong VP8X / insufficient data.
|
||||
}
|
||||
features->has_alpha = !!(flags & ALPHA_FLAG);
|
||||
if (found_vp8x) {
|
||||
return VP8_STATUS_OK; // Return features from VP8X header.
|
||||
}
|
||||
}
|
||||
|
||||
// Skip over VP8 header.
|
||||
status = WebPParseVP8Header(&data, &data_size, riff_size, &vp8_skip_size,
|
||||
&vp8_chunk_size);
|
||||
status = ParseVP8Header(&data, &data_size, riff_size,
|
||||
&chunk_size, &is_lossless);
|
||||
if (status != VP8_STATUS_OK) {
|
||||
return status; // Wrong VP8 chunk-header / insufficient data.
|
||||
}
|
||||
if (vp8_skip_size == 0) {
|
||||
vp8_chunk_size = data_size; // No VP8 chunk wrapper over raw VP8 data.
|
||||
}
|
||||
|
||||
// Validates raw VP8 data.
|
||||
if (!VP8GetInfo(data, data_size, vp8_chunk_size, width, height)) {
|
||||
return VP8_STATUS_BITSTREAM_ERROR;
|
||||
if (!is_lossless) {
|
||||
// Validates raw VP8 data.
|
||||
if (!VP8GetInfo(data, data_size, chunk_size, width, height)) {
|
||||
return VP8_STATUS_BITSTREAM_ERROR;
|
||||
}
|
||||
} else {
|
||||
// Validates raw VP8L data.
|
||||
if (!VP8LGetInfo(data, data_size, width, height)) {
|
||||
return VP8_STATUS_BITSTREAM_ERROR;
|
||||
}
|
||||
features->has_alpha = 1;
|
||||
}
|
||||
|
||||
return VP8_STATUS_OK; // Return features from VP8 header.
|
||||
|
Reference in New Issue
Block a user