diff mbox series

[FFmpeg-devel,v3,4/7] avformat/seek: add ff_rescale_interval() function

Message ID 20220307221542.3265-4-pal@sandflow.com
State New
Headers show
Series [FFmpeg-devel,v3,1/7] avformat/imf: relocate static function imf_time_to_ts() | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished

Commit Message

Pierre-Anthony Lemieux March 7, 2022, 10:15 p.m. UTC
From: Pierre-Anthony Lemieux <pal@palemieux.com>

Refactors a function used by avformat/concat and avformat/imf.

---
 libavformat/avio_internal.h | 21 +++++++++++++++++++++
 libavformat/seek.c          | 10 ++++++++++
 2 files changed, 31 insertions(+)
diff mbox series

Patch

diff --git a/libavformat/avio_internal.h b/libavformat/avio_internal.h
index 1f5e3d474b..ef2ec7864f 100644
--- a/libavformat/avio_internal.h
+++ b/libavformat/avio_internal.h
@@ -23,6 +23,7 @@ 
 #include "url.h"
 
 #include "libavutil/log.h"
+#include "libavutil/rational.h"
 
 extern const AVClass ff_avio_class;
 
@@ -281,4 +282,24 @@  int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp);
 int64_t ff_read_string_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp,
                                            int64_t max_len);
 
+/**
+ * Rescales a timestamp and the endpoints of an interval to which the temstamp
+ * belongs, from a timebase `tb_in` to a timebase `tb_out`.
+ *
+ * The upper (lower) bound of the output interval is rounded up (down) such that
+ * the output interval always falls within the intput interval. The timestamp is
+ * rounded to the nearest integer and halfway cases away from zero, and can
+ * therefore fall outside of the output interval.
+ * 
+ * Useful to simplify the rescaling of the arguments of AVInputFormat::read_seek2()
+ *
+ * @param[in] tb_in      Timebase of the input `min_ts`, `ts` and `max_ts`
+ * @param[in] tb_out     Timebase of the ouput `min_ts`, `ts` and `max_ts`
+ * @param[in,out] min_ts Lower bound of the interval
+ * @param[in,out] ts     Timestamp
+ * @param[in,out] max_ts Upper bound of the interval
+ */
+void ff_rescale_interval(AVRational tb_in, AVRational tb_out,
+                         int64_t *min_ts, int64_t *ts, int64_t *max_ts);
+
 #endif /* AVFORMAT_AVIO_INTERNAL_H */
diff --git a/libavformat/seek.c b/libavformat/seek.c
index 405ca316b3..890aea7f8a 100644
--- a/libavformat/seek.c
+++ b/libavformat/seek.c
@@ -751,3 +751,13 @@  int avformat_flush(AVFormatContext *s)
     ff_read_frame_flush(s);
     return 0;
 }
+
+void ff_rescale_interval(AVRational tb_in, AVRational tb_out,
+                         int64_t *min_ts, int64_t *ts, int64_t *max_ts)
+{
+    *ts     = av_rescale_q    (*    ts, tb_in, tb_out);
+    *min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
+                               AV_ROUND_UP   | AV_ROUND_PASS_MINMAX);
+    *max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
+                               AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
+}