mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 21:12:33 +00:00
craft-video: H.264 decoder from FilmCraft, with DeckCraft's hardening (h264-decode) (#834)
* craft-video: H.264 decoder from FilmCraft (`h264-decode` feature) FilmCraft's crates/h264 at 4c0d2ad7 becomes craft_video::h264::decode. The encoder moves to h264/encode and is still exported from craft_video::h264. Feature h264 now turns on h264-decode and h264-encode, so either half can be built without the other. Without the threads feature a reference row that was never published read through expect(); it now reads as a gray row. craft_video::budget (the picture memory budget from DeckCraft's bitstream crate) is added for the next commit. craft-video is built at opt-level 3 in the dev profile, as FilmCraft built its codecs. * craft-video: H.264 decoder hardening from DeckCraft's copy - The picture store (DPB, current picture, pictures in flight or queued for output) is checked against a memory budget before a picture is allocated; Decoder::set_memory_budget. - configure_avcc checks every entry before handling any: SPS NAL units in the first list, PPS in the second. A picture slice in the record starts no picture. - The inverse transforms clamp coefficients to the spec's r range instead of overflowing. - Temporal direct motion vectors are scaled in i32 and clipped to the spec range. - slice_qp_delta is added with a checked add. - A picture job sets up its picture state inside the panic guard.
This commit is contained in:
+12
-2
@@ -21,8 +21,10 @@ dnx = []
|
||||
vp8 = []
|
||||
# Apple ProRes (SMPTE RDD 36): decoder and encoder.
|
||||
prores = ["craft-media/bitstream"]
|
||||
# H.264 / AVC (ITU-T H.264): encoder.
|
||||
h264 = ["craft-media/isobmff"]
|
||||
# H.264 / AVC: decoder and encoder; `h264-decode` or `h264-encode` for one of them.
|
||||
h264 = ["h264-decode", "h264-encode"]
|
||||
h264-decode = ["craft-media/bitstream"]
|
||||
h264-encode = ["craft-media/isobmff"]
|
||||
# H.265 / HEVC: decoder and encoder; `hevc-decode` or `hevc-encode` for one of them.
|
||||
hevc = ["hevc-decode", "hevc-encode"]
|
||||
hevc-decode = ["craft-media/bitstream"]
|
||||
@@ -60,6 +62,14 @@ required-features = ["vp8"]
|
||||
name = "vp8_vp8dec"
|
||||
required-features = ["vp8"]
|
||||
|
||||
[[test]]
|
||||
name = "h264_decode"
|
||||
required-features = ["h264-decode"]
|
||||
|
||||
[[example]]
|
||||
name = "h264_h264dec"
|
||||
required-features = ["h264-decode"]
|
||||
|
||||
[[test]]
|
||||
name = "hevc_decode"
|
||||
required-features = ["hevc-decode"]
|
||||
|
||||
@@ -21,7 +21,7 @@ No codec feature is on by default.
|
||||
| `dnx` | [`dnx`](src/dnx/README.md) | Avid DNxHD / DNxHR (SMPTE ST 2019-1 VC-3): decoder and encoder |
|
||||
| `vp8` | `vp8` | VP8 (RFC 6386): decoder |
|
||||
| `prores` | `prores` | Apple ProRes (SMPTE RDD 36): decoder and encoder |
|
||||
| `h264` | `h264` | ITU-T H.264 / AVC: encoder |
|
||||
| `h264` | [`h264`](src/h264/decode/README.md) | ITU-T H.264 / AVC: decoder (`h264-decode` alone) and encoder (`h264-encode` alone) |
|
||||
| `hevc` | [`hevc`](src/hevc/decode/README.md) | ITU-T H.265 / HEVC: decoder (`hevc-decode` alone) and encoder (`hevc-encode` alone) |
|
||||
| `vp9` | [`vp9`](src/vp9/decode/README.md) | VP9: decoder (`vp9-decode` alone) and encoder (`vp9-encode` alone) |
|
||||
| `av1` | `av1` | AV1 (AOMedia Video 1): encoder |
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
//! Decode an Annex-B H.264 file to raw yuv420p: `h264dec in.h264 [out.yuv]`.
|
||||
//! Prints timing information to stderr. `H264_BENCH_ITERS=n` (with `H264_THREADS=t`, and
|
||||
//! `H264_DRAFT=1` for draft mode) turns it into a benchmark.
|
||||
|
||||
use craft_video::h264::decode::Decoder;
|
||||
use std::io::Write;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
eprintln!("usage: h264dec in.h264 [out.yuv]");
|
||||
std::process::exit(2);
|
||||
}
|
||||
let data = std::fs::read(&args[1]).expect("read input");
|
||||
// H264_BENCH_ITERS=n: decode n times without output and report the best run
|
||||
if let Some(n) = std::env::var("H264_BENCH_ITERS").ok().and_then(|v| v.parse::<usize>().ok()) {
|
||||
let mut best = f64::MAX;
|
||||
let mut frames = 0;
|
||||
let threads = std::env::var("H264_THREADS").ok().and_then(|v| v.parse::<usize>().ok());
|
||||
for _ in 0..n {
|
||||
let mut dec = threads.map(Decoder::with_threads).unwrap_or_default();
|
||||
dec.set_draft(std::env::var_os("H264_DRAFT").is_some());
|
||||
let t0 = std::time::Instant::now();
|
||||
frames = dec.decode(&data, 0).expect("decode").len() + dec.flush().len();
|
||||
best = best.min(t0.elapsed().as_secs_f64());
|
||||
}
|
||||
eprintln!("best of {n}: {frames} frames in {best:.3}s ({:.1} fps)", frames as f64 / best);
|
||||
return;
|
||||
}
|
||||
let mut out = args.get(2).map(|p| std::io::BufWriter::new(std::fs::File::create(p).expect("create output")));
|
||||
let mut dec = Decoder::new();
|
||||
let t0 = std::time::Instant::now();
|
||||
let mut n = 0usize;
|
||||
let mut write = |pics: Vec<craft_video::h264::decode::Picture>| {
|
||||
for p in pics {
|
||||
n += 1;
|
||||
if let Some(o) = out.as_mut() {
|
||||
o.write_all(&p.y).unwrap();
|
||||
o.write_all(&p.u).unwrap();
|
||||
o.write_all(&p.v).unwrap();
|
||||
}
|
||||
}
|
||||
};
|
||||
match dec.decode(&data, 0) {
|
||||
Ok(p) => write(p),
|
||||
Err(e) => eprintln!("decode error: {e}"),
|
||||
}
|
||||
write(dec.flush());
|
||||
let dt = t0.elapsed().as_secs_f64();
|
||||
if let Some(e) = dec.take_error() {
|
||||
eprintln!("decode error: {e}");
|
||||
}
|
||||
if std::env::var("H264_STATS").is_ok() {
|
||||
eprintln!("{:#?}", dec.stats());
|
||||
}
|
||||
eprintln!("{n} frames in {dt:.3}s ({:.1} fps)", n as f64 / dt);
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
# H.264 / AVC decoder (`craft_video::h264::decode`, feature `h264-decode`)
|
||||
|
||||
Clean-room, pure-Rust (no `unsafe`) H.264 / AVC decoder, implemented from the public ITU-T
|
||||
Rec. H.264 (ISO/IEC 14496-10) specification. It depends only on
|
||||
`craft_media::bitstream`, `thiserror`, and optionally `rayon` (feature `threads`), and builds
|
||||
for `wasm32-unknown-unknown`.
|
||||
|
||||
All syntax tables (CAVLC code tables, the 1024 CABAC `(m, n)` context initialisation pairs,
|
||||
`rangeTabLPS`, scaling defaults, deblocking thresholds, the 8x8 significance-map tables) were
|
||||
extracted from the specification text (the extraction scripts only read the ITU-T PDF). No code was
|
||||
taken or ported from FFmpeg, x264, JM or openh264. ffmpeg/libx264 are only used as external test
|
||||
oracles and fixture generators.
|
||||
|
||||
## Supported
|
||||
|
||||
- NAL units: Annex-B byte streams and length-prefixed (`avcC`) samples; SPS (all fields incl.
|
||||
VUI/HRD, scaling lists with fall-back rules A/B, cropping, high-profile fields), PPS (incl.
|
||||
`transform_8x8_mode`, scaling lists, slice groups parsed), SEI / AUD / filler skipped.
|
||||
- Slice header: all fields incl. reference list modification, prediction weight table and
|
||||
decoded reference picture marking. POC types 0, 1, 2. `frame_num` gap handling ("non-existing"
|
||||
frames).
|
||||
- Entropy coding: CAVLC and CABAC (all syntax elements needed for frame coding, 4:2:0).
|
||||
- Macroblocks: I (Intra 4x4 / 8x8 / 16x16, I_PCM), P (all partitions, P_8x8ref0, P_Skip),
|
||||
B (all partitions and sub-partitions, B_Skip, B_Direct_16x16, B_Direct_8x8), spatial and temporal
|
||||
direct prediction with/without `direct_8x8_inference`, constrained intra prediction.
|
||||
- Transforms / quantisation: 4x4, 8x8, Intra16x16 DC Hadamard, 2x2 chroma DC; flat and custom
|
||||
scaling matrices (SPS and PPS level).
|
||||
- Inter prediction: quarter-sample 6-tap luma, eighth-sample chroma, explicit and implicit weighted
|
||||
prediction, multiple reference frames, long-term references.
|
||||
- DPB: sliding window, all MMCOs (1-6), IDR / `no_output_of_prior_pics`, bumping with DPB size from
|
||||
the level (or VUI `max_dec_frame_buffering`) and `max_num_reorder_frames`.
|
||||
- Deblocking filter: full bS derivation, `disable_deblocking_filter_idc` 0/1/2, slice offsets,
|
||||
8x8-transform edges.
|
||||
- Output: cropped 8-bit planar 4:2:0 pictures in output order with the caller's `pts`, POC, key
|
||||
flag, VUI colour description (range, primaries, transfer, matrix) and sample aspect ratio.
|
||||
- Frame-level multithreading (see below).
|
||||
- Draft mode for reduced-resolution playback (off by default): non-reference pictures skip the
|
||||
deblocking filter and are flagged `Picture::draft`; every other picture stays bit-exact.
|
||||
|
||||
## API
|
||||
|
||||
```rust
|
||||
use craft_video::h264::decode::Decoder;
|
||||
|
||||
let mut dec = Decoder::new(); // or Decoder::with_threads(n), Decoder::from_avcc(&avcc)?
|
||||
// dec.threads(): worker threads in use; dec.set_draft(true): draft mode (playback only)
|
||||
for (pts, access_unit) in access_units {
|
||||
for pic in dec.decode(access_unit, pts)? {
|
||||
// pic.width / pic.height (cropped), pic.y / pic.u / pic.v, pic.y_stride / pic.uv_stride,
|
||||
// pic.pts, pic.poc, pic.key, pic.color, pic.sar
|
||||
}
|
||||
}
|
||||
for pic in dec.flush() { /* ... */ }
|
||||
if let Some(err) = dec.take_error() { /* error reported by a decoding thread */ }
|
||||
```
|
||||
|
||||
`decode` expects whole access units (a buffer with several complete access units — e.g. an
|
||||
entire Annex-B file — also works). `Decoder::stats()` returns counters of the coding tools seen
|
||||
(macroblock types, slice types, MMCOs, long-term marks, frame_num gaps, ...).
|
||||
|
||||
### Threading model
|
||||
|
||||
The calling thread parses headers and performs all POC / DPB / reference-list bookkeeping (it only
|
||||
needs header data). The macroblock layer of each picture is decoded as a job on a rayon pool. A job
|
||||
reconstructs into private buffers, deblocks each macroblock row once the row below it is
|
||||
reconstructed, and publishes finished rows (samples + motion data for co-located lookups) into the
|
||||
picture's shared `Frame` (`OnceLock` per macroblock row). Jobs of later pictures block per row on
|
||||
exactly the reference rows they read, so many pictures decode concurrently while the output stays
|
||||
bit-exact. Without the `threads` feature (or on wasm, or with `with_threads(1)`) jobs run inline.
|
||||
With threads, `decode` returns pictures once their job has finished, so output lags input by up to
|
||||
roughly the number of threads.
|
||||
|
||||
Deblocking is row-parallel in this pipelined form: each picture's job filters macroblock row r as
|
||||
soon as row r + 1 is reconstructed (intra prediction of row r + 1 reads the unfiltered bottom
|
||||
line of row r) and publishes it one row later, while the jobs of other pictures run on other
|
||||
threads. A picture waiting on a reference therefore waits only for that reference's rows its
|
||||
motion vectors reach plus about two rows, so a chain of reference pictures (a seek from an IDR
|
||||
picture) advances a few rows behind its predecessor instead of a whole picture behind it. A
|
||||
separate deblocking thread per picture would shorten that lag by about one row and would need its
|
||||
own copy of the reconstructed rows and macroblock state; it is not worth it while the pool has
|
||||
more pictures in flight than cores. The hot loops are written to vectorise without `unsafe` or
|
||||
intrinsics: the edge filters process all 16 (luma) or 8 (chroma) lines of an edge at once with
|
||||
per-lane masks, the inverse transforms do their column pass across all columns, and the CABAC
|
||||
engine decodes a context-coded bin without branching on its outcome.
|
||||
|
||||
### Draft mode
|
||||
|
||||
`Decoder::set_draft(true)` (reduced-resolution playback only) skips the
|
||||
deblocking filter (8.7) of non-reference pictures (`nal_ref_idc` 0). No picture is predicted from
|
||||
a non-reference picture, and deblocking never changes the motion data that direct prediction
|
||||
reads, so every other picture is unchanged; the skipped pictures are flagged `Picture::draft` and
|
||||
the media stack never hands them to a paused monitor, a render or an export. On the 4K fixture
|
||||
below it saves ~9 % of the decoding work (the B-pyramid's non-reference half of the B pictures).
|
||||
|
||||
## Memory and configuration records
|
||||
|
||||
A picture is refused when the pictures the decoder would hold do not fit
|
||||
`Decoder::set_memory_budget` (`craft_video::budget::DECODE_MEMORY_BUDGET` by default: 4 GiB,
|
||||
1 GiB on 32-bit targets): the DPB (the larger of its slots at the new size and what it holds
|
||||
now), the picture being decoded, and what is in flight or waiting in the output queue. Pictures
|
||||
already held count at their own size, and a buffer shared by the DPB and the queue counts once.
|
||||
The per-picture size cap applies as well.
|
||||
|
||||
`configure_avcc` checks every entry of the record before it handles any: the first list holds
|
||||
SPS NAL units and the second PPS NAL units. A record that carries a picture slice is an error and
|
||||
starts no picture.
|
||||
|
||||
## Tests
|
||||
|
||||
`cargo test -p craft-video --features h264-decode,threads` (fixtures are generated on first use into `target/fixtures/h264/`
|
||||
with ffmpeg + libx264; tests print a message and skip when `/opt/homebrew/bin/ffmpeg` is absent).
|
||||
|
||||
- Unit tests: CAVLC tables (prefix-freeness / Kraft sums, spec-style residual example), CABAC engine
|
||||
(context init formula, round trip against an encoder model), intra predictors with hand-computed
|
||||
values, transforms, luma interpolation against a straightforward reference implementation,
|
||||
weighting, macroblock type tables, scaling-list parsing.
|
||||
- Synthetic streams (`synth_tests.rs`): hand-built CAVLC and CABAC bitstreams covering what
|
||||
libx264 never produces — I_PCM, long-term references, MMCO 1-6, `frame_num` gaps, reference list
|
||||
modification (short- and long-term), explicit weighted prediction in P and B slices, B_Skip with
|
||||
explicit bi-prediction. The expected output is known exactly and is also checked against ffmpeg.
|
||||
- Robustness: avcC input with pts round trip; randomly corrupted, truncated and garbage input never
|
||||
panics or deadlocks (single- and multi-threaded).
|
||||
- Conformance (`tests/it/conformance.rs`): every fixture is decoded single-threaded, with 3 threads
|
||||
and with every core, and compared byte-for-byte with `ffmpeg -f rawvideo -pix_fmt yuv420p`; on
|
||||
mismatch the first differing frame, plane, sample position and macroblock are reported. Output
|
||||
pts/POC order is checked too, and that no picture is flagged draft without draft mode. Draft
|
||||
mode (`draft_mode_changes_only_flagged_non_reference_pictures`): on five B-pyramid / B-frame
|
||||
fixtures, single- and frame-threaded, every unflagged picture is bit-exact and only flagged
|
||||
pictures differ.
|
||||
- Vectorised kernels against their straightforward forms on random input: the deblocking edge
|
||||
filter against the per-line filter, the 4x4 / 8x8 inverse transforms against the per-column
|
||||
formulation.
|
||||
|
||||
### Fixture matrix (all bit-exact, single- and multi-threaded)
|
||||
|
||||
libx264 fixtures unless noted; the VideoToolbox (hardware encoder) fixtures are skipped where that
|
||||
encoder is unavailable.
|
||||
|
||||
| fixture | source / size | configuration |
|
||||
|---|---|---|
|
||||
| intra_cavlc | testsrc2 176x144 | Baseline, keyint=1, no deblock |
|
||||
| intra_cavlc_noise | mandelbrot+noise 352x288 | Baseline, keyint=1, no deblock |
|
||||
| intra_cavlc_8x8 | testsrc2+noise 352x288 | High CAVLC, 8x8 transform, intra only |
|
||||
| intra_cavlc_cqm | testsrc2+noise 352x288 | High CAVLC, cqm=jvt, intra only |
|
||||
| p_cavlc_nodeblock | testsrc2+noise 352x288 | Baseline, ref=3, no deblock |
|
||||
| baseline_qcif | testsrc2 176x144 | Baseline, keyint=15 |
|
||||
| baseline_cif_noise | mandelbrot+noise 352x288 | Baseline |
|
||||
| cavlc_b | testsrc2+noise 352x288 | High, cabac=0, bframes=3 |
|
||||
| cavlc_b_temporal | testsrc2+noise 352x288 | Main, cabac=0, direct=temporal, weightb |
|
||||
| cavlc_weightp | testsrc2 fade 352x288 | Main, cabac=0, weightp=2, weightb |
|
||||
| slices4_cavlc | testsrc2+noise 352x288 | Baseline, slices=4 |
|
||||
| qp1_cavlc | mandelbrot+noise 176x144 | Main, CAVLC, QP 1 |
|
||||
| main_cabac_b | testsrc2+noise 352x288 | Main (CABAC, B-frames) |
|
||||
| high_720p | smptehdbars+noise 1280x720 | High |
|
||||
| high_1080p | testsrc2 1920x1080 | High |
|
||||
| crop_1918x1078 | testsrc2+noise 1918x1078 | High, frame cropping |
|
||||
| bpyramid | testsrc2+noise 352x288 | bframes=3, b-pyramid=normal (MMCO) |
|
||||
| weightp2 | testsrc2 fade 352x288 | weightp=2 |
|
||||
| weightb | testsrc2 fade 352x288 | weightb=1, bframes=3 (implicit) |
|
||||
| direct_temporal | testsrc2+noise 352x288 | direct=temporal |
|
||||
| direct_spatial | mandelbrot+noise 352x288 | direct=spatial |
|
||||
| ref4 | testsrc2+noise 352x288 | ref=4 |
|
||||
| no_deblock | testsrc2+noise 352x288 | no-deblock |
|
||||
| deblock_m2_2 | testsrc2+noise 352x288 | deblock=-2,2 |
|
||||
| cqm_jvt | testsrc2+noise 352x288 | cqm=jvt |
|
||||
| slices4 | testsrc2+noise 352x288 | slices=4 (CABAC) |
|
||||
| keyint10 | testsrc2+noise 352x288 | keyint=10 |
|
||||
| open_gop | testsrc2+noise 352x288 | open-gop, keyint=12, bframes=3 |
|
||||
| constrained_intra | testsrc2+noise 352x288 | constrained-intra |
|
||||
| no8x8dct | testsrc2+noise 352x288 | 8x8dct=0 |
|
||||
| qp50 | testsrc2+noise 352x288 | QP 50 |
|
||||
| qp1 | mandelbrot+noise 352x288 | QP 1 |
|
||||
| bench_1080p | testsrc2+noise 1920x1080, 120 frames | preset medium, CRF 20 (8.8 Mbit/s) |
|
||||
| vt_baseline | testsrc2+noise 320x240 | Apple VideoToolbox, Baseline (macOS only) |
|
||||
| vt_main | mandelbrot+noise 640x360 | VideoToolbox, Main |
|
||||
| vt_high | testsrc2+noise 640x360 | VideoToolbox, High |
|
||||
| vt_high_1080p | testsrc2 1920x1080 | VideoToolbox, High 8 Mbit/s |
|
||||
|
||||
`cargo test --release -p craft-video --features h264-decode,threads --test h264_decode conformance::coverage_report -- --ignored --nocapture`
|
||||
prints which coding tools each fixture exercises.
|
||||
|
||||
## Performance
|
||||
|
||||
`cargo test --release -p craft-video --features h264-decode,threads --test h264_decode perf:: -- --ignored --nocapture` (1080p High profile,
|
||||
CABAC, B-pyramid, 120 frames, 8.8 Mbit/s; bit-exactness is verified first). Measured on an Apple M4
|
||||
Pro (14 cores) while the machine was heavily shared (load average 40-180), so these are lower bounds:
|
||||
|
||||
| threads | fps |
|
||||
|---|---|
|
||||
| 1 | ~105-122 (≈ 33 Mcycles per frame) |
|
||||
| 14 | ~500-600 |
|
||||
|
||||
4K High profile (testsrc2 + grain, 3840x2160, CABAC, B-pyramid, ~170 Mbit/s; the first 48 frames
|
||||
of the playback benchmark's `a2160.mp4`), CPU cycles per frame counted by the kernel
|
||||
(`proc_pid_rusage`; independent of machine load, all threads summed; bit-exact with ffmpeg):
|
||||
|
||||
| build | Mcycles / frame | instructions / frame |
|
||||
|---|---|---|
|
||||
| before M4.9 | 505 | 1232 M |
|
||||
| M4.9 | **390** (−23 %) | 1195 M |
|
||||
| M4.9, draft mode | **356** (−30 %) | 1068 M |
|
||||
|
||||
The same on the benchmark's 1080p fixture (`dec_h264_1080.mp4`, 120 frames): 132 → 100 Mcycles
|
||||
per frame (−24 %), 90 in draft mode. Single-threaded 4K time (`sample` profile) splits into CABAC
|
||||
residual decoding 41 %, deblocking 16 % (25-30 % before), other macroblock-layer parsing 11 %,
|
||||
inverse transforms and dequantisation 9 %, motion compensation 8 %, intra prediction 6 %, row and
|
||||
picture copies 4 %. At ~4.5 GHz a 4K frame takes ~85 ms of one
|
||||
core, so 23.976 fps needs about two cores' worth of frame threads.
|
||||
|
||||
The example `cargo run --release -p craft-video --features h264-decode,threads --example h264_h264dec -- in.h264 out.yuv` decodes a
|
||||
file; `H264_BENCH_ITERS=n` (and `H264_THREADS=t`, `H264_DRAFT=1`) turns it into a benchmark,
|
||||
`H264_STATS=1` prints the coverage counters.
|
||||
|
||||
## Known gaps
|
||||
|
||||
- Interlaced coding (field pictures, PAFF, MBAFF): streams with `frame_mbs_only_flag = 0` return
|
||||
`Error::Unsupported`.
|
||||
- Only 8-bit 4:2:0. High 10 / 4:2:2 / 4:4:4 / monochrome and lossless
|
||||
(`qpprime_y_zero_transform_bypass`) return `Error::Unsupported`.
|
||||
- FMO (slice groups), SP/SI slices, data partitioning (Extended profile), MVC/SVC NAL units.
|
||||
- Error concealment is minimal: undecodable slices leave the prediction/previous content; missing
|
||||
references are replaced by other references.
|
||||
- No SIMD intrinsics (the hot loops are written to auto-vectorise); CABAC residual decoding is
|
||||
inherently serial and now dominates.
|
||||
|
||||
### Extending to High 10 and 4:2:2
|
||||
|
||||
The chroma format and bit depth are isolated behind `Sps::check_supported`. The parts that are
|
||||
format-specific today: `Planes`/`FrameRow` sample type (`u8`; a `u16` variant or a generic sample
|
||||
type is needed for bit depths > 8, together with `Clip1` ranges, `QpBdOffset` in the QP
|
||||
derivations and the scaled tC0/alpha/beta in deblocking), chroma block geometry (`MbWidthC` /
|
||||
`MbHeightC` = 8x8 in `slicedec`, `intra::pred_chroma`, the chroma DC transform and the CABAC/CAVLC
|
||||
chroma DC parsing — the 2x4 total_zeros tables and ctxBlockCat 3 `NumC8x8` handling are already in
|
||||
place), chroma motion-vector vertical scaling and the chroma deblocking edges (4:2:2 has four
|
||||
horizontal chroma edges).
|
||||
@@ -0,0 +1,335 @@
|
||||
//! CABAC arithmetic decoding engine (9.3.1.2, 9.3.3.2) and context initialisation (9.3.1.1).
|
||||
|
||||
use crate::h264::decode::cabac_tables::CABAC_INIT_MN;
|
||||
use crate::h264::decode::error::{Result, ensure};
|
||||
|
||||
/// Table 9-44 rangeTabLPS[pStateIdx][qCodIRangeIdx].
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static RANGE_TAB_LPS: [[u8; 4]; 64] = [
|
||||
[128, 176, 208, 240], [128, 167, 197, 227], [128, 158, 187, 216], [123, 150, 178, 205],
|
||||
[116, 142, 169, 195], [111, 135, 160, 185], [105, 128, 152, 175], [100, 122, 144, 166],
|
||||
[95, 116, 137, 158], [90, 110, 130, 150], [85, 104, 123, 142], [81, 99, 117, 135],
|
||||
[77, 94, 111, 128], [73, 89, 105, 122], [69, 85, 100, 116], [66, 80, 95, 110],
|
||||
[62, 76, 90, 104], [59, 72, 86, 99], [56, 69, 81, 94], [53, 65, 77, 89],
|
||||
[51, 62, 73, 85], [48, 59, 69, 80], [46, 56, 66, 76], [43, 53, 63, 72],
|
||||
[41, 50, 59, 69], [39, 48, 56, 65], [37, 45, 54, 62], [35, 43, 51, 59],
|
||||
[33, 41, 48, 56], [32, 39, 46, 53], [30, 37, 43, 50], [29, 35, 41, 48],
|
||||
[27, 33, 39, 45], [26, 31, 37, 43], [24, 30, 35, 41], [23, 28, 33, 39],
|
||||
[22, 27, 32, 37], [21, 26, 30, 35], [20, 24, 29, 33], [19, 23, 27, 31],
|
||||
[18, 22, 26, 30], [17, 21, 25, 28], [16, 20, 23, 27], [15, 19, 22, 25],
|
||||
[14, 18, 21, 24], [14, 17, 20, 23], [13, 16, 19, 22], [12, 15, 18, 21],
|
||||
[12, 14, 17, 20], [11, 14, 16, 19], [11, 13, 15, 18], [10, 12, 15, 17],
|
||||
[10, 12, 14, 16], [9, 11, 13, 15], [9, 11, 12, 14], [8, 10, 12, 14],
|
||||
[8, 9, 11, 13], [7, 9, 11, 12], [7, 9, 10, 12], [7, 8, 10, 11],
|
||||
[6, 8, 9, 11], [6, 7, 9, 10], [6, 7, 8, 9], [2, 2, 2, 2],
|
||||
];
|
||||
|
||||
/// Table 9-45 transIdxLPS.
|
||||
#[rustfmt::skip]
|
||||
static TRANS_IDX_LPS: [u8; 64] = [
|
||||
0, 0, 1, 2, 2, 4, 4, 5, 6, 7, 8, 9, 9, 11, 11, 12,
|
||||
13, 13, 15, 15, 16, 16, 18, 18, 19, 19, 21, 21, 22, 22, 23, 24,
|
||||
24, 25, 26, 26, 27, 27, 28, 29, 29, 30, 30, 30, 31, 32, 32, 33,
|
||||
33, 33, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 63,
|
||||
];
|
||||
|
||||
/// Table 9-45 transIdxMPS.
|
||||
#[rustfmt::skip]
|
||||
static TRANS_IDX_MPS: [u8; 64] = [
|
||||
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, 62, 63,
|
||||
];
|
||||
|
||||
/// Combined state transition: index = (pStateIdx << 1 | valMPS) * 2 + bin_is_lps.
|
||||
/// Each context is stored as `pStateIdx << 1 | valMPS`.
|
||||
pub(crate) static NEXT_STATE: [[u8; 2]; 128] = {
|
||||
let mut t = [[0u8; 2]; 128];
|
||||
let mut s = 0;
|
||||
while s < 128 {
|
||||
let p = s >> 1;
|
||||
let mps = s & 1;
|
||||
// MPS path
|
||||
t[s][0] = (TRANS_IDX_MPS[p] << 1) | mps as u8;
|
||||
// LPS path
|
||||
let new_mps = if p == 0 { 1 - mps } else { mps };
|
||||
t[s][1] = (TRANS_IDX_LPS[p] << 1) | new_mps as u8;
|
||||
s += 1;
|
||||
}
|
||||
t
|
||||
};
|
||||
|
||||
/// Number of context variables used (0..=1023).
|
||||
pub const NUM_CTX: usize = 1024;
|
||||
|
||||
pub struct Cabac<'a> {
|
||||
data: &'a [u8],
|
||||
/// Byte position of the next byte to load into `value`.
|
||||
next_byte: usize,
|
||||
range: u32,
|
||||
/// codIOffset scaled by 2^bits: value = (codIOffset << bits) | (bits already loaded but not yet
|
||||
/// consumed by the arithmetic decoder).
|
||||
value: u64,
|
||||
bits: u32,
|
||||
pub ctx: [u8; NUM_CTX],
|
||||
}
|
||||
|
||||
impl<'a> Cabac<'a> {
|
||||
/// Create an engine over `data` (slice RBSP) starting at bit `pos` (must be byte aligned),
|
||||
/// with contexts initialised for `slice_qp` and `init` (0..=2 cabac_init_idc, 3 = I slice).
|
||||
pub fn new(data: &'a [u8], pos: usize, slice_qp: i32, init: usize) -> Result<Self> {
|
||||
let mut c = Cabac { data, next_byte: pos / 8, range: 0, value: 0, bits: 0, ctx: [0; NUM_CTX] };
|
||||
c.init_contexts(slice_qp, init);
|
||||
c.init_engine()?;
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
pub fn init_contexts(&mut self, slice_qp: i32, init: usize) {
|
||||
let qp = slice_qp.clamp(0, 51);
|
||||
let tab = &CABAC_INIT_MN[init];
|
||||
for (c, &(m, n)) in self.ctx.iter_mut().zip(tab.iter()) {
|
||||
let pre = (((m as i32) * qp) >> 4) + n as i32;
|
||||
let pre = pre.clamp(1, 126);
|
||||
*c = if pre <= 63 { ((63 - pre) << 1) as u8 } else { (((pre - 64) << 1) | 1) as u8 };
|
||||
}
|
||||
// ctxIdx 276 (end_of_slice / I_PCM) is handled by decode_terminate.
|
||||
}
|
||||
|
||||
/// 9.3.1.2: codIRange = 510, codIOffset = read_bits(9) (from the current byte-aligned position).
|
||||
pub fn init_engine(&mut self) -> Result<()> {
|
||||
self.range = 510;
|
||||
self.value = 0;
|
||||
self.bits = 0;
|
||||
self.refill();
|
||||
// the top 9 loaded bits form codIOffset; the remaining ones stay buffered below it
|
||||
self.bits -= 9;
|
||||
ensure!((self.value >> self.bits) < 510, "invalid CABAC offset at init");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Load 32 more bits (zeros past the end of the data).
|
||||
#[inline(always)]
|
||||
fn refill(&mut self) {
|
||||
let b = self.next_byte;
|
||||
let mut w = [0u8; 4];
|
||||
if let Some(src) = self.data.get(b..b + 4) {
|
||||
w.copy_from_slice(src);
|
||||
} else if b < self.data.len() {
|
||||
let n = self.data.len() - b;
|
||||
w[..n].copy_from_slice(&self.data[b..]);
|
||||
}
|
||||
self.next_byte += 4;
|
||||
self.value = (self.value << 32) | u32::from_be_bytes(w) as u64;
|
||||
self.bits += 32;
|
||||
}
|
||||
|
||||
/// Bit position of the next bit not consumed by the arithmetic decoder (for I_PCM alignment
|
||||
/// after decode_terminate returned 1).
|
||||
pub fn bit_pos(&self) -> usize {
|
||||
self.next_byte * 8 - self.bits as usize
|
||||
}
|
||||
/// Continue reading at byte-aligned bit position `pos` (call `init_engine` afterwards).
|
||||
pub fn set_bit_pos(&mut self, pos: usize) {
|
||||
self.next_byte = pos / 8;
|
||||
self.value = 0;
|
||||
self.bits = 0;
|
||||
}
|
||||
pub fn data(&self) -> &'a [u8] {
|
||||
self.data
|
||||
}
|
||||
/// True when the reader has run past the end of the data (corrupt stream).
|
||||
pub fn overrun(&self) -> bool {
|
||||
self.next_byte > self.data.len() + 16
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn renorm(&mut self) {
|
||||
// codIRange < 512 always, so the shift is 0 when no renormalisation is needed: no branch on
|
||||
// the (unpredictable) range, only on the rare refill.
|
||||
let shift = self.range.leading_zeros() - 23;
|
||||
self.range <<= shift;
|
||||
if self.bits < shift {
|
||||
self.refill();
|
||||
}
|
||||
self.bits -= shift;
|
||||
}
|
||||
|
||||
/// 9.3.3.2.1, branch-free: the MPS / LPS outcome of a context-coded bin is close to random
|
||||
/// at high bit rates, so it selects values instead of jumping.
|
||||
#[inline(always)]
|
||||
pub fn decode_decision(&mut self, ctx_idx: usize) -> u32 {
|
||||
// Every ctxIdx is < 1024 and every state < 128: the masks only let the compiler drop the
|
||||
// bounds checks from the per-bin path.
|
||||
let ctx_idx = ctx_idx & (NUM_CTX - 1);
|
||||
let s = (self.ctx[ctx_idx] & 127) as usize;
|
||||
let q = ((self.range >> 6) & 3) as usize;
|
||||
let lps = RANGE_TAB_LPS[s >> 1][q] as u32;
|
||||
let mps_range = self.range - lps;
|
||||
let scaled = (mps_range as u64) << self.bits;
|
||||
let is_lps = self.value >= scaled;
|
||||
self.value -= scaled & (is_lps as u64).wrapping_neg();
|
||||
self.range = if is_lps { lps } else { mps_range };
|
||||
self.ctx[ctx_idx] = NEXT_STATE[s][is_lps as usize];
|
||||
self.renorm();
|
||||
(s & 1) as u32 ^ is_lps as u32
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn decode_bypass(&mut self) -> u32 {
|
||||
if self.bits == 0 {
|
||||
self.refill();
|
||||
}
|
||||
self.bits -= 1;
|
||||
let scaled = (self.range as u64) << self.bits;
|
||||
let one = self.value >= scaled;
|
||||
self.value -= scaled & (one as u64).wrapping_neg();
|
||||
one as u32
|
||||
}
|
||||
|
||||
pub fn decode_terminate(&mut self) -> u32 {
|
||||
self.range -= 2;
|
||||
let scaled = (self.range as u64) << self.bits;
|
||||
if self.value >= scaled {
|
||||
1
|
||||
} else {
|
||||
self.renorm();
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Minimal CABAC encoder (9.3.4, informative) used to round-trip test the decoder.
|
||||
struct Enc {
|
||||
low: u32,
|
||||
range: u32,
|
||||
outstanding: u32,
|
||||
first: bool,
|
||||
bits: Vec<bool>,
|
||||
ctx: Vec<u8>,
|
||||
}
|
||||
impl Enc {
|
||||
fn new(ctx: &[u8]) -> Self {
|
||||
Enc { low: 0, range: 510, outstanding: 0, first: true, bits: vec![], ctx: ctx.to_vec() }
|
||||
}
|
||||
fn put(&mut self, b: bool) {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
self.bits.push(b);
|
||||
}
|
||||
while self.outstanding > 0 {
|
||||
self.bits.push(!b);
|
||||
self.outstanding -= 1;
|
||||
}
|
||||
}
|
||||
fn renorm(&mut self) {
|
||||
while self.range < 256 {
|
||||
if self.low < 256 {
|
||||
self.put(false);
|
||||
} else if self.low >= 512 {
|
||||
self.low -= 512;
|
||||
self.put(true);
|
||||
} else {
|
||||
self.low -= 256;
|
||||
self.outstanding += 1;
|
||||
}
|
||||
self.range <<= 1;
|
||||
self.low <<= 1;
|
||||
}
|
||||
}
|
||||
fn encode(&mut self, ctx_idx: usize, bin: u32) {
|
||||
let s = self.ctx[ctx_idx] as usize;
|
||||
let p = s >> 1;
|
||||
let mps = (s & 1) as u32;
|
||||
let q = ((self.range >> 6) & 3) as usize;
|
||||
let lps = RANGE_TAB_LPS[p][q] as u32;
|
||||
self.range -= lps;
|
||||
if bin != mps {
|
||||
self.low += self.range;
|
||||
self.range = lps;
|
||||
self.ctx[ctx_idx] = NEXT_STATE[s][1];
|
||||
} else {
|
||||
self.ctx[ctx_idx] = NEXT_STATE[s][0];
|
||||
}
|
||||
self.renorm();
|
||||
}
|
||||
fn bypass(&mut self, bin: u32) {
|
||||
self.low <<= 1;
|
||||
if bin != 0 {
|
||||
self.low += self.range;
|
||||
}
|
||||
if self.low >= 1024 {
|
||||
self.put(true);
|
||||
self.low -= 1024;
|
||||
} else if self.low < 512 {
|
||||
self.put(false);
|
||||
} else {
|
||||
self.low -= 512;
|
||||
self.outstanding += 1;
|
||||
}
|
||||
}
|
||||
fn terminate_flush(&mut self) -> Vec<u8> {
|
||||
// encode terminate bin = 1 then flush (9.3.4.5)
|
||||
self.range -= 2;
|
||||
self.low += self.range;
|
||||
self.range = 2;
|
||||
self.renorm();
|
||||
self.put((self.low >> 9) & 1 != 0);
|
||||
self.bits.push((self.low >> 8) & 1 != 0);
|
||||
self.bits.push(true); // rbsp_stop_one_bit
|
||||
while !self.bits.len().is_multiple_of(8) {
|
||||
self.bits.push(false);
|
||||
}
|
||||
self.bits.chunks(8).map(|c| c.iter().fold(0u8, |a, &b| (a << 1) | b as u8)).collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn context_init_formula() {
|
||||
let c = Cabac::new(&[0, 0, 0, 0], 0, 26, 3).unwrap();
|
||||
// ctxIdx 0: m=20, n=-15 at QP 26: ((20*26)>>4) - 15 = 32 - 15 = 17 -> pState 46, MPS 0
|
||||
assert_eq!(c.ctx[0], 46 << 1);
|
||||
// ctxIdx 60: m=0,n=41 -> pre 41 -> pState 22 MPS 0
|
||||
assert_eq!(c.ctx[60], 22 << 1);
|
||||
// ctxIdx 70 (I): m=0 n=11 -> pState 52 MPS 0
|
||||
assert_eq!(c.ctx[70], 52 << 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trip_random_bins() {
|
||||
let mut seed = 12345u32;
|
||||
let mut rnd = || {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 17;
|
||||
seed ^= seed << 5;
|
||||
seed
|
||||
};
|
||||
let dummy = Cabac::new(&[0, 0, 0, 0], 0, 30, 0).unwrap();
|
||||
let mut enc = Enc::new(&dummy.ctx);
|
||||
let mut ops = Vec::new();
|
||||
for _ in 0..5000 {
|
||||
let kind = rnd() % 4;
|
||||
let ctx = (rnd() % 16) as usize;
|
||||
// skewed bins so contexts adapt
|
||||
let bin = if rnd() % 10 < 8 { (ctx & 1) as u32 } else { 1 - (ctx & 1) as u32 };
|
||||
if kind == 0 {
|
||||
enc.bypass(bin);
|
||||
} else {
|
||||
enc.encode(ctx, bin);
|
||||
}
|
||||
ops.push((kind, ctx, bin));
|
||||
}
|
||||
let bytes = enc.terminate_flush();
|
||||
let mut dec = Cabac::new(&bytes, 0, 30, 0).unwrap();
|
||||
for (i, &(kind, ctx, bin)) in ops.iter().enumerate() {
|
||||
let got = if kind == 0 { dec.decode_bypass() } else { dec.decode_decision(ctx) };
|
||||
assert_eq!(got, bin, "bin {i}");
|
||||
}
|
||||
assert_eq!(dec.decode_terminate(), 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,709 @@
|
||||
//! CABAC slice data and macroblock layer parsing (7.3.4, 7.3.5 with 9.3 binarisations and context
|
||||
//! index derivations).
|
||||
|
||||
use crate::h264::decode::cabac::Cabac;
|
||||
use crate::h264::decode::error::{Result, ensure, invalid};
|
||||
use crate::h264::decode::mbtypes::{MbTypeInfo, Part, SubMbInfo, b_mb_type, b_sub_mb_type, intra_mb_type, p_mb_type, p_sub_mb_type};
|
||||
use crate::h264::decode::picture::MbKind;
|
||||
use crate::h264::decode::slice::SliceType;
|
||||
use crate::h264::decode::slicedec::{MbCur, SliceDecoder};
|
||||
use crate::h264::decode::tables::{ZIGZAG4, ZIGZAG8};
|
||||
|
||||
/// Table 9-43: significant_coeff_flag ctxIdxInc for 8x8 frame blocks.
|
||||
#[rustfmt::skip]
|
||||
const SIG8_FRAME: [u8; 63] = [
|
||||
0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9, 10, 9, 8, 7,
|
||||
7, 6, 11, 12, 13, 11, 6, 7, 8, 9, 14, 10, 9, 8, 6, 11, 12, 13, 11, 6, 9, 14, 10, 9, 11, 12, 13, 11, 14, 10, 12,
|
||||
];
|
||||
/// Table 9-43: last_significant_coeff_flag ctxIdxInc for 8x8 blocks.
|
||||
#[rustfmt::skip]
|
||||
const LAST8: [u8; 63] = [
|
||||
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8,
|
||||
];
|
||||
|
||||
/// ctxIdxInc = levelListIdx (ctxBlockCat 0, 1, 2, 4).
|
||||
const IDENT_CTX: [u8; 64] = {
|
||||
let mut t = [0u8; 64];
|
||||
let mut i = 0;
|
||||
while i < 64 {
|
||||
t[i] = i as u8;
|
||||
i += 1;
|
||||
}
|
||||
t
|
||||
};
|
||||
/// ctxIdxInc = Min(levelListIdx / NumC8x8, 2) for 4:2:0 chroma DC (ctxBlockCat 3).
|
||||
const CHROMA_DC_CTX: [u8; 4] = [0, 1, 2, 2];
|
||||
|
||||
/// Table 9-40 ctxIdxBlockCatOffset for ctxBlockCat 0..=5: (coded_block_flag, significant/last, abs level).
|
||||
const CAT_OFFSET: [(usize, usize, usize); 6] = [(0, 0, 0), (4, 15, 10), (8, 29, 20), (12, 44, 30), (16, 47, 39), (0, 0, 0)];
|
||||
|
||||
impl SliceDecoder<'_> {
|
||||
/// Decode slice_data() with CABAC. `rbsp` is the whole slice RBSP.
|
||||
pub fn decode_cabac(&mut self, rbsp: &[u8]) -> Result<()> {
|
||||
let start = self.sh.header_bits.div_ceil(8) * 8;
|
||||
let init = if self.sh.slice_type.is_intra() { 3 } else { self.sh.cabac_init_idc as usize };
|
||||
let mut c = Cabac::new(rbsp, start, self.sh.qp(self.pps), init)?;
|
||||
let total = self.mb_w * self.mb_h;
|
||||
let mut addr = self.sh.first_mb_in_slice as usize;
|
||||
let intra_slice = self.sh.slice_type.is_intra();
|
||||
loop {
|
||||
ensure!(addr < total, "macroblock address {addr} out of range");
|
||||
self.pic.reserve_mbs(1)?;
|
||||
self.start_mb(addr);
|
||||
let skip = !intra_slice && self.decode_skip_flag(&mut c);
|
||||
if skip {
|
||||
self.decode_skip()?;
|
||||
} else {
|
||||
self.decode_mb_cabac(&mut c)?;
|
||||
}
|
||||
self.pic.mb_done(addr)?;
|
||||
ensure!(!c.overrun(), "CABAC read past end of slice data");
|
||||
if c.decode_terminate() == 1 {
|
||||
break;
|
||||
}
|
||||
addr += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode_skip_flag(&mut self, c: &mut Cabac) -> bool {
|
||||
let cond = |n: Option<usize>| n.map(|m| !self.pic.mbs[m].kind_is_skip()).unwrap_or(false) as usize;
|
||||
let inc = cond(self.nb[0]) + cond(self.nb[1]);
|
||||
let base = if self.sh.slice_type.is_b() { 24 } else { 11 };
|
||||
c.decode_decision(base + inc) == 1
|
||||
}
|
||||
|
||||
/// Intra mb_type suffix / I-slice mb_type (Table 9-36). `base` is 3 (I slice) or 17/32 (prefix
|
||||
/// offsets in P/B slices). Returns the I-slice mb_type value 0..=25.
|
||||
fn decode_mb_type_i(&mut self, c: &mut Cabac, base: usize) -> u32 {
|
||||
let islice = base == 3;
|
||||
let b0 = if islice {
|
||||
let cond = |n: Option<usize>| n.map(|m| !matches!(self.pic.mbs[m].kind, MbKind::I4x4 | MbKind::I8x8)).unwrap_or(false) as usize;
|
||||
let inc = cond(self.nb[0]) + cond(self.nb[1]);
|
||||
c.decode_decision(base + inc)
|
||||
} else {
|
||||
c.decode_decision(base)
|
||||
};
|
||||
if b0 == 0 {
|
||||
return 0;
|
||||
}
|
||||
if c.decode_terminate() == 1 {
|
||||
return 25;
|
||||
}
|
||||
let (c_luma, c_chroma, c_chroma2, c_pred0, c_pred1) =
|
||||
if islice { (base + 3, base + 4, base + 5, base + 6, base + 7) } else { (base + 1, base + 2, base + 2, base + 3, base + 3) };
|
||||
let luma = c.decode_decision(c_luma);
|
||||
let mut chroma = c.decode_decision(c_chroma);
|
||||
if chroma != 0 {
|
||||
chroma += c.decode_decision(c_chroma2);
|
||||
}
|
||||
let p0 = c.decode_decision(c_pred0);
|
||||
let p1 = c.decode_decision(c_pred1);
|
||||
1 + (p0 * 2 + p1) + 4 * chroma + 12 * luma
|
||||
}
|
||||
|
||||
fn decode_mb_type(&mut self, c: &mut Cabac) -> Result<MbTypeInfo> {
|
||||
let info = match self.sh.slice_type {
|
||||
SliceType::I | SliceType::Si => intra_mb_type(self.decode_mb_type_i(c, 3)),
|
||||
SliceType::P | SliceType::Sp => {
|
||||
if c.decode_decision(14) == 1 {
|
||||
intra_mb_type(self.decode_mb_type_i(c, 17))
|
||||
} else if c.decode_decision(15) == 0 {
|
||||
p_mb_type(if c.decode_decision(16) == 0 { 0 } else { 3 })
|
||||
} else {
|
||||
p_mb_type(if c.decode_decision(17) == 1 { 1 } else { 2 })
|
||||
}
|
||||
}
|
||||
SliceType::B => {
|
||||
let cond =
|
||||
|n: Option<usize>| n.map(|m| !matches!(self.pic.mbs[m].kind, MbKind::BSkip | MbKind::BDirect16x16)).unwrap_or(false) as usize;
|
||||
let inc = cond(self.nb[0]) + cond(self.nb[1]);
|
||||
if c.decode_decision(27 + inc) == 0 {
|
||||
b_mb_type(0)
|
||||
} else if c.decode_decision(27 + 3) == 0 {
|
||||
b_mb_type(1 + c.decode_decision(27 + 5))
|
||||
} else {
|
||||
let b2 = c.decode_decision(27 + 4);
|
||||
let b3 = c.decode_decision(27 + 5);
|
||||
let b4 = c.decode_decision(27 + 5);
|
||||
let b5 = c.decode_decision(27 + 5);
|
||||
let v = (b2 << 3) | (b3 << 2) | (b4 << 1) | b5;
|
||||
match v {
|
||||
0b1101 => intra_mb_type(self.decode_mb_type_i(c, 32)),
|
||||
0b1111 => b_mb_type(22),
|
||||
0b1110 => b_mb_type(11),
|
||||
_ if b2 == 0 => b_mb_type(3 + v),
|
||||
_ => {
|
||||
let b6 = c.decode_decision(27 + 5);
|
||||
b_mb_type(12 + (((v & 7) << 1) | b6))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
match info {
|
||||
Some(i) => Ok(i),
|
||||
None => invalid("invalid CABAC mb_type"),
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_sub_mb_type(&mut self, c: &mut Cabac) -> Result<SubMbInfo> {
|
||||
let r = if self.sh.slice_type.is_b() {
|
||||
if c.decode_decision(36) == 0 {
|
||||
b_sub_mb_type(0)
|
||||
} else if c.decode_decision(37) == 0 {
|
||||
b_sub_mb_type(1 + c.decode_decision(39))
|
||||
} else if c.decode_decision(38) == 1 {
|
||||
if c.decode_decision(39) == 1 {
|
||||
b_sub_mb_type(11 + c.decode_decision(39))
|
||||
} else {
|
||||
let b4 = c.decode_decision(39);
|
||||
let b5 = c.decode_decision(39);
|
||||
b_sub_mb_type(7 + (b4 << 1) + b5)
|
||||
}
|
||||
} else {
|
||||
let b3 = c.decode_decision(39);
|
||||
let b4 = c.decode_decision(39);
|
||||
b_sub_mb_type(3 + (b3 << 1) + b4)
|
||||
}
|
||||
} else if c.decode_decision(21) == 1 {
|
||||
p_sub_mb_type(0)
|
||||
} else if c.decode_decision(22) == 0 {
|
||||
p_sub_mb_type(1)
|
||||
} else {
|
||||
p_sub_mb_type(if c.decode_decision(23) == 1 { 2 } else { 3 })
|
||||
};
|
||||
match r {
|
||||
Some(s) => Ok(s),
|
||||
None => invalid("invalid CABAC sub_mb_type"),
|
||||
}
|
||||
}
|
||||
|
||||
/// ref_idx_lX for the partition whose top-left 4x4 block is (x, y) (9.3.3.1.1.6).
|
||||
fn decode_ref_idx(&mut self, c: &mut Cabac, list: usize, x: usize, y: usize) -> u32 {
|
||||
let cond = |xx: i32, yy: i32| -> usize {
|
||||
let (mb, bx, by) = if xx < 0 {
|
||||
(self.nb[0], 3usize, yy as usize)
|
||||
} else if yy < 0 {
|
||||
(self.nb[1], xx as usize, 3usize)
|
||||
} else {
|
||||
(Some(self.mb_addr), xx as usize, yy as usize)
|
||||
};
|
||||
let Some(m) = mb else { return 0 };
|
||||
let st = &self.pic.mbs[m];
|
||||
let b8 = (by >> 1) * 2 + (bx >> 1);
|
||||
if st.kind.is_intra() || st.kind_is_skip() || st.direct8x8 & (1 << b8) != 0 {
|
||||
return 0;
|
||||
}
|
||||
(st.ref_idx[list][b8] > 0) as usize
|
||||
};
|
||||
let inc = cond(x as i32 - 1, y as i32) + 2 * cond(x as i32, y as i32 - 1);
|
||||
let mut v = 0u32;
|
||||
if c.decode_decision(54 + inc) == 1 {
|
||||
v = 1;
|
||||
if c.decode_decision(54 + 4) == 1 {
|
||||
v = 2;
|
||||
while c.decode_decision(54 + 5) == 1 {
|
||||
v += 1;
|
||||
if v > 32 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// One mvd component for the (sub-)partition with top-left 4x4 block (x, y) (9.3.3.1.1.7, UEG3).
|
||||
fn decode_mvd(&mut self, c: &mut Cabac, list: usize, comp: usize, x: usize, y: usize) -> i32 {
|
||||
let absn = |xx: i32, yy: i32| -> u32 {
|
||||
let (mb, bx, by) = if xx < 0 {
|
||||
(self.nb[0], 3usize, yy as usize)
|
||||
} else if yy < 0 {
|
||||
(self.nb[1], xx as usize, 3usize)
|
||||
} else {
|
||||
(Some(self.mb_addr), xx as usize, yy as usize)
|
||||
};
|
||||
match mb {
|
||||
Some(m) => self.pic.mbs[m].mvd[list][by * 4 + bx][comp] as u32,
|
||||
None => 0,
|
||||
}
|
||||
};
|
||||
let sum = absn(x as i32 - 1, y as i32) + absn(x as i32, y as i32 - 1);
|
||||
let inc = if sum > 32 {
|
||||
2
|
||||
} else if sum > 2 {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let base = if comp == 0 { 40 } else { 47 };
|
||||
if c.decode_decision(base + inc) == 0 {
|
||||
return 0;
|
||||
}
|
||||
let mut prefix = 1;
|
||||
while prefix < 9 && c.decode_decision(base + (prefix + 2).min(6)) == 1 {
|
||||
prefix += 1;
|
||||
}
|
||||
let mut val = prefix as i32;
|
||||
if prefix >= 9 {
|
||||
// EG3 suffix
|
||||
let mut k = 3;
|
||||
while c.decode_bypass() == 1 {
|
||||
val += 1 << k;
|
||||
k += 1;
|
||||
if k > 24 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
while k > 0 {
|
||||
k -= 1;
|
||||
val += (c.decode_bypass() as i32) << k;
|
||||
}
|
||||
}
|
||||
if c.decode_bypass() == 1 { -val } else { val }
|
||||
}
|
||||
|
||||
fn set_cur_ref(&mut self, list: usize, x: usize, y: usize, w: usize, h: usize, r: i8) {
|
||||
let st = self.mb_mut();
|
||||
for yy in (y..y + h).step_by(2) {
|
||||
for xx in (x..x + w).step_by(2) {
|
||||
st.ref_idx[list][(yy >> 1) * 2 + (xx >> 1)] = r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_cur_mvd(&mut self, list: usize, x: usize, y: usize, w: usize, h: usize, d: [i32; 2]) {
|
||||
let a = [d[0].unsigned_abs().min(127) as u8, d[1].unsigned_abs().min(127) as u8];
|
||||
let st = self.mb_mut();
|
||||
for yy in y..y + h {
|
||||
for xx in x..x + w {
|
||||
st.mvd[list][yy * 4 + xx] = a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_cbp(&mut self, c: &mut Cabac) -> u8 {
|
||||
let mut luma = 0u8;
|
||||
for b8 in 0..4usize {
|
||||
let (bx, by) = ((b8 & 1) * 2, (b8 >> 1) * 2);
|
||||
// A
|
||||
let ca = if bx > 0 { ((luma >> (b8 - 1)) & 1 == 0) as usize } else { self.cbp_luma_cond(self.nb[0], b8 + 1) };
|
||||
let cb = if by > 0 { ((luma >> (b8 - 2)) & 1 == 0) as usize } else { self.cbp_luma_cond(self.nb[1], b8 + 2) };
|
||||
let _ = (bx, by);
|
||||
let bin = c.decode_decision(73 + ca + 2 * cb);
|
||||
luma |= (bin as u8) << b8;
|
||||
}
|
||||
let cond = |n: Option<usize>, bin: usize| -> usize {
|
||||
let Some(m) = n else { return 0 };
|
||||
let st = &self.pic.mbs[m];
|
||||
if st.kind == MbKind::IPcm {
|
||||
return 1;
|
||||
}
|
||||
if st.kind_is_skip() {
|
||||
return 0;
|
||||
}
|
||||
let ch = st.cbp >> 4;
|
||||
if bin == 0 { (ch != 0) as usize } else { (ch == 2) as usize }
|
||||
};
|
||||
let inc0 = cond(self.nb[0], 0) + 2 * cond(self.nb[1], 0);
|
||||
let mut chroma = 0u8;
|
||||
if c.decode_decision(77 + inc0) == 1 {
|
||||
let inc1 = cond(self.nb[0], 1) + 2 * cond(self.nb[1], 1) + 4;
|
||||
chroma = 1 + c.decode_decision(77 + inc1) as u8;
|
||||
}
|
||||
luma | (chroma << 4)
|
||||
}
|
||||
|
||||
/// condTermFlagN for coded_block_pattern luma bin from a neighbouring MB; `b8n` is the neighbouring
|
||||
/// 8x8 block index within that MB.
|
||||
fn cbp_luma_cond(&self, n: Option<usize>, b8n: usize) -> usize {
|
||||
let Some(m) = n else { return 0 };
|
||||
let st = &self.pic.mbs[m];
|
||||
if st.kind == MbKind::IPcm {
|
||||
return 0;
|
||||
}
|
||||
if st.kind_is_skip() {
|
||||
return 1;
|
||||
}
|
||||
((st.cbp >> (b8n & 3)) & 1 == 0) as usize
|
||||
}
|
||||
|
||||
fn decode_qp_delta(&mut self, c: &mut Cabac) -> Result<i32> {
|
||||
let inc = self.prev_qp_delta_nz as usize;
|
||||
if c.decode_decision(60 + inc) == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
let mut k = 1u32;
|
||||
let mut ctx = 62;
|
||||
while c.decode_decision(ctx) == 1 {
|
||||
k += 1;
|
||||
ctx = 63;
|
||||
ensure!(k <= 128, "mb_qp_delta too large");
|
||||
}
|
||||
let v = k.div_ceil(2) as i32;
|
||||
Ok(if k % 2 == 1 { v } else { -v })
|
||||
}
|
||||
|
||||
/// coded_block_flag ctxIdxInc condition for a neighbouring luma 4x4 block (cat 0..2) or chroma.
|
||||
fn cbf_luma_cond(&self, bx: i32, by: i32, intra_cur: bool) -> usize {
|
||||
if bx >= 0 && by >= 0 {
|
||||
return (self.mb().nnz[(by * 4 + bx) as usize] != 0) as usize;
|
||||
}
|
||||
let n = if bx < 0 { self.nb[0] } else { self.nb[1] };
|
||||
let Some(m) = n else { return intra_cur as usize };
|
||||
let st = &self.pic.mbs[m];
|
||||
let (x, y) = if bx < 0 { (3, by as usize) } else { (bx as usize, 3) };
|
||||
(st.nnz[y * 4 + x] != 0) as usize
|
||||
}
|
||||
|
||||
fn cbf_dc_cond(&self, n: Option<usize>, bit: u8, intra_cur: bool) -> usize {
|
||||
let Some(m) = n else { return intra_cur as usize };
|
||||
let st = &self.pic.mbs[m];
|
||||
if st.kind == MbKind::IPcm {
|
||||
return 1;
|
||||
}
|
||||
(st.cbf_dc & (1 << bit) != 0) as usize
|
||||
}
|
||||
|
||||
fn cbf_chroma_ac_cond(&self, comp: usize, bx: i32, by: i32, intra_cur: bool) -> usize {
|
||||
if bx >= 0 && by >= 0 {
|
||||
return (self.mb().nnz_c[comp][(by * 2 + bx) as usize] != 0) as usize;
|
||||
}
|
||||
let n = if bx < 0 { self.nb[0] } else { self.nb[1] };
|
||||
let Some(m) = n else { return intra_cur as usize };
|
||||
let st = &self.pic.mbs[m];
|
||||
let (x, y) = if bx < 0 { (1, by as usize) } else { (bx as usize, 1) };
|
||||
(st.nnz_c[comp][y * 2 + x] != 0) as usize
|
||||
}
|
||||
|
||||
/// residual_block_cabac for ctxBlockCat `CAT` (0..=5). Each non-zero coefficient is passed to
|
||||
/// `put` as (index in the coefficient list, level); the others are zero. `cbf_inc` is the
|
||||
/// coded_block_flag ctxIdxInc (ignored for cat 5). Returns the number of non-zero coefficients.
|
||||
fn residual_block_cabac<const CAT: usize>(c: &mut Cabac, cbf_inc: usize, mut put: impl FnMut(usize, i32)) -> u8 {
|
||||
if CAT != 5 && c.decode_decision(85 + CAT_OFFSET[CAT].0 + cbf_inc) == 0 {
|
||||
return 0;
|
||||
}
|
||||
let max_num = [16, 15, 16, 4, 15, 64][CAT];
|
||||
let (sig_base, last_base, abs_base) =
|
||||
if CAT == 5 { (402, 417, 426) } else { (105 + CAT_OFFSET[CAT].1, 166 + CAT_OFFSET[CAT].1, 227 + CAT_OFFSET[CAT].2) };
|
||||
let (sig_tab, last_tab): (&[u8], &[u8]) = match CAT {
|
||||
3 => (&CHROMA_DC_CTX, &CHROMA_DC_CTX),
|
||||
5 => (&SIG8_FRAME, &LAST8),
|
||||
_ => (&IDENT_CTX, &IDENT_CTX),
|
||||
};
|
||||
// significance map: positions of the significant coefficients in scan order
|
||||
let mut pos = [0u8; 64];
|
||||
let mut n = 0usize;
|
||||
let last_i = max_num - 1;
|
||||
let mut found_last = false;
|
||||
for i in 0..last_i {
|
||||
if c.decode_decision(sig_base + sig_tab[i] as usize) == 1 {
|
||||
pos[n] = i as u8;
|
||||
n += 1;
|
||||
if c.decode_decision(last_base + last_tab[i] as usize) == 1 {
|
||||
found_last = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found_last {
|
||||
pos[n] = last_i as u8;
|
||||
n += 1;
|
||||
}
|
||||
let mut eq1 = 0usize;
|
||||
let mut gt1 = 0usize;
|
||||
let gt1_cap = if CAT == 3 { 3 } else { 4 };
|
||||
for &p in pos[..n].iter().rev() {
|
||||
let inc0 = if gt1 != 0 { 0 } else { (1 + eq1).min(4) };
|
||||
let mut abs = 1i32;
|
||||
if c.decode_decision(abs_base + inc0) == 1 {
|
||||
let ctx = abs_base + 5 + gt1.min(gt1_cap);
|
||||
let mut prefix = 1;
|
||||
while prefix < 14 && c.decode_decision(ctx) == 1 {
|
||||
prefix += 1;
|
||||
}
|
||||
abs = prefix + 1;
|
||||
if prefix == 14 {
|
||||
let mut k = 0;
|
||||
let mut suf = 0i32;
|
||||
while c.decode_bypass() == 1 {
|
||||
suf += 1 << k;
|
||||
k += 1;
|
||||
if k > 24 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
while k > 0 {
|
||||
k -= 1;
|
||||
suf += (c.decode_bypass() as i32) << k;
|
||||
}
|
||||
abs += suf;
|
||||
}
|
||||
gt1 += 1;
|
||||
} else {
|
||||
eq1 += 1;
|
||||
}
|
||||
put(p as usize, if c.decode_bypass() == 1 { -abs } else { abs });
|
||||
}
|
||||
n as u8
|
||||
}
|
||||
|
||||
fn residual_cabac(&mut self, c: &mut Cabac, cbp: u8) -> Result<()> {
|
||||
let kind = self.mb().kind;
|
||||
let intra = kind.is_intra();
|
||||
let t8 = self.mb().transform_8x8;
|
||||
if kind == MbKind::I16x16 {
|
||||
let inc = self.cbf_dc_cond(self.nb[0], 0, intra) + 2 * self.cbf_dc_cond(self.nb[1], 0, intra);
|
||||
let mut lv = [0i32; 16];
|
||||
if Self::residual_block_cabac::<0>(c, inc, |k, v| lv[k] = v) > 0 {
|
||||
self.mb_mut().cbf_dc |= 1;
|
||||
self.put_luma_dc(&lv);
|
||||
}
|
||||
}
|
||||
for b8 in 0..4 {
|
||||
let (bx8, by8) = ((b8 & 1) * 2, (b8 >> 1) * 2);
|
||||
if cbp & (1 << b8) == 0 {
|
||||
continue;
|
||||
}
|
||||
if t8 {
|
||||
let (blk, ls, dq) = self.luma8_dst(b8);
|
||||
let mut ac = false;
|
||||
Self::residual_block_cabac::<5>(c, 0, |k, v| {
|
||||
let p = ZIGZAG8[k] as usize;
|
||||
ac |= p != 0;
|
||||
blk[p] = dq.apply(v, ls[p]);
|
||||
});
|
||||
self.mark_luma8(b8, ac);
|
||||
let st = self.mb_mut();
|
||||
for r in [by8 * 4 + bx8, by8 * 4 + bx8 + 1, (by8 + 1) * 4 + bx8, (by8 + 1) * 4 + bx8 + 1] {
|
||||
st.nnz[r] = 1;
|
||||
}
|
||||
} else {
|
||||
for i4 in 0..4 {
|
||||
let (bx, by) = (bx8 + (i4 & 1), by8 + (i4 >> 1));
|
||||
let raster = by * 4 + bx;
|
||||
let inc = self.cbf_luma_cond(bx as i32 - 1, by as i32, intra) + 2 * self.cbf_luma_cond(bx as i32, by as i32 - 1, intra);
|
||||
let (blk, ls, dq) = self.luma4_dst(raster);
|
||||
let mut ac = false;
|
||||
let n = if kind == MbKind::I16x16 {
|
||||
Self::residual_block_cabac::<1>(c, inc, |k, v| {
|
||||
let p = ZIGZAG4[k + 1] as usize;
|
||||
ac = true;
|
||||
blk[p] = dq.apply(v, ls[p]);
|
||||
})
|
||||
} else {
|
||||
Self::residual_block_cabac::<2>(c, inc, |k, v| {
|
||||
let p = ZIGZAG4[k] as usize;
|
||||
ac |= p != 0;
|
||||
blk[p] = dq.apply(v, ls[p]);
|
||||
})
|
||||
};
|
||||
self.mb_mut().nnz[raster] = n;
|
||||
if n > 0 {
|
||||
self.mark_luma4(raster, ac);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let cbp_c = cbp >> 4;
|
||||
if cbp_c != 0 {
|
||||
for comp in 0..2 {
|
||||
let bit = 1 + comp as u8;
|
||||
let inc = self.cbf_dc_cond(self.nb[0], bit, intra) + 2 * self.cbf_dc_cond(self.nb[1], bit, intra);
|
||||
let mut lv = [0i32; 4];
|
||||
if Self::residual_block_cabac::<3>(c, inc, |k, v| lv[k] = v) > 0 {
|
||||
self.mb_mut().cbf_dc |= 1 << bit;
|
||||
self.put_chroma_dc(comp, &lv);
|
||||
}
|
||||
}
|
||||
}
|
||||
if cbp_c == 2 {
|
||||
for comp in 0..2 {
|
||||
for b in 0..4 {
|
||||
let (bx, by) = ((b & 1) as i32, (b >> 1) as i32);
|
||||
let inc = self.cbf_chroma_ac_cond(comp, bx - 1, by, intra) + 2 * self.cbf_chroma_ac_cond(comp, bx, by - 1, intra);
|
||||
let (blk, ls, dq) = self.chroma_ac_dst(comp, b);
|
||||
let n = Self::residual_block_cabac::<4>(c, inc, |k, v| {
|
||||
let p = ZIGZAG4[k + 1] as usize;
|
||||
blk[p] = dq.apply(v, ls[p]);
|
||||
});
|
||||
self.mb_mut().nnz_c[comp][b] = n;
|
||||
if n > 0 {
|
||||
self.mark_chroma_ac(comp, b, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Parse and reconstruct one macroblock_layer() with CABAC.
|
||||
fn decode_mb_cabac(&mut self, c: &mut Cabac) -> Result<()> {
|
||||
let mut info = self.decode_mb_type(c)?;
|
||||
self.cur = MbCur { info, ..Default::default() };
|
||||
if info.kind == MbKind::IPcm {
|
||||
// pcm alignment + samples, then re-initialise the arithmetic decoder
|
||||
let pos = c.bit_pos().div_ceil(8);
|
||||
let data = c.data();
|
||||
ensure!(data.len() >= pos + 384, "truncated I_PCM macroblock");
|
||||
let Some(&samples) = data.get(pos..).and_then(|d| d.first_chunk::<384>()) else {
|
||||
return invalid("truncated I_PCM macroblock");
|
||||
};
|
||||
c.set_bit_pos((pos + 384) * 8);
|
||||
c.init_engine()?;
|
||||
self.finish_pcm();
|
||||
self.write_pcm(&samples);
|
||||
self.prev_qp_delta_nz = false;
|
||||
return Ok(());
|
||||
}
|
||||
let intra = info.kind.is_intra();
|
||||
if info.kind == MbKind::I4x4 && self.pps.transform_8x8_mode && self.decode_t8x8_flag(c) {
|
||||
info.kind = MbKind::I8x8;
|
||||
self.cur.info.kind = MbKind::I8x8;
|
||||
self.mb_mut().transform_8x8 = true;
|
||||
}
|
||||
self.mb_mut().kind = info.kind;
|
||||
let n_ref = self.sh.num_ref_idx_active;
|
||||
match info.kind {
|
||||
MbKind::I4x4 | MbKind::I8x8 => {
|
||||
let n = if info.kind == MbKind::I8x8 { 4 } else { 16 };
|
||||
for i in 0..n {
|
||||
self.cur.rem_mode[i] = if c.decode_decision(68) == 1 {
|
||||
-1
|
||||
} else {
|
||||
let b0 = c.decode_decision(69);
|
||||
let b1 = c.decode_decision(69);
|
||||
let b2 = c.decode_decision(69);
|
||||
(b0 | (b1 << 1) | (b2 << 2)) as i8
|
||||
};
|
||||
}
|
||||
self.derive_intra_modes(info.kind == MbKind::I8x8);
|
||||
let m = self.decode_chroma_mode(c);
|
||||
self.mb_mut().intra_chroma_mode = m;
|
||||
}
|
||||
MbKind::I16x16 => {
|
||||
let m = self.decode_chroma_mode(c);
|
||||
self.mb_mut().intra_chroma_mode = m;
|
||||
}
|
||||
MbKind::BDirect16x16 => {
|
||||
self.mb_mut().direct8x8 = 0xf;
|
||||
}
|
||||
_ => {
|
||||
if info.part == Part::P8x8 {
|
||||
for p in 0..4 {
|
||||
self.cur.sub[p] = self.decode_sub_mb_type(c)?;
|
||||
}
|
||||
let direct_mask: u8 = (0..4).filter(|&p| self.cur.sub[p].direct).map(|p| 1u8 << p).sum();
|
||||
self.mb_mut().direct8x8 = direct_mask;
|
||||
for l in 0..2 {
|
||||
for p in 0..4 {
|
||||
let sub = self.cur.sub[p];
|
||||
if sub.direct || sub.pred & (1 << l) == 0 {
|
||||
continue;
|
||||
}
|
||||
let (x, y) = ((p & 1) * 2, (p >> 1) * 2);
|
||||
let r = if n_ref[l] > 1 && !info.ref0 { self.decode_ref_idx(c, l, x, y) as i8 } else { 0 };
|
||||
self.cur.ref_idx[l][p] = r;
|
||||
self.set_cur_ref(l, x, y, 2, 2, r);
|
||||
}
|
||||
}
|
||||
for l in 0..2 {
|
||||
for p in 0..4 {
|
||||
let sub = self.cur.sub[p];
|
||||
if sub.direct || sub.pred & (1 << l) == 0 {
|
||||
continue;
|
||||
}
|
||||
let (x8, y8) = ((p & 1) * 2, (p >> 1) * 2);
|
||||
for j in 0..sub.shape.num_parts() {
|
||||
let (sx, sy, w, h) = sub.shape.rect(j);
|
||||
let (x, y) = (x8 + sx, y8 + sy);
|
||||
let dx = self.decode_mvd(c, l, 0, x, y);
|
||||
let dy = self.decode_mvd(c, l, 1, x, y);
|
||||
self.set_cur_mvd(l, x, y, w, h, [dx, dy]);
|
||||
self.cur.mvd[l][p * 4 + j] = [dx as i16, dy as i16];
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let np = info.part.num_parts();
|
||||
for l in 0..2 {
|
||||
for p in 0..np {
|
||||
if info.pred[p] & (1 << l) == 0 {
|
||||
continue;
|
||||
}
|
||||
let (x, y, w, h) = info.part.rect(p);
|
||||
let r = if n_ref[l] > 1 { self.decode_ref_idx(c, l, x, y) as i8 } else { 0 };
|
||||
self.cur.ref_idx[l][p] = r;
|
||||
self.set_cur_ref(l, x, y, w, h, r);
|
||||
}
|
||||
}
|
||||
for l in 0..2 {
|
||||
for p in 0..np {
|
||||
if info.pred[p] & (1 << l) == 0 {
|
||||
continue;
|
||||
}
|
||||
let (x, y, w, h) = info.part.rect(p);
|
||||
let dx = self.decode_mvd(c, l, 0, x, y);
|
||||
let dy = self.decode_mvd(c, l, 1, x, y);
|
||||
self.set_cur_mvd(l, x, y, w, h, [dx, dy]);
|
||||
self.cur.mvd[l][p * 4] = [dx as i16, dy as i16];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let cbp = if info.kind == MbKind::I16x16 { info.i16_cbp } else { self.decode_cbp(c) };
|
||||
self.cur.cbp = cbp;
|
||||
self.mb_mut().cbp = cbp;
|
||||
if !intra && cbp & 15 != 0 && self.pps.transform_8x8_mode && self.no_sub_8x8_lt() {
|
||||
let t = self.decode_t8x8_flag(c);
|
||||
self.mb_mut().transform_8x8 = t;
|
||||
}
|
||||
if !intra {
|
||||
// ref_idx written during parsing is replaced by the derived motion
|
||||
self.derive_inter_motion()?;
|
||||
}
|
||||
if cbp != 0 || info.kind == MbKind::I16x16 {
|
||||
let d = self.decode_qp_delta(c)?;
|
||||
self.apply_qp_delta(d)?;
|
||||
self.prev_qp_delta_nz = d != 0;
|
||||
} else {
|
||||
self.prev_qp_delta_nz = false;
|
||||
}
|
||||
self.store_qp();
|
||||
self.residual_cabac(c, cbp)?;
|
||||
if intra {
|
||||
self.reconstruct_intra();
|
||||
} else {
|
||||
self.predict_inter();
|
||||
self.reconstruct_inter_residual();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode_t8x8_flag(&mut self, c: &mut Cabac) -> bool {
|
||||
let cond = |n: Option<usize>| n.map(|m| self.pic.mbs[m].transform_8x8).unwrap_or(false) as usize;
|
||||
let inc = cond(self.nb[0]) + cond(self.nb[1]);
|
||||
c.decode_decision(399 + inc) == 1
|
||||
}
|
||||
|
||||
fn decode_chroma_mode(&mut self, c: &mut Cabac) -> u8 {
|
||||
let cond = |n: Option<usize>| {
|
||||
n.map(|m| {
|
||||
let st = &self.pic.mbs[m];
|
||||
st.kind.is_intra() && st.kind != MbKind::IPcm && st.intra_chroma_mode != 0
|
||||
})
|
||||
.unwrap_or(false) as usize
|
||||
};
|
||||
let inc = cond(self.nb[0]) + cond(self.nb[1]);
|
||||
if c.decode_decision(64 + inc) == 0 {
|
||||
return 0;
|
||||
}
|
||||
if c.decode_decision(67) == 0 {
|
||||
return 1;
|
||||
}
|
||||
if c.decode_decision(67) == 0 { 2 } else { 3 }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,525 @@
|
||||
// Generated from ITU-T H.264 (06/2026) Tables 9-12..9-33 (m, n) by a table-extraction script.
|
||||
// Index: [0..=2] = cabac_init_idc for P/SP/B slices, [3] = I/SI slices. (0, 0) marks "na".
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static CABAC_INIT_MN: [[(i8, i8); 1024]; 4] = [
|
||||
[
|
||||
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
|
||||
(-6, 53), (-1, 54), (7, 51), (23, 33), (23, 2), (21, 0), (1, 9), (0, 49),
|
||||
(-37, 118), (5, 57), (-13, 78), (-11, 65), (1, 62), (12, 49), (-4, 73), (17, 50),
|
||||
(18, 64), (9, 43), (29, 0), (26, 67), (16, 90), (9, 104), (-46, 127), (-20, 104),
|
||||
(1, 67), (-13, 78), (-11, 65), (1, 62), (-6, 86), (-17, 95), (-6, 61), (9, 45),
|
||||
(-3, 69), (-6, 81), (-11, 96), (6, 55), (7, 67), (-5, 86), (2, 88), (0, 58),
|
||||
(-3, 76), (-10, 94), (5, 54), (4, 69), (-3, 81), (0, 88), (-7, 67), (-5, 74),
|
||||
(-4, 74), (-5, 80), (-7, 72), (1, 58), (0, 41), (0, 63), (0, 63), (0, 63),
|
||||
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (0, 45), (-4, 78),
|
||||
(-3, 96), (-27, 126), (-28, 98), (-25, 101), (-23, 67), (-28, 82), (-20, 94), (-16, 83),
|
||||
(-22, 110), (-21, 91), (-18, 102), (-13, 93), (-29, 127), (-7, 92), (-5, 89), (-7, 96),
|
||||
(-13, 108), (-3, 46), (-1, 65), (-1, 57), (-9, 93), (-3, 74), (-9, 92), (-8, 87),
|
||||
(-23, 126), (5, 54), (6, 60), (6, 59), (6, 69), (-1, 48), (0, 68), (-4, 69),
|
||||
(-8, 88), (-2, 85), (-6, 78), (-1, 75), (-7, 77), (2, 54), (5, 50), (-3, 68),
|
||||
(1, 50), (6, 42), (-4, 81), (1, 63), (-4, 70), (0, 67), (2, 57), (-2, 76),
|
||||
(11, 35), (4, 64), (1, 61), (11, 35), (18, 25), (12, 24), (13, 29), (13, 36),
|
||||
(-10, 93), (-7, 73), (-2, 73), (13, 46), (9, 49), (-7, 100), (9, 53), (2, 53),
|
||||
(5, 53), (-2, 61), (0, 56), (0, 56), (-13, 63), (-5, 60), (-1, 62), (4, 57),
|
||||
(-6, 69), (4, 57), (14, 39), (4, 51), (13, 68), (3, 64), (1, 61), (9, 63),
|
||||
(7, 50), (16, 39), (5, 44), (4, 52), (11, 48), (-5, 60), (-1, 59), (0, 59),
|
||||
(22, 33), (5, 44), (14, 43), (-1, 78), (0, 60), (9, 69), (11, 28), (2, 40),
|
||||
(3, 44), (0, 49), (0, 46), (2, 44), (2, 51), (0, 47), (4, 39), (2, 62),
|
||||
(6, 46), (0, 54), (3, 54), (2, 58), (4, 63), (6, 51), (6, 57), (7, 53),
|
||||
(6, 52), (6, 55), (11, 45), (14, 36), (8, 53), (-1, 82), (7, 55), (-3, 78),
|
||||
(15, 46), (22, 31), (-1, 84), (25, 7), (30, -7), (28, 3), (28, 4), (32, 0),
|
||||
(34, -1), (30, 6), (30, 6), (32, 9), (31, 19), (26, 27), (26, 30), (37, 20),
|
||||
(28, 34), (17, 70), (1, 67), (5, 59), (9, 67), (16, 30), (18, 32), (18, 35),
|
||||
(22, 29), (24, 31), (23, 38), (18, 43), (20, 41), (11, 63), (9, 59), (9, 64),
|
||||
(-1, 94), (-2, 89), (-9, 108), (-6, 76), (-2, 44), (0, 45), (0, 52), (-3, 64),
|
||||
(-2, 59), (-4, 70), (-4, 75), (-8, 82), (-17, 102), (-9, 77), (3, 24), (0, 42),
|
||||
(0, 48), (0, 55), (-6, 59), (-7, 71), (-12, 83), (-11, 87), (-30, 119), (1, 58),
|
||||
(-3, 29), (-1, 36), (1, 38), (2, 43), (-6, 55), (0, 58), (0, 64), (-3, 74),
|
||||
(-10, 90), (0, 70), (-4, 29), (5, 31), (7, 42), (1, 59), (-2, 58), (-3, 72),
|
||||
(-3, 81), (-11, 97), (0, 58), (8, 5), (10, 14), (14, 18), (13, 27), (2, 40),
|
||||
(0, 58), (-3, 70), (-6, 79), (-8, 85), (0, 0), (-13, 106), (-16, 106), (-10, 87),
|
||||
(-21, 114), (-18, 110), (-14, 98), (-22, 110), (-21, 106), (-18, 103), (-21, 107), (-23, 108),
|
||||
(-26, 112), (-10, 96), (-12, 95), (-5, 91), (-9, 93), (-22, 94), (-5, 86), (9, 67),
|
||||
(-4, 80), (-10, 85), (-1, 70), (7, 60), (9, 58), (5, 61), (12, 50), (15, 50),
|
||||
(18, 49), (17, 54), (10, 41), (7, 46), (-1, 51), (7, 49), (8, 52), (9, 41),
|
||||
(6, 47), (2, 55), (13, 41), (10, 44), (6, 50), (5, 53), (13, 49), (4, 63),
|
||||
(6, 64), (-2, 69), (-2, 59), (6, 70), (10, 44), (9, 31), (12, 43), (3, 53),
|
||||
(14, 34), (10, 38), (-3, 52), (13, 40), (17, 32), (7, 44), (7, 38), (13, 50),
|
||||
(10, 57), (26, 43), (14, 11), (11, 14), (9, 11), (18, 11), (21, 9), (23, -2),
|
||||
(32, -15), (32, -15), (34, -21), (39, -23), (42, -33), (41, -31), (46, -28), (38, -12),
|
||||
(21, 29), (45, -24), (53, -45), (48, -26), (65, -43), (43, -19), (39, -10), (30, 9),
|
||||
(18, 26), (20, 27), (0, 57), (-14, 82), (-5, 75), (-19, 97), (-35, 125), (27, 0),
|
||||
(28, 0), (31, -4), (27, 6), (34, 8), (30, 10), (24, 22), (33, 19), (22, 32),
|
||||
(26, 31), (21, 41), (26, 44), (23, 47), (16, 65), (14, 71), (8, 60), (6, 63),
|
||||
(17, 65), (21, 24), (23, 20), (26, 23), (27, 32), (28, 23), (28, 24), (23, 40),
|
||||
(24, 32), (28, 29), (23, 42), (19, 57), (22, 53), (22, 61), (11, 86), (12, 40),
|
||||
(11, 51), (14, 59), (-4, 79), (-7, 71), (-5, 69), (-9, 70), (-8, 66), (-10, 68),
|
||||
(-19, 73), (-12, 69), (-16, 70), (-15, 67), (-20, 62), (-19, 70), (-16, 66), (-22, 65),
|
||||
(-20, 63), (9, -2), (26, -9), (33, -9), (39, -7), (41, -2), (45, 3), (49, 9),
|
||||
(45, 27), (36, 59), (-6, 66), (-7, 35), (-7, 42), (-8, 45), (-5, 48), (-12, 56),
|
||||
(-6, 60), (-5, 62), (-8, 66), (-8, 76), (-5, 85), (-6, 81), (-10, 77), (-7, 81),
|
||||
(-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66),
|
||||
(-14, 66), (0, 59), (2, 59), (21, -13), (33, -14), (39, -7), (46, -2), (51, 2),
|
||||
(60, 6), (61, 17), (55, 34), (42, 62), (-7, 92), (-5, 89), (-7, 96), (-13, 108),
|
||||
(-3, 46), (-1, 65), (-1, 57), (-9, 93), (-3, 74), (-9, 92), (-8, 87), (-23, 126),
|
||||
(-7, 92), (-5, 89), (-7, 96), (-13, 108), (-3, 46), (-1, 65), (-1, 57), (-9, 93),
|
||||
(-3, 74), (-9, 92), (-8, 87), (-23, 126), (-2, 85), (-6, 78), (-1, 75), (-7, 77),
|
||||
(2, 54), (5, 50), (-3, 68), (1, 50), (6, 42), (-4, 81), (1, 63), (-4, 70),
|
||||
(0, 67), (2, 57), (-2, 76), (11, 35), (4, 64), (1, 61), (11, 35), (18, 25),
|
||||
(12, 24), (13, 29), (13, 36), (-10, 93), (-7, 73), (-2, 73), (13, 46), (9, 49),
|
||||
(-7, 100), (9, 53), (2, 53), (5, 53), (-2, 61), (0, 56), (0, 56), (-13, 63),
|
||||
(-5, 60), (-1, 62), (4, 57), (-6, 69), (4, 57), (14, 39), (4, 51), (13, 68),
|
||||
(-2, 85), (-6, 78), (-1, 75), (-7, 77), (2, 54), (5, 50), (-3, 68), (1, 50),
|
||||
(6, 42), (-4, 81), (1, 63), (-4, 70), (0, 67), (2, 57), (-2, 76), (11, 35),
|
||||
(4, 64), (1, 61), (11, 35), (18, 25), (12, 24), (13, 29), (13, 36), (-10, 93),
|
||||
(-7, 73), (-2, 73), (13, 46), (9, 49), (-7, 100), (9, 53), (2, 53), (5, 53),
|
||||
(-2, 61), (0, 56), (0, 56), (-13, 63), (-5, 60), (-1, 62), (4, 57), (-6, 69),
|
||||
(4, 57), (14, 39), (4, 51), (13, 68), (11, 28), (2, 40), (3, 44), (0, 49),
|
||||
(0, 46), (2, 44), (2, 51), (0, 47), (4, 39), (2, 62), (6, 46), (0, 54),
|
||||
(3, 54), (2, 58), (4, 63), (6, 51), (6, 57), (7, 53), (6, 52), (6, 55),
|
||||
(11, 45), (14, 36), (8, 53), (-1, 82), (7, 55), (-3, 78), (15, 46), (22, 31),
|
||||
(-1, 84), (25, 7), (30, -7), (28, 3), (28, 4), (32, 0), (34, -1), (30, 6),
|
||||
(30, 6), (32, 9), (31, 19), (26, 27), (26, 30), (37, 20), (28, 34), (17, 70),
|
||||
(11, 28), (2, 40), (3, 44), (0, 49), (0, 46), (2, 44), (2, 51), (0, 47),
|
||||
(4, 39), (2, 62), (6, 46), (0, 54), (3, 54), (2, 58), (4, 63), (6, 51),
|
||||
(6, 57), (7, 53), (6, 52), (6, 55), (11, 45), (14, 36), (8, 53), (-1, 82),
|
||||
(7, 55), (-3, 78), (15, 46), (22, 31), (-1, 84), (25, 7), (30, -7), (28, 3),
|
||||
(28, 4), (32, 0), (34, -1), (30, 6), (30, 6), (32, 9), (31, 19), (26, 27),
|
||||
(26, 30), (37, 20), (28, 34), (17, 70), (-4, 79), (-7, 71), (-5, 69), (-9, 70),
|
||||
(-8, 66), (-10, 68), (-19, 73), (-12, 69), (-16, 70), (-15, 67), (-20, 62), (-19, 70),
|
||||
(-16, 66), (-22, 65), (-20, 63), (-5, 85), (-6, 81), (-10, 77), (-7, 81), (-17, 80),
|
||||
(-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66), (-14, 66),
|
||||
(0, 59), (2, 59), (9, -2), (26, -9), (33, -9), (39, -7), (41, -2), (45, 3),
|
||||
(49, 9), (45, 27), (36, 59), (21, -13), (33, -14), (39, -7), (46, -2), (51, 2),
|
||||
(60, 6), (61, 17), (55, 34), (42, 62), (-6, 66), (-7, 35), (-7, 42), (-8, 45),
|
||||
(-5, 48), (-12, 56), (-6, 60), (-5, 62), (-8, 66), (-8, 76), (-4, 79), (-7, 71),
|
||||
(-5, 69), (-9, 70), (-8, 66), (-10, 68), (-19, 73), (-12, 69), (-16, 70), (-15, 67),
|
||||
(-20, 62), (-19, 70), (-16, 66), (-22, 65), (-20, 63), (-5, 85), (-6, 81), (-10, 77),
|
||||
(-7, 81), (-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61),
|
||||
(-8, 66), (-14, 66), (0, 59), (2, 59), (9, -2), (26, -9), (33, -9), (39, -7),
|
||||
(41, -2), (45, 3), (49, 9), (45, 27), (36, 59), (21, -13), (33, -14), (39, -7),
|
||||
(46, -2), (51, 2), (60, 6), (61, 17), (55, 34), (42, 62), (-6, 66), (-7, 35),
|
||||
(-7, 42), (-8, 45), (-5, 48), (-12, 56), (-6, 60), (-5, 62), (-8, 66), (-8, 76),
|
||||
(-13, 106), (-16, 106), (-10, 87), (-21, 114), (-18, 110), (-14, 98), (-22, 110), (-21, 106),
|
||||
(-18, 103), (-21, 107), (-23, 108), (-26, 112), (-10, 96), (-12, 95), (-5, 91), (-9, 93),
|
||||
(-22, 94), (-5, 86), (9, 67), (-4, 80), (-10, 85), (-1, 70), (7, 60), (9, 58),
|
||||
(5, 61), (12, 50), (15, 50), (18, 49), (17, 54), (10, 41), (7, 46), (-1, 51),
|
||||
(7, 49), (8, 52), (9, 41), (6, 47), (2, 55), (13, 41), (10, 44), (6, 50),
|
||||
(5, 53), (13, 49), (4, 63), (6, 64), (-13, 106), (-16, 106), (-10, 87), (-21, 114),
|
||||
(-18, 110), (-14, 98), (-22, 110), (-21, 106), (-18, 103), (-21, 107), (-23, 108), (-26, 112),
|
||||
(-10, 96), (-12, 95), (-5, 91), (-9, 93), (-22, 94), (-5, 86), (9, 67), (-4, 80),
|
||||
(-10, 85), (-1, 70), (7, 60), (9, 58), (5, 61), (12, 50), (15, 50), (18, 49),
|
||||
(17, 54), (10, 41), (7, 46), (-1, 51), (7, 49), (8, 52), (9, 41), (6, 47),
|
||||
(2, 55), (13, 41), (10, 44), (6, 50), (5, 53), (13, 49), (4, 63), (6, 64),
|
||||
(14, 11), (11, 14), (9, 11), (18, 11), (21, 9), (23, -2), (32, -15), (32, -15),
|
||||
(34, -21), (39, -23), (42, -33), (41, -31), (46, -28), (38, -12), (21, 29), (45, -24),
|
||||
(53, -45), (48, -26), (65, -43), (43, -19), (39, -10), (30, 9), (18, 26), (20, 27),
|
||||
(0, 57), (-14, 82), (-5, 75), (-19, 97), (-35, 125), (27, 0), (28, 0), (31, -4),
|
||||
(27, 6), (34, 8), (30, 10), (24, 22), (33, 19), (22, 32), (26, 31), (21, 41),
|
||||
(26, 44), (23, 47), (16, 65), (14, 71), (14, 11), (11, 14), (9, 11), (18, 11),
|
||||
(21, 9), (23, -2), (32, -15), (32, -15), (34, -21), (39, -23), (42, -33), (41, -31),
|
||||
(46, -28), (38, -12), (21, 29), (45, -24), (53, -45), (48, -26), (65, -43), (43, -19),
|
||||
(39, -10), (30, 9), (18, 26), (20, 27), (0, 57), (-14, 82), (-5, 75), (-19, 97),
|
||||
(-35, 125), (27, 0), (28, 0), (31, -4), (27, 6), (34, 8), (30, 10), (24, 22),
|
||||
(33, 19), (22, 32), (26, 31), (21, 41), (26, 44), (23, 47), (16, 65), (14, 71),
|
||||
(-6, 76), (-2, 44), (0, 45), (0, 52), (-3, 64), (-2, 59), (-4, 70), (-4, 75),
|
||||
(-8, 82), (-17, 102), (-9, 77), (3, 24), (0, 42), (0, 48), (0, 55), (-6, 59),
|
||||
(-7, 71), (-12, 83), (-11, 87), (-30, 119), (1, 58), (-3, 29), (-1, 36), (1, 38),
|
||||
(2, 43), (-6, 55), (0, 58), (0, 64), (-3, 74), (-10, 90), (-6, 76), (-2, 44),
|
||||
(0, 45), (0, 52), (-3, 64), (-2, 59), (-4, 70), (-4, 75), (-8, 82), (-17, 102),
|
||||
(-9, 77), (3, 24), (0, 42), (0, 48), (0, 55), (-6, 59), (-7, 71), (-12, 83),
|
||||
(-11, 87), (-30, 119), (1, 58), (-3, 29), (-1, 36), (1, 38), (2, 43), (-6, 55),
|
||||
(0, 58), (0, 64), (-3, 74), (-10, 90), (-3, 74), (-9, 92), (-8, 87), (-23, 126),
|
||||
(-3, 74), (-9, 92), (-8, 87), (-23, 126), (-3, 74), (-9, 92), (-8, 87), (-23, 126),
|
||||
],
|
||||
[
|
||||
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
|
||||
(-6, 53), (-1, 54), (7, 51), (22, 25), (34, 0), (16, 0), (-2, 9), (4, 41),
|
||||
(-29, 118), (2, 65), (-6, 71), (-13, 79), (5, 52), (9, 50), (-3, 70), (10, 54),
|
||||
(26, 34), (19, 22), (40, 0), (57, 2), (41, 36), (26, 69), (-45, 127), (-15, 101),
|
||||
(-4, 76), (-6, 71), (-13, 79), (5, 52), (6, 69), (-13, 90), (0, 52), (8, 43),
|
||||
(-2, 69), (-5, 82), (-10, 96), (2, 59), (2, 75), (-3, 87), (-3, 100), (1, 56),
|
||||
(-3, 74), (-6, 85), (0, 59), (-3, 81), (-7, 86), (-5, 95), (-1, 66), (-1, 77),
|
||||
(1, 70), (-2, 86), (-5, 72), (0, 61), (0, 41), (0, 63), (0, 63), (0, 63),
|
||||
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (13, 15), (7, 51),
|
||||
(2, 80), (-39, 127), (-18, 91), (-17, 96), (-26, 81), (-35, 98), (-24, 102), (-23, 97),
|
||||
(-27, 119), (-24, 99), (-21, 110), (-18, 102), (-36, 127), (0, 80), (-5, 89), (-7, 94),
|
||||
(-4, 92), (0, 39), (0, 65), (-15, 84), (-35, 127), (-2, 73), (-12, 104), (-9, 91),
|
||||
(-31, 127), (3, 55), (7, 56), (7, 55), (8, 61), (-3, 53), (0, 68), (-7, 74),
|
||||
(-9, 88), (-13, 103), (-13, 91), (-9, 89), (-14, 92), (-8, 76), (-12, 87), (-23, 110),
|
||||
(-24, 105), (-10, 78), (-20, 112), (-17, 99), (-78, 127), (-70, 127), (-50, 127), (-46, 127),
|
||||
(-4, 66), (-5, 78), (-4, 71), (-8, 72), (2, 59), (-1, 55), (-7, 70), (-6, 75),
|
||||
(-8, 89), (-34, 119), (-3, 75), (32, 20), (30, 22), (-44, 127), (0, 54), (-5, 61),
|
||||
(0, 58), (-1, 60), (-3, 61), (-8, 67), (-25, 84), (-14, 74), (-5, 65), (5, 52),
|
||||
(2, 57), (0, 61), (-9, 69), (-11, 70), (18, 55), (-4, 71), (0, 58), (7, 61),
|
||||
(9, 41), (18, 25), (9, 32), (5, 43), (9, 47), (0, 44), (0, 51), (2, 46),
|
||||
(19, 38), (-4, 66), (15, 38), (12, 42), (9, 34), (0, 89), (4, 45), (10, 28),
|
||||
(10, 31), (33, -11), (52, -43), (18, 15), (28, 0), (35, -22), (38, -25), (34, 0),
|
||||
(39, -18), (32, -12), (102, -94), (0, 0), (56, -15), (33, -4), (29, 10), (37, -5),
|
||||
(51, -29), (39, -9), (52, -34), (69, -58), (67, -63), (44, -5), (32, 7), (55, -29),
|
||||
(32, 1), (0, 0), (27, 36), (33, -25), (34, -30), (36, -28), (38, -28), (38, -27),
|
||||
(34, -18), (35, -16), (34, -14), (32, -8), (37, -6), (35, 0), (30, 10), (28, 18),
|
||||
(26, 25), (29, 41), (0, 75), (2, 72), (8, 77), (14, 35), (18, 31), (17, 35),
|
||||
(21, 30), (17, 45), (20, 42), (18, 45), (27, 26), (16, 54), (7, 66), (16, 56),
|
||||
(11, 73), (10, 67), (-10, 116), (-23, 112), (-15, 71), (-7, 61), (0, 53), (-5, 66),
|
||||
(-11, 77), (-9, 80), (-9, 84), (-10, 87), (-34, 127), (-21, 101), (-3, 39), (-5, 53),
|
||||
(-7, 61), (-11, 75), (-15, 77), (-17, 91), (-25, 107), (-25, 111), (-28, 122), (-11, 76),
|
||||
(-10, 44), (-10, 52), (-10, 57), (-9, 58), (-16, 72), (-7, 69), (-4, 69), (-5, 74),
|
||||
(-9, 86), (2, 66), (-9, 34), (1, 32), (11, 31), (5, 52), (-2, 55), (-2, 67),
|
||||
(0, 73), (-8, 89), (3, 52), (7, 4), (10, 8), (17, 8), (16, 19), (3, 37),
|
||||
(-1, 61), (-5, 73), (-1, 70), (-4, 78), (0, 0), (-21, 126), (-23, 124), (-20, 110),
|
||||
(-26, 126), (-25, 124), (-17, 105), (-27, 121), (-27, 117), (-17, 102), (-26, 117), (-27, 116),
|
||||
(-33, 122), (-10, 95), (-14, 100), (-8, 95), (-17, 111), (-28, 114), (-6, 89), (-2, 80),
|
||||
(-4, 82), (-9, 85), (-8, 81), (-1, 72), (5, 64), (1, 67), (9, 56), (0, 69),
|
||||
(1, 69), (7, 69), (-7, 69), (-6, 67), (-16, 77), (-2, 64), (2, 61), (-6, 67),
|
||||
(-3, 64), (2, 57), (-3, 65), (-3, 66), (0, 62), (9, 51), (-1, 66), (-2, 71),
|
||||
(-2, 75), (-1, 70), (-9, 72), (14, 60), (16, 37), (0, 47), (18, 35), (11, 37),
|
||||
(12, 41), (10, 41), (2, 48), (12, 41), (13, 41), (0, 59), (3, 50), (19, 40),
|
||||
(3, 66), (18, 50), (19, -6), (18, -6), (14, 0), (26, -12), (31, -16), (33, -25),
|
||||
(33, -22), (37, -28), (39, -30), (42, -30), (47, -42), (45, -36), (49, -34), (41, -17),
|
||||
(32, 9), (69, -71), (63, -63), (66, -64), (77, -74), (54, -39), (52, -35), (41, -10),
|
||||
(36, 0), (40, -1), (30, 14), (28, 26), (23, 37), (12, 55), (11, 65), (37, -33),
|
||||
(39, -36), (40, -37), (38, -30), (46, -33), (42, -30), (40, -24), (49, -29), (38, -12),
|
||||
(40, -10), (38, -3), (46, -5), (31, 20), (29, 30), (25, 44), (12, 48), (11, 49),
|
||||
(26, 45), (22, 22), (23, 22), (27, 21), (33, 20), (26, 28), (30, 24), (27, 34),
|
||||
(18, 42), (25, 39), (18, 50), (12, 70), (21, 54), (14, 71), (11, 83), (25, 32),
|
||||
(21, 49), (21, 54), (-5, 85), (-6, 81), (-10, 77), (-7, 81), (-17, 80), (-18, 73),
|
||||
(-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66), (-14, 66), (0, 59),
|
||||
(2, 59), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0), (64, 3), (68, 10),
|
||||
(66, 27), (47, 57), (-5, 71), (0, 24), (-1, 36), (-2, 42), (-2, 52), (-9, 57),
|
||||
(-6, 63), (-4, 65), (-4, 67), (-7, 82), (-3, 81), (-3, 76), (-7, 72), (-6, 78),
|
||||
(-12, 72), (-14, 68), (-3, 70), (-6, 76), (-5, 66), (-5, 62), (0, 57), (-4, 61),
|
||||
(-9, 60), (1, 54), (2, 58), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0),
|
||||
(64, 3), (68, 10), (66, 27), (47, 57), (0, 80), (-5, 89), (-7, 94), (-4, 92),
|
||||
(0, 39), (0, 65), (-15, 84), (-35, 127), (-2, 73), (-12, 104), (-9, 91), (-31, 127),
|
||||
(0, 80), (-5, 89), (-7, 94), (-4, 92), (0, 39), (0, 65), (-15, 84), (-35, 127),
|
||||
(-2, 73), (-12, 104), (-9, 91), (-31, 127), (-13, 103), (-13, 91), (-9, 89), (-14, 92),
|
||||
(-8, 76), (-12, 87), (-23, 110), (-24, 105), (-10, 78), (-20, 112), (-17, 99), (-78, 127),
|
||||
(-70, 127), (-50, 127), (-46, 127), (-4, 66), (-5, 78), (-4, 71), (-8, 72), (2, 59),
|
||||
(-1, 55), (-7, 70), (-6, 75), (-8, 89), (-34, 119), (-3, 75), (32, 20), (30, 22),
|
||||
(-44, 127), (0, 54), (-5, 61), (0, 58), (-1, 60), (-3, 61), (-8, 67), (-25, 84),
|
||||
(-14, 74), (-5, 65), (5, 52), (2, 57), (0, 61), (-9, 69), (-11, 70), (18, 55),
|
||||
(-13, 103), (-13, 91), (-9, 89), (-14, 92), (-8, 76), (-12, 87), (-23, 110), (-24, 105),
|
||||
(-10, 78), (-20, 112), (-17, 99), (-78, 127), (-70, 127), (-50, 127), (-46, 127), (-4, 66),
|
||||
(-5, 78), (-4, 71), (-8, 72), (2, 59), (-1, 55), (-7, 70), (-6, 75), (-8, 89),
|
||||
(-34, 119), (-3, 75), (32, 20), (30, 22), (-44, 127), (0, 54), (-5, 61), (0, 58),
|
||||
(-1, 60), (-3, 61), (-8, 67), (-25, 84), (-14, 74), (-5, 65), (5, 52), (2, 57),
|
||||
(0, 61), (-9, 69), (-11, 70), (18, 55), (4, 45), (10, 28), (10, 31), (33, -11),
|
||||
(52, -43), (18, 15), (28, 0), (35, -22), (38, -25), (34, 0), (39, -18), (32, -12),
|
||||
(102, -94), (0, 0), (56, -15), (33, -4), (29, 10), (37, -5), (51, -29), (39, -9),
|
||||
(52, -34), (69, -58), (67, -63), (44, -5), (32, 7), (55, -29), (32, 1), (0, 0),
|
||||
(27, 36), (33, -25), (34, -30), (36, -28), (38, -28), (38, -27), (34, -18), (35, -16),
|
||||
(34, -14), (32, -8), (37, -6), (35, 0), (30, 10), (28, 18), (26, 25), (29, 41),
|
||||
(4, 45), (10, 28), (10, 31), (33, -11), (52, -43), (18, 15), (28, 0), (35, -22),
|
||||
(38, -25), (34, 0), (39, -18), (32, -12), (102, -94), (0, 0), (56, -15), (33, -4),
|
||||
(29, 10), (37, -5), (51, -29), (39, -9), (52, -34), (69, -58), (67, -63), (44, -5),
|
||||
(32, 7), (55, -29), (32, 1), (0, 0), (27, 36), (33, -25), (34, -30), (36, -28),
|
||||
(38, -28), (38, -27), (34, -18), (35, -16), (34, -14), (32, -8), (37, -6), (35, 0),
|
||||
(30, 10), (28, 18), (26, 25), (29, 41), (-5, 85), (-6, 81), (-10, 77), (-7, 81),
|
||||
(-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66),
|
||||
(-14, 66), (0, 59), (2, 59), (-3, 81), (-3, 76), (-7, 72), (-6, 78), (-12, 72),
|
||||
(-14, 68), (-3, 70), (-6, 76), (-5, 66), (-5, 62), (0, 57), (-4, 61), (-9, 60),
|
||||
(1, 54), (2, 58), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0), (64, 3),
|
||||
(68, 10), (66, 27), (47, 57), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0),
|
||||
(64, 3), (68, 10), (66, 27), (47, 57), (-5, 71), (0, 24), (-1, 36), (-2, 42),
|
||||
(-2, 52), (-9, 57), (-6, 63), (-4, 65), (-4, 67), (-7, 82), (-5, 85), (-6, 81),
|
||||
(-10, 77), (-7, 81), (-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67),
|
||||
(-1, 61), (-8, 66), (-14, 66), (0, 59), (2, 59), (-3, 81), (-3, 76), (-7, 72),
|
||||
(-6, 78), (-12, 72), (-14, 68), (-3, 70), (-6, 76), (-5, 66), (-5, 62), (0, 57),
|
||||
(-4, 61), (-9, 60), (1, 54), (2, 58), (17, -10), (32, -13), (42, -9), (49, -5),
|
||||
(53, 0), (64, 3), (68, 10), (66, 27), (47, 57), (17, -10), (32, -13), (42, -9),
|
||||
(49, -5), (53, 0), (64, 3), (68, 10), (66, 27), (47, 57), (-5, 71), (0, 24),
|
||||
(-1, 36), (-2, 42), (-2, 52), (-9, 57), (-6, 63), (-4, 65), (-4, 67), (-7, 82),
|
||||
(-21, 126), (-23, 124), (-20, 110), (-26, 126), (-25, 124), (-17, 105), (-27, 121), (-27, 117),
|
||||
(-17, 102), (-26, 117), (-27, 116), (-33, 122), (-10, 95), (-14, 100), (-8, 95), (-17, 111),
|
||||
(-28, 114), (-6, 89), (-2, 80), (-4, 82), (-9, 85), (-8, 81), (-1, 72), (5, 64),
|
||||
(1, 67), (9, 56), (0, 69), (1, 69), (7, 69), (-7, 69), (-6, 67), (-16, 77),
|
||||
(-2, 64), (2, 61), (-6, 67), (-3, 64), (2, 57), (-3, 65), (-3, 66), (0, 62),
|
||||
(9, 51), (-1, 66), (-2, 71), (-2, 75), (-21, 126), (-23, 124), (-20, 110), (-26, 126),
|
||||
(-25, 124), (-17, 105), (-27, 121), (-27, 117), (-17, 102), (-26, 117), (-27, 116), (-33, 122),
|
||||
(-10, 95), (-14, 100), (-8, 95), (-17, 111), (-28, 114), (-6, 89), (-2, 80), (-4, 82),
|
||||
(-9, 85), (-8, 81), (-1, 72), (5, 64), (1, 67), (9, 56), (0, 69), (1, 69),
|
||||
(7, 69), (-7, 69), (-6, 67), (-16, 77), (-2, 64), (2, 61), (-6, 67), (-3, 64),
|
||||
(2, 57), (-3, 65), (-3, 66), (0, 62), (9, 51), (-1, 66), (-2, 71), (-2, 75),
|
||||
(19, -6), (18, -6), (14, 0), (26, -12), (31, -16), (33, -25), (33, -22), (37, -28),
|
||||
(39, -30), (42, -30), (47, -42), (45, -36), (49, -34), (41, -17), (32, 9), (69, -71),
|
||||
(63, -63), (66, -64), (77, -74), (54, -39), (52, -35), (41, -10), (36, 0), (40, -1),
|
||||
(30, 14), (28, 26), (23, 37), (12, 55), (11, 65), (37, -33), (39, -36), (40, -37),
|
||||
(38, -30), (46, -33), (42, -30), (40, -24), (49, -29), (38, -12), (40, -10), (38, -3),
|
||||
(46, -5), (31, 20), (29, 30), (25, 44), (19, -6), (18, -6), (14, 0), (26, -12),
|
||||
(31, -16), (33, -25), (33, -22), (37, -28), (39, -30), (42, -30), (47, -42), (45, -36),
|
||||
(49, -34), (41, -17), (32, 9), (69, -71), (63, -63), (66, -64), (77, -74), (54, -39),
|
||||
(52, -35), (41, -10), (36, 0), (40, -1), (30, 14), (28, 26), (23, 37), (12, 55),
|
||||
(11, 65), (37, -33), (39, -36), (40, -37), (38, -30), (46, -33), (42, -30), (40, -24),
|
||||
(49, -29), (38, -12), (40, -10), (38, -3), (46, -5), (31, 20), (29, 30), (25, 44),
|
||||
(-23, 112), (-15, 71), (-7, 61), (0, 53), (-5, 66), (-11, 77), (-9, 80), (-9, 84),
|
||||
(-10, 87), (-34, 127), (-21, 101), (-3, 39), (-5, 53), (-7, 61), (-11, 75), (-15, 77),
|
||||
(-17, 91), (-25, 107), (-25, 111), (-28, 122), (-11, 76), (-10, 44), (-10, 52), (-10, 57),
|
||||
(-9, 58), (-16, 72), (-7, 69), (-4, 69), (-5, 74), (-9, 86), (-23, 112), (-15, 71),
|
||||
(-7, 61), (0, 53), (-5, 66), (-11, 77), (-9, 80), (-9, 84), (-10, 87), (-34, 127),
|
||||
(-21, 101), (-3, 39), (-5, 53), (-7, 61), (-11, 75), (-15, 77), (-17, 91), (-25, 107),
|
||||
(-25, 111), (-28, 122), (-11, 76), (-10, 44), (-10, 52), (-10, 57), (-9, 58), (-16, 72),
|
||||
(-7, 69), (-4, 69), (-5, 74), (-9, 86), (-2, 73), (-12, 104), (-9, 91), (-31, 127),
|
||||
(-2, 73), (-12, 104), (-9, 91), (-31, 127), (-2, 73), (-12, 104), (-9, 91), (-31, 127),
|
||||
],
|
||||
[
|
||||
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
|
||||
(-6, 53), (-1, 54), (7, 51), (29, 16), (25, 0), (14, 0), (-10, 51), (-3, 62),
|
||||
(-27, 99), (26, 16), (-4, 85), (-24, 102), (5, 57), (6, 57), (-17, 73), (14, 57),
|
||||
(20, 40), (20, 10), (29, 0), (54, 0), (37, 42), (12, 97), (-32, 127), (-22, 117),
|
||||
(-2, 74), (-4, 85), (-24, 102), (5, 57), (-6, 93), (-14, 88), (-6, 44), (4, 55),
|
||||
(-11, 89), (-15, 103), (-21, 116), (19, 57), (20, 58), (4, 84), (6, 96), (1, 63),
|
||||
(-5, 85), (-13, 106), (5, 63), (6, 75), (-3, 90), (-1, 101), (3, 55), (-4, 79),
|
||||
(-2, 75), (-12, 97), (-7, 50), (1, 60), (0, 41), (0, 63), (0, 63), (0, 63),
|
||||
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (7, 34), (-9, 88),
|
||||
(-20, 127), (-36, 127), (-17, 91), (-14, 95), (-25, 84), (-25, 86), (-12, 89), (-17, 91),
|
||||
(-31, 127), (-14, 76), (-18, 103), (-13, 90), (-37, 127), (11, 80), (5, 76), (2, 84),
|
||||
(5, 78), (-6, 55), (4, 61), (-14, 83), (-37, 127), (-5, 79), (-11, 104), (-11, 91),
|
||||
(-30, 127), (0, 65), (-2, 79), (0, 72), (-4, 92), (-6, 56), (3, 68), (-8, 71),
|
||||
(-13, 98), (-4, 86), (-12, 88), (-5, 82), (-3, 72), (-4, 67), (-8, 72), (-16, 89),
|
||||
(-9, 69), (-1, 59), (5, 66), (4, 57), (-4, 71), (-2, 71), (2, 58), (-1, 74),
|
||||
(-4, 44), (-1, 69), (0, 62), (-7, 51), (-4, 47), (-6, 42), (-3, 41), (-6, 53),
|
||||
(8, 76), (-9, 78), (-11, 83), (9, 52), (0, 67), (-5, 90), (1, 67), (-15, 72),
|
||||
(-5, 75), (-8, 80), (-21, 83), (-21, 64), (-13, 31), (-25, 64), (-29, 94), (9, 75),
|
||||
(17, 63), (-8, 74), (-5, 35), (-2, 27), (13, 91), (3, 65), (-7, 69), (8, 77),
|
||||
(-10, 66), (3, 62), (-3, 68), (-20, 81), (0, 30), (1, 7), (-3, 23), (-21, 74),
|
||||
(16, 66), (-23, 124), (17, 37), (44, -18), (50, -34), (-22, 127), (4, 39), (0, 42),
|
||||
(7, 34), (11, 29), (8, 31), (6, 37), (7, 42), (3, 40), (8, 33), (13, 43),
|
||||
(13, 36), (4, 47), (3, 55), (2, 58), (6, 60), (8, 44), (11, 44), (14, 42),
|
||||
(7, 48), (4, 56), (4, 52), (13, 37), (9, 49), (19, 58), (10, 48), (12, 45),
|
||||
(0, 69), (20, 33), (8, 63), (35, -18), (33, -25), (28, -3), (24, 10), (27, 0),
|
||||
(34, -14), (52, -44), (39, -24), (19, 17), (31, 25), (36, 29), (24, 33), (34, 15),
|
||||
(30, 20), (22, 73), (20, 34), (19, 31), (27, 44), (19, 16), (15, 36), (15, 36),
|
||||
(21, 28), (25, 21), (30, 20), (31, 12), (27, 16), (24, 42), (0, 93), (14, 56),
|
||||
(15, 57), (26, 38), (-24, 127), (-24, 115), (-22, 82), (-9, 62), (0, 53), (0, 59),
|
||||
(-14, 85), (-13, 89), (-13, 94), (-11, 92), (-29, 127), (-21, 100), (-14, 57), (-12, 67),
|
||||
(-11, 71), (-10, 77), (-21, 85), (-16, 88), (-23, 104), (-15, 98), (-37, 127), (-10, 82),
|
||||
(-8, 48), (-8, 61), (-8, 66), (-7, 70), (-14, 75), (-10, 79), (-9, 83), (-12, 92),
|
||||
(-18, 108), (-4, 79), (-22, 69), (-16, 75), (-2, 58), (1, 58), (-13, 78), (-9, 83),
|
||||
(-4, 81), (-13, 99), (-13, 81), (-6, 38), (-13, 62), (-6, 58), (-2, 59), (-16, 73),
|
||||
(-10, 76), (-13, 86), (-9, 83), (-10, 87), (0, 0), (-22, 127), (-25, 127), (-25, 120),
|
||||
(-27, 127), (-19, 114), (-23, 117), (-25, 118), (-26, 117), (-24, 113), (-28, 118), (-31, 120),
|
||||
(-37, 124), (-10, 94), (-15, 102), (-10, 99), (-13, 106), (-50, 127), (-5, 92), (17, 57),
|
||||
(-5, 86), (-13, 94), (-12, 91), (-2, 77), (0, 71), (-1, 73), (4, 64), (-7, 81),
|
||||
(5, 64), (15, 57), (1, 67), (0, 68), (-10, 67), (1, 68), (0, 77), (2, 64),
|
||||
(0, 68), (-5, 78), (7, 55), (5, 59), (2, 65), (14, 54), (15, 44), (5, 60),
|
||||
(2, 70), (-2, 76), (-18, 86), (12, 70), (5, 64), (-12, 70), (11, 55), (5, 56),
|
||||
(0, 69), (2, 65), (-6, 74), (5, 54), (7, 54), (-6, 76), (-11, 82), (-2, 77),
|
||||
(-2, 77), (25, 42), (17, -13), (16, -9), (17, -12), (27, -21), (37, -30), (41, -40),
|
||||
(42, -41), (48, -47), (39, -32), (46, -40), (52, -51), (46, -41), (52, -39), (43, -19),
|
||||
(32, 11), (61, -55), (56, -46), (62, -50), (81, -67), (45, -20), (35, -2), (28, 15),
|
||||
(34, 1), (39, 1), (30, 17), (20, 38), (18, 45), (15, 54), (0, 79), (36, -16),
|
||||
(37, -14), (37, -17), (32, 1), (34, 15), (29, 15), (24, 25), (34, 22), (31, 16),
|
||||
(35, 18), (31, 28), (33, 41), (36, 28), (27, 47), (21, 62), (18, 31), (19, 26),
|
||||
(36, 24), (24, 23), (27, 16), (24, 30), (31, 29), (22, 41), (22, 42), (16, 60),
|
||||
(15, 52), (14, 60), (3, 78), (-16, 123), (21, 53), (22, 56), (25, 61), (21, 33),
|
||||
(19, 50), (17, 61), (-3, 78), (-8, 74), (-9, 72), (-10, 72), (-18, 75), (-12, 71),
|
||||
(-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53), (-14, 59), (-9, 52),
|
||||
(-11, 68), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7), (31, 12), (37, 23),
|
||||
(31, 38), (20, 64), (-9, 71), (-7, 37), (-8, 44), (-11, 49), (-10, 56), (-12, 59),
|
||||
(-8, 63), (-9, 67), (-6, 68), (-10, 79), (-3, 78), (-8, 74), (-9, 72), (-10, 72),
|
||||
(-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53),
|
||||
(-14, 59), (-9, 52), (-11, 68), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7),
|
||||
(31, 12), (37, 23), (31, 38), (20, 64), (11, 80), (5, 76), (2, 84), (5, 78),
|
||||
(-6, 55), (4, 61), (-14, 83), (-37, 127), (-5, 79), (-11, 104), (-11, 91), (-30, 127),
|
||||
(11, 80), (5, 76), (2, 84), (5, 78), (-6, 55), (4, 61), (-14, 83), (-37, 127),
|
||||
(-5, 79), (-11, 104), (-11, 91), (-30, 127), (-4, 86), (-12, 88), (-5, 82), (-3, 72),
|
||||
(-4, 67), (-8, 72), (-16, 89), (-9, 69), (-1, 59), (5, 66), (4, 57), (-4, 71),
|
||||
(-2, 71), (2, 58), (-1, 74), (-4, 44), (-1, 69), (0, 62), (-7, 51), (-4, 47),
|
||||
(-6, 42), (-3, 41), (-6, 53), (8, 76), (-9, 78), (-11, 83), (9, 52), (0, 67),
|
||||
(-5, 90), (1, 67), (-15, 72), (-5, 75), (-8, 80), (-21, 83), (-21, 64), (-13, 31),
|
||||
(-25, 64), (-29, 94), (9, 75), (17, 63), (-8, 74), (-5, 35), (-2, 27), (13, 91),
|
||||
(-4, 86), (-12, 88), (-5, 82), (-3, 72), (-4, 67), (-8, 72), (-16, 89), (-9, 69),
|
||||
(-1, 59), (5, 66), (4, 57), (-4, 71), (-2, 71), (2, 58), (-1, 74), (-4, 44),
|
||||
(-1, 69), (0, 62), (-7, 51), (-4, 47), (-6, 42), (-3, 41), (-6, 53), (8, 76),
|
||||
(-9, 78), (-11, 83), (9, 52), (0, 67), (-5, 90), (1, 67), (-15, 72), (-5, 75),
|
||||
(-8, 80), (-21, 83), (-21, 64), (-13, 31), (-25, 64), (-29, 94), (9, 75), (17, 63),
|
||||
(-8, 74), (-5, 35), (-2, 27), (13, 91), (4, 39), (0, 42), (7, 34), (11, 29),
|
||||
(8, 31), (6, 37), (7, 42), (3, 40), (8, 33), (13, 43), (13, 36), (4, 47),
|
||||
(3, 55), (2, 58), (6, 60), (8, 44), (11, 44), (14, 42), (7, 48), (4, 56),
|
||||
(4, 52), (13, 37), (9, 49), (19, 58), (10, 48), (12, 45), (0, 69), (20, 33),
|
||||
(8, 63), (35, -18), (33, -25), (28, -3), (24, 10), (27, 0), (34, -14), (52, -44),
|
||||
(39, -24), (19, 17), (31, 25), (36, 29), (24, 33), (34, 15), (30, 20), (22, 73),
|
||||
(4, 39), (0, 42), (7, 34), (11, 29), (8, 31), (6, 37), (7, 42), (3, 40),
|
||||
(8, 33), (13, 43), (13, 36), (4, 47), (3, 55), (2, 58), (6, 60), (8, 44),
|
||||
(11, 44), (14, 42), (7, 48), (4, 56), (4, 52), (13, 37), (9, 49), (19, 58),
|
||||
(10, 48), (12, 45), (0, 69), (20, 33), (8, 63), (35, -18), (33, -25), (28, -3),
|
||||
(24, 10), (27, 0), (34, -14), (52, -44), (39, -24), (19, 17), (31, 25), (36, 29),
|
||||
(24, 33), (34, 15), (30, 20), (22, 73), (-3, 78), (-8, 74), (-9, 72), (-10, 72),
|
||||
(-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53),
|
||||
(-14, 59), (-9, 52), (-11, 68), (-3, 78), (-8, 74), (-9, 72), (-10, 72), (-18, 75),
|
||||
(-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53), (-14, 59),
|
||||
(-9, 52), (-11, 68), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7), (31, 12),
|
||||
(37, 23), (31, 38), (20, 64), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7),
|
||||
(31, 12), (37, 23), (31, 38), (20, 64), (-9, 71), (-7, 37), (-8, 44), (-11, 49),
|
||||
(-10, 56), (-12, 59), (-8, 63), (-9, 67), (-6, 68), (-10, 79), (-3, 78), (-8, 74),
|
||||
(-9, 72), (-10, 72), (-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72),
|
||||
(-16, 67), (-8, 53), (-14, 59), (-9, 52), (-11, 68), (-3, 78), (-8, 74), (-9, 72),
|
||||
(-10, 72), (-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67),
|
||||
(-8, 53), (-14, 59), (-9, 52), (-11, 68), (9, -2), (30, -10), (31, -4), (33, -1),
|
||||
(33, 7), (31, 12), (37, 23), (31, 38), (20, 64), (9, -2), (30, -10), (31, -4),
|
||||
(33, -1), (33, 7), (31, 12), (37, 23), (31, 38), (20, 64), (-9, 71), (-7, 37),
|
||||
(-8, 44), (-11, 49), (-10, 56), (-12, 59), (-8, 63), (-9, 67), (-6, 68), (-10, 79),
|
||||
(-22, 127), (-25, 127), (-25, 120), (-27, 127), (-19, 114), (-23, 117), (-25, 118), (-26, 117),
|
||||
(-24, 113), (-28, 118), (-31, 120), (-37, 124), (-10, 94), (-15, 102), (-10, 99), (-13, 106),
|
||||
(-50, 127), (-5, 92), (17, 57), (-5, 86), (-13, 94), (-12, 91), (-2, 77), (0, 71),
|
||||
(-1, 73), (4, 64), (-7, 81), (5, 64), (15, 57), (1, 67), (0, 68), (-10, 67),
|
||||
(1, 68), (0, 77), (2, 64), (0, 68), (-5, 78), (7, 55), (5, 59), (2, 65),
|
||||
(14, 54), (15, 44), (5, 60), (2, 70), (-22, 127), (-25, 127), (-25, 120), (-27, 127),
|
||||
(-19, 114), (-23, 117), (-25, 118), (-26, 117), (-24, 113), (-28, 118), (-31, 120), (-37, 124),
|
||||
(-10, 94), (-15, 102), (-10, 99), (-13, 106), (-50, 127), (-5, 92), (17, 57), (-5, 86),
|
||||
(-13, 94), (-12, 91), (-2, 77), (0, 71), (-1, 73), (4, 64), (-7, 81), (5, 64),
|
||||
(15, 57), (1, 67), (0, 68), (-10, 67), (1, 68), (0, 77), (2, 64), (0, 68),
|
||||
(-5, 78), (7, 55), (5, 59), (2, 65), (14, 54), (15, 44), (5, 60), (2, 70),
|
||||
(17, -13), (16, -9), (17, -12), (27, -21), (37, -30), (41, -40), (42, -41), (48, -47),
|
||||
(39, -32), (46, -40), (52, -51), (46, -41), (52, -39), (43, -19), (32, 11), (61, -55),
|
||||
(56, -46), (62, -50), (81, -67), (45, -20), (35, -2), (28, 15), (34, 1), (39, 1),
|
||||
(30, 17), (20, 38), (18, 45), (15, 54), (0, 79), (36, -16), (37, -14), (37, -17),
|
||||
(32, 1), (34, 15), (29, 15), (24, 25), (34, 22), (31, 16), (35, 18), (31, 28),
|
||||
(33, 41), (36, 28), (27, 47), (21, 62), (17, -13), (16, -9), (17, -12), (27, -21),
|
||||
(37, -30), (41, -40), (42, -41), (48, -47), (39, -32), (46, -40), (52, -51), (46, -41),
|
||||
(52, -39), (43, -19), (32, 11), (61, -55), (56, -46), (62, -50), (81, -67), (45, -20),
|
||||
(35, -2), (28, 15), (34, 1), (39, 1), (30, 17), (20, 38), (18, 45), (15, 54),
|
||||
(0, 79), (36, -16), (37, -14), (37, -17), (32, 1), (34, 15), (29, 15), (24, 25),
|
||||
(34, 22), (31, 16), (35, 18), (31, 28), (33, 41), (36, 28), (27, 47), (21, 62),
|
||||
(-24, 115), (-22, 82), (-9, 62), (0, 53), (0, 59), (-14, 85), (-13, 89), (-13, 94),
|
||||
(-11, 92), (-29, 127), (-21, 100), (-14, 57), (-12, 67), (-11, 71), (-10, 77), (-21, 85),
|
||||
(-16, 88), (-23, 104), (-15, 98), (-37, 127), (-10, 82), (-8, 48), (-8, 61), (-8, 66),
|
||||
(-7, 70), (-14, 75), (-10, 79), (-9, 83), (-12, 92), (-18, 108), (-24, 115), (-22, 82),
|
||||
(-9, 62), (0, 53), (0, 59), (-14, 85), (-13, 89), (-13, 94), (-11, 92), (-29, 127),
|
||||
(-21, 100), (-14, 57), (-12, 67), (-11, 71), (-10, 77), (-21, 85), (-16, 88), (-23, 104),
|
||||
(-15, 98), (-37, 127), (-10, 82), (-8, 48), (-8, 61), (-8, 66), (-7, 70), (-14, 75),
|
||||
(-10, 79), (-9, 83), (-12, 92), (-18, 108), (-5, 79), (-11, 104), (-11, 91), (-30, 127),
|
||||
(-5, 79), (-11, 104), (-11, 91), (-30, 127), (-5, 79), (-11, 104), (-11, 91), (-30, 127),
|
||||
],
|
||||
[
|
||||
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
|
||||
(-6, 53), (-1, 54), (7, 51), (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, 41), (0, 63), (0, 63), (0, 63),
|
||||
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (0, 11), (1, 55),
|
||||
(0, 69), (-17, 127), (-13, 102), (0, 82), (-7, 74), (-21, 107), (-27, 127), (-31, 127),
|
||||
(-24, 127), (-18, 95), (-27, 127), (-21, 114), (-30, 127), (-17, 123), (-12, 115), (-16, 122),
|
||||
(-11, 115), (-12, 63), (-2, 68), (-15, 84), (-13, 104), (-3, 70), (-8, 93), (-10, 90),
|
||||
(-30, 127), (-1, 74), (-6, 97), (-7, 91), (-20, 127), (-4, 56), (-5, 82), (-7, 76),
|
||||
(-22, 125), (-7, 93), (-11, 87), (-3, 77), (-5, 71), (-4, 63), (-4, 68), (-12, 84),
|
||||
(-7, 62), (-7, 65), (8, 61), (5, 56), (-2, 66), (1, 64), (0, 61), (-2, 78),
|
||||
(1, 50), (7, 52), (10, 35), (0, 44), (11, 38), (1, 45), (0, 46), (5, 44),
|
||||
(31, 17), (1, 51), (7, 50), (28, 19), (16, 33), (14, 62), (-13, 108), (-15, 100),
|
||||
(-13, 101), (-13, 91), (-12, 94), (-10, 88), (-16, 84), (-10, 86), (-7, 83), (-13, 87),
|
||||
(-19, 94), (1, 70), (0, 72), (-5, 74), (18, 59), (-8, 102), (-15, 100), (0, 95),
|
||||
(-4, 75), (2, 72), (-11, 75), (-3, 71), (15, 46), (-13, 69), (0, 62), (0, 65),
|
||||
(21, 37), (-15, 72), (9, 57), (16, 54), (0, 62), (12, 72), (24, 0), (15, 9),
|
||||
(8, 25), (13, 18), (15, 9), (13, 19), (10, 37), (12, 18), (6, 29), (20, 33),
|
||||
(15, 30), (4, 45), (1, 58), (0, 62), (7, 61), (12, 38), (11, 45), (15, 39),
|
||||
(11, 42), (13, 44), (16, 45), (12, 41), (10, 49), (30, 34), (18, 42), (10, 55),
|
||||
(17, 51), (17, 46), (0, 89), (26, -19), (22, -17), (26, -17), (30, -25), (28, -20),
|
||||
(33, -23), (37, -27), (33, -23), (40, -28), (38, -17), (33, -11), (40, -15), (41, -6),
|
||||
(38, 1), (41, 17), (30, -6), (27, 3), (26, 22), (37, -16), (35, -4), (38, -8),
|
||||
(38, -3), (37, 3), (38, 5), (42, 0), (35, 16), (39, 22), (14, 48), (27, 37),
|
||||
(21, 60), (12, 68), (2, 97), (-3, 71), (-6, 42), (-5, 50), (-3, 54), (-2, 62),
|
||||
(0, 58), (1, 63), (-2, 72), (-1, 74), (-9, 91), (-5, 67), (-5, 27), (-3, 39),
|
||||
(-2, 44), (0, 46), (-16, 64), (-8, 68), (-10, 78), (-6, 77), (-10, 86), (-12, 92),
|
||||
(-15, 55), (-10, 60), (-6, 62), (-4, 65), (-12, 73), (-8, 76), (-7, 80), (-9, 88),
|
||||
(-17, 110), (-11, 97), (-20, 84), (-11, 79), (-6, 73), (-4, 74), (-13, 86), (-13, 96),
|
||||
(-11, 97), (-19, 117), (-8, 78), (-5, 33), (-4, 48), (-2, 53), (-3, 62), (-13, 71),
|
||||
(-10, 79), (-12, 86), (-13, 90), (-14, 97), (0, 0), (-6, 93), (-6, 84), (-8, 79),
|
||||
(0, 66), (-1, 71), (0, 62), (-2, 60), (-2, 59), (-5, 75), (-3, 62), (-4, 58),
|
||||
(-9, 66), (-1, 79), (0, 71), (3, 68), (10, 44), (-7, 62), (15, 36), (14, 40),
|
||||
(16, 27), (12, 29), (1, 44), (20, 36), (18, 32), (5, 42), (1, 48), (10, 62),
|
||||
(17, 46), (9, 64), (-12, 104), (-11, 97), (-16, 96), (-7, 88), (-8, 85), (-7, 85),
|
||||
(-9, 85), (-13, 88), (4, 66), (-3, 77), (-3, 76), (-6, 76), (10, 58), (-1, 76),
|
||||
(-1, 83), (-7, 99), (-14, 95), (2, 95), (0, 76), (-5, 74), (0, 70), (-11, 75),
|
||||
(1, 68), (0, 65), (-14, 73), (3, 62), (4, 62), (-1, 68), (-13, 75), (11, 55),
|
||||
(5, 64), (12, 70), (15, 6), (6, 19), (7, 16), (12, 14), (18, 13), (13, 11),
|
||||
(13, 15), (15, 16), (12, 23), (13, 23), (15, 20), (14, 26), (14, 44), (17, 40),
|
||||
(17, 47), (24, 17), (21, 21), (25, 22), (31, 27), (22, 29), (19, 35), (14, 50),
|
||||
(10, 57), (7, 63), (-2, 77), (-4, 82), (-3, 94), (9, 69), (-12, 109), (36, -35),
|
||||
(36, -34), (32, -26), (37, -30), (44, -32), (34, -18), (34, -15), (40, -15), (33, -7),
|
||||
(35, -5), (33, 0), (38, 2), (33, 13), (23, 35), (13, 58), (29, -3), (26, 0),
|
||||
(22, 30), (31, -7), (35, -15), (34, -3), (34, 3), (36, -1), (34, 5), (32, 11),
|
||||
(35, 5), (34, 12), (39, 11), (30, 29), (34, 26), (29, 39), (19, 66), (31, 21),
|
||||
(31, 31), (25, 50), (-17, 120), (-20, 112), (-18, 114), (-11, 85), (-15, 92), (-14, 89),
|
||||
(-26, 71), (-15, 81), (-14, 80), (0, 68), (-14, 70), (-24, 56), (-23, 68), (-24, 50),
|
||||
(-11, 74), (23, -13), (26, -13), (40, -15), (49, -14), (44, 3), (45, 6), (44, 34),
|
||||
(33, 54), (19, 82), (-3, 75), (-1, 23), (1, 34), (1, 43), (0, 54), (-2, 55),
|
||||
(0, 61), (1, 64), (0, 68), (-9, 92), (-14, 106), (-13, 97), (-15, 90), (-12, 90),
|
||||
(-18, 88), (-10, 73), (-9, 79), (-14, 86), (-10, 73), (-10, 70), (-10, 69), (-5, 66),
|
||||
(-9, 64), (-5, 58), (2, 59), (21, -10), (24, -11), (28, -8), (28, -1), (29, 3),
|
||||
(29, 9), (35, 20), (29, 36), (14, 67), (-17, 123), (-12, 115), (-16, 122), (-11, 115),
|
||||
(-12, 63), (-2, 68), (-15, 84), (-13, 104), (-3, 70), (-8, 93), (-10, 90), (-30, 127),
|
||||
(-17, 123), (-12, 115), (-16, 122), (-11, 115), (-12, 63), (-2, 68), (-15, 84), (-13, 104),
|
||||
(-3, 70), (-8, 93), (-10, 90), (-30, 127), (-7, 93), (-11, 87), (-3, 77), (-5, 71),
|
||||
(-4, 63), (-4, 68), (-12, 84), (-7, 62), (-7, 65), (8, 61), (5, 56), (-2, 66),
|
||||
(1, 64), (0, 61), (-2, 78), (1, 50), (7, 52), (10, 35), (0, 44), (11, 38),
|
||||
(1, 45), (0, 46), (5, 44), (31, 17), (1, 51), (7, 50), (28, 19), (16, 33),
|
||||
(14, 62), (-13, 108), (-15, 100), (-13, 101), (-13, 91), (-12, 94), (-10, 88), (-16, 84),
|
||||
(-10, 86), (-7, 83), (-13, 87), (-19, 94), (1, 70), (0, 72), (-5, 74), (18, 59),
|
||||
(-7, 93), (-11, 87), (-3, 77), (-5, 71), (-4, 63), (-4, 68), (-12, 84), (-7, 62),
|
||||
(-7, 65), (8, 61), (5, 56), (-2, 66), (1, 64), (0, 61), (-2, 78), (1, 50),
|
||||
(7, 52), (10, 35), (0, 44), (11, 38), (1, 45), (0, 46), (5, 44), (31, 17),
|
||||
(1, 51), (7, 50), (28, 19), (16, 33), (14, 62), (-13, 108), (-15, 100), (-13, 101),
|
||||
(-13, 91), (-12, 94), (-10, 88), (-16, 84), (-10, 86), (-7, 83), (-13, 87), (-19, 94),
|
||||
(1, 70), (0, 72), (-5, 74), (18, 59), (24, 0), (15, 9), (8, 25), (13, 18),
|
||||
(15, 9), (13, 19), (10, 37), (12, 18), (6, 29), (20, 33), (15, 30), (4, 45),
|
||||
(1, 58), (0, 62), (7, 61), (12, 38), (11, 45), (15, 39), (11, 42), (13, 44),
|
||||
(16, 45), (12, 41), (10, 49), (30, 34), (18, 42), (10, 55), (17, 51), (17, 46),
|
||||
(0, 89), (26, -19), (22, -17), (26, -17), (30, -25), (28, -20), (33, -23), (37, -27),
|
||||
(33, -23), (40, -28), (38, -17), (33, -11), (40, -15), (41, -6), (38, 1), (41, 17),
|
||||
(24, 0), (15, 9), (8, 25), (13, 18), (15, 9), (13, 19), (10, 37), (12, 18),
|
||||
(6, 29), (20, 33), (15, 30), (4, 45), (1, 58), (0, 62), (7, 61), (12, 38),
|
||||
(11, 45), (15, 39), (11, 42), (13, 44), (16, 45), (12, 41), (10, 49), (30, 34),
|
||||
(18, 42), (10, 55), (17, 51), (17, 46), (0, 89), (26, -19), (22, -17), (26, -17),
|
||||
(30, -25), (28, -20), (33, -23), (37, -27), (33, -23), (40, -28), (38, -17), (33, -11),
|
||||
(40, -15), (41, -6), (38, 1), (41, 17), (-17, 120), (-20, 112), (-18, 114), (-11, 85),
|
||||
(-15, 92), (-14, 89), (-26, 71), (-15, 81), (-14, 80), (0, 68), (-14, 70), (-24, 56),
|
||||
(-23, 68), (-24, 50), (-11, 74), (-14, 106), (-13, 97), (-15, 90), (-12, 90), (-18, 88),
|
||||
(-10, 73), (-9, 79), (-14, 86), (-10, 73), (-10, 70), (-10, 69), (-5, 66), (-9, 64),
|
||||
(-5, 58), (2, 59), (23, -13), (26, -13), (40, -15), (49, -14), (44, 3), (45, 6),
|
||||
(44, 34), (33, 54), (19, 82), (21, -10), (24, -11), (28, -8), (28, -1), (29, 3),
|
||||
(29, 9), (35, 20), (29, 36), (14, 67), (-3, 75), (-1, 23), (1, 34), (1, 43),
|
||||
(0, 54), (-2, 55), (0, 61), (1, 64), (0, 68), (-9, 92), (-17, 120), (-20, 112),
|
||||
(-18, 114), (-11, 85), (-15, 92), (-14, 89), (-26, 71), (-15, 81), (-14, 80), (0, 68),
|
||||
(-14, 70), (-24, 56), (-23, 68), (-24, 50), (-11, 74), (-14, 106), (-13, 97), (-15, 90),
|
||||
(-12, 90), (-18, 88), (-10, 73), (-9, 79), (-14, 86), (-10, 73), (-10, 70), (-10, 69),
|
||||
(-5, 66), (-9, 64), (-5, 58), (2, 59), (23, -13), (26, -13), (40, -15), (49, -14),
|
||||
(44, 3), (45, 6), (44, 34), (33, 54), (19, 82), (21, -10), (24, -11), (28, -8),
|
||||
(28, -1), (29, 3), (29, 9), (35, 20), (29, 36), (14, 67), (-3, 75), (-1, 23),
|
||||
(1, 34), (1, 43), (0, 54), (-2, 55), (0, 61), (1, 64), (0, 68), (-9, 92),
|
||||
(-6, 93), (-6, 84), (-8, 79), (0, 66), (-1, 71), (0, 62), (-2, 60), (-2, 59),
|
||||
(-5, 75), (-3, 62), (-4, 58), (-9, 66), (-1, 79), (0, 71), (3, 68), (10, 44),
|
||||
(-7, 62), (15, 36), (14, 40), (16, 27), (12, 29), (1, 44), (20, 36), (18, 32),
|
||||
(5, 42), (1, 48), (10, 62), (17, 46), (9, 64), (-12, 104), (-11, 97), (-16, 96),
|
||||
(-7, 88), (-8, 85), (-7, 85), (-9, 85), (-13, 88), (4, 66), (-3, 77), (-3, 76),
|
||||
(-6, 76), (10, 58), (-1, 76), (-1, 83), (-6, 93), (-6, 84), (-8, 79), (0, 66),
|
||||
(-1, 71), (0, 62), (-2, 60), (-2, 59), (-5, 75), (-3, 62), (-4, 58), (-9, 66),
|
||||
(-1, 79), (0, 71), (3, 68), (10, 44), (-7, 62), (15, 36), (14, 40), (16, 27),
|
||||
(12, 29), (1, 44), (20, 36), (18, 32), (5, 42), (1, 48), (10, 62), (17, 46),
|
||||
(9, 64), (-12, 104), (-11, 97), (-16, 96), (-7, 88), (-8, 85), (-7, 85), (-9, 85),
|
||||
(-13, 88), (4, 66), (-3, 77), (-3, 76), (-6, 76), (10, 58), (-1, 76), (-1, 83),
|
||||
(15, 6), (6, 19), (7, 16), (12, 14), (18, 13), (13, 11), (13, 15), (15, 16),
|
||||
(12, 23), (13, 23), (15, 20), (14, 26), (14, 44), (17, 40), (17, 47), (24, 17),
|
||||
(21, 21), (25, 22), (31, 27), (22, 29), (19, 35), (14, 50), (10, 57), (7, 63),
|
||||
(-2, 77), (-4, 82), (-3, 94), (9, 69), (-12, 109), (36, -35), (36, -34), (32, -26),
|
||||
(37, -30), (44, -32), (34, -18), (34, -15), (40, -15), (33, -7), (35, -5), (33, 0),
|
||||
(38, 2), (33, 13), (23, 35), (13, 58), (15, 6), (6, 19), (7, 16), (12, 14),
|
||||
(18, 13), (13, 11), (13, 15), (15, 16), (12, 23), (13, 23), (15, 20), (14, 26),
|
||||
(14, 44), (17, 40), (17, 47), (24, 17), (21, 21), (25, 22), (31, 27), (22, 29),
|
||||
(19, 35), (14, 50), (10, 57), (7, 63), (-2, 77), (-4, 82), (-3, 94), (9, 69),
|
||||
(-12, 109), (36, -35), (36, -34), (32, -26), (37, -30), (44, -32), (34, -18), (34, -15),
|
||||
(40, -15), (33, -7), (35, -5), (33, 0), (38, 2), (33, 13), (23, 35), (13, 58),
|
||||
(-3, 71), (-6, 42), (-5, 50), (-3, 54), (-2, 62), (0, 58), (1, 63), (-2, 72),
|
||||
(-1, 74), (-9, 91), (-5, 67), (-5, 27), (-3, 39), (-2, 44), (0, 46), (-16, 64),
|
||||
(-8, 68), (-10, 78), (-6, 77), (-10, 86), (-12, 92), (-15, 55), (-10, 60), (-6, 62),
|
||||
(-4, 65), (-12, 73), (-8, 76), (-7, 80), (-9, 88), (-17, 110), (-3, 71), (-6, 42),
|
||||
(-5, 50), (-3, 54), (-2, 62), (0, 58), (1, 63), (-2, 72), (-1, 74), (-9, 91),
|
||||
(-5, 67), (-5, 27), (-3, 39), (-2, 44), (0, 46), (-16, 64), (-8, 68), (-10, 78),
|
||||
(-6, 77), (-10, 86), (-12, 92), (-15, 55), (-10, 60), (-6, 62), (-4, 65), (-12, 73),
|
||||
(-8, 76), (-7, 80), (-9, 88), (-17, 110), (-3, 70), (-8, 93), (-10, 90), (-30, 127),
|
||||
(-3, 70), (-8, 93), (-10, 90), (-30, 127), (-3, 70), (-8, 93), (-10, 90), (-30, 127),
|
||||
],
|
||||
];
|
||||
@@ -0,0 +1,288 @@
|
||||
//! CAVLC residual block parsing (9.2).
|
||||
|
||||
use crate::h264::decode::cavlc_tables::{COEFF_TOKEN, RUN_BEFORE, TOTAL_ZEROS_2X2, TOTAL_ZEROS_2X4, TOTAL_ZEROS_4X4};
|
||||
use crate::h264::decode::error::{Result, ensure};
|
||||
use craft_media::bitstream::BitReader;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
/// Prefix-code lookup keyed by (number of leading zeros, next 5 bits after the first one bit).
|
||||
pub(crate) struct VlcTable {
|
||||
/// entry = (len << 8) | value, 0 = invalid.
|
||||
entries: Box<[u16; 17 * 32]>,
|
||||
}
|
||||
|
||||
impl VlcTable {
|
||||
pub fn build(codes: &[(&str, u8)]) -> Self {
|
||||
let mut entries = Box::new([0u16; 17 * 32]);
|
||||
for &(code, value) in codes {
|
||||
if code.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let len = code.len();
|
||||
let lz = code.bytes().take_while(|&b| b == b'0').count();
|
||||
let val = ((len as u16) << 8) | value as u16;
|
||||
if lz == len {
|
||||
// all-zero code: any continuation with at least `len` zeros
|
||||
for l in lz..=16 {
|
||||
for s in 0..32 {
|
||||
entries[l * 32 + s] = val;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let suffix = &code[lz + 1..];
|
||||
assert!(suffix.len() <= 5, "suffix too long in {code}");
|
||||
let sbits = suffix.bytes().fold(0u32, |a, b| (a << 1) | u32::from(b == b'1'));
|
||||
let free = 5 - suffix.len();
|
||||
for ext in 0..(1u32 << free) {
|
||||
let s = (sbits << free) | ext;
|
||||
entries[lz * 32 + s as usize] = val;
|
||||
}
|
||||
}
|
||||
Self { entries }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn decode(&self, r: &mut BitReader) -> Result<u8> {
|
||||
let bits = r.peek(32);
|
||||
let lz = bits.leading_zeros().min(16);
|
||||
let s = if lz >= 16 { 0 } else { ((bits << lz) << 1) >> 27 };
|
||||
let e = self.entries[(lz * 32 + s) as usize];
|
||||
ensure!(e != 0, "invalid VLC code");
|
||||
r.skip((e >> 8) as usize)?;
|
||||
Ok(e as u8)
|
||||
}
|
||||
}
|
||||
|
||||
struct Tables {
|
||||
/// coeff_token per nC class; value = (TrailingOnes << 5) | TotalCoeff
|
||||
coeff_token: [VlcTable; 6],
|
||||
total_zeros_4x4: [VlcTable; 15],
|
||||
total_zeros_2x2: [VlcTable; 3],
|
||||
total_zeros_2x4: [VlcTable; 7],
|
||||
run_before: [VlcTable; 7],
|
||||
}
|
||||
|
||||
fn tables() -> &'static Tables {
|
||||
static T: OnceLock<Tables> = OnceLock::new();
|
||||
T.get_or_init(|| {
|
||||
let coeff_token = std::array::from_fn(|col| {
|
||||
let codes: Vec<(&str, u8)> = COEFF_TOKEN.iter().map(|&(t1, tc, ref c)| (c[col], (t1 << 5) | tc)).collect();
|
||||
VlcTable::build(&codes)
|
||||
});
|
||||
fn simple<const N: usize>(row: &[&'static str; N]) -> VlcTable {
|
||||
let codes: Vec<(&str, u8)> = row.iter().enumerate().map(|(i, &c)| (c, i as u8)).collect();
|
||||
VlcTable::build(&codes)
|
||||
}
|
||||
Tables {
|
||||
coeff_token,
|
||||
total_zeros_4x4: std::array::from_fn(|i| simple(&TOTAL_ZEROS_4X4[i])),
|
||||
total_zeros_2x2: std::array::from_fn(|i| simple(&TOTAL_ZEROS_2X2[i])),
|
||||
total_zeros_2x4: std::array::from_fn(|i| simple(&TOTAL_ZEROS_2X4[i])),
|
||||
run_before: std::array::from_fn(|i| simple(&RUN_BEFORE[i])),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Force table construction (so the first slice does not pay for it).
|
||||
pub(crate) fn init_tables() {
|
||||
let _ = tables();
|
||||
}
|
||||
|
||||
/// Parse residual_block_cavlc(). Each non-zero coefficient is passed to `put` as (scan index in
|
||||
/// `start_idx..=end_idx`, level); the others are zero.
|
||||
/// `nc` is the nC value (-1 for 4:2:0 chroma DC, -2 for 4:2:2 chroma DC). Returns TotalCoeff.
|
||||
pub(crate) fn residual_block(
|
||||
r: &mut BitReader,
|
||||
start_idx: usize,
|
||||
end_idx: usize,
|
||||
max_num_coeff: usize,
|
||||
nc: i32,
|
||||
mut put: impl FnMut(usize, i32),
|
||||
) -> Result<u8> {
|
||||
let t = tables();
|
||||
let col = match nc {
|
||||
-1 => 4,
|
||||
-2 => 5,
|
||||
0..=1 => 0,
|
||||
2..=3 => 1,
|
||||
4..=7 => 2,
|
||||
_ => 3,
|
||||
};
|
||||
let tok = t.coeff_token[col].decode(r)?;
|
||||
let total_coeff = (tok & 31) as usize;
|
||||
let trailing_ones = (tok >> 5) as usize;
|
||||
if total_coeff == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
ensure!(total_coeff <= max_num_coeff, "TotalCoeff exceeds maxNumCoeff");
|
||||
let mut level = [0i32; 16];
|
||||
let mut suffix_length: u32 = if total_coeff > 10 && trailing_ones < 3 { 1 } else { 0 };
|
||||
for (i, lv) in level.iter_mut().enumerate().take(total_coeff) {
|
||||
if i < trailing_ones {
|
||||
*lv = if r.read_bit()? { -1 } else { 1 };
|
||||
continue;
|
||||
}
|
||||
// level_prefix
|
||||
let bits = r.peek(32);
|
||||
let level_prefix = bits.leading_zeros();
|
||||
ensure!(level_prefix <= 25, "level_prefix too large");
|
||||
r.skip(level_prefix as usize + 1)?;
|
||||
let level_suffix_size = if level_prefix == 14 && suffix_length == 0 {
|
||||
4
|
||||
} else if level_prefix >= 15 {
|
||||
level_prefix - 3
|
||||
} else {
|
||||
suffix_length
|
||||
};
|
||||
let level_suffix = if level_suffix_size > 0 { r.read_bits(level_suffix_size)? as i32 } else { 0 };
|
||||
let mut level_code = ((level_prefix.min(15) as i32) << suffix_length) + level_suffix;
|
||||
if level_prefix >= 15 && suffix_length == 0 {
|
||||
level_code += 15;
|
||||
}
|
||||
if level_prefix >= 16 {
|
||||
level_code += (1 << (level_prefix - 3)) - 4096;
|
||||
}
|
||||
if i == trailing_ones && trailing_ones < 3 {
|
||||
level_code += 2;
|
||||
}
|
||||
*lv = if level_code % 2 == 0 { (level_code + 2) >> 1 } else { (-level_code - 1) >> 1 };
|
||||
if suffix_length == 0 {
|
||||
suffix_length = 1;
|
||||
}
|
||||
if lv.abs() > (3 << (suffix_length - 1)) && suffix_length < 6 {
|
||||
suffix_length += 1;
|
||||
}
|
||||
}
|
||||
let mut zeros_left = if total_coeff < end_idx - start_idx + 1 {
|
||||
let tz_idx = total_coeff - 1;
|
||||
let tz = match max_num_coeff {
|
||||
4 => t.total_zeros_2x2[tz_idx].decode(r)?,
|
||||
8 => t.total_zeros_2x4[tz_idx].decode(r)?,
|
||||
_ => t.total_zeros_4x4[tz_idx].decode(r)?,
|
||||
} as usize;
|
||||
ensure!(tz + total_coeff <= end_idx - start_idx + 1, "total_zeros out of range");
|
||||
tz
|
||||
} else {
|
||||
0
|
||||
};
|
||||
// runs and placement: highest-frequency coefficient first
|
||||
let mut pos = start_idx + total_coeff + zeros_left - 1;
|
||||
for (i, &lv) in level.iter().enumerate().take(total_coeff) {
|
||||
put(pos, lv);
|
||||
if i + 1 == total_coeff {
|
||||
break;
|
||||
}
|
||||
let run = if zeros_left > 0 {
|
||||
let run = t.run_before[zeros_left.min(7) - 1].decode(r)? as usize;
|
||||
ensure!(run <= zeros_left, "run_before out of range");
|
||||
zeros_left -= run;
|
||||
run
|
||||
} else {
|
||||
0
|
||||
};
|
||||
pos -= 1 + run;
|
||||
}
|
||||
Ok(total_coeff as u8)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use craft_media::bitstream::BitWriter;
|
||||
|
||||
fn kraft(codes: &[&str]) -> f64 {
|
||||
codes.iter().filter(|c| !c.is_empty()).map(|c| 0.5f64.powi(c.len() as i32)).sum()
|
||||
}
|
||||
|
||||
fn prefix_free(codes: &[&str]) -> bool {
|
||||
let c: Vec<&&str> = codes.iter().filter(|c| !c.is_empty()).collect();
|
||||
for (i, a) in c.iter().enumerate() {
|
||||
for (j, b) in c.iter().enumerate() {
|
||||
if i != j && b.starts_with(**a) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coeff_token_tables_are_prefix_codes() {
|
||||
for col in 0..6 {
|
||||
let codes: Vec<&str> = COEFF_TOKEN.iter().map(|e| e.2[col]).collect();
|
||||
assert!(prefix_free(&codes), "column {col}");
|
||||
let k = kraft(&codes);
|
||||
assert!(k <= 1.0 + 1e-12, "column {col} kraft {k}");
|
||||
}
|
||||
// nC>=8 is a complete 6-bit fixed code except the unused 000011 pattern
|
||||
let codes: Vec<&str> = COEFF_TOKEN.iter().map(|e| e.2[3]).collect();
|
||||
assert!(codes.iter().all(|c| c.len() == 6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn total_zeros_and_run_before_are_complete_prefix_codes() {
|
||||
for (i, row) in TOTAL_ZEROS_4X4.iter().enumerate() {
|
||||
assert!(prefix_free(row));
|
||||
// tzVlcIndex 1 leaves the all-zero 9-bit code unused; all others are complete
|
||||
let k = kraft(row);
|
||||
if i == 0 {
|
||||
assert!((k - (1.0 - 1.0 / 512.0)).abs() < 1e-12, "{row:?}");
|
||||
} else {
|
||||
assert!((k - 1.0).abs() < 1e-12, "{row:?}");
|
||||
}
|
||||
}
|
||||
for row in TOTAL_ZEROS_2X2.iter() {
|
||||
assert!(prefix_free(row));
|
||||
assert!((kraft(row) - 1.0).abs() < 1e-12);
|
||||
}
|
||||
for row in TOTAL_ZEROS_2X4.iter() {
|
||||
assert!(prefix_free(row));
|
||||
assert!((kraft(row) - 1.0).abs() < 1e-12);
|
||||
}
|
||||
for row in RUN_BEFORE.iter() {
|
||||
assert!(prefix_free(row));
|
||||
}
|
||||
}
|
||||
|
||||
fn write_code(w: &mut BitWriter, code: &str) {
|
||||
for b in code.bytes() {
|
||||
w.write_bit(b == b'1');
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_spec_style_example() {
|
||||
// Block: 0 3 -1 0 | 0 -1 1 0 | 1 0 0 0 | 0 0 0 0 (zig-zag order), nC = 0.
|
||||
// TotalCoeff 5, TrailingOnes 3: coeff_token 0000 100.
|
||||
// trailing ones (reverse order): +1, -1, -1 -> sign bits 0 1 1
|
||||
// level -1? No: remaining levels (reverse): 1 (idx 4?) ... use the classic example:
|
||||
// coefficients in scan order: [0, 3, -1, 0, 0, -1, 1, 0, 1, 0, ...]
|
||||
// reverse order non-zero: 1 (pos 8), 1 (pos 6), -1 (pos 5), -1 (pos 2), 3 (pos 1)
|
||||
// TotalCoeff 5, TrailingOnes 3 (1, 1, -1).
|
||||
let mut w = BitWriter::new();
|
||||
write_code(&mut w, "0000100"); // coeff_token TC=5 T1=3, nC 0..2
|
||||
write_code(&mut w, "0"); // +1
|
||||
write_code(&mut w, "0"); // +1
|
||||
write_code(&mut w, "1"); // -1
|
||||
// level -1 with suffixLength 0: levelCode = 1 (T1 == 3 so no +2) -> prefix 1 => "01"
|
||||
write_code(&mut w, "01");
|
||||
// level 3 with suffixLength 1: levelCode = 4 -> prefix 2, suffix 0 => "001" "0"
|
||||
write_code(&mut w, "0010");
|
||||
// total_zeros = 4 (positions 0, 3, 4, 7) with tzVlcIndex 5 -> "110"
|
||||
write_code(&mut w, "110");
|
||||
// run_before: zerosLeft 4 at pos 8: run 1 -> "10"; zerosLeft 3 at pos 6: run 0 -> "11";
|
||||
// zerosLeft 3 at pos 5: run 2 -> "01"; zerosLeft 1 at pos 2: run 0 -> "1"
|
||||
write_code(&mut w, "10");
|
||||
write_code(&mut w, "11");
|
||||
write_code(&mut w, "01");
|
||||
write_code(&mut w, "1");
|
||||
w.rbsp_trailing();
|
||||
let b = w.finish();
|
||||
let mut r = BitReader::new(&b);
|
||||
let mut c = [0i32; 16];
|
||||
let tc = residual_block(&mut r, 0, 15, 16, 0, |k, v| c[k] = v).unwrap();
|
||||
assert_eq!(tc, 5);
|
||||
assert_eq!(&c[..10], &[0, 3, -1, 0, 0, -1, 1, 0, 1, 0]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
// Generated from ITU-T H.264 (06/2026) Tables 9-5, 9-7..9-10 by a table-extraction script.
|
||||
// Codes are bit strings; "" marks an unused entry.
|
||||
|
||||
/// (TrailingOnes, TotalCoeff, [nC 0..2, 2..4, 4..8, 8.., -1, -2])
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static COEFF_TOKEN: [(u8, u8, [&str; 6]); 62] = [
|
||||
(0, 0, ["1", "11", "1111", "000011", "01", "1"]),
|
||||
(0, 1, ["000101", "001011", "001111", "000000", "000111", "0001111"]),
|
||||
(1, 1, ["01", "10", "1110", "000001", "1", "01"]),
|
||||
(0, 2, ["00000111", "000111", "001011", "000100", "000100", "0001110"]),
|
||||
(1, 2, ["000100", "00111", "01111", "000101", "000110", "0001101"]),
|
||||
(2, 2, ["001", "011", "1101", "000110", "001", "001"]),
|
||||
(0, 3, ["000000111", "0000111", "001000", "001000", "000011", "000000111"]),
|
||||
(1, 3, ["00000110", "001010", "01100", "001001", "0000011", "0001100"]),
|
||||
(2, 3, ["0000101", "001001", "01110", "001010", "0000010", "0001011"]),
|
||||
(3, 3, ["00011", "0101", "1100", "001011", "000101", "00001"]),
|
||||
(0, 4, ["0000000111", "00000111", "0001111", "001100", "000010", "000000110"]),
|
||||
(1, 4, ["000000110", "000110", "01010", "001101", "00000011", "000000101"]),
|
||||
(2, 4, ["00000101", "000101", "01011", "001110", "00000010", "0001010"]),
|
||||
(3, 4, ["000011", "0100", "1011", "001111", "0000000", "000001"]),
|
||||
(0, 5, ["00000000111", "00000100", "0001011", "010000", "", "0000000111"]),
|
||||
(1, 5, ["0000000110", "0000110", "01000", "010001", "", "0000000110"]),
|
||||
(2, 5, ["000000101", "0000101", "01001", "010010", "", "000000100"]),
|
||||
(3, 5, ["0000100", "00110", "1010", "010011", "", "0001001"]),
|
||||
(0, 6, ["0000000001111", "000000111", "0001001", "010100", "", "00000000111"]),
|
||||
(1, 6, ["00000000110", "00000110", "001110", "010101", "", "00000000110"]),
|
||||
(2, 6, ["0000000101", "00000101", "001101", "010110", "", "0000000101"]),
|
||||
(3, 6, ["00000100", "001000", "1001", "010111", "", "0001000"]),
|
||||
(0, 7, ["0000000001011", "00000001111", "0001000", "011000", "", "000000000111"]),
|
||||
(1, 7, ["0000000001110", "000000110", "001010", "011001", "", "000000000110"]),
|
||||
(2, 7, ["00000000101", "000000101", "001001", "011010", "", "00000000101"]),
|
||||
(3, 7, ["000000100", "000100", "1000", "011011", "", "0000000100"]),
|
||||
(0, 8, ["0000000001000", "00000001011", "00001111", "011100", "", "0000000000111"]),
|
||||
(1, 8, ["0000000001010", "00000001110", "0001110", "011101", "", "000000000101"]),
|
||||
(2, 8, ["0000000001101", "00000001101", "0001101", "011110", "", "000000000100"]),
|
||||
(3, 8, ["0000000100", "0000100", "01101", "011111", "", "00000000100"]),
|
||||
(0, 9, ["00000000001111", "000000001111", "00001011", "100000", "", ""]),
|
||||
(1, 9, ["00000000001110", "00000001010", "00001110", "100001", "", ""]),
|
||||
(2, 9, ["0000000001001", "00000001001", "0001010", "100010", "", ""]),
|
||||
(3, 9, ["00000000100", "000000100", "001100", "100011", "", ""]),
|
||||
(0, 10, ["00000000001011", "000000001011", "000001111", "100100", "", ""]),
|
||||
(1, 10, ["00000000001010", "000000001110", "00001010", "100101", "", ""]),
|
||||
(2, 10, ["00000000001101", "000000001101", "00001101", "100110", "", ""]),
|
||||
(3, 10, ["0000000001100", "00000001100", "0001100", "100111", "", ""]),
|
||||
(0, 11, ["000000000001111", "000000001000", "000001011", "101000", "", ""]),
|
||||
(1, 11, ["000000000001110", "000000001010", "000001110", "101001", "", ""]),
|
||||
(2, 11, ["00000000001001", "000000001001", "00001001", "101010", "", ""]),
|
||||
(3, 11, ["00000000001100", "00000001000", "00001100", "101011", "", ""]),
|
||||
(0, 12, ["000000000001011", "0000000001111", "000001000", "101100", "", ""]),
|
||||
(1, 12, ["000000000001010", "0000000001110", "000001010", "101101", "", ""]),
|
||||
(2, 12, ["000000000001101", "0000000001101", "000001101", "101110", "", ""]),
|
||||
(3, 12, ["00000000001000", "000000001100", "00001000", "101111", "", ""]),
|
||||
(0, 13, ["0000000000001111", "0000000001011", "0000001101", "110000", "", ""]),
|
||||
(1, 13, ["000000000000001", "0000000001010", "000000111", "110001", "", ""]),
|
||||
(2, 13, ["000000000001001", "0000000001001", "000001001", "110010", "", ""]),
|
||||
(3, 13, ["000000000001100", "0000000001100", "000001100", "110011", "", ""]),
|
||||
(0, 14, ["0000000000001011", "0000000000111", "0000001001", "110100", "", ""]),
|
||||
(1, 14, ["0000000000001110", "00000000001011", "0000001100", "110101", "", ""]),
|
||||
(2, 14, ["0000000000001101", "0000000000110", "0000001011", "110110", "", ""]),
|
||||
(3, 14, ["000000000001000", "0000000001000", "0000001010", "110111", "", ""]),
|
||||
(0, 15, ["0000000000000111", "00000000001001", "0000000101", "111000", "", ""]),
|
||||
(1, 15, ["0000000000001010", "00000000001000", "0000001000", "111001", "", ""]),
|
||||
(2, 15, ["0000000000001001", "00000000001010", "0000000111", "111010", "", ""]),
|
||||
(3, 15, ["0000000000001100", "0000000000001", "0000000110", "111011", "", ""]),
|
||||
(0, 16, ["0000000000000100", "00000000000111", "0000000001", "111100", "", ""]),
|
||||
(1, 16, ["0000000000000110", "00000000000110", "0000000100", "111101", "", ""]),
|
||||
(2, 16, ["0000000000000101", "00000000000101", "0000000011", "111110", "", ""]),
|
||||
(3, 16, ["0000000000001000", "00000000000100", "0000000010", "111111", "", ""]),
|
||||
];
|
||||
/// total_zeros for 4x4 blocks: [tzVlcIndex-1][total_zeros]
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static TOTAL_ZEROS_4X4: [[&str; 16]; 15] = [
|
||||
["1", "011", "010", "0011", "0010", "00011", "00010", "000011", "000010", "0000011", "0000010", "00000011", "00000010", "000000011", "000000010", "000000001"],
|
||||
["111", "110", "101", "100", "011", "0101", "0100", "0011", "0010", "00011", "00010", "000011", "000010", "000001", "000000", ""],
|
||||
["0101", "111", "110", "101", "0100", "0011", "100", "011", "0010", "00011", "00010", "000001", "00001", "000000", "", ""],
|
||||
["00011", "111", "0101", "0100", "110", "101", "100", "0011", "011", "0010", "00010", "00001", "00000", "", "", ""],
|
||||
["0101", "0100", "0011", "111", "110", "101", "100", "011", "0010", "00001", "0001", "00000", "", "", "", ""],
|
||||
["000001", "00001", "111", "110", "101", "100", "011", "010", "0001", "001", "000000", "", "", "", "", ""],
|
||||
["000001", "00001", "101", "100", "011", "11", "010", "0001", "001", "000000", "", "", "", "", "", ""],
|
||||
["000001", "0001", "00001", "011", "11", "10", "010", "001", "000000", "", "", "", "", "", "", ""],
|
||||
["000001", "000000", "0001", "11", "10", "001", "01", "00001", "", "", "", "", "", "", "", ""],
|
||||
["00001", "00000", "001", "11", "10", "01", "0001", "", "", "", "", "", "", "", "", ""],
|
||||
["0000", "0001", "001", "010", "1", "011", "", "", "", "", "", "", "", "", "", ""],
|
||||
["0000", "0001", "01", "1", "001", "", "", "", "", "", "", "", "", "", "", ""],
|
||||
["000", "001", "1", "01", "", "", "", "", "", "", "", "", "", "", "", ""],
|
||||
["00", "01", "1", "", "", "", "", "", "", "", "", "", "", "", "", ""],
|
||||
["0", "1", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
|
||||
];
|
||||
/// total_zeros for chroma DC 2x2: [tzVlcIndex-1][total_zeros]
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static TOTAL_ZEROS_2X2: [[&str; 4]; 3] = [
|
||||
["1", "01", "001", "000"],
|
||||
["1", "01", "00", ""],
|
||||
["1", "0", "", ""],
|
||||
];
|
||||
/// total_zeros for chroma DC 2x4: [tzVlcIndex-1][total_zeros]
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static TOTAL_ZEROS_2X4: [[&str; 8]; 7] = [
|
||||
["1", "010", "011", "0010", "0011", "0001", "00001", "00000"],
|
||||
["000", "01", "001", "100", "101", "110", "111", ""],
|
||||
["000", "001", "01", "10", "110", "111", "", ""],
|
||||
["110", "00", "01", "10", "111", "", "", ""],
|
||||
["00", "01", "10", "11", "", "", "", ""],
|
||||
["00", "01", "1", "", "", "", "", ""],
|
||||
["0", "1", "", "", "", "", "", ""],
|
||||
];
|
||||
/// run_before: [min(zerosLeft,7)-1][run_before]
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static RUN_BEFORE: [[&str; 15]; 7] = [
|
||||
["1", "0", "", "", "", "", "", "", "", "", "", "", "", "", ""],
|
||||
["1", "01", "00", "", "", "", "", "", "", "", "", "", "", "", ""],
|
||||
["11", "10", "01", "00", "", "", "", "", "", "", "", "", "", "", ""],
|
||||
["11", "10", "01", "001", "000", "", "", "", "", "", "", "", "", "", ""],
|
||||
["11", "10", "011", "010", "001", "000", "", "", "", "", "", "", "", "", ""],
|
||||
["11", "000", "001", "011", "010", "101", "100", "", "", "", "", "", "", "", ""],
|
||||
["111", "110", "101", "100", "011", "010", "001", "0001", "00001", "000001", "0000001", "00000001", "000000001", "0000000001", "00000000001"],
|
||||
];
|
||||
@@ -0,0 +1,642 @@
|
||||
//! Deblocking filter (8.7) for progressive frames, 4:2:0, 8-bit.
|
||||
|
||||
use crate::h264::decode::picture::{MbKind, MbState, Planes};
|
||||
use crate::h264::decode::slicedec::{PicState, SliceInfo};
|
||||
use crate::h264::decode::tables::{ALPHA, BETA, TC0};
|
||||
|
||||
/// Reference picture identity + mvs of one 4x4 block, for bS = 1 decisions.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Motion {
|
||||
ids: [u32; 2],
|
||||
mv: [[i16; 2]; 2],
|
||||
count: u8,
|
||||
}
|
||||
|
||||
/// Referenced picture ids per 8x8 block and list (u32::MAX = list unused).
|
||||
fn ref_ids8(st: &MbState, sl: &SliceInfo) -> [[u32; 2]; 4] {
|
||||
std::array::from_fn(|b8| {
|
||||
std::array::from_fn(|l| {
|
||||
let r = st.ref_idx[l][b8];
|
||||
if r >= 0 { sl.ref_ids[l].get(r as usize).copied().unwrap_or(u32::MAX - 1) } else { u32::MAX }
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn b8_of(raster: usize) -> usize {
|
||||
(raster >> 3) * 2 + ((raster & 3) >> 1)
|
||||
}
|
||||
|
||||
/// A macroblock's motion for bS derivation; the reference picture ids are looked up on first use.
|
||||
struct MbMotion<'a> {
|
||||
st: &'a MbState,
|
||||
sl: &'a SliceInfo,
|
||||
ids8: Option<[[u32; 2]; 4]>,
|
||||
}
|
||||
|
||||
impl<'a> MbMotion<'a> {
|
||||
fn new(st: &'a MbState, sl: &'a SliceInfo) -> Self {
|
||||
MbMotion { st, sl, ids8: None }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn block(&mut self, raster: usize) -> Motion {
|
||||
let ids = self.ids8.get_or_insert_with(|| ref_ids8(self.st, self.sl))[b8_of(raster)];
|
||||
let count = (ids[0] != u32::MAX) as u8 + (ids[1] != u32::MAX) as u8;
|
||||
Motion { ids, mv: [self.st.mv[0][raster], self.st.mv[1][raster]], count }
|
||||
}
|
||||
}
|
||||
|
||||
/// Same reference indices and mvs in both lists. Within one slice that means the same reference
|
||||
/// pictures, so bS is 0 without looking at the picture ids.
|
||||
#[inline(always)]
|
||||
fn same_motion(p: &MbState, rp: usize, q: &MbState, rq: usize) -> bool {
|
||||
let (bp, bq) = (b8_of(rp), b8_of(rq));
|
||||
p.ref_idx[0][bp] == q.ref_idx[0][bq] && p.ref_idx[1][bp] == q.ref_idx[1][bq] && p.mv[0][rp] == q.mv[0][rq] && p.mv[1][rp] == q.mv[1][rq]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mv_far(a: [i16; 2], b: [i16; 2]) -> bool {
|
||||
(a[0] as i32 - b[0] as i32).abs() >= 4 || (a[1] as i32 - b[1] as i32).abs() >= 4
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn motion_bs(p: &Motion, q: &Motion) -> u8 {
|
||||
if p.ids == q.ids && p.mv == q.mv {
|
||||
return 0;
|
||||
}
|
||||
if p.count != q.count {
|
||||
return 1;
|
||||
}
|
||||
if p.count == 1 {
|
||||
let (pi, pm) = if p.ids[0] != u32::MAX { (p.ids[0], p.mv[0]) } else { (p.ids[1], p.mv[1]) };
|
||||
let (qi, qm) = if q.ids[0] != u32::MAX { (q.ids[0], q.mv[0]) } else { (q.ids[1], q.mv[1]) };
|
||||
if pi != qi {
|
||||
return 1;
|
||||
}
|
||||
return mv_far(pm, qm) as u8;
|
||||
}
|
||||
if p.count == 0 {
|
||||
return 0;
|
||||
}
|
||||
// two motion vectors each
|
||||
let same_set = (p.ids[0] == q.ids[0] && p.ids[1] == q.ids[1]) || (p.ids[0] == q.ids[1] && p.ids[1] == q.ids[0]);
|
||||
if !same_set {
|
||||
return 1;
|
||||
}
|
||||
if p.ids[0] != p.ids[1] {
|
||||
// two different reference pictures: compare mvs referring to the same picture
|
||||
if p.ids[0] == q.ids[0] {
|
||||
(mv_far(p.mv[0], q.mv[0]) || mv_far(p.mv[1], q.mv[1])) as u8
|
||||
} else {
|
||||
(mv_far(p.mv[0], q.mv[1]) || mv_far(p.mv[1], q.mv[0])) as u8
|
||||
}
|
||||
} else {
|
||||
// both mvs refer to the same picture
|
||||
((mv_far(p.mv[0], q.mv[0]) || mv_far(p.mv[1], q.mv[1])) && (mv_far(p.mv[0], q.mv[1]) || mv_far(p.mv[1], q.mv[0]))) as u8
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn clip3(lo: i32, hi: i32, v: i32) -> i32 {
|
||||
v.clamp(lo, hi)
|
||||
}
|
||||
|
||||
/// Filter one set of eight samples p3 p2 p1 p0 | q0 q1 q2 q3 across an edge (8.7.2.3 / 8.7.2.4):
|
||||
/// the per-line reference for [`filter_lanes`].
|
||||
#[cfg(test)]
|
||||
fn filter8(v: &mut [u8; 8], bs: u8, alpha: i32, beta: i32, tc0: i32, chroma: bool) {
|
||||
let p0 = v[3] as i32;
|
||||
let q0 = v[4] as i32;
|
||||
let p1 = v[2] as i32;
|
||||
let q1 = v[5] as i32;
|
||||
if (p0 - q0).abs() >= alpha || (p1 - p0).abs() >= beta || (q1 - q0).abs() >= beta {
|
||||
return;
|
||||
}
|
||||
if chroma {
|
||||
if bs < 4 {
|
||||
let tc = tc0 + 1;
|
||||
let delta = clip3(-tc, tc, (((q0 - p0) << 2) + (p1 - q1) + 4) >> 3);
|
||||
v[3] = (p0 + delta).clamp(0, 255) as u8;
|
||||
v[4] = (q0 - delta).clamp(0, 255) as u8;
|
||||
} else {
|
||||
v[3] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
|
||||
v[4] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
|
||||
}
|
||||
return;
|
||||
}
|
||||
let p2 = v[1] as i32;
|
||||
let q2 = v[6] as i32;
|
||||
let ap = (p2 - p0).abs();
|
||||
let aq = (q2 - q0).abs();
|
||||
if bs < 4 {
|
||||
let tc = tc0 + (ap < beta) as i32 + (aq < beta) as i32;
|
||||
let delta = clip3(-tc, tc, (((q0 - p0) << 2) + (p1 - q1) + 4) >> 3);
|
||||
v[3] = (p0 + delta).clamp(0, 255) as u8;
|
||||
v[4] = (q0 - delta).clamp(0, 255) as u8;
|
||||
if ap < beta {
|
||||
v[2] = (p1 + clip3(-tc0, tc0, (p2 + ((p0 + q0 + 1) >> 1) - (p1 << 1)) >> 1)) as u8;
|
||||
}
|
||||
if aq < beta {
|
||||
v[5] = (q1 + clip3(-tc0, tc0, (q2 + ((p0 + q0 + 1) >> 1) - (q1 << 1)) >> 1)) as u8;
|
||||
}
|
||||
} else {
|
||||
let strong = (p0 - q0).abs() < ((alpha >> 2) + 2);
|
||||
if ap < beta && strong {
|
||||
let p3 = v[0] as i32;
|
||||
v[3] = ((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3) as u8;
|
||||
v[2] = ((p2 + p1 + p0 + q0 + 2) >> 2) as u8;
|
||||
v[1] = ((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3) as u8;
|
||||
} else {
|
||||
v[3] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
|
||||
}
|
||||
if aq < beta && strong {
|
||||
let q3 = v[7] as i32;
|
||||
v[4] = ((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3) as u8;
|
||||
v[5] = ((p0 + q0 + q1 + q2 + 2) >> 2) as u8;
|
||||
v[6] = ((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3) as u8;
|
||||
} else {
|
||||
v[4] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Edge filter parameters for `n` lines; `bs[i]` / `tc0[i]` apply to lines `i * n / 4 .. (i + 1) * n / 4`.
|
||||
struct EdgeParams {
|
||||
bs: [u8; 4],
|
||||
tc0: [i32; 4],
|
||||
alpha: i32,
|
||||
beta: i32,
|
||||
}
|
||||
|
||||
/// Filter one edge for `N` lines at once (lane `i` = line `i` across the edge). `s[k][i]` holds
|
||||
/// sample p3 p2 p1 p0 q0 q1 q2 q3 (k = 0..8) of line `i`. Branch-free per lane, so the loops
|
||||
/// vectorise; bit-exact with [`filter8`] applied to every line (8.7.2.3 / 8.7.2.4).
|
||||
#[inline(always)]
|
||||
fn filter_lanes<const N: usize>(s: &mut [[i16; N]; 8], ep: &EdgeParams, chroma: bool) {
|
||||
let per = N / 4;
|
||||
let (alpha, beta) = (ep.alpha as i16, ep.beta as i16);
|
||||
let mut bs_on = [false; N];
|
||||
let mut tc0 = [0i16; N];
|
||||
for k in 0..4 {
|
||||
for i in k * per..(k + 1) * per {
|
||||
bs_on[i] = ep.bs[k] != 0;
|
||||
tc0[i] = ep.tc0[k] as i16;
|
||||
}
|
||||
}
|
||||
let [p3, p2, p1, p0, q0, q1, q2, q3] = *s;
|
||||
// Masks are 0 / -1 per lane, selects are bitwise, and clipping to per-lane bounds uses
|
||||
// min / max (`clamp` would assert its bounds per lane), so every loop below vectorises.
|
||||
let lt = |x: i16, y: i16| -((x < y) as i16);
|
||||
let sel = |m: i16, a: i16, b: i16| (a & m) | (b & !m);
|
||||
let mut on = [0i16; N];
|
||||
let mut any = 0i16;
|
||||
for i in 0..N {
|
||||
on[i] = -(bs_on[i] as i16) & lt((p0[i] - q0[i]).abs(), alpha) & lt((p1[i] - p0[i]).abs(), beta) & lt((q1[i] - q0[i]).abs(), beta);
|
||||
any |= on[i];
|
||||
}
|
||||
if any == 0 {
|
||||
return;
|
||||
}
|
||||
// bS 4 is only ever assigned to all four segments of an edge
|
||||
let strong_edge = ep.bs[0] >= 4;
|
||||
if chroma {
|
||||
for i in 0..N {
|
||||
let (np0, nq0) = if strong_edge {
|
||||
((2 * p1[i] + p0[i] + q1[i] + 2) >> 2, (2 * q1[i] + q0[i] + p1[i] + 2) >> 2)
|
||||
} else {
|
||||
let tc = tc0[i] + 1;
|
||||
let delta = ((((q0[i] - p0[i]) << 2) + (p1[i] - q1[i]) + 4) >> 3).max(-tc).min(tc);
|
||||
((p0[i] + delta).clamp(0, 255), (q0[i] - delta).clamp(0, 255))
|
||||
};
|
||||
s[3][i] = sel(on[i], np0, p0[i]);
|
||||
s[4][i] = sel(on[i], nq0, q0[i]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if strong_edge {
|
||||
let lim = (alpha >> 2) + 2;
|
||||
for i in 0..N {
|
||||
let (a, b, c, d, e, f, g, h) = (p3[i], p2[i], p1[i], p0[i], q0[i], q1[i], q2[i], q3[i]);
|
||||
let strong = lt((d - e).abs(), lim);
|
||||
let sp = on[i] & lt((b - d).abs(), beta) & strong;
|
||||
let sq = on[i] & lt((g - e).abs(), beta) & strong;
|
||||
let wp0 = (2 * c + d + f + 2) >> 2;
|
||||
let wq0 = (2 * f + e + c + 2) >> 2;
|
||||
let sp0 = (b + 2 * c + 2 * d + 2 * e + f + 4) >> 3;
|
||||
let sq0 = (c + 2 * d + 2 * e + 2 * f + g + 4) >> 3;
|
||||
s[3][i] = sel(sp, sp0, sel(on[i], wp0, d));
|
||||
s[2][i] = sel(sp, (b + c + d + e + 2) >> 2, c);
|
||||
s[1][i] = sel(sp, (2 * a + 3 * b + c + d + e + 4) >> 3, b);
|
||||
s[4][i] = sel(sq, sq0, sel(on[i], wq0, e));
|
||||
s[5][i] = sel(sq, (d + e + f + g + 2) >> 2, f);
|
||||
s[6][i] = sel(sq, (2 * h + 3 * g + f + e + d + 4) >> 3, g);
|
||||
}
|
||||
} else {
|
||||
for i in 0..N {
|
||||
let ap = lt((p2[i] - p0[i]).abs(), beta);
|
||||
let aq = lt((q2[i] - q0[i]).abs(), beta);
|
||||
let t0 = tc0[i];
|
||||
let tc = t0 - ap - aq;
|
||||
let delta = ((((q0[i] - p0[i]) << 2) + (p1[i] - q1[i]) + 4) >> 3).max(-tc).min(tc);
|
||||
let avg = (p0[i] + q0[i] + 1) >> 1;
|
||||
let np1 = p1[i] + ((p2[i] + avg - (p1[i] << 1)) >> 1).max(-t0).min(t0);
|
||||
let nq1 = q1[i] + ((q2[i] + avg - (q1[i] << 1)) >> 1).max(-t0).min(t0);
|
||||
s[3][i] = sel(on[i], (p0[i] + delta).clamp(0, 255), p0[i]);
|
||||
s[4][i] = sel(on[i], (q0[i] - delta).clamp(0, 255), q0[i]);
|
||||
s[2][i] = sel(on[i] & ap, np1, p1[i]);
|
||||
s[5][i] = sel(on[i] & aq, nq1, q1[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Filter a vertical edge whose q0 column is at `x`, for `N` lines starting at row `y`.
|
||||
#[inline(always)]
|
||||
fn filter_vertical<const N: usize>(pix: &mut [u8], stride: usize, x: usize, y: usize, ep: &EdgeParams, chroma: bool) {
|
||||
// Each line's eight samples as one little-endian word: byte k = sample k (p3 .. q3).
|
||||
let mut w = [0u64; N];
|
||||
for (i, w) in w.iter_mut().enumerate() {
|
||||
let o = (y + i) * stride + x - 4;
|
||||
let Some(&b) = pix.get(o..).and_then(<[u8]>::first_chunk::<8>) else {
|
||||
return;
|
||||
};
|
||||
*w = u64::from_le_bytes(b);
|
||||
}
|
||||
let mut s = [[0i16; N]; 8];
|
||||
for (k, row) in s.iter_mut().enumerate() {
|
||||
for i in 0..N {
|
||||
row[i] = ((w[i] >> (8 * k)) & 0xff) as i16;
|
||||
}
|
||||
}
|
||||
filter_lanes(&mut s, ep, chroma);
|
||||
for (i, w) in w.iter_mut().enumerate() {
|
||||
*w = (0..8).fold(0u64, |acc, k| acc | ((s[k][i] as u8 as u64) << (8 * k)));
|
||||
}
|
||||
for (i, w) in w.iter().enumerate() {
|
||||
let o = (y + i) * stride + x - 4;
|
||||
pix[o..o + 8].copy_from_slice(&w.to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
/// Filter a horizontal edge whose q0 row is `y`, for `N` columns starting at `x`.
|
||||
#[inline(always)]
|
||||
fn filter_horizontal<const N: usize>(pix: &mut [u8], stride: usize, x: usize, y: usize, ep: &EdgeParams, chroma: bool) {
|
||||
let base = (y - 4) * stride + x;
|
||||
let mut s = [[0i16; N]; 8];
|
||||
for (k, row) in s.iter_mut().enumerate() {
|
||||
let Some(src) = pix.get(base + k * stride..).and_then(<[u8]>::first_chunk::<N>) else {
|
||||
return;
|
||||
};
|
||||
for i in 0..N {
|
||||
row[i] = src[i] as i16;
|
||||
}
|
||||
}
|
||||
filter_lanes(&mut s, ep, chroma);
|
||||
let (k0, k1) = if chroma { (3, 5) } else { (1, 7) };
|
||||
for (k, row) in s.iter().enumerate().take(k1).skip(k0) {
|
||||
let Some(dst) = pix.get_mut(base + k * stride..).and_then(<[u8]>::first_chunk_mut::<N>) else {
|
||||
return;
|
||||
};
|
||||
for i in 0..N {
|
||||
dst[i] = row[i] as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Boundary strengths `[dir][edge][segment]`; dir 0 = vertical edges (x), 1 = horizontal edges (y).
|
||||
type MbBs = [[[u8; 4]; 4]; 2];
|
||||
|
||||
/// Boundary strengths of an intra macroblock (8.7.2.1).
|
||||
fn intra_mb_bs(t8: bool, left: Option<usize>, top: Option<usize>) -> MbBs {
|
||||
let mut bs = [[[0u8; 4]; 4]; 2];
|
||||
for (dir, neighbor) in [left, top].into_iter().enumerate() {
|
||||
if neighbor.is_some() {
|
||||
bs[dir][0] = [4; 4];
|
||||
}
|
||||
for e in 1..4 {
|
||||
if !(t8 && e % 2 == 1) {
|
||||
bs[dir][e] = [3; 4];
|
||||
}
|
||||
}
|
||||
}
|
||||
bs
|
||||
}
|
||||
|
||||
/// Boundary strengths of the macroblock edge between neighbour `p` and the inter macroblock `q`.
|
||||
fn inter_outer_edge_bs(pic: &PicState, p: &MbState, q: &MbState, qm: &mut MbMotion, dir: usize) -> [u8; 4] {
|
||||
if p.kind.is_intra() {
|
||||
return [4; 4];
|
||||
}
|
||||
let same_slice = p.slice_num == q.slice_num;
|
||||
let mut pm = MbMotion::new(p, &pic.slices[p.slice_num as usize]);
|
||||
let mut out = [0u8; 4];
|
||||
for (k, o) in out.iter_mut().enumerate() {
|
||||
let (rq, rp) = if dir == 0 { (k * 4, k * 4 + 3) } else { (k, 12 + k) };
|
||||
*o = if (p.nz_mask >> rp) & 1 != 0 || (q.nz_mask >> rq) & 1 != 0 {
|
||||
2
|
||||
} else if same_slice && same_motion(p, rp, q, rq) {
|
||||
0
|
||||
} else {
|
||||
motion_bs(&pm.block(rp), &qm.block(rq))
|
||||
};
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Boundary strengths of internal edge `e` (1..4) of the inter macroblock `q`.
|
||||
fn inter_inner_edge_bs(q: &MbState, qm: &mut MbMotion, dir: usize, e: usize) -> [u8; 4] {
|
||||
let mut out = [0u8; 4];
|
||||
for (k, o) in out.iter_mut().enumerate() {
|
||||
let (rq, rp) = if dir == 0 { (k * 4 + e, k * 4 + e - 1) } else { (e * 4 + k, e * 4 + k - 4) };
|
||||
*o = if (q.nz_mask >> rp) & 1 != 0 || (q.nz_mask >> rq) & 1 != 0 {
|
||||
2
|
||||
} else if same_motion(q, rp, q, rq) {
|
||||
0
|
||||
} else {
|
||||
motion_bs(&qm.block(rp), &qm.block(rq))
|
||||
};
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Boundary strengths of an inter macroblock (8.7.2.1).
|
||||
fn inter_mb_bs(pic: &PicState, q: &MbState, sq: &SliceInfo, left: Option<usize>, top: Option<usize>) -> MbBs {
|
||||
let mut bs = [[[0u8; 4]; 4]; 2];
|
||||
let t8 = q.transform_8x8;
|
||||
let q_uniform = (0..2).all(|l| {
|
||||
let r = q.ref_idx[l];
|
||||
r[1] == r[0] && r[2] == r[0] && r[3] == r[0] && q.mv[l].iter().all(|m| *m == q.mv[l][0])
|
||||
});
|
||||
let mut qm = MbMotion::new(q, sq);
|
||||
for (dir, neighbor) in [left, top].into_iter().enumerate() {
|
||||
if let Some(n) = neighbor {
|
||||
bs[dir][0] = inter_outer_edge_bs(pic, &pic.mbs[n], q, &mut qm, dir);
|
||||
}
|
||||
if q_uniform && q.nz_mask == 0 {
|
||||
// one motion for the whole MB and no coefficients: all internal edges have bS 0
|
||||
continue;
|
||||
}
|
||||
for e in 1..4 {
|
||||
if !(t8 && e % 2 == 1) {
|
||||
bs[dir][e] = inter_inner_edge_bs(q, &mut qm, dir, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
bs
|
||||
}
|
||||
|
||||
/// Slice-level filter offsets (`FilterOffsetA` / `FilterOffsetB`).
|
||||
#[derive(Clone, Copy)]
|
||||
struct FilterOffsets {
|
||||
alpha: i32,
|
||||
beta: i32,
|
||||
}
|
||||
|
||||
/// Edge filter parameters for neighbouring QPs `qpp` / `qpq` (8.7.2.2).
|
||||
fn edge_params(qpp: i32, qpq: i32, b: [u8; 4], off: FilterOffsets) -> EdgeParams {
|
||||
let qpav = (qpp + qpq + 1) >> 1;
|
||||
let index_a = (qpav + off.alpha).clamp(0, 51) as usize;
|
||||
let index_b = (qpav + off.beta).clamp(0, 51) as usize;
|
||||
let tc = |b: u8| if (1..4).contains(&b) { TC0[index_a][b as usize - 1] as i32 } else { 0 };
|
||||
EdgeParams { bs: b, tc0: [tc(b[0]), tc(b[1]), tc(b[2]), tc(b[3])], alpha: ALPHA[index_a] as i32, beta: BETA[index_b] as i32 }
|
||||
}
|
||||
|
||||
/// Everything the per-plane edge filters need to know about one macroblock.
|
||||
struct MbEdges {
|
||||
bs: MbBs,
|
||||
offsets: FilterOffsets,
|
||||
mx: usize,
|
||||
my: usize,
|
||||
q_qp: i32,
|
||||
q_qpc: [u8; 2],
|
||||
/// (luma QP, chroma QPs) of the left and top neighbours.
|
||||
p_qp: [Option<(i32, [u8; 2])>; 2],
|
||||
}
|
||||
|
||||
/// Filter the luma edges of one macroblock.
|
||||
fn filter_luma_edges(planes: &mut Planes, m: &MbEdges) {
|
||||
let width = planes.width;
|
||||
for dir in 0..2 {
|
||||
for e in 0..4 {
|
||||
if m.bs[dir][e] == [0; 4] {
|
||||
continue;
|
||||
}
|
||||
let qpp = if e == 0 { m.p_qp[dir].map(|p| p.0).unwrap_or(m.q_qp) } else { m.q_qp };
|
||||
let ep = edge_params(qpp, m.q_qp, m.bs[dir][e], m.offsets);
|
||||
if ep.alpha == 0 || ep.beta == 0 {
|
||||
// filterSamplesFlag needs |p0 - q0| < alpha and |p1 - p0| < beta (8.7.2.2): no
|
||||
// sample of the edge changes (low QPs)
|
||||
continue;
|
||||
}
|
||||
if dir == 0 {
|
||||
filter_vertical::<16>(&mut planes.y, width, m.mx * 16 + e * 4, m.my * 16, &ep, false);
|
||||
} else {
|
||||
filter_horizontal::<16>(&mut planes.y, width, m.mx * 16, m.my * 16 + e * 4, &ep, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Filter the chroma (4:2:0) edges of one macroblock: edges at chroma samples 0 and 4 use the bS of
|
||||
/// luma edges 0 and 2.
|
||||
fn filter_chroma_edges(planes: &mut Planes, m: &MbEdges) {
|
||||
let cwidth = planes.cwidth;
|
||||
for c in 0..2 {
|
||||
for dir in 0..2 {
|
||||
for ce in 0..2 {
|
||||
let e = ce * 2;
|
||||
if m.bs[dir][e] == [0; 4] {
|
||||
continue;
|
||||
}
|
||||
let qpp = if e == 0 { m.p_qp[dir].map(|p| p.1[c]).unwrap_or(m.q_qpc[c]) } else { m.q_qpc[c] } as i32;
|
||||
let ep = edge_params(qpp, m.q_qpc[c] as i32, m.bs[dir][e], m.offsets);
|
||||
if ep.alpha == 0 || ep.beta == 0 {
|
||||
continue;
|
||||
}
|
||||
let plane = if c == 0 { &mut planes.cb } else { &mut planes.cr };
|
||||
if dir == 0 {
|
||||
filter_vertical::<8>(plane, cwidth, m.mx * 8 + ce * 4, m.my * 8, &ep, true);
|
||||
} else {
|
||||
filter_horizontal::<8>(plane, cwidth, m.mx * 8, m.my * 8 + ce * 4, &ep, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Deblock one macroblock (macroblocks must be processed in raster order).
|
||||
pub fn deblock_mb(pic: &mut PicState, addr: usize, mb_w: usize) {
|
||||
let q = &pic.mbs[addr];
|
||||
if q.slice_num == u32::MAX {
|
||||
return;
|
||||
}
|
||||
let sq = &pic.slices[q.slice_num as usize];
|
||||
if sq.disable_deblocking_filter_idc == 1 {
|
||||
return;
|
||||
}
|
||||
let (mx, my) = (addr % mb_w, addr / mb_w);
|
||||
let usable = |n: Option<usize>| -> Option<usize> {
|
||||
let n = n?;
|
||||
let st = &pic.mbs[n];
|
||||
if st.slice_num == u32::MAX || (sq.disable_deblocking_filter_idc == 2 && st.slice_num != q.slice_num) {
|
||||
return None;
|
||||
}
|
||||
Some(n)
|
||||
};
|
||||
let left = usable(if mx > 0 { Some(addr - 1) } else { None });
|
||||
let top = usable(if my > 0 { Some(addr - mb_w) } else { None });
|
||||
let bs = if q.kind.is_intra() { intra_mb_bs(q.transform_8x8, left, top) } else { inter_mb_bs(pic, q, sq, left, top) };
|
||||
let qp_of = |st: &MbState| if st.kind == MbKind::IPcm { 0 } else { st.qp as i32 };
|
||||
let neighbor_qp = |n: Option<usize>| n.map(|n| (qp_of(&pic.mbs[n]), pic.mbs[n].qpc));
|
||||
let edges = MbEdges {
|
||||
bs,
|
||||
offsets: FilterOffsets { alpha: sq.alpha_offset, beta: sq.beta_offset },
|
||||
mx,
|
||||
my,
|
||||
q_qp: qp_of(q),
|
||||
q_qpc: q.qpc,
|
||||
p_qp: [neighbor_qp(left), neighbor_qp(top)],
|
||||
};
|
||||
filter_luma_edges(&mut pic.planes, &edges);
|
||||
filter_chroma_edges(&mut pic.planes, &edges);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The vectorised edge filter equals the per-line reference [`filter8`] on random edges
|
||||
/// (small differences across the edge so that every filter branch is taken).
|
||||
#[test]
|
||||
fn lane_filter_matches_per_line_reference() {
|
||||
let mut rng = 0x9e37_79b9_7f4a_7c15u64;
|
||||
let mut next = move || {
|
||||
rng ^= rng << 13;
|
||||
rng ^= rng >> 7;
|
||||
rng ^= rng << 17;
|
||||
rng
|
||||
};
|
||||
for iter in 0..20_000 {
|
||||
let chroma = iter % 3 == 0;
|
||||
let strong = iter % 4 == 1;
|
||||
let bs: [u8; 4] = if strong { [4; 4] } else { std::array::from_fn(|_| (next() % 4) as u8) };
|
||||
let index_a = (next() % 52) as usize;
|
||||
let index_b = (next() % 52) as usize;
|
||||
let tc = |b: u8| if (1..4).contains(&b) { TC0[index_a][b as usize - 1] as i32 } else { 0 };
|
||||
let ep = EdgeParams { bs, tc0: bs.map(tc), alpha: ALPHA[index_a] as i32, beta: BETA[index_b] as i32 };
|
||||
let base = (next() % 256) as i32;
|
||||
let spread = 1 + (next() % 24) as i32;
|
||||
let mut lines = [[0u8; 8]; 16];
|
||||
for l in lines.iter_mut() {
|
||||
for v in l.iter_mut() {
|
||||
*v = (base + (next() % (2 * spread as u64 + 1)) as i32 - spread).clamp(0, 255) as u8;
|
||||
}
|
||||
}
|
||||
let n = if chroma { 8 } else { 16 };
|
||||
let mut want = lines;
|
||||
for (i, l) in want.iter_mut().enumerate().take(n) {
|
||||
let k = i / (n / 4);
|
||||
if bs[k] != 0 {
|
||||
filter8(l, bs[k], ep.alpha, ep.beta, ep.tc0[k], chroma);
|
||||
}
|
||||
}
|
||||
let got: Vec<[u8; 8]> = if chroma {
|
||||
let mut s = [[0i16; 8]; 8];
|
||||
for i in 0..8 {
|
||||
for k in 0..8 {
|
||||
s[k][i] = lines[i][k] as i16;
|
||||
}
|
||||
}
|
||||
filter_lanes(&mut s, &ep, true);
|
||||
(0..8).map(|i| std::array::from_fn(|k| s[k][i] as u8)).collect()
|
||||
} else {
|
||||
let mut s = [[0i16; 16]; 8];
|
||||
for i in 0..16 {
|
||||
for k in 0..8 {
|
||||
s[k][i] = lines[i][k] as i16;
|
||||
}
|
||||
}
|
||||
filter_lanes(&mut s, &ep, false);
|
||||
(0..16).map(|i| std::array::from_fn(|k| s[k][i] as u8)).collect()
|
||||
};
|
||||
assert_eq!(&got[..], &want[..n], "iteration {iter}: bs {bs:?} indexA {index_a} indexB {index_b}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod speed {
|
||||
use super::*;
|
||||
|
||||
/// Rough timing of the edge filters (`cargo test --release -p craft-video --features h264-decode,threads --lib edge_filter_speed -- --ignored --nocapture`).
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn edge_filter_speed() {
|
||||
let (w, h) = (1024usize, 256usize);
|
||||
let mut pix: Vec<u8> = (0..w * h).map(|i| (128 + ((i * 7919) % 13) as i32 - 6) as u8).collect();
|
||||
let eps = [
|
||||
EdgeParams { bs: [4; 4], tc0: [0; 4], alpha: 40, beta: 9 },
|
||||
EdgeParams { bs: [3; 4], tc0: [2; 4], alpha: 40, beta: 9 },
|
||||
EdgeParams { bs: [2, 1, 0, 2], tc0: [1, 1, 0, 1], alpha: 40, beta: 9 },
|
||||
];
|
||||
let t0 = std::time::Instant::now();
|
||||
let mut n = 0;
|
||||
for _ in 0..20 {
|
||||
for my in 1..h / 16 {
|
||||
for mx in 1..w / 16 {
|
||||
for (j, ep) in eps.iter().enumerate() {
|
||||
filter_horizontal::<16>(&mut pix, w, mx * 16, my * 16 + j * 4, ep, false);
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let dt = t0.elapsed();
|
||||
let tv = std::time::Instant::now();
|
||||
for _ in 0..20 {
|
||||
for my in 1..h / 16 {
|
||||
for mx in 1..w / 16 {
|
||||
for (j, ep) in eps.iter().enumerate() {
|
||||
filter_vertical::<16>(&mut pix, w, mx * 16 + j * 4, my * 16, ep, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("vertical: {:.1} ns/edge", tv.elapsed().as_nanos() as f64 / n as f64);
|
||||
let t1 = std::time::Instant::now();
|
||||
for _ in 0..20 {
|
||||
for my in 1..h / 16 {
|
||||
for mx in 1..w / 16 {
|
||||
for ep in eps.iter() {
|
||||
for (vert, x, y) in [(true, mx * 16, my * 16), (false, mx * 16, my * 16)] {
|
||||
for i in 0..16 {
|
||||
let b = ep.bs[i / 4];
|
||||
if b == 0 {
|
||||
continue;
|
||||
}
|
||||
let mut v = [0u8; 8];
|
||||
for k in 0..8 {
|
||||
v[k] = if vert { pix[(y + i) * w + x - 4 + k] } else { pix[(y - 4 + k) * w + x + i] };
|
||||
}
|
||||
filter8(&mut v, b, ep.alpha, ep.beta, ep.tc0[i / 4], false);
|
||||
for k in 1..7 {
|
||||
if vert {
|
||||
pix[(y + i) * w + x - 4 + k] = v[k];
|
||||
} else {
|
||||
pix[(y - 4 + k) * w + x + i] = v[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("scalar reference (both): {:.1} ns/edge", t1.elapsed().as_nanos() as f64 / (2 * n) as f64);
|
||||
println!(
|
||||
"horizontal: {n} luma edges in {dt:?}: {:.1} ns/edge (checksum {})",
|
||||
dt.as_nanos() as f64 / n as f64,
|
||||
pix.iter().map(|&v| v as u64).sum::<u64>()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,757 @@
|
||||
//! Top-level decoder: NAL dispatch, picture boundaries, POC, DPB and output.
|
||||
//!
|
||||
//! The calling thread parses headers and does all DPB / reference list bookkeeping (which only needs
|
||||
//! header information). Macroblock decoding of each picture runs as a job — on a thread pool with the
|
||||
//! `threads` feature — that publishes finished macroblock rows into the picture's shared [`Frame`];
|
||||
//! jobs of later pictures block per row on the reference data they need, so several pictures decode
|
||||
//! concurrently (frame-level parallelism).
|
||||
|
||||
use crate::h264::decode::dpb::{Dpb, Output, OutputMeta, RefMark};
|
||||
use crate::h264::decode::error::{Error, Result, ensure, invalid, unsupported};
|
||||
use crate::h264::decode::params::{Pps, Sps};
|
||||
use crate::h264::decode::picture::{Frame, FrameRef, MbKind, MbState, Planes, RefPic};
|
||||
use crate::h264::decode::slice::{NalHeader, Poc, PocState, SliceHeader, SliceType, is_new_picture, nal_type};
|
||||
use crate::h264::decode::slicedec::{PicState, SliceDecoder};
|
||||
use crate::h264::decode::transform::LevelScale;
|
||||
use crate::h264::decode::{ColorInfo, Picture};
|
||||
use craft_media::bitstream::{BitReader, annexb_nals, length_prefixed_nals, unescape_rbsp};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
/// Most pictures one `decode` call may start: a chunk is an access unit, or a few in a transport
|
||||
/// stream packet. Each picture is a full frame, and small slices make them cheap to ask for.
|
||||
const MAX_PICTURES_PER_CALL: usize = 64;
|
||||
|
||||
/// The decoder supports at most 16 reference pictures, so constructing more
|
||||
/// inferred gap pictures cannot improve a usable reference list.
|
||||
const MAX_INFERRED_GAP_FRAMES: u32 = 16;
|
||||
|
||||
/// One slice of a picture job.
|
||||
struct SliceJob {
|
||||
sh: SliceHeader,
|
||||
pps: Arc<Pps>,
|
||||
sps: Arc<Sps>,
|
||||
rbsp: Vec<u8>,
|
||||
refs: [Vec<RefPic>; 2],
|
||||
ls: Arc<LevelScale>,
|
||||
}
|
||||
|
||||
/// A picture whose slices are being collected.
|
||||
struct PendingPic {
|
||||
frame: FrameRef,
|
||||
sps: Arc<Sps>,
|
||||
first: SliceHeader,
|
||||
poc: Poc,
|
||||
pts: i64,
|
||||
key: bool,
|
||||
slices: Vec<SliceJob>,
|
||||
}
|
||||
|
||||
/// Counters describing what the decoder has seen (useful to check test coverage).
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct DecodeStats {
|
||||
pub pictures: u64,
|
||||
pub slices_cavlc: u64,
|
||||
pub slices_cabac: u64,
|
||||
pub slices_i: u64,
|
||||
pub slices_p: u64,
|
||||
pub slices_b: u64,
|
||||
pub mb_i4x4: u64,
|
||||
pub mb_i8x8: u64,
|
||||
pub mb_i16x16: u64,
|
||||
pub mb_pcm: u64,
|
||||
pub mb_p_skip: u64,
|
||||
pub mb_b_skip: u64,
|
||||
pub mb_b_direct16x16: u64,
|
||||
pub mb_inter: u64,
|
||||
/// Inter macroblocks using the 8x8 transform.
|
||||
pub mb_inter_8x8_transform: u64,
|
||||
pub mmco_ops: u64,
|
||||
pub long_term_marks: u64,
|
||||
pub frame_num_gaps: u64,
|
||||
pub weighted_slices: u64,
|
||||
pub temporal_direct_slices: u64,
|
||||
/// Non-reference pictures decoded in draft mode (deblocking skipped).
|
||||
pub draft_pictures: u64,
|
||||
}
|
||||
|
||||
/// State shared with decoding jobs.
|
||||
#[derive(Default)]
|
||||
struct Shared {
|
||||
error: Mutex<Option<Error>>,
|
||||
stats: Mutex<DecodeStats>,
|
||||
/// Recycled picture buffers.
|
||||
pool: Mutex<Vec<PicState>>,
|
||||
/// Set by [`Decoder::reset`]: jobs still running stop at the next macroblock row.
|
||||
cancel: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
/// H.264 decoder.
|
||||
pub struct Decoder {
|
||||
spss: Vec<Option<Arc<Sps>>>,
|
||||
ppss: Vec<Option<Arc<Pps>>>,
|
||||
nal_length_size: Option<usize>,
|
||||
dpb: Dpb<FrameRef>,
|
||||
poc_state: PocState,
|
||||
prev_ref_frame_num: u32,
|
||||
/// Pictures started by the current `decode` call (see [`MAX_PICTURES_PER_CALL`]).
|
||||
call_pictures: usize,
|
||||
pending: Option<PendingPic>,
|
||||
next_id: u32,
|
||||
active_sps: Option<Arc<Sps>>,
|
||||
ls_cache: Vec<(Arc<Pps>, Arc<LevelScale>)>,
|
||||
/// Pictures leaving the DPB, converted once their decoding job has finished.
|
||||
out_queue: VecDeque<Output<FrameRef>>,
|
||||
/// Queued entries per frame id and their summed bytes, kept in step with `out_queue` so the
|
||||
/// budget check does not rescan it.
|
||||
queued_ids: HashMap<u32, u64>,
|
||||
queued_bytes: u64,
|
||||
shared: Arc<Shared>,
|
||||
#[cfg(feature = "threads")]
|
||||
pool: Option<rayon::ThreadPool>,
|
||||
in_flight: VecDeque<FrameRef>,
|
||||
max_in_flight: usize,
|
||||
/// Bytes the picture store may take ([`crate::budget`]).
|
||||
memory_budget: u64,
|
||||
threads: usize,
|
||||
/// Draft mode: skip deblocking of non-reference pictures ([`Decoder::set_draft`]).
|
||||
draft: bool,
|
||||
}
|
||||
|
||||
impl Default for Decoder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// The worker threads [`Decoder::new`] uses: the available cores (at most 16) with the `threads`
|
||||
/// feature on native targets, 1 otherwise.
|
||||
pub fn default_threads() -> usize {
|
||||
#[cfg(all(feature = "threads", not(target_arch = "wasm32")))]
|
||||
{
|
||||
std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1).min(16)
|
||||
}
|
||||
#[cfg(not(all(feature = "threads", not(target_arch = "wasm32"))))]
|
||||
{
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
impl Decoder {
|
||||
/// A decoder using all available cores (with the `threads` feature).
|
||||
pub fn new() -> Self {
|
||||
Self::with_threads(default_threads())
|
||||
}
|
||||
|
||||
/// A decoder using up to `threads` worker threads (1 = decode on the calling thread).
|
||||
pub fn with_threads(threads: usize) -> Self {
|
||||
crate::h264::decode::cavlc::init_tables();
|
||||
#[cfg(feature = "threads")]
|
||||
let pool = if threads > 1 && cfg!(not(target_arch = "wasm32")) {
|
||||
rayon::ThreadPoolBuilder::new().num_threads(threads).thread_name(|i| format!("h264-{i}")).build().ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
#[cfg(feature = "threads")]
|
||||
let workers = if pool.is_some() { threads } else { 1 };
|
||||
#[cfg(not(feature = "threads"))]
|
||||
let workers = 1;
|
||||
Decoder {
|
||||
spss: vec![None; 32],
|
||||
ppss: vec![None; 256],
|
||||
nal_length_size: None,
|
||||
dpb: Dpb::new(),
|
||||
poc_state: PocState::default(),
|
||||
prev_ref_frame_num: 0,
|
||||
call_pictures: 0,
|
||||
pending: None,
|
||||
next_id: 1,
|
||||
active_sps: None,
|
||||
ls_cache: Vec::new(),
|
||||
out_queue: VecDeque::new(),
|
||||
queued_ids: HashMap::new(),
|
||||
queued_bytes: 0,
|
||||
shared: Arc::new(Shared::default()),
|
||||
#[cfg(feature = "threads")]
|
||||
pool,
|
||||
in_flight: VecDeque::new(),
|
||||
max_in_flight: threads.max(1) + 2,
|
||||
memory_budget: crate::budget::DECODE_MEMORY_BUDGET,
|
||||
threads: workers,
|
||||
draft: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Worker threads decoding pictures (1: on the calling thread).
|
||||
pub fn threads(&self) -> usize {
|
||||
self.threads
|
||||
}
|
||||
|
||||
/// Draft mode (off by default) for reduced-resolution playback: pictures submitted from now
|
||||
/// on that are non-reference pictures (`nal_ref_idc` 0) skip the deblocking filter (8.7).
|
||||
/// Nothing predicts from a non-reference picture (and deblocking never changes the motion
|
||||
/// data used for direct prediction), so every other picture stays bit-exact; the draft
|
||||
/// pictures themselves are approximate and flagged [`Picture::draft`]. Never use it for
|
||||
/// export or for a frame shown while paused.
|
||||
pub fn set_draft(&mut self, on: bool) {
|
||||
self.draft = on;
|
||||
}
|
||||
|
||||
pub fn draft(&self) -> bool {
|
||||
self.draft
|
||||
}
|
||||
|
||||
/// Forget the stream (parameter sets, `avcC` configuration, reference pictures and pending
|
||||
/// output) as if the decoder were new, e.g. to seek. Unlike making a new decoder, this keeps the
|
||||
/// worker threads, the draft setting and the recycled picture buffers, and the pictures still
|
||||
/// being decoded stop at their next macroblock row instead of running to the end.
|
||||
pub fn reset(&mut self) {
|
||||
self.shared.cancel.store(true, Ordering::Relaxed);
|
||||
let pool = self.shared.pool.lock().map(|mut p| std::mem::take(&mut *p)).unwrap_or_default();
|
||||
self.shared = Arc::new(Shared { pool: Mutex::new(pool), ..Shared::default() });
|
||||
self.spss.fill(None);
|
||||
self.ppss.fill(None);
|
||||
self.nal_length_size = None;
|
||||
self.dpb = Dpb::new();
|
||||
self.poc_state = PocState::default();
|
||||
self.prev_ref_frame_num = 0;
|
||||
self.pending = None;
|
||||
self.active_sps = None;
|
||||
self.ls_cache.clear();
|
||||
self.out_queue.clear();
|
||||
self.queued_ids.clear();
|
||||
self.queued_bytes = 0;
|
||||
}
|
||||
|
||||
/// Configure from an `avcC` (AVCDecoderConfigurationRecord) box payload.
|
||||
pub fn from_avcc(avcc: &[u8]) -> Result<Self> {
|
||||
let mut d = Decoder::new();
|
||||
d.configure_avcc(avcc)?;
|
||||
Ok(d)
|
||||
}
|
||||
|
||||
/// Parse an `avcC` record: SPS/PPS and the NAL length size used by [`Decoder::decode`].
|
||||
pub fn configure_avcc(&mut self, avcc: &[u8]) -> Result<()> {
|
||||
ensure!(avcc.len() >= 7, "avcC too short");
|
||||
ensure!(avcc[0] == 1, "unsupported avcC version {}", avcc[0]);
|
||||
let mut pos = 5;
|
||||
let mut sets = Vec::new();
|
||||
let read_sets = |pos: &mut usize, count: usize, sets: &mut Vec<(usize, usize)>| -> Result<()> {
|
||||
for _ in 0..count {
|
||||
ensure!(*pos + 2 <= avcc.len(), "avcC truncated");
|
||||
let len = u16::from_be_bytes([avcc[*pos], avcc[*pos + 1]]) as usize;
|
||||
*pos += 2;
|
||||
ensure!(*pos + len <= avcc.len(), "avcC truncated");
|
||||
sets.push((*pos, len));
|
||||
*pos += len;
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
let nsps = (avcc[pos] & 0x1f) as usize;
|
||||
pos += 1;
|
||||
read_sets(&mut pos, nsps, &mut sets)?;
|
||||
ensure!(pos < avcc.len(), "avcC truncated");
|
||||
let npps = avcc[pos] as usize;
|
||||
pos += 1;
|
||||
read_sets(&mut pos, npps, &mut sets)?;
|
||||
// Every entry is checked before any is handled: a record that carries anything but an
|
||||
// SPS in its first list and a PPS in its second (a picture slice, say) starts no picture.
|
||||
for (i, (start, len)) in sets.iter().enumerate() {
|
||||
let nal = &avcc[*start..*start + *len];
|
||||
let header = nal.first().copied().ok_or_else(|| Error::Invalid("empty avcC parameter set".into()))?;
|
||||
let expected = if i < nsps { nal_type::SPS } else { nal_type::PPS };
|
||||
ensure!(header & 0x1f == expected, "avcC parameter set has NAL type {}, expected {expected}", header & 0x1f);
|
||||
}
|
||||
for (start, len) in sets {
|
||||
self.handle_nal(&avcc[start..start + len], 0)?;
|
||||
}
|
||||
self.nal_length_size = Some((avcc[4] & 3) as usize + 1);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Length of NAL length prefixes (from avcC), or None for Annex-B input.
|
||||
pub fn nal_length_size(&self) -> Option<usize> {
|
||||
self.nal_length_size
|
||||
}
|
||||
|
||||
/// Decode one access unit (all NAL units of one picture, Annex-B or length-prefixed per
|
||||
/// configuration; a byte stream with several complete access units is accepted too). Returns
|
||||
/// pictures that became ready for output, in output order; `pts` travels with the picture of
|
||||
/// this access unit through reordering.
|
||||
pub fn decode(&mut self, data: &[u8], pts: i64) -> Result<Vec<Picture>> {
|
||||
let nals = match self.nal_length_size {
|
||||
Some(n) => length_prefixed_nals(data, n)?,
|
||||
None => annexb_nals(data),
|
||||
};
|
||||
self.call_pictures = 0;
|
||||
let mut result = Ok(());
|
||||
for nal in nals {
|
||||
if let Err(e) = self.handle_nal(nal, pts) {
|
||||
result = Err(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// The access unit is complete: hand its picture to a decoding job.
|
||||
self.submit_pending()?;
|
||||
if let Some(e) = self.take_error() {
|
||||
result = result.and(Err(e));
|
||||
}
|
||||
result?;
|
||||
// Return finished pictures without blocking on frames still being decoded (bounded latency).
|
||||
let max_queue = self.max_in_flight * 2;
|
||||
let mut pics = Vec::new();
|
||||
while let Some(o) = self.out_queue.front() {
|
||||
if !o.frame.is_published(o.frame.mb_h() - 1) && self.out_queue.len() <= max_queue {
|
||||
break;
|
||||
}
|
||||
let Some(o) = self.pop_out() else { break };
|
||||
pics.push(make_picture(&o));
|
||||
}
|
||||
Ok(pics)
|
||||
}
|
||||
|
||||
/// Output all remaining pictures (end of stream).
|
||||
pub fn flush(&mut self) -> Vec<Picture> {
|
||||
let _ = self.submit_pending();
|
||||
let mut outs = Vec::new();
|
||||
self.dpb.flush(&mut outs);
|
||||
self.emit(outs);
|
||||
let pics = std::iter::from_fn(|| self.pop_out()).map(|o| make_picture(&o)).collect();
|
||||
while let Some(f) = self.in_flight.pop_front() {
|
||||
f.wait_complete();
|
||||
}
|
||||
pics
|
||||
}
|
||||
|
||||
/// Statistics accumulated so far (macroblock counts include finished pictures only).
|
||||
pub fn stats(&self) -> DecodeStats {
|
||||
self.shared.stats.lock().map(|s| s.clone()).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn stat(&self, f: impl FnOnce(&mut DecodeStats)) {
|
||||
if let Ok(mut s) = self.shared.stats.lock() {
|
||||
f(&mut s);
|
||||
}
|
||||
}
|
||||
|
||||
/// First error reported by a decoding job since the last call (also returned by
|
||||
/// [`Decoder::decode`]); useful after [`Decoder::flush`].
|
||||
pub fn take_error(&mut self) -> Option<Error> {
|
||||
self.shared.error.lock().ok().and_then(|mut e| e.take())
|
||||
}
|
||||
|
||||
fn handle_nal(&mut self, nal: &[u8], pts: i64) -> Result<()> {
|
||||
if nal.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let hdr = NalHeader::parse(nal[0])?;
|
||||
match hdr.nal_unit_type {
|
||||
nal_type::SPS => {
|
||||
let sps = Sps::parse(&unescape_rbsp(&nal[1..]))?;
|
||||
let id = sps.id as usize;
|
||||
self.spss[id] = Some(Arc::new(sps));
|
||||
}
|
||||
nal_type::PPS => {
|
||||
let rbsp = unescape_rbsp(&nal[1..]);
|
||||
let spss: Vec<Option<Sps>> = self.spss.iter().map(|s| s.as_ref().map(|s| (**s).clone())).collect();
|
||||
let pps = Pps::parse(&rbsp, &spss)?;
|
||||
let id = pps.id as usize;
|
||||
self.ppss[id] = Some(Arc::new(pps));
|
||||
}
|
||||
nal_type::SLICE | nal_type::IDR => self.handle_slice(nal, hdr, pts)?,
|
||||
nal_type::SLICE_DPA | nal_type::SLICE_DPB | nal_type::SLICE_DPC => {
|
||||
return unsupported("data partitioning (Extended profile)");
|
||||
}
|
||||
nal_type::END_SEQ | nal_type::END_STREAM => self.submit_pending()?,
|
||||
// SEI, AUD, filler, SPS extension, prefix NAL, subset SPS, auxiliary and extension slices: skipped.
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn level_scale(&mut self, pps: &Arc<Pps>) -> Arc<LevelScale> {
|
||||
if let Some((_, ls)) = self.ls_cache.iter().find(|(p, _)| Arc::ptr_eq(p, pps)) {
|
||||
return ls.clone();
|
||||
}
|
||||
let ls: Arc<LevelScale> = Arc::from(LevelScale::new(&pps.scaling));
|
||||
self.ls_cache.retain(|(p, _)| self.ppss.iter().flatten().any(|q| Arc::ptr_eq(p, q)));
|
||||
self.ls_cache.push((pps.clone(), ls.clone()));
|
||||
ls
|
||||
}
|
||||
|
||||
fn handle_slice(&mut self, nal: &[u8], hdr: NalHeader, pts: i64) -> Result<()> {
|
||||
let rbsp = unescape_rbsp(&nal[1..]);
|
||||
let ppss = &self.ppss;
|
||||
let spss = &self.spss;
|
||||
let mut found: Option<(Arc<Pps>, Arc<Sps>)> = None;
|
||||
let (sh, _, _) = SliceHeader::parse(&rbsp, hdr, |id| {
|
||||
let pps = ppss[id as usize].as_ref().ok_or_else(|| Error::MissingParameterSet(format!("PPS {id}")))?;
|
||||
let sps = spss[pps.sps_id as usize].as_ref().ok_or_else(|| Error::MissingParameterSet(format!("SPS {}", pps.sps_id)))?;
|
||||
// Before the header fields that depend on the SPS: they are validated for the streams
|
||||
// we decode (a 10-bit slice may have a QP below 0), so a stream we cannot decode must
|
||||
// fail as unsupported here, not as invalid there (#626).
|
||||
sps.check_supported()?;
|
||||
found = Some((pps.clone(), sps.clone()));
|
||||
Ok((&**pps, &**sps))
|
||||
})?;
|
||||
let Some((pps, sps)) = found else {
|
||||
return Err(Error::MissingParameterSet("PPS".into()));
|
||||
};
|
||||
if pps.num_slice_groups > 1 {
|
||||
return unsupported("slice groups (FMO)");
|
||||
}
|
||||
if sh.field_pic {
|
||||
return unsupported("field pictures");
|
||||
}
|
||||
if matches!(sh.slice_type, SliceType::Sp | SliceType::Si) {
|
||||
return unsupported("SP/SI slices");
|
||||
}
|
||||
if sh.redundant_pic_cnt > 0 {
|
||||
return Ok(()); // redundant slices are ignored
|
||||
}
|
||||
let new_pic = match &self.pending {
|
||||
None => true,
|
||||
Some(p) => is_new_picture(&p.first, &sh, &sps) || sh.first_mb_in_slice == 0 || !Arc::ptr_eq(&p.sps, &sps),
|
||||
};
|
||||
if new_pic {
|
||||
self.submit_pending()?;
|
||||
if sh.first_mb_in_slice != 0 && self.active_sps.is_none() {
|
||||
return invalid("stream does not start with the first slice of a picture");
|
||||
}
|
||||
self.start_picture(&sh, &sps, pts)?;
|
||||
}
|
||||
let ls = self.level_scale(&pps);
|
||||
let Some(pending) = self.pending.as_mut() else {
|
||||
return invalid("slice without a started picture");
|
||||
};
|
||||
let refs = self.dpb.build_ref_lists(&sh, pending.poc.frame(), sps.max_frame_num(), |e| RefPic {
|
||||
frame: e.frame.clone(),
|
||||
long_term: e.mark == RefMark::Long,
|
||||
})?;
|
||||
if !sh.slice_type.is_intra() {
|
||||
ensure!(!refs[0].is_empty(), "no reference pictures available for inter slice");
|
||||
if sh.slice_type.is_b() {
|
||||
ensure!(!refs[1].is_empty(), "empty RefPicList1 in B slice");
|
||||
}
|
||||
}
|
||||
let weighted = (pps.weighted_pred && sh.slice_type.is_p()) || (pps.weighted_bipred_idc != 0 && sh.slice_type.is_b());
|
||||
let temporal = sh.slice_type.is_b() && !sh.direct_spatial_mv_pred;
|
||||
let (cabac, st, mmcos, lt) = (
|
||||
pps.entropy_coding_mode,
|
||||
sh.slice_type,
|
||||
sh.mmcos.len() as u64,
|
||||
sh.long_term_reference as u64 + sh.mmcos.iter().filter(|m| m.op == 3 || m.op == 6).count() as u64,
|
||||
);
|
||||
pending.slices.push(SliceJob { sh, pps, sps, rbsp, refs, ls });
|
||||
self.stat(|s| {
|
||||
if cabac {
|
||||
s.slices_cabac += 1;
|
||||
} else {
|
||||
s.slices_cavlc += 1;
|
||||
}
|
||||
match st {
|
||||
SliceType::I | SliceType::Si => s.slices_i += 1,
|
||||
SliceType::P | SliceType::Sp => s.slices_p += 1,
|
||||
SliceType::B => s.slices_b += 1,
|
||||
}
|
||||
s.weighted_slices += weighted as u64;
|
||||
s.temporal_direct_slices += temporal as u64;
|
||||
s.mmco_ops += mmcos;
|
||||
s.long_term_marks += lt;
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Cap the bytes of decoded pictures (current, references, in flight) this decoder may hold.
|
||||
pub fn set_memory_budget(&mut self, bytes: u64) {
|
||||
self.memory_budget = bytes;
|
||||
}
|
||||
|
||||
/// Refuse an SPS whose pictures would not fit the memory budget, before anything is allocated.
|
||||
fn check_budget(&self, sps: &Sps) -> Result<()> {
|
||||
let chroma = sps.chroma_array_type();
|
||||
let shape = crate::budget::PictureShape {
|
||||
width: u64::from(sps.width()),
|
||||
height: u64::from(sps.height()),
|
||||
shift_x: u32::from(chroma != 3),
|
||||
shift_y: u32::from(chroma == 1),
|
||||
chroma_planes: if chroma == 0 { 0 } else { 2 },
|
||||
bytes_per_sample: 1,
|
||||
};
|
||||
// Pictures already held count at their own size, not the incoming SPS's: after a
|
||||
// resolution change, larger pictures of the old size can still be in the DPB or waiting
|
||||
// for output (one decode call may carry many access units). A buffer shared with a DPB
|
||||
// entry is counted once.
|
||||
let pic = shape.bytes().unwrap_or(u64::MAX);
|
||||
// Queued bytes minus the queue entries that share a DPB frame (each distinct DPB frame once).
|
||||
let mut ids: Vec<(u32, u64)> = self.dpb.entries.iter().map(|e| (e.frame.id, e.frame.bytes())).collect();
|
||||
ids.sort_unstable();
|
||||
ids.dedup();
|
||||
let shared = ids.iter().fold(0u64, |n, (id, b)| n.saturating_add(self.queued_ids.get(id).copied().unwrap_or(0).saturating_mul(*b)));
|
||||
let queued = self.queued_bytes.saturating_sub(shared);
|
||||
let in_dpb = self.dpb.entries.iter().fold(0u64, |n, e| n.saturating_add(e.frame.bytes()));
|
||||
let dpb = sps.max_dpb_frames().max(self.dpb.entries.len()) as u64;
|
||||
// The DPB (the larger of its new slots and what it holds now), the picture being decoded,
|
||||
// and what is in flight or queued.
|
||||
let bytes = pic.saturating_mul(dpb).max(in_dpb).saturating_add(pic).saturating_add(pic.saturating_mul(self.max_in_flight as u64).max(queued));
|
||||
ensure!(bytes <= self.memory_budget, "{}x{} pictures with {bytes} bytes in the decoder exceed the memory budget", sps.width(), sps.height());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn start_picture(&mut self, sh: &SliceHeader, sps: &Arc<Sps>, pts: i64) -> Result<()> {
|
||||
self.check_budget(sps)?;
|
||||
self.call_pictures += 1;
|
||||
if self.call_pictures > MAX_PICTURES_PER_CALL {
|
||||
return Err(Error::Invalid("too many pictures in one chunk".into()));
|
||||
}
|
||||
// Activate SPS; a resolution change flushes the DPB.
|
||||
let changed = match &self.active_sps {
|
||||
None => true,
|
||||
Some(a) => a.width() != sps.width() || a.height() != sps.height() || a.max_dpb_frames() != sps.max_dpb_frames(),
|
||||
};
|
||||
if changed && self.active_sps.is_some() {
|
||||
let mut outs = Vec::new();
|
||||
self.dpb.flush(&mut outs);
|
||||
self.emit(outs);
|
||||
self.dpb.entries.clear();
|
||||
}
|
||||
self.active_sps = Some(sps.clone());
|
||||
self.dpb.capacity = sps.max_dpb_frames();
|
||||
self.dpb.max_reorder = sps.max_num_reorder_frames().min(self.dpb.capacity);
|
||||
let mb_w = sps.pic_width_in_mbs as usize;
|
||||
let mb_h = sps.frame_height_in_mbs() as usize;
|
||||
if sh.idr {
|
||||
let mut outs = Vec::new();
|
||||
self.dpb.idr(sh.no_output_of_prior_pics, &mut outs);
|
||||
self.emit(outs);
|
||||
self.prev_ref_frame_num = 0;
|
||||
} else {
|
||||
let max = sps.max_frame_num();
|
||||
if sh.frame_num != self.prev_ref_frame_num && sh.frame_num != (self.prev_ref_frame_num + 1) % max {
|
||||
let gap = (sh.frame_num + max - self.prev_ref_frame_num - 1) % max;
|
||||
if gap > MAX_INFERRED_GAP_FRAMES {
|
||||
return Err(Error::Invalid("frame number gap exceeds the supported reference limit".into()));
|
||||
}
|
||||
// frame_num gap (8.2.5.2): insert "non-existing" frames. Their samples are never used by
|
||||
// conforming streams; copy the latest decoded frame for concealment.
|
||||
let meta = Arc::new(output_meta(sps, pts, false));
|
||||
let next_id = &mut self.next_id;
|
||||
let poc_state = &mut self.poc_state;
|
||||
let last = self.dpb.entries.iter().filter(|e| !e.non_existing).max_by_key(|e| e.frame.id).map(|e| e.frame.clone());
|
||||
let mut make = |_fnum: u32| {
|
||||
let id = *next_id;
|
||||
*next_id += 1;
|
||||
let planes = match &last {
|
||||
Some(f) if f.mb_w == mb_w && f.mb_h() == mb_h => {
|
||||
let mut p = Planes::new(mb_w * 16, mb_h * 16);
|
||||
let (y, u, v) = f.copy_cropped((0, 0, mb_w * 16, mb_h * 16));
|
||||
p.y = y;
|
||||
p.cb = u;
|
||||
p.cr = v;
|
||||
p
|
||||
}
|
||||
_ => Planes::gray(mb_w * 16, mb_h * 16),
|
||||
};
|
||||
(Arc::new(Frame::from_planes(id, 0, &planes)), 0, meta.clone())
|
||||
};
|
||||
let mut upd = |fnum: u32| poc_state.update_gap_frame(fnum, sps);
|
||||
self.shared.stats.lock().map(|mut s| s.frame_num_gaps += 1).ok();
|
||||
self.dpb.fill_frame_num_gap(self.prev_ref_frame_num, sh.frame_num, max, sps.max_num_ref_frames as usize, &mut make, &mut upd);
|
||||
self.prev_ref_frame_num = (sh.frame_num + max - 1) % max;
|
||||
}
|
||||
}
|
||||
let poc = self.poc_state.compute(sh, sps);
|
||||
// MMCO5: the picture's POC becomes relative to itself (tempPicOrderCnt, 8.2.1).
|
||||
let frame_poc = if sh.has_mmco5() { 0 } else { poc.frame() };
|
||||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
let frame = Arc::new(Frame::new(id, frame_poc, mb_w, mb_h));
|
||||
self.pending = Some(PendingPic { frame, sps: sps.clone(), first: sh.clone(), poc, pts, key: sh.idr, slices: Vec::new() });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Start decoding the pending picture and do its reference marking / DPB insertion.
|
||||
fn submit_pending(&mut self) -> Result<()> {
|
||||
let Some(p) = self.pending.take() else { return Ok(()) };
|
||||
let PendingPic { frame, sps, first, poc, pts, key, slices } = p;
|
||||
let draft = self.draft && first.nal_ref_idc == 0;
|
||||
self.dispatch(frame.clone(), slices, draft);
|
||||
self.poc_state.update(&first, &poc);
|
||||
if first.nal_ref_idc != 0 {
|
||||
self.prev_ref_frame_num = if first.has_mmco5() { 0 } else { first.frame_num };
|
||||
}
|
||||
let meta = Arc::new(OutputMeta { draft, ..output_meta(&sps, pts, key) });
|
||||
let mut outs = Vec::new();
|
||||
let fpoc = frame.poc;
|
||||
self.dpb.store_picture(&first, frame, fpoc, (sps.max_frame_num(), sps.max_num_ref_frames as usize), meta, &mut outs)?;
|
||||
self.emit(outs);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn dispatch(&mut self, frame: FrameRef, slices: Vec<SliceJob>, draft: bool) {
|
||||
let shared = self.shared.clone();
|
||||
#[cfg(feature = "threads")]
|
||||
if let Some(pool) = &self.pool {
|
||||
self.in_flight.retain(|f| !f.is_published(f.mb_h() - 1));
|
||||
while self.in_flight.len() >= self.max_in_flight {
|
||||
if let Some(f) = self.in_flight.pop_front() {
|
||||
f.wait_complete();
|
||||
}
|
||||
}
|
||||
self.in_flight.push_back(frame.clone());
|
||||
pool.spawn_fifo(move || run_job(frame, slices, &shared, draft));
|
||||
return;
|
||||
}
|
||||
run_job(frame, slices, &shared, draft);
|
||||
}
|
||||
|
||||
fn emit(&mut self, outs: Vec<Output<FrameRef>>) {
|
||||
for o in &outs {
|
||||
*self.queued_ids.entry(o.frame.id).or_insert(0) += 1;
|
||||
self.queued_bytes = self.queued_bytes.saturating_add(o.frame.bytes());
|
||||
}
|
||||
self.out_queue.extend(outs);
|
||||
}
|
||||
|
||||
fn pop_out(&mut self) -> Option<Output<FrameRef>> {
|
||||
let o = self.out_queue.pop_front()?;
|
||||
if let Some(n) = self.queued_ids.get_mut(&o.frame.id) {
|
||||
*n = n.saturating_sub(1);
|
||||
if *n == 0 {
|
||||
self.queued_ids.remove(&o.frame.id);
|
||||
}
|
||||
}
|
||||
self.queued_bytes = self.queued_bytes.saturating_sub(o.frame.bytes());
|
||||
Some(o)
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode all slices of one picture and publish its rows.
|
||||
fn run_job(frame: FrameRef, slices: Vec<SliceJob>, shared: &Shared, draft: bool) {
|
||||
// The picture state is set up inside the guard too: a panic there must not escape the job.
|
||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
let reuse = shared.pool.lock().ok().and_then(|mut p| p.pop());
|
||||
let mut pic = match reuse {
|
||||
Some(mut p) if p.mb_w == frame.mb_w && p.mb_h == frame.mb_h() => {
|
||||
p.reset(frame.clone());
|
||||
p
|
||||
}
|
||||
_ => PicState::new(frame.clone()),
|
||||
};
|
||||
pic.skip_deblock = draft;
|
||||
pic.cancel = Some(shared.cancel.clone());
|
||||
let mut first_err = None;
|
||||
for s in &slices {
|
||||
if pic.cancelled() {
|
||||
break;
|
||||
}
|
||||
if let Err(e) = decode_slice(s, &mut pic) {
|
||||
first_err.get_or_insert(e);
|
||||
}
|
||||
}
|
||||
if pic.cancelled() {
|
||||
// Nothing will show this picture: publish the remaining rows as they are.
|
||||
pic.skip_deblock = true;
|
||||
}
|
||||
pic.flush_rows(true);
|
||||
(pic, first_err)
|
||||
}));
|
||||
let (pic, err) = match result {
|
||||
Ok((pic, err)) => (Some(pic), err),
|
||||
Err(_) => {
|
||||
// Make sure readers never block on this frame.
|
||||
let fallback = Planes::gray(frame.width, frame.height);
|
||||
let blank = vec![MbState::default(); frame.mb_w * frame.mb_h()];
|
||||
for r in 0..frame.mb_h() {
|
||||
if !frame.is_published(r) {
|
||||
frame.publish(r, Frame::make_row(&fallback, r, &blank, &|_, _, _| u32::MAX));
|
||||
}
|
||||
}
|
||||
(None, Some(Error::Invalid("internal error while decoding a picture".into())))
|
||||
}
|
||||
};
|
||||
if let Ok(mut s) = shared.stats.lock() {
|
||||
s.pictures += 1;
|
||||
s.draft_pictures += draft as u64;
|
||||
for st in pic.iter().flat_map(|pic| &pic.mbs) {
|
||||
match st.kind {
|
||||
MbKind::I4x4 => s.mb_i4x4 += 1,
|
||||
MbKind::I8x8 => s.mb_i8x8 += 1,
|
||||
MbKind::I16x16 => s.mb_i16x16 += 1,
|
||||
MbKind::IPcm => s.mb_pcm += 1,
|
||||
MbKind::PSkip => s.mb_p_skip += 1,
|
||||
MbKind::BSkip => s.mb_b_skip += 1,
|
||||
MbKind::BDirect16x16 => s.mb_b_direct16x16 += 1,
|
||||
MbKind::Inter => s.mb_inter += 1,
|
||||
MbKind::None => {}
|
||||
}
|
||||
if st.transform_8x8 && !st.kind.is_intra() {
|
||||
s.mb_inter_8x8_transform += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let (Some(e), Ok(mut slot)) = (err, shared.error.lock()) {
|
||||
slot.get_or_insert(e);
|
||||
}
|
||||
if let (Some(pic), Ok(mut p)) = (pic, shared.pool.lock())
|
||||
&& p.len() < 4
|
||||
{
|
||||
p.push(pic);
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_slice(s: &SliceJob, pic: &mut PicState) -> Result<()> {
|
||||
let mut sd = SliceDecoder::new(&s.sh, &s.pps, &s.sps, pic, &s.refs, &s.ls)?;
|
||||
if s.pps.entropy_coding_mode {
|
||||
sd.decode_cabac(&s.rbsp)
|
||||
} else {
|
||||
let mut r = BitReader::new(&s.rbsp);
|
||||
r.seek_bits(s.sh.header_bits);
|
||||
sd.decode_cavlc(&mut r)
|
||||
}
|
||||
}
|
||||
|
||||
fn output_meta(sps: &Sps, pts: i64, key: bool) -> OutputMeta {
|
||||
let vui = sps.vui.clone().unwrap_or_default();
|
||||
OutputMeta {
|
||||
pts,
|
||||
key,
|
||||
crop: sps.crop_rect(),
|
||||
full_range: vui.full_range,
|
||||
colour_primaries: vui.colour_primaries,
|
||||
transfer_characteristics: vui.transfer_characteristics,
|
||||
matrix_coefficients: vui.matrix_coefficients,
|
||||
sar: vui.sar,
|
||||
draft: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn make_picture(o: &Output<FrameRef>) -> Picture {
|
||||
let (cx, cy, cw, ch) = o.meta.crop;
|
||||
let (cx, cy, cw, ch) = (cx as usize, cy as usize, cw as usize, ch as usize);
|
||||
let (y, u, v) = o.frame.copy_cropped((cx, cy, cw, ch));
|
||||
let (ccw, cch) = (cw.div_ceil(2), ch.div_ceil(2));
|
||||
Picture {
|
||||
width: cw as u32,
|
||||
height: ch as u32,
|
||||
chroma_width: ccw as u32,
|
||||
chroma_height: cch as u32,
|
||||
y,
|
||||
u,
|
||||
v,
|
||||
y_stride: cw,
|
||||
uv_stride: ccw,
|
||||
pts: o.meta.pts,
|
||||
poc: o.poc,
|
||||
key: o.meta.key,
|
||||
color: ColorInfo {
|
||||
full_range: o.meta.full_range,
|
||||
primaries: o.meta.colour_primaries,
|
||||
transfer: o.meta.transfer_characteristics,
|
||||
matrix: o.meta.matrix_coefficients,
|
||||
},
|
||||
sar: o.meta.sar,
|
||||
draft: o.meta.draft,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,509 @@
|
||||
//! Decoded picture buffer: reference marking (8.2.5), reference list construction (8.2.4) and output
|
||||
//! ordering / bumping (C.4).
|
||||
//!
|
||||
//! The buffer only does bookkeeping on picture headers; what a picture is (`F`: the software
|
||||
//! decoder's `FrameRef`, a hardware decoder's surface) is up to the decoder, so
|
||||
//! hardware decoders driven by the same headers (VA-API) share this logic and output order.
|
||||
|
||||
use crate::h264::decode::error::{Result, ensure};
|
||||
use crate::h264::decode::slice::{Mmco, SliceHeader, SliceType};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum RefMark {
|
||||
Unused,
|
||||
Short,
|
||||
Long,
|
||||
}
|
||||
|
||||
/// Output-related metadata attached to a decoded frame.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct OutputMeta {
|
||||
pub pts: i64,
|
||||
pub key: bool,
|
||||
pub crop: (u32, u32, u32, u32),
|
||||
pub full_range: bool,
|
||||
pub colour_primaries: u8,
|
||||
pub transfer_characteristics: u8,
|
||||
pub matrix_coefficients: u8,
|
||||
pub sar: (u16, u16),
|
||||
/// Decoded without deblocking (draft mode, non-reference picture).
|
||||
pub draft: bool,
|
||||
}
|
||||
|
||||
pub struct DpbEntry<F> {
|
||||
pub frame: F,
|
||||
pub poc: i32,
|
||||
pub frame_num: u32,
|
||||
pub long_term_frame_idx: u32,
|
||||
pub mark: RefMark,
|
||||
pub needed_for_output: bool,
|
||||
pub non_existing: bool,
|
||||
pub meta: Arc<OutputMeta>,
|
||||
}
|
||||
|
||||
pub struct Dpb<F> {
|
||||
pub entries: Vec<DpbEntry<F>>,
|
||||
/// None = "no long-term frame indices".
|
||||
pub max_long_term_frame_idx: Option<u32>,
|
||||
pub capacity: usize,
|
||||
pub max_reorder: usize,
|
||||
}
|
||||
|
||||
/// A picture leaving the DPB for output.
|
||||
pub struct Output<F> {
|
||||
pub frame: F,
|
||||
pub poc: i32,
|
||||
pub meta: Arc<OutputMeta>,
|
||||
}
|
||||
|
||||
impl<F: Clone> Dpb<F> {
|
||||
pub fn new() -> Self {
|
||||
Dpb { entries: Vec::new(), max_long_term_frame_idx: None, capacity: 16, max_reorder: 16 }
|
||||
}
|
||||
|
||||
fn frame_num_wrap(e: &DpbEntry<F>, cur_frame_num: u32, max_frame_num: u32) -> i32 {
|
||||
if e.frame_num > cur_frame_num { e.frame_num as i32 - max_frame_num as i32 } else { e.frame_num as i32 }
|
||||
}
|
||||
|
||||
fn num_refs(&self) -> usize {
|
||||
self.entries.iter().filter(|e| e.mark != RefMark::Unused).count()
|
||||
}
|
||||
|
||||
/// Remove entries not needed for output and not used for reference.
|
||||
fn prune(&mut self) {
|
||||
self.entries.retain(|e| e.needed_for_output || e.mark != RefMark::Unused);
|
||||
}
|
||||
|
||||
/// Output the picture with the smallest POC (C.4.5.3 "bumping"). Returns false if none waits.
|
||||
fn bump(&mut self, out: &mut Vec<Output<F>>) -> bool {
|
||||
let Some(i) = self.entries.iter().enumerate().filter(|(_, e)| e.needed_for_output).min_by_key(|(_, e)| e.poc).map(|(i, _)| i) else {
|
||||
return false;
|
||||
};
|
||||
let e = &mut self.entries[i];
|
||||
e.needed_for_output = false;
|
||||
out.push(Output { frame: e.frame.clone(), poc: e.poc, meta: e.meta.clone() });
|
||||
self.prune();
|
||||
true
|
||||
}
|
||||
|
||||
/// Output everything in POC order and clear non-reference entries.
|
||||
pub fn flush(&mut self, out: &mut Vec<Output<F>>) {
|
||||
while self.bump(out) {}
|
||||
self.prune();
|
||||
}
|
||||
|
||||
/// IDR: mark all references unused; output (or drop) prior pictures.
|
||||
pub fn idr(&mut self, no_output_of_prior_pics: bool, out: &mut Vec<Output<F>>) {
|
||||
for e in &mut self.entries {
|
||||
e.mark = RefMark::Unused;
|
||||
}
|
||||
if no_output_of_prior_pics {
|
||||
self.entries.clear();
|
||||
} else {
|
||||
self.flush(out);
|
||||
}
|
||||
self.max_long_term_frame_idx = None;
|
||||
}
|
||||
|
||||
/// Sliding window marking (8.2.5.3).
|
||||
fn sliding_window(&mut self, cur_frame_num: u32, max_frame_num: u32, max_num_ref_frames: usize) {
|
||||
let limit = max_num_ref_frames.max(1);
|
||||
while self.num_refs() >= limit {
|
||||
let victim = self
|
||||
.entries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, e)| e.mark == RefMark::Short)
|
||||
.min_by_key(|(_, e)| Self::frame_num_wrap(e, cur_frame_num, max_frame_num))
|
||||
.map(|(i, _)| i);
|
||||
match victim {
|
||||
Some(i) => self.entries[i].mark = RefMark::Unused,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
self.prune();
|
||||
}
|
||||
|
||||
/// Insert "non-existing" frames for a frame_num gap (8.2.5.2). `make_frame` gives each one's
|
||||
/// picture and POC.
|
||||
pub fn fill_frame_num_gap(
|
||||
&mut self,
|
||||
prev_ref_frame_num: u32,
|
||||
frame_num: u32,
|
||||
max_frame_num: u32,
|
||||
max_num_ref_frames: usize,
|
||||
make_frame: &mut dyn FnMut(u32) -> (F, i32, Arc<OutputMeta>),
|
||||
poc_state: &mut dyn FnMut(u32),
|
||||
) {
|
||||
let mut unused = (prev_ref_frame_num + 1) % max_frame_num;
|
||||
// Missing frames before the last `max_num_ref_frames` would be dropped by the sliding window
|
||||
// (oldest FrameNumWrap first) before the gap ends, so only their POC / frame_num bookkeeping
|
||||
// runs; building pictures for them would cost up to max_frame_num frames per access unit.
|
||||
let gap = if frame_num >= unused { frame_num - unused } else { (frame_num % max_frame_num) + max_frame_num - unused };
|
||||
let skip = gap.saturating_sub(max_num_ref_frames.max(1).try_into().unwrap_or(u32::MAX));
|
||||
let mut guard = 0;
|
||||
while unused != frame_num && guard < max_frame_num {
|
||||
if guard < skip {
|
||||
poc_state(unused);
|
||||
unused = (unused + 1) % max_frame_num;
|
||||
guard += 1;
|
||||
continue;
|
||||
}
|
||||
self.sliding_window(unused, max_frame_num, max_num_ref_frames);
|
||||
let (frame, poc, meta) = make_frame(unused);
|
||||
poc_state(unused);
|
||||
self.entries.push(DpbEntry {
|
||||
poc,
|
||||
frame,
|
||||
frame_num: unused,
|
||||
long_term_frame_idx: 0,
|
||||
mark: RefMark::Short,
|
||||
needed_for_output: false,
|
||||
non_existing: true,
|
||||
meta,
|
||||
});
|
||||
unused = (unused + 1) % max_frame_num;
|
||||
guard += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Initial reference picture lists (8.2.4.2) + modification (8.2.4.3) for a frame slice, each
|
||||
/// entry made by `to_ref` from the DPB entry it refers to.
|
||||
pub fn build_ref_lists<R>(&self, sh: &SliceHeader, cur_poc: i32, max_frame_num: u32, to_ref: impl Fn(&DpbEntry<F>) -> R) -> Result<[Vec<R>; 2]> {
|
||||
let mut lists: [Vec<R>; 2] = [Vec::new(), Vec::new()];
|
||||
if sh.slice_type.is_intra() {
|
||||
return Ok(lists);
|
||||
}
|
||||
let cur_fn = sh.frame_num;
|
||||
let pic_num = |e: &DpbEntry<F>| Self::frame_num_wrap(e, cur_fn, max_frame_num);
|
||||
let short: Vec<&DpbEntry<F>> = self.entries.iter().filter(|e| e.mark == RefMark::Short).collect();
|
||||
let mut long: Vec<&DpbEntry<F>> = self.entries.iter().filter(|e| e.mark == RefMark::Long).collect();
|
||||
long.sort_by_key(|e| e.long_term_frame_idx);
|
||||
let mut entry_lists: [Vec<&DpbEntry<F>>; 2] = [Vec::new(), Vec::new()];
|
||||
if sh.slice_type != SliceType::B {
|
||||
let mut s = short.clone();
|
||||
s.sort_by_key(|e| std::cmp::Reverse(pic_num(e)));
|
||||
entry_lists[0] = s.into_iter().chain(long.iter().copied()).collect();
|
||||
} else {
|
||||
let mut before: Vec<&DpbEntry<F>> = short.iter().copied().filter(|e| e.poc < cur_poc).collect();
|
||||
let mut after: Vec<&DpbEntry<F>> = short.iter().copied().filter(|e| e.poc > cur_poc).collect();
|
||||
before.sort_by_key(|e| std::cmp::Reverse(e.poc));
|
||||
after.sort_by_key(|e| e.poc);
|
||||
entry_lists[0] = before.iter().chain(after.iter()).chain(long.iter()).copied().collect();
|
||||
entry_lists[1] = after.iter().chain(before.iter()).chain(long.iter()).copied().collect();
|
||||
if entry_lists[1].len() > 1 && entry_lists[0].len() == entry_lists[1].len() {
|
||||
let same = entry_lists[0].iter().zip(entry_lists[1].iter()).all(|(a, b)| std::ptr::eq(*a, *b));
|
||||
if same {
|
||||
entry_lists[1].swap(0, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
let nlists = if sh.slice_type == SliceType::B { 2 } else { 1 };
|
||||
for l in 0..nlists {
|
||||
let n = sh.num_ref_idx_active[l] as usize;
|
||||
let mut list: Vec<Option<&DpbEntry<F>>> = entry_lists[l].iter().copied().map(Some).collect();
|
||||
list.truncate(n);
|
||||
list.resize(n, None);
|
||||
if !sh.ref_pic_list_mod[l].is_empty() {
|
||||
let max_pic_num = max_frame_num as i32;
|
||||
let curr_pic_num = cur_fn as i32;
|
||||
let mut pred = curr_pic_num;
|
||||
let mut ref_idx = 0usize;
|
||||
for m in &sh.ref_pic_list_mod[l] {
|
||||
ensure!(ref_idx < n, "too many ref_pic_list_modification entries");
|
||||
list.push(None); // temporarily one longer
|
||||
let (target, is_long): (Option<&DpbEntry<F>>, bool) = match m.idc {
|
||||
0 | 1 => {
|
||||
let d = m.value as i32 + 1;
|
||||
let no_wrap = if m.idc == 0 {
|
||||
if pred - d < 0 { pred - d + max_pic_num } else { pred - d }
|
||||
} else if pred + d >= max_pic_num {
|
||||
pred + d - max_pic_num
|
||||
} else {
|
||||
pred + d
|
||||
};
|
||||
pred = no_wrap;
|
||||
let pn = if no_wrap > curr_pic_num { no_wrap - max_pic_num } else { no_wrap };
|
||||
(short.iter().copied().find(|e| pic_num(e) == pn), false)
|
||||
}
|
||||
2 => (long.iter().copied().find(|e| e.long_term_frame_idx == m.value), true),
|
||||
_ => continue,
|
||||
};
|
||||
let Some(target) = target else {
|
||||
// Missing picture: keep the list unchanged (error concealment).
|
||||
list.pop();
|
||||
ref_idx += 1;
|
||||
continue;
|
||||
};
|
||||
for c in (ref_idx + 1..=n).rev() {
|
||||
list[c] = list[c - 1];
|
||||
}
|
||||
list[ref_idx] = Some(target);
|
||||
ref_idx += 1;
|
||||
let mut nidx = ref_idx;
|
||||
for c in ref_idx..=n {
|
||||
let keep = match list[c] {
|
||||
None => true,
|
||||
Some(e) => {
|
||||
if is_long {
|
||||
!(e.mark == RefMark::Long && e.long_term_frame_idx == target.long_term_frame_idx)
|
||||
} else {
|
||||
!(e.mark == RefMark::Short && std::ptr::eq(e, target))
|
||||
}
|
||||
}
|
||||
};
|
||||
if keep {
|
||||
list[nidx] = list[c];
|
||||
nidx += 1;
|
||||
}
|
||||
}
|
||||
list.truncate(n);
|
||||
}
|
||||
}
|
||||
// Fill missing entries (should not happen in conforming streams) with the first valid entry.
|
||||
let fallback = list.iter().flatten().next().copied().or_else(|| self.entries.last());
|
||||
let out: Vec<R> = list.iter().filter_map(|e| e.or(fallback)).map(&to_ref).collect();
|
||||
lists[l] = out;
|
||||
}
|
||||
Ok(lists)
|
||||
}
|
||||
|
||||
/// Adaptive memory control (8.2.5.4). Returns true if MMCO 5 was executed.
|
||||
fn apply_mmcos(&mut self, mmcos: &[Mmco], cur_frame_num: u32, max_frame_num: u32, cur_long: &mut Option<u32>) -> Result<bool> {
|
||||
let mut had5 = false;
|
||||
let curr_pic_num = cur_frame_num as i32;
|
||||
for m in mmcos {
|
||||
match m.op {
|
||||
1 => {
|
||||
let pn = curr_pic_num - (m.difference_of_pic_nums_minus1 as i32 + 1);
|
||||
for e in &mut self.entries {
|
||||
if e.mark == RefMark::Short && Self::frame_num_wrap(e, cur_frame_num, max_frame_num) == pn {
|
||||
e.mark = RefMark::Unused;
|
||||
}
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
for e in &mut self.entries {
|
||||
if e.mark == RefMark::Long && e.long_term_frame_idx == m.long_term_pic_num {
|
||||
e.mark = RefMark::Unused;
|
||||
}
|
||||
}
|
||||
}
|
||||
3 => {
|
||||
ensure!(
|
||||
self.max_long_term_frame_idx.is_some_and(|max| m.long_term_frame_idx <= max),
|
||||
"MMCO long-term index exceeds the configured limit"
|
||||
);
|
||||
let pn = curr_pic_num - (m.difference_of_pic_nums_minus1 as i32 + 1);
|
||||
for e in &mut self.entries {
|
||||
if e.mark == RefMark::Long && e.long_term_frame_idx == m.long_term_frame_idx {
|
||||
e.mark = RefMark::Unused;
|
||||
}
|
||||
}
|
||||
for e in &mut self.entries {
|
||||
if e.mark == RefMark::Short && Self::frame_num_wrap(e, cur_frame_num, max_frame_num) == pn {
|
||||
e.mark = RefMark::Long;
|
||||
e.long_term_frame_idx = m.long_term_frame_idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
let max = m.max_long_term_frame_idx_plus1;
|
||||
for e in &mut self.entries {
|
||||
if e.mark == RefMark::Long && (max == 0 || e.long_term_frame_idx > max - 1) {
|
||||
e.mark = RefMark::Unused;
|
||||
}
|
||||
}
|
||||
self.max_long_term_frame_idx = if max == 0 { None } else { Some(max - 1) };
|
||||
}
|
||||
5 => {
|
||||
for e in &mut self.entries {
|
||||
e.mark = RefMark::Unused;
|
||||
}
|
||||
self.max_long_term_frame_idx = None;
|
||||
had5 = true;
|
||||
}
|
||||
6 => {
|
||||
ensure!(
|
||||
self.max_long_term_frame_idx.is_some_and(|max| m.long_term_frame_idx <= max),
|
||||
"MMCO long-term index exceeds the configured limit"
|
||||
);
|
||||
for e in &mut self.entries {
|
||||
if e.mark == RefMark::Long && e.long_term_frame_idx == m.long_term_frame_idx {
|
||||
e.mark = RefMark::Unused;
|
||||
}
|
||||
}
|
||||
*cur_long = Some(m.long_term_frame_idx);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.prune();
|
||||
Ok(had5)
|
||||
}
|
||||
|
||||
/// Reference marking for the just-decoded picture and insertion into the DPB with output bumping.
|
||||
/// Returns pictures ready for output.
|
||||
pub fn store_picture(
|
||||
&mut self,
|
||||
sh: &SliceHeader,
|
||||
frame: F,
|
||||
poc: i32,
|
||||
(max_frame_num, max_num_ref_frames): (u32, usize),
|
||||
meta: Arc<OutputMeta>,
|
||||
out: &mut Vec<Output<F>>,
|
||||
) -> Result<()> {
|
||||
let mut mark = RefMark::Unused;
|
||||
let mut long_idx = 0;
|
||||
let mut had5 = false;
|
||||
if sh.nal_ref_idc != 0 {
|
||||
if sh.idr {
|
||||
if sh.long_term_reference {
|
||||
mark = RefMark::Long;
|
||||
self.max_long_term_frame_idx = Some(0);
|
||||
} else {
|
||||
mark = RefMark::Short;
|
||||
self.max_long_term_frame_idx = None;
|
||||
}
|
||||
} else {
|
||||
let mut cur_long = None;
|
||||
if sh.adaptive_ref_pic_marking {
|
||||
had5 = self.apply_mmcos(&sh.mmcos, sh.frame_num, max_frame_num, &mut cur_long)?;
|
||||
} else {
|
||||
self.sliding_window(sh.frame_num, max_frame_num, max_num_ref_frames);
|
||||
}
|
||||
if let Some(i) = cur_long {
|
||||
mark = RefMark::Long;
|
||||
long_idx = i;
|
||||
} else {
|
||||
mark = RefMark::Short;
|
||||
// make room if MMCOs left the DPB over-full (non-conforming streams)
|
||||
if self.num_refs() >= max_num_ref_frames.max(1) && sh.adaptive_ref_pic_marking {
|
||||
self.sliding_window(sh.frame_num, max_frame_num, max_num_ref_frames);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if mark == RefMark::Long {
|
||||
ensure!(self.num_refs() < max_num_ref_frames.max(1), "long-term reference exceeds max_num_ref_frames");
|
||||
}
|
||||
if had5 {
|
||||
// All earlier pictures are output before the current one (C.4.4).
|
||||
self.flush(out);
|
||||
}
|
||||
let frame_num = if had5 { 0 } else { sh.frame_num };
|
||||
// Bumping until there is space (C.4.5.1 / C.4.5.2).
|
||||
if mark == RefMark::Unused {
|
||||
loop {
|
||||
let waiting = self.entries.len();
|
||||
if waiting < self.capacity {
|
||||
break;
|
||||
}
|
||||
// non-reference picture with the smallest POC is output directly
|
||||
let min_poc = self.entries.iter().filter(|e| e.needed_for_output).map(|e| e.poc).min();
|
||||
if min_poc.is_none_or(|m| poc < m) {
|
||||
out.push(Output { frame, poc, meta });
|
||||
return Ok(());
|
||||
}
|
||||
if !self.bump(out) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while self.entries.len() >= self.capacity {
|
||||
if !self.bump(out) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.entries.push(DpbEntry {
|
||||
frame,
|
||||
poc,
|
||||
frame_num,
|
||||
long_term_frame_idx: long_idx,
|
||||
mark,
|
||||
needed_for_output: true,
|
||||
non_existing: false,
|
||||
meta,
|
||||
});
|
||||
// Low-delay output: honour max_num_reorder_frames.
|
||||
while self.entries.iter().filter(|e| e.needed_for_output).count() > self.max_reorder {
|
||||
if !self.bump(out) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Clone> Default for Dpb<F> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn meta() -> Arc<OutputMeta> {
|
||||
Arc::new(OutputMeta {
|
||||
pts: 0,
|
||||
key: false,
|
||||
crop: (0, 0, 0, 0),
|
||||
full_range: false,
|
||||
colour_primaries: 2,
|
||||
transfer_characteristics: 2,
|
||||
matrix_coefficients: 2,
|
||||
sar: (1, 1),
|
||||
draft: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// A huge frame_num jump must only materialise the frames the sliding window keeps (#2779).
|
||||
#[test]
|
||||
fn frame_num_gap_materialises_at_most_max_num_ref_frames() {
|
||||
let mut dpb: Dpb<u32> = Dpb::new();
|
||||
let m = meta();
|
||||
let mut made = 0u32;
|
||||
let mut make = |f: u32| {
|
||||
made += 1;
|
||||
(f, 0, m.clone())
|
||||
};
|
||||
let mut updated = Vec::new();
|
||||
let mut upd = |f: u32| updated.push(f);
|
||||
dpb.fill_frame_num_gap(0, 32768, 1 << 16, 4, &mut make, &mut upd);
|
||||
assert!(made <= 4, "gap fill materialised {made} frames, expected <= 4");
|
||||
assert_eq!(updated, (1..32768).collect::<Vec<u32>>());
|
||||
let mut refs: Vec<u32> = dpb.entries.iter().filter(|e| e.mark == RefMark::Short).map(|e| e.frame_num).collect();
|
||||
refs.sort_unstable();
|
||||
assert_eq!(refs, vec![32764, 32765, 32766, 32767]);
|
||||
assert!(dpb.entries.iter().all(|e| e.frame == e.frame_num));
|
||||
}
|
||||
|
||||
/// Existing short-term references are still evicted by a gap no longer than the window.
|
||||
#[test]
|
||||
fn short_frame_num_gap_keeps_sliding_window_semantics() {
|
||||
let mut dpb: Dpb<u32> = Dpb::new();
|
||||
let m = meta();
|
||||
for f in 0..2 {
|
||||
dpb.entries.push(DpbEntry {
|
||||
frame: f,
|
||||
poc: 0,
|
||||
frame_num: f,
|
||||
long_term_frame_idx: 0,
|
||||
mark: RefMark::Short,
|
||||
needed_for_output: false,
|
||||
non_existing: false,
|
||||
meta: m.clone(),
|
||||
});
|
||||
}
|
||||
let mut make = |f: u32| (f, 0, m.clone());
|
||||
let mut upd = |_f: u32| {};
|
||||
dpb.fill_frame_num_gap(1, 5, 16, 4, &mut make, &mut upd);
|
||||
let mut refs: Vec<u32> = dpb.entries.iter().filter(|e| e.mark == RefMark::Short).map(|e| e.frame_num).collect();
|
||||
refs.sort_unstable();
|
||||
assert_eq!(refs, vec![1, 2, 3, 4]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
use craft_media::bitstream::BitError;
|
||||
|
||||
/// Errors produced by the decoder.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
/// The bitstream ended early or contained an invalid Exp-Golomb code.
|
||||
#[error("bitstream error: {0}")]
|
||||
Bitstream(#[from] BitError),
|
||||
/// A syntax element had a value outside its legal range.
|
||||
#[error("invalid bitstream: {0}")]
|
||||
Invalid(String),
|
||||
/// A valid feature this decoder does not implement (yet).
|
||||
#[error("unsupported: {0}")]
|
||||
Unsupported(String),
|
||||
/// A slice referenced a parameter set that has not been received.
|
||||
#[error("missing parameter set: {0}")]
|
||||
MissingParameterSet(String),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
pub(crate) fn invalid<T>(msg: impl Into<String>) -> Result<T> {
|
||||
Err(Error::Invalid(msg.into()))
|
||||
}
|
||||
|
||||
pub(crate) fn unsupported<T>(msg: impl Into<String>) -> Result<T> {
|
||||
Err(Error::Unsupported(msg.into()))
|
||||
}
|
||||
|
||||
/// `ensure!(cond, "message")` returns `Error::Invalid` when `cond` is false.
|
||||
macro_rules! ensure {
|
||||
($cond:expr, $($arg:tt)+) => {
|
||||
if !$cond {
|
||||
return Err($crate::h264::decode::error::Error::Invalid(format!($($arg)+)));
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use ensure;
|
||||
@@ -0,0 +1,601 @@
|
||||
//! Inter prediction sample interpolation (8.4.2.2) and weighted sample prediction (8.4.2.3).
|
||||
|
||||
/// A reference plane (used by the plane-based helpers in tests).
|
||||
#[cfg(test)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct PlaneRef<'a> {
|
||||
pub data: &'a [u8],
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub stride: usize,
|
||||
}
|
||||
|
||||
const WIN: usize = 16 + 5;
|
||||
|
||||
/// Copy the (bw+5)x(bh+5) window whose top-left is (x-2, y-2) into `win` (stride WIN), clamping
|
||||
/// coordinates to the picture.
|
||||
#[cfg(test)]
|
||||
#[inline]
|
||||
fn fetch_window(p: PlaneRef, x: i32, y: i32, bw: usize, bh: usize, win: &mut [u8; WIN * WIN]) {
|
||||
let x0 = x - 2;
|
||||
let y0 = y - 2;
|
||||
let ww = bw + 5;
|
||||
let wh = bh + 5;
|
||||
let maxx = p.width as i32 - 1;
|
||||
let maxy = p.height as i32 - 1;
|
||||
if x0 >= 0 && y0 >= 0 && x0 + ww as i32 - 1 <= maxx && y0 + wh as i32 - 1 <= maxy {
|
||||
for r in 0..wh {
|
||||
let src = (y0 as usize + r) * p.stride + x0 as usize;
|
||||
win[r * WIN..r * WIN + ww].copy_from_slice(&p.data[src..src + ww]);
|
||||
}
|
||||
} else {
|
||||
for r in 0..wh {
|
||||
let yy = (y0 + r as i32).clamp(0, maxy) as usize;
|
||||
let row = &p.data[yy * p.stride..yy * p.stride + p.width];
|
||||
for c in 0..ww {
|
||||
let xx = (x0 + c as i32).clamp(0, maxx) as usize;
|
||||
win[r * WIN + c] = row[xx];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn tap6(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> i32 {
|
||||
a - 5 * b + 20 * c + 20 * d - 5 * e + f
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn clip1(v: i32) -> u8 {
|
||||
v.clamp(0, 255) as u8
|
||||
}
|
||||
|
||||
/// Copy `n` bytes with a fixed-size move for the common block widths.
|
||||
#[inline(always)]
|
||||
fn copy_n(dst: &mut [u8], src: &[u8], n: usize) {
|
||||
fn fixed<const N: usize>(dst: &mut [u8], src: &[u8]) {
|
||||
if let (Some(d), Some(s)) = (dst.first_chunk_mut::<N>(), src.first_chunk::<N>()) {
|
||||
*d = *s;
|
||||
}
|
||||
}
|
||||
match n {
|
||||
16 => fixed::<16>(dst, src),
|
||||
8 => fixed::<8>(dst, src),
|
||||
4 => fixed::<4>(dst, src),
|
||||
2 => fixed::<2>(dst, src),
|
||||
_ => dst[..n].copy_from_slice(&src[..n]),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn avg(a: u8, b: u8) -> u8 {
|
||||
((a as u16 + b as u16 + 1) >> 1) as u8
|
||||
}
|
||||
|
||||
/// 6-tap filter in i16: the unclipped result of a first pass lies in -2550..=10710.
|
||||
#[inline(always)]
|
||||
fn tap6_16(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> i16 {
|
||||
let [a, b, c, d, e, f] = [a, b, c, d, e, f].map(i16::from);
|
||||
a - 5 * b + 20 * c + 20 * d - 5 * e + f
|
||||
}
|
||||
|
||||
/// Round and clip a first-pass value: Clip1((v + 16) >> 5).
|
||||
#[inline(always)]
|
||||
fn round5(v: i16) -> u8 {
|
||||
((v + 16) >> 5).clamp(0, 255) as u8
|
||||
}
|
||||
|
||||
/// Horizontal 6-tap (unclipped) for `BW` outputs: out[c] uses row[c..c + 6].
|
||||
#[inline(always)]
|
||||
fn htap_row<const BW: usize>(row: &[u8], out: &mut [i16; 16]) {
|
||||
let row = &row[..BW + 5];
|
||||
for c in 0..BW {
|
||||
out[c] = tap6_16(row[c], row[c + 1], row[c + 2], row[c + 3], row[c + 4], row[c + 5]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Vertical 6-tap (unclipped) over six rows at columns 0..BW.
|
||||
#[inline(always)]
|
||||
fn vtap_row<const BW: usize>(rows: [&[u8]; 6], out: &mut [i16; 16]) {
|
||||
let [a, b, c, d, e, f] = rows.map(|r| &r[..BW]);
|
||||
for i in 0..BW {
|
||||
out[i] = tap6_16(a[i], b[i], c[i], d[i], e[i], f[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn copy_fixed<const BW: usize>(dst: &mut [u8], src: &[u8]) {
|
||||
if let (Some(d), Some(s)) = (dst.first_chunk_mut::<BW>(), src.first_chunk::<BW>()) {
|
||||
*d = *s;
|
||||
}
|
||||
}
|
||||
|
||||
/// Luma sample interpolation for a bw x bh block (8.4.2.2.1). (x, y) is the integer sample position
|
||||
/// (block position + (mv >> 2)); (fx, fy) the quarter-sample fraction. Output stride is `os`.
|
||||
#[cfg(test)]
|
||||
pub fn mc_luma(p: PlaneRef, x: i32, y: i32, blk: McBlock, out: &mut [u8], os: usize) {
|
||||
let McBlock { bw, bh, .. } = blk;
|
||||
let mut win = [0u8; WIN * WIN];
|
||||
let (x0, y0) = (x - 2, y - 2);
|
||||
let inside = x0 >= 0 && y0 >= 0 && x0 as usize + bw + 5 <= p.width && y0 as usize + bh + 5 <= p.height;
|
||||
let (src, ss): (&[u8], usize) = if inside {
|
||||
(&p.data[y0 as usize * p.stride + x0 as usize..], p.stride)
|
||||
} else {
|
||||
fetch_window(p, x, y, bw, bh, &mut win);
|
||||
(&win[..], WIN)
|
||||
};
|
||||
mc_luma_win(src, ss, blk, out, os);
|
||||
}
|
||||
|
||||
/// Luma interpolation from a prepared window: `src[0]` is the sample at (x - 2, y - 2) and `ss` the
|
||||
/// window stride; the window covers (bw + 5) x (bh + 5) samples. `bw` is 4, 8 or 16.
|
||||
pub fn mc_luma_win(src: &[u8], ss: usize, blk: McBlock, out: &mut [u8], os: usize) {
|
||||
let McBlock { fx, fy, bw, bh } = blk;
|
||||
match bw {
|
||||
16 => luma_win::<16>(src, ss, fx, fy, bh, out, os),
|
||||
8 => luma_win::<8>(src, ss, fx, fy, bh, out, os),
|
||||
_ => luma_win::<4>(src, ss, fx, fy, bh, out, os),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn luma_win<const BW: usize>(src: &[u8], ss: usize, fx: u32, fy: u32, bh: usize, out: &mut [u8], os: usize) {
|
||||
// window row r (0..bh+5) starts at src[r * ss]; sample G of block (r, c) is at window (r + 2, c + 2)
|
||||
let wrow = |r: usize| &src[r * ss..r * ss + BW + 5];
|
||||
let rows6 = |r: usize, off: usize| [wrow(r), wrow(r + 1), wrow(r + 2), wrow(r + 3), wrow(r + 4), wrow(r + 5)].map(|x| &x[off..]);
|
||||
let mut t = [0i16; 16];
|
||||
match (fx, fy) {
|
||||
(0, 0) => {
|
||||
for r in 0..bh {
|
||||
copy_fixed::<BW>(&mut out[r * os..], &wrow(r + 2)[2..]);
|
||||
}
|
||||
}
|
||||
(_, 0) => {
|
||||
let g = if fx == 1 { 2 } else { 3 };
|
||||
for r in 0..bh {
|
||||
let row = wrow(r + 2);
|
||||
htap_row::<BW>(row, &mut t);
|
||||
let o = &mut out[r * os..r * os + BW];
|
||||
if fx == 2 {
|
||||
for c in 0..BW {
|
||||
o[c] = round5(t[c]);
|
||||
}
|
||||
} else {
|
||||
let g = &row[g..g + BW];
|
||||
for c in 0..BW {
|
||||
o[c] = avg(g[c], round5(t[c]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(0, _) => {
|
||||
for r in 0..bh {
|
||||
let rows = rows6(r, 2);
|
||||
vtap_row::<BW>(rows, &mut t);
|
||||
let o = &mut out[r * os..r * os + BW];
|
||||
if fy == 2 {
|
||||
for c in 0..BW {
|
||||
o[c] = round5(t[c]);
|
||||
}
|
||||
} else {
|
||||
let g = &(if fy == 1 { rows[2] } else { rows[3] })[..BW];
|
||||
for c in 0..BW {
|
||||
o[c] = avg(g[c], round5(t[c]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(2, _) | (_, 2) => {
|
||||
// unclipped horizontal half samples b1 for window rows 0..bh+5
|
||||
let mut b1 = [[0i16; 16]; WIN];
|
||||
for (r, bt) in b1.iter_mut().enumerate().take(bh + 5) {
|
||||
htap_row::<BW>(wrow(r), bt);
|
||||
}
|
||||
let mut j = [0u8; 16];
|
||||
let mut v = [0i16; 16];
|
||||
for r in 0..bh {
|
||||
let o = &mut out[r * os..r * os + BW];
|
||||
let [b0, b1r, b2, b3, b4, b5] = [&b1[r], &b1[r + 1], &b1[r + 2], &b1[r + 3], &b1[r + 4], &b1[r + 5]];
|
||||
for c in 0..BW {
|
||||
let [a, b, cc, d, e, f] = [b0[c], b1r[c], b2[c], b3[c], b4[c], b5[c]].map(i32::from);
|
||||
j[c] = clip1((tap6(a, b, cc, d, e, f) + 512) >> 10);
|
||||
}
|
||||
match (fx, fy) {
|
||||
(2, 2) => o.copy_from_slice(&j[..BW]),
|
||||
(2, 1) | (2, 3) => {
|
||||
let bh_ = if fy == 1 { b2 } else { b3 };
|
||||
for c in 0..BW {
|
||||
o[c] = avg(round5(bh_[c]), j[c]);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
vtap_row::<BW>(rows6(r, if fx == 1 { 2 } else { 3 }), &mut v);
|
||||
for c in 0..BW {
|
||||
o[c] = avg(round5(v[c]), j[c]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// e, g, p, r: average of a horizontal half sample (row r or r + 1) and a vertical one (col c or c + 1)
|
||||
let hoff = if fy == 1 { 2 } else { 3 };
|
||||
let voff = if fx == 1 { 2 } else { 3 };
|
||||
let mut v = [0i16; 16];
|
||||
for r in 0..bh {
|
||||
htap_row::<BW>(wrow(r + hoff), &mut t);
|
||||
vtap_row::<BW>(rows6(r, voff), &mut v);
|
||||
let o = &mut out[r * os..r * os + BW];
|
||||
for c in 0..BW {
|
||||
o[c] = avg(round5(t[c]), round5(v[c]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Chroma sample interpolation (8.4.2.2.2) for a bw x bh block at integer position (x, y) with
|
||||
/// eighth-sample fraction (fx, fy).
|
||||
#[cfg(test)]
|
||||
pub fn mc_chroma(p: PlaneRef, x: i32, y: i32, blk: McBlock, out: &mut [u8], os: usize) {
|
||||
let McBlock { bw, bh, .. } = blk;
|
||||
let maxx = p.width as i32 - 1;
|
||||
let maxy = p.height as i32 - 1;
|
||||
let inside = x >= 0 && y >= 0 && x + bw as i32 <= maxx && y + bh as i32 <= maxy;
|
||||
// (bw+1) x (bh+1) source window; copied with edge clamping when it crosses the picture border
|
||||
let mut win = [0u8; 9 * 9];
|
||||
let (src, ss): (&[u8], usize) = if inside {
|
||||
(&p.data[y as usize * p.stride + x as usize..], p.stride)
|
||||
} else {
|
||||
for r in 0..=bh {
|
||||
let yy = (y + r as i32).clamp(0, maxy) as usize;
|
||||
for c in 0..=bw {
|
||||
win[r * 9 + c] = p.data[yy * p.stride + (x + c as i32).clamp(0, maxx) as usize];
|
||||
}
|
||||
}
|
||||
(&win[..], 9)
|
||||
};
|
||||
mc_chroma_win(src, ss, blk, out, os);
|
||||
}
|
||||
|
||||
/// Chroma interpolation from a prepared (bw + 1) x (bh + 1) window whose first sample is the integer
|
||||
/// position of the block. `bw` is 2, 4 or 8.
|
||||
pub fn mc_chroma_win(src: &[u8], ss: usize, blk: McBlock, out: &mut [u8], os: usize) {
|
||||
let McBlock { fx, fy, bw, bh } = blk;
|
||||
match bw {
|
||||
8 => chroma_win::<8>(src, ss, fx, fy, bh, out, os),
|
||||
4 => chroma_win::<4>(src, ss, fx, fy, bh, out, os),
|
||||
_ => chroma_win::<2>(src, ss, fx, fy, bh, out, os),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn chroma_win<const BW: usize>(src: &[u8], ss: usize, fx: u32, fy: u32, bh: usize, out: &mut [u8], os: usize) {
|
||||
if fx == 0 && fy == 0 {
|
||||
for r in 0..bh {
|
||||
copy_fixed::<BW>(&mut out[r * os..], &src[r * ss..]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let (fx, fy) = (fx as u16, fy as u16);
|
||||
let (w00, w10, w01, w11) = ((8 - fx) * (8 - fy), fx * (8 - fy), (8 - fx) * fy, fx * fy);
|
||||
for r in 0..bh {
|
||||
let r0 = &src[r * ss..r * ss + BW + 1];
|
||||
let r1 = &src[(r + 1) * ss..(r + 1) * ss + BW + 1];
|
||||
let o = &mut out[r * os..r * os + BW];
|
||||
for c in 0..BW {
|
||||
let v = w00 * r0[c] as u16 + w10 * r0[c + 1] as u16 + w01 * r1[c] as u16 + w11 * r1[c + 1] as u16;
|
||||
o[c] = ((v + 32) >> 6) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Fractional offset and size of one motion-compensated block.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct McBlock {
|
||||
pub fx: u32,
|
||||
pub fy: u32,
|
||||
pub bw: usize,
|
||||
pub bh: usize,
|
||||
}
|
||||
|
||||
/// Weights for one prediction direction or pair.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Weight {
|
||||
/// Default: copy (single list) or rounded average (bi).
|
||||
Default,
|
||||
/// Explicit/implicit weighting: log2 denominator, weights and offsets for L0 and L1.
|
||||
Weighted { log_wd: i32, w0: i32, w1: i32, o0: i32, o1: i32 },
|
||||
}
|
||||
|
||||
/// Combine prediction(s) into `dst` (stride `ds`). `p0`/`p1` have stride `ps`.
|
||||
pub fn weighted_store(dst: &mut [u8], ds: usize, p0: Option<&[u8]>, p1: Option<&[u8]>, ps: usize, (bw, bh): (usize, usize), w: Weight) {
|
||||
// equal weights 1 << logWD with no offset (implicit 32/32) reduce exactly to the rounded average
|
||||
let w = match w {
|
||||
Weight::Weighted { log_wd, w0, w1, o0, o1 } if p0.is_some() && p1.is_some() && w0 == w1 && w0 == 1 << log_wd && (o0 + o1 + 1) >> 1 == 0 => {
|
||||
Weight::Default
|
||||
}
|
||||
w => w,
|
||||
};
|
||||
match (p0, p1, w) {
|
||||
(Some(a), Some(b), Weight::Default) => {
|
||||
for r in 0..bh {
|
||||
let d = &mut dst[r * ds..r * ds + bw];
|
||||
let ra = &a[r * ps..r * ps + bw];
|
||||
let rb = &b[r * ps..r * ps + bw];
|
||||
for ((d, &x), &y) in d.iter_mut().zip(ra).zip(rb) {
|
||||
*d = ((x as u32 + y as u32 + 1) >> 1) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
(Some(a), Some(b), Weight::Weighted { log_wd, w0, w1, o0, o1 }) => {
|
||||
let round = 1 << log_wd;
|
||||
let off = (o0 + o1 + 1) >> 1;
|
||||
let sh = log_wd + 1;
|
||||
for r in 0..bh {
|
||||
let d = &mut dst[r * ds..r * ds + bw];
|
||||
let ra = &a[r * ps..r * ps + bw];
|
||||
let rb = &b[r * ps..r * ps + bw];
|
||||
for ((d, &x), &y) in d.iter_mut().zip(ra).zip(rb) {
|
||||
*d = clip1(((x as i32 * w0 + y as i32 * w1 + round) >> sh) + off);
|
||||
}
|
||||
}
|
||||
}
|
||||
(Some(a), None, w) | (None, Some(a), w) => {
|
||||
let (wt, o, log_wd) = match (w, p0.is_some()) {
|
||||
(Weight::Default, _) => {
|
||||
for r in 0..bh {
|
||||
copy_n(&mut dst[r * ds..], &a[r * ps..], bw);
|
||||
}
|
||||
return;
|
||||
}
|
||||
(Weight::Weighted { w0, o0, log_wd, .. }, true) => (w0, o0, log_wd),
|
||||
(Weight::Weighted { w1, o1, log_wd, .. }, false) => (w1, o1, log_wd),
|
||||
};
|
||||
let round = if log_wd >= 1 { 1 << (log_wd - 1) } else { 0 };
|
||||
for r in 0..bh {
|
||||
let d = &mut dst[r * ds..r * ds + bw];
|
||||
let ra = &a[r * ps..r * ps + bw];
|
||||
for (d, &x) in d.iter_mut().zip(ra) {
|
||||
*d = clip1(((x as i32 * wt + round) >> log_wd) + o);
|
||||
}
|
||||
}
|
||||
}
|
||||
(None, None, _) => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod reference {
|
||||
use super::*;
|
||||
|
||||
/// Luma sample interpolation for a bw x bh block. (x, y) is the integer sample position
|
||||
/// (block position + (mv >> 2)); (fx, fy) the quarter-sample fraction. Output stride is `os`.
|
||||
pub fn mc_luma_ref(p: PlaneRef, x: i32, y: i32, blk: McBlock, out: &mut [u8], os: usize) {
|
||||
let McBlock { fx, fy, bw, bh } = blk;
|
||||
let mut win = [0u8; WIN * WIN];
|
||||
fetch_window(p, x, y, bw, bh, &mut win);
|
||||
// sample G at (r, c) in block coordinates is win[(r + 2) * WIN + c + 2]
|
||||
let g = |r: usize, c: usize| win[(r + 2) * WIN + c + 2] as i32;
|
||||
// unclipped horizontal half-sample b1 at block row r (-2..bh+3 via offset) and column c
|
||||
let b1 = |r: isize, c: usize| {
|
||||
let base = ((r + 2) as usize) * WIN + c;
|
||||
tap6(win[base] as i32, win[base + 1] as i32, win[base + 2] as i32, win[base + 3] as i32, win[base + 4] as i32, win[base + 5] as i32)
|
||||
};
|
||||
let h1 = |r: usize, c: isize| {
|
||||
let col = (c + 2) as usize;
|
||||
tap6(
|
||||
win[r * WIN + col] as i32,
|
||||
win[(r + 1) * WIN + col] as i32,
|
||||
win[(r + 2) * WIN + col] as i32,
|
||||
win[(r + 3) * WIN + col] as i32,
|
||||
win[(r + 4) * WIN + col] as i32,
|
||||
win[(r + 5) * WIN + col] as i32,
|
||||
)
|
||||
};
|
||||
match (fx, fy) {
|
||||
(0, 0) => {
|
||||
for r in 0..bh {
|
||||
out[r * os..r * os + bw].copy_from_slice(&win[(r + 2) * WIN + 2..(r + 2) * WIN + 2 + bw]);
|
||||
}
|
||||
}
|
||||
(_, 0) => {
|
||||
// a, b, c
|
||||
for r in 0..bh {
|
||||
for c in 0..bw {
|
||||
let b = clip1((b1(r as isize, c) + 16) >> 5) as i32;
|
||||
out[r * os + c] = match fx {
|
||||
1 => ((g(r, c) + b + 1) >> 1) as u8,
|
||||
2 => b as u8,
|
||||
_ => ((g(r, c + 1) + b + 1) >> 1) as u8,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
(0, _) => {
|
||||
// d, h, n
|
||||
for r in 0..bh {
|
||||
for c in 0..bw {
|
||||
let h = clip1((h1(r, c as isize) + 16) >> 5) as i32;
|
||||
out[r * os + c] = match fy {
|
||||
1 => ((g(r, c) + h + 1) >> 1) as u8,
|
||||
2 => h as u8,
|
||||
_ => ((g(r + 1, c) + h + 1) >> 1) as u8,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
(2, _) | (_, 2) => {
|
||||
// j-based: j needs b1 for rows -2..bh+3
|
||||
let mut bcol = [0i32; WIN * WIN];
|
||||
for r in 0..bh + 5 {
|
||||
for c in 0..bw {
|
||||
bcol[r * WIN + c] = b1(r as isize - 2, c);
|
||||
}
|
||||
}
|
||||
for r in 0..bh {
|
||||
for c in 0..bw {
|
||||
let j1 = tap6(
|
||||
bcol[r * WIN + c],
|
||||
bcol[(r + 1) * WIN + c],
|
||||
bcol[(r + 2) * WIN + c],
|
||||
bcol[(r + 3) * WIN + c],
|
||||
bcol[(r + 4) * WIN + c],
|
||||
bcol[(r + 5) * WIN + c],
|
||||
);
|
||||
let j = clip1((j1 + 512) >> 10) as i32;
|
||||
let v = match (fx, fy) {
|
||||
(2, 2) => j,
|
||||
(2, 1) => (clip1((bcol[(r + 2) * WIN + c] + 16) >> 5) as i32 + j + 1) >> 1, // f
|
||||
(2, 3) => (clip1((bcol[(r + 3) * WIN + c] + 16) >> 5) as i32 + j + 1) >> 1, // q
|
||||
(1, 2) => (clip1((h1(r, c as isize) + 16) >> 5) as i32 + j + 1) >> 1, // i
|
||||
_ => (clip1((h1(r, c as isize + 1) + 16) >> 5) as i32 + j + 1) >> 1, // k
|
||||
};
|
||||
out[r * os + c] = v as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// e, g, p, r: average of a horizontal half-sample (b or s) and a vertical one (h or m)
|
||||
for r in 0..bh {
|
||||
for c in 0..bw {
|
||||
let hr = if fy == 1 { r as isize } else { r as isize + 1 };
|
||||
let vc = if fx == 1 { c as isize } else { c as isize + 1 };
|
||||
let bh_ = clip1((b1(hr, c) + 16) >> 5) as i32;
|
||||
let vv = clip1((h1(r, vc) + 16) >> 5) as i32;
|
||||
out[r * os + c] = ((bh_ + vv + 1) >> 1) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn plane(w: usize, h: usize, f: impl Fn(usize, usize) -> u8) -> Vec<u8> {
|
||||
(0..w * h).map(|i| f(i % w, i / w)).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_sample_copy_and_clamp() {
|
||||
let d = plane(16, 16, |x, y| (x + 16 * y) as u8);
|
||||
let p = PlaneRef { data: &d, width: 16, height: 16, stride: 16 };
|
||||
let mut out = [0u8; 16];
|
||||
mc_luma(p, 2, 3, McBlock { fx: 0, fy: 0, bw: 4, bh: 4 }, &mut out, 4);
|
||||
assert_eq!(&out[..4], &[50, 51, 52, 53]);
|
||||
// clamped: far outside to the top-left gives sample (0,0)
|
||||
mc_luma(p, -40, -40, McBlock { fx: 0, fy: 0, bw: 4, bh: 4 }, &mut out, 4);
|
||||
assert!(out.iter().all(|&v| v == 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn half_sample_constant_is_constant() {
|
||||
let d = vec![100u8; 32 * 32];
|
||||
let p = PlaneRef { data: &d, width: 32, height: 32, stride: 32 };
|
||||
for fx in 0..4 {
|
||||
for fy in 0..4 {
|
||||
let mut out = [0u8; 64];
|
||||
mc_luma(p, 8, 8, McBlock { fx, fy, bw: 8, bh: 8 }, &mut out, 8);
|
||||
assert!(out.iter().all(|&v| v == 100), "{fx},{fy}");
|
||||
mc_chroma(p, 8, 8, McBlock { fx: fx * 2, fy: fy * 2, bw: 8, bh: 8 }, &mut out, 8);
|
||||
assert!(out.iter().all(|&v| v == 100));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn half_sample_horizontal_ramp() {
|
||||
// linear ramp: 6-tap filter of a linear function is exact: b = (G + H) / 2 (rounded)
|
||||
let d = plane(32, 32, |x, _| (x * 4) as u8);
|
||||
let p = PlaneRef { data: &d, width: 32, height: 32, stride: 32 };
|
||||
let mut out = [0u8; 16];
|
||||
mc_luma(p, 8, 8, McBlock { fx: 2, fy: 0, bw: 4, bh: 4 }, &mut out, 4);
|
||||
assert_eq!(&out[..4], &[34, 38, 42, 46]);
|
||||
mc_luma(p, 8, 8, McBlock { fx: 1, fy: 0, bw: 4, bh: 4 }, &mut out, 4);
|
||||
assert_eq!(&out[..4], &[33, 37, 41, 45]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_luma_matches_reference() {
|
||||
let mut seed = 7u32;
|
||||
let d: Vec<u8> = (0..40 * 36)
|
||||
.map(|_| {
|
||||
seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
|
||||
(seed >> 24) as u8
|
||||
})
|
||||
.collect();
|
||||
let p = PlaneRef { data: &d, width: 40, height: 36, stride: 40 };
|
||||
for &(bw, bh) in &[(16, 16), (16, 8), (8, 16), (8, 8), (8, 4), (4, 8), (4, 4)] {
|
||||
for fx in 0..4 {
|
||||
for fy in 0..4 {
|
||||
for &(x, y) in &[(10, 9), (-3, -5), (30, 30), (0, 0), (2, 2), (38, 1)] {
|
||||
let mut a = [0u8; 256];
|
||||
let mut b = [0u8; 256];
|
||||
mc_luma(p, x, y, McBlock { fx, fy, bw, bh }, &mut a, 16);
|
||||
reference::mc_luma_ref(p, x, y, McBlock { fx, fy, bw, bh }, &mut b, 16);
|
||||
assert_eq!(a, b, "{bw}x{bh} frac {fx},{fy} at {x},{y}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chroma_matches_formula() {
|
||||
let mut seed = 11u32;
|
||||
let src: Vec<u8> = (0..9 * 9)
|
||||
.map(|_| {
|
||||
seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
|
||||
(seed >> 24) as u8
|
||||
})
|
||||
.collect();
|
||||
for bw in [2, 4, 8] {
|
||||
for bh in [2, 4, 8] {
|
||||
for fx in 0..8 {
|
||||
for fy in 0..8 {
|
||||
let mut out = [0u8; 64];
|
||||
mc_chroma_win(&src, 9, McBlock { fx, fy, bw, bh }, &mut out, 8);
|
||||
for r in 0..bh {
|
||||
for c in 0..bw {
|
||||
let s = |dr: usize, dc: usize| src[(r + dr) * 9 + c + dc] as u32;
|
||||
let want =
|
||||
((8 - fx) * (8 - fy) * s(0, 0) + fx * (8 - fy) * s(0, 1) + (8 - fx) * fy * s(1, 0) + fx * fy * s(1, 1) + 32) >> 6;
|
||||
assert_eq!(out[r * 8 + c] as u32, want, "{bw}x{bh} frac {fx},{fy} at {r},{c}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn weighting() {
|
||||
let a = [100u8; 4];
|
||||
let b = [50u8; 4];
|
||||
let mut d = [0u8; 4];
|
||||
weighted_store(&mut d, 4, Some(&a), Some(&b), 4, (4, 1), Weight::Default);
|
||||
assert_eq!(d, [75; 4]);
|
||||
weighted_store(&mut d, 4, Some(&a), None, 4, (4, 1), Weight::Weighted { log_wd: 5, w0: 16, w1: 0, o0: 3, o1: 0 });
|
||||
assert_eq!(d, [53; 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equal_power_of_two_weights_are_the_average() {
|
||||
let a: Vec<u8> = (0..=255).collect();
|
||||
let b: Vec<u8> = (0..=255).rev().map(|v: u8| v.wrapping_mul(7)).collect();
|
||||
for (log_wd, o0, o1) in [(5, 0, 0), (0, 0, 0), (7, 0, 0), (3, 1, -2), (2, -1, 0)] {
|
||||
let w0 = 1 << log_wd;
|
||||
let mut fast = [0u8; 256];
|
||||
weighted_store(&mut fast, 16, Some(&a), Some(&b), 16, (16, 16), Weight::Weighted { log_wd, w0, w1: w0, o0, o1 });
|
||||
let mut want = [0u8; 256];
|
||||
for i in 0..256 {
|
||||
want[i] = clip1(((a[i] as i32 * w0 + b[i] as i32 * w0 + (1 << log_wd)) >> (log_wd + 1)) + ((o0 + o1 + 1) >> 1));
|
||||
}
|
||||
assert_eq!(fast, want, "logWD {log_wd}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,669 @@
|
||||
//! Intra prediction (8.3): Intra_4x4, Intra_8x8 (with reference filtering), Intra_16x16 and chroma.
|
||||
|
||||
/// Neighbouring sample availability for one block.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct Avail {
|
||||
pub left: bool,
|
||||
pub top: bool,
|
||||
pub top_left: bool,
|
||||
pub top_right: bool,
|
||||
}
|
||||
|
||||
/// Neighbouring samples of an NxN block in one line: p[-1, y] at `O - 1 - y` (y = 0..N-1),
|
||||
/// p[-1, -1] at `O`, p[x, -1] at `O + 1 + x` (x = 0..2N-1). The sample past each end repeats
|
||||
/// the last one, so the end cases of the three-tap formulas need no special handling.
|
||||
type Edge = [u8; 32];
|
||||
const O: usize = 9;
|
||||
|
||||
fn load_edge<const N: usize>(plane: &[u8], stride: usize, x: usize, y: usize, a: Avail) -> Edge {
|
||||
let mut e = [128u8; 32];
|
||||
if a.top {
|
||||
let row = &plane[(y - 1) * stride + x..];
|
||||
if a.top_right {
|
||||
e[O + 1..O + 1 + 2 * N].copy_from_slice(&row[..2 * N]);
|
||||
} else {
|
||||
e[O + 1..O + 1 + N].copy_from_slice(&row[..N]);
|
||||
let v = row[N - 1];
|
||||
e[O + 1 + N..O + 1 + 2 * N].fill(v);
|
||||
}
|
||||
e[O + 1 + 2 * N] = e[O + 2 * N];
|
||||
}
|
||||
if a.left {
|
||||
let col = &plane[y * stride + x - 1..];
|
||||
for i in 0..N {
|
||||
e[O - 1 - i] = col[i * stride];
|
||||
}
|
||||
e[O - 1 - N] = e[O - N];
|
||||
}
|
||||
if a.top_left {
|
||||
e[O] = plane[(y - 1) * stride + x - 1];
|
||||
}
|
||||
e
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn row_mut<const N: usize>(out: &mut [u8], stride: usize, y: usize) -> &mut [u8] {
|
||||
&mut out[y * stride..y * stride + N]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn tap3(e: &Edge, c: usize) -> u8 {
|
||||
((e[c - 1] as u16 + 2 * e[c] as u16 + e[c + 1] as u16 + 2) >> 2) as u8
|
||||
}
|
||||
|
||||
/// Directional / DC prediction shared by Intra_4x4 and Intra_8x8 (8.3.1.2.x, 8.3.2.2.x). Every
|
||||
/// directional sample is a two-tap (`f2`) or three-tap (`f3`) average along the edge line.
|
||||
fn pred_nxn<const N: usize>(mode: u8, e: &Edge, a: Avail, out: &mut [u8], stride: usize) {
|
||||
match mode {
|
||||
0 => {
|
||||
for y in 0..N {
|
||||
row_mut::<N>(out, stride, y).copy_from_slice(&e[O + 1..O + 1 + N]);
|
||||
}
|
||||
}
|
||||
1 => {
|
||||
for y in 0..N {
|
||||
row_mut::<N>(out, stride, y).fill(e[O - 1 - y]);
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
let n = N as u32;
|
||||
let shift = if N == 4 { 2 } else { 3 };
|
||||
let st: u32 = e[O + 1..O + 1 + N].iter().map(|&v| v as u32).sum();
|
||||
let sl: u32 = e[O - N..O].iter().map(|&v| v as u32).sum();
|
||||
let dc = match (a.top, a.left) {
|
||||
(true, true) => (st + sl + n) >> (shift + 1),
|
||||
(false, true) => (sl + (n >> 1)) >> shift,
|
||||
(true, false) => (st + (n >> 1)) >> shift,
|
||||
_ => 128,
|
||||
} as u8;
|
||||
for y in 0..N {
|
||||
row_mut::<N>(out, stride, y).fill(dc);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let mut f2 = [0u8; 32];
|
||||
let mut f3 = [0u8; 32];
|
||||
for c in 1..31 {
|
||||
f2[c] = ((e[c] as u16 + e[c + 1] as u16 + 1) >> 1) as u8;
|
||||
f3[c] = tap3(e, c);
|
||||
}
|
||||
match mode {
|
||||
3 => {
|
||||
for y in 0..N {
|
||||
row_mut::<N>(out, stride, y).copy_from_slice(&f3[O + 2 + y..O + 2 + y + N]);
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
for y in 0..N {
|
||||
row_mut::<N>(out, stride, y).copy_from_slice(&f3[O - y..O - y + N]);
|
||||
}
|
||||
}
|
||||
5 => {
|
||||
for y in 0..N {
|
||||
let r = row_mut::<N>(out, stride, y);
|
||||
for x in 0..N {
|
||||
let z = 2 * x as isize - y as isize;
|
||||
let i = O + x - (y >> 1);
|
||||
r[x] = if z < 0 {
|
||||
f3[(O as isize + 1 + z) as usize]
|
||||
} else if z % 2 == 0 {
|
||||
f2[i]
|
||||
} else {
|
||||
f3[i]
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
6 => {
|
||||
for y in 0..N {
|
||||
let r = row_mut::<N>(out, stride, y);
|
||||
for x in 0..N {
|
||||
let z = 2 * y as isize - x as isize;
|
||||
let i = O + (x >> 1) - y;
|
||||
r[x] = if z < 0 {
|
||||
f3[(O as isize - 1 - z) as usize]
|
||||
} else if z % 2 == 0 {
|
||||
f2[i - 1]
|
||||
} else {
|
||||
f3[i]
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
7 => {
|
||||
for y in 0..N {
|
||||
let s = if y % 2 == 0 { &f2[O + 1 + (y >> 1)..] } else { &f3[O + 2 + (y >> 1)..] };
|
||||
row_mut::<N>(out, stride, y).copy_from_slice(&s[..N]);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
for y in 0..N {
|
||||
let r = row_mut::<N>(out, stride, y);
|
||||
for x in 0..N {
|
||||
let z = x + 2 * y;
|
||||
r[x] = if z > 2 * N - 3 {
|
||||
e[O - N]
|
||||
} else {
|
||||
let i = O - 2 - y - (x >> 1);
|
||||
if z % 2 == 0 { f2[i] } else { f3[i] }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Intra_4x4 prediction written into `plane` at (x, y).
|
||||
pub fn pred4x4(plane: &mut [u8], stride: usize, x: usize, y: usize, mode: u8, a: Avail) {
|
||||
let e = load_edge::<4>(plane, stride, x, y, a);
|
||||
pred_nxn::<4>(mode, &e, a, &mut plane[y * stride + x..], stride);
|
||||
}
|
||||
|
||||
/// Intra_8x8 prediction (with reference sample filtering) written into `plane` at (x, y).
|
||||
pub fn pred8x8(plane: &mut [u8], stride: usize, x: usize, y: usize, mode: u8, a: Avail) {
|
||||
let p = load_edge::<8>(plane, stride, x, y, a);
|
||||
let mut f = p;
|
||||
let (tl, t0, t1, l0, l1) = (p[O] as u16, p[O + 1] as u16, p[O + 2] as u16, p[O - 1] as u16, p[O - 2] as u16);
|
||||
if a.top {
|
||||
for c in O + 1..O + 17 {
|
||||
f[c] = tap3(&p, c);
|
||||
}
|
||||
if !a.top_left {
|
||||
f[O + 1] = ((3 * t0 + t1 + 2) >> 2) as u8;
|
||||
}
|
||||
f[O + 17] = f[O + 16];
|
||||
}
|
||||
if a.top_left {
|
||||
f[O] = match (a.top, a.left) {
|
||||
(true, true) => tap3(&p, O),
|
||||
(true, false) => ((3 * tl + t0 + 2) >> 2) as u8,
|
||||
(false, true) => ((3 * tl + l0 + 2) >> 2) as u8,
|
||||
_ => p[O],
|
||||
};
|
||||
}
|
||||
if a.left {
|
||||
for c in O - 8..O {
|
||||
f[c] = tap3(&p, c);
|
||||
}
|
||||
if !a.top_left {
|
||||
f[O - 1] = ((3 * l0 + l1 + 2) >> 2) as u8;
|
||||
}
|
||||
f[O - 9] = f[O - 8];
|
||||
}
|
||||
pred_nxn::<8>(mode, &f, a, &mut plane[y * stride + x..], stride);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn clip(v: i32) -> u8 {
|
||||
v.clamp(0, 255) as u8
|
||||
}
|
||||
|
||||
/// Intra_16x16 prediction (8.3.3) for the macroblock at luma (x, y).
|
||||
pub fn pred16x16(plane: &mut [u8], stride: usize, x: usize, y: usize, mode: u8, a: Avail) {
|
||||
let mut top = [0i32; 16];
|
||||
let mut left = [0i32; 16];
|
||||
if a.top {
|
||||
for i in 0..16 {
|
||||
top[i] = plane[(y - 1) * stride + x + i] as i32;
|
||||
}
|
||||
}
|
||||
if a.left {
|
||||
for i in 0..16 {
|
||||
left[i] = plane[(y + i) * stride + x - 1] as i32;
|
||||
}
|
||||
}
|
||||
let base = y * stride + x;
|
||||
match mode {
|
||||
0 => {
|
||||
let t: [u8; 16] = top.map(|v| v as u8);
|
||||
for r in 0..16 {
|
||||
row_mut::<16>(&mut plane[base..], stride, r).copy_from_slice(&t);
|
||||
}
|
||||
}
|
||||
1 => {
|
||||
for r in 0..16 {
|
||||
plane[base + r * stride..base + r * stride + 16].fill(left[r] as u8);
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
let st: i32 = top.iter().sum();
|
||||
let sl: i32 = left.iter().sum();
|
||||
let dc = match (a.top, a.left) {
|
||||
(true, true) => (st + sl + 16) >> 5,
|
||||
(false, true) => (sl + 8) >> 4,
|
||||
(true, false) => (st + 8) >> 4,
|
||||
_ => 128,
|
||||
} as u8;
|
||||
for r in 0..16 {
|
||||
plane[base + r * stride..base + r * stride + 16].fill(dc);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let tl = if a.top_left { plane[(y - 1) * stride + x - 1] as i32 } else { 128 };
|
||||
let t = |i: i32| if i < 0 { tl } else { top[i as usize] };
|
||||
let l = |i: i32| if i < 0 { tl } else { left[i as usize] };
|
||||
let mut h = 0;
|
||||
let mut v = 0;
|
||||
for k in 0..8 {
|
||||
h += (k + 1) * (t(8 + k) - t(6 - k));
|
||||
v += (k + 1) * (l(8 + k) - l(6 - k));
|
||||
}
|
||||
let aa = 16 * (left[15] + top[15]);
|
||||
let b = (5 * h + 32) >> 6;
|
||||
let c = (5 * v + 32) >> 6;
|
||||
for r in 0..16 {
|
||||
let row = row_mut::<16>(&mut plane[base..], stride, r);
|
||||
for (cc, v) in row.iter_mut().enumerate() {
|
||||
*v = clip((aa + b * (cc as i32 - 7) + c * (r as i32 - 7) + 16) >> 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Chroma intra prediction for a 4:2:0 macroblock (8x8 block at chroma (x, y)), 8.3.4.
|
||||
/// Modes: 0 DC, 1 horizontal, 2 vertical, 3 plane.
|
||||
pub fn pred_chroma(plane: &mut [u8], stride: usize, x: usize, y: usize, mode: u8, a: Avail) {
|
||||
const W: usize = 8;
|
||||
const H: usize = 8;
|
||||
let mut top = [0i32; W];
|
||||
let mut left = [0i32; H];
|
||||
if a.top {
|
||||
for i in 0..W {
|
||||
top[i] = plane[(y - 1) * stride + x + i] as i32;
|
||||
}
|
||||
}
|
||||
if a.left {
|
||||
for i in 0..H {
|
||||
left[i] = plane[(y + i) * stride + x - 1] as i32;
|
||||
}
|
||||
}
|
||||
let base = y * stride + x;
|
||||
match mode {
|
||||
0 => {
|
||||
for by in 0..H / 4 {
|
||||
for bx in 0..W / 4 {
|
||||
let st: i32 = top[bx * 4..bx * 4 + 4].iter().sum();
|
||||
let sl: i32 = left[by * 4..by * 4 + 4].iter().sum();
|
||||
let dc = if (bx == 0 && by == 0) || (bx > 0 && by > 0) {
|
||||
match (a.top, a.left) {
|
||||
(true, true) => (st + sl + 4) >> 3,
|
||||
(false, true) => (sl + 2) >> 2,
|
||||
(true, false) => (st + 2) >> 2,
|
||||
_ => 128,
|
||||
}
|
||||
} else if bx > 0 {
|
||||
if a.top {
|
||||
(st + 2) >> 2
|
||||
} else if a.left {
|
||||
(sl + 2) >> 2
|
||||
} else {
|
||||
128
|
||||
}
|
||||
} else if a.left {
|
||||
(sl + 2) >> 2
|
||||
} else if a.top {
|
||||
(st + 2) >> 2
|
||||
} else {
|
||||
128
|
||||
};
|
||||
for r in 0..4 {
|
||||
let o = base + (by * 4 + r) * stride + bx * 4;
|
||||
plane[o..o + 4].fill(dc as u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1 => {
|
||||
for r in 0..H {
|
||||
plane[base + r * stride..base + r * stride + W].fill(left[r] as u8);
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
let t: [u8; W] = top.map(|v| v as u8);
|
||||
for r in 0..H {
|
||||
row_mut::<W>(&mut plane[base..], stride, r).copy_from_slice(&t);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let tl = if a.top_left { plane[(y - 1) * stride + x - 1] as i32 } else { 128 };
|
||||
let t = |i: i32| if i < 0 { tl } else { top[i as usize] };
|
||||
let l = |i: i32| if i < 0 { tl } else { left[i as usize] };
|
||||
let mut h = 0;
|
||||
let mut v = 0;
|
||||
for k in 0..4 {
|
||||
h += (k + 1) * (t(4 + k) - t(2 - k));
|
||||
v += (k + 1) * (l(4 + k) - l(2 - k));
|
||||
}
|
||||
let aa = 16 * (left[H - 1] + top[W - 1]);
|
||||
let b = (34 * h + 32) >> 6;
|
||||
let c = (34 * v + 32) >> 6;
|
||||
for r in 0..H {
|
||||
let row = row_mut::<W>(&mut plane[base..], stride, r);
|
||||
for (cc, v) in row.iter_mut().enumerate() {
|
||||
*v = clip((aa + b * (cc as i32 - 3) + c * (r as i32 - 3) + 16) >> 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The per-sample spec formulas that [`pred_nxn`] replaces, for comparison in tests.
|
||||
#[cfg(test)]
|
||||
mod reference {
|
||||
use super::Avail;
|
||||
|
||||
/// Neighbouring samples of an NxN block: p[x,-1] for x = 0..2N-1, p[-1,y] for y = 0..N-1, p[-1,-1].
|
||||
#[derive(Clone, Copy)]
|
||||
struct Edge {
|
||||
top: [i32; 16],
|
||||
left: [i32; 8],
|
||||
tl: i32,
|
||||
}
|
||||
|
||||
impl Edge {
|
||||
/// p[x, -1] for x >= -1
|
||||
#[inline(always)]
|
||||
fn t(&self, x: i32) -> i32 {
|
||||
if x < 0 { self.tl } else { self.top[x as usize] }
|
||||
}
|
||||
/// p[-1, y] for y >= -1
|
||||
#[inline(always)]
|
||||
fn l(&self, y: i32) -> i32 {
|
||||
if y < 0 { self.tl } else { self.left[y as usize] }
|
||||
}
|
||||
}
|
||||
|
||||
fn load_edge(plane: &[u8], stride: usize, x: usize, y: usize, n: usize, a: Avail) -> Edge {
|
||||
let mut e = Edge { top: [128; 16], left: [128; 8], tl: 128 };
|
||||
if a.top {
|
||||
let row = &plane[(y - 1) * stride + x..];
|
||||
for i in 0..n {
|
||||
e.top[i] = row[i] as i32;
|
||||
}
|
||||
if a.top_right {
|
||||
for i in n..2 * n {
|
||||
e.top[i] = row[i] as i32;
|
||||
}
|
||||
} else {
|
||||
let v = e.top[n - 1];
|
||||
for t in &mut e.top[n..2 * n] {
|
||||
*t = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
if a.left {
|
||||
for i in 0..n {
|
||||
e.left[i] = plane[(y + i) * stride + x - 1] as i32;
|
||||
}
|
||||
}
|
||||
if a.top_left {
|
||||
e.tl = plane[(y - 1) * stride + x - 1] as i32;
|
||||
}
|
||||
e
|
||||
}
|
||||
|
||||
/// Fill an `n`x`n` block at `out` with `f(x, y)`.
|
||||
fn fill_nxn(n: i32, out: &mut [u8], stride: usize, f: impl Fn(i32, i32) -> i32) {
|
||||
for y in 0..n {
|
||||
for x in 0..n {
|
||||
out[y as usize * stride + x as usize] = f(x, y) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// DC value of Intra_NxN prediction (mode 2).
|
||||
fn dc_nxn(e: &Edge, n: i32, a: Avail) -> i32 {
|
||||
let shift = if n == 4 { 2 } else { 3 };
|
||||
let st: i32 = (0..n).map(|x| e.t(x)).sum();
|
||||
let sl: i32 = (0..n).map(|y| e.l(y)).sum();
|
||||
match (a.top, a.left) {
|
||||
(true, true) => (st + sl + n) >> (shift + 1),
|
||||
(false, true) => (sl + (n >> 1)) >> shift,
|
||||
(true, false) => (st + (n >> 1)) >> shift,
|
||||
_ => 128,
|
||||
}
|
||||
}
|
||||
|
||||
/// Diagonal down-left sample (mode 3).
|
||||
fn diag_down_left(e: &Edge, n: i32, x: i32, y: i32) -> i32 {
|
||||
if x == n - 1 && y == n - 1 {
|
||||
(e.t(2 * n - 2) + 3 * e.t(2 * n - 1) + 2) >> 2
|
||||
} else {
|
||||
(e.t(x + y) + 2 * e.t(x + y + 1) + e.t(x + y + 2) + 2) >> 2
|
||||
}
|
||||
}
|
||||
|
||||
/// Diagonal down-right sample (mode 4).
|
||||
fn diag_down_right(e: &Edge, x: i32, y: i32) -> i32 {
|
||||
if x > y {
|
||||
(e.t(x - y - 2) + 2 * e.t(x - y - 1) + e.t(x - y) + 2) >> 2
|
||||
} else if x < y {
|
||||
(e.l(y - x - 2) + 2 * e.l(y - x - 1) + e.l(y - x) + 2) >> 2
|
||||
} else {
|
||||
(e.t(0) + 2 * e.tl + e.l(0) + 2) >> 2
|
||||
}
|
||||
}
|
||||
|
||||
/// Vertical-right sample (mode 5).
|
||||
fn vertical_right(e: &Edge, x: i32, y: i32) -> i32 {
|
||||
let z = 2 * x - y;
|
||||
if z >= 0 && z % 2 == 0 {
|
||||
(e.t(x - (y >> 1) - 1) + e.t(x - (y >> 1)) + 1) >> 1
|
||||
} else if z > 0 {
|
||||
(e.t(x - (y >> 1) - 2) + 2 * e.t(x - (y >> 1) - 1) + e.t(x - (y >> 1)) + 2) >> 2
|
||||
} else if z == -1 {
|
||||
(e.l(0) + 2 * e.tl + e.t(0) + 2) >> 2
|
||||
} else {
|
||||
(e.l(y - 2 * x - 1) + 2 * e.l(y - 2 * x - 2) + e.l(y - 2 * x - 3) + 2) >> 2
|
||||
}
|
||||
}
|
||||
|
||||
/// Horizontal-down sample (mode 6).
|
||||
fn horizontal_down(e: &Edge, x: i32, y: i32) -> i32 {
|
||||
let z = 2 * y - x;
|
||||
if z >= 0 && z % 2 == 0 {
|
||||
(e.l(y - (x >> 1) - 1) + e.l(y - (x >> 1)) + 1) >> 1
|
||||
} else if z > 0 {
|
||||
(e.l(y - (x >> 1) - 2) + 2 * e.l(y - (x >> 1) - 1) + e.l(y - (x >> 1)) + 2) >> 2
|
||||
} else if z == -1 {
|
||||
(e.l(0) + 2 * e.tl + e.t(0) + 2) >> 2
|
||||
} else {
|
||||
(e.t(x - 2 * y - 1) + 2 * e.t(x - 2 * y - 2) + e.t(x - 2 * y - 3) + 2) >> 2
|
||||
}
|
||||
}
|
||||
|
||||
/// Vertical-left sample (mode 7).
|
||||
fn vertical_left(e: &Edge, x: i32, y: i32) -> i32 {
|
||||
let i = x + (y >> 1);
|
||||
if y % 2 == 0 { (e.t(i) + e.t(i + 1) + 1) >> 1 } else { (e.t(i) + 2 * e.t(i + 1) + e.t(i + 2) + 2) >> 2 }
|
||||
}
|
||||
|
||||
/// Horizontal-up sample (mode 8).
|
||||
fn horizontal_up(e: &Edge, n: i32, x: i32, y: i32) -> i32 {
|
||||
let zmax = 2 * n - 3;
|
||||
let z = x + 2 * y;
|
||||
let i = y + (x >> 1);
|
||||
if z < zmax && z % 2 == 0 {
|
||||
(e.l(i) + e.l(i + 1) + 1) >> 1
|
||||
} else if z < zmax {
|
||||
(e.l(i) + 2 * e.l(i + 1) + e.l(i + 2) + 2) >> 2
|
||||
} else if z == zmax {
|
||||
(e.l(n - 2) + 3 * e.l(n - 1) + 2) >> 2
|
||||
} else {
|
||||
e.l(n - 1)
|
||||
}
|
||||
}
|
||||
|
||||
/// Directional / DC prediction shared by Intra_4x4 and Intra_8x8 (8.3.1.2.x, 8.3.2.2.x).
|
||||
fn pred_nxn(mode: u8, e: &Edge, n: i32, a: Avail, out: &mut [u8], stride: usize) {
|
||||
match mode {
|
||||
0 => fill_nxn(n, out, stride, |x, _| e.t(x)),
|
||||
1 => fill_nxn(n, out, stride, |_, y| e.l(y)),
|
||||
2 => {
|
||||
let dc = dc_nxn(e, n, a);
|
||||
fill_nxn(n, out, stride, |_, _| dc);
|
||||
}
|
||||
3 => fill_nxn(n, out, stride, |x, y| diag_down_left(e, n, x, y)),
|
||||
4 => fill_nxn(n, out, stride, |x, y| diag_down_right(e, x, y)),
|
||||
5 => fill_nxn(n, out, stride, |x, y| vertical_right(e, x, y)),
|
||||
6 => fill_nxn(n, out, stride, |x, y| horizontal_down(e, x, y)),
|
||||
7 => fill_nxn(n, out, stride, |x, y| vertical_left(e, x, y)),
|
||||
_ => fill_nxn(n, out, stride, |x, y| horizontal_up(e, n, x, y)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Intra_4x4 prediction written into `plane` at (x, y).
|
||||
pub fn pred4x4(plane: &mut [u8], stride: usize, x: usize, y: usize, mode: u8, a: Avail) {
|
||||
let e = load_edge(plane, stride, x, y, 4, a);
|
||||
pred_nxn(mode, &e, 4, a, &mut plane[y * stride + x..], stride);
|
||||
}
|
||||
|
||||
/// Intra_8x8 prediction (with reference sample filtering) written into `plane` at (x, y).
|
||||
pub fn pred8x8(plane: &mut [u8], stride: usize, x: usize, y: usize, mode: u8, a: Avail) {
|
||||
let p = load_edge(plane, stride, x, y, 8, a);
|
||||
let mut f = p;
|
||||
if a.top {
|
||||
f.top[0] = if a.top_left { (p.tl + 2 * p.top[0] + p.top[1] + 2) >> 2 } else { (3 * p.top[0] + p.top[1] + 2) >> 2 };
|
||||
for i in 1..15 {
|
||||
f.top[i] = (p.top[i - 1] + 2 * p.top[i] + p.top[i + 1] + 2) >> 2;
|
||||
}
|
||||
f.top[15] = (p.top[14] + 3 * p.top[15] + 2) >> 2;
|
||||
}
|
||||
if a.top_left {
|
||||
f.tl = match (a.top, a.left) {
|
||||
(true, true) => (p.top[0] + 2 * p.tl + p.left[0] + 2) >> 2,
|
||||
(true, false) => (3 * p.tl + p.top[0] + 2) >> 2,
|
||||
(false, true) => (3 * p.tl + p.left[0] + 2) >> 2,
|
||||
_ => p.tl,
|
||||
};
|
||||
}
|
||||
if a.left {
|
||||
f.left[0] = if a.top_left { (p.tl + 2 * p.left[0] + p.left[1] + 2) >> 2 } else { (3 * p.left[0] + p.left[1] + 2) >> 2 };
|
||||
for i in 1..7 {
|
||||
f.left[i] = (p.left[i - 1] + 2 * p.left[i] + p.left[i + 1] + 2) >> 2;
|
||||
}
|
||||
f.left[7] = (p.left[6] + 3 * p.left[7] + 2) >> 2;
|
||||
}
|
||||
pred_nxn(mode, &f, 8, a, &mut plane[y * stride + x..], stride);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn setup() -> (Vec<u8>, usize) {
|
||||
// 12x12 plane; block at (4,4); top row y=3 = 10,20,..; left column x=3 = 1,2,3,...
|
||||
let stride = 16;
|
||||
let mut p = vec![0u8; stride * 16];
|
||||
for i in 0..12 {
|
||||
p[3 * stride + 4 + i] = (10 * (i + 1)) as u8;
|
||||
}
|
||||
for i in 0..8 {
|
||||
p[(4 + i) * stride + 3] = (i + 1) as u8;
|
||||
}
|
||||
p[3 * stride + 3] = 5;
|
||||
(p, stride)
|
||||
}
|
||||
const ALL: Avail = Avail { left: true, top: true, top_left: true, top_right: true };
|
||||
|
||||
/// Intra_4x4 and Intra_8x8 match the per-sample spec formulas for every mode and every
|
||||
/// neighbour availability on random samples.
|
||||
#[test]
|
||||
fn nxn_matches_reference() {
|
||||
let mut rng = 0x2545_f491_4f6c_dd1du64;
|
||||
let mut next = move || {
|
||||
rng ^= rng << 13;
|
||||
rng ^= rng >> 7;
|
||||
rng ^= rng << 17;
|
||||
rng
|
||||
};
|
||||
let s = 32;
|
||||
for iter in 0..4000 {
|
||||
let base: Vec<u8> = (0..s * 32).map(|_| next() as u8).collect();
|
||||
let bits = iter % 16;
|
||||
let a = Avail { left: bits & 1 != 0, top: bits & 2 != 0, top_left: bits & 4 != 0, top_right: bits & 8 != 0 };
|
||||
for mode in 0..9 {
|
||||
let (mut got, mut want) = (base.clone(), base.clone());
|
||||
if iter % 2 == 0 {
|
||||
pred4x4(&mut got, s, 8, 8, mode, a);
|
||||
reference::pred4x4(&mut want, s, 8, 8, mode, a);
|
||||
} else {
|
||||
pred8x8(&mut got, s, 8, 8, mode, a);
|
||||
reference::pred8x8(&mut want, s, 8, 8, mode, a);
|
||||
}
|
||||
assert_eq!(got, want, "iteration {iter}: mode {mode} {a:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i4_vertical_horizontal_dc() {
|
||||
let (mut p, s) = setup();
|
||||
pred4x4(&mut p, s, 4, 4, 0, ALL);
|
||||
assert_eq!(&p[4 * s + 4..4 * s + 8], &[10, 20, 30, 40]);
|
||||
assert_eq!(&p[7 * s + 4..7 * s + 8], &[10, 20, 30, 40]);
|
||||
pred4x4(&mut p, s, 4, 4, 1, ALL);
|
||||
assert_eq!(&p[5 * s + 4..5 * s + 8], &[2, 2, 2, 2]);
|
||||
pred4x4(&mut p, s, 4, 4, 2, ALL);
|
||||
// (10+20+30+40 + 1+2+3+4 + 4) >> 3 = 114 >> 3 = 14
|
||||
assert_eq!(p[4 * s + 4], 14);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i4_diag_down_left_hand_computed() {
|
||||
let (mut p, s) = setup();
|
||||
pred4x4(&mut p, s, 4, 4, 3, ALL);
|
||||
// pred[0,0] = (t0 + 2 t1 + t2 + 2) >> 2 = (10 + 40 + 30 + 2) >> 2 = 20
|
||||
assert_eq!(p[4 * s + 4], 20);
|
||||
// pred[3,3] = (t6 + 3 t7 + 2) >> 2 = (70 + 240 + 2) >> 2 = 78
|
||||
assert_eq!(p[7 * s + 7], 78);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i4_diag_down_right_hand_computed() {
|
||||
let (mut p, s) = setup();
|
||||
pred4x4(&mut p, s, 4, 4, 4, ALL);
|
||||
// x==y: (t0 + 2 tl + l0 + 2) >> 2 = (10 + 10 + 1 + 2) >> 2 = 5
|
||||
assert_eq!(p[4 * s + 4], 5);
|
||||
// x=1,y=0: (tl + 2 t0 + t1 + 2)>>2 = (5 + 20 + 20 + 2) >> 2 = 11
|
||||
assert_eq!(p[4 * s + 5], 11);
|
||||
// x=0,y=1: (tl + 2 l0 + l1 + 2) >> 2 = (5 + 2 + 2 + 2) >> 2 = 2
|
||||
assert_eq!(p[5 * s + 4], 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i4_top_right_substitution() {
|
||||
let (mut p, s) = setup();
|
||||
let a = Avail { top_right: false, ..ALL };
|
||||
pred4x4(&mut p, s, 4, 4, 3, a);
|
||||
// top-right replaced by t3 = 40: pred[3,3] = (40 + 120 + 2) >> 2 = 40
|
||||
assert_eq!(p[7 * s + 7], 40);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i16_plane_flat_is_flat() {
|
||||
let s = 32;
|
||||
let mut p = vec![77u8; s * 32];
|
||||
pred16x16(&mut p, s, 8, 8, 3, ALL);
|
||||
assert!(p[8 * s + 8..8 * s + 24].iter().all(|&v| v == 77));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chroma_dc_rules() {
|
||||
let s = 16;
|
||||
let mut p = vec![0u8; s * 16];
|
||||
for i in 0..8 {
|
||||
p[3 * s + 4 + i] = if i < 4 { 40 } else { 80 };
|
||||
p[(4 + i) * s + 3] = if i < 4 { 8 } else { 16 };
|
||||
}
|
||||
pred_chroma(&mut p, s, 4, 4, 0, ALL);
|
||||
assert_eq!(p[4 * s + 4], (160 + 32 + 4) >> 3); // top-left block: both
|
||||
assert_eq!(p[4 * s + 8], 80); // top-right block: top only
|
||||
assert_eq!(p[8 * s + 4], 16); // bottom-left: left only
|
||||
assert_eq!(p[8 * s + 8], ((320 + 64 + 4) >> 3) as u8); // bottom-right: both
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
//! Macroblock / sub-macroblock type tables (7.4.5, Tables 7-11 .. 7-18).
|
||||
|
||||
use crate::h264::decode::picture::MbKind;
|
||||
|
||||
pub const PRED_L0: u8 = 1;
|
||||
pub const PRED_L1: u8 = 2;
|
||||
pub const PRED_BI: u8 = 3;
|
||||
|
||||
/// Macroblock partitioning for inter macroblocks.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||||
pub enum Part {
|
||||
#[default]
|
||||
P16x16,
|
||||
P16x8,
|
||||
P8x16,
|
||||
P8x8,
|
||||
}
|
||||
|
||||
impl Part {
|
||||
pub fn num_parts(self) -> usize {
|
||||
match self {
|
||||
Part::P16x16 => 1,
|
||||
Part::P16x8 | Part::P8x16 => 2,
|
||||
Part::P8x8 => 4,
|
||||
}
|
||||
}
|
||||
/// (x, y, w, h) of partition `i` in 4x4-block units.
|
||||
pub fn rect(self, i: usize) -> (usize, usize, usize, usize) {
|
||||
match self {
|
||||
Part::P16x16 => (0, 0, 4, 4),
|
||||
Part::P16x8 => (0, 2 * i, 4, 2),
|
||||
Part::P8x16 => (2 * i, 0, 2, 4),
|
||||
Part::P8x8 => ((i & 1) * 2, (i >> 1) * 2, 2, 2),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sub-macroblock partitioning.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||||
pub enum SubPart {
|
||||
#[default]
|
||||
S8x8,
|
||||
S8x4,
|
||||
S4x8,
|
||||
S4x4,
|
||||
}
|
||||
|
||||
impl SubPart {
|
||||
pub fn num_parts(self) -> usize {
|
||||
match self {
|
||||
SubPart::S8x8 => 1,
|
||||
SubPart::S8x4 | SubPart::S4x8 => 2,
|
||||
SubPart::S4x4 => 4,
|
||||
}
|
||||
}
|
||||
/// (x, y, w, h) of sub-partition `j` relative to the 8x8 block, in 4x4 units.
|
||||
pub fn rect(self, j: usize) -> (usize, usize, usize, usize) {
|
||||
match self {
|
||||
SubPart::S8x8 => (0, 0, 2, 2),
|
||||
SubPart::S8x4 => (0, j, 2, 1),
|
||||
SubPart::S4x8 => (j, 0, 1, 2),
|
||||
SubPart::S4x4 => (j & 1, j >> 1, 1, 1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Decoded meaning of mb_type.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct MbTypeInfo {
|
||||
pub kind: MbKind,
|
||||
/// Intra16x16PredMode.
|
||||
pub i16_mode: u8,
|
||||
/// coded_block_pattern implied by an Intra_16x16 mb_type.
|
||||
pub i16_cbp: u8,
|
||||
pub part: Part,
|
||||
/// Prediction list flags per macroblock partition.
|
||||
pub pred: [u8; 2],
|
||||
/// P_8x8ref0.
|
||||
pub ref0: bool,
|
||||
}
|
||||
|
||||
/// Intra mb_type (I-slice numbering 0..=25).
|
||||
pub fn intra_mb_type(t: u32) -> Option<MbTypeInfo> {
|
||||
let mut m = MbTypeInfo::default();
|
||||
match t {
|
||||
0 => m.kind = MbKind::I4x4, // or I8x8, decided by transform_size_8x8_flag
|
||||
1..=24 => {
|
||||
m.kind = MbKind::I16x16;
|
||||
m.i16_mode = ((t - 1) % 4) as u8;
|
||||
let chroma = ((t - 1) / 4) % 3;
|
||||
let luma = if t >= 13 { 15 } else { 0 };
|
||||
m.i16_cbp = (luma | (chroma << 4)) as u8;
|
||||
}
|
||||
25 => m.kind = MbKind::IPcm,
|
||||
_ => return None,
|
||||
}
|
||||
Some(m)
|
||||
}
|
||||
|
||||
/// P-slice mb_type 0..=4 (5.. are intra, handled by the caller).
|
||||
pub fn p_mb_type(t: u32) -> Option<MbTypeInfo> {
|
||||
let mut m = MbTypeInfo { kind: MbKind::Inter, pred: [PRED_L0; 2], ..Default::default() };
|
||||
m.part = match t {
|
||||
0 => Part::P16x16,
|
||||
1 => Part::P16x8,
|
||||
2 => Part::P8x16,
|
||||
3 => Part::P8x8,
|
||||
4 => {
|
||||
m.ref0 = true;
|
||||
Part::P8x8
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
Some(m)
|
||||
}
|
||||
|
||||
/// B-slice mb_type 0..=22 (23.. are intra).
|
||||
pub fn b_mb_type(t: u32) -> Option<MbTypeInfo> {
|
||||
let mut m = MbTypeInfo { kind: MbKind::Inter, ..Default::default() };
|
||||
match t {
|
||||
0 => {
|
||||
m.kind = MbKind::BDirect16x16;
|
||||
m.pred = [PRED_BI; 2];
|
||||
}
|
||||
1..=3 => {
|
||||
m.part = Part::P16x16;
|
||||
m.pred = [t as u8; 2];
|
||||
}
|
||||
4..=21 => {
|
||||
const PAIRS: [(u8, u8); 9] = [
|
||||
(PRED_L0, PRED_L0),
|
||||
(PRED_L1, PRED_L1),
|
||||
(PRED_L0, PRED_L1),
|
||||
(PRED_L1, PRED_L0),
|
||||
(PRED_L0, PRED_BI),
|
||||
(PRED_L1, PRED_BI),
|
||||
(PRED_BI, PRED_L0),
|
||||
(PRED_BI, PRED_L1),
|
||||
(PRED_BI, PRED_BI),
|
||||
];
|
||||
let (a, b) = PAIRS[((t - 4) / 2) as usize];
|
||||
m.pred = [a, b];
|
||||
m.part = if t.is_multiple_of(2) { Part::P16x8 } else { Part::P8x16 };
|
||||
}
|
||||
22 => m.part = Part::P8x8,
|
||||
_ => return None,
|
||||
}
|
||||
Some(m)
|
||||
}
|
||||
|
||||
/// Sub-macroblock type: (shape, prediction flags, direct).
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct SubMbInfo {
|
||||
pub shape: SubPart,
|
||||
pub pred: u8,
|
||||
pub direct: bool,
|
||||
}
|
||||
|
||||
pub fn p_sub_mb_type(t: u32) -> Option<SubMbInfo> {
|
||||
let shape = match t {
|
||||
0 => SubPart::S8x8,
|
||||
1 => SubPart::S8x4,
|
||||
2 => SubPart::S4x8,
|
||||
3 => SubPart::S4x4,
|
||||
_ => return None,
|
||||
};
|
||||
Some(SubMbInfo { shape, pred: PRED_L0, direct: false })
|
||||
}
|
||||
|
||||
pub fn b_sub_mb_type(t: u32) -> Option<SubMbInfo> {
|
||||
let (shape, pred, direct) = match t {
|
||||
0 => (SubPart::S8x8, PRED_BI, true),
|
||||
1 => (SubPart::S8x8, PRED_L0, false),
|
||||
2 => (SubPart::S8x8, PRED_L1, false),
|
||||
3 => (SubPart::S8x8, PRED_BI, false),
|
||||
4 => (SubPart::S8x4, PRED_L0, false),
|
||||
5 => (SubPart::S4x8, PRED_L0, false),
|
||||
6 => (SubPart::S8x4, PRED_L1, false),
|
||||
7 => (SubPart::S4x8, PRED_L1, false),
|
||||
8 => (SubPart::S8x4, PRED_BI, false),
|
||||
9 => (SubPart::S4x8, PRED_BI, false),
|
||||
10 => (SubPart::S4x4, PRED_L0, false),
|
||||
11 => (SubPart::S4x4, PRED_L1, false),
|
||||
12 => (SubPart::S4x4, PRED_BI, false),
|
||||
_ => return None,
|
||||
};
|
||||
Some(SubMbInfo { shape, pred, direct })
|
||||
}
|
||||
|
||||
/// Table 9-4 (a): codeNum -> coded_block_pattern for (Intra_4x4/Intra_8x8, Inter), ChromaArrayType 1 or 2.
|
||||
#[rustfmt::skip]
|
||||
pub const CBP_ME: [(u8, u8); 48] = [
|
||||
(47, 0), (31, 16), (15, 1), (0, 2), (23, 4), (27, 8), (29, 32), (30, 3),
|
||||
(7, 5), (11, 10), (13, 12), (14, 15), (39, 47), (43, 7), (45, 11), (46, 13),
|
||||
(16, 14), (3, 6), (5, 9), (10, 31), (12, 35), (19, 37), (21, 42), (26, 44),
|
||||
(28, 33), (35, 34), (37, 36), (42, 40), (44, 39), (1, 43), (2, 45), (4, 46),
|
||||
(8, 17), (17, 18), (18, 20), (20, 24), (24, 19), (6, 21), (9, 26), (22, 28),
|
||||
(25, 23), (32, 27), (33, 29), (34, 30), (36, 22), (40, 25), (38, 38), (41, 41),
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cbp_table_is_permutation() {
|
||||
let mut a = [false; 48];
|
||||
let mut b = [false; 48];
|
||||
for &(i, p) in &CBP_ME {
|
||||
a[i as usize] = true;
|
||||
b[p as usize] = true;
|
||||
}
|
||||
assert!(a.iter().all(|&x| x) && b.iter().all(|&x| x));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i16_types() {
|
||||
let m = intra_mb_type(1).unwrap();
|
||||
assert_eq!((m.kind, m.i16_mode, m.i16_cbp), (MbKind::I16x16, 0, 0));
|
||||
let m = intra_mb_type(24).unwrap();
|
||||
assert_eq!((m.i16_mode, m.i16_cbp), (3, 15 | (2 << 4)));
|
||||
let m = intra_mb_type(13).unwrap();
|
||||
assert_eq!((m.i16_mode, m.i16_cbp), (0, 15));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn b_types() {
|
||||
let m = b_mb_type(12).unwrap();
|
||||
assert_eq!((m.part, m.pred), (Part::P16x8, [PRED_L0, PRED_BI]));
|
||||
let m = b_mb_type(21).unwrap();
|
||||
assert_eq!((m.part, m.pred), (Part::P8x16, [PRED_BI, PRED_BI]));
|
||||
let m = b_mb_type(11).unwrap();
|
||||
assert_eq!((m.part, m.pred), (Part::P8x16, [PRED_L1, PRED_L0]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
//! Clean-room pure-Rust H.264 / AVC decoder, implemented from ITU-T Rec. H.264 (ISO/IEC 14496-10).
|
||||
//!
|
||||
//! Supported: progressive (frame) coding, 8-bit 4:2:0, Baseline / Main / High profiles — CAVLC and CABAC,
|
||||
//! I/P/B slices, 8x8 transform, custom scaling matrices, weighted prediction, spatial/temporal direct,
|
||||
//! multiple slices, long-term references and all MMCOs, deblocking.
|
||||
//!
|
||||
//! ```no_run
|
||||
//! let mut dec = craft_video::h264::decode::Decoder::new();
|
||||
//! let stream = std::fs::read("video.h264").unwrap();
|
||||
//! for pic in dec.decode(&stream, 0).unwrap() {
|
||||
//! println!("{}x{} poc {}", pic.width, pic.height, pic.poc);
|
||||
//! }
|
||||
//! for pic in dec.flush() {
|
||||
//! let _ = pic;
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
#![allow(clippy::needless_range_loop, clippy::type_complexity)]
|
||||
|
||||
mod cabac;
|
||||
mod cabac_mb;
|
||||
#[rustfmt::skip]
|
||||
mod cabac_tables;
|
||||
mod cavlc;
|
||||
#[rustfmt::skip]
|
||||
mod cavlc_tables;
|
||||
mod deblock;
|
||||
mod decoder;
|
||||
pub mod dpb;
|
||||
mod error;
|
||||
mod inter;
|
||||
mod intra;
|
||||
mod mbtypes;
|
||||
pub mod params;
|
||||
mod picture;
|
||||
pub use picture::set_plane_allocator;
|
||||
pub mod slice;
|
||||
mod slicedec;
|
||||
#[cfg(test)]
|
||||
mod synth_tests;
|
||||
mod tables;
|
||||
mod transform;
|
||||
|
||||
pub use decoder::{DecodeStats, Decoder, default_threads};
|
||||
pub use error::{Error, Result};
|
||||
|
||||
/// Colour description from the VUI (ITU-T H.273 code points).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct ColorInfo {
|
||||
/// `video_full_range_flag`.
|
||||
pub full_range: bool,
|
||||
pub primaries: u8,
|
||||
pub transfer: u8,
|
||||
pub matrix: u8,
|
||||
}
|
||||
|
||||
/// A decoded, cropped picture in 8-bit planar 4:2:0.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Picture {
|
||||
/// Cropped luma width.
|
||||
pub width: u32,
|
||||
/// Cropped luma height.
|
||||
pub height: u32,
|
||||
pub chroma_width: u32,
|
||||
pub chroma_height: u32,
|
||||
pub y: Vec<u8>,
|
||||
pub u: Vec<u8>,
|
||||
pub v: Vec<u8>,
|
||||
pub y_stride: usize,
|
||||
pub uv_stride: usize,
|
||||
/// Presentation timestamp passed to [`Decoder::decode`] with the access unit of this picture.
|
||||
pub pts: i64,
|
||||
/// Picture order count.
|
||||
pub poc: i32,
|
||||
/// IDR picture.
|
||||
pub key: bool,
|
||||
pub color: ColorInfo,
|
||||
/// Sample aspect ratio (0, 0 when unspecified).
|
||||
pub sar: (u16, u16),
|
||||
/// Decoded in draft mode ([`Decoder::set_draft`]): a non-reference picture whose deblocking
|
||||
/// filter was skipped. Its samples are approximate (not the conforming output); no other
|
||||
/// picture is affected.
|
||||
pub draft: bool,
|
||||
}
|
||||
@@ -0,0 +1,790 @@
|
||||
//! Sequence and picture parameter sets (7.3.2.1, 7.3.2.2) including VUI and scaling lists.
|
||||
|
||||
use crate::h264::decode::error::{Result, ensure, unsupported};
|
||||
use crate::h264::decode::tables::{DEFAULT_4X4_INTER, DEFAULT_4X4_INTRA, DEFAULT_8X8_INTER, DEFAULT_8X8_INTRA, ZIGZAG4, ZIGZAG8};
|
||||
use craft_media::bitstream::BitReader;
|
||||
|
||||
/// Maximum supported reconstructed luma area. 8K UHD fits comfortably while
|
||||
/// hostile SPS dimensions cannot allocate full pictures and decoder work state.
|
||||
const MAX_LUMA_SAMPLES: u64 = 1 << 25;
|
||||
|
||||
/// Scaling matrices after applying fall-back rules, stored in raster order (weightScale).
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct ScalingMatrices {
|
||||
/// [Intra Y, Intra Cb, Intra Cr, Inter Y, Inter Cb, Inter Cr], raster order.
|
||||
pub m4: [[u8; 16]; 6],
|
||||
/// [Intra Y, Inter Y, Intra Cb, Inter Cb, Intra Cr, Inter Cr], raster order.
|
||||
pub m8: [[u8; 64]; 6],
|
||||
}
|
||||
|
||||
impl ScalingMatrices {
|
||||
pub fn flat() -> Self {
|
||||
Self { m4: [[16; 16]; 6], m8: [[16; 64]; 6] }
|
||||
}
|
||||
pub fn is_flat(&self) -> bool {
|
||||
self.m4.iter().all(|m| m.iter().all(|&v| v == 16)) && self.m8.iter().all(|m| m.iter().all(|&v| v == 16))
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists as parsed, in zig-zag order, before inverse scanning.
|
||||
#[derive(Clone, Debug)]
|
||||
struct ZzLists {
|
||||
l4: [[u8; 16]; 6],
|
||||
l8: [[u8; 64]; 6],
|
||||
}
|
||||
|
||||
fn zz4_to_raster(l: &[u8; 16]) -> [u8; 16] {
|
||||
let mut out = [0; 16];
|
||||
for (k, &v) in l.iter().enumerate() {
|
||||
out[ZIGZAG4[k] as usize] = v;
|
||||
}
|
||||
out
|
||||
}
|
||||
fn zz8_to_raster(l: &[u8; 64]) -> [u8; 64] {
|
||||
let mut out = [0; 64];
|
||||
for (k, &v) in l.iter().enumerate() {
|
||||
out[ZIGZAG8[k] as usize] = v;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
impl ZzLists {
|
||||
fn to_matrices(&self) -> ScalingMatrices {
|
||||
let mut m = ScalingMatrices::flat();
|
||||
for i in 0..6 {
|
||||
m.m4[i] = zz4_to_raster(&self.l4[i]);
|
||||
m.m8[i] = zz8_to_raster(&self.l8[i]);
|
||||
}
|
||||
m
|
||||
}
|
||||
fn flat() -> Self {
|
||||
Self { l4: [[16; 16]; 6], l8: [[16; 64]; 6] }
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of parsing one scaling_list(): Some(list) or None meaning "use default".
|
||||
fn parse_scaling_list<const N: usize>(r: &mut BitReader) -> Result<Option<[u8; N]>> {
|
||||
let mut list = [0u8; N];
|
||||
let mut last = 8i32;
|
||||
let mut next = 8i32;
|
||||
for (j, item) in list.iter_mut().enumerate() {
|
||||
if next != 0 {
|
||||
let delta = r.read_se()?;
|
||||
ensure!((-128..=127).contains(&delta), "delta_scale out of range");
|
||||
next = (last + delta + 256) % 256;
|
||||
if j == 0 && next == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
*item = if next == 0 { last as u8 } else { next as u8 };
|
||||
last = *item as i32;
|
||||
}
|
||||
Ok(Some(list))
|
||||
}
|
||||
|
||||
/// Parse scaling lists with a fall-back rule. `fallback` gives the lists used for rule B
|
||||
/// (sequence-level) or None for rule A.
|
||||
fn parse_scaling_lists(r: &mut BitReader, count: usize, fallback: Option<&ZzLists>) -> Result<ZzLists> {
|
||||
let mut out = ZzLists::flat();
|
||||
for i in 0..count.max(8) {
|
||||
let present = if i < count { r.read_flag()? } else { false };
|
||||
if i < 6 {
|
||||
let parsed = if present { Some(parse_scaling_list::<16>(r)?) } else { None };
|
||||
out.l4[i] = match parsed {
|
||||
Some(Some(l)) => l,
|
||||
Some(None) => {
|
||||
if i < 3 {
|
||||
DEFAULT_4X4_INTRA
|
||||
} else {
|
||||
DEFAULT_4X4_INTER
|
||||
}
|
||||
}
|
||||
None => match (i, fallback) {
|
||||
(0, None) => DEFAULT_4X4_INTRA,
|
||||
(3, None) => DEFAULT_4X4_INTER,
|
||||
(0 | 3, Some(f)) => f.l4[i],
|
||||
_ => out.l4[i - 1],
|
||||
},
|
||||
};
|
||||
} else {
|
||||
let k = i - 6;
|
||||
if k >= 6 {
|
||||
break;
|
||||
}
|
||||
let parsed = if present { Some(parse_scaling_list::<64>(r)?) } else { None };
|
||||
out.l8[k] = match parsed {
|
||||
Some(Some(l)) => l,
|
||||
Some(None) => {
|
||||
if k % 2 == 0 {
|
||||
DEFAULT_8X8_INTRA
|
||||
} else {
|
||||
DEFAULT_8X8_INTER
|
||||
}
|
||||
}
|
||||
None => match (k, fallback) {
|
||||
(0, None) => DEFAULT_8X8_INTRA,
|
||||
(1, None) => DEFAULT_8X8_INTER,
|
||||
(0 | 1, Some(f)) => f.l8[k],
|
||||
_ => out.l8[k - 2],
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
// 8x8 chroma lists that were never signalled (count == 8) follow rule A/B chains as well.
|
||||
if count <= 8 {
|
||||
for k in 2..6 {
|
||||
out.l8[k] = out.l8[k - 2];
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct HrdParameters {
|
||||
pub cpb_cnt: u32,
|
||||
pub bit_rate_scale: u8,
|
||||
pub cpb_size_scale: u8,
|
||||
pub bit_rate_value_minus1: Vec<u32>,
|
||||
pub cpb_size_value_minus1: Vec<u32>,
|
||||
pub cbr_flag: Vec<bool>,
|
||||
pub initial_cpb_removal_delay_length: u8,
|
||||
pub cpb_removal_delay_length: u8,
|
||||
pub dpb_output_delay_length: u8,
|
||||
pub time_offset_length: u8,
|
||||
}
|
||||
|
||||
fn parse_hrd(r: &mut BitReader) -> Result<HrdParameters> {
|
||||
let cpb_cnt = r.read_ue_plus(1)?;
|
||||
ensure!(cpb_cnt <= 32, "cpb_cnt_minus1 out of range");
|
||||
let mut h = HrdParameters { cpb_cnt, bit_rate_scale: r.read_bits(4)? as u8, cpb_size_scale: r.read_bits(4)? as u8, ..Default::default() };
|
||||
for _ in 0..cpb_cnt {
|
||||
h.bit_rate_value_minus1.push(r.read_ue()?);
|
||||
h.cpb_size_value_minus1.push(r.read_ue()?);
|
||||
h.cbr_flag.push(r.read_flag()?);
|
||||
}
|
||||
h.initial_cpb_removal_delay_length = r.read_bits(5)? as u8 + 1;
|
||||
h.cpb_removal_delay_length = r.read_bits(5)? as u8 + 1;
|
||||
h.dpb_output_delay_length = r.read_bits(5)? as u8 + 1;
|
||||
h.time_offset_length = r.read_bits(5)? as u8;
|
||||
Ok(h)
|
||||
}
|
||||
|
||||
/// Video usability information (Annex E).
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Vui {
|
||||
pub aspect_ratio_idc: u8,
|
||||
pub sar: (u16, u16),
|
||||
pub overscan_appropriate: Option<bool>,
|
||||
pub video_format: u8,
|
||||
pub full_range: bool,
|
||||
pub colour_primaries: u8,
|
||||
pub transfer_characteristics: u8,
|
||||
pub matrix_coefficients: u8,
|
||||
pub chroma_loc: Option<(u32, u32)>,
|
||||
pub timing: Option<(u32, u32, bool)>,
|
||||
pub nal_hrd: Option<HrdParameters>,
|
||||
pub vcl_hrd: Option<HrdParameters>,
|
||||
pub low_delay_hrd: bool,
|
||||
pub pic_struct_present: bool,
|
||||
pub bitstream_restriction: Option<BitstreamRestriction>,
|
||||
}
|
||||
|
||||
impl Default for Vui {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
aspect_ratio_idc: 0,
|
||||
sar: (0, 0),
|
||||
overscan_appropriate: None,
|
||||
video_format: 5,
|
||||
full_range: false,
|
||||
colour_primaries: 2,
|
||||
transfer_characteristics: 2,
|
||||
matrix_coefficients: 2,
|
||||
chroma_loc: None,
|
||||
timing: None,
|
||||
nal_hrd: None,
|
||||
vcl_hrd: None,
|
||||
low_delay_hrd: false,
|
||||
pic_struct_present: false,
|
||||
bitstream_restriction: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct BitstreamRestriction {
|
||||
pub motion_vectors_over_pic_boundaries: bool,
|
||||
pub max_bytes_per_pic_denom: u32,
|
||||
pub max_bits_per_mb_denom: u32,
|
||||
pub log2_max_mv_length_horizontal: u32,
|
||||
pub log2_max_mv_length_vertical: u32,
|
||||
pub max_num_reorder_frames: u32,
|
||||
pub max_dec_frame_buffering: u32,
|
||||
}
|
||||
|
||||
/// Table E-1 sample aspect ratios.
|
||||
const SAR_TABLE: [(u16, u16); 17] = [
|
||||
(0, 0),
|
||||
(1, 1),
|
||||
(12, 11),
|
||||
(10, 11),
|
||||
(16, 11),
|
||||
(40, 33),
|
||||
(24, 11),
|
||||
(20, 11),
|
||||
(32, 11),
|
||||
(80, 33),
|
||||
(18, 11),
|
||||
(15, 11),
|
||||
(64, 33),
|
||||
(160, 99),
|
||||
(4, 3),
|
||||
(3, 2),
|
||||
(2, 1),
|
||||
];
|
||||
|
||||
fn parse_vui(r: &mut BitReader) -> Result<Vui> {
|
||||
let mut v = Vui::default();
|
||||
if r.read_flag()? {
|
||||
v.aspect_ratio_idc = r.read_bits(8)? as u8;
|
||||
if v.aspect_ratio_idc == 255 {
|
||||
v.sar = (r.read_bits(16)? as u16, r.read_bits(16)? as u16);
|
||||
} else if (v.aspect_ratio_idc as usize) < SAR_TABLE.len() {
|
||||
v.sar = SAR_TABLE[v.aspect_ratio_idc as usize];
|
||||
}
|
||||
}
|
||||
if r.read_flag()? {
|
||||
v.overscan_appropriate = Some(r.read_flag()?);
|
||||
}
|
||||
if r.read_flag()? {
|
||||
v.video_format = r.read_bits(3)? as u8;
|
||||
v.full_range = r.read_flag()?;
|
||||
if r.read_flag()? {
|
||||
v.colour_primaries = r.read_bits(8)? as u8;
|
||||
v.transfer_characteristics = r.read_bits(8)? as u8;
|
||||
v.matrix_coefficients = r.read_bits(8)? as u8;
|
||||
}
|
||||
}
|
||||
if r.read_flag()? {
|
||||
v.chroma_loc = Some((r.read_ue()?, r.read_ue()?));
|
||||
}
|
||||
if r.read_flag()? {
|
||||
let n = r.read_bits(32)?;
|
||||
let t = r.read_bits(32)?;
|
||||
let fixed = r.read_flag()?;
|
||||
v.timing = Some((n, t, fixed));
|
||||
}
|
||||
if r.read_flag()? {
|
||||
v.nal_hrd = Some(parse_hrd(r)?);
|
||||
}
|
||||
if r.read_flag()? {
|
||||
v.vcl_hrd = Some(parse_hrd(r)?);
|
||||
}
|
||||
if v.nal_hrd.is_some() || v.vcl_hrd.is_some() {
|
||||
v.low_delay_hrd = r.read_flag()?;
|
||||
}
|
||||
v.pic_struct_present = r.read_flag()?;
|
||||
// Some streams truncate the VUI after this point; treat EOF as "absent".
|
||||
if r.bits_left() > 0 && r.read_flag()? {
|
||||
let br = (|| -> Result<BitstreamRestriction> {
|
||||
Ok(BitstreamRestriction {
|
||||
motion_vectors_over_pic_boundaries: r.read_flag()?,
|
||||
max_bytes_per_pic_denom: r.read_ue()?,
|
||||
max_bits_per_mb_denom: r.read_ue()?,
|
||||
log2_max_mv_length_horizontal: r.read_ue()?,
|
||||
log2_max_mv_length_vertical: r.read_ue()?,
|
||||
max_num_reorder_frames: r.read_ue()?,
|
||||
max_dec_frame_buffering: r.read_ue()?,
|
||||
})
|
||||
})();
|
||||
v.bitstream_restriction = br.ok();
|
||||
}
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// Sequence parameter set.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Sps {
|
||||
pub profile_idc: u8,
|
||||
pub constraint_flags: u8,
|
||||
pub level_idc: u8,
|
||||
pub id: u32,
|
||||
pub chroma_format_idc: u32,
|
||||
pub separate_colour_plane: bool,
|
||||
pub bit_depth_luma: u32,
|
||||
pub bit_depth_chroma: u32,
|
||||
pub qpprime_y_zero_transform_bypass: bool,
|
||||
pub seq_scaling_matrix_present: bool,
|
||||
scaling_lists: ZzLists,
|
||||
pub scaling: ScalingMatrices,
|
||||
pub log2_max_frame_num: u32,
|
||||
pub pic_order_cnt_type: u32,
|
||||
pub log2_max_poc_lsb: u32,
|
||||
pub delta_pic_order_always_zero: bool,
|
||||
pub offset_for_non_ref_pic: i32,
|
||||
pub offset_for_top_to_bottom_field: i32,
|
||||
pub offset_for_ref_frame: Vec<i32>,
|
||||
pub max_num_ref_frames: u32,
|
||||
pub gaps_in_frame_num_allowed: bool,
|
||||
pub pic_width_in_mbs: u32,
|
||||
pub pic_height_in_map_units: u32,
|
||||
pub frame_mbs_only: bool,
|
||||
pub mb_adaptive_frame_field: bool,
|
||||
pub direct_8x8_inference: bool,
|
||||
/// (left, right, top, bottom) in crop units as coded.
|
||||
pub frame_crop: Option<(u32, u32, u32, u32)>,
|
||||
pub vui: Option<Vui>,
|
||||
}
|
||||
|
||||
impl Sps {
|
||||
pub fn parse(rbsp: &[u8]) -> Result<Sps> {
|
||||
let mut r = BitReader::new(rbsp);
|
||||
let profile_idc = r.read_bits(8)? as u8;
|
||||
let constraint_flags = r.read_bits(8)? as u8;
|
||||
let level_idc = r.read_bits(8)? as u8;
|
||||
let id = r.read_ue()?;
|
||||
ensure!(id < 32, "seq_parameter_set_id {id} out of range");
|
||||
let mut chroma_format_idc = 1;
|
||||
let mut separate_colour_plane = false;
|
||||
let mut bit_depth_luma = 8;
|
||||
let mut bit_depth_chroma = 8;
|
||||
let mut bypass = false;
|
||||
let mut seq_scaling_matrix_present = false;
|
||||
let mut scaling_lists = ZzLists::flat();
|
||||
if matches!(profile_idc, 100 | 110 | 122 | 244 | 44 | 83 | 86 | 118 | 128 | 138 | 139 | 134 | 135) {
|
||||
chroma_format_idc = r.read_ue()?;
|
||||
ensure!(chroma_format_idc <= 3, "chroma_format_idc out of range");
|
||||
if chroma_format_idc == 3 {
|
||||
separate_colour_plane = r.read_flag()?;
|
||||
}
|
||||
bit_depth_luma = r.read_ue_plus(8)?;
|
||||
bit_depth_chroma = r.read_ue_plus(8)?;
|
||||
ensure!(bit_depth_luma <= 14 && bit_depth_chroma <= 14, "bit depth out of range");
|
||||
bypass = r.read_flag()?;
|
||||
seq_scaling_matrix_present = r.read_flag()?;
|
||||
if seq_scaling_matrix_present {
|
||||
let count = if chroma_format_idc != 3 { 8 } else { 12 };
|
||||
scaling_lists = parse_scaling_lists(&mut r, count, None)?;
|
||||
}
|
||||
}
|
||||
let log2_max_frame_num = r.read_ue_plus(4)?;
|
||||
ensure!(log2_max_frame_num <= 16, "log2_max_frame_num out of range");
|
||||
let pic_order_cnt_type = r.read_ue()?;
|
||||
ensure!(pic_order_cnt_type <= 2, "pic_order_cnt_type out of range");
|
||||
let mut log2_max_poc_lsb = 0;
|
||||
let mut delta_pic_order_always_zero = false;
|
||||
let mut offset_for_non_ref_pic = 0;
|
||||
let mut offset_for_top_to_bottom_field = 0;
|
||||
let mut offset_for_ref_frame = Vec::new();
|
||||
if pic_order_cnt_type == 0 {
|
||||
log2_max_poc_lsb = r.read_ue_plus(4)?;
|
||||
ensure!(log2_max_poc_lsb <= 16, "log2_max_pic_order_cnt_lsb out of range");
|
||||
} else if pic_order_cnt_type == 1 {
|
||||
delta_pic_order_always_zero = r.read_flag()?;
|
||||
offset_for_non_ref_pic = r.read_se()?;
|
||||
offset_for_top_to_bottom_field = r.read_se()?;
|
||||
let n = r.read_ue()?;
|
||||
ensure!(n <= 255, "num_ref_frames_in_pic_order_cnt_cycle out of range");
|
||||
for _ in 0..n {
|
||||
offset_for_ref_frame.push(r.read_se()?);
|
||||
}
|
||||
}
|
||||
let max_num_ref_frames = r.read_ue()?;
|
||||
ensure!(max_num_ref_frames <= 16, "max_num_ref_frames out of range");
|
||||
let gaps_in_frame_num_allowed = r.read_flag()?;
|
||||
let pic_width_in_mbs = r.read_ue_plus(1)?;
|
||||
let pic_height_in_map_units = r.read_ue_plus(1)?;
|
||||
ensure!(pic_width_in_mbs <= 1024 && pic_height_in_map_units <= 1024, "picture too large");
|
||||
let frame_mbs_only = r.read_flag()?;
|
||||
let mb_adaptive_frame_field = if !frame_mbs_only { r.read_flag()? } else { false };
|
||||
let direct_8x8_inference = r.read_flag()?;
|
||||
let frame_crop = if r.read_flag()? { Some((r.read_ue()?, r.read_ue()?, r.read_ue()?, r.read_ue()?)) } else { None };
|
||||
let vui = if r.read_flag()? { Some(parse_vui(&mut r)?) } else { None };
|
||||
let scaling = if seq_scaling_matrix_present { scaling_lists.to_matrices() } else { ScalingMatrices::flat() };
|
||||
let sps = Sps {
|
||||
profile_idc,
|
||||
constraint_flags,
|
||||
level_idc,
|
||||
id,
|
||||
chroma_format_idc,
|
||||
separate_colour_plane,
|
||||
bit_depth_luma,
|
||||
bit_depth_chroma,
|
||||
qpprime_y_zero_transform_bypass: bypass,
|
||||
seq_scaling_matrix_present,
|
||||
scaling_lists,
|
||||
scaling,
|
||||
log2_max_frame_num,
|
||||
pic_order_cnt_type,
|
||||
log2_max_poc_lsb,
|
||||
delta_pic_order_always_zero,
|
||||
offset_for_non_ref_pic,
|
||||
offset_for_top_to_bottom_field,
|
||||
offset_for_ref_frame,
|
||||
max_num_ref_frames,
|
||||
gaps_in_frame_num_allowed,
|
||||
pic_width_in_mbs,
|
||||
pic_height_in_map_units,
|
||||
frame_mbs_only,
|
||||
mb_adaptive_frame_field,
|
||||
direct_8x8_inference,
|
||||
frame_crop,
|
||||
vui,
|
||||
};
|
||||
if let Some((l, rr, t, b)) = sps.frame_crop {
|
||||
let (cx, cy) = sps.crop_unit();
|
||||
let ok = l.checked_add(rr).and_then(|h| h.checked_mul(cx)).is_some_and(|w| w < sps.width())
|
||||
&& t.checked_add(b).and_then(|v| v.checked_mul(cy)).is_some_and(|h| h < sps.height());
|
||||
ensure!(ok, "frame cropping exceeds picture size");
|
||||
}
|
||||
Ok(sps)
|
||||
}
|
||||
|
||||
/// ChromaArrayType.
|
||||
pub fn chroma_array_type(&self) -> u32 {
|
||||
if self.separate_colour_plane { 0 } else { self.chroma_format_idc }
|
||||
}
|
||||
pub fn frame_height_in_mbs(&self) -> u32 {
|
||||
(2 - self.frame_mbs_only as u32) * self.pic_height_in_map_units
|
||||
}
|
||||
/// Decoded (uncropped) luma width.
|
||||
pub fn width(&self) -> u32 {
|
||||
self.pic_width_in_mbs * 16
|
||||
}
|
||||
pub fn height(&self) -> u32 {
|
||||
self.frame_height_in_mbs() * 16
|
||||
}
|
||||
pub fn max_frame_num(&self) -> u32 {
|
||||
1 << self.log2_max_frame_num
|
||||
}
|
||||
/// (CropUnitX, CropUnitY).
|
||||
pub fn crop_unit(&self) -> (u32, u32) {
|
||||
let (sub_w, sub_h) = match self.chroma_array_type() {
|
||||
1 => (2, 2),
|
||||
2 => (2, 1),
|
||||
_ => (1, 1),
|
||||
};
|
||||
if self.chroma_array_type() == 0 { (1, 2 - self.frame_mbs_only as u32) } else { (sub_w, sub_h * (2 - self.frame_mbs_only as u32)) }
|
||||
}
|
||||
/// Cropping rectangle in luma samples: (x, y, width, height).
|
||||
pub fn crop_rect(&self) -> (u32, u32, u32, u32) {
|
||||
match self.frame_crop {
|
||||
None => (0, 0, self.width(), self.height()),
|
||||
Some((l, r, t, b)) => {
|
||||
let (cx, cy) = self.crop_unit();
|
||||
(
|
||||
l.saturating_mul(cx),
|
||||
t.saturating_mul(cy),
|
||||
self.width().saturating_sub(l.saturating_add(r).saturating_mul(cx)).max(1),
|
||||
self.height().saturating_sub(t.saturating_add(b).saturating_mul(cy)).max(1),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// MaxDpbFrames from Table A-1 (and the VUI max_dec_frame_buffering when present).
|
||||
pub fn max_dpb_frames(&self) -> usize {
|
||||
let max_dpb_mbs: u32 = match self.level_idc {
|
||||
9 => 396,
|
||||
10 => 396,
|
||||
11 => {
|
||||
if self.constraint_flags & 0x10 != 0 && !matches!(self.profile_idc, 100 | 110 | 122 | 244 | 44) {
|
||||
396
|
||||
} else {
|
||||
900
|
||||
}
|
||||
}
|
||||
12 | 13 | 20 => 2376,
|
||||
21 => 4752,
|
||||
22 | 30 => 8100,
|
||||
31 => 18000,
|
||||
32 => 20480,
|
||||
40 | 41 => 32768,
|
||||
42 => 34816,
|
||||
50 => 110400,
|
||||
51 | 52 => 184320,
|
||||
60..=62 => 696320,
|
||||
_ => 184320,
|
||||
};
|
||||
let frame_mbs = self.pic_width_in_mbs * self.frame_height_in_mbs();
|
||||
let mut n = (max_dpb_mbs / frame_mbs.max(1)).clamp(1, 16) as usize;
|
||||
if let Some(br) = self.vui.as_ref().and_then(|v| v.bitstream_restriction.as_ref()) {
|
||||
n = (br.max_dec_frame_buffering as usize).min(16);
|
||||
}
|
||||
n.max(self.max_num_ref_frames as usize).max(1)
|
||||
}
|
||||
|
||||
/// Number of frames that may precede a frame in decoding order and follow it in output order.
|
||||
pub fn max_num_reorder_frames(&self) -> usize {
|
||||
if let Some(br) = self.vui.as_ref().and_then(|v| v.bitstream_restriction.as_ref()) {
|
||||
return br.max_num_reorder_frames as usize;
|
||||
}
|
||||
// Profiles that cannot reorder (no B slices) with constraint_set3 (intra) etc.
|
||||
if matches!(self.profile_idc, 44 | 86 | 100 | 110 | 122 | 244) && self.constraint_flags & 0x10 != 0 {
|
||||
return 0;
|
||||
}
|
||||
self.max_dpb_frames()
|
||||
}
|
||||
|
||||
pub fn check_supported(&self) -> Result<()> {
|
||||
if u64::from(self.width()).saturating_mul(u64::from(self.height())) > MAX_LUMA_SAMPLES {
|
||||
return unsupported("picture dimensions exceed the decoder memory limit");
|
||||
}
|
||||
if !self.frame_mbs_only {
|
||||
return unsupported("interlaced (field / MBAFF) coding");
|
||||
}
|
||||
if self.chroma_array_type() != 1 {
|
||||
return unsupported(format!("chroma_format_idc {} (only 4:2:0 is implemented)", self.chroma_format_idc));
|
||||
}
|
||||
if self.bit_depth_luma != 8 || self.bit_depth_chroma != 8 {
|
||||
return unsupported(format!("bit depth luma {} chroma {} (only 8-bit is implemented)", self.bit_depth_luma, self.bit_depth_chroma));
|
||||
}
|
||||
if self.qpprime_y_zero_transform_bypass {
|
||||
return unsupported("qpprime_y_zero_transform_bypass (lossless)");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Picture parameter set.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Pps {
|
||||
pub id: u32,
|
||||
pub sps_id: u32,
|
||||
pub entropy_coding_mode: bool,
|
||||
pub bottom_field_pic_order_in_frame_present: bool,
|
||||
pub num_slice_groups: u32,
|
||||
pub slice_group_map_type: u32,
|
||||
pub num_ref_idx_l0_default_active: u32,
|
||||
pub num_ref_idx_l1_default_active: u32,
|
||||
pub weighted_pred: bool,
|
||||
pub weighted_bipred_idc: u32,
|
||||
pub pic_init_qp: i32,
|
||||
pub pic_init_qs: i32,
|
||||
pub chroma_qp_index_offset: i32,
|
||||
pub deblocking_filter_control_present: bool,
|
||||
pub constrained_intra_pred: bool,
|
||||
pub redundant_pic_cnt_present: bool,
|
||||
pub transform_8x8_mode: bool,
|
||||
pub pic_scaling_matrix_present: bool,
|
||||
pub second_chroma_qp_index_offset: i32,
|
||||
/// Effective scaling matrices (after PPS/SPS fall-back), raster order.
|
||||
pub scaling: ScalingMatrices,
|
||||
}
|
||||
|
||||
impl Pps {
|
||||
/// Parse a PPS. Needs the SPS table because the tail depends on chroma_format_idc.
|
||||
pub fn parse(rbsp: &[u8], spss: &[Option<Sps>]) -> Result<Pps> {
|
||||
let mut r = BitReader::new(rbsp);
|
||||
let id = r.read_ue()?;
|
||||
ensure!(id < 256, "pic_parameter_set_id out of range");
|
||||
let sps_id = r.read_ue()?;
|
||||
ensure!(sps_id < 32, "seq_parameter_set_id out of range");
|
||||
let sps = spss
|
||||
.get(sps_id as usize)
|
||||
.and_then(|s| s.as_ref())
|
||||
.ok_or_else(|| crate::h264::decode::Error::MissingParameterSet(format!("SPS {sps_id} for PPS {id}")))?;
|
||||
let entropy_coding_mode = r.read_flag()?;
|
||||
let bottom_field_pic_order_in_frame_present = r.read_flag()?;
|
||||
let num_slice_groups_minus1 = r.read_ue()?;
|
||||
ensure!(num_slice_groups_minus1 < 8, "num_slice_groups out of range");
|
||||
let num_slice_groups = num_slice_groups_minus1 + 1;
|
||||
let mut slice_group_map_type = 0;
|
||||
if num_slice_groups > 1 {
|
||||
// Parsed for completeness; FMO decoding is unsupported.
|
||||
slice_group_map_type = r.read_ue()?;
|
||||
match slice_group_map_type {
|
||||
0 => {
|
||||
for _ in 0..num_slice_groups {
|
||||
r.read_ue()?;
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
for _ in 0..num_slice_groups - 1 {
|
||||
r.read_ue()?;
|
||||
r.read_ue()?;
|
||||
}
|
||||
}
|
||||
3..=5 => {
|
||||
r.read_flag()?;
|
||||
r.read_ue()?;
|
||||
}
|
||||
6 => {
|
||||
let n_minus1 = r.read_ue()?;
|
||||
// pic_size_in_map_units_minus1: one slice_group_id per map unit of the picture.
|
||||
ensure!(n_minus1 < sps.pic_width_in_mbs.saturating_mul(sps.pic_height_in_map_units), "pic_size_in_map_units out of range");
|
||||
let n = n_minus1 + 1;
|
||||
let bits = 32 - (num_slice_groups - 1).leading_zeros();
|
||||
for _ in 0..n {
|
||||
r.read_bits(bits)?;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let num_ref_idx_l0_minus1 = r.read_ue()?;
|
||||
let num_ref_idx_l1_minus1 = r.read_ue()?;
|
||||
ensure!(num_ref_idx_l0_minus1 < 32 && num_ref_idx_l1_minus1 < 32, "num_ref_idx_default_active out of range");
|
||||
let num_ref_idx_l0_default_active = num_ref_idx_l0_minus1 + 1;
|
||||
let num_ref_idx_l1_default_active = num_ref_idx_l1_minus1 + 1;
|
||||
let weighted_pred = r.read_flag()?;
|
||||
// 7.4.2.2.1 fall-back: a value of 3 shall be considered equal to 2.
|
||||
let weighted_bipred_idc = (r.read_bits(2)?).min(2);
|
||||
// 7.4.2.2: pic_init_qp_minus26 in -(26 + QpBdOffsetY)..=25, pic_init_qs_minus26 in -26..=25.
|
||||
let qp_bd_offset_y = 6 * (sps.bit_depth_luma.saturating_sub(8)) as i32;
|
||||
let qp_min = -(26 + qp_bd_offset_y);
|
||||
let pic_init_qp_minus26 = r.read_se()?;
|
||||
ensure!((qp_min..=25).contains(&pic_init_qp_minus26), "pic_init_qp_minus26 out of range");
|
||||
let pic_init_qs_minus26 = r.read_se()?;
|
||||
ensure!((-26..=25).contains(&pic_init_qs_minus26), "pic_init_qs_minus26 out of range");
|
||||
let pic_init_qp = 26 + pic_init_qp_minus26;
|
||||
let pic_init_qs = 26 + pic_init_qs_minus26;
|
||||
let chroma_qp_index_offset = r.read_se()?;
|
||||
ensure!((-12..=12).contains(&chroma_qp_index_offset), "chroma_qp_index_offset out of range");
|
||||
let deblocking_filter_control_present = r.read_flag()?;
|
||||
let constrained_intra_pred = r.read_flag()?;
|
||||
let redundant_pic_cnt_present = r.read_flag()?;
|
||||
let mut transform_8x8_mode = false;
|
||||
let mut pic_scaling_matrix_present = false;
|
||||
let mut second_chroma_qp_index_offset = chroma_qp_index_offset;
|
||||
let mut scaling = sps.scaling.clone();
|
||||
if r.more_rbsp_data() {
|
||||
transform_8x8_mode = r.read_flag()?;
|
||||
pic_scaling_matrix_present = r.read_flag()?;
|
||||
if pic_scaling_matrix_present {
|
||||
let count = 6 + if sps.chroma_format_idc != 3 { 2 } else { 6 } * transform_8x8_mode as usize;
|
||||
// Fall-back rule B uses the sequence-level lists (or Flat when the SPS has none, but then
|
||||
// rule A applies per 7.4.2.2).
|
||||
let lists = if sps.seq_scaling_matrix_present {
|
||||
parse_scaling_lists(&mut r, count, Some(&sps.scaling_lists))?
|
||||
} else {
|
||||
parse_scaling_lists(&mut r, count, None)?
|
||||
};
|
||||
scaling = lists.to_matrices();
|
||||
}
|
||||
second_chroma_qp_index_offset = r.read_se()?;
|
||||
ensure!((-12..=12).contains(&second_chroma_qp_index_offset), "second_chroma_qp_index_offset out of range");
|
||||
}
|
||||
Ok(Pps {
|
||||
id,
|
||||
sps_id,
|
||||
entropy_coding_mode,
|
||||
bottom_field_pic_order_in_frame_present,
|
||||
num_slice_groups,
|
||||
slice_group_map_type,
|
||||
num_ref_idx_l0_default_active,
|
||||
num_ref_idx_l1_default_active,
|
||||
weighted_pred,
|
||||
weighted_bipred_idc,
|
||||
pic_init_qp,
|
||||
pic_init_qs,
|
||||
chroma_qp_index_offset,
|
||||
deblocking_filter_control_present,
|
||||
constrained_intra_pred,
|
||||
redundant_pic_cnt_present,
|
||||
transform_8x8_mode,
|
||||
pic_scaling_matrix_present,
|
||||
second_chroma_qp_index_offset,
|
||||
scaling,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use craft_media::bitstream::BitWriter;
|
||||
|
||||
#[test]
|
||||
fn scaling_list_default_signal() {
|
||||
// delta_scale = -8 on the first entry => nextScale 0 => use default.
|
||||
let mut w = BitWriter::new();
|
||||
w.write_se(-8);
|
||||
w.rbsp_trailing();
|
||||
let b = w.finish();
|
||||
let mut r = BitReader::new(&b);
|
||||
assert_eq!(parse_scaling_list::<16>(&mut r).unwrap(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scaling_list_repeat_last() {
|
||||
// 8 -> +2 = 10, then +0 = 10, then -10 => 0 => repeat 10 for the rest.
|
||||
let mut w = BitWriter::new();
|
||||
w.write_se(2);
|
||||
w.write_se(0);
|
||||
w.write_se(-10);
|
||||
w.rbsp_trailing();
|
||||
let b = w.finish();
|
||||
let mut r = BitReader::new(&b);
|
||||
let l = parse_scaling_list::<16>(&mut r).unwrap().unwrap();
|
||||
assert_eq!(l, [10; 16]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_minimal_baseline_sps() {
|
||||
// profile 66, level 30, sps_id 0, log2_max_frame_num_minus4 0, poc type 2, 1 ref, 11x9 MBs.
|
||||
let mut w = BitWriter::new();
|
||||
w.write_bits(66, 8);
|
||||
w.write_bits(0xC0, 8);
|
||||
w.write_bits(30, 8);
|
||||
w.write_ue(0);
|
||||
w.write_ue(0);
|
||||
w.write_ue(2);
|
||||
w.write_ue(1);
|
||||
w.write_bit(false);
|
||||
w.write_ue(10);
|
||||
w.write_ue(8);
|
||||
w.write_bit(true); // frame_mbs_only
|
||||
w.write_bit(true); // direct_8x8
|
||||
w.write_bit(false); // cropping
|
||||
w.write_bit(false); // vui
|
||||
w.rbsp_trailing();
|
||||
let sps = Sps::parse(&w.finish()).unwrap();
|
||||
assert_eq!((sps.width(), sps.height()), (176, 144));
|
||||
assert_eq!(sps.pic_order_cnt_type, 2);
|
||||
assert_eq!(sps.crop_rect(), (0, 0, 176, 144));
|
||||
}
|
||||
/// Baseline SPS with the given `ue(v)` overrides for the fields hostile streams can max out.
|
||||
fn hostile_sps(profile: u32, bit_depth: u32, log2_frame_num: u32, w: u32, h: u32, crop: u32) -> Vec<u8> {
|
||||
let mut bw = BitWriter::new();
|
||||
bw.write_bits(profile, 8);
|
||||
bw.write_bits(0, 8);
|
||||
bw.write_bits(30, 8);
|
||||
bw.write_ue(0); // sps id
|
||||
if profile == 100 {
|
||||
bw.write_ue(1); // chroma_format_idc
|
||||
bw.write_ue(bit_depth);
|
||||
bw.write_ue(bit_depth);
|
||||
bw.write_bit(false); // bypass
|
||||
bw.write_bit(false); // scaling matrix
|
||||
}
|
||||
bw.write_ue(log2_frame_num);
|
||||
bw.write_ue(2); // poc type
|
||||
bw.write_ue(1); // max_num_ref_frames
|
||||
bw.write_bit(false);
|
||||
bw.write_ue(w);
|
||||
bw.write_ue(h);
|
||||
bw.write_bit(true); // frame_mbs_only
|
||||
bw.write_bit(true); // direct_8x8
|
||||
bw.write_bit(crop != 0);
|
||||
if crop != 0 {
|
||||
for _ in 0..4 {
|
||||
bw.write_ue(crop);
|
||||
}
|
||||
}
|
||||
bw.write_bit(false); // vui
|
||||
bw.rbsp_trailing();
|
||||
bw.finish()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sps_huge_ue_fields_are_errors_not_overflow() {
|
||||
let m = u32::MAX;
|
||||
assert!(Sps::parse(&hostile_sps(66, 0, 0, 10, 8, 0)).is_ok());
|
||||
assert!(Sps::parse(&hostile_sps(66, 0, m, 10, 8, 0)).is_err(), "log2_max_frame_num_minus4");
|
||||
assert!(Sps::parse(&hostile_sps(66, 0, 0, m, 8, 0)).is_err(), "pic_width_in_mbs_minus1");
|
||||
assert!(Sps::parse(&hostile_sps(66, 0, 0, 10, m, 0)).is_err(), "pic_height_in_map_units_minus1");
|
||||
assert!(Sps::parse(&hostile_sps(100, m, 0, 10, 8, 0)).is_err(), "bit_depth_minus8");
|
||||
assert!(Sps::parse(&hostile_sps(66, 0, 0, 10, 8, m)).is_err(), "frame_crop offsets");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
//! Picture buffers and per-macroblock state.
|
||||
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
/// Planar 4:2:0 8-bit sample buffers (MB-aligned dimensions, stride == width).
|
||||
#[derive(Clone)]
|
||||
pub struct Planes {
|
||||
pub y: Vec<u8>,
|
||||
pub cb: Vec<u8>,
|
||||
pub cr: Vec<u8>,
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub cwidth: usize,
|
||||
}
|
||||
|
||||
impl Planes {
|
||||
pub fn new(width: usize, height: usize) -> Self {
|
||||
let (cw, ch) = (width / 2, height / 2);
|
||||
Planes { y: vec![0; width * height], cb: vec![128; cw * ch], cr: vec![128; cw * ch], width, height, cwidth: cw }
|
||||
}
|
||||
pub fn gray(width: usize, height: usize) -> Self {
|
||||
let mut p = Self::new(width, height);
|
||||
p.y.fill(128);
|
||||
p
|
||||
}
|
||||
}
|
||||
|
||||
/// Macroblock coding category.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||||
pub enum MbKind {
|
||||
#[default]
|
||||
None,
|
||||
I4x4,
|
||||
I8x8,
|
||||
I16x16,
|
||||
IPcm,
|
||||
PSkip,
|
||||
BSkip,
|
||||
BDirect16x16,
|
||||
/// Any other P or B inter macroblock.
|
||||
Inter,
|
||||
}
|
||||
|
||||
impl MbKind {
|
||||
#[inline]
|
||||
pub fn is_intra(self) -> bool {
|
||||
matches!(self, MbKind::I4x4 | MbKind::I8x8 | MbKind::I16x16 | MbKind::IPcm)
|
||||
}
|
||||
}
|
||||
|
||||
/// Per-macroblock state kept for the whole picture (neighbour derivations, deblocking, co-located data).
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct MbState {
|
||||
/// Slice number within the picture; `u32::MAX` = not (yet) decoded.
|
||||
pub slice_num: u32,
|
||||
pub kind: MbKind,
|
||||
pub transform_8x8: bool,
|
||||
/// coded_block_pattern: bits 0..3 luma, bits 4..5 chroma.
|
||||
pub cbp: u8,
|
||||
pub qp: u8,
|
||||
/// QPc for Cb / Cr (used by deblocking).
|
||||
pub qpc: [u8; 2],
|
||||
pub intra_chroma_mode: u8,
|
||||
/// Intra4x4PredMode / Intra8x8PredMode per 4x4 block (raster order).
|
||||
pub intra_modes: [u8; 16],
|
||||
/// CAVLC: TotalCoeff per luma 4x4 block (raster). CABAC: coded_block_flag.
|
||||
pub nnz: [u8; 16],
|
||||
/// Same for chroma AC blocks [Cb, Cr][raster 2x2].
|
||||
pub nnz_c: [[u8; 4]; 2],
|
||||
/// CABAC coded_block_flag of DC blocks: bit0 luma (I16x16), bit1 Cb, bit2 Cr.
|
||||
pub cbf_dc: u8,
|
||||
/// Luma 4x4 blocks (raster bit) with non-zero coefficients, for deblocking bS = 2.
|
||||
pub nz_mask: u16,
|
||||
pub ref_idx: [[i8; 4]; 2],
|
||||
/// Motion vectors per 4x4 block (raster order).
|
||||
pub mv: [[[i16; 2]; 16]; 2],
|
||||
/// |mvd| per 4x4 block (raster), saturated, for CABAC context selection.
|
||||
pub mvd: [[[u8; 2]; 16]; 2],
|
||||
/// Bit per 8x8 block predicted in direct mode.
|
||||
pub direct8x8: u8,
|
||||
}
|
||||
|
||||
impl MbState {
|
||||
#[inline]
|
||||
pub fn kind_is_skip(&self) -> bool {
|
||||
matches!(self.kind, MbKind::PSkip | MbKind::BSkip)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for MbState {
|
||||
fn default() -> Self {
|
||||
MbState {
|
||||
slice_num: u32::MAX,
|
||||
kind: MbKind::None,
|
||||
transform_8x8: false,
|
||||
cbp: 0,
|
||||
qp: 0,
|
||||
qpc: [0; 2],
|
||||
intra_chroma_mode: 0,
|
||||
intra_modes: [2; 16],
|
||||
nnz: [0; 16],
|
||||
nnz_c: [[0; 4]; 2],
|
||||
cbf_dc: 0,
|
||||
nz_mask: 0,
|
||||
ref_idx: [[-1; 4]; 2],
|
||||
mv: [[[0; 2]; 16]; 2],
|
||||
mvd: [[[0; 2]; 16]; 2],
|
||||
direct8x8: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One published macroblock row of a decoded frame: final (deblocked) samples plus the motion data
|
||||
/// needed as co-located information for direct prediction.
|
||||
pub struct FrameRow {
|
||||
/// 16 luma lines.
|
||||
pub y: Box<[u8]>,
|
||||
/// 8 lines per chroma component.
|
||||
pub cb: Box<[u8]>,
|
||||
pub cr: Box<[u8]>,
|
||||
/// Per 4x4 block: index mb_x * 16 + raster.
|
||||
pub mv: [Box<[[i16; 2]]>; 2],
|
||||
/// Per 8x8 block: index mb_x * 4 + b8.
|
||||
pub ref_idx: [Box<[i8]>; 2],
|
||||
/// Unique id of the referenced picture per 8x8 block (u32::MAX = none).
|
||||
pub ref_id: [Box<[u32]>; 2],
|
||||
pub intra: Box<[bool]>,
|
||||
}
|
||||
|
||||
/// A decoded (or in-progress) frame as stored in the DPB and referenced by later pictures.
|
||||
///
|
||||
/// Rows are published in order by the thread decoding the picture; readers block (per row) until the
|
||||
/// data they need is available, which lets several pictures decode concurrently.
|
||||
pub struct Frame {
|
||||
pub id: u32,
|
||||
pub poc: i32,
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub cwidth: usize,
|
||||
pub cheight: usize,
|
||||
pub mb_w: usize,
|
||||
rows: Box<[OnceLock<FrameRow>]>,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
pub fn new(id: u32, poc: i32, mb_w: usize, mb_h: usize) -> Self {
|
||||
Frame {
|
||||
id,
|
||||
poc,
|
||||
width: mb_w * 16,
|
||||
height: mb_h * 16,
|
||||
cwidth: mb_w * 8,
|
||||
cheight: mb_h * 8,
|
||||
mb_w,
|
||||
rows: (0..mb_h).map(|_| OnceLock::new()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mb_h(&self) -> usize {
|
||||
self.rows.len()
|
||||
}
|
||||
|
||||
/// Sample bytes this picture holds (luma and both chroma planes), as the decoder's memory
|
||||
/// budget counts a picture.
|
||||
pub fn bytes(&self) -> u64 {
|
||||
(self.width as u64)
|
||||
.saturating_mul(self.height as u64)
|
||||
.saturating_add((self.cwidth as u64).saturating_mul(self.cheight as u64).saturating_mul(2))
|
||||
}
|
||||
|
||||
/// Publish MB row `r` (ignored if already published).
|
||||
pub fn publish(&self, r: usize, row: FrameRow) {
|
||||
let _ = self.rows[r].set(row);
|
||||
}
|
||||
|
||||
pub fn is_published(&self, r: usize) -> bool {
|
||||
self.rows[r].get().is_some()
|
||||
}
|
||||
|
||||
/// MB row `r`, waiting until it has been published.
|
||||
#[inline]
|
||||
pub fn row(&self, r: usize) -> &FrameRow {
|
||||
#[cfg(feature = "threads")]
|
||||
{
|
||||
self.rows[r].wait()
|
||||
}
|
||||
#[cfg(not(feature = "threads"))]
|
||||
{
|
||||
// Rows are decoded in order here, so a row is published before it is read; one that
|
||||
// is not reads as gray with no motion instead of panicking.
|
||||
self.rows[r].get_or_init(|| self.gray_row())
|
||||
}
|
||||
}
|
||||
|
||||
/// A gray intra row with no motion, what "non-existing" frames publish.
|
||||
#[cfg(not(feature = "threads"))]
|
||||
fn gray_row(&self) -> FrameRow {
|
||||
let per_list = |n: usize| -> [Box<[_]>; 2] { [vec![u32::MAX; n].into(), vec![u32::MAX; n].into()] };
|
||||
FrameRow {
|
||||
y: vec![128; self.width * 16].into(),
|
||||
cb: vec![128; self.cwidth * 8].into(),
|
||||
cr: vec![128; self.cwidth * 8].into(),
|
||||
mv: [vec![[0; 2]; self.mb_w * 16].into(), vec![[0; 2]; self.mb_w * 16].into()],
|
||||
ref_idx: [vec![-1; self.mb_w * 4].into(), vec![-1; self.mb_w * 4].into()],
|
||||
ref_id: per_list(self.mb_w * 4),
|
||||
intra: vec![true; self.mb_w].into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Wait until the whole frame is available.
|
||||
pub fn wait_complete(&self) {
|
||||
for r in 0..self.rows.len() {
|
||||
self.row(r);
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a published row from a contiguous plane set (rows of MB row `r`) and motion data.
|
||||
pub fn make_row(planes: &Planes, r: usize, mbs: &[MbState], ref_ids: &dyn Fn(&MbState, usize, i8) -> u32) -> FrameRow {
|
||||
let w = planes.width;
|
||||
let cw = planes.cwidth;
|
||||
let mb_w = w / 16;
|
||||
let y = planes.y[r * 16 * w..(r + 1) * 16 * w].into();
|
||||
let cb = planes.cb[r * 8 * cw..(r + 1) * 8 * cw].into();
|
||||
let cr = planes.cr[r * 8 * cw..(r + 1) * 8 * cw].into();
|
||||
let row_mbs = &mbs[r * mb_w..(r + 1) * mb_w];
|
||||
let mut mv = [vec![[0i16; 2]; mb_w * 16], vec![[0i16; 2]; mb_w * 16]];
|
||||
let mut ref_idx = [vec![-1i8; mb_w * 4], vec![-1i8; mb_w * 4]];
|
||||
let mut ref_id = [vec![u32::MAX; mb_w * 4], vec![u32::MAX; mb_w * 4]];
|
||||
let mut intra = vec![true; mb_w];
|
||||
for (x, st) in row_mbs.iter().enumerate() {
|
||||
if st.slice_num == u32::MAX {
|
||||
continue;
|
||||
}
|
||||
intra[x] = st.kind.is_intra();
|
||||
for l in 0..2 {
|
||||
mv[l][x * 16..x * 16 + 16].copy_from_slice(&st.mv[l]);
|
||||
for b in 0..4 {
|
||||
let ri = st.ref_idx[l][b];
|
||||
ref_idx[l][x * 4 + b] = ri;
|
||||
if ri >= 0 {
|
||||
ref_id[l][x * 4 + b] = ref_ids(st, l, ri);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let [mv0, mv1] = mv;
|
||||
let [ri0, ri1] = ref_idx;
|
||||
let [id0, id1] = ref_id;
|
||||
FrameRow { y, cb, cr, mv: [mv0.into(), mv1.into()], ref_idx: [ri0.into(), ri1.into()], ref_id: [id0.into(), id1.into()], intra: intra.into() }
|
||||
}
|
||||
|
||||
/// A frame with every row published from `planes` and no motion (intra), e.g. "non-existing"
|
||||
/// frames inferred for frame_num gaps.
|
||||
pub fn from_planes(id: u32, poc: i32, planes: &Planes) -> Self {
|
||||
let (mb_w, mb_h) = (planes.width / 16, planes.height / 16);
|
||||
let f = Frame::new(id, poc, mb_w, mb_h);
|
||||
let blank = vec![MbState::default(); mb_w * mb_h];
|
||||
for r in 0..mb_h {
|
||||
f.publish(r, Frame::make_row(planes, r, &blank, &|_, _, _| u32::MAX));
|
||||
}
|
||||
f
|
||||
}
|
||||
|
||||
/// Copy the w x h luma window at (x0, y0) (clamped to the picture) into `out` (stride `os`).
|
||||
#[inline]
|
||||
pub fn luma_window(&self, (x0, y0): (i32, i32), w: usize, h: usize, out: &mut [u8], os: usize) {
|
||||
let (width, height) = (self.width, self.height);
|
||||
for r in 0..h {
|
||||
let yy = (y0 + r as i32).clamp(0, height as i32 - 1) as usize;
|
||||
let row = self.row(yy >> 4);
|
||||
let line = &row.y[(yy & 15) * width..(yy & 15) * width + width];
|
||||
copy_line::<24>(line, x0, &mut out[r * os..], w);
|
||||
}
|
||||
}
|
||||
|
||||
/// Copy the w x h window of chroma component `c` at (x0, y0) (clamped) into `out`.
|
||||
#[inline]
|
||||
pub fn chroma_window(&self, c: usize, (x0, y0): (i32, i32), w: usize, h: usize, out: &mut [u8], os: usize) {
|
||||
let (cw, ch) = (self.cwidth, self.cheight);
|
||||
for r in 0..h {
|
||||
let yy = (y0 + r as i32).clamp(0, ch as i32 - 1) as usize;
|
||||
let row = self.row(yy >> 3);
|
||||
let plane = if c == 0 { &row.cb } else { &row.cr };
|
||||
let line = &plane[(yy & 7) * cw..(yy & 7) * cw + cw];
|
||||
copy_line::<16>(line, x0, &mut out[r * os..], w);
|
||||
}
|
||||
}
|
||||
|
||||
/// Crop and copy the frame into planar buffers (waits for completion).
|
||||
pub fn copy_cropped(&self, crop: (usize, usize, usize, usize)) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
|
||||
let (cx, cy, cw, ch) = crop;
|
||||
let alloc = plane_allocator();
|
||||
let mut y = alloc(cw * ch);
|
||||
for r in cy..cy + ch {
|
||||
let row = self.row(r >> 4);
|
||||
y.extend_from_slice(&row.y[(r & 15) * self.width + cx..(r & 15) * self.width + cx + cw]);
|
||||
}
|
||||
let (ccx, ccy, ccw, cch) = (cx / 2, cy / 2, cw.div_ceil(2), ch.div_ceil(2));
|
||||
let mut u = alloc(ccw * cch);
|
||||
let mut v = alloc(ccw * cch);
|
||||
for r in ccy..ccy + cch {
|
||||
let row = self.row(r >> 3);
|
||||
let o = (r & 7) * self.cwidth + ccx;
|
||||
u.extend_from_slice(&row.cb[o..o + ccw]);
|
||||
v.extend_from_slice(&row.cr[o..o + ccw]);
|
||||
}
|
||||
(y, u, v)
|
||||
}
|
||||
}
|
||||
|
||||
static PLANE_ALLOCATOR: std::sync::OnceLock<fn(usize) -> Vec<u8>> = std::sync::OnceLock::new();
|
||||
|
||||
/// Where output planes come from: an empty buffer with room for the given number of samples.
|
||||
/// The host sets it once to recycle the planes of pictures it is done with; the first call wins.
|
||||
pub fn set_plane_allocator(alloc: fn(usize) -> Vec<u8>) {
|
||||
let _ = PLANE_ALLOCATOR.set(alloc);
|
||||
}
|
||||
|
||||
fn plane_allocator() -> fn(usize) -> Vec<u8> {
|
||||
PLANE_ALLOCATOR.get().copied().unwrap_or(Vec::with_capacity)
|
||||
}
|
||||
|
||||
/// Copy `w` samples of `line` starting at x0 into `out`; when possible a fixed-size block of `N`
|
||||
/// bytes is copied instead (cheaper than a variable-length copy; `out` must then have room for `N`).
|
||||
#[inline(always)]
|
||||
fn copy_line<const N: usize>(line: &[u8], x0: i32, out: &mut [u8], w: usize) {
|
||||
if w <= N
|
||||
&& x0 >= 0
|
||||
&& let Some(src) = line.get(x0 as usize..).and_then(|l| l.first_chunk::<N>())
|
||||
&& let Some(dst) = out.first_chunk_mut::<N>()
|
||||
{
|
||||
*dst = *src;
|
||||
} else {
|
||||
copy_clamped(line, x0, &mut out[..w]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Copy `out.len()` samples of `line` starting at x0, replicating edge samples outside the line.
|
||||
/// Kept out of line so the fixed-size path of `copy_line` stays an inline move rather than being
|
||||
/// merged with this variable-length copy into a libc memcpy call.
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn copy_clamped(line: &[u8], x0: i32, out: &mut [u8]) {
|
||||
let w = out.len();
|
||||
if x0 >= 0 && x0 as usize + w <= line.len() {
|
||||
out.copy_from_slice(&line[x0 as usize..x0 as usize + w]);
|
||||
} else {
|
||||
let max = line.len() as i32 - 1;
|
||||
for (c, o) in out.iter_mut().enumerate() {
|
||||
*o = line[(x0 + c as i32).clamp(0, max) as usize];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type FrameRef = Arc<Frame>;
|
||||
|
||||
/// An entry of RefPicList0/1 for the current slice.
|
||||
#[derive(Clone)]
|
||||
pub struct RefPic {
|
||||
pub frame: FrameRef,
|
||||
pub long_term: bool,
|
||||
}
|
||||
|
||||
impl RefPic {
|
||||
pub fn poc(&self) -> i32 {
|
||||
self.frame.poc
|
||||
}
|
||||
pub fn id(&self) -> u32 {
|
||||
self.frame.id
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, not(feature = "threads")))]
|
||||
mod row_tests {
|
||||
use super::*;
|
||||
|
||||
/// Without threads a row read before it was published is gray, not a panic.
|
||||
#[test]
|
||||
fn an_unpublished_row_reads_as_gray_without_threads() {
|
||||
let f = Frame::new(0, 0, 2, 2);
|
||||
let row = f.row(1);
|
||||
assert!(row.y.iter().chain(row.cb.iter()).chain(row.cr.iter()).all(|&s| s == 128));
|
||||
assert_eq!((row.y.len(), row.cb.len(), row.mv[0].len(), row.ref_idx[1].len(), row.intra.len()), (32 * 16, 16 * 8, 32, 8, 2));
|
||||
assert!(row.intra.iter().all(|&i| i) && row.ref_id[0].iter().all(|&id| id == u32::MAX));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,552 @@
|
||||
//! NAL unit header and slice header parsing (7.3.1, 7.3.3).
|
||||
|
||||
use crate::h264::decode::error::{Result, ensure};
|
||||
use crate::h264::decode::params::{Pps, Sps};
|
||||
use craft_media::bitstream::BitReader;
|
||||
|
||||
/// NAL unit types used by the decoder.
|
||||
pub mod nal_type {
|
||||
pub const SLICE: u8 = 1;
|
||||
pub const SLICE_DPA: u8 = 2;
|
||||
pub const SLICE_DPB: u8 = 3;
|
||||
pub const SLICE_DPC: u8 = 4;
|
||||
pub const IDR: u8 = 5;
|
||||
pub const SEI: u8 = 6;
|
||||
pub const SPS: u8 = 7;
|
||||
pub const PPS: u8 = 8;
|
||||
pub const AUD: u8 = 9;
|
||||
pub const END_SEQ: u8 = 10;
|
||||
pub const END_STREAM: u8 = 11;
|
||||
pub const FILLER: u8 = 12;
|
||||
pub const SPS_EXT: u8 = 13;
|
||||
pub const PREFIX: u8 = 14;
|
||||
pub const SUBSET_SPS: u8 = 15;
|
||||
pub const AUX_SLICE: u8 = 19;
|
||||
pub const SLICE_EXT: u8 = 20;
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct NalHeader {
|
||||
pub nal_ref_idc: u8,
|
||||
pub nal_unit_type: u8,
|
||||
}
|
||||
|
||||
impl NalHeader {
|
||||
pub fn parse(b: u8) -> Result<Self> {
|
||||
ensure!(b & 0x80 == 0, "forbidden_zero_bit set");
|
||||
Ok(Self { nal_ref_idc: (b >> 5) & 3, nal_unit_type: b & 0x1f })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum SliceType {
|
||||
P,
|
||||
B,
|
||||
I,
|
||||
Sp,
|
||||
Si,
|
||||
}
|
||||
|
||||
impl SliceType {
|
||||
fn from_raw(v: u32) -> Result<Self> {
|
||||
Ok(match v % 5 {
|
||||
0 => SliceType::P,
|
||||
1 => SliceType::B,
|
||||
2 => SliceType::I,
|
||||
3 => SliceType::Sp,
|
||||
_ => SliceType::Si,
|
||||
})
|
||||
}
|
||||
pub fn is_intra(self) -> bool {
|
||||
matches!(self, SliceType::I | SliceType::Si)
|
||||
}
|
||||
pub fn is_b(self) -> bool {
|
||||
self == SliceType::B
|
||||
}
|
||||
pub fn is_p(self) -> bool {
|
||||
matches!(self, SliceType::P | SliceType::Sp)
|
||||
}
|
||||
}
|
||||
|
||||
/// One entry of ref_pic_list_modification().
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct RefPicListMod {
|
||||
pub idc: u32,
|
||||
/// abs_diff_pic_num_minus1 or long_term_pic_num.
|
||||
pub value: u32,
|
||||
}
|
||||
|
||||
/// memory_management_control_operation entry.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Mmco {
|
||||
pub op: u32,
|
||||
pub difference_of_pic_nums_minus1: u32,
|
||||
pub long_term_pic_num: u32,
|
||||
pub long_term_frame_idx: u32,
|
||||
pub max_long_term_frame_idx_plus1: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct WeightEntry {
|
||||
pub luma_weight: i32,
|
||||
pub luma_offset: i32,
|
||||
pub luma_flag: bool,
|
||||
pub chroma_weight: [i32; 2],
|
||||
pub chroma_offset: [i32; 2],
|
||||
pub chroma_flag: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct PredWeightTable {
|
||||
pub luma_log2_denom: u32,
|
||||
pub chroma_log2_denom: u32,
|
||||
pub l0: Vec<WeightEntry>,
|
||||
pub l1: Vec<WeightEntry>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SliceHeader {
|
||||
pub nal_ref_idc: u8,
|
||||
pub nal_unit_type: u8,
|
||||
pub idr: bool,
|
||||
pub first_mb_in_slice: u32,
|
||||
pub slice_type_raw: u32,
|
||||
pub slice_type: SliceType,
|
||||
pub pps_id: u32,
|
||||
pub colour_plane_id: u32,
|
||||
pub frame_num: u32,
|
||||
pub field_pic: bool,
|
||||
pub bottom_field: bool,
|
||||
pub idr_pic_id: u32,
|
||||
pub pic_order_cnt_lsb: u32,
|
||||
pub delta_pic_order_cnt_bottom: i32,
|
||||
pub delta_pic_order_cnt: [i32; 2],
|
||||
pub redundant_pic_cnt: u32,
|
||||
pub direct_spatial_mv_pred: bool,
|
||||
pub num_ref_idx_active: [u32; 2],
|
||||
pub ref_pic_list_mod: [Vec<RefPicListMod>; 2],
|
||||
pub pred_weight_table: Option<PredWeightTable>,
|
||||
pub no_output_of_prior_pics: bool,
|
||||
pub long_term_reference: bool,
|
||||
pub adaptive_ref_pic_marking: bool,
|
||||
pub mmcos: Vec<Mmco>,
|
||||
pub cabac_init_idc: u32,
|
||||
pub slice_qp_delta: i32,
|
||||
pub sp_for_switch: bool,
|
||||
pub slice_qs_delta: i32,
|
||||
pub disable_deblocking_filter_idc: u32,
|
||||
pub slice_alpha_c0_offset_div2: i32,
|
||||
pub slice_beta_offset_div2: i32,
|
||||
pub slice_group_change_cycle: u32,
|
||||
/// Bit offset of slice_data() within the RBSP.
|
||||
pub header_bits: usize,
|
||||
}
|
||||
|
||||
impl SliceHeader {
|
||||
pub fn qp(&self, pps: &Pps) -> i32 {
|
||||
pps.pic_init_qp.saturating_add(self.slice_qp_delta)
|
||||
}
|
||||
pub fn has_mmco5(&self) -> bool {
|
||||
self.mmcos.iter().any(|m| m.op == 5)
|
||||
}
|
||||
|
||||
/// ref_pic_list_modification() for both lists (7.3.3.1).
|
||||
fn parse_ref_pic_list_mods(r: &mut BitReader, slice_type: SliceType) -> Result<[Vec<RefPicListMod>; 2]> {
|
||||
let mut ref_pic_list_mod = [Vec::new(), Vec::new()];
|
||||
for (list, mods) in ref_pic_list_mod.iter_mut().enumerate() {
|
||||
let present = if list == 0 { !slice_type.is_intra() } else { slice_type.is_b() };
|
||||
if present && r.read_flag()? {
|
||||
loop {
|
||||
let idc = r.read_ue()?;
|
||||
ensure!(idc <= 5, "modification_of_pic_nums_idc out of range");
|
||||
if idc == 3 {
|
||||
break;
|
||||
}
|
||||
let value = r.read_ue()?;
|
||||
mods.push(RefPicListMod { idc, value });
|
||||
ensure!(mods.len() <= 66, "too many ref_pic_list_modification entries");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(ref_pic_list_mod)
|
||||
}
|
||||
|
||||
/// One pred_weight_table() entry (7.3.3.2).
|
||||
fn parse_weight_entry(r: &mut BitReader, has_chroma: bool, luma_log2_denom: u32, chroma_log2_denom: u32) -> Result<WeightEntry> {
|
||||
let mut e = WeightEntry { luma_weight: 1 << luma_log2_denom, chroma_weight: [1 << chroma_log2_denom; 2], ..Default::default() };
|
||||
if r.read_flag()? {
|
||||
e.luma_flag = true;
|
||||
e.luma_weight = r.read_se()?;
|
||||
e.luma_offset = r.read_se()?;
|
||||
ensure!((-128..=127).contains(&e.luma_weight) && (-128..=127).contains(&e.luma_offset), "luma weight out of range");
|
||||
}
|
||||
if has_chroma && r.read_flag()? {
|
||||
e.chroma_flag = true;
|
||||
for j in 0..2 {
|
||||
e.chroma_weight[j] = r.read_se()?;
|
||||
e.chroma_offset[j] = r.read_se()?;
|
||||
ensure!((-128..=127).contains(&e.chroma_weight[j]) && (-128..=127).contains(&e.chroma_offset[j]), "chroma weight out of range");
|
||||
}
|
||||
}
|
||||
Ok(e)
|
||||
}
|
||||
|
||||
/// pred_weight_table() (7.3.3.2).
|
||||
fn parse_pred_weight_table(r: &mut BitReader, sps: &Sps, slice_type: SliceType, num_ref_idx_active: [u32; 2]) -> Result<PredWeightTable> {
|
||||
let luma_log2_denom = r.read_ue()?;
|
||||
ensure!(luma_log2_denom <= 7, "luma_log2_weight_denom out of range");
|
||||
let has_chroma = sps.chroma_array_type() != 0;
|
||||
let mut chroma_log2_denom = 0;
|
||||
if has_chroma {
|
||||
chroma_log2_denom = r.read_ue()?;
|
||||
ensure!(chroma_log2_denom <= 7, "chroma_log2_weight_denom out of range");
|
||||
}
|
||||
let mut lists = [Vec::new(), Vec::new()];
|
||||
let nlists = if slice_type.is_b() { 2 } else { 1 };
|
||||
for (l, entries) in lists.iter_mut().enumerate().take(nlists) {
|
||||
for _ in 0..num_ref_idx_active[l] {
|
||||
entries.push(Self::parse_weight_entry(r, has_chroma, luma_log2_denom, chroma_log2_denom)?);
|
||||
}
|
||||
}
|
||||
let [l0, l1] = lists;
|
||||
Ok(PredWeightTable { luma_log2_denom, chroma_log2_denom, l0, l1 })
|
||||
}
|
||||
|
||||
/// The memory_management_control_operation list of dec_ref_pic_marking() (7.3.3.3).
|
||||
fn parse_mmcos(r: &mut BitReader) -> Result<Vec<Mmco>> {
|
||||
let mut mmcos = Vec::new();
|
||||
loop {
|
||||
let op = r.read_ue()?;
|
||||
ensure!(op <= 6, "memory_management_control_operation out of range");
|
||||
if op == 0 {
|
||||
return Ok(mmcos);
|
||||
}
|
||||
let mut m = Mmco { op, difference_of_pic_nums_minus1: 0, long_term_pic_num: 0, long_term_frame_idx: 0, max_long_term_frame_idx_plus1: 0 };
|
||||
if op == 1 || op == 3 {
|
||||
m.difference_of_pic_nums_minus1 = r.read_ue()?;
|
||||
}
|
||||
if op == 2 {
|
||||
m.long_term_pic_num = r.read_ue()?;
|
||||
}
|
||||
if op == 3 || op == 6 {
|
||||
m.long_term_frame_idx = r.read_ue()?;
|
||||
}
|
||||
if op == 4 {
|
||||
m.max_long_term_frame_idx_plus1 = r.read_ue()?;
|
||||
}
|
||||
mmcos.push(m);
|
||||
ensure!(mmcos.len() <= 66, "too many MMCOs");
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the slice header. `pps_lookup` resolves pic_parameter_set_id to (PPS, SPS).
|
||||
pub fn parse<'a>(
|
||||
rbsp: &[u8],
|
||||
nal: NalHeader,
|
||||
pps_lookup: impl FnOnce(u32) -> Result<(&'a Pps, &'a Sps)>,
|
||||
) -> Result<(SliceHeader, &'a Pps, &'a Sps)> {
|
||||
let mut r = BitReader::new(rbsp);
|
||||
let idr = nal.nal_unit_type == nal_type::IDR;
|
||||
let first_mb_in_slice = r.read_ue()?;
|
||||
let slice_type_raw = r.read_ue()?;
|
||||
ensure!(slice_type_raw <= 9, "slice_type out of range");
|
||||
let slice_type = SliceType::from_raw(slice_type_raw)?;
|
||||
let pps_id = r.read_ue()?;
|
||||
ensure!(pps_id < 256, "pic_parameter_set_id out of range");
|
||||
let (pps, sps) = pps_lookup(pps_id)?;
|
||||
let mut colour_plane_id = 0;
|
||||
if sps.separate_colour_plane {
|
||||
colour_plane_id = r.read_bits(2)?;
|
||||
}
|
||||
let frame_num = r.read_bits(sps.log2_max_frame_num)?;
|
||||
let mut field_pic = false;
|
||||
let mut bottom_field = false;
|
||||
if !sps.frame_mbs_only {
|
||||
field_pic = r.read_flag()?;
|
||||
if field_pic {
|
||||
bottom_field = r.read_flag()?;
|
||||
}
|
||||
}
|
||||
let idr_pic_id = if idr { r.read_ue()? } else { 0 };
|
||||
let mut pic_order_cnt_lsb = 0;
|
||||
let mut delta_pic_order_cnt_bottom = 0;
|
||||
let mut delta_pic_order_cnt = [0; 2];
|
||||
if sps.pic_order_cnt_type == 0 {
|
||||
pic_order_cnt_lsb = r.read_bits(sps.log2_max_poc_lsb)?;
|
||||
if pps.bottom_field_pic_order_in_frame_present && !field_pic {
|
||||
delta_pic_order_cnt_bottom = r.read_se()?;
|
||||
}
|
||||
}
|
||||
if sps.pic_order_cnt_type == 1 && !sps.delta_pic_order_always_zero {
|
||||
delta_pic_order_cnt[0] = r.read_se()?;
|
||||
if pps.bottom_field_pic_order_in_frame_present && !field_pic {
|
||||
delta_pic_order_cnt[1] = r.read_se()?;
|
||||
}
|
||||
}
|
||||
let redundant_pic_cnt = if pps.redundant_pic_cnt_present { r.read_ue()? } else { 0 };
|
||||
let direct_spatial_mv_pred = if slice_type.is_b() { r.read_flag()? } else { false };
|
||||
let mut num_ref_idx_active = [pps.num_ref_idx_l0_default_active, pps.num_ref_idx_l1_default_active];
|
||||
if !slice_type.is_intra() {
|
||||
if r.read_flag()? {
|
||||
num_ref_idx_active[0] = r.read_ue_plus(1)?;
|
||||
if slice_type.is_b() {
|
||||
num_ref_idx_active[1] = r.read_ue_plus(1)?;
|
||||
}
|
||||
}
|
||||
let max = if field_pic { 32 } else { 16 };
|
||||
ensure!(num_ref_idx_active[0] <= max && num_ref_idx_active[1] <= max, "num_ref_idx_active out of range");
|
||||
}
|
||||
if !slice_type.is_b() {
|
||||
num_ref_idx_active[1] = 0;
|
||||
}
|
||||
if slice_type.is_intra() {
|
||||
num_ref_idx_active[0] = 0;
|
||||
}
|
||||
ensure!(nal.nal_unit_type != 20 && nal.nal_unit_type != 21, "MVC slices are not supported");
|
||||
let ref_pic_list_mod = Self::parse_ref_pic_list_mods(&mut r, slice_type)?;
|
||||
let mut pred_weight_table = None;
|
||||
if (pps.weighted_pred && slice_type.is_p()) || (pps.weighted_bipred_idc == 1 && slice_type.is_b()) {
|
||||
pred_weight_table = Some(Self::parse_pred_weight_table(&mut r, sps, slice_type, num_ref_idx_active)?);
|
||||
}
|
||||
let mut no_output_of_prior_pics = false;
|
||||
let mut long_term_reference = false;
|
||||
let mut adaptive_ref_pic_marking = false;
|
||||
let mut mmcos = Vec::new();
|
||||
if nal.nal_ref_idc != 0 {
|
||||
if idr {
|
||||
no_output_of_prior_pics = r.read_flag()?;
|
||||
long_term_reference = r.read_flag()?;
|
||||
} else {
|
||||
adaptive_ref_pic_marking = r.read_flag()?;
|
||||
if adaptive_ref_pic_marking {
|
||||
mmcos = Self::parse_mmcos(&mut r)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut cabac_init_idc = 0;
|
||||
if pps.entropy_coding_mode && !slice_type.is_intra() {
|
||||
cabac_init_idc = r.read_ue()?;
|
||||
ensure!(cabac_init_idc <= 2, "cabac_init_idc out of range");
|
||||
}
|
||||
let slice_qp_delta = r.read_se()?;
|
||||
let Some(qp) = pps.pic_init_qp.checked_add(slice_qp_delta) else { return crate::h264::decode::error::invalid("slice QP out of range") };
|
||||
ensure!((0..=51).contains(&qp), "slice QP {qp} out of range");
|
||||
let mut sp_for_switch = false;
|
||||
let mut slice_qs_delta = 0;
|
||||
if matches!(slice_type, SliceType::Sp | SliceType::Si) {
|
||||
if slice_type == SliceType::Sp {
|
||||
sp_for_switch = r.read_flag()?;
|
||||
}
|
||||
slice_qs_delta = r.read_se()?;
|
||||
}
|
||||
let mut disable_deblocking_filter_idc = 0;
|
||||
let mut slice_alpha_c0_offset_div2 = 0;
|
||||
let mut slice_beta_offset_div2 = 0;
|
||||
if pps.deblocking_filter_control_present {
|
||||
disable_deblocking_filter_idc = r.read_ue()?;
|
||||
ensure!(disable_deblocking_filter_idc <= 2, "disable_deblocking_filter_idc out of range");
|
||||
if disable_deblocking_filter_idc != 1 {
|
||||
slice_alpha_c0_offset_div2 = r.read_se()?;
|
||||
slice_beta_offset_div2 = r.read_se()?;
|
||||
ensure!(
|
||||
(-6..=6).contains(&slice_alpha_c0_offset_div2) && (-6..=6).contains(&slice_beta_offset_div2),
|
||||
"deblocking offsets out of range"
|
||||
);
|
||||
}
|
||||
}
|
||||
let mut slice_group_change_cycle = 0;
|
||||
if pps.num_slice_groups > 1 && (3..=5).contains(&pps.slice_group_map_type) {
|
||||
// Ceil(Log2(PicSizeInMapUnits / SliceGroupChangeRate + 1)); rate is not retained, so read
|
||||
// conservatively using the picture size (FMO is unsupported for decoding anyway).
|
||||
let pic_size = sps.pic_width_in_mbs * sps.pic_height_in_map_units;
|
||||
let bits = 32 - pic_size.leading_zeros();
|
||||
slice_group_change_cycle = r.read_bits(bits)?;
|
||||
}
|
||||
let header_bits = r.position();
|
||||
Ok((
|
||||
SliceHeader {
|
||||
nal_ref_idc: nal.nal_ref_idc,
|
||||
nal_unit_type: nal.nal_unit_type,
|
||||
idr,
|
||||
first_mb_in_slice,
|
||||
slice_type_raw,
|
||||
slice_type,
|
||||
pps_id,
|
||||
colour_plane_id,
|
||||
frame_num,
|
||||
field_pic,
|
||||
bottom_field,
|
||||
idr_pic_id,
|
||||
pic_order_cnt_lsb,
|
||||
delta_pic_order_cnt_bottom,
|
||||
delta_pic_order_cnt,
|
||||
redundant_pic_cnt,
|
||||
direct_spatial_mv_pred,
|
||||
num_ref_idx_active,
|
||||
ref_pic_list_mod,
|
||||
pred_weight_table,
|
||||
no_output_of_prior_pics,
|
||||
long_term_reference,
|
||||
adaptive_ref_pic_marking,
|
||||
mmcos,
|
||||
cabac_init_idc,
|
||||
slice_qp_delta,
|
||||
sp_for_switch,
|
||||
slice_qs_delta,
|
||||
disable_deblocking_filter_idc,
|
||||
slice_alpha_c0_offset_div2,
|
||||
slice_beta_offset_div2,
|
||||
slice_group_change_cycle,
|
||||
header_bits,
|
||||
},
|
||||
pps,
|
||||
sps,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// POC decoding state carried between pictures (8.2.1).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct PocState {
|
||||
/// PicOrderCntMsb / pic_order_cnt_lsb of the previous reference picture (type 0).
|
||||
prev_ref_msb: i32,
|
||||
prev_ref_lsb: i32,
|
||||
/// FrameNumOffset / frame_num of the previous picture (types 1 and 2).
|
||||
prev_frame_num_offset: i32,
|
||||
prev_frame_num: u32,
|
||||
}
|
||||
|
||||
/// Is `sh` the first slice of a new primary coded picture relative to `prev` (7.4.1.2.4)?
|
||||
pub fn is_new_picture(prev: &SliceHeader, sh: &SliceHeader, sps: &Sps) -> bool {
|
||||
if sh.frame_num != prev.frame_num
|
||||
|| sh.pps_id != prev.pps_id
|
||||
|| sh.field_pic != prev.field_pic
|
||||
|| sh.bottom_field != prev.bottom_field
|
||||
|| (sh.nal_ref_idc == 0) != (prev.nal_ref_idc == 0)
|
||||
|| sh.idr != prev.idr
|
||||
|| (sh.idr && prev.idr && sh.idr_pic_id != prev.idr_pic_id)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if sps.pic_order_cnt_type == 0
|
||||
&& (sh.pic_order_cnt_lsb != prev.pic_order_cnt_lsb || sh.delta_pic_order_cnt_bottom != prev.delta_pic_order_cnt_bottom)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if sps.pic_order_cnt_type == 1 && sh.delta_pic_order_cnt != prev.delta_pic_order_cnt {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Output of POC computation.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Poc {
|
||||
pub top: i32,
|
||||
pub bottom: i32,
|
||||
msb: i32,
|
||||
frame_num_offset: i32,
|
||||
}
|
||||
|
||||
impl Poc {
|
||||
pub fn frame(&self) -> i32 {
|
||||
self.top.min(self.bottom)
|
||||
}
|
||||
}
|
||||
|
||||
impl PocState {
|
||||
/// Compute TopFieldOrderCnt/BottomFieldOrderCnt of a frame (8.2.1.1-8.2.1.3).
|
||||
pub fn compute(&self, sh: &SliceHeader, sps: &Sps) -> Poc {
|
||||
match sps.pic_order_cnt_type {
|
||||
0 => {
|
||||
let (prev_msb, prev_lsb) = if sh.idr { (0, 0) } else { (self.prev_ref_msb, self.prev_ref_lsb) };
|
||||
let max_lsb = 1i32 << sps.log2_max_poc_lsb;
|
||||
let lsb = sh.pic_order_cnt_lsb as i32;
|
||||
let msb = if lsb < prev_lsb && prev_lsb - lsb >= max_lsb / 2 {
|
||||
prev_msb + max_lsb
|
||||
} else if lsb > prev_lsb && lsb - prev_lsb > max_lsb / 2 {
|
||||
prev_msb - max_lsb
|
||||
} else {
|
||||
prev_msb
|
||||
};
|
||||
let top = msb + lsb;
|
||||
let bottom = top + sh.delta_pic_order_cnt_bottom;
|
||||
Poc { top, bottom, msb, frame_num_offset: 0 }
|
||||
}
|
||||
1 => {
|
||||
let frame_num_offset = self.frame_num_offset(sh.idr, sh.frame_num, sps);
|
||||
let n = sps.offset_for_ref_frame.len() as i32;
|
||||
let mut abs_frame_num = if n != 0 { frame_num_offset + sh.frame_num as i32 } else { 0 };
|
||||
if sh.nal_ref_idc == 0 && abs_frame_num > 0 {
|
||||
abs_frame_num -= 1;
|
||||
}
|
||||
let mut expected = 0i32;
|
||||
if abs_frame_num > 0 {
|
||||
let cycle_cnt = (abs_frame_num - 1) / n;
|
||||
let in_cycle = (abs_frame_num - 1) % n;
|
||||
let delta_cycle: i32 = sps.offset_for_ref_frame.iter().sum();
|
||||
expected = cycle_cnt.wrapping_mul(delta_cycle);
|
||||
for &o in &sps.offset_for_ref_frame[..=in_cycle as usize] {
|
||||
expected += o;
|
||||
}
|
||||
}
|
||||
if sh.nal_ref_idc == 0 {
|
||||
expected += sps.offset_for_non_ref_pic;
|
||||
}
|
||||
let top = expected + sh.delta_pic_order_cnt[0];
|
||||
let bottom = top + sps.offset_for_top_to_bottom_field + sh.delta_pic_order_cnt[1];
|
||||
Poc { top, bottom, msb: 0, frame_num_offset }
|
||||
}
|
||||
_ => {
|
||||
let frame_num_offset = self.frame_num_offset(sh.idr, sh.frame_num, sps);
|
||||
let temp = if sh.idr {
|
||||
0
|
||||
} else if sh.nal_ref_idc == 0 {
|
||||
2 * (frame_num_offset + sh.frame_num as i32) - 1
|
||||
} else {
|
||||
2 * (frame_num_offset + sh.frame_num as i32)
|
||||
};
|
||||
Poc { top: temp, bottom: temp, msb: 0, frame_num_offset }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn frame_num_offset(&self, idr: bool, frame_num: u32, sps: &Sps) -> i32 {
|
||||
if idr {
|
||||
0
|
||||
} else if self.prev_frame_num > frame_num {
|
||||
self.prev_frame_num_offset + sps.max_frame_num() as i32
|
||||
} else {
|
||||
self.prev_frame_num_offset
|
||||
}
|
||||
}
|
||||
|
||||
/// Update after a picture has been decoded. `poc` is the value returned by [`PocState::compute`]
|
||||
/// (before any MMCO5 adjustment).
|
||||
pub fn update(&mut self, sh: &SliceHeader, poc: &Poc) {
|
||||
let mmco5 = sh.has_mmco5();
|
||||
if sh.nal_ref_idc != 0 {
|
||||
if mmco5 {
|
||||
self.prev_ref_msb = 0;
|
||||
self.prev_ref_lsb = poc.top - poc.frame();
|
||||
} else {
|
||||
self.prev_ref_msb = poc.msb;
|
||||
self.prev_ref_lsb = sh.pic_order_cnt_lsb as i32;
|
||||
}
|
||||
}
|
||||
if mmco5 {
|
||||
self.prev_frame_num_offset = 0;
|
||||
self.prev_frame_num = 0;
|
||||
} else {
|
||||
self.prev_frame_num_offset = poc.frame_num_offset;
|
||||
self.prev_frame_num = sh.frame_num;
|
||||
}
|
||||
}
|
||||
|
||||
/// Account for a "non-existing" frame inferred by the frame_num gap process (8.2.5.2).
|
||||
pub fn update_gap_frame(&mut self, frame_num: u32, sps: &Sps) {
|
||||
let off = self.frame_num_offset(false, frame_num, sps);
|
||||
self.prev_frame_num_offset = off;
|
||||
self.prev_frame_num = frame_num;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,820 @@
|
||||
//! Hand-built bitstreams exercising features libx264 never emits: I_PCM (CAVLC and CABAC), long-term
|
||||
//! references, MMCO operations, frame_num gaps and reference list modification. Every picture consists
|
||||
//! of I_PCM and P_Skip macroblocks only, so the expected output is known exactly (I_PCM samples are
|
||||
//! lossless, P_Skip copies the selected reference with a zero motion vector, and deblocking cannot
|
||||
//! modify samples at these QPs).
|
||||
|
||||
use crate::h264::decode::Decoder;
|
||||
use crate::h264::decode::cabac::{Cabac, NEXT_STATE, RANGE_TAB_LPS};
|
||||
use craft_media::bitstream::{BitWriter, escape_rbsp};
|
||||
|
||||
const MB_W: usize = 4;
|
||||
const MB_H: usize = 3;
|
||||
const NMB: usize = MB_W * MB_H;
|
||||
|
||||
fn nal(ref_idc: u8, t: u8, rbsp: &[u8]) -> Vec<u8> {
|
||||
let mut v = vec![0, 0, 0, 1, (ref_idc << 5) | t];
|
||||
v.extend(escape_rbsp(rbsp));
|
||||
v
|
||||
}
|
||||
|
||||
/// POC type used by the synthetic SPS: 0 (4-bit lsb), 1 (see below) or 2.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum PocType {
|
||||
T0,
|
||||
/// offset_for_non_ref_pic = -3, one reference frame per cycle with offset 4
|
||||
T1,
|
||||
T2,
|
||||
}
|
||||
|
||||
fn sps(profile: u8, max_refs: u32, gaps: bool, poc: PocType) -> Vec<u8> {
|
||||
let mut w = BitWriter::new();
|
||||
w.write_bits(profile as u32, 8);
|
||||
w.write_bits(0, 8);
|
||||
w.write_bits(30, 8);
|
||||
w.write_ue(0); // sps id
|
||||
w.write_ue(0); // log2_max_frame_num - 4 -> 16
|
||||
match poc {
|
||||
PocType::T0 => {
|
||||
w.write_ue(0);
|
||||
w.write_ue(0); // log2_max_pic_order_cnt_lsb - 4 -> 4-bit lsb
|
||||
}
|
||||
PocType::T1 => {
|
||||
w.write_ue(1);
|
||||
w.write_bit(false); // delta_pic_order_always_zero_flag
|
||||
w.write_se(-3); // offset_for_non_ref_pic
|
||||
w.write_se(0); // offset_for_top_to_bottom_field
|
||||
w.write_ue(1); // num_ref_frames_in_pic_order_cnt_cycle
|
||||
w.write_se(4);
|
||||
}
|
||||
PocType::T2 => w.write_ue(2), // output order = decoding order
|
||||
}
|
||||
w.write_ue(max_refs);
|
||||
w.write_bit(gaps);
|
||||
w.write_ue(MB_W as u32 - 1);
|
||||
w.write_ue(MB_H as u32 - 1);
|
||||
w.write_bit(true); // frame_mbs_only
|
||||
w.write_bit(true); // direct_8x8_inference
|
||||
w.write_bit(false); // cropping
|
||||
w.write_bit(false); // vui
|
||||
w.rbsp_trailing();
|
||||
nal(3, 7, &w.finish())
|
||||
}
|
||||
|
||||
fn pps(cabac: bool, weighted: bool) -> Vec<u8> {
|
||||
let mut w = BitWriter::new();
|
||||
w.write_ue(0);
|
||||
w.write_ue(0);
|
||||
w.write_bit(cabac);
|
||||
w.write_bit(false);
|
||||
w.write_ue(0); // slice groups
|
||||
w.write_ue(0); // num_ref_idx_l0_default - 1
|
||||
w.write_ue(0);
|
||||
w.write_bit(weighted); // weighted_pred
|
||||
w.write_bits(if weighted { 1 } else { 0 }, 2); // explicit weighted bi-prediction
|
||||
w.write_se(0); // pic_init_qp - 26
|
||||
w.write_se(0);
|
||||
w.write_se(0); // chroma_qp_index_offset
|
||||
w.write_bit(true); // deblocking_filter_control_present
|
||||
w.write_bit(false);
|
||||
w.write_bit(false);
|
||||
w.rbsp_trailing();
|
||||
nal(3, 8, &w.finish())
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
struct Pic {
|
||||
idr: bool,
|
||||
frame_num: u32,
|
||||
ref_idc: u8,
|
||||
long_term_ref: bool,
|
||||
/// ref_pic_list_modification entries (idc, value)
|
||||
mods: Vec<(u32, u32)>,
|
||||
/// MMCOs: (op, a, b) with a = difference_of_pic_nums_minus1 / long_term_pic_num /
|
||||
/// max_long_term_frame_idx_plus1 and b = long_term_frame_idx
|
||||
mmcos: Vec<(u32, u32, u32)>,
|
||||
/// Per MB: Some(samples) for I_PCM, None for P_Skip / B_Skip.
|
||||
mbs: Vec<Option<Vec<u8>>>,
|
||||
/// B slice (all B_Skip).
|
||||
b: bool,
|
||||
poc_lsb: Option<u32>,
|
||||
/// delta_pic_order_cnt[0] (POC type 1).
|
||||
poc_delta: Option<i32>,
|
||||
/// Explicit weights (luma, cb, cr) as (weight, offset) for refIdx 0 of list 0 and list 1;
|
||||
/// log2 denominators are 5 (luma) and 3 (chroma).
|
||||
wt: Option<[[(i32, i32); 3]; 2]>,
|
||||
/// first_mb_in_slice (`mbs` then lists the slice's macroblocks from there).
|
||||
first_mb: u32,
|
||||
}
|
||||
|
||||
impl Pic {
|
||||
fn is_p(&self) -> bool {
|
||||
!self.idr && !self.b && self.mbs.iter().any(|m| m.is_none())
|
||||
}
|
||||
fn inter(&self) -> bool {
|
||||
self.b || self.is_p()
|
||||
}
|
||||
}
|
||||
|
||||
fn slice_header(w: &mut BitWriter, p: &Pic, cabac: bool) {
|
||||
w.write_ue(p.first_mb);
|
||||
w.write_ue(if p.b {
|
||||
6
|
||||
} else if p.is_p() {
|
||||
5
|
||||
} else {
|
||||
7
|
||||
});
|
||||
w.write_ue(0); // pps id
|
||||
w.write_bits(p.frame_num, 4);
|
||||
if p.idr {
|
||||
w.write_ue(0);
|
||||
}
|
||||
if let Some(lsb) = p.poc_lsb {
|
||||
w.write_bits(lsb, 4);
|
||||
}
|
||||
if let Some(d) = p.poc_delta {
|
||||
w.write_se(d);
|
||||
}
|
||||
if p.b {
|
||||
w.write_bit(true); // direct_spatial_mv_pred_flag
|
||||
}
|
||||
if p.inter() {
|
||||
w.write_bit(false); // num_ref_idx_override
|
||||
w.write_bit(!p.mods.is_empty());
|
||||
if !p.mods.is_empty() {
|
||||
for &(idc, v) in &p.mods {
|
||||
w.write_ue(idc);
|
||||
w.write_ue(v);
|
||||
}
|
||||
w.write_ue(3);
|
||||
}
|
||||
if p.b {
|
||||
w.write_bit(false); // no list 1 modification
|
||||
}
|
||||
}
|
||||
if let (Some(wt), true) = (p.wt, p.inter()) {
|
||||
w.write_ue(5);
|
||||
w.write_ue(3);
|
||||
for l in 0..if p.b { 2 } else { 1 } {
|
||||
w.write_bit(true);
|
||||
w.write_se(wt[l][0].0);
|
||||
w.write_se(wt[l][0].1);
|
||||
w.write_bit(true);
|
||||
for c in 1..3 {
|
||||
w.write_se(wt[l][c].0);
|
||||
w.write_se(wt[l][c].1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if p.ref_idc != 0 {
|
||||
if p.idr {
|
||||
w.write_bit(false);
|
||||
w.write_bit(p.long_term_ref);
|
||||
} else {
|
||||
w.write_bit(!p.mmcos.is_empty());
|
||||
if !p.mmcos.is_empty() {
|
||||
for &(op, a, b) in &p.mmcos {
|
||||
w.write_ue(op);
|
||||
match op {
|
||||
1 => w.write_ue(a),
|
||||
2 => w.write_ue(a),
|
||||
3 => {
|
||||
w.write_ue(a);
|
||||
w.write_ue(b);
|
||||
}
|
||||
4 => w.write_ue(a),
|
||||
6 => w.write_ue(b),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
w.write_ue(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
if cabac && p.inter() {
|
||||
w.write_ue(0); // cabac_init_idc
|
||||
}
|
||||
w.write_se(0); // slice_qp_delta
|
||||
w.write_ue(0); // disable_deblocking_filter_idc
|
||||
w.write_se(0);
|
||||
w.write_se(0);
|
||||
}
|
||||
|
||||
/// Minimal CABAC encoder (9.3.4, informative) writing into a BitWriter.
|
||||
struct Enc {
|
||||
low: u32,
|
||||
range: u32,
|
||||
outstanding: u32,
|
||||
first: bool,
|
||||
ctx: [u8; 1024],
|
||||
}
|
||||
|
||||
impl Enc {
|
||||
fn new(qp: i32, init: usize) -> Self {
|
||||
let c = Cabac::new(&[0, 0, 0, 0], 0, qp, init).unwrap();
|
||||
Enc { low: 0, range: 510, outstanding: 0, first: true, ctx: c.ctx }
|
||||
}
|
||||
fn init(&mut self) {
|
||||
self.low = 0;
|
||||
self.range = 510;
|
||||
self.outstanding = 0;
|
||||
self.first = true;
|
||||
}
|
||||
fn put(&mut self, w: &mut BitWriter, b: bool) {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
w.write_bit(b);
|
||||
}
|
||||
while self.outstanding > 0 {
|
||||
w.write_bit(!b);
|
||||
self.outstanding -= 1;
|
||||
}
|
||||
}
|
||||
fn renorm(&mut self, w: &mut BitWriter) {
|
||||
while self.range < 256 {
|
||||
if self.low < 256 {
|
||||
self.put(w, false);
|
||||
} else if self.low >= 512 {
|
||||
self.low -= 512;
|
||||
self.put(w, true);
|
||||
} else {
|
||||
self.low -= 256;
|
||||
self.outstanding += 1;
|
||||
}
|
||||
self.range <<= 1;
|
||||
self.low <<= 1;
|
||||
}
|
||||
}
|
||||
fn encode(&mut self, w: &mut BitWriter, ctx_idx: usize, bin: u32) {
|
||||
let s = self.ctx[ctx_idx] as usize;
|
||||
let lps = RANGE_TAB_LPS[s >> 1][((self.range >> 6) & 3) as usize] as u32;
|
||||
self.range -= lps;
|
||||
if bin != (s & 1) as u32 {
|
||||
self.low += self.range;
|
||||
self.range = lps;
|
||||
self.ctx[ctx_idx] = NEXT_STATE[s][1];
|
||||
} else {
|
||||
self.ctx[ctx_idx] = NEXT_STATE[s][0];
|
||||
}
|
||||
self.renorm(w);
|
||||
}
|
||||
fn terminate(&mut self, w: &mut BitWriter, bin: bool) {
|
||||
self.range -= 2;
|
||||
if bin {
|
||||
self.low += self.range;
|
||||
// EncodeFlush
|
||||
self.range = 2;
|
||||
self.renorm(w);
|
||||
self.put(w, (self.low >> 9) & 1 != 0);
|
||||
w.write_bits(((self.low >> 7) & 3) | 1, 2);
|
||||
} else {
|
||||
self.renorm(w);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_pcm(w: &mut BitWriter, samples: &[u8]) {
|
||||
w.align_zero();
|
||||
w.write_bytes(samples);
|
||||
}
|
||||
|
||||
fn slice_nal(p: &Pic, cabac: bool) -> Vec<u8> {
|
||||
let mut w = BitWriter::new();
|
||||
slice_header(&mut w, p, cabac);
|
||||
if !cabac && p.b {
|
||||
w.write_ue(NMB as u32);
|
||||
w.rbsp_trailing();
|
||||
} else if !cabac {
|
||||
let mut run = 0;
|
||||
for mb in &p.mbs {
|
||||
match mb {
|
||||
None => run += 1,
|
||||
Some(s) => {
|
||||
if p.is_p() {
|
||||
w.write_ue(run);
|
||||
run = 0;
|
||||
w.write_ue(30); // I_PCM in a P slice
|
||||
} else {
|
||||
w.write_ue(25);
|
||||
}
|
||||
write_pcm(&mut w, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
if run > 0 {
|
||||
w.write_ue(run);
|
||||
}
|
||||
w.rbsp_trailing();
|
||||
} else {
|
||||
while !w.is_byte_aligned() {
|
||||
w.write_bit(true); // cabac_alignment_one_bit
|
||||
}
|
||||
let mut e = Enc::new(26, if p.inter() { 0 } else { 3 });
|
||||
let skip = |i: usize| p.mbs[i].is_none();
|
||||
for (i, mb) in p.mbs.iter().enumerate() {
|
||||
let (x, y) = (i % MB_W, i / MB_W);
|
||||
let a = if x > 0 { Some(i - 1) } else { None };
|
||||
let b = if y > 0 { Some(i - MB_W) } else { None };
|
||||
if p.inter() {
|
||||
let inc = a.map(|n| !skip(n) as usize).unwrap_or(0) + b.map(|n| !skip(n) as usize).unwrap_or(0);
|
||||
e.encode(&mut w, if p.b { 24 } else { 11 } + inc, skip(i) as u32);
|
||||
}
|
||||
if let Some(s) = mb {
|
||||
if p.is_p() {
|
||||
e.encode(&mut w, 14, 1); // intra prefix
|
||||
e.encode(&mut w, 17, 1); // not I_NxN
|
||||
} else {
|
||||
// neighbours are I_PCM (not I_NxN) -> condTerm 1
|
||||
let inc = a.is_some() as usize + b.is_some() as usize;
|
||||
e.encode(&mut w, 3 + inc, 1);
|
||||
}
|
||||
e.terminate(&mut w, true); // I_PCM
|
||||
write_pcm(&mut w, s);
|
||||
e.init();
|
||||
}
|
||||
e.terminate(&mut w, i + 1 == NMB); // end_of_slice_flag
|
||||
}
|
||||
w.align_zero();
|
||||
}
|
||||
let t = if p.idr { 5 } else { 1 };
|
||||
nal(p.ref_idc, t, &w.finish())
|
||||
}
|
||||
|
||||
fn random_mb(seed: &mut u32) -> Vec<u8> {
|
||||
(0..384)
|
||||
.map(|_| {
|
||||
*seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
|
||||
(*seed >> 24) as u8
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Expected cropped planes of a picture made of PCM blocks (`None` = copy from `reference`).
|
||||
fn render(mbs: &[Option<Vec<u8>>], reference: Option<&(Vec<u8>, Vec<u8>, Vec<u8>)>) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
|
||||
let (w, h) = (MB_W * 16, MB_H * 16);
|
||||
let (mut y, mut u, mut v) = match reference {
|
||||
Some(r) => r.clone(),
|
||||
None => (vec![0; w * h], vec![0; w * h / 4], vec![0; w * h / 4]),
|
||||
};
|
||||
for (i, mb) in mbs.iter().enumerate() {
|
||||
let Some(s) = mb else { continue };
|
||||
let (x0, y0) = ((i % MB_W) * 16, (i / MB_W) * 16);
|
||||
for r in 0..16 {
|
||||
y[(y0 + r) * w + x0..(y0 + r) * w + x0 + 16].copy_from_slice(&s[r * 16..r * 16 + 16]);
|
||||
}
|
||||
for r in 0..8 {
|
||||
let o = (y0 / 2 + r) * (w / 2) + x0 / 2;
|
||||
u[o..o + 8].copy_from_slice(&s[256 + r * 8..256 + r * 8 + 8]);
|
||||
v[o..o + 8].copy_from_slice(&s[320 + r * 8..320 + r * 8 + 8]);
|
||||
}
|
||||
}
|
||||
(y, u, v)
|
||||
}
|
||||
|
||||
fn build_and_check(cabac: bool, threads: usize) {
|
||||
let mut seed = 99u32;
|
||||
let pcm_pic = |seed: &mut u32| (0..NMB).map(|_| Some(random_mb(seed))).collect::<Vec<_>>();
|
||||
let a = pcm_pic(&mut seed);
|
||||
let b = pcm_pic(&mut seed);
|
||||
let mixed: Vec<Option<Vec<u8>>> = (0..NMB).map(|i| if i % 3 == 1 { Some(random_mb(&mut seed)) } else { None }).collect();
|
||||
let skip = vec![None; NMB];
|
||||
let pics = vec![
|
||||
// 0: IDR, all PCM, marked long-term (LongTermFrameIdx 0)
|
||||
Pic { idr: true, frame_num: 0, ref_idc: 3, long_term_ref: true, mbs: a.clone(), ..Default::default() },
|
||||
// 1: all PCM, short-term
|
||||
Pic { frame_num: 1, ref_idc: 2, mbs: b.clone(), ..Default::default() },
|
||||
// 2: P_Skip from the long-term picture via modification idc 2 (default list would use pic 1)
|
||||
Pic { frame_num: 2, ref_idc: 2, mods: vec![(2, 0)], mbs: skip.clone(), ..Default::default() },
|
||||
// 3: frame_num gap (3, 4 missing); P_Skip from pic 2 (PicNum 2) via idc 0: 5 - (2 + 1) = 2
|
||||
Pic { frame_num: 5, ref_idc: 2, mods: vec![(0, 2)], mbs: skip.clone(), ..Default::default() },
|
||||
// 4: mixed PCM / P_Skip from pic 1 (PicNum 1: 6 - 5); MMCO 4 raises MaxLongTermFrameIdx to 2,
|
||||
// MMCO 2 drops long-term 0, MMCO 3 makes pic 1 (difference 6 - 1 - 1 = 4) long-term index 1
|
||||
Pic { frame_num: 6, ref_idc: 2, mods: vec![(0, 4)], mmcos: vec![(4, 3, 0), (2, 0, 0), (3, 4, 1)], mbs: mixed.clone(), ..Default::default() },
|
||||
// 5: non-reference P_Skip from long-term index 1 (= pic 1)
|
||||
Pic { frame_num: 7, ref_idc: 0, mods: vec![(2, 1)], mbs: skip.clone(), ..Default::default() },
|
||||
// 6: P_Skip with the default list: short-term by descending PicNum -> pic 4 first
|
||||
Pic { frame_num: 7, ref_idc: 2, mmcos: vec![(1, 0, 0), (6, 0, 2)], mbs: skip.clone(), ..Default::default() },
|
||||
// 7: MMCO 5 picture made of PCM, then 8: P_Skip referencing it (only reference left)
|
||||
Pic { frame_num: 8, ref_idc: 2, mmcos: vec![(5, 0, 0)], mbs: pcm_pic(&mut seed), ..Default::default() },
|
||||
];
|
||||
let mut stream = sps(if cabac { 77 } else { 66 }, 8, true, PocType::T2);
|
||||
stream.extend(pps(cabac, false));
|
||||
for p in &pics {
|
||||
stream.extend(slice_nal(p, cabac));
|
||||
}
|
||||
let after5 = Pic { frame_num: 1, ref_idc: 2, mbs: skip.clone(), ..Default::default() };
|
||||
stream.extend(slice_nal(&after5, cabac));
|
||||
|
||||
// expected output
|
||||
let pa = render(&a, None);
|
||||
let pb = render(&b, None);
|
||||
let p2 = pa.clone();
|
||||
let p3 = p2.clone();
|
||||
let p4 = render(&mixed, Some(&pb));
|
||||
let p5 = pb.clone();
|
||||
let p6 = p4.clone();
|
||||
let p7 = render(&pics[7].mbs, None);
|
||||
let p8 = p7.clone();
|
||||
let expected = [pa, pb, p2, p3, p4, p5, p6, p7, p8];
|
||||
|
||||
if threads == 1 {
|
||||
cross_check_ffmpeg(&stream, &expected, if cabac { "synth_cabac" } else { "synth_cavlc" });
|
||||
}
|
||||
let mut dec = Decoder::with_threads(threads);
|
||||
let mut out = dec.decode(&stream, 0).unwrap();
|
||||
out.extend(dec.flush());
|
||||
assert!(dec.take_error().is_none());
|
||||
assert_eq!(out.len(), expected.len(), "cabac={cabac}");
|
||||
for (i, (p, e)) in out.iter().zip(expected.iter()).enumerate() {
|
||||
assert_eq!((p.width as usize, p.height as usize), (MB_W * 16, MB_H * 16));
|
||||
assert!(p.y == e.0 && p.u == e.1 && p.v == e.2, "picture {i} differs (cabac={cabac}, threads={threads})");
|
||||
}
|
||||
let s = dec.stats();
|
||||
assert!(s.mb_pcm > 0 && s.frame_num_gaps == 1 && s.long_term_marks >= 3, "{s:?}");
|
||||
}
|
||||
|
||||
type Planes3 = (Vec<u8>, Vec<u8>, Vec<u8>);
|
||||
|
||||
/// Validate the hand-built stream and its expected output with ffmpeg when available.
|
||||
fn cross_check_ffmpeg(stream: &[u8], expected: &[Planes3], name: &str) {
|
||||
let Some(ff) = craft_media_testkit::ffmpeg_or_skip(name) else { return };
|
||||
let dir = craft_media_testkit::fixtures_dir("h264");
|
||||
let src = craft_media_testkit::temp_path(&dir.join(format!("{name}.h264")));
|
||||
let dst = craft_media_testkit::temp_path(&dir.join(format!("{name}.yuv")));
|
||||
std::fs::write(&src, stream).unwrap();
|
||||
let ok = std::process::Command::new(&ff)
|
||||
.args(["-hide_banner", "-loglevel", "error", "-y", "-i"])
|
||||
.arg(&src)
|
||||
.args(["-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", "yuv420p"])
|
||||
.arg(&dst)
|
||||
.status()
|
||||
.map(|s| s.success())
|
||||
.unwrap_or(false);
|
||||
assert!(ok, "ffmpeg failed to decode {name}");
|
||||
let got = std::fs::read(&dst).unwrap();
|
||||
let _ = std::fs::remove_file(&src);
|
||||
let _ = std::fs::remove_file(&dst);
|
||||
let want: Vec<u8> = expected.iter().flat_map(|(y, u, v)| y.iter().chain(u).chain(v).copied()).collect();
|
||||
assert!(got == want, "ffmpeg output differs from the expected output of {name} ({} vs {} bytes)", got.len(), want.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pcm_long_term_mmco_gaps_cavlc() {
|
||||
build_and_check(false, 1);
|
||||
build_and_check(false, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pcm_long_term_mmco_gaps_cabac() {
|
||||
build_and_check(true, 1);
|
||||
build_and_check(true, 4);
|
||||
}
|
||||
|
||||
fn weighted_check(cabac: bool, threads: usize) {
|
||||
let mut seed = 7u32;
|
||||
let a: Vec<Option<Vec<u8>>> = (0..NMB).map(|_| Some(random_mb(&mut seed))).collect();
|
||||
let b: Vec<Option<Vec<u8>>> = (0..NMB).map(|_| Some(random_mb(&mut seed))).collect();
|
||||
let skip = vec![None; NMB];
|
||||
let wp = [[(20, 3), (5, -1), (12, 2)], [(0, 0); 3]];
|
||||
let wb = [[(40, -2), (4, 1), (9, -3)], [(30, 7), (6, 0), (2, 4)]];
|
||||
// decoding order: I (POC 0), P (POC 8), B (POC 4, both refs), non-reference weighted P_Skip (POC 12)
|
||||
let pics = [
|
||||
Pic { idr: true, frame_num: 0, ref_idc: 3, poc_lsb: Some(0), mbs: a.clone(), ..Default::default() },
|
||||
Pic { frame_num: 1, ref_idc: 2, poc_lsb: Some(8), mbs: b.clone(), wt: Some(wp), ..Default::default() },
|
||||
Pic { frame_num: 2, ref_idc: 0, poc_lsb: Some(4), b: true, mbs: skip.clone(), wt: Some(wb), ..Default::default() },
|
||||
Pic { frame_num: 2, ref_idc: 0, poc_lsb: Some(12), mbs: skip.clone(), wt: Some(wp), ..Default::default() },
|
||||
];
|
||||
let mut stream = sps(77, 2, false, PocType::T0);
|
||||
stream.extend(pps(cabac, true));
|
||||
for p in &pics {
|
||||
stream.extend(slice_nal(p, cabac));
|
||||
}
|
||||
let pa = render(&a, None);
|
||||
let pb = render(&b, None);
|
||||
let clip = |v: i32| v.clamp(0, 255) as u8;
|
||||
let mix = |x: &[u8], y: &[u8], c: usize| -> Vec<u8> {
|
||||
let (w0, o0) = wb[0][c];
|
||||
let (w1, o1) = wb[1][c];
|
||||
let d = if c == 0 { 5 } else { 3 };
|
||||
x.iter().zip(y).map(|(&p, &q)| clip(((p as i32 * w0 + q as i32 * w1 + (1 << d)) >> (d + 1)) + ((o0 + o1 + 1) >> 1))).collect()
|
||||
};
|
||||
let single = |x: &[u8], c: usize| -> Vec<u8> {
|
||||
let (w, o) = wp[0][c];
|
||||
let d = if c == 0 { 5 } else { 3 };
|
||||
x.iter().map(|&p| clip(((p as i32 * w + (1 << (d - 1))) >> d) + o)).collect()
|
||||
};
|
||||
let pbi = (mix(&pa.0, &pb.0, 0), mix(&pa.1, &pb.1, 1), mix(&pa.2, &pb.2, 2));
|
||||
let pp = (single(&pb.0, 0), single(&pb.1, 1), single(&pb.2, 2));
|
||||
let expected = [pa, pbi, pb, pp];
|
||||
if threads == 1 {
|
||||
cross_check_ffmpeg(&stream, &expected, if cabac { "synth_wp_cabac" } else { "synth_wp_cavlc" });
|
||||
}
|
||||
let mut dec = Decoder::with_threads(threads);
|
||||
let mut out = dec.decode(&stream, 0).unwrap();
|
||||
out.extend(dec.flush());
|
||||
assert!(dec.take_error().is_none());
|
||||
assert_eq!(out.len(), 4);
|
||||
for (i, (p, e)) in out.iter().zip(expected.iter()).enumerate() {
|
||||
assert!(p.y == e.0 && p.u == e.1 && p.v == e.2, "picture {i} differs (cabac={cabac}, threads={threads})");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_weighted_prediction_p_and_b_skip() {
|
||||
for cabac in [false, true] {
|
||||
weighted_check(cabac, 1);
|
||||
weighted_check(cabac, 3);
|
||||
}
|
||||
}
|
||||
|
||||
/// POC type 1: non-reference pictures are displayed before the preceding reference picture.
|
||||
#[test]
|
||||
fn poc_type1_reordering() {
|
||||
for cabac in [false, true] {
|
||||
let mut seed = 5u32;
|
||||
let contents: Vec<Vec<Option<Vec<u8>>>> = (0..5).map(|_| (0..NMB).map(|_| Some(random_mb(&mut seed))).collect()).collect();
|
||||
// (frame_num, ref_idc): POCs 0, 4, 1, 8, 5
|
||||
let spec = [(0, 3), (1, 2), (2, 0), (2, 2), (3, 0)];
|
||||
let mut stream = sps(77, 2, false, PocType::T1);
|
||||
stream.extend(pps(cabac, false));
|
||||
for (i, &(frame_num, ref_idc)) in spec.iter().enumerate() {
|
||||
let p = Pic { idr: i == 0, frame_num, ref_idc, poc_delta: Some(0), mbs: contents[i].clone(), ..Default::default() };
|
||||
stream.extend(slice_nal(&p, cabac));
|
||||
}
|
||||
let expected_order = [0usize, 2, 1, 4, 3];
|
||||
let expected_poc = [0, 1, 4, 5, 8];
|
||||
for threads in [1, 2] {
|
||||
let mut dec = Decoder::with_threads(threads);
|
||||
let mut out = dec.decode(&stream, 0).unwrap();
|
||||
out.extend(dec.flush());
|
||||
assert_eq!(out.len(), 5);
|
||||
for (k, p) in out.iter().enumerate() {
|
||||
let e = render(&contents[expected_order[k]], None);
|
||||
assert_eq!(p.poc, expected_poc[k]);
|
||||
assert!(p.y == e.0 && p.u == e.1 && p.v == e.2, "output {k} (cabac={cabac})");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A stream this decoder does not implement fails as unsupported whatever its slice headers hold
|
||||
/// (#626). A 10-bit slice may code a QP below 0 (down to -12); checked against the 8-bit range
|
||||
/// first, it failed as invalid, so the media pool took it for a damaged frame and rendered black
|
||||
/// instead of the unreadable slate. 4:4:4 here, which stays unsupported after High 10 and 4:2:2.
|
||||
#[test]
|
||||
fn unsupported_stream_fails_as_unsupported_at_any_slice_qp() {
|
||||
let mut w = BitWriter::new();
|
||||
w.write_bits(244, 8); // High 4:4:4 Predictive
|
||||
w.write_bits(0, 8);
|
||||
w.write_bits(30, 8);
|
||||
w.write_ue(0); // sps id
|
||||
w.write_ue(3); // chroma_format_idc: 4:4:4
|
||||
w.write_bit(false); // separate_colour_plane
|
||||
w.write_ue(2); // bit_depth_luma - 8
|
||||
w.write_ue(2); // bit_depth_chroma - 8
|
||||
w.write_bit(false); // qpprime_y_zero_transform_bypass
|
||||
w.write_bit(false); // seq_scaling_matrix_present
|
||||
w.write_ue(0); // log2_max_frame_num - 4
|
||||
w.write_ue(2); // pic_order_cnt_type
|
||||
w.write_ue(0); // max_num_ref_frames
|
||||
w.write_bit(false); // gaps
|
||||
w.write_ue(MB_W as u32 - 1);
|
||||
w.write_ue(MB_H as u32 - 1);
|
||||
w.write_bit(true); // frame_mbs_only
|
||||
w.write_bit(true); // direct_8x8_inference
|
||||
w.write_bit(false); // cropping
|
||||
w.write_bit(false); // vui
|
||||
w.rbsp_trailing();
|
||||
let sps = nal(3, 7, &w.finish());
|
||||
// QP 26, and QP -4 (pic_init_qp 26 + slice_qp_delta -30)
|
||||
for qp_delta in [0, -30] {
|
||||
let mut w = BitWriter::new();
|
||||
w.write_ue(0); // first_mb
|
||||
w.write_ue(7); // I slice
|
||||
w.write_ue(0); // pps id
|
||||
w.write_bits(0, 4); // frame_num
|
||||
w.write_ue(0); // idr_pic_id
|
||||
w.write_bit(false); // no_output_of_prior_pics
|
||||
w.write_bit(false); // long_term_reference
|
||||
w.write_se(qp_delta);
|
||||
w.write_ue(1); // disable_deblocking_filter_idc
|
||||
w.rbsp_trailing();
|
||||
let mut stream = sps.clone();
|
||||
stream.extend(pps(false, false));
|
||||
stream.extend(nal(3, 5, &w.finish()));
|
||||
for threads in [1, 2] {
|
||||
let r = Decoder::with_threads(threads).decode(&stream, 0).map(|pics| pics.len());
|
||||
assert!(
|
||||
matches!(&r, Err(crate::h264::decode::Error::Unsupported(m)) if m.contains("chroma_format_idc 3")),
|
||||
"slice_qp_delta {qp_delta}: {r:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn avcc_accepts_generated_sps_and_pps() {
|
||||
let sps = sps(66, 4, false, PocType::T2);
|
||||
let pps = pps(false, false);
|
||||
let (sps, pps) = (&sps[4..], &pps[4..]);
|
||||
let mut avcc = vec![1, 66, 0, 30, 0xff, 0xe1];
|
||||
avcc.extend_from_slice(&(sps.len() as u16).to_be_bytes());
|
||||
avcc.extend_from_slice(sps);
|
||||
avcc.push(1);
|
||||
avcc.extend_from_slice(&(pps.len() as u16).to_be_bytes());
|
||||
avcc.extend_from_slice(pps);
|
||||
assert!(Decoder::from_avcc(&avcc).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn avcc_rejects_a_valid_picture_slice_without_starting_a_picture() {
|
||||
let sequence = sps(66, 4, false, PocType::T0);
|
||||
let picture = pps(false, false);
|
||||
let slice = slice_nal(
|
||||
&Pic {
|
||||
idr: true,
|
||||
frame_num: 0,
|
||||
ref_idc: 3,
|
||||
long_term_ref: false,
|
||||
mods: vec![],
|
||||
mmcos: vec![],
|
||||
mbs: vec![Some(vec![128; 384]); NMB],
|
||||
b: false,
|
||||
poc_lsb: Some(0),
|
||||
poc_delta: None,
|
||||
wt: None,
|
||||
first_mb: 0,
|
||||
},
|
||||
false,
|
||||
);
|
||||
let mut avcc = vec![1, 66, 0, 30, 0xff, 0xe1];
|
||||
let nal = &sequence[4..];
|
||||
avcc.extend_from_slice(&(nal.len() as u16).to_be_bytes());
|
||||
avcc.extend_from_slice(nal);
|
||||
avcc.push(2);
|
||||
avcc.extend_from_slice(&((picture.len() - 4) as u16).to_be_bytes());
|
||||
avcc.extend_from_slice(&picture[4..]);
|
||||
avcc.extend_from_slice(&((slice.len() - 4) as u16).to_be_bytes());
|
||||
avcc.extend_from_slice(&slice[4..]);
|
||||
|
||||
let mut decoder = Decoder::with_threads(1);
|
||||
assert!(decoder.configure_avcc(&avcc).is_err());
|
||||
assert!(decoder.flush().is_empty());
|
||||
assert_eq!(decoder.stats().pictures, 0);
|
||||
}
|
||||
|
||||
/// One IDR picture of PCM macroblocks.
|
||||
fn idr_stream(max_refs: u32) -> Vec<u8> {
|
||||
let mut seed = 5u32;
|
||||
let mbs: Vec<Option<Vec<u8>>> = (0..NMB).map(|_| Some(random_mb(&mut seed))).collect();
|
||||
let mut stream = sps(66, max_refs, false, PocType::T2);
|
||||
stream.extend(pps(false, false));
|
||||
stream.extend(slice_nal(&Pic { idr: true, frame_num: 0, ref_idc: 3, mbs, ..Default::default() }, false));
|
||||
stream
|
||||
}
|
||||
|
||||
fn decodes_within(stream: &[u8], budget: u64) -> bool {
|
||||
let mut dec = Decoder::with_threads(1);
|
||||
dec.set_memory_budget(budget);
|
||||
let Ok(mut out) = dec.decode(stream, 0) else { return false };
|
||||
out.extend(dec.flush());
|
||||
dec.take_error().is_none() && out.len() == 1
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn picture_store_is_checked_against_the_memory_budget() {
|
||||
let stream = idr_stream(4);
|
||||
let sps_nal = sps(66, 4, false, PocType::T2);
|
||||
let parsed = crate::h264::decode::params::Sps::parse(&craft_media::bitstream::unescape_rbsp(&sps_nal[5..])).unwrap();
|
||||
// 4:2:0, 8-bit: luma plus two quarter-size chroma planes per picture; the DPB, the current
|
||||
// picture and threads + 2 in flight.
|
||||
let one = (NMB * 256 + 2 * NMB * 64) as u64;
|
||||
let pictures = (parsed.max_dpb_frames() + 1 + 3) as u64;
|
||||
assert!(decodes_within(&stream, pictures * one));
|
||||
assert!(!decodes_within(&stream, pictures * one - 1));
|
||||
assert!(decodes_within(&stream, crate::budget::DECODE_MEMORY_BUDGET));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn largest_accepted_h264_size_is_over_the_default_budget() {
|
||||
// 1024 x 1024 macroblocks (16384 x 16384) with 16 references and 3 in flight.
|
||||
let shape = crate::budget::PictureShape { width: 16384, height: 16384, shift_x: 1, shift_y: 1, chroma_planes: 2, bytes_per_sample: 1 };
|
||||
assert!(!shape.fits(16 + 1 + 3, crate::budget::DECODE_MEMORY_BUDGET));
|
||||
}
|
||||
|
||||
fn gap_stream(gaps: bool) -> Vec<u8> {
|
||||
let mut seed = 7u32;
|
||||
let a: Vec<Option<Vec<u8>>> = (0..NMB).map(|_| Some(random_mb(&mut seed))).collect();
|
||||
let mut stream = sps(66, 2, gaps, PocType::T2);
|
||||
stream.extend(pps(false, false));
|
||||
stream.extend(slice_nal(&Pic { idr: true, frame_num: 0, ref_idc: 3, mbs: a, ..Default::default() }, false));
|
||||
// frame_num 1..=9 are missing.
|
||||
stream.extend(slice_nal(&Pic { frame_num: 10, ref_idc: 2, mbs: vec![None; NMB], ..Default::default() }, false));
|
||||
stream
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frame_num_gap_is_concealed_even_when_the_sps_forbids_gaps() {
|
||||
// A cut or lossy stream: the gap is filled (bounded) instead of dropping the picture.
|
||||
let mut dec = Decoder::with_threads(1);
|
||||
let mut out = dec.decode(&gap_stream(false), 0).unwrap();
|
||||
out.extend(dec.flush());
|
||||
assert_eq!(out.len(), 2);
|
||||
assert_eq!(dec.stats().frame_num_gaps, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frame_num_gap_is_filled_when_the_sps_allows_gaps() {
|
||||
let mut dec = Decoder::with_threads(1);
|
||||
let mut out = dec.decode(&gap_stream(true), 0).unwrap();
|
||||
out.extend(dec.flush());
|
||||
assert!(dec.take_error().is_none());
|
||||
assert_eq!(out.len(), 2);
|
||||
assert_eq!(dec.stats().frame_num_gaps, 1);
|
||||
}
|
||||
|
||||
/// An IDR picture followed by `pictures` reference P pictures of P_Skip macroblocks.
|
||||
fn skip_stream(pictures: u32) -> (Vec<u8>, Vec<Vec<u8>>) {
|
||||
let mut seed = 11u32;
|
||||
let a: Vec<Option<Vec<u8>>> = (0..NMB).map(|_| Some(random_mb(&mut seed))).collect();
|
||||
let mut head = sps(66, 2, false, PocType::T2);
|
||||
head.extend(pps(false, false));
|
||||
head.extend(slice_nal(&Pic { idr: true, frame_num: 0, ref_idc: 3, mbs: a, ..Default::default() }, false));
|
||||
let ps = (1..=pictures).map(|k| slice_nal(&Pic { frame_num: k % 16, ref_idc: 2, mbs: vec![None; NMB], ..Default::default() }, false)).collect();
|
||||
(head, ps)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlapping_slices_are_not_reconstructed_twice() {
|
||||
let (head, ps) = skip_stream(1);
|
||||
let mut once = Decoder::with_threads(1);
|
||||
let mut out = once.decode(&[head.clone(), ps[0].clone()].concat(), 0).unwrap();
|
||||
out.extend(once.flush());
|
||||
assert!(once.take_error().is_none());
|
||||
assert_eq!(out.len(), 2);
|
||||
// A second slice of the same picture starting at macroblock 1 covers those macroblocks again.
|
||||
let again = slice_nal(&Pic { frame_num: 1, ref_idc: 2, mbs: vec![None; NMB - 1], first_mb: 1, ..Default::default() }, false);
|
||||
let mut twice = Decoder::with_threads(1);
|
||||
let result = twice.decode(&[head, ps[0].clone(), again].concat(), 0);
|
||||
twice.flush();
|
||||
let err = result.err().or_else(|| twice.take_error()).expect("overlapping slice refused");
|
||||
assert!(err.to_string().contains("overlaps"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn output_queue_counts_against_the_memory_budget() {
|
||||
let (head, ps) = skip_stream(40);
|
||||
let sps_nal = sps(66, 2, false, PocType::T2);
|
||||
let parsed = crate::h264::decode::params::Sps::parse(&craft_media::bitstream::unescape_rbsp(&sps_nal[5..])).unwrap();
|
||||
let one = (NMB * 256 + 2 * NMB * 64) as u64;
|
||||
let budget = (parsed.max_dpb_frames() + 1 + 3) as u64 * one;
|
||||
// One access unit per call: output drains as it goes, and the stream fits.
|
||||
let mut dec = Decoder::with_threads(1);
|
||||
dec.set_memory_budget(budget);
|
||||
let mut out = dec.decode(&head, 0).unwrap();
|
||||
for (i, p) in ps.iter().enumerate() {
|
||||
out.extend(dec.decode(p, i as i64 + 1).unwrap());
|
||||
}
|
||||
out.extend(dec.flush());
|
||||
assert!(dec.take_error().is_none());
|
||||
assert_eq!(out.len(), 41);
|
||||
// Every access unit in one call: the pictures held for output stop fitting the budget.
|
||||
let mut dec = Decoder::with_threads(1);
|
||||
dec.set_memory_budget(budget);
|
||||
let err = dec.decode(&[head, ps.concat()].concat(), 0).expect_err("output queue is over budget");
|
||||
assert!(err.to_string().contains("memory budget"), "{err}");
|
||||
}
|
||||
|
||||
/// A held picture is charged at its own size (luma and both 4:2:0 chroma planes), the same
|
||||
/// count the budget uses for the incoming SPS's pictures.
|
||||
#[test]
|
||||
fn a_held_frame_is_charged_at_its_own_size() {
|
||||
let f = crate::h264::decode::picture::Frame::new(0, 0, 2, 3);
|
||||
assert_eq!(f.bytes(), (2 * 3 * 256 + 2 * 2 * 3 * 64) as u64);
|
||||
}
|
||||
|
||||
/// A slice_qp_delta at the end of the Exp-Golomb range is an error, not an overflow of
|
||||
/// `pic_init_qp + slice_qp_delta`.
|
||||
#[test]
|
||||
fn slice_qp_addition_does_not_overflow() {
|
||||
for qp_delta in [26, i32::MAX, -i32::MAX] {
|
||||
let mut w = BitWriter::new();
|
||||
w.write_ue(0); // first_mb
|
||||
w.write_ue(7); // I slice
|
||||
w.write_ue(0); // pps id
|
||||
w.write_bits(0, 4); // frame_num
|
||||
w.write_ue(0); // idr_pic_id
|
||||
w.write_bit(false); // no_output_of_prior_pics
|
||||
w.write_bit(false); // long_term_reference
|
||||
w.write_se(qp_delta);
|
||||
w.write_ue(1); // disable_deblocking_filter_idc
|
||||
w.rbsp_trailing();
|
||||
let mut stream = sps(66, 1, false, PocType::T2);
|
||||
stream.extend(pps(false, false));
|
||||
stream.extend(nal(3, 5, &w.finish()));
|
||||
let r = Decoder::with_threads(1).decode(&stream, 0).map(|pics| pics.len());
|
||||
assert!(matches!(&r, Err(crate::h264::decode::Error::Invalid(m)) if m.contains("slice QP")), "slice_qp_delta {qp_delta}: {r:?}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
//! Small constant tables from the H.264 specification.
|
||||
|
||||
/// 4x4 frame zig-zag scan: scan position -> raster index (row * 4 + col).
|
||||
pub const ZIGZAG4: [u8; 16] = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15];
|
||||
|
||||
/// 8x8 frame zig-zag scan: scan position -> raster index (row * 8 + col).
|
||||
pub const ZIGZAG8: [u8; 64] = zigzag8();
|
||||
|
||||
const fn zigzag8() -> [u8; 64] {
|
||||
let mut out = [0u8; 64];
|
||||
let mut k = 0;
|
||||
let mut d: i32 = 0; // anti-diagonal index = row + col
|
||||
while d < 15 {
|
||||
// even diagonals go up-right (row decreasing), odd go down-left
|
||||
let mut i: i32 = 0;
|
||||
while i < 8 {
|
||||
let (row, col) = if d % 2 == 0 { (d - i, i) } else { (i, d - i) };
|
||||
if row >= 0 && row < 8 && col >= 0 && col < 8 {
|
||||
out[k] = (row * 8 + col) as u8;
|
||||
k += 1;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
d += 1;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Table 7-3.
|
||||
pub const DEFAULT_4X4_INTRA: [u8; 16] = [6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42];
|
||||
pub const DEFAULT_4X4_INTER: [u8; 16] = [10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, 27, 30, 30, 34];
|
||||
/// Table 7-4.
|
||||
#[rustfmt::skip]
|
||||
pub const DEFAULT_8X8_INTRA: [u8; 64] = [
|
||||
6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18, 18, 18, 18, 23,
|
||||
23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27,
|
||||
27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31,
|
||||
31, 33, 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42,
|
||||
];
|
||||
#[rustfmt::skip]
|
||||
pub const DEFAULT_8X8_INTER: [u8; 64] = [
|
||||
9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19, 19, 19, 19, 21,
|
||||
21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24, 24,
|
||||
24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27,
|
||||
27, 28, 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35,
|
||||
];
|
||||
|
||||
/// Table 8-15: QPc as a function of qPI (0..=51).
|
||||
pub const QPC_TABLE: [u8; 52] = {
|
||||
let mut t = [0u8; 52];
|
||||
let tail = [29u8, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39];
|
||||
let mut i = 0;
|
||||
while i < 52 {
|
||||
t[i] = if i < 30 { i as u8 } else { tail[i - 30] };
|
||||
i += 1;
|
||||
}
|
||||
t
|
||||
};
|
||||
|
||||
/// (8-315) v matrix for normAdjust4x4.
|
||||
pub const NORM_ADJUST4: [[u16; 3]; 6] = [[10, 16, 13], [11, 18, 14], [13, 20, 16], [14, 23, 18], [16, 25, 20], [18, 29, 23]];
|
||||
/// (8-318) v matrix for normAdjust8x8.
|
||||
pub const NORM_ADJUST8: [[u16; 6]; 6] = [
|
||||
[20, 18, 32, 19, 25, 24],
|
||||
[22, 19, 35, 21, 28, 26],
|
||||
[26, 23, 42, 24, 33, 31],
|
||||
[28, 25, 45, 26, 35, 33],
|
||||
[32, 28, 51, 30, 40, 38],
|
||||
[36, 32, 58, 34, 46, 43],
|
||||
];
|
||||
|
||||
/// normAdjust4x4(m, i, j) for raster position `pos` (i = row, j = col).
|
||||
pub const fn norm_adjust4(m: usize, pos: usize) -> u16 {
|
||||
let (i, j) = (pos / 4, pos % 4);
|
||||
if i % 2 == 0 && j % 2 == 0 {
|
||||
NORM_ADJUST4[m][0]
|
||||
} else if i % 2 == 1 && j % 2 == 1 {
|
||||
NORM_ADJUST4[m][1]
|
||||
} else {
|
||||
NORM_ADJUST4[m][2]
|
||||
}
|
||||
}
|
||||
|
||||
/// normAdjust8x8(m, i, j) for raster position `pos` (i = row, j = col).
|
||||
pub const fn norm_adjust8(m: usize, pos: usize) -> u16 {
|
||||
let (i, j) = (pos / 8, pos % 8);
|
||||
let k = if i % 4 == 0 && j % 4 == 0 {
|
||||
0
|
||||
} else if i % 2 == 1 && j % 2 == 1 {
|
||||
1
|
||||
} else if i % 4 == 2 && j % 4 == 2 {
|
||||
2
|
||||
} else if (i % 4 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 4 == 0) {
|
||||
3
|
||||
} else if (i % 4 == 0 && j % 4 == 2) || (i % 4 == 2 && j % 4 == 0) {
|
||||
4
|
||||
} else {
|
||||
5
|
||||
};
|
||||
NORM_ADJUST8[m][k]
|
||||
}
|
||||
|
||||
/// Table 8-16 alpha' (indexA 0..=51).
|
||||
#[rustfmt::skip]
|
||||
pub const ALPHA: [u8; 52] = [
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 5, 6, 7, 8, 9, 10, 12, 13,
|
||||
15, 17, 20, 22, 25, 28, 32, 36, 40, 45, 50, 56, 63, 71, 80, 90, 101, 113, 127, 144, 162, 182, 203, 226, 255, 255,
|
||||
];
|
||||
/// Table 8-16 beta' (indexB 0..=51).
|
||||
#[rustfmt::skip]
|
||||
pub const BETA: [u8; 52] = [
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
|
||||
6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
|
||||
];
|
||||
/// Table 8-17 tC0' for bS = 1, 2, 3 (indexA 0..=51).
|
||||
#[rustfmt::skip]
|
||||
pub const TC0: [[u8; 3]; 52] = {
|
||||
let b1: [u8; 52] = [
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6, 6, 7, 8, 9, 10, 11, 13,
|
||||
];
|
||||
let b2: [u8; 52] = [
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 10, 11, 12, 13, 15, 17,
|
||||
];
|
||||
let b3: [u8; 52] = [
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14, 16, 18, 20, 23, 25,
|
||||
];
|
||||
let mut t = [[0u8; 3]; 52];
|
||||
let mut i = 0;
|
||||
while i < 52 {
|
||||
t[i] = [b1[i], b2[i], b3[i]];
|
||||
i += 1;
|
||||
}
|
||||
t
|
||||
};
|
||||
|
||||
/// Luma 4x4 block index (decoding order, 6.4.3) -> (x, y) in 4-sample units.
|
||||
pub const BLK4_XY: [(u8, u8); 16] =
|
||||
[(0, 0), (1, 0), (0, 1), (1, 1), (2, 0), (3, 0), (2, 1), (3, 1), (0, 2), (1, 2), (0, 3), (1, 3), (2, 2), (3, 2), (2, 3), (3, 3)];
|
||||
|
||||
/// Raster 4x4 position (y * 4 + x) -> luma4x4BlkIdx.
|
||||
pub const RASTER_TO_BLK4: [u8; 16] = [0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn zigzag8_known_prefix() {
|
||||
assert_eq!(&ZIGZAG8[..16], &[0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5]);
|
||||
assert_eq!(&ZIGZAG8[48..], &[58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63]);
|
||||
let mut seen = [false; 64];
|
||||
for &z in &ZIGZAG8 {
|
||||
seen[z as usize] = true;
|
||||
}
|
||||
assert!(seen.iter().all(|&s| s));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blk4_maps_consistent() {
|
||||
for (b, &(x, y)) in BLK4_XY.iter().enumerate() {
|
||||
let r = (y * 4 + x) as usize;
|
||||
assert_eq!(RASTER_TO_BLK4[r] as usize, b);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qpc() {
|
||||
assert_eq!(QPC_TABLE[29], 29);
|
||||
assert_eq!(QPC_TABLE[30], 29);
|
||||
assert_eq!(QPC_TABLE[51], 39);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,411 @@
|
||||
//! Scaling (dequantisation) and inverse transforms (8.5.10 - 8.5.13).
|
||||
|
||||
use crate::h264::decode::params::ScalingMatrices;
|
||||
use crate::h264::decode::tables::{norm_adjust4, norm_adjust8};
|
||||
|
||||
/// LevelScale4x4 / LevelScale8x8 for all six scaling lists and qP % 6, raster order.
|
||||
#[derive(Clone)]
|
||||
pub struct LevelScale {
|
||||
/// [list 0..6 (IntraY, IntraCb, IntraCr, InterY, InterCb, InterCr)][qp % 6][pos]
|
||||
pub ls4: [[[i32; 16]; 6]; 6],
|
||||
/// [list 0..6 (IntraY, InterY, IntraCb, InterCb, IntraCr, InterCr)][qp % 6][pos]
|
||||
pub ls8: [[[i32; 64]; 6]; 6],
|
||||
}
|
||||
|
||||
impl LevelScale {
|
||||
pub fn new(m: &ScalingMatrices) -> Box<Self> {
|
||||
let mut s = Box::new(LevelScale { ls4: [[[0; 16]; 6]; 6], ls8: [[[0; 64]; 6]; 6] });
|
||||
for l in 0..6 {
|
||||
for q in 0..6 {
|
||||
for p in 0..16 {
|
||||
s.ls4[l][q][p] = m.m4[l][p] as i32 * norm_adjust4(q, p) as i32;
|
||||
}
|
||||
for p in 0..64 {
|
||||
s.ls8[l][q][p] = m.m8[l][p] as i32 * norm_adjust8(q, p) as i32;
|
||||
}
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
/// Coefficient scaling of one block (8.5.12.1 for 4x4 AC, 8.5.13.1 for 8x8) with the qP-dependent
|
||||
/// shift and rounding resolved once per block.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Dequant {
|
||||
shl: u32,
|
||||
shr: u32,
|
||||
round: i32,
|
||||
}
|
||||
|
||||
impl Dequant {
|
||||
/// 4x4 blocks (not the DC of Intra16x16 / chroma blocks).
|
||||
pub fn new4(qp: i32) -> Self {
|
||||
Self::new(qp / 6 - 4)
|
||||
}
|
||||
|
||||
/// 8x8 blocks.
|
||||
pub fn new8(qp: i32) -> Self {
|
||||
Self::new(qp / 6 - 6)
|
||||
}
|
||||
|
||||
fn new(shift: i32) -> Self {
|
||||
if shift >= 0 { Dequant { shl: shift as u32, shr: 0, round: 0 } } else { Dequant { shl: 0, shr: -shift as u32, round: 1 << (-shift - 1) } }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn apply(self, c: i32, ls: i32) -> i32 {
|
||||
(c.wrapping_mul(ls) << self.shl).wrapping_add(self.round) >> self.shr
|
||||
}
|
||||
}
|
||||
|
||||
/// Intra16x16 luma DC: inverse Hadamard + scaling (8.5.10). `c` is raster order (row*4+col);
|
||||
/// output replaces `c` with dcY (raster: row i -> 4x4 block row, col j -> block column).
|
||||
pub fn luma_dc_dequant(c: &mut [i32; 16], qp: i32, ls00: i32) {
|
||||
let mut t = [0i32; 16];
|
||||
// rows
|
||||
for i in 0..4 {
|
||||
let r = &c[i * 4..i * 4 + 4];
|
||||
let s01 = r[0] + r[1];
|
||||
let d01 = r[0] - r[1];
|
||||
let s23 = r[2] + r[3];
|
||||
let d23 = r[2] - r[3];
|
||||
t[i * 4] = s01 + s23;
|
||||
t[i * 4 + 1] = s01 - s23;
|
||||
t[i * 4 + 2] = d01 - d23;
|
||||
t[i * 4 + 3] = d01 + d23;
|
||||
}
|
||||
// columns
|
||||
for j in 0..4 {
|
||||
let s01 = t[j] + t[4 + j];
|
||||
let d01 = t[j] - t[4 + j];
|
||||
let s23 = t[8 + j] + t[12 + j];
|
||||
let d23 = t[8 + j] - t[12 + j];
|
||||
let f = [s01 + s23, s01 - s23, d01 - d23, d01 + d23];
|
||||
for i in 0..4 {
|
||||
c[i * 4 + j] = if qp >= 36 {
|
||||
f[i].wrapping_mul(ls00) << (qp / 6 - 6)
|
||||
} else {
|
||||
f[i].wrapping_mul(ls00).wrapping_add(1 << (5 - qp / 6)) >> (6 - qp / 6)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 4:2:0 chroma DC: 2x2 transform + scaling (8.5.11). `c` = [c00, c01, c10, c11].
|
||||
pub fn chroma_dc_dequant_420(c: &mut [i32; 4], qp: i32, ls00: i32) {
|
||||
let s0 = c[0].wrapping_add(c[1]);
|
||||
let d0 = c[0].wrapping_sub(c[1]);
|
||||
let s1 = c[2].wrapping_add(c[3]);
|
||||
let d1 = c[2].wrapping_sub(c[3]);
|
||||
let f = [s0.wrapping_add(s1), d0.wrapping_add(d1), s0.wrapping_sub(s1), d0.wrapping_sub(d1)];
|
||||
for k in 0..4 {
|
||||
c[k] = (f[k].wrapping_mul(ls00) << (qp / 6)) >> 5;
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn clip_u8(v: i32) -> u8 {
|
||||
v.clamp(0, 255) as u8
|
||||
}
|
||||
|
||||
/// A dequantised coefficient the inverse transforms are defined for: the dequantisation wraps
|
||||
/// by design (see `huge_levels_dequantise_without_overflow_panics`), so their butterflies
|
||||
/// clamp an r outside −2^15..2^15−1 to this range — where the spec leaves the output
|
||||
/// unspecified anyway (8.5.12.1, 8.5.13.1) — instead of overflowing the plain i32 additions.
|
||||
#[inline(always)]
|
||||
fn r_clamp(v: i32) -> i32 {
|
||||
v.clamp(-(1 << 15), (1 << 15) - 1)
|
||||
}
|
||||
|
||||
/// Inverse 4x4 transform of raster-order `d` and add to `dst` (8.5.12.2, 8.5.14).
|
||||
///
|
||||
/// Rows first, then columns (the spec's order: the `>> 1` steps make it matter). The column
|
||||
/// pass and the reconstruction run across all columns at once so they vectorise.
|
||||
pub fn idct4_add(d: &[i32; 16], dst: &mut [u8], stride: usize) {
|
||||
let d: [i32; 16] = std::array::from_fn(|i| r_clamp(d[i]));
|
||||
let mut t = [[0i32; 4]; 4];
|
||||
for (i, row) in t.iter_mut().enumerate() {
|
||||
let r = &d[i * 4..i * 4 + 4];
|
||||
let e = r[0] + r[2];
|
||||
let f = r[0] - r[2];
|
||||
let g = (r[1] >> 1) - r[3];
|
||||
let h = r[1] + (r[3] >> 1);
|
||||
*row = [e + h, f + g, f - g, e - h];
|
||||
}
|
||||
let mut o = [[0i32; 4]; 4];
|
||||
for j in 0..4 {
|
||||
let e = t[0][j] + t[2][j];
|
||||
let f = t[0][j] - t[2][j];
|
||||
let g = (t[1][j] >> 1) - t[3][j];
|
||||
let h = t[1][j] + (t[3][j] >> 1);
|
||||
o[0][j] = e + h;
|
||||
o[1][j] = f + g;
|
||||
o[2][j] = f - g;
|
||||
o[3][j] = e - h;
|
||||
}
|
||||
for (i, row) in o.iter().enumerate() {
|
||||
let Some(line) = dst.get_mut(i * stride..).and_then(|d| d.first_chunk_mut::<4>()) else {
|
||||
return;
|
||||
};
|
||||
for (p, &v) in line.iter_mut().zip(row) {
|
||||
*p = clip_u8(*p as i32 + ((v + 32) >> 6));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `idct4_add` / `idct8_add` when all AC coefficients are zero: every output sample is
|
||||
/// `(dc + 32) >> 6`, so the transform reduces to adding that to the `N`x`N` block.
|
||||
pub fn idct_dc_add<const N: usize>(dc: i32, dst: &mut [u8], stride: usize) {
|
||||
let v = (dc + 32) >> 6;
|
||||
for i in 0..N {
|
||||
let Some(line) = dst.get_mut(i * stride..).and_then(|d| d.first_chunk_mut::<N>()) else {
|
||||
return;
|
||||
};
|
||||
for p in line {
|
||||
*p = clip_u8(*p as i32 + v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn idct8_1d(d: [i32; 8]) -> [i32; 8] {
|
||||
let a0 = d[0] + d[4];
|
||||
let a4 = d[0] - d[4];
|
||||
let a2 = (d[2] >> 1) - d[6];
|
||||
let a6 = d[2] + (d[6] >> 1);
|
||||
let b0 = a0 + a6;
|
||||
let b2 = a4 + a2;
|
||||
let b4 = a4 - a2;
|
||||
let b6 = a0 - a6;
|
||||
let a1 = -d[3] + d[5] - d[7] - (d[7] >> 1);
|
||||
let a3 = d[1] + d[7] - d[3] - (d[3] >> 1);
|
||||
let a5 = -d[1] + d[7] + d[5] + (d[5] >> 1);
|
||||
let a7 = d[3] + d[5] + d[1] + (d[1] >> 1);
|
||||
let b1 = a1 + (a7 >> 2);
|
||||
let b7 = a7 - (a1 >> 2);
|
||||
let b3 = a3 + (a5 >> 2);
|
||||
let b5 = (a3 >> 2) - a5;
|
||||
[b0 + b7, b2 + b5, b4 + b3, b6 + b1, b6 - b1, b4 - b3, b2 - b5, b0 - b7]
|
||||
}
|
||||
|
||||
/// Inverse 8x8 transform of raster-order `d` and add to `dst` (8.5.13.2): rows, then columns,
|
||||
/// the column pass and reconstruction across all columns at once (they vectorise).
|
||||
pub fn idct8_add(d: &[i32; 64], dst: &mut [u8], stride: usize) {
|
||||
let d: [i32; 64] = std::array::from_fn(|i| r_clamp(d[i]));
|
||||
let mut t = [[0i32; 8]; 8];
|
||||
for (row, src) in t.iter_mut().zip(d.as_chunks::<8>().0) {
|
||||
*row = idct8_1d(*src);
|
||||
}
|
||||
let mut o = [[0i32; 8]; 8];
|
||||
for j in 0..8 {
|
||||
let col = idct8_1d(std::array::from_fn(|i| t[i][j]));
|
||||
for i in 0..8 {
|
||||
o[i][j] = col[i];
|
||||
}
|
||||
}
|
||||
for (i, row) in o.iter().enumerate() {
|
||||
let Some(line) = dst.get_mut(i * stride..).and_then(|d| d.first_chunk_mut::<8>()) else {
|
||||
return;
|
||||
};
|
||||
for (p, &v) in line.iter_mut().zip(row) {
|
||||
*p = clip_u8(*p as i32 + ((v + 32) >> 6));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The straightforward per-column formulation (8.5.12.2 / 8.5.13.2) the vectorised
|
||||
/// transforms must equal.
|
||||
fn reference_add(d: &[i32], n: usize, dst: &mut [u8], stride: usize) {
|
||||
let one = |v: &[i32]| -> Vec<i32> {
|
||||
if n == 4 {
|
||||
let (e, f, g, h) = (v[0] + v[2], v[0] - v[2], (v[1] >> 1) - v[3], v[1] + (v[3] >> 1));
|
||||
vec![e + h, f + g, f - g, e - h]
|
||||
} else {
|
||||
idct8_1d(v.try_into().unwrap()).to_vec()
|
||||
}
|
||||
};
|
||||
let rows: Vec<Vec<i32>> = (0..n).map(|i| one(&d[i * n..i * n + n])).collect();
|
||||
for j in 0..n {
|
||||
let col = one(&(0..n).map(|i| rows[i][j]).collect::<Vec<_>>());
|
||||
for i in 0..n {
|
||||
let p = &mut dst[i * stride + j];
|
||||
*p = (*p as i32 + ((col[i] + 32) >> 6)).clamp(0, 255) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vectorised_transforms_match_reference() {
|
||||
let mut rng = 0x1234_5678_9abc_def1u64;
|
||||
let mut next = move || {
|
||||
rng ^= rng << 13;
|
||||
rng ^= rng >> 7;
|
||||
rng ^= rng << 17;
|
||||
rng
|
||||
};
|
||||
for iter in 0..5000 {
|
||||
let range = [64u64, 1024, 8192, 1 << 15][iter % 4];
|
||||
let d4: [i32; 16] = std::array::from_fn(|_| (next() % (2 * range + 1)) as i32 - range as i32);
|
||||
let d8: [i32; 64] = std::array::from_fn(|_| (next() % (2 * range + 1)) as i32 - range as i32);
|
||||
let base: Vec<u8> = (0..24 * 8).map(|_| next() as u8).collect();
|
||||
let (mut a, mut b) = (base.clone(), base.clone());
|
||||
idct4_add(&d4, &mut a[3..], 24);
|
||||
reference_add(&d4, 4, &mut b[3..], 24);
|
||||
assert_eq!(a, b, "4x4 iteration {iter}");
|
||||
let (mut a, mut b) = (base.clone(), base);
|
||||
idct8_add(&d8, &mut a[5..], 24);
|
||||
reference_add(&d8, 8, &mut b[5..], 24);
|
||||
assert_eq!(a, b, "8x8 iteration {iter}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idct4_dc_only_matches_shortcut() {
|
||||
let mut d = [0i32; 16];
|
||||
d[0] = 640; // -> +10 everywhere
|
||||
let mut a = [100u8; 16];
|
||||
let mut b = [100u8; 16];
|
||||
idct4_add(&d, &mut a, 4);
|
||||
idct_dc_add::<4>(640, &mut b, 4);
|
||||
assert_eq!(a, b);
|
||||
assert!(a.iter().all(|&v| v == 110));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dc_shortcut_matches_full_transforms() {
|
||||
let mut rng = 0x0bad_cafe_dead_beefu64;
|
||||
let mut next = move || {
|
||||
rng ^= rng << 13;
|
||||
rng ^= rng >> 7;
|
||||
rng ^= rng << 17;
|
||||
rng
|
||||
};
|
||||
for iter in 0..5000 {
|
||||
let range = [64u64, 1024, 8192, 1 << 15][iter % 4];
|
||||
let dc = (next() % (2 * range + 1)) as i32 - range as i32;
|
||||
let base: Vec<u8> = (0..24 * 8).map(|_| next() as u8).collect();
|
||||
let mut d4 = [0i32; 16];
|
||||
d4[0] = dc;
|
||||
let (mut a, mut b) = (base.clone(), base.clone());
|
||||
idct4_add(&d4, &mut a[3..], 24);
|
||||
idct_dc_add::<4>(dc, &mut b[3..], 24);
|
||||
assert_eq!(a, b, "4x4 iteration {iter}");
|
||||
let mut d8 = [0i32; 64];
|
||||
d8[0] = dc;
|
||||
let (mut a, mut b) = (base.clone(), base);
|
||||
idct8_add(&d8, &mut a[5..], 24);
|
||||
idct_dc_add::<8>(dc, &mut b[5..], 24);
|
||||
assert_eq!(a, b, "8x8 iteration {iter}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idct4_hand_example() {
|
||||
// d = [[64, 64, 0, 0], 0...]: row0: e=64,f=64,g=32,h=64 -> [128, 96, 32, 0]; columns: each col only row0 set
|
||||
// col j: e=f=t0j, g=0, h=0 -> all rows = t0j. Result (t+32)>>6 = [2, 2, 1, 0] for every row.
|
||||
let mut d = [0i32; 16];
|
||||
d[0] = 64;
|
||||
d[1] = 64;
|
||||
let mut out = [0u8; 16];
|
||||
idct4_add(&d, &mut out, 4);
|
||||
for i in 0..4 {
|
||||
assert_eq!(&out[i * 4..i * 4 + 4], &[2, 2, 1, 0]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idct8_dc() {
|
||||
let mut d = [0i32; 64];
|
||||
d[0] = 64 * 5;
|
||||
let mut out = [10u8; 64];
|
||||
idct8_add(&d, &mut out, 8);
|
||||
assert!(out.iter().all(|&v| v == 15));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn luma_dc_flat() {
|
||||
// single DC coefficient spreads equally to all 16 blocks
|
||||
let mut c = [0i32; 16];
|
||||
c[0] = 4;
|
||||
luma_dc_dequant(&mut c, 36, 16 * 16);
|
||||
assert!(c.iter().all(|&v| v == 4 * 256));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chroma_dc() {
|
||||
let mut c = [1, 0, 0, 0];
|
||||
chroma_dc_dequant_420(&mut c, 6, 16 * 16);
|
||||
// f = [1,1,1,1] -> ((1*256) << 1) >> 5 = 16
|
||||
assert_eq!(c, [16, 16, 16, 16]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scale_functions() {
|
||||
assert_eq!(Dequant::new4(24).apply(2, 160), 2 * 160);
|
||||
assert_eq!(Dequant::new4(12).apply(2, 160), (2 * 160 + 2) >> 2);
|
||||
assert_eq!(Dequant::new8(36).apply(1, 320), 320);
|
||||
assert_eq!(Dequant::new8(30).apply(1, 320), (320 + 1) >> 1);
|
||||
}
|
||||
|
||||
/// The 8.5.12.1 / 8.5.13.1 formulas, per coefficient.
|
||||
#[test]
|
||||
fn dequant_matches_spec_formulas() {
|
||||
for qp in 0..52 {
|
||||
let (d4, d8) = (Dequant::new4(qp), Dequant::new8(qp));
|
||||
for c in [-2047, -300, -17, -1, 1, 2, 5, 64, 1000, 2047] {
|
||||
for ls in [10, 16, 25, 160, 288, 2880, 18 * 255] {
|
||||
let want4 = if qp >= 24 { (c * ls) << (qp / 6 - 4) } else { (c * ls + (1 << (3 - qp / 6))) >> (4 - qp / 6) };
|
||||
let want8 = if qp >= 36 { (c * ls) << (qp / 6 - 6) } else { (c * ls + (1 << (5 - qp / 6))) >> (6 - qp / 6) };
|
||||
assert_eq!(d4.apply(c, ls), want4, "4x4 qp {qp} c {c} ls {ls}");
|
||||
assert_eq!(d8.apply(c, ls), want8, "8x8 qp {qp} c {c} ls {ls}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Corrupt streams can carry huge coefficient levels: dequantisation wraps (as release
|
||||
/// builds always did) instead of panicking with "attempt to multiply with overflow" in
|
||||
/// debug builds (found by mutation fuzzing an H.264 transport stream).
|
||||
/// The same wrapping dequantisation hands the inverse transforms any i32 coefficient, and
|
||||
/// their butterflies add in plain i32: `i32::MAX + i32::MAX` panicked a debug build. The
|
||||
/// spec leaves the transforms' output unspecified when a dequantised value falls outside
|
||||
/// −2^15..2^15−1 (8.5.12.1, 8.5.13.1), so those inputs are clamped to that range —
|
||||
/// bit-exact for every stream inside it — instead of panicking.
|
||||
#[test]
|
||||
fn huge_coefficients_are_clamped_to_the_spec_r_range() {
|
||||
assert_eq!(r_clamp(-32769), -32768);
|
||||
assert_eq!(r_clamp(-32768), -32768);
|
||||
assert_eq!(r_clamp(32767), 32767);
|
||||
assert_eq!(r_clamp(32768), 32767);
|
||||
let clamp = |v: i32| v.clamp(-(1 << 15), (1 << 15) - 1);
|
||||
for d in [[i32::MAX; 16], [i32::MIN; 16], std::array::from_fn(|i| if i % 2 == 0 { i32::MIN } else { i32::MAX })] {
|
||||
let clamped = std::array::from_fn(|i| clamp(d[i]));
|
||||
let (mut a, mut b) = ([0u8; 64], [0u8; 64]);
|
||||
idct4_add(&d, &mut a, 8);
|
||||
idct4_add(&clamped, &mut b, 8);
|
||||
assert_eq!(a, b, "idct4 of extremes equals idct4 of the spec range");
|
||||
}
|
||||
for d in [[i32::MIN; 64], [i32::MAX; 64]] {
|
||||
let clamped = std::array::from_fn(|i| clamp(d[i]));
|
||||
let (mut a, mut b) = ([0u8; 64], [0u8; 64]);
|
||||
idct8_add(&d, &mut a, 8);
|
||||
idct8_add(&clamped, &mut b, 8);
|
||||
assert_eq!(a, b, "idct8 of extremes equals idct8 of the spec range");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn huge_levels_dequantise_without_overflow_panics() {
|
||||
let mut c = [i32::MAX / 2, i32::MAX / 3, -i32::MAX / 2, 7];
|
||||
chroma_dc_dequant_420(&mut c, 51, 16 * 25);
|
||||
let _ = Dequant::new4(51).apply(i32::MAX, 16 * 25);
|
||||
let _ = Dequant::new4(0).apply(i32::MAX, 16 * 25);
|
||||
let _ = Dequant::new8(51).apply(i32::MIN, 16 * 25);
|
||||
let _ = Dequant::new8(0).apply(i32::MIN, 16 * 25);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
//! CABAC arithmetic encoder (ITU-T H.264 §9.3.4.2) and a matching bit-cost estimator.
|
||||
|
||||
use crate::h264::tables::{NUM_CTX, RANGE_LPS, TRANS_LPS};
|
||||
use crate::h264::encode::tables::{NUM_CTX, RANGE_LPS, TRANS_LPS};
|
||||
|
||||
/// A sink for CABAC bins. Implemented by the real arithmetic coder and by the rate estimator, so the same
|
||||
/// syntax-element code drives both bitstream writing and RD cost estimation.
|
||||
@@ -273,7 +273,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn roundtrip(n: usize, seed: u32) {
|
||||
let table = crate::h264::tables::cabac_init_pb0();
|
||||
let table = crate::h264::encode::tables::cabac_init_pb0();
|
||||
let ctx = Contexts::init(&table, 26);
|
||||
let mut enc = CabacEncoder::new(ctx.clone(), vec![0xa5]);
|
||||
let mut seed = seed;
|
||||
@@ -327,7 +327,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn long_bypass_runs_and_carries() {
|
||||
let ctx = Contexts::init(&crate::h264::tables::cabac_init_pb0(), 26);
|
||||
let ctx = Contexts::init(&crate::h264::encode::tables::cabac_init_pb0(), 26);
|
||||
for bin in 0..2 {
|
||||
let mut enc = CabacEncoder::new(ctx.clone(), Vec::new());
|
||||
for _ in 0..5000 {
|
||||
@@ -1,10 +1,10 @@
|
||||
//! CABAC binarisation and context selection for macroblock-layer syntax elements (§9.3.2, §9.3.3.1).
|
||||
|
||||
use crate::h264::cabac::BinSink;
|
||||
use crate::h264::mbinfo::{MbInfo, MbKind, Part};
|
||||
use crate::h264::nal::SliceType;
|
||||
use crate::h264::syntax::{MbCode, NbCtx};
|
||||
use crate::h264::tables::{BLK4_XY, LAST8X8, SIG8X8_FRAME};
|
||||
use crate::h264::encode::cabac::BinSink;
|
||||
use crate::h264::encode::mbinfo::{MbInfo, MbKind, Part};
|
||||
use crate::h264::encode::nal::SliceType;
|
||||
use crate::h264::encode::syntax::{MbCode, NbCtx};
|
||||
use crate::h264::encode::tables::{BLK4_XY, LAST8X8, SIG8X8_FRAME};
|
||||
|
||||
pub fn write_skip_flag<S: BinSink>(s: &mut S, nb: &NbCtx, skip: bool) {
|
||||
let cond = |m: Option<&MbInfo>| m.is_some_and(|m| !m.kind.is_skip()) as usize;
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
use craft_media::bitstream::BitWriter;
|
||||
|
||||
use crate::h264::mbinfo::{MbKind, Part};
|
||||
use crate::h264::nal::SliceType;
|
||||
use crate::h264::syntax::{MbCode, NbCtx};
|
||||
use crate::h264::tables::{
|
||||
use crate::h264::encode::mbinfo::{MbKind, Part};
|
||||
use crate::h264::encode::nal::SliceType;
|
||||
use crate::h264::encode::syntax::{MbCode, NbCtx};
|
||||
use crate::h264::encode::tables::{
|
||||
BLK4_XY, CBP_INTER_TO_CODE, CBP_INTRA_TO_CODE, COEFF_TOKEN_CDC_CODE, COEFF_TOKEN_CDC_LEN, COEFF_TOKEN_CODE, COEFF_TOKEN_LEN, RUN_BEFORE,
|
||||
TOTAL_ZEROS, TOTAL_ZEROS_CDC,
|
||||
};
|
||||
@@ -4,8 +4,8 @@
|
||||
//! never filtered (slices are MB-row aligned and either there is one slice, or
|
||||
//! `disable_deblocking_filter_idc = 2` is used).
|
||||
|
||||
use crate::h264::mbinfo::{MbInfo, Mv};
|
||||
use crate::h264::tables::{ALPHA, BETA, TC0, chroma_qp};
|
||||
use crate::h264::encode::mbinfo::{MbInfo, Mv};
|
||||
use crate::h264::encode::tables::{ALPHA, BETA, TC0, chroma_qp};
|
||||
|
||||
pub struct DeblockParams {
|
||||
pub alpha_offset: i32,
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Cheap half-resolution analysis used for rate control, scene-cut detection and adaptive quantisation.
|
||||
|
||||
use crate::h264::dsp::{block_var, sad, satd};
|
||||
use crate::h264::picture::Frame;
|
||||
use crate::h264::encode::dsp::{block_var, sad, satd};
|
||||
use crate::h264::encode::picture::Frame;
|
||||
|
||||
/// Half-resolution luma (2x2 box filtered) of the macroblock-aligned picture.
|
||||
pub struct LowRes {
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
//! Padded picture planes, reference pictures and motion-compensated prediction (§8.4.2.2).
|
||||
|
||||
use crate::h264::mbinfo::{MbInfo, Mv};
|
||||
use crate::h264::encode::mbinfo::{MbInfo, Mv};
|
||||
|
||||
pub const LUMA_PAD: usize = 64;
|
||||
pub const CHROMA_PAD: usize = 32;
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Frame-level rate control: constant QP, constant quality (CRF-like), 1-pass ABR with VBV, and 2-pass VBR.
|
||||
|
||||
use crate::h264::nal::SliceType;
|
||||
use crate::h264::encode::nal::SliceType;
|
||||
|
||||
pub fn qp2qscale(qp: f64) -> f64 {
|
||||
0.85 * 2f64.powf((qp - 12.0) / 6.0)
|
||||
@@ -2,19 +2,19 @@
|
||||
|
||||
use craft_media::bitstream::BitWriter;
|
||||
|
||||
use crate::h264::cabac::{BinSink, CabacEncoder, CabacEstimator, Contexts, CostTable};
|
||||
use crate::h264::dsp::{sad, sad8, sad16, satd, ssd};
|
||||
use crate::h264::intra::{Edge4, Edge8, filter8, mode_available, pred_chroma, pred4x4, pred8x8, pred16x16};
|
||||
use crate::h264::mbinfo::{MbInfo, MbKind, Mv, Part};
|
||||
use crate::h264::nal::{SliceHeader, SliceType, nal};
|
||||
use crate::h264::picture::{CHROMA_PAD, Frame, LUMA_PAD, RefPic, avg_into};
|
||||
use crate::h264::syntax::{MbCode, NbCtx};
|
||||
use crate::h264::tables::{BLK4_XY, NUM_CTX, RASTER_TO_BLK4, ZIGZAG4, ZIGZAG8, chroma_qp};
|
||||
use crate::h264::transform::{
|
||||
use crate::h264::encode::cabac::{BinSink, CabacEncoder, CabacEstimator, Contexts, CostTable};
|
||||
use crate::h264::encode::dsp::{sad, sad8, sad16, satd, ssd};
|
||||
use crate::h264::encode::intra::{Edge4, Edge8, filter8, mode_available, pred_chroma, pred4x4, pred8x8, pred16x16};
|
||||
use crate::h264::encode::mbinfo::{MbInfo, MbKind, Mv, Part};
|
||||
use crate::h264::encode::nal::{SliceHeader, SliceType, nal};
|
||||
use crate::h264::encode::picture::{CHROMA_PAD, Frame, LUMA_PAD, RefPic, avg_into};
|
||||
use crate::h264::encode::syntax::{MbCode, NbCtx};
|
||||
use crate::h264::encode::tables::{BLK4_XY, NUM_CTX, RASTER_TO_BLK4, ZIGZAG4, ZIGZAG8, chroma_qp};
|
||||
use crate::h264::encode::transform::{
|
||||
Deadzone, QuantTables, dequant_chroma_dc, dequant_luma_dc, dequant4, dequant8, fdct4, fdct8, idct4, idct8, quant_chroma_dc, quant_luma_dc,
|
||||
quant4, quant8,
|
||||
};
|
||||
use crate::h264::{cabac_mb, cavlc};
|
||||
use crate::h264::encode::{cabac_mb, cavlc};
|
||||
|
||||
mod b_slices;
|
||||
mod intra;
|
||||
+1
-1
@@ -2,7 +2,7 @@ use super::{
|
||||
Avail, Cand, MbCtx, MbEdges, MbInfo, MbKind, Mv, MvNb, Part, PartGeom, PartSearch, PredOut, RefPic, SearchBlock, Shape, SliceEnc, Src, mv_bits,
|
||||
sad, sad8, sad16, satd,
|
||||
};
|
||||
use crate::h264::picture::McBlock;
|
||||
use crate::h264::encode::picture::McBlock;
|
||||
|
||||
fn search_starts(seed: Mv, neighbour: Mv, candidates: &[Mv]) -> ([Mv; 5], usize) {
|
||||
let mut starts = [Mv::ZERO; 5];
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Macroblock-layer syntax values produced by mode decision and consumed by the entropy coders.
|
||||
|
||||
use crate::h264::mbinfo::{MbInfo, MbKind, Mv, Part};
|
||||
use crate::h264::nal::SliceType;
|
||||
use crate::h264::encode::mbinfo::{MbInfo, MbKind, Mv, Part};
|
||||
use crate::h264::encode::nal::SliceType;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MbCode {
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Integer transforms, quantisation and the normative inverse transforms/scaling (§8.5.12).
|
||||
|
||||
use crate::h264::tables::{DEQUANT4, DEQUANT8, QUANT4, pos_class4, pos_class8};
|
||||
use crate::h264::encode::tables::{DEQUANT4, DEQUANT8, QUANT4, pos_class4, pos_class8};
|
||||
|
||||
/// Forward 4x4 core transform of a residual block (raster order).
|
||||
#[inline]
|
||||
+12
-1216
File diff suppressed because it is too large
Load Diff
@@ -8,7 +8,7 @@
|
||||
//! | `dnx` | [`dnx`] | Avid DNxHD / DNxHR (SMPTE ST 2019-1): decoder and encoder |
|
||||
//! | `vp8` | [`vp8`] | VP8 (RFC 6386): decoder |
|
||||
//! | `prores` | [`prores`] | Apple ProRes (SMPTE RDD 36): decoder and encoder |
|
||||
//! | `h264` | [`h264`] | ITU-T H.264 / AVC: encoder |
|
||||
//! | `h264` | [`h264`] | ITU-T H.264 / AVC: decoder (`h264-decode`) and encoder (`h264-encode`) |
|
||||
//! | `hevc` | [`hevc`] | ITU-T H.265 / HEVC: decoder (`hevc-decode`) and encoder (`hevc-encode`) |
|
||||
//! | `vp9` | [`vp9`] | VP9: decoder (`vp9-decode`) and encoder (`vp9-encode`) |
|
||||
//! | `av1` | [`av1`] | AV1 (AOMedia Video 1): encoder |
|
||||
@@ -32,7 +32,7 @@ pub mod vp8;
|
||||
#[cfg(feature = "prores")]
|
||||
pub mod prores;
|
||||
|
||||
#[cfg(feature = "h264")]
|
||||
#[cfg(any(feature = "h264-decode", feature = "h264-encode"))]
|
||||
pub mod h264;
|
||||
|
||||
#[cfg(any(feature = "hevc-decode", feature = "hevc-encode"))]
|
||||
|
||||
@@ -0,0 +1,558 @@
|
||||
//! Test fixture generation (ffmpeg/libx264 as an external oracle) and comparison helpers.
|
||||
#![allow(dead_code)]
|
||||
|
||||
use craft_video::h264::decode::{Decoder, Picture};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
use std::sync::{Mutex, PoisonError};
|
||||
|
||||
// Tests share fixture names within each binary. Serialize the existence check,
|
||||
// generation and publication so a late producer cannot replace a file in use.
|
||||
static FIXTURE_GENERATION: Mutex<()> = Mutex::new(());
|
||||
|
||||
/// One encoder configuration of the fixture matrix.
|
||||
pub struct Fixture {
|
||||
pub name: &'static str,
|
||||
/// lavfi source (without size / rate).
|
||||
pub source: &'static str,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub frames: u32,
|
||||
/// Extra video filter appended after the source (e.g. noise, fade).
|
||||
pub filter: &'static str,
|
||||
/// Encoder arguments.
|
||||
pub args: &'static [&'static str],
|
||||
}
|
||||
|
||||
const NOISE: &str = "noise=alls=12:allf=t+u";
|
||||
|
||||
pub const FIXTURES: &[Fixture] = &[
|
||||
// Apple VideoToolbox (hardware) encoder streams: different encoder design, generated on macOS only
|
||||
Fixture {
|
||||
name: "vt_high",
|
||||
source: "testsrc2",
|
||||
width: 640,
|
||||
height: 360,
|
||||
frames: 30,
|
||||
filter: NOISE,
|
||||
args: &["-c:v", "h264_videotoolbox", "-profile:v", "high", "-b:v", "3M"],
|
||||
},
|
||||
Fixture {
|
||||
name: "vt_main",
|
||||
source: "mandelbrot",
|
||||
width: 640,
|
||||
height: 360,
|
||||
frames: 30,
|
||||
filter: NOISE,
|
||||
args: &["-c:v", "h264_videotoolbox", "-profile:v", "main", "-b:v", "2M"],
|
||||
},
|
||||
Fixture {
|
||||
name: "vt_baseline",
|
||||
source: "testsrc2",
|
||||
width: 320,
|
||||
height: 240,
|
||||
frames: 30,
|
||||
filter: NOISE,
|
||||
args: &["-c:v", "h264_videotoolbox", "-profile:v", "baseline", "-b:v", "1M"],
|
||||
},
|
||||
Fixture {
|
||||
name: "vt_high_1080p",
|
||||
source: "testsrc2",
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
frames: 10,
|
||||
filter: "",
|
||||
args: &["-c:v", "h264_videotoolbox", "-profile:v", "high", "-b:v", "8M"],
|
||||
},
|
||||
// performance reference stream (see tests/it/perf.rs)
|
||||
Fixture {
|
||||
name: "bench_1080p",
|
||||
source: "testsrc2",
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
frames: 120,
|
||||
filter: "noise=alls=3:allf=t",
|
||||
args: &["-preset", "medium", "-crf", "20"],
|
||||
},
|
||||
Fixture {
|
||||
name: "intra_cavlc",
|
||||
source: "testsrc2",
|
||||
width: 176,
|
||||
height: 144,
|
||||
frames: 3,
|
||||
filter: "",
|
||||
args: &["-profile:v", "baseline", "-x264-params", "keyint=1:no-deblock=1"],
|
||||
},
|
||||
Fixture {
|
||||
name: "intra_cavlc_noise",
|
||||
source: "mandelbrot",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 3,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "baseline", "-x264-params", "keyint=1:no-deblock=1"],
|
||||
},
|
||||
Fixture {
|
||||
name: "intra_cavlc_8x8",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 3,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "keyint=1:no-deblock=1:cabac=0"],
|
||||
},
|
||||
Fixture {
|
||||
name: "intra_cavlc_cqm",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 3,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "keyint=1:no-deblock=1:cabac=0:cqm=jvt"],
|
||||
},
|
||||
Fixture {
|
||||
name: "p_cavlc_nodeblock",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 20,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "baseline", "-x264-params", "no-deblock=1:ref=3"],
|
||||
},
|
||||
Fixture {
|
||||
name: "baseline_qcif",
|
||||
source: "testsrc2",
|
||||
width: 176,
|
||||
height: 144,
|
||||
frames: 30,
|
||||
filter: "",
|
||||
args: &["-profile:v", "baseline", "-x264-params", "keyint=15"],
|
||||
},
|
||||
Fixture {
|
||||
name: "baseline_cif_noise",
|
||||
source: "mandelbrot",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 25,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "baseline"],
|
||||
},
|
||||
Fixture {
|
||||
name: "cavlc_b",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 30,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "cabac=0:bframes=3"],
|
||||
},
|
||||
Fixture {
|
||||
name: "cavlc_b_temporal",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 30,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "main", "-x264-params", "cabac=0:bframes=3:direct=temporal:weightb=1"],
|
||||
},
|
||||
Fixture {
|
||||
name: "cavlc_weightp",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 30,
|
||||
filter: "fade=in:0:25",
|
||||
args: &["-profile:v", "main", "-x264-params", "cabac=0:weightp=2:bframes=2:weightb=1"],
|
||||
},
|
||||
Fixture { name: "main_cabac_b", source: "testsrc2", width: 352, height: 288, frames: 30, filter: NOISE, args: &["-profile:v", "main"] },
|
||||
Fixture { name: "high_720p", source: "smptehdbars", width: 1280, height: 720, frames: 10, filter: NOISE, args: &["-profile:v", "high"] },
|
||||
Fixture { name: "high_1080p", source: "testsrc2", width: 1920, height: 1080, frames: 6, filter: "", args: &["-profile:v", "high"] },
|
||||
Fixture { name: "crop_1918x1078", source: "testsrc2", width: 1918, height: 1078, frames: 5, filter: NOISE, args: &["-profile:v", "high"] },
|
||||
Fixture {
|
||||
name: "bpyramid",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 30,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "bframes=3:b-pyramid=normal"],
|
||||
},
|
||||
Fixture {
|
||||
name: "weightp2",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 30,
|
||||
filter: "fade=in:0:25",
|
||||
args: &["-profile:v", "high", "-x264-params", "weightp=2"],
|
||||
},
|
||||
Fixture {
|
||||
name: "weightb",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 30,
|
||||
filter: "fade=in:0:25",
|
||||
args: &["-profile:v", "high", "-x264-params", "weightb=1:bframes=3"],
|
||||
},
|
||||
Fixture {
|
||||
name: "direct_temporal",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 30,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "direct=temporal:bframes=3"],
|
||||
},
|
||||
Fixture {
|
||||
name: "direct_spatial",
|
||||
source: "mandelbrot",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 30,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "direct=spatial:bframes=3"],
|
||||
},
|
||||
Fixture {
|
||||
name: "ref4",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 30,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "ref=4:bframes=2"],
|
||||
},
|
||||
Fixture {
|
||||
name: "no_deblock",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 20,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "no-deblock=1"],
|
||||
},
|
||||
Fixture {
|
||||
name: "deblock_m2_2",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 20,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "deblock=-2,2"],
|
||||
},
|
||||
Fixture {
|
||||
name: "cqm_jvt",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 20,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "cqm=jvt"],
|
||||
},
|
||||
Fixture {
|
||||
name: "slices4",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 20,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "slices=4"],
|
||||
},
|
||||
Fixture {
|
||||
name: "slices4_cavlc",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 20,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "baseline", "-x264-params", "slices=4"],
|
||||
},
|
||||
Fixture {
|
||||
name: "keyint10",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 30,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "keyint=10"],
|
||||
},
|
||||
Fixture {
|
||||
name: "open_gop",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 40,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "open-gop=1:keyint=12:bframes=3"],
|
||||
},
|
||||
Fixture {
|
||||
name: "constrained_intra",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 20,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "constrained-intra=1"],
|
||||
},
|
||||
Fixture {
|
||||
name: "no8x8dct",
|
||||
source: "testsrc2",
|
||||
width: 352,
|
||||
height: 288,
|
||||
frames: 20,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "high", "-x264-params", "8x8dct=0"],
|
||||
},
|
||||
Fixture { name: "qp50", source: "testsrc2", width: 352, height: 288, frames: 20, filter: NOISE, args: &["-profile:v", "high", "-qp", "50"] },
|
||||
Fixture { name: "qp1", source: "mandelbrot", width: 352, height: 288, frames: 10, filter: NOISE, args: &["-profile:v", "high", "-qp", "1"] },
|
||||
Fixture {
|
||||
name: "qp1_cavlc",
|
||||
source: "mandelbrot",
|
||||
width: 176,
|
||||
height: 144,
|
||||
frames: 10,
|
||||
filter: NOISE,
|
||||
args: &["-profile:v", "main", "-qp", "1", "-x264-params", "cabac=0"],
|
||||
},
|
||||
];
|
||||
|
||||
pub fn fixture(name: &str) -> &'static Fixture {
|
||||
FIXTURES.iter().find(|f| f.name == name).unwrap_or_else(|| panic!("unknown fixture {name}"))
|
||||
}
|
||||
|
||||
/// ffmpeg (see `craft_media_testkit::oracle`).
|
||||
pub fn ffmpeg() -> Option<PathBuf> {
|
||||
craft_media_testkit::ffmpeg()
|
||||
}
|
||||
|
||||
pub fn fixtures_dir() -> PathBuf {
|
||||
craft_media_testkit::fixtures_dir("h264")
|
||||
}
|
||||
|
||||
fn run(cmd: &mut Command) -> bool {
|
||||
match cmd.output() {
|
||||
Ok(o) if o.status.success() => true,
|
||||
Ok(o) => {
|
||||
eprintln!("command failed: {:?}\n{}", cmd, String::from_utf8_lossy(&o.stderr));
|
||||
false
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("failed to run {:?}: {e}", cmd);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate (if needed) the fixture stream and its ffmpeg reference decode.
|
||||
/// Returns None (with a message) when ffmpeg is unavailable.
|
||||
pub fn ensure(f: &Fixture) -> Option<(PathBuf, PathBuf)> {
|
||||
let ff = craft_media_testkit::ffmpeg_or_skip(f.name)?;
|
||||
let _generation = FIXTURE_GENERATION.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
let dir = fixtures_dir();
|
||||
let h264 = dir.join(format!("{}.h264", f.name));
|
||||
let yuv = dir.join(format!("{}.yuv", f.name));
|
||||
if !h264.exists() {
|
||||
let tmp = craft_media_testkit::temp_path(&h264);
|
||||
let mut vf = format!("{}=size={}x{}:rate=25,format=yuv420p", f.source, f.width, f.height);
|
||||
if !f.filter.is_empty() {
|
||||
vf.push(',');
|
||||
vf.push_str(f.filter);
|
||||
}
|
||||
let mut c = Command::new(&ff);
|
||||
c.args(["-hide_banner", "-loglevel", "error", "-y", "-f", "lavfi", "-i", &vf]);
|
||||
c.args(["-frames:v", &f.frames.to_string()]);
|
||||
if !f.args.contains(&"-c:v") {
|
||||
c.args(["-c:v", "libx264"]);
|
||||
}
|
||||
c.args(f.args);
|
||||
c.args(["-f", "h264"]).arg(&tmp);
|
||||
if !run(&mut c) {
|
||||
// Platform encoders (VideoToolbox) are optional; libx264 fixtures must generate.
|
||||
assert!(f.args.contains(&"-c:v"), "fixture generation failed for {}", f.name);
|
||||
eprintln!("SKIPPED ({}): encoder unavailable", f.name);
|
||||
return None;
|
||||
}
|
||||
craft_media_testkit::fixtures::publish(&tmp, &h264).unwrap();
|
||||
}
|
||||
if !yuv.exists() {
|
||||
let tmp = craft_media_testkit::temp_path(&yuv);
|
||||
let mut c = Command::new(&ff);
|
||||
c.args(["-hide_banner", "-loglevel", "error", "-y", "-i"]).arg(&h264);
|
||||
c.args(["-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", "yuv420p"]).arg(&tmp);
|
||||
assert!(run(&mut c), "reference decode failed for {}", f.name);
|
||||
craft_media_testkit::fixtures::publish(&tmp, &yuv).unwrap();
|
||||
}
|
||||
Some((h264, yuv))
|
||||
}
|
||||
|
||||
/// Split an Annex-B stream into access units (new AU at AUD/SPS/PPS/SEI after a slice, or at a slice with
|
||||
/// first_mb_in_slice == 0).
|
||||
pub fn split_access_units(data: &[u8]) -> Vec<&[u8]> {
|
||||
// find start code positions
|
||||
let mut starts = Vec::new();
|
||||
let mut i = 0;
|
||||
while i + 3 <= data.len() {
|
||||
if data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1 {
|
||||
let s = if i > 0 && data[i - 1] == 0 { i - 1 } else { i };
|
||||
starts.push((s, i + 3));
|
||||
i += 3;
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
let mut aus = Vec::new();
|
||||
let mut au_start = 0usize;
|
||||
let mut seen_slice = false;
|
||||
for &(sc, payload) in &starts {
|
||||
if payload >= data.len() {
|
||||
continue;
|
||||
}
|
||||
let t = data[payload] & 0x1f;
|
||||
let is_slice = t == 1 || t == 5;
|
||||
let first_mb_zero = is_slice && payload + 1 < data.len() && data[payload + 1] & 0x80 != 0;
|
||||
let boundary = seen_slice && (matches!(t, 6..=9) || first_mb_zero);
|
||||
if boundary {
|
||||
aus.push(&data[au_start..sc]);
|
||||
au_start = sc;
|
||||
seen_slice = false;
|
||||
}
|
||||
if is_slice {
|
||||
seen_slice = true;
|
||||
}
|
||||
}
|
||||
if au_start < data.len() {
|
||||
aus.push(&data[au_start..]);
|
||||
}
|
||||
aus
|
||||
}
|
||||
|
||||
/// Decode a whole Annex-B file access unit by access unit (pts = AU index).
|
||||
/// Failed decode: (access unit index, error, pictures output before the error).
|
||||
pub type DecodeFailure = (usize, craft_video::h264::decode::Error, Vec<Picture>);
|
||||
|
||||
pub fn decode_file(path: &Path) -> Result<Vec<Picture>, DecodeFailure> {
|
||||
decode_file_threads(path, 0)
|
||||
}
|
||||
|
||||
/// Decode with `threads` worker threads (0 = decoder default).
|
||||
pub fn decode_file_threads(path: &Path, threads: usize) -> Result<Vec<Picture>, DecodeFailure> {
|
||||
decode_file_opts(path, threads, false)
|
||||
}
|
||||
|
||||
/// [`decode_file_threads`] with draft mode ([`Decoder::set_draft`]) on or off.
|
||||
pub fn decode_file_opts(path: &Path, threads: usize, draft: bool) -> Result<Vec<Picture>, DecodeFailure> {
|
||||
let data = std::fs::read(path).unwrap();
|
||||
let mut dec = if threads == 0 { Decoder::new() } else { Decoder::with_threads(threads) };
|
||||
assert!(!dec.draft(), "draft mode is off by default");
|
||||
dec.set_draft(draft);
|
||||
let mut out = Vec::new();
|
||||
for (i, au) in split_access_units(&data).into_iter().enumerate() {
|
||||
match dec.decode(au, i as i64) {
|
||||
Ok(p) => out.extend(p),
|
||||
Err(e) => return Err((i, e, out)),
|
||||
}
|
||||
}
|
||||
out.extend(dec.flush());
|
||||
if let Some(e) = dec.take_error() {
|
||||
return Err((usize::MAX, e, out));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Compare decoded pictures with a raw yuv420p reference; returns a diagnostic on the first mismatch.
|
||||
pub fn compare(pics: &[Picture], reference: &[u8], w: usize, h: usize) -> Result<(), String> {
|
||||
let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
|
||||
let fsize = w * h + 2 * cw * ch;
|
||||
let nref = reference.len() / fsize;
|
||||
for (i, p) in pics.iter().enumerate() {
|
||||
if i >= nref {
|
||||
return Err(format!("decoder produced {} frames, reference has {}", pics.len(), nref));
|
||||
}
|
||||
if p.width as usize != w || p.height as usize != h {
|
||||
return Err(format!("frame {i}: size {}x{} != {}x{}", p.width, p.height, w, h));
|
||||
}
|
||||
let f = &reference[i * fsize..(i + 1) * fsize];
|
||||
let planes = [
|
||||
("Y", &p.y[..], &f[..w * h], w, h, 16),
|
||||
("U", &p.u[..], &f[w * h..w * h + cw * ch], cw, ch, 8),
|
||||
("V", &p.v[..], &f[w * h + cw * ch..], cw, ch, 8),
|
||||
];
|
||||
for (name, got, exp, pw, ph, mbs) in planes {
|
||||
if got != exp {
|
||||
let mut first = None;
|
||||
let mut count = 0usize;
|
||||
let mut maxd = 0i32;
|
||||
for y in 0..ph {
|
||||
for x in 0..pw {
|
||||
let (a, b) = (got[y * pw + x], exp[y * pw + x]);
|
||||
if a != b {
|
||||
count += 1;
|
||||
maxd = maxd.max((a as i32 - b as i32).abs());
|
||||
if first.is_none() {
|
||||
first = Some((x, y, a, b));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let (x, y, a, b) = first.unwrap();
|
||||
let mb_w = pw.div_ceil(mbs);
|
||||
return Err(format!(
|
||||
"frame {i} (poc {}, pts {}) plane {name}: first mismatch at ({x},{y}) MB {} (mb_x {}, mb_y {}): got {a} want {b}; {count} samples differ, max diff {maxd}",
|
||||
p.poc,
|
||||
p.pts,
|
||||
(y / mbs) * mb_w + x / mbs,
|
||||
x / mbs,
|
||||
y / mbs
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
if pics.len() != nref {
|
||||
return Err(format!("decoder produced {} frames, reference has {}", pics.len(), nref));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Full check of one fixture: generate, decode, compare. Returns Ok(false) when skipped.
|
||||
pub fn check_fixture(name: &str) -> Result<bool, String> {
|
||||
let f = fixture(name);
|
||||
let Some((h264, yuv)) = ensure(f) else { return Ok(false) };
|
||||
let reference = std::fs::read(&yuv).unwrap();
|
||||
// single-threaded and frame-threaded decoding (an odd thread count and every core) must all
|
||||
// be bit-exact
|
||||
for threads in [1, 3, 0] {
|
||||
let pics = match decode_file_threads(&h264, threads) {
|
||||
Ok(p) => p,
|
||||
Err((au, e, partial)) => {
|
||||
// still report comparison of what was decoded
|
||||
let cmp = compare(&partial, &reference, f.width as usize, f.height as usize).err().unwrap_or_default();
|
||||
return Err(format!("{name} (threads {threads}): decode error at access unit {au}: {e} (after {} pictures) {cmp}", partial.len()));
|
||||
}
|
||||
};
|
||||
compare(&pics, &reference, f.width as usize, f.height as usize).map_err(|e| format!("{name} (threads {threads}): {e}"))?;
|
||||
if pics.iter().any(|p| p.draft) {
|
||||
return Err(format!("{name} (threads {threads}): draft picture without draft mode"));
|
||||
}
|
||||
check_pts(&pics).map_err(|e| format!("{name} (threads {threads}): {e}"))?;
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Output pts values (= access unit index) must be a permutation of 0..n, and follow POC order
|
||||
/// between IDR pictures.
|
||||
pub fn check_pts(pics: &[Picture]) -> Result<(), String> {
|
||||
let mut seen = vec![false; pics.len()];
|
||||
for p in pics {
|
||||
let i = p.pts as usize;
|
||||
if i >= seen.len() || seen[i] {
|
||||
return Err(format!("unexpected pts {} in output", p.pts));
|
||||
}
|
||||
seen[i] = true;
|
||||
}
|
||||
for w in pics.windows(2) {
|
||||
if !w[1].key && w[1].poc <= w[0].poc {
|
||||
return Err(format!("output POC order violated: {} then {}", w[0].poc, w[1].poc));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
//! Bit-exactness against ffmpeg's decoder on libx264-generated fixtures (skipped when ffmpeg is absent).
|
||||
|
||||
use crate::common;
|
||||
|
||||
macro_rules! fixture_tests {
|
||||
($($name:ident),* $(,)?) => {
|
||||
$(
|
||||
#[test]
|
||||
fn $name() {
|
||||
match common::check_fixture(stringify!($name)) {
|
||||
Ok(true) => {}
|
||||
Ok(false) => eprintln!("skipped {}", stringify!($name)),
|
||||
Err(e) => panic!("{e}"),
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
fixture_tests!(
|
||||
intra_cavlc,
|
||||
intra_cavlc_noise,
|
||||
intra_cavlc_8x8,
|
||||
intra_cavlc_cqm,
|
||||
p_cavlc_nodeblock,
|
||||
baseline_qcif,
|
||||
baseline_cif_noise,
|
||||
cavlc_b,
|
||||
cavlc_b_temporal,
|
||||
cavlc_weightp,
|
||||
slices4_cavlc,
|
||||
qp1_cavlc,
|
||||
main_cabac_b,
|
||||
high_720p,
|
||||
high_1080p,
|
||||
crop_1918x1078,
|
||||
bpyramid,
|
||||
weightp2,
|
||||
weightb,
|
||||
direct_temporal,
|
||||
direct_spatial,
|
||||
ref4,
|
||||
no_deblock,
|
||||
deblock_m2_2,
|
||||
cqm_jvt,
|
||||
slices4,
|
||||
keyint10,
|
||||
open_gop,
|
||||
constrained_intra,
|
||||
no8x8dct,
|
||||
qp50,
|
||||
qp1,
|
||||
vt_high,
|
||||
vt_main,
|
||||
vt_baseline,
|
||||
vt_high_1080p,
|
||||
);
|
||||
|
||||
/// Draft mode (reduced-resolution playback) skips deblocking of non-reference pictures only:
|
||||
/// every other picture stays bit-exact with ffmpeg, single- and frame-threaded, the skipped ones
|
||||
/// are flagged, and they are the only ones that differ.
|
||||
#[test]
|
||||
fn draft_mode_changes_only_flagged_non_reference_pictures() {
|
||||
let mut checked = 0;
|
||||
for name in ["main_cabac_b", "bpyramid", "cavlc_b", "direct_temporal", "weightb"] {
|
||||
let f = common::fixture(name);
|
||||
let Some((h264, yuv)) = common::ensure(f) else {
|
||||
eprintln!("skipped {name}");
|
||||
continue;
|
||||
};
|
||||
let reference = std::fs::read(&yuv).unwrap();
|
||||
let (w, h) = (f.width as usize, f.height as usize);
|
||||
let fsize = w * h + 2 * w.div_ceil(2) * h.div_ceil(2);
|
||||
for threads in [1, 0] {
|
||||
let pics = common::decode_file_opts(&h264, threads, true).unwrap_or_else(|(au, e, _)| panic!("{name}: error at {au}: {e}"));
|
||||
common::check_pts(&pics).unwrap_or_else(|e| panic!("{name}: {e}"));
|
||||
assert_eq!(pics.len() * fsize, reference.len(), "{name}: picture count");
|
||||
let (mut draft, mut changed) = (0, 0);
|
||||
for (i, p) in pics.iter().enumerate() {
|
||||
let r = &reference[i * fsize..(i + 1) * fsize];
|
||||
let same = p.y == r[..w * h] && [&p.u[..], &p.v[..]].concat() == r[w * h..];
|
||||
if p.draft {
|
||||
draft += 1;
|
||||
changed += !same as usize;
|
||||
} else {
|
||||
assert!(same, "{name} (threads {threads}): picture {i} (not draft) differs from the reference");
|
||||
}
|
||||
}
|
||||
assert!(draft > 0, "{name}: has non-reference pictures");
|
||||
if name != "no_deblock" {
|
||||
assert!(changed > 0, "{name}: skipping deblocking changes some draft picture");
|
||||
}
|
||||
checked += 1;
|
||||
}
|
||||
}
|
||||
if checked == 0 {
|
||||
eprintln!("skipped: no fixtures");
|
||||
}
|
||||
}
|
||||
|
||||
/// Print which coding tools each fixture exercises (run with `-- --ignored --nocapture`).
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn coverage_report() {
|
||||
for f in common::FIXTURES {
|
||||
let Some((h264, _)) = common::ensure(f) else { return };
|
||||
let data = std::fs::read(h264).unwrap();
|
||||
let mut dec = craft_video::h264::decode::Decoder::new();
|
||||
for au in common::split_access_units(&data) {
|
||||
dec.decode(au, 0).unwrap();
|
||||
}
|
||||
dec.flush();
|
||||
let s = dec.stats();
|
||||
println!(
|
||||
"{:<20} pics {:>3} cavlc/cabac {:>3}/{:<3} I/P/B {:>3}/{:>3}/{:>3} i4 {:>6} i8 {:>6} i16 {:>6} pcm {:>4} pskip {:>6} bskip {:>6} bdirect {:>5} inter {:>6} t8 {:>6} mmco {:>3} lt {} gaps {} wp {} tdirect {}",
|
||||
f.name,
|
||||
s.pictures,
|
||||
s.slices_cavlc,
|
||||
s.slices_cabac,
|
||||
s.slices_i,
|
||||
s.slices_p,
|
||||
s.slices_b,
|
||||
s.mb_i4x4,
|
||||
s.mb_i8x8,
|
||||
s.mb_i16x16,
|
||||
s.mb_pcm,
|
||||
s.mb_p_skip,
|
||||
s.mb_b_skip,
|
||||
s.mb_b_direct16x16,
|
||||
s.mb_inter,
|
||||
s.mb_inter_8x8_transform,
|
||||
s.mmco_ops,
|
||||
s.long_term_marks,
|
||||
s.frame_num_gaps,
|
||||
s.weighted_slices,
|
||||
s.temporal_direct_slices
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Pre-generate every fixture and its reference decode .
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn generate_fixtures() {
|
||||
let dir = common::fixtures_dir();
|
||||
for f in common::FIXTURES {
|
||||
let outs = [dir.join(format!("{}.h264", f.name)), dir.join(format!("{}.yuv", f.name))];
|
||||
craft_media_testkit::generate_and_report(&format!("h264/{}", f.name), &outs, || common::ensure(f));
|
||||
}
|
||||
}
|
||||
|
||||
/// Concurrent consumers must publish one complete fixture without replacing an open file.
|
||||
#[test]
|
||||
fn concurrent_fixture_publication_is_safe() {
|
||||
if craft_media_testkit::ffmpeg_or_skip("concurrent_fixture_publication").is_none() {
|
||||
return;
|
||||
}
|
||||
let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
|
||||
let name = Box::leak(format!("concurrent_fixture_{}_{stamp}", std::process::id()).into_boxed_str());
|
||||
let fixture = common::Fixture {
|
||||
name,
|
||||
source: "testsrc2",
|
||||
width: 64,
|
||||
height: 48,
|
||||
frames: 2,
|
||||
filter: "",
|
||||
args: &["-preset", "ultrafast", "-x264-params", "keyint=1"],
|
||||
};
|
||||
let open_published = |path: &std::path::Path| {
|
||||
let mut options = std::fs::OpenOptions::new();
|
||||
options.read(true);
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
// Like the external decoder, allow reads but deny replacement while open.
|
||||
options.share_mode(1);
|
||||
}
|
||||
options.open(path).unwrap()
|
||||
};
|
||||
let start = std::sync::Barrier::new(4);
|
||||
let results = std::thread::scope(|scope| {
|
||||
let jobs: Vec<_> = (0..4)
|
||||
.map(|_| {
|
||||
scope.spawn(|| {
|
||||
start.wait();
|
||||
let paths = common::ensure(&fixture).expect("software fixture must generate");
|
||||
// Keep both published files open until every producer has finished.
|
||||
let files = (open_published(&paths.0), open_published(&paths.1));
|
||||
(paths, files)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
jobs.into_iter().map(|j| j.join().expect("fixture producer panicked")).collect::<Vec<_>>()
|
||||
});
|
||||
let paths = results[0].0.clone();
|
||||
for (got, _) in &results {
|
||||
assert_eq!(got, &paths);
|
||||
}
|
||||
assert!(!std::fs::read(&paths.0).unwrap().is_empty());
|
||||
assert_eq!(std::fs::metadata(&paths.1).unwrap().len(), 2 * (64 * 48 + 2 * 32 * 24));
|
||||
drop(results);
|
||||
std::fs::remove_file(paths.0).unwrap();
|
||||
std::fs::remove_file(paths.1).unwrap();
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
//! Hostile-input robustness: header-region sweeps (SPS/PPS/first slice headers), truncation and
|
||||
//! random mutation of small libx264 streams must never panic or run away. Skips without ffmpeg.
|
||||
|
||||
use crate::common;
|
||||
|
||||
use std::panic::{AssertUnwindSafe, catch_unwind};
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
use craft_video::h264::decode::Decoder;
|
||||
|
||||
struct Rng(u64);
|
||||
impl Rng {
|
||||
fn next(&mut self) -> u64 {
|
||||
self.0 ^= self.0 << 13;
|
||||
self.0 ^= self.0 >> 7;
|
||||
self.0 ^= self.0 << 17;
|
||||
self.0
|
||||
}
|
||||
fn below(&mut self, n: usize) -> usize {
|
||||
(self.next() % n.max(1) as u64) as usize
|
||||
}
|
||||
}
|
||||
|
||||
/// (name, extra x264 args, pix_fmt)
|
||||
const SEEDS: &[(&str, &str, &str)] = &[
|
||||
("h264_fz_cabac_b", "-profile:v high -bf 2 -g 8 -x264-params cabac=1:weightp=2:8x8dct=1", "yuv420p"),
|
||||
("h264_fz_cavlc", "-profile:v baseline -g 6 -x264-params cabac=0", "yuv420p"),
|
||||
("h264_fz_interlaced", "-profile:v high -flags +ildct+ilme -bf 1 -g 6 -x264-params tff=1:interlaced=1", "yuv420p"),
|
||||
("h264_fz_10bit_422", "-profile:v high422 -bf 1 -g 6", "yuv422p10le"),
|
||||
("h264_fz_multislice", "-profile:v main -g 6 -x264-params slices=3:cabac=1", "yuv420p"),
|
||||
];
|
||||
|
||||
fn make(ff: &std::path::Path, name: &str, args: &str, pix: &str) -> Option<Vec<u8>> {
|
||||
let out = craft_media_testkit::fixtures_dir("h264").join(format!("{name}.h264"));
|
||||
let p = craft_media_testkit::fixtures::generate(&out, |tmp| {
|
||||
let a: Vec<&str> = args.split_whitespace().collect();
|
||||
matches!(
|
||||
Command::new(ff)
|
||||
.args(["-hide_banner", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=64x48:rate=25", "-frames:v", "7", "-pix_fmt", pix, "-c:v", "libx264"])
|
||||
.args(a)
|
||||
.args(["-f", "h264"])
|
||||
.arg(tmp)
|
||||
.stdin(Stdio::null())
|
||||
.status(),
|
||||
Ok(s) if s.success()
|
||||
)
|
||||
})?;
|
||||
std::fs::read(p).ok()
|
||||
}
|
||||
|
||||
static PANICS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
|
||||
/// Decoder worker threads catch panics and turn them into errors; count them so they still fail the test.
|
||||
fn count_panics() {
|
||||
static ONCE: std::sync::Once = std::sync::Once::new();
|
||||
ONCE.call_once(|| {
|
||||
std::panic::set_hook(Box::new(|info| {
|
||||
PANICS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
eprintln!("PANIC: {info}");
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
fn run_all(d: &[u8], label: &str) {
|
||||
craft_media_testkit::set_progress(label);
|
||||
count_panics();
|
||||
let before = PANICS.load(std::sync::atomic::Ordering::SeqCst);
|
||||
let res = catch_unwind(AssertUnwindSafe(|| {
|
||||
for threads in [1, 3] {
|
||||
let mut dec = Decoder::with_threads(threads);
|
||||
for au in common::split_access_units(d) {
|
||||
let _ = dec.decode(au, 0);
|
||||
}
|
||||
let _ = dec.decode(d, 1);
|
||||
let _ = dec.flush();
|
||||
let _ = dec.take_error();
|
||||
}
|
||||
}));
|
||||
assert!(res.is_ok() && PANICS.load(std::sync::atomic::Ordering::SeqCst) == before, "panic: {label}");
|
||||
}
|
||||
|
||||
/// Seconds the whole sweep may take before it counts as a hang (override: FILMCRAFT_HOSTILE_TIMEOUT_SECS).
|
||||
const DEADLINE_SECS: u64 = 600;
|
||||
|
||||
/// CI runs a rotating share of every part of the sweep; `FILMCRAFT_FUZZ_ITERS=400` runs all of it (see [`Depth`]).
|
||||
#[test]
|
||||
fn hostile_streams_never_panic() {
|
||||
craft_media_testkit::with_deadline(DEADLINE_SECS, sweep);
|
||||
}
|
||||
|
||||
fn sweep() {
|
||||
let Some(ff) = craft_media_testkit::ffmpeg_or_skip("h264 fuzz") else { return };
|
||||
let depth = Depth::from_env();
|
||||
for &(name, args, pix) in SEEDS {
|
||||
if !craft_media_testkit::ffmpeg_has_encoder(&ff, "libx264") {
|
||||
craft_media_testkit::skip_test("h264 fuzz", &format!("this ffmpeg has no libx264 encoder (seed {name})"));
|
||||
continue;
|
||||
}
|
||||
let Some(seed) = make(&ff, name, args, pix) else {
|
||||
panic!("ffmpeg has libx264 but could not encode the {name} seed");
|
||||
};
|
||||
for n in depth.truncations(seed.len()) {
|
||||
run_all(&seed[..n], &format!("{name} truncate {n}"));
|
||||
}
|
||||
header_flips(&depth, &seed, name);
|
||||
random_rounds(&depth, &seed, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// SPS/PPS/first slice header region: every byte to a few values, every bit.
|
||||
fn header_flips(depth: &Depth, seed: &[u8], name: &str) {
|
||||
for p in 0..seed.len().min(120) {
|
||||
for (i, v) in VALUES.into_iter().enumerate() {
|
||||
if !depth.value(p, i) {
|
||||
continue;
|
||||
}
|
||||
let mut d = seed.to_vec();
|
||||
d[p] = v;
|
||||
run_all(&d, &format!("{name} byte {p}={v:#x}"));
|
||||
}
|
||||
for b in 0..8 {
|
||||
if !depth.bit(p, b) {
|
||||
continue;
|
||||
}
|
||||
let mut d = seed.to_vec();
|
||||
d[p] ^= 1 << b;
|
||||
run_all(&d, &format!("{name} flip {p}.{b}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn random_rounds(depth: &Depth, seed: &[u8], name: &str) {
|
||||
let mut rng = Rng(0x9e3779b97f4a7c15 ^ name.len() as u64);
|
||||
// Skipped rounds still draw their mutations, so round i matches round i of a full run.
|
||||
for i in 0..depth.rounds {
|
||||
let mut d = seed.to_vec();
|
||||
for _ in 0..1 + rng.below(6) {
|
||||
let p = rng.below(d.len());
|
||||
if rng.below(2) == 0 {
|
||||
d[p] ^= 1 << rng.below(8);
|
||||
} else {
|
||||
d[p] = rng.next() as u8;
|
||||
}
|
||||
}
|
||||
if depth.round(i) {
|
||||
run_all(&d, &format!("{name} random {i}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const VALUES: [u8; 5] = [0, 0xff, 0x80, 0x01, 0x7f];
|
||||
|
||||
/// How much of the sweep runs. `FILMCRAFT_FUZZ_ITERS=N` runs all of it (about 300 truncation
|
||||
/// points spread over each seed, every header byte, overwrite value and bit flip) plus N random
|
||||
/// rounds per seed: 400 is the full depth, more hunts longer. Unset, CI runs a third of the
|
||||
/// truncation points and of the full depth's random rounds and, for each header byte, one value
|
||||
/// and two bits. A rotation offset shifts which share runs (`FILMCRAFT_FUZZ_SEED`, else the UTC
|
||||
/// day number), so every case of the full sweep runs within a few days, and a random round's
|
||||
/// mutations match the same round of a full run.
|
||||
struct Depth {
|
||||
full: bool,
|
||||
truncations: usize,
|
||||
rounds: usize,
|
||||
rotation: usize,
|
||||
}
|
||||
|
||||
impl Depth {
|
||||
fn from_env() -> Self {
|
||||
match std::env::var("FILMCRAFT_FUZZ_ITERS").ok().and_then(|s| s.parse().ok()) {
|
||||
Some(rounds) => Depth { full: true, truncations: 300, rounds, rotation: 0 },
|
||||
None => {
|
||||
let rotation = rotation();
|
||||
eprintln!("fuzz subset rotation {rotation} (FILMCRAFT_FUZZ_SEED={rotation} replays it; FILMCRAFT_FUZZ_ITERS=400 runs everything)");
|
||||
Depth { full: false, truncations: 300, rounds: 400, rotation }
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Truncation lengths for a `len`-byte input: every point of the full sweep, or every third one.
|
||||
fn truncations(&self, len: usize) -> std::iter::StepBy<std::ops::Range<usize>> {
|
||||
let stride = (len / self.truncations).max(1);
|
||||
let (start, step) = if self.full { (0, stride) } else { ((self.rotation % 3) * stride, 3 * stride) };
|
||||
(start..len).step_by(step)
|
||||
}
|
||||
fn value(&self, p: usize, i: usize) -> bool {
|
||||
self.full || i == (p + self.rotation % VALUES.len()) % VALUES.len()
|
||||
}
|
||||
fn bit(&self, p: usize, b: usize) -> bool {
|
||||
self.full || (b + p + self.rotation % 4).is_multiple_of(4)
|
||||
}
|
||||
fn round(&self, i: usize) -> bool {
|
||||
self.full || i % 3 == self.rotation % 3
|
||||
}
|
||||
}
|
||||
|
||||
/// The default subset's rotation offset: `FILMCRAFT_FUZZ_SEED`, else days since the Unix epoch (UTC).
|
||||
fn rotation() -> usize {
|
||||
std::env::var("FILMCRAFT_FUZZ_SEED").ok().and_then(|s| s.parse().ok()).unwrap_or_else(|| {
|
||||
let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_secs());
|
||||
usize::try_from(secs / 86_400).unwrap_or(0)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
//! Parameter set / slice header / POC parsing over the fixture streams.
|
||||
|
||||
use crate::common;
|
||||
|
||||
use craft_media::bitstream::{annexb_nals, unescape_rbsp};
|
||||
use craft_video::h264::decode::params::{Pps, Sps};
|
||||
use craft_video::h264::decode::slice::{NalHeader, PocState, SliceHeader, nal_type};
|
||||
|
||||
/// Parse every NAL of a fixture; returns (sps, number of pictures, pocs in decoding order).
|
||||
fn parse_stream(name: &str) -> Option<(Sps, usize, Vec<i32>)> {
|
||||
let f = common::fixture(name);
|
||||
let (h264, _) = common::ensure(f)?;
|
||||
let data = std::fs::read(h264).unwrap();
|
||||
let mut spss: Vec<Option<Sps>> = vec![None; 32];
|
||||
let mut ppss: Vec<Option<Pps>> = vec![None; 256];
|
||||
let mut poc_state = PocState::default();
|
||||
let mut pocs = Vec::new();
|
||||
let mut last: Option<SliceHeader> = None;
|
||||
let mut pictures = 0;
|
||||
for nal in annexb_nals(&data) {
|
||||
let hdr = NalHeader::parse(nal[0]).unwrap();
|
||||
let rbsp = unescape_rbsp(&nal[1..]);
|
||||
match hdr.nal_unit_type {
|
||||
nal_type::SPS => {
|
||||
let s = Sps::parse(&rbsp).unwrap();
|
||||
let id = s.id as usize;
|
||||
spss[id] = Some(s);
|
||||
}
|
||||
nal_type::PPS => {
|
||||
let p = Pps::parse(&rbsp, &spss).unwrap();
|
||||
let id = p.id as usize;
|
||||
ppss[id] = Some(p);
|
||||
}
|
||||
nal_type::SLICE | nal_type::IDR => {
|
||||
let (sh, _, sps) = SliceHeader::parse(&rbsp, hdr, |id| {
|
||||
let p = ppss[id as usize].as_ref().unwrap();
|
||||
Ok((p, spss[p.sps_id as usize].as_ref().unwrap()))
|
||||
})
|
||||
.unwrap();
|
||||
assert!(sh.header_bits <= rbsp.len() * 8);
|
||||
if sh.first_mb_in_slice == 0 {
|
||||
if let Some(prev) = &last {
|
||||
let poc = poc_state.compute(prev, sps);
|
||||
poc_state.update(prev, &poc);
|
||||
pocs.push(poc.frame());
|
||||
}
|
||||
pictures += 1;
|
||||
last = Some(sh);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let sps = spss.into_iter().flatten().next().unwrap();
|
||||
if let Some(prev) = &last {
|
||||
let poc = poc_state.compute(prev, &sps);
|
||||
pocs.push(poc.frame());
|
||||
}
|
||||
Some((sps, pictures, pocs))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn baseline_headers() {
|
||||
let Some((sps, pictures, pocs)) = parse_stream("baseline_qcif") else { return };
|
||||
assert_eq!(sps.profile_idc, 66);
|
||||
assert_eq!((sps.width(), sps.height()), (176, 144));
|
||||
assert_eq!(pictures, 30);
|
||||
// no B-frames: POC strictly increases between IDRs (keyint 15)
|
||||
for w in pocs.windows(2) {
|
||||
assert!(w[1] > w[0] || w[1] == 0, "{pocs:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cropped_high_profile_headers() {
|
||||
let Some((sps, pictures, _)) = parse_stream("crop_1918x1078") else { return };
|
||||
assert_eq!(sps.profile_idc, 100);
|
||||
assert_eq!(sps.chroma_format_idc, 1);
|
||||
assert_eq!(sps.crop_rect(), (0, 0, 1918, 1078));
|
||||
assert_eq!(pictures, 5);
|
||||
let vui = sps.vui.as_ref().expect("x264 writes a VUI");
|
||||
assert!(vui.bitstream_restriction.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn b_frame_pocs_are_a_permutation() {
|
||||
let Some((_, pictures, pocs)) = parse_stream("bpyramid") else { return };
|
||||
assert_eq!(pictures, 30);
|
||||
let mut sorted = pocs.clone();
|
||||
sorted.sort();
|
||||
sorted.dedup();
|
||||
assert_eq!(sorted.len(), pocs.len(), "duplicate POCs: {pocs:?}");
|
||||
// decoding order differs from output order
|
||||
assert_ne!(sorted, pocs);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_scaling_matrices_parsed() {
|
||||
let Some(f) = common::ensure(common::fixture("cqm_jvt")) else { return };
|
||||
let data = std::fs::read(f.0).unwrap();
|
||||
let mut spss: Vec<Option<Sps>> = vec![None; 32];
|
||||
let mut found = false;
|
||||
for nal in annexb_nals(&data) {
|
||||
let hdr = NalHeader::parse(nal[0]).unwrap();
|
||||
let rbsp = unescape_rbsp(&nal[1..]);
|
||||
if hdr.nal_unit_type == nal_type::SPS {
|
||||
let s = Sps::parse(&rbsp).unwrap();
|
||||
let id = s.id as usize;
|
||||
spss[id] = Some(s);
|
||||
} else if hdr.nal_unit_type == nal_type::PPS {
|
||||
let p = Pps::parse(&rbsp, &spss).unwrap();
|
||||
// JVT matrices are the spec defaults: intra 4x4 luma (0,0) = 6
|
||||
assert_eq!(p.scaling.m4[0][0], 6);
|
||||
assert_eq!(p.scaling.m8[0][0], 6);
|
||||
assert!(p.transform_8x8_mode);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
assert!(found);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
mod common;
|
||||
mod conformance;
|
||||
mod fuzz_hostile;
|
||||
mod headers;
|
||||
mod perf;
|
||||
mod pps_validation;
|
||||
mod robustness;
|
||||
@@ -0,0 +1,34 @@
|
||||
//! Decode speed on a 1080p High-profile stream (ignored by default; run with
|
||||
//! `cargo test --release -p craft-video --features h264-decode,threads --test h264_decode perf:: -- --ignored --nocapture`).
|
||||
|
||||
use crate::common;
|
||||
|
||||
use craft_video::h264::decode::Decoder;
|
||||
use std::time::Instant;
|
||||
|
||||
fn bench(data: &[u8], threads: usize, iters: usize) -> (usize, f64) {
|
||||
let mut best = f64::MAX;
|
||||
let mut frames = 0;
|
||||
for _ in 0..iters {
|
||||
let mut dec = Decoder::with_threads(threads);
|
||||
let t0 = Instant::now();
|
||||
frames = dec.decode(data, 0).expect("decode").len() + dec.flush().len();
|
||||
best = best.min(t0.elapsed().as_secs_f64());
|
||||
}
|
||||
(frames, best)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn perf_1080p() {
|
||||
let f = common::fixture("bench_1080p");
|
||||
let Some((h264, _)) = common::ensure(f) else { return };
|
||||
// correctness first
|
||||
common::check_fixture("bench_1080p").unwrap();
|
||||
let data = std::fs::read(h264).unwrap();
|
||||
let cores = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1);
|
||||
for threads in [1, cores.min(16)] {
|
||||
let (frames, secs) = bench(&data, threads, 5);
|
||||
println!("1080p High CRF20 ({} KB): {threads} thread(s): {frames} frames in {secs:.3}s = {:.1} fps", data.len() / 1024, frames as f64 / secs);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
//! #1441: malformed PPS fields must return `Error::Invalid`, not panic.
|
||||
|
||||
use craft_video::h264::decode::Decoder;
|
||||
|
||||
/// Valid, synthetic Baseline SPS (176x144, 8-bit).
|
||||
const SPS: [u8; 12] = [0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e, 0xda, 0x0b, 0x13, 0x90];
|
||||
|
||||
fn decode_pps(pps: &[u8]) -> Result<(), craft_video::h264::decode::Error> {
|
||||
let mut decoder = Decoder::with_threads(1);
|
||||
decoder.decode(&SPS, 0).expect("sps decodes");
|
||||
decoder.decode(pps, 0).map(|_| ())
|
||||
}
|
||||
|
||||
/// The reporter's literal: PPS with `num_ref_idx_l0_default_active_minus1 = u32::MAX`.
|
||||
#[test]
|
||||
fn hostile_pps_returns_an_error_instead_of_panicking() {
|
||||
let pps = [0x00, 0x00, 0x00, 0x01, 0x68, 0xc8, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0x02, 0x3c, 0x80];
|
||||
let outcome = std::panic::catch_unwind(|| {
|
||||
let mut decoder = Decoder::with_threads(1);
|
||||
decoder.decode(&SPS, 0).unwrap();
|
||||
decoder.decode(&pps, 0)
|
||||
});
|
||||
assert!(outcome.is_ok_and(|result| result.is_err()));
|
||||
}
|
||||
|
||||
/// Write ue(v) as bits (helper for building synthetic PPS payloads).
|
||||
fn push_ue(bits: &mut Vec<bool>, v: u32) {
|
||||
let x = v as u64 + 1;
|
||||
let len = 64 - x.leading_zeros();
|
||||
for _ in 1..len {
|
||||
bits.push(false);
|
||||
}
|
||||
for i in (0..len).rev() {
|
||||
bits.push(x & (1 << i) != 0);
|
||||
}
|
||||
}
|
||||
|
||||
fn push_bits(bits: &mut Vec<bool>, v: u32, n: u32) {
|
||||
for i in (0..n).rev() {
|
||||
bits.push(v & (1 << i) != 0);
|
||||
}
|
||||
}
|
||||
|
||||
fn to_bytes(bits: &[bool]) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
for chunk in bits.chunks(8) {
|
||||
let mut b = 0u8;
|
||||
for (i, v) in chunk.iter().enumerate() {
|
||||
b |= (*v as u8) << (7 - i);
|
||||
}
|
||||
out.push(b);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Build a PPS NAL (header byte 0x68 + payload) with `num_slice_groups_minus1` = u32::MAX.
|
||||
#[test]
|
||||
fn hostile_num_slice_groups_is_rejected() {
|
||||
let mut bits = vec![true, true, false, false];
|
||||
push_ue(&mut bits, u32::MAX); // num_slice_groups_minus1
|
||||
bits.extend(std::iter::repeat_n(false, 64));
|
||||
let payload = to_bytes(&bits);
|
||||
bits.extend(std::iter::repeat_n(false, 64));
|
||||
let mut nal = vec![0x00, 0x00, 0x00, 0x01, 0x68];
|
||||
nal.extend_from_slice(&payload);
|
||||
assert!(std::panic::catch_unwind(|| decode_pps(&nal)).is_ok_and(|r| r.is_err()));
|
||||
}
|
||||
|
||||
/// `num_ref_idx_l1_default_active_minus1 = u32::MAX` (l0 valid) must error, not panic.
|
||||
#[test]
|
||||
fn hostile_num_ref_idx_l1_is_rejected() {
|
||||
let mut bits = vec![true, true, false, false];
|
||||
push_ue(&mut bits, 0); // num_slice_groups_minus1
|
||||
push_ue(&mut bits, 1); // num_ref_idx_l0_minus1
|
||||
push_ue(&mut bits, u32::MAX); // num_ref_idx_l1_minus1
|
||||
bits.extend(std::iter::repeat_n(false, 64));
|
||||
let mut nal = vec![0x00, 0x00, 0x00, 0x01, 0x68];
|
||||
nal.extend_from_slice(&to_bytes(&bits));
|
||||
assert!(std::panic::catch_unwind(|| decode_pps(&nal)).is_ok_and(|r| r.is_err()));
|
||||
}
|
||||
|
||||
/// `pic_init_qp_minus26 = i32::MAX` must error, not overflow.
|
||||
#[test]
|
||||
fn hostile_pic_init_qp_is_rejected() {
|
||||
let mut bits = vec![true, true, false, false];
|
||||
push_ue(&mut bits, 0); // num_slice_groups_minus1
|
||||
push_ue(&mut bits, 0); // num_ref_idx_l0_minus1
|
||||
push_ue(&mut bits, 0); // num_ref_idx_l1_minus1
|
||||
bits.push(false); // weighted_pred
|
||||
push_bits(&mut bits, 0, 2); // weighted_bipred_idc
|
||||
// se(i32::MAX): codeNum = 2*MAX-1 = u32::MAX-2... encode via ue of huge codeNum
|
||||
push_ue(&mut bits, u32::MAX - 2); // codeNum for +2^31-1... any out-of-range codeNum works
|
||||
bits.extend(std::iter::repeat_n(false, 64));
|
||||
let mut nal = vec![0x00, 0x00, 0x00, 0x01, 0x68];
|
||||
nal.extend_from_slice(&to_bytes(&bits));
|
||||
assert!(std::panic::catch_unwind(|| decode_pps(&nal)).is_ok_and(|r| r.is_err()));
|
||||
}
|
||||
|
||||
/// `weighted_bipred_idc = 3` is concealed to 2 per 7.4.2.2.1 (not rejected, not a panic).
|
||||
#[test]
|
||||
fn reserved_weighted_bipred_idc_is_concealed() {
|
||||
let mut bits = vec![true, true, false, false];
|
||||
push_ue(&mut bits, 0);
|
||||
push_ue(&mut bits, 0);
|
||||
push_ue(&mut bits, 0);
|
||||
bits.push(false); // weighted_pred
|
||||
push_bits(&mut bits, 3, 2); // weighted_bipred_idc = 3 (reserved)
|
||||
push_ue(&mut bits, 0); // pic_init_qp_minus26 = 0
|
||||
push_ue(&mut bits, 0); // pic_init_qs_minus26 = 0
|
||||
push_ue(&mut bits, 0); // chroma_qp_index_offset = 0
|
||||
bits.push(false); // deblocking_filter_control_present
|
||||
bits.push(false); // constrained_intra_pred
|
||||
bits.push(false); // redundant_pic_cnt_present
|
||||
bits.extend(std::iter::repeat_n(false, 64));
|
||||
let mut nal = vec![0x00, 0x00, 0x00, 0x01, 0x68];
|
||||
nal.extend_from_slice(&to_bytes(&bits));
|
||||
// parses fine; the decoder treats the field as 2
|
||||
decode_pps(&nal).expect("weighted_bipred_idc=3 concealed to 2");
|
||||
}
|
||||
|
||||
/// A well-formed minimal PPS still parses (no regression).
|
||||
#[test]
|
||||
fn minimal_valid_pps_parses() {
|
||||
let mut bits = vec![true, true, false, false];
|
||||
push_ue(&mut bits, 0);
|
||||
push_ue(&mut bits, 0);
|
||||
push_ue(&mut bits, 0);
|
||||
bits.push(false); // weighted_pred
|
||||
push_bits(&mut bits, 0, 2); // weighted_bipred_idc
|
||||
push_ue(&mut bits, 0); // pic_init_qp_minus26
|
||||
push_ue(&mut bits, 0); // pic_init_qs_minus26
|
||||
push_ue(&mut bits, 0); // chroma_qp_index_offset
|
||||
bits.push(true); // deblocking_filter_control_present
|
||||
bits.push(false); // constrained_intra_pred
|
||||
bits.push(false); // redundant_pic_cnt_present
|
||||
bits.extend(std::iter::repeat_n(false, 64));
|
||||
let mut nal = vec![0x00, 0x00, 0x00, 0x01, 0x68];
|
||||
nal.extend_from_slice(&to_bytes(&bits));
|
||||
if let Err(e) = decode_pps(&nal) {
|
||||
panic!("minimal PPS rejected: {e}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
//! avcC / length-prefixed input and resilience against damaged streams.
|
||||
|
||||
use crate::common;
|
||||
|
||||
use craft_media::bitstream::annexb_nals;
|
||||
use craft_video::h264::decode::Decoder;
|
||||
|
||||
/// Build an avcC record and length-prefixed samples from an Annex-B stream.
|
||||
fn to_avcc(data: &[u8]) -> (Vec<u8>, Vec<Vec<u8>>) {
|
||||
let mut sps = Vec::new();
|
||||
let mut pps = Vec::new();
|
||||
let mut samples = Vec::new();
|
||||
for au in common::split_access_units(data) {
|
||||
let mut sample = Vec::new();
|
||||
for nal in annexb_nals(au) {
|
||||
match nal[0] & 0x1f {
|
||||
7 => sps.push(nal.to_vec()),
|
||||
8 => pps.push(nal.to_vec()),
|
||||
_ => {
|
||||
sample.extend((nal.len() as u32).to_be_bytes());
|
||||
sample.extend_from_slice(nal);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !sample.is_empty() {
|
||||
samples.push(sample);
|
||||
}
|
||||
}
|
||||
sps.dedup();
|
||||
pps.dedup();
|
||||
let s0 = &sps[0];
|
||||
let mut avcc = vec![1, s0[1], s0[2], s0[3], 0xff, 0xe0 | sps.len() as u8];
|
||||
for s in &sps {
|
||||
avcc.extend((s.len() as u16).to_be_bytes());
|
||||
avcc.extend_from_slice(s);
|
||||
}
|
||||
avcc.push(pps.len() as u8);
|
||||
for p in &pps {
|
||||
avcc.extend((p.len() as u16).to_be_bytes());
|
||||
avcc.extend_from_slice(p);
|
||||
}
|
||||
(avcc, samples)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn avcc_input_matches_reference() {
|
||||
for name in ["main_cabac_b", "baseline_qcif"] {
|
||||
let f = common::fixture(name);
|
||||
let Some((h264, yuv)) = common::ensure(f) else { return };
|
||||
let (avcc, samples) = to_avcc(&std::fs::read(h264).unwrap());
|
||||
let mut dec = Decoder::from_avcc(&avcc).unwrap();
|
||||
assert_eq!(dec.nal_length_size(), Some(4));
|
||||
let mut pics = Vec::new();
|
||||
for (i, s) in samples.iter().enumerate() {
|
||||
pics.extend(dec.decode(s, i as i64 * 1000).unwrap());
|
||||
}
|
||||
pics.extend(dec.flush());
|
||||
let reference = std::fs::read(yuv).unwrap();
|
||||
common::compare(&pics, &reference, f.width as usize, f.height as usize).unwrap();
|
||||
// pts are passed through reordering
|
||||
let mut pts: Vec<i64> = pics.iter().map(|p| p.pts).collect();
|
||||
pts.sort();
|
||||
assert_eq!(pts, (0..samples.len() as i64).map(|i| i * 1000).collect::<Vec<_>>());
|
||||
let p0 = &pics[0];
|
||||
assert!(p0.key);
|
||||
assert_eq!((p0.y_stride, p0.uv_stride), (f.width as usize, f.width as usize / 2));
|
||||
}
|
||||
}
|
||||
|
||||
fn xorshift(seed: &mut u64) -> u64 {
|
||||
*seed ^= *seed << 13;
|
||||
*seed ^= *seed >> 7;
|
||||
*seed ^= *seed << 17;
|
||||
*seed
|
||||
}
|
||||
|
||||
/// Damaged streams must never panic or hang; errors are fine.
|
||||
#[test]
|
||||
fn corrupted_streams_do_not_panic() {
|
||||
for name in ["main_cabac_b", "baseline_cif_noise", "cavlc_b", "bpyramid"] {
|
||||
let f = common::fixture(name);
|
||||
let Some((h264, _)) = common::ensure(f) else { return };
|
||||
let data = std::fs::read(h264).unwrap();
|
||||
let mut seed = 0x1234_5678_9abc_def0u64 ^ name.len() as u64;
|
||||
for round in 0..12 {
|
||||
let mut d = data.clone();
|
||||
// flip bytes after the parameter sets
|
||||
for _ in 0..(1 + round * 3) {
|
||||
let pos = 64 + (xorshift(&mut seed) as usize) % (d.len() - 64);
|
||||
d[pos] ^= 1 << (xorshift(&mut seed) % 8);
|
||||
}
|
||||
if round % 4 == 3 {
|
||||
d.truncate(d.len() * 2 / 3);
|
||||
}
|
||||
for threads in [1, 4] {
|
||||
let mut dec = Decoder::with_threads(threads);
|
||||
for au in common::split_access_units(&d) {
|
||||
let _ = dec.decode(au, 0);
|
||||
}
|
||||
let _ = dec.flush();
|
||||
let _ = dec.take_error();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn garbage_input_is_rejected_gracefully() {
|
||||
let mut seed = 42u64;
|
||||
for len in [0usize, 1, 5, 100, 5000] {
|
||||
let mut g: Vec<u8> = (0..len).map(|_| xorshift(&mut seed) as u8).collect();
|
||||
if len > 10 {
|
||||
g[..4].copy_from_slice(&[0, 0, 0, 1]);
|
||||
}
|
||||
let mut dec = Decoder::new();
|
||||
let _ = dec.decode(&g, 0);
|
||||
let _ = dec.flush();
|
||||
}
|
||||
assert!(Decoder::from_avcc(&[1, 2, 3]).is_err());
|
||||
}
|
||||
|
||||
fn is_idr(au: &[u8]) -> bool {
|
||||
annexb_nals(au).iter().any(|n| n.first().is_some_and(|h| h & 0x1f == 5))
|
||||
}
|
||||
|
||||
fn decode_all(dec: &mut Decoder, aus: &[&[u8]], first_pts: usize) -> Vec<craft_video::h264::decode::Picture> {
|
||||
let mut pics = Vec::new();
|
||||
for (i, au) in aus.iter().enumerate() {
|
||||
pics.extend(dec.decode(au, (first_pts + i) as i64).unwrap());
|
||||
}
|
||||
pics.extend(dec.flush());
|
||||
assert_eq!(dec.take_error(), None);
|
||||
pics
|
||||
}
|
||||
|
||||
fn assert_same(a: &[craft_video::h264::decode::Picture], b: &[craft_video::h264::decode::Picture]) {
|
||||
assert_eq!(a.len(), b.len());
|
||||
for (p, q) in a.iter().zip(b) {
|
||||
assert_eq!((p.pts, p.poc, p.key, p.width, p.height), (q.pts, q.poc, q.key, q.width, q.height));
|
||||
assert!(p.y == q.y && p.u == q.u && p.v == q.v, "picture {} differs", p.pts);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_mid_stream_matches_a_new_decoder() {
|
||||
for name in ["keyint10", "open_gop", "slices4"] {
|
||||
let f = common::fixture(name);
|
||||
let Some((h264, _)) = common::ensure(f) else { return };
|
||||
let data = std::fs::read(h264).unwrap();
|
||||
let aus = common::split_access_units(&data);
|
||||
let idrs: Vec<usize> = (0..aus.len()).filter(|&i| is_idr(aus[i])).collect();
|
||||
for (threads, draft) in [(1, false), (3, false), (8, false), (8, true)] {
|
||||
for stop in [1, 4, 9, 13, 22].map(|s| s.min(aus.len())) {
|
||||
// seek to the next IDR, or back to the start
|
||||
let next = idrs.iter().copied().find(|&i| i >= stop).unwrap_or(0);
|
||||
let mut dec = Decoder::with_threads(threads);
|
||||
dec.set_draft(draft);
|
||||
for (i, au) in aus[..stop].iter().enumerate() {
|
||||
let _ = dec.decode(au, i as i64);
|
||||
}
|
||||
dec.reset();
|
||||
assert_eq!(dec.draft(), draft);
|
||||
let got = decode_all(&mut dec, &aus[next..], next);
|
||||
let mut fresh = Decoder::with_threads(threads);
|
||||
fresh.set_draft(draft);
|
||||
let want = decode_all(&mut fresh, &aus[next..], next);
|
||||
assert!(want.len() >= aus.len() - next - 1);
|
||||
assert_same(&got, &want);
|
||||
}
|
||||
}
|
||||
// length-prefixed input: the avcC configuration is reapplied after the reset
|
||||
let (avcc, samples) = to_avcc(&data);
|
||||
let samples: Vec<&[u8]> = samples.iter().map(|s| s.as_slice()).collect();
|
||||
let mut dec = Decoder::from_avcc(&avcc).unwrap();
|
||||
for s in &samples[..7] {
|
||||
let _ = dec.decode(s, 0);
|
||||
}
|
||||
dec.reset();
|
||||
assert_eq!(dec.nal_length_size(), None);
|
||||
dec.configure_avcc(&avcc).unwrap();
|
||||
let got = decode_all(&mut dec, &samples, 0);
|
||||
let want = decode_all(&mut Decoder::from_avcc(&avcc).unwrap(), &samples, 0);
|
||||
assert_same(&got, &want);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rapid_resets_do_not_hang() {
|
||||
let f = common::fixture("bench_1080p");
|
||||
let Some((h264, _)) = common::ensure(f) else { return };
|
||||
let data = std::fs::read(h264).unwrap();
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let aus = common::split_access_units(&data);
|
||||
let mut seed = 7u64;
|
||||
for threads in [2, 8, 16] {
|
||||
let mut dec = Decoder::with_threads(threads);
|
||||
for round in 0..40 {
|
||||
// mostly from the IDR (pictures in flight at the reset), sometimes from a random point
|
||||
let start = if round % 4 == 3 { xorshift(&mut seed) as usize % aus.len() } else { 0 };
|
||||
let n = 1 + xorshift(&mut seed) as usize % 12;
|
||||
for au in aus.iter().skip(start).take(n) {
|
||||
let _ = dec.decode(au, 0);
|
||||
}
|
||||
dec.reset();
|
||||
}
|
||||
let got = decode_all(&mut dec, &aus, 0);
|
||||
let want = decode_all(&mut Decoder::with_threads(threads), &aus, 0);
|
||||
assert_same(&got, &want);
|
||||
}
|
||||
tx.send(()).unwrap();
|
||||
});
|
||||
rx.recv_timeout(std::time::Duration::from_secs(300)).expect("decoder hung after reset");
|
||||
}
|
||||
Reference in New Issue
Block a user