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;
611 #ifdef VIDEORATE_MODIFICATION
612 GstStructure *structure;
616 if (!videorate->prevbuf)
617 goto eos_before_buffers;
619 /* make sure we can write to the metadata */
620 #ifdef VIDEORATE_MODIFICATION
621 buf_caps = gst_buffer_get_caps (videorate->prevbuf);
623 GST_ERROR("not able to get the caps");
624 return GST_FLOW_ERROR;
626 structure = gst_caps_get_structure (buf_caps, 0);
628 GST_ERROR("not able to get the structure");
629 return GST_FLOW_ERROR;
631 if (!gst_structure_get_fourcc (structure, "format", &format)) {
632 GST_ERROR ("can not get format in gst structure");
634 if(format== GST_MAKE_FOURCC('S', 'T', '1', '2') || format== GST_MAKE_FOURCC('S', 'N', '1', '2')) {
635 GST_ERROR("setting the format_transcode as TRUE");
636 outbuf=videorate->prevbuf;
637 gst_caps_unref(buf_caps);
639 outbuf = gst_buffer_make_metadata_writable
640 (gst_buffer_ref (videorate->prevbuf));
643 GST_BUFFER_OFFSET (outbuf) = videorate->out;
644 GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
646 if (videorate->discont) {
647 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
648 videorate->discont = FALSE;
650 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
653 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
655 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
657 /* this is the timestamp we put on the buffer */
658 push_ts = videorate->next_ts;
661 videorate->out_frame_count++;
662 if (videorate->to_rate_numerator) {
663 /* interpolate next expected timestamp in the segment */
665 videorate->segment.accum + videorate->segment.start +
666 videorate->base_ts + gst_util_uint64_scale (videorate->out_frame_count,
667 videorate->to_rate_denominator * GST_SECOND,
668 videorate->to_rate_numerator);
669 GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
672 /* We do not need to update time in VFR (variable frame rate) mode */
673 if (!videorate->drop_only) {
674 /* adapt for looping, bring back to time in current segment. */
675 GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.accum;
678 GST_LOG_OBJECT (videorate,
679 "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
680 GST_TIME_ARGS (push_ts));
682 res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (videorate), outbuf);
689 GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
695 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
698 GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
699 if (videorate->prevbuf)
700 gst_buffer_unref (videorate->prevbuf);
701 videorate->prevbuf = buffer != NULL ? gst_buffer_ref (buffer) : NULL;
702 videorate->prev_ts = time;
706 gst_video_rate_notify_drop (GstVideoRate * videorate)
708 #if !GLIB_CHECK_VERSION(2,26,0)
709 g_object_notify ((GObject *) videorate, "drop");
711 g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
716 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
718 #if !GLIB_CHECK_VERSION(2,26,0)
719 g_object_notify ((GObject *) videorate, "duplicate");
721 g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
725 #define MAGIC_LIMIT 25
727 gst_video_rate_event (GstBaseTransform * trans, GstEvent * event)
729 GstVideoRate *videorate;
731 videorate = GST_VIDEO_RATE (trans);
733 switch (GST_EVENT_TYPE (event)) {
734 case GST_EVENT_NEWSEGMENT:
736 gint64 start, stop, time;
741 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
742 &start, &stop, &time);
744 if (format != GST_FORMAT_TIME)
747 GST_DEBUG_OBJECT (videorate, "handle NEWSEGMENT");
749 /* close up the previous segment, if appropriate */
750 if (!update && videorate->prevbuf) {
755 /* fill up to the end of current segment,
756 * or only send out the stored buffer if there is no specific stop.
757 * regardless, prevent going loopy in strange cases */
758 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
759 ((GST_CLOCK_TIME_IS_VALID (videorate->segment.stop) &&
760 videorate->next_ts - videorate->segment.accum
761 < videorate->segment.stop)
763 res = gst_video_rate_flush_prev (videorate, count > 0);
767 videorate->dup += count - 1;
768 if (!videorate->silent)
769 gst_video_rate_notify_duplicate (videorate);
770 } else if (count == 0) {
772 if (!videorate->silent)
773 gst_video_rate_notify_drop (videorate);
775 /* clean up for the new one; _chain will resume from the new start */
776 videorate->base_ts = 0;
777 videorate->out_frame_count = 0;
778 gst_video_rate_swap_prev (videorate, NULL, 0);
779 videorate->next_ts = GST_CLOCK_TIME_NONE;
782 /* We just want to update the accumulated stream_time */
783 gst_segment_set_newsegment_full (&videorate->segment, update, rate, arate,
784 format, start, stop, time);
786 GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
787 &videorate->segment);
792 GstFlowReturn res = GST_FLOW_OK;
794 GST_DEBUG_OBJECT (videorate, "Got EOS");
796 /* If the segment has a stop position, fill the segment */
797 if (GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
798 /* fill up to the end of current segment,
799 * or only send out the stored buffer if there is no specific stop.
800 * regardless, prevent going loopy in strange cases */
801 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
802 ((videorate->next_ts - videorate->segment.accum <
803 videorate->segment.stop)
805 res = gst_video_rate_flush_prev (videorate, count > 0);
808 } else if (videorate->prevbuf) {
809 /* Output at least one frame but if the buffer duration is valid, output
810 * enough frames to use the complete buffer duration */
811 if (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf)) {
812 GstClockTime end_ts =
813 videorate->next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
815 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
816 ((videorate->next_ts - videorate->segment.accum < end_ts)
818 res = gst_video_rate_flush_prev (videorate, count > 0);
822 res = gst_video_rate_flush_prev (videorate, FALSE);
828 videorate->dup += count - 1;
829 if (!videorate->silent)
830 gst_video_rate_notify_duplicate (videorate);
831 } else if (count == 0) {
833 if (!videorate->silent)
834 gst_video_rate_notify_drop (videorate);
839 case GST_EVENT_FLUSH_STOP:
840 /* also resets the segment */
841 GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
842 gst_video_rate_reset (videorate);
853 GST_WARNING_OBJECT (videorate,
854 "Got segment but doesn't have GST_FORMAT_TIME value");
860 gst_video_rate_query (GstBaseTransform * trans, GstPadDirection direction,
863 GstVideoRate *videorate = GST_VIDEO_RATE (trans);
864 gboolean res = FALSE;
867 otherpad = (direction == GST_PAD_SRC) ?
868 GST_BASE_TRANSFORM_SINK_PAD (trans) : GST_BASE_TRANSFORM_SRC_PAD (trans);
870 switch (GST_QUERY_TYPE (query)) {
871 case GST_QUERY_LATENCY:
873 GstClockTime min, max;
879 GST_OBJECT_LOCK (videorate);
880 avg_period = videorate->average_period_set;
881 GST_OBJECT_UNLOCK (videorate);
883 if (avg_period == 0 && (peer = gst_pad_get_peer (otherpad))) {
884 if ((res = gst_pad_query (peer, query))) {
885 gst_query_parse_latency (query, &live, &min, &max);
887 GST_DEBUG_OBJECT (videorate, "Peer latency: min %"
888 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
889 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
891 if (videorate->from_rate_numerator != 0) {
892 /* add latency. We don't really know since we hold on to the frames
893 * until we get a next frame, which can be anything. We assume
894 * however that this will take from_rate time. */
895 latency = gst_util_uint64_scale (GST_SECOND,
896 videorate->from_rate_denominator,
897 videorate->from_rate_numerator);
899 /* no input framerate, we don't know */
903 GST_DEBUG_OBJECT (videorate, "Our latency: %"
904 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
910 GST_DEBUG_OBJECT (videorate, "Calculated total latency : min %"
911 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
912 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
914 gst_query_set_latency (query, live, min, max);
916 gst_object_unref (peer);
919 /* Simple fallthrough if we don't have a latency or not a peer that we
920 * can't ask about its latency yet.. */
923 res = parent_class->query (trans, direction, query);
931 gst_video_rate_trans_ip_max_avg (GstVideoRate * videorate, GstBuffer * buf)
933 GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
937 if (!GST_CLOCK_TIME_IS_VALID (ts) || videorate->wanted_diff == 0)
940 /* drop frames if they exceed our output rate */
941 if (GST_CLOCK_TIME_IS_VALID (videorate->last_ts)) {
942 GstClockTimeDiff diff = ts - videorate->last_ts;
944 /* Drop buffer if its early compared to the desired frame rate and
945 * the current average is higher than the desired average
947 if (diff < videorate->wanted_diff &&
948 videorate->average < videorate->wanted_diff)
952 if (videorate->average) {
953 GstClockTimeDiff wanted_diff;
955 if (G_LIKELY (videorate->average_period > videorate->wanted_diff))
956 wanted_diff = videorate->wanted_diff;
958 wanted_diff = videorate->average_period * 10;
961 gst_util_uint64_scale_round (videorate->average,
962 videorate->average_period - wanted_diff,
963 videorate->average_period) +
964 gst_util_uint64_scale_round (diff, wanted_diff,
965 videorate->average_period);
967 videorate->average = diff;
971 videorate->last_ts = ts;
978 if (!videorate->silent)
979 gst_video_rate_notify_drop (videorate);
980 return GST_BASE_TRANSFORM_FLOW_DROPPED;
984 gst_video_rate_prepare_output_buffer (GstBaseTransform * trans,
985 GstBuffer * input, gint size, GstCaps * caps, GstBuffer ** buf)
987 if (gst_buffer_is_metadata_writable (input)) {
988 gst_buffer_set_caps (input, caps);
989 *buf = gst_buffer_ref (input);
991 *buf = gst_buffer_create_sub (input, 0, GST_BUFFER_SIZE (input));
992 gst_buffer_set_caps (*buf, caps);
999 gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
1001 GstVideoRate *videorate;
1002 GstFlowReturn res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1003 GstClockTime intime, in_ts, in_dur;
1004 GstClockTime avg_period;
1005 gboolean skip = FALSE;
1007 videorate = GST_VIDEO_RATE (trans);
1009 /* make sure the denominators are not 0 */
1010 if (videorate->from_rate_denominator == 0 ||
1011 videorate->to_rate_denominator == 0)
1012 goto not_negotiated;
1014 GST_OBJECT_LOCK (videorate);
1015 avg_period = videorate->average_period_set;
1016 GST_OBJECT_UNLOCK (videorate);
1018 /* MT-safe switching between modes */
1019 if (G_UNLIKELY (avg_period != videorate->average_period)) {
1020 gboolean switch_mode = (avg_period == 0 || videorate->average_period == 0);
1021 videorate->average_period = avg_period;
1022 videorate->last_ts = GST_CLOCK_TIME_NONE;
1026 /* enabling average mode */
1027 videorate->average = 0;
1028 /* make sure no cached buffers from regular mode are left */
1029 gst_video_rate_swap_prev (videorate, NULL, 0);
1031 /* enable regular mode */
1032 videorate->next_ts = GST_CLOCK_TIME_NONE;
1036 /* max averaging mode has a no latency, normal mode does */
1037 gst_element_post_message (GST_ELEMENT (videorate),
1038 gst_message_new_latency (GST_OBJECT (videorate)));
1042 if (videorate->average_period > 0)
1043 return gst_video_rate_trans_ip_max_avg (videorate, buffer);
1045 in_ts = GST_BUFFER_TIMESTAMP (buffer);
1046 in_dur = GST_BUFFER_DURATION (buffer);
1048 if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE)) {
1049 in_ts = videorate->last_ts;
1050 if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE))
1051 goto invalid_buffer;
1054 /* get the time of the next expected buffer timestamp, we use this when the
1055 * next buffer has -1 as a timestamp */
1056 videorate->last_ts = in_ts;
1057 if (in_dur != GST_CLOCK_TIME_NONE)
1058 videorate->last_ts += in_dur;
1060 GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
1061 GST_TIME_ARGS (in_ts));
1063 /* the input time is the time in the segment + all previously accumulated
1065 intime = in_ts + videorate->segment.accum;
1067 /* we need to have two buffers to compare */
1068 if (videorate->prevbuf == NULL) {
1069 gst_video_rate_swap_prev (videorate, buffer, intime);
1071 if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts)) {
1072 /* new buffer, we expect to output a buffer that matches the first
1073 * timestamp in the segment */
1074 if (videorate->skip_to_first || skip) {
1075 videorate->next_ts = intime;
1076 videorate->base_ts = in_ts - videorate->segment.start;
1077 videorate->out_frame_count = 0;
1079 videorate->next_ts =
1080 videorate->segment.start + videorate->segment.accum;
1084 GstClockTime prevtime;
1086 gint64 diff1, diff2;
1088 prevtime = videorate->prev_ts;
1090 GST_LOG_OBJECT (videorate,
1091 "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
1092 " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
1093 GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
1097 /* drop new buffer if it's before previous one */
1098 if (intime < prevtime) {
1099 GST_DEBUG_OBJECT (videorate,
1100 "The new buffer (%" GST_TIME_FORMAT
1101 ") is before the previous buffer (%"
1102 GST_TIME_FORMAT "). Dropping new buffer.",
1103 GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
1105 if (!videorate->silent)
1106 gst_video_rate_notify_drop (videorate);
1110 /* got 2 buffers, see which one is the best */
1113 diff1 = prevtime - videorate->next_ts;
1114 diff2 = intime - videorate->next_ts;
1116 /* take absolute values, beware: abs and ABS don't work for gint64 */
1122 GST_LOG_OBJECT (videorate,
1123 "diff with prev %" GST_TIME_FORMAT " diff with new %"
1124 GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
1125 GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
1126 GST_TIME_ARGS (videorate->next_ts));
1128 /* output first one when its the best */
1129 if (diff1 <= diff2) {
1133 /* on error the _flush function posted a warning already */
1134 if ((r = gst_video_rate_flush_prev (videorate,
1135 count > 1)) != GST_FLOW_OK) {
1141 /* Do not produce any dups. We can exit loop now */
1142 if (videorate->drop_only)
1144 /* continue while the first one was the best, if they were equal avoid
1145 * going into an infinite loop */
1147 while (diff1 < diff2);
1149 /* if we outputed the first buffer more then once, we have dups */
1151 videorate->dup += count - 1;
1152 if (!videorate->silent)
1153 gst_video_rate_notify_duplicate (videorate);
1155 /* if we didn't output the first buffer, we have a drop */
1156 else if (count == 0) {
1159 if (!videorate->silent)
1160 gst_video_rate_notify_drop (videorate);
1162 GST_LOG_OBJECT (videorate,
1163 "new is best, old never used, drop, outgoing ts %"
1164 GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
1166 GST_LOG_OBJECT (videorate,
1167 "END, putting new in old, diff1 %" GST_TIME_FORMAT
1168 ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
1169 ", in %" G_GUINT64_FORMAT ", out %" G_GUINT64_FORMAT ", drop %"
1170 G_GUINT64_FORMAT ", dup %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff1),
1171 GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
1172 videorate->in, videorate->out, videorate->drop, videorate->dup);
1174 /* swap in new one when it's the best */
1175 gst_video_rate_swap_prev (videorate, buffer, intime);
1183 GST_WARNING_OBJECT (videorate, "no framerate negotiated");
1184 res = GST_FLOW_NOT_NEGOTIATED;
1190 GST_WARNING_OBJECT (videorate,
1191 "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
1192 res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1198 gst_video_rate_start (GstBaseTransform * trans)
1200 gst_video_rate_reset (GST_VIDEO_RATE (trans));
1205 gst_video_rate_stop (GstBaseTransform * trans)
1207 gst_video_rate_reset (GST_VIDEO_RATE (trans));
1212 gst_video_rate_set_property (GObject * object,
1213 guint prop_id, const GValue * value, GParamSpec * pspec)
1215 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1217 GST_OBJECT_LOCK (videorate);
1220 videorate->silent = g_value_get_boolean (value);
1223 videorate->new_pref = g_value_get_double (value);
1225 case PROP_SKIP_TO_FIRST:
1226 videorate->skip_to_first = g_value_get_boolean (value);
1228 case PROP_DROP_ONLY:
1229 videorate->drop_only = g_value_get_boolean (value);
1232 case PROP_AVERAGE_PERIOD:
1233 videorate->average_period_set = g_value_get_uint64 (value);
1236 g_atomic_int_set (&videorate->max_rate, g_value_get_int (value));
1239 case PROP_FORCE_FPS:
1240 videorate->force_fps_n = gst_value_get_fraction_numerator (value);
1241 videorate->force_fps_d = gst_value_get_fraction_denominator (value);
1245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1248 GST_OBJECT_UNLOCK (videorate);
1252 GST_OBJECT_UNLOCK (videorate);
1253 gst_base_transform_reconfigure (GST_BASE_TRANSFORM (videorate));
1257 gst_video_rate_get_property (GObject * object,
1258 guint prop_id, GValue * value, GParamSpec * pspec)
1260 GstVideoRate *videorate = GST_VIDEO_RATE (object);
1262 GST_OBJECT_LOCK (videorate);
1265 g_value_set_uint64 (value, videorate->in);
1268 g_value_set_uint64 (value, videorate->out);
1271 g_value_set_uint64 (value, videorate->dup);
1274 g_value_set_uint64 (value, videorate->drop);
1277 g_value_set_boolean (value, videorate->silent);
1280 g_value_set_double (value, videorate->new_pref);
1282 case PROP_SKIP_TO_FIRST:
1283 g_value_set_boolean (value, videorate->skip_to_first);
1285 case PROP_DROP_ONLY:
1286 g_value_set_boolean (value, videorate->drop_only);
1288 case PROP_AVERAGE_PERIOD:
1289 g_value_set_uint64 (value, videorate->average_period_set);
1292 g_value_set_int (value, g_atomic_int_get (&videorate->max_rate));
1294 case PROP_FORCE_FPS:
1295 gst_value_set_fraction (value, videorate->force_fps_n,
1296 videorate->force_fps_d);
1299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1302 GST_OBJECT_UNLOCK (videorate);
1306 plugin_init (GstPlugin * plugin)
1308 GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
1309 "VideoRate stream fixer");
1311 return gst_element_register (plugin, "videorate", GST_RANK_NONE,
1312 GST_TYPE_VIDEO_RATE);
1315 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1318 "Adjusts video frames",
1319 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)