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, GstObject * parent,
80 static GstStateChangeReturn vorbis_parse_change_state (GstElement * element,
81 GstStateChange transition);
82 static gboolean vorbis_parse_sink_event (GstPad * pad, GstObject * parent,
84 static gboolean vorbis_parse_src_query (GstPad * pad, GstObject * parent,
86 static gboolean vorbis_parse_convert (GstPad * pad, GstFormat src_format,
87 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
88 static GstFlowReturn vorbis_parse_parse_packet (GstVorbisParse * parse,
92 gst_vorbis_parse_class_init (GstVorbisParseClass * klass)
94 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
96 gstelement_class->change_state = vorbis_parse_change_state;
98 gst_element_class_add_pad_template (gstelement_class,
99 gst_static_pad_template_get (&vorbis_parse_src_factory));
100 gst_element_class_add_pad_template (gstelement_class,
101 gst_static_pad_template_get (&vorbis_parse_sink_factory));
102 gst_element_class_set_details_simple (gstelement_class,
103 "VorbisParse", "Codec/Parser/Audio",
104 "parse raw vorbis streams",
105 "Thomas Vander Stichele <thomas at apestaart dot org>");
107 klass->parse_packet = GST_DEBUG_FUNCPTR (vorbis_parse_parse_packet);
111 gst_vorbis_parse_init (GstVorbisParse * parse)
114 gst_pad_new_from_static_template (&vorbis_parse_sink_factory, "sink");
115 gst_pad_set_chain_function (parse->sinkpad,
116 GST_DEBUG_FUNCPTR (vorbis_parse_chain));
117 gst_pad_set_event_function (parse->sinkpad,
118 GST_DEBUG_FUNCPTR (vorbis_parse_sink_event));
119 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
122 gst_pad_new_from_static_template (&vorbis_parse_src_factory, "src");
123 gst_pad_set_query_function (parse->srcpad,
124 GST_DEBUG_FUNCPTR (vorbis_parse_src_query));
125 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
129 vorbis_parse_set_header_on_caps (GstVorbisParse * parse, GstCaps * caps)
131 GstBuffer *buf1, *buf2, *buf3;
132 GstStructure *structure;
133 GValue array = { 0 };
134 GValue value = { 0 };
137 g_assert (parse->streamheader);
138 g_assert (parse->streamheader->next);
139 g_assert (parse->streamheader->next->next);
140 buf1 = parse->streamheader->data;
142 buf2 = parse->streamheader->next->data;
144 buf3 = parse->streamheader->next->next->data;
147 structure = gst_caps_get_structure (caps, 0);
150 GST_BUFFER_FLAG_SET (buf1, GST_BUFFER_FLAG_HEADER);
151 GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_HEADER);
152 GST_BUFFER_FLAG_SET (buf3, GST_BUFFER_FLAG_HEADER);
154 /* put buffers in a fixed list */
155 g_value_init (&array, GST_TYPE_ARRAY);
156 g_value_init (&value, GST_TYPE_BUFFER);
157 gst_value_set_buffer (&value, buf1);
158 gst_value_array_append_value (&array, &value);
159 g_value_unset (&value);
160 g_value_init (&value, GST_TYPE_BUFFER);
161 gst_value_set_buffer (&value, buf2);
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, buf3);
166 gst_value_array_append_value (&array, &value);
167 gst_structure_set_value (structure, "streamheader", &array);
168 g_value_unset (&value);
169 g_value_unset (&array);
173 vorbis_parse_drain_event_queue (GstVorbisParse * parse)
175 while (parse->event_queue->length) {
178 event = GST_EVENT_CAST (g_queue_pop_head (parse->event_queue));
179 gst_pad_event_default (parse->sinkpad, GST_OBJECT_CAST (parse), event);
184 vorbis_parse_push_headers (GstVorbisParse * parse)
186 /* mark and put on caps */
188 GstBuffer *outbuf, *outbuf1, *outbuf2, *outbuf3;
192 outbuf = GST_BUFFER_CAST (parse->streamheader->data);
193 gst_buffer_map (outbuf, &map, GST_MAP_READ);
194 packet.packet = map.data;
195 packet.bytes = map.size;
196 packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
200 vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
201 gst_buffer_unmap (outbuf, &map);
202 parse->sample_rate = parse->vi.rate;
203 parse->channels = parse->vi.channels;
206 outbuf = GST_BUFFER_CAST (parse->streamheader->next->data);
207 gst_buffer_map (outbuf, &map, GST_MAP_READ);
208 packet.packet = map.data;
209 packet.bytes = map.size;
210 packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
214 vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
215 gst_buffer_unmap (outbuf, &map);
218 outbuf = GST_BUFFER_CAST (parse->streamheader->next->next->data);
219 gst_buffer_map (outbuf, &map, GST_MAP_READ);
220 packet.packet = map.data;
221 packet.bytes = map.size;
222 packet.granulepos = GST_BUFFER_OFFSET_END (outbuf);
226 vorbis_synthesis_headerin (&parse->vi, &parse->vc, &packet);
227 gst_buffer_unmap (outbuf, &map);
230 /* get the headers into the caps, passing them to vorbis as we go */
231 caps = gst_caps_new_simple ("audio/x-vorbis",
232 "rate", G_TYPE_INT, parse->sample_rate,
233 "channels", G_TYPE_INT, parse->channels, NULL);;
234 vorbis_parse_set_header_on_caps (parse, caps);
235 GST_DEBUG_OBJECT (parse, "here are the caps: %" GST_PTR_FORMAT, caps);
236 gst_pad_set_caps (parse->srcpad, caps);
237 gst_caps_unref (caps);
239 /* first process queued events */
240 vorbis_parse_drain_event_queue (parse);
242 /* push out buffers, ignoring return value... */
243 gst_pad_push (parse->srcpad, outbuf1);
244 gst_pad_push (parse->srcpad, outbuf2);
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 return gst_pad_push (parse->srcpad, buf);
287 vorbis_parse_drain_queue_prematurely (GstVorbisParse * parse)
289 GstFlowReturn ret = GST_FLOW_OK;
290 gint64 granulepos = MAX (parse->prev_granulepos, 0);
292 /* got an EOS event, make sure to push out any buffers that were in the queue
293 * -- won't normally be the case, but this catches the
294 * didn't-get-a-granulepos-on-the-last-packet case. Assuming a continuous
297 /* if we got EOS before any buffers came, go ahead and push the other events
299 vorbis_parse_drain_event_queue (parse);
301 while (!g_queue_is_empty (parse->buffer_queue)) {
304 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
306 granulepos += GST_BUFFER_OFFSET (buf);
307 ret = vorbis_parse_push_buffer (parse, buf, granulepos);
309 if (ret != GST_FLOW_OK)
313 parse->prev_granulepos = granulepos;
320 vorbis_parse_drain_queue (GstVorbisParse * parse, gint64 granulepos)
322 GstFlowReturn ret = GST_FLOW_OK;
324 gint64 cur = granulepos;
327 for (walk = parse->buffer_queue->head; walk; walk = walk->next)
328 cur -= GST_BUFFER_OFFSET (walk->data);
330 if (parse->prev_granulepos != -1)
331 cur = MAX (cur, parse->prev_granulepos);
333 while (!g_queue_is_empty (parse->buffer_queue)) {
336 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
338 cur += GST_BUFFER_OFFSET (buf);
339 gp = CLAMP (cur, 0, granulepos);
341 ret = vorbis_parse_push_buffer (parse, buf, gp);
343 if (ret != GST_FLOW_OK)
347 parse->prev_granulepos = granulepos;
354 vorbis_parse_queue_buffer (GstVorbisParse * parse, GstBuffer * buf)
356 GstFlowReturn ret = GST_FLOW_OK;
361 buf = gst_buffer_make_writable (buf);
363 gst_buffer_map (buf, &map, GST_MAP_READ);
364 packet.packet = map.data;
365 packet.bytes = map.size;
366 GST_DEBUG ("%p, %" G_GSIZE_FORMAT, map.data, map.size);
367 packet.granulepos = GST_BUFFER_OFFSET_END (buf);
368 packet.packetno = parse->packetno + parse->buffer_queue->length;
371 blocksize = vorbis_packet_blocksize (&parse->vi, &packet);
372 gst_buffer_unmap (buf, &map);
374 /* temporarily store the sample count in OFFSET -- we overwrite this later */
376 if (parse->prev_blocksize < 0)
377 GST_BUFFER_OFFSET (buf) = 0;
379 GST_BUFFER_OFFSET (buf) = (blocksize + parse->prev_blocksize) / 4;
381 parse->prev_blocksize = blocksize;
383 g_queue_push_tail (parse->buffer_queue, buf);
385 if (GST_BUFFER_OFFSET_END_IS_VALID (buf))
386 ret = vorbis_parse_drain_queue (parse, GST_BUFFER_OFFSET_END (buf));
392 vorbis_parse_parse_packet (GstVorbisParse * parse, GstBuffer * buf)
396 gboolean have_header;
401 gst_buffer_map (buf, &map, GST_MAP_READ);
406 gst_buffer_unmap (buf, &map);
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, GstObject * parent, GstBuffer * buffer)
430 GstVorbisParseClass *klass;
431 GstVorbisParse *parse;
433 parse = GST_VORBIS_PARSE (parent);
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, GstObject * parent, GstEvent * event)
455 GstVorbisParse *parse;
457 parse = GST_VORBIS_PARSE (parent);
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, parent, event);
467 vorbis_parse_drain_queue_prematurely (parse);
468 ret = gst_pad_event_default (pad, parent, 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, parent, event);
482 vorbis_parse_convert (GstPad * pad,
483 GstFormat src_format, gint64 src_value,
484 GstFormat * dest_format, gint64 * dest_value)
487 GstVorbisParse *parse;
490 parse = GST_VORBIS_PARSE (GST_PAD_PARENT (pad));
492 /* fixme: assumes atomic access to lots of instance variables modified from
493 * the streaming thread, including 64-bit variables */
495 if (parse->packetno < 4)
498 if (src_format == *dest_format) {
499 *dest_value = src_value;
503 if (parse->sinkpad == pad &&
504 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
507 switch (src_format) {
508 case GST_FORMAT_TIME:
509 switch (*dest_format) {
510 case GST_FORMAT_BYTES:
511 scale = sizeof (float) * parse->vi.channels;
512 case GST_FORMAT_DEFAULT:
514 scale * gst_util_uint64_scale_int (src_value, parse->vi.rate,
521 case GST_FORMAT_DEFAULT:
522 switch (*dest_format) {
523 case GST_FORMAT_BYTES:
524 *dest_value = src_value * sizeof (float) * parse->vi.channels;
526 case GST_FORMAT_TIME:
528 gst_util_uint64_scale_int (src_value, GST_SECOND, parse->vi.rate);
534 case GST_FORMAT_BYTES:
535 switch (*dest_format) {
536 case GST_FORMAT_DEFAULT:
537 *dest_value = src_value / (sizeof (float) * parse->vi.channels);
539 case GST_FORMAT_TIME:
540 *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
541 parse->vi.rate * sizeof (float) * parse->vi.channels);
555 vorbis_parse_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
558 GstVorbisParse *parse;
559 gboolean res = FALSE;
561 parse = GST_VORBIS_PARSE (parent);
563 switch (GST_QUERY_TYPE (query)) {
564 case GST_QUERY_POSITION:
569 granulepos = parse->prev_granulepos;
571 gst_query_parse_position (query, &format, NULL);
573 /* and convert to the final format */
575 vorbis_parse_convert (pad, GST_FORMAT_DEFAULT, granulepos,
579 /* fixme: support segments
580 value = (value - parse->segment_start) + parse->segment_time;
583 gst_query_set_position (query, format, value);
585 GST_LOG_OBJECT (parse, "query %p: peer returned granulepos: %"
586 G_GUINT64_FORMAT " - we return %" G_GUINT64_FORMAT " (format %u)",
587 query, granulepos, value, format);
591 case GST_QUERY_DURATION:
593 /* fixme: not threadsafe */
594 /* query peer for total length */
595 if (!gst_pad_is_linked (parse->sinkpad)) {
596 GST_WARNING_OBJECT (parse, "sink pad %" GST_PTR_FORMAT " is not linked",
600 if (!(res = gst_pad_peer_query (parse->sinkpad, query)))
604 case GST_QUERY_CONVERT:
606 GstFormat src_fmt, dest_fmt;
607 gint64 src_val, dest_val;
609 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
611 vorbis_parse_convert (pad, src_fmt, src_val, &dest_fmt,
614 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
618 res = gst_pad_query_default (pad, parent, query);
625 GST_WARNING_OBJECT (parse, "error handling query");
630 static GstStateChangeReturn
631 vorbis_parse_change_state (GstElement * element, GstStateChange transition)
633 GstVorbisParse *parse = GST_VORBIS_PARSE (element);
634 GstStateChangeReturn ret;
636 switch (transition) {
637 case GST_STATE_CHANGE_READY_TO_PAUSED:
638 vorbis_info_init (&parse->vi);
639 vorbis_comment_init (&parse->vc);
640 parse->prev_granulepos = -1;
641 parse->prev_blocksize = -1;
643 parse->streamheader_sent = FALSE;
644 parse->buffer_queue = g_queue_new ();
645 parse->event_queue = g_queue_new ();
651 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
653 switch (transition) {
654 case GST_STATE_CHANGE_PAUSED_TO_READY:
655 vorbis_info_clear (&parse->vi);
656 vorbis_comment_clear (&parse->vc);
657 vorbis_parse_clear_queue (parse);
658 g_queue_free (parse->buffer_queue);
659 parse->buffer_queue = NULL;
660 g_queue_free (parse->event_queue);
661 parse->event_queue = NULL;