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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, 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-1.0 -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.
43 #include "gstspeexdec.h"
46 #include <gst/tag/tag.h>
47 #include <gst/audio/audio.h>
49 GST_DEBUG_CATEGORY_STATIC (speexdec_debug);
50 #define GST_CAT_DEFAULT speexdec_debug
52 #define DEFAULT_ENH TRUE
60 #define FORMAT_STR GST_AUDIO_NE(S16)
62 static GstStaticPadTemplate speex_dec_src_factory =
63 GST_STATIC_PAD_TEMPLATE ("src",
66 GST_STATIC_CAPS ("audio/x-raw, "
67 "format = (string) " FORMAT_STR ", "
68 "layout = (string) interleaved, "
69 "rate = (int) [ 6000, 48000 ], " "channels = (int) [ 1, 2 ]")
72 static GstStaticPadTemplate speex_dec_sink_factory =
73 GST_STATIC_PAD_TEMPLATE ("sink",
76 GST_STATIC_CAPS ("audio/x-speex")
79 #define gst_speex_dec_parent_class parent_class
80 G_DEFINE_TYPE (GstSpeexDec, gst_speex_dec, GST_TYPE_AUDIO_DECODER);
82 static gboolean gst_speex_dec_start (GstAudioDecoder * dec);
83 static gboolean gst_speex_dec_stop (GstAudioDecoder * dec);
84 static gboolean gst_speex_dec_set_format (GstAudioDecoder * bdec,
86 static GstFlowReturn gst_speex_dec_handle_frame (GstAudioDecoder * dec,
89 static void gst_speex_dec_get_property (GObject * object, guint prop_id,
90 GValue * value, GParamSpec * pspec);
91 static void gst_speex_dec_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
95 gst_speex_dec_class_init (GstSpeexDecClass * klass)
97 GObjectClass *gobject_class;
98 GstElementClass *gstelement_class;
99 GstAudioDecoderClass *base_class;
101 gobject_class = (GObjectClass *) klass;
102 gstelement_class = (GstElementClass *) klass;
103 base_class = (GstAudioDecoderClass *) klass;
105 gobject_class->set_property = gst_speex_dec_set_property;
106 gobject_class->get_property = gst_speex_dec_get_property;
108 base_class->start = GST_DEBUG_FUNCPTR (gst_speex_dec_start);
109 base_class->stop = GST_DEBUG_FUNCPTR (gst_speex_dec_stop);
110 base_class->set_format = GST_DEBUG_FUNCPTR (gst_speex_dec_set_format);
111 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_speex_dec_handle_frame);
113 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ENH,
114 g_param_spec_boolean ("enh", "Enh", "Enable perceptual enhancement",
115 DEFAULT_ENH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
117 gst_element_class_add_pad_template (gstelement_class,
118 gst_static_pad_template_get (&speex_dec_src_factory));
119 gst_element_class_add_pad_template (gstelement_class,
120 gst_static_pad_template_get (&speex_dec_sink_factory));
121 gst_element_class_set_static_metadata (gstelement_class,
122 "Speex audio decoder", "Codec/Decoder/Audio",
123 "decode speex streams to audio", "Wim Taymans <wim@fluendo.com>");
125 GST_DEBUG_CATEGORY_INIT (speexdec_debug, "speexdec", 0,
126 "speex decoding element");
130 gst_speex_dec_reset (GstSpeexDec * dec)
134 dec->frame_duration = 0;
138 speex_bits_destroy (&dec->bits);
139 speex_bits_set_bit_buffer (&dec->bits, NULL, 0);
141 gst_buffer_replace (&dec->streamheader, NULL);
142 gst_buffer_replace (&dec->vorbiscomment, NULL);
145 speex_stereo_state_destroy (dec->stereo);
150 speex_decoder_destroy (dec->state);
156 gst_speex_dec_init (GstSpeexDec * dec)
158 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE);
160 dec->enh = DEFAULT_ENH;
162 gst_speex_dec_reset (dec);
166 gst_speex_dec_start (GstAudioDecoder * dec)
168 GstSpeexDec *sd = GST_SPEEX_DEC (dec);
170 GST_DEBUG_OBJECT (dec, "start");
171 gst_speex_dec_reset (sd);
173 /* we know about concealment */
174 gst_audio_decoder_set_plc_aware (dec, TRUE);
180 gst_speex_dec_stop (GstAudioDecoder * dec)
182 GstSpeexDec *sd = GST_SPEEX_DEC (dec);
184 GST_DEBUG_OBJECT (dec, "stop");
185 gst_speex_dec_reset (sd);
191 gst_speex_dec_parse_header (GstSpeexDec * dec, GstBuffer * buf)
195 static const GstAudioChannelPosition chan_pos[2][2] = {
196 {GST_AUDIO_CHANNEL_POSITION_MONO},
197 {GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
198 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}
202 gst_buffer_map (buf, &map, GST_MAP_READ);
203 dec->header = speex_packet_to_header ((gchar *) map.data, map.size);
204 gst_buffer_unmap (buf, &map);
209 if (dec->header->mode >= SPEEX_NB_MODES || dec->header->mode < 0)
212 dec->mode = speex_lib_get_mode (dec->header->mode);
214 /* initialize the decoder */
215 dec->state = speex_decoder_init (dec->mode);
219 speex_decoder_ctl (dec->state, SPEEX_SET_ENH, &dec->enh);
220 speex_decoder_ctl (dec->state, SPEEX_GET_FRAME_SIZE, &dec->frame_size);
222 if (dec->header->nb_channels != 1) {
223 dec->stereo = speex_stereo_state_init ();
224 dec->callback.callback_id = SPEEX_INBAND_STEREO;
225 dec->callback.func = speex_std_stereo_request_handler;
226 dec->callback.data = dec->stereo;
227 speex_decoder_ctl (dec->state, SPEEX_SET_HANDLER, &dec->callback);
230 speex_decoder_ctl (dec->state, SPEEX_SET_SAMPLING_RATE, &dec->header->rate);
232 dec->frame_duration = gst_util_uint64_scale_int (dec->frame_size,
233 GST_SECOND, dec->header->rate);
235 speex_bits_init (&dec->bits);
238 gst_audio_info_init (&info);
239 gst_audio_info_set_format (&info,
240 GST_AUDIO_FORMAT_S16,
242 dec->header->nb_channels, chan_pos[dec->header->nb_channels - 1]);
244 if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dec), &info))
252 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
253 (NULL), ("couldn't read header"));
254 return GST_FLOW_ERROR;
258 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
260 ("Mode number %d does not (yet/any longer) exist in this version",
262 return GST_FLOW_ERROR;
266 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
267 (NULL), ("couldn't initialize decoder"));
268 return GST_FLOW_ERROR;
272 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
273 (NULL), ("couldn't negotiate format"));
274 return GST_FLOW_NOT_NEGOTIATED;
279 gst_speex_dec_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
282 gchar *ver, *encoder = NULL;
284 list = gst_tag_list_from_vorbiscomment_buffer (buf, NULL, 0, &encoder);
287 GST_WARNING_OBJECT (dec, "couldn't decode comments");
288 list = gst_tag_list_new_empty ();
292 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
293 GST_TAG_ENCODER, encoder, NULL);
296 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
297 GST_TAG_AUDIO_CODEC, "Speex", NULL);
299 ver = g_strndup (dec->header->speex_version, SPEEX_HEADER_VERSION_LENGTH);
302 if (ver != NULL && *ver != '\0') {
303 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
304 GST_TAG_ENCODER_VERSION, ver, NULL);
307 if (dec->header->bitrate > 0) {
308 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
309 GST_TAG_BITRATE, (guint) dec->header->bitrate, NULL);
312 GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
314 gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (dec), list,
315 GST_TAG_MERGE_REPLACE);
316 gst_tag_list_unref (list);
325 gst_speex_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
327 GstSpeexDec *dec = GST_SPEEX_DEC (bdec);
330 const GValue *streamheader;
332 s = gst_caps_get_structure (caps, 0);
333 if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
334 G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
335 gst_value_array_get_size (streamheader) >= 2) {
336 const GValue *header, *vorbiscomment;
338 GstFlowReturn res = GST_FLOW_OK;
340 header = gst_value_array_get_value (streamheader, 0);
341 if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
342 buf = gst_value_get_buffer (header);
343 res = gst_speex_dec_parse_header (dec, buf);
344 if (res != GST_FLOW_OK)
346 gst_buffer_replace (&dec->streamheader, buf);
349 vorbiscomment = gst_value_array_get_value (streamheader, 1);
350 if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
351 buf = gst_value_get_buffer (vorbiscomment);
352 res = gst_speex_dec_parse_comments (dec, buf);
353 if (res != GST_FLOW_OK)
355 gst_buffer_replace (&dec->vorbiscomment, buf);
364 gst_speex_dec_parse_data (GstSpeexDec * dec, GstBuffer * buf)
366 GstFlowReturn res = GST_FLOW_OK;
371 if (!dec->frame_duration)
374 if (G_LIKELY (gst_buffer_get_size (buf))) {
375 /* send data to the bitstream */
376 gst_buffer_map (buf, &map, GST_MAP_READ);
377 speex_bits_read_from (&dec->bits, (gchar *) map.data, map.size);
378 gst_buffer_unmap (buf, &map);
380 fpp = dec->header->frames_per_packet;
383 GST_DEBUG_OBJECT (dec, "received buffer of size %" G_GSIZE_FORMAT
384 ", fpp %d, %d bits", map.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++) {
396 gboolean corrupted = FALSE;
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 /* FIXME, we can use a bufferpool because we have fixed size buffers. We
413 * could also use an allocator */
415 gst_buffer_new_allocate (NULL,
416 dec->frame_size * dec->header->nb_channels * 2, NULL);
418 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
419 ret = speex_decode_int (dec->state, bits, (spx_int16_t *) map.data);
422 /* uh? end of stream */
423 GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
425 } else if (ret == -2) {
426 GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?");
430 if (bits && speex_bits_remaining (bits) < 0) {
431 GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?");
434 if (dec->header->nb_channels == 2)
435 speex_decode_stereo_int ((spx_int16_t *) map.data, dec->frame_size,
438 gst_buffer_unmap (outbuf, &map);
441 res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
443 res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
444 gst_buffer_unref (outbuf);
447 if (res != GST_FLOW_OK) {
448 GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
458 GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
459 ("decoder not initialized"));
460 return GST_FLOW_NOT_NEGOTIATED;
465 memcmp_buffers (GstBuffer * buf1, GstBuffer * buf2)
471 size1 = gst_buffer_get_size (buf1);
472 size2 = gst_buffer_get_size (buf2);
477 gst_buffer_map (buf1, &map, GST_MAP_READ);
478 res = gst_buffer_memcmp (buf2, 0, map.data, map.size) == 0;
479 gst_buffer_unmap (buf1, &map);
485 gst_speex_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
490 /* no fancy draining */
491 if (G_UNLIKELY (!buf))
494 dec = GST_SPEEX_DEC (bdec);
496 /* If we have the streamheader and vorbiscomment from the caps already
497 * ignore them here */
498 if (dec->streamheader && dec->vorbiscomment) {
499 if (memcmp_buffers (dec->streamheader, buf)) {
500 GST_DEBUG_OBJECT (dec, "found streamheader");
501 gst_audio_decoder_finish_frame (bdec, NULL, 1);
503 } else if (memcmp_buffers (dec->vorbiscomment, buf)) {
504 GST_DEBUG_OBJECT (dec, "found vorbiscomments");
505 gst_audio_decoder_finish_frame (bdec, NULL, 1);
508 res = gst_speex_dec_parse_data (dec, buf);
511 /* Otherwise fall back to packet counting and assume that the
512 * first two packets are the headers. */
513 switch (dec->packetno) {
515 GST_DEBUG_OBJECT (dec, "counted streamheader");
516 res = gst_speex_dec_parse_header (dec, buf);
517 gst_audio_decoder_finish_frame (bdec, NULL, 1);
520 GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
521 res = gst_speex_dec_parse_comments (dec, buf);
522 gst_audio_decoder_finish_frame (bdec, NULL, 1);
526 res = gst_speex_dec_parse_data (dec, buf);
538 gst_speex_dec_get_property (GObject * object, guint prop_id,
539 GValue * value, GParamSpec * pspec)
541 GstSpeexDec *speexdec;
543 speexdec = GST_SPEEX_DEC (object);
547 g_value_set_boolean (value, speexdec->enh);
550 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
556 gst_speex_dec_set_property (GObject * object, guint prop_id,
557 const GValue * value, GParamSpec * pspec)
559 GstSpeexDec *speexdec;
561 speexdec = GST_SPEEX_DEC (object);
565 speexdec->enh = g_value_get_boolean (value);
568 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);