Skip to content

Commit

Permalink
luminance: add new eotf param for PQ support
Browse files Browse the repository at this point in the history
  • Loading branch information
sgt0 committed Aug 3, 2024
1 parent dc54f9b commit 66540a9
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 80 deletions.
70 changes: 46 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ crate-type = ["cdylib"]
[dependencies]
const-str = "0.5.7"
kolor-64 = "0.1.9"
num-derive = "0.4.2"
num-traits = "0.2.19"
vapoursynth4-rs = { git = "https://github.com/inflation/vapoursynth4-rs", rev = "7c1b3b8cd3c3b7b4c7d09e174cd43fb853128ec8" }

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cambi.Cambi(
topk: float = 0.6,
tvi_threshold: float = 0.019,
max_log_contrast: int = 2,
eotf: int = 0,
prop: str = "CAMBI",
) -> vs.VideoNode
```
Expand All @@ -35,6 +36,12 @@ property named `prop`.
- `tvi_threshold` — Visibility threshold for luminance ΔL < tvi_threshold\*L_mean for BT.1886.
- `max_log_contrast` — Maximum contrast in log luma level (2^max_log_contrast)
at 10-bit. Default 2 is equivalent to 4 luma levels at 10-bit.
- `eotf` — Electro-optical transfer function.
| Value | Description |
| ----- | ----------- |
| 0 | Determine from the `_Transfer` frame property. |
| 1 | ITU-R BT.1886. |
| 2 | Perceptual quantizer (SMPTE ST 2084). |
- `prop` — Name of the frame property to store the CAMBI score in.

## Benchmark
Expand Down
34 changes: 13 additions & 21 deletions src/cambi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ enum TviBisect {
TooBig,
}

fn tvi_condition<T: Eotf>(sample: i32, diff: i32, tvi_threshold: f64, luma_range: &LumaRange, eotf: &T) -> bool {
fn tvi_condition(sample: i32, diff: i32, tvi_threshold: f64, luma_range: &LumaRange, eotf: &Eotf) -> bool {
let mean_luminance = get_luminance(sample, luma_range, eotf);
let diff_luminance = get_luminance(sample + diff, luma_range, eotf);
let delta_luminance = diff_luminance - mean_luminance;
delta_luminance > tvi_threshold * mean_luminance
}

fn tvi_hard_threshold_condition<T: Eotf>(
fn tvi_hard_threshold_condition(
sample: i32,
diff: i32,
tvi_threshold: f64,
luma_range: &LumaRange,
eotf: &T,
eotf: &Eotf,
) -> TviBisect {
if !tvi_condition(sample, diff, tvi_threshold, luma_range, eotf) {
return TviBisect::TooBig;
Expand All @@ -47,13 +47,7 @@ fn tvi_hard_threshold_condition<T: Eotf>(
TviBisect::Correct
}

pub fn get_tvi_for_diff<T: Eotf>(
diff: i32,
tvi_threshold: f64,
bit_depth: i32,
luma_range: &LumaRange,
eotf: &T,
) -> i32 {
pub fn get_tvi_for_diff(diff: i32, tvi_threshold: f64, bit_depth: i32, luma_range: &LumaRange, eotf: &Eotf) -> i32 {
let (mut foot, mut head) = (luma_range.foot, luma_range.head);
head = head - diff - 1;

Expand Down Expand Up @@ -764,8 +758,6 @@ mod tests {
use approx::assert_relative_eq;
use vapoursynth4_rs::ffi::VSColorRange;

use crate::luminance::Bt1886;

use super::*;

const EPSILON: f32 = 0.000000000001;
Expand Down Expand Up @@ -852,19 +844,19 @@ mod tests {
fn test_tvi_hard_threshold_condition() {
let limited = LumaRange::new(10, VSColorRange::VSC_RANGE_LIMITED);
assert_eq!(
tvi_hard_threshold_condition(177, 1, 0.019, &limited, &Bt1886),
tvi_hard_threshold_condition(177, 1, 0.019, &limited, &Eotf::Bt1886),
TviBisect::TooSmall
);
assert_eq!(
tvi_hard_threshold_condition(178, 1, 0.019, &limited, &Bt1886),
tvi_hard_threshold_condition(178, 1, 0.019, &limited, &Eotf::Bt1886),
TviBisect::Correct
);
assert_eq!(
tvi_hard_threshold_condition(179, 1, 0.019, &limited, &Bt1886),
tvi_hard_threshold_condition(179, 1, 0.019, &limited, &Eotf::Bt1886),
TviBisect::TooBig
);
assert_eq!(
tvi_hard_threshold_condition(305, 2, 0.019, &limited, &Bt1886),
tvi_hard_threshold_condition(305, 2, 0.019, &limited, &Eotf::Bt1886),
TviBisect::Correct
);
}
Expand All @@ -876,39 +868,39 @@ mod tests {
1,
0.019,
&LumaRange::new(10, VSColorRange::VSC_RANGE_LIMITED),
&Bt1886
&Eotf::Bt1886
));

assert!(tvi_condition(
178,
1,
0.019,
&LumaRange::new(10, VSColorRange::VSC_RANGE_LIMITED),
&Bt1886
&Eotf::Bt1886
));

assert!(!tvi_condition(
179,
1,
0.019,
&LumaRange::new(10, VSColorRange::VSC_RANGE_LIMITED),
&Bt1886
&Eotf::Bt1886
));

assert!(tvi_condition(
935,
4,
0.01,
&LumaRange::new(10, VSColorRange::VSC_RANGE_LIMITED),
&Bt1886
&Eotf::Bt1886
));

assert!(tvi_condition(
936,
4,
0.01,
&LumaRange::new(10, VSColorRange::VSC_RANGE_LIMITED),
&Bt1886
&Eotf::Bt1886
));
}
}
Loading

0 comments on commit 66540a9

Please sign in to comment.