2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
4 * gstsegment.c: GstSegment subsystem
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.
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.
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.
22 #include "gst_private.h"
27 #include "gstsegment.h"
31 * @short_description: Structure describing the configured region of interest
33 * @see_also: #GstEvent
35 * This helper structure holds the relevant values for tracking the region of
36 * interest in a media file, called a segment.
38 * The structure can be used for two purposes:
40 * <listitem><para>performing seeks (handling seek events)</para></listitem>
41 * <listitem><para>tracking playback regions (handling newsegment events)</para></listitem>
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.
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.
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.
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.
61 * The current position in the segment should be set with the gst_segment_set_last_stop().
62 * The public last_stop field contains the last set stop position in the segment.
64 * For elements that perform seeks, the current segment should be updated with the
65 * gst_segment_set_seek() and the values from the seek event. This method will update
66 * all the segment fields. The last_stop field will contain the new playback position.
67 * If the cur_type was different from GST_SEEK_TYPE_NONE, playback continues from
68 * the last_stop position, possibly with updated flags or rate.
70 * For elements that want to use #GstSegment to track the playback region, use
71 * gst_segment_set_newsegment() to update the segment fields with the information from
72 * the newsegment event. The gst_segment_clip() method can be used to check and clip
73 * the media data to the segment boundaries.
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 all accumulated segments as well as
78 * any rate or applied_rate conversions.
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).
84 * Last reviewed on 2007-05-17 (0.10.13)
89 * @segment: (transfer none): a #GstSegment
91 * Create a copy of given @segment.
93 * Free-function: gst_segment_free
95 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
100 gst_segment_copy (GstSegment * segment)
102 GstSegment *result = NULL;
105 result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
111 gst_segment_get_type (void)
113 static GType gst_segment_type = 0;
115 if (G_UNLIKELY (gst_segment_type == 0)) {
116 gst_segment_type = g_boxed_type_register_static ("GstSegment",
117 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
120 return gst_segment_type;
126 * Allocate a new #GstSegment structure and initialize it using
127 * gst_segment_init().
129 * Free-function: gst_segment_free
131 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
134 gst_segment_new (void)
138 result = g_slice_new0 (GstSegment);
139 gst_segment_init (result, GST_FORMAT_UNDEFINED);
146 * @segment: (in) (transfer full): a #GstSegment
148 * Free the allocated segment @segment.
151 gst_segment_free (GstSegment * segment)
153 g_slice_free (GstSegment, segment);
158 * @segment: a #GstSegment structure.
159 * @format: the format of the segment.
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
165 * Initialize @segment to its default values.
168 gst_segment_init (GstSegment * segment, GstFormat format)
170 g_return_if_fail (segment != NULL);
173 segment->applied_rate = 1.0;
174 segment->format = format;
180 segment->last_stop = 0;
181 segment->duration = -1;
185 * gst_segment_set_duration:
186 * @segment: a #GstSegment structure.
187 * @format: the format of the segment.
188 * @duration: the duration of the segment info or -1 if unknown.
190 * Set the duration of the segment to @duration. This function is mainly
191 * used by elements that perform seeking and know the total duration of the
194 * This field should be set to allow seeking requests relative to the
198 gst_segment_set_duration (GstSegment * segment, GstFormat format,
201 g_return_if_fail (segment != NULL);
203 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
204 segment->format = format;
206 g_return_if_fail (segment->format == format);
208 segment->duration = duration;
212 * gst_segment_set_last_stop:
213 * @segment: a #GstSegment structure.
214 * @format: the format of the segment.
215 * @position: the position
217 * Set the last observed stop position in the segment to @position.
219 * This field should be set to allow seeking requests relative to the
220 * current playing position.
223 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
226 g_return_if_fail (segment != NULL);
228 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
229 segment->format = format;
231 g_return_if_fail (segment->format == format);
233 segment->last_stop = MAX (segment->start, position);
237 * gst_segment_set_seek:
238 * @segment: a #GstSegment structure.
239 * @rate: the rate of the segment.
240 * @format: the format of the segment.
241 * @flags: the seek flags for the segment
242 * @start_type: the seek method
243 * @start: the seek start value
244 * @stop_type: the seek method
245 * @stop: the seek stop value
246 * @update: boolean holding whether last_stop was updated.
248 * Update the segment structure with the field values of a seek event (see
249 * gst_event_new_seek()).
251 * After calling this method, the segment field last_stop and time will
252 * contain the requested new position in the segment. The new requested
253 * position in the segment depends on @rate and @start_type and @stop_type.
255 * For positive @rate, the new position in the segment is the new @segment
256 * start field when it was updated with a @start_type different from
257 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
258 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment last_stop is
261 * For negative @rate, the new position in the segment is the new @segment
262 * stop field when it was updated with a @stop_type different from
263 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
264 * duration of the segment will be used to update the stop position.
265 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
266 * @stop is ignored and @segment last_stop is unmodified.
268 * The applied rate of the segment will be set to 1.0 by default.
269 * If the caller can apply a rate change, it should update @segment
270 * rate and applied_rate after calling this function.
272 * @update will be set to TRUE if a seek should be performed to the segment
273 * last_stop field. This field can be FALSE if, for example, only the @rate
274 * has been changed but not the playback position.
277 gst_segment_set_seek (GstSegment * segment, gdouble rate,
278 GstFormat format, GstSeekFlags flags,
279 GstSeekType start_type, gint64 start,
280 GstSeekType stop_type, gint64 stop, gboolean * update)
282 gboolean update_stop, update_start;
285 g_return_if_fail (rate != 0.0);
286 g_return_if_fail (segment != NULL);
288 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
289 segment->format = format;
291 update_start = update_stop = TRUE;
293 /* segment->start is never invalid */
294 switch (start_type) {
295 case GST_SEEK_TYPE_NONE:
296 /* no update to segment, take previous start */
297 start = segment->start;
298 update_start = FALSE;
300 case GST_SEEK_TYPE_SET:
301 /* start holds desired position, map -1 to the start */
304 /* start must be 0 or the formats must match */
305 g_return_if_fail (start == 0 || segment->format == format);
307 case GST_SEEK_TYPE_CUR:
308 g_return_if_fail (start == 0 || segment->format == format);
309 /* add start to currently configured segment */
310 start = segment->start + start;
312 case GST_SEEK_TYPE_END:
313 if (segment->duration != -1) {
314 g_return_if_fail (start == 0 || segment->format == format);
315 /* add start to total length */
316 start = segment->duration + start;
318 /* no update if duration unknown */
319 start = segment->start;
320 update_start = FALSE;
324 /* bring in sane range */
325 if (segment->duration != -1)
326 start = CLAMP (start, 0, segment->duration);
328 start = MAX (start, 0);
330 /* stop can be -1 if we have not configured a stop. */
332 case GST_SEEK_TYPE_NONE:
333 stop = segment->stop;
336 case GST_SEEK_TYPE_SET:
337 /* stop holds required value, if it's not -1, it must be of the same
338 * format as the segment. */
339 g_return_if_fail (stop == -1 || segment->format == format);
341 case GST_SEEK_TYPE_CUR:
342 if (segment->stop != -1) {
343 /* only add compatible formats or 0 */
344 g_return_if_fail (stop == 0 || segment->format == format);
345 stop = segment->stop + stop;
349 case GST_SEEK_TYPE_END:
350 if (segment->duration != -1) {
351 /* only add compatible formats or 0 */
352 g_return_if_fail (stop == 0 || segment->format == format);
353 stop = segment->duration + stop;
355 stop = segment->stop;
361 /* if we have a valid stop time, make sure it is clipped */
363 if (segment->duration != -1)
364 stop = CLAMP (stop, 0, segment->duration);
366 stop = MAX (stop, 0);
369 /* we can't have stop before start */
371 g_return_if_fail (start <= stop);
373 segment->rate = rate;
374 segment->applied_rate = 1.0;
375 segment->flags = flags;
376 segment->start = start;
377 segment->stop = stop;
378 segment->time = start;
380 last_stop = segment->last_stop;
381 if (update_start && rate > 0.0) {
384 if (update_stop && rate < 0.0) {
388 if (segment->duration != -1)
389 last_stop = segment->duration;
394 /* set update arg to reflect update of last_stop */
396 *update = last_stop != segment->last_stop;
398 /* update new position */
399 segment->last_stop = last_stop;
403 * gst_segment_set_newsegment:
404 * @segment: a #GstSegment structure.
405 * @update: flag indicating a new segment is started or updated
406 * @rate: the rate of the segment.
407 * @applied_rate: the applied rate of the segment.
408 * @format: the format of the segment.
409 * @start: the new start value
410 * @stop: the new stop value
411 * @time: the new stream time
413 * Update the segment structure with the field values of a new segment event.
416 gst_segment_set_newsegment (GstSegment * segment, gboolean update,
417 gdouble rate, gdouble applied_rate, GstFormat format, gint64 start,
418 gint64 stop, gint64 time)
420 gint64 duration, last_stop;
423 g_return_if_fail (rate != 0.0);
424 g_return_if_fail (applied_rate != 0.0);
425 g_return_if_fail (segment != NULL);
427 GST_DEBUG ("configuring segment update %d, rate %lf, format %s, "
428 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT ", position %"
429 G_GINT64_FORMAT, update, rate, gst_format_get_name (format), start,
431 GST_DEBUG ("old segment was: %" GST_SEGMENT_FORMAT, segment);
433 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
434 segment->format = format;
436 /* any other format with 0 also gives time 0, the other values are
437 * invalid in the format though. */
438 if (format != segment->format && start == 0) {
439 format = segment->format;
446 g_return_if_fail (segment->format == format);
449 if (G_LIKELY (segment->rate > 0.0)) {
450 /* an update to the current segment is done, elapsed time is
451 * difference between the old start and new start. */
452 if (start > segment->start)
453 duration = start - segment->start;
457 /* for negative rates, the elapsed duration is the diff between the stop
459 if (stop != -1 && stop < segment->stop)
460 duration = segment->stop - stop;
464 /* update last_stop to be a valid value in the updated segment */
465 if (start > segment->last_stop)
467 else if (stop != -1 && stop < segment->last_stop)
470 last_stop = segment->last_stop;
472 /* the new segment has to be aligned with the old segment.
473 * We first update the accumulated time of the previous
474 * segment. the accumulated time is used when syncing to the
476 if (segment->stop != -1) {
477 duration = segment->stop - segment->start;
478 } else if (segment->last_stop != -1) {
479 /* else use last seen timestamp as segment stop */
480 duration = segment->last_stop - segment->start;
482 /* else we don't know and throw a warning.. really, this should
483 * be fixed in the element. */
484 g_warning ("closing segment of unknown duration, assuming duration of 0");
487 /* position the last_stop to the next expected position in the new segment,
488 * which is the start or the stop of the segment */
494 /* use previous rate to calculate duration */
495 abs_rate = ABS (segment->rate);
496 if (G_LIKELY (abs_rate != 1.0))
497 duration /= abs_rate;
499 /* accumulate duration */
500 segment->accum += duration;
502 /* then update the current segment */
503 segment->rate = rate;
504 segment->applied_rate = applied_rate;
505 segment->start = start;
506 segment->last_stop = last_stop;
507 segment->stop = stop;
508 segment->time = time;
512 * gst_segment_to_stream_time:
513 * @segment: a #GstSegment structure.
514 * @format: the format of the segment.
515 * @position: the position in the segment
517 * Translate @position to stream time using the currently configured
518 * segment. The @position value must be between @segment start and
521 * This function is typically used by elements that need to operate on
522 * the stream time of the buffers it receives, such as effect plugins.
523 * In those use cases, @position is typically the buffer timestamp or
524 * clock time that one wants to convert to the stream time.
525 * The stream time is always between 0 and the total duration of the
528 * Returns: the position in stream_time or -1 when an invalid position
532 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
535 gint64 result, start, stop, time;
536 gdouble abs_applied_rate;
538 /* format does not matter for -1 */
539 if (G_UNLIKELY (position == -1))
542 g_return_val_if_fail (segment != NULL, -1);
544 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
545 segment->format = format;
547 /* if we have the position for the same format as the segment, we can compare
548 * the start and stop values, otherwise we assume 0 and -1 */
549 if (G_LIKELY (segment->format == format)) {
550 start = segment->start;
551 stop = segment->stop;
552 time = segment->time;
559 /* outside of the segment boundary stop */
560 if (G_UNLIKELY (stop != -1 && position > stop))
563 /* before the segment boundary */
564 if (G_UNLIKELY (position < start))
567 /* time must be known */
568 if (G_UNLIKELY (time == -1))
571 /* bring to uncorrected position in segment */
572 result = position - start;
574 abs_applied_rate = ABS (segment->applied_rate);
576 /* correct for applied rate if needed */
577 if (G_UNLIKELY (abs_applied_rate != 1.0))
578 result *= abs_applied_rate;
580 /* add or subtract from segment time based on applied rate */
581 if (G_LIKELY (segment->applied_rate > 0.0)) {
582 /* correct for segment time */
585 /* correct for segment time, clamp at 0. Streams with a negative
586 * applied_rate have timestamps between start and stop, as usual, but have
587 * the time member starting high and going backwards. */
588 if (G_LIKELY (time > result))
589 result = time - result;
598 * gst_segment_to_running_time:
599 * @segment: a #GstSegment structure.
600 * @format: the format of the segment.
601 * @position: the position in the segment
603 * Translate @position to the total running time using the currently configured
604 * and previously accumulated segments. Position is a value between @segment
605 * start and stop time.
607 * This function is typically used by elements that need to synchronize to the
608 * global clock in a pipeline. The runnning time is a constantly increasing value
609 * starting from 0. When gst_segment_init() is called, this value will reset to
612 * This function returns -1 if the position is outside of @segment start and stop.
614 * Returns: the position as the total running time or -1 when an invalid position
618 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
622 gint64 start, stop, accum;
625 if (G_UNLIKELY (position == -1))
628 g_return_val_if_fail (segment != NULL, -1);
630 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
631 segment->format = format;
633 /* if we have the position for the same format as the segment, we can compare
634 * the start and stop values, otherwise we assume 0 and -1 */
635 if (G_LIKELY (segment->format == format)) {
636 start = segment->start;
637 stop = segment->stop;
638 accum = segment->accum;
645 /* before the segment boundary */
646 if (G_UNLIKELY (position < start))
649 if (G_LIKELY (segment->rate > 0.0)) {
650 /* outside of the segment boundary stop */
651 if (G_UNLIKELY (stop != -1 && position > stop))
654 /* bring to uncorrected position in segment */
655 result = position - start;
657 /* cannot continue if no stop position set or outside of
659 if (G_UNLIKELY (stop == -1 || position > stop))
662 /* bring to uncorrected position in segment */
663 result = stop - position;
666 /* scale based on the rate, avoid division by and conversion to
667 * float when not needed */
668 abs_rate = ABS (segment->rate);
669 if (G_UNLIKELY (abs_rate != 1.0))
672 /* correct for accumulated segments */
680 * @segment: a #GstSegment structure.
681 * @format: the format of the segment.
682 * @start: the start position in the segment
683 * @stop: the stop position in the segment
684 * @clip_start: (out) (allow-none): the clipped start position in the segment
685 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
687 * Clip the given @start and @stop values to the segment boundaries given
688 * in @segment. @start and @stop are compared and clipped to @segment
689 * start and stop values.
691 * If the function returns FALSE, @start and @stop are known to fall
692 * outside of @segment and @clip_start and @clip_stop are not updated.
694 * When the function returns TRUE, @clip_start and @clip_stop will be
695 * updated. If @clip_start or @clip_stop are different from @start or @stop
696 * respectively, the region fell partially in the segment.
698 * Note that when @stop is -1, @clip_stop will be set to the end of the
699 * segment. Depending on the use case, this may or may not be what you want.
701 * Returns: TRUE if the given @start and @stop times fall partially or
702 * completely in @segment, FALSE if the values are completely outside
706 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
707 gint64 stop, gint64 * clip_start, gint64 * clip_stop)
709 g_return_val_if_fail (segment != NULL, FALSE);
711 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
712 segment->format = format;
714 g_return_val_if_fail (segment->format == format, FALSE);
716 /* if we have a stop position and a valid start and start is bigger,
717 * we're outside of the segment */
718 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
721 /* if a stop position is given and is before the segment start,
722 * we're outside of the segment. Special case is were start
723 * and stop are equal to the segment start. In that case we
724 * are inside the segment. */
725 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
726 && stop == segment->start))))
733 *clip_start = MAX (start, segment->start);
738 *clip_stop = segment->stop;
739 else if (segment->stop == -1)
740 *clip_stop = MAX (-1, stop);
742 *clip_stop = MIN (stop, segment->stop);
744 if (segment->duration != -1)
745 *clip_stop = MIN (*clip_stop, segment->duration);
752 * gst_segment_to_position:
753 * @segment: a #GstSegment structure.
754 * @format: the format of the segment.
755 * @running_time: the running_time in the segment
757 * Convert @running_time into a position in the segment so that
758 * gst_segment_to_running_time() with that position returns @running_time.
760 * Returns: the position in the segment for @running_time. This function returns
761 * -1 when @running_time is -1 or when it is not inside @segment.
766 gst_segment_to_position (GstSegment * segment, GstFormat format,
770 gint64 start, stop, accum;
773 g_return_val_if_fail (segment != NULL, -1);
775 if (G_UNLIKELY (running_time == -1))
778 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
779 segment->format = format;
781 /* if we have the position for the same format as the segment, we can compare
782 * the start and stop values, otherwise we assume 0 and -1 */
783 if (G_LIKELY (segment->format == format)) {
784 start = segment->start;
785 stop = segment->stop;
786 accum = segment->accum;
793 /* this running_time was for a previous segment */
794 if (running_time < accum)
797 /* start by subtracting the accumulated time */
798 result = running_time - accum;
800 /* move into the segment at the right rate */
801 abs_rate = ABS (segment->rate);
802 if (G_UNLIKELY (abs_rate != 1.0))
803 result = ceil (result * abs_rate);
805 if (G_LIKELY (segment->rate > 0.0)) {
806 /* bring to corrected position in segment */
809 /* outside of the segment boundary stop */
810 if (G_UNLIKELY (stop != -1 && result > stop))
813 /* cannot continue if no stop position set or outside of
815 if (G_UNLIKELY (stop == -1 || result + start > stop))
818 /* bring to corrected position in segment */
819 result = stop - result;
826 * gst_segment_set_running_time:
827 * @segment: a #GstSegment structure.
828 * @format: the format of the segment.
829 * @running_time: the running_time in the segment
831 * Adjust the start/stop and accum values of @segment such that the next valid
832 * buffer will be one with @running_time.
834 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
835 * returned, @running_time is -1 or not in @segment.
840 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
844 gint64 start, stop, last_stop;
846 /* start by bringing the running_time into the segment position */
847 position = gst_segment_to_position (segment, format, running_time);
849 /* we must have a valid position now */
850 if (G_UNLIKELY (position == -1))
853 start = segment->start;
854 stop = segment->stop;
855 last_stop = segment->last_stop;
857 if (G_LIKELY (segment->rate > 0.0)) {
858 /* update the start/last_stop and time values */
860 if (last_stop < start)
863 /* reverse, update stop */
865 /* if we were past the position, go back */
866 if (last_stop > stop)
869 /* and accumulated time is exactly the running time */
870 segment->time = gst_segment_to_stream_time (segment, format, start);
871 segment->start = start;
872 segment->stop = stop;
873 segment->last_stop = last_stop;
874 segment->accum = running_time;