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,
312 GST_TIME_ARGS (time));
314 /* and convert to the final format */
316 vorbis_dec_convert (pad, GST_FORMAT_TIME, time, &format, &value)))
319 gst_query_set_position (*query, format, value);
322 "query %p: we return %" G_GINT64_FORMAT " (format %u)", *query, value,
327 case GST_QUERY_DURATION:
329 res = gst_pad_peer_query (dec->sinkpad, query);
335 case GST_QUERY_CONVERT:
337 GstFormat src_fmt, dest_fmt;
338 gint64 src_val, dest_val;
340 gst_query_parse_convert (*query, &src_fmt, &src_val, &dest_fmt,
343 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
345 gst_query_set_convert (*query, src_fmt, src_val, dest_fmt, dest_val);
349 res = gst_pad_query_default (pad, query);
353 gst_object_unref (dec);
360 GST_WARNING_OBJECT (dec, "error handling query");
366 vorbis_dec_sink_query (GstPad * pad, GstQuery ** query)
371 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
373 switch (GST_QUERY_TYPE (*query)) {
374 case GST_QUERY_CONVERT:
376 GstFormat src_fmt, dest_fmt;
377 gint64 src_val, dest_val;
379 gst_query_parse_convert (*query, &src_fmt, &src_val, &dest_fmt,
382 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
384 gst_query_set_convert (*query, src_fmt, src_val, dest_fmt, dest_val);
388 res = gst_pad_query_default (pad, query);
393 gst_object_unref (dec);
400 GST_DEBUG_OBJECT (dec, "error converting value");
406 vorbis_dec_src_event (GstPad * pad, GstEvent * event)
411 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
412 if (G_UNLIKELY (dec == NULL)) {
413 gst_event_unref (event);
417 switch (GST_EVENT_TYPE (event)) {
420 GstFormat format, tformat;
424 GstSeekType cur_type, stop_type;
429 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
431 seqnum = gst_event_get_seqnum (event);
432 gst_event_unref (event);
434 /* First bring the requested format to time */
435 tformat = GST_FORMAT_TIME;
436 if (!(res = vorbis_dec_convert (pad, format, cur, &tformat, &tcur)))
438 if (!(res = vorbis_dec_convert (pad, format, stop, &tformat, &tstop)))
441 /* then seek with time on the peer */
442 real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
443 flags, cur_type, tcur, stop_type, tstop);
444 gst_event_set_seqnum (real_seek, seqnum);
446 res = gst_pad_push_event (dec->sinkpad, real_seek);
450 res = gst_pad_push_event (dec->sinkpad, event);
454 gst_object_unref (dec);
461 GST_DEBUG_OBJECT (dec, "cannot convert start/stop for seek");
467 vorbis_dec_sink_event (GstPad * pad, GstEvent * event)
469 gboolean ret = FALSE;
472 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
474 GST_LOG_OBJECT (dec, "handling event");
475 switch (GST_EVENT_TYPE (event)) {
477 if (dec->segment.rate < 0.0)
478 vorbis_dec_chain_reverse (dec, TRUE, NULL);
479 ret = gst_pad_push_event (dec->srcpad, event);
481 case GST_EVENT_FLUSH_START:
482 ret = gst_pad_push_event (dec->srcpad, event);
484 case GST_EVENT_FLUSH_STOP:
485 /* here we must clean any state in the decoder */
486 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
487 vorbis_synthesis_restart (&dec->vd);
489 gst_vorbis_dec_reset (dec);
490 ret = gst_pad_push_event (dec->srcpad, event);
492 case GST_EVENT_SEGMENT:
496 gst_event_parse_segment (event, &segment);
498 /* we need time for now */
499 if (segment.format != GST_FORMAT_TIME)
500 goto newseg_wrong_format;
502 GST_DEBUG_OBJECT (dec, "segment: %" GST_SEGMENT_FORMAT, &segment);
504 /* now configure the values */
505 gst_segment_copy_into (&segment, &dec->segment);
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_event_default (pad, 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_INFO_OBJECT (vd, "using %s with %d channels and %d bit audio depth",
593 gst_structure_get_name (s), vd->vi.channels, width);
595 gst_caps_unref (caps);
597 vd->width = width >> 3;
599 /* select a copy_samples function, this way we can have specialized versions
600 * for mono/stereo and avoid the depth switch in tremor case */
601 vd->copy_samples = get_copy_sample_func (vd->vi.channels, vd->width);
603 caps = gst_caps_copy (gst_pad_get_pad_template_caps (vd->srcpad));
604 gst_caps_set_simple (caps, "rate", G_TYPE_INT, vd->vi.rate,
605 "channels", G_TYPE_INT, vd->vi.channels,
606 "width", G_TYPE_INT, width, NULL);
609 gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
612 if (vd->vi.channels > 8) {
613 g_free ((GstAudioChannelPosition *) pos);
616 gst_pad_set_caps (vd->srcpad, caps);
617 gst_caps_unref (caps);
623 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
626 gchar *encoder = NULL;
627 GstTagList *list, *old_list;
631 GST_DEBUG_OBJECT (vd, "parsing comment packet");
633 data = gst_ogg_packet_data (packet);
634 size = gst_ogg_packet_size (packet);
637 gst_tag_list_from_vorbiscomment (data, size, (guint8 *) "\003vorbis", 7,
640 old_list = vd->taglist;
641 vd->taglist = gst_tag_list_merge (vd->taglist, list, GST_TAG_MERGE_REPLACE);
644 gst_tag_list_free (old_list);
645 gst_tag_list_free (list);
648 GST_ERROR_OBJECT (vd, "couldn't decode comments");
649 vd->taglist = gst_tag_list_new ();
653 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
654 GST_TAG_ENCODER, encoder, NULL);
657 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
658 GST_TAG_ENCODER_VERSION, vd->vi.version,
659 GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
660 if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
661 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
662 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
663 bitrate = vd->vi.bitrate_nominal;
665 if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
666 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
667 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
669 bitrate = vd->vi.bitrate_upper;
671 if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
672 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
673 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
675 bitrate = vd->vi.bitrate_lower;
678 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
679 GST_TAG_BITRATE, (guint) bitrate, NULL);
682 if (vd->initialized) {
683 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd), vd->srcpad,
687 /* Only post them as messages for the time being. *
688 * They will be pushed on the pad once the decoder is initialized */
689 gst_element_post_message (GST_ELEMENT_CAST (vd),
690 gst_message_new_tag (GST_OBJECT (vd), gst_tag_list_copy (vd->taglist)));
697 vorbis_handle_type_packet (GstVorbisDec * vd)
702 g_assert (vd->initialized == FALSE);
705 if (G_UNLIKELY ((res = vorbis_dsp_init (&vd->vd, &vd->vi))))
706 goto synthesis_init_error;
708 if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
709 goto synthesis_init_error;
711 if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
712 goto block_init_error;
715 vd->initialized = TRUE;
717 if (vd->pendingevents) {
718 for (walk = vd->pendingevents; walk; walk = g_list_next (walk))
719 gst_pad_push_event (vd->srcpad, GST_EVENT_CAST (walk->data));
720 g_list_free (vd->pendingevents);
721 vd->pendingevents = NULL;
725 /* The tags have already been sent on the bus as messages. */
726 gst_pad_push_event (vd->srcpad, gst_event_new_tag (vd->taglist));
732 synthesis_init_error:
734 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
735 (NULL), ("couldn't initialize synthesis (%d)", res));
736 return GST_FLOW_ERROR;
740 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
741 (NULL), ("couldn't initialize block (%d)", res));
742 return GST_FLOW_ERROR;
747 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
752 GST_DEBUG_OBJECT (vd, "parsing header packet");
754 /* Packetno = 0 if the first byte is exactly 0x01 */
755 packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
758 if ((ret = vorbis_dsp_headerin (&vd->vi, &vd->vc, packet)))
760 if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
762 goto header_read_error;
764 switch ((gst_ogg_packet_data (packet))[0]) {
766 res = vorbis_handle_identification_packet (vd);
769 res = vorbis_handle_comment_packet (vd, packet);
772 res = vorbis_handle_type_packet (vd);
776 g_warning ("unknown vorbis header packet found");
785 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
786 (NULL), ("couldn't read header packet (%d)", ret));
787 return GST_FLOW_ERROR;
792 vorbis_dec_push_forward (GstVorbisDec * dec, GstBuffer * buf)
794 GstFlowReturn result;
797 if (!(buf = gst_audio_buffer_clip (buf, &dec->segment, dec->vi.rate,
798 dec->vi.channels * dec->width))) {
799 GST_LOG_OBJECT (dec, "clipped buffer");
804 GST_LOG_OBJECT (dec, "setting DISCONT");
805 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
806 dec->discont = FALSE;
809 GST_DEBUG_OBJECT (dec,
810 "pushing time %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
811 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
812 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
814 result = gst_pad_push (dec->srcpad, buf);
820 vorbis_dec_push_reverse (GstVorbisDec * dec, GstBuffer * buf)
822 GstFlowReturn result = GST_FLOW_OK;
824 dec->queued = g_list_prepend (dec->queued, buf);
830 vorbis_do_timestamps (GstVorbisDec * vd, GstBuffer * buf, gboolean reverse,
831 GstClockTime timestamp, GstClockTime duration)
833 /* interpolate reverse */
834 if (vd->last_timestamp != -1 && duration != -1 && reverse)
835 vd->last_timestamp -= duration;
837 /* take buffer timestamp, use interpolated timestamp otherwise */
839 vd->last_timestamp = timestamp;
841 timestamp = vd->last_timestamp;
843 /* interpolate forwards */
844 if (vd->last_timestamp != -1 && duration != -1 && !reverse)
845 vd->last_timestamp += duration;
848 "keeping timestamp %" GST_TIME_FORMAT " ts %" GST_TIME_FORMAT " dur %"
849 GST_TIME_FORMAT, GST_TIME_ARGS (vd->last_timestamp),
850 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
853 GST_BUFFER_TIMESTAMP (buf) = timestamp;
854 GST_BUFFER_DURATION (buf) = duration;
859 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
860 GstClockTime timestamp, GstClockTime duration)
863 vorbis_sample_t *pcm;
865 vorbis_sample_t **pcm;
868 GstBuffer *out = NULL;
869 GstFlowReturn result;
873 if (G_UNLIKELY (!vd->initialized))
874 goto not_initialized;
876 /* normal data packet */
877 /* FIXME, we can skip decoding if the packet is outside of the
878 * segment, this is however not very trivial as we need a previous
879 * packet to decode the current one so we must be carefull not to
880 * throw away too much. For now we decode everything and clip right
881 * before pushing data. */
884 if (G_UNLIKELY (vorbis_dsp_synthesis (&vd->vd, packet, 1)))
887 if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
890 if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
894 /* assume all goes well here */
895 result = GST_FLOW_OK;
897 /* count samples ready for reading */
899 if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0)
901 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
905 size = sample_count * vd->vi.channels * vd->width;
906 GST_LOG_OBJECT (vd, "%d samples ready for reading, size %d", sample_count,
909 /* alloc buffer for it */
910 out = gst_buffer_new_and_alloc (size);
912 /* get samples ready for reading now, should be sample_count */
914 pcm = GST_BUFFER_DATA (out);
915 if (G_UNLIKELY ((vorbis_dsp_pcmout (&vd->vd, pcm,
916 sample_count)) != sample_count))
918 if (G_UNLIKELY ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count))
923 /* copy samples in buffer */
924 data = gst_buffer_map (out, NULL, NULL, GST_MAP_WRITE);
925 vd->copy_samples ((vorbis_sample_t *) data, pcm,
926 sample_count, vd->vi.channels, vd->width);
929 GST_LOG_OBJECT (vd, "setting output size to %d", size);
930 gst_buffer_unmap (out, data, size);
932 /* this should not overflow */
934 duration = sample_count * GST_SECOND / vd->vi.rate;
936 vorbis_do_timestamps (vd, out, FALSE, timestamp, duration);
938 if (vd->segment.rate >= 0.0)
939 result = vorbis_dec_push_forward (vd, out);
941 result = vorbis_dec_push_reverse (vd, out);
945 /* no output, still keep track of timestamps */
946 vorbis_do_timestamps (vd, NULL, FALSE, timestamp, duration);
949 vorbis_dsp_read (&vd->vd, sample_count);
951 vorbis_synthesis_read (&vd->vd, sample_count);
959 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
960 (NULL), ("no header sent yet"));
961 return GST_FLOW_ERROR;
965 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
966 (NULL), ("couldn't read data packet"));
967 return GST_FLOW_ERROR;
971 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
972 (NULL), ("vorbis decoder did not accept data packet"));
973 return GST_FLOW_ERROR;
977 gst_buffer_unref (out);
978 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
979 (NULL), ("vorbis decoder reported wrong number of samples"));
980 return GST_FLOW_ERROR;
985 vorbis_dec_decode_buffer (GstVorbisDec * vd, GstBuffer * buffer)
988 ogg_packet_wrapper packet_wrapper;
989 GstFlowReturn result = GST_FLOW_OK;
991 /* make ogg_packet out of the buffer */
992 gst_ogg_packet_wrapper_map (&packet_wrapper, buffer);
993 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
994 /* set some more stuff */
995 packet->granulepos = -1;
996 packet->packetno = 0; /* we don't care */
997 /* EOS does not matter, it is used in vorbis to implement clipping the last
998 * block of samples based on the granulepos. We clip based on segments. */
1001 GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
1003 /* error out on empty header packets, but just skip empty data packets */
1004 if (G_UNLIKELY (packet->bytes == 0)) {
1005 if (vd->initialized)
1011 /* switch depending on packet type */
1012 if ((gst_ogg_packet_data (packet))[0] & 1) {
1013 if (vd->initialized) {
1014 GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
1017 result = vorbis_handle_header_packet (vd, packet);
1019 GstClockTime timestamp, duration;
1021 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1022 duration = GST_BUFFER_DURATION (buffer);
1024 result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
1028 gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer);
1034 /* don't error out here, just ignore the buffer, it's invalid for vorbis
1036 GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
1037 result = GST_FLOW_OK;
1044 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
1045 result = GST_FLOW_ERROR;
1053 * Buffer decoding order: 7 8 9 4 5 6 3 1 2 EOS
1054 * Discont flag: D D D D
1056 * - Each Discont marks a discont in the decoding order.
1058 * for vorbis, each buffer is a keyframe when we have the previous
1059 * buffer. This means that to decode buffer 7, we need buffer 6, which
1060 * arrives out of order.
1062 * we first gather buffers in the gather queue until we get a DISCONT. We
1063 * prepend each incomming buffer so that they are in reversed order.
1065 * gather queue: 9 8 7
1069 * When a DISCONT is received (buffer 4), we move the gather queue to the
1070 * decode queue. This is simply done be taking the head of the gather queue
1071 * and prepending it to the decode queue. This yields:
1074 * decode queue: 7 8 9
1077 * Then we decode each buffer in the decode queue in order and put the output
1078 * buffer in the output queue. The first buffer (7) will not produce any output
1079 * because it needs the previous buffer (6) which did not arrive yet. This
1083 * decode queue: 7 8 9
1086 * Then we remove the consumed buffers from the decode queue. Buffer 7 is not
1087 * completely consumed, we need to keep it around for when we receive buffer
1094 * Then we accumulate more buffers:
1096 * gather queue: 6 5 4
1100 * prepending to the decode queue on DISCONT yields:
1103 * decode queue: 4 5 6 7
1106 * after decoding and keeping buffer 4:
1110 * output queue: 7 6 5
1114 static GstFlowReturn
1115 vorbis_dec_flush_decode (GstVorbisDec * dec)
1117 GstFlowReturn res = GST_FLOW_OK;
1122 GST_DEBUG_OBJECT (dec, "flushing buffers to decoder");
1126 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1128 GST_DEBUG_OBJECT (dec, "decoding buffer %p, ts %" GST_TIME_FORMAT,
1129 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1131 next = g_list_next (walk);
1133 /* decode buffer, prepend to output queue */
1134 res = vorbis_dec_decode_buffer (dec, buf);
1136 /* if we generated output, we can discard the buffer, else we
1137 * keep it in the queue */
1139 GST_DEBUG_OBJECT (dec, "decoded buffer to %p", dec->queued->data);
1140 dec->decode = g_list_delete_link (dec->decode, walk);
1141 gst_buffer_unref (buf);
1143 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1147 while (dec->queued) {
1148 GstBuffer *buf = GST_BUFFER_CAST (dec->queued->data);
1149 GstClockTime timestamp, duration;
1151 timestamp = GST_BUFFER_TIMESTAMP (buf);
1152 duration = GST_BUFFER_DURATION (buf);
1154 vorbis_do_timestamps (dec, buf, TRUE, timestamp, duration);
1155 res = vorbis_dec_push_forward (dec, buf);
1157 dec->queued = g_list_delete_link (dec->queued, dec->queued);
1162 static GstFlowReturn
1163 vorbis_dec_chain_reverse (GstVorbisDec * vd, gboolean discont, GstBuffer * buf)
1165 GstFlowReturn result = GST_FLOW_OK;
1167 /* if we have a discont, move buffers to the decode list */
1168 if (G_UNLIKELY (discont)) {
1169 GST_DEBUG_OBJECT (vd, "received discont");
1170 while (vd->gather) {
1173 gbuf = GST_BUFFER_CAST (vd->gather->data);
1174 /* remove from the gather list */
1175 vd->gather = g_list_delete_link (vd->gather, vd->gather);
1176 /* copy to decode queue */
1177 vd->decode = g_list_prepend (vd->decode, gbuf);
1179 /* flush and decode the decode queue */
1180 result = vorbis_dec_flush_decode (vd);
1183 if (G_LIKELY (buf)) {
1184 GST_DEBUG_OBJECT (vd,
1185 "gathering buffer %p of size %u, time %" GST_TIME_FORMAT
1186 ", dur %" GST_TIME_FORMAT, buf, gst_buffer_get_size (buf),
1187 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1188 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1190 /* add buffer to gather queue */
1191 vd->gather = g_list_prepend (vd->gather, buf);
1197 static GstFlowReturn
1198 vorbis_dec_chain_forward (GstVorbisDec * vd, gboolean discont,
1201 GstFlowReturn result;
1203 result = vorbis_dec_decode_buffer (vd, buffer);
1205 gst_buffer_unref (buffer);
1210 static GstFlowReturn
1211 vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
1214 GstFlowReturn result = GST_FLOW_OK;
1217 vd = GST_VORBIS_DEC (gst_pad_get_parent (pad));
1219 discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1221 /* resync on DISCONT */
1222 if (G_UNLIKELY (discont)) {
1223 GST_DEBUG_OBJECT (vd, "received DISCONT buffer");
1224 vd->last_timestamp = GST_CLOCK_TIME_NONE;
1225 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
1226 vorbis_synthesis_restart (&vd->vd);
1231 if (vd->segment.rate >= 0.0)
1232 result = vorbis_dec_chain_forward (vd, discont, buffer);
1234 result = vorbis_dec_chain_reverse (vd, discont, buffer);
1236 gst_object_unref (vd);
1241 static GstStateChangeReturn
1242 vorbis_dec_change_state (GstElement * element, GstStateChange transition)
1244 GstVorbisDec *vd = GST_VORBIS_DEC (element);
1245 GstStateChangeReturn res;
1247 switch (transition) {
1248 case GST_STATE_CHANGE_NULL_TO_READY:
1250 case GST_STATE_CHANGE_READY_TO_PAUSED:
1251 vorbis_info_init (&vd->vi);
1252 vorbis_comment_init (&vd->vc);
1253 vd->initialized = FALSE;
1254 gst_vorbis_dec_reset (vd);
1256 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1262 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1264 switch (transition) {
1265 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1267 case GST_STATE_CHANGE_PAUSED_TO_READY:
1268 GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
1269 vd->initialized = FALSE;
1272 vorbis_block_clear (&vd->vb);
1275 vorbis_dsp_clear (&vd->vd);
1276 vorbis_comment_clear (&vd->vc);
1277 vorbis_info_clear (&vd->vi);
1278 gst_vorbis_dec_reset (vd);
1280 case GST_STATE_CHANGE_READY_TO_NULL: