qtmux: Be more clever with the default video track timescale
authorSebastian Dröge <sebastian@centricular.com>
Thu, 20 Oct 2016 17:07:19 +0000 (20:07 +0300)
committerSebastian Dröge <sebastian@centricular.com>
Tue, 1 Nov 2016 18:11:12 +0000 (20:11 +0200)
Use the number of milliframes per second for integral and drop-frame
framerates, as suggested by the QT file format specification and other
places. We already did that for integral framerates before, but not for
drop-frame framerates. This now keeps precision better.

For all other framerates, check if it's close to a well-known framerate
and use that instead.

https://bugzilla.gnome.org/show_bug.cgi?id=769041

gst/isomp4/gstqtmux.c

index 27f9090..9a18fa0 100644 (file)
@@ -3882,20 +3882,21 @@ refuse_renegotiation:
   }
 }
 
-/* scale rate up or down by factor of 10 to fit into [1000,10000] interval */
-static guint32
-adjust_rate (guint64 rate)
+/* return number of centiframes per second */
+static guint
+adjust_rate (gint n, gint d)
 {
-  if (rate == 0)
+  if (n == 0)
     return 10000;
 
-  while (rate >= 10000)
-    rate /= 10;
-
-  while (rate < 1000)
-    rate *= 10;
+  if (d != 1 && d != 1001) {
+    /* otherwise there are probably rounding errors and we should rather guess
+     * if it's close enough to a well known framerate */
+    gst_video_guess_framerate (gst_util_uint64_scale (d, GST_SECOND, n), &n,
+        &d);
+  }
 
-  return (guint32) rate;
+  return gst_util_uint64_scale (n, 100, d);
 }
 
 static gboolean
@@ -3973,7 +3974,7 @@ gst_qt_mux_video_sink_set_caps (GstQTPad * qtpad, GstCaps * caps)
   /* bring frame numerator into a range that ensures both reasonable resolution
    * as well as a fair duration */
   rate = qtmux->trak_timescale ?
-      qtmux->trak_timescale : adjust_rate (framerate_num);
+      qtmux->trak_timescale : adjust_rate (framerate_num, framerate_den);
   GST_DEBUG_OBJECT (qtmux, "Rate of video track selected: %" G_GUINT32_FORMAT,
       rate);