lossless.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.h"
  19. #include "../utils/utils.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. //------------------------------------------------------------------------------
  24. // Signatures and generic function-pointers
  25. typedef uint32_t (*VP8LPredictorFunc)(uint32_t left, const uint32_t* const top);
  26. extern VP8LPredictorFunc VP8LPredictors[16];
  27. typedef void (*VP8LProcessBlueAndRedFunc)(uint32_t* argb_data, int num_pixels);
  28. extern VP8LProcessBlueAndRedFunc VP8LSubtractGreenFromBlueAndRed;
  29. extern VP8LProcessBlueAndRedFunc VP8LAddGreenToBlueAndRed;
  30. typedef struct {
  31. // Note: the members are uint8_t, so that any negative values are
  32. // automatically converted to "mod 256" values.
  33. uint8_t green_to_red_;
  34. uint8_t green_to_blue_;
  35. uint8_t red_to_blue_;
  36. } VP8LMultipliers;
  37. typedef void (*VP8LTransformColorFunc)(const VP8LMultipliers* const m,
  38. uint32_t* argb_data, int num_pixels);
  39. extern VP8LTransformColorFunc VP8LTransformColor;
  40. extern VP8LTransformColorFunc VP8LTransformColorInverse;
  41. typedef void (*VP8LConvertFunc)(const uint32_t* src, int num_pixels,
  42. uint8_t* dst);
  43. extern VP8LConvertFunc VP8LConvertBGRAToRGB;
  44. extern VP8LConvertFunc VP8LConvertBGRAToRGBA;
  45. extern VP8LConvertFunc VP8LConvertBGRAToRGBA4444;
  46. extern VP8LConvertFunc VP8LConvertBGRAToRGB565;
  47. extern VP8LConvertFunc VP8LConvertBGRAToBGR;
  48. // Expose some C-only fallback functions
  49. void VP8LTransformColor_C(const VP8LMultipliers* const m,
  50. uint32_t* data, int num_pixels);
  51. void VP8LTransformColorInverse_C(const VP8LMultipliers* const m,
  52. uint32_t* data, int num_pixels);
  53. void VP8LConvertBGRAToRGB_C(const uint32_t* src, int num_pixels, uint8_t* dst);
  54. void VP8LConvertBGRAToRGBA_C(const uint32_t* src, int num_pixels, uint8_t* dst);
  55. void VP8LConvertBGRAToRGBA4444_C(const uint32_t* src,
  56. int num_pixels, uint8_t* dst);
  57. void VP8LConvertBGRAToRGB565_C(const uint32_t* src,
  58. int num_pixels, uint8_t* dst);
  59. void VP8LConvertBGRAToBGR_C(const uint32_t* src, int num_pixels, uint8_t* dst);
  60. void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data, int num_pixels);
  61. void VP8LAddGreenToBlueAndRed_C(uint32_t* data, int num_pixels);
  62. // Must be called before calling any of the above methods.
  63. void VP8LDspInit(void);
  64. //------------------------------------------------------------------------------
  65. // Image transforms.
  66. struct VP8LTransform; // Defined in dec/vp8li.h.
  67. // Performs inverse transform of data given transform information, start and end
  68. // rows. Transform will be applied to rows [row_start, row_end[.
  69. // The *in and *out pointers refer to source and destination data respectively
  70. // corresponding to the intermediate row (row_start).
  71. void VP8LInverseTransform(const struct VP8LTransform* const transform,
  72. int row_start, int row_end,
  73. const uint32_t* const in, uint32_t* const out);
  74. // Similar to the static method ColorIndexInverseTransform() that is part of
  75. // lossless.c, but used only for alpha decoding. It takes uint8_t (rather than
  76. // uint32_t) arguments for 'src' and 'dst'.
  77. void VP8LColorIndexInverseTransformAlpha(
  78. const struct VP8LTransform* const transform, int y_start, int y_end,
  79. const uint8_t* src, uint8_t* dst);
  80. void VP8LResidualImage(int width, int height, int bits,
  81. uint32_t* const argb, uint32_t* const argb_scratch,
  82. uint32_t* const image);
  83. void VP8LColorSpaceTransform(int width, int height, int bits, int quality,
  84. uint32_t* const argb, uint32_t* image);
  85. //------------------------------------------------------------------------------
  86. // Color space conversion.
  87. // Converts from BGRA to other color spaces.
  88. void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels,
  89. WEBP_CSP_MODE out_colorspace, uint8_t* const rgba);
  90. //------------------------------------------------------------------------------
  91. // Misc methods.
  92. // Computes sampled size of 'size' when sampling using 'sampling bits'.
  93. static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size,
  94. uint32_t sampling_bits) {
  95. return (size + (1 << sampling_bits) - 1) >> sampling_bits;
  96. }
  97. // -----------------------------------------------------------------------------
  98. // Faster logarithm for integers. Small values use a look-up table.
  99. #define LOG_LOOKUP_IDX_MAX 256
  100. extern const float kLog2Table[LOG_LOOKUP_IDX_MAX];
  101. extern const float kSLog2Table[LOG_LOOKUP_IDX_MAX];
  102. typedef float (*VP8LFastLog2SlowFunc)(uint32_t v);
  103. extern VP8LFastLog2SlowFunc VP8LFastLog2Slow;
  104. extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow;
  105. static WEBP_INLINE float VP8LFastLog2(uint32_t v) {
  106. return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v);
  107. }
  108. // Fast calculation of v * log2(v) for integer input.
  109. static WEBP_INLINE float VP8LFastSLog2(uint32_t v) {
  110. return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v);
  111. }
  112. // -----------------------------------------------------------------------------
  113. // Huffman-cost related functions.
  114. typedef double (*VP8LCostFunc)(const uint32_t* population, int length);
  115. typedef double (*VP8LCostCombinedFunc)(const uint32_t* X, const uint32_t* Y,
  116. int length);
  117. extern VP8LCostFunc VP8LExtraCost;
  118. extern VP8LCostCombinedFunc VP8LExtraCostCombined;
  119. typedef struct { // small struct to hold counters
  120. int counts[2]; // index: 0=zero steak, 1=non-zero streak
  121. int streaks[2][2]; // [zero/non-zero][streak<3 / streak>=3]
  122. } VP8LStreaks;
  123. typedef VP8LStreaks (*VP8LCostCountFunc)(const uint32_t* population,
  124. int length);
  125. typedef VP8LStreaks (*VP8LCostCombinedCountFunc)(const uint32_t* X,
  126. const uint32_t* Y, int length);
  127. extern VP8LCostCountFunc VP8LHuffmanCostCount;
  128. extern VP8LCostCombinedCountFunc VP8LHuffmanCostCombinedCount;
  129. typedef void (*VP8LHistogramAddFunc)(const VP8LHistogram* const a,
  130. const VP8LHistogram* const b,
  131. VP8LHistogram* const out);
  132. extern VP8LHistogramAddFunc VP8LHistogramAdd;
  133. // -----------------------------------------------------------------------------
  134. // PrefixEncode()
  135. static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) {
  136. const int log_floor = BitsLog2Floor(n);
  137. if (n == (n & ~(n - 1))) // zero or a power of two.
  138. return log_floor;
  139. else
  140. return log_floor + 1;
  141. }
  142. // Splitting of distance and length codes into prefixes and
  143. // extra bits. The prefixes are encoded with an entropy code
  144. // while the extra bits are stored just as normal bits.
  145. static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code,
  146. int* const extra_bits) {
  147. const int highest_bit = BitsLog2Floor(--distance);
  148. const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
  149. *extra_bits = highest_bit - 1;
  150. *code = 2 * highest_bit + second_highest_bit;
  151. }
  152. static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code,
  153. int* const extra_bits,
  154. int* const extra_bits_value) {
  155. const int highest_bit = BitsLog2Floor(--distance);
  156. const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
  157. *extra_bits = highest_bit - 1;
  158. *extra_bits_value = distance & ((1 << *extra_bits) - 1);
  159. *code = 2 * highest_bit + second_highest_bit;
  160. }
  161. #define PREFIX_LOOKUP_IDX_MAX 512
  162. typedef struct {
  163. int8_t code_;
  164. int8_t extra_bits_;
  165. } VP8LPrefixCode;
  166. // These tables are derived using VP8LPrefixEncodeNoLUT.
  167. extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX];
  168. extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX];
  169. static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code,
  170. int* const extra_bits) {
  171. if (distance < PREFIX_LOOKUP_IDX_MAX) {
  172. const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
  173. *code = prefix_code.code_;
  174. *extra_bits = prefix_code.extra_bits_;
  175. } else {
  176. VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits);
  177. }
  178. }
  179. static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code,
  180. int* const extra_bits,
  181. int* const extra_bits_value) {
  182. if (distance < PREFIX_LOOKUP_IDX_MAX) {
  183. const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
  184. *code = prefix_code.code_;
  185. *extra_bits = prefix_code.extra_bits_;
  186. *extra_bits_value = kPrefixEncodeExtraBitsValue[distance];
  187. } else {
  188. VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value);
  189. }
  190. }
  191. // In-place difference of each component with mod 256.
  192. static WEBP_INLINE uint32_t VP8LSubPixels(uint32_t a, uint32_t b) {
  193. const uint32_t alpha_and_green =
  194. 0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u);
  195. const uint32_t red_and_blue =
  196. 0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu);
  197. return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
  198. }
  199. void VP8LBundleColorMap(const uint8_t* const row, int width,
  200. int xbits, uint32_t* const dst);
  201. //------------------------------------------------------------------------------
  202. #ifdef __cplusplus
  203. } // extern "C"
  204. #endif
  205. #endif // WEBP_DSP_LOSSLESS_H_