Path: blob/master/3rdparty/libwebp/src/enc/histogram_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//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.42double bit_cost_; // cached value of bit cost.43double literal_cost_; // Cached values of dominant entropy costs:44double red_cost_; // literal, red & blue.45double blue_cost_;46} VP8LHistogram;4748// Collection of histograms with fixed capacity, allocated as one49// big memory chunk. Can be destroyed by calling WebPSafeFree().50typedef struct {51int size; // number of slots currently in use52int max_size; // maximum capacity53VP8LHistogram** histograms;54} VP8LHistogramSet;5556// Create the histogram.57//58// The input data is the PixOrCopy data, which models the literals, stop59// codes and backward references (both distances and lengths). Also: if60// palette_code_bits is >= 0, initialize the histogram with this value.61void VP8LHistogramCreate(VP8LHistogram* const p,62const VP8LBackwardRefs* const refs,63int palette_code_bits);6465// Return the size of the histogram for a given palette_code_bits.66int VP8LGetHistogramSize(int palette_code_bits);6768// Set the palette_code_bits and reset the stats.69void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits);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// Allocate and initialize histogram object with specified 'cache_bits'.86// Returns NULL in case of memory error.87// Special case of VP8LAllocateHistogramSet, with size equals 1.88VP8LHistogram* VP8LAllocateHistogram(int cache_bits);8990// Accumulate a token 'v' into a histogram.91void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo,92const PixOrCopy* const v,93int (*const distance_modifier)(int, int),94int distance_modifier_arg0);9596static WEBP_INLINE int VP8LHistogramNumCodes(int palette_code_bits) {97return NUM_LITERAL_CODES + NUM_LENGTH_CODES +98((palette_code_bits > 0) ? (1 << palette_code_bits) : 0);99}100101// Builds the histogram image.102int VP8LGetHistoImageSymbols(int xsize, int ysize,103const VP8LBackwardRefs* const refs,104int quality, int low_effort,105int histogram_bits, int cache_bits,106VP8LHistogramSet* const image_in,107VP8LHistogram* const tmp_histo,108uint16_t* const histogram_symbols);109110// Returns the entropy for the symbols in the input array.111double VP8LBitsEntropy(const uint32_t* const array, int n);112113// Estimate how many bits the combined entropy of literals and distance114// approximately maps to.115double VP8LHistogramEstimateBits(const VP8LHistogram* const p);116117#ifdef __cplusplus118}119#endif120121#endif // WEBP_ENC_HISTOGRAM_ENC_H_122123124