Rework GstSegment handling
[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 (const 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 void
111 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
112 {
113   memcpy (dest, src, sizeof (GstSegment));
114 }
115
116 GType
117 gst_segment_get_type (void)
118 {
119   static GType gst_segment_type = 0;
120
121   if (G_UNLIKELY (gst_segment_type == 0)) {
122     gst_segment_type = g_boxed_type_register_static ("GstSegment",
123         (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
124   }
125
126   return gst_segment_type;
127 }
128
129 /**
130  * gst_segment_new:
131  *
132  * Allocate a new #GstSegment structure and initialize it using
133  * gst_segment_init().
134  *
135  * Free-function: gst_segment_free
136  *
137  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
138  */
139 GstSegment *
140 gst_segment_new (void)
141 {
142   GstSegment *result;
143
144   result = g_slice_new0 (GstSegment);
145   gst_segment_init (result, GST_FORMAT_UNDEFINED);
146
147   return result;
148 }
149
150 /**
151  * gst_segment_free:
152  * @segment: (in) (transfer full): a #GstSegment
153  *
154  * Free the allocated segment @segment.
155  */
156 void
157 gst_segment_free (GstSegment * segment)
158 {
159   g_slice_free (GstSegment, segment);
160 }
161
162 /**
163  * gst_segment_init:
164  * @segment: a #GstSegment structure.
165  * @format: the format of the segment.
166  *
167  * The start/last_stop positions are set to 0 and the stop/duration
168  * fields are set to -1 (unknown). The default rate of 1.0 and no
169  * flags are set.
170  *
171  * Initialize @segment to its default values.
172  */
173 void
174 gst_segment_init (GstSegment * segment, GstFormat format)
175 {
176   g_return_if_fail (segment != NULL);
177
178   segment->flags = 0;
179   segment->rate = 1.0;
180   segment->applied_rate = 1.0;
181   segment->format = format;
182   segment->base = 0;
183   segment->start = 0;
184   segment->stop = -1;
185   segment->time = 0;
186   segment->position = 0;
187   segment->duration = -1;
188 }
189
190 /**
191  * gst_segment_do_seek:
192  * @segment: a #GstSegment structure.
193  * @rate: the rate of the segment.
194  * @format: the format of the segment.
195  * @flags: the segment flags for the segment
196  * @start_type: the seek method
197  * @start: the seek start value
198  * @stop_type: the seek method
199  * @stop: the seek stop value
200  * @update: boolean holding whether position was updated.
201  *
202  * Update the segment structure with the field values of a seek event (see
203  * gst_event_new_seek()).
204  *
205  * After calling this method, the segment field position and time will
206  * contain the requested new position in the segment. The new requested
207  * position in the segment depends on @rate and @start_type and @stop_type.
208  *
209  * For positive @rate, the new position in the segment is the new @segment
210  * start field when it was updated with a @start_type different from
211  * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
212  * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
213  * unmodified.
214  *
215  * For negative @rate, the new position in the segment is the new @segment
216  * stop field when it was updated with a @stop_type different from
217  * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
218  * duration of the segment will be used to update the stop position.
219  * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
220  * @stop is ignored and @segment position is unmodified.
221  *
222  * The applied rate of the segment will be set to 1.0 by default.
223  * If the caller can apply a rate change, it should update @segment
224  * rate and applied_rate after calling this function.
225  *
226  * @update will be set to TRUE if a seek should be performed to the segment
227  * position field. This field can be FALSE if, for example, only the @rate
228  * has been changed but not the playback position.
229  *
230  * Returns: %TRUE if the seek could be performed.
231  */
232 gboolean
233 gst_segment_do_seek (GstSegment * segment, gdouble rate,
234     GstFormat format, GstSeekFlags flags,
235     GstSeekType start_type, guint64 start,
236     GstSeekType stop_type, guint64 stop, gboolean * update)
237 {
238   gboolean update_stop, update_start;
239   guint64 position, base;
240
241   g_return_val_if_fail (rate != 0.0, FALSE);
242   g_return_val_if_fail (segment != NULL, FALSE);
243   g_return_val_if_fail (segment->format == format, FALSE);
244
245   update_start = update_stop = TRUE;
246
247   base = segment->base;
248   position = segment->position;
249
250   if (flags & GST_SEEK_FLAG_FLUSH) {
251     /* flush resets the running_time */
252     base = 0;
253   } else {
254     base = gst_segment_to_running_time (segment, format, position);
255   }
256
257   /* segment->start is never invalid */
258   switch (start_type) {
259     case GST_SEEK_TYPE_NONE:
260       /* no update to segment, take previous start */
261       start = segment->start;
262       update_start = FALSE;
263       break;
264     case GST_SEEK_TYPE_SET:
265       /* start holds desired position, map -1 to the start */
266       if (start == -1)
267         start = 0;
268       /* start must be 0 or the formats must match */
269       break;
270     case GST_SEEK_TYPE_CUR:
271       /* add start to currently configured segment */
272       start = segment->start + start;
273       break;
274     case GST_SEEK_TYPE_END:
275       if (segment->duration != -1) {
276         /* add start to total length */
277         start = segment->duration + start;
278       } else {
279         /* no update if duration unknown */
280         start = segment->start;
281         update_start = FALSE;
282       }
283       break;
284   }
285   /* bring in sane range */
286   if (segment->duration != -1)
287     start = CLAMP (start, 0, segment->duration);
288   else
289     start = MAX (start, 0);
290
291   /* stop can be -1 if we have not configured a stop. */
292   switch (stop_type) {
293     case GST_SEEK_TYPE_NONE:
294       stop = segment->stop;
295       update_stop = FALSE;
296       break;
297     case GST_SEEK_TYPE_SET:
298       /* stop holds required value, if it's not -1, it must be of the same
299        * format as the segment. */
300       break;
301     case GST_SEEK_TYPE_CUR:
302       if (segment->stop != -1) {
303         /* only add compatible formats or 0 */
304         stop = segment->stop + stop;
305       } else
306         stop = -1;
307       break;
308     case GST_SEEK_TYPE_END:
309       if (segment->duration != -1) {
310         /* only add compatible formats or 0 */
311         stop = segment->duration + stop;
312       } else {
313         stop = segment->stop;
314         update_stop = FALSE;
315       }
316       break;
317   }
318
319   /* if we have a valid stop time, make sure it is clipped */
320   if (stop != -1) {
321     if (segment->duration != -1)
322       stop = CLAMP (stop, 0, segment->duration);
323     else
324       stop = MAX (stop, 0);
325   }
326
327   /* we can't have stop before start */
328   if (stop != -1) {
329     if (start > stop) {
330       g_return_val_if_fail (start <= stop, FALSE);
331       return FALSE;
332     }
333   }
334
335   segment->rate = rate;
336   segment->applied_rate = 1.0;
337   segment->base = base;
338   segment->flags = flags;
339   segment->start = start;
340   segment->stop = stop;
341   segment->time = start;
342
343   if (update_start && rate > 0.0) {
344     position = start;
345   }
346   if (update_stop && rate < 0.0) {
347     if (stop != -1)
348       position = stop;
349     else {
350       if (segment->duration != -1)
351         position = segment->duration;
352       else
353         position = 0;
354     }
355   }
356   /* set update arg to reflect update of position */
357   if (update)
358     *update = position != segment->position;
359
360   /* update new position */
361   segment->position = position;
362
363   return TRUE;
364 }
365
366 /**
367  * gst_segment_to_stream_time:
368  * @segment: a #GstSegment structure.
369  * @format: the format of the segment.
370  * @position: the position in the segment
371  *
372  * Translate @position to stream time using the currently configured
373  * segment. The @position value must be between @segment start and
374  * stop value.
375  *
376  * This function is typically used by elements that need to operate on
377  * the stream time of the buffers it receives, such as effect plugins.
378  * In those use cases, @position is typically the buffer timestamp or
379  * clock time that one wants to convert to the stream time.
380  * The stream time is always between 0 and the total duration of the
381  * media stream.
382  *
383  * Returns: the position in stream_time or -1 when an invalid position
384  * was given.
385  */
386 guint64
387 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
388     guint64 position)
389 {
390   guint64 result, start, stop, time;
391   gdouble abs_applied_rate;
392
393   /* format does not matter for -1 */
394   if (G_UNLIKELY (position == -1))
395     return -1;
396
397   g_return_val_if_fail (segment != NULL, -1);
398   g_return_val_if_fail (segment->format == format, -1);
399
400   /* if we have the position for the same format as the segment, we can compare
401    * the start and stop values, otherwise we assume 0 and -1 */
402   if (G_LIKELY (segment->format == format)) {
403     start = segment->start;
404     stop = segment->stop;
405     time = segment->time;
406   } else {
407     start = 0;
408     stop = -1;
409     time = 0;
410   }
411
412   /* outside of the segment boundary stop */
413   if (G_UNLIKELY (stop != -1 && position > stop))
414     return -1;
415
416   /* before the segment boundary */
417   if (G_UNLIKELY (position < start))
418     return -1;
419
420   /* time must be known */
421   if (G_UNLIKELY (time == -1))
422     return -1;
423
424   /* bring to uncorrected position in segment */
425   result = position - start;
426
427   abs_applied_rate = ABS (segment->applied_rate);
428
429   /* correct for applied rate if needed */
430   if (G_UNLIKELY (abs_applied_rate != 1.0))
431     result *= abs_applied_rate;
432
433   /* add or subtract from segment time based on applied rate */
434   if (G_LIKELY (segment->applied_rate > 0.0)) {
435     /* correct for segment time */
436     result += time;
437   } else {
438     /* correct for segment time, clamp at 0. Streams with a negative
439      * applied_rate have timestamps between start and stop, as usual, but have
440      * the time member starting high and going backwards.  */
441     if (G_LIKELY (time > result))
442       result = time - result;
443     else
444       result = 0;
445   }
446
447   return result;
448 }
449
450 /**
451  * gst_segment_to_running_time:
452  * @segment: a #GstSegment structure.
453  * @format: the format of the segment.
454  * @position: the position in the segment
455  *
456  * Translate @position to the total running time using the currently configured
457  * and previously accumulated segments. Position is a value between @segment
458  * start and stop time.
459  *
460  * This function is typically used by elements that need to synchronize to the
461  * global clock in a pipeline. The runnning time is a constantly increasing value
462  * starting from 0. When gst_segment_init() is called, this value will reset to
463  * 0.
464  *
465  * This function returns -1 if the position is outside of @segment start and stop.
466  *
467  * Returns: the position as the total running time or -1 when an invalid position
468  * was given.
469  */
470 guint64
471 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
472     guint64 position)
473 {
474   guint64 result;
475   guint64 start, stop, base;
476   gdouble abs_rate;
477
478   if (G_UNLIKELY (position == -1))
479     return -1;
480
481   g_return_val_if_fail (segment != NULL, -1);
482   g_return_val_if_fail (segment->format == format, -1);
483
484   /* if we have the position for the same format as the segment, we can compare
485    * the start and stop values, otherwise we assume 0 and -1 */
486   if (G_LIKELY (segment->format == format)) {
487     start = segment->start;
488     stop = segment->stop;
489     base = segment->base;
490   } else {
491     start = 0;
492     stop = -1;
493     base = 0;
494   }
495
496   /* before the segment boundary */
497   if (G_UNLIKELY (position < start))
498     return -1;
499
500   if (G_LIKELY (segment->rate > 0.0)) {
501     /* outside of the segment boundary stop */
502     if (G_UNLIKELY (stop != -1 && position > stop))
503       return -1;
504
505     /* bring to uncorrected position in segment */
506     result = position - start;
507   } else {
508     /* cannot continue if no stop position set or outside of
509      * the segment. */
510     if (G_UNLIKELY (stop == -1 || position > stop))
511       return -1;
512
513     /* bring to uncorrected position in segment */
514     result = stop - position;
515   }
516
517   /* scale based on the rate, avoid division by and conversion to
518    * float when not needed */
519   abs_rate = ABS (segment->rate);
520   if (G_UNLIKELY (abs_rate != 1.0))
521     result /= abs_rate;
522
523   /* correct for base of the segment */
524   result += base;
525
526   return result;
527 }
528
529 /**
530  * gst_segment_clip:
531  * @segment: a #GstSegment structure.
532  * @format: the format of the segment.
533  * @start: the start position in the segment
534  * @stop: the stop position in the segment
535  * @clip_start: (out) (allow-none): the clipped start position in the segment
536  * @clip_stop: (out) (allow-none): the clipped stop position in the segment
537  *
538  * Clip the given @start and @stop values to the segment boundaries given
539  * in @segment. @start and @stop are compared and clipped to @segment
540  * start and stop values.
541  *
542  * If the function returns FALSE, @start and @stop are known to fall
543  * outside of @segment and @clip_start and @clip_stop are not updated.
544  *
545  * When the function returns TRUE, @clip_start and @clip_stop will be
546  * updated. If @clip_start or @clip_stop are different from @start or @stop
547  * respectively, the region fell partially in the segment.
548  *
549  * Note that when @stop is -1, @clip_stop will be set to the end of the
550  * segment. Depending on the use case, this may or may not be what you want.
551  *
552  * Returns: TRUE if the given @start and @stop times fall partially or
553  *     completely in @segment, FALSE if the values are completely outside
554  *     of the segment.
555  */
556 gboolean
557 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
558     guint64 stop, guint64 * clip_start, guint64 * clip_stop)
559 {
560   g_return_val_if_fail (segment != NULL, FALSE);
561   g_return_val_if_fail (segment->format == format, FALSE);
562
563   /* if we have a stop position and a valid start and start is bigger,
564    * we're outside of the segment */
565   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
566     return FALSE;
567
568   /* if a stop position is given and is before the segment start,
569    * we're outside of the segment. Special case is were start
570    * and stop are equal to the segment start. In that case we
571    * are inside the segment. */
572   if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
573                   && stop == segment->start))))
574     return FALSE;
575
576   if (clip_start) {
577     if (start == -1)
578       *clip_start = -1;
579     else
580       *clip_start = MAX (start, segment->start);
581   }
582
583   if (clip_stop) {
584     if (stop == -1)
585       *clip_stop = segment->stop;
586     else if (segment->stop == -1)
587       *clip_stop = stop;
588     else
589       *clip_stop = MIN (stop, segment->stop);
590
591     if (segment->duration != -1 && *clip_stop != -1)
592       *clip_stop = MIN (*clip_stop, segment->duration);
593   }
594
595   return TRUE;
596 }
597
598 /**
599  * gst_segment_to_position:
600  * @segment: a #GstSegment structure.
601  * @format: the format of the segment.
602  * @running_time: the running_time in the segment
603  *
604  * Convert @running_time into a position in the segment so that
605  * gst_segment_to_running_time() with that position returns @running_time.
606  *
607  * Returns: the position in the segment for @running_time. This function returns
608  * -1 when @running_time is -1 or when it is not inside @segment.
609  *
610  * Since: 0.10.24
611  */
612 guint64
613 gst_segment_to_position (const GstSegment * segment, GstFormat format,
614     guint64 running_time)
615 {
616   guint64 result;
617   guint64 start, stop, base;
618   gdouble abs_rate;
619
620   if (G_UNLIKELY (running_time == -1))
621     return -1;
622
623   g_return_val_if_fail (segment != NULL, -1);
624   g_return_val_if_fail (segment->format == format, FALSE);
625
626   /* if we have the position for the same format as the segment, we can compare
627    * the start and stop values, otherwise we assume 0 and -1 */
628   if (G_LIKELY (segment->format == format)) {
629     start = segment->start;
630     stop = segment->stop;
631     base = segment->base;
632   } else {
633     start = 0;
634     stop = -1;
635     base = 0;
636   }
637
638   /* this running_time was for a previous segment */
639   if (running_time < base)
640     return -1;
641
642   /* start by subtracting the base time */
643   result = running_time - base;
644
645   /* move into the segment at the right rate */
646   abs_rate = ABS (segment->rate);
647   if (G_UNLIKELY (abs_rate != 1.0))
648     result = ceil (result * abs_rate);
649
650   if (G_LIKELY (segment->rate > 0.0)) {
651     /* bring to corrected position in segment */
652     result += start;
653
654     /* outside of the segment boundary stop */
655     if (G_UNLIKELY (stop != -1 && result > stop))
656       return -1;
657   } else {
658     /* cannot continue if no stop position set or outside of
659      * the segment. */
660     if (G_UNLIKELY (stop == -1 || result + start > stop))
661       return -1;
662
663     /* bring to corrected position in segment */
664     result = stop - result;
665   }
666   return result;
667 }
668
669
670 /**
671  * gst_segment_set_running_time:
672  * @segment: a #GstSegment structure.
673  * @format: the format of the segment.
674  * @running_time: the running_time in the segment
675  *
676  * Adjust the start/stop and base values of @segment such that the next valid
677  * buffer will be one with @running_time.
678  *
679  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
680  * returned, @running_time is -1 or not in @segment.
681  *
682  * Since: 0.10.24
683  */
684 gboolean
685 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
686     guint64 running_time)
687 {
688   guint64 position;
689   guint64 start, stop;
690
691   /* start by bringing the running_time into the segment position */
692   position = gst_segment_to_position (segment, format, running_time);
693
694   /* we must have a valid position now */
695   if (G_UNLIKELY (position == -1))
696     return FALSE;
697
698   start = segment->start;
699   stop = segment->stop;
700
701   if (G_LIKELY (segment->rate > 0.0)) {
702     /* update the start and time values */
703     start = position;
704   } else {
705     /* reverse, update stop */
706     stop = position;
707   }
708   /* and base time is exactly the running time */
709   segment->time = gst_segment_to_stream_time (segment, format, start);
710   segment->start = start;
711   segment->stop = stop;
712   segment->base = running_time;
713
714   return TRUE;
715 }