The forward transform skips an all-zero residual, and the quantizer stops dividing per coefficient
Browse files at this commiteca6458f5f3442628c7548ead4607a395e9586afParent: 675eebb- perf
- the forward transform skips an all-zero residual, and the quantizer stops dividing per coefficient
1 changed file +34 -13
- crates/ec-av1/src/transform.rs +34 -13
crates/ec-av1/src/transform.rsmodified+34 -13
| @@ -1463,8 +1463,19 @@ pub fn forward_transform_2d_typed(residual: &[i32], side: usize, tx_type: TxType | |||
| 1463 | let (row_kind, col_kind) = tx_type.axes(); | 1463 | let (row_kind, col_kind) = tx_type.axes(); |
| 1464 | let (hb, vb) = (axis_basis(side, row_kind), axis_basis(side, col_kind)); | 1464 | let (hb, vb) = (axis_basis(side, row_kind), axis_basis(side, col_kind)); |
| 1465 | let mut rows = vec![0.0f64; side * side]; | 1465 | let mut rows = vec![0.0f64; side * side]; |
| 1466 | + // Zero in, zero out, and `rows` is already zero here -- the skip | ||
| 1467 | + // [`forward_rows`] takes for the same reason, which this path was | ||
| 1468 | + // missing: a census of the BD gate's own ladder (lane-av1fwd) found 68% | ||
| 1469 | + // of the 1.9M calls here carry an all-zero residual, and another 6.5M | ||
| 1470 | + // individual rows of the rest are zero. A row of zeros contributes only | ||
| 1471 | + // `0.0 * b` terms, and a sum of zeros is `+0.0`, so this is exact. | ||
| 1472 | + let mut any = false; | ||
| 1466 | for i in 0..side { | 1473 | for i in 0..side { |
| 1467 | let source = &residual[i * side..][..side]; | 1474 | let source = &residual[i * side..][..side]; |
| 1475 | + if source.iter().all(|&r| r == 0) { | ||
| 1476 | + continue; | ||
| 1477 | + } | ||
| 1478 | + any = true; | ||
| 1468 | for v in 0..side { | 1479 | for v in 0..side { |
| 1469 | rows[i * side + v] = source | 1480 | rows[i * side + v] = source |
| 1470 | .iter() | 1481 | .iter() |
| @@ -1473,6 +1484,9 @@ pub fn forward_transform_2d_typed(residual: &[i32], side: usize, tx_type: TxType | |||
| 1473 | .sum(); | 1484 | .sum(); |
| 1474 | } | 1485 | } |
| 1475 | } | 1486 | } |
| 1487 | + if !any { | ||
| 1488 | + return rows; | ||
| 1489 | + } | ||
| 1476 | let mut out = vec![0.0f64; side * side]; | 1490 | let mut out = vec![0.0f64; side * side]; |
| 1477 | for u in 0..side { | 1491 | for u in 0..side { |
| 1478 | let column = &vb[u * side..][..side]; | 1492 | let column = &vb[u * side..][..side]; |
| @@ -1914,20 +1928,20 @@ pub fn quantize(coeffs: &[f64], side: usize, bit_depth: u8, q_idx: i32, deadzone | |||
| 1914 | let dc = f64::from(crate::quant::dc_q(bit_depth, q_idx)); | 1928 | let dc = f64::from(crate::quant::dc_q(bit_depth, q_idx)); |
| 1915 | let ac = f64::from(crate::quant::ac_q(bit_depth, q_idx)); | 1929 | let ac = f64::from(crate::quant::ac_q(bit_depth, q_idx)); |
| 1916 | let mut levels = vec![0i32; side * side]; | 1930 | let mut levels = vec![0i32; side * side]; |
| 1917 | - for (i, &c) in coeffs.iter().enumerate() { | 1931 | + let coded = side.min(32); |
| 1918 | - let (row, col) = (i / side, i % side); | 1932 | + for row in 0..coded { |
| 1919 | - if row >= 32 || col >= 32 { | 1933 | + for col in 0..coded { |
| 1920 | - continue; | 1934 | + let i = row * side + col; |
| 1935 | + let q = if i == 0 { dc } else { ac }; | ||
| 1936 | + let scaled = coeffs[i] / q; | ||
| 1937 | + let magnitude = scaled.abs() + deadzone; | ||
| 1938 | + let level = if magnitude < 1.0 { | ||
| 1939 | + 0 | ||
| 1940 | + } else { | ||
| 1941 | + magnitude.floor().min(f64::from(i32::MAX)) as i32 | ||
| 1942 | + }; | ||
| 1943 | + levels[i] = if scaled < 0.0 { -level } else { level }; | ||
| 1921 | } | 1944 | } |
| 1922 | - let q = if i == 0 { dc } else { ac }; | ||
| 1923 | - let scaled = c / q; | ||
| 1924 | - let magnitude = scaled.abs() + deadzone; | ||
| 1925 | - let level = if magnitude < 1.0 { | ||
| 1926 | - 0 | ||
| 1927 | - } else { | ||
| 1928 | - magnitude.floor().min(f64::from(i32::MAX)) as i32 | ||
| 1929 | - }; | ||
| 1930 | - levels[i] = if scaled < 0.0 { -level } else { level }; | ||
| 1931 | } | 1945 | } |
| 1932 | levels | 1946 | levels |
| 1933 | } | 1947 | } |
| @@ -1941,6 +1955,13 @@ pub fn forward_and_quantize( | |||
| 1941 | q_idx: i32, | 1955 | q_idx: i32, |
| 1942 | deadzone: f64, | 1956 | deadzone: f64, |
| 1943 | ) -> Vec<i32> { | 1957 | ) -> Vec<i32> { |
| 1958 | + // 72% of this encoder's forward DCTs are of an all-zero residual | ||
| 1959 | + // (measured over the BD gate's own ladder, lane-av1fwd): every row and | ||
| 1960 | + // every column of the network is skipped for them anyway, so the levels | ||
| 1961 | + // are zero without allocating the row pass's buffer at all. | ||
| 1962 | + if residual.iter().all(|&r| r == 0) { | ||
| 1963 | + return vec![0i32; side * side]; | ||
| 1964 | + } | ||
| 1944 | // Fused: the levels are formed straight off the fixed-point coefficients, | 1965 | // Fused: the levels are formed straight off the fixed-point coefficients, |
| 1945 | // so neither the `Vec<f64>` of coefficients nor the pass that reads it | 1966 | // so neither the `Vec<f64>` of coefficients nor the pass that reads it |
| 1946 | // exists. | 1967 | // exists. |