2012-04-03 16:24:25 +02:00
|
|
|
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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/
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Author: Jyrki Alakuijala (jyrki@google.com)
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef WEBP_ENC_BACKWARD_REFERENCES_H_
|
|
|
|
#define WEBP_ENC_BACKWARD_REFERENCES_H_
|
|
|
|
|
2012-04-11 11:52:13 +02:00
|
|
|
#ifdef USE_LOSSLESS_ENCODER
|
|
|
|
|
2012-04-03 16:24:25 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "../webp/types.h"
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Backward reference distance prefix codes
|
|
|
|
#define DISTANCE_CODES_MAX 40
|
|
|
|
|
|
|
|
// Compression constants
|
|
|
|
#define CODE_LENGTH_CODES 19
|
|
|
|
static const int kLengthCodes = 24;
|
2012-04-25 09:33:57 +02:00
|
|
|
static const int kColorCacheBitsMax = 11;
|
2012-04-03 16:24:25 +02:00
|
|
|
#define PIX_OR_COPY_CODES_MAX (256 + 24 + (1 << 11))
|
|
|
|
static const int kMaxLength = 4096;
|
|
|
|
|
|
|
|
// use GNU builtins where available.
|
|
|
|
#if defined(__GNUC__) && \
|
|
|
|
((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
|
|
|
|
static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
|
|
|
|
return n == 0 ? -1 : 31 ^ __builtin_clz(n);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
|
|
|
|
int log;
|
|
|
|
uint32_t value;
|
|
|
|
int i;
|
|
|
|
if (n == 0)
|
|
|
|
return -1;
|
|
|
|
log = 0;
|
|
|
|
value = n;
|
|
|
|
for (i = 4; i >= 0; --i) {
|
|
|
|
int shift = (1 << i);
|
|
|
|
uint32_t x = value >> shift;
|
|
|
|
if (x != 0) {
|
|
|
|
value = x;
|
|
|
|
log += shift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return log;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-12 13:31:17 +02:00
|
|
|
static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int floor = BitsLog2Floor(n);
|
|
|
|
if (n == (n & ~(n - 1))) // zero or a power of two.
|
|
|
|
return floor;
|
|
|
|
else
|
|
|
|
return floor + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Splitting of distance and length codes into prefixes and
|
|
|
|
// extra bits. The prefixes are encoded with an entropy code
|
|
|
|
// while the extra bits are stored just as normal bits.
|
|
|
|
static WEBP_INLINE void PrefixEncode(
|
|
|
|
int distance,
|
|
|
|
int *code,
|
|
|
|
int *extra_bits_count,
|
|
|
|
int *extra_bits_value) {
|
|
|
|
// Collect the two most significant bits where the highest bit is 1.
|
|
|
|
const int highest_bit = BitsLog2Floor(--distance);
|
|
|
|
// & 0x3f is to make behavior well defined when highest_bit
|
|
|
|
// does not exist or is the least significant bit.
|
|
|
|
const int second_highest_bit =
|
|
|
|
(distance >> ((highest_bit - 1) & 0x3f)) & 1;
|
|
|
|
*extra_bits_count = (highest_bit > 0) ? highest_bit - 1 : 0;
|
|
|
|
*extra_bits_value = distance & ((1 << *extra_bits_count) - 1);
|
|
|
|
*code = (highest_bit > 0) ? 2 * highest_bit + second_highest_bit :
|
|
|
|
(highest_bit == 0) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Mode {
|
|
|
|
kLiteral,
|
2012-04-25 09:33:57 +02:00
|
|
|
kCacheIdx,
|
2012-04-03 16:24:25 +02:00
|
|
|
kCopy,
|
|
|
|
kNone,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
// mode as uint8_t to make the memory layout to be exactly 8 bytes.
|
|
|
|
uint8_t mode;
|
|
|
|
uint16_t len;
|
2012-04-25 09:33:57 +02:00
|
|
|
uint32_t argb_or_distance;
|
2012-04-03 16:24:25 +02:00
|
|
|
} PixOrCopy;
|
|
|
|
|
2012-04-26 11:55:14 +02:00
|
|
|
typedef struct {
|
|
|
|
PixOrCopy* refs;
|
|
|
|
int size;
|
|
|
|
} VP8LBackwardRefs;
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t distance,
|
2012-04-03 16:24:25 +02:00
|
|
|
uint16_t len) {
|
|
|
|
PixOrCopy retval;
|
|
|
|
retval.mode = kCopy;
|
2012-04-25 09:33:57 +02:00
|
|
|
retval.argb_or_distance = distance;
|
2012-04-03 16:24:25 +02:00
|
|
|
retval.len = len;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
static WEBP_INLINE PixOrCopy PixOrCopyCreateCacheIdx(int idx) {
|
2012-04-03 16:24:25 +02:00
|
|
|
PixOrCopy retval;
|
2012-04-25 09:33:57 +02:00
|
|
|
assert(idx >= 0);
|
|
|
|
assert(idx < (1 << kColorCacheBitsMax));
|
|
|
|
retval.mode = kCacheIdx;
|
|
|
|
retval.argb_or_distance = idx;
|
2012-04-03 16:24:25 +02:00
|
|
|
retval.len = 1;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) {
|
|
|
|
PixOrCopy retval;
|
|
|
|
retval.mode = kLiteral;
|
2012-04-25 09:33:57 +02:00
|
|
|
retval.argb_or_distance = argb;
|
2012-04-03 16:24:25 +02:00
|
|
|
retval.len = 1;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy* const p) {
|
2012-04-03 16:24:25 +02:00
|
|
|
return p->mode == kLiteral;
|
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
static WEBP_INLINE int PixOrCopyIsCacheIdx(const PixOrCopy* const p) {
|
|
|
|
return p->mode == kCacheIdx;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy* const p) {
|
2012-04-03 16:24:25 +02:00
|
|
|
return p->mode == kCopy;
|
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy* const p,
|
2012-04-03 16:24:25 +02:00
|
|
|
int component) {
|
|
|
|
assert(p->mode == kLiteral);
|
2012-04-25 09:33:57 +02:00
|
|
|
return (p->argb_or_distance >> (component * 8)) & 0xff;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy* const p) {
|
2012-04-03 16:24:25 +02:00
|
|
|
return p->len;
|
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
static WEBP_INLINE uint32_t PixOrCopyArgb(const PixOrCopy* const p) {
|
2012-04-03 16:24:25 +02:00
|
|
|
assert(p->mode == kLiteral);
|
2012-04-25 09:33:57 +02:00
|
|
|
return p->argb_or_distance;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
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;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy* const p) {
|
2012-04-03 16:24:25 +02:00
|
|
|
assert(p->mode == kCopy);
|
2012-04-25 09:33:57 +02:00
|
|
|
return p->argb_or_distance;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ridiculously simple backward references for images where it is unlikely
|
|
|
|
// that there are large backward references (photos).
|
2012-04-10 05:56:07 +02:00
|
|
|
void VP8LBackwardReferencesRle(
|
2012-04-25 09:33:57 +02:00
|
|
|
int xsize, int ysize, const uint32_t* const argb, PixOrCopy* const stream,
|
|
|
|
int* const stream_size);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
|
|
// This is a simple fast function for obtaining backward references
|
|
|
|
// based on simple heuristics. Returns 1 on success.
|
2012-04-10 05:56:07 +02:00
|
|
|
int VP8LBackwardReferencesHashChain(
|
2012-04-25 09:33:57 +02:00
|
|
|
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);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
|
|
// 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.
|
2012-04-10 05:56:07 +02:00
|
|
|
int VP8LBackwardReferencesTraceBackwards(
|
2012-04-25 09:33:57 +02:00
|
|
|
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);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
|
|
// 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.
|
2012-04-10 05:56:07 +02:00
|
|
|
void VP8LBackwardReferences2DLocality(int xsize, int data_size,
|
2012-04-25 09:33:57 +02:00
|
|
|
PixOrCopy* const data);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
|
|
// Internals of locality transform exposed for testing use.
|
2012-04-10 05:56:07 +02:00
|
|
|
int VP8LDistanceToPlaneCode(int xsize, int distance);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
|
|
// Returns true if the given backward references actually produce
|
|
|
|
// the image given in tuple (argb, xsize, ysize).
|
2012-04-25 09:33:57 +02:00
|
|
|
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 color cache size for the image.
|
|
|
|
int VP8LCalculateEstimateForCacheSize(
|
|
|
|
const uint32_t* const argb, int xsize, int ysize,
|
|
|
|
int* const best_cache_bits);
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-11 11:52:13 +02:00
|
|
|
#endif
|
|
|
|
|
2012-04-03 16:24:25 +02:00
|
|
|
#endif // WEBP_ENC_BACKWARD_REFERENCES_H_
|