Blame src/enc/histogram_enc.h

Packit 9c6abc
// Copyright 2012 Google Inc. All Rights Reserved.
Packit 9c6abc
//
Packit 9c6abc
// Use of this source code is governed by a BSD-style license
Packit 9c6abc
// that can be found in the COPYING file in the root of the source
Packit 9c6abc
// tree. An additional intellectual property rights grant can be found
Packit 9c6abc
// in the file PATENTS. All contributing project authors may
Packit 9c6abc
// be found in the AUTHORS file in the root of the source tree.
Packit 9c6abc
// -----------------------------------------------------------------------------
Packit 9c6abc
//
Packit 9c6abc
// Author: Jyrki Alakuijala (jyrki@google.com)
Packit 9c6abc
//
Packit 9c6abc
// Models the histograms of literal and distance codes.
Packit 9c6abc
Packit 9c6abc
#ifndef WEBP_ENC_HISTOGRAM_ENC_H_
Packit 9c6abc
#define WEBP_ENC_HISTOGRAM_ENC_H_
Packit 9c6abc
Packit 9c6abc
#include <string.h>
Packit 9c6abc
Packit 9c6abc
#include "src/enc/backward_references_enc.h"
Packit 9c6abc
#include "src/webp/format_constants.h"
Packit 9c6abc
#include "src/webp/types.h"
Packit 9c6abc
Packit 9c6abc
#ifdef __cplusplus
Packit 9c6abc
extern "C" {
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
// Not a trivial literal symbol.
Packit 9c6abc
#define VP8L_NON_TRIVIAL_SYM (0xffffffff)
Packit 9c6abc
Packit 9c6abc
// A simple container for histograms of data.
Packit 9c6abc
typedef struct {
Packit 9c6abc
  // literal_ contains green literal, palette-code and
Packit 9c6abc
  // copy-length-prefix histogram
Packit 9c6abc
  uint32_t* literal_;         // Pointer to the allocated buffer for literal.
Packit 9c6abc
  uint32_t red_[NUM_LITERAL_CODES];
Packit 9c6abc
  uint32_t blue_[NUM_LITERAL_CODES];
Packit 9c6abc
  uint32_t alpha_[NUM_LITERAL_CODES];
Packit 9c6abc
  // Backward reference prefix-code histogram.
Packit 9c6abc
  uint32_t distance_[NUM_DISTANCE_CODES];
Packit 9c6abc
  int palette_code_bits_;
Packit 9c6abc
  uint32_t trivial_symbol_;  // True, if histograms for Red, Blue & Alpha
Packit 9c6abc
                             // literal symbols are single valued.
Packit 9c6abc
  double bit_cost_;          // cached value of bit cost.
Packit 9c6abc
  double literal_cost_;      // Cached values of dominant entropy costs:
Packit 9c6abc
  double red_cost_;          // literal, red & blue.
Packit 9c6abc
  double blue_cost_;
Packit 9c6abc
} VP8LHistogram;
Packit 9c6abc
Packit 9c6abc
// Collection of histograms with fixed capacity, allocated as one
Packit 9c6abc
// big memory chunk. Can be destroyed by calling WebPSafeFree().
Packit 9c6abc
typedef struct {
Packit 9c6abc
  int size;         // number of slots currently in use
Packit 9c6abc
  int max_size;     // maximum capacity
Packit 9c6abc
  VP8LHistogram** histograms;
Packit 9c6abc
} VP8LHistogramSet;
Packit 9c6abc
Packit 9c6abc
// Create the histogram.
Packit 9c6abc
//
Packit 9c6abc
// The input data is the PixOrCopy data, which models the literals, stop
Packit 9c6abc
// codes and backward references (both distances and lengths).  Also: if
Packit 9c6abc
// palette_code_bits is >= 0, initialize the histogram with this value.
Packit 9c6abc
void VP8LHistogramCreate(VP8LHistogram* const p,
Packit 9c6abc
                         const VP8LBackwardRefs* const refs,
Packit 9c6abc
                         int palette_code_bits);
Packit 9c6abc
Packit 9c6abc
// Return the size of the histogram for a given palette_code_bits.
Packit 9c6abc
int VP8LGetHistogramSize(int palette_code_bits);
Packit 9c6abc
Packit 9c6abc
// Set the palette_code_bits and reset the stats.
Packit 9c6abc
void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits);
Packit 9c6abc
Packit 9c6abc
// Collect all the references into a histogram (without reset)
Packit 9c6abc
void VP8LHistogramStoreRefs(const VP8LBackwardRefs* const refs,
Packit 9c6abc
                            VP8LHistogram* const histo);
Packit 9c6abc
Packit 9c6abc
// Free the memory allocated for the histogram.
Packit 9c6abc
void VP8LFreeHistogram(VP8LHistogram* const histo);
Packit 9c6abc
Packit 9c6abc
// Free the memory allocated for the histogram set.
Packit 9c6abc
void VP8LFreeHistogramSet(VP8LHistogramSet* const histo);
Packit 9c6abc
Packit 9c6abc
// Allocate an array of pointer to histograms, allocated and initialized
Packit 9c6abc
// using 'cache_bits'. Return NULL in case of memory error.
Packit 9c6abc
VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits);
Packit 9c6abc
Packit 9c6abc
// Allocate and initialize histogram object with specified 'cache_bits'.
Packit 9c6abc
// Returns NULL in case of memory error.
Packit 9c6abc
// Special case of VP8LAllocateHistogramSet, with size equals 1.
Packit 9c6abc
VP8LHistogram* VP8LAllocateHistogram(int cache_bits);
Packit 9c6abc
Packit 9c6abc
// Accumulate a token 'v' into a histogram.
Packit 9c6abc
void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo,
Packit 9c6abc
                                     const PixOrCopy* const v,
Packit 9c6abc
                                     int (*const distance_modifier)(int, int),
Packit 9c6abc
                                     int distance_modifier_arg0);
Packit 9c6abc
Packit 9c6abc
static WEBP_INLINE int VP8LHistogramNumCodes(int palette_code_bits) {
Packit 9c6abc
  return NUM_LITERAL_CODES + NUM_LENGTH_CODES +
Packit 9c6abc
      ((palette_code_bits > 0) ? (1 << palette_code_bits) : 0);
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
// Builds the histogram image.
Packit 9c6abc
int VP8LGetHistoImageSymbols(int xsize, int ysize,
Packit 9c6abc
                             const VP8LBackwardRefs* const refs,
Packit 9c6abc
                             int quality, int low_effort,
Packit 9c6abc
                             int histogram_bits, int cache_bits,
Packit 9c6abc
                             VP8LHistogramSet* const image_in,
Packit 9c6abc
                             VP8LHistogram* const tmp_histo,
Packit 9c6abc
                             uint16_t* const histogram_symbols);
Packit 9c6abc
Packit 9c6abc
// Returns the entropy for the symbols in the input array.
Packit 9c6abc
double VP8LBitsEntropy(const uint32_t* const array, int n);
Packit 9c6abc
Packit 9c6abc
// Estimate how many bits the combined entropy of literals and distance
Packit 9c6abc
// approximately maps to.
Packit 9c6abc
double VP8LHistogramEstimateBits(const VP8LHistogram* const p);
Packit 9c6abc
Packit 9c6abc
#ifdef __cplusplus
Packit 9c6abc
}
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
#endif  // WEBP_ENC_HISTOGRAM_ENC_H_