ref: a0678eac0ee1a67ff871247b551e42fa448591b7
parent: 4d97f5a9d959679d472e7aa9f68503a69eb47202
author: Henrik Gramner <gramner@twoorioles.com>
date: Sun May 10 18:44:33 EDT 2020
cli: Reduce fps fraction in ivf parsing Also avoid integer overflows by using 64-bit intermediate precision.
--- a/tools/input/ivf.c
+++ b/tools/input/ivf.c
@@ -28,6 +28,7 @@
#include "config.h"
#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
@@ -92,8 +93,27 @@
break; // EOF
fseeko(c->f, rl32(data) + 8, SEEK_CUR);
}
- fps[0] = timebase[0] * *num_frames;
- fps[1] = timebase[1] * duration;
+
+ uint64_t fps_num = (uint64_t) timebase[0] * *num_frames;
+ uint64_t fps_den = (uint64_t) timebase[1] * duration;
+ if (fps_num && fps_den) { /* Reduce fraction */
+ uint64_t gcd = fps_num;
+ for (uint64_t a = fps_den, b; (b = a % gcd); a = gcd, gcd = b);
+ fps_num /= gcd;
+ fps_den /= gcd;
+
+ while ((fps_num | fps_den) > UINT_MAX) {
+ fps_num >>= 1;
+ fps_den >>= 1;
+ }
+ }
+ if (fps_num && fps_den) {
+ fps[0] = (unsigned) fps_num;
+ fps[1] = (unsigned) fps_den;
+ } else {
+ fps[0] = fps[1] = 0;
+ }
+
fseeko(c->f, 32, SEEK_SET);
return 0;