]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/mathematics.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavutil / mathematics.c
index f9cf87da803418f32800878d0b7e21c4c66b8480..30963aa07b434b935ebbf393a01311552cb49637 100644 (file)
@@ -63,6 +63,9 @@ int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
     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;
@@ -171,3 +174,17 @@ simple_round:
 
     return av_rescale_q(this, fs_tb, out_tb);
 }
+
+int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t inc)
+{
+    AVRational step = av_mul_q(inc_tb, (AVRational) {inc, 1});
+
+    if (av_cmp_q(step, ts_tb) < 0) {
+        //increase step is too small for even 1 step to be representable
+        return ts;
+    } else {
+        int64_t old = av_rescale_q(ts, ts_tb, step);
+        int64_t old_ts = av_rescale_q(old, step, ts_tb);
+        return av_rescale_q(old + 1, step, ts_tb) + (ts - old_ts);
+    }
+}