~/bend-docscommunity

ts.bend checks

raw source on the hub · import 0xfbc4773809086afacef84e7fba6d620c/ts.bend as Ts

MPEG-TS (ISO 13818-1), the writing side: a file of 188-byte packets that carries the video's access units and the audio's frames with their times, which a raw .h264 has no place for.

PID 0 the PAT: program 1 is described on PID 4096 PID 4096 the PMT: the video on PID 256, the audio (if any) on 257 PID 256 the video, one PES packet per access unit; it carries the clock (PCR) PID 257 the audio, one PES packet per frame (AAC, as ADTS)

Times are 90 kHz ticks in a U32: enough for 13 hours.

2 imports
import Base
import ./bytes.bend as B

Types

type Adapt source · line 102 · raw

Data

The adaptation field of a packet with n bytes still to carry: its given start (the clock, or nothing), then filling so that the field and the payload make 184 bytes. None when the payload fills them.

Definitions

def Crc.bit source · line 18 · raw

@n:Nat -> @+c:U32 -> U32

def Crc.of source · line 27 · raw

@bs:List<&2, U32> -> @c:U32 -> U32

CRC-32/MPEG-2: polynomial 04C11DB7, from FFFFFFFF, nothing reflected.

def Ts.fill source · line 37 · raw

@n:Nat -> @rest:List<&2, U32> -> List<&2, U32>

def Ts.head source · line 47 · raw

@+pid:U32 -> @start:Bool -> @adapt:Bool -> @cc:U32 -> @rest:List<&2, U32> -> List<&2, U32>

The 4-byte header: the sync byte, whether a unit starts here, the PID, whether an adaptation field comes before the payload, and the PID's counter.

def Ts.table source · line 54 · raw

@pid:U32 -> @cc:U32 -> @+section:List<&2, U32> -> List<&2, U32>

A table: a section in one packet, after a pointer byte, its CRC after it, the rest of the packet filled.

def Ts.pat source · line 61 · raw

@cc:U32 -> List<&2, U32>

The PAT: program 1 on PID 4096.

def Ts.es source · line 64 · raw

@typ:U32 -> @+pid:U32 -> List<&2, U32>

def Ts.audio source · line 67 · raw

@+typ:U32 -> List<&2, U32>

def Ts.pmt source · line 72 · raw

@cc:U32 -> @video:U32 -> @+audio:U32 -> List<&2, U32>

The PMT: the clock on the video's PID; the video's type (27 H.264, 36 H.265), and the audio's (15 AAC in ADTS; 0 for no audio).

def Pes.pts source · line 81 · raw

@+t:U32 -> @rest:List<&2, U32> -> List<&2, U32>

A time as the 5 bytes of a PTS.

def Pes.of source · line 90 · raw

@id:U32 -> @sized:Bool -> @pts:U32 -> @+data:List<&2, U32> -> List<&2, U32>

A PES packet: the stream's id (224 video, 192 audio), its size when it is to be said (audio; a video's may be 0), and the time.

def Ts.pcr source · line 95 · raw

@+t:U32 -> List<&2, U32>

The clock: a time as the 6 bytes of a PCR, after the field's flags.

def Ts.adapt.of source · line 106 · raw

@+len:U32 -> @base:List<&2, U32> -> @+had:U32 -> Adapt

def Ts.adapt source · line 114 · raw

@+n:U32 -> @+base:List<&2, U32> -> Adapt

def Ts.cut.put source · line 119 · raw

@c:0xfbc4773809086afacef84e7fba6d620c/bytes.Cut -> @+room:U32 -> @field:List<&2, U32> -> @adapt:Bool -> @+pid:U32 -> @start:Bool -> @+cc:U32 -> @+n:U32 -> @acc:List<&2, U32> -> @k:(@_:U32 -> @_:U32 -> @_:List<&2, U32> -> @_:List<&2, U32> -> List<&2, U32>) -> List<&2, U32>

def Ts.cut source · line 131 · raw

@fuel:Nat -> @done:Bool -> @a:Adapt -> @+pid:U32 -> @start:Bool -> @cc:U32 -> @n:U32 -> @data:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>

A PES packet as TS packets, last first onto acc: the first says a unit starts and may carry the clock, the last is filled to size.

def Ts.pes source · line 149 · raw

@pid:U32 -> @cc:U32 -> @clock:List<&2, U32> -> @+pes:List<&2, U32> -> List<&2, U32>

The TS packets of a PES packet on a PID, from counter cc; clock is the adaptation field's start for the first (Ts.pcr, or nothing).

def Ts.count source · line 157 · raw

@n:U32 -> @clock:Bool -> U32

How many packets a PES packet of n bytes takes (the counter moves by as many): the first loses 8 bytes to the clock when there is one.