videorate: Handle closing segment on EOS right after caps event
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-base / gst / videorate / gstvideorate.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-videorate
22  * @title: videorate
23  *
24  * This element takes an incoming stream of timestamped video frames.
25  * It will produce a perfect stream that matches the source pad's framerate.
26  *
27  * The correction is performed by dropping and duplicating frames, no fancy
28  * algorithm is used to interpolate frames (yet).
29  *
30  * By default the element will simply negotiate the same framerate on its
31  * source and sink pad.
32  *
33  * This operation is useful to link to elements that require a perfect stream.
34  * Typical examples are formats that do not store timestamps for video frames,
35  * but only store a framerate, like Ogg and AVI.
36  *
37  * A conversion to a specific framerate can be forced by using filtered caps on
38  * the source pad.
39  *
40  * The properties #GstVideoRate:in, #GstVideoRate:out, #GstVideoRate:duplicate
41  * and #GstVideoRate:drop can be read to obtain information about number of
42  * input frames, output frames, dropped frames (i.e. the number of unused input
43  * frames) and duplicated frames (i.e. the number of times an input frame was
44  * duplicated, beside being used normally).
45  *
46  * An input stream that needs no adjustments will thus never have dropped or
47  * duplicated frames.
48  *
49  * When the #GstVideoRate:silent property is set to FALSE, a GObject property
50  * notification will be emitted whenever one of the #GstVideoRate:duplicate or
51  * #GstVideoRate:drop values changes.
52  * This can potentially cause performance degradation.
53  * Note that property notification will happen from the streaming thread, so
54  * applications should be prepared for this.
55  *
56  * The property #GstVideoRate:rate allows the modification of video speed by a
57  * certain factor. It must not be confused with framerate. Think of rate as
58  * speed and framerate as flow.
59  *
60  * ## Example pipelines
61  * |[
62  * gst-launch-1.0 -v uridecodebin uri=file:///path/to/video.ogg ! videoconvert ! videoscale ! videorate ! video/x-raw,framerate=15/1 ! autovideosink
63  * ]|
64  *  Decode a video file and adjust the framerate to 15 fps before playing.
65  * To create a test Ogg/Theora file refer to the documentation of theoraenc.
66  * |[
67  * gst-launch-1.0 -v v4l2src ! videorate ! video/x-raw,framerate=25/2 ! theoraenc ! oggmux ! filesink location=recording.ogg
68  * ]|
69  *  Capture video from a V4L device, and adjust the stream to 12.5 fps before
70  * encoding to Ogg/Theora.
71  * |[
72  * gst-launch-1.0 -v uridecodebin uri=file:///path/to/video.ogg ! videoconvert ! videoscale ! videorate ! video/x-raw,framerate=1/5 ! jpegenc ! multifilesink location=snapshot-%05d.jpg
73  * ]|
74  *  Decode a video file and save a snapshot every 5 seconds as consecutively numbered jpeg file.
75  *
76  */
77
78 #ifdef HAVE_CONFIG_H
79 #include "config.h"
80 #endif
81
82 #include "gstvideorate.h"
83 #include <gst/video/video.h>
84
85 GST_DEBUG_CATEGORY_STATIC (video_rate_debug);
86 #define GST_CAT_DEFAULT video_rate_debug
87
88 /* GstVideoRate signals and args */
89 enum
90 {
91   /* FILL ME */
92   LAST_SIGNAL
93 };
94
95 #define DEFAULT_SILENT          TRUE
96 #define DEFAULT_NEW_PREF        1.0
97 #define DEFAULT_SKIP_TO_FIRST   FALSE
98 #define DEFAULT_DROP_ONLY       FALSE
99 #define DEFAULT_AVERAGE_PERIOD  0
100 #define DEFAULT_MAX_RATE        G_MAXINT
101 #define DEFAULT_RATE            1.0
102 #define DEFAULT_MAX_DUPLICATION_TIME      0
103 #define DEFAULT_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION   GST_SECOND
104
105 enum
106 {
107   PROP_0,
108   PROP_IN,
109   PROP_OUT,
110   PROP_DUP,
111   PROP_DROP,
112   PROP_SILENT,
113   PROP_NEW_PREF,
114   PROP_SKIP_TO_FIRST,
115   PROP_DROP_ONLY,
116   PROP_AVERAGE_PERIOD,
117   PROP_MAX_RATE,
118   PROP_RATE,
119   PROP_MAX_DUPLICATION_TIME,
120   PROP_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION
121 };
122
123 static GstStaticPadTemplate gst_video_rate_src_template =
124     GST_STATIC_PAD_TEMPLATE ("src",
125     GST_PAD_SRC,
126     GST_PAD_ALWAYS,
127     GST_STATIC_CAPS ("video/x-raw(ANY);" "video/x-bayer(ANY);"
128         "image/jpeg(ANY);" "image/png(ANY)")
129     );
130
131 static GstStaticPadTemplate gst_video_rate_sink_template =
132     GST_STATIC_PAD_TEMPLATE ("sink",
133     GST_PAD_SINK,
134     GST_PAD_ALWAYS,
135     GST_STATIC_CAPS ("video/x-raw(ANY);" "video/x-bayer(ANY);"
136         "image/jpeg(ANY);" "image/png(ANY)")
137     );
138
139 static void gst_video_rate_swap_prev (GstVideoRate * videorate,
140     GstBuffer * buffer, gint64 time);
141 static gboolean gst_video_rate_sink_event (GstBaseTransform * trans,
142     GstEvent * event);
143 static gboolean gst_video_rate_src_event (GstBaseTransform * trans,
144     GstEvent * event);
145 static gboolean gst_video_rate_query (GstBaseTransform * trans,
146     GstPadDirection direction, GstQuery * query);
147
148 static gboolean gst_video_rate_setcaps (GstBaseTransform * trans,
149     GstCaps * in_caps, GstCaps * out_caps);
150
151 static GstCaps *gst_video_rate_transform_caps (GstBaseTransform * trans,
152     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
153
154 static GstCaps *gst_video_rate_fixate_caps (GstBaseTransform * trans,
155     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
156
157 static GstFlowReturn gst_video_rate_transform_ip (GstBaseTransform * trans,
158     GstBuffer * buf);
159
160 static gboolean gst_video_rate_propose_allocation (GstBaseTransform * trans,
161     GstQuery * decide_query, GstQuery * query);
162
163 static gboolean gst_video_rate_start (GstBaseTransform * trans);
164 static gboolean gst_video_rate_stop (GstBaseTransform * trans);
165
166
167 static void gst_video_rate_set_property (GObject * object,
168     guint prop_id, const GValue * value, GParamSpec * pspec);
169 static void gst_video_rate_get_property (GObject * object,
170     guint prop_id, GValue * value, GParamSpec * pspec);
171
172 static GParamSpec *pspec_drop = NULL;
173 static GParamSpec *pspec_duplicate = NULL;
174
175 #define gst_video_rate_parent_class parent_class
176 G_DEFINE_TYPE (GstVideoRate, gst_video_rate, GST_TYPE_BASE_TRANSFORM);
177 GST_ELEMENT_REGISTER_DEFINE (videorate, "videorate",
178     GST_RANK_NONE, GST_TYPE_VIDEO_RATE);
179
180 static void
181 gst_video_rate_class_init (GstVideoRateClass * klass)
182 {
183   GObjectClass *object_class = G_OBJECT_CLASS (klass);
184   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
185   GstBaseTransformClass *base_class = GST_BASE_TRANSFORM_CLASS (klass);
186
187   object_class->set_property = gst_video_rate_set_property;
188   object_class->get_property = gst_video_rate_get_property;
189
190   base_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_rate_setcaps);
191   base_class->transform_caps =
192       GST_DEBUG_FUNCPTR (gst_video_rate_transform_caps);
193   base_class->transform_ip = GST_DEBUG_FUNCPTR (gst_video_rate_transform_ip);
194   base_class->sink_event = GST_DEBUG_FUNCPTR (gst_video_rate_sink_event);
195   base_class->src_event = GST_DEBUG_FUNCPTR (gst_video_rate_src_event);
196   base_class->start = GST_DEBUG_FUNCPTR (gst_video_rate_start);
197   base_class->stop = GST_DEBUG_FUNCPTR (gst_video_rate_stop);
198   base_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_video_rate_fixate_caps);
199   base_class->query = GST_DEBUG_FUNCPTR (gst_video_rate_query);
200   base_class->propose_allocation =
201       GST_DEBUG_FUNCPTR (gst_video_rate_propose_allocation);
202
203   g_object_class_install_property (object_class, PROP_IN,
204       g_param_spec_uint64 ("in", "In",
205           "Number of input frames", 0, G_MAXUINT64, 0,
206           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
207   g_object_class_install_property (object_class, PROP_OUT,
208       g_param_spec_uint64 ("out", "Out", "Number of output frames", 0,
209           G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
210   pspec_duplicate = g_param_spec_uint64 ("duplicate", "Duplicate",
211       "Number of duplicated frames", 0, G_MAXUINT64, 0,
212       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
213   g_object_class_install_property (object_class, PROP_DUP, pspec_duplicate);
214   pspec_drop = g_param_spec_uint64 ("drop", "Drop", "Number of dropped frames",
215       0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
216   g_object_class_install_property (object_class, PROP_DROP, pspec_drop);
217   g_object_class_install_property (object_class, PROP_SILENT,
218       g_param_spec_boolean ("silent", "silent",
219           "Don't emit notify for dropped and duplicated frames", DEFAULT_SILENT,
220           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221   g_object_class_install_property (object_class, PROP_NEW_PREF,
222       g_param_spec_double ("new-pref", "New Pref",
223           "Value indicating how much to prefer new frames (unused)", 0.0, 1.0,
224           DEFAULT_NEW_PREF, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225
226   /**
227    * GstVideoRate:skip-to-first:
228    *
229    * Don't produce buffers before the first one we receive.
230    */
231   g_object_class_install_property (object_class, PROP_SKIP_TO_FIRST,
232       g_param_spec_boolean ("skip-to-first", "Skip to first buffer",
233           "Don't produce buffers before the first one we receive",
234           DEFAULT_SKIP_TO_FIRST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235
236   /**
237    * GstVideoRate:drop-only:
238    *
239    * Only drop frames, no duplicates are produced.
240    */
241   g_object_class_install_property (object_class, PROP_DROP_ONLY,
242       g_param_spec_boolean ("drop-only", "Only Drop",
243           "Only drop frames, no duplicates are produced",
244           DEFAULT_DROP_ONLY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245
246   /**
247    * GstVideoRate:average-period:
248    *
249    * Arrange for maximum framerate by dropping frames beyond a certain framerate,
250    * where the framerate is calculated using a moving average over the
251    * configured.
252    */
253   g_object_class_install_property (object_class, PROP_AVERAGE_PERIOD,
254       g_param_spec_uint64 ("average-period", "Period over which to average",
255           "Period over which to average the framerate (in ns) (0 = disabled)",
256           0, G_MAXINT64, DEFAULT_AVERAGE_PERIOD,
257           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258
259   /**
260    * GstVideoRate:max-rate:
261    *
262    * maximum framerate to pass through
263    */
264   g_object_class_install_property (object_class, PROP_MAX_RATE,
265       g_param_spec_int ("max-rate", "maximum framerate",
266           "Maximum framerate allowed to pass through "
267           "(in frames per second, implies drop-only)",
268           1, G_MAXINT, DEFAULT_MAX_RATE,
269           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
270
271   /**
272    * GstVideoRate:rate:
273    *
274    * Factor of speed for frame displaying
275    *
276    * Since: 1.12
277    */
278   g_object_class_install_property (object_class, PROP_RATE,
279       g_param_spec_double ("rate", "Rate",
280           "Factor of speed for frame displaying", 0.0, G_MAXDOUBLE,
281           DEFAULT_RATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
282           GST_PARAM_MUTABLE_READY));
283
284   /**
285    * GstVideoRate:max-duplication-time:
286    *
287    * Duplicate frames only if the gap between two consecutive frames does not
288    * exceed this duration.
289    *
290    * Since: 1.16
291    */
292   g_object_class_install_property (object_class, PROP_MAX_DUPLICATION_TIME,
293       g_param_spec_uint64 ("max-duplication-time",
294           "Maximum time to duplicate a frame",
295           "Do not duplicate frames if the gap exceeds this period "
296           "(in ns) (0 = disabled)",
297           0, G_MAXUINT64, DEFAULT_MAX_DUPLICATION_TIME,
298           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299
300   /**
301    * GstVideoRate:max-closing-segment-duplication-duration:
302    *
303    * Limits the maximum duration for which the last buffer is duplicated when
304    * finalizing a segment or on EOS. When receiving an EOS event or a new
305    * segment, videorate duplicates the last frame to close the configured
306    * segment (copying the last buffer until its #GstSegment.stop time (or
307    * #GstSegment.start time for reverse playback) is reached), this property
308    * ensures that it won't push buffers covering a duration longer than
309    * specified.
310    *
311    * Since: 1.22
312    */
313   g_object_class_install_property (object_class,
314       PROP_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION,
315       g_param_spec_uint64 ("max-closing-segment-duplication-duration",
316           "Maximum closing segment duplication duration",
317           "Maximum duration of duplicated buffers to close current segment", 0,
318           G_MAXUINT64, DEFAULT_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION,
319           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
320
321   gst_element_class_set_static_metadata (element_class,
322       "Video rate adjuster", "Filter/Effect/Video",
323       "Drops/duplicates/adjusts timestamps on video frames to make a perfect stream",
324       "Wim Taymans <wim@fluendo.com>");
325
326   gst_element_class_add_static_pad_template (element_class,
327       &gst_video_rate_sink_template);
328   gst_element_class_add_static_pad_template (element_class,
329       &gst_video_rate_src_template);
330 }
331
332 static void
333 gst_value_fraction_get_extremes (const GValue * v,
334     gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
335 {
336   if (GST_VALUE_HOLDS_FRACTION (v)) {
337     *min_num = *max_num = gst_value_get_fraction_numerator (v);
338     *min_denom = *max_denom = gst_value_get_fraction_denominator (v);
339   } else if (GST_VALUE_HOLDS_FRACTION_RANGE (v)) {
340     const GValue *min, *max;
341
342     min = gst_value_get_fraction_range_min (v);
343     *min_num = gst_value_get_fraction_numerator (min);
344     *min_denom = gst_value_get_fraction_denominator (min);
345
346     max = gst_value_get_fraction_range_max (v);
347     *max_num = gst_value_get_fraction_numerator (max);
348     *max_denom = gst_value_get_fraction_denominator (max);
349   } else if (GST_VALUE_HOLDS_LIST (v)) {
350     gint min_n = G_MAXINT, min_d = 1, max_n = 0, max_d = 1;
351     int i, n;
352
353     *min_num = G_MAXINT;
354     *min_denom = 1;
355     *max_num = 0;
356     *max_denom = 1;
357
358     n = gst_value_list_get_size (v);
359
360     g_assert (n > 0);
361
362     for (i = 0; i < n; i++) {
363       const GValue *t = gst_value_list_get_value (v, i);
364
365       gst_value_fraction_get_extremes (t, &min_n, &min_d, &max_n, &max_d);
366       if (gst_util_fraction_compare (min_n, min_d, *min_num, *min_denom) < 0) {
367         *min_num = min_n;
368         *min_denom = min_d;
369       }
370
371       if (gst_util_fraction_compare (max_n, max_d, *max_num, *max_denom) > 0) {
372         *max_num = max_n;
373         *max_denom = max_d;
374       }
375     }
376   } else {
377     g_warning ("Unknown type for framerate");
378     *min_num = 0;
379     *min_denom = 1;
380     *max_num = G_MAXINT;
381     *max_denom = 1;
382   }
383 }
384
385 /* Clamp the framerate in a caps structure to be a smaller range then
386  * [1...max_rate], otherwise return false */
387 static gboolean
388 gst_video_max_rate_clamp_structure (GstStructure * s, gint maxrate,
389     gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
390 {
391   gboolean ret = FALSE;
392
393   if (!gst_structure_has_field (s, "framerate")) {
394     /* No framerate field implies any framerate, clamping would result in
395      * [1..max_rate] so not a real subset */
396     goto out;
397   } else {
398     const GValue *v;
399     GValue intersection = { 0, };
400     GValue clamp = { 0, };
401     gint tmp_num, tmp_denom;
402
403     g_value_init (&clamp, GST_TYPE_FRACTION_RANGE);
404     gst_value_set_fraction_range_full (&clamp, 0, 1, maxrate, 1);
405
406     v = gst_structure_get_value (s, "framerate");
407     ret = gst_value_intersect (&intersection, v, &clamp);
408     g_value_unset (&clamp);
409
410     if (!ret)
411       goto out;
412
413     gst_value_fraction_get_extremes (&intersection,
414         min_num, min_denom, max_num, max_denom);
415
416     gst_value_fraction_get_extremes (v,
417         &tmp_num, &tmp_denom, max_num, max_denom);
418
419     if (gst_util_fraction_compare (*max_num, *max_denom, maxrate, 1) > 0) {
420       *max_num = maxrate;
421       *max_denom = 1;
422     }
423
424     gst_structure_take_value (s, "framerate", &intersection);
425   }
426
427 out:
428   return ret;
429 }
430
431 static GstCaps *
432 gst_video_rate_transform_caps (GstBaseTransform * trans,
433     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
434 {
435   GstVideoRate *videorate = GST_VIDEO_RATE (trans);
436   GstCaps *ret;
437   GstStructure *s, *s1, *s2, *s3 = NULL;
438   int maxrate = g_atomic_int_get (&videorate->max_rate);
439   gint i;
440
441   ret = gst_caps_new_empty ();
442
443   for (i = 0; i < gst_caps_get_size (caps); i++) {
444     s = gst_caps_get_structure (caps, i);
445
446     s1 = gst_structure_copy (s);
447
448     if (videorate->updating_caps && direction == GST_PAD_SINK) {
449       GST_INFO_OBJECT (trans,
450           "Only updating caps %" GST_PTR_FORMAT " with framerate" " %d/%d",
451           caps, videorate->to_rate_numerator, videorate->to_rate_denominator);
452
453       gst_structure_set (s1, "framerate", GST_TYPE_FRACTION,
454           videorate->to_rate_numerator, videorate->to_rate_denominator, NULL);
455       ret = gst_caps_merge_structure (ret, s1);
456
457       continue;
458     }
459
460     s2 = gst_structure_copy (s);
461     s3 = NULL;
462
463     if (videorate->drop_only) {
464       gint min_num = 0, min_denom = 1;
465       gint max_num = G_MAXINT, max_denom = 1;
466
467       /* Clamp the caps to our maximum rate as the first caps if possible */
468       if (!gst_video_max_rate_clamp_structure (s1, maxrate,
469               &min_num, &min_denom, &max_num, &max_denom)) {
470         min_num = 0;
471         min_denom = 1;
472         max_num = maxrate;
473         max_denom = 1;
474
475         /* clamp wouldn't be a real subset of 1..maxrate, in this case the sink
476          * caps should become [1..maxrate], [1..maxint] and the src caps just
477          * [1..maxrate].  In case there was a caps incompatibility things will
478          * explode later as appropriate :)
479          *
480          * In case [X..maxrate] == [X..maxint], skip as we'll set it later
481          */
482         if (direction == GST_PAD_SRC && maxrate != G_MAXINT)
483           gst_structure_set (s1, "framerate", GST_TYPE_FRACTION_RANGE,
484               min_num, min_denom, maxrate, 1, NULL);
485         else {
486           gst_structure_free (s1);
487           s1 = NULL;
488         }
489       }
490
491       if (direction == GST_PAD_SRC) {
492         /* We can accept anything as long as it's at least the minimal framerate
493          * the the sink needs */
494         gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE,
495             min_num, min_denom, G_MAXINT, 1, NULL);
496
497         /* Also allow unknown framerate, if it isn't already */
498         if (min_num != 0 || min_denom != 1) {
499           s3 = gst_structure_copy (s);
500           gst_structure_set (s3, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
501         }
502       } else if (max_num != 0 || max_denom != 1) {
503         /* We can provide everything up to the maximum framerate at the src */
504         gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE,
505             0, 1, max_num, max_denom, NULL);
506       }
507     } else if (direction == GST_PAD_SINK) {
508       gint min_num = 0, min_denom = 1;
509       gint max_num = G_MAXINT, max_denom = 1;
510
511       if (!gst_video_max_rate_clamp_structure (s1, maxrate,
512               &min_num, &min_denom, &max_num, &max_denom)) {
513         gst_structure_free (s1);
514         s1 = NULL;
515       }
516       gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
517           maxrate, 1, NULL);
518     } else {
519       /* set the framerate as a range */
520       gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
521           G_MAXINT, 1, NULL);
522     }
523     if (s1 != NULL)
524       ret = gst_caps_merge_structure_full (ret, s1,
525           gst_caps_features_copy (gst_caps_get_features (caps, i)));
526     ret = gst_caps_merge_structure_full (ret, s2,
527         gst_caps_features_copy (gst_caps_get_features (caps, i)));
528     if (s3 != NULL)
529       ret = gst_caps_merge_structure_full (ret, s3,
530           gst_caps_features_copy (gst_caps_get_features (caps, i)));
531   }
532   if (filter) {
533     GstCaps *intersection;
534
535     intersection =
536         gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
537     gst_caps_unref (ret);
538     ret = intersection;
539   }
540   return ret;
541 }
542
543 static GstCaps *
544 gst_video_rate_fixate_caps (GstBaseTransform * trans,
545     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
546 {
547   GstStructure *s;
548   gint num, denom;
549   const GValue *par;
550
551   s = gst_caps_get_structure (caps, 0);
552   if (G_UNLIKELY (!gst_structure_get_fraction (s, "framerate", &num, &denom)))
553     return othercaps;
554
555   othercaps = gst_caps_truncate (othercaps);
556   othercaps = gst_caps_make_writable (othercaps);
557   s = gst_caps_get_structure (othercaps, 0);
558   gst_structure_fixate_field_nearest_fraction (s, "framerate", num, denom);
559
560   if ((par = gst_structure_get_value (s, "pixel-aspect-ratio")))
561     gst_structure_fixate_field_nearest_fraction (s, "pixel-aspect-ratio", 1, 1);
562
563   return gst_caps_fixate (othercaps);
564 }
565
566 static gboolean
567 gst_video_rate_setcaps (GstBaseTransform * trans, GstCaps * in_caps,
568     GstCaps * out_caps)
569 {
570   GstVideoRate *videorate = GST_VIDEO_RATE (trans);
571   GstStructure *structure;
572   gboolean ret = TRUE;
573   gint rate_numerator, rate_denominator;
574
575   GST_DEBUG_OBJECT (trans, "setcaps called in: %" GST_PTR_FORMAT
576       " out: %" GST_PTR_FORMAT, in_caps, out_caps);
577
578   structure = gst_caps_get_structure (in_caps, 0);
579   if (!gst_structure_get_fraction (structure, "framerate",
580           &rate_numerator, &rate_denominator))
581     goto no_framerate;
582
583   videorate->from_rate_numerator = rate_numerator;
584   videorate->from_rate_denominator = rate_denominator;
585
586   structure = gst_caps_get_structure (out_caps, 0);
587   if (!gst_structure_get_fraction (structure, "framerate",
588           &rate_numerator, &rate_denominator))
589     goto no_framerate;
590
591   /* out_frame_count is scaled by the frame rate caps when calculating next_ts.
592    * when the frame rate caps change, we must update base_ts and reset
593    * out_frame_count */
594   if (videorate->to_rate_numerator) {
595     videorate->base_ts +=
596         gst_util_uint64_scale (videorate->out_frame_count +
597         (videorate->segment.rate < 0.0 ? 1 : 0),
598         videorate->to_rate_denominator * GST_SECOND,
599         videorate->to_rate_numerator);
600   }
601   videorate->out_frame_count = 0;
602   videorate->to_rate_numerator = rate_numerator;
603   videorate->to_rate_denominator = rate_denominator;
604
605   if (rate_numerator)
606     videorate->wanted_diff = gst_util_uint64_scale_int (GST_SECOND,
607         rate_denominator, rate_numerator);
608   else
609     videorate->wanted_diff = 0;
610
611 done:
612   if (ret) {
613     gst_caps_replace (&videorate->in_caps, in_caps);
614   }
615
616   return ret;
617
618 no_framerate:
619   {
620     GST_DEBUG_OBJECT (videorate, "no framerate specified");
621     ret = FALSE;
622     goto done;
623   }
624 }
625
626 static void
627 gst_video_rate_reset (GstVideoRate * videorate, gboolean on_flush)
628 {
629   GST_DEBUG_OBJECT (videorate, "resetting internal variables");
630
631   videorate->in = 0;
632   videorate->out = 0;
633   videorate->base_ts = 0;
634   videorate->out_frame_count = 0;
635   videorate->drop = 0;
636   videorate->dup = 0;
637   videorate->next_ts = GST_CLOCK_TIME_NONE;
638   videorate->last_ts = GST_CLOCK_TIME_NONE;
639   videorate->discont = TRUE;
640   videorate->average = 0;
641   videorate->force_variable_rate = FALSE;
642   if (!on_flush) {
643     /* Do not clear caps on flush events as those are still valid */
644     gst_clear_caps (&videorate->in_caps);
645   }
646   gst_video_rate_swap_prev (videorate, NULL, 0);
647
648   gst_segment_init (&videorate->segment, GST_FORMAT_TIME);
649 }
650
651 static void
652 gst_video_rate_init (GstVideoRate * videorate)
653 {
654   gst_video_rate_reset (videorate, FALSE);
655   videorate->silent = DEFAULT_SILENT;
656   videorate->new_pref = DEFAULT_NEW_PREF;
657   videorate->drop_only = DEFAULT_DROP_ONLY;
658   videorate->average_period = DEFAULT_AVERAGE_PERIOD;
659   videorate->average_period_set = DEFAULT_AVERAGE_PERIOD;
660   videorate->max_rate = DEFAULT_MAX_RATE;
661   videorate->rate = DEFAULT_RATE;
662   videorate->pending_rate = DEFAULT_RATE;
663   videorate->max_duplication_time = DEFAULT_MAX_DUPLICATION_TIME;
664   videorate->max_closing_segment_duplication_duration =
665       DEFAULT_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION;
666
667   videorate->from_rate_numerator = 0;
668   videorate->from_rate_denominator = 0;
669   videorate->to_rate_numerator = 0;
670   videorate->to_rate_denominator = 0;
671
672   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (videorate), TRUE);
673 }
674
675 /* @outbuf: (transfer full) needs to be writable */
676 static GstFlowReturn
677 gst_video_rate_push_buffer (GstVideoRate * videorate, GstBuffer * outbuf,
678     gboolean duplicate, GstClockTime next_intime, gboolean invalid_duration)
679 {
680   GstFlowReturn res;
681   GstClockTime push_ts;
682
683   GST_BUFFER_OFFSET (outbuf) = videorate->out;
684   GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
685
686   if (videorate->discont) {
687     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
688     videorate->discont = FALSE;
689   } else
690     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
691
692   if (duplicate)
693     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
694   else
695     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
696
697   /* this is the timestamp we put on the buffer */
698   push_ts = videorate->next_ts;
699
700   videorate->out++;
701   videorate->out_frame_count++;
702   if (videorate->segment.rate < 0.0) {
703     if (videorate->to_rate_numerator) {
704       /* interpolate next expected timestamp in the segment */
705       GstClockTimeDiff next_ts =
706           videorate->segment.base + videorate->segment.stop -
707           videorate->base_ts -
708           gst_util_uint64_scale (videorate->out_frame_count + 1,
709           videorate->to_rate_denominator * GST_SECOND,
710           videorate->to_rate_numerator);
711
712       videorate->next_ts = next_ts < 0 ? GST_CLOCK_TIME_NONE : next_ts;
713
714       GST_BUFFER_DURATION (outbuf) =
715           gst_util_uint64_scale (videorate->out_frame_count,
716           videorate->to_rate_denominator * GST_SECOND,
717           videorate->to_rate_numerator) -
718           gst_util_uint64_scale (videorate->out_frame_count - 1,
719           videorate->to_rate_denominator * GST_SECOND,
720           videorate->to_rate_numerator);
721     } else if (next_intime != GST_CLOCK_TIME_NONE) {
722       videorate->next_ts = next_intime;
723     } else {
724       GST_FIXME_OBJECT (videorate, "No next intime for reverse playback");
725     }
726   } else {
727     if (videorate->to_rate_numerator) {
728       /* interpolate next expected timestamp in the segment */
729       videorate->next_ts =
730           videorate->segment.base + videorate->segment.start +
731           videorate->base_ts +
732           gst_util_uint64_scale (videorate->out_frame_count,
733           videorate->to_rate_denominator * GST_SECOND,
734           videorate->to_rate_numerator);
735       GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
736     } else if (!invalid_duration) {
737       /* There must always be a valid duration on prevbuf if rate > 0,
738        * it is ensured in the transform_ip function */
739       g_assert (GST_BUFFER_PTS_IS_VALID (outbuf));
740       g_assert (GST_BUFFER_DURATION_IS_VALID (outbuf));
741       g_assert (GST_BUFFER_DURATION (outbuf) != 0);
742
743       videorate->next_ts
744           = GST_BUFFER_PTS (outbuf) + GST_BUFFER_DURATION (outbuf);
745     }
746   }
747
748   /* We do not need to update time in VFR (variable frame rate) mode */
749   if (!videorate->drop_only) {
750     /* adapt for looping, bring back to time in current segment. */
751     GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.base;
752   }
753
754   GST_LOG_OBJECT (videorate,
755       "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
756       GST_TIME_ARGS (push_ts));
757
758   res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (videorate), outbuf);
759
760   return res;
761 }
762
763 /* flush the oldest buffer */
764 static GstFlowReturn
765 gst_video_rate_flush_prev (GstVideoRate * videorate, gboolean duplicate,
766     GstClockTime next_intime, gboolean invalid_duration)
767 {
768   GstBuffer *outbuf;
769
770   if (!videorate->prevbuf)
771     goto eos_before_buffers;
772
773   outbuf = gst_buffer_ref (videorate->prevbuf);
774   /* make sure we can write to the metadata */
775   outbuf = gst_buffer_make_writable (outbuf);
776
777   return gst_video_rate_push_buffer (videorate, outbuf, duplicate, next_intime,
778       invalid_duration);
779
780   /* WARNINGS */
781 eos_before_buffers:
782   {
783     GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
784     return GST_FLOW_OK;
785   }
786 }
787
788 static void
789 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
790     gint64 time)
791 {
792   GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
793
794   gst_buffer_replace (&videorate->prevbuf, buffer);
795   /* Ensure that ->prev_caps always match ->prevbuf */
796   if (!buffer)
797     gst_caps_replace (&videorate->prev_caps, NULL);
798   else if (videorate->prev_caps != videorate->in_caps)
799     gst_caps_replace (&videorate->prev_caps, videorate->in_caps);
800
801   videorate->prev_ts = time;
802 }
803
804 static void
805 gst_video_rate_notify_drop (GstVideoRate * videorate)
806 {
807   g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
808 }
809
810 static void
811 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
812 {
813   g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
814 }
815
816 static gboolean
817 gst_video_rate_check_duplicate_to_close_segment (GstVideoRate * videorate,
818     GstClockTime last_input_ts, gboolean is_first)
819 {
820   GstClockTime next_stream_time = videorate->next_ts - videorate->segment.base;
821   GstClockTime max_closing_segment_duplication_duration =
822       videorate->max_closing_segment_duplication_duration;
823
824   if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts))
825     return FALSE;
826
827   if (videorate->segment.rate > 0.0) {
828
829     if (!GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
830       /* Ensure that if no 'stop' is set, we push the last frame anyway */
831       return is_first;
832     }
833
834     if (next_stream_time >= videorate->segment.stop)
835       return FALSE;
836
837     if (GST_CLOCK_TIME_IS_VALID (max_closing_segment_duplication_duration)) {
838       if (last_input_ts > videorate->next_ts)
839         return TRUE;
840
841       return (videorate->next_ts - last_input_ts <
842           max_closing_segment_duplication_duration);
843     }
844
845     return TRUE;
846   }
847
848   /* Reverse playback */
849
850   if (!GST_CLOCK_TIME_IS_VALID (videorate->segment.start)) {
851     /* Ensure that if no 'start' is set, we push the last frame anyway */
852     return is_first;
853   }
854
855   if (next_stream_time < videorate->segment.start)
856     return FALSE;
857
858   if (GST_CLOCK_TIME_IS_VALID (max_closing_segment_duplication_duration)) {
859     if (last_input_ts < videorate->next_ts)
860       return TRUE;
861
862     return (last_input_ts - videorate->next_ts <
863         max_closing_segment_duplication_duration);
864   }
865
866   return TRUE;
867 }
868
869 static gint
870 gst_video_rate_duplicate_to_close_segment (GstVideoRate * videorate)
871 {
872   gint count = 0;
873   GstFlowReturn res;
874   GstClockTime last_input_ts = videorate->prev_ts;
875
876   if (videorate->drop_only)
877     return count;
878
879   if (!videorate->prevbuf) {
880     GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
881
882     return count;
883   }
884
885   GST_DEBUG_OBJECT (videorate, "Pushing buffers to close segment");
886
887   res = GST_FLOW_OK;
888   /* fill up to the end of current segment */
889   while (res == GST_FLOW_OK
890       && gst_video_rate_check_duplicate_to_close_segment (videorate,
891           last_input_ts, count < 1)) {
892     res =
893         gst_video_rate_flush_prev (videorate, count > 0, GST_CLOCK_TIME_NONE,
894         FALSE);
895
896     count++;
897   }
898   GST_DEBUG_OBJECT (videorate, "----> Pushed %d buffers to close segment",
899       count);
900
901   return count;
902 }
903
904 /* WORKAROUND: This works around BaseTransform limitation as instead of rolling
905  * back caps, we should be able to push caps only when we are sure we are ready
906  * to do so. Right now, BaseTransform doesn't let us do anything like that
907  * so we rollback to previous caps when strictly required (though we now it
908  * might not be so safe).
909  *
910  * To be used only when wanting to 'close' a segment, this function will reset
911  * caps to previous caps, which will match the content of `prevbuf` in that case
912  *
913  * Returns: The previous GstCaps if we rolled back to previous buffers, NULL
914  * otherwise.
915  *
916  * NOTE: When some caps are returned, we should reset them back after
917  * closing the segment is done.
918  */
919 static GstCaps *
920 gst_video_rate_rollback_to_prev_caps_if_needed (GstVideoRate * videorate)
921 {
922   GstCaps *prev_caps = NULL;
923
924   if (videorate->prev_caps && videorate->prev_caps != videorate->in_caps) {
925     if (videorate->in_caps)
926       prev_caps = gst_caps_ref (videorate->in_caps);
927
928     if (!gst_pad_send_event (GST_BASE_TRANSFORM_SINK_PAD (videorate),
929             gst_event_new_caps (videorate->prev_caps)
930         )) {
931
932       GST_WARNING_OBJECT (videorate, "Could not send previous caps to close "
933           " segment, not closing it");
934
935       gst_video_rate_swap_prev (videorate, NULL, GST_CLOCK_TIME_NONE);
936       videorate->last_ts = GST_CLOCK_TIME_NONE;
937       videorate->average = 0;
938     }
939
940     gst_clear_caps (&videorate->prev_caps);
941   }
942
943   return prev_caps;
944 }
945
946 static gboolean
947 gst_video_rate_sink_event (GstBaseTransform * trans, GstEvent * event)
948 {
949   GstVideoRate *videorate;
950
951   videorate = GST_VIDEO_RATE (trans);
952
953   switch (GST_EVENT_TYPE (event)) {
954     case GST_EVENT_SEGMENT:
955     {
956       GstSegment segment;
957       gint seqnum;
958       GstCaps *rolled_back_caps;
959
960       gst_event_copy_segment (event, &segment);
961       if (segment.format != GST_FORMAT_TIME)
962         goto format_error;
963
964       rolled_back_caps =
965           gst_video_rate_rollback_to_prev_caps_if_needed (videorate);
966
967       /* close up the previous segment, if appropriate */
968       if (videorate->prevbuf) {
969         /* fill up to the end of current segment */
970         gint count = gst_video_rate_duplicate_to_close_segment (videorate);
971         if (count > 1) {
972           videorate->dup += count - 1;
973           if (!videorate->silent)
974             gst_video_rate_notify_duplicate (videorate);
975         }
976         /* clean up for the new one; _chain will resume from the new start */
977         gst_video_rate_swap_prev (videorate, NULL, 0);
978       }
979
980       if (rolled_back_caps) {
981         GST_DEBUG_OBJECT (videorate,
982             "Resetting rolled back caps %" GST_PTR_FORMAT, rolled_back_caps);
983         if (!gst_pad_send_event (GST_BASE_TRANSFORM_SINK_PAD (videorate),
984                 gst_event_new_caps (rolled_back_caps)
985             )) {
986
987           GST_WARNING_OBJECT (videorate, "Could not resend caps after closing "
988               " segment");
989
990           GST_ELEMENT_ERROR (videorate, CORE, NEGOTIATION,
991               ("Could not resend caps after closing segment"), (NULL));
992           gst_caps_unref (rolled_back_caps);
993
994           return FALSE;
995         }
996
997         gst_caps_unref (rolled_back_caps);
998       }
999
1000       videorate->base_ts = 0;
1001       videorate->out_frame_count = 0;
1002       videorate->next_ts = GST_CLOCK_TIME_NONE;
1003
1004       /* We just want to update the accumulated stream_time  */
1005
1006       segment.start = (gint64) (segment.start / videorate->rate);
1007       segment.position = (gint64) (segment.position / videorate->rate);
1008       if (GST_CLOCK_TIME_IS_VALID (segment.stop))
1009         segment.stop = (gint64) (segment.stop / videorate->rate);
1010       segment.time = (gint64) (segment.time / videorate->rate);
1011
1012       gst_segment_copy_into (&segment, &videorate->segment);
1013       GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
1014           &videorate->segment);
1015
1016
1017       seqnum = gst_event_get_seqnum (event);
1018       gst_event_unref (event);
1019       event = gst_event_new_segment (&segment);
1020       gst_event_set_seqnum (event, seqnum);
1021
1022       break;
1023     }
1024     case GST_EVENT_SEGMENT_DONE:
1025     case GST_EVENT_EOS:{
1026       gint count = 0;
1027       GstFlowReturn res = GST_FLOW_OK;
1028       GstCaps *rolled_back_caps;
1029
1030       GST_DEBUG_OBJECT (videorate, "Got %s",
1031           gst_event_type_get_name (GST_EVENT_TYPE (event)));
1032
1033       rolled_back_caps =
1034           gst_video_rate_rollback_to_prev_caps_if_needed (videorate);
1035
1036       /* If the segment has a stop position, fill the segment */
1037       if (GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
1038         /* fill up to the end of current segment */
1039         count = gst_video_rate_duplicate_to_close_segment (videorate);
1040       } else if (!videorate->drop_only && videorate->prevbuf) {
1041         /* Output at least one frame but if the buffer duration is valid, output
1042          * enough frames to use the complete buffer duration */
1043         if (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf)) {
1044           GstClockTime end_ts, duration =
1045               GST_BUFFER_DURATION (videorate->prevbuf);
1046
1047           if (GST_CLOCK_TIME_IS_VALID
1048               (videorate->max_closing_segment_duplication_duration))
1049             duration =
1050                 MIN (videorate->max_closing_segment_duplication_duration,
1051                 duration);
1052
1053           end_ts = videorate->next_ts + duration;
1054           while (res == GST_FLOW_OK && ((videorate->segment.rate > 0.0
1055                       && GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)
1056                       && GST_CLOCK_TIME_IS_VALID (videorate->next_ts)
1057                       && videorate->next_ts - videorate->segment.base < end_ts)
1058                   || count < 1)) {
1059             res =
1060                 gst_video_rate_flush_prev (videorate, count > 0,
1061                 GST_CLOCK_TIME_NONE, FALSE);
1062             count++;
1063           }
1064         } else {
1065           /* allow the duration to be invalid as there is no way to infer it if we
1066            * received a single buffer and not output framerate was set. */
1067           res =
1068               gst_video_rate_flush_prev (videorate, FALSE, GST_CLOCK_TIME_NONE,
1069               TRUE);
1070           count = 1;
1071         }
1072       }
1073
1074       if (rolled_back_caps) {
1075         GST_DEBUG_OBJECT (videorate,
1076             "Resetting rolled back caps %" GST_PTR_FORMAT, rolled_back_caps);
1077
1078         if (!gst_pad_send_event (GST_BASE_TRANSFORM_SINK_PAD (videorate),
1079                 gst_event_new_caps (rolled_back_caps)
1080             )) {
1081
1082           /* Not erroring out on EOS as it won't be too bad in any case */
1083           GST_WARNING_OBJECT (videorate, "Could not resend caps after closing "
1084               " segment on EOS (ignoring the error)");
1085         }
1086
1087         gst_caps_unref (rolled_back_caps);
1088       }
1089
1090       if (count > 1) {
1091         videorate->dup += count - 1;
1092         if (!videorate->silent)
1093           gst_video_rate_notify_duplicate (videorate);
1094       } else if (count == 0
1095           && !GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
1096         videorate->drop++;
1097         if (!videorate->silent)
1098           gst_video_rate_notify_drop (videorate);
1099       }
1100
1101       break;
1102     }
1103     case GST_EVENT_FLUSH_STOP:
1104       /* also resets the segment */
1105       GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
1106       gst_video_rate_reset (videorate, TRUE);
1107       break;
1108     case GST_EVENT_GAP:
1109       /* no gaps after videorate, ignore the event */
1110       gst_event_unref (event);
1111       return TRUE;
1112     default:
1113       break;
1114   }
1115
1116   return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
1117
1118   /* ERRORS */
1119 format_error:
1120   {
1121     GST_WARNING_OBJECT (videorate,
1122         "Got segment but doesn't have GST_FORMAT_TIME value");
1123     return FALSE;
1124   }
1125 }
1126
1127 static gboolean
1128 gst_video_rate_src_event (GstBaseTransform * trans, GstEvent * event)
1129 {
1130   GstVideoRate *videorate;
1131   GstPad *sinkpad;
1132   gboolean res = FALSE;
1133
1134   videorate = GST_VIDEO_RATE (trans);
1135   sinkpad = GST_BASE_TRANSFORM_SINK_PAD (trans);
1136   switch (GST_EVENT_TYPE (event)) {
1137     case GST_EVENT_SEEK:
1138     {
1139       gdouble srate;
1140       GstSeekFlags flags;
1141       GstSeekType start_type, stop_type;
1142       gint64 start, stop;
1143       gint seqnum = gst_event_get_seqnum (event);
1144
1145       gst_event_parse_seek (event, &srate, NULL, &flags, &start_type, &start,
1146           &stop_type, &stop);
1147
1148       start = (gint64) (start * videorate->rate);
1149       if (GST_CLOCK_TIME_IS_VALID (stop)) {
1150         stop = (gint64) (stop * videorate->rate);
1151       }
1152
1153       gst_event_unref (event);
1154       event = gst_event_new_seek (srate, GST_FORMAT_TIME,
1155           flags, start_type, start, stop_type, stop);
1156       gst_event_set_seqnum (event, seqnum);
1157
1158       res = gst_pad_push_event (sinkpad, event);
1159       break;
1160     }
1161     case GST_EVENT_QOS:
1162     {
1163       GstQOSType type;
1164       gdouble proportion;
1165       GstClockTimeDiff diff;
1166       GstClockTime timestamp;
1167
1168       gst_event_parse_qos (event, &type, &proportion, &diff, &timestamp);
1169
1170       if (GST_CLOCK_TIME_IS_VALID (timestamp) && videorate->rate != 1.0) {
1171         GST_OBJECT_LOCK (trans);
1172         GST_DEBUG_OBJECT (trans, "Rescaling QoS event taking our rate into"
1173             "account. Timestamp:  %" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT
1174             " - diff %" G_GINT64_FORMAT "-> %" G_GINT64_FORMAT,
1175             GST_TIME_ARGS (timestamp),
1176             GST_TIME_ARGS (videorate->base_ts + ((timestamp -
1177                         videorate->base_ts) * videorate->rate)), diff,
1178             (GstClockTimeDiff) (diff * videorate->rate));
1179
1180         if (videorate->segment.rate < 0.0)
1181           timestamp =
1182               (videorate->segment.stop - videorate->base_ts) -
1183               ((videorate->segment.stop - videorate->base_ts -
1184                   timestamp) * videorate->rate);
1185         else
1186           timestamp =
1187               videorate->base_ts + ((timestamp -
1188                   videorate->base_ts) * videorate->rate);
1189
1190         diff *= videorate->rate;
1191         GST_OBJECT_UNLOCK (trans);
1192
1193         gst_event_unref (event);
1194         event = gst_event_new_qos (type, proportion, diff, timestamp);
1195       }
1196       /* Fallthrough */
1197     }
1198     default:
1199       res = gst_pad_push_event (sinkpad, event);
1200       break;
1201   }
1202   return res;
1203 }
1204
1205 static gboolean
1206 gst_video_rate_query (GstBaseTransform * trans, GstPadDirection direction,
1207     GstQuery * query)
1208 {
1209   GstVideoRate *videorate = GST_VIDEO_RATE (trans);
1210   gboolean res = FALSE;
1211   GstPad *otherpad;
1212
1213   otherpad = (direction == GST_PAD_SRC) ?
1214       GST_BASE_TRANSFORM_SINK_PAD (trans) : GST_BASE_TRANSFORM_SRC_PAD (trans);
1215
1216   switch (GST_QUERY_TYPE (query)) {
1217     case GST_QUERY_LATENCY:
1218     {
1219       GstClockTime min, max;
1220       gboolean live;
1221       guint64 latency;
1222       guint64 avg_period;
1223       gboolean drop_only;
1224       GstPad *peer;
1225
1226       GST_OBJECT_LOCK (videorate);
1227       avg_period = videorate->average_period_set;
1228       drop_only = videorate->drop_only;
1229       GST_OBJECT_UNLOCK (videorate);
1230
1231       if (avg_period == 0 && (peer = gst_pad_get_peer (otherpad))) {
1232         if ((res = gst_pad_query (peer, query))) {
1233           gst_query_parse_latency (query, &live, &min, &max);
1234
1235           GST_DEBUG_OBJECT (videorate, "Peer latency: min %"
1236               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1237               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1238
1239           /* Drop only has no latency, other modes have one frame latency */
1240           if (!drop_only && videorate->from_rate_numerator != 0) {
1241             /* add latency. We don't really know since we hold on to the frames
1242              * until we get a next frame, which can be anything. We assume
1243              * however that this will take from_rate time. */
1244             latency = gst_util_uint64_scale (GST_SECOND,
1245                 videorate->from_rate_denominator,
1246                 videorate->from_rate_numerator);
1247           } else {
1248             /* no input framerate, we don't know */
1249             latency = 0;
1250           }
1251
1252           GST_DEBUG_OBJECT (videorate, "Our latency: %"
1253               GST_TIME_FORMAT, GST_TIME_ARGS (latency));
1254
1255           min += latency;
1256           if (max != -1)
1257             max += latency;
1258
1259           GST_DEBUG_OBJECT (videorate, "Calculated total latency : min %"
1260               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1261               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1262
1263           gst_query_set_latency (query, live, min, max);
1264         }
1265         gst_object_unref (peer);
1266         break;
1267       }
1268       /* Simple fall back if we don't have a latency or a peer that we
1269        * can ask about its latency yet.. */
1270       res =
1271           GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
1272           query);
1273       break;
1274     }
1275     case GST_QUERY_DURATION:
1276     {
1277       GstFormat format;
1278       gint64 duration;
1279       gdouble rate;
1280
1281       res =
1282           GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
1283           query);
1284
1285       if (!res)
1286         break;
1287
1288       GST_OBJECT_LOCK (videorate);
1289       rate = videorate->pending_rate;
1290       GST_OBJECT_UNLOCK (videorate);
1291
1292       if (rate == 1.0)
1293         break;
1294
1295       gst_query_parse_duration (query, &format, &duration);
1296
1297       if (format != GST_FORMAT_TIME) {
1298         GST_DEBUG_OBJECT (videorate, "not TIME format");
1299         break;
1300       }
1301       GST_LOG_OBJECT (videorate, "upstream duration: %" G_GINT64_FORMAT,
1302           duration);
1303       /* Shouldn't this be a multiplication if the direction is downstream? */
1304       if (GST_CLOCK_TIME_IS_VALID (duration)) {
1305         duration = (gint64) (duration / rate);
1306       }
1307       GST_LOG_OBJECT (videorate, "our duration: %" G_GINT64_FORMAT, duration);
1308       gst_query_set_duration (query, format, duration);
1309       break;
1310     }
1311     case GST_QUERY_POSITION:
1312     {
1313       GstFormat dst_format;
1314       gint64 dst_value;
1315       gdouble rate;
1316
1317       GST_OBJECT_LOCK (videorate);
1318       rate = videorate->rate;
1319       GST_OBJECT_UNLOCK (videorate);
1320
1321       gst_query_parse_position (query, &dst_format, NULL);
1322
1323       if (dst_format != GST_FORMAT_TIME) {
1324         GST_DEBUG_OBJECT (videorate, "not TIME format");
1325         break;
1326       }
1327       /* Shouldn't this be a multiplication if the direction is downstream? */
1328       dst_value =
1329           (gint64) (gst_segment_to_stream_time (&videorate->segment,
1330               GST_FORMAT_TIME, videorate->last_ts / rate));
1331       GST_LOG_OBJECT (videorate, "our position: %" GST_TIME_FORMAT,
1332           GST_TIME_ARGS (dst_value));
1333       gst_query_set_position (query, dst_format, dst_value);
1334       res = TRUE;
1335       break;
1336     }
1337     default:
1338       res =
1339           GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
1340           query);
1341       break;
1342   }
1343
1344   return res;
1345 }
1346
1347 static gboolean
1348 gst_video_rate_propose_allocation (GstBaseTransform * trans,
1349     GstQuery * decide_query, GstQuery * query)
1350 {
1351   GstBaseTransformClass *klass = GST_BASE_TRANSFORM_CLASS (parent_class);
1352   gboolean res;
1353
1354   /* We should always be passthrough */
1355   g_return_val_if_fail (decide_query == NULL, FALSE);
1356
1357   res = klass->propose_allocation (trans, NULL, query);
1358
1359   if (res) {
1360     guint i = 0;
1361     guint n_allocation;
1362     guint down_min = 0;
1363
1364     n_allocation = gst_query_get_n_allocation_pools (query);
1365
1366     while (i < n_allocation) {
1367       GstBufferPool *pool = NULL;
1368       guint size, min, max;
1369
1370       gst_query_parse_nth_allocation_pool (query, i, &pool, &size, &min, &max);
1371
1372       if (min == max) {
1373         if (pool)
1374           gst_object_unref (pool);
1375         gst_query_remove_nth_allocation_pool (query, i);
1376         n_allocation--;
1377         down_min = MAX (min, down_min);
1378         continue;
1379       }
1380
1381       gst_query_set_nth_allocation_pool (query, i, pool, size, min + 1, max);
1382       if (pool)
1383         gst_object_unref (pool);
1384       i++;
1385     }
1386
1387     if (n_allocation == 0) {
1388       GstCaps *caps;
1389       GstVideoInfo info;
1390
1391       gst_query_parse_allocation (query, &caps, NULL);
1392       gst_video_info_from_caps (&info, caps);
1393
1394       gst_query_add_allocation_pool (query, NULL, info.size, down_min + 1, 0);
1395     }
1396   }
1397
1398   return res;
1399 }
1400
1401 static GstFlowReturn
1402 gst_video_rate_trans_ip_max_avg (GstVideoRate * videorate, GstBuffer * buf)
1403 {
1404   GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
1405
1406   videorate->in++;
1407
1408   if (!GST_CLOCK_TIME_IS_VALID (ts) || videorate->wanted_diff == 0)
1409     goto push;
1410
1411   /* drop frames if they exceed our output rate */
1412   if (GST_CLOCK_TIME_IS_VALID (videorate->last_ts)) {
1413     GstClockTimeDiff diff =
1414         videorate->segment.rate <
1415         0 ? videorate->last_ts - ts : ts - videorate->last_ts;
1416
1417     /* Drop buffer if its early compared to the desired frame rate and
1418      * the current average is higher than the desired average
1419      */
1420     if (diff < videorate->wanted_diff &&
1421         videorate->average < videorate->wanted_diff)
1422       goto drop;
1423
1424     /* Update average */
1425     if (videorate->average) {
1426       GstClockTimeDiff wanted_diff;
1427
1428       if (G_LIKELY (videorate->average_period > videorate->wanted_diff))
1429         wanted_diff = videorate->wanted_diff;
1430       else
1431         wanted_diff = videorate->average_period * 10;
1432
1433       videorate->average =
1434           gst_util_uint64_scale_round (videorate->average,
1435           videorate->average_period - wanted_diff,
1436           videorate->average_period) +
1437           gst_util_uint64_scale_round (diff, wanted_diff,
1438           videorate->average_period);
1439     } else {
1440       videorate->average = diff;
1441     }
1442   }
1443
1444   videorate->last_ts = ts;
1445
1446 push:
1447   videorate->out++;
1448   return GST_FLOW_OK;
1449
1450 drop:
1451   if (!videorate->silent)
1452     gst_video_rate_notify_drop (videorate);
1453   return GST_BASE_TRANSFORM_FLOW_DROPPED;
1454 }
1455
1456 /* Check if downstream forces variable framerate (0/1) and if
1457  * it is the case, use variable framerate ourself
1458  * Otherwise compute the framerate from the 2 buffers that we
1459  * have already received and make use of it as wanted framerate
1460  */
1461 static void
1462 gst_video_rate_check_variable_rate (GstVideoRate * videorate,
1463     GstBuffer * buffer)
1464 {
1465   GstStructure *st;
1466   gint fps_d, fps_n;
1467   GstCaps *srcpadcaps, *tmpcaps, *downstream_caps;
1468   GstPad *pad = NULL;
1469
1470   srcpadcaps =
1471       gst_pad_get_current_caps (GST_BASE_TRANSFORM_SRC_PAD (videorate));
1472
1473   gst_video_guess_framerate (GST_BUFFER_PTS (buffer) -
1474       GST_BUFFER_PTS (videorate->prevbuf), &fps_n, &fps_d);
1475
1476   tmpcaps = gst_caps_copy (srcpadcaps);
1477   st = gst_caps_get_structure (tmpcaps, 0);
1478   gst_structure_set (st, "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
1479   gst_caps_unref (srcpadcaps);
1480
1481   pad = gst_pad_get_peer (GST_BASE_TRANSFORM_SRC_PAD (videorate));
1482   downstream_caps = gst_pad_query_caps (pad, NULL);
1483   if (pad && !gst_caps_can_intersect (tmpcaps, downstream_caps)) {
1484     videorate->force_variable_rate = TRUE;
1485     gst_caps_unref (downstream_caps);
1486     GST_DEBUG_OBJECT (videorate, "Downstream forces variable framerate"
1487         " respecting it");
1488
1489     goto done;
1490   }
1491   gst_caps_unref (downstream_caps);
1492
1493   videorate->to_rate_numerator = fps_n;
1494   videorate->to_rate_denominator = fps_d;
1495
1496   GST_INFO_OBJECT (videorate, "Computed framerate to %d/%d",
1497       videorate->to_rate_numerator, videorate->to_rate_denominator);
1498
1499   videorate->updating_caps = TRUE;
1500   gst_base_transform_update_src_caps (GST_BASE_TRANSFORM (videorate), tmpcaps);
1501
1502   /* also reconfigure sink so that buffer pool can be updated again */
1503   gst_base_transform_reconfigure_sink (GST_BASE_TRANSFORM (videorate));
1504
1505 done:
1506   gst_caps_unref (tmpcaps);
1507   if (pad)
1508     gst_object_unref (pad);
1509 }
1510
1511 static gboolean
1512 gst_video_rate_switch_mode_if_needed (GstVideoRate * videorate)
1513 {
1514   gboolean switch_mode;
1515   GstClockTime avg_period;
1516   gboolean skip = FALSE;
1517
1518   GST_OBJECT_LOCK (videorate);
1519   avg_period = videorate->average_period_set;
1520   GST_OBJECT_UNLOCK (videorate);
1521
1522   /* MT-safe switching between modes */
1523   if (G_LIKELY (avg_period == videorate->average_period))
1524     return skip;
1525
1526   switch_mode = (avg_period == 0 || videorate->average_period == 0);
1527
1528   if (!switch_mode)
1529     return skip;
1530
1531
1532   videorate->average_period = avg_period;
1533   videorate->last_ts = GST_CLOCK_TIME_NONE;
1534   if (avg_period) {
1535     /* enabling average mode */
1536     videorate->average = 0;
1537     /* make sure no cached buffers from regular mode are left */
1538     gst_video_rate_swap_prev (videorate, NULL, 0);
1539   } else {
1540     /* enable regular mode */
1541     videorate->next_ts = GST_CLOCK_TIME_NONE;
1542     skip = TRUE;
1543   }
1544
1545   /* max averaging mode has no latency, normal mode does */
1546   gst_element_post_message (GST_ELEMENT (videorate),
1547       gst_message_new_latency (GST_OBJECT (videorate)));
1548
1549   return skip;
1550 }
1551
1552 static gboolean
1553 gst_video_rate_do_max_duplicate (GstVideoRate * videorate, GstBuffer * buffer,
1554     GstClockTime intime, GstClockTime prevtime, gint * count)
1555 {
1556   if (videorate->max_duplication_time <= 0)
1557     return TRUE;
1558
1559   /* We already know that intime and prevtime are not out of order, based
1560    * on the previous condition. Using ABS in case rate < 0, in which case
1561    * the order is reversed. */
1562   if (ABS (GST_CLOCK_DIFF (intime, prevtime)) > videorate->max_duplication_time) {
1563     GST_DEBUG_OBJECT (videorate,
1564         "The new buffer (%" GST_TIME_FORMAT
1565         ") is further away from previous buffer (%" GST_TIME_FORMAT
1566         ") than max-duplication-time (%" GST_TIME_FORMAT ")",
1567         GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime),
1568         GST_TIME_ARGS (videorate->max_duplication_time));
1569     /* First send out enough buffers to actually reach the time of the
1570      * previous buffer */
1571     if (videorate->segment.rate < 0.0) {
1572       while (videorate->next_ts > prevtime) {
1573         gst_video_rate_flush_prev (videorate, *count > 0, GST_CLOCK_TIME_NONE,
1574             FALSE);
1575         *count += 1;
1576       }
1577     } else {
1578       while (videorate->next_ts <= prevtime) {
1579         gst_video_rate_flush_prev (videorate, *count > 0, GST_CLOCK_TIME_NONE,
1580             FALSE);
1581         *count += 1;
1582       }
1583     }
1584
1585     if (*count > 1) {
1586       videorate->dup += *count - 1;
1587       if (!videorate->silent)
1588         gst_video_rate_notify_duplicate (videorate);
1589     }
1590
1591     /* The gap between the two buffers is too large. Don't fill it, just
1592      * let a discont through */
1593     videorate->discont = TRUE;
1594
1595     if (videorate->segment.rate < 0.0) {
1596       videorate->base_ts -= prevtime - intime;
1597     } else {
1598       videorate->base_ts += intime - prevtime;
1599     }
1600     videorate->next_ts = intime;
1601     /* Swap in new buffer and get rid of old buffer so that starting with
1602      * the next input buffer we output from the new position */
1603     gst_video_rate_swap_prev (videorate, buffer, intime);
1604     return FALSE;
1605   }
1606
1607   return TRUE;
1608 }
1609
1610 static gboolean
1611 gst_video_rate_apply_pending_rate (GstVideoRate * videorate)
1612 {
1613   gboolean ret = FALSE;
1614
1615   GST_OBJECT_LOCK (videorate);
1616   if (videorate->pending_rate == videorate->rate)
1617     goto done;
1618
1619   ret = TRUE;
1620   videorate->base_ts += gst_util_uint64_scale (videorate->out_frame_count,
1621       videorate->to_rate_denominator * GST_SECOND,
1622       videorate->to_rate_numerator);
1623   videorate->rate = videorate->pending_rate;
1624   videorate->out_frame_count = 0;
1625
1626 done:
1627   GST_OBJECT_UNLOCK (videorate);
1628
1629   return ret;
1630 }
1631
1632 static GstFlowReturn
1633 gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
1634 {
1635   GstVideoRate *videorate;
1636   GstFlowReturn res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1637   GstClockTime intime, in_ts, in_dur, last_ts;
1638   gboolean skip;
1639
1640   videorate = GST_VIDEO_RATE (trans);
1641
1642   if (videorate->prev_caps != videorate->in_caps) {
1643     /* After caps where set we didn't reset the state so we could close
1644      * the segment from previous caps if necessary, we got a buffer after the
1645      * new caps so we can reset now */
1646     GST_DEBUG_OBJECT (videorate, "Clearing old buffers now that we had a buffer"
1647         " after receiving caps");
1648     gst_video_rate_swap_prev (videorate, NULL, GST_CLOCK_TIME_NONE);
1649     gst_clear_caps (&videorate->prev_caps);
1650     videorate->last_ts = GST_CLOCK_TIME_NONE;
1651     videorate->average = 0;
1652   }
1653
1654   /* make sure the denominators are not 0 */
1655   if (videorate->from_rate_denominator == 0 ||
1656       videorate->to_rate_denominator == 0)
1657     goto not_negotiated;
1658
1659   if (videorate->to_rate_numerator == 0 && videorate->prevbuf &&
1660       !videorate->force_variable_rate) {
1661     if (!GST_BUFFER_PTS_IS_VALID (buffer) ||
1662         !GST_BUFFER_PTS_IS_VALID (videorate->prevbuf)) {
1663       GST_ELEMENT_ERROR (videorate, STREAM, FAILED, (NULL),
1664           ("videorate requires a non-variable framerate on the output caps or the"
1665               " two first consecutive buffers to have valid timestamps to guess the"
1666               " framerate."));
1667       return GST_FLOW_ERROR;
1668     }
1669     gst_video_rate_check_variable_rate (videorate, buffer);
1670   }
1671
1672   skip = gst_video_rate_switch_mode_if_needed (videorate);
1673
1674   if (videorate->average_period > 0)
1675     return gst_video_rate_trans_ip_max_avg (videorate, buffer);
1676
1677   gst_video_rate_apply_pending_rate (videorate);
1678   in_ts = GST_BUFFER_TIMESTAMP (buffer);
1679   in_dur = GST_BUFFER_DURATION (buffer);
1680
1681   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (in_ts))) {
1682     /* For reverse playback, we need all input timestamps as we can't
1683      * guess from the previous buffers timestamp and duration */
1684     if (G_UNLIKELY (videorate->segment.rate < 0.0))
1685       goto invalid_buffer;
1686     in_ts = videorate->last_ts;
1687     if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (in_ts)))
1688       goto invalid_buffer;
1689   }
1690
1691   /* get the time of the next expected buffer timestamp, we use this when the
1692    * next buffer has -1 as a timestamp */
1693   last_ts = videorate->last_ts;
1694   videorate->last_ts = in_ts;
1695   if (GST_CLOCK_TIME_IS_VALID (in_dur) && videorate->segment.rate > 0.0)
1696     videorate->last_ts += in_dur;
1697
1698   GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
1699       GST_TIME_ARGS (in_ts));
1700
1701   /* the input time is the time in the segment + all previously accumulated
1702    * segments */
1703   intime = in_ts + videorate->segment.base;
1704
1705   /* we need to have two buffers to compare */
1706   if (videorate->prevbuf == NULL || videorate->drop_only) {
1707     /* We can calculate the duration of the buffer here if not given for
1708      * reverse playback. We need this later */
1709     if (videorate->segment.rate < 0.0 && !GST_BUFFER_DURATION_IS_VALID (buffer)) {
1710       /* As we require valid timestamps all the time for reverse playback, we either
1711        * have a valid last_ts or we're at the very first buffer. */
1712       if (!GST_CLOCK_TIME_IS_VALID (last_ts))
1713         GST_BUFFER_DURATION (buffer) = videorate->segment.stop - in_ts;
1714       else
1715         GST_BUFFER_DURATION (buffer) = last_ts - in_ts;
1716     }
1717
1718     gst_video_rate_swap_prev (videorate, buffer, intime);
1719     videorate->in++;
1720     if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts)) {
1721       /* new buffer, we expect to output a buffer that matches the first
1722        * timestamp in the segment */
1723       if (videorate->skip_to_first || skip) {
1724         videorate->next_ts = intime;
1725         if (videorate->segment.rate < 0.0) {
1726           videorate->base_ts = videorate->segment.stop - in_ts;
1727         } else {
1728           videorate->base_ts = in_ts - videorate->segment.start;
1729         }
1730         videorate->out_frame_count = 0;
1731       } else {
1732         if (videorate->segment.rate < 0.0) {
1733           if (videorate->to_rate_numerator) {
1734             GstClockTime frame_duration = gst_util_uint64_scale (1,
1735                 videorate->to_rate_denominator * GST_SECOND,
1736                 videorate->to_rate_numerator);
1737
1738             videorate->next_ts =
1739                 videorate->segment.stop + videorate->segment.base;
1740
1741             if (videorate->next_ts > frame_duration)
1742               videorate->next_ts =
1743                   MAX (videorate->segment.start,
1744                   videorate->next_ts - frame_duration);
1745             else
1746               videorate->next_ts = videorate->segment.start;
1747           } else {
1748             /* What else can we do? */
1749             videorate->next_ts = intime;
1750           }
1751         } else {
1752           videorate->next_ts =
1753               videorate->segment.start + videorate->segment.base;
1754         }
1755       }
1756     }
1757
1758     /* In drop-only mode we can already decide here if we should output the
1759      * current frame or drop it because it's coming earlier than our minimum
1760      * allowed frame period. This also keeps latency down to 0 frames
1761      */
1762     if (videorate->drop_only) {
1763       if ((videorate->segment.rate > 0.0 && intime >= videorate->next_ts) ||
1764           (videorate->segment.rate < 0.0 && intime <= videorate->next_ts)) {
1765         GstFlowReturn r;
1766
1767         /* The buffer received from basetransform is guaranteed to be writable.
1768          * It just needs to be reffed so the buffer won't be consumed once pushed and
1769          * GstBaseTransform can get its reference back. */
1770         if ((r = gst_video_rate_push_buffer (videorate,
1771                     gst_buffer_ref (buffer), FALSE,
1772                     GST_CLOCK_TIME_NONE, FALSE)) != GST_FLOW_OK) {
1773           res = r;
1774           goto done;
1775         }
1776       }
1777       /* No need to keep the buffer around for longer */
1778       gst_buffer_replace (&videorate->prevbuf, NULL);
1779     }
1780   } else {
1781     GstClockTime prevtime;
1782     gint count = 0;
1783     gint64 diff1 = 0, diff2 = 0;
1784
1785     prevtime = videorate->prev_ts;
1786
1787     GST_LOG_OBJECT (videorate,
1788         "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
1789         " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
1790         GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
1791
1792     videorate->in++;
1793
1794     /* drop new buffer if it's before previous one */
1795     if ((videorate->segment.rate > 0.0 && intime < prevtime) ||
1796         (videorate->segment.rate < 0.0 && intime > prevtime)) {
1797       GST_DEBUG_OBJECT (videorate,
1798           "The new buffer (%" GST_TIME_FORMAT
1799           ") is before the previous buffer (%"
1800           GST_TIME_FORMAT "). Dropping new buffer.",
1801           GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
1802       videorate->drop++;
1803       if (!videorate->silent)
1804         gst_video_rate_notify_drop (videorate);
1805       goto done;
1806     }
1807
1808     if (!gst_video_rate_do_max_duplicate (videorate, buffer, intime, prevtime,
1809             &count))
1810       goto done;
1811
1812     /* got 2 buffers, see which one is the best */
1813     do {
1814       GstClockTime next_ts;
1815
1816       if (gst_video_rate_apply_pending_rate (videorate))
1817         goto done;
1818
1819       if (videorate->segment.rate < 0.0) {
1820         /* Make sure that we have a duration for this buffer. The previous
1821          * buffer already has a duration given by either exactly this code,
1822          * or the code above for the very first buffer */
1823         g_assert (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf));
1824         if (!GST_BUFFER_DURATION_IS_VALID (buffer))
1825           GST_BUFFER_DURATION (buffer) =
1826               prevtime > intime ? prevtime - intime : 0;
1827       } else {
1828         /* Make sure that we have a duration for previous buffer */
1829         if (!GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf))
1830           GST_BUFFER_DURATION (videorate->prevbuf) =
1831               intime > prevtime ? intime - prevtime : 0;
1832       }
1833
1834 #ifndef ABSDIFF
1835 #define ABSDIFF(a, b) (((a) > (b)) ? (a) - (b) : (b) - (a))
1836 #endif
1837
1838       /* take absolute diffs */
1839       if (videorate->segment.rate < 0.0) {
1840         GstClockTime next_end_ts;
1841         GstClockTime prev_endtime;
1842         GstClockTime in_endtime, base_ts_in_segment;
1843
1844         next_ts = videorate->next_ts;
1845
1846         if (!GST_CLOCK_TIME_IS_VALID (next_ts)) {
1847           GST_DEBUG_OBJECT (videorate, "Already reached segment start,"
1848               "ignoring buffer");
1849           break;
1850         }
1851
1852         prev_endtime = prevtime + GST_BUFFER_DURATION (videorate->prevbuf);
1853         in_endtime = intime + GST_BUFFER_DURATION (buffer);
1854
1855         if (videorate->to_rate_numerator) {
1856           GstClockTime frame_duration = gst_util_uint64_scale (1,
1857               videorate->to_rate_denominator * GST_SECOND,
1858               videorate->to_rate_numerator);
1859           next_end_ts = next_ts + frame_duration;
1860         } else {
1861           next_end_ts = next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
1862         }
1863
1864         base_ts_in_segment = videorate->segment.stop - videorate->base_ts;
1865         next_ts = base_ts_in_segment - (
1866             (base_ts_in_segment - next_ts) * videorate->rate);
1867         next_end_ts = base_ts_in_segment - (MAX (0,
1868                 (base_ts_in_segment - next_end_ts)) * videorate->rate);
1869
1870         diff1 = ABSDIFF (prev_endtime, next_end_ts);
1871         diff2 = ABSDIFF (in_endtime, next_end_ts);
1872
1873         GST_LOG_OBJECT (videorate,
1874             "diff with prev %" GST_TIME_FORMAT " diff with new %"
1875             GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
1876             GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
1877             GST_TIME_ARGS (next_end_ts));
1878       } else {
1879         next_ts =
1880             videorate->base_ts + ((videorate->next_ts -
1881                 videorate->base_ts) * videorate->rate);
1882
1883         diff1 = ABSDIFF (prevtime, next_ts);
1884         diff2 = ABSDIFF (intime, next_ts);
1885
1886         GST_LOG_OBJECT (videorate,
1887             "diff with prev %" GST_TIME_FORMAT " diff with new %"
1888             GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
1889             GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
1890             GST_TIME_ARGS (next_ts));
1891       }
1892
1893       /* output first one when its the best */
1894       if (diff1 <= diff2) {
1895         GstFlowReturn r;
1896         count++;
1897
1898         /* on error the _flush function posted a warning already */
1899         if ((r = gst_video_rate_flush_prev (videorate,
1900                     count > 1, intime, FALSE)) != GST_FLOW_OK) {
1901           res = r;
1902           goto done;
1903         }
1904       }
1905
1906       /* continue while the first one was the best, if they were equal avoid
1907        * going into an infinite loop */
1908     }
1909     while (diff1 < diff2);
1910
1911     /* if we outputted the first buffer more then once, we have dups */
1912     if (count > 1) {
1913       videorate->dup += count - 1;
1914       if (!videorate->silent)
1915         gst_video_rate_notify_duplicate (videorate);
1916     }
1917     /* if we didn't output the first buffer, we have a drop */
1918     else if (count == 0) {
1919       videorate->drop++;
1920
1921       if (!videorate->silent)
1922         gst_video_rate_notify_drop (videorate);
1923
1924       GST_LOG_OBJECT (videorate,
1925           "new is best, old never used, drop, outgoing ts %"
1926           GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
1927     }
1928     GST_LOG_OBJECT (videorate,
1929         "END, putting new in old, diff1 %" GST_TIME_FORMAT
1930         ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
1931         ", in %" G_GUINT64_FORMAT ", out %" G_GUINT64_FORMAT ", drop %"
1932         G_GUINT64_FORMAT ", dup %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff1),
1933         GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
1934         videorate->in, videorate->out, videorate->drop, videorate->dup);
1935
1936     /* swap in new one when it's the best */
1937     gst_video_rate_swap_prev (videorate, buffer, intime);
1938   }
1939 done:
1940   return res;
1941
1942   /* ERRORS */
1943 not_negotiated:
1944   {
1945     GST_WARNING_OBJECT (videorate, "no framerate negotiated");
1946     res = GST_FLOW_NOT_NEGOTIATED;
1947     goto done;
1948   }
1949
1950 invalid_buffer:
1951   {
1952     GST_WARNING_OBJECT (videorate,
1953         "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
1954     res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1955     goto done;
1956   }
1957 }
1958
1959 static gboolean
1960 gst_video_rate_start (GstBaseTransform * trans)
1961 {
1962   gst_video_rate_reset (GST_VIDEO_RATE (trans), FALSE);
1963   return TRUE;
1964 }
1965
1966 static gboolean
1967 gst_video_rate_stop (GstBaseTransform * trans)
1968 {
1969   gst_video_rate_reset (GST_VIDEO_RATE (trans), FALSE);
1970   return TRUE;
1971 }
1972
1973 static void
1974 gst_videorate_update_duration (GstVideoRate * videorate)
1975 {
1976   GstMessage *m;
1977
1978   m = gst_message_new_duration_changed (GST_OBJECT (videorate));
1979   gst_element_post_message (GST_ELEMENT (videorate), m);
1980 }
1981
1982 static void
1983 gst_video_rate_set_property (GObject * object,
1984     guint prop_id, const GValue * value, GParamSpec * pspec)
1985 {
1986   GstVideoRate *videorate = GST_VIDEO_RATE (object);
1987   gboolean latency_changed = FALSE;
1988
1989   GST_OBJECT_LOCK (videorate);
1990   switch (prop_id) {
1991     case PROP_SILENT:
1992       videorate->silent = g_value_get_boolean (value);
1993       break;
1994     case PROP_NEW_PREF:
1995       videorate->new_pref = g_value_get_double (value);
1996       break;
1997     case PROP_SKIP_TO_FIRST:
1998       videorate->skip_to_first = g_value_get_boolean (value);
1999       break;
2000     case PROP_DROP_ONLY:{
2001       gboolean new_value = g_value_get_boolean (value);
2002
2003       /* Latency changes if we switch drop-only mode */
2004       latency_changed = new_value != videorate->drop_only;
2005       videorate->drop_only = g_value_get_boolean (value);
2006       goto reconfigure;
2007     }
2008     case PROP_AVERAGE_PERIOD:
2009       videorate->average_period_set = g_value_get_uint64 (value);
2010       break;
2011     case PROP_MAX_RATE:
2012       g_atomic_int_set (&videorate->max_rate, g_value_get_int (value));
2013       goto reconfigure;
2014     case PROP_RATE:
2015       videorate->pending_rate = g_value_get_double (value);
2016       GST_OBJECT_UNLOCK (videorate);
2017
2018       gst_videorate_update_duration (videorate);
2019       return;
2020     case PROP_MAX_DUPLICATION_TIME:
2021       videorate->max_duplication_time = g_value_get_uint64 (value);
2022       break;
2023     case PROP_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION:
2024       videorate->max_closing_segment_duplication_duration =
2025           g_value_get_uint64 (value);
2026       break;
2027     default:
2028       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2029       break;
2030   }
2031   GST_OBJECT_UNLOCK (videorate);
2032
2033   return;
2034
2035 reconfigure:
2036   GST_OBJECT_UNLOCK (videorate);
2037   gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (videorate));
2038
2039   if (latency_changed) {
2040     gst_element_post_message (GST_ELEMENT (videorate),
2041         gst_message_new_latency (GST_OBJECT (videorate)));
2042   }
2043 }
2044
2045 static void
2046 gst_video_rate_get_property (GObject * object,
2047     guint prop_id, GValue * value, GParamSpec * pspec)
2048 {
2049   GstVideoRate *videorate = GST_VIDEO_RATE (object);
2050
2051   GST_OBJECT_LOCK (videorate);
2052   switch (prop_id) {
2053     case PROP_IN:
2054       g_value_set_uint64 (value, videorate->in);
2055       break;
2056     case PROP_OUT:
2057       g_value_set_uint64 (value, videorate->out);
2058       break;
2059     case PROP_DUP:
2060       g_value_set_uint64 (value, videorate->dup);
2061       break;
2062     case PROP_DROP:
2063       g_value_set_uint64 (value, videorate->drop);
2064       break;
2065     case PROP_SILENT:
2066       g_value_set_boolean (value, videorate->silent);
2067       break;
2068     case PROP_NEW_PREF:
2069       g_value_set_double (value, videorate->new_pref);
2070       break;
2071     case PROP_SKIP_TO_FIRST:
2072       g_value_set_boolean (value, videorate->skip_to_first);
2073       break;
2074     case PROP_DROP_ONLY:
2075       g_value_set_boolean (value, videorate->drop_only);
2076       break;
2077     case PROP_AVERAGE_PERIOD:
2078       g_value_set_uint64 (value, videorate->average_period_set);
2079       break;
2080     case PROP_MAX_RATE:
2081       g_value_set_int (value, g_atomic_int_get (&videorate->max_rate));
2082       break;
2083     case PROP_RATE:
2084       g_value_set_double (value, videorate->pending_rate);
2085       break;
2086     case PROP_MAX_DUPLICATION_TIME:
2087       g_value_set_uint64 (value, videorate->max_duplication_time);
2088       break;
2089     case PROP_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION:
2090       g_value_set_uint64 (value,
2091           videorate->max_closing_segment_duplication_duration);
2092       break;
2093     default:
2094       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2095       break;
2096   }
2097   GST_OBJECT_UNLOCK (videorate);
2098 }
2099
2100 static gboolean
2101 plugin_init (GstPlugin * plugin)
2102 {
2103   GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
2104       "VideoRate stream fixer");
2105
2106   return GST_ELEMENT_REGISTER (videorate, plugin);
2107 }
2108
2109 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2110     GST_VERSION_MINOR,
2111     videorate,
2112     "Adjusts video frames",
2113     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)