segment: remove the bounds check from _to_running_time_full()

Do not do any checks for the start/stop in the new
gst_segment_to_running_time_full() method, we can let this be done by
the more capable gst_segment_clip() method. This allows us to remove the
enum of results and only return the sign of the calculated running-time.
We need to put the old clipping checks in the old
gst_segment_to_running_time() still because they work slightly
differently than the _clip methods.

See https://bugzilla.gnome.org/show_bug.cgi?id=740575
This commit is contained in:
Wim Taymans 2015-03-20 09:00:47 +01:00
parent edf484ab6b
commit bc282da83c
5 changed files with 61 additions and 96 deletions

View file

@ -669,7 +669,6 @@ init_post (GOptionContext * context, GOptionGroup * group, gpointer data,
g_type_class_ref (gst_lock_flags_get_type ());
g_type_class_ref (gst_allocator_flags_get_type ());
g_type_class_ref (gst_stream_flags_get_type ());
g_type_class_ref (gst_segment_result_get_type ());
_priv_gst_event_initialize ();
_priv_gst_buffer_initialize ();
@ -1068,7 +1067,6 @@ gst_deinit (void)
g_type_class_unref (g_type_class_peek (gst_allocator_flags_get_type ()));
g_type_class_unref (g_type_class_peek (gst_stream_flags_get_type ()));
g_type_class_unref (g_type_class_peek (gst_debug_color_mode_get_type ()));
g_type_class_unref (g_type_class_peek (gst_segment_result_get_type ()));
gst_deinitialized = TRUE;
GST_INFO ("deinitialized GStreamer");

View file

@ -457,95 +457,65 @@ gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
* @segment: a #GstSegment structure.
* @format: the format of the segment.
* @position: the position in the segment
* @clip: clip against segment boundaries
* @running_time: 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 and also check if a @position is before or after the
* segment.
* negative running-time.
*
* This function is typically used by elements that need to synchronize buffers
* against the clock or eachother.
*
* If clip is %TRUE, @position is a value between @segment start and stop.
* When @position is outside of the segment start and stop values,
* #GST_SEGMENT_RESULT_BEFORE or #GST_SEGMENT_RESULT_AFTER is returned depending
* if @position is respectively before or after the segment.
* @position can be any value and the result of this function for values outside
* of the segment is extrapolated.
*
* If clip is %FALSE, @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 #GST_SEGMENT_RESULT_OK 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.
*
* When this function returns #GST_SEGMENT_RESULT_NEGATIVE, the returned
* @running_time should be negated to get the real negative running time.
*
* Returns: a #GstSegmentResult
* Returns: a 1 or -1 on success, 0 on failure.
*
* Since: 1.6
*/
GstSegmentResult
gint
gst_segment_to_running_time_full (const GstSegment * segment, GstFormat format,
guint64 position, gboolean clip, guint64 * running_time)
guint64 position, guint64 * running_time)
{
GstSegmentResult res;
gint res = 0;
guint64 result;
guint64 start, stop, offset;
gdouble abs_rate;
if (G_UNLIKELY (position == -1)) {
GST_DEBUG ("invalid position (-1)");
res = GST_SEGMENT_RESULT_INVALID;
goto done;
}
g_return_val_if_fail (segment != NULL, GST_SEGMENT_RESULT_INVALID);
g_return_val_if_fail (segment->format == format, GST_SEGMENT_RESULT_INVALID);
start = segment->start;
/* before the segment boundary */
if (clip && G_UNLIKELY (position < start)) {
GST_DEBUG ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
")", position, start);
if (G_LIKELY (segment->rate > 0.0))
res = GST_SEGMENT_RESULT_BEFORE;
else
res = GST_SEGMENT_RESULT_AFTER;
goto done;
}
stop = segment->stop;
/* after the segment boundary */
if (clip && G_UNLIKELY (stop != -1 && position > stop)) {
GST_DEBUG ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
")", position, stop);
if (G_LIKELY (segment->rate > 0.0))
res = GST_SEGMENT_RESULT_AFTER;
else
res = GST_SEGMENT_RESULT_BEFORE;
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 += offset;
start = segment->start + offset;
/* bring to uncorrected position in segment */
if (position < start) {
/* negative value */
result = start - position;
res = GST_SEGMENT_RESULT_NEGATIVE;
res = -1;
} else {
result = position - start;
res = GST_SEGMENT_RESULT_OK;
res = 1;
}
} else {
stop = segment->stop;
/* cannot continue if no stop position set or invalid offset */
g_return_val_if_fail (stop != -1, GST_SEGMENT_RESULT_INVALID);
g_return_val_if_fail (stop >= segment->offset, GST_SEGMENT_RESULT_INVALID);
g_return_val_if_fail (stop != -1, 0);
g_return_val_if_fail (stop >= offset, 0);
stop -= offset;
@ -553,10 +523,10 @@ gst_segment_to_running_time_full (const GstSegment * segment, GstFormat format,
if (position > stop) {
/* negative value */
result = position - stop;
res = GST_SEGMENT_RESULT_NEGATIVE;
res = -1;
} else {
result = stop - position;
res = GST_SEGMENT_RESULT_OK;
res = 1;
}
}
@ -568,14 +538,14 @@ gst_segment_to_running_time_full (const GstSegment * segment, GstFormat format,
result /= abs_rate;
/* correct for base of the segment */
if (res == GST_SEGMENT_RESULT_OK)
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 = GST_SEGMENT_RESULT_OK;
res = 1;
} else {
/* negative and base is smaller, subtract base and remainder is
* negative */
@ -588,7 +558,7 @@ done:
{
if (running_time)
*running_time = -1;
return res;
return 0;
}
}
@ -616,12 +586,25 @@ gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
guint64 position)
{
guint64 result;
GstSegmentResult res;
res =
gst_segment_to_running_time_full (segment, format, position, TRUE,
&result);
if (res == GST_SEGMENT_RESULT_OK)
g_return_val_if_fail (segment != NULL, -1);
g_return_val_if_fail (segment->format == format, -1);
/* 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 (gst_segment_to_running_time_full (segment, format, position,
&result) == 1)
return result;
return -1;

View file

@ -168,27 +168,6 @@ typedef enum { /*< flags >*/
GST_SEGMENT_FLAG_TRICKMODE_NO_AUDIO = GST_SEEK_FLAG_TRICKMODE_NO_AUDIO
} GstSegmentFlags;
/**
* GstSegmentResult:
* @GST_SEGMENT_RESULT_INVALID: input value is invalid and no output value can
* be calculated.
* @GST_SEGMENT_RESULT_OK: value is within the segment and positive.
* @GST_SEGMENT_RESULT_NEGATIVE: value is within the segment but negative.
* @GST_SEGMENT_RESULT_BEFORE: value is outside before the segment.
* @GST_SEGMENT_RESULT_AFTER: value is outside after the segment.
*
* Possible return values from gst_segment_to_running_time_full().
*
* Since: 1.6
*/
typedef enum {
GST_SEGMENT_RESULT_INVALID = 0,
GST_SEGMENT_RESULT_OK = 1,
GST_SEGMENT_RESULT_NEGATIVE = -1,
GST_SEGMENT_RESULT_BEFORE = -2,
GST_SEGMENT_RESULT_AFTER = -3
} GstSegmentResult;
/**
* GstSegment:
* @flags: flags for this segment
@ -238,9 +217,9 @@ void gst_segment_init (GstSegment *segment, GstFormat for
guint64 gst_segment_to_stream_time (const GstSegment *segment, GstFormat format, guint64 position);
guint64 gst_segment_to_running_time (const GstSegment *segment, GstFormat format, guint64 position);
GstSegmentResult
gst_segment_to_running_time_full (const GstSegment *segment, GstFormat format, guint64 position,
gboolean clip, guint64 * running_time);
gint gst_segment_to_running_time_full (const GstSegment *segment, GstFormat format, guint64 position,
guint64 * running_time);
guint64 gst_segment_to_position (const GstSegment *segment, GstFormat format, guint64 running_time);
gboolean gst_segment_set_running_time (GstSegment *segment, GstFormat format, guint64 running_time);

View file

@ -762,24 +762,30 @@ GST_START_TEST (segment_full)
check_times (&segment, 220, -1, -1);
fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME,
50, TRUE, &rt) == GST_SEGMENT_RESULT_OK);
50, &rt) == 1);
fail_unless (rt == 0);
fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME,
200, TRUE, &rt) == GST_SEGMENT_RESULT_OK);
200, &rt) == 1);
fail_unless (rt == 150);
fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME,
40, TRUE, &rt) == GST_SEGMENT_RESULT_BEFORE);
fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME,
49, TRUE, &rt) == GST_SEGMENT_RESULT_BEFORE);
fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME,
201, TRUE, &rt) == GST_SEGMENT_RESULT_AFTER);
fail_unless (!gst_segment_clip (&segment, GST_FORMAT_TIME, 40, 40, NULL,
NULL));
fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME, 40,
&rt) == -1);
fail_unless (!gst_segment_clip (&segment, GST_FORMAT_TIME, 49, 49, NULL,
NULL));
fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME, 49,
&rt) == -1);
fail_unless (!gst_segment_clip (&segment, GST_FORMAT_TIME, 201, 201, NULL,
NULL));
fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME, 201,
&rt) == 1);
fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
-50) == TRUE);
fail_unless (segment.offset == 50);
fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME,
50, TRUE, &rt) == GST_SEGMENT_RESULT_NEGATIVE);
50, &rt) == -1);
GST_DEBUG ("%" G_GUINT64_FORMAT, rt);
fail_unless (rt == 50);
}

View file

@ -1124,7 +1124,6 @@ EXPORTS
gst_segment_init
gst_segment_new
gst_segment_offset_running_time
gst_segment_result_get_type
gst_segment_set_running_time
gst_segment_to_position
gst_segment_to_running_time