Port gtk-doc comments to their equivalent markdown syntax
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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  * @title: GstSegment
32  * @short_description: Structure describing the configured region of interest
33  *                     in a media file.
34  * @see_also: #GstEvent
35  *
36  * This helper structure holds the relevant values for tracking the region of
37  * interest in a media file, called a segment.
38  *
39  * The structure can be used for two purposes:
40  *
41  *   * performing seeks (handling seek events)
42  *   * tracking playback regions (handling newsegment events)
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  * The public duration field contains the duration of the segment. When using
56  * the segment for seeking, the start and time members should normally be left
57  * to their default 0 value. The stop position is left to -1 unless explicitly
58  * configured to a different value after a seek event.
59  *
60  * The current position in the segment should be set by changing the position
61  * member in the structure.
62  *
63  * For elements that perform seeks, the current segment should be updated with the
64  * gst_segment_do_seek() and the values from the seek event. This method will update
65  * all the segment fields. The position field will contain the new playback position.
66  * If the start_type was different from GST_SEEK_TYPE_NONE, playback continues from
67  * the position position, possibly with updated flags or rate.
68  *
69  * For elements that want to use #GstSegment to track the playback region,
70  * update the segment fields with the information from the newsegment event.
71  * The gst_segment_clip() method can be used to check and clip
72  * the media data to the segment boundaries.
73  *
74  * For elements that want to synchronize to the pipeline clock, gst_segment_to_running_time()
75  * can be used to convert a timestamp to a value that can be used to synchronize
76  * to the clock. This function takes into account the base as well as
77  * any rate or applied_rate conversions.
78  *
79  * For elements that need to perform operations on media data in stream_time,
80  * gst_segment_to_stream_time() can be used to convert a timestamp and the segment
81  * info to stream time (which is always between 0 and the duration of the stream).
82  */
83
84 /**
85  * gst_segment_copy:
86  * @segment: (transfer none): a #GstSegment
87  *
88  * Create a copy of given @segment.
89  *
90  * Free-function: gst_segment_free
91  *
92  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
93  */
94 GstSegment *
95 gst_segment_copy (const GstSegment * segment)
96 {
97   GstSegment *result = NULL;
98
99   if (segment) {
100     result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
101   }
102   return result;
103 }
104
105 /**
106  * gst_segment_copy_into:
107  * @src: (transfer none): a #GstSegment
108  * @dest: (transfer none): a #GstSegment
109  *
110  * Copy the contents of @src into @dest.
111  */
112 void
113 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
114 {
115   memcpy (dest, src, sizeof (GstSegment));
116 }
117
118 G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
119     (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
120
121 /**
122  * gst_segment_new:
123  *
124  * Allocate a new #GstSegment structure and initialize it using
125  * gst_segment_init().
126  *
127  * Free-function: gst_segment_free
128  *
129  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
130  */
131 GstSegment *
132 gst_segment_new (void)
133 {
134   GstSegment *result;
135
136   result = g_slice_new0 (GstSegment);
137   gst_segment_init (result, GST_FORMAT_UNDEFINED);
138
139   return result;
140 }
141
142 /**
143  * gst_segment_free:
144  * @segment: (in) (transfer full): a #GstSegment
145  *
146  * Free the allocated segment @segment.
147  */
148 void
149 gst_segment_free (GstSegment * segment)
150 {
151   g_slice_free (GstSegment, segment);
152 }
153
154 /**
155  * gst_segment_init:
156  * @segment: a #GstSegment structure.
157  * @format: the format of the segment.
158  *
159  * The start/position fields are set to 0 and the stop/duration
160  * fields are set to -1 (unknown). The default rate of 1.0 and no
161  * flags are set.
162  *
163  * Initialize @segment to its default values.
164  */
165 void
166 gst_segment_init (GstSegment * segment, GstFormat format)
167 {
168   g_return_if_fail (segment != NULL);
169
170   segment->flags = GST_SEGMENT_FLAG_NONE;
171   segment->rate = 1.0;
172   segment->applied_rate = 1.0;
173   segment->format = format;
174   segment->base = 0;
175   segment->offset = 0;
176   segment->start = 0;
177   segment->stop = -1;
178   segment->time = 0;
179   segment->position = 0;
180   segment->duration = -1;
181 }
182
183 /**
184  * gst_segment_do_seek:
185  * @segment: a #GstSegment structure.
186  * @rate: the rate of the segment.
187  * @format: the format of the segment.
188  * @flags: the segment flags for the segment
189  * @start_type: the seek method
190  * @start: the seek start value
191  * @stop_type: the seek method
192  * @stop: the seek stop value
193  * @update: boolean holding whether position was updated.
194  *
195  * Update the segment structure with the field values of a seek event (see
196  * gst_event_new_seek()).
197  *
198  * After calling this method, the segment field position and time will
199  * contain the requested new position in the segment. The new requested
200  * position in the segment depends on @rate and @start_type and @stop_type.
201  *
202  * For positive @rate, the new position in the segment is the new @segment
203  * start field when it was updated with a @start_type different from
204  * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
205  * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
206  * unmodified.
207  *
208  * For negative @rate, the new position in the segment is the new @segment
209  * stop field when it was updated with a @stop_type different from
210  * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
211  * duration of the segment will be used to update the stop position.
212  * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
213  * @stop is ignored and @segment position is unmodified.
214  *
215  * The applied rate of the segment will be set to 1.0 by default.
216  * If the caller can apply a rate change, it should update @segment
217  * rate and applied_rate after calling this function.
218  *
219  * @update will be set to %TRUE if a seek should be performed to the segment
220  * position field. This field can be %FALSE if, for example, only the @rate
221  * has been changed but not the playback position.
222  *
223  * Returns: %TRUE if the seek could be performed.
224  */
225 gboolean
226 gst_segment_do_seek (GstSegment * segment, gdouble rate,
227     GstFormat format, GstSeekFlags flags,
228     GstSeekType start_type, guint64 start,
229     GstSeekType stop_type, guint64 stop, gboolean * update)
230 {
231   gboolean update_stop, update_start;
232   guint64 position, base;
233
234   g_return_val_if_fail (rate != 0.0, FALSE);
235   g_return_val_if_fail (segment != NULL, FALSE);
236   g_return_val_if_fail (segment->format == format, FALSE);
237
238   update_start = update_stop = TRUE;
239
240   position = segment->position;
241
242   /* segment->start is never invalid */
243   switch (start_type) {
244     case GST_SEEK_TYPE_NONE:
245       /* no update to segment, take previous start */
246       start = segment->start;
247       update_start = FALSE;
248       break;
249     case GST_SEEK_TYPE_SET:
250       /* start holds desired position, map -1 to the start */
251       if (start == -1)
252         start = 0;
253       break;
254     case GST_SEEK_TYPE_END:
255       if (segment->duration != -1) {
256         /* add start to total length */
257         start = segment->duration + start;
258       } else {
259         /* no update if duration unknown */
260         start = segment->start;
261         update_start = FALSE;
262       }
263       break;
264   }
265   /* bring in sane range */
266   if (segment->duration != -1)
267     start = MIN (start, segment->duration);
268   else
269     start = MAX ((gint64) start, 0);
270
271   /* stop can be -1 if we have not configured a stop. */
272   switch (stop_type) {
273     case GST_SEEK_TYPE_NONE:
274       stop = segment->stop;
275       update_stop = FALSE;
276       break;
277     case GST_SEEK_TYPE_SET:
278       /* stop holds required value */
279       break;
280     case GST_SEEK_TYPE_END:
281       if (segment->duration != -1) {
282         stop = segment->duration + stop;
283       } else {
284         stop = segment->stop;
285         update_stop = FALSE;
286       }
287       break;
288   }
289
290   /* if we have a valid stop time, make sure it is clipped */
291   if (stop != -1) {
292     if (segment->duration != -1)
293       stop = CLAMP ((gint64) stop, 0, (gint64) segment->duration);
294     else
295       stop = MAX ((gint64) stop, 0);
296   }
297
298   /* we can't have stop before start */
299   if (stop != -1) {
300     if (start > stop) {
301       GST_WARNING ("segment update failed: start(%" G_GUINT64_FORMAT
302           ") > stop(%" G_GUINT64_FORMAT ")", start, stop);
303       g_return_val_if_fail (start <= stop, FALSE);
304       return FALSE;
305     }
306   }
307
308   if (flags & GST_SEEK_FLAG_FLUSH) {
309     /* flush resets the running_time */
310     base = 0;
311   } else {
312     /* make sure the position is inside the segment start/stop */
313     position = CLAMP (position, segment->start, segment->stop);
314
315     /* remember the elapsed time */
316     base = gst_segment_to_running_time (segment, format, position);
317     GST_DEBUG ("updated segment.base: %" G_GUINT64_FORMAT, base);
318   }
319
320   if (update_start && rate > 0.0) {
321     position = start;
322   }
323   if (update_stop && rate < 0.0) {
324     if (stop != -1)
325       position = stop;
326     else {
327       if (segment->duration != -1)
328         position = segment->duration;
329       else
330         position = 0;
331     }
332   }
333
334   /* set update arg to reflect update of position */
335   if (update)
336     *update = position != segment->position;
337
338   /* update new values */
339   /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
340   segment->flags = GST_SEGMENT_FLAG_NONE;
341   if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
342     segment->flags |= GST_SEGMENT_FLAG_RESET;
343   if ((flags & GST_SEEK_FLAG_TRICKMODE) != 0)
344     segment->flags |= GST_SEGMENT_FLAG_TRICKMODE;
345   if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
346     segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
347   if ((flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS) != 0)
348     segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS;
349   if ((flags & GST_SEEK_FLAG_TRICKMODE_NO_AUDIO) != 0)
350     segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_NO_AUDIO;
351
352   segment->rate = rate;
353   segment->applied_rate = 1.0;
354
355   segment->base = base;
356   if (rate > 0.0)
357     segment->offset = position - start;
358   else {
359     if (stop != -1)
360       segment->offset = stop - position;
361     else if (segment->duration != -1)
362       segment->offset = segment->duration - position;
363     else
364       segment->offset = 0;
365   }
366
367   segment->start = start;
368   segment->stop = stop;
369   segment->time = start;
370   segment->position = position;
371
372   GST_INFO ("segment updated: %" GST_SEGMENT_FORMAT, segment);
373
374   return TRUE;
375 }
376
377 /**
378  * gst_segment_to_stream_time_full:
379  * @segment: a #GstSegment structure.
380  * @format: the format of the segment.
381  * @position: the position in the segment
382  * @stream_time: result stream-time
383  *
384  * Translate @position to the total stream time using the currently configured
385  * segment. Compared to gst_segment_to_stream_time() this function can return
386  * negative stream-time.
387  *
388  * This function is typically used by elements that need to synchronize buffers
389  * against the clock or eachother.
390  *
391  * @position can be any value and the result of this function for values outside
392  * of the segment is extrapolated.
393  *
394  * When 1 is returned, @position resulted in a positive stream-time returned
395  * in @stream_time.
396  *
397  * When this function returns -1, the returned @stream_time should be negated
398  * to get the real negative stream time.
399  *
400  * Returns: a 1 or -1 on success, 0 on failure.
401  *
402  * Since: 1.8
403  */
404 gint
405 gst_segment_to_stream_time_full (const GstSegment * segment, GstFormat format,
406     guint64 position, guint64 * stream_time)
407 {
408   guint64 start, stop, time;
409   gdouble abs_applied_rate;
410   gint res;
411
412   /* format does not matter for -1 */
413   if (G_UNLIKELY (position == -1)) {
414     *stream_time = -1;
415     return 0;
416   }
417
418   g_return_val_if_fail (segment != NULL, 0);
419   g_return_val_if_fail (segment->format == format, 0);
420
421   stop = segment->stop;
422
423   start = segment->start;
424   time = segment->time;
425
426   /* time must be known */
427   if (G_UNLIKELY (time == -1))
428     return 0;
429
430   abs_applied_rate = ABS (segment->applied_rate);
431
432   /* add or subtract from segment time based on applied rate */
433   if (G_LIKELY (segment->applied_rate > 0.0)) {
434     if (G_LIKELY (position > start)) {
435       /* bring to uncorrected position in segment */
436       *stream_time = position - start;
437       /* correct for applied rate if needed */
438       if (G_UNLIKELY (abs_applied_rate != 1.0))
439         *stream_time *= abs_applied_rate;
440       /* correct for segment time */
441       *stream_time += time;
442       res = 1;
443     } else {
444       *stream_time = start - position;
445       if (G_UNLIKELY (abs_applied_rate != 1.0))
446         *stream_time *= abs_applied_rate;
447       if (*stream_time > time) {
448         *stream_time -= time;
449         res = -1;
450       } else {
451         *stream_time = time - *stream_time;
452         res = 1;
453       }
454     }
455   } else {
456     /* correct for segment time. Streams with a negative applied_rate
457      * have timestamps between start and stop, as usual, but have the
458      * time member starting high and going backwards.  */
459     /* cannot continue without a known segment stop */
460     if (G_UNLIKELY (stop == -1))
461       return 0;
462     if (G_UNLIKELY (position > stop)) {
463       *stream_time = position - stop;
464       if (G_UNLIKELY (abs_applied_rate != 1.0))
465         *stream_time *= abs_applied_rate;
466       if (*stream_time > time) {
467         *stream_time -= time;
468         res = -1;
469       } else {
470         *stream_time = time - *stream_time;
471         res = 1;
472       }
473     } else {
474       *stream_time = stop - position;
475       if (G_UNLIKELY (abs_applied_rate != 1.0))
476         *stream_time *= abs_applied_rate;
477       *stream_time += time;
478       res = 1;
479     }
480   }
481
482   return res;
483 }
484
485 /**
486  * gst_segment_to_stream_time:
487  * @segment: a #GstSegment structure.
488  * @format: the format of the segment.
489  * @position: the position in the segment
490  *
491  * Translate @position to stream time using the currently configured
492  * segment. The @position value must be between @segment start and
493  * stop value.
494  *
495  * This function is typically used by elements that need to operate on
496  * the stream time of the buffers it receives, such as effect plugins.
497  * In those use cases, @position is typically the buffer timestamp or
498  * clock time that one wants to convert to the stream time.
499  * The stream time is always between 0 and the total duration of the
500  * media stream.
501  *
502  * Returns: the position in stream_time or -1 when an invalid position
503  * was given.
504  *
505  * Since: 1.8
506  */
507 guint64
508 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
509     guint64 position)
510 {
511   guint64 result;
512
513   g_return_val_if_fail (segment != NULL, -1);
514   g_return_val_if_fail (segment->format == format, -1);
515
516   /* before the segment boundary */
517   if (G_UNLIKELY (position < segment->start)) {
518     GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
519         ")", position, segment->start);
520     return -1;
521   }
522   /* after the segment boundary */
523   if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
524     GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
525         ")", position, segment->stop);
526     return -1;
527   }
528
529   if (gst_segment_to_stream_time_full (segment, format, position, &result) == 1)
530     return result;
531
532   return -1;
533 }
534
535 /**
536  * gst_segment_position_from_stream_time_full:
537  * @segment: a #GstSegment structure.
538  * @format: the format of the segment.
539  * @stream_time: the stream-time
540  * @position: the resulting position in the segment
541  *
542  * Translate @stream_time to the segment position using the currently configured
543  * segment. Compared to gst_segment_position_from_stream_time() this function can
544  * return negative segment position.
545  *
546  * This function is typically used by elements that need to synchronize buffers
547  * against the clock or each other.
548  *
549  * @stream_time can be any value and the result of this function for values outside
550  * of the segment is extrapolated.
551  *
552  * When 1 is returned, @stream_time resulted in a positive position returned
553  * in @position.
554  *
555  * When this function returns -1, the returned @position should be negated
556  * to get the real negative segment position.
557  *
558  * Returns: a 1 or -1 on success, 0 on failure.
559  *
560  * Since: 1.8
561  */
562 gint
563 gst_segment_position_from_stream_time_full (const GstSegment * segment,
564     GstFormat format, guint64 stream_time, guint64 * position)
565 {
566   guint64 start, time;
567   gdouble abs_applied_rate;
568   gint res;
569
570   /* format does not matter for -1 */
571   if (G_UNLIKELY (stream_time == -1)) {
572     *position = -1;
573     return 0;
574   }
575
576   g_return_val_if_fail (segment != NULL, -1);
577   g_return_val_if_fail (segment->format == format, -1);
578
579   start = segment->start;
580   time = segment->time;
581
582   /* time must be known */
583   if (G_UNLIKELY (time == -1))
584     return 0;
585
586   abs_applied_rate = ABS (segment->applied_rate);
587
588   if (G_LIKELY (segment->applied_rate > 0.0)) {
589     if (G_LIKELY (stream_time > time)) {
590       res = 1;
591       *position = stream_time - time;
592     } else {
593       res = -1;
594       *position = time - stream_time;
595     }
596     /* correct for applied rate if needed */
597     if (G_UNLIKELY (abs_applied_rate != 1.0))
598       *position /= abs_applied_rate;
599
600     if (G_UNLIKELY (res == -1)) {
601       if (*position > start) {
602         *position -= start;
603       } else {
604         *position = start - *position;
605         res = 1;
606       }
607     } else {
608       *position += start;
609     }
610   } else {
611     GstClockTime stop = segment->stop;
612     /* cannot continue without a known segment stop */
613     if (G_UNLIKELY (stop == -1))
614       return 0;
615     if (G_UNLIKELY (time > stream_time)) {
616       res = -1;
617       *position = time - stream_time;
618     } else {
619       res = 1;
620       *position = stream_time - time;
621     }
622     if (G_UNLIKELY (abs_applied_rate != 1.0))
623       *position /= abs_applied_rate;
624     if (G_UNLIKELY (stop < *position)) {
625       if (G_LIKELY (res == 1)) {
626         *position -= stop;
627         res = -1;
628       } else {
629         *position += stop;
630         res = 1;
631       }
632     } else {
633       if (G_LIKELY (res == 1)) {
634         *position = stop - *position;
635         res = 1;
636       } else {
637         *position += stop;
638         res = 1;
639       }
640     }
641   }
642
643   return res;
644 }
645
646 /**
647  * gst_segment_position_from_stream_time:
648  * @segment: a #GstSegment structure.
649  * @format: the format of the segment.
650  * @stream_time: the stream_time in the segment
651  *
652  * Convert @stream_time into a position in the segment so that
653  * gst_segment_to_stream_time() with that position returns @stream_time.
654  *
655  * Returns: the position in the segment for @stream_time. This function returns
656  * -1 when @stream_time is -1 or when it is not inside @segment.
657  *
658  * Since: 1.8
659  */
660 guint64
661 gst_segment_position_from_stream_time (const GstSegment * segment,
662     GstFormat format, guint64 stream_time)
663 {
664   guint64 position;
665   gint res;
666
667   g_return_val_if_fail (segment != NULL, -1);
668   g_return_val_if_fail (segment->format == format, -1);
669
670   res =
671       gst_segment_position_from_stream_time_full (segment, format, stream_time,
672       &position);
673
674   /* before the segment boundary */
675   if (G_UNLIKELY (position < segment->start)) {
676     GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
677         ")", position, segment->start);
678     return -1;
679   }
680
681   /* after the segment boundary */
682   if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
683     GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
684         ")", position, segment->stop);
685     return -1;
686   }
687
688   if (res == 1)
689     return position;
690
691   return -1;
692 }
693
694 /**
695  * gst_segment_to_running_time_full:
696  * @segment: a #GstSegment structure.
697  * @format: the format of the segment.
698  * @position: the position in the segment
699  * @running_time: result running-time
700  *
701  * Translate @position to the total running time using the currently configured
702  * segment. Compared to gst_segment_to_running_time() this function can return
703  * negative running-time.
704  *
705  * This function is typically used by elements that need to synchronize buffers
706  * against the clock or eachother.
707  *
708  * @position can be any value and the result of this function for values outside
709  * of the segment is extrapolated.
710  *
711  * When 1 is returned, @position resulted in a positive running-time returned
712  * in @running_time.
713  *
714  * When this function returns -1, the returned @running_time should be negated
715  * to get the real negative running time.
716  *
717  * Returns: a 1 or -1 on success, 0 on failure.
718  *
719  * Since: 1.6
720  */
721 gint
722 gst_segment_to_running_time_full (const GstSegment * segment, GstFormat format,
723     guint64 position, guint64 * running_time)
724 {
725   gint res = 0;
726   guint64 result;
727   guint64 start, stop, offset;
728   gdouble abs_rate;
729
730   if (G_UNLIKELY (position == -1)) {
731     GST_DEBUG ("invalid position (-1)");
732     goto done;
733   }
734
735   g_return_val_if_fail (segment != NULL, 0);
736   g_return_val_if_fail (segment->format == format, 0);
737
738   offset = segment->offset;
739
740   if (G_LIKELY (segment->rate > 0.0)) {
741     start = segment->start + offset;
742
743     /* bring to uncorrected position in segment */
744     if (position < start) {
745       /* negative value */
746       result = start - position;
747       res = -1;
748     } else {
749       result = position - start;
750       res = 1;
751     }
752   } else {
753     stop = segment->stop;
754
755     /* cannot continue if no stop position set or invalid offset */
756     g_return_val_if_fail (stop != -1, 0);
757     g_return_val_if_fail (stop >= offset, 0);
758
759     stop -= offset;
760
761     /* bring to uncorrected position in segment */
762     if (position > stop) {
763       /* negative value */
764       result = position - stop;
765       res = -1;
766     } else {
767       result = stop - position;
768       res = 1;
769     }
770   }
771
772   if (running_time) {
773     /* scale based on the rate, avoid division by and conversion to
774      * float when not needed */
775     abs_rate = ABS (segment->rate);
776     if (G_UNLIKELY (abs_rate != 1.0))
777       result /= abs_rate;
778
779     /* correct for base of the segment */
780     if (res == 1)
781       /* positive, add base */
782       *running_time = result + segment->base;
783     else if (segment->base >= result) {
784       /* negative and base is bigger, subtract from base and we have a
785        * positive value again */
786       *running_time = segment->base - result;
787       res = 1;
788     } else {
789       /* negative and base is smaller, subtract base and remainder is
790        * negative */
791       *running_time = result - segment->base;
792     }
793   }
794   return res;
795
796 done:
797   {
798     if (running_time)
799       *running_time = -1;
800     return 0;
801   }
802 }
803
804 /**
805  * gst_segment_to_running_time:
806  * @segment: a #GstSegment structure.
807  * @format: the format of the segment.
808  * @position: the position in the segment
809  *
810  * Translate @position to the total running time using the currently configured
811  * segment. Position is a value between @segment start and stop time.
812  *
813  * This function is typically used by elements that need to synchronize to the
814  * global clock in a pipeline. The running time is a constantly increasing value
815  * starting from 0. When gst_segment_init() is called, this value will reset to
816  * 0.
817  *
818  * This function returns -1 if the position is outside of @segment start and stop.
819  *
820  * Returns: the position as the total running time or -1 when an invalid position
821  * was given.
822  */
823 guint64
824 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
825     guint64 position)
826 {
827   guint64 result;
828
829   g_return_val_if_fail (segment != NULL, -1);
830   g_return_val_if_fail (segment->format == format, -1);
831
832   /* before the segment boundary */
833   if (G_UNLIKELY (position < segment->start)) {
834     GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
835         ")", position, segment->start);
836     return -1;
837   }
838   /* after the segment boundary */
839   if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
840     GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
841         ")", position, segment->stop);
842     return -1;
843   }
844
845   if (gst_segment_to_running_time_full (segment, format, position,
846           &result) == 1)
847     return result;
848
849   return -1;
850 }
851
852 /**
853  * gst_segment_clip:
854  * @segment: a #GstSegment structure.
855  * @format: the format of the segment.
856  * @start: the start position in the segment
857  * @stop: the stop position in the segment
858  * @clip_start: (out) (allow-none): the clipped start position in the segment
859  * @clip_stop: (out) (allow-none): the clipped stop position in the segment
860  *
861  * Clip the given @start and @stop values to the segment boundaries given
862  * in @segment. @start and @stop are compared and clipped to @segment
863  * start and stop values.
864  *
865  * If the function returns %FALSE, @start and @stop are known to fall
866  * outside of @segment and @clip_start and @clip_stop are not updated.
867  *
868  * When the function returns %TRUE, @clip_start and @clip_stop will be
869  * updated. If @clip_start or @clip_stop are different from @start or @stop
870  * respectively, the region fell partially in the segment.
871  *
872  * Note that when @stop is -1, @clip_stop will be set to the end of the
873  * segment. Depending on the use case, this may or may not be what you want.
874  *
875  * Returns: %TRUE if the given @start and @stop times fall partially or
876  *     completely in @segment, %FALSE if the values are completely outside
877  *     of the segment.
878  */
879 gboolean
880 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
881     guint64 stop, guint64 * clip_start, guint64 * clip_stop)
882 {
883   g_return_val_if_fail (segment != NULL, FALSE);
884   g_return_val_if_fail (segment->format == format, FALSE);
885
886   /* if we have a stop position and a valid start and start is bigger,
887    * we're outside of the segment */
888   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
889     return FALSE;
890
891   /* if a stop position is given and is before the segment start,
892    * we're outside of the segment. Special case is were start
893    * and stop are equal to the segment start. In that case we
894    * are inside the segment. */
895   if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
896                   && stop == segment->start))))
897     return FALSE;
898
899   if (clip_start) {
900     if (start == -1)
901       *clip_start = -1;
902     else
903       *clip_start = MAX (start, segment->start);
904   }
905
906   if (clip_stop) {
907     if (stop == -1)
908       *clip_stop = segment->stop;
909     else if (segment->stop == -1)
910       *clip_stop = stop;
911     else
912       *clip_stop = MIN (stop, segment->stop);
913   }
914
915   return TRUE;
916 }
917
918 /**
919  * gst_segment_position_from_running_time:
920  * @segment: a #GstSegment structure.
921  * @format: the format of the segment.
922  * @running_time: the running_time in the segment
923  *
924  * Convert @running_time into a position in the segment so that
925  * gst_segment_to_running_time() with that position returns @running_time.
926  *
927  * Returns: the position in the segment for @running_time. This function returns
928  * -1 when @running_time is -1 or when it is not inside @segment.
929  *
930  * Since: 1.8
931  */
932 guint64
933 gst_segment_position_from_running_time (const GstSegment * segment,
934     GstFormat format, guint64 running_time)
935 {
936   guint64 position;
937   gint res;
938
939   g_return_val_if_fail (segment != NULL, -1);
940   g_return_val_if_fail (segment->format == format, -1);
941
942   res =
943       gst_segment_position_from_running_time_full (segment, format,
944       running_time, &position);
945
946   if (res != 1)
947     return -1;
948
949   /* before the segment boundary */
950   if (G_UNLIKELY (position < segment->start)) {
951     GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
952         ")", position, segment->start);
953     return -1;
954   }
955
956   /* after the segment boundary */
957   if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
958     GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
959         ")", position, segment->stop);
960     return -1;
961   }
962
963   return position;
964 }
965
966 /**
967  * gst_segment_position_from_running_time_full:
968  * @segment: a #GstSegment structure.
969  * @format: the format of the segment.
970  * @running_time: the running-time
971  * @position: the resulting position in the segment
972  *
973  * Translate @running_time to the segment position using the currently configured
974  * segment. Compared to gst_segment_position_from_running_time() this function can
975  * return negative segment position.
976  *
977  * This function is typically used by elements that need to synchronize buffers
978  * against the clock or each other.
979  *
980  * @running_time can be any value and the result of this function for values
981  * outside of the segment is extrapolated.
982  *
983  * When 1 is returned, @running_time resulted in a positive position returned
984  * in @position.
985  *
986  * When this function returns -1, the returned @position should be negated
987  * to get the real negative segment position.
988  *
989  * Returns: a 1 or -1 on success, 0 on failure.
990  *
991  * Since: 1.8
992  */
993 gint
994 gst_segment_position_from_running_time_full (const GstSegment * segment,
995     GstFormat format, guint64 running_time, guint64 * position)
996 {
997   gint res;
998   guint64 start, stop, base;
999   gdouble abs_rate;
1000
1001   if (G_UNLIKELY (running_time == -1)) {
1002     *position = -1;
1003     return 0;
1004   }
1005
1006   g_return_val_if_fail (segment != NULL, 0);
1007   g_return_val_if_fail (segment->format == format, 0);
1008
1009   base = segment->base;
1010
1011   abs_rate = ABS (segment->rate);
1012
1013   start = segment->start;
1014   stop = segment->stop;
1015
1016   if (G_LIKELY (segment->rate > 0.0)) {
1017     /* start by subtracting the base time */
1018     if (G_LIKELY (running_time >= base)) {
1019       *position = running_time - base;
1020       /* move into the segment at the right rate */
1021       if (G_UNLIKELY (abs_rate != 1.0))
1022         *position = ceil (*position * abs_rate);
1023       /* bring to corrected position in segment */
1024       *position += start + segment->offset;
1025       res = 1;
1026     } else {
1027       *position = base - running_time;
1028       if (G_UNLIKELY (abs_rate != 1.0))
1029         *position = ceil (*position * abs_rate);
1030       if (start + segment->offset > *position) {
1031         *position -= start + segment->offset;
1032         res = -1;
1033       } else {
1034         *position = start + segment->offset - *position;
1035         res = 1;
1036       }
1037     }
1038   } else {
1039     if (G_LIKELY (running_time >= base)) {
1040       *position = running_time - base;
1041       if (G_UNLIKELY (abs_rate != 1.0))
1042         *position = ceil (*position * abs_rate);
1043       if (G_UNLIKELY (stop < *position + segment->offset)) {
1044         *position += segment->offset - stop;
1045         res = -1;
1046       } else {
1047         *position = stop - *position - segment->offset;
1048         res = 1;
1049       }
1050     } else {
1051       *position = base - running_time;
1052       if (G_UNLIKELY (abs_rate != 1.0))
1053         *position = ceil (*position * abs_rate);
1054       if (G_UNLIKELY (stop < segment->offset - *position)) {
1055         *position -= segment->offset - stop;
1056         res = -1;
1057       } else {
1058         *position = stop + *position - segment->offset;
1059         res = 1;
1060       }
1061     }
1062   }
1063   return res;
1064 }
1065
1066 /**
1067  * gst_segment_to_position:
1068  * @segment: a #GstSegment structure.
1069  * @format: the format of the segment.
1070  * @running_time: the running_time in the segment
1071  *
1072  * Convert @running_time into a position in the segment so that
1073  * gst_segment_to_running_time() with that position returns @running_time.
1074  *
1075  * Returns: the position in the segment for @running_time. This function returns
1076  * -1 when @running_time is -1 or when it is not inside @segment.
1077  *
1078  * Deprecated. Use gst_segment_position_from_running_time() instead.
1079  */
1080 #ifndef GST_REMOVE_DEPRECATED
1081 #ifdef GST_DISABLE_DEPRECATED
1082 guint64 gst_segment_to_position (const GstSegment * segment, GstFormat format,
1083     guint64 running_time);
1084 #endif
1085 guint64
1086 gst_segment_to_position (const GstSegment * segment, GstFormat format,
1087     guint64 running_time)
1088 {
1089   return gst_segment_position_from_running_time (segment, format, running_time);
1090 }
1091 #endif
1092
1093 /**
1094  * gst_segment_set_running_time:
1095  * @segment: a #GstSegment structure.
1096  * @format: the format of the segment.
1097  * @running_time: the running_time in the segment
1098  *
1099  * Adjust the start/stop and base values of @segment such that the next valid
1100  * buffer will be one with @running_time.
1101  *
1102  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
1103  * returned, @running_time is -1 or not in @segment.
1104  */
1105 gboolean
1106 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
1107     guint64 running_time)
1108 {
1109   guint64 position;
1110   guint64 start, stop;
1111
1112   /* start by bringing the running_time into the segment position */
1113   position =
1114       gst_segment_position_from_running_time (segment, format, running_time);
1115
1116   /* we must have a valid position now */
1117   if (G_UNLIKELY (position == -1))
1118     return FALSE;
1119
1120   start = segment->start;
1121   stop = segment->stop;
1122
1123   if (G_LIKELY (segment->rate > 0.0)) {
1124     /* update the start and time values */
1125     start = position;
1126   } else {
1127     /* reverse, update stop */
1128     stop = position;
1129   }
1130   /* and base time is exactly the running time */
1131   segment->time = gst_segment_to_stream_time (segment, format, start);
1132   segment->start = start;
1133   segment->stop = stop;
1134   segment->base = running_time;
1135
1136   return TRUE;
1137 }
1138
1139 /**
1140  * gst_segment_offset_running_time:
1141  * @segment: a #GstSegment structure.
1142  * @format: the format of the segment.
1143  * @offset: the offset to apply in the segment
1144  *
1145  * Adjust the values in @segment so that @offset is applied to all
1146  * future running-time calculations.
1147  *
1148  * Since: 1.2.3
1149  *
1150  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
1151  * returned, @offset is not in @segment.
1152  */
1153 gboolean
1154 gst_segment_offset_running_time (GstSegment * segment, GstFormat format,
1155     gint64 offset)
1156 {
1157   g_return_val_if_fail (segment != NULL, FALSE);
1158   g_return_val_if_fail (segment->format == format, FALSE);
1159
1160   if (offset == 0)
1161     return TRUE;
1162
1163   if (offset > 0) {
1164     /* positive offset, we can simply apply to the base time */
1165     segment->base += offset;
1166   } else {
1167     offset = -offset;
1168     /* negative offset, first try to subtract from base */
1169     if (segment->base > offset) {
1170       segment->base -= offset;
1171     } else {
1172       guint64 position;
1173
1174       /* subtract all from segment.base, remainder in offset */
1175       offset -= segment->base;
1176       segment->base = 0;
1177       position =
1178           gst_segment_position_from_running_time (segment, format, offset);
1179       if (position == -1)
1180         return FALSE;
1181
1182       segment->offset = position - segment->start;
1183     }
1184   }
1185   return TRUE;
1186 }
1187
1188 /**
1189  * gst_segment_is_equal:
1190  * @s0: a #GstSegment structure.
1191  * @s1: a #GstSegment structure.
1192  *
1193  * Checks for two segments being equal. Equality here is defined
1194  * as perfect equality, including floating point values.
1195  *
1196  * Since: 1.6
1197  *
1198  * Returns: %TRUE if the segments are equal, %FALSE otherwise.
1199  */
1200 gboolean
1201 gst_segment_is_equal (const GstSegment * s0, const GstSegment * s1)
1202 {
1203   if (s0->flags != s1->flags)
1204     return FALSE;
1205   if (s0->rate != s1->rate)
1206     return FALSE;
1207   if (s0->applied_rate != s1->applied_rate)
1208     return FALSE;
1209   if (s0->format != s1->format)
1210     return FALSE;
1211   if (s0->base != s1->base)
1212     return FALSE;
1213   if (s0->offset != s1->offset)
1214     return FALSE;
1215   if (s0->start != s1->start)
1216     return FALSE;
1217   if (s0->stop != s1->stop)
1218     return FALSE;
1219   if (s0->time != s1->time)
1220     return FALSE;
1221   if (s0->position != s1->position)
1222     return FALSE;
1223   if (s0->duration != s1->duration)
1224     return FALSE;
1225   return TRUE;
1226 }