gst/gstsegment.c: _set_last_stop() must be with a value != -1
[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
23 #include "gst_private.h"
24
25 #include "gstutils.h"
26 #include "gstsegment.h"
27
28 /**
29  * SECTION:gstsegment
30  * @short_description: Structure describing the configured region of interest
31  *                     in a media file.
32  * @see_also: #GstEvent
33  *
34  * This helper structure holds the relevant values for tracking the region of
35  * interest in a media file, called a segment. 
36  *
37  * The structure can be used for two purposes:
38  * <itemizedlist>
39  *   <listitem><para>performing seeks (handling seek events)</para></listitem>
40  *   <listitem><para>tracking playback regions (handling newsegment events)</para></listitem>
41  * </itemizedlist>
42  *
43  * The segment is usually configured by the application with a seek event which 
44  * is propagated upstream and eventually handled by an element that performs the seek.
45  *
46  * The configured segment is then propagated back downstream with a newsegment event.
47  * This information is then used to clip media to the segment boundaries.
48  *
49  * A segment structure is initialized with gst_segment_init(), which takes a #GstFormat
50  * that will be used as the format of the segment values. The segment will be configured
51  * with a start value of 0 and a stop/duration of -1, which is undefined. The default
52  * rate and applied_rate is 1.0.
53  *
54  * If the segment is used for managing seeks, the segment duration should be set with
55  * gst_segment_set_duration(). The public duration field contains the duration of the
56  * segment. When using the segment for seeking, the start and time members should 
57  * normally be left to their default 0 value. The stop position is left to -1 unless
58  * explicitly configured to a different value after a seek event.
59  *
60  * The current position in the segment should be set with the gst_segment_set_last_stop().
61  * The public last_stop field contains the last set stop position in the segment.
62  *
63  * For elements that perform seeks, the current segment should be updated with the
64  * gst_segment_set_seek() and the values from the seek event. This method will update
65  * all the segment fields. The last_pos field will contain the new playback position.
66  * If the cur_type was different from GST_SEEK_TYPE_NONE, playback continues from
67  * the last_pos position, possibly with updated flags or rate.
68  *
69  * For elements that want to use #GstSegment to track the playback region, use
70  * gst_segment_set_newsegment() to update the segment fields with the information from
71  * the newsegment event. 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 all accumulated segments 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  * Last reviewed on 2006-05-03 (0.10.6)
84  */
85
86 static GstSegment *
87 gst_segment_copy (GstSegment * segment)
88 {
89   GstSegment *result = NULL;
90
91   if (segment) {
92     result = gst_segment_new ();
93     memcpy (result, segment, sizeof (GstSegment));
94   }
95   return result;
96 }
97
98 GType
99 gst_segment_get_type (void)
100 {
101   static GType gst_segment_type = 0;
102
103   if (G_UNLIKELY (gst_segment_type == 0)) {
104     gst_segment_type = g_boxed_type_register_static ("GstSegment",
105         (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
106   }
107
108   return gst_segment_type;
109 }
110
111 /**
112  * gst_segment_new:
113  *
114  * Allocate a new #GstSegment structure and initialize it using 
115  * gst_segment_init().
116  *
117  * Returns: a new #GstSegment, free with gst_segment_free().
118  */
119 GstSegment *
120 gst_segment_new (void)
121 {
122   GstSegment *result;
123
124   result = g_new0 (GstSegment, 1);
125   gst_segment_init (result, GST_FORMAT_UNDEFINED);
126
127   return result;
128 }
129
130 /**
131  * gst_segment_free:
132  * @segment: a #GstSegment
133  *
134  * Free the allocated segment @segment.
135  */
136 void
137 gst_segment_free (GstSegment * segment)
138 {
139   g_free (segment);
140 }
141
142 /**
143  * gst_segment_init:
144  * @segment: a #GstSegment structure.
145  * @format: the format of the segment.
146  *
147  * The start/last_stop positions are set to 0 and the stop/duration
148  * fields are set to -1 (unknown). The default rate of 1.0 and no
149  * flags are set.
150  *
151  * Initialize @segment to its default values.
152  */
153 void
154 gst_segment_init (GstSegment * segment, GstFormat format)
155 {
156   g_return_if_fail (segment != NULL);
157
158   segment->rate = 1.0;
159   segment->abs_rate = 1.0;
160   segment->applied_rate = 1.0;
161   segment->format = format;
162   segment->flags = 0;
163   segment->start = 0;
164   segment->stop = -1;
165   segment->time = 0;
166   segment->accum = 0;
167   segment->last_stop = 0;
168   segment->duration = -1;
169 }
170
171 /**
172  * gst_segment_set_duration:
173  * @segment: a #GstSegment structure.
174  * @format: the format of the segment.
175  * @duration: the duration of the segment info or -1 if unknown.
176  *
177  * Set the duration of the segment to @duration. This function is mainly
178  * used by elements that perform seeking and know the total duration of the
179  * segment. 
180  * 
181  * This field should be set to allow seeking requests relative to the
182  * duration.
183  */
184 void
185 gst_segment_set_duration (GstSegment * segment, GstFormat format,
186     gint64 duration)
187 {
188   g_return_if_fail (segment != NULL);
189
190   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
191     segment->format = format;
192   else
193     g_return_if_fail (segment->format == format);
194
195   segment->duration = duration;
196 }
197
198 /**
199  * gst_segment_set_last_stop:
200  * @segment: a #GstSegment structure.
201  * @format: the format of the segment.
202  * @position: the position 
203  *
204  * Set the last observed stop position in the segment to @position.
205  *
206  * This field should be set to allow seeking requests relative to the
207  * current playing position.
208  */
209 void
210 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
211     gint64 position)
212 {
213   g_return_if_fail (segment != NULL);
214   g_return_if_fail (position != -1);
215
216   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
217     segment->format = format;
218   else
219     g_return_if_fail (segment->format == format);
220
221   segment->last_stop = MAX (segment->start, position);
222 }
223
224 /**
225  * gst_segment_set_seek:
226  * @segment: a #GstSegment structure.
227  * @rate: the rate of the segment.
228  * @format: the format of the segment.
229  * @flags: the seek flags for the segment
230  * @start_type: the seek method
231  * @start: the seek start value
232  * @stop_type: the seek method
233  * @stop: the seek stop value
234  * @update: boolean holding whether start or stop were updated.
235  *
236  * Update the segment structure with the field values of a seek event (see
237  * gst_event_new_seek()).
238  *
239  * After calling this method, the segment field last_stop and time will
240  * contain the requested new position in the segment. The new requested
241  * position in the segment depends on @rate and @start_type and @stop_type. 
242  *
243  * For positive @rate, the new position in the segment is the new @segment
244  * start field when it was updated with a @start_type different from
245  * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
246  * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment last_stop is
247  * unmodified.
248  *
249  * For negative @rate, the new position in the segment is the new @segment
250  * stop field when it was updated with a @stop_type different from
251  * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
252  * duration of the segment will be used to update the stop position.
253  * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
254  * @stop is ignored and @segment last_stop is unmodified.
255  *
256  * The applied rate of the segment will be set to 1.0 by default.
257  * If the caller can apply a rate change, it should update @segment
258  * rate and applied_rate after calling this function.
259  */
260 void
261 gst_segment_set_seek (GstSegment * segment, gdouble rate,
262     GstFormat format, GstSeekFlags flags,
263     GstSeekType start_type, gint64 start,
264     GstSeekType stop_type, gint64 stop, gboolean * update)
265 {
266   gboolean update_stop, update_start;
267
268   g_return_if_fail (rate != 0.0);
269   g_return_if_fail (segment != NULL);
270
271   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
272     segment->format = format;
273   else
274     g_return_if_fail (segment->format == format);
275
276   update_stop = update_start = TRUE;
277
278   /* start is never invalid */
279   switch (start_type) {
280     case GST_SEEK_TYPE_NONE:
281       /* no update to segment */
282       start = segment->start;
283       update_start = FALSE;
284       break;
285     case GST_SEEK_TYPE_SET:
286       /* start holds desired position, map -1 to the start */
287       if (start == -1)
288         start = 0;
289       break;
290     case GST_SEEK_TYPE_CUR:
291       /* add start to currently configure segment */
292       start = segment->start + start;
293       break;
294     case GST_SEEK_TYPE_END:
295       if (segment->duration != -1) {
296         /* add start to total length */
297         start = segment->duration + start;
298       } else {
299         /* no update if duration unknown */
300         start = segment->start;
301         update_start = FALSE;
302       }
303       break;
304   }
305   /* bring in sane range */
306   if (segment->duration != -1)
307     start = CLAMP (start, 0, segment->duration);
308   else
309     start = MAX (start, 0);
310
311   /* stop can be -1 if we have not configured a stop. */
312   switch (stop_type) {
313     case GST_SEEK_TYPE_NONE:
314       stop = segment->stop;
315       update_stop = FALSE;
316       break;
317     case GST_SEEK_TYPE_SET:
318       /* stop holds required value */
319       break;
320     case GST_SEEK_TYPE_CUR:
321       if (segment->stop != -1)
322         stop = segment->stop + stop;
323       else
324         stop = -1;
325       break;
326     case GST_SEEK_TYPE_END:
327       if (segment->duration != -1)
328         stop = segment->duration + stop;
329       else {
330         stop = segment->stop;
331         update_stop = FALSE;
332       }
333       break;
334   }
335
336   /* if we have a valid stop time, make sure it is clipped */
337   if (stop != -1) {
338     if (segment->duration != -1)
339       stop = CLAMP (stop, 0, segment->duration);
340     else
341       stop = MAX (stop, 0);
342   }
343
344   /* we can't have stop before start */
345   if (stop != -1)
346     g_return_if_fail (start <= stop);
347
348   segment->rate = rate;
349   segment->abs_rate = ABS (rate);
350   segment->applied_rate = 1.0;
351   segment->flags = flags;
352   segment->start = start;
353   if (update_start && rate > 0.0) {
354     segment->last_stop = start;
355   }
356   if (update_stop && rate < 0.0) {
357     if (stop != -1)
358       segment->last_stop = stop;
359     else {
360       if (segment->duration != -1)
361         segment->last_stop = segment->duration;
362       else
363         segment->last_stop = 0;
364     }
365   }
366   segment->time = start;
367   segment->stop = stop;
368
369   if (update)
370     *update = update_start || update_stop;
371 }
372
373 /**
374  * gst_segment_set_newsegment:
375  * @segment: a #GstSegment structure.
376  * @update: flag indicating a new segment is started or updated
377  * @rate: the rate of the segment.
378  * @format: the format of the segment.
379  * @start: the new start value
380  * @stop: the new stop value
381  * @time: the new stream time
382  *
383  * Update the segment structure with the field values of a new segment event and
384  * with a default applied_rate of 1.0.
385  *
386  * Since: 0.10.6
387  */
388 void
389 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
390     GstFormat format, gint64 start, gint64 stop, gint64 time)
391 {
392   gst_segment_set_newsegment_full (segment, update, rate, 1.0, format, start,
393       stop, time);
394 }
395
396 /**
397  * gst_segment_set_newsegment_full:
398  * @segment: a #GstSegment structure.
399  * @update: flag indicating a new segment is started or updated
400  * @rate: the rate of the segment.
401  * @applied_rate: the applied rate of the segment.
402  * @format: the format of the segment.
403  * @start: the new start value
404  * @stop: the new stop value
405  * @time: the new stream time
406  *
407  * Update the segment structure with the field values of a new segment event.
408  */
409 void
410 gst_segment_set_newsegment_full (GstSegment * segment, gboolean update,
411     gdouble rate, gdouble applied_rate, GstFormat format, gint64 start,
412     gint64 stop, gint64 time)
413 {
414   gint64 duration;
415
416   g_return_if_fail (rate != 0.0);
417   g_return_if_fail (applied_rate != 0.0);
418   g_return_if_fail (segment != NULL);
419
420   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
421     segment->format = format;
422
423   /* any other format with 0 also gives time 0, the other values are
424    * invalid in the format though. */
425   if (format != segment->format && start == 0) {
426     format = segment->format;
427     if (stop != 0)
428       stop = -1;
429     if (time != 0)
430       time = -1;
431   }
432
433   g_return_if_fail (segment->format == format);
434
435   if (update) {
436     /* an update to the current segment is done, elapsed time is
437      * difference between the old start and new start. */
438     if (start > segment->start)
439       duration = start - segment->start;
440     else
441       duration = 0;
442   } else {
443     /* the new segment has to be aligned with the old segment.
444      * We first update the accumulated time of the previous
445      * segment. the accumulated time is used when syncing to the
446      * clock. 
447      */
448     if (segment->stop != -1) {
449       duration = segment->stop - segment->start;
450     } else if (segment->last_stop != -1) {
451       /* else use last seen timestamp as segment stop */
452       duration = segment->last_stop - segment->start;
453     } else {
454       /* else we don't know and throw a warning.. really, this should
455        * be fixed in the element. */
456       g_warning ("closing segment of unknown duration, assuming duration of 0");
457       duration = 0;
458     }
459   }
460   /* use previous rate to calculate duration */
461   if (segment->abs_rate != 1.0)
462     duration /= segment->abs_rate;
463
464   /* accumulate duration */
465   segment->accum += duration;
466
467   /* then update the current segment */
468   segment->rate = rate;
469   segment->abs_rate = ABS (rate);
470   segment->applied_rate = applied_rate;
471   segment->start = start;
472   segment->last_stop = start;
473   segment->stop = stop;
474   segment->time = time;
475 }
476
477 /**
478  * gst_segment_to_stream_time:
479  * @segment: a #GstSegment structure.
480  * @format: the format of the segment.
481  * @position: the position in the segment
482  *
483  * Translate @position to stream time using the currently configured 
484  * segment. The @position value must be between @segment start and
485  * stop value. 
486  *
487  * This function is typically used by elements that need to operate on
488  * the stream time of the buffers it receives, such as effect plugins.
489  * In those use cases, @position is typically the buffer timestamp or 
490  * clock time that one wants to convert to the stream time.
491  * The stream time is always between 0 and the total duration of the 
492  * media stream. 
493  *
494  * Returns: the position in stream_time or -1 when an invalid position
495  * was given.
496  */
497 gint64
498 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
499     gint64 position)
500 {
501   gint64 result;
502   gdouble abs_applied_rate;
503
504   g_return_val_if_fail (segment != NULL, -1);
505
506   /* format does not matter for -1 */
507   if (G_UNLIKELY (position == -1))
508     return -1;
509
510   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
511     segment->format = format;
512   else
513     g_return_val_if_fail (segment->format == format, -1);
514
515   /* outside of the segment boundary stop */
516   if (G_UNLIKELY (segment->stop != -1 && position >= segment->stop))
517     return -1;
518
519   /* before the segment boundary */
520   if (G_UNLIKELY (position < segment->start))
521     return -1;
522
523   /* time must be known */
524   if (G_UNLIKELY (segment->time == -1))
525     return -1;
526
527   /* bring to uncorrected position in segment */
528   result = position - segment->start;
529
530   abs_applied_rate = ABS (segment->applied_rate);
531
532   /* correct for applied rate if needed */
533   if (abs_applied_rate != 1.0)
534     result *= abs_applied_rate;
535
536   /* add or subtract from segment time based on applied rate */
537   if (segment->applied_rate > 0.0) {
538     /* correct for segment time */
539     result += segment->time;
540   } else {
541     /* correct for segment time, clamp at 0 */
542     if (segment->time > result)
543       result = segment->time - result;
544     else
545       result = 0;
546   }
547
548   return result;
549 }
550
551 /**
552  * gst_segment_to_running_time:
553  * @segment: a #GstSegment structure.
554  * @format: the format of the segment.
555  * @position: the position in the segment
556  *
557  * Translate @position to the total running time using the currently configured 
558  * and previously accumulated segments. Position is a value between @segment
559  * start and stop time.
560  *
561  * This function is typically used by elements that need to synchronize to the
562  * global clock in a pipeline. The runnning time is a constantly increasing value
563  * starting from 0. When gst_segment_init() is called, this value will reset to
564  * 0.
565  *
566  * This function returns -1 if the position is outside of @segment start and stop.
567  *
568  * Returns: the position as the total running time or -1 when an invalid position
569  * was given.
570  */
571 gint64
572 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
573     gint64 position)
574 {
575   gint64 result;
576
577   g_return_val_if_fail (segment != NULL, -1);
578
579   if (G_UNLIKELY (position == -1))
580     return -1;
581
582   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
583     segment->format = format;
584   else if (segment->accum)
585     g_return_val_if_fail (segment->format == format, -1);
586
587   /* before the segment boundary */
588   if (G_UNLIKELY (position < segment->start))
589     return -1;
590
591   if (segment->rate > 0.0) {
592     /* outside of the segment boundary stop */
593     if (G_UNLIKELY (segment->stop != -1 && position >= segment->stop))
594       return -1;
595
596     /* bring to uncorrected position in segment */
597     result = position - segment->start;
598   } else {
599     /* cannot continue if no stop position set or outside of
600      * the segment. */
601     if (G_UNLIKELY (segment->stop == -1 || position >= segment->stop))
602       return -1;
603
604     /* bring to uncorrected position in segment */
605     result = segment->stop - position;
606   }
607
608   /* scale based on the rate, avoid division by and conversion to 
609    * float when not needed */
610   if (segment->abs_rate != 1.0)
611     result /= segment->abs_rate;
612
613   /* correct for accumulated segments */
614   result += segment->accum;
615
616   return result;
617 }
618
619 /**
620  * gst_segment_clip:
621  * @segment: a #GstSegment structure.
622  * @format: the format of the segment.
623  * @start: the start position in the segment
624  * @stop: the stop position in the segment
625  * @clip_start: the clipped start position in the segment
626  * @clip_stop: the clipped stop position in the segment
627  *
628  * Clip the given @start and @stop values to the segment boundaries given
629  * in @segment. @start and @stop are compared and clipped to @segment 
630  * start and stop values.
631  *
632  * If the function returns FALSE, @start and @stop are known to fall
633  * outside of @segment and @clip_start and @clip_stop are not updated.
634  *
635  * When the function returns TRUE, @clip_start and @clip_stop will be
636  * updated. If @clip_start or @clip_stop are different from @start or @stop
637  * respectively, the region fell partially in the segment.
638  *
639  * Returns: TRUE if the given @start and @stop times fall partially or 
640  *     completely in @segment, FALSE if the values are completely outside 
641  *     of the segment.
642  */
643 gboolean
644 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
645     gint64 stop, gint64 * clip_start, gint64 * clip_stop)
646 {
647   g_return_val_if_fail (segment != NULL, FALSE);
648
649   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
650     segment->format = format;
651   else
652     g_return_val_if_fail (segment->format == format, FALSE);
653
654   /* if we have a stop position and a valid start and start is bigger, 
655    * we're outside of the segment */
656   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
657     return FALSE;
658
659   /* if a stop position is given and is before the segment start,
660    * we're outside of the segment */
661   if (G_UNLIKELY (stop != -1 && stop != start && stop <= segment->start))
662     return FALSE;
663
664   if (clip_start) {
665     if (start == -1)
666       *clip_start = -1;
667     else
668       *clip_start = MAX (start, segment->start);
669   }
670
671   if (clip_stop) {
672     if (stop == -1)
673       *clip_stop = segment->stop;
674     else if (segment->stop == -1)
675       *clip_stop = MAX (-1, stop);
676     else
677       *clip_stop = MIN (stop, segment->stop);
678
679     if (segment->duration != -1)
680       *clip_stop = MIN (*clip_stop, segment->duration);
681   }
682
683   return TRUE;
684 }