tizen 2.3.1 release
[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 #ifdef VIDEORATE_MODIFICATION
612   GstStructure *structure;
613   guint32 format;
614   GstCaps *buf_caps;
615 #endif
616   if (!videorate->prevbuf)
617     goto eos_before_buffers;
618
619   /* make sure we can write to the metadata */
620 #ifdef VIDEORATE_MODIFICATION
621   buf_caps = gst_buffer_get_caps (videorate->prevbuf);
622   if(!buf_caps) {
623     GST_ERROR("not able to get the caps");
624     return GST_FLOW_ERROR;
625   }
626   structure = gst_caps_get_structure (buf_caps, 0);
627   if(!structure) {
628     GST_ERROR("not able to get the structure");
629     return GST_FLOW_ERROR;
630   }
631   if (!gst_structure_get_fourcc (structure, "format", &format)) {
632     GST_ERROR ("can not get format in gst structure");
633   }
634   if(format== GST_MAKE_FOURCC('S', 'T', '1', '2') || format== GST_MAKE_FOURCC('S', 'N', '1', '2')) {
635     GST_ERROR("setting the format_transcode as TRUE");
636     outbuf=videorate->prevbuf;
637     gst_caps_unref(buf_caps);
638   } else {
639     outbuf = gst_buffer_make_metadata_writable
640     (gst_buffer_ref (videorate->prevbuf));
641   }
642 #endif
643   GST_BUFFER_OFFSET (outbuf) = videorate->out;
644   GST_BUFFER_OFFSET_END (outbuf) = videorate->out + 1;
645
646   if (videorate->discont) {
647     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
648     videorate->discont = FALSE;
649   } else
650     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
651
652   if (duplicate)
653     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
654   else
655     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_GAP);
656
657   /* this is the timestamp we put on the buffer */
658   push_ts = videorate->next_ts;
659
660   videorate->out++;
661   videorate->out_frame_count++;
662   if (videorate->to_rate_numerator) {
663     /* interpolate next expected timestamp in the segment */
664     videorate->next_ts =
665         videorate->segment.accum + videorate->segment.start +
666         videorate->base_ts + gst_util_uint64_scale (videorate->out_frame_count,
667         videorate->to_rate_denominator * GST_SECOND,
668         videorate->to_rate_numerator);
669     GST_BUFFER_DURATION (outbuf) = videorate->next_ts - push_ts;
670   }
671
672   /* We do not need to update time in VFR (variable frame rate) mode */
673   if (!videorate->drop_only) {
674     /* adapt for looping, bring back to time in current segment. */
675     GST_BUFFER_TIMESTAMP (outbuf) = push_ts - videorate->segment.accum;
676   }
677
678   GST_LOG_OBJECT (videorate,
679       "old is best, dup, pushing buffer outgoing ts %" GST_TIME_FORMAT,
680       GST_TIME_ARGS (push_ts));
681
682   res = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (videorate), outbuf);
683
684   return res;
685
686   /* WARNINGS */
687 eos_before_buffers:
688   {
689     GST_INFO_OBJECT (videorate, "got EOS before any buffer was received");
690     return GST_FLOW_OK;
691   }
692 }
693
694 static void
695 gst_video_rate_swap_prev (GstVideoRate * videorate, GstBuffer * buffer,
696     gint64 time)
697 {
698   GST_LOG_OBJECT (videorate, "swap_prev: storing buffer %p in prev", buffer);
699   if (videorate->prevbuf)
700     gst_buffer_unref (videorate->prevbuf);
701   videorate->prevbuf = buffer != NULL ? gst_buffer_ref (buffer) : NULL;
702   videorate->prev_ts = time;
703 }
704
705 static void
706 gst_video_rate_notify_drop (GstVideoRate * videorate)
707 {
708 #if !GLIB_CHECK_VERSION(2,26,0)
709   g_object_notify ((GObject *) videorate, "drop");
710 #else
711   g_object_notify_by_pspec ((GObject *) videorate, pspec_drop);
712 #endif
713 }
714
715 static void
716 gst_video_rate_notify_duplicate (GstVideoRate * videorate)
717 {
718 #if !GLIB_CHECK_VERSION(2,26,0)
719   g_object_notify ((GObject *) videorate, "duplicate");
720 #else
721   g_object_notify_by_pspec ((GObject *) videorate, pspec_duplicate);
722 #endif
723 }
724
725 #define MAGIC_LIMIT  25
726 static gboolean
727 gst_video_rate_event (GstBaseTransform * trans, GstEvent * event)
728 {
729   GstVideoRate *videorate;
730
731   videorate = GST_VIDEO_RATE (trans);
732
733   switch (GST_EVENT_TYPE (event)) {
734     case GST_EVENT_NEWSEGMENT:
735     {
736       gint64 start, stop, time;
737       gdouble rate, arate;
738       gboolean update;
739       GstFormat format;
740
741       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
742           &start, &stop, &time);
743
744       if (format != GST_FORMAT_TIME)
745         goto format_error;
746
747       GST_DEBUG_OBJECT (videorate, "handle NEWSEGMENT");
748
749       /* close up the previous segment, if appropriate */
750       if (!update && videorate->prevbuf) {
751         gint count = 0;
752         GstFlowReturn res;
753
754         res = GST_FLOW_OK;
755         /* fill up to the end of current segment,
756          * or only send out the stored buffer if there is no specific stop.
757          * regardless, prevent going loopy in strange cases */
758         while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
759             ((GST_CLOCK_TIME_IS_VALID (videorate->segment.stop) &&
760                     videorate->next_ts - videorate->segment.accum
761                     < videorate->segment.stop)
762                 || count < 1)) {
763           res = gst_video_rate_flush_prev (videorate, count > 0);
764           count++;
765         }
766         if (count > 1) {
767           videorate->dup += count - 1;
768           if (!videorate->silent)
769             gst_video_rate_notify_duplicate (videorate);
770         } else if (count == 0) {
771           videorate->drop++;
772           if (!videorate->silent)
773             gst_video_rate_notify_drop (videorate);
774         }
775         /* clean up for the new one; _chain will resume from the new start */
776         videorate->base_ts = 0;
777         videorate->out_frame_count = 0;
778         gst_video_rate_swap_prev (videorate, NULL, 0);
779         videorate->next_ts = GST_CLOCK_TIME_NONE;
780       }
781
782       /* We just want to update the accumulated stream_time  */
783       gst_segment_set_newsegment_full (&videorate->segment, update, rate, arate,
784           format, start, stop, time);
785
786       GST_DEBUG_OBJECT (videorate, "updated segment: %" GST_SEGMENT_FORMAT,
787           &videorate->segment);
788       break;
789     }
790     case GST_EVENT_EOS:{
791       gint count = 0;
792       GstFlowReturn res = GST_FLOW_OK;
793
794       GST_DEBUG_OBJECT (videorate, "Got EOS");
795
796       /* If the segment has a stop position, fill the segment */
797       if (GST_CLOCK_TIME_IS_VALID (videorate->segment.stop)) {
798         /* fill up to the end of current segment,
799          * or only send out the stored buffer if there is no specific stop.
800          * regardless, prevent going loopy in strange cases */
801         while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
802             ((videorate->next_ts - videorate->segment.accum <
803                     videorate->segment.stop)
804                 || count < 1)) {
805           res = gst_video_rate_flush_prev (videorate, count > 0);
806           count++;
807         }
808       } else if (videorate->prevbuf) {
809         /* Output at least one frame but if the buffer duration is valid, output
810          * enough frames to use the complete buffer duration */
811         if (GST_BUFFER_DURATION_IS_VALID (videorate->prevbuf)) {
812           GstClockTime end_ts =
813               videorate->next_ts + GST_BUFFER_DURATION (videorate->prevbuf);
814
815           while (res == GST_FLOW_OK && count <= MAGIC_LIMIT &&
816               ((videorate->next_ts - videorate->segment.accum < end_ts)
817                   || count < 1)) {
818             res = gst_video_rate_flush_prev (videorate, count > 0);
819             count++;
820           }
821         } else {
822           res = gst_video_rate_flush_prev (videorate, FALSE);
823           count = 1;
824         }
825       }
826
827       if (count > 1) {
828         videorate->dup += count - 1;
829         if (!videorate->silent)
830           gst_video_rate_notify_duplicate (videorate);
831       } else if (count == 0) {
832         videorate->drop++;
833         if (!videorate->silent)
834           gst_video_rate_notify_drop (videorate);
835       }
836
837       break;
838     }
839     case GST_EVENT_FLUSH_STOP:
840       /* also resets the segment */
841       GST_DEBUG_OBJECT (videorate, "Got FLUSH_STOP");
842       gst_video_rate_reset (videorate);
843       break;
844     default:
845       break;
846   }
847
848   return TRUE;
849
850   /* ERRORS */
851 format_error:
852   {
853     GST_WARNING_OBJECT (videorate,
854         "Got segment but doesn't have GST_FORMAT_TIME value");
855     return FALSE;
856   }
857 }
858
859 static gboolean
860 gst_video_rate_query (GstBaseTransform * trans, GstPadDirection direction,
861     GstQuery * query)
862 {
863   GstVideoRate *videorate = GST_VIDEO_RATE (trans);
864   gboolean res = FALSE;
865   GstPad *otherpad;
866
867   otherpad = (direction == GST_PAD_SRC) ?
868       GST_BASE_TRANSFORM_SINK_PAD (trans) : GST_BASE_TRANSFORM_SRC_PAD (trans);
869
870   switch (GST_QUERY_TYPE (query)) {
871     case GST_QUERY_LATENCY:
872     {
873       GstClockTime min, max;
874       gboolean live;
875       guint64 latency;
876       guint64 avg_period;
877       GstPad *peer;
878
879       GST_OBJECT_LOCK (videorate);
880       avg_period = videorate->average_period_set;
881       GST_OBJECT_UNLOCK (videorate);
882
883       if (avg_period == 0 && (peer = gst_pad_get_peer (otherpad))) {
884         if ((res = gst_pad_query (peer, query))) {
885           gst_query_parse_latency (query, &live, &min, &max);
886
887           GST_DEBUG_OBJECT (videorate, "Peer latency: min %"
888               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
889               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
890
891           if (videorate->from_rate_numerator != 0) {
892             /* add latency. We don't really know since we hold on to the frames
893              * until we get a next frame, which can be anything. We assume
894              * however that this will take from_rate time. */
895             latency = gst_util_uint64_scale (GST_SECOND,
896                 videorate->from_rate_denominator,
897                 videorate->from_rate_numerator);
898           } else {
899             /* no input framerate, we don't know */
900             latency = 0;
901           }
902
903           GST_DEBUG_OBJECT (videorate, "Our latency: %"
904               GST_TIME_FORMAT, GST_TIME_ARGS (latency));
905
906           min += latency;
907           if (max != -1)
908             max += latency;
909
910           GST_DEBUG_OBJECT (videorate, "Calculated total latency : min %"
911               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
912               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
913
914           gst_query_set_latency (query, live, min, max);
915         }
916         gst_object_unref (peer);
917         break;
918       }
919       /* Simple fallthrough if we don't have a latency or not a peer that we
920        * can't ask about its latency yet.. */
921     }
922     default:
923       res = parent_class->query (trans, direction, query);
924       break;
925   }
926
927   return res;
928 }
929
930 static GstFlowReturn
931 gst_video_rate_trans_ip_max_avg (GstVideoRate * videorate, GstBuffer * buf)
932 {
933   GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
934
935   videorate->in++;
936
937   if (!GST_CLOCK_TIME_IS_VALID (ts) || videorate->wanted_diff == 0)
938     goto push;
939
940   /* drop frames if they exceed our output rate */
941   if (GST_CLOCK_TIME_IS_VALID (videorate->last_ts)) {
942     GstClockTimeDiff diff = ts - videorate->last_ts;
943
944     /* Drop buffer if its early compared to the desired frame rate and
945      * the current average is higher than the desired average
946      */
947     if (diff < videorate->wanted_diff &&
948         videorate->average < videorate->wanted_diff)
949       goto drop;
950
951     /* Update average */
952     if (videorate->average) {
953       GstClockTimeDiff wanted_diff;
954
955       if (G_LIKELY (videorate->average_period > videorate->wanted_diff))
956         wanted_diff = videorate->wanted_diff;
957       else
958         wanted_diff = videorate->average_period * 10;
959
960       videorate->average =
961           gst_util_uint64_scale_round (videorate->average,
962           videorate->average_period - wanted_diff,
963           videorate->average_period) +
964           gst_util_uint64_scale_round (diff, wanted_diff,
965           videorate->average_period);
966     } else {
967       videorate->average = diff;
968     }
969   }
970
971   videorate->last_ts = ts;
972
973 push:
974   videorate->out++;
975   return GST_FLOW_OK;
976
977 drop:
978   if (!videorate->silent)
979     gst_video_rate_notify_drop (videorate);
980   return GST_BASE_TRANSFORM_FLOW_DROPPED;
981 }
982
983 static GstFlowReturn
984 gst_video_rate_prepare_output_buffer (GstBaseTransform * trans,
985     GstBuffer * input, gint size, GstCaps * caps, GstBuffer ** buf)
986 {
987   if (gst_buffer_is_metadata_writable (input)) {
988     gst_buffer_set_caps (input, caps);
989     *buf = gst_buffer_ref (input);
990   } else {
991     *buf = gst_buffer_create_sub (input, 0, GST_BUFFER_SIZE (input));
992     gst_buffer_set_caps (*buf, caps);
993   }
994
995   return GST_FLOW_OK;
996 }
997
998 static GstFlowReturn
999 gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
1000 {
1001   GstVideoRate *videorate;
1002   GstFlowReturn res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1003   GstClockTime intime, in_ts, in_dur;
1004   GstClockTime avg_period;
1005   gboolean skip = FALSE;
1006
1007   videorate = GST_VIDEO_RATE (trans);
1008
1009   /* make sure the denominators are not 0 */
1010   if (videorate->from_rate_denominator == 0 ||
1011       videorate->to_rate_denominator == 0)
1012     goto not_negotiated;
1013
1014   GST_OBJECT_LOCK (videorate);
1015   avg_period = videorate->average_period_set;
1016   GST_OBJECT_UNLOCK (videorate);
1017
1018   /* MT-safe switching between modes */
1019   if (G_UNLIKELY (avg_period != videorate->average_period)) {
1020     gboolean switch_mode = (avg_period == 0 || videorate->average_period == 0);
1021     videorate->average_period = avg_period;
1022     videorate->last_ts = GST_CLOCK_TIME_NONE;
1023
1024     if (switch_mode) {
1025       if (avg_period) {
1026         /* enabling average mode */
1027         videorate->average = 0;
1028         /* make sure no cached buffers from regular mode are left */
1029         gst_video_rate_swap_prev (videorate, NULL, 0);
1030       } else {
1031         /* enable regular mode */
1032         videorate->next_ts = GST_CLOCK_TIME_NONE;
1033         skip = TRUE;
1034       }
1035
1036       /* max averaging mode has a no latency, normal mode does */
1037       gst_element_post_message (GST_ELEMENT (videorate),
1038           gst_message_new_latency (GST_OBJECT (videorate)));
1039     }
1040   }
1041
1042   if (videorate->average_period > 0)
1043     return gst_video_rate_trans_ip_max_avg (videorate, buffer);
1044
1045   in_ts = GST_BUFFER_TIMESTAMP (buffer);
1046   in_dur = GST_BUFFER_DURATION (buffer);
1047
1048   if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE)) {
1049     in_ts = videorate->last_ts;
1050     if (G_UNLIKELY (in_ts == GST_CLOCK_TIME_NONE))
1051       goto invalid_buffer;
1052   }
1053
1054   /* get the time of the next expected buffer timestamp, we use this when the
1055    * next buffer has -1 as a timestamp */
1056   videorate->last_ts = in_ts;
1057   if (in_dur != GST_CLOCK_TIME_NONE)
1058     videorate->last_ts += in_dur;
1059
1060   GST_DEBUG_OBJECT (videorate, "got buffer with timestamp %" GST_TIME_FORMAT,
1061       GST_TIME_ARGS (in_ts));
1062
1063   /* the input time is the time in the segment + all previously accumulated
1064    * segments */
1065   intime = in_ts + videorate->segment.accum;
1066
1067   /* we need to have two buffers to compare */
1068   if (videorate->prevbuf == NULL) {
1069     gst_video_rate_swap_prev (videorate, buffer, intime);
1070     videorate->in++;
1071     if (!GST_CLOCK_TIME_IS_VALID (videorate->next_ts)) {
1072       /* new buffer, we expect to output a buffer that matches the first
1073        * timestamp in the segment */
1074       if (videorate->skip_to_first || skip) {
1075         videorate->next_ts = intime;
1076         videorate->base_ts = in_ts - videorate->segment.start;
1077         videorate->out_frame_count = 0;
1078       } else {
1079         videorate->next_ts =
1080             videorate->segment.start + videorate->segment.accum;
1081       }
1082     }
1083   } else {
1084     GstClockTime prevtime;
1085     gint count = 0;
1086     gint64 diff1, diff2;
1087
1088     prevtime = videorate->prev_ts;
1089
1090     GST_LOG_OBJECT (videorate,
1091         "BEGINNING prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
1092         " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
1093         GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
1094
1095     videorate->in++;
1096
1097     /* drop new buffer if it's before previous one */
1098     if (intime < prevtime) {
1099       GST_DEBUG_OBJECT (videorate,
1100           "The new buffer (%" GST_TIME_FORMAT
1101           ") is before the previous buffer (%"
1102           GST_TIME_FORMAT "). Dropping new buffer.",
1103           GST_TIME_ARGS (intime), GST_TIME_ARGS (prevtime));
1104       videorate->drop++;
1105       if (!videorate->silent)
1106         gst_video_rate_notify_drop (videorate);
1107       goto done;
1108     }
1109
1110     /* got 2 buffers, see which one is the best */
1111     do {
1112
1113       diff1 = prevtime - videorate->next_ts;
1114       diff2 = intime - videorate->next_ts;
1115
1116       /* take absolute values, beware: abs and ABS don't work for gint64 */
1117       if (diff1 < 0)
1118         diff1 = -diff1;
1119       if (diff2 < 0)
1120         diff2 = -diff2;
1121
1122       GST_LOG_OBJECT (videorate,
1123           "diff with prev %" GST_TIME_FORMAT " diff with new %"
1124           GST_TIME_FORMAT " outgoing ts %" GST_TIME_FORMAT,
1125           GST_TIME_ARGS (diff1), GST_TIME_ARGS (diff2),
1126           GST_TIME_ARGS (videorate->next_ts));
1127
1128       /* output first one when its the best */
1129       if (diff1 <= diff2) {
1130         GstFlowReturn r;
1131         count++;
1132
1133         /* on error the _flush function posted a warning already */
1134         if ((r = gst_video_rate_flush_prev (videorate,
1135                     count > 1)) != GST_FLOW_OK) {
1136           res = r;
1137           goto done;
1138         }
1139       }
1140
1141       /* Do not produce any dups. We can exit loop now */
1142       if (videorate->drop_only)
1143         break;
1144       /* continue while the first one was the best, if they were equal avoid
1145        * going into an infinite loop */
1146     }
1147     while (diff1 < diff2);
1148
1149     /* if we outputed the first buffer more then once, we have dups */
1150     if (count > 1) {
1151       videorate->dup += count - 1;
1152       if (!videorate->silent)
1153         gst_video_rate_notify_duplicate (videorate);
1154     }
1155     /* if we didn't output the first buffer, we have a drop */
1156     else if (count == 0) {
1157       videorate->drop++;
1158
1159       if (!videorate->silent)
1160         gst_video_rate_notify_drop (videorate);
1161
1162       GST_LOG_OBJECT (videorate,
1163           "new is best, old never used, drop, outgoing ts %"
1164           GST_TIME_FORMAT, GST_TIME_ARGS (videorate->next_ts));
1165     }
1166     GST_LOG_OBJECT (videorate,
1167         "END, putting new in old, diff1 %" GST_TIME_FORMAT
1168         ", diff2 %" GST_TIME_FORMAT ", next_ts %" GST_TIME_FORMAT
1169         ", in %" G_GUINT64_FORMAT ", out %" G_GUINT64_FORMAT ", drop %"
1170         G_GUINT64_FORMAT ", dup %" G_GUINT64_FORMAT, GST_TIME_ARGS (diff1),
1171         GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
1172         videorate->in, videorate->out, videorate->drop, videorate->dup);
1173
1174     /* swap in new one when it's the best */
1175     gst_video_rate_swap_prev (videorate, buffer, intime);
1176   }
1177 done:
1178   return res;
1179
1180   /* ERRORS */
1181 not_negotiated:
1182   {
1183     GST_WARNING_OBJECT (videorate, "no framerate negotiated");
1184     res = GST_FLOW_NOT_NEGOTIATED;
1185     goto done;
1186   }
1187
1188 invalid_buffer:
1189   {
1190     GST_WARNING_OBJECT (videorate,
1191         "Got buffer with GST_CLOCK_TIME_NONE timestamp, discarding it");
1192     res = GST_BASE_TRANSFORM_FLOW_DROPPED;
1193     goto done;
1194   }
1195 }
1196
1197 static gboolean
1198 gst_video_rate_start (GstBaseTransform * trans)
1199 {
1200   gst_video_rate_reset (GST_VIDEO_RATE (trans));
1201   return TRUE;
1202 }
1203
1204 static gboolean
1205 gst_video_rate_stop (GstBaseTransform * trans)
1206 {
1207   gst_video_rate_reset (GST_VIDEO_RATE (trans));
1208   return TRUE;
1209 }
1210
1211 static void
1212 gst_video_rate_set_property (GObject * object,
1213     guint prop_id, const GValue * value, GParamSpec * pspec)
1214 {
1215   GstVideoRate *videorate = GST_VIDEO_RATE (object);
1216
1217   GST_OBJECT_LOCK (videorate);
1218   switch (prop_id) {
1219     case PROP_SILENT:
1220       videorate->silent = g_value_get_boolean (value);
1221       break;
1222     case PROP_NEW_PREF:
1223       videorate->new_pref = g_value_get_double (value);
1224       break;
1225     case PROP_SKIP_TO_FIRST:
1226       videorate->skip_to_first = g_value_get_boolean (value);
1227       break;
1228     case PROP_DROP_ONLY:
1229       videorate->drop_only = g_value_get_boolean (value);
1230       goto reconfigure;
1231       break;
1232     case PROP_AVERAGE_PERIOD:
1233       videorate->average_period_set = g_value_get_uint64 (value);
1234       break;
1235     case PROP_MAX_RATE:
1236       g_atomic_int_set (&videorate->max_rate, g_value_get_int (value));
1237       goto reconfigure;
1238       break;
1239     case PROP_FORCE_FPS:
1240       videorate->force_fps_n = gst_value_get_fraction_numerator (value);
1241       videorate->force_fps_d = gst_value_get_fraction_denominator (value);
1242       goto reconfigure;
1243       break;
1244     default:
1245       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1246       break;
1247   }
1248   GST_OBJECT_UNLOCK (videorate);
1249   return;
1250
1251 reconfigure:
1252   GST_OBJECT_UNLOCK (videorate);
1253   gst_base_transform_reconfigure (GST_BASE_TRANSFORM (videorate));
1254 }
1255
1256 static void
1257 gst_video_rate_get_property (GObject * object,
1258     guint prop_id, GValue * value, GParamSpec * pspec)
1259 {
1260   GstVideoRate *videorate = GST_VIDEO_RATE (object);
1261
1262   GST_OBJECT_LOCK (videorate);
1263   switch (prop_id) {
1264     case PROP_IN:
1265       g_value_set_uint64 (value, videorate->in);
1266       break;
1267     case PROP_OUT:
1268       g_value_set_uint64 (value, videorate->out);
1269       break;
1270     case PROP_DUP:
1271       g_value_set_uint64 (value, videorate->dup);
1272       break;
1273     case PROP_DROP:
1274       g_value_set_uint64 (value, videorate->drop);
1275       break;
1276     case PROP_SILENT:
1277       g_value_set_boolean (value, videorate->silent);
1278       break;
1279     case PROP_NEW_PREF:
1280       g_value_set_double (value, videorate->new_pref);
1281       break;
1282     case PROP_SKIP_TO_FIRST:
1283       g_value_set_boolean (value, videorate->skip_to_first);
1284       break;
1285     case PROP_DROP_ONLY:
1286       g_value_set_boolean (value, videorate->drop_only);
1287       break;
1288     case PROP_AVERAGE_PERIOD:
1289       g_value_set_uint64 (value, videorate->average_period_set);
1290       break;
1291     case PROP_MAX_RATE:
1292       g_value_set_int (value, g_atomic_int_get (&videorate->max_rate));
1293       break;
1294     case PROP_FORCE_FPS:
1295       gst_value_set_fraction (value, videorate->force_fps_n,
1296           videorate->force_fps_d);
1297       break;
1298     default:
1299       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1300       break;
1301   }
1302   GST_OBJECT_UNLOCK (videorate);
1303 }
1304
1305 static gboolean
1306 plugin_init (GstPlugin * plugin)
1307 {
1308   GST_DEBUG_CATEGORY_INIT (video_rate_debug, "videorate", 0,
1309       "VideoRate stream fixer");
1310
1311   return gst_element_register (plugin, "videorate", GST_RANK_NONE,
1312       GST_TYPE_VIDEO_RATE);
1313 }
1314
1315 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1316     GST_VERSION_MINOR,
1317     "videorate",
1318     "Adjusts video frames",
1319     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)