22 #include "libavutil/bswap.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/mathematics.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
34 #define PCR_TIME_BASE 27000000
84 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
85 #define MPEGTS_FLAG_AAC_LATM 0x02
92 #define DEFAULT_PES_HEADER_FREQ 16
93 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
96 {
"mpegts_transport_stream_id",
"Set transport_stream_id field.",
98 {
"mpegts_original_network_id",
"Set original_network_id field.",
100 {
"mpegts_service_id",
"Set service_id field.",
102 {
"mpegts_pmt_start_pid",
"Set the first pid of the PMT.",
104 {
"mpegts_start_pid",
"Set the first pid.",
106 {
"mpegts_m2ts_mode",
"Enable m2ts mode.",
110 {
"pes_payload_size",
"Minimum PES packet payload in bytes",
114 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
117 {
"latm",
"Use LATM packetization for AAC",
121 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
123 {
"mpegts_copyts",
"don't offset dts/pts",
125 {
"tables_version",
"set PAT, PMT and SDT version",
142 const unsigned char *buf_ptr;
144 int first,
b, len1, left;
147 buf[len - 4] = (crc >> 24) & 0xff;
148 buf[len - 3] = (crc >> 16) & 0xff;
149 buf[len - 2] = (crc >> 8) & 0xff;
150 buf[len - 1] = (crc) & 0xff;
155 first = (buf == buf_ptr);
163 s->
cc = (s->
cc + 1) & 0xf;
170 memcpy(q, buf_ptr, len1);
175 memset(q, 0xff, left);
194 int version,
int sec_num,
int last_sec_num,
198 unsigned int tot_len;
202 tot_len = 3 + 5 + len + 4;
209 put16(&q, flags | (len + 5 + 4));
211 *q++ = 0xc1 | (version << 1);
223 #define DEFAULT_PROVIDER_NAME "FFmpeg"
224 #define DEFAULT_SERVICE_NAME "Service01"
227 #define SDT_RETRANS_TIME 500
228 #define PAT_RETRANS_TIME 100
229 #define PCR_RETRANS_TIME 20
265 uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
266 int val, stream_type, i;
271 program_info_length_ptr = q;
276 val = 0xf000 | (q - program_info_length_ptr - 2);
277 program_info_length_ptr[0] = val >> 8;
278 program_info_length_ptr[1] =
val;
342 char *next = lang->
value;
349 for (p = lang->
value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
350 next = strchr(p,
',');
351 if (strlen(p) != 3 && (!next || next != p + 3))
376 const char *language;
377 language = lang && strlen(lang->
value)==3 ? lang->
value :
"eng";
415 val = 0xf000 | (q - desc_length_ptr - 2);
416 desc_length_ptr[0] = val >> 8;
417 desc_length_ptr[1] =
val;
444 uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
445 int i, running_status, free_ca_mode,
val;
454 desc_list_len_ptr = q;
466 desc_len_ptr[0] = q - desc_len_ptr - 1;
469 val = (running_status << 13) | (free_ca_mode << 12) |
470 (q - desc_list_len_ptr - 2);
471 desc_list_len_ptr[0] = val >> 8;
472 desc_list_len_ptr[1] =
val;
508 uint32_t tp_extra_header = pcr % 0x3fffffff;
509 tp_extra_header =
AV_RB32(&tp_extra_header);
511 sizeof(tp_extra_header));
530 const char *service_name;
553 service->
pmt.
cc = 15;
589 }
else if (st->
id < 0x1FFF) {
601 for (j = 0; j < i; j++)
602 if (pids[j] == ts_st->
pid) {
607 pids[i] = ts_st->
pid;
693 "sdt every %d, pat/pmt every %d pkts\n",
748 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
750 *buf++ = pcr_high >> 25;
751 *buf++ = pcr_high >> 17;
752 *buf++ = pcr_high >> 9;
753 *buf++ = pcr_high >> 1;
754 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
786 *q++ = ts_st->
pid >> 8;
788 *q++ = 0x20 | ts_st->
cc;
806 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
808 val = (((pts >> 15) & 0x7fff) << 1) | 1;
811 val = (((pts) & 0x7fff) << 1) | 1;
822 if ((pkt[3] & 0x20) == 0) {
844 return pkt + 5 + pkt[4];
855 const uint8_t *payload,
int payload_size,
856 int64_t pts, int64_t dts,
int key)
862 int val, is_start,
len, header_len, write_pcr, private_code,
flags;
863 int afc_len, stuffing_len;
869 while (payload_size > 0) {
885 (dts -
get_pcr(ts, s->
pb)/300) > delay) {
897 val = (ts_st->
pid >> 8);
902 ts_st->
cc = (ts_st->
cc + 1) & 0xf;
903 *q++ = 0x10 | ts_st->
cc;
918 pcr = (dts - delay)*300;
925 int pes_extension = 0;
985 len = payload_size + header_len + 3;
986 if (private_code != 0)
1028 if (private_code != 0)
1029 *q++ = private_code;
1033 header_len = q -
buf;
1036 if (len > payload_size)
1039 if (stuffing_len > 0) {
1041 if (buf[3] & 0x20) {
1043 afc_len = buf[4] + 1;
1044 memmove(buf + 4 + afc_len + stuffing_len,
1046 header_len - (4 + afc_len));
1047 buf[4] += stuffing_len;
1048 memset(buf + 4 + afc_len, 0xff, stuffing_len);
1051 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1053 buf[4] = stuffing_len - 1;
1054 if (stuffing_len >= 2) {
1056 memset(buf + 6, 0xff, stuffing_len - 2);
1062 payload_size -=
len;
1079 int64_t dts = pkt->
dts, pts = pkt->
pts;
1082 av_log(s,
AV_LOG_WARNING,
"resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1108 uint32_t
state = -1;
1113 "no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)\n");
1121 av_dlog(s,
"nal %d\n", state & 0x1f);
1122 }
while (p < buf_end && (state & 0x1f) != 9 &&
1123 (state & 0x1f) != 5 && (state & 0x1f) != 1);
1125 if ((state & 0x1f) != 9) {
1129 memcpy(data+6, pkt->
data, pkt->
size);
1137 if (pkt->
size < 2) {
1147 "and extradata missing\n");
1275 .mime_type =
"video/x-mpegts",
1276 .extensions =
"ts,m2t,m2ts,mts",
1284 .priv_class = &mpegts_muxer_class,
const char const char void * val
static void retransmit_si_info(AVFormatContext *s, int force_pat)
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
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
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
#define STREAM_TYPE_VIDEO_CAVS
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
AVCodecContext * codec
Codec context associated with this stream.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", 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 avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *ptr)
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 * av_default_item_name(void *ctx)
Return the context name.
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 DEFAULT_PES_PAYLOAD_SIZE
int id
Format-specific stream ID.
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
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)
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
const OptionDef options[]
static void extend_af(uint8_t *pkt, int size)
#define MPEGTS_FLAG_AAC_LATM
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
#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)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
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)
void * priv_data
Format private data.
char filename[1024]
input or output filename
#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)
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.
unsigned int nb_streams
A list of all streams in the file.
#define STREAM_TYPE_AUDIO_AAC_LATM
Opaque data information usually continuous.
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
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
static int write_trailer(AVFormatContext *s1)
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define DEFAULT_PROVIDER_NAME
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length) av_pure
Calculate the CRC of a block.
AVOutputFormat ff_mpegts_muxer
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
int frame_size
Number of samples per channel in an audio frame.
enum AVMediaType codec_type
typedef void(RENAME(mix_any_func_type))
int sample_rate
samples per second
AVIOContext * pb
I/O context.
#define STREAM_TYPE_PRIVATE_DATA
Describe the class of an AVClass context structure.
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 void mpegts_write_flush(AVFormatContext *s)
#define DEFAULT_SERVICE_NAME
common internal api header.
static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
int disposition
AV_DISPOSITION_* bit field.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
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
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
struct AVOutputFormat * oformat
#define AVERROR_INVALIDDATA
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
#define STREAM_TYPE_VIDEO_MPEG2
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 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_assert0(cond)
assert() equivalent, that is always enabled.
#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)
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.
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static MpegTSService * mpegts_add_service(MpegTSWrite *ts, int sid, const char *provider_name, const char *name)