mpegts.c
从这个文件最下面开始:
AVInputFormat mpegts_demuxer = {"mpegts",
NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
sizeof(MpegTSContext),
mpegts_probe,
mpegts_read_header,
mpegts_read_packet,
mpegts_read_close,
read_seek,
mpegts_get_pcr,
.flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
};
mpegts_probe:
这函数一看就知道是检测数据格式是不是mpegts格式的。
mpegts_read_header:
读数据头信息,比如在ts流当中的数据包大小,和ts流中的节目信息,sdt表,pmt表,video pid,audio pid等等,以便后面读数据时使用。
/* read the first 1024 bytes to get packet size */上面这几行代码是从数据流当中读了5 * 1024个字节来判断数据包的大小 raw_packet_size,一般这个值是188,当然如果这个ts流不是标准和dvb ts流的话,那当然会不一样的。
pos = url_ftell(pb);
len = get_buffer(pb, buf, sizeof(buf));
if (len != sizeof(buf))
goto fail;
ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
if (ts->raw_packet_size <= 0)
goto fail;
/* first do a scaning to get all the services */上面这几行代码是扫描节目信息,首先 mpegts_scan_sdt当中调用 mpegts_open_section_filter设置了一个SDT表的filter,SDT表当中会有节目的名子,提供商名子等等。接着在 mpegts_set_service当中又设置 mpegts_open_section_filter设置了一个PAT表filter,PAT表当中会存放节目的SID, PMT_PID,从而可以取到对应的PMT表,然后解板出VIDEO PID, AUDIO PID来, handle_packets就不用看了,上面设置了filter,这里紧跟着就得让filter工作起来了。
url_fseek(pb, pos, SEEK_SET);
mpegts_scan_sdt(ts);
mpegts_set_service(ts);
handle_packets(ts, s->probesize);
/* if could not find service, enable auto_guess */
ts->auto_guess = 1;
pat_cb:
……pmt_cb:
av_new_program(ts->stream, sid);
ts->stop_parse--;
mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
add_pat_entry(ts, sid);
add_pid_to_pmt(ts, sid, 0); //add pat pid to program
add_pid_to_pmt(ts, sid, pmt_pid);
……
……
/* now create ffmpeg stream */
switch(stream_type) {
case STREAM_TYPE_AUDIO_MPEG1:
case STREAM_TYPE_AUDIO_MPEG2:
case STREAM_TYPE_VIDEO_MPEG1:
case STREAM_TYPE_VIDEO_MPEG2:
case STREAM_TYPE_VIDEO_MPEG4:
case STREAM_TYPE_VIDEO_H264:
case STREAM_TYPE_VIDEO_VC1:
case STREAM_TYPE_VIDEO_DIRAC:
case STREAM_TYPE_AUDIO_AAC:
case STREAM_TYPE_AUDIO_AC3:
case STREAM_TYPE_AUDIO_DTS:
case STREAM_TYPE_AUDIO_HDMV_DTS:
case STREAM_TYPE_SUBTITLE_DVB:
if((stream_type == STREAM_TYPE_AUDIO_HDMV_DTS && !has_hdmv_descr)
|| (stream_type == STREAM_TYPE_VIDEO_DIRAC && !has_dirac_descr))
break;
if(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES){
pes= ts->pids[pid]->u.pes_filter.opaque;
st= pes->st;
}else{
if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
pes = add_pes_stream(ts, pid, pcr_pid, stream_type);
if (pes)
st = new_pes_av_stream(pes, 0);
}
add_pid_to_pmt(ts, h->id, pid);
if(st)
av_program_add_stream_index(ts->stream, h->id, st->index);
break;
default:
/* we ignore the other streams */
break;
}
……
得到每个video, audio的PID,然后就设置成pes filter,到这里基本上获取流的基本信息就已经结束了。
下面再来看使用最多的一个函数
mpegts_read_packet:/* return AVERROR_something if error or EOF. Return 0 if OK. */
static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size,
const uint8_t **data)
{
AVIOContext *pb = s->pb;
int len;
for (;;) {
//读188个字节出来,如果缓冲里有,读缓冲,如果没有,读文件。
len = ffio_read_indirect(pb, buf, TS_PACKET_SIZE, data);
if (len != TS_PACKET_SIZE)
return len < 0 ? len : AVERROR_EOF;
/* check packet sync byte */
if ((*data)[0] != 0x47) {
/* find a new packet start */
if (mpegts_resync(s, raw_packet_size, *data) < 0)
return AVERROR(EAGAIN);
else
continue;
} else {
break;
}
}
return 0;
}
handle_packets 这是我们刚刚跳过去的函数,此函数执行完,就得到一个PES包,保存在pkt中。
handle_packet 这函数是处理单个包的,所以后面没有sif (tss->type == MPEGTS_SECTION) {
……
write_section_data(s, tss,
p, p_end - p, 0);
……
} else {
// Note: The position here points actually behind the current packet.
tss->u.pes_filter.pes_cb(tss,
p, p_end - p, is_start, pos - ts->raw_packet_size);
}
处理每一个包,如果是section包,就调用 write_section_data,这个函数里面如果一个PAT, PMT, SDT表已经构成,则会调用刚刚看到的pat_cb, pmt_cb, sdt_cb,分析到这里,已经不用再管section包了,只看pes包,所以一般会调用 tss->u.pes_filter.pes_cb,这个函数指针到底是什么呢?在函数 add_pes_stream里面可以看到, mpegts_open_pes_filter函数的一个参数 mpegts_push_data就是这里的 tss->u.pes_filter.pes_cb,好,跟到这个函数里面瞧瞧。
mpegts_push_data:
……
while (buf_size > 0) {
switch(pes->state) {
case MPEGTS_HEADER:
case MPEGTS_PESHEADER_FILL:
case MPEGTS_PAYLOAD:
if (pes->data_index > 0 &&
pes->data_index + buf_size > pes->total_size) {
new_pes_packet(pes, ts->pkt);
pes->total_size = MAX_PES_PAYLOAD;
pes->buffer = av_buffer_alloc(pes->total_size +
AV_INPUT_BUFFER_PADDING_SIZE);
if (!pes->buffer)
return AVERROR(ENOMEM);
ts->stop_parse = 1;
}
return;
……
ts->stop_parse = 1意味着一个pes包构成了,所以上面的函数mpegts_read_packet就返回了,这样,一个pes包送上去了,再送到codec去解码,最后送去video或audio输出设置显示了。由些可以看到ts流和avi, mkv这些一样,都是一个容器,真真的数据都是包含在其中的一个一个的串流。
I帧判断
获取到一个PES包后,会进行帧判断。
问题
PES packet size mismatch
static int new_pes_packet(PESContext *pes, AVPacket *pkt)
{
...
if (pes->total_size != MAX_PES_PAYLOAD &&
pes->pes_header_size + pes->data_index != pes->total_size +
PES_START_SIZE) {
av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
pes->flags |= AV_PKT_FLAG_CORRUPT;
}
...
}
多久插入PCR
muxrate 默认是1。
if (ts->mux_rate > 1) {
service->pcr_packet_period = (int64_t)ts->mux_rate* ts->pcr_period /
(TS_PACKET_SIZE * 8 * 1000);
ts->sdt_packet_period = (int64_t)ts->mux_rate *SDT_RETRANS_TIME /
(TS_PACKET_SIZE * 8 * 1000);
ts->pat_packet_period = (int64_t)ts->mux_rate *PAT_RETRANS_TIME /
(TS_PACKET_SIZE * 8 * 1000);
if (ts->copyts < 1)
ts->first_pcr =av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
}else {
/* Arbitrary values, PAT/PMT will alsobe written on video key frames */
ts->sdt_packet_period = 200;
ts->pat_packet_period = 40;
if (pcr_st->codecpar->codec_type== AVMEDIA_TYPE_AUDIO) {
int frame_size =av_get_audio_frame_duration2(pcr_st->codecpar, 0);
if (!frame_size) {
av_log(s, AV_LOG_WARNING,"frame size not set\n");
service->pcr_packet_period =
pcr_st->codecpar->sample_rate/ (10 * 512);
} else {
service->pcr_packet_period =
pcr_st->codecpar->sample_rate / (10 * frame_size);
}
} else {
// max delta PCR 0.1s
// TODO: should be avg_frame_rate
service->pcr_packet_period =
ts_st->user_tb.den / (10 *ts_st->user_tb.num);
//ts_st->user_tb为(1,50),service->pcr_packet_period为5。
}
if (!service->pcr_packet_period)
service->pcr_packet_period = 1;
}
service->pcr_packet_count =service->pcr_packet_period;
是否要加PCR。
if (ts->mux_rate > 1 || is_start) // VBR pcr period is based onframes
ts_st->service->pcr_packet_count++;
if(ts_st->service->pcr_packet_count >=
ts_st->service->pcr_packet_period) {
ts_st->service->pcr_packet_count= 0;
write_pcr = 1;
}