36 #define PCR_TIME_BASE 27000000 40 #define DVB_PRIVATE_NETWORK_START 0xff01 89 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01 90 #define MPEGTS_FLAG_AAC_LATM 0x02 91 #define MPEGTS_FLAG_SYSTEM_B 0x04 96 #define DEFAULT_PES_HEADER_FREQ 16 97 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170) 101 #define SECTION_LENGTH 1020 108 const unsigned char *buf_ptr;
110 int first,
b, len1, left;
115 buf[len - 4] = (crc >> 24) & 0xff;
116 buf[len - 3] = (crc >> 16) & 0xff;
117 buf[len - 2] = (crc >> 8) & 0xff;
118 buf[len - 1] = crc & 0xff;
123 first = buf == buf_ptr;
131 s->
cc = s->
cc + 1 & 0xf;
138 memcpy(q, buf_ptr, len1);
143 memset(q, 0xff, left);
162 int version,
int sec_num,
int last_sec_num,
166 unsigned int tot_len;
170 tot_len = 3 + 5 + len + 4;
177 put16(&q, flags | (len + 5 + 4));
179 *q++ = 0xc1 | (version << 1);
191 #define DEFAULT_PROVIDER_NAME "Libav" 192 #define DEFAULT_SERVICE_NAME "Service01" 195 #define SDT_RETRANS_TIME 500 196 #define PAT_RETRANS_TIME 100 197 #define PCR_RETRANS_TIME 20 234 int val, stream_type, i, err = 0;
239 program_info_length_ptr = q;
244 val = 0xf000 | (q - program_info_length_ptr - 2);
245 program_info_length_ptr[0] = val >> 8;
246 program_info_length_ptr[1] = val;
314 char *next = lang->
value;
321 for (p = lang->
value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
326 next = strchr(p,
',');
327 if (strlen(p) != 3 && (!next || next != p + 3))
352 const char *language;
353 language = lang && strlen(lang->
value) == 3 ? lang->
value :
"eng";
387 val = 0xf000 | (q - desc_length_ptr - 2);
388 desc_length_ptr[0] = val >> 8;
389 desc_length_ptr[1] = val;
394 "The PMT section cannot fit stream %d and all following streams.\n" 395 "Try reducing the number of languages in the audio streams " 396 "or the total number of streams.\n", i);
424 int i, running_status, free_ca_mode, val;
433 desc_list_len_ptr = q;
445 desc_len_ptr[0] = q - desc_len_ptr - 1;
448 val = (running_status << 13) | (free_ca_mode << 12) |
449 (q - desc_list_len_ptr - 2);
450 desc_list_len_ptr[0] = val >> 8;
451 desc_list_len_ptr[1] = val;
492 uint32_t tp_extra_header = pcr % 0x3fffffff;
493 tp_extra_header =
AV_RB32(&tp_extra_header);
495 sizeof(tp_extra_header));
514 const char *service_name;
535 provider_name, service_name);
542 service->
pmt.
cc = 15;
586 }
else if (st->
id < 0x1FFF) {
590 "Invalid stream id %d, must be less than 8191\n", st->
id);
599 for (j = 0; j < i; j++) {
600 if (pids[j] == ts_st->
pid) {
606 pids[i] = ts_st->
pid;
697 "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
751 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
753 *buf++ = pcr_high >> 25;
754 *buf++ = pcr_high >> 17;
755 *buf++ = pcr_high >> 9;
756 *buf++ = pcr_high >> 1;
757 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
789 *q++ = ts_st->
pid >> 8;
791 *q++ = 0x20 | ts_st->
cc;
809 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
811 val = (((pts >> 15) & 0x7fff) << 1) | 1;
814 val = (((
pts) & 0x7fff) << 1) | 1;
825 if ((pkt[3] & 0x20) == 0) {
839 assert(pkt[3] & 0x20);
847 return pkt + 5 + pkt[4];
857 const uint8_t *payload,
int payload_size,
858 int64_t
pts, int64_t dts,
int key)
864 int val, is_start,
len, header_len, write_pcr, private_code,
flags;
865 int afc_len, stuffing_len;
870 while (payload_size > 0) {
885 (dts -
get_pcr(ts, s->
pb) / 300) > delay) {
898 val = ts_st->
pid >> 8;
903 ts_st->
cc = ts_st->
cc + 1 & 0xf;
904 *q++ = 0x10 | ts_st->
cc;
919 pcr = (dts - delay) * 300;
926 int pes_extension = 0;
968 len = payload_size + header_len + 3;
969 if (private_code != 0)
998 if (private_code != 0)
1003 header_len = q - buf;
1006 if (len > payload_size)
1009 if (stuffing_len > 0) {
1011 if (buf[3] & 0x20) {
1013 afc_len = buf[4] + 1;
1014 memmove(buf + 4 + afc_len + stuffing_len,
1016 header_len - (4 + afc_len));
1017 buf[4] += stuffing_len;
1018 memset(buf + 4 + afc_len, 0xff, stuffing_len);
1021 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1023 buf[4] = stuffing_len - 1;
1024 if (stuffing_len >= 2) {
1026 memset(buf + 6, 0xff, stuffing_len - 2);
1032 payload_size -=
len;
1052 "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1064 pts = pkt->
pts + delay;
1066 dts = pkt->
dts + delay;
1076 uint32_t
state = -1;
1080 "no startcode found, use -bsf h264_mp4toannexb\n");
1087 }
while (p < buf_end && (state & 0x1f) != 9 &&
1088 (state & 0x1f) != 5 && (state & 0x1f) != 1);
1090 if ((state & 0x1f) != 9) {
1094 memcpy(data + 6, pkt->
data, pkt->
size);
1099 size = pkt->
size + 6;
1102 if (pkt->
size < 2) {
1112 "and extradata missing\n");
1234 {
"mpegts_transport_stream_id",
"Set transport_stream_id field.",
1237 {
"mpegts_original_network_id",
"Set original_network_id field.",
1240 {
"mpegts_service_id",
"Set service_id field.",
1242 { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1243 {
"mpegts_pmt_start_pid",
"Set the first pid of the PMT.",
1245 { .i64 = 0x1000 }, 0x1000, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
1246 {
"mpegts_start_pid",
"Set the first pid.",
1248 { .i64 = 0x0100 }, 0x0100, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
1249 {
"mpegts_m2ts_mode",
"Enable m2ts mode.",
1251 { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
1254 { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1255 {
"pes_payload_size",
"Minimum PES packet payload in bytes",
1258 {
"mpegts_flags",
"MPEG-TS muxing flags",
1261 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1264 {
"latm",
"Use LATM packetization for AAC",
1267 {
"system_b",
"Conform to System B (DVB) instead of System A (ATSC)",
1271 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1273 { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1274 {
"pcr_period",
"PCR retransmission time",
1290 .mime_type =
"video/MP2T",
1291 .extensions =
"ts,m2t,m2ts,mts",
1299 .priv_class = &mpegts_muxer_class,
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
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 STREAM_TYPE_VIDEO_CAVS
static const uint8_t frame_size[4]
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
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...
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define DEFAULT_PES_PAYLOAD_SIZE
int id
Format-specific stream ID.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
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)
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
struct AVOutputFormat * oformat
The output container format.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define STREAM_TYPE_AUDIO_AAC
struct MpegTSService * service
static void mpegts_prefix_m2ts_header(AVFormatContext *s)
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVDictionary * metadata
Metadata that applies to the whole file.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
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
enum AVMediaType codec_type
General type of the encoded data.
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
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 av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters in...
#define MPEGTS_FLAG_SYSTEM_B
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.
int extradata_size
Size of the extradata content in bytes.
static void retransmit_si_info(AVFormatContext *s)
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
#define STREAM_TYPE_AUDIO_AAC_LATM
static const AVOption options[]
int void avio_flush(AVIOContext *s)
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
#define DEFAULT_PROVIDER_NAME
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.
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
#define DVB_PRIVATE_NETWORK_START
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
char * av_strdup(const char *s)
Duplicate the string s.
AVIOContext * pb
I/O context.
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
#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.
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)
const uint8_t * avpriv_find_start_code(const uint8_t *restrict p, const uint8_t *end, uint32_t *restrict state)
#define STREAM_TYPE_VIDEO_HEVC
int sample_rate
Audio only.
#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.
static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
#define STREAM_TYPE_VIDEO_MPEG2
void * priv_data
Format private data.
static void write_header(FFV1Context *f)
static void mpegts_write_pat(AVFormatContext *s)
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static const AVClass mpegts_muxer_class
AVCodecParameters * codecpar
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 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.
static MpegTSService * mpegts_add_service(MpegTSWrite *ts, int sid, const char *provider_name, const char *name)