opus: port to base audio encoder/decoder

This commit is contained in:
Vincent Penquerc'h 2011-11-16 16:56:43 +00:00
parent e500ec524c
commit da1eaa2d78
5 changed files with 523 additions and 1300 deletions

View file

@ -2,10 +2,12 @@ plugin_LTLIBRARIES = libgstopus.la
libgstopus_la_SOURCES = gstopus.c gstopusdec.c gstopusenc.c
libgstopus_la_CFLAGS = \
-DGST_USE_UNSTABLE_API \
$(GST_PLUGINS_BASE_CFLAGS) \
$(GST_CFLAGS) \
$(OPUS_CFLAGS)
libgstopus_la_LIBADD = \
-lgstaudio-$(GST_MAJORMINOR) \
$(GST_PLUGINS_BASE_LIBS) -lgsttag-$(GST_MAJORMINOR) \
$(GST_BASE_LIBS) \
$(GST_LIBS) \

View file

@ -68,31 +68,17 @@ GST_STATIC_PAD_TEMPLATE ("sink",
GST_STATIC_CAPS ("audio/x-opus")
);
GST_BOILERPLATE (GstOpusDec, gst_opus_dec, GstElement, GST_TYPE_ELEMENT);
GST_BOILERPLATE (GstOpusDec, gst_opus_dec, GstAudioDecoder,
GST_TYPE_AUDIO_DECODER);
static gboolean opus_dec_sink_event (GstPad * pad, GstEvent * event);
static GstFlowReturn opus_dec_chain (GstPad * pad, GstBuffer * buf);
static gboolean opus_dec_sink_setcaps (GstPad * pad, GstCaps * caps);
static GstStateChangeReturn opus_dec_change_state (GstElement * element,
GstStateChange transition);
static gboolean opus_dec_src_event (GstPad * pad, GstEvent * event);
static gboolean opus_dec_src_query (GstPad * pad, GstQuery * query);
static gboolean opus_dec_sink_query (GstPad * pad, GstQuery * query);
static const GstQueryType *opus_get_src_query_types (GstPad * pad);
static const GstQueryType *opus_get_sink_query_types (GstPad * pad);
static gboolean opus_dec_convert (GstPad * pad,
GstFormat src_format, gint64 src_value,
GstFormat * dest_format, gint64 * dest_value);
static GstFlowReturn opus_dec_chain_parse_data (GstOpusDec * dec,
GstBuffer * buf, GstClockTime timestamp, GstClockTime duration);
static GstFlowReturn opus_dec_chain_parse_header (GstOpusDec * dec,
static GstFlowReturn gst_opus_dec_parse_header (GstOpusDec * dec,
GstBuffer * buf);
#if 0
static GstFlowReturn opus_dec_chain_parse_comments (GstOpusDec * dec,
GstBuffer * buf);
#endif
static gboolean gst_opus_dec_start (GstAudioDecoder * dec);
static gboolean gst_opus_dec_stop (GstAudioDecoder * dec);
static GstFlowReturn gst_opus_dec_handle_frame (GstAudioDecoder * dec,
GstBuffer * buffer);
static gboolean gst_opus_dec_set_format (GstAudioDecoder * bdec,
GstCaps * caps);
static void
gst_opus_dec_base_init (gpointer g_class)
@ -112,11 +98,16 @@ gst_opus_dec_base_init (gpointer g_class)
static void
gst_opus_dec_class_init (GstOpusDecClass * klass)
{
GstAudioDecoderClass *adclass;
GstElementClass *gstelement_class;
adclass = (GstAudioDecoderClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstelement_class->change_state = GST_DEBUG_FUNCPTR (opus_dec_change_state);
adclass->start = GST_DEBUG_FUNCPTR (gst_opus_dec_start);
adclass->stop = GST_DEBUG_FUNCPTR (gst_opus_dec_stop);
adclass->handle_frame = GST_DEBUG_FUNCPTR (gst_opus_dec_handle_frame);
adclass->set_format = GST_DEBUG_FUNCPTR (gst_opus_dec_set_format);
GST_DEBUG_CATEGORY_INIT (opusdec_debug, "opusdec", 0,
"opus decoding element");
@ -125,8 +116,6 @@ gst_opus_dec_class_init (GstOpusDecClass * klass)
static void
gst_opus_dec_reset (GstOpusDec * dec)
{
gst_segment_init (&dec->segment, GST_FORMAT_UNDEFINED);
dec->granulepos = -1;
dec->packetno = 0;
dec->frame_size = 0;
dec->frame_samples = 960;
@ -135,50 +124,14 @@ gst_opus_dec_reset (GstOpusDec * dec)
opus_decoder_destroy (dec->state);
dec->state = NULL;
}
#if 0
if (dec->mode) {
opus_mode_destroy (dec->mode);
dec->mode = NULL;
}
#endif
gst_buffer_replace (&dec->streamheader, NULL);
gst_buffer_replace (&dec->vorbiscomment, NULL);
g_list_foreach (dec->extra_headers, (GFunc) gst_mini_object_unref, NULL);
g_list_free (dec->extra_headers);
dec->extra_headers = NULL;
#if 0
memset (&dec->header, 0, sizeof (dec->header));
#endif
}
static void
gst_opus_dec_init (GstOpusDec * dec, GstOpusDecClass * g_class)
{
dec->sinkpad =
gst_pad_new_from_static_template (&opus_dec_sink_factory, "sink");
gst_pad_set_chain_function (dec->sinkpad, GST_DEBUG_FUNCPTR (opus_dec_chain));
gst_pad_set_event_function (dec->sinkpad,
GST_DEBUG_FUNCPTR (opus_dec_sink_event));
gst_pad_set_query_type_function (dec->sinkpad,
GST_DEBUG_FUNCPTR (opus_get_sink_query_types));
gst_pad_set_query_function (dec->sinkpad,
GST_DEBUG_FUNCPTR (opus_dec_sink_query));
gst_pad_set_setcaps_function (dec->sinkpad,
GST_DEBUG_FUNCPTR (opus_dec_sink_setcaps));
gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
dec->srcpad = gst_pad_new_from_static_template (&opus_dec_src_factory, "src");
gst_pad_use_fixed_caps (dec->srcpad);
gst_pad_set_event_function (dec->srcpad,
GST_DEBUG_FUNCPTR (opus_dec_src_event));
gst_pad_set_query_type_function (dec->srcpad,
GST_DEBUG_FUNCPTR (opus_get_src_query_types));
gst_pad_set_query_function (dec->srcpad,
GST_DEBUG_FUNCPTR (opus_dec_src_query));
gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
dec->sample_rate = 48000;
dec->n_channels = 2;
@ -186,532 +139,39 @@ gst_opus_dec_init (GstOpusDec * dec, GstOpusDecClass * g_class)
}
static gboolean
opus_dec_sink_setcaps (GstPad * pad, GstCaps * caps)
gst_opus_dec_start (GstAudioDecoder * dec)
{
GstOpusDec *dec = GST_OPUS_DEC (gst_pad_get_parent (pad));
gboolean ret = TRUE;
GstStructure *s;
const GValue *streamheader;
GstOpusDec *odec = GST_OPUS_DEC (dec);
GST_DEBUG_OBJECT (pad, "Setting sink caps to %" GST_PTR_FORMAT, caps);
gst_opus_dec_reset (odec);
s = gst_caps_get_structure (caps, 0);
if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
gst_value_array_get_size (streamheader) >= 2) {
const GValue *header;
GstBuffer *buf;
GstFlowReturn res = GST_FLOW_OK;
/* we know about concealment */
gst_audio_decoder_set_plc_aware (dec, TRUE);
header = gst_value_array_get_value (streamheader, 0);
if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
buf = gst_value_get_buffer (header);
res = opus_dec_chain_parse_header (dec, buf);
if (res != GST_FLOW_OK)
goto done;
gst_buffer_replace (&dec->streamheader, buf);
}
#if 0
vorbiscomment = gst_value_array_get_value (streamheader, 1);
if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
buf = gst_value_get_buffer (vorbiscomment);
res = opus_dec_chain_parse_comments (dec, buf);
if (res != GST_FLOW_OK)
goto done;
gst_buffer_replace (&dec->vorbiscomment, buf);
}
#endif
g_list_foreach (dec->extra_headers, (GFunc) gst_mini_object_unref, NULL);
g_list_free (dec->extra_headers);
dec->extra_headers = NULL;
if (gst_value_array_get_size (streamheader) > 2) {
gint i, n;
n = gst_value_array_get_size (streamheader);
for (i = 2; i < n; i++) {
header = gst_value_array_get_value (streamheader, i);
buf = gst_value_get_buffer (header);
dec->extra_headers =
g_list_prepend (dec->extra_headers, gst_buffer_ref (buf));
}
}
}
if (!gst_structure_get_int (s, "frame-size", &dec->frame_size)) {
GST_WARNING_OBJECT (dec, "Frame size not included in caps");
}
if (!gst_structure_get_int (s, "channels", &dec->n_channels)) {
GST_WARNING_OBJECT (dec, "Number of channels not included in caps");
}
if (!gst_structure_get_int (s, "rate", &dec->sample_rate)) {
GST_WARNING_OBJECT (dec, "Sample rate not included in caps");
}
switch (dec->frame_size) {
case 2:
dec->frame_samples = dec->sample_rate / 400;
break;
case 5:
dec->frame_samples = dec->sample_rate / 200;
break;
case 10:
dec->frame_samples = dec->sample_rate / 100;
break;
case 20:
dec->frame_samples = dec->sample_rate / 50;
break;
case 40:
dec->frame_samples = dec->sample_rate / 25;
break;
case 60:
dec->frame_samples = 3 * dec->sample_rate / 50;
break;
default:
GST_WARNING_OBJECT (dec, "Unsupported frame size: %d", dec->frame_size);
break;
}
dec->frame_duration = gst_util_uint64_scale_int (dec->frame_samples,
GST_SECOND, dec->sample_rate);
GST_INFO_OBJECT (dec,
"Got frame size %d, %d channels, %d Hz, giving %d samples per frame, frame duration %"
GST_TIME_FORMAT, dec->frame_size, dec->n_channels, dec->sample_rate,
dec->frame_samples, GST_TIME_ARGS (dec->frame_duration));
done:
gst_object_unref (dec);
return ret;
return TRUE;
}
static gboolean
opus_dec_convert (GstPad * pad,
GstFormat src_format, gint64 src_value,
GstFormat * dest_format, gint64 * dest_value)
gst_opus_dec_stop (GstAudioDecoder * dec)
{
gboolean res = TRUE;
GstOpusDec *dec;
guint64 scale = 1;
GstOpusDec *odec = GST_OPUS_DEC (dec);
dec = GST_OPUS_DEC (gst_pad_get_parent (pad));
gst_opus_dec_reset (odec);
if (dec->packetno < 1) {
res = FALSE;
goto cleanup;
}
if (src_format == *dest_format) {
*dest_value = src_value;
res = TRUE;
goto cleanup;
}
if (pad == dec->sinkpad &&
(src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES)) {
res = FALSE;
goto cleanup;
}
switch (src_format) {
case GST_FORMAT_TIME:
switch (*dest_format) {
case GST_FORMAT_BYTES:
scale = sizeof (gint16) * dec->n_channels;
case GST_FORMAT_DEFAULT:
*dest_value =
gst_util_uint64_scale_int (scale * src_value,
dec->sample_rate, GST_SECOND);
break;
default:
res = FALSE;
break;
}
break;
case GST_FORMAT_DEFAULT:
switch (*dest_format) {
case GST_FORMAT_BYTES:
*dest_value = src_value * sizeof (gint16) * dec->n_channels;
break;
case GST_FORMAT_TIME:
*dest_value =
gst_util_uint64_scale_int (src_value, GST_SECOND,
dec->sample_rate);
break;
default:
res = FALSE;
break;
}
break;
case GST_FORMAT_BYTES:
switch (*dest_format) {
case GST_FORMAT_DEFAULT:
*dest_value = src_value / (sizeof (gint16) * dec->n_channels);
break;
case GST_FORMAT_TIME:
*dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
dec->sample_rate * sizeof (gint16) * dec->n_channels);
break;
default:
res = FALSE;
break;
}
break;
default:
res = FALSE;
break;
}
cleanup:
gst_object_unref (dec);
return res;
}
static const GstQueryType *
opus_get_sink_query_types (GstPad * pad)
{
static const GstQueryType opus_dec_sink_query_types[] = {
GST_QUERY_CONVERT,
0
};
return opus_dec_sink_query_types;
}
static gboolean
opus_dec_sink_query (GstPad * pad, GstQuery * query)
{
GstOpusDec *dec;
gboolean res;
dec = GST_OPUS_DEC (gst_pad_get_parent (pad));
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONVERT:
{
GstFormat src_fmt, dest_fmt;
gint64 src_val, dest_val;
gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
res = opus_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val);
if (res) {
gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
}
break;
}
default:
res = gst_pad_query_default (pad, query);
break;
}
gst_object_unref (dec);
return res;
}
static const GstQueryType *
opus_get_src_query_types (GstPad * pad)
{
static const GstQueryType opus_dec_src_query_types[] = {
GST_QUERY_POSITION,
GST_QUERY_DURATION,
0
};
return opus_dec_src_query_types;
}
static gboolean
opus_dec_src_query (GstPad * pad, GstQuery * query)
{
GstOpusDec *dec;
gboolean res = FALSE;
dec = GST_OPUS_DEC (gst_pad_get_parent (pad));
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_POSITION:{
GstSegment segment;
GstFormat format;
gint64 cur;
gst_query_parse_position (query, &format, NULL);
GST_PAD_STREAM_LOCK (dec->sinkpad);
segment = dec->segment;
GST_PAD_STREAM_UNLOCK (dec->sinkpad);
if (segment.format != GST_FORMAT_TIME) {
GST_DEBUG_OBJECT (dec, "segment not initialised yet");
break;
}
if ((res = opus_dec_convert (dec->srcpad, GST_FORMAT_TIME,
segment.last_stop, &format, &cur))) {
gst_query_set_position (query, format, cur);
}
break;
}
case GST_QUERY_DURATION:{
GstFormat format = GST_FORMAT_TIME;
gint64 dur;
/* get duration from demuxer */
if (!gst_pad_query_peer_duration (dec->sinkpad, &format, &dur))
break;
gst_query_parse_duration (query, &format, NULL);
/* and convert it into the requested format */
if ((res = opus_dec_convert (dec->srcpad, GST_FORMAT_TIME,
dur, &format, &dur))) {
gst_query_set_duration (query, format, dur);
}
break;
}
default:
res = gst_pad_query_default (pad, query);
break;
}
gst_object_unref (dec);
return res;
}
static gboolean
opus_dec_src_event (GstPad * pad, GstEvent * event)
{
gboolean res = FALSE;
GstOpusDec *dec = GST_OPUS_DEC (gst_pad_get_parent (pad));
GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEEK:{
GstFormat format, tformat;
gdouble rate;
GstEvent *real_seek;
GstSeekFlags flags;
GstSeekType cur_type, stop_type;
gint64 cur, stop;
gint64 tcur, tstop;
gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
&stop_type, &stop);
/* we have to ask our peer to seek to time here as we know
* nothing about how to generate a granulepos from the src
* formats or anything.
*
* First bring the requested format to time
*/
tformat = GST_FORMAT_TIME;
if (!(res = opus_dec_convert (pad, format, cur, &tformat, &tcur)))
break;
if (!(res = opus_dec_convert (pad, format, stop, &tformat, &tstop)))
break;
/* then seek with time on the peer */
real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
flags, cur_type, tcur, stop_type, tstop);
GST_LOG_OBJECT (dec, "seek to %" GST_TIME_FORMAT, GST_TIME_ARGS (tcur));
res = gst_pad_push_event (dec->sinkpad, real_seek);
gst_event_unref (event);
break;
}
default:
res = gst_pad_event_default (pad, event);
break;
}
gst_object_unref (dec);
return res;
}
static gboolean
opus_dec_sink_event (GstPad * pad, GstEvent * event)
{
GstOpusDec *dec;
gboolean ret = FALSE;
dec = GST_OPUS_DEC (gst_pad_get_parent (pad));
GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_NEWSEGMENT:{
GstFormat format;
gdouble rate, arate;
gint64 start, stop, time;
gboolean update;
gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
&start, &stop, &time);
if (format != GST_FORMAT_TIME)
goto newseg_wrong_format;
if (rate <= 0.0)
goto newseg_wrong_rate;
if (update) {
/* time progressed without data, see if we can fill the gap with
* some concealment data */
if (dec->segment.last_stop < start) {
GstClockTime duration;
duration = start - dec->segment.last_stop;
opus_dec_chain_parse_data (dec, NULL, dec->segment.last_stop,
duration);
}
}
/* now configure the values */
gst_segment_set_newsegment_full (&dec->segment, update,
rate, arate, GST_FORMAT_TIME, start, stop, time);
dec->granulepos = -1;
GST_DEBUG_OBJECT (dec, "segment now: cur = %" GST_TIME_FORMAT " [%"
GST_TIME_FORMAT " - %" GST_TIME_FORMAT "]",
GST_TIME_ARGS (dec->segment.last_stop),
GST_TIME_ARGS (dec->segment.start),
GST_TIME_ARGS (dec->segment.stop));
ret = gst_pad_push_event (dec->srcpad, event);
break;
}
default:
ret = gst_pad_event_default (pad, event);
break;
}
gst_object_unref (dec);
return ret;
/* ERRORS */
newseg_wrong_format:
{
GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
gst_object_unref (dec);
return FALSE;
}
newseg_wrong_rate:
{
GST_DEBUG_OBJECT (dec, "negative rates not supported yet");
gst_object_unref (dec);
return FALSE;
}
return TRUE;
}
static GstFlowReturn
opus_dec_chain_parse_header (GstOpusDec * dec, GstBuffer * buf)
gst_opus_dec_parse_header (GstOpusDec * dec, GstBuffer * buf)
{
GstCaps *caps;
int err;
#if 0
dec->samples_per_frame = opus_packet_get_samples_per_frame (
(const unsigned char *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
#endif
#if 0
if (memcmp (dec->header.codec_id, "OPUS ", 8) != 0)
goto invalid_header;
#endif
dec->state = opus_decoder_create (dec->sample_rate, dec->n_channels, &err);
if (!dec->state || err != OPUS_OK)
goto init_failed;
dec->frame_duration = gst_util_uint64_scale_int (dec->frame_size,
GST_SECOND, dec->sample_rate);
/* set caps */
caps = gst_caps_new_simple ("audio/x-raw-int",
"rate", G_TYPE_INT, dec->sample_rate,
"channels", G_TYPE_INT, dec->n_channels,
"signed", G_TYPE_BOOLEAN, TRUE,
"endianness", G_TYPE_INT, G_BYTE_ORDER,
"width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
GST_DEBUG_OBJECT (dec, "rate=%d channels=%d frame-size=%d",
dec->sample_rate, dec->n_channels, dec->frame_size);
if (!gst_pad_set_caps (dec->srcpad, caps))
goto nego_failed;
gst_caps_unref (caps);
return GST_FLOW_OK;
/* ERRORS */
#if 0
invalid_header:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("Invalid header"));
return GST_FLOW_ERROR;
}
mode_init_failed:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("Mode initialization failed: %d", error));
return GST_FLOW_ERROR;
}
#endif
init_failed:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("couldn't initialize decoder"));
return GST_FLOW_ERROR;
}
nego_failed:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("couldn't negotiate format"));
gst_caps_unref (caps);
return GST_FLOW_NOT_NEGOTIATED;
}
}
#if 0
static GstFlowReturn
opus_dec_chain_parse_comments (GstOpusDec * dec, GstBuffer * buf)
gst_opus_dec_parse_comments (GstOpusDec * dec, GstBuffer * buf)
{
GstTagList *list;
gchar *encoder = NULL;
list = gst_tag_list_from_vorbiscomment_buffer (buf, NULL, 0, &encoder);
if (!list) {
GST_WARNING_OBJECT (dec, "couldn't decode comments");
list = gst_tag_list_new ();
}
if (encoder) {
gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
GST_TAG_ENCODER, encoder, NULL);
}
gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
GST_TAG_AUDIO_CODEC, "Opus", NULL);
if (dec->header.bytes_per_packet > 0) {
gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
GST_TAG_BITRATE, (guint) dec->header.bytes_per_packet * 8, NULL);
}
GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, list);
g_free (encoder);
g_free (ver);
return GST_FLOW_OK;
}
#endif
static GstFlowReturn
opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
@ -724,11 +184,6 @@ opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
gint16 *out_data;
int n, err;
if (timestamp != -1) {
dec->segment.last_stop = timestamp;
dec->granulepos = -1;
}
if (dec->state == NULL) {
GstCaps *caps;
@ -747,7 +202,7 @@ opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
GST_DEBUG_OBJECT (dec, "rate=%d channels=%d frame-size=%d",
dec->sample_rate, dec->n_channels, dec->frame_size);
if (!gst_pad_set_caps (dec->srcpad, caps))
if (!gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps))
GST_ERROR ("nego failure");
gst_caps_unref (caps);
@ -767,14 +222,15 @@ opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
size = 0;
}
GST_DEBUG ("frames %d", opus_packet_get_nb_frames (data, size));
GST_DEBUG ("bandwidth %d", opus_packet_get_bandwidth (data));
GST_DEBUG ("samples_per_frame %d", opus_packet_get_samples_per_frame (data,
48000));
GST_DEBUG ("channels %d", opus_packet_get_nb_channels (data));
res = gst_pad_alloc_buffer_and_set_caps (dec->srcpad,
res = gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec),
GST_BUFFER_OFFSET_NONE, dec->frame_samples * dec->n_channels * 2,
GST_PAD_CAPS (dec->srcpad), &outbuf);
GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
if (res != GST_FLOW_OK) {
GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
@ -783,7 +239,7 @@ opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
out_data = (gint16 *) GST_BUFFER_DATA (outbuf);
GST_LOG_OBJECT (dec, "decoding frame");
GST_LOG_OBJECT (dec, "decoding %d sample frame", dec->frame_samples);
n = opus_decode (dec->state, data, size, out_data, dec->frame_samples, 0);
if (n < 0) {
@ -792,8 +248,7 @@ opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
}
if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
timestamp = gst_util_uint64_scale_int (dec->granulepos - dec->frame_size,
GST_SECOND, dec->sample_rate);
GST_WARNING_OBJECT (dec, "No timestamp in -> no timestamp out");
}
GST_DEBUG_OBJECT (dec, "timestamp=%" GST_TIME_FORMAT,
@ -801,18 +256,12 @@ opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
if (dec->discont) {
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
dec->discont = 0;
}
dec->segment.last_stop += dec->frame_duration;
GST_LOG_OBJECT (dec, "pushing buffer with ts=%" GST_TIME_FORMAT ", dur=%"
GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
GST_TIME_ARGS (dec->frame_duration));
res = gst_pad_push (dec->srcpad, outbuf);
res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
if (res != GST_FLOW_OK)
GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
@ -824,63 +273,173 @@ creation_failed:
return GST_FLOW_ERROR;
}
static gint
gst_opus_dec_get_frame_samples (GstOpusDec * dec)
{
gint frame_samples = 0;
switch (dec->frame_size) {
case 2:
frame_samples = dec->sample_rate / 400;
break;
case 5:
frame_samples = dec->sample_rate / 200;
break;
case 10:
frame_samples = dec->sample_rate / 100;
break;
case 20:
frame_samples = dec->sample_rate / 50;
break;
case 40:
frame_samples = dec->sample_rate / 25;
break;
case 60:
frame_samples = 3 * dec->sample_rate / 50;
break;
default:
GST_WARNING_OBJECT (dec, "Unsupported frame size: %d", dec->frame_size);
frame_samples = 0;
break;
}
return frame_samples;
}
static gboolean
gst_opus_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
{
GstOpusDec *dec = GST_OPUS_DEC (bdec);
gboolean ret = TRUE;
GstStructure *s;
const GValue *streamheader;
GST_DEBUG_OBJECT (dec, "set_format: %" GST_PTR_FORMAT, caps);
s = gst_caps_get_structure (caps, 0);
if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
gst_value_array_get_size (streamheader) >= 2) {
const GValue *header, *vorbiscomment;
GstBuffer *buf;
GstFlowReturn res = GST_FLOW_OK;
header = gst_value_array_get_value (streamheader, 0);
if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
buf = gst_value_get_buffer (header);
res = gst_opus_dec_parse_header (dec, buf);
if (res != GST_FLOW_OK)
goto done;
gst_buffer_replace (&dec->streamheader, buf);
}
vorbiscomment = gst_value_array_get_value (streamheader, 1);
if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
buf = gst_value_get_buffer (vorbiscomment);
res = gst_opus_dec_parse_comments (dec, buf);
if (res != GST_FLOW_OK)
goto done;
gst_buffer_replace (&dec->vorbiscomment, buf);
}
}
if (!gst_structure_get_int (s, "frame-size", &dec->frame_size)) {
GST_WARNING_OBJECT (dec, "Frame size not included in caps");
}
if (!gst_structure_get_int (s, "channels", &dec->n_channels)) {
GST_WARNING_OBJECT (dec, "Number of channels not included in caps");
}
if (!gst_structure_get_int (s, "rate", &dec->sample_rate)) {
GST_WARNING_OBJECT (dec, "Sample rate not included in caps");
}
dec->frame_samples = gst_opus_dec_get_frame_samples (dec);
dec->frame_duration = gst_util_uint64_scale_int (dec->frame_samples,
GST_SECOND, dec->sample_rate);
GST_INFO_OBJECT (dec,
"Got frame size %d, %d channels, %d Hz, giving %d samples per frame, frame duration %"
GST_TIME_FORMAT, dec->frame_size, dec->n_channels, dec->sample_rate,
dec->frame_samples, GST_TIME_ARGS (dec->frame_duration));
caps = gst_caps_new_simple ("audio/x-raw-int",
"rate", G_TYPE_INT, dec->sample_rate,
"channels", G_TYPE_INT, dec->n_channels,
"signed", G_TYPE_BOOLEAN, TRUE,
"endianness", G_TYPE_INT, G_BYTE_ORDER,
"width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps);
gst_caps_unref (caps);
done:
return ret;
}
static gboolean
memcmp_buffers (GstBuffer * buf1, GstBuffer * buf2)
{
gsize size1, size2;
size1 = GST_BUFFER_SIZE (buf1);
size2 = GST_BUFFER_SIZE (buf2);
if (size1 != size2)
return FALSE;
return !memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2), size1);
}
static GstFlowReturn
opus_dec_chain (GstPad * pad, GstBuffer * buf)
gst_opus_dec_handle_frame (GstAudioDecoder * adec, GstBuffer * buf)
{
GstFlowReturn res;
GstOpusDec *dec;
dec = GST_OPUS_DEC (gst_pad_get_parent (pad));
GST_LOG_OBJECT (pad,
/* no fancy draining */
if (G_UNLIKELY (!buf))
return GST_FLOW_OK;
dec = GST_OPUS_DEC (adec);
GST_LOG_OBJECT (dec,
"Got buffer ts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
if (GST_BUFFER_IS_DISCONT (buf)) {
dec->discont = TRUE;
/* If we have the streamheader and vorbiscomment from the caps already
* ignore them here */
if (dec->streamheader && dec->vorbiscomment) {
if (memcmp_buffers (dec->streamheader, buf)) {
GST_DEBUG_OBJECT (dec, "found streamheader");
gst_audio_decoder_finish_frame (adec, NULL, 1);
res = GST_FLOW_OK;
} else if (memcmp_buffers (dec->vorbiscomment, buf)) {
GST_DEBUG_OBJECT (dec, "found vorbiscomments");
gst_audio_decoder_finish_frame (adec, NULL, 1);
res = GST_FLOW_OK;
} else {
res = opus_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
GST_BUFFER_DURATION (buf));
}
} else {
/* Otherwise fall back to packet counting and assume that the
* first two packets are the headers. */
switch (dec->packetno) {
case 0:
GST_DEBUG_OBJECT (dec, "counted streamheader");
res = gst_opus_dec_parse_header (dec, buf);
gst_audio_decoder_finish_frame (adec, NULL, 1);
break;
case 1:
GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
res = gst_opus_dec_parse_comments (dec, buf);
gst_audio_decoder_finish_frame (adec, NULL, 1);
break;
default:
{
res = opus_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
GST_BUFFER_DURATION (buf));
break;
}
}
}
res = opus_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
GST_BUFFER_DURATION (buf));
//done:
dec->packetno++;
gst_buffer_unref (buf);
gst_object_unref (dec);
return res;
}
static GstStateChangeReturn
opus_dec_change_state (GstElement * element, GstStateChange transition)
{
GstStateChangeReturn ret;
GstOpusDec *dec = GST_OPUS_DEC (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
case GST_STATE_CHANGE_READY_TO_PAUSED:
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
default:
break;
}
ret = parent_class->change_state (element, transition);
if (ret != GST_STATE_CHANGE_SUCCESS)
return ret;
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
gst_opus_dec_reset (dec);
break;
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
break;
}
return ret;
}

View file

@ -22,6 +22,7 @@
#define __GST_OPUS_DEC_H__
#include <gst/gst.h>
#include <gst/audio/gstaudiodecoder.h>
#include <opus/opus.h>
G_BEGIN_DECLS
@ -41,11 +42,7 @@ typedef struct _GstOpusDec GstOpusDec;
typedef struct _GstOpusDecClass GstOpusDecClass;
struct _GstOpusDec {
GstElement element;
/* pads */
GstPad *sinkpad;
GstPad *srcpad;
GstAudioDecoder element;
OpusDecoder *state;
int frame_samples;
@ -54,20 +51,15 @@ struct _GstOpusDec {
GstClockTime frame_duration;
guint64 packetno;
GstSegment segment; /* STREAM LOCK */
gint64 granulepos; /* -1 = needs to be set from current time */
gboolean discont;
GstBuffer *streamheader;
GstBuffer *vorbiscomment;
GList *extra_headers;
int sample_rate;
int n_channels;
};
struct _GstOpusDecClass {
GstElementClass parent_class;
GstAudioDecoderClass parent_class;
};
GType gst_opus_dec_get_type (void);

File diff suppressed because it is too large Load diff

View file

@ -24,7 +24,7 @@
#include <gst/gst.h>
#include <gst/base/gstadapter.h>
#include <gst/audio/gstaudioencoder.h>
#include <opus/opus.h>
@ -48,16 +48,9 @@ typedef struct _GstOpusEnc GstOpusEnc;
typedef struct _GstOpusEncClass GstOpusEncClass;
struct _GstOpusEnc {
GstElement element;
GstAudioEncoder element;
/* pads */
GstPad *sinkpad;
GstPad *srcpad;
//OpusHeader header;
//OpusMode *mode;
OpusEncoder *state;
GstAdapter *adapter;
/* properties */
gboolean audio_or_voip;
@ -71,28 +64,20 @@ struct _GstOpusEnc {
gboolean dtx;
gint packet_loss_percentage;
int frame_samples;
gint frame_samples;
gint n_channels;
gint sample_rate;
gboolean setup;
gboolean header_sent;
gboolean eos;
guint64 samples_in;
guint64 bytes_out;
GSList *headers;
guint64 frameno;
guint64 frameno_out;
GstClockTime start_ts;
GstClockTime next_ts;
guint64 granulepos_offset;
GstTagList *tags;
};
struct _GstOpusEncClass {
GstElementClass parent_class;
GstAudioEncoderClass parent_class;
/* signals */
void (*frame_encoded) (GstElement *element);