ffmpeg源码跟踪之时间基转换,时间戳比较笔记
来源:互联网 发布:淘宝模特拍照24姿势 编辑:程序博客网 时间:2024/04/30 02:32
1、概述
经常在代码中我们转换时间基的时候用到av_rescale_q,av_rescale_q_rnd,av_compare_ts这些函数,也从来没有去好好看看代码,今天有时间好好看看代码。
2、代码+解释
av_rescale_q:
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){ return av_rescale_q_rnd(a, bq, cq, AV_ROUND_NEAR_INF);}
av_rescale_q_rnd:
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd){ int64_t b = bq.num * (int64_t)cq.den; int64_t c = cq.num * (int64_t)bq.den; return av_rescale_rnd(a, b, c, rnd);}
av_rescale_rnd:
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){ int64_t r = 0; av_assert2(c > 0); av_assert2(b >=0); av_assert2((unsigned)(rnd&~AV_ROUND_PASS_MINMAX)<=5 && (rnd&~AV_ROUND_PASS_MINMAX)!=4); if (c <= 0 || b < 0 || !((unsigned)(rnd&~AV_ROUND_PASS_MINMAX)<=5 && (rnd&~AV_ROUND_PASS_MINMAX)!=4)) return INT64_MIN; if (rnd & AV_ROUND_PASS_MINMAX) { if (a == INT64_MIN || a == INT64_MAX) return a; rnd -= AV_ROUND_PASS_MINMAX; } if (a < 0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd >> 1) & 1)); if (rnd == AV_ROUND_NEAR_INF) r = c / 2; else if (rnd & 1) r = c - 1; if (b <= INT_MAX && c <= INT_MAX) { if (a <= INT_MAX) return (a * b + r) / c; else return a / c * b + (a % c * b + r) / c; } else {#if 1 uint64_t a0 = a & 0xFFFFFFFF; uint64_t a1 = a >> 32; uint64_t b0 = b & 0xFFFFFFFF; uint64_t b1 = b >> 32; uint64_t t1 = a0 * b1 + a1 * b0; uint64_t t1a = t1 << 32; int i; a0 = a0 * b0 + t1a; a1 = a1 * b1 + (t1 >> 32) + (a0 < t1a); a0 += r; a1 += a0 < r; for (i = 63; i >= 0; i--) { a1 += a1 + ((a0 >> i) & 1); t1 += t1; if (c <= a1) { a1 -= c; t1++; } } return t1; }#else AVInteger ai; ai = av_mul_i(av_int2i(a), av_int2i(b)); ai = av_add_i(ai, av_int2i(r)); return av_i2int(av_div_i(ai, av_int2i(c))); }#endif}函数av_rescale_rnd其实就是返回(a * b)/c,只是加了一些舍入方案。
那么整个结构就清晰了,当我们调用av_rescale_q(pts, timebase1, timebase2)或者av_rescale_q_rnd(pts, timebase1, timebase2, XX)时
其实就是按照下面的公式计算了一下, 公式如下:
x = pts * (timebase1.num / timebase1.den )* (timebase2.den / timebase2.num);
这个x就是转换后的时间戳。
明白了时间基转换对理解av_compare_ts很有帮助,下面来看看av_compare_ts的代码:
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b){ int64_t a = tb_a.num * (int64_t)tb_b.den; int64_t b = tb_b.num * (int64_t)tb_a.den; if ((FFABS(ts_a)|a|FFABS(ts_b)|b) <= INT_MAX) return (ts_a*a > ts_b*b) - (ts_a*a < ts_b*b); if (av_rescale_rnd(ts_a, a, b, AV_ROUND_DOWN) < ts_b) return -1; if (av_rescale_rnd(ts_b, b, a, AV_ROUND_DOWN) < ts_a) return 1; return 0;}从代码中可以了总结出一句伪代码(假设2个时间基相等):
return ts_a == ts_b ? 0 : ts_a < ts_b ? -1 : 1
3 0
- ffmpeg源码跟踪之时间基转换,时间戳比较笔记
- ffmpeg源码跟踪笔记之avformat_open_input
- ffmpeg源码跟踪笔记之avformat_find_stream_info
- ffmpeg源码跟踪笔记之avcodec_open2
- ffmpeg源码跟踪笔记之av_read_frame
- ffmpeg源码跟踪笔记之avcodec_decode_video2
- FFMPEG之时间基
- ffmpeg 之 时间戳
- ffmpeg 之 时间戳
- ffmpeg源码跟踪笔记之av_write_frame 与 av_interleaved_write_frame
- 时间转换与比较
- FFmpeg 时间单位与转换
- iOS 时间戳 时间串 比较和转换
- 时间戳转换时间
- 时间戳转换时间
- ffmpeg 时间戳
- ffmpeg的时间戳
- FFmpeg中的时间戳
- 基于Bootstrap3制作响应式布局网站(五)
- linux 常用命令 dd/ln/cat/echo/grep/tar/wc/find/xargs/ssh/scp/strace/strings
- 取消的Costing新增
- 黑马程序员——java基础-IO(一)
- 对宏定义中出现的##运算符和#运算符的说明
- ffmpeg源码跟踪之时间基转换,时间戳比较笔记
- 九度OJ-题目1352:和为S的两个数字
- 字典树模板--静态 ----HDU 1251
- json
- NSCache 原理 (著名开源框架SDWebImage 就是使用的NSCache实现的缓存)
- 网站建设的发展前景
- 第二章:MongoDB简单的增删改查
- 学习linux
- 验证手机号码和电话号码的正则表达式