Update for new seeking variable name
[platform/upstream/gstreamer.git] / gst / gstsegment.c
1 /* GStreamer
2  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3  *
4  * gstsegment.c: GstSegment subsystem
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "gst_private.h"
23
24 #include <math.h>
25
26 #include "gstutils.h"
27 #include "gstsegment.h"
28
29 /**
30  * SECTION:gstsegment
31  * @short_description: Structure describing the configured region of interest
32  *                     in a media file.
33  * @see_also: #GstEvent
34  *
35  * This helper structure holds the relevant values for tracking the region of
36  * interest in a media file, called a segment.
37  *
38  * The structure can be used for two purposes:
39  * <itemizedlist>
40  *   <listitem><para>performing seeks (handling seek events)</para></listitem>
41  *   <listitem><para>tracking playback regions (handling newsegment events)</para></listitem>
42  * </itemizedlist>
43  *
44  * The segment is usually configured by the application with a seek event which
45  * is propagated upstream and eventually handled by an element that performs the seek.
46  *
47  * The configured segment is then propagated back downstream with a newsegment event.
48  * This information is then used to clip media to the segment boundaries.
49  *
50  * A segment structure is initialized with gst_segment_init(), which takes a #GstFormat
51  * that will be used as the format of the segment values. The segment will be configured
52  * with a start value of 0 and a stop/duration of -1, which is undefined. The default
53  * rate and applied_rate is 1.0.
54  *
55  * The public duration field contains the duration of the segment. When using
56  * the segment for seeking, the start and time members should normally be left
57  * to their default 0 value. The stop position is left to -1 unless explicitly
58  * configured to a different value after a seek event.
59  *
60  * The current position in the segment should be set by changing the position
61  * member in the structure.
62  *
63  * For elements that perform seeks, the current segment should be updated with the
64  * gst_segment_do_seek() and the values from the seek event. This method will update
65  * all the segment fields. The position field will contain the new playback position.
66  * If the start_type was different from GST_SEEK_TYPE_NONE, playback continues from
67  * the position position, possibly with updated flags or rate.
68  *
69  * For elements that want to use #GstSegment to track the playback region,
70  * update the segment fields with the information from the newsegment event.
71  * The gst_segment_clip() method can be used to check and clip
72  * the media data to the segment boundaries.
73  *
74  * For elements that want to synchronize to the pipeline clock, gst_segment_to_running_time()
75  * can be used to convert a timestamp to a value that can be used to synchronize
76  * to the clock. This function takes into account the base as well as
77  * any rate or applied_rate conversions.
78  *
79  * For elements that need to perform operations on media data in stream_time,
80  * gst_segment_to_stream_time() can be used to convert a timestamp and the segment
81  * info to stream time (which is always between 0 and the duration of the stream).
82  *
83  * Last reviewed on 2012-03-29 (0.11.3)
84  */
85
86 /**
87  * gst_segment_copy:
88  * @segment: (transfer none): a #GstSegment
89  *
90  * Create a copy of given @segment.
91  *
92  * Free-function: gst_segment_free
93  *
94  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
95  */
96 GstSegment *
97 gst_segment_copy (const GstSegment * segment)
98 {
99   GstSegment *result = NULL;
100
101   if (segment) {
102     result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
103   }
104   return result;
105 }
106
107 /**
108  * gst_segment_copy_into:
109  * @src: (transfer none): a #GstSegment
110  * @dest: (transfer none): a #GstSegment
111  *
112  * Copy the contents of @src into @dest.
113  */
114 void
115 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
116 {
117   memcpy (dest, src, sizeof (GstSegment));
118 }
119
120 G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
121     (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
122
123 /**
124  * gst_segment_new:
125  *
126  * Allocate a new #GstSegment structure and initialize it using
127  * gst_segment_init().
128  *
129  * Free-function: gst_segment_free
130  *
131  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
132  */
133 GstSegment *
134 gst_segment_new (void)
135 {
136   GstSegment *result;
137
138   result = g_slice_new0 (GstSegment);
139   gst_segment_init (result, GST_FORMAT_UNDEFINED);
140
141   return result;
142 }
143
144 /**
145  * gst_segment_free:
146  * @segment: (in) (transfer full): a #GstSegment
147  *
148  * Free the allocated segment @segment.
149  */
150 void
151 gst_segment_free (GstSegment * segment)
152 {
153   g_slice_free (GstSegment, segment);
154 }
155
156 /**
157  * gst_segment_init:
158  * @segment: a #GstSegment structure.
159  * @format: the format of the segment.
160  *
161  * The start/last_stop positions are set to 0 and the stop/duration
162  * fields are set to -1 (unknown). The default rate of 1.0 and no
163  * flags are set.
164  *
165  * Initialize @segment to its default values.
166  */
167 void
168 gst_segment_init (GstSegment * segment, GstFormat format)
169 {
170   g_return_if_fail (segment != NULL);
171
172   segment->flags = GST_SEGMENT_FLAG_NONE;
173   segment->rate = 1.0;
174   segment->applied_rate = 1.0;
175   segment->format = format;
176   segment->base = 0;
177   segment->start = 0;
178   segment->stop = -1;
179   segment->time = 0;
180   segment->position = 0;
181   segment->duration = -1;
182 }
183
184 /**
185  * gst_segment_do_seek:
186  * @segment: a #GstSegment structure.
187  * @rate: the rate of the segment.
188  * @format: the format of the segment.
189  * @flags: the segment flags for the segment
190  * @start_type: the seek method
191  * @start: the seek start value
192  * @stop_type: the seek method
193  * @stop: the seek stop value
194  * @update: boolean holding whether position was updated.
195  *
196  * Update the segment structure with the field values of a seek event (see
197  * gst_event_new_seek()).
198  *
199  * After calling this method, the segment field position and time will
200  * contain the requested new position in the segment. The new requested
201  * position in the segment depends on @rate and @start_type and @stop_type.
202  *
203  * For positive @rate, the new position in the segment is the new @segment
204  * start field when it was updated with a @start_type different from
205  * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
206  * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
207  * unmodified.
208  *
209  * For negative @rate, the new position in the segment is the new @segment
210  * stop field when it was updated with a @stop_type different from
211  * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
212  * duration of the segment will be used to update the stop position.
213  * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
214  * @stop is ignored and @segment position is unmodified.
215  *
216  * The applied rate of the segment will be set to 1.0 by default.
217  * If the caller can apply a rate change, it should update @segment
218  * rate and applied_rate after calling this function.
219  *
220  * @update will be set to TRUE if a seek should be performed to the segment
221  * position field. This field can be FALSE if, for example, only the @rate
222  * has been changed but not the playback position.
223  *
224  * Returns: %TRUE if the seek could be performed.
225  */
226 gboolean
227 gst_segment_do_seek (GstSegment * segment, gdouble rate,
228     GstFormat format, GstSeekFlags flags,
229     GstSeekType start_type, guint64 start,
230     GstSeekType stop_type, guint64 stop, gboolean * update)
231 {
232   gboolean update_stop, update_start;
233   guint64 position, base;
234
235   g_return_val_if_fail (rate != 0.0, FALSE);
236   g_return_val_if_fail (segment != NULL, FALSE);
237   g_return_val_if_fail (segment->format == format, FALSE);
238
239   update_start = update_stop = TRUE;
240
241   position = segment->position;
242
243   /* segment->start is never invalid */
244   switch (start_type) {
245     case GST_SEEK_TYPE_NONE:
246       /* no update to segment, take previous start */
247       start = segment->start;
248       update_start = FALSE;
249       break;
250     case GST_SEEK_TYPE_SET:
251       /* start holds desired position, map -1 to the start */
252       if (start == -1)
253         start = 0;
254       break;
255     case GST_SEEK_TYPE_END:
256       if (segment->duration != -1) {
257         /* add start to total length */
258         start = segment->duration + start;
259       } else {
260         /* no update if duration unknown */
261         start = segment->start;
262         update_start = FALSE;
263       }
264       break;
265   }
266   /* bring in sane range */
267   if (segment->duration != -1)
268     start = MIN (start, segment->duration);
269   else
270     start = MAX (start, 0);
271
272   /* stop can be -1 if we have not configured a stop. */
273   switch (stop_type) {
274     case GST_SEEK_TYPE_NONE:
275       stop = segment->stop;
276       update_stop = FALSE;
277       break;
278     case GST_SEEK_TYPE_SET:
279       /* stop holds required value */
280       break;
281     case GST_SEEK_TYPE_END:
282       if (segment->duration != -1) {
283         stop = segment->duration + stop;
284       } else {
285         stop = segment->stop;
286         update_stop = FALSE;
287       }
288       break;
289   }
290
291   /* if we have a valid stop time, make sure it is clipped */
292   if (stop != -1) {
293     if (segment->duration != -1)
294       stop = CLAMP (stop, 0, segment->duration);
295     else
296       stop = MAX (stop, 0);
297   }
298
299   /* we can't have stop before start */
300   if (stop != -1) {
301     if (start > stop) {
302       g_return_val_if_fail (start <= stop, FALSE);
303       return FALSE;
304     }
305   }
306
307   if (flags & GST_SEEK_FLAG_FLUSH) {
308     /* flush resets the running_time */
309     base = 0;
310   } else {
311     base = gst_segment_to_running_time (segment, format, position);
312   }
313
314   if (update_start && rate > 0.0) {
315     position = start;
316   }
317   if (update_stop && rate < 0.0) {
318     if (stop != -1)
319       position = stop;
320     else {
321       if (segment->duration != -1)
322         position = segment->duration;
323       else
324         position = 0;
325     }
326   }
327   /* set update arg to reflect update of position */
328   if (update)
329     *update = position != segment->position;
330
331   /* update new values */
332   segment->rate = rate;
333   segment->applied_rate = 1.0;
334   segment->base = base;
335   /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
336   segment->flags = GST_SEGMENT_FLAG_NONE;
337   if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
338     segment->flags |= GST_SEGMENT_FLAG_RESET;
339   if ((flags & GST_SEEK_FLAG_SKIP) != 0)
340     segment->flags |= GST_SEGMENT_FLAG_SKIP;
341   if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
342     segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
343   segment->start = start;
344   segment->stop = stop;
345   segment->time = start;
346   segment->position = position;
347
348   return TRUE;
349 }
350
351 /**
352  * gst_segment_to_stream_time:
353  * @segment: a #GstSegment structure.
354  * @format: the format of the segment.
355  * @position: the position in the segment
356  *
357  * Translate @position to stream time using the currently configured
358  * segment. The @position value must be between @segment start and
359  * stop value.
360  *
361  * This function is typically used by elements that need to operate on
362  * the stream time of the buffers it receives, such as effect plugins.
363  * In those use cases, @position is typically the buffer timestamp or
364  * clock time that one wants to convert to the stream time.
365  * The stream time is always between 0 and the total duration of the
366  * media stream.
367  *
368  * Returns: the position in stream_time or -1 when an invalid position
369  * was given.
370  */
371 guint64
372 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
373     guint64 position)
374 {
375   guint64 result, start, stop, time;
376   gdouble abs_applied_rate;
377
378   /* format does not matter for -1 */
379   if (G_UNLIKELY (position == -1))
380     return -1;
381
382   g_return_val_if_fail (segment != NULL, -1);
383   g_return_val_if_fail (segment->format == format, -1);
384
385   stop = segment->stop;
386
387   /* outside of the segment boundary stop */
388   if (G_UNLIKELY (stop != -1 && position > stop))
389     return -1;
390
391   start = segment->start;
392
393   /* before the segment boundary */
394   if (G_UNLIKELY (position < start))
395     return -1;
396
397   time = segment->time;
398
399   /* time must be known */
400   if (G_UNLIKELY (time == -1))
401     return -1;
402
403   /* bring to uncorrected position in segment */
404   result = position - start;
405
406   abs_applied_rate = ABS (segment->applied_rate);
407
408   /* correct for applied rate if needed */
409   if (G_UNLIKELY (abs_applied_rate != 1.0))
410     result *= abs_applied_rate;
411
412   /* add or subtract from segment time based on applied rate */
413   if (G_LIKELY (segment->applied_rate > 0.0)) {
414     /* correct for segment time */
415     result += time;
416   } else {
417     /* correct for segment time, clamp at 0. Streams with a negative
418      * applied_rate have timestamps between start and stop, as usual, but have
419      * the time member starting high and going backwards.  */
420     if (G_LIKELY (time > result))
421       result = time - result;
422     else
423       result = 0;
424   }
425
426   return result;
427 }
428
429 /**
430  * gst_segment_to_running_time:
431  * @segment: a #GstSegment structure.
432  * @format: the format of the segment.
433  * @position: the position in the segment
434  *
435  * Translate @position to the total running time using the currently configured
436  * segment. Position is a value between @segment start and stop time.
437  *
438  * This function is typically used by elements that need to synchronize to the
439  * global clock in a pipeline. The runnning time is a constantly increasing value
440  * starting from 0. When gst_segment_init() is called, this value will reset to
441  * 0.
442  *
443  * This function returns -1 if the position is outside of @segment start and stop.
444  *
445  * Returns: the position as the total running time or -1 when an invalid position
446  * was given.
447  */
448 guint64
449 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
450     guint64 position)
451 {
452   guint64 result;
453   guint64 start, stop;
454   gdouble abs_rate;
455
456   if (G_UNLIKELY (position == -1))
457     return -1;
458
459   g_return_val_if_fail (segment != NULL, -1);
460   g_return_val_if_fail (segment->format == format, -1);
461
462   start = segment->start;
463
464   /* before the segment boundary */
465   if (G_UNLIKELY (position < start))
466     return -1;
467
468   stop = segment->stop;
469
470   if (G_LIKELY (segment->rate > 0.0)) {
471     /* outside of the segment boundary stop */
472     if (G_UNLIKELY (stop != -1 && position > stop))
473       return -1;
474
475     /* bring to uncorrected position in segment */
476     result = position - start;
477   } else {
478     /* cannot continue if no stop position set or outside of
479      * the segment. */
480     if (G_UNLIKELY (stop == -1 || position > stop))
481       return -1;
482
483     /* bring to uncorrected position in segment */
484     result = stop - position;
485   }
486
487   /* scale based on the rate, avoid division by and conversion to
488    * float when not needed */
489   abs_rate = ABS (segment->rate);
490   if (G_UNLIKELY (abs_rate != 1.0))
491     result /= abs_rate;
492
493   /* correct for base of the segment */
494   result += segment->base;
495
496   return result;
497 }
498
499 /**
500  * gst_segment_clip:
501  * @segment: a #GstSegment structure.
502  * @format: the format of the segment.
503  * @start: the start position in the segment
504  * @stop: the stop position in the segment
505  * @clip_start: (out) (allow-none): the clipped start position in the segment
506  * @clip_stop: (out) (allow-none): the clipped stop position in the segment
507  *
508  * Clip the given @start and @stop values to the segment boundaries given
509  * in @segment. @start and @stop are compared and clipped to @segment
510  * start and stop values.
511  *
512  * If the function returns FALSE, @start and @stop are known to fall
513  * outside of @segment and @clip_start and @clip_stop are not updated.
514  *
515  * When the function returns TRUE, @clip_start and @clip_stop will be
516  * updated. If @clip_start or @clip_stop are different from @start or @stop
517  * respectively, the region fell partially in the segment.
518  *
519  * Note that when @stop is -1, @clip_stop will be set to the end of the
520  * segment. Depending on the use case, this may or may not be what you want.
521  *
522  * Returns: TRUE if the given @start and @stop times fall partially or
523  *     completely in @segment, FALSE if the values are completely outside
524  *     of the segment.
525  */
526 gboolean
527 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
528     guint64 stop, guint64 * clip_start, guint64 * clip_stop)
529 {
530   g_return_val_if_fail (segment != NULL, FALSE);
531   g_return_val_if_fail (segment->format == format, FALSE);
532
533   /* if we have a stop position and a valid start and start is bigger,
534    * we're outside of the segment */
535   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
536     return FALSE;
537
538   /* if a stop position is given and is before the segment start,
539    * we're outside of the segment. Special case is were start
540    * and stop are equal to the segment start. In that case we
541    * are inside the segment. */
542   if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
543                   && stop == segment->start))))
544     return FALSE;
545
546   if (clip_start) {
547     if (start == -1)
548       *clip_start = -1;
549     else
550       *clip_start = MAX (start, segment->start);
551   }
552
553   if (clip_stop) {
554     if (stop == -1)
555       *clip_stop = segment->stop;
556     else if (segment->stop == -1)
557       *clip_stop = stop;
558     else
559       *clip_stop = MIN (stop, segment->stop);
560   }
561
562   return TRUE;
563 }
564
565 /**
566  * gst_segment_to_position:
567  * @segment: a #GstSegment structure.
568  * @format: the format of the segment.
569  * @running_time: the running_time in the segment
570  *
571  * Convert @running_time into a position in the segment so that
572  * gst_segment_to_running_time() with that position returns @running_time.
573  *
574  * Returns: the position in the segment for @running_time. This function returns
575  * -1 when @running_time is -1 or when it is not inside @segment.
576  */
577 guint64
578 gst_segment_to_position (const GstSegment * segment, GstFormat format,
579     guint64 running_time)
580 {
581   guint64 result;
582   guint64 start, stop, base;
583   gdouble abs_rate;
584
585   if (G_UNLIKELY (running_time == -1))
586     return -1;
587
588   g_return_val_if_fail (segment != NULL, -1);
589   g_return_val_if_fail (segment->format == format, FALSE);
590
591   base = segment->base;
592
593   /* this running_time was for a previous segment */
594   if (running_time < base)
595     return -1;
596
597   /* start by subtracting the base time */
598   result = running_time - base;
599
600   /* move into the segment at the right rate */
601   abs_rate = ABS (segment->rate);
602   if (G_UNLIKELY (abs_rate != 1.0))
603     result = ceil (result * abs_rate);
604
605   start = segment->start;
606   stop = segment->stop;
607
608   if (G_LIKELY (segment->rate > 0.0)) {
609     /* bring to corrected position in segment */
610     result += start;
611
612     /* outside of the segment boundary stop */
613     if (G_UNLIKELY (stop != -1 && result > stop))
614       return -1;
615   } else {
616     /* cannot continue if no stop position set or outside of
617      * the segment. */
618     if (G_UNLIKELY (stop == -1 || result + start > stop))
619       return -1;
620
621     /* bring to corrected position in segment */
622     result = stop - result;
623   }
624   return result;
625 }
626
627
628 /**
629  * gst_segment_set_running_time:
630  * @segment: a #GstSegment structure.
631  * @format: the format of the segment.
632  * @running_time: the running_time in the segment
633  *
634  * Adjust the start/stop and base values of @segment such that the next valid
635  * buffer will be one with @running_time.
636  *
637  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
638  * returned, @running_time is -1 or not in @segment.
639  */
640 gboolean
641 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
642     guint64 running_time)
643 {
644   guint64 position;
645   guint64 start, stop;
646
647   /* start by bringing the running_time into the segment position */
648   position = gst_segment_to_position (segment, format, running_time);
649
650   /* we must have a valid position now */
651   if (G_UNLIKELY (position == -1))
652     return FALSE;
653
654   start = segment->start;
655   stop = segment->stop;
656
657   if (G_LIKELY (segment->rate > 0.0)) {
658     /* update the start and time values */
659     start = position;
660   } else {
661     /* reverse, update stop */
662     stop = position;
663   }
664   /* and base time is exactly the running time */
665   segment->time = gst_segment_to_stream_time (segment, format, start);
666   segment->start = start;
667   segment->stop = stop;
668   segment->base = running_time;
669
670   return TRUE;
671 }