videorate: Add max-duplication-time property
authorVivia Nikolaidou <vivia@ahiru.eu>
Tue, 19 Feb 2019 14:59:34 +0000 (16:59 +0200)
committerVivia Nikolaidou <vivia@ahiru.eu>
Thu, 21 Feb 2019 15:50:55 +0000 (15:50 +0000)
This will only duplicate buffers if the gap between two consecutive
buffers is up to fill-until nsec. If it's larger, it will only output
the new buffer and mark it as discont.

gst/videorate/gstvideorate.c
gst/videorate/gstvideorate.h
tests/check/elements/videorate.c

index 5ff8de9..fb692ff 100644 (file)
@@ -99,6 +99,7 @@ enum
 #define DEFAULT_AVERAGE_PERIOD  0
 #define DEFAULT_MAX_RATE        G_MAXINT
 #define DEFAULT_RATE            1.0
+#define DEFAULT_MAX_DUPLICATION_TIME      0
 
 enum
 {
@@ -113,7 +114,8 @@ enum
   PROP_DROP_ONLY,
   PROP_AVERAGE_PERIOD,
   PROP_MAX_RATE,
-  PROP_RATE
+  PROP_RATE,
+  PROP_MAX_DUPLICATION_TIME
 };
 
 static GstStaticPadTemplate gst_video_rate_src_template =
@@ -275,6 +277,22 @@ gst_video_rate_class_init (GstVideoRateClass * klass)
           DEFAULT_RATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
           GST_PARAM_MUTABLE_READY));
 
+  /**
+   * GstVideoRate:max-duplication-time:
+   *
+   * Duplicate frames only if the gap between two consecutive frames does not
+   * exceed this duration.
+   *
+   * Since: 1.16
+   */
+  g_object_class_install_property (object_class, PROP_MAX_DUPLICATION_TIME,
+      g_param_spec_uint64 ("max-duplication-time",
+          "Maximum time to duplicate a frame",
+          "Do not duplicate frames if the gap exceeds this period "
+          "(in ns) (0 = disabled)",
+          0, G_MAXUINT64, DEFAULT_MAX_DUPLICATION_TIME,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   gst_element_class_set_static_metadata (element_class,
       "Video rate adjuster", "Filter/Effect/Video",
       "Drops/duplicates/adjusts timestamps on video frames to make a perfect stream",
@@ -615,6 +633,7 @@ gst_video_rate_init (GstVideoRate * videorate)
   videorate->average_period_set = DEFAULT_AVERAGE_PERIOD;
   videorate->max_rate = DEFAULT_MAX_RATE;
   videorate->rate = DEFAULT_RATE;
+  videorate->max_duplication_time = DEFAULT_MAX_DUPLICATION_TIME;
 
   videorate->from_rate_numerator = 0;
   videorate->from_rate_denominator = 0;
@@ -1446,6 +1465,19 @@ gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
       goto done;
     }
 
+    if (videorate->max_duplication_time > 0) {
+      /* We already know that intime and prevtime are not out of order, based
+       * on the previous condition. Using ABS in case rate < 0, in which case
+       * the order is reversed. */
+      if (ABS (GST_CLOCK_DIFF (intime,
+                  prevtime)) > videorate->max_duplication_time) {
+        /* The gap between the two buffers is too large. Don't fill it, just
+         * let a discont through */
+        videorate->discont = TRUE;
+        goto done;
+      }
+    }
+
     /* got 2 buffers, see which one is the best */
     do {
       GstClockTime next_ts;
@@ -1645,6 +1677,9 @@ gst_video_rate_set_property (GObject * object,
 
       gst_videorate_update_duration (videorate);
       return;
+    case PROP_MAX_DUPLICATION_TIME:
+      videorate->max_duplication_time = g_value_get_uint64 (value);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -1704,6 +1739,9 @@ gst_video_rate_get_property (GObject * object,
     case PROP_RATE:
       g_value_set_double (value, videorate->rate);
       break;
+    case PROP_MAX_DUPLICATION_TIME:
+      g_value_set_uint64 (value, videorate->max_duplication_time);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
index 9a64413..650aecc 100644 (file)
@@ -65,6 +65,7 @@ struct _GstVideoRate
   GstClockTimeDiff average;     /* moving average period */
   gboolean force_variable_rate;
   gboolean updating_caps;
+  guint64 max_duplication_time;
 
   /* segment handling */
   GstSegment segment;
index 4aecdc9..87ae64a 100644 (file)
@@ -402,6 +402,107 @@ GST_START_TEST (test_wrong_order_from_zero)
 
 GST_END_TEST;
 
+/* send frames with 0, 1, 2, 5 seconds, max-duplication-time=2sec */
+GST_START_TEST (test_max_duplication_time)
+{
+  GstElement *videorate;
+  GstBuffer *first, *second, *third, *fourth, *outbuffer;
+  GstCaps *caps;
+
+  videorate = setup_videorate ();
+  g_object_set (videorate, "max-duplication-time", 2 * GST_SECOND, NULL);
+  fail_unless (gst_element_set_state (videorate,
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
+      "could not set to playing");
+  assert_videorate_stats (videorate, "start", 0, 0, 0, 0);
+
+  /* first buffer */
+  first = gst_buffer_new_and_alloc (4);
+  GST_BUFFER_TIMESTAMP (first) = 0;
+  gst_buffer_memset (first, 0, 0, 4);
+  caps = gst_caps_from_string (VIDEO_CAPS_STRING);
+  gst_check_setup_events (mysrcpad, videorate, caps, GST_FORMAT_TIME);
+  gst_caps_unref (caps);
+  ASSERT_BUFFER_REFCOUNT (first, "first", 1);
+  gst_buffer_ref (first);
+
+  GST_DEBUG ("pushing first buffer");
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, first) == GST_FLOW_OK);
+  /* ... and a copy is now stuck inside videorate */
+  ASSERT_BUFFER_REFCOUNT (first, "first", 1);
+  fail_unless_equals_int (g_list_length (buffers), 0);
+  assert_videorate_stats (videorate, "first", 1, 0, 0, 0);
+
+  /* second buffer */
+  second = gst_buffer_new_and_alloc (4);
+  GST_BUFFER_TIMESTAMP (second) = GST_SECOND;
+  gst_buffer_memset (second, 0, 0, 4);
+  ASSERT_BUFFER_REFCOUNT (second, "second", 1);
+  gst_buffer_ref (second);
+
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, second) == GST_FLOW_OK);
+  /* ... and a copy is now stuck inside videorate */
+  ASSERT_BUFFER_REFCOUNT (second, "second", 1);
+  /* and it created 13 output buffers as copies of the first frame */
+  fail_unless_equals_int (g_list_length (buffers), 13);
+  assert_videorate_stats (videorate, "second", 2, 13, 0, 12);
+  ASSERT_BUFFER_REFCOUNT (first, "first", 1);
+
+  /* third buffer */
+  third = gst_buffer_new_and_alloc (4);
+  GST_BUFFER_TIMESTAMP (third) = 2 * GST_SECOND;
+  gst_buffer_memset (third, 0, 0, 4);
+  ASSERT_BUFFER_REFCOUNT (third, "third", 1);
+  gst_buffer_ref (third);
+
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, third) == GST_FLOW_OK);
+  /* ... and a copy is now stuck inside videorate */
+  ASSERT_BUFFER_REFCOUNT (third, "third", 1);
+
+  /* submitting a frame with 2 seconds triggers output of 25 more frames */
+  fail_unless_equals_int (g_list_length (buffers), 38);
+  ASSERT_BUFFER_REFCOUNT (first, "first", 1);
+  ASSERT_BUFFER_REFCOUNT (second, "second", 1);
+  /* three frames submitted; two of them output as is, and 36 duplicated */
+  assert_videorate_stats (videorate, "third", 3, 38, 0, 36);
+
+  /* fourth buffer */
+  fourth = gst_buffer_new_and_alloc (4);
+  GST_BUFFER_TIMESTAMP (fourth) = 5 * GST_SECOND;
+  gst_buffer_memset (fourth, 0, 0, 4);
+  ASSERT_BUFFER_REFCOUNT (fourth, "fourth", 1);
+  gst_buffer_ref (fourth);
+
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, fourth) == GST_FLOW_OK);
+  /* ... and a copy is now stuck inside videorate */
+  ASSERT_BUFFER_REFCOUNT (fourth, "fourth", 1);
+
+  fail_unless_equals_int (g_list_length (buffers), 38);
+  ASSERT_BUFFER_REFCOUNT (first, "first", 1);
+  ASSERT_BUFFER_REFCOUNT (second, "second", 1);
+  assert_videorate_stats (videorate, "fourth", 4, 38, 0, 36);
+
+  /* verify last buffer */
+  outbuffer = g_list_last (buffers)->data;
+  fail_unless (GST_IS_BUFFER (outbuffer));
+  fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer),
+      GST_SECOND * 37 / 25);
+
+
+  /* cleanup */
+  gst_buffer_unref (first);
+  gst_buffer_unref (second);
+  gst_buffer_unref (third);
+  gst_buffer_unref (fourth);
+  cleanup_videorate (videorate);
+}
+
+GST_END_TEST;
+
 /* send frames with 0, 1, 2, 0 seconds */
 GST_START_TEST (test_wrong_order)
 {
@@ -1419,6 +1520,7 @@ videorate_suite (void)
   tcase_add_test (tc_chain, test_variable_framerate_renegotiation);
   tcase_add_loop_test (tc_chain, test_rate, 0, G_N_ELEMENTS (rate_tests));
   tcase_add_test (tc_chain, test_query_duration);
+  tcase_add_test (tc_chain, test_max_duplication_time);
   tcase_add_loop_test (tc_chain, test_query_position, 0,
       G_N_ELEMENTS (position_tests));