time_base 理解

作者: 爱玩保龄球 | 来源:发表于2020-03-30 18:33 被阅读0次
    • time_base

    • AVStream的time_base的单位是秒。每种格式的time_base的值不一样,根据采样来计算,比如mpeg的pts、dts都是以90kHz来采样的,所以采样间隔就是1/900000秒。

    • AVCodecContext的time_base单位同样为秒,不过精度没有AVStream->time_base高,大小为1/framerate。

    • AVPacket下的pts和dts以AVStream->time_base为单位(数值比较大),时间间隔就是AVStream->time_base。

    • AVFrame里面的pkt_pts和pkt_dts是拷贝自AVPacket,同样以AVStream->time_base为单位;而pts是为输出(显示)准备的,以AVCodecContex->time_base为单位。

    输入流InputStream下的pts和dts以AV_TIME_BASE为单位(微秒),至于为什么要转化为微秒,可能是为了避免使用浮点数。

    输出流OutputStream涉及音视频同步,结构和InputStream不同,暂时只作记录,不分析。

    • 各个time_base之前的转换
      ffmpeg提供av_rescale_q函数用于time_base之间转换,av_rescale_q(a,b,c)作用相当于执行a*b/c,通过设置b,c的值,可以很方便的实现time_base之间转换。
    ** 例如 **
    
    InputStream(AV_TIME_BASE)到AVPacket(AVStream->time_base)
    static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
    {
            pkt->dts  = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ist->st->time_base);
    }
    AVPacket(AVStream->time_base)到InputStream(AV_TIME_BASE)
    static int process_input_packet(InputStream *ist, const AVPacket *pkt) 
    {    
          if (pkt->dts != AV_NOPTS_VALUE) 
          {       
            ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q); 
          }
    }
    

    三、其他
    AVFrame->pts和AVPacket->pts、AVPacket->dts的值,在解码/编码后,会经历短暂的time_base不匹配的情况:

    解码后,decoded_frame->pts的值使用AVStream->time_base为单位,后在AVFilter里面转换成以AVCodecContext->time_base为单位。
    编码后,pkt.pts、pkt.dts使用AVCodecContext->time_base为单位,后通过调用"av_packet_rescale_ts"转换为AVStream->time_base为单位

    相关文章

      网友评论

        本文标题:time_base 理解

        本文链接:https://www.haomeiwen.com/subject/yqxbuhtx.html