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 by changing the position
62 * member in the structure.
64 * For elements that perform seeks, the current segment should be updated with the
65 * gst_segment_do_seek() and the values from the seek event. This method will update
66 * all the segment fields. The position field will contain the new playback position.
67 * If the cur_type was different from GST_SEEK_TYPE_NONE, playback continues from
68 * the position position, possibly with updated flags or rate.
70 * For elements that want to use #GstSegment to track the playback region,
71 * update the segment fields with the information from the newsegment event.
72 * 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 the base 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 2012-03-29 (0.11.3)
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:
112 * @src: (transfer none): a #GstSegment
113 * @dest: (transfer none): a #GstSegment
115 * Copy the contents of @src into @dest.
118 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
120 memcpy (dest, src, sizeof (GstSegment));
123 G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
124 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
129 * Allocate a new #GstSegment structure and initialize it using
130 * gst_segment_init().
132 * Free-function: gst_segment_free
134 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
137 gst_segment_new (void)
141 result = g_slice_new0 (GstSegment);
142 gst_segment_init (result, GST_FORMAT_UNDEFINED);
149 * @segment: (in) (transfer full): a #GstSegment
151 * Free the allocated segment @segment.
154 gst_segment_free (GstSegment * segment)
156 g_slice_free (GstSegment, segment);
161 * @segment: a #GstSegment structure.
162 * @format: the format of the segment.
164 * The start/last_stop positions are set to 0 and the stop/duration
165 * fields are set to -1 (unknown). The default rate of 1.0 and no
168 * Initialize @segment to its default values.
171 gst_segment_init (GstSegment * segment, GstFormat format)
173 g_return_if_fail (segment != NULL);
175 segment->flags = GST_SEGMENT_FLAG_NONE;
177 segment->applied_rate = 1.0;
178 segment->format = format;
183 segment->position = 0;
184 segment->duration = -1;
188 * gst_segment_do_seek:
189 * @segment: a #GstSegment structure.
190 * @rate: the rate of the segment.
191 * @format: the format of the segment.
192 * @flags: the segment flags for the segment
193 * @start_type: the seek method
194 * @start: the seek start value
195 * @stop_type: the seek method
196 * @stop: the seek stop value
197 * @update: boolean holding whether position was updated.
199 * Update the segment structure with the field values of a seek event (see
200 * gst_event_new_seek()).
202 * After calling this method, the segment field position and time will
203 * contain the requested new position in the segment. The new requested
204 * position in the segment depends on @rate and @start_type and @stop_type.
206 * For positive @rate, the new position in the segment is the new @segment
207 * start field when it was updated with a @start_type different from
208 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
209 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
212 * For negative @rate, the new position in the segment is the new @segment
213 * stop field when it was updated with a @stop_type different from
214 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
215 * duration of the segment will be used to update the stop position.
216 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
217 * @stop is ignored and @segment position is unmodified.
219 * The applied rate of the segment will be set to 1.0 by default.
220 * If the caller can apply a rate change, it should update @segment
221 * rate and applied_rate after calling this function.
223 * @update will be set to TRUE if a seek should be performed to the segment
224 * position field. This field can be FALSE if, for example, only the @rate
225 * has been changed but not the playback position.
227 * Returns: %TRUE if the seek could be performed.
230 gst_segment_do_seek (GstSegment * segment, gdouble rate,
231 GstFormat format, GstSeekFlags flags,
232 GstSeekType start_type, guint64 start,
233 GstSeekType stop_type, guint64 stop, gboolean * update)
235 gboolean update_stop, update_start;
236 guint64 position, base;
238 g_return_val_if_fail (rate != 0.0, FALSE);
239 g_return_val_if_fail (segment != NULL, FALSE);
240 g_return_val_if_fail (segment->format == format, FALSE);
242 update_start = update_stop = TRUE;
244 position = segment->position;
246 if (flags & GST_SEEK_FLAG_FLUSH) {
247 /* flush resets the running_time */
250 base = gst_segment_to_running_time (segment, format, position);
253 /* segment->start is never invalid */
254 switch (start_type) {
255 case GST_SEEK_TYPE_NONE:
256 /* no update to segment, take previous start */
257 start = segment->start;
258 update_start = FALSE;
260 case GST_SEEK_TYPE_SET:
261 /* start holds desired position, map -1 to the start */
265 case GST_SEEK_TYPE_END:
266 if (segment->duration != -1) {
267 /* add start to total length */
268 start = segment->duration + start;
270 /* no update if duration unknown */
271 start = segment->start;
272 update_start = FALSE;
276 /* bring in sane range */
277 if (segment->duration != -1)
278 start = MIN (start, segment->duration);
280 start = MAX (start, 0);
282 /* stop can be -1 if we have not configured a stop. */
284 case GST_SEEK_TYPE_NONE:
285 stop = segment->stop;
288 case GST_SEEK_TYPE_SET:
289 /* stop holds required value */
291 case GST_SEEK_TYPE_END:
292 if (segment->duration != -1) {
293 stop = segment->duration + stop;
295 stop = segment->stop;
301 /* if we have a valid stop time, make sure it is clipped */
303 if (segment->duration != -1)
304 stop = CLAMP (stop, 0, segment->duration);
306 stop = MAX (stop, 0);
309 /* we can't have stop before start */
312 g_return_val_if_fail (start <= stop, FALSE);
317 segment->rate = rate;
318 segment->applied_rate = 1.0;
319 segment->base = base;
320 /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
321 segment->flags = GST_SEGMENT_FLAG_NONE;
322 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
323 segment->flags |= GST_SEGMENT_FLAG_RESET;
324 if ((flags & GST_SEEK_FLAG_SKIP) != 0)
325 segment->flags |= GST_SEGMENT_FLAG_SKIP;
326 if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
327 segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
328 segment->start = start;
329 segment->stop = stop;
330 segment->time = start;
332 if (update_start && rate > 0.0) {
335 if (update_stop && rate < 0.0) {
339 if (segment->duration != -1)
340 position = segment->duration;
345 /* set update arg to reflect update of position */
347 *update = position != segment->position;
349 /* update new position */
350 segment->position = position;
356 * gst_segment_to_stream_time:
357 * @segment: a #GstSegment structure.
358 * @format: the format of the segment.
359 * @position: the position in the segment
361 * Translate @position to stream time using the currently configured
362 * segment. The @position value must be between @segment start and
365 * This function is typically used by elements that need to operate on
366 * the stream time of the buffers it receives, such as effect plugins.
367 * In those use cases, @position is typically the buffer timestamp or
368 * clock time that one wants to convert to the stream time.
369 * The stream time is always between 0 and the total duration of the
372 * Returns: the position in stream_time or -1 when an invalid position
376 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
379 guint64 result, start, stop, time;
380 gdouble abs_applied_rate;
382 /* format does not matter for -1 */
383 if (G_UNLIKELY (position == -1))
386 g_return_val_if_fail (segment != NULL, -1);
387 g_return_val_if_fail (segment->format == format, -1);
389 /* if we have the position for the same format as the segment, we can compare
390 * the start and stop values, otherwise we assume 0 and -1 */
391 if (G_LIKELY (segment->format == format)) {
392 start = segment->start;
393 stop = segment->stop;
394 time = segment->time;
401 /* outside of the segment boundary stop */
402 if (G_UNLIKELY (stop != -1 && position > stop))
405 /* before the segment boundary */
406 if (G_UNLIKELY (position < start))
409 /* time must be known */
410 if (G_UNLIKELY (time == -1))
413 /* bring to uncorrected position in segment */
414 result = position - start;
416 abs_applied_rate = ABS (segment->applied_rate);
418 /* correct for applied rate if needed */
419 if (G_UNLIKELY (abs_applied_rate != 1.0))
420 result *= abs_applied_rate;
422 /* add or subtract from segment time based on applied rate */
423 if (G_LIKELY (segment->applied_rate > 0.0)) {
424 /* correct for segment time */
427 /* correct for segment time, clamp at 0. Streams with a negative
428 * applied_rate have timestamps between start and stop, as usual, but have
429 * the time member starting high and going backwards. */
430 if (G_LIKELY (time > result))
431 result = time - result;
440 * gst_segment_to_running_time:
441 * @segment: a #GstSegment structure.
442 * @format: the format of the segment.
443 * @position: the position in the segment
445 * Translate @position to the total running time using the currently configured
446 * and previously accumulated segments. Position is a value between @segment
447 * start and stop time.
449 * This function is typically used by elements that need to synchronize to the
450 * global clock in a pipeline. The runnning time is a constantly increasing value
451 * starting from 0. When gst_segment_init() is called, this value will reset to
454 * This function returns -1 if the position is outside of @segment start and stop.
456 * Returns: the position as the total running time or -1 when an invalid position
460 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
464 guint64 start, stop, base;
467 if (G_UNLIKELY (position == -1))
470 g_return_val_if_fail (segment != NULL, -1);
471 g_return_val_if_fail (segment->format == format, -1);
473 /* if we have the position for the same format as the segment, we can compare
474 * the start and stop values, otherwise we assume 0 and -1 */
475 if (G_LIKELY (segment->format == format)) {
476 start = segment->start;
477 stop = segment->stop;
478 base = segment->base;
485 /* before the segment boundary */
486 if (G_UNLIKELY (position < start))
489 if (G_LIKELY (segment->rate > 0.0)) {
490 /* outside of the segment boundary stop */
491 if (G_UNLIKELY (stop != -1 && position > stop))
494 /* bring to uncorrected position in segment */
495 result = position - start;
497 /* cannot continue if no stop position set or outside of
499 if (G_UNLIKELY (stop == -1 || position > stop))
502 /* bring to uncorrected position in segment */
503 result = stop - position;
506 /* scale based on the rate, avoid division by and conversion to
507 * float when not needed */
508 abs_rate = ABS (segment->rate);
509 if (G_UNLIKELY (abs_rate != 1.0))
512 /* correct for base of the segment */
520 * @segment: a #GstSegment structure.
521 * @format: the format of the segment.
522 * @start: the start position in the segment
523 * @stop: the stop position in the segment
524 * @clip_start: (out) (allow-none): the clipped start position in the segment
525 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
527 * Clip the given @start and @stop values to the segment boundaries given
528 * in @segment. @start and @stop are compared and clipped to @segment
529 * start and stop values.
531 * If the function returns FALSE, @start and @stop are known to fall
532 * outside of @segment and @clip_start and @clip_stop are not updated.
534 * When the function returns TRUE, @clip_start and @clip_stop will be
535 * updated. If @clip_start or @clip_stop are different from @start or @stop
536 * respectively, the region fell partially in the segment.
538 * Note that when @stop is -1, @clip_stop will be set to the end of the
539 * segment. Depending on the use case, this may or may not be what you want.
541 * Returns: TRUE if the given @start and @stop times fall partially or
542 * completely in @segment, FALSE if the values are completely outside
546 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
547 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
549 g_return_val_if_fail (segment != NULL, FALSE);
550 g_return_val_if_fail (segment->format == format, FALSE);
552 /* if we have a stop position and a valid start and start is bigger,
553 * we're outside of the segment */
554 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
557 /* if a stop position is given and is before the segment start,
558 * we're outside of the segment. Special case is were start
559 * and stop are equal to the segment start. In that case we
560 * are inside the segment. */
561 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
562 && stop == segment->start))))
569 *clip_start = MAX (start, segment->start);
574 *clip_stop = segment->stop;
575 else if (segment->stop == -1)
578 *clip_stop = MIN (stop, segment->stop);
585 * gst_segment_to_position:
586 * @segment: a #GstSegment structure.
587 * @format: the format of the segment.
588 * @running_time: the running_time in the segment
590 * Convert @running_time into a position in the segment so that
591 * gst_segment_to_running_time() with that position returns @running_time.
593 * Returns: the position in the segment for @running_time. This function returns
594 * -1 when @running_time is -1 or when it is not inside @segment.
599 gst_segment_to_position (const GstSegment * segment, GstFormat format,
600 guint64 running_time)
603 guint64 start, stop, base;
606 if (G_UNLIKELY (running_time == -1))
609 g_return_val_if_fail (segment != NULL, -1);
610 g_return_val_if_fail (segment->format == format, FALSE);
612 /* if we have the position for the same format as the segment, we can compare
613 * the start and stop values, otherwise we assume 0 and -1 */
614 if (G_LIKELY (segment->format == format)) {
615 start = segment->start;
616 stop = segment->stop;
617 base = segment->base;
624 /* this running_time was for a previous segment */
625 if (running_time < base)
628 /* start by subtracting the base time */
629 result = running_time - base;
631 /* move into the segment at the right rate */
632 abs_rate = ABS (segment->rate);
633 if (G_UNLIKELY (abs_rate != 1.0))
634 result = ceil (result * abs_rate);
636 if (G_LIKELY (segment->rate > 0.0)) {
637 /* bring to corrected position in segment */
640 /* outside of the segment boundary stop */
641 if (G_UNLIKELY (stop != -1 && result > stop))
644 /* cannot continue if no stop position set or outside of
646 if (G_UNLIKELY (stop == -1 || result + start > stop))
649 /* bring to corrected position in segment */
650 result = stop - result;
657 * gst_segment_set_running_time:
658 * @segment: a #GstSegment structure.
659 * @format: the format of the segment.
660 * @running_time: the running_time in the segment
662 * Adjust the start/stop and base values of @segment such that the next valid
663 * buffer will be one with @running_time.
665 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
666 * returned, @running_time is -1 or not in @segment.
671 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
672 guint64 running_time)
677 /* start by bringing the running_time into the segment position */
678 position = gst_segment_to_position (segment, format, running_time);
680 /* we must have a valid position now */
681 if (G_UNLIKELY (position == -1))
684 start = segment->start;
685 stop = segment->stop;
687 if (G_LIKELY (segment->rate > 0.0)) {
688 /* update the start and time values */
691 /* reverse, update stop */
694 /* and base time is exactly the running time */
695 segment->time = gst_segment_to_stream_time (segment, format, start);
696 segment->start = start;
697 segment->stop = stop;
698 segment->base = running_time;