Remove 0.10-related documentation and "Since" markers
[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  * If the segment is used for managing seeks, the segment duration should be set with
56  * gst_segment_set_duration(). The public duration field contains the duration of the
57  * segment. When using the segment for seeking, the start and time members should
58  * normally be left to their default 0 value. The stop position is left to -1 unless
59  * explicitly configured to a different value after a seek event.
60  *
61  * The current position in the segment should be set by changing the position
62  * member in the structure.
63  *
64  * For elements that perform seeks, the current segment should be updated with the
65  * gst_segment_do_seek() and the values from the seek event. This method will update
66  * all the segment fields. The position field will contain the new playback position.
67  * If the cur_type was different from GST_SEEK_TYPE_NONE, playback continues from
68  * the position position, possibly with updated flags or rate.
69  *
70  * For elements that want to use #GstSegment to track the playback region,
71  * update the segment fields with the information from the newsegment event.
72  * The gst_segment_clip() method can be used to check and clip
73  * the media data to the segment boundaries.
74  *
75  * For elements that want to synchronize to the pipeline clock, gst_segment_to_running_time()
76  * can be used to convert a timestamp to a value that can be used to synchronize
77  * to the clock. This function takes into account the base as well as
78  * any rate or applied_rate conversions.
79  *
80  * For elements that need to perform operations on media data in stream_time,
81  * gst_segment_to_stream_time() can be used to convert a timestamp and the segment
82  * info to stream time (which is always between 0 and the duration of the stream).
83  *
84  * Last reviewed on 2012-03-29 (0.11.3)
85  */
86
87 /**
88  * gst_segment_copy:
89  * @segment: (transfer none): a #GstSegment
90  *
91  * Create a copy of given @segment.
92  *
93  * Free-function: gst_segment_free
94  *
95  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
96  */
97 GstSegment *
98 gst_segment_copy (const GstSegment * segment)
99 {
100   GstSegment *result = NULL;
101
102   if (segment) {
103     result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
104   }
105   return result;
106 }
107
108 /**
109  * gst_segment_copy_into:
110  * @src: (transfer none): a #GstSegment
111  * @dest: (transfer none): a #GstSegment
112  *
113  * Copy the contents of @src into @dest.
114  */
115 void
116 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
117 {
118   memcpy (dest, src, sizeof (GstSegment));
119 }
120
121 G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
122     (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
123
124 /**
125  * gst_segment_new:
126  *
127  * Allocate a new #GstSegment structure and initialize it using
128  * gst_segment_init().
129  *
130  * Free-function: gst_segment_free
131  *
132  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
133  */
134 GstSegment *
135 gst_segment_new (void)
136 {
137   GstSegment *result;
138
139   result = g_slice_new0 (GstSegment);
140   gst_segment_init (result, GST_FORMAT_UNDEFINED);
141
142   return result;
143 }
144
145 /**
146  * gst_segment_free:
147  * @segment: (in) (transfer full): a #GstSegment
148  *
149  * Free the allocated segment @segment.
150  */
151 void
152 gst_segment_free (GstSegment * segment)
153 {
154   g_slice_free (GstSegment, segment);
155 }
156
157 /**
158  * gst_segment_init:
159  * @segment: a #GstSegment structure.
160  * @format: the format of the segment.
161  *
162  * The start/last_stop positions are set to 0 and the stop/duration
163  * fields are set to -1 (unknown). The default rate of 1.0 and no
164  * flags are set.
165  *
166  * Initialize @segment to its default values.
167  */
168 void
169 gst_segment_init (GstSegment * segment, GstFormat format)
170 {
171   g_return_if_fail (segment != NULL);
172
173   segment->flags = GST_SEGMENT_FLAG_NONE;
174   segment->rate = 1.0;
175   segment->applied_rate = 1.0;
176   segment->format = format;
177   segment->base = 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   if (flags & GST_SEEK_FLAG_FLUSH) {
245     /* flush resets the running_time */
246     base = 0;
247   } else {
248     base = gst_segment_to_running_time (segment, format, position);
249   }
250
251   /* segment->start is never invalid */
252   switch (start_type) {
253     case GST_SEEK_TYPE_NONE:
254       /* no update to segment, take previous start */
255       start = segment->start;
256       update_start = FALSE;
257       break;
258     case GST_SEEK_TYPE_SET:
259       /* start holds desired position, map -1 to the start */
260       if (start == -1)
261         start = 0;
262       break;
263     case GST_SEEK_TYPE_END:
264       if (segment->duration != -1) {
265         /* add start to total length */
266         start = segment->duration + start;
267       } else {
268         /* no update if duration unknown */
269         start = segment->start;
270         update_start = FALSE;
271       }
272       break;
273   }
274   /* bring in sane range */
275   if (segment->duration != -1)
276     start = MIN (start, segment->duration);
277   else
278     start = MAX (start, 0);
279
280   /* stop can be -1 if we have not configured a stop. */
281   switch (stop_type) {
282     case GST_SEEK_TYPE_NONE:
283       stop = segment->stop;
284       update_stop = FALSE;
285       break;
286     case GST_SEEK_TYPE_SET:
287       /* stop holds required value */
288       break;
289     case GST_SEEK_TYPE_END:
290       if (segment->duration != -1) {
291         stop = segment->duration + stop;
292       } else {
293         stop = segment->stop;
294         update_stop = FALSE;
295       }
296       break;
297   }
298
299   /* if we have a valid stop time, make sure it is clipped */
300   if (stop != -1) {
301     if (segment->duration != -1)
302       stop = CLAMP (stop, 0, segment->duration);
303     else
304       stop = MAX (stop, 0);
305   }
306
307   /* we can't have stop before start */
308   if (stop != -1) {
309     if (start > stop) {
310       g_return_val_if_fail (start <= stop, FALSE);
311       return FALSE;
312     }
313   }
314
315   segment->rate = rate;
316   segment->applied_rate = 1.0;
317   segment->base = base;
318   /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
319   segment->flags = GST_SEGMENT_FLAG_NONE;
320   if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
321     segment->flags |= GST_SEGMENT_FLAG_RESET;
322   if ((flags & GST_SEEK_FLAG_SKIP) != 0)
323     segment->flags |= GST_SEGMENT_FLAG_SKIP;
324   if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
325     segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
326   segment->start = start;
327   segment->stop = stop;
328   segment->time = start;
329
330   if (update_start && rate > 0.0) {
331     position = start;
332   }
333   if (update_stop && rate < 0.0) {
334     if (stop != -1)
335       position = stop;
336     else {
337       if (segment->duration != -1)
338         position = segment->duration;
339       else
340         position = 0;
341     }
342   }
343   /* set update arg to reflect update of position */
344   if (update)
345     *update = position != segment->position;
346
347   /* update new position */
348   segment->position = position;
349
350   return TRUE;
351 }
352
353 /**
354  * gst_segment_to_stream_time:
355  * @segment: a #GstSegment structure.
356  * @format: the format of the segment.
357  * @position: the position in the segment
358  *
359  * Translate @position to stream time using the currently configured
360  * segment. The @position value must be between @segment start and
361  * stop value.
362  *
363  * This function is typically used by elements that need to operate on
364  * the stream time of the buffers it receives, such as effect plugins.
365  * In those use cases, @position is typically the buffer timestamp or
366  * clock time that one wants to convert to the stream time.
367  * The stream time is always between 0 and the total duration of the
368  * media stream.
369  *
370  * Returns: the position in stream_time or -1 when an invalid position
371  * was given.
372  */
373 guint64
374 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
375     guint64 position)
376 {
377   guint64 result, start, stop, time;
378   gdouble abs_applied_rate;
379
380   /* format does not matter for -1 */
381   if (G_UNLIKELY (position == -1))
382     return -1;
383
384   g_return_val_if_fail (segment != NULL, -1);
385   g_return_val_if_fail (segment->format == format, -1);
386
387   /* if we have the position for the same format as the segment, we can compare
388    * the start and stop values, otherwise we assume 0 and -1 */
389   if (G_LIKELY (segment->format == format)) {
390     start = segment->start;
391     stop = segment->stop;
392     time = segment->time;
393   } else {
394     start = 0;
395     stop = -1;
396     time = 0;
397   }
398
399   /* outside of the segment boundary stop */
400   if (G_UNLIKELY (stop != -1 && position > stop))
401     return -1;
402
403   /* before the segment boundary */
404   if (G_UNLIKELY (position < start))
405     return -1;
406
407   /* time must be known */
408   if (G_UNLIKELY (time == -1))
409     return -1;
410
411   /* bring to uncorrected position in segment */
412   result = position - start;
413
414   abs_applied_rate = ABS (segment->applied_rate);
415
416   /* correct for applied rate if needed */
417   if (G_UNLIKELY (abs_applied_rate != 1.0))
418     result *= abs_applied_rate;
419
420   /* add or subtract from segment time based on applied rate */
421   if (G_LIKELY (segment->applied_rate > 0.0)) {
422     /* correct for segment time */
423     result += time;
424   } else {
425     /* correct for segment time, clamp at 0. Streams with a negative
426      * applied_rate have timestamps between start and stop, as usual, but have
427      * the time member starting high and going backwards.  */
428     if (G_LIKELY (time > result))
429       result = time - result;
430     else
431       result = 0;
432   }
433
434   return result;
435 }
436
437 /**
438  * gst_segment_to_running_time:
439  * @segment: a #GstSegment structure.
440  * @format: the format of the segment.
441  * @position: the position in the segment
442  *
443  * Translate @position to the total running time using the currently configured
444  * and previously accumulated segments. Position is a value between @segment
445  * start and stop time.
446  *
447  * This function is typically used by elements that need to synchronize to the
448  * global clock in a pipeline. The runnning time is a constantly increasing value
449  * starting from 0. When gst_segment_init() is called, this value will reset to
450  * 0.
451  *
452  * This function returns -1 if the position is outside of @segment start and stop.
453  *
454  * Returns: the position as the total running time or -1 when an invalid position
455  * was given.
456  */
457 guint64
458 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
459     guint64 position)
460 {
461   guint64 result;
462   guint64 start, stop, base;
463   gdouble abs_rate;
464
465   if (G_UNLIKELY (position == -1))
466     return -1;
467
468   g_return_val_if_fail (segment != NULL, -1);
469   g_return_val_if_fail (segment->format == format, -1);
470
471   /* if we have the position for the same format as the segment, we can compare
472    * the start and stop values, otherwise we assume 0 and -1 */
473   if (G_LIKELY (segment->format == format)) {
474     start = segment->start;
475     stop = segment->stop;
476     base = segment->base;
477   } else {
478     start = 0;
479     stop = -1;
480     base = 0;
481   }
482
483   /* before the segment boundary */
484   if (G_UNLIKELY (position < start))
485     return -1;
486
487   if (G_LIKELY (segment->rate > 0.0)) {
488     /* outside of the segment boundary stop */
489     if (G_UNLIKELY (stop != -1 && position > stop))
490       return -1;
491
492     /* bring to uncorrected position in segment */
493     result = position - start;
494   } else {
495     /* cannot continue if no stop position set or outside of
496      * the segment. */
497     if (G_UNLIKELY (stop == -1 || position > stop))
498       return -1;
499
500     /* bring to uncorrected position in segment */
501     result = stop - position;
502   }
503
504   /* scale based on the rate, avoid division by and conversion to
505    * float when not needed */
506   abs_rate = ABS (segment->rate);
507   if (G_UNLIKELY (abs_rate != 1.0))
508     result /= abs_rate;
509
510   /* correct for base of the segment */
511   result += base;
512
513   return result;
514 }
515
516 /**
517  * gst_segment_clip:
518  * @segment: a #GstSegment structure.
519  * @format: the format of the segment.
520  * @start: the start position in the segment
521  * @stop: the stop position in the segment
522  * @clip_start: (out) (allow-none): the clipped start position in the segment
523  * @clip_stop: (out) (allow-none): the clipped stop position in the segment
524  *
525  * Clip the given @start and @stop values to the segment boundaries given
526  * in @segment. @start and @stop are compared and clipped to @segment
527  * start and stop values.
528  *
529  * If the function returns FALSE, @start and @stop are known to fall
530  * outside of @segment and @clip_start and @clip_stop are not updated.
531  *
532  * When the function returns TRUE, @clip_start and @clip_stop will be
533  * updated. If @clip_start or @clip_stop are different from @start or @stop
534  * respectively, the region fell partially in the segment.
535  *
536  * Note that when @stop is -1, @clip_stop will be set to the end of the
537  * segment. Depending on the use case, this may or may not be what you want.
538  *
539  * Returns: TRUE if the given @start and @stop times fall partially or
540  *     completely in @segment, FALSE if the values are completely outside
541  *     of the segment.
542  */
543 gboolean
544 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
545     guint64 stop, guint64 * clip_start, guint64 * clip_stop)
546 {
547   g_return_val_if_fail (segment != NULL, FALSE);
548   g_return_val_if_fail (segment->format == format, FALSE);
549
550   /* if we have a stop position and a valid start and start is bigger,
551    * we're outside of the segment */
552   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
553     return FALSE;
554
555   /* if a stop position is given and is before the segment start,
556    * we're outside of the segment. Special case is were start
557    * and stop are equal to the segment start. In that case we
558    * are inside the segment. */
559   if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
560                   && stop == segment->start))))
561     return FALSE;
562
563   if (clip_start) {
564     if (start == -1)
565       *clip_start = -1;
566     else
567       *clip_start = MAX (start, segment->start);
568   }
569
570   if (clip_stop) {
571     if (stop == -1)
572       *clip_stop = segment->stop;
573     else if (segment->stop == -1)
574       *clip_stop = stop;
575     else
576       *clip_stop = MIN (stop, segment->stop);
577   }
578
579   return TRUE;
580 }
581
582 /**
583  * gst_segment_to_position:
584  * @segment: a #GstSegment structure.
585  * @format: the format of the segment.
586  * @running_time: the running_time in the segment
587  *
588  * Convert @running_time into a position in the segment so that
589  * gst_segment_to_running_time() with that position returns @running_time.
590  *
591  * Returns: the position in the segment for @running_time. This function returns
592  * -1 when @running_time is -1 or when it is not inside @segment.
593  */
594 guint64
595 gst_segment_to_position (const GstSegment * segment, GstFormat format,
596     guint64 running_time)
597 {
598   guint64 result;
599   guint64 start, stop, base;
600   gdouble abs_rate;
601
602   if (G_UNLIKELY (running_time == -1))
603     return -1;
604
605   g_return_val_if_fail (segment != NULL, -1);
606   g_return_val_if_fail (segment->format == format, FALSE);
607
608   /* if we have the position for the same format as the segment, we can compare
609    * the start and stop values, otherwise we assume 0 and -1 */
610   if (G_LIKELY (segment->format == format)) {
611     start = segment->start;
612     stop = segment->stop;
613     base = segment->base;
614   } else {
615     start = 0;
616     stop = -1;
617     base = 0;
618   }
619
620   /* this running_time was for a previous segment */
621   if (running_time < base)
622     return -1;
623
624   /* start by subtracting the base time */
625   result = running_time - base;
626
627   /* move into the segment at the right rate */
628   abs_rate = ABS (segment->rate);
629   if (G_UNLIKELY (abs_rate != 1.0))
630     result = ceil (result * abs_rate);
631
632   if (G_LIKELY (segment->rate > 0.0)) {
633     /* bring to corrected position in segment */
634     result += start;
635
636     /* outside of the segment boundary stop */
637     if (G_UNLIKELY (stop != -1 && result > stop))
638       return -1;
639   } else {
640     /* cannot continue if no stop position set or outside of
641      * the segment. */
642     if (G_UNLIKELY (stop == -1 || result + start > stop))
643       return -1;
644
645     /* bring to corrected position in segment */
646     result = stop - result;
647   }
648   return result;
649 }
650
651
652 /**
653  * gst_segment_set_running_time:
654  * @segment: a #GstSegment structure.
655  * @format: the format of the segment.
656  * @running_time: the running_time in the segment
657  *
658  * Adjust the start/stop and base values of @segment such that the next valid
659  * buffer will be one with @running_time.
660  *
661  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
662  * returned, @running_time is -1 or not in @segment.
663  */
664 gboolean
665 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
666     guint64 running_time)
667 {
668   guint64 position;
669   guint64 start, stop;
670
671   /* start by bringing the running_time into the segment position */
672   position = gst_segment_to_position (segment, format, running_time);
673
674   /* we must have a valid position now */
675   if (G_UNLIKELY (position == -1))
676     return FALSE;
677
678   start = segment->start;
679   stop = segment->stop;
680
681   if (G_LIKELY (segment->rate > 0.0)) {
682     /* update the start and time values */
683     start = position;
684   } else {
685     /* reverse, update stop */
686     stop = position;
687   }
688   /* and base time is exactly the running time */
689   segment->time = gst_segment_to_stream_time (segment, format, start);
690   segment->start = start;
691   segment->stop = stop;
692   segment->base = running_time;
693
694   return TRUE;
695 }