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
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.
204 g_object_class_install_property (object_class, PROP_SKIP_TO_FIRST,
205 g_param_spec_boolean ("skip-to-first", "Skip to first buffer",
206 "Don't produce buffers before the first one we receive",
207 DEFAULT_SKIP_TO_FIRST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 * GstVideoRate:drop-only:
212 * Only drop frames, no duplicates are produced.
214 g_object_class_install_property (object_class, PROP_DROP_ONLY,
215 g_param_spec_boolean ("drop-only", "Only Drop",
216 "Only drop frames, no duplicates are produced",
217 DEFAULT_DROP_ONLY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 * GstVideoRate:average-period:
222 * Arrange for maximum framerate by dropping frames beyond a certain framerate,
223 * where the framerate is calculated using a moving average over the
226 g_object_class_install_property (object_class, PROP_AVERAGE_PERIOD,
227 g_param_spec_uint64 ("average-period", "Period over which to average",
228 "Period over which to average the framerate (in ns) (0 = disabled)",
229 0, G_MAXINT64, DEFAULT_AVERAGE_PERIOD,
230 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 * GstVideoRate:max-rate:
235 * maximum framerate to pass through
237 g_object_class_install_property (object_class, PROP_MAX_RATE,
238 g_param_spec_int ("max-rate", "maximum framerate",
239 "Maximum framerate allowed to pass through "
240 "(in frames per second, implies drop-only)",
241 1, G_MAXINT, DEFAULT_MAX_RATE,
242 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
244 gst_element_class_set_static_metadata (element_class,
245 "Video rate adjuster", "Filter/Effect/Video",
246 "Drops/duplicates/adjusts timestamps on video frames to make a perfect stream",
247 "Wim Taymans <wim@fluendo.com>");
249 gst_element_class_add_pad_template (element_class,
250 gst_static_pad_template_get (&gst_video_rate_sink_template));
251 gst_element_class_add_pad_template (element_class,
252 gst_static_pad_template_get (&gst_video_rate_src_template));
256 gst_value_fraction_get_extremes (const GValue * v,
257 gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
259 if (GST_VALUE_HOLDS_FRACTION (v)) {
260 *min_num = *max_num = gst_value_get_fraction_numerator (v);
261 *min_denom = *max_denom = gst_value_get_fraction_denominator (v);
262 } else if (GST_VALUE_HOLDS_FRACTION_RANGE (v)) {
263 const GValue *min, *max;
265 min = gst_value_get_fraction_range_min (v);
266 *min_num = gst_value_get_fraction_numerator (min);
267 *min_denom = gst_value_get_fraction_denominator (min);
269 max = gst_value_get_fraction_range_max (v);
270 *max_num = gst_value_get_fraction_numerator (max);
271 *max_denom = gst_value_get_fraction_denominator (max);
272 } else if (GST_VALUE_HOLDS_LIST (v)) {
273 gint min_n = G_MAXINT, min_d = 1, max_n = 0, max_d = 1;
281 n = gst_value_list_get_size (v);
285 for (i = 0; i < n; i++) {
286 const GValue *t = gst_value_list_get_value (v, i);
288 gst_value_fraction_get_extremes (t, &min_n, &min_d, &max_n, &max_d);
289 if (gst_util_fraction_compare (min_n, min_d, *min_num, *min_denom) < 0) {
294 if (gst_util_fraction_compare (max_n, max_d, *max_num, *max_denom) > 0) {
300 g_warning ("Unknown type for framerate");
308 /* Clamp the framerate in a caps structure to be a smaller range then
309 * [1...max_rate], otherwise return false */
311 gst_video_max_rate_clamp_structure (GstStructure * s, gint maxrate,
312 gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
314 gboolean ret = FALSE;
316 if (!gst_structure_has_field (s, "framerate")) {
317 /* No framerate field implies any framerate, clamping would result in
318 * [1..max_rate] so not a real subset */
322 GValue intersection = { 0, };
323 GValue clamp = { 0, };
324 gint tmp_num, tmp_denom;
326 g_value_init (&clamp, GST_TYPE_FRACTION_RANGE);
327 gst_value_set_fraction_range_full (&clamp, 0, 1, maxrate, 1);
329 v = gst_structure_get_value (s, "framerate");
330 ret = gst_value_intersect (&intersection, v, &clamp);
331 g_value_unset (&clamp);
336 gst_value_fraction_get_extremes (&intersection,
337 min_num, min_denom, max_num, max_denom);
339 gst_value_fraction_get_extremes (v,
340 &tmp_num, &tmp_denom, max_num, max_denom);
342 if (gst_util_fraction_compare (*max_num, *max_denom, maxrate, 1) > 0) {
347 gst_structure_take_value (s, "framerate", &intersection);
355 gst_video_rate_transform_caps (GstBaseTransform * trans,
356 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
358 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
360 GstStructure *s, *s1, *s2, *s3 = NULL;
361 int maxrate = g_atomic_int_get (&videorate->max_rate);
364 ret = gst_caps_new_empty ();
366 for (i = 0; i < gst_caps_get_size (caps); i++) {
367 s = gst_caps_get_structure (caps, i);
369 s1 = gst_structure_copy (s);
370 s2 = gst_structure_copy (s);
372 if (videorate->drop_only) {
373 gint min_num = 0, min_denom = 1;
374 gint max_num = G_MAXINT, max_denom = 1;
376 /* Clamp the caps to our maximum rate as the first caps if possible */
377 if (!gst_video_max_rate_clamp_structure (s1, maxrate,
378 &min_num, &min_denom, &max_num, &max_denom)) {
384 /* clamp wouldn't be a real subset of 1..maxrate, in this case the sink
385 * caps should become [1..maxrate], [1..maxint] and the src caps just
386 * [1..maxrate]. In case there was a caps incompatibility things will
387 * explode later as appropriate :)
389 * In case [X..maxrate] == [X..maxint], skip as we'll set it later
391 if (direction == GST_PAD_SRC && maxrate != G_MAXINT)
392 gst_structure_set (s1, "framerate", GST_TYPE_FRACTION_RANGE,
393 min_num, min_denom, maxrate, 1, NULL);
395 gst_structure_free (s1);
400 if (direction == GST_PAD_SRC) {
401 /* We can accept anything as long as it's at least the minimal framerate
402 * the the sink needs */
403 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE,
404 min_num, min_denom, G_MAXINT, 1, NULL);
406 /* Also allow unknown framerate, if it isn't already */
407 if (min_num != 0 || min_denom != 1) {
408 s3 = gst_structure_copy (s);
409 gst_structure_set (s3, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
411 } else if (max_num != 0 || max_denom != 1) {
412 /* We can provide everything upto the maximum framerate at the src */
413 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE,
414 0, 1, max_num, max_denom, NULL);
416 } else if (direction == GST_PAD_SINK) {
417 gint min_num = 0, min_denom = 1;
418 gint max_num = G_MAXINT, max_denom = 1;
420 if (!gst_video_max_rate_clamp_structure (s1, maxrate,
421 &min_num, &min_denom, &max_num, &max_denom)) {
422 gst_structure_free (s1);
425 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
428 /* set the framerate as a range */
429 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
433 ret = gst_caps_merge_structure (ret, s1);
434 ret = gst_caps_merge_structure (ret, s2);
436 ret = gst_caps_merge_structure (ret, s3);
439 GstCaps *intersection;
442 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
443 gst_caps_unref (ret);
450 gst_video_rate_fixate_caps (GstBaseTransform * trans,
451 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
456 s = gst_caps_get_structure (caps, 0);
457 if (G_UNLIKELY (!gst_structure_get_fraction (s, "framerate", &num, &denom)))
460 othercaps = gst_caps_truncate (othercaps);
461 othercaps = gst_caps_make_writable (othercaps);
462 s = gst_caps_get_structure (othercaps, 0);
463 gst_structure_fixate_field_nearest_fraction (s, "framerate", num, denom);
469 gst_video_rate_setcaps (GstBaseTransform * trans, GstCaps * in_caps,
472 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
473 GstStructure *structure;
475 gint rate_numerator, rate_denominator;
477 videorate = GST_VIDEO_RATE (trans);
479 GST_DEBUG_OBJECT (trans, "setcaps called in: %" GST_PTR_FORMAT
480 " out: %" GST_PTR_FORMAT, in_caps, out_caps);
482 structure = gst_caps_get_structure (in_caps, 0);
483 if (!gst_structure_get_fraction (structure, "framerate",
484 &rate_numerator, &rate_denominator))
487 videorate->from_rate_numerator = rate_numerator;
488 videorate->from_rate_denominator = rate_denominator;
490 structure = gst_caps_get_structure (out_caps, 0);
491 if (!gst_structure_get_fraction (structure, "framerate",
492 &rate_numerator, &rate_denominator))
495 /* out_frame_count is scaled by the frame rate caps when calculating next_ts.
496 * when the frame rate caps change, we must update base_ts and reset
498 if (videorate->to_rate_numerator) {
499 videorate->base_ts +=
500 gst_util_uint64_scale (videorate->out_frame_count,
501 videorate->to_rate_denominator * GST_SECOND,
502 videorate->to_rate_numerator);
504 videorate->out_frame_count = 0;
505 videorate->to_rate_numerator = rate_numerator;
506 videorate->to_rate_denominator = rate_denominator;
509 videorate->wanted_diff = gst_util_uint64_scale_int (GST_SECOND,
510 rate_denominator, rate_numerator);
512 videorate->wanted_diff = 0;
515 /* After a setcaps, our caps may have changed. In that case, we can't use
516 * the old buffer, if there was one (it might have different dimensions) */
517 GST_DEBUG_OBJECT (videorate, "swapping old buffers");
518 gst_video_rate_swap_prev (videorate, NULL, GST_CLOCK_TIME_NONE);
519 videorate->last_ts = GST_CLOCK_TIME_NONE;
520 videorate->average = 0;
526 GST_DEBUG_OBJECT (videorate, "no framerate specified");
533 gst_video_rate_reset (GstVideoRate * videorate)
535 GST_DEBUG_OBJECT (videorate, "resetting internal variables");
539 videorate->base_ts = 0;
540 videorate->out_frame_count = 0;
543 videorate->next_ts = GST_CLOCK_TIME_NONE;
544 videorate->last_ts = GST_CLOCK_TIME_NONE;
545 videorate->discont = TRUE;
546 videorate->average = 0;
547 gst_video_rate_swap_prev (videorate, NULL, 0);
549 gst_segment_init (&videorate->segment, GST_FORMAT_TIME);
553 gst_video_rate_init (GstVideoRate * videorate)
555 gst_video_rate_reset (videorate);
556 videorate->silent = DEFAULT_SILENT;
557 videorate->new_pref = DEFAULT_NEW_PREF;
558 videorate->drop_only = DEFAULT_DROP_ONLY;
559 videorate->average_period = DEFAULT_AVERAGE_PERIOD;
560 videorate->average_period_set = DEFAULT_AVERAGE_PERIOD;
561 videorate->max_rate = DEFAULT_MAX_RATE;
563 videorate->from_rate_numerator = 0;
564 videorate->from_rate_denominator = 0;
565 videorate->to_rate_numerator = 0;
566 videorate->to_rate_denominator = 0;
568 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (videorate), TRUE);
571 /* flush the oldest buffer */
573 gst_video_rate_flush_prev (GstVideoRate * videorate, gboolean duplicate)
577 GstClockTime push_ts;
579 if (!videorate->prevbuf)
580 goto eos_before_buffers;
582 /* make sure we can write to the metadata */
583 outbuf = gst_buffer_make_writable (gst_buffer_ref (videorate->prevbuf));
585 GST_BUFFER_OFFSET (outbuf) = videorate->out;
586 GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
588 if (videorate->discont) {
589 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
590 videorate->discont = FALSE;
592 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
595 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
597 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
599 /* this is the timestamp we put on the buffer */
600 push_ts = videorate->next_ts;
603 videorate->out_frame_count++;
604 if (videorate->to_rate_numerator) {
605 /* interpolate next expected timestamp in the segment */
607 videorate->segment.base + videorate->segment.start +
608 videorate->base_ts + gst_util_uint64_scale (videorate->out_frame_count,
609 videorate->to_rate_denominator * GST_SECOND,
610 videorate->to_rate_numerator);
611 GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
614 /* We do not need to update time in VFR (variable frame rate) mode */
615 if (!videorate->drop_only) {
616 /* adapt for looping, bring back to time in current segment. */
617 GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.base;
620 GST_LOG_OBJECT (videorate,
621 "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
622 GST_TIME_ARGS (push_ts));
624 res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (videorate), outbuf);
631 GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
637 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
640 GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
641 if (videorate->prevbuf)
642 gst_buffer_unref (videorate->prevbuf);
643 videorate->prevbuf = buffer != NULL ? gst_buffer_ref (buffer) : NULL;
644 videorate->prev_ts = time;
648 gst_video_rate_notify_drop (GstVideoRate * videorate)
650 g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
654 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
656 g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
659 #define MAGIC_LIMIT 25
661 gst_video_rate_sink_event (GstBaseTransform * trans, GstEvent * event)
663 GstVideoRate *videorate;
665 videorate = GST_VIDEO_RATE (trans);
667 switch (GST_EVENT_TYPE (event)) {
668 case GST_EVENT_SEGMENT:
670 const GstSegment *segment;
672 gst_event_parse_segment (event, &segment);
674 if (segment->format != GST_FORMAT_TIME)
677 GST_DEBUG_OBJECT (videorate, "handle NEWSEGMENT");
679 /* close up the previous segment, if appropriate */
680 if (videorate->prevbuf) {
685 /* fill up to the end of current segment,
686 * or only send out the stored buffer if there is no specific stop.
687 * regardless, prevent going loopy in strange cases */
688 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
689 ((GST_CLOCK_TIME_IS_VALID (videorate->segment.stop) &&
690 videorate->next_ts - videorate->segment.base
691 < videorate->segment.stop)
693 res = gst_video_rate_flush_prev (videorate, count > 0);
697 videorate->dup += count - 1;
698 if (!videorate->silent)
699 gst_video_rate_notify_duplicate (videorate);
700 } else if (count == 0) {
702 if (!videorate->silent)
703 gst_video_rate_notify_drop (videorate);
705 /* clean up for the new one; _chain will resume from the new start */
706 gst_video_rate_swap_prev (videorate, NULL, 0);
709 videorate->base_ts = 0;
710 videorate->out_frame_count = 0;
711 videorate->next_ts = GST_CLOCK_TIME_NONE;
713 /* We just want to update the accumulated stream_time */
714 gst_segment_copy_into (segment, &videorate->segment);
716 GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
717 &videorate->segment);
722 GstFlowReturn res = GST_FLOW_OK;
724 GST_DEBUG_OBJECT (videorate, "Got EOS");
726 /* If the segment has a stop position, fill the segment */
727 if (GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
728 /* fill up to the end of current segment,
729 * or only send out the stored buffer if there is no specific stop.
730 * regardless, prevent going loopy in strange cases */
731 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
732 ((videorate->next_ts - videorate->segment.base <
733 videorate->segment.stop)
735 res = gst_video_rate_flush_prev (videorate, count > 0);
738 } else if (videorate->prevbuf) {
739 /* Output at least one frame but if the buffer duration is valid, output
740 * enough frames to use the complete buffer duration */
741 if (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf)) {
742 GstClockTime end_ts =
743 videorate->next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
745 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
746 ((videorate->next_ts - videorate->segment.base < end_ts)
748 res = gst_video_rate_flush_prev (videorate, count > 0);
752 res = gst_video_rate_flush_prev (videorate, FALSE);
758 videorate->dup += count - 1;
759 if (!videorate->silent)
760 gst_video_rate_notify_duplicate (videorate);
761 } else if (count == 0) {
763 if (!videorate->silent)
764 gst_video_rate_notify_drop (videorate);
769 case GST_EVENT_FLUSH_STOP:
770 /* also resets the segment */
771 GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
772 gst_video_rate_reset (videorate);
775 /* no gaps after videorate, ignore the event */
776 gst_event_unref (event);
782 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
787 GST_WARNING_OBJECT (videorate,
788 "Got segment but doesn't have GST_FORMAT_TIME value");
794 gst_video_rate_query (GstBaseTransform * trans, GstPadDirection direction,
797 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
798 gboolean res = FALSE;
801 otherpad = (direction == GST_PAD_SRC) ?
802 GST_BASE_TRANSFORM_SINK_PAD (trans) : GST_BASE_TRANSFORM_SRC_PAD (trans);
804 switch (GST_QUERY_TYPE (query)) {
805 case GST_QUERY_LATENCY:
807 GstClockTime min, max;
813 GST_OBJECT_LOCK (videorate);
814 avg_period = videorate->average_period_set;
815 GST_OBJECT_UNLOCK (videorate);
817 if (avg_period == 0 && (peer = gst_pad_get_peer (otherpad))) {
818 if ((res = gst_pad_query (peer, query))) {
819 gst_query_parse_latency (query, &live, &min, &max);
821 GST_DEBUG_OBJECT (videorate, "Peer latency: min %"
822 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
823 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
825 if (videorate->from_rate_numerator != 0) {
826 /* add latency. We don't really know since we hold on to the frames
827 * until we get a next frame, which can be anything. We assume
828 * however that this will take from_rate time. */
829 latency = gst_util_uint64_scale (GST_SECOND,
830 videorate->from_rate_denominator,
831 videorate->from_rate_numerator);
833 /* no input framerate, we don't know */
837 GST_DEBUG_OBJECT (videorate, "Our latency: %"
838 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
844 GST_DEBUG_OBJECT (videorate, "Calculated total latency : min %"
845 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
846 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
848 gst_query_set_latency (query, live, min, max);
850 gst_object_unref (peer);
853 /* Simple fallthrough if we don't have a latency or not a peer that we
854 * can't ask about its latency yet.. */
858 GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
867 gst_video_rate_trans_ip_max_avg (GstVideoRate * videorate, GstBuffer * buf)
869 GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
873 if (!GST_CLOCK_TIME_IS_VALID (ts) || videorate->wanted_diff == 0)
876 /* drop frames if they exceed our output rate */
877 if (GST_CLOCK_TIME_IS_VALID (videorate->last_ts)) {
878 GstClockTimeDiff diff = ts - videorate->last_ts;
880 /* Drop buffer if its early compared to the desired frame rate and
881 * the current average is higher than the desired average
883 if (diff < videorate->wanted_diff &&
884 videorate->average < videorate->wanted_diff)
888 if (videorate->average) {
889 GstClockTimeDiff wanted_diff;
891 if (G_LIKELY (videorate->average_period > videorate->wanted_diff))
892 wanted_diff = videorate->wanted_diff;
894 wanted_diff = videorate->average_period * 10;
897 gst_util_uint64_scale_round (videorate->average,
898 videorate->average_period - wanted_diff,
899 videorate->average_period) +
900 gst_util_uint64_scale_round (diff, wanted_diff,
901 videorate->average_period);
903 videorate->average = diff;
907 videorate->last_ts = ts;
914 if (!videorate->silent)
915 gst_video_rate_notify_drop (videorate);
916 return GST_BASE_TRANSFORM_FLOW_DROPPED;
920 gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
922 GstVideoRate *videorate;
923 GstFlowReturn res = GST_BASE_TRANSFORM_FLOW_DROPPED;
924 GstClockTime intime, in_ts, in_dur;
925 GstClockTime avg_period;
926 gboolean skip = FALSE;
928 videorate = GST_VIDEO_RATE (trans);
930 /* make sure the denominators are not 0 */
931 if (videorate->from_rate_denominator == 0 ||
932 videorate->to_rate_denominator == 0)
935 GST_OBJECT_LOCK (videorate);
936 avg_period = videorate->average_period_set;
937 GST_OBJECT_UNLOCK (videorate);
939 /* MT-safe switching between modes */
940 if (G_UNLIKELY (avg_period != videorate->average_period)) {
941 gboolean switch_mode = (avg_period == 0 || videorate->average_period == 0);
942 videorate->average_period = avg_period;
943 videorate->last_ts = GST_CLOCK_TIME_NONE;
947 /* enabling average mode */
948 videorate->average = 0;
949 /* make sure no cached buffers from regular mode are left */
950 gst_video_rate_swap_prev (videorate, NULL, 0);
952 /* enable regular mode */
953 videorate->next_ts = GST_CLOCK_TIME_NONE;
957 /* max averaging mode has a no latency, normal mode does */
958 gst_element_post_message (GST_ELEMENT (videorate),
959 gst_message_new_latency (GST_OBJECT (videorate)));
963 if (videorate->average_period > 0)
964 return gst_video_rate_trans_ip_max_avg (videorate, buffer);
966 in_ts = GST_BUFFER_TIMESTAMP (buffer);
967 in_dur = GST_BUFFER_DURATION (buffer);
969 if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE)) {
970 in_ts = videorate->last_ts;
971 if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE))
975 /* get the time of the next expected buffer timestamp, we use this when the
976 * next buffer has -1 as a timestamp */
977 videorate->last_ts = in_ts;
978 if (in_dur != GST_CLOCK_TIME_NONE)
979 videorate->last_ts += in_dur;
981 GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
982 GST_TIME_ARGS (in_ts));
984 /* the input time is the time in the segment + all previously accumulated
986 intime = in_ts + videorate->segment.base;
988 /* we need to have two buffers to compare */
989 if (videorate->prevbuf == NULL) {
990 gst_video_rate_swap_prev (videorate, buffer, intime);
992 if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts)) {
993 /* new buffer, we expect to output a buffer that matches the first
994 * timestamp in the segment */
995 if (videorate->skip_to_first || skip) {
996 videorate->next_ts = intime;
997 videorate->base_ts = in_ts - videorate->segment.start;
998 videorate->out_frame_count = 0;
1000 videorate->next_ts = videorate->segment.start + videorate->segment.base;
1004 GstClockTime prevtime;
1006 gint64 diff1, diff2;
1008 prevtime = videorate->prev_ts;
1010 GST_LOG_OBJECT (videorate,
1011 "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
1012 " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
1013 GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
1017 /* drop new buffer if it's before previous one */
1018 if (intime < prevtime) {
1019 GST_DEBUG_OBJECT (videorate,
1020 "The new buffer (%" GST_TIME_FORMAT
1021 ") is before the previous buffer (%"
1022 GST_TIME_FORMAT "). Dropping new buffer.",
1023 GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
1025 if (!videorate->silent)
1026 gst_video_rate_notify_drop (videorate);
1030 /* got 2 buffers, see which one is the best */
1033 diff1 = prevtime - videorate->next_ts;
1034 diff2 = intime - videorate->next_ts;
1036 /* take absolute values, beware: abs and ABS don't work for gint64 */
1042 GST_LOG_OBJECT (videorate,
1043 "diff with prev %" GST_TIME_FORMAT " diff with new %"
1044 GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
1045 GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
1046 GST_TIME_ARGS (videorate->next_ts));
1048 /* output first one when its the best */
1049 if (diff1 <= diff2) {
1053 /* on error the _flush function posted a warning already */
1054 if ((r = gst_video_rate_flush_prev (videorate,
1055 count > 1)) != GST_FLOW_OK) {
1061 /* Do not produce any dups. We can exit loop now */
1062 if (videorate->drop_only)
1064 /* continue while the first one was the best, if they were equal avoid
1065 * going into an infinite loop */
1067 while (diff1 < diff2);
1069 /* if we outputed the first buffer more then once, we have dups */
1071 videorate->dup += count - 1;
1072 if (!videorate->silent)
1073 gst_video_rate_notify_duplicate (videorate);
1075 /* if we didn't output the first buffer, we have a drop */
1076 else if (count == 0) {
1079 if (!videorate->silent)
1080 gst_video_rate_notify_drop (videorate);
1082 GST_LOG_OBJECT (videorate,
1083 "new is best, old never used, drop, outgoing ts %"
1084 GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
1086 GST_LOG_OBJECT (videorate,
1087 "END, putting new in old, diff1 %" GST_TIME_FORMAT
1088 ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
1089 ", in %" G_GUINT64_FORMAT ", out %" G_GUINT64_FORMAT ", drop %"
1090 G_GUINT64_FORMAT ", dup %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff1),
1091 GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
1092 videorate->in, videorate->out, videorate->drop, videorate->dup);
1094 /* swap in new one when it's the best */
1095 gst_video_rate_swap_prev (videorate, buffer, intime);
1103 GST_WARNING_OBJECT (videorate, "no framerate negotiated");
1104 res = GST_FLOW_NOT_NEGOTIATED;
1110 GST_WARNING_OBJECT (videorate,
1111 "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
1112 res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1118 gst_video_rate_start (GstBaseTransform * trans)
1120 gst_video_rate_reset (GST_VIDEO_RATE (trans));
1125 gst_video_rate_stop (GstBaseTransform * trans)
1127 gst_video_rate_reset (GST_VIDEO_RATE (trans));
1132 gst_video_rate_set_property (GObject * object,
1133 guint prop_id, const GValue * value, GParamSpec * pspec)
1135 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1137 GST_OBJECT_LOCK (videorate);
1140 videorate->silent = g_value_get_boolean (value);
1143 videorate->new_pref = g_value_get_double (value);
1145 case PROP_SKIP_TO_FIRST:
1146 videorate->skip_to_first = g_value_get_boolean (value);
1148 case PROP_DROP_ONLY:
1149 videorate->drop_only = g_value_get_boolean (value);
1152 case PROP_AVERAGE_PERIOD:
1153 videorate->average_period_set = g_value_get_uint64 (value);
1156 g_atomic_int_set (&videorate->max_rate, g_value_get_int (value));
1160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1163 GST_OBJECT_UNLOCK (videorate);
1167 GST_OBJECT_UNLOCK (videorate);
1168 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (videorate));
1172 gst_video_rate_get_property (GObject * object,
1173 guint prop_id, GValue * value, GParamSpec * pspec)
1175 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1177 GST_OBJECT_LOCK (videorate);
1180 g_value_set_uint64 (value, videorate->in);
1183 g_value_set_uint64 (value, videorate->out);
1186 g_value_set_uint64 (value, videorate->dup);
1189 g_value_set_uint64 (value, videorate->drop);
1192 g_value_set_boolean (value, videorate->silent);
1195 g_value_set_double (value, videorate->new_pref);
1197 case PROP_SKIP_TO_FIRST:
1198 g_value_set_boolean (value, videorate->skip_to_first);
1200 case PROP_DROP_ONLY:
1201 g_value_set_boolean (value, videorate->drop_only);
1203 case PROP_AVERAGE_PERIOD:
1204 g_value_set_uint64 (value, videorate->average_period_set);
1207 g_value_set_int (value, g_atomic_int_get (&videorate->max_rate));
1210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1213 GST_OBJECT_UNLOCK (videorate);
1217 plugin_init (GstPlugin * plugin)
1219 GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
1220 "VideoRate stream fixer");
1222 return gst_element_register (plugin, "videorate", GST_RANK_NONE,
1223 GST_TYPE_VIDEO_RATE);
1226 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1229 "Adjusts video frames",
1230 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)