2.0 beta init
[framework/multimedia/gstreamer0.10.git] / gst / gstsegment.c
1 /* GStreamer
2  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3  *
4  * gstsegment.c: GstSegment subsystem
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "gst_private.h"
23
24 #include <math.h>
25
26 #include "gstutils.h"
27 #include "gstsegment.h"
28
29 /**
30  * SECTION:gstsegment
31  * @short_description: Structure describing the configured region of interest
32  *                     in a media file.
33  * @see_also: #GstEvent
34  *
35  * This helper structure holds the relevant values for tracking the region of
36  * interest in a media file, called a segment. 
37  *
38  * The structure can be used for two purposes:
39  * <itemizedlist>
40  *   <listitem><para>performing seeks (handling seek events)</para></listitem>
41  *   <listitem><para>tracking playback regions (handling newsegment events)</para></listitem>
42  * </itemizedlist>
43  *
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.
46  *
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.
49  *
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.
54  *
55  * If the segment is used for managing seeks, the segment duration should be set with
56  * gst_segment_set_duration(). The public duration field contains the duration of the
57  * segment. When using the segment for seeking, the start and time members should 
58  * normally be left to their default 0 value. The stop position is left to -1 unless
59  * explicitly configured to a different value after a seek event.
60  *
61  * The current position in the segment should be set with the gst_segment_set_last_stop().
62  * The public last_stop field contains the last set stop position in the segment.
63  *
64  * For elements that perform seeks, the current segment should be updated with the
65  * gst_segment_set_seek() and the values from the seek event. This method will update
66  * all the segment fields. The last_stop field will contain the new playback position.
67  * If the cur_type was different from GST_SEEK_TYPE_NONE, playback continues from
68  * the last_stop position, possibly with updated flags or rate.
69  *
70  * For elements that want to use #GstSegment to track the playback region, use
71  * gst_segment_set_newsegment() to update the segment fields with the information from
72  * the newsegment event. The gst_segment_clip() method can be used to check and clip
73  * the media data to the segment boundaries.
74  *
75  * For elements that want to synchronize to the pipeline clock, gst_segment_to_running_time()
76  * can be used to convert a timestamp to a value that can be used to synchronize
77  * to the clock. This function takes into account all accumulated segments as well as
78  * any rate or applied_rate conversions.
79  *
80  * For elements that need to perform operations on media data in stream_time, 
81  * gst_segment_to_stream_time() can be used to convert a timestamp and the segment
82  * info to stream time (which is always between 0 and the duration of the stream).
83  *
84  * Last reviewed on 2007-05-17 (0.10.13)
85  */
86
87 /**
88  * gst_segment_copy:
89  * @segment: (transfer none): a #GstSegment
90  *
91  * Create a copy of given @segment.
92  *
93  * Free-function: gst_segment_free
94  *
95  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
96  *
97  * Since: 0.10.20
98  */
99 GstSegment *
100 gst_segment_copy (GstSegment * segment)
101 {
102   GstSegment *result = NULL;
103
104   if (segment) {
105     result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
106   }
107   return result;
108 }
109
110 GType
111 gst_segment_get_type (void)
112 {
113   static GType gst_segment_type = 0;
114
115   if (G_UNLIKELY (gst_segment_type == 0)) {
116     gst_segment_type = g_boxed_type_register_static ("GstSegment",
117         (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
118   }
119
120   return gst_segment_type;
121 }
122
123 /**
124  * gst_segment_new:
125  *
126  * Allocate a new #GstSegment structure and initialize it using 
127  * gst_segment_init().
128  *
129  * Free-function: gst_segment_free
130  *
131  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
132  */
133 GstSegment *
134 gst_segment_new (void)
135 {
136   GstSegment *result;
137
138   result = g_slice_new0 (GstSegment);
139   gst_segment_init (result, GST_FORMAT_UNDEFINED);
140
141   return result;
142 }
143
144 /**
145  * gst_segment_free:
146  * @segment: (in) (transfer full): a #GstSegment
147  *
148  * Free the allocated segment @segment.
149  */
150 void
151 gst_segment_free (GstSegment * segment)
152 {
153   g_slice_free (GstSegment, segment);
154 }
155
156 /**
157  * gst_segment_init:
158  * @segment: a #GstSegment structure.
159  * @format: the format of the segment.
160  *
161  * The start/last_stop positions are set to 0 and the stop/duration
162  * fields are set to -1 (unknown). The default rate of 1.0 and no
163  * flags are set.
164  *
165  * Initialize @segment to its default values.
166  */
167 void
168 gst_segment_init (GstSegment * segment, GstFormat format)
169 {
170   g_return_if_fail (segment != NULL);
171
172   segment->rate = 1.0;
173   segment->abs_rate = 1.0;
174   segment->applied_rate = 1.0;
175   segment->format = format;
176   segment->flags = GST_SEEK_FLAG_NONE;
177   segment->start = 0;
178   segment->stop = -1;
179   segment->time = 0;
180   segment->accum = 0;
181   segment->last_stop = 0;
182   segment->duration = -1;
183 }
184
185 /**
186  * gst_segment_set_duration:
187  * @segment: a #GstSegment structure.
188  * @format: the format of the segment.
189  * @duration: the duration of the segment info or -1 if unknown.
190  *
191  * Set the duration of the segment to @duration. This function is mainly
192  * used by elements that perform seeking and know the total duration of the
193  * segment. 
194  * 
195  * This field should be set to allow seeking requests relative to the
196  * duration.
197  */
198 void
199 gst_segment_set_duration (GstSegment * segment, GstFormat format,
200     gint64 duration)
201 {
202   g_return_if_fail (segment != NULL);
203
204   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
205     segment->format = format;
206   else
207     g_return_if_fail (segment->format == format);
208
209   segment->duration = duration;
210 }
211
212 /**
213  * gst_segment_set_last_stop:
214  * @segment: a #GstSegment structure.
215  * @format: the format of the segment.
216  * @position: the position 
217  *
218  * Set the last observed stop position in the segment to @position.
219  *
220  * This field should be set to allow seeking requests relative to the
221  * current playing position.
222  */
223 void
224 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
225     gint64 position)
226 {
227   g_return_if_fail (segment != NULL);
228
229   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
230     segment->format = format;
231   else
232     g_return_if_fail (segment->format == format);
233
234   segment->last_stop = MAX (segment->start, position);
235 }
236
237 /**
238  * gst_segment_set_seek:
239  * @segment: a #GstSegment structure.
240  * @rate: the rate of the segment.
241  * @format: the format of the segment.
242  * @flags: the seek flags for the segment
243  * @start_type: the seek method
244  * @start: the seek start value
245  * @stop_type: the seek method
246  * @stop: the seek stop value
247  * @update: boolean holding whether last_stop was updated.
248  *
249  * Update the segment structure with the field values of a seek event (see
250  * gst_event_new_seek()).
251  *
252  * After calling this method, the segment field last_stop and time will
253  * contain the requested new position in the segment. The new requested
254  * position in the segment depends on @rate and @start_type and @stop_type. 
255  *
256  * For positive @rate, the new position in the segment is the new @segment
257  * start field when it was updated with a @start_type different from
258  * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
259  * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment last_stop is
260  * unmodified.
261  *
262  * For negative @rate, the new position in the segment is the new @segment
263  * stop field when it was updated with a @stop_type different from
264  * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
265  * duration of the segment will be used to update the stop position.
266  * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
267  * @stop is ignored and @segment last_stop is unmodified.
268  *
269  * The applied rate of the segment will be set to 1.0 by default.
270  * If the caller can apply a rate change, it should update @segment
271  * rate and applied_rate after calling this function.
272  *
273  * @update will be set to TRUE if a seek should be performed to the segment 
274  * last_stop field. This field can be FALSE if, for example, only the @rate
275  * has been changed but not the playback position.
276  */
277 void
278 gst_segment_set_seek (GstSegment * segment, gdouble rate,
279     GstFormat format, GstSeekFlags flags,
280     GstSeekType start_type, gint64 start,
281     GstSeekType stop_type, gint64 stop, gboolean * update)
282 {
283   gboolean update_stop, update_start;
284   gint64 last_stop;
285
286   g_return_if_fail (rate != 0.0);
287   g_return_if_fail (segment != NULL);
288
289   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
290     segment->format = format;
291
292   update_start = update_stop = TRUE;
293
294   /* segment->start is never invalid */
295   switch (start_type) {
296     case GST_SEEK_TYPE_NONE:
297       /* no update to segment, take previous start */
298       start = segment->start;
299       update_start = FALSE;
300       break;
301     case GST_SEEK_TYPE_SET:
302       /* start holds desired position, map -1 to the start */
303       if (start == -1)
304         start = 0;
305       /* start must be 0 or the formats must match */
306       g_return_if_fail (start == 0 || segment->format == format);
307       break;
308     case GST_SEEK_TYPE_CUR:
309       g_return_if_fail (start == 0 || segment->format == format);
310       /* add start to currently configured segment */
311       start = segment->start + start;
312       break;
313     case GST_SEEK_TYPE_END:
314       if (segment->duration != -1) {
315         g_return_if_fail (start == 0 || segment->format == format);
316         /* add start to total length */
317         start = segment->duration + start;
318       } else {
319         /* no update if duration unknown */
320         start = segment->start;
321         update_start = FALSE;
322       }
323       break;
324   }
325   /* bring in sane range */
326   if (segment->duration != -1)
327     start = CLAMP (start, 0, segment->duration);
328   else
329     start = MAX (start, 0);
330
331   /* stop can be -1 if we have not configured a stop. */
332   switch (stop_type) {
333     case GST_SEEK_TYPE_NONE:
334       stop = segment->stop;
335       update_stop = FALSE;
336       break;
337     case GST_SEEK_TYPE_SET:
338       /* stop holds required value, if it's not -1, it must be of the same
339        * format as the segment. */
340       g_return_if_fail (stop == -1 || segment->format == format);
341       break;
342     case GST_SEEK_TYPE_CUR:
343       if (segment->stop != -1) {
344         /* only add compatible formats or 0 */
345         g_return_if_fail (stop == 0 || segment->format == format);
346         stop = segment->stop + stop;
347       } else
348         stop = -1;
349       break;
350     case GST_SEEK_TYPE_END:
351       if (segment->duration != -1) {
352         /* only add compatible formats or 0 */
353         g_return_if_fail (stop == 0 || segment->format == format);
354         stop = segment->duration + stop;
355       } else {
356         stop = segment->stop;
357         update_stop = FALSE;
358       }
359       break;
360   }
361
362   /* if we have a valid stop time, make sure it is clipped */
363   if (stop != -1) {
364     if (segment->duration != -1)
365       stop = CLAMP (stop, 0, segment->duration);
366     else
367       stop = MAX (stop, 0);
368   }
369
370   /* we can't have stop before start */
371   if (stop != -1)
372     g_return_if_fail (start <= stop);
373
374   segment->rate = rate;
375   segment->abs_rate = ABS (rate);
376   segment->applied_rate = 1.0;
377   segment->flags = flags;
378   segment->start = start;
379   segment->stop = stop;
380   segment->time = start;
381
382   last_stop = segment->last_stop;
383   if (update_start && rate > 0.0) {
384     last_stop = start;
385   }
386   if (update_stop && rate < 0.0) {
387     if (stop != -1)
388       last_stop = stop;
389     else {
390       if (segment->duration != -1)
391         last_stop = segment->duration;
392       else
393         last_stop = 0;
394     }
395   }
396   /* set update arg to reflect update of last_stop */
397   if (update)
398     *update = last_stop != segment->last_stop;
399
400   /* update new position */
401   segment->last_stop = last_stop;
402 }
403
404 /**
405  * gst_segment_set_newsegment:
406  * @segment: a #GstSegment structure.
407  * @update: flag indicating a new segment is started or updated
408  * @rate: the rate of the segment.
409  * @format: the format of the segment.
410  * @start: the new start value
411  * @stop: the new stop value
412  * @time: the new stream time
413  *
414  * Update the segment structure with the field values of a new segment event and
415  * with a default applied_rate of 1.0.
416  *
417  * Since: 0.10.6
418  */
419 void
420 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
421     GstFormat format, gint64 start, gint64 stop, gint64 time)
422 {
423   gst_segment_set_newsegment_full (segment, update, rate, 1.0, format, start,
424       stop, time);
425 }
426
427 /**
428  * gst_segment_set_newsegment_full:
429  * @segment: a #GstSegment structure.
430  * @update: flag indicating a new segment is started or updated
431  * @rate: the rate of the segment.
432  * @applied_rate: the applied rate of the segment.
433  * @format: the format of the segment.
434  * @start: the new start value
435  * @stop: the new stop value
436  * @time: the new stream time
437  *
438  * Update the segment structure with the field values of a new segment event.
439  */
440 void
441 gst_segment_set_newsegment_full (GstSegment * segment, gboolean update,
442     gdouble rate, gdouble applied_rate, GstFormat format, gint64 start,
443     gint64 stop, gint64 time)
444 {
445   gint64 duration, last_stop;
446
447   g_return_if_fail (rate != 0.0);
448   g_return_if_fail (applied_rate != 0.0);
449   g_return_if_fail (segment != NULL);
450
451   GST_DEBUG ("configuring segment update %d, rate %lf, format %s, "
452       "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT ", position %"
453       G_GINT64_FORMAT, update, rate, gst_format_get_name (format), start,
454       stop, time);
455   GST_DEBUG ("old segment was: %" GST_SEGMENT_FORMAT, segment);
456
457   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
458     segment->format = format;
459
460   /* any other format with 0 also gives time 0, the other values are
461    * invalid in the format though. */
462   if (format != segment->format && start == 0) {
463     format = segment->format;
464     if (stop != 0)
465       stop = -1;
466     if (time != 0)
467       time = -1;
468   }
469
470   g_return_if_fail (segment->format == format);
471
472   if (update) {
473     if (G_LIKELY (segment->rate > 0.0)) {
474       /* an update to the current segment is done, elapsed time is
475        * difference between the old start and new start. */
476       if (start > segment->start)
477         duration = start - segment->start;
478       else
479         duration = 0;
480     } else {
481       /* for negative rates, the elapsed duration is the diff between the stop
482        * positions */
483       if (stop != -1 && stop < segment->stop)
484         duration = segment->stop - stop;
485       else
486         duration = 0;
487     }
488     /* update last_stop to be a valid value in the updated segment */
489     if (start > segment->last_stop)
490       last_stop = start;
491     else if (stop != -1 && stop < segment->last_stop)
492       last_stop = stop;
493     else
494       last_stop = segment->last_stop;
495   } else {
496     /* the new segment has to be aligned with the old segment.
497      * We first update the accumulated time of the previous
498      * segment. the accumulated time is used when syncing to the
499      * clock. */
500     if (segment->stop != -1) {
501       duration = segment->stop - segment->start;
502     } else if (segment->last_stop != -1) {
503       /* else use last seen timestamp as segment stop */
504       duration = segment->last_stop - segment->start;
505     } else {
506       /* else we don't know and throw a warning.. really, this should
507        * be fixed in the element. */
508       g_warning ("closing segment of unknown duration, assuming duration of 0");
509       duration = 0;
510     }
511     /* position the last_stop to the next expected position in the new segment,
512      * which is the start or the stop of the segment */
513     if (rate > 0.0)
514       last_stop = start;
515     else
516       last_stop = stop;
517   }
518   /* use previous rate to calculate duration */
519   if (G_LIKELY (segment->abs_rate != 1.0))
520     duration /= segment->abs_rate;
521
522   /* accumulate duration */
523   segment->accum += duration;
524
525   /* then update the current segment */
526   segment->rate = rate;
527   segment->abs_rate = ABS (rate);
528   segment->applied_rate = applied_rate;
529   segment->start = start;
530   segment->last_stop = last_stop;
531   segment->stop = stop;
532   segment->time = time;
533 }
534
535 /**
536  * gst_segment_to_stream_time:
537  * @segment: a #GstSegment structure.
538  * @format: the format of the segment.
539  * @position: the position in the segment
540  *
541  * Translate @position to stream time using the currently configured 
542  * segment. The @position value must be between @segment start and
543  * stop value. 
544  *
545  * This function is typically used by elements that need to operate on
546  * the stream time of the buffers it receives, such as effect plugins.
547  * In those use cases, @position is typically the buffer timestamp or 
548  * clock time that one wants to convert to the stream time.
549  * The stream time is always between 0 and the total duration of the 
550  * media stream. 
551  *
552  * Returns: the position in stream_time or -1 when an invalid position
553  * was given.
554  */
555 gint64
556 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
557     gint64 position)
558 {
559   gint64 result, start, stop, time;
560   gdouble abs_applied_rate;
561
562   /* format does not matter for -1 */
563   if (G_UNLIKELY (position == -1))
564     return -1;
565
566   g_return_val_if_fail (segment != NULL, -1);
567
568   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
569     segment->format = format;
570
571   /* if we have the position for the same format as the segment, we can compare
572    * the start and stop values, otherwise we assume 0 and -1 */
573   if (G_LIKELY (segment->format == format)) {
574     start = segment->start;
575     stop = segment->stop;
576     time = segment->time;
577   } else {
578     start = 0;
579     stop = -1;
580     time = 0;
581   }
582
583   /* outside of the segment boundary stop */
584   if (G_UNLIKELY (stop != -1 && position > stop))
585     return -1;
586
587   /* before the segment boundary */
588   if (G_UNLIKELY (position < start))
589     return -1;
590
591   /* time must be known */
592   if (G_UNLIKELY (time == -1))
593     return -1;
594
595   /* bring to uncorrected position in segment */
596   result = position - start;
597
598   abs_applied_rate = ABS (segment->applied_rate);
599
600   /* correct for applied rate if needed */
601   if (G_UNLIKELY (abs_applied_rate != 1.0))
602     result *= abs_applied_rate;
603
604   /* add or subtract from segment time based on applied rate */
605   if (G_LIKELY (segment->applied_rate > 0.0)) {
606     /* correct for segment time */
607     result += time;
608   } else {
609     /* correct for segment time, clamp at 0. Streams with a negative
610      * applied_rate have timestamps between start and stop, as usual, but have
611      * the time member starting high and going backwards.  */
612     if (G_LIKELY (time > result))
613       result = time - result;
614     else
615       result = 0;
616   }
617
618   return result;
619 }
620
621 /**
622  * gst_segment_to_running_time:
623  * @segment: a #GstSegment structure.
624  * @format: the format of the segment.
625  * @position: the position in the segment
626  *
627  * Translate @position to the total running time using the currently configured 
628  * and previously accumulated segments. Position is a value between @segment
629  * start and stop time.
630  *
631  * This function is typically used by elements that need to synchronize to the
632  * global clock in a pipeline. The runnning time is a constantly increasing value
633  * starting from 0. When gst_segment_init() is called, this value will reset to
634  * 0.
635  *
636  * This function returns -1 if the position is outside of @segment start and stop.
637  *
638  * Returns: the position as the total running time or -1 when an invalid position
639  * was given.
640  */
641 gint64
642 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
643     gint64 position)
644 {
645   gint64 result;
646   gint64 start, stop, accum;
647
648   if (G_UNLIKELY (position == -1))
649     return -1;
650
651   g_return_val_if_fail (segment != NULL, -1);
652
653   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
654     segment->format = format;
655
656   /* if we have the position for the same format as the segment, we can compare
657    * the start and stop values, otherwise we assume 0 and -1 */
658   if (G_LIKELY (segment->format == format)) {
659     start = segment->start;
660     stop = segment->stop;
661     accum = segment->accum;
662   } else {
663     start = 0;
664     stop = -1;
665     accum = 0;
666   }
667
668   /* before the segment boundary */
669   if (G_UNLIKELY (position < start))
670     return -1;
671
672   if (G_LIKELY (segment->rate > 0.0)) {
673     /* outside of the segment boundary stop */
674     if (G_UNLIKELY (stop != -1 && position > stop))
675       return -1;
676
677     /* bring to uncorrected position in segment */
678     result = position - start;
679   } else {
680     /* cannot continue if no stop position set or outside of
681      * the segment. */
682     if (G_UNLIKELY (stop == -1 || position > stop))
683       return -1;
684
685     /* bring to uncorrected position in segment */
686     result = stop - position;
687   }
688
689   /* scale based on the rate, avoid division by and conversion to 
690    * float when not needed */
691   if (G_UNLIKELY (segment->abs_rate != 1.0))
692     result /= segment->abs_rate;
693
694   /* correct for accumulated segments */
695   result += accum;
696
697   return result;
698 }
699
700 /**
701  * gst_segment_clip:
702  * @segment: a #GstSegment structure.
703  * @format: the format of the segment.
704  * @start: the start position in the segment
705  * @stop: the stop position in the segment
706  * @clip_start: (out) (allow-none): the clipped start position in the segment
707  * @clip_stop: (out) (allow-none): the clipped stop position in the segment
708  *
709  * Clip the given @start and @stop values to the segment boundaries given
710  * in @segment. @start and @stop are compared and clipped to @segment 
711  * start and stop values.
712  *
713  * If the function returns FALSE, @start and @stop are known to fall
714  * outside of @segment and @clip_start and @clip_stop are not updated.
715  *
716  * When the function returns TRUE, @clip_start and @clip_stop will be
717  * updated. If @clip_start or @clip_stop are different from @start or @stop
718  * respectively, the region fell partially in the segment.
719  *
720  * Note that when @stop is -1, @clip_stop will be set to the end of the
721  * segment. Depending on the use case, this may or may not be what you want.
722  *
723  * Returns: TRUE if the given @start and @stop times fall partially or 
724  *     completely in @segment, FALSE if the values are completely outside 
725  *     of the segment.
726  */
727 gboolean
728 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
729     gint64 stop, gint64 * clip_start, gint64 * clip_stop)
730 {
731   g_return_val_if_fail (segment != NULL, FALSE);
732
733   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
734     segment->format = format;
735   else
736     g_return_val_if_fail (segment->format == format, FALSE);
737
738   /* if we have a stop position and a valid start and start is bigger, 
739    * we're outside of the segment */
740   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
741     return FALSE;
742
743   /* if a stop position is given and is before the segment start,
744    * we're outside of the segment. Special case is were start
745    * and stop are equal to the segment start. In that case we
746    * are inside the segment. */
747   if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
748                   && stop == segment->start))))
749     return FALSE;
750
751   if (clip_start) {
752     if (start == -1)
753       *clip_start = -1;
754     else
755       *clip_start = MAX (start, segment->start);
756   }
757
758   if (clip_stop) {
759     if (stop == -1)
760       *clip_stop = segment->stop;
761     else if (segment->stop == -1)
762       *clip_stop = MAX (-1, stop);
763     else
764       *clip_stop = MIN (stop, segment->stop);
765
766     if (segment->duration != -1)
767       *clip_stop = MIN (*clip_stop, segment->duration);
768   }
769
770   return TRUE;
771 }
772
773 /**
774  * gst_segment_to_position:
775  * @segment: a #GstSegment structure.
776  * @format: the format of the segment.
777  * @running_time: the running_time in the segment
778  *
779  * Convert @running_time into a position in the segment so that
780  * gst_segment_to_running_time() with that position returns @running_time.
781  *
782  * Returns: the position in the segment for @running_time. This function returns
783  * -1 when @running_time is -1 or when it is not inside @segment.
784  *
785  * Since: 0.10.24
786  */
787 gint64
788 gst_segment_to_position (GstSegment * segment, GstFormat format,
789     gint64 running_time)
790 {
791   gint64 result;
792   gint64 start, stop, accum;
793
794   g_return_val_if_fail (segment != NULL, -1);
795
796   if (G_UNLIKELY (running_time == -1))
797     return -1;
798
799   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
800     segment->format = format;
801
802   /* if we have the position for the same format as the segment, we can compare
803    * the start and stop values, otherwise we assume 0 and -1 */
804   if (G_LIKELY (segment->format == format)) {
805     start = segment->start;
806     stop = segment->stop;
807     accum = segment->accum;
808   } else {
809     start = 0;
810     stop = -1;
811     accum = 0;
812   }
813
814   /* this running_time was for a previous segment */
815   if (running_time < accum)
816     return -1;
817
818   /* start by subtracting the accumulated time */
819   result = running_time - accum;
820
821   /* move into the segment at the right rate */
822   if (G_UNLIKELY (segment->abs_rate != 1.0))
823     result = ceil (result * segment->abs_rate);
824
825   if (G_LIKELY (segment->rate > 0.0)) {
826     /* bring to corrected position in segment */
827     result += start;
828
829     /* outside of the segment boundary stop */
830     if (G_UNLIKELY (stop != -1 && result > stop))
831       return -1;
832   } else {
833     /* cannot continue if no stop position set or outside of
834      * the segment. */
835     if (G_UNLIKELY (stop == -1 || result + start > stop))
836       return -1;
837
838     /* bring to corrected position in segment */
839     result = stop - result;
840   }
841   return result;
842 }
843
844
845 /**
846  * gst_segment_set_running_time:
847  * @segment: a #GstSegment structure.
848  * @format: the format of the segment.
849  * @running_time: the running_time in the segment
850  *
851  * Adjust the start/stop and accum values of @segment such that the next valid
852  * buffer will be one with @running_time.
853  *
854  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
855  * returned, @running_time is -1 or not in @segment.
856  *
857  * Since: 0.10.24
858  */
859 gboolean
860 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
861     gint64 running_time)
862 {
863   gint64 position;
864   gint64 start, stop, last_stop;
865
866   /* start by bringing the running_time into the segment position */
867   position = gst_segment_to_position (segment, format, running_time);
868
869   /* we must have a valid position now */
870   if (G_UNLIKELY (position == -1))
871     return FALSE;
872
873   start = segment->start;
874   stop = segment->stop;
875   last_stop = segment->last_stop;
876
877   if (G_LIKELY (segment->rate > 0.0)) {
878     /* update the start/last_stop and time values */
879     start = position;
880     if (last_stop < start)
881       last_stop = start;
882   } else {
883     /* reverse, update stop */
884     stop = position;
885     /* if we were past the position, go back */
886     if (last_stop > stop)
887       last_stop = stop;
888   }
889   /* and accumulated time is exactly the running time */
890   segment->time = gst_segment_to_stream_time (segment, format, start);
891   segment->start = start;
892   segment->stop = stop;
893   segment->last_stop = last_stop;
894   segment->accum = running_time;
895
896   return TRUE;
897 }