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 * @format: the format of the segment.
408 * @start: the new start value
409 * @stop: the new stop value
410 * @time: the new stream time
412 * Update the segment structure with the field values of a new segment event and
413 * with a default applied_rate of 1.0.
418 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
419 GstFormat format, gint64 start, gint64 stop, gint64 time)
421 gst_segment_set_newsegment_full (segment, update, rate, 1.0, format, start,
426 * gst_segment_set_newsegment_full:
427 * @segment: a #GstSegment structure.
428 * @update: flag indicating a new segment is started or updated
429 * @rate: the rate of the segment.
430 * @applied_rate: the applied rate of the segment.
431 * @format: the format of the segment.
432 * @start: the new start value
433 * @stop: the new stop value
434 * @time: the new stream time
436 * Update the segment structure with the field values of a new segment event.
439 gst_segment_set_newsegment_full (GstSegment * segment, gboolean update,
440 gdouble rate, gdouble applied_rate, GstFormat format, gint64 start,
441 gint64 stop, gint64 time)
443 gint64 duration, last_stop;
446 g_return_if_fail (rate != 0.0);
447 g_return_if_fail (applied_rate != 0.0);
448 g_return_if_fail (segment != NULL);
450 GST_DEBUG ("configuring segment update %d, rate %lf, format %s, "
451 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT ", position %"
452 G_GINT64_FORMAT, update, rate, gst_format_get_name (format), start,
454 GST_DEBUG ("old segment was: %" GST_SEGMENT_FORMAT, segment);
456 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
457 segment->format = format;
459 /* any other format with 0 also gives time 0, the other values are
460 * invalid in the format though. */
461 if (format != segment->format && start == 0) {
462 format = segment->format;
469 g_return_if_fail (segment->format == format);
472 if (G_LIKELY (segment->rate > 0.0)) {
473 /* an update to the current segment is done, elapsed time is
474 * difference between the old start and new start. */
475 if (start > segment->start)
476 duration = start - segment->start;
480 /* for negative rates, the elapsed duration is the diff between the stop
482 if (stop != -1 && stop < segment->stop)
483 duration = segment->stop - stop;
487 /* update last_stop to be a valid value in the updated segment */
488 if (start > segment->last_stop)
490 else if (stop != -1 && stop < segment->last_stop)
493 last_stop = segment->last_stop;
495 /* the new segment has to be aligned with the old segment.
496 * We first update the accumulated time of the previous
497 * segment. the accumulated time is used when syncing to the
499 if (segment->stop != -1) {
500 duration = segment->stop - segment->start;
501 } else if (segment->last_stop != -1) {
502 /* else use last seen timestamp as segment stop */
503 duration = segment->last_stop - segment->start;
505 /* else we don't know and throw a warning.. really, this should
506 * be fixed in the element. */
507 g_warning ("closing segment of unknown duration, assuming duration of 0");
510 /* position the last_stop to the next expected position in the new segment,
511 * which is the start or the stop of the segment */
517 /* use previous rate to calculate duration */
518 abs_rate = ABS (segment->rate);
519 if (G_LIKELY (abs_rate != 1.0))
520 duration /= abs_rate;
522 /* accumulate duration */
523 segment->accum += duration;
525 /* then update the current segment */
526 segment->rate = rate;
527 segment->applied_rate = applied_rate;
528 segment->start = start;
529 segment->last_stop = last_stop;
530 segment->stop = stop;
531 segment->time = time;
535 * gst_segment_to_stream_time:
536 * @segment: a #GstSegment structure.
537 * @format: the format of the segment.
538 * @position: the position in the segment
540 * Translate @position to stream time using the currently configured
541 * segment. The @position value must be between @segment start and
544 * This function is typically used by elements that need to operate on
545 * the stream time of the buffers it receives, such as effect plugins.
546 * In those use cases, @position is typically the buffer timestamp or
547 * clock time that one wants to convert to the stream time.
548 * The stream time is always between 0 and the total duration of the
551 * Returns: the position in stream_time or -1 when an invalid position
555 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
558 gint64 result, start, stop, time;
559 gdouble abs_applied_rate;
561 /* format does not matter for -1 */
562 if (G_UNLIKELY (position == -1))
565 g_return_val_if_fail (segment != NULL, -1);
567 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
568 segment->format = format;
570 /* if we have the position for the same format as the segment, we can compare
571 * the start and stop values, otherwise we assume 0 and -1 */
572 if (G_LIKELY (segment->format == format)) {
573 start = segment->start;
574 stop = segment->stop;
575 time = segment->time;
582 /* outside of the segment boundary stop */
583 if (G_UNLIKELY (stop != -1 && position > stop))
586 /* before the segment boundary */
587 if (G_UNLIKELY (position < start))
590 /* time must be known */
591 if (G_UNLIKELY (time == -1))
594 /* bring to uncorrected position in segment */
595 result = position - start;
597 abs_applied_rate = ABS (segment->applied_rate);
599 /* correct for applied rate if needed */
600 if (G_UNLIKELY (abs_applied_rate != 1.0))
601 result *= abs_applied_rate;
603 /* add or subtract from segment time based on applied rate */
604 if (G_LIKELY (segment->applied_rate > 0.0)) {
605 /* correct for segment time */
608 /* correct for segment time, clamp at 0. Streams with a negative
609 * applied_rate have timestamps between start and stop, as usual, but have
610 * the time member starting high and going backwards. */
611 if (G_LIKELY (time > result))
612 result = time - result;
621 * gst_segment_to_running_time:
622 * @segment: a #GstSegment structure.
623 * @format: the format of the segment.
624 * @position: the position in the segment
626 * Translate @position to the total running time using the currently configured
627 * and previously accumulated segments. Position is a value between @segment
628 * start and stop time.
630 * This function is typically used by elements that need to synchronize to the
631 * global clock in a pipeline. The runnning time is a constantly increasing value
632 * starting from 0. When gst_segment_init() is called, this value will reset to
635 * This function returns -1 if the position is outside of @segment start and stop.
637 * Returns: the position as the total running time or -1 when an invalid position
641 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
645 gint64 start, stop, accum;
648 if (G_UNLIKELY (position == -1))
651 g_return_val_if_fail (segment != NULL, -1);
653 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
654 segment->format = format;
656 /* if we have the position for the same format as the segment, we can compare
657 * the start and stop values, otherwise we assume 0 and -1 */
658 if (G_LIKELY (segment->format == format)) {
659 start = segment->start;
660 stop = segment->stop;
661 accum = segment->accum;
668 /* before the segment boundary */
669 if (G_UNLIKELY (position < start))
672 if (G_LIKELY (segment->rate > 0.0)) {
673 /* outside of the segment boundary stop */
674 if (G_UNLIKELY (stop != -1 && position > stop))
677 /* bring to uncorrected position in segment */
678 result = position - start;
680 /* cannot continue if no stop position set or outside of
682 if (G_UNLIKELY (stop == -1 || position > stop))
685 /* bring to uncorrected position in segment */
686 result = stop - position;
689 /* scale based on the rate, avoid division by and conversion to
690 * float when not needed */
691 abs_rate = ABS (segment->rate);
692 if (G_UNLIKELY (abs_rate != 1.0))
695 /* correct for accumulated segments */
703 * @segment: a #GstSegment structure.
704 * @format: the format of the segment.
705 * @start: the start position in the segment
706 * @stop: the stop position in the segment
707 * @clip_start: (out) (allow-none): the clipped start position in the segment
708 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
710 * Clip the given @start and @stop values to the segment boundaries given
711 * in @segment. @start and @stop are compared and clipped to @segment
712 * start and stop values.
714 * If the function returns FALSE, @start and @stop are known to fall
715 * outside of @segment and @clip_start and @clip_stop are not updated.
717 * When the function returns TRUE, @clip_start and @clip_stop will be
718 * updated. If @clip_start or @clip_stop are different from @start or @stop
719 * respectively, the region fell partially in the segment.
721 * Note that when @stop is -1, @clip_stop will be set to the end of the
722 * segment. Depending on the use case, this may or may not be what you want.
724 * Returns: TRUE if the given @start and @stop times fall partially or
725 * completely in @segment, FALSE if the values are completely outside
729 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
730 gint64 stop, gint64 * clip_start, gint64 * clip_stop)
732 g_return_val_if_fail (segment != NULL, FALSE);
734 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
735 segment->format = format;
737 g_return_val_if_fail (segment->format == format, FALSE);
739 /* if we have a stop position and a valid start and start is bigger,
740 * we're outside of the segment */
741 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
744 /* if a stop position is given and is before the segment start,
745 * we're outside of the segment. Special case is were start
746 * and stop are equal to the segment start. In that case we
747 * are inside the segment. */
748 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
749 && stop == segment->start))))
756 *clip_start = MAX (start, segment->start);
761 *clip_stop = segment->stop;
762 else if (segment->stop == -1)
763 *clip_stop = MAX (-1, stop);
765 *clip_stop = MIN (stop, segment->stop);
767 if (segment->duration != -1)
768 *clip_stop = MIN (*clip_stop, segment->duration);
775 * gst_segment_to_position:
776 * @segment: a #GstSegment structure.
777 * @format: the format of the segment.
778 * @running_time: the running_time in the segment
780 * Convert @running_time into a position in the segment so that
781 * gst_segment_to_running_time() with that position returns @running_time.
783 * Returns: the position in the segment for @running_time. This function returns
784 * -1 when @running_time is -1 or when it is not inside @segment.
789 gst_segment_to_position (GstSegment * segment, GstFormat format,
793 gint64 start, stop, accum;
796 g_return_val_if_fail (segment != NULL, -1);
798 if (G_UNLIKELY (running_time == -1))
801 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
802 segment->format = format;
804 /* if we have the position for the same format as the segment, we can compare
805 * the start and stop values, otherwise we assume 0 and -1 */
806 if (G_LIKELY (segment->format == format)) {
807 start = segment->start;
808 stop = segment->stop;
809 accum = segment->accum;
816 /* this running_time was for a previous segment */
817 if (running_time < accum)
820 /* start by subtracting the accumulated time */
821 result = running_time - accum;
823 /* move into the segment at the right rate */
824 abs_rate = ABS (segment->rate);
825 if (G_UNLIKELY (abs_rate != 1.0))
826 result = ceil (result * abs_rate);
828 if (G_LIKELY (segment->rate > 0.0)) {
829 /* bring to corrected position in segment */
832 /* outside of the segment boundary stop */
833 if (G_UNLIKELY (stop != -1 && result > stop))
836 /* cannot continue if no stop position set or outside of
838 if (G_UNLIKELY (stop == -1 || result + start > stop))
841 /* bring to corrected position in segment */
842 result = stop - result;
849 * gst_segment_set_running_time:
850 * @segment: a #GstSegment structure.
851 * @format: the format of the segment.
852 * @running_time: the running_time in the segment
854 * Adjust the start/stop and accum values of @segment such that the next valid
855 * buffer will be one with @running_time.
857 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
858 * returned, @running_time is -1 or not in @segment.
863 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
867 gint64 start, stop, last_stop;
869 /* start by bringing the running_time into the segment position */
870 position = gst_segment_to_position (segment, format, running_time);
872 /* we must have a valid position now */
873 if (G_UNLIKELY (position == -1))
876 start = segment->start;
877 stop = segment->stop;
878 last_stop = segment->last_stop;
880 if (G_LIKELY (segment->rate > 0.0)) {
881 /* update the start/last_stop and time values */
883 if (last_stop < start)
886 /* reverse, update stop */
888 /* if we were past the position, go back */
889 if (last_stop > stop)
892 /* and accumulated time is exactly the running time */
893 segment->time = gst_segment_to_stream_time (segment, format, start);
894 segment->start = start;
895 segment->stop = stop;
896 segment->last_stop = last_stop;
897 segment->accum = running_time;