原 ts, mp4文件快进快退(seek)原理

https://blog.csdn.net/lrzkd/article/details/78353561


最近用potplayer播放一些ts文件,seek(快进快退)发现会有卡顿问题,但是同一个文件用mp4转封装之后seek就很快很流畅了。所以抽空研究了ffplay 对mp4文件和ts文件的 seek 逻辑,结论:对mp4 和t s的seek 逻辑完全不一样。

 

最重要的区别在于mp4 可以seek到指定的时间戳 ts 是 seek到文件的某个position,而不能直接seek到指定的时间点,下面结合ffplay的代码看一下实际的seek逻辑。

 

在ffplay.c中的event_loop函数中包含了seek的相关代码:

[cpp]  view plain  copy
  1. if (seek_by_bytes || cur_stream->ic->duration <= 0) { // ts只能 seek by bytes  
  2.     uint64_t size =  avio_size(cur_stream->ic->pb);  
  3.     stream_seek(cur_stream,size*x/cur_stream->width, 0, 1); //根据鼠标在播放窗口中点击的位置x,计算seek到的文件的具体位置  
  4. else { // mp4 可以seek by timestamp  
  5.     int64_t ts;  
  6.     int ns, hh, mm, ss;  
  7.     int tns, thh, tmm, tss;  
  8.     tns  = cur_stream->ic->duration / 1000000LL;  // 单位秒  
  9.     thh  = tns / 3600;  
  10.     tmm  = (tns % 3600) / 60;  
  11.     tss  = (tns % 60);  
  12.     frac = x /cur_stream->width;  
  13.     ns   = frac * tns;  
  14.     hh   = ns / 3600;  
  15.     mm   = (ns % 3600) / 60;  
  16.     ss   = (ns % 60);  
  17.     av_log(NULL, AV_LOG_INFO,  
  18.            "Seek to%2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d)       \n", frac*100,  
  19.             hh, mm, ss, thh,tmm, tss);  
  20.     ts = frac *cur_stream->ic->duration;  // 单位微秒  
  21.     if(cur_stream->ic->start_time != AV_NOPTS_VALUE)  
  22.         ts +=cur_stream->ic->start_time;  
  23.     stream_seek(cur_stream,ts, 0, 0);  
  24. }  

对于ts,具体seek操作调用函数关系为 avformat_seek_file()=> av_seek_frame() => seek_frame_internal() => seek_frame_byte()

 

对于mp4,具体seek操作调用函数关系为 avformat_seek_file()=> av_seek_frame() => seek_frame_internal() =>mov_read_seek()


所以 ts 和 mp4 seek的差别就在最后一个环节, seek_frame_byte()  与 mov_read_seek() 的差别。


下面贴出这几个函数的相关代码,稍加注释:

[cpp]  view plain  copy
  1. int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,  
  2.                        int64_t ts, int64_t max_ts, int flags)  
  3. {  
  4.     if (min_ts > ts || max_ts < ts)  
  5.         return -1;  
  6.     if (stream_index < -1 || stream_index >= (int)s->nb_streams)  
  7.         return AVERROR(EINVAL);  
  8.   
  9.   ……  
  10.   
  11.     // Fall back on old API if new is not implemented but old is.  
  12.     // Note the old API has somewhat different semantics.  
  13.     if (s->iformat->read_seek || 1) {  
  14.         int dir = (ts - (uint64_t)min_ts > (uint64_t)max_ts - ts ? AVSEEK_FLAG_BACKWARD : 0);  
  15.         int ret = av_seek_frame(s, stream_index, ts, flags | dir);  
  16.         if (ret<0 && ts != min_ts && max_ts != ts) {  
  17.             ret = av_seek_frame(s, stream_index, dir ? max_ts : min_ts, flags | dir);  
  18.             if (ret >= 0)  
  19.                 ret = av_seek_frame(s, stream_index, ts, flags | (dir^AVSEEK_FLAG_BACKWARD));  
  20.         }  
  21.         return ret;  
  22.     }  
  23.   
  24.     // try some generic seek like seek_frame_generic() but with new ts semantics  
  25.     return -1; //unreachable  
  26. }  


av_seek_frame函数

[cpp]  view plain  copy
  1. int av_seek_frame(AVFormatContext *s, intstream_index,  
  2.                   int64_t timestamp, int flags)  
  3. {  
  4.    int ret;  
  5.    
  6.     ……  
  7.    
  8.    ret = seek_frame_internal(s, stream_index, timestamp, flags);  
  9.    
  10.    if (ret >= 0)  
  11.        ret = avformat_queue_attached_pictures(s);  
  12.    
  13.    return ret;  
  14. }  

seek_frame_internal函数

[cpp]  view plain  copy
  1. static intseek_frame_internal(AVFormatContext *s, int stream_index,  
  2.                                int64_ttimestamp, int flags)  
  3. {  
  4.    int ret;  
  5.    AVStream *st;  
  6.    
  7.    if (flags & AVSEEK_FLAG_BYTE) { // ts文件走这个if条件的逻辑  
  8.        if (s->iformat->flags &AVFMT_NO_BYTE_SEEK)  
  9.            return -1;  
  10.        ff_read_frame_flush(s);  
  11.        return seek_frame_byte(s, stream_index, timestamp, flags); //根据position来seek  
  12.     }  
  13.    // mp4文件走下面的逻辑  
  14.    if (stream_index < 0) {  
  15.        stream_index = av_find_default_stream_index(s);  
  16.        if (stream_index < 0)  
  17.            return -1;  
  18.    
  19.        st = s->streams[stream_index];  
  20.        /* timestamp for default must be expressed in AV_TIME_BASE units */  
  21.        timestamp = av_rescale(timestamp, st->time_base.den,  
  22.                                AV_TIME_BASE *(int64_t) st->time_base.num);  
  23.     }  
  24.    
  25.    /* first, we try the format specific seek */  
  26.    if (s->iformat->read_seek) {  
  27.        ff_read_frame_flush(s);  
  28.        ret = s->iformat->read_seek(s, stream_index, timestamp, flags);  
  29.     }else  
  30.        ret = -1;  
  31.    if (ret >= 0)  
  32.        return 0;  
  33.    
  34.    ......  
  35. }  

seek_frame_byte函数:直接seek到文件指定的position,ts 的 seek 调用此函数。

[cpp]  view plain  copy
  1. static int seek_frame_byte(AVFormatContext*s, int stream_index,  
  2.                            int64_t pos, intflags)  
  3. {  
  4.    int64_t pos_min, pos_max;  
  5.    
  6.    pos_min = s->internal->data_offset;  
  7.    pos_max = avio_size(s->pb) - 1;  
  8.    
  9.    if (pos < pos_min)  
  10.        pos = pos_min;  
  11.    else if (pos > pos_max)  
  12.        pos = pos_max;  
  13.    
  14.    avio_seek(s->pb, pos, SEEK_SET); //直接将文件指针指向pos位置  
  15.    
  16.    s->io_repositioned = 1;  
  17.    
  18.    return 0;  
  19. }  


所以 ts  seek 逻辑是:给定一个文件位置,直接将文件指针指向该位置。接下来调用 read_packet() 读取一个 ts 包(188字节)时,由于之前进行了seek操作,文件指针很可能没有指到一个 ts packet 的包头位置(包头以0x47 byte打头的),这时候需要调用 mpegts_resync() 进行重新同步找到包头,然后再重新读取一个完整 ts packet。

mp4 的 seek 操作逻辑是:给定一个 seek的目标时间戳(timestamp),根据mp4 里每个包的索引信息,找到时间戳对应的包就可以了。根据下面的 mp4 的文件组织结构,利用Sample Table,可以快速找到任意给定时间戳的 video audio 数据包。


mp4 的文件组织结构如下图



总结:

1、对 mp4 文件来说,由于有索引表,可以快速找到某个时间戳所对应的数据,所以 seek 操作可以快速完成。

 

2、ts 文件没有时间戳和数据包位置的对应关系,所以对播放器来说,给定seek 的时间戳ts_seek,首先应该根据文件的码率估算一个位置pos,然后获取该位置的数据包的时间戳ts_actual,如果ts_actual < ts_seek ,则需要继续往后读取数据包;如果ts_actual> ts_seek,则需要往前读取数据包,直到读到 ts_seek 对应的数据包。所以 ts 文件的操作可能更加耗时; 如果 ts 包含的是 CBR 码流,则 ts_actual 与 ts_seek 一般差别不大, seek 相对较快; 如果 ts 包含的 VBR 码流, 则 ts_actual 与 ts_seek 可能相差甚远, 则 seek 相对较慢。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值