The BD gate decodes every ladder point with our own decoder too
Browse files at this commit8f036d9b0e0c01ca9a31b8e02e79681df2de8ee2Parent: d4248c3- test
- the BD gate decodes every ladder point with our own decoder too
1 changed file +63 -15
- crates/ec-av1/src/encode.rs +63 -15
crates/ec-av1/src/encode.rsmodified+63 -15
| @@ -9120,11 +9120,32 @@ mod tests { | ||
| 9120 | 9120 | (ladder, wall) |
| 9121 | 9121 | } |
| 9122 | 9122 | |
| 9123 | + /// First differing sample between two pictures, as | |
| 9124 | + /// `(plane, index, left, right)` -- so a gate failure names the plane and | |
| 9125 | + /// the sample instead of just "differs". A length difference counts as a | |
| 9126 | + /// mismatch of that plane at the first index past the shorter one. | |
| 9127 | + fn first_plane_mismatch(a: &Picture, b: &Picture) -> Option<(&'static str, usize, i64, i64)> { | |
| 9128 | + for (plane, l, r) in [("Y", &a.y, &b.y), ("U", &a.u, &b.u), ("V", &a.v, &b.v)] { | |
| 9129 | + if let Some(i) = l.iter().zip(r.iter()).position(|(x, y)| x != y) { | |
| 9130 | + return Some((plane, i, i64::from(l[i]), i64::from(r[i]))); | |
| 9131 | + } | |
| 9132 | + if l.len() != r.len() { | |
| 9133 | + return Some((plane, l.len().min(r.len()), l.len() as i64, r.len() as i64)); | |
| 9134 | + } | |
| 9135 | + } | |
| 9136 | + None | |
| 9137 | + } | |
| 9138 | + | |
| 9123 | 9139 | /// The same ladder for this encoder, at four `base_q_idx` points. The PSNR |
| 9124 | - /// comes from ffmpeg's decode of our own stream, and every frame of that | |
| 9125 | - /// decode is asserted sample-exact against our reconstruction -- which is | |
| 9126 | - /// itself a gate: it proves the bitstream says what the encoder thinks. | |
| 9140 | + /// comes from ffmpeg's decode of our own stream, and at EVERY point the | |
| 9141 | + /// stream is decoded twice -- by ffmpeg and by our own | |
| 9142 | + /// [`crate::stream::decode_stream`] -- and all three pictures (encoder | |
| 9143 | + /// reconstruction, ffmpeg's decode, our decode) are asserted sample-exact | |
| 9144 | + /// in Y, U and V over every shown frame in display order. That is itself | |
| 9145 | + /// a gate: it proves the bitstream says what the encoder thinks, in the | |
| 9146 | + /// only two decoders that read it. | |
| 9127 | 9147 | fn our_ladder( |
| 9148 | + name: &str, | |
| 9128 | 9149 | source: &[Picture], |
| 9129 | 9150 | width: usize, |
| 9130 | 9151 | height: usize, |
| @@ -9137,13 +9158,27 @@ mod tests { | ||
| 9137 | 9158 | let encoded = encode_sequence_with_ctx(source, q, 0.5, fctx).unwrap(); |
| 9138 | 9159 | wall += start.elapsed().as_secs_f64(); |
| 9139 | 9160 | let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, source.len()); |
| 9161 | + let ours = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); | |
| 9162 | + assert_eq!( | |
| 9163 | + ours.len(), | |
| 9164 | + source.len(), | |
| 9165 | + "{name} q={q}: our decoder's display-order frame count" | |
| 9166 | + ); | |
| 9140 | 9167 | for (i, (d, e)) in decoded.iter().zip(&encoded.frames).enumerate() { |
| 9141 | - assert!( | |
| 9142 | - d.y == e.reconstruction.y | |
| 9143 | - && d.u == e.reconstruction.u | |
| 9144 | - && d.v == e.reconstruction.v, | |
| 9145 | - "q={q} frame {i}: ffmpeg's decode differs from our reconstruction" | |
| 9146 | - ); | |
| 9168 | + if let Some((plane, s, got, want)) = first_plane_mismatch(d, &e.reconstruction) { | |
| 9169 | + panic!( | |
| 9170 | + "{name} q={q} frame {i} plane {plane} sample {s}: ffmpeg decoded \ | |
| 9171 | + {got}, the encoder reconstructed {want}" | |
| 9172 | + ); | |
| 9173 | + } | |
| 9174 | + } | |
| 9175 | + for (i, (o, e)) in ours.iter().zip(&encoded.frames).enumerate() { | |
| 9176 | + if let Some((plane, s, got, want)) = first_plane_mismatch(o, &e.reconstruction) { | |
| 9177 | + panic!( | |
| 9178 | + "{name} q={q} frame {i} plane {plane} sample {s}: our decoder decoded \ | |
| 9179 | + {got}, the encoder reconstructed {want}" | |
| 9180 | + ); | |
| 9181 | + } | |
| 9147 | 9182 | } |
| 9148 | 9183 | let mean: f64 = decoded |
| 9149 | 9184 | .iter() |
| @@ -9180,6 +9215,7 @@ mod tests { | ||
| 9180 | 9215 | /// the ladder, the wall, and — per pyramid level over the whole ladder — |
| 9181 | 9216 | /// how many frames and how many bytes it spent. |
| 9182 | 9217 | fn our_ladder_pyramid( |
| 9218 | + name: &str, | |
| 9183 | 9219 | source: &[Picture], |
| 9184 | 9220 | width: usize, |
| 9185 | 9221 | height: usize, |
| @@ -9224,10 +9260,15 @@ mod tests { | ||
| 9224 | 9260 | // source picture, in the right order -- checked against our own |
| 9225 | 9261 | // decoder as well as ffmpeg's, both below. |
| 9226 | 9262 | let ours = crate::stream::decode_stream(&stream).expect("our decoder"); |
| 9227 | - assert_eq!(ours.len(), source.len(), "q={q}: our display-order count"); | |
| 9263 | + assert_eq!(ours.len(), source.len(), "{name} q={q}: our display-order count"); | |
| 9228 | 9264 | let decoded = ffmpeg_decode_sequence(&stream, width, height, source.len()); |
| 9229 | 9265 | for (i, (d, o)) in decoded.iter().zip(&ours).enumerate() { |
| 9230 | - assert!(d.y == o.y, "q={q} display frame {i}: ffmpeg differs from our decoder"); | |
| 9266 | + if let Some((plane, s, got, want)) = first_plane_mismatch(d, o) { | |
| 9267 | + panic!( | |
| 9268 | + "{name} q={q} display frame {i} plane {plane} sample {s}: ffmpeg \ | |
| 9269 | + decoded {got}, our decoder decoded {want}" | |
| 9270 | + ); | |
| 9271 | + } | |
| 9231 | 9272 | } |
| 9232 | 9273 | let mean: f64 = decoded |
| 9233 | 9274 | .iter() |
| @@ -9426,8 +9467,15 @@ mod tests { | ||
| 9426 | 9467 | /// search's own cost, uncut: see the two intra-pruning variants measured |
| 9427 | 9468 | /// and rejected in the lane report). No |
| 9428 | 9469 | /// threshold is asserted yet -- this run sets the baseline; the gate |
| 9429 | - /// asserts only four monotone points per ladder and that ffmpeg decodes | |
| 9430 | - /// our stream sample-exact against our reconstruction. | |
| 9470 | + /// asserts four monotone points per ladder, and at EVERY ladder point of | |
| 9471 | + /// EVERY clip it decodes our stream twice -- with ffmpeg and with our own | |
| 9472 | + /// `crate::stream::decode_stream` -- and asserts the encoder's | |
| 9473 | + /// reconstruction, ffmpeg's decode and our decode sample-exact in Y, U | |
| 9474 | + /// and V over every shown frame in display order. A failure names the | |
| 9475 | + /// clip, the q, the frame, the plane and the first differing sample. | |
| 9476 | + /// (Under `EC_AV1_PYRAMID` the frames come out of the streaming facade, | |
| 9477 | + /// which exposes no per-packet reconstruction, so that path asserts the | |
| 9478 | + /// two decoders against each other on all three planes.) | |
| 9431 | 9479 | /// |
| 9432 | 9480 | /// Run: |
| 9433 | 9481 | /// cargo test -p ec-av1 --release --lib -- --ignored \ |
| @@ -9509,10 +9557,10 @@ mod tests { | ||
| 9509 | 9557 | let _ = crate::tile::take_compound_pair_hits(); |
| 9510 | 9558 | let _ = crate::motion::take_census(); |
| 9511 | 9559 | let (ours, ours_wall) = match pyramid_from_env() { |
| 9512 | - None => our_ladder(&source, width, height, fctx), | |
| 9560 | + None => our_ladder(name, &source, width, height, fctx), | |
| 9513 | 9561 | Some(pyramid) => { |
| 9514 | 9562 | let (ladder, wall, counts, bytes) = |
| 9515 | - our_ladder_pyramid(&source, width, height, pyramid); | |
| 9563 | + our_ladder_pyramid(name, &source, width, height, pyramid); | |
| 9516 | 9564 | eprintln!( |
| 9517 | 9565 | "{name}: pyramid {pyramid:?} -- frames key {} arf {} leaf {} \ |
| 9518 | 9566 | show_existing {}; bytes key {} arf {} leaf {} show_existing {}", |