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().
98 gst_segment_copy (const GstSegment * segment)
100 GstSegment *result = NULL;
103 result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
109 * gst_segment_copy_into:
110 * @src: (transfer none): a #GstSegment
111 * @dest: (transfer none): a #GstSegment
113 * Copy the contents of @src into @dest.
116 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
118 memcpy (dest, src, sizeof (GstSegment));
121 G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
122 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
127 * Allocate a new #GstSegment structure and initialize it using
128 * gst_segment_init().
130 * Free-function: gst_segment_free
132 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
135 gst_segment_new (void)
139 result = g_slice_new0 (GstSegment);
140 gst_segment_init (result, GST_FORMAT_UNDEFINED);
147 * @segment: (in) (transfer full): a #GstSegment
149 * Free the allocated segment @segment.
152 gst_segment_free (GstSegment * segment)
154 g_slice_free (GstSegment, segment);
159 * @segment: a #GstSegment structure.
160 * @format: the format of the segment.
162 * The start/last_stop positions are set to 0 and the stop/duration
163 * fields are set to -1 (unknown). The default rate of 1.0 and no
166 * Initialize @segment to its default values.
169 gst_segment_init (GstSegment * segment, GstFormat format)
171 g_return_if_fail (segment != NULL);
173 segment->flags = GST_SEGMENT_FLAG_NONE;
175 segment->applied_rate = 1.0;
176 segment->format = format;
181 segment->position = 0;
182 segment->duration = -1;
186 * gst_segment_do_seek:
187 * @segment: a #GstSegment structure.
188 * @rate: the rate of the segment.
189 * @format: the format of the segment.
190 * @flags: the segment flags for the segment
191 * @start_type: the seek method
192 * @start: the seek start value
193 * @stop_type: the seek method
194 * @stop: the seek stop value
195 * @update: boolean holding whether position was updated.
197 * Update the segment structure with the field values of a seek event (see
198 * gst_event_new_seek()).
200 * After calling this method, the segment field position and time will
201 * contain the requested new position in the segment. The new requested
202 * position in the segment depends on @rate and @start_type and @stop_type.
204 * For positive @rate, the new position in the segment is the new @segment
205 * start field when it was updated with a @start_type different from
206 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
207 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
210 * For negative @rate, the new position in the segment is the new @segment
211 * stop field when it was updated with a @stop_type different from
212 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
213 * duration of the segment will be used to update the stop position.
214 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
215 * @stop is ignored and @segment position is unmodified.
217 * The applied rate of the segment will be set to 1.0 by default.
218 * If the caller can apply a rate change, it should update @segment
219 * rate and applied_rate after calling this function.
221 * @update will be set to TRUE if a seek should be performed to the segment
222 * position field. This field can be FALSE if, for example, only the @rate
223 * has been changed but not the playback position.
225 * Returns: %TRUE if the seek could be performed.
228 gst_segment_do_seek (GstSegment * segment, gdouble rate,
229 GstFormat format, GstSeekFlags flags,
230 GstSeekType start_type, guint64 start,
231 GstSeekType stop_type, guint64 stop, gboolean * update)
233 gboolean update_stop, update_start;
234 guint64 position, base;
236 g_return_val_if_fail (rate != 0.0, FALSE);
237 g_return_val_if_fail (segment != NULL, FALSE);
238 g_return_val_if_fail (segment->format == format, FALSE);
240 update_start = update_stop = TRUE;
242 position = segment->position;
244 if (flags & GST_SEEK_FLAG_FLUSH) {
245 /* flush resets the running_time */
248 base = gst_segment_to_running_time (segment, format, position);
251 /* segment->start is never invalid */
252 switch (start_type) {
253 case GST_SEEK_TYPE_NONE:
254 /* no update to segment, take previous start */
255 start = segment->start;
256 update_start = FALSE;
258 case GST_SEEK_TYPE_SET:
259 /* start holds desired position, map -1 to the start */
263 case GST_SEEK_TYPE_END:
264 if (segment->duration != -1) {
265 /* add start to total length */
266 start = segment->duration + start;
268 /* no update if duration unknown */
269 start = segment->start;
270 update_start = FALSE;
274 /* bring in sane range */
275 if (segment->duration != -1)
276 start = MIN (start, segment->duration);
278 start = MAX (start, 0);
280 /* stop can be -1 if we have not configured a stop. */
282 case GST_SEEK_TYPE_NONE:
283 stop = segment->stop;
286 case GST_SEEK_TYPE_SET:
287 /* stop holds required value */
289 case GST_SEEK_TYPE_END:
290 if (segment->duration != -1) {
291 stop = segment->duration + stop;
293 stop = segment->stop;
299 /* if we have a valid stop time, make sure it is clipped */
301 if (segment->duration != -1)
302 stop = CLAMP (stop, 0, segment->duration);
304 stop = MAX (stop, 0);
307 /* we can't have stop before start */
310 g_return_val_if_fail (start <= stop, FALSE);
315 segment->rate = rate;
316 segment->applied_rate = 1.0;
317 segment->base = base;
318 /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
319 segment->flags = GST_SEGMENT_FLAG_NONE;
320 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
321 segment->flags |= GST_SEGMENT_FLAG_RESET;
322 if ((flags & GST_SEEK_FLAG_SKIP) != 0)
323 segment->flags |= GST_SEGMENT_FLAG_SKIP;
324 if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
325 segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
326 segment->start = start;
327 segment->stop = stop;
328 segment->time = start;
330 if (update_start && rate > 0.0) {
333 if (update_stop && rate < 0.0) {
337 if (segment->duration != -1)
338 position = segment->duration;
343 /* set update arg to reflect update of position */
345 *update = position != segment->position;
347 /* update new position */
348 segment->position = position;
354 * gst_segment_to_stream_time:
355 * @segment: a #GstSegment structure.
356 * @format: the format of the segment.
357 * @position: the position in the segment
359 * Translate @position to stream time using the currently configured
360 * segment. The @position value must be between @segment start and
363 * This function is typically used by elements that need to operate on
364 * the stream time of the buffers it receives, such as effect plugins.
365 * In those use cases, @position is typically the buffer timestamp or
366 * clock time that one wants to convert to the stream time.
367 * The stream time is always between 0 and the total duration of the
370 * Returns: the position in stream_time or -1 when an invalid position
374 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
377 guint64 result, start, stop, time;
378 gdouble abs_applied_rate;
380 /* format does not matter for -1 */
381 if (G_UNLIKELY (position == -1))
384 g_return_val_if_fail (segment != NULL, -1);
385 g_return_val_if_fail (segment->format == format, -1);
387 /* if we have the position for the same format as the segment, we can compare
388 * the start and stop values, otherwise we assume 0 and -1 */
389 if (G_LIKELY (segment->format == format)) {
390 start = segment->start;
391 stop = segment->stop;
392 time = segment->time;
399 /* outside of the segment boundary stop */
400 if (G_UNLIKELY (stop != -1 && position > stop))
403 /* before the segment boundary */
404 if (G_UNLIKELY (position < start))
407 /* time must be known */
408 if (G_UNLIKELY (time == -1))
411 /* bring to uncorrected position in segment */
412 result = position - start;
414 abs_applied_rate = ABS (segment->applied_rate);
416 /* correct for applied rate if needed */
417 if (G_UNLIKELY (abs_applied_rate != 1.0))
418 result *= abs_applied_rate;
420 /* add or subtract from segment time based on applied rate */
421 if (G_LIKELY (segment->applied_rate > 0.0)) {
422 /* correct for segment time */
425 /* correct for segment time, clamp at 0. Streams with a negative
426 * applied_rate have timestamps between start and stop, as usual, but have
427 * the time member starting high and going backwards. */
428 if (G_LIKELY (time > result))
429 result = time - result;
438 * gst_segment_to_running_time:
439 * @segment: a #GstSegment structure.
440 * @format: the format of the segment.
441 * @position: the position in the segment
443 * Translate @position to the total running time using the currently configured
444 * and previously accumulated segments. Position is a value between @segment
445 * start and stop time.
447 * This function is typically used by elements that need to synchronize to the
448 * global clock in a pipeline. The runnning time is a constantly increasing value
449 * starting from 0. When gst_segment_init() is called, this value will reset to
452 * This function returns -1 if the position is outside of @segment start and stop.
454 * Returns: the position as the total running time or -1 when an invalid position
458 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
462 guint64 start, stop, base;
465 if (G_UNLIKELY (position == -1))
468 g_return_val_if_fail (segment != NULL, -1);
469 g_return_val_if_fail (segment->format == format, -1);
471 /* if we have the position for the same format as the segment, we can compare
472 * the start and stop values, otherwise we assume 0 and -1 */
473 if (G_LIKELY (segment->format == format)) {
474 start = segment->start;
475 stop = segment->stop;
476 base = segment->base;
483 /* before the segment boundary */
484 if (G_UNLIKELY (position < start))
487 if (G_LIKELY (segment->rate > 0.0)) {
488 /* outside of the segment boundary stop */
489 if (G_UNLIKELY (stop != -1 && position > stop))
492 /* bring to uncorrected position in segment */
493 result = position - start;
495 /* cannot continue if no stop position set or outside of
497 if (G_UNLIKELY (stop == -1 || position > stop))
500 /* bring to uncorrected position in segment */
501 result = stop - position;
504 /* scale based on the rate, avoid division by and conversion to
505 * float when not needed */
506 abs_rate = ABS (segment->rate);
507 if (G_UNLIKELY (abs_rate != 1.0))
510 /* correct for base of the segment */
518 * @segment: a #GstSegment structure.
519 * @format: the format of the segment.
520 * @start: the start position in the segment
521 * @stop: the stop position in the segment
522 * @clip_start: (out) (allow-none): the clipped start position in the segment
523 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
525 * Clip the given @start and @stop values to the segment boundaries given
526 * in @segment. @start and @stop are compared and clipped to @segment
527 * start and stop values.
529 * If the function returns FALSE, @start and @stop are known to fall
530 * outside of @segment and @clip_start and @clip_stop are not updated.
532 * When the function returns TRUE, @clip_start and @clip_stop will be
533 * updated. If @clip_start or @clip_stop are different from @start or @stop
534 * respectively, the region fell partially in the segment.
536 * Note that when @stop is -1, @clip_stop will be set to the end of the
537 * segment. Depending on the use case, this may or may not be what you want.
539 * Returns: TRUE if the given @start and @stop times fall partially or
540 * completely in @segment, FALSE if the values are completely outside
544 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
545 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
547 g_return_val_if_fail (segment != NULL, FALSE);
548 g_return_val_if_fail (segment->format == format, FALSE);
550 /* if we have a stop position and a valid start and start is bigger,
551 * we're outside of the segment */
552 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
555 /* if a stop position is given and is before the segment start,
556 * we're outside of the segment. Special case is were start
557 * and stop are equal to the segment start. In that case we
558 * are inside the segment. */
559 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
560 && stop == segment->start))))
567 *clip_start = MAX (start, segment->start);
572 *clip_stop = segment->stop;
573 else if (segment->stop == -1)
576 *clip_stop = MIN (stop, segment->stop);
583 * gst_segment_to_position:
584 * @segment: a #GstSegment structure.
585 * @format: the format of the segment.
586 * @running_time: the running_time in the segment
588 * Convert @running_time into a position in the segment so that
589 * gst_segment_to_running_time() with that position returns @running_time.
591 * Returns: the position in the segment for @running_time. This function returns
592 * -1 when @running_time is -1 or when it is not inside @segment.
595 gst_segment_to_position (const GstSegment * segment, GstFormat format,
596 guint64 running_time)
599 guint64 start, stop, base;
602 if (G_UNLIKELY (running_time == -1))
605 g_return_val_if_fail (segment != NULL, -1);
606 g_return_val_if_fail (segment->format == format, FALSE);
608 /* if we have the position for the same format as the segment, we can compare
609 * the start and stop values, otherwise we assume 0 and -1 */
610 if (G_LIKELY (segment->format == format)) {
611 start = segment->start;
612 stop = segment->stop;
613 base = segment->base;
620 /* this running_time was for a previous segment */
621 if (running_time < base)
624 /* start by subtracting the base time */
625 result = running_time - base;
627 /* move into the segment at the right rate */
628 abs_rate = ABS (segment->rate);
629 if (G_UNLIKELY (abs_rate != 1.0))
630 result = ceil (result * abs_rate);
632 if (G_LIKELY (segment->rate > 0.0)) {
633 /* bring to corrected position in segment */
636 /* outside of the segment boundary stop */
637 if (G_UNLIKELY (stop != -1 && result > stop))
640 /* cannot continue if no stop position set or outside of
642 if (G_UNLIKELY (stop == -1 || result + start > stop))
645 /* bring to corrected position in segment */
646 result = stop - result;
653 * gst_segment_set_running_time:
654 * @segment: a #GstSegment structure.
655 * @format: the format of the segment.
656 * @running_time: the running_time in the segment
658 * Adjust the start/stop and base values of @segment such that the next valid
659 * buffer will be one with @running_time.
661 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
662 * returned, @running_time is -1 or not in @segment.
665 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
666 guint64 running_time)
671 /* start by bringing the running_time into the segment position */
672 position = gst_segment_to_position (segment, format, running_time);
674 /* we must have a valid position now */
675 if (G_UNLIKELY (position == -1))
678 start = segment->start;
679 stop = segment->stop;
681 if (G_LIKELY (segment->rate > 0.0)) {
682 /* update the start and time values */
685 /* reverse, update stop */
688 /* and base time is exactly the running time */
689 segment->time = gst_segment_to_stream_time (segment, format, start);
690 segment->start = start;
691 segment->stop = stop;
692 segment->base = running_time;