oximedia-metering 0.1.8

Professional broadcast audio metering: ITU-R BS.1770-4, EBU R128, ATSC A/85
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Video luminance metering.
//!
//! Implements luminance measurement for video signals including:
//! - Peak luminance (nits)
//! - Average luminance
//! - Minimum luminance
//! - Luminance distribution
//! - PQ (HDR10/ST.2084) and HLG (Hybrid Log-Gamma) electro-optical transfer functions

use crate::video_quality::Frame2D;
use crate::{MeteringError, MeteringResult};

/// Transfer function for video luminance encoding.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TransferFunction {
    /// Linear (no transfer function, values are already absolute nits).
    Linear,
    /// SMPTE ST 2084 (PQ / HDR10). Signal range 0–1 maps to 0–10 000 cd/m².
    Pq,
    /// Hybrid Log-Gamma (ITU-R BT.2100, BBC/NHK HLG). Signal range 0–1.
    /// Peak luminance is configurable (typically 1000 cd/m² for broadcast).
    Hlg,
    /// ITU-R BT.709 / BT.1886 (SDR). Signal range 0–1 maps to 0–100 cd/m².
    Sdr,
}

impl TransferFunction {
    /// Convert a normalised signal value (0–1) to absolute luminance in cd/m²
    /// (nits) using the specified peak white luminance `peak_nits`.
    ///
    /// # Arguments
    ///
    /// * `signal` - Normalised signal value in [0, 1]
    /// * `peak_nits` - Display peak white luminance in cd/m²
    pub fn to_nits(&self, signal: f64, peak_nits: f64) -> f64 {
        let signal = signal.clamp(0.0, 1.0);
        match self {
            Self::Linear => signal * peak_nits,
            Self::Sdr => {
                // ITU-R BT.1886 gamma 2.4 EOTF, reference white = 100 nits.
                // L = Lw * (E / 1.0)^2.4  (simplified; Lw = peak SDR white = 100 nits)
                signal.powf(2.4) * peak_nits.min(100.0)
            }
            Self::Pq => Self::pq_eotf(signal) * peak_nits.max(1.0),
            Self::Hlg => Self::hlg_eotf(signal, peak_nits),
        }
    }

    /// SMPTE ST 2084 (PQ) EOTF — maps normalised signal to linear scene luminance.
    /// Output is normalised to [0, 1] relative to 10 000 cd/m².
    fn pq_eotf(e: f64) -> f64 {
        // ST 2084 constants
        const M1: f64 = 0.1593_017_578_125;
        const M2: f64 = 78.843_750;
        const C1: f64 = 0.835_937_5;
        const C2: f64 = 18.851_562_5;
        const C3: f64 = 18.687_5;

        let ep = e.powf(1.0 / M2);
        let num = (ep - C1).max(0.0);
        let den = C2 - C3 * ep;
        let linear = if den.abs() > 1e-12 {
            (num / den).powf(1.0 / M1)
        } else {
            0.0
        };
        linear
    }

    /// ITU-R BT.2100 HLG EOTF — maps normalised signal to linear scene luminance
    /// in cd/m², using the system gamma that depends on `peak_nits`.
    ///
    /// Reference: ITU-R BT.2100-2, Table 5.
    fn hlg_eotf(e: f64, peak_nits: f64) -> f64 {
        // HLG constants
        const A: f64 = 0.178_832_77;
        const B: f64 = 0.284_668_66;
        const C: f64 = 0.559_910_73;

        // Compute normalised scene linear value E_s from opto-electronic signal E.
        let e_s = if e <= 0.5 {
            (e * e) / 3.0
        } else {
            ((e - C).exp() / A + B) / 12.0
        };

        // Apply system gamma γ = 1.2 + 0.42 * log10(Lw / 1000).
        // For Lw = 1000 nits, γ = 1.2 (no correction).
        let gamma = 1.2 + 0.42 * (peak_nits / 1000.0).log10();
        let e_d = e_s.powf(gamma);

        // Scale to nits
        e_d * peak_nits
    }
}

/// Convert a frame whose pixel values are encoded as a normalised signal (0–1)
/// into absolute luminance values in nits according to the given transfer function.
///
/// Returns a new `Frame2D` where each pixel value is in cd/m².
pub fn decode_to_nits(frame: &Frame2D, tf: TransferFunction, peak_nits: f64) -> Frame2D {
    Frame2D::from_shape_fn(frame.height, frame.width, |y, x| {
        tf.to_nits(frame.get(y, x), peak_nits)
    })
}

/// Luminance meter for video frames.
pub struct LuminanceMeter {
    width: usize,
    height: usize,
    peak_nits: f64,
    min_nits: f64,
    average_nits: f64,
    histogram: Vec<usize>,
    histogram_bins: usize,
    max_nits: f64,
}

impl LuminanceMeter {
    /// Create a new luminance meter.
    ///
    /// # Arguments
    ///
    /// * `width` - Frame width in pixels
    /// * `height` - Frame height in pixels
    /// * `max_nits` - Maximum expected luminance in nits (e.g., 1000 for HDR, 100 for SDR)
    /// * `histogram_bins` - Number of histogram bins
    ///
    /// # Errors
    ///
    /// Returns error if configuration is invalid.
    pub fn new(
        width: usize,
        height: usize,
        max_nits: f64,
        histogram_bins: usize,
    ) -> MeteringResult<Self> {
        if width == 0 || height == 0 {
            return Err(MeteringError::InvalidConfig(
                "Width and height must be positive".to_string(),
            ));
        }

        if max_nits <= 0.0 {
            return Err(MeteringError::InvalidConfig(
                "Max nits must be positive".to_string(),
            ));
        }

        Ok(Self {
            width,
            height,
            peak_nits: 0.0,
            min_nits: f64::INFINITY,
            average_nits: 0.0,
            histogram: vec![0; histogram_bins],
            histogram_bins,
            max_nits,
        })
    }

    /// Process a luminance frame.
    ///
    /// # Arguments
    ///
    /// * `luminance` - 2D array of luminance values in nits
    ///
    /// # Errors
    ///
    /// Returns error if frame dimensions don't match.
    pub fn process(&mut self, luminance: &Frame2D) -> MeteringResult<()> {
        let (height, width) = luminance.dim();

        if width != self.width || height != self.height {
            return Err(MeteringError::InvalidConfig(format!(
                "Frame dimensions {}x{} don't match expected {}x{}",
                width, height, self.width, self.height
            )));
        }

        // Reset metrics
        self.peak_nits = 0.0;
        self.min_nits = f64::INFINITY;
        let mut sum = 0.0;
        self.histogram.fill(0);

        // Analyze frame
        for &value in luminance.iter() {
            // Update peak and min
            if value > self.peak_nits {
                self.peak_nits = value;
            }
            if value < self.min_nits {
                self.min_nits = value;
            }

            // Update sum for average
            sum += value;

            // Update histogram
            let bin = ((value / self.max_nits) * (self.histogram_bins - 1) as f64)
                .clamp(0.0, (self.histogram_bins - 1) as f64) as usize;
            self.histogram[bin] += 1;
        }

        // Calculate average
        let pixel_count = (self.width * self.height) as f64;
        self.average_nits = sum / pixel_count;

        Ok(())
    }

    /// Get the peak luminance in nits.
    pub fn peak_nits(&self) -> f64 {
        self.peak_nits
    }

    /// Get the minimum luminance in nits.
    pub fn min_nits(&self) -> f64 {
        if self.min_nits.is_infinite() {
            0.0
        } else {
            self.min_nits
        }
    }

    /// Get the average luminance in nits.
    pub fn average_nits(&self) -> f64 {
        self.average_nits
    }

    /// Get the luminance histogram.
    pub fn histogram(&self) -> &[usize] {
        &self.histogram
    }

    /// Get the contrast ratio.
    ///
    /// Contrast ratio = Peak / Min
    pub fn contrast_ratio(&self) -> f64 {
        let min = if self.min_nits.is_infinite() || self.min_nits == 0.0 {
            0.001 // Prevent division by zero
        } else {
            self.min_nits
        };

        self.peak_nits / min
    }

    /// Get the dynamic range in stops.
    ///
    /// Dynamic range (stops) = log2(Peak / Min)
    pub fn dynamic_range_stops(&self) -> f64 {
        self.contrast_ratio().log2()
    }

    /// Check if frame is within SDR range (0-100 nits).
    pub fn is_sdr(&self) -> bool {
        self.peak_nits <= 100.0
    }

    /// Check if frame is HDR10 (up to 1000 nits).
    pub fn is_hdr10(&self) -> bool {
        self.peak_nits > 100.0 && self.peak_nits <= 1000.0
    }

    /// Check if frame is HDR10+ or Dolby Vision (up to 10000 nits).
    pub fn is_extreme_hdr(&self) -> bool {
        self.peak_nits > 1000.0
    }

    /// Reset the meter.
    pub fn reset(&mut self) {
        self.peak_nits = 0.0;
        self.min_nits = f64::INFINITY;
        self.average_nits = 0.0;
        self.histogram.fill(0);
    }
}

/// Black and white level meter for broadcast compliance.
pub struct BlackWhiteLevelMeter {
    width: usize,
    height: usize,
    black_level: f64,
    white_level: f64,
    below_black_count: usize,
    above_white_count: usize,
    black_threshold: f64,
    white_threshold: f64,
}

impl BlackWhiteLevelMeter {
    /// Create a new black/white level meter.
    ///
    /// # Arguments
    ///
    /// * `width` - Frame width
    /// * `height` - Frame height
    /// * `black_threshold` - Black level threshold (e.g., 0 for digital, 16/255 for video)
    /// * `white_threshold` - White level threshold (e.g., 1.0 for digital, 235/255 for video)
    pub fn new(
        width: usize,
        height: usize,
        black_threshold: f64,
        white_threshold: f64,
    ) -> MeteringResult<Self> {
        if width == 0 || height == 0 {
            return Err(MeteringError::InvalidConfig(
                "Width and height must be positive".to_string(),
            ));
        }

        Ok(Self {
            width,
            height,
            black_level: 0.0,
            white_level: 0.0,
            below_black_count: 0,
            above_white_count: 0,
            black_threshold,
            white_threshold,
        })
    }

    /// Process a video frame (normalized 0.0 to 1.0).
    pub fn process(&mut self, frame: &Frame2D) -> MeteringResult<()> {
        let (height, width) = frame.dim();

        if width != self.width || height != self.height {
            return Err(MeteringError::InvalidConfig(
                "Frame dimensions don't match".to_string(),
            ));
        }

        self.below_black_count = 0;
        self.above_white_count = 0;
        let mut min_val = f64::INFINITY;
        let mut max_val = 0.0;

        for &value in frame.iter() {
            if value < min_val {
                min_val = value;
            }
            if value > max_val {
                max_val = value;
            }

            if value < self.black_threshold {
                self.below_black_count += 1;
            }
            if value > self.white_threshold {
                self.above_white_count += 1;
            }
        }

        self.black_level = min_val;
        self.white_level = max_val;

        Ok(())
    }

    /// Get the black level (minimum value).
    pub fn black_level(&self) -> f64 {
        self.black_level
    }

    /// Get the white level (maximum value).
    pub fn white_level(&self) -> f64 {
        self.white_level
    }

    /// Get the number of pixels below black threshold.
    pub fn below_black_count(&self) -> usize {
        self.below_black_count
    }

    /// Get the number of pixels above white threshold.
    pub fn above_white_count(&self) -> usize {
        self.above_white_count
    }

    /// Check if frame is compliant (no pixels outside legal range).
    pub fn is_compliant(&self) -> bool {
        self.below_black_count == 0 && self.above_white_count == 0
    }

    /// Get the percentage of illegal pixels.
    pub fn illegal_pixel_percentage(&self) -> f64 {
        let total_pixels = (self.width * self.height) as f64;
        let illegal_pixels = (self.below_black_count + self.above_white_count) as f64;
        (illegal_pixels / total_pixels) * 100.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_luminance_meter() {
        let mut meter =
            LuminanceMeter::new(1920, 1080, 1000.0, 256).expect("test expectation failed");

        // Create test frame with known values
        let frame = Frame2D::from_shape_fn(1080, 1920, |y, x| {
            (x + y) as f64 / (1920 + 1080) as f64 * 100.0
        });

        meter.process(&frame).expect("process should succeed");

        assert!(meter.peak_nits() > 0.0);
        assert!(meter.average_nits() > 0.0);
        assert!(meter.min_nits() >= 0.0);
    }

    #[test]
    fn test_sdr_detection() {
        let mut meter =
            LuminanceMeter::new(100, 100, 1000.0, 256).expect("test expectation failed");

        // SDR frame (max 100 nits)
        let frame = Frame2D::from_elem(100, 100, 80.0);
        meter.process(&frame).expect("process should succeed");

        assert!(meter.is_sdr());
        assert!(!meter.is_hdr10());
    }

    #[test]
    fn test_hdr_detection() {
        let mut meter =
            LuminanceMeter::new(100, 100, 1000.0, 256).expect("test expectation failed");

        // HDR frame (500 nits)
        let frame = Frame2D::from_elem(100, 100, 500.0);
        meter.process(&frame).expect("process should succeed");

        assert!(!meter.is_sdr());
        assert!(meter.is_hdr10());
    }

    #[test]
    fn test_contrast_ratio() {
        let mut meter =
            LuminanceMeter::new(100, 100, 1000.0, 256).expect("test expectation failed");

        // Frame with known contrast
        let mut frame = Frame2D::zeros(100, 100);
        frame.set(0, 0, 1.0); // Min
        frame.set(99, 99, 100.0); // Max

        meter.process(&frame).expect("process should succeed");

        assert_eq!(meter.peak_nits(), 100.0);
        assert_eq!(meter.min_nits(), 0.0);
    }

    #[test]
    fn test_black_white_level_meter() {
        let mut meter =
            BlackWhiteLevelMeter::new(100, 100, 0.0, 1.0).expect("test expectation failed");

        // Compliant frame
        let frame = Frame2D::from_elem(100, 100, 0.5);
        meter.process(&frame).expect("process should succeed");

        assert!(meter.is_compliant());
        assert_eq!(meter.illegal_pixel_percentage(), 0.0);
    }

    #[test]
    fn test_illegal_pixels() {
        let mut meter =
            BlackWhiteLevelMeter::new(100, 100, 0.0, 1.0).expect("test expectation failed");

        // Frame with illegal pixels
        let mut frame = Frame2D::from_elem(100, 100, 0.5);
        frame.set(0, 0, -0.1); // Below black
        frame.set(99, 99, 1.1); // Above white

        meter.process(&frame).expect("process should succeed");

        assert!(!meter.is_compliant());
        assert!(meter.below_black_count() > 0);
        assert!(meter.above_white_count() > 0);
    }
}