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
#![warn(missing_docs)]
use crate::codec::header::Magic;
use crate::{DeltaT, Event, PlaneSize, SourceCamera, TimeMode};
use bitstream_io::{BigEndian, BitReader};
use enum_dispatch::enum_dispatch;
use std::io;
use std::io::{Read, Seek, Sink, Write};
#[enum_dispatch(WriteCompression<W>)]
enum WriteCompressionEnum<W: Write> {
CompressedOutput(CompressedOutput<W>),
RawOutput(RawOutput<W>),
EmptyOutput(EmptyOutput<Sink>),
}
#[enum_dispatch(ReadCompression<R>)]
enum ReadCompressionEnum<R: Read + Seek> {
CompressedInput(CompressedInput<R>),
RawInput(RawInput<R>),
}
pub mod compressed;
pub mod decoder;
pub mod empty;
pub mod encoder;
mod header;
pub mod raw;
pub const LATEST_CODEC_VERSION: u8 = 2;
#[allow(missing_docs)]
#[derive(Copy, Clone)]
pub struct CodecMetadata {
pub codec_version: u8,
pub header_size: usize,
pub time_mode: TimeMode,
pub plane: PlaneSize,
pub tps: DeltaT,
pub ref_interval: DeltaT,
pub delta_t_max: DeltaT,
pub event_size: u8,
pub source_camera: SourceCamera,
}
impl Default for CodecMetadata {
fn default() -> Self {
CodecMetadata {
codec_version: LATEST_CODEC_VERSION,
header_size: 24,
time_mode: Default::default(),
plane: Default::default(),
tps: 2550,
ref_interval: 255,
delta_t_max: 255,
event_size: 9,
source_camera: Default::default(),
}
}
}
#[enum_dispatch]
pub trait WriteCompression<W: Write> {
fn magic(&self) -> Magic;
fn meta(&self) -> &CodecMetadata;
fn meta_mut(&mut self) -> &mut CodecMetadata;
fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>;
fn byte_align(&mut self) -> io::Result<()>;
fn into_writer(&mut self) -> Option<W>;
fn flush_writer(&mut self) -> io::Result<()>;
fn compress(&self, data: &[u8]) -> Vec<u8>;
fn ingest_event(&mut self, event: &Event) -> Result<(), CodecError>;
}
#[enum_dispatch]
pub trait ReadCompression<R: Read> {
fn magic(&self) -> Magic;
fn meta(&self) -> &CodecMetadata;
fn meta_mut(&mut self) -> &mut CodecMetadata;
fn read_bytes(
&mut self,
bytes: &mut [u8],
reader: &mut BitReader<R, BigEndian>,
) -> io::Result<()>;
fn digest_event(&mut self, reader: &mut BitReader<R, BigEndian>) -> Result<Event, CodecError>;
fn set_input_stream_position(
&mut self,
reader: &mut BitReader<R, BigEndian>,
position: u64,
) -> Result<(), CodecError>;
}
use crate::codec::compressed::stream::{CompressedInput, CompressedOutput};
use crate::codec::empty::stream::EmptyOutput;
use crate::codec::raw::stream::{RawInput, RawOutput};
use thiserror::Error;
#[allow(missing_docs)]
#[derive(Error, Debug)]
pub enum CodecError {
#[error("stream has not been initialized")]
UnitializedStream,
#[error("Reached end of file when expected")]
Eof,
#[error("Could not deserialize data. EOF reached at unexpected time.")]
Deserialize,
#[error("File formatted incorrectly")]
BadFile,
#[error("File is of unexpected type (compressed or raw)")]
WrongMagic,
#[error("Attempted to seek to a bad position in the stream")]
Seek,
#[error("Unsupported codec version (expected {LATEST_CODEC_VERSION} or lower, found {0})")]
UnsupportedVersion(u8),
#[error("Malformed encoder")]
MalformedEncoder,
#[error("Bincode error")]
BincodeError(#[from] bincode::Error),
#[error("IO error")]
IoError(#[from] io::Error),
#[error("Plane error")]
PlaneError(#[from] crate::PlaneError),
}