gst/gstsegment.c: _copy() was always returning NULL...
[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.
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 (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
215   if (segment->format == GST_FORMAT_UNDEFINED)
216     segment->format = format;
217   else
218     g_return_if_fail (segment->format == format);
219
220   segment->last_stop = MAX (segment->start, position);
221 }
222
223 /**
224  * gst_segment_set_seek:
225  * @segment: a #GstSegment structure.
226  * @rate: the rate of the segment.
227  * @format: the format of the segment.
228  * @flags: the seek flags for the segment
229  * @cur_type: the seek method
230  * @cur: the seek start value
231  * @stop_type: the seek method
232  * @stop: the seek stop value
233  * @update: boolean holding whether start or stop were updated.
234  *
235  * Update the segment structure with the field values of a seek event.
236  *
237  * After calling this method, the segment field last_stop will contain
238  * the requested new position in the segment. If the cur_type is different
239  * from GST_SEEK_TYPE_NONE, the current position is not updated and 
240  * streaming should continue from the last position, possibly with
241  * updated rate, flags or stop position.
242  *
243  * The applied rate of the segment will be set to 1.0 by default.
244  * If the caller can apply a rate change, it should update @segment
245  * rate and applied_rate after calling this function.
246  */
247 void
248 gst_segment_set_seek (GstSegment * segment, gdouble rate,
249     GstFormat format, GstSeekFlags flags,
250     GstSeekType cur_type, gint64 cur,
251     GstSeekType stop_type, gint64 stop, gboolean * update)
252 {
253   gboolean update_stop, update_start;
254
255   g_return_if_fail (rate != 0.0);
256   g_return_if_fail (segment != NULL);
257
258   if (segment->format == GST_FORMAT_UNDEFINED)
259     segment->format = format;
260   else
261     g_return_if_fail (segment->format == format);
262
263   update_stop = update_start = TRUE;
264
265   /* start is never invalid */
266   switch (cur_type) {
267     case GST_SEEK_TYPE_NONE:
268       /* no update to segment */
269       cur = segment->start;
270       update_start = FALSE;
271       break;
272     case GST_SEEK_TYPE_SET:
273       /* cur holds desired position */
274       break;
275     case GST_SEEK_TYPE_CUR:
276       /* add cur to currently configure segment */
277       cur = segment->start + cur;
278       break;
279     case GST_SEEK_TYPE_END:
280       if (segment->duration != -1) {
281         /* add cur to total length */
282         cur = segment->duration + cur;
283       } else {
284         /* no update if duration unknown */
285         cur = segment->start;
286         update_start = FALSE;
287       }
288       break;
289   }
290   /* bring in sane range */
291   if (segment->duration != -1)
292     cur = CLAMP (cur, 0, segment->duration);
293   else
294     cur = MAX (cur, 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 folds required value */
304       break;
305     case GST_SEEK_TYPE_CUR:
306       if (segment->stop != -1)
307         stop = segment->stop + stop;
308       else
309         stop = -1;
310       break;
311     case GST_SEEK_TYPE_END:
312       if (segment->duration != -1)
313         stop = segment->duration + stop;
314       else {
315         stop = segment->stop;
316         update_stop = FALSE;
317       }
318       break;
319   }
320
321   /* if we have a valid stop time, make sure it is clipped */
322   if (stop != -1) {
323     if (segment->duration != -1)
324       stop = CLAMP (stop, 0, segment->duration);
325     else
326       stop = MAX (stop, 0);
327   }
328
329   /* we can't have stop before start */
330   if (stop != -1)
331     g_return_if_fail (cur <= stop);
332
333   segment->rate = rate;
334   segment->abs_rate = ABS (rate);
335   segment->applied_rate = 1.0;
336   segment->flags = flags;
337   segment->start = cur;
338   if (update_start) {
339     segment->last_stop = cur;
340   }
341   segment->time = segment->last_stop;
342   segment->stop = stop;
343
344   if (update)
345     *update = update_start || update_stop;
346 }
347
348 /**
349  * gst_segment_set_newsegment:
350  * @segment: a #GstSegment structure.
351  * @update: flag indicating a new segment is started or updated
352  * @rate: the rate of the segment.
353  * @format: the format of the segment.
354  * @start: the new start value
355  * @stop: the new stop value
356  * @time: the new stream time
357  *
358  * Update the segment structure with the field values of a new segment event and
359  * with a default applied_rate of 1.0.
360  *
361  * Since: 0.10.6
362  */
363 void
364 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
365     GstFormat format, gint64 start, gint64 stop, gint64 time)
366 {
367   gst_segment_set_newsegment_full (segment, update, rate, 1.0, format, start,
368       stop, time);
369 }
370
371 /**
372  * gst_segment_set_newsegment_full:
373  * @segment: a #GstSegment structure.
374  * @update: flag indicating a new segment is started or updated
375  * @rate: the rate of the segment.
376  * @applied_rate: the applied rate of the segment.
377  * @format: the format of the segment.
378  * @start: the new start value
379  * @stop: the new stop value
380  * @time: the new stream time
381  *
382  * Update the segment structure with the field values of a new segment event.
383  */
384 void
385 gst_segment_set_newsegment_full (GstSegment * segment, gboolean update,
386     gdouble rate, gdouble applied_rate, GstFormat format, gint64 start,
387     gint64 stop, gint64 time)
388 {
389   gint64 duration;
390
391   g_return_if_fail (rate != 0.0);
392   g_return_if_fail (applied_rate != 0.0);
393   g_return_if_fail (segment != NULL);
394
395   if (segment->format == GST_FORMAT_UNDEFINED)
396     segment->format = format;
397
398   /* any other format with 0 also gives time 0, the other values are
399    * invalid in the format though. */
400   if (format != segment->format && start == 0) {
401     format = segment->format;
402     if (stop != 0)
403       stop = -1;
404     if (time != 0)
405       time = -1;
406   }
407
408   g_return_if_fail (segment->format == format);
409
410   if (update) {
411     /* an update to the current segment is done, elapsed time is
412      * difference between the old start and new start. */
413     duration = start - segment->start;
414   } else {
415     /* the new segment has to be aligned with the old segment.
416      * We first update the accumulated time of the previous
417      * segment. the accumulated time is used when syncing to the
418      * clock. 
419      */
420     if (segment->stop != -1) {
421       duration = segment->stop - segment->start;
422     } else if (segment->last_stop != -1) {
423       /* else use last seen timestamp as segment stop */
424       duration = segment->last_stop - segment->start;
425     } else {
426       /* else we don't know and throw a warning.. really, this should
427        * be fixed in the element. */
428       g_warning ("closing segment of unknown duration, assuming duration of 0");
429       duration = 0;
430     }
431   }
432   /* use previous rate to calculate duration */
433   if (segment->abs_rate != 1.0)
434     duration /= segment->abs_rate;
435
436   /* accumulate duration */
437   segment->accum += duration;
438
439   /* then update the current segment */
440   segment->rate = rate;
441   segment->abs_rate = ABS (rate);
442   segment->applied_rate = applied_rate;
443   segment->start = start;
444   segment->last_stop = start;
445   segment->stop = stop;
446   segment->time = time;
447 }
448
449 /**
450  * gst_segment_to_stream_time:
451  * @segment: a #GstSegment structure.
452  * @format: the format of the segment.
453  * @position: the position in the segment
454  *
455  * Translate @position to stream time using the currently configured 
456  * segment. The @position value must be between @segment start and
457  * stop value. 
458  *
459  * This function is typically used by elements that need to operate on
460  * the stream time of the buffers it receives, such as effect plugins.
461  * In those use cases, @position is typically the buffer timestamp or 
462  * clock time that one wants to convert to the stream time.
463  * The stream time is always between 0 and the total duration of the 
464  * media stream. 
465  *
466  * Returns: the position in stream_time or -1 when an invalid position
467  * was given.
468  */
469 gint64
470 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
471     gint64 position)
472 {
473   gint64 result;
474   gdouble abs_applied_rate;
475
476   g_return_val_if_fail (segment != NULL, -1);
477
478   /* format does not matter for -1 */
479   if (position == -1)
480     return -1;
481
482   if (segment->format == GST_FORMAT_UNDEFINED)
483     segment->format = format;
484   else
485     g_return_val_if_fail (segment->format == format, -1);
486
487   /* outside of the segment boundary stop */
488   if (segment->stop != -1 && position >= segment->stop)
489     return -1;
490
491   /* before the segment boundary */
492   if (position < segment->start)
493     return -1;
494
495   /* time must be known */
496   if (segment->time == -1)
497     return -1;
498
499   /* bring to uncorrected position in segment */
500   result = position - segment->start;
501
502   abs_applied_rate = ABS (segment->applied_rate);
503
504   /* correct for applied rate if needed */
505   if (abs_applied_rate != 1.0)
506     result *= abs_applied_rate;
507
508   /* add or subtract from segment time based on applied rate */
509   if (segment->applied_rate > 0.0) {
510     /* correct for segment time */
511     result += segment->time;
512   } else {
513     /* correct for segment time, clamp at 0 */
514     if (segment->time > result)
515       result = segment->time - result;
516     else
517       result = 0;
518   }
519
520   return result;
521 }
522
523 /**
524  * gst_segment_to_running_time:
525  * @segment: a #GstSegment structure.
526  * @format: the format of the segment.
527  * @position: the position in the segment
528  *
529  * Translate @position to the total running time using the currently configured 
530  * and previously accumulated segments. Position is a value between @segment
531  * start and stop time.
532  *
533  * This function is typically used by elements that need to synchronize to the
534  * global clock in a pipeline. The runnning time is a constantly increasing value
535  * starting from 0. When gst_segment_init() is called, this value will reset to
536  * 0.
537  *
538  * This function returns -1 if the position is outside of @segment start and stop.
539  *
540  * Returns: the position as the total running time or -1 when an invalid position
541  * was given.
542  */
543 gint64
544 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
545     gint64 position)
546 {
547   gint64 result;
548
549   g_return_val_if_fail (segment != NULL, -1);
550
551   if (position == -1)
552     return -1;
553
554   if (segment->format == GST_FORMAT_UNDEFINED)
555     segment->format = format;
556   else if (segment->accum)
557     g_return_val_if_fail (segment->format == format, -1);
558
559   /* before the segment boundary */
560   if (position < segment->start)
561     return -1;
562
563   if (segment->rate > 0.0) {
564     /* outside of the segment boundary stop */
565     if (segment->stop != -1 && position >= segment->stop)
566       return -1;
567
568     /* bring to uncorrected position in segment */
569     result = position - segment->start;
570   } else {
571     /* cannot continue if no stop position set or outside of
572      * the segment. */
573     if (segment->stop == -1 || position >= segment->stop)
574       return -1;
575
576     /* bring to uncorrected position in segment */
577     result = segment->stop - position;
578   }
579
580   /* scale based on the rate, avoid division by and conversion to 
581    * float when not needed */
582   if (segment->abs_rate != 1.0)
583     result /= segment->abs_rate;
584
585   /* correct for accumulated segments */
586   result += segment->accum;
587
588   return result;
589 }
590
591 /**
592  * gst_segment_clip:
593  * @segment: a #GstSegment structure.
594  * @format: the format of the segment.
595  * @start: the start position in the segment
596  * @stop: the stop position in the segment
597  * @clip_start: the clipped start position in the segment
598  * @clip_stop: the clipped stop position in the segment
599  *
600  * Clip the given @start and @stop values to the segment boundaries given
601  * in @segment. @start and @stop are compared and clipped to @segment 
602  * start and stop values.
603  *
604  * If the function returns FALSE, @start and @stop are known to fall
605  * outside of @segment and @clip_start and @clip_stop are not updated.
606  *
607  * When the function returns TRUE, @clip_start and @clip_stop will be
608  * updated. If @clip_start or @clip_stop are different from @start or @stop
609  * respectively, the region fell partially in the segment.
610  *
611  * Returns: TRUE if the given @start and @stop times fall partially or 
612  *     completely in @segment, FALSE if the values are completely outside 
613  *     of the segment.
614  */
615 gboolean
616 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
617     gint64 stop, gint64 * clip_start, gint64 * clip_stop)
618 {
619   g_return_val_if_fail (segment != NULL, FALSE);
620
621   if (segment->format == GST_FORMAT_UNDEFINED)
622     segment->format = format;
623   else
624     g_return_val_if_fail (segment->format == format, FALSE);
625
626   /* if we have a stop position and a valid start and start is bigger, 
627    * we're outside of the segment */
628   if (segment->stop != -1 && start != -1 && start >= segment->stop)
629     return FALSE;
630
631   /* if a stop position is given and is before the segment start,
632    * we're outside of the segment */
633   if (stop != -1 && stop <= segment->start)
634     return FALSE;
635
636   if (clip_start) {
637     if (start == -1)
638       *clip_start = -1;
639     else
640       *clip_start = MAX (start, segment->start);
641   }
642
643   if (clip_stop) {
644     if (stop == -1)
645       *clip_stop = segment->stop;
646     else if (segment->stop == -1)
647       *clip_stop = MAX (-1, stop);
648     else
649       *clip_stop = MIN (stop, segment->stop);
650
651     if (segment->duration != -1)
652       *clip_stop = MIN (*clip_stop, segment->duration);
653   }
654
655   return TRUE;
656 }