Merge remote-tracking branch 'origin/master' into 0.11
[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   position = segment->position;
248
249   if (flags & GST_SEEK_FLAG_FLUSH) {
250     /* flush resets the running_time */
251     base = 0;
252   } else {
253     base = gst_segment_to_running_time (segment, format, position);
254   }
255
256   /* segment->start is never invalid */
257   switch (start_type) {
258     case GST_SEEK_TYPE_NONE:
259       /* no update to segment, take previous start */
260       start = segment->start;
261       update_start = FALSE;
262       break;
263     case GST_SEEK_TYPE_SET:
264       /* start holds desired position, map -1 to the start */
265       if (start == -1)
266         start = 0;
267       break;
268     case GST_SEEK_TYPE_CUR:
269     {
270       gint64 sstart = (gint64) start;
271       /* add start to currently configured segment */
272       if (sstart > 0 || segment->start > -sstart)
273         start = segment->start + start;
274       else
275         start = 0;
276       break;
277     }
278     case GST_SEEK_TYPE_END:
279       if (segment->duration != -1) {
280         /* add start to total length */
281         start = segment->duration + start;
282       } else {
283         /* no update if duration unknown */
284         start = segment->start;
285         update_start = FALSE;
286       }
287       break;
288   }
289   /* bring in sane range */
290   if (segment->duration != -1)
291     start = MIN (start, segment->duration);
292   else
293     start = MAX (start, 0);
294
295   /* stop can be -1 if we have not configured a stop. */
296   switch (stop_type) {
297     case GST_SEEK_TYPE_NONE:
298       stop = segment->stop;
299       update_stop = FALSE;
300       break;
301     case GST_SEEK_TYPE_SET:
302       /* stop holds required value */
303       break;
304     case GST_SEEK_TYPE_CUR:
305       if (segment->stop != -1) {
306         gint64 sstop = (gint64) stop;
307         if (sstop > 0 || segment->stop > -sstop)
308           stop = segment->stop + stop;
309         else
310           stop = 0;
311       } else
312         stop = -1;
313       break;
314     case GST_SEEK_TYPE_END:
315       if (segment->duration != -1) {
316         stop = segment->duration + stop;
317       } else {
318         stop = segment->stop;
319         update_stop = FALSE;
320       }
321       break;
322   }
323
324   /* if we have a valid stop time, make sure it is clipped */
325   if (stop != -1) {
326     if (segment->duration != -1)
327       stop = CLAMP (stop, 0, segment->duration);
328     else
329       stop = MAX (stop, 0);
330   }
331
332   /* we can't have stop before start */
333   if (stop != -1) {
334     if (start > stop) {
335       g_return_val_if_fail (start <= stop, FALSE);
336       return FALSE;
337     }
338   }
339
340   segment->rate = rate;
341   segment->applied_rate = 1.0;
342   segment->base = base;
343   segment->flags = (GstSegmentFlags) flags;
344   segment->start = start;
345   segment->stop = stop;
346   segment->time = start;
347
348   if (update_start && rate > 0.0) {
349     position = start;
350   }
351   if (update_stop && rate < 0.0) {
352     if (stop != -1)
353       position = stop;
354     else {
355       if (segment->duration != -1)
356         position = segment->duration;
357       else
358         position = 0;
359     }
360   }
361   /* set update arg to reflect update of position */
362   if (update)
363     *update = position != segment->position;
364
365   /* update new position */
366   segment->position = position;
367
368   return TRUE;
369 }
370
371 /**
372  * gst_segment_to_stream_time:
373  * @segment: a #GstSegment structure.
374  * @format: the format of the segment.
375  * @position: the position in the segment
376  *
377  * Translate @position to stream time using the currently configured
378  * segment. The @position value must be between @segment start and
379  * stop value.
380  *
381  * This function is typically used by elements that need to operate on
382  * the stream time of the buffers it receives, such as effect plugins.
383  * In those use cases, @position is typically the buffer timestamp or
384  * clock time that one wants to convert to the stream time.
385  * The stream time is always between 0 and the total duration of the
386  * media stream.
387  *
388  * Returns: the position in stream_time or -1 when an invalid position
389  * was given.
390  */
391 guint64
392 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
393     guint64 position)
394 {
395   guint64 result, start, stop, time;
396   gdouble abs_applied_rate;
397
398   /* format does not matter for -1 */
399   if (G_UNLIKELY (position == -1))
400     return -1;
401
402   g_return_val_if_fail (segment != NULL, -1);
403   g_return_val_if_fail (segment->format == format, -1);
404
405   /* if we have the position for the same format as the segment, we can compare
406    * the start and stop values, otherwise we assume 0 and -1 */
407   if (G_LIKELY (segment->format == format)) {
408     start = segment->start;
409     stop = segment->stop;
410     time = segment->time;
411   } else {
412     start = 0;
413     stop = -1;
414     time = 0;
415   }
416
417   /* outside of the segment boundary stop */
418   if (G_UNLIKELY (stop != -1 && position > stop))
419     return -1;
420
421   /* before the segment boundary */
422   if (G_UNLIKELY (position < start))
423     return -1;
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  * and previously accumulated segments. Position is a value between @segment
463  * start and stop time.
464  *
465  * This function is typically used by elements that need to synchronize to the
466  * global clock in a pipeline. The runnning time is a constantly increasing value
467  * starting from 0. When gst_segment_init() is called, this value will reset to
468  * 0.
469  *
470  * This function returns -1 if the position is outside of @segment start and stop.
471  *
472  * Returns: the position as the total running time or -1 when an invalid position
473  * was given.
474  */
475 guint64
476 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
477     guint64 position)
478 {
479   guint64 result;
480   guint64 start, stop, base;
481   gdouble abs_rate;
482
483   if (G_UNLIKELY (position == -1))
484     return -1;
485
486   g_return_val_if_fail (segment != NULL, -1);
487   g_return_val_if_fail (segment->format == format, -1);
488
489   /* if we have the position for the same format as the segment, we can compare
490    * the start and stop values, otherwise we assume 0 and -1 */
491   if (G_LIKELY (segment->format == format)) {
492     start = segment->start;
493     stop = segment->stop;
494     base = segment->base;
495   } else {
496     start = 0;
497     stop = -1;
498     base = 0;
499   }
500
501   /* before the segment boundary */
502   if (G_UNLIKELY (position < start))
503     return -1;
504
505   if (G_LIKELY (segment->rate > 0.0)) {
506     /* outside of the segment boundary stop */
507     if (G_UNLIKELY (stop != -1 && position > stop))
508       return -1;
509
510     /* bring to uncorrected position in segment */
511     result = position - start;
512   } else {
513     /* cannot continue if no stop position set or outside of
514      * the segment. */
515     if (G_UNLIKELY (stop == -1 || position > stop))
516       return -1;
517
518     /* bring to uncorrected position in segment */
519     result = stop - position;
520   }
521
522   /* scale based on the rate, avoid division by and conversion to
523    * float when not needed */
524   abs_rate = ABS (segment->rate);
525   if (G_UNLIKELY (abs_rate != 1.0))
526     result /= abs_rate;
527
528   /* correct for base of the segment */
529   result += base;
530
531   return result;
532 }
533
534 /**
535  * gst_segment_clip:
536  * @segment: a #GstSegment structure.
537  * @format: the format of the segment.
538  * @start: the start position in the segment
539  * @stop: the stop position in the segment
540  * @clip_start: (out) (allow-none): the clipped start position in the segment
541  * @clip_stop: (out) (allow-none): the clipped stop position in the segment
542  *
543  * Clip the given @start and @stop values to the segment boundaries given
544  * in @segment. @start and @stop are compared and clipped to @segment
545  * start and stop values.
546  *
547  * If the function returns FALSE, @start and @stop are known to fall
548  * outside of @segment and @clip_start and @clip_stop are not updated.
549  *
550  * When the function returns TRUE, @clip_start and @clip_stop will be
551  * updated. If @clip_start or @clip_stop are different from @start or @stop
552  * respectively, the region fell partially in the segment.
553  *
554  * Note that when @stop is -1, @clip_stop will be set to the end of the
555  * segment. Depending on the use case, this may or may not be what you want.
556  *
557  * Returns: TRUE if the given @start and @stop times fall partially or
558  *     completely in @segment, FALSE if the values are completely outside
559  *     of the segment.
560  */
561 gboolean
562 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
563     guint64 stop, guint64 * clip_start, guint64 * clip_stop)
564 {
565   g_return_val_if_fail (segment != NULL, FALSE);
566   g_return_val_if_fail (segment->format == format, FALSE);
567
568   /* if we have a stop position and a valid start and start is bigger,
569    * we're outside of the segment */
570   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
571     return FALSE;
572
573   /* if a stop position is given and is before the segment start,
574    * we're outside of the segment. Special case is were start
575    * and stop are equal to the segment start. In that case we
576    * are inside the segment. */
577   if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
578                   && stop == segment->start))))
579     return FALSE;
580
581   if (clip_start) {
582     if (start == -1)
583       *clip_start = -1;
584     else
585       *clip_start = MAX (start, segment->start);
586   }
587
588   if (clip_stop) {
589     if (stop == -1)
590       *clip_stop = segment->stop;
591     else if (segment->stop == -1)
592       *clip_stop = stop;
593     else
594       *clip_stop = MIN (stop, segment->stop);
595
596     if (segment->duration != -1 && *clip_stop != -1)
597       *clip_stop = MIN (*clip_stop, segment->duration);
598   }
599
600   return TRUE;
601 }
602
603 /**
604  * gst_segment_to_position:
605  * @segment: a #GstSegment structure.
606  * @format: the format of the segment.
607  * @running_time: the running_time in the segment
608  *
609  * Convert @running_time into a position in the segment so that
610  * gst_segment_to_running_time() with that position returns @running_time.
611  *
612  * Returns: the position in the segment for @running_time. This function returns
613  * -1 when @running_time is -1 or when it is not inside @segment.
614  *
615  * Since: 0.10.24
616  */
617 guint64
618 gst_segment_to_position (const GstSegment * segment, GstFormat format,
619     guint64 running_time)
620 {
621   guint64 result;
622   guint64 start, stop, base;
623   gdouble abs_rate;
624
625   if (G_UNLIKELY (running_time == -1))
626     return -1;
627
628   g_return_val_if_fail (segment != NULL, -1);
629   g_return_val_if_fail (segment->format == format, FALSE);
630
631   /* if we have the position for the same format as the segment, we can compare
632    * the start and stop values, otherwise we assume 0 and -1 */
633   if (G_LIKELY (segment->format == format)) {
634     start = segment->start;
635     stop = segment->stop;
636     base = segment->base;
637   } else {
638     start = 0;
639     stop = -1;
640     base = 0;
641   }
642
643   /* this running_time was for a previous segment */
644   if (running_time < base)
645     return -1;
646
647   /* start by subtracting the base time */
648   result = running_time - base;
649
650   /* move into the segment at the right rate */
651   abs_rate = ABS (segment->rate);
652   if (G_UNLIKELY (abs_rate != 1.0))
653     result = ceil (result * abs_rate);
654
655   if (G_LIKELY (segment->rate > 0.0)) {
656     /* bring to corrected position in segment */
657     result += start;
658
659     /* outside of the segment boundary stop */
660     if (G_UNLIKELY (stop != -1 && result > stop))
661       return -1;
662   } else {
663     /* cannot continue if no stop position set or outside of
664      * the segment. */
665     if (G_UNLIKELY (stop == -1 || result + start > stop))
666       return -1;
667
668     /* bring to corrected position in segment */
669     result = stop - result;
670   }
671   return result;
672 }
673
674
675 /**
676  * gst_segment_set_running_time:
677  * @segment: a #GstSegment structure.
678  * @format: the format of the segment.
679  * @running_time: the running_time in the segment
680  *
681  * Adjust the start/stop and base values of @segment such that the next valid
682  * buffer will be one with @running_time.
683  *
684  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
685  * returned, @running_time is -1 or not in @segment.
686  *
687  * Since: 0.10.24
688  */
689 gboolean
690 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
691     guint64 running_time)
692 {
693   guint64 position;
694   guint64 start, stop;
695
696   /* start by bringing the running_time into the segment position */
697   position = gst_segment_to_position (segment, format, running_time);
698
699   /* we must have a valid position now */
700   if (G_UNLIKELY (position == -1))
701     return FALSE;
702
703   start = segment->start;
704   stop = segment->stop;
705
706   if (G_LIKELY (segment->rate > 0.0)) {
707     /* update the start and time values */
708     start = position;
709   } else {
710     /* reverse, update stop */
711     stop = position;
712   }
713   /* and base time is exactly the running time */
714   segment->time = gst_segment_to_stream_time (segment, format, start);
715   segment->start = start;
716   segment->stop = stop;
717   segment->base = running_time;
718
719   return TRUE;
720 }