Fix debug statements
[platform/upstream/gstreamer.git] / ext / vorbis / gstvorbisdec.c
1 /* GStreamer
2  * Copyright (C) 2004 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-vorbisdec
22  * @see_also: vorbisenc, oggdemux
23  *
24  * This element decodes a Vorbis stream to raw float audio.
25  * <ulink url="http://www.vorbis.com/">Vorbis</ulink> is a royalty-free
26  * audio codec maintained by the <ulink url="http://www.xiph.org/">Xiph.org
27  * Foundation</ulink>.
28  *
29  * <refsect2>
30  * <title>Example pipelines</title>
31  * |[
32  * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! alsasink
33  * ]| Decode an Ogg/Vorbis. To create an Ogg/Vorbis file refer to the documentation of vorbisenc.
34  * </refsect2>
35  *
36  * Last reviewed on 2006-03-01 (0.10.4)
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #  include "config.h"
41 #endif
42
43 #include "gstvorbisdec.h"
44 #include <string.h>
45 #include <gst/audio/audio.h>
46 #include <gst/tag/tag.h>
47 #include <gst/audio/multichannel.h>
48
49 #include "gstvorbiscommon.h"
50
51 GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug);
52 #define GST_CAT_DEFAULT vorbisdec_debug
53
54 static GstStaticPadTemplate vorbis_dec_src_factory =
55 GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_VORBIS_DEC_SRC_CAPS);
59
60 static GstStaticPadTemplate vorbis_dec_sink_factory =
61 GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("audio/x-vorbis")
65     );
66
67 #define gst_vorbis_dec_parent_class parent_class
68 G_DEFINE_TYPE (GST_VORBIS_DEC_GLIB_TYPE_NAME, gst_vorbis_dec, GST_TYPE_ELEMENT);
69
70 static void vorbis_dec_finalize (GObject * object);
71 static gboolean vorbis_dec_sink_event (GstPad * pad, GstEvent * event);
72 static GstFlowReturn vorbis_dec_chain (GstPad * pad, GstBuffer * buffer);
73 static GstFlowReturn vorbis_dec_chain_forward (GstVorbisDec * vd,
74     gboolean discont, GstBuffer * buffer);
75 static GstFlowReturn vorbis_dec_chain_reverse (GstVorbisDec * vd,
76     gboolean discont, GstBuffer * buf);
77 static GstStateChangeReturn vorbis_dec_change_state (GstElement * element,
78     GstStateChange transition);
79
80 static gboolean vorbis_dec_src_event (GstPad * pad, GstEvent * event);
81 static gboolean vorbis_dec_src_query (GstPad * pad, GstQuery * query);
82 static gboolean vorbis_dec_convert (GstPad * pad,
83     GstFormat src_format, gint64 src_value,
84     GstFormat * dest_format, gint64 * dest_value);
85
86 static gboolean vorbis_dec_sink_query (GstPad * pad, GstQuery * query);
87
88 static void
89 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
90 {
91   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
92   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
93
94   gobject_class->finalize = vorbis_dec_finalize;
95
96   gst_element_class_add_pad_template (gstelement_class,
97       gst_static_pad_template_get (&vorbis_dec_src_factory));
98
99   gst_element_class_add_pad_template (gstelement_class,
100       gst_static_pad_template_get (&vorbis_dec_sink_factory));
101
102   gst_element_class_set_details_simple (gstelement_class,
103       "Vorbis audio decoder", "Codec/Decoder/Audio",
104       GST_VORBIS_DEC_DESCRIPTION,
105       "Benjamin Otte <otte@gnome.org>, Chris Lord <chris@openedhand.com>");
106
107   gstelement_class->change_state = GST_DEBUG_FUNCPTR (vorbis_dec_change_state);
108 }
109
110 static const GstQueryType *
111 vorbis_get_query_types (GstPad * pad)
112 {
113   static const GstQueryType vorbis_dec_src_query_types[] = {
114     GST_QUERY_POSITION,
115     GST_QUERY_DURATION,
116     GST_QUERY_CONVERT,
117     0
118   };
119
120   return vorbis_dec_src_query_types;
121 }
122
123 static void
124 gst_vorbis_dec_init (GstVorbisDec * dec)
125 {
126   dec->sinkpad = gst_pad_new_from_static_template (&vorbis_dec_sink_factory,
127       "sink");
128
129   gst_pad_set_event_function (dec->sinkpad,
130       GST_DEBUG_FUNCPTR (vorbis_dec_sink_event));
131   gst_pad_set_chain_function (dec->sinkpad,
132       GST_DEBUG_FUNCPTR (vorbis_dec_chain));
133   gst_pad_set_query_function (dec->sinkpad,
134       GST_DEBUG_FUNCPTR (vorbis_dec_sink_query));
135   gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
136
137   dec->srcpad = gst_pad_new_from_static_template (&vorbis_dec_src_factory,
138       "src");
139
140   gst_pad_set_event_function (dec->srcpad,
141       GST_DEBUG_FUNCPTR (vorbis_dec_src_event));
142   gst_pad_set_query_type_function (dec->srcpad,
143       GST_DEBUG_FUNCPTR (vorbis_get_query_types));
144   gst_pad_set_query_function (dec->srcpad,
145       GST_DEBUG_FUNCPTR (vorbis_dec_src_query));
146   gst_pad_use_fixed_caps (dec->srcpad);
147   gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
148
149   dec->queued = NULL;
150   dec->pendingevents = NULL;
151   dec->taglist = NULL;
152 }
153
154 static void
155 vorbis_dec_finalize (GObject * object)
156 {
157   /* Release any possibly allocated libvorbis data.
158    * _clear functions can safely be called multiple times
159    */
160   GstVorbisDec *vd = GST_VORBIS_DEC (object);
161
162 #ifndef USE_TREMOLO
163   vorbis_block_clear (&vd->vb);
164 #endif
165
166   vorbis_dsp_clear (&vd->vd);
167   vorbis_comment_clear (&vd->vc);
168   vorbis_info_clear (&vd->vi);
169
170   G_OBJECT_CLASS (parent_class)->finalize (object);
171 }
172
173 static void
174 gst_vorbis_dec_reset (GstVorbisDec * dec)
175 {
176   dec->last_timestamp = GST_CLOCK_TIME_NONE;
177   dec->discont = TRUE;
178   dec->seqnum = gst_util_seqnum_next ();
179   gst_segment_init (&dec->segment, GST_FORMAT_TIME);
180
181   g_list_foreach (dec->queued, (GFunc) gst_mini_object_unref, NULL);
182   g_list_free (dec->queued);
183   dec->queued = NULL;
184   g_list_foreach (dec->gather, (GFunc) gst_mini_object_unref, NULL);
185   g_list_free (dec->gather);
186   dec->gather = NULL;
187   g_list_foreach (dec->decode, (GFunc) gst_mini_object_unref, NULL);
188   g_list_free (dec->decode);
189   dec->decode = NULL;
190   g_list_foreach (dec->pendingevents, (GFunc) gst_mini_object_unref, NULL);
191   g_list_free (dec->pendingevents);
192   dec->pendingevents = NULL;
193
194   if (dec->taglist)
195     gst_tag_list_free (dec->taglist);
196   dec->taglist = NULL;
197 }
198
199
200 static gboolean
201 vorbis_dec_convert (GstPad * pad,
202     GstFormat src_format, gint64 src_value,
203     GstFormat * dest_format, gint64 * dest_value)
204 {
205   gboolean res = TRUE;
206   GstVorbisDec *dec;
207   guint64 scale = 1;
208
209   if (src_format == *dest_format) {
210     *dest_value = src_value;
211     return TRUE;
212   }
213
214   dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
215
216   if (!dec->initialized)
217     goto no_header;
218
219   if (dec->sinkpad == pad &&
220       (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
221     goto no_format;
222
223   switch (src_format) {
224     case GST_FORMAT_TIME:
225       switch (*dest_format) {
226         case GST_FORMAT_BYTES:
227           scale = dec->width * dec->vi.channels;
228         case GST_FORMAT_DEFAULT:
229           *dest_value =
230               scale * gst_util_uint64_scale_int (src_value, dec->vi.rate,
231               GST_SECOND);
232           break;
233         default:
234           res = FALSE;
235       }
236       break;
237     case GST_FORMAT_DEFAULT:
238       switch (*dest_format) {
239         case GST_FORMAT_BYTES:
240           *dest_value = src_value * dec->width * dec->vi.channels;
241           break;
242         case GST_FORMAT_TIME:
243           *dest_value =
244               gst_util_uint64_scale_int (src_value, GST_SECOND, dec->vi.rate);
245           break;
246         default:
247           res = FALSE;
248       }
249       break;
250     case GST_FORMAT_BYTES:
251       switch (*dest_format) {
252         case GST_FORMAT_DEFAULT:
253           *dest_value = src_value / (dec->width * dec->vi.channels);
254           break;
255         case GST_FORMAT_TIME:
256           *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
257               dec->vi.rate * dec->width * dec->vi.channels);
258           break;
259         default:
260           res = FALSE;
261       }
262       break;
263     default:
264       res = FALSE;
265   }
266 done:
267   gst_object_unref (dec);
268
269   return res;
270
271   /* ERRORS */
272 no_header:
273   {
274     GST_DEBUG_OBJECT (dec, "no header packets received");
275     res = FALSE;
276     goto done;
277   }
278 no_format:
279   {
280     GST_DEBUG_OBJECT (dec, "formats unsupported");
281     res = FALSE;
282     goto done;
283   }
284 }
285
286 static gboolean
287 vorbis_dec_src_query (GstPad * pad, GstQuery * query)
288 {
289   GstVorbisDec *dec;
290   gboolean res = FALSE;
291
292   dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
293   if (G_UNLIKELY (dec == NULL))
294     return FALSE;
295
296   switch (GST_QUERY_TYPE (query)) {
297     case GST_QUERY_POSITION:
298     {
299       gint64 value;
300       GstFormat format;
301       gint64 time;
302
303       gst_query_parse_position (query, &format, NULL);
304
305       /* we start from the last seen time */
306       time = dec->last_timestamp;
307       /* correct for the segment values */
308       time = gst_segment_to_stream_time (&dec->segment, GST_FORMAT_TIME, time);
309
310       GST_LOG_OBJECT (dec,
311           "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
312
313       /* and convert to the final format */
314       if (!(res =
315               vorbis_dec_convert (pad, GST_FORMAT_TIME, time, &format, &value)))
316         goto error;
317
318       gst_query_set_position (query, format, value);
319
320       GST_LOG_OBJECT (dec,
321           "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
322           format);
323
324       break;
325     }
326     case GST_QUERY_DURATION:
327     {
328       res = gst_pad_peer_query (dec->sinkpad, query);
329       if (!res)
330         goto error;
331
332       break;
333     }
334     case GST_QUERY_CONVERT:
335     {
336       GstFormat src_fmt, dest_fmt;
337       gint64 src_val, dest_val;
338
339       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
340       if (!(res =
341               vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
342         goto error;
343       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
344       break;
345     }
346     default:
347       res = gst_pad_query_default (pad, query);
348       break;
349   }
350 done:
351   gst_object_unref (dec);
352
353   return res;
354
355   /* ERRORS */
356 error:
357   {
358     GST_WARNING_OBJECT (dec, "error handling query");
359     goto done;
360   }
361 }
362
363 static gboolean
364 vorbis_dec_sink_query (GstPad * pad, GstQuery * query)
365 {
366   GstVorbisDec *dec;
367   gboolean res;
368
369   dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
370
371   switch (GST_QUERY_TYPE (query)) {
372     case GST_QUERY_CONVERT:
373     {
374       GstFormat src_fmt, dest_fmt;
375       gint64 src_val, dest_val;
376
377       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
378       if (!(res =
379               vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
380         goto error;
381       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
382       break;
383     }
384     default:
385       res = gst_pad_query_default (pad, query);
386       break;
387   }
388
389 done:
390   gst_object_unref (dec);
391
392   return res;
393
394   /* ERRORS */
395 error:
396   {
397     GST_DEBUG_OBJECT (dec, "error converting value");
398     goto done;
399   }
400 }
401
402 static gboolean
403 vorbis_dec_src_event (GstPad * pad, GstEvent * event)
404 {
405   gboolean res = TRUE;
406   GstVorbisDec *dec;
407
408   dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
409   if (G_UNLIKELY (dec == NULL)) {
410     gst_event_unref (event);
411     return FALSE;
412   }
413
414   switch (GST_EVENT_TYPE (event)) {
415     case GST_EVENT_SEEK:
416     {
417       GstFormat format, tformat;
418       gdouble rate;
419       GstEvent *real_seek;
420       GstSeekFlags flags;
421       GstSeekType cur_type, stop_type;
422       gint64 cur, stop;
423       gint64 tcur, tstop;
424       guint32 seqnum;
425
426       gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
427           &stop_type, &stop);
428       seqnum = gst_event_get_seqnum (event);
429       gst_event_unref (event);
430
431       /* First bring the requested format to time */
432       tformat = GST_FORMAT_TIME;
433       if (!(res = vorbis_dec_convert (pad, format, cur, &tformat, &tcur)))
434         goto convert_error;
435       if (!(res = vorbis_dec_convert (pad, format, stop, &tformat, &tstop)))
436         goto convert_error;
437
438       /* then seek with time on the peer */
439       real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
440           flags, cur_type, tcur, stop_type, tstop);
441       gst_event_set_seqnum (real_seek, seqnum);
442
443       res = gst_pad_push_event (dec->sinkpad, real_seek);
444       break;
445     }
446     default:
447       res = gst_pad_push_event (dec->sinkpad, event);
448       break;
449   }
450 done:
451   gst_object_unref (dec);
452
453   return res;
454
455   /* ERRORS */
456 convert_error:
457   {
458     GST_DEBUG_OBJECT (dec, "cannot convert start/stop for seek");
459     goto done;
460   }
461 }
462
463 static gboolean
464 vorbis_dec_sink_event (GstPad * pad, GstEvent * event)
465 {
466   gboolean ret = FALSE;
467   GstVorbisDec *dec;
468
469   dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
470
471   GST_LOG_OBJECT (dec, "handling event");
472   switch (GST_EVENT_TYPE (event)) {
473     case GST_EVENT_EOS:
474       if (dec->segment.rate < 0.0)
475         vorbis_dec_chain_reverse (dec, TRUE, NULL);
476       ret = gst_pad_push_event (dec->srcpad, event);
477       break;
478     case GST_EVENT_FLUSH_START:
479       ret = gst_pad_push_event (dec->srcpad, event);
480       break;
481     case GST_EVENT_FLUSH_STOP:
482       /* here we must clean any state in the decoder */
483 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
484       vorbis_synthesis_restart (&dec->vd);
485 #endif
486       gst_vorbis_dec_reset (dec);
487       ret = gst_pad_push_event (dec->srcpad, event);
488       break;
489     case GST_EVENT_SEGMENT:
490     {
491       const GstSegment *segment;
492
493       gst_event_parse_segment (event, &segment);
494
495       /* we need time for now */
496       if (segment->format != GST_FORMAT_TIME)
497         goto newseg_wrong_format;
498
499       GST_DEBUG_OBJECT (dec, "segment: %" GST_SEGMENT_FORMAT, segment);
500
501       /* now configure the values */
502       gst_segment_copy_into (segment, &dec->segment);
503       dec->seqnum = gst_event_get_seqnum (event);
504
505       if (dec->initialized)
506         /* and forward */
507         ret = gst_pad_push_event (dec->srcpad, event);
508       else {
509         /* store it to send once we're initialized */
510         dec->pendingevents = g_list_append (dec->pendingevents, event);
511         ret = TRUE;
512       }
513       break;
514     }
515     case GST_EVENT_TAG:
516     {
517       if (dec->initialized)
518         /* and forward */
519         ret = gst_pad_push_event (dec->srcpad, event);
520       else {
521         /* store it to send once we're initialized */
522         dec->pendingevents = g_list_append (dec->pendingevents, event);
523         ret = TRUE;
524       }
525       break;
526     }
527     default:
528       ret = gst_pad_event_default (pad, event);
529       break;
530   }
531 done:
532   gst_object_unref (dec);
533
534   return ret;
535
536   /* ERRORS */
537 newseg_wrong_format:
538   {
539     GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
540     goto done;
541   }
542 }
543
544 static GstFlowReturn
545 vorbis_handle_identification_packet (GstVorbisDec * vd)
546 {
547   GstCaps *caps;
548   const GstAudioChannelPosition *pos = NULL;
549   gint width = GST_VORBIS_DEC_DEFAULT_SAMPLE_WIDTH;
550
551   switch (vd->vi.channels) {
552     case 1:
553     case 2:
554       /* nothing */
555       break;
556     case 3:
557     case 4:
558     case 5:
559     case 6:
560     case 7:
561     case 8:
562       pos = gst_vorbis_channel_positions[vd->vi.channels - 1];
563       break;
564     default:{
565       gint i;
566       GstAudioChannelPosition *posn =
567           g_new (GstAudioChannelPosition, vd->vi.channels);
568
569       GST_ELEMENT_WARNING (GST_ELEMENT (vd), STREAM, DECODE,
570           (NULL), ("Using NONE channel layout for more than 8 channels"));
571
572       for (i = 0; i < vd->vi.channels; i++)
573         posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
574
575       pos = posn;
576     }
577   }
578
579   /* negotiate width with downstream */
580   caps = gst_pad_get_allowed_caps (vd->srcpad);
581   if (caps) {
582     if (!gst_caps_is_empty (caps)) {
583       GstStructure *s;
584
585       s = gst_caps_get_structure (caps, 0);
586       /* template ensures 16 or 32 */
587       gst_structure_get_int (s, "width", &width);
588
589       GST_INFO_OBJECT (vd, "using %s with %d channels and %d bit audio depth",
590           gst_structure_get_name (s), vd->vi.channels, width);
591     }
592     gst_caps_unref (caps);
593   }
594   vd->width = width >> 3;
595
596   /* select a copy_samples function, this way we can have specialized versions
597    * for mono/stereo and avoid the depth switch in tremor case */
598   vd->copy_samples = get_copy_sample_func (vd->vi.channels, vd->width);
599
600   caps = gst_caps_make_writable (gst_pad_get_pad_template_caps (vd->srcpad));
601   gst_caps_set_simple (caps, "rate", G_TYPE_INT, vd->vi.rate,
602       "channels", G_TYPE_INT, vd->vi.channels,
603       "width", G_TYPE_INT, width, NULL);
604
605   if (pos) {
606     gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
607   }
608
609   if (vd->vi.channels > 8) {
610     g_free ((GstAudioChannelPosition *) pos);
611   }
612
613   gst_pad_set_caps (vd->srcpad, caps);
614   gst_caps_unref (caps);
615
616   return GST_FLOW_OK;
617 }
618
619 static GstFlowReturn
620 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
621 {
622   guint bitrate = 0;
623   gchar *encoder = NULL;
624   GstTagList *list, *old_list;
625   guint8 *data;
626   gsize size;
627
628   GST_DEBUG_OBJECT (vd, "parsing comment packet");
629
630   data = gst_ogg_packet_data (packet);
631   size = gst_ogg_packet_size (packet);
632
633   list =
634       gst_tag_list_from_vorbiscomment (data, size, (guint8 *) "\003vorbis", 7,
635       &encoder);
636
637   old_list = vd->taglist;
638   vd->taglist = gst_tag_list_merge (vd->taglist, list, GST_TAG_MERGE_REPLACE);
639
640   if (old_list)
641     gst_tag_list_free (old_list);
642   gst_tag_list_free (list);
643
644   if (!vd->taglist) {
645     GST_ERROR_OBJECT (vd, "couldn't decode comments");
646     vd->taglist = gst_tag_list_new ();
647   }
648   if (encoder) {
649     if (encoder[0])
650       gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
651           GST_TAG_ENCODER, encoder, NULL);
652     g_free (encoder);
653   }
654   gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
655       GST_TAG_ENCODER_VERSION, vd->vi.version,
656       GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
657   if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
658     gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
659         GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
660     bitrate = vd->vi.bitrate_nominal;
661   }
662   if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
663     gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
664         GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
665     if (!bitrate)
666       bitrate = vd->vi.bitrate_upper;
667   }
668   if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
669     gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
670         GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
671     if (!bitrate)
672       bitrate = vd->vi.bitrate_lower;
673   }
674   if (bitrate) {
675     gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
676         GST_TAG_BITRATE, (guint) bitrate, NULL);
677   }
678
679   if (vd->initialized) {
680     gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd), vd->srcpad,
681         vd->taglist);
682     vd->taglist = NULL;
683   } else {
684     /* Only post them as messages for the time being. *
685      * They will be pushed on the pad once the decoder is initialized */
686     gst_element_post_message (GST_ELEMENT_CAST (vd),
687         gst_message_new_tag (GST_OBJECT (vd), gst_tag_list_copy (vd->taglist)));
688   }
689
690   return GST_FLOW_OK;
691 }
692
693 static GstFlowReturn
694 vorbis_handle_type_packet (GstVorbisDec * vd)
695 {
696   GList *walk;
697   gint res;
698
699   g_assert (vd->initialized == FALSE);
700
701 #ifdef USE_TREMOLO
702   if (G_UNLIKELY ((res = vorbis_dsp_init (&vd->vd, &vd->vi))))
703     goto synthesis_init_error;
704 #else
705   if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
706     goto synthesis_init_error;
707
708   if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
709     goto block_init_error;
710 #endif
711
712   vd->initialized = TRUE;
713
714   if (vd->pendingevents) {
715     for (walk = vd->pendingevents; walk; walk = g_list_next (walk))
716       gst_pad_push_event (vd->srcpad, GST_EVENT_CAST (walk->data));
717     g_list_free (vd->pendingevents);
718     vd->pendingevents = NULL;
719   }
720
721   if (vd->taglist) {
722     /* The tags have already been sent on the bus as messages. */
723     gst_pad_push_event (vd->srcpad, gst_event_new_tag (vd->taglist));
724     vd->taglist = NULL;
725   }
726   return GST_FLOW_OK;
727
728   /* ERRORS */
729 synthesis_init_error:
730   {
731     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
732         (NULL), ("couldn't initialize synthesis (%d)", res));
733     return GST_FLOW_ERROR;
734   }
735 block_init_error:
736   {
737     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
738         (NULL), ("couldn't initialize block (%d)", res));
739     return GST_FLOW_ERROR;
740   }
741 }
742
743 static GstFlowReturn
744 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
745 {
746   GstFlowReturn res;
747   gint ret;
748
749   GST_DEBUG_OBJECT (vd, "parsing header packet");
750
751   /* Packetno = 0 if the first byte is exactly 0x01 */
752   packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
753
754 #ifdef USE_TREMOLO
755   if ((ret = vorbis_dsp_headerin (&vd->vi, &vd->vc, packet)))
756 #else
757   if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
758 #endif
759     goto header_read_error;
760
761   switch ((gst_ogg_packet_data (packet))[0]) {
762     case 0x01:
763       res = vorbis_handle_identification_packet (vd);
764       break;
765     case 0x03:
766       res = vorbis_handle_comment_packet (vd, packet);
767       break;
768     case 0x05:
769       res = vorbis_handle_type_packet (vd);
770       break;
771     default:
772       /* ignore */
773       g_warning ("unknown vorbis header packet found");
774       res = GST_FLOW_OK;
775       break;
776   }
777   return res;
778
779   /* ERRORS */
780 header_read_error:
781   {
782     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
783         (NULL), ("couldn't read header packet (%d)", ret));
784     return GST_FLOW_ERROR;
785   }
786 }
787
788 static GstFlowReturn
789 vorbis_dec_push_forward (GstVorbisDec * dec, GstBuffer * buf)
790 {
791   GstFlowReturn result;
792
793   /* clip */
794   if (!(buf = gst_audio_buffer_clip (buf, &dec->segment, dec->vi.rate,
795               dec->vi.channels * dec->width))) {
796     GST_LOG_OBJECT (dec, "clipped buffer");
797     return GST_FLOW_OK;
798   }
799
800   if (dec->discont) {
801     GST_LOG_OBJECT (dec, "setting DISCONT");
802     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
803     dec->discont = FALSE;
804   }
805
806   GST_DEBUG_OBJECT (dec,
807       "pushing time %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
808       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
809       GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
810
811   result = gst_pad_push (dec->srcpad, buf);
812
813   return result;
814 }
815
816 static GstFlowReturn
817 vorbis_dec_push_reverse (GstVorbisDec * dec, GstBuffer * buf)
818 {
819   GstFlowReturn result = GST_FLOW_OK;
820
821   dec->queued = g_list_prepend (dec->queued, buf);
822
823   return result;
824 }
825
826 static void
827 vorbis_do_timestamps (GstVorbisDec * vd, GstBuffer * buf, gboolean reverse,
828     GstClockTime timestamp, GstClockTime duration)
829 {
830   /* interpolate reverse */
831   if (vd->last_timestamp != -1 && duration != -1 && reverse)
832     vd->last_timestamp -= duration;
833
834   /* take buffer timestamp, use interpolated timestamp otherwise */
835   if (timestamp != -1)
836     vd->last_timestamp = timestamp;
837   else
838     timestamp = vd->last_timestamp;
839
840   /* interpolate forwards */
841   if (vd->last_timestamp != -1 && duration != -1 && !reverse)
842     vd->last_timestamp += duration;
843
844   GST_LOG_OBJECT (vd,
845       "keeping timestamp %" GST_TIME_FORMAT " ts %" GST_TIME_FORMAT " dur %"
846       GST_TIME_FORMAT, GST_TIME_ARGS (vd->last_timestamp),
847       GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
848
849   if (buf) {
850     GST_BUFFER_TIMESTAMP (buf) = timestamp;
851     GST_BUFFER_DURATION (buf) = duration;
852   }
853 }
854
855 static GstFlowReturn
856 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
857     GstClockTime timestamp, GstClockTime duration)
858 {
859 #ifdef USE_TREMOLO
860   vorbis_sample_t *pcm;
861 #else
862   vorbis_sample_t **pcm;
863 #endif
864   guint sample_count;
865   GstBuffer *out = NULL;
866   GstFlowReturn result;
867   guint8 *data;
868   gsize size;
869
870   if (G_UNLIKELY (!vd->initialized))
871     goto not_initialized;
872
873   /* normal data packet */
874   /* FIXME, we can skip decoding if the packet is outside of the
875    * segment, this is however not very trivial as we need a previous
876    * packet to decode the current one so we must be carefull not to
877    * throw away too much. For now we decode everything and clip right
878    * before pushing data. */
879
880 #ifdef USE_TREMOLO
881   if (G_UNLIKELY (vorbis_dsp_synthesis (&vd->vd, packet, 1)))
882     goto could_not_read;
883 #else
884   if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
885     goto could_not_read;
886
887   if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
888     goto not_accepted;
889 #endif
890
891   /* assume all goes well here */
892   result = GST_FLOW_OK;
893
894   /* count samples ready for reading */
895 #ifdef USE_TREMOLO
896   if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0)
897 #else
898   if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
899 #endif
900     goto done;
901
902   size = sample_count * vd->vi.channels * vd->width;
903   GST_LOG_OBJECT (vd, "%d samples ready for reading, size %" G_GSIZE_FORMAT,
904       sample_count, size);
905
906   /* alloc buffer for it */
907   out = gst_buffer_new_and_alloc (size);
908
909   /* get samples ready for reading now, should be sample_count */
910 #ifdef USE_TREMOLO
911   pcm = GST_BUFFER_DATA (out);
912   if (G_UNLIKELY ((vorbis_dsp_pcmout (&vd->vd, pcm,
913                   sample_count)) != sample_count))
914 #else
915   if (G_UNLIKELY ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count))
916 #endif
917     goto wrong_samples;
918
919 #ifndef USE_TREMOLO
920   /* copy samples in buffer */
921   data = gst_buffer_map (out, NULL, NULL, GST_MAP_WRITE);
922   vd->copy_samples ((vorbis_sample_t *) data, pcm,
923       sample_count, vd->vi.channels, vd->width);
924 #endif
925
926   GST_LOG_OBJECT (vd, "setting output size to %" G_GSIZE_FORMAT, size);
927   gst_buffer_unmap (out, data, size);
928
929   /* this should not overflow */
930   if (duration == -1)
931     duration = sample_count * GST_SECOND / vd->vi.rate;
932
933   vorbis_do_timestamps (vd, out, FALSE, timestamp, duration);
934
935   if (vd->segment.rate >= 0.0)
936     result = vorbis_dec_push_forward (vd, out);
937   else
938     result = vorbis_dec_push_reverse (vd, out);
939
940 done:
941   if (out == NULL) {
942     /* no output, still keep track of timestamps */
943     vorbis_do_timestamps (vd, NULL, FALSE, timestamp, duration);
944   }
945 #ifdef USE_TREMOLO
946   vorbis_dsp_read (&vd->vd, sample_count);
947 #else
948   vorbis_synthesis_read (&vd->vd, sample_count);
949 #endif
950
951   return result;
952
953   /* ERRORS */
954 not_initialized:
955   {
956     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
957         (NULL), ("no header sent yet"));
958     return GST_FLOW_ERROR;
959   }
960 could_not_read:
961   {
962     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
963         (NULL), ("couldn't read data packet"));
964     return GST_FLOW_ERROR;
965   }
966 not_accepted:
967   {
968     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
969         (NULL), ("vorbis decoder did not accept data packet"));
970     return GST_FLOW_ERROR;
971   }
972 wrong_samples:
973   {
974     gst_buffer_unref (out);
975     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
976         (NULL), ("vorbis decoder reported wrong number of samples"));
977     return GST_FLOW_ERROR;
978   }
979 }
980
981 static GstFlowReturn
982 vorbis_dec_handle_header_buffer (GstVorbisDec * vd, GstBuffer * buffer)
983 {
984   ogg_packet *packet;
985   ogg_packet_wrapper packet_wrapper;
986   GstFlowReturn ret;
987
988   gst_ogg_packet_wrapper_map (&packet_wrapper, buffer);
989   packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
990
991   ret = vorbis_handle_header_packet (vd, packet);
992
993   gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer);
994
995   return ret;
996 }
997
998
999 #define MIN_NUM_HEADERS 3
1000 static GstFlowReturn
1001 vorbis_dec_handle_header_caps (GstVorbisDec * vd, GstBuffer * buffer)
1002 {
1003   GstFlowReturn result = GST_FLOW_OK;
1004   GstCaps *caps;
1005   GstStructure *s;
1006   const GValue *array;
1007   const GValue *value = NULL;
1008   GstBuffer *buf = NULL;
1009
1010   if ((caps = gst_pad_get_current_caps (vd->sinkpad)) == NULL)
1011     goto no_caps;
1012
1013   if ((s = gst_caps_get_structure (caps, 0)) == NULL)
1014     goto no_caps;
1015
1016   array = gst_structure_get_value (s, "streamheader");
1017
1018   if (array == NULL || (gst_value_array_get_size (array) < MIN_NUM_HEADERS))
1019     goto array_error;
1020
1021   /* initial header */
1022   value = gst_value_array_get_value (array, 0);
1023   buf = gst_value_get_buffer (value);
1024   if (!buf)
1025     goto null_buffer;
1026   result = vorbis_dec_handle_header_buffer (vd, buf);
1027   if (result != GST_FLOW_OK)
1028     goto buffer_error;
1029
1030   /* comment header */
1031   value = gst_value_array_get_value (array, 1);
1032   buf = gst_value_get_buffer (value);
1033   if (!buf)
1034     goto null_buffer;
1035   result = vorbis_dec_handle_header_buffer (vd, buf);
1036   if (result != GST_FLOW_OK)
1037     goto buffer_error;
1038
1039   /* bitstream codebook header */
1040   value = gst_value_array_get_value (array, 2);
1041   buf = gst_value_get_buffer (value);
1042   if (!buf)
1043     goto null_buffer;
1044   result = vorbis_dec_handle_header_buffer (vd, buf);
1045   if (result != GST_FLOW_OK)
1046     goto buffer_error;
1047
1048   return result;
1049
1050 no_caps:
1051   {
1052     GST_WARNING_OBJECT (vd, "no caps negotiated");
1053     return GST_FLOW_NOT_NEGOTIATED;
1054   }
1055 array_error:
1056   {
1057     GST_WARNING_OBJECT (vd, "streamheader array not found");
1058     return GST_FLOW_NOT_NEGOTIATED;
1059   }
1060 null_buffer:
1061   {
1062     GST_WARNING_OBJECT (vd, "streamheader with null buffer received");
1063     return GST_FLOW_NOT_NEGOTIATED;
1064   }
1065 buffer_error:
1066   {
1067     GST_WARNING_OBJECT (vd, "error handling buffer");
1068     return GST_FLOW_NOT_NEGOTIATED;
1069   }
1070 }
1071
1072 static GstFlowReturn
1073 vorbis_dec_decode_buffer (GstVorbisDec * vd, GstBuffer * buffer)
1074 {
1075   ogg_packet *packet;
1076   ogg_packet_wrapper packet_wrapper;
1077   GstFlowReturn result = GST_FLOW_OK;
1078
1079   /* make ogg_packet out of the buffer */
1080   gst_ogg_packet_wrapper_map (&packet_wrapper, buffer);
1081   packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
1082   /* set some more stuff */
1083   packet->granulepos = -1;
1084   packet->packetno = 0;         /* we don't care */
1085   /* EOS does not matter, it is used in vorbis to implement clipping the last
1086    * block of samples based on the granulepos. We clip based on segments. */
1087   packet->e_o_s = 0;
1088
1089   GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
1090
1091   /* error out on empty header packets, but just skip empty data packets */
1092   if (G_UNLIKELY (packet->bytes == 0)) {
1093     if (vd->initialized)
1094       goto empty_buffer;
1095     else
1096       goto empty_header;
1097   }
1098
1099   /* switch depending on packet type */
1100   if ((gst_ogg_packet_data (packet))[0] & 1) {
1101     if (vd->initialized) {
1102       GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
1103       goto done;
1104     }
1105     result = vorbis_handle_header_packet (vd, packet);
1106   } else {
1107     GstClockTime timestamp, duration;
1108
1109     /* try to find header in caps so we can initialize the decoder */
1110     if (!vd->initialized) {
1111       result = vorbis_dec_handle_header_caps (vd, buffer);
1112       if (result != GST_FLOW_OK)
1113         goto invalid_caps_header;
1114     }
1115
1116     timestamp = GST_BUFFER_TIMESTAMP (buffer);
1117     duration = GST_BUFFER_DURATION (buffer);
1118
1119     result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
1120   }
1121
1122 done:
1123   gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer);
1124
1125   return result;
1126
1127 empty_buffer:
1128   {
1129     /* don't error out here, just ignore the buffer, it's invalid for vorbis
1130      * but not fatal. */
1131     GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
1132     result = GST_FLOW_OK;
1133     goto done;
1134   }
1135
1136 /* ERRORS */
1137 empty_header:
1138   {
1139     GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
1140     result = GST_FLOW_ERROR;
1141     vd->discont = TRUE;
1142     goto done;
1143   }
1144
1145 invalid_caps_header:
1146   {
1147     GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL),
1148         ("invalid streamheader in caps"));
1149     goto done;
1150   }
1151 }
1152
1153 /*
1154  * Input:
1155  *  Buffer decoding order:  7  8  9  4  5  6  3  1  2  EOS
1156  *  Discont flag:           D        D        D  D
1157  *
1158  * - Each Discont marks a discont in the decoding order.
1159  *
1160  * for vorbis, each buffer is a keyframe when we have the previous
1161  * buffer. This means that to decode buffer 7, we need buffer 6, which
1162  * arrives out of order.
1163  *
1164  * we first gather buffers in the gather queue until we get a DISCONT. We
1165  * prepend each incomming buffer so that they are in reversed order.
1166  *
1167  *    gather queue:    9  8  7
1168  *    decode queue:
1169  *    output queue:
1170  *
1171  * When a DISCONT is received (buffer 4), we move the gather queue to the
1172  * decode queue. This is simply done be taking the head of the gather queue
1173  * and prepending it to the decode queue. This yields:
1174  *
1175  *    gather queue:
1176  *    decode queue:    7  8  9
1177  *    output queue:
1178  *
1179  * Then we decode each buffer in the decode queue in order and put the output
1180  * buffer in the output queue. The first buffer (7) will not produce any output
1181  * because it needs the previous buffer (6) which did not arrive yet. This
1182  * yields:
1183  *
1184  *    gather queue:
1185  *    decode queue:    7  8  9
1186  *    output queue:    9  8
1187  *
1188  * Then we remove the consumed buffers from the decode queue. Buffer 7 is not
1189  * completely consumed, we need to keep it around for when we receive buffer
1190  * 6. This yields:
1191  *
1192  *    gather queue:
1193  *    decode queue:    7
1194  *    output queue:    9  8
1195  *
1196  * Then we accumulate more buffers:
1197  *
1198  *    gather queue:    6  5  4
1199  *    decode queue:    7
1200  *    output queue:
1201  *
1202  * prepending to the decode queue on DISCONT yields:
1203  *
1204  *    gather queue:
1205  *    decode queue:    4  5  6  7
1206  *    output queue:
1207  *
1208  * after decoding and keeping buffer 4:
1209  *
1210  *    gather queue:
1211  *    decode queue:    4
1212  *    output queue:    7  6  5
1213  *
1214  * Etc..
1215  */
1216 static GstFlowReturn
1217 vorbis_dec_flush_decode (GstVorbisDec * dec)
1218 {
1219   GstFlowReturn res = GST_FLOW_OK;
1220   GList *walk;
1221
1222   walk = dec->decode;
1223
1224   GST_DEBUG_OBJECT (dec, "flushing buffers to decoder");
1225
1226   while (walk) {
1227     GList *next;
1228     GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1229
1230     GST_DEBUG_OBJECT (dec, "decoding buffer %p, ts %" GST_TIME_FORMAT,
1231         buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1232
1233     next = g_list_next (walk);
1234
1235     /* decode buffer, prepend to output queue */
1236     res = vorbis_dec_decode_buffer (dec, buf);
1237
1238     /* if we generated output, we can discard the buffer, else we
1239      * keep it in the queue */
1240     if (dec->queued) {
1241       GST_DEBUG_OBJECT (dec, "decoded buffer to %p", dec->queued->data);
1242       dec->decode = g_list_delete_link (dec->decode, walk);
1243       gst_buffer_unref (buf);
1244     } else {
1245       GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1246     }
1247     walk = next;
1248   }
1249   while (dec->queued) {
1250     GstBuffer *buf = GST_BUFFER_CAST (dec->queued->data);
1251     GstClockTime timestamp, duration;
1252
1253     timestamp = GST_BUFFER_TIMESTAMP (buf);
1254     duration = GST_BUFFER_DURATION (buf);
1255
1256     vorbis_do_timestamps (dec, buf, TRUE, timestamp, duration);
1257     res = vorbis_dec_push_forward (dec, buf);
1258
1259     dec->queued = g_list_delete_link (dec->queued, dec->queued);
1260   }
1261   return res;
1262 }
1263
1264 static GstFlowReturn
1265 vorbis_dec_chain_reverse (GstVorbisDec * vd, gboolean discont, GstBuffer * buf)
1266 {
1267   GstFlowReturn result = GST_FLOW_OK;
1268
1269   /* if we have a discont, move buffers to the decode list */
1270   if (G_UNLIKELY (discont)) {
1271     GST_DEBUG_OBJECT (vd, "received discont");
1272     while (vd->gather) {
1273       GstBuffer *gbuf;
1274
1275       gbuf = GST_BUFFER_CAST (vd->gather->data);
1276       /* remove from the gather list */
1277       vd->gather = g_list_delete_link (vd->gather, vd->gather);
1278       /* copy to decode queue */
1279       vd->decode = g_list_prepend (vd->decode, gbuf);
1280     }
1281     /* flush and decode the decode queue */
1282     result = vorbis_dec_flush_decode (vd);
1283   }
1284
1285   if (G_LIKELY (buf)) {
1286     GST_DEBUG_OBJECT (vd,
1287         "gathering buffer %p of size %" G_GSIZE_FORMAT
1288         ", time %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
1289         buf, gst_buffer_get_size (buf),
1290         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1291         GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1292
1293     /* add buffer to gather queue */
1294     vd->gather = g_list_prepend (vd->gather, buf);
1295   }
1296
1297   return result;
1298 }
1299
1300 static GstFlowReturn
1301 vorbis_dec_chain_forward (GstVorbisDec * vd, gboolean discont,
1302     GstBuffer * buffer)
1303 {
1304   GstFlowReturn result;
1305
1306   result = vorbis_dec_decode_buffer (vd, buffer);
1307
1308   gst_buffer_unref (buffer);
1309
1310   return result;
1311 }
1312
1313 static GstFlowReturn
1314 vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
1315 {
1316   GstVorbisDec *vd;
1317   GstFlowReturn result = GST_FLOW_OK;
1318   gboolean discont;
1319
1320   vd = GST_VORBIS_DEC (gst_pad_get_parent (pad));
1321
1322   discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1323
1324   /* resync on DISCONT */
1325   if (G_UNLIKELY (discont)) {
1326     GST_DEBUG_OBJECT (vd, "received DISCONT buffer");
1327     vd->last_timestamp = GST_CLOCK_TIME_NONE;
1328 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
1329     vorbis_synthesis_restart (&vd->vd);
1330 #endif
1331     vd->discont = TRUE;
1332   }
1333
1334   if (vd->segment.rate >= 0.0)
1335     result = vorbis_dec_chain_forward (vd, discont, buffer);
1336   else
1337     result = vorbis_dec_chain_reverse (vd, discont, buffer);
1338
1339   gst_object_unref (vd);
1340
1341   return result;
1342 }
1343
1344 static GstStateChangeReturn
1345 vorbis_dec_change_state (GstElement * element, GstStateChange transition)
1346 {
1347   GstVorbisDec *vd = GST_VORBIS_DEC (element);
1348   GstStateChangeReturn res;
1349
1350   switch (transition) {
1351     case GST_STATE_CHANGE_NULL_TO_READY:
1352       break;
1353     case GST_STATE_CHANGE_READY_TO_PAUSED:
1354       vorbis_info_init (&vd->vi);
1355       vorbis_comment_init (&vd->vc);
1356       vd->initialized = FALSE;
1357       gst_vorbis_dec_reset (vd);
1358       break;
1359     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1360       break;
1361     default:
1362       break;
1363   }
1364
1365   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1366
1367   switch (transition) {
1368     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1369       break;
1370     case GST_STATE_CHANGE_PAUSED_TO_READY:
1371       GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
1372       vd->initialized = FALSE;
1373
1374 #ifndef USE_TREMOLO
1375       vorbis_block_clear (&vd->vb);
1376 #endif
1377
1378       vorbis_dsp_clear (&vd->vd);
1379       vorbis_comment_clear (&vd->vc);
1380       vorbis_info_clear (&vd->vi);
1381       gst_vorbis_dec_reset (vd);
1382       break;
1383     case GST_STATE_CHANGE_READY_TO_NULL:
1384       break;
1385     default:
1386       break;
1387   }
1388
1389   return res;
1390 }