Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / ext / vorbis / gstvorbisparse.c
1 /* GStreamer
2  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
3  * Copyright (C) 2006 Andy Wingo <wingo@pobox.com>
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-vorbisparse
23  * @see_also: vorbisdec, oggdemux, theoraparse
24  *
25  * The vorbisparse element will parse the header packets of the Vorbis
26  * stream and put them as the streamheader in the caps. This is used in the
27  * multifdsink case where you want to stream live vorbis streams to multiple
28  * clients, each client has to receive the streamheaders first before they can
29  * consume the vorbis packets.
30  *
31  * This element also makes sure that the buffers that it pushes out are properly
32  * timestamped and that their offset and offset_end are set. The buffers that
33  * vorbisparse outputs have all of the metadata that oggmux expects to receive,
34  * which allows you to (for example) remux an ogg/vorbis file.
35  *
36  * <refsect2>
37  * <title>Example pipelines</title>
38  * |[
39  * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisparse ! fakesink
40  * ]| This pipeline shows that the streamheader is set in the caps, and that each
41  * buffer has the timestamp, duration, offset, and offset_end set.
42  * |[
43  * gst-launch filesrc location=sine.ogg ! oggdemux ! vorbisparse \
44  *            ! oggmux ! filesink location=sine-remuxed.ogg
45  * ]| This pipeline shows remuxing. sine-remuxed.ogg might not be exactly the same
46  * as sine.ogg, but they should produce exactly the same decoded data.
47  * </refsect2>
48  *
49  * Last reviewed on 2006-04-01 (0.10.4.1)
50  */
51
52 #ifdef HAVE_CONFIG_H
53 #  include "config.h"
54 #endif
55
56 #include "gstvorbisparse.h"
57
58 GST_DEBUG_CATEGORY_EXTERN (vorbisparse_debug);
59 #define GST_CAT_DEFAULT vorbisparse_debug
60
61 static GstStaticPadTemplate vorbis_parse_sink_factory =
62 GST_STATIC_PAD_TEMPLATE ("sink",
63     GST_PAD_SINK,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("audio/x-vorbis")
66     );
67
68 static GstStaticPadTemplate vorbis_parse_src_factory =
69 GST_STATIC_PAD_TEMPLATE ("src",
70     GST_PAD_SRC,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS ("audio/x-vorbis")
73     );
74
75 GST_BOILERPLATE (GstVorbisParse, gst_vorbis_parse, GstElement,
76     GST_TYPE_ELEMENT);
77
78 static GstFlowReturn vorbis_parse_chain (GstPad * pad, GstBuffer * buffer);
79 static GstStateChangeReturn vorbis_parse_change_state (GstElement * element,
80     GstStateChange transition);
81 static gboolean vorbis_parse_sink_event (GstPad * pad, GstEvent * event);
82 static gboolean vorbis_parse_src_query (GstPad * pad, GstQuery * query);
83 static gboolean vorbis_parse_convert (GstPad * pad,
84     GstFormat src_format, gint64 src_value,
85     GstFormat * dest_format, gint64 * dest_value);
86 static GstFlowReturn vorbis_parse_parse_packet (GstVorbisParse * parse,
87     GstBuffer * buf);
88
89 static void
90 gst_vorbis_parse_base_init (gpointer g_class)
91 {
92   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
93
94   gst_element_class_add_pad_template (element_class,
95       gst_static_pad_template_get (&vorbis_parse_src_factory));
96   gst_element_class_add_pad_template (element_class,
97       gst_static_pad_template_get (&vorbis_parse_sink_factory));
98   gst_element_class_set_details_simple (element_class,
99       "VorbisParse", "Codec/Parser/Audio",
100       "parse raw vorbis streams",
101       "Thomas Vander Stichele <thomas at apestaart dot org>");
102 }
103
104 static void
105 gst_vorbis_parse_class_init (GstVorbisParseClass * klass)
106 {
107   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
108
109   gstelement_class->change_state = vorbis_parse_change_state;
110
111   klass->parse_packet = GST_DEBUG_FUNCPTR (vorbis_parse_parse_packet);
112 }
113
114 static void
115 gst_vorbis_parse_init (GstVorbisParse * parse, GstVorbisParseClass * g_class)
116 {
117   parse->sinkpad =
118       gst_pad_new_from_static_template (&vorbis_parse_sink_factory, "sink");
119   gst_pad_set_chain_function (parse->sinkpad,
120       GST_DEBUG_FUNCPTR (vorbis_parse_chain));
121   gst_pad_set_event_function (parse->sinkpad,
122       GST_DEBUG_FUNCPTR (vorbis_parse_sink_event));
123   gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
124
125   parse->srcpad =
126       gst_pad_new_from_static_template (&vorbis_parse_src_factory, "src");
127   gst_pad_set_query_function (parse->srcpad,
128       GST_DEBUG_FUNCPTR (vorbis_parse_src_query));
129   gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
130 }
131
132 static void
133 vorbis_parse_set_header_on_caps (GstVorbisParse * parse, GstCaps * caps)
134 {
135   GstBuffer *buf1, *buf2, *buf3;
136   GstStructure *structure;
137   GValue array = { 0 };
138   GValue value = { 0 };
139
140   g_assert (parse);
141   g_assert (parse->streamheader);
142   g_assert (parse->streamheader->next);
143   g_assert (parse->streamheader->next->next);
144   buf1 = parse->streamheader->data;
145   g_assert (buf1);
146   buf2 = parse->streamheader->next->data;
147   g_assert (buf2);
148   buf3 = parse->streamheader->next->next->data;
149   g_assert (buf3);
150
151   structure = gst_caps_get_structure (caps, 0);
152
153   /* mark buffers */
154   GST_BUFFER_FLAG_SET (buf1, GST_BUFFER_FLAG_IN_CAPS);
155   GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_IN_CAPS);
156   GST_BUFFER_FLAG_SET (buf3, GST_BUFFER_FLAG_IN_CAPS);
157
158   /* put buffers in a fixed list */
159   g_value_init (&array, GST_TYPE_ARRAY);
160   g_value_init (&value, GST_TYPE_BUFFER);
161   gst_value_set_buffer (&value, buf1);
162   gst_value_array_append_value (&array, &value);
163   g_value_unset (&value);
164   g_value_init (&value, GST_TYPE_BUFFER);
165   gst_value_set_buffer (&value, buf2);
166   gst_value_array_append_value (&array, &value);
167   g_value_unset (&value);
168   g_value_init (&value, GST_TYPE_BUFFER);
169   gst_value_set_buffer (&value, buf3);
170   gst_value_array_append_value (&array, &value);
171   gst_structure_set_value (structure, "streamheader", &array);
172   g_value_unset (&value);
173   g_value_unset (&array);
174 }
175
176 static void
177 vorbis_parse_drain_event_queue (GstVorbisParse * parse)
178 {
179   while (parse->event_queue->length) {
180     GstEvent *event;
181
182     event = GST_EVENT_CAST (g_queue_pop_head (parse->event_queue));
183     gst_pad_event_default (parse->sinkpad, event);
184   }
185 }
186
187 static void
188 vorbis_parse_push_headers (GstVorbisParse * parse)
189 {
190   /* mark and put on caps */
191   GstCaps *caps;
192   GstBuffer *outbuf, *outbuf1, *outbuf2, *outbuf3;
193   ogg_packet packet;
194
195   /* get the headers into the caps, passing them to vorbis as we go */
196   caps = gst_caps_make_writable (gst_pad_get_caps (parse->srcpad));
197   vorbis_parse_set_header_on_caps (parse, caps);
198   GST_DEBUG_OBJECT (parse, "here are the caps: %" GST_PTR_FORMAT, caps);
199   gst_pad_set_caps (parse->srcpad, caps);
200   gst_caps_unref (caps);
201
202   outbuf = GST_BUFFER_CAST (parse->streamheader->data);
203   packet.packet = GST_BUFFER_DATA (outbuf);
204   packet.bytes = GST_BUFFER_SIZE (outbuf);
205   packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
206   packet.packetno = 1;
207   packet.e_o_s = 0;
208   packet.b_o_s = 1;
209   vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
210   parse->sample_rate = parse->vi.rate;
211   outbuf1 = outbuf;
212
213   outbuf = GST_BUFFER_CAST (parse->streamheader->next->data);
214   packet.packet = GST_BUFFER_DATA (outbuf);
215   packet.bytes = GST_BUFFER_SIZE (outbuf);
216   packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
217   packet.packetno = 2;
218   packet.e_o_s = 0;
219   packet.b_o_s = 0;
220   vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
221   outbuf2 = outbuf;
222
223   outbuf = GST_BUFFER_CAST (parse->streamheader->next->next->data);
224   packet.packet = GST_BUFFER_DATA (outbuf);
225   packet.bytes = GST_BUFFER_SIZE (outbuf);
226   packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
227   packet.packetno = 3;
228   packet.e_o_s = 0;
229   packet.b_o_s = 0;
230   vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
231   outbuf3 = outbuf;
232
233   /* first process queued events */
234   vorbis_parse_drain_event_queue (parse);
235
236   /* push out buffers, ignoring return value... */
237   outbuf1 = gst_buffer_make_metadata_writable (outbuf1);
238   gst_buffer_set_caps (outbuf1, GST_PAD_CAPS (parse->srcpad));
239   gst_pad_push (parse->srcpad, outbuf1);
240   outbuf2 = gst_buffer_make_metadata_writable (outbuf2);
241   gst_buffer_set_caps (outbuf2, GST_PAD_CAPS (parse->srcpad));
242   gst_pad_push (parse->srcpad, outbuf2);
243   outbuf3 = gst_buffer_make_metadata_writable (outbuf3);
244   gst_buffer_set_caps (outbuf3, GST_PAD_CAPS (parse->srcpad));
245   gst_pad_push (parse->srcpad, outbuf3);
246
247   g_list_free (parse->streamheader);
248   parse->streamheader = NULL;
249 }
250
251 static void
252 vorbis_parse_clear_queue (GstVorbisParse * parse)
253 {
254   while (parse->buffer_queue->length) {
255     GstBuffer *buf;
256
257     buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
258     gst_buffer_unref (buf);
259   }
260   while (parse->event_queue->length) {
261     GstEvent *event;
262
263     event = GST_EVENT_CAST (g_queue_pop_head (parse->event_queue));
264     gst_event_unref (event);
265   }
266 }
267
268 static GstFlowReturn
269 vorbis_parse_push_buffer (GstVorbisParse * parse, GstBuffer * buf,
270     gint64 granulepos)
271 {
272   guint64 samples;
273
274   /* our hack as noted below */
275   samples = GST_BUFFER_OFFSET (buf);
276
277   GST_BUFFER_OFFSET_END (buf) = granulepos;
278   GST_BUFFER_DURATION (buf) = samples * GST_SECOND / parse->sample_rate;
279   GST_BUFFER_OFFSET (buf) = granulepos * GST_SECOND / parse->sample_rate;
280   GST_BUFFER_TIMESTAMP (buf) =
281       GST_BUFFER_OFFSET (buf) - GST_BUFFER_DURATION (buf);
282
283   gst_buffer_set_caps (buf, GST_PAD_CAPS (parse->srcpad));
284
285   return gst_pad_push (parse->srcpad, buf);
286 }
287
288 static GstFlowReturn
289 vorbis_parse_drain_queue_prematurely (GstVorbisParse * parse)
290 {
291   GstFlowReturn ret = GST_FLOW_OK;
292   gint64 granulepos = MAX (parse->prev_granulepos, 0);
293
294   /* got an EOS event, make sure to push out any buffers that were in the queue
295    * -- won't normally be the case, but this catches the
296    * didn't-get-a-granulepos-on-the-last-packet case. Assuming a continuous
297    * stream. */
298
299   /* if we got EOS before any buffers came, go ahead and push the other events
300    * first */
301   vorbis_parse_drain_event_queue (parse);
302
303   while (!g_queue_is_empty (parse->buffer_queue)) {
304     GstBuffer *buf;
305
306     buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
307
308     granulepos += GST_BUFFER_OFFSET (buf);
309     ret = vorbis_parse_push_buffer (parse, buf, granulepos);
310
311     if (ret != GST_FLOW_OK)
312       goto done;
313   }
314
315   parse->prev_granulepos = granulepos;
316
317 done:
318   return ret;
319 }
320
321 static GstFlowReturn
322 vorbis_parse_drain_queue (GstVorbisParse * parse, gint64 granulepos)
323 {
324   GstFlowReturn ret = GST_FLOW_OK;
325   GList *walk;
326   gint64 cur = granulepos;
327   gint64 gp;
328
329   for (walk = parse->buffer_queue->head; walk; walk = walk->next)
330     cur -= GST_BUFFER_OFFSET (walk->data);
331
332   if (parse->prev_granulepos != -1)
333     cur = MAX (cur, parse->prev_granulepos);
334
335   while (!g_queue_is_empty (parse->buffer_queue)) {
336     GstBuffer *buf;
337
338     buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
339
340     cur += GST_BUFFER_OFFSET (buf);
341     gp = CLAMP (cur, 0, granulepos);
342
343     ret = vorbis_parse_push_buffer (parse, buf, gp);
344
345     if (ret != GST_FLOW_OK)
346       goto done;
347   }
348
349   parse->prev_granulepos = granulepos;
350
351 done:
352   return ret;
353 }
354
355 static GstFlowReturn
356 vorbis_parse_queue_buffer (GstVorbisParse * parse, GstBuffer * buf)
357 {
358   GstFlowReturn ret = GST_FLOW_OK;
359   long blocksize;
360   ogg_packet packet;
361
362   buf = gst_buffer_make_metadata_writable (buf);
363
364   packet.packet = GST_BUFFER_DATA (buf);
365   packet.bytes = GST_BUFFER_SIZE (buf);
366   packet.granulepos = GST_BUFFER_OFFSET_END (buf);
367   packet.packetno = parse->packetno + parse->buffer_queue->length;
368   packet.e_o_s = 0;
369
370   blocksize = vorbis_packet_blocksize (&parse->vi, &packet);
371
372   /* temporarily store the sample count in OFFSET -- we overwrite this later */
373
374   if (parse->prev_blocksize < 0)
375     GST_BUFFER_OFFSET (buf) = 0;
376   else
377     GST_BUFFER_OFFSET (buf) = (blocksize + parse->prev_blocksize) / 4;
378
379   parse->prev_blocksize = blocksize;
380
381   g_queue_push_tail (parse->buffer_queue, buf);
382
383   if (GST_BUFFER_OFFSET_END_IS_VALID (buf))
384     ret = vorbis_parse_drain_queue (parse, GST_BUFFER_OFFSET_END (buf));
385
386   return ret;
387 }
388
389 static GstFlowReturn
390 vorbis_parse_parse_packet (GstVorbisParse * parse, GstBuffer * buf)
391 {
392   GstFlowReturn ret;
393   guint8 *data;
394   guint size;
395   gboolean have_header;
396
397   data = GST_BUFFER_DATA (buf);
398   size = GST_BUFFER_SIZE (buf);
399
400   parse->packetno++;
401
402   have_header = FALSE;
403   if (size >= 1) {
404     if (data[0] >= 0x01 && data[0] <= 0x05)
405       have_header = TRUE;
406   }
407
408   if (have_header) {
409     if (!parse->streamheader_sent) {
410       /* we need to collect the headers still */
411       /* so put it on the streamheader list and return */
412       parse->streamheader = g_list_append (parse->streamheader, buf);
413     }
414     ret = GST_FLOW_OK;
415   } else {
416     /* data packet, push the headers we collected before */
417     if (!parse->streamheader_sent) {
418       vorbis_parse_push_headers (parse);
419       parse->streamheader_sent = TRUE;
420     }
421     ret = vorbis_parse_queue_buffer (parse, buf);
422   }
423
424   return ret;
425 }
426
427 static GstFlowReturn
428 vorbis_parse_chain (GstPad * pad, GstBuffer * buffer)
429 {
430   GstVorbisParseClass *klass;
431   GstVorbisParse *parse;
432
433   parse = GST_VORBIS_PARSE (GST_PAD_PARENT (pad));
434   klass = GST_VORBIS_PARSE_CLASS (G_OBJECT_GET_CLASS (parse));
435
436   g_assert (klass->parse_packet != NULL);
437
438   return klass->parse_packet (parse, buffer);
439 }
440
441 static gboolean
442 vorbis_parse_queue_event (GstVorbisParse * parse, GstEvent * event)
443 {
444   GstFlowReturn ret = TRUE;
445
446   g_queue_push_tail (parse->event_queue, event);
447
448   return ret;
449 }
450
451 static gboolean
452 vorbis_parse_sink_event (GstPad * pad, GstEvent * event)
453 {
454   gboolean ret;
455   GstVorbisParse *parse;
456
457   parse = GST_VORBIS_PARSE (gst_pad_get_parent (pad));
458
459   switch (GST_EVENT_TYPE (event)) {
460     case GST_EVENT_FLUSH_START:
461       vorbis_parse_clear_queue (parse);
462       parse->prev_granulepos = -1;
463       parse->prev_blocksize = -1;
464       ret = gst_pad_event_default (pad, event);
465       break;
466     case GST_EVENT_EOS:
467       vorbis_parse_drain_queue_prematurely (parse);
468       ret = gst_pad_event_default (pad, event);
469       break;
470     default:
471       if (!parse->streamheader_sent && GST_EVENT_IS_SERIALIZED (event))
472         ret = vorbis_parse_queue_event (parse, event);
473       else
474         ret = gst_pad_event_default (pad, event);
475       break;
476   }
477
478   gst_object_unref (parse);
479
480   return ret;
481 }
482
483 static gboolean
484 vorbis_parse_convert (GstPad * pad,
485     GstFormat src_format, gint64 src_value,
486     GstFormat * dest_format, gint64 * dest_value)
487 {
488   gboolean res = TRUE;
489   GstVorbisParse *parse;
490   guint64 scale = 1;
491
492   parse = GST_VORBIS_PARSE (GST_PAD_PARENT (pad));
493
494   /* fixme: assumes atomic access to lots of instance variables modified from
495    * the streaming thread, including 64-bit variables */
496
497   if (parse->packetno < 4)
498     return FALSE;
499
500   if (src_format == *dest_format) {
501     *dest_value = src_value;
502     return TRUE;
503   }
504
505   if (parse->sinkpad == pad &&
506       (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
507     return FALSE;
508
509   switch (src_format) {
510     case GST_FORMAT_TIME:
511       switch (*dest_format) {
512         case GST_FORMAT_BYTES:
513           scale = sizeof (float) * parse->vi.channels;
514         case GST_FORMAT_DEFAULT:
515           *dest_value =
516               scale * gst_util_uint64_scale_int (src_value, parse->vi.rate,
517               GST_SECOND);
518           break;
519         default:
520           res = FALSE;
521       }
522       break;
523     case GST_FORMAT_DEFAULT:
524       switch (*dest_format) {
525         case GST_FORMAT_BYTES:
526           *dest_value = src_value * sizeof (float) * parse->vi.channels;
527           break;
528         case GST_FORMAT_TIME:
529           *dest_value =
530               gst_util_uint64_scale_int (src_value, GST_SECOND, parse->vi.rate);
531           break;
532         default:
533           res = FALSE;
534       }
535       break;
536     case GST_FORMAT_BYTES:
537       switch (*dest_format) {
538         case GST_FORMAT_DEFAULT:
539           *dest_value = src_value / (sizeof (float) * parse->vi.channels);
540           break;
541         case GST_FORMAT_TIME:
542           *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
543               parse->vi.rate * sizeof (float) * parse->vi.channels);
544           break;
545         default:
546           res = FALSE;
547       }
548       break;
549     default:
550       res = FALSE;
551   }
552
553   return res;
554 }
555
556 static gboolean
557 vorbis_parse_src_query (GstPad * pad, GstQuery * query)
558 {
559   gint64 granulepos;
560   GstVorbisParse *parse;
561   gboolean res = FALSE;
562
563   parse = GST_VORBIS_PARSE (GST_PAD_PARENT (pad));
564
565   switch (GST_QUERY_TYPE (query)) {
566     case GST_QUERY_POSITION:
567     {
568       GstFormat format;
569       gint64 value;
570
571       granulepos = parse->prev_granulepos;
572
573       gst_query_parse_position (query, &format, NULL);
574
575       /* and convert to the final format */
576       if (!(res =
577               vorbis_parse_convert (pad, GST_FORMAT_DEFAULT, granulepos,
578                   &format, &value)))
579         goto error;
580
581       /* fixme: support segments
582          value = (value - parse->segment_start) + parse->segment_time;
583        */
584
585       gst_query_set_position (query, format, value);
586
587       GST_LOG_OBJECT (parse, "query %p: peer returned granulepos: %"
588           G_GUINT64_FORMAT " - we return %" G_GUINT64_FORMAT " (format %u)",
589           query, granulepos, value, format);
590
591       break;
592     }
593     case GST_QUERY_DURATION:
594     {
595       /* fixme: not threadsafe */
596       /* query peer for total length */
597       if (!gst_pad_is_linked (parse->sinkpad)) {
598         GST_WARNING_OBJECT (parse, "sink pad %" GST_PTR_FORMAT " is not linked",
599             parse->sinkpad);
600         goto error;
601       }
602       if (!(res = gst_pad_query (GST_PAD_PEER (parse->sinkpad), query)))
603         goto error;
604       break;
605     }
606     case GST_QUERY_CONVERT:
607     {
608       GstFormat src_fmt, dest_fmt;
609       gint64 src_val, dest_val;
610
611       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
612       if (!(res =
613               vorbis_parse_convert (pad, src_fmt, src_val, &dest_fmt,
614                   &dest_val)))
615         goto error;
616       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
617       break;
618     }
619     default:
620       res = gst_pad_query_default (pad, query);
621       break;
622   }
623   return res;
624
625 error:
626   {
627     GST_WARNING_OBJECT (parse, "error handling query");
628     return res;
629   }
630 }
631
632 static GstStateChangeReturn
633 vorbis_parse_change_state (GstElement * element, GstStateChange transition)
634 {
635   GstVorbisParse *parse = GST_VORBIS_PARSE (element);
636   GstStateChangeReturn ret;
637
638   switch (transition) {
639     case GST_STATE_CHANGE_READY_TO_PAUSED:
640       vorbis_info_init (&parse->vi);
641       vorbis_comment_init (&parse->vc);
642       parse->prev_granulepos = -1;
643       parse->prev_blocksize = -1;
644       parse->packetno = 0;
645       parse->streamheader_sent = FALSE;
646       parse->buffer_queue = g_queue_new ();
647       parse->event_queue = g_queue_new ();
648       break;
649     default:
650       break;
651   }
652
653   ret = parent_class->change_state (element, transition);
654
655   switch (transition) {
656     case GST_STATE_CHANGE_PAUSED_TO_READY:
657       vorbis_info_clear (&parse->vi);
658       vorbis_comment_clear (&parse->vc);
659       vorbis_parse_clear_queue (parse);
660       g_queue_free (parse->buffer_queue);
661       parse->buffer_queue = NULL;
662       g_queue_free (parse->event_queue);
663       parse->event_queue = NULL;
664       break;
665     default:
666       break;
667   }
668
669   return ret;
670 }