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 eachother.
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 eachother.
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 /* cannot continue if no stop position set or invalid offset */
762 g_return_val_if_fail (stop != -1, 0);
763 g_return_val_if_fail (stop >= offset, 0);
767 /* bring to uncorrected position in segment */
768 if (position > stop) {
770 result = position - stop;
773 result = stop - position;
779 /* scale based on the rate, avoid division by and conversion to
780 * float when not needed */
781 abs_rate = ABS (segment->rate);
782 if (G_UNLIKELY (abs_rate != 1.0))
785 /* correct for base of the segment */
787 /* positive, add base */
788 *running_time = result + segment->base;
789 else if (segment->base >= result) {
790 /* negative and base is bigger, subtract from base and we have a
791 * positive value again */
792 *running_time = segment->base - result;
795 /* negative and base is smaller, subtract base and remainder is
797 *running_time = result - segment->base;
811 * gst_segment_to_running_time:
812 * @segment: a #GstSegment structure.
813 * @format: the format of the segment.
814 * @position: the position in the segment
816 * Translate @position to the total running time using the currently configured
817 * segment. Position is a value between @segment start and stop time.
819 * This function is typically used by elements that need to synchronize to the
820 * global clock in a pipeline. The running time is a constantly increasing value
821 * starting from 0. When gst_segment_init() is called, this value will reset to
824 * This function returns -1 if the position is outside of @segment start and stop.
826 * Returns: the position as the total running time or -1 when an invalid position
830 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
835 g_return_val_if_fail (segment != NULL, -1);
836 g_return_val_if_fail (segment->format == format, -1);
838 /* before the segment boundary */
839 if (G_UNLIKELY (position < segment->start)) {
840 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
841 ")", position, segment->start);
844 /* after the segment boundary */
845 if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
846 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
847 ")", position, segment->stop);
851 if (gst_segment_to_running_time_full (segment, format, position,
860 * @segment: a #GstSegment structure.
861 * @format: the format of the segment.
862 * @start: the start position in the segment
863 * @stop: the stop position in the segment
864 * @clip_start: (out) (allow-none): the clipped start position in the segment
865 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
867 * Clip the given @start and @stop values to the segment boundaries given
868 * in @segment. @start and @stop are compared and clipped to @segment
869 * start and stop values.
871 * If the function returns %FALSE, @start and @stop are known to fall
872 * outside of @segment and @clip_start and @clip_stop are not updated.
874 * When the function returns %TRUE, @clip_start and @clip_stop will be
875 * updated. If @clip_start or @clip_stop are different from @start or @stop
876 * respectively, the region fell partially in the segment.
878 * Note that when @stop is -1, @clip_stop will be set to the end of the
879 * segment. Depending on the use case, this may or may not be what you want.
881 * Returns: %TRUE if the given @start and @stop times fall partially or
882 * completely in @segment, %FALSE if the values are completely outside
886 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
887 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
889 g_return_val_if_fail (segment != NULL, FALSE);
890 g_return_val_if_fail (segment->format == format, FALSE);
892 /* if we have a stop position and a valid start and start is bigger,
893 * we're outside of the segment. (Special case) segment start and
894 * segment stop can be identical. In this case, if start is also identical,
895 * it's inside of segment */
896 if (G_UNLIKELY (segment->stop != -1 && start != -1 && (start > segment->stop
897 || (segment->start != segment->stop && start == segment->stop))))
900 /* if a stop position is given and is before the segment start,
901 * we're outside of the segment. Special case is were start
902 * and stop are equal to the segment start. In that case we
903 * are inside the segment. */
904 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
905 && stop == segment->start))))
912 *clip_start = MAX (start, segment->start);
917 *clip_stop = segment->stop;
918 else if (segment->stop == -1)
921 *clip_stop = MIN (stop, segment->stop);
928 * gst_segment_position_from_running_time:
929 * @segment: a #GstSegment structure.
930 * @format: the format of the segment.
931 * @running_time: the running_time in the segment
933 * Convert @running_time into a position in the segment so that
934 * gst_segment_to_running_time() with that position returns @running_time.
936 * Returns: the position in the segment for @running_time. This function returns
937 * -1 when @running_time is -1 or when it is not inside @segment.
942 gst_segment_position_from_running_time (const GstSegment * segment,
943 GstFormat format, guint64 running_time)
948 g_return_val_if_fail (segment != NULL, -1);
949 g_return_val_if_fail (segment->format == format, -1);
952 gst_segment_position_from_running_time_full (segment, format,
953 running_time, &position);
958 /* before the segment boundary */
959 if (G_UNLIKELY (position < segment->start)) {
960 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
961 ")", position, segment->start);
965 /* after the segment boundary */
966 if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
967 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
968 ")", position, segment->stop);
976 * gst_segment_position_from_running_time_full:
977 * @segment: a #GstSegment structure.
978 * @format: the format of the segment.
979 * @running_time: the running-time
980 * @position: (out): the resulting position in the segment
982 * Translate @running_time to the segment position using the currently configured
983 * segment. Compared to gst_segment_position_from_running_time() this function can
984 * return negative segment position.
986 * This function is typically used by elements that need to synchronize buffers
987 * against the clock or each other.
989 * @running_time can be any value and the result of this function for values
990 * outside of the segment is extrapolated.
992 * When 1 is returned, @running_time resulted in a positive position returned
995 * When this function returns -1, the returned @position should be negated
996 * to get the real negative segment position.
998 * Returns: a 1 or -1 on success, 0 on failure.
1003 gst_segment_position_from_running_time_full (const GstSegment * segment,
1004 GstFormat format, guint64 running_time, guint64 * position)
1007 guint64 start, stop, base;
1010 if (G_UNLIKELY (running_time == -1)) {
1015 g_return_val_if_fail (segment != NULL, 0);
1016 g_return_val_if_fail (segment->format == format, 0);
1018 base = segment->base;
1020 abs_rate = ABS (segment->rate);
1022 start = segment->start;
1023 stop = segment->stop;
1025 if (G_LIKELY (segment->rate > 0.0)) {
1026 /* start by subtracting the base time */
1027 if (G_LIKELY (running_time >= base)) {
1028 *position = running_time - base;
1029 /* move into the segment at the right rate */
1030 if (G_UNLIKELY (abs_rate != 1.0))
1031 *position = ceil (*position * abs_rate);
1032 /* bring to corrected position in segment */
1033 *position += start + segment->offset;
1036 *position = base - running_time;
1037 if (G_UNLIKELY (abs_rate != 1.0))
1038 *position = ceil (*position * abs_rate);
1039 if (start + segment->offset > *position) {
1040 *position -= start + segment->offset;
1043 *position = start + segment->offset - *position;
1048 if (G_LIKELY (running_time >= base)) {
1049 *position = running_time - base;
1050 if (G_UNLIKELY (abs_rate != 1.0))
1051 *position = ceil (*position * abs_rate);
1052 if (G_UNLIKELY (stop < *position + segment->offset)) {
1053 *position += segment->offset - stop;
1056 *position = stop - *position - segment->offset;
1060 *position = base - running_time;
1061 if (G_UNLIKELY (abs_rate != 1.0))
1062 *position = ceil (*position * abs_rate);
1063 if (G_UNLIKELY (stop < segment->offset - *position)) {
1064 *position -= segment->offset - stop;
1067 *position = stop + *position - segment->offset;
1076 * gst_segment_to_position:
1077 * @segment: a #GstSegment structure.
1078 * @format: the format of the segment.
1079 * @running_time: the running_time in the segment
1081 * Convert @running_time into a position in the segment so that
1082 * gst_segment_to_running_time() with that position returns @running_time.
1084 * Returns: the position in the segment for @running_time. This function returns
1085 * -1 when @running_time is -1 or when it is not inside @segment.
1087 * Deprecated. Use gst_segment_position_from_running_time() instead.
1089 #ifndef GST_REMOVE_DEPRECATED
1091 gst_segment_to_position (const GstSegment * segment, GstFormat format,
1092 guint64 running_time)
1094 return gst_segment_position_from_running_time (segment, format, running_time);
1099 * gst_segment_set_running_time:
1100 * @segment: a #GstSegment structure.
1101 * @format: the format of the segment.
1102 * @running_time: the running_time in the segment
1104 * Adjust the start/stop and base values of @segment such that the next valid
1105 * buffer will be one with @running_time.
1107 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
1108 * returned, @running_time is -1 or not in @segment.
1111 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
1112 guint64 running_time)
1115 guint64 start, stop;
1117 /* start by bringing the running_time into the segment position */
1119 gst_segment_position_from_running_time (segment, format, running_time);
1121 /* we must have a valid position now */
1122 if (G_UNLIKELY (position == -1))
1125 start = segment->start;
1126 stop = segment->stop;
1128 if (G_LIKELY (segment->rate > 0.0)) {
1129 /* update the start and time values */
1132 /* reverse, update stop */
1135 /* and base time is exactly the running time */
1136 segment->time = gst_segment_to_stream_time (segment, format, start);
1137 segment->start = start;
1138 segment->stop = stop;
1139 segment->base = running_time;
1145 * gst_segment_offset_running_time:
1146 * @segment: a #GstSegment structure.
1147 * @format: the format of the segment.
1148 * @offset: the offset to apply in the segment
1150 * Adjust the values in @segment so that @offset is applied to all
1151 * future running-time calculations.
1155 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
1156 * returned, @offset is not in @segment.
1159 gst_segment_offset_running_time (GstSegment * segment, GstFormat format,
1162 g_return_val_if_fail (segment != NULL, FALSE);
1163 g_return_val_if_fail (segment->format == format, FALSE);
1169 /* positive offset, we can simply apply to the base time */
1170 segment->base += offset;
1173 /* negative offset, first try to subtract from base */
1174 if (segment->base > offset) {
1175 segment->base -= offset;
1179 /* subtract all from segment.base, remainder in offset */
1180 offset -= segment->base;
1183 gst_segment_position_from_running_time (segment, format, offset);
1187 segment->offset = position - segment->start;
1194 * gst_segment_is_equal:
1195 * @s0: a #GstSegment structure.
1196 * @s1: a #GstSegment structure.
1198 * Checks for two segments being equal. Equality here is defined
1199 * as perfect equality, including floating point values.
1203 * Returns: %TRUE if the segments are equal, %FALSE otherwise.
1206 gst_segment_is_equal (const GstSegment * s0, const GstSegment * s1)
1208 if (s0->flags != s1->flags)
1210 if (s0->rate != s1->rate)
1212 if (s0->applied_rate != s1->applied_rate)
1214 if (s0->format != s1->format)
1216 if (s0->base != s1->base)
1218 if (s0->offset != s1->offset)
1220 if (s0->start != s1->start)
1222 if (s0->stop != s1->stop)
1224 if (s0->time != s1->time)
1226 if (s0->position != s1->position)
1228 if (s0->duration != s1->duration)