Path: blob/master/thirdparty/libwebp/src/enc/histogram_enc.h
21654 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 "src/enc/backward_references_enc.h"17#include "src/webp/encode.h"18#include "src/webp/format_constants.h"19#include "src/webp/types.h"2021#ifdef __cplusplus22extern "C" {23#endif2425// Not a trivial literal symbol.26#define VP8L_NON_TRIVIAL_SYM ((uint16_t)(0xffff))2728// A simple container for histograms of data.29typedef struct {30// 'literal' contains green literal, palette-code and31// copy-length-prefix histogram32uint32_t* literal; // Pointer to the allocated buffer for literal.33uint32_t red[NUM_LITERAL_CODES];34uint32_t blue[NUM_LITERAL_CODES];35uint32_t alpha[NUM_LITERAL_CODES];36// Backward reference prefix-code histogram.37uint32_t distance[NUM_DISTANCE_CODES];38int palette_code_bits;39// The following members are only used within VP8LGetHistoImageSymbols.4041// Index of the unique value of a histogram if any, VP8L_NON_TRIVIAL_SYM42// otherwise.43uint16_t trivial_symbol[5];44uint64_t bit_cost; // Cached value of total bit cost.45// Cached values of entropy costs: literal, red, blue, alpha, distance46uint64_t costs[5];47uint8_t is_used[5]; // 5 for literal, red, blue, alpha, distance48uint16_t bin_id; // entropy bin index.49} VP8LHistogram;5051// Collection of histograms with fixed capacity, allocated as one52// big memory chunk. Can be destroyed by calling WebPSafeFree().53typedef struct {54int size; // number of slots currently in use55int max_size; // maximum capacity56VP8LHistogram** histograms;57} VP8LHistogramSet;5859// Create the histogram.60//61// The input data is the PixOrCopy data, which models the literals, stop62// codes and backward references (both distances and lengths). Also: if63// palette_code_bits is >= 0, initialize the histogram with this value.64void VP8LHistogramCreate(VP8LHistogram* const h,65const VP8LBackwardRefs* const refs,66int palette_code_bits);6768// Set the palette_code_bits and reset the stats.69// If init_arrays is true, the arrays are also filled with 0's.70void VP8LHistogramInit(VP8LHistogram* const h, int palette_code_bits,71int init_arrays);7273// Collect all the references into a histogram (without reset)74// The distance modifier function is applied to the distance before75// the histogram is updated. It can be NULL.76void VP8LHistogramStoreRefs(const VP8LBackwardRefs* const refs,77int (*const distance_modifier)(int, int),78int distance_modifier_arg0,79VP8LHistogram* const histo);8081// Free the memory allocated for the histogram.82void VP8LFreeHistogram(VP8LHistogram* const histo);8384// Free the memory allocated for the histogram set.85void VP8LFreeHistogramSet(VP8LHistogramSet* const histo);8687// Allocate an array of pointer to histograms, allocated and initialized88// using 'cache_bits'. Return NULL in case of memory error.89VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits);9091// Set the histograms in set to 0.92void VP8LHistogramSetClear(VP8LHistogramSet* const set);9394// Allocate and initialize histogram object with specified 'cache_bits'.95// Returns NULL in case of memory error.96// Special case of VP8LAllocateHistogramSet, with size equals 1.97VP8LHistogram* VP8LAllocateHistogram(int cache_bits);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(const VP8LHistogram* const h);121122#ifdef __cplusplus123}124#endif125126#endif // WEBP_ENC_HISTOGRAM_ENC_H_127128129