2 * Copyright (C) 2004 Benjamin Otte <in7y118@public.uni-hamburg.de>
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.
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.
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.
24 #include "vorbisdec.h"
26 #include <gst/audio/audio.h>
27 #include <gst/tag/tag.h>
28 #include <gst/audio/multichannel.h>
30 GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug);
31 #define GST_CAT_DEFAULT vorbisdec_debug
33 static GstElementDetails vorbis_dec_details = {
35 "Codec/Decoder/Audio",
36 "decode raw vorbis streams to float audio",
37 "Benjamin Otte <in7y118@public.uni-hamburg.de>",
40 /* Filter signals and args */
52 static GstStaticPadTemplate vorbis_dec_src_factory =
53 GST_STATIC_PAD_TEMPLATE ("src",
56 GST_STATIC_CAPS ("audio/x-raw-float, "
57 "rate = (int) [ 8000, 50000 ], "
58 "channels = (int) [ 1, 6 ], " "endianness = (int) BYTE_ORDER, "
59 /* no ifdef in macros, please
60 #ifdef GST_VORBIS_DEC_SEQUENTIAL
61 "layout = \"sequential\", "
67 static GstStaticPadTemplate vorbis_dec_sink_factory =
68 GST_STATIC_PAD_TEMPLATE ("sink",
71 GST_STATIC_CAPS ("audio/x-vorbis")
74 GST_BOILERPLATE (GstVorbisDec, gst_vorbis_dec, GstElement, GST_TYPE_ELEMENT);
76 static void vorbisdec_finalize (GObject * object);
77 static gboolean vorbis_dec_sink_event (GstPad * pad, GstEvent * event);
78 static GstFlowReturn vorbis_dec_chain (GstPad * pad, GstBuffer * buffer);
79 static GstStateChangeReturn vorbis_dec_change_state (GstElement * element,
80 GstStateChange transition);
83 static const GstFormat *vorbis_dec_get_formats (GstPad * pad);
86 static gboolean vorbis_dec_src_event (GstPad * pad, GstEvent * event);
87 static gboolean vorbis_dec_src_query (GstPad * pad, GstQuery * query);
88 static gboolean vorbis_dec_convert (GstPad * pad,
89 GstFormat src_format, gint64 src_value,
90 GstFormat * dest_format, gint64 * dest_value);
92 static gboolean vorbis_dec_sink_query (GstPad * pad, GstQuery * query);
95 gst_vorbis_dec_base_init (gpointer g_class)
97 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
98 GstPadTemplate *src_template, *sink_template;
100 src_template = gst_static_pad_template_get (&vorbis_dec_src_factory);
101 gst_element_class_add_pad_template (element_class, src_template);
103 sink_template = gst_static_pad_template_get (&vorbis_dec_sink_factory);
104 gst_element_class_add_pad_template (element_class, sink_template);
106 gst_element_class_set_details (element_class, &vorbis_dec_details);
110 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
115 gobject_class->finalize = vorbisdec_finalize;
117 gstelement_class->change_state = vorbis_dec_change_state;
121 static const GstFormat *
122 vorbis_dec_get_formats (GstPad * pad)
124 static GstFormat src_formats[] = {
126 GST_FORMAT_DEFAULT, /* samples in the audio case */
130 static GstFormat sink_formats[] = {
131 /*GST_FORMAT_BYTES, */
133 GST_FORMAT_DEFAULT, /* granulepos or samples */
137 return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
142 static const GstEventMask *
143 vorbis_get_event_masks (GstPad * pad)
145 static const GstEventMask vorbis_dec_src_event_masks[] = {
146 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
150 return vorbis_dec_src_event_masks;
154 static const GstQueryType *
155 vorbis_get_query_types (GstPad * pad)
157 static const GstQueryType vorbis_dec_src_query_types[] = {
162 return vorbis_dec_src_query_types;
166 gst_vorbis_dec_init (GstVorbisDec * dec, GstVorbisDecClass * g_class)
168 dec->sinkpad = gst_pad_new_from_static_template (&vorbis_dec_sink_factory,
171 gst_pad_set_event_function (dec->sinkpad, vorbis_dec_sink_event);
172 gst_pad_set_chain_function (dec->sinkpad, vorbis_dec_chain);
173 gst_pad_set_query_function (dec->sinkpad, vorbis_dec_sink_query);
174 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
176 dec->srcpad = gst_pad_new_from_static_template (&vorbis_dec_src_factory,
179 gst_pad_set_event_function (dec->srcpad, vorbis_dec_src_event);
180 gst_pad_set_query_type_function (dec->srcpad, vorbis_get_query_types);
181 gst_pad_set_query_function (dec->srcpad, vorbis_dec_src_query);
182 gst_pad_use_fixed_caps (dec->srcpad);
183 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
189 vorbisdec_finalize (GObject * object)
191 /* Release any possibly allocated libvorbis data.
192 * _clear functions can safely be called multiple times
194 GstVorbisDec *vd = GST_VORBIS_DEC (object);
196 vorbis_block_clear (&vd->vb);
197 vorbis_dsp_clear (&vd->vd);
198 vorbis_comment_clear (&vd->vc);
199 vorbis_info_clear (&vd->vi);
201 G_OBJECT_CLASS (parent_class)->finalize (object);
205 vorbis_dec_convert (GstPad * pad,
206 GstFormat src_format, gint64 src_value,
207 GstFormat * dest_format, gint64 * dest_value)
213 dec = GST_VORBIS_DEC (GST_PAD_PARENT (pad));
215 if (dec->packetno < 1)
218 if (src_format == *dest_format) {
219 *dest_value = src_value;
223 if (dec->sinkpad == pad &&
224 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
227 switch (src_format) {
228 case GST_FORMAT_TIME:
229 switch (*dest_format) {
230 case GST_FORMAT_BYTES:
231 scale = sizeof (float) * dec->vi.channels;
232 case GST_FORMAT_DEFAULT:
233 *dest_value = scale * (src_value * dec->vi.rate / GST_SECOND);
239 case GST_FORMAT_DEFAULT:
240 switch (*dest_format) {
241 case GST_FORMAT_BYTES:
242 *dest_value = src_value * sizeof (float) * dec->vi.channels;
244 case GST_FORMAT_TIME:
246 gst_util_uint64_scale (src_value, GST_SECOND, dec->vi.rate);
252 case GST_FORMAT_BYTES:
253 switch (*dest_format) {
254 case GST_FORMAT_DEFAULT:
255 *dest_value = src_value / (sizeof (float) * dec->vi.channels);
257 case GST_FORMAT_TIME:
258 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
259 dec->vi.rate * sizeof (float) * dec->vi.channels);
273 vorbis_dec_src_query (GstPad * pad, GstQuery * query)
279 dec = GST_VORBIS_DEC (GST_PAD_PARENT (pad));
281 switch (GST_QUERY_TYPE (query)) {
282 case GST_QUERY_POSITION:
287 granulepos = dec->granulepos;
289 gst_query_parse_position (query, &format, NULL);
291 /* and convert to the final format */
293 vorbis_dec_convert (pad, GST_FORMAT_DEFAULT, granulepos, &format,
297 value = (value - dec->segment_start) + dec->segment_time;
299 gst_query_set_position (query, format, value);
302 "query %u: peer returned granulepos: %llu - we return %llu (format %u)",
303 query, granulepos, value, format);
307 case GST_QUERY_DURATION:
309 /* query peer for total length */
310 if (!(res = gst_pad_query (GST_PAD_PEER (dec->sinkpad), query)))
314 case GST_QUERY_CONVERT:
316 GstFormat src_fmt, dest_fmt;
317 gint64 src_val, dest_val;
319 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
321 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
323 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
334 GST_WARNING_OBJECT (dec, "error handling query");
340 vorbis_dec_sink_query (GstPad * pad, GstQuery * query)
345 dec = GST_VORBIS_DEC (GST_PAD_PARENT (pad));
347 switch (GST_QUERY_TYPE (query)) {
348 case GST_QUERY_CONVERT:
350 GstFormat src_fmt, dest_fmt;
351 gint64 src_val, dest_val;
353 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
355 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
357 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
369 vorbis_dec_src_event (GstPad * pad, GstEvent * event)
372 GstVorbisDec *dec = GST_VORBIS_DEC (GST_PAD_PARENT (pad));
374 switch (GST_EVENT_TYPE (event)) {
375 case GST_EVENT_SEEK:{
376 GstFormat format, tformat;
380 GstSeekType cur_type, stop_type;
384 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
387 /* we have to ask our peer to seek to time here as we know
388 * nothing about how to generate a granulepos from the src
389 * formats or anything.
391 * First bring the requested format to time
393 tformat = GST_FORMAT_TIME;
394 if (!(res = vorbis_dec_convert (pad, format, cur, &tformat, &tcur)))
396 if (!(res = vorbis_dec_convert (pad, format, stop, &tformat, &tstop)))
399 /* then seek with time on the peer */
400 real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
401 flags, cur_type, tcur, stop_type, tstop);
403 res = gst_pad_push_event (dec->sinkpad, real_seek);
405 gst_event_unref (event);
409 res = gst_pad_event_default (pad, event);
416 gst_event_unref (event);
421 vorbis_dec_sink_event (GstPad * pad, GstEvent * event)
423 gboolean ret = FALSE;
426 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
428 GST_LOG_OBJECT (dec, "handling event");
429 switch (GST_EVENT_TYPE (event)) {
431 ret = gst_pad_push_event (dec->srcpad, event);
433 case GST_EVENT_NEWSEGMENT:
437 gint64 start, stop, time;
440 gst_event_parse_new_segment (event, &update, &rate, &format, &start,
443 if (format != GST_FORMAT_TIME)
444 goto newseg_wrong_format;
447 goto newseg_wrong_rate;
449 /* now copy over the values */
450 dec->segment_rate = rate;
451 dec->segment_start = start;
452 dec->segment_stop = stop;
453 dec->segment_time = time;
455 dec->granulepos = -1;
456 dec->cur_timestamp = GST_CLOCK_TIME_NONE;
457 dec->prev_timestamp = GST_CLOCK_TIME_NONE;
459 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
460 vorbis_synthesis_restart (&dec->vd);
462 ret = gst_pad_push_event (dec->srcpad, event);
466 ret = gst_pad_push_event (dec->srcpad, event);
470 gst_object_unref (dec);
476 GST_DEBUG ("received non TIME newsegment");
481 GST_DEBUG ("negative rates not supported yet");
488 vorbis_handle_identification_packet (GstVorbisDec * vd)
491 const GstAudioChannelPosition *pos = NULL;
493 caps = gst_caps_new_simple ("audio/x-raw-float",
494 "rate", G_TYPE_INT, vd->vi.rate,
495 "channels", G_TYPE_INT, vd->vi.channels,
496 "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
498 switch (vd->vi.channels) {
504 static GstAudioChannelPosition pos3[] = {
505 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
506 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
507 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
513 static GstAudioChannelPosition pos4[] = {
514 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
515 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
516 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
517 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
523 static GstAudioChannelPosition pos5[] = {
524 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
525 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
526 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
527 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
528 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
534 static GstAudioChannelPosition pos6[] = {
535 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
536 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
537 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
538 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
539 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
540 GST_AUDIO_CHANNEL_POSITION_LFE
546 goto channel_count_error;
549 gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
551 gst_pad_set_caps (vd->srcpad, caps);
552 gst_caps_unref (caps);
559 gst_caps_unref (caps);
560 GST_ELEMENT_ERROR (vd, STREAM, NOT_IMPLEMENTED, (NULL),
561 ("Unsupported channel count %d", vd->vi.channels));
562 return GST_FLOW_ERROR;
567 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
570 gchar *encoder = NULL;
575 GST_DEBUG_OBJECT (vd, "parsing comment packet");
577 buf = gst_buffer_new_and_alloc (packet->bytes);
578 GST_BUFFER_DATA (buf) = packet->packet;
581 gst_tag_list_from_vorbiscomment_buffer (buf, (guint8 *) "\003vorbis", 7,
584 gst_buffer_unref (buf);
587 GST_ERROR_OBJECT (vd, "couldn't decode comments");
588 list = gst_tag_list_new ();
591 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
592 GST_TAG_ENCODER, encoder, NULL);
595 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
596 GST_TAG_ENCODER_VERSION, vd->vi.version,
597 GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
598 if (vd->vi.bitrate_nominal > 0) {
599 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
600 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
601 bitrate = vd->vi.bitrate_nominal;
603 if (vd->vi.bitrate_upper > 0) {
604 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
605 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
607 bitrate = vd->vi.bitrate_upper;
609 if (vd->vi.bitrate_lower > 0) {
610 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
611 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
613 bitrate = vd->vi.bitrate_lower;
616 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
617 GST_TAG_BITRATE, (guint) bitrate, NULL);
620 message = gst_message_new_tag ((GstObject *) vd, list);
621 gst_element_post_message (GST_ELEMENT (vd), message);
627 vorbis_handle_type_packet (GstVorbisDec * vd)
629 g_assert (vd->initialized == FALSE);
631 vorbis_synthesis_init (&vd->vd, &vd->vi);
632 vorbis_block_init (&vd->vd, &vd->vb);
633 vd->initialized = TRUE;
639 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
643 GST_DEBUG_OBJECT (vd, "parsing header packet");
645 /* Packetno = 0 if the first byte is exactly 0x01 */
646 packet->b_o_s = (packet->packet[0] == 0x1) ? 1 : 0;
648 if (vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet))
649 goto header_read_error;
651 /* FIXME: we should probably double-check if packet[0] is 1/3/5 for each
653 switch (packet->packetno) {
655 res = vorbis_handle_identification_packet (vd);
658 res = vorbis_handle_comment_packet (vd, packet);
661 res = vorbis_handle_type_packet (vd);
673 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
674 (NULL), ("couldn't read header packet"));
675 return GST_FLOW_ERROR;
680 copy_samples (float *out, float **in, guint samples, gint channels)
684 #ifdef GST_VORBIS_DEC_SEQUENTIAL
685 for (i = 0; i < channels; i++) {
686 memcpy (out, in[i], samples * sizeof (float));
690 for (j = 0; j < samples; j++) {
691 for (i = 0; i < channels; i++) {
699 vorbis_dec_push (GstVorbisDec * dec, GstBuffer * buf)
701 GstFlowReturn result;
702 gint64 outoffset = GST_BUFFER_OFFSET (buf);
704 if (outoffset == -1) {
705 dec->queued = g_list_append (dec->queued, buf);
706 GST_DEBUG_OBJECT (dec, "queued buffer");
707 result = GST_FLOW_OK;
713 GST_DEBUG_OBJECT (dec, "first buffer with offset %lld", outoffset);
715 size = g_list_length (dec->queued);
716 for (walk = g_list_last (dec->queued); walk;
717 walk = g_list_previous (walk)) {
718 GstBuffer *buffer = GST_BUFFER (walk->data);
721 GST_BUFFER_SIZE (buffer) / (sizeof (float) * dec->vi.channels);
723 GST_BUFFER_OFFSET (buffer) = outoffset;
724 GST_BUFFER_TIMESTAMP (buffer) =
725 gst_util_uint64_scale (outoffset, GST_SECOND, dec->vi.rate);
726 GST_DEBUG_OBJECT (dec, "patch buffer %" G_GUINT64_FORMAT
727 " offset %" G_GUINT64_FORMAT, size, outoffset);
730 for (walk = dec->queued; walk; walk = g_list_next (walk)) {
731 GstBuffer *buffer = GST_BUFFER (walk->data);
733 /* ignore the result */
734 gst_pad_push (dec->srcpad, buffer);
736 g_list_free (dec->queued);
739 result = gst_pad_push (dec->srcpad, buf);
746 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet)
751 GstFlowReturn result;
754 if (!vd->initialized)
755 goto not_initialized;
757 /* normal data packet */
758 if (vorbis_synthesis (&vd->vb, packet))
761 if (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0)
764 /* assume all goes well here */
765 result = GST_FLOW_OK;
767 /* count samples ready for reading */
768 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
771 size = sample_count * vd->vi.channels * sizeof (float);
773 /* alloc buffer for it */
775 gst_pad_alloc_buffer_and_set_caps (vd->srcpad, GST_BUFFER_OFFSET_NONE,
776 size, GST_PAD_CAPS (vd->srcpad), &out);
777 if (result != GST_FLOW_OK)
780 /* get samples ready for reading now, should be sample_count */
781 if ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count)
784 /* copy samples in buffer */
785 copy_samples ((float *) GST_BUFFER_DATA (out), pcm, sample_count,
788 GST_BUFFER_SIZE (out) = size;
789 GST_BUFFER_OFFSET (out) = vd->granulepos;
790 if (vd->granulepos != -1) {
791 GST_BUFFER_OFFSET_END (out) = vd->granulepos + sample_count;
792 GST_BUFFER_TIMESTAMP (out) =
793 gst_util_uint64_scale (vd->granulepos, GST_SECOND, vd->vi.rate);
795 GST_BUFFER_TIMESTAMP (out) = -1;
797 GST_BUFFER_DURATION (out) = sample_count * GST_SECOND / vd->vi.rate;
799 if (vd->cur_timestamp != GST_CLOCK_TIME_NONE) {
800 GST_BUFFER_TIMESTAMP (out) = vd->cur_timestamp;
801 GST_DEBUG ("cur_timestamp: %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT " = % "
802 GST_TIME_FORMAT, GST_TIME_ARGS (vd->cur_timestamp),
803 GST_TIME_ARGS (GST_BUFFER_DURATION (out)),
804 GST_TIME_ARGS (vd->cur_timestamp + GST_BUFFER_DURATION (out)));
805 vd->cur_timestamp += GST_BUFFER_DURATION (out);
806 GST_BUFFER_OFFSET (out) = GST_CLOCK_TIME_TO_FRAMES (vd->cur_timestamp,
808 GST_BUFFER_OFFSET_END (out) = GST_BUFFER_OFFSET (out) + sample_count;
811 if (vd->granulepos != -1)
812 vd->granulepos += sample_count;
814 result = vorbis_dec_push (vd, out);
817 vorbis_synthesis_read (&vd->vd, sample_count);
819 /* granulepos is the last sample in the packet */
820 if (packet->granulepos != -1)
821 vd->granulepos = packet->granulepos;
828 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
829 (NULL), ("no header sent yet (packet no is %d)", packet->packetno));
830 return GST_FLOW_ERROR;
834 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
835 (NULL), ("couldn't read data packet"));
836 return GST_FLOW_ERROR;
840 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
841 (NULL), ("vorbis decoder did not accept data packet"));
842 return GST_FLOW_ERROR;
846 gst_buffer_unref (out);
847 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
848 (NULL), ("vorbis decoder reported wrong number of samples"));
849 return GST_FLOW_ERROR;
854 vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
858 GstFlowReturn result = GST_FLOW_OK;
860 vd = GST_VORBIS_DEC (GST_PAD_PARENT (pad));
862 if (GST_BUFFER_SIZE (buffer) == 0) {
863 gst_buffer_unref (buffer);
864 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty buffer received"));
865 return GST_FLOW_ERROR;
868 /* only ogg has granulepos, demuxers of other container formats
869 * might provide us with timestamps instead (e.g. matroskademux) */
870 if (GST_BUFFER_OFFSET_END (buffer) == GST_BUFFER_OFFSET_NONE &&
871 GST_BUFFER_TIMESTAMP (buffer) != GST_CLOCK_TIME_NONE) {
872 /* we might get multiple consecutive buffers with the same timestamp */
873 if (GST_BUFFER_TIMESTAMP (buffer) != vd->prev_timestamp) {
874 vd->cur_timestamp = GST_BUFFER_TIMESTAMP (buffer);
875 vd->prev_timestamp = GST_BUFFER_TIMESTAMP (buffer);
878 vd->cur_timestamp = GST_CLOCK_TIME_NONE;
879 vd->prev_timestamp = GST_CLOCK_TIME_NONE;
882 /* make ogg_packet out of the buffer */
883 packet.packet = GST_BUFFER_DATA (buffer);
884 packet.bytes = GST_BUFFER_SIZE (buffer);
885 packet.granulepos = GST_BUFFER_OFFSET_END (buffer);
886 packet.packetno = vd->packetno++;
888 * FIXME. Is there anyway to know that this is the last packet and
890 * Yes there is, keep one packet at all times and only push out when
891 * you receive a new one. Implement this.
895 GST_DEBUG_OBJECT (vd, "vorbis granule: %" G_GINT64_FORMAT,
896 (gint64) packet.granulepos);
898 /* switch depending on packet type */
899 if (packet.packet[0] & 1) {
900 if (vd->initialized) {
901 GST_WARNING_OBJECT (vd, "Ignoring header");
904 result = vorbis_handle_header_packet (vd, &packet);
906 result = vorbis_handle_data_packet (vd, &packet);
909 GST_DEBUG_OBJECT (vd, "offset end: %" G_GINT64_FORMAT,
910 (gint64) GST_BUFFER_OFFSET_END (buffer));
913 gst_buffer_unref (buffer);
918 static GstStateChangeReturn
919 vorbis_dec_change_state (GstElement * element, GstStateChange transition)
921 GstVorbisDec *vd = GST_VORBIS_DEC (element);
922 GstStateChangeReturn res;
925 switch (transition) {
926 case GST_STATE_CHANGE_NULL_TO_READY:
928 case GST_STATE_CHANGE_READY_TO_PAUSED:
929 vorbis_info_init (&vd->vi);
930 vorbis_comment_init (&vd->vc);
931 vd->initialized = FALSE;
932 vd->cur_timestamp = GST_CLOCK_TIME_NONE;
933 vd->prev_timestamp = GST_CLOCK_TIME_NONE;
937 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
943 res = parent_class->change_state (element, transition);
945 switch (transition) {
946 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
948 case GST_STATE_CHANGE_PAUSED_TO_READY:
949 GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
950 vorbis_block_clear (&vd->vb);
951 vorbis_dsp_clear (&vd->vd);
952 vorbis_comment_clear (&vd->vc);
953 vorbis_info_clear (&vd->vi);
955 case GST_STATE_CHANGE_READY_TO_NULL: