2 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
3 * Copyright (C) 2006 Andy Wingo <wingo@pobox.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-vorbisparse
23 * @see_also: vorbisdec, oggdemux, theoraparse
25 * The vorbisparse element will parse the header packets of the Vorbis
26 * stream and put them as the streamheader in the caps. This is used in the
27 * multifdsink case where you want to stream live vorbis streams to multiple
28 * clients, each client has to receive the streamheaders first before they can
29 * consume the vorbis packets.
31 * This element also makes sure that the buffers that it pushes out are properly
32 * timestamped and that their offset and offset_end are set. The buffers that
33 * vorbisparse outputs have all of the metadata that oggmux expects to receive,
34 * which allows you to (for example) remux an ogg/vorbis file.
37 * <title>Example pipelines</title>
39 * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisparse ! fakesink
40 * ]| This pipeline shows that the streamheader is set in the caps, and that each
41 * buffer has the timestamp, duration, offset, and offset_end set.
43 * gst-launch filesrc location=sine.ogg ! oggdemux ! vorbisparse \
44 * ! oggmux ! filesink location=sine-remuxed.ogg
45 * ]| This pipeline shows remuxing. sine-remuxed.ogg might not be exactly the same
46 * as sine.ogg, but they should produce exactly the same decoded data.
49 * Last reviewed on 2006-04-01 (0.10.4.1)
56 #include "gstvorbisparse.h"
58 GST_DEBUG_CATEGORY_EXTERN (vorbisparse_debug);
59 #define GST_CAT_DEFAULT vorbisparse_debug
61 static GstStaticPadTemplate vorbis_parse_sink_factory =
62 GST_STATIC_PAD_TEMPLATE ("sink",
65 GST_STATIC_CAPS ("audio/x-vorbis")
68 static GstStaticPadTemplate vorbis_parse_src_factory =
69 GST_STATIC_PAD_TEMPLATE ("src",
72 GST_STATIC_CAPS ("audio/x-vorbis")
75 GST_BOILERPLATE (GstVorbisParse, gst_vorbis_parse, GstElement,
78 static GstFlowReturn vorbis_parse_chain (GstPad * pad, GstBuffer * buffer);
79 static GstStateChangeReturn vorbis_parse_change_state (GstElement * element,
80 GstStateChange transition);
81 static gboolean vorbis_parse_sink_event (GstPad * pad, GstEvent * event);
82 static gboolean vorbis_parse_src_query (GstPad * pad, GstQuery * query);
83 static gboolean vorbis_parse_convert (GstPad * pad,
84 GstFormat src_format, gint64 src_value,
85 GstFormat * dest_format, gint64 * dest_value);
86 static GstFlowReturn vorbis_parse_parse_packet (GstVorbisParse * parse,
90 gst_vorbis_parse_base_init (gpointer g_class)
92 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
94 gst_element_class_add_static_pad_template (element_class,
95 &vorbis_parse_src_factory);
96 gst_element_class_add_static_pad_template (element_class,
97 &vorbis_parse_sink_factory);
98 gst_element_class_set_details_simple (element_class,
99 "VorbisParse", "Codec/Parser/Audio",
100 "parse raw vorbis streams",
101 "Thomas Vander Stichele <thomas at apestaart dot org>");
105 gst_vorbis_parse_class_init (GstVorbisParseClass * klass)
107 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
109 gstelement_class->change_state = vorbis_parse_change_state;
111 klass->parse_packet = GST_DEBUG_FUNCPTR (vorbis_parse_parse_packet);
115 gst_vorbis_parse_init (GstVorbisParse * parse, GstVorbisParseClass * g_class)
118 gst_pad_new_from_static_template (&vorbis_parse_sink_factory, "sink");
119 gst_pad_set_chain_function (parse->sinkpad,
120 GST_DEBUG_FUNCPTR (vorbis_parse_chain));
121 gst_pad_set_event_function (parse->sinkpad,
122 GST_DEBUG_FUNCPTR (vorbis_parse_sink_event));
123 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
126 gst_pad_new_from_static_template (&vorbis_parse_src_factory, "src");
127 gst_pad_set_query_function (parse->srcpad,
128 GST_DEBUG_FUNCPTR (vorbis_parse_src_query));
129 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
133 vorbis_parse_set_header_on_caps (GstVorbisParse * parse, GstCaps * caps)
135 GstBuffer *buf1, *buf2, *buf3;
136 GstStructure *structure;
137 GValue array = { 0 };
138 GValue value = { 0 };
141 g_assert (parse->streamheader);
142 g_assert (parse->streamheader->next);
143 g_assert (parse->streamheader->next->next);
144 buf1 = parse->streamheader->data;
146 buf2 = parse->streamheader->next->data;
148 buf3 = parse->streamheader->next->next->data;
151 structure = gst_caps_get_structure (caps, 0);
154 GST_BUFFER_FLAG_SET (buf1, GST_BUFFER_FLAG_IN_CAPS);
155 GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_IN_CAPS);
156 GST_BUFFER_FLAG_SET (buf3, GST_BUFFER_FLAG_IN_CAPS);
158 /* put buffers in a fixed list */
159 g_value_init (&array, GST_TYPE_ARRAY);
160 g_value_init (&value, GST_TYPE_BUFFER);
161 gst_value_set_buffer (&value, buf1);
162 gst_value_array_append_value (&array, &value);
163 g_value_unset (&value);
164 g_value_init (&value, GST_TYPE_BUFFER);
165 gst_value_set_buffer (&value, buf2);
166 gst_value_array_append_value (&array, &value);
167 g_value_unset (&value);
168 g_value_init (&value, GST_TYPE_BUFFER);
169 gst_value_set_buffer (&value, buf3);
170 gst_value_array_append_value (&array, &value);
171 gst_structure_set_value (structure, "streamheader", &array);
172 g_value_unset (&value);
173 g_value_unset (&array);
177 vorbis_parse_drain_event_queue (GstVorbisParse * parse)
179 while (parse->event_queue->length) {
182 event = GST_EVENT_CAST (g_queue_pop_head (parse->event_queue));
183 gst_pad_event_default (parse->sinkpad, event);
188 vorbis_parse_push_headers (GstVorbisParse * parse)
190 /* mark and put on caps */
192 GstBuffer *outbuf, *outbuf1, *outbuf2, *outbuf3;
195 /* get the headers into the caps, passing them to vorbis as we go */
196 caps = gst_caps_make_writable (gst_pad_get_caps (parse->srcpad));
197 vorbis_parse_set_header_on_caps (parse, caps);
198 GST_DEBUG_OBJECT (parse, "here are the caps: %" GST_PTR_FORMAT, caps);
199 gst_pad_set_caps (parse->srcpad, caps);
200 gst_caps_unref (caps);
202 outbuf = GST_BUFFER_CAST (parse->streamheader->data);
203 packet.packet = GST_BUFFER_DATA (outbuf);
204 packet.bytes = GST_BUFFER_SIZE (outbuf);
205 packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
209 vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
210 parse->sample_rate = parse->vi.rate;
213 outbuf = GST_BUFFER_CAST (parse->streamheader->next->data);
214 packet.packet = GST_BUFFER_DATA (outbuf);
215 packet.bytes = GST_BUFFER_SIZE (outbuf);
216 packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
220 vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
223 outbuf = GST_BUFFER_CAST (parse->streamheader->next->next->data);
224 packet.packet = GST_BUFFER_DATA (outbuf);
225 packet.bytes = GST_BUFFER_SIZE (outbuf);
226 packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
230 vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
233 /* first process queued events */
234 vorbis_parse_drain_event_queue (parse);
236 /* push out buffers, ignoring return value... */
237 outbuf1 = gst_buffer_make_metadata_writable (outbuf1);
238 gst_buffer_set_caps (outbuf1, GST_PAD_CAPS (parse->srcpad));
239 gst_pad_push (parse->srcpad, outbuf1);
240 outbuf2 = gst_buffer_make_metadata_writable (outbuf2);
241 gst_buffer_set_caps (outbuf2, GST_PAD_CAPS (parse->srcpad));
242 gst_pad_push (parse->srcpad, outbuf2);
243 outbuf3 = gst_buffer_make_metadata_writable (outbuf3);
244 gst_buffer_set_caps (outbuf3, GST_PAD_CAPS (parse->srcpad));
245 gst_pad_push (parse->srcpad, outbuf3);
247 g_list_free (parse->streamheader);
248 parse->streamheader = NULL;
252 vorbis_parse_clear_queue (GstVorbisParse * parse)
254 while (parse->buffer_queue->length) {
257 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
258 gst_buffer_unref (buf);
260 while (parse->event_queue->length) {
263 event = GST_EVENT_CAST (g_queue_pop_head (parse->event_queue));
264 gst_event_unref (event);
269 vorbis_parse_push_buffer (GstVorbisParse * parse, GstBuffer * buf,
274 /* our hack as noted below */
275 samples = GST_BUFFER_OFFSET (buf);
277 GST_BUFFER_OFFSET_END (buf) = granulepos;
278 GST_BUFFER_DURATION (buf) = samples * GST_SECOND / parse->sample_rate;
279 GST_BUFFER_OFFSET (buf) = granulepos * GST_SECOND / parse->sample_rate;
280 GST_BUFFER_TIMESTAMP (buf) =
281 GST_BUFFER_OFFSET (buf) - GST_BUFFER_DURATION (buf);
283 gst_buffer_set_caps (buf, GST_PAD_CAPS (parse->srcpad));
285 return gst_pad_push (parse->srcpad, buf);
289 vorbis_parse_drain_queue_prematurely (GstVorbisParse * parse)
291 GstFlowReturn ret = GST_FLOW_OK;
292 gint64 granulepos = MAX (parse->prev_granulepos, 0);
294 /* got an EOS event, make sure to push out any buffers that were in the queue
295 * -- won't normally be the case, but this catches the
296 * didn't-get-a-granulepos-on-the-last-packet case. Assuming a continuous
299 /* if we got EOS before any buffers came, go ahead and push the other events
301 vorbis_parse_drain_event_queue (parse);
303 while (!g_queue_is_empty (parse->buffer_queue)) {
306 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
308 granulepos += GST_BUFFER_OFFSET (buf);
309 ret = vorbis_parse_push_buffer (parse, buf, granulepos);
311 if (ret != GST_FLOW_OK)
315 parse->prev_granulepos = granulepos;
322 vorbis_parse_drain_queue (GstVorbisParse * parse, gint64 granulepos)
324 GstFlowReturn ret = GST_FLOW_OK;
326 gint64 cur = granulepos;
329 for (walk = parse->buffer_queue->head; walk; walk = walk->next)
330 cur -= GST_BUFFER_OFFSET (walk->data);
332 if (parse->prev_granulepos != -1)
333 cur = MAX (cur, parse->prev_granulepos);
335 while (!g_queue_is_empty (parse->buffer_queue)) {
338 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
340 cur += GST_BUFFER_OFFSET (buf);
341 gp = CLAMP (cur, 0, granulepos);
343 ret = vorbis_parse_push_buffer (parse, buf, gp);
345 if (ret != GST_FLOW_OK)
349 parse->prev_granulepos = granulepos;
356 vorbis_parse_queue_buffer (GstVorbisParse * parse, GstBuffer * buf)
358 GstFlowReturn ret = GST_FLOW_OK;
362 buf = gst_buffer_make_metadata_writable (buf);
364 packet.packet = GST_BUFFER_DATA (buf);
365 packet.bytes = GST_BUFFER_SIZE (buf);
366 packet.granulepos = GST_BUFFER_OFFSET_END (buf);
367 packet.packetno = parse->packetno + parse->buffer_queue->length;
370 blocksize = vorbis_packet_blocksize (&parse->vi, &packet);
372 /* temporarily store the sample count in OFFSET -- we overwrite this later */
374 if (parse->prev_blocksize < 0)
375 GST_BUFFER_OFFSET (buf) = 0;
377 GST_BUFFER_OFFSET (buf) = (blocksize + parse->prev_blocksize) / 4;
379 parse->prev_blocksize = blocksize;
381 g_queue_push_tail (parse->buffer_queue, buf);
383 if (GST_BUFFER_OFFSET_END_IS_VALID (buf))
384 ret = vorbis_parse_drain_queue (parse, GST_BUFFER_OFFSET_END (buf));
390 vorbis_parse_parse_packet (GstVorbisParse * parse, GstBuffer * buf)
395 gboolean have_header;
397 data = GST_BUFFER_DATA (buf);
398 size = GST_BUFFER_SIZE (buf);
409 if (!parse->streamheader_sent) {
410 /* we need to collect the headers still */
411 /* so put it on the streamheader list and return */
412 parse->streamheader = g_list_append (parse->streamheader, buf);
416 /* data packet, push the headers we collected before */
417 if (!parse->streamheader_sent) {
418 vorbis_parse_push_headers (parse);
419 parse->streamheader_sent = TRUE;
421 ret = vorbis_parse_queue_buffer (parse, buf);
428 vorbis_parse_chain (GstPad * pad, GstBuffer * buffer)
430 GstVorbisParseClass *klass;
431 GstVorbisParse *parse;
433 parse = GST_VORBIS_PARSE (GST_PAD_PARENT (pad));
434 klass = GST_VORBIS_PARSE_CLASS (G_OBJECT_GET_CLASS (parse));
436 g_assert (klass->parse_packet != NULL);
438 return klass->parse_packet (parse, buffer);
442 vorbis_parse_queue_event (GstVorbisParse * parse, GstEvent * event)
444 GstFlowReturn ret = TRUE;
446 g_queue_push_tail (parse->event_queue, event);
452 vorbis_parse_sink_event (GstPad * pad, GstEvent * event)
455 GstVorbisParse *parse;
457 parse = GST_VORBIS_PARSE (gst_pad_get_parent (pad));
459 switch (GST_EVENT_TYPE (event)) {
460 case GST_EVENT_FLUSH_START:
461 vorbis_parse_clear_queue (parse);
462 parse->prev_granulepos = -1;
463 parse->prev_blocksize = -1;
464 ret = gst_pad_event_default (pad, event);
467 vorbis_parse_drain_queue_prematurely (parse);
468 ret = gst_pad_event_default (pad, event);
471 if (!parse->streamheader_sent && GST_EVENT_IS_SERIALIZED (event))
472 ret = vorbis_parse_queue_event (parse, event);
474 ret = gst_pad_event_default (pad, event);
478 gst_object_unref (parse);
484 vorbis_parse_convert (GstPad * pad,
485 GstFormat src_format, gint64 src_value,
486 GstFormat * dest_format, gint64 * dest_value)
489 GstVorbisParse *parse;
492 parse = GST_VORBIS_PARSE (GST_PAD_PARENT (pad));
494 /* fixme: assumes atomic access to lots of instance variables modified from
495 * the streaming thread, including 64-bit variables */
497 if (parse->packetno < 4)
500 if (src_format == *dest_format) {
501 *dest_value = src_value;
505 if (parse->sinkpad == pad &&
506 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
509 switch (src_format) {
510 case GST_FORMAT_TIME:
511 switch (*dest_format) {
512 case GST_FORMAT_BYTES:
513 scale = sizeof (float) * parse->vi.channels;
514 case GST_FORMAT_DEFAULT:
516 scale * gst_util_uint64_scale_int (src_value, parse->vi.rate,
523 case GST_FORMAT_DEFAULT:
524 switch (*dest_format) {
525 case GST_FORMAT_BYTES:
526 *dest_value = src_value * sizeof (float) * parse->vi.channels;
528 case GST_FORMAT_TIME:
530 gst_util_uint64_scale_int (src_value, GST_SECOND, parse->vi.rate);
536 case GST_FORMAT_BYTES:
537 switch (*dest_format) {
538 case GST_FORMAT_DEFAULT:
539 *dest_value = src_value / (sizeof (float) * parse->vi.channels);
541 case GST_FORMAT_TIME:
542 *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
543 parse->vi.rate * sizeof (float) * parse->vi.channels);
557 vorbis_parse_src_query (GstPad * pad, GstQuery * query)
560 GstVorbisParse *parse;
561 gboolean res = FALSE;
563 parse = GST_VORBIS_PARSE (GST_PAD_PARENT (pad));
565 switch (GST_QUERY_TYPE (query)) {
566 case GST_QUERY_POSITION:
571 granulepos = parse->prev_granulepos;
573 gst_query_parse_position (query, &format, NULL);
575 /* and convert to the final format */
577 vorbis_parse_convert (pad, GST_FORMAT_DEFAULT, granulepos,
581 /* fixme: support segments
582 value = (value - parse->segment_start) + parse->segment_time;
585 gst_query_set_position (query, format, value);
587 GST_LOG_OBJECT (parse, "query %p: peer returned granulepos: %"
588 G_GUINT64_FORMAT " - we return %" G_GUINT64_FORMAT " (format %u)",
589 query, granulepos, value, format);
593 case GST_QUERY_DURATION:
595 /* fixme: not threadsafe */
596 /* query peer for total length */
597 if (!gst_pad_is_linked (parse->sinkpad)) {
598 GST_WARNING_OBJECT (parse, "sink pad %" GST_PTR_FORMAT " is not linked",
602 if (!(res = gst_pad_query (GST_PAD_PEER (parse->sinkpad), query)))
606 case GST_QUERY_CONVERT:
608 GstFormat src_fmt, dest_fmt;
609 gint64 src_val, dest_val;
611 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
613 vorbis_parse_convert (pad, src_fmt, src_val, &dest_fmt,
616 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
620 res = gst_pad_query_default (pad, query);
627 GST_WARNING_OBJECT (parse, "error handling query");
632 static GstStateChangeReturn
633 vorbis_parse_change_state (GstElement * element, GstStateChange transition)
635 GstVorbisParse *parse = GST_VORBIS_PARSE (element);
636 GstStateChangeReturn ret;
638 switch (transition) {
639 case GST_STATE_CHANGE_READY_TO_PAUSED:
640 vorbis_info_init (&parse->vi);
641 vorbis_comment_init (&parse->vc);
642 parse->prev_granulepos = -1;
643 parse->prev_blocksize = -1;
645 parse->streamheader_sent = FALSE;
646 parse->buffer_queue = g_queue_new ();
647 parse->event_queue = g_queue_new ();
653 ret = parent_class->change_state (element, transition);
655 switch (transition) {
656 case GST_STATE_CHANGE_PAUSED_TO_READY:
657 vorbis_info_clear (&parse->vi);
658 vorbis_comment_clear (&parse->vc);
659 vorbis_parse_clear_queue (parse);
660 g_queue_free (parse->buffer_queue);
661 parse->buffer_queue = NULL;
662 g_queue_free (parse->event_queue);
663 parse->event_queue = NULL;