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)
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "./backward_references.h"
|
|
|
|
#include "./histogram.h"
|
2012-08-02 03:22:06 +02:00
|
|
|
#include "../dsp/lossless.h"
|
2012-04-03 16:24:25 +02:00
|
|
|
#include "../utils/color_cache.h"
|
2012-08-01 21:06:04 +02:00
|
|
|
#include "../utils/utils.h"
|
2012-04-03 16:24:25 +02:00
|
|
|
|
|
|
|
#define VALUES_IN_BYTE 256
|
|
|
|
|
2012-05-25 11:52:44 +02:00
|
|
|
#define HASH_BITS 18
|
|
|
|
#define HASH_SIZE (1 << HASH_BITS)
|
|
|
|
#define HASH_MULTIPLIER (0xc6a4a7935bd1e995ULL)
|
|
|
|
|
|
|
|
// 1M window (4M bytes) minus 120 special codes for short distances.
|
|
|
|
#define WINDOW_SIZE ((1 << 20) - 120)
|
|
|
|
|
|
|
|
// Bounds for the match length.
|
|
|
|
#define MIN_LENGTH 2
|
|
|
|
#define MAX_LENGTH 4096
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
// Stores the most recently added position with the given hash value.
|
|
|
|
int32_t hash_to_first_index_[HASH_SIZE];
|
|
|
|
// chain_[pos] stores the previous position with the same hash value
|
|
|
|
// for every pixel in the image.
|
|
|
|
int32_t* chain_;
|
|
|
|
} HashChain;
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2012-04-03 16:24:25 +02:00
|
|
|
static const uint8_t plane_to_code_lut[128] = {
|
|
|
|
96, 73, 55, 39, 23, 13, 5, 1, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
101, 78, 58, 42, 26, 16, 8, 2, 0, 3, 9, 17, 27, 43, 59, 79,
|
|
|
|
102, 86, 62, 46, 32, 20, 10, 6, 4, 7, 11, 21, 33, 47, 63, 87,
|
|
|
|
105, 90, 70, 52, 37, 28, 18, 14, 12, 15, 19, 29, 38, 53, 71, 91,
|
|
|
|
110, 99, 82, 66, 48, 35, 30, 24, 22, 25, 31, 36, 49, 67, 83, 100,
|
|
|
|
115, 108, 94, 76, 64, 50, 44, 40, 34, 41, 45, 51, 65, 77, 95, 109,
|
|
|
|
118, 113, 103, 92, 80, 68, 60, 56, 54, 57, 61, 69, 81, 93, 104, 114,
|
2012-05-25 11:52:44 +02:00
|
|
|
119, 116, 111, 106, 97, 88, 84, 74, 72, 75, 85, 89, 98, 107, 112, 117
|
2012-04-03 16:24:25 +02:00
|
|
|
};
|
|
|
|
|
2012-05-09 08:41:55 +02:00
|
|
|
static int DistanceToPlaneCode(int xsize, int dist) {
|
2012-04-25 09:33:57 +02:00
|
|
|
const int yoffset = dist / xsize;
|
|
|
|
const int xoffset = dist - yoffset * xsize;
|
2012-04-03 16:24:25 +02:00
|
|
|
if (xoffset <= 8 && yoffset < 8) {
|
|
|
|
return plane_to_code_lut[yoffset * 16 + 8 - xoffset] + 1;
|
|
|
|
} else if (xoffset > xsize - 8 && yoffset < 7) {
|
|
|
|
return plane_to_code_lut[(yoffset + 1) * 16 + 8 + (xsize - xoffset)] + 1;
|
|
|
|
}
|
|
|
|
return dist + 120;
|
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
static WEBP_INLINE int FindMatchLength(const uint32_t* const array1,
|
|
|
|
const uint32_t* const array2,
|
2012-04-03 16:24:25 +02:00
|
|
|
const int max_limit) {
|
2012-04-25 09:33:57 +02:00
|
|
|
int match_len = 0;
|
|
|
|
while (match_len < max_limit && array1[match_len] == array2[match_len]) {
|
|
|
|
++match_len;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-04-25 09:33:57 +02:00
|
|
|
return match_len;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-25 11:52:44 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// VP8LBackwardRefs
|
|
|
|
|
|
|
|
void VP8LInitBackwardRefs(VP8LBackwardRefs* const refs) {
|
|
|
|
if (refs != NULL) {
|
|
|
|
refs->refs = NULL;
|
|
|
|
refs->size = 0;
|
|
|
|
refs->max_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VP8LClearBackwardRefs(VP8LBackwardRefs* const refs) {
|
|
|
|
if (refs != NULL) {
|
|
|
|
free(refs->refs);
|
|
|
|
VP8LInitBackwardRefs(refs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int VP8LBackwardRefsAlloc(VP8LBackwardRefs* const refs, int max_size) {
|
|
|
|
assert(refs != NULL);
|
|
|
|
refs->size = 0;
|
|
|
|
refs->max_size = 0;
|
2012-08-01 21:06:04 +02:00
|
|
|
refs->refs = (PixOrCopy*)WebPSafeMalloc((uint64_t)max_size,
|
|
|
|
sizeof(*refs->refs));
|
2012-05-25 11:52:44 +02:00
|
|
|
if (refs->refs == NULL) return 0;
|
|
|
|
refs->max_size = max_size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Hash chains
|
2012-04-03 16:24:25 +02:00
|
|
|
|
2012-05-09 08:41:55 +02:00
|
|
|
static WEBP_INLINE uint64_t GetPixPairHash64(const uint32_t* const argb) {
|
|
|
|
uint64_t key = ((uint64_t)(argb[1]) << 32) | argb[0];
|
2012-05-25 11:52:44 +02:00
|
|
|
key = (key * HASH_MULTIPLIER) >> (64 - HASH_BITS);
|
2012-05-09 08:41:55 +02:00
|
|
|
return key;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-25 11:52:44 +02:00
|
|
|
static int HashChainInit(HashChain* const p, int size) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int i;
|
2012-08-01 21:06:04 +02:00
|
|
|
p->chain_ = (int*)WebPSafeMalloc((uint64_t)size, sizeof(*p->chain_));
|
2012-04-30 14:18:50 +02:00
|
|
|
if (p->chain_ == NULL) {
|
2012-04-03 16:24:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < size; ++i) {
|
|
|
|
p->chain_[i] = -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < HASH_SIZE; ++i) {
|
|
|
|
p->hash_to_first_index_[i] = -1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-05-25 11:52:44 +02:00
|
|
|
static void HashChainDelete(HashChain* const p) {
|
2012-04-03 16:24:25 +02:00
|
|
|
if (p != NULL) {
|
|
|
|
free(p->chain_);
|
2012-05-25 11:52:44 +02:00
|
|
|
free(p);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-25 11:52:44 +02:00
|
|
|
// Insertion of two pixels at a time.
|
|
|
|
static void HashChainInsert(HashChain* const p,
|
|
|
|
const uint32_t* const argb, int pos) {
|
2012-05-09 08:41:55 +02:00
|
|
|
const uint64_t hash_code = GetPixPairHash64(argb);
|
2012-04-25 09:33:57 +02:00
|
|
|
p->chain_[pos] = p->hash_to_first_index_[hash_code];
|
|
|
|
p->hash_to_first_index_[hash_code] = pos;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-10-31 01:25:54 +01:00
|
|
|
static void GetParamsForHashChainFindCopy(int quality, int xsize,
|
|
|
|
int* window_size, int* iter_pos,
|
|
|
|
int* iter_limit) {
|
|
|
|
const int iter_mult = (quality < 27) ? 1 : 1 + ((quality - 27) >> 4);
|
|
|
|
// Limit the backward-ref window size for lower qualities.
|
|
|
|
const int max_window_size = (quality > 50) ? WINDOW_SIZE
|
|
|
|
: (quality > 25) ? (xsize << 8)
|
|
|
|
: (xsize << 4);
|
|
|
|
assert(xsize > 0);
|
|
|
|
*window_size = (max_window_size > WINDOW_SIZE) ? WINDOW_SIZE
|
|
|
|
: max_window_size;
|
|
|
|
*iter_pos = 5 + (quality >> 3);
|
|
|
|
*iter_limit = -quality * iter_mult;
|
|
|
|
}
|
|
|
|
|
2012-05-25 11:52:44 +02:00
|
|
|
static int HashChainFindCopy(const HashChain* const p,
|
2013-01-21 17:20:14 +01:00
|
|
|
int base_position, int xsize,
|
2012-05-25 11:52:44 +02:00
|
|
|
const uint32_t* const argb, int maxlen,
|
2012-10-31 01:25:54 +01:00
|
|
|
int window_size, int iter_pos, int iter_limit,
|
2012-05-25 11:52:44 +02:00
|
|
|
int* const distance_ptr,
|
|
|
|
int* const length_ptr) {
|
2013-01-21 17:20:14 +01:00
|
|
|
const uint64_t hash_code = GetPixPairHash64(&argb[base_position]);
|
2012-04-03 16:24:25 +02:00
|
|
|
int prev_length = 0;
|
|
|
|
int64_t best_val = 0;
|
2012-07-17 04:08:59 +02:00
|
|
|
int best_length = 0;
|
|
|
|
int best_distance = 0;
|
2013-01-21 17:20:14 +01:00
|
|
|
const uint32_t* const argb_start = argb + base_position;
|
|
|
|
const int min_pos =
|
|
|
|
(base_position > window_size) ? base_position - window_size : 0;
|
2012-07-17 04:08:59 +02:00
|
|
|
int pos;
|
|
|
|
|
2012-05-09 08:41:55 +02:00
|
|
|
assert(xsize > 0);
|
2012-04-03 16:24:25 +02:00
|
|
|
for (pos = p->hash_to_first_index_[hash_code];
|
|
|
|
pos >= min_pos;
|
|
|
|
pos = p->chain_[pos]) {
|
2012-07-17 04:08:59 +02:00
|
|
|
int64_t val;
|
2012-04-25 09:33:57 +02:00
|
|
|
int curr_length;
|
2012-10-31 01:25:54 +01:00
|
|
|
if (iter_pos < 0) {
|
|
|
|
if (iter_pos < iter_limit || best_val >= 0xff0000) {
|
2012-04-03 16:24:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-10-31 01:25:54 +01:00
|
|
|
--iter_pos;
|
2012-04-25 09:33:57 +02:00
|
|
|
if (best_length != 0 &&
|
2012-07-17 04:08:59 +02:00
|
|
|
argb[pos + best_length - 1] != argb_start[best_length - 1]) {
|
2012-04-03 16:24:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-07-17 04:08:59 +02:00
|
|
|
curr_length = FindMatchLength(argb + pos, argb_start, maxlen);
|
2012-04-25 09:33:57 +02:00
|
|
|
if (curr_length < prev_length) {
|
2012-04-03 16:24:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-25 09:33:57 +02:00
|
|
|
val = 65536 * curr_length;
|
2012-04-03 16:24:25 +02:00
|
|
|
// Favoring 2d locality here gives savings for certain images.
|
2013-01-21 17:20:14 +01:00
|
|
|
if (base_position - pos < 9 * xsize) {
|
|
|
|
const int y = (base_position - pos) / xsize;
|
|
|
|
int x = (base_position - pos) % xsize;
|
2012-04-03 16:24:25 +02:00
|
|
|
if (x > xsize / 2) {
|
|
|
|
x = xsize - x;
|
|
|
|
}
|
|
|
|
if (x <= 7 && x >= -8) {
|
|
|
|
val -= y * y + x * x;
|
|
|
|
} else {
|
|
|
|
val -= 9 * 9 + 9 * 9;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val -= 9 * 9 + 9 * 9;
|
|
|
|
}
|
|
|
|
if (best_val < val) {
|
2012-04-25 09:33:57 +02:00
|
|
|
prev_length = curr_length;
|
2012-04-03 16:24:25 +02:00
|
|
|
best_val = val;
|
2012-04-25 09:33:57 +02:00
|
|
|
best_length = curr_length;
|
2013-01-21 17:20:14 +01:00
|
|
|
best_distance = base_position - pos;
|
2012-05-25 11:52:44 +02:00
|
|
|
if (curr_length >= MAX_LENGTH) {
|
2012-04-03 16:24:25 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-04-25 09:33:57 +02:00
|
|
|
if ((best_distance == 1 || best_distance == xsize) &&
|
|
|
|
best_length >= 128) {
|
2012-04-03 16:24:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-25 09:33:57 +02:00
|
|
|
*distance_ptr = best_distance;
|
|
|
|
*length_ptr = best_length;
|
2012-05-25 11:52:44 +02:00
|
|
|
return (best_length >= MIN_LENGTH);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
static WEBP_INLINE void PushBackCopy(VP8LBackwardRefs* const refs, int length) {
|
2012-05-25 11:52:44 +02:00
|
|
|
int size = refs->size;
|
|
|
|
while (length >= MAX_LENGTH) {
|
|
|
|
refs->refs[size++] = PixOrCopyCreateCopy(1, MAX_LENGTH);
|
|
|
|
length -= MAX_LENGTH;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
if (length > 0) {
|
2012-05-25 11:52:44 +02:00
|
|
|
refs->refs[size++] = PixOrCopyCreateCopy(1, length);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-05-25 11:52:44 +02:00
|
|
|
refs->size = size;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-25 11:52:44 +02:00
|
|
|
static void BackwardReferencesRle(int xsize, int ysize,
|
|
|
|
const uint32_t* const argb,
|
|
|
|
VP8LBackwardRefs* const refs) {
|
2012-04-03 16:24:25 +02:00
|
|
|
const int pix_count = xsize * ysize;
|
2012-04-25 09:33:57 +02:00
|
|
|
int match_len = 0;
|
2012-04-03 16:24:25 +02:00
|
|
|
int i;
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->size = 0;
|
2012-05-25 11:52:44 +02:00
|
|
|
PushBackCopy(refs, match_len); // i=0 case
|
|
|
|
refs->refs[refs->size++] = PixOrCopyCreateLiteral(argb[0]);
|
|
|
|
for (i = 1; i < pix_count; ++i) {
|
|
|
|
if (argb[i] == argb[i - 1]) {
|
2012-04-25 09:33:57 +02:00
|
|
|
++match_len;
|
2012-04-03 16:24:25 +02:00
|
|
|
} else {
|
2012-04-30 14:18:50 +02:00
|
|
|
PushBackCopy(refs, match_len);
|
2012-04-25 09:33:57 +02:00
|
|
|
match_len = 0;
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->refs[refs->size++] = PixOrCopyCreateLiteral(argb[i]);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 14:18:50 +02:00
|
|
|
PushBackCopy(refs, match_len);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-25 11:52:44 +02:00
|
|
|
static int BackwardReferencesHashChain(int xsize, int ysize,
|
|
|
|
const uint32_t* const argb,
|
|
|
|
int cache_bits, int quality,
|
|
|
|
VP8LBackwardRefs* const refs) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int i;
|
|
|
|
int ok = 0;
|
2012-05-09 08:41:55 +02:00
|
|
|
int cc_init = 0;
|
|
|
|
const int use_color_cache = (cache_bits > 0);
|
2012-04-24 11:55:19 +02:00
|
|
|
const int pix_count = xsize * ysize;
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChain* const hash_chain = (HashChain*)malloc(sizeof(*hash_chain));
|
2012-04-03 16:24:25 +02:00
|
|
|
VP8LColorCache hashers;
|
2012-10-31 01:25:54 +01:00
|
|
|
int window_size = WINDOW_SIZE;
|
|
|
|
int iter_pos = 1;
|
|
|
|
int iter_limit = -1;
|
2012-05-09 08:41:55 +02:00
|
|
|
|
|
|
|
if (hash_chain == NULL) return 0;
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) {
|
|
|
|
cc_init = VP8LColorCacheInit(&hashers, cache_bits);
|
|
|
|
if (!cc_init) goto Error;
|
|
|
|
}
|
2012-05-25 11:52:44 +02:00
|
|
|
|
|
|
|
if (!HashChainInit(hash_chain, pix_count)) goto Error;
|
2012-05-09 08:41:55 +02:00
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->size = 0;
|
2012-10-31 01:25:54 +01:00
|
|
|
GetParamsForHashChainFindCopy(quality, xsize, &window_size, &iter_pos,
|
|
|
|
&iter_limit);
|
2012-04-03 16:24:25 +02:00
|
|
|
for (i = 0; i < pix_count; ) {
|
|
|
|
// Alternative#1: Code the pixels starting at 'i' using backward reference.
|
|
|
|
int offset = 0;
|
|
|
|
int len = 0;
|
|
|
|
if (i < pix_count - 1) { // FindCopy(i,..) reads pixels at [i] and [i + 1].
|
|
|
|
int maxlen = pix_count - i;
|
2012-05-25 11:52:44 +02:00
|
|
|
if (maxlen > MAX_LENGTH) {
|
|
|
|
maxlen = MAX_LENGTH;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-10-31 01:25:54 +01:00
|
|
|
HashChainFindCopy(hash_chain, i, xsize, argb, maxlen,
|
|
|
|
window_size, iter_pos, iter_limit,
|
2012-05-09 08:41:55 +02:00
|
|
|
&offset, &len);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-05-25 11:52:44 +02:00
|
|
|
if (len >= MIN_LENGTH) {
|
2012-04-03 16:24:25 +02:00
|
|
|
// Alternative#2: Insert the pixel at 'i' as literal, and code the
|
|
|
|
// pixels starting at 'i + 1' using backward reference.
|
|
|
|
int offset2 = 0;
|
|
|
|
int len2 = 0;
|
|
|
|
int k;
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChainInsert(hash_chain, &argb[i], i);
|
2012-04-03 16:24:25 +02:00
|
|
|
if (i < pix_count - 2) { // FindCopy(i+1,..) reads [i + 1] and [i + 2].
|
|
|
|
int maxlen = pix_count - (i + 1);
|
2012-05-25 11:52:44 +02:00
|
|
|
if (maxlen > MAX_LENGTH) {
|
|
|
|
maxlen = MAX_LENGTH;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-10-31 01:25:54 +01:00
|
|
|
HashChainFindCopy(hash_chain, i + 1, xsize, argb, maxlen,
|
|
|
|
window_size, iter_pos, iter_limit,
|
|
|
|
&offset2, &len2);
|
2012-04-03 16:24:25 +02:00
|
|
|
if (len2 > len + 1) {
|
2012-08-01 09:32:12 +02:00
|
|
|
const uint32_t pixel = argb[i];
|
2012-04-03 16:24:25 +02:00
|
|
|
// Alternative#2 is a better match. So push pixel at 'i' as literal.
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache && VP8LColorCacheContains(&hashers, pixel)) {
|
|
|
|
const int ix = VP8LColorCacheGetIndex(&hashers, pixel);
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->refs[refs->size] = PixOrCopyCreateCacheIdx(ix);
|
2012-04-03 16:24:25 +02:00
|
|
|
} else {
|
2012-08-01 09:32:12 +02:00
|
|
|
refs->refs[refs->size] = PixOrCopyCreateLiteral(pixel);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-04-30 14:18:50 +02:00
|
|
|
++refs->size;
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) VP8LColorCacheInsert(&hashers, pixel);
|
2012-04-03 16:24:25 +02:00
|
|
|
i++; // Backward reference to be done for next pixel.
|
|
|
|
len = len2;
|
|
|
|
offset = offset2;
|
|
|
|
}
|
|
|
|
}
|
2012-05-25 11:52:44 +02:00
|
|
|
if (len >= MAX_LENGTH) {
|
|
|
|
len = MAX_LENGTH - 1;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->refs[refs->size++] = PixOrCopyCreateCopy(offset, len);
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) {
|
|
|
|
for (k = 0; k < len; ++k) {
|
|
|
|
VP8LColorCacheInsert(&hashers, argb[i + k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add to the hash_chain (but cannot add the last pixel).
|
|
|
|
{
|
|
|
|
const int last = (len < pix_count - 1 - i) ? len : pix_count - 1 - i;
|
|
|
|
for (k = 1; k < last; ++k) {
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChainInsert(hash_chain, &argb[i + k], i + k);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i += len;
|
|
|
|
} else {
|
2012-08-01 09:32:12 +02:00
|
|
|
const uint32_t pixel = argb[i];
|
|
|
|
if (use_color_cache && VP8LColorCacheContains(&hashers, pixel)) {
|
2012-04-25 09:33:57 +02:00
|
|
|
// push pixel as a PixOrCopyCreateCacheIdx pixel
|
2012-08-01 09:32:12 +02:00
|
|
|
const int ix = VP8LColorCacheGetIndex(&hashers, pixel);
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->refs[refs->size] = PixOrCopyCreateCacheIdx(ix);
|
2012-04-03 16:24:25 +02:00
|
|
|
} else {
|
2012-08-01 09:32:12 +02:00
|
|
|
refs->refs[refs->size] = PixOrCopyCreateLiteral(pixel);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-04-30 14:18:50 +02:00
|
|
|
++refs->size;
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) VP8LColorCacheInsert(&hashers, pixel);
|
2012-04-03 16:24:25 +02:00
|
|
|
if (i + 1 < pix_count) {
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChainInsert(hash_chain, &argb[i], i);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ok = 1;
|
|
|
|
Error:
|
2012-05-09 08:41:55 +02:00
|
|
|
if (cc_init) VP8LColorCacheClear(&hashers);
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChainDelete(hash_chain);
|
2012-04-03 16:24:25 +02:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2012-04-03 16:24:25 +02:00
|
|
|
typedef struct {
|
|
|
|
double alpha_[VALUES_IN_BYTE];
|
|
|
|
double red_[VALUES_IN_BYTE];
|
|
|
|
double literal_[PIX_OR_COPY_CODES_MAX];
|
|
|
|
double blue_[VALUES_IN_BYTE];
|
2012-05-24 14:15:05 +02:00
|
|
|
double distance_[NUM_DISTANCE_CODES];
|
2012-04-03 16:24:25 +02:00
|
|
|
} CostModel;
|
|
|
|
|
2012-05-09 08:41:55 +02:00
|
|
|
static int BackwardReferencesTraceBackwards(
|
|
|
|
int xsize, int ysize, int recursive_cost_model,
|
2012-10-05 18:55:06 +02:00
|
|
|
const uint32_t* const argb, int quality, int cache_bits,
|
|
|
|
VP8LBackwardRefs* const refs);
|
2012-05-09 08:41:55 +02:00
|
|
|
|
2012-08-02 03:22:06 +02:00
|
|
|
static void ConvertPopulationCountTableToBitEstimates(
|
|
|
|
int num_symbols, const int population_counts[], double output[]) {
|
|
|
|
int sum = 0;
|
|
|
|
int nonzeros = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < num_symbols; ++i) {
|
|
|
|
sum += population_counts[i];
|
|
|
|
if (population_counts[i] > 0) {
|
|
|
|
++nonzeros;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nonzeros <= 1) {
|
|
|
|
memset(output, 0, num_symbols * sizeof(*output));
|
|
|
|
} else {
|
|
|
|
const double logsum = VP8LFastLog2(sum);
|
|
|
|
for (i = 0; i < num_symbols; ++i) {
|
|
|
|
output[i] = logsum - VP8LFastLog2(population_counts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int CostModelBuild(CostModel* const m, int xsize, int ysize,
|
2012-05-09 08:41:55 +02:00
|
|
|
int recursion_level, const uint32_t* const argb,
|
2012-10-05 18:55:06 +02:00
|
|
|
int quality, int cache_bits) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int ok = 0;
|
2012-04-10 05:56:07 +02:00
|
|
|
VP8LHistogram histo;
|
2012-04-30 14:18:50 +02:00
|
|
|
VP8LBackwardRefs refs;
|
|
|
|
|
|
|
|
if (!VP8LBackwardRefsAlloc(&refs, xsize * ysize)) goto Error;
|
|
|
|
|
2012-04-03 16:24:25 +02:00
|
|
|
if (recursion_level > 0) {
|
2012-05-09 08:41:55 +02:00
|
|
|
if (!BackwardReferencesTraceBackwards(xsize, ysize, recursion_level - 1,
|
2012-10-05 18:55:06 +02:00
|
|
|
argb, quality, cache_bits, &refs)) {
|
2012-04-03 16:24:25 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
} else {
|
2012-05-09 08:41:55 +02:00
|
|
|
if (!BackwardReferencesHashChain(xsize, ysize, argb, cache_bits, quality,
|
|
|
|
&refs)) {
|
2012-04-03 16:24:25 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 14:18:50 +02:00
|
|
|
VP8LHistogramCreate(&histo, &refs, cache_bits);
|
2012-08-02 03:22:06 +02:00
|
|
|
ConvertPopulationCountTableToBitEstimates(
|
|
|
|
VP8LHistogramNumCodes(&histo), histo.literal_, m->literal_);
|
|
|
|
ConvertPopulationCountTableToBitEstimates(
|
|
|
|
VALUES_IN_BYTE, histo.red_, m->red_);
|
|
|
|
ConvertPopulationCountTableToBitEstimates(
|
|
|
|
VALUES_IN_BYTE, histo.blue_, m->blue_);
|
|
|
|
ConvertPopulationCountTableToBitEstimates(
|
|
|
|
VALUES_IN_BYTE, histo.alpha_, m->alpha_);
|
|
|
|
ConvertPopulationCountTableToBitEstimates(
|
|
|
|
NUM_DISTANCE_CODES, histo.distance_, m->distance_);
|
2012-04-03 16:24:25 +02:00
|
|
|
ok = 1;
|
2012-04-30 14:18:50 +02:00
|
|
|
|
|
|
|
Error:
|
|
|
|
VP8LClearBackwardRefs(&refs);
|
2012-04-03 16:24:25 +02:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2012-08-02 03:22:06 +02:00
|
|
|
static WEBP_INLINE double GetLiteralCost(const CostModel* const m, uint32_t v) {
|
|
|
|
return m->alpha_[v >> 24] +
|
|
|
|
m->red_[(v >> 16) & 0xff] +
|
|
|
|
m->literal_[(v >> 8) & 0xff] +
|
|
|
|
m->blue_[v & 0xff];
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 03:22:06 +02:00
|
|
|
static WEBP_INLINE double GetCacheCost(const CostModel* const m, uint32_t idx) {
|
2012-05-24 14:15:05 +02:00
|
|
|
const int literal_idx = VALUES_IN_BYTE + NUM_LENGTH_CODES + idx;
|
2012-08-02 03:22:06 +02:00
|
|
|
return m->literal_[literal_idx];
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 03:22:06 +02:00
|
|
|
static WEBP_INLINE double GetLengthCost(const CostModel* const m,
|
2012-04-25 09:33:57 +02:00
|
|
|
uint32_t length) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int code, extra_bits_count, extra_bits_value;
|
2012-04-25 09:33:57 +02:00
|
|
|
PrefixEncode(length, &code, &extra_bits_count, &extra_bits_value);
|
2012-08-02 03:22:06 +02:00
|
|
|
return m->literal_[VALUES_IN_BYTE + code] + extra_bits_count;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 03:22:06 +02:00
|
|
|
static WEBP_INLINE double GetDistanceCost(const CostModel* const m,
|
2012-04-25 09:33:57 +02:00
|
|
|
uint32_t distance) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int code, extra_bits_count, extra_bits_value;
|
|
|
|
PrefixEncode(distance, &code, &extra_bits_count, &extra_bits_value);
|
2012-08-02 03:22:06 +02:00
|
|
|
return m->distance_[code] + extra_bits_count;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int BackwardReferencesHashChainDistanceOnly(
|
2012-05-09 08:41:55 +02:00
|
|
|
int xsize, int ysize, int recursive_cost_model, const uint32_t* const argb,
|
2012-10-05 18:55:06 +02:00
|
|
|
int quality, int cache_bits, uint32_t* const dist_array) {
|
2012-05-09 08:41:55 +02:00
|
|
|
int i;
|
|
|
|
int ok = 0;
|
|
|
|
int cc_init = 0;
|
2012-04-03 16:24:25 +02:00
|
|
|
const int pix_count = xsize * ysize;
|
2012-05-09 08:41:55 +02:00
|
|
|
const int use_color_cache = (cache_bits > 0);
|
2012-10-03 15:15:58 +02:00
|
|
|
float* const cost =
|
|
|
|
(float*)WebPSafeMalloc((uint64_t)pix_count, sizeof(*cost));
|
2012-04-03 16:24:25 +02:00
|
|
|
CostModel* cost_model = (CostModel*)malloc(sizeof(*cost_model));
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChain* hash_chain = (HashChain*)malloc(sizeof(*hash_chain));
|
2012-05-09 08:41:55 +02:00
|
|
|
VP8LColorCache hashers;
|
2012-05-25 11:52:44 +02:00
|
|
|
const double mul0 = (recursive_cost_model != 0) ? 1.0 : 0.68;
|
|
|
|
const double mul1 = (recursive_cost_model != 0) ? 1.0 : 0.82;
|
2013-02-05 19:42:23 +01:00
|
|
|
const int min_distance_code = 2; // TODO(vikasa): tune as function of quality
|
2012-10-31 01:25:54 +01:00
|
|
|
int window_size = WINDOW_SIZE;
|
|
|
|
int iter_pos = 1;
|
|
|
|
int iter_limit = -1;
|
2012-05-09 08:41:55 +02:00
|
|
|
|
2012-05-25 11:52:44 +02:00
|
|
|
if (cost == NULL || cost_model == NULL || hash_chain == NULL) goto Error;
|
|
|
|
|
2012-08-01 09:32:12 +02:00
|
|
|
if (!HashChainInit(hash_chain, pix_count)) goto Error;
|
|
|
|
|
|
|
|
if (use_color_cache) {
|
|
|
|
cc_init = VP8LColorCacheInit(&hashers, cache_bits);
|
|
|
|
if (!cc_init) goto Error;
|
|
|
|
}
|
2012-05-25 11:52:44 +02:00
|
|
|
|
|
|
|
if (!CostModelBuild(cost_model, xsize, ysize, recursive_cost_model, argb,
|
2012-10-05 18:55:06 +02:00
|
|
|
quality, cache_bits)) {
|
2012-04-03 16:24:25 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
2012-05-25 11:52:44 +02:00
|
|
|
|
2012-10-03 15:15:58 +02:00
|
|
|
for (i = 0; i < pix_count; ++i) cost[i] = 1e38f;
|
2012-05-25 11:52:44 +02:00
|
|
|
|
2012-04-03 16:24:25 +02:00
|
|
|
// We loop one pixel at a time, but store all currently best points to
|
|
|
|
// non-processed locations from this point.
|
|
|
|
dist_array[0] = 0;
|
2012-10-31 01:25:54 +01:00
|
|
|
GetParamsForHashChainFindCopy(quality, xsize, &window_size, &iter_pos,
|
|
|
|
&iter_limit);
|
2012-04-03 16:24:25 +02:00
|
|
|
for (i = 0; i < pix_count; ++i) {
|
|
|
|
double prev_cost = 0.0;
|
|
|
|
int shortmax;
|
|
|
|
if (i > 0) {
|
|
|
|
prev_cost = cost[i - 1];
|
|
|
|
}
|
|
|
|
for (shortmax = 0; shortmax < 2; ++shortmax) {
|
|
|
|
int offset = 0;
|
|
|
|
int len = 0;
|
|
|
|
if (i < pix_count - 1) { // FindCopy reads pixels at [i] and [i + 1].
|
2012-05-25 11:52:44 +02:00
|
|
|
int maxlen = shortmax ? 2 : MAX_LENGTH;
|
2012-04-03 16:24:25 +02:00
|
|
|
if (maxlen > pix_count - i) {
|
|
|
|
maxlen = pix_count - i;
|
|
|
|
}
|
2012-10-31 01:25:54 +01:00
|
|
|
HashChainFindCopy(hash_chain, i, xsize, argb, maxlen,
|
|
|
|
window_size, iter_pos, iter_limit,
|
2012-05-09 08:41:55 +02:00
|
|
|
&offset, &len);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-05-25 11:52:44 +02:00
|
|
|
if (len >= MIN_LENGTH) {
|
2012-05-09 08:41:55 +02:00
|
|
|
const int code = DistanceToPlaneCode(xsize, offset);
|
2012-04-03 16:24:25 +02:00
|
|
|
const double distance_cost =
|
2012-04-25 09:33:57 +02:00
|
|
|
prev_cost + GetDistanceCost(cost_model, code);
|
2012-04-03 16:24:25 +02:00
|
|
|
int k;
|
|
|
|
for (k = 1; k < len; ++k) {
|
2012-10-03 15:15:58 +02:00
|
|
|
const double cost_val = distance_cost + GetLengthCost(cost_model, k);
|
2012-04-03 16:24:25 +02:00
|
|
|
if (cost[i + k] > cost_val) {
|
2012-10-03 15:15:58 +02:00
|
|
|
cost[i + k] = (float)cost_val;
|
2012-04-03 16:24:25 +02:00
|
|
|
dist_array[i + k] = k + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This if is for speedup only. It roughly doubles the speed, and
|
|
|
|
// makes compression worse by .1 %.
|
2013-02-05 19:42:23 +01:00
|
|
|
if (len >= 128 && code <= min_distance_code) {
|
2012-04-03 16:24:25 +02:00
|
|
|
// Long copy for short distances, let's skip the middle
|
|
|
|
// lookups for better copies.
|
|
|
|
// 1) insert the hashes.
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) {
|
|
|
|
for (k = 0; k < len; ++k) {
|
|
|
|
VP8LColorCacheInsert(&hashers, argb[i + k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 2) Add to the hash_chain (but cannot add the last pixel)
|
|
|
|
{
|
2013-03-05 23:05:00 +01:00
|
|
|
const int last = (len + i < pix_count - 1) ? len + i
|
|
|
|
: pix_count - 1;
|
|
|
|
for (k = i; k < last; ++k) {
|
|
|
|
HashChainInsert(hash_chain, &argb[k], k);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-01 09:32:12 +02:00
|
|
|
// 3) jump.
|
2012-04-03 16:24:25 +02:00
|
|
|
i += len - 1; // for loop does ++i, thus -1 here.
|
|
|
|
goto next_symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < pix_count - 1) {
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChainInsert(hash_chain, &argb[i], i);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// inserting a literal pixel
|
|
|
|
double cost_val = prev_cost;
|
2012-04-25 09:33:57 +02:00
|
|
|
if (use_color_cache && VP8LColorCacheContains(&hashers, argb[i])) {
|
2012-05-25 11:52:44 +02:00
|
|
|
const int ix = VP8LColorCacheGetIndex(&hashers, argb[i]);
|
2012-04-25 09:33:57 +02:00
|
|
|
cost_val += GetCacheCost(cost_model, ix) * mul0;
|
2012-04-03 16:24:25 +02:00
|
|
|
} else {
|
2012-04-25 09:33:57 +02:00
|
|
|
cost_val += GetLiteralCost(cost_model, argb[i]) * mul1;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
if (cost[i] > cost_val) {
|
2012-10-09 03:27:27 +02:00
|
|
|
cost[i] = (float)cost_val;
|
2012-04-03 16:24:25 +02:00
|
|
|
dist_array[i] = 1; // only one is inserted.
|
|
|
|
}
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) VP8LColorCacheInsert(&hashers, argb[i]);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
next_symbol: ;
|
|
|
|
}
|
|
|
|
// Last pixel still to do, it can only be a single step if not reached
|
|
|
|
// through cheaper means already.
|
|
|
|
ok = 1;
|
|
|
|
Error:
|
2012-05-09 08:41:55 +02:00
|
|
|
if (cc_init) VP8LColorCacheClear(&hashers);
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChainDelete(hash_chain);
|
2012-04-03 16:24:25 +02:00
|
|
|
free(cost_model);
|
|
|
|
free(cost);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2013-02-05 19:43:43 +01:00
|
|
|
// We pack the path at the end of *dist_array and return
|
|
|
|
// a pointer to this part of the array. Example:
|
|
|
|
// dist_array = [1x2xx3x2] => packed [1x2x1232], chosen_path = [1232]
|
|
|
|
static void TraceBackwards(uint32_t* const dist_array,
|
|
|
|
int dist_array_size,
|
|
|
|
uint32_t** const chosen_path,
|
|
|
|
int* const chosen_path_size) {
|
|
|
|
uint32_t* path = dist_array + dist_array_size;
|
|
|
|
uint32_t* cur = dist_array + dist_array_size - 1;
|
|
|
|
while (cur >= dist_array) {
|
|
|
|
const int k = *cur;
|
|
|
|
--path;
|
|
|
|
*path = k;
|
|
|
|
cur -= k;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2013-02-05 19:43:43 +01:00
|
|
|
*chosen_path = path;
|
2013-02-06 17:51:10 +01:00
|
|
|
*chosen_path_size = (int)(dist_array + dist_array_size - path);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int BackwardReferencesHashChainFollowChosenPath(
|
2012-10-05 18:55:06 +02:00
|
|
|
int xsize, int ysize, const uint32_t* const argb,
|
|
|
|
int quality, int cache_bits,
|
2012-05-09 08:41:55 +02:00
|
|
|
const uint32_t* const chosen_path, int chosen_path_size,
|
2012-04-30 14:18:50 +02:00
|
|
|
VP8LBackwardRefs* const refs) {
|
2012-04-03 16:24:25 +02:00
|
|
|
const int pix_count = xsize * ysize;
|
2012-05-09 08:41:55 +02:00
|
|
|
const int use_color_cache = (cache_bits > 0);
|
2012-04-30 14:18:50 +02:00
|
|
|
int size = 0;
|
2012-04-03 16:24:25 +02:00
|
|
|
int i = 0;
|
|
|
|
int k;
|
|
|
|
int ix;
|
|
|
|
int ok = 0;
|
2012-05-09 08:41:55 +02:00
|
|
|
int cc_init = 0;
|
2012-10-31 01:25:54 +01:00
|
|
|
int window_size = WINDOW_SIZE;
|
|
|
|
int iter_pos = 1;
|
|
|
|
int iter_limit = -1;
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChain* hash_chain = (HashChain*)malloc(sizeof(*hash_chain));
|
2012-05-09 08:41:55 +02:00
|
|
|
VP8LColorCache hashers;
|
|
|
|
|
2012-08-01 09:32:12 +02:00
|
|
|
if (hash_chain == NULL || !HashChainInit(hash_chain, pix_count)) {
|
2012-04-03 16:24:25 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) {
|
|
|
|
cc_init = VP8LColorCacheInit(&hashers, cache_bits);
|
|
|
|
if (!cc_init) goto Error;
|
|
|
|
}
|
2012-05-09 08:41:55 +02:00
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->size = 0;
|
2012-10-31 01:25:54 +01:00
|
|
|
GetParamsForHashChainFindCopy(quality, xsize, &window_size, &iter_pos,
|
|
|
|
&iter_limit);
|
2012-04-30 14:18:50 +02:00
|
|
|
for (ix = 0; ix < chosen_path_size; ++ix, ++size) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int offset = 0;
|
|
|
|
int len = 0;
|
|
|
|
int maxlen = chosen_path[ix];
|
|
|
|
if (maxlen != 1) {
|
2012-10-31 01:25:54 +01:00
|
|
|
HashChainFindCopy(hash_chain, i, xsize, argb, maxlen,
|
|
|
|
window_size, iter_pos, iter_limit,
|
|
|
|
&offset, &len);
|
2012-04-03 16:24:25 +02:00
|
|
|
assert(len == maxlen);
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->refs[size] = PixOrCopyCreateCopy(offset, len);
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) {
|
|
|
|
for (k = 0; k < len; ++k) {
|
|
|
|
VP8LColorCacheInsert(&hashers, argb[i + k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const int last = (len < pix_count - 1 - i) ? len : pix_count - 1 - i;
|
|
|
|
for (k = 0; k < last; ++k) {
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChainInsert(hash_chain, &argb[i + k], i + k);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i += len;
|
|
|
|
} else {
|
2012-04-25 09:33:57 +02:00
|
|
|
if (use_color_cache && VP8LColorCacheContains(&hashers, argb[i])) {
|
|
|
|
// push pixel as a color cache index
|
2012-05-15 22:17:34 +02:00
|
|
|
const int idx = VP8LColorCacheGetIndex(&hashers, argb[i]);
|
|
|
|
refs->refs[size] = PixOrCopyCreateCacheIdx(idx);
|
2012-04-03 16:24:25 +02:00
|
|
|
} else {
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->refs[size] = PixOrCopyCreateLiteral(argb[i]);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) VP8LColorCacheInsert(&hashers, argb[i]);
|
2012-04-03 16:24:25 +02:00
|
|
|
if (i + 1 < pix_count) {
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChainInsert(hash_chain, &argb[i], i);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2012-06-11 08:56:19 +02:00
|
|
|
assert(size <= refs->max_size);
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->size = size;
|
2012-04-03 16:24:25 +02:00
|
|
|
ok = 1;
|
|
|
|
Error:
|
2012-05-09 08:41:55 +02:00
|
|
|
if (cc_init) VP8LColorCacheClear(&hashers);
|
2012-05-25 11:52:44 +02:00
|
|
|
HashChainDelete(hash_chain);
|
2012-04-03 16:24:25 +02:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns 1 on success.
|
2012-05-25 11:52:44 +02:00
|
|
|
static int BackwardReferencesTraceBackwards(int xsize, int ysize,
|
|
|
|
int recursive_cost_model,
|
|
|
|
const uint32_t* const argb,
|
2012-10-05 18:55:06 +02:00
|
|
|
int quality, int cache_bits,
|
2012-05-25 11:52:44 +02:00
|
|
|
VP8LBackwardRefs* const refs) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int ok = 0;
|
|
|
|
const int dist_array_size = xsize * ysize;
|
|
|
|
uint32_t* chosen_path = NULL;
|
|
|
|
int chosen_path_size = 0;
|
2012-05-25 11:52:44 +02:00
|
|
|
uint32_t* dist_array =
|
2012-08-01 21:06:04 +02:00
|
|
|
(uint32_t*)WebPSafeMalloc((uint64_t)dist_array_size, sizeof(*dist_array));
|
2012-05-25 11:52:44 +02:00
|
|
|
|
|
|
|
if (dist_array == NULL) goto Error;
|
|
|
|
|
2012-04-03 16:24:25 +02:00
|
|
|
if (!BackwardReferencesHashChainDistanceOnly(
|
2012-10-05 18:55:06 +02:00
|
|
|
xsize, ysize, recursive_cost_model, argb, quality, cache_bits,
|
|
|
|
dist_array)) {
|
2012-04-03 16:24:25 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
2013-02-05 19:43:43 +01:00
|
|
|
TraceBackwards(dist_array, dist_array_size, &chosen_path, &chosen_path_size);
|
2012-04-03 16:24:25 +02:00
|
|
|
if (!BackwardReferencesHashChainFollowChosenPath(
|
2012-10-05 18:55:06 +02:00
|
|
|
xsize, ysize, argb, quality, cache_bits, chosen_path, chosen_path_size,
|
|
|
|
refs)) {
|
2012-04-03 16:24:25 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
ok = 1;
|
2012-04-30 14:18:50 +02:00
|
|
|
Error:
|
2012-05-25 11:52:44 +02:00
|
|
|
free(dist_array);
|
2012-04-03 16:24:25 +02:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2012-05-09 08:41:55 +02:00
|
|
|
static void BackwardReferences2DLocality(int xsize,
|
|
|
|
VP8LBackwardRefs* const refs) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int i;
|
2012-04-30 14:18:50 +02:00
|
|
|
for (i = 0; i < refs->size; ++i) {
|
|
|
|
if (PixOrCopyIsCopy(&refs->refs[i])) {
|
|
|
|
const int dist = refs->refs[i].argb_or_distance;
|
2012-05-09 08:41:55 +02:00
|
|
|
const int transformed_dist = DistanceToPlaneCode(xsize, dist);
|
2012-04-30 14:18:50 +02:00
|
|
|
refs->refs[i].argb_or_distance = transformed_dist;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-09 08:41:55 +02:00
|
|
|
int VP8LGetBackwardReferences(int width, int height,
|
|
|
|
const uint32_t* const argb,
|
|
|
|
int quality, int cache_bits, int use_2d_locality,
|
|
|
|
VP8LBackwardRefs* const best) {
|
|
|
|
int ok = 0;
|
|
|
|
int lz77_is_useful;
|
|
|
|
VP8LBackwardRefs refs_rle, refs_lz77;
|
|
|
|
const int num_pix = width * height;
|
|
|
|
|
|
|
|
VP8LBackwardRefsAlloc(&refs_rle, num_pix);
|
|
|
|
VP8LBackwardRefsAlloc(&refs_lz77, num_pix);
|
|
|
|
VP8LInitBackwardRefs(best);
|
|
|
|
if (refs_rle.refs == NULL || refs_lz77.refs == NULL) {
|
|
|
|
Error1:
|
|
|
|
VP8LClearBackwardRefs(&refs_rle);
|
|
|
|
VP8LClearBackwardRefs(&refs_lz77);
|
|
|
|
goto End;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BackwardReferencesHashChain(width, height, argb, cache_bits, quality,
|
|
|
|
&refs_lz77)) {
|
|
|
|
goto End;
|
|
|
|
}
|
|
|
|
// Backward Reference using RLE only.
|
|
|
|
BackwardReferencesRle(width, height, argb, &refs_rle);
|
|
|
|
|
|
|
|
{
|
|
|
|
double bit_cost_lz77, bit_cost_rle;
|
|
|
|
VP8LHistogram* const histo = (VP8LHistogram*)malloc(sizeof(*histo));
|
|
|
|
if (histo == NULL) goto Error1;
|
|
|
|
// Evaluate lz77 coding
|
|
|
|
VP8LHistogramCreate(histo, &refs_lz77, cache_bits);
|
|
|
|
bit_cost_lz77 = VP8LHistogramEstimateBits(histo);
|
|
|
|
// Evaluate RLE coding
|
|
|
|
VP8LHistogramCreate(histo, &refs_rle, cache_bits);
|
|
|
|
bit_cost_rle = VP8LHistogramEstimateBits(histo);
|
|
|
|
// Decide if LZ77 is useful.
|
|
|
|
lz77_is_useful = (bit_cost_lz77 < bit_cost_rle);
|
|
|
|
free(histo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Choose appropriate backward reference.
|
|
|
|
if (lz77_is_useful) {
|
2012-10-31 01:25:54 +01:00
|
|
|
// TraceBackwards is costly. Don't execute it at lower quality (q <= 10).
|
|
|
|
const int try_lz77_trace_backwards = (quality > 10);
|
2012-05-09 08:41:55 +02:00
|
|
|
*best = refs_lz77; // default guess: lz77 is better
|
|
|
|
VP8LClearBackwardRefs(&refs_rle);
|
|
|
|
if (try_lz77_trace_backwards) {
|
|
|
|
const int recursion_level = (num_pix < 320 * 200) ? 1 : 0;
|
|
|
|
VP8LBackwardRefs refs_trace;
|
|
|
|
if (!VP8LBackwardRefsAlloc(&refs_trace, num_pix)) {
|
|
|
|
goto End;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-10-05 18:55:06 +02:00
|
|
|
if (BackwardReferencesTraceBackwards(width, height, recursion_level, argb,
|
|
|
|
quality, cache_bits, &refs_trace)) {
|
2012-05-09 08:41:55 +02:00
|
|
|
VP8LClearBackwardRefs(&refs_lz77);
|
|
|
|
*best = refs_trace;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-09 08:41:55 +02:00
|
|
|
} else {
|
|
|
|
VP8LClearBackwardRefs(&refs_lz77);
|
|
|
|
*best = refs_rle;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-05-09 08:41:55 +02:00
|
|
|
|
2012-05-25 11:52:44 +02:00
|
|
|
if (use_2d_locality) BackwardReferences2DLocality(width, best);
|
|
|
|
|
2012-05-09 08:41:55 +02:00
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
End:
|
|
|
|
if (!ok) {
|
|
|
|
VP8LClearBackwardRefs(best);
|
|
|
|
}
|
|
|
|
return ok;
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns 1 on success.
|
2012-05-25 11:52:44 +02:00
|
|
|
static int ComputeCacheHistogram(const uint32_t* const argb,
|
|
|
|
int xsize, int ysize,
|
|
|
|
const VP8LBackwardRefs* const refs,
|
|
|
|
int cache_bits,
|
|
|
|
VP8LHistogram* const histo) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int pixel_index = 0;
|
|
|
|
int i;
|
|
|
|
uint32_t k;
|
|
|
|
VP8LColorCache hashers;
|
2012-08-01 09:32:12 +02:00
|
|
|
const int use_color_cache = (cache_bits > 0);
|
|
|
|
int cc_init = 0;
|
2012-05-25 11:52:44 +02:00
|
|
|
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) {
|
|
|
|
cc_init = VP8LColorCacheInit(&hashers, cache_bits);
|
|
|
|
if (!cc_init) return 0;
|
|
|
|
}
|
2012-05-25 11:52:44 +02:00
|
|
|
|
2012-04-30 14:18:50 +02:00
|
|
|
for (i = 0; i < refs->size; ++i) {
|
|
|
|
const PixOrCopy* const v = &refs->refs[i];
|
|
|
|
if (PixOrCopyIsLiteral(v)) {
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache &&
|
2012-04-03 16:24:25 +02:00
|
|
|
VP8LColorCacheContains(&hashers, argb[pixel_index])) {
|
2012-04-25 09:33:57 +02:00
|
|
|
// push pixel as a cache index
|
2012-04-03 16:24:25 +02:00
|
|
|
const int ix = VP8LColorCacheGetIndex(&hashers, argb[pixel_index]);
|
2012-04-30 14:18:50 +02:00
|
|
|
const PixOrCopy token = PixOrCopyCreateCacheIdx(ix);
|
|
|
|
VP8LHistogramAddSinglePixOrCopy(histo, &token);
|
2012-04-03 16:24:25 +02:00
|
|
|
} else {
|
2012-04-10 05:56:07 +02:00
|
|
|
VP8LHistogramAddSinglePixOrCopy(histo, v);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
2012-04-10 05:56:07 +02:00
|
|
|
VP8LHistogramAddSinglePixOrCopy(histo, v);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-08-01 09:32:12 +02:00
|
|
|
if (use_color_cache) {
|
|
|
|
for (k = 0; k < PixOrCopyLength(v); ++k) {
|
|
|
|
VP8LColorCacheInsert(&hashers, argb[pixel_index + k]);
|
|
|
|
}
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
2012-08-01 09:32:12 +02:00
|
|
|
pixel_index += PixOrCopyLength(v);
|
2012-04-03 16:24:25 +02:00
|
|
|
}
|
|
|
|
assert(pixel_index == xsize * ysize);
|
|
|
|
(void)xsize; // xsize is not used in non-debug compilations otherwise.
|
|
|
|
(void)ysize; // ysize is not used in non-debug compilations otherwise.
|
2012-08-01 09:32:12 +02:00
|
|
|
if (cc_init) VP8LColorCacheClear(&hashers);
|
2012-04-03 16:24:25 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-25 09:33:57 +02:00
|
|
|
// Returns how many bits are to be used for a color cache.
|
2012-05-25 11:52:44 +02:00
|
|
|
int VP8LCalculateEstimateForCacheSize(const uint32_t* const argb,
|
|
|
|
int xsize, int ysize,
|
|
|
|
int* const best_cache_bits) {
|
2012-04-03 16:24:25 +02:00
|
|
|
int ok = 0;
|
2012-04-25 09:33:57 +02:00
|
|
|
int cache_bits;
|
2012-04-03 16:24:25 +02:00
|
|
|
double lowest_entropy = 1e99;
|
2012-04-30 14:18:50 +02:00
|
|
|
VP8LBackwardRefs refs;
|
2012-04-25 09:33:57 +02:00
|
|
|
static const double kSmallPenaltyForLargeCache = 4.0;
|
2012-04-03 16:24:25 +02:00
|
|
|
static const int quality = 30;
|
2012-04-30 14:18:50 +02:00
|
|
|
if (!VP8LBackwardRefsAlloc(&refs, xsize * ysize) ||
|
2012-05-09 08:41:55 +02:00
|
|
|
!BackwardReferencesHashChain(xsize, ysize, argb, 0, quality, &refs)) {
|
2012-04-03 16:24:25 +02:00
|
|
|
goto Error;
|
|
|
|
}
|
2012-05-25 11:52:44 +02:00
|
|
|
for (cache_bits = 0; cache_bits <= MAX_COLOR_CACHE_BITS; ++cache_bits) {
|
2012-04-03 16:24:25 +02:00
|
|
|
double cur_entropy;
|
2012-04-10 05:56:07 +02:00
|
|
|
VP8LHistogram histo;
|
2012-04-25 09:33:57 +02:00
|
|
|
VP8LHistogramInit(&histo, cache_bits);
|
2012-04-30 14:18:50 +02:00
|
|
|
ComputeCacheHistogram(argb, xsize, ysize, &refs, cache_bits, &histo);
|
2012-04-10 05:56:07 +02:00
|
|
|
cur_entropy = VP8LHistogramEstimateBits(&histo) +
|
2012-04-25 09:33:57 +02:00
|
|
|
kSmallPenaltyForLargeCache * cache_bits;
|
|
|
|
if (cache_bits == 0 || cur_entropy < lowest_entropy) {
|
|
|
|
*best_cache_bits = cache_bits;
|
2012-04-03 16:24:25 +02:00
|
|
|
lowest_entropy = cur_entropy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ok = 1;
|
2012-04-30 14:18:50 +02:00
|
|
|
Error:
|
|
|
|
VP8LClearBackwardRefs(&refs);
|
2012-04-03 16:24:25 +02:00
|
|
|
return ok;
|
|
|
|
}
|