A quick pass of cleanup in backward reference code

const correctness, renaming, cosmetics etc.

Change-Id: I432befbb22f0eafd9a613f5f632398b6ef03c0f6
This commit is contained in:
Urvang Joshi
2012-04-25 07:33:57 +00:00
committed by James Zern
parent 83332b3c16
commit e491729905
4 changed files with 207 additions and 247 deletions

View File

@ -15,7 +15,6 @@
#include <assert.h>
#include <stdint.h>
#include "../webp/types.h"
#if defined(__cplusplus) || defined(c_plusplus)
@ -28,7 +27,7 @@ extern "C" {
// Compression constants
#define CODE_LENGTH_CODES 19
static const int kLengthCodes = 24;
static const int kPaletteCodeBitsMax = 11;
static const int kColorCacheBitsMax = 11;
#define PIX_OR_COPY_CODES_MAX (256 + 24 + (1 << 11))
static const int kMaxLength = 4096;
@ -89,7 +88,7 @@ static WEBP_INLINE void PrefixEncode(
enum Mode {
kLiteral,
kPaletteIx,
kCacheIdx,
kCopy,
kNone,
};
@ -98,24 +97,24 @@ typedef struct {
// mode as uint8_t to make the memory layout to be exactly 8 bytes.
uint8_t mode;
uint16_t len;
uint32_t argb_or_offset;
uint32_t argb_or_distance;
} PixOrCopy;
static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t offset,
static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t distance,
uint16_t len) {
PixOrCopy retval;
retval.mode = kCopy;
retval.argb_or_offset = offset;
retval.argb_or_distance = distance;
retval.len = len;
return retval;
}
static WEBP_INLINE PixOrCopy PixOrCopyCreatePaletteIx(int ix) {
static WEBP_INLINE PixOrCopy PixOrCopyCreateCacheIdx(int idx) {
PixOrCopy retval;
assert(ix >= 0);
assert(ix < (1 << kPaletteCodeBitsMax));
retval.mode = kPaletteIx;
retval.argb_or_offset = ix;
assert(idx >= 0);
assert(idx < (1 << kColorCacheBitsMax));
retval.mode = kCacheIdx;
retval.argb_or_distance = idx;
retval.len = 1;
return retval;
}
@ -123,111 +122,89 @@ static WEBP_INLINE PixOrCopy PixOrCopyCreatePaletteIx(int ix) {
static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) {
PixOrCopy retval;
retval.mode = kLiteral;
retval.argb_or_offset = argb;
retval.argb_or_distance = argb;
retval.len = 1;
return retval;
}
static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy *p) {
static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy* const p) {
return p->mode == kLiteral;
}
static WEBP_INLINE int PixOrCopyIsPaletteIx(const PixOrCopy *p) {
return p->mode == kPaletteIx;
static WEBP_INLINE int PixOrCopyIsCacheIdx(const PixOrCopy* const p) {
return p->mode == kCacheIdx;
}
static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy *p) {
static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy* const p) {
return p->mode == kCopy;
}
static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy *p,
static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy* const p,
int component) {
assert(p->mode == kLiteral);
return (p->argb_or_offset >> (component * 8)) & 0xff;
return (p->argb_or_distance >> (component * 8)) & 0xff;
}
static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy *p) {
static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy* const p) {
return p->len;
}
static WEBP_INLINE uint32_t PixOrCopyArgb(const PixOrCopy *p) {
static WEBP_INLINE uint32_t PixOrCopyArgb(const PixOrCopy* const p) {
assert(p->mode == kLiteral);
return p->argb_or_offset;
return p->argb_or_distance;
}
static WEBP_INLINE uint32_t PixOrCopyPaletteIx(const PixOrCopy *p) {
assert(p->mode == kPaletteIx);
assert(p->argb_or_offset < (1 << kPaletteCodeBitsMax));
return p->argb_or_offset;
static WEBP_INLINE uint32_t PixOrCopyCacheIdx(const PixOrCopy* const p) {
assert(p->mode == kCacheIdx);
assert(p->argb_or_distance < (1 << kColorCacheBitsMax));
return p->argb_or_distance;
}
static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy *p) {
static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy* const p) {
assert(p->mode == kCopy);
return p->argb_or_offset;
return p->argb_or_distance;
}
static WEBP_INLINE void PixOrCopyLengthCodeAndBits(
const PixOrCopy *p, int *code, int *n_bits, int *bits) {
assert(p->len >= 1 && p->len <= kMaxLength);
PrefixEncode(p->len, code, n_bits, bits);
}
// Ridiculously simple backward references for images where it is unlikely
// that there are large backward references (photos).
void VP8LBackwardReferencesRle(
int xsize,
int ysize,
const uint32_t *argb,
PixOrCopy *stream,
int *stream_size);
int xsize, int ysize, const uint32_t* const argb, PixOrCopy* const stream,
int* const stream_size);
// This is a simple fast function for obtaining backward references
// based on simple heuristics. Returns 1 on success.
int VP8LBackwardReferencesHashChain(
int xsize,
int ysize,
int use_palette,
const uint32_t *argb,
int palette_bits,
int quality,
PixOrCopy *stream,
int *stream_size);
int xsize, int ysize, int use_color_cache, const uint32_t* const argb,
int cache_bits, int quality, PixOrCopy* const stream,
int* const stream_size);
// This method looks for a shortest path through the backward reference
// network based on a cost model generated by a first round of compression.
// Returns 1 on success.
int VP8LBackwardReferencesTraceBackwards(
int xsize,
int ysize,
int recursive_cost_model,
int use_palette,
const uint32_t *argb,
int palette_bits,
PixOrCopy *stream,
int *stream_size);
int xsize, int ysize, int recursive_cost_model, int use_color_cache,
const uint32_t* const argb, int cache_bits, PixOrCopy* const stream,
int* const stream_size);
// Convert backward references that are of linear distance along
// the image scan lines to have a 2d locality indexing where
// smaller values are used for backward references that are close by.
void VP8LBackwardReferences2DLocality(int xsize, int data_size,
PixOrCopy *data);
PixOrCopy* const data);
// Internals of locality transform exposed for testing use.
int VP8LDistanceToPlaneCode(int xsize, int distance);
// Returns true if the given backward references actually produce
// the image given in tuple (argb, xsize, ysize).
int VP8LVerifyBackwardReferences(const uint32_t* argb,
int xsize, int ysize,
int palette_bits,
const PixOrCopy *lit,
int lit_size);
int VP8LVerifyBackwardReferences(
const uint32_t* const argb, int xsize, int ysize, int cache_bits,
const PixOrCopy* const lit, int lit_size);
// Produce an estimate for a good emerging palette size for the image.
int VP8LCalculateEstimateForPaletteSize(const uint32_t *argb,
int xsize, int ysize,
int *best_palette_bits);
// Produce an estimate for a good color cache size for the image.
int VP8LCalculateEstimateForCacheSize(
const uint32_t* const argb, int xsize, int ysize,
int* const best_cache_bits);
#if defined(__cplusplus) || defined(c_plusplus)
}