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 = 0;
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_seek:
86 * @segment: a #GstSegment structure.
87 * @rate: the rate of the segment.
88 * @format: the format of the segment.
89 * @flags: the seek flags for the segment
90 * @cur_type: the seek method
91 * @cur: the seek start value
92 * @stop_type: the seek method
93 * @stop: the seek stop value
95 * Update the segment structure with the field values of a seek event.
98 gst_segment_set_seek (GstSegment * segment, gdouble rate,
99 GstFormat format, GstSeekFlags flags,
100 GstSeekType cur_type, gint64 cur, GstSeekType stop_type, gint64 stop)
102 gboolean update_stop, update_start;
104 g_return_if_fail (rate != 0.0);
105 g_return_if_fail (segment != NULL);
106 g_return_if_fail (segment->format == format);
108 update_stop = update_start = TRUE;
110 /* start is never invalid */
112 case GST_SEEK_TYPE_NONE:
113 /* no update to segment */
114 cur = segment->start;
115 update_start = FALSE;
117 case GST_SEEK_TYPE_SET:
118 /* cur holds desired position */
120 case GST_SEEK_TYPE_CUR:
121 /* add cur to currently configure segment */
122 cur = segment->start + cur;
124 case GST_SEEK_TYPE_END:
125 if (segment->duration != -1) {
126 /* add cur to total length */
127 cur = segment->duration + cur;
129 /* no update if duration unknown */
130 cur = segment->start;
131 update_start = FALSE;
135 /* bring in sane range */
136 if (segment->duration != -1)
137 cur = CLAMP (cur, 0, segment->duration);
141 /* stop can be -1 if we have not configured a stop. */
143 case GST_SEEK_TYPE_NONE:
144 stop = segment->stop;
147 case GST_SEEK_TYPE_SET:
148 /* stop folds required value */
150 case GST_SEEK_TYPE_CUR:
151 if (segment->stop != -1)
152 stop = segment->stop + stop;
156 case GST_SEEK_TYPE_END:
157 if (segment->duration != -1)
158 stop = segment->duration + stop;
160 stop = segment->stop;
166 /* if we have a valid stop time, make sure it is clipped */
168 if (segment->duration != -1)
169 stop = CLAMP (stop, 0, segment->duration);
171 stop = MAX (stop, 0);
174 /* we can't have stop before start */
176 g_return_if_fail (cur <= stop);
178 segment->rate = rate;
179 segment->abs_rate = ABS (rate);
180 segment->flags = flags;
181 segment->start = cur;
182 segment->stop = stop;
186 * gst_segment_set_newsegment:
187 * @segment: a #GstSegment structure.
188 * @update: flag indicating a new segment is started or updated
189 * @rate: the rate of the segment.
190 * @format: the format of the segment.
191 * @start: the new start value
192 * @stop: the new stop value
193 * @time: the new stream time
195 * Update the segment structure with the field values of a new segment event.
198 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
199 GstFormat format, gint64 start, gint64 stop, gint64 time)
203 g_return_if_fail (rate != 0.0);
204 g_return_if_fail (segment != NULL);
205 g_return_if_fail (segment->format == format);
208 /* an update to the current segment is done, elapsed time is
209 * difference between the old start and new start. */
210 duration = start - segment->start;
212 /* the new segment has to be aligned with the old segment.
213 * We first update the accumulated time of the previous
214 * segment. the accumulated time is used when syncing to the
215 * clock. A flush event sets the accumulated time back to 0
217 if (GST_CLOCK_TIME_IS_VALID (segment->stop)) {
218 duration = segment->stop - segment->start;
219 } else if (GST_CLOCK_TIME_IS_VALID (segment->duration)) {
220 /* else use last seen timestamp as segment stop */
221 duration = segment->duration - segment->start;
223 /* else we don't know */
227 /* use previous rate to calculate duration */
228 segment->accum += gst_gdouble_to_guint64 (
229 (gst_guint64_to_gdouble (duration) / segment->abs_rate));
230 /* then update the current segment */
231 segment->rate = rate;
232 segment->abs_rate = ABS (rate);
233 segment->start = start;
234 segment->stop = stop;
235 segment->time = time;
237 GST_DEBUG ("received NEWSEGMENT %" GST_TIME_FORMAT " -- %"
238 GST_TIME_FORMAT ", time %" GST_TIME_FORMAT ", accum %"
240 GST_TIME_ARGS (segment->start),
241 GST_TIME_ARGS (segment->stop),
242 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->accum));
246 * gst_segment_to_stream_time:
247 * @segment: a #GstSegment structure.
248 * @format: the format of the segment.
249 * @position: the position in the segment
251 * Translate @position to stream time using the currently configured
254 * This function is typically used by elements that need to operate on
255 * the stream time of the buffers it receives, such as effect plugins.
257 * Returns: the position in stream_time.
260 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
265 g_return_val_if_fail (segment != NULL, FALSE);
266 g_return_val_if_fail (segment->format == format, FALSE);
268 result = ((position - segment->start) / segment->abs_rate) + segment->time;
274 * gst_segment_to_running_time:
275 * @segment: a #GstSegment structure.
276 * @format: the format of the segment.
277 * @position: the position in the segment
279 * Translate @position to the total running time using the currently configured
282 * This function is typically used by elements that need to synchronize to the
283 * global clock in a pipeline.
285 * Returns: the position as the total running time.
288 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
293 g_return_val_if_fail (segment != NULL, FALSE);
294 g_return_val_if_fail (segment->format == format, FALSE);
296 result = ((position - segment->start) / segment->abs_rate) + segment->accum;
303 * @segment: a #GstSegment structure.
304 * @format: the format of the segment.
305 * @start: the start position in the segment
306 * @stop: the stop position in the segment
307 * @clip_start: the clipped start position in the segment
308 * @clip_stop: the clipped stop position in the segment
310 * Clip the given @start and @stop values to the segment boundaries given
313 * Returns: TRUE if the given @start and @stop times fall partially in
314 * @segment, FALSE if the values are completely outside of the segment.
317 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
318 gint64 stop, gint64 * clip_start, gint64 * clip_stop)
320 g_return_val_if_fail (segment != NULL, FALSE);
321 g_return_val_if_fail (segment->format == format, FALSE);
323 /* we need a valid start position */
327 /* if we have a stop position and start is bigger, we're
328 * outside of the segment */
329 if (segment->stop != -1 && start >= segment->stop)
332 /* if a stop position is given and is before the segment start,
333 * we're outside of the segment */
334 if (stop != -1 && stop <= segment->start)
338 *clip_start = MAX (start, segment->start);
342 *clip_stop = segment->stop;
343 else if (segment->stop == -1)
344 *clip_stop = MAX (-1, stop);
346 *clip_stop = MIN (stop, segment->stop);
348 if (segment->duration != -1)
349 *clip_stop = MIN (*clip_stop, segment->duration);