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.
21 * SECTION:element-vorbisdec
22 * @see_also: vorbisenc, oggdemux
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
30 * <title>Example pipelines</title>
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.
36 * Last reviewed on 2006-03-01 (0.10.4)
43 #include "gstvorbisdec.h"
45 #include <gst/audio/audio.h>
46 #include <gst/tag/tag.h>
47 #include <gst/audio/multichannel.h>
49 #include "gstvorbiscommon.h"
51 GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug);
52 #define GST_CAT_DEFAULT vorbisdec_debug
54 static GstStaticPadTemplate vorbis_dec_src_factory =
55 GST_STATIC_PAD_TEMPLATE ("src",
58 GST_VORBIS_DEC_SRC_CAPS);
60 static GstStaticPadTemplate vorbis_dec_sink_factory =
61 GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("audio/x-vorbis")
67 GST_BOILERPLATE (GST_VORBIS_DEC_GLIB_TYPE_NAME, gst_vorbis_dec, GstElement,
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 GstStateChangeReturn vorbis_dec_change_state (GstElement * element,
76 GstStateChange transition);
78 static gboolean vorbis_dec_src_event (GstPad * pad, GstEvent * event);
79 static gboolean vorbis_dec_src_query (GstPad * pad, GstQuery * query);
80 static gboolean vorbis_dec_convert (GstPad * pad,
81 GstFormat src_format, gint64 src_value,
82 GstFormat * dest_format, gint64 * dest_value);
84 static gboolean vorbis_dec_sink_query (GstPad * pad, GstQuery * query);
87 gst_vorbis_dec_base_init (gpointer g_class)
89 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
90 GstPadTemplate *src_template, *sink_template;
92 src_template = gst_static_pad_template_get (&vorbis_dec_src_factory);
93 gst_element_class_add_pad_template (element_class, src_template);
95 sink_template = gst_static_pad_template_get (&vorbis_dec_sink_factory);
96 gst_element_class_add_pad_template (element_class, sink_template);
98 gst_element_class_set_details_simple (element_class,
99 "Vorbis audio decoder", "Codec/Decoder/Audio",
100 GST_VORBIS_DEC_DESCRIPTION,
101 "Benjamin Otte <otte@gnome.org>, Chris Lord <chris@openedhand.com>");
105 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
107 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
108 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
110 gobject_class->finalize = vorbis_dec_finalize;
112 gstelement_class->change_state = GST_DEBUG_FUNCPTR (vorbis_dec_change_state);
115 static const GstQueryType *
116 vorbis_get_query_types (GstPad * pad)
118 static const GstQueryType vorbis_dec_src_query_types[] = {
125 return vorbis_dec_src_query_types;
129 gst_vorbis_dec_init (GstVorbisDec * dec, GstVorbisDecClass * g_class)
131 dec->sinkpad = gst_pad_new_from_static_template (&vorbis_dec_sink_factory,
134 gst_pad_set_event_function (dec->sinkpad,
135 GST_DEBUG_FUNCPTR (vorbis_dec_sink_event));
136 gst_pad_set_chain_function (dec->sinkpad,
137 GST_DEBUG_FUNCPTR (vorbis_dec_chain));
138 gst_pad_set_query_function (dec->sinkpad,
139 GST_DEBUG_FUNCPTR (vorbis_dec_sink_query));
140 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
142 dec->srcpad = gst_pad_new_from_static_template (&vorbis_dec_src_factory,
145 gst_pad_set_event_function (dec->srcpad,
146 GST_DEBUG_FUNCPTR (vorbis_dec_src_event));
147 gst_pad_set_query_type_function (dec->srcpad,
148 GST_DEBUG_FUNCPTR (vorbis_get_query_types));
149 gst_pad_set_query_function (dec->srcpad,
150 GST_DEBUG_FUNCPTR (vorbis_dec_src_query));
151 gst_pad_use_fixed_caps (dec->srcpad);
152 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
155 dec->pendingevents = NULL;
160 vorbis_dec_finalize (GObject * object)
162 /* Release any possibly allocated libvorbis data.
163 * _clear functions can safely be called multiple times
165 GstVorbisDec *vd = GST_VORBIS_DEC (object);
167 vorbis_block_clear (&vd->vb);
168 vorbis_dsp_clear (&vd->vd);
169 vorbis_comment_clear (&vd->vc);
170 vorbis_info_clear (&vd->vi);
172 G_OBJECT_CLASS (parent_class)->finalize (object);
176 gst_vorbis_dec_reset (GstVorbisDec * dec)
178 dec->last_timestamp = GST_CLOCK_TIME_NONE;
180 dec->seqnum = gst_util_seqnum_next ();
181 gst_segment_init (&dec->segment, GST_FORMAT_TIME);
183 g_list_foreach (dec->queued, (GFunc) gst_mini_object_unref, NULL);
184 g_list_free (dec->queued);
186 g_list_foreach (dec->gather, (GFunc) gst_mini_object_unref, NULL);
187 g_list_free (dec->gather);
189 g_list_foreach (dec->decode, (GFunc) gst_mini_object_unref, NULL);
190 g_list_free (dec->decode);
192 g_list_foreach (dec->pendingevents, (GFunc) gst_mini_object_unref, NULL);
193 g_list_free (dec->pendingevents);
194 dec->pendingevents = NULL;
197 gst_tag_list_free (dec->taglist);
203 vorbis_dec_convert (GstPad * pad,
204 GstFormat src_format, gint64 src_value,
205 GstFormat * dest_format, gint64 * dest_value)
211 if (src_format == *dest_format) {
212 *dest_value = src_value;
216 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
218 if (!dec->initialized)
221 if (dec->sinkpad == pad &&
222 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
225 switch (src_format) {
226 case GST_FORMAT_TIME:
227 switch (*dest_format) {
228 case GST_FORMAT_BYTES:
229 scale = dec->width * dec->vi.channels;
230 case GST_FORMAT_DEFAULT:
232 scale * gst_util_uint64_scale_int (src_value, dec->vi.rate,
239 case GST_FORMAT_DEFAULT:
240 switch (*dest_format) {
241 case GST_FORMAT_BYTES:
242 *dest_value = src_value * dec->width * dec->vi.channels;
244 case GST_FORMAT_TIME:
246 gst_util_uint64_scale_int (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 / (dec->width * dec->vi.channels);
257 case GST_FORMAT_TIME:
258 *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
259 dec->vi.rate * dec->width * dec->vi.channels);
269 gst_object_unref (dec);
276 GST_DEBUG_OBJECT (dec, "no header packets received");
282 GST_DEBUG_OBJECT (dec, "formats unsupported");
289 vorbis_dec_src_query (GstPad * pad, GstQuery * query)
292 gboolean res = FALSE;
294 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
296 switch (GST_QUERY_TYPE (query)) {
297 case GST_QUERY_POSITION:
303 gst_query_parse_position (query, &format, NULL);
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);
311 "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
313 /* and convert to the final format */
315 vorbis_dec_convert (pad, GST_FORMAT_TIME, time, &format, &value)))
318 gst_query_set_position (query, format, value);
321 "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
326 case GST_QUERY_DURATION:
328 res = gst_pad_peer_query (dec->sinkpad, query);
334 case GST_QUERY_CONVERT:
336 GstFormat src_fmt, dest_fmt;
337 gint64 src_val, dest_val;
339 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
341 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
343 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
347 res = gst_pad_query_default (pad, query);
351 gst_object_unref (dec);
358 GST_WARNING_OBJECT (dec, "error handling query");
364 vorbis_dec_sink_query (GstPad * pad, GstQuery * query)
369 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
371 switch (GST_QUERY_TYPE (query)) {
372 case GST_QUERY_CONVERT:
374 GstFormat src_fmt, dest_fmt;
375 gint64 src_val, dest_val;
377 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
379 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
381 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
385 res = gst_pad_query_default (pad, query);
390 gst_object_unref (dec);
397 GST_DEBUG_OBJECT (dec, "error converting value");
403 vorbis_dec_src_event (GstPad * pad, GstEvent * event)
408 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
410 switch (GST_EVENT_TYPE (event)) {
413 GstFormat format, tformat;
417 GstSeekType cur_type, stop_type;
422 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
424 seqnum = gst_event_get_seqnum (event);
425 gst_event_unref (event);
427 /* First bring the requested format to time */
428 tformat = GST_FORMAT_TIME;
429 if (!(res = vorbis_dec_convert (pad, format, cur, &tformat, &tcur)))
431 if (!(res = vorbis_dec_convert (pad, format, stop, &tformat, &tstop)))
434 /* then seek with time on the peer */
435 real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
436 flags, cur_type, tcur, stop_type, tstop);
437 gst_event_set_seqnum (real_seek, seqnum);
439 res = gst_pad_push_event (dec->sinkpad, real_seek);
443 res = gst_pad_push_event (dec->sinkpad, event);
447 gst_object_unref (dec);
454 GST_DEBUG_OBJECT (dec, "cannot convert start/stop for seek");
460 vorbis_dec_sink_event (GstPad * pad, GstEvent * event)
462 gboolean ret = FALSE;
465 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
467 GST_LOG_OBJECT (dec, "handling event");
468 switch (GST_EVENT_TYPE (event)) {
470 ret = gst_pad_push_event (dec->srcpad, event);
472 case GST_EVENT_FLUSH_START:
473 ret = gst_pad_push_event (dec->srcpad, event);
475 case GST_EVENT_FLUSH_STOP:
476 /* here we must clean any state in the decoder */
477 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
478 vorbis_synthesis_restart (&dec->vd);
480 gst_vorbis_dec_reset (dec);
481 ret = gst_pad_push_event (dec->srcpad, event);
483 case GST_EVENT_NEWSEGMENT:
487 gint64 start, stop, time;
490 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
491 &start, &stop, &time);
493 /* we need time for now */
494 if (format != GST_FORMAT_TIME)
495 goto newseg_wrong_format;
497 GST_DEBUG_OBJECT (dec,
498 "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
499 ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
500 update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
501 GST_TIME_ARGS (time));
503 /* now configure the values */
504 gst_segment_set_newsegment_full (&dec->segment, update,
505 rate, arate, format, start, stop, time);
506 dec->seqnum = gst_event_get_seqnum (event);
508 if (dec->initialized)
510 ret = gst_pad_push_event (dec->srcpad, event);
512 /* store it to send once we're initialized */
513 dec->pendingevents = g_list_append (dec->pendingevents, event);
520 if (dec->initialized)
522 ret = gst_pad_push_event (dec->srcpad, event);
524 /* store it to send once we're initialized */
525 dec->pendingevents = g_list_append (dec->pendingevents, event);
531 ret = gst_pad_push_event (dec->srcpad, event);
535 gst_object_unref (dec);
542 GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
548 vorbis_handle_identification_packet (GstVorbisDec * vd)
551 const GstAudioChannelPosition *pos = NULL;
552 gint width = GST_VORBIS_DEC_DEFAULT_SAMPLE_WIDTH;
554 switch (vd->vi.channels) {
565 pos = gst_vorbis_channel_positions[vd->vi.channels - 1];
569 GstAudioChannelPosition *posn =
570 g_new (GstAudioChannelPosition, vd->vi.channels);
572 GST_ELEMENT_WARNING (GST_ELEMENT (vd), STREAM, DECODE,
573 (NULL), ("Using NONE channel layout for more than 8 channels"));
575 for (i = 0; i < vd->vi.channels; i++)
576 posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
582 /* negotiate width with downstream */
583 caps = gst_pad_get_allowed_caps (vd->srcpad);
585 if (!gst_caps_is_empty (caps)) {
588 s = gst_caps_get_structure (caps, 0);
589 /* template ensures 16 or 32 */
590 gst_structure_get_int (s, "width", &width);
592 gst_caps_unref (caps);
594 vd->width = width >> 3;
596 caps = gst_caps_copy (gst_pad_get_pad_template_caps (vd->srcpad));
597 gst_caps_set_simple (caps, "rate", G_TYPE_INT, vd->vi.rate,
598 "channels", G_TYPE_INT, vd->vi.channels,
599 "width", G_TYPE_INT, width, NULL);
602 gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
605 if (vd->vi.channels > 8) {
606 g_free ((GstAudioChannelPosition *) pos);
609 gst_pad_set_caps (vd->srcpad, caps);
610 gst_caps_unref (caps);
616 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
619 gchar *encoder = NULL;
620 GstTagList *list, *old_list;
623 GST_DEBUG_OBJECT (vd, "parsing comment packet");
625 buf = gst_buffer_new ();
626 GST_BUFFER_DATA (buf) = gst_ogg_packet_data (packet);
627 GST_BUFFER_SIZE (buf) = gst_ogg_packet_size (packet);
630 gst_tag_list_from_vorbiscomment_buffer (buf, (guint8 *) "\003vorbis", 7,
633 old_list = vd->taglist;
634 vd->taglist = gst_tag_list_merge (vd->taglist, list, GST_TAG_MERGE_REPLACE);
637 gst_tag_list_free (old_list);
638 gst_tag_list_free (list);
639 gst_buffer_unref (buf);
642 GST_ERROR_OBJECT (vd, "couldn't decode comments");
643 vd->taglist = gst_tag_list_new ();
647 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
648 GST_TAG_ENCODER, encoder, NULL);
651 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
652 GST_TAG_ENCODER_VERSION, vd->vi.version,
653 GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
654 if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
655 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
656 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
657 bitrate = vd->vi.bitrate_nominal;
659 if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
660 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
661 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
663 bitrate = vd->vi.bitrate_upper;
665 if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
666 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
667 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
669 bitrate = vd->vi.bitrate_lower;
672 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
673 GST_TAG_BITRATE, (guint) bitrate, NULL);
676 if (vd->initialized) {
677 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd), vd->srcpad,
681 /* Only post them as messages for the time being. *
682 * They will be pushed on the pad once the decoder is initialized */
683 gst_element_post_message (GST_ELEMENT_CAST (vd),
684 gst_message_new_tag (GST_OBJECT (vd), gst_tag_list_copy (vd->taglist)));
691 vorbis_handle_type_packet (GstVorbisDec * vd)
696 g_assert (vd->initialized == FALSE);
698 if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
699 goto synthesis_init_error;
701 if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
702 goto block_init_error;
704 vd->initialized = TRUE;
706 if (vd->pendingevents) {
707 for (walk = vd->pendingevents; walk; walk = g_list_next (walk))
708 gst_pad_push_event (vd->srcpad, GST_EVENT_CAST (walk->data));
709 g_list_free (vd->pendingevents);
710 vd->pendingevents = NULL;
714 /* The tags have already been sent on the bus as messages. */
715 gst_pad_push_event (vd->srcpad, gst_event_new_tag (vd->taglist));
721 synthesis_init_error:
723 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
724 (NULL), ("couldn't initialize synthesis (%d)", res));
725 return GST_FLOW_ERROR;
729 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
730 (NULL), ("couldn't initialize block (%d)", res));
731 return GST_FLOW_ERROR;
736 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
741 GST_DEBUG_OBJECT (vd, "parsing header packet");
743 /* Packetno = 0 if the first byte is exactly 0x01 */
744 packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
746 if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
747 goto header_read_error;
749 switch ((gst_ogg_packet_data (packet))[0]) {
751 res = vorbis_handle_identification_packet (vd);
754 res = vorbis_handle_comment_packet (vd, packet);
757 res = vorbis_handle_type_packet (vd);
761 g_warning ("unknown vorbis header packet found");
770 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
771 (NULL), ("couldn't read header packet (%d)", ret));
772 return GST_FLOW_ERROR;
777 vorbis_dec_push_forward (GstVorbisDec * dec, GstBuffer * buf)
779 GstFlowReturn result;
782 if (!(buf = gst_audio_buffer_clip (buf, &dec->segment, dec->vi.rate,
783 dec->vi.channels * dec->width))) {
784 GST_LOG_OBJECT (dec, "clipped buffer");
789 GST_LOG_OBJECT (dec, "setting DISCONT");
790 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
791 dec->discont = FALSE;
794 GST_DEBUG_OBJECT (dec,
795 "pushing time %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
796 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
797 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
799 result = gst_pad_push (dec->srcpad, buf);
805 vorbis_dec_push_reverse (GstVorbisDec * dec, GstBuffer * buf)
807 GstFlowReturn result = GST_FLOW_OK;
809 dec->queued = g_list_prepend (dec->queued, buf);
815 vorbis_do_timestamps (GstVorbisDec * vd, GstBuffer * buf, gboolean reverse,
816 GstClockTime timestamp, GstClockTime duration)
818 /* interpolate reverse */
819 if (vd->last_timestamp != -1 && reverse)
820 vd->last_timestamp -= duration;
822 /* take buffer timestamp, use interpolated timestamp otherwise */
824 vd->last_timestamp = timestamp;
826 timestamp = vd->last_timestamp;
828 /* interpolate forwards */
829 if (vd->last_timestamp != -1 && !reverse)
830 vd->last_timestamp += duration;
832 GST_BUFFER_TIMESTAMP (buf) = timestamp;
833 GST_BUFFER_DURATION (buf) = duration;
837 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
838 GstClockTime timestamp, GstClockTime duration)
840 vorbis_sample_t **pcm;
843 GstFlowReturn result;
846 if (G_UNLIKELY (!vd->initialized))
847 goto not_initialized;
849 /* normal data packet */
850 /* FIXME, we can skip decoding if the packet is outside of the
851 * segment, this is however not very trivial as we need a previous
852 * packet to decode the current one so we must be carefull not to
853 * throw away too much. For now we decode everything and clip right
854 * before pushing data. */
855 if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
858 if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
861 /* assume all goes well here */
862 result = GST_FLOW_OK;
864 /* count samples ready for reading */
865 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
868 size = sample_count * vd->vi.channels * vd->width;
869 GST_LOG_OBJECT (vd, "%d samples ready for reading, size %d", sample_count,
872 /* alloc buffer for it */
874 gst_pad_alloc_buffer_and_set_caps (vd->srcpad, GST_BUFFER_OFFSET_NONE,
875 size, GST_PAD_CAPS (vd->srcpad), &out);
876 if (G_UNLIKELY (result != GST_FLOW_OK))
879 /* get samples ready for reading now, should be sample_count */
880 if (G_UNLIKELY ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count))
883 /* copy samples in buffer */
884 copy_samples ((vorbis_sample_t *) GST_BUFFER_DATA (out), pcm, sample_count,
885 vd->vi.channels, vd->width);
887 GST_LOG_OBJECT (vd, "setting output size to %d", size);
888 GST_BUFFER_SIZE (out) = size;
890 /* this should not overflow */
892 duration = sample_count * GST_SECOND / vd->vi.rate;
894 vorbis_do_timestamps (vd, out, FALSE, timestamp, duration);
896 if (vd->segment.rate >= 0.0)
897 result = vorbis_dec_push_forward (vd, out);
899 result = vorbis_dec_push_reverse (vd, out);
902 vorbis_synthesis_read (&vd->vd, sample_count);
909 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
910 (NULL), ("no header sent yet"));
911 return GST_FLOW_ERROR;
915 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
916 (NULL), ("couldn't read data packet"));
917 return GST_FLOW_ERROR;
921 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
922 (NULL), ("vorbis decoder did not accept data packet"));
923 return GST_FLOW_ERROR;
927 gst_buffer_unref (out);
928 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
929 (NULL), ("vorbis decoder reported wrong number of samples"));
930 return GST_FLOW_ERROR;
935 vorbis_dec_decode_buffer (GstVorbisDec * vd, GstBuffer * buffer)
938 ogg_packet_wrapper packet_wrapper;
939 GstFlowReturn result = GST_FLOW_OK;
941 /* make ogg_packet out of the buffer */
942 gst_ogg_packet_wrapper_from_buffer (&packet_wrapper, buffer);
943 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
944 /* set some more stuff */
945 packet->granulepos = -1;
946 packet->packetno = 0; /* we don't care */
947 /* EOS does not matter, it is used in vorbis to implement clipping the last
948 * block of samples based on the granulepos. We clip based on segments. */
951 GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
953 /* error out on empty header packets, but just skip empty data packets */
954 if (G_UNLIKELY (packet->bytes == 0)) {
961 /* switch depending on packet type */
962 if ((gst_ogg_packet_data (packet))[0] & 1) {
963 if (vd->initialized) {
964 GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
967 result = vorbis_handle_header_packet (vd, packet);
969 GstClockTime timestamp, duration;
971 timestamp = GST_BUFFER_TIMESTAMP (buffer);
972 duration = GST_BUFFER_DURATION (buffer);
974 result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
982 /* don't error out here, just ignore the buffer, it's invalid for vorbis
984 GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
985 result = GST_FLOW_OK;
992 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
993 result = GST_FLOW_ERROR;
1001 * Buffer decoding order: 7 8 9 4 5 6 3 1 2 EOS
1002 * Discont flag: D D D D
1004 * - Each Discont marks a discont in the decoding order.
1006 * for vorbis, each buffer is a keyframe when we have the previous
1007 * buffer. This means that to decode buffer 7, we need buffer 6, which
1008 * arrives out of order.
1010 * we first gather buffers in the gather queue until we get a DISCONT. We
1011 * prepend each incomming buffer so that they are in reversed order.
1013 * gather queue: 9 8 7
1017 * When a DISCONT is received (buffer 4), we move the gather queue to the
1018 * decode queue. This is simply done be taking the head of the gather queue
1019 * and prepending it to the decode queue. This yields:
1022 * decode queue: 7 8 9
1025 * Then we decode each buffer in the decode queue in order and put the output
1026 * buffer in the output queue. The first buffer (7) will not produce any output
1027 * because it needs the previous buffer (6) which did not arrive yet. This
1031 * decode queue: 7 8 9
1034 * Then we remove the consumed buffers from the decode queue. Buffer 7 is not
1035 * completely consumed, we need to keep it around for when we receive buffer
1042 * Then we accumulate more buffers:
1044 * gather queue: 6 5 4
1048 * prepending to the decode queue on DISCONT yields:
1051 * decode queue: 4 5 6 7
1054 * after decoding and keeping buffer 4:
1058 * output queue: 7 6 5
1062 static GstFlowReturn
1063 vorbis_dec_flush_decode (GstVorbisDec * dec)
1065 GstFlowReturn res = GST_FLOW_OK;
1070 GST_DEBUG_OBJECT (dec, "flushing buffers to decoder");
1074 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1076 GST_DEBUG_OBJECT (dec, "decoding buffer %p, ts %" GST_TIME_FORMAT,
1077 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1079 next = g_list_next (walk);
1081 /* decode buffer, prepend to output queue */
1082 res = vorbis_dec_decode_buffer (dec, buf);
1084 /* if we generated output, we can discard the buffer, else we
1085 * keep it in the queue */
1087 GST_DEBUG_OBJECT (dec, "decoded buffer to %p", dec->queued->data);
1088 dec->decode = g_list_delete_link (dec->decode, walk);
1089 gst_buffer_unref (buf);
1091 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1095 while (dec->queued) {
1096 GstBuffer *buf = GST_BUFFER_CAST (dec->queued->data);
1097 GstClockTime timestamp, duration;
1099 timestamp = GST_BUFFER_TIMESTAMP (buf);
1100 duration = GST_BUFFER_DURATION (buf);
1102 vorbis_do_timestamps (dec, buf, TRUE, timestamp, duration);
1103 res = vorbis_dec_push_forward (dec, buf);
1105 dec->queued = g_list_delete_link (dec->queued, dec->queued);
1110 static GstFlowReturn
1111 vorbis_dec_chain_reverse (GstVorbisDec * vd, gboolean discont, GstBuffer * buf)
1113 GstFlowReturn result = GST_FLOW_OK;
1115 /* if we have a discont, move buffers to the decode list */
1116 if (G_UNLIKELY (discont)) {
1117 GST_DEBUG_OBJECT (vd, "received discont");
1118 while (vd->gather) {
1121 gbuf = GST_BUFFER_CAST (vd->gather->data);
1122 /* remove from the gather list */
1123 vd->gather = g_list_delete_link (vd->gather, vd->gather);
1124 /* copy to decode queue */
1125 vd->decode = g_list_prepend (vd->decode, gbuf);
1127 /* flush and decode the decode queue */
1128 result = vorbis_dec_flush_decode (vd);
1131 GST_DEBUG_OBJECT (vd, "gathering buffer %p of size %u, time %" GST_TIME_FORMAT
1132 ", dur %" GST_TIME_FORMAT, buf, GST_BUFFER_SIZE (buf),
1133 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1134 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1136 /* add buffer to gather queue */
1137 vd->gather = g_list_prepend (vd->gather, buf);
1142 static GstFlowReturn
1143 vorbis_dec_chain_forward (GstVorbisDec * vd, gboolean discont,
1146 GstFlowReturn result;
1148 result = vorbis_dec_decode_buffer (vd, buffer);
1150 gst_buffer_unref (buffer);
1155 static GstFlowReturn
1156 vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
1159 GstFlowReturn result = GST_FLOW_OK;
1162 vd = GST_VORBIS_DEC (gst_pad_get_parent (pad));
1164 discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1166 /* resync on DISCONT */
1167 if (G_UNLIKELY (discont)) {
1168 GST_DEBUG_OBJECT (vd, "received DISCONT buffer");
1169 vd->last_timestamp = GST_CLOCK_TIME_NONE;
1170 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
1171 vorbis_synthesis_restart (&vd->vd);
1176 if (vd->segment.rate >= 0.0)
1177 result = vorbis_dec_chain_forward (vd, discont, buffer);
1179 result = vorbis_dec_chain_reverse (vd, discont, buffer);
1181 gst_object_unref (vd);
1186 static GstStateChangeReturn
1187 vorbis_dec_change_state (GstElement * element, GstStateChange transition)
1189 GstVorbisDec *vd = GST_VORBIS_DEC (element);
1190 GstStateChangeReturn res;
1192 switch (transition) {
1193 case GST_STATE_CHANGE_NULL_TO_READY:
1195 case GST_STATE_CHANGE_READY_TO_PAUSED:
1196 vorbis_info_init (&vd->vi);
1197 vorbis_comment_init (&vd->vc);
1198 vd->initialized = FALSE;
1199 gst_vorbis_dec_reset (vd);
1201 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1207 res = parent_class->change_state (element, transition);
1209 switch (transition) {
1210 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1212 case GST_STATE_CHANGE_PAUSED_TO_READY:
1213 GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
1214 vd->initialized = FALSE;
1215 vorbis_block_clear (&vd->vb);
1216 vorbis_dsp_clear (&vd->vd);
1217 vorbis_comment_clear (&vd->vc);
1218 vorbis_info_clear (&vd->vi);
1219 gst_vorbis_dec_reset (vd);
1221 case GST_STATE_CHANGE_READY_TO_NULL: