forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB3d2e08fTahinli7 days ago3929 commits

lib.rs79 lines, 4.1 KiBHistoryRawDownload
1//! MPEG-2 Transport Stream demuxer (and, in [`PsDemuxer`], MPEG-1 system / MPEG-2
2//! program streams and raw MPEG-1/2 video): `.ts`/`.mpg`-style 188-byte streams and the
3//! 192-byte BDAV (`.m2ts`, `.mts`) files AVCHD camcorders and Blu-ray write.
4//!
5//! [`TsDemuxer`] speaks [`ec_core::Demuxer`]. Contracts worth knowing before
6//! integrating:
7//!
8//! - **Packet size and resync.** 188, 192 (4-byte `TP_extra_header`) and 204
9//! (Reed-Solomon) are told apart by the sync-byte stride. A lost sync byte is
10//! searched for again (four confirming strides) and the PES in flight is flagged
11//! `corrupt`; nothing panics on any input.
12//! - **Streams.** The first program is read (`with_program` picks another;
13//! [`TsDemuxer::programs`] lists all). Streams the family can decode are in
14//! `streams()` (MPEG audio layers I, II and III as `Mp1`, `Mp2`, `Mp3`); the rest (LATM, odd-channel
15//! LPCM, DVB subtitles, DTS, ...) are in [`TsDemuxer::tracks`] with a
16//! refusal that names the codec. A PMT change mid-stream keeps the indices of
17//! streams that stay and appends new ones.
18//! - **Timing.** Every packet is in a `1/90000` time base. 33-bit PTS/DTS are
19//! unwrapped into monotonic `i64`; `StreamInfo::start_time` is the first PTS.
20//! - **Video** is delivered as Annex-B access units (split on PES boundaries, and on
21//! access unit delimiters inside a PES for progressive H.264 and HEVC), with the
22//! keyframe flag from IDR/IRAP NAL types, recovery-point SEI or the PES
23//! `random_access_indicator`. `extradata` carries an `avcC`/`hvcC` built from the
24//! in-band parameter sets; [`VideoFraming::LengthPrefixed`] converts packets to
25//! the matching length-prefixed form.
26//! - **MPEG-1/2 video** (stream types 0x01, 0x02) is exposed as
27//! `CodecId::Mpeg2Video`, one packet per coded picture with the sequence/GOP
28//! headers in front of it (a PES holding several pictures is cut at picture start
29//! codes; a picture split over several timestamped PES is not re-joined). Keyframe
30//! = an I picture or a sequence header. Size, frame rate, aspect and chroma come
31//! from the sequence header and sequence_extension, which are also the extradata.
32//! - **Audio** is split into frames (ADTS, AC-3/E-AC-3 syncframes, MPEG audio);
33//! ADTS frames keep their headers and `extradata` is the derived
34//! AudioSpecificConfig. HDMV LPCM loses its 4-byte header.
35//! - **TrueHD** (stream type 0x83, Blu-ray) is exposed as `CodecId::TrueHd`, one
36//! packet per access unit: the 4-byte access-unit header (length word, input
37//! timing) and everything after it, the form `ec_truehd::TrueHdDecoder` decodes
38//! (and the form the MP4 and Matroska demuxers hand over). Blu-ray interleaves an
39//! AC-3 core (0x0B77 syncframes) in the same PID; those frames are dropped, not
40//! exposed, since the TrueHD access units carry the same audio losslessly and
41//! they take no time on the timeline. Timestamps come from the first access unit
42//! that starts in a PES and advance by 40 << n samples per access unit (n from the
43//! major sync's rate code), so the AC-3 frames do not shift them.
44//! - **Seeking** bisects the byte range on the video track's timestamps, then scans
45//! forward to the keyframe at or before the target. It is exact: the packets read
46//! after a seek are those a sequential read yields from
47//! [`TsDemuxer::last_packet_offset`] on.
48//!
49//! No async, no unsafe.
50
51#![forbid(unsafe_code)]
52#![warn(missing_docs)]
53
54mod codec;
55mod demux;
56mod dts;
57mod ps;
58mod psi;
59mod psvideo;
60mod track;
61mod ts;
62mod window;
63
64#[doc(hidden)]
65pub mod fuzz_api;
66
67pub use codec::{
68 LpcmHeader, NalIter, StreamKind, annexb_to_length_prefixed, audio_specific_config, build_avcc,
69 build_hvcc, nal_units, parse_lpcm_header,
70};
71pub use demux::{ProgramInfo, Stats, TrackInfo, TsDemuxer, VideoFraming};
72pub use ps::{PsDemuxer, PsKind, is_mpeg_video_es, is_mpegps};
73pub use psi::{EsEntry, Pmt};
74
75/// True when `head` (the first few KiB of a file) is a transport stream at one of
76/// the three packet sizes. A probe hands the file to [`TsDemuxer`] on this.
77pub fn is_mpegts(head: &[u8]) -> bool {
78 ts::looks_like_ts(head)
79}