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
use crate::{FrameData, FrameDataSource, ImageData, Timestamp};
use color_eyre::{
    eyre::{self as anyhow, WrapErr},
    Result,
};
use fmf::reader::FMFReader;
use std::path::Path;
use timestamped_frame::ExtraTimeData;

struct FmfSourceIter {
    rdr: FMFReader,
    frame0_time_utc: chrono::DateTime<chrono::Utc>,
    idx: usize,
}
impl FmfSourceIter {
    fn new(parent: &FmfSource) -> Result<Self> {
        let mut rdr = FMFReader::new(&parent.filename).with_context(|| {
            anyhow::anyhow!(
                "Error from FMFReader opening '{}'",
                parent.filename.display()
            )
        })?;
        let frame0_time_utc = parent.frame0_time_utc;
        for _ in 0..parent.skip_frames {
            rdr.next();
        }
        Ok(Self {
            rdr,
            frame0_time_utc,
            idx: 0,
        })
    }
}
impl Iterator for FmfSourceIter {
    type Item = Result<FrameData>;
    fn next(&mut self) -> Option<Self::Item> {
        let pos_start = self.rdr.file_pos();
        self.rdr.next().map(|fmf_result| match fmf_result {
            Ok(frame) => {
                let pos_end = self.rdr.file_pos();
                let buf_len = pos_end - pos_start;
                let frame_time_utc = frame.extra().host_timestamp();
                let timestamp = frame_time_utc - self.frame0_time_utc;
                let timestamp = Timestamp::Duration(timestamp.to_std()?);
                let idx = self.idx;
                self.idx += 1;
                Ok(FrameData {
                    image: ImageData::Decoded(frame),
                    timestamp,
                    buf_len,
                    idx,
                })
            }
            Err(e) => Err(anyhow::Error::from(e)),
        })
    }
}

// Because of the need to create an iterator over the frames an arbitrary number
// of times but the inability of `FMFReader` to seek (due to underlying
// potential use of a .gz file reader which does not support seeking), we store
// the filename and repeatedly reopen the file as necessary. An an optimization,
// the opened reader and its last read frame could be kept in a cache. This
// would reduce the number of re-openings.
pub struct FmfSource {
    filename: std::path::PathBuf,
    width: u32,
    height: u32,
    frame0_time_utc: chrono::DateTime<chrono::Utc>,
    frame0_time: chrono::DateTime<chrono::FixedOffset>,
    skip_frames: usize,
}

impl FrameDataSource for FmfSource {
    fn width(&self) -> u32 {
        self.width
    }
    fn height(&self) -> u32 {
        self.height
    }
    fn frame0_time(&self) -> Option<chrono::DateTime<chrono::FixedOffset>> {
        Some(self.frame0_time)
    }
    fn skip_n_frames(&mut self, n_frames: usize) -> Result<()> {
        if n_frames == 0 {
            return Ok(());
        }
        let mut rdr = FMFReader::new(&self.filename).with_context(|| {
            anyhow::anyhow!("Error from FMFReader opening '{}'", self.filename.display())
        })?;

        let mut frame = None;
        for _ in 0..n_frames {
            frame = rdr.next()
        }

        let frame = frame
            .map(|f| f.map_err(anyhow::Error::from))
            .unwrap_or_else(|| {
                anyhow::bail!(
                    "fmf file without {n_frames} of data '{}'",
                    self.filename.display()
                )
            })?;

        let frame_time_utc = frame.extra().host_timestamp();
        let duration = frame_time_utc - self.frame0_time_utc;
        let frame_time = self.frame0_time + duration;

        self.skip_frames = n_frames;
        self.frame0_time = frame_time;
        self.frame0_time_utc = frame_time_utc;
        Ok(())
    }
    fn estimate_luminance_range(&mut self) -> Result<(u16, u16)> {
        // FMF reader does not support seek because we may read .gz files.
        anyhow::bail!("estimating luminance range not supported for FMF source.");
    }
    fn iter(&mut self) -> Box<dyn Iterator<Item = Result<FrameData>>> {
        Box::new(FmfSourceIter::new(self).unwrap())
    }
    fn timestamp_source(&self) -> &str {
        "FMF frame metadata"
    }
    fn has_timestamps(&self) -> bool {
        true
    }
}

impl FmfSource {
    fn new<P: AsRef<std::path::Path>>(filename: P) -> Result<Self> {
        let filename = filename.as_ref().to_path_buf();
        let mut rdr = FMFReader::new(&filename).with_context(|| {
            anyhow::anyhow!("Error from FMFReader opening '{}'", filename.display())
        })?;
        let width = rdr.width();
        let height = rdr.height();
        let frame0 = rdr
            .next()
            .map(|f| f.map_err(anyhow::Error::from))
            .unwrap_or_else(|| anyhow::bail!("fmf file with no data '{}'", filename.display()))?;

        let frame0_time_utc = frame0.extra().host_timestamp();
        let frame0_time = mkv_strand_reader::infer_timezone(&frame0_time_utc, filename.to_str())?;

        Ok(Self {
            filename,
            width,
            height,
            frame0_time_utc,
            frame0_time,
            skip_frames: 0,
        })
    }
}

pub fn from_path<P: AsRef<Path>>(path: P) -> Result<FmfSource> {
    let filename = path.as_ref();
    FmfSource::new(filename).with_context(|| format!("Reading FMF file {}", filename.display()))
}