diff --git a/crates/ec-h264/src/enc/mb.rs b/crates/ec-h264/src/enc/mb.rs index d89f8afc..88a23e9c 100644 --- a/crates/ec-h264/src/enc/mb.rs +++ b/crates/ec-h264/src/enc/mb.rs @@ -1124,75 +1124,73 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Vec &[], - 1 => &[2], - _ => &[2, 1], - } - }; - if fast && e.fast.me_sub == 1 { - // The four half-sample neighbours on the axes, then only the - // diagonal the two winning axes point at. - let base = best; - let mut axis = [(base.0, base.1); 2]; - for (k, dirs) in [[(2i16, 0i16), (-2, 0)], [(0, 2), (0, -2)]] - .into_iter() - .enumerate() - { - for (dx, dy) in dirs { - let cand = [base.0[0] + dx, base.0[1] + dy]; - let c = cost_of(cand, sub_satd); - if c < axis[k].1 { - axis[k] = (cand, c); - } - } - } - let both = axis[0].1 < base.1 && axis[1].1 < base.1; - if both { - let cand = [axis[0].0[0], axis[1].0[1]]; + best.1 = cost_of(best.0, sub_satd); + // Balanced iterates both sub-sample steps to convergence. Fast + // looks at each ring of eight once. + let rings: &[i16] = if !fast { + &[2, 1] + } else { + match e.fast.me_sub { + 0 => &[], + 1 => &[2], + _ => &[2, 1], + } + }; + if fast && e.fast.me_sub == 1 { + // The four half-sample neighbours on the axes, then only the + // diagonal the two winning axes point at. + let base = best; + let mut axis = [(base.0, base.1); 2]; + for (k, dirs) in [[(2i16, 0i16), (-2, 0)], [(0, 2), (0, -2)]] + .into_iter() + .enumerate() + { + for (dx, dy) in dirs { + let cand = [base.0[0] + dx, base.0[1] + dy]; let c = cost_of(cand, sub_satd); - if c < best.1 { - best = (cand, c); + if c < axis[k].1 { + axis[k] = (cand, c); } } - for a in axis { - if a.1 < best.1 { - best = a; - } + } + let both = axis[0].1 < base.1 && axis[1].1 < base.1; + if both { + let cand = [axis[0].0[0], axis[1].0[1]]; + let c = cost_of(cand, sub_satd); + if c < best.1 { + best = (cand, c); } } - let rings: &[i16] = if fast && e.fast.me_sub == 1 { - &[] - } else { - rings - }; - for &step in rings { - let mut improved = true; - while improved { - improved = false; - for (dx, dy) in [ - (step, 0), - (-step, 0), - (0, step), - (0, -step), - (step, step), - (-step, -step), - (step, -step), - (-step, step), - ] { - let cand = [best.0[0] + dx, best.0[1] + dy]; - let c = cost_of(cand, sub_satd); - if c < best.1 { - best = (cand, c); - improved = !fast; - } + for a in axis { + if a.1 < best.1 { + best = a; + } + } + } + let rings: &[i16] = if fast && e.fast.me_sub == 1 { + &[] + } else { + rings + }; + for &step in rings { + let mut improved = true; + while improved { + improved = false; + for (dx, dy) in [ + (step, 0), + (-step, 0), + (0, step), + (0, -step), + (step, step), + (-step, -step), + (step, -step), + (-step, step), + ] { + let cand = [best.0[0] + dx, best.0[1] + dy]; + let c = cost_of(cand, sub_satd); + if c < best.1 { + best = (cand, c); + improved = !fast; } } } diff --git a/crates/ec-h264/tests/conformance.rs b/crates/ec-h264/tests/conformance.rs index 4e819a54..68b1107c 100644 --- a/crates/ec-h264/tests/conformance.rs +++ b/crates/ec-h264/tests/conformance.rs @@ -1120,29 +1120,26 @@ fn switching_slices_are_refused_by_name() { } } - /// Extract the video elementary stream of a real file as Annex B. fn extract_annexb(src: &Path, dst: &Path, frames: usize) -> bool { - run( - &[ - "-v", - "error", - "-y", - "-i", - &src.to_string_lossy(), - "-map", - "0:v:0", - "-c", - "copy", - "-bsf:v", - "h264_mp4toannexb", - "-frames:v", - &frames.to_string(), - "-f", - "h264", - &dst.to_string_lossy(), - ], - ) + run(&[ + "-v", + "error", + "-y", + "-i", + &src.to_string_lossy(), + "-map", + "0:v:0", + "-c", + "copy", + "-bsf:v", + "h264_mp4toannexb", + "-frames:v", + &frames.to_string(), + "-f", + "h264", + &dst.to_string_lossy(), + ]) } #[test]