• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4  *
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.
9  *
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.
14  *
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.
19  */
20 
21 /**
22  * SECTION:element-speexdec
23  * @title: speexdec
24  * @see_also: speexenc, oggdemux
25  *
26  * This element decodes a Speex stream to raw integer audio.
27  * [Speex](http://www.speex.org/) is a royalty-free
28  * audio codec maintained by the [Xiph.org Foundation](http://www.xiph.org/).
29  *
30  * ## Example pipelines
31  * |[
32  * gst-launch-1.0 -v filesrc location=speex.ogg ! oggdemux ! speexdec ! audioconvert ! audioresample ! alsasink
33  * ]| Decode an Ogg/Speex file. To create an Ogg/Speex file refer to the
34  * documentation of speexenc.
35  *
36  */
37 
38 #ifdef HAVE_CONFIG_H
39 #  include "config.h"
40 #endif
41 
42 #include "gstspeexelements.h"
43 #include "gstspeexdec.h"
44 #include <stdlib.h>
45 #include <string.h>
46 #include <gst/tag/tag.h>
47 #include <gst/audio/audio.h>
48 
49 GST_DEBUG_CATEGORY_STATIC (speexdec_debug);
50 #define GST_CAT_DEFAULT speexdec_debug
51 
52 #define DEFAULT_ENH   TRUE
53 
54 enum
55 {
56   ARG_0,
57   ARG_ENH
58 };
59 
60 #define FORMAT_STR GST_AUDIO_NE(S16)
61 
62 static GstStaticPadTemplate speex_dec_src_factory =
63 GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
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 ]")
70     );
71 
72 static GstStaticPadTemplate speex_dec_sink_factory =
73 GST_STATIC_PAD_TEMPLATE ("sink",
74     GST_PAD_SINK,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS ("audio/x-speex")
77     );
78 
79 #define gst_speex_dec_parent_class parent_class
80 G_DEFINE_TYPE (GstSpeexDec, gst_speex_dec, GST_TYPE_AUDIO_DECODER);
81 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (speexdec, "speexdec",
82     GST_RANK_PRIMARY, GST_TYPE_SPEEX_DEC, speex_element_init (plugin));
83 
84 static gboolean gst_speex_dec_start (GstAudioDecoder * dec);
85 static gboolean gst_speex_dec_stop (GstAudioDecoder * dec);
86 static gboolean gst_speex_dec_set_format (GstAudioDecoder * bdec,
87     GstCaps * caps);
88 static GstFlowReturn gst_speex_dec_handle_frame (GstAudioDecoder * dec,
89     GstBuffer * buffer);
90 
91 static void gst_speex_dec_get_property (GObject * object, guint prop_id,
92     GValue * value, GParamSpec * pspec);
93 static void gst_speex_dec_set_property (GObject * object, guint prop_id,
94     const GValue * value, GParamSpec * pspec);
95 
96 static void
gst_speex_dec_class_init(GstSpeexDecClass * klass)97 gst_speex_dec_class_init (GstSpeexDecClass * klass)
98 {
99   GObjectClass *gobject_class;
100   GstElementClass *gstelement_class;
101   GstAudioDecoderClass *base_class;
102 
103   gobject_class = (GObjectClass *) klass;
104   gstelement_class = (GstElementClass *) klass;
105   base_class = (GstAudioDecoderClass *) klass;
106 
107   gobject_class->set_property = gst_speex_dec_set_property;
108   gobject_class->get_property = gst_speex_dec_get_property;
109 
110   base_class->start = GST_DEBUG_FUNCPTR (gst_speex_dec_start);
111   base_class->stop = GST_DEBUG_FUNCPTR (gst_speex_dec_stop);
112   base_class->set_format = GST_DEBUG_FUNCPTR (gst_speex_dec_set_format);
113   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_speex_dec_handle_frame);
114 
115   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ENH,
116       g_param_spec_boolean ("enh", "Enh", "Enable perceptual enhancement",
117           DEFAULT_ENH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
118 
119   gst_element_class_add_static_pad_template (gstelement_class,
120       &speex_dec_src_factory);
121   gst_element_class_add_static_pad_template (gstelement_class,
122       &speex_dec_sink_factory);
123   gst_element_class_set_static_metadata (gstelement_class,
124       "Speex audio decoder", "Codec/Decoder/Audio",
125       "decode speex streams to audio", "Wim Taymans <wim@fluendo.com>");
126 
127   GST_DEBUG_CATEGORY_INIT (speexdec_debug, "speexdec", 0,
128       "speex decoding element");
129 }
130 
131 static void
gst_speex_dec_reset(GstSpeexDec * dec)132 gst_speex_dec_reset (GstSpeexDec * dec)
133 {
134   dec->packetno = 0;
135   dec->frame_size = 0;
136   dec->frame_duration = 0;
137   dec->mode = NULL;
138   speex_header_free (dec->header);
139   dec->header = NULL;
140   speex_bits_destroy (&dec->bits);
141   speex_bits_set_bit_buffer (&dec->bits, NULL, 0);
142 
143   gst_buffer_replace (&dec->streamheader, NULL);
144   gst_buffer_replace (&dec->vorbiscomment, NULL);
145 
146   if (dec->stereo) {
147     speex_stereo_state_destroy (dec->stereo);
148     dec->stereo = NULL;
149   }
150 
151   if (dec->state) {
152     speex_decoder_destroy (dec->state);
153     dec->state = NULL;
154   }
155 }
156 
157 static void
gst_speex_dec_init(GstSpeexDec * dec)158 gst_speex_dec_init (GstSpeexDec * dec)
159 {
160   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE);
161   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
162       (dec), TRUE);
163   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec));
164 
165   dec->enh = DEFAULT_ENH;
166 
167   gst_speex_dec_reset (dec);
168 }
169 
170 static gboolean
gst_speex_dec_start(GstAudioDecoder * dec)171 gst_speex_dec_start (GstAudioDecoder * dec)
172 {
173   GstSpeexDec *sd = GST_SPEEX_DEC (dec);
174 
175   GST_DEBUG_OBJECT (dec, "start");
176   gst_speex_dec_reset (sd);
177 
178   /* we know about concealment */
179   gst_audio_decoder_set_plc_aware (dec, TRUE);
180 
181   return TRUE;
182 }
183 
184 static gboolean
gst_speex_dec_stop(GstAudioDecoder * dec)185 gst_speex_dec_stop (GstAudioDecoder * dec)
186 {
187   GstSpeexDec *sd = GST_SPEEX_DEC (dec);
188 
189   GST_DEBUG_OBJECT (dec, "stop");
190   gst_speex_dec_reset (sd);
191 
192   return TRUE;
193 }
194 
195 static GstFlowReturn
gst_speex_dec_parse_header(GstSpeexDec * dec,GstBuffer * buf)196 gst_speex_dec_parse_header (GstSpeexDec * dec, GstBuffer * buf)
197 {
198   GstMapInfo map;
199   GstAudioInfo info;
200   static const GstAudioChannelPosition chan_pos[2][2] = {
201     {GST_AUDIO_CHANNEL_POSITION_MONO},
202     {GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
203         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}
204   };
205 
206   /* get the header */
207   gst_buffer_map (buf, &map, GST_MAP_READ);
208   dec->header = speex_packet_to_header ((gchar *) map.data, map.size);
209   gst_buffer_unmap (buf, &map);
210 
211   if (!dec->header)
212     goto no_header;
213 
214   if (dec->header->mode >= SPEEX_NB_MODES || dec->header->mode < 0)
215     goto mode_too_old;
216 
217   dec->mode = speex_lib_get_mode (dec->header->mode);
218 
219   /* initialize the decoder */
220   dec->state = speex_decoder_init (dec->mode);
221   if (!dec->state)
222     goto init_failed;
223 
224   speex_decoder_ctl (dec->state, SPEEX_SET_ENH, &dec->enh);
225   speex_decoder_ctl (dec->state, SPEEX_GET_FRAME_SIZE, &dec->frame_size);
226 
227   if (dec->header->nb_channels != 1) {
228     dec->stereo = speex_stereo_state_init ();
229     dec->callback.callback_id = SPEEX_INBAND_STEREO;
230     dec->callback.func = speex_std_stereo_request_handler;
231     dec->callback.data = dec->stereo;
232     speex_decoder_ctl (dec->state, SPEEX_SET_HANDLER, &dec->callback);
233   }
234 
235   speex_decoder_ctl (dec->state, SPEEX_SET_SAMPLING_RATE, &dec->header->rate);
236 
237   dec->frame_duration = gst_util_uint64_scale_int (dec->frame_size,
238       GST_SECOND, dec->header->rate);
239 
240   speex_bits_init (&dec->bits);
241 
242   /* set caps */
243   gst_audio_info_init (&info);
244   gst_audio_info_set_format (&info,
245       GST_AUDIO_FORMAT_S16,
246       dec->header->rate,
247       dec->header->nb_channels, chan_pos[dec->header->nb_channels - 1]);
248 
249   if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dec), &info))
250     goto nego_failed;
251 
252   return GST_FLOW_OK;
253 
254   /* ERRORS */
255 no_header:
256   {
257     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
258         (NULL), ("couldn't read header"));
259     return GST_FLOW_ERROR;
260   }
261 mode_too_old:
262   {
263     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
264         (NULL),
265         ("Mode number %d does not (yet/any longer) exist in this version",
266             dec->header->mode));
267     return GST_FLOW_ERROR;
268   }
269 init_failed:
270   {
271     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
272         (NULL), ("couldn't initialize decoder"));
273     return GST_FLOW_ERROR;
274   }
275 nego_failed:
276   {
277     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
278         (NULL), ("couldn't negotiate format"));
279     return GST_FLOW_NOT_NEGOTIATED;
280   }
281 }
282 
283 static GstFlowReturn
gst_speex_dec_parse_comments(GstSpeexDec * dec,GstBuffer * buf)284 gst_speex_dec_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
285 {
286   GstTagList *list;
287   gchar *ver, *encoder = NULL;
288 
289   list = gst_tag_list_from_vorbiscomment_buffer (buf, NULL, 0, &encoder);
290 
291   if (!list) {
292     GST_WARNING_OBJECT (dec, "couldn't decode comments");
293     list = gst_tag_list_new_empty ();
294   }
295 
296   if (encoder) {
297     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
298         GST_TAG_ENCODER, encoder, NULL);
299   }
300 
301   gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
302       GST_TAG_AUDIO_CODEC, "Speex", NULL);
303 
304   ver = g_strndup (dec->header->speex_version, SPEEX_HEADER_VERSION_LENGTH);
305   g_strstrip (ver);
306 
307   if (ver != NULL && *ver != '\0') {
308     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
309         GST_TAG_ENCODER_VERSION, ver, NULL);
310   }
311 
312   if (dec->header->bitrate > 0) {
313     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
314         GST_TAG_BITRATE, (guint) dec->header->bitrate, NULL);
315   }
316 
317   GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
318 
319   gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (dec), list,
320       GST_TAG_MERGE_REPLACE);
321   gst_tag_list_unref (list);
322 
323   g_free (encoder);
324   g_free (ver);
325 
326   return GST_FLOW_OK;
327 }
328 
329 static gboolean
gst_speex_dec_set_format(GstAudioDecoder * bdec,GstCaps * caps)330 gst_speex_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
331 {
332   GstSpeexDec *dec = GST_SPEEX_DEC (bdec);
333   gboolean ret = TRUE;
334   GstStructure *s;
335   const GValue *streamheader;
336 
337   s = gst_caps_get_structure (caps, 0);
338   if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
339       G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
340       gst_value_array_get_size (streamheader) >= 2) {
341     const GValue *header, *vorbiscomment;
342     GstBuffer *buf;
343     GstFlowReturn res = GST_FLOW_OK;
344 
345     header = gst_value_array_get_value (streamheader, 0);
346     if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
347       buf = gst_value_get_buffer (header);
348       res = gst_speex_dec_parse_header (dec, buf);
349       if (res != GST_FLOW_OK)
350         goto done;
351       gst_buffer_replace (&dec->streamheader, buf);
352     }
353 
354     vorbiscomment = gst_value_array_get_value (streamheader, 1);
355     if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
356       buf = gst_value_get_buffer (vorbiscomment);
357       res = gst_speex_dec_parse_comments (dec, buf);
358       if (res != GST_FLOW_OK)
359         goto done;
360       gst_buffer_replace (&dec->vorbiscomment, buf);
361     }
362   }
363 
364 done:
365   return ret;
366 }
367 
368 static GstFlowReturn
gst_speex_dec_parse_data(GstSpeexDec * dec,GstBuffer * buf)369 gst_speex_dec_parse_data (GstSpeexDec * dec, GstBuffer * buf)
370 {
371   GstFlowReturn res = GST_FLOW_OK;
372   gint i, fpp;
373   SpeexBits *bits;
374   GstMapInfo map;
375 
376   if (!dec->frame_duration)
377     goto not_negotiated;
378 
379   if (G_LIKELY (gst_buffer_get_size (buf))) {
380     /* send data to the bitstream */
381     gst_buffer_map (buf, &map, GST_MAP_READ);
382     speex_bits_read_from (&dec->bits, (gchar *) map.data, map.size);
383     gst_buffer_unmap (buf, &map);
384 
385     fpp = dec->header->frames_per_packet;
386     bits = &dec->bits;
387 
388     GST_DEBUG_OBJECT (dec, "received buffer of size %" G_GSIZE_FORMAT
389         ", fpp %d, %d bits", map.size, fpp, speex_bits_remaining (bits));
390   } else {
391     /* FIXME ? actually consider how much concealment is needed */
392     /* concealment data, pass NULL as the bits parameters */
393     GST_DEBUG_OBJECT (dec, "creating concealment data");
394     fpp = dec->header->frames_per_packet;
395     bits = NULL;
396   }
397 
398   /* now decode each frame, catering for unknown number of them (e.g. rtp) */
399   for (i = 0; i < fpp; i++) {
400     GstBuffer *outbuf;
401     gboolean corrupted = FALSE;
402     gint ret;
403 
404     GST_LOG_OBJECT (dec, "decoding frame %d/%d, %d bits remaining", i, fpp,
405         bits ? speex_bits_remaining (bits) : -1);
406 #if 0
407     res =
408         gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec),
409         GST_BUFFER_OFFSET_NONE, dec->frame_size * dec->header->nb_channels * 2,
410         GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
411 
412     if (res != GST_FLOW_OK) {
413       GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
414       return res;
415     }
416 #endif
417     /* FIXME, we can use a bufferpool because we have fixed size buffers. We
418      * could also use an allocator */
419     outbuf =
420         gst_buffer_new_allocate (NULL,
421         dec->frame_size * dec->header->nb_channels * 2, NULL);
422 
423     gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
424     ret = speex_decode_int (dec->state, bits, (spx_int16_t *) map.data);
425 
426     if (ret == -1) {
427       /* uh? end of stream */
428       GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
429       corrupted = TRUE;
430     } else if (ret == -2) {
431       GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?");
432       corrupted = TRUE;
433     }
434 
435     if (bits && speex_bits_remaining (bits) < 0) {
436       GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?");
437       corrupted = TRUE;
438     }
439     if (dec->header->nb_channels == 2)
440       speex_decode_stereo_int ((spx_int16_t *) map.data, dec->frame_size,
441           dec->stereo);
442 
443     gst_buffer_unmap (outbuf, &map);
444 
445     if (!corrupted) {
446       res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
447     } else {
448       res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
449       gst_buffer_unref (outbuf);
450     }
451 
452     if (res != GST_FLOW_OK) {
453       GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
454       break;
455     }
456   }
457 
458   return res;
459 
460   /* ERRORS */
461 not_negotiated:
462   {
463     GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
464         ("decoder not initialized"));
465     return GST_FLOW_NOT_NEGOTIATED;
466   }
467 }
468 
469 static gboolean
memcmp_buffers(GstBuffer * buf1,GstBuffer * buf2)470 memcmp_buffers (GstBuffer * buf1, GstBuffer * buf2)
471 {
472   GstMapInfo map;
473   gsize size1, size2;
474   gboolean res;
475 
476   size1 = gst_buffer_get_size (buf1);
477   size2 = gst_buffer_get_size (buf2);
478 
479   if (size1 != size2)
480     return FALSE;
481 
482   gst_buffer_map (buf1, &map, GST_MAP_READ);
483   res = gst_buffer_memcmp (buf2, 0, map.data, map.size) == 0;
484   gst_buffer_unmap (buf1, &map);
485 
486   return res;
487 }
488 
489 static GstFlowReturn
gst_speex_dec_handle_frame(GstAudioDecoder * bdec,GstBuffer * buf)490 gst_speex_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
491 {
492   GstFlowReturn res;
493   GstSpeexDec *dec;
494 
495   /* no fancy draining */
496   if (G_UNLIKELY (!buf))
497     return GST_FLOW_OK;
498 
499   dec = GST_SPEEX_DEC (bdec);
500 
501   /* If we have the streamheader and vorbiscomment from the caps already
502    * ignore them here */
503   if (dec->streamheader && dec->vorbiscomment) {
504     if (memcmp_buffers (dec->streamheader, buf)) {
505       GST_DEBUG_OBJECT (dec, "found streamheader");
506       gst_audio_decoder_finish_frame (bdec, NULL, 1);
507       res = GST_FLOW_OK;
508     } else if (memcmp_buffers (dec->vorbiscomment, buf)) {
509       GST_DEBUG_OBJECT (dec, "found vorbiscomments");
510       gst_audio_decoder_finish_frame (bdec, NULL, 1);
511       res = GST_FLOW_OK;
512     } else {
513       res = gst_speex_dec_parse_data (dec, buf);
514     }
515   } else {
516     /* Otherwise fall back to packet counting and assume that the
517      * first two packets are the headers. */
518     switch (dec->packetno) {
519       case 0:
520         GST_DEBUG_OBJECT (dec, "counted streamheader");
521         res = gst_speex_dec_parse_header (dec, buf);
522         gst_audio_decoder_finish_frame (bdec, NULL, 1);
523         break;
524       case 1:
525         GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
526         res = gst_speex_dec_parse_comments (dec, buf);
527         gst_audio_decoder_finish_frame (bdec, NULL, 1);
528         break;
529       default:
530       {
531         res = gst_speex_dec_parse_data (dec, buf);
532         break;
533       }
534     }
535   }
536 
537   dec->packetno++;
538 
539   return res;
540 }
541 
542 static void
gst_speex_dec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)543 gst_speex_dec_get_property (GObject * object, guint prop_id,
544     GValue * value, GParamSpec * pspec)
545 {
546   GstSpeexDec *speexdec;
547 
548   speexdec = GST_SPEEX_DEC (object);
549 
550   switch (prop_id) {
551     case ARG_ENH:
552       g_value_set_boolean (value, speexdec->enh);
553       break;
554     default:
555       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
556       break;
557   }
558 }
559 
560 static void
gst_speex_dec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)561 gst_speex_dec_set_property (GObject * object, guint prop_id,
562     const GValue * value, GParamSpec * pspec)
563 {
564   GstSpeexDec *speexdec;
565 
566   speexdec = GST_SPEEX_DEC (object);
567 
568   switch (prop_id) {
569     case ARG_ENH:
570       speexdec->enh = g_value_get_boolean (value);
571       break;
572     default:
573       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
574       break;
575   }
576 }
577