//! Blu-ray Presentation Graphic Stream: the subtitles that are *pictures*. //! //! A PGS stream is a sequence of display sets, each a handful of segments: a //! presentation composition (PCS — the canvas and where the objects go on it), a //! window definition (WDS), a palette (PDS), one or more run-length objects //! (ODS) and an end marker (END). A set is decoded against the state the sets //! before it left behind, which is why this is a decoder with an epoch and not //! a function: an acquisition point re-states everything, a normal-case set may //! re-use an object or merely swap the palette. //! //! What comes out is one straight-alpha RGBA frame per display set, the size of //! the composition's own canvas (1920×1080 off a disc), transparent everywhere //! the cue paints nothing. That is the shape a compositor wants and the shape //! `.sup` tooling shows: a caller lays the whole canvas over the film rather //! than placing a sprite. //! //! The framing read here is the `.sup` one — `PG`, two timestamps, type, size — //! because that is what a `.sup` file holds and what a Matroska block becomes //! once its header is put back on ([`ec_subs`] is the text half of the same //! story). Colour is BT.601 limited-range, which is what the palette entries of //! a disc are and what every other decoder of this format assumes. #![forbid(unsafe_code)] use std::collections::{HashMap, VecDeque}; use std::sync::Arc; use ec_core::{ CodecId, CodecParameters, Decoder, Error, Frame, PixelFormat, Plane, Result, VideoFrame, }; /// Palette definition segment. pub const SEG_PDS: u8 = 0x14; /// Object definition segment: the run-length bitmap itself. pub const SEG_ODS: u8 = 0x15; /// Presentation composition segment: canvas size and object placement. pub const SEG_PCS: u8 = 0x16; /// Window definition segment. pub const SEG_WDS: u8 = 0x17; /// End of display set — the point at which a frame is complete. pub const SEG_END: u8 = 0x80; /// The largest width or height an object or a canvas may state. A disc's canvas is 1920×1080; the /// bound is generous, and it keeps the RLE bitmap and the RGBA canvas from being sized by a 16-bit /// field (65535×65535 is 17 GB of RGBA). pub const MAX_DIMENSION: u32 = 4096; /// How many finished display sets may wait for [`PgsDecoder::take_frame`] at once. A finished set /// is only its composition and the objects it places (the canvas is drawn when it is taken), so /// the bound is on a few kilobytes per set, not on a canvas. When the queue is full, [`PgsDecoder::push`] /// stops at the next display set and reports how many bytes it took. pub const MAX_PENDING_SETS: usize = 8; /// A palette: 256 straight-alpha RGBA entries. type Palette = [[u8; 4]; 256]; /// The magic every `.sup` segment starts with. const MAGIC: [u8; 2] = *b"PG"; /// Bytes before a segment's body: magic, PTS, DTS, type, size. const HEADER: usize = 13; /// A decoder over a `.sup` byte stream. /// /// Feed it whole segments ([`PgsDecoder::push`]) — a partial one is held until /// the rest arrives, so a caller may split the stream anywhere — and take a /// frame per display set ([`PgsDecoder::take_frame`]). pub struct PgsDecoder { params: CodecParameters, /// Bytes not yet forming a whole segment. pending: Vec, /// Objects by id, decoded run-length data as the stream stated it. Shared with the finished /// display sets that place them. objects: HashMap>, /// The object being assembled out of a multi-segment ODS sequence. partial: Option<(u16, Object)>, /// Palettes by id. Shared with finished display sets; a later palette segment copies on write. palettes: HashMap>, /// Windows by id, for clipping. windows: HashMap, /// The composition the current display set is building. composition: Composition, /// Display sets finished but not yet taken, oldest first. Capped at [`MAX_PENDING_SETS`]. ready: VecDeque, /// Bytes of a packet [`Decoder::send_packet`] could not finish because the queue was full. backlog: Vec, /// An error from a segment of a call that stopped early, reported by the call that consumes /// the rest of its input. deferred: Option, } /// A finished display set, not yet drawn: the canvas size and everything it places, resolved /// against the state at the set's end so that later segments cannot change it. struct Pending { width: u32, height: u32, draws: Vec, } /// One object placed on a canvas. struct Draw { object: Arc, palette: Arc, x: u32, y: u32, window: Option, crop: Rect, } #[derive(Clone, Default)] struct Object { width: u32, height: u32, rle: Vec, } #[derive(Clone, Copy, Default)] struct Rect { x: u32, y: u32, width: u32, height: u32, } #[derive(Clone, Default)] struct Composition { width: u32, height: u32, palette_id: u8, objects: Vec, } #[derive(Clone, Copy)] struct Placement { object_id: u16, window_id: u8, x: u32, y: u32, crop: Option, } impl Default for PgsDecoder { fn default() -> Self { PgsDecoder::new() } } impl PgsDecoder { /// A decoder with no epoch yet: the first display set states everything. pub fn new() -> PgsDecoder { PgsDecoder { params: CodecParameters::new(CodecId::Pgs), pending: Vec::new(), objects: HashMap::new(), partial: None, palettes: HashMap::new(), windows: HashMap::new(), composition: Composition::default(), ready: VecDeque::new(), backlog: Vec::new(), deferred: None, } } /// Feed stream bytes. Complete segments are consumed; a partial tail is kept for the next call. /// /// Returns how many bytes of `bytes` were consumed. That is all of them, unless the queue of /// finished display sets is full ([`MAX_PENDING_SETS`]): then the call stops before the next /// display set and the caller takes frames and pushes `bytes[consumed..]` again. A caller that /// drains every frame as it goes never sees a short count. /// /// A segment whose body is nonsense (a palette entry short, an object claiming more pixels /// than its bitmap holds) is dropped and the stream carries on: one torn display set out of /// eleven thousand must not cost the track. A segment whose *size* is past [`MAX_DIMENSION`] /// is refused with [`Error::corrupt`] the same way. The first such error is returned by the /// call that consumes the rest of its input (an `Err` means every byte was consumed). A /// framing error (no `PG` at a segment boundary) stops the call at once. pub fn push(&mut self, bytes: &[u8]) -> Result { let mut at = 0; let mut first_error = self.deferred.take(); loop { if self.pending.len() < HEADER { let take = (HEADER - self.pending.len()).min(bytes.len() - at); self.pending.extend_from_slice(&bytes[at..at + take]); at += take; if self.pending.len() < HEADER { break; } if self.pending[..2] != MAGIC { // Not a segment boundary at all: resynchronising would invent structure, // so this is the one hard error. self.pending.clear(); return Err(Error::corrupt("PGS: segment does not start with `PG`")); } } let kind = self.pending[10]; if kind == SEG_END && self.ready.len() >= MAX_PENDING_SETS { // The header waits in `pending`; the next call (or the next take_frame) resumes it. break; } let size = u16::from_be_bytes([self.pending[11], self.pending[12]]) as usize; let total = HEADER + size; if self.pending.len() < total { let take = (total - self.pending.len()).min(bytes.len() - at); self.pending.extend_from_slice(&bytes[at..at + take]); at += take; if self.pending.len() < total { break; } } let segment = std::mem::take(&mut self.pending); if let Err(e) = self.segment(kind, &segment[HEADER..total]) { first_error.get_or_insert(e); } } if at < bytes.len() { self.deferred = first_error; return Ok(at); } match first_error { Some(e) => Err(e), None => Ok(at), } } /// The next finished display set, drawn, oldest first. pub fn take_frame(&mut self) -> Option { let set = self.ready.pop_front()?; let frame = rasterize(&set); self.resume(); frame } /// How many finished display sets are waiting for [`PgsDecoder::take_frame`]. Never more than /// [`MAX_PENDING_SETS`]. pub fn pending_frames(&self) -> usize { self.ready.len() } /// Carry on with what a full queue held back: the rest of a packet, or a header waiting on /// its display set. fn resume(&mut self) { let rest = std::mem::take(&mut self.backlog); match self.push(&rest) { Ok(used) => self.backlog = rest[used..].to_vec(), Err(e) => { self.deferred.get_or_insert(e); } } } /// One segment against the running state. fn segment(&mut self, kind: u8, body: &[u8]) -> Result<()> { match kind { SEG_PCS => self.pcs(body), SEG_WDS => self.wds(body), SEG_PDS => self.pds(body), SEG_ODS => self.ods(body)?, SEG_END => { if let Some(set) = self.resolve()? { self.ready.push_back(set); } } _ => {} } Ok(()) } fn pcs(&mut self, body: &[u8]) { if body.len() < 11 { return; } let composition_state = body[7]; // Epoch start: nothing from before this point is referenced again. if composition_state & 0x80 != 0 { self.objects.clear(); self.palettes.clear(); self.windows.clear(); self.partial = None; } let mut composition = Composition { width: u32::from(u16::from_be_bytes([body[0], body[1]])), height: u32::from(u16::from_be_bytes([body[2], body[3]])), palette_id: body[9], objects: Vec::new(), }; let count = body[10] as usize; let mut at = 11; for _ in 0..count { if body.len() < at + 8 { break; } let flags = body[at + 3]; let cropped = flags & 0x80 != 0; let placement = Placement { object_id: u16::from_be_bytes([body[at], body[at + 1]]), window_id: body[at + 2], x: u32::from(u16::from_be_bytes([body[at + 4], body[at + 5]])), y: u32::from(u16::from_be_bytes([body[at + 6], body[at + 7]])), crop: match cropped && body.len() >= at + 16 { true => Some(Rect { x: u32::from(u16::from_be_bytes([body[at + 8], body[at + 9]])), y: u32::from(u16::from_be_bytes([body[at + 10], body[at + 11]])), width: u32::from(u16::from_be_bytes([body[at + 12], body[at + 13]])), height: u32::from(u16::from_be_bytes([body[at + 14], body[at + 15]])), }), false => None, }, }; at += if cropped { 16 } else { 8 }; composition.objects.push(placement); } self.composition = composition; } fn wds(&mut self, body: &[u8]) { let count = body.first().copied().unwrap_or(0) as usize; for i in 0..count { let at = 1 + i * 9; if body.len() < at + 9 { break; } self.windows.insert( body[at], Rect { x: u32::from(u16::from_be_bytes([body[at + 1], body[at + 2]])), y: u32::from(u16::from_be_bytes([body[at + 3], body[at + 4]])), width: u32::from(u16::from_be_bytes([body[at + 5], body[at + 6]])), height: u32::from(u16::from_be_bytes([body[at + 7], body[at + 8]])), }, ); } } fn pds(&mut self, body: &[u8]) { if body.len() < 2 { return; } // A palette segment *updates* the palette it names: entries it does not // mention keep whatever the epoch left there. let palette = self .palettes .entry(body[0]) .or_insert_with(|| Arc::new([[0, 0, 0, 0]; 256])); let (entries, _) = body[2..].as_chunks::<5>(); let palette = Arc::make_mut(palette); for entry in entries { palette[entry[0] as usize] = rgba(entry[1], entry[2], entry[3], entry[4]); } } fn ods(&mut self, body: &[u8]) -> Result<()> { if body.len() < 4 { return Ok(()); } let id = u16::from_be_bytes([body[0], body[1]]); let sequence = body[3]; if sequence & 0x80 != 0 { // First (or only) segment of the sequence: the dimensions ride here. if body.len() < 11 { return Ok(()); } let width = u32::from(u16::from_be_bytes([body[7], body[8]])); let height = u32::from(u16::from_be_bytes([body[9], body[10]])); if width > MAX_DIMENSION || height > MAX_DIMENSION { // A partial sequence under this id is abandoned too, so its later fragments // cannot attach to an older object. self.partial = None; return Err(Error::corrupt(format!( "PGS: object {id} is {width}x{height}, past the {MAX_DIMENSION}x{MAX_DIMENSION} limit" ))); } let object = Object { width, height, rle: body[11..].to_vec(), }; self.partial = Some((id, object)); } else if let Some((pending_id, object)) = self.partial.as_mut() && *pending_id == id { object.rle.extend_from_slice(&body[4..]); } if sequence & 0x40 != 0 && let Some((id, object)) = self.partial.take() { self.objects.insert(id, Arc::new(object)); } Ok(()) } /// The finished display set the composition describes, resolved against the state now, or /// `None` when it states no canvas at all (a display set seen before any PCS). A canvas past /// [`MAX_DIMENSION`] is refused. Nothing is drawn here: that waits for [`rasterize`]. fn resolve(&self) -> Result> { let (width, height) = (self.composition.width, self.composition.height); if width == 0 || height == 0 { return Ok(None); } if width > MAX_DIMENSION || height > MAX_DIMENSION { return Err(Error::corrupt(format!( "PGS: canvas {width}x{height} is past the {MAX_DIMENSION}x{MAX_DIMENSION} limit" ))); } let mut draws = Vec::new(); if let Some(palette) = self.palettes.get(&self.composition.palette_id) { for placement in &self.composition.objects { let Some(object) = self.objects.get(&placement.object_id) else { continue; }; // A crop takes a rectangle out of the object before it is placed. let crop = placement.crop.unwrap_or(Rect { x: 0, y: 0, width: object.width, height: object.height, }); draws.push(Draw { object: Arc::clone(object), palette: Arc::clone(palette), x: placement.x, y: placement.y, window: self.windows.get(&placement.window_id).copied(), crop, }); } } Ok(Some(Pending { width, height, draws, })) } } /// One finished display set as an RGBA canvas. An object whose run-length data does not describe /// its bitmap is skipped. fn rasterize(set: &Pending) -> Option { let (width, height) = (set.width, set.height); let stride = width as usize * 4; let mut canvas = vec![0u8; stride * height as usize]; for draw in &set.draws { let object = &draw.object; let Some(indices) = decode_rle(&object.rle, object.width, object.height) else { continue; }; let crop = draw.crop; for row in 0..crop.height.min(object.height.saturating_sub(crop.y)) { let target_y = draw.y + row; if target_y >= height { break; } for col in 0..crop.width.min(object.width.saturating_sub(crop.x)) { let target_x = draw.x + col; if target_x >= width { break; } // A window clips its objects. if let Some(w) = draw.window && (target_x < w.x || target_y < w.y || target_x >= w.x + w.width || target_y >= w.y + w.height) { continue; } let index = indices[((crop.y + row) * object.width + crop.x + col) as usize]; let colour = draw.palette[index as usize]; // Fully transparent palette entries paint nothing, which is what lets two // objects share a window. if colour[3] == 0 { continue; } let at = target_y as usize * stride + target_x as usize * 4; canvas[at..at + 4].copy_from_slice(&colour); } } } VideoFrame::try_new( PixelFormat::Rgba8, width, height, vec![Plane::new(canvas, stride)], ) .ok() } impl Decoder for PgsDecoder { fn codec_parameters(&self) -> &CodecParameters { &self.params } fn send_packet(&mut self, packet: &ec_core::Packet) -> Result<()> { // A packet the full queue held back is finished first; the caller receives before it sends. if !self.backlog.is_empty() { return Err(Error::NeedMore); } let used = self.push(&packet.data)?; if used < packet.data.len() { self.backlog = packet.data[used..].to_vec(); } Ok(()) } fn receive_frame(&mut self) -> Result { match self.take_frame() { Some(frame) => Ok(Frame::Video(frame)), None => Err(Error::NeedMore), } } fn flush(&mut self) -> Result<()> { Ok(()) } fn reset(&mut self) { let params = self.params.clone(); *self = PgsDecoder::new(); self.params = params; } } /// One palette entry: BT.601 Y'CbCr with straight alpha, the way a disc writes /// it, into RGBA. /// /// The luma is taken at its own scale — `Y' = 235` comes out 235, not the 255 /// an expansion of the 16..235 video range would give. That is not a reading of /// the Blu-ray spec but a measurement of what every player draws: the oracle's own /// `pgssub` decoder, burning `fixtures/subs/pgs-1080p.sup` over black, tops out /// at 235 (`scripts/pgs-white-level.sh`), and a subtitle that is brighter than /// the one the disc is mastered against is our bug, not the world's. fn rgba(y: u8, cr: u8, cb: u8, alpha: u8) -> [u8; 4] { let y = i32::from(y) << 8; let cb = i32::from(cb) - 128; let cr = i32::from(cr) - 128; let clamp = |v: i32| ((v + 128) >> 8).clamp(0, 255) as u8; [ clamp(y + 359 * cr), clamp(y - 88 * cb - 183 * cr), clamp(y + 454 * cb), alpha, ] } /// The run-length coding of §8.2 of the format: a byte is a pixel of that /// palette index; a zero byte opens a run whose length and colour follow. /// /// `None` for data that does not describe a `width` × `height` bitmap — a /// truncated object, or one whose rows run past their width. fn decode_rle(data: &[u8], width: u32, height: u32) -> Option> { let (width, height) = (width as usize, height as usize); let mut out = vec![0u8; width.checked_mul(height)?]; let mut at = 0; let mut x = 0; let mut y = 0; while at < data.len() { let (colour, run) = match data[at] { 0 => { let flags = *data.get(at + 1)?; match flags { // A line ends where the encoder says it does, even short. 0 => { at += 2; x = 0; y += 1; continue; } // 00 0LLLLLL — L pixels of entry 0. f if f & 0xC0 == 0x00 => { at += 2; (0, usize::from(f & 0x3F)) } // 00 01LLLLLL LLLLLLLL — 14-bit run of entry 0. f if f & 0xC0 == 0x40 => { let run = (usize::from(f & 0x3F) << 8) | usize::from(*data.get(at + 2)?); at += 3; (0, run) } // 00 10LLLLLL CC — L pixels of entry CC. f if f & 0xC0 == 0x80 => { let colour = *data.get(at + 2)?; at += 3; (colour, usize::from(f & 0x3F)) } // 00 11LLLLLL LLLLLLLL CC — 14-bit run of entry CC. f => { let run = (usize::from(f & 0x3F) << 8) | usize::from(*data.get(at + 2)?); let colour = *data.get(at + 3)?; at += 4; (colour, run) } } } colour => { at += 1; (colour, 1) } }; if y >= height || x + run > width { return None; } out[y * width + x..y * width + x + run].fill(colour); x += run; } Some(out) } #[cfg(test)] mod tests { use super::*; /// One `.sup` segment: the framing a Matroska block gets back on the way in. fn segment(kind: u8, body: &[u8]) -> Vec { let mut out = Vec::with_capacity(HEADER + body.len()); out.extend_from_slice(&MAGIC); out.extend_from_slice(&[0; 8]); out.push(kind); out.extend_from_slice(&(body.len() as u16).to_be_bytes()); out.extend_from_slice(body); out } /// A display set: an 8×2 white-on-transparent object at (2, 1) of a 16×4 /// canvas. fn display_set() -> Vec { let mut pcs = vec![0, 16, 0, 4, 0x10, 0, 0, 0x80, 0, 0, 1]; pcs.extend_from_slice(&[0, 0, 0, 0, 0, 2, 0, 1]); // object 0, window 0, at (2,1) let wds = vec![1, 0, 0, 0, 0, 0, 0, 16, 0, 4]; // Entry 1 is opaque white, entry 2 opaque black; entry 0 stays clear. let pds = vec![0, 0, 1, 235, 128, 128, 255, 2, 16, 128, 128, 255]; // Two rows of 8: a run of 8 of entry 1, then a run of 8 of entry 2. let rle = vec![0x00, 0x88, 0x01, 0x00, 0x00, 0x00, 0x88, 0x02, 0x00, 0x00]; let mut ods = vec![0, 0, 0, 0xC0]; ods.extend_from_slice(&((rle.len() + 4) as u32).to_be_bytes()[1..]); ods.extend_from_slice(&[0, 8, 0, 2]); ods.extend_from_slice(&rle); [ segment(SEG_PCS, &pcs), segment(SEG_WDS, &wds), segment(SEG_PDS, &pds), segment(SEG_ODS, &ods), segment(SEG_END, &[]), ] .concat() } #[test] fn a_display_set_decodes_to_the_canvas_it_composes() { let mut decoder = PgsDecoder::new(); decoder.push(&display_set()).unwrap(); let frame = decoder.take_frame().expect("one frame per display set"); assert_eq!((frame.width, frame.height), (16, 4)); assert_eq!(frame.format, PixelFormat::Rgba8); let stride = frame.planes[0].stride; let pixel = |x: usize, y: usize| &frame.planes[0].data[y * stride + x * 4..][..4]; // Outside the object: nothing painted at all. assert_eq!(pixel(0, 0), [0, 0, 0, 0]); assert_eq!(pixel(1, 1), [0, 0, 0, 0]); // Row 1 of the object is white, row 2 is black, both opaque. White is // the palette's own `Y' = 235`, black its `Y' = 16` — see [`rgba`]. assert_eq!(pixel(2, 1), [235, 235, 235, 255]); assert_eq!(pixel(9, 1), [235, 235, 235, 255]); assert_eq!(pixel(2, 2), [16, 16, 16, 255]); // Past the object's width again. assert_eq!(pixel(10, 1), [0, 0, 0, 0]); assert!(decoder.take_frame().is_none()); } #[test] fn a_stream_split_anywhere_decodes_the_same() { let bytes = display_set(); for split in [1, 7, 13, 20, bytes.len() - 1] { let mut decoder = PgsDecoder::new(); decoder.push(&bytes[..split]).unwrap(); decoder.push(&bytes[split..]).unwrap(); let frame = decoder.take_frame().expect("split framing still decodes"); assert_eq!((frame.width, frame.height), (16, 4)); } } #[test] fn rubbish_never_panics() { // Not segment framing at all. let mut decoder = PgsDecoder::new(); assert!(decoder.push(b"not a sup file at all").is_err()); // Well-framed segments with torn bodies: dropped, no frame, no panic. let mut decoder = PgsDecoder::new(); let torn = [ segment(SEG_PCS, &[0, 16, 0, 4]), segment(SEG_ODS, &[0, 0, 0, 0x80, 9, 9, 9]), segment(SEG_PDS, &[0]), segment(SEG_END, &[]), ] .concat(); decoder.push(&torn).unwrap(); assert!(decoder.take_frame().is_none()); } #[test] fn a_long_stream_in_one_push_keeps_the_queue_bounded() { // 3000 display sets in one call: far more than the queue holds. The call stops when the // queue is full and reports how far it got; draining and pushing the rest finishes it. const SETS: usize = 3000; let one = display_set(); let stream = one.repeat(SETS); let mut decoder = PgsDecoder::new(); let mut at = 0; let mut max_queued = 0; let mut frames = 0; let first = decoder.push(&stream).unwrap(); assert!(first < stream.len(), "a full queue must stop the call"); at += first; while at < stream.len() || decoder.pending_frames() > 0 { max_queued = max_queued.max(decoder.pending_frames()); while let Some(frame) = decoder.take_frame() { assert_eq!((frame.width, frame.height), (16, 4)); frames += 1; } if at < stream.len() { at += decoder.push(&stream[at..]).unwrap(); } max_queued = max_queued.max(decoder.pending_frames()); } assert!(max_queued <= MAX_PENDING_SETS, "queued {max_queued}"); assert_eq!(frames, SETS); } #[test] fn a_full_queue_holds_the_last_end_until_a_frame_is_taken() { // One more set than the queue holds. The END that would finish the last set has an empty // body, so its header is the last thing in the call: it waits in the decoder, and taking a // frame finishes it without another push. let stream = display_set().repeat(MAX_PENDING_SETS + 1); let mut decoder = PgsDecoder::new(); assert_eq!(decoder.push(&stream).unwrap(), stream.len()); assert_eq!(decoder.pending_frames(), MAX_PENDING_SETS); let mut frames = 0; while decoder.take_frame().is_some() { frames += 1; } assert_eq!(frames, MAX_PENDING_SETS + 1); } #[test] fn run_length_shapes_and_their_refusals() { // Short form, long form, coloured runs, end of line. let data = [0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x83, 0x02, 0x00, 0x00]; let out = decode_rle(&data, 4, 2).unwrap(); assert_eq!(out, [0, 0, 0, 1, 2, 2, 2, 0]); // A run past the row is a refusal, not a wrap. assert!(decode_rle(&[0x00, 0x08, 0x01], 4, 1).is_none()); // A truncated run is a refusal too. assert!(decode_rle(&[0x00, 0x80], 4, 1).is_none()); } #[test] fn palette_entries_convert_at_their_own_luma_scale() { // A disc's white and black land where the oracle's PGS decoder puts them — // 235 and 16, not an expansion to 255 and 0 — and alpha rides through // untouched. assert_eq!(rgba(235, 128, 128, 255), [235, 235, 235, 255]); assert_eq!(rgba(16, 128, 128, 128), [16, 16, 16, 128]); // A red palette entry: Y=81, Cr=240, Cb=90 in BT.601. let red = rgba(81, 240, 90, 255); assert!(red[0] > 230 && red[1] < 30 && red[2] < 30, "{red:?}"); } }