mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-28 14:38:21 +01:00
Don't allocate VP8LHashChain, but treat like automatic object
the unique instance of VP8LHashChain (1MB size corresponding to hash_to_first_index_) is now wholy part of VP8LEncoder, instead of maintaining the pointer to VP8LHashChain in the encoder. Change-Id: Ib6fe52019fdd211fbbc78dc0ba731a4af0728677
This commit is contained in:
parent
b7f19b8311
commit
d3bcf72bf5
@ -112,12 +112,7 @@ int VP8LBackwardRefsCopy(const VP8LBackwardRefs* const src,
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// Hash chains
|
// Hash chains
|
||||||
|
|
||||||
static WEBP_INLINE uint64_t GetPixPairHash64(const uint32_t* const argb) {
|
// initialize as empty
|
||||||
uint64_t key = ((uint64_t)(argb[1]) << 32) | argb[0];
|
|
||||||
key = (key * HASH_MULTIPLIER) >> (64 - HASH_BITS);
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void HashChainInit(VP8LHashChain* const p) {
|
static void HashChainInit(VP8LHashChain* const p) {
|
||||||
int i;
|
int i;
|
||||||
assert(p != NULL);
|
assert(p != NULL);
|
||||||
@ -129,26 +124,30 @@ static void HashChainInit(VP8LHashChain* const p) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VP8LHashChain* VP8LHashChainNew(int size) {
|
int VP8LHashChainInit(VP8LHashChain* const p, int size) {
|
||||||
VP8LHashChain* const p = (VP8LHashChain*)WebPSafeMalloc(1ULL, sizeof(*p));
|
assert(p->size_ == 0);
|
||||||
if (p == NULL) {
|
assert(p->chain_ == NULL);
|
||||||
return NULL;
|
assert(size > 0);
|
||||||
}
|
|
||||||
p->chain_ = (int*)WebPSafeMalloc(size, sizeof(*p->chain_));
|
p->chain_ = (int*)WebPSafeMalloc(size, sizeof(*p->chain_));
|
||||||
if (p->chain_ == NULL) {
|
if (p->chain_ == NULL) return 0;
|
||||||
WebPSafeFree(p);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
p->size_ = size;
|
p->size_ = size;
|
||||||
HashChainInit(p);
|
HashChainInit(p);
|
||||||
return p;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VP8LHashChainDelete(VP8LHashChain* const p) {
|
void VP8LHashChainClear(VP8LHashChain* const p) {
|
||||||
if (p != NULL) {
|
assert(p != NULL);
|
||||||
WebPSafeFree(p->chain_);
|
WebPSafeFree(p->chain_);
|
||||||
WebPSafeFree(p);
|
p->size_ = 0;
|
||||||
|
p->chain_ = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static WEBP_INLINE uint64_t GetPixPairHash64(const uint32_t* const argb) {
|
||||||
|
uint64_t key = ((uint64_t)argb[1] << 32) | argb[0];
|
||||||
|
key = (key * HASH_MULTIPLIER) >> (64 - HASH_BITS);
|
||||||
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insertion of two pixels at a time.
|
// Insertion of two pixels at a time.
|
||||||
|
@ -130,8 +130,9 @@ struct VP8LHashChain {
|
|||||||
int size_;
|
int size_;
|
||||||
};
|
};
|
||||||
|
|
||||||
VP8LHashChain* VP8LHashChainNew(int size);
|
// Must be called first, to set size.
|
||||||
void VP8LHashChainDelete(VP8LHashChain* const p);
|
int VP8LHashChainInit(VP8LHashChain* const p, int size);
|
||||||
|
void VP8LHashChainClear(VP8LHashChain* const p); // release memory
|
||||||
|
|
||||||
typedef struct VP8LBackwardRefs VP8LBackwardRefs;
|
typedef struct VP8LBackwardRefs VP8LBackwardRefs;
|
||||||
struct VP8LBackwardRefs {
|
struct VP8LBackwardRefs {
|
||||||
|
@ -167,8 +167,7 @@ static int AnalyzeAndInit(VP8LEncoder* const enc, WebPImageHint image_hint) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
enc->hash_chain_ = VP8LHashChainNew(pix_cnt);
|
if (!VP8LHashChainInit(&enc->hash_chain_, pix_cnt)) return 0;
|
||||||
if (enc->hash_chain_ == NULL) return 0;
|
|
||||||
|
|
||||||
enc->refs_[0] = VP8LBackwardRefsNew(pix_cnt);
|
enc->refs_[0] = VP8LBackwardRefsNew(pix_cnt);
|
||||||
enc->refs_[1] = VP8LBackwardRefsNew(pix_cnt);
|
enc->refs_[1] = VP8LBackwardRefsNew(pix_cnt);
|
||||||
@ -739,7 +738,8 @@ static int ApplyPredictFilter(const VP8LEncoder* const enc,
|
|||||||
VP8LWriteBits(bw, 2, PREDICTOR_TRANSFORM);
|
VP8LWriteBits(bw, 2, PREDICTOR_TRANSFORM);
|
||||||
assert(pred_bits >= 2);
|
assert(pred_bits >= 2);
|
||||||
VP8LWriteBits(bw, 3, pred_bits - 2);
|
VP8LWriteBits(bw, 3, pred_bits - 2);
|
||||||
if (!EncodeImageNoHuffman(bw, enc->transform_data_, enc->hash_chain_,
|
if (!EncodeImageNoHuffman(bw, enc->transform_data_,
|
||||||
|
(VP8LHashChain*)&enc->hash_chain_,
|
||||||
enc->refs_, transform_width, transform_height,
|
enc->refs_, transform_width, transform_height,
|
||||||
quality)) {
|
quality)) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -760,7 +760,8 @@ static int ApplyCrossColorFilter(const VP8LEncoder* const enc,
|
|||||||
VP8LWriteBits(bw, 2, CROSS_COLOR_TRANSFORM);
|
VP8LWriteBits(bw, 2, CROSS_COLOR_TRANSFORM);
|
||||||
assert(ccolor_transform_bits >= 2);
|
assert(ccolor_transform_bits >= 2);
|
||||||
VP8LWriteBits(bw, 3, ccolor_transform_bits - 2);
|
VP8LWriteBits(bw, 3, ccolor_transform_bits - 2);
|
||||||
if (!EncodeImageNoHuffman(bw, enc->transform_data_, enc->hash_chain_,
|
if (!EncodeImageNoHuffman(bw, enc->transform_data_,
|
||||||
|
(VP8LHashChain*)&enc->hash_chain_,
|
||||||
enc->refs_, transform_width, transform_height,
|
enc->refs_, transform_width, transform_height,
|
||||||
quality)) {
|
quality)) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -960,7 +961,7 @@ static WebPEncodingError EncodePalette(VP8LBitWriter* const bw,
|
|||||||
for (i = palette_size - 1; i >= 1; --i) {
|
for (i = palette_size - 1; i >= 1; --i) {
|
||||||
palette[i] = VP8LSubPixels(palette[i], palette[i - 1]);
|
palette[i] = VP8LSubPixels(palette[i], palette[i - 1]);
|
||||||
}
|
}
|
||||||
if (!EncodeImageNoHuffman(bw, palette, enc->hash_chain_, enc->refs_,
|
if (!EncodeImageNoHuffman(bw, palette, &enc->hash_chain_, enc->refs_,
|
||||||
palette_size, 1, quality)) {
|
palette_size, 1, quality)) {
|
||||||
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
||||||
goto Error;
|
goto Error;
|
||||||
@ -1027,7 +1028,7 @@ static VP8LEncoder* VP8LEncoderNew(const WebPConfig* const config,
|
|||||||
|
|
||||||
static void VP8LEncoderDelete(VP8LEncoder* enc) {
|
static void VP8LEncoderDelete(VP8LEncoder* enc) {
|
||||||
if (enc != NULL) {
|
if (enc != NULL) {
|
||||||
VP8LHashChainDelete(enc->hash_chain_);
|
VP8LHashChainClear(&enc->hash_chain_);
|
||||||
VP8LBackwardRefsDelete(enc->refs_[0]);
|
VP8LBackwardRefsDelete(enc->refs_[0]);
|
||||||
VP8LBackwardRefsDelete(enc->refs_[1]);
|
VP8LBackwardRefsDelete(enc->refs_[1]);
|
||||||
WebPSafeFree(enc->argb_);
|
WebPSafeFree(enc->argb_);
|
||||||
@ -1112,7 +1113,7 @@ WebPEncodingError VP8LEncodeStream(const WebPConfig* const config,
|
|||||||
|
|
||||||
if (enc->cache_bits_ > 0) {
|
if (enc->cache_bits_ > 0) {
|
||||||
if (!VP8LCalculateEstimateForCacheSize(enc->argb_, enc->current_width_,
|
if (!VP8LCalculateEstimateForCacheSize(enc->argb_, enc->current_width_,
|
||||||
height, quality, enc->hash_chain_,
|
height, quality, &enc->hash_chain_,
|
||||||
enc->refs_[0], &enc->cache_bits_)) {
|
enc->refs_[0], &enc->cache_bits_)) {
|
||||||
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
||||||
goto Error;
|
goto Error;
|
||||||
@ -1122,7 +1123,7 @@ WebPEncodingError VP8LEncodeStream(const WebPConfig* const config,
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Encode and write the transformed image.
|
// Encode and write the transformed image.
|
||||||
|
|
||||||
if (!EncodeImageInternal(bw, enc->argb_, enc->hash_chain_, enc->refs_,
|
if (!EncodeImageInternal(bw, enc->argb_, &enc->hash_chain_, enc->refs_,
|
||||||
enc->current_width_, height, quality,
|
enc->current_width_, height, quality,
|
||||||
enc->cache_bits_, enc->histo_bits_)) {
|
enc->cache_bits_, enc->histo_bits_)) {
|
||||||
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
#ifndef WEBP_ENC_VP8LI_H_
|
#ifndef WEBP_ENC_VP8LI_H_
|
||||||
#define WEBP_ENC_VP8LI_H_
|
#define WEBP_ENC_VP8LI_H_
|
||||||
|
|
||||||
|
#include "./backward_references.h"
|
||||||
#include "./histogram.h"
|
#include "./histogram.h"
|
||||||
#include "../utils/bit_writer.h"
|
#include "../utils/bit_writer.h"
|
||||||
#include "../webp/encode.h"
|
#include "../webp/encode.h"
|
||||||
@ -23,8 +24,6 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct VP8LHashChain; // Defined in backward_references.h
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const WebPConfig* config_; // user configuration and parameters
|
const WebPConfig* config_; // user configuration and parameters
|
||||||
const WebPPicture* pic_; // input picture.
|
const WebPPicture* pic_; // input picture.
|
||||||
@ -35,11 +34,6 @@ typedef struct {
|
|||||||
uint32_t* transform_data_; // Scratch memory for transform data.
|
uint32_t* transform_data_; // Scratch memory for transform data.
|
||||||
int current_width_; // Corresponds to packed image width.
|
int current_width_; // Corresponds to packed image width.
|
||||||
|
|
||||||
struct VP8LHashChain* hash_chain_; // HashChain data for constructing
|
|
||||||
// backward references.
|
|
||||||
struct VP8LBackwardRefs* refs_[2]; // Backward Refs array corresponding to
|
|
||||||
// LZ77 & RLE coding.
|
|
||||||
|
|
||||||
// Encoding parameters derived from quality parameter.
|
// Encoding parameters derived from quality parameter.
|
||||||
int histo_bits_;
|
int histo_bits_;
|
||||||
int transform_bits_;
|
int transform_bits_;
|
||||||
@ -52,6 +46,12 @@ typedef struct {
|
|||||||
int use_palette_;
|
int use_palette_;
|
||||||
int palette_size_;
|
int palette_size_;
|
||||||
uint32_t palette_[MAX_PALETTE_SIZE];
|
uint32_t palette_[MAX_PALETTE_SIZE];
|
||||||
|
|
||||||
|
// Some 'scratch' (potentially large) objects.
|
||||||
|
struct VP8LBackwardRefs* refs_[2]; // Backward Refs array corresponding to
|
||||||
|
// LZ77 & RLE coding.
|
||||||
|
VP8LHashChain hash_chain_; // HashChain data for constructing
|
||||||
|
// backward references.
|
||||||
} VP8LEncoder;
|
} VP8LEncoder;
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user