2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include "gstspeexdec.h"
26 #include <gst/tag/tag.h>
28 GST_DEBUG_CATEGORY (speexdec_debug);
29 #define GST_CAT_DEFAULT speexdec_debug
31 static GstElementDetails speex_dec_details = {
33 "Codec/Decoder/Audio",
34 "decode speex streams to audio",
35 "Wim Taymans <wim@fluendo.com>",
38 /* Filter signals and args */
45 #define DEFAULT_ENH TRUE
53 static GstStaticPadTemplate speex_dec_src_factory =
54 GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS ("audio/x-raw-int, "
58 "rate = (int) [ 6000, 48000 ], "
59 "channels = (int) [ 1, 2 ], "
60 "endianness = (int) BYTE_ORDER, "
61 "signed = (boolean) true, " "width = (int) 16, " "depth = (int) 16")
64 static GstStaticPadTemplate speex_dec_sink_factory =
65 GST_STATIC_PAD_TEMPLATE ("sink",
68 GST_STATIC_CAPS ("audio/x-speex")
71 GST_BOILERPLATE (GstSpeexDec, gst_speex_dec, GstElement, GST_TYPE_ELEMENT);
73 static void speex_dec_chain (GstPad * pad, GstData * data);
74 static GstElementStateReturn speex_dec_change_state (GstElement * element);
75 static const GstFormat *speex_dec_get_formats (GstPad * pad);
77 static gboolean speex_dec_src_event (GstPad * pad, GstEvent * event);
78 static gboolean speex_dec_src_query (GstPad * pad,
79 GstQueryType query, GstFormat * format, gint64 * value);
80 static gboolean speex_dec_convert (GstPad * pad,
81 GstFormat src_format, gint64 src_value,
82 GstFormat * dest_format, gint64 * dest_value);
84 static void gst_speexdec_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
86 static void gst_speexdec_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
90 gst_speex_dec_base_init (gpointer g_class)
92 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
94 gst_element_class_add_pad_template (element_class,
95 gst_static_pad_template_get (&speex_dec_src_factory));
96 gst_element_class_add_pad_template (element_class,
97 gst_static_pad_template_get (&speex_dec_sink_factory));
98 gst_element_class_set_details (element_class, &speex_dec_details);
102 gst_speex_dec_class_init (GstSpeexDecClass * klass)
104 GObjectClass *gobject_class;
105 GstElementClass *gstelement_class;
107 gobject_class = (GObjectClass *) klass;
108 gstelement_class = (GstElementClass *) klass;
110 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ENH,
111 g_param_spec_boolean ("enh", "Enh", "Enable perceptual enhancement",
112 DEFAULT_ENH, G_PARAM_READWRITE));
114 gstelement_class->change_state = speex_dec_change_state;
116 gobject_class->set_property = gst_speexdec_set_property;
117 gobject_class->get_property = gst_speexdec_get_property;
119 GST_DEBUG_CATEGORY_INIT (speexdec_debug, "speexdec", 0,
120 "speex decoding element");
123 static const GstFormat *
124 speex_dec_get_formats (GstPad * pad)
126 static GstFormat src_formats[] = {
128 GST_FORMAT_DEFAULT, /* samples in the audio case */
132 static GstFormat sink_formats[] = {
135 GST_FORMAT_DEFAULT, /* samples */
139 return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
142 static const GstEventMask *
143 speex_get_event_masks (GstPad * pad)
145 static const GstEventMask speex_dec_src_event_masks[] = {
146 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
150 return speex_dec_src_event_masks;
153 static const GstQueryType *
154 speex_get_query_types (GstPad * pad)
156 static const GstQueryType speex_dec_src_query_types[] = {
162 return speex_dec_src_query_types;
166 gst_speex_dec_init (GstSpeexDec * dec)
169 gst_pad_new_from_template (gst_static_pad_template_get
170 (&speex_dec_sink_factory), "sink");
171 gst_pad_set_chain_function (dec->sinkpad, speex_dec_chain);
172 gst_pad_set_formats_function (dec->sinkpad, speex_dec_get_formats);
173 gst_pad_set_convert_function (dec->sinkpad, speex_dec_convert);
174 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
177 gst_pad_new_from_template (gst_static_pad_template_get
178 (&speex_dec_src_factory), "src");
179 gst_pad_use_explicit_caps (dec->srcpad);
180 gst_pad_set_event_mask_function (dec->srcpad, speex_get_event_masks);
181 gst_pad_set_event_function (dec->srcpad, speex_dec_src_event);
182 gst_pad_set_query_type_function (dec->srcpad, speex_get_query_types);
183 gst_pad_set_query_function (dec->srcpad, speex_dec_src_query);
184 gst_pad_set_formats_function (dec->srcpad, speex_dec_get_formats);
185 gst_pad_set_convert_function (dec->srcpad, speex_dec_convert);
186 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
188 dec->enh = DEFAULT_ENH;
190 GST_FLAG_SET (dec, GST_ELEMENT_EVENT_AWARE);
194 speex_dec_convert (GstPad * pad,
195 GstFormat src_format, gint64 src_value,
196 GstFormat * dest_format, gint64 * dest_value)
202 dec = GST_SPEEXDEC (gst_pad_get_parent (pad));
204 if (dec->packetno < 1)
207 if (pad == dec->sinkpad &&
208 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
211 switch (src_format) {
212 case GST_FORMAT_TIME:
213 switch (*dest_format) {
214 case GST_FORMAT_BYTES:
215 scale = sizeof (float) * dec->header->nb_channels;
216 case GST_FORMAT_DEFAULT:
217 *dest_value = scale * (src_value * dec->header->rate / GST_SECOND);
223 case GST_FORMAT_DEFAULT:
224 switch (*dest_format) {
225 case GST_FORMAT_BYTES:
226 *dest_value = src_value * sizeof (float) * dec->header->nb_channels;
228 case GST_FORMAT_TIME:
229 *dest_value = src_value * GST_SECOND / dec->header->rate;
235 case GST_FORMAT_BYTES:
236 switch (*dest_format) {
237 case GST_FORMAT_DEFAULT:
238 *dest_value = src_value / (sizeof (float) * dec->header->nb_channels);
240 case GST_FORMAT_TIME:
241 *dest_value = src_value * GST_SECOND /
242 (dec->header->rate * sizeof (float) * dec->header->nb_channels);
256 speex_dec_src_query (GstPad * pad, GstQueryType query, GstFormat * format,
259 gint64 samples_out = 0;
260 GstSpeexDec *dec = GST_SPEEXDEC (gst_pad_get_parent (pad));
261 GstFormat my_format = GST_FORMAT_DEFAULT;
263 if (query == GST_QUERY_POSITION) {
264 samples_out = dec->samples_out;
266 /* query peer in default format */
267 if (!gst_pad_query (GST_PAD_PEER (dec->sinkpad), query, &my_format,
272 /* and convert to the final format */
273 if (!gst_pad_convert (pad, GST_FORMAT_DEFAULT, samples_out, format, value))
277 "query %u: peer returned samples_out: %llu - we return %llu (format %u)\n",
278 query, samples_out, *value, *format);
283 speex_dec_src_event (GstPad * pad, GstEvent * event)
286 GstSpeexDec *dec = GST_SPEEXDEC (gst_pad_get_parent (pad));
288 switch (GST_EVENT_TYPE (event)) {
289 case GST_EVENT_SEEK:{
291 GstFormat my_format = GST_FORMAT_DEFAULT;
293 /* convert to samples_out */
294 res = speex_dec_convert (pad, GST_EVENT_SEEK_FORMAT (event),
295 GST_EVENT_SEEK_OFFSET (event), &my_format, &value);
297 GstEvent *real_seek = gst_event_new_seek (
298 (GST_EVENT_SEEK_TYPE (event) & ~GST_SEEK_FORMAT_MASK) |
302 res = gst_pad_send_event (GST_PAD_PEER (dec->sinkpad), real_seek);
304 gst_event_unref (event);
308 res = gst_pad_event_default (pad, event);
316 speex_dec_event (GstSpeexDec * dec, GstEvent * event)
318 guint64 value, time, bytes;
320 GST_LOG_OBJECT (dec, "handling event");
321 switch (GST_EVENT_TYPE (event)) {
322 case GST_EVENT_DISCONTINUOUS:
323 if (gst_event_discont_get_value (event, GST_FORMAT_DEFAULT,
324 (gint64 *) & value)) {
325 dec->samples_out = value;
326 GST_DEBUG_OBJECT (dec,
327 "setting samples_out to %" G_GUINT64_FORMAT " after discont",
330 GST_WARNING_OBJECT (dec,
331 "discont event didn't include offset, we might set it wrong now");
333 if (dec->packetno < 2) {
334 if (dec->samples_out != 0)
335 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL),
336 ("can't handle discont before parsing first 2 packets"));
338 gst_pad_push (dec->srcpad, GST_DATA (gst_event_new_discontinuous (FALSE,
339 GST_FORMAT_TIME, (guint64) 0, GST_FORMAT_DEFAULT,
340 (guint64) 0, GST_FORMAT_BYTES, (guint64) 0, 0)));
342 GstFormat time_format, default_format, bytes_format;
344 time_format = GST_FORMAT_TIME;
345 default_format = GST_FORMAT_DEFAULT;
346 bytes_format = GST_FORMAT_BYTES;
349 /* if one of them works, all of them work */
350 if (speex_dec_convert (dec->srcpad, GST_FORMAT_DEFAULT,
351 dec->samples_out, &time_format, &time)
352 && speex_dec_convert (dec->srcpad, GST_FORMAT_DEFAULT,
353 dec->samples_out, &bytes_format, &bytes)) {
354 gst_pad_push (dec->srcpad,
355 GST_DATA (gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME,
356 time, GST_FORMAT_DEFAULT, dec->samples_out,
357 GST_FORMAT_BYTES, bytes, 0)));
359 GST_ERROR_OBJECT (dec,
360 "failed to parse data for DISCONT event, not sending any");
363 gst_data_unref (GST_DATA (event));
366 gst_pad_event_default (dec->sinkpad, event);
372 speex_dec_chain (GstPad * pad, GstData * data)
377 dec = GST_SPEEXDEC (gst_pad_get_parent (pad));
378 if (GST_IS_EVENT (data)) {
379 speex_dec_event (dec, GST_EVENT (data));
383 buf = GST_BUFFER (data);
385 if (dec->packetno == 0) {
390 speex_packet_to_header (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
391 gst_data_unref (data);
393 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
394 (NULL), ("couldn't read header"));
397 if (dec->header->mode >= SPEEX_NB_MODES) {
398 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
400 ("Mode number %d does not (yet/any longer) exist in this version",
405 dec->mode = (SpeexMode *) speex_mode_list[dec->header->mode];
407 /* initialize the decoder */
408 dec->state = speex_decoder_init (dec->mode);
410 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
411 (NULL), ("couldn't initialize decoder"));
412 gst_data_unref (data);
416 speex_decoder_ctl (dec->state, SPEEX_SET_ENH, &dec->enh);
417 speex_decoder_ctl (dec->state, SPEEX_GET_FRAME_SIZE, &dec->frame_size);
419 if (dec->header->nb_channels != 1) {
420 dec->callback.callback_id = SPEEX_INBAND_STEREO;
421 dec->callback.func = speex_std_stereo_request_handler;
422 dec->callback.data = &dec->stereo;
423 speex_decoder_ctl (dec->state, SPEEX_SET_HANDLER, &dec->callback);
426 speex_decoder_ctl (dec->state, SPEEX_SET_SAMPLING_RATE, &dec->header->rate);
428 speex_bits_init (&dec->bits);
431 caps = gst_caps_new_simple ("audio/x-raw-int",
432 "rate", G_TYPE_INT, dec->header->rate,
433 "channels", G_TYPE_INT, dec->header->nb_channels,
434 "signed", G_TYPE_BOOLEAN, TRUE,
435 "endianness", G_TYPE_INT, G_BYTE_ORDER,
436 "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
438 if (!gst_pad_set_explicit_caps (dec->srcpad, caps)) {
439 gst_caps_free (caps);
442 gst_caps_free (caps);
443 } else if (dec->packetno == 1) {
444 gchar *encoder = NULL;
446 /* FIXME parse comments */
447 GstTagList *list = gst_tag_list_from_vorbiscomment_buffer (buf, "", 1,
450 gst_data_unref (data);
453 GST_WARNING_OBJECT (dec, "couldn't decode comments");
454 list = gst_tag_list_new ();
457 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
458 GST_TAG_ENCODER, encoder, NULL);
461 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
462 GST_TAG_AUDIO_CODEC, "Speex", NULL);
464 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
465 GST_TAG_ENCODER_VERSION, dec->vi.version, NULL);
467 if (dec->vi.bitrate_upper > 0)
468 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
469 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
470 if (vd->vi.bitrate_nominal > 0)
471 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
472 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
473 if (vd->vi.bitrate_lower > 0)
474 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
475 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
477 gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, 0, list);
481 /* send data to the bitstream */
482 speex_bits_read_from (&dec->bits, GST_BUFFER_DATA (buf),
483 GST_BUFFER_SIZE (buf));
484 gst_data_unref (data);
486 /* now decode each frame */
487 for (i = 0; i < dec->header->frames_per_packet; i++) {
492 ret = speex_decode (dec->state, &dec->bits, dec->output);
494 /* uh? end of stream */
495 GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
497 } else if (ret == -2) {
498 GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?");
501 if (speex_bits_remaining (&dec->bits) < 0) {
502 GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?");
505 if (dec->header->nb_channels == 2)
506 speex_decode_stereo (dec->output, dec->frame_size, &dec->stereo);
508 outbuf = gst_pad_alloc_buffer (dec->srcpad, GST_BUFFER_OFFSET_NONE,
509 dec->frame_size * dec->header->nb_channels * 2);
510 out_data = (gint16 *) GST_BUFFER_DATA (outbuf);
512 /*PCM saturation (just in case) */
513 for (i = 0; i < dec->frame_size * dec->header->nb_channels; i++) {
514 if (dec->output[i] > 32767.0)
516 else if (dec->output[i] < -32768.0)
517 out_data[i] = -32768;
519 out_data[i] = (gint16) dec->output[i];
522 GST_BUFFER_OFFSET (outbuf) = dec->samples_out;
523 GST_BUFFER_OFFSET_END (outbuf) = dec->samples_out + dec->frame_size;
524 GST_BUFFER_TIMESTAMP (outbuf) =
525 dec->samples_out * GST_SECOND / dec->header->rate;
526 GST_BUFFER_DURATION (outbuf) =
527 dec->frame_size * GST_SECOND / dec->header->rate;
528 gst_pad_push (dec->srcpad, GST_DATA (outbuf));
529 dec->samples_out += dec->frame_size;
536 gst_speexdec_get_property (GObject * object, guint prop_id,
537 GValue * value, GParamSpec * pspec)
539 GstSpeexDec *speexdec;
541 g_return_if_fail (GST_IS_SPEEXDEC (object));
543 speexdec = GST_SPEEXDEC (object);
547 g_value_set_boolean (value, speexdec->enh);
550 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
556 gst_speexdec_set_property (GObject * object, guint prop_id,
557 const GValue * value, GParamSpec * pspec)
559 GstSpeexDec *speexdec;
561 g_return_if_fail (GST_IS_SPEEXDEC (object));
563 speexdec = GST_SPEEXDEC (object);
567 speexdec->enh = g_value_get_boolean (value);
570 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
576 static GstElementStateReturn
577 speex_dec_change_state (GstElement * element)
579 GstSpeexDec *vd = GST_SPEEXDEC (element);
581 switch (GST_STATE_TRANSITION (element)) {
582 case GST_STATE_NULL_TO_READY:
584 case GST_STATE_READY_TO_PAUSED:
586 case GST_STATE_PAUSED_TO_PLAYING:
588 case GST_STATE_PLAYING_TO_PAUSED:
590 case GST_STATE_PAUSED_TO_READY:
594 case GST_STATE_READY_TO_NULL:
600 return parent_class->change_state (element);