2 * Copyright (C) 2005 Michael Smith <msmith@fluendo.com>
4 * gstoggparse.c: ogg stream parser
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 /* This ogg parser is essentially a subset of the ogg demuxer - rather than
23 * fully demuxing into packets, we only parse out the pages, create one
24 * GstBuffer per page, set all the appropriate flags on those pages, set caps
25 * appropriately (particularly the 'streamheader' which gives all the header
26 * pages required for initialing decode).
28 * It's dramatically simpler than the full demuxer as it does not support
40 #include "gstoggstream.h"
42 static const GstElementDetails gst_ogg_parse_details =
43 GST_ELEMENT_DETAILS ("Ogg parser",
45 "parse ogg streams into pages (info about ogg: http://xiph.org)",
46 "Michael Smith <msmith@fluendo.com>");
48 GST_DEBUG_CATEGORY_STATIC (gst_ogg_parse_debug);
49 #define GST_CAT_DEFAULT gst_ogg_parse_debug
51 #define GST_TYPE_OGG_PARSE (gst_ogg_parse_get_type())
52 #define GST_OGG_PARSE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_OGG_PARSE, GstOggParse))
53 #define GST_OGG_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_OGG_PARSE, GstOggParse))
54 #define GST_IS_OGG_PARSE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_OGG_PARSE))
55 #define GST_IS_OGG_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_OGG_PARSE))
57 static GType gst_ogg_parse_get_type (void);
59 typedef struct _GstOggParse GstOggParse;
60 typedef struct _GstOggParseClass GstOggParseClass;
66 GstPad *sinkpad; /* Sink pad we're reading data from */
68 GstPad *srcpad; /* Source pad we're writing to */
70 GSList *oggstreams; /* list of GstOggStreams for known streams */
72 gint64 offset; /* Current stream offset */
74 gboolean in_headers; /* Set if we're reading headers for streams */
76 gboolean last_page_not_bos; /* Set if we've seen a non-BOS page */
78 ogg_sync_state sync; /* Ogg page synchronisation */
80 GstCaps *caps; /* Our src caps */
83 struct _GstOggParseClass
85 GstElementClass parent_class;
88 static void gst_ogg_parse_base_init (gpointer g_class);
89 static void gst_ogg_parse_class_init (GstOggParseClass * klass);
90 static void gst_ogg_parse_init (GstOggParse * ogg);
91 static GstElementClass *parent_class = NULL;
94 gst_ogg_parse_get_type (void)
96 static GType ogg_parse_type = 0;
98 if (!ogg_parse_type) {
99 static const GTypeInfo ogg_parse_info = {
100 sizeof (GstOggParseClass),
101 gst_ogg_parse_base_init,
103 (GClassInitFunc) gst_ogg_parse_class_init,
106 sizeof (GstOggParse),
108 (GInstanceInitFunc) gst_ogg_parse_init,
111 ogg_parse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstOggParse",
114 return ogg_parse_type;
118 free_stream (GstOggStream * stream)
120 g_list_foreach (stream->headers, (GFunc) gst_mini_object_unref, NULL);
121 g_list_foreach (stream->unknown_pages, (GFunc) gst_mini_object_unref, NULL);
127 gst_ogg_parse_delete_all_streams (GstOggParse * ogg)
129 g_slist_foreach (ogg->oggstreams, (GFunc) free_stream, NULL);
130 g_slist_free (ogg->oggstreams);
131 ogg->oggstreams = NULL;
134 static GstOggStream *
135 gst_ogg_parse_new_stream (GstOggParse * parser, ogg_page * page)
137 GstOggStream *stream;
142 serialno = ogg_page_serialno (page);
144 GST_DEBUG_OBJECT (parser, "creating new stream %08x", serialno);
146 stream = g_new0 (GstOggStream, 1);
148 stream->serialno = serialno;
149 stream->in_headers = 1;
151 if (ogg_stream_init (&stream->stream, serialno) != 0) {
152 GST_ERROR ("Could not initialize ogg_stream struct for serial %08x.",
157 /* FIXME check return */
158 ogg_stream_pagein (&stream->stream, page);
160 /* FIXME check return */
161 ret = ogg_stream_packetout (&stream->stream, &packet);
163 gst_ogg_stream_setup_map (stream, &packet);
166 parser->oggstreams = g_slist_append (parser->oggstreams, stream);
171 static GstOggStream *
172 gst_ogg_parse_find_stream (GstOggParse * parser, guint32 serialno)
176 for (l = parser->oggstreams; l != NULL; l = l->next) {
177 GstOggStream *stream = (GstOggStream *) l->data;
179 if (stream->serialno == serialno)
185 /* signals and args */
198 static GstStaticPadTemplate ogg_parse_src_template_factory =
199 GST_STATIC_PAD_TEMPLATE ("src",
202 GST_STATIC_CAPS ("application/ogg")
205 static GstStaticPadTemplate ogg_parse_sink_template_factory =
206 GST_STATIC_PAD_TEMPLATE ("sink",
209 GST_STATIC_CAPS ("application/ogg")
212 static void gst_ogg_parse_dispose (GObject * object);
213 static GstStateChangeReturn gst_ogg_parse_change_state (GstElement * element,
214 GstStateChange transition);
215 static GstFlowReturn gst_ogg_parse_chain (GstPad * pad, GstBuffer * buffer);
218 gst_ogg_parse_base_init (gpointer g_class)
220 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
222 gst_element_class_set_details (element_class, &gst_ogg_parse_details);
224 gst_element_class_add_pad_template (element_class,
225 gst_static_pad_template_get (&ogg_parse_sink_template_factory));
226 gst_element_class_add_pad_template (element_class,
227 gst_static_pad_template_get (&ogg_parse_src_template_factory));
231 gst_ogg_parse_class_init (GstOggParseClass * klass)
233 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
234 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
236 parent_class = g_type_class_peek_parent (klass);
238 gstelement_class->change_state = gst_ogg_parse_change_state;
240 gobject_class->dispose = gst_ogg_parse_dispose;
244 gst_ogg_parse_init (GstOggParse * ogg)
246 /* create the sink and source pads */
248 gst_pad_new_from_static_template (&ogg_parse_sink_template_factory,
251 gst_pad_new_from_static_template (&ogg_parse_src_template_factory, "src");
253 /* TODO: Are there any events we must handle? */
254 /* gst_pad_set_event_function (ogg->sinkpad, gst_ogg_parse_handle_event); */
255 gst_pad_set_chain_function (ogg->sinkpad, gst_ogg_parse_chain);
257 gst_element_add_pad (GST_ELEMENT (ogg), ogg->sinkpad);
258 gst_element_add_pad (GST_ELEMENT (ogg), ogg->srcpad);
260 ogg->oggstreams = NULL;
264 gst_ogg_parse_dispose (GObject * object)
266 GstOggParse *ogg = GST_OGG_PARSE (object);
268 GST_LOG_OBJECT (ogg, "Disposing of object %p", ogg);
270 ogg_sync_clear (&ogg->sync);
271 gst_ogg_parse_delete_all_streams (ogg);
274 gst_caps_unref (ogg->caps);
278 if (G_OBJECT_CLASS (parent_class)->dispose)
279 G_OBJECT_CLASS (parent_class)->dispose (object);
282 /* submit the given buffer to the ogg sync.
284 * Returns the number of bytes submited.
287 gst_ogg_parse_submit_buffer (GstOggParse * ogg, GstBuffer * buffer)
293 size = GST_BUFFER_SIZE (buffer);
294 data = GST_BUFFER_DATA (buffer);
296 /* We now have a buffer, submit it to the ogg sync layer */
297 oggbuffer = ogg_sync_buffer (&ogg->sync, size);
298 memcpy (oggbuffer, data, size);
299 ogg_sync_wrote (&ogg->sync, size);
301 /* We've copied all the neccesary data, so we're done with the buffer */
302 gst_buffer_unref (buffer);
308 gst_ogg_parse_append_header (GValue * array, GstBuffer * buf)
310 GValue value = { 0 };
311 /* We require a copy to avoid circular refcounts */
312 GstBuffer *buffer = gst_buffer_copy (buf);
314 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
316 g_value_init (&value, GST_TYPE_BUFFER);
317 gst_value_set_buffer (&value, buffer);
318 gst_value_array_append_value (array, &value);
319 g_value_unset (&value);
325 PAGE_HEADER, /* Header page */
326 PAGE_DATA, /* Data page */
327 PAGE_PENDING, /* We don't know yet, we'll have to see some future pages */
331 gst_ogg_parse_is_header (GstOggParse * ogg, GstOggStream * stream,
334 ogg_int64_t gpos = ogg_page_granulepos (page);
339 /* This is good enough for now, but technically requires codec-specific
340 * behaviour to be perfect. This is where we need the mooted library for
341 * this stuff, which nobody has written.
350 gst_ogg_parse_buffer_from_page (ogg_page * page,
351 guint64 offset, gboolean delta, GstClockTime timestamp)
353 int size = page->header_len + page->body_len;
354 GstBuffer *buf = gst_buffer_new_and_alloc (size);
356 memcpy (GST_BUFFER_DATA (buf), page->header, page->header_len);
357 memcpy (GST_BUFFER_DATA (buf) + page->header_len, page->body, page->body_len);
359 GST_BUFFER_TIMESTAMP (buf) = timestamp;
360 GST_BUFFER_OFFSET (buf) = offset;
361 GST_BUFFER_OFFSET_END (buf) = offset + size;
367 /* Reads in buffers, parses them, reframes into one-buffer-per-ogg-page, submits
368 * pages to output pad.
371 gst_ogg_parse_chain (GstPad * pad, GstBuffer * buffer)
374 GstFlowReturn result = GST_FLOW_OK;
377 GstBuffer *pagebuffer;
378 GstClockTime buffertimestamp = GST_BUFFER_TIMESTAMP (buffer);
380 ogg = GST_OGG_PARSE (GST_OBJECT_PARENT (pad));
382 GST_LOG_OBJECT (ogg, "Chain function received buffer of size %d",
383 GST_BUFFER_SIZE (buffer));
385 gst_ogg_parse_submit_buffer (ogg, buffer);
387 while (ret != 0 && result == GST_FLOW_OK) {
390 /* We use ogg_sync_pageseek() rather than ogg_sync_pageout() so that we can
391 * track how many bytes the ogg layer discarded (in the case of sync errors,
392 * etc.); this allows us to accurately track the current stream offset
394 ret = ogg_sync_pageseek (&ogg->sync, &page);
396 /* need more data, that's fine... */
398 } else if (ret < 0) {
399 /* discontinuity; track how many bytes we skipped (-ret) */
402 gint64 granule = ogg_page_granulepos (&page);
403 #ifndef GST_DISABLE_GST_DEBUG
404 int bos = ogg_page_bos (&page);
406 guint64 startoffset = ogg->offset;
407 GstOggStream *stream;
409 serialno = ogg_page_serialno (&page);
410 stream = gst_ogg_parse_find_stream (ogg, serialno);
412 GST_LOG_OBJECT (ogg, "Timestamping outgoing buffer as %" GST_TIME_FORMAT,
413 GST_TIME_ARGS (buffertimestamp));
415 buffertimestamp = gst_ogg_stream_get_end_time_for_granulepos (stream,
417 pagebuffer = gst_ogg_parse_buffer_from_page (&page, startoffset, FALSE,
420 /* We read out 'ret' bytes, so we set the next offset appropriately */
424 "processing ogg page (serial %08x, pageno %ld, "
425 "granule pos %" G_GUINT64_FORMAT ", bos %d, offset %"
426 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT ")",
427 serialno, ogg_page_pageno (&page),
428 granule, bos, startoffset, ogg->offset);
430 if (ogg_page_bos (&page)) {
431 /* If we've seen this serialno before, this is technically an error,
432 * we log this case but accept it - this one replaces the previous
433 * stream with this serialno. We can do this since we're streaming, and
434 * not supporting seeking...
436 GstOggStream *stream = gst_ogg_parse_find_stream (ogg, serialno);
438 if (stream != NULL) {
439 GST_LOG_OBJECT (ogg, "Incorrect stream; repeats serial number %u "
440 "at offset %" G_GINT64_FORMAT, serialno, ogg->offset);
443 if (ogg->last_page_not_bos) {
444 GST_LOG_OBJECT (ogg, "Deleting all referenced streams, found a new "
445 "chain starting with serial %u", serialno);
446 gst_ogg_parse_delete_all_streams (ogg);
449 stream = gst_ogg_parse_new_stream (ogg, &page);
451 ogg->last_page_not_bos = FALSE;
453 gst_buffer_ref (pagebuffer);
454 stream->headers = g_list_append (stream->headers, pagebuffer);
456 if (!ogg->in_headers) {
458 "Found start of new chain at offset %" G_GUINT64_FORMAT,
463 /* For now, we just keep the header buffer in the stream->headers list;
464 * it actually gets output once we've collected the entire set
467 /* Non-BOS page. Either: we're outside headers, and this isn't a
468 * header (normal data), outside headers and this is (error!), inside
469 * headers, this is (append header), or inside headers and this isn't
470 * (we've found the end of headers; flush the lot!)
472 * Before that, we flag that the last page seen (this one) was not a
473 * BOS page; that way we know that when we next see a BOS page it's a
474 * new chain, and we can flush all existing streams.
477 GstOggStream *stream = gst_ogg_parse_find_stream (ogg, serialno);
481 "Non-BOS page unexpectedly found at %" G_GINT64_FORMAT,
486 ogg->last_page_not_bos = TRUE;
488 type = gst_ogg_parse_is_header (ogg, stream, &page);
490 if (type == PAGE_PENDING && ogg->in_headers) {
491 gst_buffer_ref (pagebuffer);
493 stream->unknown_pages = g_list_append (stream->unknown_pages,
495 } else if (type == PAGE_HEADER) {
496 if (!ogg->in_headers) {
497 GST_LOG_OBJECT (ogg, "Header page unexpectedly found outside "
498 "headers at offset %" G_GINT64_FORMAT, ogg->offset);
501 /* Append the header to the buffer list, after any unknown previous
504 stream->headers = g_list_concat (stream->headers,
505 stream->unknown_pages);
506 g_list_free (stream->unknown_pages);
507 gst_buffer_ref (pagebuffer);
508 stream->headers = g_list_append (stream->headers, pagebuffer);
510 } else { /* PAGE_DATA, or PAGE_PENDING but outside headers */
511 if (ogg->in_headers) {
512 /* First non-header page... set caps, flush headers.
514 * First up, we build a single GValue list of all the pagebuffers
515 * we're using for the headers, in order.
516 * Then we set this on the caps structure. Then we can start pushing
517 * buffers for the headers, and finally we send this non-header
521 GstStructure *structure;
522 GValue array = { 0 };
524 gboolean found_pending_headers = FALSE;
527 g_value_init (&array, GST_TYPE_ARRAY);
529 for (l = ogg->oggstreams; l != NULL; l = l->next) {
530 GstOggStream *stream = (GstOggStream *) l->data;
532 if (g_list_length (stream->headers) == 0) {
533 GST_LOG_OBJECT (ogg, "No primary header found for stream %08lx",
538 gst_ogg_parse_append_header (&array,
539 GST_BUFFER (stream->headers->data));
543 for (l = ogg->oggstreams; l != NULL; l = l->next) {
544 GstOggStream *stream = (GstOggStream *) l->data;
547 for (j = 1; j < g_list_length (stream->headers); j++) {
548 gst_ogg_parse_append_header (&array,
549 GST_BUFFER (g_list_nth_data (stream->headers, j)));
554 caps = gst_pad_get_caps (ogg->srcpad);
555 caps = gst_caps_make_writable (caps);
557 structure = gst_caps_get_structure (caps, 0);
558 gst_structure_set_value (structure, "streamheader", &array);
560 gst_pad_set_caps (ogg->srcpad, caps);
562 g_value_unset (&array);
565 gst_caps_unref (ogg->caps);
568 GST_LOG_OBJECT (ogg, "Set \"streamheader\" caps with %d buffers "
569 "(one per page)", count);
571 /* Now, we do the same thing, but push buffers... */
572 for (l = ogg->oggstreams; l != NULL; l = l->next) {
573 GstOggStream *stream = (GstOggStream *) l->data;
574 GstBuffer *buf = GST_BUFFER (stream->headers->data);
576 gst_buffer_set_caps (buf, caps);
578 result = gst_pad_push (ogg->srcpad, buf);
579 if (result != GST_FLOW_OK)
582 for (l = ogg->oggstreams; l != NULL; l = l->next) {
583 GstOggStream *stream = (GstOggStream *) l->data;
586 for (j = 1; j < g_list_length (stream->headers); j++) {
588 GST_BUFFER (g_list_nth_data (stream->headers, j));
589 gst_buffer_set_caps (buf, caps);
591 result = gst_pad_push (ogg->srcpad, buf);
592 if (result != GST_FLOW_OK)
599 /* And finally the pending data pages */
600 for (l = ogg->oggstreams; l != NULL; l = l->next) {
601 GstOggStream *stream = (GstOggStream *) l->data;
604 if (stream->unknown_pages == NULL)
607 if (found_pending_headers) {
608 GST_WARNING_OBJECT (ogg, "Incorrectly muxed headers found at "
609 "approximate offset %" G_GINT64_FORMAT, ogg->offset);
611 found_pending_headers = TRUE;
613 GST_LOG_OBJECT (ogg, "Pushing %d pending pages after headers",
614 g_list_length (stream->unknown_pages) + 1);
616 for (k = stream->unknown_pages; k != NULL; k = k->next) {
619 buf = GST_BUFFER (k->data);
620 gst_buffer_set_caps (buf, caps);
621 result = gst_pad_push (ogg->srcpad, buf);
622 if (result != GST_FLOW_OK)
625 g_list_foreach (stream->unknown_pages,
626 (GFunc) gst_mini_object_unref, NULL);
627 g_list_free (stream->unknown_pages);
628 stream->unknown_pages = NULL;
631 gst_buffer_set_caps (pagebuffer, caps);
633 result = gst_pad_push (ogg->srcpad, GST_BUFFER (pagebuffer));
634 if (result != GST_FLOW_OK)
637 /* Normal data page, submit buffer */
638 gst_buffer_set_caps (pagebuffer, ogg->caps);
639 result = gst_pad_push (ogg->srcpad, GST_BUFFER (pagebuffer));
640 if (result != GST_FLOW_OK)
651 gst_pad_push_event (GST_PAD (ogg->srcpad), gst_event_new_eos ());
652 return GST_FLOW_ERROR;
655 static GstStateChangeReturn
656 gst_ogg_parse_change_state (GstElement * element, GstStateChange transition)
659 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
661 ogg = GST_OGG_PARSE (element);
663 switch (transition) {
664 case GST_STATE_CHANGE_NULL_TO_READY:
665 ogg_sync_init (&ogg->sync);
667 case GST_STATE_CHANGE_READY_TO_PAUSED:
668 ogg_sync_reset (&ogg->sync);
670 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
676 result = parent_class->change_state (element, transition);
678 switch (transition) {
679 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
681 case GST_STATE_CHANGE_PAUSED_TO_READY:
683 case GST_STATE_CHANGE_READY_TO_NULL:
684 ogg_sync_clear (&ogg->sync);
693 gst_ogg_parse_plugin_init (GstPlugin * plugin)
695 GST_DEBUG_CATEGORY_INIT (gst_ogg_parse_debug, "oggparse", 0, "ogg parser");
697 return gst_element_register (plugin, "oggparse", GST_RANK_NONE,