Skip to main content

mpeg_ps/
lib.rs

1//! MPEG-1/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//! PES payloads are parsed via the `mpeg-pes` crate.
9//!
10//! Depends only on `dvb-common` + `mpeg-pes` and is `#![no_std]` (+ `alloc`).
11//!
12//! # Examples
13//!
14//! Parse a pack header from bytes:
15//!
16//! ```
17//! use mpeg_ps::PackHeader;
18//! use dvb_common::Parse;
19//!
20//! // A minimal pack header: start_code 0x000001BA, SCR=0, mux_rate=3, stuffing=0
21//! let bytes = [
22//!     0x00, 0x00, 0x01, 0xBA,
23//!     0x44, 0x00, 0x04, 0x00, 0x04, 0x01,
24//!     0x40, 0x00, 0x03, 0x00,
25//! ];
26//! let h = PackHeader::parse(&bytes).unwrap();
27//! assert_eq!(h.program_mux_rate, 3);
28//! assert_eq!(h.scr.ticks(), 0);
29//! ```
30//!
31//! Walk a Program Stream:
32//!
33//! ```no_run
34//! # use std::fs;
35//! # use mpeg_ps::program_stream;
36//! let data = fs::read("tests/fixtures/ffmpeg-mpeg2-ps.mpg").unwrap();
37//! let (packs, _trailing) = program_stream::parse_all_packs(&data).unwrap();
38//! println!("Found {} packs", packs.len());
39//! for (i, pack) in packs.iter().enumerate() {
40//!     println!("Pack {}: SCR={} ticks, mux_rate={} B/s",
41//!              i, pack.pack_header.scr.ticks(),
42//!              pack.pack_header.program_mux_rate * 50);
43//! }
44//! ```
45#![no_std]
46#![cfg_attr(docsrs, feature(doc_cfg))]
47#![warn(missing_docs)]
48// Runnable examples, embedded so they render on docs.rs and stay in sync with
49// the actual `examples/*.rs` files (shown, not compiled).
50#![doc = "\n## Runnable examples\n"]
51#![doc = "Run with `cargo run -p mpeg-ps --example <name>`.\n"]
52#![doc = "\n### `parse_pack_header`\n\n```rust,ignore"]
53#![doc = include_str!("../examples/parse_pack_header.rs")]
54#![doc = "```\n\n### `walk_ps`\n\n```rust,ignore"]
55#![doc = include_str!("../examples/walk_ps.rs")]
56#![doc = "```"]
57
58extern crate alloc;
59
60mod error;
61mod pack_header;
62pub mod program_stream;
63pub mod program_stream_map;
64mod scr;
65mod system_header;
66
67pub use error::{Error, Result};
68pub use pack_header::PackHeader;
69pub use program_stream_map::{EsMapEntry, ProgramStreamMap};
70pub use scr::Scr;
71pub use system_header::{StdBufferBound, SystemHeader};
72
73/// The 3-byte `packet_start_code_prefix` that opens PES and PSM packets (`0x000001`).
74pub const PACKET_START_CODE_PREFIX: [u8; 3] = [0x00, 0x00, 0x01];
75
76/// `MPEG_program_end_code` — `0x000001B9`, terminates the program stream.
77pub const PROGRAM_END_CODE: u32 = 0x0000_01B9;