segment: Add new skip flags for clarifying trick mode playback.
[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  * @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  * 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:
379  * @segment: a #GstSegment structure.
380  * @format: the format of the segment.
381  * @position: the position in the segment
382  *
383  * Translate @position to stream time using the currently configured
384  * segment. The @position value must be between @segment start and
385  * stop value.
386  *
387  * This function is typically used by elements that need to operate on
388  * the stream time of the buffers it receives, such as effect plugins.
389  * In those use cases, @position is typically the buffer timestamp or
390  * clock time that one wants to convert to the stream time.
391  * The stream time is always between 0 and the total duration of the
392  * media stream.
393  *
394  * Returns: the position in stream_time or -1 when an invalid position
395  * was given.
396  */
397 guint64
398 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
399     guint64 position)
400 {
401   guint64 result, start, stop, time;
402   gdouble abs_applied_rate;
403
404   /* format does not matter for -1 */
405   if (G_UNLIKELY (position == -1))
406     return -1;
407
408   g_return_val_if_fail (segment != NULL, -1);
409   g_return_val_if_fail (segment->format == format, -1);
410
411   stop = segment->stop;
412
413   /* outside of the segment boundary stop */
414   if (G_UNLIKELY (stop != -1 && position > stop))
415     return -1;
416
417   start = segment->start;
418
419   /* before the segment boundary */
420   if (G_UNLIKELY (position < start))
421     return -1;
422
423   time = segment->time;
424
425   /* time must be known */
426   if (G_UNLIKELY (time == -1))
427     return -1;
428
429   /* bring to uncorrected position in segment */
430   result = position - start;
431
432   abs_applied_rate = ABS (segment->applied_rate);
433
434   /* correct for applied rate if needed */
435   if (G_UNLIKELY (abs_applied_rate != 1.0))
436     result *= abs_applied_rate;
437
438   /* add or subtract from segment time based on applied rate */
439   if (G_LIKELY (segment->applied_rate > 0.0)) {
440     /* correct for segment time */
441     result += time;
442   } else {
443     /* correct for segment time, clamp at 0. Streams with a negative
444      * applied_rate have timestamps between start and stop, as usual, but have
445      * the time member starting high and going backwards.  */
446     if (G_LIKELY (time > result))
447       result = time - result;
448     else
449       result = 0;
450   }
451
452   return result;
453 }
454
455 /**
456  * gst_segment_to_running_time:
457  * @segment: a #GstSegment structure.
458  * @format: the format of the segment.
459  * @position: the position in the segment
460  *
461  * Translate @position to the total running time using the currently configured
462  * segment. Position is a value between @segment start and stop time.
463  *
464  * This function is typically used by elements that need to synchronize to the
465  * global clock in a pipeline. The running time is a constantly increasing value
466  * starting from 0. When gst_segment_init() is called, this value will reset to
467  * 0.
468  *
469  * This function returns -1 if the position is outside of @segment start and stop.
470  *
471  * Returns: the position as the total running time or -1 when an invalid position
472  * was given.
473  */
474 guint64
475 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
476     guint64 position)
477 {
478   guint64 result;
479   guint64 start, stop;
480   gdouble abs_rate;
481
482   if (G_UNLIKELY (position == -1)) {
483     GST_DEBUG ("invalid position (-1)");
484     return -1;
485   }
486
487   g_return_val_if_fail (segment != NULL, -1);
488   g_return_val_if_fail (segment->format == format, -1);
489
490   start = segment->start;
491
492   if (segment->rate > 0.0)
493     start += segment->offset;
494
495   /* before the segment boundary */
496   if (G_UNLIKELY (position < start)) {
497     GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
498         ")", position, start);
499     return -1;
500   }
501
502   stop = segment->stop;
503
504   if (G_LIKELY (segment->rate > 0.0)) {
505     /* after of the segment boundary */
506     if (G_UNLIKELY (stop != -1 && position > stop)) {
507       GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
508           ")", position, stop);
509       return -1;
510     }
511
512     /* bring to uncorrected position in segment */
513     result = position - start;
514   } else {
515     /* cannot continue if no stop position set or outside of
516      * the segment. */
517     if (G_UNLIKELY (stop == -1)) {
518       GST_DEBUG ("invalid stop (-1)");
519       return -1;
520     }
521
522     stop -= segment->offset;
523     if (G_UNLIKELY (position > stop)) {
524       GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
525           ")", position, stop);
526       return -1;
527     }
528
529     /* bring to uncorrected position in segment */
530     result = stop - position;
531   }
532
533   /* scale based on the rate, avoid division by and conversion to
534    * float when not needed */
535   abs_rate = ABS (segment->rate);
536   if (G_UNLIKELY (abs_rate != 1.0))
537     result /= abs_rate;
538
539   /* correct for base of the segment */
540   result += segment->base;
541
542   return result;
543 }
544
545 /**
546  * gst_segment_clip:
547  * @segment: a #GstSegment structure.
548  * @format: the format of the segment.
549  * @start: the start position in the segment
550  * @stop: the stop position in the segment
551  * @clip_start: (out) (allow-none): the clipped start position in the segment
552  * @clip_stop: (out) (allow-none): the clipped stop position in the segment
553  *
554  * Clip the given @start and @stop values to the segment boundaries given
555  * in @segment. @start and @stop are compared and clipped to @segment
556  * start and stop values.
557  *
558  * If the function returns %FALSE, @start and @stop are known to fall
559  * outside of @segment and @clip_start and @clip_stop are not updated.
560  *
561  * When the function returns %TRUE, @clip_start and @clip_stop will be
562  * updated. If @clip_start or @clip_stop are different from @start or @stop
563  * respectively, the region fell partially in the segment.
564  *
565  * Note that when @stop is -1, @clip_stop will be set to the end of the
566  * segment. Depending on the use case, this may or may not be what you want.
567  *
568  * Returns: %TRUE if the given @start and @stop times fall partially or
569  *     completely in @segment, %FALSE if the values are completely outside
570  *     of the segment.
571  */
572 gboolean
573 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
574     guint64 stop, guint64 * clip_start, guint64 * clip_stop)
575 {
576   g_return_val_if_fail (segment != NULL, FALSE);
577   g_return_val_if_fail (segment->format == format, FALSE);
578
579   /* if we have a stop position and a valid start and start is bigger,
580    * we're outside of the segment */
581   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
582     return FALSE;
583
584   /* if a stop position is given and is before the segment start,
585    * we're outside of the segment. Special case is were start
586    * and stop are equal to the segment start. In that case we
587    * are inside the segment. */
588   if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
589                   && stop == segment->start))))
590     return FALSE;
591
592   if (clip_start) {
593     if (start == -1)
594       *clip_start = -1;
595     else
596       *clip_start = MAX (start, segment->start);
597   }
598
599   if (clip_stop) {
600     if (stop == -1)
601       *clip_stop = segment->stop;
602     else if (segment->stop == -1)
603       *clip_stop = stop;
604     else
605       *clip_stop = MIN (stop, segment->stop);
606   }
607
608   return TRUE;
609 }
610
611 /**
612  * gst_segment_to_position:
613  * @segment: a #GstSegment structure.
614  * @format: the format of the segment.
615  * @running_time: the running_time in the segment
616  *
617  * Convert @running_time into a position in the segment so that
618  * gst_segment_to_running_time() with that position returns @running_time.
619  *
620  * Returns: the position in the segment for @running_time. This function returns
621  * -1 when @running_time is -1 or when it is not inside @segment.
622  */
623 guint64
624 gst_segment_to_position (const GstSegment * segment, GstFormat format,
625     guint64 running_time)
626 {
627   guint64 result;
628   guint64 start, stop, base;
629   gdouble abs_rate;
630
631   if (G_UNLIKELY (running_time == -1))
632     return -1;
633
634   g_return_val_if_fail (segment != NULL, -1);
635   g_return_val_if_fail (segment->format == format, FALSE);
636
637   base = segment->base;
638
639   /* this running_time was for a previous segment */
640   if (running_time < base)
641     return -1;
642
643   /* start by subtracting the base time */
644   result = running_time - base;
645
646   /* move into the segment at the right rate */
647   abs_rate = ABS (segment->rate);
648   if (G_UNLIKELY (abs_rate != 1.0))
649     result = ceil (result * abs_rate);
650
651   start = segment->start;
652   stop = segment->stop;
653
654   if (G_LIKELY (segment->rate > 0.0)) {
655     /* bring to corrected position in segment */
656     result += start + segment->offset;
657
658     /* outside of the segment boundary stop */
659     if (G_UNLIKELY (stop != -1 && result > stop))
660       return -1;
661   } else {
662     /* cannot continue if no stop position set or outside of
663      * the segment. */
664     if (G_UNLIKELY (stop == -1 || result + start > stop))
665       return -1;
666
667     /* bring to corrected position in segment */
668     result = stop - result - segment->offset;
669   }
670   return result;
671 }
672
673 /**
674  * gst_segment_set_running_time:
675  * @segment: a #GstSegment structure.
676  * @format: the format of the segment.
677  * @running_time: the running_time in the segment
678  *
679  * Adjust the start/stop and base values of @segment such that the next valid
680  * buffer will be one with @running_time.
681  *
682  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
683  * returned, @running_time is -1 or not in @segment.
684  */
685 gboolean
686 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
687     guint64 running_time)
688 {
689   guint64 position;
690   guint64 start, stop;
691
692   /* start by bringing the running_time into the segment position */
693   position = gst_segment_to_position (segment, format, running_time);
694
695   /* we must have a valid position now */
696   if (G_UNLIKELY (position == -1))
697     return FALSE;
698
699   start = segment->start;
700   stop = segment->stop;
701
702   if (G_LIKELY (segment->rate > 0.0)) {
703     /* update the start and time values */
704     start = position;
705   } else {
706     /* reverse, update stop */
707     stop = position;
708   }
709   /* and base time is exactly the running time */
710   segment->time = gst_segment_to_stream_time (segment, format, start);
711   segment->start = start;
712   segment->stop = stop;
713   segment->base = running_time;
714
715   return TRUE;
716 }
717
718 /**
719  * gst_segment_offset_running_time:
720  * @segment: a #GstSegment structure.
721  * @format: the format of the segment.
722  * @offset: the offset to apply in the segment
723  *
724  * Adjust the values in @segment so that @offset is applied to all
725  * future running-time calculations.
726  *
727  * Since: 1.2.3
728  *
729  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
730  * returned, @offset is not in @segment.
731  */
732 gboolean
733 gst_segment_offset_running_time (GstSegment * segment, GstFormat format,
734     gint64 offset)
735 {
736   g_return_val_if_fail (segment != NULL, FALSE);
737   g_return_val_if_fail (segment->format == format, FALSE);
738
739   if (offset == 0)
740     return TRUE;
741
742   if (offset > 0) {
743     /* positive offset, we can simply apply to the base time */
744     segment->base += offset;
745   } else {
746     offset = -offset;
747     /* negative offset, first try to subtract from base */
748     if (segment->base > offset) {
749       segment->base -= offset;
750     } else {
751       guint64 position;
752
753       /* subtract all from segment.base, remainder in offset */
754       offset -= segment->base;
755       segment->base = 0;
756       position = gst_segment_to_position (segment, format, offset);
757       if (position == -1)
758         return FALSE;
759
760       segment->offset = position;
761     }
762   }
763   return TRUE;
764 }