backward_references_enc.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // Author: Jyrki Alakuijala (jyrki@google.com)
  11. //
  12. #ifndef WEBP_ENC_BACKWARD_REFERENCES_H_
  13. #define WEBP_ENC_BACKWARD_REFERENCES_H_
  14. #include <assert.h>
  15. #include <stdlib.h>
  16. #include "../webp/types.h"
  17. #include "../webp/format_constants.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. // The maximum allowed limit is 11.
  22. #define MAX_COLOR_CACHE_BITS 10
  23. // -----------------------------------------------------------------------------
  24. // PixOrCopy
  25. enum Mode {
  26. kLiteral,
  27. kCacheIdx,
  28. kCopy,
  29. kNone
  30. };
  31. typedef struct {
  32. // mode as uint8_t to make the memory layout to be exactly 8 bytes.
  33. uint8_t mode;
  34. uint16_t len;
  35. uint32_t argb_or_distance;
  36. } PixOrCopy;
  37. static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t distance,
  38. uint16_t len) {
  39. PixOrCopy retval;
  40. retval.mode = kCopy;
  41. retval.argb_or_distance = distance;
  42. retval.len = len;
  43. return retval;
  44. }
  45. static WEBP_INLINE PixOrCopy PixOrCopyCreateCacheIdx(int idx) {
  46. PixOrCopy retval;
  47. assert(idx >= 0);
  48. assert(idx < (1 << MAX_COLOR_CACHE_BITS));
  49. retval.mode = kCacheIdx;
  50. retval.argb_or_distance = idx;
  51. retval.len = 1;
  52. return retval;
  53. }
  54. static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) {
  55. PixOrCopy retval;
  56. retval.mode = kLiteral;
  57. retval.argb_or_distance = argb;
  58. retval.len = 1;
  59. return retval;
  60. }
  61. static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy* const p) {
  62. return (p->mode == kLiteral);
  63. }
  64. static WEBP_INLINE int PixOrCopyIsCacheIdx(const PixOrCopy* const p) {
  65. return (p->mode == kCacheIdx);
  66. }
  67. static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy* const p) {
  68. return (p->mode == kCopy);
  69. }
  70. static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy* const p,
  71. int component) {
  72. assert(p->mode == kLiteral);
  73. return (p->argb_or_distance >> (component * 8)) & 0xff;
  74. }
  75. static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy* const p) {
  76. return p->len;
  77. }
  78. static WEBP_INLINE uint32_t PixOrCopyArgb(const PixOrCopy* const p) {
  79. assert(p->mode == kLiteral);
  80. return p->argb_or_distance;
  81. }
  82. static WEBP_INLINE uint32_t PixOrCopyCacheIdx(const PixOrCopy* const p) {
  83. assert(p->mode == kCacheIdx);
  84. assert(p->argb_or_distance < (1U << MAX_COLOR_CACHE_BITS));
  85. return p->argb_or_distance;
  86. }
  87. static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy* const p) {
  88. assert(p->mode == kCopy);
  89. return p->argb_or_distance;
  90. }
  91. // -----------------------------------------------------------------------------
  92. // VP8LHashChain
  93. #define HASH_BITS 18
  94. #define HASH_SIZE (1 << HASH_BITS)
  95. typedef struct VP8LHashChain VP8LHashChain;
  96. struct VP8LHashChain {
  97. // The 20 most significant bits contain the offset at which the best match
  98. // is found. These 20 bits are the limit defined by GetWindowSizeForHashChain
  99. // (through WINDOW_SIZE = 1<<20).
  100. // The lower 12 bits contain the length of the match. The 12 bit limit is
  101. // defined in MaxFindCopyLength with MAX_LENGTH=4096.
  102. uint32_t* offset_length_;
  103. // This is the maximum size of the hash_chain that can be constructed.
  104. // Typically this is the pixel count (width x height) for a given image.
  105. int size_;
  106. };
  107. // Must be called first, to set size.
  108. int VP8LHashChainInit(VP8LHashChain* const p, int size);
  109. // Pre-compute the best matches for argb.
  110. int VP8LHashChainFill(VP8LHashChain* const p, int quality,
  111. const uint32_t* const argb, int xsize, int ysize,
  112. int low_effort);
  113. void VP8LHashChainClear(VP8LHashChain* const p); // release memory
  114. // -----------------------------------------------------------------------------
  115. // VP8LBackwardRefs (block-based backward-references storage)
  116. // maximum number of reference blocks the image will be segmented into
  117. #define MAX_REFS_BLOCK_PER_IMAGE 16
  118. typedef struct PixOrCopyBlock PixOrCopyBlock; // forward declaration
  119. typedef struct VP8LBackwardRefs VP8LBackwardRefs;
  120. // Container for blocks chain
  121. struct VP8LBackwardRefs {
  122. int block_size_; // common block-size
  123. int error_; // set to true if some memory error occurred
  124. PixOrCopyBlock* refs_; // list of currently used blocks
  125. PixOrCopyBlock** tail_; // for list recycling
  126. PixOrCopyBlock* free_blocks_; // free-list
  127. PixOrCopyBlock* last_block_; // used for adding new refs (internal)
  128. };
  129. // Initialize the object. 'block_size' is the common block size to store
  130. // references (typically, width * height / MAX_REFS_BLOCK_PER_IMAGE).
  131. void VP8LBackwardRefsInit(VP8LBackwardRefs* const refs, int block_size);
  132. // Release memory for backward references.
  133. void VP8LBackwardRefsClear(VP8LBackwardRefs* const refs);
  134. // Copies the 'src' backward refs to the 'dst'. Returns 0 in case of error.
  135. int VP8LBackwardRefsCopy(const VP8LBackwardRefs* const src,
  136. VP8LBackwardRefs* const dst);
  137. // Cursor for iterating on references content
  138. typedef struct {
  139. // public:
  140. PixOrCopy* cur_pos; // current position
  141. // private:
  142. PixOrCopyBlock* cur_block_; // current block in the refs list
  143. const PixOrCopy* last_pos_; // sentinel for switching to next block
  144. } VP8LRefsCursor;
  145. // Returns a cursor positioned at the beginning of the references list.
  146. VP8LRefsCursor VP8LRefsCursorInit(const VP8LBackwardRefs* const refs);
  147. // Returns true if cursor is pointing at a valid position.
  148. static WEBP_INLINE int VP8LRefsCursorOk(const VP8LRefsCursor* const c) {
  149. return (c->cur_pos != NULL);
  150. }
  151. // Move to next block of references. Internal, not to be called directly.
  152. void VP8LRefsCursorNextBlock(VP8LRefsCursor* const c);
  153. // Move to next position, or NULL. Should not be called if !VP8LRefsCursorOk().
  154. static WEBP_INLINE void VP8LRefsCursorNext(VP8LRefsCursor* const c) {
  155. assert(c != NULL);
  156. assert(VP8LRefsCursorOk(c));
  157. if (++c->cur_pos == c->last_pos_) VP8LRefsCursorNextBlock(c);
  158. }
  159. // -----------------------------------------------------------------------------
  160. // Main entry points
  161. // Evaluates best possible backward references for specified quality.
  162. // The input cache_bits to 'VP8LGetBackwardReferences' sets the maximum cache
  163. // bits to use (passing 0 implies disabling the local color cache).
  164. // The optimal cache bits is evaluated and set for the *cache_bits parameter.
  165. // The return value is the pointer to the best of the two backward refs viz,
  166. // refs[0] or refs[1].
  167. VP8LBackwardRefs* VP8LGetBackwardReferences(
  168. int width, int height, const uint32_t* const argb, int quality,
  169. int low_effort, int* const cache_bits,
  170. const VP8LHashChain* const hash_chain, VP8LBackwardRefs refs[2]);
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174. #endif // WEBP_ENC_BACKWARD_REFERENCES_H_