7722badb1465d2cf616fe26026618c7c81b062c5
[platform/upstream/gstreamer.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->applied_rate = 1.0;
174   segment->format = format;
175   segment->flags = 0;
176   segment->start = 0;
177   segment->stop = -1;
178   segment->time = 0;
179   segment->accum = 0;
180   segment->last_stop = 0;
181   segment->duration = -1;
182 }
183
184 /**
185  * gst_segment_set_duration:
186  * @segment: a #GstSegment structure.
187  * @format: the format of the segment.
188  * @duration: the duration of the segment info or -1 if unknown.
189  *
190  * Set the duration of the segment to @duration. This function is mainly
191  * used by elements that perform seeking and know the total duration of the
192  * segment. 
193  * 
194  * This field should be set to allow seeking requests relative to the
195  * duration.
196  */
197 void
198 gst_segment_set_duration (GstSegment * segment, GstFormat format,
199     gint64 duration)
200 {
201   g_return_if_fail (segment != NULL);
202
203   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
204     segment->format = format;
205   else
206     g_return_if_fail (segment->format == format);
207
208   segment->duration = duration;
209 }
210
211 /**
212  * gst_segment_set_last_stop:
213  * @segment: a #GstSegment structure.
214  * @format: the format of the segment.
215  * @position: the position 
216  *
217  * Set the last observed stop position in the segment to @position.
218  *
219  * This field should be set to allow seeking requests relative to the
220  * current playing position.
221  */
222 void
223 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
224     gint64 position)
225 {
226   g_return_if_fail (segment != NULL);
227
228   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
229     segment->format = format;
230   else
231     g_return_if_fail (segment->format == format);
232
233   segment->last_stop = MAX (segment->start, position);
234 }
235
236 /**
237  * gst_segment_set_seek:
238  * @segment: a #GstSegment structure.
239  * @rate: the rate of the segment.
240  * @format: the format of the segment.
241  * @flags: the seek flags for the segment
242  * @start_type: the seek method
243  * @start: the seek start value
244  * @stop_type: the seek method
245  * @stop: the seek stop value
246  * @update: boolean holding whether last_stop was updated.
247  *
248  * Update the segment structure with the field values of a seek event (see
249  * gst_event_new_seek()).
250  *
251  * After calling this method, the segment field last_stop and time will
252  * contain the requested new position in the segment. The new requested
253  * position in the segment depends on @rate and @start_type and @stop_type. 
254  *
255  * For positive @rate, the new position in the segment is the new @segment
256  * start field when it was updated with a @start_type different from
257  * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
258  * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment last_stop is
259  * unmodified.
260  *
261  * For negative @rate, the new position in the segment is the new @segment
262  * stop field when it was updated with a @stop_type different from
263  * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
264  * duration of the segment will be used to update the stop position.
265  * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
266  * @stop is ignored and @segment last_stop is unmodified.
267  *
268  * The applied rate of the segment will be set to 1.0 by default.
269  * If the caller can apply a rate change, it should update @segment
270  * rate and applied_rate after calling this function.
271  *
272  * @update will be set to TRUE if a seek should be performed to the segment 
273  * last_stop field. This field can be FALSE if, for example, only the @rate
274  * has been changed but not the playback position.
275  */
276 void
277 gst_segment_set_seek (GstSegment * segment, gdouble rate,
278     GstFormat format, GstSeekFlags flags,
279     GstSeekType start_type, gint64 start,
280     GstSeekType stop_type, gint64 stop, gboolean * update)
281 {
282   gboolean update_stop, update_start;
283   gint64 last_stop;
284
285   g_return_if_fail (rate != 0.0);
286   g_return_if_fail (segment != NULL);
287
288   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
289     segment->format = format;
290
291   update_start = update_stop = TRUE;
292
293   /* segment->start is never invalid */
294   switch (start_type) {
295     case GST_SEEK_TYPE_NONE:
296       /* no update to segment, take previous start */
297       start = segment->start;
298       update_start = FALSE;
299       break;
300     case GST_SEEK_TYPE_SET:
301       /* start holds desired position, map -1 to the start */
302       if (start == -1)
303         start = 0;
304       /* start must be 0 or the formats must match */
305       g_return_if_fail (start == 0 || segment->format == format);
306       break;
307     case GST_SEEK_TYPE_CUR:
308       g_return_if_fail (start == 0 || segment->format == format);
309       /* add start to currently configured segment */
310       start = segment->start + start;
311       break;
312     case GST_SEEK_TYPE_END:
313       if (segment->duration != -1) {
314         g_return_if_fail (start == 0 || segment->format == format);
315         /* add start to total length */
316         start = segment->duration + start;
317       } else {
318         /* no update if duration unknown */
319         start = segment->start;
320         update_start = FALSE;
321       }
322       break;
323   }
324   /* bring in sane range */
325   if (segment->duration != -1)
326     start = CLAMP (start, 0, segment->duration);
327   else
328     start = MAX (start, 0);
329
330   /* stop can be -1 if we have not configured a stop. */
331   switch (stop_type) {
332     case GST_SEEK_TYPE_NONE:
333       stop = segment->stop;
334       update_stop = FALSE;
335       break;
336     case GST_SEEK_TYPE_SET:
337       /* stop holds required value, if it's not -1, it must be of the same
338        * format as the segment. */
339       g_return_if_fail (stop == -1 || segment->format == format);
340       break;
341     case GST_SEEK_TYPE_CUR:
342       if (segment->stop != -1) {
343         /* only add compatible formats or 0 */
344         g_return_if_fail (stop == 0 || segment->format == format);
345         stop = segment->stop + stop;
346       } else
347         stop = -1;
348       break;
349     case GST_SEEK_TYPE_END:
350       if (segment->duration != -1) {
351         /* only add compatible formats or 0 */
352         g_return_if_fail (stop == 0 || segment->format == format);
353         stop = segment->duration + stop;
354       } else {
355         stop = segment->stop;
356         update_stop = FALSE;
357       }
358       break;
359   }
360
361   /* if we have a valid stop time, make sure it is clipped */
362   if (stop != -1) {
363     if (segment->duration != -1)
364       stop = CLAMP (stop, 0, segment->duration);
365     else
366       stop = MAX (stop, 0);
367   }
368
369   /* we can't have stop before start */
370   if (stop != -1)
371     g_return_if_fail (start <= stop);
372
373   segment->rate = rate;
374   segment->applied_rate = 1.0;
375   segment->flags = flags;
376   segment->start = start;
377   segment->stop = stop;
378   segment->time = start;
379
380   last_stop = segment->last_stop;
381   if (update_start && rate > 0.0) {
382     last_stop = start;
383   }
384   if (update_stop && rate < 0.0) {
385     if (stop != -1)
386       last_stop = stop;
387     else {
388       if (segment->duration != -1)
389         last_stop = segment->duration;
390       else
391         last_stop = 0;
392     }
393   }
394   /* set update arg to reflect update of last_stop */
395   if (update)
396     *update = last_stop != segment->last_stop;
397
398   /* update new position */
399   segment->last_stop = last_stop;
400 }
401
402 /**
403  * gst_segment_set_newsegment:
404  * @segment: a #GstSegment structure.
405  * @update: flag indicating a new segment is started or updated
406  * @rate: the rate of the segment.
407  * @applied_rate: the applied rate of the segment.
408  * @format: the format of the segment.
409  * @start: the new start value
410  * @stop: the new stop value
411  * @time: the new stream time
412  *
413  * Update the segment structure with the field values of a new segment event.
414  */
415 void
416 gst_segment_set_newsegment (GstSegment * segment, gboolean update,
417     gdouble rate, gdouble applied_rate, GstFormat format, gint64 start,
418     gint64 stop, gint64 time)
419 {
420   gint64 duration, last_stop;
421   gdouble abs_rate;
422
423   g_return_if_fail (rate != 0.0);
424   g_return_if_fail (applied_rate != 0.0);
425   g_return_if_fail (segment != NULL);
426
427   GST_DEBUG ("configuring segment update %d, rate %lf, format %s, "
428       "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT ", position %"
429       G_GINT64_FORMAT, update, rate, gst_format_get_name (format), start,
430       stop, time);
431   GST_DEBUG ("old segment was: %" GST_SEGMENT_FORMAT, segment);
432
433   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
434     segment->format = format;
435
436   /* any other format with 0 also gives time 0, the other values are
437    * invalid in the format though. */
438   if (format != segment->format && start == 0) {
439     format = segment->format;
440     if (stop != 0)
441       stop = -1;
442     if (time != 0)
443       time = -1;
444   }
445
446   g_return_if_fail (segment->format == format);
447
448   if (update) {
449     if (G_LIKELY (segment->rate > 0.0)) {
450       /* an update to the current segment is done, elapsed time is
451        * difference between the old start and new start. */
452       if (start > segment->start)
453         duration = start - segment->start;
454       else
455         duration = 0;
456     } else {
457       /* for negative rates, the elapsed duration is the diff between the stop
458        * positions */
459       if (stop != -1 && stop < segment->stop)
460         duration = segment->stop - stop;
461       else
462         duration = 0;
463     }
464     /* update last_stop to be a valid value in the updated segment */
465     if (start > segment->last_stop)
466       last_stop = start;
467     else if (stop != -1 && stop < segment->last_stop)
468       last_stop = stop;
469     else
470       last_stop = segment->last_stop;
471   } else {
472     /* the new segment has to be aligned with the old segment.
473      * We first update the accumulated time of the previous
474      * segment. the accumulated time is used when syncing to the
475      * clock. */
476     if (segment->stop != -1) {
477       duration = segment->stop - segment->start;
478     } else if (segment->last_stop != -1) {
479       /* else use last seen timestamp as segment stop */
480       duration = segment->last_stop - segment->start;
481     } else {
482       /* else we don't know and throw a warning.. really, this should
483        * be fixed in the element. */
484       g_warning ("closing segment of unknown duration, assuming duration of 0");
485       duration = 0;
486     }
487     /* position the last_stop to the next expected position in the new segment,
488      * which is the start or the stop of the segment */
489     if (rate > 0.0)
490       last_stop = start;
491     else
492       last_stop = stop;
493   }
494   /* use previous rate to calculate duration */
495   abs_rate = ABS (segment->rate);
496   if (G_LIKELY (abs_rate != 1.0))
497     duration /= abs_rate;
498
499   /* accumulate duration */
500   segment->accum += duration;
501
502   /* then update the current segment */
503   segment->rate = rate;
504   segment->applied_rate = applied_rate;
505   segment->start = start;
506   segment->last_stop = last_stop;
507   segment->stop = stop;
508   segment->time = time;
509 }
510
511 /**
512  * gst_segment_to_stream_time:
513  * @segment: a #GstSegment structure.
514  * @format: the format of the segment.
515  * @position: the position in the segment
516  *
517  * Translate @position to stream time using the currently configured 
518  * segment. The @position value must be between @segment start and
519  * stop value. 
520  *
521  * This function is typically used by elements that need to operate on
522  * the stream time of the buffers it receives, such as effect plugins.
523  * In those use cases, @position is typically the buffer timestamp or 
524  * clock time that one wants to convert to the stream time.
525  * The stream time is always between 0 and the total duration of the 
526  * media stream. 
527  *
528  * Returns: the position in stream_time or -1 when an invalid position
529  * was given.
530  */
531 gint64
532 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
533     gint64 position)
534 {
535   gint64 result, start, stop, time;
536   gdouble abs_applied_rate;
537
538   /* format does not matter for -1 */
539   if (G_UNLIKELY (position == -1))
540     return -1;
541
542   g_return_val_if_fail (segment != NULL, -1);
543
544   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
545     segment->format = format;
546
547   /* if we have the position for the same format as the segment, we can compare
548    * the start and stop values, otherwise we assume 0 and -1 */
549   if (G_LIKELY (segment->format == format)) {
550     start = segment->start;
551     stop = segment->stop;
552     time = segment->time;
553   } else {
554     start = 0;
555     stop = -1;
556     time = 0;
557   }
558
559   /* outside of the segment boundary stop */
560   if (G_UNLIKELY (stop != -1 && position > stop))
561     return -1;
562
563   /* before the segment boundary */
564   if (G_UNLIKELY (position < start))
565     return -1;
566
567   /* time must be known */
568   if (G_UNLIKELY (time == -1))
569     return -1;
570
571   /* bring to uncorrected position in segment */
572   result = position - start;
573
574   abs_applied_rate = ABS (segment->applied_rate);
575
576   /* correct for applied rate if needed */
577   if (G_UNLIKELY (abs_applied_rate != 1.0))
578     result *= abs_applied_rate;
579
580   /* add or subtract from segment time based on applied rate */
581   if (G_LIKELY (segment->applied_rate > 0.0)) {
582     /* correct for segment time */
583     result += time;
584   } else {
585     /* correct for segment time, clamp at 0. Streams with a negative
586      * applied_rate have timestamps between start and stop, as usual, but have
587      * the time member starting high and going backwards.  */
588     if (G_LIKELY (time > result))
589       result = time - result;
590     else
591       result = 0;
592   }
593
594   return result;
595 }
596
597 /**
598  * gst_segment_to_running_time:
599  * @segment: a #GstSegment structure.
600  * @format: the format of the segment.
601  * @position: the position in the segment
602  *
603  * Translate @position to the total running time using the currently configured 
604  * and previously accumulated segments. Position is a value between @segment
605  * start and stop time.
606  *
607  * This function is typically used by elements that need to synchronize to the
608  * global clock in a pipeline. The runnning time is a constantly increasing value
609  * starting from 0. When gst_segment_init() is called, this value will reset to
610  * 0.
611  *
612  * This function returns -1 if the position is outside of @segment start and stop.
613  *
614  * Returns: the position as the total running time or -1 when an invalid position
615  * was given.
616  */
617 gint64
618 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
619     gint64 position)
620 {
621   gint64 result;
622   gint64 start, stop, accum;
623   gdouble abs_rate;
624
625   if (G_UNLIKELY (position == -1))
626     return -1;
627
628   g_return_val_if_fail (segment != NULL, -1);
629
630   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
631     segment->format = format;
632
633   /* if we have the position for the same format as the segment, we can compare
634    * the start and stop values, otherwise we assume 0 and -1 */
635   if (G_LIKELY (segment->format == format)) {
636     start = segment->start;
637     stop = segment->stop;
638     accum = segment->accum;
639   } else {
640     start = 0;
641     stop = -1;
642     accum = 0;
643   }
644
645   /* before the segment boundary */
646   if (G_UNLIKELY (position < start))
647     return -1;
648
649   if (G_LIKELY (segment->rate > 0.0)) {
650     /* outside of the segment boundary stop */
651     if (G_UNLIKELY (stop != -1 && position > stop))
652       return -1;
653
654     /* bring to uncorrected position in segment */
655     result = position - start;
656   } else {
657     /* cannot continue if no stop position set or outside of
658      * the segment. */
659     if (G_UNLIKELY (stop == -1 || position > stop))
660       return -1;
661
662     /* bring to uncorrected position in segment */
663     result = stop - position;
664   }
665
666   /* scale based on the rate, avoid division by and conversion to 
667    * float when not needed */
668   abs_rate = ABS (segment->rate);
669   if (G_UNLIKELY (abs_rate != 1.0))
670     result /= abs_rate;
671
672   /* correct for accumulated segments */
673   result += accum;
674
675   return result;
676 }
677
678 /**
679  * gst_segment_clip:
680  * @segment: a #GstSegment structure.
681  * @format: the format of the segment.
682  * @start: the start position in the segment
683  * @stop: the stop position in the segment
684  * @clip_start: (out) (allow-none): the clipped start position in the segment
685  * @clip_stop: (out) (allow-none): the clipped stop position in the segment
686  *
687  * Clip the given @start and @stop values to the segment boundaries given
688  * in @segment. @start and @stop are compared and clipped to @segment 
689  * start and stop values.
690  *
691  * If the function returns FALSE, @start and @stop are known to fall
692  * outside of @segment and @clip_start and @clip_stop are not updated.
693  *
694  * When the function returns TRUE, @clip_start and @clip_stop will be
695  * updated. If @clip_start or @clip_stop are different from @start or @stop
696  * respectively, the region fell partially in the segment.
697  *
698  * Note that when @stop is -1, @clip_stop will be set to the end of the
699  * segment. Depending on the use case, this may or may not be what you want.
700  *
701  * Returns: TRUE if the given @start and @stop times fall partially or 
702  *     completely in @segment, FALSE if the values are completely outside 
703  *     of the segment.
704  */
705 gboolean
706 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
707     gint64 stop, gint64 * clip_start, gint64 * clip_stop)
708 {
709   g_return_val_if_fail (segment != NULL, FALSE);
710
711   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
712     segment->format = format;
713   else
714     g_return_val_if_fail (segment->format == format, FALSE);
715
716   /* if we have a stop position and a valid start and start is bigger, 
717    * we're outside of the segment */
718   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
719     return FALSE;
720
721   /* if a stop position is given and is before the segment start,
722    * we're outside of the segment. Special case is were start
723    * and stop are equal to the segment start. In that case we
724    * are inside the segment. */
725   if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
726                   && stop == segment->start))))
727     return FALSE;
728
729   if (clip_start) {
730     if (start == -1)
731       *clip_start = -1;
732     else
733       *clip_start = MAX (start, segment->start);
734   }
735
736   if (clip_stop) {
737     if (stop == -1)
738       *clip_stop = segment->stop;
739     else if (segment->stop == -1)
740       *clip_stop = MAX (-1, stop);
741     else
742       *clip_stop = MIN (stop, segment->stop);
743
744     if (segment->duration != -1)
745       *clip_stop = MIN (*clip_stop, segment->duration);
746   }
747
748   return TRUE;
749 }
750
751 /**
752  * gst_segment_to_position:
753  * @segment: a #GstSegment structure.
754  * @format: the format of the segment.
755  * @running_time: the running_time in the segment
756  *
757  * Convert @running_time into a position in the segment so that
758  * gst_segment_to_running_time() with that position returns @running_time.
759  *
760  * Returns: the position in the segment for @running_time. This function returns
761  * -1 when @running_time is -1 or when it is not inside @segment.
762  *
763  * Since: 0.10.24
764  */
765 gint64
766 gst_segment_to_position (GstSegment * segment, GstFormat format,
767     gint64 running_time)
768 {
769   gint64 result;
770   gint64 start, stop, accum;
771   gdouble abs_rate;
772
773   g_return_val_if_fail (segment != NULL, -1);
774
775   if (G_UNLIKELY (running_time == -1))
776     return -1;
777
778   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
779     segment->format = format;
780
781   /* if we have the position for the same format as the segment, we can compare
782    * the start and stop values, otherwise we assume 0 and -1 */
783   if (G_LIKELY (segment->format == format)) {
784     start = segment->start;
785     stop = segment->stop;
786     accum = segment->accum;
787   } else {
788     start = 0;
789     stop = -1;
790     accum = 0;
791   }
792
793   /* this running_time was for a previous segment */
794   if (running_time < accum)
795     return -1;
796
797   /* start by subtracting the accumulated time */
798   result = running_time - accum;
799
800   /* move into the segment at the right rate */
801   abs_rate = ABS (segment->rate);
802   if (G_UNLIKELY (abs_rate != 1.0))
803     result = ceil (result * abs_rate);
804
805   if (G_LIKELY (segment->rate > 0.0)) {
806     /* bring to corrected position in segment */
807     result += start;
808
809     /* outside of the segment boundary stop */
810     if (G_UNLIKELY (stop != -1 && result > stop))
811       return -1;
812   } else {
813     /* cannot continue if no stop position set or outside of
814      * the segment. */
815     if (G_UNLIKELY (stop == -1 || result + start > stop))
816       return -1;
817
818     /* bring to corrected position in segment */
819     result = stop - result;
820   }
821   return result;
822 }
823
824
825 /**
826  * gst_segment_set_running_time:
827  * @segment: a #GstSegment structure.
828  * @format: the format of the segment.
829  * @running_time: the running_time in the segment
830  *
831  * Adjust the start/stop and accum values of @segment such that the next valid
832  * buffer will be one with @running_time.
833  *
834  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
835  * returned, @running_time is -1 or not in @segment.
836  *
837  * Since: 0.10.24
838  */
839 gboolean
840 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
841     gint64 running_time)
842 {
843   gint64 position;
844   gint64 start, stop, last_stop;
845
846   /* start by bringing the running_time into the segment position */
847   position = gst_segment_to_position (segment, format, running_time);
848
849   /* we must have a valid position now */
850   if (G_UNLIKELY (position == -1))
851     return FALSE;
852
853   start = segment->start;
854   stop = segment->stop;
855   last_stop = segment->last_stop;
856
857   if (G_LIKELY (segment->rate > 0.0)) {
858     /* update the start/last_stop and time values */
859     start = position;
860     if (last_stop < start)
861       last_stop = start;
862   } else {
863     /* reverse, update stop */
864     stop = position;
865     /* if we were past the position, go back */
866     if (last_stop > stop)
867       last_stop = stop;
868   }
869   /* and accumulated time is exactly the running time */
870   segment->time = gst_segment_to_stream_time (segment, format, start);
871   segment->start = start;
872   segment->stop = stop;
873   segment->last_stop = last_stop;
874   segment->accum = running_time;
875
876   return TRUE;
877 }