2012-04-03 14:24:25 +00:00
|
|
|
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
|
|
//
|
2013-06-06 23:05:58 -07:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2012-04-03 14:24:25 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Author: Jyrki Alakuijala (jyrki@google.com)
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2017-10-07 14:15:11 -07:00
|
|
|
#include "src/enc/backward_references_enc.h"
|
|
|
|
#include "src/enc/histogram_enc.h"
|
|
|
|
#include "src/dsp/lossless.h"
|
|
|
|
#include "src/dsp/lossless_common.h"
|
|
|
|
#include "src/dsp/dsp.h"
|
|
|
|
#include "src/utils/color_cache_utils.h"
|
|
|
|
#include "src/utils/utils.h"
|
2012-04-03 14:24:25 +00:00
|
|
|
|
2014-05-05 11:11:55 -07:00
|
|
|
#define MIN_BLOCK_SIZE 256 // minimum block size for backward references
|
|
|
|
|
2014-03-13 10:29:50 -07:00
|
|
|
#define MAX_ENTROPY (1e30f)
|
|
|
|
|
2012-05-25 02:52:44 -07:00
|
|
|
// 1M window (4M bytes) minus 120 special codes for short distances.
|
2016-06-08 19:19:08 +02:00
|
|
|
#define WINDOW_SIZE ((1 << WINDOW_SIZE_BITS) - 120)
|
2012-05-25 02:52:44 -07:00
|
|
|
|
2016-10-10 15:37:45 +02:00
|
|
|
// Minimum number of pixels for which it is cheaper to encode a
|
|
|
|
// distance + length instead of each pixel as a literal.
|
|
|
|
#define MIN_LENGTH 4
|
2012-05-25 02:52:44 -07:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2012-04-03 14:24:25 +00: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 02:52:44 -07:00
|
|
|
119, 116, 111, 106, 97, 88, 84, 74, 72, 75, 85, 89, 98, 107, 112, 117
|
2012-04-03 14:24:25 +00:00
|
|
|
};
|
|
|
|
|
2017-06-01 15:07:28 +02:00
|
|
|
extern int VP8LDistanceToPlaneCode(int xsize, int dist);
|
|
|
|
int VP8LDistanceToPlaneCode(int xsize, int dist) {
|
2012-04-25 07:33:57 +00:00
|
|
|
const int yoffset = dist / xsize;
|
|
|
|
const int xoffset = dist - yoffset * xsize;
|
2012-04-03 14:24:25 +00: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;
|
|
|
|
}
|
|
|
|
|
2016-01-07 17:23:48 +01:00
|
|
|
// Returns the exact index where array1 and array2 are different. For an index
|
|
|
|
// inferior or equal to best_len_match, the return value just has to be strictly
|
|
|
|
// inferior to best_len_match. The current behavior is to return 0 if this index
|
|
|
|
// is best_len_match, and the index itself otherwise.
|
2015-12-04 10:19:58 +01:00
|
|
|
// If no two elements are the same, it returns max_limit.
|
2012-04-25 07:33:57 +00:00
|
|
|
static WEBP_INLINE int FindMatchLength(const uint32_t* const array1,
|
|
|
|
const uint32_t* const array2,
|
2016-01-07 17:23:48 +01:00
|
|
|
int best_len_match, int max_limit) {
|
2014-10-17 09:20:02 -07:00
|
|
|
// Before 'expensive' linear match, check if the two arrays match at the
|
|
|
|
// current best length index.
|
|
|
|
if (array1[best_len_match] != array2[best_len_match]) return 0;
|
2015-12-04 10:19:58 +01:00
|
|
|
|
2016-01-07 17:23:48 +01:00
|
|
|
return VP8LVectorMismatch(array1, array2, max_limit);
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
|
|
|
|
2012-05-25 02:52:44 -07:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// VP8LBackwardRefs
|
|
|
|
|
2014-05-05 11:11:55 -07:00
|
|
|
struct PixOrCopyBlock {
|
|
|
|
PixOrCopyBlock* next_; // next block (or NULL)
|
|
|
|
PixOrCopy* start_; // data start
|
|
|
|
int size_; // currently used size
|
|
|
|
};
|
|
|
|
|
2017-06-01 15:07:28 +02:00
|
|
|
extern void VP8LClearBackwardRefs(VP8LBackwardRefs* const refs);
|
|
|
|
void VP8LClearBackwardRefs(VP8LBackwardRefs* const refs) {
|
2014-04-25 16:01:49 -07:00
|
|
|
assert(refs != NULL);
|
2014-05-05 11:11:55 -07:00
|
|
|
if (refs->tail_ != NULL) {
|
|
|
|
*refs->tail_ = refs->free_blocks_; // recycle all blocks at once
|
|
|
|
}
|
|
|
|
refs->free_blocks_ = refs->refs_;
|
|
|
|
refs->tail_ = &refs->refs_;
|
|
|
|
refs->last_block_ = NULL;
|
|
|
|
refs->refs_ = NULL;
|
2012-05-25 02:52:44 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 11:11:55 -07:00
|
|
|
void VP8LBackwardRefsClear(VP8LBackwardRefs* const refs) {
|
|
|
|
assert(refs != NULL);
|
2017-06-01 15:07:28 +02:00
|
|
|
VP8LClearBackwardRefs(refs);
|
2014-05-05 11:11:55 -07:00
|
|
|
while (refs->free_blocks_ != NULL) {
|
|
|
|
PixOrCopyBlock* const next = refs->free_blocks_->next_;
|
|
|
|
WebPSafeFree(refs->free_blocks_);
|
|
|
|
refs->free_blocks_ = next;
|
2012-05-25 02:52:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 11:11:55 -07:00
|
|
|
void VP8LBackwardRefsInit(VP8LBackwardRefs* const refs, int block_size) {
|
|
|
|
assert(refs != NULL);
|
|
|
|
memset(refs, 0, sizeof(*refs));
|
|
|
|
refs->tail_ = &refs->refs_;
|
|
|
|
refs->block_size_ =
|
|
|
|
(block_size < MIN_BLOCK_SIZE) ? MIN_BLOCK_SIZE : block_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
VP8LRefsCursor VP8LRefsCursorInit(const VP8LBackwardRefs* const refs) {
|
|
|
|
VP8LRefsCursor c;
|
|
|
|
c.cur_block_ = refs->refs_;
|
|
|
|
if (refs->refs_ != NULL) {
|
|
|
|
c.cur_pos = c.cur_block_->start_;
|
|
|
|
c.last_pos_ = c.cur_pos + c.cur_block_->size_;
|
|
|
|
} else {
|
|
|
|
c.cur_pos = NULL;
|
|
|
|
c.last_pos_ = NULL;
|
2014-04-25 16:01:49 -07:00
|
|
|
}
|
2014-05-05 11:11:55 -07:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VP8LRefsCursorNextBlock(VP8LRefsCursor* const c) {
|
|
|
|
PixOrCopyBlock* const b = c->cur_block_->next_;
|
|
|
|
c->cur_pos = (b == NULL) ? NULL : b->start_;
|
|
|
|
c->last_pos_ = (b == NULL) ? NULL : b->start_ + b->size_;
|
|
|
|
c->cur_block_ = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new block, either from the free list or allocated
|
|
|
|
static PixOrCopyBlock* BackwardRefsNewBlock(VP8LBackwardRefs* const refs) {
|
|
|
|
PixOrCopyBlock* b = refs->free_blocks_;
|
|
|
|
if (b == NULL) { // allocate new memory chunk
|
|
|
|
const size_t total_size =
|
|
|
|
sizeof(*b) + refs->block_size_ * sizeof(*b->start_);
|
|
|
|
b = (PixOrCopyBlock*)WebPSafeMalloc(1ULL, total_size);
|
|
|
|
if (b == NULL) {
|
|
|
|
refs->error_ |= 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
b->start_ = (PixOrCopy*)((uint8_t*)b + sizeof(*b)); // not always aligned
|
|
|
|
} else { // recycle from free-list
|
|
|
|
refs->free_blocks_ = b->next_;
|
|
|
|
}
|
|
|
|
*refs->tail_ = b;
|
|
|
|
refs->tail_ = &b->next_;
|
|
|
|
refs->last_block_ = b;
|
|
|
|
b->next_ = NULL;
|
|
|
|
b->size_ = 0;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2017-06-01 15:07:28 +02:00
|
|
|
extern void VP8LBackwardRefsCursorAdd(VP8LBackwardRefs* const refs,
|
|
|
|
const PixOrCopy v);
|
2017-06-19 17:12:29 +02:00
|
|
|
void VP8LBackwardRefsCursorAdd(VP8LBackwardRefs* const refs,
|
|
|
|
const PixOrCopy v) {
|
2014-05-05 11:11:55 -07:00
|
|
|
PixOrCopyBlock* b = refs->last_block_;
|
|
|
|
if (b == NULL || b->size_ == refs->block_size_) {
|
|
|
|
b = BackwardRefsNewBlock(refs);
|
|
|
|
if (b == NULL) return; // refs->error_ is set
|
2014-04-25 16:01:49 -07:00
|
|
|
}
|
2014-05-05 11:11:55 -07:00
|
|
|
b->start_[b->size_++] = v;
|
2014-04-25 16:01:49 -07:00
|
|
|
}
|
|
|
|
|
2012-05-25 02:52:44 -07:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Hash chains
|
2012-04-03 14:24:25 +00:00
|
|
|
|
2014-04-30 14:10:48 -07:00
|
|
|
int VP8LHashChainInit(VP8LHashChain* const p, int size) {
|
|
|
|
assert(p->size_ == 0);
|
2016-06-08 19:19:08 +02:00
|
|
|
assert(p->offset_length_ == NULL);
|
2014-04-30 14:10:48 -07:00
|
|
|
assert(size > 0);
|
2016-06-08 19:19:08 +02:00
|
|
|
p->offset_length_ =
|
|
|
|
(uint32_t*)WebPSafeMalloc(size, sizeof(*p->offset_length_));
|
|
|
|
if (p->offset_length_ == NULL) return 0;
|
2014-04-25 16:01:49 -07:00
|
|
|
p->size_ = size;
|
2016-05-24 18:00:48 +02:00
|
|
|
|
2014-04-30 14:10:48 -07:00
|
|
|
return 1;
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 14:10:48 -07:00
|
|
|
void VP8LHashChainClear(VP8LHashChain* const p) {
|
|
|
|
assert(p != NULL);
|
2016-06-08 19:19:08 +02:00
|
|
|
WebPSafeFree(p->offset_length_);
|
|
|
|
|
2014-04-30 14:10:48 -07:00
|
|
|
p->size_ = 0;
|
2016-06-08 19:19:08 +02:00
|
|
|
p->offset_length_ = NULL;
|
2014-04-30 14:10:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2016-08-16 15:02:43 -07:00
|
|
|
#define HASH_MULTIPLIER_HI (0xc6a4a793ULL)
|
|
|
|
#define HASH_MULTIPLIER_LO (0x5bd1e996ULL)
|
2015-04-15 17:44:52 +02:00
|
|
|
|
2015-04-16 00:55:25 -07:00
|
|
|
static WEBP_INLINE uint32_t GetPixPairHash64(const uint32_t* const argb) {
|
2015-04-15 17:44:52 +02:00
|
|
|
uint32_t key;
|
2016-08-16 15:02:43 -07:00
|
|
|
key = (argb[1] * HASH_MULTIPLIER_HI) & 0xffffffffu;
|
|
|
|
key += (argb[0] * HASH_MULTIPLIER_LO) & 0xffffffffu;
|
2015-04-15 17:44:52 +02:00
|
|
|
key = key >> (32 - HASH_BITS);
|
2014-04-30 14:10:48 -07:00
|
|
|
return key;
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 15:09:40 +00:00
|
|
|
// Returns the maximum number of hash chain lookups to do for a
|
2016-06-08 19:19:08 +02:00
|
|
|
// given compression quality. Return value in range [8, 86].
|
|
|
|
static int GetMaxItersForQuality(int quality) {
|
|
|
|
return 8 + (quality * quality) / 128;
|
2014-10-31 18:07:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int GetWindowSizeForHashChain(int quality, int xsize) {
|
|
|
|
const int max_window_size = (quality > 75) ? WINDOW_SIZE
|
|
|
|
: (quality > 50) ? (xsize << 8)
|
|
|
|
: (quality > 25) ? (xsize << 6)
|
2012-10-30 17:25:54 -07:00
|
|
|
: (xsize << 4);
|
|
|
|
assert(xsize > 0);
|
2014-10-31 18:07:11 -07:00
|
|
|
return (max_window_size > WINDOW_SIZE) ? WINDOW_SIZE : max_window_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE int MaxFindCopyLength(int len) {
|
2014-11-04 17:34:35 +01:00
|
|
|
return (len < MAX_LENGTH) ? len : MAX_LENGTH;
|
2012-10-30 17:25:54 -07:00
|
|
|
}
|
|
|
|
|
2016-06-08 19:19:08 +02:00
|
|
|
int VP8LHashChainFill(VP8LHashChain* const p, int quality,
|
2016-07-14 19:21:07 +02:00
|
|
|
const uint32_t* const argb, int xsize, int ysize,
|
|
|
|
int low_effort) {
|
2016-05-24 18:00:48 +02:00
|
|
|
const int size = xsize * ysize;
|
2016-06-08 19:19:08 +02:00
|
|
|
const int iter_max = GetMaxItersForQuality(quality);
|
|
|
|
const uint32_t window_size = GetWindowSizeForHashChain(quality, xsize);
|
2016-05-24 18:00:48 +02:00
|
|
|
int pos;
|
2016-08-25 09:17:02 +02:00
|
|
|
int argb_comp;
|
2016-06-08 19:19:08 +02:00
|
|
|
uint32_t base_position;
|
2016-05-24 18:00:48 +02:00
|
|
|
int32_t* hash_to_first_index;
|
2016-06-08 19:19:08 +02:00
|
|
|
// Temporarily use the p->offset_length_ as a hash chain.
|
|
|
|
int32_t* chain = (int32_t*)p->offset_length_;
|
2016-08-25 09:17:02 +02:00
|
|
|
assert(size > 0);
|
2016-05-24 18:00:48 +02:00
|
|
|
assert(p->size_ != 0);
|
2016-06-08 19:19:08 +02:00
|
|
|
assert(p->offset_length_ != NULL);
|
2016-05-24 18:00:48 +02:00
|
|
|
|
2016-08-25 09:17:02 +02:00
|
|
|
if (size <= 2) {
|
|
|
|
p->offset_length_[0] = p->offset_length_[size - 1] = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-24 18:00:48 +02:00
|
|
|
hash_to_first_index =
|
|
|
|
(int32_t*)WebPSafeMalloc(HASH_SIZE, sizeof(*hash_to_first_index));
|
|
|
|
if (hash_to_first_index == NULL) return 0;
|
|
|
|
|
|
|
|
// Set the int32_t array to -1.
|
|
|
|
memset(hash_to_first_index, 0xff, HASH_SIZE * sizeof(*hash_to_first_index));
|
|
|
|
// Fill the chain linking pixels with the same hash.
|
2016-08-25 09:17:02 +02:00
|
|
|
argb_comp = (argb[0] == argb[1]);
|
|
|
|
for (pos = 0; pos < size - 2;) {
|
|
|
|
uint32_t hash_code;
|
|
|
|
const int argb_comp_next = (argb[pos + 1] == argb[pos + 2]);
|
|
|
|
if (argb_comp && argb_comp_next) {
|
|
|
|
// Consecutive pixels with the same color will share the same hash.
|
|
|
|
// We therefore use a different hash: the color and its repetition
|
|
|
|
// length.
|
|
|
|
uint32_t tmp[2];
|
|
|
|
uint32_t len = 1;
|
|
|
|
tmp[0] = argb[pos];
|
|
|
|
// Figure out how far the pixels are the same.
|
|
|
|
// The last pixel has a different 64 bit hash, as its next pixel does
|
|
|
|
// not have the same color, so we just need to get to the last pixel equal
|
|
|
|
// to its follower.
|
|
|
|
while (pos + (int)len + 2 < size && argb[pos + len + 2] == argb[pos]) {
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
if (len > MAX_LENGTH) {
|
|
|
|
// Skip the pixels that match for distance=1 and length>MAX_LENGTH
|
|
|
|
// because they are linked to their predecessor and we automatically
|
|
|
|
// check that in the main for loop below. Skipping means setting no
|
|
|
|
// predecessor in the chain, hence -1.
|
|
|
|
memset(chain + pos, 0xff, (len - MAX_LENGTH) * sizeof(*chain));
|
|
|
|
pos += len - MAX_LENGTH;
|
|
|
|
len = MAX_LENGTH;
|
|
|
|
}
|
|
|
|
// Process the rest of the hash chain.
|
|
|
|
while (len) {
|
|
|
|
tmp[1] = len--;
|
|
|
|
hash_code = GetPixPairHash64(tmp);
|
|
|
|
chain[pos] = hash_to_first_index[hash_code];
|
|
|
|
hash_to_first_index[hash_code] = pos++;
|
|
|
|
}
|
|
|
|
argb_comp = 0;
|
|
|
|
} else {
|
|
|
|
// Just move one pixel forward.
|
|
|
|
hash_code = GetPixPairHash64(argb + pos);
|
|
|
|
chain[pos] = hash_to_first_index[hash_code];
|
|
|
|
hash_to_first_index[hash_code] = pos++;
|
|
|
|
argb_comp = argb_comp_next;
|
|
|
|
}
|
2016-05-24 18:00:48 +02:00
|
|
|
}
|
2016-08-25 09:17:02 +02:00
|
|
|
// Process the penultimate pixel.
|
|
|
|
chain[pos] = hash_to_first_index[GetPixPairHash64(argb + pos)];
|
|
|
|
|
2016-05-24 18:00:48 +02:00
|
|
|
WebPSafeFree(hash_to_first_index);
|
|
|
|
|
2016-06-08 19:19:08 +02:00
|
|
|
// Find the best match interval at each pixel, defined by an offset to the
|
|
|
|
// pixel and a length. The right-most pixel cannot match anything to the right
|
|
|
|
// (hence a best length of 0) and the left-most pixel nothing to the left
|
|
|
|
// (hence an offset of 0).
|
2016-08-25 09:17:02 +02:00
|
|
|
assert(size > 2);
|
2016-06-08 19:19:08 +02:00
|
|
|
p->offset_length_[0] = p->offset_length_[size - 1] = 0;
|
2016-08-25 09:17:02 +02:00
|
|
|
for (base_position = size - 2; base_position > 0;) {
|
2016-06-08 19:19:08 +02:00
|
|
|
const int max_len = MaxFindCopyLength(size - 1 - base_position);
|
|
|
|
const uint32_t* const argb_start = argb + base_position;
|
|
|
|
int iter = iter_max;
|
|
|
|
int best_length = 0;
|
|
|
|
uint32_t best_distance = 0;
|
2016-09-22 14:03:25 +02:00
|
|
|
uint32_t best_argb;
|
2016-06-08 19:19:08 +02:00
|
|
|
const int min_pos =
|
|
|
|
(base_position > window_size) ? base_position - window_size : 0;
|
|
|
|
const int length_max = (max_len < 256) ? max_len : 256;
|
|
|
|
uint32_t max_base_position;
|
|
|
|
|
2016-07-14 19:21:07 +02:00
|
|
|
pos = chain[base_position];
|
|
|
|
if (!low_effort) {
|
2016-08-25 09:17:02 +02:00
|
|
|
int curr_length;
|
2016-07-14 19:21:07 +02:00
|
|
|
// Heuristic: use the comparison with the above line as an initialization.
|
|
|
|
if (base_position >= (uint32_t)xsize) {
|
2016-08-25 09:17:02 +02:00
|
|
|
curr_length = FindMatchLength(argb_start - xsize, argb_start,
|
|
|
|
best_length, max_len);
|
2016-07-14 19:21:07 +02:00
|
|
|
if (curr_length > best_length) {
|
|
|
|
best_length = curr_length;
|
|
|
|
best_distance = xsize;
|
|
|
|
}
|
2016-08-25 09:17:02 +02:00
|
|
|
--iter;
|
|
|
|
}
|
|
|
|
// Heuristic: compare to the previous pixel.
|
|
|
|
curr_length =
|
|
|
|
FindMatchLength(argb_start - 1, argb_start, best_length, max_len);
|
|
|
|
if (curr_length > best_length) {
|
|
|
|
best_length = curr_length;
|
|
|
|
best_distance = 1;
|
2016-07-14 19:21:07 +02:00
|
|
|
}
|
|
|
|
--iter;
|
|
|
|
// Skip the for loop if we already have the maximum.
|
|
|
|
if (best_length == MAX_LENGTH) pos = min_pos - 1;
|
|
|
|
}
|
2016-09-22 14:03:25 +02:00
|
|
|
best_argb = argb_start[best_length];
|
2016-07-14 19:21:07 +02:00
|
|
|
|
2016-09-22 14:03:25 +02:00
|
|
|
for (; pos >= min_pos && --iter; pos = chain[pos]) {
|
2016-06-08 19:19:08 +02:00
|
|
|
int curr_length;
|
|
|
|
assert(base_position > (uint32_t)pos);
|
|
|
|
|
2016-09-22 14:03:25 +02:00
|
|
|
if (argb[pos + best_length] != best_argb) continue;
|
|
|
|
|
|
|
|
curr_length = VP8LVectorMismatch(argb + pos, argb_start, max_len);
|
2016-06-08 19:19:08 +02:00
|
|
|
if (best_length < curr_length) {
|
|
|
|
best_length = curr_length;
|
|
|
|
best_distance = base_position - pos;
|
2016-09-22 14:03:25 +02:00
|
|
|
best_argb = argb_start[best_length];
|
|
|
|
// Stop if we have reached a good enough length.
|
|
|
|
if (best_length >= length_max) break;
|
2016-06-08 19:19:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// We have the best match but in case the two intervals continue matching
|
|
|
|
// to the left, we have the best matches for the left-extended pixels.
|
|
|
|
max_base_position = base_position;
|
|
|
|
while (1) {
|
|
|
|
assert(best_length <= MAX_LENGTH);
|
|
|
|
assert(best_distance <= WINDOW_SIZE);
|
|
|
|
p->offset_length_[base_position] =
|
|
|
|
(best_distance << MAX_LENGTH_BITS) | (uint32_t)best_length;
|
|
|
|
--base_position;
|
|
|
|
// Stop if we don't have a match or if we are out of bounds.
|
|
|
|
if (best_distance == 0 || base_position == 0) break;
|
|
|
|
// Stop if we cannot extend the matching intervals to the left.
|
|
|
|
if (base_position < best_distance ||
|
|
|
|
argb[base_position - best_distance] != argb[base_position]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Stop if we are matching at its limit because there could be a closer
|
|
|
|
// matching interval with the same maximum length. Then again, if the
|
|
|
|
// matching interval is as close as possible (best_distance == 1), we will
|
|
|
|
// never find anything better so let's continue.
|
|
|
|
if (best_length == MAX_LENGTH && best_distance != 1 &&
|
|
|
|
base_position + MAX_LENGTH < max_base_position) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (best_length < MAX_LENGTH) {
|
|
|
|
++best_length;
|
|
|
|
max_base_position = base_position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 18:00:48 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-26 14:24:59 +00:00
|
|
|
static WEBP_INLINE void AddSingleLiteral(uint32_t pixel, int use_color_cache,
|
|
|
|
VP8LColorCache* const hashers,
|
|
|
|
VP8LBackwardRefs* const refs) {
|
2014-11-04 08:10:31 -08:00
|
|
|
PixOrCopy v;
|
2015-08-17 16:54:27 +00:00
|
|
|
if (use_color_cache) {
|
|
|
|
const uint32_t key = VP8LColorCacheGetIndex(hashers, pixel);
|
|
|
|
if (VP8LColorCacheLookup(hashers, key) == pixel) {
|
|
|
|
v = PixOrCopyCreateCacheIdx(key);
|
|
|
|
} else {
|
|
|
|
v = PixOrCopyCreateLiteral(pixel);
|
|
|
|
VP8LColorCacheSet(hashers, key, pixel);
|
|
|
|
}
|
2014-11-04 08:10:31 -08:00
|
|
|
} else {
|
|
|
|
v = PixOrCopyCreateLiteral(pixel);
|
|
|
|
}
|
2017-06-01 15:07:28 +02:00
|
|
|
VP8LBackwardRefsCursorAdd(refs, v);
|
2014-11-04 08:10:31 -08:00
|
|
|
}
|
|
|
|
|
2014-05-05 11:11:55 -07:00
|
|
|
static int BackwardReferencesRle(int xsize, int ysize,
|
|
|
|
const uint32_t* const argb,
|
2014-11-04 08:10:31 -08:00
|
|
|
int cache_bits, VP8LBackwardRefs* const refs) {
|
2012-04-03 14:24:25 +00:00
|
|
|
const int pix_count = xsize * ysize;
|
2015-07-02 10:54:01 +00:00
|
|
|
int i, k;
|
2014-11-04 08:10:31 -08:00
|
|
|
const int use_color_cache = (cache_bits > 0);
|
|
|
|
VP8LColorCache hashers;
|
|
|
|
|
2015-07-02 10:54:01 +00:00
|
|
|
if (use_color_cache && !VP8LColorCacheInit(&hashers, cache_bits)) {
|
|
|
|
return 0;
|
2014-11-04 08:10:31 -08:00
|
|
|
}
|
2017-06-01 15:07:28 +02:00
|
|
|
VP8LClearBackwardRefs(refs);
|
2014-11-04 08:10:31 -08:00
|
|
|
// Add first pixel as literal.
|
|
|
|
AddSingleLiteral(argb[0], use_color_cache, &hashers, refs);
|
2015-07-02 10:54:01 +00:00
|
|
|
i = 1;
|
|
|
|
while (i < pix_count) {
|
|
|
|
const int max_len = MaxFindCopyLength(pix_count - i);
|
|
|
|
const int rle_len = FindMatchLength(argb + i, argb + i - 1, 0, max_len);
|
|
|
|
const int prev_row_len = (i < xsize) ? 0 :
|
|
|
|
FindMatchLength(argb + i, argb + i - xsize, 0, max_len);
|
2016-10-10 15:37:45 +02:00
|
|
|
if (rle_len >= prev_row_len && rle_len >= MIN_LENGTH) {
|
2017-06-01 15:07:28 +02:00
|
|
|
VP8LBackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(1, rle_len));
|
2015-07-02 10:54:01 +00:00
|
|
|
// We don't need to update the color cache here since it is always the
|
|
|
|
// same pixel being copied, and that does not change the color cache
|
|
|
|
// state.
|
|
|
|
i += rle_len;
|
2016-10-10 15:37:45 +02:00
|
|
|
} else if (prev_row_len >= MIN_LENGTH) {
|
2017-06-01 15:07:28 +02:00
|
|
|
VP8LBackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(xsize, prev_row_len));
|
2015-07-02 10:54:01 +00:00
|
|
|
if (use_color_cache) {
|
|
|
|
for (k = 0; k < prev_row_len; ++k) {
|
|
|
|
VP8LColorCacheInsert(&hashers, argb[i + k]);
|
2015-06-11 18:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-02 10:54:01 +00:00
|
|
|
i += prev_row_len;
|
|
|
|
} else {
|
2014-11-04 08:10:31 -08:00
|
|
|
AddSingleLiteral(argb[i], use_color_cache, &hashers, refs);
|
2015-07-02 10:54:01 +00:00
|
|
|
i++;
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-02 10:54:01 +00:00
|
|
|
if (use_color_cache) VP8LColorCacheClear(&hashers);
|
2014-05-05 11:11:55 -07:00
|
|
|
return !refs->error_;
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 08:10:31 -08:00
|
|
|
static int BackwardReferencesLz77(int xsize, int ysize,
|
2014-12-22 17:00:30 +00:00
|
|
|
const uint32_t* const argb, int cache_bits,
|
2016-05-24 18:00:48 +02:00
|
|
|
const VP8LHashChain* const hash_chain,
|
2014-11-04 17:34:35 +01:00
|
|
|
VP8LBackwardRefs* const refs) {
|
2012-04-03 14:24:25 +00:00
|
|
|
int i;
|
2016-06-08 19:19:08 +02:00
|
|
|
int i_last_check = -1;
|
2012-04-03 14:24:25 +00:00
|
|
|
int ok = 0;
|
2012-05-09 12:11:55 +05:30
|
|
|
int cc_init = 0;
|
|
|
|
const int use_color_cache = (cache_bits > 0);
|
2012-04-24 09:55:19 +00:00
|
|
|
const int pix_count = xsize * ysize;
|
2012-04-03 14:24:25 +00:00
|
|
|
VP8LColorCache hashers;
|
2012-05-09 12:11:55 +05:30
|
|
|
|
2012-08-01 00:32:12 -07:00
|
|
|
if (use_color_cache) {
|
|
|
|
cc_init = VP8LColorCacheInit(&hashers, cache_bits);
|
|
|
|
if (!cc_init) goto Error;
|
|
|
|
}
|
2017-06-01 15:07:28 +02:00
|
|
|
VP8LClearBackwardRefs(refs);
|
2016-06-08 19:19:08 +02:00
|
|
|
for (i = 0; i < pix_count;) {
|
2012-04-03 14:24:25 +00:00
|
|
|
// Alternative#1: Code the pixels starting at 'i' using backward reference.
|
|
|
|
int offset = 0;
|
|
|
|
int len = 0;
|
2016-06-08 19:19:08 +02:00
|
|
|
int j;
|
2017-06-01 15:07:28 +02:00
|
|
|
VP8LHashChainFindCopy(hash_chain, i, &offset, &len);
|
2016-10-10 15:37:45 +02:00
|
|
|
if (len >= MIN_LENGTH) {
|
2016-06-08 19:19:08 +02:00
|
|
|
const int len_ini = len;
|
|
|
|
int max_reach = 0;
|
2017-06-07 19:06:28 +02:00
|
|
|
const int j_max =
|
|
|
|
(i + len_ini >= pix_count) ? pix_count - 1 : i + len_ini;
|
2016-06-08 19:19:08 +02:00
|
|
|
// Only start from what we have not checked already.
|
|
|
|
i_last_check = (i > i_last_check) ? i : i_last_check;
|
|
|
|
// We know the best match for the current pixel but we try to find the
|
|
|
|
// best matches for the current pixel AND the next one combined.
|
|
|
|
// The naive method would use the intervals:
|
|
|
|
// [i,i+len) + [i+len, length of best match at i+len)
|
|
|
|
// while we check if we can use:
|
|
|
|
// [i,j) (where j<=i+len) + [j, length of best match at j)
|
2017-06-07 19:06:28 +02:00
|
|
|
for (j = i_last_check + 1; j <= j_max; ++j) {
|
2017-06-01 15:07:28 +02:00
|
|
|
const int len_j = VP8LHashChainFindLength(hash_chain, j);
|
2016-06-08 19:19:08 +02:00
|
|
|
const int reach =
|
2016-10-10 15:37:45 +02:00
|
|
|
j + (len_j >= MIN_LENGTH ? len_j : 1); // 1 for single literal.
|
2016-06-08 19:19:08 +02:00
|
|
|
if (reach > max_reach) {
|
|
|
|
len = j - i;
|
|
|
|
max_reach = reach;
|
2017-08-04 13:32:12 +02:00
|
|
|
if (max_reach >= pix_count) break;
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-24 16:11:25 +02:00
|
|
|
len = 1;
|
2016-06-08 19:19:08 +02:00
|
|
|
}
|
|
|
|
// Go with literal or backward reference.
|
|
|
|
assert(len > 0);
|
|
|
|
if (len == 1) {
|
2014-10-31 18:07:11 -07:00
|
|
|
AddSingleLiteral(argb[i], use_color_cache, &hashers, refs);
|
2016-06-08 19:19:08 +02:00
|
|
|
} else {
|
2017-06-01 15:07:28 +02:00
|
|
|
VP8LBackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(offset, len));
|
2016-06-08 19:19:08 +02:00
|
|
|
if (use_color_cache) {
|
|
|
|
for (j = i; j < i + len; ++j) VP8LColorCacheInsert(&hashers, argb[j]);
|
2015-06-23 12:11:51 +00:00
|
|
|
}
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
2016-06-08 19:19:08 +02:00
|
|
|
i += len;
|
2014-10-31 18:07:11 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 11:11:55 -07:00
|
|
|
ok = !refs->error_;
|
2014-11-04 17:34:35 +01:00
|
|
|
Error:
|
2012-05-09 12:11:55 +05:30
|
|
|
if (cc_init) VP8LColorCacheClear(&hashers);
|
2012-04-03 14:24:25 +00:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:06:28 +02:00
|
|
|
// Compute an LZ77 by forcing matches to happen within a given distance cost.
|
|
|
|
// We therefore limit the algorithm to the lowest 32 values in the PlaneCode
|
|
|
|
// definition.
|
|
|
|
#define WINDOW_OFFSETS_SIZE_MAX 32
|
|
|
|
static int BackwardReferencesLz77Box(int xsize, int ysize,
|
|
|
|
const uint32_t* const argb, int cache_bits,
|
|
|
|
const VP8LHashChain* const hash_chain_best,
|
|
|
|
VP8LHashChain* hash_chain,
|
|
|
|
VP8LBackwardRefs* const refs) {
|
|
|
|
int i;
|
|
|
|
const int pix_count = xsize * ysize;
|
|
|
|
uint16_t* counts;
|
|
|
|
int window_offsets[WINDOW_OFFSETS_SIZE_MAX] = {0};
|
2017-08-04 13:35:45 +02:00
|
|
|
int window_offsets_new[WINDOW_OFFSETS_SIZE_MAX] = {0};
|
2017-06-07 19:06:28 +02:00
|
|
|
int window_offsets_size = 0;
|
2017-08-04 13:35:45 +02:00
|
|
|
int window_offsets_new_size = 0;
|
2017-06-07 19:06:28 +02:00
|
|
|
uint16_t* const counts_ini =
|
|
|
|
(uint16_t*)WebPSafeMalloc(xsize * ysize, sizeof(*counts_ini));
|
2017-08-04 13:35:45 +02:00
|
|
|
int best_offset_prev = -1, best_length_prev = -1;
|
2017-06-07 19:06:28 +02:00
|
|
|
if (counts_ini == NULL) return 0;
|
|
|
|
|
|
|
|
// counts[i] counts how many times a pixel is repeated starting at position i.
|
|
|
|
i = pix_count - 2;
|
|
|
|
counts = counts_ini + i;
|
|
|
|
counts[1] = 1;
|
|
|
|
for (; i >= 0; --i, --counts) {
|
|
|
|
if (argb[i] == argb[i + 1]) {
|
|
|
|
// Max out the counts to MAX_LENGTH.
|
|
|
|
counts[0] = counts[1] + (counts[1] != MAX_LENGTH);
|
|
|
|
} else {
|
|
|
|
counts[0] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out the window offsets around a pixel. They are stored in a
|
|
|
|
// spiraling order around the pixel as defined by VP8LDistanceToPlaneCode.
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
for (y = 0; y <= 6; ++y) {
|
|
|
|
for (x = -6; x <= 6; ++x) {
|
|
|
|
const int offset = y * xsize + x;
|
|
|
|
int plane_code;
|
|
|
|
// Ignore offsets that bring us after the pixel.
|
|
|
|
if (offset <= 0) continue;
|
|
|
|
plane_code = VP8LDistanceToPlaneCode(xsize, offset) - 1;
|
|
|
|
if (plane_code >= WINDOW_OFFSETS_SIZE_MAX) continue;
|
|
|
|
window_offsets[plane_code] = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// For narrow images, not all plane codes are reached, so remove those.
|
|
|
|
for (i = 0; i < WINDOW_OFFSETS_SIZE_MAX; ++i) {
|
|
|
|
if (window_offsets[i] == 0) continue;
|
|
|
|
window_offsets[window_offsets_size++] = window_offsets[i];
|
|
|
|
}
|
2017-08-04 13:35:45 +02:00
|
|
|
// Given a pixel P, find the offsets that reach pixels unreachable from P-1
|
|
|
|
// with any of the offsets in window_offsets[].
|
|
|
|
for (i = 0; i < window_offsets_size; ++i) {
|
|
|
|
int j;
|
|
|
|
int is_reachable = 0;
|
|
|
|
for (j = 0; j < window_offsets_size && !is_reachable; ++j) {
|
|
|
|
is_reachable |= (window_offsets[i] == window_offsets[j] + 1);
|
|
|
|
}
|
|
|
|
if (!is_reachable) {
|
|
|
|
window_offsets_new[window_offsets_new_size] = window_offsets[i];
|
|
|
|
++window_offsets_new_size;
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 19:06:28 +02:00
|
|
|
}
|
|
|
|
|
2017-08-04 13:35:45 +02:00
|
|
|
hash_chain->offset_length_[0] = 0;
|
|
|
|
for (i = 1; i < pix_count; ++i) {
|
2017-06-07 19:06:28 +02:00
|
|
|
int ind;
|
|
|
|
int best_length = VP8LHashChainFindLength(hash_chain_best, i);
|
|
|
|
int best_offset;
|
|
|
|
int do_compute = 1;
|
|
|
|
|
|
|
|
if (best_length >= MAX_LENGTH) {
|
|
|
|
// Do not recompute the best match if we already have a maximal one in the
|
|
|
|
// window.
|
|
|
|
best_offset = VP8LHashChainFindOffset(hash_chain_best, i);
|
|
|
|
for (ind = 0; ind < window_offsets_size; ++ind) {
|
|
|
|
if (best_offset == window_offsets[ind]) {
|
|
|
|
do_compute = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (do_compute) {
|
2017-08-04 13:35:45 +02:00
|
|
|
// Figure out if we should use the offset/length from the previous pixel
|
|
|
|
// as an initial guess and therefore only inspect the offsets in
|
|
|
|
// window_offsets_new[].
|
2017-08-07 23:02:15 +02:00
|
|
|
const int use_prev =
|
|
|
|
(best_length_prev > 1) && (best_length_prev < MAX_LENGTH);
|
|
|
|
const int num_ind =
|
|
|
|
use_prev ? window_offsets_new_size : window_offsets_size;
|
2017-08-04 13:35:45 +02:00
|
|
|
best_length = use_prev ? best_length_prev - 1 : 0;
|
|
|
|
best_offset = use_prev ? best_offset_prev : 0;
|
2017-06-07 19:06:28 +02:00
|
|
|
// Find the longest match in a window around the pixel.
|
2017-08-04 13:35:45 +02:00
|
|
|
for (ind = 0; ind < num_ind; ++ind) {
|
2017-06-07 19:06:28 +02:00
|
|
|
int curr_length = 0;
|
|
|
|
int j = i;
|
2017-08-04 13:35:45 +02:00
|
|
|
int j_offset =
|
|
|
|
use_prev ? i - window_offsets_new[ind] : i - window_offsets[ind];
|
2017-06-07 19:06:28 +02:00
|
|
|
if (j_offset < 0 || argb[j_offset] != argb[i]) continue;
|
|
|
|
// The longest match is the sum of how many times each pixel is
|
|
|
|
// repeated.
|
|
|
|
do {
|
|
|
|
const int counts_j_offset = counts_ini[j_offset];
|
|
|
|
const int counts_j = counts_ini[j];
|
|
|
|
if (counts_j_offset != counts_j) {
|
|
|
|
curr_length +=
|
|
|
|
(counts_j_offset < counts_j) ? counts_j_offset : counts_j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// The same color is repeated counts_pos times at j_offset and j.
|
|
|
|
curr_length += counts_j_offset;
|
|
|
|
j_offset += counts_j_offset;
|
|
|
|
j += counts_j_offset;
|
|
|
|
} while (curr_length <= MAX_LENGTH && j < pix_count &&
|
|
|
|
argb[j_offset] == argb[j]);
|
|
|
|
if (best_length < curr_length) {
|
2017-08-04 13:35:45 +02:00
|
|
|
best_offset =
|
|
|
|
use_prev ? window_offsets_new[ind] : window_offsets[ind];
|
|
|
|
if (curr_length >= MAX_LENGTH) {
|
2017-06-07 19:06:28 +02:00
|
|
|
best_length = MAX_LENGTH;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
best_length = curr_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(i + best_length <= pix_count);
|
|
|
|
assert(best_length <= MAX_LENGTH);
|
|
|
|
if (best_length <= MIN_LENGTH) {
|
|
|
|
hash_chain->offset_length_[i] = 0;
|
2017-08-04 13:35:45 +02:00
|
|
|
best_offset_prev = 0;
|
|
|
|
best_length_prev = 0;
|
2017-06-07 19:06:28 +02:00
|
|
|
} else {
|
|
|
|
hash_chain->offset_length_[i] =
|
|
|
|
(best_offset << MAX_LENGTH_BITS) | (uint32_t)best_length;
|
2017-08-04 13:35:45 +02:00
|
|
|
best_offset_prev = best_offset;
|
|
|
|
best_length_prev = best_length;
|
2017-06-07 19:06:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hash_chain->offset_length_[0] = 0;
|
|
|
|
WebPSafeFree(counts_ini);
|
|
|
|
|
|
|
|
return BackwardReferencesLz77(xsize, ysize, argb, cache_bits, hash_chain,
|
|
|
|
refs);
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:18:50 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2012-05-09 12:11:55 +05:30
|
|
|
static void BackwardReferences2DLocality(int xsize,
|
2014-05-05 11:11:55 -07:00
|
|
|
const VP8LBackwardRefs* const refs) {
|
|
|
|
VP8LRefsCursor c = VP8LRefsCursorInit(refs);
|
|
|
|
while (VP8LRefsCursorOk(&c)) {
|
|
|
|
if (PixOrCopyIsCopy(c.cur_pos)) {
|
|
|
|
const int dist = c.cur_pos->argb_or_distance;
|
2017-06-01 15:07:28 +02:00
|
|
|
const int transformed_dist = VP8LDistanceToPlaneCode(xsize, dist);
|
2014-05-05 11:11:55 -07:00
|
|
|
c.cur_pos->argb_or_distance = transformed_dist;
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
2014-05-05 11:11:55 -07:00
|
|
|
VP8LRefsCursorNext(&c);
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 17:00:37 +01:00
|
|
|
// Evaluate optimal cache bits for the local color cache.
|
|
|
|
// The input *best_cache_bits sets the maximum cache bits to use (passing 0
|
|
|
|
// implies disabling the local color cache). The local color cache is also
|
|
|
|
// disabled for the lower (<= 25) quality.
|
|
|
|
// Returns 0 in case of memory error.
|
|
|
|
static int CalculateBestCacheSize(const uint32_t* argb, int quality,
|
|
|
|
const VP8LBackwardRefs* const refs,
|
|
|
|
int* const best_cache_bits) {
|
|
|
|
int i;
|
|
|
|
const int cache_bits_max = (quality <= 25) ? 0 : *best_cache_bits;
|
|
|
|
double entropy_min = MAX_ENTROPY;
|
2017-01-10 18:24:31 +01:00
|
|
|
int cc_init[MAX_COLOR_CACHE_BITS + 1] = { 0 };
|
|
|
|
VP8LColorCache hashers[MAX_COLOR_CACHE_BITS + 1];
|
2014-11-13 13:17:24 -08:00
|
|
|
VP8LRefsCursor c = VP8LRefsCursorInit(refs);
|
2017-01-10 18:24:31 +01:00
|
|
|
VP8LHistogram* histos[MAX_COLOR_CACHE_BITS + 1] = { NULL };
|
2017-01-10 18:53:19 +01:00
|
|
|
int ok = 0;
|
2014-11-13 13:17:24 -08:00
|
|
|
|
2017-02-23 17:00:37 +01:00
|
|
|
assert(cache_bits_max >= 0 && cache_bits_max <= MAX_COLOR_CACHE_BITS);
|
|
|
|
|
|
|
|
if (cache_bits_max == 0) {
|
|
|
|
*best_cache_bits = 0;
|
|
|
|
// Local color cache is disabled.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate data.
|
2017-01-10 18:24:31 +01:00
|
|
|
for (i = 0; i <= cache_bits_max; ++i) {
|
|
|
|
histos[i] = VP8LAllocateHistogram(i);
|
|
|
|
if (histos[i] == NULL) goto Error;
|
2018-10-30 20:33:20 +01:00
|
|
|
VP8LHistogramInit(histos[i], i, /*init_arrays=*/ 1);
|
2017-01-10 18:24:31 +01:00
|
|
|
if (i == 0) continue;
|
|
|
|
cc_init[i] = VP8LColorCacheInit(&hashers[i], i);
|
|
|
|
if (!cc_init[i]) goto Error;
|
2014-11-13 13:17:24 -08:00
|
|
|
}
|
2017-01-10 18:24:31 +01:00
|
|
|
|
2017-02-23 17:00:37 +01:00
|
|
|
// Find the cache_bits giving the lowest entropy. The search is done in a
|
|
|
|
// brute-force way as the function (entropy w.r.t cache_bits) can be
|
|
|
|
// anything in practice.
|
2017-01-10 18:24:31 +01:00
|
|
|
while (VP8LRefsCursorOk(&c)) {
|
2017-02-23 17:00:37 +01:00
|
|
|
const PixOrCopy* const v = c.cur_pos;
|
|
|
|
if (PixOrCopyIsLiteral(v)) {
|
|
|
|
const uint32_t pix = *argb++;
|
|
|
|
const uint32_t a = (pix >> 24) & 0xff;
|
|
|
|
const uint32_t r = (pix >> 16) & 0xff;
|
|
|
|
const uint32_t g = (pix >> 8) & 0xff;
|
|
|
|
const uint32_t b = (pix >> 0) & 0xff;
|
|
|
|
// The keys of the caches can be derived from the longest one.
|
2017-03-16 16:15:40 +01:00
|
|
|
int key = VP8LHashPix(pix, 32 - cache_bits_max);
|
2017-02-23 17:00:37 +01:00
|
|
|
// Do not use the color cache for cache_bits = 0.
|
|
|
|
++histos[0]->blue_[b];
|
|
|
|
++histos[0]->literal_[g];
|
|
|
|
++histos[0]->red_[r];
|
|
|
|
++histos[0]->alpha_[a];
|
|
|
|
// Deal with cache_bits > 0.
|
|
|
|
for (i = cache_bits_max; i >= 1; --i, key >>= 1) {
|
|
|
|
if (VP8LColorCacheLookup(&hashers[i], key) == pix) {
|
|
|
|
++histos[i]->literal_[NUM_LITERAL_CODES + NUM_LENGTH_CODES + key];
|
|
|
|
} else {
|
|
|
|
VP8LColorCacheSet(&hashers[i], key, pix);
|
|
|
|
++histos[i]->blue_[b];
|
|
|
|
++histos[i]->literal_[g];
|
|
|
|
++histos[i]->red_[r];
|
|
|
|
++histos[i]->alpha_[a];
|
2017-01-10 18:24:31 +01:00
|
|
|
}
|
2014-11-13 13:17:24 -08:00
|
|
|
}
|
2017-02-23 17:00:37 +01:00
|
|
|
} else {
|
|
|
|
// We should compute the contribution of the (distance,length)
|
|
|
|
// histograms but those are the same independently from the cache size.
|
|
|
|
// As those constant contributions are in the end added to the other
|
|
|
|
// histogram contributions, we can safely ignore them.
|
|
|
|
int len = PixOrCopyLength(v);
|
|
|
|
uint32_t argb_prev = *argb ^ 0xffffffffu;
|
|
|
|
// Update the color caches.
|
|
|
|
do {
|
|
|
|
if (*argb != argb_prev) {
|
|
|
|
// Efficiency: insert only if the color changes.
|
2017-03-16 16:15:40 +01:00
|
|
|
int key = VP8LHashPix(*argb, 32 - cache_bits_max);
|
2017-02-23 17:00:37 +01:00
|
|
|
for (i = cache_bits_max; i >= 1; --i, key >>= 1) {
|
|
|
|
hashers[i].colors_[key] = *argb;
|
|
|
|
}
|
|
|
|
argb_prev = *argb;
|
|
|
|
}
|
|
|
|
argb++;
|
|
|
|
} while (--len != 0);
|
2014-11-13 13:17:24 -08:00
|
|
|
}
|
2017-02-23 17:00:37 +01:00
|
|
|
VP8LRefsCursorNext(&c);
|
2014-11-13 13:17:24 -08:00
|
|
|
}
|
2017-02-23 17:00:37 +01:00
|
|
|
|
2017-01-10 18:24:31 +01:00
|
|
|
for (i = 0; i <= cache_bits_max; ++i) {
|
2017-02-23 17:00:37 +01:00
|
|
|
const double entropy = VP8LHistogramEstimateBits(histos[i]);
|
|
|
|
if (i == 0 || entropy < entropy_min) {
|
|
|
|
entropy_min = entropy;
|
|
|
|
*best_cache_bits = i;
|
|
|
|
}
|
2017-01-10 18:24:31 +01:00
|
|
|
}
|
2017-01-10 18:53:19 +01:00
|
|
|
ok = 1;
|
2017-01-10 18:24:31 +01:00
|
|
|
Error:
|
|
|
|
for (i = 0; i <= cache_bits_max; ++i) {
|
|
|
|
if (cc_init[i]) VP8LColorCacheClear(&hashers[i]);
|
|
|
|
VP8LFreeHistogram(histos[i]);
|
|
|
|
}
|
2017-01-10 18:53:19 +01:00
|
|
|
return ok;
|
2014-11-13 13:17:24 -08:00
|
|
|
}
|
|
|
|
|
2014-11-12 16:53:46 -08:00
|
|
|
// Update (in-place) backward references for specified cache_bits.
|
2014-11-11 11:24:02 -08:00
|
|
|
static int BackwardRefsWithLocalCache(const uint32_t* const argb,
|
|
|
|
int cache_bits,
|
2014-11-12 16:53:46 -08:00
|
|
|
VP8LBackwardRefs* const refs) {
|
2014-11-11 11:24:02 -08:00
|
|
|
int pixel_index = 0;
|
|
|
|
VP8LColorCache hashers;
|
2014-11-12 16:53:46 -08:00
|
|
|
VP8LRefsCursor c = VP8LRefsCursorInit(refs);
|
2014-11-11 11:24:02 -08:00
|
|
|
if (!VP8LColorCacheInit(&hashers, cache_bits)) return 0;
|
|
|
|
|
|
|
|
while (VP8LRefsCursorOk(&c)) {
|
2014-11-12 16:53:46 -08:00
|
|
|
PixOrCopy* const v = c.cur_pos;
|
|
|
|
if (PixOrCopyIsLiteral(v)) {
|
|
|
|
const uint32_t argb_literal = v->argb_or_distance;
|
2016-08-12 15:16:06 -07:00
|
|
|
const int ix = VP8LColorCacheContains(&hashers, argb_literal);
|
|
|
|
if (ix >= 0) {
|
|
|
|
// hashers contains argb_literal
|
2014-11-12 16:53:46 -08:00
|
|
|
*v = PixOrCopyCreateCacheIdx(ix);
|
2014-11-11 11:24:02 -08:00
|
|
|
} else {
|
|
|
|
VP8LColorCacheInsert(&hashers, argb_literal);
|
|
|
|
}
|
|
|
|
++pixel_index;
|
|
|
|
} else {
|
2014-11-12 16:53:46 -08:00
|
|
|
// refs was created without local cache, so it can not have cache indexes.
|
2014-11-11 11:24:02 -08:00
|
|
|
int k;
|
2014-11-12 16:53:46 -08:00
|
|
|
assert(PixOrCopyIsCopy(v));
|
|
|
|
for (k = 0; k < v->len; ++k) {
|
2014-11-11 11:24:02 -08:00
|
|
|
VP8LColorCacheInsert(&hashers, argb[pixel_index++]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VP8LRefsCursorNext(&c);
|
|
|
|
}
|
|
|
|
VP8LColorCacheClear(&hashers);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-01-27 09:35:32 -08:00
|
|
|
static VP8LBackwardRefs* GetBackwardReferencesLowEffort(
|
2016-06-08 19:19:08 +02:00
|
|
|
int width, int height, const uint32_t* const argb,
|
2016-05-24 18:00:48 +02:00
|
|
|
int* const cache_bits, const VP8LHashChain* const hash_chain,
|
2017-04-13 16:52:38 +02:00
|
|
|
VP8LBackwardRefs* const refs_lz77) {
|
2015-01-27 09:35:32 -08:00
|
|
|
*cache_bits = 0;
|
2016-06-08 19:19:08 +02:00
|
|
|
if (!BackwardReferencesLz77(width, height, argb, 0, hash_chain, refs_lz77)) {
|
2015-01-27 09:35:32 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
BackwardReferences2DLocality(width, refs_lz77);
|
|
|
|
return refs_lz77;
|
|
|
|
}
|
|
|
|
|
2017-06-01 15:07:28 +02:00
|
|
|
extern int VP8LBackwardReferencesTraceBackwards(
|
|
|
|
int xsize, int ysize, const uint32_t* const argb, int cache_bits,
|
|
|
|
const VP8LHashChain* const hash_chain,
|
|
|
|
const VP8LBackwardRefs* const refs_src, VP8LBackwardRefs* const refs_dst);
|
2015-01-27 09:35:32 -08:00
|
|
|
static VP8LBackwardRefs* GetBackwardReferences(
|
2014-04-25 16:01:49 -07:00
|
|
|
int width, int height, const uint32_t* const argb, int quality,
|
2017-06-07 15:13:35 +02:00
|
|
|
int lz77_types_to_try, int* const cache_bits,
|
|
|
|
const VP8LHashChain* const hash_chain, VP8LBackwardRefs* best,
|
|
|
|
VP8LBackwardRefs* worst) {
|
2017-05-29 16:00:44 +02:00
|
|
|
const int cache_bits_initial = *cache_bits;
|
|
|
|
double bit_cost_best = -1;
|
2014-11-06 13:13:00 -08:00
|
|
|
VP8LHistogram* histo = NULL;
|
2017-06-07 15:13:35 +02:00
|
|
|
int lz77_type, lz77_type_best = 0;
|
2017-06-07 19:06:28 +02:00
|
|
|
VP8LHashChain hash_chain_box;
|
|
|
|
memset(&hash_chain_box, 0, sizeof(hash_chain_box));
|
2014-11-06 13:13:00 -08:00
|
|
|
|
2017-05-29 16:00:44 +02:00
|
|
|
histo = VP8LAllocateHistogram(MAX_COLOR_CACHE_BITS);
|
|
|
|
if (histo == NULL) goto Error;
|
|
|
|
|
2017-06-07 15:13:35 +02:00
|
|
|
for (lz77_type = 1; lz77_types_to_try;
|
|
|
|
lz77_types_to_try &= ~lz77_type, lz77_type <<= 1) {
|
|
|
|
int res = 0;
|
2017-05-29 16:00:44 +02:00
|
|
|
double bit_cost;
|
|
|
|
int cache_bits_tmp = cache_bits_initial;
|
2017-06-07 15:13:35 +02:00
|
|
|
if ((lz77_types_to_try & lz77_type) == 0) continue;
|
|
|
|
switch (lz77_type) {
|
|
|
|
case kLZ77RLE:
|
|
|
|
res = BackwardReferencesRle(width, height, argb, 0, worst);
|
|
|
|
break;
|
|
|
|
case kLZ77Standard:
|
|
|
|
// Compute LZ77 with no cache (0 bits), as the ideal LZ77 with a color
|
|
|
|
// cache is not that different in practice.
|
|
|
|
res = BackwardReferencesLz77(width, height, argb, 0, hash_chain, worst);
|
|
|
|
break;
|
2017-06-07 19:06:28 +02:00
|
|
|
case kLZ77Box:
|
|
|
|
if (!VP8LHashChainInit(&hash_chain_box, width * height)) goto Error;
|
|
|
|
res = BackwardReferencesLz77Box(width, height, argb, 0, hash_chain,
|
|
|
|
&hash_chain_box, worst);
|
|
|
|
break;
|
2017-06-07 15:13:35 +02:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-11-11 11:24:02 -08:00
|
|
|
}
|
2017-05-29 16:00:44 +02:00
|
|
|
if (!res) goto Error;
|
2014-11-11 11:24:02 -08:00
|
|
|
|
2017-05-29 16:00:44 +02:00
|
|
|
// Next, try with a color cache and update the references.
|
|
|
|
if (!CalculateBestCacheSize(argb, quality, worst, &cache_bits_tmp)) {
|
2017-02-23 17:00:37 +01:00
|
|
|
goto Error;
|
|
|
|
}
|
2017-05-29 16:00:44 +02:00
|
|
|
if (cache_bits_tmp > 0) {
|
|
|
|
if (!BackwardRefsWithLocalCache(argb, cache_bits_tmp, worst)) {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
2014-11-06 13:13:00 -08:00
|
|
|
|
2017-05-29 16:00:44 +02:00
|
|
|
// Keep the best backward references.
|
|
|
|
VP8LHistogramCreate(histo, worst, cache_bits_tmp);
|
|
|
|
bit_cost = VP8LHistogramEstimateBits(histo);
|
2017-06-07 15:13:35 +02:00
|
|
|
if (lz77_type_best == 0 || bit_cost < bit_cost_best) {
|
2017-05-29 16:00:44 +02:00
|
|
|
VP8LBackwardRefs* const tmp = worst;
|
|
|
|
worst = best;
|
|
|
|
best = tmp;
|
|
|
|
bit_cost_best = bit_cost;
|
|
|
|
*cache_bits = cache_bits_tmp;
|
2017-06-07 15:13:35 +02:00
|
|
|
lz77_type_best = lz77_type;
|
2017-05-29 16:00:44 +02:00
|
|
|
}
|
2012-05-09 12:11:55 +05:30
|
|
|
}
|
2017-06-07 15:13:35 +02:00
|
|
|
assert(lz77_type_best > 0);
|
2012-05-09 12:11:55 +05:30
|
|
|
|
2017-05-29 16:00:44 +02:00
|
|
|
// Improve on simple LZ77 but only for high quality (TraceBackwards is
|
|
|
|
// costly).
|
2017-06-07 19:06:28 +02:00
|
|
|
if ((lz77_type_best == kLZ77Standard || lz77_type_best == kLZ77Box) &&
|
|
|
|
quality >= 25) {
|
|
|
|
const VP8LHashChain* const hash_chain_tmp =
|
|
|
|
(lz77_type_best == kLZ77Standard) ? hash_chain : &hash_chain_box;
|
2017-06-01 15:07:28 +02:00
|
|
|
if (VP8LBackwardReferencesTraceBackwards(width, height, argb, *cache_bits,
|
2017-06-07 19:06:28 +02:00
|
|
|
hash_chain_tmp, best, worst)) {
|
2017-05-29 16:00:44 +02:00
|
|
|
double bit_cost_trace;
|
|
|
|
VP8LHistogramCreate(histo, worst, *cache_bits);
|
|
|
|
bit_cost_trace = VP8LHistogramEstimateBits(histo);
|
|
|
|
if (bit_cost_trace < bit_cost_best) best = worst;
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-09 12:11:55 +05:30
|
|
|
|
2014-11-06 13:13:00 -08:00
|
|
|
BackwardReferences2DLocality(width, best);
|
2012-05-25 02:52:44 -07:00
|
|
|
|
2017-06-07 15:13:35 +02:00
|
|
|
Error:
|
2017-06-07 19:06:28 +02:00
|
|
|
VP8LHashChainClear(&hash_chain_box);
|
2014-10-17 09:20:02 -07:00
|
|
|
VP8LFreeHistogram(histo);
|
2014-04-25 16:01:49 -07:00
|
|
|
return best;
|
2012-04-03 14:24:25 +00:00
|
|
|
}
|
2015-01-27 09:35:32 -08:00
|
|
|
|
|
|
|
VP8LBackwardRefs* VP8LGetBackwardReferences(
|
|
|
|
int width, int height, const uint32_t* const argb, int quality,
|
2017-06-07 15:13:35 +02:00
|
|
|
int low_effort, int lz77_types_to_try, int* const cache_bits,
|
2017-04-13 16:52:38 +02:00
|
|
|
const VP8LHashChain* const hash_chain, VP8LBackwardRefs* const refs_tmp1,
|
|
|
|
VP8LBackwardRefs* const refs_tmp2) {
|
2015-01-27 09:35:32 -08:00
|
|
|
if (low_effort) {
|
2016-06-08 19:19:08 +02:00
|
|
|
return GetBackwardReferencesLowEffort(width, height, argb, cache_bits,
|
2017-04-13 16:52:38 +02:00
|
|
|
hash_chain, refs_tmp1);
|
2015-01-27 09:35:32 -08:00
|
|
|
} else {
|
2017-06-07 15:13:35 +02:00
|
|
|
return GetBackwardReferences(width, height, argb, quality,
|
|
|
|
lz77_types_to_try, cache_bits, hash_chain,
|
|
|
|
refs_tmp1, refs_tmp2);
|
2015-01-27 09:35:32 -08:00
|
|
|
}
|
|
|
|
}
|