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 const GstElementDetails vorbis_dec_details =
55 GST_ELEMENT_DETAILS ("Vorbis audio decoder",
56 "Codec/Decoder/Audio",
57 "decode raw vorbis streams to float audio",
58 "Benjamin Otte <in7y118@public.uni-hamburg.de>");
60 static GstStaticPadTemplate vorbis_dec_src_factory =
61 GST_STATIC_PAD_TEMPLATE ("src",
64 GST_STATIC_CAPS ("audio/x-raw-float, "
65 "rate = (int) [ 1, MAX ], "
66 "channels = (int) [ 1, 256 ], " "endianness = (int) BYTE_ORDER, "
67 /* no ifdef in macros, please
68 #ifdef GST_VORBIS_DEC_SEQUENTIAL
69 "layout = \"sequential\", "
75 static GstStaticPadTemplate vorbis_dec_sink_factory =
76 GST_STATIC_PAD_TEMPLATE ("sink",
79 GST_STATIC_CAPS ("audio/x-vorbis")
82 GST_BOILERPLATE (GstVorbisDec, gst_vorbis_dec, GstElement, GST_TYPE_ELEMENT);
84 static void vorbis_dec_finalize (GObject * object);
85 static gboolean vorbis_dec_sink_event (GstPad * pad, GstEvent * event);
86 static GstFlowReturn vorbis_dec_chain (GstPad * pad, GstBuffer * buffer);
87 static GstFlowReturn vorbis_dec_chain_forward (GstVorbisDec * vd,
88 gboolean discont, GstBuffer * buffer);
89 static GstStateChangeReturn vorbis_dec_change_state (GstElement * element,
90 GstStateChange transition);
92 static gboolean vorbis_dec_src_event (GstPad * pad, GstEvent * event);
93 static gboolean vorbis_dec_src_query (GstPad * pad, GstQuery * query);
94 static gboolean vorbis_dec_convert (GstPad * pad,
95 GstFormat src_format, gint64 src_value,
96 GstFormat * dest_format, gint64 * dest_value);
98 static gboolean vorbis_dec_sink_query (GstPad * pad, GstQuery * query);
101 gst_vorbis_dec_base_init (gpointer g_class)
103 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
104 GstPadTemplate *src_template, *sink_template;
106 src_template = gst_static_pad_template_get (&vorbis_dec_src_factory);
107 gst_element_class_add_pad_template (element_class, src_template);
109 sink_template = gst_static_pad_template_get (&vorbis_dec_sink_factory);
110 gst_element_class_add_pad_template (element_class, sink_template);
112 gst_element_class_set_details (element_class, &vorbis_dec_details);
116 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
118 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
121 gobject_class->finalize = vorbis_dec_finalize;
123 gstelement_class->change_state = GST_DEBUG_FUNCPTR (vorbis_dec_change_state);
126 static const GstQueryType *
127 vorbis_get_query_types (GstPad * pad)
129 static const GstQueryType vorbis_dec_src_query_types[] = {
136 return vorbis_dec_src_query_types;
140 gst_vorbis_dec_init (GstVorbisDec * dec, GstVorbisDecClass * g_class)
142 dec->sinkpad = gst_pad_new_from_static_template (&vorbis_dec_sink_factory,
145 gst_pad_set_event_function (dec->sinkpad,
146 GST_DEBUG_FUNCPTR (vorbis_dec_sink_event));
147 gst_pad_set_chain_function (dec->sinkpad,
148 GST_DEBUG_FUNCPTR (vorbis_dec_chain));
149 gst_pad_set_query_function (dec->sinkpad,
150 GST_DEBUG_FUNCPTR (vorbis_dec_sink_query));
151 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
153 dec->srcpad = gst_pad_new_from_static_template (&vorbis_dec_src_factory,
156 gst_pad_set_event_function (dec->srcpad,
157 GST_DEBUG_FUNCPTR (vorbis_dec_src_event));
158 gst_pad_set_query_type_function (dec->srcpad,
159 GST_DEBUG_FUNCPTR (vorbis_get_query_types));
160 gst_pad_set_query_function (dec->srcpad,
161 GST_DEBUG_FUNCPTR (vorbis_dec_src_query));
162 gst_pad_use_fixed_caps (dec->srcpad);
163 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
166 dec->pendingevents = NULL;
171 vorbis_dec_finalize (GObject * object)
173 /* Release any possibly allocated libvorbis data.
174 * _clear functions can safely be called multiple times
176 GstVorbisDec *vd = GST_VORBIS_DEC (object);
178 vorbis_block_clear (&vd->vb);
179 vorbis_dsp_clear (&vd->vd);
180 vorbis_comment_clear (&vd->vc);
181 vorbis_info_clear (&vd->vi);
183 G_OBJECT_CLASS (parent_class)->finalize (object);
187 gst_vorbis_dec_reset (GstVorbisDec * dec)
189 dec->last_timestamp = GST_CLOCK_TIME_NONE;
191 dec->seqnum = gst_util_seqnum_next ();
192 gst_segment_init (&dec->segment, GST_FORMAT_TIME);
194 g_list_foreach (dec->queued, (GFunc) gst_mini_object_unref, NULL);
195 g_list_free (dec->queued);
197 g_list_foreach (dec->gather, (GFunc) gst_mini_object_unref, NULL);
198 g_list_free (dec->gather);
200 g_list_foreach (dec->decode, (GFunc) gst_mini_object_unref, NULL);
201 g_list_free (dec->decode);
203 g_list_foreach (dec->pendingevents, (GFunc) gst_mini_object_unref, NULL);
204 g_list_free (dec->pendingevents);
205 dec->pendingevents = NULL;
208 gst_tag_list_free (dec->taglist);
214 vorbis_dec_convert (GstPad * pad,
215 GstFormat src_format, gint64 src_value,
216 GstFormat * dest_format, gint64 * dest_value)
222 if (src_format == *dest_format) {
223 *dest_value = src_value;
227 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
229 if (!dec->initialized)
232 if (dec->sinkpad == pad &&
233 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
236 switch (src_format) {
237 case GST_FORMAT_TIME:
238 switch (*dest_format) {
239 case GST_FORMAT_BYTES:
240 scale = dec->width * dec->vi.channels;
241 case GST_FORMAT_DEFAULT:
243 scale * gst_util_uint64_scale_int (src_value, dec->vi.rate,
250 case GST_FORMAT_DEFAULT:
251 switch (*dest_format) {
252 case GST_FORMAT_BYTES:
253 *dest_value = src_value * dec->width * dec->vi.channels;
255 case GST_FORMAT_TIME:
257 gst_util_uint64_scale_int (src_value, GST_SECOND, dec->vi.rate);
263 case GST_FORMAT_BYTES:
264 switch (*dest_format) {
265 case GST_FORMAT_DEFAULT:
266 *dest_value = src_value / (dec->width * dec->vi.channels);
268 case GST_FORMAT_TIME:
269 *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
270 dec->vi.rate * dec->width * dec->vi.channels);
280 gst_object_unref (dec);
287 GST_DEBUG_OBJECT (dec, "no header packets received");
293 GST_DEBUG_OBJECT (dec, "formats unsupported");
300 vorbis_dec_src_query (GstPad * pad, GstQuery * query)
303 gboolean res = FALSE;
305 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
307 switch (GST_QUERY_TYPE (query)) {
308 case GST_QUERY_POSITION:
314 gst_query_parse_position (query, &format, NULL);
316 /* we start from the last seen time */
317 time = dec->last_timestamp;
318 /* correct for the segment values */
319 time = gst_segment_to_stream_time (&dec->segment, GST_FORMAT_TIME, time);
322 "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
324 /* and convert to the final format */
326 vorbis_dec_convert (pad, GST_FORMAT_TIME, time, &format, &value)))
329 gst_query_set_position (query, format, value);
332 "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
337 case GST_QUERY_DURATION:
339 res = gst_pad_peer_query (dec->sinkpad, query);
345 case GST_QUERY_CONVERT:
347 GstFormat src_fmt, dest_fmt;
348 gint64 src_val, dest_val;
350 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
352 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
354 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
358 res = gst_pad_query_default (pad, query);
362 gst_object_unref (dec);
369 GST_WARNING_OBJECT (dec, "error handling query");
375 vorbis_dec_sink_query (GstPad * pad, GstQuery * query)
380 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
382 switch (GST_QUERY_TYPE (query)) {
383 case GST_QUERY_CONVERT:
385 GstFormat src_fmt, dest_fmt;
386 gint64 src_val, dest_val;
388 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
390 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
392 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
396 res = gst_pad_query_default (pad, query);
401 gst_object_unref (dec);
408 GST_DEBUG_OBJECT (dec, "error converting value");
414 vorbis_dec_src_event (GstPad * pad, GstEvent * event)
419 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
421 switch (GST_EVENT_TYPE (event)) {
424 GstFormat format, tformat;
428 GstSeekType cur_type, stop_type;
433 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
435 seqnum = gst_event_get_seqnum (event);
436 gst_event_unref (event);
438 /* First bring the requested format to time */
439 tformat = GST_FORMAT_TIME;
440 if (!(res = vorbis_dec_convert (pad, format, cur, &tformat, &tcur)))
442 if (!(res = vorbis_dec_convert (pad, format, stop, &tformat, &tstop)))
445 /* then seek with time on the peer */
446 real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
447 flags, cur_type, tcur, stop_type, tstop);
448 gst_event_set_seqnum (real_seek, seqnum);
450 res = gst_pad_push_event (dec->sinkpad, real_seek);
454 res = gst_pad_push_event (dec->sinkpad, event);
458 gst_object_unref (dec);
465 GST_DEBUG_OBJECT (dec, "cannot convert start/stop for seek");
471 vorbis_dec_sink_event (GstPad * pad, GstEvent * event)
473 gboolean ret = FALSE;
476 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
478 GST_LOG_OBJECT (dec, "handling event");
479 switch (GST_EVENT_TYPE (event)) {
481 ret = gst_pad_push_event (dec->srcpad, event);
483 case GST_EVENT_FLUSH_START:
484 ret = gst_pad_push_event (dec->srcpad, event);
486 case GST_EVENT_FLUSH_STOP:
487 /* here we must clean any state in the decoder */
488 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
489 vorbis_synthesis_restart (&dec->vd);
491 gst_vorbis_dec_reset (dec);
492 ret = gst_pad_push_event (dec->srcpad, event);
494 case GST_EVENT_NEWSEGMENT:
498 gint64 start, stop, time;
501 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
502 &start, &stop, &time);
504 /* we need time for now */
505 if (format != GST_FORMAT_TIME)
506 goto newseg_wrong_format;
508 GST_DEBUG_OBJECT (dec,
509 "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
510 ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
511 update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
512 GST_TIME_ARGS (time));
514 /* now configure the values */
515 gst_segment_set_newsegment_full (&dec->segment, update,
516 rate, arate, format, start, stop, time);
517 dec->seqnum = gst_event_get_seqnum (event);
519 if (dec->initialized)
521 ret = gst_pad_push_event (dec->srcpad, event);
523 /* store it to send once we're initialized */
524 dec->pendingevents = g_list_append (dec->pendingevents, event);
531 if (dec->initialized)
533 ret = gst_pad_push_event (dec->srcpad, event);
535 /* store it to send once we're initialized */
536 dec->pendingevents = g_list_append (dec->pendingevents, event);
542 ret = gst_pad_push_event (dec->srcpad, event);
546 gst_object_unref (dec);
553 GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
559 vorbis_handle_identification_packet (GstVorbisDec * vd)
562 const GstAudioChannelPosition *pos = NULL;
564 switch (vd->vi.channels) {
575 pos = gst_vorbis_channel_positions[vd->vi.channels - 1];
579 GstAudioChannelPosition *posn =
580 g_new (GstAudioChannelPosition, vd->vi.channels);
582 GST_ELEMENT_WARNING (GST_ELEMENT (vd), STREAM, DECODE,
583 (NULL), ("Using NONE channel layout for more than 8 channels"));
585 for (i = 0; i < vd->vi.channels; i++)
586 posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
594 caps = gst_caps_copy (gst_pad_get_pad_template_caps (vd->srcpad));
595 gst_caps_set_simple (caps, "rate", G_TYPE_INT, vd->vi.rate,
596 "channels", G_TYPE_INT, vd->vi.channels);
599 gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
602 if (vd->vi.channels > 8) {
603 g_free ((GstAudioChannelPosition *) pos);
606 gst_pad_set_caps (vd->srcpad, caps);
607 gst_caps_unref (caps);
613 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
616 gchar *encoder = NULL;
617 GstTagList *list, *old_list;
620 GST_DEBUG_OBJECT (vd, "parsing comment packet");
622 buf = gst_buffer_new ();
623 GST_BUFFER_DATA (buf) = packet->packet;
624 GST_BUFFER_SIZE (buf) = packet->bytes;
627 gst_tag_list_from_vorbiscomment_buffer (buf, (guint8 *) "\003vorbis", 7,
630 old_list = vd->taglist;
631 vd->taglist = gst_tag_list_merge (vd->taglist, list, GST_TAG_MERGE_REPLACE);
634 gst_tag_list_free (old_list);
635 gst_tag_list_free (list);
636 gst_buffer_unref (buf);
639 GST_ERROR_OBJECT (vd, "couldn't decode comments");
640 vd->taglist = gst_tag_list_new ();
644 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
645 GST_TAG_ENCODER, encoder, NULL);
648 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
649 GST_TAG_ENCODER_VERSION, vd->vi.version,
650 GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
651 if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
652 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
653 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
654 bitrate = vd->vi.bitrate_nominal;
656 if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
657 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
658 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
660 bitrate = vd->vi.bitrate_upper;
662 if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
663 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
664 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
666 bitrate = vd->vi.bitrate_lower;
669 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
670 GST_TAG_BITRATE, (guint) bitrate, NULL);
673 if (vd->initialized) {
674 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd), vd->srcpad,
678 /* Only post them as messages for the time being. *
679 * They will be pushed on the pad once the decoder is initialized */
680 gst_element_post_message (GST_ELEMENT_CAST (vd),
681 gst_message_new_tag (GST_OBJECT (vd), gst_tag_list_copy (vd->taglist)));
688 vorbis_handle_type_packet (GstVorbisDec * vd)
693 g_assert (vd->initialized == FALSE);
695 if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
696 goto synthesis_init_error;
698 if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
699 goto block_init_error;
701 vd->initialized = TRUE;
703 if (vd->pendingevents) {
704 for (walk = vd->pendingevents; walk; walk = g_list_next (walk))
705 gst_pad_push_event (vd->srcpad, GST_EVENT_CAST (walk->data));
706 g_list_free (vd->pendingevents);
707 vd->pendingevents = NULL;
711 /* The tags have already been sent on the bus as messages. */
712 gst_pad_push_event (vd->srcpad, gst_event_new_tag (vd->taglist));
718 synthesis_init_error:
720 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
721 (NULL), ("couldn't initialize synthesis (%d)", res));
722 return GST_FLOW_ERROR;
726 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
727 (NULL), ("couldn't initialize block (%d)", res));
728 return GST_FLOW_ERROR;
733 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
738 GST_DEBUG_OBJECT (vd, "parsing header packet");
740 /* Packetno = 0 if the first byte is exactly 0x01 */
741 packet->b_o_s = (packet->packet[0] == 0x1) ? 1 : 0;
743 if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
744 goto header_read_error;
746 switch (packet->packet[0]) {
748 res = vorbis_handle_identification_packet (vd);
751 res = vorbis_handle_comment_packet (vd, packet);
754 res = vorbis_handle_type_packet (vd);
758 g_warning ("unknown vorbis header packet found");
767 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
768 (NULL), ("couldn't read header packet (%d)", ret));
769 return GST_FLOW_ERROR;
773 /* These samples can be outside of the float -1.0 -- 1.0 range, this
774 * is allowed, downstream elements are supposed to clip */
776 copy_samples (float *out, float **in, guint samples, gint channels)
780 #ifdef GST_VORBIS_DEC_SEQUENTIAL
781 for (i = 0; i < channels; i++) {
782 memcpy (out, in[i], samples * sizeof (float));
786 for (j = 0; j < samples; j++) {
787 for (i = 0; i < channels; i++) {
795 vorbis_dec_push_forward (GstVorbisDec * dec, GstBuffer * buf)
797 GstFlowReturn result;
800 if (!(buf = gst_audio_buffer_clip (buf, &dec->segment, dec->vi.rate,
801 dec->vi.channels * dec->width))) {
802 GST_LOG_OBJECT (dec, "clipped buffer");
807 GST_LOG_OBJECT (dec, "setting DISCONT");
808 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
809 dec->discont = FALSE;
812 GST_DEBUG_OBJECT (dec,
813 "pushing time %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
814 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
815 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
817 result = gst_pad_push (dec->srcpad, buf);
823 vorbis_dec_push_reverse (GstVorbisDec * dec, GstBuffer * buf)
825 GstFlowReturn result = GST_FLOW_OK;
827 dec->queued = g_list_prepend (dec->queued, buf);
833 vorbis_do_timestamps (GstVorbisDec * vd, GstBuffer * buf, gboolean reverse,
834 GstClockTime timestamp, GstClockTime duration)
836 /* interpolate reverse */
837 if (vd->last_timestamp != -1 && reverse)
838 vd->last_timestamp -= duration;
840 /* take buffer timestamp, use interpolated timestamp otherwise */
842 vd->last_timestamp = timestamp;
844 timestamp = vd->last_timestamp;
846 /* interpolate forwards */
847 if (vd->last_timestamp != -1 && !reverse)
848 vd->last_timestamp += duration;
850 GST_BUFFER_TIMESTAMP (buf) = timestamp;
851 GST_BUFFER_DURATION (buf) = duration;
855 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
856 GstClockTime timestamp, GstClockTime duration)
861 GstFlowReturn result;
864 if (G_UNLIKELY (!vd->initialized))
865 goto not_initialized;
867 /* normal data packet */
868 /* FIXME, we can skip decoding if the packet is outside of the
869 * segment, this is however not very trivial as we need a previous
870 * packet to decode the current one so we must be carefull not to
871 * throw away too much. For now we decode everything and clip right
872 * before pushing data. */
873 if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
876 if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
879 /* assume all goes well here */
880 result = GST_FLOW_OK;
882 /* count samples ready for reading */
883 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
886 size = sample_count * vd->vi.channels * vd->width;
887 GST_LOG_OBJECT (vd, "%d samples ready for reading, size %d", sample_count,
890 /* alloc buffer for it */
892 gst_pad_alloc_buffer_and_set_caps (vd->srcpad, GST_BUFFER_OFFSET_NONE,
893 size, GST_PAD_CAPS (vd->srcpad), &out);
894 if (G_UNLIKELY (result != GST_FLOW_OK))
897 /* get samples ready for reading now, should be sample_count */
898 if (G_UNLIKELY ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count))
901 /* copy samples in buffer */
902 copy_samples ((float *) GST_BUFFER_DATA (out), pcm, sample_count,
905 GST_LOG_OBJECT (vd, "setting output size to %d", size);
906 GST_BUFFER_SIZE (out) = size;
908 /* this should not overflow */
910 duration = sample_count * GST_SECOND / vd->vi.rate;
912 vorbis_do_timestamps (vd, out, FALSE, timestamp, duration);
914 if (vd->segment.rate >= 0.0)
915 result = vorbis_dec_push_forward (vd, out);
917 result = vorbis_dec_push_reverse (vd, out);
920 vorbis_synthesis_read (&vd->vd, sample_count);
927 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
928 (NULL), ("no header sent yet"));
929 return GST_FLOW_ERROR;
933 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
934 (NULL), ("couldn't read data packet"));
935 return GST_FLOW_ERROR;
939 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
940 (NULL), ("vorbis decoder did not accept data packet"));
941 return GST_FLOW_ERROR;
945 gst_buffer_unref (out);
946 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
947 (NULL), ("vorbis decoder reported wrong number of samples"));
948 return GST_FLOW_ERROR;
953 vorbis_dec_decode_buffer (GstVorbisDec * vd, GstBuffer * buffer)
956 GstFlowReturn result = GST_FLOW_OK;
958 /* make ogg_packet out of the buffer */
959 packet.packet = GST_BUFFER_DATA (buffer);
960 packet.bytes = GST_BUFFER_SIZE (buffer);
961 packet.granulepos = -1;
962 packet.packetno = 0; /* we don't care */
963 /* EOS does not matter, it is used in vorbis to implement clipping the last
964 * block of samples based on the granulepos. We clip based on segments. */
967 GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet.bytes);
969 /* error out on empty header packets, but just skip empty data packets */
970 if (G_UNLIKELY (packet.bytes == 0)) {
977 /* switch depending on packet type */
978 if (packet.packet[0] & 1) {
979 if (vd->initialized) {
980 GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
983 result = vorbis_handle_header_packet (vd, &packet);
985 GstClockTime timestamp, duration;
987 timestamp = GST_BUFFER_TIMESTAMP (buffer);
988 duration = GST_BUFFER_DURATION (buffer);
990 result = vorbis_handle_data_packet (vd, &packet, timestamp, duration);
998 /* don't error out here, just ignore the buffer, it's invalid for vorbis
1000 GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
1001 result = GST_FLOW_OK;
1008 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
1009 result = GST_FLOW_ERROR;
1017 * Buffer decoding order: 7 8 9 4 5 6 3 1 2 EOS
1018 * Discont flag: D D D D
1020 * - Each Discont marks a discont in the decoding order.
1022 * for vorbis, each buffer is a keyframe when we have the previous
1023 * buffer. This means that to decode buffer 7, we need buffer 6, which
1024 * arrives out of order.
1026 * we first gather buffers in the gather queue until we get a DISCONT. We
1027 * prepend each incomming buffer so that they are in reversed order.
1029 * gather queue: 9 8 7
1033 * When a DISCONT is received (buffer 4), we move the gather queue to the
1034 * decode queue. This is simply done be taking the head of the gather queue
1035 * and prepending it to the decode queue. This yields:
1038 * decode queue: 7 8 9
1041 * Then we decode each buffer in the decode queue in order and put the output
1042 * buffer in the output queue. The first buffer (7) will not produce any output
1043 * because it needs the previous buffer (6) which did not arrive yet. This
1047 * decode queue: 7 8 9
1050 * Then we remove the consumed buffers from the decode queue. Buffer 7 is not
1051 * completely consumed, we need to keep it around for when we receive buffer
1058 * Then we accumulate more buffers:
1060 * gather queue: 6 5 4
1064 * prepending to the decode queue on DISCONT yields:
1067 * decode queue: 4 5 6 7
1070 * after decoding and keeping buffer 4:
1074 * output queue: 7 6 5
1078 static GstFlowReturn
1079 vorbis_dec_flush_decode (GstVorbisDec * dec)
1081 GstFlowReturn res = GST_FLOW_OK;
1086 GST_DEBUG_OBJECT (dec, "flushing buffers to decoder");
1090 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1092 GST_DEBUG_OBJECT (dec, "decoding buffer %p, ts %" GST_TIME_FORMAT,
1093 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1095 next = g_list_next (walk);
1097 /* decode buffer, prepend to output queue */
1098 res = vorbis_dec_decode_buffer (dec, buf);
1100 /* if we generated output, we can discard the buffer, else we
1101 * keep it in the queue */
1103 GST_DEBUG_OBJECT (dec, "decoded buffer to %p", dec->queued->data);
1104 dec->decode = g_list_delete_link (dec->decode, walk);
1105 gst_buffer_unref (buf);
1107 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1111 while (dec->queued) {
1112 GstBuffer *buf = GST_BUFFER_CAST (dec->queued->data);
1113 GstClockTime timestamp, duration;
1115 timestamp = GST_BUFFER_TIMESTAMP (buf);
1116 duration = GST_BUFFER_DURATION (buf);
1118 vorbis_do_timestamps (dec, buf, TRUE, timestamp, duration);
1119 res = vorbis_dec_push_forward (dec, buf);
1121 dec->queued = g_list_delete_link (dec->queued, dec->queued);
1126 static GstFlowReturn
1127 vorbis_dec_chain_reverse (GstVorbisDec * vd, gboolean discont, GstBuffer * buf)
1129 GstFlowReturn result = GST_FLOW_OK;
1131 /* if we have a discont, move buffers to the decode list */
1132 if (G_UNLIKELY (discont)) {
1133 GST_DEBUG_OBJECT (vd, "received discont");
1134 while (vd->gather) {
1137 gbuf = GST_BUFFER_CAST (vd->gather->data);
1138 /* remove from the gather list */
1139 vd->gather = g_list_delete_link (vd->gather, vd->gather);
1140 /* copy to decode queue */
1141 vd->decode = g_list_prepend (vd->decode, gbuf);
1143 /* flush and decode the decode queue */
1144 result = vorbis_dec_flush_decode (vd);
1147 GST_DEBUG_OBJECT (vd, "gathering buffer %p of size %u, time %" GST_TIME_FORMAT
1148 ", dur %" GST_TIME_FORMAT, buf, GST_BUFFER_SIZE (buf),
1149 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1150 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1152 /* add buffer to gather queue */
1153 vd->gather = g_list_prepend (vd->gather, buf);
1158 static GstFlowReturn
1159 vorbis_dec_chain_forward (GstVorbisDec * vd, gboolean discont,
1162 GstFlowReturn result;
1164 result = vorbis_dec_decode_buffer (vd, buffer);
1166 gst_buffer_unref (buffer);
1171 static GstFlowReturn
1172 vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
1175 GstFlowReturn result = GST_FLOW_OK;
1178 vd = GST_VORBIS_DEC (gst_pad_get_parent (pad));
1180 discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1182 /* resync on DISCONT */
1183 if (G_UNLIKELY (discont)) {
1184 GST_DEBUG_OBJECT (vd, "received DISCONT buffer");
1185 vd->last_timestamp = GST_CLOCK_TIME_NONE;
1186 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
1187 vorbis_synthesis_restart (&vd->vd);
1192 if (vd->segment.rate >= 0.0)
1193 result = vorbis_dec_chain_forward (vd, discont, buffer);
1195 result = vorbis_dec_chain_reverse (vd, discont, buffer);
1197 gst_object_unref (vd);
1202 static GstStateChangeReturn
1203 vorbis_dec_change_state (GstElement * element, GstStateChange transition)
1205 GstVorbisDec *vd = GST_VORBIS_DEC (element);
1206 GstStateChangeReturn res;
1208 switch (transition) {
1209 case GST_STATE_CHANGE_NULL_TO_READY:
1211 case GST_STATE_CHANGE_READY_TO_PAUSED:
1212 vorbis_info_init (&vd->vi);
1213 vorbis_comment_init (&vd->vc);
1214 vd->initialized = FALSE;
1215 gst_vorbis_dec_reset (vd);
1217 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1223 res = parent_class->change_state (element, transition);
1225 switch (transition) {
1226 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1228 case GST_STATE_CHANGE_PAUSED_TO_READY:
1229 GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
1230 vd->initialized = FALSE;
1231 vorbis_block_clear (&vd->vb);
1232 vorbis_dsp_clear (&vd->vd);
1233 vorbis_comment_clear (&vd->vc);
1234 vorbis_info_clear (&vd->vi);
1235 gst_vorbis_dec_reset (vd);
1237 case GST_STATE_CHANGE_READY_TO_NULL: