lossless.h 12 KB

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