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