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-yuv,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 v4lsrc ! videorate ! video/x-raw-yuv,framerate=25/2 ! theoraenc ! oggmux ! filesink location=v4l.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
92 #define DEFAULT_FORCE_FPS_N -1
93 #define DEFAULT_FORCE_FPS_D 1
112 static GstStaticPadTemplate gst_video_rate_src_template =
113 GST_STATIC_PAD_TEMPLATE ("src",
116 GST_STATIC_CAPS ("video/x-raw-yuv;"
117 "video/x-raw-rgb;" "video/x-raw-gray;" "image/jpeg;" "image/png")
120 static GstStaticPadTemplate gst_video_rate_sink_template =
121 GST_STATIC_PAD_TEMPLATE ("sink",
124 GST_STATIC_CAPS ("video/x-raw-yuv;"
125 "video/x-raw-rgb;" "video/x-raw-gray;" "image/jpeg;" "image/png")
128 static void gst_video_rate_swap_prev (GstVideoRate * videorate,
129 GstBuffer * buffer, gint64 time);
130 static gboolean gst_video_rate_event (GstBaseTransform * trans,
132 static gboolean gst_video_rate_query (GstBaseTransform * trans,
133 GstPadDirection direction, GstQuery * query);
135 static gboolean gst_video_rate_setcaps (GstBaseTransform * trans,
136 GstCaps * in_caps, GstCaps * out_caps);
138 static GstCaps *gst_video_rate_transform_caps (GstBaseTransform * trans,
139 GstPadDirection direction, GstCaps * caps);
141 static void gst_video_rate_fixate_caps (GstBaseTransform * trans,
142 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
144 static GstFlowReturn gst_video_rate_prepare_output_buffer (GstBaseTransform *
145 trans, GstBuffer * input, gint size, GstCaps * caps, GstBuffer ** buf);
146 static GstFlowReturn gst_video_rate_transform_ip (GstBaseTransform * trans,
149 static gboolean gst_video_rate_start (GstBaseTransform * trans);
150 static gboolean gst_video_rate_stop (GstBaseTransform * trans);
153 static void gst_video_rate_set_property (GObject * object,
154 guint prop_id, const GValue * value, GParamSpec * pspec);
155 static void gst_video_rate_get_property (GObject * object,
156 guint prop_id, GValue * value, GParamSpec * pspec);
158 static GParamSpec *pspec_drop = NULL;
159 static GParamSpec *pspec_duplicate = NULL;
161 GST_BOILERPLATE (GstVideoRate, gst_video_rate,
162 GstBaseTransform, GST_TYPE_BASE_TRANSFORM);
165 gst_video_rate_base_init (gpointer g_class)
167 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
169 gst_element_class_set_details_simple (element_class,
170 "Video rate adjuster", "Filter/Effect/Video",
171 "Drops/duplicates/adjusts timestamps on video frames to make a perfect stream",
172 "Wim Taymans <wim@fluendo.com>");
174 gst_element_class_add_static_pad_template (element_class,
175 &gst_video_rate_sink_template);
176 gst_element_class_add_static_pad_template (element_class,
177 &gst_video_rate_src_template);
181 gst_video_rate_class_init (GstVideoRateClass * klass)
183 GObjectClass *object_class = G_OBJECT_CLASS (klass);
184 GstBaseTransformClass *base_class = GST_BASE_TRANSFORM_CLASS (klass);
186 parent_class = g_type_class_peek_parent (klass);
188 object_class->set_property = gst_video_rate_set_property;
189 object_class->get_property = gst_video_rate_get_property;
191 base_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_rate_setcaps);
192 base_class->transform_caps =
193 GST_DEBUG_FUNCPTR (gst_video_rate_transform_caps);
194 base_class->transform_ip = GST_DEBUG_FUNCPTR (gst_video_rate_transform_ip);
195 base_class->prepare_output_buffer =
196 GST_DEBUG_FUNCPTR (gst_video_rate_prepare_output_buffer);
197 base_class->event = GST_DEBUG_FUNCPTR (gst_video_rate_event);
198 base_class->start = GST_DEBUG_FUNCPTR (gst_video_rate_start);
199 base_class->stop = GST_DEBUG_FUNCPTR (gst_video_rate_stop);
200 base_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_video_rate_fixate_caps);
201 base_class->query = GST_DEBUG_FUNCPTR (gst_video_rate_query);
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.
233 g_object_class_install_property (object_class, PROP_SKIP_TO_FIRST,
234 g_param_spec_boolean ("skip-to-first", "Skip to first buffer",
235 "Don't produce buffers before the first one we receive",
236 DEFAULT_SKIP_TO_FIRST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 * GstVideoRate:drop-only:
241 * Only drop frames, no duplicates are produced.
245 g_object_class_install_property (object_class, PROP_DROP_ONLY,
246 g_param_spec_boolean ("drop-only", "Only Drop",
247 "Only drop frames, no duplicates are produced",
248 DEFAULT_DROP_ONLY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
251 * GstVideoRate:average-period:
253 * Arrange for maximum framerate by dropping frames beyond a certain framerate,
254 * where the framerate is calculated using a moving average over the
259 g_object_class_install_property (object_class, PROP_AVERAGE_PERIOD,
260 g_param_spec_uint64 ("average-period", "Period over which to average",
261 "Period over which to average the framerate (in ns) (0 = disabled)",
262 0, G_MAXINT64, DEFAULT_AVERAGE_PERIOD,
263 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 * GstVideoRate:max-rate:
268 * maximum framerate to pass through
272 g_object_class_install_property (object_class, PROP_MAX_RATE,
273 g_param_spec_int ("max-rate", "maximum framerate",
274 "Maximum framerate allowed to pass through "
275 "(in frames per second, implies drop-only)",
276 1, G_MAXINT, DEFAULT_MAX_RATE,
277 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
280 * GstVideoRate:force-fps:
282 * Forced output framerate
286 g_object_class_install_property (object_class, PROP_FORCE_FPS,
287 gst_param_spec_fraction ("force-fps", "Force output framerate",
288 "Force output framerate (negative means negotiate via caps)",
289 -1, 1, G_MAXINT, 1, DEFAULT_FORCE_FPS_N, DEFAULT_FORCE_FPS_D,
290 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
294 gst_value_fraction_get_extremes (const GValue * v,
295 gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
297 if (GST_VALUE_HOLDS_FRACTION (v)) {
298 *min_num = *max_num = gst_value_get_fraction_numerator (v);
299 *min_denom = *max_denom = gst_value_get_fraction_denominator (v);
300 } else if (GST_VALUE_HOLDS_FRACTION_RANGE (v)) {
301 const GValue *min, *max;
303 min = gst_value_get_fraction_range_min (v);
304 *min_num = gst_value_get_fraction_numerator (min);
305 *min_denom = gst_value_get_fraction_denominator (min);
307 max = gst_value_get_fraction_range_max (v);
308 *max_num = gst_value_get_fraction_numerator (max);
309 *max_denom = gst_value_get_fraction_denominator (max);
310 } else if (GST_VALUE_HOLDS_LIST (v)) {
311 gint min_n = G_MAXINT, min_d = 1, max_n = 0, max_d = 1;
319 n = gst_value_list_get_size (v);
323 for (i = 0; i < n; i++) {
324 const GValue *t = gst_value_list_get_value (v, i);
326 gst_value_fraction_get_extremes (t, &min_n, &min_d, &max_n, &max_d);
327 if (gst_util_fraction_compare (min_n, min_d, *min_num, *min_denom) < 0) {
332 if (gst_util_fraction_compare (max_n, max_d, *max_num, *max_denom) > 0) {
338 g_warning ("Unknown type for framerate");
346 /* Clamp the framerate in a caps structure to be a smaller range then
347 * [1...max_rate], otherwise return false */
349 gst_video_max_rate_clamp_structure (GstStructure * s, gint maxrate,
350 gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
352 gboolean ret = FALSE;
354 if (!gst_structure_has_field (s, "framerate")) {
355 /* No framerate field implies any framerate, clamping would result in
356 * [1..max_rate] so not a real subset */
360 GValue intersection = { 0, };
361 GValue clamp = { 0, };
362 gint tmp_num, tmp_denom;
364 g_value_init (&clamp, GST_TYPE_FRACTION_RANGE);
365 gst_value_set_fraction_range_full (&clamp, 0, 1, maxrate, 1);
367 v = gst_structure_get_value (s, "framerate");
368 ret = gst_value_intersect (&intersection, v, &clamp);
369 g_value_unset (&clamp);
374 gst_value_fraction_get_extremes (&intersection,
375 min_num, min_denom, max_num, max_denom);
377 gst_value_fraction_get_extremes (v,
378 &tmp_num, &tmp_denom, max_num, max_denom);
380 if (gst_util_fraction_compare (*max_num, *max_denom, maxrate, 1) > 0) {
385 gst_structure_take_value (s, "framerate", &intersection);
393 gst_video_rate_transform_caps (GstBaseTransform * trans,
394 GstPadDirection direction, GstCaps * caps)
396 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
398 GstStructure *s, *s2;
399 GstStructure *s3 = NULL;
400 int maxrate = g_atomic_int_get (&videorate->max_rate);
402 /* Should always be called with simple caps */
403 g_return_val_if_fail (GST_CAPS_IS_SIMPLE (caps), NULL);
405 ret = gst_caps_copy (caps);
407 s = gst_caps_get_structure (ret, 0);
408 s2 = gst_structure_copy (s);
410 if (videorate->force_fps_n >= 0 && videorate->force_fps_d >= 0) {
411 if (direction == GST_PAD_SINK) {
412 gst_caps_remove_structure (ret, 0);
413 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION,
414 videorate->force_fps_n, videorate->force_fps_d, NULL);
416 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
419 } else if (videorate->drop_only) {
420 gint min_num = 0, min_denom = 1;
421 gint max_num = G_MAXINT, max_denom = 1;
423 /* Clamp the caps to our maximum rate as the first caps if possible */
424 if (!gst_video_max_rate_clamp_structure (s, maxrate,
425 &min_num, &min_denom, &max_num, &max_denom)) {
431 /* clamp wouldn't be a real subset of 1..maxrate, in this case the sink
432 * caps should become [1..maxrate], [1..maxint] and the src caps just
433 * [1..maxrate]. In case there was a caps incompatibility things will
434 * explode later as appropriate :)
436 * In case [X..maxrate] == [X..maxint], skip as we'll set it later
438 if (direction == GST_PAD_SRC && maxrate != G_MAXINT)
439 gst_structure_set (s, "framerate", GST_TYPE_FRACTION_RANGE,
440 min_num, min_denom, maxrate, 1, NULL);
442 gst_caps_remove_structure (ret, 0);
445 if (direction == GST_PAD_SRC) {
446 /* We can accept anything as long as it's at least the minimal framerate
447 * the the sink needs */
448 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE,
449 min_num, min_denom, G_MAXINT, 1, NULL);
451 /* Also allow unknown framerate, if it isn't already */
452 if (min_num != 0 || min_denom != 1) {
453 s3 = gst_structure_copy (s);
454 gst_structure_set (s3, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
456 } else if (max_num != 0 || max_denom != 1) {
457 /* We can provide everything upto the maximum framerate at the src */
458 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE,
459 0, 1, max_num, max_denom, NULL);
461 } else if (direction == GST_PAD_SINK) {
462 gint min_num = 0, min_denom = 1;
463 gint max_num = G_MAXINT, max_denom = 1;
465 if (!gst_video_max_rate_clamp_structure (s, maxrate,
466 &min_num, &min_denom, &max_num, &max_denom))
467 gst_caps_remove_structure (ret, 0);
469 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
472 /* set the framerate as a range */
473 gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
477 gst_caps_merge_structure (ret, s2);
479 gst_caps_merge_structure (ret, s3);
485 gst_video_rate_fixate_caps (GstBaseTransform * trans,
486 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
491 s = gst_caps_get_structure (caps, 0);
492 if (G_UNLIKELY (!gst_structure_get_fraction (s, "framerate", &num, &denom)))
495 s = gst_caps_get_structure (othercaps, 0);
496 gst_structure_fixate_field_nearest_fraction (s, "framerate", num, denom);
500 gst_video_rate_setcaps (GstBaseTransform * trans, GstCaps * in_caps,
503 GstVideoRate *videorate;
504 GstStructure *structure;
506 gint rate_numerator, rate_denominator;
508 videorate = GST_VIDEO_RATE (trans);
510 GST_DEBUG_OBJECT (trans, "setcaps called in: %" GST_PTR_FORMAT
511 " out: %" GST_PTR_FORMAT, in_caps, out_caps);
513 structure = gst_caps_get_structure (in_caps, 0);
514 if (!gst_structure_get_fraction (structure, "framerate",
515 &rate_numerator, &rate_denominator))
518 videorate->from_rate_numerator = rate_numerator;
519 videorate->from_rate_denominator = rate_denominator;
521 structure = gst_caps_get_structure (out_caps, 0);
522 if (!gst_structure_get_fraction (structure, "framerate",
523 &rate_numerator, &rate_denominator))
526 /* out_frame_count is scaled by the frame rate caps when calculating next_ts.
527 * when the frame rate caps change, we must update base_ts and reset
529 if (videorate->to_rate_numerator) {
530 videorate->base_ts +=
531 gst_util_uint64_scale (videorate->out_frame_count,
532 videorate->to_rate_denominator * GST_SECOND,
533 videorate->to_rate_numerator);
535 videorate->out_frame_count = 0;
536 videorate->to_rate_numerator = rate_numerator;
537 videorate->to_rate_denominator = rate_denominator;
540 videorate->wanted_diff = gst_util_uint64_scale_int (GST_SECOND,
541 rate_denominator, rate_numerator);
543 videorate->wanted_diff = 0;
546 /* After a setcaps, our caps may have changed. In that case, we can't use
547 * the old buffer, if there was one (it might have different dimensions) */
548 GST_DEBUG_OBJECT (videorate, "swapping old buffers");
549 gst_video_rate_swap_prev (videorate, NULL, GST_CLOCK_TIME_NONE);
550 videorate->last_ts = GST_CLOCK_TIME_NONE;
551 videorate->average = 0;
557 GST_DEBUG_OBJECT (videorate, "no framerate specified");
564 gst_video_rate_reset (GstVideoRate * videorate)
566 GST_DEBUG_OBJECT (videorate, "resetting internal variables");
570 videorate->base_ts = 0;
571 videorate->out_frame_count = 0;
574 videorate->next_ts = GST_CLOCK_TIME_NONE;
575 videorate->last_ts = GST_CLOCK_TIME_NONE;
576 videorate->discont = TRUE;
577 videorate->average = 0;
578 gst_video_rate_swap_prev (videorate, NULL, 0);
580 gst_segment_init (&videorate->segment, GST_FORMAT_TIME);
584 gst_video_rate_init (GstVideoRate * videorate, GstVideoRateClass * klass)
586 gst_video_rate_reset (videorate);
587 videorate->silent = DEFAULT_SILENT;
588 videorate->new_pref = DEFAULT_NEW_PREF;
589 videorate->drop_only = DEFAULT_DROP_ONLY;
590 videorate->average_period = DEFAULT_AVERAGE_PERIOD;
591 videorate->average_period_set = DEFAULT_AVERAGE_PERIOD;
592 videorate->max_rate = DEFAULT_MAX_RATE;
593 videorate->force_fps_n = DEFAULT_FORCE_FPS_N;
594 videorate->force_fps_d = DEFAULT_FORCE_FPS_D;
596 videorate->from_rate_numerator = 0;
597 videorate->from_rate_denominator = 0;
598 videorate->to_rate_numerator = 0;
599 videorate->to_rate_denominator = 0;
601 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (videorate), TRUE);
604 /* flush the oldest buffer */
606 gst_video_rate_flush_prev (GstVideoRate * videorate, gboolean duplicate)
610 GstClockTime push_ts;
612 if (!videorate->prevbuf)
613 goto eos_before_buffers;
615 /* make sure we can write to the metadata */
616 outbuf = gst_buffer_make_metadata_writable
617 (gst_buffer_ref (videorate->prevbuf));
619 GST_BUFFER_OFFSET (outbuf) = videorate->out;
620 GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
622 if (videorate->discont) {
623 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
624 videorate->discont = FALSE;
626 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
629 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
631 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
633 /* this is the timestamp we put on the buffer */
634 push_ts = videorate->next_ts;
637 videorate->out_frame_count++;
638 if (videorate->to_rate_numerator) {
639 /* interpolate next expected timestamp in the segment */
641 videorate->segment.accum + videorate->segment.start +
642 videorate->base_ts + gst_util_uint64_scale (videorate->out_frame_count,
643 videorate->to_rate_denominator * GST_SECOND,
644 videorate->to_rate_numerator);
645 GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
648 /* We do not need to update time in VFR (variable frame rate) mode */
649 if (!videorate->drop_only) {
650 /* adapt for looping, bring back to time in current segment. */
651 GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.accum;
654 GST_LOG_OBJECT (videorate,
655 "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
656 GST_TIME_ARGS (push_ts));
658 res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (videorate), outbuf);
665 GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
671 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
674 GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
675 if (videorate->prevbuf)
676 gst_buffer_unref (videorate->prevbuf);
677 videorate->prevbuf = buffer != NULL ? gst_buffer_ref (buffer) : NULL;
678 videorate->prev_ts = time;
682 gst_video_rate_notify_drop (GstVideoRate * videorate)
684 #if !GLIB_CHECK_VERSION(2,26,0)
685 g_object_notify ((GObject *) videorate, "drop");
687 g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
692 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
694 #if !GLIB_CHECK_VERSION(2,26,0)
695 g_object_notify ((GObject *) videorate, "duplicate");
697 g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
701 #define MAGIC_LIMIT 25
703 gst_video_rate_event (GstBaseTransform * trans, GstEvent * event)
705 GstVideoRate *videorate;
707 videorate = GST_VIDEO_RATE (trans);
709 switch (GST_EVENT_TYPE (event)) {
710 case GST_EVENT_NEWSEGMENT:
712 gint64 start, stop, time;
717 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
718 &start, &stop, &time);
720 if (format != GST_FORMAT_TIME)
723 GST_DEBUG_OBJECT (videorate, "handle NEWSEGMENT");
725 /* close up the previous segment, if appropriate */
726 if (!update && videorate->prevbuf) {
731 /* fill up to the end of current segment,
732 * or only send out the stored buffer if there is no specific stop.
733 * regardless, prevent going loopy in strange cases */
734 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
735 ((GST_CLOCK_TIME_IS_VALID (videorate->segment.stop) &&
736 videorate->next_ts - videorate->segment.accum
737 < videorate->segment.stop)
739 res = gst_video_rate_flush_prev (videorate, count > 0);
743 videorate->dup += count - 1;
744 if (!videorate->silent)
745 gst_video_rate_notify_duplicate (videorate);
746 } else if (count == 0) {
748 if (!videorate->silent)
749 gst_video_rate_notify_drop (videorate);
751 /* clean up for the new one; _chain will resume from the new start */
752 videorate->base_ts = 0;
753 videorate->out_frame_count = 0;
754 gst_video_rate_swap_prev (videorate, NULL, 0);
755 videorate->next_ts = GST_CLOCK_TIME_NONE;
758 /* We just want to update the accumulated stream_time */
759 gst_segment_set_newsegment_full (&videorate->segment, update, rate, arate,
760 format, start, stop, time);
762 GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
763 &videorate->segment);
768 GstFlowReturn res = GST_FLOW_OK;
770 GST_DEBUG_OBJECT (videorate, "Got EOS");
772 /* If the segment has a stop position, fill the segment */
773 if (GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
774 /* fill up to the end of current segment,
775 * or only send out the stored buffer if there is no specific stop.
776 * regardless, prevent going loopy in strange cases */
777 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
778 ((videorate->next_ts - videorate->segment.accum <
779 videorate->segment.stop)
781 res = gst_video_rate_flush_prev (videorate, count > 0);
784 } else if (videorate->prevbuf) {
785 /* Output at least one frame but if the buffer duration is valid, output
786 * enough frames to use the complete buffer duration */
787 if (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf)) {
788 GstClockTime end_ts =
789 videorate->next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
791 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
792 ((videorate->next_ts - videorate->segment.accum < end_ts)
794 res = gst_video_rate_flush_prev (videorate, count > 0);
798 res = gst_video_rate_flush_prev (videorate, FALSE);
804 videorate->dup += count - 1;
805 if (!videorate->silent)
806 gst_video_rate_notify_duplicate (videorate);
807 } else if (count == 0) {
809 if (!videorate->silent)
810 gst_video_rate_notify_drop (videorate);
815 case GST_EVENT_FLUSH_STOP:
816 /* also resets the segment */
817 GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
818 gst_video_rate_reset (videorate);
829 GST_WARNING_OBJECT (videorate,
830 "Got segment but doesn't have GST_FORMAT_TIME value");
836 gst_video_rate_query (GstBaseTransform * trans, GstPadDirection direction,
839 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
840 gboolean res = FALSE;
843 otherpad = (direction == GST_PAD_SRC) ?
844 GST_BASE_TRANSFORM_SINK_PAD (trans) : GST_BASE_TRANSFORM_SRC_PAD (trans);
846 switch (GST_QUERY_TYPE (query)) {
847 case GST_QUERY_LATENCY:
849 GstClockTime min, max;
855 GST_OBJECT_LOCK (videorate);
856 avg_period = videorate->average_period_set;
857 GST_OBJECT_UNLOCK (videorate);
859 if (avg_period == 0 && (peer = gst_pad_get_peer (otherpad))) {
860 if ((res = gst_pad_query (peer, query))) {
861 gst_query_parse_latency (query, &live, &min, &max);
863 GST_DEBUG_OBJECT (videorate, "Peer latency: min %"
864 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
865 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
867 if (videorate->from_rate_numerator != 0) {
868 /* add latency. We don't really know since we hold on to the frames
869 * until we get a next frame, which can be anything. We assume
870 * however that this will take from_rate time. */
871 latency = gst_util_uint64_scale (GST_SECOND,
872 videorate->from_rate_denominator,
873 videorate->from_rate_numerator);
875 /* no input framerate, we don't know */
879 GST_DEBUG_OBJECT (videorate, "Our latency: %"
880 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
886 GST_DEBUG_OBJECT (videorate, "Calculated total latency : min %"
887 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
888 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
890 gst_query_set_latency (query, live, min, max);
892 gst_object_unref (peer);
895 /* Simple fallthrough if we don't have a latency or not a peer that we
896 * can't ask about its latency yet.. */
899 res = parent_class->query (trans, direction, query);
907 gst_video_rate_trans_ip_max_avg (GstVideoRate * videorate, GstBuffer * buf)
909 GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
913 if (!GST_CLOCK_TIME_IS_VALID (ts) || videorate->wanted_diff == 0)
916 /* drop frames if they exceed our output rate */
917 if (GST_CLOCK_TIME_IS_VALID (videorate->last_ts)) {
918 GstClockTimeDiff diff = ts - videorate->last_ts;
920 /* Drop buffer if its early compared to the desired frame rate and
921 * the current average is higher than the desired average
923 if (diff < videorate->wanted_diff &&
924 videorate->average < videorate->wanted_diff)
928 if (videorate->average) {
929 GstClockTimeDiff wanted_diff;
931 if (G_LIKELY (videorate->average_period > videorate->wanted_diff))
932 wanted_diff = videorate->wanted_diff;
934 wanted_diff = videorate->average_period * 10;
937 gst_util_uint64_scale_round (videorate->average,
938 videorate->average_period - wanted_diff,
939 videorate->average_period) +
940 gst_util_uint64_scale_round (diff, wanted_diff,
941 videorate->average_period);
943 videorate->average = diff;
947 videorate->last_ts = ts;
954 if (!videorate->silent)
955 gst_video_rate_notify_drop (videorate);
956 return GST_BASE_TRANSFORM_FLOW_DROPPED;
960 gst_video_rate_prepare_output_buffer (GstBaseTransform * trans,
961 GstBuffer * input, gint size, GstCaps * caps, GstBuffer ** buf)
963 if (gst_buffer_is_metadata_writable (input)) {
964 gst_buffer_set_caps (input, caps);
965 *buf = gst_buffer_ref (input);
967 *buf = gst_buffer_create_sub (input, 0, GST_BUFFER_SIZE (input));
968 gst_buffer_set_caps (*buf, caps);
975 gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
977 GstVideoRate *videorate;
978 GstFlowReturn res = GST_BASE_TRANSFORM_FLOW_DROPPED;
979 GstClockTime intime, in_ts, in_dur;
980 GstClockTime avg_period;
981 gboolean skip = FALSE;
983 videorate = GST_VIDEO_RATE (trans);
985 /* make sure the denominators are not 0 */
986 if (videorate->from_rate_denominator == 0 ||
987 videorate->to_rate_denominator == 0)
990 GST_OBJECT_LOCK (videorate);
991 avg_period = videorate->average_period_set;
992 GST_OBJECT_UNLOCK (videorate);
994 /* MT-safe switching between modes */
995 if (G_UNLIKELY (avg_period != videorate->average_period)) {
996 gboolean switch_mode = (avg_period == 0 || videorate->average_period == 0);
997 videorate->average_period = avg_period;
998 videorate->last_ts = GST_CLOCK_TIME_NONE;
1002 /* enabling average mode */
1003 videorate->average = 0;
1004 /* make sure no cached buffers from regular mode are left */
1005 gst_video_rate_swap_prev (videorate, NULL, 0);
1007 /* enable regular mode */
1008 videorate->next_ts = GST_CLOCK_TIME_NONE;
1012 /* max averaging mode has a no latency, normal mode does */
1013 gst_element_post_message (GST_ELEMENT (videorate),
1014 gst_message_new_latency (GST_OBJECT (videorate)));
1018 if (videorate->average_period > 0)
1019 return gst_video_rate_trans_ip_max_avg (videorate, buffer);
1021 in_ts = GST_BUFFER_TIMESTAMP (buffer);
1022 in_dur = GST_BUFFER_DURATION (buffer);
1024 if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE)) {
1025 in_ts = videorate->last_ts;
1026 if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE))
1027 goto invalid_buffer;
1030 /* get the time of the next expected buffer timestamp, we use this when the
1031 * next buffer has -1 as a timestamp */
1032 videorate->last_ts = in_ts;
1033 if (in_dur != GST_CLOCK_TIME_NONE)
1034 videorate->last_ts += in_dur;
1036 GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
1037 GST_TIME_ARGS (in_ts));
1039 /* the input time is the time in the segment + all previously accumulated
1041 intime = in_ts + videorate->segment.accum;
1043 /* we need to have two buffers to compare */
1044 if (videorate->prevbuf == NULL) {
1045 gst_video_rate_swap_prev (videorate, buffer, intime);
1047 if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts)) {
1048 /* new buffer, we expect to output a buffer that matches the first
1049 * timestamp in the segment */
1050 if (videorate->skip_to_first || skip) {
1051 videorate->next_ts = intime;
1052 videorate->base_ts = in_ts - videorate->segment.start;
1053 videorate->out_frame_count = 0;
1055 videorate->next_ts =
1056 videorate->segment.start + videorate->segment.accum;
1060 GstClockTime prevtime;
1062 gint64 diff1, diff2;
1064 prevtime = videorate->prev_ts;
1066 GST_LOG_OBJECT (videorate,
1067 "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
1068 " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
1069 GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
1073 /* drop new buffer if it's before previous one */
1074 if (intime < prevtime) {
1075 GST_DEBUG_OBJECT (videorate,
1076 "The new buffer (%" GST_TIME_FORMAT
1077 ") is before the previous buffer (%"
1078 GST_TIME_FORMAT "). Dropping new buffer.",
1079 GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
1081 if (!videorate->silent)
1082 gst_video_rate_notify_drop (videorate);
1086 /* got 2 buffers, see which one is the best */
1089 diff1 = prevtime - videorate->next_ts;
1090 diff2 = intime - videorate->next_ts;
1092 /* take absolute values, beware: abs and ABS don't work for gint64 */
1098 GST_LOG_OBJECT (videorate,
1099 "diff with prev %" GST_TIME_FORMAT " diff with new %"
1100 GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
1101 GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
1102 GST_TIME_ARGS (videorate->next_ts));
1104 /* output first one when its the best */
1105 if (diff1 <= diff2) {
1109 /* on error the _flush function posted a warning already */
1110 if ((r = gst_video_rate_flush_prev (videorate,
1111 count > 1)) != GST_FLOW_OK) {
1117 /* Do not produce any dups. We can exit loop now */
1118 if (videorate->drop_only)
1120 /* continue while the first one was the best, if they were equal avoid
1121 * going into an infinite loop */
1123 while (diff1 < diff2);
1125 /* if we outputed the first buffer more then once, we have dups */
1127 videorate->dup += count - 1;
1128 if (!videorate->silent)
1129 gst_video_rate_notify_duplicate (videorate);
1131 /* if we didn't output the first buffer, we have a drop */
1132 else if (count == 0) {
1135 if (!videorate->silent)
1136 gst_video_rate_notify_drop (videorate);
1138 GST_LOG_OBJECT (videorate,
1139 "new is best, old never used, drop, outgoing ts %"
1140 GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
1142 GST_LOG_OBJECT (videorate,
1143 "END, putting new in old, diff1 %" GST_TIME_FORMAT
1144 ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
1145 ", in %" G_GUINT64_FORMAT ", out %" G_GUINT64_FORMAT ", drop %"
1146 G_GUINT64_FORMAT ", dup %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff1),
1147 GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
1148 videorate->in, videorate->out, videorate->drop, videorate->dup);
1150 /* swap in new one when it's the best */
1151 gst_video_rate_swap_prev (videorate, buffer, intime);
1159 GST_WARNING_OBJECT (videorate, "no framerate negotiated");
1160 res = GST_FLOW_NOT_NEGOTIATED;
1166 GST_WARNING_OBJECT (videorate,
1167 "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
1168 res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1174 gst_video_rate_start (GstBaseTransform * trans)
1176 gst_video_rate_reset (GST_VIDEO_RATE (trans));
1181 gst_video_rate_stop (GstBaseTransform * trans)
1183 gst_video_rate_reset (GST_VIDEO_RATE (trans));
1188 gst_video_rate_set_property (GObject * object,
1189 guint prop_id, const GValue * value, GParamSpec * pspec)
1191 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1193 GST_OBJECT_LOCK (videorate);
1196 videorate->silent = g_value_get_boolean (value);
1199 videorate->new_pref = g_value_get_double (value);
1201 case PROP_SKIP_TO_FIRST:
1202 videorate->skip_to_first = g_value_get_boolean (value);
1204 case PROP_DROP_ONLY:
1205 videorate->drop_only = g_value_get_boolean (value);
1208 case PROP_AVERAGE_PERIOD:
1209 videorate->average_period_set = g_value_get_uint64 (value);
1212 g_atomic_int_set (&videorate->max_rate, g_value_get_int (value));
1215 case PROP_FORCE_FPS:
1216 videorate->force_fps_n = gst_value_get_fraction_numerator (value);
1217 videorate->force_fps_d = gst_value_get_fraction_denominator (value);
1221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1224 GST_OBJECT_UNLOCK (videorate);
1228 GST_OBJECT_UNLOCK (videorate);
1229 gst_base_transform_reconfigure (GST_BASE_TRANSFORM (videorate));
1233 gst_video_rate_get_property (GObject * object,
1234 guint prop_id, GValue * value, GParamSpec * pspec)
1236 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1238 GST_OBJECT_LOCK (videorate);
1241 g_value_set_uint64 (value, videorate->in);
1244 g_value_set_uint64 (value, videorate->out);
1247 g_value_set_uint64 (value, videorate->dup);
1250 g_value_set_uint64 (value, videorate->drop);
1253 g_value_set_boolean (value, videorate->silent);
1256 g_value_set_double (value, videorate->new_pref);
1258 case PROP_SKIP_TO_FIRST:
1259 g_value_set_boolean (value, videorate->skip_to_first);
1261 case PROP_DROP_ONLY:
1262 g_value_set_boolean (value, videorate->drop_only);
1264 case PROP_AVERAGE_PERIOD:
1265 g_value_set_uint64 (value, videorate->average_period_set);
1268 g_value_set_int (value, g_atomic_int_get (&videorate->max_rate));
1270 case PROP_FORCE_FPS:
1271 gst_value_set_fraction (value, videorate->force_fps_n,
1272 videorate->force_fps_d);
1275 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1278 GST_OBJECT_UNLOCK (videorate);
1282 plugin_init (GstPlugin * plugin)
1284 GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
1285 "VideoRate stream fixer");
1287 return gst_element_register (plugin, "videorate", GST_RANK_NONE,
1288 GST_TYPE_VIDEO_RATE);
1291 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1294 "Adjusts video frames",
1295 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)