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;
612 /* After a setcaps, our caps may have changed. In that case, we can't use
613 * the old buffer, if there was one (it might have different dimensions) */
614 GST_DEBUG_OBJECT (videorate, "swapping old buffers");
615 gst_video_rate_swap_prev (videorate, NULL, GST_CLOCK_TIME_NONE);
616 videorate->last_ts = GST_CLOCK_TIME_NONE;
617 videorate->average = 0;
623 GST_DEBUG_OBJECT (videorate, "no framerate specified");
630 gst_video_rate_reset (GstVideoRate * videorate)
632 GST_DEBUG_OBJECT (videorate, "resetting internal variables");
636 videorate->base_ts = 0;
637 videorate->out_frame_count = 0;
640 videorate->next_ts = GST_CLOCK_TIME_NONE;
641 videorate->last_ts = GST_CLOCK_TIME_NONE;
642 videorate->discont = TRUE;
643 videorate->average = 0;
644 videorate->force_variable_rate = FALSE;
645 gst_video_rate_swap_prev (videorate, NULL, 0);
647 gst_segment_init (&videorate->segment, GST_FORMAT_TIME);
651 gst_video_rate_init (GstVideoRate * videorate)
653 gst_video_rate_reset (videorate);
654 videorate->silent = DEFAULT_SILENT;
655 videorate->new_pref = DEFAULT_NEW_PREF;
656 videorate->drop_only = DEFAULT_DROP_ONLY;
657 videorate->average_period = DEFAULT_AVERAGE_PERIOD;
658 videorate->average_period_set = DEFAULT_AVERAGE_PERIOD;
659 videorate->max_rate = DEFAULT_MAX_RATE;
660 videorate->rate = DEFAULT_RATE;
661 videorate->pending_rate = DEFAULT_RATE;
662 videorate->max_duplication_time = DEFAULT_MAX_DUPLICATION_TIME;
663 videorate->max_closing_segment_duplication_duration =
664 DEFAULT_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION;
666 videorate->from_rate_numerator = 0;
667 videorate->from_rate_denominator = 0;
668 videorate->to_rate_numerator = 0;
669 videorate->to_rate_denominator = 0;
671 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (videorate), TRUE);
674 /* @outbuf: (transfer full) needs to be writable */
676 gst_video_rate_push_buffer (GstVideoRate * videorate, GstBuffer * outbuf,
677 gboolean duplicate, GstClockTime next_intime, gboolean invalid_duration)
680 GstClockTime push_ts;
682 GST_BUFFER_OFFSET (outbuf) = videorate->out;
683 GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
685 if (videorate->discont) {
686 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
687 videorate->discont = FALSE;
689 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
692 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
694 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
696 /* this is the timestamp we put on the buffer */
697 push_ts = videorate->next_ts;
700 videorate->out_frame_count++;
701 if (videorate->segment.rate < 0.0) {
702 if (videorate->to_rate_numerator) {
703 /* interpolate next expected timestamp in the segment */
704 GstClockTimeDiff next_ts =
705 videorate->segment.base + videorate->segment.stop -
707 gst_util_uint64_scale (videorate->out_frame_count + 1,
708 videorate->to_rate_denominator * GST_SECOND,
709 videorate->to_rate_numerator);
711 videorate->next_ts = next_ts < 0 ? GST_CLOCK_TIME_NONE : next_ts;
713 GST_BUFFER_DURATION (outbuf) =
714 gst_util_uint64_scale (videorate->out_frame_count,
715 videorate->to_rate_denominator * GST_SECOND,
716 videorate->to_rate_numerator) -
717 gst_util_uint64_scale (videorate->out_frame_count - 1,
718 videorate->to_rate_denominator * GST_SECOND,
719 videorate->to_rate_numerator);
720 } else if (next_intime != GST_CLOCK_TIME_NONE) {
721 videorate->next_ts = next_intime;
723 GST_FIXME_OBJECT (videorate, "No next intime for reverse playback");
726 if (videorate->to_rate_numerator) {
727 /* interpolate next expected timestamp in the segment */
729 videorate->segment.base + videorate->segment.start +
731 gst_util_uint64_scale (videorate->out_frame_count,
732 videorate->to_rate_denominator * GST_SECOND,
733 videorate->to_rate_numerator);
734 GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
735 } else if (!invalid_duration) {
736 /* There must always be a valid duration on prevbuf if rate > 0,
737 * it is ensured in the transform_ip function */
738 g_assert (GST_BUFFER_PTS_IS_VALID (outbuf));
739 g_assert (GST_BUFFER_DURATION_IS_VALID (outbuf));
740 g_assert (GST_BUFFER_DURATION (outbuf) != 0);
743 = GST_BUFFER_PTS (outbuf) + GST_BUFFER_DURATION (outbuf);
747 /* We do not need to update time in VFR (variable frame rate) mode */
748 if (!videorate->drop_only) {
749 /* adapt for looping, bring back to time in current segment. */
750 GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.base;
753 GST_LOG_OBJECT (videorate,
754 "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
755 GST_TIME_ARGS (push_ts));
757 res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (videorate), outbuf);
762 /* flush the oldest buffer */
764 gst_video_rate_flush_prev (GstVideoRate * videorate, gboolean duplicate,
765 GstClockTime next_intime, gboolean invalid_duration)
769 if (!videorate->prevbuf)
770 goto eos_before_buffers;
772 outbuf = gst_buffer_ref (videorate->prevbuf);
773 /* make sure we can write to the metadata */
774 outbuf = gst_buffer_make_writable (outbuf);
776 return gst_video_rate_push_buffer (videorate, outbuf, duplicate, next_intime,
782 GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
788 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
791 GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
792 if (videorate->prevbuf)
793 gst_buffer_unref (videorate->prevbuf);
794 videorate->prevbuf = buffer != NULL ? gst_buffer_ref (buffer) : NULL;
795 videorate->prev_ts = time;
799 gst_video_rate_notify_drop (GstVideoRate * videorate)
801 g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
805 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
807 g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
811 gst_video_rate_check_duplicate_to_close_segment (GstVideoRate * videorate,
812 GstClockTime last_input_ts, gboolean is_first)
814 GstClockTime next_stream_time = videorate->next_ts - videorate->segment.base;
815 GstClockTime max_closing_segment_duplication_duration =
816 videorate->max_closing_segment_duplication_duration;
818 if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts))
821 if (videorate->segment.rate > 0.0) {
823 if (!GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
824 /* Ensure that if no 'stop' is set, we push the last frame anyway */
828 if (next_stream_time >= videorate->segment.stop)
831 if (GST_CLOCK_TIME_IS_VALID (max_closing_segment_duplication_duration)) {
832 if (last_input_ts > videorate->next_ts)
835 return (videorate->next_ts - last_input_ts <
836 max_closing_segment_duplication_duration);
842 /* Reverse playback */
844 if (!GST_CLOCK_TIME_IS_VALID (videorate->segment.start)) {
845 /* Ensure that if no 'start' is set, we push the last frame anyway */
849 if (next_stream_time < videorate->segment.start)
852 if (GST_CLOCK_TIME_IS_VALID (max_closing_segment_duplication_duration)) {
853 if (last_input_ts < videorate->next_ts)
856 return (last_input_ts - videorate->next_ts <
857 max_closing_segment_duplication_duration);
864 gst_video_rate_duplicate_to_close_segment (GstVideoRate * videorate)
868 GstClockTime last_input_ts = videorate->prev_ts;
870 if (videorate->drop_only)
873 if (!videorate->prevbuf) {
874 GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
880 /* fill up to the end of current segment */
881 while (res == GST_FLOW_OK
882 && gst_video_rate_check_duplicate_to_close_segment (videorate,
883 last_input_ts, count < 1)) {
885 gst_video_rate_flush_prev (videorate, count > 0, GST_CLOCK_TIME_NONE,
895 gst_video_rate_sink_event (GstBaseTransform * trans, GstEvent * event)
897 GstVideoRate *videorate;
899 videorate = GST_VIDEO_RATE (trans);
901 switch (GST_EVENT_TYPE (event)) {
902 case GST_EVENT_SEGMENT:
907 gst_event_copy_segment (event, &segment);
908 if (segment.format != GST_FORMAT_TIME)
911 GST_DEBUG_OBJECT (videorate, "handle NEWSEGMENT");
913 /* close up the previous segment, if appropriate */
914 if (videorate->prevbuf) {
915 /* fill up to the end of current segment */
916 gint count = gst_video_rate_duplicate_to_close_segment (videorate);
918 videorate->dup += count - 1;
919 if (!videorate->silent)
920 gst_video_rate_notify_duplicate (videorate);
922 /* clean up for the new one; _chain will resume from the new start */
923 gst_video_rate_swap_prev (videorate, NULL, 0);
926 videorate->base_ts = 0;
927 videorate->out_frame_count = 0;
928 videorate->next_ts = GST_CLOCK_TIME_NONE;
930 /* We just want to update the accumulated stream_time */
932 segment.start = (gint64) (segment.start / videorate->rate);
933 segment.position = (gint64) (segment.position / videorate->rate);
934 if (GST_CLOCK_TIME_IS_VALID (segment.stop))
935 segment.stop = (gint64) (segment.stop / videorate->rate);
936 segment.time = (gint64) (segment.time / videorate->rate);
938 gst_segment_copy_into (&segment, &videorate->segment);
939 GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
940 &videorate->segment);
943 seqnum = gst_event_get_seqnum (event);
944 gst_event_unref (event);
945 event = gst_event_new_segment (&segment);
946 gst_event_set_seqnum (event, seqnum);
950 case GST_EVENT_SEGMENT_DONE:
953 GstFlowReturn res = GST_FLOW_OK;
955 GST_DEBUG_OBJECT (videorate, "Got %s",
956 gst_event_type_get_name (GST_EVENT_TYPE (event)));
958 /* If the segment has a stop position, fill the segment */
959 if (GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
960 /* fill up to the end of current segment */
961 count = gst_video_rate_duplicate_to_close_segment (videorate);
962 } else if (!videorate->drop_only && videorate->prevbuf) {
963 /* Output at least one frame but if the buffer duration is valid, output
964 * enough frames to use the complete buffer duration */
965 if (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf)) {
966 GstClockTime end_ts, duration =
967 GST_BUFFER_DURATION (videorate->prevbuf);
969 if (GST_CLOCK_TIME_IS_VALID
970 (videorate->max_closing_segment_duplication_duration))
972 MIN (videorate->max_closing_segment_duplication_duration,
975 end_ts = videorate->next_ts + duration;
976 while (res == GST_FLOW_OK && ((videorate->segment.rate > 0.0
977 && GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)
978 && GST_CLOCK_TIME_IS_VALID (videorate->next_ts)
979 && videorate->next_ts - videorate->segment.base < end_ts)
982 gst_video_rate_flush_prev (videorate, count > 0,
983 GST_CLOCK_TIME_NONE, FALSE);
987 /* allow the duration to be invalid as there is no way to infer it if we
988 * received a single buffer and not output framerate was set. */
990 gst_video_rate_flush_prev (videorate, FALSE, GST_CLOCK_TIME_NONE,
997 videorate->dup += count - 1;
998 if (!videorate->silent)
999 gst_video_rate_notify_duplicate (videorate);
1000 } else if (count == 0
1001 && !GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
1003 if (!videorate->silent)
1004 gst_video_rate_notify_drop (videorate);
1009 case GST_EVENT_FLUSH_STOP:
1010 /* also resets the segment */
1011 GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
1012 gst_video_rate_reset (videorate);
1015 /* no gaps after videorate, ignore the event */
1016 gst_event_unref (event);
1022 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
1027 GST_WARNING_OBJECT (videorate,
1028 "Got segment but doesn't have GST_FORMAT_TIME value");
1034 gst_video_rate_src_event (GstBaseTransform * trans, GstEvent * event)
1036 GstVideoRate *videorate;
1038 gboolean res = FALSE;
1040 videorate = GST_VIDEO_RATE (trans);
1041 sinkpad = GST_BASE_TRANSFORM_SINK_PAD (trans);
1042 switch (GST_EVENT_TYPE (event)) {
1043 case GST_EVENT_SEEK:
1047 GstSeekType start_type, stop_type;
1049 gint seqnum = gst_event_get_seqnum (event);
1051 gst_event_parse_seek (event, &srate, NULL, &flags, &start_type, &start,
1054 start = (gint64) (start * videorate->rate);
1055 if (GST_CLOCK_TIME_IS_VALID (stop)) {
1056 stop = (gint64) (stop * videorate->rate);
1059 gst_event_unref (event);
1060 event = gst_event_new_seek (srate, GST_FORMAT_TIME,
1061 flags, start_type, start, stop_type, stop);
1062 gst_event_set_seqnum (event, seqnum);
1064 res = gst_pad_push_event (sinkpad, event);
1071 GstClockTimeDiff diff;
1072 GstClockTime timestamp;
1074 gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
1076 if (GST_CLOCK_TIME_IS_VALID (timestamp) && videorate->rate != 1.0) {
1077 GST_OBJECT_LOCK (trans);
1078 GST_DEBUG_OBJECT (trans, "Rescaling QoS event taking our rate into"
1079 "account. Timestamp: %" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT
1080 " - diff %" G_GINT64_FORMAT "-> %" G_GINT64_FORMAT,
1081 GST_TIME_ARGS (timestamp),
1082 GST_TIME_ARGS (videorate->base_ts + ((timestamp -
1083 videorate->base_ts) * videorate->rate)), diff,
1084 (GstClockTimeDiff) (diff * videorate->rate));
1086 if (videorate->segment.rate < 0.0)
1088 (videorate->segment.stop - videorate->base_ts) -
1089 ((videorate->segment.stop - videorate->base_ts -
1090 timestamp) * videorate->rate);
1093 videorate->base_ts + ((timestamp -
1094 videorate->base_ts) * videorate->rate);
1096 diff *= videorate->rate;
1097 GST_OBJECT_UNLOCK (trans);
1099 gst_event_unref (event);
1100 event = gst_event_new_qos (type, proportion, diff, timestamp);
1105 res = gst_pad_push_event (sinkpad, event);
1112 gst_video_rate_query (GstBaseTransform * trans, GstPadDirection direction,
1115 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
1116 gboolean res = FALSE;
1119 otherpad = (direction == GST_PAD_SRC) ?
1120 GST_BASE_TRANSFORM_SINK_PAD (trans) : GST_BASE_TRANSFORM_SRC_PAD (trans);
1122 switch (GST_QUERY_TYPE (query)) {
1123 case GST_QUERY_LATENCY:
1125 GstClockTime min, max;
1132 GST_OBJECT_LOCK (videorate);
1133 avg_period = videorate->average_period_set;
1134 drop_only = videorate->drop_only;
1135 GST_OBJECT_UNLOCK (videorate);
1137 if (avg_period == 0 && (peer = gst_pad_get_peer (otherpad))) {
1138 if ((res = gst_pad_query (peer, query))) {
1139 gst_query_parse_latency (query, &live, &min, &max);
1141 GST_DEBUG_OBJECT (videorate, "Peer latency: min %"
1142 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1143 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1145 /* Drop only has no latency, other modes have one frame latency */
1146 if (!drop_only && videorate->from_rate_numerator != 0) {
1147 /* add latency. We don't really know since we hold on to the frames
1148 * until we get a next frame, which can be anything. We assume
1149 * however that this will take from_rate time. */
1150 latency = gst_util_uint64_scale (GST_SECOND,
1151 videorate->from_rate_denominator,
1152 videorate->from_rate_numerator);
1154 /* no input framerate, we don't know */
1158 GST_DEBUG_OBJECT (videorate, "Our latency: %"
1159 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
1165 GST_DEBUG_OBJECT (videorate, "Calculated total latency : min %"
1166 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1167 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1169 gst_query_set_latency (query, live, min, max);
1171 gst_object_unref (peer);
1174 /* Simple fall back if we don't have a latency or a peer that we
1175 * can ask about its latency yet.. */
1177 GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
1181 case GST_QUERY_DURATION:
1188 GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
1194 GST_OBJECT_LOCK (videorate);
1195 rate = videorate->pending_rate;
1196 GST_OBJECT_UNLOCK (videorate);
1201 gst_query_parse_duration (query, &format, &duration);
1203 if (format != GST_FORMAT_TIME) {
1204 GST_DEBUG_OBJECT (videorate, "not TIME format");
1207 GST_LOG_OBJECT (videorate, "upstream duration: %" G_GINT64_FORMAT,
1209 /* Shouldn't this be a multiplication if the direction is downstream? */
1210 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1211 duration = (gint64) (duration / rate);
1213 GST_LOG_OBJECT (videorate, "our duration: %" G_GINT64_FORMAT, duration);
1214 gst_query_set_duration (query, format, duration);
1217 case GST_QUERY_POSITION:
1219 GstFormat dst_format;
1223 GST_OBJECT_LOCK (videorate);
1224 rate = videorate->rate;
1225 GST_OBJECT_UNLOCK (videorate);
1227 gst_query_parse_position (query, &dst_format, NULL);
1229 if (dst_format != GST_FORMAT_TIME) {
1230 GST_DEBUG_OBJECT (videorate, "not TIME format");
1233 /* Shouldn't this be a multiplication if the direction is downstream? */
1235 (gint64) (gst_segment_to_stream_time (&videorate->segment,
1236 GST_FORMAT_TIME, videorate->last_ts / rate));
1237 GST_LOG_OBJECT (videorate, "our position: %" GST_TIME_FORMAT,
1238 GST_TIME_ARGS (dst_value));
1239 gst_query_set_position (query, dst_format, dst_value);
1245 GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
1254 gst_video_rate_propose_allocation (GstBaseTransform * trans,
1255 GstQuery * decide_query, GstQuery * query)
1257 GstBaseTransformClass *klass = GST_BASE_TRANSFORM_CLASS (parent_class);
1260 /* We should always be passthrough */
1261 g_return_val_if_fail (decide_query == NULL, FALSE);
1263 res = klass->propose_allocation (trans, NULL, query);
1270 n_allocation = gst_query_get_n_allocation_pools (query);
1272 while (i < n_allocation) {
1273 GstBufferPool *pool = NULL;
1274 guint size, min, max;
1276 gst_query_parse_nth_allocation_pool (query, i, &pool, &size, &min, &max);
1280 gst_object_unref (pool);
1281 gst_query_remove_nth_allocation_pool (query, i);
1283 down_min = MAX (min, down_min);
1287 gst_query_set_nth_allocation_pool (query, i, pool, size, min + 1, max);
1289 gst_object_unref (pool);
1293 if (n_allocation == 0) {
1297 gst_query_parse_allocation (query, &caps, NULL);
1298 gst_video_info_from_caps (&info, caps);
1300 gst_query_add_allocation_pool (query, NULL, info.size, down_min + 1, 0);
1307 static GstFlowReturn
1308 gst_video_rate_trans_ip_max_avg (GstVideoRate * videorate, GstBuffer * buf)
1310 GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
1314 if (!GST_CLOCK_TIME_IS_VALID (ts) || videorate->wanted_diff == 0)
1317 /* drop frames if they exceed our output rate */
1318 if (GST_CLOCK_TIME_IS_VALID (videorate->last_ts)) {
1319 GstClockTimeDiff diff =
1320 videorate->segment.rate <
1321 0 ? videorate->last_ts - ts : ts - videorate->last_ts;
1323 /* Drop buffer if its early compared to the desired frame rate and
1324 * the current average is higher than the desired average
1326 if (diff < videorate->wanted_diff &&
1327 videorate->average < videorate->wanted_diff)
1330 /* Update average */
1331 if (videorate->average) {
1332 GstClockTimeDiff wanted_diff;
1334 if (G_LIKELY (videorate->average_period > videorate->wanted_diff))
1335 wanted_diff = videorate->wanted_diff;
1337 wanted_diff = videorate->average_period * 10;
1339 videorate->average =
1340 gst_util_uint64_scale_round (videorate->average,
1341 videorate->average_period - wanted_diff,
1342 videorate->average_period) +
1343 gst_util_uint64_scale_round (diff, wanted_diff,
1344 videorate->average_period);
1346 videorate->average = diff;
1350 videorate->last_ts = ts;
1357 if (!videorate->silent)
1358 gst_video_rate_notify_drop (videorate);
1359 return GST_BASE_TRANSFORM_FLOW_DROPPED;
1362 /* Check if downstream forces variable framerate (0/1) and if
1363 * it is the case, use variable framerate ourself
1364 * Otherwise compute the framerate from the 2 buffers that we
1365 * have already received and make use of it as wanted framerate
1368 gst_video_rate_check_variable_rate (GstVideoRate * videorate,
1373 GstCaps *srcpadcaps, *tmpcaps, *downstream_caps;
1377 gst_pad_get_current_caps (GST_BASE_TRANSFORM_SRC_PAD (videorate));
1379 gst_video_guess_framerate (GST_BUFFER_PTS (buffer) -
1380 GST_BUFFER_PTS (videorate->prevbuf), &fps_n, &fps_d);
1382 tmpcaps = gst_caps_copy (srcpadcaps);
1383 st = gst_caps_get_structure (tmpcaps, 0);
1384 gst_structure_set (st, "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
1385 gst_caps_unref (srcpadcaps);
1387 pad = gst_pad_get_peer (GST_BASE_TRANSFORM_SRC_PAD (videorate));
1388 downstream_caps = gst_pad_query_caps (pad, NULL);
1389 if (pad && !gst_caps_can_intersect (tmpcaps, downstream_caps)) {
1390 videorate->force_variable_rate = TRUE;
1391 gst_caps_unref (downstream_caps);
1392 GST_DEBUG_OBJECT (videorate, "Downstream forces variable framerate"
1397 gst_caps_unref (downstream_caps);
1399 videorate->to_rate_numerator = fps_n;
1400 videorate->to_rate_denominator = fps_d;
1402 GST_INFO_OBJECT (videorate, "Computed framerate to %d/%d",
1403 videorate->to_rate_numerator, videorate->to_rate_denominator);
1405 videorate->updating_caps = TRUE;
1406 gst_base_transform_update_src_caps (GST_BASE_TRANSFORM (videorate), tmpcaps);
1408 /* also reconfigure sink so that buffer pool can be updated again */
1409 gst_base_transform_reconfigure_sink (GST_BASE_TRANSFORM (videorate));
1412 gst_caps_unref (tmpcaps);
1414 gst_object_unref (pad);
1418 gst_video_rate_switch_mode_if_needed (GstVideoRate * videorate)
1420 gboolean switch_mode;
1421 GstClockTime avg_period;
1422 gboolean skip = FALSE;
1424 GST_OBJECT_LOCK (videorate);
1425 avg_period = videorate->average_period_set;
1426 GST_OBJECT_UNLOCK (videorate);
1428 /* MT-safe switching between modes */
1429 if (G_LIKELY (avg_period == videorate->average_period))
1432 switch_mode = (avg_period == 0 || videorate->average_period == 0);
1438 videorate->average_period = avg_period;
1439 videorate->last_ts = GST_CLOCK_TIME_NONE;
1441 /* enabling average mode */
1442 videorate->average = 0;
1443 /* make sure no cached buffers from regular mode are left */
1444 gst_video_rate_swap_prev (videorate, NULL, 0);
1446 /* enable regular mode */
1447 videorate->next_ts = GST_CLOCK_TIME_NONE;
1451 /* max averaging mode has no latency, normal mode does */
1452 gst_element_post_message (GST_ELEMENT (videorate),
1453 gst_message_new_latency (GST_OBJECT (videorate)));
1459 gst_video_rate_do_max_duplicate (GstVideoRate * videorate, GstBuffer * buffer,
1460 GstClockTime intime, GstClockTime prevtime, gint * count)
1462 if (videorate->max_duplication_time <= 0)
1465 /* We already know that intime and prevtime are not out of order, based
1466 * on the previous condition. Using ABS in case rate < 0, in which case
1467 * the order is reversed. */
1468 if (ABS (GST_CLOCK_DIFF (intime, prevtime)) > videorate->max_duplication_time) {
1469 GST_DEBUG_OBJECT (videorate,
1470 "The new buffer (%" GST_TIME_FORMAT
1471 ") is further away from previous buffer (%" GST_TIME_FORMAT
1472 ") than max-duplication-time (%" GST_TIME_FORMAT ")",
1473 GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime),
1474 GST_TIME_ARGS (videorate->max_duplication_time));
1475 /* First send out enough buffers to actually reach the time of the
1476 * previous buffer */
1477 if (videorate->segment.rate < 0.0) {
1478 while (videorate->next_ts > prevtime) {
1479 gst_video_rate_flush_prev (videorate, *count > 0, GST_CLOCK_TIME_NONE,
1484 while (videorate->next_ts <= prevtime) {
1485 gst_video_rate_flush_prev (videorate, *count > 0, GST_CLOCK_TIME_NONE,
1492 videorate->dup += *count - 1;
1493 if (!videorate->silent)
1494 gst_video_rate_notify_duplicate (videorate);
1497 /* The gap between the two buffers is too large. Don't fill it, just
1498 * let a discont through */
1499 videorate->discont = TRUE;
1501 if (videorate->segment.rate < 0.0) {
1502 videorate->base_ts -= prevtime - intime;
1504 videorate->base_ts += intime - prevtime;
1506 videorate->next_ts = intime;
1507 /* Swap in new buffer and get rid of old buffer so that starting with
1508 * the next input buffer we output from the new position */
1509 gst_video_rate_swap_prev (videorate, buffer, intime);
1517 gst_video_rate_apply_pending_rate (GstVideoRate * videorate)
1519 gboolean ret = FALSE;
1521 GST_OBJECT_LOCK (videorate);
1522 if (videorate->pending_rate == videorate->rate)
1526 videorate->base_ts += gst_util_uint64_scale (videorate->out_frame_count,
1527 videorate->to_rate_denominator * GST_SECOND,
1528 videorate->to_rate_numerator);
1529 videorate->rate = videorate->pending_rate;
1530 videorate->out_frame_count = 0;
1533 GST_OBJECT_UNLOCK (videorate);
1538 static GstFlowReturn
1539 gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
1541 GstVideoRate *videorate;
1542 GstFlowReturn res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1543 GstClockTime intime, in_ts, in_dur, last_ts;
1546 videorate = GST_VIDEO_RATE (trans);
1548 /* make sure the denominators are not 0 */
1549 if (videorate->from_rate_denominator == 0 ||
1550 videorate->to_rate_denominator == 0)
1551 goto not_negotiated;
1553 if (videorate->to_rate_numerator == 0 && videorate->prevbuf &&
1554 !videorate->force_variable_rate) {
1555 if (!GST_BUFFER_PTS_IS_VALID (buffer) ||
1556 !GST_BUFFER_PTS_IS_VALID (videorate->prevbuf)) {
1557 GST_ELEMENT_ERROR (videorate, STREAM, FAILED, (NULL),
1558 ("videorate requires a non-variable framerate on the output caps or the"
1559 " two first consecutive buffers to have valid timestamps to guess the"
1561 return GST_FLOW_ERROR;
1563 gst_video_rate_check_variable_rate (videorate, buffer);
1566 skip = gst_video_rate_switch_mode_if_needed (videorate);
1568 if (videorate->average_period > 0)
1569 return gst_video_rate_trans_ip_max_avg (videorate, buffer);
1571 gst_video_rate_apply_pending_rate (videorate);
1572 in_ts = GST_BUFFER_TIMESTAMP (buffer);
1573 in_dur = GST_BUFFER_DURATION (buffer);
1575 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (in_ts))) {
1576 /* For reverse playback, we need all input timestamps as we can't
1577 * guess from the previous buffers timestamp and duration */
1578 if (G_UNLIKELY (videorate->segment.rate < 0.0))
1579 goto invalid_buffer;
1580 in_ts = videorate->last_ts;
1581 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (in_ts)))
1582 goto invalid_buffer;
1585 /* get the time of the next expected buffer timestamp, we use this when the
1586 * next buffer has -1 as a timestamp */
1587 last_ts = videorate->last_ts;
1588 videorate->last_ts = in_ts;
1589 if (GST_CLOCK_TIME_IS_VALID (in_dur) && videorate->segment.rate > 0.0)
1590 videorate->last_ts += in_dur;
1592 GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
1593 GST_TIME_ARGS (in_ts));
1595 /* the input time is the time in the segment + all previously accumulated
1597 intime = in_ts + videorate->segment.base;
1599 /* we need to have two buffers to compare */
1600 if (videorate->prevbuf == NULL || videorate->drop_only) {
1601 /* We can calculate the duration of the buffer here if not given for
1602 * reverse playback. We need this later */
1603 if (videorate->segment.rate < 0.0 && !GST_BUFFER_DURATION_IS_VALID (buffer)) {
1604 /* As we require valid timestamps all the time for reverse playback, we either
1605 * have a valid last_ts or we're at the very first buffer. */
1606 if (!GST_CLOCK_TIME_IS_VALID (last_ts))
1607 GST_BUFFER_DURATION (buffer) = videorate->segment.stop - in_ts;
1609 GST_BUFFER_DURATION (buffer) = last_ts - in_ts;
1612 gst_video_rate_swap_prev (videorate, buffer, intime);
1614 if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts)) {
1615 /* new buffer, we expect to output a buffer that matches the first
1616 * timestamp in the segment */
1617 if (videorate->skip_to_first || skip) {
1618 videorate->next_ts = intime;
1619 if (videorate->segment.rate < 0.0) {
1620 videorate->base_ts = videorate->segment.stop - in_ts;
1622 videorate->base_ts = in_ts - videorate->segment.start;
1624 videorate->out_frame_count = 0;
1626 if (videorate->segment.rate < 0.0) {
1627 if (videorate->to_rate_numerator) {
1628 GstClockTime frame_duration = gst_util_uint64_scale (1,
1629 videorate->to_rate_denominator * GST_SECOND,
1630 videorate->to_rate_numerator);
1632 videorate->next_ts =
1633 videorate->segment.stop + videorate->segment.base;
1635 if (videorate->next_ts > frame_duration)
1636 videorate->next_ts =
1637 MAX (videorate->segment.start,
1638 videorate->next_ts - frame_duration);
1640 videorate->next_ts = videorate->segment.start;
1642 /* What else can we do? */
1643 videorate->next_ts = intime;
1646 videorate->next_ts =
1647 videorate->segment.start + videorate->segment.base;
1652 /* In drop-only mode we can already decide here if we should output the
1653 * current frame or drop it because it's coming earlier than our minimum
1654 * allowed frame period. This also keeps latency down to 0 frames
1656 if (videorate->drop_only) {
1657 if ((videorate->segment.rate > 0.0 && intime >= videorate->next_ts) ||
1658 (videorate->segment.rate < 0.0 && intime <= videorate->next_ts)) {
1661 /* The buffer received from basetransform is guaranteed to be writable.
1662 * It just needs to be reffed so the buffer won't be consumed once pushed and
1663 * GstBaseTransform can get its reference back. */
1664 if ((r = gst_video_rate_push_buffer (videorate,
1665 gst_buffer_ref (buffer), FALSE,
1666 GST_CLOCK_TIME_NONE, FALSE)) != GST_FLOW_OK) {
1671 /* No need to keep the buffer around for longer */
1672 gst_buffer_replace (&videorate->prevbuf, NULL);
1675 GstClockTime prevtime;
1677 gint64 diff1 = 0, diff2 = 0;
1679 prevtime = videorate->prev_ts;
1681 GST_LOG_OBJECT (videorate,
1682 "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
1683 " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
1684 GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
1688 /* drop new buffer if it's before previous one */
1689 if ((videorate->segment.rate > 0.0 && intime < prevtime) ||
1690 (videorate->segment.rate < 0.0 && intime > prevtime)) {
1691 GST_DEBUG_OBJECT (videorate,
1692 "The new buffer (%" GST_TIME_FORMAT
1693 ") is before the previous buffer (%"
1694 GST_TIME_FORMAT "). Dropping new buffer.",
1695 GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
1697 if (!videorate->silent)
1698 gst_video_rate_notify_drop (videorate);
1702 if (!gst_video_rate_do_max_duplicate (videorate, buffer, intime, prevtime,
1706 /* got 2 buffers, see which one is the best */
1708 GstClockTime next_ts;
1710 if (gst_video_rate_apply_pending_rate (videorate))
1713 if (videorate->segment.rate < 0.0) {
1714 /* Make sure that we have a duration for this buffer. The previous
1715 * buffer already has a duration given by either exactly this code,
1716 * or the code above for the very first buffer */
1717 g_assert (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf));
1718 if (!GST_BUFFER_DURATION_IS_VALID (buffer))
1719 GST_BUFFER_DURATION (buffer) =
1720 prevtime > intime ? prevtime - intime : 0;
1722 /* Make sure that we have a duration for previous buffer */
1723 if (!GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf))
1724 GST_BUFFER_DURATION (videorate->prevbuf) =
1725 intime > prevtime ? intime - prevtime : 0;
1729 #define ABSDIFF(a, b) (((a) > (b)) ? (a) - (b) : (b) - (a))
1732 /* take absolute diffs */
1733 if (videorate->segment.rate < 0.0) {
1734 GstClockTime next_end_ts;
1735 GstClockTime prev_endtime;
1736 GstClockTime in_endtime, base_ts_in_segment;
1738 next_ts = videorate->next_ts;
1740 if (!GST_CLOCK_TIME_IS_VALID (next_ts)) {
1741 GST_DEBUG_OBJECT (videorate, "Already reached segment start,"
1746 prev_endtime = prevtime + GST_BUFFER_DURATION (videorate->prevbuf);
1747 in_endtime = intime + GST_BUFFER_DURATION (buffer);
1749 if (videorate->to_rate_numerator) {
1750 GstClockTime frame_duration = gst_util_uint64_scale (1,
1751 videorate->to_rate_denominator * GST_SECOND,
1752 videorate->to_rate_numerator);
1753 next_end_ts = next_ts + frame_duration;
1755 next_end_ts = next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
1758 base_ts_in_segment = videorate->segment.stop - videorate->base_ts;
1759 next_ts = base_ts_in_segment - (
1760 (base_ts_in_segment - next_ts) * videorate->rate);
1761 next_end_ts = base_ts_in_segment - (MAX (0,
1762 (base_ts_in_segment - next_end_ts)) * videorate->rate);
1764 diff1 = ABSDIFF (prev_endtime, next_end_ts);
1765 diff2 = ABSDIFF (in_endtime, next_end_ts);
1767 GST_LOG_OBJECT (videorate,
1768 "diff with prev %" GST_TIME_FORMAT " diff with new %"
1769 GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
1770 GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
1771 GST_TIME_ARGS (next_end_ts));
1774 videorate->base_ts + ((videorate->next_ts -
1775 videorate->base_ts) * videorate->rate);
1777 diff1 = ABSDIFF (prevtime, next_ts);
1778 diff2 = ABSDIFF (intime, next_ts);
1780 GST_LOG_OBJECT (videorate,
1781 "diff with prev %" GST_TIME_FORMAT " diff with new %"
1782 GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
1783 GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
1784 GST_TIME_ARGS (next_ts));
1787 /* output first one when its the best */
1788 if (diff1 <= diff2) {
1792 /* on error the _flush function posted a warning already */
1793 if ((r = gst_video_rate_flush_prev (videorate,
1794 count > 1, intime, FALSE)) != GST_FLOW_OK) {
1800 /* continue while the first one was the best, if they were equal avoid
1801 * going into an infinite loop */
1803 while (diff1 < diff2);
1805 /* if we outputted the first buffer more then once, we have dups */
1807 videorate->dup += count - 1;
1808 if (!videorate->silent)
1809 gst_video_rate_notify_duplicate (videorate);
1811 /* if we didn't output the first buffer, we have a drop */
1812 else if (count == 0) {
1815 if (!videorate->silent)
1816 gst_video_rate_notify_drop (videorate);
1818 GST_LOG_OBJECT (videorate,
1819 "new is best, old never used, drop, outgoing ts %"
1820 GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
1822 GST_LOG_OBJECT (videorate,
1823 "END, putting new in old, diff1 %" GST_TIME_FORMAT
1824 ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
1825 ", in %" G_GUINT64_FORMAT ", out %" G_GUINT64_FORMAT ", drop %"
1826 G_GUINT64_FORMAT ", dup %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff1),
1827 GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
1828 videorate->in, videorate->out, videorate->drop, videorate->dup);
1830 /* swap in new one when it's the best */
1831 gst_video_rate_swap_prev (videorate, buffer, intime);
1839 GST_WARNING_OBJECT (videorate, "no framerate negotiated");
1840 res = GST_FLOW_NOT_NEGOTIATED;
1846 GST_WARNING_OBJECT (videorate,
1847 "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
1848 res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1854 gst_video_rate_start (GstBaseTransform * trans)
1856 gst_video_rate_reset (GST_VIDEO_RATE (trans));
1861 gst_video_rate_stop (GstBaseTransform * trans)
1863 gst_video_rate_reset (GST_VIDEO_RATE (trans));
1868 gst_videorate_update_duration (GstVideoRate * videorate)
1872 m = gst_message_new_duration_changed (GST_OBJECT (videorate));
1873 gst_element_post_message (GST_ELEMENT (videorate), m);
1877 gst_video_rate_set_property (GObject * object,
1878 guint prop_id, const GValue * value, GParamSpec * pspec)
1880 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1881 gboolean latency_changed = FALSE;
1883 GST_OBJECT_LOCK (videorate);
1886 videorate->silent = g_value_get_boolean (value);
1889 videorate->new_pref = g_value_get_double (value);
1891 case PROP_SKIP_TO_FIRST:
1892 videorate->skip_to_first = g_value_get_boolean (value);
1894 case PROP_DROP_ONLY:{
1895 gboolean new_value = g_value_get_boolean (value);
1897 /* Latency changes if we switch drop-only mode */
1898 latency_changed = new_value != videorate->drop_only;
1899 videorate->drop_only = g_value_get_boolean (value);
1902 case PROP_AVERAGE_PERIOD:
1903 videorate->average_period_set = g_value_get_uint64 (value);
1906 g_atomic_int_set (&videorate->max_rate, g_value_get_int (value));
1909 videorate->pending_rate = g_value_get_double (value);
1910 GST_OBJECT_UNLOCK (videorate);
1912 gst_videorate_update_duration (videorate);
1914 case PROP_MAX_DUPLICATION_TIME:
1915 videorate->max_duplication_time = g_value_get_uint64 (value);
1917 case PROP_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION:
1918 videorate->max_closing_segment_duplication_duration =
1919 g_value_get_uint64 (value);
1922 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1925 GST_OBJECT_UNLOCK (videorate);
1930 GST_OBJECT_UNLOCK (videorate);
1931 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (videorate));
1933 if (latency_changed) {
1934 gst_element_post_message (GST_ELEMENT (videorate),
1935 gst_message_new_latency (GST_OBJECT (videorate)));
1940 gst_video_rate_get_property (GObject * object,
1941 guint prop_id, GValue * value, GParamSpec * pspec)
1943 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1945 GST_OBJECT_LOCK (videorate);
1948 g_value_set_uint64 (value, videorate->in);
1951 g_value_set_uint64 (value, videorate->out);
1954 g_value_set_uint64 (value, videorate->dup);
1957 g_value_set_uint64 (value, videorate->drop);
1960 g_value_set_boolean (value, videorate->silent);
1963 g_value_set_double (value, videorate->new_pref);
1965 case PROP_SKIP_TO_FIRST:
1966 g_value_set_boolean (value, videorate->skip_to_first);
1968 case PROP_DROP_ONLY:
1969 g_value_set_boolean (value, videorate->drop_only);
1971 case PROP_AVERAGE_PERIOD:
1972 g_value_set_uint64 (value, videorate->average_period_set);
1975 g_value_set_int (value, g_atomic_int_get (&videorate->max_rate));
1978 g_value_set_double (value, videorate->pending_rate);
1980 case PROP_MAX_DUPLICATION_TIME:
1981 g_value_set_uint64 (value, videorate->max_duplication_time);
1983 case PROP_MAX_CLOSING_SEGMENT_DUPLICATION_DURATION:
1984 g_value_set_uint64 (value,
1985 videorate->max_closing_segment_duplication_duration);
1988 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1991 GST_OBJECT_UNLOCK (videorate);
1995 plugin_init (GstPlugin * plugin)
1997 GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
1998 "VideoRate stream fixer");
2000 return GST_ELEMENT_REGISTER (videorate, plugin);
2003 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2006 "Adjusts video frames",
2007 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)