audio: Add reverse playback support to GstAudioStreamAlign

https://bugzilla.gnome.org/show_bug.cgi?id=787560
This commit is contained in:
Sebastian Dröge 2017-09-12 16:03:44 +03:00
parent ec1e20ffe5
commit d2fd740388
2 changed files with 236 additions and 22 deletions

View file

@ -64,6 +64,7 @@ struct _GstAudioStreamAlign
* Allocate a new #GstAudioStreamAlign with the given configuration. All * Allocate a new #GstAudioStreamAlign with the given configuration. All
* processing happens according to sample rate @rate, until * processing happens according to sample rate @rate, until
* gst_audio_discont_wait_set_rate() is called with a new @rate. * gst_audio_discont_wait_set_rate() is called with a new @rate.
* A negative rate can be used for reverse playback.
* *
* @alignment_threshold gives the tolerance in nanoseconds after which a * @alignment_threshold gives the tolerance in nanoseconds after which a
* timestamp difference is considered a discontinuity. Once detected, * timestamp difference is considered a discontinuity. Once detected,
@ -82,7 +83,7 @@ gst_audio_stream_align_new (gint rate, GstClockTime alignment_threshold,
{ {
GstAudioStreamAlign *align; GstAudioStreamAlign *align;
g_return_val_if_fail (rate > 0, NULL); g_return_val_if_fail (rate != 0, NULL);
align = g_new0 (GstAudioStreamAlign, 1); align = g_new0 (GstAudioStreamAlign, 1);
align->rate = rate; align->rate = rate;
@ -147,7 +148,7 @@ void
gst_audio_stream_align_set_rate (GstAudioStreamAlign * align, gint rate) gst_audio_stream_align_set_rate (GstAudioStreamAlign * align, gint rate)
{ {
g_return_if_fail (align != NULL); g_return_if_fail (align != NULL);
g_return_if_fail (rate > 0); g_return_if_fail (rate != 0);
if (align->rate == rate) if (align->rate == rate)
return; return;
@ -283,10 +284,18 @@ gst_audio_stream_align_mark_discont (GstAudioStreamAlign * align)
* discontinuity differs by more than the alignment threshold for a duration * discontinuity differs by more than the alignment threshold for a duration
* longer than discont wait. * longer than discont wait.
* *
* Note: In reverse playback, every buffer is considered discontinuous in the
* context of buffer flags because the last sample of the previous buffer is
* discontinuous with the first sample of the current one. However for this
* function they are only considered discontinuous in reverse playback if the
* first sample of the previous buffer is discontinuous with the last sample
* of the current one.
*
* Returns: %TRUE if a discontinuity was detected, %FALSE otherwise. * Returns: %TRUE if a discontinuity was detected, %FALSE otherwise.
* *
* Since: 1.14 * Since: 1.14
*/ */
#define ABSDIFF(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
gboolean gboolean
gst_audio_stream_align_process (GstAudioStreamAlign * align, gst_audio_stream_align_process (GstAudioStreamAlign * align,
gboolean discont, GstClockTime timestamp, guint n_samples, gboolean discont, GstClockTime timestamp, guint n_samples,
@ -299,10 +308,12 @@ gst_audio_stream_align_process (GstAudioStreamAlign * align,
g_return_val_if_fail (align != NULL, FALSE); g_return_val_if_fail (align != NULL, FALSE);
start_time = timestamp; start_time = timestamp;
start_offset = gst_util_uint64_scale (start_time, align->rate, GST_SECOND); start_offset =
gst_util_uint64_scale (start_time, ABS (align->rate), GST_SECOND);
end_offset = start_offset + n_samples; end_offset = start_offset + n_samples;
end_time = gst_util_uint64_scale_int (end_offset, GST_SECOND, align->rate); end_time =
gst_util_uint64_scale_int (end_offset, GST_SECOND, ABS (align->rate));
duration = end_time - start_time; duration = end_time - start_time;
@ -312,24 +323,27 @@ gst_audio_stream_align_process (GstAudioStreamAlign * align,
guint64 diff, max_sample_diff; guint64 diff, max_sample_diff;
/* Check discont */ /* Check discont */
if (start_offset <= align->next_offset) if (align->rate > 0) {
diff = align->next_offset - start_offset; diff = ABSDIFF (start_offset, align->next_offset);
else } else {
diff = start_offset - align->next_offset; diff = ABSDIFF (end_offset, align->next_offset);
}
max_sample_diff = max_sample_diff =
gst_util_uint64_scale_int (align->alignment_threshold, gst_util_uint64_scale_int (align->alignment_threshold,
align->rate, GST_SECOND); ABS (align->rate), GST_SECOND);
/* Discont! */ /* Discont! */
if (G_UNLIKELY (diff >= max_sample_diff)) { if (G_UNLIKELY (diff >= max_sample_diff)) {
if (align->discont_wait > 0) { if (align->discont_wait > 0) {
if (align->discont_time == GST_CLOCK_TIME_NONE) { if (align->discont_time == GST_CLOCK_TIME_NONE) {
align->discont_time = start_time; align->discont_time = align->rate > 0 ? start_time : end_time;
} else if ((start_time >= align->discont_time } else if ((align->rate > 0
&& start_time - align->discont_time >= align->discont_wait) && ABSDIFF (start_time,
|| (start_time < align->discont_time align->discont_time) >= align->discont_wait)
&& align->discont_time - start_time >= align->discont_wait)) { || (align->rate < 0
&& ABSDIFF (end_time,
align->discont_time) >= align->discont_wait)) {
discont = TRUE; discont = TRUE;
align->discont_time = GST_CLOCK_TIME_NONE; align->discont_time = GST_CLOCK_TIME_NONE;
} }
@ -348,22 +362,41 @@ gst_audio_stream_align_process (GstAudioStreamAlign * align,
GST_INFO ("Have discont. Expected %" GST_INFO ("Have discont. Expected %"
G_GUINT64_FORMAT ", got %" G_GUINT64_FORMAT, G_GUINT64_FORMAT ", got %" G_GUINT64_FORMAT,
align->next_offset, start_offset); align->next_offset, start_offset);
align->next_offset = end_offset; align->next_offset = align->rate > 0 ? end_offset : start_offset;
/* Got a discont and adjusted, reset the discont_time marker */ /* Got a discont and adjusted, reset the discont_time marker */
align->discont_time = GST_CLOCK_TIME_NONE; align->discont_time = GST_CLOCK_TIME_NONE;
} else { } else {
/* No discont, just keep counting */ /* No discont, just keep counting */
if (align->rate > 0) {
timestamp = timestamp =
gst_util_uint64_scale (align->next_offset, GST_SECOND, align->rate); gst_util_uint64_scale (align->next_offset, GST_SECOND,
ABS (align->rate));
start_offset = align->next_offset; start_offset = align->next_offset;
align->next_offset += n_samples; align->next_offset += n_samples;
duration = duration =
gst_util_uint64_scale (align->next_offset, GST_SECOND, gst_util_uint64_scale (align->next_offset, GST_SECOND,
align->rate) - timestamp; ABS (align->rate)) - timestamp;
} else {
guint64 old_offset = align->next_offset;
if (align->next_offset > n_samples)
align->next_offset -= n_samples;
else
align->next_offset = 0;
start_offset = align->next_offset;
timestamp =
gst_util_uint64_scale (align->next_offset, GST_SECOND,
ABS (align->rate));
duration =
gst_util_uint64_scale (old_offset, GST_SECOND,
ABS (align->rate)) - timestamp;
}
} }
if (out_timestamp) if (out_timestamp)
@ -375,3 +408,5 @@ gst_audio_stream_align_process (GstAudioStreamAlign * align,
return discont; return discont;
} }
#undef ABSDIFF

View file

@ -883,6 +883,184 @@ GST_START_TEST (test_stream_align)
GST_END_TEST; GST_END_TEST;
GST_START_TEST (test_stream_align_reverse)
{
GstAudioStreamAlign *align;
gint i;
GstClockTime timestamp;
GstClockTime out_timestamp, out_duration;
gboolean discont;
align = gst_audio_stream_align_new (-1000);
for (i = 499; i >= 0; i--) {
timestamp = 10 * GST_MSECOND * i;
discont = i == 499;
discont =
gst_audio_stream_align_process (align, discont, timestamp, 10,
&out_timestamp, &out_duration, NULL);
fail_unless_equals_uint64 (out_timestamp, 10 * GST_MSECOND * i);
fail_unless_equals_uint64 (out_duration, 10 * GST_MSECOND);
if (i == 499)
fail_unless (discont);
else
fail_unless (!discont);
}
/* Drift forwards by 1ms per 10ms buffer for the first 40 buffers.
* - after 40 buffers we're above alignment threshold
* - after 40 + 100 buffers we're at discont wait
*/
for (i = 499; i >= 0; i--) {
timestamp = 10 * GST_MSECOND * i;
discont = i == 499;
if (i < 499)
timestamp += 1 * GST_MSECOND * MIN (499 - i, 40);
discont =
gst_audio_stream_align_process (align, discont, timestamp, 10,
&out_timestamp, &out_duration, NULL);
if (i >= 500 - 140) {
fail_unless_equals_uint64 (out_timestamp, 10 * GST_MSECOND * i);
fail_unless_equals_uint64 (out_duration, 10 * GST_MSECOND);
if (i == 499)
fail_unless (discont);
else
fail_unless (!discont);
} else {
if (i == 499 - 140)
fail_unless (discont);
else
fail_unless (!discont);
fail_unless_equals_uint64 (out_timestamp,
10 * GST_MSECOND * i + 40 * GST_MSECOND);
fail_unless_equals_uint64 (out_duration, 10 * GST_MSECOND);
}
}
/* Drift backwards by 1ms per 10ms buffer for the first 40 buffers.
* - after 40 buffers we're above alignment threshold
* - after 40 + 100 buffers we're at discont wait
*/
for (i = 499; i >= 4; i--) {
timestamp = 10 * GST_MSECOND * i;
discont = i == 499;
if (i < 499)
timestamp -= 1 * GST_MSECOND * MIN (499 - i, 40);
discont =
gst_audio_stream_align_process (align, discont, timestamp, 10,
&out_timestamp, &out_duration, NULL);
if (i >= 500 - 140) {
fail_unless_equals_uint64 (out_timestamp, 10 * GST_MSECOND * i);
fail_unless_equals_uint64 (out_duration, 10 * GST_MSECOND);
if (i == 499)
fail_unless (discont);
else
fail_unless (!discont);
} else {
if (i == 499 - 140)
fail_unless (discont);
else
fail_unless (!discont);
fail_unless_equals_uint64 (out_timestamp,
10 * GST_MSECOND * i - 40 * GST_MSECOND);
fail_unless_equals_uint64 (out_duration, 10 * GST_MSECOND);
}
}
/* Shift all buffers but the first by 40ms
* - after 1 buffers we're above alignment threshold
* - after 101 buffers we're at discont wait
*/
for (i = 499; i >= 0; i--) {
timestamp = 10 * GST_MSECOND * i;
discont = i == 499;
if (i < 499)
timestamp += 40 * GST_MSECOND;
discont =
gst_audio_stream_align_process (align, discont, timestamp, 10,
&out_timestamp, &out_duration, NULL);
if (i >= 500 - 101) {
fail_unless_equals_uint64 (out_timestamp, 10 * GST_MSECOND * i);
fail_unless_equals_uint64 (out_duration, 10 * GST_MSECOND);
if (i == 499)
fail_unless (discont);
else
fail_unless (!discont);
} else {
if (i == 499 - 101)
fail_unless (discont);
else
fail_unless (!discont);
fail_unless_equals_uint64 (out_timestamp,
10 * GST_MSECOND * i + 40 * GST_MSECOND);
fail_unless_equals_uint64 (out_duration, 10 * GST_MSECOND);
}
}
/* Shift every second buffer by 40ms:
* - never discont!
*/
for (i = 499; i >= 0; i--) {
timestamp = 10 * GST_MSECOND * i;
discont = i == 499;
if (i % 2 == 0 && i < 499)
timestamp += 40 * GST_MSECOND;
discont =
gst_audio_stream_align_process (align, discont, timestamp, 10,
&out_timestamp, &out_duration, NULL);
fail_unless_equals_uint64 (out_timestamp, 10 * GST_MSECOND * i);
fail_unless_equals_uint64 (out_duration, 10 * GST_MSECOND);
if (i == 499)
fail_unless (discont);
else
fail_unless (!discont);
}
/* Shift buffer 100 by 2: discont at buffer 200
*/
for (i = 499; i >= 0; i--) {
timestamp = 10 * GST_MSECOND * i;
discont = i == 499;
if (i < 500 - 100)
timestamp += 2 * GST_SECOND;
discont =
gst_audio_stream_align_process (align, discont, timestamp, 10,
&out_timestamp, &out_duration, NULL);
if (i >= 500 - 200) {
fail_unless_equals_uint64 (out_timestamp, 10 * GST_MSECOND * i);
fail_unless_equals_uint64 (out_duration, 10 * GST_MSECOND);
if (i == 499)
fail_unless (discont);
else
fail_unless (!discont);
} else {
fail_unless_equals_uint64 (out_timestamp,
10 * GST_MSECOND * i + 2 * GST_SECOND);
fail_unless_equals_uint64 (out_duration, 10 * GST_MSECOND);
if (i == 499 - 200)
fail_unless (discont);
else
fail_unless (!discont);
}
}
}
GST_END_TEST;
static Suite * static Suite *
audio_suite (void) audio_suite (void)
{ {
@ -911,6 +1089,7 @@ audio_suite (void)
tcase_add_test (tc_chain, test_audio_format_u8); tcase_add_test (tc_chain, test_audio_format_u8);
tcase_add_test (tc_chain, test_fill_silence); tcase_add_test (tc_chain, test_fill_silence);
tcase_add_test (tc_chain, test_stream_align); tcase_add_test (tc_chain, test_stream_align);
tcase_add_test (tc_chain, test_stream_align_reverse);
return s; return s;
} }