Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash VMAF args for cache #165

Merged
merged 3 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
* Fix sample-encode caching to consider vmaf args.

# v0.7.10
* Fix validation preventing use of svt args starting with "-i", "-b".

Expand Down
6 changes: 5 additions & 1 deletion src/command/args/vmaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Parser;
use std::{fmt::Display, sync::Arc, thread};

/// Common vmaf options.
#[derive(Parser, Clone)]
#[derive(Parser, Clone, Hash)]
pub struct Vmaf {
/// Additional vmaf arg(s). E.g. --vmaf n_threads=8 --vmaf n_subsample=4
///
Expand Down Expand Up @@ -34,6 +34,10 @@ fn parse_vmaf_arg(arg: &str) -> anyhow::Result<Arc<str>> {
}

impl Vmaf {
pub fn is_default(&self) -> bool {
self.vmaf_args.is_empty() && self.vmaf_scale == VmafScale::Auto
}

/// Returns ffmpeg `filter_complex`/`lavfi` value for calculating vmaf.
pub fn ffmpeg_lavfi(&self, distorted_res: Option<(u32, u32)>) -> String {
let mut args = self.vmaf_args.clone();
Expand Down
1 change: 1 addition & 0 deletions src/command/sample_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ pub async fn run(
input_len,
full_pass,
&enc_args,
&vmaf,
)
.await
{
Expand Down
15 changes: 13 additions & 2 deletions src/command/sample_encode/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! _sample-encode_ file system caching logic.
use crate::ffmpeg::FfmpegEncodeArgs;
use crate::{command::args::Vmaf, ffmpeg::FfmpegEncodeArgs};
use anyhow::Context;
use std::{
ffi::OsStr,
Expand All @@ -9,6 +9,7 @@ use std::{
};

/// Return a previous stored encode result for the same sample & args.
#[allow(clippy::too_many_arguments)]
pub async fn cached_encode(
cache: bool,
sample: &Path,
Expand All @@ -17,6 +18,7 @@ pub async fn cached_encode(
input_size: u64,
full_pass: bool,
enc_args: &FfmpegEncodeArgs<'_>,
vmaf_args: &Vmaf,
) -> (Option<super::EncodeResult>, Option<Key>) {
if !cache {
return (None, None);
Expand All @@ -34,6 +36,7 @@ pub async fn cached_encode(
full_pass,
),
enc_args,
vmaf_args,
);

let key = Key(hash);
Expand Down Expand Up @@ -96,11 +99,19 @@ fn open_db() -> sled::Result<sled::Db> {
#[derive(Debug, Clone, Copy)]
pub struct Key(blake3::Hash);

fn hash_encode(input_info: impl Hash, enc_args: &FfmpegEncodeArgs<'_>) -> blake3::Hash {
fn hash_encode(
input_info: impl Hash,
enc_args: &FfmpegEncodeArgs<'_>,
vmaf_args: &Vmaf,
) -> blake3::Hash {
let mut hasher = blake3::Hasher::new();
let mut std_hasher = BlakeStdHasher(&mut hasher);
input_info.hash(&mut std_hasher);
enc_args.sample_encode_hash(&mut std_hasher);
if !vmaf_args.is_default() {
// avoid hashing if default for back compat
vmaf_args.hash(&mut std_hasher);
}
hasher.finalize()
}

Expand Down