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