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 #define gst_vorbis_dec_parent_class parent_class
68 G_DEFINE_TYPE (GST_VORBIS_DEC_GLIB_TYPE_NAME, gst_vorbis_dec, GST_TYPE_ELEMENT);
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);
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);
86 static gboolean vorbis_dec_sink_query (GstPad * pad, GstQuery * query);
89 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
91 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
92 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
94 gobject_class->finalize = vorbis_dec_finalize;
96 gst_element_class_add_pad_template (gstelement_class,
97 gst_static_pad_template_get (&vorbis_dec_src_factory));
99 gst_element_class_add_pad_template (gstelement_class,
100 gst_static_pad_template_get (&vorbis_dec_sink_factory));
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>");
107 gstelement_class->change_state = GST_DEBUG_FUNCPTR (vorbis_dec_change_state);
110 static const GstQueryType *
111 vorbis_get_query_types (GstPad * pad)
113 static const GstQueryType vorbis_dec_src_query_types[] = {
120 return vorbis_dec_src_query_types;
124 gst_vorbis_dec_init (GstVorbisDec * dec)
126 dec->sinkpad = gst_pad_new_from_static_template (&vorbis_dec_sink_factory,
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);
137 dec->srcpad = gst_pad_new_from_static_template (&vorbis_dec_src_factory,
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);
150 dec->pendingevents = NULL;
155 vorbis_dec_finalize (GObject * object)
157 /* Release any possibly allocated libvorbis data.
158 * _clear functions can safely be called multiple times
160 GstVorbisDec *vd = GST_VORBIS_DEC (object);
163 vorbis_block_clear (&vd->vb);
166 vorbis_dsp_clear (&vd->vd);
167 vorbis_comment_clear (&vd->vc);
168 vorbis_info_clear (&vd->vi);
170 G_OBJECT_CLASS (parent_class)->finalize (object);
174 gst_vorbis_dec_reset (GstVorbisDec * dec)
176 dec->last_timestamp = GST_CLOCK_TIME_NONE;
178 dec->seqnum = gst_util_seqnum_next ();
179 gst_segment_init (&dec->segment, GST_FORMAT_TIME);
181 g_list_foreach (dec->queued, (GFunc) gst_mini_object_unref, NULL);
182 g_list_free (dec->queued);
184 g_list_foreach (dec->gather, (GFunc) gst_mini_object_unref, NULL);
185 g_list_free (dec->gather);
187 g_list_foreach (dec->decode, (GFunc) gst_mini_object_unref, NULL);
188 g_list_free (dec->decode);
190 g_list_foreach (dec->pendingevents, (GFunc) gst_mini_object_unref, NULL);
191 g_list_free (dec->pendingevents);
192 dec->pendingevents = NULL;
195 gst_tag_list_free (dec->taglist);
201 vorbis_dec_convert (GstPad * pad,
202 GstFormat src_format, gint64 src_value,
203 GstFormat * dest_format, gint64 * dest_value)
209 if (src_format == *dest_format) {
210 *dest_value = src_value;
214 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
216 if (!dec->initialized)
219 if (dec->sinkpad == pad &&
220 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
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:
230 scale * gst_util_uint64_scale_int (src_value, dec->vi.rate,
237 case GST_FORMAT_DEFAULT:
238 switch (*dest_format) {
239 case GST_FORMAT_BYTES:
240 *dest_value = src_value * dec->width * dec->vi.channels;
242 case GST_FORMAT_TIME:
244 gst_util_uint64_scale_int (src_value, GST_SECOND, dec->vi.rate);
250 case GST_FORMAT_BYTES:
251 switch (*dest_format) {
252 case GST_FORMAT_DEFAULT:
253 *dest_value = src_value / (dec->width * dec->vi.channels);
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);
267 gst_object_unref (dec);
274 GST_DEBUG_OBJECT (dec, "no header packets received");
280 GST_DEBUG_OBJECT (dec, "formats unsupported");
287 vorbis_dec_src_query (GstPad * pad, GstQuery * query)
290 gboolean res = FALSE;
292 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
293 if (G_UNLIKELY (dec == NULL))
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));
409 if (G_UNLIKELY (dec == NULL)) {
410 gst_event_unref (event);
414 switch (GST_EVENT_TYPE (event)) {
417 GstFormat format, tformat;
421 GstSeekType cur_type, stop_type;
426 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
428 seqnum = gst_event_get_seqnum (event);
429 gst_event_unref (event);
431 /* First bring the requested format to time */
432 tformat = GST_FORMAT_TIME;
433 if (!(res = vorbis_dec_convert (pad, format, cur, &tformat, &tcur)))
435 if (!(res = vorbis_dec_convert (pad, format, stop, &tformat, &tstop)))
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);
443 res = gst_pad_push_event (dec->sinkpad, real_seek);
447 res = gst_pad_push_event (dec->sinkpad, event);
451 gst_object_unref (dec);
458 GST_DEBUG_OBJECT (dec, "cannot convert start/stop for seek");
464 vorbis_dec_sink_event (GstPad * pad, GstEvent * event)
466 gboolean ret = FALSE;
469 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
471 GST_LOG_OBJECT (dec, "handling event");
472 switch (GST_EVENT_TYPE (event)) {
474 if (dec->segment.rate < 0.0)
475 vorbis_dec_chain_reverse (dec, TRUE, NULL);
476 ret = gst_pad_push_event (dec->srcpad, event);
478 case GST_EVENT_FLUSH_START:
479 ret = gst_pad_push_event (dec->srcpad, event);
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);
486 gst_vorbis_dec_reset (dec);
487 ret = gst_pad_push_event (dec->srcpad, event);
489 case GST_EVENT_SEGMENT:
491 const GstSegment *segment;
493 gst_event_parse_segment (event, &segment);
495 /* we need time for now */
496 if (segment->format != GST_FORMAT_TIME)
497 goto newseg_wrong_format;
499 GST_DEBUG_OBJECT (dec, "segment: %" GST_SEGMENT_FORMAT, segment);
501 /* now configure the values */
502 gst_segment_copy_into (segment, &dec->segment);
503 dec->seqnum = gst_event_get_seqnum (event);
505 if (dec->initialized)
507 ret = gst_pad_push_event (dec->srcpad, event);
509 /* store it to send once we're initialized */
510 dec->pendingevents = g_list_append (dec->pendingevents, event);
517 if (dec->initialized)
519 ret = gst_pad_push_event (dec->srcpad, event);
521 /* store it to send once we're initialized */
522 dec->pendingevents = g_list_append (dec->pendingevents, event);
528 ret = gst_pad_event_default (pad, event);
532 gst_object_unref (dec);
539 GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
545 vorbis_handle_identification_packet (GstVorbisDec * vd)
548 const GstAudioChannelPosition *pos = NULL;
549 gint width = GST_VORBIS_DEC_DEFAULT_SAMPLE_WIDTH;
551 switch (vd->vi.channels) {
562 pos = gst_vorbis_channel_positions[vd->vi.channels - 1];
566 GstAudioChannelPosition *posn =
567 g_new (GstAudioChannelPosition, vd->vi.channels);
569 GST_ELEMENT_WARNING (GST_ELEMENT (vd), STREAM, DECODE,
570 (NULL), ("Using NONE channel layout for more than 8 channels"));
572 for (i = 0; i < vd->vi.channels; i++)
573 posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
579 /* negotiate width with downstream */
580 caps = gst_pad_get_allowed_caps (vd->srcpad);
582 if (!gst_caps_is_empty (caps)) {
585 s = gst_caps_get_structure (caps, 0);
586 /* template ensures 16 or 32 */
587 gst_structure_get_int (s, "width", &width);
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);
592 gst_caps_unref (caps);
594 vd->width = width >> 3;
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);
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);
606 gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
609 if (vd->vi.channels > 8) {
610 g_free ((GstAudioChannelPosition *) pos);
613 gst_pad_set_caps (vd->srcpad, caps);
614 gst_caps_unref (caps);
620 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
623 gchar *encoder = NULL;
624 GstTagList *list, *old_list;
628 GST_DEBUG_OBJECT (vd, "parsing comment packet");
630 data = gst_ogg_packet_data (packet);
631 size = gst_ogg_packet_size (packet);
634 gst_tag_list_from_vorbiscomment (data, size, (guint8 *) "\003vorbis", 7,
637 old_list = vd->taglist;
638 vd->taglist = gst_tag_list_merge (vd->taglist, list, GST_TAG_MERGE_REPLACE);
641 gst_tag_list_free (old_list);
642 gst_tag_list_free (list);
645 GST_ERROR_OBJECT (vd, "couldn't decode comments");
646 vd->taglist = gst_tag_list_new ();
650 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
651 GST_TAG_ENCODER, encoder, NULL);
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;
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);
666 bitrate = vd->vi.bitrate_upper;
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);
672 bitrate = vd->vi.bitrate_lower;
675 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
676 GST_TAG_BITRATE, (guint) bitrate, NULL);
679 if (vd->initialized) {
680 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd), vd->srcpad,
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)));
694 vorbis_handle_type_packet (GstVorbisDec * vd)
699 g_assert (vd->initialized == FALSE);
702 if (G_UNLIKELY ((res = vorbis_dsp_init (&vd->vd, &vd->vi))))
703 goto synthesis_init_error;
705 if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
706 goto synthesis_init_error;
708 if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
709 goto block_init_error;
712 vd->initialized = TRUE;
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;
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));
729 synthesis_init_error:
731 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
732 (NULL), ("couldn't initialize synthesis (%d)", res));
733 return GST_FLOW_ERROR;
737 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
738 (NULL), ("couldn't initialize block (%d)", res));
739 return GST_FLOW_ERROR;
744 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
749 GST_DEBUG_OBJECT (vd, "parsing header packet");
751 /* Packetno = 0 if the first byte is exactly 0x01 */
752 packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
755 if ((ret = vorbis_dsp_headerin (&vd->vi, &vd->vc, packet)))
757 if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
759 goto header_read_error;
761 switch ((gst_ogg_packet_data (packet))[0]) {
763 res = vorbis_handle_identification_packet (vd);
766 res = vorbis_handle_comment_packet (vd, packet);
769 res = vorbis_handle_type_packet (vd);
773 g_warning ("unknown vorbis header packet found");
782 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
783 (NULL), ("couldn't read header packet (%d)", ret));
784 return GST_FLOW_ERROR;
789 vorbis_dec_push_forward (GstVorbisDec * dec, GstBuffer * buf)
791 GstFlowReturn result;
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");
801 GST_LOG_OBJECT (dec, "setting DISCONT");
802 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
803 dec->discont = FALSE;
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)));
811 result = gst_pad_push (dec->srcpad, buf);
817 vorbis_dec_push_reverse (GstVorbisDec * dec, GstBuffer * buf)
819 GstFlowReturn result = GST_FLOW_OK;
821 dec->queued = g_list_prepend (dec->queued, buf);
827 vorbis_do_timestamps (GstVorbisDec * vd, GstBuffer * buf, gboolean reverse,
828 GstClockTime timestamp, GstClockTime duration)
830 /* interpolate reverse */
831 if (vd->last_timestamp != -1 && duration != -1 && reverse)
832 vd->last_timestamp -= duration;
834 /* take buffer timestamp, use interpolated timestamp otherwise */
836 vd->last_timestamp = timestamp;
838 timestamp = vd->last_timestamp;
840 /* interpolate forwards */
841 if (vd->last_timestamp != -1 && duration != -1 && !reverse)
842 vd->last_timestamp += duration;
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));
850 GST_BUFFER_TIMESTAMP (buf) = timestamp;
851 GST_BUFFER_DURATION (buf) = duration;
856 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
857 GstClockTime timestamp, GstClockTime duration)
860 vorbis_sample_t *pcm;
862 vorbis_sample_t **pcm;
865 GstBuffer *out = NULL;
866 GstFlowReturn result;
870 if (G_UNLIKELY (!vd->initialized))
871 goto not_initialized;
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. */
881 if (G_UNLIKELY (vorbis_dsp_synthesis (&vd->vd, packet, 1)))
884 if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
887 if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
891 /* assume all goes well here */
892 result = GST_FLOW_OK;
894 /* count samples ready for reading */
896 if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0)
898 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
902 size = sample_count * vd->vi.channels * vd->width;
903 GST_LOG_OBJECT (vd, "%d samples ready for reading, size %d", sample_count,
906 /* alloc buffer for it */
907 out = gst_buffer_new_and_alloc (size);
909 /* get samples ready for reading now, should be sample_count */
911 pcm = GST_BUFFER_DATA (out);
912 if (G_UNLIKELY ((vorbis_dsp_pcmout (&vd->vd, pcm,
913 sample_count)) != sample_count))
915 if (G_UNLIKELY ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count))
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);
926 GST_LOG_OBJECT (vd, "setting output size to %d", size);
927 gst_buffer_unmap (out, data, size);
929 /* this should not overflow */
931 duration = sample_count * GST_SECOND / vd->vi.rate;
933 vorbis_do_timestamps (vd, out, FALSE, timestamp, duration);
935 if (vd->segment.rate >= 0.0)
936 result = vorbis_dec_push_forward (vd, out);
938 result = vorbis_dec_push_reverse (vd, out);
942 /* no output, still keep track of timestamps */
943 vorbis_do_timestamps (vd, NULL, FALSE, timestamp, duration);
946 vorbis_dsp_read (&vd->vd, sample_count);
948 vorbis_synthesis_read (&vd->vd, sample_count);
956 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
957 (NULL), ("no header sent yet"));
958 return GST_FLOW_ERROR;
962 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
963 (NULL), ("couldn't read data packet"));
964 return GST_FLOW_ERROR;
968 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
969 (NULL), ("vorbis decoder did not accept data packet"));
970 return GST_FLOW_ERROR;
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;
982 vorbis_dec_handle_header_buffer (GstVorbisDec * vd, GstBuffer * buffer)
985 ogg_packet_wrapper packet_wrapper;
988 gst_ogg_packet_wrapper_map (&packet_wrapper, buffer);
989 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
991 ret = vorbis_handle_header_packet (vd, packet);
993 gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer);
999 #define MIN_NUM_HEADERS 3
1000 static GstFlowReturn
1001 vorbis_dec_handle_header_caps (GstVorbisDec * vd, GstBuffer * buffer)
1003 GstFlowReturn result = GST_FLOW_OK;
1006 const GValue *array;
1007 const GValue *value = NULL;
1008 GstBuffer *buf = NULL;
1010 if ((caps = gst_pad_get_current_caps (vd->sinkpad)) == NULL)
1013 if ((s = gst_caps_get_structure (caps, 0)) == NULL)
1016 array = gst_structure_get_value (s, "streamheader");
1018 if (array == NULL || (gst_value_array_get_size (array) < MIN_NUM_HEADERS))
1021 /* initial header */
1022 value = gst_value_array_get_value (array, 0);
1023 buf = gst_value_get_buffer (value);
1026 result = vorbis_dec_handle_header_buffer (vd, buf);
1027 if (result != GST_FLOW_OK)
1030 /* comment header */
1031 value = gst_value_array_get_value (array, 1);
1032 buf = gst_value_get_buffer (value);
1035 result = vorbis_dec_handle_header_buffer (vd, buf);
1036 if (result != GST_FLOW_OK)
1039 /* bitstream codebook header */
1040 value = gst_value_array_get_value (array, 2);
1041 buf = gst_value_get_buffer (value);
1044 result = vorbis_dec_handle_header_buffer (vd, buf);
1045 if (result != GST_FLOW_OK)
1052 GST_WARNING_OBJECT (vd, "no caps negotiated");
1053 return GST_FLOW_NOT_NEGOTIATED;
1057 GST_WARNING_OBJECT (vd, "streamheader array not found");
1058 return GST_FLOW_NOT_NEGOTIATED;
1062 GST_WARNING_OBJECT (vd, "streamheader with null buffer received");
1063 return GST_FLOW_NOT_NEGOTIATED;
1067 GST_WARNING_OBJECT (vd, "error handling buffer");
1068 return GST_FLOW_NOT_NEGOTIATED;
1072 static GstFlowReturn
1073 vorbis_dec_decode_buffer (GstVorbisDec * vd, GstBuffer * buffer)
1076 ogg_packet_wrapper packet_wrapper;
1077 GstFlowReturn result = GST_FLOW_OK;
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. */
1089 GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
1091 /* error out on empty header packets, but just skip empty data packets */
1092 if (G_UNLIKELY (packet->bytes == 0)) {
1093 if (vd->initialized)
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");
1105 result = vorbis_handle_header_packet (vd, packet);
1107 GstClockTime timestamp, duration;
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;
1116 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1117 duration = GST_BUFFER_DURATION (buffer);
1119 result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
1123 gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer);
1129 /* don't error out here, just ignore the buffer, it's invalid for vorbis
1131 GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
1132 result = GST_FLOW_OK;
1139 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
1140 result = GST_FLOW_ERROR;
1145 invalid_caps_header:
1147 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL),
1148 ("invalid streamheader in caps"));
1155 * Buffer decoding order: 7 8 9 4 5 6 3 1 2 EOS
1156 * Discont flag: D D D D
1158 * - Each Discont marks a discont in the decoding order.
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.
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.
1167 * gather queue: 9 8 7
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:
1176 * decode queue: 7 8 9
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
1185 * decode queue: 7 8 9
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
1196 * Then we accumulate more buffers:
1198 * gather queue: 6 5 4
1202 * prepending to the decode queue on DISCONT yields:
1205 * decode queue: 4 5 6 7
1208 * after decoding and keeping buffer 4:
1212 * output queue: 7 6 5
1216 static GstFlowReturn
1217 vorbis_dec_flush_decode (GstVorbisDec * dec)
1219 GstFlowReturn res = GST_FLOW_OK;
1224 GST_DEBUG_OBJECT (dec, "flushing buffers to decoder");
1228 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1230 GST_DEBUG_OBJECT (dec, "decoding buffer %p, ts %" GST_TIME_FORMAT,
1231 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1233 next = g_list_next (walk);
1235 /* decode buffer, prepend to output queue */
1236 res = vorbis_dec_decode_buffer (dec, buf);
1238 /* if we generated output, we can discard the buffer, else we
1239 * keep it in the queue */
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);
1245 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1249 while (dec->queued) {
1250 GstBuffer *buf = GST_BUFFER_CAST (dec->queued->data);
1251 GstClockTime timestamp, duration;
1253 timestamp = GST_BUFFER_TIMESTAMP (buf);
1254 duration = GST_BUFFER_DURATION (buf);
1256 vorbis_do_timestamps (dec, buf, TRUE, timestamp, duration);
1257 res = vorbis_dec_push_forward (dec, buf);
1259 dec->queued = g_list_delete_link (dec->queued, dec->queued);
1264 static GstFlowReturn
1265 vorbis_dec_chain_reverse (GstVorbisDec * vd, gboolean discont, GstBuffer * buf)
1267 GstFlowReturn result = GST_FLOW_OK;
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) {
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);
1281 /* flush and decode the decode queue */
1282 result = vorbis_dec_flush_decode (vd);
1285 if (G_LIKELY (buf)) {
1286 GST_DEBUG_OBJECT (vd,
1287 "gathering buffer %p of size %u, time %" GST_TIME_FORMAT
1288 ", dur %" GST_TIME_FORMAT, buf, gst_buffer_get_size (buf),
1289 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1290 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1292 /* add buffer to gather queue */
1293 vd->gather = g_list_prepend (vd->gather, buf);
1299 static GstFlowReturn
1300 vorbis_dec_chain_forward (GstVorbisDec * vd, gboolean discont,
1303 GstFlowReturn result;
1305 result = vorbis_dec_decode_buffer (vd, buffer);
1307 gst_buffer_unref (buffer);
1312 static GstFlowReturn
1313 vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
1316 GstFlowReturn result = GST_FLOW_OK;
1319 vd = GST_VORBIS_DEC (gst_pad_get_parent (pad));
1321 discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1323 /* resync on DISCONT */
1324 if (G_UNLIKELY (discont)) {
1325 GST_DEBUG_OBJECT (vd, "received DISCONT buffer");
1326 vd->last_timestamp = GST_CLOCK_TIME_NONE;
1327 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
1328 vorbis_synthesis_restart (&vd->vd);
1333 if (vd->segment.rate >= 0.0)
1334 result = vorbis_dec_chain_forward (vd, discont, buffer);
1336 result = vorbis_dec_chain_reverse (vd, discont, buffer);
1338 gst_object_unref (vd);
1343 static GstStateChangeReturn
1344 vorbis_dec_change_state (GstElement * element, GstStateChange transition)
1346 GstVorbisDec *vd = GST_VORBIS_DEC (element);
1347 GstStateChangeReturn res;
1349 switch (transition) {
1350 case GST_STATE_CHANGE_NULL_TO_READY:
1352 case GST_STATE_CHANGE_READY_TO_PAUSED:
1353 vorbis_info_init (&vd->vi);
1354 vorbis_comment_init (&vd->vc);
1355 vd->initialized = FALSE;
1356 gst_vorbis_dec_reset (vd);
1358 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1364 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1366 switch (transition) {
1367 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1369 case GST_STATE_CHANGE_PAUSED_TO_READY:
1370 GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
1371 vd->initialized = FALSE;
1374 vorbis_block_clear (&vd->vb);
1377 vorbis_dsp_clear (&vd->vd);
1378 vorbis_comment_clear (&vd->vc);
1379 vorbis_info_clear (&vd->vi);
1380 gst_vorbis_dec_reset (vd);
1382 case GST_STATE_CHANGE_READY_TO_NULL: