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
13#ifndef WEBP_ENC_BACKWARD_REFERENCES_H_
14#define WEBP_ENC_BACKWARD_REFERENCES_H_
15
16#include <assert.h>
17#include <stdlib.h>
18#include "../webp/types.h"
19#include "../webp/format_constants.h"
20
21#if defined(__cplusplus) || defined(c_plusplus)
22extern "C" {
23#endif
24
25// The spec allows 11, we use 9 bits to reduce memory consumption in encoding.
26// Having 9 instead of 11 only removes about 0.25 % of compression density.
27#define MAX_COLOR_CACHE_BITS 9
28
29// Max ever number of codes we'll use:
30#define PIX_OR_COPY_CODES_MAX \
31    (NUM_LITERAL_CODES + NUM_LENGTH_CODES + (1 << MAX_COLOR_CACHE_BITS))
32
33// -----------------------------------------------------------------------------
34// PrefixEncode()
35
36// use GNU builtins where available.
37#if defined(__GNUC__) && \
38    ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
39static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
40  assert(n != 0);
41  return 31 ^ __builtin_clz(n);
42}
43#elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
44#include <intrin.h>
45#pragma intrinsic(_BitScanReverse)
46
47static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
48  unsigned long first_set_bit;
49  assert(n != 0);
50  _BitScanReverse(&first_set_bit, n);
51  return first_set_bit;
52}
53#else
54// Returns (int)floor(log2(n)). n must be > 0.
55static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
56  int log = 0;
57  uint32_t value = n;
58  int i;
59
60  assert(n != 0);
61  for (i = 4; i >= 0; --i) {
62    const int shift = (1 << i);
63    const uint32_t x = value >> shift;
64    if (x != 0) {
65      value = x;
66      log += shift;
67    }
68  }
69  return log;
70}
71#endif
72
73static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) {
74  const int log_floor = BitsLog2Floor(n);
75  if (n == (n & ~(n - 1)))  // zero or a power of two.
76    return log_floor;
77  else
78    return log_floor + 1;
79}
80
81// Splitting of distance and length codes into prefixes and
82// extra bits. The prefixes are encoded with an entropy code
83// while the extra bits are stored just as normal bits.
84static WEBP_INLINE void PrefixEncode(int distance, int* const code,
85                                     int* const extra_bits_count,
86                                     int* const extra_bits_value) {
87  if (distance > 2) {  // Collect the two most significant bits.
88    const int highest_bit = BitsLog2Floor(--distance);
89    const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
90    *extra_bits_count = highest_bit - 1;
91    *extra_bits_value = distance & ((1 << *extra_bits_count) - 1);
92    *code = 2 * highest_bit + second_highest_bit;
93  } else {
94    *extra_bits_count = 0;
95    *extra_bits_value = 0;
96    *code = (distance == 2) ? 1 : 0;
97  }
98}
99
100// -----------------------------------------------------------------------------
101// PixOrCopy
102
103enum Mode {
104  kLiteral,
105  kCacheIdx,
106  kCopy,
107  kNone
108};
109
110typedef struct {
111  // mode as uint8_t to make the memory layout to be exactly 8 bytes.
112  uint8_t mode;
113  uint16_t len;
114  uint32_t argb_or_distance;
115} PixOrCopy;
116
117static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t distance,
118                                                 uint16_t len) {
119  PixOrCopy retval;
120  retval.mode = kCopy;
121  retval.argb_or_distance = distance;
122  retval.len = len;
123  return retval;
124}
125
126static WEBP_INLINE PixOrCopy PixOrCopyCreateCacheIdx(int idx) {
127  PixOrCopy retval;
128  assert(idx >= 0);
129  assert(idx < (1 << MAX_COLOR_CACHE_BITS));
130  retval.mode = kCacheIdx;
131  retval.argb_or_distance = idx;
132  retval.len = 1;
133  return retval;
134}
135
136static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) {
137  PixOrCopy retval;
138  retval.mode = kLiteral;
139  retval.argb_or_distance = argb;
140  retval.len = 1;
141  return retval;
142}
143
144static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy* const p) {
145  return (p->mode == kLiteral);
146}
147
148static WEBP_INLINE int PixOrCopyIsCacheIdx(const PixOrCopy* const p) {
149  return (p->mode == kCacheIdx);
150}
151
152static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy* const p) {
153  return (p->mode == kCopy);
154}
155
156static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy* const p,
157                                             int component) {
158  assert(p->mode == kLiteral);
159  return (p->argb_or_distance >> (component * 8)) & 0xff;
160}
161
162static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy* const p) {
163  return p->len;
164}
165
166static WEBP_INLINE uint32_t PixOrCopyArgb(const PixOrCopy* const p) {
167  assert(p->mode == kLiteral);
168  return p->argb_or_distance;
169}
170
171static WEBP_INLINE uint32_t PixOrCopyCacheIdx(const PixOrCopy* const p) {
172  assert(p->mode == kCacheIdx);
173  assert(p->argb_or_distance < (1U << MAX_COLOR_CACHE_BITS));
174  return p->argb_or_distance;
175}
176
177static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy* const p) {
178  assert(p->mode == kCopy);
179  return p->argb_or_distance;
180}
181
182// -----------------------------------------------------------------------------
183// VP8LBackwardRefs
184
185typedef struct {
186  PixOrCopy* refs;
187  int size;      // currently used
188  int max_size;  // maximum capacity
189} VP8LBackwardRefs;
190
191// Initialize the object. Must be called first. 'refs' can be NULL.
192void VP8LInitBackwardRefs(VP8LBackwardRefs* const refs);
193
194// Release memory and re-initialize the object. 'refs' can be NULL.
195void VP8LClearBackwardRefs(VP8LBackwardRefs* const refs);
196
197// Allocate 'max_size' references. Returns false in case of memory error.
198int VP8LBackwardRefsAlloc(VP8LBackwardRefs* const refs, int max_size);
199
200// -----------------------------------------------------------------------------
201// Main entry points
202
203// Evaluates best possible backward references for specified quality.
204// Further optimize for 2D locality if use_2d_locality flag is set.
205int VP8LGetBackwardReferences(int width, int height,
206                              const uint32_t* const argb,
207                              int quality, int cache_bits, int use_2d_locality,
208                              VP8LBackwardRefs* const best);
209
210// Produce an estimate for a good color cache size for the image.
211int VP8LCalculateEstimateForCacheSize(const uint32_t* const argb,
212                                      int xsize, int ysize,
213                                      int* const best_cache_bits);
214
215#if defined(__cplusplus) || defined(c_plusplus)
216}
217#endif
218
219#endif  // WEBP_ENC_BACKWARD_REFERENCES_H_
220