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 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.
597 gst_segment_to_position (const GstSegment * segment, GstFormat format,
598 guint64 running_time)
601 guint64 start, stop, base;
604 if (G_UNLIKELY (running_time == -1))
607 g_return_val_if_fail (segment != NULL, -1);
608 g_return_val_if_fail (segment->format == format, FALSE);
610 /* if we have the position for the same format as the segment, we can compare
611 * the start and stop values, otherwise we assume 0 and -1 */
612 if (G_LIKELY (segment->format == format)) {
613 start = segment->start;
614 stop = segment->stop;
615 base = segment->base;
622 /* this running_time was for a previous segment */
623 if (running_time < base)
626 /* start by subtracting the base time */
627 result = running_time - base;
629 /* move into the segment at the right rate */
630 abs_rate = ABS (segment->rate);
631 if (G_UNLIKELY (abs_rate != 1.0))
632 result = ceil (result * abs_rate);
634 if (G_LIKELY (segment->rate > 0.0)) {
635 /* bring to corrected position in segment */
638 /* outside of the segment boundary stop */
639 if (G_UNLIKELY (stop != -1 && result > stop))
642 /* cannot continue if no stop position set or outside of
644 if (G_UNLIKELY (stop == -1 || result + start > stop))
647 /* bring to corrected position in segment */
648 result = stop - result;
655 * gst_segment_set_running_time:
656 * @segment: a #GstSegment structure.
657 * @format: the format of the segment.
658 * @running_time: the running_time in the segment
660 * Adjust the start/stop and base values of @segment such that the next valid
661 * buffer will be one with @running_time.
663 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
664 * returned, @running_time is -1 or not in @segment.
669 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
670 guint64 running_time)
675 /* start by bringing the running_time into the segment position */
676 position = gst_segment_to_position (segment, format, running_time);
678 /* we must have a valid position now */
679 if (G_UNLIKELY (position == -1))
682 start = segment->start;
683 stop = segment->stop;
685 if (G_LIKELY (segment->rate > 0.0)) {
686 /* update the start and time values */
689 /* reverse, update stop */
692 /* and base time is exactly the running time */
693 segment->time = gst_segment_to_stream_time (segment, format, start);
694 segment->start = start;
695 segment->stop = stop;
696 segment->base = running_time;