Skip to main content

Crate scarletbook

Crate scarletbook 

Source
Expand description

A clean-room reader for Super Audio CD (Scarletbook) disc images.

Written only from the specifications of aclater/sacd-spec; section numbers in the documentation refer to them. Every value read from an image is treated as hostile: no input makes this crate panic, loop without bound or allocate without bound.

let file = std::fs::File::open("disc.iso")?;
let disc = scarletbook::Disc::open(std::io::BufReader::new(file))?;
for area in &disc.toc().areas {
    println!("{}: {} tracks", area.slot, area.tracks.len());
}

§Threads

Every public type of this crate is Send and Sync, except that those holding a reader follow the reader:

TypeSend ifSync if
Disc<R>, Image<R>, AreaFrames<R>, TrackFrames<R>, TrackAudio<R>R: SendR: Sync
ReadAtReader<A>A: SendA: Sync
Locked<R>R: SendR: Send

This is part of the API, not an accident of the fields: assertions in this crate’s source stop it building if a type loses one of these. It is a statement about what the compiler allows, so here is what it means for reading:

  • Reading is one thread per Disc. A disc has one reader with one position, so its reads take &mut self. Its &self methods (the TOC, making a cursor) may run on several threads at once when R: Sync.
  • To read one image on several threads at once, give each a disc of its own (Disc::share, or clone of a disc read through a ReadAtReader): see Several readers. A ReadAt source is called from several threads at once when it is Sync.
  • Cursors hold no reader. TrackAudioCursor, TrackCursor and AreaCursor can move between threads, and be read through any disc of the same image. A borrowing iterator can move only with the &mut disc it borrows.
  • Results own their bytes. TrackFrame, AudioFrame and AreaFrame borrow nothing, so a frame can go to a worker thread and its decoded result come back.
  • A DstDecoder decodes one frame at a time (&mut self): one per worker. It holds only buffers, so it can also move between threads.
  • Made to be used while another thread reads: CancelToken, Progress, and the per-area seek index that a disc and the discs made from it share.
  • A Log sink must be Send + Sync, since with the parallel feature it is also called from the threads that decode.

The crate uses no thread of its own without the parallel feature. Its global state carries nothing from one image to another: atomic counters that number images and provisional readings, the charset tables, built once on first use, and with parallel, a decoder cached on each thread that decodes. With parallel, a cursor not given a pool of its own (DecodeOn) uses rayon’s current pool, by default its global one.

Stack. Calls may use up to 68 KiB of the calling thread’s stack (measured on x86-64 Linux in release builds; Layout::read_at of the output module, over a DST area on a CPU with AVX2, is the deepest), and the reader, ReadAt source or Log sink they call runs on top of that: give threads that call this crate at least 136 KiB. The threads that decode with parallel use about 20 KiB of the 2 MiB that rayon gives them (Rust’s default, or RUST_MIN_STACK). Unoptimised builds of this crate need far more: on a CPU with AVX2, decoding a DST frame then takes about 1.2 MiB and Layout::read_at about 1.6 MiB (with debug info), within the 2 MiB of a new Rust thread but not of a smaller one. A debug build that runs this crate on threads with less stack can build it optimised ([profile.dev.package.scarletbook] opt-level = 2, as this repository does) or set RUST_MIN_STACK.

Re-exports§

pub use audio::DecodeOn;
pub use audio::AreaCapacity;
pub use audio::AreaCursor;
pub use audio::AreaFrame;
pub use audio::AreaFrames;
pub use audio::AudioFrame;
pub use audio::Continuation;
pub use audio::CursorOptions;
pub use audio::Demuxer;
pub use audio::FrameContent;
pub use audio::FrameKind;
pub use audio::FrameSpan;
pub use audio::FrameStatus;
pub use audio::Stored;
pub use audio::TrackAudio;
pub use audio::TrackAudioCursor;
pub use audio::TrackCursor;
pub use audio::TrackFrame;
pub use audio::TrackFrames;
pub use dst::DstDecoder;
pub use dst::DstError;
pub use read_at::ReadAt;
pub use read_at::ReadAtReader;
pub use seek::AreaIndex;
pub use seek::IndexOptions;
pub use seek::ResumedFrom;
pub use seek::SeekEvent;
pub use seek::SeekPolicy;
pub use seek::Speculation;
pub use seek::SpeculativeOptions;
pub use seek::Suspect;
pub use toc::Area;
pub use toc::AreaKind;
pub use toc::LoudspeakerLayout;
pub use toc::MasterToc;
pub use toc::Speaker;
pub use toc::Toc;
pub use toc::Track;

Modules§

audio
Audio sector multiplexing (specification 4): frame reassembly, the timeline and tracks.
dst
DST frame decoding (specification 5).
log
An optional diagnostic trail: what the library read, detected and met on the way.
output
Output files: the layout of a DSF or DSDIFF file holding a track, a frame range or a track with pause (specification 8), for writers and for filesystems that serve such files without writing them (issue #131). This module, and the Disc methods that make a Layout, come with the output feature, which is on by default.
read_at
Positional reads: image sources that several readers, on several threads, read at once.
seek
Seeking within an area: saved reading state, so that a frame range far into an area does not need a scan of the area from its first audio sector (issue #68).
text
Text decoding (specification 2, 2.6).
toc
The master TOC (specification 2) and the area TOCs (specification 3).

Structs§

CancelToken
A request, from any thread, that the reads of an image stop: one per image, shared by every disc made from it (crate::Disc::share, clone) and their cursors. Get it with crate::Disc::cancel_token (or crate::Image::cancel_token) before handing the disc to the thread that reads.
Disc
An opened disc image with its table of contents.
DiscOptions
How to open a Disc: Disc::options, then what differs from the default, then DiscOptions::open.
Image
A disc image: a reader, its sector format and its sector count.
ImageOptions
How to open an Image: Image::options, then what differs from the default, then ImageOptions::open.
Progress
How far a cursor has read, readable from any thread while the cursor reads: the sectors it has read through the image so far, and the sectors it expects to have read once it is done. Get it from the cursor (for example crate::TrackAudioCursor::progress) before handing the cursor to the thread that reads; it stays valid after the cursor is dropped.
UnreadableSectors
The audio sectors of an image that cannot be read (#435): one set per image, shared by every disc made from it (crate::Disc::share, clone) and their cursors, like CancelToken. Get it with crate::Disc::unreadable_sectors (or crate::Image::unreadable_sectors) and mark sectors with UnreadableSectors::insert; a reader can also report sectors as it reads them, with crate::read_at::unreadable_error, and they are added here.
Warning
A warning: a code with the slot, track and LSN it concerns, where they apply.

Enums§

EditedMasterCheck
A requirement of an edited master (specification 8, 8.4.11) that an area or range fails (OutputRefusal::NotEditedMaster).
Error
An error that stops an operation.
ErrorClass
Broad classes of Error, for callers (and the FFI) that map errors to codes.
OutputRefusal
Why an output file can’t be written (Error::OutputRefused).
SectorFormat
How logical sectors are stored in an image file (specification 1, 1.2).
Slot
A master TOC area slot (specification 3, 3.1).
WarningCode
The warning codes of specification 7, 7.7.2 and 7.7.3.

Constants§

MASTER_TOC_LSN
LSN of the first master TOC sector (specification 1, 1.5).
SECTOR_SIZE
Size of a logical sector in bytes (specification 0, 0.3).