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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
use crate::framer::scale_intensity::FrameValue;
use crate::{BigT, DeltaT, Event, PlaneSize, SourceCamera, TimeMode, D};
use bincode::config::{BigEndian, FixintEncoding, WithOtherEndian, WithOtherIntEncoding};
use bincode::{DefaultOptions, Options};
use rayon::iter::ParallelIterator;

use std::collections::VecDeque;
use std::error::Error;
use std::fmt;

use std::fs::File;
use std::io::BufWriter;
use std::ops::Add;

// Want one main framer with the same functions
// Want additional functions
// Want ability to get instantaneous frames at a fixed interval, or at api-spec'd times
// Want ability to get full integration frames at a fixed interval, or at api-spec'd times

/// An ADΔER event representation
#[derive(Debug, Copy, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct EventCoordless {
    pub d: D,
    pub delta_t: DeltaT,
}

impl Add<EventCoordless> for EventCoordless {
    type Output = EventCoordless;

    fn add(self, _rhs: EventCoordless) -> EventCoordless {
        todo!()
    }
}

impl num_traits::Zero for EventCoordless {
    fn zero() -> Self {
        EventCoordless { d: 0, delta_t: 0 }
    }

    fn is_zero(&self) -> bool {
        self.d.is_zero() && self.delta_t.is_zero()
    }
}

// impl Add<Self, Output = Self> for EventCoordless {
//     type Output = ();
//
//     fn add(self, rhs: Self) -> Self::Output {
//         todo!()
//     }
// }
//
// impl num::traits::Zero for EventCoordless {
//     fn zero() -> Self {
//         EventCoordless { d: 0, delta_t: 0 }
//     }
//
//     fn is_zero(&self) -> bool {
//         self.d.is_zero() && self.delta_t.is_zero()
//     }
// }

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FramerMode {
    INSTANTANEOUS,
    INTEGRATION,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SourceType {
    U8,
    U16,
    U32,
    U64,
    F32,
    F64,
}

#[derive(Clone)]
pub struct FramerBuilder {
    plane: PlaneSize,
    tps: DeltaT,
    output_fps: f64,
    mode: FramerMode,
    view_mode: FramedViewMode,
    source: SourceType,
    codec_version: u8,
    source_camera: SourceCamera,
    time_mode: TimeMode,
    ref_interval: DeltaT,
    delta_t_max: DeltaT,
    pub chunk_rows: usize,
}

impl FramerBuilder {
    #[must_use]
    pub fn new(plane: PlaneSize, chunk_rows: usize) -> FramerBuilder {
        FramerBuilder {
            plane,
            chunk_rows,
            tps: 150_000,
            output_fps: 30.0,
            mode: FramerMode::INSTANTANEOUS,
            view_mode: FramedViewMode::Intensity,
            source: SourceType::U8,
            codec_version: 1,
            source_camera: SourceCamera::default(),
            time_mode: TimeMode::DeltaT,
            ref_interval: 5000,
            delta_t_max: 5000,
        }
    }
    #[must_use]
    pub fn time_parameters(
        mut self,
        tps: DeltaT,
        ref_interval: DeltaT,
        delta_t_max: DeltaT,
        output_fps: f64,
    ) -> FramerBuilder {
        self.tps = tps;
        self.ref_interval = ref_interval;
        self.delta_t_max = delta_t_max;
        self.output_fps = output_fps;
        self
    }

    #[must_use]
    pub fn mode(mut self, mode: FramerMode) -> FramerBuilder {
        self.mode = mode;
        self
    }

    #[must_use]
    pub fn view_mode(mut self, mode: FramedViewMode) -> FramerBuilder {
        self.view_mode = mode;
        self
    }

    #[must_use]
    pub fn source(mut self, source: SourceType, source_camera: SourceCamera) -> FramerBuilder {
        self.source_camera = source_camera;
        self.source = source;
        self
    }

    #[must_use]
    pub fn codec_version(mut self, codec_version: u8, time_mode: TimeMode) -> FramerBuilder {
        self.codec_version = codec_version;
        self.time_mode = time_mode;
        self
    }

    // TODO: Make this return a result
    #[must_use]
    pub fn finish<T>(self) -> FrameSequence<T>
    where
        T: FrameValue<Output = T>
            + Default
            + Send
            + Serialize
            + Sync
            + std::marker::Copy
            + num_traits::Zero,
    {
        FrameSequence::<T>::new(self)
    }
}

pub trait Framer {
    type Output;
    fn new(builder: FramerBuilder) -> Self;

    /// Ingest an ADΔER event. Will process differently depending on choice of [`FramerMode`].
    ///
    /// If [`INSTANTANEOUS`], this function will set the corresponding output frame's pixel value to
    /// the value derived from this [`Event`], only if this is the first value ingested for that
    /// pixel and frame. Otherwise, the operation will silently be ignored.
    ///
    /// If [`INTEGRATION`], this function will integrate this [`Event`] value for the corresponding
    /// output frame(s)
    fn ingest_event(&mut self, event: &mut Event) -> bool;

    // fn ingest_event_for_chunk(
    //     &self,
    //     event: &Event,
    //     frame_chunk: &mut VecDeque<Frame<Option<Self::Output>>>,
    //     pixel_ts_tracker: &mut BigT,
    //     frame_idx_offset: &mut i64,
    //     last_filled_tracker: &mut i64,
    // ) -> bool;
    fn ingest_events_events(&mut self, events: Vec<Vec<Event>>) -> bool;
}

#[derive(Debug, Clone, Default)]
pub(crate) struct Frame<T> {
    pub(crate) array: Array3<T>,
    pub(crate) filled_count: usize,
}

#[derive(Debug)]
pub enum FrameSequenceError {
    /// Frame index out of bounds
    InvalidIndex,

    /// Frame not initialized
    UninitializedFrame,

    /// Frame not initialized
    UninitializedFrameChunk,

    /// An impossible "fill count" encountered
    BadFillCount,
}

impl fmt::Display for FrameSequenceError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            FrameSequenceError::InvalidIndex => write!(f, "Invalid frame index"),
            FrameSequenceError::UninitializedFrame => write!(f, "Uninitialized frame"),
            FrameSequenceError::UninitializedFrameChunk => write!(f, "Uninitialized frame chunk"),
            FrameSequenceError::BadFillCount => write!(f, "Bad fill count"),
        }
    }
}

impl From<FrameSequenceError> for Box<dyn std::error::Error> {
    fn from(value: FrameSequenceError) -> Self {
        value.to_string().into()
    }
}

// impl Display for FrameSequenceError {
//     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
//         todo!()
//     }
// }
//
// impl std::error::Error for FrameSequenceError {}

pub struct FrameSequenceState {
    pub frames_written: i64,
    pub tpf: DeltaT,
    pub(crate) source: SourceType,
    codec_version: u8,
    source_camera: SourceCamera,
    ref_interval: DeltaT,
    source_dtm: DeltaT,
    view_mode: FramedViewMode,
    time_mode: TimeMode,
}

#[allow(dead_code)]
pub struct FrameSequence<T> {
    pub state: FrameSequenceState,
    pub(crate) frames: Vec<VecDeque<Frame<Option<T>>>>,
    pub(crate) frame_idx_offsets: Vec<i64>,
    pub(crate) pixel_ts_tracker: Vec<Array3<BigT>>,
    pub(crate) last_filled_tracker: Vec<Array3<i64>>,
    pub(crate) last_frame_intensity_tracker: Vec<Array3<T>>,
    chunk_filled_tracker: Vec<bool>,
    pub(crate) mode: FramerMode,
    pub chunk_rows: usize,
    bincode: WithOtherEndian<WithOtherIntEncoding<DefaultOptions, FixintEncoding>, BigEndian>,
}

use ndarray::Array3;

use crate::transcoder::source::video::FramedViewMode;
use rayon::prelude::IntoParallelIterator;
use serde::Serialize;

impl<
        T: Clone
            + Default
            + FrameValue<Output = T>
            + Copy
            + Serialize
            + Send
            + Sync
            + num_traits::identities::Zero,
    > Framer for FrameSequence<T>
{
    type Output = T;
    fn new(builder: FramerBuilder) -> Self {
        let plane = &builder.plane;

        let chunk_rows = builder.chunk_rows;
        assert!(chunk_rows > 0);

        let num_chunks: usize = ((builder.plane.h()) as f64 / chunk_rows as f64).ceil() as usize;
        let last_chunk_rows = builder.plane.h_usize() - (num_chunks - 1) * chunk_rows;

        assert!(num_chunks > 0);
        let array: Array3<Option<T>> =
            Array3::<Option<T>>::default((chunk_rows, plane.w_usize(), plane.c_usize()));
        let last_array: Array3<Option<T>> =
            Array3::<Option<T>>::default((last_chunk_rows, plane.w_usize(), plane.c_usize()));

        let mut frames = vec![
            VecDeque::from(vec![Frame {
                array,
                filled_count: 0,
            }]);
            num_chunks
        ];

        // Override the last chunk, in case the chunk size doesn't perfectly divide the number of rows
        if let Some(last) = frames.last_mut() {
            *last = VecDeque::from(vec![Frame {
                array: last_array,
                filled_count: 0,
            }]);
        };

        let mut pixel_ts_tracker: Vec<Array3<BigT>> =
            vec![Array3::zeros((chunk_rows, plane.w_usize(), plane.c_usize())); num_chunks];
        if let Some(last) = pixel_ts_tracker.last_mut() {
            *last = Array3::zeros((last_chunk_rows, plane.w_usize(), plane.c_usize()));
        };

        let mut last_frame_intensity_tracker: Vec<Array3<T>> =
            vec![Array3::zeros((chunk_rows, plane.w_usize(), plane.c_usize())); num_chunks];
        if let Some(last) = last_frame_intensity_tracker.last_mut() {
            *last = Array3::zeros((last_chunk_rows, plane.w_usize(), plane.c_usize()));
        };

        let mut last_filled_tracker: Vec<Array3<i64>> =
            vec![Array3::zeros((chunk_rows, plane.w_usize(), plane.c_usize())); num_chunks];
        if let Some(last) = last_filled_tracker.last_mut() {
            *last = Array3::zeros((last_chunk_rows, plane.w_usize(), plane.c_usize()));
        };
        for chunk in &mut last_filled_tracker {
            for mut row in chunk.rows_mut() {
                row.fill(-1);
            }
        }

        // Array3::<Option<T>>::new(num_rows, num_cols, num_channels);
        FrameSequence {
            state: FrameSequenceState {
                frames_written: 0,
                view_mode: builder.view_mode,
                tpf: builder.tps / builder.output_fps as u32,
                source: builder.source,
                codec_version: builder.codec_version,
                source_camera: builder.source_camera,
                ref_interval: builder.ref_interval,
                source_dtm: builder.delta_t_max,
                time_mode: builder.time_mode,
            },
            frames,
            frame_idx_offsets: vec![0; num_chunks],
            pixel_ts_tracker,
            last_filled_tracker,
            last_frame_intensity_tracker,
            chunk_filled_tracker: vec![false; num_chunks],
            mode: builder.mode,
            chunk_rows,
            bincode: DefaultOptions::new()
                .with_fixint_encoding()
                .with_big_endian(),
        }
    }

    ///
    ///
    /// # Examples
    ///
    /// ```
    /// # use adder_codec_rs::{Coord, Event, PlaneSize, TimeMode};
    /// # use adder_codec_rs::framer::driver::FramerMode::INSTANTANEOUS;
    /// # use adder_codec_rs::framer::driver::{FrameSequence, Framer, FramerBuilder};
    /// # use adder_codec_rs::framer::driver::SourceType::U8;
    /// use adder_codec_rs::SourceCamera::FramedU8;
    ///
    /// let mut frame_sequence: FrameSequence<u8> =
    /// FramerBuilder::new(
    ///             PlaneSize::new(10,10,3).unwrap(), 64)
    ///             .codec_version(1, TimeMode::DeltaT)
    ///             .time_parameters(50000, 1000, 1000, 50.0)
    ///             .mode(INSTANTANEOUS)
    ///             .source(U8, FramedU8)
    ///             .finish();
    /// let mut event: Event = Event {
    ///         coord: Coord {
    ///             x: 5,
    ///             y: 5,
    ///             c: Some(1)
    ///         },
    ///         d: 5,
    ///         delta_t: 1000
    ///     };
    /// frame_sequence.ingest_event(&mut event);
    /// let elem = frame_sequence.px_at_current(5, 5, 1).unwrap();
    /// assert_eq!(*elem, Some(32));
    /// ```
    fn ingest_event(&mut self, event: &mut Event) -> bool {
        let channel = event.coord.c.unwrap_or(0);
        let chunk_num = event.coord.y as usize / self.chunk_rows;

        event.coord.y -= (chunk_num * self.chunk_rows) as u16; // Modify the coordinate here, so it gets ingested at the right place

        let frame_chunk = &mut self.frames[chunk_num];
        let last_filled_frame_ref = &mut self.last_filled_tracker[chunk_num]
            [[event.coord.y.into(), event.coord.x.into(), channel.into()]];
        let running_ts_ref = &mut self.pixel_ts_tracker[chunk_num]
            [[event.coord.y.into(), event.coord.x.into(), channel.into()]];
        let frame_idx_offset = &mut self.frame_idx_offsets[chunk_num];
        let last_frame_intensity_ref = &mut self.last_frame_intensity_tracker[chunk_num]
            [[event.coord.y.into(), event.coord.x.into(), channel.into()]];

        self.chunk_filled_tracker[chunk_num] = ingest_event_for_chunk(
            event,
            frame_chunk,
            running_ts_ref,
            frame_idx_offset,
            last_filled_frame_ref,
            last_frame_intensity_ref,
            &self.state,
        );
        for chunk in &self.chunk_filled_tracker {
            if !chunk {
                return false;
            }
        }
        debug_assert!(self.is_frame_0_filled());
        true
    }

    fn ingest_events_events(&mut self, mut events: Vec<Vec<Event>>) -> bool {
        // Make sure that the chunk division is aligned between the source and the framer
        assert_eq!(events.len(), self.frames.len());

        (
            &mut events,
            &mut self.frames,
            &mut self.chunk_filled_tracker,
            &mut self.pixel_ts_tracker,
            &mut self.frame_idx_offsets,
            &mut self.last_filled_tracker,
            &mut self.last_frame_intensity_tracker,
        )
            .into_par_iter()
            .for_each(
                |(
                    a,
                    frame_chunk,
                    chunk_filled,
                    chunk_ts_tracker,
                    frame_idx_offset,
                    chunk_last_filled_tracker,
                    last_frame_intensity_tracker,
                )| {
                    for event in a {
                        let channel = event.coord.c.unwrap_or(0);
                        let chunk_num = event.coord.y as usize / self.chunk_rows;
                        event.coord.y -= (chunk_num * self.chunk_rows) as u16; // Modify the coordinate here, so it gets ingested at the right place
                        let last_filled_frame_ref = &mut chunk_last_filled_tracker
                            [[event.coord.y.into(), event.coord.x.into(), channel.into()]];
                        let running_ts_ref = &mut chunk_ts_tracker
                            [[event.coord.y.into(), event.coord.x.into(), channel.into()]];
                        let last_frame_intensity_ref = &mut last_frame_intensity_tracker
                            [[event.coord.y.into(), event.coord.x.into(), channel.into()]];

                        *chunk_filled = ingest_event_for_chunk(
                            event,
                            frame_chunk,
                            running_ts_ref,
                            frame_idx_offset,
                            last_filled_frame_ref,
                            last_frame_intensity_ref,
                            &self.state,
                        );
                    }
                },
            );

        self.is_frame_0_filled()
    }
}

impl<T: Clone + Default + FrameValue<Output = T> + Serialize> FrameSequence<T> {
    /// Get the number of frames queue'd up to be written
    #[must_use]
    pub fn get_frames_len(&self) -> usize {
        self.frames.len()
    }

    /// Get the number of chunks in a frame
    #[must_use]
    pub fn get_frame_chunks_num(&self) -> usize {
        self.pixel_ts_tracker.len()
    }

    /// Get the reference for the pixel at the given coordinates
    /// # Arguments
    /// * `y` - The y coordinate of the pixel
    /// * `x` - The x coordinate of the pixel
    /// * `c` - The channel of the pixel
    /// # Returns
    /// * `Option<&T>` - The reference to the pixel value
    /// # Errors
    /// * If the frame has not been initialized
    pub fn px_at_current(
        &self,
        y: usize,
        x: usize,
        c: usize,
    ) -> Result<&Option<T>, FrameSequenceError> {
        if self.frames.is_empty() {
            return Err(FrameSequenceError::UninitializedFrame);
        }
        let chunk_num = y / self.chunk_rows;
        let local_row = y - (chunk_num * self.chunk_rows);
        Ok(&self.frames[chunk_num][0].array[[local_row, x, c]])
    }

    /// Get the reference for the pixel at the given coordinates and frame index
    /// # Arguments
    /// * `y` - The y coordinate of the pixel
    /// * `x` - The x coordinate of the pixel
    /// * `c` - The channel of the pixel
    /// * `frame_idx` - The index of the frame to get the pixel from
    /// # Returns
    /// * `Option<&T>` - The reference to the pixel value
    /// # Errors
    /// * If the frame at the given index has not been initialized
    pub fn px_at_frame(
        &self,
        y: usize,
        x: usize,
        c: usize,
        frame_idx: usize,
    ) -> Result<&Option<T>, FrameSequenceError> {
        let chunk_num = y / self.chunk_rows;
        let local_row = y - (chunk_num * self.chunk_rows);
        match self.frames.len() {
            a if frame_idx < a => Ok(&self.frames[chunk_num][frame_idx].array[[local_row, x, c]]),
            _ => Err(FrameSequenceError::InvalidIndex),
        }
    }

    #[allow(clippy::unused_self)]
    fn _get_frame(&self, _frame_idx: usize) -> Result<&Array3<Option<T>>, FrameSequenceError> {
        todo!()
        // match self.frames.len() <= frame_idx {
        //     true => Err(FrameSequenceError::InvalidIndex),
        //     false => Ok(&self.frames[frame_idx].array),
        // }
    }

    /// Get whether or not the frame at the given index is "filled" (i.e., all pixels have been
    /// written to)
    /// # Arguments
    /// * `frame_idx` - The index of the frame to check
    /// # Returns
    /// * `bool` - Whether or not the frame is filled
    /// # Errors
    /// * If the frame at the given index has not been initialized
    /// * If the frame index is out of bounds
    /// * If the frame is not aligned with the chunk division
    pub fn is_frame_filled(&self, frame_idx: usize) -> Result<bool, FrameSequenceError> {
        for chunk in &self.frames {
            if chunk.len() <= frame_idx {
                return Err(FrameSequenceError::InvalidIndex);
            }

            match chunk[frame_idx].filled_count {
                a if a == chunk[0].array.len() => {}
                a if a > chunk[0].array.len() => {
                    return Err(FrameSequenceError::BadFillCount);
                }
                _ => {
                    return Ok(false);
                }
            }
        }
        Ok(true)
    }

    #[must_use]
    pub fn is_frame_0_filled(&self) -> bool {
        for chunk in &self.chunk_filled_tracker {
            if !chunk {
                return false;
            }
        }
        true
    }

    pub fn pop_next_frame(&mut self) -> Option<Vec<Array3<Option<T>>>> {
        let mut ret: Vec<Array3<Option<T>>> = Vec::with_capacity(self.frames.len());

        for chunk_num in 0..self.frames.len() {
            match self.pop_next_frame_for_chunk(chunk_num) {
                Some(frame) => {
                    ret.push(frame);
                }
                None => {
                    println!("Couldn't pop chunk {chunk_num}!");
                }
            }
        }
        self.state.frames_written += 1;
        Some(ret)
    }

    pub fn pop_next_frame_for_chunk(&mut self, chunk_num: usize) -> Option<Array3<Option<T>>> {
        self.frames[chunk_num].rotate_left(1);
        match self.frames[chunk_num].pop_back() {
            Some(a) => {
                // If this is the only frame left, then add a new one to prevent invalid accesses later
                if self.frames[chunk_num].is_empty() {
                    let array: Array3<Option<T>> = Array3::<Option<T>>::default(a.array.raw_dim());
                    self.frames[chunk_num].append(&mut VecDeque::from(vec![
                        Frame {
                            array,
                            filled_count: 0
                        };
                        1
                    ]));
                    self.frame_idx_offsets[chunk_num] += 1;
                }
                self.chunk_filled_tracker[chunk_num] = false;
                Some(a.array)
            }
            None => None,
        }
    }

    /// Write out the next frame to the given writer
    /// # Arguments
    /// * `writer` - The writer to write the frame to
    /// # Returns
    /// * `Result<(), FrameSequenceError>` - Whether or not the write was successful
    /// # Errors
    /// * If the frame chunk has not been initialized
    /// * If the data cannot be written
    pub fn write_frame_bytes(
        &mut self,
        writer: &mut BufWriter<File>,
    ) -> Result<(), Box<dyn Error>> {
        let none_val = T::default();
        for chunk_num in 0..self.frames.len() {
            match self.pop_next_frame_for_chunk(chunk_num) {
                Some(arr) => {
                    for px in arr.iter() {
                        self.bincode.serialize_into(
                            &mut *writer,
                            match px {
                                Some(event) => event,
                                None => &none_val,
                            },
                        )?;
                    }
                }
                None => {
                    return Err(FrameSequenceError::UninitializedFrameChunk.into());
                }
            }
        }
        self.state.frames_written += 1;
        Ok(())
    }

    /// Write out next frames to the given writer so long as the frame is filled
    /// # Arguments
    /// * `writer` - The writer to write the frames to
    /// # Returns
    /// * `Result<(), FrameSequenceError>` - Whether or not the write was successful
    /// # Errors
    /// * If a frame could not be written
    pub fn write_multi_frame_bytes(
        &mut self,
        writer: &mut BufWriter<File>,
    ) -> Result<i32, Box<dyn Error>> {
        let mut frame_count = 0;
        while self.is_frame_filled(0)? {
            self.write_frame_bytes(writer)?;
            frame_count += 1;
        }
        Ok(frame_count)
    }
}

// TODO: refactor this garbage
fn ingest_event_for_chunk<
    T: Clone + Default + FrameValue<Output = T> + Copy + Serialize + Send + Sync,
>(
    event: &mut Event,
    frame_chunk: &mut VecDeque<Frame<Option<T>>>,
    running_ts_ref: &mut BigT,
    frame_idx_offset: &mut i64,
    last_filled_frame_ref: &mut i64,
    last_frame_intensity_ref: &mut T,
    state: &FrameSequenceState,
) -> bool {
    let channel = event.coord.c.unwrap_or(0);

    let prev_last_filled_frame = *last_filled_frame_ref;
    let prev_running_ts = *running_ts_ref;

    if state.codec_version >= 2 && state.time_mode == TimeMode::AbsoluteT {
        *running_ts_ref = event.delta_t as BigT;
    } else {
        *running_ts_ref += u64::from(event.delta_t);
    }

    if ((running_ts_ref.saturating_sub(1)) as i64 / i64::from(state.tpf)) > *last_filled_frame_ref {
        // Set the frame's value from the event

        if event.d != 0xFF {
            // If d == 0xFF, then the event was empty, and we simply repeat the last non-empty
            // event's intensity. Else we reset the intensity here.
            let practical_d_max =
                fast_math::log2_raw(T::max_f32() * (state.source_dtm / state.ref_interval) as f32);
            if state.codec_version >= 2 && state.time_mode == TimeMode::AbsoluteT {
                // event.delta_t -= ((*last_filled_frame_ref + 1) * state.ref_interval as i64) as u32;
                event.delta_t -= prev_running_ts as u32;
            }

            *last_frame_intensity_ref = T::get_frame_value(
                event,
                state.source,
                state.ref_interval,
                practical_d_max,
                state.source_dtm,
                state.view_mode,
            );
        }
        *last_filled_frame_ref = (running_ts_ref.saturating_sub(1)) as i64 / i64::from(state.tpf);

        // Grow the frames vec if necessary
        match *last_filled_frame_ref - *frame_idx_offset {
            a if a > 0 => {
                let array: Array3<Option<T>> =
                    Array3::<Option<T>>::default(frame_chunk[0].array.raw_dim());
                frame_chunk.append(&mut VecDeque::from(vec![
                    Frame {
                        array,
                        filled_count: 0
                    };
                    a as usize
                ]));
                *frame_idx_offset += a;
            }
            a if a < 0 => {
                // We can get here if we've forcibly popped a frame before it's ready.
                // Increment pixel ts trackers as normal, but don't actually do anything
                // with the intensities if they correspond to frames that we've already
                // popped.
                //
                // ALSO can arrive here if the source events are not perfectly
                // temporally interleaved. This may be the case for transcoder
                // performance reasons. The only invariant we hold is that a sequence
                // of events for a given (individual) pixel is in the correct order.
                // There is no invariant for the relative order or interleaving
                // of different pixel event sequences.
            }
            _ => {}
        }

        let mut frame: &mut Option<T>;
        for i in prev_last_filled_frame..*last_filled_frame_ref {
            if i - state.frames_written + 1 >= 0 {
                frame = &mut frame_chunk[(i - state.frames_written + 1) as usize].array
                    [[event.coord.y.into(), event.coord.x.into(), channel.into()]];
                match frame {
                    Some(_val) => {}
                    None => {
                        *frame = Some(*last_frame_intensity_ref);
                        frame_chunk[(i - state.frames_written + 1) as usize].filled_count += 1;
                    }
                }
            }
        }
    }

    // If framed video source, we can take advantage of scheme that reduces event rate by half
    if state.codec_version >= 1
        // && state.time_mode == TimeMode::DeltaT
        && match state.source_camera {
            SourceCamera::FramedU8
            | SourceCamera::FramedU16
            | SourceCamera::FramedU32
            | SourceCamera::FramedU64
            | SourceCamera::FramedF32
            | SourceCamera::FramedF64 => true,
            SourceCamera::Dvs
            | SourceCamera::DavisU8
            | SourceCamera::Atis
            | SourceCamera::Asint => false,
            // TODO: switch statement on the transcode MODE (frame-perfect or continuous), not just the source
        }
        && *running_ts_ref % u64::from(state.ref_interval) > 0
    {
        *running_ts_ref =
            ((*running_ts_ref / u64::from(state.ref_interval)) + 1) * u64::from(state.ref_interval);
    }

    debug_assert!(*last_filled_frame_ref >= 0);
    debug_assert!(frame_chunk[0].filled_count <= frame_chunk[0].array.len());
    frame_chunk[0].filled_count == frame_chunk[0].array.len()
}