From a135868262f183eecd1957c0053da75cebb956bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 3 Nov 2015 10:58:35 +0200 Subject: [PATCH] oggdemux: Add GstAudioClippingMeta for Opus for accurate start/end clipping https://bugzilla.gnome.org/show_bug.cgi?id=757153 --- ext/ogg/Makefile.am | 1 + ext/ogg/gstoggdemux.c | 30 ++++++++++++++++++++++++++++++ ext/ogg/gstoggstream.c | 1 + ext/ogg/gstoggstream.h | 1 + 4 files changed, 33 insertions(+) diff --git a/ext/ogg/Makefile.am b/ext/ogg/Makefile.am index 1128141c29..05d9063a8c 100644 --- a/ext/ogg/Makefile.am +++ b/ext/ogg/Makefile.am @@ -30,6 +30,7 @@ libgstogg_la_LIBADD = \ $(top_builddir)/gst-libs/gst/riff/libgstriff-$(GST_API_VERSION).la \ $(top_builddir)/gst-libs/gst/tag/libgsttag-$(GST_API_VERSION).la \ $(top_builddir)/gst-libs/gst/pbutils/libgstpbutils-$(GST_API_VERSION).la \ + $(top_builddir)/gst-libs/gst/audio/libgstaudio-$(GST_API_VERSION).la \ $(GST_BASE_LIBS) \ $(GST_LIBS) \ $(OGG_LIBS) diff --git a/ext/ogg/gstoggdemux.c b/ext/ogg/gstoggdemux.c index 165420c80e..05cd759770 100644 --- a/ext/ogg/gstoggdemux.c +++ b/ext/ogg/gstoggdemux.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "gstoggdemux.h" @@ -500,6 +501,7 @@ gst_ogg_demux_chain_peer (GstOggPad * pad, ogg_packet * packet, guint64 out_offset, out_offset_end; gboolean delta_unit = FALSE; gboolean is_header; + guint64 clip_start = 0, clip_end = 0; ret = cret = GST_FLOW_OK; GST_DEBUG_OBJECT (pad, "Chaining %d %d %" GST_TIME_FORMAT " %d %p", @@ -723,9 +725,29 @@ gst_ogg_demux_chain_peer (GstOggPad * pad, ogg_packet * packet, pad->prev_granule); else out_timestamp = 0; + + if (pad->map.audio_clipping + && pad->current_granule < pad->prev_granule + duration) { + clip_end = pad->prev_granule + duration - pad->current_granule; + } + if (pad->map.audio_clipping + && pad->current_granule - duration < -pad->map.granule_offset) { + if (pad->current_granule >= -pad->map.granule_offset) + clip_start = -pad->map.granule_offset; + else + clip_start = pad->current_granule; + } } else { out_timestamp = gst_ogg_stream_granule_to_time (&pad->map, pad->current_granule - duration); + + if (pad->map.audio_clipping + && pad->current_granule - duration < -pad->map.granule_offset) { + if (pad->current_granule >= -pad->map.granule_offset) + clip_start = -pad->map.granule_offset; + else + clip_start = pad->current_granule; + } } out_duration = gst_ogg_stream_granule_to_time (&pad->map, @@ -751,6 +773,14 @@ gst_ogg_demux_chain_peer (GstOggPad * pad, ogg_packet * packet, buf = gst_buffer_new_and_alloc (packet->bytes - offset - trim); + if (pad->map.audio_clipping && (clip_start || clip_end)) { + GST_DEBUG_OBJECT (pad, + "Adding audio clipping %" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT, + clip_start, clip_end); + gst_buffer_add_audio_clipping_meta (buf, GST_FORMAT_DEFAULT, clip_start, + clip_end); + } + /* set delta flag for OGM content */ if (delta_unit) GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT); diff --git a/ext/ogg/gstoggstream.c b/ext/ogg/gstoggstream.c index 241c9fffb8..d22a377f9e 100644 --- a/ext/ogg/gstoggstream.c +++ b/ext/ogg/gstoggstream.c @@ -2000,6 +2000,7 @@ setup_opus_mapper (GstOggStream * pad, ogg_packet * packet) pad->granuleshift = 0; pad->n_header_packets = 2; pad->first_granpos = -1; + pad->audio_clipping = TRUE; /* pre-skip is in samples at 48000 Hz, which matches granule one for one */ pad->granule_offset = -GST_READ_UINT16_LE (packet->packet + 10); diff --git a/ext/ogg/gstoggstream.h b/ext/ogg/gstoggstream.h index 26dc8e0610..d49527ff2e 100644 --- a/ext/ogg/gstoggstream.h +++ b/ext/ogg/gstoggstream.h @@ -70,6 +70,7 @@ struct _GstOggStream gint bitrate; guint64 total_time; gboolean is_sparse; + gboolean audio_clipping; GstCaps *caps;