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