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).
83 * Last reviewed on 2012-03-29 (0.11.3)
88 * @segment: (transfer none): a #GstSegment
90 * Create a copy of given @segment.
92 * Free-function: gst_segment_free
94 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
97 gst_segment_copy (const GstSegment * segment)
99 GstSegment *result = NULL;
102 result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
108 * gst_segment_copy_into:
109 * @src: (transfer none): a #GstSegment
110 * @dest: (transfer none): a #GstSegment
112 * Copy the contents of @src into @dest.
115 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
117 memcpy (dest, src, sizeof (GstSegment));
120 G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
121 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
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/position fields 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);
172 segment->flags = GST_SEGMENT_FLAG_NONE;
174 segment->applied_rate = 1.0;
175 segment->format = format;
181 segment->position = 0;
182 segment->duration = -1;
186 * gst_segment_do_seek:
187 * @segment: a #GstSegment structure.
188 * @rate: the rate of the segment.
189 * @format: the format of the segment.
190 * @flags: the segment flags for the segment
191 * @start_type: the seek method
192 * @start: the seek start value
193 * @stop_type: the seek method
194 * @stop: the seek stop value
195 * @update: boolean holding whether position was updated.
197 * Update the segment structure with the field values of a seek event (see
198 * gst_event_new_seek()).
200 * After calling this method, the segment field position and time will
201 * contain the requested new position in the segment. The new requested
202 * position in the segment depends on @rate and @start_type and @stop_type.
204 * For positive @rate, the new position in the segment is the new @segment
205 * start field when it was updated with a @start_type different from
206 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
207 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
210 * For negative @rate, the new position in the segment is the new @segment
211 * stop field when it was updated with a @stop_type different from
212 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
213 * duration of the segment will be used to update the stop position.
214 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
215 * @stop is ignored and @segment position is unmodified.
217 * The applied rate of the segment will be set to 1.0 by default.
218 * If the caller can apply a rate change, it should update @segment
219 * rate and applied_rate after calling this function.
221 * @update will be set to TRUE if a seek should be performed to the segment
222 * position field. This field can be FALSE if, for example, only the @rate
223 * has been changed but not the playback position.
225 * Returns: %TRUE if the seek could be performed.
228 gst_segment_do_seek (GstSegment * segment, gdouble rate,
229 GstFormat format, GstSeekFlags flags,
230 GstSeekType start_type, guint64 start,
231 GstSeekType stop_type, guint64 stop, gboolean * update)
233 gboolean update_stop, update_start;
234 guint64 position, base;
236 g_return_val_if_fail (rate != 0.0, FALSE);
237 g_return_val_if_fail (segment != NULL, FALSE);
238 g_return_val_if_fail (segment->format == format, FALSE);
240 update_start = update_stop = TRUE;
242 position = segment->position;
244 /* segment->start is never invalid */
245 switch (start_type) {
246 case GST_SEEK_TYPE_NONE:
247 /* no update to segment, take previous start */
248 start = segment->start;
249 update_start = FALSE;
251 case GST_SEEK_TYPE_SET:
252 /* start holds desired position, map -1 to the start */
256 case GST_SEEK_TYPE_END:
257 if (segment->duration != -1) {
258 /* add start to total length */
259 start = segment->duration + start;
261 /* no update if duration unknown */
262 start = segment->start;
263 update_start = FALSE;
267 /* bring in sane range */
268 if (segment->duration != -1)
269 start = MIN (start, segment->duration);
271 start = MAX ((gint64) start, 0);
273 /* stop can be -1 if we have not configured a stop. */
275 case GST_SEEK_TYPE_NONE:
276 stop = segment->stop;
279 case GST_SEEK_TYPE_SET:
280 /* stop holds required value */
282 case GST_SEEK_TYPE_END:
283 if (segment->duration != -1) {
284 stop = segment->duration + stop;
286 stop = segment->stop;
292 /* if we have a valid stop time, make sure it is clipped */
294 if (segment->duration != -1)
295 stop = CLAMP ((gint64) stop, 0, (gint64) segment->duration);
297 stop = MAX ((gint64) stop, 0);
300 /* we can't have stop before start */
303 GST_WARNING ("segment update failed: start(%" G_GUINT64_FORMAT
304 ") > stop(%" G_GUINT64_FORMAT ")", start, stop);
305 g_return_val_if_fail (start <= stop, FALSE);
310 if (flags & GST_SEEK_FLAG_FLUSH) {
311 /* flush resets the running_time */
314 /* make sure the position is inside the segment start/stop */
315 position = CLAMP (position, segment->start, segment->stop);
317 /* remember the elapsed time */
318 base = gst_segment_to_running_time (segment, format, position);
319 GST_DEBUG ("updated segment.base: %" G_GUINT64_FORMAT, base);
322 if (update_start && rate > 0.0) {
325 if (update_stop && rate < 0.0) {
329 if (segment->duration != -1)
330 position = segment->duration;
336 /* set update arg to reflect update of position */
338 *update = position != segment->position;
340 /* update new values */
341 /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
342 segment->flags = GST_SEGMENT_FLAG_NONE;
343 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
344 segment->flags |= GST_SEGMENT_FLAG_RESET;
345 if ((flags & GST_SEEK_FLAG_SKIP) != 0)
346 segment->flags |= GST_SEGMENT_FLAG_SKIP;
347 if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
348 segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
350 segment->rate = rate;
351 segment->applied_rate = 1.0;
353 segment->base = base;
355 segment->offset = position - start;
358 segment->offset = stop - position;
359 else if (segment->duration != -1)
360 segment->offset = segment->duration - position;
365 segment->start = start;
366 segment->stop = stop;
367 segment->time = start;
368 segment->position = position;
370 GST_INFO ("segment updated: %" GST_SEGMENT_FORMAT, segment);
376 * gst_segment_to_stream_time:
377 * @segment: a #GstSegment structure.
378 * @format: the format of the segment.
379 * @position: the position in the segment
381 * Translate @position to stream time using the currently configured
382 * segment. The @position value must be between @segment start and
385 * This function is typically used by elements that need to operate on
386 * the stream time of the buffers it receives, such as effect plugins.
387 * In those use cases, @position is typically the buffer timestamp or
388 * clock time that one wants to convert to the stream time.
389 * The stream time is always between 0 and the total duration of the
392 * Returns: the position in stream_time or -1 when an invalid position
396 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
399 guint64 result, start, stop, time;
400 gdouble abs_applied_rate;
402 /* format does not matter for -1 */
403 if (G_UNLIKELY (position == -1))
406 g_return_val_if_fail (segment != NULL, -1);
407 g_return_val_if_fail (segment->format == format, -1);
409 stop = segment->stop;
411 /* outside of the segment boundary stop */
412 if (G_UNLIKELY (stop != -1 && position > stop))
415 start = segment->start;
417 /* before the segment boundary */
418 if (G_UNLIKELY (position < start))
421 time = segment->time;
423 /* time must be known */
424 if (G_UNLIKELY (time == -1))
427 /* bring to uncorrected position in segment */
428 result = position - start;
430 abs_applied_rate = ABS (segment->applied_rate);
432 /* correct for applied rate if needed */
433 if (G_UNLIKELY (abs_applied_rate != 1.0))
434 result *= abs_applied_rate;
436 /* add or subtract from segment time based on applied rate */
437 if (G_LIKELY (segment->applied_rate > 0.0)) {
438 /* correct for segment time */
441 /* correct for segment time, clamp at 0. Streams with a negative
442 * applied_rate have timestamps between start and stop, as usual, but have
443 * the time member starting high and going backwards. */
444 if (G_LIKELY (time > result))
445 result = time - result;
454 * gst_segment_to_running_time:
455 * @segment: a #GstSegment structure.
456 * @format: the format of the segment.
457 * @position: the position in the segment
459 * Translate @position to the total running time using the currently configured
460 * segment. Position is a value between @segment start and stop time.
462 * This function is typically used by elements that need to synchronize to the
463 * global clock in a pipeline. The running time is a constantly increasing value
464 * starting from 0. When gst_segment_init() is called, this value will reset to
467 * This function returns -1 if the position is outside of @segment start and stop.
469 * Returns: the position as the total running time or -1 when an invalid position
473 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
480 if (G_UNLIKELY (position == -1)) {
481 GST_DEBUG ("invalid position (-1)");
485 g_return_val_if_fail (segment != NULL, -1);
486 g_return_val_if_fail (segment->format == format, -1);
488 start = segment->start;
490 if (segment->rate > 0.0)
491 start += segment->offset;
493 /* before the segment boundary */
494 if (G_UNLIKELY (position < start)) {
495 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
496 ")", position, start);
500 stop = segment->stop;
502 if (G_LIKELY (segment->rate > 0.0)) {
503 /* after of the segment boundary */
504 if (G_UNLIKELY (stop != -1 && position > stop)) {
505 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
506 ")", position, stop);
510 /* bring to uncorrected position in segment */
511 result = position - start;
513 /* cannot continue if no stop position set or outside of
515 if (G_UNLIKELY (stop == -1)) {
516 GST_DEBUG ("invalid stop (-1)");
520 stop -= segment->offset;
521 if (G_UNLIKELY (position > stop)) {
522 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
523 ")", position, stop);
527 /* bring to uncorrected position in segment */
528 result = stop - position;
531 /* scale based on the rate, avoid division by and conversion to
532 * float when not needed */
533 abs_rate = ABS (segment->rate);
534 if (G_UNLIKELY (abs_rate != 1.0))
537 /* correct for base of the segment */
538 result += segment->base;
545 * @segment: a #GstSegment structure.
546 * @format: the format of the segment.
547 * @start: the start position in the segment
548 * @stop: the stop position in the segment
549 * @clip_start: (out) (allow-none): the clipped start position in the segment
550 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
552 * Clip the given @start and @stop values to the segment boundaries given
553 * in @segment. @start and @stop are compared and clipped to @segment
554 * start and stop values.
556 * If the function returns FALSE, @start and @stop are known to fall
557 * outside of @segment and @clip_start and @clip_stop are not updated.
559 * When the function returns TRUE, @clip_start and @clip_stop will be
560 * updated. If @clip_start or @clip_stop are different from @start or @stop
561 * respectively, the region fell partially in the segment.
563 * Note that when @stop is -1, @clip_stop will be set to the end of the
564 * segment. Depending on the use case, this may or may not be what you want.
566 * Returns: TRUE if the given @start and @stop times fall partially or
567 * completely in @segment, FALSE if the values are completely outside
571 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
572 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
574 g_return_val_if_fail (segment != NULL, FALSE);
575 g_return_val_if_fail (segment->format == format, FALSE);
577 /* if we have a stop position and a valid start and start is bigger,
578 * we're outside of the segment */
579 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
582 /* if a stop position is given and is before the segment start,
583 * we're outside of the segment. Special case is were start
584 * and stop are equal to the segment start. In that case we
585 * are inside the segment. */
586 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
587 && stop == segment->start))))
594 *clip_start = MAX (start, segment->start);
599 *clip_stop = segment->stop;
600 else if (segment->stop == -1)
603 *clip_stop = MIN (stop, segment->stop);
610 * gst_segment_to_position:
611 * @segment: a #GstSegment structure.
612 * @format: the format of the segment.
613 * @running_time: the running_time in the segment
615 * Convert @running_time into a position in the segment so that
616 * gst_segment_to_running_time() with that position returns @running_time.
618 * Returns: the position in the segment for @running_time. This function returns
619 * -1 when @running_time is -1 or when it is not inside @segment.
622 gst_segment_to_position (const GstSegment * segment, GstFormat format,
623 guint64 running_time)
626 guint64 start, stop, base;
629 if (G_UNLIKELY (running_time == -1))
632 g_return_val_if_fail (segment != NULL, -1);
633 g_return_val_if_fail (segment->format == format, FALSE);
635 base = segment->base;
637 /* this running_time was for a previous segment */
638 if (running_time < base)
641 /* start by subtracting the base time */
642 result = running_time - base;
644 /* move into the segment at the right rate */
645 abs_rate = ABS (segment->rate);
646 if (G_UNLIKELY (abs_rate != 1.0))
647 result = ceil (result * abs_rate);
649 start = segment->start;
650 stop = segment->stop;
652 if (G_LIKELY (segment->rate > 0.0)) {
653 /* bring to corrected position in segment */
654 result += start + segment->offset;
656 /* outside of the segment boundary stop */
657 if (G_UNLIKELY (stop != -1 && result > stop))
660 /* cannot continue if no stop position set or outside of
662 if (G_UNLIKELY (stop == -1 || result + start > stop))
665 /* bring to corrected position in segment */
666 result = stop - result - segment->offset;
672 * gst_segment_set_running_time:
673 * @segment: a #GstSegment structure.
674 * @format: the format of the segment.
675 * @running_time: the running_time in the segment
677 * Adjust the start/stop and base values of @segment such that the next valid
678 * buffer will be one with @running_time.
680 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
681 * returned, @running_time is -1 or not in @segment.
684 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
685 guint64 running_time)
690 /* start by bringing the running_time into the segment position */
691 position = gst_segment_to_position (segment, format, running_time);
693 /* we must have a valid position now */
694 if (G_UNLIKELY (position == -1))
697 start = segment->start;
698 stop = segment->stop;
700 if (G_LIKELY (segment->rate > 0.0)) {
701 /* update the start and time values */
704 /* reverse, update stop */
707 /* and base time is exactly the running time */
708 segment->time = gst_segment_to_stream_time (segment, format, start);
709 segment->start = start;
710 segment->stop = stop;
711 segment->base = running_time;
717 * gst_segment_offset_running_time:
718 * @segment: a #GstSegment structure.
719 * @format: the format of the segment.
720 * @offset: the offset to apply in the segment
722 * Adjust the values in @segment so that @offset is applied to all
723 * future running-time calculations.
727 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
728 * returned, @offset is not in @segment.
731 gst_segment_offset_running_time (GstSegment * segment, GstFormat format,
734 g_return_val_if_fail (segment != NULL, FALSE);
735 g_return_val_if_fail (segment->format == format, FALSE);
741 /* positive offset, we can simply apply to the base time */
742 segment->base += offset;
745 /* negative offset, first try to subtract from base */
746 if (segment->base > offset) {
747 segment->base -= offset;
751 /* subtract all from segment.base, remainder in offset */
752 offset -= segment->base;
754 position = gst_segment_to_position (segment, format, offset);
758 segment->offset = position;