Path: blob/master/thirdparty/libwebp/src/enc/histogram_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//11// Models the histograms of literal and distance codes.1213#ifndef WEBP_ENC_HISTOGRAM_ENC_H_14#define WEBP_ENC_HISTOGRAM_ENC_H_1516#include <string.h>1718#include "src/enc/backward_references_enc.h"19#include "src/webp/format_constants.h"20#include "src/webp/types.h"2122#ifdef __cplusplus23extern "C" {24#endif2526// Not a trivial literal symbol.27#define VP8L_NON_TRIVIAL_SYM (0xffffffff)2829// A simple container for histograms of data.30typedef struct {31// literal_ contains green literal, palette-code and32// copy-length-prefix histogram33uint32_t* literal_; // Pointer to the allocated buffer for literal.34uint32_t red_[NUM_LITERAL_CODES];35uint32_t blue_[NUM_LITERAL_CODES];36uint32_t alpha_[NUM_LITERAL_CODES];37// Backward reference prefix-code histogram.38uint32_t distance_[NUM_DISTANCE_CODES];39int palette_code_bits_;40uint32_t trivial_symbol_; // True, if histograms for Red, Blue & Alpha41// literal symbols are single valued.42uint64_t bit_cost_; // cached value of bit cost.43uint64_t literal_cost_; // Cached values of dominant entropy costs:44uint64_t red_cost_; // literal, red & blue.45uint64_t blue_cost_;46uint8_t is_used_[5]; // 5 for literal, red, blue, alpha, distance47} VP8LHistogram;4849// Collection of histograms with fixed capacity, allocated as one50// big memory chunk. Can be destroyed by calling WebPSafeFree().51typedef struct {52int size; // number of slots currently in use53int max_size; // maximum capacity54VP8LHistogram** histograms;55} VP8LHistogramSet;5657// Create the histogram.58//59// The input data is the PixOrCopy data, which models the literals, stop60// codes and backward references (both distances and lengths). Also: if61// palette_code_bits is >= 0, initialize the histogram with this value.62void VP8LHistogramCreate(VP8LHistogram* const p,63const VP8LBackwardRefs* const refs,64int palette_code_bits);6566// Set the palette_code_bits and reset the stats.67// If init_arrays is true, the arrays are also filled with 0's.68void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits,69int init_arrays);7071// Collect all the references into a histogram (without reset)72void VP8LHistogramStoreRefs(const VP8LBackwardRefs* const refs,73VP8LHistogram* const histo);7475// Free the memory allocated for the histogram.76void VP8LFreeHistogram(VP8LHistogram* const histo);7778// Free the memory allocated for the histogram set.79void VP8LFreeHistogramSet(VP8LHistogramSet* const histo);8081// Allocate an array of pointer to histograms, allocated and initialized82// using 'cache_bits'. Return NULL in case of memory error.83VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits);8485// Set the histograms in set to 0.86void VP8LHistogramSetClear(VP8LHistogramSet* const set);8788// Allocate and initialize histogram object with specified 'cache_bits'.89// Returns NULL in case of memory error.90// Special case of VP8LAllocateHistogramSet, with size equals 1.91VP8LHistogram* VP8LAllocateHistogram(int cache_bits);9293// Accumulate a token 'v' into a histogram.94void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo,95const PixOrCopy* const v,96int (*const distance_modifier)(int, int),97int distance_modifier_arg0);9899static WEBP_INLINE int VP8LHistogramNumCodes(int palette_code_bits) {100return NUM_LITERAL_CODES + NUM_LENGTH_CODES +101((palette_code_bits > 0) ? (1 << palette_code_bits) : 0);102}103104// Builds the histogram image. pic and percent are for progress.105// Returns false in case of error (stored in pic->error_code).106int VP8LGetHistoImageSymbols(int xsize, int ysize,107const VP8LBackwardRefs* const refs, int quality,108int low_effort, int histogram_bits, int cache_bits,109VP8LHistogramSet* const image_histo,110VP8LHistogram* const tmp_histo,111uint32_t* const histogram_symbols,112const WebPPicture* const pic, int percent_range,113int* const percent);114115// Returns the entropy for the symbols in the input array.116uint64_t VP8LBitsEntropy(const uint32_t* const array, int n);117118// Estimate how many bits the combined entropy of literals and distance119// approximately maps to.120uint64_t VP8LHistogramEstimateBits(VP8LHistogram* const p);121122#ifdef __cplusplus123}124#endif125126#endif // WEBP_ENC_HISTOGRAM_ENC_H_127128129