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 #define gst_vorbis_parse_parent_class parent_class
76 G_DEFINE_TYPE (GstVorbisParse, gst_vorbis_parse, GST_TYPE_ELEMENT);
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_class_init (GstVorbisParseClass * klass)
92 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
94 gstelement_class->change_state = vorbis_parse_change_state;
96 gst_element_class_add_pad_template (gstelement_class,
97 gst_static_pad_template_get (&vorbis_parse_src_factory));
98 gst_element_class_add_pad_template (gstelement_class,
99 gst_static_pad_template_get (&vorbis_parse_sink_factory));
100 gst_element_class_set_details_simple (gstelement_class,
101 "VorbisParse", "Codec/Parser/Audio",
102 "parse raw vorbis streams",
103 "Thomas Vander Stichele <thomas at apestaart dot org>");
105 klass->parse_packet = GST_DEBUG_FUNCPTR (vorbis_parse_parse_packet);
109 gst_vorbis_parse_init (GstVorbisParse * parse)
112 gst_pad_new_from_static_template (&vorbis_parse_sink_factory, "sink");
113 gst_pad_set_chain_function (parse->sinkpad,
114 GST_DEBUG_FUNCPTR (vorbis_parse_chain));
115 gst_pad_set_event_function (parse->sinkpad,
116 GST_DEBUG_FUNCPTR (vorbis_parse_sink_event));
117 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
120 gst_pad_new_from_static_template (&vorbis_parse_src_factory, "src");
121 gst_pad_set_query_function (parse->srcpad,
122 GST_DEBUG_FUNCPTR (vorbis_parse_src_query));
123 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
127 vorbis_parse_set_header_on_caps (GstVorbisParse * parse, GstCaps * caps)
129 GstBuffer *buf1, *buf2, *buf3;
130 GstStructure *structure;
131 GValue array = { 0 };
132 GValue value = { 0 };
135 g_assert (parse->streamheader);
136 g_assert (parse->streamheader->next);
137 g_assert (parse->streamheader->next->next);
138 buf1 = parse->streamheader->data;
140 buf2 = parse->streamheader->next->data;
142 buf3 = parse->streamheader->next->next->data;
145 structure = gst_caps_get_structure (caps, 0);
148 GST_BUFFER_FLAG_SET (buf1, GST_BUFFER_FLAG_IN_CAPS);
149 GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_IN_CAPS);
150 GST_BUFFER_FLAG_SET (buf3, GST_BUFFER_FLAG_IN_CAPS);
152 /* put buffers in a fixed list */
153 g_value_init (&array, GST_TYPE_ARRAY);
154 g_value_init (&value, GST_TYPE_BUFFER);
155 gst_value_set_buffer (&value, buf1);
156 gst_value_array_append_value (&array, &value);
157 g_value_unset (&value);
158 g_value_init (&value, GST_TYPE_BUFFER);
159 gst_value_set_buffer (&value, buf2);
160 gst_value_array_append_value (&array, &value);
161 g_value_unset (&value);
162 g_value_init (&value, GST_TYPE_BUFFER);
163 gst_value_set_buffer (&value, buf3);
164 gst_value_array_append_value (&array, &value);
165 gst_structure_set_value (structure, "streamheader", &array);
166 g_value_unset (&value);
167 g_value_unset (&array);
171 vorbis_parse_drain_event_queue (GstVorbisParse * parse)
173 while (parse->event_queue->length) {
176 event = GST_EVENT_CAST (g_queue_pop_head (parse->event_queue));
177 gst_pad_event_default (parse->sinkpad, event);
182 vorbis_parse_push_headers (GstVorbisParse * parse)
184 /* mark and put on caps */
186 GstBuffer *outbuf, *outbuf1, *outbuf2, *outbuf3;
190 /* get the headers into the caps, passing them to vorbis as we go */
191 caps = gst_caps_make_writable (gst_pad_query_caps (parse->srcpad, NULL));
192 vorbis_parse_set_header_on_caps (parse, caps);
193 GST_DEBUG_OBJECT (parse, "here are the caps: %" GST_PTR_FORMAT, caps);
194 gst_pad_set_caps (parse->srcpad, caps);
195 gst_caps_unref (caps);
197 outbuf = GST_BUFFER_CAST (parse->streamheader->data);
198 packet.packet = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READ);
200 packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
204 vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
205 gst_buffer_unmap (outbuf, packet.packet, size);
206 parse->sample_rate = parse->vi.rate;
209 outbuf = GST_BUFFER_CAST (parse->streamheader->next->data);
210 packet.packet = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READ);
212 packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
216 vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
217 gst_buffer_unmap (outbuf, packet.packet, size);
220 outbuf = GST_BUFFER_CAST (parse->streamheader->next->next->data);
221 packet.packet = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READ);
223 packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
227 vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
228 gst_buffer_unmap (outbuf, packet.packet, size);
231 /* first process queued events */
232 vorbis_parse_drain_event_queue (parse);
234 /* push out buffers, ignoring return value... */
235 gst_pad_push (parse->srcpad, outbuf1);
236 gst_pad_push (parse->srcpad, outbuf2);
237 gst_pad_push (parse->srcpad, outbuf3);
239 g_list_free (parse->streamheader);
240 parse->streamheader = NULL;
244 vorbis_parse_clear_queue (GstVorbisParse * parse)
246 while (parse->buffer_queue->length) {
249 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
250 gst_buffer_unref (buf);
252 while (parse->event_queue->length) {
255 event = GST_EVENT_CAST (g_queue_pop_head (parse->event_queue));
256 gst_event_unref (event);
261 vorbis_parse_push_buffer (GstVorbisParse * parse, GstBuffer * buf,
266 /* our hack as noted below */
267 samples = GST_BUFFER_OFFSET (buf);
269 GST_BUFFER_OFFSET_END (buf) = granulepos;
270 GST_BUFFER_DURATION (buf) = samples * GST_SECOND / parse->sample_rate;
271 GST_BUFFER_OFFSET (buf) = granulepos * GST_SECOND / parse->sample_rate;
272 GST_BUFFER_TIMESTAMP (buf) =
273 GST_BUFFER_OFFSET (buf) - GST_BUFFER_DURATION (buf);
275 return gst_pad_push (parse->srcpad, buf);
279 vorbis_parse_drain_queue_prematurely (GstVorbisParse * parse)
281 GstFlowReturn ret = GST_FLOW_OK;
282 gint64 granulepos = MAX (parse->prev_granulepos, 0);
284 /* got an EOS event, make sure to push out any buffers that were in the queue
285 * -- won't normally be the case, but this catches the
286 * didn't-get-a-granulepos-on-the-last-packet case. Assuming a continuous
289 /* if we got EOS before any buffers came, go ahead and push the other events
291 vorbis_parse_drain_event_queue (parse);
293 while (!g_queue_is_empty (parse->buffer_queue)) {
296 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
298 granulepos += GST_BUFFER_OFFSET (buf);
299 ret = vorbis_parse_push_buffer (parse, buf, granulepos);
301 if (ret != GST_FLOW_OK)
305 parse->prev_granulepos = granulepos;
312 vorbis_parse_drain_queue (GstVorbisParse * parse, gint64 granulepos)
314 GstFlowReturn ret = GST_FLOW_OK;
316 gint64 cur = granulepos;
319 for (walk = parse->buffer_queue->head; walk; walk = walk->next)
320 cur -= GST_BUFFER_OFFSET (walk->data);
322 if (parse->prev_granulepos != -1)
323 cur = MAX (cur, parse->prev_granulepos);
325 while (!g_queue_is_empty (parse->buffer_queue)) {
328 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
330 cur += GST_BUFFER_OFFSET (buf);
331 gp = CLAMP (cur, 0, granulepos);
333 ret = vorbis_parse_push_buffer (parse, buf, gp);
335 if (ret != GST_FLOW_OK)
339 parse->prev_granulepos = granulepos;
346 vorbis_parse_queue_buffer (GstVorbisParse * parse, GstBuffer * buf)
348 GstFlowReturn ret = GST_FLOW_OK;
353 buf = gst_buffer_make_writable (buf);
355 packet.packet = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
357 packet.granulepos = GST_BUFFER_OFFSET_END (buf);
358 packet.packetno = parse->packetno + parse->buffer_queue->length;
361 blocksize = vorbis_packet_blocksize (&parse->vi, &packet);
362 gst_buffer_unmap (buf, packet.packet, size);
364 /* temporarily store the sample count in OFFSET -- we overwrite this later */
366 if (parse->prev_blocksize < 0)
367 GST_BUFFER_OFFSET (buf) = 0;
369 GST_BUFFER_OFFSET (buf) = (blocksize + parse->prev_blocksize) / 4;
371 parse->prev_blocksize = blocksize;
373 g_queue_push_tail (parse->buffer_queue, buf);
375 if (GST_BUFFER_OFFSET_END_IS_VALID (buf))
376 ret = vorbis_parse_drain_queue (parse, GST_BUFFER_OFFSET_END (buf));
382 vorbis_parse_parse_packet (GstVorbisParse * parse, GstBuffer * buf)
387 gboolean have_header;
392 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
394 if (data[0] >= 0x01 && data[0] <= 0x05)
397 gst_buffer_unmap (buf, data, size);
400 if (!parse->streamheader_sent) {
401 /* we need to collect the headers still */
402 /* so put it on the streamheader list and return */
403 parse->streamheader = g_list_append (parse->streamheader, buf);
407 /* data packet, push the headers we collected before */
408 if (!parse->streamheader_sent) {
409 vorbis_parse_push_headers (parse);
410 parse->streamheader_sent = TRUE;
412 ret = vorbis_parse_queue_buffer (parse, buf);
419 vorbis_parse_chain (GstPad * pad, GstBuffer * buffer)
421 GstVorbisParseClass *klass;
422 GstVorbisParse *parse;
424 parse = GST_VORBIS_PARSE (GST_PAD_PARENT (pad));
425 klass = GST_VORBIS_PARSE_CLASS (G_OBJECT_GET_CLASS (parse));
427 g_assert (klass->parse_packet != NULL);
429 return klass->parse_packet (parse, buffer);
433 vorbis_parse_queue_event (GstVorbisParse * parse, GstEvent * event)
435 GstFlowReturn ret = TRUE;
437 g_queue_push_tail (parse->event_queue, event);
443 vorbis_parse_sink_event (GstPad * pad, GstEvent * event)
446 GstVorbisParse *parse;
448 parse = GST_VORBIS_PARSE (gst_pad_get_parent (pad));
450 switch (GST_EVENT_TYPE (event)) {
451 case GST_EVENT_FLUSH_START:
452 vorbis_parse_clear_queue (parse);
453 parse->prev_granulepos = -1;
454 parse->prev_blocksize = -1;
455 ret = gst_pad_event_default (pad, event);
458 vorbis_parse_drain_queue_prematurely (parse);
459 ret = gst_pad_event_default (pad, event);
462 if (!parse->streamheader_sent && GST_EVENT_IS_SERIALIZED (event))
463 ret = vorbis_parse_queue_event (parse, event);
465 ret = gst_pad_event_default (pad, event);
469 gst_object_unref (parse);
475 vorbis_parse_convert (GstPad * pad,
476 GstFormat src_format, gint64 src_value,
477 GstFormat * dest_format, gint64 * dest_value)
480 GstVorbisParse *parse;
483 parse = GST_VORBIS_PARSE (GST_PAD_PARENT (pad));
485 /* fixme: assumes atomic access to lots of instance variables modified from
486 * the streaming thread, including 64-bit variables */
488 if (parse->packetno < 4)
491 if (src_format == *dest_format) {
492 *dest_value = src_value;
496 if (parse->sinkpad == pad &&
497 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
500 switch (src_format) {
501 case GST_FORMAT_TIME:
502 switch (*dest_format) {
503 case GST_FORMAT_BYTES:
504 scale = sizeof (float) * parse->vi.channels;
505 case GST_FORMAT_DEFAULT:
507 scale * gst_util_uint64_scale_int (src_value, parse->vi.rate,
514 case GST_FORMAT_DEFAULT:
515 switch (*dest_format) {
516 case GST_FORMAT_BYTES:
517 *dest_value = src_value * sizeof (float) * parse->vi.channels;
519 case GST_FORMAT_TIME:
521 gst_util_uint64_scale_int (src_value, GST_SECOND, parse->vi.rate);
527 case GST_FORMAT_BYTES:
528 switch (*dest_format) {
529 case GST_FORMAT_DEFAULT:
530 *dest_value = src_value / (sizeof (float) * parse->vi.channels);
532 case GST_FORMAT_TIME:
533 *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
534 parse->vi.rate * sizeof (float) * parse->vi.channels);
548 vorbis_parse_src_query (GstPad * pad, GstQuery * query)
551 GstVorbisParse *parse;
552 gboolean res = FALSE;
554 parse = GST_VORBIS_PARSE (GST_PAD_PARENT (pad));
556 switch (GST_QUERY_TYPE (query)) {
557 case GST_QUERY_POSITION:
562 granulepos = parse->prev_granulepos;
564 gst_query_parse_position (query, &format, NULL);
566 /* and convert to the final format */
568 vorbis_parse_convert (pad, GST_FORMAT_DEFAULT, granulepos,
572 /* fixme: support segments
573 value = (value - parse->segment_start) + parse->segment_time;
576 gst_query_set_position (query, format, value);
578 GST_LOG_OBJECT (parse, "query %p: peer returned granulepos: %"
579 G_GUINT64_FORMAT " - we return %" G_GUINT64_FORMAT " (format %u)",
580 query, granulepos, value, format);
584 case GST_QUERY_DURATION:
586 /* fixme: not threadsafe */
587 /* query peer for total length */
588 if (!gst_pad_is_linked (parse->sinkpad)) {
589 GST_WARNING_OBJECT (parse, "sink pad %" GST_PTR_FORMAT " is not linked",
593 if (!(res = gst_pad_query (GST_PAD_PEER (parse->sinkpad), query)))
597 case GST_QUERY_CONVERT:
599 GstFormat src_fmt, dest_fmt;
600 gint64 src_val, dest_val;
602 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
604 vorbis_parse_convert (pad, src_fmt, src_val, &dest_fmt,
607 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
611 res = gst_pad_query_default (pad, query);
618 GST_WARNING_OBJECT (parse, "error handling query");
623 static GstStateChangeReturn
624 vorbis_parse_change_state (GstElement * element, GstStateChange transition)
626 GstVorbisParse *parse = GST_VORBIS_PARSE (element);
627 GstStateChangeReturn ret;
629 switch (transition) {
630 case GST_STATE_CHANGE_READY_TO_PAUSED:
631 vorbis_info_init (&parse->vi);
632 vorbis_comment_init (&parse->vc);
633 parse->prev_granulepos = -1;
634 parse->prev_blocksize = -1;
636 parse->streamheader_sent = FALSE;
637 parse->buffer_queue = g_queue_new ();
638 parse->event_queue = g_queue_new ();
644 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
646 switch (transition) {
647 case GST_STATE_CHANGE_PAUSED_TO_READY:
648 vorbis_info_clear (&parse->vi);
649 vorbis_comment_clear (&parse->vc);
650 vorbis_parse_clear_queue (parse);
651 g_queue_free (parse->buffer_queue);
652 parse->buffer_queue = NULL;
653 g_queue_free (parse->event_queue);
654 parse->event_queue = NULL;