mpeg_ps/lib.rs
1//! MPEG-2 Program Stream parsing — ISO/IEC 13818-1 (Rec. ITU-T H.222.0) §2.5.
2//!
3//! The Program Stream (`.mpg` / `.vob`) framing that wraps PES packets: the
4//! [`PackHeader`] (42-bit SCR + `program_mux_rate`), the optional
5//! [`SystemHeader`] (rate/audio/video bounds + per-stream P-STD buffer bounds),
6//! and the [`ProgramStreamMap`] (PSM).
7//!
8//! Only the ISO/IEC 13818-1 (MPEG-2) pack header layout (`'01'` SCR prefix)
9//! is implemented. The different ISO/IEC 11172-1 (MPEG-1) pack layout
10//! (`'0010'` prefix, no SCR extension) is recognized and reported as
11//! [`Error::Mpeg1NotSupported`] rather than misparsed as corrupt bytes, but
12//! is not otherwise implemented (#1119).
13//!
14//! PES payloads are parsed via the `mpeg-pes` crate.
15//!
16//! Depends only on `broadcast-common` + `mpeg-pes` and is `#![no_std]` (+ `alloc`).
17//!
18//! # Examples
19//!
20//! Parse a pack header from bytes:
21//!
22//! ```
23//! use mpeg_ps::PackHeader;
24//! use broadcast_common::Parse;
25//!
26//! // A minimal pack header: start_code 0x000001BA, SCR=0, mux_rate=3, stuffing=0
27//! let bytes = [
28//! 0x00, 0x00, 0x01, 0xBA,
29//! 0x44, 0x00, 0x04, 0x00, 0x04, 0x01,
30//! 0x00, 0x00, 0x0F, 0x00,
31//! ];
32//! let h = PackHeader::parse(&bytes).unwrap();
33//! assert_eq!(h.program_mux_rate, 3);
34//! assert_eq!(h.scr.ticks(), 0);
35//! ```
36//!
37//! Walk a Program Stream:
38//!
39//! ```no_run
40//! # use std::fs;
41//! # use mpeg_ps::program_stream;
42//! let data = fs::read("tests/fixtures/ffmpeg-mpeg2-ps.mpg").unwrap();
43//! let (packs, _trailing) = program_stream::parse_all_packs(&data).unwrap();
44//! println!("Found {} packs", packs.len());
45//! for (i, pack) in packs.iter().enumerate() {
46//! println!("Pack {}: SCR={} ticks, mux_rate={} B/s",
47//! i, pack.pack_header.scr.ticks(),
48//! pack.pack_header.program_mux_rate * 50);
49//! }
50//! ```
51#![no_std]
52#![cfg_attr(docsrs, feature(doc_cfg))]
53#![warn(missing_docs)]
54// Runnable examples, embedded so they render on docs.rs and stay in sync with
55// the actual `examples/*.rs` files (shown, not compiled).
56#![doc = "\n## Runnable examples\n"]
57#![doc = "Run with `cargo run -p mpeg-ps --example <name>`.\n"]
58#![doc = "\n### `parse_pack_header`\n\n```rust,ignore"]
59#![doc = include_str!("../examples/parse_pack_header.rs")]
60#![doc = "```\n\n### `walk_ps`\n\n```rust,ignore"]
61#![doc = include_str!("../examples/walk_ps.rs")]
62#![doc = "```"]
63
64extern crate alloc;
65
66mod error;
67mod pack_header;
68pub mod program_stream;
69pub mod program_stream_map;
70mod scr;
71mod system_header;
72
73pub use error::{Error, Result};
74pub use pack_header::PackHeader;
75pub use program_stream_map::{EsMapEntry, ProgramStreamMap};
76pub use scr::Scr;
77pub use system_header::{StdBufferBound, SystemHeader};
78
79/// The 3-byte `packet_start_code_prefix` that opens PES and PSM packets
80/// (`0x000001`) — the one definition lives in `mpeg-pes` (#1141).
81pub use mpeg_pes::PACKET_START_CODE_PREFIX;
82
83/// `MPEG_program_end_code` — `0x000001B9`, terminates the program stream.
84pub const PROGRAM_END_CODE: u32 = 0x0000_01B9;