diff options
Diffstat (limited to 'ext/speex')
-rw-r--r-- | ext/speex/Makefile.am | 5 | ||||
-rw-r--r-- | ext/speex/gstspeexdec.c | 582 | ||||
-rw-r--r-- | ext/speex/gstspeexdec.h | 12 | ||||
-rw-r--r-- | ext/speex/gstspeexenc.c | 859 | ||||
-rw-r--r-- | ext/speex/gstspeexenc.h | 37 |
5 files changed, 328 insertions, 1167 deletions
diff --git a/ext/speex/Makefile.am b/ext/speex/Makefile.am index f2efb3614..fe5523796 100644 --- a/ext/speex/Makefile.am +++ b/ext/speex/Makefile.am @@ -1,13 +1,14 @@ plugin_LTLIBRARIES = libgstspeex.la libgstspeex_la_SOURCES = gstspeex.c gstspeexdec.c gstspeexenc.c -libgstspeex_la_CFLAGS = \ +libgstspeex_la_CFLAGS = -DGST_USE_UNSTABLE_API \ $(GST_PLUGINS_BASE_CFLAGS) \ $(GST_BASE_CFLAGS) \ $(GST_CFLAGS) \ $(SPEEX_CFLAGS) libgstspeex_la_LIBADD = \ - $(GST_PLUGINS_BASE_LIBS) -lgsttag-$(GST_MAJORMINOR) \ + $(GST_PLUGINS_BASE_LIBS) \ + -lgsttag-$(GST_MAJORMINOR) -lgstaudio-$(GST_MAJORMINOR) \ $(GST_BASE_LIBS) \ $(GST_LIBS) \ $(SPEEX_LIBS) diff --git a/ext/speex/gstspeexdec.c b/ext/speex/gstspeexdec.c index c5add8268..de4121436 100644 --- a/ext/speex/gstspeexdec.c +++ b/ext/speex/gstspeexdec.c @@ -78,53 +78,43 @@ GST_STATIC_PAD_TEMPLATE ("sink", ); #define gst_speex_dec_parent_class parent_class -G_DEFINE_TYPE (GstSpeexDec, gst_speex_dec, GST_TYPE_ELEMENT); - -static gboolean speex_dec_sink_event (GstPad * pad, GstEvent * event); -static GstFlowReturn speex_dec_chain (GstPad * pad, GstBuffer * buf); -static GstStateChangeReturn speex_dec_change_state (GstElement * element, - GstStateChange transition); - -static gboolean speex_dec_src_event (GstPad * pad, GstEvent * event); -static gboolean speex_dec_src_query (GstPad * pad, GstQuery * query); -static gboolean speex_dec_sink_query (GstPad * pad, GstQuery * query); -static const GstQueryType *speex_get_src_query_types (GstPad * pad); -static const GstQueryType *speex_get_sink_query_types (GstPad * pad); -static gboolean speex_dec_convert (GstPad * pad, - GstFormat src_format, gint64 src_value, - GstFormat * dest_format, gint64 * dest_value); +G_DEFINE_TYPE (GstSpeexDec, gst_speex_dec, GST_TYPE_AUDIO_DECODER); + +static gboolean gst_speex_dec_start (GstAudioDecoder * dec); +static gboolean gst_speex_dec_stop (GstAudioDecoder * dec); +static gboolean gst_speex_dec_set_format (GstAudioDecoder * bdec, + GstCaps * caps); +static GstFlowReturn gst_speex_dec_handle_frame (GstAudioDecoder * dec, + GstBuffer * buffer); static void gst_speex_dec_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec); static void gst_speex_dec_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec); -static GstFlowReturn speex_dec_chain_parse_data (GstSpeexDec * dec, - GstBuffer * buf, GstClockTime timestamp, GstClockTime duration); - -static GstFlowReturn speex_dec_chain_parse_header (GstSpeexDec * dec, - GstBuffer * buf); -static GstFlowReturn speex_dec_chain_parse_comments (GstSpeexDec * dec, - GstBuffer * buf); - static void gst_speex_dec_class_init (GstSpeexDecClass * klass) { GObjectClass *gobject_class; GstElementClass *gstelement_class; + GstAudioDecoderClass *base_class; gobject_class = (GObjectClass *) klass; gstelement_class = (GstElementClass *) klass; + base_class = (GstAudioDecoderClass *) klass; gobject_class->set_property = gst_speex_dec_set_property; gobject_class->get_property = gst_speex_dec_get_property; + base_class->start = GST_DEBUG_FUNCPTR (gst_speex_dec_start); + base_class->stop = GST_DEBUG_FUNCPTR (gst_speex_dec_stop); + base_class->set_format = GST_DEBUG_FUNCPTR (gst_speex_dec_set_format); + base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_speex_dec_handle_frame); + g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ENH, g_param_spec_boolean ("enh", "Enh", "Enable perceptual enhancement", DEFAULT_ENH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); - gstelement_class->change_state = GST_DEBUG_FUNCPTR (speex_dec_change_state); - gst_element_class_add_pad_template (gstelement_class, gst_static_pad_template_get (&speex_dec_src_factory)); gst_element_class_add_pad_template (gstelement_class, @@ -140,7 +130,6 @@ gst_speex_dec_class_init (GstSpeexDecClass * klass) static void gst_speex_dec_reset (GstSpeexDec * dec) { - gst_segment_init (&dec->segment, GST_FORMAT_UNDEFINED); dec->packetno = 0; dec->frame_size = 0; dec->frame_duration = 0; @@ -166,396 +155,38 @@ gst_speex_dec_reset (GstSpeexDec * dec) static void gst_speex_dec_init (GstSpeexDec * dec) { - dec->sinkpad = - gst_pad_new_from_static_template (&speex_dec_sink_factory, "sink"); - gst_pad_set_chain_function (dec->sinkpad, - GST_DEBUG_FUNCPTR (speex_dec_chain)); - gst_pad_set_event_function (dec->sinkpad, - GST_DEBUG_FUNCPTR (speex_dec_sink_event)); - gst_pad_set_query_type_function (dec->sinkpad, - GST_DEBUG_FUNCPTR (speex_get_sink_query_types)); - gst_pad_set_query_function (dec->sinkpad, - GST_DEBUG_FUNCPTR (speex_dec_sink_query)); - gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad); - - dec->srcpad = - gst_pad_new_from_static_template (&speex_dec_src_factory, "src"); - gst_pad_use_fixed_caps (dec->srcpad); - gst_pad_set_event_function (dec->srcpad, - GST_DEBUG_FUNCPTR (speex_dec_src_event)); - gst_pad_set_query_type_function (dec->srcpad, - GST_DEBUG_FUNCPTR (speex_get_src_query_types)); - gst_pad_set_query_function (dec->srcpad, - GST_DEBUG_FUNCPTR (speex_dec_src_query)); - gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad); - dec->enh = DEFAULT_ENH; gst_speex_dec_reset (dec); } static gboolean -speex_dec_sink_setcaps (GstPad * pad, GstCaps * caps) -{ - GstSpeexDec *dec = GST_SPEEX_DEC (gst_pad_get_parent (pad)); - gboolean ret = TRUE; - GstStructure *s; - const GValue *streamheader; - - 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 = speex_dec_chain_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 = speex_dec_chain_parse_comments (dec, buf); - if (res != GST_FLOW_OK) - goto done; - gst_buffer_replace (&dec->vorbiscomment, buf); - } - } - -done: - gst_object_unref (dec); - return ret; -} - -static gboolean -speex_dec_convert (GstPad * pad, - GstFormat src_format, gint64 src_value, - GstFormat * dest_format, gint64 * dest_value) -{ - gboolean res = TRUE; - GstSpeexDec *dec; - guint64 scale = 1; - - dec = GST_SPEEX_DEC (gst_pad_get_parent (pad)); - - if (src_format == *dest_format) { - *dest_value = src_value; - res = TRUE; - goto cleanup; - } - - if (dec->packetno < 1) { - res = FALSE; - 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 = 2 * dec->header->nb_channels; - case GST_FORMAT_DEFAULT: - *dest_value = - gst_util_uint64_scale_int (scale * src_value, dec->header->rate, - GST_SECOND); - break; - default: - res = FALSE; - break; - } - break; - case GST_FORMAT_DEFAULT: - switch (*dest_format) { - case GST_FORMAT_BYTES: - *dest_value = src_value * 2 * dec->header->nb_channels; - break; - case GST_FORMAT_TIME: - *dest_value = - gst_util_uint64_scale_int (src_value, GST_SECOND, - dec->header->rate); - break; - default: - res = FALSE; - break; - } - break; - case GST_FORMAT_BYTES: - switch (*dest_format) { - case GST_FORMAT_DEFAULT: - *dest_value = src_value / (2 * dec->header->nb_channels); - break; - case GST_FORMAT_TIME: - *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND, - dec->header->rate * 2 * dec->header->nb_channels); - break; - default: - res = FALSE; - break; - } - break; - default: - res = FALSE; - break; - } - -cleanup: - gst_object_unref (dec); - return res; -} - -static const GstQueryType * -speex_get_sink_query_types (GstPad * pad) -{ - static const GstQueryType speex_dec_sink_query_types[] = { - GST_QUERY_CONVERT, - 0 - }; - - return speex_dec_sink_query_types; -} - -static gboolean -speex_dec_sink_query (GstPad * pad, GstQuery * query) -{ - GstSpeexDec *dec; - gboolean res; - - dec = GST_SPEEX_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 = speex_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 * -speex_get_src_query_types (GstPad * pad) -{ - static const GstQueryType speex_dec_src_query_types[] = { - GST_QUERY_POSITION, - GST_QUERY_DURATION, - 0 - }; - - return speex_dec_src_query_types; -} - -static gboolean -speex_dec_src_query (GstPad * pad, GstQuery * query) -{ - GstSpeexDec *dec; - gboolean res = FALSE; - - dec = GST_SPEEX_DEC (gst_pad_get_parent (pad)); - - /* FIXME: why not just pass position/duration queries upstream to demuxer? */ - 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 = speex_dec_convert (dec->srcpad, GST_FORMAT_TIME, - segment.position, &format, &cur))) { - gst_query_set_position (query, format, cur); - } - break; - } - case GST_QUERY_DURATION:{ - GstFormat format; - gint64 dur; - - /* get duration from demuxer */ - if (!gst_pad_query_peer_duration (dec->sinkpad, GST_FORMAT_TIME, &dur)) - break; - - gst_query_parse_duration (query, &format, NULL); - - /* and convert it into the requested format */ - if ((res = speex_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 -speex_dec_src_event (GstPad * pad, GstEvent * event) +gst_speex_dec_start (GstAudioDecoder * dec) { - gboolean res = FALSE; - GstSpeexDec *dec = GST_SPEEX_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 = speex_dec_convert (pad, format, cur, &tformat, &tcur))) - break; - if (!(res = speex_dec_convert (pad, format, stop, &tformat, &tstop))) - break; + GstSpeexDec *sd = GST_SPEEX_DEC (dec); - /* 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_DEBUG_OBJECT (dec, "start"); + gst_speex_dec_reset (sd); - 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; - } + /* we know about concealment */ + gst_audio_decoder_set_plc_aware (dec, TRUE); - gst_object_unref (dec); - return res; + return TRUE; } static gboolean -speex_dec_sink_event (GstPad * pad, GstEvent * event) +gst_speex_dec_stop (GstAudioDecoder * dec) { - GstSpeexDec *dec; - gboolean ret = FALSE; - - dec = GST_SPEEX_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_CAPS: - { - GstCaps *caps; - - gst_event_parse_caps (event, &caps); - ret = speex_dec_sink_setcaps (pad, caps); - gst_event_unref (event); - break; - } - case GST_EVENT_SEGMENT:{ - GstSegment segment; - - gst_event_copy_segment (event, &segment); - - if (segment.format != GST_FORMAT_TIME) - goto newseg_wrong_format; - - if (segment.rate <= 0.0) - goto newseg_wrong_rate; - -#if 0 - if (update) { - /* time progressed without data, see if we can fill the gap with - * some concealment data */ - if (dec->segment.position < start) { - GstClockTime duration; - - duration = start - dec->segment.position; - speex_dec_chain_parse_data (dec, NULL, dec->segment.position, - duration); - } - } -#endif - - /* now configure the values */ - dec->segment = segment; - - GST_DEBUG_OBJECT (dec, "segment now: %" GST_SEGMENT_FORMAT, &segment); - ret = gst_pad_push_event (dec->srcpad, event); - break; - } - default: - ret = gst_pad_event_default (pad, event); - break; - } + GstSpeexDec *sd = GST_SPEEX_DEC (dec); - gst_object_unref (dec); - return ret; + GST_DEBUG_OBJECT (dec, "stop"); + gst_speex_dec_reset (sd); - /* 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 -speex_dec_chain_parse_header (GstSpeexDec * dec, GstBuffer * buf) +gst_speex_dec_parse_header (GstSpeexDec * dec, GstBuffer * buf) { GstCaps *caps; char *data; @@ -603,7 +234,7 @@ speex_dec_chain_parse_header (GstSpeexDec * dec, GstBuffer * buf) "rate", G_TYPE_INT, dec->header->rate, "channels", G_TYPE_INT, dec->header->nb_channels, NULL); - if (!gst_pad_set_caps (dec->srcpad, caps)) + if (!gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps)) goto nego_failed; gst_caps_unref (caps); @@ -640,7 +271,7 @@ nego_failed: } static GstFlowReturn -speex_dec_chain_parse_comments (GstSpeexDec * dec, GstBuffer * buf) +gst_speex_dec_parse_comments (GstSpeexDec * dec, GstBuffer * buf) { GstTagList *list; gchar *ver, *encoder = NULL; @@ -675,7 +306,8 @@ speex_dec_chain_parse_comments (GstSpeexDec * dec, GstBuffer * buf) GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list); - gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, list); + gst_element_found_tags_for_pad (GST_ELEMENT (dec), + GST_AUDIO_DECODER_SRC_PAD (dec), list); g_free (encoder); g_free (ver); @@ -683,9 +315,47 @@ speex_dec_chain_parse_comments (GstSpeexDec * dec, GstBuffer * buf) return GST_FLOW_OK; } +static gboolean +gst_speex_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps) +{ + GstSpeexDec *dec = GST_SPEEX_DEC (bdec); + gboolean ret = TRUE; + GstStructure *s; + const GValue *streamheader; + + 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_speex_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_speex_dec_parse_comments (dec, buf); + if (res != GST_FLOW_OK) + goto done; + gst_buffer_replace (&dec->vorbiscomment, buf); + } + } + +done: + return ret; +} + static GstFlowReturn -speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf, - GstClockTime timestamp, GstClockTime duration) +gst_speex_dec_parse_data (GstSpeexDec * dec, GstBuffer * buf) { GstFlowReturn res = GST_FLOW_OK; gint i, fpp; @@ -696,13 +366,7 @@ speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf, if (!dec->frame_duration) goto not_negotiated; - if (timestamp != -1) { - dec->segment.position = timestamp; - } else { - timestamp = dec->segment.position; - } - - if (buf) { + if (G_LIKELY (gst_buffer_get_size (buf))) { /* send data to the bitstream */ data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ); speex_bits_read_from (&dec->bits, data, size); @@ -711,16 +375,16 @@ speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf, fpp = dec->header->frames_per_packet; bits = &dec->bits; - GST_DEBUG_OBJECT (dec, "received buffer of size %u, fpp %d, %d bits", size, - fpp, speex_bits_remaining (bits)); + GST_DEBUG_OBJECT (dec, "received buffer of size %u, fpp %d, %d bits", + size, fpp, speex_bits_remaining (bits)); } else { + /* FIXME ? actually consider how much concealment is needed */ /* concealment data, pass NULL as the bits parameters */ GST_DEBUG_OBJECT (dec, "creating concealment data"); fpp = dec->header->frames_per_packet; bits = NULL; } - /* now decode each frame, catering for unknown number of them (e.g. rtp) */ for (i = 0; i < fpp; i++) { GstBuffer *outbuf; @@ -730,9 +394,10 @@ speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf, GST_LOG_OBJECT (dec, "decoding frame %d/%d, %d bits remaining", i, fpp, bits ? speex_bits_remaining (bits) : -1); #if 0 - 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_size * dec->header->nb_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)); @@ -754,39 +419,27 @@ speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf, if (fpp == 0 && speex_bits_remaining (bits) < 8) { /* if we did not know how many frames to expect, then we get this at the end if there are leftover bits to pad to the next byte */ + GST_DEBUG_OBJECT (dec, "Discarding leftover bits"); } else { GST_WARNING_OBJECT (dec, "Unexpected end of stream found"); } + gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1); gst_buffer_unref (outbuf); - outbuf = NULL; - break; } else if (ret == -2) { GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?"); + gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1); gst_buffer_unref (outbuf); - outbuf = NULL; - break; } if (bits && speex_bits_remaining (bits) < 0) { GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?"); + gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1); gst_buffer_unref (outbuf); - outbuf = NULL; - break; } if (dec->header->nb_channels == 2) speex_decode_stereo_int (out_data, dec->frame_size, dec->stereo); - GST_BUFFER_TIMESTAMP (outbuf) = timestamp; - GST_BUFFER_DURATION (outbuf) = dec->frame_duration; - - dec->segment.position += dec->frame_duration; - timestamp = dec->segment.position; - - GST_LOG_OBJECT (dec, "pushing buffer with ts=%" GST_TIME_FORMAT ", dur=%" - GST_TIME_FORMAT, GST_TIME_ARGS (timestamp), - 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)); @@ -826,48 +479,55 @@ memcmp_buffers (GstBuffer * buf1, GstBuffer * buf2) } static GstFlowReturn -speex_dec_chain (GstPad * pad, GstBuffer * buf) +gst_speex_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf) { GstFlowReturn res; GstSpeexDec *dec; - dec = GST_SPEEX_DEC (gst_pad_get_parent (pad)); + /* no fancy draining */ + if (G_UNLIKELY (!buf)) + return GST_FLOW_OK; + + dec = GST_SPEEX_DEC (bdec); /* 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 (bdec, NULL, 1); res = GST_FLOW_OK; } else if (memcmp_buffers (dec->vorbiscomment, buf)) { + GST_DEBUG_OBJECT (dec, "found vorbiscomments"); + gst_audio_decoder_finish_frame (bdec, NULL, 1); res = GST_FLOW_OK; } else { - res = - speex_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf), - GST_BUFFER_DURATION (buf)); + res = gst_speex_dec_parse_data (dec, buf); } } else { /* Otherwise fall back to packet counting and assume that the * first two packets are the headers. */ switch (dec->packetno) { case 0: - res = speex_dec_chain_parse_header (dec, buf); + GST_DEBUG_OBJECT (dec, "counted streamheader"); + res = gst_speex_dec_parse_header (dec, buf); + gst_audio_decoder_finish_frame (bdec, NULL, 1); break; case 1: - res = speex_dec_chain_parse_comments (dec, buf); + GST_DEBUG_OBJECT (dec, "counted vorbiscomments"); + res = gst_speex_dec_parse_comments (dec, buf); + gst_audio_decoder_finish_frame (bdec, NULL, 1); break; default: - res = - speex_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf), - GST_BUFFER_DURATION (buf)); + { + res = gst_speex_dec_parse_data (dec, buf); break; + } } } dec->packetno++; - gst_buffer_unref (buf); - gst_object_unref (dec); - return res; } @@ -906,37 +566,3 @@ gst_speex_dec_set_property (GObject * object, guint prop_id, break; } } - - -static GstStateChangeReturn -speex_dec_change_state (GstElement * element, GstStateChange transition) -{ - GstStateChangeReturn ret; - GstSpeexDec *dec = GST_SPEEX_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 = GST_ELEMENT_CLASS (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_speex_dec_reset (dec); - break; - case GST_STATE_CHANGE_READY_TO_NULL: - break; - default: - break; - } - - return ret; -} diff --git a/ext/speex/gstspeexdec.h b/ext/speex/gstspeexdec.h index 660d8053d..8187af87c 100644 --- a/ext/speex/gstspeexdec.h +++ b/ext/speex/gstspeexdec.h @@ -22,6 +22,8 @@ #define __GST_SPEEX_DEC_H__ #include <gst/gst.h> +#include <gst/audio/gstaudiodecoder.h> + #include <speex/speex.h> #include <speex/speex_callbacks.h> #include <speex/speex_header.h> @@ -44,11 +46,7 @@ typedef struct _GstSpeexDec GstSpeexDec; typedef struct _GstSpeexDecClass GstSpeexDecClass; struct _GstSpeexDec { - GstElement element; - - /* pads */ - GstPad *sinkpad; - GstPad *srcpad; + GstAudioDecoder element; void *state; SpeexStereoState *stereo; @@ -67,14 +65,12 @@ struct _GstSpeexDec { GstClockTime frame_duration; guint64 packetno; - GstSegment segment; /* STREAM LOCK */ - GstBuffer *streamheader; GstBuffer *vorbiscomment; }; struct _GstSpeexDecClass { - GstElementClass parent_class; + GstAudioDecoderClass parent_class; }; GType gst_speex_dec_get_type (void); diff --git a/ext/speex/gstspeexenc.c b/ext/speex/gstspeexenc.c index e202d9224..e4eda25cf 100644 --- a/ext/speex/gstspeexenc.c +++ b/ext/speex/gstspeexenc.c @@ -113,43 +113,28 @@ gst_speex_enc_mode_get_type (void) return speex_enc_mode_type; } -#if 0 -static const GstFormat * -gst_speex_enc_get_formats (GstPad * pad) -{ - static const GstFormat src_formats[] = { - GST_FORMAT_BYTES, - GST_FORMAT_TIME, - 0 - }; - static const GstFormat sink_formats[] = { - GST_FORMAT_BYTES, - GST_FORMAT_DEFAULT, - GST_FORMAT_TIME, - 0 - }; - - return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats); -} -#endif - static void gst_speex_enc_finalize (GObject * object); -static gboolean gst_speex_enc_sink_event (GstPad * pad, GstEvent * event); -static GstFlowReturn gst_speex_enc_chain (GstPad * pad, GstBuffer * buf); static gboolean gst_speex_enc_setup (GstSpeexEnc * enc); static void gst_speex_enc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec); static void gst_speex_enc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec); -static GstStateChangeReturn gst_speex_enc_change_state (GstElement * element, - GstStateChange transition); -static GstFlowReturn gst_speex_enc_encode (GstSpeexEnc * enc, gboolean flush); +static GstFlowReturn gst_speex_enc_encode (GstSpeexEnc * enc, GstBuffer * buf); + +static gboolean gst_speex_enc_start (GstAudioEncoder * enc); +static gboolean gst_speex_enc_stop (GstAudioEncoder * enc); +static gboolean gst_speex_enc_set_format (GstAudioEncoder * enc, + GstAudioInfo * info); +static GstFlowReturn gst_speex_enc_handle_frame (GstAudioEncoder * enc, + GstBuffer * in_buf); +static gboolean gst_speex_enc_sink_event (GstAudioEncoder * enc, + GstEvent * event); #define gst_speex_enc_parent_class parent_class -G_DEFINE_TYPE_WITH_CODE (GstSpeexEnc, gst_speex_enc, GST_TYPE_ELEMENT, +G_DEFINE_TYPE_WITH_CODE (GstSpeexEnc, gst_speex_enc, GST_TYPE_AUDIO_ENCODER, G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL); G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL)); @@ -158,62 +143,67 @@ gst_speex_enc_class_init (GstSpeexEncClass * klass) { GObjectClass *gobject_class; GstElementClass *gstelement_class; + GstAudioEncoderClass *base_class; gobject_class = (GObjectClass *) klass; gstelement_class = (GstElementClass *) klass; + base_class = (GstAudioEncoderClass *) klass; gobject_class->finalize = gst_speex_enc_finalize; gobject_class->set_property = gst_speex_enc_set_property; gobject_class->get_property = gst_speex_enc_get_property; + base_class->start = GST_DEBUG_FUNCPTR (gst_speex_enc_start); + base_class->stop = GST_DEBUG_FUNCPTR (gst_speex_enc_stop); + base_class->set_format = GST_DEBUG_FUNCPTR (gst_speex_enc_set_format); + base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_speex_enc_handle_frame); + base_class->event = GST_DEBUG_FUNCPTR (gst_speex_enc_sink_event); + g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUALITY, g_param_spec_float ("quality", "Quality", "Encoding quality", 0.0, 10.0, DEFAULT_QUALITY, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BITRATE, g_param_spec_int ("bitrate", "Encoding Bit-rate", "Specify an encoding bit-rate (in bps). (0 = automatic)", 0, G_MAXINT, DEFAULT_BITRATE, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (gobject_class, PROP_MODE, g_param_spec_enum ("mode", "Mode", "The encoding mode", GST_TYPE_SPEEX_ENC_MODE, GST_SPEEX_ENC_MODE_AUTO, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_VBR, g_param_spec_boolean ("vbr", "VBR", "Enable variable bit-rate", DEFAULT_VBR, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ABR, g_param_spec_int ("abr", "ABR", "Enable average bit-rate (0 = disabled)", 0, G_MAXINT, DEFAULT_ABR, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_VAD, g_param_spec_boolean ("vad", "VAD", "Enable voice activity detection", DEFAULT_VAD, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DTX, g_param_spec_boolean ("dtx", "DTX", "Enable discontinuous transmission", DEFAULT_DTX, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_COMPLEXITY, g_param_spec_int ("complexity", "Complexity", "Set encoding complexity", 0, G_MAXINT, DEFAULT_COMPLEXITY, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_NFRAMES, g_param_spec_int ("nframes", "NFrames", "Number of frames per buffer", 0, G_MAXINT, DEFAULT_NFRAMES, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LAST_MESSAGE, g_param_spec_string ("last-message", "last-message", "The last status message", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); - gstelement_class->change_state = - GST_DEBUG_FUNCPTR (gst_speex_enc_change_state); - gst_element_class_add_pad_template (gstelement_class, gst_static_pad_template_get (&src_factory)); gst_element_class_add_pad_template (gstelement_class, @@ -233,166 +223,49 @@ gst_speex_enc_finalize (GObject * object) enc = GST_SPEEX_ENC (object); g_free (enc->last_message); - g_object_unref (enc->adapter); G_OBJECT_CLASS (parent_class)->finalize (object); } -static gboolean -gst_speex_enc_sink_setcaps (GstPad * pad, GstCaps * caps) -{ - GstSpeexEnc *enc; - GstStructure *structure; - - enc = GST_SPEEX_ENC (GST_PAD_PARENT (pad)); - enc->setup = FALSE; - - structure = gst_caps_get_structure (caps, 0); - gst_structure_get_int (structure, "channels", &enc->channels); - gst_structure_get_int (structure, "rate", &enc->rate); - - gst_speex_enc_setup (enc); - - return enc->setup; -} - - -static GstCaps * -gst_speex_enc_sink_getcaps (GstPad * pad, GstCaps * filter) +static void +gst_speex_enc_init (GstSpeexEnc * enc) { - GstCaps *caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad)); - GstCaps *peercaps = NULL; - GstSpeexEnc *enc = GST_SPEEX_ENC (gst_pad_get_parent_element (pad)); - - peercaps = gst_pad_peer_get_caps (enc->srcpad, filter); - - if (peercaps) { - if (!gst_caps_is_empty (peercaps) && !gst_caps_is_any (peercaps)) { - GstStructure *ps = gst_caps_get_structure (peercaps, 0); - GstStructure *s = gst_caps_get_structure (caps, 0); - gint rate, channels; - - if (gst_structure_get_int (ps, "rate", &rate)) { - gst_structure_fixate_field_nearest_int (s, "rate", rate); - } - - if (gst_structure_get_int (ps, "channels", &channels)) { - gst_structure_fixate_field_nearest_int (s, "channels", channels); - } - } - gst_caps_unref (peercaps); - } - - gst_object_unref (enc); + GstAudioEncoder *benc = GST_AUDIO_ENCODER (enc); - return caps; + /* arrange granulepos marking (and required perfect ts) */ + gst_audio_encoder_set_mark_granule (benc, TRUE); + gst_audio_encoder_set_perfect_timestamp (benc, TRUE); } - static gboolean -gst_speex_enc_convert_src (GstPad * pad, GstFormat src_format, gint64 src_value, - GstFormat dest_format, gint64 * dest_value) +gst_speex_enc_start (GstAudioEncoder * benc) { - gboolean res = TRUE; - GstSpeexEnc *enc; - gint64 avg; - - enc = GST_SPEEX_ENC (GST_PAD_PARENT (pad)); + GstSpeexEnc *enc = GST_SPEEX_ENC (benc); - if (enc->samples_in == 0 || enc->bytes_out == 0 || enc->rate == 0) - return FALSE; - - avg = (enc->bytes_out * enc->rate) / (enc->samples_in); + GST_DEBUG_OBJECT (enc, "start"); + speex_bits_init (&enc->bits); + enc->tags = gst_tag_list_new (); + enc->header_sent = FALSE; - switch (src_format) { - case GST_FORMAT_BYTES: - switch (dest_format) { - case GST_FORMAT_TIME: - *dest_value = src_value * GST_SECOND / avg; - break; - default: - res = FALSE; - } - break; - case GST_FORMAT_TIME: - switch (dest_format) { - case GST_FORMAT_BYTES: - *dest_value = src_value * avg / GST_SECOND; - break; - default: - res = FALSE; - } - break; - default: - res = FALSE; - } - return res; + return TRUE; } static gboolean -gst_speex_enc_convert_sink (GstPad * pad, GstFormat src_format, - gint64 src_value, GstFormat * dest_format, gint64 * dest_value) +gst_speex_enc_stop (GstAudioEncoder * benc) { - gboolean res = TRUE; - guint scale = 1; - gint bytes_per_sample; - GstSpeexEnc *enc; + GstSpeexEnc *enc = GST_SPEEX_ENC (benc); - enc = GST_SPEEX_ENC (GST_PAD_PARENT (pad)); - - bytes_per_sample = enc->channels * 2; - - switch (src_format) { - case GST_FORMAT_BYTES: - switch (*dest_format) { - case GST_FORMAT_DEFAULT: - if (bytes_per_sample == 0) - return FALSE; - *dest_value = src_value / bytes_per_sample; - break; - case GST_FORMAT_TIME: - { - gint byterate = bytes_per_sample * enc->rate; - - if (byterate == 0) - return FALSE; - *dest_value = src_value * GST_SECOND / byterate; - break; - } - default: - res = FALSE; - } - break; - case GST_FORMAT_DEFAULT: - switch (*dest_format) { - case GST_FORMAT_BYTES: - *dest_value = src_value * bytes_per_sample; - break; - case GST_FORMAT_TIME: - if (enc->rate == 0) - return FALSE; - *dest_value = src_value * GST_SECOND / enc->rate; - break; - default: - res = FALSE; - } - break; - case GST_FORMAT_TIME: - switch (*dest_format) { - case GST_FORMAT_BYTES: - scale = bytes_per_sample; - /* fallthrough */ - case GST_FORMAT_DEFAULT: - *dest_value = src_value * scale * enc->rate / GST_SECOND; - break; - default: - res = FALSE; - } - break; - default: - res = FALSE; + GST_DEBUG_OBJECT (enc, "stop"); + enc->header_sent = FALSE; + if (enc->state) { + speex_encoder_destroy (enc->state); + enc->state = NULL; } - return res; + speex_bits_destroy (&enc->bits); + gst_tag_list_free (enc->tags); + enc->tags = NULL; + + return TRUE; } static gint64 @@ -405,185 +278,45 @@ gst_speex_enc_get_latency (GstSpeexEnc * enc) return 34 * GST_MSECOND; } -static const GstQueryType * -gst_speex_enc_get_query_types (GstPad * pad) -{ - static const GstQueryType gst_speex_enc_src_query_types[] = { - GST_QUERY_POSITION, - GST_QUERY_DURATION, - GST_QUERY_CONVERT, - GST_QUERY_LATENCY, - 0 - }; - - return gst_speex_enc_src_query_types; -} - static gboolean -gst_speex_enc_src_query (GstPad * pad, GstQuery * query) +gst_speex_enc_set_format (GstAudioEncoder * benc, GstAudioInfo * info) { - gboolean res = TRUE; GstSpeexEnc *enc; - enc = GST_SPEEX_ENC (gst_pad_get_parent (pad)); - - switch (GST_QUERY_TYPE (query)) { - case GST_QUERY_POSITION: - { - GstFormat req_fmt; - gint64 pos, val; - - gst_query_parse_position (query, &req_fmt, NULL); - if ((res = gst_pad_query_peer_position (enc->sinkpad, req_fmt, &val))) { - gst_query_set_position (query, req_fmt, val); - break; - } - - res = gst_pad_query_peer_position (enc->sinkpad, GST_FORMAT_TIME, &pos); - if (!res) - break; - - if ((res = - gst_pad_query_peer_convert (enc->sinkpad, GST_FORMAT_TIME, pos, - req_fmt, &val))) { - gst_query_set_position (query, req_fmt, val); - } - break; - } - case GST_QUERY_DURATION: - { - GstFormat req_fmt; - gint64 dur, val; - - gst_query_parse_duration (query, &req_fmt, NULL); - if ((res = gst_pad_query_peer_duration (enc->sinkpad, req_fmt, &val))) { - gst_query_set_duration (query, req_fmt, val); - break; - } + enc = GST_SPEEX_ENC (benc); - res = gst_pad_query_peer_duration (enc->sinkpad, GST_FORMAT_TIME, &dur); - if (!res) - break; + enc->channels = GST_AUDIO_INFO_CHANNELS (info); + enc->rate = GST_AUDIO_INFO_RATE (info); - if ((res = - gst_pad_query_peer_convert (enc->sinkpad, GST_FORMAT_TIME, dur, - req_fmt, &val))) { - gst_query_set_duration (query, req_fmt, val); - } - break; - } - 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); - if (!(res = gst_speex_enc_convert_src (pad, src_fmt, src_val, dest_fmt, - &dest_val))) - goto error; - gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val); - break; - } - case GST_QUERY_LATENCY: - { - gboolean live; - GstClockTime min_latency, max_latency; - gint64 latency; - - if ((res = gst_pad_peer_query (enc->sinkpad, query))) { - gst_query_parse_latency (query, &live, &min_latency, &max_latency); - GST_LOG_OBJECT (pad, "Upstream latency: %" GST_PTR_FORMAT, query); - - latency = gst_speex_enc_get_latency (enc); - - /* add our latency */ - min_latency += latency; - if (max_latency != -1) - max_latency += latency; - - gst_query_set_latency (query, live, min_latency, max_latency); - GST_LOG_OBJECT (pad, "Adjusted latency: %" GST_PTR_FORMAT, query); - } - break; - } - default: - res = gst_pad_peer_query (enc->sinkpad, query); - break; + /* handle reconfigure */ + if (enc->state) { + speex_encoder_destroy (enc->state); + enc->state = NULL; } -error: - - gst_object_unref (enc); - - return res; -} + if (!gst_speex_enc_setup (enc)) + return FALSE; -static gboolean -gst_speex_enc_sink_query (GstPad * pad, GstQuery * query) -{ - gboolean res = TRUE; + /* feedback to base class */ + gst_audio_encoder_set_latency (benc, + gst_speex_enc_get_latency (enc), gst_speex_enc_get_latency (enc)); + gst_audio_encoder_set_lookahead (benc, enc->lookahead); - 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); - if (!(res = - gst_speex_enc_convert_sink (pad, src_fmt, src_val, &dest_fmt, - &dest_val))) - goto error; - gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val); - break; - } - default: - res = gst_pad_query_default (pad, query); - break; + if (enc->nframes == 0) { + /* as many frames as available input allows */ + gst_audio_encoder_set_frame_samples_min (benc, enc->frame_size); + gst_audio_encoder_set_frame_samples_max (benc, enc->frame_size); + gst_audio_encoder_set_frame_max (benc, 0); + } else { + /* exactly as many frames as configured */ + gst_audio_encoder_set_frame_samples_min (benc, + enc->frame_size * enc->nframes); + gst_audio_encoder_set_frame_samples_max (benc, + enc->frame_size * enc->nframes); + gst_audio_encoder_set_frame_max (benc, 1); } -error: - return res; -} - -static void -gst_speex_enc_init (GstSpeexEnc * enc) -{ - enc->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink"); - gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad); - gst_pad_set_event_function (enc->sinkpad, - GST_DEBUG_FUNCPTR (gst_speex_enc_sink_event)); - gst_pad_set_chain_function (enc->sinkpad, - GST_DEBUG_FUNCPTR (gst_speex_enc_chain)); - gst_pad_set_getcaps_function (enc->sinkpad, - GST_DEBUG_FUNCPTR (gst_speex_enc_sink_getcaps)); - gst_pad_set_query_function (enc->sinkpad, - GST_DEBUG_FUNCPTR (gst_speex_enc_sink_query)); - - enc->srcpad = gst_pad_new_from_static_template (&src_factory, "src"); - gst_pad_set_query_function (enc->srcpad, - GST_DEBUG_FUNCPTR (gst_speex_enc_src_query)); - gst_pad_set_query_type_function (enc->srcpad, - GST_DEBUG_FUNCPTR (gst_speex_enc_get_query_types)); - gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad); - - enc->channels = -1; - enc->rate = -1; - - enc->quality = DEFAULT_QUALITY; - enc->bitrate = DEFAULT_BITRATE; - enc->mode = DEFAULT_MODE; - enc->vbr = DEFAULT_VBR; - enc->abr = DEFAULT_ABR; - enc->vad = DEFAULT_VAD; - enc->dtx = DEFAULT_DTX; - enc->complexity = DEFAULT_COMPLEXITY; - enc->nframes = DEFAULT_NFRAMES; - - enc->setup = FALSE; - enc->header_sent = FALSE; - - enc->adapter = gst_adapter_new (); + return TRUE; } static GstBuffer * @@ -610,7 +343,7 @@ gst_speex_enc_create_metadata_buffer (GstSpeexEnc * enc) 0, "Encoded with GStreamer Speexenc"); gst_tag_list_free (merged_tags); - GST_BUFFER_OFFSET (comments) = enc->bytes_out; + GST_BUFFER_OFFSET (comments) = 0; GST_BUFFER_OFFSET_END (comments) = 0; return comments; @@ -628,8 +361,6 @@ gst_speex_enc_set_last_msg (GstSpeexEnc * enc, const gchar * msg) static gboolean gst_speex_enc_setup (GstSpeexEnc * enc) { - enc->setup = FALSE; - switch (enc->mode) { case GST_SPEEX_ENC_MODE_UWB: GST_LOG_OBJECT (enc, "configuring for requested UWB mode"); @@ -746,105 +477,27 @@ gst_speex_enc_setup (GstSpeexEnc * enc) GST_LOG_OBJECT (enc, "we have frame size %d, lookahead %d", enc->frame_size, enc->lookahead); - enc->setup = TRUE; - return TRUE; } -/* prepare a buffer for transmission */ -static GstBuffer * -gst_speex_enc_buffer_from_data (GstSpeexEnc * enc, guchar * data, - gint data_len, guint64 granulepos) -{ - GstBuffer *outbuf; - - outbuf = gst_buffer_new_and_alloc (data_len); - gst_buffer_fill (outbuf, 0, data, data_len); - GST_BUFFER_OFFSET (outbuf) = enc->bytes_out; - GST_BUFFER_OFFSET_END (outbuf) = granulepos; - - GST_LOG_OBJECT (enc, "encoded buffer of %d bytes", data_len); - return outbuf; -} - - -/* push out the buffer and do internal bookkeeping */ +/* push out the buffer */ static GstFlowReturn gst_speex_enc_push_buffer (GstSpeexEnc * enc, GstBuffer * buffer) { - guint size; - - size = gst_buffer_get_size (buffer); - enc->bytes_out += size; - - GST_DEBUG_OBJECT (enc, "pushing output buffer of size %u", size); - - return gst_pad_push (enc->srcpad, buffer); -} - -static GstCaps * -gst_speex_enc_set_header_on_caps (GstCaps * caps, GstBuffer * buf1, - GstBuffer * buf2) -{ - GstStructure *structure = NULL; - GstBuffer *buf; - GValue array = { 0 }; - GValue value = { 0 }; - - caps = gst_caps_make_writable (caps); - structure = gst_caps_get_structure (caps, 0); + GST_DEBUG_OBJECT (enc, "pushing output buffer of size %u", + gst_buffer_get_size (buffer)); - g_assert (gst_buffer_is_writable (buf1)); - g_assert (gst_buffer_is_writable (buf2)); - - /* mark buffers */ - GST_BUFFER_FLAG_SET (buf1, GST_BUFFER_FLAG_IN_CAPS); - GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_IN_CAPS); - - /* put buffers in a fixed list */ - g_value_init (&array, GST_TYPE_ARRAY); - g_value_init (&value, GST_TYPE_BUFFER); - buf = gst_buffer_copy (buf1); - gst_value_set_buffer (&value, buf); - gst_buffer_unref (buf); - gst_value_array_append_value (&array, &value); - g_value_unset (&value); - g_value_init (&value, GST_TYPE_BUFFER); - buf = gst_buffer_copy (buf2); - gst_value_set_buffer (&value, buf); - gst_buffer_unref (buf); - gst_value_array_append_value (&array, &value); - gst_structure_set_value (structure, "streamheader", &array); - g_value_unset (&value); - g_value_unset (&array); - - return caps; + return gst_pad_push (GST_AUDIO_ENCODER_SRC_PAD (enc), buffer); } - static gboolean -gst_speex_enc_sink_event (GstPad * pad, GstEvent * event) +gst_speex_enc_sink_event (GstAudioEncoder * benc, GstEvent * event) { - gboolean res = TRUE; GstSpeexEnc *enc; - enc = GST_SPEEX_ENC (gst_pad_get_parent (pad)); + enc = GST_SPEEX_ENC (benc); switch (GST_EVENT_TYPE (event)) { - case GST_EVENT_CAPS: - { - GstCaps *caps; - - gst_event_parse_caps (event, &caps); - res = gst_speex_enc_sink_setcaps (pad, caps); - gst_event_unref (event); - break; - } - case GST_EVENT_EOS: - if (enc->setup) - gst_speex_enc_encode (enc, TRUE); - res = gst_pad_event_default (pad, event); - break; case GST_EVENT_TAG: { if (enc->tags) { @@ -856,116 +509,165 @@ gst_speex_enc_sink_event (GstPad * pad, GstEvent * event) } else { g_assert_not_reached (); } - res = gst_pad_event_default (pad, event); break; } default: - res = gst_pad_event_default (pad, event); break; } - gst_object_unref (enc); - - return res; + /* we only peeked, let base class handle it */ + return FALSE; } static GstFlowReturn -gst_speex_enc_encode (GstSpeexEnc * enc, gboolean flush) +gst_speex_enc_encode (GstSpeexEnc * enc, GstBuffer * buf) { gint frame_size = enc->frame_size; - gint bytes = frame_size * 2 * enc->channels; + gint bytes = frame_size * 2 * enc->channels, samples; + gint outsize, written, dtx_ret = 0; + guint8 *data, *bdata, *outdata; + gsize bsize, size; + GstBuffer *outbuf; GstFlowReturn ret = GST_FLOW_OK; - if (flush && gst_adapter_available (enc->adapter) % bytes != 0) { - guint diff = gst_adapter_available (enc->adapter) % bytes; - GstBuffer *buf = gst_buffer_new_and_alloc (diff); - gst_buffer_memset (buf, 0, 0, diff); - gst_adapter_push (enc->adapter, buf); - } + if (G_LIKELY (buf)) { + bdata = gst_buffer_map (buf, &bsize, NULL, GST_MAP_READ); + + if (G_UNLIKELY (bsize % bytes)) { + GST_DEBUG_OBJECT (enc, "draining; adding silence samples"); - while (gst_adapter_available (enc->adapter) >= bytes) { - gint16 *data; - gint outsize, written, dtx_ret; - GstBuffer *outbuf; - gchar *outdata; + size = ((bsize / bytes) + 1) * bytes; + data = g_malloc0 (size); + memcpy (data, bdata, bsize); + gst_buffer_unmap (buf, bdata, bsize); + bdata = NULL; + } else { + data = bdata; + size = bsize; + } + } else { + GST_DEBUG_OBJECT (enc, "nothing to drain"); + goto done; + } - data = (gint16 *) gst_adapter_take (enc->adapter, bytes); + samples = size / (2 * enc->channels); + speex_bits_reset (&enc->bits); - enc->samples_in += frame_size; + /* FIXME what about dropped samples if DTS enabled ?? */ + while (size) { GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)", frame_size, bytes); if (enc->channels == 2) { - speex_encode_stereo_int (data, frame_size, &enc->bits); + speex_encode_stereo_int ((gint16 *) data, frame_size, &enc->bits); } - dtx_ret = speex_encode_int (enc->state, data, &enc->bits); - - g_free (data); + dtx_ret += speex_encode_int (enc->state, (gint16 *) data, &enc->bits); - enc->frameno++; - enc->frameno_out++; + data += bytes; + size -= bytes; + } - if ((enc->frameno % enc->nframes) != 0) - continue; + speex_bits_insert_terminator (&enc->bits); + outsize = speex_bits_nbytes (&enc->bits); - speex_bits_insert_terminator (&enc->bits); - outsize = speex_bits_nbytes (&enc->bits); + if (bdata) + gst_buffer_unmap (buf, bdata, bsize); #if 0 - ret = gst_pad_alloc_buffer_and_set_caps (enc->srcpad, - GST_BUFFER_OFFSET_NONE, outsize, GST_PAD_CAPS (enc->srcpad), &outbuf); - if ((GST_FLOW_OK != ret)) - goto done; + ret = gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_ENCODER_SRC_PAD (enc), + GST_BUFFER_OFFSET_NONE, outsize, + GST_PAD_CAPS (GST_AUDIO_ENCODER_SRC_PAD (enc)), &outbuf); + + if ((GST_FLOW_OK != ret)) + goto done; #endif - outbuf = gst_buffer_new_allocate (NULL, outsize, 0); + outbuf = gst_buffer_new_allocate (NULL, outsize, 0); + outdata = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE); - outdata = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE); - written = speex_bits_write (&enc->bits, outdata, outsize); + written = speex_bits_write (&enc->bits, (gchar *) outdata, outsize); - if (G_UNLIKELY (written != outsize)) { - GST_ERROR_OBJECT (enc, "short write: %d < %d bytes", written, outsize); - } - gst_buffer_unmap (outbuf, outdata, written); + if (G_UNLIKELY (written < outsize)) { + GST_ERROR_OBJECT (enc, "short write: %d < %d bytes", written, outsize); + } else if (G_UNLIKELY (written > outsize)) { + GST_ERROR_OBJECT (enc, "overrun: %d > %d bytes", written, outsize); + written = outsize; + } + gst_buffer_unmap (outbuf, outdata, written); + + if (!dtx_ret) + GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP); - speex_bits_reset (&enc->bits); + ret = gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc), + outbuf, samples); - if (!dtx_ret) - GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP); +done: + return ret; +} - GST_BUFFER_TIMESTAMP (outbuf) = enc->start_ts + - gst_util_uint64_scale_int ((enc->frameno_out - - enc->nframes) * frame_size - enc->lookahead, GST_SECOND, enc->rate); - GST_BUFFER_DURATION (outbuf) = - gst_util_uint64_scale_int (frame_size * enc->nframes, GST_SECOND, - enc->rate); - /* set gp time and granulepos; see gst-plugins-base/ext/ogg/README */ - GST_BUFFER_OFFSET_END (outbuf) = enc->granulepos_offset + - ((enc->frameno_out) * frame_size - enc->lookahead); - GST_BUFFER_OFFSET (outbuf) = - gst_util_uint64_scale_int (GST_BUFFER_OFFSET_END (outbuf), GST_SECOND, - enc->rate); +/* + * (really really) FIXME: move into core (dixit tpm) + */ +/** + * _gst_caps_set_buffer_array: + * @caps: a #GstCaps + * @field: field in caps to set + * @buf: header buffers + * + * Adds given buffers to an array of buffers set as the given @field + * on the given @caps. List of buffer arguments must be NULL-terminated. + * + * Returns: input caps with a streamheader field added, or NULL if some error + */ +static GstCaps * +_gst_caps_set_buffer_array (GstCaps * caps, const gchar * field, + GstBuffer * buf, ...) +{ + GstStructure *structure = NULL; + va_list va; + GValue array = { 0 }; + GValue value = { 0 }; - ret = gst_speex_enc_push_buffer (enc, outbuf); + g_return_val_if_fail (caps != NULL, NULL); + g_return_val_if_fail (gst_caps_is_fixed (caps), NULL); + g_return_val_if_fail (field != NULL, NULL); - if ((GST_FLOW_OK != ret) && (GST_FLOW_NOT_LINKED != ret)) - goto done; + caps = gst_caps_make_writable (caps); + structure = gst_caps_get_structure (caps, 0); + + g_value_init (&array, GST_TYPE_ARRAY); + + va_start (va, buf); + /* put buffers in a fixed list */ + while (buf) { + g_assert (gst_buffer_is_writable (buf)); + + /* mark buffer */ + GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS); + + g_value_init (&value, GST_TYPE_BUFFER); + buf = gst_buffer_copy (buf); + GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS); + gst_value_set_buffer (&value, buf); + gst_buffer_unref (buf); + gst_value_array_append_value (&array, &value); + g_value_unset (&value); + + buf = va_arg (va, GstBuffer *); } -done: + gst_structure_set_value (structure, field, &array); + g_value_unset (&array); - return ret; + return caps; } static GstFlowReturn -gst_speex_enc_chain (GstPad * pad, GstBuffer * buf) +gst_speex_enc_handle_frame (GstAudioEncoder * benc, GstBuffer * buf) { GstSpeexEnc *enc; GstFlowReturn ret = GST_FLOW_OK; - enc = GST_SPEEX_ENC (GST_PAD_PARENT (pad)); - - if (!enc->setup) - goto not_setup; + enc = GST_SPEEX_ENC (benc); if (!enc->header_sent) { /* Speex streams begin with two headers; the initial header (with @@ -981,24 +683,20 @@ gst_speex_enc_chain (GstPad * pad, GstBuffer * buf) /* create header buffer */ data = (guint8 *) speex_header_to_packet (&enc->header, &data_len); - buf1 = gst_speex_enc_buffer_from_data (enc, data, data_len, 0); - free (data); + buf1 = gst_buffer_new_wrapped (data, data_len); + GST_BUFFER_OFFSET_END (buf1) = 0; + GST_BUFFER_OFFSET (buf1) = 0; /* create comment buffer */ buf2 = gst_speex_enc_create_metadata_buffer (enc); /* mark and put on caps */ - caps = gst_pad_get_caps (enc->srcpad, NULL); - caps = gst_speex_enc_set_header_on_caps (caps, buf1, buf2); - - gst_caps_set_simple (caps, - "rate", G_TYPE_INT, enc->rate, + caps = gst_caps_new_simple ("audio/x-speex", "rate", G_TYPE_INT, enc->rate, "channels", G_TYPE_INT, enc->channels, NULL); + caps = _gst_caps_set_buffer_array (caps, "streamheader", buf1, buf2, NULL); /* negotiate with these caps */ GST_DEBUG_OBJECT (enc, "here are the caps: %" GST_PTR_FORMAT, caps); - gst_pad_set_caps (enc->srcpad, caps); - gst_caps_unref (caps); /* push out buffers */ ret = gst_speex_enc_push_buffer (enc, buf1); @@ -1013,102 +711,16 @@ gst_speex_enc_chain (GstPad * pad, GstBuffer * buf) if (ret != GST_FLOW_OK) goto done; - speex_bits_reset (&enc->bits); - enc->header_sent = TRUE; } - /* Save the timestamp of the first buffer. This will be later - * used as offset for all following buffers */ - if (enc->start_ts == GST_CLOCK_TIME_NONE) { - if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) { - enc->start_ts = GST_BUFFER_TIMESTAMP (buf); - enc->granulepos_offset = gst_util_uint64_scale - (GST_BUFFER_TIMESTAMP (buf), enc->rate, GST_SECOND); - } else { - enc->start_ts = 0; - enc->granulepos_offset = 0; - } - } - - /* Check if we have a continous stream, if not drop some samples or the buffer or - * insert some silence samples */ - if (enc->next_ts != GST_CLOCK_TIME_NONE && - GST_BUFFER_TIMESTAMP_IS_VALID (buf) && - GST_BUFFER_TIMESTAMP (buf) < enc->next_ts) { - guint64 diff = enc->next_ts - GST_BUFFER_TIMESTAMP (buf); - guint64 diff_bytes; - - GST_WARNING_OBJECT (enc, "Buffer is older than previous " - "timestamp + duration (%" GST_TIME_FORMAT "< %" GST_TIME_FORMAT - "), cannot handle. Clipping buffer.", - GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), - GST_TIME_ARGS (enc->next_ts)); - - diff_bytes = GST_CLOCK_TIME_TO_FRAMES (diff, enc->rate) * enc->channels * 2; - if (diff_bytes >= gst_buffer_get_size (buf)) { - gst_buffer_unref (buf); - return GST_FLOW_OK; - } - buf = gst_buffer_make_writable (buf); - gst_buffer_resize (buf, diff_bytes, -1); - - GST_BUFFER_TIMESTAMP (buf) += diff; - if (GST_BUFFER_DURATION_IS_VALID (buf)) - GST_BUFFER_DURATION (buf) -= diff; - } + GST_DEBUG_OBJECT (enc, "received buffer %p of %u bytes", buf, + buf ? gst_buffer_get_size (buf) : 0); - if (enc->next_ts != GST_CLOCK_TIME_NONE - && GST_BUFFER_TIMESTAMP_IS_VALID (buf)) { - guint64 max_diff = - gst_util_uint64_scale (enc->frame_size, GST_SECOND, enc->rate); - - if (GST_BUFFER_TIMESTAMP (buf) != enc->next_ts && - GST_BUFFER_TIMESTAMP (buf) - enc->next_ts > max_diff) { - GST_WARNING_OBJECT (enc, - "Discontinuity detected: %" G_GUINT64_FORMAT " > %" G_GUINT64_FORMAT, - GST_BUFFER_TIMESTAMP (buf) - enc->next_ts, max_diff); - - gst_speex_enc_encode (enc, TRUE); - - enc->frameno_out = 0; - enc->start_ts = GST_BUFFER_TIMESTAMP (buf); - enc->granulepos_offset = gst_util_uint64_scale - (GST_BUFFER_TIMESTAMP (buf), enc->rate, GST_SECOND); - } - } - - if (GST_BUFFER_TIMESTAMP_IS_VALID (buf) - && GST_BUFFER_DURATION_IS_VALID (buf)) - enc->next_ts = GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf); - else - enc->next_ts = GST_CLOCK_TIME_NONE; - - GST_DEBUG_OBJECT (enc, "received buffer of %u bytes", - gst_buffer_get_size (buf)); - - /* push buffer to adapter */ - gst_adapter_push (enc->adapter, buf); - buf = NULL; - - ret = gst_speex_enc_encode (enc, FALSE); + ret = gst_speex_enc_encode (enc, buf); done: - - if (buf) - gst_buffer_unref (buf); - return ret; - - /* ERRORS */ -not_setup: - { - GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL), - ("encoder not initialized (input is not audio?)")); - ret = GST_FLOW_NOT_NEGOTIATED; - goto done; - } - } @@ -1198,54 +810,3 @@ gst_speex_enc_set_property (GObject * object, guint prop_id, break; } } - -static GstStateChangeReturn -gst_speex_enc_change_state (GstElement * element, GstStateChange transition) -{ - GstSpeexEnc *enc = GST_SPEEX_ENC (element); - GstStateChangeReturn res; - - switch (transition) { - case GST_STATE_CHANGE_NULL_TO_READY: - enc->tags = gst_tag_list_new (); - break; - case GST_STATE_CHANGE_READY_TO_PAUSED: - speex_bits_init (&enc->bits); - enc->frameno = 0; - enc->frameno_out = 0; - enc->samples_in = 0; - enc->start_ts = GST_CLOCK_TIME_NONE; - enc->next_ts = GST_CLOCK_TIME_NONE; - enc->granulepos_offset = 0; - break; - case GST_STATE_CHANGE_PAUSED_TO_PLAYING: - /* fall through */ - default: - break; - } - - res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); - if (res == GST_STATE_CHANGE_FAILURE) - return res; - - switch (transition) { - case GST_STATE_CHANGE_PLAYING_TO_PAUSED: - break; - case GST_STATE_CHANGE_PAUSED_TO_READY: - enc->setup = FALSE; - enc->header_sent = FALSE; - if (enc->state) { - speex_encoder_destroy (enc->state); - enc->state = NULL; - } - speex_bits_destroy (&enc->bits); - break; - case GST_STATE_CHANGE_READY_TO_NULL: - gst_tag_list_free (enc->tags); - enc->tags = NULL; - default: - break; - } - - return res; -} diff --git a/ext/speex/gstspeexenc.h b/ext/speex/gstspeexenc.h index 6de9ebd45..17cb1e2df 100644 --- a/ext/speex/gstspeexenc.h +++ b/ext/speex/gstspeexenc.h @@ -23,7 +23,7 @@ #include <gst/gst.h> -#include <gst/base/gstadapter.h> +#include <gst/audio/gstaudioencoder.h> #include <speex/speex.h> #include <speex/speex_header.h> @@ -53,14 +53,7 @@ typedef struct _GstSpeexEnc GstSpeexEnc; typedef struct _GstSpeexEncClass GstSpeexEncClass; struct _GstSpeexEnc { - GstElement element; - - /* pads */ - GstPad *sinkpad, - *srcpad; - - gint packet_count; - gint n_packets; + GstAudioEncoder element; SpeexBits bits; SpeexHeader header; @@ -70,9 +63,9 @@ struct _GstSpeexEnc { const SpeexMode *speex_mode; #endif void *state; - GstSpeexMode mode; - GstAdapter *adapter; + /* properties */ + GstSpeexMode mode; gfloat quality; gint bitrate; gboolean vbr; @@ -81,40 +74,24 @@ struct _GstSpeexEnc { gboolean dtx; gint complexity; gint nframes; - - gint lookahead; + gchar *last_message; gint channels; gint rate; - gboolean setup; gboolean header_sent; - guint64 samples_in; - guint64 bytes_out; - GstTagList *tags; - gchar *last_message; - gint frame_size; - guint64 frameno; - guint64 frameno_out; + gint lookahead; guint8 *comments; gint comment_len; - - /* Timestamp and granulepos tracking */ - GstClockTime start_ts; - GstClockTime next_ts; - guint64 granulepos_offset; }; struct _GstSpeexEncClass { - GstElementClass parent_class; - - /* signals */ - void (*frame_encoded) (GstElement *element); + GstAudioEncoderClass parent_class; }; GType gst_speex_enc_get_type (void); |