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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-videorate
23 * This element takes an incoming stream of timestamped video frames.
24 * It will produce a perfect stream that matches the source pad's framerate.
26 * The correction is performed by dropping and duplicating frames, no fancy
27 * algorithm is used to interpolate frames (yet).
29 * By default the element will simply negotiate the same framerate on its
30 * source and sink pad.
32 * This operation is useful to link to elements that require a perfect stream.
33 * Typical examples are formats that do not store timestamps for video frames,
34 * but only store a framerate, like Ogg and AVI.
36 * A conversion to a specific framerate can be forced by using filtered caps on
39 * The properties #GstVideoRate:in, #GstVideoRate:out, #GstVideoRate:duplicate
40 * and #GstVideoRate:drop can be read to obtain information about number of
41 * input frames, output frames, dropped frames (i.e. the number of unused input
42 * frames) and duplicated frames (i.e. the number of times an input frame was
43 * duplicated, beside being used normally).
45 * An input stream that needs no adjustments will thus never have dropped or
48 * When the #GstVideoRate:silent property is set to FALSE, a GObject property
49 * notification will be emitted whenever one of the #GstVideoRate:duplicate or
50 * #GstVideoRate:drop values changes.
51 * This can potentially cause performance degradation.
52 * Note that property notification will happen from the streaming thread, so
53 * applications should be prepared for this.
56 * <title>Example pipelines</title>
58 * gst-launch -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! videorate ! video/x-raw,framerate=15/1 ! xvimagesink
59 * ]| Decode an Ogg/Theora file and adjust the framerate to 15 fps before playing.
60 * To create the test Ogg/Theora file refer to the documentation of theoraenc.
62 * gst-launch -v v4l2src ! videorate ! video/x-raw,framerate=25/2 ! theoraenc ! oggmux ! filesink location=recording.ogg
63 * ]| Capture video from a V4L device, and adjust the stream to 12.5 fps before
64 * encoding to Ogg/Theora.
67 * Last reviewed on 2006-09-02 (0.10.11)
74 #include "gstvideorate.h"
76 GST_DEBUG_CATEGORY_STATIC (video_rate_debug);
77 #define GST_CAT_DEFAULT video_rate_debug
79 /* GstVideoRate signals and args */
86 #define DEFAULT_SILENT TRUE
87 #define DEFAULT_NEW_PREF 1.0
88 #define DEFAULT_SKIP_TO_FIRST FALSE
89 #define DEFAULT_DROP_ONLY FALSE
90 #define DEFAULT_AVERAGE_PERIOD 0
91 #define DEFAULT_MAX_RATE G_MAXINT
108 static GstStaticPadTemplate gst_video_rate_src_template =
109 GST_STATIC_PAD_TEMPLATE ("src",
112 GST_STATIC_CAPS ("video/x-raw;" "image/jpeg;" "image/png")
115 static GstStaticPadTemplate gst_video_rate_sink_template =
116 GST_STATIC_PAD_TEMPLATE ("sink",
119 GST_STATIC_CAPS ("video/x-raw;" "image/jpeg;" "image/png")
122 static void gst_video_rate_swap_prev (GstVideoRate * videorate,
123 GstBuffer * buffer, gint64 time);
124 static gboolean gst_video_rate_sink_event (GstBaseTransform * trans,
126 static gboolean gst_video_rate_query (GstBaseTransform * trans,
127 GstPadDirection direction, GstQuery * query);
129 static gboolean gst_video_rate_setcaps (GstBaseTransform * trans,
130 GstCaps * in_caps, GstCaps * out_caps);
132 static GstCaps *gst_video_rate_transform_caps (GstBaseTransform * trans,
133 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
135 static GstCaps *gst_video_rate_fixate_caps (GstBaseTransform * trans,
136 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
138 static GstFlowReturn gst_video_rate_transform_ip (GstBaseTransform * trans,
141 static gboolean gst_video_rate_start (GstBaseTransform * trans);
142 static gboolean gst_video_rate_stop (GstBaseTransform * trans);
145 static void gst_video_rate_set_property (GObject * object,
146 guint prop_id, const GValue * value, GParamSpec * pspec);
147 static void gst_video_rate_get_property (GObject * object,
148 guint prop_id, GValue * value, GParamSpec * pspec);
150 static GParamSpec *pspec_drop = NULL;
151 static GParamSpec *pspec_duplicate = NULL;
153 #define gst_video_rate_parent_class parent_class
154 G_DEFINE_TYPE (GstVideoRate, gst_video_rate, GST_TYPE_BASE_TRANSFORM);
157 gst_video_rate_class_init (GstVideoRateClass * klass)
159 GObjectClass *object_class = G_OBJECT_CLASS (klass);
160 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
161 GstBaseTransformClass *base_class = GST_BASE_TRANSFORM_CLASS (klass);
163 object_class->set_property = gst_video_rate_set_property;
164 object_class->get_property = gst_video_rate_get_property;
166 base_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_rate_setcaps);
167 base_class->transform_caps =
168 GST_DEBUG_FUNCPTR (gst_video_rate_transform_caps);
169 base_class->transform_ip = GST_DEBUG_FUNCPTR (gst_video_rate_transform_ip);
170 base_class->sink_event = GST_DEBUG_FUNCPTR (gst_video_rate_sink_event);
171 base_class->start = GST_DEBUG_FUNCPTR (gst_video_rate_start);
172 base_class->stop = GST_DEBUG_FUNCPTR (gst_video_rate_stop);
173 base_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_video_rate_fixate_caps);
174 base_class->query = GST_DEBUG_FUNCPTR (gst_video_rate_query);
176 g_object_class_install_property (object_class, PROP_IN,
177 g_param_spec_uint64 ("in", "In",
178 "Number of input frames", 0, G_MAXUINT64, 0,
179 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (object_class, PROP_OUT,
181 g_param_spec_uint64 ("out", "Out", "Number of output frames", 0,
182 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
183 pspec_duplicate = g_param_spec_uint64 ("duplicate", "Duplicate",
184 "Number of duplicated frames", 0, G_MAXUINT64, 0,
185 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
186 g_object_class_install_property (object_class, PROP_DUP, pspec_duplicate);
187 pspec_drop = g_param_spec_uint64 ("drop", "Drop", "Number of dropped frames",
188 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
189 g_object_class_install_property (object_class, PROP_DROP, pspec_drop);
190 g_object_class_install_property (object_class, PROP_SILENT,
191 g_param_spec_boolean ("silent", "silent",
192 "Don't emit notify for dropped and duplicated frames", DEFAULT_SILENT,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 g_object_class_install_property (object_class, PROP_NEW_PREF,
195 g_param_spec_double ("new-pref", "New Pref",
196 "Value indicating how much to prefer new frames (unused)", 0.0, 1.0,
197 DEFAULT_NEW_PREF, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200 * GstVideoRate:skip-to-first:
202 * Don't produce buffers before the first one we receive.
206 g_object_class_install_property (object_class, PROP_SKIP_TO_FIRST,
207 g_param_spec_boolean ("skip-to-first", "Skip to first buffer",
208 "Don't produce buffers before the first one we receive",
209 DEFAULT_SKIP_TO_FIRST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 * GstVideoRate:drop-only:
214 * Only drop frames, no duplicates are produced.
218 g_object_class_install_property (object_class, PROP_DROP_ONLY,
219 g_param_spec_boolean ("drop-only", "Only Drop",
220 "Only drop frames, no duplicates are produced",
221 DEFAULT_DROP_ONLY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 * GstVideoRate:average-period:
226 * Arrange for maximum framerate by dropping frames beyond a certain framerate,
227 * where the framerate is calculated using a moving average over the
232 g_object_class_install_property (object_class, PROP_AVERAGE_PERIOD,
233 g_param_spec_uint64 ("average-period", "Period over which to average",
234 "Period over which to average the framerate (in ns) (0 = disabled)",
235 0, G_MAXINT64, DEFAULT_AVERAGE_PERIOD,
236 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 * GstVideoRate:max-rate:
241 * maximum framerate to pass through
245 g_object_class_install_property (object_class, PROP_MAX_RATE,
246 g_param_spec_int ("max-rate", "maximum framerate",
247 "Maximum framerate allowed to pass through "
248 "(in frames per second, implies drop-only)",
249 1, G_MAXINT, DEFAULT_MAX_RATE,
250 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
252 gst_element_class_set_details_simple (element_class,
253 "Video rate adjuster", "Filter/Effect/Video",
254 "Drops/duplicates/adjusts timestamps on video frames to make a perfect stream",
255 "Wim Taymans <wim@fluendo.com>");
257 gst_element_class_add_pad_template (element_class,
258 gst_static_pad_template_get (&gst_video_rate_sink_template));
259 gst_element_class_add_pad_template (element_class,
260 gst_static_pad_template_get (&gst_video_rate_src_template));
264 gst_value_fraction_get_extremes (const GValue * v,
265 gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
267 if (GST_VALUE_HOLDS_FRACTION (v)) {
268 *min_num = *max_num = gst_value_get_fraction_numerator (v);
269 *min_denom = *max_denom = gst_value_get_fraction_denominator (v);
270 } else if (GST_VALUE_HOLDS_FRACTION_RANGE (v)) {
271 const GValue *min, *max;
273 min = gst_value_get_fraction_range_min (v);
274 *min_num = gst_value_get_fraction_numerator (min);
275 *min_denom = gst_value_get_fraction_denominator (min);
277 max = gst_value_get_fraction_range_max (v);
278 *max_num = gst_value_get_fraction_numerator (max);
279 *max_denom = gst_value_get_fraction_denominator (max);
280 } else if (GST_VALUE_HOLDS_LIST (v)) {
281 gint min_n = G_MAXINT, min_d = 1, max_n = 0, max_d = 1;
289 n = gst_value_list_get_size (v);
293 for (i = 0; i < n; i++) {
294 const GValue *t = gst_value_list_get_value (v, i);
296 gst_value_fraction_get_extremes (t, &min_n, &min_d, &max_n, &max_d);
297 if (gst_util_fraction_compare (min_n, min_d, *min_num, *min_denom) < 0) {
302 if (gst_util_fraction_compare (max_n, max_d, *max_num, *max_denom) > 0) {
308 g_warning ("Unknown type for framerate");
316 /* Clamp the framerate in a caps structure to be a smaller range then
317 * [1...max_rate], otherwise return false */
319 gst_video_max_rate_clamp_structure (GstStructure * s, gint maxrate,
320 gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
322 gboolean ret = FALSE;
324 if (!gst_structure_has_field (s, "framerate")) {
325 /* No framerate field implies any framerate, clamping would result in
326 * [1..max_rate] so not a real subset */
330 GValue intersection = { 0, };
331 GValue clamp = { 0, };
332 gint tmp_num, tmp_denom;
334 g_value_init (&clamp, GST_TYPE_FRACTION_RANGE);
335 gst_value_set_fraction_range_full (&clamp, 0, 1, maxrate, 1);
337 v = gst_structure_get_value (s, "framerate");
338 ret = gst_value_intersect (&intersection, v, &clamp);
339 g_value_unset (&clamp);
344 gst_value_fraction_get_extremes (&intersection,
345 min_num, min_denom, max_num, max_denom);
347 gst_value_fraction_get_extremes (v,
348 &tmp_num, &tmp_denom, max_num, max_denom);
350 if (gst_util_fraction_compare (*max_num, *max_denom, maxrate, 1) > 0) {
355 gst_structure_take_value (s, "framerate", &intersection);
363 gst_video_rate_transform_caps (GstBaseTransform * trans,
364 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
366 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
368 GstStructure *s, *s1, *s2, *s3 = NULL;
369 int maxrate = g_atomic_int_get (&videorate->max_rate);
372 ret = gst_caps_new_empty ();
374 for (i = 0; i < gst_caps_get_size (caps); i++) {
375 s = gst_caps_get_structure (caps, i);
377 s1 = gst_structure_copy (s);
378 s2 = gst_structure_copy (s);
380 if (videorate->drop_only) {
381 gint min_num = 0, min_denom = 1;
382 gint max_num = G_MAXINT, max_denom = 1;
384 /* Clamp the caps to our maximum rate as the first caps if possible */
385 if (!gst_video_max_rate_clamp_structure (s1, maxrate,
386 &min_num, &min_denom, &max_num, &max_denom)) {
392 /* clamp wouldn't be a real subset of 1..maxrate, in this case the sink
393 * caps should become [1..maxrate], [1..maxint] and the src caps just
394 * [1..maxrate]. In case there was a caps incompatibility things will
395 * explode later as appropriate :)
397 * In case [X..maxrate] == [X..maxint], skip as we'll set it later
399 if (direction == GST_PAD_SRC && maxrate != G_MAXINT)
400 gst_structure_set (s1, "framerate", GST_TYPE_FRACTION_RANGE,
401 min_num, min_denom, maxrate, 1, NULL);
403 gst_structure_free (s1);
408 if (direction == GST_PAD_SRC) {
409 /* We can accept anything as long as it's at least the minimal framerate
410 * the the sink needs */
411 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE,
412 min_num, min_denom, G_MAXINT, 1, NULL);
414 /* Also allow unknown framerate, if it isn't already */
415 if (min_num != 0 || min_denom != 1) {
416 s3 = gst_structure_copy (s);
417 gst_structure_set (s3, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
419 } else if (max_num != 0 || max_denom != 1) {
420 /* We can provide everything upto the maximum framerate at the src */
421 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE,
422 0, 1, max_num, max_denom, NULL);
424 } else if (direction == GST_PAD_SINK) {
425 gint min_num = 0, min_denom = 1;
426 gint max_num = G_MAXINT, max_denom = 1;
428 if (!gst_video_max_rate_clamp_structure (s1, maxrate,
429 &min_num, &min_denom, &max_num, &max_denom)) {
430 gst_structure_free (s1);
433 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
436 /* set the framerate as a range */
437 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
441 ret = gst_caps_merge_structure (ret, s1);
442 ret = gst_caps_merge_structure (ret, s2);
444 ret = gst_caps_merge_structure (ret, s3);
447 GstCaps *intersection;
450 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
451 gst_caps_unref (ret);
458 gst_video_rate_fixate_caps (GstBaseTransform * trans,
459 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
464 s = gst_caps_get_structure (caps, 0);
465 if (G_UNLIKELY (!gst_structure_get_fraction (s, "framerate", &num, &denom)))
468 othercaps = gst_caps_truncate (othercaps);
469 othercaps = gst_caps_make_writable (othercaps);
470 s = gst_caps_get_structure (othercaps, 0);
471 gst_structure_fixate_field_nearest_fraction (s, "framerate", num, denom);
477 gst_video_rate_setcaps (GstBaseTransform * trans, GstCaps * in_caps,
480 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
481 GstStructure *structure;
483 gint rate_numerator, rate_denominator;
485 videorate = GST_VIDEO_RATE (trans);
487 GST_DEBUG_OBJECT (trans, "setcaps called in: %" GST_PTR_FORMAT
488 " out: %" GST_PTR_FORMAT, in_caps, out_caps);
490 structure = gst_caps_get_structure (in_caps, 0);
491 if (!gst_structure_get_fraction (structure, "framerate",
492 &rate_numerator, &rate_denominator))
495 videorate->from_rate_numerator = rate_numerator;
496 videorate->from_rate_denominator = rate_denominator;
498 structure = gst_caps_get_structure (out_caps, 0);
499 if (!gst_structure_get_fraction (structure, "framerate",
500 &rate_numerator, &rate_denominator))
503 /* out_frame_count is scaled by the frame rate caps when calculating next_ts.
504 * when the frame rate caps change, we must update base_ts and reset
506 if (videorate->to_rate_numerator) {
507 videorate->base_ts +=
508 gst_util_uint64_scale (videorate->out_frame_count,
509 videorate->to_rate_denominator * GST_SECOND,
510 videorate->to_rate_numerator);
512 videorate->out_frame_count = 0;
513 videorate->to_rate_numerator = rate_numerator;
514 videorate->to_rate_denominator = rate_denominator;
517 videorate->wanted_diff = gst_util_uint64_scale_int (GST_SECOND,
518 rate_denominator, rate_numerator);
520 videorate->wanted_diff = 0;
523 /* After a setcaps, our caps may have changed. In that case, we can't use
524 * the old buffer, if there was one (it might have different dimensions) */
525 GST_DEBUG_OBJECT (videorate, "swapping old buffers");
526 gst_video_rate_swap_prev (videorate, NULL, GST_CLOCK_TIME_NONE);
527 videorate->last_ts = GST_CLOCK_TIME_NONE;
528 videorate->average = 0;
534 GST_DEBUG_OBJECT (videorate, "no framerate specified");
541 gst_video_rate_reset (GstVideoRate * videorate)
543 GST_DEBUG_OBJECT (videorate, "resetting internal variables");
547 videorate->base_ts = 0;
548 videorate->out_frame_count = 0;
551 videorate->next_ts = GST_CLOCK_TIME_NONE;
552 videorate->last_ts = GST_CLOCK_TIME_NONE;
553 videorate->discont = TRUE;
554 videorate->average = 0;
555 gst_video_rate_swap_prev (videorate, NULL, 0);
557 gst_segment_init (&videorate->segment, GST_FORMAT_TIME);
561 gst_video_rate_init (GstVideoRate * videorate)
563 gst_video_rate_reset (videorate);
564 videorate->silent = DEFAULT_SILENT;
565 videorate->new_pref = DEFAULT_NEW_PREF;
566 videorate->drop_only = DEFAULT_DROP_ONLY;
567 videorate->average_period = DEFAULT_AVERAGE_PERIOD;
568 videorate->average_period_set = DEFAULT_AVERAGE_PERIOD;
569 videorate->max_rate = DEFAULT_MAX_RATE;
571 videorate->from_rate_numerator = 0;
572 videorate->from_rate_denominator = 0;
573 videorate->to_rate_numerator = 0;
574 videorate->to_rate_denominator = 0;
576 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (videorate), TRUE);
579 /* flush the oldest buffer */
581 gst_video_rate_flush_prev (GstVideoRate * videorate, gboolean duplicate)
585 GstClockTime push_ts;
587 if (!videorate->prevbuf)
588 goto eos_before_buffers;
590 /* make sure we can write to the metadata */
591 outbuf = gst_buffer_make_writable (gst_buffer_ref (videorate->prevbuf));
593 GST_BUFFER_OFFSET (outbuf) = videorate->out;
594 GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
596 if (videorate->discont) {
597 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
598 videorate->discont = FALSE;
600 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
603 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
605 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
607 /* this is the timestamp we put on the buffer */
608 push_ts = videorate->next_ts;
611 videorate->out_frame_count++;
612 if (videorate->to_rate_numerator) {
613 /* interpolate next expected timestamp in the segment */
615 videorate->segment.base + videorate->segment.start +
616 videorate->base_ts + gst_util_uint64_scale (videorate->out_frame_count,
617 videorate->to_rate_denominator * GST_SECOND,
618 videorate->to_rate_numerator);
619 GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
622 /* We do not need to update time in VFR (variable frame rate) mode */
623 if (!videorate->drop_only) {
624 /* adapt for looping, bring back to time in current segment. */
625 GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.base;
628 GST_LOG_OBJECT (videorate,
629 "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
630 GST_TIME_ARGS (push_ts));
632 res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (videorate), outbuf);
639 GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
645 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
648 GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
649 if (videorate->prevbuf)
650 gst_buffer_unref (videorate->prevbuf);
651 videorate->prevbuf = buffer != NULL ? gst_buffer_ref (buffer) : NULL;
652 videorate->prev_ts = time;
656 gst_video_rate_notify_drop (GstVideoRate * videorate)
658 g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
662 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
664 g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
667 #define MAGIC_LIMIT 25
669 gst_video_rate_sink_event (GstBaseTransform * trans, GstEvent * event)
671 GstVideoRate *videorate;
673 videorate = GST_VIDEO_RATE (trans);
675 switch (GST_EVENT_TYPE (event)) {
676 case GST_EVENT_SEGMENT:
678 const GstSegment *segment;
680 gst_event_parse_segment (event, &segment);
682 if (segment->format != GST_FORMAT_TIME)
685 GST_DEBUG_OBJECT (videorate, "handle NEWSEGMENT");
687 /* close up the previous segment, if appropriate */
688 if (videorate->prevbuf) {
693 /* fill up to the end of current segment,
694 * or only send out the stored buffer if there is no specific stop.
695 * regardless, prevent going loopy in strange cases */
696 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
697 ((GST_CLOCK_TIME_IS_VALID (videorate->segment.stop) &&
698 videorate->next_ts - videorate->segment.base
699 < videorate->segment.stop)
701 res = gst_video_rate_flush_prev (videorate, count > 0);
705 videorate->dup += count - 1;
706 if (!videorate->silent)
707 gst_video_rate_notify_duplicate (videorate);
708 } else if (count == 0) {
710 if (!videorate->silent)
711 gst_video_rate_notify_drop (videorate);
713 /* clean up for the new one; _chain will resume from the new start */
714 videorate->base_ts = 0;
715 videorate->out_frame_count = 0;
716 gst_video_rate_swap_prev (videorate, NULL, 0);
717 videorate->next_ts = GST_CLOCK_TIME_NONE;
720 /* We just want to update the accumulated stream_time */
721 gst_segment_copy_into (segment, &videorate->segment);
723 GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
724 &videorate->segment);
729 GstFlowReturn res = GST_FLOW_OK;
731 GST_DEBUG_OBJECT (videorate, "Got EOS");
733 /* If the segment has a stop position, fill the segment */
734 if (GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
735 /* fill up to the end of current segment,
736 * or only send out the stored buffer if there is no specific stop.
737 * regardless, prevent going loopy in strange cases */
738 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
739 ((videorate->next_ts - videorate->segment.base <
740 videorate->segment.stop)
742 res = gst_video_rate_flush_prev (videorate, count > 0);
745 } else if (videorate->prevbuf) {
746 /* Output at least one frame but if the buffer duration is valid, output
747 * enough frames to use the complete buffer duration */
748 if (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf)) {
749 GstClockTime end_ts =
750 videorate->next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
752 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
753 ((videorate->next_ts - videorate->segment.base < end_ts)
755 res = gst_video_rate_flush_prev (videorate, count > 0);
759 res = gst_video_rate_flush_prev (videorate, FALSE);
765 videorate->dup += count - 1;
766 if (!videorate->silent)
767 gst_video_rate_notify_duplicate (videorate);
768 } else if (count == 0) {
770 if (!videorate->silent)
771 gst_video_rate_notify_drop (videorate);
776 case GST_EVENT_FLUSH_STOP:
777 /* also resets the segment */
778 GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
779 gst_video_rate_reset (videorate);
785 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
790 GST_WARNING_OBJECT (videorate,
791 "Got segment but doesn't have GST_FORMAT_TIME value");
797 gst_video_rate_query (GstBaseTransform * trans, GstPadDirection direction,
800 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
801 gboolean res = FALSE;
804 otherpad = (direction == GST_PAD_SRC) ?
805 GST_BASE_TRANSFORM_SINK_PAD (trans) : GST_BASE_TRANSFORM_SRC_PAD (trans);
807 switch (GST_QUERY_TYPE (query)) {
808 case GST_QUERY_LATENCY:
810 GstClockTime min, max;
816 GST_OBJECT_LOCK (videorate);
817 avg_period = videorate->average_period_set;
818 GST_OBJECT_UNLOCK (videorate);
820 if (avg_period == 0 && (peer = gst_pad_get_peer (otherpad))) {
821 if ((res = gst_pad_query (peer, query))) {
822 gst_query_parse_latency (query, &live, &min, &max);
824 GST_DEBUG_OBJECT (videorate, "Peer latency: min %"
825 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
826 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
828 if (videorate->from_rate_numerator != 0) {
829 /* add latency. We don't really know since we hold on to the frames
830 * until we get a next frame, which can be anything. We assume
831 * however that this will take from_rate time. */
832 latency = gst_util_uint64_scale (GST_SECOND,
833 videorate->from_rate_denominator,
834 videorate->from_rate_numerator);
836 /* no input framerate, we don't know */
840 GST_DEBUG_OBJECT (videorate, "Our latency: %"
841 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
847 GST_DEBUG_OBJECT (videorate, "Calculated total latency : min %"
848 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
849 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
851 gst_query_set_latency (query, live, min, max);
853 gst_object_unref (peer);
856 /* Simple fallthrough if we don't have a latency or not a peer that we
857 * can't ask about its latency yet.. */
861 GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
870 gst_video_rate_trans_ip_max_avg (GstVideoRate * videorate, GstBuffer * buf)
872 GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
876 if (!GST_CLOCK_TIME_IS_VALID (ts) || videorate->wanted_diff == 0)
879 /* drop frames if they exceed our output rate */
880 if (GST_CLOCK_TIME_IS_VALID (videorate->last_ts)) {
881 GstClockTimeDiff diff = ts - videorate->last_ts;
883 /* Drop buffer if its early compared to the desired frame rate and
884 * the current average is higher than the desired average
886 if (diff < videorate->wanted_diff &&
887 videorate->average < videorate->wanted_diff)
891 if (videorate->average) {
892 GstClockTimeDiff wanted_diff;
894 if (G_LIKELY (videorate->average_period > videorate->wanted_diff))
895 wanted_diff = videorate->wanted_diff;
897 wanted_diff = videorate->average_period * 10;
900 gst_util_uint64_scale_round (videorate->average,
901 videorate->average_period - wanted_diff,
902 videorate->average_period) +
903 gst_util_uint64_scale_round (diff, wanted_diff,
904 videorate->average_period);
906 videorate->average = diff;
910 videorate->last_ts = ts;
917 if (!videorate->silent)
918 gst_video_rate_notify_drop (videorate);
919 return GST_BASE_TRANSFORM_FLOW_DROPPED;
923 gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
925 GstVideoRate *videorate;
926 GstFlowReturn res = GST_BASE_TRANSFORM_FLOW_DROPPED;
927 GstClockTime intime, in_ts, in_dur;
928 GstClockTime avg_period;
929 gboolean skip = FALSE;
931 videorate = GST_VIDEO_RATE (trans);
933 /* make sure the denominators are not 0 */
934 if (videorate->from_rate_denominator == 0 ||
935 videorate->to_rate_denominator == 0)
938 GST_OBJECT_LOCK (videorate);
939 avg_period = videorate->average_period_set;
940 GST_OBJECT_UNLOCK (videorate);
942 /* MT-safe switching between modes */
943 if (G_UNLIKELY (avg_period != videorate->average_period)) {
944 gboolean switch_mode = (avg_period == 0 || videorate->average_period == 0);
945 videorate->average_period = avg_period;
946 videorate->last_ts = GST_CLOCK_TIME_NONE;
950 /* enabling average mode */
951 videorate->average = 0;
952 /* make sure no cached buffers from regular mode are left */
953 gst_video_rate_swap_prev (videorate, NULL, 0);
955 /* enable regular mode */
956 videorate->next_ts = GST_CLOCK_TIME_NONE;
960 /* max averaging mode has a no latency, normal mode does */
961 gst_element_post_message (GST_ELEMENT (videorate),
962 gst_message_new_latency (GST_OBJECT (videorate)));
966 if (videorate->average_period > 0)
967 return gst_video_rate_trans_ip_max_avg (videorate, buffer);
969 in_ts = GST_BUFFER_TIMESTAMP (buffer);
970 in_dur = GST_BUFFER_DURATION (buffer);
972 if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE)) {
973 in_ts = videorate->last_ts;
974 if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE))
978 /* get the time of the next expected buffer timestamp, we use this when the
979 * next buffer has -1 as a timestamp */
980 videorate->last_ts = in_ts;
981 if (in_dur != GST_CLOCK_TIME_NONE)
982 videorate->last_ts += in_dur;
984 GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
985 GST_TIME_ARGS (in_ts));
987 /* the input time is the time in the segment + all previously accumulated
989 intime = in_ts + videorate->segment.base;
991 /* we need to have two buffers to compare */
992 if (videorate->prevbuf == NULL) {
993 gst_video_rate_swap_prev (videorate, buffer, intime);
995 if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts)) {
996 /* new buffer, we expect to output a buffer that matches the first
997 * timestamp in the segment */
998 if (videorate->skip_to_first || skip) {
999 videorate->next_ts = intime;
1000 videorate->base_ts = in_ts - videorate->segment.start;
1001 videorate->out_frame_count = 0;
1003 videorate->next_ts = videorate->segment.start + videorate->segment.base;
1007 GstClockTime prevtime;
1009 gint64 diff1, diff2;
1011 prevtime = videorate->prev_ts;
1013 GST_LOG_OBJECT (videorate,
1014 "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
1015 " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
1016 GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
1020 /* drop new buffer if it's before previous one */
1021 if (intime < prevtime) {
1022 GST_DEBUG_OBJECT (videorate,
1023 "The new buffer (%" GST_TIME_FORMAT
1024 ") is before the previous buffer (%"
1025 GST_TIME_FORMAT "). Dropping new buffer.",
1026 GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
1028 if (!videorate->silent)
1029 gst_video_rate_notify_drop (videorate);
1033 /* got 2 buffers, see which one is the best */
1036 diff1 = prevtime - videorate->next_ts;
1037 diff2 = intime - videorate->next_ts;
1039 /* take absolute values, beware: abs and ABS don't work for gint64 */
1045 GST_LOG_OBJECT (videorate,
1046 "diff with prev %" GST_TIME_FORMAT " diff with new %"
1047 GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
1048 GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
1049 GST_TIME_ARGS (videorate->next_ts));
1051 /* output first one when its the best */
1052 if (diff1 <= diff2) {
1056 /* on error the _flush function posted a warning already */
1057 if ((r = gst_video_rate_flush_prev (videorate,
1058 count > 1)) != GST_FLOW_OK) {
1064 /* Do not produce any dups. We can exit loop now */
1065 if (videorate->drop_only)
1067 /* continue while the first one was the best, if they were equal avoid
1068 * going into an infinite loop */
1070 while (diff1 < diff2);
1072 /* if we outputed the first buffer more then once, we have dups */
1074 videorate->dup += count - 1;
1075 if (!videorate->silent)
1076 gst_video_rate_notify_duplicate (videorate);
1078 /* if we didn't output the first buffer, we have a drop */
1079 else if (count == 0) {
1082 if (!videorate->silent)
1083 gst_video_rate_notify_drop (videorate);
1085 GST_LOG_OBJECT (videorate,
1086 "new is best, old never used, drop, outgoing ts %"
1087 GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
1089 GST_LOG_OBJECT (videorate,
1090 "END, putting new in old, diff1 %" GST_TIME_FORMAT
1091 ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
1092 ", in %" G_GUINT64_FORMAT ", out %" G_GUINT64_FORMAT ", drop %"
1093 G_GUINT64_FORMAT ", dup %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff1),
1094 GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
1095 videorate->in, videorate->out, videorate->drop, videorate->dup);
1097 /* swap in new one when it's the best */
1098 gst_video_rate_swap_prev (videorate, buffer, intime);
1106 GST_WARNING_OBJECT (videorate, "no framerate negotiated");
1107 res = GST_FLOW_NOT_NEGOTIATED;
1113 GST_WARNING_OBJECT (videorate,
1114 "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
1115 res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1121 gst_video_rate_start (GstBaseTransform * trans)
1123 gst_video_rate_reset (GST_VIDEO_RATE (trans));
1128 gst_video_rate_stop (GstBaseTransform * trans)
1130 gst_video_rate_reset (GST_VIDEO_RATE (trans));
1135 gst_video_rate_set_property (GObject * object,
1136 guint prop_id, const GValue * value, GParamSpec * pspec)
1138 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1140 GST_OBJECT_LOCK (videorate);
1143 videorate->silent = g_value_get_boolean (value);
1146 videorate->new_pref = g_value_get_double (value);
1148 case PROP_SKIP_TO_FIRST:
1149 videorate->skip_to_first = g_value_get_boolean (value);
1151 case PROP_DROP_ONLY:
1152 videorate->drop_only = g_value_get_boolean (value);
1155 case PROP_AVERAGE_PERIOD:
1156 videorate->average_period_set = g_value_get_uint64 (value);
1159 g_atomic_int_set (&videorate->max_rate, g_value_get_int (value));
1163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1166 GST_OBJECT_UNLOCK (videorate);
1170 GST_OBJECT_UNLOCK (videorate);
1171 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (videorate));
1175 gst_video_rate_get_property (GObject * object,
1176 guint prop_id, GValue * value, GParamSpec * pspec)
1178 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1180 GST_OBJECT_LOCK (videorate);
1183 g_value_set_uint64 (value, videorate->in);
1186 g_value_set_uint64 (value, videorate->out);
1189 g_value_set_uint64 (value, videorate->dup);
1192 g_value_set_uint64 (value, videorate->drop);
1195 g_value_set_boolean (value, videorate->silent);
1198 g_value_set_double (value, videorate->new_pref);
1200 case PROP_SKIP_TO_FIRST:
1201 g_value_set_boolean (value, videorate->skip_to_first);
1203 case PROP_DROP_ONLY:
1204 g_value_set_boolean (value, videorate->drop_only);
1206 case PROP_AVERAGE_PERIOD:
1207 g_value_set_uint64 (value, videorate->average_period_set);
1210 g_value_set_int (value, g_atomic_int_get (&videorate->max_rate));
1213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1216 GST_OBJECT_UNLOCK (videorate);
1220 plugin_init (GstPlugin * plugin)
1222 GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
1223 "VideoRate stream fixer");
1225 return gst_element_register (plugin, "videorate", GST_RANK_NONE,
1226 GST_TYPE_VIDEO_RATE);
1229 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1232 "Adjusts video frames",
1233 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)