The Golomb tail carries levels the base range cannot reach
Browse files at this commit0eba7705ffda46bd2a87bcbde8f116f9fd8faf4cParent: 7f9b390- feat
- the Golomb tail carries levels the base range cannot reach
2 changed files +144 -10
- crates/ec-av1/src/msac.rs +4 -4
- crates/ec-av1/src/tile.rs +140 -6
crates/ec-av1/src/msac.rsmodified+4 -4
| @@ -204,13 +204,13 @@ fn update_cdf(cdf: &mut [u16], symbol: usize) { | |||
| 204 | } | 204 | } |
| 205 | 205 | ||
| 206 | #[cfg(test)] | 206 | #[cfg(test)] |
| 207 | -mod tests { | 207 | +pub(crate) mod tests { |
| 208 | use super::*; | 208 | use super::*; |
| 209 | 209 | ||
| 210 | /// The decoding process of spec 8.2, written from the specification's | 210 | /// The decoding process of spec 8.2, written from the specification's |
| 211 | /// pseudocode so that the roundtrip tests measure the encoder against the | 211 | /// pseudocode so that the roundtrip tests measure the encoder against the |
| 212 | /// decoder AV1 defines rather than against the encoder's own arithmetic. | 212 | /// decoder AV1 defines rather than against the encoder's own arithmetic. |
| 213 | - struct SymbolDecoder<'a> { | 213 | + pub(crate) struct SymbolDecoder<'a> { |
| 214 | data: &'a [u8], | 214 | data: &'a [u8], |
| 215 | /// Next bit to read, as a bit offset into `data`. | 215 | /// Next bit to read, as a bit offset into `data`. |
| 216 | bit: usize, | 216 | bit: usize, |
| @@ -223,7 +223,7 @@ mod tests { | |||
| 223 | 223 | ||
| 224 | impl<'a> SymbolDecoder<'a> { | 224 | impl<'a> SymbolDecoder<'a> { |
| 225 | /// `init_symbol` (spec 8.2.2). | 225 | /// `init_symbol` (spec 8.2.2). |
| 226 | - fn new(data: &'a [u8]) -> SymbolDecoder<'a> { | 226 | + pub(crate) fn new(data: &'a [u8]) -> SymbolDecoder<'a> { |
| 227 | let mut d = SymbolDecoder { | 227 | let mut d = SymbolDecoder { |
| 228 | data, | 228 | data, |
| 229 | bit: 0, | 229 | bit: 0, |
| @@ -285,7 +285,7 @@ mod tests { | |||
| 285 | symbol | 285 | symbol |
| 286 | } | 286 | } |
| 287 | 287 | ||
| 288 | - fn literal(&mut self, bits: u32) -> u32 { | 288 | + pub(crate) fn literal(&mut self, bits: u32) -> u32 { |
| 289 | let mut v = 0; | 289 | let mut v = 0; |
| 290 | for _ in 0..bits { | 290 | for _ in 0..bits { |
| 291 | v = (v << 1) | self.symbol_fixed(&EQUIPROBABLE) as u32; | 291 | v = (v << 1) | self.symbol_fixed(&EQUIPROBABLE) as u32; |
crates/ec-av1/src/tile.rsmodified+140 -6
| @@ -68,7 +68,12 @@ const COEFF_BASE_RANGE: i32 = 12; | |||
| 68 | /// carries. | 68 | /// carries. |
| 69 | const BR_STEP: i32 = 3; | 69 | const BR_STEP: i32 = 3; |
| 70 | /// The largest level the base and base-range syntax carry between them. | 70 | /// The largest level the base and base-range syntax carry between them. |
| 71 | -const MAX_LEVEL: i32 = NUM_BASE_LEVELS + COEFF_BASE_RANGE; | 71 | +/// Anything above it is written as a Golomb tail on top of this. |
| 72 | +const MAX_BR_LEVEL: i32 = NUM_BASE_LEVELS + COEFF_BASE_RANGE; | ||
| 73 | +/// The largest level this writer codes. The Golomb tail is a length in unary | ||
| 74 | +/// followed by that many bits, and the spec's decoder reads at most twenty of | ||
| 75 | +/// each, so a level a decoder cannot read back is refused rather than written. | ||
| 76 | +const MAX_LEVEL: i32 = MAX_BR_LEVEL + (1 << 19); | ||
| 72 | /// The q-context band whose default CDFs [`crate::cdf`] carries. | 77 | /// The q-context band whose default CDFs [`crate::cdf`] carries. |
| 73 | const Q_CTX_2: std::ops::RangeInclusive<u8> = 61..=120; | 78 | const Q_CTX_2: std::ops::RangeInclusive<u8> = 61..=120; |
| 74 | 79 | ||
| @@ -610,8 +615,9 @@ fn level_grid(coeffs: &[Coeff], side: usize) -> Result<Vec<i32>> { | |||
| 610 | if coeff.level == 0 || coeff.level.abs() > MAX_LEVEL { | 615 | if coeff.level == 0 || coeff.level.abs() > MAX_LEVEL { |
| 611 | return Err(Error::unsupported( | 616 | return Err(Error::unsupported( |
| 612 | "AV1 tile", | 617 | "AV1 tile", |
| 613 | - "coefficients are written for levels -14..=14 without zero; \ | 618 | + format!( |
| 614 | - wider levels need the Golomb tail", | 619 | + "coefficients are written for levels -{MAX_LEVEL}..={MAX_LEVEL} without zero" |
| 620 | + ), | ||
| 615 | )); | 621 | )); |
| 616 | } | 622 | } |
| 617 | let pos = usize::from(coeff.row) * side + usize::from(coeff.col); | 623 | let pos = usize::from(coeff.row) * side + usize::from(coeff.col); |
| @@ -717,6 +723,11 @@ fn write_coeffs( | |||
| 717 | } else { | 723 | } else { |
| 718 | enc.literal(u32::from(level < 0), 1); | 724 | enc.literal(u32::from(level < 0), 1); |
| 719 | } | 725 | } |
| 726 | + // A level the base and base-range syntax cannot reach carries the rest | ||
| 727 | + // of itself here, after its own sign (spec 5.11.39). | ||
| 728 | + if level.abs() > MAX_BR_LEVEL { | ||
| 729 | + write_golomb(enc, (level.abs() - MAX_BR_LEVEL - 1) as u32); | ||
| 730 | + } | ||
| 720 | } | 731 | } |
| 721 | } | 732 | } |
| 722 | 733 | ||
| @@ -749,6 +760,20 @@ fn write_eob(enc: &mut SymbolEncoder, coding: &TxbCoding, eob: usize) { | |||
| 749 | } | 760 | } |
| 750 | } | 761 | } |
| 751 | 762 | ||
| 763 | +/// Writes the remainder of a level the base and base-range syntax could not | ||
| 764 | +/// reach (spec 5.11.40): the value plus one, as its bit length in unary | ||
| 765 | +/// followed by the value's own bits, most significant first. | ||
| 766 | +pub(crate) fn write_golomb(enc: &mut SymbolEncoder, value: u32) { | ||
| 767 | + let x = value + 1; | ||
| 768 | + let length = 32 - x.leading_zeros(); | ||
| 769 | + for _ in 0..length - 1 { | ||
| 770 | + enc.literal(0, 1); | ||
| 771 | + } | ||
| 772 | + for i in (0..length).rev() { | ||
| 773 | + enc.literal((x >> i) & 1, 1); | ||
| 774 | + } | ||
| 775 | +} | ||
| 776 | + | ||
| 752 | /// The context of the level of the last coded coefficient (spec 8.3.2): how | 777 | /// The context of the level of the last coded coefficient (spec 8.3.2): how |
| 753 | /// far into the scan it sits, in quarters and eighths of the transform. | 778 | /// far into the scan it sits, in quarters and eighths of the transform. |
| 754 | fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize { | 779 | fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize { |
| @@ -813,7 +838,7 @@ fn check_levels(levels: &[i32], blocks: usize, base_q_idx: u8) -> Result<()> { | |||
| 813 | "a DC-only key frame needs one level per coded block", | 838 | "a DC-only key frame needs one level per coded block", |
| 814 | )); | 839 | )); |
| 815 | } | 840 | } |
| 816 | - if levels.iter().any(|&l| l == 0 || l.abs() > MAX_LEVEL) { | 841 | + if levels.iter().any(|&l| l == 0 || l.abs() > MAX_BR_LEVEL) { |
| 817 | return Err(Error::unsupported( | 842 | return Err(Error::unsupported( |
| 818 | "AV1 tile", | 843 | "AV1 tile", |
| 819 | "a DC-only key frame is written for levels -14..=14 without zero; \ | 844 | "a DC-only key frame is written for levels -14..=14 without zero; \ |
| @@ -1852,6 +1877,115 @@ mod tests { | |||
| 1852 | ); | 1877 | ); |
| 1853 | } | 1878 | } |
| 1854 | 1879 | ||
| 1880 | + /// The mean sample of a decoded block. | ||
| 1881 | + fn mean_of(block: &[Vec<u8>]) -> f64 { | ||
| 1882 | + let sum: i32 = block.iter().flatten().map(|&s| i32::from(s)).sum(); | ||
| 1883 | + f64::from(sum) / (block.len() * block[0].len()) as f64 | ||
| 1884 | + } | ||
| 1885 | + | ||
| 1886 | + /// The top-left block of a frame whose only coefficient is a luma DC of | ||
| 1887 | + /// `level`. | ||
| 1888 | + fn decode_dc_level_block(level: i32) -> Vec<Vec<u8>> { | ||
| 1889 | + let blocks = vec![ | ||
| 1890 | + vec![Coeff { | ||
| 1891 | + row: 0, | ||
| 1892 | + col: 0, | ||
| 1893 | + level, | ||
| 1894 | + }], | ||
| 1895 | + Vec::new(), | ||
| 1896 | + Vec::new(), | ||
| 1897 | + Vec::new(), | ||
| 1898 | + ]; | ||
| 1899 | + top_left_block(&decode_coeff_quadrants(&blocks)) | ||
| 1900 | + } | ||
| 1901 | + | ||
| 1902 | + /// `read_golomb` (spec 5.11.40), written from the specification's | ||
| 1903 | + /// pseudocode, and the level its caller builds from it: a level above the | ||
| 1904 | + /// base-range tail is fifteen plus what the tail carries. | ||
| 1905 | + fn read_golomb_level(dec: &mut crate::msac::tests::SymbolDecoder) -> u32 { | ||
| 1906 | + let mut length = 0; | ||
| 1907 | + while dec.literal(1) == 0 { | ||
| 1908 | + length += 1; | ||
| 1909 | + assert!( | ||
| 1910 | + length < 20, | ||
| 1911 | + "the tail's length prefix runs past twenty bits" | ||
| 1912 | + ); | ||
| 1913 | + } | ||
| 1914 | + let mut x = 1; | ||
| 1915 | + for _ in 0..length { | ||
| 1916 | + x = (x << 1) | dec.literal(1); | ||
| 1917 | + } | ||
| 1918 | + (MAX_BR_LEVEL + 1) as u32 + (x - 1) | ||
| 1919 | + } | ||
| 1920 | + | ||
| 1921 | + /// The tail says how much of the level the base and base-range syntax could | ||
| 1922 | + /// not carry, and the spec's own reader must hand back the level that was | ||
| 1923 | + /// asked for — not one either side of it. A tail written a bit long, a bit | ||
| 1924 | + /// short, or against the wrong base fails this at the first level it | ||
| 1925 | + /// reaches. | ||
| 1926 | + #[test] | ||
| 1927 | + fn the_golomb_tail_reads_back_as_the_level_it_was_written_for() { | ||
| 1928 | + for level in (MAX_BR_LEVEL + 1..=MAX_BR_LEVEL + 300) | ||
| 1929 | + .chain([MAX_LEVEL - 1, MAX_LEVEL]) | ||
| 1930 | + .map(|l| l as u32) | ||
| 1931 | + { | ||
| 1932 | + let mut enc = SymbolEncoder::new(); | ||
| 1933 | + write_golomb(&mut enc, level - (MAX_BR_LEVEL + 1) as u32); | ||
| 1934 | + // The tail is raw bits, so it needs no padding beyond what the | ||
| 1935 | + // encoder's own flush writes. | ||
| 1936 | + let data = enc.finish(); | ||
| 1937 | + let mut dec = crate::msac::tests::SymbolDecoder::new(&data); | ||
| 1938 | + assert_eq!( | ||
| 1939 | + read_golomb_level(&mut dec), | ||
| 1940 | + level, | ||
| 1941 | + "the tail of level {level}" | ||
| 1942 | + ); | ||
| 1943 | + } | ||
| 1944 | + } | ||
| 1945 | + | ||
| 1946 | + /// The base and base-range syntax reach level fourteen between them; | ||
| 1947 | + /// anything above that carries the rest of itself as a Golomb tail, written | ||
| 1948 | + /// after its own sign. A DC level moves the whole block off its mid-grey | ||
| 1949 | + /// prediction by an amount that grows with the level, so a run of levels | ||
| 1950 | + /// either side of the tail's threshold — spaced wide enough that the | ||
| 1951 | + /// quantiser separates them — must decode to a run of steadily | ||
| 1952 | + /// brighter blocks — and the negatives of the same levels to steadily | ||
| 1953 | + /// darker ones. A tail written with the wrong length, in the wrong place in | ||
| 1954 | + /// the syntax, or off by one desyncs the decoder outright. | ||
| 1955 | + #[test] | ||
| 1956 | + fn levels_above_the_base_range_tail_carry_a_golomb_tail() { | ||
| 1957 | + if !have_ffmpeg() { | ||
| 1958 | + eprintln!("SKIP levels_above_the_base_range_tail_carry_a_golomb_tail: no ffmpeg"); | ||
| 1959 | + return; | ||
| 1960 | + } | ||
| 1961 | + let mut brighter = Vec::new(); | ||
| 1962 | + let mut darker = Vec::new(); | ||
| 1963 | + for level in [10, 14, 18, 31, 60, 400] { | ||
| 1964 | + brighter.push((level, mean_of(&decode_dc_level_block(level)))); | ||
| 1965 | + darker.push((level, mean_of(&decode_dc_level_block(-level)))); | ||
| 1966 | + } | ||
| 1967 | + for pair in brighter.windows(2) { | ||
| 1968 | + let [(low, dim), (high, bright)] = [pair[0], pair[1]]; | ||
| 1969 | + assert!( | ||
| 1970 | + bright > dim, | ||
| 1971 | + "level {high} decodes brighter than level {low}, got {bright} against {dim}" | ||
| 1972 | + ); | ||
| 1973 | + } | ||
| 1974 | + for pair in darker.windows(2) { | ||
| 1975 | + let [(low, bright), (high, dim)] = [pair[0], pair[1]]; | ||
| 1976 | + assert!( | ||
| 1977 | + dim < bright, | ||
| 1978 | + "level -{high} decodes darker than level -{low}, got {dim} against {bright}" | ||
| 1979 | + ); | ||
| 1980 | + } | ||
| 1981 | + let (_, mid) = brighter[0]; | ||
| 1982 | + assert!( | ||
| 1983 | + mid > 128.0 && darker[0].1 < 128.0, | ||
| 1984 | + "a positive level brightens its block and a negative one darkens it, got {mid} and {}", | ||
| 1985 | + darker[0].1 | ||
| 1986 | + ); | ||
| 1987 | + } | ||
| 1988 | + | ||
| 1855 | /// A frame need not be a whole number of superblocks. A 96x64 frame is a | 1989 | /// A frame need not be a whole number of superblocks. A 96x64 frame is a |
| 1856 | /// superblock and a half across: the second superblock has no right-hand | 1990 | /// superblock and a half across: the second superblock has no right-hand |
| 1857 | /// half, so its partition is a gathered flag rather than the full symbol, | 1991 | /// half, so its partition is a gathered flag rather than the full symbol, |
| @@ -2016,12 +2150,12 @@ mod tests { | |||
| 2016 | }]; | 2150 | }]; |
| 2017 | b | 2151 | b |
| 2018 | }), | 2152 | }), |
| 2019 | - ("a level past the base-range tail", { | 2153 | + ("a level past the Golomb tail's reach", { |
| 2020 | let mut b = empty(); | 2154 | let mut b = empty(); |
| 2021 | b[2].luma = vec![Coeff { | 2155 | b[2].luma = vec![Coeff { |
| 2022 | row: 0, | 2156 | row: 0, |
| 2023 | col: 0, | 2157 | col: 0, |
| 2024 | - level: 15, | 2158 | + level: MAX_LEVEL + 1, |
| 2025 | }]; | 2159 | }]; |
| 2026 | b | 2160 | b |
| 2027 | }), | 2161 | }), |