histogram_enc.h 4.9 KB

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