2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
21 * SECTION:element-videorate
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.
27 * The correction is performed by dropping and duplicating frames, no fancy
28 * algorithm is used to interpolate frames (yet).
30 * By default the element will simply negotiate the same framerate on its
31 * source and sink pad.
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.
37 * A conversion to a specific framerate can be forced by using filtered caps on
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).
46 * An input stream that needs no adjustments will thus never have dropped or
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.
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.
60 * ## Example pipelines
62 * gst-launch-1.0 -v uridecodebin uri=file:///path/to/video.ogg ! videoconvert ! videoscale ! videorate ! video/x-raw,framerate=15/1 ! autovideosink
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.
67 * gst-launch-1.0 -v v4l2src ! videorate ! video/x-raw,framerate=25/2 ! theoraenc ! oggmux ! filesink location=recording.ogg
69 * Capture video from a V4L device, and adjust the stream to 12.5 fps before
70 * encoding to Ogg/Theora.
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
74 * Decode a video file and save a snapshot every 5 seconds as consecutively numbered jpeg file.
82 #include "gstvideorate.h"
83 #include <gst/video/video.h>
85 GST_DEBUG_CATEGORY_STATIC (video_rate_debug);
86 #define GST_CAT_DEFAULT video_rate_debug
88 /* GstVideoRate signals and args */
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
119 PROP_MAX_DUPLICATION_TIME,
120 PROP_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION
123 static GstStaticPadTemplate gst_video_rate_src_template =
124 GST_STATIC_PAD_TEMPLATE ("src",
127 GST_STATIC_CAPS ("video/x-raw(ANY);" "video/x-bayer(ANY);"
128 "image/jpeg(ANY);" "image/png(ANY)")
131 static GstStaticPadTemplate gst_video_rate_sink_template =
132 GST_STATIC_PAD_TEMPLATE ("sink",
135 GST_STATIC_CAPS ("video/x-raw(ANY);" "video/x-bayer(ANY);"
136 "image/jpeg(ANY);" "image/png(ANY)")
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,
143 static gboolean gst_video_rate_src_event (GstBaseTransform * trans,
145 static gboolean gst_video_rate_query (GstBaseTransform * trans,
146 GstPadDirection direction, GstQuery * query);
148 static gboolean gst_video_rate_setcaps (GstBaseTransform * trans,
149 GstCaps * in_caps, GstCaps * out_caps);
151 static GstCaps *gst_video_rate_transform_caps (GstBaseTransform * trans,
152 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
154 static GstCaps *gst_video_rate_fixate_caps (GstBaseTransform * trans,
155 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
157 static GstFlowReturn gst_video_rate_transform_ip (GstBaseTransform * trans,
160 static gboolean gst_video_rate_propose_allocation (GstBaseTransform * trans,
161 GstQuery * decide_query, GstQuery * query);
163 static gboolean gst_video_rate_start (GstBaseTransform * trans);
164 static gboolean gst_video_rate_stop (GstBaseTransform * trans);
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);
172 static GParamSpec *pspec_drop = NULL;
173 static GParamSpec *pspec_duplicate = NULL;
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);
181 gst_video_rate_class_init (GstVideoRateClass * klass)
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);
187 object_class->set_property = gst_video_rate_set_property;
188 object_class->get_property = gst_video_rate_get_property;
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);
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));
227 * GstVideoRate:skip-to-first:
229 * Don't produce buffers before the first one we receive.
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));
237 * GstVideoRate:drop-only:
239 * Only drop frames, no duplicates are produced.
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));
247 * GstVideoRate:average-period:
249 * Arrange for maximum framerate by dropping frames beyond a certain framerate,
250 * where the framerate is calculated using a moving average over the
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));
260 * GstVideoRate:max-rate:
262 * maximum framerate to pass through
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));
274 * Factor of speed for frame displaying
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));
285 * GstVideoRate:max-duplication-time:
287 * Duplicate frames only if the gap between two consecutive frames does not
288 * exceed this duration.
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));
301 * GstVideoRate:max-closing-segment-duplication-duration:
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
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));
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>");
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);
333 gst_value_fraction_get_extremes (const GValue * v,
334 gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
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;
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);
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;
358 n = gst_value_list_get_size (v);
362 for (i = 0; i < n; i++) {
363 const GValue *t = gst_value_list_get_value (v, i);
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) {
371 if (gst_util_fraction_compare (max_n, max_d, *max_num, *max_denom) > 0) {
377 g_warning ("Unknown type for framerate");
385 /* Clamp the framerate in a caps structure to be a smaller range then
386 * [1...max_rate], otherwise return false */
388 gst_video_max_rate_clamp_structure (GstStructure * s, gint maxrate,
389 gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
391 gboolean ret = FALSE;
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 */
399 GValue intersection = { 0, };
400 GValue clamp = { 0, };
401 gint tmp_num, tmp_denom;
403 g_value_init (&clamp, GST_TYPE_FRACTION_RANGE);
404 gst_value_set_fraction_range_full (&clamp, 0, 1, maxrate, 1);
406 v = gst_structure_get_value (s, "framerate");
407 ret = gst_value_intersect (&intersection, v, &clamp);
408 g_value_unset (&clamp);
413 gst_value_fraction_get_extremes (&intersection,
414 min_num, min_denom, max_num, max_denom);
416 gst_value_fraction_get_extremes (v,
417 &tmp_num, &tmp_denom, max_num, max_denom);
419 if (gst_util_fraction_compare (*max_num, *max_denom, maxrate, 1) > 0) {
424 gst_structure_take_value (s, "framerate", &intersection);
432 gst_video_rate_transform_caps (GstBaseTransform * trans,
433 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
435 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
437 GstStructure *s, *s1, *s2, *s3 = NULL;
438 int maxrate = g_atomic_int_get (&videorate->max_rate);
441 ret = gst_caps_new_empty ();
443 for (i = 0; i < gst_caps_get_size (caps); i++) {
444 s = gst_caps_get_structure (caps, i);
446 s1 = gst_structure_copy (s);
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);
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);
460 s2 = gst_structure_copy (s);
463 if (videorate->drop_only) {
464 gint min_num = 0, min_denom = 1;
465 gint max_num = G_MAXINT, max_denom = 1;
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)) {
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 :)
480 * In case [X..maxrate] == [X..maxint], skip as we'll set it later
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);
486 gst_structure_free (s1);
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);
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);
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);
507 } else if (direction == GST_PAD_SINK) {
508 gint min_num = 0, min_denom = 1;
509 gint max_num = G_MAXINT, max_denom = 1;
511 if (!gst_video_max_rate_clamp_structure (s1, maxrate,
512 &min_num, &min_denom, &max_num, &max_denom)) {
513 gst_structure_free (s1);
516 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
519 /* set the framerate as a range */
520 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
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)));
529 ret = gst_caps_merge_structure_full (ret, s3,
530 gst_caps_features_copy (gst_caps_get_features (caps, i)));
533 GstCaps *intersection;
536 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
537 gst_caps_unref (ret);
544 gst_video_rate_fixate_caps (GstBaseTransform * trans,
545 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
551 s = gst_caps_get_structure (caps, 0);
552 if (G_UNLIKELY (!gst_structure_get_fraction (s, "framerate", &num, &denom)))
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);
560 if ((par = gst_structure_get_value (s, "pixel-aspect-ratio")))
561 gst_structure_fixate_field_nearest_fraction (s, "pixel-aspect-ratio", 1, 1);
563 return gst_caps_fixate (othercaps);
567 gst_video_rate_setcaps (GstBaseTransform * trans, GstCaps * in_caps,
570 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
571 GstStructure *structure;
573 gint rate_numerator, rate_denominator;
575 GST_DEBUG_OBJECT (trans, "setcaps called in: %" GST_PTR_FORMAT
576 " out: %" GST_PTR_FORMAT, in_caps, out_caps);
578 structure = gst_caps_get_structure (in_caps, 0);
579 if (!gst_structure_get_fraction (structure, "framerate",
580 &rate_numerator, &rate_denominator))
583 videorate->from_rate_numerator = rate_numerator;
584 videorate->from_rate_denominator = rate_denominator;
586 structure = gst_caps_get_structure (out_caps, 0);
587 if (!gst_structure_get_fraction (structure, "framerate",
588 &rate_numerator, &rate_denominator))
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
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);
601 videorate->out_frame_count = 0;
602 videorate->to_rate_numerator = rate_numerator;
603 videorate->to_rate_denominator = rate_denominator;
606 videorate->wanted_diff = gst_util_uint64_scale_int (GST_SECOND,
607 rate_denominator, rate_numerator);
609 videorate->wanted_diff = 0;
613 gst_caps_replace (&videorate->in_caps, in_caps);
620 GST_DEBUG_OBJECT (videorate, "no framerate specified");
627 gst_video_rate_reset (GstVideoRate * videorate, gboolean on_flush)
629 GST_DEBUG_OBJECT (videorate, "resetting internal variables");
633 videorate->base_ts = 0;
634 videorate->out_frame_count = 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;
643 /* Do not clear caps on flush events as those are still valid */
644 gst_clear_caps (&videorate->in_caps);
646 gst_video_rate_swap_prev (videorate, NULL, 0);
648 gst_segment_init (&videorate->segment, GST_FORMAT_TIME);
652 gst_video_rate_init (GstVideoRate * videorate)
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;
667 videorate->from_rate_numerator = 0;
668 videorate->from_rate_denominator = 0;
669 videorate->to_rate_numerator = 0;
670 videorate->to_rate_denominator = 0;
672 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (videorate), TRUE);
675 /* @outbuf: (transfer full) needs to be writable */
677 gst_video_rate_push_buffer (GstVideoRate * videorate, GstBuffer * outbuf,
678 gboolean duplicate, GstClockTime next_intime, gboolean invalid_duration)
681 GstClockTime push_ts;
683 GST_BUFFER_OFFSET (outbuf) = videorate->out;
684 GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
686 if (videorate->discont) {
687 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
688 videorate->discont = FALSE;
690 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
693 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
695 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
697 /* this is the timestamp we put on the buffer */
698 push_ts = videorate->next_ts;
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 -
708 gst_util_uint64_scale (videorate->out_frame_count + 1,
709 videorate->to_rate_denominator * GST_SECOND,
710 videorate->to_rate_numerator);
712 videorate->next_ts = next_ts < 0 ? GST_CLOCK_TIME_NONE : next_ts;
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;
724 GST_FIXME_OBJECT (videorate, "No next intime for reverse playback");
727 if (videorate->to_rate_numerator) {
728 /* interpolate next expected timestamp in the segment */
730 videorate->segment.base + videorate->segment.start +
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);
744 = GST_BUFFER_PTS (outbuf) + GST_BUFFER_DURATION (outbuf);
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;
754 GST_LOG_OBJECT (videorate,
755 "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
756 GST_TIME_ARGS (push_ts));
758 res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (videorate), outbuf);
763 /* flush the oldest buffer */
765 gst_video_rate_flush_prev (GstVideoRate * videorate, gboolean duplicate,
766 GstClockTime next_intime, gboolean invalid_duration)
770 if (!videorate->prevbuf)
771 goto eos_before_buffers;
773 outbuf = gst_buffer_ref (videorate->prevbuf);
774 /* make sure we can write to the metadata */
775 outbuf = gst_buffer_make_writable (outbuf);
777 return gst_video_rate_push_buffer (videorate, outbuf, duplicate, next_intime,
783 GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
789 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
792 GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
794 gst_buffer_replace (&videorate->prevbuf, buffer);
795 /* Ensure that ->prev_caps always match ->prevbuf */
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);
801 videorate->prev_ts = time;
805 gst_video_rate_notify_drop (GstVideoRate * videorate)
807 g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
811 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
813 g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
817 gst_video_rate_check_duplicate_to_close_segment (GstVideoRate * videorate,
818 GstClockTime last_input_ts, gboolean is_first)
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;
824 if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts))
827 if (videorate->segment.rate > 0.0) {
829 if (!GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
830 /* Ensure that if no 'stop' is set, we push the last frame anyway */
834 if (next_stream_time >= videorate->segment.stop)
837 if (GST_CLOCK_TIME_IS_VALID (max_closing_segment_duplication_duration)) {
838 if (last_input_ts > videorate->next_ts)
841 return (videorate->next_ts - last_input_ts <
842 max_closing_segment_duplication_duration);
848 /* Reverse playback */
850 if (!GST_CLOCK_TIME_IS_VALID (videorate->segment.start)) {
851 /* Ensure that if no 'start' is set, we push the last frame anyway */
855 if (next_stream_time < videorate->segment.start)
858 if (GST_CLOCK_TIME_IS_VALID (max_closing_segment_duplication_duration)) {
859 if (last_input_ts < videorate->next_ts)
862 return (last_input_ts - videorate->next_ts <
863 max_closing_segment_duplication_duration);
870 gst_video_rate_duplicate_to_close_segment (GstVideoRate * videorate)
874 GstClockTime last_input_ts = videorate->prev_ts;
876 if (videorate->drop_only)
879 if (!videorate->prevbuf) {
880 GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
885 GST_DEBUG_OBJECT (videorate, "Pushing buffers to close segment");
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)) {
893 gst_video_rate_flush_prev (videorate, count > 0, GST_CLOCK_TIME_NONE,
898 GST_DEBUG_OBJECT (videorate, "----> Pushed %d buffers to close segment",
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).
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
913 * Returns: The previous GstCaps if we rolled back to previous buffers, NULL
916 * NOTE: When some caps are returned, we should reset them back after
917 * closing the segment is done.
920 gst_video_rate_rollback_to_prev_caps_if_needed (GstVideoRate * videorate)
922 GstCaps *prev_caps = NULL;
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);
928 if (!gst_pad_send_event (GST_BASE_TRANSFORM_SINK_PAD (videorate),
929 gst_event_new_caps (videorate->prev_caps)
932 GST_WARNING_OBJECT (videorate, "Could not send previous caps to close "
933 " segment, not closing it");
935 gst_video_rate_swap_prev (videorate, NULL, GST_CLOCK_TIME_NONE);
936 videorate->last_ts = GST_CLOCK_TIME_NONE;
937 videorate->average = 0;
940 gst_clear_caps (&videorate->prev_caps);
947 gst_video_rate_sink_event (GstBaseTransform * trans, GstEvent * event)
949 GstVideoRate *videorate;
951 videorate = GST_VIDEO_RATE (trans);
953 switch (GST_EVENT_TYPE (event)) {
954 case GST_EVENT_SEGMENT:
958 GstCaps *rolled_back_caps;
960 gst_event_copy_segment (event, &segment);
961 if (segment.format != GST_FORMAT_TIME)
965 gst_video_rate_rollback_to_prev_caps_if_needed (videorate);
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);
972 videorate->dup += count - 1;
973 if (!videorate->silent)
974 gst_video_rate_notify_duplicate (videorate);
976 /* clean up for the new one; _chain will resume from the new start */
977 gst_video_rate_swap_prev (videorate, NULL, 0);
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)
987 GST_WARNING_OBJECT (videorate, "Could not resend caps after closing "
990 GST_ELEMENT_ERROR (videorate, CORE, NEGOTIATION,
991 ("Could not resend caps after closing segment"), (NULL));
992 gst_caps_unref (rolled_back_caps);
997 gst_caps_unref (rolled_back_caps);
1000 videorate->base_ts = 0;
1001 videorate->out_frame_count = 0;
1002 videorate->next_ts = GST_CLOCK_TIME_NONE;
1004 /* We just want to update the accumulated stream_time */
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);
1012 gst_segment_copy_into (&segment, &videorate->segment);
1013 GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
1014 &videorate->segment);
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);
1024 case GST_EVENT_SEGMENT_DONE:
1025 case GST_EVENT_EOS:{
1027 GstFlowReturn res = GST_FLOW_OK;
1028 GstCaps *rolled_back_caps;
1030 GST_DEBUG_OBJECT (videorate, "Got %s",
1031 gst_event_type_get_name (GST_EVENT_TYPE (event)));
1034 gst_video_rate_rollback_to_prev_caps_if_needed (videorate);
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);
1047 if (GST_CLOCK_TIME_IS_VALID
1048 (videorate->max_closing_segment_duplication_duration))
1050 MIN (videorate->max_closing_segment_duplication_duration,
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)
1060 gst_video_rate_flush_prev (videorate, count > 0,
1061 GST_CLOCK_TIME_NONE, FALSE);
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. */
1068 gst_video_rate_flush_prev (videorate, FALSE, GST_CLOCK_TIME_NONE,
1074 if (rolled_back_caps) {
1075 GST_DEBUG_OBJECT (videorate,
1076 "Resetting rolled back caps %" GST_PTR_FORMAT, rolled_back_caps);
1078 if (!gst_pad_send_event (GST_BASE_TRANSFORM_SINK_PAD (videorate),
1079 gst_event_new_caps (rolled_back_caps)
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)");
1087 gst_caps_unref (rolled_back_caps);
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)) {
1097 if (!videorate->silent)
1098 gst_video_rate_notify_drop (videorate);
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);
1109 /* no gaps after videorate, ignore the event */
1110 gst_event_unref (event);
1116 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
1121 GST_WARNING_OBJECT (videorate,
1122 "Got segment but doesn't have GST_FORMAT_TIME value");
1128 gst_video_rate_src_event (GstBaseTransform * trans, GstEvent * event)
1130 GstVideoRate *videorate;
1132 gboolean res = FALSE;
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:
1141 GstSeekType start_type, stop_type;
1143 gint seqnum = gst_event_get_seqnum (event);
1145 gst_event_parse_seek (event, &srate, NULL, &flags, &start_type, &start,
1148 start = (gint64) (start * videorate->rate);
1149 if (GST_CLOCK_TIME_IS_VALID (stop)) {
1150 stop = (gint64) (stop * videorate->rate);
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);
1158 res = gst_pad_push_event (sinkpad, event);
1165 GstClockTimeDiff diff;
1166 GstClockTime timestamp;
1168 gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
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));
1180 if (videorate->segment.rate < 0.0)
1182 (videorate->segment.stop - videorate->base_ts) -
1183 ((videorate->segment.stop - videorate->base_ts -
1184 timestamp) * videorate->rate);
1187 videorate->base_ts + ((timestamp -
1188 videorate->base_ts) * videorate->rate);
1190 diff *= videorate->rate;
1191 GST_OBJECT_UNLOCK (trans);
1193 gst_event_unref (event);
1194 event = gst_event_new_qos (type, proportion, diff, timestamp);
1199 res = gst_pad_push_event (sinkpad, event);
1206 gst_video_rate_query (GstBaseTransform * trans, GstPadDirection direction,
1209 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
1210 gboolean res = FALSE;
1213 otherpad = (direction == GST_PAD_SRC) ?
1214 GST_BASE_TRANSFORM_SINK_PAD (trans) : GST_BASE_TRANSFORM_SRC_PAD (trans);
1216 switch (GST_QUERY_TYPE (query)) {
1217 case GST_QUERY_LATENCY:
1219 GstClockTime min, max;
1226 GST_OBJECT_LOCK (videorate);
1227 avg_period = videorate->average_period_set;
1228 drop_only = videorate->drop_only;
1229 GST_OBJECT_UNLOCK (videorate);
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);
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));
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);
1248 /* no input framerate, we don't know */
1252 GST_DEBUG_OBJECT (videorate, "Our latency: %"
1253 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
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));
1263 gst_query_set_latency (query, live, min, max);
1265 gst_object_unref (peer);
1268 /* Simple fall back if we don't have a latency or a peer that we
1269 * can ask about its latency yet.. */
1271 GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
1275 case GST_QUERY_DURATION:
1282 GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
1288 GST_OBJECT_LOCK (videorate);
1289 rate = videorate->pending_rate;
1290 GST_OBJECT_UNLOCK (videorate);
1295 gst_query_parse_duration (query, &format, &duration);
1297 if (format != GST_FORMAT_TIME) {
1298 GST_DEBUG_OBJECT (videorate, "not TIME format");
1301 GST_LOG_OBJECT (videorate, "upstream duration: %" G_GINT64_FORMAT,
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);
1307 GST_LOG_OBJECT (videorate, "our duration: %" G_GINT64_FORMAT, duration);
1308 gst_query_set_duration (query, format, duration);
1311 case GST_QUERY_POSITION:
1313 GstFormat dst_format;
1317 GST_OBJECT_LOCK (videorate);
1318 rate = videorate->rate;
1319 GST_OBJECT_UNLOCK (videorate);
1321 gst_query_parse_position (query, &dst_format, NULL);
1323 if (dst_format != GST_FORMAT_TIME) {
1324 GST_DEBUG_OBJECT (videorate, "not TIME format");
1327 /* Shouldn't this be a multiplication if the direction is downstream? */
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);
1339 GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
1348 gst_video_rate_propose_allocation (GstBaseTransform * trans,
1349 GstQuery * decide_query, GstQuery * query)
1351 GstBaseTransformClass *klass = GST_BASE_TRANSFORM_CLASS (parent_class);
1354 /* We should always be passthrough */
1355 g_return_val_if_fail (decide_query == NULL, FALSE);
1357 res = klass->propose_allocation (trans, NULL, query);
1364 n_allocation = gst_query_get_n_allocation_pools (query);
1366 while (i < n_allocation) {
1367 GstBufferPool *pool = NULL;
1368 guint size, min, max;
1370 gst_query_parse_nth_allocation_pool (query, i, &pool, &size, &min, &max);
1374 gst_object_unref (pool);
1375 gst_query_remove_nth_allocation_pool (query, i);
1377 down_min = MAX (min, down_min);
1381 gst_query_set_nth_allocation_pool (query, i, pool, size, min + 1, max);
1383 gst_object_unref (pool);
1387 if (n_allocation == 0) {
1391 gst_query_parse_allocation (query, &caps, NULL);
1392 gst_video_info_from_caps (&info, caps);
1394 gst_query_add_allocation_pool (query, NULL, info.size, down_min + 1, 0);
1401 static GstFlowReturn
1402 gst_video_rate_trans_ip_max_avg (GstVideoRate * videorate, GstBuffer * buf)
1404 GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
1408 if (!GST_CLOCK_TIME_IS_VALID (ts) || videorate->wanted_diff == 0)
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;
1417 /* Drop buffer if its early compared to the desired frame rate and
1418 * the current average is higher than the desired average
1420 if (diff < videorate->wanted_diff &&
1421 videorate->average < videorate->wanted_diff)
1424 /* Update average */
1425 if (videorate->average) {
1426 GstClockTimeDiff wanted_diff;
1428 if (G_LIKELY (videorate->average_period > videorate->wanted_diff))
1429 wanted_diff = videorate->wanted_diff;
1431 wanted_diff = videorate->average_period * 10;
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);
1440 videorate->average = diff;
1444 videorate->last_ts = ts;
1451 if (!videorate->silent)
1452 gst_video_rate_notify_drop (videorate);
1453 return GST_BASE_TRANSFORM_FLOW_DROPPED;
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
1462 gst_video_rate_check_variable_rate (GstVideoRate * videorate,
1467 GstCaps *srcpadcaps, *tmpcaps, *downstream_caps;
1471 gst_pad_get_current_caps (GST_BASE_TRANSFORM_SRC_PAD (videorate));
1473 gst_video_guess_framerate (GST_BUFFER_PTS (buffer) -
1474 GST_BUFFER_PTS (videorate->prevbuf), &fps_n, &fps_d);
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);
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"
1491 gst_caps_unref (downstream_caps);
1493 videorate->to_rate_numerator = fps_n;
1494 videorate->to_rate_denominator = fps_d;
1496 GST_INFO_OBJECT (videorate, "Computed framerate to %d/%d",
1497 videorate->to_rate_numerator, videorate->to_rate_denominator);
1499 videorate->updating_caps = TRUE;
1500 gst_base_transform_update_src_caps (GST_BASE_TRANSFORM (videorate), tmpcaps);
1502 /* also reconfigure sink so that buffer pool can be updated again */
1503 gst_base_transform_reconfigure_sink (GST_BASE_TRANSFORM (videorate));
1506 gst_caps_unref (tmpcaps);
1508 gst_object_unref (pad);
1512 gst_video_rate_switch_mode_if_needed (GstVideoRate * videorate)
1514 gboolean switch_mode;
1515 GstClockTime avg_period;
1516 gboolean skip = FALSE;
1518 GST_OBJECT_LOCK (videorate);
1519 avg_period = videorate->average_period_set;
1520 GST_OBJECT_UNLOCK (videorate);
1522 /* MT-safe switching between modes */
1523 if (G_LIKELY (avg_period == videorate->average_period))
1526 switch_mode = (avg_period == 0 || videorate->average_period == 0);
1532 videorate->average_period = avg_period;
1533 videorate->last_ts = GST_CLOCK_TIME_NONE;
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);
1540 /* enable regular mode */
1541 videorate->next_ts = GST_CLOCK_TIME_NONE;
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)));
1553 gst_video_rate_do_max_duplicate (GstVideoRate * videorate, GstBuffer * buffer,
1554 GstClockTime intime, GstClockTime prevtime, gint * count)
1556 if (videorate->max_duplication_time <= 0)
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,
1578 while (videorate->next_ts <= prevtime) {
1579 gst_video_rate_flush_prev (videorate, *count > 0, GST_CLOCK_TIME_NONE,
1586 videorate->dup += *count - 1;
1587 if (!videorate->silent)
1588 gst_video_rate_notify_duplicate (videorate);
1591 /* The gap between the two buffers is too large. Don't fill it, just
1592 * let a discont through */
1593 videorate->discont = TRUE;
1595 if (videorate->segment.rate < 0.0) {
1596 videorate->base_ts -= prevtime - intime;
1598 videorate->base_ts += intime - prevtime;
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);
1611 gst_video_rate_apply_pending_rate (GstVideoRate * videorate)
1613 gboolean ret = FALSE;
1615 GST_OBJECT_LOCK (videorate);
1616 if (videorate->pending_rate == videorate->rate)
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;
1627 GST_OBJECT_UNLOCK (videorate);
1632 static GstFlowReturn
1633 gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
1635 GstVideoRate *videorate;
1636 GstFlowReturn res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1637 GstClockTime intime, in_ts, in_dur, last_ts;
1640 videorate = GST_VIDEO_RATE (trans);
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;
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;
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"
1667 return GST_FLOW_ERROR;
1669 gst_video_rate_check_variable_rate (videorate, buffer);
1672 skip = gst_video_rate_switch_mode_if_needed (videorate);
1674 if (videorate->average_period > 0)
1675 return gst_video_rate_trans_ip_max_avg (videorate, buffer);
1677 gst_video_rate_apply_pending_rate (videorate);
1678 in_ts = GST_BUFFER_TIMESTAMP (buffer);
1679 in_dur = GST_BUFFER_DURATION (buffer);
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;
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;
1698 GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
1699 GST_TIME_ARGS (in_ts));
1701 /* the input time is the time in the segment + all previously accumulated
1703 intime = in_ts + videorate->segment.base;
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;
1715 GST_BUFFER_DURATION (buffer) = last_ts - in_ts;
1718 gst_video_rate_swap_prev (videorate, buffer, intime);
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;
1728 videorate->base_ts = in_ts - videorate->segment.start;
1730 videorate->out_frame_count = 0;
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);
1738 videorate->next_ts =
1739 videorate->segment.stop + videorate->segment.base;
1741 if (videorate->next_ts > frame_duration)
1742 videorate->next_ts =
1743 MAX (videorate->segment.start,
1744 videorate->next_ts - frame_duration);
1746 videorate->next_ts = videorate->segment.start;
1748 /* What else can we do? */
1749 videorate->next_ts = intime;
1752 videorate->next_ts =
1753 videorate->segment.start + videorate->segment.base;
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
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)) {
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) {
1777 /* No need to keep the buffer around for longer */
1778 gst_buffer_replace (&videorate->prevbuf, NULL);
1781 GstClockTime prevtime;
1783 gint64 diff1 = 0, diff2 = 0;
1785 prevtime = videorate->prev_ts;
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));
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));
1803 if (!videorate->silent)
1804 gst_video_rate_notify_drop (videorate);
1808 if (!gst_video_rate_do_max_duplicate (videorate, buffer, intime, prevtime,
1812 /* got 2 buffers, see which one is the best */
1814 GstClockTime next_ts;
1816 if (gst_video_rate_apply_pending_rate (videorate))
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;
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;
1835 #define ABSDIFF(a, b) (((a) > (b)) ? (a) - (b) : (b) - (a))
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;
1844 next_ts = videorate->next_ts;
1846 if (!GST_CLOCK_TIME_IS_VALID (next_ts)) {
1847 GST_DEBUG_OBJECT (videorate, "Already reached segment start,"
1852 prev_endtime = prevtime + GST_BUFFER_DURATION (videorate->prevbuf);
1853 in_endtime = intime + GST_BUFFER_DURATION (buffer);
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;
1861 next_end_ts = next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
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);
1870 diff1 = ABSDIFF (prev_endtime, next_end_ts);
1871 diff2 = ABSDIFF (in_endtime, next_end_ts);
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));
1880 videorate->base_ts + ((videorate->next_ts -
1881 videorate->base_ts) * videorate->rate);
1883 diff1 = ABSDIFF (prevtime, next_ts);
1884 diff2 = ABSDIFF (intime, next_ts);
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));
1893 /* output first one when its the best */
1894 if (diff1 <= diff2) {
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) {
1906 /* continue while the first one was the best, if they were equal avoid
1907 * going into an infinite loop */
1909 while (diff1 < diff2);
1911 /* if we outputted the first buffer more then once, we have dups */
1913 videorate->dup += count - 1;
1914 if (!videorate->silent)
1915 gst_video_rate_notify_duplicate (videorate);
1917 /* if we didn't output the first buffer, we have a drop */
1918 else if (count == 0) {
1921 if (!videorate->silent)
1922 gst_video_rate_notify_drop (videorate);
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));
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);
1936 /* swap in new one when it's the best */
1937 gst_video_rate_swap_prev (videorate, buffer, intime);
1945 GST_WARNING_OBJECT (videorate, "no framerate negotiated");
1946 res = GST_FLOW_NOT_NEGOTIATED;
1952 GST_WARNING_OBJECT (videorate,
1953 "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
1954 res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1960 gst_video_rate_start (GstBaseTransform * trans)
1962 gst_video_rate_reset (GST_VIDEO_RATE (trans), FALSE);
1967 gst_video_rate_stop (GstBaseTransform * trans)
1969 gst_video_rate_reset (GST_VIDEO_RATE (trans), FALSE);
1974 gst_videorate_update_duration (GstVideoRate * videorate)
1978 m = gst_message_new_duration_changed (GST_OBJECT (videorate));
1979 gst_element_post_message (GST_ELEMENT (videorate), m);
1983 gst_video_rate_set_property (GObject * object,
1984 guint prop_id, const GValue * value, GParamSpec * pspec)
1986 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1987 gboolean latency_changed = FALSE;
1989 GST_OBJECT_LOCK (videorate);
1992 videorate->silent = g_value_get_boolean (value);
1995 videorate->new_pref = g_value_get_double (value);
1997 case PROP_SKIP_TO_FIRST:
1998 videorate->skip_to_first = g_value_get_boolean (value);
2000 case PROP_DROP_ONLY:{
2001 gboolean new_value = g_value_get_boolean (value);
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);
2008 case PROP_AVERAGE_PERIOD:
2009 videorate->average_period_set = g_value_get_uint64 (value);
2012 g_atomic_int_set (&videorate->max_rate, g_value_get_int (value));
2015 videorate->pending_rate = g_value_get_double (value);
2016 GST_OBJECT_UNLOCK (videorate);
2018 gst_videorate_update_duration (videorate);
2020 case PROP_MAX_DUPLICATION_TIME:
2021 videorate->max_duplication_time = g_value_get_uint64 (value);
2023 case PROP_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION:
2024 videorate->max_closing_segment_duplication_duration =
2025 g_value_get_uint64 (value);
2028 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2031 GST_OBJECT_UNLOCK (videorate);
2036 GST_OBJECT_UNLOCK (videorate);
2037 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (videorate));
2039 if (latency_changed) {
2040 gst_element_post_message (GST_ELEMENT (videorate),
2041 gst_message_new_latency (GST_OBJECT (videorate)));
2046 gst_video_rate_get_property (GObject * object,
2047 guint prop_id, GValue * value, GParamSpec * pspec)
2049 GstVideoRate *videorate = GST_VIDEO_RATE (object);
2051 GST_OBJECT_LOCK (videorate);
2054 g_value_set_uint64 (value, videorate->in);
2057 g_value_set_uint64 (value, videorate->out);
2060 g_value_set_uint64 (value, videorate->dup);
2063 g_value_set_uint64 (value, videorate->drop);
2066 g_value_set_boolean (value, videorate->silent);
2069 g_value_set_double (value, videorate->new_pref);
2071 case PROP_SKIP_TO_FIRST:
2072 g_value_set_boolean (value, videorate->skip_to_first);
2074 case PROP_DROP_ONLY:
2075 g_value_set_boolean (value, videorate->drop_only);
2077 case PROP_AVERAGE_PERIOD:
2078 g_value_set_uint64 (value, videorate->average_period_set);
2081 g_value_set_int (value, g_atomic_int_get (&videorate->max_rate));
2084 g_value_set_double (value, videorate->pending_rate);
2086 case PROP_MAX_DUPLICATION_TIME:
2087 g_value_set_uint64 (value, videorate->max_duplication_time);
2089 case PROP_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION:
2090 g_value_set_uint64 (value,
2091 videorate->max_closing_segment_duplication_duration);
2094 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2097 GST_OBJECT_UNLOCK (videorate);
2101 plugin_init (GstPlugin * plugin)
2103 GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
2104 "VideoRate stream fixer");
2106 return GST_ELEMENT_REGISTER (videorate, plugin);
2109 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2112 "Adjusts video frames",
2113 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)