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));
117 gst_segment_get_type (void)
119 static GType gst_segment_type = 0;
121 if (G_UNLIKELY (gst_segment_type == 0)) {
122 gst_segment_type = g_boxed_type_register_static ("GstSegment",
123 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
126 return gst_segment_type;
132 * Allocate a new #GstSegment structure and initialize it using
133 * gst_segment_init().
135 * Free-function: gst_segment_free
137 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
140 gst_segment_new (void)
144 result = g_slice_new0 (GstSegment);
145 gst_segment_init (result, GST_FORMAT_UNDEFINED);
152 * @segment: (in) (transfer full): a #GstSegment
154 * Free the allocated segment @segment.
157 gst_segment_free (GstSegment * segment)
159 g_slice_free (GstSegment, segment);
164 * @segment: a #GstSegment structure.
165 * @format: the format of the segment.
167 * The start/last_stop positions are set to 0 and the stop/duration
168 * fields are set to -1 (unknown). The default rate of 1.0 and no
171 * Initialize @segment to its default values.
174 gst_segment_init (GstSegment * segment, GstFormat format)
176 g_return_if_fail (segment != NULL);
178 segment->flags = GST_SEEK_FLAG_NONE;
180 segment->applied_rate = 1.0;
181 segment->format = format;
186 segment->position = 0;
187 segment->duration = -1;
191 * gst_segment_do_seek:
192 * @segment: a #GstSegment structure.
193 * @rate: the rate of the segment.
194 * @format: the format of the segment.
195 * @flags: the segment flags for the segment
196 * @start_type: the seek method
197 * @start: the seek start value
198 * @stop_type: the seek method
199 * @stop: the seek stop value
200 * @update: boolean holding whether position was updated.
202 * Update the segment structure with the field values of a seek event (see
203 * gst_event_new_seek()).
205 * After calling this method, the segment field position and time will
206 * contain the requested new position in the segment. The new requested
207 * position in the segment depends on @rate and @start_type and @stop_type.
209 * For positive @rate, the new position in the segment is the new @segment
210 * start field when it was updated with a @start_type different from
211 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
212 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
215 * For negative @rate, the new position in the segment is the new @segment
216 * stop field when it was updated with a @stop_type different from
217 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
218 * duration of the segment will be used to update the stop position.
219 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
220 * @stop is ignored and @segment position is unmodified.
222 * The applied rate of the segment will be set to 1.0 by default.
223 * If the caller can apply a rate change, it should update @segment
224 * rate and applied_rate after calling this function.
226 * @update will be set to TRUE if a seek should be performed to the segment
227 * position field. This field can be FALSE if, for example, only the @rate
228 * has been changed but not the playback position.
230 * Returns: %TRUE if the seek could be performed.
233 gst_segment_do_seek (GstSegment * segment, gdouble rate,
234 GstFormat format, GstSeekFlags flags,
235 GstSeekType start_type, guint64 start,
236 GstSeekType stop_type, guint64 stop, gboolean * update)
238 gboolean update_stop, update_start;
239 guint64 position, base;
241 g_return_val_if_fail (rate != 0.0, FALSE);
242 g_return_val_if_fail (segment != NULL, FALSE);
243 g_return_val_if_fail (segment->format == format, FALSE);
245 update_start = update_stop = TRUE;
247 position = segment->position;
249 if (flags & GST_SEEK_FLAG_FLUSH) {
250 /* flush resets the running_time */
253 base = gst_segment_to_running_time (segment, format, position);
256 /* segment->start is never invalid */
257 switch (start_type) {
258 case GST_SEEK_TYPE_NONE:
259 /* no update to segment, take previous start */
260 start = segment->start;
261 update_start = FALSE;
263 case GST_SEEK_TYPE_SET:
264 /* start holds desired position, map -1 to the start */
268 case GST_SEEK_TYPE_END:
269 if (segment->duration != -1) {
270 /* add start to total length */
271 start = segment->duration + start;
273 /* no update if duration unknown */
274 start = segment->start;
275 update_start = FALSE;
279 /* bring in sane range */
280 if (segment->duration != -1)
281 start = MIN (start, segment->duration);
283 start = MAX (start, 0);
285 /* stop can be -1 if we have not configured a stop. */
287 case GST_SEEK_TYPE_NONE:
288 stop = segment->stop;
291 case GST_SEEK_TYPE_SET:
292 /* stop holds required value */
294 case GST_SEEK_TYPE_END:
295 if (segment->duration != -1) {
296 stop = segment->duration + stop;
298 stop = segment->stop;
304 /* if we have a valid stop time, make sure it is clipped */
306 if (segment->duration != -1)
307 stop = CLAMP (stop, 0, segment->duration);
309 stop = MAX (stop, 0);
312 /* we can't have stop before start */
315 g_return_val_if_fail (start <= stop, FALSE);
320 segment->rate = rate;
321 segment->applied_rate = 1.0;
322 segment->base = base;
323 segment->flags = (GstSegmentFlags) flags;
324 segment->start = start;
325 segment->stop = stop;
326 segment->time = start;
328 if (update_start && rate > 0.0) {
331 if (update_stop && rate < 0.0) {
335 if (segment->duration != -1)
336 position = segment->duration;
341 /* set update arg to reflect update of position */
343 *update = position != segment->position;
345 /* update new position */
346 segment->position = position;
352 * gst_segment_to_stream_time:
353 * @segment: a #GstSegment structure.
354 * @format: the format of the segment.
355 * @position: the position in the segment
357 * Translate @position to stream time using the currently configured
358 * segment. The @position value must be between @segment start and
361 * This function is typically used by elements that need to operate on
362 * the stream time of the buffers it receives, such as effect plugins.
363 * In those use cases, @position is typically the buffer timestamp or
364 * clock time that one wants to convert to the stream time.
365 * The stream time is always between 0 and the total duration of the
368 * Returns: the position in stream_time or -1 when an invalid position
372 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
375 guint64 result, start, stop, time;
376 gdouble abs_applied_rate;
378 /* format does not matter for -1 */
379 if (G_UNLIKELY (position == -1))
382 g_return_val_if_fail (segment != NULL, -1);
383 g_return_val_if_fail (segment->format == format, -1);
385 /* if we have the position for the same format as the segment, we can compare
386 * the start and stop values, otherwise we assume 0 and -1 */
387 if (G_LIKELY (segment->format == format)) {
388 start = segment->start;
389 stop = segment->stop;
390 time = segment->time;
397 /* outside of the segment boundary stop */
398 if (G_UNLIKELY (stop != -1 && position > stop))
401 /* before the segment boundary */
402 if (G_UNLIKELY (position < start))
405 /* time must be known */
406 if (G_UNLIKELY (time == -1))
409 /* bring to uncorrected position in segment */
410 result = position - start;
412 abs_applied_rate = ABS (segment->applied_rate);
414 /* correct for applied rate if needed */
415 if (G_UNLIKELY (abs_applied_rate != 1.0))
416 result *= abs_applied_rate;
418 /* add or subtract from segment time based on applied rate */
419 if (G_LIKELY (segment->applied_rate > 0.0)) {
420 /* correct for segment time */
423 /* correct for segment time, clamp at 0. Streams with a negative
424 * applied_rate have timestamps between start and stop, as usual, but have
425 * the time member starting high and going backwards. */
426 if (G_LIKELY (time > result))
427 result = time - result;
436 * gst_segment_to_running_time:
437 * @segment: a #GstSegment structure.
438 * @format: the format of the segment.
439 * @position: the position in the segment
441 * Translate @position to the total running time using the currently configured
442 * and previously accumulated segments. Position is a value between @segment
443 * start and stop time.
445 * This function is typically used by elements that need to synchronize to the
446 * global clock in a pipeline. The runnning time is a constantly increasing value
447 * starting from 0. When gst_segment_init() is called, this value will reset to
450 * This function returns -1 if the position is outside of @segment start and stop.
452 * Returns: the position as the total running time or -1 when an invalid position
456 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
460 guint64 start, stop, base;
463 if (G_UNLIKELY (position == -1))
466 g_return_val_if_fail (segment != NULL, -1);
467 g_return_val_if_fail (segment->format == format, -1);
469 /* if we have the position for the same format as the segment, we can compare
470 * the start and stop values, otherwise we assume 0 and -1 */
471 if (G_LIKELY (segment->format == format)) {
472 start = segment->start;
473 stop = segment->stop;
474 base = segment->base;
481 /* before the segment boundary */
482 if (G_UNLIKELY (position < start))
485 if (G_LIKELY (segment->rate > 0.0)) {
486 /* outside of the segment boundary stop */
487 if (G_UNLIKELY (stop != -1 && position > stop))
490 /* bring to uncorrected position in segment */
491 result = position - start;
493 /* cannot continue if no stop position set or outside of
495 if (G_UNLIKELY (stop == -1 || position > stop))
498 /* bring to uncorrected position in segment */
499 result = stop - position;
502 /* scale based on the rate, avoid division by and conversion to
503 * float when not needed */
504 abs_rate = ABS (segment->rate);
505 if (G_UNLIKELY (abs_rate != 1.0))
508 /* correct for base of the segment */
516 * @segment: a #GstSegment structure.
517 * @format: the format of the segment.
518 * @start: the start position in the segment
519 * @stop: the stop position in the segment
520 * @clip_start: (out) (allow-none): the clipped start position in the segment
521 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
523 * Clip the given @start and @stop values to the segment boundaries given
524 * in @segment. @start and @stop are compared and clipped to @segment
525 * start and stop values.
527 * If the function returns FALSE, @start and @stop are known to fall
528 * outside of @segment and @clip_start and @clip_stop are not updated.
530 * When the function returns TRUE, @clip_start and @clip_stop will be
531 * updated. If @clip_start or @clip_stop are different from @start or @stop
532 * respectively, the region fell partially in the segment.
534 * Note that when @stop is -1, @clip_stop will be set to the end of the
535 * segment. Depending on the use case, this may or may not be what you want.
537 * Returns: TRUE if the given @start and @stop times fall partially or
538 * completely in @segment, FALSE if the values are completely outside
542 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
543 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
545 g_return_val_if_fail (segment != NULL, FALSE);
546 g_return_val_if_fail (segment->format == format, FALSE);
548 /* if we have a stop position and a valid start and start is bigger,
549 * we're outside of the segment */
550 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
553 /* if a stop position is given and is before the segment start,
554 * we're outside of the segment. Special case is were start
555 * and stop are equal to the segment start. In that case we
556 * are inside the segment. */
557 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
558 && stop == segment->start))))
565 *clip_start = MAX (start, segment->start);
570 *clip_stop = segment->stop;
571 else if (segment->stop == -1)
574 *clip_stop = MIN (stop, segment->stop);
576 if (segment->duration != -1 && *clip_stop != -1)
577 *clip_stop = MIN (*clip_stop, segment->duration);
584 * gst_segment_to_position:
585 * @segment: a #GstSegment structure.
586 * @format: the format of the segment.
587 * @running_time: the running_time in the segment
589 * Convert @running_time into a position in the segment so that
590 * gst_segment_to_running_time() with that position returns @running_time.
592 * Returns: the position in the segment for @running_time. This function returns
593 * -1 when @running_time is -1 or when it is not inside @segment.
598 gst_segment_to_position (const GstSegment * segment, GstFormat format,
599 guint64 running_time)
602 guint64 start, stop, base;
605 if (G_UNLIKELY (running_time == -1))
608 g_return_val_if_fail (segment != NULL, -1);
609 g_return_val_if_fail (segment->format == format, FALSE);
611 /* if we have the position for the same format as the segment, we can compare
612 * the start and stop values, otherwise we assume 0 and -1 */
613 if (G_LIKELY (segment->format == format)) {
614 start = segment->start;
615 stop = segment->stop;
616 base = segment->base;
623 /* this running_time was for a previous segment */
624 if (running_time < base)
627 /* start by subtracting the base time */
628 result = running_time - base;
630 /* move into the segment at the right rate */
631 abs_rate = ABS (segment->rate);
632 if (G_UNLIKELY (abs_rate != 1.0))
633 result = ceil (result * abs_rate);
635 if (G_LIKELY (segment->rate > 0.0)) {
636 /* bring to corrected position in segment */
639 /* outside of the segment boundary stop */
640 if (G_UNLIKELY (stop != -1 && result > stop))
643 /* cannot continue if no stop position set or outside of
645 if (G_UNLIKELY (stop == -1 || result + start > stop))
648 /* bring to corrected position in segment */
649 result = stop - result;
656 * gst_segment_set_running_time:
657 * @segment: a #GstSegment structure.
658 * @format: the format of the segment.
659 * @running_time: the running_time in the segment
661 * Adjust the start/stop and base values of @segment such that the next valid
662 * buffer will be one with @running_time.
664 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
665 * returned, @running_time is -1 or not in @segment.
670 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
671 guint64 running_time)
676 /* start by bringing the running_time into the segment position */
677 position = gst_segment_to_position (segment, format, running_time);
679 /* we must have a valid position now */
680 if (G_UNLIKELY (position == -1))
683 start = segment->start;
684 stop = segment->stop;
686 if (G_LIKELY (segment->rate > 0.0)) {
687 /* update the start and time values */
690 /* reverse, update stop */
693 /* and base time is exactly the running time */
694 segment->time = gst_segment_to_stream_time (segment, format, start);
695 segment->start = start;
696 segment->stop = stop;
697 segment->base = running_time;