raw: dng_metadata::Timecode is the crate Timecode; share the CinemaDNG frame-rate range with dng.rs

Removes the duplicated Timecode type and BCD decoder (Sonar duplication 28%) and the copied
frame-rate plausibility check; capture_info is tested against the full reader's CinemaTags.
This commit is contained in:
FredApps
2026-11-16 08:35:12 +01:00
parent 77125b817f
commit 7feff2a02d
3 changed files with 47 additions and 33 deletions
+7 -1
View File
@@ -500,6 +500,12 @@ const FRAME_RATE: u16 = 51044;
/// CinemaDNG `ReelName` (tag 51081).
const REEL_NAME: u16 = 51081;
/// A CinemaDNG `FrameRate` of `n / d` frames a second between 1/1000 and 1000 (shared with the
/// bounded `dng_metadata` reader).
pub(crate) fn plausible_frame_rate(n: i64, d: i64) -> bool {
n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d
}
/// The CinemaDNG tags of a frame, from IFD 0 (where the specification puts them), else the raw IFD. A frame rate
/// outside 1/1000 to 1000 frames a second and a time code that is not valid BCD are ignored.
fn cinema_tags(ifd0: &Ifd, raw: &Ifd) -> CinemaTags {
@@ -510,7 +516,7 @@ fn cinema_tags(ifd0: &Ifd, raw: &Ifd) -> CinemaTags {
_ => None,
});
CinemaTags {
frame_rate: frame_rate.filter(|&(n, d)| n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d),
frame_rate: frame_rate.filter(|&(n, d)| plausible_frame_rate(n, d)),
timecode: pick(TIME_CODES).bytes(TIME_CODES).and_then(Timecode::from_bytes),
reel_name: pick(REEL_NAME).string(REEL_NAME).map(|s| s.trim().to_owned()).filter(|s| !s.is_empty()),
}
+9 -32
View File
@@ -43,29 +43,9 @@ pub struct Calibration {
pub as_shot_white_xy: Option<(f64, f64)>,
pub baseline_exposure: f64,
}
/// SMPTE ST 12-1 time code of a CinemaDNG frame.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Timecode {
pub hours: u8,
pub minutes: u8,
pub seconds: u8,
pub frames: u8,
pub drop_frame: bool,
}
impl Timecode {
/// The first time code of a CinemaDNG TimeCodes tag (SMPTE ST 331 / 12-1 packing: BCD frames,
/// seconds, minutes, hours in bytes 0–3, the drop-frame flag in bit 6 of byte 0).
pub fn from_bytes(b: &[u8]) -> Option<Timecode> {
let bcd = |v: u8, tens_mask: u8| {
let (u, t) = (v & 0x0F, (v >> 4) & tens_mask);
(u <= 9).then_some(t * 10 + u)
};
let [f, s, m, h, ..] = *b else { return None };
let tc = Timecode { frames: bcd(f, 0x3)?, seconds: bcd(s, 0x7)?, minutes: bcd(m, 0x7)?, hours: bcd(h, 0x3)?, drop_frame: f & 0x40 != 0 };
(tc.seconds < 60 && tc.minutes < 60 && tc.hours < 24).then_some(tc)
}
}
/// SMPTE ST 12-1 time code of a CinemaDNG frame: the crate's [`crate::Timecode`], which the full
/// reader's `CinemaTags` uses too.
pub use crate::Timecode;
/// File-level capture fields, without image geometry or rendering policy.
#[derive(Clone, Debug, PartialEq)]
@@ -217,7 +197,7 @@ impl Metadata<'_, '_> {
}
let n = i64::from(self.tiff.u32_of(b.get(0..4)?.try_into().ok()?) as i32);
let d = i64::from(self.tiff.u32_of(b.get(4..8)?.try_into().ok()?) as i32);
(n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d).then_some((n, d))
crate::dng::plausible_frame_rate(n, d).then_some((n, d))
});
let timecode = self.main_tag(tags::TIME_CODES).and_then(|e| self.tiff.value_bytes(e).ok()).and_then(Timecode::from_bytes);
let camera = self.text(tags::UNIQUE_CAMERA_MODEL).or_else(|| {
@@ -252,14 +232,11 @@ mod tests {
use super::*;
#[test]
fn timecode_bcd_and_limits() {
assert_eq!(
Timecode::from_bytes(&[0x52, 0x34, 0x12, 0x09]),
Some(Timecode { frames: 12, seconds: 34, minutes: 12, hours: 9, drop_frame: true })
);
assert_eq!(Timecode::from_bytes(&[0, 0x60, 0, 0]), None);
assert_eq!(Timecode::from_bytes(&[0x0a, 0, 0, 0]), None);
assert_eq!(Timecode::from_bytes(&[0, 0, 0]), None);
fn timecode_is_the_crate_timecode() {
let tc = Timecode { frames: 12, seconds: 34, minutes: 12, hours: 9, drop_frame: true };
assert_eq!(Timecode::from_bytes(&[0x52, 0x34, 0x12, 0x09]), Some(tc));
let shared: crate::Timecode = tc;
assert_eq!(Timecode::from_bytes(&shared.to_bytes()), Some(tc));
}
#[test]
+31
View File
@@ -112,3 +112,34 @@ fn calibration_matches_the_full_reader() {
assert_eq!(c.forward_matrix, [None, None]);
assert_eq!(c.camera_calibration, [None, None]);
}
#[test]
fn capture_info_matches_the_full_readers_cinema_tags() {
use craft_raw::dng_metadata::Timecode;
let tc = Timecode { hours: 10, minutes: 59, seconds: 7, frames: 23, drop_frame: true };
let bytes = dng_with(cfa_raw(|_| {}), ByteOrder::Big, |ifd0| {
ifd0.set(ct::FRAME_RATE, Value::SRational(vec![(24000, 1001)]));
ifd0.set(ct::TIME_CODES, Value::Byte(tc.to_bytes().to_vec()));
ifd0.set(ct::REEL_NAME, Value::Ascii("A001".into()));
ifd0.set(ct::MODEL, Value::Ascii("Cam".into()));
});
let info = with_metadata(&bytes, |m| m.capture_info());
let cinema = craft_raw::decode(&bytes).unwrap().cinema;
assert_eq!(info.dng_version, [1, 6, 0, 0]);
assert_eq!(info.frame_rate, Some((24000, 1001)));
assert_eq!((info.frame_rate, info.timecode), (cinema.frame_rate, cinema.timecode));
assert_eq!(info.timecode, Some(tc));
assert_eq!(info.reel.as_deref(), cinema.reel_name.as_deref());
assert_eq!(info.camera.as_deref(), Some("Hand Cam"));
// an implausible rate and a broken time code are dropped by both; UniqueCameraModel wins
let bytes = dng_with(cfa_raw(|_| {}), ByteOrder::Little, |ifd0| {
ifd0.set(ct::FRAME_RATE, Value::Rational(vec![(5000, 1)]));
ifd0.set(ct::TIME_CODES, Value::Byte(vec![0x0a, 0, 0, 0, 0, 0, 0, 0]));
ifd0.set(ct::UNIQUE_CAMERA_MODEL, Value::Ascii("Hand Cam Mk II".into()));
});
let info = with_metadata(&bytes, |m| m.capture_info());
assert_eq!((info.frame_rate, info.timecode, info.reel), (None, None, None));
assert_eq!(info.camera.as_deref(), Some("Hand Cam Mk II"));
let cinema = craft_raw::decode(&bytes).unwrap().cinema;
assert_eq!((cinema.frame_rate, cinema.timecode), (None, None));
}