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 (GstVorbisDec, gst_vorbis_dec, GstAudioDecoder,
68 GST_TYPE_AUDIO_DECODER);
70 static void vorbis_dec_finalize (GObject * object);
72 static gboolean vorbis_dec_start (GstAudioDecoder * dec);
73 static gboolean vorbis_dec_stop (GstAudioDecoder * dec);
74 static GstFlowReturn vorbis_dec_handle_frame (GstAudioDecoder * dec,
76 static void vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard);
79 gst_vorbis_dec_base_init (gpointer g_class)
81 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
83 gst_element_class_add_static_pad_template (element_class,
84 &vorbis_dec_src_factory);
85 gst_element_class_add_static_pad_template (element_class,
86 &vorbis_dec_sink_factory);
88 gst_element_class_set_details_simple (element_class,
89 "Vorbis audio decoder", "Codec/Decoder/Audio",
90 GST_VORBIS_DEC_DESCRIPTION,
91 "Benjamin Otte <otte@gnome.org>, Chris Lord <chris@openedhand.com>");
95 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98 GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
100 gobject_class->finalize = vorbis_dec_finalize;
102 base_class->start = GST_DEBUG_FUNCPTR (vorbis_dec_start);
103 base_class->stop = GST_DEBUG_FUNCPTR (vorbis_dec_stop);
104 base_class->handle_frame = GST_DEBUG_FUNCPTR (vorbis_dec_handle_frame);
105 base_class->flush = GST_DEBUG_FUNCPTR (vorbis_dec_flush);
109 gst_vorbis_dec_init (GstVorbisDec * dec, GstVorbisDecClass * g_class)
114 vorbis_dec_finalize (GObject * object)
116 /* Release any possibly allocated libvorbis data.
117 * _clear functions can safely be called multiple times
119 GstVorbisDec *vd = GST_VORBIS_DEC (object);
122 vorbis_block_clear (&vd->vb);
124 vorbis_dsp_clear (&vd->vd);
125 vorbis_comment_clear (&vd->vc);
126 vorbis_info_clear (&vd->vi);
128 G_OBJECT_CLASS (parent_class)->finalize (object);
132 gst_vorbis_dec_reset (GstVorbisDec * dec)
135 gst_tag_list_free (dec->taglist);
140 vorbis_dec_start (GstAudioDecoder * dec)
142 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
144 GST_DEBUG_OBJECT (dec, "start");
145 vorbis_info_init (&vd->vi);
146 vorbis_comment_init (&vd->vc);
147 vd->initialized = FALSE;
148 gst_vorbis_dec_reset (vd);
154 vorbis_dec_stop (GstAudioDecoder * dec)
156 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
158 GST_DEBUG_OBJECT (dec, "stop");
159 vd->initialized = FALSE;
161 vorbis_block_clear (&vd->vb);
163 vorbis_dsp_clear (&vd->vd);
164 vorbis_comment_clear (&vd->vc);
165 vorbis_info_clear (&vd->vi);
166 gst_vorbis_dec_reset (vd);
173 vorbis_dec_src_event (GstPad * pad, GstEvent * event)
178 dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
180 switch (GST_EVENT_TYPE (event)) {
183 GstFormat format, tformat;
187 GstSeekType cur_type, stop_type;
192 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
194 seqnum = gst_event_get_seqnum (event);
195 gst_event_unref (event);
197 /* First bring the requested format to time */
198 tformat = GST_FORMAT_TIME;
199 if (!(res = vorbis_dec_convert (pad, format, cur, &tformat, &tcur)))
201 if (!(res = vorbis_dec_convert (pad, format, stop, &tformat, &tstop)))
204 /* then seek with time on the peer */
205 real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
206 flags, cur_type, tcur, stop_type, tstop);
207 gst_event_set_seqnum (real_seek, seqnum);
209 res = gst_pad_push_event (dec->sinkpad, real_seek);
213 res = gst_pad_push_event (dec->sinkpad, event);
217 gst_object_unref (dec);
224 GST_DEBUG_OBJECT (dec, "cannot convert start/stop for seek");
231 vorbis_handle_identification_packet (GstVorbisDec * vd)
234 const GstAudioChannelPosition *pos = NULL;
235 gint width = GST_VORBIS_DEC_DEFAULT_SAMPLE_WIDTH;
237 switch (vd->vi.channels) {
248 pos = gst_vorbis_channel_positions[vd->vi.channels - 1];
252 GstAudioChannelPosition *posn =
253 g_new (GstAudioChannelPosition, vd->vi.channels);
255 GST_ELEMENT_WARNING (GST_ELEMENT (vd), STREAM, DECODE,
256 (NULL), ("Using NONE channel layout for more than 8 channels"));
258 for (i = 0; i < vd->vi.channels; i++)
259 posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
265 /* negotiate width with downstream */
266 caps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (vd));
268 if (!gst_caps_is_empty (caps)) {
271 s = gst_caps_get_structure (caps, 0);
272 /* template ensures 16 or 32 */
273 gst_structure_get_int (s, "width", &width);
275 GST_INFO_OBJECT (vd, "using %s with %d channels and %d bit audio depth",
276 gst_structure_get_name (s), vd->vi.channels, width);
278 gst_caps_unref (caps);
280 vd->width = width >> 3;
282 /* select a copy_samples function, this way we can have specialized versions
283 * for mono/stereo and avoid the depth switch in tremor case */
284 vd->copy_samples = get_copy_sample_func (vd->vi.channels, vd->width);
287 gst_caps_copy (gst_pad_get_pad_template_caps
288 (GST_AUDIO_DECODER_SRC_PAD (vd)));
289 gst_caps_set_simple (caps, "rate", G_TYPE_INT, vd->vi.rate, "channels",
290 G_TYPE_INT, vd->vi.channels, "width", G_TYPE_INT, width, NULL);
293 gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
296 if (vd->vi.channels > 8) {
297 g_free ((GstAudioChannelPosition *) pos);
300 gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (vd), caps);
301 gst_caps_unref (caps);
307 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
310 gchar *encoder = NULL;
311 GstTagList *list, *old_list;
314 GST_DEBUG_OBJECT (vd, "parsing comment packet");
316 buf = gst_buffer_new ();
317 GST_BUFFER_DATA (buf) = gst_ogg_packet_data (packet);
318 GST_BUFFER_SIZE (buf) = gst_ogg_packet_size (packet);
321 gst_tag_list_from_vorbiscomment_buffer (buf, (guint8 *) "\003vorbis", 7,
324 old_list = vd->taglist;
325 vd->taglist = gst_tag_list_merge (vd->taglist, list, GST_TAG_MERGE_REPLACE);
328 gst_tag_list_free (old_list);
329 gst_tag_list_free (list);
330 gst_buffer_unref (buf);
333 GST_ERROR_OBJECT (vd, "couldn't decode comments");
334 vd->taglist = gst_tag_list_new ();
338 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
339 GST_TAG_ENCODER, encoder, NULL);
342 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
343 GST_TAG_ENCODER_VERSION, vd->vi.version,
344 GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
345 if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
346 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
347 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
348 bitrate = vd->vi.bitrate_nominal;
350 if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
351 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
352 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
354 bitrate = vd->vi.bitrate_upper;
356 if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
357 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
358 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
360 bitrate = vd->vi.bitrate_lower;
363 gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
364 GST_TAG_BITRATE, (guint) bitrate, NULL);
367 if (vd->initialized) {
368 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd),
369 GST_AUDIO_DECODER_SRC_PAD (vd), vd->taglist);
372 /* Only post them as messages for the time being. *
373 * They will be pushed on the pad once the decoder is initialized */
374 gst_element_post_message (GST_ELEMENT_CAST (vd),
375 gst_message_new_tag (GST_OBJECT (vd), gst_tag_list_copy (vd->taglist)));
382 vorbis_handle_type_packet (GstVorbisDec * vd)
386 g_assert (vd->initialized == FALSE);
389 if (G_UNLIKELY ((res = vorbis_dsp_init (&vd->vd, &vd->vi))))
390 goto synthesis_init_error;
392 if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
393 goto synthesis_init_error;
395 if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
396 goto block_init_error;
399 vd->initialized = TRUE;
402 /* The tags have already been sent on the bus as messages. */
403 gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (vd),
404 gst_event_new_tag (vd->taglist));
410 synthesis_init_error:
412 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
413 (NULL), ("couldn't initialize synthesis (%d)", res));
414 return GST_FLOW_ERROR;
418 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
419 (NULL), ("couldn't initialize block (%d)", res));
420 return GST_FLOW_ERROR;
425 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
430 GST_DEBUG_OBJECT (vd, "parsing header packet");
432 /* Packetno = 0 if the first byte is exactly 0x01 */
433 packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
436 if ((ret = vorbis_dsp_headerin (&vd->vi, &vd->vc, packet)))
438 if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
440 goto header_read_error;
442 switch ((gst_ogg_packet_data (packet))[0]) {
444 res = vorbis_handle_identification_packet (vd);
447 res = vorbis_handle_comment_packet (vd, packet);
450 res = vorbis_handle_type_packet (vd);
454 g_warning ("unknown vorbis header packet found");
464 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
465 (NULL), ("couldn't read header packet (%d)", ret));
466 return GST_FLOW_ERROR;
471 vorbis_dec_handle_header_buffer (GstVorbisDec * vd, GstBuffer * buffer)
474 ogg_packet_wrapper packet_wrapper;
476 gst_ogg_packet_wrapper_from_buffer (&packet_wrapper, buffer);
477 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
479 return vorbis_handle_header_packet (vd, packet);
482 #define MIN_NUM_HEADERS 3
484 vorbis_dec_handle_header_caps (GstVorbisDec * vd)
486 GstFlowReturn result = GST_FLOW_OK;
488 GstStructure *s = NULL;
489 const GValue *array = NULL;
491 caps = GST_PAD_CAPS (GST_AUDIO_DECODER_SINK_PAD (vd));
493 s = gst_caps_get_structure (caps, 0);
495 array = gst_structure_get_value (s, "streamheader");
497 if (array && (gst_value_array_get_size (array) >= MIN_NUM_HEADERS)) {
498 const GValue *value = NULL;
499 GstBuffer *buf = NULL;
502 while (result == GST_FLOW_OK && i < gst_value_array_get_size (array)) {
503 value = gst_value_array_get_value (array, i);
504 buf = gst_value_get_buffer (value);
507 result = vorbis_dec_handle_header_buffer (vd, buf);
514 return (result != GST_FLOW_OK ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_OK);
519 GST_WARNING_OBJECT (vd, "streamheader array not found");
520 result = GST_FLOW_ERROR;
525 GST_WARNING_OBJECT (vd, "streamheader with null buffer received");
526 result = GST_FLOW_ERROR;
533 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
534 GstClockTime timestamp, GstClockTime duration)
537 vorbis_sample_t *pcm;
539 vorbis_sample_t **pcm;
542 GstBuffer *out = NULL;
543 GstFlowReturn result;
546 if (G_UNLIKELY (!vd->initialized)) {
547 result = vorbis_dec_handle_header_caps (vd);
548 if (result != GST_FLOW_OK)
549 goto not_initialized;
552 /* normal data packet */
553 /* FIXME, we can skip decoding if the packet is outside of the
554 * segment, this is however not very trivial as we need a previous
555 * packet to decode the current one so we must be careful not to
556 * throw away too much. For now we decode everything and clip right
557 * before pushing data. */
560 if (G_UNLIKELY (vorbis_dsp_synthesis (&vd->vb, packet, 1)))
563 if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
566 if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
570 /* assume all goes well here */
571 result = GST_FLOW_OK;
573 /* count samples ready for reading */
575 if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0)
577 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
581 size = sample_count * vd->vi.channels * vd->width;
582 GST_LOG_OBJECT (vd, "%d samples ready for reading, size %d", sample_count,
585 /* alloc buffer for it */
587 gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (vd),
588 GST_BUFFER_OFFSET_NONE, size,
589 GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (vd)), &out);
590 if (G_UNLIKELY (result != GST_FLOW_OK))
593 /* get samples ready for reading now, should be sample_count */
595 pcm = GST_BUFFER_DATA (out);
596 if (G_UNLIKELY (vorbis_dsp_pcmout (&vd->vd, pcm, sample_count) !=
599 if (G_UNLIKELY (vorbis_synthesis_pcmout (&vd->vd, &pcm) != sample_count))
604 /* copy samples in buffer */
605 vd->copy_samples ((vorbis_sample_t *) GST_BUFFER_DATA (out), pcm,
606 sample_count, vd->vi.channels, vd->width);
609 GST_LOG_OBJECT (vd, "setting output size to %d", size);
610 GST_BUFFER_SIZE (out) = size;
613 /* whether or not data produced, consume one frame and advance time */
614 result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), out, 1);
617 vorbis_dsp_read (&vd->vd, sample_count);
619 vorbis_synthesis_read (&vd->vd, sample_count);
627 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
628 (NULL), ("no header sent yet"));
629 return GST_FLOW_NOT_NEGOTIATED;
633 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
634 (NULL), ("couldn't read data packet"));
635 return GST_FLOW_ERROR;
639 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
640 (NULL), ("vorbis decoder did not accept data packet"));
641 return GST_FLOW_ERROR;
645 gst_buffer_unref (out);
646 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
647 (NULL), ("vorbis decoder reported wrong number of samples"));
648 return GST_FLOW_ERROR;
653 vorbis_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
656 ogg_packet_wrapper packet_wrapper;
657 GstFlowReturn result = GST_FLOW_OK;
658 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
660 /* no draining etc */
661 if (G_UNLIKELY (!buffer))
664 /* make ogg_packet out of the buffer */
665 gst_ogg_packet_wrapper_from_buffer (&packet_wrapper, buffer);
666 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
667 /* set some more stuff */
668 packet->granulepos = -1;
669 packet->packetno = 0; /* we don't care */
670 /* EOS does not matter, it is used in vorbis to implement clipping the last
671 * block of samples based on the granulepos. We clip based on segments. */
674 GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
676 /* error out on empty header packets, but just skip empty data packets */
677 if (G_UNLIKELY (packet->bytes == 0)) {
684 /* switch depending on packet type */
685 if ((gst_ogg_packet_data (packet))[0] & 1) {
686 if (vd->initialized) {
687 GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
690 result = vorbis_handle_header_packet (vd, packet);
691 /* consumer header packet/frame */
692 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), NULL, 1);
694 GstClockTime timestamp, duration;
696 timestamp = GST_BUFFER_TIMESTAMP (buffer);
697 duration = GST_BUFFER_DURATION (buffer);
699 result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
707 /* don't error out here, just ignore the buffer, it's invalid for vorbis
709 GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
710 result = GST_FLOW_OK;
717 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
718 result = GST_FLOW_ERROR;
724 vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard)
726 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
728 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
729 vorbis_synthesis_restart (&vd->vd);
733 gst_vorbis_dec_reset (vd);