5abce70b6726ce2960af04b7932fc1a5590430aa
[platform/upstream/gstreamer.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
50 GST_DEBUG_CATEGORY_STATIC (speexdec_debug);
51 #define GST_CAT_DEFAULT speexdec_debug
52
53 #define DEFAULT_ENH   TRUE
54
55 enum
56 {
57   ARG_0,
58   ARG_ENH
59 };
60
61 static GstStaticPadTemplate speex_dec_src_factory =
62 GST_STATIC_PAD_TEMPLATE ("src",
63     GST_PAD_SRC,
64     GST_PAD_ALWAYS,
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")
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 GST_BOILERPLATE (GstSpeexDec, gst_speex_dec, GstAudioDecoder,
80     GST_TYPE_AUDIO_DECODER);
81
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_base_init (gpointer g_class)
97 {
98   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
99
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>");
107 }
108
109 static void
110 gst_speex_dec_class_init (GstSpeexDecClass * klass)
111 {
112   GObjectClass *gobject_class;
113   GstAudioDecoderClass *base_class;
114
115   gobject_class = (GObjectClass *) klass;
116   base_class = (GstAudioDecoderClass *) klass;
117
118   gobject_class->set_property = gst_speex_dec_set_property;
119   gobject_class->get_property = gst_speex_dec_get_property;
120
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);
125
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));
129
130   GST_DEBUG_CATEGORY_INIT (speexdec_debug, "speexdec", 0,
131       "speex decoding element");
132 }
133
134 static void
135 gst_speex_dec_reset (GstSpeexDec * dec)
136 {
137   dec->packetno = 0;
138   dec->frame_size = 0;
139   dec->frame_duration = 0;
140   dec->mode = NULL;
141   free (dec->header);
142   dec->header = NULL;
143   speex_bits_destroy (&dec->bits);
144
145   gst_buffer_replace (&dec->streamheader, NULL);
146   gst_buffer_replace (&dec->vorbiscomment, NULL);
147
148   if (dec->stereo) {
149     speex_stereo_state_destroy (dec->stereo);
150     dec->stereo = NULL;
151   }
152
153   if (dec->state) {
154     speex_decoder_destroy (dec->state);
155     dec->state = NULL;
156   }
157 }
158
159 static void
160 gst_speex_dec_init (GstSpeexDec * dec, GstSpeexDecClass * g_class)
161 {
162   dec->enh = DEFAULT_ENH;
163
164   gst_speex_dec_reset (dec);
165 }
166
167 static gboolean
168 gst_speex_dec_start (GstAudioDecoder * dec)
169 {
170   GstSpeexDec *sd = GST_SPEEX_DEC (dec);
171
172   GST_DEBUG_OBJECT (dec, "start");
173   gst_speex_dec_reset (sd);
174
175   /* we know about concealment */
176   gst_audio_decoder_set_plc_aware (dec, TRUE);
177
178   return TRUE;
179 }
180
181 static gboolean
182 gst_speex_dec_stop (GstAudioDecoder * dec)
183 {
184   GstSpeexDec *sd = GST_SPEEX_DEC (dec);
185
186   GST_DEBUG_OBJECT (dec, "stop");
187   gst_speex_dec_reset (sd);
188
189   return TRUE;
190 }
191
192 static GstFlowReturn
193 gst_speex_dec_parse_header (GstSpeexDec * dec, GstBuffer * buf)
194 {
195   GstCaps *caps;
196
197   /* get the header */
198   dec->header = speex_packet_to_header ((char *) GST_BUFFER_DATA (buf),
199       GST_BUFFER_SIZE (buf));
200
201   if (!dec->header)
202     goto no_header;
203
204   if (dec->header->mode >= SPEEX_NB_MODES || dec->header->mode < 0)
205     goto mode_too_old;
206
207   dec->mode = speex_lib_get_mode (dec->header->mode);
208
209   /* initialize the decoder */
210   dec->state = speex_decoder_init (dec->mode);
211   if (!dec->state)
212     goto init_failed;
213
214   speex_decoder_ctl (dec->state, SPEEX_SET_ENH, &dec->enh);
215   speex_decoder_ctl (dec->state, SPEEX_GET_FRAME_SIZE, &dec->frame_size);
216
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);
223   }
224
225   speex_decoder_ctl (dec->state, SPEEX_SET_SAMPLING_RATE, &dec->header->rate);
226
227   dec->frame_duration = gst_util_uint64_scale_int (dec->frame_size,
228       GST_SECOND, dec->header->rate);
229
230   speex_bits_init (&dec->bits);
231
232   /* set caps */
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);
239
240   if (!gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps))
241     goto nego_failed;
242
243   gst_caps_unref (caps);
244   return GST_FLOW_OK;
245
246   /* ERRORS */
247 no_header:
248   {
249     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
250         (NULL), ("couldn't read header"));
251     return GST_FLOW_ERROR;
252   }
253 mode_too_old:
254   {
255     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
256         (NULL),
257         ("Mode number %d does not (yet/any longer) exist in this version",
258             dec->header->mode));
259     return GST_FLOW_ERROR;
260   }
261 init_failed:
262   {
263     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
264         (NULL), ("couldn't initialize decoder"));
265     return GST_FLOW_ERROR;
266   }
267 nego_failed:
268   {
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;
273   }
274 }
275
276 static GstFlowReturn
277 gst_speex_dec_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
278 {
279   GstTagList *list;
280   gchar *ver, *encoder = NULL;
281
282   list = gst_tag_list_from_vorbiscomment_buffer (buf, NULL, 0, &encoder);
283
284   if (!list) {
285     GST_WARNING_OBJECT (dec, "couldn't decode comments");
286     list = gst_tag_list_new ();
287   }
288
289   if (encoder) {
290     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
291         GST_TAG_ENCODER, encoder, NULL);
292   }
293
294   gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
295       GST_TAG_AUDIO_CODEC, "Speex", NULL);
296
297   ver = g_strndup (dec->header->speex_version, SPEEX_HEADER_VERSION_LENGTH);
298   g_strstrip (ver);
299
300   if (ver != NULL && *ver != '\0') {
301     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
302         GST_TAG_ENCODER_VERSION, ver, NULL);
303   }
304
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);
308   }
309
310   GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
311
312   gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (dec), list,
313       GST_TAG_MERGE_REPLACE);
314   gst_tag_list_free (list);
315
316   g_free (encoder);
317   g_free (ver);
318
319   return GST_FLOW_OK;
320 }
321
322 static gboolean
323 gst_speex_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
324 {
325   GstSpeexDec *dec = GST_SPEEX_DEC (bdec);
326   gboolean ret = TRUE;
327   GstStructure *s;
328   const GValue *streamheader;
329
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;
335     GstBuffer *buf;
336     GstFlowReturn res = GST_FLOW_OK;
337
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)
343         goto done;
344       gst_buffer_replace (&dec->streamheader, buf);
345     }
346
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)
352         goto done;
353       gst_buffer_replace (&dec->vorbiscomment, buf);
354     }
355   }
356
357 done:
358   return ret;
359 }
360
361 static GstFlowReturn
362 gst_speex_dec_parse_data (GstSpeexDec * dec, GstBuffer * buf)
363 {
364   GstFlowReturn res = GST_FLOW_OK;
365   gint i, fpp;
366   guint size;
367   guint8 *data;
368   SpeexBits *bits;
369
370   if (!dec->frame_duration)
371     goto not_negotiated;
372
373   if (G_LIKELY (GST_BUFFER_SIZE (buf))) {
374     data = GST_BUFFER_DATA (buf);
375     size = GST_BUFFER_SIZE (buf);
376
377     /* send data to the bitstream */
378     speex_bits_read_from (&dec->bits, (char *) data, size);
379
380     fpp = dec->header->frames_per_packet;
381     bits = &dec->bits;
382
383     GST_DEBUG_OBJECT (dec, "received buffer of size %u, fpp %d, %d bits",
384         size, fpp, speex_bits_remaining (bits));
385   } else {
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;
390     bits = NULL;
391   }
392
393   /* now decode each frame, catering for unknown number of them (e.g. rtp) */
394   for (i = 0; i < fpp; i++) {
395     GstBuffer *outbuf;
396     gint16 *out_data;
397     gint ret;
398
399     GST_LOG_OBJECT (dec, "decoding frame %d/%d, %d bits remaining", i, fpp,
400         bits ? speex_bits_remaining (bits) : -1);
401
402     res =
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);
406
407     if (res != GST_FLOW_OK) {
408       GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
409       return res;
410     }
411
412     out_data = (gint16 *) GST_BUFFER_DATA (outbuf);
413
414     ret = speex_decode_int (dec->state, bits, out_data);
415     if (ret == -1) {
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");
421       } else {
422         GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
423       }
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);
430     }
431
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);
436     }
437     if (dec->header->nb_channels == 2)
438       speex_decode_stereo_int (out_data, dec->frame_size, dec->stereo);
439
440     res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
441
442     if (res != GST_FLOW_OK) {
443       GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
444       break;
445     }
446   }
447
448   return res;
449
450   /* ERRORS */
451 not_negotiated:
452   {
453     GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
454         ("decoder not initialized"));
455     return GST_FLOW_NOT_NEGOTIATED;
456   }
457 }
458
459 static GstFlowReturn
460 gst_speex_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
461 {
462   GstFlowReturn res;
463   GstSpeexDec *dec;
464
465   /* no fancy draining */
466   if (G_UNLIKELY (!buf))
467     return GST_FLOW_OK;
468
469   dec = GST_SPEEX_DEC (bdec);
470
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);
479       res = GST_FLOW_OK;
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);
485       res = GST_FLOW_OK;
486     } else {
487       res = gst_speex_dec_parse_data (dec, buf);
488     }
489   } else {
490     /* Otherwise fall back to packet counting and assume that the
491      * first two packets are the headers. */
492     switch (dec->packetno) {
493       case 0:
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);
497         break;
498       case 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);
502         break;
503       default:
504       {
505         res = gst_speex_dec_parse_data (dec, buf);
506         break;
507       }
508     }
509   }
510
511   dec->packetno++;
512
513   return res;
514 }
515
516 static void
517 gst_speex_dec_get_property (GObject * object, guint prop_id,
518     GValue * value, GParamSpec * pspec)
519 {
520   GstSpeexDec *speexdec;
521
522   speexdec = GST_SPEEX_DEC (object);
523
524   switch (prop_id) {
525     case ARG_ENH:
526       g_value_set_boolean (value, speexdec->enh);
527       break;
528     default:
529       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
530       break;
531   }
532 }
533
534 static void
535 gst_speex_dec_set_property (GObject * object, guint prop_id,
536     const 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       speexdec->enh = g_value_get_boolean (value);
545       break;
546     default:
547       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
548       break;
549   }
550 }