pub mod bio;
mod geometry;
mod progression;
pub mod spans;
pub mod tagtree;
use crate::codestream::markers::Rect;
use crate::codestream::markers::marker;
use crate::codestream::{MainHeader, Tile};
use crate::{Error, Result};
use bio::BitReader;
use spans::UnreadSpans;
use std::cell::Cell;
use tagtree::TagTree;
pub(crate) use geometry::{
BandGeom, MAX_CODE_BLOCKS, MAX_PRECINCTS, PrecinctGeom, resolution_geoms,
};
pub(crate) use progression::{PacketWalk, for_each_packet};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BandKind {
Ll,
Hl,
Lh,
Hh,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodeBlock<'a> {
pub x: usize,
pub y: usize,
pub width: usize,
pub height: usize,
pub num_passes: u32,
pub zero_bit_planes: u32,
pub segments: Vec<CodedSegment<'a>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodedSegment<'a> {
pub passes: u32,
pub chunks: Vec<&'a [u8]>,
}
impl CodedSegment<'_> {
pub fn bytes(&self) -> Vec<u8> {
self.chunks.concat()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Subband<'a> {
pub kind: BandKind,
pub origin: (u32, u32),
pub width: usize,
pub height: usize,
pub block_cols: usize,
pub block_rows: usize,
pub blocks: Vec<CodeBlock<'a>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Resolution<'a> {
pub subbands: Vec<Subband<'a>>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ComponentCoded<'a> {
pub resolutions: Vec<Resolution<'a>>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CodedData<'a> {
pub components: Vec<ComponentCoded<'a>>,
}
pub fn decode_packets<'a>(
header: &MainHeader,
tile: &'a Tile<'a>,
visits: &mut u64,
poc_bitmap: u64,
reduction: u8,
) -> std::result::Result<CodedData<'a>, Refused> {
let mut budget = Budget {
reduction,
..Budget::decode(*visits, poc_bitmap)
};
let coded = decode_packets_within(header, tile, &mut budget);
*visits = budget.visits;
coded.map_err(|error| match (budget.refusal, error) {
(Some(work), Error::Limit(_)) => Refused::Work(work),
(_, error) => Refused::Other(error),
})
}
pub(crate) const DECODE_VISITS_MIN: u64 = 1 << 24;
pub(crate) const DECODE_VISITS_PER_BYTE: u64 = 32;
pub(crate) fn decode_visits(bytes: u64) -> u64 {
DECODE_VISITS_MIN.max(bytes.saturating_mul(DECODE_VISITS_PER_BYTE))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Budget {
pub(crate) precincts: usize,
pub(crate) blocks: usize,
pub(crate) visits: u64,
pub(crate) poc_bitmap: u64,
pub(crate) poc_set: bool,
pub(crate) poc_coverage: bool,
pub(crate) reduction: u8,
pub(crate) refusal: Option<WorkRefusal>,
}
impl Budget {
pub(crate) const fn decode(visits: u64, poc_bitmap: u64) -> Budget {
Budget {
precincts: MAX_PRECINCTS,
blocks: MAX_CODE_BLOCKS,
visits,
poc_bitmap: if visits < poc_bitmap {
visits
} else {
poc_bitmap
},
poc_set: true,
poc_coverage: true,
reduction: 0,
refusal: None,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Reach {
pub(crate) missing_from: Vec<Vec<Option<u32>>>,
pub(crate) leftover: Option<(usize, usize)>,
pub(crate) leftover_packed: Option<usize>,
}
impl Reach {
fn miss(&mut self, comp: usize, res: usize, layer: u32) {
if let Some(from) = self.missing_from.get_mut(comp).and_then(|c| c.get_mut(res)) {
*from = Some(from.map_or(layer, |f| f.min(layer)));
}
}
}
pub fn decode_packets_partial<'a>(
header: &MainHeader,
tile: &'a Tile<'a>,
visits: &mut u64,
poc_bitmap: u64,
reduction: u8,
) -> std::result::Result<(CodedData<'a>, Reach), Refused> {
let mut budget = Budget {
reduction,
..Budget::decode(*visits, poc_bitmap)
};
let mut reach = Reach::default();
let coded = walk_packets(header, tile, &mut budget, Some(&mut reach));
*visits = budget.visits;
match coded {
Ok(coded) => Ok((coded, reach)),
Err(error) => Err(match (budget.refusal, error) {
(Some(work), Error::Limit(_)) => Refused::Work(work),
(_, error) => Refused::Other(error),
}),
}
}
pub(crate) fn decode_packets_within<'a>(
header: &MainHeader,
tile: &'a Tile<'a>,
budget: &mut Budget,
) -> Result<CodedData<'a>> {
walk_packets(header, tile, budget, None)
}
fn walk_packets<'a>(
header: &MainHeader,
tile: &'a Tile<'a>,
budget: &mut Budget,
mut reach: Option<&mut Reach>,
) -> Result<CodedData<'a>> {
let data: &'a [u8] = &tile.data;
let component_count = header.siz.components.len();
let visits = Visits::new(budget.visits);
let mut geoms = Vec::with_capacity(component_count);
for c in 0..component_count {
let resolutions = resolution_geoms(
header,
tile.index,
c,
&mut budget.precincts,
&mut budget.blocks,
)?;
let kept = resolutions
.len()
.saturating_sub(usize::from(budget.reduction));
let dropped: usize = resolutions[kept..]
.iter()
.flat_map(|resolution| &resolution.bands)
.map(BandGeom::block_count)
.sum();
let charged = charge_dropped_blocks(&visits, dropped);
budget.visits = visits.get();
budget.refusal = visits.refusal.get();
charged?;
geoms.push(resolutions);
}
let mut states: Vec<Vec<Vec<BandState<'a>>>> = geoms
.iter()
.map(|component| {
component
.iter()
.map(|level| level.bands.iter().map(BandState::new).collect())
.collect()
})
.collect();
let walk = PacketWalk {
siz: &header.siz,
tile: header
.siz
.tile_rect(tile.index)
.ok_or_else(|| Error::Inconsistent(format!("no tile {} in `SIZ`", tile.index)))?,
geoms: &geoms,
};
let delimiters = Delimiters {
sop: header.cod.use_sop,
eph: header.cod.use_eph,
};
let packed = !tile.packed_headers.is_empty();
let mut streams = PacketStreams {
header: if packed { &tile.packed_headers } else { data },
header_pos: 0,
data,
body_pos: 0,
packed,
tile: tile.index,
};
if let Some(reach) = reach.as_deref_mut() {
reach.missing_from = geoms.iter().map(|c| vec![None; c.len()]).collect();
progression::poc_gaps(header, &walk, |comp, res, layer| {
reach.miss(comp, res, u32::from(layer));
Ok(())
})?;
} else if budget.poc_coverage {
progression::check_poc_coverage(header, &walk, tile.index)?;
}
let strict = reach.is_none();
let end_visits = geoms
.iter()
.map(Vec::len)
.sum::<usize>()
.saturating_mul(END_VISITS);
let mut log = Vec::new();
let mut scratch = Scratch::default();
let mut ended = false;
let mut packet_index = 0u32;
let walked = for_each_packet(
header,
&walk,
&visits,
budget.poc_bitmap,
budget.poc_set,
|layer, resolution, component, precinct| {
if let Some(reach) = reach.as_deref_mut() {
if ended {
visit(&visits, 1)?;
reach.miss(component, resolution, layer);
return Ok(());
}
}
let geom = &geoms[component][resolution];
let packet = parse_packet(
&mut streams,
&visits,
layer,
packet_index,
delimiters,
header.components[component].coding.code_block_style,
&geom.bands,
precinct,
&mut states[component][resolution],
&mut scratch,
if strict { None } else { Some(&mut log) },
)?;
if packet != Packet::Read {
ended = true;
visit(&visits, end_visits)?;
if let Some(reach) = reach.as_deref_mut() {
reach.miss(component, resolution, layer);
}
}
packet_index += 1;
Ok(())
},
);
budget.visits = visits.get();
budget.refusal = visits.refusal.get();
walked?;
let mut components = Vec::with_capacity(states.len());
for (comp, (component_states, component_geoms)) in states.into_iter().zip(&geoms).enumerate() {
let rect = header
.siz
.tile_component_rect(tile.index, comp)
.ok_or_else(|| Error::Inconsistent(format!("no component {comp} in `SIZ`")))?;
let nl = (component_geoms.len() - 1) as u32;
let mut resolutions = Vec::with_capacity(component_states.len());
for (r, (band_states, level)) in component_states
.into_iter()
.zip(component_geoms)
.enumerate()
{
let subbands = if level.bands.is_empty() {
empty_subbands(rect, nl, r as u32)
} else {
build_subbands(&level.bands, band_states)
};
resolutions.push(Resolution { subbands });
}
components.push(ComponentCoded { resolutions });
}
if let Some(reach) = reach {
if !ended {
if streams.body_pos < data.len() {
reach.leftover = Some((streams.body_pos, data.len() - streams.body_pos));
}
if packed && streams.header_pos < streams.header.len() {
reach.leftover_packed = Some(streams.header.len() - streams.header_pos);
}
}
return Ok(CodedData { components });
}
if streams.body_pos != data.len() {
return Err(Error::Codestream(format!(
"tile {} has {} byte(s) left after the last packet",
tile.index,
data.len() - streams.body_pos
)));
}
if packed && streams.header_pos != streams.header.len() {
return Err(Error::Codestream(format!(
"tile {} has {} byte(s) of packed packet headers left after the last packet",
tile.index,
streams.header.len() - streams.header_pos
)));
}
Ok(CodedData { components })
}
const ZBP_LIMIT: u32 = 293;
const LBLOCK_MAX: u32 = 24;
fn segment_max_passes(style: u8, segments: &[SegmentState<'_>]) -> u32 {
use crate::codestream::markers::code_block_style::{LAZY, TERMALL};
if style & TERMALL != 0 {
1
} else if style & LAZY != 0 {
match segments.last() {
None => 10,
Some(prev) => {
if prev.max_passes == 1 || prev.max_passes == 10 {
2
} else {
1
}
}
}
} else {
109
}
}
#[derive(Debug, Clone, Copy)]
struct Delimiters {
sop: bool,
eph: bool,
}
struct PacketStreams<'a> {
header: &'a [u8],
header_pos: usize,
data: &'a [u8],
body_pos: usize,
packed: bool,
tile: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum WorkRefusal {
PacketWalk,
PocReach { reached: u64 },
DroppedBlocks { blocks: u64 },
}
impl WorkRefusal {
pub(crate) fn why(&self) -> String {
match self {
WorkRefusal::PacketWalk => "reading the packet headers goes past the packet work \
bound (see `DecodeOptions::packet_work_limit`)"
.into(),
WorkRefusal::PocReach { reached } => format!(
"the `POC` volumes reach {reached} packets, a step each, more than \
the steps left of the packet work bound (see \
`DecodeOptions::packet_work_limit`)"
),
WorkRefusal::DroppedBlocks { blocks } => format!(
"the resolutions the reduction drops hold {blocks} code-blocks in \
one component, a step each, more than the steps left of the \
packet work bound (see `DecodeOptions::packet_work_limit`)"
),
}
}
}
#[derive(Debug)]
pub(crate) enum Refused {
Work(WorkRefusal),
Other(Error),
}
impl From<Refused> for Error {
fn from(refused: Refused) -> Error {
match refused {
Refused::Work(work) => Error::Limit(work.why()),
Refused::Other(error) => error,
}
}
}
pub(crate) struct Visits {
left: Cell<u64>,
refusal: Cell<Option<WorkRefusal>>,
}
impl Visits {
pub(crate) fn new(left: u64) -> Self {
Visits {
left: Cell::new(left),
refusal: Cell::new(None),
}
}
pub(crate) fn get(&self) -> u64 {
self.left.get()
}
pub(crate) fn refuse(&self, why: WorkRefusal) -> Error {
self.refusal.set(Some(why));
Error::Limit(why.why())
}
}
fn charge_dropped_blocks(visits: &Visits, blocks: usize) -> Result<()> {
match visits.left.get().checked_sub(blocks as u64) {
Some(rest) => {
visits.left.set(rest);
Ok(())
}
None => Err(visits.refuse(WorkRefusal::DroppedBlocks {
blocks: blocks as u64,
})),
}
}
const END_VISITS: usize = 32;
pub(crate) fn visit(visits: &Visits, n: usize) -> Result<()> {
match visits.left.get().checked_sub(n as u64) {
Some(rest) => {
visits.left.set(rest);
Ok(())
}
None => Err(visits.refuse(WorkRefusal::PacketWalk)),
}
}
fn packed_overrun() -> Error {
Error::Codestream(
"a packet header runs past the end of its `PPM` or `PPT` packed headers".into(),
)
}
fn peek_marker(data: &[u8], pos: usize) -> Option<u16> {
let hi = *data.get(pos)?;
let lo = *data.get(pos + 1)?;
Some(u16::from_be_bytes([hi, lo]))
}
fn read_sop(data: &[u8], pos: usize, packet_index: u32) -> Result<usize> {
let end = pos + 6;
if end > data.len() {
return Err(Error::Codestream("truncated `SOP` marker segment".into()));
}
let lsop = u16::from_be_bytes([data[pos + 2], data[pos + 3]]);
if lsop != 4 {
return Err(Error::Codestream(format!(
"`SOP` declares `Lsop` {lsop}, expected 4"
)));
}
let nsop = u16::from_be_bytes([data[pos + 4], data[pos + 5]]);
let expected = (packet_index % 65536) as u16;
if nsop != expected {
return Err(Error::Codestream(format!(
"`SOP` sequence number {nsop} for packet {packet_index}, expected {expected}"
)));
}
Ok(end)
}
struct BlockState<'a> {
included: bool,
lblock: u32,
zero_bit_planes: u32,
num_passes: u32,
segments: Vec<SegmentState<'a>>,
}
struct SegmentState<'a> {
passes: u32,
max_passes: u32,
chunks: Vec<&'a [u8]>,
}
impl BlockState<'_> {
fn new() -> Self {
BlockState {
included: false,
lblock: 3,
zero_bit_planes: 0,
num_passes: 0,
segments: Vec::new(),
}
}
}
struct BandState<'a> {
precincts: Vec<PrecinctState>,
blocks: Vec<BlockState<'a>>,
}
struct PrecinctState {
inclusion: TagTree,
zero_bits: TagTree,
}
impl BandState<'_> {
fn new(band: &BandGeom) -> Self {
BandState {
precincts: band
.precincts
.iter()
.map(|precinct| PrecinctState {
inclusion: TagTree::new(precinct.cols as u32, precinct.rows as u32),
zero_bits: TagTree::new(precinct.cols as u32, precinct.rows as u32),
})
.collect(),
blocks: (0..band.block_count()).map(|_| BlockState::new()).collect(),
}
}
}
struct Contribution {
band: usize,
block: usize,
segment: usize,
seg_len: usize,
}
#[derive(Default)]
struct Scratch {
contributions: Vec<Contribution>,
unread: UnreadSpans,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Packet {
Read,
Ended,
}
struct Undo {
band: usize,
block: usize,
included: bool,
lblock: u32,
zero_bit_planes: u32,
num_passes: u32,
segments: usize,
last_passes: u32,
last_chunks: usize,
}
fn undo(log: &[Undo], states: &mut [BandState<'_>]) {
for entry in log.iter().rev() {
let block = &mut states[entry.band].blocks[entry.block];
block.included = entry.included;
block.lblock = entry.lblock;
block.zero_bit_planes = entry.zero_bit_planes;
block.num_passes = entry.num_passes;
block.segments.truncate(entry.segments);
if let Some(last) = block.segments.last_mut() {
last.passes = entry.last_passes;
last.chunks.truncate(entry.last_chunks);
}
}
}
#[allow(clippy::too_many_arguments)]
fn parse_packet<'a>(
streams: &mut PacketStreams<'a>,
visits: &Visits,
layer: u32,
packet_index: u32,
delimiters: Delimiters,
style: u8,
bands: &[BandGeom],
precinct: usize,
states: &mut [BandState<'a>],
scratch: &mut Scratch,
mut log: Option<&mut Vec<Undo>>,
) -> Result<Packet> {
visit(visits, 1)?;
let partial = log.is_some();
if partial
&& delimiters.sop
&& peek_marker(streams.data, streams.body_pos) == Some(marker::SOP)
&& streams.body_pos + 6 > streams.data.len()
{
return Ok(Packet::Ended);
}
if delimiters.sop && peek_marker(streams.data, streams.body_pos) == Some(marker::SOP) {
streams.body_pos = read_sop(streams.data, streams.body_pos, packet_index)?;
}
if !streams.packed {
streams.header_pos = streams.body_pos;
}
if streams.header_pos >= streams.header.len() {
if partial {
return Ok(Packet::Ended);
}
return Err(Error::Codestream(format!(
"the data of tile {} ends before the last packet",
streams.tile
)));
}
let mut bio = BitReader::new(&streams.header[streams.header_pos..]);
if let Some(log) = log.as_deref_mut() {
log.clear();
}
let present = bio.read_bit() == 1;
let contributions = &mut scratch.contributions;
contributions.clear();
let mut fault = None;
if present {
for (band_index, (band, state)) in bands.iter().zip(states.iter_mut()).enumerate() {
let parsed = parse_band_header(
visits,
band_index,
&band.precincts[precinct],
&mut state.precincts[precinct],
&mut state.blocks,
layer,
style,
&mut bio,
contributions,
&mut scratch.unread,
log.as_deref_mut(),
);
if let Err(error) = parsed {
fault = Some(error);
break;
}
}
}
if streams.packed && bio.overran() {
return Err(packed_overrun());
}
if let Some(error) = fault {
return match log.as_deref() {
Some(log) if bio.overran() => {
undo(log, states);
Ok(Packet::Ended)
}
_ => Err(error),
};
}
let header_overran = bio.overran() && !streams.packed;
bio.align();
if streams.packed && bio.overran() {
return Err(packed_overrun());
}
streams.header_pos += bio.bytes_consumed();
if let Some(log) = log.as_deref() {
let eph_cut = delimiters.eph && streams.header_pos + 2 > streams.header.len();
if header_overran || eph_cut {
undo(log, states);
return Ok(Packet::Ended);
}
}
if delimiters.eph {
if peek_marker(streams.header, streams.header_pos) != Some(marker::EPH) {
return Err(Error::Codestream(
"`COD` signals `EPH` but the packet header is not followed by one".into(),
));
}
streams.header_pos += 2;
}
if !streams.packed {
streams.body_pos = streams.header_pos;
}
for contribution in contributions.iter() {
let end = match streams
.body_pos
.checked_add(contribution.seg_len)
.filter(|&e| e <= streams.data.len())
{
Some(end) => end,
None => {
if let Some(log) = log.as_deref() {
let from = log
.iter()
.position(|u| (u.band, u.block) == (contribution.band, contribution.block))
.unwrap_or(0);
undo(&log[from..], states);
return Ok(Packet::Ended);
}
return Err(Error::Codestream(format!(
"a packet body segment overruns the data of tile {}",
streams.tile
)));
}
};
states[contribution.band].blocks[contribution.block].segments[contribution.segment]
.chunks
.push(&streams.data[streams.body_pos..end]);
streams.body_pos = end;
}
if !streams.packed {
streams.header_pos = streams.body_pos;
}
Ok(Packet::Read)
}
#[allow(clippy::too_many_arguments)]
fn parse_band_header(
visits: &Visits,
band_index: usize,
precinct: &PrecinctGeom,
trees: &mut PrecinctState,
blocks: &mut [BlockState<'_>],
layer: u32,
style: u8,
bio: &mut BitReader,
contributions: &mut Vec<Contribution>,
unread: &mut UnreadSpans,
mut log: Option<&mut Vec<Undo>>,
) -> Result<()> {
let cols = precinct.cols as u32;
let threshold = layer + 1;
unread.clear();
let mut entry = 0;
while entry < precinct.cols * precinct.rows {
let bx = entry as u32 % cols;
let by = entry as u32 / cols;
let block_index = precinct.block(bx as usize, by as usize);
entry += 1;
let was_included = blocks[block_index].included;
let span = if was_included {
None
} else {
unread.find(&trees.inclusion, bx, by, threshold)
};
if let Some(span) = span {
visit(visits, 1)?;
entry = span.next_entry(by, cols);
continue;
}
let contributes = if was_included {
bio.read_bit() == 1
} else {
match trees.inclusion.read(bx, by, threshold, bio) {
Some(_) => {
blocks[block_index].included = true;
true
}
None => false,
}
};
if !contributes {
continue;
}
if let Some(log) = log.as_deref_mut() {
let block = &blocks[block_index];
let last = block.segments.last();
log.push(Undo {
band: band_index,
block: block_index,
included: was_included,
lblock: block.lblock,
zero_bit_planes: block.zero_bit_planes,
num_passes: block.num_passes,
segments: block.segments.len(),
last_passes: last.map_or(0, |s| s.passes),
last_chunks: last.map_or(0, |s| s.chunks.len()),
});
}
if !was_included {
blocks[block_index].zero_bit_planes = trees
.zero_bits
.read(bx, by, ZBP_LIMIT, bio)
.ok_or_else(|| {
Error::Codestream(format!(
"code-block zero-bitplane count is over {}, the most a valid stream \
can signal",
ZBP_LIMIT - 1
))
})?;
}
let num_passes = read_num_passes(bio);
let block = &mut blocks[block_index];
while bio.read_bit() == 1 {
block.lblock += 1;
if block.lblock > LBLOCK_MAX {
return Err(Error::Codestream(format!(
"code-block `Lblock` grows past the limit of {LBLOCK_MAX}"
)));
}
}
block.num_passes += num_passes;
let mut remaining = num_passes;
loop {
if block
.segments
.last()
.is_none_or(|s| s.passes >= s.max_passes)
{
let max_passes = segment_max_passes(style, &block.segments);
block.segments.push(SegmentState {
passes: 0,
max_passes,
chunks: Vec::new(),
});
}
let segment_index = block.segments.len() - 1;
let segment = &mut block.segments[segment_index];
let new_passes = remaining.min(segment.max_passes - segment.passes);
let length_bits = block.lblock + new_passes.ilog2();
let seg_len = bio.read(length_bits) as usize;
let segment = &mut block.segments[segment_index];
segment.passes += new_passes;
if seg_len > 0 {
contributions.push(Contribution {
band: band_index,
block: block_index,
segment: segment_index,
seg_len,
});
}
remaining -= new_passes;
if remaining == 0 {
break;
}
}
}
Ok(())
}
fn empty_subbands<'a>(rect: Rect, nl: u32, r: u32) -> Vec<Subband<'a>> {
geometry::band_rects(rect, nl, r)
.map(|(kind, x0, x1, y0, y1)| {
debug_assert!(
x1 <= x0 || y1 <= y0,
"band of an empty resolution is not empty"
);
Subband {
kind,
origin: (x0.max(0) as u32, y0.max(0) as u32),
width: (x1 - x0).max(0) as usize,
height: (y1 - y0).max(0) as usize,
block_cols: 0,
block_rows: 0,
blocks: Vec::new(),
}
})
.collect()
}
fn build_subbands<'a>(bands: &[BandGeom], states: Vec<BandState<'a>>) -> Vec<Subband<'a>> {
bands
.iter()
.zip(states)
.map(|(band, state)| {
debug_assert_eq!(
state.blocks.len(),
band.block_count(),
"one block state per code-block of the band"
);
let mut blocks = Vec::with_capacity(state.blocks.len());
blocks.extend(band.block_rects().zip(state.blocks).map(
|((x, y, width, height), block)| {
CodeBlock {
x,
y,
width,
height,
num_passes: block.num_passes,
zero_bit_planes: block.zero_bit_planes,
segments: block
.segments
.into_iter()
.map(|s| CodedSegment {
passes: s.passes,
chunks: s.chunks,
})
.collect(),
}
},
));
Subband {
kind: band.kind,
origin: band.origin,
width: band.width,
height: band.height,
block_cols: band.block_cols,
block_rows: band.block_rows,
blocks,
}
})
.collect()
}
fn read_num_passes(bio: &mut BitReader) -> u32 {
if bio.read_bit() == 0 {
return 1;
}
if bio.read_bit() == 0 {
return 2;
}
let n = bio.read(2);
if n != 3 {
return 3 + n;
}
let n = bio.read(5);
if n != 31 {
return 6 + n;
}
37 + bio.read(7)
}
#[cfg(test)]
mod partial_tests;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod work_bound_tests;