diff mbox series

[FFmpeg-devel,v2,4/7] avutil/mathematics: add av_rescale_interval() function

Message ID 20220221060230.6665-4-pal@sandflow.com
State New
Headers show
Series [FFmpeg-devel,v2,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 Feb. 21, 2022, 6:02 a.m. UTC
From: Pierre-Anthony Lemieux <pal@palemieux.com>

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

---
 libavutil/mathematics.c | 10 ++++++++++
 libavutil/mathematics.h | 21 +++++++++++++++++++++
 2 files changed, 31 insertions(+)

Comments

Andreas Rheinhardt March 4, 2022, 8:13 p.m. UTC | #1
pal@sandflow.com:
> From: Pierre-Anthony Lemieux <pal@palemieux.com>
> 
> Refactors a function used by avformat/concat and avformat/imf.
> 
> ---
>  libavutil/mathematics.c | 10 ++++++++++
>  libavutil/mathematics.h | 21 +++++++++++++++++++++
>  2 files changed, 31 insertions(+)
> 
> diff --git a/libavutil/mathematics.c b/libavutil/mathematics.c
> index f4e541fa24..2c7f57b950 100644
> --- a/libavutil/mathematics.c
> +++ b/libavutil/mathematics.c
> @@ -212,3 +212,13 @@ int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t i
>          return av_sat_add64(av_rescale_q(old + 1, inc_tb, ts_tb), ts - old_ts);
>      }
>  }
> +
> +void av_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);
> +}
> diff --git a/libavutil/mathematics.h b/libavutil/mathematics.h
> index 64d4137a60..eb8a3f4002 100644
> --- a/libavutil/mathematics.h
> +++ b/libavutil/mathematics.h
> @@ -161,6 +161,27 @@ int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const;
>  int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq,
>                           enum AVRounding rnd) av_const;
>  
> +/**
> + * 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 av_rescale_interval(AVRational tb_in, AVRational tb_out,
> +                         int64_t *min_ts, int64_t *ts, int64_t *max_ts);
> +
> +
>  /**
>   * Compare two timestamps each in its own time base.
>   *

I don't see why this function should be public at all. It seems very
specific to a usecase in lavf.

- Andreas
Pierre-Anthony Lemieux March 4, 2022, 8:40 p.m. UTC | #2
On Fri, Mar 4, 2022 at 12:13 PM Andreas Rheinhardt
<andreas.rheinhardt@outlook.com> wrote:
>
> pal@sandflow.com:
> > From: Pierre-Anthony Lemieux <pal@palemieux.com>
> >
> > Refactors a function used by avformat/concat and avformat/imf.
> >
> > ---
> >  libavutil/mathematics.c | 10 ++++++++++
> >  libavutil/mathematics.h | 21 +++++++++++++++++++++
> >  2 files changed, 31 insertions(+)
> >
> > diff --git a/libavutil/mathematics.c b/libavutil/mathematics.c
> > index f4e541fa24..2c7f57b950 100644
> > --- a/libavutil/mathematics.c
> > +++ b/libavutil/mathematics.c
> > @@ -212,3 +212,13 @@ int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t i
> >          return av_sat_add64(av_rescale_q(old + 1, inc_tb, ts_tb), ts - old_ts);
> >      }
> >  }
> > +
> > +void av_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);
> > +}
> > diff --git a/libavutil/mathematics.h b/libavutil/mathematics.h
> > index 64d4137a60..eb8a3f4002 100644
> > --- a/libavutil/mathematics.h
> > +++ b/libavutil/mathematics.h
> > @@ -161,6 +161,27 @@ int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const;
> >  int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq,
> >                           enum AVRounding rnd) av_const;
> >
> > +/**
> > + * 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 av_rescale_interval(AVRational tb_in, AVRational tb_out,
> > +                         int64_t *min_ts, int64_t *ts, int64_t *max_ts);
> > +
> > +
> >  /**
> >   * Compare two timestamps each in its own time base.
> >   *
>
> I don't see why this function should be public at all. It seems very
> specific to a usecase in lavf.

Ok. Unless someone objects/has a better idea, I cam move it to seek.c
as ff_rescale_interval() since it is primarily useful (today) with
seek_file().

>
> - Andreas
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Pierre-Anthony Lemieux March 7, 2022, 10:17 p.m. UTC | #3
On Fri, Mar 4, 2022 at 12:40 PM Pierre-Anthony Lemieux <pal@sandflow.com> wrote:
>
> On Fri, Mar 4, 2022 at 12:13 PM Andreas Rheinhardt
> <andreas.rheinhardt@outlook.com> wrote:
> >
> > pal@sandflow.com:
> > > From: Pierre-Anthony Lemieux <pal@palemieux.com>
> > >
> > > Refactors a function used by avformat/concat and avformat/imf.
> > >
> > > ---
> > >  libavutil/mathematics.c | 10 ++++++++++
> > >  libavutil/mathematics.h | 21 +++++++++++++++++++++
> > >  2 files changed, 31 insertions(+)
> > >
> > > diff --git a/libavutil/mathematics.c b/libavutil/mathematics.c
> > > index f4e541fa24..2c7f57b950 100644
> > > --- a/libavutil/mathematics.c
> > > +++ b/libavutil/mathematics.c
> > > @@ -212,3 +212,13 @@ int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t i
> > >          return av_sat_add64(av_rescale_q(old + 1, inc_tb, ts_tb), ts - old_ts);
> > >      }
> > >  }
> > > +
> > > +void av_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);
> > > +}
> > > diff --git a/libavutil/mathematics.h b/libavutil/mathematics.h
> > > index 64d4137a60..eb8a3f4002 100644
> > > --- a/libavutil/mathematics.h
> > > +++ b/libavutil/mathematics.h
> > > @@ -161,6 +161,27 @@ int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const;
> > >  int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq,
> > >                           enum AVRounding rnd) av_const;
> > >
> > > +/**
> > > + * 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 av_rescale_interval(AVRational tb_in, AVRational tb_out,
> > > +                         int64_t *min_ts, int64_t *ts, int64_t *max_ts);
> > > +
> > > +
> > >  /**
> > >   * Compare two timestamps each in its own time base.
> > >   *
> >
> > I don't see why this function should be public at all. It seems very
> > specific to a usecase in lavf.
>
> Ok. Unless someone objects/has a better idea, I cam move it to seek.c
> as ff_rescale_interval() since it is primarily useful (today) with
> seek_file().

Addressed by v3 at
http://ffmpeg.org/pipermail/ffmpeg-devel/2022-March/293795.html


>
> >
> > - Andreas
> > _______________________________________________
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff mbox series

Patch

diff --git a/libavutil/mathematics.c b/libavutil/mathematics.c
index f4e541fa24..2c7f57b950 100644
--- a/libavutil/mathematics.c
+++ b/libavutil/mathematics.c
@@ -212,3 +212,13 @@  int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t i
         return av_sat_add64(av_rescale_q(old + 1, inc_tb, ts_tb), ts - old_ts);
     }
 }
+
+void av_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);
+}
diff --git a/libavutil/mathematics.h b/libavutil/mathematics.h
index 64d4137a60..eb8a3f4002 100644
--- a/libavutil/mathematics.h
+++ b/libavutil/mathematics.h
@@ -161,6 +161,27 @@  int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const;
 int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq,
                          enum AVRounding rnd) av_const;
 
+/**
+ * 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 av_rescale_interval(AVRational tb_in, AVRational tb_out,
+                         int64_t *min_ts, int64_t *ts, int64_t *max_ts);
+
+
 /**
  * Compare two timestamps each in its own time base.
  *