Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / ffmpeg / libavutil / mathematics.c
index 81841f7..126cffc 100644 (file)
@@ -188,14 +188,22 @@ simple_round:
 
 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});
+    int64_t m, d;
 
-    if (av_cmp_q(step, ts_tb) < 0) {
-        //increase step is too small for even 1 step to be representable
+    if (inc != 1)
+        inc_tb = av_mul_q(inc_tb, (AVRational) {inc, 1});
+
+    m = inc_tb.num * (int64_t)ts_tb.den;
+    d = inc_tb.den * (int64_t)ts_tb.num;
+
+    if (m % d == 0)
+        return ts + m / d;
+    if (m < d)
         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);
+
+    {
+        int64_t old = av_rescale_q(ts, ts_tb, inc_tb);
+        int64_t old_ts = av_rescale_q(old, inc_tb, ts_tb);
+        return av_rescale_q(old + 1, inc_tb, ts_tb) + (ts - old_ts);
     }
 }