Path: blob/master/3rdparty/libwebp/src/enc/backward_references_enc.h
16349 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/format_constants.h"1920#ifdef __cplusplus21extern "C" {22#endif2324// The maximum allowed limit is 11.25#define MAX_COLOR_CACHE_BITS 102627// -----------------------------------------------------------------------------28// PixOrCopy2930enum Mode {31kLiteral,32kCacheIdx,33kCopy,34kNone35};3637typedef struct {38// mode as uint8_t to make the memory layout to be exactly 8 bytes.39uint8_t mode;40uint16_t len;41uint32_t argb_or_distance;42} PixOrCopy;4344static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t distance,45uint16_t len) {46PixOrCopy retval;47retval.mode = kCopy;48retval.argb_or_distance = distance;49retval.len = len;50return retval;51}5253static WEBP_INLINE PixOrCopy PixOrCopyCreateCacheIdx(int idx) {54PixOrCopy retval;55assert(idx >= 0);56assert(idx < (1 << MAX_COLOR_CACHE_BITS));57retval.mode = kCacheIdx;58retval.argb_or_distance = idx;59retval.len = 1;60return retval;61}6263static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) {64PixOrCopy retval;65retval.mode = kLiteral;66retval.argb_or_distance = argb;67retval.len = 1;68return retval;69}7071static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy* const p) {72return (p->mode == kLiteral);73}7475static WEBP_INLINE int PixOrCopyIsCacheIdx(const PixOrCopy* const p) {76return (p->mode == kCacheIdx);77}7879static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy* const p) {80return (p->mode == kCopy);81}8283static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy* const p,84int component) {85assert(p->mode == kLiteral);86return (p->argb_or_distance >> (component * 8)) & 0xff;87}8889static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy* const p) {90return p->len;91}9293static WEBP_INLINE uint32_t PixOrCopyCacheIdx(const PixOrCopy* const p) {94assert(p->mode == kCacheIdx);95assert(p->argb_or_distance < (1U << MAX_COLOR_CACHE_BITS));96return p->argb_or_distance;97}9899static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy* const p) {100assert(p->mode == kCopy);101return p->argb_or_distance;102}103104// -----------------------------------------------------------------------------105// VP8LHashChain106107#define HASH_BITS 18108#define HASH_SIZE (1 << HASH_BITS)109110// If you change this, you need MAX_LENGTH_BITS + WINDOW_SIZE_BITS <= 32 as it111// is used in VP8LHashChain.112#define MAX_LENGTH_BITS 12113#define WINDOW_SIZE_BITS 20114// We want the max value to be attainable and stored in MAX_LENGTH_BITS bits.115#define MAX_LENGTH ((1 << MAX_LENGTH_BITS) - 1)116#if MAX_LENGTH_BITS + WINDOW_SIZE_BITS > 32117#error "MAX_LENGTH_BITS + WINDOW_SIZE_BITS > 32"118#endif119120typedef struct VP8LHashChain VP8LHashChain;121struct VP8LHashChain {122// The 20 most significant bits contain the offset at which the best match123// is found. These 20 bits are the limit defined by GetWindowSizeForHashChain124// (through WINDOW_SIZE = 1<<20).125// The lower 12 bits contain the length of the match. The 12 bit limit is126// defined in MaxFindCopyLength with MAX_LENGTH=4096.127uint32_t* offset_length_;128// This is the maximum size of the hash_chain that can be constructed.129// Typically this is the pixel count (width x height) for a given image.130int size_;131};132133// Must be called first, to set size.134int VP8LHashChainInit(VP8LHashChain* const p, int size);135// Pre-compute the best matches for argb.136int VP8LHashChainFill(VP8LHashChain* const p, int quality,137const uint32_t* const argb, int xsize, int ysize,138int low_effort);139void VP8LHashChainClear(VP8LHashChain* const p); // release memory140141static WEBP_INLINE int VP8LHashChainFindOffset(const VP8LHashChain* const p,142const int base_position) {143return p->offset_length_[base_position] >> MAX_LENGTH_BITS;144}145146static WEBP_INLINE int VP8LHashChainFindLength(const VP8LHashChain* const p,147const int base_position) {148return p->offset_length_[base_position] & ((1U << MAX_LENGTH_BITS) - 1);149}150151static WEBP_INLINE void VP8LHashChainFindCopy(const VP8LHashChain* const p,152int base_position,153int* const offset_ptr,154int* const length_ptr) {155*offset_ptr = VP8LHashChainFindOffset(p, base_position);156*length_ptr = VP8LHashChainFindLength(p, base_position);157}158159// -----------------------------------------------------------------------------160// VP8LBackwardRefs (block-based backward-references storage)161162// maximum number of reference blocks the image will be segmented into163#define MAX_REFS_BLOCK_PER_IMAGE 16164165typedef struct PixOrCopyBlock PixOrCopyBlock; // forward declaration166typedef struct VP8LBackwardRefs VP8LBackwardRefs;167168// Container for blocks chain169struct VP8LBackwardRefs {170int block_size_; // common block-size171int error_; // set to true if some memory error occurred172PixOrCopyBlock* refs_; // list of currently used blocks173PixOrCopyBlock** tail_; // for list recycling174PixOrCopyBlock* free_blocks_; // free-list175PixOrCopyBlock* last_block_; // used for adding new refs (internal)176};177178// Initialize the object. 'block_size' is the common block size to store179// references (typically, width * height / MAX_REFS_BLOCK_PER_IMAGE).180void VP8LBackwardRefsInit(VP8LBackwardRefs* const refs, int block_size);181// Release memory for backward references.182void VP8LBackwardRefsClear(VP8LBackwardRefs* const refs);183184// Cursor for iterating on references content185typedef struct {186// public:187PixOrCopy* cur_pos; // current position188// private:189PixOrCopyBlock* cur_block_; // current block in the refs list190const PixOrCopy* last_pos_; // sentinel for switching to next block191} VP8LRefsCursor;192193// Returns a cursor positioned at the beginning of the references list.194VP8LRefsCursor VP8LRefsCursorInit(const VP8LBackwardRefs* const refs);195// Returns true if cursor is pointing at a valid position.196static WEBP_INLINE int VP8LRefsCursorOk(const VP8LRefsCursor* const c) {197return (c->cur_pos != NULL);198}199// Move to next block of references. Internal, not to be called directly.200void VP8LRefsCursorNextBlock(VP8LRefsCursor* const c);201// Move to next position, or NULL. Should not be called if !VP8LRefsCursorOk().202static WEBP_INLINE void VP8LRefsCursorNext(VP8LRefsCursor* const c) {203assert(c != NULL);204assert(VP8LRefsCursorOk(c));205if (++c->cur_pos == c->last_pos_) VP8LRefsCursorNextBlock(c);206}207208// -----------------------------------------------------------------------------209// Main entry points210211enum VP8LLZ77Type {212kLZ77Standard = 1,213kLZ77RLE = 2,214kLZ77Box = 4215};216217// Evaluates best possible backward references for specified quality.218// The input cache_bits to 'VP8LGetBackwardReferences' sets the maximum cache219// bits to use (passing 0 implies disabling the local color cache).220// The optimal cache bits is evaluated and set for the *cache_bits parameter.221// The return value is the pointer to the best of the two backward refs viz,222// refs[0] or refs[1].223VP8LBackwardRefs* VP8LGetBackwardReferences(224int width, int height, const uint32_t* const argb, int quality,225int low_effort, int lz77_types_to_try, int* const cache_bits,226const VP8LHashChain* const hash_chain, VP8LBackwardRefs* const refs_tmp1,227VP8LBackwardRefs* const refs_tmp2);228229#ifdef __cplusplus230}231#endif232233#endif // WEBP_ENC_BACKWARD_REFERENCES_ENC_H_234235236