2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-speexdec
23 * @see_also: speexenc, oggdemux
25 * This element decodes a Speex stream to raw integer audio.
26 * <ulink url="http://www.speex.org/">Speex</ulink> is a royalty-free
27 * audio codec maintained by the <ulink url="http://www.xiph.org/">Xiph.org
31 * <title>Example pipelines</title>
33 * gst-launch -v filesrc location=speex.ogg ! oggdemux ! speexdec ! audioconvert ! audioresample ! alsasink
34 * ]| Decode an Ogg/Speex file. To create an Ogg/Speex file refer to the
35 * documentation of speexenc.
38 * Last reviewed on 2006-04-05 (0.10.2)
45 #include "gstspeexdec.h"
48 #include <gst/tag/tag.h>
50 GST_DEBUG_CATEGORY_STATIC (speexdec_debug);
51 #define GST_CAT_DEFAULT speexdec_debug
53 #define DEFAULT_ENH TRUE
61 static GstStaticPadTemplate speex_dec_src_factory =
62 GST_STATIC_PAD_TEMPLATE ("src",
65 GST_STATIC_CAPS ("audio/x-raw-int, "
66 "rate = (int) [ 6000, 48000 ], "
67 "channels = (int) [ 1, 2 ], "
68 "endianness = (int) BYTE_ORDER, "
69 "signed = (boolean) true, " "width = (int) 16, " "depth = (int) 16")
72 static GstStaticPadTemplate speex_dec_sink_factory =
73 GST_STATIC_PAD_TEMPLATE ("sink",
76 GST_STATIC_CAPS ("audio/x-speex")
79 GST_BOILERPLATE (GstSpeexDec, gst_speex_dec, GstElement, GST_TYPE_ELEMENT);
81 static gboolean speex_dec_sink_event (GstPad * pad, GstEvent * event);
82 static GstFlowReturn speex_dec_chain (GstPad * pad, GstBuffer * buf);
83 static GstStateChangeReturn speex_dec_change_state (GstElement * element,
84 GstStateChange transition);
86 static gboolean speex_dec_src_event (GstPad * pad, GstEvent * event);
87 static gboolean speex_dec_src_query (GstPad * pad, GstQuery * query);
88 static gboolean speex_dec_sink_query (GstPad * pad, GstQuery * query);
89 static const GstQueryType *speex_get_src_query_types (GstPad * pad);
90 static const GstQueryType *speex_get_sink_query_types (GstPad * pad);
91 static gboolean speex_dec_convert (GstPad * pad,
92 GstFormat src_format, gint64 src_value,
93 GstFormat * dest_format, gint64 * dest_value);
95 static void gst_speex_dec_get_property (GObject * object, guint prop_id,
96 GValue * value, GParamSpec * pspec);
97 static void gst_speex_dec_set_property (GObject * object, guint prop_id,
98 const GValue * value, GParamSpec * pspec);
100 static GstFlowReturn speex_dec_chain_parse_data (GstSpeexDec * dec,
101 GstBuffer * buf, GstClockTime timestamp, GstClockTime duration);
104 gst_speex_dec_base_init (gpointer g_class)
106 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
108 gst_element_class_add_pad_template (element_class,
109 gst_static_pad_template_get (&speex_dec_src_factory));
110 gst_element_class_add_pad_template (element_class,
111 gst_static_pad_template_get (&speex_dec_sink_factory));
112 gst_element_class_set_details_simple (element_class, "Speex audio decoder",
113 "Codec/Decoder/Audio",
114 "decode speex streams to audio", "Wim Taymans <wim@fluendo.com>");
118 gst_speex_dec_class_init (GstSpeexDecClass * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
123 gobject_class = (GObjectClass *) klass;
124 gstelement_class = (GstElementClass *) klass;
126 gobject_class->set_property = gst_speex_dec_set_property;
127 gobject_class->get_property = gst_speex_dec_get_property;
129 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ENH,
130 g_param_spec_boolean ("enh", "Enh", "Enable perceptual enhancement",
131 DEFAULT_ENH, G_PARAM_READWRITE));
133 gstelement_class->change_state = GST_DEBUG_FUNCPTR (speex_dec_change_state);
135 GST_DEBUG_CATEGORY_INIT (speexdec_debug, "speexdec", 0,
136 "speex decoding element");
140 gst_speex_dec_reset (GstSpeexDec * dec)
142 gst_segment_init (&dec->segment, GST_FORMAT_UNDEFINED);
145 dec->frame_duration = 0;
149 speex_bits_destroy (&dec->bits);
152 speex_stereo_state_destroy (dec->stereo);
157 speex_decoder_destroy (dec->state);
163 gst_speex_dec_init (GstSpeexDec * dec, GstSpeexDecClass * g_class)
166 gst_pad_new_from_static_template (&speex_dec_sink_factory, "sink");
167 gst_pad_set_chain_function (dec->sinkpad,
168 GST_DEBUG_FUNCPTR (speex_dec_chain));
169 gst_pad_set_event_function (dec->sinkpad,
170 GST_DEBUG_FUNCPTR (speex_dec_sink_event));
171 gst_pad_set_query_type_function (dec->sinkpad,
172 GST_DEBUG_FUNCPTR (speex_get_sink_query_types));
173 gst_pad_set_query_function (dec->sinkpad,
174 GST_DEBUG_FUNCPTR (speex_dec_sink_query));
175 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
178 gst_pad_new_from_static_template (&speex_dec_src_factory, "src");
179 gst_pad_use_fixed_caps (dec->srcpad);
180 gst_pad_set_event_function (dec->srcpad,
181 GST_DEBUG_FUNCPTR (speex_dec_src_event));
182 gst_pad_set_query_type_function (dec->srcpad,
183 GST_DEBUG_FUNCPTR (speex_get_src_query_types));
184 gst_pad_set_query_function (dec->srcpad,
185 GST_DEBUG_FUNCPTR (speex_dec_src_query));
186 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
188 dec->enh = DEFAULT_ENH;
190 gst_speex_dec_reset (dec);
194 speex_dec_convert (GstPad * pad,
195 GstFormat src_format, gint64 src_value,
196 GstFormat * dest_format, gint64 * dest_value)
202 dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
204 if (src_format == *dest_format) {
205 *dest_value = src_value;
210 if (dec->packetno < 1) {
215 if (pad == dec->sinkpad &&
216 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES)) {
221 switch (src_format) {
222 case GST_FORMAT_TIME:
223 switch (*dest_format) {
224 case GST_FORMAT_BYTES:
225 scale = 2 * dec->header->nb_channels;
226 case GST_FORMAT_DEFAULT:
228 gst_util_uint64_scale_int (scale * src_value, dec->header->rate,
236 case GST_FORMAT_DEFAULT:
237 switch (*dest_format) {
238 case GST_FORMAT_BYTES:
239 *dest_value = src_value * 2 * dec->header->nb_channels;
241 case GST_FORMAT_TIME:
243 gst_util_uint64_scale_int (src_value, GST_SECOND,
251 case GST_FORMAT_BYTES:
252 switch (*dest_format) {
253 case GST_FORMAT_DEFAULT:
254 *dest_value = src_value / (2 * dec->header->nb_channels);
256 case GST_FORMAT_TIME:
257 *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
258 dec->header->rate * 2 * dec->header->nb_channels);
271 gst_object_unref (dec);
275 static const GstQueryType *
276 speex_get_sink_query_types (GstPad * pad)
278 static const GstQueryType speex_dec_sink_query_types[] = {
283 return speex_dec_sink_query_types;
287 speex_dec_sink_query (GstPad * pad, GstQuery * query)
292 dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
294 switch (GST_QUERY_TYPE (query)) {
295 case GST_QUERY_CONVERT:
297 GstFormat src_fmt, dest_fmt;
298 gint64 src_val, dest_val;
300 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
301 res = speex_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val);
303 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
308 res = gst_pad_query_default (pad, query);
312 gst_object_unref (dec);
316 static const GstQueryType *
317 speex_get_src_query_types (GstPad * pad)
319 static const GstQueryType speex_dec_src_query_types[] = {
325 return speex_dec_src_query_types;
329 speex_dec_src_query (GstPad * pad, GstQuery * query)
332 gboolean res = FALSE;
334 dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
336 switch (GST_QUERY_TYPE (query)) {
337 case GST_QUERY_POSITION:{
342 gst_query_parse_position (query, &format, NULL);
344 GST_PAD_STREAM_LOCK (dec->sinkpad);
345 segment = dec->segment;
346 GST_PAD_STREAM_UNLOCK (dec->sinkpad);
348 if (segment.format != GST_FORMAT_TIME) {
349 GST_DEBUG_OBJECT (dec, "segment not initialised yet");
353 if ((res = speex_dec_convert (dec->srcpad, GST_FORMAT_TIME,
354 segment.last_stop, &format, &cur))) {
355 gst_query_set_position (query, format, cur);
359 case GST_QUERY_DURATION:{
360 GstFormat format = GST_FORMAT_TIME;
363 /* get duration from demuxer */
364 if (!gst_pad_query_peer_duration (dec->sinkpad, &format, &dur))
367 gst_query_parse_duration (query, &format, NULL);
369 /* and convert it into the requested format */
370 if ((res = speex_dec_convert (dec->srcpad, GST_FORMAT_TIME,
371 dur, &format, &dur))) {
372 gst_query_set_duration (query, format, dur);
377 res = gst_pad_query_default (pad, query);
381 gst_object_unref (dec);
386 speex_dec_src_event (GstPad * pad, GstEvent * event)
388 gboolean res = FALSE;
389 GstSpeexDec *dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
391 GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
393 switch (GST_EVENT_TYPE (event)) {
394 case GST_EVENT_SEEK:{
395 GstFormat format, tformat;
399 GstSeekType cur_type, stop_type;
403 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
406 /* we have to ask our peer to seek to time here as we know
407 * nothing about how to generate a granulepos from the src
408 * formats or anything.
410 * First bring the requested format to time
412 tformat = GST_FORMAT_TIME;
413 if (!(res = speex_dec_convert (pad, format, cur, &tformat, &tcur)))
415 if (!(res = speex_dec_convert (pad, format, stop, &tformat, &tstop)))
418 /* then seek with time on the peer */
419 real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
420 flags, cur_type, tcur, stop_type, tstop);
422 GST_LOG_OBJECT (dec, "seek to %" GST_TIME_FORMAT, GST_TIME_ARGS (tcur));
424 res = gst_pad_push_event (dec->sinkpad, real_seek);
425 gst_event_unref (event);
429 res = gst_pad_event_default (pad, event);
433 gst_object_unref (dec);
438 speex_dec_sink_event (GstPad * pad, GstEvent * event)
441 gboolean ret = FALSE;
443 dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
445 GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
447 switch (GST_EVENT_TYPE (event)) {
448 case GST_EVENT_NEWSEGMENT:{
451 gint64 start, stop, time;
454 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
455 &start, &stop, &time);
457 if (format != GST_FORMAT_TIME)
458 goto newseg_wrong_format;
461 goto newseg_wrong_rate;
464 /* time progressed without data, see if we can fill the gap with
465 * some concealment data */
466 if (dec->segment.last_stop < start) {
467 GstClockTime duration;
469 duration = start - dec->segment.last_stop;
470 speex_dec_chain_parse_data (dec, NULL, dec->segment.last_stop,
475 /* now configure the values */
476 gst_segment_set_newsegment_full (&dec->segment, update,
477 rate, arate, GST_FORMAT_TIME, start, stop, time);
479 GST_DEBUG_OBJECT (dec, "segment now: cur = %" GST_TIME_FORMAT " [%"
480 GST_TIME_FORMAT " - %" GST_TIME_FORMAT "]",
481 GST_TIME_ARGS (dec->segment.last_stop),
482 GST_TIME_ARGS (dec->segment.start),
483 GST_TIME_ARGS (dec->segment.stop));
485 ret = gst_pad_push_event (dec->srcpad, event);
489 ret = gst_pad_event_default (pad, event);
493 gst_object_unref (dec);
499 GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
500 gst_object_unref (dec);
505 GST_DEBUG_OBJECT (dec, "negative rates not supported yet");
506 gst_object_unref (dec);
512 speex_dec_chain_parse_header (GstSpeexDec * dec, GstBuffer * buf)
517 dec->header = speex_packet_to_header ((char *) GST_BUFFER_DATA (buf),
518 GST_BUFFER_SIZE (buf));
523 if (dec->header->mode >= SPEEX_NB_MODES || dec->header->mode < 0)
526 dec->mode = speex_lib_get_mode (dec->header->mode);
528 /* initialize the decoder */
529 dec->state = speex_decoder_init (dec->mode);
533 speex_decoder_ctl (dec->state, SPEEX_SET_ENH, &dec->enh);
534 speex_decoder_ctl (dec->state, SPEEX_GET_FRAME_SIZE, &dec->frame_size);
536 if (dec->header->nb_channels != 1) {
537 dec->stereo = speex_stereo_state_init ();
538 dec->callback.callback_id = SPEEX_INBAND_STEREO;
539 dec->callback.func = speex_std_stereo_request_handler;
540 dec->callback.data = dec->stereo;
541 speex_decoder_ctl (dec->state, SPEEX_SET_HANDLER, &dec->callback);
544 speex_decoder_ctl (dec->state, SPEEX_SET_SAMPLING_RATE, &dec->header->rate);
546 dec->frame_duration = gst_util_uint64_scale_int (dec->frame_size,
547 GST_SECOND, dec->header->rate);
549 speex_bits_init (&dec->bits);
552 caps = gst_caps_new_simple ("audio/x-raw-int",
553 "rate", G_TYPE_INT, dec->header->rate,
554 "channels", G_TYPE_INT, dec->header->nb_channels,
555 "signed", G_TYPE_BOOLEAN, TRUE,
556 "endianness", G_TYPE_INT, G_BYTE_ORDER,
557 "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
559 if (!gst_pad_set_caps (dec->srcpad, caps))
562 gst_caps_unref (caps);
568 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
569 (NULL), ("couldn't read header"));
570 return GST_FLOW_ERROR;
574 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
576 ("Mode number %d does not (yet/any longer) exist in this version",
578 return GST_FLOW_ERROR;
582 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
583 (NULL), ("couldn't initialize decoder"));
584 return GST_FLOW_ERROR;
588 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
589 (NULL), ("couldn't negotiate format"));
590 gst_caps_unref (caps);
591 return GST_FLOW_NOT_NEGOTIATED;
596 speex_dec_chain_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
599 gchar *ver, *encoder = NULL;
601 list = gst_tag_list_from_vorbiscomment_buffer (buf, NULL, 0, &encoder);
604 GST_WARNING_OBJECT (dec, "couldn't decode comments");
605 list = gst_tag_list_new ();
609 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
610 GST_TAG_ENCODER, encoder, NULL);
613 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
614 GST_TAG_AUDIO_CODEC, "Speex", NULL);
616 ver = g_strndup (dec->header->speex_version, SPEEX_HEADER_VERSION_LENGTH);
619 if (ver != NULL && *ver != '\0') {
620 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
621 GST_TAG_ENCODER_VERSION, ver, NULL);
624 if (dec->header->bitrate > 0) {
625 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
626 GST_TAG_BITRATE, (guint) dec->header->bitrate, NULL);
629 GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
631 gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, list);
640 speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf,
641 GstClockTime timestamp, GstClockTime duration)
643 GstFlowReturn res = GST_FLOW_OK;
649 if (!dec->frame_duration)
652 if (timestamp != -1) {
653 dec->segment.last_stop = timestamp;
655 timestamp = dec->segment.last_stop;
659 data = GST_BUFFER_DATA (buf);
660 size = GST_BUFFER_SIZE (buf);
662 /* send data to the bitstream */
663 speex_bits_read_from (&dec->bits, (char *) data, size);
668 GST_DEBUG_OBJECT (dec, "received buffer of size %u, fpp %d", size, fpp);
670 /* concealment data, pass NULL as the bits parameters */
671 GST_DEBUG_OBJECT (dec, "creating concealment data");
672 fpp = dec->header->frames_per_packet;
677 /* now decode each frame, catering for unknown number of them (e.g. rtp) */
678 for (i = 0; (!fpp || i < fpp) && (!bits || speex_bits_remaining (bits) > 0);
684 GST_LOG_OBJECT (dec, "decoding frame %d/%d", i, fpp);
686 res = gst_pad_alloc_buffer_and_set_caps (dec->srcpad,
687 GST_BUFFER_OFFSET_NONE, dec->frame_size * dec->header->nb_channels * 2,
688 GST_PAD_CAPS (dec->srcpad), &outbuf);
690 if (res != GST_FLOW_OK) {
691 GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
695 out_data = (gint16 *) GST_BUFFER_DATA (outbuf);
697 ret = speex_decode_int (dec->state, bits, out_data);
699 /* uh? end of stream */
700 GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
701 gst_buffer_unref (outbuf);
704 } else if (ret == -2) {
705 GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?");
706 gst_buffer_unref (outbuf);
711 if (bits && speex_bits_remaining (bits) < 0) {
712 GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?");
713 gst_buffer_unref (outbuf);
717 if (dec->header->nb_channels == 2)
718 speex_decode_stereo_int (out_data, dec->frame_size, dec->stereo);
720 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
721 GST_BUFFER_DURATION (outbuf) = dec->frame_duration;
723 dec->segment.last_stop += dec->frame_duration;
724 timestamp = dec->segment.last_stop;
726 GST_LOG_OBJECT (dec, "pushing buffer with ts=%" GST_TIME_FORMAT ", dur=%"
727 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
728 GST_TIME_ARGS (dec->frame_duration));
730 res = gst_pad_push (dec->srcpad, outbuf);
732 if (res != GST_FLOW_OK) {
733 GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
743 GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
744 ("decoder not initialized"));
745 return GST_FLOW_NOT_NEGOTIATED;
750 speex_dec_chain (GstPad * pad, GstBuffer * buf)
755 dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
757 switch (dec->packetno) {
759 res = speex_dec_chain_parse_header (dec, buf);
762 res = speex_dec_chain_parse_comments (dec, buf);
767 speex_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
768 GST_BUFFER_DURATION (buf));
775 gst_buffer_unref (buf);
776 gst_object_unref (dec);
782 gst_speex_dec_get_property (GObject * object, guint prop_id,
783 GValue * value, GParamSpec * pspec)
785 GstSpeexDec *speexdec;
787 speexdec = GST_SPEEX_DEC (object);
791 g_value_set_boolean (value, speexdec->enh);
794 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
800 gst_speex_dec_set_property (GObject * object, guint prop_id,
801 const GValue * value, GParamSpec * pspec)
803 GstSpeexDec *speexdec;
805 speexdec = GST_SPEEX_DEC (object);
809 speexdec->enh = g_value_get_boolean (value);
812 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
818 static GstStateChangeReturn
819 speex_dec_change_state (GstElement * element, GstStateChange transition)
821 GstStateChangeReturn ret;
822 GstSpeexDec *dec = GST_SPEEX_DEC (element);
824 switch (transition) {
825 case GST_STATE_CHANGE_NULL_TO_READY:
826 case GST_STATE_CHANGE_READY_TO_PAUSED:
827 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
832 ret = parent_class->change_state (element, transition);
833 if (ret != GST_STATE_CHANGE_SUCCESS)
836 switch (transition) {
837 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
839 case GST_STATE_CHANGE_PAUSED_TO_READY:
840 gst_speex_dec_reset (dec);
842 case GST_STATE_CHANGE_READY_TO_NULL: