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 GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug);
50 #define GST_CAT_DEFAULT vorbisdec_debug
52 static const GstElementDetails vorbis_dec_details =
53 GST_ELEMENT_DETAILS ("Vorbis audio decoder",
54 "Codec/Decoder/Audio",
55 "decode raw vorbis streams to float audio",
56 "Benjamin Otte <in7y118@public.uni-hamburg.de>");
58 static GstStaticPadTemplate vorbis_dec_src_factory =
59 GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("audio/x-raw-float, "
63 "rate = (int) [ 1, MAX ], "
64 "channels = (int) [ 1, 256 ], " "endianness = (int) BYTE_ORDER, "
65 /* no ifdef in macros, please
66 #ifdef GST_VORBIS_DEC_SEQUENTIAL
67 "layout = \"sequential\", "
73 static GstStaticPadTemplate vorbis_dec_sink_factory =
74 GST_STATIC_PAD_TEMPLATE ("sink",
77 GST_STATIC_CAPS ("audio/x-vorbis")
80 GST_BOILERPLATE (GstVorbisDec, gst_vorbis_dec, GstElement, GST_TYPE_ELEMENT);
82 static void vorbis_dec_finalize (GObject * object);
83 static gboolean vorbis_dec_sink_event (GstPad * pad, GstEvent * event);
84 static GstFlowReturn vorbis_dec_chain (GstPad * pad, GstBuffer * buffer);
85 static GstFlowReturn vorbis_dec_chain_forward (GstVorbisDec * vd,
86 gboolean discont, GstBuffer * buffer);
87 static GstStateChangeReturn vorbis_dec_change_state (GstElement * element,
88 GstStateChange transition);
90 static gboolean vorbis_dec_src_event (GstPad * pad, GstEvent * event);
91 static gboolean vorbis_dec_src_query (GstPad * pad, GstQuery * query);
92 static gboolean vorbis_dec_convert (GstPad * pad,
93 GstFormat src_format, gint64 src_value,
94 GstFormat * dest_format, gint64 * dest_value);
96 static gboolean vorbis_dec_sink_query (GstPad * pad, GstQuery * query);
99 gst_vorbis_dec_base_init (gpointer g_class)
101 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
102 GstPadTemplate *src_template, *sink_template;
104 src_template = gst_static_pad_template_get (&vorbis_dec_src_factory);
105 gst_element_class_add_pad_template (element_class, src_template);
107 sink_template = gst_static_pad_template_get (&vorbis_dec_sink_factory);
108 gst_element_class_add_pad_template (element_class, sink_template);
110 gst_element_class_set_details (element_class, &vorbis_dec_details);
114 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
116 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
117 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
119 gobject_class->finalize = vorbis_dec_finalize;
121 gstelement_class->change_state = GST_DEBUG_FUNCPTR (vorbis_dec_change_state);
124 static const GstQueryType *
125 vorbis_get_query_types (GstPad * pad)
127 static const GstQueryType vorbis_dec_src_query_types[] = {
134 return vorbis_dec_src_query_types;
138 gst_vorbis_dec_init (GstVorbisDec * dec, GstVorbisDecClass * g_class)
140 dec->sinkpad = gst_pad_new_from_static_template (&vorbis_dec_sink_factory,
143 gst_pad_set_event_function (dec->sinkpad,
144 GST_DEBUG_FUNCPTR (vorbis_dec_sink_event));
145 gst_pad_set_chain_function (dec->sinkpad,
146 GST_DEBUG_FUNCPTR (vorbis_dec_chain));
147 gst_pad_set_query_function (dec->sinkpad,
148 GST_DEBUG_FUNCPTR (vorbis_dec_sink_query));
149 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
151 dec->srcpad = gst_pad_new_from_static_template (&vorbis_dec_src_factory,
154 gst_pad_set_event_function (dec->srcpad,
155 GST_DEBUG_FUNCPTR (vorbis_dec_src_event));
156 gst_pad_set_query_type_function (dec->srcpad,
157 GST_DEBUG_FUNCPTR (vorbis_get_query_types));
158 gst_pad_set_query_function (dec->srcpad,
159 GST_DEBUG_FUNCPTR (vorbis_dec_src_query));
160 gst_pad_use_fixed_caps (dec->srcpad);
161 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
164 dec->pendingevents = NULL;
169 vorbis_dec_finalize (GObject * object)
171 /* Release any possibly allocated libvorbis data.
172 * _clear functions can safely be called multiple times
174 GstVorbisDec *vd = GST_VORBIS_DEC (object);
176 vorbis_block_clear (&vd->vb);
177 vorbis_dsp_clear (&vd->vd);
178 vorbis_comment_clear (&vd->vc);
179 vorbis_info_clear (&vd->vi);
181 G_OBJECT_CLASS (parent_class)->finalize (object);
185 gst_vorbis_dec_reset (GstVorbisDec * dec)
187 dec->cur_timestamp = GST_CLOCK_TIME_NONE;
188 dec->prev_timestamp = GST_CLOCK_TIME_NONE;
189 dec->granulepos = -1;
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 = sizeof (float) * 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 * sizeof (float) * 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 / (sizeof (float) * dec->vi.channels);
268 case GST_FORMAT_TIME:
269 *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
270 dec->vi.rate * sizeof (float) * 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:
310 gint64 granulepos, value;
311 GstFormat my_format, format;
314 /* we start from the last seen granulepos */
315 granulepos = dec->granulepos;
317 gst_query_parse_position (query, &format, NULL);
319 /* and convert to the final format in two steps with time as the
320 * intermediate step */
321 my_format = GST_FORMAT_TIME;
323 vorbis_dec_convert (pad, GST_FORMAT_DEFAULT, granulepos,
327 /* correct for the segment values */
328 time = gst_segment_to_stream_time (&dec->segment, GST_FORMAT_TIME, time);
331 "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
333 /* and convert to the final format */
334 if (!(res = vorbis_dec_convert (pad, my_format, time, &format, &value)))
337 gst_query_set_position (query, format, value);
340 "query %p: we return %lld (format %u)", query, value, format);
344 case GST_QUERY_DURATION:
348 if (!(peer = gst_pad_get_peer (dec->sinkpad))) {
349 GST_WARNING_OBJECT (dec, "sink pad %" GST_PTR_FORMAT " is not linked",
354 res = gst_pad_query (peer, query);
355 gst_object_unref (peer);
361 case GST_QUERY_CONVERT:
363 GstFormat src_fmt, dest_fmt;
364 gint64 src_val, dest_val;
366 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
368 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
370 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
374 res = gst_pad_query_default (pad, query);
378 gst_object_unref (dec);
385 GST_WARNING_OBJECT (dec, "error handling query");
391 vorbis_dec_sink_query (GstPad * pad, GstQuery * query)
396 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
398 switch (GST_QUERY_TYPE (query)) {
399 case GST_QUERY_CONVERT:
401 GstFormat src_fmt, dest_fmt;
402 gint64 src_val, dest_val;
404 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
406 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
408 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
412 res = gst_pad_query_default (pad, query);
417 gst_object_unref (dec);
424 GST_DEBUG_OBJECT (dec, "error converting value");
430 vorbis_dec_src_event (GstPad * pad, GstEvent * event)
435 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
437 switch (GST_EVENT_TYPE (event)) {
440 GstFormat format, tformat;
444 GstSeekType cur_type, stop_type;
449 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
451 seqnum = gst_event_get_seqnum (event);
452 gst_event_unref (event);
454 /* we have to ask our peer to seek to time here as we know
455 * nothing about how to generate a granulepos from the src
456 * formats or anything.
458 * First bring the requested format to time
460 tformat = GST_FORMAT_TIME;
461 if (!(res = vorbis_dec_convert (pad, format, cur, &tformat, &tcur)))
463 if (!(res = vorbis_dec_convert (pad, format, stop, &tformat, &tstop)))
466 /* then seek with time on the peer */
467 real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
468 flags, cur_type, tcur, stop_type, tstop);
469 gst_event_set_seqnum (real_seek, seqnum);
471 res = gst_pad_push_event (dec->sinkpad, real_seek);
475 res = gst_pad_push_event (dec->sinkpad, event);
479 gst_object_unref (dec);
486 GST_DEBUG_OBJECT (dec, "cannot convert start/stop for seek");
492 vorbis_dec_sink_event (GstPad * pad, GstEvent * event)
494 gboolean ret = FALSE;
497 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
499 GST_LOG_OBJECT (dec, "handling event");
500 switch (GST_EVENT_TYPE (event)) {
502 ret = gst_pad_push_event (dec->srcpad, event);
504 case GST_EVENT_FLUSH_START:
505 ret = gst_pad_push_event (dec->srcpad, event);
507 case GST_EVENT_FLUSH_STOP:
508 /* here we must clean any state in the decoder */
509 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
510 vorbis_synthesis_restart (&dec->vd);
512 gst_vorbis_dec_reset (dec);
513 ret = gst_pad_push_event (dec->srcpad, event);
515 case GST_EVENT_NEWSEGMENT:
519 gint64 start, stop, time;
522 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
523 &start, &stop, &time);
525 /* we need time for now */
526 if (format != GST_FORMAT_TIME)
527 goto newseg_wrong_format;
529 GST_DEBUG_OBJECT (dec,
530 "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
531 ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
532 update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
533 GST_TIME_ARGS (time));
535 /* now configure the values */
536 gst_segment_set_newsegment_full (&dec->segment, update,
537 rate, arate, format, start, stop, time);
538 dec->seqnum = gst_event_get_seqnum (event);
540 if (dec->initialized)
542 ret = gst_pad_push_event (dec->srcpad, event);
544 /* store it to send once we're initialized */
545 dec->pendingevents = g_list_append (dec->pendingevents, event);
551 ret = gst_pad_push_event (dec->srcpad, event);
555 gst_object_unref (dec);
562 GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
568 vorbis_handle_identification_packet (GstVorbisDec * vd)
571 const GstAudioChannelPosition *pos = NULL;
573 switch (vd->vi.channels) {
579 static const GstAudioChannelPosition pos3[] = {
580 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
581 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
582 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
588 static const GstAudioChannelPosition pos4[] = {
589 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
590 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
591 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
592 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
598 static const GstAudioChannelPosition pos5[] = {
599 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
600 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
601 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
602 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
603 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
609 static const GstAudioChannelPosition pos6[] = {
610 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
611 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
612 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
613 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
614 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
615 GST_AUDIO_CHANNEL_POSITION_LFE
620 /* FIXME: for >6 channels the layout is not defined by the Vorbis
621 * spec. These are the gstreamer "defaults" for 7/8 channels and
622 * NONE layouts for more channels
625 static const GstAudioChannelPosition pos7[] = {
626 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
627 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
628 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
629 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
630 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
631 GST_AUDIO_CHANNEL_POSITION_LFE,
632 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
638 static const GstAudioChannelPosition pos8[] = {
639 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
640 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
641 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
642 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
643 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
644 GST_AUDIO_CHANNEL_POSITION_LFE,
645 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
646 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
654 GstAudioChannelPosition *posn =
655 g_new (GstAudioChannelPosition, vd->vi.channels);
657 GST_ELEMENT_WARNING (GST_ELEMENT (vd), STREAM, DECODE,
658 (NULL), ("Using NONE channel layout for more than 8 channels"));
660 for (i = 0; i < vd->vi.channels; i++)
661 posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
667 caps = gst_caps_new_simple ("audio/x-raw-float",
668 "rate", G_TYPE_INT, vd->vi.rate,
669 "channels", G_TYPE_INT, vd->vi.channels,
670 "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
673 gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
676 if (vd->vi.channels > 8) {
677 g_free ((GstAudioChannelPosition *) pos);
680 gst_pad_set_caps (vd->srcpad, caps);
681 gst_caps_unref (caps);
687 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
690 gchar *encoder = NULL;
691 GstTagList *list, *old_list;
694 GST_DEBUG_OBJECT (vd, "parsing comment packet");
696 buf = gst_buffer_new ();
697 GST_BUFFER_DATA (buf) = packet->packet;
698 GST_BUFFER_SIZE (buf) = packet->bytes;
701 gst_tag_list_from_vorbiscomment_buffer (buf, (guint8 *) "\003vorbis", 7,
704 old_list = vd->taglist;
705 vd->taglist = gst_tag_list_merge (vd->taglist, list, GST_TAG_MERGE_REPLACE);
708 gst_tag_list_free (old_list);
709 gst_tag_list_free (list);
710 gst_buffer_unref (buf);
713 GST_ERROR_OBJECT (vd, "couldn't decode comments");
714 vd->taglist = gst_tag_list_new ();
717 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
718 GST_TAG_ENCODER, encoder, NULL);
721 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
722 GST_TAG_ENCODER_VERSION, vd->vi.version,
723 GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
724 if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
725 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
726 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
727 bitrate = vd->vi.bitrate_nominal;
729 if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
730 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
731 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
733 bitrate = vd->vi.bitrate_upper;
735 if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
736 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
737 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
739 bitrate = vd->vi.bitrate_lower;
742 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
743 GST_TAG_BITRATE, (guint) bitrate, NULL);
746 if (vd->initialized) {
747 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd), vd->srcpad,
751 /* Only post them as messages for the time being. *
752 * They will be pushed on the pad once the decoder is initialized */
753 gst_element_post_message (GST_ELEMENT_CAST (vd),
754 gst_message_new_tag (GST_OBJECT (vd), gst_tag_list_copy (vd->taglist)));
761 vorbis_handle_type_packet (GstVorbisDec * vd)
766 g_assert (vd->initialized == FALSE);
768 if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
769 goto synthesis_init_error;
771 if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
772 goto block_init_error;
774 vd->initialized = TRUE;
776 if (vd->pendingevents) {
777 for (walk = vd->pendingevents; walk; walk = g_list_next (walk))
778 gst_pad_push_event (vd->srcpad, GST_EVENT_CAST (walk->data));
779 g_list_free (vd->pendingevents);
780 vd->pendingevents = NULL;
784 /* The tags have already been sent on the bus as messages. */
785 gst_pad_push_event (vd->srcpad, gst_event_new_tag (vd->taglist));
791 synthesis_init_error:
793 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
794 (NULL), ("couldn't initialize synthesis (%d)", res));
795 return GST_FLOW_ERROR;
799 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
800 (NULL), ("couldn't initialize block (%d)", res));
801 return GST_FLOW_ERROR;
806 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
811 GST_DEBUG_OBJECT (vd, "parsing header packet");
813 /* Packetno = 0 if the first byte is exactly 0x01 */
814 packet->b_o_s = (packet->packet[0] == 0x1) ? 1 : 0;
816 if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
817 goto header_read_error;
819 switch (packet->packet[0]) {
821 res = vorbis_handle_identification_packet (vd);
824 res = vorbis_handle_comment_packet (vd, packet);
827 res = vorbis_handle_type_packet (vd);
831 g_warning ("unknown vorbis header packet found");
840 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
841 (NULL), ("couldn't read header packet (%d)", ret));
842 return GST_FLOW_ERROR;
846 /* These samples can be outside of the float -1.0 -- 1.0 range, this
847 * is allowed, downstream elements are supposed to clip */
849 copy_samples (float *out, float **in, guint samples, gint channels)
853 #ifdef GST_VORBIS_DEC_SEQUENTIAL
854 for (i = 0; i < channels; i++) {
855 memcpy (out, in[i], samples * sizeof (float));
859 for (j = 0; j < samples; j++) {
860 for (i = 0; i < channels; i++) {
868 vorbis_dec_push_forward (GstVorbisDec * dec, GstBuffer * buf)
870 GstFlowReturn result;
871 gint64 outoffset, origoffset;
873 origoffset = GST_BUFFER_OFFSET (buf);
876 outoffset = origoffset;
878 if (outoffset == -1) {
879 dec->queued = g_list_append (dec->queued, buf);
880 GST_DEBUG_OBJECT (dec, "queued buffer");
881 result = GST_FLOW_OK;
883 if (G_UNLIKELY (dec->queued)) {
888 GST_DEBUG_OBJECT (dec, "first buffer with offset %lld", outoffset);
889 ts = gst_util_uint64_scale_int (outoffset, GST_SECOND, dec->vi.rate);
891 size = g_list_length (dec->queued);
892 /* we walk the queued up list in reverse, and set the buffer fields
893 * calculating backwards */
894 for (walk = g_list_last (dec->queued); walk;
895 walk = g_list_previous (walk)) {
896 GstBuffer *buffer = GST_BUFFER (walk->data);
899 offset = GST_BUFFER_SIZE (buffer) / (sizeof (float) * dec->vi.channels);
901 if (outoffset >= offset)
904 /* we can't go below 0, this means this first offset was at the eos
905 * page and we need to clip to it instead */
906 GST_DEBUG_OBJECT (dec, "clipping %" G_GINT64_FORMAT,
908 origoffset += (offset - outoffset);
912 GST_BUFFER_OFFSET (buffer) = outoffset;
913 GST_BUFFER_TIMESTAMP (buffer) =
914 gst_util_uint64_scale_int (outoffset, GST_SECOND, dec->vi.rate);
915 GST_BUFFER_DURATION (buffer) = GST_CLOCK_DIFF (GST_BUFFER_TIMESTAMP
917 ts = GST_BUFFER_TIMESTAMP (buffer);
918 GST_DEBUG_OBJECT (dec, "patch buffer %u, offset %" G_GUINT64_FORMAT
919 ", timestamp %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
921 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
922 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
925 for (walk = dec->queued; walk; walk = g_list_next (walk)) {
926 GstBuffer *buffer = GST_BUFFER (walk->data);
928 /* clips to the configured segment, or returns NULL with buffer
929 * unreffed when the input buffer is completely outside the segment */
930 if (!(buffer = gst_audio_buffer_clip (buffer, &dec->segment,
931 dec->vi.rate, dec->vi.channels * sizeof (float))))
935 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
936 dec->discont = FALSE;
938 /* ignore the result */
939 gst_pad_push (dec->srcpad, buffer);
941 g_list_free (dec->queued);
946 if (!(buf = gst_audio_buffer_clip (buf, &dec->segment, dec->vi.rate,
947 dec->vi.channels * sizeof (float))))
951 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
952 dec->discont = FALSE;
955 result = gst_pad_push (dec->srcpad, buf);
962 vorbis_dec_push_reverse (GstVorbisDec * dec, GstBuffer * buf)
964 GstFlowReturn result = GST_FLOW_OK;
966 dec->queued = g_list_prepend (dec->queued, buf);
972 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet)
977 GstFlowReturn result;
978 GstClockTime timestamp = GST_CLOCK_TIME_NONE, nextts;
981 if (!vd->initialized)
982 goto not_initialized;
984 /* FIXME, we should queue undecoded packets here until we get
985 * a timestamp, then we reverse timestamp the queued packets and
986 * clip them, then we decode only the ones we want and don't
987 * keep decoded data in memory.
988 * Ideally, of course, the demuxer gives us a valid timestamp on
992 /* normal data packet */
993 /* FIXME, we can skip decoding if the packet is outside of the
994 * segment, this is however not very trivial as we need a previous
995 * packet to decode the current one so we must be carefull not to
996 * throw away too much. For now we decode everything and clip right
997 * before pushing data. */
998 if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
1001 if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
1004 /* assume all goes well here */
1005 result = GST_FLOW_OK;
1007 /* count samples ready for reading */
1008 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
1011 GST_LOG_OBJECT (vd, "%d samples ready for reading", sample_count);
1012 size = sample_count * vd->vi.channels * sizeof (float);
1014 /* alloc buffer for it */
1016 gst_pad_alloc_buffer_and_set_caps (vd->srcpad, GST_BUFFER_OFFSET_NONE,
1017 size, GST_PAD_CAPS (vd->srcpad), &out);
1018 if (G_UNLIKELY (result != GST_FLOW_OK))
1021 /* get samples ready for reading now, should be sample_count */
1022 if (G_UNLIKELY ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count))
1025 /* copy samples in buffer */
1026 copy_samples ((float *) GST_BUFFER_DATA (out), pcm, sample_count,
1029 GST_BUFFER_SIZE (out) = size;
1031 /* this should not overflow */
1032 GST_BUFFER_DURATION (out) = sample_count * GST_SECOND / vd->vi.rate;
1034 if (packet->granulepos != -1)
1035 vd->granulepos = packet->granulepos - sample_count;
1037 if (vd->cur_timestamp != GST_CLOCK_TIME_NONE) {
1038 /* we have incoming timestamps */
1039 timestamp = vd->cur_timestamp;
1040 GST_DEBUG_OBJECT (vd,
1041 "cur_timestamp: %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT " = %"
1042 GST_TIME_FORMAT, GST_TIME_ARGS (vd->cur_timestamp),
1043 GST_TIME_ARGS (GST_BUFFER_DURATION (out)),
1044 GST_TIME_ARGS (vd->cur_timestamp + GST_BUFFER_DURATION (out)));
1045 vd->cur_timestamp += GST_BUFFER_DURATION (out);
1046 GST_BUFFER_OFFSET (out) = GST_CLOCK_TIME_TO_FRAMES (timestamp, vd->vi.rate);
1047 GST_BUFFER_OFFSET_END (out) = GST_BUFFER_OFFSET (out) + sample_count;
1049 /* we have incoming granulepos */
1050 GST_BUFFER_OFFSET (out) = vd->granulepos;
1051 if (vd->granulepos != -1) {
1052 GST_DEBUG_OBJECT (vd, "granulepos: %" G_GINT64_FORMAT, vd->granulepos);
1053 GST_BUFFER_OFFSET_END (out) = vd->granulepos + sample_count;
1055 gst_util_uint64_scale_int (vd->granulepos, GST_SECOND, vd->vi.rate);
1057 gst_util_uint64_scale_int (vd->granulepos + sample_count,
1058 GST_SECOND, vd->vi.rate);
1059 GST_DEBUG_OBJECT (vd, "corresponding timestamp %" GST_TIME_FORMAT,
1060 GST_TIME_ARGS (timestamp));
1061 /* calculate a nano-second accurate duration */
1062 GST_BUFFER_DURATION (out) = GST_CLOCK_DIFF (timestamp, nextts);
1063 GST_DEBUG_OBJECT (vd, "set duration %" GST_TIME_FORMAT,
1064 GST_TIME_ARGS (GST_BUFFER_DURATION (out)));
1069 GST_BUFFER_TIMESTAMP (out) = timestamp;
1071 if (vd->granulepos != -1)
1072 vd->granulepos += sample_count;
1074 if (vd->segment.rate >= 0.0)
1075 result = vorbis_dec_push_forward (vd, out);
1077 result = vorbis_dec_push_reverse (vd, out);
1080 vorbis_synthesis_read (&vd->vd, sample_count);
1082 GST_DEBUG_OBJECT (vd,
1083 "decoded %ld bytes into %d samples, ts %" GST_TIME_FORMAT, packet->bytes,
1084 sample_count, GST_TIME_ARGS (timestamp));
1086 /* granulepos is the last sample in the packet */
1087 if (packet->granulepos != -1)
1088 vd->granulepos = packet->granulepos;
1095 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
1096 (NULL), ("no header sent yet"));
1097 return GST_FLOW_ERROR;
1101 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
1102 (NULL), ("couldn't read data packet"));
1103 return GST_FLOW_ERROR;
1107 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
1108 (NULL), ("vorbis decoder did not accept data packet"));
1109 return GST_FLOW_ERROR;
1113 gst_buffer_unref (out);
1114 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
1115 (NULL), ("vorbis decoder reported wrong number of samples"));
1116 return GST_FLOW_ERROR;
1120 static GstFlowReturn
1121 vorbis_dec_decode_buffer (GstVorbisDec * vd, GstBuffer * buffer)
1124 GstFlowReturn result = GST_FLOW_OK;
1125 GstClockTime timestamp;
1128 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1129 offset_end = GST_BUFFER_OFFSET_END (buffer);
1131 /* only ogg has granulepos, demuxers of other container formats
1132 * might provide us with timestamps instead (e.g. matroskademux) */
1133 if (offset_end == GST_BUFFER_OFFSET_NONE && timestamp != GST_CLOCK_TIME_NONE) {
1134 /* we might get multiple consecutive buffers with the same timestamp */
1135 if (timestamp != vd->prev_timestamp) {
1136 vd->cur_timestamp = timestamp;
1137 vd->prev_timestamp = timestamp;
1140 vd->cur_timestamp = GST_CLOCK_TIME_NONE;
1141 vd->prev_timestamp = GST_CLOCK_TIME_NONE;
1144 /* make ogg_packet out of the buffer */
1145 packet.packet = GST_BUFFER_DATA (buffer);
1146 packet.bytes = GST_BUFFER_SIZE (buffer);
1147 packet.granulepos = offset_end;
1148 packet.packetno = 0; /* we don't care */
1150 * FIXME. Is there anyway to know that this is the last packet and
1152 * Yes there is, keep one packet at all times and only push out when
1153 * you receive a new one. Implement this.
1157 /* error out on empty header packets, but just skip empty data packets */
1158 if (G_UNLIKELY (packet.bytes == 0)) {
1159 if (vd->initialized)
1165 GST_DEBUG_OBJECT (vd, "vorbis granule: %" G_GINT64_FORMAT,
1166 (gint64) packet.granulepos);
1168 /* switch depending on packet type */
1169 if (packet.packet[0] & 1) {
1170 if (vd->initialized) {
1171 GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
1174 result = vorbis_handle_header_packet (vd, &packet);
1176 result = vorbis_handle_data_packet (vd, &packet);
1184 /* don't error out here, just ignore the buffer, it's invalid for vorbis
1186 GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
1187 if (packet.granulepos != -1)
1188 vd->granulepos = packet.granulepos;
1189 result = GST_FLOW_OK;
1196 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
1197 result = GST_FLOW_ERROR;
1205 * Buffer decoding order: 7 8 9 4 5 6 3 1 2 EOS
1206 * Discont flag: D D D D
1208 * - Each Discont marks a discont in the decoding order.
1210 * for vorbis, each buffer is a keyframe when we have the previous
1211 * buffer. This means that to decode buffer 7, we need buffer 6, which
1212 * arrives out of order.
1214 * we first gather buffers in the gather queue until we get a DISCONT. We
1215 * prepend each incomming buffer so that they are in reversed order.
1217 * gather queue: 9 8 7
1221 * When a DISCONT is received (buffer 4), we move the gather queue to the
1222 * decode queue. This is simply done be taking the head of the gather queue
1223 * and prepending it to the decode queue. This yields:
1226 * decode queue: 7 8 9
1229 * Then we decode each buffer in the decode queue in order and put the output
1230 * buffer in the output queue. The first buffer (7) will not produce any output
1231 * because it needs the previous buffer (6) which did not arrive yet. This
1235 * decode queue: 7 8 9
1238 * Then we remove the consumed buffers from the decode queue. Buffer 7 is not
1239 * completely consumed, we need to keep it around for when we receive buffer
1246 * Then we accumulate more buffers:
1248 * gather queue: 6 5 4
1252 * prepending to the decode queue on DISCONT yields:
1255 * decode queue: 4 5 6 7
1258 * after decoding and keeping buffer 4:
1262 * output queue: 7 6 5
1266 static GstFlowReturn
1267 vorbis_dec_flush_decode (GstVorbisDec * dec)
1269 GstFlowReturn res = GST_FLOW_OK;
1274 GST_DEBUG_OBJECT (dec, "flushing buffers to decoder");
1278 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1280 GST_DEBUG_OBJECT (dec, "decoding buffer %p, ts %" GST_TIME_FORMAT,
1281 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1283 next = g_list_next (walk);
1285 /* decode buffer, prepend to output queue */
1286 res = vorbis_dec_decode_buffer (dec, buf);
1288 /* if we generated output, we can discard the buffer, else we
1289 * keep it in the queue */
1291 GST_DEBUG_OBJECT (dec, "decoded buffer to %p", dec->queued->data);
1292 dec->decode = g_list_delete_link (dec->decode, walk);
1293 gst_buffer_unref (buf);
1295 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1299 if (dec->granulepos != -1) {
1303 gst_util_uint64_scale_int (dec->granulepos, GST_SECOND, dec->vi.rate);
1305 GST_DEBUG_OBJECT (dec, "we have granulepos %" G_GUINT64_FORMAT ", ts %"
1306 GST_TIME_FORMAT, dec->granulepos, GST_TIME_ARGS (endts));
1308 while (dec->queued) {
1312 buf = GST_BUFFER_CAST (dec->queued->data);
1315 GST_BUFFER_SIZE (buf) / (dec->vi.channels * sizeof (float));
1317 GST_BUFFER_OFFSET_END (buf) = dec->granulepos;
1319 gst_util_uint64_scale_int (dec->granulepos, GST_SECOND, dec->vi.rate);
1320 dec->granulepos -= sample_count;
1321 GST_BUFFER_OFFSET (buf) = dec->granulepos;
1322 GST_BUFFER_TIMESTAMP (buf) =
1323 gst_util_uint64_scale_int (dec->granulepos, GST_SECOND, dec->vi.rate);
1324 GST_BUFFER_DURATION (buf) = endts - GST_BUFFER_TIMESTAMP (buf);
1326 /* clip, this will unref the buffer in case of clipping */
1327 if (!(buf = gst_audio_buffer_clip (buf, &dec->segment, dec->vi.rate,
1328 dec->vi.channels * sizeof (float)))) {
1329 GST_DEBUG_OBJECT (dec, "clipped buffer %p", buf);
1334 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1335 dec->discont = FALSE;
1337 GST_DEBUG_OBJECT (dec, "pushing buffer %p, samples %u, "
1338 "ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
1339 buf, sample_count, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1340 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1342 res = gst_pad_push (dec->srcpad, buf);
1344 dec->queued = g_list_delete_link (dec->queued, dec->queued);
1347 GST_DEBUG_OBJECT (dec, "we don't have a granulepos yet, delayed push");
1352 static GstFlowReturn
1353 vorbis_dec_chain_reverse (GstVorbisDec * vd, gboolean discont, GstBuffer * buf)
1355 GstFlowReturn result = GST_FLOW_OK;
1357 /* if we have a discont, move buffers to the decode list */
1358 if (G_UNLIKELY (discont)) {
1359 GST_DEBUG_OBJECT (vd, "received discont");
1360 while (vd->gather) {
1363 gbuf = GST_BUFFER_CAST (vd->gather->data);
1364 /* remove from the gather list */
1365 vd->gather = g_list_delete_link (vd->gather, vd->gather);
1366 /* copy to decode queue */
1367 vd->decode = g_list_prepend (vd->decode, gbuf);
1369 /* flush and decode the decode queue */
1370 result = vorbis_dec_flush_decode (vd);
1373 GST_DEBUG_OBJECT (vd, "gathering buffer %p, size %u", buf,
1374 GST_BUFFER_SIZE (buf));
1375 /* add buffer to gather queue */
1376 vd->gather = g_list_prepend (vd->gather, buf);
1381 static GstFlowReturn
1382 vorbis_dec_chain_forward (GstVorbisDec * vd, gboolean discont,
1385 GstFlowReturn result;
1387 result = vorbis_dec_decode_buffer (vd, buffer);
1389 gst_buffer_unref (buffer);
1395 static GstFlowReturn
1396 vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
1399 GstFlowReturn result = GST_FLOW_OK;
1402 vd = GST_VORBIS_DEC (gst_pad_get_parent (pad));
1404 discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1406 /* resync on DISCONT */
1407 if (G_UNLIKELY (discont)) {
1408 GST_DEBUG_OBJECT (vd, "received DISCONT buffer");
1409 vd->granulepos = -1;
1410 vd->cur_timestamp = GST_CLOCK_TIME_NONE;
1411 vd->prev_timestamp = GST_CLOCK_TIME_NONE;
1412 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
1413 vorbis_synthesis_restart (&vd->vd);
1418 if (vd->segment.rate >= 0.0)
1419 result = vorbis_dec_chain_forward (vd, discont, buffer);
1421 result = vorbis_dec_chain_reverse (vd, discont, buffer);
1423 gst_object_unref (vd);
1428 static GstStateChangeReturn
1429 vorbis_dec_change_state (GstElement * element, GstStateChange transition)
1431 GstVorbisDec *vd = GST_VORBIS_DEC (element);
1432 GstStateChangeReturn res;
1434 switch (transition) {
1435 case GST_STATE_CHANGE_NULL_TO_READY:
1437 case GST_STATE_CHANGE_READY_TO_PAUSED:
1438 vorbis_info_init (&vd->vi);
1439 vorbis_comment_init (&vd->vc);
1440 vd->initialized = FALSE;
1441 gst_vorbis_dec_reset (vd);
1443 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1449 res = parent_class->change_state (element, transition);
1451 switch (transition) {
1452 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1454 case GST_STATE_CHANGE_PAUSED_TO_READY:
1455 GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
1456 vd->initialized = FALSE;
1457 vorbis_block_clear (&vd->vb);
1458 vorbis_dsp_clear (&vd->vd);
1459 vorbis_comment_clear (&vd->vc);
1460 vorbis_info_clear (&vd->vi);
1461 gst_vorbis_dec_reset (vd);
1463 case GST_STATE_CHANGE_READY_TO_NULL: