tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.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_element_found_tags_for_pad (GST_ELEMENT (dec),
313       GST_AUDIO_DECODER_SRC_PAD (dec), list);
314
315   g_free (encoder);
316   g_free (ver);
317
318   return GST_FLOW_OK;
319 }
320
321 static gboolean
322 gst_speex_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
323 {
324   GstSpeexDec *dec = GST_SPEEX_DEC (bdec);
325   gboolean ret = TRUE;
326   GstStructure *s;
327   const GValue *streamheader;
328
329   s = gst_caps_get_structure (caps, 0);
330   if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
331       G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
332       gst_value_array_get_size (streamheader) >= 2) {
333     const GValue *header, *vorbiscomment;
334     GstBuffer *buf;
335     GstFlowReturn res = GST_FLOW_OK;
336
337     header = gst_value_array_get_value (streamheader, 0);
338     if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
339       buf = gst_value_get_buffer (header);
340       res = gst_speex_dec_parse_header (dec, buf);
341       if (res != GST_FLOW_OK)
342         goto done;
343       gst_buffer_replace (&dec->streamheader, buf);
344     }
345
346     vorbiscomment = gst_value_array_get_value (streamheader, 1);
347     if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
348       buf = gst_value_get_buffer (vorbiscomment);
349       res = gst_speex_dec_parse_comments (dec, buf);
350       if (res != GST_FLOW_OK)
351         goto done;
352       gst_buffer_replace (&dec->vorbiscomment, buf);
353     }
354   }
355
356 done:
357   return ret;
358 }
359
360 static GstFlowReturn
361 gst_speex_dec_parse_data (GstSpeexDec * dec, GstBuffer * buf)
362 {
363   GstFlowReturn res = GST_FLOW_OK;
364   gint i, fpp;
365   guint size;
366   guint8 *data;
367   SpeexBits *bits;
368
369   if (!dec->frame_duration)
370     goto not_negotiated;
371
372   if (G_LIKELY (GST_BUFFER_SIZE (buf))) {
373     data = GST_BUFFER_DATA (buf);
374     size = GST_BUFFER_SIZE (buf);
375
376     /* send data to the bitstream */
377     speex_bits_read_from (&dec->bits, (char *) data, size);
378
379     fpp = dec->header->frames_per_packet;
380     bits = &dec->bits;
381
382     GST_DEBUG_OBJECT (dec, "received buffer of size %u, fpp %d, %d bits",
383         size, fpp, speex_bits_remaining (bits));
384   } else {
385     /* FIXME ? actually consider how much concealment is needed */
386     /* concealment data, pass NULL as the bits parameters */
387     GST_DEBUG_OBJECT (dec, "creating concealment data");
388     fpp = dec->header->frames_per_packet;
389     bits = NULL;
390   }
391
392   /* now decode each frame, catering for unknown number of them (e.g. rtp) */
393   for (i = 0; i < fpp; i++) {
394     GstBuffer *outbuf;
395     gint16 *out_data;
396     gint ret;
397
398     GST_LOG_OBJECT (dec, "decoding frame %d/%d, %d bits remaining", i, fpp,
399         bits ? speex_bits_remaining (bits) : -1);
400
401     res =
402         gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec),
403         GST_BUFFER_OFFSET_NONE, dec->frame_size * dec->header->nb_channels * 2,
404         GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
405
406     if (res != GST_FLOW_OK) {
407       GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
408       return res;
409     }
410
411     out_data = (gint16 *) GST_BUFFER_DATA (outbuf);
412
413     ret = speex_decode_int (dec->state, bits, out_data);
414     if (ret == -1) {
415       /* uh? end of stream */
416       if (fpp == 0 && speex_bits_remaining (bits) < 8) {
417         /* if we did not know how many frames to expect, then we get this
418            at the end if there are leftover bits to pad to the next byte */
419         GST_DEBUG_OBJECT (dec, "Discarding leftover bits");
420       } else {
421         GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
422       }
423       gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
424       gst_buffer_unref (outbuf);
425     } else if (ret == -2) {
426       GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?");
427       gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
428       gst_buffer_unref (outbuf);
429     }
430
431     if (bits && speex_bits_remaining (bits) < 0) {
432       GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?");
433       gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
434       gst_buffer_unref (outbuf);
435     }
436     if (dec->header->nb_channels == 2)
437       speex_decode_stereo_int (out_data, dec->frame_size, dec->stereo);
438
439     res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
440
441     if (res != GST_FLOW_OK) {
442       GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
443       break;
444     }
445   }
446
447   return res;
448
449   /* ERRORS */
450 not_negotiated:
451   {
452     GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
453         ("decoder not initialized"));
454     return GST_FLOW_NOT_NEGOTIATED;
455   }
456 }
457
458 static GstFlowReturn
459 gst_speex_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
460 {
461   GstFlowReturn res;
462   GstSpeexDec *dec;
463
464   /* no fancy draining */
465   if (G_UNLIKELY (!buf))
466     return GST_FLOW_OK;
467
468   dec = GST_SPEEX_DEC (bdec);
469
470   /* If we have the streamheader and vorbiscomment from the caps already
471    * ignore them here */
472   if (dec->streamheader && dec->vorbiscomment) {
473     if (GST_BUFFER_SIZE (dec->streamheader) == GST_BUFFER_SIZE (buf)
474         && memcmp (GST_BUFFER_DATA (dec->streamheader), GST_BUFFER_DATA (buf),
475             GST_BUFFER_SIZE (buf)) == 0) {
476       GST_DEBUG_OBJECT (dec, "found streamheader");
477       gst_audio_decoder_finish_frame (bdec, NULL, 1);
478       res = GST_FLOW_OK;
479     } else if (GST_BUFFER_SIZE (dec->vorbiscomment) == GST_BUFFER_SIZE (buf)
480         && memcmp (GST_BUFFER_DATA (dec->vorbiscomment), GST_BUFFER_DATA (buf),
481             GST_BUFFER_SIZE (buf)) == 0) {
482       GST_DEBUG_OBJECT (dec, "found vorbiscomments");
483       gst_audio_decoder_finish_frame (bdec, NULL, 1);
484       res = GST_FLOW_OK;
485     } else {
486       res = gst_speex_dec_parse_data (dec, buf);
487     }
488   } else {
489     /* Otherwise fall back to packet counting and assume that the
490      * first two packets are the headers. */
491     switch (dec->packetno) {
492       case 0:
493         GST_DEBUG_OBJECT (dec, "counted streamheader");
494         res = gst_speex_dec_parse_header (dec, buf);
495         gst_audio_decoder_finish_frame (bdec, NULL, 1);
496         break;
497       case 1:
498         GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
499         res = gst_speex_dec_parse_comments (dec, buf);
500         gst_audio_decoder_finish_frame (bdec, NULL, 1);
501         break;
502       default:
503       {
504         res = gst_speex_dec_parse_data (dec, buf);
505         break;
506       }
507     }
508   }
509
510   dec->packetno++;
511
512   return res;
513 }
514
515 static void
516 gst_speex_dec_get_property (GObject * object, guint prop_id,
517     GValue * value, GParamSpec * pspec)
518 {
519   GstSpeexDec *speexdec;
520
521   speexdec = GST_SPEEX_DEC (object);
522
523   switch (prop_id) {
524     case ARG_ENH:
525       g_value_set_boolean (value, speexdec->enh);
526       break;
527     default:
528       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
529       break;
530   }
531 }
532
533 static void
534 gst_speex_dec_set_property (GObject * object, guint prop_id,
535     const GValue * value, GParamSpec * pspec)
536 {
537   GstSpeexDec *speexdec;
538
539   speexdec = GST_SPEEX_DEC (object);
540
541   switch (prop_id) {
542     case ARG_ENH:
543       speexdec->enh = g_value_get_boolean (value);
544       break;
545     default:
546       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
547       break;
548   }
549 }