37 #define PCR_TIME_BASE 27000000
99 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
100 #define MPEGTS_FLAG_AAC_LATM 0x02
101 #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
114 #define DEFAULT_PES_HEADER_FREQ 16
115 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
119 #define SECTION_LENGTH 1020
126 const unsigned char *buf_ptr;
128 int first,
b, len1, left;
133 buf[len - 4] = (crc >> 24) & 0xff;
134 buf[len - 3] = (crc >> 16) & 0xff;
135 buf[len - 2] = (crc >> 8) & 0xff;
136 buf[len - 1] = crc & 0xff;
141 first = buf == buf_ptr;
149 s->
cc = s->
cc + 1 & 0xf;
156 memcpy(q, buf_ptr, len1);
161 memset(q, 0xff, left);
180 int version,
int sec_num,
int last_sec_num,
184 unsigned int tot_len;
188 tot_len = 3 + 5 + len + 4;
195 put16(&q, flags | (len + 5 + 4));
197 *q++ = 0xc1 | (version << 1);
209 #define DEFAULT_PROVIDER_NAME "FFmpeg"
210 #define DEFAULT_SERVICE_NAME "Service01"
213 #define SDT_RETRANS_TIME 500
214 #define PAT_RETRANS_TIME 100
215 #define PCR_RETRANS_TIME 20
253 int val, stream_type, i, err = 0;
258 program_info_length_ptr = q;
263 val = 0xf000 | (q - program_info_length_ptr - 2);
264 program_info_length_ptr[0] = val >> 8;
265 program_info_length_ptr[1] =
val;
346 char *next = lang->
value;
353 for (p = lang->
value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
358 next = strchr(p,
',');
359 if (strlen(p) != 3 && (!next || next != p + 3))
384 const char default_language[] =
"und";
385 const char *language = lang && strlen(lang->
value) >= 3 ? lang->
value : default_language;
389 int extradata_copied = 0;
394 while (strlen(language) >= 3) {
395 if (
sizeof(data) - (q - data) < 8) {
403 if (*language !=
'\0')
409 extradata_copied += 5;
419 extradata_copied += 4;
428 *len_ptr = q - len_ptr - 1;
431 int extradata_copied = 0;
437 while (strlen(language) >= 3 && q - data <
sizeof(data) - 6) {
442 if (*language !=
'\0')
447 extradata_copied += 2;
459 *len_ptr = q - len_ptr - 1;
485 val = 0xf000 | (q - desc_length_ptr - 2);
486 desc_length_ptr[0] = val >> 8;
487 desc_length_ptr[1] =
val;
492 "The PMT section cannot fit stream %d and all following streams.\n"
493 "Try reducing the number of languages in the audio streams "
494 "or the total number of streams.\n", i);
523 int i, running_status, free_ca_mode,
val;
532 desc_list_len_ptr = q;
544 desc_len_ptr[0] = q - desc_len_ptr - 1;
547 val = (running_status << 13) | (free_ca_mode << 12) |
548 (q - desc_list_len_ptr - 2);
549 desc_list_len_ptr[0] = val >> 8;
550 desc_list_len_ptr[1] =
val;
594 uint32_t tp_extra_header = pcr % 0x3fffffff;
595 tp_extra_header =
AV_RB32(&tp_extra_header);
597 sizeof(tp_extra_header));
616 const char *service_name;
637 provider_name, service_name);
644 service->
pmt.
cc = 15;
688 }
else if (st->
id < 0x1FFF) {
692 "Invalid stream id %d, must be less than 8191\n", st->
id);
701 for (j = 0; j < i; j++) {
702 if (pids[j] == ts_st->
pid) {
708 pids[i] = ts_st->
pid;
811 "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
880 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
882 *buf++ = pcr_high >> 25;
883 *buf++ = pcr_high >> 17;
884 *buf++ = pcr_high >> 9;
885 *buf++ = pcr_high >> 1;
886 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
918 *q++ = ts_st->
pid >> 8;
920 *q++ = 0x20 | ts_st->
cc;
938 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
940 val = (((pts >> 15) & 0x7fff) << 1) | 1;
943 val = (((
pts) & 0x7fff) << 1) | 1;
954 if ((pkt[3] & 0x20) == 0) {
976 return pkt + 5 + pkt[4];
986 const uint8_t *payload,
int payload_size,
987 int64_t
pts, int64_t dts,
int key)
993 int val, is_start,
len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext,
flags;
994 int afc_len, stuffing_len;
1005 while (payload_size > 0) {
1021 (dts -
get_pcr(ts, s->
pb) / 300) > delay) {
1034 val = ts_st->
pid >> 8;
1039 ts_st->
cc = ts_st->
cc + 1 & 0xf;
1040 *q++ = 0x10 | ts_st->
cc;
1055 pcr = (dts - delay) * 300;
1062 int pes_extension = 0;
1063 int pes_header_stuffing_bytes = 0;
1068 is_dvb_subtitle = 0;
1069 is_dvb_teletext = 0;
1088 is_dvb_subtitle = 1;
1090 is_dvb_teletext = 1;
1126 if (is_dvb_teletext) {
1127 pes_header_stuffing_bytes = 0x24 - header_len;
1130 len = payload_size + header_len + 3;
1132 if (is_dvb_subtitle) {
1177 if (is_dvb_subtitle) {
1184 if (is_dvb_teletext) {
1185 memset(q, 0xff, pes_header_stuffing_bytes);
1186 q += pes_header_stuffing_bytes;
1191 header_len = q -
buf;
1194 if (len > payload_size)
1197 if (stuffing_len > 0) {
1199 if (buf[3] & 0x20) {
1201 afc_len = buf[4] + 1;
1202 memmove(buf + 4 + afc_len + stuffing_len,
1204 header_len - (4 + afc_len));
1205 buf[4] += stuffing_len;
1206 memset(buf + 4 + afc_len, 0xff, stuffing_len);
1209 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1211 buf[4] = stuffing_len - 1;
1212 if (stuffing_len >= 2) {
1214 memset(buf + 6, 0xff, stuffing_len - 2);
1219 if (is_dvb_subtitle && payload_size == len) {
1227 payload_size -=
len;
1239 "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
1240 "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
1277 "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1303 uint32_t
state = -1;
1315 if ((state & 0x1f) == 7)
1317 }
while (p < buf_end && (state & 0x1f) != 9 &&
1318 (state & 0x1f) != 5 && (state & 0x1f) != 1);
1320 if ((state & 0x1f) != 5)
1322 if ((state & 0x1f) != 9) {
1327 memcpy(data + 6 + extradd, pkt->
data, pkt->
size);
1332 size = pkt->
size + 6 + extradd;
1335 if (pkt->
size < 2) {
1345 "and extradata missing\n");
1481 {
"mpegts_transport_stream_id",
"Set transport_stream_id field.",
1484 {
"mpegts_original_network_id",
"Set original_network_id field.",
1487 {
"mpegts_service_id",
"Set service_id field.",
1490 {
"mpegts_service_type",
"Set service_type field.",
1493 {
"digital_tv",
"Digital Television.",
1496 {
"digital_radio",
"Digital Radio.",
1499 {
"teletext",
"Teletext.",
1502 {
"advanced_codec_digital_radio",
"Advanced Codec Digital Radio.",
1505 {
"mpeg2_digital_hdtv",
"MPEG2 Digital HDTV.",
1508 {
"advanced_codec_digital_sdtv",
"Advanced Codec Digital SDTV.",
1511 {
"advanced_codec_digital_hdtv",
"Advanced Codec Digital HDTV.",
1514 {
"mpegts_pmt_start_pid",
"Set the first pid of the PMT.",
1517 {
"mpegts_start_pid",
"Set the first pid.",
1520 {
"mpegts_m2ts_mode",
"Enable m2ts mode.",
1526 {
"pes_payload_size",
"Minimum PES packet payload in bytes",
1529 {
"mpegts_flags",
"MPEG-TS muxing flags",
1532 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1535 {
"latm",
"Use LATM packetization for AAC",
1538 {
"pat_pmt_at_frames",
"Reemit PAT and PMT at each video frame",
1542 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1545 {
"mpegts_copyts",
"don't offset dts/pts",
1548 {
"tables_version",
"set PAT, PMT and SDT version",
1551 {
"omit_video_pes_length",
"Omit the PES packet length for video packets",
1554 {
"pcr_period",
"PCR retransmission time",
1557 {
"pat_period",
"PAT/PMT retransmission time limit in seconds",
1560 {
"sdt_period",
"SDT retransmission time limit in seconds",
1576 .mime_type =
"video/MP2T",
1577 .extensions =
"ts,m2t,m2ts,mts",
1585 .priv_class = &mpegts_muxer_class,
const char const char void * val
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
ptrdiff_t const GLvoid * data
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
#define STREAM_TYPE_VIDEO_CAVS
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
int first_pts_check
first pts check needed
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
Opaque data information usually continuous.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define DEFAULT_PES_PAYLOAD_SIZE
int id
Format-specific stream ID.
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
AVStream ** streams
A list of all streams in the file.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
int omit_video_pes_length
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
static void mpegts_insert_null_packet(AVFormatContext *s)
#define AV_LOG_VERBOSE
Detailed information.
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
static void mpegts_write_sdt(AVFormatContext *s)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
static void extend_af(uint8_t *pkt, int size)
#define MPEGTS_FLAG_AAC_LATM
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
struct AVOutputFormat * oformat
The output container format.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define STREAM_TYPE_AUDIO_AAC
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
struct MpegTSService * service
#define STREAM_TYPE_AUDIO_DTS
static void mpegts_prefix_m2ts_header(AVFormatContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVDictionary * metadata
Metadata that applies to the whole file.
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 write_pts(uint8_t *q, int fourbits, int64_t pts)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static uint8_t * get_ts_payload_start(uint8_t *pkt)
#define STREAM_TYPE_VIDEO_DIRAC
static void set_af_flag(uint8_t *pkt, int flag)
preferred ID for decoding MPEG audio layer 1, 2 or 3
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
simple assert() macros that are a bit more flexible than ISO C assert().
int mux_rate
set to 1 when VBR
static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
int flags
A combination of AV_PKT_FLAG values.
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
AVCodecContext * codec
Codec context associated with this stream.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
#define STREAM_TYPE_AUDIO_AAC_LATM
int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
Check presence of H264 startcode.
static const AVOption options[]
char filename[1024]
input or output filename
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
#define AV_TIME_BASE
Internal time base represented as integer.
static void putstr8(uint8_t **q_ptr, const char *str)
#define STREAM_TYPE_VIDEO_H264
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static int write_trailer(AVFormatContext *s1)
#define DEFAULT_PROVIDER_NAME
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
static int check_hevc_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
AVOutputFormat ff_mpegts_muxer
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
static int mpegts_write_header(AVFormatContext *s)
#define MPEGTS_FLAG_REEMIT_PAT_PMT
static void mpegts_write_pes(AVFormatContext *s, AVStream *st, const uint8_t *payload, int payload_size, int64_t pts, int64_t dts, int key)
preferred ID for MPEG-1/2 video decoding
#define STREAM_TYPE_VIDEO_MPEG4
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
int frame_size
Number of samples per channel in an audio frame.
enum AVMediaType codec_type
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
char * av_strdup(const char *s)
Duplicate the string s.
int sample_rate
samples per second
AVIOContext * pb
I/O context.
#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES
#define STREAM_TYPE_PRIVATE_DATA
Describe the class of an AVClass context structure.
rational number numerator/denominator
static int mpegts_write_end(AVFormatContext *s)
#define STREAM_TYPE_AUDIO_AC3
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
MpegTSService ** services
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
static int64_t pts
Global timestamp for the audio frames.
static void mpegts_write_flush(AVFormatContext *s)
#define STREAM_TYPE_VIDEO_HEVC
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
#define DEFAULT_SERVICE_NAME
common internal api header.
static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
int disposition
AV_DISPOSITION_* bit field.
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
int64_t nb_frames
number of frames in this stream if known or 0
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
#define STREAM_TYPE_VIDEO_MPEG2
void * priv_data
Format private data.
static void write_header(FFV1Context *f)
static void mpegts_write_pat(AVFormatContext *s)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static const AVClass mpegts_muxer_class
#define av_malloc_array(a, b)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
#define STREAM_TYPE_AUDIO_MPEG1
This structure stores compressed data.
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define STREAM_TYPE_AUDIO_TRUEHD
static MpegTSService * mpegts_add_service(MpegTSWrite *ts, int sid, const char *provider_name, const char *name)