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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include "gst_private.h"
27 #include "gstsegment.h"
32 * @short_description: Structure describing the configured region of interest
34 * @see_also: #GstEvent
36 * This helper structure holds the relevant values for tracking the region of
37 * interest in a media file, called a segment.
39 * The structure can be used for two purposes:
41 * * performing seeks (handling seek events)
42 * * tracking playback regions (handling newsegment events)
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 * The public duration field contains the duration of the segment. When using
56 * the segment for seeking, the start and time members should normally be left
57 * to their default 0 value. The stop position is left to -1 unless explicitly
58 * configured to a different value after a seek event.
60 * The current position in the segment should be set by changing the position
61 * member in the structure.
63 * For elements that perform seeks, the current segment should be updated with the
64 * gst_segment_do_seek() and the values from the seek event. This method will update
65 * all the segment fields. The position field will contain the new playback position.
66 * If the start_type was different from GST_SEEK_TYPE_NONE, playback continues from
67 * the position position, possibly with updated flags or rate.
69 * For elements that want to use #GstSegment to track the playback region,
70 * update the segment fields with the information from the newsegment event.
71 * 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 the base 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).
84 /* FIXME 2.0: remove unused format parameter.
85 * Most of the methods in gstsegment.c take and extra GstFormat format, just to
86 * verify segment->format == format.
87 * See https://bugzilla.gnome.org/show_bug.cgi?id=788979
92 * @segment: (transfer none): a #GstSegment
94 * Create a copy of given @segment.
96 * Free-function: gst_segment_free
98 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
101 gst_segment_copy (const GstSegment * segment)
103 GstSegment *result = NULL;
106 result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
112 * gst_segment_copy_into:
113 * @src: (transfer none): a #GstSegment
114 * @dest: (transfer none): a #GstSegment
116 * Copy the contents of @src into @dest.
119 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
121 memcpy (dest, src, sizeof (GstSegment));
124 G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
125 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
130 * Allocate a new #GstSegment structure and initialize it using
131 * gst_segment_init().
133 * Free-function: gst_segment_free
135 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
138 gst_segment_new (void)
142 result = g_slice_new0 (GstSegment);
143 gst_segment_init (result, GST_FORMAT_UNDEFINED);
150 * @segment: (in) (transfer full): a #GstSegment
152 * Free the allocated segment @segment.
155 gst_segment_free (GstSegment * segment)
157 g_slice_free (GstSegment, segment);
162 * @segment: a #GstSegment structure.
163 * @format: the format of the segment.
165 * The start/position fields are set to 0 and the stop/duration
166 * fields are set to -1 (unknown). The default rate of 1.0 and no
169 * Initialize @segment to its default values.
172 gst_segment_init (GstSegment * segment, GstFormat format)
174 g_return_if_fail (segment != NULL);
176 segment->flags = GST_SEGMENT_FLAG_NONE;
178 segment->applied_rate = 1.0;
179 segment->format = format;
185 segment->position = 0;
186 segment->duration = -1;
190 * gst_segment_do_seek:
191 * @segment: a #GstSegment structure.
192 * @rate: the rate of the segment.
193 * @format: the format of the segment.
194 * @flags: the segment flags for the segment
195 * @start_type: the seek method
196 * @start: the seek start value
197 * @stop_type: the seek method
198 * @stop: the seek stop value
199 * @update: (out) (allow-none): boolean holding whether position was updated.
201 * Update the segment structure with the field values of a seek event (see
202 * gst_event_new_seek()).
204 * After calling this method, the segment field position and time will
205 * contain the requested new position in the segment. The new requested
206 * position in the segment depends on @rate and @start_type and @stop_type.
208 * For positive @rate, the new position in the segment is the new @segment
209 * start field when it was updated with a @start_type different from
210 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
211 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
214 * For negative @rate, the new position in the segment is the new @segment
215 * stop field when it was updated with a @stop_type different from
216 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
217 * duration of the segment will be used to update the stop position.
218 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
219 * @stop is ignored and @segment position is unmodified.
221 * The applied rate of the segment will be set to 1.0 by default.
222 * If the caller can apply a rate change, it should update @segment
223 * rate and applied_rate after calling this function.
225 * @update will be set to %TRUE if a seek should be performed to the segment
226 * position field. This field can be %FALSE if, for example, only the @rate
227 * has been changed but not the playback position.
229 * Returns: %TRUE if the seek could be performed.
232 gst_segment_do_seek (GstSegment * segment, gdouble rate,
233 GstFormat format, GstSeekFlags flags,
234 GstSeekType start_type, guint64 start,
235 GstSeekType stop_type, guint64 stop, gboolean * update)
237 gboolean update_stop, update_start;
238 guint64 position, base;
240 g_return_val_if_fail (rate != 0.0, FALSE);
241 g_return_val_if_fail (segment != NULL, FALSE);
242 g_return_val_if_fail (segment->format == format, FALSE);
244 update_start = update_stop = TRUE;
246 position = segment->position;
248 /* segment->start is never invalid */
249 switch (start_type) {
250 case GST_SEEK_TYPE_NONE:
251 /* no update to segment, take previous start */
252 start = segment->start;
253 update_start = FALSE;
255 case GST_SEEK_TYPE_SET:
256 /* start holds desired position, map -1 to the start */
260 case GST_SEEK_TYPE_END:
261 if (segment->duration != -1) {
262 /* add start to total length */
263 start = segment->duration + start;
265 /* no update if duration unknown */
266 start = segment->start;
267 update_start = FALSE;
271 /* bring in sane range */
272 if (segment->duration != -1)
273 start = MIN (start, segment->duration);
275 start = MAX ((gint64) start, 0);
277 /* stop can be -1 if we have not configured a stop. */
279 case GST_SEEK_TYPE_NONE:
280 stop = segment->stop;
283 case GST_SEEK_TYPE_SET:
284 /* stop holds required value */
286 case GST_SEEK_TYPE_END:
287 if (segment->duration != -1) {
288 stop = segment->duration + stop;
290 stop = segment->stop;
296 /* if we have a valid stop time, make sure it is clipped */
298 if (segment->duration != -1)
299 stop = CLAMP ((gint64) stop, 0, (gint64) segment->duration);
301 stop = MAX ((gint64) stop, 0);
304 /* we can't have stop before start */
307 GST_WARNING ("segment update failed: start(%" G_GUINT64_FORMAT
308 ") > stop(%" G_GUINT64_FORMAT ")", start, stop);
309 g_return_val_if_fail (start <= stop, FALSE);
314 if (flags & GST_SEEK_FLAG_FLUSH) {
315 /* flush resets the running_time */
318 /* make sure the position is inside the segment start/stop */
319 position = CLAMP (position, segment->start, segment->stop);
321 /* remember the elapsed time */
322 base = gst_segment_to_running_time (segment, format, position);
323 GST_DEBUG ("updated segment.base: %" G_GUINT64_FORMAT, base);
326 if (update_start && rate > 0.0) {
329 if (update_stop && rate < 0.0) {
333 if (segment->duration != -1)
334 position = segment->duration;
340 /* set update arg to reflect update of position */
342 *update = position != segment->position;
344 /* update new values */
345 /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
346 segment->flags = GST_SEGMENT_FLAG_NONE;
347 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
348 segment->flags |= GST_SEGMENT_FLAG_RESET;
349 if ((flags & GST_SEEK_FLAG_TRICKMODE) != 0)
350 segment->flags |= GST_SEGMENT_FLAG_TRICKMODE;
351 if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
352 segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
353 if ((flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS) != 0)
354 segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS;
355 if ((flags & GST_SEEK_FLAG_TRICKMODE_NO_AUDIO) != 0)
356 segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_NO_AUDIO;
358 segment->rate = rate;
359 segment->applied_rate = 1.0;
361 segment->base = base;
363 segment->offset = position - start;
366 segment->offset = stop - position;
367 else if (segment->duration != -1)
368 segment->offset = segment->duration - position;
373 segment->start = start;
374 segment->stop = stop;
375 segment->time = start;
376 segment->position = position;
378 GST_INFO ("segment updated: %" GST_SEGMENT_FORMAT, segment);
384 * gst_segment_to_stream_time_full:
385 * @segment: a #GstSegment structure.
386 * @format: the format of the segment.
387 * @position: the position in the segment
388 * @stream_time: (out): result stream-time
390 * Translate @position to the total stream time using the currently configured
391 * segment. Compared to gst_segment_to_stream_time() this function can return
392 * negative stream-time.
394 * This function is typically used by elements that need to synchronize buffers
395 * against the clock or each other.
397 * @position can be any value and the result of this function for values outside
398 * of the segment is extrapolated.
400 * When 1 is returned, @position resulted in a positive stream-time returned
403 * When this function returns -1, the returned @stream_time should be negated
404 * to get the real negative stream time.
406 * Returns: a 1 or -1 on success, 0 on failure.
411 gst_segment_to_stream_time_full (const GstSegment * segment, GstFormat format,
412 guint64 position, guint64 * stream_time)
414 guint64 start, stop, time;
415 gdouble abs_applied_rate;
418 /* format does not matter for -1 */
419 if (G_UNLIKELY (position == -1)) {
424 g_return_val_if_fail (segment != NULL, 0);
425 g_return_val_if_fail (segment->format == format, 0);
427 stop = segment->stop;
429 start = segment->start;
430 time = segment->time;
432 /* time must be known */
433 if (G_UNLIKELY (time == -1))
436 abs_applied_rate = ABS (segment->applied_rate);
438 /* add or subtract from segment time based on applied rate */
439 if (G_LIKELY (segment->applied_rate > 0.0)) {
440 if (G_LIKELY (position > start)) {
441 /* bring to uncorrected position in segment */
442 *stream_time = position - start;
443 /* correct for applied rate if needed */
444 if (G_UNLIKELY (abs_applied_rate != 1.0))
445 *stream_time *= abs_applied_rate;
446 /* correct for segment time */
447 *stream_time += time;
450 *stream_time = start - position;
451 if (G_UNLIKELY (abs_applied_rate != 1.0))
452 *stream_time *= abs_applied_rate;
453 if (*stream_time > time) {
454 *stream_time -= time;
457 *stream_time = time - *stream_time;
462 /* correct for segment time. Streams with a negative applied_rate
463 * have timestamps between start and stop, as usual, but have the
464 * time member starting high and going backwards. */
465 /* cannot continue without a known segment stop */
466 if (G_UNLIKELY (stop == -1))
468 if (G_UNLIKELY (position > stop)) {
469 *stream_time = position - stop;
470 if (G_UNLIKELY (abs_applied_rate != 1.0))
471 *stream_time *= abs_applied_rate;
472 if (*stream_time > time) {
473 *stream_time -= time;
476 *stream_time = time - *stream_time;
480 *stream_time = stop - position;
481 if (G_UNLIKELY (abs_applied_rate != 1.0))
482 *stream_time *= abs_applied_rate;
483 *stream_time += time;
492 * gst_segment_to_stream_time:
493 * @segment: a #GstSegment structure.
494 * @format: the format of the segment.
495 * @position: the position in the segment
497 * Translate @position to stream time using the currently configured
498 * segment. The @position value must be between @segment start and
501 * This function is typically used by elements that need to operate on
502 * the stream time of the buffers it receives, such as effect plugins.
503 * In those use cases, @position is typically the buffer timestamp or
504 * clock time that one wants to convert to the stream time.
505 * The stream time is always between 0 and the total duration of the
508 * Returns: the position in stream_time or -1 when an invalid position
514 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
519 g_return_val_if_fail (segment != NULL, -1);
520 g_return_val_if_fail (segment->format == format, -1);
522 /* before the segment boundary */
523 if (G_UNLIKELY (position < segment->start)) {
524 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
525 ")", position, segment->start);
528 /* after the segment boundary */
529 if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
530 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
531 ")", position, segment->stop);
535 if (gst_segment_to_stream_time_full (segment, format, position, &result) == 1)
542 * gst_segment_position_from_stream_time_full:
543 * @segment: a #GstSegment structure.
544 * @format: the format of the segment.
545 * @stream_time: the stream-time
546 * @position: (out): the resulting position in the segment
548 * Translate @stream_time to the segment position using the currently configured
549 * segment. Compared to gst_segment_position_from_stream_time() this function can
550 * return negative segment position.
552 * This function is typically used by elements that need to synchronize buffers
553 * against the clock or each other.
555 * @stream_time can be any value and the result of this function for values outside
556 * of the segment is extrapolated.
558 * When 1 is returned, @stream_time resulted in a positive position returned
561 * When this function returns -1, the returned @position should be negated
562 * to get the real negative segment position.
564 * Returns: a 1 or -1 on success, 0 on failure.
569 gst_segment_position_from_stream_time_full (const GstSegment * segment,
570 GstFormat format, guint64 stream_time, guint64 * position)
573 gdouble abs_applied_rate;
576 /* format does not matter for -1 */
577 if (G_UNLIKELY (stream_time == -1)) {
582 g_return_val_if_fail (segment != NULL, -1);
583 g_return_val_if_fail (segment->format == format, -1);
585 start = segment->start;
586 time = segment->time;
588 /* time must be known */
589 if (G_UNLIKELY (time == -1))
592 abs_applied_rate = ABS (segment->applied_rate);
594 if (G_LIKELY (segment->applied_rate > 0.0)) {
595 if (G_LIKELY (stream_time > time)) {
597 *position = stream_time - time;
600 *position = time - stream_time;
602 /* correct for applied rate if needed */
603 if (G_UNLIKELY (abs_applied_rate != 1.0))
604 *position /= abs_applied_rate;
606 if (G_UNLIKELY (res == -1)) {
607 if (*position > start) {
610 *position = start - *position;
617 GstClockTime stop = segment->stop;
618 /* cannot continue without a known segment stop */
619 if (G_UNLIKELY (stop == -1))
621 if (G_UNLIKELY (time > stream_time)) {
623 *position = time - stream_time;
626 *position = stream_time - time;
628 if (G_UNLIKELY (abs_applied_rate != 1.0))
629 *position /= abs_applied_rate;
630 if (G_UNLIKELY (stop < *position)) {
631 if (G_LIKELY (res == 1)) {
639 if (G_LIKELY (res == 1)) {
640 *position = stop - *position;
653 * gst_segment_position_from_stream_time:
654 * @segment: a #GstSegment structure.
655 * @format: the format of the segment.
656 * @stream_time: the stream_time in the segment
658 * Convert @stream_time into a position in the segment so that
659 * gst_segment_to_stream_time() with that position returns @stream_time.
661 * Returns: the position in the segment for @stream_time. This function returns
662 * -1 when @stream_time is -1 or when it is not inside @segment.
667 gst_segment_position_from_stream_time (const GstSegment * segment,
668 GstFormat format, guint64 stream_time)
673 g_return_val_if_fail (segment != NULL, -1);
674 g_return_val_if_fail (segment->format == format, -1);
677 gst_segment_position_from_stream_time_full (segment, format, stream_time,
680 /* before the segment boundary */
681 if (G_UNLIKELY (position < segment->start)) {
682 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
683 ")", position, segment->start);
687 /* after the segment boundary */
688 if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
689 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
690 ")", position, segment->stop);
701 * gst_segment_to_running_time_full:
702 * @segment: a #GstSegment structure.
703 * @format: the format of the segment.
704 * @position: the position in the segment
705 * @running_time: (out) (allow-none): result running-time
707 * Translate @position to the total running time using the currently configured
708 * segment. Compared to gst_segment_to_running_time() this function can return
709 * negative running-time.
711 * This function is typically used by elements that need to synchronize buffers
712 * against the clock or each other.
714 * @position can be any value and the result of this function for values outside
715 * of the segment is extrapolated.
717 * When 1 is returned, @position resulted in a positive running-time returned
720 * When this function returns -1, the returned @running_time should be negated
721 * to get the real negative running time.
723 * Returns: a 1 or -1 on success, 0 on failure.
728 gst_segment_to_running_time_full (const GstSegment * segment, GstFormat format,
729 guint64 position, guint64 * running_time)
733 guint64 start, stop, offset;
736 if (G_UNLIKELY (position == -1)) {
737 GST_DEBUG ("invalid position (-1)");
741 g_return_val_if_fail (segment != NULL, 0);
742 g_return_val_if_fail (segment->format == format, 0);
744 offset = segment->offset;
746 if (G_LIKELY (segment->rate > 0.0)) {
747 start = segment->start + offset;
749 /* bring to uncorrected position in segment */
750 if (position < start) {
752 result = start - position;
755 result = position - start;
759 stop = segment->stop;
761 if (stop == -1 && segment->duration != -1)
762 stop = segment->start + segment->duration;
764 /* cannot continue if no stop position set or invalid offset */
765 g_return_val_if_fail (stop != -1, 0);
766 g_return_val_if_fail (stop >= offset, 0);
770 /* bring to uncorrected position in segment */
771 if (position > stop) {
773 result = position - stop;
776 result = stop - position;
782 /* scale based on the rate, avoid division by and conversion to
783 * float when not needed */
784 abs_rate = ABS (segment->rate);
785 if (G_UNLIKELY (abs_rate != 1.0))
788 /* correct for base of the segment */
790 /* positive, add base */
791 *running_time = result + segment->base;
792 else if (segment->base >= result) {
793 /* negative and base is bigger, subtract from base and we have a
794 * positive value again */
795 *running_time = segment->base - result;
798 /* negative and base is smaller, subtract base and remainder is
800 *running_time = result - segment->base;
814 * gst_segment_to_running_time:
815 * @segment: a #GstSegment structure.
816 * @format: the format of the segment.
817 * @position: the position in the segment
819 * Translate @position to the total running time using the currently configured
820 * segment. Position is a value between @segment start and stop time.
822 * This function is typically used by elements that need to synchronize to the
823 * global clock in a pipeline. The running time is a constantly increasing value
824 * starting from 0. When gst_segment_init() is called, this value will reset to
827 * This function returns -1 if the position is outside of @segment start and stop.
829 * Returns: the position as the total running time or -1 when an invalid position
833 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
838 g_return_val_if_fail (segment != NULL, -1);
839 g_return_val_if_fail (segment->format == format, -1);
841 /* before the segment boundary */
842 if (G_UNLIKELY (position < segment->start)) {
843 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
844 ")", position, segment->start);
847 /* after the segment boundary */
848 if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
849 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
850 ")", position, segment->stop);
854 if (gst_segment_to_running_time_full (segment, format, position,
863 * @segment: a #GstSegment structure.
864 * @format: the format of the segment.
865 * @start: the start position in the segment
866 * @stop: the stop position in the segment
867 * @clip_start: (out) (allow-none): the clipped start position in the segment
868 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
870 * Clip the given @start and @stop values to the segment boundaries given
871 * in @segment. @start and @stop are compared and clipped to @segment
872 * start and stop values.
874 * If the function returns %FALSE, @start and @stop are known to fall
875 * outside of @segment and @clip_start and @clip_stop are not updated.
877 * When the function returns %TRUE, @clip_start and @clip_stop will be
878 * updated. If @clip_start or @clip_stop are different from @start or @stop
879 * respectively, the region fell partially in the segment.
881 * Note that when @stop is -1, @clip_stop will be set to the end of the
882 * segment. Depending on the use case, this may or may not be what you want.
884 * Returns: %TRUE if the given @start and @stop times fall partially or
885 * completely in @segment, %FALSE if the values are completely outside
889 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
890 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
892 g_return_val_if_fail (segment != NULL, FALSE);
893 g_return_val_if_fail (segment->format == format, FALSE);
895 /* if we have a stop position and a valid start and start is bigger,
896 * we're outside of the segment. (Special case) segment start and
897 * segment stop can be identical. In this case, if start is also identical,
898 * it's inside of segment */
899 if (G_UNLIKELY (segment->stop != -1 && start != -1 && (start > segment->stop
900 || (segment->start != segment->stop && start == segment->stop))))
903 /* if a stop position is given and is before the segment start,
904 * we're outside of the segment. Special case is were start
905 * and stop are equal to the segment start. In that case we
906 * are inside the segment. */
907 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
908 && stop == segment->start))))
915 *clip_start = MAX (start, segment->start);
920 *clip_stop = segment->stop;
921 else if (segment->stop == -1)
924 *clip_stop = MIN (stop, segment->stop);
931 * gst_segment_position_from_running_time:
932 * @segment: a #GstSegment structure.
933 * @format: the format of the segment.
934 * @running_time: the running_time in the segment
936 * Convert @running_time into a position in the segment so that
937 * gst_segment_to_running_time() with that position returns @running_time.
939 * Returns: the position in the segment for @running_time. This function returns
940 * -1 when @running_time is -1 or when it is not inside @segment.
945 gst_segment_position_from_running_time (const GstSegment * segment,
946 GstFormat format, guint64 running_time)
951 g_return_val_if_fail (segment != NULL, -1);
952 g_return_val_if_fail (segment->format == format, -1);
955 gst_segment_position_from_running_time_full (segment, format,
956 running_time, &position);
961 /* before the segment boundary */
962 if (G_UNLIKELY (position < segment->start)) {
963 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
964 ")", position, segment->start);
968 /* after the segment boundary */
969 if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
970 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
971 ")", position, segment->stop);
979 * gst_segment_position_from_running_time_full:
980 * @segment: a #GstSegment structure.
981 * @format: the format of the segment.
982 * @running_time: the running-time
983 * @position: (out): the resulting position in the segment
985 * Translate @running_time to the segment position using the currently configured
986 * segment. Compared to gst_segment_position_from_running_time() this function can
987 * return negative segment position.
989 * This function is typically used by elements that need to synchronize buffers
990 * against the clock or each other.
992 * @running_time can be any value and the result of this function for values
993 * outside of the segment is extrapolated.
995 * When 1 is returned, @running_time resulted in a positive position returned
998 * When this function returns -1, the returned @position was < 0, and the value
999 * in the position variable should be negated to get the real negative segment
1002 * Returns: a 1 or -1 on success, 0 on failure.
1007 gst_segment_position_from_running_time_full (const GstSegment * segment,
1008 GstFormat format, guint64 running_time, guint64 * position)
1011 guint64 start, stop, base;
1014 if (G_UNLIKELY (running_time == -1)) {
1019 g_return_val_if_fail (segment != NULL, 0);
1020 g_return_val_if_fail (segment->format == format, 0);
1022 base = segment->base;
1024 abs_rate = ABS (segment->rate);
1026 start = segment->start;
1027 stop = segment->stop;
1029 if (G_LIKELY (segment->rate > 0.0)) {
1030 /* start by subtracting the base time */
1031 if (G_LIKELY (running_time >= base)) {
1032 *position = running_time - base;
1033 /* move into the segment at the right rate */
1034 if (G_UNLIKELY (abs_rate != 1.0))
1035 *position = ceil (*position * abs_rate);
1036 /* bring to corrected position in segment */
1037 *position += start + segment->offset;
1040 *position = base - running_time;
1041 if (G_UNLIKELY (abs_rate != 1.0))
1042 *position = ceil (*position * abs_rate);
1043 if (start + segment->offset >= *position) {
1044 /* The TS is before the segment, but the result is >= 0 */
1045 *position = start + segment->offset - *position;
1048 /* The TS is before the segment, and the result is < 0
1049 * so negate the return result */
1050 *position = *position - (start + segment->offset);
1055 if (G_LIKELY (running_time >= base)) {
1056 *position = running_time - base;
1057 if (G_UNLIKELY (abs_rate != 1.0))
1058 *position = ceil (*position * abs_rate);
1059 if (G_UNLIKELY (stop < *position + segment->offset)) {
1060 *position += segment->offset - stop;
1063 *position = stop - *position - segment->offset;
1067 /* This case is tricky. Requested running time precedes the
1068 * segment base, so in a reversed segment where rate < 0, that
1069 * means it's before the alignment point of (stop - offset).
1070 * Before = always bigger than (stop-offset), which is usually +ve,
1071 * but could be -ve is offset is big enough. -ve position implies
1072 * that the offset has clipped away the entire segment anyway */
1073 *position = base - running_time;
1074 if (G_UNLIKELY (abs_rate != 1.0))
1075 *position = ceil (*position * abs_rate);
1077 if (G_LIKELY (stop + *position >= segment->offset)) {
1078 *position = stop + *position - segment->offset;
1081 /* Requested position is still negative because offset is big,
1082 * so negate the result */
1083 *position = segment->offset - *position - stop;
1092 * gst_segment_to_position:
1093 * @segment: a #GstSegment structure.
1094 * @format: the format of the segment.
1095 * @running_time: the running_time in the segment
1097 * Convert @running_time into a position in the segment so that
1098 * gst_segment_to_running_time() with that position returns @running_time.
1100 * Returns: the position in the segment for @running_time. This function returns
1101 * -1 when @running_time is -1 or when it is not inside @segment.
1103 * Deprecated: Use gst_segment_position_from_running_time() instead.
1105 #ifndef GST_REMOVE_DEPRECATED
1107 gst_segment_to_position (const GstSegment * segment, GstFormat format,
1108 guint64 running_time)
1110 return gst_segment_position_from_running_time (segment, format, running_time);
1115 * gst_segment_set_running_time:
1116 * @segment: a #GstSegment structure.
1117 * @format: the format of the segment.
1118 * @running_time: the running_time in the segment
1120 * Adjust the start/stop and base values of @segment such that the next valid
1121 * buffer will be one with @running_time.
1123 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
1124 * returned, @running_time is -1 or not in @segment.
1127 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
1128 guint64 running_time)
1131 guint64 start, stop;
1133 /* start by bringing the running_time into the segment position */
1135 gst_segment_position_from_running_time (segment, format, running_time);
1137 /* we must have a valid position now */
1138 if (G_UNLIKELY (position == -1))
1141 start = segment->start;
1142 stop = segment->stop;
1144 if (G_LIKELY (segment->rate > 0.0)) {
1145 /* update the start and time values */
1148 /* reverse, update stop */
1151 /* and base time is exactly the running time */
1152 segment->time = gst_segment_to_stream_time (segment, format, start);
1153 segment->start = start;
1154 segment->stop = stop;
1155 segment->base = running_time;
1161 * gst_segment_offset_running_time:
1162 * @segment: a #GstSegment structure.
1163 * @format: the format of the segment.
1164 * @offset: the offset to apply in the segment
1166 * Adjust the values in @segment so that @offset is applied to all
1167 * future running-time calculations.
1171 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
1172 * returned, @offset is not in @segment.
1175 gst_segment_offset_running_time (GstSegment * segment, GstFormat format,
1178 g_return_val_if_fail (segment != NULL, FALSE);
1179 g_return_val_if_fail (segment->format == format, FALSE);
1185 /* positive offset, we can simply apply to the base time */
1186 segment->base += offset;
1189 /* negative offset, first try to subtract from base */
1190 if (segment->base > offset) {
1191 segment->base -= offset;
1195 /* subtract all from segment.base, remainder in offset */
1196 offset -= segment->base;
1199 gst_segment_position_from_running_time (segment, format, offset);
1203 segment->offset = position - segment->start;
1210 * gst_segment_is_equal:
1211 * @s0: a #GstSegment structure.
1212 * @s1: a #GstSegment structure.
1214 * Checks for two segments being equal. Equality here is defined
1215 * as perfect equality, including floating point values.
1219 * Returns: %TRUE if the segments are equal, %FALSE otherwise.
1222 gst_segment_is_equal (const GstSegment * s0, const GstSegment * s1)
1224 if (s0->flags != s1->flags)
1226 if (s0->rate != s1->rate)
1228 if (s0->applied_rate != s1->applied_rate)
1230 if (s0->format != s1->format)
1232 if (s0->base != s1->base)
1234 if (s0->offset != s1->offset)
1236 if (s0->start != s1->start)
1238 if (s0->stop != s1->stop)
1240 if (s0->time != s1->time)
1242 if (s0->position != s1->position)
1244 if (s0->duration != s1->duration)