segment: take offset into account in _to_position()
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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/position fields 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->offset = 0;
178   segment->start = 0;
179   segment->stop = -1;
180   segment->time = 0;
181   segment->position = 0;
182   segment->duration = -1;
183 }
184
185 /**
186  * gst_segment_do_seek:
187  * @segment: a #GstSegment structure.
188  * @rate: the rate of the segment.
189  * @format: the format of the segment.
190  * @flags: the segment flags for the segment
191  * @start_type: the seek method
192  * @start: the seek start value
193  * @stop_type: the seek method
194  * @stop: the seek stop value
195  * @update: boolean holding whether position was updated.
196  *
197  * Update the segment structure with the field values of a seek event (see
198  * gst_event_new_seek()).
199  *
200  * After calling this method, the segment field position and time will
201  * contain the requested new position in the segment. The new requested
202  * position in the segment depends on @rate and @start_type and @stop_type.
203  *
204  * For positive @rate, the new position in the segment is the new @segment
205  * start field when it was updated with a @start_type different from
206  * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
207  * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
208  * unmodified.
209  *
210  * For negative @rate, the new position in the segment is the new @segment
211  * stop field when it was updated with a @stop_type different from
212  * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
213  * duration of the segment will be used to update the stop position.
214  * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
215  * @stop is ignored and @segment position is unmodified.
216  *
217  * The applied rate of the segment will be set to 1.0 by default.
218  * If the caller can apply a rate change, it should update @segment
219  * rate and applied_rate after calling this function.
220  *
221  * @update will be set to TRUE if a seek should be performed to the segment
222  * position field. This field can be FALSE if, for example, only the @rate
223  * has been changed but not the playback position.
224  *
225  * Returns: %TRUE if the seek could be performed.
226  */
227 gboolean
228 gst_segment_do_seek (GstSegment * segment, gdouble rate,
229     GstFormat format, GstSeekFlags flags,
230     GstSeekType start_type, guint64 start,
231     GstSeekType stop_type, guint64 stop, gboolean * update)
232 {
233   gboolean update_stop, update_start;
234   guint64 position, base;
235
236   g_return_val_if_fail (rate != 0.0, FALSE);
237   g_return_val_if_fail (segment != NULL, FALSE);
238   g_return_val_if_fail (segment->format == format, FALSE);
239
240   update_start = update_stop = TRUE;
241
242   position = segment->position;
243
244   /* segment->start is never invalid */
245   switch (start_type) {
246     case GST_SEEK_TYPE_NONE:
247       /* no update to segment, take previous start */
248       start = segment->start;
249       update_start = FALSE;
250       break;
251     case GST_SEEK_TYPE_SET:
252       /* start holds desired position, map -1 to the start */
253       if (start == -1)
254         start = 0;
255       break;
256     case GST_SEEK_TYPE_END:
257       if (segment->duration != -1) {
258         /* add start to total length */
259         start = segment->duration + start;
260       } else {
261         /* no update if duration unknown */
262         start = segment->start;
263         update_start = FALSE;
264       }
265       break;
266   }
267   /* bring in sane range */
268   if (segment->duration != -1)
269     start = MIN (start, segment->duration);
270   else
271     start = MAX ((gint64) start, 0);
272
273   /* stop can be -1 if we have not configured a stop. */
274   switch (stop_type) {
275     case GST_SEEK_TYPE_NONE:
276       stop = segment->stop;
277       update_stop = FALSE;
278       break;
279     case GST_SEEK_TYPE_SET:
280       /* stop holds required value */
281       break;
282     case GST_SEEK_TYPE_END:
283       if (segment->duration != -1) {
284         stop = segment->duration + stop;
285       } else {
286         stop = segment->stop;
287         update_stop = FALSE;
288       }
289       break;
290   }
291
292   /* if we have a valid stop time, make sure it is clipped */
293   if (stop != -1) {
294     if (segment->duration != -1)
295       stop = CLAMP ((gint64) stop, 0, (gint64) segment->duration);
296     else
297       stop = MAX ((gint64) stop, 0);
298   }
299
300   /* we can't have stop before start */
301   if (stop != -1) {
302     if (start > stop) {
303       GST_WARNING ("segment update failed: start(%" G_GUINT64_FORMAT
304           ") > stop(%" G_GUINT64_FORMAT ")", start, stop);
305       g_return_val_if_fail (start <= stop, FALSE);
306       return FALSE;
307     }
308   }
309
310   if (flags & GST_SEEK_FLAG_FLUSH) {
311     /* flush resets the running_time */
312     base = 0;
313   } else {
314     /* make sure the position is inside the segment start/stop */
315     position = CLAMP (position, segment->start, segment->stop);
316
317     /* remember the elapsed time */
318     base = gst_segment_to_running_time (segment, format, position);
319     GST_DEBUG ("updated segment.base: %" G_GUINT64_FORMAT, base);
320   }
321
322   if (update_start && rate > 0.0) {
323     position = start;
324   }
325   if (update_stop && rate < 0.0) {
326     if (stop != -1)
327       position = stop;
328     else {
329       if (segment->duration != -1)
330         position = segment->duration;
331       else
332         position = 0;
333     }
334   }
335
336   /* set update arg to reflect update of position */
337   if (update)
338     *update = position != segment->position;
339
340   /* update new values */
341   /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
342   segment->flags = GST_SEGMENT_FLAG_NONE;
343   if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
344     segment->flags |= GST_SEGMENT_FLAG_RESET;
345   if ((flags & GST_SEEK_FLAG_SKIP) != 0)
346     segment->flags |= GST_SEGMENT_FLAG_SKIP;
347   if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
348     segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
349
350   segment->rate = rate;
351   segment->applied_rate = 1.0;
352
353   segment->base = base;
354   if (rate > 0.0)
355     segment->offset = position - start;
356   else {
357     if (stop != -1)
358       segment->offset = stop - position;
359     else if (segment->duration != -1)
360       segment->offset = segment->duration - position;
361     else
362       segment->offset = 0;
363   }
364
365   segment->start = start;
366   segment->stop = stop;
367   segment->time = start;
368   segment->position = position;
369
370   GST_INFO ("segment updated: %" GST_SEGMENT_FORMAT, segment);
371
372   return TRUE;
373 }
374
375 /**
376  * gst_segment_to_stream_time:
377  * @segment: a #GstSegment structure.
378  * @format: the format of the segment.
379  * @position: the position in the segment
380  *
381  * Translate @position to stream time using the currently configured
382  * segment. The @position value must be between @segment start and
383  * stop value.
384  *
385  * This function is typically used by elements that need to operate on
386  * the stream time of the buffers it receives, such as effect plugins.
387  * In those use cases, @position is typically the buffer timestamp or
388  * clock time that one wants to convert to the stream time.
389  * The stream time is always between 0 and the total duration of the
390  * media stream.
391  *
392  * Returns: the position in stream_time or -1 when an invalid position
393  * was given.
394  */
395 guint64
396 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
397     guint64 position)
398 {
399   guint64 result, start, stop, time;
400   gdouble abs_applied_rate;
401
402   /* format does not matter for -1 */
403   if (G_UNLIKELY (position == -1))
404     return -1;
405
406   g_return_val_if_fail (segment != NULL, -1);
407   g_return_val_if_fail (segment->format == format, -1);
408
409   stop = segment->stop;
410
411   /* outside of the segment boundary stop */
412   if (G_UNLIKELY (stop != -1 && position > stop))
413     return -1;
414
415   start = segment->start;
416
417   /* before the segment boundary */
418   if (G_UNLIKELY (position < start))
419     return -1;
420
421   time = segment->time;
422
423   /* time must be known */
424   if (G_UNLIKELY (time == -1))
425     return -1;
426
427   /* bring to uncorrected position in segment */
428   result = position - start;
429
430   abs_applied_rate = ABS (segment->applied_rate);
431
432   /* correct for applied rate if needed */
433   if (G_UNLIKELY (abs_applied_rate != 1.0))
434     result *= abs_applied_rate;
435
436   /* add or subtract from segment time based on applied rate */
437   if (G_LIKELY (segment->applied_rate > 0.0)) {
438     /* correct for segment time */
439     result += time;
440   } else {
441     /* correct for segment time, clamp at 0. Streams with a negative
442      * applied_rate have timestamps between start and stop, as usual, but have
443      * the time member starting high and going backwards.  */
444     if (G_LIKELY (time > result))
445       result = time - result;
446     else
447       result = 0;
448   }
449
450   return result;
451 }
452
453 /**
454  * gst_segment_to_running_time:
455  * @segment: a #GstSegment structure.
456  * @format: the format of the segment.
457  * @position: the position in the segment
458  *
459  * Translate @position to the total running time using the currently configured
460  * segment. Position is a value between @segment start and stop time.
461  *
462  * This function is typically used by elements that need to synchronize to the
463  * global clock in a pipeline. The running time is a constantly increasing value
464  * starting from 0. When gst_segment_init() is called, this value will reset to
465  * 0.
466  *
467  * This function returns -1 if the position is outside of @segment start and stop.
468  *
469  * Returns: the position as the total running time or -1 when an invalid position
470  * was given.
471  */
472 guint64
473 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
474     guint64 position)
475 {
476   guint64 result;
477   guint64 start, stop;
478   gdouble abs_rate;
479
480   if (G_UNLIKELY (position == -1)) {
481     GST_DEBUG ("invalid position (-1)");
482     return -1;
483   }
484
485   g_return_val_if_fail (segment != NULL, -1);
486   g_return_val_if_fail (segment->format == format, -1);
487
488   start = segment->start;
489
490   if (segment->rate > 0.0)
491     start += segment->offset;
492
493   /* before the segment boundary */
494   if (G_UNLIKELY (position < start)) {
495     GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
496         ")", position, start);
497     return -1;
498   }
499
500   stop = segment->stop;
501
502   if (G_LIKELY (segment->rate > 0.0)) {
503     /* after of the segment boundary */
504     if (G_UNLIKELY (stop != -1 && position > stop)) {
505       GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
506           ")", position, stop);
507       return -1;
508     }
509
510     /* bring to uncorrected position in segment */
511     result = position - start;
512   } else {
513     /* cannot continue if no stop position set or outside of
514      * the segment. */
515     if (G_UNLIKELY (stop == -1)) {
516       GST_DEBUG ("invalid stop (-1)");
517       return -1;
518     }
519
520     stop -= segment->offset;
521     if (G_UNLIKELY (position > stop)) {
522       GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
523           ")", position, stop);
524       return -1;
525     }
526
527     /* bring to uncorrected position in segment */
528     result = stop - position;
529   }
530
531   /* scale based on the rate, avoid division by and conversion to
532    * float when not needed */
533   abs_rate = ABS (segment->rate);
534   if (G_UNLIKELY (abs_rate != 1.0))
535     result /= abs_rate;
536
537   /* correct for base of the segment */
538   result += segment->base;
539
540   return result;
541 }
542
543 /**
544  * gst_segment_clip:
545  * @segment: a #GstSegment structure.
546  * @format: the format of the segment.
547  * @start: the start position in the segment
548  * @stop: the stop position in the segment
549  * @clip_start: (out) (allow-none): the clipped start position in the segment
550  * @clip_stop: (out) (allow-none): the clipped stop position in the segment
551  *
552  * Clip the given @start and @stop values to the segment boundaries given
553  * in @segment. @start and @stop are compared and clipped to @segment
554  * start and stop values.
555  *
556  * If the function returns FALSE, @start and @stop are known to fall
557  * outside of @segment and @clip_start and @clip_stop are not updated.
558  *
559  * When the function returns TRUE, @clip_start and @clip_stop will be
560  * updated. If @clip_start or @clip_stop are different from @start or @stop
561  * respectively, the region fell partially in the segment.
562  *
563  * Note that when @stop is -1, @clip_stop will be set to the end of the
564  * segment. Depending on the use case, this may or may not be what you want.
565  *
566  * Returns: TRUE if the given @start and @stop times fall partially or
567  *     completely in @segment, FALSE if the values are completely outside
568  *     of the segment.
569  */
570 gboolean
571 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
572     guint64 stop, guint64 * clip_start, guint64 * clip_stop)
573 {
574   g_return_val_if_fail (segment != NULL, FALSE);
575   g_return_val_if_fail (segment->format == format, FALSE);
576
577   /* if we have a stop position and a valid start and start is bigger,
578    * we're outside of the segment */
579   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
580     return FALSE;
581
582   /* if a stop position is given and is before the segment start,
583    * we're outside of the segment. Special case is were start
584    * and stop are equal to the segment start. In that case we
585    * are inside the segment. */
586   if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
587                   && stop == segment->start))))
588     return FALSE;
589
590   if (clip_start) {
591     if (start == -1)
592       *clip_start = -1;
593     else
594       *clip_start = MAX (start, segment->start);
595   }
596
597   if (clip_stop) {
598     if (stop == -1)
599       *clip_stop = segment->stop;
600     else if (segment->stop == -1)
601       *clip_stop = stop;
602     else
603       *clip_stop = MIN (stop, segment->stop);
604   }
605
606   return TRUE;
607 }
608
609 /**
610  * gst_segment_to_position:
611  * @segment: a #GstSegment structure.
612  * @format: the format of the segment.
613  * @running_time: the running_time in the segment
614  *
615  * Convert @running_time into a position in the segment so that
616  * gst_segment_to_running_time() with that position returns @running_time.
617  *
618  * Returns: the position in the segment for @running_time. This function returns
619  * -1 when @running_time is -1 or when it is not inside @segment.
620  */
621 guint64
622 gst_segment_to_position (const GstSegment * segment, GstFormat format,
623     guint64 running_time)
624 {
625   guint64 result;
626   guint64 start, stop, base;
627   gdouble abs_rate;
628
629   if (G_UNLIKELY (running_time == -1))
630     return -1;
631
632   g_return_val_if_fail (segment != NULL, -1);
633   g_return_val_if_fail (segment->format == format, FALSE);
634
635   base = segment->base;
636
637   /* this running_time was for a previous segment */
638   if (running_time < base)
639     return -1;
640
641   /* start by subtracting the base time */
642   result = running_time - base;
643
644   /* move into the segment at the right rate */
645   abs_rate = ABS (segment->rate);
646   if (G_UNLIKELY (abs_rate != 1.0))
647     result = ceil (result * abs_rate);
648
649   start = segment->start;
650   stop = segment->stop;
651
652   if (G_LIKELY (segment->rate > 0.0)) {
653     /* bring to corrected position in segment */
654     result += start + segment->offset;
655
656     /* outside of the segment boundary stop */
657     if (G_UNLIKELY (stop != -1 && result > stop))
658       return -1;
659   } else {
660     /* cannot continue if no stop position set or outside of
661      * the segment. */
662     if (G_UNLIKELY (stop == -1 || result + start > stop))
663       return -1;
664
665     /* bring to corrected position in segment */
666     result = stop - result - segment->offset;
667   }
668   return result;
669 }
670
671 /**
672  * gst_segment_set_running_time:
673  * @segment: a #GstSegment structure.
674  * @format: the format of the segment.
675  * @running_time: the running_time in the segment
676  *
677  * Adjust the start/stop and base values of @segment such that the next valid
678  * buffer will be one with @running_time.
679  *
680  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
681  * returned, @running_time is -1 or not in @segment.
682  */
683 gboolean
684 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
685     guint64 running_time)
686 {
687   guint64 position;
688   guint64 start, stop;
689
690   /* start by bringing the running_time into the segment position */
691   position = gst_segment_to_position (segment, format, running_time);
692
693   /* we must have a valid position now */
694   if (G_UNLIKELY (position == -1))
695     return FALSE;
696
697   start = segment->start;
698   stop = segment->stop;
699
700   if (G_LIKELY (segment->rate > 0.0)) {
701     /* update the start and time values */
702     start = position;
703   } else {
704     /* reverse, update stop */
705     stop = position;
706   }
707   /* and base time is exactly the running time */
708   segment->time = gst_segment_to_stream_time (segment, format, start);
709   segment->start = start;
710   segment->stop = stop;
711   segment->base = running_time;
712
713   return TRUE;
714 }
715
716 /**
717  * gst_segment_offset_running_time:
718  * @segment: a #GstSegment structure.
719  * @format: the format of the segment.
720  * @offset: the offset to apply in the segment
721  *
722  * Adjust the values in @segment so that @offset is applied to all
723  * future running-time calculations.
724  *
725  * Since: 1.4
726  *
727  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
728  * returned, @offset is not in @segment.
729  */
730 gboolean
731 gst_segment_offset_running_time (GstSegment * segment, GstFormat format,
732     gint64 offset)
733 {
734   g_return_val_if_fail (segment != NULL, FALSE);
735   g_return_val_if_fail (segment->format == format, FALSE);
736
737   if (offset == 0)
738     return TRUE;
739
740   if (offset > 0) {
741     /* positive offset, we can simply apply to the base time */
742     segment->base += offset;
743   } else {
744     offset = -offset;
745     /* negative offset, first try to subtract from base */
746     if (segment->base > offset) {
747       segment->base -= offset;
748     } else {
749       guint64 position;
750
751       /* subtract all from segment.base, remainder in offset */
752       offset -= segment->base;
753       segment->base = 0;
754       position = gst_segment_to_position (segment, format, offset);
755       if (position == -1)
756         return FALSE;
757
758       segment->offset = position;
759     }
760   }
761   return TRUE;
762 }