* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-//#define USE_SYNCPOINT_SEARCH
-
#include "libavutil/crc.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/log.h"
static const AVOption options[] = {
{"compute_pcr", "Compute exact PCR for each transport stream packet.", offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_INT,
- {.dbl = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
+ {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
{ NULL },
};
typedef struct {
uint32_t stream_type;
enum AVMediaType codec_type;
- enum CodecID codec_id;
+ enum AVCodecID codec_id;
} StreamType;
static const StreamType ISO_types[] = {
- { 0x01, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
- { 0x02, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
- { 0x03, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
- { 0x04, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
- { 0x0f, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC },
- { 0x10, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG4 },
- { 0x11, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC_LATM }, /* LATM syntax */
- { 0x1b, AVMEDIA_TYPE_VIDEO, CODEC_ID_H264 },
- { 0xd1, AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
- { 0xea, AVMEDIA_TYPE_VIDEO, CODEC_ID_VC1 },
+ { 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 },
+ { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
+ { 0x1b, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
+ { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
+ { 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
{ 0 },
};
static const StreamType HDMV_types[] = {
- { 0x80, AVMEDIA_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
- { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
- { 0x82, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
- { 0x83, AVMEDIA_TYPE_AUDIO, CODEC_ID_TRUEHD },
- { 0x84, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
- { 0x90, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
+ { 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*/
+ { 0x90, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE },
{ 0 },
};
/* ATSC ? */
static const StreamType MISC_types[] = {
- { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
- { 0x8a, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
+ { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
+ { 0x8a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
{ 0 },
};
static const StreamType REGD_types[] = {
- { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
- { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
- { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, CODEC_ID_S302M },
+ { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
+ { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
+ { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M },
+ { MKTAG('D','T','S','1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
+ { MKTAG('D','T','S','2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
+ { MKTAG('D','T','S','3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
+ { MKTAG('V','C','-','1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
{ 0 },
};
/* descriptor present */
static const StreamType DESC_types[] = {
- { 0x6a, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 }, /* AC-3 descriptor */
- { 0x7a, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
- { 0x7b, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
- { 0x56, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
- { 0x59, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
+ { 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 */
{ 0 },
};
avpriv_set_pts_info(st, 33, 1, 90000);
st->priv_data = pes;
st->codec->codec_type = AVMEDIA_TYPE_DATA;
- st->codec->codec_id = CODEC_ID_NONE;
+ st->codec->codec_id = AV_CODEC_ID_NONE;
st->need_parsing = AVSTREAM_PARSE_FULL;
pes->st = st;
pes->stream_type = stream_type;
mpegts_find_stream_type(st, pes->stream_type, ISO_types);
if (prog_reg_desc == AV_RL32("HDMV") &&
- st->codec->codec_id == CODEC_ID_NONE) {
+ st->codec->codec_id == AV_CODEC_ID_NONE) {
mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
if (pes->stream_type == 0x83) {
// HDMV TrueHD streams also contain an AC3 coded version of the
avpriv_set_pts_info(sub_st, 33, 1, 90000);
sub_st->priv_data = sub_pes;
sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
- sub_st->codec->codec_id = CODEC_ID_AC3;
+ sub_st->codec->codec_id = AV_CODEC_ID_AC3;
sub_st->need_parsing = AVSTREAM_PARSE_FULL;
sub_pes->sub_st = pes->sub_st = sub_st;
}
}
- if (st->codec->codec_id == CODEC_ID_NONE)
+ if (st->codec->codec_id == AV_CODEC_ID_NONE)
mpegts_find_stream_type(st, pes->stream_type, MISC_types);
return 0;
code = pes->header[3] | 0x100;
av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
- if ((pes->st && pes->st->discard == AVDISCARD_ALL) ||
+ if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
+ (!pes->sub_st || pes->sub_st->discard == AVDISCARD_ALL)) ||
code == 0x1be) /* padding_stream */
goto skip;
code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
pes->state = MPEGTS_PESHEADER;
- if (pes->st->codec->codec_id == CODEC_ID_NONE) {
+ if (pes->st->codec->codec_id == AV_CODEC_ID_NONE) {
av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
pes->pid, pes->stream_type);
- pes->st->codec->codec_id = CODEC_ID_PROBE;
+ pes->st->codec->codec_id = AV_CODEC_ID_PROBE;
}
} else {
pes->state = MPEGTS_PAYLOAD;
ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
ff_mp4_read_dec_config_descr(s, st, &pb);
- if (st->codec->codec_id == CODEC_ID_AAC &&
+ if (st->codec->codec_id == AV_CODEC_ID_AAC &&
st->codec->extradata_size > 0)
st->need_parsing = 0;
- if (st->codec->codec_id == CODEC_ID_H264 &&
+ if (st->codec->codec_id == AV_CODEC_ID_H264 &&
st->codec->extradata_size > 0)
st->need_parsing = 0;
- if (st->codec->codec_id <= CODEC_ID_NONE) {
- } else if (st->codec->codec_id < CODEC_ID_FIRST_AUDIO) {
+ if (st->codec->codec_id <= AV_CODEC_ID_NONE) {
+ } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_AUDIO) {
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
- } else if (st->codec->codec_id < CODEC_ID_FIRST_SUBTITLE) {
+ } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_SUBTITLE) {
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
- } else if (st->codec->codec_id < CODEC_ID_FIRST_UNKNOWN) {
+ } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_UNKNOWN) {
st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
}
}
av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
- if (st->codec->codec_id == CODEC_ID_NONE &&
+ if (st->codec->codec_id == AV_CODEC_ID_NONE &&
stream_type == STREAM_TYPE_PRIVATE_DATA)
mpegts_find_stream_type(st, desc_tag, DESC_types);
ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
ff_mp4_read_dec_config_descr(fc, st, &pb);
- if (st->codec->codec_id == CODEC_ID_AAC &&
+ if (st->codec->codec_id == AV_CODEC_ID_AAC &&
st->codec->extradata_size > 0)
st->need_parsing = 0;
- if (st->codec->codec_id == CODEC_ID_MPEG4SYSTEMS)
+ if (st->codec->codec_id == AV_CODEC_ID_MPEG4SYSTEMS)
mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
}
break;
case 0x1F: /* FMC descriptor */
get16(pp, desc_end);
- if (mp4_descr_count > 0 && st->codec->codec_id == CODEC_ID_AAC_LATM &&
+ if (mp4_descr_count > 0 && st->codec->codec_id == AV_CODEC_ID_AAC_LATM &&
mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
AVIOContext pb;
ffio_init_context(&pb, mp4_descr->dec_config_descr,
mp4_descr->dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
ff_mp4_read_dec_config_descr(fc, st, &pb);
- if (st->codec->codec_id == CODEC_ID_AAC &&
+ if (st->codec->codec_id == AV_CODEC_ID_AAC &&
st->codec->extradata_size > 0)
st->need_parsing = 0;
}
case 0x05: /* registration descriptor */
st->codec->codec_tag = bytestream_get_le32(pp);
av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
- if (st->codec->codec_id == CODEC_ID_NONE)
+ if (st->codec->codec_id == AV_CODEC_ID_NONE)
mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
break;
default:
return;
clear_program(ts, h->id);
- pcr_pid = get16(&p, p_end) & 0x1fff;
+ pcr_pid = get16(&p, p_end);
if (pcr_pid < 0)
return;
+ pcr_pid &= 0x1fff;
add_pid_to_pmt(ts, h->id, pcr_pid);
av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
- program_info_length = get16(&p, p_end) & 0xfff;
+ program_info_length = get16(&p, p_end);
if (program_info_length < 0)
return;
+ program_info_length &= 0xfff;
while(program_info_length >= 2) {
uint8_t tag, len;
tag = get8(&p, p_end);
stream_type = get8(&p, p_end);
if (stream_type < 0)
break;
- pid = get16(&p, p_end) & 0x1fff;
+ pid = get16(&p, p_end);
if (pid < 0)
break;
+ pid &= 0x1fff;
/* now create stream */
if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
ff_program_add_stream_index(ts->stream, h->id, st->index);
- desc_list_len = get16(&p, p_end) & 0xfff;
+ desc_list_len = get16(&p, p_end);
if (desc_list_len < 0)
break;
+ desc_list_len &= 0xfff;
desc_list_end = p + desc_list_len;
if (desc_list_end > p_end)
break;
sid = get16(&p, p_end);
if (sid < 0)
break;
- pmt_pid = get16(&p, p_end) & 0x1fff;
+ pmt_pid = get16(&p, p_end);
if (pmt_pid < 0)
break;
+ pmt_pid &= 0x1fff;
av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
val = get8(&p, p_end);
if (val < 0)
break;
- desc_list_len = get16(&p, p_end) & 0xfff;
+ desc_list_len = get16(&p, p_end);
if (desc_list_len < 0)
break;
+ desc_list_len &= 0xfff;
desc_list_end = p + desc_list_len;
if (desc_list_end > p_end)
break;
static int mpegts_probe(AVProbeData *p)
{
-#if 1
const int size= p->buf_size;
int score, fec_score, dvhs_score;
int check_count= size / TS_FEC_PACKET_SIZE;
else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
else return -1;
-#else
- /* only use the extension for safer guess */
- if (av_match_ext(p->filename, "ts"))
- return AVPROBE_SCORE_MAX;
- else
- return 0;
-#endif
}
/* return the 90kHz PCR and the extension for the 27MHz PCR. return
/* normal demux */
/* first do a scan to get all the services */
- if (pb->seekable && avio_seek(pb, pos, SEEK_SET) < 0)
+ if (avio_seek(pb, pos, SEEK_SET) < 0 && pb->seekable)
av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
goto fail;
avpriv_set_pts_info(st, 60, 1, 27000000);
st->codec->codec_type = AVMEDIA_TYPE_DATA;
- st->codec->codec_id = CODEC_ID_MPEG2TS;
+ st->codec->codec_id = AV_CODEC_ID_MPEG2TS;
/* we iterate until we find two PCRs to estimate the bitrate */
pcr_pid = -1;
MpegTSContext *ts = s->priv_data;
int ret, i;
+ pkt->size = -1;
ts->pkt = pkt;
ret = handle_packets(ts, 0);
if (ret < 0) {
}
}
+ if (!ret && pkt->size < 0)
+ ret = AVERROR(EINTR);
return ret;
}
return timestamp;
}
-#ifdef USE_SYNCPOINT_SEARCH
-
-static int read_seek2(AVFormatContext *s,
- int stream_index,
- int64_t min_ts,
- int64_t target_ts,
- int64_t max_ts,
- int flags)
-{
- int64_t pos;
-
- int64_t ts_ret, ts_adj;
- int stream_index_gen_search;
- AVStream *st;
- AVParserState *backup;
-
- backup = ff_store_parser_state(s);
-
- // detect direction of seeking for search purposes
- flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
- AVSEEK_FLAG_BACKWARD : 0;
-
- if (flags & AVSEEK_FLAG_BYTE) {
- // use position directly, we will search starting from it
- pos = target_ts;
- } else {
- // search for some position with good timestamp match
- if (stream_index < 0) {
- stream_index_gen_search = av_find_default_stream_index(s);
- if (stream_index_gen_search < 0) {
- ff_restore_parser_state(s, backup);
- return -1;
- }
-
- st = s->streams[stream_index_gen_search];
- // timestamp for default must be expressed in AV_TIME_BASE units
- ts_adj = av_rescale(target_ts,
- st->time_base.den,
- AV_TIME_BASE * (int64_t)st->time_base.num);
- } else {
- ts_adj = target_ts;
- stream_index_gen_search = stream_index;
- }
- pos = ff_gen_search(s, stream_index_gen_search, ts_adj,
- 0, INT64_MAX, -1,
- AV_NOPTS_VALUE,
- AV_NOPTS_VALUE,
- flags, &ts_ret, mpegts_get_pcr);
- if (pos < 0) {
- ff_restore_parser_state(s, backup);
- return -1;
- }
- }
-
- // search for actual matching keyframe/starting position for all streams
- if (ff_gen_syncpoint_search(s, stream_index, pos,
- min_ts, target_ts, max_ts,
- flags) < 0) {
- ff_restore_parser_state(s, backup);
- return -1;
- }
-
- ff_free_parser_state(s, backup);
- return 0;
-}
-
-static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
-{
- int ret;
- if (flags & AVSEEK_FLAG_BACKWARD) {
- flags &= ~AVSEEK_FLAG_BACKWARD;
- ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
- if (ret < 0)
- // for compatibility reasons, seek to the best-fitting timestamp
- ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
- } else {
- ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
- if (ret < 0)
- // for compatibility reasons, seek to the best-fitting timestamp
- ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
- }
- return ret;
-}
-
-#else
-
static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
MpegTSContext *ts = s->priv_data;
uint8_t buf[TS_PACKET_SIZE];
return 0;
}
-#endif
-
/**************************************************************/
/* parsing functions - called from other demuxers such as RTP */
AVInputFormat ff_mpegts_demuxer = {
.name = "mpegts",
- .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
.priv_data_size = sizeof(MpegTSContext),
.read_probe = mpegts_probe,
.read_header = mpegts_read_header,
.read_close = mpegts_read_close,
.read_seek = read_seek,
.read_timestamp = mpegts_get_pcr,
- .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
-#ifdef USE_SYNCPOINT_SEARCH
- .read_seek2 = read_seek2,
-#endif
+ .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
};
AVInputFormat ff_mpegtsraw_demuxer = {
.name = "mpegtsraw",
- .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
+ .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
.priv_data_size = sizeof(MpegTSContext),
.read_header = mpegts_read_header,
.read_packet = mpegts_raw_read_packet,
.read_close = mpegts_read_close,
.read_seek = read_seek,
.read_timestamp = mpegts_get_pcr,
- .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
-#ifdef USE_SYNCPOINT_SEARCH
- .read_seek2 = read_seek2,
-#endif
- .priv_class = &mpegtsraw_class,
+ .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
+ .priv_class = &mpegtsraw_class,
};