upload tizen1.0 source
[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, GstElement, GST_TYPE_ELEMENT);
80
81 static gboolean speex_dec_sink_event (GstPad * pad, GstEvent * event);
82 static GstFlowReturn speex_dec_chain (GstPad * pad, GstBuffer * buf);
83 static GstStateChangeReturn speex_dec_change_state (GstElement * element,
84     GstStateChange transition);
85
86 static gboolean speex_dec_src_event (GstPad * pad, GstEvent * event);
87 static gboolean speex_dec_src_query (GstPad * pad, GstQuery * query);
88 static gboolean speex_dec_sink_query (GstPad * pad, GstQuery * query);
89 static gboolean speex_dec_sink_setcaps (GstPad * pad, GstCaps * caps);
90 static const GstQueryType *speex_get_src_query_types (GstPad * pad);
91 static const GstQueryType *speex_get_sink_query_types (GstPad * pad);
92 static gboolean speex_dec_convert (GstPad * pad,
93     GstFormat src_format, gint64 src_value,
94     GstFormat * dest_format, gint64 * dest_value);
95
96 static void gst_speex_dec_get_property (GObject * object, guint prop_id,
97     GValue * value, GParamSpec * pspec);
98 static void gst_speex_dec_set_property (GObject * object, guint prop_id,
99     const GValue * value, GParamSpec * pspec);
100
101 static GstFlowReturn speex_dec_chain_parse_data (GstSpeexDec * dec,
102     GstBuffer * buf, GstClockTime timestamp, GstClockTime duration);
103
104 static GstFlowReturn speex_dec_chain_parse_header (GstSpeexDec * dec,
105     GstBuffer * buf);
106 static GstFlowReturn speex_dec_chain_parse_comments (GstSpeexDec * dec,
107     GstBuffer * buf);
108
109 static void
110 gst_speex_dec_base_init (gpointer g_class)
111 {
112   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
113
114   gst_element_class_add_pad_template (element_class,
115       gst_static_pad_template_get (&speex_dec_src_factory));
116   gst_element_class_add_pad_template (element_class,
117       gst_static_pad_template_get (&speex_dec_sink_factory));
118   gst_element_class_set_details_simple (element_class, "Speex audio decoder",
119       "Codec/Decoder/Audio",
120       "decode speex streams to audio", "Wim Taymans <wim@fluendo.com>");
121 }
122
123 static void
124 gst_speex_dec_class_init (GstSpeexDecClass * klass)
125 {
126   GObjectClass *gobject_class;
127   GstElementClass *gstelement_class;
128
129   gobject_class = (GObjectClass *) klass;
130   gstelement_class = (GstElementClass *) klass;
131
132   gobject_class->set_property = gst_speex_dec_set_property;
133   gobject_class->get_property = gst_speex_dec_get_property;
134
135   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ENH,
136       g_param_spec_boolean ("enh", "Enh", "Enable perceptual enhancement",
137           DEFAULT_ENH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138
139   gstelement_class->change_state = GST_DEBUG_FUNCPTR (speex_dec_change_state);
140
141   GST_DEBUG_CATEGORY_INIT (speexdec_debug, "speexdec", 0,
142       "speex decoding element");
143 }
144
145 static void
146 gst_speex_dec_reset (GstSpeexDec * dec)
147 {
148   gst_segment_init (&dec->segment, GST_FORMAT_UNDEFINED);
149   dec->packetno = 0;
150   dec->frame_size = 0;
151   dec->frame_duration = 0;
152   dec->mode = NULL;
153   free (dec->header);
154   dec->header = NULL;
155   speex_bits_destroy (&dec->bits);
156
157   gst_buffer_replace (&dec->streamheader, NULL);
158   gst_buffer_replace (&dec->vorbiscomment, NULL);
159
160   if (dec->stereo) {
161     speex_stereo_state_destroy (dec->stereo);
162     dec->stereo = NULL;
163   }
164
165   if (dec->state) {
166     speex_decoder_destroy (dec->state);
167     dec->state = NULL;
168   }
169 }
170
171 static void
172 gst_speex_dec_init (GstSpeexDec * dec, GstSpeexDecClass * g_class)
173 {
174   dec->sinkpad =
175       gst_pad_new_from_static_template (&speex_dec_sink_factory, "sink");
176   gst_pad_set_chain_function (dec->sinkpad,
177       GST_DEBUG_FUNCPTR (speex_dec_chain));
178   gst_pad_set_event_function (dec->sinkpad,
179       GST_DEBUG_FUNCPTR (speex_dec_sink_event));
180   gst_pad_set_query_type_function (dec->sinkpad,
181       GST_DEBUG_FUNCPTR (speex_get_sink_query_types));
182   gst_pad_set_query_function (dec->sinkpad,
183       GST_DEBUG_FUNCPTR (speex_dec_sink_query));
184   gst_pad_set_setcaps_function (dec->sinkpad,
185       GST_DEBUG_FUNCPTR (speex_dec_sink_setcaps));
186   gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
187
188   dec->srcpad =
189       gst_pad_new_from_static_template (&speex_dec_src_factory, "src");
190   gst_pad_use_fixed_caps (dec->srcpad);
191   gst_pad_set_event_function (dec->srcpad,
192       GST_DEBUG_FUNCPTR (speex_dec_src_event));
193   gst_pad_set_query_type_function (dec->srcpad,
194       GST_DEBUG_FUNCPTR (speex_get_src_query_types));
195   gst_pad_set_query_function (dec->srcpad,
196       GST_DEBUG_FUNCPTR (speex_dec_src_query));
197   gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
198
199   dec->enh = DEFAULT_ENH;
200
201   gst_speex_dec_reset (dec);
202 }
203
204 static gboolean
205 speex_dec_sink_setcaps (GstPad * pad, GstCaps * caps)
206 {
207   GstSpeexDec *dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
208   gboolean ret = TRUE;
209   GstStructure *s;
210   const GValue *streamheader;
211
212   s = gst_caps_get_structure (caps, 0);
213   if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
214       G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
215       gst_value_array_get_size (streamheader) >= 2) {
216     const GValue *header, *vorbiscomment;
217     GstBuffer *buf;
218     GstFlowReturn res = GST_FLOW_OK;
219
220     header = gst_value_array_get_value (streamheader, 0);
221     if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
222       buf = gst_value_get_buffer (header);
223       res = speex_dec_chain_parse_header (dec, buf);
224       if (res != GST_FLOW_OK)
225         goto done;
226       gst_buffer_replace (&dec->streamheader, buf);
227     }
228
229     vorbiscomment = gst_value_array_get_value (streamheader, 1);
230     if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
231       buf = gst_value_get_buffer (vorbiscomment);
232       res = speex_dec_chain_parse_comments (dec, buf);
233       if (res != GST_FLOW_OK)
234         goto done;
235       gst_buffer_replace (&dec->vorbiscomment, buf);
236     }
237   }
238
239 done:
240   gst_object_unref (dec);
241   return ret;
242 }
243
244 static gboolean
245 speex_dec_convert (GstPad * pad,
246     GstFormat src_format, gint64 src_value,
247     GstFormat * dest_format, gint64 * dest_value)
248 {
249   gboolean res = TRUE;
250   GstSpeexDec *dec;
251   guint64 scale = 1;
252
253   dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
254
255   if (src_format == *dest_format) {
256     *dest_value = src_value;
257     res = TRUE;
258     goto cleanup;
259   }
260
261   if (dec->packetno < 1) {
262     res = FALSE;
263     goto cleanup;
264   }
265
266   if (pad == dec->sinkpad &&
267       (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES)) {
268     res = FALSE;
269     goto cleanup;
270   }
271
272   switch (src_format) {
273     case GST_FORMAT_TIME:
274       switch (*dest_format) {
275         case GST_FORMAT_BYTES:
276           scale = 2 * dec->header->nb_channels;
277         case GST_FORMAT_DEFAULT:
278           *dest_value =
279               gst_util_uint64_scale_int (scale * src_value, dec->header->rate,
280               GST_SECOND);
281           break;
282         default:
283           res = FALSE;
284           break;
285       }
286       break;
287     case GST_FORMAT_DEFAULT:
288       switch (*dest_format) {
289         case GST_FORMAT_BYTES:
290           *dest_value = src_value * 2 * dec->header->nb_channels;
291           break;
292         case GST_FORMAT_TIME:
293           *dest_value =
294               gst_util_uint64_scale_int (src_value, GST_SECOND,
295               dec->header->rate);
296           break;
297         default:
298           res = FALSE;
299           break;
300       }
301       break;
302     case GST_FORMAT_BYTES:
303       switch (*dest_format) {
304         case GST_FORMAT_DEFAULT:
305           *dest_value = src_value / (2 * dec->header->nb_channels);
306           break;
307         case GST_FORMAT_TIME:
308           *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
309               dec->header->rate * 2 * dec->header->nb_channels);
310           break;
311         default:
312           res = FALSE;
313           break;
314       }
315       break;
316     default:
317       res = FALSE;
318       break;
319   }
320
321 cleanup:
322   gst_object_unref (dec);
323   return res;
324 }
325
326 static const GstQueryType *
327 speex_get_sink_query_types (GstPad * pad)
328 {
329   static const GstQueryType speex_dec_sink_query_types[] = {
330     GST_QUERY_CONVERT,
331     0
332   };
333
334   return speex_dec_sink_query_types;
335 }
336
337 static gboolean
338 speex_dec_sink_query (GstPad * pad, GstQuery * query)
339 {
340   GstSpeexDec *dec;
341   gboolean res;
342
343   dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
344
345   switch (GST_QUERY_TYPE (query)) {
346     case GST_QUERY_CONVERT:
347     {
348       GstFormat src_fmt, dest_fmt;
349       gint64 src_val, dest_val;
350
351       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
352       res = speex_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val);
353       if (res) {
354         gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
355       }
356       break;
357     }
358     default:
359       res = gst_pad_query_default (pad, query);
360       break;
361   }
362
363   gst_object_unref (dec);
364   return res;
365 }
366
367 static const GstQueryType *
368 speex_get_src_query_types (GstPad * pad)
369 {
370   static const GstQueryType speex_dec_src_query_types[] = {
371     GST_QUERY_POSITION,
372     GST_QUERY_DURATION,
373     0
374   };
375
376   return speex_dec_src_query_types;
377 }
378
379 static gboolean
380 speex_dec_src_query (GstPad * pad, GstQuery * query)
381 {
382   GstSpeexDec *dec;
383   gboolean res = FALSE;
384
385   dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
386
387   switch (GST_QUERY_TYPE (query)) {
388     case GST_QUERY_POSITION:{
389       GstSegment segment;
390       GstFormat format;
391       gint64 cur;
392
393       gst_query_parse_position (query, &format, NULL);
394
395       GST_PAD_STREAM_LOCK (dec->sinkpad);
396       segment = dec->segment;
397       GST_PAD_STREAM_UNLOCK (dec->sinkpad);
398
399       if (segment.format != GST_FORMAT_TIME) {
400         GST_DEBUG_OBJECT (dec, "segment not initialised yet");
401         break;
402       }
403
404       if ((res = speex_dec_convert (dec->srcpad, GST_FORMAT_TIME,
405                   segment.last_stop, &format, &cur))) {
406         gst_query_set_position (query, format, cur);
407       }
408       break;
409     }
410     case GST_QUERY_DURATION:{
411       GstFormat format = GST_FORMAT_TIME;
412       gint64 dur;
413
414       /* get duration from demuxer */
415       if (!gst_pad_query_peer_duration (dec->sinkpad, &format, &dur))
416         break;
417
418       gst_query_parse_duration (query, &format, NULL);
419
420       /* and convert it into the requested format */
421       if ((res = speex_dec_convert (dec->srcpad, GST_FORMAT_TIME,
422                   dur, &format, &dur))) {
423         gst_query_set_duration (query, format, dur);
424       }
425       break;
426     }
427     default:
428       res = gst_pad_query_default (pad, query);
429       break;
430   }
431
432   gst_object_unref (dec);
433   return res;
434 }
435
436 static gboolean
437 speex_dec_src_event (GstPad * pad, GstEvent * event)
438 {
439   gboolean res = FALSE;
440   GstSpeexDec *dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
441
442   GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
443
444   switch (GST_EVENT_TYPE (event)) {
445     case GST_EVENT_SEEK:{
446       GstFormat format, tformat;
447       gdouble rate;
448       GstEvent *real_seek;
449       GstSeekFlags flags;
450       GstSeekType cur_type, stop_type;
451       gint64 cur, stop;
452       gint64 tcur, tstop;
453
454       gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
455           &stop_type, &stop);
456
457       /* we have to ask our peer to seek to time here as we know
458        * nothing about how to generate a granulepos from the src
459        * formats or anything.
460        *
461        * First bring the requested format to time
462        */
463       tformat = GST_FORMAT_TIME;
464       if (!(res = speex_dec_convert (pad, format, cur, &tformat, &tcur)))
465         break;
466       if (!(res = speex_dec_convert (pad, format, stop, &tformat, &tstop)))
467         break;
468
469       /* then seek with time on the peer */
470       real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
471           flags, cur_type, tcur, stop_type, tstop);
472
473       GST_LOG_OBJECT (dec, "seek to %" GST_TIME_FORMAT, GST_TIME_ARGS (tcur));
474
475       res = gst_pad_push_event (dec->sinkpad, real_seek);
476       gst_event_unref (event);
477       break;
478     }
479     default:
480       res = gst_pad_event_default (pad, event);
481       break;
482   }
483
484   gst_object_unref (dec);
485   return res;
486 }
487
488 static gboolean
489 speex_dec_sink_event (GstPad * pad, GstEvent * event)
490 {
491   GstSpeexDec *dec;
492   gboolean ret = FALSE;
493
494   dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
495
496   GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
497
498   switch (GST_EVENT_TYPE (event)) {
499     case GST_EVENT_NEWSEGMENT:{
500       GstFormat format;
501       gdouble rate, arate;
502       gint64 start, stop, time;
503       gboolean update;
504
505       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
506           &start, &stop, &time);
507
508       if (format != GST_FORMAT_TIME)
509         goto newseg_wrong_format;
510
511       if (rate <= 0.0)
512         goto newseg_wrong_rate;
513
514       if (update) {
515         /* time progressed without data, see if we can fill the gap with
516          * some concealment data */
517         if (dec->segment.last_stop < start) {
518           GstClockTime duration;
519
520           duration = start - dec->segment.last_stop;
521           speex_dec_chain_parse_data (dec, NULL, dec->segment.last_stop,
522               duration);
523         }
524       }
525
526       /* now configure the values */
527       gst_segment_set_newsegment_full (&dec->segment, update,
528           rate, arate, GST_FORMAT_TIME, start, stop, time);
529
530       GST_DEBUG_OBJECT (dec, "segment now: cur = %" GST_TIME_FORMAT " [%"
531           GST_TIME_FORMAT " - %" GST_TIME_FORMAT "]",
532           GST_TIME_ARGS (dec->segment.last_stop),
533           GST_TIME_ARGS (dec->segment.start),
534           GST_TIME_ARGS (dec->segment.stop));
535
536       ret = gst_pad_push_event (dec->srcpad, event);
537       break;
538     }
539     default:
540       ret = gst_pad_event_default (pad, event);
541       break;
542   }
543
544   gst_object_unref (dec);
545   return ret;
546
547   /* ERRORS */
548 newseg_wrong_format:
549   {
550     GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
551     gst_object_unref (dec);
552     return FALSE;
553   }
554 newseg_wrong_rate:
555   {
556     GST_DEBUG_OBJECT (dec, "negative rates not supported yet");
557     gst_object_unref (dec);
558     return FALSE;
559   }
560 }
561
562 static GstFlowReturn
563 speex_dec_chain_parse_header (GstSpeexDec * dec, GstBuffer * buf)
564 {
565   GstCaps *caps;
566
567   /* get the header */
568   dec->header = speex_packet_to_header ((char *) GST_BUFFER_DATA (buf),
569       GST_BUFFER_SIZE (buf));
570
571   if (!dec->header)
572     goto no_header;
573
574   if (dec->header->mode >= SPEEX_NB_MODES || dec->header->mode < 0)
575     goto mode_too_old;
576
577   dec->mode = speex_lib_get_mode (dec->header->mode);
578
579   /* initialize the decoder */
580   dec->state = speex_decoder_init (dec->mode);
581   if (!dec->state)
582     goto init_failed;
583
584   speex_decoder_ctl (dec->state, SPEEX_SET_ENH, &dec->enh);
585   speex_decoder_ctl (dec->state, SPEEX_GET_FRAME_SIZE, &dec->frame_size);
586
587   if (dec->header->nb_channels != 1) {
588     dec->stereo = speex_stereo_state_init ();
589     dec->callback.callback_id = SPEEX_INBAND_STEREO;
590     dec->callback.func = speex_std_stereo_request_handler;
591     dec->callback.data = dec->stereo;
592     speex_decoder_ctl (dec->state, SPEEX_SET_HANDLER, &dec->callback);
593   }
594
595   speex_decoder_ctl (dec->state, SPEEX_SET_SAMPLING_RATE, &dec->header->rate);
596
597   dec->frame_duration = gst_util_uint64_scale_int (dec->frame_size,
598       GST_SECOND, dec->header->rate);
599
600   speex_bits_init (&dec->bits);
601
602   /* set caps */
603   caps = gst_caps_new_simple ("audio/x-raw-int",
604       "rate", G_TYPE_INT, dec->header->rate,
605       "channels", G_TYPE_INT, dec->header->nb_channels,
606       "signed", G_TYPE_BOOLEAN, TRUE,
607       "endianness", G_TYPE_INT, G_BYTE_ORDER,
608       "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
609
610   if (!gst_pad_set_caps (dec->srcpad, caps))
611     goto nego_failed;
612
613   gst_caps_unref (caps);
614   return GST_FLOW_OK;
615
616   /* ERRORS */
617 no_header:
618   {
619     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
620         (NULL), ("couldn't read header"));
621     return GST_FLOW_ERROR;
622   }
623 mode_too_old:
624   {
625     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
626         (NULL),
627         ("Mode number %d does not (yet/any longer) exist in this version",
628             dec->header->mode));
629     return GST_FLOW_ERROR;
630   }
631 init_failed:
632   {
633     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
634         (NULL), ("couldn't initialize decoder"));
635     return GST_FLOW_ERROR;
636   }
637 nego_failed:
638   {
639     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
640         (NULL), ("couldn't negotiate format"));
641     gst_caps_unref (caps);
642     return GST_FLOW_NOT_NEGOTIATED;
643   }
644 }
645
646 static GstFlowReturn
647 speex_dec_chain_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
648 {
649   GstTagList *list;
650   gchar *ver, *encoder = NULL;
651
652   list = gst_tag_list_from_vorbiscomment_buffer (buf, NULL, 0, &encoder);
653
654   if (!list) {
655     GST_WARNING_OBJECT (dec, "couldn't decode comments");
656     list = gst_tag_list_new ();
657   }
658
659   if (encoder) {
660     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
661         GST_TAG_ENCODER, encoder, NULL);
662   }
663
664   gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
665       GST_TAG_AUDIO_CODEC, "Speex", NULL);
666
667   ver = g_strndup (dec->header->speex_version, SPEEX_HEADER_VERSION_LENGTH);
668   g_strstrip (ver);
669
670   if (ver != NULL && *ver != '\0') {
671     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
672         GST_TAG_ENCODER_VERSION, ver, NULL);
673   }
674
675   if (dec->header->bitrate > 0) {
676     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
677         GST_TAG_BITRATE, (guint) dec->header->bitrate, NULL);
678   }
679
680   GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
681
682   gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, list);
683
684   g_free (encoder);
685   g_free (ver);
686
687   return GST_FLOW_OK;
688 }
689
690 static GstFlowReturn
691 speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf,
692     GstClockTime timestamp, GstClockTime duration)
693 {
694   GstFlowReturn res = GST_FLOW_OK;
695   gint i, fpp;
696   guint size;
697   guint8 *data;
698   SpeexBits *bits;
699
700   if (!dec->frame_duration)
701     goto not_negotiated;
702
703   if (timestamp != -1) {
704     dec->segment.last_stop = timestamp;
705   } else {
706     timestamp = dec->segment.last_stop;
707   }
708
709   if (buf) {
710     data = GST_BUFFER_DATA (buf);
711     size = GST_BUFFER_SIZE (buf);
712
713     /* send data to the bitstream */
714     speex_bits_read_from (&dec->bits, (char *) data, size);
715
716     fpp = dec->header->frames_per_packet;
717     bits = &dec->bits;
718
719     GST_DEBUG_OBJECT (dec, "received buffer of size %u, fpp %d, %d bits", size,
720         fpp, speex_bits_remaining (bits));
721   } else {
722     /* concealment data, pass NULL as the bits parameters */
723     GST_DEBUG_OBJECT (dec, "creating concealment data");
724     fpp = dec->header->frames_per_packet;
725     bits = NULL;
726   }
727
728
729   /* now decode each frame, catering for unknown number of them (e.g. rtp) */
730   for (i = 0; i < fpp; i++) {
731     GstBuffer *outbuf;
732     gint16 *out_data;
733     gint ret;
734
735     GST_LOG_OBJECT (dec, "decoding frame %d/%d, %d bits remaining", i, fpp,
736         bits ? speex_bits_remaining (bits) : -1);
737
738     res = gst_pad_alloc_buffer_and_set_caps (dec->srcpad,
739         GST_BUFFER_OFFSET_NONE, dec->frame_size * dec->header->nb_channels * 2,
740         GST_PAD_CAPS (dec->srcpad), &outbuf);
741
742     if (res != GST_FLOW_OK) {
743       GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
744       return res;
745     }
746
747     out_data = (gint16 *) GST_BUFFER_DATA (outbuf);
748
749     ret = speex_decode_int (dec->state, bits, out_data);
750     if (ret == -1) {
751       /* uh? end of stream */
752       if (fpp == 0 && speex_bits_remaining (bits) < 8) {
753         /* if we did not know how many frames to expect, then we get this
754            at the end if there are leftover bits to pad to the next byte */
755       } else {
756         GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
757       }
758       gst_buffer_unref (outbuf);
759       outbuf = NULL;
760       break;
761     } else if (ret == -2) {
762       GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?");
763       gst_buffer_unref (outbuf);
764       outbuf = NULL;
765       break;
766     }
767
768     if (bits && speex_bits_remaining (bits) < 0) {
769       GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?");
770       gst_buffer_unref (outbuf);
771       outbuf = NULL;
772       break;
773     }
774     if (dec->header->nb_channels == 2)
775       speex_decode_stereo_int (out_data, dec->frame_size, dec->stereo);
776
777     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
778     GST_BUFFER_DURATION (outbuf) = dec->frame_duration;
779
780     dec->segment.last_stop += dec->frame_duration;
781     timestamp = dec->segment.last_stop;
782
783     GST_LOG_OBJECT (dec, "pushing buffer with ts=%" GST_TIME_FORMAT ", dur=%"
784         GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
785         GST_TIME_ARGS (dec->frame_duration));
786
787     res = gst_pad_push (dec->srcpad, outbuf);
788
789     if (res != GST_FLOW_OK) {
790       GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
791       break;
792     }
793   }
794
795   return res;
796
797   /* ERRORS */
798 not_negotiated:
799   {
800     GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
801         ("decoder not initialized"));
802     return GST_FLOW_NOT_NEGOTIATED;
803   }
804 }
805
806 static GstFlowReturn
807 speex_dec_chain (GstPad * pad, GstBuffer * buf)
808 {
809   GstFlowReturn res;
810   GstSpeexDec *dec;
811
812   dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
813
814   /* If we have the streamheader and vorbiscomment from the caps already
815    * ignore them here */
816   if (dec->streamheader && dec->vorbiscomment) {
817     if (GST_BUFFER_SIZE (dec->streamheader) == GST_BUFFER_SIZE (buf)
818         && memcmp (GST_BUFFER_DATA (dec->streamheader), GST_BUFFER_DATA (buf),
819             GST_BUFFER_SIZE (buf)) == 0) {
820       res = GST_FLOW_OK;
821     } else if (GST_BUFFER_SIZE (dec->vorbiscomment) == GST_BUFFER_SIZE (buf)
822         && memcmp (GST_BUFFER_DATA (dec->vorbiscomment), GST_BUFFER_DATA (buf),
823             GST_BUFFER_SIZE (buf)) == 0) {
824       res = GST_FLOW_OK;
825     } else {
826       res =
827           speex_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
828           GST_BUFFER_DURATION (buf));
829     }
830   } else {
831     /* Otherwise fall back to packet counting and assume that the
832      * first two packets are the headers. */
833     switch (dec->packetno) {
834       case 0:
835         res = speex_dec_chain_parse_header (dec, buf);
836         break;
837       case 1:
838         res = speex_dec_chain_parse_comments (dec, buf);
839         break;
840       default:
841         res =
842             speex_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
843             GST_BUFFER_DURATION (buf));
844         break;
845     }
846   }
847
848   dec->packetno++;
849
850   gst_buffer_unref (buf);
851   gst_object_unref (dec);
852
853   return res;
854 }
855
856 static void
857 gst_speex_dec_get_property (GObject * object, guint prop_id,
858     GValue * value, GParamSpec * pspec)
859 {
860   GstSpeexDec *speexdec;
861
862   speexdec = GST_SPEEX_DEC (object);
863
864   switch (prop_id) {
865     case ARG_ENH:
866       g_value_set_boolean (value, speexdec->enh);
867       break;
868     default:
869       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
870       break;
871   }
872 }
873
874 static void
875 gst_speex_dec_set_property (GObject * object, guint prop_id,
876     const GValue * value, GParamSpec * pspec)
877 {
878   GstSpeexDec *speexdec;
879
880   speexdec = GST_SPEEX_DEC (object);
881
882   switch (prop_id) {
883     case ARG_ENH:
884       speexdec->enh = g_value_get_boolean (value);
885       break;
886     default:
887       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
888       break;
889   }
890 }
891
892
893 static GstStateChangeReturn
894 speex_dec_change_state (GstElement * element, GstStateChange transition)
895 {
896   GstStateChangeReturn ret;
897   GstSpeexDec *dec = GST_SPEEX_DEC (element);
898
899   switch (transition) {
900     case GST_STATE_CHANGE_NULL_TO_READY:
901     case GST_STATE_CHANGE_READY_TO_PAUSED:
902     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
903     default:
904       break;
905   }
906
907   ret = parent_class->change_state (element, transition);
908   if (ret != GST_STATE_CHANGE_SUCCESS)
909     return ret;
910
911   switch (transition) {
912     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
913       break;
914     case GST_STATE_CHANGE_PAUSED_TO_READY:
915       gst_speex_dec_reset (dec);
916       break;
917     case GST_STATE_CHANGE_READY_TO_NULL:
918       break;
919     default:
920       break;
921   }
922
923   return ret;
924 }