1 /* GStreamer
2 * Copyright (C) 2006 Wim Taymans <wim@fluendo.com>
3 *
4 * gstoggaviparse.c: ogg avi stream parser
5 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /*
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
25 * pages.
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
29 * directly.
30 * This parser only does ogg/vorbis for now.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 #include <gst/gst.h>
37 #include <ogg/ogg.h>
38 #include <string.h>
39
40 #include "gstogg.h"
41
42 GST_DEBUG_CATEGORY_STATIC (gst_ogg_avi_parse_debug);
43 #define GST_CAT_DEFAULT gst_ogg_avi_parse_debug
44
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))
50
51 static GType gst_ogg_avi_parse_get_type (void);
52
53 typedef struct _GstOggAviParse GstOggAviParse;
54 typedef struct _GstOggAviParseClass GstOggAviParseClass;
55
56 struct _GstOggAviParse
57 {
58 GstElement element;
59
60 GstPad *sinkpad;
61 GstPad *srcpad;
62
63 gboolean discont;
64 gint serial;
65
66 ogg_sync_state sync;
67 ogg_stream_state stream;
68 };
69
70 struct _GstOggAviParseClass
71 {
72 GstElementClass parent_class;
73 };
74
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;
79
80 static GType
gst_ogg_avi_parse_get_type(void)81 gst_ogg_avi_parse_get_type (void)
82 {
83 static GType ogg_avi_parse_type = 0;
84
85 if (!ogg_avi_parse_type) {
86 static const GTypeInfo ogg_avi_parse_info = {
87 sizeof (GstOggAviParseClass),
88 gst_ogg_avi_parse_base_init,
89 NULL,
90 (GClassInitFunc) gst_ogg_avi_parse_class_init,
91 NULL,
92 NULL,
93 sizeof (GstOggAviParse),
94 0,
95 (GInstanceInitFunc) gst_ogg_avi_parse_init,
96 };
97
98 ogg_avi_parse_type =
99 g_type_register_static (GST_TYPE_ELEMENT, "GstOggAviParse",
100 &ogg_avi_parse_info, 0);
101 }
102 return ogg_avi_parse_type;
103 }
104
105 enum
106 {
107 PROP_0
108 };
109
110 static GstStaticPadTemplate ogg_avi_parse_src_template_factory =
111 GST_STATIC_PAD_TEMPLATE ("src",
112 GST_PAD_SRC,
113 GST_PAD_ALWAYS,
114 GST_STATIC_CAPS ("audio/x-vorbis")
115 );
116
117 static GstStaticPadTemplate ogg_avi_parse_sink_template_factory =
118 GST_STATIC_PAD_TEMPLATE ("sink",
119 GST_PAD_SINK,
120 GST_PAD_ALWAYS,
121 GST_STATIC_CAPS ("application/x-ogg-avi")
122 );
123
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, GstObject * parent,
128 GstEvent * event);
129 static GstFlowReturn gst_ogg_avi_parse_chain (GstPad * pad, GstObject * parent,
130 GstBuffer * buffer);
131 static gboolean gst_ogg_avi_parse_setcaps (GstPad * pad, GstCaps * caps);
132
133 static void
gst_ogg_avi_parse_base_init(gpointer g_class)134 gst_ogg_avi_parse_base_init (gpointer g_class)
135 {
136 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
137
138 gst_element_class_set_static_metadata (element_class,
139 "Ogg AVI parser", "Codec/Parser",
140 "parse an ogg avi stream into pages (info about ogg: http://xiph.org)",
141 "Wim Taymans <wim@fluendo.com>");
142
143 gst_element_class_add_static_pad_template (element_class,
144 &ogg_avi_parse_sink_template_factory);
145 gst_element_class_add_static_pad_template (element_class,
146 &ogg_avi_parse_src_template_factory);
147 }
148
149 static void
gst_ogg_avi_parse_class_init(GstOggAviParseClass * klass)150 gst_ogg_avi_parse_class_init (GstOggAviParseClass * klass)
151 {
152 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
153 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
154
155 parent_class = g_type_class_peek_parent (klass);
156
157 gstelement_class->change_state = gst_ogg_avi_parse_change_state;
158
159 gobject_class->finalize = gst_ogg_avi_parse_finalize;
160 }
161
162 static void
gst_ogg_avi_parse_init(GstOggAviParse * ogg)163 gst_ogg_avi_parse_init (GstOggAviParse * ogg)
164 {
165 /* create the sink and source pads */
166 ogg->sinkpad =
167 gst_pad_new_from_static_template (&ogg_avi_parse_sink_template_factory,
168 "sink");
169 gst_pad_set_event_function (ogg->sinkpad, gst_ogg_avi_parse_event);
170 gst_pad_set_chain_function (ogg->sinkpad, gst_ogg_avi_parse_chain);
171 gst_element_add_pad (GST_ELEMENT (ogg), ogg->sinkpad);
172
173 ogg->srcpad =
174 gst_pad_new_from_static_template (&ogg_avi_parse_src_template_factory,
175 "src");
176 gst_pad_use_fixed_caps (ogg->srcpad);
177 gst_element_add_pad (GST_ELEMENT (ogg), ogg->srcpad);
178 }
179
180 static void
gst_ogg_avi_parse_finalize(GObject * object)181 gst_ogg_avi_parse_finalize (GObject * object)
182 {
183 GstOggAviParse *ogg = GST_OGG_AVI_PARSE (object);
184
185 GST_LOG_OBJECT (ogg, "Disposing of object %p", ogg);
186
187 ogg_sync_clear (&ogg->sync);
188 ogg_stream_clear (&ogg->stream);
189
190 G_OBJECT_CLASS (parent_class)->finalize (object);
191 }
192
193 static gboolean
gst_ogg_avi_parse_setcaps(GstPad * pad,GstCaps * caps)194 gst_ogg_avi_parse_setcaps (GstPad * pad, GstCaps * caps)
195 {
196 GstOggAviParse *ogg;
197 GstStructure *structure;
198 const GValue *codec_data;
199 GstBuffer *buffer;
200 GstMapInfo map;
201 guint8 *ptr;
202 gsize left;
203 guint32 sizes[3];
204 GstCaps *outcaps;
205 gint i, offs;
206
207 ogg = GST_OGG_AVI_PARSE (GST_OBJECT_PARENT (pad));
208
209 structure = gst_caps_get_structure (caps, 0);
210
211 /* take codec data */
212 codec_data = gst_structure_get_value (structure, "codec_data");
213 if (codec_data == NULL)
214 goto no_data;
215
216 /* only buffers are valid */
217 if (G_VALUE_TYPE (codec_data) != GST_TYPE_BUFFER)
218 goto wrong_format;
219
220 /* Now parse the data */
221 buffer = gst_value_get_buffer (codec_data);
222
223 /* first 22 bytes are bits_per_sample, channel_mask, GUID
224 * Then we get 3 LE guint32 with the 3 header sizes
225 * then we get the bytes of the 3 headers. */
226 gst_buffer_map (buffer, &map, GST_MAP_READ);
227
228 ptr = map.data;
229 left = map.size;
230
231 GST_LOG_OBJECT (ogg, "configuring codec_data of size %" G_GSIZE_FORMAT, left);
232
233 /* skip headers */
234 ptr += 22;
235 left -= 22;
236
237 /* we need at least 12 bytes for the packet sizes of the 3 headers */
238 if (left < 12)
239 goto buffer_too_small;
240
241 /* read sizes of the 3 headers */
242 sizes[0] = GST_READ_UINT32_LE (ptr);
243 sizes[1] = GST_READ_UINT32_LE (ptr + 4);
244 sizes[2] = GST_READ_UINT32_LE (ptr + 8);
245
246 GST_DEBUG_OBJECT (ogg, "header sizes: %u %u %u", sizes[0], sizes[1],
247 sizes[2]);
248
249 left -= 12;
250
251 /* and we need at least enough data for all the headers */
252 if (left < sizes[0] + sizes[1] + sizes[2])
253 goto buffer_too_small;
254
255 /* set caps */
256 outcaps = gst_caps_new_empty_simple ("audio/x-vorbis");
257 gst_pad_set_caps (ogg->srcpad, outcaps);
258 gst_caps_unref (outcaps);
259
260 /* copy header data */
261 offs = 34;
262 for (i = 0; i < 3; i++) {
263 GstBuffer *out;
264
265 /* now output the raw vorbis header packets */
266 out = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offs, sizes[i]);
267 gst_pad_push (ogg->srcpad, out);
268
269 offs += sizes[i];
270 }
271 gst_buffer_unmap (buffer, &map);
272
273 return TRUE;
274
275 /* ERRORS */
276 no_data:
277 {
278 GST_DEBUG_OBJECT (ogg, "no codec_data found in caps");
279 return FALSE;
280 }
281 wrong_format:
282 {
283 GST_DEBUG_OBJECT (ogg, "codec_data is not a buffer");
284 return FALSE;
285 }
286 buffer_too_small:
287 {
288 GST_DEBUG_OBJECT (ogg, "codec_data is too small");
289 gst_buffer_unmap (buffer, &map);
290 return FALSE;
291 }
292 }
293
294 static gboolean
gst_ogg_avi_parse_event(GstPad * pad,GstObject * parent,GstEvent * event)295 gst_ogg_avi_parse_event (GstPad * pad, GstObject * parent, GstEvent * event)
296 {
297 GstOggAviParse *ogg;
298 gboolean ret;
299
300 ogg = GST_OGG_AVI_PARSE (parent);
301
302 switch (GST_EVENT_TYPE (event)) {
303 case GST_EVENT_CAPS:
304 {
305 GstCaps *caps;
306
307 gst_event_parse_caps (event, &caps);
308 ret = gst_ogg_avi_parse_setcaps (pad, caps);
309 gst_event_unref (event);
310 break;
311 }
312 case GST_EVENT_FLUSH_START:
313 ret = gst_pad_push_event (ogg->srcpad, event);
314 break;
315 case GST_EVENT_FLUSH_STOP:
316 ogg_sync_reset (&ogg->sync);
317 ogg_stream_reset (&ogg->stream);
318 ogg->discont = TRUE;
319 ret = gst_pad_push_event (ogg->srcpad, event);
320 break;
321 default:
322 ret = gst_pad_push_event (ogg->srcpad, event);
323 break;
324 }
325 return ret;
326 }
327
328 static GstFlowReturn
gst_ogg_avi_parse_push_packet(GstOggAviParse * ogg,ogg_packet * packet)329 gst_ogg_avi_parse_push_packet (GstOggAviParse * ogg, ogg_packet * packet)
330 {
331 GstBuffer *buffer;
332 GstFlowReturn result;
333
334 /* allocate space for header and body */
335 buffer = gst_buffer_new_and_alloc (packet->bytes);
336 gst_buffer_fill (buffer, 0, packet->packet, packet->bytes);
337
338 GST_LOG_OBJECT (ogg, "created buffer %p from page", buffer);
339
340 GST_BUFFER_OFFSET_END (buffer) = packet->granulepos;
341
342 if (ogg->discont) {
343 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
344 ogg->discont = FALSE;
345 }
346
347 result = gst_pad_push (ogg->srcpad, buffer);
348
349 return result;
350 }
351
352 static GstFlowReturn
gst_ogg_avi_parse_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)353 gst_ogg_avi_parse_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
354 {
355 GstFlowReturn result = GST_FLOW_OK;
356 GstOggAviParse *ogg;
357 guint size;
358 gchar *oggbuf;
359 gint ret = -1;
360
361 ogg = GST_OGG_AVI_PARSE (parent);
362
363 size = gst_buffer_get_size (buffer);
364
365 GST_LOG_OBJECT (ogg, "Chain function received buffer of size %d", size);
366
367 if (GST_BUFFER_IS_DISCONT (buffer)) {
368 ogg_sync_reset (&ogg->sync);
369 ogg->discont = TRUE;
370 }
371
372 /* write data to sync layer */
373 oggbuf = ogg_sync_buffer (&ogg->sync, size);
374 gst_buffer_extract (buffer, 0, oggbuf, size);
375 ogg_sync_wrote (&ogg->sync, size);
376 gst_buffer_unref (buffer);
377
378 /* try to get as many packets out of the stream as possible */
379 do {
380 ogg_page page;
381
382 /* try to swap out a page */
383 ret = ogg_sync_pageout (&ogg->sync, &page);
384 if (ret == 0) {
385 GST_DEBUG_OBJECT (ogg, "need more data");
386 break;
387 } else if (ret == -1) {
388 GST_DEBUG_OBJECT (ogg, "discont in pages");
389 ogg->discont = TRUE;
390 } else {
391 /* new unknown stream, init the ogg stream with the serial number of the
392 * page. */
393 if (ogg->serial == -1) {
394 ogg->serial = ogg_page_serialno (&page);
395 ogg_stream_init (&ogg->stream, ogg->serial);
396 }
397
398 /* submit page */
399 if (ogg_stream_pagein (&ogg->stream, &page) != 0) {
400 GST_WARNING_OBJECT (ogg, "ogg stream choked on page resetting stream");
401 ogg_sync_reset (&ogg->sync);
402 ogg->discont = TRUE;
403 continue;
404 }
405
406 /* try to get as many packets as possible out of the page */
407 do {
408 ogg_packet packet;
409
410 ret = ogg_stream_packetout (&ogg->stream, &packet);
411 GST_LOG_OBJECT (ogg, "packetout gave %d", ret);
412 switch (ret) {
413 case 0:
414 break;
415 case -1:
416 /* out of sync, We mark a DISCONT. */
417 ogg->discont = TRUE;
418 break;
419 case 1:
420 result = gst_ogg_avi_parse_push_packet (ogg, &packet);
421 if (result != GST_FLOW_OK)
422 goto done;
423 break;
424 default:
425 GST_WARNING_OBJECT (ogg,
426 "invalid return value %d for ogg_stream_packetout, resetting stream",
427 ret);
428 break;
429 }
430 }
431 while (ret != 0);
432 }
433 }
434 while (ret != 0);
435
436 done:
437 return result;
438 }
439
440 static GstStateChangeReturn
gst_ogg_avi_parse_change_state(GstElement * element,GstStateChange transition)441 gst_ogg_avi_parse_change_state (GstElement * element, GstStateChange transition)
442 {
443 GstOggAviParse *ogg;
444 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
445
446 ogg = GST_OGG_AVI_PARSE (element);
447
448 switch (transition) {
449 case GST_STATE_CHANGE_NULL_TO_READY:
450 ogg_sync_init (&ogg->sync);
451 break;
452 case GST_STATE_CHANGE_READY_TO_PAUSED:
453 ogg_sync_reset (&ogg->sync);
454 ogg_stream_reset (&ogg->stream);
455 ogg->serial = -1;
456 ogg->discont = TRUE;
457 break;
458 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
459 break;
460 default:
461 break;
462 }
463
464 result = parent_class->change_state (element, transition);
465
466 switch (transition) {
467 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
468 break;
469 case GST_STATE_CHANGE_PAUSED_TO_READY:
470 break;
471 case GST_STATE_CHANGE_READY_TO_NULL:
472 ogg_sync_clear (&ogg->sync);
473 break;
474 default:
475 break;
476 }
477 return result;
478 }
479
480 gboolean
gst_ogg_avi_parse_plugin_init(GstPlugin * plugin)481 gst_ogg_avi_parse_plugin_init (GstPlugin * plugin)
482 {
483 GST_DEBUG_CATEGORY_INIT (gst_ogg_avi_parse_debug, "oggaviparse", 0,
484 "ogg avi parser");
485
486 return gst_element_register (plugin, "oggaviparse", GST_RANK_PRIMARY,
487 GST_TYPE_OGG_AVI_PARSE);
488 }
489