+/**
+ * gst_segment_position_from_stream_time_full:
+ * @segment: a #GstSegment structure.
+ * @format: the format of the segment.
+ * @stream_time: the stream-time
+ * @position: (out): the resulting position in the segment
+ *
+ * Translate @stream_time to the segment position using the currently configured
+ * segment. Compared to gst_segment_position_from_stream_time() this function can
+ * return negative segment position.
+ *
+ * This function is typically used by elements that need to synchronize buffers
+ * against the clock or each other.
+ *
+ * @stream_time can be any value and the result of this function for values outside
+ * of the segment is extrapolated.
+ *
+ * When 1 is returned, @stream_time resulted in a positive position returned
+ * in @position.
+ *
+ * When this function returns -1, the returned @position should be negated
+ * to get the real negative segment position.
+ *
+ * Returns: a 1 or -1 on success, 0 on failure.
+ *
+ * Since: 1.8
+ */
+gint
+gst_segment_position_from_stream_time_full (const GstSegment * segment,
+ GstFormat format, guint64 stream_time, guint64 * position)
+{
+ guint64 start, time;
+ gdouble abs_applied_rate;
+ gint res;
+
+ /* format does not matter for -1 */
+ if (G_UNLIKELY (stream_time == -1)) {
+ *position = -1;
+ return 0;
+ }
+
+ g_return_val_if_fail (segment != NULL, -1);
+ g_return_val_if_fail (segment->format == format, -1);
+
+ start = segment->start;
+ time = segment->time;
+
+ /* time must be known */
+ if (G_UNLIKELY (time == -1))
+ return 0;
+
+ abs_applied_rate = ABS (segment->applied_rate);
+
+ if (G_LIKELY (segment->applied_rate > 0.0)) {
+ if (G_LIKELY (stream_time > time)) {
+ res = 1;
+ *position = stream_time - time;
+ } else {
+ res = -1;
+ *position = time - stream_time;
+ }
+ /* correct for applied rate if needed */
+ if (G_UNLIKELY (abs_applied_rate != 1.0))
+ *position /= abs_applied_rate;
+
+ if (G_UNLIKELY (res == -1)) {
+ if (*position > start) {
+ *position -= start;
+ } else {
+ *position = start - *position;
+ res = 1;
+ }
+ } else {
+ *position += start;
+ }
+ } else {
+ GstClockTime stop = segment->stop;
+ /* cannot continue without a known segment stop */
+ if (G_UNLIKELY (stop == -1))
+ return 0;
+ if (G_UNLIKELY (time > stream_time)) {
+ res = -1;
+ *position = time - stream_time;
+ } else {
+ res = 1;
+ *position = stream_time - time;
+ }
+ if (G_UNLIKELY (abs_applied_rate != 1.0))
+ *position /= abs_applied_rate;
+ if (G_UNLIKELY (stop < *position)) {
+ if (G_LIKELY (res == 1)) {
+ *position -= stop;
+ res = -1;
+ } else {
+ *position += stop;
+ res = 1;
+ }
+ } else {
+ if (G_LIKELY (res == 1)) {
+ *position = stop - *position;
+ res = 1;
+ } else {
+ *position += stop;
+ res = 1;
+ }
+ }
+ }
+
+ return res;
+}
+
+/**
+ * gst_segment_position_from_stream_time:
+ * @segment: a #GstSegment structure.
+ * @format: the format of the segment.
+ * @stream_time: the stream_time in the segment
+ *
+ * Convert @stream_time into a position in the segment so that
+ * gst_segment_to_stream_time() with that position returns @stream_time.
+ *
+ * Returns: the position in the segment for @stream_time. This function returns
+ * -1 when @stream_time is -1 or when it is not inside @segment.
+ *
+ * Since: 1.8
+ */
+guint64
+gst_segment_position_from_stream_time (const GstSegment * segment,
+ GstFormat format, guint64 stream_time)
+{
+ guint64 position;
+ gint res;
+
+ g_return_val_if_fail (segment != NULL, -1);
+ g_return_val_if_fail (segment->format == format, -1);
+
+ res =
+ gst_segment_position_from_stream_time_full (segment, format, stream_time,
+ &position);
+
+ /* before the segment boundary */
+ if (G_UNLIKELY (position < segment->start)) {
+ GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
+ ")", position, segment->start);
+ return -1;
+ }
+
+ /* after the segment boundary */
+ if (G_UNLIKELY (segment->stop != -1 && position > segment->stop)) {
+ GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
+ ")", position, segment->stop);
+ return -1;
+ }
+
+ if (res == 1)
+ return position;
+
+ return -1;
+}
+
+/**
+ * gst_segment_to_running_time_full:
+ * @segment: a #GstSegment structure.
+ * @format: the format of the segment.
+ * @position: the position in the segment
+ * @running_time: (out) (allow-none): result running-time
+ *
+ * Translate @position to the total running time using the currently configured
+ * segment. Compared to gst_segment_to_running_time() this function can return
+ * negative running-time.
+ *
+ * This function is typically used by elements that need to synchronize buffers
+ * against the clock or each other.
+ *
+ * @position can be any value and the result of this function for values outside
+ * of the segment is extrapolated.
+ *
+ * When 1 is returned, @position resulted in a positive running-time returned
+ * in @running_time.
+ *
+ * When this function returns -1, the returned @running_time should be negated
+ * to get the real negative running time.
+ *
+ * Returns: a 1 or -1 on success, 0 on failure.
+ *
+ * Since: 1.6
+ */
+gint
+gst_segment_to_running_time_full (const GstSegment * segment, GstFormat format,
+ guint64 position, guint64 * running_time)
+{
+ gint res = 0;
+ guint64 result;
+ guint64 start, stop, offset;
+ gdouble abs_rate;
+
+ if (G_UNLIKELY (position == -1)) {
+ GST_DEBUG ("invalid position (-1)");
+ goto done;
+ }
+
+ g_return_val_if_fail (segment != NULL, 0);
+ g_return_val_if_fail (segment->format == format, 0);
+
+ offset = segment->offset;
+
+ if (G_LIKELY (segment->rate > 0.0)) {
+ start = segment->start + offset;
+
+ /* bring to uncorrected position in segment */
+ if (position < start) {
+ /* negative value */
+ result = start - position;
+ res = -1;
+ } else {
+ result = position - start;
+ res = 1;
+ }
+ } else {
+ stop = segment->stop;
+
+ if (stop == -1 && segment->duration != -1)
+ stop = segment->start + segment->duration;
+
+ /* cannot continue if no stop position set or invalid offset */
+ g_return_val_if_fail (stop != -1, 0);
+ g_return_val_if_fail (stop >= offset, 0);
+
+ stop -= offset;
+
+ /* bring to uncorrected position in segment */
+ if (position > stop) {
+ /* negative value */
+ result = position - stop;
+ res = -1;
+ } else {
+ result = stop - position;
+ res = 1;
+ }
+ }
+
+ if (running_time) {
+ /* scale based on the rate, avoid division by and conversion to
+ * float when not needed */
+ abs_rate = ABS (segment->rate);
+ if (G_UNLIKELY (abs_rate != 1.0))
+ result /= abs_rate;
+
+ /* correct for base of the segment */
+ if (res == 1)
+ /* positive, add base */
+ *running_time = result + segment->base;
+ else if (segment->base >= result) {
+ /* negative and base is bigger, subtract from base and we have a
+ * positive value again */
+ *running_time = segment->base - result;
+ res = 1;
+ } else {
+ /* negative and base is smaller, subtract base and remainder is
+ * negative */
+ *running_time = result - segment->base;
+ }
+ }
+ return res;
+
+done:
+ {
+ if (running_time)
+ *running_time = -1;
+ return 0;
+ }