Blame gst/gstsegment.c

Packit a6ee4b
/* GStreamer
Packit a6ee4b
 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
Packit a6ee4b
 *
Packit a6ee4b
 * gstsegment.c: GstSegment subsystem
Packit a6ee4b
 *
Packit a6ee4b
 * This library is free software; you can redistribute it and/or
Packit a6ee4b
 * modify it under the terms of the GNU Library General Public
Packit a6ee4b
 * License as published by the Free Software Foundation; either
Packit a6ee4b
 * version 2 of the License, or (at your option) any later version.
Packit a6ee4b
 *
Packit a6ee4b
 * This library is distributed in the hope that it will be useful,
Packit a6ee4b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a6ee4b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a6ee4b
 * Library General Public License for more details.
Packit a6ee4b
 *
Packit a6ee4b
 * You should have received a copy of the GNU Library General Public
Packit a6ee4b
 * License along with this library; if not, write to the
Packit a6ee4b
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit a6ee4b
 * Boston, MA 02110-1301, USA.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
#include "gst_private.h"
Packit a6ee4b
Packit a6ee4b
#include <math.h>
Packit a6ee4b
Packit a6ee4b
#include "gstutils.h"
Packit a6ee4b
#include "gstsegment.h"
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * SECTION:gstsegment
Packit a6ee4b
 * @title: GstSegment
Packit a6ee4b
 * @short_description: Structure describing the configured region of interest
Packit a6ee4b
 *                     in a media file.
Packit a6ee4b
 * @see_also: #GstEvent
Packit a6ee4b
 *
Packit a6ee4b
 * This helper structure holds the relevant values for tracking the region of
Packit a6ee4b
 * interest in a media file, called a segment.
Packit a6ee4b
 *
Packit a6ee4b
 * The structure can be used for two purposes:
Packit a6ee4b
 *
Packit a6ee4b
 *   * performing seeks (handling seek events)
Packit a6ee4b
 *   * tracking playback regions (handling newsegment events)
Packit a6ee4b
 *
Packit a6ee4b
 * The segment is usually configured by the application with a seek event which
Packit a6ee4b
 * is propagated upstream and eventually handled by an element that performs the seek.
Packit a6ee4b
 *
Packit a6ee4b
 * The configured segment is then propagated back downstream with a newsegment event.
Packit a6ee4b
 * This information is then used to clip media to the segment boundaries.
Packit a6ee4b
 *
Packit a6ee4b
 * A segment structure is initialized with gst_segment_init(), which takes a #GstFormat
Packit a6ee4b
 * that will be used as the format of the segment values. The segment will be configured
Packit a6ee4b
 * with a start value of 0 and a stop/duration of -1, which is undefined. The default
Packit a6ee4b
 * rate and applied_rate is 1.0.
Packit a6ee4b
 *
Packit a6ee4b
 * The public duration field contains the duration of the segment. When using
Packit a6ee4b
 * the segment for seeking, the start and time members should normally be left
Packit a6ee4b
 * to their default 0 value. The stop position is left to -1 unless explicitly
Packit a6ee4b
 * configured to a different value after a seek event.
Packit a6ee4b
 *
Packit a6ee4b
 * The current position in the segment should be set by changing the position
Packit a6ee4b
 * member in the structure.
Packit a6ee4b
 *
Packit a6ee4b
 * For elements that perform seeks, the current segment should be updated with the
Packit a6ee4b
 * gst_segment_do_seek() and the values from the seek event. This method will update
Packit a6ee4b
 * all the segment fields. The position field will contain the new playback position.
Packit a6ee4b
 * If the start_type was different from GST_SEEK_TYPE_NONE, playback continues from
Packit a6ee4b
 * the position position, possibly with updated flags or rate.
Packit a6ee4b
 *
Packit a6ee4b
 * For elements that want to use #GstSegment to track the playback region,
Packit a6ee4b
 * update the segment fields with the information from the newsegment event.
Packit a6ee4b
 * The gst_segment_clip() method can be used to check and clip
Packit a6ee4b
 * the media data to the segment boundaries.
Packit a6ee4b
 *
Packit a6ee4b
 * For elements that want to synchronize to the pipeline clock, gst_segment_to_running_time()
Packit a6ee4b
 * can be used to convert a timestamp to a value that can be used to synchronize
Packit a6ee4b
 * to the clock. This function takes into account the base as well as
Packit a6ee4b
 * any rate or applied_rate conversions.
Packit a6ee4b
 *
Packit a6ee4b
 * For elements that need to perform operations on media data in stream_time,
Packit a6ee4b
 * gst_segment_to_stream_time() can be used to convert a timestamp and the segment
Packit a6ee4b
 * info to stream time (which is always between 0 and the duration of the stream).
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/* FIXME 2.0: remove unused format parameter.
Packit a6ee4b
 * Most of the methods in gstsegment.c take and extra GstFormat format, just to
Packit a6ee4b
 * verify segment->format == format.
Packit a6ee4b
 * See https://bugzilla.gnome.org/show_bug.cgi?id=788979
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_copy:
Packit a6ee4b
 * @segment: (transfer none): a #GstSegment
Packit a6ee4b
 *
Packit a6ee4b
 * Create a copy of given @segment.
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_segment_free
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
Packit a6ee4b
 */
Packit a6ee4b
GstSegment *
Packit a6ee4b
gst_segment_copy (const GstSegment * segment)
Packit a6ee4b
{
Packit a6ee4b
  GstSegment *result = NULL;
Packit a6ee4b
Packit a6ee4b
  if (segment) {
Packit a6ee4b
    result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
Packit a6ee4b
  }
Packit a6ee4b
  return result;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_copy_into:
Packit a6ee4b
 * @src: (transfer none): a #GstSegment
Packit a6ee4b
 * @dest: (transfer none): a #GstSegment
Packit a6ee4b
 *
Packit a6ee4b
 * Copy the contents of @src into @dest.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
Packit a6ee4b
{
Packit a6ee4b
  memcpy (dest, src, sizeof (GstSegment));
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
Packit a6ee4b
    (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_new:
Packit a6ee4b
 *
Packit a6ee4b
 * Allocate a new #GstSegment structure and initialize it using
Packit a6ee4b
 * gst_segment_init().
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_segment_free
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
Packit a6ee4b
 */
Packit a6ee4b
GstSegment *
Packit a6ee4b
gst_segment_new (void)
Packit a6ee4b
{
Packit a6ee4b
  GstSegment *result;
Packit a6ee4b
Packit a6ee4b
  result = g_slice_new0 (GstSegment);
Packit a6ee4b
  gst_segment_init (result, GST_FORMAT_UNDEFINED);
Packit a6ee4b
Packit a6ee4b
  return result;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_free:
Packit a6ee4b
 * @segment: (in) (transfer full): a #GstSegment
Packit a6ee4b
 *
Packit a6ee4b
 * Free the allocated segment @segment.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_segment_free (GstSegment * segment)
Packit a6ee4b
{
Packit a6ee4b
  g_slice_free (GstSegment, segment);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_init:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 *
Packit a6ee4b
 * The start/position fields are set to 0 and the stop/duration
Packit a6ee4b
 * fields are set to -1 (unknown). The default rate of 1.0 and no
Packit a6ee4b
 * flags are set.
Packit a6ee4b
 *
Packit a6ee4b
 * Initialize @segment to its default values.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_segment_init (GstSegment * segment, GstFormat format)
Packit a6ee4b
{
Packit a6ee4b
  g_return_if_fail (segment != NULL);
Packit a6ee4b
Packit a6ee4b
  segment->flags = GST_SEGMENT_FLAG_NONE;
Packit a6ee4b
  segment->rate = 1.0;
Packit a6ee4b
  segment->applied_rate = 1.0;
Packit a6ee4b
  segment->format = format;
Packit a6ee4b
  segment->base = 0;
Packit a6ee4b
  segment->offset = 0;
Packit a6ee4b
  segment->start = 0;
Packit a6ee4b
  segment->stop = -1;
Packit a6ee4b
  segment->time = 0;
Packit a6ee4b
  segment->position = 0;
Packit a6ee4b
  segment->duration = -1;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_do_seek:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @rate: the rate of the segment.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @flags: the segment flags for the segment
Packit a6ee4b
 * @start_type: the seek method
Packit a6ee4b
 * @start: the seek start value
Packit a6ee4b
 * @stop_type: the seek method
Packit a6ee4b
 * @stop: the seek stop value
Packit a6ee4b
 * @update: (out) (allow-none): boolean holding whether position was updated.
Packit a6ee4b
 *
Packit a6ee4b
 * Update the segment structure with the field values of a seek event (see
Packit a6ee4b
 * gst_event_new_seek()).
Packit a6ee4b
 *
Packit a6ee4b
 * After calling this method, the segment field position and time will
Packit a6ee4b
 * contain the requested new position in the segment. The new requested
Packit a6ee4b
 * position in the segment depends on @rate and @start_type and @stop_type.
Packit a6ee4b
 *
Packit a6ee4b
 * For positive @rate, the new position in the segment is the new @segment
Packit a6ee4b
 * start field when it was updated with a @start_type different from
Packit a6ee4b
 * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
Packit a6ee4b
 * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
Packit a6ee4b
 * unmodified.
Packit a6ee4b
 *
Packit a6ee4b
 * For negative @rate, the new position in the segment is the new @segment
Packit a6ee4b
 * stop field when it was updated with a @stop_type different from
Packit a6ee4b
 * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
Packit a6ee4b
 * duration of the segment will be used to update the stop position.
Packit a6ee4b
 * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
Packit a6ee4b
 * @stop is ignored and @segment position is unmodified.
Packit a6ee4b
 *
Packit a6ee4b
 * The applied rate of the segment will be set to 1.0 by default.
Packit a6ee4b
 * If the caller can apply a rate change, it should update @segment
Packit a6ee4b
 * rate and applied_rate after calling this function.
Packit a6ee4b
 *
Packit a6ee4b
 * @update will be set to %TRUE if a seek should be performed to the segment
Packit a6ee4b
 * position field. This field can be %FALSE if, for example, only the @rate
Packit a6ee4b
 * has been changed but not the playback position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if the seek could be performed.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_segment_do_seek (GstSegment * segment, gdouble rate,
Packit a6ee4b
    GstFormat format, GstSeekFlags flags,
Packit a6ee4b
    GstSeekType start_type, guint64 start,
Packit a6ee4b
    GstSeekType stop_type, guint64 stop, gboolean * update)
Packit a6ee4b
{
Packit a6ee4b
  gboolean update_stop, update_start;
Packit a6ee4b
  guint64 position, base;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (rate != 0.0, FALSE);
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, FALSE);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, FALSE);
Packit a6ee4b
Packit a6ee4b
  update_start = update_stop = TRUE;
Packit a6ee4b
Packit a6ee4b
  position = segment->position;
Packit a6ee4b
Packit a6ee4b
  /* segment->start is never invalid */
Packit a6ee4b
  switch (start_type) {
Packit a6ee4b
    case GST_SEEK_TYPE_NONE:
Packit a6ee4b
      /* no update to segment, take previous start */
Packit a6ee4b
      start = segment->start;
Packit a6ee4b
      update_start = FALSE;
Packit a6ee4b
      break;
Packit a6ee4b
    case GST_SEEK_TYPE_SET:
Packit a6ee4b
      /* start holds desired position, map -1 to the start */
Packit a6ee4b
      if (start == -1)
Packit a6ee4b
        start = 0;
Packit a6ee4b
      break;
Packit a6ee4b
    case GST_SEEK_TYPE_END:
Packit a6ee4b
      if (segment->duration != -1) {
Packit a6ee4b
        /* add start to total length */
Packit a6ee4b
        start = segment->duration + start;
Packit a6ee4b
      } else {
Packit a6ee4b
        /* no update if duration unknown */
Packit a6ee4b
        start = segment->start;
Packit a6ee4b
        update_start = FALSE;
Packit a6ee4b
      }
Packit a6ee4b
      break;
Packit a6ee4b
  }
Packit a6ee4b
  /* bring in sane range */
Packit a6ee4b
  if (segment->duration != -1)
Packit a6ee4b
    start = MIN (start, segment->duration);
Packit a6ee4b
  else
Packit a6ee4b
    start = MAX ((gint64) start, 0);
Packit a6ee4b
Packit a6ee4b
  /* stop can be -1 if we have not configured a stop. */
Packit a6ee4b
  switch (stop_type) {
Packit a6ee4b
    case GST_SEEK_TYPE_NONE:
Packit a6ee4b
      stop = segment->stop;
Packit a6ee4b
      update_stop = FALSE;
Packit a6ee4b
      break;
Packit a6ee4b
    case GST_SEEK_TYPE_SET:
Packit a6ee4b
      /* stop holds required value */
Packit a6ee4b
      break;
Packit a6ee4b
    case GST_SEEK_TYPE_END:
Packit a6ee4b
      if (segment->duration != -1) {
Packit a6ee4b
        stop = segment->duration + stop;
Packit a6ee4b
      } else {
Packit a6ee4b
        stop = segment->stop;
Packit a6ee4b
        update_stop = FALSE;
Packit a6ee4b
      }
Packit a6ee4b
      break;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* if we have a valid stop time, make sure it is clipped */
Packit a6ee4b
  if (stop != -1) {
Packit a6ee4b
    if (segment->duration != -1)
Packit a6ee4b
      stop = CLAMP ((gint64) stop, 0, (gint64) segment->duration);
Packit a6ee4b
    else
Packit a6ee4b
      stop = MAX ((gint64) stop, 0);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* we can't have stop before start */
Packit a6ee4b
  if (stop != -1) {
Packit a6ee4b
    if (start > stop) {
Packit a6ee4b
      GST_WARNING ("segment update failed: start(%" G_GUINT64_FORMAT
Packit a6ee4b
          ") > stop(%" G_GUINT64_FORMAT ")", start, stop);
Packit a6ee4b
      g_return_val_if_fail (start <= stop, FALSE);
Packit a6ee4b
      return FALSE;
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (flags & GST_SEEK_FLAG_FLUSH) {
Packit a6ee4b
    /* flush resets the running_time */
Packit a6ee4b
    base = 0;
Packit a6ee4b
  } else {
Packit a6ee4b
    /* make sure the position is inside the segment start/stop */
Packit a6ee4b
    position = CLAMP (position, segment->start, segment->stop);
Packit a6ee4b
Packit a6ee4b
    /* remember the elapsed time */
Packit a6ee4b
    base = gst_segment_to_running_time (segment, format, position);
Packit a6ee4b
    GST_DEBUG ("updated segment.base: %" G_GUINT64_FORMAT, base);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (update_start && rate > 0.0) {
Packit a6ee4b
    position = start;
Packit a6ee4b
  }
Packit a6ee4b
  if (update_stop && rate < 0.0) {
Packit a6ee4b
    if (stop != -1)
Packit a6ee4b
      position = stop;
Packit a6ee4b
    else {
Packit a6ee4b
      if (segment->duration != -1)
Packit a6ee4b
        position = segment->duration;
Packit a6ee4b
      else
Packit a6ee4b
        position = 0;
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* set update arg to reflect update of position */
Packit a6ee4b
  if (update)
Packit a6ee4b
    *update = position != segment->position;
Packit a6ee4b
Packit a6ee4b
  /* update new values */
Packit a6ee4b
  /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
Packit a6ee4b
  segment->flags = GST_SEGMENT_FLAG_NONE;
Packit a6ee4b
  if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
Packit a6ee4b
    segment->flags |= GST_SEGMENT_FLAG_RESET;
Packit a6ee4b
  if ((flags & GST_SEEK_FLAG_TRICKMODE) != 0)
Packit a6ee4b
    segment->flags |= GST_SEGMENT_FLAG_TRICKMODE;
Packit a6ee4b
  if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
Packit a6ee4b
    segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
Packit a6ee4b
  if ((flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS) != 0)
Packit a6ee4b
    segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS;
Packit a6ee4b
  if ((flags & GST_SEEK_FLAG_TRICKMODE_NO_AUDIO) != 0)
Packit a6ee4b
    segment->flags |= GST_SEGMENT_FLAG_TRICKMODE_NO_AUDIO;
Packit a6ee4b
Packit a6ee4b
  segment->rate = rate;
Packit a6ee4b
  segment->applied_rate = 1.0;
Packit a6ee4b
Packit a6ee4b
  segment->base = base;
Packit a6ee4b
  if (rate > 0.0)
Packit a6ee4b
    segment->offset = position - start;
Packit a6ee4b
  else {
Packit a6ee4b
    if (stop != -1)
Packit a6ee4b
      segment->offset = stop - position;
Packit a6ee4b
    else if (segment->duration != -1)
Packit a6ee4b
      segment->offset = segment->duration - position;
Packit a6ee4b
    else
Packit a6ee4b
      segment->offset = 0;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  segment->start = start;
Packit a6ee4b
  segment->stop = stop;
Packit a6ee4b
  segment->time = start;
Packit a6ee4b
  segment->position = position;
Packit a6ee4b
Packit a6ee4b
  GST_INFO ("segment updated: %" GST_SEGMENT_FORMAT, segment);
Packit a6ee4b
Packit a6ee4b
  return TRUE;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_to_stream_time_full:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @position: the position in the segment
Packit a6ee4b
 * @stream_time: (out): result stream-time
Packit a6ee4b
 *
Packit a6ee4b
 * Translate @position to the total stream time using the currently configured
Packit a6ee4b
 * segment. Compared to gst_segment_to_stream_time() this function can return
Packit a6ee4b
 * negative stream-time.
Packit a6ee4b
 *
Packit a6ee4b
 * This function is typically used by elements that need to synchronize buffers
Packit a6ee4b
 * against the clock or each other.
Packit a6ee4b
 *
Packit a6ee4b
 * @position can be any value and the result of this function for values outside
Packit a6ee4b
 * of the segment is extrapolated.
Packit a6ee4b
 *
Packit a6ee4b
 * When 1 is returned, @position resulted in a positive stream-time returned
Packit a6ee4b
 * in @stream_time.
Packit a6ee4b
 *
Packit a6ee4b
 * When this function returns -1, the returned @stream_time should be negated
Packit a6ee4b
 * to get the real negative stream time.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: a 1 or -1 on success, 0 on failure.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.8
Packit a6ee4b
 */
Packit a6ee4b
gint
Packit a6ee4b
gst_segment_to_stream_time_full (const GstSegment * segment, GstFormat format,
Packit a6ee4b
    guint64 position, guint64 * stream_time)
Packit a6ee4b
{
Packit a6ee4b
  guint64 start, stop, time;
Packit a6ee4b
  gdouble abs_applied_rate;
Packit a6ee4b
  gint res;
Packit a6ee4b
Packit a6ee4b
  /* format does not matter for -1 */
Packit a6ee4b
  if (G_UNLIKELY (position == -1)) {
Packit a6ee4b
    *stream_time = -1;
Packit a6ee4b
    return 0;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, 0);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, 0);
Packit a6ee4b
Packit a6ee4b
  stop = segment->stop;
Packit a6ee4b
Packit a6ee4b
  start = segment->start;
Packit a6ee4b
  time = segment->time;
Packit a6ee4b
Packit a6ee4b
  /* time must be known */
Packit a6ee4b
  if (G_UNLIKELY (time == -1))
Packit a6ee4b
    return 0;
Packit a6ee4b
Packit a6ee4b
  abs_applied_rate = ABS (segment->applied_rate);
Packit a6ee4b
Packit a6ee4b
  /* add or subtract from segment time based on applied rate */
Packit a6ee4b
  if (G_LIKELY (segment->applied_rate > 0.0)) {
Packit a6ee4b
    if (G_LIKELY (position > start)) {
Packit a6ee4b
      /* bring to uncorrected position in segment */
Packit a6ee4b
      *stream_time = position - start;
Packit a6ee4b
      /* correct for applied rate if needed */
Packit a6ee4b
      if (G_UNLIKELY (abs_applied_rate != 1.0))
Packit a6ee4b
        *stream_time *= abs_applied_rate;
Packit a6ee4b
      /* correct for segment time */
Packit a6ee4b
      *stream_time += time;
Packit a6ee4b
      res = 1;
Packit a6ee4b
    } else {
Packit a6ee4b
      *stream_time = start - position;
Packit a6ee4b
      if (G_UNLIKELY (abs_applied_rate != 1.0))
Packit a6ee4b
        *stream_time *= abs_applied_rate;
Packit a6ee4b
      if (*stream_time > time) {
Packit a6ee4b
        *stream_time -= time;
Packit a6ee4b
        res = -1;
Packit a6ee4b
      } else {
Packit a6ee4b
        *stream_time = time - *stream_time;
Packit a6ee4b
        res = 1;
Packit a6ee4b
      }
Packit a6ee4b
    }
Packit a6ee4b
  } else {
Packit a6ee4b
    /* correct for segment time. Streams with a negative applied_rate
Packit a6ee4b
     * have timestamps between start and stop, as usual, but have the
Packit a6ee4b
     * time member starting high and going backwards.  */
Packit a6ee4b
    /* cannot continue without a known segment stop */
Packit a6ee4b
    if (G_UNLIKELY (stop == -1))
Packit a6ee4b
      return 0;
Packit a6ee4b
    if (G_UNLIKELY (position > stop)) {
Packit a6ee4b
      *stream_time = position - stop;
Packit a6ee4b
      if (G_UNLIKELY (abs_applied_rate != 1.0))
Packit a6ee4b
        *stream_time *= abs_applied_rate;
Packit a6ee4b
      if (*stream_time > time) {
Packit a6ee4b
        *stream_time -= time;
Packit a6ee4b
        res = -1;
Packit a6ee4b
      } else {
Packit a6ee4b
        *stream_time = time - *stream_time;
Packit a6ee4b
        res = 1;
Packit a6ee4b
      }
Packit a6ee4b
    } else {
Packit a6ee4b
      *stream_time = stop - position;
Packit a6ee4b
      if (G_UNLIKELY (abs_applied_rate != 1.0))
Packit a6ee4b
        *stream_time *= abs_applied_rate;
Packit a6ee4b
      *stream_time += time;
Packit a6ee4b
      res = 1;
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  return res;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_to_stream_time:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @position: the position in the segment
Packit a6ee4b
 *
Packit a6ee4b
 * Translate @position to stream time using the currently configured
Packit a6ee4b
 * segment. The @position value must be between @segment start and
Packit a6ee4b
 * stop value.
Packit a6ee4b
 *
Packit a6ee4b
 * This function is typically used by elements that need to operate on
Packit a6ee4b
 * the stream time of the buffers it receives, such as effect plugins.
Packit a6ee4b
 * In those use cases, @position is typically the buffer timestamp or
Packit a6ee4b
 * clock time that one wants to convert to the stream time.
Packit a6ee4b
 * The stream time is always between 0 and the total duration of the
Packit a6ee4b
 * media stream.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: the position in stream_time or -1 when an invalid position
Packit a6ee4b
 * was given.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.8
Packit a6ee4b
 */
Packit a6ee4b
guint64
Packit a6ee4b
gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
Packit a6ee4b
    guint64 position)
Packit a6ee4b
{
Packit a6ee4b
  guint64 result;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, -1);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, -1);
Packit a6ee4b
Packit a6ee4b
  /* before the segment boundary */
Packit a6ee4b
  if (G_UNLIKELY (position < segment->start)) {
Packit a6ee4b
    GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
Packit a6ee4b
        ")", position, segment->start);
Packit a6ee4b
    return -1;
Packit a6ee4b
  }
Packit a6ee4b
  /* after the segment boundary */
Packit a6ee4b
  if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
Packit a6ee4b
    GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
Packit a6ee4b
        ")", position, segment->stop);
Packit a6ee4b
    return -1;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (gst_segment_to_stream_time_full (segment, format, position, &result) == 1)
Packit a6ee4b
    return result;
Packit a6ee4b
Packit a6ee4b
  return -1;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_position_from_stream_time_full:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @stream_time: the stream-time
Packit a6ee4b
 * @position: (out): the resulting position in the segment
Packit a6ee4b
 *
Packit a6ee4b
 * Translate @stream_time to the segment position using the currently configured
Packit a6ee4b
 * segment. Compared to gst_segment_position_from_stream_time() this function can
Packit a6ee4b
 * return negative segment position.
Packit a6ee4b
 *
Packit a6ee4b
 * This function is typically used by elements that need to synchronize buffers
Packit a6ee4b
 * against the clock or each other.
Packit a6ee4b
 *
Packit a6ee4b
 * @stream_time can be any value and the result of this function for values outside
Packit a6ee4b
 * of the segment is extrapolated.
Packit a6ee4b
 *
Packit a6ee4b
 * When 1 is returned, @stream_time resulted in a positive position returned
Packit a6ee4b
 * in @position.
Packit a6ee4b
 *
Packit a6ee4b
 * When this function returns -1, the returned @position should be negated
Packit a6ee4b
 * to get the real negative segment position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: a 1 or -1 on success, 0 on failure.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.8
Packit a6ee4b
 */
Packit a6ee4b
gint
Packit a6ee4b
gst_segment_position_from_stream_time_full (const GstSegment * segment,
Packit a6ee4b
    GstFormat format, guint64 stream_time, guint64 * position)
Packit a6ee4b
{
Packit a6ee4b
  guint64 start, time;
Packit a6ee4b
  gdouble abs_applied_rate;
Packit a6ee4b
  gint res;
Packit a6ee4b
Packit a6ee4b
  /* format does not matter for -1 */
Packit a6ee4b
  if (G_UNLIKELY (stream_time == -1)) {
Packit a6ee4b
    *position = -1;
Packit a6ee4b
    return 0;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, -1);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, -1);
Packit a6ee4b
Packit a6ee4b
  start = segment->start;
Packit a6ee4b
  time = segment->time;
Packit a6ee4b
Packit a6ee4b
  /* time must be known */
Packit a6ee4b
  if (G_UNLIKELY (time == -1))
Packit a6ee4b
    return 0;
Packit a6ee4b
Packit a6ee4b
  abs_applied_rate = ABS (segment->applied_rate);
Packit a6ee4b
Packit a6ee4b
  if (G_LIKELY (segment->applied_rate > 0.0)) {
Packit a6ee4b
    if (G_LIKELY (stream_time > time)) {
Packit a6ee4b
      res = 1;
Packit a6ee4b
      *position = stream_time - time;
Packit a6ee4b
    } else {
Packit a6ee4b
      res = -1;
Packit a6ee4b
      *position = time - stream_time;
Packit a6ee4b
    }
Packit a6ee4b
    /* correct for applied rate if needed */
Packit a6ee4b
    if (G_UNLIKELY (abs_applied_rate != 1.0))
Packit a6ee4b
      *position /= abs_applied_rate;
Packit a6ee4b
Packit a6ee4b
    if (G_UNLIKELY (res == -1)) {
Packit a6ee4b
      if (*position > start) {
Packit a6ee4b
        *position -= start;
Packit a6ee4b
      } else {
Packit a6ee4b
        *position = start - *position;
Packit a6ee4b
        res = 1;
Packit a6ee4b
      }
Packit a6ee4b
    } else {
Packit a6ee4b
      *position += start;
Packit a6ee4b
    }
Packit a6ee4b
  } else {
Packit a6ee4b
    GstClockTime stop = segment->stop;
Packit a6ee4b
    /* cannot continue without a known segment stop */
Packit a6ee4b
    if (G_UNLIKELY (stop == -1))
Packit a6ee4b
      return 0;
Packit a6ee4b
    if (G_UNLIKELY (time > stream_time)) {
Packit a6ee4b
      res = -1;
Packit a6ee4b
      *position = time - stream_time;
Packit a6ee4b
    } else {
Packit a6ee4b
      res = 1;
Packit a6ee4b
      *position = stream_time - time;
Packit a6ee4b
    }
Packit a6ee4b
    if (G_UNLIKELY (abs_applied_rate != 1.0))
Packit a6ee4b
      *position /= abs_applied_rate;
Packit a6ee4b
    if (G_UNLIKELY (stop < *position)) {
Packit a6ee4b
      if (G_LIKELY (res == 1)) {
Packit a6ee4b
        *position -= stop;
Packit a6ee4b
        res = -1;
Packit a6ee4b
      } else {
Packit a6ee4b
        *position += stop;
Packit a6ee4b
        res = 1;
Packit a6ee4b
      }
Packit a6ee4b
    } else {
Packit a6ee4b
      if (G_LIKELY (res == 1)) {
Packit a6ee4b
        *position = stop - *position;
Packit a6ee4b
        res = 1;
Packit a6ee4b
      } else {
Packit a6ee4b
        *position += stop;
Packit a6ee4b
        res = 1;
Packit a6ee4b
      }
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  return res;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_position_from_stream_time:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @stream_time: the stream_time in the segment
Packit a6ee4b
 *
Packit a6ee4b
 * Convert @stream_time into a position in the segment so that
Packit a6ee4b
 * gst_segment_to_stream_time() with that position returns @stream_time.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: the position in the segment for @stream_time. This function returns
Packit a6ee4b
 * -1 when @stream_time is -1 or when it is not inside @segment.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.8
Packit a6ee4b
 */
Packit a6ee4b
guint64
Packit a6ee4b
gst_segment_position_from_stream_time (const GstSegment * segment,
Packit a6ee4b
    GstFormat format, guint64 stream_time)
Packit a6ee4b
{
Packit a6ee4b
  guint64 position;
Packit a6ee4b
  gint res;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, -1);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, -1);
Packit a6ee4b
Packit a6ee4b
  res =
Packit a6ee4b
      gst_segment_position_from_stream_time_full (segment, format, stream_time,
Packit a6ee4b
      &position);
Packit a6ee4b
Packit a6ee4b
  /* before the segment boundary */
Packit a6ee4b
  if (G_UNLIKELY (position < segment->start)) {
Packit a6ee4b
    GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
Packit a6ee4b
        ")", position, segment->start);
Packit a6ee4b
    return -1;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* after the segment boundary */
Packit a6ee4b
  if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
Packit a6ee4b
    GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
Packit a6ee4b
        ")", position, segment->stop);
Packit a6ee4b
    return -1;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (res == 1)
Packit a6ee4b
    return position;
Packit a6ee4b
Packit a6ee4b
  return -1;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_to_running_time_full:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @position: the position in the segment
Packit a6ee4b
 * @running_time: (out) (allow-none): result running-time
Packit a6ee4b
 *
Packit a6ee4b
 * Translate @position to the total running time using the currently configured
Packit a6ee4b
 * segment. Compared to gst_segment_to_running_time() this function can return
Packit a6ee4b
 * negative running-time.
Packit a6ee4b
 *
Packit a6ee4b
 * This function is typically used by elements that need to synchronize buffers
Packit a6ee4b
 * against the clock or each other.
Packit a6ee4b
 *
Packit a6ee4b
 * @position can be any value and the result of this function for values outside
Packit a6ee4b
 * of the segment is extrapolated.
Packit a6ee4b
 *
Packit a6ee4b
 * When 1 is returned, @position resulted in a positive running-time returned
Packit a6ee4b
 * in @running_time.
Packit a6ee4b
 *
Packit a6ee4b
 * When this function returns -1, the returned @running_time should be negated
Packit a6ee4b
 * to get the real negative running time.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: a 1 or -1 on success, 0 on failure.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.6
Packit a6ee4b
 */
Packit a6ee4b
gint
Packit a6ee4b
gst_segment_to_running_time_full (const GstSegment * segment, GstFormat format,
Packit a6ee4b
    guint64 position, guint64 * running_time)
Packit a6ee4b
{
Packit a6ee4b
  gint res = 0;
Packit a6ee4b
  guint64 result;
Packit a6ee4b
  guint64 start, stop, offset;
Packit a6ee4b
  gdouble abs_rate;
Packit a6ee4b
Packit a6ee4b
  if (G_UNLIKELY (position == -1)) {
Packit a6ee4b
    GST_DEBUG ("invalid position (-1)");
Packit a6ee4b
    goto done;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, 0);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, 0);
Packit a6ee4b
Packit a6ee4b
  offset = segment->offset;
Packit a6ee4b
Packit a6ee4b
  if (G_LIKELY (segment->rate > 0.0)) {
Packit a6ee4b
    start = segment->start + offset;
Packit a6ee4b
Packit a6ee4b
    /* bring to uncorrected position in segment */
Packit a6ee4b
    if (position < start) {
Packit a6ee4b
      /* negative value */
Packit a6ee4b
      result = start - position;
Packit a6ee4b
      res = -1;
Packit a6ee4b
    } else {
Packit a6ee4b
      result = position - start;
Packit a6ee4b
      res = 1;
Packit a6ee4b
    }
Packit a6ee4b
  } else {
Packit a6ee4b
    stop = segment->stop;
Packit a6ee4b
Packit a6ee4b
    if (stop == -1 && segment->duration != -1)
Packit a6ee4b
      stop = segment->start + segment->duration;
Packit a6ee4b
Packit a6ee4b
    /* cannot continue if no stop position set or invalid offset */
Packit a6ee4b
    g_return_val_if_fail (stop != -1, 0);
Packit a6ee4b
    g_return_val_if_fail (stop >= offset, 0);
Packit a6ee4b
Packit a6ee4b
    stop -= offset;
Packit a6ee4b
Packit a6ee4b
    /* bring to uncorrected position in segment */
Packit a6ee4b
    if (position > stop) {
Packit a6ee4b
      /* negative value */
Packit a6ee4b
      result = position - stop;
Packit a6ee4b
      res = -1;
Packit a6ee4b
    } else {
Packit a6ee4b
      result = stop - position;
Packit a6ee4b
      res = 1;
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (running_time) {
Packit a6ee4b
    /* scale based on the rate, avoid division by and conversion to
Packit a6ee4b
     * float when not needed */
Packit a6ee4b
    abs_rate = ABS (segment->rate);
Packit a6ee4b
    if (G_UNLIKELY (abs_rate != 1.0))
Packit a6ee4b
      result /= abs_rate;
Packit a6ee4b
Packit a6ee4b
    /* correct for base of the segment */
Packit a6ee4b
    if (res == 1)
Packit a6ee4b
      /* positive, add base */
Packit a6ee4b
      *running_time = result + segment->base;
Packit a6ee4b
    else if (segment->base >= result) {
Packit a6ee4b
      /* negative and base is bigger, subtract from base and we have a
Packit a6ee4b
       * positive value again */
Packit a6ee4b
      *running_time = segment->base - result;
Packit a6ee4b
      res = 1;
Packit a6ee4b
    } else {
Packit a6ee4b
      /* negative and base is smaller, subtract base and remainder is
Packit a6ee4b
       * negative */
Packit a6ee4b
      *running_time = result - segment->base;
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
  return res;
Packit a6ee4b
Packit a6ee4b
done:
Packit a6ee4b
  {
Packit a6ee4b
    if (running_time)
Packit a6ee4b
      *running_time = -1;
Packit a6ee4b
    return 0;
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_to_running_time:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @position: the position in the segment
Packit a6ee4b
 *
Packit a6ee4b
 * Translate @position to the total running time using the currently configured
Packit a6ee4b
 * segment. Position is a value between @segment start and stop time.
Packit a6ee4b
 *
Packit a6ee4b
 * This function is typically used by elements that need to synchronize to the
Packit a6ee4b
 * global clock in a pipeline. The running time is a constantly increasing value
Packit a6ee4b
 * starting from 0. When gst_segment_init() is called, this value will reset to
Packit a6ee4b
 * 0.
Packit a6ee4b
 *
Packit a6ee4b
 * This function returns -1 if the position is outside of @segment start and stop.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: the position as the total running time or -1 when an invalid position
Packit a6ee4b
 * was given.
Packit a6ee4b
 */
Packit a6ee4b
guint64
Packit a6ee4b
gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
Packit a6ee4b
    guint64 position)
Packit a6ee4b
{
Packit a6ee4b
  guint64 result;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, -1);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, -1);
Packit a6ee4b
Packit a6ee4b
  /* before the segment boundary */
Packit a6ee4b
  if (G_UNLIKELY (position < segment->start)) {
Packit a6ee4b
    GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
Packit a6ee4b
        ")", position, segment->start);
Packit a6ee4b
    return -1;
Packit a6ee4b
  }
Packit a6ee4b
  /* after the segment boundary */
Packit a6ee4b
  if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
Packit a6ee4b
    GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
Packit a6ee4b
        ")", position, segment->stop);
Packit a6ee4b
    return -1;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (gst_segment_to_running_time_full (segment, format, position,
Packit a6ee4b
          &result) == 1)
Packit a6ee4b
    return result;
Packit a6ee4b
Packit a6ee4b
  return -1;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_clip:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @start: the start position in the segment
Packit a6ee4b
 * @stop: the stop position in the segment
Packit a6ee4b
 * @clip_start: (out) (allow-none): the clipped start position in the segment
Packit a6ee4b
 * @clip_stop: (out) (allow-none): the clipped stop position in the segment
Packit a6ee4b
 *
Packit a6ee4b
 * Clip the given @start and @stop values to the segment boundaries given
Packit a6ee4b
 * in @segment. @start and @stop are compared and clipped to @segment
Packit a6ee4b
 * start and stop values.
Packit a6ee4b
 *
Packit a6ee4b
 * If the function returns %FALSE, @start and @stop are known to fall
Packit a6ee4b
 * outside of @segment and @clip_start and @clip_stop are not updated.
Packit a6ee4b
 *
Packit a6ee4b
 * When the function returns %TRUE, @clip_start and @clip_stop will be
Packit a6ee4b
 * updated. If @clip_start or @clip_stop are different from @start or @stop
Packit a6ee4b
 * respectively, the region fell partially in the segment.
Packit a6ee4b
 *
Packit a6ee4b
 * Note that when @stop is -1, @clip_stop will be set to the end of the
Packit a6ee4b
 * segment. Depending on the use case, this may or may not be what you want.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if the given @start and @stop times fall partially or
Packit a6ee4b
 *     completely in @segment, %FALSE if the values are completely outside
Packit a6ee4b
 *     of the segment.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
Packit a6ee4b
    guint64 stop, guint64 * clip_start, guint64 * clip_stop)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, FALSE);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, FALSE);
Packit a6ee4b
Packit a6ee4b
  /* if we have a stop position and a valid start and start is bigger,
Packit a6ee4b
   * we're outside of the segment. (Special case) segment start and
Packit a6ee4b
   * segment stop can be identical. In this case, if start is also identical,
Packit a6ee4b
   * it's inside of segment */
Packit a6ee4b
  if (G_UNLIKELY (segment->stop != -1 && start != -1 && (start > segment->stop
Packit a6ee4b
              || (segment->start != segment->stop && start == segment->stop))))
Packit a6ee4b
    return FALSE;
Packit a6ee4b
Packit a6ee4b
  /* if a stop position is given and is before the segment start,
Packit a6ee4b
   * we're outside of the segment. Special case is were start
Packit a6ee4b
   * and stop are equal to the segment start. In that case we
Packit a6ee4b
   * are inside the segment. */
Packit a6ee4b
  if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
Packit a6ee4b
                  && stop == segment->start))))
Packit a6ee4b
    return FALSE;
Packit a6ee4b
Packit a6ee4b
  if (clip_start) {
Packit a6ee4b
    if (start == -1)
Packit a6ee4b
      *clip_start = -1;
Packit a6ee4b
    else
Packit a6ee4b
      *clip_start = MAX (start, segment->start);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (clip_stop) {
Packit a6ee4b
    if (stop == -1)
Packit a6ee4b
      *clip_stop = segment->stop;
Packit a6ee4b
    else if (segment->stop == -1)
Packit a6ee4b
      *clip_stop = stop;
Packit a6ee4b
    else
Packit a6ee4b
      *clip_stop = MIN (stop, segment->stop);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  return TRUE;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_position_from_running_time:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @running_time: the running_time in the segment
Packit a6ee4b
 *
Packit a6ee4b
 * Convert @running_time into a position in the segment so that
Packit a6ee4b
 * gst_segment_to_running_time() with that position returns @running_time.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: the position in the segment for @running_time. This function returns
Packit a6ee4b
 * -1 when @running_time is -1 or when it is not inside @segment.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.8
Packit a6ee4b
 */
Packit a6ee4b
guint64
Packit a6ee4b
gst_segment_position_from_running_time (const GstSegment * segment,
Packit a6ee4b
    GstFormat format, guint64 running_time)
Packit a6ee4b
{
Packit a6ee4b
  guint64 position;
Packit a6ee4b
  gint res;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, -1);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, -1);
Packit a6ee4b
Packit a6ee4b
  res =
Packit a6ee4b
      gst_segment_position_from_running_time_full (segment, format,
Packit a6ee4b
      running_time, &position);
Packit a6ee4b
Packit a6ee4b
  if (res != 1)
Packit a6ee4b
    return -1;
Packit a6ee4b
Packit a6ee4b
  /* before the segment boundary */
Packit a6ee4b
  if (G_UNLIKELY (position < segment->start)) {
Packit a6ee4b
    GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
Packit a6ee4b
        ")", position, segment->start);
Packit a6ee4b
    return -1;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* after the segment boundary */
Packit a6ee4b
  if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
Packit a6ee4b
    GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
Packit a6ee4b
        ")", position, segment->stop);
Packit a6ee4b
    return -1;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  return position;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_position_from_running_time_full:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @running_time: the running-time
Packit a6ee4b
 * @position: (out): the resulting position in the segment
Packit a6ee4b
 *
Packit a6ee4b
 * Translate @running_time to the segment position using the currently configured
Packit a6ee4b
 * segment. Compared to gst_segment_position_from_running_time() this function can
Packit a6ee4b
 * return negative segment position.
Packit a6ee4b
 *
Packit a6ee4b
 * This function is typically used by elements that need to synchronize buffers
Packit a6ee4b
 * against the clock or each other.
Packit a6ee4b
 *
Packit a6ee4b
 * @running_time can be any value and the result of this function for values
Packit a6ee4b
 * outside of the segment is extrapolated.
Packit a6ee4b
 *
Packit a6ee4b
 * When 1 is returned, @running_time resulted in a positive position returned
Packit a6ee4b
 * in @position.
Packit a6ee4b
 *
Packit a6ee4b
 * When this function returns -1, the returned @position was < 0, and the value
Packit a6ee4b
 * in the position variable should be negated to get the real negative segment
Packit a6ee4b
 * position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: a 1 or -1 on success, 0 on failure.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.8
Packit a6ee4b
 */
Packit a6ee4b
gint
Packit a6ee4b
gst_segment_position_from_running_time_full (const GstSegment * segment,
Packit a6ee4b
    GstFormat format, guint64 running_time, guint64 * position)
Packit a6ee4b
{
Packit a6ee4b
  gint res;
Packit a6ee4b
  guint64 start, stop, base;
Packit a6ee4b
  gdouble abs_rate;
Packit a6ee4b
Packit a6ee4b
  if (G_UNLIKELY (running_time == -1)) {
Packit a6ee4b
    *position = -1;
Packit a6ee4b
    return 0;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, 0);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, 0);
Packit a6ee4b
Packit a6ee4b
  base = segment->base;
Packit a6ee4b
Packit a6ee4b
  abs_rate = ABS (segment->rate);
Packit a6ee4b
Packit a6ee4b
  start = segment->start;
Packit a6ee4b
  stop = segment->stop;
Packit a6ee4b
Packit a6ee4b
  if (G_LIKELY (segment->rate > 0.0)) {
Packit a6ee4b
    /* start by subtracting the base time */
Packit a6ee4b
    if (G_LIKELY (running_time >= base)) {
Packit a6ee4b
      *position = running_time - base;
Packit a6ee4b
      /* move into the segment at the right rate */
Packit a6ee4b
      if (G_UNLIKELY (abs_rate != 1.0))
Packit a6ee4b
        *position = ceil (*position * abs_rate);
Packit a6ee4b
      /* bring to corrected position in segment */
Packit a6ee4b
      *position += start + segment->offset;
Packit a6ee4b
      res = 1;
Packit a6ee4b
    } else {
Packit a6ee4b
      *position = base - running_time;
Packit a6ee4b
      if (G_UNLIKELY (abs_rate != 1.0))
Packit a6ee4b
        *position = ceil (*position * abs_rate);
Packit a6ee4b
      if (start + segment->offset >= *position) {
Packit a6ee4b
        /* The TS is before the segment, but the result is >= 0 */
Packit a6ee4b
        *position = start + segment->offset - *position;
Packit a6ee4b
        res = 1;
Packit a6ee4b
      } else {
Packit a6ee4b
        /* The TS is before the segment, and the result is < 0
Packit a6ee4b
         * so negate the return result */
Packit a6ee4b
        *position = *position - (start + segment->offset);
Packit a6ee4b
        res = -1;
Packit a6ee4b
      }
Packit a6ee4b
    }
Packit a6ee4b
  } else {
Packit a6ee4b
    if (G_LIKELY (running_time >= base)) {
Packit a6ee4b
      *position = running_time - base;
Packit a6ee4b
      if (G_UNLIKELY (abs_rate != 1.0))
Packit a6ee4b
        *position = ceil (*position * abs_rate);
Packit a6ee4b
      if (G_UNLIKELY (stop < *position + segment->offset)) {
Packit a6ee4b
        *position += segment->offset - stop;
Packit a6ee4b
        res = -1;
Packit a6ee4b
      } else {
Packit a6ee4b
        *position = stop - *position - segment->offset;
Packit a6ee4b
        res = 1;
Packit a6ee4b
      }
Packit a6ee4b
    } else {
Packit a6ee4b
      /* This case is tricky. Requested running time precedes the
Packit a6ee4b
       * segment base, so in a reversed segment where rate < 0, that
Packit a6ee4b
       * means it's before the alignment point of (stop - offset).
Packit a6ee4b
       * Before = always bigger than (stop-offset), which is usually +ve,
Packit a6ee4b
       * but could be -ve is offset is big enough. -ve position implies
Packit a6ee4b
       * that the offset has clipped away the entire segment anyway */
Packit a6ee4b
      *position = base - running_time;
Packit a6ee4b
      if (G_UNLIKELY (abs_rate != 1.0))
Packit a6ee4b
        *position = ceil (*position * abs_rate);
Packit a6ee4b
Packit a6ee4b
      if (G_LIKELY (stop + *position >= segment->offset)) {
Packit a6ee4b
        *position = stop + *position - segment->offset;
Packit a6ee4b
        res = 1;
Packit a6ee4b
      } else {
Packit a6ee4b
        /* Requested position is still negative because offset is big,
Packit a6ee4b
         * so negate the result */
Packit a6ee4b
        *position = segment->offset - *position - stop;
Packit a6ee4b
        res = -1;
Packit a6ee4b
      }
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
  return res;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_to_position:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @running_time: the running_time in the segment
Packit a6ee4b
 *
Packit a6ee4b
 * Convert @running_time into a position in the segment so that
Packit a6ee4b
 * gst_segment_to_running_time() with that position returns @running_time.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: the position in the segment for @running_time. This function returns
Packit a6ee4b
 * -1 when @running_time is -1 or when it is not inside @segment.
Packit a6ee4b
 *
Packit a6ee4b
 * Deprecated: Use gst_segment_position_from_running_time() instead.
Packit a6ee4b
 */
Packit a6ee4b
#ifndef GST_REMOVE_DEPRECATED
Packit a6ee4b
guint64
Packit a6ee4b
gst_segment_to_position (const GstSegment * segment, GstFormat format,
Packit a6ee4b
    guint64 running_time)
Packit a6ee4b
{
Packit a6ee4b
  return gst_segment_position_from_running_time (segment, format, running_time);
Packit a6ee4b
}
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_set_running_time:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @running_time: the running_time in the segment
Packit a6ee4b
 *
Packit a6ee4b
 * Adjust the start/stop and base values of @segment such that the next valid
Packit a6ee4b
 * buffer will be one with @running_time.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
Packit a6ee4b
 * returned, @running_time is -1 or not in @segment.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_segment_set_running_time (GstSegment * segment, GstFormat format,
Packit a6ee4b
    guint64 running_time)
Packit a6ee4b
{
Packit a6ee4b
  guint64 position;
Packit a6ee4b
  guint64 start, stop;
Packit a6ee4b
Packit a6ee4b
  /* start by bringing the running_time into the segment position */
Packit a6ee4b
  position =
Packit a6ee4b
      gst_segment_position_from_running_time (segment, format, running_time);
Packit a6ee4b
Packit a6ee4b
  /* we must have a valid position now */
Packit a6ee4b
  if (G_UNLIKELY (position == -1))
Packit a6ee4b
    return FALSE;
Packit a6ee4b
Packit a6ee4b
  start = segment->start;
Packit a6ee4b
  stop = segment->stop;
Packit a6ee4b
Packit a6ee4b
  if (G_LIKELY (segment->rate > 0.0)) {
Packit a6ee4b
    /* update the start and time values */
Packit a6ee4b
    start = position;
Packit a6ee4b
  } else {
Packit a6ee4b
    /* reverse, update stop */
Packit a6ee4b
    stop = position;
Packit a6ee4b
  }
Packit a6ee4b
  /* and base time is exactly the running time */
Packit a6ee4b
  segment->time = gst_segment_to_stream_time (segment, format, start);
Packit a6ee4b
  segment->start = start;
Packit a6ee4b
  segment->stop = stop;
Packit a6ee4b
  segment->base = running_time;
Packit a6ee4b
Packit a6ee4b
  return TRUE;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_offset_running_time:
Packit a6ee4b
 * @segment: a #GstSegment structure.
Packit a6ee4b
 * @format: the format of the segment.
Packit a6ee4b
 * @offset: the offset to apply in the segment
Packit a6ee4b
 *
Packit a6ee4b
 * Adjust the values in @segment so that @offset is applied to all
Packit a6ee4b
 * future running-time calculations.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.2.3
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
Packit a6ee4b
 * returned, @offset is not in @segment.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_segment_offset_running_time (GstSegment * segment, GstFormat format,
Packit a6ee4b
    gint64 offset)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (segment != NULL, FALSE);
Packit a6ee4b
  g_return_val_if_fail (segment->format == format, FALSE);
Packit a6ee4b
Packit a6ee4b
  if (offset == 0)
Packit a6ee4b
    return TRUE;
Packit a6ee4b
Packit a6ee4b
  if (offset > 0) {
Packit a6ee4b
    /* positive offset, we can simply apply to the base time */
Packit a6ee4b
    segment->base += offset;
Packit a6ee4b
  } else {
Packit a6ee4b
    offset = -offset;
Packit a6ee4b
    /* negative offset, first try to subtract from base */
Packit a6ee4b
    if (segment->base > offset) {
Packit a6ee4b
      segment->base -= offset;
Packit a6ee4b
    } else {
Packit a6ee4b
      guint64 position;
Packit a6ee4b
Packit a6ee4b
      /* subtract all from segment.base, remainder in offset */
Packit a6ee4b
      offset -= segment->base;
Packit a6ee4b
      segment->base = 0;
Packit a6ee4b
      position =
Packit a6ee4b
          gst_segment_position_from_running_time (segment, format, offset);
Packit a6ee4b
      if (position == -1)
Packit a6ee4b
        return FALSE;
Packit a6ee4b
Packit a6ee4b
      segment->offset = position - segment->start;
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
  return TRUE;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_segment_is_equal:
Packit a6ee4b
 * @s0: a #GstSegment structure.
Packit a6ee4b
 * @s1: a #GstSegment structure.
Packit a6ee4b
 *
Packit a6ee4b
 * Checks for two segments being equal. Equality here is defined
Packit a6ee4b
 * as perfect equality, including floating point values.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.6
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if the segments are equal, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_segment_is_equal (const GstSegment * s0, const GstSegment * s1)
Packit a6ee4b
{
Packit a6ee4b
  if (s0->flags != s1->flags)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  if (s0->rate != s1->rate)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  if (s0->applied_rate != s1->applied_rate)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  if (s0->format != s1->format)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  if (s0->base != s1->base)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  if (s0->offset != s1->offset)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  if (s0->start != s1->start)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  if (s0->stop != s1->stop)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  if (s0->time != s1->time)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  if (s0->position != s1->position)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  if (s0->duration != s1->duration)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  return TRUE;
Packit a6ee4b
}