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 * 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 cur_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/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);
172 segment->flags = GST_SEGMENT_FLAG_NONE;
174 segment->applied_rate = 1.0;
175 segment->format = format;
180 segment->position = 0;
181 segment->duration = -1;
185 * gst_segment_do_seek:
186 * @segment: a #GstSegment structure.
187 * @rate: the rate of the segment.
188 * @format: the format of the segment.
189 * @flags: the segment flags for the segment
190 * @start_type: the seek method
191 * @start: the seek start value
192 * @stop_type: the seek method
193 * @stop: the seek stop value
194 * @update: boolean holding whether position was updated.
196 * Update the segment structure with the field values of a seek event (see
197 * gst_event_new_seek()).
199 * After calling this method, the segment field position and time will
200 * contain the requested new position in the segment. The new requested
201 * position in the segment depends on @rate and @start_type and @stop_type.
203 * For positive @rate, the new position in the segment is the new @segment
204 * start field when it was updated with a @start_type different from
205 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
206 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
209 * For negative @rate, the new position in the segment is the new @segment
210 * stop field when it was updated with a @stop_type different from
211 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
212 * duration of the segment will be used to update the stop position.
213 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
214 * @stop is ignored and @segment position is unmodified.
216 * The applied rate of the segment will be set to 1.0 by default.
217 * If the caller can apply a rate change, it should update @segment
218 * rate and applied_rate after calling this function.
220 * @update will be set to TRUE if a seek should be performed to the segment
221 * position field. This field can be FALSE if, for example, only the @rate
222 * has been changed but not the playback position.
224 * Returns: %TRUE if the seek could be performed.
227 gst_segment_do_seek (GstSegment * segment, gdouble rate,
228 GstFormat format, GstSeekFlags flags,
229 GstSeekType start_type, guint64 start,
230 GstSeekType stop_type, guint64 stop, gboolean * update)
232 gboolean update_stop, update_start;
233 guint64 position, base;
235 g_return_val_if_fail (rate != 0.0, FALSE);
236 g_return_val_if_fail (segment != NULL, FALSE);
237 g_return_val_if_fail (segment->format == format, FALSE);
239 update_start = update_stop = TRUE;
241 position = segment->position;
243 if (flags & GST_SEEK_FLAG_FLUSH) {
244 /* flush resets the running_time */
247 base = gst_segment_to_running_time (segment, format, position);
250 /* segment->start is never invalid */
251 switch (start_type) {
252 case GST_SEEK_TYPE_NONE:
253 /* no update to segment, take previous start */
254 start = segment->start;
255 update_start = FALSE;
257 case GST_SEEK_TYPE_SET:
258 /* start holds desired position, map -1 to the start */
262 case GST_SEEK_TYPE_END:
263 if (segment->duration != -1) {
264 /* add start to total length */
265 start = segment->duration + start;
267 /* no update if duration unknown */
268 start = segment->start;
269 update_start = FALSE;
273 /* bring in sane range */
274 if (segment->duration != -1)
275 start = MIN (start, segment->duration);
277 start = MAX (start, 0);
279 /* stop can be -1 if we have not configured a stop. */
281 case GST_SEEK_TYPE_NONE:
282 stop = segment->stop;
285 case GST_SEEK_TYPE_SET:
286 /* stop holds required value */
288 case GST_SEEK_TYPE_END:
289 if (segment->duration != -1) {
290 stop = segment->duration + stop;
292 stop = segment->stop;
298 /* if we have a valid stop time, make sure it is clipped */
300 if (segment->duration != -1)
301 stop = CLAMP (stop, 0, segment->duration);
303 stop = MAX (stop, 0);
306 /* we can't have stop before start */
309 g_return_val_if_fail (start <= stop, FALSE);
314 segment->rate = rate;
315 segment->applied_rate = 1.0;
316 segment->base = base;
317 /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
318 segment->flags = GST_SEGMENT_FLAG_NONE;
319 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
320 segment->flags |= GST_SEGMENT_FLAG_RESET;
321 if ((flags & GST_SEEK_FLAG_SKIP) != 0)
322 segment->flags |= GST_SEGMENT_FLAG_SKIP;
323 if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
324 segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
325 segment->start = start;
326 segment->stop = stop;
327 segment->time = start;
329 if (update_start && rate > 0.0) {
332 if (update_stop && rate < 0.0) {
336 if (segment->duration != -1)
337 position = segment->duration;
342 /* set update arg to reflect update of position */
344 *update = position != segment->position;
346 /* update new position */
347 segment->position = position;
353 * gst_segment_to_stream_time:
354 * @segment: a #GstSegment structure.
355 * @format: the format of the segment.
356 * @position: the position in the segment
358 * Translate @position to stream time using the currently configured
359 * segment. The @position value must be between @segment start and
362 * This function is typically used by elements that need to operate on
363 * the stream time of the buffers it receives, such as effect plugins.
364 * In those use cases, @position is typically the buffer timestamp or
365 * clock time that one wants to convert to the stream time.
366 * The stream time is always between 0 and the total duration of the
369 * Returns: the position in stream_time or -1 when an invalid position
373 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
376 guint64 result, start, stop, time;
377 gdouble abs_applied_rate;
379 /* format does not matter for -1 */
380 if (G_UNLIKELY (position == -1))
383 g_return_val_if_fail (segment != NULL, -1);
384 g_return_val_if_fail (segment->format == format, -1);
386 /* if we have the position for the same format as the segment, we can compare
387 * the start and stop values, otherwise we assume 0 and -1 */
388 if (G_LIKELY (segment->format == format)) {
389 start = segment->start;
390 stop = segment->stop;
391 time = segment->time;
398 /* outside of the segment boundary stop */
399 if (G_UNLIKELY (stop != -1 && position > stop))
402 /* before the segment boundary */
403 if (G_UNLIKELY (position < start))
406 /* time must be known */
407 if (G_UNLIKELY (time == -1))
410 /* bring to uncorrected position in segment */
411 result = position - start;
413 abs_applied_rate = ABS (segment->applied_rate);
415 /* correct for applied rate if needed */
416 if (G_UNLIKELY (abs_applied_rate != 1.0))
417 result *= abs_applied_rate;
419 /* add or subtract from segment time based on applied rate */
420 if (G_LIKELY (segment->applied_rate > 0.0)) {
421 /* correct for segment time */
424 /* correct for segment time, clamp at 0. Streams with a negative
425 * applied_rate have timestamps between start and stop, as usual, but have
426 * the time member starting high and going backwards. */
427 if (G_LIKELY (time > result))
428 result = time - result;
437 * gst_segment_to_running_time:
438 * @segment: a #GstSegment structure.
439 * @format: the format of the segment.
440 * @position: the position in the segment
442 * Translate @position to the total running time using the currently configured
443 * and previously accumulated segments. Position is a value between @segment
444 * start and stop time.
446 * This function is typically used by elements that need to synchronize to the
447 * global clock in a pipeline. The runnning time is a constantly increasing value
448 * starting from 0. When gst_segment_init() is called, this value will reset to
451 * This function returns -1 if the position is outside of @segment start and stop.
453 * Returns: the position as the total running time or -1 when an invalid position
457 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
461 guint64 start, stop, base;
464 if (G_UNLIKELY (position == -1))
467 g_return_val_if_fail (segment != NULL, -1);
468 g_return_val_if_fail (segment->format == format, -1);
470 /* if we have the position for the same format as the segment, we can compare
471 * the start and stop values, otherwise we assume 0 and -1 */
472 if (G_LIKELY (segment->format == format)) {
473 start = segment->start;
474 stop = segment->stop;
475 base = segment->base;
482 /* before the segment boundary */
483 if (G_UNLIKELY (position < start))
486 if (G_LIKELY (segment->rate > 0.0)) {
487 /* outside of the segment boundary stop */
488 if (G_UNLIKELY (stop != -1 && position > stop))
491 /* bring to uncorrected position in segment */
492 result = position - start;
494 /* cannot continue if no stop position set or outside of
496 if (G_UNLIKELY (stop == -1 || position > stop))
499 /* bring to uncorrected position in segment */
500 result = stop - position;
503 /* scale based on the rate, avoid division by and conversion to
504 * float when not needed */
505 abs_rate = ABS (segment->rate);
506 if (G_UNLIKELY (abs_rate != 1.0))
509 /* correct for base of the segment */
517 * @segment: a #GstSegment structure.
518 * @format: the format of the segment.
519 * @start: the start position in the segment
520 * @stop: the stop position in the segment
521 * @clip_start: (out) (allow-none): the clipped start position in the segment
522 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
524 * Clip the given @start and @stop values to the segment boundaries given
525 * in @segment. @start and @stop are compared and clipped to @segment
526 * start and stop values.
528 * If the function returns FALSE, @start and @stop are known to fall
529 * outside of @segment and @clip_start and @clip_stop are not updated.
531 * When the function returns TRUE, @clip_start and @clip_stop will be
532 * updated. If @clip_start or @clip_stop are different from @start or @stop
533 * respectively, the region fell partially in the segment.
535 * Note that when @stop is -1, @clip_stop will be set to the end of the
536 * segment. Depending on the use case, this may or may not be what you want.
538 * Returns: TRUE if the given @start and @stop times fall partially or
539 * completely in @segment, FALSE if the values are completely outside
543 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
544 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
546 g_return_val_if_fail (segment != NULL, FALSE);
547 g_return_val_if_fail (segment->format == format, FALSE);
549 /* if we have a stop position and a valid start and start is bigger,
550 * we're outside of the segment */
551 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
554 /* if a stop position is given and is before the segment start,
555 * we're outside of the segment. Special case is were start
556 * and stop are equal to the segment start. In that case we
557 * are inside the segment. */
558 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
559 && stop == segment->start))))
566 *clip_start = MAX (start, segment->start);
571 *clip_stop = segment->stop;
572 else if (segment->stop == -1)
575 *clip_stop = MIN (stop, segment->stop);
582 * gst_segment_to_position:
583 * @segment: a #GstSegment structure.
584 * @format: the format of the segment.
585 * @running_time: the running_time in the segment
587 * Convert @running_time into a position in the segment so that
588 * gst_segment_to_running_time() with that position returns @running_time.
590 * Returns: the position in the segment for @running_time. This function returns
591 * -1 when @running_time is -1 or when it is not inside @segment.
594 gst_segment_to_position (const GstSegment * segment, GstFormat format,
595 guint64 running_time)
598 guint64 start, stop, base;
601 if (G_UNLIKELY (running_time == -1))
604 g_return_val_if_fail (segment != NULL, -1);
605 g_return_val_if_fail (segment->format == format, FALSE);
607 /* if we have the position for the same format as the segment, we can compare
608 * the start and stop values, otherwise we assume 0 and -1 */
609 if (G_LIKELY (segment->format == format)) {
610 start = segment->start;
611 stop = segment->stop;
612 base = segment->base;
619 /* this running_time was for a previous segment */
620 if (running_time < base)
623 /* start by subtracting the base time */
624 result = running_time - base;
626 /* move into the segment at the right rate */
627 abs_rate = ABS (segment->rate);
628 if (G_UNLIKELY (abs_rate != 1.0))
629 result = ceil (result * abs_rate);
631 if (G_LIKELY (segment->rate > 0.0)) {
632 /* bring to corrected position in segment */
635 /* outside of the segment boundary stop */
636 if (G_UNLIKELY (stop != -1 && result > stop))
639 /* cannot continue if no stop position set or outside of
641 if (G_UNLIKELY (stop == -1 || result + start > stop))
644 /* bring to corrected position in segment */
645 result = stop - result;
652 * gst_segment_set_running_time:
653 * @segment: a #GstSegment structure.
654 * @format: the format of the segment.
655 * @running_time: the running_time in the segment
657 * Adjust the start/stop and base values of @segment such that the next valid
658 * buffer will be one with @running_time.
660 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
661 * returned, @running_time is -1 or not in @segment.
664 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
665 guint64 running_time)
670 /* start by bringing the running_time into the segment position */
671 position = gst_segment_to_position (segment, format, running_time);
673 /* we must have a valid position now */
674 if (G_UNLIKELY (position == -1))
677 start = segment->start;
678 stop = segment->stop;
680 if (G_LIKELY (segment->rate > 0.0)) {
681 /* update the start and time values */
684 /* reverse, update stop */
687 /* and base time is exactly the running time */
688 segment->time = gst_segment_to_stream_time (segment, format, start);
689 segment->start = start;
690 segment->stop = stop;
691 segment->base = running_time;