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