| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifndef WEBP_ENC_BACKWARD_REFERENCES_ENC_H_ |
| 14 |
#define WEBP_ENC_BACKWARD_REFERENCES_ENC_H_ |
| 15 |
|
| 16 |
#include <assert.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include "src/webp/types.h" |
| 19 |
#include "src/webp/encode.h" |
| 20 |
#include "src/webp/format_constants.h" |
| 21 |
|
| 22 |
#ifdef __cplusplus |
| 23 |
extern "C" { |
| 24 |
#endif |
| 25 |
|
| 26 |
|
| 27 |
#define MAX_COLOR_CACHE_BITS 10 |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
enum Mode { |
| 33 |
kLiteral, |
| 34 |
kCacheIdx, |
| 35 |
kCopy, |
| 36 |
kNone |
| 37 |
}; |
| 38 |
|
| 39 |
typedef struct { |
| 40 |
|
| 41 |
uint8_t mode; |
| 42 |
uint16_t len; |
| 43 |
uint32_t argb_or_distance; |
| 44 |
} PixOrCopy; |
| 45 |
|
| 46 |
static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t distance, |
| 47 |
uint16_t len) { |
| 48 |
PixOrCopy retval; |
| 49 |
retval.mode = kCopy; |
| 50 |
retval.argb_or_distance = distance; |
| 51 |
retval.len = len; |
| 52 |
return retval; |
| 53 |
} |
| 54 |
|
| 55 |
static WEBP_INLINE PixOrCopy PixOrCopyCreateCacheIdx(int idx) { |
| 56 |
PixOrCopy retval; |
| 57 |
assert(idx >= 0); |
| 58 |
assert(idx < (1 << MAX_COLOR_CACHE_BITS)); |
| 59 |
retval.mode = kCacheIdx; |
| 60 |
retval.argb_or_distance = idx; |
| 61 |
retval.len = 1; |
| 62 |
return retval; |
| 63 |
} |
| 64 |
|
| 65 |
static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) { |
| 66 |
PixOrCopy retval; |
| 67 |
retval.mode = kLiteral; |
| 68 |
retval.argb_or_distance = argb; |
| 69 |
retval.len = 1; |
| 70 |
return retval; |
| 71 |
} |
| 72 |
|
| 73 |
static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy* const p) { |
| 74 |
return (p->mode == kLiteral); |
| 75 |
} |
| 76 |
|
| 77 |
static WEBP_INLINE int PixOrCopyIsCacheIdx(const PixOrCopy* const p) { |
| 78 |
return (p->mode == kCacheIdx); |
| 79 |
} |
| 80 |
|
| 81 |
static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy* const p) { |
| 82 |
return (p->mode == kCopy); |
| 83 |
} |
| 84 |
|
| 85 |
static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy* const p, |
| 86 |
int component) { |
| 87 |
assert(p->mode == kLiteral); |
| 88 |
return (p->argb_or_distance >> (component * 8)) & 0xff; |
| 89 |
} |
| 90 |
|
| 91 |
static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy* const p) { |
| 92 |
return p->len; |
| 93 |
} |
| 94 |
|
| 95 |
static WEBP_INLINE uint32_t PixOrCopyCacheIdx(const PixOrCopy* const p) { |
| 96 |
assert(p->mode == kCacheIdx); |
| 97 |
assert(p->argb_or_distance < (1U << MAX_COLOR_CACHE_BITS)); |
| 98 |
return p->argb_or_distance; |
| 99 |
} |
| 100 |
|
| 101 |
static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy* const p) { |
| 102 |
assert(p->mode == kCopy); |
| 103 |
return p->argb_or_distance; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#define HASH_BITS 18 |
| 110 |
#define HASH_SIZE (1 << HASH_BITS) |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
#define MAX_LENGTH_BITS 12 |
| 115 |
#define WINDOW_SIZE_BITS 20 |
| 116 |
|
| 117 |
#define MAX_LENGTH ((1 << MAX_LENGTH_BITS) - 1) |
| 118 |
#if MAX_LENGTH_BITS + WINDOW_SIZE_BITS > 32 |
| 119 |
#error "MAX_LENGTH_BITS + WINDOW_SIZE_BITS > 32" |
| 120 |
#endif |
| 121 |
|
| 122 |
typedef struct VP8LHashChain VP8LHashChain; |
| 123 |
struct VP8LHashChain { |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
uint32_t* offset_length_; |
| 130 |
|
| 131 |
|
| 132 |
int size_; |
| 133 |
}; |
| 134 |
|
| 135 |
|
| 136 |
int VP8LHashChainInit(VP8LHashChain* const p, int size); |
| 137 |
|
| 138 |
int VP8LHashChainFill(VP8LHashChain* const p, int quality, |
| 139 |
const uint32_t* const argb, int xsize, int ysize, |
| 140 |
int low_effort, const WebPPicture* const pic, |
| 141 |
int percent_range, int* const percent); |
| 142 |
void VP8LHashChainClear(VP8LHashChain* const p); |
| 143 |
|
| 144 |
static WEBP_INLINE int VP8LHashChainFindOffset(const VP8LHashChain* const p, |
| 145 |
const int base_position) { |
| 146 |
return p->offset_length_[base_position] >> MAX_LENGTH_BITS; |
| 147 |
} |
| 148 |
|
| 149 |
static WEBP_INLINE int VP8LHashChainFindLength(const VP8LHashChain* const p, |
| 150 |
const int base_position) { |
| 151 |
return p->offset_length_[base_position] & ((1U << MAX_LENGTH_BITS) - 1); |
| 152 |
} |
| 153 |
|
| 154 |
static WEBP_INLINE void VP8LHashChainFindCopy(const VP8LHashChain* const p, |
| 155 |
int base_position, |
| 156 |
int* const offset_ptr, |
| 157 |
int* const length_ptr) { |
| 158 |
*offset_ptr = VP8LHashChainFindOffset(p, base_position); |
| 159 |
*length_ptr = VP8LHashChainFindLength(p, base_position); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
#define MAX_REFS_BLOCK_PER_IMAGE 16 |
| 167 |
|
| 168 |
typedef struct PixOrCopyBlock PixOrCopyBlock; |
| 169 |
typedef struct VP8LBackwardRefs VP8LBackwardRefs; |
| 170 |
|
| 171 |
|
| 172 |
struct VP8LBackwardRefs { |
| 173 |
int block_size_; |
| 174 |
int error_; |
| 175 |
PixOrCopyBlock* refs_; |
| 176 |
PixOrCopyBlock** tail_; |
| 177 |
PixOrCopyBlock* free_blocks_; |
| 178 |
PixOrCopyBlock* last_block_; |
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
void VP8LBackwardRefsInit(VP8LBackwardRefs* const refs, int block_size); |
| 184 |
|
| 185 |
void VP8LBackwardRefsClear(VP8LBackwardRefs* const refs); |
| 186 |
|
| 187 |
|
| 188 |
typedef struct { |
| 189 |
|
| 190 |
PixOrCopy* cur_pos; |
| 191 |
|
| 192 |
PixOrCopyBlock* cur_block_; |
| 193 |
const PixOrCopy* last_pos_; |
| 194 |
} VP8LRefsCursor; |
| 195 |
|
| 196 |
|
| 197 |
VP8LRefsCursor VP8LRefsCursorInit(const VP8LBackwardRefs* const refs); |
| 198 |
|
| 199 |
static WEBP_INLINE int VP8LRefsCursorOk(const VP8LRefsCursor* const c) { |
| 200 |
return (c->cur_pos != NULL); |
| 201 |
} |
| 202 |
|
| 203 |
void VP8LRefsCursorNextBlock(VP8LRefsCursor* const c); |
| 204 |
|
| 205 |
static WEBP_INLINE void VP8LRefsCursorNext(VP8LRefsCursor* const c) { |
| 206 |
assert(c != NULL); |
| 207 |
assert(VP8LRefsCursorOk(c)); |
| 208 |
if (++c->cur_pos == c->last_pos_) VP8LRefsCursorNextBlock(c); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
enum VP8LLZ77Type { |
| 215 |
kLZ77Standard = 1, |
| 216 |
kLZ77RLE = 2, |
| 217 |
kLZ77Box = 4 |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
int VP8LGetBackwardReferences( |
| 234 |
int width, int height, const uint32_t* const argb, int quality, |
| 235 |
int low_effort, int lz77_types_to_try, int cache_bits_max, int do_no_cache, |
| 236 |
const VP8LHashChain* const hash_chain, VP8LBackwardRefs* const refs, |
| 237 |
int* const cache_bits_best, const WebPPicture* const pic, int percent_range, |
| 238 |
int* const percent); |
| 239 |
|
| 240 |
#ifdef __cplusplus |
| 241 |
} |
| 242 |
#endif |
| 243 |
|
| 244 |
#endif |