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
22 * @short_description: retimestamps and drops/duplicates video frames to
23 * match the source pad's framerate and create a perfect stream
27 * This element takes an incoming stream of timestamped video frames.
28 * It will produce a perfect stream that matches the source pad's framerate.
30 * The correction is performed by dropping and duplicating frames, no fancy
31 * algorithm is used to interpolate frames (yet).
34 * By default the element will simply negotiate the same framerate on its
35 * source and sink pad.
38 * This operation is useful to link to elements that require a perfect stream.
39 * Typical examples are formats that do not store timestamps for video frames,
40 * but only store a framerate, like Ogg and AVI.
43 * A conversion to a specific framerate can be forced by using filtered caps on
47 * The properties "in", "out", "duplicate" and "drop" can be read to obtain
48 * information about number of input frames, output frames, dropped frames
49 * (i.e. the number of unused input frames) and duplicated frames (i.e. the
50 * number of times an input frame was duplicated, beside being used normally).
52 * An input stream that needs no adjustments will thus never have dropped or
55 * When the "silent" property is set to FALSE, a GObject property notification
56 * will be emitted whenever one of the "duplicate" or "drop" values changes.
57 * This can potentially cause performance degradation.
58 * Note that property notification will happen from the streaming thread, so
59 * applications should be prepared for this.
61 * <title>Example pipelines</title>
64 * gst-launch -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! videorate ! video/x-raw-yuv,framerate=15/1 ! xvimagesink
66 * Decode an Ogg/Theora file and adjust the framerate to 15 fps before playing.
67 * To create the test Ogg/Theora file refer to the documentation of theoraenc.
71 * gst-launch -v v4lsrc ! videorate ! video/x-raw-yuv,framerate=25/2 ! theoraenc ! oggmux ! filesink location=v4l.ogg
73 * Capture video from a V4L device, and adjust the stream to 12.5 fps before
74 * encoding to Ogg/Theora.
78 * Last reviewed on 2006-09-02 (0.10.11)
85 #include "gstvideorate.h"
87 GST_DEBUG_CATEGORY_STATIC (video_rate_debug);
88 #define GST_CAT_DEFAULT video_rate_debug
90 /* elementfactory information */
91 static const GstElementDetails video_rate_details =
92 GST_ELEMENT_DETAILS ("Video rate adjuster",
93 "Filter/Effect/Video",
94 "Drops/duplicates/adjusts timestamps on video frames to make a perfect stream",
95 "Wim Taymans <wim@fluendo.com>");
97 /* GstVideoRate signals and args */
104 #define DEFAULT_SILENT TRUE
105 #define DEFAULT_NEW_PREF 1.0
119 static GstStaticPadTemplate gst_video_rate_src_template =
120 GST_STATIC_PAD_TEMPLATE ("src",
123 GST_STATIC_CAPS ("video/x-raw-yuv; video/x-raw-rgb")
126 static GstStaticPadTemplate gst_video_rate_sink_template =
127 GST_STATIC_PAD_TEMPLATE ("sink",
130 GST_STATIC_CAPS ("video/x-raw-yuv; video/x-raw-rgb")
133 static void gst_video_rate_base_init (gpointer g_class);
134 static void gst_video_rate_class_init (GstVideoRateClass * klass);
135 static void gst_video_rate_init (GstVideoRate * videorate);
137 static void gst_video_rate_swap_prev (GstVideoRate * videorate,
138 GstBuffer * buffer, gint64 time);
139 static gboolean gst_video_rate_event (GstPad * pad, GstEvent * event);
140 static GstFlowReturn gst_video_rate_chain (GstPad * pad, GstBuffer * buffer);
142 static void gst_video_rate_set_property (GObject * object,
143 guint prop_id, const GValue * value, GParamSpec * pspec);
144 static void gst_video_rate_get_property (GObject * object,
145 guint prop_id, GValue * value, GParamSpec * pspec);
147 static GstStateChangeReturn gst_video_rate_change_state (GstElement * element,
148 GstStateChange transition);
150 static GstElementClass *parent_class = NULL;
152 /*static guint gst_video_rate_signals[LAST_SIGNAL] = { 0 }; */
155 gst_video_rate_get_type (void)
157 static GType video_rate_type = 0;
159 if (!video_rate_type) {
160 static const GTypeInfo video_rate_info = {
161 sizeof (GstVideoRateClass),
162 gst_video_rate_base_init,
164 (GClassInitFunc) gst_video_rate_class_init,
167 sizeof (GstVideoRate),
169 (GInstanceInitFunc) gst_video_rate_init,
172 video_rate_type = g_type_register_static (GST_TYPE_ELEMENT,
173 "GstVideoRate", &video_rate_info, 0);
176 return video_rate_type;
180 gst_video_rate_base_init (gpointer g_class)
182 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
184 gst_element_class_set_details (element_class, &video_rate_details);
186 gst_element_class_add_pad_template (element_class,
187 gst_static_pad_template_get (&gst_video_rate_sink_template));
188 gst_element_class_add_pad_template (element_class,
189 gst_static_pad_template_get (&gst_video_rate_src_template));
192 gst_video_rate_class_init (GstVideoRateClass * klass)
194 GObjectClass *object_class = G_OBJECT_CLASS (klass);
195 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
197 parent_class = g_type_class_peek_parent (klass);
199 object_class->set_property = gst_video_rate_set_property;
200 object_class->get_property = gst_video_rate_get_property;
202 g_object_class_install_property (object_class, ARG_IN,
203 g_param_spec_uint64 ("in", "In",
204 "Number of input frames", 0, G_MAXUINT64, 0, G_PARAM_READABLE));
205 g_object_class_install_property (object_class, ARG_OUT,
206 g_param_spec_uint64 ("out", "Out",
207 "Number of output frames", 0, G_MAXUINT64, 0, G_PARAM_READABLE));
208 g_object_class_install_property (object_class, ARG_DUP,
209 g_param_spec_uint64 ("duplicate", "Duplicate",
210 "Number of duplicated frames", 0, G_MAXUINT64, 0, G_PARAM_READABLE));
211 g_object_class_install_property (object_class, ARG_DROP,
212 g_param_spec_uint64 ("drop", "Drop",
213 "Number of dropped frames", 0, G_MAXUINT64, 0, G_PARAM_READABLE));
214 g_object_class_install_property (object_class, ARG_SILENT,
215 g_param_spec_boolean ("silent", "silent",
216 "Don't emit notify for dropped and duplicated frames",
217 DEFAULT_SILENT, G_PARAM_READWRITE));
218 g_object_class_install_property (object_class, ARG_NEW_PREF,
219 g_param_spec_double ("new_pref", "New Pref",
220 "Value indicating how much to prefer new frames (unused)",
221 0.0, 1.0, DEFAULT_NEW_PREF, G_PARAM_READWRITE));
223 element_class->change_state = gst_video_rate_change_state;
226 /* return the caps that can be used on out_pad given in_caps on in_pad */
228 gst_video_rate_transformcaps (GstPad * in_pad, GstCaps * in_caps,
229 GstPad * out_pad, GstCaps ** out_caps)
232 const GstCaps *in_templ;
235 in_templ = gst_pad_get_pad_template_caps (in_pad);
236 intersect = gst_caps_intersect (in_caps, in_templ);
238 /* all possible framerates are allowed */
239 for (i = 0; i < gst_caps_get_size (intersect); i++) {
240 GstStructure *structure;
242 structure = gst_caps_get_structure (intersect, i);
244 gst_structure_set (structure,
245 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
247 *out_caps = intersect;
253 gst_video_rate_getcaps (GstPad * pad)
255 GstVideoRate *videorate;
259 videorate = GST_VIDEO_RATE (GST_PAD_PARENT (pad));
261 otherpad = (pad == videorate->srcpad) ? videorate->sinkpad :
264 /* we can do what the peer can */
265 caps = gst_pad_peer_get_caps (otherpad);
269 gst_video_rate_transformcaps (otherpad, caps, pad, &transform);
270 gst_caps_unref (caps);
273 /* no peer, our padtemplate is enough then */
274 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
281 gst_video_rate_setcaps (GstPad * pad, GstCaps * caps)
283 GstVideoRate *videorate;
284 GstStructure *structure;
286 GstPad *otherpad, *opeer;
287 gint rate_numerator, rate_denominator;
289 videorate = GST_VIDEO_RATE (gst_pad_get_parent (pad));
291 structure = gst_caps_get_structure (caps, 0);
292 if (!gst_structure_get_fraction (structure, "framerate",
293 &rate_numerator, &rate_denominator))
296 if (pad == videorate->srcpad) {
297 videorate->to_rate_numerator = rate_numerator;
298 videorate->to_rate_denominator = rate_denominator;
299 otherpad = videorate->sinkpad;
301 videorate->from_rate_numerator = rate_numerator;
302 videorate->from_rate_denominator = rate_denominator;
303 otherpad = videorate->srcpad;
305 /* now try to find something for the peer */
306 opeer = gst_pad_get_peer (otherpad);
308 if (gst_pad_accept_caps (opeer, caps)) {
309 /* the peer accepts the caps as they are */
310 gst_pad_set_caps (otherpad, caps);
316 GstCaps *transform = NULL;
320 /* see how we can transform the input caps */
321 if (!gst_video_rate_transformcaps (pad, caps, otherpad, &transform))
324 /* see what the peer can do */
325 peercaps = gst_pad_get_caps (opeer);
327 GST_DEBUG ("icaps %" GST_PTR_FORMAT, peercaps);
328 GST_DEBUG ("transform %" GST_PTR_FORMAT, transform);
330 /* filter against our possibilities */
331 intersect = gst_caps_intersect (peercaps, transform);
332 gst_caps_unref (peercaps);
333 gst_caps_unref (transform);
335 GST_DEBUG ("intersect %" GST_PTR_FORMAT, intersect);
337 /* take first possibility */
338 caps = gst_caps_copy_nth (intersect, 0);
339 gst_caps_unref (intersect);
340 structure = gst_caps_get_structure (caps, 0);
343 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
344 rate_numerator, rate_denominator);
346 gst_structure_get_fraction (structure, "framerate",
347 &rate_numerator, &rate_denominator);
349 if (otherpad == videorate->srcpad) {
350 videorate->to_rate_numerator = rate_numerator;
351 videorate->to_rate_denominator = rate_denominator;
353 videorate->from_rate_numerator = rate_numerator;
354 videorate->from_rate_denominator = rate_denominator;
356 gst_pad_set_caps (otherpad, caps);
359 gst_object_unref (opeer);
362 gst_object_unref (videorate);
367 GST_DEBUG_OBJECT (videorate, "no framerate specified");
372 GST_DEBUG_OBJECT (videorate, "no framerate transform possible");
379 gst_video_rate_reset (GstVideoRate * videorate)
381 GST_DEBUG ("resetting internal variables");
387 videorate->next_ts = G_GINT64_CONSTANT (0);
388 gst_video_rate_swap_prev (videorate, NULL, 0);
390 gst_segment_init (&videorate->segment, GST_FORMAT_TIME);
394 gst_video_rate_init (GstVideoRate * videorate)
396 GST_DEBUG ("gst_video_rate_init");
398 gst_pad_new_from_static_template (&gst_video_rate_sink_template, "sink");
399 gst_element_add_pad (GST_ELEMENT (videorate), videorate->sinkpad);
400 gst_pad_set_event_function (videorate->sinkpad, gst_video_rate_event);
401 gst_pad_set_chain_function (videorate->sinkpad, gst_video_rate_chain);
402 gst_pad_set_getcaps_function (videorate->sinkpad, gst_video_rate_getcaps);
403 gst_pad_set_setcaps_function (videorate->sinkpad, gst_video_rate_setcaps);
406 gst_pad_new_from_static_template (&gst_video_rate_src_template, "src");
407 gst_element_add_pad (GST_ELEMENT (videorate), videorate->srcpad);
408 gst_pad_set_getcaps_function (videorate->srcpad, gst_video_rate_getcaps);
409 gst_pad_set_setcaps_function (videorate->srcpad, gst_video_rate_setcaps);
411 gst_video_rate_reset (videorate);
412 videorate->silent = DEFAULT_SILENT;
413 videorate->new_pref = DEFAULT_NEW_PREF;
415 videorate->from_rate_numerator = 0;
416 videorate->from_rate_denominator = 0;
417 videorate->to_rate_numerator = 0;
418 videorate->to_rate_denominator = 0;
421 /* flush the oldest buffer */
423 gst_video_rate_flush_prev (GstVideoRate * videorate)
427 GstClockTime push_ts;
429 if (!videorate->prevbuf)
430 goto eos_before_buffers;
432 /* make sure we can write to the metadata */
433 outbuf = gst_buffer_make_metadata_writable
434 (gst_buffer_ref (videorate->prevbuf));
436 /* this is the timestamp we put on the buffer */
437 push_ts = videorate->next_ts;
440 if (videorate->to_rate_numerator) {
441 /* interpolate next expected timestamp in the segment */
442 videorate->next_ts = videorate->segment.accum + videorate->segment.start +
443 gst_util_uint64_scale (videorate->out,
444 videorate->to_rate_denominator * GST_SECOND,
445 videorate->to_rate_numerator);
446 GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
449 /* adapt for looping, bring back to time in current segment. */
450 GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.accum;
452 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (videorate->srcpad));
454 GST_LOG_OBJECT (videorate,
455 "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
456 GST_TIME_ARGS (push_ts));
458 res = gst_pad_push (videorate->srcpad, outbuf);
465 GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
471 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
474 GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
475 if (videorate->prevbuf)
476 gst_buffer_unref (videorate->prevbuf);
477 videorate->prevbuf = buffer;
478 videorate->prev_ts = time;
482 gst_video_rate_event (GstPad * pad, GstEvent * event)
484 GstVideoRate *videorate;
487 videorate = GST_VIDEO_RATE (gst_pad_get_parent (pad));
489 switch (GST_EVENT_TYPE (event)) {
490 case GST_EVENT_NEWSEGMENT:
492 gint64 start, stop, time;
497 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
498 &start, &stop, &time);
500 if (format != GST_FORMAT_TIME)
503 GST_DEBUG_OBJECT (videorate, "handle NEWSEGMENT");
505 /* We just want to update the accumulated stream_time */
506 gst_segment_set_newsegment_full (&videorate->segment, update, rate, arate,
507 format, start, stop, time);
509 GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
510 &videorate->segment);
514 /* flush last queued frame */
515 GST_DEBUG_OBJECT (videorate, "Got EOS");
516 gst_video_rate_flush_prev (videorate);
518 case GST_EVENT_FLUSH_STOP:
519 /* also resets the segment */
520 GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
521 gst_video_rate_reset (videorate);
527 ret = gst_pad_push_event (videorate->srcpad, event);
530 gst_object_unref (videorate);
537 GST_WARNING_OBJECT (videorate,
538 "Got segment but doesn't have GST_FORMAT_TIME value");
539 gst_event_unref (event);
546 gst_video_rate_chain (GstPad * pad, GstBuffer * buffer)
548 GstVideoRate *videorate;
549 GstFlowReturn res = GST_FLOW_OK;
550 GstClockTime intime, in_ts;
552 videorate = GST_VIDEO_RATE (gst_pad_get_parent (pad));
554 /* make sure the denominators are not 0 */
555 if (videorate->from_rate_denominator == 0 ||
556 videorate->to_rate_denominator == 0)
559 in_ts = GST_BUFFER_TIMESTAMP (buffer);
561 GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
562 GST_TIME_ARGS (in_ts));
564 /* the input time is the time in the segment + all previously accumulated
566 intime = in_ts + videorate->segment.accum;
568 /* we need to have two buffers to compare */
569 if (videorate->prevbuf == NULL) {
570 gst_video_rate_swap_prev (videorate, buffer, intime);
572 /* new buffer, we expect to output a buffer that matches the first
573 * timestamp in the segment */
574 videorate->next_ts = videorate->segment.start;
576 GstClockTime prevtime;
580 prevtime = videorate->prev_ts;
582 GST_LOG_OBJECT (videorate,
583 "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
584 " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
585 GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
589 /* drop new buffer if it's before previous one */
590 if (intime < prevtime) {
591 GST_DEBUG_OBJECT (videorate,
592 "The new buffer (%" GST_TIME_FORMAT
593 ") is before the previous buffer (%"
594 GST_TIME_FORMAT "). Dropping new buffer.",
595 GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
597 if (!videorate->silent)
598 g_object_notify (G_OBJECT (videorate), "drop");
599 gst_buffer_unref (buffer);
603 /* got 2 buffers, see which one is the best */
605 diff1 = prevtime - videorate->next_ts;
606 diff2 = intime - videorate->next_ts;
608 /* take absolute values, beware: abs and ABS don't work for gint64 */
614 GST_LOG_OBJECT (videorate,
615 "diff with prev %" GST_TIME_FORMAT " diff with new %"
616 GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
617 GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
618 GST_TIME_ARGS (videorate->next_ts));
620 /* output first one when its the best */
624 /* on error the _flush function posted a warning already */
625 if ((res = gst_video_rate_flush_prev (videorate)) != GST_FLOW_OK)
628 /* continue while the first one was the best */
630 while (diff1 < diff2);
632 /* if we outputed the first buffer more then once, we have dups */
634 videorate->dup += count - 1;
635 if (!videorate->silent)
636 g_object_notify (G_OBJECT (videorate), "duplicate");
638 /* if we didn't output the first buffer, we have a drop */
639 else if (count == 0) {
642 if (!videorate->silent)
643 g_object_notify (G_OBJECT (videorate), "drop");
645 GST_LOG_OBJECT (videorate,
646 "new is best, old never used, drop, outgoing ts %"
647 GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
649 GST_LOG_OBJECT (videorate,
650 "END, putting new in old, diff1 %" GST_TIME_FORMAT
651 ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
652 ", in %lld, out %lld, drop %lld, dup %lld", GST_TIME_ARGS (diff1),
653 GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
654 videorate->in, videorate->out, videorate->drop, videorate->dup);
656 /* swap in new one when it's the best */
657 gst_video_rate_swap_prev (videorate, buffer, intime);
660 gst_object_unref (videorate);
666 GST_WARNING_OBJECT (videorate, "no framerate negotiated");
667 gst_buffer_unref (buffer);
668 res = GST_FLOW_NOT_NEGOTIATED;
674 gst_video_rate_set_property (GObject * object,
675 guint prop_id, const GValue * value, GParamSpec * pspec)
677 GstVideoRate *videorate = GST_VIDEO_RATE (object);
681 videorate->silent = g_value_get_boolean (value);
684 videorate->new_pref = g_value_get_double (value);
687 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
693 gst_video_rate_get_property (GObject * object,
694 guint prop_id, GValue * value, GParamSpec * pspec)
696 GstVideoRate *videorate = GST_VIDEO_RATE (object);
700 g_value_set_uint64 (value, videorate->in);
703 g_value_set_uint64 (value, videorate->out);
706 g_value_set_uint64 (value, videorate->dup);
709 g_value_set_uint64 (value, videorate->drop);
712 g_value_set_boolean (value, videorate->silent);
715 g_value_set_double (value, videorate->new_pref);
718 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
723 static GstStateChangeReturn
724 gst_video_rate_change_state (GstElement * element, GstStateChange transition)
726 GstStateChangeReturn ret;
727 GstVideoRate *videorate;
729 videorate = GST_VIDEO_RATE (element);
731 switch (transition) {
736 ret = parent_class->change_state (element, transition);
738 switch (transition) {
739 case GST_STATE_CHANGE_PAUSED_TO_READY:
740 gst_video_rate_reset (videorate);
750 plugin_init (GstPlugin * plugin)
752 GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
753 "VideoRate stream fixer");
755 return gst_element_register (plugin, "videorate", GST_RANK_NONE,
756 GST_TYPE_VIDEO_RATE);
759 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
762 "Adjusts video frames",
763 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)