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);
169 vorbis_block_clear (&vd->vb);
170 vorbis_dsp_clear (&vd->vd);
171 vorbis_comment_clear (&vd->vc);
172 vorbis_info_clear (&vd->vi);
174 G_OBJECT_CLASS (parent_class)->finalize (object);
178 gst_vorbis_dec_reset (GstVorbisDec * dec)
180 dec->last_timestamp = GST_CLOCK_TIME_NONE;
182 dec->seqnum = gst_util_seqnum_next ();
183 gst_segment_init (&dec->segment, GST_FORMAT_TIME);
185 g_list_foreach (dec->queued, (GFunc) gst_mini_object_unref, NULL);
186 g_list_free (dec->queued);
188 g_list_foreach (dec->gather, (GFunc) gst_mini_object_unref, NULL);
189 g_list_free (dec->gather);
191 g_list_foreach (dec->decode, (GFunc) gst_mini_object_unref, NULL);
192 g_list_free (dec->decode);
194 g_list_foreach (dec->pendingevents, (GFunc) gst_mini_object_unref, NULL);
195 g_list_free (dec->pendingevents);
196 dec->pendingevents = NULL;
199 gst_tag_list_free (dec->taglist);
205 vorbis_dec_convert (GstPad * pad,
206 GstFormat src_format, gint64 src_value,
207 GstFormat * dest_format, gint64 * dest_value)
213 if (src_format == *dest_format) {
214 *dest_value = src_value;
218 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
220 if (!dec->initialized)
223 if (dec->sinkpad == pad &&
224 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
227 switch (src_format) {
228 case GST_FORMAT_TIME:
229 switch (*dest_format) {
230 case GST_FORMAT_BYTES:
231 scale = dec->width * dec->vi.channels;
232 case GST_FORMAT_DEFAULT:
234 scale * gst_util_uint64_scale_int (src_value, dec->vi.rate,
241 case GST_FORMAT_DEFAULT:
242 switch (*dest_format) {
243 case GST_FORMAT_BYTES:
244 *dest_value = src_value * dec->width * dec->vi.channels;
246 case GST_FORMAT_TIME:
248 gst_util_uint64_scale_int (src_value, GST_SECOND, dec->vi.rate);
254 case GST_FORMAT_BYTES:
255 switch (*dest_format) {
256 case GST_FORMAT_DEFAULT:
257 *dest_value = src_value / (dec->width * dec->vi.channels);
259 case GST_FORMAT_TIME:
260 *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
261 dec->vi.rate * dec->width * dec->vi.channels);
271 gst_object_unref (dec);
278 GST_DEBUG_OBJECT (dec, "no header packets received");
284 GST_DEBUG_OBJECT (dec, "formats unsupported");
291 vorbis_dec_src_query (GstPad * pad, GstQuery * query)
294 gboolean res = FALSE;
296 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
297 if (G_UNLIKELY (dec == NULL))
300 switch (GST_QUERY_TYPE (query)) {
301 case GST_QUERY_POSITION:
307 gst_query_parse_position (query, &format, NULL);
309 /* we start from the last seen time */
310 time = dec->last_timestamp;
311 /* correct for the segment values */
312 time = gst_segment_to_stream_time (&dec->segment, GST_FORMAT_TIME, time);
315 "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
317 /* and convert to the final format */
319 vorbis_dec_convert (pad, GST_FORMAT_TIME, time, &format, &value)))
322 gst_query_set_position (query, format, value);
325 "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
330 case GST_QUERY_DURATION:
332 res = gst_pad_peer_query (dec->sinkpad, query);
338 case GST_QUERY_CONVERT:
340 GstFormat src_fmt, dest_fmt;
341 gint64 src_val, dest_val;
343 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
345 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
347 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
351 res = gst_pad_query_default (pad, query);
355 gst_object_unref (dec);
362 GST_WARNING_OBJECT (dec, "error handling query");
368 vorbis_dec_sink_query (GstPad * pad, GstQuery * query)
373 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
375 switch (GST_QUERY_TYPE (query)) {
376 case GST_QUERY_CONVERT:
378 GstFormat src_fmt, dest_fmt;
379 gint64 src_val, dest_val;
381 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
383 vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
385 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
389 res = gst_pad_query_default (pad, query);
394 gst_object_unref (dec);
401 GST_DEBUG_OBJECT (dec, "error converting value");
407 vorbis_dec_src_event (GstPad * pad, GstEvent * event)
412 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
413 if (G_UNLIKELY (dec == NULL)) {
414 gst_event_unref (event);
418 switch (GST_EVENT_TYPE (event)) {
421 GstFormat format, tformat;
425 GstSeekType cur_type, stop_type;
430 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
432 seqnum = gst_event_get_seqnum (event);
433 gst_event_unref (event);
435 /* First bring the requested format to time */
436 tformat = GST_FORMAT_TIME;
437 if (!(res = vorbis_dec_convert (pad, format, cur, &tformat, &tcur)))
439 if (!(res = vorbis_dec_convert (pad, format, stop, &tformat, &tstop)))
442 /* then seek with time on the peer */
443 real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
444 flags, cur_type, tcur, stop_type, tstop);
445 gst_event_set_seqnum (real_seek, seqnum);
447 res = gst_pad_push_event (dec->sinkpad, real_seek);
451 res = gst_pad_push_event (dec->sinkpad, event);
455 gst_object_unref (dec);
462 GST_DEBUG_OBJECT (dec, "cannot convert start/stop for seek");
468 vorbis_dec_sink_event (GstPad * pad, GstEvent * event)
470 gboolean ret = FALSE;
473 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
475 GST_LOG_OBJECT (dec, "handling event");
476 switch (GST_EVENT_TYPE (event)) {
478 if (dec->segment.rate < 0.0)
479 vorbis_dec_chain_reverse (dec, TRUE, NULL);
480 ret = gst_pad_push_event (dec->srcpad, event);
482 case GST_EVENT_FLUSH_START:
483 ret = gst_pad_push_event (dec->srcpad, event);
485 case GST_EVENT_FLUSH_STOP:
486 /* here we must clean any state in the decoder */
487 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
488 vorbis_synthesis_restart (&dec->vd);
490 gst_vorbis_dec_reset (dec);
491 ret = gst_pad_push_event (dec->srcpad, event);
493 case GST_EVENT_NEWSEGMENT:
497 gint64 start, stop, time;
500 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
501 &start, &stop, &time);
503 /* we need time for now */
504 if (format != GST_FORMAT_TIME)
505 goto newseg_wrong_format;
507 GST_DEBUG_OBJECT (dec,
508 "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
509 ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
510 update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
511 GST_TIME_ARGS (time));
513 /* now configure the values */
514 gst_segment_set_newsegment_full (&dec->segment, update,
515 rate, arate, format, start, stop, time);
516 dec->seqnum = gst_event_get_seqnum (event);
518 if (dec->initialized)
520 ret = gst_pad_push_event (dec->srcpad, event);
522 /* store it to send once we're initialized */
523 dec->pendingevents = g_list_append (dec->pendingevents, event);
530 if (dec->initialized)
532 ret = gst_pad_push_event (dec->srcpad, event);
534 /* store it to send once we're initialized */
535 dec->pendingevents = g_list_append (dec->pendingevents, event);
541 ret = gst_pad_push_event (dec->srcpad, event);
545 gst_object_unref (dec);
552 GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
558 vorbis_handle_identification_packet (GstVorbisDec * vd)
561 const GstAudioChannelPosition *pos = NULL;
562 gint width = GST_VORBIS_DEC_DEFAULT_SAMPLE_WIDTH;
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;
592 /* negotiate width with downstream */
593 caps = gst_pad_get_allowed_caps (vd->srcpad);
595 if (!gst_caps_is_empty (caps)) {
598 s = gst_caps_get_structure (caps, 0);
599 /* template ensures 16 or 32 */
600 gst_structure_get_int (s, "width", &width);
602 GST_INFO_OBJECT (vd, "using %s with %d channels and %d bit audio depth",
603 gst_structure_get_name (s), vd->vi.channels, width);
605 gst_caps_unref (caps);
607 vd->width = width >> 3;
609 /* select a copy_samples function, this way we can have specialized versions
610 * for mono/stereo and avoid the depth switch in tremor case */
611 vd->copy_samples = get_copy_sample_func (vd->vi.channels, vd->width);
613 caps = gst_caps_copy (gst_pad_get_pad_template_caps (vd->srcpad));
614 gst_caps_set_simple (caps, "rate", G_TYPE_INT, vd->vi.rate,
615 "channels", G_TYPE_INT, vd->vi.channels,
616 "width", G_TYPE_INT, width, NULL);
619 gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
622 if (vd->vi.channels > 8) {
623 g_free ((GstAudioChannelPosition *) pos);
626 gst_pad_set_caps (vd->srcpad, caps);
627 gst_caps_unref (caps);
633 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
636 gchar *encoder = NULL;
637 GstTagList *list, *old_list;
641 GST_DEBUG_OBJECT (vd, "parsing comment packet");
643 data = gst_ogg_packet_data (packet);
644 size = gst_ogg_packet_size (packet);
647 gst_tag_list_from_vorbiscomment (data, size, (guint8 *) "\003vorbis", 7,
650 old_list = vd->taglist;
651 vd->taglist = gst_tag_list_merge (vd->taglist, list, GST_TAG_MERGE_REPLACE);
654 gst_tag_list_free (old_list);
655 gst_tag_list_free (list);
658 GST_ERROR_OBJECT (vd, "couldn't decode comments");
659 vd->taglist = gst_tag_list_new ();
663 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
664 GST_TAG_ENCODER, encoder, NULL);
667 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
668 GST_TAG_ENCODER_VERSION, vd->vi.version,
669 GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
670 if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
671 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
672 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
673 bitrate = vd->vi.bitrate_nominal;
675 if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
676 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
677 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
679 bitrate = vd->vi.bitrate_upper;
681 if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
682 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
683 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
685 bitrate = vd->vi.bitrate_lower;
688 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
689 GST_TAG_BITRATE, (guint) bitrate, NULL);
692 if (vd->initialized) {
693 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd), vd->srcpad,
697 /* Only post them as messages for the time being. *
698 * They will be pushed on the pad once the decoder is initialized */
699 gst_element_post_message (GST_ELEMENT_CAST (vd),
700 gst_message_new_tag (GST_OBJECT (vd), gst_tag_list_copy (vd->taglist)));
707 vorbis_handle_type_packet (GstVorbisDec * vd)
712 g_assert (vd->initialized == FALSE);
714 if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
715 goto synthesis_init_error;
717 if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
718 goto block_init_error;
720 vd->initialized = TRUE;
722 if (vd->pendingevents) {
723 for (walk = vd->pendingevents; walk; walk = g_list_next (walk))
724 gst_pad_push_event (vd->srcpad, GST_EVENT_CAST (walk->data));
725 g_list_free (vd->pendingevents);
726 vd->pendingevents = NULL;
730 /* The tags have already been sent on the bus as messages. */
731 gst_pad_push_event (vd->srcpad, gst_event_new_tag (vd->taglist));
737 synthesis_init_error:
739 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
740 (NULL), ("couldn't initialize synthesis (%d)", res));
741 return GST_FLOW_ERROR;
745 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
746 (NULL), ("couldn't initialize block (%d)", res));
747 return GST_FLOW_ERROR;
752 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
757 GST_DEBUG_OBJECT (vd, "parsing header packet");
759 /* Packetno = 0 if the first byte is exactly 0x01 */
760 packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
762 if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
763 goto header_read_error;
765 switch ((gst_ogg_packet_data (packet))[0]) {
767 res = vorbis_handle_identification_packet (vd);
770 res = vorbis_handle_comment_packet (vd, packet);
773 res = vorbis_handle_type_packet (vd);
777 g_warning ("unknown vorbis header packet found");
786 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
787 (NULL), ("couldn't read header packet (%d)", ret));
788 return GST_FLOW_ERROR;
793 vorbis_dec_push_forward (GstVorbisDec * dec, GstBuffer * buf)
795 GstFlowReturn result;
798 if (!(buf = gst_audio_buffer_clip (buf, &dec->segment, dec->vi.rate,
799 dec->vi.channels * dec->width))) {
800 GST_LOG_OBJECT (dec, "clipped buffer");
805 GST_LOG_OBJECT (dec, "setting DISCONT");
806 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
807 dec->discont = FALSE;
810 GST_DEBUG_OBJECT (dec,
811 "pushing time %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
812 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
813 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
815 result = gst_pad_push (dec->srcpad, buf);
821 vorbis_dec_push_reverse (GstVorbisDec * dec, GstBuffer * buf)
823 GstFlowReturn result = GST_FLOW_OK;
825 dec->queued = g_list_prepend (dec->queued, buf);
831 vorbis_do_timestamps (GstVorbisDec * vd, GstBuffer * buf, gboolean reverse,
832 GstClockTime timestamp, GstClockTime duration)
834 /* interpolate reverse */
835 if (vd->last_timestamp != -1 && duration != -1 && reverse)
836 vd->last_timestamp -= duration;
838 /* take buffer timestamp, use interpolated timestamp otherwise */
840 vd->last_timestamp = timestamp;
842 timestamp = vd->last_timestamp;
844 /* interpolate forwards */
845 if (vd->last_timestamp != -1 && duration != -1 && !reverse)
846 vd->last_timestamp += duration;
849 "keeping timestamp %" GST_TIME_FORMAT " ts %" GST_TIME_FORMAT " dur %"
850 GST_TIME_FORMAT, GST_TIME_ARGS (vd->last_timestamp),
851 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
854 GST_BUFFER_TIMESTAMP (buf) = timestamp;
855 GST_BUFFER_DURATION (buf) = duration;
860 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
861 GstClockTime timestamp, GstClockTime duration)
863 vorbis_sample_t **pcm;
865 GstBuffer *out = NULL;
866 GstFlowReturn result;
870 if (G_UNLIKELY (!vd->initialized))
871 goto not_initialized;
873 /* normal data packet */
874 /* FIXME, we can skip decoding if the packet is outside of the
875 * segment, this is however not very trivial as we need a previous
876 * packet to decode the current one so we must be carefull not to
877 * throw away too much. For now we decode everything and clip right
878 * before pushing data. */
879 if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
882 if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
885 /* assume all goes well here */
886 result = GST_FLOW_OK;
888 /* count samples ready for reading */
889 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
892 size = sample_count * vd->vi.channels * vd->width;
893 GST_LOG_OBJECT (vd, "%d samples ready for reading, size %d", sample_count,
896 /* alloc buffer for it */
898 gst_pad_alloc_buffer_and_set_caps (vd->srcpad, GST_BUFFER_OFFSET_NONE,
899 size, GST_PAD_CAPS (vd->srcpad), &out);
900 if (G_UNLIKELY (result != GST_FLOW_OK))
903 /* get samples ready for reading now, should be sample_count */
904 if (G_UNLIKELY ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count))
907 /* copy samples in buffer */
908 data = gst_buffer_map (out, NULL, NULL, GST_MAP_WRITE);
909 vd->copy_samples ((vorbis_sample_t *) data, pcm,
910 sample_count, vd->vi.channels, vd->width);
912 GST_LOG_OBJECT (vd, "setting output size to %d", size);
913 gst_buffer_unmap (out, data, size);
915 /* this should not overflow */
917 duration = sample_count * GST_SECOND / vd->vi.rate;
919 vorbis_do_timestamps (vd, out, FALSE, timestamp, duration);
921 if (vd->segment.rate >= 0.0)
922 result = vorbis_dec_push_forward (vd, out);
924 result = vorbis_dec_push_reverse (vd, out);
928 /* no output, still keep track of timestamps */
929 vorbis_do_timestamps (vd, NULL, FALSE, timestamp, duration);
931 vorbis_synthesis_read (&vd->vd, sample_count);
938 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
939 (NULL), ("no header sent yet"));
940 return GST_FLOW_ERROR;
944 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
945 (NULL), ("couldn't read data packet"));
946 return GST_FLOW_ERROR;
950 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
951 (NULL), ("vorbis decoder did not accept data packet"));
952 return GST_FLOW_ERROR;
956 gst_buffer_unref (out);
957 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
958 (NULL), ("vorbis decoder reported wrong number of samples"));
959 return GST_FLOW_ERROR;
964 vorbis_dec_decode_buffer (GstVorbisDec * vd, GstBuffer * buffer)
967 ogg_packet_wrapper packet_wrapper;
968 GstFlowReturn result = GST_FLOW_OK;
970 /* make ogg_packet out of the buffer */
971 gst_ogg_packet_wrapper_map (&packet_wrapper, buffer);
972 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
973 /* set some more stuff */
974 packet->granulepos = -1;
975 packet->packetno = 0; /* we don't care */
976 /* EOS does not matter, it is used in vorbis to implement clipping the last
977 * block of samples based on the granulepos. We clip based on segments. */
980 GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
982 /* error out on empty header packets, but just skip empty data packets */
983 if (G_UNLIKELY (packet->bytes == 0)) {
990 /* switch depending on packet type */
991 if ((gst_ogg_packet_data (packet))[0] & 1) {
992 if (vd->initialized) {
993 GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
996 result = vorbis_handle_header_packet (vd, packet);
998 GstClockTime timestamp, duration;
1000 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1001 duration = GST_BUFFER_DURATION (buffer);
1003 result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
1007 gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer);
1013 /* don't error out here, just ignore the buffer, it's invalid for vorbis
1015 GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
1016 result = GST_FLOW_OK;
1023 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
1024 result = GST_FLOW_ERROR;
1032 * Buffer decoding order: 7 8 9 4 5 6 3 1 2 EOS
1033 * Discont flag: D D D D
1035 * - Each Discont marks a discont in the decoding order.
1037 * for vorbis, each buffer is a keyframe when we have the previous
1038 * buffer. This means that to decode buffer 7, we need buffer 6, which
1039 * arrives out of order.
1041 * we first gather buffers in the gather queue until we get a DISCONT. We
1042 * prepend each incomming buffer so that they are in reversed order.
1044 * gather queue: 9 8 7
1048 * When a DISCONT is received (buffer 4), we move the gather queue to the
1049 * decode queue. This is simply done be taking the head of the gather queue
1050 * and prepending it to the decode queue. This yields:
1053 * decode queue: 7 8 9
1056 * Then we decode each buffer in the decode queue in order and put the output
1057 * buffer in the output queue. The first buffer (7) will not produce any output
1058 * because it needs the previous buffer (6) which did not arrive yet. This
1062 * decode queue: 7 8 9
1065 * Then we remove the consumed buffers from the decode queue. Buffer 7 is not
1066 * completely consumed, we need to keep it around for when we receive buffer
1073 * Then we accumulate more buffers:
1075 * gather queue: 6 5 4
1079 * prepending to the decode queue on DISCONT yields:
1082 * decode queue: 4 5 6 7
1085 * after decoding and keeping buffer 4:
1089 * output queue: 7 6 5
1093 static GstFlowReturn
1094 vorbis_dec_flush_decode (GstVorbisDec * dec)
1096 GstFlowReturn res = GST_FLOW_OK;
1101 GST_DEBUG_OBJECT (dec, "flushing buffers to decoder");
1105 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1107 GST_DEBUG_OBJECT (dec, "decoding buffer %p, ts %" GST_TIME_FORMAT,
1108 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1110 next = g_list_next (walk);
1112 /* decode buffer, prepend to output queue */
1113 res = vorbis_dec_decode_buffer (dec, buf);
1115 /* if we generated output, we can discard the buffer, else we
1116 * keep it in the queue */
1118 GST_DEBUG_OBJECT (dec, "decoded buffer to %p", dec->queued->data);
1119 dec->decode = g_list_delete_link (dec->decode, walk);
1120 gst_buffer_unref (buf);
1122 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1126 while (dec->queued) {
1127 GstBuffer *buf = GST_BUFFER_CAST (dec->queued->data);
1128 GstClockTime timestamp, duration;
1130 timestamp = GST_BUFFER_TIMESTAMP (buf);
1131 duration = GST_BUFFER_DURATION (buf);
1133 vorbis_do_timestamps (dec, buf, TRUE, timestamp, duration);
1134 res = vorbis_dec_push_forward (dec, buf);
1136 dec->queued = g_list_delete_link (dec->queued, dec->queued);
1141 static GstFlowReturn
1142 vorbis_dec_chain_reverse (GstVorbisDec * vd, gboolean discont, GstBuffer * buf)
1144 GstFlowReturn result = GST_FLOW_OK;
1146 /* if we have a discont, move buffers to the decode list */
1147 if (G_UNLIKELY (discont)) {
1148 GST_DEBUG_OBJECT (vd, "received discont");
1149 while (vd->gather) {
1152 gbuf = GST_BUFFER_CAST (vd->gather->data);
1153 /* remove from the gather list */
1154 vd->gather = g_list_delete_link (vd->gather, vd->gather);
1155 /* copy to decode queue */
1156 vd->decode = g_list_prepend (vd->decode, gbuf);
1158 /* flush and decode the decode queue */
1159 result = vorbis_dec_flush_decode (vd);
1162 if (G_LIKELY (buf)) {
1163 GST_DEBUG_OBJECT (vd,
1164 "gathering buffer %p of size %u, time %" GST_TIME_FORMAT
1165 ", dur %" GST_TIME_FORMAT, buf, gst_buffer_get_size (buf),
1166 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1167 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1169 /* add buffer to gather queue */
1170 vd->gather = g_list_prepend (vd->gather, buf);
1176 static GstFlowReturn
1177 vorbis_dec_chain_forward (GstVorbisDec * vd, gboolean discont,
1180 GstFlowReturn result;
1182 result = vorbis_dec_decode_buffer (vd, buffer);
1184 gst_buffer_unref (buffer);
1189 static GstFlowReturn
1190 vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
1193 GstFlowReturn result = GST_FLOW_OK;
1196 vd = GST_VORBIS_DEC (gst_pad_get_parent (pad));
1198 discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1200 /* resync on DISCONT */
1201 if (G_UNLIKELY (discont)) {
1202 GST_DEBUG_OBJECT (vd, "received DISCONT buffer");
1203 vd->last_timestamp = GST_CLOCK_TIME_NONE;
1204 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
1205 vorbis_synthesis_restart (&vd->vd);
1210 if (vd->segment.rate >= 0.0)
1211 result = vorbis_dec_chain_forward (vd, discont, buffer);
1213 result = vorbis_dec_chain_reverse (vd, discont, buffer);
1215 gst_object_unref (vd);
1220 static GstStateChangeReturn
1221 vorbis_dec_change_state (GstElement * element, GstStateChange transition)
1223 GstVorbisDec *vd = GST_VORBIS_DEC (element);
1224 GstStateChangeReturn res;
1226 switch (transition) {
1227 case GST_STATE_CHANGE_NULL_TO_READY:
1229 case GST_STATE_CHANGE_READY_TO_PAUSED:
1230 vorbis_info_init (&vd->vi);
1231 vorbis_comment_init (&vd->vc);
1232 vd->initialized = FALSE;
1233 gst_vorbis_dec_reset (vd);
1235 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1241 res = parent_class->change_state (element, transition);
1243 switch (transition) {
1244 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1246 case GST_STATE_CHANGE_PAUSED_TO_READY:
1247 GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
1248 vd->initialized = FALSE;
1249 vorbis_block_clear (&vd->vb);
1250 vorbis_dsp_clear (&vd->vd);
1251 vorbis_comment_clear (&vd->vc);
1252 vorbis_info_clear (&vd->vi);
1253 gst_vorbis_dec_reset (vd);
1255 case GST_STATE_CHANGE_READY_TO_NULL: