33 #define PCR_TIME_BASE 27000000
82 #define DEFAULT_PES_HEADER_FREQ 16
83 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
86 {
"mpegts_transport_stream_id",
"Set transport_stream_id field.",
88 {
"mpegts_original_network_id",
"Set original_network_id field.",
90 {
"mpegts_service_id",
"Set service_id field.",
92 {
"mpegts_pmt_start_pid",
"Set the first pid of the PMT.",
94 {
"mpegts_start_pid",
"Set the first pid.",
97 {
"pes_payload_size",
"Minimum PES packet payload in bytes",
111 #define SECTION_LENGTH 1020
118 const unsigned char *buf_ptr;
120 int first,
b, len1, left;
123 buf[len - 4] = (crc >> 24) & 0xff;
124 buf[len - 3] = (crc >> 16) & 0xff;
125 buf[len - 2] = (crc >> 8) & 0xff;
126 buf[len - 1] = (crc) & 0xff;
131 first = (buf == buf_ptr);
139 s->
cc = (s->
cc + 1) & 0xf;
146 memcpy(q, buf_ptr, len1);
151 memset(q, 0xff, left);
160 static inline void put16(uint8_t **q_ptr,
int val)
170 int version,
int sec_num,
int last_sec_num,
171 uint8_t *buf,
int len)
173 uint8_t section[1024], *q;
174 unsigned int tot_len;
178 tot_len = 3 + 5 + len + 4;
185 put16(&q, flags | (len + 5 + 4));
187 *q++ = 0xc1 | (version << 1);
199 #define DEFAULT_PROVIDER_NAME "Libav"
200 #define DEFAULT_SERVICE_NAME "Service01"
203 #define SDT_RETRANS_TIME 500
204 #define PAT_RETRANS_TIME 100
205 #define PCR_RETRANS_TIME 20
240 int val, stream_type, i, err = 0;
245 program_info_length_ptr = q;
250 val = 0xf000 | (q - program_info_length_ptr - 2);
251 program_info_length_ptr[0] = val >> 8;
252 program_info_length_ptr[1] = val;
304 char *next = lang->
value;
311 for (p = lang->
value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
316 next = strchr(p,
',');
317 if (strlen(p) != 3 && (!next || next != p + 3))
342 const char *language;
343 language = lang && strlen(lang->
value)==3 ? lang->
value :
"eng";
376 val = 0xf000 | (q - desc_length_ptr - 2);
377 desc_length_ptr[0] = val >> 8;
378 desc_length_ptr[1] = val;
383 "The PMT section is too small for stream %d and following.\n"
384 "Try reducing the number of languages in the audio streams "
385 "or the total number of streams.\n",
393 static void putstr8(uint8_t **q_ptr,
const char *str)
414 int i, running_status, free_ca_mode, val;
423 desc_list_len_ptr = q;
435 desc_len_ptr[0] = q - desc_len_ptr - 1;
438 val = (running_status << 13) | (free_ca_mode << 12) |
439 (q - desc_list_len_ptr - 2);
440 desc_list_len_ptr[0] = val >> 8;
441 desc_list_len_ptr[1] = val;
480 const char *service_name;
499 service->
pmt.
cc = 15;
531 }
else if (st->
id < 0x1FFF) {
541 for (j = 0; j < i; j++)
542 if (pids[j] == ts_st->
pid) {
546 pids[i] = ts_st->
pid;
621 "sdt every %d, pat/pmt every %d pkts\n",
671 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
673 *buf++ = pcr_high >> 25;
674 *buf++ = pcr_high >> 17;
675 *buf++ = pcr_high >> 9;
676 *buf++ = pcr_high >> 1;
677 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
708 *q++ = ts_st->
pid >> 8;
710 *q++ = 0x20 | ts_st->
cc;
723 static void write_pts(uint8_t *q,
int fourbits, int64_t pts)
727 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
729 val = (((pts >> 15) & 0x7fff) << 1) | 1;
732 val = (((pts) & 0x7fff) << 1) | 1;
743 if ((pkt[3] & 0x20) == 0) {
757 assert(pkt[3] & 0x20);
765 return pkt + 5 + pkt[4];
776 const uint8_t *payload,
int payload_size,
777 int64_t pts, int64_t dts,
int key)
783 int val, is_start,
len, header_len, write_pcr, private_code,
flags;
784 int afc_len, stuffing_len;
789 while (payload_size > 0) {
804 (dts -
get_pcr(ts, s->
pb)/300) > delay) {
816 val = (ts_st->
pid >> 8);
821 ts_st->
cc = (ts_st->
cc + 1) & 0xf;
822 *q++ = 0x10 | ts_st->
cc;
837 pcr = (dts - delay)*300;
844 int pes_extension = 0;
889 len = payload_size + header_len + 3;
890 if (private_code != 0)
921 if (private_code != 0)
926 header_len = q - buf;
929 if (len > payload_size)
932 if (stuffing_len > 0) {
936 afc_len = buf[4] + 1;
937 memmove(buf + 4 + afc_len + stuffing_len,
939 header_len - (4 + afc_len));
940 buf[4] += stuffing_len;
941 memset(buf + 4 + afc_len, 0xff, stuffing_len);
944 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
946 buf[4] = stuffing_len - 1;
947 if (stuffing_len >= 2) {
949 memset(buf + 6, 0xff, stuffing_len - 2);
965 uint8_t *buf= pkt->
data;
973 pts = pkt->
pts + delay;
975 dts = pkt->
dts + delay;
984 const uint8_t *p = buf, *buf_end = p+
size;
989 "no startcode found, use -vbsf h264_mp4toannexb\n");
996 }
while (p < buf_end && (state & 0x1f) != 9 &&
997 (state & 0x1f) != 5 && (state & 0x1f) != 1);
999 if ((state & 0x1f) != 9) {
1003 memcpy(data+6, pkt->
data, pkt->
size);
1018 "and extradata missing\n");
1022 if ((
unsigned)new_size >= INT_MAX)
1115 .mime_type =
"video/x-mpegts",
1116 .extensions =
"ts,m2t",
1123 .priv_class = &mpegts_muxer_class,
uint8_t pce_data[MAX_PCE_SIZE]
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_WB24 AV_RB16
AV_WL32 AV_WL24 AV_WL16 AV_RB32
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
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 DEFAULT_PES_PAYLOAD_SIZE
int id
format-specific stream ID
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
static void mpegts_insert_null_packet(AVFormatContext *s)
struct MpegTSService MpegTSService
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)
preferred ID for MPEG-1/2 video decoding
#define STREAM_TYPE_AUDIO_AAC
struct MpegTSService * service
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)
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
void av_log(void *avcl, int level, const char *fmt,...)
int mux_rate
set to 1 when VBR
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
int flags
A combination of AV_PKT_FLAG values.
AVCodecContext * codec
codec context
static void retransmit_si_info(AVFormatContext *s)
unsigned int nb_streams
A list of all streams in the file.
#define STREAM_TYPE_AUDIO_AAC_LATM
static const AVOption options[]
int void avio_flush(AVIOContext *s)
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
static int mpegts_write_header(AVFormatContext *s)
static void mpegts_write_pes(AVFormatContext *s, AVStream *st, const uint8_t *payload, int payload_size, int64_t pts, int64_t dts, int key)
#define STREAM_TYPE_VIDEO_MPEG4
int ff_adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
int frame_size
Samples per packet, initialized when calling 'init'.
const uint8_t * avpriv_mpv_find_start_code(const uint8_t *restrict p, const uint8_t *end, uint32_t *restrict state)
enum AVMediaType codec_type
char * av_strdup(const char *s)
Duplicate the string s.
int sample_rate
samples per second
int ff_adts_write_frame_header(ADTSContext *ctx, uint8_t *buf, int size, int pce_size)
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define STREAM_TYPE_PRIVATE_DATA
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Describe the class of an AVClass context structure.
static int mpegts_write_end(AVFormatContext *s)
#define STREAM_TYPE_AUDIO_AC3
MpegTSService ** services
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
struct MpegTSWriteStream MpegTSWriteStream
#define DEFAULT_SERVICE_NAME
static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
int disposition
AV_DISPOSITION_* bit field.
struct MpegTSWrite MpegTSWrite
static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
#define STREAM_TYPE_VIDEO_MPEG2
void * priv_data
Format private data.
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
struct MpegTSSection MpegTSSection
#define STREAM_TYPE_AUDIO_MPEG1
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
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.
preferred ID for decoding MPEG audio layer 1, 2 or 3
static MpegTSService * mpegts_add_service(MpegTSWrite *ts, int sid, const char *provider_name, const char *name)