1 // Copyright 2012 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
10 // Author: Jyrki Alakuijala (jyrki@google.com)
13 #ifndef WEBP_ENC_BACKWARD_REFERENCES_H_
14 #define WEBP_ENC_BACKWARD_REFERENCES_H_
18 #include "../webp/types.h"
19 #include "../webp/format_constants.h"
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
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))
33 // -----------------------------------------------------------------------------
44 // mode as uint8_t to make the memory layout to be exactly 8 bytes.
47 uint32_t argb_or_distance
;
50 static WEBP_INLINE PixOrCopy
PixOrCopyCreateCopy(uint32_t distance
,
54 retval
.argb_or_distance
= distance
;
59 static WEBP_INLINE PixOrCopy
PixOrCopyCreateCacheIdx(int idx
) {
62 assert(idx
< (1 << MAX_COLOR_CACHE_BITS
));
63 retval
.mode
= kCacheIdx
;
64 retval
.argb_or_distance
= idx
;
69 static WEBP_INLINE PixOrCopy
PixOrCopyCreateLiteral(uint32_t argb
) {
71 retval
.mode
= kLiteral
;
72 retval
.argb_or_distance
= argb
;
77 static WEBP_INLINE
int PixOrCopyIsLiteral(const PixOrCopy
* const p
) {
78 return (p
->mode
== kLiteral
);
81 static WEBP_INLINE
int PixOrCopyIsCacheIdx(const PixOrCopy
* const p
) {
82 return (p
->mode
== kCacheIdx
);
85 static WEBP_INLINE
int PixOrCopyIsCopy(const PixOrCopy
* const p
) {
86 return (p
->mode
== kCopy
);
89 static WEBP_INLINE
uint32_t PixOrCopyLiteral(const PixOrCopy
* const p
,
91 assert(p
->mode
== kLiteral
);
92 return (p
->argb_or_distance
>> (component
* 8)) & 0xff;
95 static WEBP_INLINE
uint32_t PixOrCopyLength(const PixOrCopy
* const p
) {
99 static WEBP_INLINE
uint32_t PixOrCopyArgb(const PixOrCopy
* const p
) {
100 assert(p
->mode
== kLiteral
);
101 return p
->argb_or_distance
;
104 static WEBP_INLINE
uint32_t PixOrCopyCacheIdx(const PixOrCopy
* const p
) {
105 assert(p
->mode
== kCacheIdx
);
106 assert(p
->argb_or_distance
< (1U << MAX_COLOR_CACHE_BITS
));
107 return p
->argb_or_distance
;
110 static WEBP_INLINE
uint32_t PixOrCopyDistance(const PixOrCopy
* const p
) {
111 assert(p
->mode
== kCopy
);
112 return p
->argb_or_distance
;
115 // -----------------------------------------------------------------------------
120 int size
; // currently used
121 int max_size
; // maximum capacity
124 // Initialize the object. Must be called first. 'refs' can be NULL.
125 void VP8LInitBackwardRefs(VP8LBackwardRefs
* const refs
);
127 // Release memory and re-initialize the object. 'refs' can be NULL.
128 void VP8LClearBackwardRefs(VP8LBackwardRefs
* const refs
);
130 // Allocate 'max_size' references. Returns false in case of memory error.
131 int VP8LBackwardRefsAlloc(VP8LBackwardRefs
* const refs
, int max_size
);
133 // -----------------------------------------------------------------------------
136 // Evaluates best possible backward references for specified quality.
137 // Further optimize for 2D locality if use_2d_locality flag is set.
138 int VP8LGetBackwardReferences(int width
, int height
,
139 const uint32_t* const argb
,
140 int quality
, int cache_bits
, int use_2d_locality
,
141 VP8LBackwardRefs
* const best
);
143 // Produce an estimate for a good color cache size for the image.
144 int VP8LCalculateEstimateForCacheSize(const uint32_t* const argb
,
145 int xsize
, int ysize
,
146 int* const best_cache_bits
);
152 #endif // WEBP_ENC_BACKWARD_REFERENCES_H_