forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB03dc6e9Tahinli3 months ago178 commits

lib.rs83 lines, 3.3 KiBHistoryRawDownload
1//! `oxideav-sub-image` as edith consumes it, over [`ec_pgs`].
2//!
3//! Four items: [`PGS_CODEC_ID`], [`pgs::make_decoder`], [`pgs::SEG_PCS`] and
4//! [`pgs::SEG_END`] — the replica reads a Matroska block's segments, puts the
5//! `.sup` framing back on and stops at the `END` that closes the first set
6//! which composes something, then decodes that through the trait.
7
8#![forbid(unsafe_code)]
9
10/// The codec id a PGS track is named by.
11pub const PGS_CODEC_ID: &str = "pgs";
12
13/// Presentation Graphic Stream.
14pub mod pgs {
15 use oxideav_core::{CodecId, CodecParameters, Decoder, EcDecoder, Result};
16
17 pub use ec_pgs::{SEG_END, SEG_ODS, SEG_PCS, SEG_PDS, SEG_WDS};
18
19 /// A PGS decoder: `.sup`-framed segments in, one RGBA canvas per display
20 /// set out. The parameters say nothing a PGS stream does not state itself.
21 pub fn make_decoder(_params: &CodecParameters) -> Result<Box<dyn Decoder>> {
22 Ok(Box::new(EcDecoder::new(
23 CodecId::new(crate::PGS_CODEC_ID),
24 Box::new(ec_pgs::PgsDecoder::new()),
25 )))
26 }
27}
28
29#[cfg(test)]
30mod tests {
31 use oxideav_core::{CodecId, CodecParameters, Frame, Packet, TimeBase};
32
33 use super::*;
34
35 /// The `.sup` framing the replica rebuilds around a Matroska block.
36 fn segment(kind: u8, body: &[u8]) -> Vec<u8> {
37 let mut out = Vec::new();
38 out.extend_from_slice(b"PG");
39 out.extend_from_slice(&[0; 8]);
40 out.push(kind);
41 out.extend_from_slice(&(body.len() as u16).to_be_bytes());
42 out.extend_from_slice(body);
43 out
44 }
45
46 #[test]
47 fn a_display_set_decodes_through_the_replica_s_own_call_shape() {
48 let params = CodecParameters::subtitle(CodecId::new(PGS_CODEC_ID));
49 let mut decoder = pgs::make_decoder(&params).unwrap();
50
51 // A 4x2 canvas with one 2x1 opaque-white object at (1, 0).
52 let mut pcs = vec![0, 4, 0, 2, 0x10, 0, 0, 0x80, 0, 0, 1];
53 pcs.extend_from_slice(&[0, 0, 0, 0, 0, 1, 0, 0]);
54 let pds = vec![0, 0, 1, 235, 128, 128, 255];
55 let rle = vec![0x00, 0x82, 0x01, 0x00, 0x00];
56 let mut ods = vec![0, 0, 0, 0xC0];
57 ods.extend_from_slice(&((rle.len() + 4) as u32).to_be_bytes()[1..]);
58 ods.extend_from_slice(&[0, 2, 0, 1]);
59 ods.extend_from_slice(&rle);
60 let mut sup = segment(pgs::SEG_PCS, &pcs);
61 // The composition object count is where the replica reads whether the
62 // set shows anything.
63 assert_eq!(pcs[10], 1);
64 sup.extend(segment(pgs::SEG_PDS, &pds));
65 sup.extend(segment(pgs::SEG_ODS, &ods));
66 sup.extend(segment(pgs::SEG_END, &[]));
67
68 decoder
69 .send_packet(&Packet::new(0, TimeBase::new(1, 90_000), sup))
70 .unwrap();
71 let Ok(Frame::Video(frame)) = decoder.receive_frame() else {
72 panic!("a display set is a picture");
73 };
74 // A 4x2 canvas: no dimensions on the frame, so the row stride and the
75 // byte count are what state them.
76 assert_eq!(frame.planes[0].stride, 4 * 4);
77 let data = frame.planes.into_iter().next().unwrap().data;
78 assert_eq!(data.len(), 4 * 2 * 4);
79 // The palette's own white, `Y' = 235` (see `ec_pgs`'s `rgba`).
80 assert_eq!(&data[4..12], &[235, 235, 235, 255, 235, 235, 235, 255]);
81 assert_eq!(&data[0..4], &[0, 0, 0, 0]);
82 }
83}