2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
4 * gstoggdemux.c: ogg stream demuxer
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.
23 * SECTION:element-oggdemux
24 * @see_also: <link linkend="gst-plugins-base-plugins-oggmux">oggmux</link>
26 * This element demuxes ogg files into their encoded audio and video components.
29 * <title>Example pipelines</title>
31 * gst-launch -v filesrc location=test.ogg ! oggdemux ! vorbisdec ! audioconvert ! alsasink
32 * ]| Decodes the vorbis audio stored inside an ogg container.
35 * Last reviewed on 2006-12-30 (0.10.5)
43 #include <gst/gst-i18n-plugin.h>
44 #include <gst/base/gsttypefindhelper.h>
46 #include "gstoggdemux.h"
48 static const GstElementDetails gst_ogg_demux_details =
49 GST_ELEMENT_DETAILS ("Ogg demuxer",
51 "demux ogg streams (info about ogg: http://xiph.org)",
52 "Wim Taymans <wim@fluendo.com>");
54 #define CHUNKSIZE (8500) /* this is out of vorbisfile */
55 #define SKELETON_FISHEAD_SIZE 64
56 #define SKELETON_FISBONE_MIN_SIZE 52
58 #define GST_FLOW_LIMIT GST_FLOW_CUSTOM_ERROR
60 #define GST_CHAIN_LOCK(ogg) g_mutex_lock((ogg)->chain_lock)
61 #define GST_CHAIN_UNLOCK(ogg) g_mutex_unlock((ogg)->chain_lock)
63 GST_DEBUG_CATEGORY_STATIC (gst_ogg_demux_debug);
64 GST_DEBUG_CATEGORY_STATIC (gst_ogg_demux_setup_debug);
65 #define GST_CAT_DEFAULT gst_ogg_demux_debug
68 gst_ogg_page_copy (ogg_page * page)
70 ogg_page *p = g_new0 (ogg_page, 1);
72 /* make a copy of the page */
73 p->header = g_memdup (page->header, page->header_len);
74 p->header_len = page->header_len;
75 p->body = g_memdup (page->body, page->body_len);
76 p->body_len = page->body_len;
82 gst_ogg_page_free (ogg_page * page)
84 g_free (page->header);
89 static GstStaticPadTemplate internaltemplate =
90 GST_STATIC_PAD_TEMPLATE ("internal",
95 static gboolean gst_ogg_demux_collect_chain_info (GstOggDemux * ogg,
97 static gboolean gst_ogg_demux_activate_chain (GstOggDemux * ogg,
98 GstOggChain * chain, GstEvent * event);
99 static void gst_ogg_chain_mark_discont (GstOggChain * chain);
101 static gboolean gst_ogg_demux_perform_seek (GstOggDemux * ogg,
103 static gboolean gst_ogg_demux_receive_event (GstElement * element,
106 static void gst_ogg_pad_class_init (GstOggPadClass * klass);
107 static void gst_ogg_pad_init (GstOggPad * pad);
108 static void gst_ogg_pad_dispose (GObject * object);
109 static void gst_ogg_pad_finalize (GObject * object);
112 static const GstFormat *gst_ogg_pad_formats (GstPad * pad);
113 static const GstEventMask *gst_ogg_pad_event_masks (GstPad * pad);
115 static const GstQueryType *gst_ogg_pad_query_types (GstPad * pad);
116 static gboolean gst_ogg_pad_src_query (GstPad * pad, GstQuery * query);
117 static gboolean gst_ogg_pad_event (GstPad * pad, GstEvent * event);
118 static GstCaps *gst_ogg_pad_getcaps (GstPad * pad);
119 static GstOggPad *gst_ogg_chain_get_stream (GstOggChain * chain,
122 static gboolean gst_ogg_pad_query_convert (GstOggPad * pad,
123 GstFormat src_format, gint64 src_val,
124 GstFormat * dest_format, gint64 * dest_val);
125 static GstClockTime gst_annodex_granule_to_time (gint64 granulepos,
126 gint64 granulerate_n, gint64 granulerate_d, guint8 granuleshift);
128 static GstFlowReturn gst_ogg_demux_combine_flows (GstOggDemux * ogg,
129 GstOggPad * pad, GstFlowReturn ret);
131 G_DEFINE_TYPE (GstOggPad, gst_ogg_pad, GST_TYPE_PAD);
134 gst_ogg_pad_class_init (GstOggPadClass * klass)
136 GObjectClass *gobject_class;
138 gobject_class = (GObjectClass *) klass;
140 gobject_class->dispose = gst_ogg_pad_dispose;
141 gobject_class->finalize = gst_ogg_pad_finalize;
145 gst_ogg_pad_init (GstOggPad * pad)
147 gst_pad_set_event_function (GST_PAD (pad),
148 GST_DEBUG_FUNCPTR (gst_ogg_pad_event));
149 gst_pad_set_getcaps_function (GST_PAD (pad),
150 GST_DEBUG_FUNCPTR (gst_ogg_pad_getcaps));
151 gst_pad_set_query_type_function (GST_PAD (pad),
152 GST_DEBUG_FUNCPTR (gst_ogg_pad_query_types));
153 gst_pad_set_query_function (GST_PAD (pad),
154 GST_DEBUG_FUNCPTR (gst_ogg_pad_src_query));
156 pad->mode = GST_OGG_PAD_MODE_INIT;
158 pad->first_granule = -1;
159 pad->current_granule = -1;
161 pad->start_time = GST_CLOCK_TIME_NONE;
162 pad->first_time = GST_CLOCK_TIME_NONE;
164 pad->have_type = FALSE;
165 pad->continued = NULL;
170 gst_ogg_pad_dispose (GObject * object)
172 GstOggPad *pad = GST_OGG_PAD (object);
174 GstElement **element_p;
178 gst_element_set_state (pad->element, GST_STATE_NULL);
180 elem_pad_p = &pad->elem_pad;
181 element_p = &pad->element;
182 elem_out_p = &pad->elem_out;
183 gst_object_replace ((GstObject **) elem_pad_p, NULL);
184 gst_object_replace ((GstObject **) element_p, NULL);
185 gst_object_replace ((GstObject **) elem_out_p, NULL);
190 g_list_foreach (pad->headers, (GFunc) gst_mini_object_unref, NULL);
191 g_list_free (pad->headers);
194 /* clear continued pages */
195 g_list_foreach (pad->continued, (GFunc) gst_ogg_page_free, NULL);
196 g_list_free (pad->continued);
197 pad->continued = NULL;
199 ogg_stream_reset (&pad->stream);
201 G_OBJECT_CLASS (gst_ogg_pad_parent_class)->dispose (object);
205 gst_ogg_pad_finalize (GObject * object)
207 GstOggPad *pad = GST_OGG_PAD (object);
209 ogg_stream_clear (&pad->stream);
211 G_OBJECT_CLASS (gst_ogg_pad_parent_class)->finalize (object);
215 static const GstFormat *
216 gst_ogg_pad_formats (GstPad * pad)
218 static GstFormat src_formats[] = {
219 GST_FORMAT_DEFAULT, /* time */
220 GST_FORMAT_TIME, /* granulepos */
223 static GstFormat sink_formats[] = {
225 GST_FORMAT_DEFAULT, /* bytes */
229 return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
234 static const GstEventMask *
235 gst_ogg_pad_event_masks (GstPad * pad)
237 static const GstEventMask src_event_masks[] = {
238 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
242 return src_event_masks;
246 static const GstQueryType *
247 gst_ogg_pad_query_types (GstPad * pad)
249 static const GstQueryType query_types[] = {
259 gst_ogg_pad_getcaps (GstPad * pad)
261 return gst_caps_ref (GST_PAD_CAPS (pad));
265 gst_ogg_pad_src_query (GstPad * pad, GstQuery * query)
270 ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));
272 switch (GST_QUERY_TYPE (query)) {
273 case GST_QUERY_DURATION:
278 gst_query_parse_duration (query, &format, NULL);
279 /* can only get position in time */
280 if (format != GST_FORMAT_TIME)
284 /* we must return the total seekable length */
285 total_time = ogg->total_time;
287 /* in non-seek mode we can answer the query and we must return -1 */
291 gst_query_set_duration (query, GST_FORMAT_TIME, total_time);
294 case GST_QUERY_SEEKING:
298 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
299 if (format == GST_FORMAT_TIME) {
300 gst_query_set_seeking (query, GST_FORMAT_TIME, ogg->seekable,
309 res = gst_pad_query_default (pad, query);
313 gst_object_unref (ogg);
320 GST_DEBUG_OBJECT (ogg, "only query duration on TIME is supported");
327 gst_ogg_demux_receive_event (GstElement * element, GstEvent * event)
332 ogg = GST_OGG_DEMUX (element);
334 switch (GST_EVENT_TYPE (event)) {
336 /* can't seek if we are not seekable, FIXME could pass the
337 * seek query upstream after converting it to bytes using
338 * the average bitrate of the stream. */
339 if (!ogg->seekable) {
340 GST_DEBUG_OBJECT (ogg, "seek on non seekable stream");
344 /* now do the seek */
345 res = gst_ogg_demux_perform_seek (ogg, event);
346 gst_event_unref (event);
349 GST_DEBUG_OBJECT (ogg, "We only handle seek events here");
358 GST_DEBUG_OBJECT (ogg, "error handling event");
359 gst_event_unref (event);
365 gst_ogg_pad_event (GstPad * pad, GstEvent * event)
370 ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));
372 switch (GST_EVENT_TYPE (event)) {
374 /* can't seek if we are not seekable, FIXME could pass the
375 * seek query upstream after converting it to bytes using
376 * the average bitrate of the stream. */
377 if (!ogg->seekable) {
378 GST_DEBUG_OBJECT (ogg, "seek on non seekable stream");
382 /* now do the seek */
383 res = gst_ogg_demux_perform_seek (ogg, event);
384 gst_event_unref (event);
387 res = gst_pad_event_default (pad, event);
391 gst_object_unref (ogg);
398 GST_DEBUG_OBJECT (ogg, "error handling event");
399 gst_event_unref (event);
406 gst_ogg_pad_reset (GstOggPad * pad)
408 ogg_stream_reset (&pad->stream);
410 GST_DEBUG_OBJECT (pad, "doing reset");
412 /* clear continued pages */
413 g_list_foreach (pad->continued, (GFunc) gst_ogg_page_free, NULL);
414 g_list_free (pad->continued);
415 pad->continued = NULL;
417 pad->last_ret = GST_FLOW_OK;
420 /* the filter function for selecting the elements we can use in
423 gst_ogg_demux_factory_filter (GstPluginFeature * feature, GstCaps * caps)
428 /* we only care about element factories */
429 if (!GST_IS_ELEMENT_FACTORY (feature))
432 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
433 /* only demuxers and decoders can play */
434 if (strstr (klass, "Demux") == NULL &&
435 strstr (klass, "Decoder") == NULL && strstr (klass, "Parse") == NULL) {
439 /* only select elements with autoplugging rank */
440 rank = gst_plugin_feature_get_rank (feature);
441 if (rank < GST_RANK_MARGINAL)
444 GST_DEBUG ("checking factory %s", GST_PLUGIN_FEATURE_NAME (feature));
445 /* now see if it is compatible with the caps */
447 GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
448 const GList *templates;
451 /* get the templates from the element factory */
452 templates = gst_element_factory_get_static_pad_templates (factory);
454 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
455 GstStaticPadTemplate *templ = walk->data;
457 /* we only care about the sink templates */
458 if (templ->direction == GST_PAD_SINK) {
463 /* try to intersect the caps with the caps of the template */
464 scaps = gst_static_caps_get (&templ->static_caps);
465 intersect = gst_caps_intersect (caps, scaps);
466 gst_caps_unref (scaps);
468 empty = gst_caps_is_empty (intersect);
469 gst_caps_unref (intersect);
471 /* check if the intersection is empty */
473 /* non empty intersection, we can use this element */
485 /* function used to sort element features */
487 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
491 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
494 return strcmp (gst_plugin_feature_get_name (f2),
495 gst_plugin_feature_get_name (f1));
498 /* called when the skeleton fishead is found. Caller ensures the packet is
499 * precisely the correct size; we don't re-check this here. */
501 gst_ogg_pad_parse_skeleton_fishead (GstOggPad * pad, ogg_packet * packet)
503 GstOggDemux *ogg = pad->ogg;
504 guint8 *data = packet->packet;
505 guint16 major, minor;
506 gint64 prestime_n, prestime_d;
507 gint64 basetime_n, basetime_d;
509 /* skip "fishead\0" */
511 major = GST_READ_UINT16_LE (data);
513 minor = GST_READ_UINT16_LE (data);
515 prestime_n = (gint64) GST_READ_UINT64_LE (data);
517 prestime_d = (gint64) GST_READ_UINT64_LE (data);
519 basetime_n = (gint64) GST_READ_UINT64_LE (data);
521 basetime_d = (gint64) GST_READ_UINT64_LE (data);
524 ogg->basetime = gst_util_uint64_scale (GST_SECOND, basetime_n, basetime_d);
525 ogg->prestime = gst_util_uint64_scale (GST_SECOND, prestime_n, prestime_d);
526 ogg->have_fishead = TRUE;
527 pad->is_skeleton = TRUE;
528 pad->start_time = GST_CLOCK_TIME_NONE;
529 pad->first_granule = -1;
530 pad->first_time = GST_CLOCK_TIME_NONE;
531 GST_INFO_OBJECT (ogg, "skeleton fishead parsed (basetime: %"
532 GST_TIME_FORMAT ", prestime: %" GST_TIME_FORMAT ")",
533 GST_TIME_ARGS (ogg->basetime), GST_TIME_ARGS (ogg->prestime));
536 /* function called when a skeleton fisbone is found. Caller ensures that
537 * the packet length is sufficient */
539 gst_ogg_pad_parse_skeleton_fisbone (GstOggPad * pad, ogg_packet * packet)
541 GstOggDemux *ogg = pad->ogg;
542 GstOggPad *fisbone_pad;
543 gint64 start_granule;
545 guint8 *data = packet->packet;
547 /* skip "fisbone\0" */
549 /* skip headers offset */
551 serialno = GST_READ_UINT32_LE (data);
553 fisbone_pad = gst_ogg_chain_get_stream (pad->chain, serialno);
555 if (fisbone_pad->have_fisbone)
559 fisbone_pad->have_fisbone = TRUE;
562 /* skip number of headers */
564 fisbone_pad->granulerate_n = GST_READ_UINT64_LE (data);
566 fisbone_pad->granulerate_d = GST_READ_UINT64_LE (data);
568 start_granule = GST_READ_UINT64_LE (data);
570 fisbone_pad->preroll = GST_READ_UINT32_LE (data);
572 fisbone_pad->granuleshift = GST_READ_UINT8 (data);
577 fisbone_pad->start_time = ogg->prestime - ogg->basetime;
579 GST_INFO_OBJECT (pad->ogg, "skeleton fisbone parsed "
580 "(serialno: %08x start time: %" GST_TIME_FORMAT
581 " granulerate_n: %" G_GINT64_FORMAT " granulerate_d: %" G_GINT64_FORMAT
582 " preroll: %" G_GUINT32_FORMAT " granuleshift: %d)",
583 serialno, GST_TIME_ARGS (fisbone_pad->start_time),
584 fisbone_pad->granulerate_n, fisbone_pad->granulerate_d,
585 fisbone_pad->preroll, fisbone_pad->granuleshift);
587 GST_WARNING_OBJECT (pad->ogg,
588 "found skeleton fisbone for an unknown stream %" G_GUINT32_FORMAT,
593 /* function called to convert a granulepos to a timestamp */
595 gst_ogg_pad_query_convert (GstOggPad * pad, GstFormat src_format,
596 gint64 src_val, GstFormat * dest_format, gint64 * dest_val)
605 if (!pad->have_fisbone && pad->elem_pad == NULL)
608 switch (src_format) {
609 case GST_FORMAT_DEFAULT:
610 if (pad->have_fisbone && *dest_format == GST_FORMAT_TIME) {
611 *dest_val = gst_annodex_granule_to_time (src_val,
612 pad->granulerate_n, pad->granulerate_d, pad->granuleshift);
616 if (pad->elem_pad == NULL)
619 res = gst_pad_query_convert (pad->elem_pad, src_format, src_val,
620 dest_format, dest_val);
625 if (pad->elem_pad == NULL)
628 res = gst_pad_query_convert (pad->elem_pad, src_format, src_val,
629 dest_format, dest_val);
635 /* function called by the internal decoder elements when it outputs
636 * a buffer. We use it to get the first timestamp of the stream
639 gst_ogg_pad_internal_chain (GstPad * pad, GstBuffer * buffer)
642 GstClockTime timestamp;
644 oggpad = gst_pad_get_element_private (pad);
646 timestamp = GST_BUFFER_TIMESTAMP (buffer);
647 GST_DEBUG_OBJECT (oggpad, "received buffer from internal pad, TS=%"
648 GST_TIME_FORMAT "=%" G_GINT64_FORMAT, GST_TIME_ARGS (timestamp),
651 if (oggpad->start_time == GST_CLOCK_TIME_NONE) {
652 oggpad->start_time = timestamp;
653 GST_DEBUG_OBJECT (oggpad, "new start time: %" GST_TIME_FORMAT,
654 GST_TIME_ARGS (timestamp));
657 gst_buffer_unref (buffer);
663 internal_element_pad_added_cb (GstElement * element, GstPad * pad,
666 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
667 if (!(gst_pad_link (pad, oggpad->elem_out) == GST_PAD_LINK_OK)) {
668 GST_ERROR ("Really couldn't find a valid pad");
670 oggpad->dynamic = FALSE;
671 g_signal_handler_disconnect (element, oggpad->padaddedid);
672 oggpad->padaddedid = 0;
676 /* runs typefind on the packet, which is assumed to be the first
677 * packet in the stream.
679 * Based on the type returned from the typefind function, an element
680 * is created to help in conversion between granulepos and timestamps
681 * so that we can do decent seeking.
684 gst_ogg_pad_typefind (GstOggPad * pad, ogg_packet * packet)
688 GstElement *element = NULL;
690 #ifndef GST_DISABLE_GST_DEBUG
691 GstOggDemux *ogg = pad->ogg;
694 if (GST_PAD_CAPS (pad) != NULL)
697 /* The ogg spec defines that the first packet of an ogg stream must identify
698 * the stream. Therefore ogg can use a simplified approach to typefinding
699 * and only needs to check the first packet */
700 buf = gst_buffer_new ();
701 GST_BUFFER_DATA (buf) = packet->packet;
702 GST_BUFFER_SIZE (buf) = packet->bytes;
703 GST_BUFFER_OFFSET (buf) = 0;
705 caps = gst_type_find_helper_for_buffer (GST_OBJECT (pad), buf, NULL);
706 gst_buffer_unref (buf);
709 GST_WARNING_OBJECT (ogg,
710 "couldn't find caps for stream with serial %08x", pad->serialno);
711 caps = gst_caps_new_simple ("application/octet-stream", NULL);
713 /* ogg requires you to use a decoder element to define the
714 * meaning of granulepos etc so we make one. We also do this if
715 * we are in the streaming mode to calculate the first timestamp. */
718 GST_LOG_OBJECT (ogg, "found caps: %" GST_PTR_FORMAT, caps);
720 /* first filter out the interesting element factories */
721 factories = gst_default_registry_feature_filter (
722 (GstPluginFeatureFilter) gst_ogg_demux_factory_filter, FALSE, caps);
724 /* sort them according to their ranks */
725 factories = g_list_sort (factories, (GCompareFunc) compare_ranks);
727 /* then pick the first factory to create an element */
730 gst_element_factory_create (GST_ELEMENT_FACTORY (factories->data),
733 GstPadTemplate *template;
736 gst_object_ref (element);
737 gst_object_sink (GST_OBJECT (element));
739 /* FIXME, it might not be named "sink" */
740 pad->elem_pad = gst_element_get_static_pad (element, "sink");
741 gst_element_set_state (element, GST_STATE_PAUSED);
742 template = gst_static_pad_template_get (&internaltemplate);
743 pad->elem_out = gst_pad_new_from_template (template, "internal");
744 gst_pad_set_chain_function (pad->elem_out, gst_ogg_pad_internal_chain);
745 gst_pad_set_element_private (pad->elem_out, pad);
746 gst_pad_set_active (pad->elem_out, TRUE);
747 gst_object_unref (template);
749 /* and this pad may not be named src.. */
750 /* And it might also not exist at this time... */
754 p = gst_element_get_static_pad (element, "src");
756 gst_pad_link (p, pad->elem_out);
757 gst_object_unref (p);
760 pad->padaddedid = g_signal_connect (G_OBJECT (element),
761 "pad-added", G_CALLBACK (internal_element_pad_added_cb), pad);
766 gst_plugin_feature_list_free (factories);
768 pad->element = element;
770 gst_pad_set_caps (GST_PAD (pad), caps);
771 gst_caps_unref (caps);
776 /* send packet to internal element */
778 gst_ogg_demux_chain_elem_pad (GstOggPad * pad, ogg_packet * packet)
783 #ifndef GST_DISABLE_GST_DEBUG
784 GstOggDemux *ogg = pad->ogg;
787 /* initialize our internal decoder with packets */
791 GST_DEBUG_OBJECT (ogg, "%p init decoder serial %08x", pad, pad->serialno);
793 buf = gst_buffer_new_and_alloc (packet->bytes);
794 memcpy (GST_BUFFER_DATA (buf), packet->packet, packet->bytes);
795 gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
796 GST_BUFFER_OFFSET (buf) = -1;
797 GST_BUFFER_OFFSET_END (buf) = packet->granulepos;
799 ret = gst_pad_chain (pad->elem_pad, buf);
800 if (GST_FLOW_IS_FATAL (ret))
807 GST_WARNING_OBJECT (ogg,
808 "pad %p does not have elem_pad, no decoder ?", pad);
809 return GST_FLOW_ERROR;
813 GST_WARNING_OBJECT (ogg, "internal decoder error");
818 /* queue data, basically takes the packet, puts it in a buffer and store the
819 * buffer in the headers list.
822 gst_ogg_demux_queue_data (GstOggPad * pad, ogg_packet * packet)
826 #ifndef GST_DISABLE_GST_DEBUG
827 GstOggDemux *ogg = pad->ogg;
830 GST_DEBUG_OBJECT (ogg, "%p queueing data serial %08x", pad, pad->serialno);
832 buf = gst_buffer_new_and_alloc (packet->bytes);
833 memcpy (buf->data, packet->packet, packet->bytes);
834 GST_BUFFER_OFFSET (buf) = -1;
835 GST_BUFFER_OFFSET_END (buf) = packet->granulepos;
836 pad->headers = g_list_append (pad->headers, buf);
842 /* send packet to internal element */
844 gst_ogg_demux_chain_peer (GstOggPad * pad, ogg_packet * packet)
847 GstFlowReturn ret, cret;
848 GstOggDemux *ogg = pad->ogg;
853 GST_DEBUG_OBJECT (ogg,
854 "%p streaming to peer serial %08x", pad, pad->serialno);
857 gst_pad_alloc_buffer_and_set_caps (GST_PAD_CAST (pad),
858 GST_BUFFER_OFFSET_NONE, packet->bytes, GST_PAD_CAPS (pad), &buf);
861 cret = gst_ogg_demux_combine_flows (ogg, pad, ret);
862 if (ret != GST_FLOW_OK)
865 /* copy packet in buffer */
866 memcpy (buf->data, packet->packet, packet->bytes);
868 GST_BUFFER_OFFSET (buf) = -1;
869 GST_BUFFER_OFFSET_END (buf) = packet->granulepos;
871 /* Mark discont on the buffer */
873 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
874 pad->discont = FALSE;
877 ret = gst_pad_push (GST_PAD_CAST (pad), buf);
880 cret = gst_ogg_demux_combine_flows (ogg, pad, ret);
882 /* we're done with skeleton stuff */
883 if (pad->is_skeleton)
886 /* check if valid granulepos, then we can calculate the current
888 if (packet->granulepos < 0)
891 /* store current granule pos */
892 ogg->current_granule = packet->granulepos;
894 /* convert to time */
895 format = GST_FORMAT_TIME;
896 if (!gst_ogg_pad_query_convert (pad,
897 GST_FORMAT_DEFAULT, packet->granulepos, &format,
898 (gint64 *) & current_time))
901 /* convert to stream time */
902 if ((chain = pad->chain)) {
903 gint64 chain_start = 0;
905 if (chain->segment_start != GST_CLOCK_TIME_NONE)
906 chain_start = chain->segment_start;
908 current_time = current_time - chain_start + chain->begin_time;
911 /* and store as the current position */
912 gst_segment_set_last_stop (&ogg->segment, GST_FORMAT_TIME, current_time);
914 GST_DEBUG_OBJECT (ogg, "ogg current time %" GST_TIME_FORMAT,
915 GST_TIME_ARGS (current_time));
918 /* return combined flow result */
924 GST_DEBUG_OBJECT (ogg,
925 "%p could not get buffer from peer %08x, %d (%s), combined %d (%s)",
926 pad, pad->serialno, ret, gst_flow_get_name (ret),
927 cret, gst_flow_get_name (cret));
932 GST_WARNING_OBJECT (ogg, "could not convert granulepos to time");
937 /* submit a packet to the oggpad, this function will run the
938 * typefind code for the pad if this is the first packet for this
942 gst_ogg_pad_submit_packet (GstOggPad * pad, ogg_packet * packet)
947 GstOggDemux *ogg = pad->ogg;
949 GST_DEBUG_OBJECT (ogg, "%p submit packet serial %08x", pad, pad->serialno);
951 if (!pad->have_type) {
952 if (!ogg->have_fishead && packet->bytes == SKELETON_FISHEAD_SIZE &&
953 !memcmp (packet->packet, "fishead\0", 8)) {
954 gst_ogg_pad_parse_skeleton_fishead (pad, packet);
956 gst_ogg_pad_typefind (pad, packet);
957 pad->have_type = TRUE;
960 if (ogg->have_fishead && packet->bytes >= SKELETON_FISBONE_MIN_SIZE &&
961 !memcmp (packet->packet, "fisbone\0", 8)) {
962 gst_ogg_pad_parse_skeleton_fisbone (pad, packet);
965 granule = packet->granulepos;
967 GST_DEBUG_OBJECT (ogg, "%p has granulepos %" G_GINT64_FORMAT, pad, granule);
968 ogg->current_granule = granule;
969 pad->current_granule = granule;
970 /* granulepos 0 and -1 are considered header packets.
971 * Note that since theora is busted, it can create non-header
972 * packets with 0 granulepos. We will correct for this when our
973 * internal decoder produced a frame and we don't have a
974 * granulepos because in that case the granulpos must have been 0 */
975 if (pad->first_granule == -1 && granule != 0) {
976 GST_DEBUG_OBJECT (ogg, "%p found first granulepos %" G_GINT64_FORMAT, pad,
978 pad->first_granule = granule;
982 if (granule != -1 && memcmp (packet->packet, "KW-DIRAC", 8) == 0) {
986 /* no start time known, stream to internal plugin to
987 * get time. always stream to the skeleton decoder */
988 if (pad->start_time == GST_CLOCK_TIME_NONE || pad->is_skeleton) {
989 ret = gst_ogg_demux_chain_elem_pad (pad, packet);
991 /* we know the start_time of the pad data, see if we
992 * can activate the complete chain if this is a dynamic
994 if (pad->start_time != GST_CLOCK_TIME_NONE) {
995 GstOggChain *chain = pad->chain;
997 /* correction for busted ogg, if the internal decoder outputed
998 * a timestamp but we did not get a granulepos, it must have
999 * been 0 and the time is therefore also 0 */
1000 if (pad->first_granule == -1) {
1001 GST_DEBUG_OBJECT (ogg, "%p found start time without granulepos", pad);
1002 pad->first_granule = 0;
1003 pad->first_time = 0;
1006 /* check if complete chain has start time */
1007 if (chain == ogg->building_chain) {
1009 /* see if we have enough info to activate the chain, we have enough info
1010 * when all streams have a valid start time. */
1011 if (gst_ogg_demux_collect_chain_info (ogg, chain)) {
1014 GST_DEBUG_OBJECT (ogg, "segment_start: %" GST_TIME_FORMAT,
1015 GST_TIME_ARGS (chain->segment_start));
1016 GST_DEBUG_OBJECT (ogg, "segment_stop: %" GST_TIME_FORMAT,
1017 GST_TIME_ARGS (chain->segment_stop));
1018 GST_DEBUG_OBJECT (ogg, "segment_time: %" GST_TIME_FORMAT,
1019 GST_TIME_ARGS (chain->begin_time));
1021 /* create the newsegment event we are going to send out */
1022 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
1023 GST_FORMAT_TIME, chain->segment_start, chain->segment_stop,
1025 gst_event_set_seqnum (event, ogg->seqnum);
1027 gst_ogg_demux_activate_chain (ogg, chain, event);
1029 ogg->building_chain = NULL;
1034 /* if we are building a chain, store buffer for when we activate
1035 * it. This path is taken if we operate in streaming mode. */
1036 if (ogg->building_chain) {
1037 ret = gst_ogg_demux_queue_data (pad, packet);
1039 /* else we are completely streaming to the peer */
1041 ret = gst_ogg_demux_chain_peer (pad, packet);
1046 /* flush at most @npackets from the stream layer. All packets if
1049 static GstFlowReturn
1050 gst_ogg_pad_stream_out (GstOggPad * pad, gint npackets)
1052 GstFlowReturn result = GST_FLOW_OK;
1053 gboolean done = FALSE;
1062 ret = ogg_stream_packetout (&pad->stream, &packet);
1065 GST_LOG_OBJECT (ogg, "packetout done");
1069 GST_LOG_OBJECT (ogg, "packetout discont");
1070 gst_ogg_chain_mark_discont (pad->chain);
1073 GST_LOG_OBJECT (ogg, "packetout gave packet of size %ld", packet.bytes);
1074 result = gst_ogg_pad_submit_packet (pad, &packet);
1075 if (GST_FLOW_IS_FATAL (result))
1076 goto could_not_submit;
1079 GST_WARNING_OBJECT (ogg,
1080 "invalid return value %d for ogg_stream_packetout, resetting stream",
1082 gst_ogg_pad_reset (pad);
1087 done = (npackets == 0);
1095 GST_WARNING_OBJECT (ogg,
1096 "could not submit packet for stream %08x, error: %d", pad->serialno,
1098 gst_ogg_pad_reset (pad);
1103 /* submit a page to an oggpad, this function will then submit all
1104 * the packets in the page.
1106 static GstFlowReturn
1107 gst_ogg_pad_submit_page (GstOggPad * pad, ogg_page * page)
1109 GstFlowReturn result = GST_FLOW_OK;
1111 gboolean continued = FALSE;
1115 /* for negative rates we read pages backwards and must therefore be carefull
1116 * with continued pages */
1117 if (ogg->segment.rate < 0.0) {
1120 continued = ogg_page_continued (page);
1122 /* number of completed packets in the page */
1123 npackets = ogg_page_packets (page);
1125 /* page is not continued so it contains at least one packet start. It's
1126 * possible that no packet ends on this page (npackets == 0). In that
1127 * case, the next (continued) page(s) we kept contain the remainder of the
1128 * packets. We mark npackets=1 to make us start decoding the pages in the
1129 * remainder of the algorithm. */
1133 GST_LOG_OBJECT (ogg, "continued: %d, %d packets", continued, npackets);
1135 if (npackets == 0) {
1136 GST_LOG_OBJECT (ogg, "no decodable packets, we need a previous page");
1141 if (ogg_stream_pagein (&pad->stream, page) != 0)
1144 /* flush all packets in the stream layer, this might not give a packet if
1145 * the page had no packets finishing on the page (npackets == 0). */
1146 result = gst_ogg_pad_stream_out (pad, 0);
1148 if (pad->continued) {
1151 /* now send the continued pages to the stream layer */
1152 while (pad->continued) {
1153 ogg_page *p = (ogg_page *) pad->continued->data;
1155 GST_LOG_OBJECT (ogg, "submitting continued page %p", p);
1156 if (ogg_stream_pagein (&pad->stream, p) != 0)
1159 pad->continued = g_list_delete_link (pad->continued, pad->continued);
1162 gst_ogg_page_free (p);
1165 GST_LOG_OBJECT (ogg, "flushing last continued packet");
1166 /* flush 1 continued packet in the stream layer */
1167 result = gst_ogg_pad_stream_out (pad, 1);
1169 /* flush all remaining packets, we pushed them in the previous round.
1170 * We don't use _reset() because we still want to get the discont when
1171 * we submit a next page. */
1172 while (ogg_stream_packetout (&pad->stream, &packet) != 0);
1176 /* keep continued pages (only in reverse mode) */
1178 ogg_page *p = gst_ogg_page_copy (page);
1180 GST_LOG_OBJECT (ogg, "keeping continued page %p", p);
1181 pad->continued = g_list_prepend (pad->continued, p);
1188 GST_WARNING_OBJECT (ogg,
1189 "ogg stream choked on page (serial %08x), resetting stream",
1191 gst_ogg_pad_reset (pad);
1192 /* we continue to recover */
1198 static GstOggChain *
1199 gst_ogg_chain_new (GstOggDemux * ogg)
1201 GstOggChain *chain = g_new0 (GstOggChain, 1);
1203 GST_DEBUG_OBJECT (ogg, "creating new chain %p", chain);
1207 chain->have_bos = FALSE;
1208 chain->streams = g_array_new (FALSE, TRUE, sizeof (GstOggPad *));
1209 chain->begin_time = GST_CLOCK_TIME_NONE;
1210 chain->segment_start = GST_CLOCK_TIME_NONE;
1211 chain->segment_stop = GST_CLOCK_TIME_NONE;
1212 chain->total_time = GST_CLOCK_TIME_NONE;
1218 gst_ogg_chain_free (GstOggChain * chain)
1222 for (i = 0; i < chain->streams->len; i++) {
1223 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1225 gst_object_unref (pad);
1227 g_array_free (chain->streams, TRUE);
1232 gst_ogg_chain_mark_discont (GstOggChain * chain)
1236 for (i = 0; i < chain->streams->len; i++) {
1237 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1239 pad->discont = TRUE;
1244 gst_ogg_chain_reset (GstOggChain * chain)
1248 for (i = 0; i < chain->streams->len; i++) {
1249 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1251 gst_ogg_pad_reset (pad);
1256 gst_ogg_chain_new_stream (GstOggChain * chain, glong serialno)
1262 GST_DEBUG_OBJECT (chain->ogg, "creating new stream %08lx in chain %p",
1265 ret = g_object_new (GST_TYPE_OGG_PAD, NULL);
1266 /* we own this one */
1267 gst_object_ref (ret);
1268 gst_object_sink (ret);
1270 GST_PAD_DIRECTION (ret) = GST_PAD_SRC;
1271 ret->discont = TRUE;
1274 ret->ogg = chain->ogg;
1276 ret->serialno = serialno;
1277 if (ogg_stream_init (&ret->stream, serialno) != 0)
1280 name = g_strdup_printf ("serial_%08lx", serialno);
1281 gst_object_set_name (GST_OBJECT (ret), name);
1284 /* FIXME: either do something with it or remove it */
1285 list = gst_tag_list_new ();
1286 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_SERIAL, serialno,
1288 gst_tag_list_free (list);
1290 GST_DEBUG_OBJECT (chain->ogg,
1291 "created new ogg src %p for stream with serial %08lx", ret, serialno);
1293 g_array_append_val (chain->streams, ret);
1300 GST_ERROR ("Could not initialize ogg_stream struct for serial %08lx.",
1302 gst_object_unref (ret);
1308 gst_ogg_chain_get_stream (GstOggChain * chain, glong serialno)
1312 for (i = 0; i < chain->streams->len; i++) {
1313 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1315 if (pad->serialno == serialno)
1322 gst_ogg_chain_has_stream (GstOggChain * chain, glong serialno)
1324 return gst_ogg_chain_get_stream (chain, serialno) != NULL;
1327 #define CURRENT_CHAIN(ogg) (&g_array_index ((ogg)->chains, GstOggChain, (ogg)->current_chain))
1329 /* signals and args */
1342 static GstStaticPadTemplate ogg_demux_src_template_factory =
1343 GST_STATIC_PAD_TEMPLATE ("src_%d",
1346 GST_STATIC_CAPS_ANY);
1348 static GstStaticPadTemplate ogg_demux_sink_template_factory =
1349 GST_STATIC_PAD_TEMPLATE ("sink",
1352 GST_STATIC_CAPS ("application/ogg; application/x-annodex")
1355 static void gst_ogg_demux_finalize (GObject * object);
1357 //static const GstEventMask *gst_ogg_demux_get_event_masks (GstPad * pad);
1358 //static const GstQueryType *gst_ogg_demux_get_query_types (GstPad * pad);
1359 static GstFlowReturn gst_ogg_demux_read_chain (GstOggDemux * ogg,
1360 GstOggChain ** chain);
1361 static GstFlowReturn gst_ogg_demux_read_end_chain (GstOggDemux * ogg,
1362 GstOggChain * chain);
1364 static gboolean gst_ogg_demux_sink_event (GstPad * pad, GstEvent * event);
1365 static void gst_ogg_demux_loop (GstOggPad * pad);
1366 static GstFlowReturn gst_ogg_demux_chain (GstPad * pad, GstBuffer * buffer);
1367 static gboolean gst_ogg_demux_sink_activate (GstPad * sinkpad);
1368 static gboolean gst_ogg_demux_sink_activate_pull (GstPad * sinkpad,
1370 static gboolean gst_ogg_demux_sink_activate_push (GstPad * sinkpad,
1372 static GstStateChangeReturn gst_ogg_demux_change_state (GstElement * element,
1373 GstStateChange transition);
1374 static void gst_ogg_demux_send_event (GstOggDemux * ogg, GstEvent * event);
1376 static void gst_ogg_print (GstOggDemux * demux);
1378 GST_BOILERPLATE (GstOggDemux, gst_ogg_demux, GstElement, GST_TYPE_ELEMENT);
1381 gst_ogg_demux_base_init (gpointer g_class)
1383 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1385 gst_element_class_set_details (element_class, &gst_ogg_demux_details);
1387 gst_element_class_add_pad_template (element_class,
1388 gst_static_pad_template_get (&ogg_demux_sink_template_factory));
1389 gst_element_class_add_pad_template (element_class,
1390 gst_static_pad_template_get (&ogg_demux_src_template_factory));
1394 gst_ogg_demux_class_init (GstOggDemuxClass * klass)
1396 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1397 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1399 gstelement_class->change_state = gst_ogg_demux_change_state;
1400 gstelement_class->send_event = gst_ogg_demux_receive_event;
1402 gobject_class->finalize = gst_ogg_demux_finalize;
1406 gst_ogg_demux_init (GstOggDemux * ogg, GstOggDemuxClass * g_class)
1408 /* create the sink pad */
1410 gst_pad_new_from_static_template (&ogg_demux_sink_template_factory,
1413 gst_pad_set_event_function (ogg->sinkpad, gst_ogg_demux_sink_event);
1414 gst_pad_set_chain_function (ogg->sinkpad, gst_ogg_demux_chain);
1415 gst_pad_set_activate_function (ogg->sinkpad, gst_ogg_demux_sink_activate);
1416 gst_pad_set_activatepull_function (ogg->sinkpad,
1417 gst_ogg_demux_sink_activate_pull);
1418 gst_pad_set_activatepush_function (ogg->sinkpad,
1419 gst_ogg_demux_sink_activate_push);
1420 gst_element_add_pad (GST_ELEMENT (ogg), ogg->sinkpad);
1422 ogg->chain_lock = g_mutex_new ();
1423 ogg->chains = g_array_new (FALSE, TRUE, sizeof (GstOggChain *));
1425 ogg->newsegment = NULL;
1429 gst_ogg_demux_finalize (GObject * object)
1433 ogg = GST_OGG_DEMUX (object);
1435 g_array_free (ogg->chains, TRUE);
1436 g_mutex_free (ogg->chain_lock);
1437 ogg_sync_clear (&ogg->sync);
1439 if (ogg->newsegment)
1440 gst_event_unref (ogg->newsegment);
1442 G_OBJECT_CLASS (parent_class)->finalize (object);
1446 gst_ogg_demux_sink_event (GstPad * pad, GstEvent * event)
1451 ogg = GST_OGG_DEMUX (gst_pad_get_parent (pad));
1453 switch (GST_EVENT_TYPE (event)) {
1454 case GST_EVENT_NEWSEGMENT:
1456 GST_DEBUG_OBJECT (ogg, "got a new segment event");
1457 ogg_sync_reset (&ogg->sync);
1458 gst_event_unref (event);
1463 GST_DEBUG_OBJECT (ogg, "got an EOS event");
1464 res = gst_pad_event_default (pad, event);
1465 if (ogg->current_chain == NULL) {
1466 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL),
1467 ("can't get first chain"));
1472 res = gst_pad_event_default (pad, event);
1475 gst_object_unref (ogg);
1480 /* submit the given buffer to the ogg sync.
1482 * Returns the number of bytes submited.
1484 static GstFlowReturn
1485 gst_ogg_demux_submit_buffer (GstOggDemux * ogg, GstBuffer * buffer)
1490 GstFlowReturn ret = GST_FLOW_OK;
1492 size = GST_BUFFER_SIZE (buffer);
1493 data = GST_BUFFER_DATA (buffer);
1495 GST_DEBUG_OBJECT (ogg, "submitting %u bytes", size);
1496 if (G_UNLIKELY (size == 0))
1499 oggbuffer = ogg_sync_buffer (&ogg->sync, size);
1500 if (G_UNLIKELY (oggbuffer == NULL))
1503 memcpy (oggbuffer, data, size);
1504 if (G_UNLIKELY (ogg_sync_wrote (&ogg->sync, size) < 0))
1508 gst_buffer_unref (buffer);
1515 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
1516 (NULL), ("failed to get ogg sync buffer"));
1517 ret = GST_FLOW_ERROR;
1522 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
1523 (NULL), ("failed to write %d bytes to the sync buffer", size));
1524 ret = GST_FLOW_ERROR;
1529 /* in random access mode this code updates the current read position
1530 * and resets the ogg sync buffer so that the next read will happen
1531 * from this new location.
1534 gst_ogg_demux_seek (GstOggDemux * ogg, gint64 offset)
1536 GST_LOG_OBJECT (ogg, "seeking to %" G_GINT64_FORMAT, offset);
1538 ogg->offset = offset;
1539 ogg->read_offset = offset;
1540 ogg_sync_reset (&ogg->sync);
1543 /* read more data from the current offset and submit to
1544 * the ogg sync layer.
1546 static GstFlowReturn
1547 gst_ogg_demux_get_data (GstOggDemux * ogg, gint64 end_offset)
1552 GST_LOG_OBJECT (ogg,
1553 "get data %" G_GINT64_FORMAT " %" G_GINT64_FORMAT " %" G_GINT64_FORMAT,
1554 ogg->read_offset, ogg->length, end_offset);
1556 if (end_offset > 0 && ogg->read_offset >= end_offset)
1557 goto boundary_reached;
1559 if (ogg->read_offset == ogg->length)
1562 ret = gst_pad_pull_range (ogg->sinkpad, ogg->read_offset, CHUNKSIZE, &buffer);
1563 if (ret != GST_FLOW_OK)
1566 ogg->read_offset += GST_BUFFER_SIZE (buffer);
1568 ret = gst_ogg_demux_submit_buffer (ogg, buffer);
1575 GST_LOG_OBJECT (ogg, "reached boundary");
1576 return GST_FLOW_LIMIT;
1580 GST_LOG_OBJECT (ogg, "reached EOS");
1581 return GST_FLOW_UNEXPECTED;
1585 GST_WARNING_OBJECT (ogg, "got %d (%s) from pull range", ret,
1586 gst_flow_get_name (ret));
1591 /* Read the next page from the current offset.
1592 * boundary: number of bytes ahead we allow looking for;
1595 * @offset will contain the offset the next page starts at when this function
1596 * returns GST_FLOW_OK.
1598 * GST_FLOW_UNEXPECTED is returned on EOS.
1600 * GST_FLOW_LIMIT is returned when we did not find a page before the
1601 * boundary. If @boundary is -1, this is never returned.
1603 * Any other error returned while retrieving data from the peer is returned as
1606 static GstFlowReturn
1607 gst_ogg_demux_get_next_page (GstOggDemux * ogg, ogg_page * og, gint64 boundary,
1610 gint64 end_offset = -1;
1613 GST_LOG_OBJECT (ogg,
1614 "get next page, current offset %" G_GINT64_FORMAT ", bytes boundary %"
1615 G_GINT64_FORMAT, ogg->offset, boundary);
1618 end_offset = ogg->offset + boundary;
1623 if (end_offset > 0 && ogg->offset >= end_offset)
1624 goto boundary_reached;
1626 more = ogg_sync_pageseek (&ogg->sync, og);
1628 GST_LOG_OBJECT (ogg, "pageseek gave %ld", more);
1631 /* skipped n bytes */
1632 ogg->offset -= more;
1633 GST_LOG_OBJECT (ogg, "skipped %ld bytes, offset %" G_GINT64_FORMAT, more,
1635 } else if (more == 0) {
1636 /* we need more data */
1638 goto boundary_reached;
1640 GST_LOG_OBJECT (ogg, "need more data");
1641 ret = gst_ogg_demux_get_data (ogg, end_offset);
1642 if (ret != GST_FLOW_OK)
1645 gint64 res_offset = ogg->offset;
1647 /* got a page. Return the offset at the page beginning,
1648 advance the internal offset past the page end */
1650 *offset = res_offset;
1653 ogg->offset += more;
1655 GST_LOG_OBJECT (ogg,
1656 "got page at %" G_GINT64_FORMAT ", serial %08x, end at %"
1657 G_GINT64_FORMAT ", granule %" G_GINT64_FORMAT, res_offset,
1658 ogg_page_serialno (og), ogg->offset, ogg_page_granulepos (og));
1662 GST_LOG_OBJECT (ogg, "returning %d", ret);
1669 GST_LOG_OBJECT (ogg,
1670 "offset %" G_GINT64_FORMAT " >= end_offset %" G_GINT64_FORMAT,
1671 ogg->offset, end_offset);
1672 return GST_FLOW_LIMIT;
1676 /* from the current offset, find the previous page, seeking backwards
1677 * until we find the page.
1679 static GstFlowReturn
1680 gst_ogg_demux_get_prev_page (GstOggDemux * ogg, ogg_page * og, gint64 * offset)
1683 gint64 begin = ogg->offset;
1685 gint64 cur_offset = -1;
1687 GST_LOG_OBJECT (ogg, "getting page before %" G_GINT64_FORMAT, begin);
1689 while (cur_offset == -1) {
1694 /* seek CHUNKSIZE back */
1695 gst_ogg_demux_seek (ogg, begin);
1697 /* now continue reading until we run out of data, if we find a page
1698 * start, we save it. It might not be the final page as there could be
1699 * another page after this one. */
1700 while (ogg->offset < end) {
1704 gst_ogg_demux_get_next_page (ogg, og, end - ogg->offset, &new_offset);
1705 /* we hit the upper limit, offset contains the last page start */
1706 if (ret == GST_FLOW_LIMIT) {
1707 GST_LOG_OBJECT (ogg, "hit limit");
1710 /* something went wrong */
1711 if (ret == GST_FLOW_UNEXPECTED) {
1713 GST_LOG_OBJECT (ogg, "got unexpected");
1714 } else if (ret != GST_FLOW_OK) {
1715 GST_LOG_OBJECT (ogg, "got error %d", ret);
1719 GST_LOG_OBJECT (ogg, "found page at %" G_GINT64_FORMAT, new_offset);
1721 /* offset is next page start */
1722 cur_offset = new_offset;
1726 GST_LOG_OBJECT (ogg, "found previous page at %" G_GINT64_FORMAT, cur_offset);
1728 /* we have the offset. Actually snork and hold the page now */
1729 gst_ogg_demux_seek (ogg, cur_offset);
1730 ret = gst_ogg_demux_get_next_page (ogg, og, -1, NULL);
1731 if (ret != GST_FLOW_OK) {
1732 GST_WARNING_OBJECT (ogg, "can't get last page at %" G_GINT64_FORMAT,
1734 /* this shouldn't be possible */
1739 *offset = cur_offset;
1745 gst_ogg_demux_deactivate_current_chain (GstOggDemux * ogg)
1748 GstOggChain *chain = ogg->current_chain;
1753 GST_DEBUG_OBJECT (ogg, "deactivating chain %p", chain);
1755 /* send EOS on all the pads */
1756 for (i = 0; i < chain->streams->len; i++) {
1757 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
1760 if (pad->is_skeleton)
1763 event = gst_event_new_eos ();
1764 gst_event_set_seqnum (event, ogg->seqnum);
1765 gst_pad_push_event (GST_PAD_CAST (pad), event);
1767 GST_DEBUG_OBJECT (ogg, "removing pad %" GST_PTR_FORMAT, pad);
1769 /* deactivate first */
1770 gst_pad_set_active (GST_PAD_CAST (pad), FALSE);
1772 gst_element_remove_pad (GST_ELEMENT (ogg), GST_PAD_CAST (pad));
1774 /* if we cannot seek back to the chain, we can destroy the chain
1776 if (!ogg->seekable) {
1777 gst_ogg_chain_free (chain);
1779 ogg->current_chain = NULL;
1785 gst_ogg_demux_activate_chain (GstOggDemux * ogg, GstOggChain * chain,
1790 if (chain == ogg->current_chain) {
1792 gst_event_unref (event);
1796 /* FIXME, should not be called with NULL */
1797 if (chain != NULL) {
1798 GST_DEBUG_OBJECT (ogg, "activating chain %p", chain);
1800 /* first add the pads */
1801 for (i = 0; i < chain->streams->len; i++) {
1804 pad = g_array_index (chain->streams, GstOggPad *, i);
1806 if (pad->is_skeleton)
1809 GST_DEBUG_OBJECT (ogg, "adding pad %" GST_PTR_FORMAT, pad);
1812 pad->discont = TRUE;
1813 pad->last_ret = GST_FLOW_OK;
1815 /* activate first */
1816 gst_pad_set_active (GST_PAD_CAST (pad), TRUE);
1818 gst_element_add_pad (GST_ELEMENT (ogg), GST_PAD_CAST (pad));
1822 /* after adding the new pads, remove the old pads */
1823 gst_ogg_demux_deactivate_current_chain (ogg);
1825 ogg->current_chain = chain;
1827 /* we are finished now */
1828 gst_element_no_more_pads (GST_ELEMENT (ogg));
1830 /* FIXME, must be sent from the streaming thread */
1832 gst_ogg_demux_send_event (ogg, event);
1834 gst_element_found_tags (GST_ELEMENT_CAST (ogg),
1835 gst_tag_list_new_full (GST_TAG_CONTAINER_FORMAT, "Ogg", NULL));
1838 GST_DEBUG_OBJECT (ogg, "starting chain");
1840 /* then send out any queued buffers */
1841 for (i = 0; i < chain->streams->len; i++) {
1845 pad = g_array_index (chain->streams, GstOggPad *, i);
1847 for (headers = pad->headers; headers; headers = g_list_next (headers)) {
1848 GstBuffer *buffer = GST_BUFFER (headers->data);
1851 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1852 pad->discont = FALSE;
1855 /* we don't care about the return value here */
1856 gst_pad_push (GST_PAD_CAST (pad), buffer);
1858 /* and free the headers */
1859 g_list_free (pad->headers);
1860 pad->headers = NULL;
1866 * do seek to time @position, return FALSE or chain and TRUE
1869 gst_ogg_demux_do_seek (GstOggDemux * ogg, GstSegment * segment,
1870 gboolean accurate, GstOggChain ** rchain)
1873 GstOggChain *chain = NULL;
1875 gint64 begintime, endtime;
1883 position = segment->last_stop;
1885 /* first find the chain to search in */
1886 total = ogg->total_time;
1887 if (ogg->chains->len == 0)
1890 for (i = ogg->chains->len - 1; i >= 0; i--) {
1891 chain = g_array_index (ogg->chains, GstOggChain *, i);
1892 total -= chain->total_time;
1893 if (position >= total)
1897 begin = chain->offset;
1898 end = chain->end_offset;
1899 begintime = chain->begin_time;
1900 endtime = begintime + chain->total_time;
1901 target = position - total + begintime;
1903 if (segment->rate > 0.0) {
1904 /* FIXME, seek 2 seconds early to catch keyframes, better implement
1905 * keyframe detection. */
1906 if (target - 2 * GST_SECOND > begintime)
1907 target = target - (gint64) 2 *GST_SECOND;
1911 if (target + GST_SECOND < endtime)
1912 target = target + (gint64) GST_SECOND;
1919 GST_DEBUG_OBJECT (ogg,
1920 "seeking to %" GST_TIME_FORMAT " in chain %p",
1921 GST_TIME_ARGS (position), chain);
1922 GST_DEBUG_OBJECT (ogg,
1923 "chain offset %" G_GINT64_FORMAT ", end offset %" G_GINT64_FORMAT, begin,
1925 GST_DEBUG_OBJECT (ogg,
1926 "chain begin time %" GST_TIME_FORMAT ", end time %" GST_TIME_FORMAT,
1927 GST_TIME_ARGS (begintime), GST_TIME_ARGS (endtime));
1928 GST_DEBUG_OBJECT (ogg, "target %" GST_TIME_FORMAT, GST_TIME_ARGS (target));
1930 /* perform the seek */
1931 while (begin < end) {
1934 if ((end - begin < CHUNKSIZE) || (endtime == begintime)) {
1937 /* take a (pretty decent) guess, avoiding overflow */
1938 gint64 rate = (end - begin) * GST_MSECOND / (endtime - begintime);
1940 bisect = (target - begintime) / GST_MSECOND * rate + begin - CHUNKSIZE;
1942 if (bisect <= begin)
1944 GST_DEBUG_OBJECT (ogg, "Initial guess: %" G_GINT64_FORMAT, bisect);
1946 gst_ogg_demux_seek (ogg, bisect);
1948 while (begin < end) {
1951 GST_DEBUG_OBJECT (ogg,
1952 "after seek, bisect %" G_GINT64_FORMAT ", begin %" G_GINT64_FORMAT
1953 ", end %" G_GINT64_FORMAT, bisect, begin, end);
1955 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, &result);
1956 GST_LOG_OBJECT (ogg, "looking for next page returned %" G_GINT64_FORMAT,
1959 if (ret == GST_FLOW_LIMIT) {
1960 /* we hit the upper limit, go back a bit */
1961 if (bisect <= begin + 1) {
1962 end = begin; /* found it */
1967 bisect -= CHUNKSIZE;
1968 if (bisect <= begin)
1971 gst_ogg_demux_seek (ogg, bisect);
1973 } else if (ret == GST_FLOW_OK) {
1974 /* found offset of next ogg page */
1976 GstClockTime granuletime;
1980 GST_LOG_OBJECT (ogg, "found next ogg page at %" G_GINT64_FORMAT,
1982 granulepos = ogg_page_granulepos (&og);
1983 if (granulepos == -1) {
1984 GST_LOG_OBJECT (ogg, "granulepos of next page is -1");
1988 pad = gst_ogg_chain_get_stream (chain, ogg_page_serialno (&og));
1989 if (pad == NULL || pad->is_skeleton)
1992 format = GST_FORMAT_TIME;
1993 if (!gst_ogg_pad_query_convert (pad,
1994 GST_FORMAT_DEFAULT, granulepos, &format,
1995 (gint64 *) & granuletime)) {
1996 GST_WARNING_OBJECT (ogg, "could not convert granulepos to time");
1997 granuletime = target;
1999 if (granuletime < pad->start_time)
2001 GST_LOG_OBJECT (ogg, "granulepos %" G_GINT64_FORMAT " maps to time %"
2002 GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (granuletime));
2004 granuletime -= pad->start_time;
2007 GST_DEBUG_OBJECT (ogg,
2008 "found page with granule %" G_GINT64_FORMAT " and time %"
2009 GST_TIME_FORMAT, granulepos, GST_TIME_ARGS (granuletime));
2011 if (granuletime < target) {
2012 best = result; /* raw offset of packet with granulepos */
2013 begin = ogg->offset; /* raw offset of next page */
2014 begintime = granuletime;
2016 if (target - begintime > GST_SECOND)
2019 bisect = begin; /* *not* begin + 1 */
2021 if (bisect <= begin + 1) {
2022 end = begin; /* found it */
2024 if (end == ogg->offset) { /* we're pretty close - we'd be stuck in */
2026 bisect -= CHUNKSIZE; /* an endless loop otherwise. */
2027 if (bisect <= begin)
2029 gst_ogg_demux_seek (ogg, bisect);
2032 endtime = granuletime;
2042 GST_DEBUG_OBJECT (ogg, "seeking to %" G_GINT64_FORMAT, best);
2043 gst_ogg_demux_seek (ogg, best);
2050 GST_DEBUG_OBJECT (ogg, "no chains");
2055 GST_DEBUG_OBJECT (ogg, "got a seek error");
2060 /* does not take ownership of the event */
2062 gst_ogg_demux_perform_seek (GstOggDemux * ogg, GstEvent * event)
2064 GstOggChain *chain = NULL;
2066 gboolean flush, accurate;
2070 GstSeekType cur_type, stop_type;
2077 GST_DEBUG_OBJECT (ogg, "seek with event");
2079 gst_event_parse_seek (event, &rate, &format, &flags,
2080 &cur_type, &cur, &stop_type, &stop);
2082 /* we can only seek on time */
2083 if (format != GST_FORMAT_TIME) {
2084 GST_DEBUG_OBJECT (ogg, "can only seek on TIME");
2087 seqnum = gst_event_get_seqnum (event);
2089 GST_DEBUG_OBJECT (ogg, "seek without event");
2093 seqnum = gst_util_seqnum_next ();
2096 GST_DEBUG_OBJECT (ogg, "seek, rate %g", rate);
2098 flush = flags & GST_SEEK_FLAG_FLUSH;
2099 accurate = flags & GST_SEEK_FLAG_ACCURATE;
2101 /* first step is to unlock the streaming thread if it is
2102 * blocked in a chain call, we do this by starting the flush. because
2103 * we cannot yet hold any streaming lock, we have to protect the chains
2104 * with their own lock. */
2108 tevent = gst_event_new_flush_start ();
2109 gst_event_set_seqnum (tevent, seqnum);
2111 gst_event_ref (tevent);
2112 gst_pad_push_event (ogg->sinkpad, tevent);
2114 GST_CHAIN_LOCK (ogg);
2115 for (i = 0; i < ogg->chains->len; i++) {
2116 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2119 for (j = 0; j < chain->streams->len; j++) {
2120 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, j);
2122 gst_event_ref (tevent);
2123 gst_pad_push_event (GST_PAD (pad), tevent);
2126 GST_CHAIN_UNLOCK (ogg);
2128 gst_event_unref (tevent);
2130 gst_pad_pause_task (ogg->sinkpad);
2133 /* now grab the stream lock so that streaming cannot continue, for
2134 * non flushing seeks when the element is in PAUSED this could block
2136 GST_PAD_STREAM_LOCK (ogg->sinkpad);
2138 if (ogg->segment_running && !flush) {
2139 /* create the segment event to close the current segment */
2140 if ((chain = ogg->current_chain)) {
2142 gint64 chain_start = 0;
2144 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2145 chain_start = chain->segment_start;
2147 newseg = gst_event_new_new_segment (TRUE, ogg->segment.rate,
2148 GST_FORMAT_TIME, ogg->segment.start + chain_start,
2149 ogg->segment.last_stop + chain_start, ogg->segment.time);
2150 /* set the seqnum of the running segment */
2151 gst_event_set_seqnum (newseg, ogg->seqnum);
2153 /* send segment on old chain, FIXME, must be sent from streaming thread. */
2154 gst_ogg_demux_send_event (ogg, newseg);
2159 gst_segment_set_seek (&ogg->segment, rate, format, flags,
2160 cur_type, cur, stop_type, stop, &update);
2163 GST_DEBUG_OBJECT (ogg, "segment positions set to %" GST_TIME_FORMAT "-%"
2164 GST_TIME_FORMAT, GST_TIME_ARGS (ogg->segment.start),
2165 GST_TIME_ARGS (ogg->segment.stop));
2167 /* we need to stop flushing on the srcpad as we're going to use it
2168 * next. We can do this as we have the STREAM lock now. */
2170 tevent = gst_event_new_flush_stop ();
2171 gst_event_set_seqnum (tevent, seqnum);
2172 gst_pad_push_event (ogg->sinkpad, tevent);
2178 /* reset all ogg streams now, need to do this from within the lock to
2179 * make sure the streaming thread is not messing with the stream */
2180 for (i = 0; i < ogg->chains->len; i++) {
2181 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2183 gst_ogg_chain_reset (chain);
2187 /* for reverse we will already seek accurately */
2188 res = gst_ogg_demux_do_seek (ogg, &ogg->segment, accurate, &chain);
2190 /* seek failed, make sure we continue the current chain */
2192 GST_DEBUG_OBJECT (ogg, "seek failed");
2193 chain = ogg->current_chain;
2195 GST_DEBUG_OBJECT (ogg, "seek success");
2201 /* now we have a new position, prepare for streaming again */
2206 gint64 last_stop, begin_time;
2208 /* we have to send the flush to the old chain, not the new one */
2210 tevent = gst_event_new_flush_stop ();
2211 gst_event_set_seqnum (tevent, seqnum);
2212 gst_ogg_demux_send_event (ogg, tevent);
2215 /* we need this to see how far inside the chain we need to start */
2216 if (chain->begin_time != GST_CLOCK_TIME_NONE)
2217 begin_time = chain->begin_time;
2221 /* segment.start gives the start over all chains, we calculate the amount
2222 * of time into this chain we need to start */
2223 start = ogg->segment.start - begin_time;
2224 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2225 start += chain->segment_start;
2227 if ((stop = ogg->segment.stop) == -1)
2228 stop = ogg->segment.duration;
2230 /* segment.stop gives the stop time over all chains, calculate the amount of
2231 * time we need to stop in this chain */
2233 if (stop > begin_time)
2237 stop += chain->segment_start;
2238 /* we must stop when this chain ends and switch to the next chain to play
2239 * the remainder of the segment. */
2240 stop = MIN (stop, chain->segment_stop);
2243 last_stop = ogg->segment.last_stop - begin_time;
2244 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2245 last_stop += chain->segment_start;
2247 /* create the segment event we are going to send out */
2248 if (ogg->segment.rate >= 0.0)
2249 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
2250 ogg->segment.format, last_stop, stop, ogg->segment.time);
2252 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
2253 ogg->segment.format, start, last_stop, ogg->segment.time);
2255 gst_event_set_seqnum (event, seqnum);
2257 if (chain != ogg->current_chain) {
2258 /* switch to different chain, send segment on new chain */
2259 gst_ogg_demux_activate_chain (ogg, chain, event);
2261 /* mark discont and send segment on current chain */
2262 gst_ogg_chain_mark_discont (chain);
2263 /* This event should be sent from the streaming thread (sink pad task) */
2264 if (ogg->newsegment)
2265 gst_event_unref (ogg->newsegment);
2266 ogg->newsegment = event;
2269 /* notify start of new segment */
2270 if (ogg->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2271 GstMessage *message;
2273 message = gst_message_new_segment_start (GST_OBJECT (ogg),
2274 GST_FORMAT_TIME, ogg->segment.last_stop);
2275 gst_message_set_seqnum (message, seqnum);
2277 gst_element_post_message (GST_ELEMENT (ogg), message);
2280 ogg->segment_running = TRUE;
2281 ogg->seqnum = seqnum;
2282 /* restart our task since it might have been stopped when we did the
2284 gst_pad_start_task (ogg->sinkpad, (GstTaskFunction) gst_ogg_demux_loop,
2288 /* streaming can continue now */
2289 GST_PAD_STREAM_UNLOCK (ogg->sinkpad);
2295 GST_DEBUG_OBJECT (ogg, "seek failed");
2300 GST_DEBUG_OBJECT (ogg, "no chain to seek in");
2301 GST_PAD_STREAM_UNLOCK (ogg->sinkpad);
2306 /* finds each bitstream link one at a time using a bisection search
2307 * (has to begin by knowing the offset of the lb's initial page).
2308 * Recurses for each link so it can alloc the link storage after
2309 * finding them all, then unroll and fill the cache at the same time
2311 static GstFlowReturn
2312 gst_ogg_demux_bisect_forward_serialno (GstOggDemux * ogg,
2313 gint64 begin, gint64 searched, gint64 end, GstOggChain * chain, glong m)
2315 gint64 endsearched = end;
2320 GstOggChain *nextchain;
2322 GST_LOG_OBJECT (ogg,
2323 "bisect begin: %" G_GINT64_FORMAT ", searched: %" G_GINT64_FORMAT
2324 ", end %" G_GINT64_FORMAT ", chain: %p", begin, searched, end, chain);
2326 /* the below guards against garbage seperating the last and
2327 * first pages of two links. */
2328 while (searched < endsearched) {
2331 if (endsearched - searched < CHUNKSIZE) {
2334 bisect = (searched + endsearched) / 2;
2337 gst_ogg_demux_seek (ogg, bisect);
2338 ret = gst_ogg_demux_get_next_page (ogg, &og, -1, &offset);
2340 if (ret == GST_FLOW_UNEXPECTED) {
2341 endsearched = bisect;
2342 } else if (ret == GST_FLOW_OK) {
2343 glong serial = ogg_page_serialno (&og);
2345 if (!gst_ogg_chain_has_stream (chain, serial)) {
2346 endsearched = bisect;
2349 searched = offset + og.header_len + og.body_len;
2355 GST_LOG_OBJECT (ogg, "current chain ends at %" G_GINT64_FORMAT, searched);
2357 chain->end_offset = searched;
2358 ret = gst_ogg_demux_read_end_chain (ogg, chain);
2359 if (ret != GST_FLOW_OK)
2362 GST_LOG_OBJECT (ogg, "found begin at %" G_GINT64_FORMAT, next);
2364 gst_ogg_demux_seek (ogg, next);
2365 ret = gst_ogg_demux_read_chain (ogg, &nextchain);
2366 if (ret == GST_FLOW_UNEXPECTED) {
2369 GST_LOG_OBJECT (ogg, "no next chain");
2370 } else if (ret != GST_FLOW_OK)
2373 if (searched < end && nextchain != NULL) {
2374 ret = gst_ogg_demux_bisect_forward_serialno (ogg, next, ogg->offset,
2375 end, nextchain, m + 1);
2376 if (ret != GST_FLOW_OK)
2379 GST_LOG_OBJECT (ogg, "adding chain %p", chain);
2381 g_array_insert_val (ogg->chains, 0, chain);
2387 /* read a chain from the ogg file. This code will
2388 * read all BOS pages and will create and return a GstOggChain
2389 * structure with the results.
2391 * This function will also read N pages from each stream in the
2392 * chain and submit them to the decoders. When the decoder has
2393 * decoded the first buffer, we know the timestamp of the first
2394 * page in the chain.
2396 static GstFlowReturn
2397 gst_ogg_demux_read_chain (GstOggDemux * ogg, GstOggChain ** res_chain)
2400 GstOggChain *chain = NULL;
2401 gint64 offset = ogg->offset;
2406 GST_LOG_OBJECT (ogg, "reading chain at %" G_GINT64_FORMAT, offset);
2408 /* first read the BOS pages, do typefind on them, create
2409 * the decoders, send data to the decoders. */
2414 ret = gst_ogg_demux_get_next_page (ogg, &op, -1, NULL);
2415 if (ret != GST_FLOW_OK) {
2416 GST_WARNING_OBJECT (ogg, "problem reading BOS page: ret=%d", ret);
2419 if (!ogg_page_bos (&op)) {
2420 GST_WARNING_OBJECT (ogg, "page is not BOS page");
2421 /* if we did not find a chain yet, assume this is a bogus stream and
2424 ret = GST_FLOW_UNEXPECTED;
2428 if (chain == NULL) {
2429 chain = gst_ogg_chain_new (ogg);
2430 chain->offset = offset;
2433 serial = ogg_page_serialno (&op);
2434 if (gst_ogg_chain_get_stream (chain, serial) != NULL) {
2435 GST_WARNING_OBJECT (ogg, "found serial %08lx BOS page twice, ignoring",
2440 pad = gst_ogg_chain_new_stream (chain, serial);
2441 gst_ogg_pad_submit_page (pad, &op);
2444 if (ret != GST_FLOW_OK || chain == NULL) {
2445 if (ret == GST_FLOW_OK) {
2446 GST_WARNING_OBJECT (ogg, "no chain was found");
2447 ret = GST_FLOW_ERROR;
2448 } else if (ret != GST_FLOW_UNEXPECTED) {
2449 GST_WARNING_OBJECT (ogg, "failed to read chain");
2451 GST_DEBUG_OBJECT (ogg, "done reading chains");
2454 gst_ogg_chain_free (chain);
2461 chain->have_bos = TRUE;
2462 GST_LOG_OBJECT (ogg, "read bos pages, init decoder now");
2464 /* now read pages until we receive a buffer from each of the
2465 * stream decoders, this will tell us the timestamp of the
2466 * first packet in the chain then */
2468 /* save the offset to the first non bos page in the chain: if searching for
2469 * pad->first_time we read past the end of the chain, we'll seek back to this
2472 offset = ogg->offset;
2477 gboolean known_serial = FALSE;
2480 serial = ogg_page_serialno (&op);
2482 for (i = 0; i < chain->streams->len; i++) {
2483 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2485 GST_LOG_OBJECT (ogg, "serial %08x time %" GST_TIME_FORMAT, pad->serialno,
2486 GST_TIME_ARGS (pad->start_time));
2488 if (pad->serialno == serial) {
2489 known_serial = TRUE;
2491 /* submit the page now, this will fill in the start_time when the
2492 * internal decoder finds it */
2493 gst_ogg_pad_submit_page (pad, &op);
2495 if (!pad->is_skeleton && pad->start_time == -1 && ogg_page_eos (&op)) {
2496 /* got EOS on a pad before we could find its start_time.
2497 * We have no chance of finding a start_time for every pad so
2498 * stop searching for the other start_time(s).
2504 /* the timestamp will be filled in when we submit the pages */
2505 if (!pad->is_skeleton)
2506 done &= (pad->start_time != GST_CLOCK_TIME_NONE);
2508 GST_LOG_OBJECT (ogg, "done %08x now %d", pad->serialno, done);
2511 /* we read a page not belonging to the current chain: seek back to the
2512 * beginning of the chain
2514 if (!known_serial) {
2515 GST_LOG_OBJECT (ogg, "unknown serial %08lx", serial);
2516 gst_ogg_demux_seek (ogg, offset);
2521 ret = gst_ogg_demux_get_next_page (ogg, &op, -1, NULL);
2522 if (ret != GST_FLOW_OK)
2526 GST_LOG_OBJECT (ogg, "done reading chain");
2527 /* now we can fill in the missing info using queries */
2528 for (i = 0; i < chain->streams->len; i++) {
2529 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2532 if (pad->is_skeleton)
2535 GST_LOG_OBJECT (ogg, "convert first granule %" G_GUINT64_FORMAT " to time ",
2536 pad->first_granule);
2538 target = GST_FORMAT_TIME;
2539 if (!gst_ogg_pad_query_convert (pad,
2540 GST_FORMAT_DEFAULT, pad->first_granule, &target,
2541 (gint64 *) & pad->first_time)) {
2542 GST_WARNING_OBJECT (ogg, "could not convert granulepos to time");
2544 GST_LOG_OBJECT (ogg, "converted to first time %" GST_TIME_FORMAT,
2545 GST_TIME_ARGS (pad->first_time));
2548 pad->mode = GST_OGG_PAD_MODE_STREAMING;
2557 /* read the last pages from the ogg stream to get the final
2560 static GstFlowReturn
2561 gst_ogg_demux_read_end_chain (GstOggDemux * ogg, GstOggChain * chain)
2563 gint64 begin = chain->end_offset;
2565 gint64 last_granule = -1;
2566 GstOggPad *last_pad = NULL;
2569 gboolean done = FALSE;
2578 gst_ogg_demux_seek (ogg, begin);
2580 /* now continue reading until we run out of data, if we find a page
2581 * start, we save it. It might not be the final page as there could be
2582 * another page after this one. */
2583 while (ogg->offset < end) {
2584 ret = gst_ogg_demux_get_next_page (ogg, &og, end - ogg->offset, NULL);
2586 if (ret == GST_FLOW_LIMIT)
2588 if (ret != GST_FLOW_OK)
2591 for (i = 0; i < chain->streams->len; i++) {
2592 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2594 if (pad->is_skeleton)
2597 if (pad->serialno == ogg_page_serialno (&og)) {
2598 gint64 granulepos = ogg_page_granulepos (&og);
2600 if (last_granule < granulepos) {
2601 last_granule = granulepos;
2611 target = GST_FORMAT_TIME;
2612 if (last_granule == -1 || !gst_ogg_pad_query_convert (last_pad,
2613 GST_FORMAT_DEFAULT, last_granule, &target,
2614 (gint64 *) & chain->segment_stop)) {
2615 GST_WARNING_OBJECT (ogg, "could not convert granulepos to time");
2616 chain->segment_stop = GST_CLOCK_TIME_NONE;
2622 /* find a pad with a given serial number
2625 gst_ogg_demux_find_pad (GstOggDemux * ogg, int serialno)
2630 /* first look in building chain if any */
2631 if (ogg->building_chain) {
2632 pad = gst_ogg_chain_get_stream (ogg->building_chain, serialno);
2637 /* then look in current chain if any */
2638 if (ogg->current_chain) {
2639 pad = gst_ogg_chain_get_stream (ogg->current_chain, serialno);
2644 for (i = 0; i < ogg->chains->len; i++) {
2645 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2647 pad = gst_ogg_chain_get_stream (chain, serialno);
2654 /* find a chain with a given serial number
2656 static GstOggChain *
2657 gst_ogg_demux_find_chain (GstOggDemux * ogg, int serialno)
2661 pad = gst_ogg_demux_find_pad (ogg, serialno);
2668 /* returns TRUE if all streams have valid start time */
2670 gst_ogg_demux_collect_chain_info (GstOggDemux * ogg, GstOggChain * chain)
2673 gboolean res = TRUE;
2675 chain->total_time = GST_CLOCK_TIME_NONE;
2676 chain->segment_start = G_MAXUINT64;
2678 GST_DEBUG_OBJECT (ogg, "trying to collect chain info");
2680 for (i = 0; i < chain->streams->len; i++) {
2681 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2683 if (pad->is_skeleton)
2686 /* can do this if the pad start time is not defined */
2687 if (pad->start_time == GST_CLOCK_TIME_NONE)
2690 chain->segment_start = MIN (chain->segment_start, pad->start_time);
2693 if (chain->segment_stop != GST_CLOCK_TIME_NONE
2694 && chain->segment_start != G_MAXUINT64)
2695 chain->total_time = chain->segment_stop - chain->segment_start;
2697 GST_DEBUG_OBJECT (ogg, "return %d", res);
2703 gst_ogg_demux_collect_info (GstOggDemux * ogg)
2707 /* collect all info */
2708 ogg->total_time = 0;
2710 for (i = 0; i < ogg->chains->len; i++) {
2711 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
2713 chain->begin_time = ogg->total_time;
2715 gst_ogg_demux_collect_chain_info (ogg, chain);
2717 ogg->total_time += chain->total_time;
2719 gst_segment_set_duration (&ogg->segment, GST_FORMAT_TIME, ogg->total_time);
2722 /* find all the chains in the ogg file, this reads the first and
2723 * last page of the ogg stream, if they match then the ogg file has
2724 * just one chain, else we do a binary search for all chains.
2726 static GstFlowReturn
2727 gst_ogg_demux_find_chains (GstOggDemux * ogg)
2737 /* get peer to figure out length */
2738 if ((peer = gst_pad_get_peer (ogg->sinkpad)) == NULL)
2741 /* find length to read last page, we store this for later use. */
2742 format = GST_FORMAT_BYTES;
2743 res = gst_pad_query_duration (peer, &format, &ogg->length);
2744 gst_object_unref (peer);
2745 if (!res || ogg->length <= 0)
2748 GST_DEBUG_OBJECT (ogg, "file length %" G_GINT64_FORMAT, ogg->length);
2750 /* read chain from offset 0, this is the first chain of the
2752 gst_ogg_demux_seek (ogg, 0);
2753 ret = gst_ogg_demux_read_chain (ogg, &chain);
2754 if (ret != GST_FLOW_OK)
2755 goto no_first_chain;
2757 /* read page from end offset, we use this page to check if its serial
2758 * number is contained in the first chain. If this is the case then
2759 * this ogg is not a chained ogg and we can skip the scanning. */
2760 gst_ogg_demux_seek (ogg, ogg->length);
2761 ret = gst_ogg_demux_get_prev_page (ogg, &og, NULL);
2762 if (ret != GST_FLOW_OK)
2765 serialno = ogg_page_serialno (&og);
2767 if (!gst_ogg_chain_has_stream (chain, serialno)) {
2768 /* the last page is not in the first stream, this means we should
2769 * find all the chains in this chained ogg. */
2771 gst_ogg_demux_bisect_forward_serialno (ogg, 0, 0, ogg->length, chain,
2774 /* we still call this function here but with an empty range so that
2775 * we can reuse the setup code in this routine. */
2777 gst_ogg_demux_bisect_forward_serialno (ogg, 0, ogg->length, ogg->length,
2780 if (ret != GST_FLOW_OK)
2783 /* all fine, collect and print */
2784 gst_ogg_demux_collect_info (ogg);
2786 /* dump our chains and streams */
2787 gst_ogg_print (ogg);
2792 /*** error cases ***/
2795 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("we don't have a peer"));
2796 return GST_FLOW_NOT_LINKED;
2800 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("can't get file length"));
2801 return GST_FLOW_NOT_SUPPORTED;
2805 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL), ("can't get first chain"));
2806 return GST_FLOW_ERROR;
2810 GST_DEBUG_OBJECT (ogg, "can't get last page");
2812 gst_ogg_chain_free (chain);
2817 static GstFlowReturn
2818 gst_ogg_demux_handle_page (GstOggDemux * ogg, ogg_page * page)
2823 GstFlowReturn result = GST_FLOW_OK;
2825 serialno = ogg_page_serialno (page);
2826 granule = ogg_page_granulepos (page);
2828 GST_LOG_OBJECT (ogg,
2829 "processing ogg page (serial %08x, pageno %ld, granulepos %"
2830 G_GINT64_FORMAT ", bos %d)",
2831 serialno, ogg_page_pageno (page), granule, ogg_page_bos (page));
2833 if (ogg_page_bos (page)) {
2837 /* see if we know about the chain already */
2838 chain = gst_ogg_demux_find_chain (ogg, serialno);
2843 if (chain->segment_start != GST_CLOCK_TIME_NONE)
2844 start = chain->segment_start;
2846 /* create the newsegment event we are going to send out */
2847 event = gst_event_new_new_segment (FALSE, ogg->segment.rate,
2848 GST_FORMAT_TIME, start, chain->segment_stop, chain->begin_time);
2849 gst_event_set_seqnum (event, ogg->seqnum);
2851 GST_DEBUG_OBJECT (ogg,
2852 "segment: start %" GST_TIME_FORMAT ", stop %" GST_TIME_FORMAT
2853 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
2854 GST_TIME_ARGS (chain->segment_stop),
2855 GST_TIME_ARGS (chain->begin_time));
2857 /* activate it as it means we have a non-header, this will also deactivate
2858 * the currently running chain. */
2859 gst_ogg_demux_activate_chain (ogg, chain, event);
2860 pad = gst_ogg_demux_find_pad (ogg, serialno);
2862 GstClockTime chain_time;
2863 GstOggChain *current_chain;
2864 gint64 current_time;
2866 /* this can only happen in non-seekabe mode */
2870 current_chain = ogg->current_chain;
2871 current_time = ogg->segment.last_stop;
2873 /* time of new chain is current time */
2874 chain_time = current_time;
2876 if (ogg->building_chain == NULL) {
2877 GstOggChain *newchain;
2879 newchain = gst_ogg_chain_new (ogg);
2880 newchain->offset = 0;
2881 /* set new chain begin time aligned with end time of old chain */
2882 newchain->begin_time = chain_time;
2883 GST_DEBUG_OBJECT (ogg, "new chain, begin time %" GST_TIME_FORMAT,
2884 GST_TIME_ARGS (chain_time));
2886 /* and this is the one we are building now */
2887 ogg->building_chain = newchain;
2889 pad = gst_ogg_chain_new_stream (ogg->building_chain, serialno);
2892 pad = gst_ogg_demux_find_pad (ogg, serialno);
2895 result = gst_ogg_pad_submit_page (pad, page);
2897 /* no pad. This means an ogg page without bos has been seen for this
2898 * serialno. we just ignore it but post a warning... */
2899 GST_ELEMENT_WARNING (ogg, STREAM, DECODE,
2900 (NULL), ("unknown ogg pad for serial %08x detected", serialno));
2908 GST_ELEMENT_ERROR (ogg, STREAM, DECODE,
2909 (NULL), ("unknown ogg chain for serial %08x detected", serialno));
2910 return GST_FLOW_ERROR;
2914 /* streaming mode, receive a buffer, parse it, create pads for
2915 * the serialno, submit pages and packets to the oggpads
2917 static GstFlowReturn
2918 gst_ogg_demux_chain (GstPad * pad, GstBuffer * buffer)
2922 GstFlowReturn result = GST_FLOW_OK;
2924 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (pad));
2926 GST_DEBUG_OBJECT (ogg, "chain");
2927 result = gst_ogg_demux_submit_buffer (ogg, buffer);
2929 while (result == GST_FLOW_OK) {
2932 ret = ogg_sync_pageout (&ogg->sync, &page);
2934 /* need more data */
2937 /* discontinuity in the pages */
2938 GST_DEBUG_OBJECT (ogg, "discont in page found, continuing");
2940 result = gst_ogg_demux_handle_page (ogg, &page);
2947 gst_ogg_demux_send_event (GstOggDemux * ogg, GstEvent * event)
2949 GstOggChain *chain = ogg->current_chain;
2954 for (i = 0; i < chain->streams->len; i++) {
2955 GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
2957 gst_event_ref (event);
2958 GST_DEBUG_OBJECT (pad, "Pushing event %" GST_PTR_FORMAT, event);
2959 gst_pad_push_event (GST_PAD (pad), event);
2962 gst_event_unref (event);
2965 static GstFlowReturn
2966 gst_ogg_demux_combine_flows (GstOggDemux * ogg, GstOggPad * pad,
2971 /* store the value */
2972 pad->last_ret = ret;
2974 /* any other error that is not-linked can be returned right
2976 if (ret != GST_FLOW_NOT_LINKED)
2979 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
2980 chain = ogg->current_chain;
2984 for (i = 0; i < chain->streams->len; i++) {
2985 GstOggPad *opad = g_array_index (chain->streams, GstOggPad *, i);
2987 ret = opad->last_ret;
2988 /* some other return value (must be SUCCESS but we can return
2989 * other values as well) */
2990 if (ret != GST_FLOW_NOT_LINKED)
2993 /* if we get here, all other pads were unlinked and we return
2994 * NOT_LINKED then */
3000 static GstFlowReturn
3001 gst_ogg_demux_loop_forward (GstOggDemux * ogg)
3006 if (ogg->offset == ogg->length) {
3007 GST_LOG_OBJECT (ogg, "no more data to pull %" G_GINT64_FORMAT
3008 " == %" G_GINT64_FORMAT, ogg->offset, ogg->length);
3009 ret = GST_FLOW_UNEXPECTED;
3013 GST_LOG_OBJECT (ogg, "pull data %" G_GINT64_FORMAT, ogg->offset);
3014 ret = gst_pad_pull_range (ogg->sinkpad, ogg->offset, CHUNKSIZE, &buffer);
3015 if (ret != GST_FLOW_OK) {
3016 GST_LOG_OBJECT (ogg, "Failed pull_range");
3020 ogg->offset += GST_BUFFER_SIZE (buffer);
3022 if (G_UNLIKELY (ogg->newsegment)) {
3023 gst_ogg_demux_send_event (ogg, ogg->newsegment);
3024 ogg->newsegment = NULL;
3027 ret = gst_ogg_demux_chain (ogg->sinkpad, buffer);
3028 if (ret != GST_FLOW_OK) {
3029 GST_LOG_OBJECT (ogg, "Failed demux_chain");
3033 /* check for the end of the segment */
3034 if (ogg->segment.stop != -1 && ogg->segment.last_stop != -1) {
3035 if (ogg->segment.last_stop > ogg->segment.stop) {
3036 ret = GST_FLOW_UNEXPECTED;
3046 * We read the pages backwards and send the packets forwards. The first packet
3047 * in the page will be pushed with the DISCONT flag set.
3049 * Special care has to be taken for continued pages, which we can only decode
3050 * when we have the previous page(s).
3052 static GstFlowReturn
3053 gst_ogg_demux_loop_reverse (GstOggDemux * ogg)
3059 if (ogg->offset == 0) {
3060 GST_LOG_OBJECT (ogg, "no more data to pull %" G_GINT64_FORMAT
3061 " == 0", ogg->offset);
3062 ret = GST_FLOW_UNEXPECTED;
3066 GST_LOG_OBJECT (ogg, "read page from %" G_GINT64_FORMAT, ogg->offset);
3067 ret = gst_ogg_demux_get_prev_page (ogg, &page, &offset);
3068 if (ret != GST_FLOW_OK)
3071 ogg->offset = offset;
3073 if (G_UNLIKELY (ogg->newsegment)) {
3074 gst_ogg_demux_send_event (ogg, ogg->newsegment);
3075 ogg->newsegment = NULL;
3078 ret = gst_ogg_demux_handle_page (ogg, &page);
3079 if (ret != GST_FLOW_OK)
3082 /* check for the end of the segment */
3083 if (ogg->segment.start != -1 && ogg->segment.last_stop != -1) {
3084 if (ogg->segment.last_stop <= ogg->segment.start) {
3085 ret = GST_FLOW_UNEXPECTED;
3093 /* random access code
3095 * - first find all the chains and streams by scanning the file.
3096 * - then get and chain buffers, just like the streaming case.
3097 * - when seeking, we can use the chain info to perform the seek.
3100 gst_ogg_demux_loop (GstOggPad * pad)
3106 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (pad));
3108 if (ogg->need_chains) {
3111 /* this is the only place where we write chains and thus need to lock. */
3112 GST_CHAIN_LOCK (ogg);
3113 ret = gst_ogg_demux_find_chains (ogg);
3114 GST_CHAIN_UNLOCK (ogg);
3115 if (ret != GST_FLOW_OK)
3116 goto chain_read_failed;
3118 ogg->need_chains = FALSE;
3120 GST_OBJECT_LOCK (ogg);
3121 ogg->running = TRUE;
3124 GST_OBJECT_UNLOCK (ogg);
3126 /* and seek to configured positions without FLUSH */
3127 res = gst_ogg_demux_perform_seek (ogg, event);
3129 gst_event_unref (event);
3135 if (ogg->segment.rate >= 0.0)
3136 ret = gst_ogg_demux_loop_forward (ogg);
3138 ret = gst_ogg_demux_loop_reverse (ogg);
3140 if (ret != GST_FLOW_OK)
3148 /* error was posted */
3153 GST_ELEMENT_ERROR (ogg, STREAM, DEMUX, (NULL),
3154 ("failed to start demuxing ogg"));
3155 ret = GST_FLOW_ERROR;
3160 const gchar *reason = gst_flow_get_name (ret);
3161 GstEvent *event = NULL;
3163 GST_LOG_OBJECT (ogg, "pausing task, reason %s", reason);
3164 ogg->segment_running = FALSE;
3165 gst_pad_pause_task (ogg->sinkpad);
3167 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
3168 if (ret == GST_FLOW_UNEXPECTED) {
3169 /* perform EOS logic */
3170 if (ogg->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3172 GstMessage *message;
3174 /* for segment playback we need to post when (in stream time)
3175 * we stopped, this is either stop (when set) or the duration. */
3176 if ((stop = ogg->segment.stop) == -1)
3177 stop = ogg->segment.duration;
3179 GST_LOG_OBJECT (ogg, "Sending segment done, at end of segment");
3181 gst_message_new_segment_done (GST_OBJECT (ogg), GST_FORMAT_TIME,
3183 gst_message_set_seqnum (message, ogg->seqnum);
3185 gst_element_post_message (GST_ELEMENT (ogg), message);
3187 /* normal playback, send EOS to all linked pads */
3188 GST_LOG_OBJECT (ogg, "Sending EOS, at end of stream");
3189 event = gst_event_new_eos ();
3192 GST_ELEMENT_ERROR (ogg, STREAM, FAILED,
3193 (_("Internal data stream error.")),
3194 ("stream stopped, reason %s", reason));
3195 event = gst_event_new_eos ();
3198 gst_event_set_seqnum (event, ogg->seqnum);
3199 gst_ogg_demux_send_event (ogg, event);
3207 gst_ogg_demux_clear_chains (GstOggDemux * ogg)
3211 gst_ogg_demux_deactivate_current_chain (ogg);
3213 GST_CHAIN_LOCK (ogg);
3214 for (i = 0; i < ogg->chains->len; i++) {
3215 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
3217 gst_ogg_chain_free (chain);
3219 ogg->chains = g_array_set_size (ogg->chains, 0);
3220 GST_CHAIN_UNLOCK (ogg);
3223 /* this function is called when the pad is activated and should start
3226 * We check if we can do random access to decide if we work push or
3230 gst_ogg_demux_sink_activate (GstPad * sinkpad)
3232 if (gst_pad_check_pull_range (sinkpad)) {
3233 GST_DEBUG_OBJECT (sinkpad, "activating pull");
3234 return gst_pad_activate_pull (sinkpad, TRUE);
3236 GST_DEBUG_OBJECT (sinkpad, "activating push");
3237 return gst_pad_activate_push (sinkpad, TRUE);
3241 /* this function gets called when we activate ourselves in push mode.
3242 * We cannot seek (ourselves) in the stream */
3244 gst_ogg_demux_sink_activate_push (GstPad * sinkpad, gboolean active)
3248 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (sinkpad));
3250 ogg->seekable = FALSE;
3255 /* this function gets called when we activate ourselves in pull mode.
3256 * We can perform random access to the resource and we start a task
3257 * to start reading */
3259 gst_ogg_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
3263 ogg = GST_OGG_DEMUX (GST_OBJECT_PARENT (sinkpad));
3266 ogg->need_chains = TRUE;
3267 ogg->seekable = TRUE;
3269 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_ogg_demux_loop,
3272 return gst_pad_stop_task (sinkpad);
3276 static GstStateChangeReturn
3277 gst_ogg_demux_change_state (GstElement * element, GstStateChange transition)
3280 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
3282 ogg = GST_OGG_DEMUX (element);
3284 switch (transition) {
3285 case GST_STATE_CHANGE_NULL_TO_READY:
3287 ogg->have_fishead = FALSE;
3288 ogg_sync_init (&ogg->sync);
3290 case GST_STATE_CHANGE_READY_TO_PAUSED:
3291 ogg_sync_reset (&ogg->sync);
3292 ogg->current_granule = -1;
3293 ogg->running = FALSE;
3294 ogg->segment_running = FALSE;
3295 gst_segment_init (&ogg->segment, GST_FORMAT_TIME);
3297 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3303 result = parent_class->change_state (element, transition);
3305 switch (transition) {
3306 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3308 case GST_STATE_CHANGE_PAUSED_TO_READY:
3309 gst_ogg_demux_clear_chains (ogg);
3310 GST_OBJECT_LOCK (ogg);
3311 ogg->running = FALSE;
3312 ogg->segment_running = FALSE;
3313 ogg->have_fishead = FALSE;
3314 GST_OBJECT_UNLOCK (ogg);
3316 case GST_STATE_CHANGE_READY_TO_NULL:
3317 ogg_sync_clear (&ogg->sync);
3326 gst_annodex_granule_to_time (gint64 granulepos, gint64 granulerate_n,
3327 gint64 granulerate_d, guint8 granuleshift)
3329 gint64 keyindex, keyoffset;
3333 if (granulepos == 0 || granulerate_n == 0 || granulerate_d == 0)
3336 if (granuleshift != 0) {
3337 keyindex = granulepos >> granuleshift;
3338 keyoffset = granulepos - (keyindex << granuleshift);
3339 granulepos = keyindex + keyoffset;
3342 /* GST_SECOND / (granulerate_n / granulerate_d) */
3343 granulerate = gst_util_uint64_scale (GST_SECOND,
3344 granulerate_d, granulerate_n);
3345 res = gst_util_uint64_scale (granulepos, granulerate, 1);
3350 gst_ogg_demux_plugin_init (GstPlugin * plugin)
3352 GST_DEBUG_CATEGORY_INIT (gst_ogg_demux_debug, "oggdemux", 0, "ogg demuxer");
3353 GST_DEBUG_CATEGORY_INIT (gst_ogg_demux_setup_debug, "oggdemux_setup", 0,
3354 "ogg demuxer setup stage when parsing pipeline");
3357 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
3359 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
3360 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
3363 return gst_element_register (plugin, "oggdemux", GST_RANK_PRIMARY,
3364 GST_TYPE_OGG_DEMUX);
3367 /* prints all info about the element */
3368 #undef GST_CAT_DEFAULT
3369 #define GST_CAT_DEFAULT gst_ogg_demux_setup_debug
3371 #ifdef GST_DISABLE_GST_DEBUG
3374 gst_ogg_print (GstOggDemux * ogg)
3379 #else /* !GST_DISABLE_GST_DEBUG */
3382 gst_ogg_print (GstOggDemux * ogg)
3386 GST_INFO_OBJECT (ogg, "%u chains", ogg->chains->len);
3387 GST_INFO_OBJECT (ogg, " total time: %" GST_TIME_FORMAT,
3388 GST_TIME_ARGS (ogg->total_time));
3390 for (i = 0; i < ogg->chains->len; i++) {
3391 GstOggChain *chain = g_array_index (ogg->chains, GstOggChain *, i);
3393 GST_INFO_OBJECT (ogg, " chain %d (%u streams):", i, chain->streams->len);
3394 GST_INFO_OBJECT (ogg, " offset: %" G_GINT64_FORMAT " - %" G_GINT64_FORMAT,
3395 chain->offset, chain->end_offset);
3396 GST_INFO_OBJECT (ogg, " begin time: %" GST_TIME_FORMAT,
3397 GST_TIME_ARGS (chain->begin_time));
3398 GST_INFO_OBJECT (ogg, " total time: %" GST_TIME_FORMAT,
3399 GST_TIME_ARGS (chain->total_time));
3400 GST_INFO_OBJECT (ogg, " segment start: %" GST_TIME_FORMAT,
3401 GST_TIME_ARGS (chain->segment_start));
3402 GST_INFO_OBJECT (ogg, " segment stop: %" GST_TIME_FORMAT,
3403 GST_TIME_ARGS (chain->segment_stop));
3405 for (j = 0; j < chain->streams->len; j++) {
3406 GstOggPad *stream = g_array_index (chain->streams, GstOggPad *, j);
3408 GST_INFO_OBJECT (ogg, " stream %08x:", stream->serialno);
3409 GST_INFO_OBJECT (ogg, " start time: %" GST_TIME_FORMAT,
3410 GST_TIME_ARGS (stream->start_time));
3411 GST_INFO_OBJECT (ogg, " first granulepos: %" G_GINT64_FORMAT,
3412 stream->first_granule);
3413 GST_INFO_OBJECT (ogg, " first time: %" GST_TIME_FORMAT,
3414 GST_TIME_ARGS (stream->first_time));
3418 #endif /* GST_DISABLE_GST_DEBUG */