image.codec\codec\webp_vp8l_lz77_encode.mbt
92.7% · 164 / 177 points · 470 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # WebP VP8L LZ77 编码器(M2) - 3
/// - 4
/// 与 `webp_vp8l_encode.mbt` 的 literal-only 版相比,多了三件事: - 5
/// 1. 贪心 LZ77 匹配(哈希链,最短 3、最长 4096); - 6
/// 2. 每幅图按统计频次构建长度受限(≤15 位)的 canonical Huffman 码表; - 7
/// 3. 码表按 RFC 9649 §3.7.2.1.2 的"码长码 + 零游程"写法输出。 - 8
/// - 9
/// 未实现(M3):subtract-green / predictor / palette 变换、色彩缓存、meta 多组。 - 10
- 11
///| - 12
/// 位数反转(写码时用:流里是 canonical 码的 MSB-first 顺序,写入器是 LSB-first) - 13
fn vp8l_rev_bits(v : Int32, n : Int32) -> Int32 { - 14●
let mut r = 0 - 15
for i in 0..<n { - 16●
r = r | (((v >> i) & 1) << (n - 1 - i)) - 17
} - 18
r - 19
} - 20
- 21
///| - 22
/// 由码长表算 canonical 码值表(与解码侧同一规则) - 23
fn vp8l_canonical_codes(lens : Array[Int32], alphabet : Int32) -> Array[Int32] { - 24●
let codes : Array[Int32] = Array::make(alphabet, 0) - 25
let mut max_len = 0 - 26
for i in 0..<alphabet { - 27●
if lens[i] > max_len { - 28●
max_len = lens[i] - 29
} - 30
} - 31
if max_len == 0 { - 32●
return codes - 33
} - 34●
let bl_count : Array[Int32] = Array::make(max_len + 1, 0) - 35
for i in 0..<alphabet { - 36●
if lens[i] > 0 { - 37●
bl_count[lens[i]] += 1 - 38
} - 39
} - 40●
let next_code : Array[Int32] = Array::make(max_len + 1, 0) - 41
let mut code = 0 - 42
for bits in 1..<=max_len { - 43●
code = (code + bl_count[bits - 1]) << 1 - 44
next_code[bits] = code - 45
} - 46
for s in 0..<alphabet { - 47●
let l = lens[s] - 48
if l > 0 { - 49●
codes[s] = next_code[l] - 50
next_code[l] += 1 - 51
} - 52
} - 53
codes - 54
} - 55
- 56
///| - 57
/// 长度受限 Huffman:按频次生成 ≤`max_bits` 位的码长表。 - 58
/// - 59
/// 做法:先建普通 Huffman 得深度,再按 JPEG Annex K.3 的"深度钳制 + Kraft 补偿"把 - 60
/// 超过 `max_bits` 的部分压回,最后按频次降序重新分配长度。 - 61
fn vp8l_build_lengths( - 62
counts : Array[Int32], - 63
alphabet : Int32, - 64
max_bits : Int32, - 65
) -> Array[Int32] { - 66●
let lens : Array[Int32] = Array::make(alphabet, 0) - 67
// 收集使用中的符号 - 68
let used : Array[Int32] = [] - 69
for s in 0..<alphabet { - 70●
if counts[s] > 0 { - 71●
used.push(s) - 72
} - 73
} - 74●
let nu = used.length() - 75
if nu == 0 { - 76●
return lens - 77
} - 78
if nu == 1 { - 79●
lens[used[0]] = 1 - 80
return lens - 81
} - 82
// 普通 Huffman:反复合并两个最小频次,记录每个符号的深度 - 83●
let weights : Array[Int32] = Array::make(2 * nu - 1, 0) - 84●
let parents : Array[Int32] = Array::make(2 * nu - 1, -1) - 85●
let symbols : Array[Int32] = Array::make(2 * nu - 1, -1) - 86
for k in 0..<nu { - 87●
weights[k] = counts[used[k]] - 88
symbols[k] = used[k] - 89
} - 90
let mut active : Array[Int32] = [] - 91
for k in 0..<nu { - 92●
active.push(k) - 93
} - 94
let mut next = nu - 95●
while active.length() > 1 { - 96
// 找两个最小 - 97●
let mut a = 0 - 98
let mut b = 1 - 99
if weights[active[b]] < weights[active[a]] { - 100●
let t = a - 101
a = b - 102
b = t - 103
} - 104●
for k in 2..<active.length() { - 105●
let idx = active[k] - 106
if weights[idx] < weights[active[a]] { - 107●
b = a - 108
a = k - 109●
} else if weights[idx] < weights[active[b]] { - 110●
b = k - 111
} - 112
} - 113
let na = active[a] - 114
let nb = active[b] - 115
let node = next - 116
next += 1 - 117
weights[node] = weights[na] + weights[nb] - 118
parents[na] = node - 119
parents[nb] = node - 120●
let hi = if a > b { a } else { b } - 121●
let lo = if a > b { b } else { a } - 122
let rebuilt : Array[Int32] = [] - 123●
for k in 0..<active.length() { - 124●
if k != lo && k != hi { - 125●
rebuilt.push(active[k]) - 126
} - 127
} - 128●
rebuilt.push(node) - 129
active = rebuilt - 130
} - 131
// 由父链算深度(先钳到 max_bits) - 132●
let depths : Array[Int32] = Array::make(nu, 0) - 133
for s in 0..<nu { - 134●
let mut d = 0 - 135
let mut p = parents[s] - 136
while p >= 0 { - 137●
d += 1 - 138
p = parents[p] - 139
} - 140◐
depths[s] = if d > max_bits { max_bits } else { d } - 141
} - 142
for s in 0..<nu { - 143●
lens[used[s]] = depths[s] - 144
} - 145
// Kraft 补偿:以 2^-max_bits 为单位,K = Σ 2^(max_bits - len) 必须正好等于 2^max_bits - 146
let unit = 1 << max_bits - 147
let mut kraft = 0 - 148
for s in 0..<nu { - 149●
kraft = kraft + (1 << (max_bits - depths[s])) - 150
} - 151
// 超出:把"最深但仍短于 max_bits"的符号加深一位(优先动低频符号) - 152
while kraft > unit { - 153○
let mut pick = -1 - 154
for s in 0..<nu { - 155○
if depths[s] < max_bits { - 156○
if pick < 0 || - 157
depths[s] > depths[pick] || - 158
(depths[s] == depths[pick] && counts[used[s]] < counts[used[pick]]) { - 159○
pick = s - 160
} - 161
} - 162
} - 163
if pick < 0 { - 164○
break - 165
} - 166
kraft = kraft - (1 << (max_bits - depths[pick] - 1)) - 167
depths[pick] += 1 - 168
} - 169
// 不足:把最短的符号缩短一位(同样优先低频) - 170
while kraft < unit { - 171○
let mut pick = -1 - 172
for s in 0..<nu { - 173○
if depths[s] > 1 { - 174○
if pick < 0 || - 175
depths[s] < depths[pick] || - 176
(depths[s] == depths[pick] && counts[used[s]] < counts[used[pick]]) { - 177○
pick = s - 178
} - 179
} - 180
} - 181
if pick < 0 { - 182○
break - 183
} - 184
depths[pick] -= 1 - 185
kraft = kraft + (1 << (max_bits - depths[pick])) - 186
} - 187
// 写回码长表 - 188
for s in 0..<nu { - 189●
lens[used[s]] = depths[s] - 190
} - 191
lens - 192
} - 193
- 194
///| - 195
/// 写一个符号的 canonical 码(位反转后按 LSB-first 写入) - 196
fn vp8l_write_code(w : Vp8lBits, code : Int32, len : Int32) -> Unit { - 197●
w.write(vp8l_rev_bits(code, len), len) - 198
} - 199
- 200
///| - 201
/// 统计长度码 token(0..18)的使用次数,用于构建码长码 - 202
fn vp8l_count_length_tokens( - 203
lens : Array[Int32], - 204
alphabet : Int32, - 205
out : Array[Int32], - 206
) -> Unit { - 207●
let mut i = 0 - 208
let mut prev = -1 - 209
while i < alphabet { - 210●
let v = lens[i] - 211
if v == 0 { - 212●
let mut z = 0 - 213
while i + z < alphabet && lens[i + z] == 0 { - 214●
z += 1 - 215
} - 216
let mut left = z - 217
while left >= 11 { - 218●
let take = if left > 138 { 138 } else { left } - 219
out[18] += 1 - 220
left -= take - 221
} - 222
while left >= 3 { - 223◐
let take = if left > 10 { 10 } else { left } - 224
out[17] += 1 - 225
left -= take - 226
} - 227
while left > 0 { - 228●
out[0] += 1 - 229
left -= 1 - 230
} - 231
i += z - 232
prev = 0 - 233
} else { - 234●
let mut run = 1 - 235
while i + run < alphabet && lens[i + run] == v { - 236●
run += 1 - 237
} - 238
out[v] += 1 - 239
let mut left = run - 1 - 240
i += 1 - 241
while left > 0 { - 242●
if left >= 3 { - 243●
let take = if left > 6 { 6 } else { left } - 244
out[16] += 1 - 245
left -= take - 246
i += take - 247
} else { - 248●
out[v] += 1 - 249
left -= 1 - 250
i += 1 - 251
} - 252
} - 253
prev = v - 254
} - 255
} - 256
let _ = prev - 257
} - 258
- 259
///| - 260
/// 真正把码长表写进流:simple(1/2 个符号)或 normal(码长码 + 零游程) - 261
fn vp8l_write_prefix_table( - 262
w : Vp8lBits, - 263
lens : Array[Int32], - 264
alphabet : Int32, - 265
) -> Unit { - 266●
let used : Array[Int32] = [] - 267
for s in 0..<alphabet { - 268●
if lens[s] > 0 { - 269●
used.push(s) - 270
} - 271
} - 272●
if used.length() == 0 { - 273
// 空表按单符号 0 写(解码器视作零比特) - 274●
vp8l_write_simple_single(w, 0) - 275
return - 276
} - 277●
if used.length() == 1 && used[0] < 256 { - 278●
vp8l_write_simple_single(w, used[0]) - 279
return - 280
} - 281
let mut all_one_bit = true - 282
for s in used { - 283●
if lens[s] != 1 { - 284●
all_one_bit = false - 285
} - 286
} - 287●
if all_one_bit && used.length() == 2 && used[0] < 256 && used[1] < 256 { - 288
// 双符号 simple 码表:符号必须能塞进 8 位;首个符号按 is_first_8bits 决定 1 位还是 8 位 - 289●
w.write(1, 1) - 290●
w.write(1, 1) - 291
if used[0] > 1 { - 292●
w.write(1, 1) - 293●
w.write(used[0], 8) - 294
} else { - 295●
w.write(0, 1) - 296●
w.write(used[0], 1) - 297
} - 298●
w.write(used[1], 8) - 299
return - 300
} - 301
// normal:先构建码长码 - 302●
let tokens : Array[Int32] = Array::make(19, 0) - 303●
vp8l_count_length_tokens(lens, alphabet, tokens) - 304●
let cl_lens = vp8l_build_lengths(tokens, 19, 7) - 305
// num_code_lengths:覆盖所有非零码长码符号在 order 中的位置 - 306
let mut num_code_lengths = 4 - 307
for i in 0..<19 { - 308●
if cl_lens[vp8l_code_length_order[i]] > 0 && i + 1 > num_code_lengths { - 309●
num_code_lengths = i + 1 - 310
} - 311
} - 312●
w.write(0, 1) - 313●
w.write(num_code_lengths - 4, 4) - 314
for i in 0..<num_code_lengths { - 315●
w.write(cl_lens[vp8l_code_length_order[i]], 3) - 316
} - 317●
w.write(0, 1) // max_symbol = 字母表大小 - 318●
let cl_codes = vp8l_canonical_codes(cl_lens, 19) - 319
// 按同一规则重新遍历并写 token - 320
let mut i = 0 - 321
while i < alphabet { - 322●
let v = lens[i] - 323
if v == 0 { - 324●
let mut z = 0 - 325
while i + z < alphabet && lens[i + z] == 0 { - 326●
z += 1 - 327
} - 328
let mut left = z - 329
while left >= 11 { - 330●
let take = if left > 138 { 138 } else { left } - 331●
vp8l_write_code(w, cl_codes[18], cl_lens[18]) - 332●
w.write(take - 11, 7) - 333
left -= take - 334
} - 335
while left >= 3 { - 336◐
let take = if left > 10 { 10 } else { left } - 337●
vp8l_write_code(w, cl_codes[17], cl_lens[17]) - 338●
w.write(take - 3, 3) - 339
left -= take - 340
} - 341
while left > 0 { - 342●
vp8l_write_code(w, cl_codes[0], cl_lens[0]) - 343
left -= 1 - 344
} - 345
i += z - 346
} else { - 347●
let mut run = 1 - 348
while i + run < alphabet && lens[i + run] == v { - 349●
run += 1 - 350
} - 351●
vp8l_write_code(w, cl_codes[v], cl_lens[v]) - 352
let mut left = run - 1 - 353
i += 1 - 354
while left > 0 { - 355●
if left >= 3 { - 356●
let take = if left > 6 { 6 } else { left } - 357●
vp8l_write_code(w, cl_codes[16], cl_lens[16]) - 358●
w.write(take - 3, 2) - 359
left -= take - 360
i += take - 361
} else { - 362●
vp8l_write_code(w, cl_codes[v], cl_lens[v]) - 363
left -= 1 - 364
i += 1 - 365
} - 366
} - 367
} - 368
} - 369
} - 370
- 371
///| - 372
/// 长度值 → (前缀码, extra 值, extra 位数) - 373
fn vp8l_encode_length(len : Int32) -> (Int32, Int32, Int32) { - 374●
if len <= 4 { - 375●
(len - 1, 0, 0) - 376
} else { - 377●
let mut prefix = 0 - 378
let mut found = false - 379
let mut p = 4 - 380
while !found && p < 24 { - 381●
let eb = (p - 2) >> 1 - 382
let off = (2 + (p & 1)) << eb - 383
if len >= off + 1 && len <= off + (1 << eb) { - 384●
prefix = p - 385
found = true - 386
} - 387
p += 1 - 388
} - 389
let eb = (prefix - 2) >> 1 - 390
let off = (2 + (prefix & 1)) << eb - 391
(prefix, len - off - 1, eb) - 392
} - 393
} - 394
- 395
///| - 396
/// 距离码值 → (距离前缀码 0..39, extra 值, extra 位数)(RFC 9649 §3.6.2.2.1 的 40 项前缀表) - 397
fn vp8l_encode_distance(d : Int32) -> (Int32, Int32, Int32) { - 398●
if d <= 4 { - 399●
(d - 1, 0, 0) - 400
} else { - 401●
let mut p = 4 - 402
while p < 40 { - 403●
let eb = (p - 2) >> 1 - 404
let off = (2 + (p & 1)) << eb - 405
if d >= off + 1 && d <= off + (1 << eb) { - 406●
return (p, d - off - 1, eb) - 407
} - 408
p += 1 - 409
} - 410
// 上限 1048576,调用方已把距离裁到此范围内,这里只作兜底 - 411
(39, 0, 0) - 412
} - 413
} - 414
- 415
///| - 416
/// 扫描行距离 → 距离码(先查 120 项邻域映射,命中用 1..120,否则 dist + 120) - 417
fn vp8l_distance_code(d : Int32, width : Int32) -> Int32 { - 418●
for k in 0..<120 { - 419●
let xi = vp8l_distance_dx[k].to_int() - 420●
let yi = vp8l_distance_dy[k].to_int() - 421
let cand = xi + yi * width - 422
if cand == d && cand >= 1 { - 423●
return k + 1 - 424
} - 425
} - 426
d + 120 - 427
} - 428
- 429
///| - 430
/// 判断码表是否只有一个符号(单符号树的符号位宽为 0,编码时不得写任何比特) - 431
fn vp8l_is_single(lens : Array[Int32], alphabet : Int32) -> Bool { - 432●
let mut count = 0 - 433
for i in 0..<alphabet { - 434●
if lens[i] > 0 { - 435●
count += 1 - 436
} - 437
} - 438
count <= 1 - 439
} - 440
- 441
///| - 442
/// 32 位像素哈希(4096 槽) - 443
fn vp8l_hash_pixel(argb : Int32) -> Int32 { - 444●
(((argb * 0x1E35A7BD) & 0xFFFFFFFF) >> 20) & 0xFFF - 445
} - 446
- 447
///| - 448
/// M2 编码器:贪心 LZ77 + 频次驱动 Huffman(无变换)。 - 449
/// - 450
/// 变换版本见 encode_webp_lossless。 - 451
pub fn encode_webp_vp8l_lz77( - 452
img : @types.Image, - 453
) -> Bytes raise @types.LoadError { - 454●
let (pixels, w, h) = vp8l_pack_argb(img) - 455
let mut alpha_solid = true - 456●
for i in 0..<pixels.length() { - 457●
if vp8l_ch(pixels[i], 24) != 255 { - 458●
alpha_solid = false - 459
} - 460
} - 461●
let bw = Vp8lBits::new() - 462●
bw.write(0x2F, 8) - 463●
bw.write(w - 1, 14) - 464●
bw.write(h - 1, 14) - 465●
bw.write(if alpha_solid { 0 } else { 1 }, 1) - 466●
bw.write(0, 3) - 467●
bw.write(0, 1) // 无变换 - 468●
vp8l_write_image_stream(bw, pixels, w, true) - 469●
vp8l_wrap_riff(bw.finish()) - 470
}