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 /* Elements should not pass instant-rate seeks to gst_segment_do_seek().
245 * This helps catch elements that have not been updated yet */
246 if (flags & GST_SEEK_FLAG_INSTANT_RATE_CHANGE)
249 update_start = update_stop = TRUE;
251 position = segment->position;
253 /* segment->start is never invalid */
254 switch (start_type) {
255 case GST_SEEK_TYPE_NONE:
256 /* no update to segment, take previous start */
257 start = segment->start;
258 update_start = FALSE;
260 case GST_SEEK_TYPE_SET:
261 /* start holds desired position, map -1 to the start */
265 case GST_SEEK_TYPE_END:
266 if (segment->duration != -1) {
267 /* add start to total length */
268 start = segment->duration + start;
270 /* no update if duration unknown */
271 start = segment->start;
272 update_start = FALSE;
276 /* bring in sane range */
277 if (segment->duration != -1)
278 start = MIN (start, segment->duration);
280 start = MAX ((gint64) start, 0);
282 /* stop can be -1 if we have not configured a stop. */
284 case GST_SEEK_TYPE_NONE:
285 stop = segment->stop;
288 case GST_SEEK_TYPE_SET:
289 /* stop holds required value */
291 case GST_SEEK_TYPE_END:
292 if (segment->duration != -1) {
293 stop = segment->duration + stop;
295 stop = segment->stop;
301 /* if we have a valid stop time, make sure it is clipped */
303 if (segment->duration != -1)
304 stop = CLAMP ((gint64) stop, 0, (gint64) segment->duration);
306 stop = MAX ((gint64) stop, 0);
309 /* we can't have stop before start */
312 GST_WARNING ("segment update failed: start(%" G_GUINT64_FORMAT
313 ") > stop(%" G_GUINT64_FORMAT ")", start, stop);
314 g_return_val_if_fail (start <= stop, FALSE);
319 if (flags & GST_SEEK_FLAG_FLUSH) {
320 /* flush resets the running_time */
323 /* make sure the position is inside the segment start/stop */
324 position = CLAMP (position, segment->start, segment->stop);
326 /* remember the elapsed time */
327 base = gst_segment_to_running_time (segment, format, position);
328 GST_DEBUG ("updated segment.base: %" G_GUINT64_FORMAT, base);
331 if (update_start && rate > 0.0) {
334 if (update_stop && rate < 0.0) {
338 if (segment->duration != -1)
339 position = segment->duration;
345 /* set update arg to reflect update of position */
347 *update = position != segment->position;
349 /* update new values */
350 /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
351 segment->flags = GST_SEGMENT_FLAG_NONE;
352 if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
353 segment->flags |= GST_SEGMENT_FLAG_RESET;
354 if ((flags & GST_SEEK_FLAG_TRICKMODE) != 0)
355 segment->flags |= GST_SEGMENT_FLAG_TRICKMODE;
356 if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
357 segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
358 if ((flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS) != 0)
359 segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS;
360 if ((flags & GST_SEEK_FLAG_TRICKMODE_NO_AUDIO) != 0)
361 segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_NO_AUDIO;
362 if ((flags & GST_SEEK_FLAG_TRICKMODE_FORWARD_PREDICTED) != 0)
363 segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_FORWARD_PREDICTED;
365 segment->rate = rate;
366 segment->applied_rate = 1.0;
368 segment->base = base;
370 segment->offset = position - start;
373 segment->offset = stop - position;
374 else if (segment->duration != -1)
375 segment->offset = segment->duration - position;
380 segment->start = start;
381 segment->stop = stop;
382 segment->time = start;
383 segment->position = position;
385 GST_INFO ("segment updated: %" GST_SEGMENT_FORMAT, segment);
391 * gst_segment_to_stream_time_full:
392 * @segment: a #GstSegment structure.
393 * @format: the format of the segment.
394 * @position: the position in the segment
395 * @stream_time: (out): result stream-time
397 * Translate @position to the total stream time using the currently configured
398 * segment. Compared to gst_segment_to_stream_time() this function can return
399 * negative stream-time.
401 * This function is typically used by elements that need to synchronize buffers
402 * against the clock or each other.
404 * @position can be any value and the result of this function for values outside
405 * of the segment is extrapolated.
407 * When 1 is returned, @position resulted in a positive stream-time returned
410 * When this function returns -1, the returned @stream_time should be negated
411 * to get the real negative stream time.
413 * Returns: a 1 or -1 on success, 0 on failure.
418 gst_segment_to_stream_time_full (const GstSegment * segment, GstFormat format,
419 guint64 position, guint64 * stream_time)
421 guint64 start, stop, time;
422 gdouble abs_applied_rate;
425 /* format does not matter for -1 */
426 if (G_UNLIKELY (position == -1)) {
431 g_return_val_if_fail (segment != NULL, 0);
432 g_return_val_if_fail (segment->format == format, 0);
434 stop = segment->stop;
436 start = segment->start;
437 time = segment->time;
439 /* time must be known */
440 if (G_UNLIKELY (time == -1))
443 abs_applied_rate = ABS (segment->applied_rate);
445 /* add or subtract from segment time based on applied rate */
446 if (G_LIKELY (segment->applied_rate > 0.0)) {
447 if (G_LIKELY (position > start)) {
448 /* bring to uncorrected position in segment */
449 *stream_time = position - start;
450 /* correct for applied rate if needed */
451 if (G_UNLIKELY (abs_applied_rate != 1.0))
452 *stream_time *= abs_applied_rate;
453 /* correct for segment time */
454 *stream_time += time;
457 *stream_time = start - position;
458 if (G_UNLIKELY (abs_applied_rate != 1.0))
459 *stream_time *= abs_applied_rate;
460 if (*stream_time > time) {
461 *stream_time -= time;
464 *stream_time = time - *stream_time;
469 /* correct for segment time. Streams with a negative applied_rate
470 * have timestamps between start and stop, as usual, but have the
471 * time member starting high and going backwards. */
472 /* cannot continue without a known segment stop */
473 if (G_UNLIKELY (stop == -1))
475 if (G_UNLIKELY (position > stop)) {
476 *stream_time = position - stop;
477 if (G_UNLIKELY (abs_applied_rate != 1.0))
478 *stream_time *= abs_applied_rate;
479 if (*stream_time > time) {
480 *stream_time -= time;
483 *stream_time = time - *stream_time;
487 *stream_time = stop - position;
488 if (G_UNLIKELY (abs_applied_rate != 1.0))
489 *stream_time *= abs_applied_rate;
490 *stream_time += time;
499 * gst_segment_to_stream_time:
500 * @segment: a #GstSegment structure.
501 * @format: the format of the segment.
502 * @position: the position in the segment
504 * Translate @position to stream time using the currently configured
505 * segment. The @position value must be between @segment start and
508 * This function is typically used by elements that need to operate on
509 * the stream time of the buffers it receives, such as effect plugins.
510 * In those use cases, @position is typically the buffer timestamp or
511 * clock time that one wants to convert to the stream time.
512 * The stream time is always between 0 and the total duration of the
515 * Returns: the position in stream_time or -1 when an invalid position
521 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
526 g_return_val_if_fail (segment != NULL, -1);
527 g_return_val_if_fail (segment->format == format, -1);
529 /* before the segment boundary */
530 if (G_UNLIKELY (position < segment->start)) {
531 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
532 ")", position, segment->start);
535 /* after the segment boundary */
536 if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
537 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
538 ")", position, segment->stop);
542 if (gst_segment_to_stream_time_full (segment, format, position, &result) == 1)
549 * gst_segment_position_from_stream_time_full:
550 * @segment: a #GstSegment structure.
551 * @format: the format of the segment.
552 * @stream_time: the stream-time
553 * @position: (out): the resulting position in the segment
555 * Translate @stream_time to the segment position using the currently configured
556 * segment. Compared to gst_segment_position_from_stream_time() this function can
557 * return negative segment position.
559 * This function is typically used by elements that need to synchronize buffers
560 * against the clock or each other.
562 * @stream_time can be any value and the result of this function for values outside
563 * of the segment is extrapolated.
565 * When 1 is returned, @stream_time resulted in a positive position returned
568 * When this function returns -1, the returned @position should be negated
569 * to get the real negative segment position.
571 * Returns: a 1 or -1 on success, 0 on failure.
576 gst_segment_position_from_stream_time_full (const GstSegment * segment,
577 GstFormat format, guint64 stream_time, guint64 * position)
580 gdouble abs_applied_rate;
583 /* format does not matter for -1 */
584 if (G_UNLIKELY (stream_time == -1)) {
589 g_return_val_if_fail (segment != NULL, -1);
590 g_return_val_if_fail (segment->format == format, -1);
592 start = segment->start;
593 time = segment->time;
595 /* time must be known */
596 if (G_UNLIKELY (time == -1))
599 abs_applied_rate = ABS (segment->applied_rate);
601 if (G_LIKELY (segment->applied_rate > 0.0)) {
602 if (G_LIKELY (stream_time > time)) {
604 *position = stream_time - time;
607 *position = time - stream_time;
609 /* correct for applied rate if needed */
610 if (G_UNLIKELY (abs_applied_rate != 1.0))
611 *position /= abs_applied_rate;
613 if (G_UNLIKELY (res == -1)) {
614 if (*position > start) {
617 *position = start - *position;
624 GstClockTime stop = segment->stop;
625 /* cannot continue without a known segment stop */
626 if (G_UNLIKELY (stop == -1))
628 if (G_UNLIKELY (time > stream_time)) {
630 *position = time - stream_time;
633 *position = stream_time - time;
635 if (G_UNLIKELY (abs_applied_rate != 1.0))
636 *position /= abs_applied_rate;
637 if (G_UNLIKELY (stop < *position)) {
638 if (G_LIKELY (res == 1)) {
646 if (G_LIKELY (res == 1)) {
647 *position = stop - *position;
660 * gst_segment_position_from_stream_time:
661 * @segment: a #GstSegment structure.
662 * @format: the format of the segment.
663 * @stream_time: the stream_time in the segment
665 * Convert @stream_time into a position in the segment so that
666 * gst_segment_to_stream_time() with that position returns @stream_time.
668 * Returns: the position in the segment for @stream_time. This function returns
669 * -1 when @stream_time is -1 or when it is not inside @segment.
674 gst_segment_position_from_stream_time (const GstSegment * segment,
675 GstFormat format, guint64 stream_time)
680 g_return_val_if_fail (segment != NULL, -1);
681 g_return_val_if_fail (segment->format == format, -1);
684 gst_segment_position_from_stream_time_full (segment, format, stream_time,
687 /* before the segment boundary */
688 if (G_UNLIKELY (position < segment->start)) {
689 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
690 ")", position, segment->start);
694 /* after the segment boundary */
695 if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
696 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
697 ")", position, segment->stop);
708 * gst_segment_to_running_time_full:
709 * @segment: a #GstSegment structure.
710 * @format: the format of the segment.
711 * @position: the position in the segment
712 * @running_time: (out) (allow-none): result running-time
714 * Translate @position to the total running time using the currently configured
715 * segment. Compared to gst_segment_to_running_time() this function can return
716 * negative running-time.
718 * This function is typically used by elements that need to synchronize buffers
719 * against the clock or each other.
721 * @position can be any value and the result of this function for values outside
722 * of the segment is extrapolated.
724 * When 1 is returned, @position resulted in a positive running-time returned
727 * When this function returns -1, the returned @running_time should be negated
728 * to get the real negative running time.
730 * Returns: a 1 or -1 on success, 0 on failure.
735 gst_segment_to_running_time_full (const GstSegment * segment, GstFormat format,
736 guint64 position, guint64 * running_time)
740 guint64 start, stop, offset;
743 if (G_UNLIKELY (position == -1)) {
744 GST_DEBUG ("invalid position (-1)");
748 g_return_val_if_fail (segment != NULL, 0);
749 g_return_val_if_fail (segment->format == format, 0);
751 offset = segment->offset;
753 if (G_LIKELY (segment->rate > 0.0)) {
754 start = segment->start + offset;
756 /* bring to uncorrected position in segment */
757 if (position < start) {
759 result = start - position;
762 result = position - start;
766 stop = segment->stop;
768 if (stop == -1 && segment->duration != -1)
769 stop = segment->start + segment->duration;
771 /* cannot continue if no stop position set or invalid offset */
772 g_return_val_if_fail (stop != -1, 0);
773 g_return_val_if_fail (stop >= offset, 0);
777 /* bring to uncorrected position in segment */
778 if (position > stop) {
780 result = position - stop;
783 result = stop - position;
789 /* scale based on the rate, avoid division by and conversion to
790 * float when not needed */
791 abs_rate = ABS (segment->rate);
792 if (G_UNLIKELY (abs_rate != 1.0))
795 /* correct for base of the segment */
797 /* positive, add base */
798 *running_time = result + segment->base;
799 else if (segment->base >= result) {
800 /* negative and base is bigger, subtract from base and we have a
801 * positive value again */
802 *running_time = segment->base - result;
805 /* negative and base is smaller, subtract base and remainder is
807 *running_time = result - segment->base;
821 * gst_segment_to_running_time:
822 * @segment: a #GstSegment structure.
823 * @format: the format of the segment.
824 * @position: the position in the segment
826 * Translate @position to the total running time using the currently configured
827 * segment. Position is a value between @segment start and stop time.
829 * This function is typically used by elements that need to synchronize to the
830 * global clock in a pipeline. The running time is a constantly increasing value
831 * starting from 0. When gst_segment_init() is called, this value will reset to
834 * This function returns -1 if the position is outside of @segment start and stop.
836 * Returns: the position as the total running time or -1 when an invalid position
840 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
845 g_return_val_if_fail (segment != NULL, -1);
846 g_return_val_if_fail (segment->format == format, -1);
848 /* before the segment boundary */
849 if (G_UNLIKELY (position < segment->start)) {
850 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
851 ")", position, segment->start);
854 /* after the segment boundary */
855 if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
856 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
857 ")", position, segment->stop);
861 if (gst_segment_to_running_time_full (segment, format, position,
870 * @segment: a #GstSegment structure.
871 * @format: the format of the segment.
872 * @start: the start position in the segment
873 * @stop: the stop position in the segment
874 * @clip_start: (out) (allow-none): the clipped start position in the segment
875 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
877 * Clip the given @start and @stop values to the segment boundaries given
878 * in @segment. @start and @stop are compared and clipped to @segment
879 * start and stop values.
881 * If the function returns %FALSE, @start and @stop are known to fall
882 * outside of @segment and @clip_start and @clip_stop are not updated.
884 * When the function returns %TRUE, @clip_start and @clip_stop will be
885 * updated. If @clip_start or @clip_stop are different from @start or @stop
886 * respectively, the region fell partially in the segment.
888 * Note that when @stop is -1, @clip_stop will be set to the end of the
889 * segment. Depending on the use case, this may or may not be what you want.
891 * Returns: %TRUE if the given @start and @stop times fall partially or
892 * completely in @segment, %FALSE if the values are completely outside
896 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
897 guint64 stop, guint64 * clip_start, guint64 * clip_stop)
899 g_return_val_if_fail (segment != NULL, FALSE);
900 g_return_val_if_fail (segment->format == format, FALSE);
902 /* if we have a stop position and a valid start and start is bigger,
903 * we're outside of the segment. (Special case) segment start and
904 * segment stop can be identical. In this case, if start is also identical,
905 * it's inside of segment */
906 if (G_UNLIKELY (segment->stop != -1 && start != -1 && (start > segment->stop
907 || (segment->start != segment->stop && start == segment->stop))))
910 /* if a stop position is given and is before the segment start,
911 * we're outside of the segment. Special case is were start
912 * and stop are equal to the segment start. In that case we
913 * are inside the segment. */
914 if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
915 && stop == segment->start))))
922 *clip_start = MAX (start, segment->start);
927 *clip_stop = segment->stop;
928 else if (segment->stop == -1)
931 *clip_stop = MIN (stop, segment->stop);
938 * gst_segment_position_from_running_time:
939 * @segment: a #GstSegment structure.
940 * @format: the format of the segment.
941 * @running_time: the running_time in the segment
943 * Convert @running_time into a position in the segment so that
944 * gst_segment_to_running_time() with that position returns @running_time.
946 * Returns: the position in the segment for @running_time. This function returns
947 * -1 when @running_time is -1 or when it is not inside @segment.
952 gst_segment_position_from_running_time (const GstSegment * segment,
953 GstFormat format, guint64 running_time)
958 g_return_val_if_fail (segment != NULL, -1);
959 g_return_val_if_fail (segment->format == format, -1);
962 gst_segment_position_from_running_time_full (segment, format,
963 running_time, &position);
968 /* before the segment boundary */
969 if (G_UNLIKELY (position < segment->start)) {
970 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
971 ")", position, segment->start);
975 /* after the segment boundary */
976 if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
977 GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
978 ")", position, segment->stop);
986 * gst_segment_position_from_running_time_full:
987 * @segment: a #GstSegment structure.
988 * @format: the format of the segment.
989 * @running_time: the running-time
990 * @position: (out): the resulting position in the segment
992 * Translate @running_time to the segment position using the currently configured
993 * segment. Compared to gst_segment_position_from_running_time() this function can
994 * return negative segment position.
996 * This function is typically used by elements that need to synchronize buffers
997 * against the clock or each other.
999 * @running_time can be any value and the result of this function for values
1000 * outside of the segment is extrapolated.
1002 * When 1 is returned, @running_time resulted in a positive position returned
1005 * When this function returns -1, the returned @position was < 0, and the value
1006 * in the position variable should be negated to get the real negative segment
1009 * Returns: a 1 or -1 on success, 0 on failure.
1014 gst_segment_position_from_running_time_full (const GstSegment * segment,
1015 GstFormat format, guint64 running_time, guint64 * position)
1018 guint64 start, stop, base;
1021 if (G_UNLIKELY (running_time == -1)) {
1026 g_return_val_if_fail (segment != NULL, 0);
1027 g_return_val_if_fail (segment->format == format, 0);
1029 base = segment->base;
1031 abs_rate = ABS (segment->rate);
1033 start = segment->start;
1034 stop = segment->stop;
1036 if (G_LIKELY (segment->rate > 0.0)) {
1037 /* start by subtracting the base time */
1038 if (G_LIKELY (running_time >= base)) {
1039 *position = running_time - base;
1040 /* move into the segment at the right rate */
1041 if (G_UNLIKELY (abs_rate != 1.0))
1042 *position = ceil (*position * abs_rate);
1043 /* bring to corrected position in segment */
1044 *position += start + segment->offset;
1047 *position = base - running_time;
1048 if (G_UNLIKELY (abs_rate != 1.0))
1049 *position = ceil (*position * abs_rate);
1050 if (start + segment->offset >= *position) {
1051 /* The TS is before the segment, but the result is >= 0 */
1052 *position = start + segment->offset - *position;
1055 /* The TS is before the segment, and the result is < 0
1056 * so negate the return result */
1057 *position = *position - (start + segment->offset);
1062 if (G_LIKELY (running_time >= base)) {
1063 *position = running_time - base;
1064 if (G_UNLIKELY (abs_rate != 1.0))
1065 *position = ceil (*position * abs_rate);
1066 if (G_UNLIKELY (stop < *position + segment->offset)) {
1067 *position += segment->offset - stop;
1070 *position = stop - *position - segment->offset;
1074 /* This case is tricky. Requested running time precedes the
1075 * segment base, so in a reversed segment where rate < 0, that
1076 * means it's before the alignment point of (stop - offset).
1077 * Before = always bigger than (stop-offset), which is usually +ve,
1078 * but could be -ve is offset is big enough. -ve position implies
1079 * that the offset has clipped away the entire segment anyway */
1080 *position = base - running_time;
1081 if (G_UNLIKELY (abs_rate != 1.0))
1082 *position = ceil (*position * abs_rate);
1084 if (G_LIKELY (stop + *position >= segment->offset)) {
1085 *position = stop + *position - segment->offset;
1088 /* Requested position is still negative because offset is big,
1089 * so negate the result */
1090 *position = segment->offset - *position - stop;
1099 * gst_segment_to_position:
1100 * @segment: a #GstSegment structure.
1101 * @format: the format of the segment.
1102 * @running_time: the running_time in the segment
1104 * Convert @running_time into a position in the segment so that
1105 * gst_segment_to_running_time() with that position returns @running_time.
1107 * Returns: the position in the segment for @running_time. This function returns
1108 * -1 when @running_time is -1 or when it is not inside @segment.
1110 * Deprecated: Use gst_segment_position_from_running_time() instead.
1112 #ifndef GST_REMOVE_DEPRECATED
1114 gst_segment_to_position (const GstSegment * segment, GstFormat format,
1115 guint64 running_time)
1117 return gst_segment_position_from_running_time (segment, format, running_time);
1122 * gst_segment_set_running_time:
1123 * @segment: a #GstSegment structure.
1124 * @format: the format of the segment.
1125 * @running_time: the running_time in the segment
1127 * Adjust the start/stop and base values of @segment such that the next valid
1128 * buffer will be one with @running_time.
1130 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
1131 * returned, @running_time is -1 or not in @segment.
1134 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
1135 guint64 running_time)
1138 guint64 start, stop;
1140 /* start by bringing the running_time into the segment position */
1142 gst_segment_position_from_running_time (segment, format, running_time);
1144 /* we must have a valid position now */
1145 if (G_UNLIKELY (position == -1))
1148 start = segment->start;
1149 stop = segment->stop;
1151 if (G_LIKELY (segment->rate > 0.0)) {
1152 /* update the start and time values */
1155 /* reverse, update stop */
1158 /* and base time is exactly the running time */
1159 segment->time = gst_segment_to_stream_time (segment, format, start);
1160 segment->start = start;
1161 segment->stop = stop;
1162 segment->base = running_time;
1168 * gst_segment_offset_running_time:
1169 * @segment: a #GstSegment structure.
1170 * @format: the format of the segment.
1171 * @offset: the offset to apply in the segment
1173 * Adjust the values in @segment so that @offset is applied to all
1174 * future running-time calculations.
1178 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
1179 * returned, @offset is not in @segment.
1182 gst_segment_offset_running_time (GstSegment * segment, GstFormat format,
1185 g_return_val_if_fail (segment != NULL, FALSE);
1186 g_return_val_if_fail (segment->format == format, FALSE);
1192 /* positive offset, we can simply apply to the base time */
1193 segment->base += offset;
1196 /* negative offset, first try to subtract from base */
1197 if (segment->base > offset) {
1198 segment->base -= offset;
1202 /* subtract all from segment.base, remainder in offset */
1203 offset -= segment->base;
1206 gst_segment_position_from_running_time (segment, format, offset);
1210 segment->offset = position - segment->start;
1217 * gst_segment_is_equal:
1218 * @s0: a #GstSegment structure.
1219 * @s1: a #GstSegment structure.
1221 * Checks for two segments being equal. Equality here is defined
1222 * as perfect equality, including floating point values.
1226 * Returns: %TRUE if the segments are equal, %FALSE otherwise.
1229 gst_segment_is_equal (const GstSegment * s0, const GstSegment * s1)
1231 if (s0->flags != s1->flags)
1233 if (s0->rate != s1->rate)
1235 if (s0->applied_rate != s1->applied_rate)
1237 if (s0->format != s1->format)
1239 if (s0->base != s1->base)
1241 if (s0->offset != s1->offset)
1243 if (s0->start != s1->start)
1245 if (s0->stop != s1->stop)
1247 if (s0->time != s1->time)
1249 if (s0->position != s1->position)
1251 if (s0->duration != s1->duration)