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.
23 #include "gst_private.h"
26 #include "gstsegment.h"
30 * @short_description: Structure describing the configured region of interest
32 * @see_also: #GstEvent
34 * This helper structure holds the relevant values for tracking the region of
35 * interest in a media file, called a segment.
37 * The structure can be used for two purposes:
39 * <listitem><para>performing seeks (handling seek events)</para></listitem>
40 * <listitem><para>tracking playback regions (handling newsegment events)</para></listitem>
43 * The segment is usually configured by the application with a seek event which
44 * is propagated upstream and eventually handled by an element that performs the seek.
46 * The configured segment is then propagated back downstream with a newsegment event.
47 * This information is then used to clip media to the segment boundaries.
49 * A segment structure is initialized with gst_segment_init(), which takes a #GstFormat
50 * that will be used as the format of the segment values. The segment will be configured
51 * with a start value of 0 and a stop/duration of -1, which is undefined. The default
52 * rate and applied_rate is 1.0.
54 * If the segment is used for managing seeks, the segment duration should be set with
55 * gst_segment_set_duration(). The public duration field contains the duration of the
56 * segment. When using the segment for seeking, the start and time members should
57 * normally be left to their default 0 value. The stop position is left to -1 unless
58 * explicitly configured to a different value after a seek event.
60 * The current position in the segment should be set with the gst_segment_set_last_stop().
61 * The public last_stop field contains the last set stop position in the segment.
63 * For elements that perform seeks, the current segment should be updated with the
64 * gst_segment_set_seek() and the values from the seek event. This method will update
65 * all the segment fields. The last_pos field will contain the new playback position.
66 * If the cur_type was different from GST_SEEK_TYPE_NONE, playback continues from
67 * the last_pos position, possibly with updated flags or rate.
69 * For elements that want to use #GstSegment to track the playback region, use
70 * gst_segment_set_newsegment() to update the segment fields with the information from
71 * the newsegment event. The gst_segment_clip() method can be used to check and clip
72 * the media data to the segment boundaries.
74 * For elements that want to synchronize to the pipeline clock, gst_segment_to_running_time()
75 * can be used to convert a timestamp to a value that can be used to synchronize
76 * to the clock. This function takes into account all accumulated segments as well as
77 * any rate or applied_rate conversions.
79 * For elements that need to perform operations on media data in stream_time,
80 * gst_segment_to_stream_time() can be used to convert a timestamp and the segment
81 * info to stream time (which is always between 0 and the duration of the stream).
83 * Last reviewed on 2006-05-03 (0.10.6)
87 gst_segment_copy (GstSegment * segment)
89 GstSegment *result = NULL;
92 result = gst_segment_new ();
93 memcpy (result, segment, sizeof (GstSegment));
99 gst_segment_get_type (void)
101 static GType gst_segment_type = 0;
103 if (G_UNLIKELY (gst_segment_type == 0)) {
104 gst_segment_type = g_boxed_type_register_static ("GstSegment",
105 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
108 return gst_segment_type;
114 * Allocate a new #GstSegment structure and initialize it using
115 * gst_segment_init().
117 * Returns: a new #GstSegment, free with gst_segment_free().
120 gst_segment_new (void)
124 result = g_new0 (GstSegment, 1);
125 gst_segment_init (result, GST_FORMAT_UNDEFINED);
132 * @segment: a #GstSegment
134 * Free the allocated segment @segment.
137 gst_segment_free (GstSegment * segment)
144 * @segment: a #GstSegment structure.
145 * @format: the format of the segment.
147 * The start/last_stop positions are set to 0 and the stop/duration
148 * fields are set to -1 (unknown). The default rate of 1.0 and no
151 * Initialize @segment to its default values.
154 gst_segment_init (GstSegment * segment, GstFormat format)
156 g_return_if_fail (segment != NULL);
159 segment->abs_rate = 1.0;
160 segment->applied_rate = 1.0;
161 segment->format = format;
167 segment->last_stop = 0;
168 segment->duration = -1;
172 * gst_segment_set_duration:
173 * @segment: a #GstSegment structure.
174 * @format: the format of the segment.
175 * @duration: the duration of the segment info or -1 if unknown.
177 * Set the duration of the segment to @duration. This function is mainly
178 * used by elements that perform seeking and know the total duration of the
181 * This field should be set to allow seeking requests relative to the
185 gst_segment_set_duration (GstSegment * segment, GstFormat format,
188 g_return_if_fail (segment != NULL);
190 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
191 segment->format = format;
193 g_return_if_fail (segment->format == format);
195 segment->duration = duration;
199 * gst_segment_set_last_stop:
200 * @segment: a #GstSegment structure.
201 * @format: the format of the segment.
202 * @position: the position
204 * Set the last observed stop position in the segment to @position.
206 * This field should be set to allow seeking requests relative to the
207 * current playing position.
210 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
213 g_return_if_fail (segment != NULL);
215 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
216 segment->format = format;
218 g_return_if_fail (segment->format == format);
220 segment->last_stop = MAX (segment->start, position);
224 * gst_segment_set_seek:
225 * @segment: a #GstSegment structure.
226 * @rate: the rate of the segment.
227 * @format: the format of the segment.
228 * @flags: the seek flags for the segment
229 * @start_type: the seek method
230 * @start: the seek start value
231 * @stop_type: the seek method
232 * @stop: the seek stop value
233 * @update: boolean holding whether start or stop were updated.
235 * Update the segment structure with the field values of a seek event (see
236 * gst_event_new_seek()).
238 * After calling this method, the segment field last_stop and time will
239 * contain the requested new position in the segment. The new requested
240 * position in the segment depends on @rate and @start_type and @stop_type.
242 * For positive @rate, the new position in the segment is the new @segment
243 * start field when it was updated with a @start_type different from
244 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
245 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment last_stop is
248 * For negative @rate, the new position in the segment is the new @segment
249 * stop field when it was updated with a @stop_type different from
250 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
251 * duration of the segment will be used to update the stop position.
252 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
253 * @stop is ignored and @segment last_stop is unmodified.
255 * The applied rate of the segment will be set to 1.0 by default.
256 * If the caller can apply a rate change, it should update @segment
257 * rate and applied_rate after calling this function.
260 gst_segment_set_seek (GstSegment * segment, gdouble rate,
261 GstFormat format, GstSeekFlags flags,
262 GstSeekType start_type, gint64 start,
263 GstSeekType stop_type, gint64 stop, gboolean * update)
265 gboolean update_stop, update_start;
267 g_return_if_fail (rate != 0.0);
268 g_return_if_fail (segment != NULL);
270 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
271 segment->format = format;
273 g_return_if_fail (segment->format == format);
275 update_stop = update_start = TRUE;
277 /* start is never invalid */
278 switch (start_type) {
279 case GST_SEEK_TYPE_NONE:
280 /* no update to segment */
281 start = segment->start;
282 update_start = FALSE;
284 case GST_SEEK_TYPE_SET:
285 /* start holds desired position, map -1 to the start */
289 case GST_SEEK_TYPE_CUR:
290 /* add start to currently configure segment */
291 start = segment->start + start;
293 case GST_SEEK_TYPE_END:
294 if (segment->duration != -1) {
295 /* add start to total length */
296 start = segment->duration + start;
298 /* no update if duration unknown */
299 start = segment->start;
300 update_start = FALSE;
304 /* bring in sane range */
305 if (segment->duration != -1)
306 start = CLAMP (start, 0, segment->duration);
308 start = MAX (start, 0);
310 /* stop can be -1 if we have not configured a stop. */
312 case GST_SEEK_TYPE_NONE:
313 stop = segment->stop;
316 case GST_SEEK_TYPE_SET:
317 /* stop holds required value */
319 case GST_SEEK_TYPE_CUR:
320 if (segment->stop != -1)
321 stop = segment->stop + stop;
325 case GST_SEEK_TYPE_END:
326 if (segment->duration != -1)
327 stop = segment->duration + stop;
329 stop = segment->stop;
335 /* if we have a valid stop time, make sure it is clipped */
337 if (segment->duration != -1)
338 stop = CLAMP (stop, 0, segment->duration);
340 stop = MAX (stop, 0);
343 /* we can't have stop before start */
345 g_return_if_fail (start <= stop);
347 segment->rate = rate;
348 segment->abs_rate = ABS (rate);
349 segment->applied_rate = 1.0;
350 segment->flags = flags;
351 segment->start = start;
352 if (update_start && rate > 0.0) {
353 segment->last_stop = start;
355 if (update_stop && rate < 0.0) {
357 segment->last_stop = stop;
359 if (segment->duration != -1)
360 segment->last_stop = segment->duration;
362 segment->last_stop = 0;
365 segment->time = start;
366 segment->stop = stop;
369 *update = update_start || update_stop;
373 * gst_segment_set_newsegment:
374 * @segment: a #GstSegment structure.
375 * @update: flag indicating a new segment is started or updated
376 * @rate: the rate of the segment.
377 * @format: the format of the segment.
378 * @start: the new start value
379 * @stop: the new stop value
380 * @time: the new stream time
382 * Update the segment structure with the field values of a new segment event and
383 * with a default applied_rate of 1.0.
388 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
389 GstFormat format, gint64 start, gint64 stop, gint64 time)
391 gst_segment_set_newsegment_full (segment, update, rate, 1.0, format, start,
396 * gst_segment_set_newsegment_full:
397 * @segment: a #GstSegment structure.
398 * @update: flag indicating a new segment is started or updated
399 * @rate: the rate of the segment.
400 * @applied_rate: the applied rate of the segment.
401 * @format: the format of the segment.
402 * @start: the new start value
403 * @stop: the new stop value
404 * @time: the new stream time
406 * Update the segment structure with the field values of a new segment event.
409 gst_segment_set_newsegment_full (GstSegment * segment, gboolean update,
410 gdouble rate, gdouble applied_rate, GstFormat format, gint64 start,
411 gint64 stop, gint64 time)
415 g_return_if_fail (rate != 0.0);
416 g_return_if_fail (applied_rate != 0.0);
417 g_return_if_fail (segment != NULL);
419 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
420 segment->format = format;
422 /* any other format with 0 also gives time 0, the other values are
423 * invalid in the format though. */
424 if (format != segment->format && start == 0) {
425 format = segment->format;
432 g_return_if_fail (segment->format == format);
435 /* an update to the current segment is done, elapsed time is
436 * difference between the old start and new start. */
437 if (start > segment->start)
438 duration = start - segment->start;
442 /* the new segment has to be aligned with the old segment.
443 * We first update the accumulated time of the previous
444 * segment. the accumulated time is used when syncing to the
447 if (segment->stop != -1) {
448 duration = segment->stop - segment->start;
449 } else if (segment->last_stop != -1) {
450 /* else use last seen timestamp as segment stop */
451 duration = segment->last_stop - segment->start;
453 /* else we don't know and throw a warning.. really, this should
454 * be fixed in the element. */
455 g_warning ("closing segment of unknown duration, assuming duration of 0");
459 /* use previous rate to calculate duration */
460 if (segment->abs_rate != 1.0)
461 duration /= segment->abs_rate;
463 /* accumulate duration */
464 segment->accum += duration;
466 /* then update the current segment */
467 segment->rate = rate;
468 segment->abs_rate = ABS (rate);
469 segment->applied_rate = applied_rate;
470 segment->start = start;
471 segment->last_stop = start;
472 segment->stop = stop;
473 segment->time = time;
477 * gst_segment_to_stream_time:
478 * @segment: a #GstSegment structure.
479 * @format: the format of the segment.
480 * @position: the position in the segment
482 * Translate @position to stream time using the currently configured
483 * segment. The @position value must be between @segment start and
486 * This function is typically used by elements that need to operate on
487 * the stream time of the buffers it receives, such as effect plugins.
488 * In those use cases, @position is typically the buffer timestamp or
489 * clock time that one wants to convert to the stream time.
490 * The stream time is always between 0 and the total duration of the
493 * Returns: the position in stream_time or -1 when an invalid position
497 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
501 gdouble abs_applied_rate;
503 g_return_val_if_fail (segment != NULL, -1);
505 /* format does not matter for -1 */
506 if (G_UNLIKELY (position == -1))
509 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
510 segment->format = format;
512 g_return_val_if_fail (segment->format == format, -1);
514 /* outside of the segment boundary stop */
515 if (G_UNLIKELY (segment->stop != -1 && position >= segment->stop))
518 /* before the segment boundary */
519 if (G_UNLIKELY (position < segment->start))
522 /* time must be known */
523 if (G_UNLIKELY (segment->time == -1))
526 /* bring to uncorrected position in segment */
527 result = position - segment->start;
529 abs_applied_rate = ABS (segment->applied_rate);
531 /* correct for applied rate if needed */
532 if (abs_applied_rate != 1.0)
533 result *= abs_applied_rate;
535 /* add or subtract from segment time based on applied rate */
536 if (segment->applied_rate > 0.0) {
537 /* correct for segment time */
538 result += segment->time;
540 /* correct for segment time, clamp at 0 */
541 if (segment->time > result)
542 result = segment->time - result;
551 * gst_segment_to_running_time:
552 * @segment: a #GstSegment structure.
553 * @format: the format of the segment.
554 * @position: the position in the segment
556 * Translate @position to the total running time using the currently configured
557 * and previously accumulated segments. Position is a value between @segment
558 * start and stop time.
560 * This function is typically used by elements that need to synchronize to the
561 * global clock in a pipeline. The runnning time is a constantly increasing value
562 * starting from 0. When gst_segment_init() is called, this value will reset to
565 * This function returns -1 if the position is outside of @segment start and stop.
567 * Returns: the position as the total running time or -1 when an invalid position
571 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
576 g_return_val_if_fail (segment != NULL, -1);
578 if (G_UNLIKELY (position == -1))
581 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
582 segment->format = format;
583 else if (segment->accum)
584 g_return_val_if_fail (segment->format == format, -1);
586 /* before the segment boundary */
587 if (G_UNLIKELY (position < segment->start))
590 if (segment->rate > 0.0) {
591 /* outside of the segment boundary stop */
592 if (G_UNLIKELY (segment->stop != -1 && position >= segment->stop))
595 /* bring to uncorrected position in segment */
596 result = position - segment->start;
598 /* cannot continue if no stop position set or outside of
600 if (G_UNLIKELY (segment->stop == -1 || position >= segment->stop))
603 /* bring to uncorrected position in segment */
604 result = segment->stop - position;
607 /* scale based on the rate, avoid division by and conversion to
608 * float when not needed */
609 if (segment->abs_rate != 1.0)
610 result /= segment->abs_rate;
612 /* correct for accumulated segments */
613 result += segment->accum;
620 * @segment: a #GstSegment structure.
621 * @format: the format of the segment.
622 * @start: the start position in the segment
623 * @stop: the stop position in the segment
624 * @clip_start: the clipped start position in the segment
625 * @clip_stop: the clipped stop position in the segment
627 * Clip the given @start and @stop values to the segment boundaries given
628 * in @segment. @start and @stop are compared and clipped to @segment
629 * start and stop values.
631 * If the function returns FALSE, @start and @stop are known to fall
632 * outside of @segment and @clip_start and @clip_stop are not updated.
634 * When the function returns TRUE, @clip_start and @clip_stop will be
635 * updated. If @clip_start or @clip_stop are different from @start or @stop
636 * respectively, the region fell partially in the segment.
638 * Returns: TRUE if the given @start and @stop times fall partially or
639 * completely in @segment, FALSE if the values are completely outside
643 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
644 gint64 stop, gint64 * clip_start, gint64 * clip_stop)
646 g_return_val_if_fail (segment != NULL, FALSE);
648 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
649 segment->format = format;
651 g_return_val_if_fail (segment->format == format, FALSE);
653 /* if we have a stop position and a valid start and start is bigger,
654 * we're outside of the segment */
655 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
658 /* if a stop position is given and is before the segment start,
659 * we're outside of the segment */
660 if (G_UNLIKELY (stop != -1 && stop != start && stop <= segment->start))
667 *clip_start = MAX (start, segment->start);
672 *clip_stop = segment->stop;
673 else if (segment->stop == -1)
674 *clip_stop = MAX (-1, stop);
676 *clip_stop = MIN (stop, segment->stop);
678 if (segment->duration != -1)
679 *clip_stop = MIN (*clip_stop, segment->duration);