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