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