av_gettime() 与 av_gettime_relative() 是什么含义?

av_gettime() 与 av_gettime_relative() 是什么含义?

读函数头的说明,云里雾里一堆,表示看不懂,干脆写程序测试一下:

$cat main.cpp
#include <stdio.h>
extern "C"
{
    #include <libavutil/time.h>
}
int main()
{
    int64_t time1=av_gettime();
    int64_t time2=av_gettime_relative();

    printf("%-24s:%ld\n","av_gettime",time1);
    printf("%-24s:%ld\n","av_gettime_relative",time2);

    return 0;
}

结果:
$ ./main
av_gettime              :1668047258787826
av_gettime_relative     :101219318785

可见gettime 比 relative 大得多.
我们知道av_gettime 从公元1970年1月1日0时0分0秒开始的微秒值,

而relative 从近期的某一点开始.
这近期的点是什么时间?
看这结果差1万多倍,
 

跟踪ffmpeg 代码
int64_t av_gettime(void)  //返回1970年1月1日0时0分0秒开始的微秒值,查资料得.
{
    struct timeval tv;
    gettimeofday(&tv, NULL); //时区填空即可
    return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
}
注意: timeval 结构,微秒级.
struct timeval
{
       time_t      tv_sec;     /* seconds */
       suseconds_t tv_usec;    /* microseconds */
};
int gettimeofday(struct timeval *tv, struct timezone *tz);
gives the number of seconds and microseconds since the Epoch
给出自大纪元以来的秒数和微秒数

int64_t av_gettime_relative(void) // 返回某一点开始后的微妙数
{
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
}

注意: timespec 是纳秒级, 与上面的timeval 结构不同
struct timespec
{
       time_t   tv_sec;        /* seconds */
       long     tv_nsec;       /* nanoseconds */
};

重启一次机器,
这次看清了也确认了. clock_gettime(CLOCK_MONOTONIC,&ts)是
开机后开始的微妙数. 如下表示开机到执行测试程序时58秒
执行结果:
 ./main
 av_gettime              :1668049206167258
 av_gettime_relative     :58553202

总结:
int64_t av_gettime(void)  //
从公元1970年1月1日0时0分0秒开始的微秒值
int64_t av_gettime_relative(void) // 返回开机后开始的微妙值

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值