2 * Copyright (C) 2006 Wim Taymans <wim@fluendo.com>
4 * gstoggaviparse.c: ogg avi 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.
23 * Ogg in AVI is mostly done for vorbis audio. In the codec_data we receive the
24 * first 3 packets of the raw vorbis data. On the sinkpad we receive full-blown Ogg
26 * Before extracting the packets out of the ogg pages, we push the raw vorbis
27 * header packets to the decoder.
28 * We don't use the incomming timestamps but use the ganulepos on the ogg pages
30 * This parser only does ogg/vorbis for now.
42 static const GstElementDetails gst_ogg_avi_parse_details =
43 GST_ELEMENT_DETAILS ("Ogg AVI parser",
45 "parse an ogg avi stream into pages (info about ogg: http://xiph.org)",
46 "Wim Taymans <wim@fluendo.com>");
48 GST_DEBUG_CATEGORY_STATIC (gst_ogg_avi_parse_debug);
49 #define GST_CAT_DEFAULT gst_ogg_avi_parse_debug
51 #define GST_TYPE_OGG_AVI_PARSE (gst_ogg_avi_parse_get_type())
52 #define GST_OGG_AVI_PARSE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_OGG_AVI_PARSE, GstOggAviParse))
53 #define GST_OGG_AVI_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_OGG_AVI_PARSE, GstOggAviParse))
54 #define GST_IS_OGG_AVI_PARSE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_OGG_AVI_PARSE))
55 #define GST_IS_OGG_AVI_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_OGG_AVI_PARSE))
57 static GType gst_ogg_avi_parse_get_type (void);
59 typedef struct _GstOggAviParse GstOggAviParse;
60 typedef struct _GstOggAviParseClass GstOggAviParseClass;
62 struct _GstOggAviParse
73 ogg_stream_state stream;
76 struct _GstOggAviParseClass
78 GstElementClass parent_class;
81 static void gst_ogg_avi_parse_base_init (gpointer g_class);
82 static void gst_ogg_avi_parse_class_init (GstOggAviParseClass * klass);
83 static void gst_ogg_avi_parse_init (GstOggAviParse * ogg);
84 static GstElementClass *parent_class = NULL;
87 gst_ogg_avi_parse_get_type (void)
89 static GType ogg_avi_parse_type = 0;
91 if (!ogg_avi_parse_type) {
92 static const GTypeInfo ogg_avi_parse_info = {
93 sizeof (GstOggAviParseClass),
94 gst_ogg_avi_parse_base_init,
96 (GClassInitFunc) gst_ogg_avi_parse_class_init,
99 sizeof (GstOggAviParse),
101 (GInstanceInitFunc) gst_ogg_avi_parse_init,
105 g_type_register_static (GST_TYPE_ELEMENT, "GstOggAviParse",
106 &ogg_avi_parse_info, 0);
108 return ogg_avi_parse_type;
116 static GstStaticPadTemplate ogg_avi_parse_src_template_factory =
117 GST_STATIC_PAD_TEMPLATE ("src",
120 GST_STATIC_CAPS ("audio/x-vorbis")
123 static GstStaticPadTemplate ogg_avi_parse_sink_template_factory =
124 GST_STATIC_PAD_TEMPLATE ("sink",
127 GST_STATIC_CAPS ("application/x-ogg-avi")
130 static void gst_ogg_avi_parse_finalize (GObject * object);
131 static GstStateChangeReturn gst_ogg_avi_parse_change_state (GstElement *
132 element, GstStateChange transition);
133 static gboolean gst_ogg_avi_parse_event (GstPad * pad, GstEvent * event);
134 static GstFlowReturn gst_ogg_avi_parse_chain (GstPad * pad, GstBuffer * buffer);
135 static gboolean gst_ogg_avi_parse_setcaps (GstPad * pad, GstCaps * caps);
138 gst_ogg_avi_parse_base_init (gpointer g_class)
140 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
142 gst_element_class_set_details (element_class, &gst_ogg_avi_parse_details);
144 gst_element_class_add_pad_template (element_class,
145 gst_static_pad_template_get (&ogg_avi_parse_sink_template_factory));
146 gst_element_class_add_pad_template (element_class,
147 gst_static_pad_template_get (&ogg_avi_parse_src_template_factory));
151 gst_ogg_avi_parse_class_init (GstOggAviParseClass * klass)
153 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
154 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
156 parent_class = g_type_class_peek_parent (klass);
158 gstelement_class->change_state = gst_ogg_avi_parse_change_state;
160 gobject_class->finalize = gst_ogg_avi_parse_finalize;
164 gst_ogg_avi_parse_init (GstOggAviParse * ogg)
166 /* create the sink and source pads */
168 gst_pad_new_from_static_template (&ogg_avi_parse_sink_template_factory,
170 gst_pad_set_setcaps_function (ogg->sinkpad, gst_ogg_avi_parse_setcaps);
171 gst_pad_set_event_function (ogg->sinkpad, gst_ogg_avi_parse_event);
172 gst_pad_set_chain_function (ogg->sinkpad, gst_ogg_avi_parse_chain);
173 gst_element_add_pad (GST_ELEMENT (ogg), ogg->sinkpad);
176 gst_pad_new_from_static_template (&ogg_avi_parse_src_template_factory,
178 gst_pad_use_fixed_caps (ogg->srcpad);
179 gst_element_add_pad (GST_ELEMENT (ogg), ogg->srcpad);
183 gst_ogg_avi_parse_finalize (GObject * object)
185 GstOggAviParse *ogg = GST_OGG_AVI_PARSE (object);
187 GST_LOG_OBJECT (ogg, "Disposing of object %p", ogg);
189 ogg_sync_clear (&ogg->sync);
190 ogg_stream_clear (&ogg->stream);
192 G_OBJECT_CLASS (parent_class)->finalize (object);
196 gst_ogg_avi_parse_setcaps (GstPad * pad, GstCaps * caps)
199 GstStructure *structure;
200 const GValue *codec_data;
208 ogg = GST_OGG_AVI_PARSE (GST_OBJECT_PARENT (pad));
210 structure = gst_caps_get_structure (caps, 0);
212 /* take codec data */
213 codec_data = gst_structure_get_value (structure, "codec_data");
214 if (codec_data == NULL)
217 /* only buffers are valid */
218 if (G_VALUE_TYPE (codec_data) != GST_TYPE_BUFFER)
221 /* Now parse the data */
222 buffer = gst_value_get_buffer (codec_data);
224 /* first 22 bytes are bits_per_sample, channel_mask, GUID
225 * Then we get 3 LE guint32 with the 3 header sizes
226 * then we get the bytes of the 3 headers. */
227 data = GST_BUFFER_DATA (buffer);
228 size = GST_BUFFER_SIZE (buffer);
230 GST_LOG_OBJECT (ogg, "configuring codec_data of size %u", size);
236 /* we need at least 12 bytes for the packet sizes of the 3 headers */
238 goto buffer_too_small;
240 /* read sizes of the 3 headers */
241 sizes[0] = GST_READ_UINT32_LE (data);
242 sizes[1] = GST_READ_UINT32_LE (data + 4);
243 sizes[2] = GST_READ_UINT32_LE (data + 8);
245 GST_DEBUG_OBJECT (ogg, "header sizes: %u %u %u", sizes[0], sizes[1],
250 /* and we need at least enough data for all the headers */
251 if (size < sizes[0] + sizes[1] + sizes[2])
252 goto buffer_too_small;
255 outcaps = gst_caps_new_simple ("audio/x-vorbis", NULL);
256 gst_pad_set_caps (ogg->srcpad, outcaps);
258 /* copy header data */
260 for (i = 0; i < 3; i++) {
263 /* now output the raw vorbis header packets */
264 out = gst_buffer_create_sub (buffer, offs, sizes[i]);
265 gst_buffer_set_caps (out, outcaps);
266 gst_pad_push (ogg->srcpad, out);
270 gst_caps_unref (outcaps);
277 GST_DEBUG_OBJECT (ogg, "no codec_data found in caps");
282 GST_DEBUG_OBJECT (ogg, "codec_data is not a buffer");
287 GST_DEBUG_OBJECT (ogg, "codec_data is too small");
293 gst_ogg_avi_parse_event (GstPad * pad, GstEvent * event)
298 ogg = GST_OGG_AVI_PARSE (GST_OBJECT_PARENT (pad));
300 switch (GST_EVENT_TYPE (event)) {
301 case GST_EVENT_FLUSH_START:
302 ret = gst_pad_push_event (ogg->srcpad, event);
304 case GST_EVENT_FLUSH_STOP:
305 ogg_sync_reset (&ogg->sync);
306 ogg_stream_reset (&ogg->stream);
308 ret = gst_pad_push_event (ogg->srcpad, event);
311 ret = gst_pad_push_event (ogg->srcpad, event);
318 gst_ogg_avi_parse_push_packet (GstOggAviParse * ogg, ogg_packet * packet)
321 GstFlowReturn result;
323 /* allocate space for header and body */
324 buffer = gst_buffer_new_and_alloc (packet->bytes);
325 memcpy (GST_BUFFER_DATA (buffer), packet->packet, packet->bytes);
327 GST_LOG_OBJECT (ogg, "created buffer %p from page", buffer);
329 GST_BUFFER_OFFSET_END (buffer) = packet->granulepos;
332 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
333 ogg->discont = FALSE;
336 result = gst_pad_push (ogg->srcpad, buffer);
342 gst_ogg_avi_parse_chain (GstPad * pad, GstBuffer * buffer)
344 GstFlowReturn result = GST_FLOW_OK;
351 ogg = GST_OGG_AVI_PARSE (GST_OBJECT_PARENT (pad));
353 data = GST_BUFFER_DATA (buffer);
354 size = GST_BUFFER_SIZE (buffer);
356 GST_LOG_OBJECT (ogg, "Chain function received buffer of size %d", size);
358 if (GST_BUFFER_IS_DISCONT (buffer)) {
359 ogg_sync_reset (&ogg->sync);
363 /* write data to sync layer */
364 oggbuf = ogg_sync_buffer (&ogg->sync, size);
365 memcpy (oggbuf, data, size);
366 ogg_sync_wrote (&ogg->sync, size);
367 gst_buffer_unref (buffer);
369 /* try to get as many packets out of the stream as possible */
373 /* try to swap out a page */
374 ret = ogg_sync_pageout (&ogg->sync, &page);
376 GST_DEBUG_OBJECT (ogg, "need more data");
378 } else if (ret == -1) {
379 GST_DEBUG_OBJECT (ogg, "discont in pages");
382 /* new unknown stream, init the ogg stream with the serial number of the
384 if (ogg->serial == -1) {
385 ogg->serial = ogg_page_serialno (&page);
386 ogg_stream_init (&ogg->stream, ogg->serial);
390 if (ogg_stream_pagein (&ogg->stream, &page) != 0) {
391 GST_WARNING_OBJECT (ogg, "ogg stream choked on page resetting stream");
392 ogg_sync_reset (&ogg->sync);
397 /* try to get as many packets as possible out of the page */
401 ret = ogg_stream_packetout (&ogg->stream, &packet);
402 GST_LOG_OBJECT (ogg, "packetout gave %d", ret);
407 /* out of sync, We mark a DISCONT. */
411 result = gst_ogg_avi_parse_push_packet (ogg, &packet);
412 if (GST_FLOW_IS_FATAL (result))
416 GST_WARNING_OBJECT (ogg,
417 "invalid return value %d for ogg_stream_packetout, resetting stream",
431 static GstStateChangeReturn
432 gst_ogg_avi_parse_change_state (GstElement * element, GstStateChange transition)
435 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
437 ogg = GST_OGG_AVI_PARSE (element);
439 switch (transition) {
440 case GST_STATE_CHANGE_NULL_TO_READY:
441 ogg_sync_init (&ogg->sync);
443 case GST_STATE_CHANGE_READY_TO_PAUSED:
444 ogg_sync_reset (&ogg->sync);
445 ogg_stream_reset (&ogg->stream);
449 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
455 result = parent_class->change_state (element, transition);
457 switch (transition) {
458 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
460 case GST_STATE_CHANGE_PAUSED_TO_READY:
462 case GST_STATE_CHANGE_READY_TO_NULL:
463 ogg_sync_clear (&ogg->sync);
472 gst_ogg_avi_parse_plugin_init (GstPlugin * plugin)
474 GST_DEBUG_CATEGORY_INIT (gst_ogg_avi_parse_debug, "oggaviparse", 0,
477 return gst_element_register (plugin, "oggaviparse", GST_RANK_PRIMARY,
478 GST_TYPE_OGG_AVI_PARSE);