Path: blob/master/thirdparty/libwebp/src/enc/backward_references_enc.h
21661 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>1718#include "src/webp/encode.h"19#include "src/webp/format_constants.h"20#include "src/webp/types.h"2122#ifdef __cplusplus23extern "C" {24#endif2526// The maximum allowed limit is 11.27#define MAX_COLOR_CACHE_BITS 102829// -----------------------------------------------------------------------------30// PixOrCopy3132enum Mode {33kLiteral,34kCacheIdx,35kCopy,36kNone37};3839typedef struct {40// mode as uint8_t to make the memory layout to be exactly 8 bytes.41uint8_t mode;42uint16_t len;43uint32_t argb_or_distance;44} PixOrCopy;4546static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t distance,47uint16_t len) {48PixOrCopy retval;49retval.mode = kCopy;50retval.argb_or_distance = distance;51retval.len = len;52return retval;53}5455static WEBP_INLINE PixOrCopy PixOrCopyCreateCacheIdx(int idx) {56PixOrCopy retval;57assert(idx >= 0);58assert(idx < (1 << MAX_COLOR_CACHE_BITS));59retval.mode = kCacheIdx;60retval.argb_or_distance = idx;61retval.len = 1;62return retval;63}6465static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) {66PixOrCopy retval;67retval.mode = kLiteral;68retval.argb_or_distance = argb;69retval.len = 1;70return retval;71}7273static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy* const p) {74return (p->mode == kLiteral);75}7677static WEBP_INLINE int PixOrCopyIsCacheIdx(const PixOrCopy* const p) {78return (p->mode == kCacheIdx);79}8081static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy* const p) {82return (p->mode == kCopy);83}8485static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy* const p,86int component) {87assert(p->mode == kLiteral);88return (p->argb_or_distance >> (component * 8)) & 0xff;89}9091static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy* const p) {92return p->len;93}9495static WEBP_INLINE uint32_t PixOrCopyCacheIdx(const PixOrCopy* const p) {96assert(p->mode == kCacheIdx);97assert(p->argb_or_distance < (1U << MAX_COLOR_CACHE_BITS));98return p->argb_or_distance;99}100101static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy* const p) {102assert(p->mode == kCopy);103return p->argb_or_distance;104}105106// -----------------------------------------------------------------------------107// VP8LHashChain108109#define HASH_BITS 18110#define HASH_SIZE (1 << HASH_BITS)111112// If you change this, you need MAX_LENGTH_BITS + WINDOW_SIZE_BITS <= 32 as it113// is used in VP8LHashChain.114#define MAX_LENGTH_BITS 12115#define WINDOW_SIZE_BITS 20116// We want the max value to be attainable and stored in MAX_LENGTH_BITS bits.117#define MAX_LENGTH ((1 << MAX_LENGTH_BITS) - 1)118#if MAX_LENGTH_BITS + WINDOW_SIZE_BITS > 32119#error "MAX_LENGTH_BITS + WINDOW_SIZE_BITS > 32"120#endif121122typedef struct VP8LHashChain VP8LHashChain;123struct VP8LHashChain {124// The 20 most significant bits contain the offset at which the best match125// is found. These 20 bits are the limit defined by GetWindowSizeForHashChain126// (through WINDOW_SIZE = 1<<20).127// The lower 12 bits contain the length of the match. The 12 bit limit is128// defined in MaxFindCopyLength with MAX_LENGTH=4096.129uint32_t* offset_length;130// This is the maximum size of the hash_chain that can be constructed.131// Typically this is the pixel count (width x height) for a given image.132int size;133};134135// Must be called first, to set size.136int VP8LHashChainInit(VP8LHashChain* const p, int size);137// Pre-compute the best matches for argb. pic and percent are for progress.138int VP8LHashChainFill(VP8LHashChain* const p, int quality,139const uint32_t* const argb, int xsize, int ysize,140int low_effort, const WebPPicture* const pic,141int percent_range, int* const percent);142void VP8LHashChainClear(VP8LHashChain* const p); // release memory143144static WEBP_INLINE int VP8LHashChainFindOffset(const VP8LHashChain* const p,145const int base_position) {146return p->offset_length[base_position] >> MAX_LENGTH_BITS;147}148149static WEBP_INLINE int VP8LHashChainFindLength(const VP8LHashChain* const p,150const int base_position) {151return p->offset_length[base_position] & ((1U << MAX_LENGTH_BITS) - 1);152}153154static WEBP_INLINE void VP8LHashChainFindCopy(const VP8LHashChain* const p,155int base_position,156int* const offset_ptr,157int* const length_ptr) {158*offset_ptr = VP8LHashChainFindOffset(p, base_position);159*length_ptr = VP8LHashChainFindLength(p, base_position);160}161162// -----------------------------------------------------------------------------163// VP8LBackwardRefs (block-based backward-references storage)164165// maximum number of reference blocks the image will be segmented into166#define MAX_REFS_BLOCK_PER_IMAGE 16167168typedef struct PixOrCopyBlock PixOrCopyBlock; // forward declaration169typedef struct VP8LBackwardRefs VP8LBackwardRefs;170171// Container for blocks chain172struct VP8LBackwardRefs {173int block_size; // common block-size174int error; // set to true if some memory error occurred175PixOrCopyBlock* refs; // list of currently used blocks176PixOrCopyBlock** tail; // for list recycling177PixOrCopyBlock* free_blocks; // free-list178PixOrCopyBlock* last_block; // used for adding new refs (internal)179};180181// Initialize the object. 'block_size' is the common block size to store182// references (typically, width * height / MAX_REFS_BLOCK_PER_IMAGE).183void VP8LBackwardRefsInit(VP8LBackwardRefs* const refs, int block_size);184// Release memory for backward references.185void VP8LBackwardRefsClear(VP8LBackwardRefs* const refs);186187// Cursor for iterating on references content188typedef struct {189// public:190PixOrCopy* cur_pos; // current position191// private:192PixOrCopyBlock* cur_block; // current block in the refs list193const PixOrCopy* last_pos; // sentinel for switching to next block194} VP8LRefsCursor;195196// Returns a cursor positioned at the beginning of the references list.197VP8LRefsCursor VP8LRefsCursorInit(const VP8LBackwardRefs* const refs);198// Returns true if cursor is pointing at a valid position.199static WEBP_INLINE int VP8LRefsCursorOk(const VP8LRefsCursor* const c) {200return (c->cur_pos != NULL);201}202// Move to next block of references. Internal, not to be called directly.203void VP8LRefsCursorNextBlock(VP8LRefsCursor* const c);204// Move to next position, or NULL. Should not be called if !VP8LRefsCursorOk().205static WEBP_INLINE void VP8LRefsCursorNext(VP8LRefsCursor* const c) {206assert(c != NULL);207assert(VP8LRefsCursorOk(c));208if (++c->cur_pos == c->last_pos) VP8LRefsCursorNextBlock(c);209}210211// -----------------------------------------------------------------------------212// Main entry points213214enum VP8LLZ77Type {215kLZ77Standard = 1,216kLZ77RLE = 2,217kLZ77Box = 4218};219220// Evaluates best possible backward references for specified quality.221// The input cache_bits to 'VP8LGetBackwardReferences' sets the maximum cache222// bits to use (passing 0 implies disabling the local color cache).223// The optimal cache bits is evaluated and set for the *cache_bits_best224// parameter with the matching refs_best.225// If do_no_cache == 0, refs is an array of 2 values and the best226// VP8LBackwardRefs is put in the first element.227// If do_no_cache != 0, refs is an array of 3 values and the best228// VP8LBackwardRefs is put in the first element, the best value with no-cache in229// the second element.230// In both cases, the last element is used as temporary internally.231// pic and percent are for progress.232// Returns false in case of error (stored in pic->error_code).233int VP8LGetBackwardReferences(234int width, int height, const uint32_t* const argb, int quality,235int low_effort, int lz77_types_to_try, int cache_bits_max, int do_no_cache,236const VP8LHashChain* const hash_chain, VP8LBackwardRefs* const refs,237int* const cache_bits_best, const WebPPicture* const pic, int percent_range,238int* const percent);239240#ifdef __cplusplus241}242#endif243244#endif // WEBP_ENC_BACKWARD_REFERENCES_ENC_H_245246247