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
use crate::transcoder::source::video::SourceError;
use crate::transcoder::source::video::Video;
use crate::transcoder::source::video::{Source, VideoBuilder};
use adder_codec_core::Mode::FramePerfect;
use adder_codec_core::{DeltaT, Event, PixelMultiMode, PlaneSize, SourceCamera, TimeMode};

use crate::utils::viz::ShowFeatureMode;
use adder_codec_core::codec::{EncoderOptions, EncoderType};

use crate::utils::cv::handle_color;
#[cfg(feature = "feature-logging")]
use crate::utils::cv::{calculate_quality_metrics, QualityMetrics};

use rayon::ThreadPool;
use std::io::Write;
use std::path::PathBuf;

#[cfg(feature = "feature-logging")]
use chrono::Local;
use video_rs_adder_dep::{self, Decoder, Frame, Locator, Options, Resize};

/// Attributes of a framed video -> ADΔER transcode
pub struct Framed<W: Write + 'static> {
    cap: Decoder,
    pub(crate) input_frame: Frame,

    /// Index of the first frame to be read from the input video
    pub frame_idx_start: u32,

    /// FPS of the input video. Set automatically by `Framed::new()`
    pub source_fps: f32,

    /// Scale of the input video. Input frames are resized to this scale before transcoding.
    pub scale: f64,

    /// Whether the input video is color
    color_input: bool,

    pub(crate) video: Video<W>,
}
unsafe impl<W: Write> Sync for Framed<W> {}

impl<W: Write + 'static> Framed<W> {
    /// Create a new `Framed` source
    pub fn new(
        input_filename: String,
        color_input: bool,
        scale: f64,
    ) -> Result<Framed<W>, SourceError> {
        let source = Locator::Path(PathBuf::from(input_filename));
        let mut cap = Decoder::new(&source)?;
        let (width, height) = cap.size();
        let width = ((width as f64) * scale) as u32;
        let height = ((height as f64) * scale) as u32;

        cap = Decoder::new_with_options_and_resize(
            &source,
            &Options::default(),
            Resize::Fit(width, height),
        )?;

        // Calculate TPS based on ticks per frame and source FPS
        let source_fps = cap.frame_rate();
        let (width, height) = cap.size_out();

        let plane = PlaneSize::new(width as u16, height as u16, if color_input { 3 } else { 1 })?;

        let video = Video::new(plane, FramePerfect, None)?;

        Ok(Framed {
            cap,
            input_frame: Frame::default((height as usize, width as usize, 3)), // Note that this will be limited to 8-bit precision (due to video-rs crate)
            frame_idx_start: 0,
            source_fps,
            scale,
            color_input,
            video,
        })
    }

    /// Set the start frame of the source
    pub fn frame_start(mut self, frame_idx_start: u32) -> Result<Self, SourceError> {
        let video_frame_count = self.cap.frame_count();
        if frame_idx_start >= video_frame_count as u32 {
            return Err(SourceError::StartOutOfBounds(frame_idx_start));
        };
        let ts_millis = (frame_idx_start as f32 / self.source_fps * 1000.0) as i64;
        self.cap.reader.seek(ts_millis)?;

        self.frame_idx_start = frame_idx_start;
        Ok(self)
    }

    /// Automatically derive the ticks per second from the source FPS and `ref_time`
    pub fn auto_time_parameters(
        mut self,
        ref_time: DeltaT,
        delta_t_max: DeltaT,
        time_mode: Option<TimeMode>,
    ) -> Result<Self, SourceError> {
        if delta_t_max % ref_time == 0 {
            let tps = (ref_time as f32 * self.source_fps) as DeltaT;
            self.video = self
                .video
                .time_parameters(tps, ref_time, delta_t_max, time_mode)?;
        } else {
            return Err(SourceError::BadParams(
                "delta_t_max must be a multiple of ref_time".to_string(),
            ));
        }
        Ok(self)
    }

    /// Get the number of ticks each frame is said to span
    pub fn get_ref_time(&self) -> u32 {
        self.video.state.params.ref_time
    }

    pub fn get_last_input_frame(&self) -> &Frame {
        &self.input_frame
    }
}

impl<W: Write + 'static> Source<W> for Framed<W> {
    /// Get pixel-wise intensities directly from source frame, and integrate them with
    /// `ref_time` (the number of ticks each frame is said to span)
    fn consume(
        &mut self,
        view_interval: u32,
        thread_pool: &ThreadPool,
    ) -> Result<Vec<Vec<Event>>, SourceError> {
        let (_, frame) = self.cap.decode()?;
        self.input_frame = handle_color(frame, self.color_input)?;

        let res = thread_pool.install(|| {
            self.video.integrate_matrix(
                self.input_frame.clone(),
                self.video.state.params.ref_time as f32,
                view_interval,
            )
        });
        #[cfg(feature = "feature-logging")]
        {
            if let Some(handle) = &mut self.video.state.feature_log_handle {
                // Calculate the quality metrics
                let mut image_mat = self.video.state.running_intensities.clone();

                #[rustfmt::skip]
                    let metrics = calculate_quality_metrics(
                    &self.input_frame,
                    &image_mat,
                    QualityMetrics {
                        mse: Some(0.0),
                        psnr: Some(0.0),
                        ssim: None,
                    });

                let metrics = metrics.unwrap();
                let bytes = serde_pickle::to_vec(&metrics, Default::default()).unwrap();
                handle.write_all(&bytes).unwrap();
            }
        }
        res
    }

    fn crf(&mut self, crf: u8) {
        self.video.update_crf(crf);
    }

    fn get_video_mut(&mut self) -> &mut Video<W> {
        &mut self.video
    }

    fn get_video_ref(&self) -> &Video<W> {
        &self.video
    }

    fn get_video(self) -> Video<W> {
        todo!()
    }

    fn get_input(&self) -> Option<&Frame> {
        Some(self.get_last_input_frame())
    }

    fn get_running_input_bitrate(&self) -> f64 {
        let video = self.get_video_ref();
        video.get_tps() as f64 / video.get_ref_time() as f64
            * video.state.plane.volume() as f64
            * 8.0
    }
}

impl<W: Write + 'static> VideoBuilder<W> for Framed<W> {
    fn contrast_thresholds(mut self, c_thresh_pos: u8, _c_thresh_neg: u8) -> Self {
        self.video = self.video.c_thresh_pos(c_thresh_pos);
        // self.video = self.video.c_thresh_neg(c_thresh_neg);
        self
    }

    fn crf(mut self, crf: u8) -> Self {
        self.video.update_crf(crf);
        self
    }

    fn quality_manual(
        mut self,
        c_thresh_baseline: u8,
        c_thresh_max: u8,
        delta_t_max_multiplier: u32,
        c_increase_velocity: u8,
        feature_c_radius_denom: f32,
    ) -> Self {
        self.video.update_quality_manual(
            c_thresh_baseline,
            c_thresh_max,
            delta_t_max_multiplier,
            c_increase_velocity,
            feature_c_radius_denom,
        );
        self
    }

    fn c_thresh_pos(mut self, c_thresh_pos: u8) -> Self {
        self.video = self.video.c_thresh_pos(c_thresh_pos);
        self
    }

    fn c_thresh_neg(mut self, c_thresh_neg: u8) -> Self {
        self.video = self.video.c_thresh_neg(c_thresh_neg);
        self
    }

    fn chunk_rows(mut self, chunk_rows: usize) -> Self {
        self.video = self.video.chunk_rows(chunk_rows);
        self
    }

    fn time_parameters(
        mut self,
        tps: DeltaT,
        ref_time: DeltaT,
        delta_t_max: DeltaT,
        time_mode: Option<TimeMode>,
    ) -> Result<Self, SourceError> {
        if delta_t_max % ref_time == 0 {
            self.video = self
                .video
                .time_parameters(tps, ref_time, delta_t_max, time_mode)?;
        } else {
            eprintln!("delta_t_max must be a multiple of ref_time");
        }
        Ok(self)
    }

    fn write_out(
        mut self,
        source_camera: SourceCamera,
        time_mode: TimeMode,
        pixel_multi_mode: PixelMultiMode,
        adu_interval: Option<usize>,
        encoder_type: EncoderType,
        encoder_options: EncoderOptions,
        write: W,
    ) -> Result<Box<Self>, SourceError> {
        self.video = self.video.write_out(
            Some(source_camera),
            Some(time_mode),
            Some(pixel_multi_mode),
            adu_interval,
            encoder_type,
            encoder_options,
            write,
        )?;
        Ok(Box::new(self))
    }

    fn show_display(mut self, show_display: bool) -> Self {
        self.video = self.video.show_display(show_display);
        self
    }

    fn detect_features(mut self, detect_features: bool, show_features: ShowFeatureMode) -> Self {
        self.video = self.video.detect_features(detect_features, show_features);
        self
    }

    #[cfg(feature = "feature-logging")]
    fn log_path(mut self, name: String) -> Self {
        let date_time = Local::now();
        let formatted = format!("{}_{}.log", name, date_time.format("%d_%m_%Y_%H_%M_%S"));
        let log_handle = std::fs::File::create(formatted).ok();
        self.video.state.feature_log_handle = log_handle;

        // Write the plane size to the log file
        if let Some(handle) = &mut self.video.state.feature_log_handle {
            writeln!(
                handle,
                "{}x{}x{}",
                self.video.state.plane.w(),
                self.video.state.plane.h(),
                self.video.state.plane.c()
            )
            .unwrap();
        }
        self
    }
}