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->abs_rate = 1.0;
174 segment->applied_rate = 1.0;
175 segment->format = format;
181 segment->last_stop = 0;
182 segment->duration = -1;
186 * gst_segment_set_duration:
187 * @segment: a #GstSegment structure.
188 * @format: the format of the segment.
189 * @duration: the duration of the segment info or -1 if unknown.
191 * Set the duration of the segment to @duration. This function is mainly
192 * used by elements that perform seeking and know the total duration of the
195 * This field should be set to allow seeking requests relative to the
199 gst_segment_set_duration (GstSegment * segment, GstFormat format,
202 g_return_if_fail (segment != NULL);
204 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
205 segment->format = format;
207 g_return_if_fail (segment->format == format);
209 segment->duration = duration;
213 * gst_segment_set_last_stop:
214 * @segment: a #GstSegment structure.
215 * @format: the format of the segment.
216 * @position: the position
218 * Set the last observed stop position in the segment to @position.
220 * This field should be set to allow seeking requests relative to the
221 * current playing position.
224 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
227 g_return_if_fail (segment != NULL);
229 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
230 segment->format = format;
232 g_return_if_fail (segment->format == format);
234 segment->last_stop = MAX (segment->start, position);
238 * gst_segment_set_seek:
239 * @segment: a #GstSegment structure.
240 * @rate: the rate of the segment.
241 * @format: the format of the segment.
242 * @flags: the seek flags for the segment
243 * @start_type: the seek method
244 * @start: the seek start value
245 * @stop_type: the seek method
246 * @stop: the seek stop value
247 * @update: boolean holding whether last_stop was updated.
249 * Update the segment structure with the field values of a seek event (see
250 * gst_event_new_seek()).
252 * After calling this method, the segment field last_stop and time will
253 * contain the requested new position in the segment. The new requested
254 * position in the segment depends on @rate and @start_type and @stop_type.
256 * For positive @rate, the new position in the segment is the new @segment
257 * start field when it was updated with a @start_type different from
258 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
259 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment last_stop is
262 * For negative @rate, the new position in the segment is the new @segment
263 * stop field when it was updated with a @stop_type different from
264 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
265 * duration of the segment will be used to update the stop position.
266 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
267 * @stop is ignored and @segment last_stop is unmodified.
269 * The applied rate of the segment will be set to 1.0 by default.
270 * If the caller can apply a rate change, it should update @segment
271 * rate and applied_rate after calling this function.
273 * @update will be set to TRUE if a seek should be performed to the segment
274 * last_stop field. This field can be FALSE if, for example, only the @rate
275 * has been changed but not the playback position.
278 gst_segment_set_seek (GstSegment * segment, gdouble rate,
279 GstFormat format, GstSeekFlags flags,
280 GstSeekType start_type, gint64 start,
281 GstSeekType stop_type, gint64 stop, gboolean * update)
283 gboolean update_stop, update_start;
286 g_return_if_fail (rate != 0.0);
287 g_return_if_fail (segment != NULL);
289 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
290 segment->format = format;
292 update_start = update_stop = TRUE;
294 /* segment->start is never invalid */
295 switch (start_type) {
296 case GST_SEEK_TYPE_NONE:
297 /* no update to segment, take previous start */
298 start = segment->start;
299 update_start = FALSE;
301 case GST_SEEK_TYPE_SET:
302 /* start holds desired position, map -1 to the start */
305 /* start must be 0 or the formats must match */
306 g_return_if_fail (start == 0 || segment->format == format);
308 case GST_SEEK_TYPE_CUR:
309 g_return_if_fail (start == 0 || segment->format == format);
310 /* add start to currently configured segment */
311 start = segment->start + start;
313 case GST_SEEK_TYPE_END:
314 if (segment->duration != -1) {
315 g_return_if_fail (start == 0 || segment->format == format);
316 /* add start to total length */
317 start = segment->duration + start;
319 /* no update if duration unknown */
320 start = segment->start;
321 update_start = FALSE;
325 /* bring in sane range */
326 if (segment->duration != -1)
327 start = CLAMP (start, 0, segment->duration);
329 start = MAX (start, 0);
331 /* stop can be -1 if we have not configured a stop. */
333 case GST_SEEK_TYPE_NONE:
334 stop = segment->stop;
337 case GST_SEEK_TYPE_SET:
338 /* stop holds required value, if it's not -1, it must be of the same
339 * format as the segment. */
340 g_return_if_fail (stop == -1 || segment->format == format);
342 case GST_SEEK_TYPE_CUR:
343 if (segment->stop != -1) {
344 /* only add compatible formats or 0 */
345 g_return_if_fail (stop == 0 || segment->format == format);
346 stop = segment->stop + stop;
350 case GST_SEEK_TYPE_END:
351 if (segment->duration != -1) {
352 /* only add compatible formats or 0 */
353 g_return_if_fail (stop == 0 || segment->format == format);
354 stop = segment->duration + stop;
356 stop = segment->stop;
362 /* if we have a valid stop time, make sure it is clipped */
364 if (segment->duration != -1)
365 stop = CLAMP (stop, 0, segment->duration);
367 stop = MAX (stop, 0);
370 /* we can't have stop before start */
372 g_return_if_fail (start <= stop);
374 segment->rate = rate;
375 segment->abs_rate = ABS (rate);
376 segment->applied_rate = 1.0;
377 segment->flags = flags;
378 segment->start = start;
379 segment->stop = stop;
380 segment->time = start;
382 last_stop = segment->last_stop;
383 if (update_start && rate > 0.0) {
386 if (update_stop && rate < 0.0) {
390 if (segment->duration != -1)
391 last_stop = segment->duration;
396 /* set update arg to reflect update of last_stop */
398 *update = last_stop != segment->last_stop;
400 /* update new position */
401 segment->last_stop = last_stop;
405 * gst_segment_set_newsegment:
406 * @segment: a #GstSegment structure.
407 * @update: flag indicating a new segment is started or updated
408 * @rate: the rate of the segment.
409 * @format: the format of the segment.
410 * @start: the new start value
411 * @stop: the new stop value
412 * @time: the new stream time
414 * Update the segment structure with the field values of a new segment event and
415 * with a default applied_rate of 1.0.
420 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
421 GstFormat format, gint64 start, gint64 stop, gint64 time)
423 gst_segment_set_newsegment_full (segment, update, rate, 1.0, format, start,
428 * gst_segment_set_newsegment_full:
429 * @segment: a #GstSegment structure.
430 * @update: flag indicating a new segment is started or updated
431 * @rate: the rate of the segment.
432 * @applied_rate: the applied rate of the segment.
433 * @format: the format of the segment.
434 * @start: the new start value
435 * @stop: the new stop value
436 * @time: the new stream time
438 * Update the segment structure with the field values of a new segment event.
441 gst_segment_set_newsegment_full (GstSegment * segment, gboolean update,
442 gdouble rate, gdouble applied_rate, GstFormat format, gint64 start,
443 gint64 stop, gint64 time)
445 gint64 duration, last_stop;
447 g_return_if_fail (rate != 0.0);
448 g_return_if_fail (applied_rate != 0.0);
449 g_return_if_fail (segment != NULL);
451 GST_DEBUG ("configuring segment update %d, rate %lf, format %s, "
452 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT ", position %"
453 G_GINT64_FORMAT, update, rate, gst_format_get_name (format), start,
455 GST_DEBUG ("old segment was: %" GST_SEGMENT_FORMAT, segment);
457 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
458 segment->format = format;
460 /* any other format with 0 also gives time 0, the other values are
461 * invalid in the format though. */
462 if (format != segment->format && start == 0) {
463 format = segment->format;
470 g_return_if_fail (segment->format == format);
473 if (G_LIKELY (segment->rate > 0.0)) {
474 /* an update to the current segment is done, elapsed time is
475 * difference between the old start and new start. */
476 if (start > segment->start)
477 duration = start - segment->start;
481 /* for negative rates, the elapsed duration is the diff between the stop
483 if (stop != -1 && stop < segment->stop)
484 duration = segment->stop - stop;
488 /* update last_stop to be a valid value in the updated segment */
489 if (start > segment->last_stop)
491 else if (stop != -1 && stop < segment->last_stop)
494 last_stop = segment->last_stop;
496 /* the new segment has to be aligned with the old segment.
497 * We first update the accumulated time of the previous
498 * segment. the accumulated time is used when syncing to the
500 if (segment->stop != -1) {
501 duration = segment->stop - segment->start;
502 } else if (segment->last_stop != -1) {
503 /* else use last seen timestamp as segment stop */
504 duration = segment->last_stop - segment->start;
506 /* else we don't know and throw a warning.. really, this should
507 * be fixed in the element. */
508 g_warning ("closing segment of unknown duration, assuming duration of 0");
511 /* position the last_stop to the next expected position in the new segment,
512 * which is the start or the stop of the segment */
518 /* use previous rate to calculate duration */
519 if (G_LIKELY (segment->abs_rate != 1.0))
520 duration /= segment->abs_rate;
522 /* accumulate duration */
523 segment->accum += duration;
525 /* then update the current segment */
526 segment->rate = rate;
527 segment->abs_rate = ABS (rate);
528 segment->applied_rate = applied_rate;
529 segment->start = start;
530 segment->last_stop = last_stop;
531 segment->stop = stop;
532 segment->time = time;
536 * gst_segment_to_stream_time:
537 * @segment: a #GstSegment structure.
538 * @format: the format of the segment.
539 * @position: the position in the segment
541 * Translate @position to stream time using the currently configured
542 * segment. The @position value must be between @segment start and
545 * This function is typically used by elements that need to operate on
546 * the stream time of the buffers it receives, such as effect plugins.
547 * In those use cases, @position is typically the buffer timestamp or
548 * clock time that one wants to convert to the stream time.
549 * The stream time is always between 0 and the total duration of the
552 * Returns: the position in stream_time or -1 when an invalid position
556 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
559 gint64 result, start, stop, time;
560 gdouble abs_applied_rate;
562 /* format does not matter for -1 */
563 if (G_UNLIKELY (position == -1))
566 g_return_val_if_fail (segment != NULL, -1);
568 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
569 segment->format = format;
571 /* if we have the position for the same format as the segment, we can compare
572 * the start and stop values, otherwise we assume 0 and -1 */
573 if (G_LIKELY (segment->format == format)) {
574 start = segment->start;
575 stop = segment->stop;
576 time = segment->time;
583 /* outside of the segment boundary stop */
584 if (G_UNLIKELY (stop != -1 && position > stop))
587 /* before the segment boundary */
588 if (G_UNLIKELY (position < start))
591 /* time must be known */
592 if (G_UNLIKELY (time == -1))
595 /* bring to uncorrected position in segment */
596 result = position - start;
598 abs_applied_rate = ABS (segment->applied_rate);
600 /* correct for applied rate if needed */
601 if (G_UNLIKELY (abs_applied_rate != 1.0))
602 result *= abs_applied_rate;
604 /* add or subtract from segment time based on applied rate */
605 if (G_LIKELY (segment->applied_rate > 0.0)) {
606 /* correct for segment time */
609 /* correct for segment time, clamp at 0. Streams with a negative
610 * applied_rate have timestamps between start and stop, as usual, but have
611 * the time member starting high and going backwards. */
612 if (G_LIKELY (time > result))
613 result = time - result;
622 * gst_segment_to_running_time:
623 * @segment: a #GstSegment structure.
624 * @format: the format of the segment.
625 * @position: the position in the segment
627 * Translate @position to the total running time using the currently configured
628 * and previously accumulated segments. Position is a value between @segment
629 * start and stop time.
631 * This function is typically used by elements that need to synchronize to the
632 * global clock in a pipeline. The runnning time is a constantly increasing value
633 * starting from 0. When gst_segment_init() is called, this value will reset to
636 * This function returns -1 if the position is outside of @segment start and stop.
638 * Returns: the position as the total running time or -1 when an invalid position
642 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
646 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 if (G_UNLIKELY (segment->abs_rate != 1.0))
692 result /= segment->abs_rate;
694 /* correct for accumulated segments */
702 * @segment: a #GstSegment structure.
703 * @format: the format of the segment.
704 * @start: the start position in the segment
705 * @stop: the stop position in the segment
706 * @clip_start: (out) (allow-none): the clipped start position in the segment
707 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
709 * Clip the given @start and @stop values to the segment boundaries given
710 * in @segment. @start and @stop are compared and clipped to @segment
711 * start and stop values.
713 * If the function returns FALSE, @start and @stop are known to fall
714 * outside of @segment and @clip_start and @clip_stop are not updated.
716 * When the function returns TRUE, @clip_start and @clip_stop will be
717 * updated. If @clip_start or @clip_stop are different from @start or @stop
718 * respectively, the region fell partially in the segment.
720 * Note that when @stop is -1, @clip_stop will be set to the end of the
721 * segment. Depending on the use case, this may or may not be what you want.
723 * Returns: TRUE if the given @start and @stop times fall partially or
724 * completely in @segment, FALSE if the values are completely outside
728 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
729 gint64 stop, gint64 * clip_start, gint64 * clip_stop)
731 g_return_val_if_fail (segment != NULL, FALSE);
733 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
734 segment->format = format;
736 g_return_val_if_fail (segment->format == format, FALSE);
738 /* if we have a stop position and a valid start and start is bigger,
739 * we're outside of the segment */
740 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
743 /* if a stop position is given and is before the segment start,
744 * we're outside of the segment. Special case is were start
745 * and stop are equal to the segment start. In that case we
746 * are inside the segment. */
747 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
748 && stop == segment->start))))
755 *clip_start = MAX (start, segment->start);
760 *clip_stop = segment->stop;
761 else if (segment->stop == -1)
762 *clip_stop = MAX (-1, stop);
764 *clip_stop = MIN (stop, segment->stop);
766 if (segment->duration != -1)
767 *clip_stop = MIN (*clip_stop, segment->duration);
774 * gst_segment_to_position:
775 * @segment: a #GstSegment structure.
776 * @format: the format of the segment.
777 * @running_time: the running_time in the segment
779 * Convert @running_time into a position in the segment so that
780 * gst_segment_to_running_time() with that position returns @running_time.
782 * Returns: the position in the segment for @running_time. This function returns
783 * -1 when @running_time is -1 or when it is not inside @segment.
788 gst_segment_to_position (GstSegment * segment, GstFormat format,
792 gint64 start, stop, accum;
794 g_return_val_if_fail (segment != NULL, -1);
796 if (G_UNLIKELY (running_time == -1))
799 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
800 segment->format = format;
802 /* if we have the position for the same format as the segment, we can compare
803 * the start and stop values, otherwise we assume 0 and -1 */
804 if (G_LIKELY (segment->format == format)) {
805 start = segment->start;
806 stop = segment->stop;
807 accum = segment->accum;
814 /* this running_time was for a previous segment */
815 if (running_time < accum)
818 /* start by subtracting the accumulated time */
819 result = running_time - accum;
821 /* move into the segment at the right rate */
822 if (G_UNLIKELY (segment->abs_rate != 1.0))
823 result = ceil (result * segment->abs_rate);
825 if (G_LIKELY (segment->rate > 0.0)) {
826 /* bring to corrected position in segment */
829 /* outside of the segment boundary stop */
830 if (G_UNLIKELY (stop != -1 && result > stop))
833 /* cannot continue if no stop position set or outside of
835 if (G_UNLIKELY (stop == -1 || result + start > stop))
838 /* bring to corrected position in segment */
839 result = stop - result;
846 * gst_segment_set_running_time:
847 * @segment: a #GstSegment structure.
848 * @format: the format of the segment.
849 * @running_time: the running_time in the segment
851 * Adjust the start/stop and accum values of @segment such that the next valid
852 * buffer will be one with @running_time.
854 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
855 * returned, @running_time is -1 or not in @segment.
860 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
864 gint64 start, stop, last_stop;
866 /* start by bringing the running_time into the segment position */
867 position = gst_segment_to_position (segment, format, running_time);
869 /* we must have a valid position now */
870 if (G_UNLIKELY (position == -1))
873 start = segment->start;
874 stop = segment->stop;
875 last_stop = segment->last_stop;
877 if (G_LIKELY (segment->rate > 0.0)) {
878 /* update the start/last_stop and time values */
880 if (last_stop < start)
883 /* reverse, update stop */
885 /* if we were past the position, go back */
886 if (last_stop > stop)
889 /* and accumulated time is exactly the running time */
890 segment->time = gst_segment_to_stream_time (segment, format, start);
891 segment->start = start;
892 segment->stop = stop;
893 segment->last_stop = last_stop;
894 segment->accum = running_time;