1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// libwebp v1.6.0, src/dsp/lossless_common.h:
//
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
// -----------------------------------------------------------------------------
//
// Image transforms and color space conversion methods for lossless decoder.
//
// Authors: Vikas Arora (vikaas.arora@gmail.com)
// Jyrki Alakuijala (jyrki@google.com)
// Vincent Rabaud (vrabaud@google.com)
//! Translation of `src/dsp/lossless_common.h` from libwebp v1.6.0.
//!
//! Work package: WP3 (lossless shared).
//!
//! Exception: WP11 translates `VP8LFastLog2`, `VP8LFastSLog2`, `VP8LPrefixEncodeBits` and `VP8LPrefixEncode` from `src/dsp/lossless_common.h`, because they read `kLog2Table`, `kSLog2Table`, `VP8LFastLog2Slow`, `VP8LFastSLog2Slow`, `kPrefixEncodeCode` and `kPrefixEncodeExtraBitsValue`, which WP11 translates from `src/dsp/lossless_enc.c`.
//!
//! Not translated here, with the reason: the four functions of the exception
//! above, which WP11 appends to this module with the tables they read
//! (decided 2026-11-06). The declarations of those tables and slow functions
//! belong with their definitions in `src/dsp/lossless_enc.rs`.
//!
//! # Deviations from upstream
//!
//! Changes of representation that the ADRs allow, or behavior that the Rust
//! side cannot reproduce exactly. Each records the reason.
//!
//! 1. **`GENERATE_PREDICTOR_ADD`** is a `macro_rules!` macro of this module,
//! re-exported to the crate, which generates functions of the decoder form.
//! The C `assert(upper != NULL)` has no counterpart, because positions
//! cannot be `NULL`; the left position is formed with a wrapping
//! subtraction, as the C `&out[x - 1]` is formed, and only read by the
//! predictors that read it.
//!
//! # Excluded undefined behavior
//!
//! `VP8LSubSampleSize`, `RightShiftRound`, `DivRound` and the prefix encodings
//! exclude inputs that are undefined behavior in C; they are listed in
//! `src/utils/color_cache_utils.rs`, `# Excluded undefined behavior` (item 1).
//!
//! # Checks not applicable
//!
//! These checks do not apply, or cannot be made, with the reason:
//!
//! 1. **Floating point.** No translated function of the lossless utilities
//! computes in floating point. `LOG_2_RECIPROCAL` and
//! `LOG_2_RECIPROCAL_FIXED_DOUBLE` are constants and are compared as bit
//! patterns.
use cratebits_log2_floor;
//------------------------------------------------------------------------------
// Decoding
// upstream: src/dsp/lossless_common.h VP8GetARGBIndex
/// Color mapping related functions.
// upstream: src/dsp/lossless_common.h VP8GetAlphaIndex
// upstream: src/dsp/lossless_common.h VP8GetARGBValue
// upstream: src/dsp/lossless_common.h VP8GetAlphaValue
//------------------------------------------------------------------------------
// Misc methods.
// upstream: src/dsp/lossless_common.h VP8LSubSampleSize
/// Computes sampled size of `size` when sampling using `sampling_bits`.
/// The `uint32_t` sum wraps, as in C.
// upstream: src/dsp/lossless_common.h VP8LNearLosslessBits
/// Converts near lossless quality into max number of bits shaved off.
// -----------------------------------------------------------------------------
// Faster logarithm for integers. Small values use a look-up table.
// upstream: src/dsp/lossless_common.h APPROX_LOG_WITH_CORRECTION_MAX
/// The threshold till approximate version of `log_2` can be used.
/// Practically, we can get rid of the call to `log()` as the two values match
/// to very high degree (the ratio of these two is 0.99999x).
/// Keeping a high threshold for now.
pub const APPROX_LOG_WITH_CORRECTION_MAX: i32 = 65536;
// upstream: src/dsp/lossless_common.h APPROX_LOG_MAX
pub const APPROX_LOG_MAX: i32 = 4096;
// upstream: src/dsp/lossless_common.h LOG_2_PRECISION_BITS
/// `VP8LFastLog2` and `VP8LFastSLog2` are used on elements from image
/// histograms. The histogram values cannot exceed the maximum number of
/// pixels, which is (1 << 14) * (1 << 14). Therefore S * log(S) < (1 << 33).
/// No more than 32 bits of precision should be chosen.
/// To match the original float implementation, 23 bits of precision are used.
pub const LOG_2_PRECISION_BITS: i32 = 23;
// upstream: src/dsp/lossless_common.h LOG_2_RECIPROCAL
pub const LOG_2_RECIPROCAL: f64 = 1.442_695_040_888_963_387_004_650_940_070_86;
// upstream: src/dsp/lossless_common.h LOG_2_RECIPROCAL_FIXED_DOUBLE
/// `LOG_2_RECIPROCAL * (1 << LOG_2_PRECISION_BITS)`
pub const LOG_2_RECIPROCAL_FIXED_DOUBLE: f64 = 12_102_203.161_561_485_379_934_310_913_085_937_500;
// upstream: src/dsp/lossless_common.h LOG_2_RECIPROCAL_FIXED
pub const LOG_2_RECIPROCAL_FIXED: u64 = 12_102_203;
// upstream: src/dsp/lossless_common.h LOG_LOOKUP_IDX_MAX
pub const LOG_LOOKUP_IDX_MAX: i32 = 256;
// upstream: src/dsp/lossless_common.h RightShiftRound
/// `v` shifted right by `shift`, rounded to nearest. The `uint64_t` sum wraps,
/// as in C.
// upstream: src/dsp/lossless_common.h DivRound
/// `a / b`, rounded half away from zero.
// upstream: src/dsp/lossless_common.h WEBP_INT64_MAX
pub const WEBP_INT64_MAX: i64 = .cast_signed;
// upstream: src/dsp/lossless_common.h WEBP_UINT64_MAX
pub const WEBP_UINT64_MAX: u64 = !0_u64;
// -----------------------------------------------------------------------------
// PrefixEncode()
// upstream: src/dsp/lossless_common.h VP8LPrefixEncodeBitsNoLUT
/// Splitting of distance and length codes into prefixes and extra bits. The
/// prefixes are encoded with an entropy code while the extra bits are stored
/// just as normal bits.
///
/// Returns `(code, extra_bits)`, the two out-parameters of the C function.
/// `distance` must be at least 3; C shifts by a negative amount below that.
// upstream: src/dsp/lossless_common.h VP8LPrefixEncodeNoLUT
/// Returns `(code, extra_bits, extra_bits_value)`, the three out-parameters of
/// the C function. `distance` must be at least 3.
// upstream: src/dsp/lossless_common.h PREFIX_LOOKUP_IDX_MAX
pub const PREFIX_LOOKUP_IDX_MAX: i32 = 512;
// upstream: src/dsp/lossless_common.h VP8LPrefixCode
// upstream: src/dsp/lossless_common.h VP8LAddPixels
/// Sum of each component, mod 256.
// upstream: src/dsp/lossless_common.h VP8LSubPixels
/// Difference of each component, mod 256.
//------------------------------------------------------------------------------
// Transform-related functions used in both encoding and decoding.
// upstream: src/dsp/lossless_common.h GENERATE_PREDICTOR_ADD
/// Macros used to create a batch predictor that iteratively uses a one-pixel
/// predictor.
///
/// The predictor is added to the output pixel (which is therefore considered
/// as a residual) to get the final prediction. The generated function has the
/// form of `VP8LPredictorAddSubFunc` in `src/dsp/lossless.rs`: `in`, `upper`
/// and `out` are positions in one buffer `data`, because the decoder reads the
/// row above the output, and transforms in place.
pub use generate_predictor_add;
// -----------------------------------------------------------------------------
// WP11's exception (decided 2026-11-06): the four functions below read the
// tables and slow functions that `src/dsp/lossless_enc.rs` defines.
// upstream: src/dsp/lossless_common.h VP8LFastLog2
/// `log2(v)` in fixed point with `LOG_2_PRECISION_BITS` fractional bits: a
/// table entry for small values, `VP8LFastLog2Slow` otherwise.
// upstream: src/dsp/lossless_common.h VP8LFastSLog2
/// Fast calculation of `v * log2(v)` for integer input.
// upstream: src/dsp/lossless_common.h VP8LPrefixEncodeBits
/// Returns `(code, extra_bits)`, the two out-parameters of the C function: a
/// table entry for distances below `PREFIX_LOOKUP_IDX_MAX`,
/// `VP8LPrefixEncodeBitsNoLUT` otherwise.
///
/// # Panics
///
/// Panics for a negative distance, which C reads before the table.
// upstream: src/dsp/lossless_common.h VP8LPrefixEncode
/// Returns `(code, extra_bits, extra_bits_value)`, the three out-parameters of
/// the C function.
///
/// # Panics
///
/// Panics for a negative distance, which C reads before the table.