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_stop 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_stop 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 2007-05-17 (0.10.13)
88 * @segment: a #GstSegment
90 * Create a copy of given @segment.
92 * Returns: a new #GstSegment, free with gst_segment_free().
97 gst_segment_copy (GstSegment * segment)
99 GstSegment *result = NULL;
102 /* we do not use g_slice_dup or g_slice_copy here because those were
103 * added in GLib 2.14 and we require only >= 2.12 */
104 result = g_slice_new (GstSegment);
111 gst_segment_get_type (void)
113 static GType gst_segment_type = 0;
115 if (G_UNLIKELY (gst_segment_type == 0)) {
116 gst_segment_type = g_boxed_type_register_static ("GstSegment",
117 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
120 return gst_segment_type;
126 * Allocate a new #GstSegment structure and initialize it using
127 * gst_segment_init().
129 * Returns: a new #GstSegment, free with gst_segment_free().
132 gst_segment_new (void)
136 result = g_slice_new0 (GstSegment);
137 gst_segment_init (result, GST_FORMAT_UNDEFINED);
144 * @segment: a #GstSegment
146 * Free the allocated segment @segment.
149 gst_segment_free (GstSegment * segment)
151 g_slice_free (GstSegment, segment);
156 * @segment: a #GstSegment structure.
157 * @format: the format of the segment.
159 * The start/last_stop positions are set to 0 and the stop/duration
160 * fields are set to -1 (unknown). The default rate of 1.0 and no
163 * Initialize @segment to its default values.
166 gst_segment_init (GstSegment * segment, GstFormat format)
168 g_return_if_fail (segment != NULL);
171 segment->abs_rate = 1.0;
172 segment->applied_rate = 1.0;
173 segment->format = format;
179 segment->last_stop = 0;
180 segment->duration = -1;
184 * gst_segment_set_duration:
185 * @segment: a #GstSegment structure.
186 * @format: the format of the segment.
187 * @duration: the duration of the segment info or -1 if unknown.
189 * Set the duration of the segment to @duration. This function is mainly
190 * used by elements that perform seeking and know the total duration of the
193 * This field should be set to allow seeking requests relative to the
197 gst_segment_set_duration (GstSegment * segment, GstFormat format,
200 g_return_if_fail (segment != NULL);
202 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
203 segment->format = format;
205 g_return_if_fail (segment->format == format);
207 segment->duration = duration;
211 * gst_segment_set_last_stop:
212 * @segment: a #GstSegment structure.
213 * @format: the format of the segment.
214 * @position: the position
216 * Set the last observed stop position in the segment to @position.
218 * This field should be set to allow seeking requests relative to the
219 * current playing position.
222 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
225 g_return_if_fail (segment != NULL);
227 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
228 segment->format = format;
230 g_return_if_fail (segment->format == format);
232 segment->last_stop = MAX (segment->start, position);
236 * gst_segment_set_seek:
237 * @segment: a #GstSegment structure.
238 * @rate: the rate of the segment.
239 * @format: the format of the segment.
240 * @flags: the seek flags for the segment
241 * @start_type: the seek method
242 * @start: the seek start value
243 * @stop_type: the seek method
244 * @stop: the seek stop value
245 * @update: boolean holding whether last_stop was updated.
247 * Update the segment structure with the field values of a seek event (see
248 * gst_event_new_seek()).
250 * After calling this method, the segment field last_stop and time will
251 * contain the requested new position in the segment. The new requested
252 * position in the segment depends on @rate and @start_type and @stop_type.
254 * For positive @rate, the new position in the segment is the new @segment
255 * start field when it was updated with a @start_type different from
256 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
257 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment last_stop is
260 * For negative @rate, the new position in the segment is the new @segment
261 * stop field when it was updated with a @stop_type different from
262 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
263 * duration of the segment will be used to update the stop position.
264 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
265 * @stop is ignored and @segment last_stop is unmodified.
267 * The applied rate of the segment will be set to 1.0 by default.
268 * If the caller can apply a rate change, it should update @segment
269 * rate and applied_rate after calling this function.
271 * @update will be set to TRUE if a seek should be performed to the segment
272 * last_stop field. This field can be FALSE if, for example, only the @rate
273 * has been changed but not the playback position.
276 gst_segment_set_seek (GstSegment * segment, gdouble rate,
277 GstFormat format, GstSeekFlags flags,
278 GstSeekType start_type, gint64 start,
279 GstSeekType stop_type, gint64 stop, gboolean * update)
281 gboolean update_stop, update_start;
284 g_return_if_fail (rate != 0.0);
285 g_return_if_fail (segment != NULL);
287 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
288 segment->format = format;
290 update_start = update_stop = TRUE;
292 /* segment->start is never invalid */
293 switch (start_type) {
294 case GST_SEEK_TYPE_NONE:
295 /* no update to segment, take previous start */
296 start = segment->start;
297 update_start = FALSE;
299 case GST_SEEK_TYPE_SET:
300 /* start holds desired position, map -1 to the start */
303 /* start must be 0 or the formats must match */
304 g_return_if_fail (start == 0 || segment->format == format);
306 case GST_SEEK_TYPE_CUR:
307 g_return_if_fail (start == 0 || segment->format == format);
308 /* add start to currently configured segment */
309 start = segment->start + start;
311 case GST_SEEK_TYPE_END:
312 if (segment->duration != -1) {
313 g_return_if_fail (start == 0 || segment->format == format);
314 /* add start to total length */
315 start = segment->duration + start;
317 /* no update if duration unknown */
318 start = segment->start;
319 update_start = FALSE;
323 /* bring in sane range */
324 if (segment->duration != -1)
325 start = CLAMP (start, 0, segment->duration);
327 start = MAX (start, 0);
329 /* stop can be -1 if we have not configured a stop. */
331 case GST_SEEK_TYPE_NONE:
332 stop = segment->stop;
335 case GST_SEEK_TYPE_SET:
336 /* stop holds required value, if it's not -1, it must be of the same
337 * format as the segment. */
338 g_return_if_fail (stop == -1 || segment->format == format);
340 case GST_SEEK_TYPE_CUR:
341 if (segment->stop != -1) {
342 /* only add compatible formats or 0 */
343 g_return_if_fail (stop == 0 || segment->format == format);
344 stop = segment->stop + stop;
348 case GST_SEEK_TYPE_END:
349 if (segment->duration != -1) {
350 /* only add compatible formats or 0 */
351 g_return_if_fail (stop == 0 || segment->format == format);
352 stop = segment->duration + stop;
354 stop = segment->stop;
360 /* if we have a valid stop time, make sure it is clipped */
362 if (segment->duration != -1)
363 stop = CLAMP (stop, 0, segment->duration);
365 stop = MAX (stop, 0);
368 /* we can't have stop before start */
370 g_return_if_fail (start <= stop);
372 segment->rate = rate;
373 segment->abs_rate = ABS (rate);
374 segment->applied_rate = 1.0;
375 segment->flags = flags;
376 segment->start = start;
377 segment->stop = stop;
378 segment->time = start;
380 last_stop = segment->last_stop;
381 if (update_start && rate > 0.0) {
384 if (update_stop && rate < 0.0) {
388 if (segment->duration != -1)
389 last_stop = segment->duration;
394 /* set update arg to reflect update of last_stop */
396 *update = last_stop != segment->last_stop;
398 /* update new position */
399 segment->last_stop = last_stop;
403 * gst_segment_set_newsegment:
404 * @segment: a #GstSegment structure.
405 * @update: flag indicating a new segment is started or updated
406 * @rate: the rate of the segment.
407 * @format: the format of the segment.
408 * @start: the new start value
409 * @stop: the new stop value
410 * @time: the new stream time
412 * Update the segment structure with the field values of a new segment event and
413 * with a default applied_rate of 1.0.
418 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
419 GstFormat format, gint64 start, gint64 stop, gint64 time)
421 gst_segment_set_newsegment_full (segment, update, rate, 1.0, format, start,
426 * gst_segment_set_newsegment_full:
427 * @segment: a #GstSegment structure.
428 * @update: flag indicating a new segment is started or updated
429 * @rate: the rate of the segment.
430 * @applied_rate: the applied rate of the segment.
431 * @format: the format of the segment.
432 * @start: the new start value
433 * @stop: the new stop value
434 * @time: the new stream time
436 * Update the segment structure with the field values of a new segment event.
439 gst_segment_set_newsegment_full (GstSegment * segment, gboolean update,
440 gdouble rate, gdouble applied_rate, GstFormat format, gint64 start,
441 gint64 stop, gint64 time)
445 g_return_if_fail (rate != 0.0);
446 g_return_if_fail (applied_rate != 0.0);
447 g_return_if_fail (segment != NULL);
449 GST_DEBUG ("configuring segment update %d, rate %lf, format %s, "
450 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT ", position %"
451 G_GINT64_FORMAT, update, rate, gst_format_get_name (format), start,
453 GST_DEBUG ("old segment was: %" GST_SEGMENT_FORMAT, segment);
455 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
456 segment->format = format;
458 /* any other format with 0 also gives time 0, the other values are
459 * invalid in the format though. */
460 if (format != segment->format && start == 0) {
461 format = segment->format;
468 g_return_if_fail (segment->format == format);
471 if (segment->rate > 0.0) {
472 /* an update to the current segment is done, elapsed time is
473 * difference between the old start and new start. */
474 if (start > segment->start)
475 duration = start - segment->start;
479 /* for negative rates, the elapsed duration is the diff between the stop
481 if (stop != -1 && stop < segment->stop)
482 duration = segment->stop - stop;
487 /* the new segment has to be aligned with the old segment.
488 * We first update the accumulated time of the previous
489 * segment. the accumulated time is used when syncing to the
492 if (segment->stop != -1) {
493 duration = segment->stop - segment->start;
494 } else if (segment->last_stop != -1) {
495 /* else use last seen timestamp as segment stop */
496 duration = segment->last_stop - segment->start;
498 /* else we don't know and throw a warning.. really, this should
499 * be fixed in the element. */
500 g_warning ("closing segment of unknown duration, assuming duration of 0");
504 /* use previous rate to calculate duration */
505 if (segment->abs_rate != 1.0)
506 duration /= segment->abs_rate;
508 /* accumulate duration */
509 segment->accum += duration;
511 /* then update the current segment */
512 segment->rate = rate;
513 segment->abs_rate = ABS (rate);
514 segment->applied_rate = applied_rate;
515 segment->start = start;
516 segment->last_stop = start;
517 segment->stop = stop;
518 segment->time = time;
522 * gst_segment_to_stream_time:
523 * @segment: a #GstSegment structure.
524 * @format: the format of the segment.
525 * @position: the position in the segment
527 * Translate @position to stream time using the currently configured
528 * segment. The @position value must be between @segment start and
531 * This function is typically used by elements that need to operate on
532 * the stream time of the buffers it receives, such as effect plugins.
533 * In those use cases, @position is typically the buffer timestamp or
534 * clock time that one wants to convert to the stream time.
535 * The stream time is always between 0 and the total duration of the
538 * Returns: the position in stream_time or -1 when an invalid position
542 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
545 gint64 result, start, stop, time;
546 gdouble abs_applied_rate;
548 g_return_val_if_fail (segment != NULL, -1);
550 /* format does not matter for -1 */
551 if (G_UNLIKELY (position == -1))
554 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
555 segment->format = format;
557 /* if we have the position for the same format as the segment, we can compare
558 * the start and stop values, otherwise we assume 0 and -1 */
559 if (segment->format == format) {
560 start = segment->start;
561 stop = segment->stop;
562 time = segment->time;
569 /* outside of the segment boundary stop */
570 if (G_UNLIKELY (stop != -1 && position > stop))
573 /* before the segment boundary */
574 if (G_UNLIKELY (position < start))
577 /* time must be known */
578 if (G_UNLIKELY (time == -1))
581 /* bring to uncorrected position in segment */
582 result = position - start;
584 abs_applied_rate = ABS (segment->applied_rate);
586 /* correct for applied rate if needed */
587 if (abs_applied_rate != 1.0)
588 result *= abs_applied_rate;
590 /* add or subtract from segment time based on applied rate */
591 if (segment->applied_rate > 0.0) {
592 /* correct for segment time */
595 /* correct for segment time, clamp at 0. Streams with a negative
596 * applied_rate have timestamps between start and stop, as usual, but have
597 * the time member starting high and going backwards. */
599 result = time - result;
608 * gst_segment_to_running_time:
609 * @segment: a #GstSegment structure.
610 * @format: the format of the segment.
611 * @position: the position in the segment
613 * Translate @position to the total running time using the currently configured
614 * and previously accumulated segments. Position is a value between @segment
615 * start and stop time.
617 * This function is typically used by elements that need to synchronize to the
618 * global clock in a pipeline. The runnning time is a constantly increasing value
619 * starting from 0. When gst_segment_init() is called, this value will reset to
622 * This function returns -1 if the position is outside of @segment start and stop.
624 * Returns: the position as the total running time or -1 when an invalid position
628 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
632 gint64 start, stop, accum;
634 g_return_val_if_fail (segment != NULL, -1);
636 if (G_UNLIKELY (position == -1))
639 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
640 segment->format = format;
642 /* if we have the position for the same format as the segment, we can compare
643 * the start and stop values, otherwise we assume 0 and -1 */
644 if (segment->format == format) {
645 start = segment->start;
646 stop = segment->stop;
647 accum = segment->accum;
654 /* before the segment boundary */
655 if (G_UNLIKELY (position < start))
658 if (segment->rate > 0.0) {
659 /* outside of the segment boundary stop */
660 if (G_UNLIKELY (stop != -1 && position > stop))
663 /* bring to uncorrected position in segment */
664 result = position - start;
666 /* cannot continue if no stop position set or outside of
668 if (G_UNLIKELY (stop == -1 || position > stop))
671 /* bring to uncorrected position in segment */
672 result = stop - position;
675 /* scale based on the rate, avoid division by and conversion to
676 * float when not needed */
677 if (segment->abs_rate != 1.0)
678 result /= segment->abs_rate;
680 /* correct for accumulated segments */
688 * @segment: a #GstSegment structure.
689 * @format: the format of the segment.
690 * @start: the start position in the segment
691 * @stop: the stop position in the segment
692 * @clip_start: the clipped start position in the segment
693 * @clip_stop: the clipped stop position in the segment
695 * Clip the given @start and @stop values to the segment boundaries given
696 * in @segment. @start and @stop are compared and clipped to @segment
697 * start and stop values.
699 * If the function returns FALSE, @start and @stop are known to fall
700 * outside of @segment and @clip_start and @clip_stop are not updated.
702 * When the function returns TRUE, @clip_start and @clip_stop will be
703 * updated. If @clip_start or @clip_stop are different from @start or @stop
704 * respectively, the region fell partially in the segment.
706 * Note that when @stop is -1, @clip_stop will be set to the end of the
707 * segment. Depending on the use case, this may or may not be what you want.
709 * Returns: TRUE if the given @start and @stop times fall partially or
710 * completely in @segment, FALSE if the values are completely outside
714 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
715 gint64 stop, gint64 * clip_start, gint64 * clip_stop)
717 g_return_val_if_fail (segment != NULL, FALSE);
719 if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
720 segment->format = format;
722 g_return_val_if_fail (segment->format == format, FALSE);
724 /* if we have a stop position and a valid start and start is bigger,
725 * we're outside of the segment */
726 if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
729 /* if a stop position is given and is before the segment start,
730 * we're outside of the segment */
731 if (G_UNLIKELY (stop != -1 && stop != start && stop <= segment->start))
738 *clip_start = MAX (start, segment->start);
743 *clip_stop = segment->stop;
744 else if (segment->stop == -1)
745 *clip_stop = MAX (-1, stop);
747 *clip_stop = MIN (stop, segment->stop);
749 if (segment->duration != -1)
750 *clip_stop = MIN (*clip_stop, segment->duration);