SER Video Files
Planetary and lunar imaging records thousands of short frames, then stacks
the sharpest ones. Capture programs save those frames as SER files: one
header, the raw frames back to back, and an optional trailer that holds a
timestamp for each frame. The ser module reads and writes this format.
Read the frames of a capture
use celestial_images::ser::reader::SerReader;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let reader = SerReader::open("jupiter.ser")?;
println!(
"{} frames of {}x{}",
reader.frame_count(),
reader.header().image_width,
reader.header().image_height
);
for frame in reader.iter_frames() {
let frame = frame?;
println!("frame {}: {} bytes, {:?}", frame.id, frame.data.len(), frame.timestamp);
}
Ok(())
}
SerReader memory-maps the file, so frame.data is a slice of the file’s
bytes and no frame is copied. frame.timestamp is None when the file has
no trailer. open checks that the file holds every frame the header
declares, so a file that is too short fails at open rather than at the
first frame read.
For a part of the capture, iter_frames_range(start, count) iterates over
count frames from start, and read_frame(id) returns one frame. Frames
with a pixel depth of 9 to 16 bits store two bytes per sample.
read_frame_u16 decodes them in the byte order the header gives.
Read a large capture quickly
A borrowed frame costs nothing when the file is already in the page cache.
When it isn’t, every page of the frame waits on its own page fault, and that
is far slower than the disk. On an internal SSD, reading a 15 GB capture
through the mapping took 19.6 s with one thread. Reading it with
read_frame_into took 2.3 s.
If you make several passes over a capture, or read it right after copying it off a camera, read frames into a buffer you own:
use celestial_images::ser::reader::SerReader;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let reader = SerReader::open("sun.ser")?;
let mut frame = vec![0u8; usize::try_from(reader.header().frame_size()?)?];
for id in 0..reader.frame_count() {
reader.read_frame_into(id, &mut frame)?;
// process `frame`
}
Ok(())
}
read_frame_into copies the frame with one positioned read, pread on
Linux and macOS, and the loop reuses one buffer. The buffer must be exactly
one frame long. The read still goes through the page cache, so a second
pass reads from memory.
To read a band of a frame, read_rows_into(id, rows, &mut buf) reads rows
rows.start..rows.end. A row is image_width * bytes_per_pixel() bytes.
read_frame_data, read_frame_u16 and read_timestamp read the same way.
Write a capture
SerWriter takes a header and then one frame at a time:
use celestial_images::ser::header::SerHeader;
use celestial_images::ser::timestamp::SerTimestamp;
use celestial_images::ser::types::ColorId;
use celestial_images::ser::writer::SerWriter;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let header = SerHeader {
color_id: ColorId::Mono,
image_width: 640,
image_height: 480,
pixel_depth_per_plane: 8,
..SerHeader::default()
};
let mut writer = SerWriter::create("capture.ser", header)?;
let frame = vec![0u8; 640 * 480];
let time = SerTimestamp::from_unix_seconds(1_776_801_600.0)?;
writer.write_frame_with_timestamp(&frame, Some(time))?;
writer.finalize()?;
Ok(())
}
The writer counts frames itself and ignores the header’s frame_count.
Each frame must be exactly one frame’s worth of bytes for the header’s
size, color format and pixel depth. Either every frame has a timestamp or
none does, because the trailer has one entry per frame.
finalize writes the header with the final frame count, then the
timestamp trailer. Call it and check its result. Dropping the writer
finalizes the file too, but a drop has no way to report an error.
flush_buffer updates the header without finalizing, so the file on disk
holds every frame written so far. The trailer is still written only by
finalize.
To buffer frames in memory before they go to disk,
SerWriter::with_buffer(path, header, megabytes) sets the buffer size. The
limit is 1024 MiB, because the buffer is allocated in full up front.
Timestamps
SER stores each timestamp as .NET ticks: 100 ns units since 0001-01-01, with
no leap seconds. SerTimestamp converts between ticks and Unix time
(from_unix, to_unix, from_unix_seconds, to_unix_seconds) and to and
from the library’s UTC type (from_celestial_time, to_utc).
to_utc returns an error for a time before 1960, where UTC starts. A
timestamp of zero ticks is 0001-01-01, so a capture program that writes zeros
when it has no clock produces timestamps that to_unix converts and to_utc
refuses.
Files that change while open
Because SerReader memory-maps the file, another program that changes the
file while the reader has it open affects the reader:
- If it truncates the file, the next read of a removed page through the
mapping makes the kernel stop the process with
SIGBUSon Linux and macOS. That read can be a new frame fromread_frame,frame_slice,frame_slicesoriter_frames, or a frame you already hold. Rust cannot turn this into an error.read_frame_into,read_rows_into,read_frame_data,read_frame_u16andread_timestampdon’t read through the mapping, so they return an I/O error instead. - If it writes over the file in place, nothing reports it. Frames from
read_frame,frame_slice,frame_slicesanditer_framesborrow the mapped file, so frames already returned change under you. The methods that copy into a buffer don’t change what they already returned. - If it replaces the file by renaming a new one over it, the reader is unaffected. It keeps reading the file it opened.
Open a SER file only after the capture program has finished writing it. If the file sits on a drive that may be unplugged while you read, use only the methods that copy, so a lost drive is an error and not a crash. To work on a file that may be rewritten in place, copy it first and open the copy.
Where next
- Debayering: turn the frames of a color capture into RGB.
- Stretching for Display: make a stacked result visible on screen.