Path: blob/master/thirdparty/libwebp/src/enc/backward_references_enc.h
9913 views
// Copyright 2012 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Author: Jyrki Alakuijala ([email protected])10//1112#ifndef WEBP_ENC_BACKWARD_REFERENCES_ENC_H_13#define WEBP_ENC_BACKWARD_REFERENCES_ENC_H_1415#include <assert.h>16#include <stdlib.h>17#include "src/webp/types.h"18#include "src/webp/encode.h"19#include "src/webp/format_constants.h"2021#ifdef __cplusplus22extern "C" {23#endif2425// The maximum allowed limit is 11.26#define MAX_COLOR_CACHE_BITS 102728// -----------------------------------------------------------------------------29// PixOrCopy3031enum Mode {32kLiteral,33kCacheIdx,34kCopy,35kNone36};3738typedef struct {39// mode as uint8_t to make the memory layout to be exactly 8 bytes.40uint8_t mode;41uint16_t len;42uint32_t argb_or_distance;43} PixOrCopy;4445static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t distance,46uint16_t len) {47PixOrCopy retval;48retval.mode = kCopy;49retval.argb_or_distance = distance;50retval.len = len;51return retval;52}5354static WEBP_INLINE PixOrCopy PixOrCopyCreateCacheIdx(int idx) {55PixOrCopy retval;56assert(idx >= 0);57assert(idx < (1 << MAX_COLOR_CACHE_BITS));58retval.mode = kCacheIdx;59retval.argb_or_distance = idx;60retval.len = 1;61return retval;62}6364static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) {65PixOrCopy retval;66retval.mode = kLiteral;67retval.argb_or_distance = argb;68retval.len = 1;69return retval;70}7172static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy* const p) {73return (p->mode == kLiteral);74}7576static WEBP_INLINE int PixOrCopyIsCacheIdx(const PixOrCopy* const p) {77return (p->mode == kCacheIdx);78}7980static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy* const p) {81return (p->mode == kCopy);82}8384static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy* const p,85int component) {86assert(p->mode == kLiteral);87return (p->argb_or_distance >> (component * 8)) & 0xff;88}8990static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy* const p) {91return p->len;92}9394static WEBP_INLINE uint32_t PixOrCopyCacheIdx(const PixOrCopy* const p) {95assert(p->mode == kCacheIdx);96assert(p->argb_or_distance < (1U << MAX_COLOR_CACHE_BITS));97return p->argb_or_distance;98}99100static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy* const p) {101assert(p->mode == kCopy);102return p->argb_or_distance;103}104105// -----------------------------------------------------------------------------106// VP8LHashChain107108#define HASH_BITS 18109#define HASH_SIZE (1 << HASH_BITS)110111// If you change this, you need MAX_LENGTH_BITS + WINDOW_SIZE_BITS <= 32 as it112// is used in VP8LHashChain.113#define MAX_LENGTH_BITS 12114#define WINDOW_SIZE_BITS 20115// We want the max value to be attainable and stored in MAX_LENGTH_BITS bits.116#define MAX_LENGTH ((1 << MAX_LENGTH_BITS) - 1)117#if MAX_LENGTH_BITS + WINDOW_SIZE_BITS > 32118#error "MAX_LENGTH_BITS + WINDOW_SIZE_BITS > 32"119#endif120121typedef struct VP8LHashChain VP8LHashChain;122struct VP8LHashChain {123// The 20 most significant bits contain the offset at which the best match124// is found. These 20 bits are the limit defined by GetWindowSizeForHashChain125// (through WINDOW_SIZE = 1<<20).126// The lower 12 bits contain the length of the match. The 12 bit limit is127// defined in MaxFindCopyLength with MAX_LENGTH=4096.128uint32_t* offset_length_;129// This is the maximum size of the hash_chain that can be constructed.130// Typically this is the pixel count (width x height) for a given image.131int size_;132};133134// Must be called first, to set size.135int VP8LHashChainInit(VP8LHashChain* const p, int size);136// Pre-compute the best matches for argb. pic and percent are for progress.137int VP8LHashChainFill(VP8LHashChain* const p, int quality,138const uint32_t* const argb, int xsize, int ysize,139int low_effort, const WebPPicture* const pic,140int percent_range, int* const percent);141void VP8LHashChainClear(VP8LHashChain* const p); // release memory142143static WEBP_INLINE int VP8LHashChainFindOffset(const VP8LHashChain* const p,144const int base_position) {145return p->offset_length_[base_position] >> MAX_LENGTH_BITS;146}147148static WEBP_INLINE int VP8LHashChainFindLength(const VP8LHashChain* const p,149const int base_position) {150return p->offset_length_[base_position] & ((1U << MAX_LENGTH_BITS) - 1);151}152153static WEBP_INLINE void VP8LHashChainFindCopy(const VP8LHashChain* const p,154int base_position,155int* const offset_ptr,156int* const length_ptr) {157*offset_ptr = VP8LHashChainFindOffset(p, base_position);158*length_ptr = VP8LHashChainFindLength(p, base_position);159}160161// -----------------------------------------------------------------------------162// VP8LBackwardRefs (block-based backward-references storage)163164// maximum number of reference blocks the image will be segmented into165#define MAX_REFS_BLOCK_PER_IMAGE 16166167typedef struct PixOrCopyBlock PixOrCopyBlock; // forward declaration168typedef struct VP8LBackwardRefs VP8LBackwardRefs;169170// Container for blocks chain171struct VP8LBackwardRefs {172int block_size_; // common block-size173int error_; // set to true if some memory error occurred174PixOrCopyBlock* refs_; // list of currently used blocks175PixOrCopyBlock** tail_; // for list recycling176PixOrCopyBlock* free_blocks_; // free-list177PixOrCopyBlock* last_block_; // used for adding new refs (internal)178};179180// Initialize the object. 'block_size' is the common block size to store181// references (typically, width * height / MAX_REFS_BLOCK_PER_IMAGE).182void VP8LBackwardRefsInit(VP8LBackwardRefs* const refs, int block_size);183// Release memory for backward references.184void VP8LBackwardRefsClear(VP8LBackwardRefs* const refs);185186// Cursor for iterating on references content187typedef struct {188// public:189PixOrCopy* cur_pos; // current position190// private:191PixOrCopyBlock* cur_block_; // current block in the refs list192const PixOrCopy* last_pos_; // sentinel for switching to next block193} VP8LRefsCursor;194195// Returns a cursor positioned at the beginning of the references list.196VP8LRefsCursor VP8LRefsCursorInit(const VP8LBackwardRefs* const refs);197// Returns true if cursor is pointing at a valid position.198static WEBP_INLINE int VP8LRefsCursorOk(const VP8LRefsCursor* const c) {199return (c->cur_pos != NULL);200}201// Move to next block of references. Internal, not to be called directly.202void VP8LRefsCursorNextBlock(VP8LRefsCursor* const c);203// Move to next position, or NULL. Should not be called if !VP8LRefsCursorOk().204static WEBP_INLINE void VP8LRefsCursorNext(VP8LRefsCursor* const c) {205assert(c != NULL);206assert(VP8LRefsCursorOk(c));207if (++c->cur_pos == c->last_pos_) VP8LRefsCursorNextBlock(c);208}209210// -----------------------------------------------------------------------------211// Main entry points212213enum VP8LLZ77Type {214kLZ77Standard = 1,215kLZ77RLE = 2,216kLZ77Box = 4217};218219// Evaluates best possible backward references for specified quality.220// The input cache_bits to 'VP8LGetBackwardReferences' sets the maximum cache221// bits to use (passing 0 implies disabling the local color cache).222// The optimal cache bits is evaluated and set for the *cache_bits_best223// parameter with the matching refs_best.224// If do_no_cache == 0, refs is an array of 2 values and the best225// VP8LBackwardRefs is put in the first element.226// If do_no_cache != 0, refs is an array of 3 values and the best227// VP8LBackwardRefs is put in the first element, the best value with no-cache in228// the second element.229// In both cases, the last element is used as temporary internally.230// pic and percent are for progress.231// Returns false in case of error (stored in pic->error_code).232int VP8LGetBackwardReferences(233int width, int height, const uint32_t* const argb, int quality,234int low_effort, int lz77_types_to_try, int cache_bits_max, int do_no_cache,235const VP8LHashChain* const hash_chain, VP8LBackwardRefs* const refs,236int* const cache_bits_best, const WebPPicture* const pic, int percent_range,237int* const percent);238239#ifdef __cplusplus240}241#endif242243#endif // WEBP_ENC_BACKWARD_REFERENCES_ENC_H_244245246