Move some more defines to format_constants.h

Also remove some duplicate const/defines.

Change-Id: I0ec48866b874f546022d72e938fb65669b0b3211
This commit is contained in:
Urvang Joshi 2012-05-24 17:45:05 +05:30
parent c13f663261
commit e75dc80516
8 changed files with 30 additions and 32 deletions

View File

@ -17,6 +17,7 @@
#include "./backward_references.h" #include "./backward_references.h"
#include "./histogram.h" #include "./histogram.h"
#include "../utils/color_cache.h" #include "../utils/color_cache.h"
#include "../webp/format_constants.h"
#define VALUES_IN_BYTE 256 #define VALUES_IN_BYTE 256
@ -306,7 +307,7 @@ typedef struct {
double red_[VALUES_IN_BYTE]; double red_[VALUES_IN_BYTE];
double literal_[PIX_OR_COPY_CODES_MAX]; double literal_[PIX_OR_COPY_CODES_MAX];
double blue_[VALUES_IN_BYTE]; double blue_[VALUES_IN_BYTE];
double distance_[DISTANCE_CODES_MAX]; double distance_[NUM_DISTANCE_CODES];
int cache_bits_; int cache_bits_;
} CostModel; } CostModel;
@ -347,7 +348,7 @@ static int CostModelBuild(CostModel* const p, int xsize, int ysize,
VP8LConvertPopulationCountTableToBitEstimates( VP8LConvertPopulationCountTableToBitEstimates(
VALUES_IN_BYTE, &histo.alpha_[0], &p->alpha_[0]); VALUES_IN_BYTE, &histo.alpha_[0], &p->alpha_[0]);
VP8LConvertPopulationCountTableToBitEstimates( VP8LConvertPopulationCountTableToBitEstimates(
DISTANCE_CODES_MAX, &histo.distance_[0], &p->distance_[0]); NUM_DISTANCE_CODES, &histo.distance_[0], &p->distance_[0]);
ok = 1; ok = 1;
Error: Error:
@ -363,7 +364,7 @@ static WEBP_INLINE double GetLiteralCost(const CostModel* const p, uint32_t v) {
} }
static WEBP_INLINE double GetCacheCost(const CostModel* const p, uint32_t idx) { static WEBP_INLINE double GetCacheCost(const CostModel* const p, uint32_t idx) {
const int literal_idx = VALUES_IN_BYTE + kLengthCodes + idx; const int literal_idx = VALUES_IN_BYTE + NUM_LENGTH_CODES + idx;
return p->literal_[literal_idx]; return p->literal_[literal_idx];
} }

View File

@ -22,12 +22,6 @@
extern "C" { extern "C" {
#endif #endif
// Backward reference distance prefix codes
#define DISTANCE_CODES_MAX 40
// Compression constants
#define CODE_LENGTH_CODES 19
static const int kLengthCodes = 24;
// The spec allows 11, we use 9 bits to reduce memory consumption in encoding. // The spec allows 11, we use 9 bits to reduce memory consumption in encoding.
// Having 9 instead of 11 removes about 0.25 % of compression density. // Having 9 instead of 11 removes about 0.25 % of compression density.
static const int kColorCacheBitsMax = 9; static const int kColorCacheBitsMax = 9;

View File

@ -106,7 +106,7 @@ void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const p,
++p->literal_[PixOrCopyLiteral(v, 1)]; ++p->literal_[PixOrCopyLiteral(v, 1)];
++p->blue_[PixOrCopyLiteral(v, 0)]; ++p->blue_[PixOrCopyLiteral(v, 0)];
} else if (PixOrCopyIsCacheIdx(v)) { } else if (PixOrCopyIsCacheIdx(v)) {
int literal_ix = 256 + kLengthCodes + PixOrCopyCacheIdx(v); int literal_ix = 256 + NUM_LENGTH_CODES + PixOrCopyCacheIdx(v);
++p->literal_[literal_ix]; ++p->literal_[literal_ix];
} else { } else {
int code, extra_bits_count, extra_bits_value; int code, extra_bits_count, extra_bits_value;
@ -176,14 +176,14 @@ double VP8LHistogramEstimateBitsBulk(const VP8LHistogram* const p) {
BitsEntropy(&p->red_[0], 256) + BitsEntropy(&p->red_[0], 256) +
BitsEntropy(&p->blue_[0], 256) + BitsEntropy(&p->blue_[0], 256) +
BitsEntropy(&p->alpha_[0], 256) + BitsEntropy(&p->alpha_[0], 256) +
BitsEntropy(&p->distance_[0], DISTANCE_CODES_MAX); BitsEntropy(&p->distance_[0], NUM_DISTANCE_CODES);
// Compute the extra bits cost. // Compute the extra bits cost.
int i; int i;
for (i = 2; i < kLengthCodes - 2; ++i) { for (i = 2; i < NUM_LENGTH_CODES - 2; ++i) {
retval += retval +=
(i >> 1) * p->literal_[256 + i + 2]; (i >> 1) * p->literal_[256 + i + 2];
} }
for (i = 2; i < DISTANCE_CODES_MAX - 2; ++i) { for (i = 2; i < NUM_DISTANCE_CODES - 2; ++i) {
retval += (i >> 1) * p->distance_[i + 2]; retval += (i >> 1) * p->distance_[i + 2];
} }
return retval; return retval;
@ -237,7 +237,7 @@ double VP8LHistogramEstimateBitsHeader(const VP8LHistogram* const p) {
HuffmanCost(&p->red_[0], 256) + HuffmanCost(&p->red_[0], 256) +
HuffmanCost(&p->literal_[0], VP8LHistogramNumCodes(p)) + HuffmanCost(&p->literal_[0], VP8LHistogramNumCodes(p)) +
HuffmanCost(&p->blue_[0], 256) + HuffmanCost(&p->blue_[0], 256) +
HuffmanCost(&p->distance_[0], DISTANCE_CODES_MAX); HuffmanCost(&p->distance_[0], NUM_DISTANCE_CODES);
} }
static void HistogramBuildImage(int xsize, int histo_bits, static void HistogramBuildImage(int xsize, int histo_bits,

View File

@ -21,6 +21,7 @@
#include <string.h> #include <string.h>
#include "./backward_references.h" #include "./backward_references.h"
#include "../webp/format_constants.h"
#include "../webp/types.h" #include "../webp/types.h"
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
@ -36,7 +37,7 @@ typedef struct {
int blue_[256]; int blue_[256];
int alpha_[256]; int alpha_[256];
// Backward reference prefix-code histogram. // Backward reference prefix-code histogram.
int distance_[DISTANCE_CODES_MAX]; int distance_[NUM_DISTANCE_CODES];
int palette_code_bits_; int palette_code_bits_;
double bit_cost_; // cached value of VP8LHistogramEstimateBits(this) double bit_cost_; // cached value of VP8LHistogramEstimateBits(this)
} VP8LHistogram; } VP8LHistogram;
@ -86,7 +87,7 @@ static WEBP_INLINE void VP8LHistogramAdd(VP8LHistogram* const p,
for (i = 0; i < PIX_OR_COPY_CODES_MAX; ++i) { for (i = 0; i < PIX_OR_COPY_CODES_MAX; ++i) {
p->literal_[i] += a->literal_[i]; p->literal_[i] += a->literal_[i];
} }
for (i = 0; i < DISTANCE_CODES_MAX; ++i) { for (i = 0; i < NUM_DISTANCE_CODES; ++i) {
p->distance_[i] += a->distance_[i]; p->distance_[i] += a->distance_[i];
} }
for (i = 0; i < 256; ++i) { for (i = 0; i < 256; ++i) {
@ -103,7 +104,7 @@ static WEBP_INLINE void VP8LHistogramRemove(VP8LHistogram* const p,
p->literal_[i] -= a->literal_[i]; p->literal_[i] -= a->literal_[i];
assert(p->literal_[i] >= 0); assert(p->literal_[i] >= 0);
} }
for (i = 0; i < DISTANCE_CODES_MAX; ++i) { for (i = 0; i < NUM_DISTANCE_CODES; ++i) {
p->distance_[i] -= a->distance_[i]; p->distance_[i] -= a->distance_[i];
assert(p->distance_[i] >= 0); assert(p->distance_[i] >= 0);
} }
@ -118,7 +119,7 @@ static WEBP_INLINE void VP8LHistogramRemove(VP8LHistogram* const p,
} }
static WEBP_INLINE int VP8LHistogramNumCodes(const VP8LHistogram* const p) { static WEBP_INLINE int VP8LHistogramNumCodes(const VP8LHistogram* const p) {
return 256 + kLengthCodes + return 256 + NUM_LENGTH_CODES +
((p->palette_code_bits_ > 0) ? (1 << p->palette_code_bits_) : 0); ((p->palette_code_bits_ > 0) ? (1 << p->palette_code_bits_) : 0);
} }

View File

@ -22,6 +22,7 @@
#include "../dsp/lossless.h" #include "../dsp/lossless.h"
#include "../utils/bit_writer.h" #include "../utils/bit_writer.h"
#include "../utils/huffman_encode.h" #include "../utils/huffman_encode.h"
#include "../webp/format_constants.h"
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
extern "C" { extern "C" {
@ -163,7 +164,7 @@ static int GetHuffBitLengthsAndCodes(
HuffmanTreeCode* const codes = &huffman_codes[5 * i]; HuffmanTreeCode* const codes = &huffman_codes[5 * i];
for (k = 0; k < 5; ++k) { for (k = 0; k < 5; ++k) {
const int num_symbols = (k == 0) ? VP8LHistogramNumCodes(histo) const int num_symbols = (k == 0) ? VP8LHistogramNumCodes(histo)
: (k == 4) ? DISTANCE_CODES_MAX : (k == 4) ? NUM_DISTANCE_CODES
: 256; : 256;
codes[k].num_symbols = num_symbols; codes[k].num_symbols = num_symbols;
total_length_size += num_symbols; total_length_size += num_symbols;
@ -402,7 +403,7 @@ static void StoreImageToBitMask(
const HuffmanTreeCode* const codes = huffman_codes + 5 * histogram_ix; const HuffmanTreeCode* const codes = huffman_codes + 5 * histogram_ix;
if (PixOrCopyIsCacheIdx(v)) { if (PixOrCopyIsCacheIdx(v)) {
const int code = PixOrCopyCacheIdx(v); const int code = PixOrCopyCacheIdx(v);
const int literal_ix = 256 + kLengthCodes + code; const int literal_ix = 256 + NUM_LENGTH_CODES + code;
WriteHuffmanCode(bw, codes, literal_ix); WriteHuffmanCode(bw, codes, literal_ix);
} else if (PixOrCopyIsLiteral(v)) { } else if (PixOrCopyIsLiteral(v)) {
static const int order[] = { 1, 2, 0, 3 }; static const int order[] = { 1, 2, 0, 3 };

View File

@ -12,12 +12,12 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include "./huffman.h" #include "./huffman.h"
#include "../webp/format_constants.h"
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
extern "C" { extern "C" {
#endif #endif
#define MAX_ALLOWED_CODE_LENGTH 15
#define NON_EXISTENT_SYMBOL (-1) #define NON_EXISTENT_SYMBOL (-1)
static void TreeNodeInit(HuffmanTreeNode* const node) { static void TreeNodeInit(HuffmanTreeNode* const node) {

View File

@ -11,14 +11,12 @@
#ifdef USE_LOSSLESS_ENCODER #ifdef USE_LOSSLESS_ENCODER
#include "./huffman_encode.h"
#define MAX_BITS 16 // maximum allowed length for the codes
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "./huffman_encode.h"
#include "../webp/format_constants.h"
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Util function to optimize the symbol map for RLE coding // Util function to optimize the symbol map for RLE coding
@ -285,7 +283,7 @@ static int GenerateOptimalTree(const int* const histogram, int histogram_size,
static HuffmanTreeToken* CodeRepeatedValues(int repetitions, static HuffmanTreeToken* CodeRepeatedValues(int repetitions,
HuffmanTreeToken* tokens, HuffmanTreeToken* tokens,
int value, int prev_value) { int value, int prev_value) {
assert(value < MAX_BITS); assert(value <= MAX_ALLOWED_CODE_LENGTH);
if (value != prev_value) { if (value != prev_value) {
tokens->code = value; tokens->code = value;
tokens->extra_bits = 0; tokens->extra_bits = 0;
@ -387,10 +385,10 @@ static uint32_t ReverseBits(int num_bits, uint32_t bits) {
int i = 0; int i = 0;
while (i < num_bits) { while (i < num_bits) {
i += 4; i += 4;
retval |= kReversedBits[bits & 0xf] << (MAX_BITS - i); retval |= kReversedBits[bits & 0xf] << (MAX_ALLOWED_CODE_LENGTH + 1 - i);
bits >>= 4; bits >>= 4;
} }
retval >>= (MAX_BITS - num_bits); retval >>= (MAX_ALLOWED_CODE_LENGTH + 1 - num_bits);
return retval; return retval;
} }
@ -399,21 +397,21 @@ static void ConvertBitDepthsToSymbols(HuffmanTreeCode* const tree) {
// 0 bit-depth means that the symbol does not exist. // 0 bit-depth means that the symbol does not exist.
int i; int i;
int len; int len;
uint32_t next_code[MAX_BITS]; uint32_t next_code[MAX_ALLOWED_CODE_LENGTH + 1];
int depth_count[MAX_BITS] = { 0 }; int depth_count[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };
assert(tree != NULL); assert(tree != NULL);
len = tree->num_symbols; len = tree->num_symbols;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
const int code_length = tree->code_lengths[i]; const int code_length = tree->code_lengths[i];
assert(code_length < MAX_BITS); assert(code_length <= MAX_ALLOWED_CODE_LENGTH);
++depth_count[code_length]; ++depth_count[code_length];
} }
depth_count[0] = 0; // ignore unused symbol depth_count[0] = 0; // ignore unused symbol
next_code[0] = 0; next_code[0] = 0;
{ {
uint32_t code = 0; uint32_t code = 0;
for (i = 1; i < MAX_BITS; ++i) { for (i = 1; i <= MAX_ALLOWED_CODE_LENGTH; ++i) {
code = (code + depth_count[i - 1]) << 1; code = (code + depth_count[i - 1]) << 1;
next_code[i] = code; next_code[i] = code;
} }

View File

@ -29,11 +29,14 @@
#define MAX_CACHE_BITS 11 #define MAX_CACHE_BITS 11
#define HUFFMAN_CODES_PER_META_CODE 5 #define HUFFMAN_CODES_PER_META_CODE 5
#define ARGB_BLACK 0xff000000 #define ARGB_BLACK 0xff000000
#define DEFAULT_CODE_LENGTH 8 #define DEFAULT_CODE_LENGTH 8
#define MAX_ALLOWED_CODE_LENGTH 15
#define NUM_LITERAL_CODES 256 #define NUM_LITERAL_CODES 256
#define NUM_LENGTH_CODES 24 #define NUM_LENGTH_CODES 24
#define NUM_DISTANCE_CODES 40 #define NUM_DISTANCE_CODES 40
#define CODE_LENGTH_CODES 19
#define TRANSFORM_PRESENT 1 // The bit to be written when next data #define TRANSFORM_PRESENT 1 // The bit to be written when next data
// to be read is a transform. // to be read is a transform.