Skip to main content

mpeg_pes/
lib.rs

1//! PES (Packetized Elementary Stream) depacketization + PTS/DTS — ISO/IEC 13818-1
2//! (Rec. ITU-T H.222.0) §2.4.3.6 / §2.4.3.7.
3//!
4//! `mpeg-pes` is the sublayer between an MPEG-TS packet layer (e.g. `dvb-si`'s
5//! `TsPacket` / `SiDemux`) and an elementary-stream consumer. Feed it the
6//! payload bytes of TS packets for one PID (split on `payload_unit_start`), and
7//! it yields [`PesPacket`]s carrying the `stream_id`, presentation/decoding
8//! timestamps ([`Pts`]/[`Dts`], 33-bit @ 90 kHz), and the elementary-stream
9//! bytes.
10//!
11//! It depends only on `dvb-common` and is `#![no_std]` (+ `alloc`), WASM-clean.
12//!
13//! ```
14//! use mpeg_pes::{PesPacket, StreamId};
15//! // A minimal PES packet: start code, stream_id 0xE0 (video), len, header, PTS, payload.
16//! let bytes = [
17//!     0x00, 0x00, 0x01, 0xE0, 0x00, 0x0A, 0x80, 0x80, 0x05,
18//!     0x21, 0x00, 0x01, 0x00, 0x01, // PTS = 0
19//!     0xAA, 0xBB, // ES payload
20//! ];
21//! let pkt = PesPacket::parse(&bytes).unwrap();
22//! assert_eq!(pkt.stream_id, StreamId(0xE0));
23//! assert!(pkt.header.as_ref().unwrap().pts.is_some());
24//! assert_eq!(pkt.payload, &[0xAA, 0xBB]);
25//! ```
26#![no_std]
27#![cfg_attr(docsrs, feature(doc_cfg))]
28#![warn(missing_docs)]
29// Runnable examples, embedded so they render on docs.rs and stay in sync with
30// the actual `examples/*.rs` files (shown, not compiled).
31#![doc = "\n# Examples\n"]
32#![doc = "Two runnable examples ship with this crate (`cargo run -p mpeg-pes --example <name>`).\n"]
33#![doc = "\n## `parse_pes_packet`\n\n```rust,ignore"]
34#![doc = include_str!("../examples/parse_pes_packet.rs")]
35#![doc = "```\n\n## `extract_pts`\n\n```rust,ignore"]
36#![doc = include_str!("../examples/extract_pts.rs")]
37#![doc = "```"]
38
39extern crate alloc;
40
41mod assembler;
42mod error;
43mod packet;
44mod stream_id;
45mod timestamp;
46
47pub use assembler::PesAssembler;
48pub use error::{Error, Result};
49pub use packet::{PesHeader, PesPacket};
50pub use stream_id::StreamId;
51pub use timestamp::{Dts, Pts};
52
53/// The 3-byte `packet_start_code_prefix` that opens every PES packet (`0x000001`).
54pub const PACKET_START_CODE_PREFIX: [u8; 3] = [0x00, 0x00, 0x01];