avformat/mpegts*: reduce use of magic numbers

Note ISO/IEC 13818-1 defines an Extension_descriptor with descriptor_tag value
0x3f (63), so I kept the DVB comment.

I don't know what defines stream_type value 0x8a as DTS.

I don't have any Blu-ray standards so I don't know where those stream_type
values are defined.

Signed-off-by: Marton Balint <cus@passwd.hu>
pull/391/head
Scott Theisen 2 months ago committed by Marton Balint
parent 56419fd096
commit 1259760825
  1. 150
      libavformat/mpegts.c
  2. 59
      libavformat/mpegts.h
  3. 78
      libavformat/mpegtsenc.c

@ -444,7 +444,7 @@ static void write_section_data(MpegTSContext *ts, MpegTSFilter *tss1,
offset = 0;
cur_section_buf = tss->section_buf;
while (cur_section_buf - tss->section_buf < MAX_SECTION_SIZE && cur_section_buf[0] != 0xff) {
while (cur_section_buf - tss->section_buf < MAX_SECTION_SIZE && cur_section_buf[0] != STUFFING_BYTE) {
/* compute section length if possible */
if (tss->section_h_size == -1 && tss->section_index - offset >= 3) {
len = (AV_RB16(cur_section_buf + 1) & 0xfff) + 3;
@ -590,7 +590,7 @@ static int analyze(const uint8_t *buf, int size, int packet_size,
memset(stat, 0, packet_size * sizeof(*stat));
for (i = 0; i < size - 3; i++) {
if (buf[i] == 0x47) {
if (buf[i] == SYNC_BYTE) {
int pid = AV_RB16(buf+1) & 0x1FFF;
int asc = buf[i + 3] & 0x30;
if (!probe || pid == 0x1FFF || asc) {
@ -798,66 +798,66 @@ typedef struct StreamType {
} StreamType;
static const StreamType ISO_types[] = {
{ 0x01, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
{ 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
{ 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
{ 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
{ 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
{ 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4 },
{ STREAM_TYPE_VIDEO_MPEG1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
{ STREAM_TYPE_VIDEO_MPEG2, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
{ STREAM_TYPE_AUDIO_MPEG1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
{ STREAM_TYPE_AUDIO_MPEG2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
{ STREAM_TYPE_AUDIO_AAC, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
{ STREAM_TYPE_VIDEO_MPEG4, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4 },
/* Makito encoder sets stream type 0x11 for AAC,
* so auto-detect LOAS/LATM instead of hardcoding it. */
#if !CONFIG_LOAS_DEMUXER
{ 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
{ STREAM_TYPE_AUDIO_AAC_LATM, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
#endif
{ 0x1b, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
{ 0x1c, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
{ 0x20, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
{ 0x21, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_JPEG2000 },
{ 0x24, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
{ 0x33, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VVC },
{ 0x42, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS },
{ 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
{ 0xd2, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_AVS2 },
{ 0xd4, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_AVS3 },
{ 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
{ STREAM_TYPE_VIDEO_H264, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
{ STREAM_TYPE_AUDIO_MPEG4, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
{ STREAM_TYPE_VIDEO_MVC, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
{ STREAM_TYPE_VIDEO_JPEG2000, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_JPEG2000 },
{ STREAM_TYPE_VIDEO_HEVC, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
{ STREAM_TYPE_VIDEO_VVC, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VVC },
{ STREAM_TYPE_VIDEO_CAVS, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS },
{ STREAM_TYPE_VIDEO_DIRAC, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
{ STREAM_TYPE_VIDEO_AVS2, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_AVS2 },
{ STREAM_TYPE_VIDEO_AVS3, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_AVS3 },
{ STREAM_TYPE_VIDEO_VC1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
{ 0 },
};
static const StreamType HDMV_types[] = {
{ 0x80, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_PCM_BLURAY },
{ 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
{ 0x82, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
{ 0x83, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_TRUEHD },
{ 0x84, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
{ 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */
{ 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/
{ 0xa1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC3 Secondary Audio */
{ 0xa2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS Express Secondary Audio */
{ 0x90, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE },
{ 0x92, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_TEXT_SUBTITLE },
{ STREAM_TYPE_BLURAY_AUDIO_PCM_BLURAY, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_PCM_BLURAY },
{ STREAM_TYPE_BLURAY_AUDIO_AC3, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
{ STREAM_TYPE_BLURAY_AUDIO_DTS, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
{ STREAM_TYPE_BLURAY_AUDIO_TRUEHD, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_TRUEHD },
{ STREAM_TYPE_BLURAY_AUDIO_EAC3, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
{ STREAM_TYPE_BLURAY_AUDIO_DTS_HD, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
{ STREAM_TYPE_BLURAY_AUDIO_DTS_HD_MASTER, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
{ STREAM_TYPE_BLURAY_AUDIO_EAC3_SECONDARY, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
{ STREAM_TYPE_BLURAY_AUDIO_DTS_EXPRESS_SECONDARY, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
{ STREAM_TYPE_BLURAY_SUBTITLE_PGS, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE },
{ STREAM_TYPE_BLURAY_SUBTITLE_TEXT, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_TEXT_SUBTITLE },
{ 0 },
};
/* SCTE types */
static const StreamType SCTE_types[] = {
{ 0x86, AVMEDIA_TYPE_DATA, AV_CODEC_ID_SCTE_35 },
{ STREAM_TYPE_SCTE_DATA_SCTE_35, AVMEDIA_TYPE_DATA, AV_CODEC_ID_SCTE_35 },
{ 0 },
};
/* ATSC ? */
static const StreamType MISC_types[] = {
{ 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
{ 0x87, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
{ STREAM_TYPE_ATSC_AUDIO_AC3, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
{ STREAM_TYPE_ATSC_AUDIO_EAC3, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
{ 0x8a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
{ 0 },
};
/* HLS Sample Encryption Types */
static const StreamType HLS_SAMPLE_ENC_types[] = {
{ 0xdb, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264},
{ 0xcf, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
{ 0xc1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
{ 0xc2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3},
{ STREAM_TYPE_HLS_SE_VIDEO_H264, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264},
{ STREAM_TYPE_HLS_SE_AUDIO_AAC, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
{ STREAM_TYPE_HLS_SE_AUDIO_AC3, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
{ STREAM_TYPE_HLS_SE_AUDIO_EAC3, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3},
{ 0 },
};
@ -888,11 +888,11 @@ static const StreamType METADATA_types[] = {
/* descriptor present */
static const StreamType DESC_types[] = {
{ 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */
{ 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
{ 0x7b, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
{ 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT },
{ 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
{ AC3_DESCRIPTOR, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
{ ENHANCED_AC3_DESCRIPTOR, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
{ DTS_DESCRIPTOR, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
{ TELETEXT_DESCRIPTOR, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT },
{ SUBTITLING_DESCRIPTOR, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE },
{ 0 },
};
@ -937,13 +937,13 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
st->codecpar->codec_tag = pes->stream_type;
mpegts_find_stream_type(st, pes->stream_type, ISO_types);
if (pes->stream_type == 4 || pes->stream_type == 0x0f)
if (pes->stream_type == STREAM_TYPE_AUDIO_MPEG2 || pes->stream_type == STREAM_TYPE_AUDIO_AAC)
sti->request_probe = 50;
if ((prog_reg_desc == AV_RL32("HDMV") ||
prog_reg_desc == AV_RL32("HDPR")) &&
st->codecpar->codec_id == AV_CODEC_ID_NONE) {
mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
if (pes->stream_type == 0x83) {
if (pes->stream_type == STREAM_TYPE_BLURAY_AUDIO_TRUEHD) {
// HDMV TrueHD streams also contain an AC3 coded version of the
// audio track - add a second stream for this
AVStream *sub_st;
@ -1028,7 +1028,7 @@ static int new_pes_packet(PESContext *pes, AVPacket *pkt)
memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
// Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
if (pes->sub_st && pes->stream_type == STREAM_TYPE_BLURAY_AUDIO_TRUEHD && pes->extended_stream_id == 0x76)
pkt->stream_index = pes->sub_st->index;
else
pkt->stream_index = pes->st->index;
@ -1299,7 +1299,7 @@ skip:
/* we got the full header. We parse it and get the payload */
pes->state = MPEGTS_PAYLOAD;
pes->data_index = 0;
if (pes->stream_type == 0x12 && buf_size > 0) {
if (pes->stream_type == STREAM_TYPE_ISO_IEC_14496_PES && buf_size > 0) {
int sl_header_bytes = read_sl_header(pes, &pes->sl, p,
buf_size);
pes->pes_header_size += sl_header_bytes;
@ -1343,7 +1343,7 @@ skip:
// the standard says they should be handled after 40.6 ms at most,
// and the pcr error to this packet should be no more than 100 ms.
// TODO: we should interpolate the PCR, not just use the last one
int64_t pcr = f->last_pcr / 300;
int64_t pcr = f->last_pcr / SYSTEM_CLOCK_FREQUENCY_DIVISOR;
pcr_found = 1;
if (st) {
const FFStream *const sti = ffstream(st);
@ -1395,7 +1395,7 @@ skip:
ts->stop_parse = 1;
} else if (pes->data_index == 0 &&
buf_size > max_packet_size) {
// pes packet size is < ts size packet and pes data is padded with 0xff
// pes packet size is < ts size packet and pes data is padded with STUFFING_BYTE
// not sure if this is legal in ts but see issue #2392
buf_size = max_packet_size;
}
@ -1788,7 +1788,7 @@ static void scte_data_cb(MpegTSFilter *filter, const uint8_t *section,
if (prg && prg->pcr_pid != -1 && prg->discard != AVDISCARD_ALL) {
MpegTSFilter *f = ts->pids[prg->pcr_pid];
if (f && f->last_pcr != -1)
ts->pkt->pts = ts->pkt->dts = f->last_pcr/300;
ts->pkt->pts = ts->pkt->dts = f->last_pcr/SYSTEM_CLOCK_FREQUENCY_DIVISOR;
}
ts->stop_parse = 1;
@ -1888,7 +1888,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
}
}
break;
case 0x56: /* DVB teletext descriptor */
case TELETEXT_DESCRIPTOR:
{
uint8_t *extradata = NULL;
int language_count = desc_len / 5, ret;
@ -1929,7 +1929,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
}
}
break;
case 0x59: /* subtitling descriptor */
case SUBTITLING_DESCRIPTOR:
{
/* 8 bytes per DVB subtitle substream data:
* ISO_639_language_code (3 bytes),
@ -2028,7 +2028,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
sti->request_probe = 50;
}
break;
case 0x52: /* stream identifier descriptor */
case STREAM_IDENTIFIER_DESCRIPTOR:
sti->stream_identifier = 1 + get8(pp, desc_end);
break;
case METADATA_DESCRIPTOR:
@ -2040,7 +2040,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
mpegts_find_stream_type(st, st->codecpar->codec_tag, METADATA_types);
}
break;
case 0x7f: /* DVB extension descriptor */
case EXTENSION_DESCRIPTOR: /* DVB extension descriptor */
ext_desc_tag = get8(pp, desc_end);
if (ext_desc_tag < 0)
return AVERROR_INVALIDDATA;
@ -2073,7 +2073,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
sti->need_context_update = 1;
}
}
if (ext_desc_tag == 0x06) { /* supplementary audio descriptor */
if (ext_desc_tag == SUPPLEMENTARY_AUDIO_DESCRIPTOR) {
int flags;
if (desc_len < 1)
@ -2111,7 +2111,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
}
}
break;
case 0x6a: /* ac-3_descriptor */
case AC3_DESCRIPTOR:
{
int component_type_flag = get8(pp, desc_end) & (1 << 7);
if (component_type_flag) {
@ -2125,7 +2125,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
}
}
break;
case 0x7a: /* enhanced_ac-3_descriptor */
case ENHANCED_AC3_DESCRIPTOR:
{
int component_type_flag = get8(pp, desc_end) & (1 << 7);
if (component_type_flag) {
@ -2139,7 +2139,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
}
}
break;
case 0xfd: /* ARIB data coding type descriptor */
case DATA_COMPONENT_DESCRIPTOR:
// STD-B24, fascicle 3, chapter 4 defines private_stream_1
// for captions
if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
@ -2187,7 +2187,7 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
sti->need_parsing = 0;
}
break;
case 0xb0: /* DOVI video stream descriptor */
case DOVI_VIDEO_STREAM_DESCRIPTOR:
{
uint32_t buf;
AVDOVIDecoderConfigurationRecord *dovi;
@ -2305,7 +2305,7 @@ static int parse_stream_identifier_desc(const uint8_t *p, const uint8_t *p_end)
if (desc_end > desc_list_end)
return -1;
if (desc_tag == 0x52) {
if (desc_tag == STREAM_IDENTIFIER_DESCRIPTOR) {
return get8(pp, desc_end);
}
*pp = desc_end;
@ -2316,8 +2316,8 @@ static int parse_stream_identifier_desc(const uint8_t *p, const uint8_t *p_end)
static int is_pes_stream(int stream_type, uint32_t prog_reg_desc)
{
return !(stream_type == 0x13 ||
(stream_type == 0x86 && prog_reg_desc == AV_RL32("CUEI")) );
return !(stream_type == STREAM_TYPE_ISO_IEC_14496_SECTION ||
(stream_type == STREAM_TYPE_SCTE_DATA_SCTE_35 && prog_reg_desc == AV_RL32("CUEI")) );
}
static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
@ -2487,7 +2487,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
goto out;
st->id = pid;
st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
if (stream_type == 0x86 && prog_reg_desc == AV_RL32("CUEI")) {
if (stream_type == STREAM_TYPE_SCTE_DATA_SCTE_35 && prog_reg_desc == AV_RL32("CUEI")) {
mpegts_find_stream_type(st, stream_type, SCTE_types);
mpegts_open_section_filter(ts, pid, scte_data_cb, ts, 1);
}
@ -2523,7 +2523,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
break;
if (pes && prog_reg_desc == AV_RL32("HDMV") &&
stream_type == 0x83 && pes->sub_st) {
stream_type == STREAM_TYPE_BLURAY_AUDIO_TRUEHD && pes->sub_st) {
av_program_add_stream_index(ts->stream, h->id,
pes->sub_st->index);
pes->sub_st->codecpar->codec_tag = st->codecpar->codec_tag;
@ -2740,7 +2740,7 @@ static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
desc_tag, desc_len);
switch (desc_tag) {
case 0x48:
case SERVICE_DESCRIPTOR:
service_type = get8(&p, desc_end);
if (service_type < 0)
break;
@ -2806,7 +2806,7 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet, int64_t pos)
/* continuity check (currently not used) */
cc = (packet[3] & 0xf);
expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
cc_ok = pid == 0x1FFF || // null packet PID
cc_ok = pid == NULL_PID ||
is_discontinuity ||
tss->last_cc < 0 ||
expected_cc == cc;
@ -2835,7 +2835,7 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet, int64_t pos)
int64_t pcr_h;
int pcr_l;
if (parse_pcr(&pcr_h, &pcr_l, packet) == 0)
tss->last_pcr = pcr_h * 300 + pcr_l;
tss->last_pcr = pcr_h * SYSTEM_CLOCK_FREQUENCY_DIVISOR + pcr_l;
/* skip adaptation field */
p += p[0] + 1;
}
@ -2911,7 +2911,7 @@ static int mpegts_resync(AVFormatContext *s, int seekback, const uint8_t *curren
int64_t back = FFMIN(seekback, pos);
//Special case for files like 01c56b0dc1.ts
if (current_packet[0] == 0x80 && current_packet[12] == 0x47 && pos >= TS_PACKET_SIZE) {
if (current_packet[0] == 0x80 && current_packet[12] == SYNC_BYTE && pos >= TS_PACKET_SIZE) {
avio_seek(pb, 12 - TS_PACKET_SIZE, SEEK_CUR);
return 0;
}
@ -2922,7 +2922,7 @@ static int mpegts_resync(AVFormatContext *s, int seekback, const uint8_t *curren
c = avio_r8(pb);
if (avio_feof(pb))
return AVERROR_EOF;
if (c == 0x47) {
if (c == SYNC_BYTE) {
int new_packet_size, ret;
avio_seek(pb, -1, SEEK_CUR);
pos = avio_tell(pb);
@ -2956,7 +2956,7 @@ static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size,
if (len != TS_PACKET_SIZE)
return len < 0 ? len : AVERROR_EOF;
/* check packet sync byte */
if ((*data)[0] != 0x47) {
if ((*data)[0] != SYNC_BYTE) {
/* find a new packet start */
if (mpegts_resync(s, raw_packet_size, *data) < 0)
@ -3175,7 +3175,7 @@ static int mpegts_read_header(AVFormatContext *s)
parse_pcr(&pcr_h, &pcr_l, data) == 0) {
finished_reading_packet(s, ts->raw_packet_size);
pcr_pid = pid;
pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
pcrs[nb_pcrs] = pcr_h * SYSTEM_CLOCK_FREQUENCY_DIVISOR + pcr_l;
nb_pcrs++;
if (nb_pcrs >= 2) {
if (pcrs[1] - pcrs[0] > 0) {
@ -3240,14 +3240,14 @@ static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt)
if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
/* XXX: not precise enough */
ts->pcr_incr =
((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
((next_pcr_h - pcr_h) * SYSTEM_CLOCK_FREQUENCY_DIVISOR + (next_pcr_l - pcr_l)) /
(i + 1);
break;
}
}
avio_seek(s->pb, pos, SEEK_SET);
/* no next PCR found: we use previous increment */
ts->cur_pcr = pcr_h * 300 + pcr_l;
ts->cur_pcr = pcr_h * SYSTEM_CLOCK_FREQUENCY_DIVISOR + pcr_l;
}
pkt->pts = ts->cur_pcr;
pkt->duration = ts->pcr_incr;
@ -3325,7 +3325,7 @@ static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
return AV_NOPTS_VALUE;
if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
return AV_NOPTS_VALUE;
if (buf[0] != 0x47) {
if (buf[0] != SYNC_BYTE) {
if (mpegts_resync(s, TS_PACKET_SIZE, buf) < 0)
return AV_NOPTS_VALUE;
pos = avio_tell(s->pb);
@ -3416,7 +3416,7 @@ int avpriv_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
ts->stop_parse = 0;
if (len < TS_PACKET_SIZE)
return AVERROR_INVALIDDATA;
if (buf[0] != 0x47) {
if (buf[0] != SYNC_BYTE) {
buf++;
len--;
} else {

@ -33,6 +33,10 @@
#define USUAL_SECTION_SIZE 1024 /* except EIT which is limited to 4096 */
#define MAX_SECTION_SIZE 4096
#define SYNC_BYTE 0x47
#define STUFFING_BYTE 0xFF
#define SYSTEM_CLOCK_FREQUENCY_DIVISOR 300 /* convert 27 MHz to 90 kHz */
/* pids */
#define PAT_PID 0x0000 /* Program Association Table */
#define CAT_PID 0x0001 /* Conditional Access Table */
@ -116,6 +120,7 @@
/* TID from 0x80 to 0xFE are user defined */
/* TID 0xFF is reserved */
/* ISO/IEC 13818-1 Table 2-34 - Stream type assignments */
#define STREAM_TYPE_VIDEO_MPEG1 0x01
#define STREAM_TYPE_VIDEO_MPEG2 0x02
#define STREAM_TYPE_AUDIO_MPEG1 0x03
@ -125,8 +130,19 @@
#define STREAM_TYPE_AUDIO_AAC 0x0f
#define STREAM_TYPE_AUDIO_AAC_LATM 0x11
#define STREAM_TYPE_VIDEO_MPEG4 0x10
/** ISO/IEC 14496-1 (MPEG-4 Systems) SL-packetized stream or FlexMux stream
carried in PES packets */
#define STREAM_TYPE_ISO_IEC_14496_PES 0x12
/** ISO/IEC 14496-1 (MPEG-4 Systems) SL-packetized stream or FlexMux stream
carried in ISO_IEC_14496_section()s */
#define STREAM_TYPE_ISO_IEC_14496_SECTION 0x13
#define STREAM_TYPE_METADATA 0x15
#define STREAM_TYPE_VIDEO_H264 0x1b
/** ISO/IEC 14496-3 Audio, without using any additional transport syntax,
such as DST, ALS and SLS */
#define STREAM_TYPE_AUDIO_MPEG4 0x1c
#define STREAM_TYPE_VIDEO_MVC 0x20
#define STREAM_TYPE_VIDEO_JPEG2000 0x21
#define STREAM_TYPE_VIDEO_HEVC 0x24
#define STREAM_TYPE_VIDEO_VVC 0x33
#define STREAM_TYPE_VIDEO_CAVS 0x42
@ -135,10 +151,32 @@
#define STREAM_TYPE_VIDEO_VC1 0xea
#define STREAM_TYPE_VIDEO_DIRAC 0xd1
#define STREAM_TYPE_AUDIO_AC3 0x81
#define STREAM_TYPE_AUDIO_DTS 0x82
#define STREAM_TYPE_AUDIO_TRUEHD 0x83
#define STREAM_TYPE_AUDIO_EAC3 0x87
/* stream_type values [0x80, 0xff] are User Private */
#define STREAM_TYPE_BLURAY_AUDIO_PCM_BLURAY 0x80
#define STREAM_TYPE_BLURAY_AUDIO_AC3 0x81
#define STREAM_TYPE_BLURAY_AUDIO_DTS 0x82
#define STREAM_TYPE_BLURAY_AUDIO_TRUEHD 0x83
#define STREAM_TYPE_BLURAY_AUDIO_EAC3 0x84
#define STREAM_TYPE_BLURAY_AUDIO_DTS_HD 0x85
#define STREAM_TYPE_BLURAY_AUDIO_DTS_HD_MASTER 0x86
#define STREAM_TYPE_BLURAY_AUDIO_EAC3_SECONDARY 0xa1
#define STREAM_TYPE_BLURAY_AUDIO_DTS_EXPRESS_SECONDARY 0xa2
#define STREAM_TYPE_BLURAY_SUBTITLE_PGS 0x90
#define STREAM_TYPE_BLURAY_SUBTITLE_TEXT 0x92
#define STREAM_TYPE_SCTE_DATA_SCTE_35 0x86 /* ANSI/SCTE 35 */
#define STREAM_TYPE_ATSC_AUDIO_AC3 0x81 /* ATSC A/52 */
#define STREAM_TYPE_ATSC_AUDIO_EAC3 0x87 /* ATSC A/52 */
/* HTTP Live Streaming (HLS) Sample Encryption
see "MPEG-2 Stream Encryption Format for HTTP Live Streaming",
https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/HLS_Sample_Encryption/ */
#define STREAM_TYPE_HLS_SE_VIDEO_H264 0xdb
#define STREAM_TYPE_HLS_SE_AUDIO_AAC 0xcf
#define STREAM_TYPE_HLS_SE_AUDIO_AC3 0xc1
#define STREAM_TYPE_HLS_SE_AUDIO_EAC3 0xc2
/* ISO/IEC 13818-1 Table 2-22 */
#define STREAM_ID_PROGRAM_STREAM_MAP 0xbc
@ -164,9 +202,12 @@
#define FMC_DESCRIPTOR 0x1f
#define METADATA_DESCRIPTOR 0x26
#define METADATA_STD_DESCRIPTOR 0x27
/* descriptor_tag values [0x40, 0xff] are User Private */
/* DVB descriptor tag values [0x40, 0x7F] from
ETSI EN 300 468 Table 12: Possible locations of descriptors */
#define NETWORK_NAME_DESCRIPTOR 0x40
#define SERVICE_LIST_DESCRIPTOR 0x41
#define SERVICE_DESCRIPTOR 0x48
#define STREAM_IDENTIFIER_DESCRIPTOR 0x52
#define TELETEXT_DESCRIPTOR 0x56
@ -176,6 +217,16 @@
#define DTS_DESCRIPTOR 0x7b
#define EXTENSION_DESCRIPTOR 0x7f
/* DVB descriptor_tag_extension values from
ETSI EN 300 468 Table 109: Possible locations of extended descriptors */
#define SUPPLEMENTARY_AUDIO_DESCRIPTOR 0x06
/** see "Dolby Vision Streams Within the MPEG-2 Transport Stream Format"
https://professional.dolby.com/siteassets/content-creation/dolby-vision-for-content-creators/dolby-vision-bitstreams-in-mpeg-2-transport-stream-multiplex-v1.2.pdf */
#define DOVI_VIDEO_STREAM_DESCRIPTOR 0xb0
#define DATA_COMPONENT_DESCRIPTOR 0xfd /* ARIB STD-B10 */
typedef struct MpegTSContext MpegTSContext;
MpegTSContext *avpriv_mpegts_parse_open(AVFormatContext *s);

@ -163,7 +163,7 @@ static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
while (len > 0) {
first = buf == buf_ptr;
q = packet;
*q++ = 0x47;
*q++ = SYNC_BYTE;
b = s->pid >> 8;
if (first)
b |= 0x40;
@ -187,7 +187,7 @@ static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
/* add known padding data */
left = TS_PACKET_SIZE - (q - packet);
if (left > 0)
memset(q, 0xff, left);
memset(q, STUFFING_BYTE, left);
s->write_packet(s, packet);
@ -319,12 +319,12 @@ static int put_arib_caption_descriptor(AVFormatContext *s, uint8_t **q_ptr,
}
// stream_identifier_descriptor
*q++ = 0x52; // descriptor_tag
*q++ = STREAM_IDENTIFIER_DESCRIPTOR; // descriptor_tag
*q++ = 1; // descriptor_length
*q++ = stream_identifier; // component_tag: stream_identifier
// data_component_descriptor, defined in ARIB STD-B10, part 2, 6.2.20
*q++ = 0xFD; // descriptor_tag: ARIB data coding type descriptor
*q++ = DATA_COMPONENT_DESCRIPTOR; // descriptor_tag: ARIB data coding type descriptor
*q++ = 3; // descriptor_length
put16(&q, data_component_id); // data_component_id
// additional_arib_caption_info: defined in ARIB STD-B24, fascicle 1, Part 3, 9.6.1
@ -409,18 +409,18 @@ static int get_dvb_stream_type(AVFormatContext *s, AVStream *st)
case AV_CODEC_ID_AC3:
stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
? STREAM_TYPE_PRIVATE_DATA
: STREAM_TYPE_AUDIO_AC3;
: STREAM_TYPE_ATSC_AUDIO_AC3;
break;
case AV_CODEC_ID_EAC3:
stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
? STREAM_TYPE_PRIVATE_DATA
: STREAM_TYPE_AUDIO_EAC3;
: STREAM_TYPE_ATSC_AUDIO_EAC3;
break;
case AV_CODEC_ID_DTS:
stream_type = STREAM_TYPE_AUDIO_DTS;
stream_type = STREAM_TYPE_BLURAY_AUDIO_DTS; // should be STREAM_TYPE_PRIVATE_DATA (ETSI TS 101 154), needs a DTS_descriptor() (ETSI EN 300 468)
break;
case AV_CODEC_ID_TRUEHD:
stream_type = STREAM_TYPE_AUDIO_TRUEHD;
stream_type = STREAM_TYPE_BLURAY_AUDIO_TRUEHD; // should be STREAM_TYPE_PRIVATE_DATA (ETSI TS 101 154), needs a DTS-HD_descriptor() (ETSI EN 300 468)
break;
case AV_CODEC_ID_OPUS:
stream_type = STREAM_TYPE_PRIVATE_DATA;
@ -474,25 +474,27 @@ static int get_m2ts_stream_type(AVFormatContext *s, AVStream *st)
stream_type = STREAM_TYPE_VIDEO_HEVC;
break;
case AV_CODEC_ID_PCM_BLURAY:
stream_type = 0x80;
stream_type = STREAM_TYPE_BLURAY_AUDIO_PCM_BLURAY;
break;
case AV_CODEC_ID_AC3:
stream_type = 0x81;
stream_type = STREAM_TYPE_BLURAY_AUDIO_AC3;
break;
case AV_CODEC_ID_DTS:
stream_type = (st->codecpar->ch_layout.nb_channels > 6) ? 0x85 : 0x82;
stream_type = (st->codecpar->ch_layout.nb_channels > 6) ?
STREAM_TYPE_BLURAY_AUDIO_DTS_HD :
STREAM_TYPE_BLURAY_AUDIO_DTS;
break;
case AV_CODEC_ID_TRUEHD:
stream_type = 0x83;
stream_type = STREAM_TYPE_BLURAY_AUDIO_TRUEHD;
break;
case AV_CODEC_ID_EAC3:
stream_type = 0x84;
stream_type = STREAM_TYPE_BLURAY_AUDIO_EAC3;
break;
case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
stream_type = 0x90;
stream_type = STREAM_TYPE_BLURAY_SUBTITLE_PGS;
break;
case AV_CODEC_ID_HDMV_TEXT_SUBTITLE:
stream_type = 0x92;
stream_type = STREAM_TYPE_BLURAY_SUBTITLE_TEXT;
break;
default:
av_log_once(s, AV_LOG_WARNING, AV_LOG_DEBUG, &ts_st->data_st_warning,
@ -577,7 +579,7 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
if (codec_id == AV_CODEC_ID_AC3) {
DVBAC3Descriptor *dvb_ac3_desc = ts_st->dvb_ac3_desc;
*q++=0x6a; // AC3 descriptor see A038 DVB SI
*q++= AC3_DESCRIPTOR; // AC3 descriptor see A038 DVB SI
if (dvb_ac3_desc) {
int len = 1 +
!!(dvb_ac3_desc->component_type_flag) +
@ -598,7 +600,7 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
*q++=0; // omit all fields...
}
} else if (codec_id == AV_CODEC_ID_EAC3) {
*q++=0x7a; // EAC3 descriptor see A038 DVB SI
*q++= ENHANCED_AC3_DESCRIPTOR; // EAC3 descriptor see A038 DVB SI
*q++=1; // 1 byte, all flags sets to 0
*q++=0; // omit all fields...
}
@ -616,7 +618,7 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
put_registration_descriptor(&q, MKTAG('O', 'p', 'u', 's'));
*q++ = 0x7f; /* DVB extension descriptor */
*q++ = EXTENSION_DESCRIPTOR; /* DVB extension descriptor */
*q++ = 2;
*q++ = 0x80;
@ -724,7 +726,7 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
uint8_t *len_ptr;
int extradata_copied = 0;
*q++ = 0x59; /* subtitling_descriptor */
*q++ = SUBTITLING_DESCRIPTOR; /* subtitling_descriptor */
len_ptr = q++;
while (strlen(language) >= 3) {
@ -767,7 +769,7 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
int extradata_copied = 0;
/* The descriptor tag. teletext_descriptor */
*q++ = 0x56;
*q++ = TELETEXT_DESCRIPTOR;
len_ptr = q++;
while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
@ -865,7 +867,7 @@ static void mpegts_write_sdt(AVFormatContext *s)
free_ca_mode = 0;
/* write only one descriptor for the service name and provider */
*q++ = 0x48;
*q++ = SERVICE_DESCRIPTOR;
desc_len_ptr = q;
q++;
*q++ = ts->service_type;
@ -894,7 +896,7 @@ static void mpegts_write_nit(AVFormatContext *s)
put16(&q, 0xf000 | (ts->provider_name[0] + 2));
//network_name_descriptor
*q++ = 0x40;
*q++ = NETWORK_NAME_DESCRIPTOR;
putbuf(&q, ts->provider_name, ts->provider_name[0] + 1);
//transport_stream_loop_length
@ -909,7 +911,7 @@ static void mpegts_write_nit(AVFormatContext *s)
q += 2;
//service_list_descriptor
*q++ = 0x41;
*q++ = SERVICE_LIST_DESCRIPTOR;
*q++ = 3 * ts->nb_services;
for (int i = 0; i < ts->nb_services; i++) {
put16(&q, ts->services[i]->sid);
@ -1341,7 +1343,7 @@ static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt,
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
{
int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
int64_t pcr_low = pcr % SYSTEM_CLOCK_FREQUENCY_DIVISOR, pcr_high = pcr / SYSTEM_CLOCK_FREQUENCY_DIVISOR;
*buf++ = pcr_high >> 25;
*buf++ = pcr_high >> 17;
@ -1360,11 +1362,11 @@ static void mpegts_insert_null_packet(AVFormatContext *s)
uint8_t buf[TS_PACKET_SIZE];
q = buf;
*q++ = 0x47;
*q++ = 0x00 | 0x1f;
*q++ = 0xff;
*q++ = SYNC_BYTE;
*q++ = 0x00 | (NULL_PID >> 8);
*q++ = NULL_PID & 0xff;
*q++ = 0x10;
memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
memset(q, STUFFING_BYTE, TS_PACKET_SIZE - (q - buf)); /* data_bytes may be assigned any value */
write_packet(s, buf);
}
@ -1377,7 +1379,7 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
uint8_t buf[TS_PACKET_SIZE];
q = buf;
*q++ = 0x47;
*q++ = SYNC_BYTE;
*q++ = ts_st->pid >> 8;
*q++ = ts_st->pid;
*q++ = 0x20 | ts_st->cc; /* Adaptation only */
@ -1393,7 +1395,7 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
q += write_pcr_bits(q, get_pcr(ts));
/* stuffing bytes */
memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
memset(q, STUFFING_BYTE, TS_PACKET_SIZE - (q - buf));
write_packet(s, buf);
}
@ -1514,7 +1516,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
if (ts->mux_rate > 1)
pcr = get_pcr(ts);
else if (dts != AV_NOPTS_VALUE)
pcr = (dts - delay) * 300;
pcr = (dts - delay) * SYSTEM_CLOCK_FREQUENCY_DIVISOR;
retransmit_si_info(s, force_pat, force_sdt, force_nit, pcr);
force_pat = 0;
@ -1548,7 +1550,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
}
ts->next_pcr = next_pcr;
}
if (dts != AV_NOPTS_VALUE && (dts - pcr / 300) > delay) {
if (dts != AV_NOPTS_VALUE && (dts - pcr / SYSTEM_CLOCK_FREQUENCY_DIVISOR) > delay) {
/* pcr insert gets priority over null packet insert */
if (write_pcr)
mpegts_insert_pcr_only(s, st);
@ -1566,7 +1568,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
/* prepare packet header */
q = buf;
*q++ = 0x47;
*q++ = SYNC_BYTE;
val = ts_st->pid >> 8;
if (ts->m2ts_mode && st->codecpar->codec_id == AV_CODEC_ID_AC3)
val |= 0x20;
@ -1594,7 +1596,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
set_af_flag(buf, 0x10);
q = get_ts_payload_start(buf);
// add 11, pcr references the last byte of program clock reference base
if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
if (dts != AV_NOPTS_VALUE && dts < pcr / SYSTEM_CLOCK_FREQUENCY_DIVISOR)
av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
extend_af(buf, write_pcr_bits(q, pcr));
q = get_ts_payload_start(buf);
@ -1712,7 +1714,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
*q++ = 0x00;
}
if (is_dvb_teletext) {
memset(q, 0xff, pes_header_stuffing_bytes);
memset(q, STUFFING_BYTE, pes_header_stuffing_bytes);
q += pes_header_stuffing_bytes;
}
} else {
@ -1738,7 +1740,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
buf + 4 + afc_len,
header_len - (4 + afc_len));
buf[4] += stuffing_len;
memset(buf + 4 + afc_len, 0xff, stuffing_len);
memset(buf + 4 + afc_len, STUFFING_BYTE, stuffing_len);
} else {
/* add stuffing */
memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
@ -1746,7 +1748,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
buf[4] = stuffing_len - 1;
if (stuffing_len >= 2) {
buf[5] = 0x00;
memset(buf + 6, 0xff, stuffing_len - 2);
memset(buf + 6, STUFFING_BYTE, stuffing_len - 2);
}
}
}
@ -1880,7 +1882,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
stream_id = side_data[0];
if (!ts->first_dts_checked && dts != AV_NOPTS_VALUE) {
ts->first_pcr += dts * 300;
ts->first_pcr += dts * SYSTEM_CLOCK_FREQUENCY_DIVISOR;
ts->first_dts_checked = 1;
}

Loading…
Cancel
Save