tizen 2.0 init
[framework/multimedia/gst-plugins-base0.10.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-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.
61  * |[
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.
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 #define DEFAULT_MAX_RATE        G_MAXINT
92 #define DEFAULT_FORCE_FPS_N     -1
93 #define DEFAULT_FORCE_FPS_D     1
94
95 enum
96 {
97   PROP_0,
98   PROP_IN,
99   PROP_OUT,
100   PROP_DUP,
101   PROP_DROP,
102   PROP_SILENT,
103   PROP_NEW_PREF,
104   PROP_SKIP_TO_FIRST,
105   PROP_DROP_ONLY,
106   PROP_AVERAGE_PERIOD,
107   PROP_MAX_RATE,
108   PROP_FORCE_FPS
109       /* FILL ME */
110 };
111
112 static GstStaticPadTemplate gst_video_rate_src_template =
113     GST_STATIC_PAD_TEMPLATE ("src",
114     GST_PAD_SRC,
115     GST_PAD_ALWAYS,
116     GST_STATIC_CAPS ("video/x-raw-yuv;"
117         "video/x-raw-rgb;" "video/x-raw-gray;" "image/jpeg;" "image/png")
118     );
119
120 static GstStaticPadTemplate gst_video_rate_sink_template =
121     GST_STATIC_PAD_TEMPLATE ("sink",
122     GST_PAD_SINK,
123     GST_PAD_ALWAYS,
124     GST_STATIC_CAPS ("video/x-raw-yuv;"
125         "video/x-raw-rgb;" "video/x-raw-gray;" "image/jpeg;" "image/png")
126     );
127
128 static void gst_video_rate_swap_prev (GstVideoRate * videorate,
129     GstBuffer * buffer, gint64 time);
130 static gboolean gst_video_rate_event (GstBaseTransform * trans,
131     GstEvent * event);
132 static gboolean gst_video_rate_query (GstBaseTransform * trans,
133     GstPadDirection direction, GstQuery * query);
134
135 static gboolean gst_video_rate_setcaps (GstBaseTransform * trans,
136     GstCaps * in_caps, GstCaps * out_caps);
137
138 static GstCaps *gst_video_rate_transform_caps (GstBaseTransform * trans,
139     GstPadDirection direction, GstCaps * caps);
140
141 static void gst_video_rate_fixate_caps (GstBaseTransform * trans,
142     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
143
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,
147     GstBuffer * buf);
148
149 static gboolean gst_video_rate_start (GstBaseTransform * trans);
150 static gboolean gst_video_rate_stop (GstBaseTransform * trans);
151
152
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);
157
158 static GParamSpec *pspec_drop = NULL;
159 static GParamSpec *pspec_duplicate = NULL;
160
161 GST_BOILERPLATE (GstVideoRate, gst_video_rate,
162     GstBaseTransform, GST_TYPE_BASE_TRANSFORM);
163
164 static void
165 gst_video_rate_base_init (gpointer g_class)
166 {
167   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
168
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>");
173
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);
178 }
179
180 static void
181 gst_video_rate_class_init (GstVideoRateClass * klass)
182 {
183   GObjectClass *object_class = G_OBJECT_CLASS (klass);
184   GstBaseTransformClass *base_class = GST_BASE_TRANSFORM_CLASS (klass);
185
186   parent_class = g_type_class_peek_parent (klass);
187
188   object_class->set_property = gst_video_rate_set_property;
189   object_class->get_property = gst_video_rate_get_property;
190
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);
202
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));
225
226   /**
227    * GstVideoRate:skip-to-first:
228    * 
229    * Don't produce buffers before the first one we receive.
230    *
231    * Since: 0.10.25
232    */
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));
237
238   /**
239    * GstVideoRate:drop-only:
240    *
241    * Only drop frames, no duplicates are produced.
242    *
243    * Since: 0.10.36
244    */
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));
249
250   /**
251    * GstVideoRate:average-period:
252    *
253    * Arrange for maximum framerate by dropping frames beyond a certain framerate,
254    * where the framerate is calculated using a moving average over the
255    * configured.
256    *
257    * Since: 0.10.36
258    */
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));
264
265   /**
266    * GstVideoRate:max-rate:
267    *
268    * maximum framerate to pass through
269    *
270    * Since: 0.10.36
271    */
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));
278
279   /**
280    * GstVideoRate:force-fps:
281    *
282    * Forced output framerate
283    *
284    * Since: 0.10.36
285    */
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));
291 }
292
293 static void
294 gst_value_fraction_get_extremes (const GValue * v,
295     gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
296 {
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;
302
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);
306
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;
312     int i, n;
313
314     *min_num = G_MAXINT;
315     *min_denom = 1;
316     *max_num = 0;
317     *max_denom = 1;
318
319     n = gst_value_list_get_size (v);
320
321     g_assert (n > 0);
322
323     for (i = 0; i < n; i++) {
324       const GValue *t = gst_value_list_get_value (v, i);
325
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) {
328         *min_num = min_n;
329         *min_denom = min_d;
330       }
331
332       if (gst_util_fraction_compare (max_n, max_d, *max_num, *max_denom) > 0) {
333         *max_num = max_n;
334         *max_denom = max_d;
335       }
336     }
337   } else {
338     g_warning ("Unknown type for framerate");
339     *min_num = 0;
340     *min_denom = 1;
341     *max_num = G_MAXINT;
342     *max_denom = 1;
343   }
344 }
345
346 /* Clamp the framerate in a caps structure to be a smaller range then
347  * [1...max_rate], otherwise return false */
348 static gboolean
349 gst_video_max_rate_clamp_structure (GstStructure * s, gint maxrate,
350     gint * min_num, gint * min_denom, gint * max_num, gint * max_denom)
351 {
352   gboolean ret = FALSE;
353
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 */
357     goto out;
358   } else {
359     const GValue *v;
360     GValue intersection = { 0, };
361     GValue clamp = { 0, };
362     gint tmp_num, tmp_denom;
363
364     g_value_init (&clamp, GST_TYPE_FRACTION_RANGE);
365     gst_value_set_fraction_range_full (&clamp, 0, 1, maxrate, 1);
366
367     v = gst_structure_get_value (s, "framerate");
368     ret = gst_value_intersect (&intersection, v, &clamp);
369     g_value_unset (&clamp);
370
371     if (!ret)
372       goto out;
373
374     gst_value_fraction_get_extremes (&intersection,
375         min_num, min_denom, max_num, max_denom);
376
377     gst_value_fraction_get_extremes (v,
378         &tmp_num, &tmp_denom, max_num, max_denom);
379
380     if (gst_util_fraction_compare (*max_num, *max_denom, maxrate, 1) > 0) {
381       *max_num = maxrate;
382       *max_denom = 1;
383     }
384
385     gst_structure_take_value (s, "framerate", &intersection);
386   }
387
388 out:
389   return ret;
390 }
391
392 static GstCaps *
393 gst_video_rate_transform_caps (GstBaseTransform * trans,
394     GstPadDirection direction, GstCaps * caps)
395 {
396   GstVideoRate *videorate = GST_VIDEO_RATE (trans);
397   GstCaps *ret;
398   GstStructure *s, *s2;
399   GstStructure *s3 = NULL;
400   int maxrate = g_atomic_int_get (&videorate->max_rate);
401
402   /* Should always be called with simple caps */
403   g_return_val_if_fail (GST_CAPS_IS_SIMPLE (caps), NULL);
404
405   ret = gst_caps_copy (caps);
406
407   s = gst_caps_get_structure (ret, 0);
408   s2 = gst_structure_copy (s);
409
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);
415     } else {
416       gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
417           G_MAXINT, 1, NULL);
418     }
419   } else if (videorate->drop_only) {
420     gint min_num = 0, min_denom = 1;
421     gint max_num = G_MAXINT, max_denom = 1;
422
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)) {
426       min_num = 0;
427       min_denom = 1;
428       max_num = maxrate;
429       max_denom = 1;
430
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 :)
435        *
436        * In case [X..maxrate] == [X..maxint], skip as we'll set it later
437        */
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);
441       else
442         gst_caps_remove_structure (ret, 0);
443     }
444
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);
450
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);
455       }
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);
460     }
461   } else if (direction == GST_PAD_SINK) {
462     gint min_num = 0, min_denom = 1;
463     gint max_num = G_MAXINT, max_denom = 1;
464
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);
468
469     gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
470         maxrate, 1, NULL);
471   } else {
472     /* set the framerate as a range */
473     gst_structure_set (s2, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
474         G_MAXINT, 1, NULL);
475   }
476
477   gst_caps_merge_structure (ret, s2);
478   if (s3 != NULL)
479     gst_caps_merge_structure (ret, s3);
480
481   return ret;
482 }
483
484 static void
485 gst_video_rate_fixate_caps (GstBaseTransform * trans,
486     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
487 {
488   GstStructure *s;
489   gint num, denom;
490
491   s = gst_caps_get_structure (caps, 0);
492   if (G_UNLIKELY (!gst_structure_get_fraction (s, "framerate", &num, &denom)))
493     return;
494
495   s = gst_caps_get_structure (othercaps, 0);
496   gst_structure_fixate_field_nearest_fraction (s, "framerate", num, denom);
497 }
498
499 static gboolean
500 gst_video_rate_setcaps (GstBaseTransform * trans, GstCaps * in_caps,
501     GstCaps * out_caps)
502 {
503   GstVideoRate *videorate;
504   GstStructure *structure;
505   gboolean ret = TRUE;
506   gint rate_numerator, rate_denominator;
507
508   videorate = GST_VIDEO_RATE (trans);
509
510   GST_DEBUG_OBJECT (trans, "setcaps called in: %" GST_PTR_FORMAT
511       " out: %" GST_PTR_FORMAT, in_caps, out_caps);
512
513   structure = gst_caps_get_structure (in_caps, 0);
514   if (!gst_structure_get_fraction (structure, "framerate",
515           &rate_numerator, &rate_denominator))
516     goto no_framerate;
517
518   videorate->from_rate_numerator = rate_numerator;
519   videorate->from_rate_denominator = rate_denominator;
520
521   structure = gst_caps_get_structure (out_caps, 0);
522   if (!gst_structure_get_fraction (structure, "framerate",
523           &rate_numerator, &rate_denominator))
524     goto no_framerate;
525
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
528    * out_frame_count */
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);
534   }
535   videorate->out_frame_count = 0;
536   videorate->to_rate_numerator = rate_numerator;
537   videorate->to_rate_denominator = rate_denominator;
538
539   if (rate_numerator)
540     videorate->wanted_diff = gst_util_uint64_scale_int (GST_SECOND,
541         rate_denominator, rate_numerator);
542   else
543     videorate->wanted_diff = 0;
544
545 done:
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;
552
553   return ret;
554
555 no_framerate:
556   {
557     GST_DEBUG_OBJECT (videorate, "no framerate specified");
558     ret = FALSE;
559     goto done;
560   }
561 }
562
563 static void
564 gst_video_rate_reset (GstVideoRate * videorate)
565 {
566   GST_DEBUG_OBJECT (videorate, "resetting internal variables");
567
568   videorate->in = 0;
569   videorate->out = 0;
570   videorate->base_ts = 0;
571   videorate->out_frame_count = 0;
572   videorate->drop = 0;
573   videorate->dup = 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);
579
580   gst_segment_init (&videorate->segment, GST_FORMAT_TIME);
581 }
582
583 static void
584 gst_video_rate_init (GstVideoRate * videorate, GstVideoRateClass * klass)
585 {
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;
595
596   videorate->from_rate_numerator = 0;
597   videorate->from_rate_denominator = 0;
598   videorate->to_rate_numerator = 0;
599   videorate->to_rate_denominator = 0;
600
601   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (videorate), TRUE);
602 }
603
604 /* flush the oldest buffer */
605 static GstFlowReturn
606 gst_video_rate_flush_prev (GstVideoRate * videorate, gboolean duplicate)
607 {
608   GstFlowReturn res;
609   GstBuffer *outbuf;
610   GstClockTime push_ts;
611
612   if (!videorate->prevbuf)
613     goto eos_before_buffers;
614
615   /* make sure we can write to the metadata */
616   outbuf = gst_buffer_make_metadata_writable
617       (gst_buffer_ref (videorate->prevbuf));
618
619   GST_BUFFER_OFFSET (outbuf) = videorate->out;
620   GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
621
622   if (videorate->discont) {
623     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
624     videorate->discont = FALSE;
625   } else
626     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
627
628   if (duplicate)
629     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
630   else
631     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
632
633   /* this is the timestamp we put on the buffer */
634   push_ts = videorate->next_ts;
635
636   videorate->out++;
637   videorate->out_frame_count++;
638   if (videorate->to_rate_numerator) {
639     /* interpolate next expected timestamp in the segment */
640     videorate->next_ts =
641         videorate->segment.accum + videorate->segment.start +
642         videorate->base_ts + gst_util_uint64_scale (videorate->out_frame_count,
643         videorate->to_rate_denominator * GST_SECOND,
644         videorate->to_rate_numerator);
645     GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
646   }
647
648   /* We do not need to update time in VFR (variable frame rate) mode */
649   if (!videorate->drop_only) {
650     /* adapt for looping, bring back to time in current segment. */
651     GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.accum;
652   }
653
654   GST_LOG_OBJECT (videorate,
655       "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
656       GST_TIME_ARGS (push_ts));
657
658   res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (videorate), outbuf);
659
660   return res;
661
662   /* WARNINGS */
663 eos_before_buffers:
664   {
665     GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
666     return GST_FLOW_OK;
667   }
668 }
669
670 static void
671 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
672     gint64 time)
673 {
674   GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
675   if (videorate->prevbuf)
676     gst_buffer_unref (videorate->prevbuf);
677   videorate->prevbuf = buffer != NULL ? gst_buffer_ref (buffer) : NULL;
678   videorate->prev_ts = time;
679 }
680
681 static void
682 gst_video_rate_notify_drop (GstVideoRate * videorate)
683 {
684 #if !GLIB_CHECK_VERSION(2,26,0)
685   g_object_notify ((GObject *) videorate, "drop");
686 #else
687   g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
688 #endif
689 }
690
691 static void
692 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
693 {
694 #if !GLIB_CHECK_VERSION(2,26,0)
695   g_object_notify ((GObject *) videorate, "duplicate");
696 #else
697   g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
698 #endif
699 }
700
701 #define MAGIC_LIMIT  25
702 static gboolean
703 gst_video_rate_event (GstBaseTransform * trans, GstEvent * event)
704 {
705   GstVideoRate *videorate;
706
707   videorate = GST_VIDEO_RATE (trans);
708
709   switch (GST_EVENT_TYPE (event)) {
710     case GST_EVENT_NEWSEGMENT:
711     {
712       gint64 start, stop, time;
713       gdouble rate, arate;
714       gboolean update;
715       GstFormat format;
716
717       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
718           &start, &stop, &time);
719
720       if (format != GST_FORMAT_TIME)
721         goto format_error;
722
723       GST_DEBUG_OBJECT (videorate, "handle NEWSEGMENT");
724
725       /* close up the previous segment, if appropriate */
726       if (!update && videorate->prevbuf) {
727         gint count = 0;
728         GstFlowReturn res;
729
730         res = GST_FLOW_OK;
731         /* fill up to the end of current segment,
732          * or only send out the stored buffer if there is no specific stop.
733          * regardless, prevent going loopy in strange cases */
734         while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
735             ((GST_CLOCK_TIME_IS_VALID (videorate->segment.stop) &&
736                     videorate->next_ts - videorate->segment.accum
737                     < videorate->segment.stop)
738                 || count < 1)) {
739           res = gst_video_rate_flush_prev (videorate, count > 0);
740           count++;
741         }
742         if (count > 1) {
743           videorate->dup += count - 1;
744           if (!videorate->silent)
745             gst_video_rate_notify_duplicate (videorate);
746         } else if (count == 0) {
747           videorate->drop++;
748           if (!videorate->silent)
749             gst_video_rate_notify_drop (videorate);
750         }
751         /* clean up for the new one; _chain will resume from the new start */
752         videorate->base_ts = 0;
753         videorate->out_frame_count = 0;
754         gst_video_rate_swap_prev (videorate, NULL, 0);
755         videorate->next_ts = GST_CLOCK_TIME_NONE;
756       }
757
758       /* We just want to update the accumulated stream_time  */
759       gst_segment_set_newsegment_full (&videorate->segment, update, rate, arate,
760           format, start, stop, time);
761
762       GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
763           &videorate->segment);
764       break;
765     }
766     case GST_EVENT_EOS:{
767       gint count = 0;
768       GstFlowReturn res = GST_FLOW_OK;
769
770       GST_DEBUG_OBJECT (videorate, "Got EOS");
771
772       /* If the segment has a stop position, fill the segment */
773       if (GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
774         /* fill up to the end of current segment,
775          * or only send out the stored buffer if there is no specific stop.
776          * regardless, prevent going loopy in strange cases */
777         while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
778             ((videorate->next_ts - videorate->segment.accum <
779                     videorate->segment.stop)
780                 || count < 1)) {
781           res = gst_video_rate_flush_prev (videorate, count > 0);
782           count++;
783         }
784       } else if (videorate->prevbuf) {
785         /* Output at least one frame but if the buffer duration is valid, output
786          * enough frames to use the complete buffer duration */
787         if (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf)) {
788           GstClockTime end_ts =
789               videorate->next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
790
791           while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
792               ((videorate->next_ts - videorate->segment.accum < end_ts)
793                   || count < 1)) {
794             res = gst_video_rate_flush_prev (videorate, count > 0);
795             count++;
796           }
797         } else {
798           res = gst_video_rate_flush_prev (videorate, FALSE);
799           count = 1;
800         }
801       }
802
803       if (count > 1) {
804         videorate->dup += count - 1;
805         if (!videorate->silent)
806           gst_video_rate_notify_duplicate (videorate);
807       } else if (count == 0) {
808         videorate->drop++;
809         if (!videorate->silent)
810           gst_video_rate_notify_drop (videorate);
811       }
812
813       break;
814     }
815     case GST_EVENT_FLUSH_STOP:
816       /* also resets the segment */
817       GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
818       gst_video_rate_reset (videorate);
819       break;
820     default:
821       break;
822   }
823
824   return TRUE;
825
826   /* ERRORS */
827 format_error:
828   {
829     GST_WARNING_OBJECT (videorate,
830         "Got segment but doesn't have GST_FORMAT_TIME value");
831     return FALSE;
832   }
833 }
834
835 static gboolean
836 gst_video_rate_query (GstBaseTransform * trans, GstPadDirection direction,
837     GstQuery * query)
838 {
839   GstVideoRate *videorate = GST_VIDEO_RATE (trans);
840   gboolean res = FALSE;
841   GstPad *otherpad;
842
843   otherpad = (direction == GST_PAD_SRC) ?
844       GST_BASE_TRANSFORM_SINK_PAD (trans) : GST_BASE_TRANSFORM_SRC_PAD (trans);
845
846   switch (GST_QUERY_TYPE (query)) {
847     case GST_QUERY_LATENCY:
848     {
849       GstClockTime min, max;
850       gboolean live;
851       guint64 latency;
852       guint64 avg_period;
853       GstPad *peer;
854
855       GST_OBJECT_LOCK (videorate);
856       avg_period = videorate->average_period_set;
857       GST_OBJECT_UNLOCK (videorate);
858
859       if (avg_period == 0 && (peer = gst_pad_get_peer (otherpad))) {
860         if ((res = gst_pad_query (peer, query))) {
861           gst_query_parse_latency (query, &live, &min, &max);
862
863           GST_DEBUG_OBJECT (videorate, "Peer latency: min %"
864               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
865               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
866
867           if (videorate->from_rate_numerator != 0) {
868             /* add latency. We don't really know since we hold on to the frames
869              * until we get a next frame, which can be anything. We assume
870              * however that this will take from_rate time. */
871             latency = gst_util_uint64_scale (GST_SECOND,
872                 videorate->from_rate_denominator,
873                 videorate->from_rate_numerator);
874           } else {
875             /* no input framerate, we don't know */
876             latency = 0;
877           }
878
879           GST_DEBUG_OBJECT (videorate, "Our latency: %"
880               GST_TIME_FORMAT, GST_TIME_ARGS (latency));
881
882           min += latency;
883           if (max != -1)
884             max += latency;
885
886           GST_DEBUG_OBJECT (videorate, "Calculated total latency : min %"
887               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
888               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
889
890           gst_query_set_latency (query, live, min, max);
891         }
892         gst_object_unref (peer);
893         break;
894       }
895       /* Simple fallthrough if we don't have a latency or not a peer that we
896        * can't ask about its latency yet.. */
897     }
898     default:
899       res = parent_class->query (trans, direction, query);
900       break;
901   }
902
903   return res;
904 }
905
906 static GstFlowReturn
907 gst_video_rate_trans_ip_max_avg (GstVideoRate * videorate, GstBuffer * buf)
908 {
909   GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
910
911   videorate->in++;
912
913   if (!GST_CLOCK_TIME_IS_VALID (ts) || videorate->wanted_diff == 0)
914     goto push;
915
916   /* drop frames if they exceed our output rate */
917   if (GST_CLOCK_TIME_IS_VALID (videorate->last_ts)) {
918     GstClockTimeDiff diff = ts - videorate->last_ts;
919
920     /* Drop buffer if its early compared to the desired frame rate and
921      * the current average is higher than the desired average
922      */
923     if (diff < videorate->wanted_diff &&
924         videorate->average < videorate->wanted_diff)
925       goto drop;
926
927     /* Update average */
928     if (videorate->average) {
929       GstClockTimeDiff wanted_diff;
930
931       if (G_LIKELY (videorate->average_period > videorate->wanted_diff))
932         wanted_diff = videorate->wanted_diff;
933       else
934         wanted_diff = videorate->average_period * 10;
935
936       videorate->average =
937           gst_util_uint64_scale_round (videorate->average,
938           videorate->average_period - wanted_diff,
939           videorate->average_period) +
940           gst_util_uint64_scale_round (diff, wanted_diff,
941           videorate->average_period);
942     } else {
943       videorate->average = diff;
944     }
945   }
946
947   videorate->last_ts = ts;
948
949 push:
950   videorate->out++;
951   return GST_FLOW_OK;
952
953 drop:
954   if (!videorate->silent)
955     gst_video_rate_notify_drop (videorate);
956   return GST_BASE_TRANSFORM_FLOW_DROPPED;
957 }
958
959 static GstFlowReturn
960 gst_video_rate_prepare_output_buffer (GstBaseTransform * trans,
961     GstBuffer * input, gint size, GstCaps * caps, GstBuffer ** buf)
962 {
963   if (gst_buffer_is_metadata_writable (input)) {
964     gst_buffer_set_caps (input, caps);
965     *buf = gst_buffer_ref (input);
966   } else {
967     *buf = gst_buffer_create_sub (input, 0, GST_BUFFER_SIZE (input));
968     gst_buffer_set_caps (*buf, caps);
969   }
970
971   return GST_FLOW_OK;
972 }
973
974 static GstFlowReturn
975 gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
976 {
977   GstVideoRate *videorate;
978   GstFlowReturn res = GST_BASE_TRANSFORM_FLOW_DROPPED;
979   GstClockTime intime, in_ts, in_dur;
980   GstClockTime avg_period;
981   gboolean skip = FALSE;
982
983   videorate = GST_VIDEO_RATE (trans);
984
985   /* make sure the denominators are not 0 */
986   if (videorate->from_rate_denominator == 0 ||
987       videorate->to_rate_denominator == 0)
988     goto not_negotiated;
989
990   GST_OBJECT_LOCK (videorate);
991   avg_period = videorate->average_period_set;
992   GST_OBJECT_UNLOCK (videorate);
993
994   /* MT-safe switching between modes */
995   if (G_UNLIKELY (avg_period != videorate->average_period)) {
996     gboolean switch_mode = (avg_period == 0 || videorate->average_period == 0);
997     videorate->average_period = avg_period;
998     videorate->last_ts = GST_CLOCK_TIME_NONE;
999
1000     if (switch_mode) {
1001       if (avg_period) {
1002         /* enabling average mode */
1003         videorate->average = 0;
1004         /* make sure no cached buffers from regular mode are left */
1005         gst_video_rate_swap_prev (videorate, NULL, 0);
1006       } else {
1007         /* enable regular mode */
1008         videorate->next_ts = GST_CLOCK_TIME_NONE;
1009         skip = TRUE;
1010       }
1011
1012       /* max averaging mode has a no latency, normal mode does */
1013       gst_element_post_message (GST_ELEMENT (videorate),
1014           gst_message_new_latency (GST_OBJECT (videorate)));
1015     }
1016   }
1017
1018   if (videorate->average_period > 0)
1019     return gst_video_rate_trans_ip_max_avg (videorate, buffer);
1020
1021   in_ts = GST_BUFFER_TIMESTAMP (buffer);
1022   in_dur = GST_BUFFER_DURATION (buffer);
1023
1024   if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE)) {
1025     in_ts = videorate->last_ts;
1026     if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE))
1027       goto invalid_buffer;
1028   }
1029
1030   /* get the time of the next expected buffer timestamp, we use this when the
1031    * next buffer has -1 as a timestamp */
1032   videorate->last_ts = in_ts;
1033   if (in_dur != GST_CLOCK_TIME_NONE)
1034     videorate->last_ts += in_dur;
1035
1036   GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
1037       GST_TIME_ARGS (in_ts));
1038
1039   /* the input time is the time in the segment + all previously accumulated
1040    * segments */
1041   intime = in_ts + videorate->segment.accum;
1042
1043   /* we need to have two buffers to compare */
1044   if (videorate->prevbuf == NULL) {
1045     gst_video_rate_swap_prev (videorate, buffer, intime);
1046     videorate->in++;
1047     if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts)) {
1048       /* new buffer, we expect to output a buffer that matches the first
1049        * timestamp in the segment */
1050       if (videorate->skip_to_first || skip) {
1051         videorate->next_ts = intime;
1052         videorate->base_ts = in_ts - videorate->segment.start;
1053         videorate->out_frame_count = 0;
1054       } else {
1055         videorate->next_ts =
1056             videorate->segment.start + videorate->segment.accum;
1057       }
1058     }
1059   } else {
1060     GstClockTime prevtime;
1061     gint count = 0;
1062     gint64 diff1, diff2;
1063
1064     prevtime = videorate->prev_ts;
1065
1066     GST_LOG_OBJECT (videorate,
1067         "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
1068         " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
1069         GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
1070
1071     videorate->in++;
1072
1073     /* drop new buffer if it's before previous one */
1074     if (intime < prevtime) {
1075       GST_DEBUG_OBJECT (videorate,
1076           "The new buffer (%" GST_TIME_FORMAT
1077           ") is before the previous buffer (%"
1078           GST_TIME_FORMAT "). Dropping new buffer.",
1079           GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
1080       videorate->drop++;
1081       if (!videorate->silent)
1082         gst_video_rate_notify_drop (videorate);
1083       goto done;
1084     }
1085
1086     /* got 2 buffers, see which one is the best */
1087     do {
1088
1089       diff1 = prevtime - videorate->next_ts;
1090       diff2 = intime - videorate->next_ts;
1091
1092       /* take absolute values, beware: abs and ABS don't work for gint64 */
1093       if (diff1 < 0)
1094         diff1 = -diff1;
1095       if (diff2 < 0)
1096         diff2 = -diff2;
1097
1098       GST_LOG_OBJECT (videorate,
1099           "diff with prev %" GST_TIME_FORMAT " diff with new %"
1100           GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
1101           GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
1102           GST_TIME_ARGS (videorate->next_ts));
1103
1104       /* output first one when its the best */
1105       if (diff1 <= diff2) {
1106         GstFlowReturn r;
1107         count++;
1108
1109         /* on error the _flush function posted a warning already */
1110         if ((r = gst_video_rate_flush_prev (videorate,
1111                     count > 1)) != GST_FLOW_OK) {
1112           res = r;
1113           goto done;
1114         }
1115       }
1116
1117       /* Do not produce any dups. We can exit loop now */
1118       if (videorate->drop_only)
1119         break;
1120       /* continue while the first one was the best, if they were equal avoid
1121        * going into an infinite loop */
1122     }
1123     while (diff1 < diff2);
1124
1125     /* if we outputed the first buffer more then once, we have dups */
1126     if (count > 1) {
1127       videorate->dup += count - 1;
1128       if (!videorate->silent)
1129         gst_video_rate_notify_duplicate (videorate);
1130     }
1131     /* if we didn't output the first buffer, we have a drop */
1132     else if (count == 0) {
1133       videorate->drop++;
1134
1135       if (!videorate->silent)
1136         gst_video_rate_notify_drop (videorate);
1137
1138       GST_LOG_OBJECT (videorate,
1139           "new is best, old never used, drop, outgoing ts %"
1140           GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
1141     }
1142     GST_LOG_OBJECT (videorate,
1143         "END, putting new in old, diff1 %" GST_TIME_FORMAT
1144         ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
1145         ", in %" G_GUINT64_FORMAT ", out %" G_GUINT64_FORMAT ", drop %"
1146         G_GUINT64_FORMAT ", dup %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff1),
1147         GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
1148         videorate->in, videorate->out, videorate->drop, videorate->dup);
1149
1150     /* swap in new one when it's the best */
1151     gst_video_rate_swap_prev (videorate, buffer, intime);
1152   }
1153 done:
1154   return res;
1155
1156   /* ERRORS */
1157 not_negotiated:
1158   {
1159     GST_WARNING_OBJECT (videorate, "no framerate negotiated");
1160     res = GST_FLOW_NOT_NEGOTIATED;
1161     goto done;
1162   }
1163
1164 invalid_buffer:
1165   {
1166     GST_WARNING_OBJECT (videorate,
1167         "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
1168     res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1169     goto done;
1170   }
1171 }
1172
1173 static gboolean
1174 gst_video_rate_start (GstBaseTransform * trans)
1175 {
1176   gst_video_rate_reset (GST_VIDEO_RATE (trans));
1177   return TRUE;
1178 }
1179
1180 static gboolean
1181 gst_video_rate_stop (GstBaseTransform * trans)
1182 {
1183   gst_video_rate_reset (GST_VIDEO_RATE (trans));
1184   return TRUE;
1185 }
1186
1187 static void
1188 gst_video_rate_set_property (GObject * object,
1189     guint prop_id, const GValue * value, GParamSpec * pspec)
1190 {
1191   GstVideoRate *videorate = GST_VIDEO_RATE (object);
1192
1193   GST_OBJECT_LOCK (videorate);
1194   switch (prop_id) {
1195     case PROP_SILENT:
1196       videorate->silent = g_value_get_boolean (value);
1197       break;
1198     case PROP_NEW_PREF:
1199       videorate->new_pref = g_value_get_double (value);
1200       break;
1201     case PROP_SKIP_TO_FIRST:
1202       videorate->skip_to_first = g_value_get_boolean (value);
1203       break;
1204     case PROP_DROP_ONLY:
1205       videorate->drop_only = g_value_get_boolean (value);
1206       goto reconfigure;
1207       break;
1208     case PROP_AVERAGE_PERIOD:
1209       videorate->average_period_set = g_value_get_uint64 (value);
1210       break;
1211     case PROP_MAX_RATE:
1212       g_atomic_int_set (&videorate->max_rate, g_value_get_int (value));
1213       goto reconfigure;
1214       break;
1215     case PROP_FORCE_FPS:
1216       videorate->force_fps_n = gst_value_get_fraction_numerator (value);
1217       videorate->force_fps_d = gst_value_get_fraction_denominator (value);
1218       goto reconfigure;
1219       break;
1220     default:
1221       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1222       break;
1223   }
1224   GST_OBJECT_UNLOCK (videorate);
1225   return;
1226
1227 reconfigure:
1228   GST_OBJECT_UNLOCK (videorate);
1229   gst_base_transform_reconfigure (GST_BASE_TRANSFORM (videorate));
1230 }
1231
1232 static void
1233 gst_video_rate_get_property (GObject * object,
1234     guint prop_id, GValue * value, GParamSpec * pspec)
1235 {
1236   GstVideoRate *videorate = GST_VIDEO_RATE (object);
1237
1238   GST_OBJECT_LOCK (videorate);
1239   switch (prop_id) {
1240     case PROP_IN:
1241       g_value_set_uint64 (value, videorate->in);
1242       break;
1243     case PROP_OUT:
1244       g_value_set_uint64 (value, videorate->out);
1245       break;
1246     case PROP_DUP:
1247       g_value_set_uint64 (value, videorate->dup);
1248       break;
1249     case PROP_DROP:
1250       g_value_set_uint64 (value, videorate->drop);
1251       break;
1252     case PROP_SILENT:
1253       g_value_set_boolean (value, videorate->silent);
1254       break;
1255     case PROP_NEW_PREF:
1256       g_value_set_double (value, videorate->new_pref);
1257       break;
1258     case PROP_SKIP_TO_FIRST:
1259       g_value_set_boolean (value, videorate->skip_to_first);
1260       break;
1261     case PROP_DROP_ONLY:
1262       g_value_set_boolean (value, videorate->drop_only);
1263       break;
1264     case PROP_AVERAGE_PERIOD:
1265       g_value_set_uint64 (value, videorate->average_period_set);
1266       break;
1267     case PROP_MAX_RATE:
1268       g_value_set_int (value, g_atomic_int_get (&videorate->max_rate));
1269       break;
1270     case PROP_FORCE_FPS:
1271       gst_value_set_fraction (value, videorate->force_fps_n,
1272           videorate->force_fps_d);
1273       break;
1274     default:
1275       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1276       break;
1277   }
1278   GST_OBJECT_UNLOCK (videorate);
1279 }
1280
1281 static gboolean
1282 plugin_init (GstPlugin * plugin)
1283 {
1284   GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
1285       "VideoRate stream fixer");
1286
1287   return gst_element_register (plugin, "videorate", GST_RANK_NONE,
1288       GST_TYPE_VIDEO_RATE);
1289 }
1290
1291 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1292     GST_VERSION_MINOR,
1293     "videorate",
1294     "Adjusts video frames",
1295     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)