evlib 0.8.1

Event Camera Data Processing Library
Documentation
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*!
ECF (Event Compression Format) codec implementation in Rust.

This is a native Rust implementation of Prophesee's ECF codec based on their
open-source C++ implementation at: https://github.com/prophesee-ai/hdf5_ecf

The ECF codec is designed specifically for compressing event camera data with:
- Delta encoding for timestamps
- Bit-packing for coordinates and polarities
- Adaptive encoding strategies based on event patterns
- Multiple compression modes for different data characteristics

This implementation provides the same functionality as the official C++ codec
but integrated directly into evlib's Rust backend for optimal performance.
*/

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::io::{self, Cursor};

#[cfg(unix)]
use tracing::debug;

#[cfg(not(unix))]
macro_rules! debug {
    ($($args:tt)*) => {};
}

/// Type alias for decoded coordinate data
type CoordinateData = (Vec<u16>, Vec<u16>, Vec<i16>);

/// Maximum number of events that can be stored in a single ECF chunk
const MAX_BUFFER_SIZE: usize = 65535;

/// ECF encoding flags
#[derive(Debug, Clone, Copy)]
pub struct EncodingFlags {
    pub delta_timestamps: bool,
    pub packed_coordinates: bool,
    pub masked_x_coords: bool,
}

impl EncodingFlags {
    /// Convert flags to u32 representation for encoding
    /// TODO: This will be used when implementing advanced ECF encoding modes
    #[allow(dead_code)]
    fn to_u32(self) -> u32 {
        let mut flags = 0u32;
        if self.delta_timestamps {
            flags |= 0x1;
        }
        if self.packed_coordinates {
            flags |= 0x2;
        }
        if self.masked_x_coords {
            flags |= 0x4;
        }
        flags
    }
}

/// Event structure matching Prophesee's EventCD
#[derive(Debug, Clone, Copy)]
pub struct EventCD {
    pub x: u16,
    pub y: u16,
    pub p: i16,
    pub t: i64,
}

/// ECF codec decoder
pub struct ECFDecoder {
    debug: bool,
}

impl Default for ECFDecoder {
    fn default() -> Self {
        Self::new()
    }
}

impl ECFDecoder {
    pub fn new() -> Self {
        Self { debug: false }
    }

    pub fn with_debug(mut self, debug: bool) -> Self {
        self.debug = debug;
        self
    }

    /// Decode ECF-compressed chunk data
    pub fn decode(&self, compressed_data: &[u8]) -> Result<Vec<EventCD>, io::Error> {
        if compressed_data.len() < 4 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "ECF chunk too small for header",
            ));
        }

        let mut cursor = Cursor::new(compressed_data);

        // Read chunk header - it's a single 32-bit integer with bit-packed fields
        let header = cursor.read_u32::<LittleEndian>()?;

        // Extract fields from the bit-packed header
        let num_events = (header >> 3) as usize; // Bits 3-31
        let delta_timestamps = (header >> 2) & 1 != 0; // Bit 2
        let ys_xs_and_ps_packed = (header >> 1) & 1 != 0; // Bit 1
        let xs_and_ps_packed = header & 1 != 0; // Bit 0

        // Convert to our encoding flags structure
        let encoding_flags = EncodingFlags {
            delta_timestamps,
            packed_coordinates: ys_xs_and_ps_packed || xs_and_ps_packed,
            masked_x_coords: false, // Will be determined later based on data
        };

        if self.debug {
            debug!(header = %format!("0x{:08x}", header), num_events, delta_timestamps, ys_xs_and_ps_packed, xs_and_ps_packed, "ECF header");
        }

        if num_events == 0 {
            return Ok(Vec::new());
        }

        if num_events > MAX_BUFFER_SIZE {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!(
                    "ECF chunk too large: {} events (max {})",
                    num_events, MAX_BUFFER_SIZE
                ),
            ));
        }

        // Decode based on encoding flags
        if encoding_flags.delta_timestamps {
            self.decode_delta_timestamps(&mut cursor, num_events, encoding_flags)
        } else {
            self.decode_raw_events(&mut cursor, num_events)
        }
    }

    /// Decode events with delta-compressed timestamps
    fn decode_delta_timestamps(
        &self,
        cursor: &mut Cursor<&[u8]>,
        num_events: usize,
        flags: EncodingFlags,
    ) -> Result<Vec<EventCD>, io::Error> {
        // Read base timestamp
        let base_timestamp = cursor.read_i64::<LittleEndian>()?;

        if self.debug {
            debug!(base_timestamp, "ECF base timestamp");
        }

        // Decode coordinates and polarities
        let coords_and_pols = if flags.packed_coordinates {
            self.decode_packed_coordinates(cursor, num_events)?
        } else {
            self.decode_raw_coordinates(cursor, num_events)?
        };

        // Decode timestamp deltas
        let timestamps = self.decode_timestamp_deltas(cursor, num_events, base_timestamp)?;

        // Combine into events
        let events = coords_and_pols
            .0
            .into_iter()
            .zip(coords_and_pols.1)
            .zip(coords_and_pols.2)
            .zip(timestamps)
            .map(|(((x, y), p), t)| EventCD { x, y, p, t })
            .collect();

        Ok(events)
    }

    /// Decode packed coordinates and polarities
    fn decode_packed_coordinates(
        &self,
        cursor: &mut Cursor<&[u8]>,
        num_events: usize,
    ) -> Result<CoordinateData, io::Error> {
        let mut x_coords = Vec::with_capacity(num_events);
        let mut y_coords = Vec::with_capacity(num_events);
        let mut polarities = Vec::with_capacity(num_events);

        // Simplified packed coordinate decoding
        // In the real ECF codec, this uses variable bit widths based on coordinate ranges
        for _ in 0..num_events {
            let x = cursor.read_u16::<LittleEndian>()?;
            let y = cursor.read_u16::<LittleEndian>()?;
            let p = cursor.read_i16::<LittleEndian>()?;

            x_coords.push(x);
            y_coords.push(y);
            polarities.push(p);
        }

        Ok((x_coords, y_coords, polarities))
    }

    /// Decode raw (uncompressed) coordinates
    fn decode_raw_coordinates(
        &self,
        cursor: &mut Cursor<&[u8]>,
        num_events: usize,
    ) -> Result<CoordinateData, io::Error> {
        // For now, use the same logic as packed coordinates
        // The real implementation has different strategies
        self.decode_packed_coordinates(cursor, num_events)
    }

    /// Decode delta-compressed timestamps
    fn decode_timestamp_deltas(
        &self,
        cursor: &mut Cursor<&[u8]>,
        num_events: usize,
        base_timestamp: i64,
    ) -> Result<Vec<i64>, io::Error> {
        let mut timestamps = Vec::with_capacity(num_events);
        let mut current_timestamp = base_timestamp;

        // Simplified delta decoding - real ECF uses variable-length encoding
        for i in 0..num_events {
            if cursor.position() >= cursor.get_ref().len() as u64 {
                // Fill remaining timestamps with current value
                for _ in i..num_events {
                    timestamps.push(current_timestamp);
                }
                break;
            }

            // Read 4-byte delta (simplified - real format uses variable encoding)
            let delta = cursor.read_u32::<LittleEndian>()? as i64;
            current_timestamp += delta;
            timestamps.push(current_timestamp);
        }

        Ok(timestamps)
    }

    /// Decode raw (uncompressed) events
    fn decode_raw_events(
        &self,
        cursor: &mut Cursor<&[u8]>,
        num_events: usize,
    ) -> Result<Vec<EventCD>, io::Error> {
        let mut events = Vec::with_capacity(num_events);

        // Each raw event: x(2), y(2), p(2), t(8) = 14 bytes
        for _ in 0..num_events {
            if cursor.position() + 14 > cursor.get_ref().len() as u64 {
                break;
            }

            let x = cursor.read_u16::<LittleEndian>()?;
            let y = cursor.read_u16::<LittleEndian>()?;
            let p = cursor.read_i16::<LittleEndian>()?;
            let t = cursor.read_i64::<LittleEndian>()?;

            events.push(EventCD { x, y, p, t });
        }

        Ok(events)
    }
}

/// ECF codec encoder (for completeness, though primarily used for decoding)
pub struct ECFEncoder {
    debug: bool,
}

impl Default for ECFEncoder {
    fn default() -> Self {
        Self::new()
    }
}

impl ECFEncoder {
    pub fn new() -> Self {
        Self { debug: false }
    }

    pub fn with_debug(mut self, debug: bool) -> Self {
        self.debug = debug;
        self
    }

    /// Encode events using ECF compression
    pub fn encode(&self, events: &[EventCD]) -> Result<Vec<u8>, io::Error> {
        if events.is_empty() {
            // Write empty header for empty events
            let mut output = Vec::new();
            let mut cursor = Cursor::new(&mut output);
            let header = 0u32; // 0 events, no flags set
            cursor.write_u32::<LittleEndian>(header)?;
            return Ok(output);
        }

        if events.len() > MAX_BUFFER_SIZE {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                format!(
                    "Too many events: {} (max {})",
                    events.len(),
                    MAX_BUFFER_SIZE
                ),
            ));
        }

        let mut output = Vec::new();
        let mut cursor = Cursor::new(&mut output);

        // Determine encoding strategy (simplified)
        let flags = self.determine_encoding_flags(events);

        // Write header as single bit-packed u32 to match decoder expectations
        // Bits 3-31: number of events, Bit 2: delta_timestamps, Bit 1: ys_xs_and_ps_packed, Bit 0: xs_and_ps_packed
        let header = ((events.len() as u32) << 3)
            | ((flags.delta_timestamps as u32) << 2)
            | ((flags.packed_coordinates as u32) << 1)
            | (flags.packed_coordinates as u32);
        cursor.write_u32::<LittleEndian>(header)?;

        if flags.delta_timestamps {
            self.encode_with_delta_timestamps(&mut cursor, events, flags)?;
        } else {
            self.encode_raw_events(&mut cursor, events)?;
        }

        Ok(output)
    }

    /// Determine optimal encoding flags for the given events
    fn determine_encoding_flags(&self, events: &[EventCD]) -> EncodingFlags {
        // Simplified heuristics - real ECF uses sophisticated analysis
        let use_delta = events.len() > 10; // Use delta encoding for larger chunks
        let use_packed = true; // Generally beneficial for event data

        EncodingFlags {
            delta_timestamps: use_delta,
            packed_coordinates: use_packed,
            masked_x_coords: false, // Simplified for now
        }
    }

    /// Encode events with delta timestamps
    fn encode_with_delta_timestamps(
        &self,
        cursor: &mut Cursor<&mut Vec<u8>>,
        events: &[EventCD],
        flags: EncodingFlags,
    ) -> Result<(), io::Error> {
        // Write base timestamp
        let base_timestamp = events[0].t;
        cursor.write_i64::<LittleEndian>(base_timestamp)?;

        // Encode coordinates
        if flags.packed_coordinates {
            self.encode_packed_coordinates(cursor, events)?;
        } else {
            self.encode_raw_coordinates(cursor, events)?;
        }

        // Encode timestamp deltas
        self.encode_timestamp_deltas(cursor, events, base_timestamp)?;

        Ok(())
    }

    /// Encode coordinates in packed format
    fn encode_packed_coordinates(
        &self,
        cursor: &mut Cursor<&mut Vec<u8>>,
        events: &[EventCD],
    ) -> Result<(), io::Error> {
        for event in events {
            cursor.write_u16::<LittleEndian>(event.x)?;
            cursor.write_u16::<LittleEndian>(event.y)?;
            cursor.write_i16::<LittleEndian>(event.p)?;
        }
        Ok(())
    }

    /// Encode coordinates in raw format
    fn encode_raw_coordinates(
        &self,
        cursor: &mut Cursor<&mut Vec<u8>>,
        events: &[EventCD],
    ) -> Result<(), io::Error> {
        // For now, same as packed
        self.encode_packed_coordinates(cursor, events)
    }

    /// Encode timestamp deltas
    fn encode_timestamp_deltas(
        &self,
        cursor: &mut Cursor<&mut Vec<u8>>,
        events: &[EventCD],
        base_timestamp: i64,
    ) -> Result<(), io::Error> {
        let mut previous_timestamp = base_timestamp;

        for event in events {
            let delta = (event.t - previous_timestamp) as u32;
            cursor.write_u32::<LittleEndian>(delta)?;
            previous_timestamp = event.t;
        }

        Ok(())
    }

    /// Encode events in raw format
    fn encode_raw_events(
        &self,
        cursor: &mut Cursor<&mut Vec<u8>>,
        events: &[EventCD],
    ) -> Result<(), io::Error> {
        for event in events {
            cursor.write_u16::<LittleEndian>(event.x)?;
            cursor.write_u16::<LittleEndian>(event.y)?;
            cursor.write_i16::<LittleEndian>(event.p)?;
            cursor.write_i64::<LittleEndian>(event.t)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ecf_roundtrip() {
        let events = vec![
            EventCD {
                x: 100,
                y: 150,
                p: 1,
                t: 1000,
            },
            EventCD {
                x: 101,
                y: 151,
                p: -1,
                t: 1010,
            },
            EventCD {
                x: 102,
                y: 152,
                p: 1,
                t: 1020,
            },
        ];

        let encoder = ECFEncoder::new().with_debug(true);
        let decoder = ECFDecoder::new().with_debug(true);

        // Encode
        let compressed = encoder.encode(&events).unwrap();
        debug!(
            events = events.len(),
            bytes = compressed.len(),
            "Compressed events"
        );

        // Decode
        let decoded = decoder.decode(&compressed).unwrap();

        // Verify
        assert_eq!(events.len(), decoded.len());
        for (original, decoded) in events.iter().zip(decoded.iter()) {
            assert_eq!(original.x, decoded.x);
            assert_eq!(original.y, decoded.y);
            assert_eq!(original.p, decoded.p);
            assert_eq!(original.t, decoded.t);
        }
    }

    #[test]
    fn test_empty_events() {
        let encoder = ECFEncoder::new();
        let decoder = ECFDecoder::new();

        let compressed = encoder.encode(&[]).unwrap();
        let decoded = decoder.decode(&compressed).unwrap();

        assert!(decoded.is_empty());
    }
}