lossless.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // Image transforms and color space conversion methods for lossless decoder.
  11. //
  12. // Authors: Vikas Arora (vikaas.arora@gmail.com)
  13. // Jyrki Alakuijala (jyrki@google.com)
  14. #ifndef WEBP_DSP_LOSSLESS_H_
  15. #define WEBP_DSP_LOSSLESS_H_
  16. #include "../webp/types.h"
  17. #include "../webp/decode.h"
  18. #include "../enc/histogram_enc.h"
  19. #include "../utils/utils.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #ifdef WEBP_EXPERIMENTAL_FEATURES
  24. #include "../enc/delta_palettization_enc.h"
  25. #endif // WEBP_EXPERIMENTAL_FEATURES
  26. //------------------------------------------------------------------------------
  27. // Decoding
  28. typedef uint32_t (*VP8LPredictorFunc)(uint32_t left, const uint32_t* const top);
  29. extern VP8LPredictorFunc VP8LPredictors[16];
  30. extern VP8LPredictorFunc VP8LPredictors_C[16];
  31. // These Add/Sub function expects upper[-1] and out[-1] to be readable.
  32. typedef void (*VP8LPredictorAddSubFunc)(const uint32_t* in,
  33. const uint32_t* upper, int num_pixels,
  34. uint32_t* out);
  35. extern VP8LPredictorAddSubFunc VP8LPredictorsAdd[16];
  36. extern VP8LPredictorAddSubFunc VP8LPredictorsAdd_C[16];
  37. typedef void (*VP8LProcessDecBlueAndRedFunc)(const uint32_t* src,
  38. int num_pixels, uint32_t* dst);
  39. extern VP8LProcessDecBlueAndRedFunc VP8LAddGreenToBlueAndRed;
  40. typedef struct {
  41. // Note: the members are uint8_t, so that any negative values are
  42. // automatically converted to "mod 256" values.
  43. uint8_t green_to_red_;
  44. uint8_t green_to_blue_;
  45. uint8_t red_to_blue_;
  46. } VP8LMultipliers;
  47. typedef void (*VP8LTransformColorInverseFunc)(const VP8LMultipliers* const m,
  48. const uint32_t* src,
  49. int num_pixels, uint32_t* dst);
  50. extern VP8LTransformColorInverseFunc VP8LTransformColorInverse;
  51. struct VP8LTransform; // Defined in dec/vp8li.h.
  52. // Performs inverse transform of data given transform information, start and end
  53. // rows. Transform will be applied to rows [row_start, row_end[.
  54. // The *in and *out pointers refer to source and destination data respectively
  55. // corresponding to the intermediate row (row_start).
  56. void VP8LInverseTransform(const struct VP8LTransform* const transform,
  57. int row_start, int row_end,
  58. const uint32_t* const in, uint32_t* const out);
  59. // Color space conversion.
  60. typedef void (*VP8LConvertFunc)(const uint32_t* src, int num_pixels,
  61. uint8_t* dst);
  62. extern VP8LConvertFunc VP8LConvertBGRAToRGB;
  63. extern VP8LConvertFunc VP8LConvertBGRAToRGBA;
  64. extern VP8LConvertFunc VP8LConvertBGRAToRGBA4444;
  65. extern VP8LConvertFunc VP8LConvertBGRAToRGB565;
  66. extern VP8LConvertFunc VP8LConvertBGRAToBGR;
  67. // Converts from BGRA to other color spaces.
  68. void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels,
  69. WEBP_CSP_MODE out_colorspace, uint8_t* const rgba);
  70. typedef void (*VP8LMapARGBFunc)(const uint32_t* src,
  71. const uint32_t* const color_map,
  72. uint32_t* dst, int y_start,
  73. int y_end, int width);
  74. typedef void (*VP8LMapAlphaFunc)(const uint8_t* src,
  75. const uint32_t* const color_map,
  76. uint8_t* dst, int y_start,
  77. int y_end, int width);
  78. extern VP8LMapARGBFunc VP8LMapColor32b;
  79. extern VP8LMapAlphaFunc VP8LMapColor8b;
  80. // Similar to the static method ColorIndexInverseTransform() that is part of
  81. // lossless.c, but used only for alpha decoding. It takes uint8_t (rather than
  82. // uint32_t) arguments for 'src' and 'dst'.
  83. void VP8LColorIndexInverseTransformAlpha(
  84. const struct VP8LTransform* const transform, int y_start, int y_end,
  85. const uint8_t* src, uint8_t* dst);
  86. // Expose some C-only fallback functions
  87. void VP8LTransformColorInverse_C(const VP8LMultipliers* const m,
  88. const uint32_t* src, int num_pixels,
  89. uint32_t* dst);
  90. void VP8LConvertBGRAToRGB_C(const uint32_t* src, int num_pixels, uint8_t* dst);
  91. void VP8LConvertBGRAToRGBA_C(const uint32_t* src, int num_pixels, uint8_t* dst);
  92. void VP8LConvertBGRAToRGBA4444_C(const uint32_t* src,
  93. int num_pixels, uint8_t* dst);
  94. void VP8LConvertBGRAToRGB565_C(const uint32_t* src,
  95. int num_pixels, uint8_t* dst);
  96. void VP8LConvertBGRAToBGR_C(const uint32_t* src, int num_pixels, uint8_t* dst);
  97. void VP8LAddGreenToBlueAndRed_C(const uint32_t* src, int num_pixels,
  98. uint32_t* dst);
  99. // Must be called before calling any of the above methods.
  100. void VP8LDspInit(void);
  101. //------------------------------------------------------------------------------
  102. // Encoding
  103. typedef void (*VP8LProcessEncBlueAndRedFunc)(uint32_t* dst, int num_pixels);
  104. extern VP8LProcessEncBlueAndRedFunc VP8LSubtractGreenFromBlueAndRed;
  105. typedef void (*VP8LTransformColorFunc)(const VP8LMultipliers* const m,
  106. uint32_t* const dst, int num_pixels);
  107. extern VP8LTransformColorFunc VP8LTransformColor;
  108. typedef void (*VP8LCollectColorBlueTransformsFunc)(
  109. const uint32_t* argb, int stride,
  110. int tile_width, int tile_height,
  111. int green_to_blue, int red_to_blue, int histo[]);
  112. extern VP8LCollectColorBlueTransformsFunc VP8LCollectColorBlueTransforms;
  113. typedef void (*VP8LCollectColorRedTransformsFunc)(
  114. const uint32_t* argb, int stride,
  115. int tile_width, int tile_height,
  116. int green_to_red, int histo[]);
  117. extern VP8LCollectColorRedTransformsFunc VP8LCollectColorRedTransforms;
  118. // Expose some C-only fallback functions
  119. void VP8LTransformColor_C(const VP8LMultipliers* const m,
  120. uint32_t* data, int num_pixels);
  121. void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data, int num_pixels);
  122. void VP8LCollectColorRedTransforms_C(const uint32_t* argb, int stride,
  123. int tile_width, int tile_height,
  124. int green_to_red, int histo[]);
  125. void VP8LCollectColorBlueTransforms_C(const uint32_t* argb, int stride,
  126. int tile_width, int tile_height,
  127. int green_to_blue, int red_to_blue,
  128. int histo[]);
  129. extern VP8LPredictorAddSubFunc VP8LPredictorsSub[16];
  130. extern VP8LPredictorAddSubFunc VP8LPredictorsSub_C[16];
  131. // -----------------------------------------------------------------------------
  132. // Huffman-cost related functions.
  133. typedef double (*VP8LCostFunc)(const uint32_t* population, int length);
  134. typedef double (*VP8LCostCombinedFunc)(const uint32_t* X, const uint32_t* Y,
  135. int length);
  136. typedef float (*VP8LCombinedShannonEntropyFunc)(const int X[256],
  137. const int Y[256]);
  138. extern VP8LCostFunc VP8LExtraCost;
  139. extern VP8LCostCombinedFunc VP8LExtraCostCombined;
  140. extern VP8LCombinedShannonEntropyFunc VP8LCombinedShannonEntropy;
  141. typedef struct { // small struct to hold counters
  142. int counts[2]; // index: 0=zero steak, 1=non-zero streak
  143. int streaks[2][2]; // [zero/non-zero][streak<3 / streak>=3]
  144. } VP8LStreaks;
  145. typedef struct { // small struct to hold bit entropy results
  146. double entropy; // entropy
  147. uint32_t sum; // sum of the population
  148. int nonzeros; // number of non-zero elements in the population
  149. uint32_t max_val; // maximum value in the population
  150. uint32_t nonzero_code; // index of the last non-zero in the population
  151. } VP8LBitEntropy;
  152. void VP8LBitEntropyInit(VP8LBitEntropy* const entropy);
  153. // Get the combined symbol bit entropy and Huffman cost stats for the
  154. // distributions 'X' and 'Y'. Those results can then be refined according to
  155. // codec specific heuristics.
  156. typedef void (*VP8LGetCombinedEntropyUnrefinedFunc)(
  157. const uint32_t X[], const uint32_t Y[], int length,
  158. VP8LBitEntropy* const bit_entropy, VP8LStreaks* const stats);
  159. extern VP8LGetCombinedEntropyUnrefinedFunc VP8LGetCombinedEntropyUnrefined;
  160. // Get the entropy for the distribution 'X'.
  161. typedef void (*VP8LGetEntropyUnrefinedFunc)(const uint32_t X[], int length,
  162. VP8LBitEntropy* const bit_entropy,
  163. VP8LStreaks* const stats);
  164. extern VP8LGetEntropyUnrefinedFunc VP8LGetEntropyUnrefined;
  165. void VP8LBitsEntropyUnrefined(const uint32_t* const array, int n,
  166. VP8LBitEntropy* const entropy);
  167. typedef void (*VP8LHistogramAddFunc)(const VP8LHistogram* const a,
  168. const VP8LHistogram* const b,
  169. VP8LHistogram* const out);
  170. extern VP8LHistogramAddFunc VP8LHistogramAdd;
  171. // -----------------------------------------------------------------------------
  172. // PrefixEncode()
  173. typedef int (*VP8LVectorMismatchFunc)(const uint32_t* const array1,
  174. const uint32_t* const array2, int length);
  175. // Returns the first index where array1 and array2 are different.
  176. extern VP8LVectorMismatchFunc VP8LVectorMismatch;
  177. typedef void (*VP8LBundleColorMapFunc)(const uint8_t* const row, int width,
  178. int xbits, uint32_t* dst);
  179. extern VP8LBundleColorMapFunc VP8LBundleColorMap;
  180. void VP8LBundleColorMap_C(const uint8_t* const row, int width, int xbits,
  181. uint32_t* dst);
  182. // Must be called before calling any of the above methods.
  183. void VP8LEncDspInit(void);
  184. //------------------------------------------------------------------------------
  185. #ifdef __cplusplus
  186. } // extern "C"
  187. #endif
  188. #endif // WEBP_DSP_LOSSLESS_H_