libclosedcaption 1.0.3
ISDB-Tb/SBTVD (Brazilian DTV) closed caption extraction from MPEG-TS
Loading...
Searching...
No Matches
ts-demux.h File Reference

MPEG-TS demuxer: PAT/PMT tables, per PID stats and reassembly of the PES packets of one PID. More...

#include "caption-common.h"
#include "ts-reader.h"
Include dependency graph for ts-demux.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TsPidInfo_t
 

Macros

#define TS_PID_PAT   0x0000
 
#define TS_PID_NULL   0x1fff
 
#define TS_PID_MAX   0x2000
 
#define TS_PID_NONE   0xffff
 

Typedefs

typedef void(* TsDemuxPesCallback_t) (const uint8_t *pes, size_t len, void *userdata)
 
typedef struct TsDemux_s TsDemux_t
 

Functions

TsDemux_t * ts_demux_new (void)
 
void ts_demux_free (TsDemux_t *demux)
 
void ts_demux_set_pes_callback (TsDemux_t *demux, uint16_t pid, TsDemuxPesCallback_t callback, void *userdata)
 
void ts_demux_push (TsDemux_t *demux, const uint8_t *packet)
 
void ts_demux_flush (TsDemux_t *demux)
 
const TsPidInfo_t * ts_demux_get_pid_info (const TsDemux_t *demux, uint16_t pid)
 
uint16_t ts_demux_get_caption_pid (const TsDemux_t *demux)
 
const char * ts_demux_describe_pid (const TsDemux_t *demux, uint16_t pid, char *buf, size_t len)
 
const char * ts_stream_type_name (uint8_t stream_type)
 
const char * ts_component_tag_name (int16_t component_tag)
 
const char * ts_system_pid_name (uint16_t pid)
 

Detailed Description

MPEG-TS demuxer: PAT/PMT tables, per PID stats and reassembly of the PES packets of one PID.

Author
Jorge Pereira jpere.nosp@m.iran.nosp@m.@gmai.nosp@m.l.co.nosp@m.m

The demuxer takes the 188 byte TS packets (see ts-reader.h) and:

  • counts the packets of each PID;
  • reads the PAT and the PMTs of all programs, finding out the type of each PID (video, audio, caption, PCR, etc.);
  • suggests the caption PID, from the PMT component_tag;
  • reassembles the PES packets of a chosen PID and hands each whole PES to a callback (for example, caption_screen_push_pes()).

Example:

static void
on_pes (const uint8_t *pes, size_t len, void *userdata)
{
caption_screen_push_pes (userdata, pes, len);
}
TsDemux_t *demux = ts_demux_new ();
ts_demux_set_pes_callback (demux, 0x3ed, on_pes, screen);
while ((packet = ts_reader_next (reader)) != NULL)
ts_demux_push (demux, packet);
ts_demux_free (demux);
bool caption_screen_push_pes(CaptionScreen_t *screen, const uint8_t *pes, size_t len)
void ts_demux_free(TsDemux_t *demux)
void ts_demux_flush(TsDemux_t *demux)
struct TsDemux_s TsDemux_t
Definition ts-demux.h:82
void ts_demux_set_pes_callback(TsDemux_t *demux, uint16_t pid, TsDemuxPesCallback_t callback, void *userdata)
TsDemux_t * ts_demux_new(void)
void ts_demux_push(TsDemux_t *demux, const uint8_t *packet)
const uint8_t * ts_reader_next(TsReader_t *reader)

Definition in file ts-demux.h.

Macro Definition Documentation

◆ TS_PID_PAT

#define TS_PID_PAT   0x0000

PAT PID (Program Association Table).

Definition at line 42 of file ts-demux.h.

◆ TS_PID_NULL

#define TS_PID_NULL   0x1fff

PID of the null packets (stuffing).

Definition at line 45 of file ts-demux.h.

◆ TS_PID_MAX

#define TS_PID_MAX   0x2000

Number of possible PIDs (13 bits).

Definition at line 48 of file ts-demux.h.

◆ TS_PID_NONE

#define TS_PID_NONE   0xffff

Value used to say "no PID".

Definition at line 51 of file ts-demux.h.

Typedef Documentation

◆ TsDemuxPesCallback_t

typedef void(* TsDemuxPesCallback_t) (const uint8_t *pes, size_t len, void *userdata)

Callback called for each whole PES of the PID chosen in ts_demux_set_pes_callback().

Parameters
pesWhole PES packet, starting at the packet_start_code_prefix (00 00 01). Only valid during the call.
lenSize of pes in bytes.
userdataPointer given to ts_demux_set_pes_callback().

Definition at line 75 of file ts-demux.h.

◆ TsDemux_t

typedef struct TsDemux_s TsDemux_t

MPEG-TS demuxer. Opaque struct, created by ts_demux_new().

Definition at line 82 of file ts-demux.h.

Function Documentation

◆ ts_demux_new()

TsDemux_t * ts_demux_new ( void  )

Creates a demuxer.

Returns
The demuxer, or NULL if out of memory. Free with ts_demux_free().

◆ ts_demux_free()

void ts_demux_free ( TsDemux_t *  demux)

Frees the demuxer. Does not call ts_demux_flush().

Parameters
demuxDemuxer created by ts_demux_new(). NULL is fine.

◆ ts_demux_set_pes_callback()

void ts_demux_set_pes_callback ( TsDemux_t *  demux,
uint16_t  pid,
TsDemuxPesCallback_t  callback,
void *  userdata 
)

Chooses the PID whose PES packets will be reassembled and handed to the callback.

Only one PID is reassembled at a time. A new call replaces the previous one and drops the PES that was incomplete.

Parameters
demuxDemuxer created by ts_demux_new().
pidPID to be reassembled, or TS_PID_NONE to turn it off.
callbackFunction called for each whole PES.
userdataPassed on to the callback.

◆ ts_demux_push()

void ts_demux_push ( TsDemux_t *  demux,
const uint8_t *  packet 
)

Processes a TS packet.

Packets with the transport_error_indicator set are ignored. Lost packets (continuity_counter failure) drop the incomplete PES.

Parameters
demuxDemuxer created by ts_demux_new().
packetPacket of TS_PACKET_SIZE bytes, starting with TS_SYNC_BYTE.

◆ ts_demux_flush()

void ts_demux_flush ( TsDemux_t *  demux)

Delivers the last PES, if it is still pending.

A PES is only considered complete when the next one starts or when it reaches the PES_packet_length; so call this function at the end of the file.

Parameters
demuxDemuxer created by ts_demux_new().

◆ ts_demux_get_pid_info()

const TsPidInfo_t * ts_demux_get_pid_info ( const TsDemux_t *  demux,
uint16_t  pid 
)

Returns the info collected about a PID.

Parameters
demuxDemuxer created by ts_demux_new().
pidPID between 0 and TS_PID_MAX - 1.
Returns
The PID info, or NULL if pid is invalid.

◆ ts_demux_get_caption_pid()

uint16_t ts_demux_get_caption_pid ( const TsDemux_t *  demux)

Returns the caption PID suggested by the PMT.

It picks the private stream (stream_type 0x06) with a caption component_tag: 0x30 to 0x37 (full-seg) or 0x87 (one-seg), preferring the lowest component_tag, that is, the main full-seg caption.

Parameters
demuxDemuxer created by ts_demux_new().
Returns
The caption PID, or TS_PID_NONE if none was found.

◆ ts_demux_describe_pid()

const char * ts_demux_describe_pid ( const TsDemux_t *  demux,
uint16_t  pid,
char *  buf,
size_t  len 
)

Builds a readable description of the PID, for example "0x06 PES privado (component_tag 0x30, legenda)".

Parameters
demuxDemuxer created by ts_demux_new().
pidPID between 0 and TS_PID_MAX - 1.
bufBuffer that receives the description.
lenSize of buf (128 bytes is enough).
Returns
buf itself.

◆ ts_stream_type_name()

const char * ts_stream_type_name ( uint8_t  stream_type)

Name of the PMT stream_type (ISO/IEC 13818-1, Table 2-34).

Parameters
stream_typestream_type value.
Returns
The name, for example "H.264 video", or "desconhecido" (unknown).

◆ ts_component_tag_name()

const char * ts_component_tag_name ( int16_t  component_tag)

Name of the component_tag in ISDB-T (ARIB TR-B14 / ABNT NBR 15608).

Parameters
component_tagcomponent_tag value.
Returns
"legenda", "legenda one-seg", "superimpose", or NULL for the other values.

◆ ts_system_pid_name()

const char * ts_system_pid_name ( uint16_t  pid)

Name of the PIDs reserved for system tables (PAT, NIT, SDT, EIT, etc.).

Parameters
pidPID to look up.
Returns
The table name, or NULL if the PID is not reserved.