2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
4 * gstsegment.c: GstSegment subsystem
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "gst_private.h"
26 #include "gstsegment.h"
30 * @short_description: Structure describing the configured region of interest
32 * @see_also: #GstEvent
34 * This helper structure holds the relevant values for tracking the region of
35 * interest in a media file, called a segment.
37 * Last reviewed on 2005-20-09 (0.9.5)
42 * @segment: a #GstSegment structure.
43 * @format: the format of the segment.
45 * Initialize @segment to its default values, which is a rate of 1.0, a
49 gst_segment_init (GstSegment * segment, GstFormat format)
51 g_return_if_fail (segment != NULL);
54 segment->format = format;
60 segment->last_stop = -1;
61 segment->duration = -1;
65 * gst_segment_set_duration:
66 * @segment: a #GstSegment structure.
67 * @format: the format of the segment.
68 * @duration: the duration of the segment info.
70 * Set the duration of the segment to @duration. This function is mainly
71 * used by elements that perform seeking and know the total duration of the
75 gst_segment_set_duration (GstSegment * segment, GstFormat format,
78 g_return_if_fail (segment != NULL);
79 g_return_if_fail (segment->format == format);
81 segment->duration = duration;
85 * gst_segment_set_last_stop:
86 * @segment: a #GstSegment structure.
87 * @format: the format of the segment.
88 * @position: the position
90 * Set the last observed stop position in the segment to @position.
93 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
96 g_return_if_fail (segment != NULL);
97 g_return_if_fail (segment->format == format);
99 segment->last_stop = position;
103 * gst_segment_set_seek:
104 * @segment: a #GstSegment structure.
105 * @rate: the rate of the segment.
106 * @format: the format of the segment.
107 * @flags: the seek flags for the segment
108 * @cur_type: the seek method
109 * @cur: the seek start value
110 * @stop_type: the seek method
111 * @stop: the seek stop value
113 * Update the segment structure with the field values of a seek event.
116 gst_segment_set_seek (GstSegment * segment, gdouble rate,
117 GstFormat format, GstSeekFlags flags,
118 GstSeekType cur_type, gint64 cur, GstSeekType stop_type, gint64 stop)
120 gboolean update_stop, update_start;
122 g_return_if_fail (rate != 0.0);
123 g_return_if_fail (segment != NULL);
124 g_return_if_fail (segment->format == format);
126 update_stop = update_start = TRUE;
128 /* start is never invalid */
130 case GST_SEEK_TYPE_NONE:
131 /* no update to segment */
132 cur = segment->start;
133 update_start = FALSE;
135 case GST_SEEK_TYPE_SET:
136 /* cur holds desired position */
138 case GST_SEEK_TYPE_CUR:
139 /* add cur to currently configure segment */
140 cur = segment->start + cur;
142 case GST_SEEK_TYPE_END:
143 if (segment->duration != -1) {
144 /* add cur to total length */
145 cur = segment->duration + cur;
147 /* no update if duration unknown */
148 cur = segment->start;
149 update_start = FALSE;
153 /* bring in sane range */
154 if (segment->duration != -1)
155 cur = CLAMP (cur, 0, segment->duration);
159 /* stop can be -1 if we have not configured a stop. */
161 case GST_SEEK_TYPE_NONE:
162 stop = segment->stop;
165 case GST_SEEK_TYPE_SET:
166 /* stop folds required value */
168 case GST_SEEK_TYPE_CUR:
169 if (segment->stop != -1)
170 stop = segment->stop + stop;
174 case GST_SEEK_TYPE_END:
175 if (segment->duration != -1)
176 stop = segment->duration + stop;
178 stop = segment->stop;
184 /* if we have a valid stop time, make sure it is clipped */
186 if (segment->duration != -1)
187 stop = CLAMP (stop, 0, segment->duration);
189 stop = MAX (stop, 0);
192 /* we can't have stop before start */
194 g_return_if_fail (cur <= stop);
196 segment->rate = rate;
197 segment->abs_rate = ABS (rate);
198 segment->flags = flags;
199 segment->start = cur;
200 segment->stop = stop;
204 * gst_segment_set_newsegment:
205 * @segment: a #GstSegment structure.
206 * @update: flag indicating a new segment is started or updated
207 * @rate: the rate of the segment.
208 * @format: the format of the segment.
209 * @start: the new start value
210 * @stop: the new stop value
211 * @time: the new stream time
213 * Update the segment structure with the field values of a new segment event.
216 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
217 GstFormat format, gint64 start, gint64 stop, gint64 time)
221 g_return_if_fail (rate != 0.0);
222 g_return_if_fail (segment != NULL);
224 /* any other format with 0 also gives time 0, the other values are
225 * invalid in the format though. */
226 if (format != segment->format && start == 0) {
227 format = segment->format;
234 g_return_if_fail (segment->format == format);
237 /* an update to the current segment is done, elapsed time is
238 * difference between the old start and new start. */
239 duration = start - segment->start;
241 /* the new segment has to be aligned with the old segment.
242 * We first update the accumulated time of the previous
243 * segment. the accumulated time is used when syncing to the
246 if (GST_CLOCK_TIME_IS_VALID (segment->stop)) {
247 duration = segment->stop - segment->start;
248 } else if (GST_CLOCK_TIME_IS_VALID (segment->last_stop)) {
249 /* else use last seen timestamp as segment stop */
250 duration = segment->last_stop - segment->start;
252 /* else we don't know */
256 /* use previous rate to calculate duration */
257 segment->accum += gst_gdouble_to_guint64 (
258 (gst_guint64_to_gdouble (duration) / segment->abs_rate));
259 /* then update the current segment */
260 segment->rate = rate;
261 segment->abs_rate = ABS (rate);
262 segment->start = start;
263 segment->stop = stop;
264 segment->time = time;
268 * gst_segment_to_stream_time:
269 * @segment: a #GstSegment structure.
270 * @format: the format of the segment.
271 * @position: the position in the segment
273 * Translate @position to stream time using the currently configured
276 * This function is typically used by elements that need to operate on
277 * the stream time of the buffers it receives, such as effect plugins.
279 * Returns: the position in stream_time.
282 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
287 g_return_val_if_fail (segment != NULL, FALSE);
288 g_return_val_if_fail (segment->format == format, FALSE);
290 result = ((position - segment->start) / segment->abs_rate) + segment->time;
296 * gst_segment_to_running_time:
297 * @segment: a #GstSegment structure.
298 * @format: the format of the segment.
299 * @position: the position in the segment
301 * Translate @position to the total running time using the currently configured
304 * This function is typically used by elements that need to synchronize to the
305 * global clock in a pipeline.
307 * Returns: the position as the total running time.
310 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
315 g_return_val_if_fail (segment != NULL, FALSE);
316 g_return_val_if_fail (segment->format == format, FALSE);
318 result = ((position - segment->start) / segment->abs_rate) + segment->accum;
325 * @segment: a #GstSegment structure.
326 * @format: the format of the segment.
327 * @start: the start position in the segment
328 * @stop: the stop position in the segment
329 * @clip_start: the clipped start position in the segment
330 * @clip_stop: the clipped stop position in the segment
332 * Clip the given @start and @stop values to the segment boundaries given
335 * Returns: TRUE if the given @start and @stop times fall partially in
336 * @segment, FALSE if the values are completely outside of the segment.
339 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
340 gint64 stop, gint64 * clip_start, gint64 * clip_stop)
342 g_return_val_if_fail (segment != NULL, FALSE);
343 g_return_val_if_fail (segment->format == format, FALSE);
345 /* we need a valid start position */
349 /* if we have a stop position and start is bigger, we're
350 * outside of the segment */
351 if (segment->stop != -1 && start >= segment->stop)
354 /* if a stop position is given and is before the segment start,
355 * we're outside of the segment */
356 if (stop != -1 && stop <= segment->start)
360 *clip_start = MAX (start, segment->start);
364 *clip_stop = segment->stop;
365 else if (segment->stop == -1)
366 *clip_stop = MAX (-1, stop);
368 *clip_stop = MIN (stop, segment->stop);
370 if (segment->duration != -1)
371 *clip_stop = MIN (*clip_stop, segment->duration);