Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / ext / ogg / gstoggaviparse.c
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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
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, 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);
130
131 static void
132 gst_ogg_avi_parse_base_init (gpointer g_class)
133 {
134   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
135
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>");
140
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));
145 }
146
147 static void
148 gst_ogg_avi_parse_class_init (GstOggAviParseClass * klass)
149 {
150   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
151   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
152
153   parent_class = g_type_class_peek_parent (klass);
154
155   gstelement_class->change_state = gst_ogg_avi_parse_change_state;
156
157   gobject_class->finalize = gst_ogg_avi_parse_finalize;
158 }
159
160 static void
161 gst_ogg_avi_parse_init (GstOggAviParse * ogg)
162 {
163   /* create the sink and source pads */
164   ogg->sinkpad =
165       gst_pad_new_from_static_template (&ogg_avi_parse_sink_template_factory,
166       "sink");
167   gst_pad_set_setcaps_function (ogg->sinkpad, gst_ogg_avi_parse_setcaps);
168   gst_pad_set_event_function (ogg->sinkpad, gst_ogg_avi_parse_event);
169   gst_pad_set_chain_function (ogg->sinkpad, gst_ogg_avi_parse_chain);
170   gst_element_add_pad (GST_ELEMENT (ogg), ogg->sinkpad);
171
172   ogg->srcpad =
173       gst_pad_new_from_static_template (&ogg_avi_parse_src_template_factory,
174       "src");
175   gst_pad_use_fixed_caps (ogg->srcpad);
176   gst_element_add_pad (GST_ELEMENT (ogg), ogg->srcpad);
177 }
178
179 static void
180 gst_ogg_avi_parse_finalize (GObject * object)
181 {
182   GstOggAviParse *ogg = GST_OGG_AVI_PARSE (object);
183
184   GST_LOG_OBJECT (ogg, "Disposing of object %p", ogg);
185
186   ogg_sync_clear (&ogg->sync);
187   ogg_stream_clear (&ogg->stream);
188
189   G_OBJECT_CLASS (parent_class)->finalize (object);
190 }
191
192 static gboolean
193 gst_ogg_avi_parse_setcaps (GstPad * pad, GstCaps * caps)
194 {
195   GstOggAviParse *ogg;
196   GstStructure *structure;
197   const GValue *codec_data;
198   GstBuffer *buffer;
199   guint8 *data;
200   guint size;
201   guint32 sizes[3];
202   GstCaps *outcaps;
203   gint i, offs;
204
205   ogg = GST_OGG_AVI_PARSE (GST_OBJECT_PARENT (pad));
206
207   structure = gst_caps_get_structure (caps, 0);
208
209   /* take codec data */
210   codec_data = gst_structure_get_value (structure, "codec_data");
211   if (codec_data == NULL)
212     goto no_data;
213
214   /* only buffers are valid */
215   if (G_VALUE_TYPE (codec_data) != GST_TYPE_BUFFER)
216     goto wrong_format;
217
218   /* Now parse the data */
219   buffer = gst_value_get_buffer (codec_data);
220
221   /* first 22 bytes are bits_per_sample, channel_mask, GUID
222    * Then we get 3 LE guint32 with the 3 header sizes
223    * then we get the bytes of the 3 headers. */
224   data = GST_BUFFER_DATA (buffer);
225   size = GST_BUFFER_SIZE (buffer);
226
227   GST_LOG_OBJECT (ogg, "configuring codec_data of size %u", size);
228
229   /* skip headers */
230   data += 22;
231   size -= 22;
232
233   /* we need at least 12 bytes for the packet sizes of the 3 headers */
234   if (size < 12)
235     goto buffer_too_small;
236
237   /* read sizes of the 3 headers */
238   sizes[0] = GST_READ_UINT32_LE (data);
239   sizes[1] = GST_READ_UINT32_LE (data + 4);
240   sizes[2] = GST_READ_UINT32_LE (data + 8);
241
242   GST_DEBUG_OBJECT (ogg, "header sizes: %u %u %u", sizes[0], sizes[1],
243       sizes[2]);
244
245   size -= 12;
246
247   /* and we need at least enough data for all the headers */
248   if (size < sizes[0] + sizes[1] + sizes[2])
249     goto buffer_too_small;
250
251   /* set caps */
252   outcaps = gst_caps_new_simple ("audio/x-vorbis", NULL);
253   gst_pad_set_caps (ogg->srcpad, outcaps);
254
255   /* copy header data */
256   offs = 34;
257   for (i = 0; i < 3; i++) {
258     GstBuffer *out;
259
260     /* now output the raw vorbis header packets */
261     out = gst_buffer_create_sub (buffer, offs, sizes[i]);
262     gst_buffer_set_caps (out, outcaps);
263     gst_pad_push (ogg->srcpad, out);
264
265     offs += sizes[i];
266   }
267   gst_caps_unref (outcaps);
268
269   return TRUE;
270
271   /* ERRORS */
272 no_data:
273   {
274     GST_DEBUG_OBJECT (ogg, "no codec_data found in caps");
275     return FALSE;
276   }
277 wrong_format:
278   {
279     GST_DEBUG_OBJECT (ogg, "codec_data is not a buffer");
280     return FALSE;
281   }
282 buffer_too_small:
283   {
284     GST_DEBUG_OBJECT (ogg, "codec_data is too small");
285     return FALSE;
286   }
287 }
288
289 static gboolean
290 gst_ogg_avi_parse_event (GstPad * pad, GstEvent * event)
291 {
292   GstOggAviParse *ogg;
293   gboolean ret;
294
295   ogg = GST_OGG_AVI_PARSE (GST_OBJECT_PARENT (pad));
296
297   switch (GST_EVENT_TYPE (event)) {
298     case GST_EVENT_FLUSH_START:
299       ret = gst_pad_push_event (ogg->srcpad, event);
300       break;
301     case GST_EVENT_FLUSH_STOP:
302       ogg_sync_reset (&ogg->sync);
303       ogg_stream_reset (&ogg->stream);
304       ogg->discont = TRUE;
305       ret = gst_pad_push_event (ogg->srcpad, event);
306       break;
307     default:
308       ret = gst_pad_push_event (ogg->srcpad, event);
309       break;
310   }
311   return ret;
312 }
313
314 static GstFlowReturn
315 gst_ogg_avi_parse_push_packet (GstOggAviParse * ogg, ogg_packet * packet)
316 {
317   GstBuffer *buffer;
318   GstFlowReturn result;
319
320   /* allocate space for header and body */
321   buffer = gst_buffer_new_and_alloc (packet->bytes);
322   memcpy (GST_BUFFER_DATA (buffer), packet->packet, packet->bytes);
323
324   GST_LOG_OBJECT (ogg, "created buffer %p from page", buffer);
325
326   GST_BUFFER_OFFSET_END (buffer) = packet->granulepos;
327
328   if (ogg->discont) {
329     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
330     ogg->discont = FALSE;
331   }
332
333   result = gst_pad_push (ogg->srcpad, buffer);
334
335   return result;
336 }
337
338 static GstFlowReturn
339 gst_ogg_avi_parse_chain (GstPad * pad, GstBuffer * buffer)
340 {
341   GstFlowReturn result = GST_FLOW_OK;
342   GstOggAviParse *ogg;
343   guint8 *data;
344   guint size;
345   gchar *oggbuf;
346   gint ret = -1;
347
348   ogg = GST_OGG_AVI_PARSE (GST_OBJECT_PARENT (pad));
349
350   data = GST_BUFFER_DATA (buffer);
351   size = GST_BUFFER_SIZE (buffer);
352
353   GST_LOG_OBJECT (ogg, "Chain function received buffer of size %d", size);
354
355   if (GST_BUFFER_IS_DISCONT (buffer)) {
356     ogg_sync_reset (&ogg->sync);
357     ogg->discont = TRUE;
358   }
359
360   /* write data to sync layer */
361   oggbuf = ogg_sync_buffer (&ogg->sync, size);
362   memcpy (oggbuf, data, size);
363   ogg_sync_wrote (&ogg->sync, size);
364   gst_buffer_unref (buffer);
365
366   /* try to get as many packets out of the stream as possible */
367   do {
368     ogg_page page;
369
370     /* try to swap out a page */
371     ret = ogg_sync_pageout (&ogg->sync, &page);
372     if (ret == 0) {
373       GST_DEBUG_OBJECT (ogg, "need more data");
374       break;
375     } else if (ret == -1) {
376       GST_DEBUG_OBJECT (ogg, "discont in pages");
377       ogg->discont = TRUE;
378     } else {
379       /* new unknown stream, init the ogg stream with the serial number of the
380        * page. */
381       if (ogg->serial == -1) {
382         ogg->serial = ogg_page_serialno (&page);
383         ogg_stream_init (&ogg->stream, ogg->serial);
384       }
385
386       /* submit page */
387       if (ogg_stream_pagein (&ogg->stream, &page) != 0) {
388         GST_WARNING_OBJECT (ogg, "ogg stream choked on page resetting stream");
389         ogg_sync_reset (&ogg->sync);
390         ogg->discont = TRUE;
391         continue;
392       }
393
394       /* try to get as many packets as possible out of the page */
395       do {
396         ogg_packet packet;
397
398         ret = ogg_stream_packetout (&ogg->stream, &packet);
399         GST_LOG_OBJECT (ogg, "packetout gave %d", ret);
400         switch (ret) {
401           case 0:
402             break;
403           case -1:
404             /* out of sync, We mark a DISCONT. */
405             ogg->discont = TRUE;
406             break;
407           case 1:
408             result = gst_ogg_avi_parse_push_packet (ogg, &packet);
409             if (result != GST_FLOW_OK)
410               goto done;
411             break;
412           default:
413             GST_WARNING_OBJECT (ogg,
414                 "invalid return value %d for ogg_stream_packetout, resetting stream",
415                 ret);
416             break;
417         }
418       }
419       while (ret != 0);
420     }
421   }
422   while (ret != 0);
423
424 done:
425   return result;
426 }
427
428 static GstStateChangeReturn
429 gst_ogg_avi_parse_change_state (GstElement * element, GstStateChange transition)
430 {
431   GstOggAviParse *ogg;
432   GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
433
434   ogg = GST_OGG_AVI_PARSE (element);
435
436   switch (transition) {
437     case GST_STATE_CHANGE_NULL_TO_READY:
438       ogg_sync_init (&ogg->sync);
439       break;
440     case GST_STATE_CHANGE_READY_TO_PAUSED:
441       ogg_sync_reset (&ogg->sync);
442       ogg_stream_reset (&ogg->stream);
443       ogg->serial = -1;
444       ogg->discont = TRUE;
445       break;
446     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
447       break;
448     default:
449       break;
450   }
451
452   result = parent_class->change_state (element, transition);
453
454   switch (transition) {
455     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
456       break;
457     case GST_STATE_CHANGE_PAUSED_TO_READY:
458       break;
459     case GST_STATE_CHANGE_READY_TO_NULL:
460       ogg_sync_clear (&ogg->sync);
461       break;
462     default:
463       break;
464   }
465   return result;
466 }
467
468 gboolean
469 gst_ogg_avi_parse_plugin_init (GstPlugin * plugin)
470 {
471   GST_DEBUG_CATEGORY_INIT (gst_ogg_avi_parse_debug, "oggaviparse", 0,
472       "ogg avi parser");
473
474   return gst_element_register (plugin, "oggaviparse", GST_RANK_PRIMARY,
475       GST_TYPE_OGG_AVI_PARSE);
476 }