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