FFmpeg
Data Structures | Macros | Enumerations | Functions | Variables
mpegtsenc.c File Reference
#include "libavutil/avassert.h"
#include "libavutil/bswap.h"
#include "libavutil/crc.h"
#include "libavutil/dict.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libavcodec/ac3_parser_internal.h"
#include "libavcodec/bytestream.h"
#include "libavcodec/defs.h"
#include "libavcodec/h264.h"
#include "libavcodec/hevc.h"
#include "libavcodec/vvc.h"
#include "libavcodec/startcode.h"
#include "avformat.h"
#include "avio_internal.h"
#include "internal.h"
#include "mpegts.h"
#include "mux.h"

Go to the source code of this file.

Data Structures

struct  MpegTSSection
 
struct  MpegTSService
 
struct  MpegTSWrite
 
struct  MpegTSWriteStream
 

Macros

#define PCR_TIME_BASE   27000000
 
#define DVB_PRIVATE_NETWORK_START   0xff01
 
#define MPEGTS_FLAG_REEMIT_PAT_PMT   0x01
 
#define MPEGTS_FLAG_AAC_LATM   0x02
 
#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES   0x04
 
#define MPEGTS_FLAG_SYSTEM_B   0x08
 
#define MPEGTS_FLAG_DISCONT   0x10
 
#define MPEGTS_FLAG_NIT   0x20
 
#define MPEGTS_FLAG_OMIT_RAI   0x40
 
#define DEFAULT_PES_HEADER_FREQ   16
 
#define DEFAULT_PES_PAYLOAD_SIZE   ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
 
#define SECTION_LENGTH   1020
 
#define DEFAULT_PROVIDER_NAME   "FFmpeg"
 
#define DEFAULT_SERVICE_NAME   "Service"
 
#define SDT_RETRANS_TIME   500
 
#define PAT_RETRANS_TIME   100
 
#define PCR_RETRANS_TIME   20
 
#define NIT_RETRANS_TIME   500
 
#define H264_NAL_TYPE(state)   (state & 0x1f)
 
#define HEVC_NAL_TYPE(state)   ((state & 0x7e) >> 1)
 
#define VVC_NAL_TYPE(state)   ((state >> 11) & 0x1f)
 
#define OFFSET(x)   offsetof(MpegTSWrite, x)
 
#define ENC   AV_OPT_FLAG_ENCODING_PARAM
 

Enumerations

enum  {
  MPEGTS_SERVICE_TYPE_DIGITAL_TV = 0x01, MPEGTS_SERVICE_TYPE_DIGITAL_RADIO = 0x02, MPEGTS_SERVICE_TYPE_TELETEXT = 0x03, MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO = 0x0A,
  MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV = 0x11, MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV = 0x16, MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV = 0x19, MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV = 0x1F
}
 

Functions

static void mpegts_write_section (MpegTSSection *s, uint8_t *buf, int len)
 
static void put16 (uint8_t **q_ptr, int val)
 
static int mpegts_write_section1 (MpegTSSection *s, int tid, int id, int version, int sec_num, int last_sec_num, uint8_t *buf, int len)
 
static void mpegts_write_pat (AVFormatContext *s)
 
static void putbuf (uint8_t **q_ptr, const uint8_t *buf, size_t len)
 
static int put_arib_caption_descriptor (AVFormatContext *s, uint8_t **q_ptr, AVCodecParameters *codecpar)
 
static void put_registration_descriptor (uint8_t **q_ptr, uint32_t tag)
 
static int get_dvb_stream_type (AVFormatContext *s, AVStream *st)
 
static int get_m2ts_stream_type (AVFormatContext *s, AVStream *st)
 
static int mpegts_write_pmt (AVFormatContext *s, MpegTSService *service)
 
static void mpegts_write_sdt (AVFormatContext *s)
 
static void mpegts_write_nit (AVFormatContext *s)
 
static int encode_str8 (uint8_t *buf, const char *str)
 
static int64_t get_pcr (const MpegTSWrite *ts)
 
static void write_packet (AVFormatContext *s, const uint8_t *packet)
 
static void section_write_packet (MpegTSSection *s, const uint8_t *packet)
 
static MpegTSServicempegts_add_service (AVFormatContext *s, int sid, const AVDictionary *metadata, AVProgram *program)
 
static void enable_pcr_generation_for_stream (AVFormatContext *s, AVStream *pcr_st)
 
static void select_pcr_streams (AVFormatContext *s)
 
static int mpegts_init (AVFormatContext *s)
 
static void retransmit_si_info (AVFormatContext *s, int force_pat, int force_sdt, int force_nit, int64_t pcr)
 
static int write_pcr_bits (uint8_t *buf, int64_t pcr)
 
static void mpegts_insert_null_packet (AVFormatContext *s)
 
static void mpegts_insert_pcr_only (AVFormatContext *s, AVStream *st)
 
static void write_pts (uint8_t *q, int fourbits, int64_t pts)
 
static void set_af_flag (uint8_t *pkt, int flag)
 
static void extend_af (uint8_t *pkt, int size)
 
static uint8_t * get_ts_payload_start (uint8_t *pkt)
 
static int get_pes_stream_id (AVFormatContext *s, AVStream *st, int stream_id, int *async)
 
static void mpegts_write_pes (AVFormatContext *s, AVStream *st, const uint8_t *payload, int payload_size, int64_t pts, int64_t dts, int key, int stream_id)
 
static int check_h26x_startcode (AVFormatContext *s, const AVStream *st, const AVPacket *pkt, const char *codec)
 
int ff_check_h264_startcode (AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
 Check presence of H264 startcode. More...
 
static int opus_get_packet_samples (AVFormatContext *s, AVPacket *pkt)
 
static uint8_t * h26x_prefix_aud (const uint8_t *aud, const int aud_size, const uint8_t *extra_data, const int extra_size, AVPacket *pkt, int *size)
 
static int mpegts_write_packet_internal (AVFormatContext *s, AVPacket *pkt)
 
static void mpegts_write_flush (AVFormatContext *s)
 
static int mpegts_write_packet (AVFormatContext *s, AVPacket *pkt)
 
static int mpegts_write_end (AVFormatContext *s)
 
static void mpegts_deinit (AVFormatContext *s)
 
static int mpegts_check_bitstream (AVFormatContext *s, AVStream *st, const AVPacket *pkt)
 

Variables

static const AVOption options []
 
static const AVClass mpegts_muxer_class
 
const FFOutputFormat ff_mpegts_muxer
 

Macro Definition Documentation

◆ PCR_TIME_BASE

#define PCR_TIME_BASE   27000000

Definition at line 44 of file mpegtsenc.c.

◆ DVB_PRIVATE_NETWORK_START

#define DVB_PRIVATE_NETWORK_START   0xff01

Definition at line 48 of file mpegtsenc.c.

◆ MPEGTS_FLAG_REEMIT_PAT_PMT

#define MPEGTS_FLAG_REEMIT_PAT_PMT   0x01

Definition at line 113 of file mpegtsenc.c.

◆ MPEGTS_FLAG_AAC_LATM

#define MPEGTS_FLAG_AAC_LATM   0x02

Definition at line 114 of file mpegtsenc.c.

◆ MPEGTS_FLAG_PAT_PMT_AT_FRAMES

#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES   0x04

Definition at line 115 of file mpegtsenc.c.

◆ MPEGTS_FLAG_SYSTEM_B

#define MPEGTS_FLAG_SYSTEM_B   0x08

Definition at line 116 of file mpegtsenc.c.

◆ MPEGTS_FLAG_DISCONT

#define MPEGTS_FLAG_DISCONT   0x10

Definition at line 117 of file mpegtsenc.c.

◆ MPEGTS_FLAG_NIT

#define MPEGTS_FLAG_NIT   0x20

Definition at line 118 of file mpegtsenc.c.

◆ MPEGTS_FLAG_OMIT_RAI

#define MPEGTS_FLAG_OMIT_RAI   0x40

Definition at line 119 of file mpegtsenc.c.

◆ DEFAULT_PES_HEADER_FREQ

#define DEFAULT_PES_HEADER_FREQ   16

Definition at line 136 of file mpegtsenc.c.

◆ DEFAULT_PES_PAYLOAD_SIZE

#define DEFAULT_PES_PAYLOAD_SIZE   ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)

Definition at line 137 of file mpegtsenc.c.

◆ SECTION_LENGTH

#define SECTION_LENGTH   1020

Definition at line 141 of file mpegtsenc.c.

◆ DEFAULT_PROVIDER_NAME

#define DEFAULT_PROVIDER_NAME   "FFmpeg"

Definition at line 237 of file mpegtsenc.c.

◆ DEFAULT_SERVICE_NAME

#define DEFAULT_SERVICE_NAME   "Service"

Definition at line 238 of file mpegtsenc.c.

◆ SDT_RETRANS_TIME

#define SDT_RETRANS_TIME   500

Definition at line 241 of file mpegtsenc.c.

◆ PAT_RETRANS_TIME

#define PAT_RETRANS_TIME   100

Definition at line 242 of file mpegtsenc.c.

◆ PCR_RETRANS_TIME

#define PCR_RETRANS_TIME   20

Definition at line 243 of file mpegtsenc.c.

◆ NIT_RETRANS_TIME

#define NIT_RETRANS_TIME   500

Definition at line 244 of file mpegtsenc.c.

◆ H264_NAL_TYPE

#define H264_NAL_TYPE (   state)    (state & 0x1f)

Definition at line 1856 of file mpegtsenc.c.

◆ HEVC_NAL_TYPE

#define HEVC_NAL_TYPE (   state)    ((state & 0x7e) >> 1)

Definition at line 1857 of file mpegtsenc.c.

◆ VVC_NAL_TYPE

#define VVC_NAL_TYPE (   state)    ((state >> 11) & 0x1f)

Definition at line 1858 of file mpegtsenc.c.

◆ OFFSET

#define OFFSET (   x)    offsetof(MpegTSWrite, x)

Definition at line 2328 of file mpegtsenc.c.

◆ ENC

#define ENC   AV_OPT_FLAG_ENCODING_PARAM

Definition at line 2329 of file mpegtsenc.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
MPEGTS_SERVICE_TYPE_DIGITAL_TV 
MPEGTS_SERVICE_TYPE_DIGITAL_RADIO 
MPEGTS_SERVICE_TYPE_TELETEXT 
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO 
MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV 
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV 
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV 
MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV 

Definition at line 71 of file mpegtsenc.c.

Function Documentation

◆ mpegts_write_section()

static void mpegts_write_section ( MpegTSSection s,
uint8_t *  buf,
int  len 
)
static

Definition at line 144 of file mpegtsenc.c.

Referenced by mpegts_write_section1().

◆ put16()

static void put16 ( uint8_t **  q_ptr,
int  val 
)
inlinestatic

◆ mpegts_write_section1()

static int mpegts_write_section1 ( MpegTSSection s,
int  tid,
int  id,
int  version,
int  sec_num,
int  last_sec_num,
uint8_t *  buf,
int  len 
)
static

◆ mpegts_write_pat()

static void mpegts_write_pat ( AVFormatContext s)
static

Definition at line 270 of file mpegtsenc.c.

Referenced by retransmit_si_info().

◆ putbuf()

static void putbuf ( uint8_t **  q_ptr,
const uint8_t *  buf,
size_t  len 
)
static

Definition at line 291 of file mpegtsenc.c.

Referenced by mpegts_write_nit(), mpegts_write_pmt(), and mpegts_write_sdt().

◆ put_arib_caption_descriptor()

static int put_arib_caption_descriptor ( AVFormatContext s,
uint8_t **  q_ptr,
AVCodecParameters codecpar 
)
static

Definition at line 297 of file mpegtsenc.c.

Referenced by mpegts_write_pmt().

◆ put_registration_descriptor()

static void put_registration_descriptor ( uint8_t **  q_ptr,
uint32_t  tag 
)
static

Definition at line 341 of file mpegtsenc.c.

Referenced by mpegts_write_pmt().

◆ get_dvb_stream_type()

static int get_dvb_stream_type ( AVFormatContext s,
AVStream st 
)
static

Definition at line 353 of file mpegtsenc.c.

Referenced by mpegts_write_pmt().

◆ get_m2ts_stream_type()

static int get_m2ts_stream_type ( AVFormatContext s,
AVStream st 
)
static

Definition at line 457 of file mpegtsenc.c.

Referenced by mpegts_write_pmt().

◆ mpegts_write_pmt()

static int mpegts_write_pmt ( AVFormatContext s,
MpegTSService service 
)
static

Definition at line 508 of file mpegtsenc.c.

Referenced by retransmit_si_info().

◆ mpegts_write_sdt()

static void mpegts_write_sdt ( AVFormatContext s)
static

Definition at line 847 of file mpegtsenc.c.

Referenced by retransmit_si_info().

◆ mpegts_write_nit()

static void mpegts_write_nit ( AVFormatContext s)
static

Definition at line 885 of file mpegtsenc.c.

Referenced by retransmit_si_info().

◆ encode_str8()

static int encode_str8 ( uint8_t *  buf,
const char *  str 
)
static

Definition at line 930 of file mpegtsenc.c.

Referenced by mpegts_add_service(), and mpegts_init().

◆ get_pcr()

static int64_t get_pcr ( const MpegTSWrite ts)
static

Definition at line 962 of file mpegtsenc.c.

Referenced by mpegts_insert_pcr_only(), mpegts_write_pes(), and write_packet().

◆ write_packet()

static void write_packet ( AVFormatContext s,
const uint8_t *  packet 
)
static

◆ section_write_packet()

static void section_write_packet ( MpegTSSection s,
const uint8_t *  packet 
)
static

Definition at line 982 of file mpegtsenc.c.

Referenced by mpegts_add_service(), and mpegts_init().

◆ mpegts_add_service()

static MpegTSService* mpegts_add_service ( AVFormatContext s,
int  sid,
const AVDictionary metadata,
AVProgram program 
)
static

Definition at line 988 of file mpegtsenc.c.

Referenced by mpegts_init().

◆ enable_pcr_generation_for_stream()

static void enable_pcr_generation_for_stream ( AVFormatContext s,
AVStream pcr_st 
)
static

Definition at line 1033 of file mpegtsenc.c.

Referenced by select_pcr_streams().

◆ select_pcr_streams()

static void select_pcr_streams ( AVFormatContext s)
static

Definition at line 1064 of file mpegtsenc.c.

Referenced by mpegts_init().

◆ mpegts_init()

static int mpegts_init ( AVFormatContext s)
static

Definition at line 1093 of file mpegtsenc.c.

◆ retransmit_si_info()

static void retransmit_si_info ( AVFormatContext s,
int  force_pat,
int  force_sdt,
int  force_nit,
int64_t  pcr 
)
static

Definition at line 1308 of file mpegtsenc.c.

Referenced by mpegts_write_pes().

◆ write_pcr_bits()

static int write_pcr_bits ( uint8_t *  buf,
int64_t  pcr 
)
static

Definition at line 1341 of file mpegtsenc.c.

Referenced by mpegts_insert_pcr_only(), and mpegts_write_pes().

◆ mpegts_insert_null_packet()

static void mpegts_insert_null_packet ( AVFormatContext s)
static

Definition at line 1356 of file mpegtsenc.c.

Referenced by mpegts_write_flush(), and mpegts_write_pes().

◆ mpegts_insert_pcr_only()

static void mpegts_insert_pcr_only ( AVFormatContext s,
AVStream st 
)
static

Definition at line 1371 of file mpegtsenc.c.

Referenced by mpegts_write_pes().

◆ write_pts()

static void write_pts ( uint8_t *  q,
int  fourbits,
int64_t  pts 
)
static

Definition at line 1399 of file mpegtsenc.c.

Referenced by mpegts_write_pes().

◆ set_af_flag()

static void set_af_flag ( uint8_t *  pkt,
int  flag 
)
static

Definition at line 1414 of file mpegtsenc.c.

Referenced by mpegts_write_pes().

◆ extend_af()

static void extend_af ( uint8_t *  pkt,
int  size 
)
static

Definition at line 1430 of file mpegtsenc.c.

Referenced by mpegts_write_pes().

◆ get_ts_payload_start()

static uint8_t* get_ts_payload_start ( uint8_t *  pkt)
static

Definition at line 1438 of file mpegtsenc.c.

Referenced by mpegts_write_pes().

◆ get_pes_stream_id()

static int get_pes_stream_id ( AVFormatContext s,
AVStream st,
int  stream_id,
int async 
)
static

Definition at line 1446 of file mpegtsenc.c.

Referenced by mpegts_write_pes().

◆ mpegts_write_pes()

static void mpegts_write_pes ( AVFormatContext s,
AVStream st,
const uint8_t *  payload,
int  payload_size,
int64_t  pts,
int64_t  dts,
int  key,
int  stream_id 
)
static

Definition at line 1481 of file mpegtsenc.c.

Referenced by mpegts_write_flush(), and mpegts_write_packet_internal().

◆ check_h26x_startcode()

static int check_h26x_startcode ( AVFormatContext s,
const AVStream st,
const AVPacket pkt,
const char *  codec 
)
static

Definition at line 1767 of file mpegtsenc.c.

Referenced by ff_check_h264_startcode(), and mpegts_write_packet_internal().

◆ ff_check_h264_startcode()

int ff_check_h264_startcode ( AVFormatContext s,
const AVStream st,
const AVPacket pkt 
)

Check presence of H264 startcode.

Returns
<0 to stop processing

Definition at line 1784 of file mpegtsenc.c.

Referenced by avi_write_packet(), mpegts_write_packet_internal(), and write_packet().

◆ opus_get_packet_samples()

static int opus_get_packet_samples ( AVFormatContext s,
AVPacket pkt 
)
static

Definition at line 1793 of file mpegtsenc.c.

Referenced by mpegts_write_packet_internal().

◆ h26x_prefix_aud()

static uint8_t* h26x_prefix_aud ( const uint8_t *  aud,
const int  aud_size,
const uint8_t *  extra_data,
const int  extra_size,
AVPacket pkt,
int size 
)
static

Definition at line 1841 of file mpegtsenc.c.

Referenced by mpegts_write_packet_internal().

◆ mpegts_write_packet_internal()

static int mpegts_write_packet_internal ( AVFormatContext s,
AVPacket pkt 
)
static

Definition at line 1859 of file mpegtsenc.c.

Referenced by mpegts_write_packet().

◆ mpegts_write_flush()

static void mpegts_write_flush ( AVFormatContext s)
static

Definition at line 2233 of file mpegtsenc.c.

Referenced by mpegts_write_end(), and mpegts_write_packet().

◆ mpegts_write_packet()

static int mpegts_write_packet ( AVFormatContext s,
AVPacket pkt 
)
static

Definition at line 2258 of file mpegtsenc.c.

◆ mpegts_write_end()

static int mpegts_write_end ( AVFormatContext s)
static

Definition at line 2268 of file mpegtsenc.c.

◆ mpegts_deinit()

static void mpegts_deinit ( AVFormatContext s)
static

Definition at line 2276 of file mpegtsenc.c.

◆ mpegts_check_bitstream()

static int mpegts_check_bitstream ( AVFormatContext s,
AVStream st,
const AVPacket pkt 
)
static

Definition at line 2302 of file mpegtsenc.c.

Variable Documentation

◆ options

const AVOption options[]
static

Definition at line 2330 of file mpegtsenc.c.

◆ mpegts_muxer_class

const AVClass mpegts_muxer_class
static
Initial value:
= {
.class_name = "MPEGTS muxer",
.item_name = av_default_item_name,
.option = options,
}

Definition at line 2393 of file mpegtsenc.c.

◆ ff_mpegts_muxer

const FFOutputFormat ff_mpegts_muxer
Initial value:
= {
.p.name = "mpegts",
.p.long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
.p.mime_type = "video/MP2T",
.p.extensions = "ts,m2t,m2ts,mts",
.priv_data_size = sizeof(MpegTSWrite),
.p.audio_codec = AV_CODEC_ID_MP2,
.p.video_codec = AV_CODEC_ID_MPEG2VIDEO,
.init = mpegts_init,
.write_packet = mpegts_write_packet,
.write_trailer = mpegts_write_end,
.deinit = mpegts_deinit,
.check_bitstream = mpegts_check_bitstream,
.flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
.p.priv_class = &mpegts_muxer_class,
}

Definition at line 2400 of file mpegtsenc.c.

AVFMT_NODIMENSIONS
#define AVFMT_NODIMENSIONS
Format does not need width/height.
Definition: avformat.h:483
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:482
mpegts_deinit
static void mpegts_deinit(AVFormatContext *s)
Definition: mpegtsenc.c:2276
mpegts_check_bitstream
static int mpegts_check_bitstream(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition: mpegtsenc.c:2302
mpegts_write_end
static int mpegts_write_end(AVFormatContext *s)
Definition: mpegtsenc.c:2268
mpegts_write_packet
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:2258
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:440
options
static const AVOption options[]
Definition: mpegtsenc.c:2330
mpegts_init
static int mpegts_init(AVFormatContext *s)
Definition: mpegtsenc.c:1093
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
FF_OFMT_FLAG_ALLOW_FLUSH
#define FF_OFMT_FLAG_ALLOW_FLUSH
This flag indicates that the muxer stores data internally and supports flushing it.
Definition: mux.h:38
MpegTSWrite
Definition: mpegtsenc.c:81
mpegts_muxer_class
static const AVClass mpegts_muxer_class
Definition: mpegtsenc.c:2393
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54