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->abs_rate = 1.0;
55 segment->format = format;
61 segment->last_stop = -1;
62 segment->duration = -1;
66 * gst_segment_set_duration:
67 * @segment: a #GstSegment structure.
68 * @format: the format of the segment.
69 * @duration: the duration of the segment info.
71 * Set the duration of the segment to @duration. This function is mainly
72 * used by elements that perform seeking and know the total duration of the
76 gst_segment_set_duration (GstSegment * segment, GstFormat format,
79 g_return_if_fail (segment != NULL);
80 g_return_if_fail (segment->format == format);
82 segment->duration = duration;
86 * gst_segment_set_last_stop:
87 * @segment: a #GstSegment structure.
88 * @format: the format of the segment.
89 * @position: the position
91 * Set the last observed stop position in the segment to @position.
94 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
97 g_return_if_fail (segment != NULL);
98 g_return_if_fail (segment->format == format);
100 segment->last_stop = position;
104 * gst_segment_set_seek:
105 * @segment: a #GstSegment structure.
106 * @rate: the rate of the segment.
107 * @format: the format of the segment.
108 * @flags: the seek flags for the segment
109 * @cur_type: the seek method
110 * @cur: the seek start value
111 * @stop_type: the seek method
112 * @stop: the seek stop value
114 * Update the segment structure with the field values of a seek event.
117 gst_segment_set_seek (GstSegment * segment, gdouble rate,
118 GstFormat format, GstSeekFlags flags,
119 GstSeekType cur_type, gint64 cur, GstSeekType stop_type, gint64 stop)
121 gboolean update_stop, update_start;
123 g_return_if_fail (rate != 0.0);
124 g_return_if_fail (segment != NULL);
125 g_return_if_fail (segment->format == format);
127 update_stop = update_start = TRUE;
129 /* start is never invalid */
131 case GST_SEEK_TYPE_NONE:
132 /* no update to segment */
133 cur = segment->start;
134 update_start = FALSE;
136 case GST_SEEK_TYPE_SET:
137 /* cur holds desired position */
139 case GST_SEEK_TYPE_CUR:
140 /* add cur to currently configure segment */
141 cur = segment->start + cur;
143 case GST_SEEK_TYPE_END:
144 if (segment->duration != -1) {
145 /* add cur to total length */
146 cur = segment->duration + cur;
148 /* no update if duration unknown */
149 cur = segment->start;
150 update_start = FALSE;
154 /* bring in sane range */
155 if (segment->duration != -1)
156 cur = CLAMP (cur, 0, segment->duration);
160 /* stop can be -1 if we have not configured a stop. */
162 case GST_SEEK_TYPE_NONE:
163 stop = segment->stop;
166 case GST_SEEK_TYPE_SET:
167 /* stop folds required value */
169 case GST_SEEK_TYPE_CUR:
170 if (segment->stop != -1)
171 stop = segment->stop + stop;
175 case GST_SEEK_TYPE_END:
176 if (segment->duration != -1)
177 stop = segment->duration + stop;
179 stop = segment->stop;
185 /* if we have a valid stop time, make sure it is clipped */
187 if (segment->duration != -1)
188 stop = CLAMP (stop, 0, segment->duration);
190 stop = MAX (stop, 0);
193 /* we can't have stop before start */
195 g_return_if_fail (cur <= stop);
197 segment->rate = rate;
198 segment->abs_rate = ABS (rate);
199 segment->flags = flags;
200 segment->start = cur;
201 segment->stop = stop;
205 * gst_segment_set_newsegment:
206 * @segment: a #GstSegment structure.
207 * @update: flag indicating a new segment is started or updated
208 * @rate: the rate of the segment.
209 * @format: the format of the segment.
210 * @start: the new start value
211 * @stop: the new stop value
212 * @time: the new stream time
214 * Update the segment structure with the field values of a new segment event.
217 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
218 GstFormat format, gint64 start, gint64 stop, gint64 time)
222 g_return_if_fail (rate != 0.0);
223 g_return_if_fail (segment != NULL);
225 /* any other format with 0 also gives time 0, the other values are
226 * invalid in the format though. */
227 if (format != segment->format && start == 0) {
228 format = segment->format;
235 g_return_if_fail (segment->format == format);
238 /* an update to the current segment is done, elapsed time is
239 * difference between the old start and new start. */
240 duration = start - segment->start;
242 /* the new segment has to be aligned with the old segment.
243 * We first update the accumulated time of the previous
244 * segment. the accumulated time is used when syncing to the
247 if (GST_CLOCK_TIME_IS_VALID (segment->stop)) {
248 duration = segment->stop - segment->start;
249 } else if (GST_CLOCK_TIME_IS_VALID (segment->last_stop)) {
250 /* else use last seen timestamp as segment stop */
251 duration = segment->last_stop - segment->start;
253 /* else we don't know */
257 /* use previous rate to calculate duration */
258 segment->accum += gst_gdouble_to_guint64 (
259 (gst_guint64_to_gdouble (duration) / segment->abs_rate));
260 /* then update the current segment */
261 segment->rate = rate;
262 segment->abs_rate = ABS (rate);
263 segment->start = start;
264 segment->stop = stop;
265 segment->time = time;
269 * gst_segment_to_stream_time:
270 * @segment: a #GstSegment structure.
271 * @format: the format of the segment.
272 * @position: the position in the segment
274 * Translate @position to stream time using the currently configured
277 * This function is typically used by elements that need to operate on
278 * the stream time of the buffers it receives, such as effect plugins.
280 * Returns: the position in stream_time.
283 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
288 g_return_val_if_fail (segment != NULL, FALSE);
289 g_return_val_if_fail (segment->format == format, FALSE);
291 result = ((position - segment->start) / segment->abs_rate) + segment->time;
297 * gst_segment_to_running_time:
298 * @segment: a #GstSegment structure.
299 * @format: the format of the segment.
300 * @position: the position in the segment
302 * Translate @position to the total running time using the currently configured
305 * This function is typically used by elements that need to synchronize to the
306 * global clock in a pipeline.
308 * Returns: the position as the total running time.
311 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
316 g_return_val_if_fail (segment != NULL, FALSE);
317 g_return_val_if_fail (segment->format == format, FALSE);
319 result = ((position - segment->start) / segment->abs_rate) + segment->accum;
326 * @segment: a #GstSegment structure.
327 * @format: the format of the segment.
328 * @start: the start position in the segment
329 * @stop: the stop position in the segment
330 * @clip_start: the clipped start position in the segment
331 * @clip_stop: the clipped stop position in the segment
333 * Clip the given @start and @stop values to the segment boundaries given
336 * Returns: TRUE if the given @start and @stop times fall partially in
337 * @segment, FALSE if the values are completely outside of the segment.
340 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
341 gint64 stop, gint64 * clip_start, gint64 * clip_stop)
343 g_return_val_if_fail (segment != NULL, FALSE);
344 g_return_val_if_fail (segment->format == format, FALSE);
346 /* we need a valid start position */
350 /* if we have a stop position and start is bigger, we're
351 * outside of the segment */
352 if (segment->stop != -1 && start >= segment->stop)
355 /* if a stop position is given and is before the segment start,
356 * we're outside of the segment */
357 if (stop != -1 && stop <= segment->start)
361 *clip_start = MAX (start, segment->start);
365 *clip_stop = segment->stop;
366 else if (segment->stop == -1)
367 *clip_stop = MAX (-1, stop);
369 *clip_stop = MIN (stop, segment->stop);
371 if (segment->duration != -1)
372 *clip_stop = MIN (*clip_stop, segment->duration);