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

Reading MPEG-TS files, packet by packet. More...

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

Go to the source code of this file.

Macros

#define TS_PACKET_SIZE   188
 
#define TS_SYNC_BYTE   0x47
 

Typedefs

typedef struct TsReader_s TsReader_t
 

Enumerations

enum  TsReaderError_t { TS_READER_OK = 0 , TS_READER_ERROR_OPEN , TS_READER_ERROR_FORMAT , TS_READER_ERROR_MEMORY }
 

Functions

bool ts_detect_packet_format (const uint8_t *buf, size_t len, size_t *packet_size, size_t *offset)
 
TsReader_t * ts_reader_open (const char *filename, TsReaderError_t *error)
 
const uint8_t * ts_reader_next (TsReader_t *reader)
 
size_t ts_reader_get_packet_size (const TsReader_t *reader)
 
void ts_reader_close (TsReader_t *reader)
 

Detailed Description

Reading MPEG-TS files, packet by packet.

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

The reader detects the file format by itself and always hands over packets of TS_PACKET_SIZE bytes, ready for ts_demux_push().

Accepted formats:

  • 188 bytes: standard MPEG-TS;
  • 192 bytes: M2TS (4 bytes of timecode before each packet);
  • 204 bytes: MPEG-TS with 16 bytes of Reed-Solomon at the end of each packet, common in ISDB-T captures.

When sync is lost (the TS_SYNC_BYTE byte is out of place), the reader looks for the next TS_SYNC_BYTE and keeps going from there.

Example:

TsReader_t *reader = ts_reader_open ("video.ts", &error);
const uint8_t *packet;
if (reader == NULL)
return;
while ((packet = ts_reader_next (reader)) != NULL)
ts_demux_push (demux, packet);
ts_reader_close (reader);
void ts_demux_push(TsDemux_t *demux, const uint8_t *packet)
const uint8_t * ts_reader_next(TsReader_t *reader)
struct TsReader_s TsReader_t
Definition ts-reader.h:57
TsReaderError_t
Definition ts-reader.h:47
void ts_reader_close(TsReader_t *reader)
TsReader_t * ts_reader_open(const char *filename, TsReaderError_t *error)

Definition in file ts-reader.h.

Macro Definition Documentation

◆ TS_PACKET_SIZE

#define TS_PACKET_SIZE   188

Size of an MPEG-TS packet, without the extra timecode or Reed-Solomon bytes.

Definition at line 39 of file ts-reader.h.

◆ TS_SYNC_BYTE

#define TS_SYNC_BYTE   0x47

Sync byte that starts every MPEG-TS packet.

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

Typedef Documentation

◆ TsReader_t

typedef struct TsReader_s TsReader_t

MPEG-TS file reader. Opaque struct, created by ts_reader_open().

Definition at line 57 of file ts-reader.h.

Enumeration Type Documentation

◆ TsReaderError_t

Errors returned by ts_reader_open().

Enumerator
TS_READER_OK 

File opened fine.

TS_READER_ERROR_OPEN 

Could not open the file; the reason is in errno.

TS_READER_ERROR_FORMAT 

The file doesn't look like an MPEG-TS (188, 192 or 204 bytes).

TS_READER_ERROR_MEMORY 

Failed to allocate memory.

Definition at line 47 of file ts-reader.h.

Function Documentation

◆ ts_detect_packet_format()

bool ts_detect_packet_format ( const uint8_t *  buf,
size_t  len,
size_t *  packet_size,
size_t *  offset 
)

Detects the packet format from the start of a buffer.

It needs at least 3 consecutive packets with the sync byte in the right place for the format to be recognized.

Parameters
bufStart of the file content.
lenNumber of bytes in buf (recommended: 1024 or more).
packet_sizeReceives the packet size: 188, 192 or 204.
offsetReceives the position of the TS_SYNC_BYTE inside the packet (4 for M2TS, 0 for the others).
Returns
TRUE if the format was recognized.

◆ ts_reader_open()

TsReader_t * ts_reader_open ( const char *  filename,
TsReaderError_t *  error 
)

Opens an MPEG-TS file for reading.

Parameters
filenameFile path.
errorOptional (can be NULL). Receives the reason of the failure.
Returns
The reader, or NULL on error. Free with ts_reader_close().

◆ ts_reader_next()

const uint8_t * ts_reader_next ( TsReader_t *  reader)

Reads the next packet of the file.

Parameters
readerReader created by ts_reader_open().
Returns
Pointer to the TS_PACKET_SIZE bytes of the packet (already without the M2TS timecode and without the Reed-Solomon), or NULL at the end of the file. The pointer is only valid until the next call.

◆ ts_reader_get_packet_size()

size_t ts_reader_get_packet_size ( const TsReader_t *  reader)

Tells the packet size in the file.

Parameters
readerReader created by ts_reader_open().
Returns
188, 192 or 204 bytes.

◆ ts_reader_close()

void ts_reader_close ( TsReader_t *  reader)

Closes the file and frees the reader.

Parameters
readerReader created by ts_reader_open(). NULL is fine.