xref: /aosp_15_r20/external/webp/src/enc/histogram_enc.h (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2012 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li // Author: Jyrki Alakuijala ([email protected])
11*b2055c35SXin Li //
12*b2055c35SXin Li // Models the histograms of literal and distance codes.
13*b2055c35SXin Li 
14*b2055c35SXin Li #ifndef WEBP_ENC_HISTOGRAM_ENC_H_
15*b2055c35SXin Li #define WEBP_ENC_HISTOGRAM_ENC_H_
16*b2055c35SXin Li 
17*b2055c35SXin Li #include <string.h>
18*b2055c35SXin Li 
19*b2055c35SXin Li #include "src/enc/backward_references_enc.h"
20*b2055c35SXin Li #include "src/webp/format_constants.h"
21*b2055c35SXin Li #include "src/webp/types.h"
22*b2055c35SXin Li 
23*b2055c35SXin Li #ifdef __cplusplus
24*b2055c35SXin Li extern "C" {
25*b2055c35SXin Li #endif
26*b2055c35SXin Li 
27*b2055c35SXin Li // Not a trivial literal symbol.
28*b2055c35SXin Li #define VP8L_NON_TRIVIAL_SYM (0xffffffff)
29*b2055c35SXin Li 
30*b2055c35SXin Li // A simple container for histograms of data.
31*b2055c35SXin Li typedef struct {
32*b2055c35SXin Li   // literal_ contains green literal, palette-code and
33*b2055c35SXin Li   // copy-length-prefix histogram
34*b2055c35SXin Li   uint32_t* literal_;         // Pointer to the allocated buffer for literal.
35*b2055c35SXin Li   uint32_t red_[NUM_LITERAL_CODES];
36*b2055c35SXin Li   uint32_t blue_[NUM_LITERAL_CODES];
37*b2055c35SXin Li   uint32_t alpha_[NUM_LITERAL_CODES];
38*b2055c35SXin Li   // Backward reference prefix-code histogram.
39*b2055c35SXin Li   uint32_t distance_[NUM_DISTANCE_CODES];
40*b2055c35SXin Li   int palette_code_bits_;
41*b2055c35SXin Li   uint32_t trivial_symbol_;  // True, if histograms for Red, Blue & Alpha
42*b2055c35SXin Li                              // literal symbols are single valued.
43*b2055c35SXin Li   float bit_cost_;           // cached value of bit cost.
44*b2055c35SXin Li   float literal_cost_;       // Cached values of dominant entropy costs:
45*b2055c35SXin Li   float red_cost_;           // literal, red & blue.
46*b2055c35SXin Li   float blue_cost_;
47*b2055c35SXin Li   uint8_t is_used_[5];       // 5 for literal, red, blue, alpha, distance
48*b2055c35SXin Li } VP8LHistogram;
49*b2055c35SXin Li 
50*b2055c35SXin Li // Collection of histograms with fixed capacity, allocated as one
51*b2055c35SXin Li // big memory chunk. Can be destroyed by calling WebPSafeFree().
52*b2055c35SXin Li typedef struct {
53*b2055c35SXin Li   int size;         // number of slots currently in use
54*b2055c35SXin Li   int max_size;     // maximum capacity
55*b2055c35SXin Li   VP8LHistogram** histograms;
56*b2055c35SXin Li } VP8LHistogramSet;
57*b2055c35SXin Li 
58*b2055c35SXin Li // Create the histogram.
59*b2055c35SXin Li //
60*b2055c35SXin Li // The input data is the PixOrCopy data, which models the literals, stop
61*b2055c35SXin Li // codes and backward references (both distances and lengths).  Also: if
62*b2055c35SXin Li // palette_code_bits is >= 0, initialize the histogram with this value.
63*b2055c35SXin Li void VP8LHistogramCreate(VP8LHistogram* const p,
64*b2055c35SXin Li                          const VP8LBackwardRefs* const refs,
65*b2055c35SXin Li                          int palette_code_bits);
66*b2055c35SXin Li 
67*b2055c35SXin Li // Return the size of the histogram for a given cache_bits.
68*b2055c35SXin Li int VP8LGetHistogramSize(int cache_bits);
69*b2055c35SXin Li 
70*b2055c35SXin Li // Set the palette_code_bits and reset the stats.
71*b2055c35SXin Li // If init_arrays is true, the arrays are also filled with 0's.
72*b2055c35SXin Li void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits,
73*b2055c35SXin Li                        int init_arrays);
74*b2055c35SXin Li 
75*b2055c35SXin Li // Collect all the references into a histogram (without reset)
76*b2055c35SXin Li void VP8LHistogramStoreRefs(const VP8LBackwardRefs* const refs,
77*b2055c35SXin Li                             VP8LHistogram* const histo);
78*b2055c35SXin Li 
79*b2055c35SXin Li // Free the memory allocated for the histogram.
80*b2055c35SXin Li void VP8LFreeHistogram(VP8LHistogram* const histo);
81*b2055c35SXin Li 
82*b2055c35SXin Li // Free the memory allocated for the histogram set.
83*b2055c35SXin Li void VP8LFreeHistogramSet(VP8LHistogramSet* const histo);
84*b2055c35SXin Li 
85*b2055c35SXin Li // Allocate an array of pointer to histograms, allocated and initialized
86*b2055c35SXin Li // using 'cache_bits'. Return NULL in case of memory error.
87*b2055c35SXin Li VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits);
88*b2055c35SXin Li 
89*b2055c35SXin Li // Set the histograms in set to 0.
90*b2055c35SXin Li void VP8LHistogramSetClear(VP8LHistogramSet* const set);
91*b2055c35SXin Li 
92*b2055c35SXin Li // Allocate and initialize histogram object with specified 'cache_bits'.
93*b2055c35SXin Li // Returns NULL in case of memory error.
94*b2055c35SXin Li // Special case of VP8LAllocateHistogramSet, with size equals 1.
95*b2055c35SXin Li VP8LHistogram* VP8LAllocateHistogram(int cache_bits);
96*b2055c35SXin Li 
97*b2055c35SXin Li // Accumulate a token 'v' into a histogram.
98*b2055c35SXin Li void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo,
99*b2055c35SXin Li                                      const PixOrCopy* const v,
100*b2055c35SXin Li                                      int (*const distance_modifier)(int, int),
101*b2055c35SXin Li                                      int distance_modifier_arg0);
102*b2055c35SXin Li 
VP8LHistogramNumCodes(int palette_code_bits)103*b2055c35SXin Li static WEBP_INLINE int VP8LHistogramNumCodes(int palette_code_bits) {
104*b2055c35SXin Li   return NUM_LITERAL_CODES + NUM_LENGTH_CODES +
105*b2055c35SXin Li       ((palette_code_bits > 0) ? (1 << palette_code_bits) : 0);
106*b2055c35SXin Li }
107*b2055c35SXin Li 
108*b2055c35SXin Li // Builds the histogram image. pic and percent are for progress.
109*b2055c35SXin Li // Returns false in case of error (stored in pic->error_code).
110*b2055c35SXin Li int VP8LGetHistoImageSymbols(int xsize, int ysize,
111*b2055c35SXin Li                              const VP8LBackwardRefs* const refs, int quality,
112*b2055c35SXin Li                              int low_effort, int histogram_bits, int cache_bits,
113*b2055c35SXin Li                              VP8LHistogramSet* const image_histo,
114*b2055c35SXin Li                              VP8LHistogram* const tmp_histo,
115*b2055c35SXin Li                              uint16_t* const histogram_symbols,
116*b2055c35SXin Li                              const WebPPicture* const pic, int percent_range,
117*b2055c35SXin Li                              int* const percent);
118*b2055c35SXin Li 
119*b2055c35SXin Li // Returns the entropy for the symbols in the input array.
120*b2055c35SXin Li float VP8LBitsEntropy(const uint32_t* const array, int n);
121*b2055c35SXin Li 
122*b2055c35SXin Li // Estimate how many bits the combined entropy of literals and distance
123*b2055c35SXin Li // approximately maps to.
124*b2055c35SXin Li float VP8LHistogramEstimateBits(VP8LHistogram* const p);
125*b2055c35SXin Li 
126*b2055c35SXin Li #ifdef __cplusplus
127*b2055c35SXin Li }
128*b2055c35SXin Li #endif
129*b2055c35SXin Li 
130*b2055c35SXin Li #endif  // WEBP_ENC_HISTOGRAM_ENC_H_
131