-base: port elements to new video caps
[platform/upstream/gstreamer.git] / gst / videorate / gstvideorate.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /**
21  * SECTION:element-videorate
22  *
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.
25  *
26  * The correction is performed by dropping and duplicating frames, no fancy
27  * algorithm is used to interpolate frames (yet).
28  *
29  * By default the element will simply negotiate the same framerate on its
30  * source and sink pad.
31  *
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.
35  *
36  * A conversion to a specific framerate can be forced by using filtered caps on
37  * the source pad.
38  *
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).
44  *
45  * An input stream that needs no adjustments will thus never have dropped or
46  * duplicated frames.
47  *
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.
54  *
55  * <refsect2>
56  * <title>Example pipelines</title>
57  * |[
58  * gst-launch -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! videorate ! video/x-raw,framerate=15/1 ! xvimagesink
59  * ]| Decode an Ogg/Theora file and adjust the framerate to 15 fps before playing.
60  * To create the test Ogg/Theora file refer to the documentation of theoraenc.
61  * |[
62  * gst-launch -v v4l2src ! videorate ! video/x-raw,framerate=25/2 ! theoraenc ! oggmux ! filesink location=recording.ogg
63  * ]| Capture video from a V4L device, and adjust the stream to 12.5 fps before
64  * encoding to Ogg/Theora.
65  * </refsect2>
66  *
67  * Last reviewed on 2006-09-02 (0.10.11)
68  */
69
70 #ifdef HAVE_CONFIG_H
71 #include "config.h"
72 #endif
73
74 #include "gstvideorate.h"
75
76 GST_DEBUG_CATEGORY_STATIC (video_rate_debug);
77 #define GST_CAT_DEFAULT video_rate_debug
78
79 /* GstVideoRate signals and args */
80 enum
81 {
82   /* FILL ME */
83   LAST_SIGNAL
84 };
85
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
92 enum
93 {
94   ARG_0,
95   ARG_IN,
96   ARG_OUT,
97   ARG_DUP,
98   ARG_DROP,
99   ARG_SILENT,
100   ARG_NEW_PREF,
101   ARG_SKIP_TO_FIRST,
102   ARG_DROP_ONLY,
103   ARG_AVERAGE_PERIOD
104       /* FILL ME */
105 };
106
107 static GstStaticPadTemplate gst_video_rate_src_template =
108     GST_STATIC_PAD_TEMPLATE ("src",
109     GST_PAD_SRC,
110     GST_PAD_ALWAYS,
111     GST_STATIC_CAPS ("video/x-raw;" "image/jpeg;" "image/png")
112     );
113
114 static GstStaticPadTemplate gst_video_rate_sink_template =
115     GST_STATIC_PAD_TEMPLATE ("sink",
116     GST_PAD_SINK,
117     GST_PAD_ALWAYS,
118     GST_STATIC_CAPS ("video/x-raw;" "image/jpeg;" "image/png")
119     );
120
121 static void gst_video_rate_swap_prev (GstVideoRate * videorate,
122     GstBuffer * buffer, gint64 time);
123 static gboolean gst_video_rate_event (GstPad * pad, GstEvent * event);
124 static gboolean gst_video_rate_query (GstPad * pad, GstQuery * query);
125 static GstFlowReturn gst_video_rate_chain (GstPad * pad, GstBuffer * buffer);
126
127 static void gst_video_rate_set_property (GObject * object,
128     guint prop_id, const GValue * value, GParamSpec * pspec);
129 static void gst_video_rate_get_property (GObject * object,
130     guint prop_id, GValue * value, GParamSpec * pspec);
131
132 static GstStateChangeReturn gst_video_rate_change_state (GstElement * element,
133     GstStateChange transition);
134
135 /*static guint gst_video_rate_signals[LAST_SIGNAL] = { 0 }; */
136
137 static GParamSpec *pspec_drop = NULL;
138 static GParamSpec *pspec_duplicate = NULL;
139
140 #define gst_video_rate_parent_class parent_class
141 G_DEFINE_TYPE (GstVideoRate, gst_video_rate, GST_TYPE_ELEMENT);
142
143 static void
144 gst_video_rate_class_init (GstVideoRateClass * klass)
145 {
146   GObjectClass *object_class = G_OBJECT_CLASS (klass);
147   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
148
149   parent_class = g_type_class_peek_parent (klass);
150
151   object_class->set_property = gst_video_rate_set_property;
152   object_class->get_property = gst_video_rate_get_property;
153
154   g_object_class_install_property (object_class, ARG_IN,
155       g_param_spec_uint64 ("in", "In",
156           "Number of input frames", 0, G_MAXUINT64, 0,
157           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
158   g_object_class_install_property (object_class, ARG_OUT,
159       g_param_spec_uint64 ("out", "Out", "Number of output frames", 0,
160           G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
161   pspec_duplicate = g_param_spec_uint64 ("duplicate", "Duplicate",
162       "Number of duplicated frames", 0, G_MAXUINT64, 0,
163       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
164   g_object_class_install_property (object_class, ARG_DUP, pspec_duplicate);
165   pspec_drop = g_param_spec_uint64 ("drop", "Drop", "Number of dropped frames",
166       0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
167   g_object_class_install_property (object_class, ARG_DROP, pspec_drop);
168   g_object_class_install_property (object_class, ARG_SILENT,
169       g_param_spec_boolean ("silent", "silent",
170           "Don't emit notify for dropped and duplicated frames", DEFAULT_SILENT,
171           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172   g_object_class_install_property (object_class, ARG_NEW_PREF,
173       g_param_spec_double ("new-pref", "New Pref",
174           "Value indicating how much to prefer new frames (unused)", 0.0, 1.0,
175           DEFAULT_NEW_PREF, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176
177   /**
178    * GstVideoRate:skip-to-first:
179    * 
180    * Don't produce buffers before the first one we receive.
181    *
182    * Since: 0.10.25
183    */
184   g_object_class_install_property (object_class, ARG_SKIP_TO_FIRST,
185       g_param_spec_boolean ("skip-to-first", "Skip to first buffer",
186           "Don't produce buffers before the first one we receive",
187           DEFAULT_SKIP_TO_FIRST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188
189   gst_element_class_set_details_simple (element_class,
190       "Video rate adjuster", "Filter/Effect/Video",
191       "Drops/duplicates/adjusts timestamps on video frames to make a perfect stream",
192       "Wim Taymans <wim@fluendo.com>");
193
194   gst_element_class_add_pad_template (element_class,
195       gst_static_pad_template_get (&gst_video_rate_sink_template));
196   gst_element_class_add_pad_template (element_class,
197       gst_static_pad_template_get (&gst_video_rate_src_template));
198
199   /**
200    * GstVideoRate:drop-only:
201    *
202    * Only drop frames, no duplicates are produced.
203    *
204    * Since: 0.10.34
205    */
206   g_object_class_install_property (object_class, ARG_DROP_ONLY,
207       g_param_spec_boolean ("drop-only", "Only Drop",
208           "Only drop frames, no duplicates are produced",
209           DEFAULT_DROP_ONLY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210
211   /**
212    * GstVideoRate:average-period:
213    *
214    * Arrange for maximum framerate by dropping frames beyond a certain framerate,
215    * where the framerate is calculated using a moving average over the
216    * configured.
217    *
218    * Since: 0.10.34
219    */
220   g_object_class_install_property (object_class, ARG_AVERAGE_PERIOD,
221       g_param_spec_uint64 ("average-period", "Period over which to average",
222           "Period over which to average the framerate (in ns) (0 = disabled)",
223           0, G_MAXINT64, DEFAULT_AVERAGE_PERIOD,
224           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225
226   element_class->change_state = GST_DEBUG_FUNCPTR (gst_video_rate_change_state);
227 }
228
229 /* return the caps that can be used on out_pad given in_caps on in_pad */
230 static gboolean
231 gst_video_rate_transformcaps (GstPad * in_pad, GstCaps * in_caps,
232     GstPad * out_pad, GstCaps ** out_caps, GstCaps * filter)
233 {
234   GstCaps *intersect, *in_templ;
235   gint i;
236   GSList *extra_structures = NULL;
237   GSList *iter;
238
239   in_templ = gst_pad_get_pad_template_caps (in_pad);
240   intersect =
241       gst_caps_intersect_full (in_caps, in_templ, GST_CAPS_INTERSECT_FIRST);
242   gst_caps_unref (in_templ);
243
244   /* all possible framerates are allowed */
245   for (i = 0; i < gst_caps_get_size (intersect); i++) {
246     GstStructure *structure;
247
248     structure = gst_caps_get_structure (intersect, i);
249
250     if (gst_structure_has_field (structure, "framerate")) {
251       GstStructure *copy_structure;
252
253       copy_structure = gst_structure_copy (structure);
254       gst_structure_set (copy_structure,
255           "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
256       extra_structures = g_slist_append (extra_structures, copy_structure);
257     }
258   }
259
260   /* append the extra structures */
261   for (iter = extra_structures; iter != NULL; iter = g_slist_next (iter)) {
262     gst_caps_append_structure (intersect, (GstStructure *) iter->data);
263   }
264   g_slist_free (extra_structures);
265
266   if (filter) {
267     GstCaps *tmp;
268
269     tmp = gst_caps_intersect_full (filter, intersect, GST_CAPS_INTERSECT_FIRST);
270     gst_caps_unref (intersect);
271     intersect = tmp;
272   }
273
274   *out_caps = intersect;
275
276   return TRUE;
277 }
278
279 static GstCaps *
280 gst_video_rate_getcaps (GstPad * pad, GstCaps * filter)
281 {
282   GstVideoRate *videorate;
283   GstPad *otherpad;
284   GstCaps *caps;
285
286   videorate = GST_VIDEO_RATE (GST_PAD_PARENT (pad));
287
288   otherpad = (pad == videorate->srcpad) ? videorate->sinkpad :
289       videorate->srcpad;
290
291   /* we can do what the peer can */
292   caps = gst_pad_peer_get_caps (otherpad, filter);
293   if (caps) {
294     GstCaps *transform, *intersect;
295
296     gst_video_rate_transformcaps (otherpad, caps, pad, &transform, filter);
297
298     /* Now prefer the downstream caps if possible */
299     intersect =
300         gst_caps_intersect_full (caps, transform, GST_CAPS_INTERSECT_FIRST);
301     if (!gst_caps_is_empty (intersect)) {
302       gst_caps_append (intersect, transform);
303       gst_caps_unref (caps);
304       caps = intersect;
305     } else {
306       gst_caps_unref (intersect);
307       caps = transform;
308     }
309   } else {
310     /* no peer, our padtemplate is enough then */
311     caps = gst_pad_get_pad_template_caps (pad);
312     if (filter) {
313       GstCaps *intersection;
314       intersection =
315           gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
316       gst_caps_unref (caps);
317       caps = intersection;
318     }
319   }
320
321   return caps;
322 }
323
324 static gboolean
325 gst_video_rate_set_src_caps (GstVideoRate * videorate, GstCaps * caps)
326 {
327   GstStructure *structure;
328   gint rate_numerator, rate_denominator;
329
330   GST_DEBUG_OBJECT (videorate, "src caps %" GST_PTR_FORMAT, caps);
331
332   structure = gst_caps_get_structure (caps, 0);
333   if (!gst_structure_get_fraction (structure, "framerate",
334           &rate_numerator, &rate_denominator))
335     goto no_framerate;
336
337   /* out_frame_count is scaled by the frame rate caps when calculating next_ts.
338    * when the frame rate caps change, we must update base_ts and reset
339    * out_frame_count */
340   if (videorate->to_rate_numerator) {
341     videorate->base_ts +=
342         gst_util_uint64_scale (videorate->out_frame_count,
343         videorate->to_rate_denominator * GST_SECOND,
344         videorate->to_rate_numerator);
345   }
346   videorate->out_frame_count = 0;
347   videorate->to_rate_numerator = rate_numerator;
348   videorate->to_rate_denominator = rate_denominator;
349   videorate->wanted_diff = gst_util_uint64_scale_int (GST_SECOND,
350       rate_denominator, rate_numerator);
351
352   gst_pad_push_event (videorate->srcpad, gst_event_new_caps (caps));
353
354   return TRUE;
355
356   /* ERRORS */
357 no_framerate:
358   {
359     GST_DEBUG_OBJECT (videorate, "no framerate specified");
360     return FALSE;
361   }
362 }
363
364 static gboolean
365 gst_video_rate_set_sink_caps (GstVideoRate * videorate, GstCaps * caps)
366 {
367   GstStructure *structure;
368   gboolean ret = TRUE;
369   gint rate_numerator, rate_denominator;
370
371   GST_DEBUG_OBJECT (videorate, "sink caps %" GST_PTR_FORMAT, caps);
372
373   structure = gst_caps_get_structure (caps, 0);
374   if (!gst_structure_get_fraction (structure, "framerate",
375           &rate_numerator, &rate_denominator))
376     goto no_framerate;
377
378   videorate->from_rate_numerator = rate_numerator;
379   videorate->from_rate_denominator = rate_denominator;
380
381   /* now try to find something for the peer */
382   if (gst_pad_peer_accept_caps (videorate->srcpad, caps)) {
383     /* the peer accepts the caps as they are */
384     ret = gst_video_rate_set_src_caps (videorate, caps);
385   } else {
386     GstCaps *transform = NULL;
387
388     ret = FALSE;
389
390     /* see how we can transform the input caps */
391     if (!gst_video_rate_transformcaps (videorate->sinkpad, caps,
392             videorate->srcpad, &transform, NULL))
393       goto no_transform;
394
395     GST_DEBUG_OBJECT (videorate, "transform %" GST_PTR_FORMAT, transform);
396
397     /* see what the peer can do */
398     caps = gst_pad_peer_get_caps (videorate->srcpad, transform);
399
400     GST_DEBUG_OBJECT (videorate, "icaps %" GST_PTR_FORMAT, caps);
401
402     /* could turn up empty, due to e.g. colorspace etc */
403     if (gst_caps_get_size (caps) == 0) {
404       gst_caps_unref (caps);
405       goto no_transform;
406     }
407
408     /* take first possibility */
409     caps = gst_caps_make_writable (caps);
410     gst_caps_truncate (caps);
411     structure = gst_caps_get_structure (caps, 0);
412
413     /* and fixate */
414     gst_structure_fixate_field_nearest_fraction (structure, "framerate",
415         rate_numerator, rate_denominator);
416     gst_structure_get_fraction (structure, "framerate",
417         &rate_numerator, &rate_denominator);
418
419     videorate->to_rate_numerator = rate_numerator;
420     videorate->to_rate_denominator = rate_denominator;
421
422     if (gst_structure_has_field (structure, "interlaced"))
423       gst_structure_fixate_field_boolean (structure, "interlaced", FALSE);
424     if (gst_structure_has_field (structure, "color-matrix"))
425       gst_structure_fixate_field_string (structure, "color-matrix", "sdtv");
426     if (gst_structure_has_field (structure, "chroma-site"))
427       gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
428     if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
429       gst_structure_fixate_field_nearest_fraction (structure,
430           "pixel-aspect-ratio", 1, 1);
431
432     ret = gst_video_rate_set_src_caps (videorate, caps);
433     gst_caps_unref (caps);
434   }
435 done:
436   /* After a setcaps, our caps may have changed. In that case, we can't use
437    * the old buffer, if there was one (it might have different dimensions) */
438   GST_DEBUG_OBJECT (videorate, "swapping old buffers");
439   gst_video_rate_swap_prev (videorate, NULL, GST_CLOCK_TIME_NONE);
440
441   return ret;
442
443 no_framerate:
444   {
445     GST_DEBUG_OBJECT (videorate, "no framerate specified");
446     goto done;
447   }
448 no_transform:
449   {
450     GST_DEBUG_OBJECT (videorate, "no framerate transform possible");
451     ret = FALSE;
452     goto done;
453   }
454 }
455
456 static void
457 gst_video_rate_reset (GstVideoRate * videorate)
458 {
459   GST_DEBUG_OBJECT (videorate, "resetting internal variables");
460
461   videorate->in = 0;
462   videorate->out = 0;
463   videorate->base_ts = 0;
464   videorate->out_frame_count = 0;
465   videorate->drop = 0;
466   videorate->dup = 0;
467   videorate->next_ts = GST_CLOCK_TIME_NONE;
468   videorate->last_ts = GST_CLOCK_TIME_NONE;
469   videorate->discont = TRUE;
470   videorate->average = 0;
471   gst_video_rate_swap_prev (videorate, NULL, 0);
472
473   gst_segment_init (&videorate->segment, GST_FORMAT_TIME);
474 }
475
476 static void
477 gst_video_rate_init (GstVideoRate * videorate)
478 {
479   videorate->sinkpad =
480       gst_pad_new_from_static_template (&gst_video_rate_sink_template, "sink");
481   gst_pad_set_event_function (videorate->sinkpad,
482       GST_DEBUG_FUNCPTR (gst_video_rate_event));
483   gst_pad_set_chain_function (videorate->sinkpad,
484       GST_DEBUG_FUNCPTR (gst_video_rate_chain));
485   gst_pad_set_getcaps_function (videorate->sinkpad,
486       GST_DEBUG_FUNCPTR (gst_video_rate_getcaps));
487   gst_element_add_pad (GST_ELEMENT (videorate), videorate->sinkpad);
488
489   videorate->srcpad =
490       gst_pad_new_from_static_template (&gst_video_rate_src_template, "src");
491   gst_pad_set_query_function (videorate->srcpad,
492       GST_DEBUG_FUNCPTR (gst_video_rate_query));
493   gst_pad_set_getcaps_function (videorate->srcpad,
494       GST_DEBUG_FUNCPTR (gst_video_rate_getcaps));
495   gst_element_add_pad (GST_ELEMENT (videorate), videorate->srcpad);
496
497   gst_video_rate_reset (videorate);
498   videorate->silent = DEFAULT_SILENT;
499   videorate->new_pref = DEFAULT_NEW_PREF;
500   videorate->drop_only = DEFAULT_DROP_ONLY;
501   videorate->average_period = DEFAULT_AVERAGE_PERIOD;
502
503   videorate->from_rate_numerator = 0;
504   videorate->from_rate_denominator = 0;
505   videorate->to_rate_numerator = 0;
506   videorate->to_rate_denominator = 0;
507 }
508
509 /* flush the oldest buffer */
510 static GstFlowReturn
511 gst_video_rate_flush_prev (GstVideoRate * videorate, gboolean duplicate)
512 {
513   GstFlowReturn res;
514   GstBuffer *outbuf;
515   GstClockTime push_ts;
516
517   if (!videorate->prevbuf)
518     goto eos_before_buffers;
519
520   /* make sure we can write to the metadata */
521   outbuf = gst_buffer_make_writable (gst_buffer_ref (videorate->prevbuf));
522
523   GST_BUFFER_OFFSET (outbuf) = videorate->out;
524   GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
525
526   if (videorate->discont) {
527     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
528     videorate->discont = FALSE;
529   } else
530     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
531
532   if (duplicate)
533     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
534   else
535     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
536
537   /* this is the timestamp we put on the buffer */
538   push_ts = videorate->next_ts;
539
540   videorate->out++;
541   videorate->out_frame_count++;
542   if (videorate->to_rate_numerator) {
543     /* interpolate next expected timestamp in the segment */
544     videorate->next_ts =
545         videorate->segment.base + videorate->segment.start +
546         videorate->base_ts + gst_util_uint64_scale (videorate->out_frame_count,
547         videorate->to_rate_denominator * GST_SECOND,
548         videorate->to_rate_numerator);
549     GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
550   }
551
552   /* We do not need to update time in VFR (variable frame rate) mode */
553   if (!videorate->drop_only) {
554     /* adapt for looping, bring back to time in current segment. */
555     GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.base;
556   }
557
558   GST_LOG_OBJECT (videorate,
559       "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
560       GST_TIME_ARGS (push_ts));
561
562   res = gst_pad_push (videorate->srcpad, outbuf);
563
564   return res;
565
566   /* WARNINGS */
567 eos_before_buffers:
568   {
569     GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
570     return GST_FLOW_OK;
571   }
572 }
573
574 static void
575 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
576     gint64 time)
577 {
578   GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
579   if (videorate->prevbuf)
580     gst_buffer_unref (videorate->prevbuf);
581   videorate->prevbuf = buffer;
582   videorate->prev_ts = time;
583 }
584
585 static void
586 gst_video_rate_notify_drop (GstVideoRate * videorate)
587 {
588 #if !GLIB_CHECK_VERSION(2,26,0)
589   g_object_notify ((GObject *) videorate, "drop");
590 #else
591   g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
592 #endif
593 }
594
595 static void
596 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
597 {
598 #if !GLIB_CHECK_VERSION(2,26,0)
599   g_object_notify ((GObject *) videorate, "duplicate");
600 #else
601   g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
602 #endif
603 }
604
605 #define MAGIC_LIMIT  25
606 static gboolean
607 gst_video_rate_event (GstPad * pad, GstEvent * event)
608 {
609   GstVideoRate *videorate;
610   gboolean ret;
611
612   videorate = GST_VIDEO_RATE (gst_pad_get_parent (pad));
613
614   switch (GST_EVENT_TYPE (event)) {
615     case GST_EVENT_CAPS:
616     {
617       GstCaps *caps;
618
619       gst_event_parse_caps (event, &caps);
620       ret = gst_video_rate_set_sink_caps (videorate, caps);
621       gst_event_unref (event);
622
623       /* don't forward */
624       goto done;
625     }
626     case GST_EVENT_SEGMENT:
627     {
628       const GstSegment *segment;
629
630       gst_event_parse_segment (event, &segment);
631
632       if (segment->format != GST_FORMAT_TIME)
633         goto format_error;
634
635       GST_DEBUG_OBJECT (videorate, "handle NEWSEGMENT");
636
637       /* close up the previous segment, if appropriate */
638       if (videorate->prevbuf) {
639         gint count = 0;
640         GstFlowReturn res;
641
642         res = GST_FLOW_OK;
643         /* fill up to the end of current segment,
644          * or only send out the stored buffer if there is no specific stop.
645          * regardless, prevent going loopy in strange cases */
646         while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
647             ((GST_CLOCK_TIME_IS_VALID (videorate->segment.stop) &&
648                     videorate->next_ts - videorate->segment.base
649                     < videorate->segment.stop)
650                 || count < 1)) {
651           res = gst_video_rate_flush_prev (videorate, count > 0);
652           count++;
653         }
654         if (count > 1) {
655           videorate->dup += count - 1;
656           if (!videorate->silent)
657             gst_video_rate_notify_duplicate (videorate);
658         } else if (count == 0) {
659           videorate->drop++;
660           if (!videorate->silent)
661             gst_video_rate_notify_drop (videorate);
662         }
663         /* clean up for the new one; _chain will resume from the new start */
664         videorate->base_ts = 0;
665         videorate->out_frame_count = 0;
666         gst_video_rate_swap_prev (videorate, NULL, 0);
667         videorate->next_ts = GST_CLOCK_TIME_NONE;
668       }
669
670       /* We just want to update the accumulated stream_time  */
671       gst_segment_copy_into (segment, &videorate->segment);
672
673       GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
674           &videorate->segment);
675       break;
676     }
677     case GST_EVENT_EOS:{
678       gint count = 0;
679       GstFlowReturn res = GST_FLOW_OK;
680
681       GST_DEBUG_OBJECT (videorate, "Got EOS");
682
683       /* If the segment has a stop position, fill the segment */
684       if (GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
685         /* fill up to the end of current segment,
686          * or only send out the stored buffer if there is no specific stop.
687          * regardless, prevent going loopy in strange cases */
688         while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
689             ((videorate->next_ts - videorate->segment.base <
690                     videorate->segment.stop)
691                 || count < 1)) {
692           res = gst_video_rate_flush_prev (videorate, count > 0);
693           count++;
694         }
695       } else if (videorate->prevbuf) {
696         /* Output at least one frame but if the buffer duration is valid, output
697          * enough frames to use the complete buffer duration */
698         if (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf)) {
699           GstClockTime end_ts =
700               videorate->next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
701
702           while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
703               ((videorate->next_ts - videorate->segment.base < end_ts)
704                   || count < 1)) {
705             res = gst_video_rate_flush_prev (videorate, count > 0);
706             count++;
707           }
708         } else {
709           res = gst_video_rate_flush_prev (videorate, FALSE);
710           count = 1;
711         }
712       }
713
714       if (count > 1) {
715         videorate->dup += count - 1;
716         if (!videorate->silent)
717           gst_video_rate_notify_duplicate (videorate);
718       } else if (count == 0) {
719         videorate->drop++;
720         if (!videorate->silent)
721           gst_video_rate_notify_drop (videorate);
722       }
723
724       break;
725     }
726     case GST_EVENT_FLUSH_STOP:
727       /* also resets the segment */
728       GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
729       gst_video_rate_reset (videorate);
730       break;
731     default:
732       break;
733   }
734
735   ret = gst_pad_push_event (videorate->srcpad, event);
736
737 done:
738   gst_object_unref (videorate);
739
740   return ret;
741
742   /* ERRORS */
743 format_error:
744   {
745     GST_WARNING_OBJECT (videorate,
746         "Got segment but doesn't have GST_FORMAT_TIME value");
747     gst_event_unref (event);
748     ret = FALSE;
749     goto done;
750   }
751 }
752
753 static gboolean
754 gst_video_rate_query (GstPad * pad, GstQuery * query)
755 {
756   GstVideoRate *videorate;
757   gboolean res = FALSE;
758
759   videorate = GST_VIDEO_RATE (gst_pad_get_parent (pad));
760
761   switch (GST_QUERY_TYPE (query)) {
762     case GST_QUERY_LATENCY:
763     {
764       GstClockTime min, max;
765       gboolean live;
766       guint64 latency;
767       GstPad *peer;
768
769       if ((peer = gst_pad_get_peer (videorate->sinkpad))) {
770         if ((res = gst_pad_query (peer, query))) {
771           gst_query_parse_latency (query, &live, &min, &max);
772
773           GST_DEBUG_OBJECT (videorate, "Peer latency: min %"
774               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
775               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
776
777           if (videorate->from_rate_numerator != 0) {
778             /* add latency. We don't really know since we hold on to the frames
779              * until we get a next frame, which can be anything. We assume
780              * however that this will take from_rate time. */
781             latency = gst_util_uint64_scale (GST_SECOND,
782                 videorate->from_rate_denominator,
783                 videorate->from_rate_numerator);
784           } else {
785             /* no input framerate, we don't know */
786             latency = 0;
787           }
788
789           GST_DEBUG_OBJECT (videorate, "Our latency: %"
790               GST_TIME_FORMAT, GST_TIME_ARGS (latency));
791
792           min += latency;
793           if (max != -1)
794             max += latency;
795
796           GST_DEBUG_OBJECT (videorate, "Calculated total latency : min %"
797               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
798               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
799
800           gst_query_set_latency (query, live, min, max);
801         }
802         gst_object_unref (peer);
803       }
804       break;
805     }
806     default:
807       res = gst_pad_query_default (pad, query);
808       break;
809   }
810   gst_object_unref (videorate);
811
812   return res;
813 }
814
815 static GstFlowReturn
816 gst_video_rate_chain_max_avg (GstVideoRate * videorate, GstBuffer * buf)
817 {
818   GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
819
820   videorate->in++;
821
822   if (!GST_CLOCK_TIME_IS_VALID (ts) || videorate->wanted_diff == 0)
823     goto push;
824
825   /* drop frames if they exceed our output rate */
826   if (GST_CLOCK_TIME_IS_VALID (videorate->last_ts)) {
827     GstClockTimeDiff diff = ts - videorate->last_ts;
828
829     /* Drop buffer if its early compared to the desired frame rate and
830      * the current average is higher than the desired average
831      */
832     if (diff < videorate->wanted_diff &&
833         videorate->average < videorate->wanted_diff)
834       goto drop;
835
836     /* Update average */
837     if (videorate->average) {
838       GstClockTimeDiff wanted_diff;
839
840       if (G_LIKELY (videorate->average_period > videorate->wanted_diff))
841         wanted_diff = videorate->wanted_diff;
842       else
843         wanted_diff = videorate->average_period * 10;
844
845       videorate->average =
846           gst_util_uint64_scale_round (videorate->average,
847           videorate->average_period - wanted_diff,
848           videorate->average_period) +
849           gst_util_uint64_scale_round (diff, wanted_diff,
850           videorate->average_period);
851     } else {
852       videorate->average = diff;
853     }
854   }
855
856   videorate->last_ts = ts;
857
858 push:
859   videorate->out++;
860
861   return gst_pad_push (videorate->srcpad, buf);
862
863 drop:
864   gst_buffer_unref (buf);
865   if (!videorate->silent)
866     gst_video_rate_notify_drop (videorate);
867   return GST_FLOW_OK;
868 }
869
870 static GstFlowReturn
871 gst_video_rate_chain (GstPad * pad, GstBuffer * buffer)
872 {
873   GstVideoRate *videorate;
874   GstFlowReturn res = GST_FLOW_OK;
875   GstClockTime intime, in_ts, in_dur;
876   GstClockTime avg_period;
877   gboolean skip = FALSE;
878
879   videorate = GST_VIDEO_RATE (GST_PAD_PARENT (pad));
880
881   /* make sure the denominators are not 0 */
882   if (videorate->from_rate_denominator == 0 ||
883       videorate->to_rate_denominator == 0)
884     goto not_negotiated;
885
886   GST_OBJECT_LOCK (videorate);
887   avg_period = videorate->average_period_set;
888   GST_OBJECT_UNLOCK (videorate);
889
890   /* MT-safe switching between modes */
891   if (G_UNLIKELY (avg_period != videorate->average_period)) {
892     videorate->average_period = avg_period;
893     videorate->last_ts = GST_CLOCK_TIME_NONE;
894     if (avg_period && !videorate->average) {
895       /* enabling average mode */
896       videorate->average = 0;
897     } else {
898       /* enable regular mode */
899       gst_video_rate_swap_prev (videorate, NULL, 0);
900       /* arrange for skip-to-first behaviour */
901       videorate->next_ts = GST_CLOCK_TIME_NONE;
902       skip = TRUE;
903     }
904   }
905
906   if (videorate->average_period > 0)
907     return gst_video_rate_chain_max_avg (videorate, buffer);
908
909   in_ts = GST_BUFFER_TIMESTAMP (buffer);
910   in_dur = GST_BUFFER_DURATION (buffer);
911
912   if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE)) {
913     in_ts = videorate->last_ts;
914     if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE))
915       goto invalid_buffer;
916   }
917
918   /* get the time of the next expected buffer timestamp, we use this when the
919    * next buffer has -1 as a timestamp */
920   videorate->last_ts = in_ts;
921   if (in_dur != GST_CLOCK_TIME_NONE)
922     videorate->last_ts += in_dur;
923
924   GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
925       GST_TIME_ARGS (in_ts));
926
927   /* the input time is the time in the segment + all previously accumulated
928    * segments */
929   intime = in_ts + videorate->segment.base;
930
931   /* we need to have two buffers to compare */
932   if (videorate->prevbuf == NULL) {
933     gst_video_rate_swap_prev (videorate, buffer, intime);
934     videorate->in++;
935     if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts)) {
936       /* new buffer, we expect to output a buffer that matches the first
937        * timestamp in the segment */
938       if (videorate->skip_to_first || skip) {
939         videorate->next_ts = intime;
940         videorate->base_ts = in_ts - videorate->segment.start;
941         videorate->out_frame_count = 0;
942       } else {
943         videorate->next_ts = videorate->segment.start + videorate->segment.base;
944       }
945     }
946   } else {
947     GstClockTime prevtime;
948     gint count = 0;
949     gint64 diff1, diff2;
950
951     prevtime = videorate->prev_ts;
952
953     GST_LOG_OBJECT (videorate,
954         "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
955         " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
956         GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
957
958     videorate->in++;
959
960     /* drop new buffer if it's before previous one */
961     if (intime < prevtime) {
962       GST_DEBUG_OBJECT (videorate,
963           "The new buffer (%" GST_TIME_FORMAT
964           ") is before the previous buffer (%"
965           GST_TIME_FORMAT "). Dropping new buffer.",
966           GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
967       videorate->drop++;
968       if (!videorate->silent)
969         gst_video_rate_notify_drop (videorate);
970       gst_buffer_unref (buffer);
971       goto done;
972     }
973
974     /* got 2 buffers, see which one is the best */
975     do {
976
977       diff1 = prevtime - videorate->next_ts;
978       diff2 = intime - videorate->next_ts;
979
980       /* take absolute values, beware: abs and ABS don't work for gint64 */
981       if (diff1 < 0)
982         diff1 = -diff1;
983       if (diff2 < 0)
984         diff2 = -diff2;
985
986       GST_LOG_OBJECT (videorate,
987           "diff with prev %" GST_TIME_FORMAT " diff with new %"
988           GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
989           GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
990           GST_TIME_ARGS (videorate->next_ts));
991
992       /* output first one when its the best */
993       if (diff1 <= diff2) {
994         count++;
995
996         /* on error the _flush function posted a warning already */
997         if ((res =
998                 gst_video_rate_flush_prev (videorate,
999                     count > 1)) != GST_FLOW_OK) {
1000           gst_buffer_unref (buffer);
1001           goto done;
1002         }
1003       }
1004
1005       /* Do not produce any dups. We can exit loop now */
1006       if (videorate->drop_only)
1007         break;
1008       /* continue while the first one was the best, if they were equal avoid
1009        * going into an infinite loop */
1010     }
1011     while (diff1 < diff2);
1012
1013     /* if we outputed the first buffer more then once, we have dups */
1014     if (count > 1) {
1015       videorate->dup += count - 1;
1016       if (!videorate->silent)
1017         gst_video_rate_notify_duplicate (videorate);
1018     }
1019     /* if we didn't output the first buffer, we have a drop */
1020     else if (count == 0) {
1021       videorate->drop++;
1022
1023       if (!videorate->silent)
1024         gst_video_rate_notify_drop (videorate);
1025
1026       GST_LOG_OBJECT (videorate,
1027           "new is best, old never used, drop, outgoing ts %"
1028           GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
1029     }
1030     GST_LOG_OBJECT (videorate,
1031         "END, putting new in old, diff1 %" GST_TIME_FORMAT
1032         ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
1033         ", in %" G_GUINT64_FORMAT ", out %" G_GUINT64_FORMAT ", drop %"
1034         G_GUINT64_FORMAT ", dup %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff1),
1035         GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
1036         videorate->in, videorate->out, videorate->drop, videorate->dup);
1037
1038     /* swap in new one when it's the best */
1039     gst_video_rate_swap_prev (videorate, buffer, intime);
1040   }
1041 done:
1042   return res;
1043
1044   /* ERRORS */
1045 not_negotiated:
1046   {
1047     GST_WARNING_OBJECT (videorate, "no framerate negotiated");
1048     gst_buffer_unref (buffer);
1049     res = GST_FLOW_NOT_NEGOTIATED;
1050     goto done;
1051   }
1052
1053 invalid_buffer:
1054   {
1055     GST_WARNING_OBJECT (videorate,
1056         "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
1057     gst_buffer_unref (buffer);
1058     goto done;
1059   }
1060 }
1061
1062 static void
1063 gst_video_rate_set_property (GObject * object,
1064     guint prop_id, const GValue * value, GParamSpec * pspec)
1065 {
1066   GstVideoRate *videorate = GST_VIDEO_RATE (object);
1067
1068   GST_OBJECT_LOCK (videorate);
1069   switch (prop_id) {
1070     case ARG_SILENT:
1071       videorate->silent = g_value_get_boolean (value);
1072       break;
1073     case ARG_NEW_PREF:
1074       videorate->new_pref = g_value_get_double (value);
1075       break;
1076     case ARG_SKIP_TO_FIRST:
1077       videorate->skip_to_first = g_value_get_boolean (value);
1078       break;
1079     case ARG_DROP_ONLY:
1080       videorate->drop_only = g_value_get_boolean (value);
1081       break;
1082     case ARG_AVERAGE_PERIOD:
1083       videorate->average_period = g_value_get_uint64 (value);
1084       break;
1085     default:
1086       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1087       break;
1088   }
1089   GST_OBJECT_UNLOCK (videorate);
1090 }
1091
1092 static void
1093 gst_video_rate_get_property (GObject * object,
1094     guint prop_id, GValue * value, GParamSpec * pspec)
1095 {
1096   GstVideoRate *videorate = GST_VIDEO_RATE (object);
1097
1098   GST_OBJECT_LOCK (videorate);
1099   switch (prop_id) {
1100     case ARG_IN:
1101       g_value_set_uint64 (value, videorate->in);
1102       break;
1103     case ARG_OUT:
1104       g_value_set_uint64 (value, videorate->out);
1105       break;
1106     case ARG_DUP:
1107       g_value_set_uint64 (value, videorate->dup);
1108       break;
1109     case ARG_DROP:
1110       g_value_set_uint64 (value, videorate->drop);
1111       break;
1112     case ARG_SILENT:
1113       g_value_set_boolean (value, videorate->silent);
1114       break;
1115     case ARG_NEW_PREF:
1116       g_value_set_double (value, videorate->new_pref);
1117       break;
1118     case ARG_SKIP_TO_FIRST:
1119       g_value_set_boolean (value, videorate->skip_to_first);
1120       break;
1121     case ARG_DROP_ONLY:
1122       g_value_set_boolean (value, videorate->drop_only);
1123       break;
1124     case ARG_AVERAGE_PERIOD:
1125       g_value_set_uint64 (value, videorate->average_period);
1126       break;
1127     default:
1128       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1129       break;
1130   }
1131   GST_OBJECT_UNLOCK (videorate);
1132 }
1133
1134 static GstStateChangeReturn
1135 gst_video_rate_change_state (GstElement * element, GstStateChange transition)
1136 {
1137   GstStateChangeReturn ret;
1138   GstVideoRate *videorate;
1139
1140   videorate = GST_VIDEO_RATE (element);
1141
1142   switch (transition) {
1143     case GST_STATE_CHANGE_READY_TO_PAUSED:
1144       videorate->discont = TRUE;
1145       videorate->last_ts = -1;
1146       break;
1147     default:
1148       break;
1149   }
1150
1151   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1152
1153   switch (transition) {
1154     case GST_STATE_CHANGE_PAUSED_TO_READY:
1155       gst_video_rate_reset (videorate);
1156       break;
1157     default:
1158       break;
1159   }
1160
1161   return ret;
1162 }
1163
1164 static gboolean
1165 plugin_init (GstPlugin * plugin)
1166 {
1167   GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
1168       "VideoRate stream fixer");
1169
1170   return gst_element_register (plugin, "videorate", GST_RANK_NONE,
1171       GST_TYPE_VIDEO_RATE);
1172 }
1173
1174 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1175     GST_VERSION_MINOR,
1176     "videorate",
1177     "Adjusts video frames",
1178     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)