Film grain panicked on a zero-row overlap block at the ragged tail
Browse files at this commit6b9fb3c173274b2839c749ca4a72018fae0497dcParent: f89b1a2- fix
- film grain panicked on a zero-row overlap block at the ragged tail
1 changed file +264 -0
- crates/ec-av1/src/film_grain.rs +264 -0
crates/ec-av1/src/film_grain.rsmodified+264 -0
| @@ -336,6 +336,16 @@ fn add_noise_to_block( | |||
| 336 | half_h: i32, | 336 | half_h: i32, |
| 337 | half_w: i32, fctx: &crate::decode::FrameCtx, | 337 | half_w: i32, fctx: &crate::decode::FrameCtx, |
| 338 | ) { | 338 | ) { |
| 339 | + // spec 7.18.3.5 runs the block's noise loops `for i in 0..h`/`for j in | ||
| 340 | + // 0..w`, so a block with no rows (or no columns) writes nothing. The | ||
| 341 | + // ragged tail of a picture whose chroma plane height is `16k + 1` gives | ||
| 342 | + // `half_h == 0` under `overlap_flag` -- the single tail row IS the | ||
| 343 | + // overlap row, already written by the line-buffer call -- and the row | ||
| 344 | + // base `py0 * y_stride` then addresses one row past the plane's end. | ||
| 345 | + // Iterating zero times is the spec's answer; slicing it would panic. | ||
| 346 | + if half_h <= 0 || half_w <= 0 { | ||
| 347 | + return; | ||
| 348 | + } | ||
| 339 | let y_stride = picture.width as i32; | 349 | let y_stride = picture.width as i32; |
| 340 | let c_stride = (picture.width / 2) as i32; | 350 | let c_stride = (picture.width / 2) as i32; |
| 341 | let scaling_shift = fg.grain_scaling_minus_8 as i32 + 8; | 351 | let scaling_shift = fg.grain_scaling_minus_8 as i32 + 8; |
| @@ -1743,3 +1753,257 @@ mod grain2_tests { | |||
| 1743 | assert_eq!(out2.y.len(), w * h, "the ragged tail row is present"); | 1753 | assert_eq!(out2.y.len(), w * h, "the ragged tail row is present"); |
| 1744 | } | 1754 | } |
| 1745 | } | 1755 | } |
| 1756 | + | ||
| 1757 | +#[cfg(test)] | ||
| 1758 | +mod ragged_grain_tests { | ||
| 1759 | + use super::*; | ||
| 1760 | + | ||
| 1761 | + fn round2(x: i32, n: i32) -> i32 { | ||
| 1762 | + (x + (1 << (n - 1))) >> n | ||
| 1763 | + } | ||
| 1764 | + | ||
| 1765 | + /// One 17-row chroma stripe block of spec 7.18.3.4's `add_noise_stripe`. | ||
| 1766 | + fn chroma_stripe_block( | ||
| 1767 | + st: &mut [i32], | ||
| 1768 | + tpl: &[i32], | ||
| 1769 | + coy: i32, | ||
| 1770 | + cox: i32, | ||
| 1771 | + x: i32, | ||
| 1772 | + cw: i32, | ||
| 1773 | + overlap: bool, | ||
| 1774 | + gmin: i32, | ||
| 1775 | + gmax: i32, | ||
| 1776 | + ) { | ||
| 1777 | + for i in 0..17 { | ||
| 1778 | + for j in 0..17 { | ||
| 1779 | + let col = x + j; | ||
| 1780 | + let mut g = tpl[((coy + i) * CHROMA_BLOCK_W + cox + j) as usize]; | ||
| 1781 | + if overlap && x > 0 && j < 1 { | ||
| 1782 | + let old = st[(i * cw + col) as usize]; | ||
| 1783 | + g = round2(old * 23 + g * 22, 5).clamp(gmin, gmax); | ||
| 1784 | + } | ||
| 1785 | + if col < cw { | ||
| 1786 | + st[(i * cw + col) as usize] = g; | ||
| 1787 | + } | ||
| 1788 | + } | ||
| 1789 | + } | ||
| 1790 | + } | ||
| 1791 | + | ||
| 1792 | + /// A straightforward transcription of spec 7.18.3.4 (`add_noise_stripe`) | ||
| 1793 | + /// and 7.18.3.5 (stripe-to-image vertical overlap), independent of the | ||
| 1794 | + /// fused col/line-buffer walk in [`apply_grain`]: it builds the whole | ||
| 1795 | + /// noise image first, then hands each row to the crate's per-sample | ||
| 1796 | + /// pixel kernels (which are not what this test exercises). 8-bit, 4:2:0, | ||
| 1797 | + /// `clip_to_restricted_range == false`. | ||
| 1798 | + fn reference(src: &Picture, fg: &FilmGrainParams, fctx: &crate::decode::FrameCtx) -> Picture { | ||
| 1799 | + let w = src.width as i32; | ||
| 1800 | + let h = src.height as i32; | ||
| 1801 | + let cw = w / 2; | ||
| 1802 | + let ch = h / 2; | ||
| 1803 | + let (gmin, gmax) = grain_range(fctx); | ||
| 1804 | + let overlap = fg.overlap_flag; | ||
| 1805 | + | ||
| 1806 | + let mut rng = Rng::raw(fg.grain_seed); | ||
| 1807 | + let pl = build_pred_pos(fg.ar_coeff_lag as i32, false); | ||
| 1808 | + let pc = build_pred_pos(fg.ar_coeff_lag as i32, fg.num_y_points > 0); | ||
| 1809 | + let lt = generate_luma_grain_block(fg, &pl, &mut rng, fctx); | ||
| 1810 | + let (cbt, crt) = generate_chroma_grain_blocks(fg, &pc, <, fg.grain_seed, fctx); | ||
| 1811 | + | ||
| 1812 | + let mut nl = vec![0i32; (w * h) as usize]; | ||
| 1813 | + let mut ncb = vec![0i32; (cw * ch) as usize]; | ||
| 1814 | + let mut ncr = vec![0i32; (cw * ch) as usize]; | ||
| 1815 | + let mut prev: Option<(Vec<i32>, Vec<i32>, Vec<i32>)> = None; | ||
| 1816 | + let mut y = 0; | ||
| 1817 | + while y < ch { | ||
| 1818 | + let mut row_rng = Rng::line(y * 2, fg.grain_seed); | ||
| 1819 | + let mut sl = vec![0i32; (34 * w) as usize]; | ||
| 1820 | + let mut scb = vec![0i32; (17 * cw) as usize]; | ||
| 1821 | + let mut scr = vec![0i32; (17 * cw) as usize]; | ||
| 1822 | + let mut x = 0; | ||
| 1823 | + while x < cw { | ||
| 1824 | + let raw = row_rng.get(8); | ||
| 1825 | + let ox = (raw >> 4) & 15; | ||
| 1826 | + let oy = raw & 15; | ||
| 1827 | + let (loy, lox) = (9 + 2 * oy, 9 + 2 * ox); | ||
| 1828 | + let (coy, cox) = (6 + oy, 6 + ox); | ||
| 1829 | + for i in 0..34 { | ||
| 1830 | + for j in 0..34 { | ||
| 1831 | + let col = x * 2 + j; | ||
| 1832 | + let mut g = lt[((loy + i) * LUMA_BLOCK_W + lox + j) as usize]; | ||
| 1833 | + if overlap && x > 0 && j < 2 { | ||
| 1834 | + let old = sl[(i * w + col) as usize]; | ||
| 1835 | + g = if j == 0 { old * 27 + g * 17 } else { old * 17 + g * 27 }; | ||
| 1836 | + g = round2(g, 5).clamp(gmin, gmax); | ||
| 1837 | + } | ||
| 1838 | + if col < w { | ||
| 1839 | + sl[(i * w + col) as usize] = g; | ||
| 1840 | + } | ||
| 1841 | + } | ||
| 1842 | + } | ||
| 1843 | + chroma_stripe_block(&mut scb, &cbt, coy, cox, x, cw, overlap, gmin, gmax); | ||
| 1844 | + chroma_stripe_block(&mut scr, &crt, coy, cox, x, cw, overlap, gmin, gmax); | ||
| 1845 | + x += 16; | ||
| 1846 | + } | ||
| 1847 | + for i in 0..32 { | ||
| 1848 | + let row = y * 2 + i; | ||
| 1849 | + if row >= h { | ||
| 1850 | + break; | ||
| 1851 | + } | ||
| 1852 | + for col in 0..w { | ||
| 1853 | + let mut g = sl[(i * w + col) as usize]; | ||
| 1854 | + if overlap && i < 2 { | ||
| 1855 | + if let Some((p, _, _)) = &prev { | ||
| 1856 | + let old = p[((i + 32) * w + col) as usize]; | ||
| 1857 | + g = if i == 0 { old * 27 + g * 17 } else { old * 17 + g * 27 }; | ||
| 1858 | + g = round2(g, 5).clamp(gmin, gmax); | ||
| 1859 | + } | ||
| 1860 | + } | ||
| 1861 | + nl[(row * w + col) as usize] = g; | ||
| 1862 | + } | ||
| 1863 | + } | ||
| 1864 | + for i in 0..16 { | ||
| 1865 | + let row = y + i; | ||
| 1866 | + if row >= ch { | ||
| 1867 | + break; | ||
| 1868 | + } | ||
| 1869 | + for col in 0..cw { | ||
| 1870 | + for (st, img) in [(&scb, &mut ncb), (&scr, &mut ncr)] { | ||
| 1871 | + let mut g = st[(i * cw + col) as usize]; | ||
| 1872 | + if overlap && i < 1 { | ||
| 1873 | + let old = match &prev { | ||
| 1874 | + Some((_, pb, pr)) => { | ||
| 1875 | + let p = if std::ptr::eq(st.as_ptr(), scb.as_ptr()) { pb } else { pr }; | ||
| 1876 | + Some(p[((i + 16) * cw + col) as usize]) | ||
| 1877 | + } | ||
| 1878 | + None => None, | ||
| 1879 | + }; | ||
| 1880 | + if let Some(old) = old { | ||
| 1881 | + g = round2(old * 23 + g * 22, 5).clamp(gmin, gmax); | ||
| 1882 | + } | ||
| 1883 | + } | ||
| 1884 | + img[(row * cw + col) as usize] = g; | ||
| 1885 | + } | ||
| 1886 | + } | ||
| 1887 | + } | ||
| 1888 | + prev = Some((sl, scb, scr)); | ||
| 1889 | + y += 16; | ||
| 1890 | + } | ||
| 1891 | + | ||
| 1892 | + let mut out = src.clone(); | ||
| 1893 | + let sy = expand_scaling_lut( | ||
| 1894 | + &build_scaling_lut(&fg.point_y_value, &fg.point_y_scaling, fg.num_y_points as usize), | ||
| 1895 | + 8, | ||
| 1896 | + ); | ||
| 1897 | + let s_cb = expand_scaling_lut( | ||
| 1898 | + &build_scaling_lut(&fg.point_cb_value, &fg.point_cb_scaling, fg.num_cb_points as usize), | ||
| 1899 | + 8, | ||
| 1900 | + ); | ||
| 1901 | + let s_cr = expand_scaling_lut( | ||
| 1902 | + &build_scaling_lut(&fg.point_cr_value, &fg.point_cr_scaling, fg.num_cr_points as usize), | ||
| 1903 | + 8, | ||
| 1904 | + ); | ||
| 1905 | + let scaling_shift = fg.grain_scaling_minus_8 as i32 + 8; | ||
| 1906 | + let ro = 1i32 << (scaling_shift - 1); | ||
| 1907 | + let p = ChromaNoise { | ||
| 1908 | + index_max: 255, | ||
| 1909 | + rounding_offset: ro, | ||
| 1910 | + scaling_shift, | ||
| 1911 | + min_chroma: 0, | ||
| 1912 | + max_chroma: 255, | ||
| 1913 | + }; | ||
| 1914 | + // Chroma first, and out of the CLEAN luma: `add_noise_to_block` adds | ||
| 1915 | + // the block's chroma noise before its luma noise, so no chroma | ||
| 1916 | + // sample ever sees a noised luma neighbour. | ||
| 1917 | + for r in 0..ch { | ||
| 1918 | + let (cs, ce) = ((r * cw) as usize, ((r + 1) * cw) as usize); | ||
| 1919 | + let (ls, le) = ((2 * r * w) as usize, (2 * r * w + w) as usize); | ||
| 1920 | + if fg.num_cb_points > 0 { | ||
| 1921 | + chroma_noise_row_scalar( | ||
| 1922 | + &mut out.u[cs..ce], | ||
| 1923 | + &src.y[ls..le], | ||
| 1924 | + &ncb[cs..ce], | ||
| 1925 | + &s_cb, | ||
| 1926 | + fg.cb_luma_mult as i32 - 128, | ||
| 1927 | + fg.cb_mult as i32 - 128, | ||
| 1928 | + fg.cb_offset as i32 - 256, | ||
| 1929 | + p, | ||
| 1930 | + ); | ||
| 1931 | + } | ||
| 1932 | + if fg.num_cr_points > 0 { | ||
| 1933 | + chroma_noise_row_scalar( | ||
| 1934 | + &mut out.v[cs..ce], | ||
| 1935 | + &src.y[ls..le], | ||
| 1936 | + &ncr[cs..ce], | ||
| 1937 | + &s_cr, | ||
| 1938 | + fg.cr_luma_mult as i32 - 128, | ||
| 1939 | + fg.cr_mult as i32 - 128, | ||
| 1940 | + fg.cr_offset as i32 - 256, | ||
| 1941 | + p, | ||
| 1942 | + ); | ||
| 1943 | + } | ||
| 1944 | + } | ||
| 1945 | + if fg.num_y_points > 0 { | ||
| 1946 | + for r in 0..h { | ||
| 1947 | + let (ls, le) = ((r * w) as usize, (r * w + w) as usize); | ||
| 1948 | + luma_noise_row_scalar(&mut out.y[ls..le], &nl[ls..le], &sy, ro, scaling_shift, 0, 255); | ||
| 1949 | + } | ||
| 1950 | + } | ||
| 1951 | + out | ||
| 1952 | + } | ||
| 1953 | + | ||
| 1954 | + fn params() -> FilmGrainParams { | ||
| 1955 | + let mut fg = FilmGrainParams { | ||
| 1956 | + apply_grain: true, | ||
| 1957 | + grain_seed: 0x51a7, | ||
| 1958 | + overlap_flag: true, | ||
| 1959 | + ar_coeff_lag: 2, | ||
| 1960 | + ar_coeff_shift_minus_6: 1, | ||
| 1961 | + num_y_points: 2, | ||
| 1962 | + num_cb_points: 2, | ||
| 1963 | + num_cr_points: 2, | ||
| 1964 | + cb_mult: 128, | ||
| 1965 | + cb_luma_mult: 192, | ||
| 1966 | + cb_offset: 256, | ||
| 1967 | + cr_mult: 128, | ||
| 1968 | + cr_luma_mult: 192, | ||
| 1969 | + cr_offset: 256, | ||
| 1970 | + ..Default::default() | ||
| 1971 | + }; | ||
| 1972 | + fg.point_y_value[..2].copy_from_slice(&[0, 255]); | ||
| 1973 | + fg.point_y_scaling[..2].copy_from_slice(&[50, 70]); | ||
| 1974 | + fg.point_cb_value[..2].copy_from_slice(&[0, 255]); | ||
| 1975 | + fg.point_cb_scaling[..2].copy_from_slice(&[40, 60]); | ||
| 1976 | + fg.point_cr_value[..2].copy_from_slice(&[0, 255]); | ||
| 1977 | + fg.point_cr_scaling[..2].copy_from_slice(&[45, 65]); | ||
| 1978 | + fg | ||
| 1979 | + } | ||
| 1980 | + | ||
| 1981 | + /// A chroma plane height of `16k + 1` (luma height `32k + 2`) makes the | ||
| 1982 | + /// last block row's `half_h` zero under `overlap_flag`: the single tail | ||
| 1983 | + /// row is the overlap row itself. That used to slice the luma plane one | ||
| 1984 | + /// row past its end ("range start index out of range"). The ragged width | ||
| 1985 | + /// twin (`16k + 1` chroma columns) and a taller case are pinned with it, | ||
| 1986 | + /// and 48x100 is the control that proves the reference on a size the | ||
| 1987 | + /// decoder already handled. | ||
| 1988 | + #[test] | ||
| 1989 | + fn ragged_overlap_tail_matches_spec_reference() { | ||
| 1990 | + let fctx = crate::decode::FrameCtx::new(); | ||
| 1991 | + fctx.grain_bit_depth.with(|c| c.set(8)); | ||
| 1992 | + let fg = params(); | ||
| 1993 | + for &(w, h) in &[(64usize, 66usize), (66, 64), (64, 98), (48, 100)] { | ||
| 1994 | + let src = Picture { | ||
| 1995 | + width: w, | ||
| 1996 | + height: h, | ||
| 1997 | + y: (0..w * h).map(|i| ((i * 7 + 3) % 256) as u16).collect(), | ||
| 1998 | + u: (0..w / 2 * (h / 2)).map(|i| ((i * 11 + 5) % 256) as u16).collect(), | ||
| 1999 | + v: (0..w / 2 * (h / 2)).map(|i| ((i * 13 + 9) % 256) as u16).collect(), | ||
| 2000 | + }; | ||
| 2001 | + let got = apply_grain(&src, &fg, false, 8, &fctx); | ||
| 2002 | + let want = reference(&src, &fg, &fctx); | ||
| 2003 | + assert_eq!(got.y, want.y, "{w}x{h} luma"); | ||
| 2004 | + assert_eq!(got.u, want.u, "{w}x{h} cb"); | ||
| 2005 | + assert_eq!(got.v, want.v, "{w}x{h} cr"); | ||
| 2006 | + assert_ne!(got.y, src.y, "{w}x{h} grain actually landed"); | ||
| 2007 | + } | ||
| 2008 | + } | ||
| 2009 | +} | ||