benclmnt / tmc2-rs

Fast VPCC Point Cloud Decoder
5 stars 2 forks source link

psa: debugging tricks #4

Open benclmnt opened 1 year ago

benclmnt commented 1 year ago

I spent some time today debugging wrong depth which causes the model to disintegrate. You can check the reconstruction logic by plugging in the artifacts from the original encoder and "force" it into our decoder.

just before the reconstruction in Decoder::decode function of decoder.rs add

atlas_context.attr_frames[0] = load_yuv_into_video(
"/path/to/folder/S26C2AIR02_F1_GOF0_attribute_1280x1280_8bit_p420.yuv",1280,1280);

atlas_context.geo_frames[0] = load_yuv_into_video( "/path/to/folder/S26C2AIR02_F1_dec_GOF0_geometry_rec_1280x1280_8bit_p420.yuv", 1280, 1280);

atlas_context.occ_frames = load_yuv_into_video(
"/path/to/folder/S26C2AIR02_F1_dec_GOF0_occupancy_rec_320x320_8bit_p420.yuv", 320, 320);

And add the following functions at the end of decoder.rs

fn load_yuv_into_video<T>(filename: &str, width: usize, height: usize) -> Video<T>
where
    T: Copy + Default,
{
    use std::io::Read;
    let mut video = Video::<T>::default();
    let mut file = File::open(filename).unwrap();
    let file_len = file.metadata().unwrap().len();
    let image_size = width * height * 3 / 2;
    assert!(file_len as usize % image_size == 0);
    let num_frames = file_len as usize / image_size;
    dbg!(filename, file_len, num_frames);
    for _ in 0..num_frames {
        let mut buf = vec![0; image_size];
        file.read_exact(&mut buf).unwrap();
        let image = Image::<T> {
            width,
            height,
            channels: [
                buf[0..width * height].to_owned(),
                buf[width * height..width * height + width * height / 4].to_owned(),
                buf[width * height + width * height / 4..].to_owned(),
            ],
            format: ColorFormat::Yuv420,
            ..Default::default()
        };
        dbg!(buf.len());
        let image = image_to_10bit(image);
        dbg!(image.channels[0].len(), image.channels[1].len());
        video.frames.push(image);
    }
    video
}

fn image_to_10bit<T>(image: Image<T>) -> Image<T>
where
    T: Copy + Default,
{
    if std::mem::size_of::<T>() == 1 {
        return image;
    }
    Image::<T> {
        width: image.width,
        height: image.height,
        channels: [
            image.channels[0]
                .iter()
                .flat_map(|x| ((*x as f64 / 255. * 1023.) as u16).to_ne_bytes())
                .collect(),
            image.channels[1]
                .iter()
                .flat_map(|x| ((*x as f64 / 255. * 1023.) as u16).to_ne_bytes())
                .collect(),
            image.channels[2]
                .iter()
                .flat_map(|x| ((*x as f64 / 255. * 1023.) as u16).to_ne_bytes())
                .collect(),
        ],
        format: ColorFormat::Yuv420,
        ..Default::default()
    }
}