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 segment->flags = (GstSegmentFlags) flags;
321 segment->start = start;
322 segment->stop = stop;
323 segment->time = start;
325 if (update_start && rate > 0.0) {
328 if (update_stop && rate < 0.0) {
332 if (segment->duration != -1)
333 position = segment->duration;
338 /* set update arg to reflect update of position */
340 *update = position != segment->position;
342 /* update new position */
343 segment->position = position;
349 * gst_segment_to_stream_time:
350 * @segment: a #GstSegment structure.
351 * @format: the format of the segment.
352 * @position: the position in the segment
354 * Translate @position to stream time using the currently configured
355 * segment. The @position value must be between @segment start and
358 * This function is typically used by elements that need to operate on
359 * the stream time of the buffers it receives, such as effect plugins.
360 * In those use cases, @position is typically the buffer timestamp or
361 * clock time that one wants to convert to the stream time.
362 * The stream time is always between 0 and the total duration of the
365 * Returns: the position in stream_time or -1 when an invalid position
369 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
372 guint64 result, start, stop, time;
373 gdouble abs_applied_rate;
375 /* format does not matter for -1 */
376 if (G_UNLIKELY (position == -1))
379 g_return_val_if_fail (segment != NULL, -1);
380 g_return_val_if_fail (segment->format == format, -1);
382 /* if we have the position for the same format as the segment, we can compare
383 * the start and stop values, otherwise we assume 0 and -1 */
384 if (G_LIKELY (segment->format == format)) {
385 start = segment->start;
386 stop = segment->stop;
387 time = segment->time;
394 /* outside of the segment boundary stop */
395 if (G_UNLIKELY (stop != -1 && position > stop))
398 /* before the segment boundary */
399 if (G_UNLIKELY (position < start))
402 /* time must be known */
403 if (G_UNLIKELY (time == -1))
406 /* bring to uncorrected position in segment */
407 result = position - start;
409 abs_applied_rate = ABS (segment->applied_rate);
411 /* correct for applied rate if needed */
412 if (G_UNLIKELY (abs_applied_rate != 1.0))
413 result *= abs_applied_rate;
415 /* add or subtract from segment time based on applied rate */
416 if (G_LIKELY (segment->applied_rate > 0.0)) {
417 /* correct for segment time */
420 /* correct for segment time, clamp at 0. Streams with a negative
421 * applied_rate have timestamps between start and stop, as usual, but have
422 * the time member starting high and going backwards. */
423 if (G_LIKELY (time > result))
424 result = time - result;
433 * gst_segment_to_running_time:
434 * @segment: a #GstSegment structure.
435 * @format: the format of the segment.
436 * @position: the position in the segment
438 * Translate @position to the total running time using the currently configured
439 * and previously accumulated segments. Position is a value between @segment
440 * start and stop time.
442 * This function is typically used by elements that need to synchronize to the
443 * global clock in a pipeline. The runnning time is a constantly increasing value
444 * starting from 0. When gst_segment_init() is called, this value will reset to
447 * This function returns -1 if the position is outside of @segment start and stop.
449 * Returns: the position as the total running time or -1 when an invalid position
453 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
457 guint64 start, stop, base;
460 if (G_UNLIKELY (position == -1))
463 g_return_val_if_fail (segment != NULL, -1);
464 g_return_val_if_fail (segment->format == format, -1);
466 /* if we have the position for the same format as the segment, we can compare
467 * the start and stop values, otherwise we assume 0 and -1 */
468 if (G_LIKELY (segment->format == format)) {
469 start = segment->start;
470 stop = segment->stop;
471 base = segment->base;
478 /* before the segment boundary */
479 if (G_UNLIKELY (position < start))
482 if (G_LIKELY (segment->rate > 0.0)) {
483 /* outside of the segment boundary stop */
484 if (G_UNLIKELY (stop != -1 && position > stop))
487 /* bring to uncorrected position in segment */
488 result = position - start;
490 /* cannot continue if no stop position set or outside of
492 if (G_UNLIKELY (stop == -1 || position > stop))
495 /* bring to uncorrected position in segment */
496 result = stop - position;
499 /* scale based on the rate, avoid division by and conversion to
500 * float when not needed */
501 abs_rate = ABS (segment->rate);
502 if (G_UNLIKELY (abs_rate != 1.0))
505 /* correct for base of the segment */
513 * @segment: a #GstSegment structure.
514 * @format: the format of the segment.
515 * @start: the start position in the segment
516 * @stop: the stop position in the segment
517 * @clip_start: (out) (allow-none): the clipped start position in the segment
518 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
520 * Clip the given @start and @stop values to the segment boundaries given
521 * in @segment. @start and @stop are compared and clipped to @segment
522 * start and stop values.
524 * If the function returns FALSE, @start and @stop are known to fall
525 * outside of @segment and @clip_start and @clip_stop are not updated.
527 * When the function returns TRUE, @clip_start and @clip_stop will be
528 * updated. If @clip_start or @clip_stop are different from @start or @stop
529 * respectively, the region fell partially in the segment.
531 * Note that when @stop is -1, @clip_stop will be set to the end of the
532 * segment. Depending on the use case, this may or may not be what you want.
534 * Returns: TRUE if the given @start and @stop times fall partially or
535 * completely in @segment, FALSE if the values are completely outside
539 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
540 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
542 g_return_val_if_fail (segment != NULL, FALSE);
543 g_return_val_if_fail (segment->format == format, FALSE);
545 /* if we have a stop position and a valid start and start is bigger,
546 * we're outside of the segment */
547 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
550 /* if a stop position is given and is before the segment start,
551 * we're outside of the segment. Special case is were start
552 * and stop are equal to the segment start. In that case we
553 * are inside the segment. */
554 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
555 && stop == segment->start))))
562 *clip_start = MAX (start, segment->start);
567 *clip_stop = segment->stop;
568 else if (segment->stop == -1)
571 *clip_stop = MIN (stop, segment->stop);
578 * gst_segment_to_position:
579 * @segment: a #GstSegment structure.
580 * @format: the format of the segment.
581 * @running_time: the running_time in the segment
583 * Convert @running_time into a position in the segment so that
584 * gst_segment_to_running_time() with that position returns @running_time.
586 * Returns: the position in the segment for @running_time. This function returns
587 * -1 when @running_time is -1 or when it is not inside @segment.
592 gst_segment_to_position (const GstSegment * segment, GstFormat format,
593 guint64 running_time)
596 guint64 start, stop, base;
599 if (G_UNLIKELY (running_time == -1))
602 g_return_val_if_fail (segment != NULL, -1);
603 g_return_val_if_fail (segment->format == format, FALSE);
605 /* if we have the position for the same format as the segment, we can compare
606 * the start and stop values, otherwise we assume 0 and -1 */
607 if (G_LIKELY (segment->format == format)) {
608 start = segment->start;
609 stop = segment->stop;
610 base = segment->base;
617 /* this running_time was for a previous segment */
618 if (running_time < base)
621 /* start by subtracting the base time */
622 result = running_time - base;
624 /* move into the segment at the right rate */
625 abs_rate = ABS (segment->rate);
626 if (G_UNLIKELY (abs_rate != 1.0))
627 result = ceil (result * abs_rate);
629 if (G_LIKELY (segment->rate > 0.0)) {
630 /* bring to corrected position in segment */
633 /* outside of the segment boundary stop */
634 if (G_UNLIKELY (stop != -1 && result > stop))
637 /* cannot continue if no stop position set or outside of
639 if (G_UNLIKELY (stop == -1 || result + start > stop))
642 /* bring to corrected position in segment */
643 result = stop - result;
650 * gst_segment_set_running_time:
651 * @segment: a #GstSegment structure.
652 * @format: the format of the segment.
653 * @running_time: the running_time in the segment
655 * Adjust the start/stop and base values of @segment such that the next valid
656 * buffer will be one with @running_time.
658 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
659 * 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;