rust_h265 0.1.0

Pure Rust H.265/HEVC video decoder (Main and Main 10 profile, 8-bit and 10-bit 4:2:0)
Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
//! CABAC engine tables (HEVC spec section 9.3.4).
//!
//! `NORM_SHIFT`, `LPS_RANGE`, and `MLPS_STATE` are the renormalization /
//! state-transition tables. HEVC inherited them verbatim from H.264, so
//! these are byte-for-byte the same as the corresponding rust_h264 tables.
//! The HEVC spec presents them as `rangeTabLPS` (table 9-41), `transIdxLPS`
//! (table 9-42), and `transIdxMPS` (table 9-43); the FFmpeg-style packed
//! encoding used here interleaves them for efficient lookup.
//!
//! **Per-syntax-element init values** (HEVC spec table 9-4) are intentionally
//! NOT in this file yet. They are the second half of CABAC bring-up:
//! transcribing them from the spec is mechanical but error-prone, and a
//! single wrong value silently corrupts the decoder. They will be added
//! incrementally per syntax element as Phase 2b proceeds.

#[rustfmt::skip]
pub static NORM_SHIFT: [u8; 512] = [
    9,8,7,7,6,6,6,6,5,5,5,5,5,5,5,5,
    4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
    3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
    3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
];

/// Equivalent of HEVC spec `rangeTabLPS` (table 9-41), packed for the
/// FFmpeg-style 4-way range bucket lookup (`(range >> 6) & 3`).
#[rustfmt::skip]
pub static LPS_RANGE: [u8; 512] = [
    // Range group 0
    128, 128, 128, 128, 128, 128, 123, 123,
    116, 116, 111, 111, 105, 105, 100, 100,
     95,  95,  90,  90,  85,  85,  81,  81,
     77,  77,  73,  73,  69,  69,  66,  66,
     62,  62,  59,  59,  56,  56,  53,  53,
     51,  51,  48,  48,  46,  46,  43,  43,
     41,  41,  39,  39,  37,  37,  35,  35,
     33,  33,  32,  32,  30,  30,  29,  29,
     27,  27,  26,  26,  24,  24,  23,  23,
     22,  22,  21,  21,  20,  20,  19,  19,
     18,  18,  17,  17,  16,  16,  15,  15,
     14,  14,  14,  14,  13,  13,  12,  12,
     12,  12,  11,  11,  11,  11,  10,  10,
     10,  10,   9,   9,   9,   9,   8,   8,
      8,   8,   7,   7,   7,   7,   7,   7,
      6,   6,   6,   6,   6,   6,   2,   2,
    // Range group 1
    176, 176, 167, 167, 158, 158, 150, 150,
    142, 142, 135, 135, 128, 128, 122, 122,
    116, 116, 110, 110, 104, 104,  99,  99,
     94,  94,  89,  89,  85,  85,  80,  80,
     76,  76,  72,  72,  69,  69,  65,  65,
     62,  62,  59,  59,  56,  56,  53,  53,
     50,  50,  48,  48,  45,  45,  43,  43,
     41,  41,  39,  39,  37,  37,  35,  35,
     33,  33,  31,  31,  30,  30,  28,  28,
     27,  27,  26,  26,  24,  24,  23,  23,
     22,  22,  21,  21,  20,  20,  19,  19,
     18,  18,  17,  17,  16,  16,  15,  15,
     14,  14,  14,  14,  13,  13,  12,  12,
     12,  12,  11,  11,  11,  11,  10,  10,
      9,   9,   9,   9,   9,   9,   8,   8,
      8,   8,   7,   7,   7,   7,   2,   2,
    // Range group 2
    208, 208, 197, 197, 187, 187, 178, 178,
    169, 169, 160, 160, 152, 152, 144, 144,
    137, 137, 130, 130, 123, 123, 117, 117,
    111, 111, 105, 105, 100, 100,  95,  95,
     90,  90,  86,  86,  81,  81,  77,  77,
     73,  73,  69,  69,  66,  66,  63,  63,
     59,  59,  56,  56,  54,  54,  51,  51,
     48,  48,  46,  46,  43,  43,  41,  41,
     39,  39,  37,  37,  35,  35,  33,  33,
     32,  32,  30,  30,  29,  29,  27,  27,
     26,  26,  25,  25,  23,  23,  22,  22,
     21,  21,  20,  20,  19,  19,  18,  18,
     17,  17,  16,  16,  15,  15,  15,  15,
     14,  14,  13,  13,  12,  12,  12,  12,
     11,  11,  11,  11,  10,  10,  10,  10,
      9,   9,   9,   9,   8,   8,   2,   2,
    // Range group 3
    240, 240, 227, 227, 216, 216, 205, 205,
    195, 195, 185, 185, 175, 175, 166, 166,
    158, 158, 150, 150, 142, 142, 135, 135,
    128, 128, 122, 122, 116, 116, 110, 110,
    104, 104,  99,  99,  94,  94,  89,  89,
     85,  85,  80,  80,  76,  76,  72,  72,
     69,  69,  65,  65,  62,  62,  59,  59,
     56,  56,  53,  53,  50,  50,  48,  48,
     45,  45,  43,  43,  41,  41,  39,  39,
     37,  37,  35,  35,  33,  33,  31,  31,
     30,  30,  28,  28,  27,  27,  25,  25,
     24,  24,  23,  23,  22,  22,  21,  21,
     20,  20,  19,  19,  18,  18,  17,  17,
     16,  16,  15,  15,  14,  14,  14,  14,
     13,  13,  12,  12,  12,  12,  11,  11,
     11,  11,  10,  10,   9,   9,   2,   2,
];

/// Combined MPS / LPS state transition table. Indices 0..127 are the MPS
/// branch (next state after decoding the most-probable symbol); indices
/// 128..255 are the LPS branch.
#[rustfmt::skip]
pub static MLPS_STATE: [u8; 256] = [
    // MPS transitions
    127, 126,  77,  76,  77,  76,  75,  74,
     75,  74,  75,  74,  73,  72,  73,  72,
     73,  72,  71,  70,  71,  70,  71,  70,
     69,  68,  69,  68,  67,  66,  67,  66,
     67,  66,  65,  64,  65,  64,  63,  62,
     61,  60,  61,  60,  61,  60,  59,  58,
     59,  58,  57,  56,  55,  54,  55,  54,
     53,  52,  53,  52,  51,  50,  49,  48,
     49,  48,  47,  46,  45,  44,  45,  44,
     43,  42,  43,  42,  39,  38,  39,  38,
     37,  36,  37,  36,  33,  32,  33,  32,
     31,  30,  31,  30,  27,  26,  27,  26,
     25,  24,  23,  22,  23,  22,  19,  18,
     19,  18,  17,  16,  15,  14,  13,  12,
     11,  10,   9,   8,   9,   8,   5,   4,
      5,   4,   3,   2,   1,   0,   0,   1,
    // LPS transitions
      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, 124, 125, 126, 127,
];

// ---- Per-syntax-element CABAC context init values (HEVC spec table 9-4) ----
//
// `INIT_VALUES[init_type][i]` is the 8-bit packed init value for context `i`,
// where `init_type` is 0 for I slices, 1 for "P with cabac_init=0 / B with
// cabac_init=1", and 2 for "P with cabac_init=1 / B with cabac_init=0".
// Spec encoding: high nibble = `slopeIdx`, low nibble = `offsetIdx`.
//
// Layout matches FFmpeg's `libavcodec/hevc/cabac.c` `init_values[]`. Contexts
// that aren't read in a given slice type are filled with `CNU` (= 154,
// "context not used") so the indexing stays uniform across slice types.
//
// Total: 179 contexts. The named offsets/lengths in [`ctx`] below partition
// the table by syntax element. Sub-element ordering matches HEVC spec table
// 9-4 / 9-5 / etc. exactly.

/// Number of CABAC contexts in HEVC.
pub const HEVC_CONTEXTS: usize = 179;

/// "Context not used" placeholder, kept so contexts unused in a given slice
/// type still have a defined value (= 154) when the table is indexed.
const CNU: u8 = 154;

#[rustfmt::skip]
pub static INIT_VALUES: [[u8; HEVC_CONTEXTS]; 3] = [
    // ---------- init_type = 0 (I slice) ----------
    [
        // sao_merge_flag
        153,
        // sao_type_idx
        200,
        // split_coding_unit_flag (3)
        139, 141, 157,
        // cu_transquant_bypass_flag (1)
        154,
        // skip_flag (3) — not read in I, CNU
        CNU, CNU, CNU,
        // cu_qp_delta (3)
        154, 154, 154,
        // pred_mode_flag (1) — not read in I, CNU
        CNU,
        // part_mode (4) — only the first context is read in I-slice
        184, CNU, CNU, CNU,
        // prev_intra_luma_pred_flag (1)
        184,
        // intra_chroma_pred_mode (2)
        63, 139,
        // merge_flag (1) — P/B only
        CNU,
        // merge_idx (1) — P/B only
        CNU,
        // inter_pred_idc (5) — B only
        CNU, CNU, CNU, CNU, CNU,
        // ref_idx_l0 (2) — P/B only
        CNU, CNU,
        // ref_idx_l1 (2) — B only
        CNU, CNU,
        // abs_mvd_greater0_flag (2) — P/B only
        CNU, CNU,
        // abs_mvd_greater1_flag (2) — P/B only
        CNU, CNU,
        // mvp_lx_flag (1) — P/B only
        CNU,
        // no_residual_data_flag (1) — P/B only
        CNU,
        // split_transform_flag (3)
        153, 138, 138,
        // cbf_luma (2)
        111, 141,
        // cbf_cb / cbf_cr (5)
        94, 138, 182, 154, 154,
        // transform_skip_flag (2) — luma + chroma
        139, 139,
        // explicit_rdpcm_flag (2)
        139, 139,
        // explicit_rdpcm_dir_flag (2)
        139, 139,
        // last_significant_coeff_x_prefix (18)
        110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
         79, 108, 123,  63,
        // last_significant_coeff_y_prefix (18)
        110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
         79, 108, 123,  63,
        // significant_coeff_group_flag (4)
        91, 171, 134, 141,
        // significant_coeff_flag (44)
        111, 111, 125, 110, 110,  94, 124, 108, 124, 107, 125, 141, 179, 153,
        125, 107, 125, 141, 179, 153, 125, 107, 125, 141, 179, 153, 125, 140,
        139, 182, 182, 152, 136, 152, 136, 153, 136, 139, 111, 136, 139, 111,
        141, 111,
        // coeff_abs_level_greater1_flag (24)
        140,  92, 137, 138, 140, 152, 138, 139, 153,  74, 149,  92, 139, 107,
        122, 152, 140, 179, 166, 182, 140, 227, 122, 197,
        // coeff_abs_level_greater2_flag (6)
        138, 153, 136, 167, 152, 152,
        // log2_res_scale_abs (8)
        154, 154, 154, 154, 154, 154, 154, 154,
        // res_scale_sign_flag (2)
        154, 154,
        // cu_chroma_qp_offset_flag (1)
        154,
        // cu_chroma_qp_offset_idx (1)
        154,
    ],
    // ---------- init_type = 1 (P cabac_init=0 / B cabac_init=1) ----------
    [
        // sao_merge_flag
        153,
        // sao_type_idx
        185,
        // split_coding_unit_flag
        107, 139, 126,
        // cu_transquant_bypass_flag
        154,
        // skip_flag
        197, 185, 201,
        // cu_qp_delta
        154, 154, 154,
        // pred_mode_flag
        149,
        // part_mode
        154, 139, 154, 154,
        // prev_intra_luma_pred_flag
        154,
        // intra_chroma_pred_mode
        152, 139,
        // merge_flag
        110,
        // merge_idx
        122,
        // inter_pred_idc
        95, 79, 63, 31, 31,
        // ref_idx_l0
        153, 153,
        // ref_idx_l1
        153, 153,
        // abs_mvd_greater0_flag
        140, 198,
        // abs_mvd_greater1_flag
        140, 198,
        // mvp_lx_flag
        168,
        // no_residual_data_flag
        79,
        // split_transform_flag
        124, 138,  94,
        // cbf_luma
        153, 111,
        // cbf_cb / cbf_cr
        149, 107, 167, 154, 154,
        // transform_skip_flag
        139, 139,
        // explicit_rdpcm_flag
        139, 139,
        // explicit_rdpcm_dir_flag
        139, 139,
        // last_significant_coeff_x_prefix
        125, 110,  94, 110,  95,  79, 125, 111, 110,  78, 110, 111, 111,  95,
         94, 108, 123, 108,
        // last_significant_coeff_y_prefix
        125, 110,  94, 110,  95,  79, 125, 111, 110,  78, 110, 111, 111,  95,
         94, 108, 123, 108,
        // significant_coeff_group_flag
        121, 140,  61, 154,
        // significant_coeff_flag
        155, 154, 139, 153, 139, 123, 123,  63, 153, 166, 183, 140, 136, 153,
        154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
        153, 123, 123, 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140,
        140, 140,
        // coeff_abs_level_greater1_flag
        154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
        136, 137, 169, 194, 166, 167, 154, 167, 137, 182,
        // coeff_abs_level_greater2_flag
        107, 167,  91, 122, 107, 167,
        // log2_res_scale_abs
        154, 154, 154, 154, 154, 154, 154, 154,
        // res_scale_sign_flag
        154, 154,
        // cu_chroma_qp_offset_flag
        154,
        // cu_chroma_qp_offset_idx
        154,
    ],
    // ---------- init_type = 2 (P cabac_init=1 / B cabac_init=0) ----------
    [
        // sao_merge_flag
        153,
        // sao_type_idx
        160,
        // split_coding_unit_flag
        107, 139, 126,
        // cu_transquant_bypass_flag
        154,
        // skip_flag
        197, 185, 201,
        // cu_qp_delta
        154, 154, 154,
        // pred_mode_flag
        134,
        // part_mode
        154, 139, 154, 154,
        // prev_intra_luma_pred_flag
        183,
        // intra_chroma_pred_mode
        152, 139,
        // merge_flag
        154,
        // merge_idx
        137,
        // inter_pred_idc
        95, 79, 63, 31, 31,
        // ref_idx_l0
        153, 153,
        // ref_idx_l1
        153, 153,
        // abs_mvd_greater0_flag
        169, 198,
        // abs_mvd_greater1_flag
        169, 198,
        // mvp_lx_flag
        168,
        // no_residual_data_flag
        79,
        // split_transform_flag
        224, 167, 122,
        // cbf_luma
        153, 111,
        // cbf_cb / cbf_cr
        149,  92, 167, 154, 154,
        // transform_skip_flag
        139, 139,
        // explicit_rdpcm_flag
        139, 139,
        // explicit_rdpcm_dir_flag
        139, 139,
        // last_significant_coeff_x_prefix
        125, 110, 124, 110,  95,  94, 125, 111, 111,  79, 125, 126, 111, 111,
         79, 108, 123,  93,
        // last_significant_coeff_y_prefix
        125, 110, 124, 110,  95,  94, 125, 111, 111,  79, 125, 126, 111, 111,
         79, 108, 123,  93,
        // significant_coeff_group_flag
        121, 140,  61, 154,
        // significant_coeff_flag
        170, 154, 139, 153, 139, 123, 123,  63, 124, 166, 183, 140, 136, 153,
        154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
        153, 138, 138, 122, 121, 122, 121, 167, 151, 183, 140, 151, 183, 140,
        140, 140,
        // coeff_abs_level_greater1_flag
        154, 196, 167, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
        136, 122, 169, 208, 166, 167, 154, 152, 167, 182,
        // coeff_abs_level_greater2_flag
        107, 167,  91, 107, 107, 167,
        // log2_res_scale_abs
        154, 154, 154, 154, 154, 154, 154, 154,
        // res_scale_sign_flag
        154, 154,
        // cu_chroma_qp_offset_flag
        154,
        // cu_chroma_qp_offset_idx
        154,
    ],
];

/// Per-syntax-element offsets and bin counts into [`INIT_VALUES`] /
/// `CabacContexts::state`. Mirrors the `_OFFSET` enum FFmpeg builds from its
/// `CABAC_ELEMS` macro.
///
/// Syntax elements that are pure-bypass or terminate-only (e.g. `mpm_idx`,
/// `coeff_sign_flag`, `end_of_slice_flag`, the various SAO offset bits) have
/// no entry here because they don't consume CABAC contexts.
pub mod ctx {
    pub const SAO_MERGE_FLAG: usize = 0;
    pub const SAO_MERGE_FLAG_LEN: usize = 1;

    pub const SAO_TYPE_IDX: usize = 1;
    pub const SAO_TYPE_IDX_LEN: usize = 1;

    pub const SPLIT_CODING_UNIT_FLAG: usize = 2;
    pub const SPLIT_CODING_UNIT_FLAG_LEN: usize = 3;

    pub const CU_TRANSQUANT_BYPASS_FLAG: usize = 5;
    pub const CU_TRANSQUANT_BYPASS_FLAG_LEN: usize = 1;

    pub const SKIP_FLAG: usize = 6;
    pub const SKIP_FLAG_LEN: usize = 3;

    pub const CU_QP_DELTA: usize = 9;
    pub const CU_QP_DELTA_LEN: usize = 3;

    pub const PRED_MODE_FLAG: usize = 12;
    pub const PRED_MODE_FLAG_LEN: usize = 1;

    pub const PART_MODE: usize = 13;
    pub const PART_MODE_LEN: usize = 4;

    pub const PREV_INTRA_LUMA_PRED_FLAG: usize = 17;
    pub const PREV_INTRA_LUMA_PRED_FLAG_LEN: usize = 1;

    pub const INTRA_CHROMA_PRED_MODE: usize = 18;
    pub const INTRA_CHROMA_PRED_MODE_LEN: usize = 2;

    pub const MERGE_FLAG: usize = 20;
    pub const MERGE_FLAG_LEN: usize = 1;

    pub const MERGE_IDX: usize = 21;
    pub const MERGE_IDX_LEN: usize = 1;

    pub const INTER_PRED_IDC: usize = 22;
    pub const INTER_PRED_IDC_LEN: usize = 5;

    pub const REF_IDX_L0: usize = 27;
    pub const REF_IDX_L0_LEN: usize = 2;

    pub const REF_IDX_L1: usize = 29;
    pub const REF_IDX_L1_LEN: usize = 2;

    pub const ABS_MVD_GREATER0_FLAG: usize = 31;
    pub const ABS_MVD_GREATER0_FLAG_LEN: usize = 2;

    pub const ABS_MVD_GREATER1_FLAG: usize = 33;
    pub const ABS_MVD_GREATER1_FLAG_LEN: usize = 2;

    pub const MVP_LX_FLAG: usize = 35;
    pub const MVP_LX_FLAG_LEN: usize = 1;

    pub const NO_RESIDUAL_DATA_FLAG: usize = 36;
    pub const NO_RESIDUAL_DATA_FLAG_LEN: usize = 1;

    pub const SPLIT_TRANSFORM_FLAG: usize = 37;
    pub const SPLIT_TRANSFORM_FLAG_LEN: usize = 3;

    pub const CBF_LUMA: usize = 40;
    pub const CBF_LUMA_LEN: usize = 2;

    pub const CBF_CB_CR: usize = 42;
    pub const CBF_CB_CR_LEN: usize = 5;

    pub const TRANSFORM_SKIP_FLAG: usize = 47;
    pub const TRANSFORM_SKIP_FLAG_LEN: usize = 2;

    pub const EXPLICIT_RDPCM_FLAG: usize = 49;
    pub const EXPLICIT_RDPCM_FLAG_LEN: usize = 2;

    pub const EXPLICIT_RDPCM_DIR_FLAG: usize = 51;
    pub const EXPLICIT_RDPCM_DIR_FLAG_LEN: usize = 2;

    pub const LAST_SIGNIFICANT_COEFF_X_PREFIX: usize = 53;
    pub const LAST_SIGNIFICANT_COEFF_X_PREFIX_LEN: usize = 18;

    pub const LAST_SIGNIFICANT_COEFF_Y_PREFIX: usize = 71;
    pub const LAST_SIGNIFICANT_COEFF_Y_PREFIX_LEN: usize = 18;

    pub const SIGNIFICANT_COEFF_GROUP_FLAG: usize = 89;
    pub const SIGNIFICANT_COEFF_GROUP_FLAG_LEN: usize = 4;

    pub const SIGNIFICANT_COEFF_FLAG: usize = 93;
    pub const SIGNIFICANT_COEFF_FLAG_LEN: usize = 44;

    pub const COEFF_ABS_LEVEL_GREATER1_FLAG: usize = 137;
    pub const COEFF_ABS_LEVEL_GREATER1_FLAG_LEN: usize = 24;

    pub const COEFF_ABS_LEVEL_GREATER2_FLAG: usize = 161;
    pub const COEFF_ABS_LEVEL_GREATER2_FLAG_LEN: usize = 6;

    pub const LOG2_RES_SCALE_ABS: usize = 167;
    pub const LOG2_RES_SCALE_ABS_LEN: usize = 8;

    pub const RES_SCALE_SIGN_FLAG: usize = 175;
    pub const RES_SCALE_SIGN_FLAG_LEN: usize = 2;

    pub const CU_CHROMA_QP_OFFSET_FLAG: usize = 177;
    pub const CU_CHROMA_QP_OFFSET_FLAG_LEN: usize = 1;

    pub const CU_CHROMA_QP_OFFSET_IDX: usize = 178;
    pub const CU_CHROMA_QP_OFFSET_IDX_LEN: usize = 1;
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Both `INIT_VALUES` rows must contain exactly `HEVC_CONTEXTS` entries.
    /// (`[u8; HEVC_CONTEXTS]` already enforces this at compile time, so this
    /// is just defense in depth — it'll fail to compile if the table is
    /// resized accidentally.)
    #[test]
    fn test_init_values_shape() {
        for row in &INIT_VALUES {
            assert_eq!(row.len(), HEVC_CONTEXTS);
        }
    }

    /// Verify offsets cover the table without overlap and end exactly at
    /// `HEVC_CONTEXTS`. This catches transcription errors in the `ctx` module
    /// (the most likely source of subtle off-by-ones).
    #[test]
    fn test_ctx_offsets_partition_table() {
        // Pairs of (offset, length) in the order they appear in the table.
        let elements: &[(usize, usize)] = &[
            (ctx::SAO_MERGE_FLAG, ctx::SAO_MERGE_FLAG_LEN),
            (ctx::SAO_TYPE_IDX, ctx::SAO_TYPE_IDX_LEN),
            (ctx::SPLIT_CODING_UNIT_FLAG, ctx::SPLIT_CODING_UNIT_FLAG_LEN),
            (
                ctx::CU_TRANSQUANT_BYPASS_FLAG,
                ctx::CU_TRANSQUANT_BYPASS_FLAG_LEN,
            ),
            (ctx::SKIP_FLAG, ctx::SKIP_FLAG_LEN),
            (ctx::CU_QP_DELTA, ctx::CU_QP_DELTA_LEN),
            (ctx::PRED_MODE_FLAG, ctx::PRED_MODE_FLAG_LEN),
            (ctx::PART_MODE, ctx::PART_MODE_LEN),
            (
                ctx::PREV_INTRA_LUMA_PRED_FLAG,
                ctx::PREV_INTRA_LUMA_PRED_FLAG_LEN,
            ),
            (ctx::INTRA_CHROMA_PRED_MODE, ctx::INTRA_CHROMA_PRED_MODE_LEN),
            (ctx::MERGE_FLAG, ctx::MERGE_FLAG_LEN),
            (ctx::MERGE_IDX, ctx::MERGE_IDX_LEN),
            (ctx::INTER_PRED_IDC, ctx::INTER_PRED_IDC_LEN),
            (ctx::REF_IDX_L0, ctx::REF_IDX_L0_LEN),
            (ctx::REF_IDX_L1, ctx::REF_IDX_L1_LEN),
            (ctx::ABS_MVD_GREATER0_FLAG, ctx::ABS_MVD_GREATER0_FLAG_LEN),
            (ctx::ABS_MVD_GREATER1_FLAG, ctx::ABS_MVD_GREATER1_FLAG_LEN),
            (ctx::MVP_LX_FLAG, ctx::MVP_LX_FLAG_LEN),
            (ctx::NO_RESIDUAL_DATA_FLAG, ctx::NO_RESIDUAL_DATA_FLAG_LEN),
            (ctx::SPLIT_TRANSFORM_FLAG, ctx::SPLIT_TRANSFORM_FLAG_LEN),
            (ctx::CBF_LUMA, ctx::CBF_LUMA_LEN),
            (ctx::CBF_CB_CR, ctx::CBF_CB_CR_LEN),
            (ctx::TRANSFORM_SKIP_FLAG, ctx::TRANSFORM_SKIP_FLAG_LEN),
            (ctx::EXPLICIT_RDPCM_FLAG, ctx::EXPLICIT_RDPCM_FLAG_LEN),
            (
                ctx::EXPLICIT_RDPCM_DIR_FLAG,
                ctx::EXPLICIT_RDPCM_DIR_FLAG_LEN,
            ),
            (
                ctx::LAST_SIGNIFICANT_COEFF_X_PREFIX,
                ctx::LAST_SIGNIFICANT_COEFF_X_PREFIX_LEN,
            ),
            (
                ctx::LAST_SIGNIFICANT_COEFF_Y_PREFIX,
                ctx::LAST_SIGNIFICANT_COEFF_Y_PREFIX_LEN,
            ),
            (
                ctx::SIGNIFICANT_COEFF_GROUP_FLAG,
                ctx::SIGNIFICANT_COEFF_GROUP_FLAG_LEN,
            ),
            (ctx::SIGNIFICANT_COEFF_FLAG, ctx::SIGNIFICANT_COEFF_FLAG_LEN),
            (
                ctx::COEFF_ABS_LEVEL_GREATER1_FLAG,
                ctx::COEFF_ABS_LEVEL_GREATER1_FLAG_LEN,
            ),
            (
                ctx::COEFF_ABS_LEVEL_GREATER2_FLAG,
                ctx::COEFF_ABS_LEVEL_GREATER2_FLAG_LEN,
            ),
            (ctx::LOG2_RES_SCALE_ABS, ctx::LOG2_RES_SCALE_ABS_LEN),
            (ctx::RES_SCALE_SIGN_FLAG, ctx::RES_SCALE_SIGN_FLAG_LEN),
            (
                ctx::CU_CHROMA_QP_OFFSET_FLAG,
                ctx::CU_CHROMA_QP_OFFSET_FLAG_LEN,
            ),
            (
                ctx::CU_CHROMA_QP_OFFSET_IDX,
                ctx::CU_CHROMA_QP_OFFSET_IDX_LEN,
            ),
        ];
        let mut expected = 0usize;
        for &(offset, len) in elements {
            assert_eq!(offset, expected, "non-contiguous offsets at {offset}");
            expected += len;
        }
        assert_eq!(expected, HEVC_CONTEXTS);
    }

    /// Spot check: known I-slice initial values for the first `split_cu_flag`
    /// context (init_value 139). This protects against transcription errors
    /// in the table itself.
    #[test]
    fn test_split_cu_flag_init_value() {
        assert_eq!(INIT_VALUES[0][ctx::SPLIT_CODING_UNIT_FLAG], 139);
        assert_eq!(INIT_VALUES[0][ctx::SPLIT_CODING_UNIT_FLAG + 1], 141);
        assert_eq!(INIT_VALUES[0][ctx::SPLIT_CODING_UNIT_FLAG + 2], 157);
    }

    /// Spot check: `last_significant_coeff_x_prefix[17] = 63` (last entry
    /// of an 18-element block) for I-slice. Catches off-by-ones at the
    /// 53..71 region boundary.
    #[test]
    fn test_last_sig_coeff_x_prefix_tail() {
        assert_eq!(
            INIT_VALUES[0][ctx::LAST_SIGNIFICANT_COEFF_X_PREFIX
                + ctx::LAST_SIGNIFICANT_COEFF_X_PREFIX_LEN
                - 1],
            63
        );
    }

    /// Spot check: the cbf_luma section is 2 contexts at offset 40 with
    /// values [111, 141] for I-slice.
    #[test]
    fn test_cbf_luma_init_values() {
        assert_eq!(INIT_VALUES[0][ctx::CBF_LUMA], 111);
        assert_eq!(INIT_VALUES[0][ctx::CBF_LUMA + 1], 141);
    }
}