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 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_base_init (gpointer g_class)
91 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
92 GstPadTemplate *src_template, *sink_template;
94 src_template = gst_static_pad_template_get (&vorbis_dec_src_factory);
95 gst_element_class_add_pad_template (element_class, src_template);
97 sink_template = gst_static_pad_template_get (&vorbis_dec_sink_factory);
98 gst_element_class_add_pad_template (element_class, sink_template);
100 gst_element_class_set_details_simple (element_class,
101 "Vorbis audio decoder", "Codec/Decoder/Audio",
102 GST_VORBIS_DEC_DESCRIPTION,
103 "Benjamin Otte <otte@gnome.org>, Chris Lord <chris@openedhand.com>");
107 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
109 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
110 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
112 gobject_class->finalize = vorbis_dec_finalize;
114 gstelement_class->change_state = GST_DEBUG_FUNCPTR (vorbis_dec_change_state);
117 static const GstQueryType *
118 vorbis_get_query_types (GstPad * pad)
120 static const GstQueryType vorbis_dec_src_query_types[] = {
127 return vorbis_dec_src_query_types;
131 gst_vorbis_dec_init (GstVorbisDec * dec, GstVorbisDecClass * g_class)
133 dec->sinkpad = gst_pad_new_from_static_template (&vorbis_dec_sink_factory,
136 gst_pad_set_event_function (dec->sinkpad,
137 GST_DEBUG_FUNCPTR (vorbis_dec_sink_event));
138 gst_pad_set_chain_function (dec->sinkpad,
139 GST_DEBUG_FUNCPTR (vorbis_dec_chain));
140 gst_pad_set_query_function (dec->sinkpad,
141 GST_DEBUG_FUNCPTR (vorbis_dec_sink_query));
142 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
144 dec->srcpad = gst_pad_new_from_static_template (&vorbis_dec_src_factory,
147 gst_pad_set_event_function (dec->srcpad,
148 GST_DEBUG_FUNCPTR (vorbis_dec_src_event));
149 gst_pad_set_query_type_function (dec->srcpad,
150 GST_DEBUG_FUNCPTR (vorbis_get_query_types));
151 gst_pad_set_query_function (dec->srcpad,
152 GST_DEBUG_FUNCPTR (vorbis_dec_src_query));
153 gst_pad_use_fixed_caps (dec->srcpad);
154 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
157 dec->pendingevents = NULL;
162 vorbis_dec_finalize (GObject * object)
164 /* Release any possibly allocated libvorbis data.
165 * _clear functions can safely be called multiple times
167 GstVorbisDec *vd = GST_VORBIS_DEC (object);
170 vorbis_block_clear (&vd->vb);
173 vorbis_dsp_clear (&vd->vd);
174 vorbis_comment_clear (&vd->vc);
175 vorbis_info_clear (&vd->vi);
177 G_OBJECT_CLASS (parent_class)->finalize (object);
181 gst_vorbis_dec_reset (GstVorbisDec * dec)
183 dec->last_timestamp = GST_CLOCK_TIME_NONE;
185 dec->seqnum = gst_util_seqnum_next ();
186 gst_segment_init (&dec->segment, GST_FORMAT_TIME);
188 g_list_foreach (dec->queued, (GFunc) gst_mini_object_unref, NULL);
189 g_list_free (dec->queued);
191 g_list_foreach (dec->gather, (GFunc) gst_mini_object_unref, NULL);
192 g_list_free (dec->gather);
194 g_list_foreach (dec->decode, (GFunc) gst_mini_object_unref, NULL);
195 g_list_free (dec->decode);
197 g_list_foreach (dec->pendingevents, (GFunc) gst_mini_object_unref, NULL);
198 g_list_free (dec->pendingevents);
199 dec->pendingevents = NULL;
202 gst_tag_list_free (dec->taglist);
208 vorbis_dec_convert (GstPad * pad,
209 GstFormat src_format, gint64 src_value,
210 GstFormat * dest_format, gint64 * dest_value)
216 if (src_format == *dest_format) {
217 *dest_value = src_value;
221 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
223 if (!dec->initialized)
226 if (dec->sinkpad == pad &&
227 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
230 switch (src_format) {
231 case GST_FORMAT_TIME:
232 switch (*dest_format) {
233 case GST_FORMAT_BYTES:
234 scale = dec->width * dec->vi.channels;
235 case GST_FORMAT_DEFAULT:
237 scale * gst_util_uint64_scale_int (src_value, dec->vi.rate,
244 case GST_FORMAT_DEFAULT:
245 switch (*dest_format) {
246 case GST_FORMAT_BYTES:
247 *dest_value = src_value * dec->width * dec->vi.channels;
249 case GST_FORMAT_TIME:
251 gst_util_uint64_scale_int (src_value, GST_SECOND, dec->vi.rate);
257 case GST_FORMAT_BYTES:
258 switch (*dest_format) {
259 case GST_FORMAT_DEFAULT:
260 *dest_value = src_value / (dec->width * dec->vi.channels);
262 case GST_FORMAT_TIME:
263 *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
264 dec->vi.rate * dec->width * dec->vi.channels);
274 gst_object_unref (dec);
281 GST_DEBUG_OBJECT (dec, "no header packets received");
287 GST_DEBUG_OBJECT (dec, "formats unsupported");
294 vorbis_dec_src_query (GstPad * pad, GstQuery * query)
297 gboolean res = FALSE;
299 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
300 if (G_UNLIKELY (dec == NULL))
303 switch (GST_QUERY_TYPE (query)) {
304 case GST_QUERY_POSITION:
310 gst_query_parse_position (query, &format, NULL);
312 /* we start from the last seen time */
313 time = dec->last_timestamp;
314 /* correct for the segment values */
315 time = gst_segment_to_stream_time (&dec->segment, GST_FORMAT_TIME, time);
318 "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
320 /* and convert to the final format */
322 vorbis_dec_convert (pad, GST_FORMAT_TIME, time, &format, &value)))
325 gst_query_set_position (query, format, value);
328 "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
333 case GST_QUERY_DURATION:
335 res = gst_pad_peer_query (dec->sinkpad, query);
341 case GST_QUERY_CONVERT:
343 GstFormat src_fmt, dest_fmt;
344 gint64 src_val, dest_val;
346 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
348 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
350 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
354 res = gst_pad_query_default (pad, query);
358 gst_object_unref (dec);
365 GST_WARNING_OBJECT (dec, "error handling query");
371 vorbis_dec_sink_query (GstPad * pad, GstQuery * query)
376 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
378 switch (GST_QUERY_TYPE (query)) {
379 case GST_QUERY_CONVERT:
381 GstFormat src_fmt, dest_fmt;
382 gint64 src_val, dest_val;
384 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
386 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
388 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
392 res = gst_pad_query_default (pad, query);
397 gst_object_unref (dec);
404 GST_DEBUG_OBJECT (dec, "error converting value");
410 vorbis_dec_src_event (GstPad * pad, GstEvent * event)
415 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
416 if (G_UNLIKELY (dec == NULL)) {
417 gst_event_unref (event);
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 if (dec->segment.rate < 0.0)
482 vorbis_dec_chain_reverse (dec, TRUE, NULL);
483 ret = gst_pad_push_event (dec->srcpad, event);
485 case GST_EVENT_FLUSH_START:
486 ret = gst_pad_push_event (dec->srcpad, event);
488 case GST_EVENT_FLUSH_STOP:
489 /* here we must clean any state in the decoder */
490 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
491 vorbis_synthesis_restart (&dec->vd);
493 gst_vorbis_dec_reset (dec);
494 ret = gst_pad_push_event (dec->srcpad, event);
496 case GST_EVENT_NEWSEGMENT:
500 gint64 start, stop, time;
503 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
504 &start, &stop, &time);
506 /* we need time for now */
507 if (format != GST_FORMAT_TIME)
508 goto newseg_wrong_format;
510 GST_DEBUG_OBJECT (dec,
511 "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
512 ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
513 update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
514 GST_TIME_ARGS (time));
516 /* now configure the values */
517 gst_segment_set_newsegment_full (&dec->segment, update,
518 rate, arate, format, start, stop, time);
519 dec->seqnum = gst_event_get_seqnum (event);
521 if (dec->initialized)
523 ret = gst_pad_push_event (dec->srcpad, event);
525 /* store it to send once we're initialized */
526 dec->pendingevents = g_list_append (dec->pendingevents, event);
533 if (dec->initialized)
535 ret = gst_pad_push_event (dec->srcpad, event);
537 /* store it to send once we're initialized */
538 dec->pendingevents = g_list_append (dec->pendingevents, event);
544 ret = gst_pad_push_event (dec->srcpad, event);
548 gst_object_unref (dec);
555 GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
561 vorbis_handle_identification_packet (GstVorbisDec * vd)
564 const GstAudioChannelPosition *pos = NULL;
565 gint width = GST_VORBIS_DEC_DEFAULT_SAMPLE_WIDTH;
567 switch (vd->vi.channels) {
578 pos = gst_vorbis_channel_positions[vd->vi.channels - 1];
582 GstAudioChannelPosition *posn =
583 g_new (GstAudioChannelPosition, vd->vi.channels);
585 GST_ELEMENT_WARNING (GST_ELEMENT (vd), STREAM, DECODE,
586 (NULL), ("Using NONE channel layout for more than 8 channels"));
588 for (i = 0; i < vd->vi.channels; i++)
589 posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
595 /* negotiate width with downstream */
596 caps = gst_pad_get_allowed_caps (vd->srcpad);
598 if (!gst_caps_is_empty (caps)) {
601 s = gst_caps_get_structure (caps, 0);
602 /* template ensures 16 or 32 */
603 gst_structure_get_int (s, "width", &width);
605 GST_INFO_OBJECT (vd, "using %s with %d channels and %d bit audio depth",
606 gst_structure_get_name (s), vd->vi.channels, width);
608 gst_caps_unref (caps);
610 vd->width = width >> 3;
612 /* select a copy_samples function, this way we can have specialized versions
613 * for mono/stereo and avoid the depth switch in tremor case */
614 vd->copy_samples = get_copy_sample_func (vd->vi.channels, vd->width);
616 caps = gst_caps_copy (gst_pad_get_pad_template_caps (vd->srcpad));
617 gst_caps_set_simple (caps, "rate", G_TYPE_INT, vd->vi.rate,
618 "channels", G_TYPE_INT, vd->vi.channels,
619 "width", G_TYPE_INT, width, NULL);
622 gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
625 if (vd->vi.channels > 8) {
626 g_free ((GstAudioChannelPosition *) pos);
629 gst_pad_set_caps (vd->srcpad, caps);
630 gst_caps_unref (caps);
636 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
639 gchar *encoder = NULL;
640 GstTagList *list, *old_list;
643 GST_DEBUG_OBJECT (vd, "parsing comment packet");
645 buf = gst_buffer_new ();
646 GST_BUFFER_DATA (buf) = gst_ogg_packet_data (packet);
647 GST_BUFFER_SIZE (buf) = gst_ogg_packet_size (packet);
650 gst_tag_list_from_vorbiscomment_buffer (buf, (guint8 *) "\003vorbis", 7,
653 old_list = vd->taglist;
654 vd->taglist = gst_tag_list_merge (vd->taglist, list, GST_TAG_MERGE_REPLACE);
657 gst_tag_list_free (old_list);
658 gst_tag_list_free (list);
659 gst_buffer_unref (buf);
662 GST_ERROR_OBJECT (vd, "couldn't decode comments");
663 vd->taglist = gst_tag_list_new ();
667 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
668 GST_TAG_ENCODER, encoder, NULL);
671 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
672 GST_TAG_ENCODER_VERSION, vd->vi.version,
673 GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
674 if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
675 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
676 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
677 bitrate = vd->vi.bitrate_nominal;
679 if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
680 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
681 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
683 bitrate = vd->vi.bitrate_upper;
685 if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
686 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
687 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
689 bitrate = vd->vi.bitrate_lower;
692 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
693 GST_TAG_BITRATE, (guint) bitrate, NULL);
696 if (vd->initialized) {
697 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd), vd->srcpad,
701 /* Only post them as messages for the time being. *
702 * They will be pushed on the pad once the decoder is initialized */
703 gst_element_post_message (GST_ELEMENT_CAST (vd),
704 gst_message_new_tag (GST_OBJECT (vd), gst_tag_list_copy (vd->taglist)));
711 vorbis_handle_type_packet (GstVorbisDec * vd)
716 g_assert (vd->initialized == FALSE);
719 if (G_UNLIKELY ((res = vorbis_dsp_init (&vd->vd, &vd->vi))))
720 goto synthesis_init_error;
722 if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
723 goto synthesis_init_error;
725 if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
726 goto block_init_error;
729 vd->initialized = TRUE;
731 if (vd->pendingevents) {
732 for (walk = vd->pendingevents; walk; walk = g_list_next (walk))
733 gst_pad_push_event (vd->srcpad, GST_EVENT_CAST (walk->data));
734 g_list_free (vd->pendingevents);
735 vd->pendingevents = NULL;
739 /* The tags have already been sent on the bus as messages. */
740 gst_pad_push_event (vd->srcpad, gst_event_new_tag (vd->taglist));
746 synthesis_init_error:
748 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
749 (NULL), ("couldn't initialize synthesis (%d)", res));
750 return GST_FLOW_ERROR;
754 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
755 (NULL), ("couldn't initialize block (%d)", res));
756 return GST_FLOW_ERROR;
761 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
766 GST_DEBUG_OBJECT (vd, "parsing header packet");
768 /* Packetno = 0 if the first byte is exactly 0x01 */
769 packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
772 if ((ret = vorbis_dsp_headerin (&vd->vi, &vd->vc, packet)))
774 if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
776 goto header_read_error;
778 switch ((gst_ogg_packet_data (packet))[0]) {
780 res = vorbis_handle_identification_packet (vd);
783 res = vorbis_handle_comment_packet (vd, packet);
786 res = vorbis_handle_type_packet (vd);
790 g_warning ("unknown vorbis header packet found");
799 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
800 (NULL), ("couldn't read header packet (%d)", ret));
801 return GST_FLOW_ERROR;
806 vorbis_dec_push_forward (GstVorbisDec * dec, GstBuffer * buf)
808 GstFlowReturn result;
811 if (!(buf = gst_audio_buffer_clip (buf, &dec->segment, dec->vi.rate,
812 dec->vi.channels * dec->width))) {
813 GST_LOG_OBJECT (dec, "clipped buffer");
818 GST_LOG_OBJECT (dec, "setting DISCONT");
819 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
820 dec->discont = FALSE;
823 GST_DEBUG_OBJECT (dec,
824 "pushing time %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
825 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
826 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
828 result = gst_pad_push (dec->srcpad, buf);
834 vorbis_dec_push_reverse (GstVorbisDec * dec, GstBuffer * buf)
836 GstFlowReturn result = GST_FLOW_OK;
838 dec->queued = g_list_prepend (dec->queued, buf);
844 vorbis_do_timestamps (GstVorbisDec * vd, GstBuffer * buf, gboolean reverse,
845 GstClockTime timestamp, GstClockTime duration)
847 /* interpolate reverse */
848 if (vd->last_timestamp != -1 && duration != -1 && reverse)
849 vd->last_timestamp -= duration;
851 /* take buffer timestamp, use interpolated timestamp otherwise */
853 vd->last_timestamp = timestamp;
855 timestamp = vd->last_timestamp;
857 /* interpolate forwards */
858 if (vd->last_timestamp != -1 && duration != -1 && !reverse)
859 vd->last_timestamp += duration;
862 "keeping timestamp %" GST_TIME_FORMAT " ts %" GST_TIME_FORMAT " dur %"
863 GST_TIME_FORMAT, GST_TIME_ARGS (vd->last_timestamp),
864 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
867 GST_BUFFER_TIMESTAMP (buf) = timestamp;
868 GST_BUFFER_DURATION (buf) = duration;
873 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
874 GstClockTime timestamp, GstClockTime duration)
877 vorbis_sample_t *pcm;
879 vorbis_sample_t **pcm;
882 GstBuffer *out = NULL;
883 GstFlowReturn result;
886 if (G_UNLIKELY (!vd->initialized))
887 goto not_initialized;
889 /* normal data packet */
890 /* FIXME, we can skip decoding if the packet is outside of the
891 * segment, this is however not very trivial as we need a previous
892 * packet to decode the current one so we must be carefull not to
893 * throw away too much. For now we decode everything and clip right
894 * before pushing data. */
897 if (G_UNLIKELY (vorbis_dsp_synthesis (&vd->vd, packet, 1)))
900 if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
903 if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
907 /* assume all goes well here */
908 result = GST_FLOW_OK;
910 /* count samples ready for reading */
912 if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0)
914 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
918 size = sample_count * vd->vi.channels * vd->width;
919 GST_LOG_OBJECT (vd, "%d samples ready for reading, size %d", sample_count,
922 /* alloc buffer for it */
924 gst_pad_alloc_buffer_and_set_caps (vd->srcpad, GST_BUFFER_OFFSET_NONE,
925 size, GST_PAD_CAPS (vd->srcpad), &out);
926 if (G_UNLIKELY (result != GST_FLOW_OK))
929 /* get samples ready for reading now, should be sample_count */
931 pcm = GST_BUFFER_DATA (out);
932 if (G_UNLIKELY ((vorbis_dsp_pcmout (&vd->vd, pcm,
933 sample_count)) != sample_count))
935 if (G_UNLIKELY ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count))
940 /* copy samples in buffer */
941 vd->copy_samples ((vorbis_sample_t *) GST_BUFFER_DATA (out), pcm,
942 sample_count, vd->vi.channels, vd->width);
945 GST_LOG_OBJECT (vd, "setting output size to %d", size);
946 GST_BUFFER_SIZE (out) = size;
948 /* this should not overflow */
950 duration = sample_count * GST_SECOND / vd->vi.rate;
952 vorbis_do_timestamps (vd, out, FALSE, timestamp, duration);
954 if (vd->segment.rate >= 0.0)
955 result = vorbis_dec_push_forward (vd, out);
957 result = vorbis_dec_push_reverse (vd, out);
961 /* no output, still keep track of timestamps */
962 vorbis_do_timestamps (vd, NULL, FALSE, timestamp, duration);
965 vorbis_dsp_read (&vd->vd, sample_count);
967 vorbis_synthesis_read (&vd->vd, sample_count);
975 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
976 (NULL), ("no header sent yet"));
977 return GST_FLOW_ERROR;
981 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
982 (NULL), ("couldn't read data packet"));
983 return GST_FLOW_ERROR;
987 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
988 (NULL), ("vorbis decoder did not accept data packet"));
989 return GST_FLOW_ERROR;
993 gst_buffer_unref (out);
994 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
995 (NULL), ("vorbis decoder reported wrong number of samples"));
996 return GST_FLOW_ERROR;
1000 static GstFlowReturn
1001 vorbis_dec_handle_header_buffer (GstVorbisDec * vd, GstBuffer * buffer)
1004 ogg_packet_wrapper packet_wrapper;
1006 gst_ogg_packet_wrapper_from_buffer (&packet_wrapper, buffer);
1007 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
1009 return vorbis_handle_header_packet (vd, packet);
1013 #define MIN_NUM_HEADERS 3
1014 static GstFlowReturn
1015 vorbis_dec_handle_header_caps (GstVorbisDec * vd, GstBuffer * buffer)
1017 GstFlowReturn result = GST_FLOW_OK;
1018 GstCaps *caps = GST_PAD_CAPS (vd->sinkpad);
1019 GstStructure *s = gst_caps_get_structure (caps, 0);
1020 const GValue *array = gst_structure_get_value (s, "streamheader");
1022 if (array && (gst_value_array_get_size (array) >= MIN_NUM_HEADERS)) {
1023 const GValue *value = NULL;
1024 GstBuffer *buf = NULL;
1026 /* initial header */
1027 value = gst_value_array_get_value (array, 0);
1028 buf = gst_value_get_buffer (value);
1031 result = vorbis_dec_handle_header_buffer (vd, buf);
1033 /* comment header */
1034 if (result == GST_FLOW_OK) {
1035 value = gst_value_array_get_value (array, 1);
1036 buf = gst_value_get_buffer (value);
1039 result = vorbis_dec_handle_header_buffer (vd, buf);
1042 /* bitstream codebook header */
1043 if (result == GST_FLOW_OK) {
1044 value = gst_value_array_get_value (array, 2);
1045 buf = gst_value_get_buffer (value);
1048 result = vorbis_dec_handle_header_buffer (vd, buf);
1054 return (result != GST_FLOW_OK ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_OK);
1058 GST_WARNING_OBJECT (vd, "streamheader array not found");
1059 result = GST_FLOW_ERROR;
1065 GST_WARNING_OBJECT (vd, "streamheader with null buffer received");
1066 result = GST_FLOW_ERROR;
1071 static GstFlowReturn
1072 vorbis_dec_decode_buffer (GstVorbisDec * vd, GstBuffer * buffer)
1075 ogg_packet_wrapper packet_wrapper;
1076 GstFlowReturn result = GST_FLOW_OK;
1078 /* make ogg_packet out of the buffer */
1079 gst_ogg_packet_wrapper_from_buffer (&packet_wrapper, buffer);
1080 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
1081 /* set some more stuff */
1082 packet->granulepos = -1;
1083 packet->packetno = 0; /* we don't care */
1084 /* EOS does not matter, it is used in vorbis to implement clipping the last
1085 * block of samples based on the granulepos. We clip based on segments. */
1088 GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
1090 /* error out on empty header packets, but just skip empty data packets */
1091 if (G_UNLIKELY (packet->bytes == 0)) {
1092 if (vd->initialized)
1098 /* switch depending on packet type */
1099 if ((gst_ogg_packet_data (packet))[0] & 1) {
1100 if (vd->initialized) {
1101 GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
1104 result = vorbis_handle_header_packet (vd, packet);
1106 GstClockTime timestamp, duration;
1108 /* try to find header in caps so we can initialize the decoder */
1109 if (!vd->initialized) {
1110 result = vorbis_dec_handle_header_caps (vd, buffer);
1111 if (result != GST_FLOW_OK)
1112 goto invalid_caps_header;
1115 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1116 duration = GST_BUFFER_DURATION (buffer);
1118 result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
1126 /* don't error out here, just ignore the buffer, it's invalid for vorbis
1128 GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
1129 result = GST_FLOW_OK;
1136 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
1137 result = GST_FLOW_ERROR;
1142 invalid_caps_header:
1144 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL),
1145 ("invalid streamheader in caps"));
1152 * Buffer decoding order: 7 8 9 4 5 6 3 1 2 EOS
1153 * Discont flag: D D D D
1155 * - Each Discont marks a discont in the decoding order.
1157 * for vorbis, each buffer is a keyframe when we have the previous
1158 * buffer. This means that to decode buffer 7, we need buffer 6, which
1159 * arrives out of order.
1161 * we first gather buffers in the gather queue until we get a DISCONT. We
1162 * prepend each incomming buffer so that they are in reversed order.
1164 * gather queue: 9 8 7
1168 * When a DISCONT is received (buffer 4), we move the gather queue to the
1169 * decode queue. This is simply done be taking the head of the gather queue
1170 * and prepending it to the decode queue. This yields:
1173 * decode queue: 7 8 9
1176 * Then we decode each buffer in the decode queue in order and put the output
1177 * buffer in the output queue. The first buffer (7) will not produce any output
1178 * because it needs the previous buffer (6) which did not arrive yet. This
1182 * decode queue: 7 8 9
1185 * Then we remove the consumed buffers from the decode queue. Buffer 7 is not
1186 * completely consumed, we need to keep it around for when we receive buffer
1193 * Then we accumulate more buffers:
1195 * gather queue: 6 5 4
1199 * prepending to the decode queue on DISCONT yields:
1202 * decode queue: 4 5 6 7
1205 * after decoding and keeping buffer 4:
1209 * output queue: 7 6 5
1213 static GstFlowReturn
1214 vorbis_dec_flush_decode (GstVorbisDec * dec)
1216 GstFlowReturn res = GST_FLOW_OK;
1221 GST_DEBUG_OBJECT (dec, "flushing buffers to decoder");
1225 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1227 GST_DEBUG_OBJECT (dec, "decoding buffer %p, ts %" GST_TIME_FORMAT,
1228 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1230 next = g_list_next (walk);
1232 /* decode buffer, prepend to output queue */
1233 res = vorbis_dec_decode_buffer (dec, buf);
1235 /* if we generated output, we can discard the buffer, else we
1236 * keep it in the queue */
1238 GST_DEBUG_OBJECT (dec, "decoded buffer to %p", dec->queued->data);
1239 dec->decode = g_list_delete_link (dec->decode, walk);
1240 gst_buffer_unref (buf);
1242 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1246 while (dec->queued) {
1247 GstBuffer *buf = GST_BUFFER_CAST (dec->queued->data);
1248 GstClockTime timestamp, duration;
1250 timestamp = GST_BUFFER_TIMESTAMP (buf);
1251 duration = GST_BUFFER_DURATION (buf);
1253 vorbis_do_timestamps (dec, buf, TRUE, timestamp, duration);
1254 res = vorbis_dec_push_forward (dec, buf);
1256 dec->queued = g_list_delete_link (dec->queued, dec->queued);
1261 static GstFlowReturn
1262 vorbis_dec_chain_reverse (GstVorbisDec * vd, gboolean discont, GstBuffer * buf)
1264 GstFlowReturn result = GST_FLOW_OK;
1266 /* if we have a discont, move buffers to the decode list */
1267 if (G_UNLIKELY (discont)) {
1268 GST_DEBUG_OBJECT (vd, "received discont");
1269 while (vd->gather) {
1272 gbuf = GST_BUFFER_CAST (vd->gather->data);
1273 /* remove from the gather list */
1274 vd->gather = g_list_delete_link (vd->gather, vd->gather);
1275 /* copy to decode queue */
1276 vd->decode = g_list_prepend (vd->decode, gbuf);
1278 /* flush and decode the decode queue */
1279 result = vorbis_dec_flush_decode (vd);
1282 if (G_LIKELY (buf)) {
1283 GST_DEBUG_OBJECT (vd,
1284 "gathering buffer %p of size %u, time %" GST_TIME_FORMAT
1285 ", dur %" GST_TIME_FORMAT, buf, GST_BUFFER_SIZE (buf),
1286 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1287 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1289 /* add buffer to gather queue */
1290 vd->gather = g_list_prepend (vd->gather, buf);
1296 static GstFlowReturn
1297 vorbis_dec_chain_forward (GstVorbisDec * vd, gboolean discont,
1300 GstFlowReturn result;
1302 result = vorbis_dec_decode_buffer (vd, buffer);
1304 gst_buffer_unref (buffer);
1309 static GstFlowReturn
1310 vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
1313 GstFlowReturn result = GST_FLOW_OK;
1316 vd = GST_VORBIS_DEC (gst_pad_get_parent (pad));
1318 discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1320 /* resync on DISCONT */
1321 if (G_UNLIKELY (discont)) {
1322 GST_DEBUG_OBJECT (vd, "received DISCONT buffer");
1323 vd->last_timestamp = GST_CLOCK_TIME_NONE;
1324 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
1325 vorbis_synthesis_restart (&vd->vd);
1330 if (vd->segment.rate >= 0.0)
1331 result = vorbis_dec_chain_forward (vd, discont, buffer);
1333 result = vorbis_dec_chain_reverse (vd, discont, buffer);
1335 gst_object_unref (vd);
1340 static GstStateChangeReturn
1341 vorbis_dec_change_state (GstElement * element, GstStateChange transition)
1343 GstVorbisDec *vd = GST_VORBIS_DEC (element);
1344 GstStateChangeReturn res;
1346 switch (transition) {
1347 case GST_STATE_CHANGE_NULL_TO_READY:
1349 case GST_STATE_CHANGE_READY_TO_PAUSED:
1350 vorbis_info_init (&vd->vi);
1351 vorbis_comment_init (&vd->vc);
1352 vd->initialized = FALSE;
1353 gst_vorbis_dec_reset (vd);
1355 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1361 res = parent_class->change_state (element, transition);
1363 switch (transition) {
1364 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1366 case GST_STATE_CHANGE_PAUSED_TO_READY:
1367 GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
1368 vd->initialized = FALSE;
1371 vorbis_block_clear (&vd->vb);
1374 vorbis_dsp_clear (&vd->vd);
1375 vorbis_comment_clear (&vd->vc);
1376 vorbis_info_clear (&vd->vi);
1377 gst_vorbis_dec_reset (vd);
1379 case GST_STATE_CHANGE_READY_TO_NULL: