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 GST_DEBUG_CATEGORY_STATIC (gst_ogg_avi_parse_debug);
43 #define GST_CAT_DEFAULT gst_ogg_avi_parse_debug
45 #define GST_TYPE_OGG_AVI_PARSE (gst_ogg_avi_parse_get_type())
46 #define GST_OGG_AVI_PARSE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_OGG_AVI_PARSE, GstOggAviParse))
47 #define GST_OGG_AVI_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_OGG_AVI_PARSE, GstOggAviParse))
48 #define GST_IS_OGG_AVI_PARSE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_OGG_AVI_PARSE))
49 #define GST_IS_OGG_AVI_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_OGG_AVI_PARSE))
51 static GType gst_ogg_avi_parse_get_type (void);
53 typedef struct _GstOggAviParse GstOggAviParse;
54 typedef struct _GstOggAviParseClass GstOggAviParseClass;
56 struct _GstOggAviParse
67 ogg_stream_state stream;
70 struct _GstOggAviParseClass
72 GstElementClass parent_class;
75 static void gst_ogg_avi_parse_base_init (gpointer g_class);
76 static void gst_ogg_avi_parse_class_init (GstOggAviParseClass * klass);
77 static void gst_ogg_avi_parse_init (GstOggAviParse * ogg);
78 static GstElementClass *parent_class = NULL;
81 gst_ogg_avi_parse_get_type (void)
83 static GType ogg_avi_parse_type = 0;
85 if (!ogg_avi_parse_type) {
86 static const GTypeInfo ogg_avi_parse_info = {
87 sizeof (GstOggAviParseClass),
88 gst_ogg_avi_parse_base_init,
90 (GClassInitFunc) gst_ogg_avi_parse_class_init,
93 sizeof (GstOggAviParse),
95 (GInstanceInitFunc) gst_ogg_avi_parse_init,
99 g_type_register_static (GST_TYPE_ELEMENT, "GstOggAviParse",
100 &ogg_avi_parse_info, 0);
102 return ogg_avi_parse_type;
110 static GstStaticPadTemplate ogg_avi_parse_src_template_factory =
111 GST_STATIC_PAD_TEMPLATE ("src",
114 GST_STATIC_CAPS ("audio/x-vorbis")
117 static GstStaticPadTemplate ogg_avi_parse_sink_template_factory =
118 GST_STATIC_PAD_TEMPLATE ("sink",
121 GST_STATIC_CAPS ("application/x-ogg-avi")
124 static void gst_ogg_avi_parse_finalize (GObject * object);
125 static GstStateChangeReturn gst_ogg_avi_parse_change_state (GstElement *
126 element, GstStateChange transition);
127 static gboolean gst_ogg_avi_parse_event (GstPad * pad, GstEvent * event);
128 static GstFlowReturn gst_ogg_avi_parse_chain (GstPad * pad, GstBuffer * buffer);
129 static gboolean gst_ogg_avi_parse_setcaps (GstPad * pad, GstCaps * caps);
132 gst_ogg_avi_parse_base_init (gpointer g_class)
134 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
136 gst_element_class_set_details_simple (element_class,
137 "Ogg AVI parser", "Codec/Parser",
138 "parse an ogg avi stream into pages (info about ogg: http://xiph.org)",
139 "Wim Taymans <wim@fluendo.com>");
141 gst_element_class_add_pad_template (element_class,
142 gst_static_pad_template_get (&ogg_avi_parse_sink_template_factory));
143 gst_element_class_add_pad_template (element_class,
144 gst_static_pad_template_get (&ogg_avi_parse_src_template_factory));
148 gst_ogg_avi_parse_class_init (GstOggAviParseClass * klass)
150 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
151 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
153 parent_class = g_type_class_peek_parent (klass);
155 gstelement_class->change_state = gst_ogg_avi_parse_change_state;
157 gobject_class->finalize = gst_ogg_avi_parse_finalize;
161 gst_ogg_avi_parse_init (GstOggAviParse * ogg)
163 /* create the sink and source pads */
165 gst_pad_new_from_static_template (&ogg_avi_parse_sink_template_factory,
167 gst_pad_set_event_function (ogg->sinkpad, gst_ogg_avi_parse_event);
168 gst_pad_set_chain_function (ogg->sinkpad, gst_ogg_avi_parse_chain);
169 gst_element_add_pad (GST_ELEMENT (ogg), ogg->sinkpad);
172 gst_pad_new_from_static_template (&ogg_avi_parse_src_template_factory,
174 gst_pad_use_fixed_caps (ogg->srcpad);
175 gst_element_add_pad (GST_ELEMENT (ogg), ogg->srcpad);
179 gst_ogg_avi_parse_finalize (GObject * object)
181 GstOggAviParse *ogg = GST_OGG_AVI_PARSE (object);
183 GST_LOG_OBJECT (ogg, "Disposing of object %p", ogg);
185 ogg_sync_clear (&ogg->sync);
186 ogg_stream_clear (&ogg->stream);
188 G_OBJECT_CLASS (parent_class)->finalize (object);
192 gst_ogg_avi_parse_setcaps (GstPad * pad, GstCaps * caps)
195 GstStructure *structure;
196 const GValue *codec_data;
204 ogg = GST_OGG_AVI_PARSE (GST_OBJECT_PARENT (pad));
206 structure = gst_caps_get_structure (caps, 0);
208 /* take codec data */
209 codec_data = gst_structure_get_value (structure, "codec_data");
210 if (codec_data == NULL)
213 /* only buffers are valid */
214 if (G_VALUE_TYPE (codec_data) != GST_TYPE_BUFFER)
217 /* Now parse the data */
218 buffer = gst_value_get_buffer (codec_data);
220 /* first 22 bytes are bits_per_sample, channel_mask, GUID
221 * Then we get 3 LE guint32 with the 3 header sizes
222 * then we get the bytes of the 3 headers. */
223 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
228 GST_LOG_OBJECT (ogg, "configuring codec_data of size %u", left);
234 /* we need at least 12 bytes for the packet sizes of the 3 headers */
236 goto buffer_too_small;
238 /* read sizes of the 3 headers */
239 sizes[0] = GST_READ_UINT32_LE (ptr);
240 sizes[1] = GST_READ_UINT32_LE (ptr + 4);
241 sizes[2] = GST_READ_UINT32_LE (ptr + 8);
243 GST_DEBUG_OBJECT (ogg, "header sizes: %u %u %u", sizes[0], sizes[1],
248 /* and we need at least enough data for all the headers */
249 if (left < sizes[0] + sizes[1] + sizes[2])
250 goto buffer_too_small;
253 outcaps = gst_caps_new_simple ("audio/x-vorbis", NULL);
254 gst_pad_push_event (ogg->srcpad, gst_event_new_caps (outcaps));
255 gst_caps_unref (outcaps);
257 /* copy header data */
259 for (i = 0; i < 3; i++) {
262 /* now output the raw vorbis header packets */
263 out = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offs, sizes[i]);
264 gst_pad_push (ogg->srcpad, out);
268 gst_buffer_unmap (buffer, data, size);
275 GST_DEBUG_OBJECT (ogg, "no codec_data found in caps");
280 GST_DEBUG_OBJECT (ogg, "codec_data is not a buffer");
285 GST_DEBUG_OBJECT (ogg, "codec_data is too small");
286 gst_buffer_unmap (buffer, data, size);
292 gst_ogg_avi_parse_event (GstPad * pad, GstEvent * event)
297 ogg = GST_OGG_AVI_PARSE (GST_OBJECT_PARENT (pad));
299 switch (GST_EVENT_TYPE (event)) {
304 gst_event_parse_caps (event, &caps);
305 ret = gst_ogg_avi_parse_setcaps (pad, caps);
306 gst_event_unref (event);
309 case GST_EVENT_FLUSH_START:
310 ret = gst_pad_push_event (ogg->srcpad, event);
312 case GST_EVENT_FLUSH_STOP:
313 ogg_sync_reset (&ogg->sync);
314 ogg_stream_reset (&ogg->stream);
316 ret = gst_pad_push_event (ogg->srcpad, event);
319 ret = gst_pad_push_event (ogg->srcpad, event);
326 gst_ogg_avi_parse_push_packet (GstOggAviParse * ogg, ogg_packet * packet)
329 GstFlowReturn result;
331 /* allocate space for header and body */
332 buffer = gst_buffer_new_and_alloc (packet->bytes);
333 gst_buffer_fill (buffer, 0, packet->packet, packet->bytes);
335 GST_LOG_OBJECT (ogg, "created buffer %p from page", buffer);
337 GST_BUFFER_OFFSET_END (buffer) = packet->granulepos;
340 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
341 ogg->discont = FALSE;
344 result = gst_pad_push (ogg->srcpad, buffer);
350 gst_ogg_avi_parse_chain (GstPad * pad, GstBuffer * buffer)
352 GstFlowReturn result = GST_FLOW_OK;
358 ogg = GST_OGG_AVI_PARSE (GST_OBJECT_PARENT (pad));
360 size = gst_buffer_get_size (buffer);
362 GST_LOG_OBJECT (ogg, "Chain function received buffer of size %d", size);
364 if (GST_BUFFER_IS_DISCONT (buffer)) {
365 ogg_sync_reset (&ogg->sync);
369 /* write data to sync layer */
370 oggbuf = ogg_sync_buffer (&ogg->sync, size);
371 gst_buffer_extract (buffer, 0, oggbuf, size);
372 ogg_sync_wrote (&ogg->sync, size);
373 gst_buffer_unref (buffer);
375 /* try to get as many packets out of the stream as possible */
379 /* try to swap out a page */
380 ret = ogg_sync_pageout (&ogg->sync, &page);
382 GST_DEBUG_OBJECT (ogg, "need more data");
384 } else if (ret == -1) {
385 GST_DEBUG_OBJECT (ogg, "discont in pages");
388 /* new unknown stream, init the ogg stream with the serial number of the
390 if (ogg->serial == -1) {
391 ogg->serial = ogg_page_serialno (&page);
392 ogg_stream_init (&ogg->stream, ogg->serial);
396 if (ogg_stream_pagein (&ogg->stream, &page) != 0) {
397 GST_WARNING_OBJECT (ogg, "ogg stream choked on page resetting stream");
398 ogg_sync_reset (&ogg->sync);
403 /* try to get as many packets as possible out of the page */
407 ret = ogg_stream_packetout (&ogg->stream, &packet);
408 GST_LOG_OBJECT (ogg, "packetout gave %d", ret);
413 /* out of sync, We mark a DISCONT. */
417 result = gst_ogg_avi_parse_push_packet (ogg, &packet);
418 if (result != GST_FLOW_OK)
422 GST_WARNING_OBJECT (ogg,
423 "invalid return value %d for ogg_stream_packetout, resetting stream",
437 static GstStateChangeReturn
438 gst_ogg_avi_parse_change_state (GstElement * element, GstStateChange transition)
441 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
443 ogg = GST_OGG_AVI_PARSE (element);
445 switch (transition) {
446 case GST_STATE_CHANGE_NULL_TO_READY:
447 ogg_sync_init (&ogg->sync);
449 case GST_STATE_CHANGE_READY_TO_PAUSED:
450 ogg_sync_reset (&ogg->sync);
451 ogg_stream_reset (&ogg->stream);
455 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
461 result = parent_class->change_state (element, transition);
463 switch (transition) {
464 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
466 case GST_STATE_CHANGE_PAUSED_TO_READY:
468 case GST_STATE_CHANGE_READY_TO_NULL:
469 ogg_sync_clear (&ogg->sync);
478 gst_ogg_avi_parse_plugin_init (GstPlugin * plugin)
480 GST_DEBUG_CATEGORY_INIT (gst_ogg_avi_parse_debug, "oggaviparse", 0,
483 return gst_element_register (plugin, "oggaviparse", GST_RANK_PRIMARY,
484 GST_TYPE_OGG_AVI_PARSE);