1
+ use adder_codec_core:: codec:: CodecMetadata ;
1
2
use adder_codec_core:: * ;
2
- use adder_codec_rs:: transcoder:: source:: video:: show_display_force;
3
- use adder_codec_rs:: utils:: viz:: { encode_video_ffmpeg, write_frame_to_video} ;
4
3
use clap:: Parser ;
5
4
use ndarray:: Array3 ;
6
- use opencv:: core:: { Mat , MatTrait , MatTraitManual , CV_8U , CV_8UC3 } ;
7
5
use std:: cmp:: max;
8
6
use std:: collections:: VecDeque ;
9
7
use std:: error:: Error ;
10
8
use std:: fs:: File ;
11
9
use std:: io:: { BufWriter , Write } ;
12
10
use std:: option:: Option ;
11
+ use std:: path:: PathBuf ;
13
12
use std:: { error, io} ;
13
+ use video_rs:: { Encoder , EncoderSettings , Options , PixelFormat } ;
14
14
15
15
/// Command line argument parser
16
- #[ derive( Parser , Debug , Default ) ]
16
+ #[ derive( Parser , Debug , Default , Clone ) ]
17
17
#[ clap( author, version, about, long_about = None ) ]
18
18
pub struct MyArgs {
19
19
/// Input ADΔER video path
@@ -49,11 +49,11 @@ struct DvsPixel {
49
49
#[ allow( dead_code) ]
50
50
fn main ( ) -> Result < ( ) , Box < dyn error:: Error > > {
51
51
let args: MyArgs = MyArgs :: parse ( ) ;
52
+ dbg ! ( args. clone( ) ) ;
52
53
let file_path = args. input . as_str ( ) ;
53
54
54
55
let output_text_path = args. output_text . as_str ( ) ;
55
56
let output_video_path = args. output_video . as_str ( ) ;
56
- let raw_path = "./dvs.gray8" ;
57
57
58
58
let ( mut stream, mut bitreader) = open_file_decoder ( file_path) ?;
59
59
@@ -72,8 +72,23 @@ fn main() -> Result<(), Box<dyn error::Error>> {
72
72
73
73
stream. set_input_stream_position ( & mut bitreader, first_event_position) ?;
74
74
75
- let mut video_writer: Option < BufWriter < File > > = match File :: create ( raw_path) {
76
- Ok ( file) => Some ( BufWriter :: new ( file) ) ,
75
+ let mut video_writer: Option < Encoder > = match File :: create ( output_video_path) {
76
+ Ok ( _) => {
77
+ let mut options = std:: collections:: HashMap :: new ( ) ;
78
+ options. insert ( "crf" . to_string ( ) , "0" . to_string ( ) ) ;
79
+ options. insert ( "preset" . to_string ( ) , "veryslow" . to_string ( ) ) ;
80
+ options. insert ( "qp" . to_string ( ) , "0" . to_string ( ) ) ;
81
+ let opts: Options = options. into ( ) ;
82
+
83
+ let settings = EncoderSettings :: for_h264_custom (
84
+ meta. plane . w_usize ( ) ,
85
+ meta. plane . h_usize ( ) ,
86
+ PixelFormat :: YUV420P ,
87
+ opts,
88
+ ) ;
89
+ let encoder = Encoder :: new ( & PathBuf :: from ( output_video_path) . into ( ) , settings) ?;
90
+ Some ( encoder)
91
+ }
77
92
Err ( _) => None ,
78
93
} ;
79
94
let mut text_writer: BufWriter < File > = BufWriter :: new ( File :: create ( output_text_path) ?) ;
@@ -115,44 +130,12 @@ fn main() -> Result<(), Box<dyn error::Error>> {
115
130
meta. plane . c ( ) . into ( ) ,
116
131
) ) ;
117
132
118
- let mut instantaneous_frame_deque = {
119
- let mut instantaneous_frame = Mat :: default ( ) ;
120
- match meta. plane . c ( ) {
121
- 1 => unsafe {
122
- instantaneous_frame. create_rows_cols (
123
- meta. plane . h ( ) as i32 ,
124
- meta. plane . w ( ) as i32 ,
125
- CV_8U ,
126
- ) ?;
127
- } ,
128
- _ => unsafe {
129
- instantaneous_frame. create_rows_cols (
130
- meta. plane . h ( ) as i32 ,
131
- meta. plane . w ( ) as i32 ,
132
- CV_8UC3 ,
133
- ) ?;
134
- } ,
135
- }
136
-
137
- VecDeque :: from ( [ instantaneous_frame] )
138
- } ;
139
-
140
- match instantaneous_frame_deque
141
- . back_mut ( )
142
- . expect ( "Could not get back of deque" )
143
- . data_bytes_mut ( )
144
- {
145
- Ok ( bytes) => {
146
- for byte in bytes {
147
- * byte = 128 ;
148
- }
149
- }
150
- Err ( e) => {
151
- return Err ( Box :: new ( e) ) ;
152
- }
153
- } ;
133
+ let mut instantaneous_frame_deque = VecDeque :: from ( [ create_blank_dvs_frame ( & meta) ?] ) ;
154
134
155
135
let frame_length = ( meta. tps as f32 / args. fps ) as u128 ; // length in ticks
136
+ let frame_duration = 1.0 / args. fps as f64 ; // length in seconds
137
+
138
+ let mut current_frame_time = 0.0 ;
156
139
let mut frame_count = 0_usize ;
157
140
let mut current_t = 0 ;
158
141
let mut max_px_event_count = 0 ;
@@ -170,13 +153,18 @@ fn main() -> Result<(), Box<dyn error::Error>> {
170
153
match instantaneous_frame_deque. pop_front ( ) {
171
154
None => { }
172
155
Some ( frame) => {
173
- if args. show_display {
174
- show_display_force ( "DVS" , & frame, 1 ) ?;
175
- }
156
+ // if args.show_display {
157
+ // show_display_force("DVS", &frame, 1)?;
158
+ // }
176
159
match video_writer {
177
160
None => { }
178
- Some ( ref mut writer) => {
179
- write_frame_to_video ( & frame, writer) ?;
161
+ Some ( ref mut encoder) => {
162
+ write_frame_to_video (
163
+ & frame,
164
+ encoder,
165
+ video_rs:: Time :: from_secs_f64 ( current_frame_time) ,
166
+ ) ?;
167
+ current_frame_time += frame_duration;
180
168
}
181
169
}
182
170
}
@@ -185,7 +173,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
185
173
}
186
174
187
175
match stream. digest_event ( & mut bitreader) {
188
- Ok ( event) => {
176
+ Ok ( mut event) => {
189
177
event_count += 1 ;
190
178
let y = event. coord . y as usize ;
191
179
let x = event. coord . x as usize ;
@@ -208,7 +196,23 @@ fn main() -> Result<(), Box<dyn error::Error>> {
208
196
}
209
197
} ,
210
198
Some ( px) => {
211
- px. t += event. t as u128 ;
199
+ if meta. time_mode == TimeMode :: DeltaT {
200
+ px. t += event. t as u128 ;
201
+ } else {
202
+ let tmp = px. t ;
203
+ px. t = event. t as u128 ;
204
+ event. t -= tmp as u32 ;
205
+ }
206
+
207
+ if is_framed ( meta. source_camera ) {
208
+ px. t = if px. t % meta. ref_interval as u128 == 0 {
209
+ px. t
210
+ } else {
211
+ ( ( ( px. t / meta. ref_interval as u128 ) + 1 )
212
+ * meta. ref_interval as u128 )
213
+ } ;
214
+ }
215
+
212
216
current_t = max ( px. t , current_t) ;
213
217
let frame_idx = ( px. t / frame_length) as usize ;
214
218
@@ -228,6 +232,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
228
232
// Fire a positive polarity event
229
233
set_instant_dvs_pixel (
230
234
event,
235
+ & meta,
231
236
& mut instantaneous_frame_deque,
232
237
frame_idx,
233
238
frame_count,
@@ -250,6 +255,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
250
255
// Fire a negative polarity event
251
256
set_instant_dvs_pixel (
252
257
event,
258
+ & meta,
253
259
& mut instantaneous_frame_deque,
254
260
frame_idx,
255
261
frame_count,
@@ -287,34 +293,39 @@ fn main() -> Result<(), Box<dyn error::Error>> {
287
293
288
294
let mut event_count_mat = instantaneous_frame_deque[ 0 ] . clone ( ) ;
289
295
unsafe {
290
- for y in 0 ..meta. plane . h ( ) as i32 {
291
- for x in 0 ..meta. plane . w ( ) as i32 {
292
- for c in 0 ..meta. plane . c ( ) as i32 {
293
- * event_count_mat. at_3d_unchecked_mut ( y, x, c) ? =
294
- ( ( event_counts[ [ y as usize , x as usize , c as usize ] ] as f32
295
- / max_px_event_count as f32 )
296
- * 255.0 ) as u8 ;
296
+ for y in 0 ..meta. plane . h_usize ( ) {
297
+ for x in 0 ..meta. plane . w_usize ( ) {
298
+ for c in 0 ..meta. plane . c_usize ( ) {
299
+ event_count_mat[ [ y, x, c] ] = ( ( event_counts[ [ y, x, c] ] as f32
300
+ / max_px_event_count as f32 )
301
+ * 255.0 ) as u8 ;
297
302
}
298
303
}
299
304
}
300
305
}
301
306
302
307
for frame in instantaneous_frame_deque {
303
- if args. show_display {
304
- show_display_force ( "DVS" , & frame, 1 ) ?;
305
- }
308
+ // if args.show_display {
309
+ // show_display_force("DVS", &frame, 1)?;
310
+ // }
306
311
match video_writer {
307
312
None => { }
308
- Some ( ref mut writer) => {
309
- write_frame_to_video ( & frame, writer) ?;
313
+ Some ( ref mut encoder) => {
314
+ write_frame_to_video (
315
+ & frame,
316
+ encoder,
317
+ video_rs:: Time :: from_secs_f64 ( current_frame_time) ,
318
+ ) ?;
319
+ current_frame_time += frame_duration;
310
320
}
311
321
}
312
322
}
313
323
println ! ( "\n " ) ;
314
- if args. show_display {
315
- show_display_force ( "Event counts" , & event_count_mat, 0 ) ?;
316
- }
317
- encode_video_ffmpeg ( raw_path, output_video_path) ?;
324
+
325
+ // TODO: restore this functionality
326
+ // if args.show_display {
327
+ // show_display_force("Event counts", &event_count_mat, 0)?;
328
+ // }
318
329
319
330
handle. flush ( ) ?;
320
331
println ! ( "Finished!" ) ;
@@ -323,7 +334,8 @@ fn main() -> Result<(), Box<dyn error::Error>> {
323
334
324
335
fn set_instant_dvs_pixel (
325
336
event : Event ,
326
- frames : & mut VecDeque < Mat > ,
337
+ meta : & CodecMetadata ,
338
+ frames : & mut VecDeque < Array3 < u8 > > ,
327
339
frame_idx : usize ,
328
340
frame_count : usize ,
329
341
value : u128 ,
@@ -332,39 +344,18 @@ fn set_instant_dvs_pixel(
332
344
let grow_len = frame_idx as i32 - frame_count as i32 - frames. len ( ) as i32 + 1 ;
333
345
334
346
for _ in 0 ..grow_len {
335
- frames. push_back ( frames[ 0 ] . clone ( ) ) ;
336
- // Clear the instantaneous frame
337
- match frames
338
- . back_mut ( )
339
- . expect ( "Could not get back of deque" )
340
- . data_bytes_mut ( )
341
- {
342
- Ok ( bytes) => {
343
- for byte in bytes {
344
- * byte = 128 ;
345
- }
346
- }
347
- Err ( e) => {
348
- return Err ( e. into ( ) ) ;
349
- }
350
- } ;
347
+ frames. push_back ( create_blank_dvs_frame ( & meta) ?) ;
351
348
}
352
349
353
350
unsafe {
354
- let px: & mut u8 = match event. coord . c {
355
- None => frames[ frame_idx - frame_count]
356
- . at_2d_unchecked_mut ( event. coord . y . into ( ) , event. coord . x . into ( ) ) ?,
357
- Some ( c) => frames[ frame_idx - frame_count] . at_3d_unchecked_mut (
358
- event. coord . y . into ( ) ,
359
- event. coord . x . into ( ) ,
360
- c. into ( ) ,
361
- ) ?,
362
- } ;
363
- * px = value as u8 ;
364
- // match value {
365
- // 128 => *px = 128,
366
- // a => *px = (*px as i16 + a) as u8,
367
- // }
351
+ if frame_idx >= frame_count {
352
+ frames[ frame_idx - frame_count] [ [ event. coord . y . into ( ) , event. coord . x . into ( ) , 0 ] ] =
353
+ value as u8 ;
354
+ frames[ frame_idx - frame_count] [ [ event. coord . y . into ( ) , event. coord . x . into ( ) , 1 ] ] =
355
+ value as u8 ;
356
+ frames[ frame_idx - frame_count] [ [ event. coord . y . into ( ) , event. coord . x . into ( ) , 2 ] ] =
357
+ value as u8 ;
358
+ }
368
359
}
369
360
Ok ( ( ) )
370
361
}
@@ -379,3 +370,19 @@ fn event_to_frame_intensity(event: &Event, frame_length: u128) -> f64 {
379
370
. ln_1p ( ) ,
380
371
}
381
372
}
373
+
374
+ fn create_blank_dvs_frame ( meta : & CodecMetadata ) -> Result < Array3 < u8 > , Box < dyn Error > > {
375
+ let instantaneous_frame: Array3 < u8 > = Array3 :: from_shape_vec (
376
+ ( meta. plane . h_usize ( ) , meta. plane . w_usize ( ) , 3 ) ,
377
+ vec ! [ 128_u8 ; meta. plane. h_usize( ) * meta. plane. w_usize( ) * 3 ] ,
378
+ ) ?;
379
+ Ok ( instantaneous_frame)
380
+ }
381
+
382
+ pub fn write_frame_to_video (
383
+ frame : & video_rs:: Frame ,
384
+ encoder : & mut video_rs:: Encoder ,
385
+ timestamp : video_rs:: Time ,
386
+ ) -> Result < ( ) , Box < dyn Error > > {
387
+ encoder. encode ( & frame, & timestamp) . map_err ( |e| e. into ( ) )
388
+ }
0 commit comments