Skip to content

Commit

Permalink
Add --xpsnr-fps arg (#262)
Browse files Browse the repository at this point in the history
Default xpsnr-fps 60, see #258 (comment)
  • Loading branch information
alexheretic authored Dec 30, 2024
1 parent 7b48a35 commit 7804833
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased (v0.9.1)
* Fix xpsnr inf score parsing.
* Add `--xpsnr-fps`: Frame rate override used to analyse both reference & distorted videos. Default 60.

# v0.9.0
* Add XPSNR support as a VMAF alternative.
Expand Down
23 changes: 23 additions & 0 deletions src/command/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,26 @@ pub struct ScoreArgs {
#[arg(long)]
pub reference_vfilter: Option<Arc<str>>,
}

/// Common xpsnr options.
#[derive(Debug, Parser, Clone, Copy)]
pub struct Xpsnr {
/// Frame rate override used to analyse both reference & distorted videos.
/// Maps to ffmpeg `-r` input arg.
///
/// Setting to 0 disables use.
#[arg(long, default_value_t = 60.0)]
pub xpsnr_fps: f32,
}

impl Xpsnr {
pub fn fps(&self) -> Option<f32> {
Some(self.xpsnr_fps).filter(|r| *r > 0.0)
}
}

impl std::hash::Hash for Xpsnr {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.xpsnr_fps.to_ne_bytes().hash(state);
}
}
5 changes: 5 additions & 0 deletions src/command/crf_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ pub struct Args {
#[clap(flatten)]
pub score: args::ScoreArgs,

#[clap(flatten)]
pub xpsnr: args::Xpsnr,

#[command(flatten)]
pub verbose: clap_verbosity_flag::Verbosity,
}
Expand Down Expand Up @@ -212,6 +215,7 @@ pub fn run(
cache,
vmaf,
score,
xpsnr,
verbose: _,
}: Args,
input_probe: Arc<Ffprobe>,
Expand Down Expand Up @@ -252,6 +256,7 @@ pub fn run(
vmaf: vmaf.clone(),
score: score.clone(),
xpsnr: min_xpsnr.is_some(),
xpsnr_opts: xpsnr,
};

let mut crf_attempts = Vec::new();
Expand Down
8 changes: 6 additions & 2 deletions src/command/sample_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ pub struct Args {
#[clap(flatten)]
pub score: args::ScoreArgs,

#[clap(flatten)]
pub xpsnr_opts: args::Xpsnr,

/// Calculate a XPSNR score instead of VMAF.
#[arg(long)]
pub xpsnr: bool,
Expand Down Expand Up @@ -147,6 +150,7 @@ pub fn run(
vmaf,
score,
xpsnr,
xpsnr_opts,
}: Args,
input_probe: Arc<Ffprobe>,
) -> impl Stream<Item = anyhow::Result<Update>> {
Expand All @@ -162,7 +166,7 @@ pub fn run(
let keep = sample_args.keep;
let temp_dir = sample_args.temp_dir;
let scoring = match xpsnr {
true => ScoringInfo::Xpsnr(&score),
true => ScoringInfo::Xpsnr(&xpsnr_opts, &score),
_ => ScoringInfo::Vmaf(&vmaf, &score),
};

Expand Down Expand Up @@ -358,7 +362,7 @@ pub fn run(
let lavfi = super::xpsnr::lavfi(
score.reference_vfilter.as_deref().or(args.vfilter.as_deref())
);
let xpsnr_out = xpsnr::run(&sample, &encoded_sample, &lavfi)?;
let xpsnr_out = xpsnr::run(&sample, &encoded_sample, &lavfi, xpsnr_opts.fps())?;
let mut xpsnr_out = pin!(xpsnr_out);
let mut logger = ProgressLogger::new("ab_av1::xpsnr", Instant::now());
let mut score = None;
Expand Down
4 changes: 2 additions & 2 deletions src/command/sample_encode/cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! _sample-encode_ file system caching logic.
use crate::{
command::args::{ScoreArgs, Vmaf},
command::args::{ScoreArgs, Vmaf, Xpsnr},
ffmpeg::FfmpegEncodeArgs,
};
use anyhow::Context;
Expand Down Expand Up @@ -71,7 +71,7 @@ pub async fn cached_encode(
#[derive(Debug, Hash, Clone, Copy)]
pub enum ScoringInfo<'a> {
Vmaf(&'a Vmaf, &'a ScoreArgs),
Xpsnr(&'a ScoreArgs),
Xpsnr(&'a Xpsnr, &'a ScoreArgs),
}

pub async fn cache_result(key: Key, result: &super::EncodeResult) -> anyhow::Result<()> {
Expand Down
5 changes: 5 additions & 0 deletions src/command/xpsnr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ pub struct Args {

#[clap(flatten)]
pub score: args::ScoreArgs,

#[clap(flatten)]
pub xpsnr: args::Xpsnr,
}

pub async fn xpsnr(
Args {
reference,
distorted,
score,
xpsnr,
}: Args,
) -> anyhow::Result<()> {
let bar = ProgressBar::new(1).with_style(
Expand All @@ -65,6 +69,7 @@ pub async fn xpsnr(
&reference,
&distorted,
&lavfi(score.reference_vfilter.as_deref()),
xpsnr.fps(),
)?);
let mut logger = ProgressLogger::new(module_path!(), Instant::now());
let mut score = None;
Expand Down
5 changes: 4 additions & 1 deletion src/xpsnr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn run(
reference: &Path,
distorted: &Path,
filter_complex: &str,
fps: Option<f32>,
) -> anyhow::Result<impl Stream<Item = XpsnrOut>> {
info!(
"xpsnr {} vs reference {}",
Expand All @@ -20,7 +21,9 @@ pub fn run(
);

let mut cmd = Command::new("ffmpeg");
cmd.arg2("-i", reference)
cmd.arg2_opt("-r", fps)
.arg2("-i", reference)
.arg2_opt("-r", fps)
.arg2("-i", distorted)
.arg2("-filter_complex", filter_complex)
.arg2("-f", "null")
Expand Down

0 comments on commit 7804833

Please sign in to comment.