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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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 * The public duration field contains the duration of the segment. When using
56 * the segment for seeking, the start and time members should normally be left
57 * to their default 0 value. The stop position is left to -1 unless explicitly
58 * configured to a different value after a seek event.
60 * The current position in the segment should be set by changing the position
61 * member in the structure.
63 * For elements that perform seeks, the current segment should be updated with the
64 * gst_segment_do_seek() and the values from the seek event. This method will update
65 * all the segment fields. The position field will contain the new playback position.
66 * If the start_type was different from GST_SEEK_TYPE_NONE, playback continues from
67 * the position position, possibly with updated flags or rate.
69 * For elements that want to use #GstSegment to track the playback region,
70 * update the segment fields with the information from the newsegment event.
71 * The gst_segment_clip() method can be used to check and clip
72 * the media data to the segment boundaries.
74 * For elements that want to synchronize to the pipeline clock, gst_segment_to_running_time()
75 * can be used to convert a timestamp to a value that can be used to synchronize
76 * to the clock. This function takes into account the base as well as
77 * any rate or applied_rate conversions.
79 * For elements that need to perform operations on media data in stream_time,
80 * gst_segment_to_stream_time() can be used to convert a timestamp and the segment
81 * info to stream time (which is always between 0 and the duration of the stream).
86 * @segment: (transfer none): a #GstSegment
88 * Create a copy of given @segment.
90 * Free-function: gst_segment_free
92 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
95 gst_segment_copy (const GstSegment * segment)
97 GstSegment *result = NULL;
100 result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
106 * gst_segment_copy_into:
107 * @src: (transfer none): a #GstSegment
108 * @dest: (transfer none): a #GstSegment
110 * Copy the contents of @src into @dest.
113 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
115 memcpy (dest, src, sizeof (GstSegment));
118 G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
119 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
124 * Allocate a new #GstSegment structure and initialize it using
125 * gst_segment_init().
127 * Free-function: gst_segment_free
129 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
132 gst_segment_new (void)
136 result = g_slice_new0 (GstSegment);
137 gst_segment_init (result, GST_FORMAT_UNDEFINED);
144 * @segment: (in) (transfer full): a #GstSegment
146 * Free the allocated segment @segment.
149 gst_segment_free (GstSegment * segment)
151 g_slice_free (GstSegment, segment);
156 * @segment: a #GstSegment structure.
157 * @format: the format of the segment.
159 * The start/position fields are set to 0 and the stop/duration
160 * fields are set to -1 (unknown). The default rate of 1.0 and no
163 * Initialize @segment to its default values.
166 gst_segment_init (GstSegment * segment, GstFormat format)
168 g_return_if_fail (segment != NULL);
170 segment->flags = GST_SEGMENT_FLAG_NONE;
172 segment->applied_rate = 1.0;
173 segment->format = format;
179 segment->position = 0;
180 segment->duration = -1;
184 * gst_segment_do_seek:
185 * @segment: a #GstSegment structure.
186 * @rate: the rate of the segment.
187 * @format: the format of the segment.
188 * @flags: the segment flags for the segment
189 * @start_type: the seek method
190 * @start: the seek start value
191 * @stop_type: the seek method
192 * @stop: the seek stop value
193 * @update: boolean holding whether position was updated.
195 * Update the segment structure with the field values of a seek event (see
196 * gst_event_new_seek()).
198 * After calling this method, the segment field position and time will
199 * contain the requested new position in the segment. The new requested
200 * position in the segment depends on @rate and @start_type and @stop_type.
202 * For positive @rate, the new position in the segment is the new @segment
203 * start field when it was updated with a @start_type different from
204 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
205 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
208 * For negative @rate, the new position in the segment is the new @segment
209 * stop field when it was updated with a @stop_type different from
210 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
211 * duration of the segment will be used to update the stop position.
212 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
213 * @stop is ignored and @segment position is unmodified.
215 * The applied rate of the segment will be set to 1.0 by default.
216 * If the caller can apply a rate change, it should update @segment
217 * rate and applied_rate after calling this function.
219 * @update will be set to %TRUE if a seek should be performed to the segment
220 * position field. This field can be %FALSE if, for example, only the @rate
221 * has been changed but not the playback position.
223 * Returns: %TRUE if the seek could be performed.
226 gst_segment_do_seek (GstSegment * segment, gdouble rate,
227 GstFormat format, GstSeekFlags flags,
228 GstSeekType start_type, guint64 start,
229 GstSeekType stop_type, guint64 stop, gboolean * update)
231 gboolean update_stop, update_start;
232 guint64 position, base;
234 g_return_val_if_fail (rate != 0.0, FALSE);
235 g_return_val_if_fail (segment != NULL, FALSE);
236 g_return_val_if_fail (segment->format == format, FALSE);
238 update_start = update_stop = TRUE;
240 position = segment->position;
242 /* segment->start is never invalid */
243 switch (start_type) {
244 case GST_SEEK_TYPE_NONE:
245 /* no update to segment, take previous start */
246 start = segment->start;
247 update_start = FALSE;
249 case GST_SEEK_TYPE_SET:
250 /* start holds desired position, map -1 to the start */
254 case GST_SEEK_TYPE_END:
255 if (segment->duration != -1) {
256 /* add start to total length */
257 start = segment->duration + start;
259 /* no update if duration unknown */
260 start = segment->start;
261 update_start = FALSE;
265 /* bring in sane range */
266 if (segment->duration != -1)
267 start = MIN (start, segment->duration);
269 start = MAX ((gint64) start, 0);
271 /* stop can be -1 if we have not configured a stop. */
273 case GST_SEEK_TYPE_NONE:
274 stop = segment->stop;
277 case GST_SEEK_TYPE_SET:
278 /* stop holds required value */
280 case GST_SEEK_TYPE_END:
281 if (segment->duration != -1) {
282 stop = segment->duration + stop;
284 stop = segment->stop;
290 /* if we have a valid stop time, make sure it is clipped */
292 if (segment->duration != -1)
293 stop = CLAMP ((gint64) stop, 0, (gint64) segment->duration);
295 stop = MAX ((gint64) stop, 0);
298 /* we can't have stop before start */
301 GST_WARNING ("segment update failed: start(%" G_GUINT64_FORMAT
302 ") > stop(%" G_GUINT64_FORMAT ")", start, stop);
303 g_return_val_if_fail (start <= stop, FALSE);
308 if (flags & GST_SEEK_FLAG_FLUSH) {
309 /* flush resets the running_time */
312 /* make sure the position is inside the segment start/stop */
313 position = CLAMP (position, segment->start, segment->stop);
315 /* remember the elapsed time */
316 base = gst_segment_to_running_time (segment, format, position);
317 GST_DEBUG ("updated segment.base: %" G_GUINT64_FORMAT, base);
320 if (update_start && rate > 0.0) {
323 if (update_stop && rate < 0.0) {
327 if (segment->duration != -1)
328 position = segment->duration;
334 /* set update arg to reflect update of position */
336 *update = position != segment->position;
338 /* update new values */
339 /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
340 segment->flags = GST_SEGMENT_FLAG_NONE;
341 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
342 segment->flags |= GST_SEGMENT_FLAG_RESET;
343 if ((flags & GST_SEEK_FLAG_TRICKMODE) != 0)
344 segment->flags |= GST_SEGMENT_FLAG_TRICKMODE;
345 if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
346 segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
347 if ((flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS) != 0)
348 segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS;
349 if ((flags & GST_SEEK_FLAG_TRICKMODE_NO_AUDIO) != 0)
350 segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_NO_AUDIO;
352 segment->rate = rate;
353 segment->applied_rate = 1.0;
355 segment->base = base;
357 segment->offset = position - start;
360 segment->offset = stop - position;
361 else if (segment->duration != -1)
362 segment->offset = segment->duration - position;
367 segment->start = start;
368 segment->stop = stop;
369 segment->time = start;
370 segment->position = position;
372 GST_INFO ("segment updated: %" GST_SEGMENT_FORMAT, segment);
378 * gst_segment_to_stream_time:
379 * @segment: a #GstSegment structure.
380 * @format: the format of the segment.
381 * @position: the position in the segment
383 * Translate @position to stream time using the currently configured
384 * segment. The @position value must be between @segment start and
387 * This function is typically used by elements that need to operate on
388 * the stream time of the buffers it receives, such as effect plugins.
389 * In those use cases, @position is typically the buffer timestamp or
390 * clock time that one wants to convert to the stream time.
391 * The stream time is always between 0 and the total duration of the
394 * Returns: the position in stream_time or -1 when an invalid position
398 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
401 guint64 result, start, stop, time;
402 gdouble abs_applied_rate;
404 /* format does not matter for -1 */
405 if (G_UNLIKELY (position == -1))
408 g_return_val_if_fail (segment != NULL, -1);
409 g_return_val_if_fail (segment->format == format, -1);
411 stop = segment->stop;
413 /* outside of the segment boundary stop */
414 if (G_UNLIKELY (stop != -1 && position > stop))
417 start = segment->start;
419 /* before the segment boundary */
420 if (G_UNLIKELY (position < start))
423 time = segment->time;
425 /* time must be known */
426 if (G_UNLIKELY (time == -1))
429 /* bring to uncorrected position in segment */
430 result = position - start;
432 abs_applied_rate = ABS (segment->applied_rate);
434 /* correct for applied rate if needed */
435 if (G_UNLIKELY (abs_applied_rate != 1.0))
436 result *= abs_applied_rate;
438 /* add or subtract from segment time based on applied rate */
439 if (G_LIKELY (segment->applied_rate > 0.0)) {
440 /* correct for segment time */
443 /* correct for segment time, clamp at 0. Streams with a negative
444 * applied_rate have timestamps between start and stop, as usual, but have
445 * the time member starting high and going backwards. */
446 if (G_LIKELY (time > result))
447 result = time - result;
456 * gst_segment_to_running_time_full:
457 * @segment: a #GstSegment structure.
458 * @format: the format of the segment.
459 * @position: the position in the segment
460 * @clip: clip against segment boundaries
461 * @running_time: result running-time
463 * Translate @position to the total running time using the currently configured
464 * segment. Compared to gst_segment_to_running_time() this function can return
465 * negative running-time and also check if a @position is before or after the
468 * This function is typically used by elements that need to synchronize buffers
469 * against the clock or eachother.
471 * If clip is %TRUE, @position is a value between @segment start and stop.
472 * When @position is outside of the segment start and stop values,
473 * #GST_SEGMENT_RESULT_BEFORE or #GST_SEGMENT_RESULT_AFTER is returned depending
474 * if @position is respectively before or after the segment.
476 * If clip is %FALSE, @position can be any value and the result of this function
477 * for values outside of the segment is extrapolated.
479 * When #GST_SEGMENT_RESULT_OK is returned, @position resulted in a positive
480 * running-time returned in @running_time.
482 * When this function returns #GST_SEGMENT_RESULT_NEGATIVE, the returned
483 * @running_time should be negated to get the real negative running time.
485 * Returns: a #GstSegmentResult
490 gst_segment_to_running_time_full (const GstSegment * segment, GstFormat format,
491 guint64 position, gboolean clip, guint64 * running_time)
493 GstSegmentResult res;
495 guint64 start, stop, offset;
498 if (G_UNLIKELY (position == -1)) {
499 GST_DEBUG ("invalid position (-1)");
500 res = GST_SEGMENT_RESULT_INVALID;
504 g_return_val_if_fail (segment != NULL, GST_SEGMENT_RESULT_INVALID);
505 g_return_val_if_fail (segment->format == format, GST_SEGMENT_RESULT_INVALID);
507 start = segment->start;
508 /* before the segment boundary */
509 if (clip && G_UNLIKELY (position < start)) {
510 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
511 ")", position, start);
512 if (G_LIKELY (segment->rate > 0.0))
513 res = GST_SEGMENT_RESULT_BEFORE;
515 res = GST_SEGMENT_RESULT_AFTER;
519 stop = segment->stop;
520 /* after the segment boundary */
521 if (clip && G_UNLIKELY (stop != -1 && position > stop)) {
522 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
523 ")", position, stop);
524 if (G_LIKELY (segment->rate > 0.0))
525 res = GST_SEGMENT_RESULT_AFTER;
527 res = GST_SEGMENT_RESULT_BEFORE;
531 offset = segment->offset;
533 if (G_LIKELY (segment->rate > 0.0)) {
536 /* bring to uncorrected position in segment */
537 if (position < start) {
539 result = start - position;
540 res = GST_SEGMENT_RESULT_NEGATIVE;
542 result = position - start;
543 res = GST_SEGMENT_RESULT_OK;
546 /* cannot continue if no stop position set or invalid offset */
547 g_return_val_if_fail (stop != -1, GST_SEGMENT_RESULT_INVALID);
548 g_return_val_if_fail (stop >= segment->offset, GST_SEGMENT_RESULT_INVALID);
552 /* bring to uncorrected position in segment */
553 if (position > stop) {
555 result = position - stop;
556 res = GST_SEGMENT_RESULT_NEGATIVE;
558 result = stop - position;
559 res = GST_SEGMENT_RESULT_OK;
564 /* scale based on the rate, avoid division by and conversion to
565 * float when not needed */
566 abs_rate = ABS (segment->rate);
567 if (G_UNLIKELY (abs_rate != 1.0))
570 /* correct for base of the segment */
571 if (res == GST_SEGMENT_RESULT_OK)
572 /* positive, add base */
573 *running_time = result + segment->base;
574 else if (segment->base >= result) {
575 /* negative and base is bigger, subtract from base and we have a
576 * positive value again */
577 *running_time = segment->base - result;
578 res = GST_SEGMENT_RESULT_OK;
580 /* negative and base is smaller, subtract base and remainder is
582 *running_time = result - segment->base;
596 * gst_segment_to_running_time:
597 * @segment: a #GstSegment structure.
598 * @format: the format of the segment.
599 * @position: the position in the segment
601 * Translate @position to the total running time using the currently configured
602 * segment. Position is a value between @segment start and stop time.
604 * This function is typically used by elements that need to synchronize to the
605 * global clock in a pipeline. The running time is a constantly increasing value
606 * starting from 0. When gst_segment_init() is called, this value will reset to
609 * This function returns -1 if the position is outside of @segment start and stop.
611 * Returns: the position as the total running time or -1 when an invalid position
615 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
619 GstSegmentResult res;
622 gst_segment_to_running_time_full (segment, format, position, TRUE,
624 if (res == GST_SEGMENT_RESULT_OK)
632 * @segment: a #GstSegment structure.
633 * @format: the format of the segment.
634 * @start: the start position in the segment
635 * @stop: the stop position in the segment
636 * @clip_start: (out) (allow-none): the clipped start position in the segment
637 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
639 * Clip the given @start and @stop values to the segment boundaries given
640 * in @segment. @start and @stop are compared and clipped to @segment
641 * start and stop values.
643 * If the function returns %FALSE, @start and @stop are known to fall
644 * outside of @segment and @clip_start and @clip_stop are not updated.
646 * When the function returns %TRUE, @clip_start and @clip_stop will be
647 * updated. If @clip_start or @clip_stop are different from @start or @stop
648 * respectively, the region fell partially in the segment.
650 * Note that when @stop is -1, @clip_stop will be set to the end of the
651 * segment. Depending on the use case, this may or may not be what you want.
653 * Returns: %TRUE if the given @start and @stop times fall partially or
654 * completely in @segment, %FALSE if the values are completely outside
658 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
659 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
661 g_return_val_if_fail (segment != NULL, FALSE);
662 g_return_val_if_fail (segment->format == format, FALSE);
664 /* if we have a stop position and a valid start and start is bigger,
665 * we're outside of the segment */
666 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
669 /* if a stop position is given and is before the segment start,
670 * we're outside of the segment. Special case is were start
671 * and stop are equal to the segment start. In that case we
672 * are inside the segment. */
673 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
674 && stop == segment->start))))
681 *clip_start = MAX (start, segment->start);
686 *clip_stop = segment->stop;
687 else if (segment->stop == -1)
690 *clip_stop = MIN (stop, segment->stop);
697 * gst_segment_to_position:
698 * @segment: a #GstSegment structure.
699 * @format: the format of the segment.
700 * @running_time: the running_time in the segment
702 * Convert @running_time into a position in the segment so that
703 * gst_segment_to_running_time() with that position returns @running_time.
705 * Returns: the position in the segment for @running_time. This function returns
706 * -1 when @running_time is -1 or when it is not inside @segment.
709 gst_segment_to_position (const GstSegment * segment, GstFormat format,
710 guint64 running_time)
713 guint64 start, stop, base;
716 if (G_UNLIKELY (running_time == -1))
719 g_return_val_if_fail (segment != NULL, -1);
720 g_return_val_if_fail (segment->format == format, FALSE);
722 base = segment->base;
724 /* this running_time was for a previous segment */
725 if (running_time < base)
728 /* start by subtracting the base time */
729 result = running_time - base;
731 /* move into the segment at the right rate */
732 abs_rate = ABS (segment->rate);
733 if (G_UNLIKELY (abs_rate != 1.0))
734 result = ceil (result * abs_rate);
736 start = segment->start;
737 stop = segment->stop;
739 if (G_LIKELY (segment->rate > 0.0)) {
740 /* bring to corrected position in segment */
741 result += start + segment->offset;
743 /* outside of the segment boundary stop */
744 if (G_UNLIKELY (stop != -1 && result > stop))
747 /* cannot continue if no stop position set or outside of
749 if (G_UNLIKELY (stop == -1 || result + start > stop))
752 /* bring to corrected position in segment */
753 result = stop - result - segment->offset;
759 * gst_segment_set_running_time:
760 * @segment: a #GstSegment structure.
761 * @format: the format of the segment.
762 * @running_time: the running_time in the segment
764 * Adjust the start/stop and base values of @segment such that the next valid
765 * buffer will be one with @running_time.
767 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
768 * returned, @running_time is -1 or not in @segment.
771 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
772 guint64 running_time)
777 /* start by bringing the running_time into the segment position */
778 position = gst_segment_to_position (segment, format, running_time);
780 /* we must have a valid position now */
781 if (G_UNLIKELY (position == -1))
784 start = segment->start;
785 stop = segment->stop;
787 if (G_LIKELY (segment->rate > 0.0)) {
788 /* update the start and time values */
791 /* reverse, update stop */
794 /* and base time is exactly the running time */
795 segment->time = gst_segment_to_stream_time (segment, format, start);
796 segment->start = start;
797 segment->stop = stop;
798 segment->base = running_time;
804 * gst_segment_offset_running_time:
805 * @segment: a #GstSegment structure.
806 * @format: the format of the segment.
807 * @offset: the offset to apply in the segment
809 * Adjust the values in @segment so that @offset is applied to all
810 * future running-time calculations.
814 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
815 * returned, @offset is not in @segment.
818 gst_segment_offset_running_time (GstSegment * segment, GstFormat format,
821 g_return_val_if_fail (segment != NULL, FALSE);
822 g_return_val_if_fail (segment->format == format, FALSE);
828 /* positive offset, we can simply apply to the base time */
829 segment->base += offset;
832 /* negative offset, first try to subtract from base */
833 if (segment->base > offset) {
834 segment->base -= offset;
838 /* subtract all from segment.base, remainder in offset */
839 offset -= segment->base;
841 position = gst_segment_to_position (segment, format, offset);
845 segment->offset = position - segment->start;