rust-j2k 0.5.0

Decodes JPEG 2000 images in pure Rust, for programs that need to read them without linking a C library.
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
//! The HTJ2K block coder (Rec. ITU-T T.814 | ISO/IEC 15444-15, clause 7).
//!
//! An HT code-block codes its magnitudes in one cleanup pass, optionally
//! followed by a SigProp and a MagRef pass that refine one more bit-plane.
//!
//! The cleanup segment holds three byte-streams. MagSgn runs forward from the
//! start; MEL runs forward from `Pcup`; VLC runs backward from the end. The
//! block is scanned in 2x2 quads, row-pairs at a time, and each quad's
//! significance, exponent bound and magnitudes come from those three streams
//! (7.3). The vertically causal context flag changes only the SigProp pass
//! (7.4), so the cleanup pass ignores it.
//!
//! The refinement segment holds two more: SigProp runs forward from its
//! start, MagRef backward from its end. Both passes scan the block in
//! four-line stripes, as the Part 1 coder does. SigProp finds the samples
//! that become significant in the next bit-plane and reads their signs
//! (7.4); MagRef reads that bit-plane for the samples the cleanup pass found
//! significant (7.5).
//!
//! The rest of tier-1 reaches this module only through
//! [`BlockCoder`](super::BlockCoder): [`decode`] and [`passes_run`].

#[cfg(test)]
mod tests;
mod vlc_tables;

use super::ebcot::passes::undo_maxshift;
use crate::tier2::CodeBlock;
use crate::{Error, Result};
use vlc_tables::{CXT_VLC_TABLE_0, CXT_VLC_TABLE_1};

/// The most `Mb + s - P` a block can have for the double-scale reconstruction
/// to fit `i32`, with `P` its zero bit-planes (`Mb` when there is no maxshift
/// and `P = 0`). The cleanup pass codes the top `P + 1` planes, so
/// `Mb + s - P` is one more than the cleanup plane's shift: this bounds that
/// shift, not a count of coded planes. The Part 1 coder limits the same
/// quantity, where it is the count of coded planes (`top_coded_plane`).
const MAX_BIT_PLANES: u32 = 30;

/// The largest suffix length `Scup` (7.1.1).
const MAX_SCUP: usize = 4079;

/// One more than the largest cleanup segment length `Lcup` (7.1.1).
const MAX_LCUP: usize = 65535;

/// The most coding passes of one HT set: cleanup, SigProp and MagRef (B.1).
const HT_SET_PASSES: u32 = 3;

fn malformed(what: &str) -> Error {
    Error::codestream(format!("HT code-block {what}"))
}

/// The tile-component's parameters an HT code-block decode needs beyond the
/// block and its subband's `Mb`.
#[derive(Debug, Clone, Copy, Default)]
pub(super) struct Params {
    /// `SPrgn`, the region-of-interest maxshift `s` (ISO/IEC 15444-1
    /// Annex H); 0 without one.
    pub roi_shift: u8,
    /// The vertically causal context flag, which keeps the SigProp pass from
    /// looking at the next stripe (7.4).
    pub causal: bool,
    /// The bound on the cleanup magnitudes that `CAP` signals for this
    /// subband (A.3.7, 8.7.3), as a bit count: every `mu` is below
    /// `2^bits`. `None` without a bound.
    pub bound_bits: Option<u32>,
}

/// Decode one HT code-block into its coefficients, row-major over the block,
/// at twice their weight like the Part 1 coder.
///
/// `numbps` is the subband's magnitude bit-plane count `Mb`. A maxshift
/// `s` raises the block's count to `Mb + s` (Annex H, H-3), and the decode
/// runs on that count. The cleanup pass codes the top `P + 1` of those
/// magnitude bit-planes, `P` being the block's zero bit-planes (`S_blk`,
/// 7.6), so a significant sample of magnitude `mu` reconstructs to
/// `(2 mu + 1) << (Mb + s - P - 1)`: `mu` at its bit-planes, plus half of
/// the first plane not coded. The SigProp and MagRef passes code one plane
/// more for the samples they reach, which then reconstruct with half of the
/// plane below that. OpenJPEG's `opj_t1_ht_decode_cblk` reconstructs the
/// same way.
///
/// Last, the maxshift is undone (7.6 hands the samples to Annex H) exactly
/// as the Part 1 coder undoes it, by H.1: a magnitude at or above `2^(s + 1)`
/// at double scale is region and comes down `s` planes; one below is
/// background and stays (see `undo_maxshift`).
pub(super) fn decode(block: &CodeBlock<'_>, numbps: u32, params: Params) -> Result<Vec<i32>> {
    let mut scratch = Scratch::default();
    decode_into(block, numbps, params, &mut scratch)?;
    Ok(scratch.coeffs)
}

/// The buffers one HT code-block's decode uses, kept from block to block so
/// a subband of many small blocks allocates them once. [`decode_into`]
/// clears each one before a block uses it, and gives the coefficients a new
/// buffer.
#[derive(Debug, Default)]
pub(super) struct Scratch {
    coeffs: Vec<i32>,
    cleanup: CleanupScratch,
    refinement: RefinementScratch,
}

/// The cleanup pass's significance and magnitude exponents, by quad-scan
/// index.
#[derive(Debug, Default)]
struct CleanupScratch {
    sigma: Vec<bool>,
    exps: Vec<u32>,
}

/// The refinement passes' buffers, row-major over the block.
#[derive(Debug, Default)]
struct RefinementScratch {
    /// The samples the cleanup pass made significant.
    sigma: Vec<bool>,
    /// The samples SigProp made significant.
    refined: Vec<bool>,
    /// The samples of one column-group SigProp makes significant.
    new: Vec<usize>,
}

/// Empty `buffer` and refill it with `len` default values, keeping its
/// allocation.
fn zeroed<T: Clone + Default>(buffer: &mut Vec<T>, len: usize) {
    buffer.clear();
    buffer.resize(len, T::default());
}

/// [`decode`] into `scratch`, whose coefficients hold the block until its
/// next decode.
pub(super) fn decode_into<'s>(
    block: &CodeBlock<'_>,
    numbps: u32,
    params: Params,
    scratch: &'s mut Scratch,
) -> Result<&'s [i32]> {
    fill(block, numbps, params, scratch)?;
    Ok(&scratch.coeffs)
}

/// The body of [`decode_into`].
fn fill(block: &CodeBlock<'_>, numbps: u32, params: Params, scratch: &mut Scratch) -> Result<()> {
    let (width, height) = (block.width, block.height);
    // The coefficients get a fresh buffer for each block, unlike the other
    // buffers: reusing it measured 1 to 2% slower on HT files of 64x64
    // blocks, likely from where the long-lived buffers fall in memory.
    scratch.coeffs = vec![0i32; width * height];
    let Scratch {
        coeffs,
        cleanup,
        refinement: refine,
    } = scratch;
    // `M'b = Mb + s` (H-3): the zero bit-planes count down from it.
    let planes = numbps + u32::from(params.roi_shift);
    // More passes would be a second HT set, which only a MULTIHT codestream
    // holds (8.3), and the `CAP` parse refuses those. So a count past one set
    // is a damaged one. OpenJPEG refuses it too.
    if block.num_passes > HT_SET_PASSES {
        return Err(malformed(&format!(
            "has {} coding passes, more than the {HT_SET_PASSES} of one HT set",
            block.num_passes
        )));
    }
    if block.num_passes == 0 || planes == 0 {
        return Ok(());
    }
    let zbp = block.zero_bit_planes;
    // At least one bit-plane must be left to code; OpenJPEG refuses the
    // same blocks.
    if zbp >= planes {
        return Err(malformed(&format!(
            "has {zbp} zero bit-planes in a block of {planes}, leaving none to code"
        )));
    }
    // `Mb + s - P` bounds the cleanup plane's shift, as `top_coded_plane`
    // does in the Part 1 coder; `Mb + s` alone does not, since a block with
    // many zero bit-planes keeps the shift small under any maxshift. The
    // per-sample `try_from` in the cleanup pass still refuses any magnitude
    // that does not fit.
    let coded = planes - zbp;
    if coded > MAX_BIT_PLANES {
        return Err(Error::limit_fixed(
            crate::LimitKind::BitPlanes,
            if params.roi_shift == 0 {
                format!(
                    "HT code-block of {coded} bit-planes (a subband of {numbps} and {zbp} \
                 zero bit-planes), over the {MAX_BIT_PLANES}-plane limit"
                )
            } else {
                format!(
                    "HT code-block of {coded} bit-planes (a subband of {numbps}, a \
                 maxshift of {} and {zbp} zero bit-planes), over the \
                 {MAX_BIT_PLANES}-plane limit",
                    params.roi_shift
                )
            },
        ));
    }
    let data = block
        .segments
        .first()
        .map(|s| s.bytes())
        .unwrap_or_default();
    let lcup = data.len();
    if !(2..MAX_LCUP).contains(&lcup) {
        return Err(malformed(&format!(
            "cleanup segment is {lcup} bytes, outside 2..{MAX_LCUP}"
        )));
    }
    let scup = 16 * usize::from(data[lcup - 1]) + usize::from(data[lcup - 2] & 0x0F);
    if !(2..=lcup.min(MAX_SCUP)).contains(&scup) {
        return Err(malformed(&format!(
            "suffix length Scup = {scup} is outside 2..=min(Lcup = {lcup}, {MAX_SCUP})"
        )));
    }
    let seg = Segment {
        data: &data,
        pcup: lcup - scup,
    };
    let shift = planes - zbp - 1;
    Cleanup {
        seg,
        magsgn: MagSgnReader::new(),
        mel: MelDecoder::new(seg.pcup),
        vlc: VlcReader::new(&seg),
        width,
        height,
        qw: width.div_ceil(2),
        // `mu` has at most `P + 1` bits (7.6), so its exponent, the bit
        // length of `2 mu - 1`, is at most `P + 2`. OpenJPEG has the same
        // bound. The cap at `MAX_BIT_PLANES + 2` keeps the shifts below in
        // range when `P` is large under a maxshift; a magnitude that fits
        // `i32` has an exponent of at most 31.
        max_u: (zbp + 2).min(MAX_BIT_PLANES + 2),
        // A bound of 64 bits or more holds of every `u64`.
        bound_bits: params.bound_bits.filter(|&bits| bits < u64::BITS),
        shift,
    }
    .run(coeffs, cleanup)?;
    let z_blk = z_blk(block, planes);
    if z_blk > 1 {
        let refinement = block.segments.get(1).map(|s| s.bytes()).unwrap_or_default();
        Refinement {
            width,
            height,
            causal: params.causal,
            shift,
        }
        .run(&refinement, z_blk == HT_SET_PASSES, coeffs, refine)?;
    }
    if params.roi_shift != 0 {
        for c in coeffs.iter_mut() {
            let magnitude = undo_maxshift(c.abs(), params.roi_shift);
            *c = if *c < 0 { -magnitude } else { magnitude };
        }
    }
    Ok(())
}

/// The coding passes [`decode`] runs on `block` in a subband of `numbps`
/// magnitude bit-planes, under a maxshift of `roi_shift`. See [`z_blk`].
pub(super) fn passes_run(block: &CodeBlock<'_>, numbps: u32, roi_shift: u8) -> u64 {
    u64::from(z_blk(block, numbps + u32::from(roi_shift)))
}

/// The passes of `block` to decode: `Z_blk` (B.3), but only the cleanup pass
/// where a refinement pass would code below the block's last bit-plane.
///
/// `Z_blk` is the block's passes, except that a refinement segment of zero
/// length leaves the cleanup pass alone. A block whose cleanup pass already
/// reaches the last of its `numbps` planes (`Mb`, or `Mb + s` under a
/// maxshift), `P = numbps - 1`, has no plane left to refine: a refinement
/// pass would give it `Nb = numbps + 1` planes (7.6). OpenJPEG decodes its
/// cleanup pass alone, with a warning, and so does this decoder.
fn z_blk(block: &CodeBlock<'_>, numbps: u32) -> u32 {
    let passes = block.num_passes.min(HT_SET_PASSES);
    let refined = block
        .segments
        .get(1)
        .is_some_and(|s| s.chunks.iter().any(|c| !c.is_empty()));
    let plane_left = numbps >= block.zero_bit_planes.saturating_add(2);
    if passes > 1 && refined && plane_left {
        passes
    } else {
        passes.min(1)
    }
}

/// The cleanup segment `Dcup`, with `Pcup`, the length of its MagSgn part.
#[derive(Clone, Copy)]
struct Segment<'a> {
    data: &'a [u8],
    pcup: usize,
}

impl Segment<'_> {
    /// `modDcup` (7.1.1): the segment with its last byte and the low nibble of
    /// the one before it, which held `Scup`, read as ones.
    fn byte(&self, pos: usize) -> u8 {
        let lcup = self.data.len();
        if pos == lcup - 1 {
            0xFF
        } else if pos == lcup - 2 {
            self.data[pos] | 0x0F
        } else {
            self.data[pos]
        }
    }
}

/// The MagSgn bit-stream: forward from byte 0, least significant bit first,
/// with a stuffing bit after every 0xFF (`importMagSgnBit`, 7.1.2).
struct MagSgnReader {
    pos: usize,
    bits: u32,
    tmp: u32,
    last: u32,
}

impl MagSgnReader {
    fn new() -> Self {
        MagSgnReader {
            pos: 0,
            bits: 0,
            tmp: 0,
            last: 0,
        }
    }

    fn bit(&mut self, seg: &Segment<'_>) -> Result<u32> {
        if self.bits == 0 {
            self.bits = if self.last == 0xFF { 7 } else { 8 };
            if self.pos < seg.pcup {
                self.tmp = u32::from(seg.byte(self.pos));
                if self.tmp & (1 << self.bits) != 0 {
                    return Err(malformed("MagSgn byte after 0xFF has its stuffing bit set"));
                }
            } else if self.pos == seg.pcup {
                // One 0xFF byte the encoder may have dropped is implied.
                self.tmp = 0xFF;
            } else {
                return Err(malformed("MagSgn stream runs past its end"));
            }
            self.last = self.tmp;
            self.pos += 1;
        }
        let bit = self.tmp & 1;
        self.tmp >>= 1;
        self.bits -= 1;
        Ok(bit)
    }

    /// `decodeMagSgnValue` (7.3.8): `m` bits, least significant first.
    fn value(&mut self, seg: &Segment<'_>, m: u32) -> Result<u64> {
        let mut val = 0u64;
        for i in 0..m {
            val |= u64::from(self.bit(seg)?) << i;
        }
        Ok(val)
    }
}

/// The MEL bit-stream and its run-length decoder (`importMELBit`, 7.1.3, and
/// `decodeMELSym`, 7.3.3).
struct MelDecoder {
    pos: usize,
    bits: u32,
    tmp: u32,
    k: usize,
    run: u32,
    one: bool,
}

/// `MEL_E` (Table 2): the run-length exponent of each MEL state.
const MEL_E: [u32; 13] = [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5];

impl MelDecoder {
    fn new(pcup: usize) -> Self {
        MelDecoder {
            pos: pcup,
            bits: 0,
            tmp: 0,
            k: 0,
            run: 0,
            one: false,
        }
    }

    /// Most significant bit first, with a stuffing bit after every 0xFF;
    /// past the end of the segment the stream reads as ones.
    fn bit(&mut self, seg: &Segment<'_>) -> u32 {
        if self.bits == 0 {
            self.bits = if self.tmp == 0xFF { 7 } else { 8 };
            if self.pos < seg.data.len() {
                self.tmp = u32::from(seg.byte(self.pos));
                self.pos += 1;
            } else {
                self.tmp = 0xFF;
            }
        }
        self.bits -= 1;
        (self.tmp >> self.bits) & 1
    }

    fn symbol(&mut self, seg: &Segment<'_>) -> u32 {
        if self.run == 0 && !self.one {
            let mut eval = MEL_E[self.k];
            if self.bit(seg) == 1 {
                self.run = 1 << eval;
                self.k = (self.k + 1).min(12);
            } else {
                self.run = 0;
                while eval > 0 {
                    self.run = 2 * self.run + self.bit(seg);
                    eval -= 1;
                }
                self.k = self.k.saturating_sub(1);
                self.one = true;
            }
        }
        if self.run > 0 {
            self.run -= 1;
            0
        } else {
            self.one = false;
            1
        }
    }
}

/// The VLC bit-stream: backward from byte `Lcup - 2`, least significant bit
/// first, skipping the stuffing bit of a byte whose low seven bits are ones
/// after a byte above 0x8F (`importVLCBit`, 7.1.4).
struct VlcReader {
    /// The next byte to read; below `Pcup` (or below 0) the stream is spent.
    pos: Option<usize>,
    bits: u32,
    tmp: u32,
    last: u32,
}

impl VlcReader {
    fn new(seg: &Segment<'_>) -> Self {
        let lcup = seg.data.len();
        let last = u32::from(seg.byte(lcup - 2));
        let tmp = last >> 4;
        VlcReader {
            pos: lcup.checked_sub(3),
            bits: if tmp & 7 < 7 { 4 } else { 3 },
            tmp,
            last,
        }
    }

    fn bit(&mut self, seg: &Segment<'_>) -> Result<u32> {
        if self.bits == 0 {
            let pos = self
                .pos
                .filter(|&pos| pos >= seg.pcup)
                .ok_or_else(|| malformed("VLC stream runs into the MagSgn bytes"))?;
            self.tmp = u32::from(seg.byte(pos));
            self.bits = if self.last > 0x8F && self.tmp & 0x7F == 0x7F {
                7
            } else {
                8
            };
            self.last = self.tmp;
            self.pos = pos.checked_sub(1);
        }
        let bit = self.tmp & 1;
        self.tmp >>= 1;
        self.bits -= 1;
        Ok(bit)
    }

    /// `n` bits, least significant first.
    fn bits(&mut self, seg: &Segment<'_>, n: u32) -> Result<u32> {
        let mut val = 0;
        for i in 0..n {
            val |= self.bit(seg)? << i;
        }
        Ok(val)
    }

    /// `decodeUPrefix` (7.3.6).
    fn u_prefix(&mut self, seg: &Segment<'_>) -> Result<u32> {
        if self.bit(seg)? == 1 {
            return Ok(1);
        }
        if self.bit(seg)? == 1 {
            return Ok(2);
        }
        Ok(if self.bit(seg)? == 1 { 3 } else { 5 })
    }

    /// `decodeUSuffix` (7.3.6).
    fn u_suffix(&mut self, seg: &Segment<'_>, prefix: u32) -> Result<u32> {
        match prefix {
            0..3 => Ok(0),
            3 => self.bit(seg),
            _ => self.bits(seg, 5),
        }
    }

    /// `decodeUExtension` (7.3.6).
    fn u_extension(&mut self, seg: &Segment<'_>, suffix: u32) -> Result<u32> {
        if suffix < 28 {
            Ok(0)
        } else {
            self.bits(seg, 4)
        }
    }
}

/// One quad's decoded CxtVLC tuple `(rho, u_off, eps_k, eps_1)`.
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
struct QuadInfo {
    rho: u8,
    u_off: bool,
    e_k: u8,
    e_1: u8,
}

/// A CxtVLC table as a lookup: `[c][len][cwd]` holds one more than the index
/// of the row whose `len`-bit codeword is `cwd` in context `c`, or 0.
type VlcLookup = [[[u16; 128]; 8]; 8];

const fn lookup(table: &[[u8; 7]]) -> VlcLookup {
    let mut out = [[[0u16; 128]; 8]; 8];
    let mut i = 0;
    while i < table.len() {
        let [c, _, _, _, _, w, l_w] = table[i];
        out[c as usize][l_w as usize][w as usize] = i as u16 + 1;
        i += 1;
    }
    out
}

static VLC_LOOKUP_0: VlcLookup = lookup(&CXT_VLC_TABLE_0);
static VLC_LOOKUP_1: VlcLookup = lookup(&CXT_VLC_TABLE_1);

/// `decodeCxtVLC` (7.3.5): read bits until they spell a codeword of context
/// `c` in the table of the first row of quads or of a later one.
fn decode_cxt_vlc(
    vlc: &mut VlcReader,
    seg: &Segment<'_>,
    first_row: bool,
    c: u8,
) -> Result<QuadInfo> {
    let (table, lookup): (&[[u8; 7]], &VlcLookup) = if first_row {
        (&CXT_VLC_TABLE_0, &VLC_LOOKUP_0)
    } else {
        (&CXT_VLC_TABLE_1, &VLC_LOOKUP_1)
    };
    let mut cwd = 0usize;
    // Entry 0 of each context is for zero-bit codewords, which no row has.
    for (len, codewords) in lookup[usize::from(c)].iter().enumerate().skip(1) {
        cwd |= (vlc.bit(seg)? as usize) << (len - 1);
        let hit = codewords[cwd];
        if hit != 0 {
            let [_, rho, u_off, e_k, e_1, _, _] = table[usize::from(hit - 1)];
            return Ok(QuadInfo {
                rho,
                u_off: u_off == 1,
                e_k,
                e_1,
            });
        }
    }
    Err(malformed(&format!(
        "VLC bits match no CxtVLC codeword of context {c}"
    )))
}

/// The cleanup pass of one block: its three streams and its geometry.
struct Cleanup<'a> {
    seg: Segment<'a>,
    magsgn: MagSgnReader,
    mel: MelDecoder,
    vlc: VlcReader,
    width: usize,
    height: usize,
    /// Quads per row, `ceil(width / 2)`.
    qw: usize,
    /// The largest exponent bound `U_q` a quad may have.
    max_u: u32,
    /// The `CAP` bound on `mu` as a bit count below 64, if there is one.
    bound_bits: Option<u32>,
    /// The left shift from `2 mu + 1` to the double-scale coefficient.
    shift: u32,
}

impl Cleanup<'_> {
    /// Decode every quad into `coeffs`, row-major over the block, with
    /// `scratch`'s buffers.
    fn run(mut self, coeffs: &mut [i32], scratch: &mut CleanupScratch) -> Result<()> {
        let qw = self.qw;
        let qh = self.height.div_ceil(2);
        // Significance and magnitude exponent of every sample, by its
        // quad-scan index `n = 4q + j` (7.2): `j` is 0 top-left, 1
        // bottom-left, 2 top-right, 3 bottom-right. Both start cleared:
        // only significant samples write an exponent.
        let CleanupScratch { sigma, exps } = scratch;
        zeroed(sigma, 4 * qw * qh);
        zeroed(exps, 4 * qw * qh);
        let (sigma, exps) = (sigma.as_mut_slice(), exps.as_mut_slice());
        for qy in 0..qh {
            let first_row = qy == 0;
            for qx in (0..qw).step_by(2) {
                let q1 = qy * qw + qx;
                // With an odd `QW` the last pair of a row has one quad.
                let quads = [q1, q1 + 1];
                let pair = &quads[..if qx + 1 < qw { 2 } else { 1 }];
                let mut info = [QuadInfo::default(); 2];
                for (slot, &q) in pair.iter().enumerate() {
                    let c = self.context(sigma, q);
                    info[slot] = self.sig_emb(first_row, c)?;
                    for j in 0..4 {
                        let on = info[slot].rho >> j & 1 == 1;
                        if on && !self.inside(q, j) {
                            return Err(malformed("codes a significant sample outside the block"));
                        }
                        sigma[4 * q + j] = on;
                    }
                }
                let u = self.residuals(first_row, &info[..pair.len()])?;
                for (slot, &q) in pair.iter().enumerate() {
                    let kappa = if first_row {
                        1
                    } else {
                        self.predictor(exps, q, info[slot].rho)
                    };
                    let bound = kappa + u[slot];
                    if bound > self.max_u {
                        // `max_u` is `P + 2`, at most the cap; the cap's message also holds
                        // when `P` is exactly 30 and nothing was capped.
                        return Err(malformed(&if self.max_u == MAX_BIT_PLANES + 2 {
                            format!(
                                "exponent bound U_q = {bound} exceeds {}, the most a block can code (zero \
                                 bit-planes + 2, at most {0})",
                                self.max_u
                            )
                        } else {
                            format!(
                                "exponent bound U_q = {bound} exceeds zero bit-planes + 2 = {}",
                                self.max_u
                            )
                        }));
                    }
                    self.magnitudes(q, info[slot], bound, exps, coeffs)?;
                }
            }
        }
        Ok(())
    }

    /// Whether sample `j` of quad `q` lies inside the block.
    fn inside(&self, q: usize, j: usize) -> bool {
        let x = 2 * (q % self.qw) + (j >> 1);
        let y = 2 * (q / self.qw) + (j & 1);
        x < self.width && y < self.height
    }

    /// The context `c_q` of quad `q` from its decoded neighbors (7.3.5).
    fn context(&self, sigma: &[bool], q: usize) -> u8 {
        let qw = self.qw;
        let s = |n: usize| u8::from(sigma[n]);
        // The quad to the left, in the same row: its right column.
        let (w, sw) = if !q.is_multiple_of(qw) {
            (s(4 * q - 2), s(4 * q - 1))
        } else {
            (0, 0)
        };
        if q < qw {
            let (f, sf) = if q > 0 {
                (s(4 * q - 4), s(4 * q - 3))
            } else {
                (0, 0)
            };
            (f | sf) + 2 * w + 4 * sw
        } else {
            let up = 4 * (q - qw);
            let n = s(up + 1);
            let ne = s(up + 3);
            let nw = if !q.is_multiple_of(qw) { s(up - 1) } else { 0 };
            let nf = if !(q + 1).is_multiple_of(qw) {
                s(up + 5)
            } else {
                0
            };
            (nw | n) + 2 * (w | sw) + 4 * (ne | nf)
        }
    }

    /// `decodeSigEMB` (7.3.5): an all-zero-context quad first asks MEL
    /// whether it is significant at all.
    fn sig_emb(&mut self, first_row: bool, c: u8) -> Result<QuadInfo> {
        if c == 0 && self.mel.symbol(&self.seg) == 0 {
            return Ok(QuadInfo::default());
        }
        decode_cxt_vlc(&mut self.vlc, &self.seg, first_row, c)
    }

    /// The unsigned residuals `u_q` of a quad-pair (7.3.4, 7.3.6): prefixes,
    /// then suffixes, then extensions, first quad before second.
    fn residuals(&mut self, first_row: bool, info: &[QuadInfo]) -> Result<[u32; 2]> {
        let seg = self.seg;
        let both = info.len() == 2 && info[0].u_off && info[1].u_off;
        let mut prefix = [0u32; 2];
        let mut offset = 0;
        if first_row && both {
            if self.mel.symbol(&seg) == 1 {
                offset = 2;
                prefix = [self.vlc.u_prefix(&seg)?, self.vlc.u_prefix(&seg)?];
            } else {
                prefix[0] = self.vlc.u_prefix(&seg)?;
                prefix[1] = if prefix[0] > 2 {
                    self.vlc.bit(&seg)? + 1
                } else {
                    self.vlc.u_prefix(&seg)?
                };
            }
        } else {
            for (p, quad) in prefix.iter_mut().zip(info) {
                if quad.u_off {
                    *p = self.vlc.u_prefix(&seg)?;
                }
            }
        }
        let mut suffix = [0u32; 2];
        for (s, &p) in suffix.iter_mut().zip(&prefix) {
            *s = self.vlc.u_suffix(&seg, p)?;
        }
        let mut u = [0u32; 2];
        for ((u, quad), (&p, &s)) in u.iter_mut().zip(info).zip(prefix.iter().zip(&suffix)) {
            if quad.u_off {
                *u = offset + p + s + 4 * self.vlc.u_extension(&seg, s)?;
            }
        }
        Ok(u)
    }

    /// The exponent predictor `kappa_q` of quad `q` below the first row
    /// (7.3.7), from the exponents of the row above.
    fn predictor(&self, exps: &[u32], q: usize, rho: u8) -> u32 {
        if rho.count_ones() < 2 {
            return 1;
        }
        let qw = self.qw;
        let up = 4 * (q - qw);
        let mut e = exps[up + 1].max(exps[up + 3]);
        if !q.is_multiple_of(qw) {
            e = e.max(exps[up - 1]);
        }
        if !(q + 1).is_multiple_of(qw) {
            e = e.max(exps[up + 5]);
        }
        e.saturating_sub(1).max(1)
    }

    /// Read quad `q`'s magnitudes and signs from MagSgn (7.3.8), record their
    /// exponents, and write its samples.
    fn magnitudes(
        &mut self,
        q: usize,
        info: QuadInfo,
        bound: u32,
        exps: &mut [u32],
        coeffs: &mut [i32],
    ) -> Result<()> {
        for j in 0..4 {
            if info.rho >> j & 1 == 0 {
                continue;
            }
            let k = u32::from(info.e_k >> j & 1);
            let i = u64::from(info.e_1 >> j & 1);
            let m = bound - k;
            let v = self.magsgn.value(&self.seg, m)? | i << m;
            let mu = (v >> 1) + 1;
            // `CAP` promises every `mu` of the codestream is below its bound
            // (A.3.7).
            if let Some(bits) = self.bound_bits.filter(|&bits| mu >> bits != 0) {
                return Err(malformed(&format!(
                    "cleanup magnitude {mu} is not below 2^{bits}, the bound `CAP` signals"
                )));
            }
            exps[4 * q + j] = 64 - (2 * mu - 1).leading_zeros();
            // Within the exponent bound, a `mu` with more bits than the block
            // codes (7.6) can still overflow `i32`, shifted up by the
            // cleanup plane.
            let magnitude = i32::try_from((2 * mu + 1) << self.shift)
                .map_err(|_| malformed("codes a magnitude past its bit-planes"))?;
            let x = 2 * (q % self.qw) + (j >> 1);
            let y = 2 * (q / self.qw) + (j & 1);
            coeffs[y * self.width + x] = if v & 1 == 1 { -magnitude } else { magnitude };
        }
        Ok(())
    }
}

/// The SigProp bit-stream: forward from byte 0 of the refinement segment,
/// least significant bit first, with a stuffing bit after every 0xFF; past
/// the end it reads as zeros (`importSigPropBit`, 7.1.5).
struct SigPropReader<'a> {
    data: &'a [u8],
    pos: usize,
    bits: u32,
    tmp: u32,
    last: u32,
}

impl<'a> SigPropReader<'a> {
    fn new(data: &'a [u8]) -> Self {
        SigPropReader {
            data,
            pos: 0,
            bits: 0,
            tmp: 0,
            last: 0,
        }
    }

    fn bit(&mut self) -> Result<u32> {
        if self.bits == 0 {
            self.bits = if self.last == 0xFF { 7 } else { 8 };
            self.tmp = match self.data.get(self.pos) {
                Some(&byte) => {
                    self.pos += 1;
                    u32::from(byte)
                }
                None => 0,
            };
            if self.tmp & (1 << self.bits) != 0 {
                return Err(malformed(
                    "SigProp byte after 0xFF has its stuffing bit set",
                ));
            }
            self.last = self.tmp;
        }
        let bit = self.tmp & 1;
        self.tmp >>= 1;
        self.bits -= 1;
        Ok(bit)
    }
}

/// The MagRef bit-stream: backward from the last byte of the refinement
/// segment, least significant bit first, skipping the stuffing bit of a byte
/// whose low seven bits are ones after a byte above 0x8F (the first byte
/// read counts as following one); before the start it reads as zeros
/// (`importMagRefBit`, 7.1.6).
struct MagRefReader<'a> {
    data: &'a [u8],
    /// The bytes not yet read; the next one is `data[left - 1]`.
    left: usize,
    bits: u32,
    tmp: u32,
    last: u32,
}

impl<'a> MagRefReader<'a> {
    fn new(data: &'a [u8]) -> Self {
        MagRefReader {
            data,
            left: data.len(),
            bits: 0,
            tmp: 0,
            last: 0xFF,
        }
    }

    fn bit(&mut self) -> u32 {
        if self.bits == 0 {
            self.tmp = match self.left.checked_sub(1) {
                Some(pos) => {
                    self.left = pos;
                    u32::from(self.data[pos])
                }
                None => 0,
            };
            self.bits = if self.last > 0x8F && self.tmp & 0x7F == 0x7F {
                7
            } else {
                8
            };
            self.last = self.tmp;
        }
        let bit = self.tmp & 1;
        self.tmp >>= 1;
        self.bits -= 1;
        bit
    }
}

/// The SigProp and MagRef passes of one block, which code the plane below
/// the cleanup pass's last (7.4, 7.5).
struct Refinement {
    width: usize,
    height: usize,
    causal: bool,
    /// The cleanup pass's shift from `2 mu + 1`, at least 1: the plane
    /// refined is bit `shift` of the double-scale magnitude.
    shift: u32,
}

impl Refinement {
    /// Run the SigProp pass over `data`, then the MagRef pass if `magref`,
    /// on the cleanup pass's `coeffs`, with `scratch`'s buffers.
    fn run(
        &self,
        data: &[u8],
        magref: bool,
        coeffs: &mut [i32],
        scratch: &mut RefinementScratch,
    ) -> Result<()> {
        let RefinementScratch {
            sigma,
            refined,
            new,
        } = scratch;
        // `sigma_n` of the cleanup pass: its significant samples are exactly
        // its nonzero ones.
        sigma.clear();
        sigma.extend(coeffs.iter().map(|&c| c != 0));
        // `r_n`: the samples SigProp makes significant, none yet.
        zeroed(refined, coeffs.len());
        self.sigprop(data, sigma, refined, new, coeffs)?;
        if magref {
            self.magref(data, sigma, coeffs);
        }
        Ok(())
    }

    /// The stripe-oriented scan (7.4), by column-group: four-line stripes top
    /// to bottom, and in each, groups of up to four columns left to right.
    /// Each item is a group's rows `y0..y1` and columns `x0..x1`, which the
    /// scan visits column by column, top to bottom.
    fn groups(&self) -> impl Iterator<Item = (usize, usize, usize, usize)> + use<> {
        let (w, h) = (self.width, self.height);
        (0..h).step_by(4).flat_map(move |y0| {
            (0..w)
                .step_by(4)
                .map(move |x0| (y0, (y0 + 4).min(h), x0, (x0 + 4).min(w)))
        })
    }

    /// `decodeSigPropMag` and `decodeSigPropSign` (7.4). A sample the cleanup
    /// pass left insignificant is visited when a neighbor is significant: by
    /// the cleanup pass, or by this pass earlier in the scan. Under the
    /// vertically causal flag the next stripe holds no neighbors. A
    /// column-group's refinement bits come first, then the signs of the
    /// samples they made significant. Such a sample's magnitude is the
    /// refined plane alone, so it reconstructs to `3 << (shift - 1)`.
    ///
    /// `r` holds `r_n`, the samples this pass made significant, and starts
    /// all false; `new` is room for a column-group's.
    fn sigprop(
        &self,
        data: &[u8],
        sigma: &[bool],
        r: &mut [bool],
        new: &mut Vec<usize>,
        coeffs: &mut [i32],
    ) -> Result<()> {
        let w = self.width;
        let mut sp = SigPropReader::new(data);
        let magnitude = 3i32 << (self.shift - 1);
        for (y0, y1, x0, x1) in self.groups() {
            let rows = if self.causal { y1 } else { self.height };
            new.clear();
            for x in x0..x1 {
                for y in y0..y1 {
                    let n = y * w + x;
                    if sigma[n] {
                        continue;
                    }
                    let mut mbr = false;
                    for ny in y.saturating_sub(1)..(y + 2).min(rows) {
                        for nx in x.saturating_sub(1)..(x + 2).min(w) {
                            let m = ny * w + nx;
                            mbr |= sigma[m] || r[m];
                        }
                    }
                    if mbr && sp.bit()? == 1 {
                        r[n] = true;
                        new.push(n);
                    }
                }
            }
            for &n in new.iter() {
                coeffs[n] = if sp.bit()? == 1 {
                    -magnitude
                } else {
                    magnitude
                };
            }
        }
        Ok(())
    }

    /// `decodeMagRefValue` (7.5): one bit for each sample the cleanup pass
    /// found significant, in the stripe-oriented scan. The bit replaces the
    /// half-plane the cleanup pass reconstructed with, and half of the plane
    /// below is added.
    fn magref(&self, data: &[u8], sigma: &[bool], coeffs: &mut [i32]) {
        let w = self.width;
        let mut mr = MagRefReader::new(data);
        let (center, half) = (1i32 << self.shift, 1i32 << (self.shift - 1));
        for (y0, y1, x0, x1) in self.groups() {
            for x in x0..x1 {
                for y in y0..y1 {
                    let n = y * w + x;
                    if !sigma[n] {
                        continue;
                    }
                    let c = coeffs[n];
                    let mut m = c.abs();
                    if mr.bit() == 0 {
                        m ^= center;
                    }
                    m |= half;
                    coeffs[n] = if c < 0 { -m } else { m };
                }
            }
        }
    }
}