lossless_common.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. // Vincent Rabaud (vrabaud@google.com)
  15. #ifndef WEBP_DSP_LOSSLESS_COMMON_H_
  16. #define WEBP_DSP_LOSSLESS_COMMON_H_
  17. #include "../webp/types.h"
  18. #include "../utils/utils.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. //------------------------------------------------------------------------------
  23. // Decoding
  24. // color mapping related functions.
  25. static WEBP_INLINE uint32_t VP8GetARGBIndex(uint32_t idx) {
  26. return (idx >> 8) & 0xff;
  27. }
  28. static WEBP_INLINE uint8_t VP8GetAlphaIndex(uint8_t idx) {
  29. return idx;
  30. }
  31. static WEBP_INLINE uint32_t VP8GetARGBValue(uint32_t val) {
  32. return val;
  33. }
  34. static WEBP_INLINE uint8_t VP8GetAlphaValue(uint32_t val) {
  35. return (val >> 8) & 0xff;
  36. }
  37. //------------------------------------------------------------------------------
  38. // Misc methods.
  39. // Computes sampled size of 'size' when sampling using 'sampling bits'.
  40. static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size,
  41. uint32_t sampling_bits) {
  42. return (size + (1 << sampling_bits) - 1) >> sampling_bits;
  43. }
  44. // Converts near lossless quality into max number of bits shaved off.
  45. static WEBP_INLINE int VP8LNearLosslessBits(int near_lossless_quality) {
  46. // 100 -> 0
  47. // 80..99 -> 1
  48. // 60..79 -> 2
  49. // 40..59 -> 3
  50. // 20..39 -> 4
  51. // 0..19 -> 5
  52. return 5 - near_lossless_quality / 20;
  53. }
  54. // -----------------------------------------------------------------------------
  55. // Faster logarithm for integers. Small values use a look-up table.
  56. // The threshold till approximate version of log_2 can be used.
  57. // Practically, we can get rid of the call to log() as the two values match to
  58. // very high degree (the ratio of these two is 0.99999x).
  59. // Keeping a high threshold for now.
  60. #define APPROX_LOG_WITH_CORRECTION_MAX 65536
  61. #define APPROX_LOG_MAX 4096
  62. #define LOG_2_RECIPROCAL 1.44269504088896338700465094007086
  63. #define LOG_LOOKUP_IDX_MAX 256
  64. extern const float kLog2Table[LOG_LOOKUP_IDX_MAX];
  65. extern const float kSLog2Table[LOG_LOOKUP_IDX_MAX];
  66. typedef float (*VP8LFastLog2SlowFunc)(uint32_t v);
  67. extern VP8LFastLog2SlowFunc VP8LFastLog2Slow;
  68. extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow;
  69. static WEBP_INLINE float VP8LFastLog2(uint32_t v) {
  70. return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v);
  71. }
  72. // Fast calculation of v * log2(v) for integer input.
  73. static WEBP_INLINE float VP8LFastSLog2(uint32_t v) {
  74. return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v);
  75. }
  76. // -----------------------------------------------------------------------------
  77. // PrefixEncode()
  78. // Splitting of distance and length codes into prefixes and
  79. // extra bits. The prefixes are encoded with an entropy code
  80. // while the extra bits are stored just as normal bits.
  81. static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code,
  82. int* const extra_bits) {
  83. const int highest_bit = BitsLog2Floor(--distance);
  84. const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
  85. *extra_bits = highest_bit - 1;
  86. *code = 2 * highest_bit + second_highest_bit;
  87. }
  88. static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code,
  89. int* const extra_bits,
  90. int* const extra_bits_value) {
  91. const int highest_bit = BitsLog2Floor(--distance);
  92. const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
  93. *extra_bits = highest_bit - 1;
  94. *extra_bits_value = distance & ((1 << *extra_bits) - 1);
  95. *code = 2 * highest_bit + second_highest_bit;
  96. }
  97. #define PREFIX_LOOKUP_IDX_MAX 512
  98. typedef struct {
  99. int8_t code_;
  100. int8_t extra_bits_;
  101. } VP8LPrefixCode;
  102. // These tables are derived using VP8LPrefixEncodeNoLUT.
  103. extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX];
  104. extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX];
  105. static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code,
  106. int* const extra_bits) {
  107. if (distance < PREFIX_LOOKUP_IDX_MAX) {
  108. const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
  109. *code = prefix_code.code_;
  110. *extra_bits = prefix_code.extra_bits_;
  111. } else {
  112. VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits);
  113. }
  114. }
  115. static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code,
  116. int* const extra_bits,
  117. int* const extra_bits_value) {
  118. if (distance < PREFIX_LOOKUP_IDX_MAX) {
  119. const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
  120. *code = prefix_code.code_;
  121. *extra_bits = prefix_code.extra_bits_;
  122. *extra_bits_value = kPrefixEncodeExtraBitsValue[distance];
  123. } else {
  124. VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value);
  125. }
  126. }
  127. // Sum of each component, mod 256.
  128. static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
  129. uint32_t VP8LAddPixels(uint32_t a, uint32_t b) {
  130. const uint32_t alpha_and_green = (a & 0xff00ff00u) + (b & 0xff00ff00u);
  131. const uint32_t red_and_blue = (a & 0x00ff00ffu) + (b & 0x00ff00ffu);
  132. return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
  133. }
  134. // Difference of each component, mod 256.
  135. static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
  136. uint32_t VP8LSubPixels(uint32_t a, uint32_t b) {
  137. const uint32_t alpha_and_green =
  138. 0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u);
  139. const uint32_t red_and_blue =
  140. 0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu);
  141. return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
  142. }
  143. //------------------------------------------------------------------------------
  144. // Transform-related functions use din both encoding and decoding.
  145. // Macros used to create a batch predictor that iteratively uses a
  146. // one-pixel predictor.
  147. // The predictor is added to the output pixel (which
  148. // is therefore considered as a residual) to get the final prediction.
  149. #define GENERATE_PREDICTOR_ADD(PREDICTOR, PREDICTOR_ADD) \
  150. static void PREDICTOR_ADD(const uint32_t* in, const uint32_t* upper, \
  151. int num_pixels, uint32_t* out) { \
  152. int x; \
  153. assert(upper != NULL); \
  154. for (x = 0; x < num_pixels; ++x) { \
  155. const uint32_t pred = (PREDICTOR)(&out[x - 1], upper + x); \
  156. out[x] = VP8LAddPixels(in[x], pred); \
  157. } \
  158. }
  159. #ifdef __cplusplus
  160. } // extern "C"
  161. #endif
  162. #endif // WEBP_DSP_LOSSLESS_COMMON_H_