diff --git a/adder-codec-core/src/codec/decoder.rs b/adder-codec-core/src/codec/decoder.rs index 49ff8057..ef466565 100644 --- a/adder-codec-core/src/codec/decoder.rs +++ b/adder-codec-core/src/codec/decoder.rs @@ -13,7 +13,6 @@ use crate::codec::header::{ }; use crate::codec::raw::stream::RawInput; use crate::codec::CodecError::Deserialize; -use crate::SourceType::U8; use bincode::config::{FixintEncoding, WithOtherEndian, WithOtherIntEncoding}; use bincode::{DefaultOptions, Options}; use bitstream_io::{BigEndian, BitRead, BitReader}; diff --git a/adder-codec-core/src/codec/encoder.rs b/adder-codec-core/src/codec/encoder.rs index 69f5515c..12ab1072 100644 --- a/adder-codec-core/src/codec/encoder.rs +++ b/adder-codec-core/src/codec/encoder.rs @@ -22,7 +22,6 @@ use crate::codec::header::{ }; use crate::codec::raw::stream::RawOutput; -use crate::SourceType::U8; use bincode::config::{FixintEncoding, WithOtherEndian, WithOtherIntEncoding}; use bincode::{DefaultOptions, Options}; diff --git a/adder-codec-core/src/lib.rs b/adder-codec-core/src/lib.rs index 5d95984c..0bcbc040 100644 --- a/adder-codec-core/src/lib.rs +++ b/adder-codec-core/src/lib.rs @@ -187,7 +187,7 @@ pub const D_MAX: D = 127; pub const D_EMPTY: D = 255; /// Special symbol signifying no information (filler dt) -pub const D_ZERO_INTEGRATION: D = 254; +pub const D_ZERO_INTEGRATION: D = 128; /// Special symbol signifying no [`Event`] exists pub const D_NO_EVENT: D = 253; @@ -219,11 +219,11 @@ use seq_macro::seq; macro_rules! make_d_shift_array { ($name:ident, $type:ty) => { - seq!(N in 0..=127 { + seq!(N in 0..=128 { /// Array for computing the intensity to integrate for a given [`D`] value - pub const $name: [$type; 128] = [ + pub const $name: [$type; 129] = [ #( - (1_u128 << N) as $type, + if N == 128 { 0 as $type } else { (1_u128 << N) as $type }, )* ]; }); @@ -560,9 +560,9 @@ mod tests { assert_eq!(D_SHIFT[0], 1); assert_eq!(D_SHIFT_F64[0], 1.0); assert_eq!(D_SHIFT_F32[0], 1.0); - assert_eq!(D_SHIFT.len(), 128); - assert_eq!(D_SHIFT_F64.len(), 128); - assert_eq!(D_SHIFT_F32.len(), 128); + assert_eq!(D_SHIFT.len(), 129); + assert_eq!(D_SHIFT_F64.len(), 129); + assert_eq!(D_SHIFT_F32.len(), 129); assert_eq!(D_SHIFT[127], 1_u128 << 127); assert_eq!(D_SHIFT_F64[127], D_SHIFT[127] as f64); assert_eq!(D_SHIFT_F32[127], D_SHIFT[127] as f32); diff --git a/adder-codec-rs/src/bin/adder_simulproc.rs b/adder-codec-rs/src/bin/adder_simulproc.rs index 91e43336..6c157890 100644 --- a/adder-codec-rs/src/bin/adder_simulproc.rs +++ b/adder-codec-rs/src/bin/adder_simulproc.rs @@ -164,6 +164,7 @@ mod tests { use std::process::Command; use std::thread::sleep; use std::time::Duration; + use adder_codec_core::codec::rate_controller::Crf; #[test] fn dark() -> Result<(), Box> { @@ -173,8 +174,8 @@ mod tests { let args: SimulProcArgs = SimulProcArgs { args_filename: String::new(), color_input: false, - ref_time: 5000, - delta_t_max: 120_000, + ref_time: 255, + delta_t_max: 6120, frame_count_max: 0, frame_idx_start: 1, show_display: false, @@ -192,7 +193,6 @@ mod tests { let mut source = Framed::new(args.input_filename, args.color_input, args.scale)? // .chunk_rows(64) .crf(0) - .time_parameters(5000 * 30, 5000, 120_000, Some(TimeMode::DeltaT))? .frame_start(args.frame_idx_start)?; let source_fps = source.source_fps as f64; @@ -212,7 +212,11 @@ mod tests { PixelMultiMode::Normal, None, EncoderType::Raw, - EncoderOptions::default(plane), + EncoderOptions { + event_drop: Default::default(), + event_order: Default::default(), + crf: Crf::new(Some(0), plane), + }, writer, )?; } diff --git a/adder-codec-rs/src/framer/scale_intensity.rs b/adder-codec-rs/src/framer/scale_intensity.rs index 86209fef..232e94a6 100644 --- a/adder-codec-rs/src/framer/scale_intensity.rs +++ b/adder-codec-rs/src/framer/scale_intensity.rs @@ -68,7 +68,7 @@ impl FrameValue for u8 { FramedViewMode::Intensity => { let intensity = event_to_intensity(event); match source_type { - SourceType::U8 => (intensity * tpf) as u8, + SourceType::U8 => ((intensity * tpf)) as u8, SourceType::U16 => { (intensity / f64::from(u16::MAX) * tpf * f64::from(u8::MAX)) as u8 } diff --git a/adder-codec-rs/src/transcoder/event_pixel_tree.rs b/adder-codec-rs/src/transcoder/event_pixel_tree.rs index 98f85030..5eebc680 100644 --- a/adder-codec-rs/src/transcoder/event_pixel_tree.rs +++ b/adder-codec-rs/src/transcoder/event_pixel_tree.rs @@ -400,7 +400,11 @@ impl PixelArena { d_usize = node.state.d as usize; - let prop = (D_SHIFT_F32[d_usize] - node.state.integration) / intensity; + + let mut prop = (D_SHIFT_F32[d_usize] - node.state.integration) / intensity; + if d_usize == D_ZERO_INTEGRATION as usize { + prop = 1.0; + } debug_assert!(prop > 0.0); node.best_event = Some(Event32 { coord: self.coord, @@ -443,9 +447,13 @@ impl PixelArena { } fn get_d_from_intensity(intensity: Intensity32) -> D { + if intensity < f32::EPSILON { + return D_ZERO_INTEGRATION; + } + min( { - if intensity > 0.0 { + // SAFETY: // By design, the integration will not exceed 2^[`D_MAX`], so we can // safely cast it to integer [`D`] type. @@ -453,9 +461,7 @@ fn get_d_from_intensity(intensity: Intensity32) -> D { (128 - intensity.to_int_unchecked::().leading_zeros() - 1) as D // fast_math::log2_raw(intensity).to_int_unchecked::() } - } else { - 0 - } + }, D_MAX, ) @@ -464,7 +470,7 @@ fn get_d_from_intensity(intensity: Intensity32) -> D { impl PixelNode { pub fn new(start_intensity: Intensity32) -> PixelNode { let start_d = get_d_from_intensity(start_intensity); - debug_assert!(start_d <= D_MAX); + debug_assert!(start_d <= D_MAX + 1); PixelNode { alt: None, state: PixelState { diff --git a/adder-codec-rs/src/transcoder/source/video.rs b/adder-codec-rs/src/transcoder/source/video.rs index 5f6470bf..086846ff 100644 --- a/adder-codec-rs/src/transcoder/source/video.rs +++ b/adder-codec-rs/src/transcoder/source/video.rs @@ -15,10 +15,7 @@ use adder_codec_core::codec::raw::stream::RawOutput; use adder_codec_core::codec::{ CodecError, CodecMetadata, EncoderOptions, EncoderType, LATEST_CODEC_VERSION, }; -use adder_codec_core::{ - Coord, DeltaT, Event, Mode, PixelAddress, PixelMultiMode, PlaneError, PlaneSize, SourceCamera, - SourceType, TimeMode, D_EMPTY, -}; +use adder_codec_core::{Coord, DeltaT, Event, Mode, PixelAddress, PixelMultiMode, PlaneError, PlaneSize, SourceCamera, SourceType, TimeMode, D_EMPTY, D_ZERO_INTEGRATION}; use bumpalo::Bump; use std::sync::mpsc::{channel, Sender}; @@ -778,9 +775,15 @@ impl Video { ) .for_each(|(mut px, frame_chunk)| { for (px, frame_val) in px.iter_mut().zip(frame_chunk.iter()) { - let d_start = f32::from(*frame_val).log2().floor() as D; + let d_start = if *frame_val == 0 { + D_ZERO_INTEGRATION + } else { + (f32::from(*frame_val)).log2().floor() as D + }; + + px.arena[0].set_d(d_start); - px.base_val = *frame_val; + px.base_val = *frame_val as u8; } }); } @@ -1279,8 +1282,8 @@ impl Video { pub fn integrate_for_px( px: &mut PixelArena, base_val: &mut u8, - frame_val: u8, - intensity: Intensity32, + mut frame_val: u8, + mut intensity: Intensity32, time_spanned: f32, buffer: &mut Vec, params: &VideoStateParams, diff --git a/adder-codec-rs/tests/samples/lake_scaled_hd_out.adder b/adder-codec-rs/tests/samples/lake_scaled_hd_out.adder index 9b80b942..340af52a 100644 Binary files a/adder-codec-rs/tests/samples/lake_scaled_hd_out.adder and b/adder-codec-rs/tests/samples/lake_scaled_hd_out.adder differ diff --git a/adder-codec-rs/tests/samples/lake_scaled_out b/adder-codec-rs/tests/samples/lake_scaled_out index 3d49b424..807d40aa 100644 Binary files a/adder-codec-rs/tests/samples/lake_scaled_out and b/adder-codec-rs/tests/samples/lake_scaled_out differ