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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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.
41 #include "gstvorbisdec.h"
43 #include <gst/audio/audio.h>
44 #include <gst/tag/tag.h>
46 #include "gstvorbiscommon.h"
49 GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug);
50 #define GST_CAT_DEFAULT vorbisdec_debug
52 GST_DEBUG_CATEGORY_EXTERN (ivorbisdec_debug);
53 #define GST_CAT_DEFAULT ivorbisdec_debug
56 static GstStaticPadTemplate vorbis_dec_src_factory =
57 GST_STATIC_PAD_TEMPLATE ("src",
60 GST_VORBIS_DEC_SRC_CAPS);
62 static GstStaticPadTemplate vorbis_dec_sink_factory =
63 GST_STATIC_PAD_TEMPLATE ("sink",
66 GST_STATIC_CAPS ("audio/x-vorbis")
69 #define gst_vorbis_dec_parent_class parent_class
70 G_DEFINE_TYPE (GstVorbisDec, gst_vorbis_dec, GST_TYPE_AUDIO_DECODER);
72 static void vorbis_dec_finalize (GObject * object);
74 static gboolean vorbis_dec_start (GstAudioDecoder * dec);
75 static gboolean vorbis_dec_stop (GstAudioDecoder * dec);
76 static GstFlowReturn vorbis_dec_handle_frame (GstAudioDecoder * dec,
78 static void vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard);
81 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
83 GstPadTemplate *src_template, *sink_template;
84 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
85 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
86 GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
88 gobject_class->finalize = vorbis_dec_finalize;
90 src_template = gst_static_pad_template_get (&vorbis_dec_src_factory);
91 gst_element_class_add_pad_template (element_class, src_template);
93 sink_template = gst_static_pad_template_get (&vorbis_dec_sink_factory);
94 gst_element_class_add_pad_template (element_class, sink_template);
96 gst_element_class_set_static_metadata (element_class,
97 "Vorbis audio decoder", "Codec/Decoder/Audio",
98 GST_VORBIS_DEC_DESCRIPTION,
99 "Benjamin Otte <otte@gnome.org>, Chris Lord <chris@openedhand.com>");
101 base_class->start = GST_DEBUG_FUNCPTR (vorbis_dec_start);
102 base_class->stop = GST_DEBUG_FUNCPTR (vorbis_dec_stop);
103 base_class->handle_frame = GST_DEBUG_FUNCPTR (vorbis_dec_handle_frame);
104 base_class->flush = GST_DEBUG_FUNCPTR (vorbis_dec_flush);
108 gst_vorbis_dec_init (GstVorbisDec * dec)
113 vorbis_dec_finalize (GObject * object)
115 /* Release any possibly allocated libvorbis data.
116 * _clear functions can safely be called multiple times
118 GstVorbisDec *vd = GST_VORBIS_DEC (object);
121 vorbis_block_clear (&vd->vb);
123 vorbis_dsp_clear (&vd->vd);
124 vorbis_comment_clear (&vd->vc);
125 vorbis_info_clear (&vd->vi);
127 G_OBJECT_CLASS (parent_class)->finalize (object);
131 vorbis_dec_start (GstAudioDecoder * dec)
133 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
135 GST_DEBUG_OBJECT (dec, "start");
136 vorbis_info_init (&vd->vi);
137 vorbis_comment_init (&vd->vc);
138 vd->initialized = FALSE;
144 vorbis_dec_stop (GstAudioDecoder * dec)
146 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
148 GST_DEBUG_OBJECT (dec, "stop");
149 vd->initialized = FALSE;
151 vorbis_block_clear (&vd->vb);
153 vorbis_dsp_clear (&vd->vd);
154 vorbis_comment_clear (&vd->vc);
155 vorbis_info_clear (&vd->vi);
161 vorbis_handle_identification_packet (GstVorbisDec * vd)
165 switch (vd->vi.channels) {
175 const GstAudioChannelPosition *pos;
177 pos = gst_vorbis_default_channel_positions[vd->vi.channels - 1];
178 gst_audio_info_set_format (&info, GST_VORBIS_AUDIO_FORMAT, vd->vi.rate,
179 vd->vi.channels, pos);
183 GstAudioChannelPosition position[64];
184 gint i, max_pos = MAX (vd->vi.channels, 64);
186 GST_ELEMENT_WARNING (vd, STREAM, DECODE,
187 (NULL), ("Using NONE channel layout for more than 8 channels"));
188 for (i = 0; i < max_pos; i++)
189 position[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
190 gst_audio_info_set_format (&info, GST_VORBIS_AUDIO_FORMAT, vd->vi.rate,
191 vd->vi.channels, position);
196 gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (vd), &info);
199 /* select a copy_samples function, this way we can have specialized versions
200 * for mono/stereo and avoid the depth switch in tremor case */
201 vd->copy_samples = gst_vorbis_get_copy_sample_func (info.channels);
206 /* FIXME 0.11: remove tag handling and let container take care of that? */
208 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
211 gchar *encoder = NULL;
216 GST_DEBUG_OBJECT (vd, "parsing comment packet");
218 data = gst_ogg_packet_data (packet);
219 size = gst_ogg_packet_size (packet);
222 gst_tag_list_from_vorbiscomment (data, size, (guint8 *) "\003vorbis", 7,
226 GST_ERROR_OBJECT (vd, "couldn't decode comments");
227 list = gst_tag_list_new_empty ();
232 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
233 GST_TAG_ENCODER, encoder, NULL);
236 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
237 GST_TAG_ENCODER_VERSION, vd->vi.version,
238 GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
239 if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
240 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
241 GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
242 bitrate = vd->vi.bitrate_nominal;
244 if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
245 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
246 GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
248 bitrate = vd->vi.bitrate_upper;
250 if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
251 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
252 GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
254 bitrate = vd->vi.bitrate_lower;
257 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
258 GST_TAG_BITRATE, (guint) bitrate, NULL);
261 gst_audio_decoder_merge_tags (GST_AUDIO_DECODER_CAST (vd), list,
262 GST_TAG_MERGE_REPLACE);
263 gst_tag_list_unref (list);
269 vorbis_handle_type_packet (GstVorbisDec * vd)
273 g_assert (vd->initialized == FALSE);
276 if (G_UNLIKELY ((res = vorbis_dsp_init (&vd->vd, &vd->vi))))
277 goto synthesis_init_error;
279 if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
280 goto synthesis_init_error;
282 if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
283 goto block_init_error;
286 vd->initialized = TRUE;
291 synthesis_init_error:
293 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
294 (NULL), ("couldn't initialize synthesis (%d)", res));
295 return GST_FLOW_ERROR;
299 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
300 (NULL), ("couldn't initialize block (%d)", res));
301 return GST_FLOW_ERROR;
306 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
311 GST_DEBUG_OBJECT (vd, "parsing header packet");
313 /* Packetno = 0 if the first byte is exactly 0x01 */
314 packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
317 if ((ret = vorbis_dsp_headerin (&vd->vi, &vd->vc, packet)))
319 if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
321 goto header_read_error;
323 switch ((gst_ogg_packet_data (packet))[0]) {
325 res = vorbis_handle_identification_packet (vd);
328 res = vorbis_handle_comment_packet (vd, packet);
331 res = vorbis_handle_type_packet (vd);
335 g_warning ("unknown vorbis header packet found");
345 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
346 (NULL), ("couldn't read header packet (%d)", ret));
347 return GST_FLOW_ERROR;
352 vorbis_dec_handle_header_buffer (GstVorbisDec * vd, GstBuffer * buffer)
355 ogg_packet_wrapper packet_wrapper;
359 gst_ogg_packet_wrapper_map (&packet_wrapper, buffer, &map);
360 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
362 ret = vorbis_handle_header_packet (vd, packet);
364 gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer, &map);
369 #define MIN_NUM_HEADERS 3
371 vorbis_dec_handle_header_caps (GstVorbisDec * vd)
373 GstFlowReturn result = GST_FLOW_OK;
375 GstStructure *s = NULL;
376 const GValue *array = NULL;
378 caps = gst_pad_get_current_caps (GST_AUDIO_DECODER_SINK_PAD (vd));
380 s = gst_caps_get_structure (caps, 0);
382 array = gst_structure_get_value (s, "streamheader");
385 gst_caps_unref (caps);
387 if (array && (gst_value_array_get_size (array) >= MIN_NUM_HEADERS)) {
388 const GValue *value = NULL;
389 GstBuffer *buf = NULL;
392 while (result == GST_FLOW_OK && i < gst_value_array_get_size (array)) {
393 value = gst_value_array_get_value (array, i);
394 buf = gst_value_get_buffer (value);
397 result = vorbis_dec_handle_header_buffer (vd, buf);
404 return (result != GST_FLOW_OK ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_OK);
409 GST_WARNING_OBJECT (vd, "streamheader array not found");
410 result = GST_FLOW_ERROR;
415 GST_WARNING_OBJECT (vd, "streamheader with null buffer received");
416 result = GST_FLOW_ERROR;
423 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
424 GstClockTime timestamp, GstClockTime duration)
427 vorbis_sample_t *pcm;
429 vorbis_sample_t **pcm;
432 GstBuffer *out = NULL;
433 GstFlowReturn result;
437 if (G_UNLIKELY (!vd->initialized)) {
438 result = vorbis_dec_handle_header_caps (vd);
439 if (result != GST_FLOW_OK)
440 goto not_initialized;
443 /* normal data packet */
444 /* FIXME, we can skip decoding if the packet is outside of the
445 * segment, this is however not very trivial as we need a previous
446 * packet to decode the current one so we must be careful not to
447 * throw away too much. For now we decode everything and clip right
448 * before pushing data. */
451 if (G_UNLIKELY (vorbis_dsp_synthesis (&vd->vd, packet, 1)))
454 if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
457 if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
461 /* assume all goes well here */
462 result = GST_FLOW_OK;
464 /* count samples ready for reading */
466 if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0)
468 if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
472 size = sample_count * vd->info.bpf;
473 GST_LOG_OBJECT (vd, "%d samples ready for reading, size %" G_GSIZE_FORMAT,
476 /* alloc buffer for it */
477 out = gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (vd), size);
479 gst_buffer_map (out, &map, GST_MAP_WRITE);
480 /* get samples ready for reading now, should be sample_count */
482 if (G_UNLIKELY (vorbis_dsp_pcmout (&vd->vd, map.data, sample_count) !=
485 if (G_UNLIKELY (vorbis_synthesis_pcmout (&vd->vd, &pcm) != sample_count))
490 if (vd->info.channels < 9)
491 gst_audio_reorder_channels (map.data, map.size, GST_VORBIS_AUDIO_FORMAT,
492 vd->info.channels, gst_vorbis_channel_positions[vd->info.channels - 1],
493 gst_vorbis_default_channel_positions[vd->info.channels - 1]);
495 /* copy samples in buffer */
496 vd->copy_samples ((vorbis_sample_t *) map.data, pcm,
497 sample_count, vd->info.channels);
500 GST_LOG_OBJECT (vd, "have output size of %" G_GSIZE_FORMAT, size);
501 gst_buffer_unmap (out, &map);
504 /* whether or not data produced, consume one frame and advance time */
505 result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), out, 1);
508 vorbis_dsp_read (&vd->vd, sample_count);
510 vorbis_synthesis_read (&vd->vd, sample_count);
518 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
519 (NULL), ("no header sent yet"));
520 return GST_FLOW_NOT_NEGOTIATED;
524 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
525 (NULL), ("couldn't read data packet"));
526 return GST_FLOW_ERROR;
530 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
531 (NULL), ("vorbis decoder did not accept data packet"));
532 return GST_FLOW_ERROR;
536 gst_buffer_unref (out);
537 GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
538 (NULL), ("vorbis decoder reported wrong number of samples"));
539 return GST_FLOW_ERROR;
544 vorbis_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
547 ogg_packet_wrapper packet_wrapper;
548 GstFlowReturn result = GST_FLOW_OK;
550 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
552 /* no draining etc */
553 if (G_UNLIKELY (!buffer))
556 GST_LOG_OBJECT (vd, "got buffer %p", buffer);
557 /* make ogg_packet out of the buffer */
558 gst_ogg_packet_wrapper_map (&packet_wrapper, buffer, &map);
559 packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
560 /* set some more stuff */
561 packet->granulepos = -1;
562 packet->packetno = 0; /* we don't care */
563 /* EOS does not matter, it is used in vorbis to implement clipping the last
564 * block of samples based on the granulepos. We clip based on segments. */
567 GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
569 /* error out on empty header packets, but just skip empty data packets */
570 if (G_UNLIKELY (packet->bytes == 0)) {
577 /* switch depending on packet type */
578 if ((gst_ogg_packet_data (packet))[0] & 1) {
579 if (vd->initialized) {
580 GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
583 result = vorbis_handle_header_packet (vd, packet);
584 if (result != GST_FLOW_OK)
586 /* consumer header packet/frame */
587 result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), NULL, 1);
589 GstClockTime timestamp, duration;
591 timestamp = GST_BUFFER_TIMESTAMP (buffer);
592 duration = GST_BUFFER_DURATION (buffer);
594 result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
598 GST_LOG_OBJECT (vd, "unmap buffer %p", buffer);
599 gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer, &map);
605 /* don't error out here, just ignore the buffer, it's invalid for vorbis
607 GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
608 result = GST_FLOW_OK;
615 GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
616 result = GST_FLOW_ERROR;
622 vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard)
624 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
625 GstVorbisDec *vd = GST_VORBIS_DEC (dec);
627 vorbis_synthesis_restart (&vd->vd);