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>
48 #include "gstvorbiscommon.h"
50 GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug);
51 #define GST_CAT_DEFAULT vorbisdec_debug
53 static GstStaticPadTemplate vorbis_dec_src_factory =
54 GST_STATIC_PAD_TEMPLATE ("src",
57 GST_VORBIS_DEC_SRC_CAPS);
59 static GstStaticPadTemplate vorbis_dec_sink_factory =
60 GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_STATIC_CAPS ("audio/x-vorbis")
66 #define gst_vorbis_dec_parent_class parent_class
67 G_DEFINE_TYPE (GstVorbisDec, gst_vorbis_dec, GST_TYPE_AUDIO_DECODER);
69 static void vorbis_dec_finalize (GObject * object);
71 static gboolean vorbis_dec_start (GstAudioDecoder * dec);
72 static gboolean vorbis_dec_stop (GstAudioDecoder * dec);
73 static GstFlowReturn vorbis_dec_handle_frame (GstAudioDecoder * dec,
75 static void vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard);
78 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
80 GstPadTemplate *src_template, *sink_template;
81 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
82 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
83 GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
85 gobject_class->finalize = vorbis_dec_finalize;
87 src_template = gst_static_pad_template_get (&vorbis_dec_src_factory);
88 gst_element_class_add_pad_template (element_class, src_template);
90 sink_template = gst_static_pad_template_get (&vorbis_dec_sink_factory);
91 gst_element_class_add_pad_template (element_class, sink_template);
93 gst_element_class_set_details_simple (element_class,
94 "Vorbis audio decoder", "Codec/Decoder/Audio",
95 GST_VORBIS_DEC_DESCRIPTION,
96 "Benjamin Otte <otte@gnome.org>, Chris Lord <chris@openedhand.com>");
98 base_class->start = GST_DEBUG_FUNCPTR (vorbis_dec_start);
99 base_class->stop = GST_DEBUG_FUNCPTR (vorbis_dec_stop);
100 base_class->handle_frame = GST_DEBUG_FUNCPTR (vorbis_dec_handle_frame);
101 base_class->flush = GST_DEBUG_FUNCPTR (vorbis_dec_flush);
105 gst_vorbis_dec_init (GstVorbisDec * dec)
110 vorbis_dec_finalize (GObject * object)
112 /* Release any possibly allocated libvorbis data.
113 * _clear functions can safely be called multiple times
115 GstVorbisDec *vd = GST_VORBIS_DEC (object);
118 vorbis_block_clear (&vd->vb);
120 vorbis_dsp_clear (&vd->vd);
121 vorbis_comment_clear (&vd->vc);
122 vorbis_info_clear (&vd->vi);
124 G_OBJECT_CLASS (parent_class)->finalize (object);
128 vorbis_dec_start (GstAudioDecoder * dec)
130 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
132 GST_DEBUG_OBJECT (dec, "start");
133 vorbis_info_init (&vd->vi);
134 vorbis_comment_init (&vd->vc);
135 vd->initialized = FALSE;
141 vorbis_dec_stop (GstAudioDecoder * dec)
143 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
145 GST_DEBUG_OBJECT (dec, "stop");
146 vd->initialized = FALSE;
148 vorbis_block_clear (&vd->vb);
150 vorbis_dsp_clear (&vd->vd);
151 vorbis_comment_clear (&vd->vc);
152 vorbis_info_clear (&vd->vi);
158 vorbis_handle_identification_packet (GstVorbisDec * vd)
162 switch (vd->vi.channels) {
172 const GstAudioChannelPosition *pos;
174 pos = gst_vorbis_default_channel_positions[vd->vi.channels - 1];
175 gst_audio_info_set_format (&info, GST_VORBIS_AUDIO_FORMAT, vd->vi.rate,
176 vd->vi.channels, pos);
180 GstAudioChannelPosition position[64];
181 gint i, max_pos = MAX (vd->vi.channels, 64);
183 GST_ELEMENT_WARNING (vd, STREAM, DECODE,
184 (NULL), ("Using NONE channel layout for more than 8 channels"));
185 for (i = 0; i < max_pos; i++)
186 position[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
187 gst_audio_info_set_format (&info, GST_VORBIS_AUDIO_FORMAT, vd->vi.rate,
188 vd->vi.channels, position);
193 gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (vd), &info);
196 /* select a copy_samples function, this way we can have specialized versions
197 * for mono/stereo and avoid the depth switch in tremor case */
198 vd->copy_samples = get_copy_sample_func (info.channels);
203 /* FIXME 0.11: remove tag handling and let container take care of that? */
205 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
208 gchar *encoder = NULL;
213 GST_DEBUG_OBJECT (vd, "parsing comment packet");
215 data = gst_ogg_packet_data (packet);
216 size = gst_ogg_packet_size (packet);
219 gst_tag_list_from_vorbiscomment (data, size, (guint8 *) "\003vorbis", 7,
223 GST_ERROR_OBJECT (vd, "couldn't decode comments");
224 list = gst_tag_list_new_empty ();
229 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
230 GST_TAG_ENCODER, encoder, NULL);
233 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
234 GST_TAG_ENCODER_VERSION, vd->vi.version,
235 GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
236 if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
237 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
238 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
239 bitrate = vd->vi.bitrate_nominal;
241 if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
242 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
243 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
245 bitrate = vd->vi.bitrate_upper;
247 if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
248 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
249 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
251 bitrate = vd->vi.bitrate_lower;
254 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
255 GST_TAG_BITRATE, (guint) bitrate, NULL);
258 gst_audio_decoder_merge_tags (GST_AUDIO_DECODER_CAST (vd), list,
259 GST_TAG_MERGE_REPLACE);
260 if (vd->initialized) {
261 gst_tag_list_free (list);
263 /* Only post them as messages for the time being. *
264 * They will be pushed on the pad once the decoder is initialized */
265 gst_element_post_message (GST_ELEMENT_CAST (vd),
266 gst_message_new_tag (GST_OBJECT (vd), list));
273 vorbis_handle_type_packet (GstVorbisDec * vd)
277 g_assert (vd->initialized == FALSE);
280 if (G_UNLIKELY ((res = vorbis_dsp_init (&vd->vd, &vd->vi))))
281 goto synthesis_init_error;
283 if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
284 goto synthesis_init_error;
286 if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
287 goto block_init_error;
290 vd->initialized = TRUE;
295 synthesis_init_error:
297 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
298 (NULL), ("couldn't initialize synthesis (%d)", res));
299 return GST_FLOW_ERROR;
303 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
304 (NULL), ("couldn't initialize block (%d)", res));
305 return GST_FLOW_ERROR;
310 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
315 GST_DEBUG_OBJECT (vd, "parsing header packet");
317 /* Packetno = 0 if the first byte is exactly 0x01 */
318 packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
321 if ((ret = vorbis_dsp_headerin (&vd->vi, &vd->vc, packet)))
323 if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
325 goto header_read_error;
327 switch ((gst_ogg_packet_data (packet))[0]) {
329 res = vorbis_handle_identification_packet (vd);
332 res = vorbis_handle_comment_packet (vd, packet);
335 res = vorbis_handle_type_packet (vd);
339 g_warning ("unknown vorbis header packet found");
349 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
350 (NULL), ("couldn't read header packet (%d)", ret));
351 return GST_FLOW_ERROR;
356 vorbis_dec_handle_header_buffer (GstVorbisDec * vd, GstBuffer * buffer)
359 ogg_packet_wrapper packet_wrapper;
363 gst_ogg_packet_wrapper_map (&packet_wrapper, buffer, &map);
364 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
366 ret = vorbis_handle_header_packet (vd, packet);
368 gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer, &map);
373 #define MIN_NUM_HEADERS 3
375 vorbis_dec_handle_header_caps (GstVorbisDec * vd)
377 GstFlowReturn result = GST_FLOW_OK;
379 GstStructure *s = NULL;
380 const GValue *array = NULL;
382 caps = gst_pad_get_current_caps (GST_AUDIO_DECODER_SINK_PAD (vd));
384 s = gst_caps_get_structure (caps, 0);
386 array = gst_structure_get_value (s, "streamheader");
389 gst_caps_unref (caps);
391 if (array && (gst_value_array_get_size (array) >= MIN_NUM_HEADERS)) {
392 const GValue *value = NULL;
393 GstBuffer *buf = NULL;
396 while (result == GST_FLOW_OK && i < gst_value_array_get_size (array)) {
397 value = gst_value_array_get_value (array, i);
398 buf = gst_value_get_buffer (value);
401 result = vorbis_dec_handle_header_buffer (vd, buf);
408 return (result != GST_FLOW_OK ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_OK);
413 GST_WARNING_OBJECT (vd, "streamheader array not found");
414 result = GST_FLOW_ERROR;
419 GST_WARNING_OBJECT (vd, "streamheader with null buffer received");
420 result = GST_FLOW_ERROR;
427 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
428 GstClockTime timestamp, GstClockTime duration)
431 vorbis_sample_t *pcm;
433 vorbis_sample_t **pcm;
436 GstBuffer *out = NULL;
437 GstFlowReturn result;
441 if (G_UNLIKELY (!vd->initialized)) {
442 result = vorbis_dec_handle_header_caps (vd);
443 if (result != GST_FLOW_OK)
444 goto not_initialized;
447 /* normal data packet */
448 /* FIXME, we can skip decoding if the packet is outside of the
449 * segment, this is however not very trivial as we need a previous
450 * packet to decode the current one so we must be careful not to
451 * throw away too much. For now we decode everything and clip right
452 * before pushing data. */
455 if (G_UNLIKELY (vorbis_dsp_synthesis (&vd->vd, packet, 1)))
458 if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
461 if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
465 /* assume all goes well here */
466 result = GST_FLOW_OK;
468 /* count samples ready for reading */
470 if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0)
472 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
476 size = sample_count * vd->info.bpf;
477 GST_LOG_OBJECT (vd, "%d samples ready for reading, size %" G_GSIZE_FORMAT,
480 /* alloc buffer for it */
481 out = gst_buffer_new_allocate (NULL, size, NULL);
483 gst_buffer_map (out, &map, GST_MAP_WRITE);
484 /* get samples ready for reading now, should be sample_count */
486 if (G_UNLIKELY (vorbis_dsp_pcmout (&vd->vd, map.data, sample_count) !=
489 if (G_UNLIKELY (vorbis_synthesis_pcmout (&vd->vd, &pcm) != sample_count))
494 if (vd->info.channels < 9)
495 gst_audio_reorder_channels (map.data, map.size, GST_VORBIS_AUDIO_FORMAT,
496 vd->info.channels, gst_vorbis_channel_positions[vd->info.channels - 1],
497 gst_vorbis_default_channel_positions[vd->info.channels - 1]);
499 /* copy samples in buffer */
500 vd->copy_samples ((vorbis_sample_t *) map.data, pcm,
501 sample_count, vd->info.channels);
504 GST_LOG_OBJECT (vd, "have output size of %" G_GSIZE_FORMAT, size);
505 gst_buffer_unmap (out, &map);
508 /* whether or not data produced, consume one frame and advance time */
509 result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), out, 1);
512 vorbis_dsp_read (&vd->vd, sample_count);
514 vorbis_synthesis_read (&vd->vd, sample_count);
522 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
523 (NULL), ("no header sent yet"));
524 return GST_FLOW_NOT_NEGOTIATED;
528 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
529 (NULL), ("couldn't read data packet"));
530 return GST_FLOW_ERROR;
534 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
535 (NULL), ("vorbis decoder did not accept data packet"));
536 return GST_FLOW_ERROR;
540 gst_buffer_unref (out);
541 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
542 (NULL), ("vorbis decoder reported wrong number of samples"));
543 return GST_FLOW_ERROR;
548 vorbis_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
551 ogg_packet_wrapper packet_wrapper;
552 GstFlowReturn result = GST_FLOW_OK;
554 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
556 /* no draining etc */
557 if (G_UNLIKELY (!buffer))
560 GST_LOG_OBJECT (vd, "got buffer %p", buffer);
561 /* make ogg_packet out of the buffer */
562 gst_ogg_packet_wrapper_map (&packet_wrapper, buffer, &map);
563 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
564 /* set some more stuff */
565 packet->granulepos = -1;
566 packet->packetno = 0; /* we don't care */
567 /* EOS does not matter, it is used in vorbis to implement clipping the last
568 * block of samples based on the granulepos. We clip based on segments. */
571 GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
573 /* error out on empty header packets, but just skip empty data packets */
574 if (G_UNLIKELY (packet->bytes == 0)) {
581 /* switch depending on packet type */
582 if ((gst_ogg_packet_data (packet))[0] & 1) {
583 if (vd->initialized) {
584 GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
587 result = vorbis_handle_header_packet (vd, packet);
588 /* consumer header packet/frame */
589 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), NULL, 1);
591 GstClockTime timestamp, duration;
593 timestamp = GST_BUFFER_TIMESTAMP (buffer);
594 duration = GST_BUFFER_DURATION (buffer);
596 result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
600 GST_LOG_OBJECT (vd, "unmap buffer %p", buffer);
601 gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer, &map);
607 /* don't error out here, just ignore the buffer, it's invalid for vorbis
609 GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
610 result = GST_FLOW_OK;
617 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
618 result = GST_FLOW_ERROR;
624 vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard)
626 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
628 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
629 vorbis_synthesis_restart (&vd->vd);