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