Widen intra::predict/Edges to rect (bw != bh)
Browse files at this commit24d69ed629d986a390f6159fa932655ace845f1aParent: 66b5de3- feat
- widen intra::predict/Edges to rect (bw != bh)
3 changed files +132 -59
- crates/ec-av1/src/decode.rs +2 -0
- crates/ec-av1/src/encode.rs +4 -0
- crates/ec-av1/src/intra.rs +126 -59
crates/ec-av1/src/decode.rsmodified+2 -0
| @@ -1823,6 +1823,7 @@ impl PlaneBuf { | |||
| 1823 | left.as_deref(), | 1823 | left.as_deref(), |
| 1824 | corner, | 1824 | corner, |
| 1825 | side, | 1825 | side, |
| 1826 | + side, | ||
| 1826 | enable_edge_filter, | 1827 | enable_edge_filter, |
| 1827 | false, | 1828 | false, |
| 1828 | &mut prediction, | 1829 | &mut prediction, |
| @@ -4689,6 +4690,7 @@ fn interintra_blend( | |||
| 4689 | left.as_deref(), | 4690 | left.as_deref(), |
| 4690 | corner, | 4691 | corner, |
| 4691 | side, | 4692 | side, |
| 4693 | + side, | ||
| 4692 | false, | 4694 | false, |
| 4693 | false, | 4695 | false, |
| 4694 | &mut intra, | 4696 | &mut intra, |
crates/ec-av1/src/encode.rsmodified+4 -0
| @@ -604,6 +604,7 @@ impl Plane<'_> { | |||
| 604 | left.as_deref(), | 604 | left.as_deref(), |
| 605 | corner, | 605 | corner, |
| 606 | side, | 606 | side, |
| 607 | + side, | ||
| 607 | false, | 608 | false, |
| 608 | false, | 609 | false, |
| 609 | &mut prediction, | 610 | &mut prediction, |
| @@ -823,6 +824,7 @@ impl Plane<'_> { | |||
| 823 | left.as_deref(), | 824 | left.as_deref(), |
| 824 | corner, | 825 | corner, |
| 825 | side, | 826 | side, |
| 827 | + side, | ||
| 826 | false, | 828 | false, |
| 827 | false, | 829 | false, |
| 828 | &mut prediction, | 830 | &mut prediction, |
| @@ -874,6 +876,7 @@ impl Plane<'_> { | |||
| 874 | left.as_deref(), | 876 | left.as_deref(), |
| 875 | corner, | 877 | corner, |
| 876 | side, | 878 | side, |
| 879 | + side, | ||
| 877 | false, | 880 | false, |
| 878 | false, | 881 | false, |
| 879 | &mut prediction, | 882 | &mut prediction, |
| @@ -4450,6 +4453,7 @@ mod tests { | |||
| 4450 | Some(&left), | 4453 | Some(&left), |
| 4451 | Some(128), | 4454 | Some(128), |
| 4452 | side, | 4455 | side, |
| 4456 | + side, | ||
| 4453 | false, | 4457 | false, |
| 4454 | false, | 4458 | false, |
| 4455 | &mut prediction, | 4459 | &mut prediction, |
crates/ec-av1/src/intra.rsmodified+126 -59
| @@ -141,9 +141,17 @@ struct Edges { | |||
| 141 | } | 141 | } |
| 142 | 142 | ||
| 143 | impl Edges { | 143 | impl Edges { |
| 144 | - fn build(above: Option<&[u8]>, left: Option<&[u8]>, corner: Option<u8>, side: usize) -> Self { | 144 | + /// `bw + bh` is the reach both `av1_dr_prediction_z1_c`'s `max_base_x` |
| 145 | - // A directional mode reads out to w + h - 1 either way. | 145 | + /// and `z3_c`'s `max_base_y` use (libaom `reconintra.c`) -- above and |
| 146 | - let want = side * 2; | 146 | + /// left both extend that far regardless of which of `bw`/`bh` is larger. |
| 147 | + fn build( | ||
| 148 | + above: Option<&[u8]>, | ||
| 149 | + left: Option<&[u8]>, | ||
| 150 | + corner: Option<u8>, | ||
| 151 | + bw: usize, | ||
| 152 | + bh: usize, | ||
| 153 | + ) -> Self { | ||
| 154 | + let want = bw + bh; | ||
| 147 | let extend = |samples: &[u8]| { | 155 | let extend = |samples: &[u8]| { |
| 148 | let mut row: Vec<i32> = samples.iter().map(|&s| i32::from(s)).collect(); | 156 | let mut row: Vec<i32> = samples.iter().map(|&s| i32::from(s)).collect(); |
| 149 | let last = *row.last().expect("an edge that exists has samples"); | 157 | let last = *row.last().expect("an edge that exists has samples"); |
| @@ -189,15 +197,18 @@ impl Edges { | |||
| 189 | } | 197 | } |
| 190 | } | 198 | } |
| 191 | 199 | ||
| 192 | -/// Predicts one square block into `dst`, row-major and `side * side` long. | 200 | +/// Predicts one `bw`-wide, `bh`-tall block into `dst`, row-major and |
| 201 | +/// `bw * bh` long; `bw == bh` for a square block, the only shape every | ||
| 202 | +/// caller outside this lane's own tests passes. | ||
| 193 | /// | 203 | /// |
| 194 | /// `above` is the reconstructed row above the block and `left` its | 204 | /// `above` is the reconstructed row above the block and `left` its |
| 195 | -/// reconstructed left column, each at least `side` samples long, and `corner` | 205 | +/// reconstructed left column, each at least `bw`/`bh` samples long |
| 196 | -/// the sample diagonally above-left; `None` where the block sits against an | 206 | +/// respectively, and `corner` the sample diagonally above-left; `None` where |
| 197 | -/// edge of the frame. A directional mode reads out to `2 * side`: pass the | 207 | +/// the block sits against an edge of the frame. A directional mode reads out |
| 198 | -/// samples above-right and below-left where the decoder has them decoded, and | 208 | +/// to `bw + bh`: pass the samples above-right and below-left where the |
| 199 | -/// the edge is extended by repetition where it does not, which is what the | 209 | +/// decoder has them decoded, and the edge is extended by repetition where it |
| 200 | -/// decoder's own clamp to `aboveLimit` and `leftLimit` comes to. | 210 | +/// does not, which is what the decoder's own clamp to `aboveLimit` and |
| 211 | +/// `leftLimit` comes to. | ||
| 201 | /// | 212 | /// |
| 202 | /// `angle_delta` (spec `AngleDeltaY`/`AngleDeltaUV`, `-MAX_ANGLE_DELTA` to | 213 | /// `angle_delta` (spec `AngleDeltaY`/`AngleDeltaUV`, `-MAX_ANGLE_DELTA` to |
| 203 | /// `MAX_ANGLE_DELTA`) steers every one of `V_PRED`, `H_PRED` and the six | 214 | /// `MAX_ANGLE_DELTA`) steers every one of `V_PRED`, `H_PRED` and the six |
| @@ -214,7 +225,7 @@ impl Edges { | |||
| 214 | /// | 225 | /// |
| 215 | /// # Panics | 226 | /// # Panics |
| 216 | /// Panics on a mode this module does not predict, or when `dst` is not | 227 | /// Panics on a mode this module does not predict, or when `dst` is not |
| 217 | -/// `side * side` long. | 228 | +/// `bw * bh` long. |
| 218 | #[allow(clippy::too_many_arguments)] | 229 | #[allow(clippy::too_many_arguments)] |
| 219 | pub fn predict( | 230 | pub fn predict( |
| 220 | mode: u8, | 231 | mode: u8, |
| @@ -222,13 +233,14 @@ pub fn predict( | |||
| 222 | above: Option<&[u8]>, | 233 | above: Option<&[u8]>, |
| 223 | left: Option<&[u8]>, | 234 | left: Option<&[u8]>, |
| 224 | corner: Option<u8>, | 235 | corner: Option<u8>, |
| 225 | - side: usize, | 236 | + bw: usize, |
| 237 | + bh: usize, | ||
| 226 | enable_edge_filter: bool, | 238 | enable_edge_filter: bool, |
| 227 | smooth_neighbor: bool, | 239 | smooth_neighbor: bool, |
| 228 | dst: &mut [u8], | 240 | dst: &mut [u8], |
| 229 | ) { | 241 | ) { |
| 230 | - assert_eq!(dst.len(), side * side, "the destination is the block"); | 242 | + assert_eq!(dst.len(), bw * bh, "the destination is the block"); |
| 231 | - let edges = Edges::build(above, left, corner, side); | 243 | + let edges = Edges::build(above, left, corner, bw, bh); |
| 232 | let is_directional = matches!( | 244 | let is_directional = matches!( |
| 233 | mode, | 245 | mode, |
| 234 | V_PRED | H_PRED | D45_PRED | D67_PRED | D113_PRED | D135_PRED | D157_PRED | D203_PRED | 246 | V_PRED | H_PRED | D45_PRED | D67_PRED | D113_PRED | D135_PRED | D157_PRED | D203_PRED |
| @@ -241,12 +253,13 @@ pub fn predict( | |||
| 241 | // way (`av1_is_directional_mode`'s callers never call the z1/z2/z3 | 253 | // way (`av1_is_directional_mode`'s callers never call the z1/z2/z3 |
| 242 | // walk there either). | 254 | // walk there either). |
| 243 | if angle != 90 && angle != 180 { | 255 | if angle != 90 && angle != 180 { |
| 244 | - let n_top = above.map_or(0, |a| a.len().min(side)); | 256 | + let n_top = above.map_or(0, |a| a.len().min(bw)); |
| 245 | - let n_left = left.map_or(0, |a| a.len().min(side)); | 257 | + let n_left = left.map_or(0, |a| a.len().min(bh)); |
| 246 | directional( | 258 | directional( |
| 247 | angle as u16, | 259 | angle as u16, |
| 248 | &edges, | 260 | &edges, |
| 249 | - side, | 261 | + bw, |
| 262 | + bh, | ||
| 250 | enable_edge_filter, | 263 | enable_edge_filter, |
| 251 | smooth_neighbor, | 264 | smooth_neighbor, |
| 252 | n_top, | 265 | n_top, |
| @@ -256,36 +269,42 @@ pub fn predict( | |||
| 256 | return; | 269 | return; |
| 257 | } | 270 | } |
| 258 | } | 271 | } |
| 259 | - let weights = &SM_WEIGHTS[side..side * 2]; | 272 | + // `smooth_predictor` (libaom `intrapred.c`) indexes its width and height |
| 260 | - for row in 0..side { | 273 | + // weight rows separately (`smooth_weights + bw - 4` / `+ bh - 4`) -- a |
| 261 | - for col in 0..side { | 274 | + // square block reads the same row for both, which is why this collapsed |
| 275 | + // to one `weights` slice before. | ||
| 276 | + let weights_w = &SM_WEIGHTS[bw..bw * 2]; | ||
| 277 | + let weights_h = &SM_WEIGHTS[bh..bh * 2]; | ||
| 278 | + for row in 0..bh { | ||
| 279 | + for col in 0..bw { | ||
| 262 | let (r, c) = (row as i32, col as i32); | 280 | let (r, c) = (row as i32, col as i32); |
| 263 | - let last = side as i32 - 1; | 281 | + let last_w = bw as i32 - 1; |
| 282 | + let last_h = bh as i32 - 1; | ||
| 264 | let value = match mode { | 283 | let value = match mode { |
| 265 | - DC_PRED => dc(above, left, side), | 284 | + DC_PRED => dc(above, left, bw, bh), |
| 266 | V_PRED => edges.above(c), | 285 | V_PRED => edges.above(c), |
| 267 | H_PRED => edges.left(r), | 286 | H_PRED => edges.left(r), |
| 268 | SMOOTH_PRED => round2( | 287 | SMOOTH_PRED => round2( |
| 269 | - i32::from(weights[row]) * edges.above(c) | 288 | + i32::from(weights_h[row]) * edges.above(c) |
| 270 | - + (256 - i32::from(weights[row])) * edges.left(last) | 289 | + + (256 - i32::from(weights_h[row])) * edges.left(last_h) |
| 271 | - + i32::from(weights[col]) * edges.left(r) | 290 | + + i32::from(weights_w[col]) * edges.left(r) |
| 272 | - + (256 - i32::from(weights[col])) * edges.above(last), | 291 | + + (256 - i32::from(weights_w[col])) * edges.above(last_w), |
| 273 | 9, | 292 | 9, |
| 274 | ), | 293 | ), |
| 275 | SMOOTH_V_PRED => round2( | 294 | SMOOTH_V_PRED => round2( |
| 276 | - i32::from(weights[row]) * edges.above(c) | 295 | + i32::from(weights_h[row]) * edges.above(c) |
| 277 | - + (256 - i32::from(weights[row])) * edges.left(last), | 296 | + + (256 - i32::from(weights_h[row])) * edges.left(last_h), |
| 278 | 8, | 297 | 8, |
| 279 | ), | 298 | ), |
| 280 | SMOOTH_H_PRED => round2( | 299 | SMOOTH_H_PRED => round2( |
| 281 | - i32::from(weights[col]) * edges.left(r) | 300 | + i32::from(weights_w[col]) * edges.left(r) |
| 282 | - + (256 - i32::from(weights[col])) * edges.above(last), | 301 | + + (256 - i32::from(weights_w[col])) * edges.above(last_w), |
| 283 | 8, | 302 | 8, |
| 284 | ), | 303 | ), |
| 285 | PAETH_PRED => paeth(edges.above(c), edges.left(r), edges.above(-1)), | 304 | PAETH_PRED => paeth(edges.above(c), edges.left(r), edges.above(-1)), |
| 286 | other => panic!("intra mode {other} is not one this module predicts"), | 305 | other => panic!("intra mode {other} is not one this module predicts"), |
| 287 | }; | 306 | }; |
| 288 | - dst[row * side + col] = value.clamp(0, 255) as u8; | 307 | + dst[row * bw + col] = value.clamp(0, 255) as u8; |
| 289 | } | 308 | } |
| 290 | } | 309 | } |
| 291 | } | 310 | } |
| @@ -302,6 +321,13 @@ fn intra_edge_filter_strength(bs0: i32, bs1: i32, delta: i32, smooth_neighbor: b | |||
| 302 | if d >= 56 { | 321 | if d >= 56 { |
| 303 | strength = 1; | 322 | strength = 1; |
| 304 | } | 323 | } |
| 324 | + } else if blk_wh <= 12 { | ||
| 325 | + // Only reachable by a rect block (e.g. 4x8's blk_wh == 12): no | ||
| 326 | + // square side sum ever lands in 9..=12, which is why this branch | ||
| 327 | + // was silently absent before this lane. | ||
| 328 | + if d >= 40 { | ||
| 329 | + strength = 1; | ||
| 330 | + } | ||
| 305 | } else if blk_wh <= 16 { | 331 | } else if blk_wh <= 16 { |
| 306 | if d >= 40 { | 332 | if d >= 40 { |
| 307 | strength = 1; | 333 | strength = 1; |
| @@ -420,50 +446,58 @@ fn upsample_intra_edge(buf: &[i32]) -> Vec<i32> { | |||
| 420 | fn directional( | 446 | fn directional( |
| 421 | angle: u16, | 447 | angle: u16, |
| 422 | edges: &Edges, | 448 | edges: &Edges, |
| 423 | - side: usize, | 449 | + bw: usize, |
| 450 | + bh: usize, | ||
| 424 | enable_edge_filter: bool, | 451 | enable_edge_filter: bool, |
| 425 | smooth_neighbor: bool, | 452 | smooth_neighbor: bool, |
| 426 | n_top: usize, | 453 | n_top: usize, |
| 427 | n_left: usize, | 454 | n_left: usize, |
| 428 | dst: &mut [u8], | 455 | dst: &mut [u8], |
| 429 | ) { | 456 | ) { |
| 430 | - let n = side as i32; | 457 | + let (w, h) = (bw as i32, bh as i32); |
| 458 | + let reach = w + h; // `av1_dr_prediction_z{1,3}_c`'s `max_base_{x,y}` reach. | ||
| 431 | let need_above = angle < 180; | 459 | let need_above = angle < 180; |
| 432 | let need_left = angle > 90; | 460 | let need_left = angle > 90; |
| 433 | let need_right = angle < 90; | 461 | let need_right = angle < 90; |
| 434 | let need_bottom = angle > 180; | 462 | let need_bottom = angle > 180; |
| 435 | 463 | ||
| 436 | - // Spec position `-1..=2n - 1`, `above[0]`/`left[0]` the shared corner. | 464 | + // Spec position `-1..=w + h - 1`, `above[0]`/`left[0]` the shared corner. |
| 437 | - let mut above: Vec<i32> = (-1..2 * n).map(|p| edges.above(p)).collect(); | 465 | + let mut above: Vec<i32> = (-1..reach).map(|p| edges.above(p)).collect(); |
| 438 | - let mut left: Vec<i32> = (-1..2 * n).map(|p| edges.left(p)).collect(); | 466 | + let mut left: Vec<i32> = (-1..reach).map(|p| edges.left(p)).collect(); |
| 439 | 467 | ||
| 440 | if enable_edge_filter && angle != 90 && angle != 180 { | 468 | if enable_edge_filter && angle != 90 && angle != 180 { |
| 441 | - if need_above && need_left && 2 * n >= 24 { | 469 | + if need_above && need_left && reach >= 24 { |
| 442 | let s = round2(left[1] * 5 + above[0] * 6 + above[1] * 5, 4); | 470 | let s = round2(left[1] * 5 + above[0] * 6 + above[1] * 5, 4); |
| 443 | above[0] = s; | 471 | above[0] = s; |
| 444 | left[0] = s; | 472 | left[0] = s; |
| 445 | } | 473 | } |
| 474 | + // `intra_edge_filter_strength(txwpx, txhpx, ...)` for above and | ||
| 475 | + // `(txhpx, txwpx, ...)` for left (`reconintra.c`) -- the strength | ||
| 476 | + // itself is symmetric in its first two args (only their sum | ||
| 477 | + // matters), but the pixel counts filtered below are NOT: above's | ||
| 478 | + // run-past-`n_top` extension is `txhpx` (the CROSS axis), left's is | ||
| 479 | + // `txwpx`. | ||
| 446 | if need_above && n_top > 0 { | 480 | if need_above && n_top > 0 { |
| 447 | - let strength = intra_edge_filter_strength(n, n, i32::from(angle) - 90, smooth_neighbor); | 481 | + let strength = intra_edge_filter_strength(w, h, i32::from(angle) - 90, smooth_neighbor); |
| 448 | - let n_px = n_top as i32 + 1 + if need_right { n } else { 0 }; | 482 | + let n_px = n_top as i32 + 1 + if need_right { h } else { 0 }; |
| 449 | filter_intra_edge(&mut above[..n_px as usize], strength); | 483 | filter_intra_edge(&mut above[..n_px as usize], strength); |
| 450 | } | 484 | } |
| 451 | if need_left && n_left > 0 { | 485 | if need_left && n_left > 0 { |
| 452 | let strength = | 486 | let strength = |
| 453 | - intra_edge_filter_strength(n, n, i32::from(angle) - 180, smooth_neighbor); | 487 | + intra_edge_filter_strength(h, w, i32::from(angle) - 180, smooth_neighbor); |
| 454 | - let n_px = n_left as i32 + 1 + if need_bottom { n } else { 0 }; | 488 | + let n_px = n_left as i32 + 1 + if need_bottom { w } else { 0 }; |
| 455 | filter_intra_edge(&mut left[..n_px as usize], strength); | 489 | filter_intra_edge(&mut left[..n_px as usize], strength); |
| 456 | } | 490 | } |
| 457 | } | 491 | } |
| 458 | 492 | ||
| 459 | let upsample_above = | 493 | let upsample_above = |
| 460 | - enable_edge_filter && use_intra_edge_upsample(n, n, i32::from(angle) - 90, smooth_neighbor); | 494 | + enable_edge_filter && use_intra_edge_upsample(w, h, i32::from(angle) - 90, smooth_neighbor); |
| 461 | let upsample_left = enable_edge_filter | 495 | let upsample_left = enable_edge_filter |
| 462 | - && use_intra_edge_upsample(n, n, i32::from(angle) - 180, smooth_neighbor); | 496 | + && use_intra_edge_upsample(h, w, i32::from(angle) - 180, smooth_neighbor); |
| 463 | 497 | ||
| 464 | let above_up; | 498 | let above_up; |
| 465 | let (above_buf, above_off): (&[i32], i32) = if need_above && upsample_above { | 499 | let (above_buf, above_off): (&[i32], i32) = if need_above && upsample_above { |
| 466 | - let n_px = (n + if need_right { n } else { 0 }) as usize; | 500 | + let n_px = (w + if need_right { h } else { 0 }) as usize; |
| 467 | above_up = upsample_intra_edge(&above[..=n_px]); | 501 | above_up = upsample_intra_edge(&above[..=n_px]); |
| 468 | (&above_up, 2) | 502 | (&above_up, 2) |
| 469 | } else { | 503 | } else { |
| @@ -471,7 +505,7 @@ fn directional( | |||
| 471 | }; | 505 | }; |
| 472 | let left_up; | 506 | let left_up; |
| 473 | let (left_buf, left_off): (&[i32], i32) = if need_left && upsample_left { | 507 | let (left_buf, left_off): (&[i32], i32) = if need_left && upsample_left { |
| 474 | - let n_px = (n + if need_bottom { n } else { 0 }) as usize; | 508 | + let n_px = (h + if need_bottom { w } else { 0 }) as usize; |
| 475 | left_up = upsample_intra_edge(&left[..=n_px]); | 509 | left_up = upsample_intra_edge(&left[..=n_px]); |
| 476 | (&left_up, 2) | 510 | (&left_up, 2) |
| 477 | } else { | 511 | } else { |
| @@ -485,11 +519,11 @@ fn directional( | |||
| 485 | round2(edge(base) * (32 - shift) + edge(base + 1) * shift, 5) | 519 | round2(edge(base) * (32 - shift) + edge(base + 1) * shift, 5) |
| 486 | }; | 520 | }; |
| 487 | 521 | ||
| 488 | - for row in 0..n { | 522 | + for row in 0..h { |
| 489 | - for col in 0..n { | 523 | + for col in 0..w { |
| 490 | let value = if angle < 90 { | 524 | let value = if angle < 90 { |
| 491 | let dx = dr_intra_derivative(angle); | 525 | let dx = dr_intra_derivative(angle); |
| 492 | - let max_base = (2 * n - 1) << up_a; | 526 | + let max_base = (reach - 1) << up_a; |
| 493 | let frac_bits = 6 - up_a; | 527 | let frac_bits = 6 - up_a; |
| 494 | let x = dx * (row + 1); | 528 | let x = dx * (row + 1); |
| 495 | let base = (x >> frac_bits) + (col << up_a); | 529 | let base = (x >> frac_bits) + (col << up_a); |
| @@ -501,7 +535,7 @@ fn directional( | |||
| 501 | } | 535 | } |
| 502 | } else if angle > 180 { | 536 | } else if angle > 180 { |
| 503 | let dy = dr_intra_derivative(270 - angle); | 537 | let dy = dr_intra_derivative(270 - angle); |
| 504 | - let max_base = (2 * n - 1) << up_l; | 538 | + let max_base = (reach - 1) << up_l; |
| 505 | let frac_bits = 6 - up_l; | 539 | let frac_bits = 6 - up_l; |
| 506 | let y = dy * (col + 1); | 540 | let y = dy * (col + 1); |
| 507 | let base = (y >> frac_bits) + (row << up_l); | 541 | let base = (y >> frac_bits) + (row << up_l); |
| @@ -531,7 +565,7 @@ fn directional( | |||
| 531 | blend(&left_at, y2 >> frac_bits_y, ((y2 << up_l) & 0x3F) >> 1) | 565 | blend(&left_at, y2 >> frac_bits_y, ((y2 << up_l) & 0x3F) >> 1) |
| 532 | } | 566 | } |
| 533 | }; | 567 | }; |
| 534 | - dst[(row * n + col) as usize] = value.clamp(0, 255) as u8; | 568 | + dst[(row * w + col) as usize] = value.clamp(0, 255) as u8; |
| 535 | } | 569 | } |
| 536 | } | 570 | } |
| 537 | } | 571 | } |
| @@ -545,19 +579,19 @@ fn directional( | |||
| 545 | /// before `dc_predictor` averages, so a slice truncated short by the true | 579 | /// before `dc_predictor` averages, so a slice truncated short by the true |
| 546 | /// frame edge must be extended by repetition here too, not averaged over its | 580 | /// frame edge must be extended by repetition here too, not averaged over its |
| 547 | /// own shorter length. | 581 | /// own shorter length. |
| 548 | -fn dc(above: Option<&[u8]>, left: Option<&[u8]>, side: usize) -> i32 { | 582 | +fn dc(above: Option<&[u8]>, left: Option<&[u8]>, bw: usize, bh: usize) -> i32 { |
| 549 | - let extend = |samples: &[u8]| -> Vec<u8> { | 583 | + let extend = |samples: &[u8], want: usize| -> Vec<u8> { |
| 550 | - if samples.len() >= side { | 584 | + if samples.len() >= want { |
| 551 | - samples[..side].to_vec() | 585 | + samples[..want].to_vec() |
| 552 | } else { | 586 | } else { |
| 553 | let last = *samples.last().expect("an edge that exists has samples"); | 587 | let last = *samples.last().expect("an edge that exists has samples"); |
| 554 | let mut v = samples.to_vec(); | 588 | let mut v = samples.to_vec(); |
| 555 | - v.resize(side, last); | 589 | + v.resize(want, last); |
| 556 | v | 590 | v |
| 557 | } | 591 | } |
| 558 | }; | 592 | }; |
| 559 | - let above = above.map(extend); | 593 | + let above = above.map(|a| extend(a, bw)); |
| 560 | - let left = left.map(extend); | 594 | + let left = left.map(|l| extend(l, bh)); |
| 561 | let average = |samples: &[u8]| { | 595 | let average = |samples: &[u8]| { |
| 562 | let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum(); | 596 | let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum(); |
| 563 | ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32 | 597 | ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32 |
| @@ -566,14 +600,47 @@ fn dc(above: Option<&[u8]>, left: Option<&[u8]>, side: usize) -> i32 { | |||
| 566 | (None, None) => 128, | 600 | (None, None) => 128, |
| 567 | (Some(a), None) => average(a), | 601 | (Some(a), None) => average(a), |
| 568 | (None, Some(l)) => average(l), | 602 | (None, Some(l)) => average(l), |
| 569 | - (Some(a), Some(l)) => { | 603 | + (Some(a), Some(l)) if bw == bh => { |
| 604 | + // `dc_predictor` (libaom `intrapred.c`): exact division, the | ||
| 605 | + // square path this whole lane must leave bit-identical. | ||
| 570 | let sum: u32 = a.iter().chain(l).map(|&s| u32::from(s)).sum(); | 606 | let sum: u32 = a.iter().chain(l).map(|&s| u32::from(s)).sum(); |
| 571 | let count = (a.len() + l.len()) as u32; | 607 | let count = (a.len() + l.len()) as u32; |
| 572 | ((sum + (count >> 1)) / count) as i32 | 608 | ((sum + (count >> 1)) / count) as i32 |
| 573 | } | 609 | } |
| 610 | + (Some(a), Some(l)) => { | ||
| 611 | + // `dc_predictor_rect`: AV1 never divides by `bw + bh` exactly for | ||
| 612 | + // a rect block -- it approximates with a multiply-shift whose | ||
| 613 | + // constants are derived from `bw + bh`'s odd factor (`d == 3` for | ||
| 614 | + // every 1:2 ratio, `d == 5` for every 1:4 one; libaom comments | ||
| 615 | + // this exact derivation in `intrapred.c` rather than tabulating | ||
| 616 | + // it, so this ports the derivation, not a lookup). | ||
| 617 | + let sum: u32 = a.iter().chain(l).map(|&s| u32::from(s)).sum(); | ||
| 618 | + let (shift1, multiplier) = dc_rect_multiplier(bw, bh); | ||
| 619 | + let num = u64::from(sum + ((bw + bh) as u32 >> 1)); | ||
| 620 | + (((num >> shift1) * multiplier) >> 16) as i32 | ||
| 621 | + } | ||
| 574 | } | 622 | } |
| 575 | } | 623 | } |
| 576 | 624 | ||
| 625 | +/// `dc_predictor_rect`'s per-call `(shift1, multiplier)` (libaom | ||
| 626 | +/// `intrapred.c`, `DC_MULTIPLIER_1X2`/`DC_MULTIPLIER_1X4`): shift `bw + bh` | ||
| 627 | +/// right until it is odd; AV1's only rect ratios (1:2, 1:4) leave that odd | ||
| 628 | +/// remainder at exactly 3 or 5. | ||
| 629 | +fn dc_rect_multiplier(bw: usize, bh: usize) -> (u32, u64) { | ||
| 630 | + let mut d = (bw + bh) as u32; | ||
| 631 | + let mut shift1 = 0; | ||
| 632 | + while d & 1 == 0 { | ||
| 633 | + d >>= 1; | ||
| 634 | + shift1 += 1; | ||
| 635 | + } | ||
| 636 | + let multiplier = match d { | ||
| 637 | + 3 => 0x5556, | ||
| 638 | + 5 => 0x3334, | ||
| 639 | + _ => panic!("AV1 rect blocks are only 1:2 or 1:4 (bw={bw} bh={bh})"), | ||
| 640 | + }; | ||
| 641 | + (shift1, multiplier) | ||
| 642 | +} | ||
| 643 | + | ||
| 577 | /// `Intra_Filter_Taps` (spec 7.11.2.3 / libaom `av1_filter_intra_taps`, | 644 | /// `Intra_Filter_Taps` (spec 7.11.2.3 / libaom `av1_filter_intra_taps`, |
| 578 | /// `av1/common/reconintra.c`): five recursive-filter modes (DC/V/H/D157/Paeth | 645 | /// `av1/common/reconintra.c`): five recursive-filter modes (DC/V/H/D157/Paeth |
| 579 | /// variants, in that order), each predicting an 8-tap block of eight output | 646 | /// variants, in that order), each predicting an 8-tap block of eight output |
| @@ -664,7 +731,7 @@ pub fn predict_filter_intra( | |||
| 664 | ) { | 731 | ) { |
| 665 | assert_eq!(dst.len(), side * side, "the destination is the block"); | 732 | assert_eq!(dst.len(), side * side, "the destination is the block"); |
| 666 | assert_eq!(side % 4, 0, "filter intra only offers 4/8/16/32"); | 733 | assert_eq!(side % 4, 0, "filter intra only offers 4/8/16/32"); |
| 667 | - let edges = Edges::build(above, left, corner, side); | 734 | + let edges = Edges::build(above, left, corner, side, side); |
| 668 | let taps = &FILTER_INTRA_TAPS[mode]; | 735 | let taps = &FILTER_INTRA_TAPS[mode]; |
| 669 | // A (side+1)-square buffer: row 0 / column 0 hold the corner and the | 736 | // A (side+1)-square buffer: row 0 / column 0 hold the corner and the |
| 670 | // above/left edges, `buffer[r+1][c+1]` the block's own sample at (r, c). | 737 | // above/left edges, `buffer[r+1][c+1]` the block's own sample at (r, c). |