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, GstAudioDecoder,
80 GST_TYPE_AUDIO_DECODER);
83 static gboolean gst_speex_dec_start (GstAudioDecoder * dec);
84 static gboolean gst_speex_dec_stop (GstAudioDecoder * dec);
85 static gboolean gst_speex_dec_set_format (GstAudioDecoder * bdec,
87 static GstFlowReturn gst_speex_dec_handle_frame (GstAudioDecoder * dec,
90 static void gst_speex_dec_get_property (GObject * object, guint prop_id,
91 GValue * value, GParamSpec * pspec);
92 static void gst_speex_dec_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec);
96 gst_speex_dec_base_init (gpointer g_class)
98 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
100 gst_element_class_add_static_pad_template (element_class,
101 &speex_dec_src_factory);
102 gst_element_class_add_static_pad_template (element_class,
103 &speex_dec_sink_factory);
104 gst_element_class_set_details_simple (element_class, "Speex audio decoder",
105 "Codec/Decoder/Audio",
106 "decode speex streams to audio", "Wim Taymans <wim@fluendo.com>");
110 gst_speex_dec_class_init (GstSpeexDecClass * klass)
112 GObjectClass *gobject_class;
113 GstAudioDecoderClass *base_class;
115 gobject_class = (GObjectClass *) klass;
116 base_class = (GstAudioDecoderClass *) klass;
118 gobject_class->set_property = gst_speex_dec_set_property;
119 gobject_class->get_property = gst_speex_dec_get_property;
121 base_class->start = GST_DEBUG_FUNCPTR (gst_speex_dec_start);
122 base_class->stop = GST_DEBUG_FUNCPTR (gst_speex_dec_stop);
123 base_class->set_format = GST_DEBUG_FUNCPTR (gst_speex_dec_set_format);
124 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_speex_dec_handle_frame);
126 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ENH,
127 g_param_spec_boolean ("enh", "Enh", "Enable perceptual enhancement",
128 DEFAULT_ENH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
130 GST_DEBUG_CATEGORY_INIT (speexdec_debug, "speexdec", 0,
131 "speex decoding element");
135 gst_speex_dec_reset (GstSpeexDec * dec)
139 dec->frame_duration = 0;
143 speex_bits_destroy (&dec->bits);
145 gst_buffer_replace (&dec->streamheader, NULL);
146 gst_buffer_replace (&dec->vorbiscomment, NULL);
149 speex_stereo_state_destroy (dec->stereo);
154 speex_decoder_destroy (dec->state);
160 gst_speex_dec_init (GstSpeexDec * dec, GstSpeexDecClass * g_class)
162 dec->enh = DEFAULT_ENH;
164 gst_speex_dec_reset (dec);
168 gst_speex_dec_start (GstAudioDecoder * dec)
170 GstSpeexDec *sd = GST_SPEEX_DEC (dec);
172 GST_DEBUG_OBJECT (dec, "start");
173 gst_speex_dec_reset (sd);
175 /* we know about concealment */
176 gst_audio_decoder_set_plc_aware (dec, TRUE);
182 gst_speex_dec_stop (GstAudioDecoder * dec)
184 GstSpeexDec *sd = GST_SPEEX_DEC (dec);
186 GST_DEBUG_OBJECT (dec, "stop");
187 gst_speex_dec_reset (sd);
193 gst_speex_dec_parse_header (GstSpeexDec * dec, GstBuffer * buf)
198 dec->header = speex_packet_to_header ((char *) GST_BUFFER_DATA (buf),
199 GST_BUFFER_SIZE (buf));
204 if (dec->header->mode >= SPEEX_NB_MODES || dec->header->mode < 0)
207 dec->mode = speex_lib_get_mode (dec->header->mode);
209 /* initialize the decoder */
210 dec->state = speex_decoder_init (dec->mode);
214 speex_decoder_ctl (dec->state, SPEEX_SET_ENH, &dec->enh);
215 speex_decoder_ctl (dec->state, SPEEX_GET_FRAME_SIZE, &dec->frame_size);
217 if (dec->header->nb_channels != 1) {
218 dec->stereo = speex_stereo_state_init ();
219 dec->callback.callback_id = SPEEX_INBAND_STEREO;
220 dec->callback.func = speex_std_stereo_request_handler;
221 dec->callback.data = dec->stereo;
222 speex_decoder_ctl (dec->state, SPEEX_SET_HANDLER, &dec->callback);
225 speex_decoder_ctl (dec->state, SPEEX_SET_SAMPLING_RATE, &dec->header->rate);
227 dec->frame_duration = gst_util_uint64_scale_int (dec->frame_size,
228 GST_SECOND, dec->header->rate);
230 speex_bits_init (&dec->bits);
233 caps = gst_caps_new_simple ("audio/x-raw-int",
234 "rate", G_TYPE_INT, dec->header->rate,
235 "channels", G_TYPE_INT, dec->header->nb_channels,
236 "signed", G_TYPE_BOOLEAN, TRUE,
237 "endianness", G_TYPE_INT, G_BYTE_ORDER,
238 "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
240 if (!gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps))
243 gst_caps_unref (caps);
249 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
250 (NULL), ("couldn't read header"));
251 return GST_FLOW_ERROR;
255 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
257 ("Mode number %d does not (yet/any longer) exist in this version",
259 return GST_FLOW_ERROR;
263 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
264 (NULL), ("couldn't initialize decoder"));
265 return GST_FLOW_ERROR;
269 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
270 (NULL), ("couldn't negotiate format"));
271 gst_caps_unref (caps);
272 return GST_FLOW_NOT_NEGOTIATED;
277 gst_speex_dec_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
280 gchar *ver, *encoder = NULL;
282 list = gst_tag_list_from_vorbiscomment_buffer (buf, NULL, 0, &encoder);
285 GST_WARNING_OBJECT (dec, "couldn't decode comments");
286 list = gst_tag_list_new ();
290 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
291 GST_TAG_ENCODER, encoder, NULL);
294 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
295 GST_TAG_AUDIO_CODEC, "Speex", NULL);
297 ver = g_strndup (dec->header->speex_version, SPEEX_HEADER_VERSION_LENGTH);
300 if (ver != NULL && *ver != '\0') {
301 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
302 GST_TAG_ENCODER_VERSION, ver, NULL);
305 if (dec->header->bitrate > 0) {
306 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
307 GST_TAG_BITRATE, (guint) dec->header->bitrate, NULL);
310 GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
312 gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (dec), list,
313 GST_TAG_MERGE_REPLACE);
314 gst_tag_list_free (list);
323 gst_speex_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
325 GstSpeexDec *dec = GST_SPEEX_DEC (bdec);
328 const GValue *streamheader;
330 s = gst_caps_get_structure (caps, 0);
331 if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
332 G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
333 gst_value_array_get_size (streamheader) >= 2) {
334 const GValue *header, *vorbiscomment;
336 GstFlowReturn res = GST_FLOW_OK;
338 header = gst_value_array_get_value (streamheader, 0);
339 if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
340 buf = gst_value_get_buffer (header);
341 res = gst_speex_dec_parse_header (dec, buf);
342 if (res != GST_FLOW_OK)
344 gst_buffer_replace (&dec->streamheader, buf);
347 vorbiscomment = gst_value_array_get_value (streamheader, 1);
348 if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
349 buf = gst_value_get_buffer (vorbiscomment);
350 res = gst_speex_dec_parse_comments (dec, buf);
351 if (res != GST_FLOW_OK)
353 gst_buffer_replace (&dec->vorbiscomment, buf);
362 gst_speex_dec_parse_data (GstSpeexDec * dec, GstBuffer * buf)
364 GstFlowReturn res = GST_FLOW_OK;
370 if (!dec->frame_duration)
373 if (G_LIKELY (GST_BUFFER_SIZE (buf))) {
374 data = GST_BUFFER_DATA (buf);
375 size = GST_BUFFER_SIZE (buf);
377 /* send data to the bitstream */
378 speex_bits_read_from (&dec->bits, (char *) data, size);
380 fpp = dec->header->frames_per_packet;
383 GST_DEBUG_OBJECT (dec, "received buffer of size %u, fpp %d, %d bits",
384 size, fpp, speex_bits_remaining (bits));
386 /* FIXME ? actually consider how much concealment is needed */
387 /* concealment data, pass NULL as the bits parameters */
388 GST_DEBUG_OBJECT (dec, "creating concealment data");
389 fpp = dec->header->frames_per_packet;
393 /* now decode each frame, catering for unknown number of them (e.g. rtp) */
394 for (i = 0; i < fpp; i++) {
399 GST_LOG_OBJECT (dec, "decoding frame %d/%d, %d bits remaining", i, fpp,
400 bits ? speex_bits_remaining (bits) : -1);
403 gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec),
404 GST_BUFFER_OFFSET_NONE, dec->frame_size * dec->header->nb_channels * 2,
405 GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
407 if (res != GST_FLOW_OK) {
408 GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
412 out_data = (gint16 *) GST_BUFFER_DATA (outbuf);
414 ret = speex_decode_int (dec->state, bits, out_data);
416 /* uh? end of stream */
417 if (fpp == 0 && speex_bits_remaining (bits) < 8) {
418 /* if we did not know how many frames to expect, then we get this
419 at the end if there are leftover bits to pad to the next byte */
420 GST_DEBUG_OBJECT (dec, "Discarding leftover bits");
422 GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
424 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
425 gst_buffer_unref (outbuf);
426 } else if (ret == -2) {
427 GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?");
428 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
429 gst_buffer_unref (outbuf);
432 if (bits && speex_bits_remaining (bits) < 0) {
433 GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?");
434 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
435 gst_buffer_unref (outbuf);
437 if (dec->header->nb_channels == 2)
438 speex_decode_stereo_int (out_data, dec->frame_size, dec->stereo);
440 res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
442 if (res != GST_FLOW_OK) {
443 GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
453 GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
454 ("decoder not initialized"));
455 return GST_FLOW_NOT_NEGOTIATED;
460 gst_speex_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
465 /* no fancy draining */
466 if (G_UNLIKELY (!buf))
469 dec = GST_SPEEX_DEC (bdec);
471 /* If we have the streamheader and vorbiscomment from the caps already
472 * ignore them here */
473 if (dec->streamheader && dec->vorbiscomment) {
474 if (GST_BUFFER_SIZE (dec->streamheader) == GST_BUFFER_SIZE (buf)
475 && memcmp (GST_BUFFER_DATA (dec->streamheader), GST_BUFFER_DATA (buf),
476 GST_BUFFER_SIZE (buf)) == 0) {
477 GST_DEBUG_OBJECT (dec, "found streamheader");
478 gst_audio_decoder_finish_frame (bdec, NULL, 1);
480 } else if (GST_BUFFER_SIZE (dec->vorbiscomment) == GST_BUFFER_SIZE (buf)
481 && memcmp (GST_BUFFER_DATA (dec->vorbiscomment), GST_BUFFER_DATA (buf),
482 GST_BUFFER_SIZE (buf)) == 0) {
483 GST_DEBUG_OBJECT (dec, "found vorbiscomments");
484 gst_audio_decoder_finish_frame (bdec, NULL, 1);
487 res = gst_speex_dec_parse_data (dec, buf);
490 /* Otherwise fall back to packet counting and assume that the
491 * first two packets are the headers. */
492 switch (dec->packetno) {
494 GST_DEBUG_OBJECT (dec, "counted streamheader");
495 res = gst_speex_dec_parse_header (dec, buf);
496 gst_audio_decoder_finish_frame (bdec, NULL, 1);
499 GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
500 res = gst_speex_dec_parse_comments (dec, buf);
501 gst_audio_decoder_finish_frame (bdec, NULL, 1);
505 res = gst_speex_dec_parse_data (dec, buf);
517 gst_speex_dec_get_property (GObject * object, guint prop_id,
518 GValue * value, GParamSpec * pspec)
520 GstSpeexDec *speexdec;
522 speexdec = GST_SPEEX_DEC (object);
526 g_value_set_boolean (value, speexdec->enh);
529 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
535 gst_speex_dec_set_property (GObject * object, guint prop_id,
536 const GValue * value, GParamSpec * pspec)
538 GstSpeexDec *speexdec;
540 speexdec = GST_SPEEX_DEC (object);
544 speexdec->enh = g_value_get_boolean (value);
547 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);