Skip to content

Commit

Permalink
Color (#112)
Browse files Browse the repository at this point in the history
* Begin fixing integration of zero-level values

* Fix it in a less bad way

* Tests passing
  • Loading branch information
ac-freeman authored Mar 18, 2024
1 parent 1258d29 commit d2f94de
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 28 deletions.
1 change: 0 additions & 1 deletion adder-codec-core/src/codec/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
1 change: 0 additions & 1 deletion adder-codec-core/src/codec/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
14 changes: 7 additions & 7 deletions adder-codec-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 },
)*
];
});
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions adder-codec-rs/src/bin/adder_simulproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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,
)?;
}
Expand Down
2 changes: 1 addition & 1 deletion adder-codec-rs/src/framer/scale_intensity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 12 additions & 6 deletions adder-codec-rs/src/transcoder/event_pixel_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -443,19 +447,21 @@ 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.
unsafe {
(128 - intensity.to_int_unchecked::<u128>().leading_zeros() - 1) as D
// fast_math::log2_raw(intensity).to_int_unchecked::<D>()
}
} else {
0
}

},
D_MAX,
)
Expand All @@ -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 {
Expand Down
19 changes: 11 additions & 8 deletions adder-codec-rs/src/transcoder/source/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -778,9 +775,15 @@ impl<W: Write + 'static> Video<W> {
)
.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;
}
});
}
Expand Down Expand Up @@ -1279,8 +1282,8 @@ impl<W: Write + 'static> Video<W> {
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<Event>,
params: &VideoStateParams,
Expand Down
Binary file modified adder-codec-rs/tests/samples/lake_scaled_hd_out.adder
Binary file not shown.
Binary file modified adder-codec-rs/tests/samples/lake_scaled_out
Binary file not shown.

0 comments on commit d2f94de

Please sign in to comment.