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
use crate::transcoder::source::video::Source;
use crate::transcoder::source::video::SourceError;
use crate::transcoder::source::video::Video;
use crate::{Coord, Event};
use core::default::Default;
use std::mem::swap;
use crate::transcoder::source::video::SourceError::*;
use opencv::core::{Mat, Size};
use opencv::videoio::{VideoCapture, CAP_PROP_FPS, CAP_PROP_FRAME_COUNT, CAP_PROP_POS_FRAMES};
use opencv::{imgproc, prelude::*, videoio, Result};
use rayon::ThreadPool;
use crate::transcoder::d_controller::DecimationMode;
use crate::transcoder::event_pixel_tree::DeltaT;
use crate::transcoder::event_pixel_tree::Mode::FramePerfect;
use crate::SourceCamera;
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct IndirectCoord {
pub(crate) forward: Coord,
pub(crate) reverse: Coord,
}
pub struct FramedSource {
cap: VideoCapture,
pub(crate) input_frame_scaled: Mat,
pub(crate) input_frame: Mat,
pub frame_idx_start: u32,
pub source_fps: f64,
pub scale: f64,
color_input: bool,
pub(crate) video: Video,
}
unsafe impl Sync for FramedSource {}
pub struct FramedSourceBuilder {
input_filename: String,
output_events_filename: Option<String>,
frame_idx_start: u32,
chunk_rows: usize,
ref_time: DeltaT,
tps: DeltaT,
delta_t_max: DeltaT,
scale: f64,
frame_skip_interval: u8,
color_input: bool,
c_thresh_pos: u8,
c_thresh_neg: u8,
write_out: bool,
communicate_events: bool,
show_display_b: bool,
source_camera: SourceCamera,
}
impl FramedSourceBuilder {
pub fn new(input_filename: String, source_camera: SourceCamera) -> FramedSourceBuilder {
FramedSourceBuilder {
input_filename,
output_events_filename: None,
frame_idx_start: 0,
chunk_rows: 0,
ref_time: 5000,
tps: 150000,
delta_t_max: 150000,
scale: 1.0,
frame_skip_interval: 0,
color_input: true,
c_thresh_pos: 0,
c_thresh_neg: 0,
write_out: false,
communicate_events: false,
show_display_b: false,
source_camera,
}
}
pub fn output_events_filename(mut self, output_events_filename: String) -> FramedSourceBuilder {
self.output_events_filename = Some(output_events_filename);
self.write_out = true;
self
}
pub fn frame_start(mut self, frame_idx_start: u32) -> FramedSourceBuilder {
self.frame_idx_start = frame_idx_start;
self
}
pub fn chunk_rows(mut self, chunk_rows: usize) -> FramedSourceBuilder {
self.chunk_rows = chunk_rows;
self
}
pub fn time_parameters(mut self, ref_time: DeltaT, delta_t_max: DeltaT) -> FramedSourceBuilder {
self.delta_t_max = delta_t_max;
self.ref_time = ref_time;
assert_eq!(self.delta_t_max % self.ref_time, 0);
self
}
pub fn contrast_thresholds(
mut self,
c_thresh_pos: u8,
c_thresh_neg: u8,
) -> FramedSourceBuilder {
self.c_thresh_pos = c_thresh_pos;
self.c_thresh_neg = c_thresh_neg;
self
}
pub fn scale(mut self, scale: f64) -> FramedSourceBuilder {
self.scale = scale;
self
}
pub fn skip_interval(mut self, frame_skip_interval: u8) -> FramedSourceBuilder {
self.frame_skip_interval = frame_skip_interval;
self
}
pub fn color(mut self, color_input: bool) -> FramedSourceBuilder {
self.color_input = color_input;
self
}
pub fn communicate_events(mut self, communicate_events: bool) -> FramedSourceBuilder {
self.communicate_events = communicate_events;
self
}
pub fn show_display(mut self, show_display_b: bool) -> FramedSourceBuilder {
self.show_display_b = show_display_b;
self
}
pub fn finish(self) -> Result<FramedSource> {
FramedSource::new(self)
}
}
impl FramedSource {
fn new(mut builder: FramedSourceBuilder) -> Result<FramedSource> {
let channels = match builder.color_input {
true => 3,
false => 1,
};
let mut cap =
videoio::VideoCapture::from_file(builder.input_filename.as_str(), videoio::CAP_FFMPEG)?;
let video_frame_count = cap.get(CAP_PROP_FRAME_COUNT).unwrap();
assert!(builder.frame_idx_start < video_frame_count as u32);
cap.set(CAP_PROP_POS_FRAMES, builder.frame_idx_start as f64)
.unwrap();
let source_fps = cap.get(CAP_PROP_FPS).unwrap().round();
builder.tps = builder.ref_time * source_fps as u32;
assert_eq!(
builder.ref_time * cap.get(CAP_PROP_FPS).unwrap().round() as u32,
builder.tps
);
let opened = videoio::VideoCapture::is_opened(&cap)?;
if !opened {
panic!("Could not open source")
}
let mut init_frame = Mat::default();
match cap.read(&mut init_frame) {
Ok(_) => {}
Err(e) => {
panic!("{}", e);
}
};
cap.set(CAP_PROP_POS_FRAMES, builder.frame_idx_start as f64)
.unwrap();
let mut init_frame_scaled = Mat::default();
println!("Original width is {}", init_frame.size()?.width);
resize_input(&mut init_frame, &mut init_frame_scaled, builder.scale).unwrap();
init_frame = init_frame_scaled;
println!("Width is {}", init_frame.size()?.width);
let video = Video::new(
init_frame.size()?.width as u16,
init_frame.size()?.height as u16,
builder.chunk_rows,
builder.output_events_filename,
channels,
builder.tps,
builder.ref_time,
builder.delta_t_max,
DecimationMode::Manual,
builder.write_out,
builder.communicate_events,
builder.show_display_b,
builder.source_camera,
builder.c_thresh_pos,
builder.c_thresh_neg,
);
Ok(FramedSource {
cap,
input_frame_scaled: Default::default(),
input_frame: Default::default(),
frame_idx_start: builder.frame_idx_start,
source_fps,
scale: builder.scale,
color_input: builder.color_input,
video,
})
}
pub fn get_ref_time(&self) -> u32 {
self.video.ref_time
}
}
impl Source for FramedSource {
fn consume(
&mut self,
view_interval: u32,
thread_pool: &ThreadPool,
) -> Result<Vec<Vec<Event>>, SourceError> {
match self.cap.read(&mut self.input_frame) {
Ok(_) => {
match resize_frame(
&self.input_frame,
&mut self.input_frame_scaled,
self.color_input,
self.scale,
) {
Ok(_) => {}
Err(_) => return Err(SourceError::NoData),
}
}
Err(e) => {
panic!("{}", e);
}
};
if self.input_frame_scaled.empty() {
eprintln!("End of video");
return Err(BufferEmpty);
}
let tmp = self.input_frame_scaled.clone();
thread_pool.install(|| {
self.video.integrate_matrix(
tmp,
self.video.ref_time as f32,
FramePerfect,
view_interval,
)
})
}
fn get_video_mut(&mut self) -> &mut Video {
&mut self.video
}
fn get_video(&self) -> &Video {
&self.video
}
}
fn resize_input(
input_frame_gray: &mut Mat,
input_frame_scaled: &mut Mat,
resize_scale: f64,
) -> Result<(), opencv::Error> {
if resize_scale != 1.0 {
opencv::imgproc::resize(
input_frame_gray,
input_frame_scaled,
Size {
width: 0,
height: 0,
},
resize_scale,
resize_scale,
0,
)?;
} else {
swap(input_frame_gray, input_frame_scaled);
}
Ok(())
}
fn resize_frame(
input: &Mat,
output: &mut Mat,
color: bool,
scale: f64,
) -> Result<(), opencv::Error> {
let mut holder = Mat::default();
if !color {
imgproc::cvt_color(&input, &mut holder, imgproc::COLOR_BGR2GRAY, 1)?;
} else {
holder = input.clone();
}
resize_input(&mut holder, output, scale)?;
Ok(())
}