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 (const GstSegment * segment)
102 GstSegment *result = NULL;
105 result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
111 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
113 memcpy (dest, src, sizeof (GstSegment));
116 G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
117 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
122 * Allocate a new #GstSegment structure and initialize it using
123 * gst_segment_init().
125 * Free-function: gst_segment_free
127 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
130 gst_segment_new (void)
134 result = g_slice_new0 (GstSegment);
135 gst_segment_init (result, GST_FORMAT_UNDEFINED);
142 * @segment: (in) (transfer full): a #GstSegment
144 * Free the allocated segment @segment.
147 gst_segment_free (GstSegment * segment)
149 g_slice_free (GstSegment, segment);
154 * @segment: a #GstSegment structure.
155 * @format: the format of the segment.
157 * The start/last_stop positions are set to 0 and the stop/duration
158 * fields are set to -1 (unknown). The default rate of 1.0 and no
161 * Initialize @segment to its default values.
164 gst_segment_init (GstSegment * segment, GstFormat format)
166 g_return_if_fail (segment != NULL);
168 segment->flags = GST_SEGMENT_FLAG_NONE;
170 segment->applied_rate = 1.0;
171 segment->format = format;
176 segment->position = 0;
177 segment->duration = -1;
181 * gst_segment_do_seek:
182 * @segment: a #GstSegment structure.
183 * @rate: the rate of the segment.
184 * @format: the format of the segment.
185 * @flags: the segment flags for the segment
186 * @start_type: the seek method
187 * @start: the seek start value
188 * @stop_type: the seek method
189 * @stop: the seek stop value
190 * @update: boolean holding whether position was updated.
192 * Update the segment structure with the field values of a seek event (see
193 * gst_event_new_seek()).
195 * After calling this method, the segment field position and time will
196 * contain the requested new position in the segment. The new requested
197 * position in the segment depends on @rate and @start_type and @stop_type.
199 * For positive @rate, the new position in the segment is the new @segment
200 * start field when it was updated with a @start_type different from
201 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
202 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
205 * For negative @rate, the new position in the segment is the new @segment
206 * stop field when it was updated with a @stop_type different from
207 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
208 * duration of the segment will be used to update the stop position.
209 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
210 * @stop is ignored and @segment position is unmodified.
212 * The applied rate of the segment will be set to 1.0 by default.
213 * If the caller can apply a rate change, it should update @segment
214 * rate and applied_rate after calling this function.
216 * @update will be set to TRUE if a seek should be performed to the segment
217 * position field. This field can be FALSE if, for example, only the @rate
218 * has been changed but not the playback position.
220 * Returns: %TRUE if the seek could be performed.
223 gst_segment_do_seek (GstSegment * segment, gdouble rate,
224 GstFormat format, GstSeekFlags flags,
225 GstSeekType start_type, guint64 start,
226 GstSeekType stop_type, guint64 stop, gboolean * update)
228 gboolean update_stop, update_start;
229 guint64 position, base;
231 g_return_val_if_fail (rate != 0.0, FALSE);
232 g_return_val_if_fail (segment != NULL, FALSE);
233 g_return_val_if_fail (segment->format == format, FALSE);
235 update_start = update_stop = TRUE;
237 position = segment->position;
239 if (flags & GST_SEEK_FLAG_FLUSH) {
240 /* flush resets the running_time */
243 base = gst_segment_to_running_time (segment, format, position);
246 /* segment->start is never invalid */
247 switch (start_type) {
248 case GST_SEEK_TYPE_NONE:
249 /* no update to segment, take previous start */
250 start = segment->start;
251 update_start = FALSE;
253 case GST_SEEK_TYPE_SET:
254 /* start holds desired position, map -1 to the start */
258 case GST_SEEK_TYPE_END:
259 if (segment->duration != -1) {
260 /* add start to total length */
261 start = segment->duration + start;
263 /* no update if duration unknown */
264 start = segment->start;
265 update_start = FALSE;
269 /* bring in sane range */
270 if (segment->duration != -1)
271 start = MIN (start, segment->duration);
273 start = MAX (start, 0);
275 /* stop can be -1 if we have not configured a stop. */
277 case GST_SEEK_TYPE_NONE:
278 stop = segment->stop;
281 case GST_SEEK_TYPE_SET:
282 /* stop holds required value */
284 case GST_SEEK_TYPE_END:
285 if (segment->duration != -1) {
286 stop = segment->duration + stop;
288 stop = segment->stop;
294 /* if we have a valid stop time, make sure it is clipped */
296 if (segment->duration != -1)
297 stop = CLAMP (stop, 0, segment->duration);
299 stop = MAX (stop, 0);
302 /* we can't have stop before start */
305 g_return_val_if_fail (start <= stop, FALSE);
310 segment->rate = rate;
311 segment->applied_rate = 1.0;
312 segment->base = base;
313 segment->flags = (GstSegmentFlags) flags;
314 segment->start = start;
315 segment->stop = stop;
316 segment->time = start;
318 if (update_start && rate > 0.0) {
321 if (update_stop && rate < 0.0) {
325 if (segment->duration != -1)
326 position = segment->duration;
331 /* set update arg to reflect update of position */
333 *update = position != segment->position;
335 /* update new position */
336 segment->position = position;
342 * gst_segment_to_stream_time:
343 * @segment: a #GstSegment structure.
344 * @format: the format of the segment.
345 * @position: the position in the segment
347 * Translate @position to stream time using the currently configured
348 * segment. The @position value must be between @segment start and
351 * This function is typically used by elements that need to operate on
352 * the stream time of the buffers it receives, such as effect plugins.
353 * In those use cases, @position is typically the buffer timestamp or
354 * clock time that one wants to convert to the stream time.
355 * The stream time is always between 0 and the total duration of the
358 * Returns: the position in stream_time or -1 when an invalid position
362 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
365 guint64 result, start, stop, time;
366 gdouble abs_applied_rate;
368 /* format does not matter for -1 */
369 if (G_UNLIKELY (position == -1))
372 g_return_val_if_fail (segment != NULL, -1);
373 g_return_val_if_fail (segment->format == format, -1);
375 /* if we have the position for the same format as the segment, we can compare
376 * the start and stop values, otherwise we assume 0 and -1 */
377 if (G_LIKELY (segment->format == format)) {
378 start = segment->start;
379 stop = segment->stop;
380 time = segment->time;
387 /* outside of the segment boundary stop */
388 if (G_UNLIKELY (stop != -1 && position > stop))
391 /* before the segment boundary */
392 if (G_UNLIKELY (position < start))
395 /* time must be known */
396 if (G_UNLIKELY (time == -1))
399 /* bring to uncorrected position in segment */
400 result = position - start;
402 abs_applied_rate = ABS (segment->applied_rate);
404 /* correct for applied rate if needed */
405 if (G_UNLIKELY (abs_applied_rate != 1.0))
406 result *= abs_applied_rate;
408 /* add or subtract from segment time based on applied rate */
409 if (G_LIKELY (segment->applied_rate > 0.0)) {
410 /* correct for segment time */
413 /* correct for segment time, clamp at 0. Streams with a negative
414 * applied_rate have timestamps between start and stop, as usual, but have
415 * the time member starting high and going backwards. */
416 if (G_LIKELY (time > result))
417 result = time - result;
426 * gst_segment_to_running_time:
427 * @segment: a #GstSegment structure.
428 * @format: the format of the segment.
429 * @position: the position in the segment
431 * Translate @position to the total running time using the currently configured
432 * and previously accumulated segments. Position is a value between @segment
433 * start and stop time.
435 * This function is typically used by elements that need to synchronize to the
436 * global clock in a pipeline. The runnning time is a constantly increasing value
437 * starting from 0. When gst_segment_init() is called, this value will reset to
440 * This function returns -1 if the position is outside of @segment start and stop.
442 * Returns: the position as the total running time or -1 when an invalid position
446 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
450 guint64 start, stop, base;
453 if (G_UNLIKELY (position == -1))
456 g_return_val_if_fail (segment != NULL, -1);
457 g_return_val_if_fail (segment->format == format, -1);
459 /* if we have the position for the same format as the segment, we can compare
460 * the start and stop values, otherwise we assume 0 and -1 */
461 if (G_LIKELY (segment->format == format)) {
462 start = segment->start;
463 stop = segment->stop;
464 base = segment->base;
471 /* before the segment boundary */
472 if (G_UNLIKELY (position < start))
475 if (G_LIKELY (segment->rate > 0.0)) {
476 /* outside of the segment boundary stop */
477 if (G_UNLIKELY (stop != -1 && position > stop))
480 /* bring to uncorrected position in segment */
481 result = position - start;
483 /* cannot continue if no stop position set or outside of
485 if (G_UNLIKELY (stop == -1 || position > stop))
488 /* bring to uncorrected position in segment */
489 result = stop - position;
492 /* scale based on the rate, avoid division by and conversion to
493 * float when not needed */
494 abs_rate = ABS (segment->rate);
495 if (G_UNLIKELY (abs_rate != 1.0))
498 /* correct for base of the segment */
506 * @segment: a #GstSegment structure.
507 * @format: the format of the segment.
508 * @start: the start position in the segment
509 * @stop: the stop position in the segment
510 * @clip_start: (out) (allow-none): the clipped start position in the segment
511 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
513 * Clip the given @start and @stop values to the segment boundaries given
514 * in @segment. @start and @stop are compared and clipped to @segment
515 * start and stop values.
517 * If the function returns FALSE, @start and @stop are known to fall
518 * outside of @segment and @clip_start and @clip_stop are not updated.
520 * When the function returns TRUE, @clip_start and @clip_stop will be
521 * updated. If @clip_start or @clip_stop are different from @start or @stop
522 * respectively, the region fell partially in the segment.
524 * Note that when @stop is -1, @clip_stop will be set to the end of the
525 * segment. Depending on the use case, this may or may not be what you want.
527 * Returns: TRUE if the given @start and @stop times fall partially or
528 * completely in @segment, FALSE if the values are completely outside
532 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
533 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
535 g_return_val_if_fail (segment != NULL, FALSE);
536 g_return_val_if_fail (segment->format == format, FALSE);
538 /* if we have a stop position and a valid start and start is bigger,
539 * we're outside of the segment */
540 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
543 /* if a stop position is given and is before the segment start,
544 * we're outside of the segment. Special case is were start
545 * and stop are equal to the segment start. In that case we
546 * are inside the segment. */
547 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
548 && stop == segment->start))))
555 *clip_start = MAX (start, segment->start);
560 *clip_stop = segment->stop;
561 else if (segment->stop == -1)
564 *clip_stop = MIN (stop, segment->stop);
566 if (segment->duration != -1 && *clip_stop != -1)
567 *clip_stop = MIN (*clip_stop, segment->duration);
574 * gst_segment_to_position:
575 * @segment: a #GstSegment structure.
576 * @format: the format of the segment.
577 * @running_time: the running_time in the segment
579 * Convert @running_time into a position in the segment so that
580 * gst_segment_to_running_time() with that position returns @running_time.
582 * Returns: the position in the segment for @running_time. This function returns
583 * -1 when @running_time is -1 or when it is not inside @segment.
588 gst_segment_to_position (const GstSegment * segment, GstFormat format,
589 guint64 running_time)
592 guint64 start, stop, base;
595 if (G_UNLIKELY (running_time == -1))
598 g_return_val_if_fail (segment != NULL, -1);
599 g_return_val_if_fail (segment->format == format, FALSE);
601 /* if we have the position for the same format as the segment, we can compare
602 * the start and stop values, otherwise we assume 0 and -1 */
603 if (G_LIKELY (segment->format == format)) {
604 start = segment->start;
605 stop = segment->stop;
606 base = segment->base;
613 /* this running_time was for a previous segment */
614 if (running_time < base)
617 /* start by subtracting the base time */
618 result = running_time - base;
620 /* move into the segment at the right rate */
621 abs_rate = ABS (segment->rate);
622 if (G_UNLIKELY (abs_rate != 1.0))
623 result = ceil (result * abs_rate);
625 if (G_LIKELY (segment->rate > 0.0)) {
626 /* bring to corrected position in segment */
629 /* outside of the segment boundary stop */
630 if (G_UNLIKELY (stop != -1 && result > stop))
633 /* cannot continue if no stop position set or outside of
635 if (G_UNLIKELY (stop == -1 || result + start > stop))
638 /* bring to corrected position in segment */
639 result = stop - result;
646 * gst_segment_set_running_time:
647 * @segment: a #GstSegment structure.
648 * @format: the format of the segment.
649 * @running_time: the running_time in the segment
651 * Adjust the start/stop and base values of @segment such that the next valid
652 * buffer will be one with @running_time.
654 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
655 * returned, @running_time is -1 or not in @segment.
660 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
661 guint64 running_time)
666 /* start by bringing the running_time into the segment position */
667 position = gst_segment_to_position (segment, format, running_time);
669 /* we must have a valid position now */
670 if (G_UNLIKELY (position == -1))
673 start = segment->start;
674 stop = segment->stop;
676 if (G_LIKELY (segment->rate > 0.0)) {
677 /* update the start and time values */
680 /* reverse, update stop */
683 /* and base time is exactly the running time */
684 segment->time = gst_segment_to_stream_time (segment, format, start);
685 segment->start = start;
686 segment->stop = stop;
687 segment->base = running_time;