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
103 static GstStaticPadTemplate gst_video_rate_src_template =
104 GST_STATIC_PAD_TEMPLATE ("src",
107 GST_STATIC_CAPS ("video/x-raw-yuv;"
108 "video/x-raw-rgb;" "video/x-raw-gray;" "image/jpeg;" "image/png")
111 static GstStaticPadTemplate gst_video_rate_sink_template =
112 GST_STATIC_PAD_TEMPLATE ("sink",
115 GST_STATIC_CAPS ("video/x-raw-yuv;"
116 "video/x-raw-rgb;" "video/x-raw-gray;" "image/jpeg;" "image/png")
119 static void gst_video_rate_swap_prev (GstVideoRate * videorate,
120 GstBuffer * buffer, gint64 time);
121 static gboolean gst_video_rate_event (GstPad * pad, GstEvent * event);
122 static gboolean gst_video_rate_query (GstPad * pad, GstQuery * query);
123 static GstFlowReturn gst_video_rate_chain (GstPad * pad, GstBuffer * buffer);
125 static void gst_video_rate_set_property (GObject * object,
126 guint prop_id, const GValue * value, GParamSpec * pspec);
127 static void gst_video_rate_get_property (GObject * object,
128 guint prop_id, GValue * value, GParamSpec * pspec);
130 static GstStateChangeReturn gst_video_rate_change_state (GstElement * element,
131 GstStateChange transition);
133 /*static guint gst_video_rate_signals[LAST_SIGNAL] = { 0 }; */
135 static GParamSpec *pspec_drop = NULL;
136 static GParamSpec *pspec_duplicate = NULL;
138 GST_BOILERPLATE (GstVideoRate, gst_video_rate, GstElement, GST_TYPE_ELEMENT);
141 gst_video_rate_base_init (gpointer g_class)
143 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
145 gst_element_class_set_details_simple (element_class,
146 "Video rate adjuster", "Filter/Effect/Video",
147 "Drops/duplicates/adjusts timestamps on video frames to make a perfect stream",
148 "Wim Taymans <wim@fluendo.com>");
150 gst_element_class_add_pad_template (element_class,
151 gst_static_pad_template_get (&gst_video_rate_sink_template));
152 gst_element_class_add_pad_template (element_class,
153 gst_static_pad_template_get (&gst_video_rate_src_template));
157 gst_video_rate_class_init (GstVideoRateClass * klass)
159 GObjectClass *object_class = G_OBJECT_CLASS (klass);
160 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
162 parent_class = g_type_class_peek_parent (klass);
164 object_class->set_property = gst_video_rate_set_property;
165 object_class->get_property = gst_video_rate_get_property;
167 g_object_class_install_property (object_class, ARG_IN,
168 g_param_spec_uint64 ("in", "In",
169 "Number of input frames", 0, G_MAXUINT64, 0,
170 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
171 g_object_class_install_property (object_class, ARG_OUT,
172 g_param_spec_uint64 ("out", "Out", "Number of output frames", 0,
173 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
174 pspec_duplicate = g_param_spec_uint64 ("duplicate", "Duplicate",
175 "Number of duplicated frames", 0, G_MAXUINT64, 0,
176 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
177 g_object_class_install_property (object_class, ARG_DUP, pspec_duplicate);
178 pspec_drop = g_param_spec_uint64 ("drop", "Drop", "Number of dropped frames",
179 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
180 g_object_class_install_property (object_class, ARG_DROP, pspec_drop);
181 g_object_class_install_property (object_class, ARG_SILENT,
182 g_param_spec_boolean ("silent", "silent",
183 "Don't emit notify for dropped and duplicated frames", DEFAULT_SILENT,
184 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185 g_object_class_install_property (object_class, ARG_NEW_PREF,
186 g_param_spec_double ("new-pref", "New Pref",
187 "Value indicating how much to prefer new frames (unused)", 0.0, 1.0,
188 DEFAULT_NEW_PREF, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 * GstVideoRate:skip-to-first:
193 * Don't produce buffers before the first one we receive.
197 g_object_class_install_property (object_class, ARG_SKIP_TO_FIRST,
198 g_param_spec_boolean ("skip-to-first", "Skip to first buffer",
199 "Don't produce buffers before the first one we receive",
200 DEFAULT_SKIP_TO_FIRST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202 element_class->change_state = GST_DEBUG_FUNCPTR (gst_video_rate_change_state);
205 /* return the caps that can be used on out_pad given in_caps on in_pad */
207 gst_video_rate_transformcaps (GstPad * in_pad, GstCaps * in_caps,
208 GstPad * out_pad, GstCaps ** out_caps)
211 const GstCaps *in_templ;
213 GSList *extra_structures = NULL;
216 in_templ = gst_pad_get_pad_template_caps (in_pad);
217 intersect = gst_caps_intersect (in_caps, in_templ);
219 /* all possible framerates are allowed */
220 for (i = 0; i < gst_caps_get_size (intersect); i++) {
221 GstStructure *structure;
223 structure = gst_caps_get_structure (intersect, i);
225 if (gst_structure_has_field (structure, "framerate")) {
226 GstStructure *copy_structure;
228 copy_structure = gst_structure_copy (structure);
229 gst_structure_set (copy_structure,
230 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
231 extra_structures = g_slist_append (extra_structures, copy_structure);
235 /* append the extra structures */
236 for (iter = extra_structures; iter != NULL; iter = g_slist_next (iter)) {
237 gst_caps_append_structure (intersect, (GstStructure *) iter->data);
239 g_slist_free (extra_structures);
241 *out_caps = intersect;
247 gst_video_rate_getcaps (GstPad * pad)
249 GstVideoRate *videorate;
253 videorate = GST_VIDEO_RATE (GST_PAD_PARENT (pad));
255 otherpad = (pad == videorate->srcpad) ? videorate->sinkpad :
258 /* we can do what the peer can */
259 caps = gst_pad_peer_get_caps (otherpad);
263 gst_video_rate_transformcaps (otherpad, caps, pad, &transform);
264 gst_caps_unref (caps);
267 /* no peer, our padtemplate is enough then */
268 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
275 gst_video_rate_setcaps (GstPad * pad, GstCaps * caps)
277 GstVideoRate *videorate;
278 GstStructure *structure;
280 GstPad *otherpad, *opeer;
281 gint rate_numerator, rate_denominator;
283 videorate = GST_VIDEO_RATE (gst_pad_get_parent (pad));
285 GST_DEBUG_OBJECT (pad, "setcaps called %" GST_PTR_FORMAT, caps);
287 structure = gst_caps_get_structure (caps, 0);
288 if (!gst_structure_get_fraction (structure, "framerate",
289 &rate_numerator, &rate_denominator))
292 if (pad == videorate->srcpad) {
293 /* out_frame_count is scaled by the frame rate caps when calculating next_ts.
294 * when the frame rate caps change, we must update base_ts and reset
296 if (videorate->to_rate_numerator) {
297 videorate->base_ts +=
298 gst_util_uint64_scale (videorate->out_frame_count,
299 videorate->to_rate_denominator * GST_SECOND,
300 videorate->to_rate_numerator);
302 videorate->out_frame_count = 0;
303 videorate->to_rate_numerator = rate_numerator;
304 videorate->to_rate_denominator = rate_denominator;
305 otherpad = videorate->sinkpad;
307 videorate->from_rate_numerator = rate_numerator;
308 videorate->from_rate_denominator = rate_denominator;
309 otherpad = videorate->srcpad;
312 /* now try to find something for the peer */
313 opeer = gst_pad_get_peer (otherpad);
315 if (gst_pad_accept_caps (opeer, caps)) {
316 /* the peer accepts the caps as they are */
317 gst_pad_set_caps (otherpad, caps);
322 GstCaps *transform = NULL;
326 /* see how we can transform the input caps */
327 if (!gst_video_rate_transformcaps (pad, caps, otherpad, &transform))
330 /* see what the peer can do */
331 peercaps = gst_pad_get_caps (opeer);
333 GST_DEBUG_OBJECT (opeer, "icaps %" GST_PTR_FORMAT, peercaps);
334 GST_DEBUG_OBJECT (videorate, "transform %" GST_PTR_FORMAT, transform);
336 /* filter against our possibilities */
337 caps = gst_caps_intersect (peercaps, transform);
338 gst_caps_unref (peercaps);
339 gst_caps_unref (transform);
341 GST_DEBUG_OBJECT (videorate, "intersect %" GST_PTR_FORMAT, caps);
343 /* take first possibility */
344 gst_caps_truncate (caps);
345 structure = gst_caps_get_structure (caps, 0);
348 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
349 rate_numerator, rate_denominator);
351 gst_structure_get_fraction (structure, "framerate",
352 &rate_numerator, &rate_denominator);
354 if (otherpad == videorate->srcpad) {
355 videorate->to_rate_numerator = rate_numerator;
356 videorate->to_rate_denominator = rate_denominator;
358 videorate->from_rate_numerator = rate_numerator;
359 videorate->from_rate_denominator = rate_denominator;
362 if (gst_structure_has_field (structure, "interlaced"))
363 gst_structure_fixate_field_boolean (structure, "interlaced", FALSE);
364 if (gst_structure_has_field (structure, "color-matrix"))
365 gst_structure_fixate_field_string (structure, "color-matrix", "sdtv");
366 if (gst_structure_has_field (structure, "chroma-site"))
367 gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
368 if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
369 gst_structure_fixate_field_nearest_fraction (structure,
370 "pixel-aspect-ratio", 1, 1);
372 gst_pad_set_caps (otherpad, caps);
373 gst_caps_unref (caps);
376 gst_object_unref (opeer);
379 /* After a setcaps, our caps may have changed. In that case, we can't use
380 * the old buffer, if there was one (it might have different dimensions) */
381 GST_DEBUG_OBJECT (videorate, "swapping old buffers");
382 gst_video_rate_swap_prev (videorate, NULL, GST_CLOCK_TIME_NONE);
384 gst_object_unref (videorate);
389 GST_DEBUG_OBJECT (videorate, "no framerate specified");
394 GST_DEBUG_OBJECT (videorate, "no framerate transform possible");
401 gst_video_rate_reset (GstVideoRate * videorate)
403 GST_DEBUG_OBJECT (videorate, "resetting internal variables");
407 videorate->base_ts = 0;
408 videorate->out_frame_count = 0;
411 videorate->next_ts = GST_CLOCK_TIME_NONE;
412 videorate->last_ts = GST_CLOCK_TIME_NONE;
413 videorate->discont = TRUE;
414 gst_video_rate_swap_prev (videorate, NULL, 0);
416 gst_segment_init (&videorate->segment, GST_FORMAT_TIME);
420 gst_video_rate_init (GstVideoRate * videorate, GstVideoRateClass * klass)
423 gst_pad_new_from_static_template (&gst_video_rate_sink_template, "sink");
424 gst_pad_set_event_function (videorate->sinkpad,
425 GST_DEBUG_FUNCPTR (gst_video_rate_event));
426 gst_pad_set_chain_function (videorate->sinkpad,
427 GST_DEBUG_FUNCPTR (gst_video_rate_chain));
428 gst_pad_set_getcaps_function (videorate->sinkpad,
429 GST_DEBUG_FUNCPTR (gst_video_rate_getcaps));
430 gst_pad_set_setcaps_function (videorate->sinkpad,
431 GST_DEBUG_FUNCPTR (gst_video_rate_setcaps));
432 gst_element_add_pad (GST_ELEMENT (videorate), videorate->sinkpad);
435 gst_pad_new_from_static_template (&gst_video_rate_src_template, "src");
436 gst_pad_set_query_function (videorate->srcpad,
437 GST_DEBUG_FUNCPTR (gst_video_rate_query));
438 gst_pad_set_getcaps_function (videorate->srcpad,
439 GST_DEBUG_FUNCPTR (gst_video_rate_getcaps));
440 gst_pad_set_setcaps_function (videorate->srcpad,
441 GST_DEBUG_FUNCPTR (gst_video_rate_setcaps));
442 gst_element_add_pad (GST_ELEMENT (videorate), videorate->srcpad);
444 gst_video_rate_reset (videorate);
445 videorate->silent = DEFAULT_SILENT;
446 videorate->new_pref = DEFAULT_NEW_PREF;
448 videorate->from_rate_numerator = 0;
449 videorate->from_rate_denominator = 0;
450 videorate->to_rate_numerator = 0;
451 videorate->to_rate_denominator = 0;
454 /* flush the oldest buffer */
456 gst_video_rate_flush_prev (GstVideoRate * videorate, gboolean duplicate)
460 GstClockTime push_ts;
462 if (!videorate->prevbuf)
463 goto eos_before_buffers;
465 /* make sure we can write to the metadata */
466 outbuf = gst_buffer_make_metadata_writable
467 (gst_buffer_ref (videorate->prevbuf));
469 GST_BUFFER_OFFSET (outbuf) = videorate->out;
470 GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
472 if (videorate->discont) {
473 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
474 videorate->discont = FALSE;
476 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
479 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
481 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
483 /* this is the timestamp we put on the buffer */
484 push_ts = videorate->next_ts;
487 videorate->out_frame_count++;
488 if (videorate->to_rate_numerator) {
489 /* interpolate next expected timestamp in the segment */
491 videorate->segment.accum + videorate->segment.start +
492 videorate->base_ts + gst_util_uint64_scale (videorate->out_frame_count,
493 videorate->to_rate_denominator * GST_SECOND,
494 videorate->to_rate_numerator);
495 GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
498 /* adapt for looping, bring back to time in current segment. */
499 GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.accum;
501 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (videorate->srcpad));
503 GST_LOG_OBJECT (videorate,
504 "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
505 GST_TIME_ARGS (push_ts));
507 res = gst_pad_push (videorate->srcpad, outbuf);
514 GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
520 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
523 GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
524 if (videorate->prevbuf)
525 gst_buffer_unref (videorate->prevbuf);
526 videorate->prevbuf = buffer;
527 videorate->prev_ts = time;
531 gst_video_rate_notify_drop (GstVideoRate * videorate)
533 #if !GLIB_CHECK_VERSION(2,26,0)
534 g_object_notify ((GObject *) videorate, "drop");
536 g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
541 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
543 #if !GLIB_CHECK_VERSION(2,26,0)
544 g_object_notify ((GObject *) videorate, "duplicate");
546 g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
550 #define MAGIC_LIMIT 25
552 gst_video_rate_event (GstPad * pad, GstEvent * event)
554 GstVideoRate *videorate;
557 videorate = GST_VIDEO_RATE (gst_pad_get_parent (pad));
559 switch (GST_EVENT_TYPE (event)) {
560 case GST_EVENT_NEWSEGMENT:
562 gint64 start, stop, time;
567 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
568 &start, &stop, &time);
570 if (format != GST_FORMAT_TIME)
573 GST_DEBUG_OBJECT (videorate, "handle NEWSEGMENT");
575 /* close up the previous segment, if appropriate */
576 if (!update && videorate->prevbuf) {
581 /* fill up to the end of current segment,
582 * or only send out the stored buffer if there is no specific stop.
583 * regardless, prevent going loopy in strange cases */
584 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
585 ((GST_CLOCK_TIME_IS_VALID (videorate->segment.stop) &&
586 videorate->next_ts - videorate->segment.accum
587 < videorate->segment.stop)
589 res = gst_video_rate_flush_prev (videorate, count > 0);
593 videorate->dup += count - 1;
594 if (!videorate->silent)
595 gst_video_rate_notify_duplicate (videorate);
596 } else if (count == 0) {
598 if (!videorate->silent)
599 gst_video_rate_notify_drop (videorate);
601 /* clean up for the new one; _chain will resume from the new start */
602 videorate->base_ts = 0;
603 videorate->out_frame_count = 0;
604 gst_video_rate_swap_prev (videorate, NULL, 0);
605 videorate->next_ts = GST_CLOCK_TIME_NONE;
608 /* We just want to update the accumulated stream_time */
609 gst_segment_set_newsegment_full (&videorate->segment, update, rate, arate,
610 format, start, stop, time);
612 GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
613 &videorate->segment);
618 GstFlowReturn res = GST_FLOW_OK;
620 GST_DEBUG_OBJECT (videorate, "Got EOS");
622 /* If the segment has a stop position, fill the segment */
623 if (GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
624 /* fill up to the end of current segment,
625 * or only send out the stored buffer if there is no specific stop.
626 * regardless, prevent going loopy in strange cases */
627 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
628 ((videorate->next_ts - videorate->segment.accum <
629 videorate->segment.stop)
631 res = gst_video_rate_flush_prev (videorate, count > 0);
634 } else if (videorate->prevbuf) {
635 /* Output at least one frame but if the buffer duration is valid, output
636 * enough frames to use the complete buffer duration */
637 if (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf)) {
638 GstClockTime end_ts =
639 videorate->next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
641 while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
642 ((videorate->next_ts - videorate->segment.accum < end_ts)
644 res = gst_video_rate_flush_prev (videorate, count > 0);
648 res = gst_video_rate_flush_prev (videorate, FALSE);
654 videorate->dup += count - 1;
655 if (!videorate->silent)
656 gst_video_rate_notify_duplicate (videorate);
657 } else if (count == 0) {
659 if (!videorate->silent)
660 gst_video_rate_notify_drop (videorate);
665 case GST_EVENT_FLUSH_STOP:
666 /* also resets the segment */
667 GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
668 gst_video_rate_reset (videorate);
674 ret = gst_pad_push_event (videorate->srcpad, event);
677 gst_object_unref (videorate);
684 GST_WARNING_OBJECT (videorate,
685 "Got segment but doesn't have GST_FORMAT_TIME value");
686 gst_event_unref (event);
693 gst_video_rate_query (GstPad * pad, GstQuery * query)
695 GstVideoRate *videorate;
696 gboolean res = FALSE;
698 videorate = GST_VIDEO_RATE (gst_pad_get_parent (pad));
700 switch (GST_QUERY_TYPE (query)) {
701 case GST_QUERY_LATENCY:
703 GstClockTime min, max;
708 if ((peer = gst_pad_get_peer (videorate->sinkpad))) {
709 if ((res = gst_pad_query (peer, query))) {
710 gst_query_parse_latency (query, &live, &min, &max);
712 GST_DEBUG_OBJECT (videorate, "Peer latency: min %"
713 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
714 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
716 if (videorate->from_rate_numerator != 0) {
717 /* add latency. We don't really know since we hold on to the frames
718 * until we get a next frame, which can be anything. We assume
719 * however that this will take from_rate time. */
720 latency = gst_util_uint64_scale (GST_SECOND,
721 videorate->from_rate_denominator,
722 videorate->from_rate_numerator);
724 /* no input framerate, we don't know */
728 GST_DEBUG_OBJECT (videorate, "Our latency: %"
729 GST_TIME_FORMAT, GST_TIME_ARGS (latency));
735 GST_DEBUG_OBJECT (videorate, "Calculated total latency : min %"
736 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
737 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
739 gst_query_set_latency (query, live, min, max);
741 gst_object_unref (peer);
746 res = gst_pad_query_default (pad, query);
749 gst_object_unref (videorate);
755 gst_video_rate_chain (GstPad * pad, GstBuffer * buffer)
757 GstVideoRate *videorate;
758 GstFlowReturn res = GST_FLOW_OK;
759 GstClockTime intime, in_ts, in_dur;
761 videorate = GST_VIDEO_RATE (GST_PAD_PARENT (pad));
763 /* make sure the denominators are not 0 */
764 if (videorate->from_rate_denominator == 0 ||
765 videorate->to_rate_denominator == 0)
768 in_ts = GST_BUFFER_TIMESTAMP (buffer);
769 in_dur = GST_BUFFER_DURATION (buffer);
771 if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE)) {
772 in_ts = videorate->last_ts;
773 if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE))
777 /* get the time of the next expected buffer timestamp, we use this when the
778 * next buffer has -1 as a timestamp */
779 videorate->last_ts = in_ts;
780 if (in_dur != GST_CLOCK_TIME_NONE)
781 videorate->last_ts += in_dur;
783 GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
784 GST_TIME_ARGS (in_ts));
786 /* the input time is the time in the segment + all previously accumulated
788 intime = in_ts + videorate->segment.accum;
790 /* we need to have two buffers to compare */
791 if (videorate->prevbuf == NULL) {
792 gst_video_rate_swap_prev (videorate, buffer, intime);
794 if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts)) {
795 /* new buffer, we expect to output a buffer that matches the first
796 * timestamp in the segment */
797 if (videorate->skip_to_first) {
798 videorate->next_ts = in_ts;
799 videorate->out_frame_count = gst_util_uint64_scale (in_ts,
800 videorate->to_rate_numerator,
801 videorate->to_rate_denominator * GST_SECOND) -
802 (videorate->segment.accum + videorate->segment.start);
805 videorate->segment.start + videorate->segment.accum;
809 GstClockTime prevtime;
813 prevtime = videorate->prev_ts;
815 GST_LOG_OBJECT (videorate,
816 "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
817 " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
818 GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
822 /* drop new buffer if it's before previous one */
823 if (intime < prevtime) {
824 GST_DEBUG_OBJECT (videorate,
825 "The new buffer (%" GST_TIME_FORMAT
826 ") is before the previous buffer (%"
827 GST_TIME_FORMAT "). Dropping new buffer.",
828 GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
830 if (!videorate->silent)
831 gst_video_rate_notify_drop (videorate);
832 gst_buffer_unref (buffer);
836 /* got 2 buffers, see which one is the best */
839 diff1 = prevtime - videorate->next_ts;
840 diff2 = intime - videorate->next_ts;
842 /* take absolute values, beware: abs and ABS don't work for gint64 */
848 GST_LOG_OBJECT (videorate,
849 "diff with prev %" GST_TIME_FORMAT " diff with new %"
850 GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
851 GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
852 GST_TIME_ARGS (videorate->next_ts));
854 /* output first one when its the best */
855 if (diff1 <= diff2) {
858 /* on error the _flush function posted a warning already */
860 gst_video_rate_flush_prev (videorate,
861 count > 1)) != GST_FLOW_OK) {
862 gst_buffer_unref (buffer);
866 /* continue while the first one was the best, if they were equal avoid
867 * going into an infinite loop */
869 while (diff1 < diff2);
871 /* if we outputed the first buffer more then once, we have dups */
873 videorate->dup += count - 1;
874 if (!videorate->silent)
875 gst_video_rate_notify_duplicate (videorate);
877 /* if we didn't output the first buffer, we have a drop */
878 else if (count == 0) {
881 if (!videorate->silent)
882 gst_video_rate_notify_drop (videorate);
884 GST_LOG_OBJECT (videorate,
885 "new is best, old never used, drop, outgoing ts %"
886 GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
888 GST_LOG_OBJECT (videorate,
889 "END, putting new in old, diff1 %" GST_TIME_FORMAT
890 ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
891 ", in %" G_GUINT64_FORMAT ", out %" G_GUINT64_FORMAT ", drop %"
892 G_GUINT64_FORMAT ", dup %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff1),
893 GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
894 videorate->in, videorate->out, videorate->drop, videorate->dup);
896 /* swap in new one when it's the best */
897 gst_video_rate_swap_prev (videorate, buffer, intime);
905 GST_WARNING_OBJECT (videorate, "no framerate negotiated");
906 gst_buffer_unref (buffer);
907 res = GST_FLOW_NOT_NEGOTIATED;
913 GST_WARNING_OBJECT (videorate,
914 "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
915 gst_buffer_unref (buffer);
921 gst_video_rate_set_property (GObject * object,
922 guint prop_id, const GValue * value, GParamSpec * pspec)
924 GstVideoRate *videorate = GST_VIDEO_RATE (object);
928 videorate->silent = g_value_get_boolean (value);
931 videorate->new_pref = g_value_get_double (value);
933 case ARG_SKIP_TO_FIRST:
934 videorate->skip_to_first = g_value_get_boolean (value);
937 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
943 gst_video_rate_get_property (GObject * object,
944 guint prop_id, GValue * value, GParamSpec * pspec)
946 GstVideoRate *videorate = GST_VIDEO_RATE (object);
950 g_value_set_uint64 (value, videorate->in);
953 g_value_set_uint64 (value, videorate->out);
956 g_value_set_uint64 (value, videorate->dup);
959 g_value_set_uint64 (value, videorate->drop);
962 g_value_set_boolean (value, videorate->silent);
965 g_value_set_double (value, videorate->new_pref);
967 case ARG_SKIP_TO_FIRST:
968 g_value_set_boolean (value, videorate->skip_to_first);
971 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
976 static GstStateChangeReturn
977 gst_video_rate_change_state (GstElement * element, GstStateChange transition)
979 GstStateChangeReturn ret;
980 GstVideoRate *videorate;
982 videorate = GST_VIDEO_RATE (element);
984 switch (transition) {
985 case GST_STATE_CHANGE_READY_TO_PAUSED:
986 videorate->discont = TRUE;
987 videorate->last_ts = -1;
993 ret = parent_class->change_state (element, transition);
995 switch (transition) {
996 case GST_STATE_CHANGE_PAUSED_TO_READY:
997 gst_video_rate_reset (videorate);
1007 plugin_init (GstPlugin * plugin)
1009 GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
1010 "VideoRate stream fixer");
1012 return gst_element_register (plugin, "videorate", GST_RANK_NONE,
1013 GST_TYPE_VIDEO_RATE);
1016 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1019 "Adjusts video frames",
1020 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)