gio: Remove unused function
[platform/upstream/gstreamer.git] / ext / ogg / gstogmparse.c
1 /* GStreamer OGM parsing
2  * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <gst/gst.h>
29 #include <gst/tag/tag.h>
30 #include <gst/riff/riff-media.h>
31 #include <gst/riff/riff-read.h>
32
33 GST_DEBUG_CATEGORY_STATIC (gst_ogm_parse_debug);
34 #define GST_CAT_DEFAULT gst_ogm_parse_debug
35
36 #define GST_TYPE_OGM_VIDEO_PARSE (gst_ogm_video_parse_get_type())
37 #define GST_IS_OGM_VIDEO_PARSE(obj) \
38   (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_VIDEO_PARSE))
39
40 #define GST_TYPE_OGM_AUDIO_PARSE (gst_ogm_audio_parse_get_type())
41 #define GST_IS_OGM_AUDIO_PARSE(obj) \
42   (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_AUDIO_PARSE))
43
44 #define GST_TYPE_OGM_TEXT_PARSE (gst_ogm_text_parse_get_type())
45 #define GST_IS_OGM_TEXT_PARSE(obj) \
46   (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_TEXT_PARSE))
47
48 #define GST_TYPE_OGM_PARSE (gst_ogm_parse_get_type())
49 #define GST_OGM_PARSE(obj) \
50   (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_OGM_PARSE, GstOgmParse))
51 #define GST_OGM_PARSE_CLASS(klass) \
52   (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_OGM_PARSE, GstOgmParse))
53 #define GST_IS_OGM_PARSE(obj) \
54   (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_PARSE))
55 #define GST_IS_OGM_PARSE_CLASS(klass) \
56   (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_OGM_PARSE))
57 #define GST_OGM_PARSE_GET_CLASS(obj) \
58   (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_OGM_PARSE, GstOgmParseClass))
59
60 static const GstElementDetails gst_ogm_audio_parse_details =
61 GST_ELEMENT_DETAILS ("OGM audio stream parser",
62     "Codec/Decoder/Audio",
63     "parse an OGM audio header and stream",
64     "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
65
66 static const GstElementDetails gst_ogm_video_parse_details =
67 GST_ELEMENT_DETAILS ("OGM video stream parser",
68     "Codec/Decoder/Video",
69     "parse an OGM video header and stream",
70     "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
71
72 static const GstElementDetails gst_ogm_text_parse_details =
73 GST_ELEMENT_DETAILS ("OGM text stream parser",
74     "Codec/Decoder/Subtitle",
75     "parse an OGM text header and stream",
76     "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
77
78 typedef struct _stream_header_video
79 {
80   gint32 width;
81   gint32 height;
82 } stream_header_video;
83
84 typedef struct _stream_header_audio
85 {
86   gint16 channels;
87   gint16 blockalign;
88   gint32 avgbytespersec;
89 } stream_header_audio;
90
91 /* sizeof(stream_header) might differ due to structure packing and
92  * alignment differences on some architectures, so not using that */
93 #define OGM_STREAM_HEADER_SIZE (8+4+4+8+8+4+4+4+8)
94
95 typedef struct _stream_header
96 {
97   gchar streamtype[8];
98   gchar subtype[4 + 1];
99
100   /* size of the structure */
101   gint32 size;
102
103   /* in reference time */
104   gint64 time_unit;
105
106   gint64 samples_per_unit;
107
108   /* in media time */
109   gint32 default_len;
110
111   gint32 buffersize;
112   gint32 bits_per_sample;
113
114   union
115   {
116     stream_header_video video;
117     stream_header_audio audio;
118     /* text has no additional data */
119   } s;
120 } stream_header;
121
122 typedef struct _GstOgmParse
123 {
124   GstElement element;
125
126   /* pads */
127   GstPad *srcpad, *sinkpad;
128   GstPadTemplate *srcpadtempl;
129
130   /* we need to cache events that we receive before creating the source pad */
131   GList *cached_events;
132
133   /* audio or video */
134   stream_header hdr;
135
136   /* expected next granulepos (used for timestamp guessing) */
137   guint64 next_granulepos;
138 } GstOgmParse;
139
140 typedef struct _GstOgmParseClass
141 {
142   GstElementClass parent_class;
143 } GstOgmParseClass;
144
145 static GstStaticPadTemplate sink_factory_video =
146 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
147     GST_STATIC_CAPS ("application/x-ogm-video"));
148 static GstStaticPadTemplate sink_factory_audio =
149 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
150     GST_STATIC_CAPS ("application/x-ogm-audio"));
151 static GstStaticPadTemplate sink_factory_text =
152 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
153     GST_STATIC_CAPS ("application/x-ogm-text"));
154 static GstPadTemplate *video_src_templ, *audio_src_templ, *text_src_templ;
155
156 static GType gst_ogm_audio_parse_get_type (void);
157 static GType gst_ogm_video_parse_get_type (void);
158 static GType gst_ogm_text_parse_get_type (void);
159 static GType gst_ogm_parse_get_type (void);
160
161 static void gst_ogm_audio_parse_base_init (GstOgmParseClass * klass);
162 static void gst_ogm_video_parse_base_init (GstOgmParseClass * klass);
163 static void gst_ogm_text_parse_base_init (GstOgmParseClass * klass);
164 static void gst_ogm_parse_class_init (GstOgmParseClass * klass);
165 static void gst_ogm_parse_init (GstOgmParse * ogm);
166 static void gst_ogm_video_parse_init (GstOgmParse * ogm);
167 static void gst_ogm_audio_parse_init (GstOgmParse * ogm);
168 static void gst_ogm_text_parse_init (GstOgmParse * ogm);
169
170 static const GstQueryType *gst_ogm_parse_get_sink_querytypes (GstPad * pad);
171 static gboolean gst_ogm_parse_sink_event (GstPad * pad, GstEvent * event);
172 static gboolean gst_ogm_parse_sink_query (GstPad * pad, GstQuery * query);
173 static gboolean gst_ogm_parse_sink_convert (GstPad * pad, GstFormat src_format,
174     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
175
176 static GstFlowReturn gst_ogm_parse_chain (GstPad * pad, GstBuffer * buffer);
177
178 static GstStateChangeReturn gst_ogm_parse_change_state (GstElement * element,
179     GstStateChange transition);
180
181 static GstElementClass *parent_class = NULL;
182
183 static GType
184 gst_ogm_parse_get_type (void)
185 {
186   static GType ogm_parse_type = 0;
187
188   if (!ogm_parse_type) {
189     static const GTypeInfo ogm_parse_info = {
190       sizeof (GstOgmParseClass),
191       NULL,
192       NULL,
193       (GClassInitFunc) gst_ogm_parse_class_init,
194       NULL,
195       NULL,
196       sizeof (GstOgmParse),
197       0,
198       (GInstanceInitFunc) gst_ogm_parse_init,
199     };
200
201     ogm_parse_type =
202         g_type_register_static (GST_TYPE_ELEMENT,
203         "GstOgmParse", &ogm_parse_info, 0);
204   }
205
206   return ogm_parse_type;
207 }
208
209 static GType
210 gst_ogm_audio_parse_get_type (void)
211 {
212   static GType ogm_audio_parse_type = 0;
213
214   if (!ogm_audio_parse_type) {
215     static const GTypeInfo ogm_audio_parse_info = {
216       sizeof (GstOgmParseClass),
217       (GBaseInitFunc) gst_ogm_audio_parse_base_init,
218       NULL,
219       NULL,
220       NULL,
221       NULL,
222       sizeof (GstOgmParse),
223       0,
224       (GInstanceInitFunc) gst_ogm_audio_parse_init,
225     };
226
227     ogm_audio_parse_type =
228         g_type_register_static (GST_TYPE_OGM_PARSE,
229         "GstOgmAudioParse", &ogm_audio_parse_info, 0);
230   }
231
232   return ogm_audio_parse_type;
233 }
234
235 static GType
236 gst_ogm_video_parse_get_type (void)
237 {
238   static GType ogm_video_parse_type = 0;
239
240   if (!ogm_video_parse_type) {
241     static const GTypeInfo ogm_video_parse_info = {
242       sizeof (GstOgmParseClass),
243       (GBaseInitFunc) gst_ogm_video_parse_base_init,
244       NULL,
245       NULL,
246       NULL,
247       NULL,
248       sizeof (GstOgmParse),
249       0,
250       (GInstanceInitFunc) gst_ogm_video_parse_init,
251     };
252
253     ogm_video_parse_type =
254         g_type_register_static (GST_TYPE_OGM_PARSE,
255         "GstOgmVideoParse", &ogm_video_parse_info, 0);
256   }
257
258   return ogm_video_parse_type;
259 }
260
261 static GType
262 gst_ogm_text_parse_get_type (void)
263 {
264   static GType ogm_text_parse_type = 0;
265
266   if (!ogm_text_parse_type) {
267     static const GTypeInfo ogm_text_parse_info = {
268       sizeof (GstOgmParseClass),
269       (GBaseInitFunc) gst_ogm_text_parse_base_init,
270       NULL,
271       NULL,
272       NULL,
273       NULL,
274       sizeof (GstOgmParse),
275       0,
276       (GInstanceInitFunc) gst_ogm_text_parse_init,
277     };
278
279     ogm_text_parse_type =
280         g_type_register_static (GST_TYPE_OGM_PARSE,
281         "GstOgmTextParse", &ogm_text_parse_info, 0);
282   }
283
284   return ogm_text_parse_type;
285 }
286
287 static void
288 gst_ogm_audio_parse_base_init (GstOgmParseClass * klass)
289 {
290   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
291   GstCaps *caps = gst_riff_create_audio_template_caps ();
292
293   gst_element_class_set_details (element_class, &gst_ogm_audio_parse_details);
294
295   gst_element_class_add_pad_template (element_class,
296       gst_static_pad_template_get (&sink_factory_audio));
297   audio_src_templ = gst_pad_template_new ("src",
298       GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
299   gst_element_class_add_pad_template (element_class, audio_src_templ);
300 }
301
302 static void
303 gst_ogm_video_parse_base_init (GstOgmParseClass * klass)
304 {
305   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
306   GstCaps *caps = gst_riff_create_video_template_caps ();
307
308   gst_element_class_set_details (element_class, &gst_ogm_video_parse_details);
309
310   gst_element_class_add_pad_template (element_class,
311       gst_static_pad_template_get (&sink_factory_video));
312   video_src_templ = gst_pad_template_new ("src",
313       GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
314   gst_element_class_add_pad_template (element_class, video_src_templ);
315 }
316
317 static void
318 gst_ogm_text_parse_base_init (GstOgmParseClass * klass)
319 {
320   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
321   GstCaps *caps = gst_caps_new_simple ("text/plain", NULL, NULL);
322
323   gst_element_class_set_details (element_class, &gst_ogm_text_parse_details);
324
325   gst_element_class_add_pad_template (element_class,
326       gst_static_pad_template_get (&sink_factory_text));
327   text_src_templ = gst_pad_template_new ("src",
328       GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
329   gst_element_class_add_pad_template (element_class, text_src_templ);
330 }
331
332 static void
333 gst_ogm_parse_class_init (GstOgmParseClass * klass)
334 {
335   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
336
337   parent_class = g_type_class_peek_parent (klass);
338
339   gstelement_class->change_state =
340       GST_DEBUG_FUNCPTR (gst_ogm_parse_change_state);
341 }
342
343 static void
344 gst_ogm_parse_init (GstOgmParse * ogm)
345 {
346   memset (&ogm->hdr, 0, sizeof (ogm->hdr));
347   ogm->next_granulepos = 0;
348   ogm->srcpad = NULL;
349   ogm->cached_events = NULL;
350 }
351
352 static void
353 gst_ogm_audio_parse_init (GstOgmParse * ogm)
354 {
355   ogm->sinkpad = gst_pad_new_from_static_template (&sink_factory_audio, "sink");
356   gst_pad_set_query_function (ogm->sinkpad,
357       GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_query));
358   gst_pad_set_chain_function (ogm->sinkpad,
359       GST_DEBUG_FUNCPTR (gst_ogm_parse_chain));
360   gst_pad_set_event_function (ogm->sinkpad,
361       GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_event));
362   gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);
363
364   ogm->srcpad = NULL;
365   ogm->srcpadtempl = audio_src_templ;
366 }
367
368 static void
369 gst_ogm_video_parse_init (GstOgmParse * ogm)
370 {
371   ogm->sinkpad = gst_pad_new_from_static_template (&sink_factory_video, "sink");
372   gst_pad_set_query_function (ogm->sinkpad,
373       GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_query));
374   gst_pad_set_chain_function (ogm->sinkpad,
375       GST_DEBUG_FUNCPTR (gst_ogm_parse_chain));
376   gst_pad_set_event_function (ogm->sinkpad,
377       GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_event));
378   gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);
379
380   ogm->srcpad = NULL;
381   ogm->srcpadtempl = video_src_templ;
382 }
383
384 static void
385 gst_ogm_text_parse_init (GstOgmParse * ogm)
386 {
387   ogm->sinkpad = gst_pad_new_from_static_template (&sink_factory_text, "sink");
388   gst_pad_set_query_type_function (ogm->sinkpad,
389       gst_ogm_parse_get_sink_querytypes);
390   gst_pad_set_query_function (ogm->sinkpad,
391       GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_query));
392   gst_pad_set_chain_function (ogm->sinkpad,
393       GST_DEBUG_FUNCPTR (gst_ogm_parse_chain));
394   gst_pad_set_event_function (ogm->sinkpad,
395       GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_event));
396   gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);
397
398   ogm->srcpad = NULL;
399   ogm->srcpadtempl = text_src_templ;
400 }
401
402 static const GstQueryType *
403 gst_ogm_parse_get_sink_querytypes (GstPad * pad)
404 {
405   static const GstQueryType types[] = {
406     GST_QUERY_POSITION,
407     0
408   };
409
410   return types;
411 }
412
413 static gboolean
414 gst_ogm_parse_sink_convert (GstPad * pad,
415     GstFormat src_format, gint64 src_value,
416     GstFormat * dest_format, gint64 * dest_value)
417 {
418   gboolean res = FALSE;
419   GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));
420
421   switch (src_format) {
422     case GST_FORMAT_DEFAULT:
423       switch (*dest_format) {
424         case GST_FORMAT_TIME:
425           switch (ogm->hdr.streamtype[0]) {
426             case 'a':
427               *dest_value = GST_SECOND * src_value / ogm->hdr.samples_per_unit;
428               res = TRUE;
429               break;
430             case 'v':
431             case 't':
432               *dest_value = (GST_SECOND / 10000000) *
433                   ogm->hdr.time_unit * src_value;
434               res = TRUE;
435               break;
436             default:
437               break;
438           }
439           break;
440         default:
441           break;
442       }
443       break;
444     case GST_FORMAT_TIME:
445       switch (*dest_format) {
446         case GST_FORMAT_DEFAULT:
447           switch (ogm->hdr.streamtype[0]) {
448             case 'a':
449               *dest_value = ogm->hdr.samples_per_unit * src_value / GST_SECOND;
450               res = TRUE;
451               break;
452             case 'v':
453             case 't':
454               *dest_value = src_value /
455                   ((GST_SECOND / 10000000) * ogm->hdr.time_unit);
456               res = TRUE;
457               break;
458             default:
459               break;
460           }
461           break;
462         default:
463           break;
464       }
465       break;
466     default:
467       break;
468   }
469
470   gst_object_unref (ogm);
471   return res;
472 }
473
474 static gboolean
475 gst_ogm_parse_sink_query (GstPad * pad, GstQuery * query)
476 {
477   GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));
478   GstFormat format;
479   gboolean res = FALSE;
480
481   switch (GST_QUERY_TYPE (query)) {
482     case GST_QUERY_POSITION:
483     {
484       gint64 val;
485
486       gst_query_parse_position (query, &format, NULL);
487
488       if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME)
489         break;
490
491       if ((res = gst_ogm_parse_sink_convert (pad,
492                   GST_FORMAT_DEFAULT, ogm->next_granulepos, &format, &val))) {
493         /* don't know the total length here.. */
494         gst_query_set_position (query, format, val);
495       }
496       break;
497     }
498     case GST_QUERY_CONVERT:
499     {
500       GstFormat src_fmt, dest_fmt;
501       gint64 src_val, dest_val;
502
503       /* peel off input */
504       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
505       if ((res = gst_ogm_parse_sink_convert (pad, src_fmt, src_val,
506                   &dest_fmt, &dest_val))) {
507         gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
508       }
509       break;
510     }
511     default:
512       res = gst_pad_query_default (pad, query);
513       break;
514   }
515
516   gst_object_unref (ogm);
517   return res;
518 }
519
520 static GstFlowReturn
521 gst_ogm_parse_stream_header (GstOgmParse * ogm, const guint8 * data, guint size)
522 {
523   GstCaps *caps = NULL;
524
525   /* stream header */
526   if (size < OGM_STREAM_HEADER_SIZE)
527     goto buffer_too_small;
528
529   if (!memcmp (data, "video\000\000\000", 8)) {
530     ogm->hdr.s.video.width = GST_READ_UINT32_LE (&data[44]);
531     ogm->hdr.s.video.height = GST_READ_UINT32_LE (&data[48]);
532   } else if (!memcmp (data, "audio\000\000\000", 8)) {
533     ogm->hdr.s.audio.channels = GST_READ_UINT32_LE (&data[44]);
534     ogm->hdr.s.audio.blockalign = GST_READ_UINT32_LE (&data[46]);
535     ogm->hdr.s.audio.avgbytespersec = GST_READ_UINT32_LE (&data[48]);
536   } else if (!memcmp (data, "text\000\000\000\000", 8)) {
537     /* nothing here */
538   } else {
539     goto cannot_decode;
540   }
541   memcpy (ogm->hdr.streamtype, &data[0], 8);
542   memcpy (ogm->hdr.subtype, &data[8], 4);
543   ogm->hdr.subtype[4] = '\0';
544   ogm->hdr.size = GST_READ_UINT32_LE (&data[12]);
545   ogm->hdr.time_unit = GST_READ_UINT64_LE (&data[16]);
546   ogm->hdr.samples_per_unit = GST_READ_UINT64_LE (&data[24]);
547   ogm->hdr.default_len = GST_READ_UINT32_LE (&data[32]);
548   ogm->hdr.buffersize = GST_READ_UINT32_LE (&data[36]);
549   ogm->hdr.bits_per_sample = GST_READ_UINT32_LE (&data[40]);
550
551   switch (ogm->hdr.streamtype[0]) {
552     case 'a':{
553       guint codec_id = 0;
554
555       if (sscanf (ogm->hdr.subtype, "%04x", &codec_id) != 1) {
556         GST_WARNING_OBJECT (ogm, "cannot parse subtype %s", ogm->hdr.subtype);
557       }
558
559       caps =
560           gst_riff_create_audio_caps (codec_id, NULL, NULL, NULL, NULL, NULL);
561
562       if (caps == NULL) {
563         GST_WARNING_OBJECT (ogm, "no audio caps for codec %u found", codec_id);
564         caps = gst_caps_new_simple ("audio/x-ogm-unknown", "codec_id",
565             G_TYPE_INT, (gint) codec_id, NULL);
566       }
567
568       gst_caps_set_simple (caps,
569           "channels", G_TYPE_INT, ogm->hdr.s.audio.channels,
570           "rate", G_TYPE_INT, ogm->hdr.samples_per_unit, NULL);
571
572       GST_LOG_OBJECT (ogm, "Type: %s, subtype: 0x%04x, channels: %d, "
573           "samplerate: %d, blockalign: %d, bps: %d, caps = %" GST_PTR_FORMAT,
574           ogm->hdr.streamtype, codec_id, ogm->hdr.s.audio.channels,
575           (gint) ogm->hdr.samples_per_unit, ogm->hdr.s.audio.blockalign,
576           ogm->hdr.s.audio.avgbytespersec, caps);
577       break;
578     }
579     case 'v':{
580       guint32 fourcc;
581       gint time_unit;
582
583       fourcc = GST_MAKE_FOURCC (ogm->hdr.subtype[0],
584           ogm->hdr.subtype[1], ogm->hdr.subtype[2], ogm->hdr.subtype[3]);
585
586       caps = gst_riff_create_video_caps (fourcc, NULL, NULL, NULL, NULL, NULL);
587
588       if (caps == NULL) {
589         GST_WARNING_OBJECT (ogm, "could not find video caps for fourcc %"
590             GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
591         caps = gst_caps_new_simple ("video/x-ogm-unknown", "fourcc",
592             GST_TYPE_FOURCC, fourcc, NULL);
593         break;
594       }
595
596       GST_LOG_OBJECT (ogm, "Type: %s, subtype: %" GST_FOURCC_FORMAT
597           ", size: %dx%d, timeunit: %" G_GINT64_FORMAT
598           " (fps: %lf), s/u: %" G_GINT64_FORMAT ", "
599           "def.len: %d, bufsize: %d, bps: %d, caps = %" GST_PTR_FORMAT,
600           ogm->hdr.streamtype, GST_FOURCC_ARGS (fourcc),
601           ogm->hdr.s.video.width, ogm->hdr.s.video.height,
602           ogm->hdr.time_unit, 10000000. / ogm->hdr.time_unit,
603           ogm->hdr.samples_per_unit, ogm->hdr.default_len,
604           ogm->hdr.buffersize, ogm->hdr.bits_per_sample, caps);
605
606       /* GST_TYPE_FRACTION contains gint */
607       if (ogm->hdr.time_unit > G_MAXINT || ogm->hdr.time_unit < G_MININT)
608         GST_WARNING_OBJECT (ogm, "timeunit is out of range");
609
610       time_unit = (gint) CLAMP (ogm->hdr.time_unit, G_MININT, G_MAXINT);
611       gst_caps_set_simple (caps,
612           "width", G_TYPE_INT, ogm->hdr.s.video.width,
613           "height", G_TYPE_INT, ogm->hdr.s.video.height,
614           "framerate", GST_TYPE_FRACTION, 10000000, time_unit, NULL);
615       break;
616     }
617     case 't':{
618       GST_LOG_OBJECT (ogm, "Type: %s, s/u: %" G_GINT64_FORMAT
619           ", timeunit=%" G_GINT64_FORMAT,
620           ogm->hdr.streamtype, ogm->hdr.samples_per_unit, ogm->hdr.time_unit);
621       caps = gst_caps_new_simple ("text/plain", NULL);
622       break;
623     }
624     default:
625       g_assert_not_reached ();
626   }
627
628   if (caps == NULL)
629     goto cannot_decode;
630
631   if (ogm->srcpad) {
632     GstCaps *current_caps = GST_PAD_CAPS (ogm->srcpad);
633
634     if (current_caps && caps && !gst_caps_is_equal (current_caps, caps)) {
635       GST_WARNING_OBJECT (ogm, "Already an existing pad %s:%s",
636           GST_DEBUG_PAD_NAME (ogm->srcpad));
637       gst_pad_set_active (ogm->srcpad, FALSE);
638       gst_element_remove_pad (GST_ELEMENT (ogm), ogm->srcpad);
639       ogm->srcpad = NULL;
640     } else {
641       GST_DEBUG_OBJECT (ogm, "Existing pad has the same caps, do nothing");
642     }
643   }
644
645   if (ogm->srcpad == NULL) {
646     GList *l, *cached_events;
647
648     ogm->srcpad = gst_pad_new_from_template (ogm->srcpadtempl, "src");
649     gst_pad_use_fixed_caps (ogm->srcpad);
650     gst_pad_set_caps (ogm->srcpad, caps);
651     gst_pad_set_active (ogm->srcpad, TRUE);
652     gst_element_add_pad (GST_ELEMENT (ogm), ogm->srcpad);
653     GST_INFO_OBJECT (ogm, "Added pad %s:%s with caps %" GST_PTR_FORMAT,
654         GST_DEBUG_PAD_NAME (ogm->srcpad), caps);
655
656     GST_OBJECT_LOCK (ogm);
657     cached_events = ogm->cached_events;
658     ogm->cached_events = NULL;
659     GST_OBJECT_UNLOCK (ogm);
660
661     for (l = cached_events; l; l = l->next) {
662       GstEvent *event = GST_EVENT_CAST (l->data);
663
664       GST_DEBUG_OBJECT (ogm, "Pushing cached event %" GST_PTR_FORMAT, event);
665       gst_pad_push_event (ogm->srcpad, event);
666     }
667     g_list_free (cached_events);
668
669     {
670       GstTagList *tags;
671
672       tags = gst_tag_list_new ();
673       gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_SUBTITLE_CODEC,
674           "Ogm", NULL);
675       gst_element_found_tags_for_pad (GST_ELEMENT (ogm), ogm->srcpad, tags);
676     }
677   }
678
679   gst_caps_unref (caps);
680
681   return GST_FLOW_OK;
682
683 /* ERRORS */
684 buffer_too_small:
685   {
686     GST_ELEMENT_ERROR (ogm, STREAM, WRONG_TYPE, ("Buffer too small"), (NULL));
687     return GST_FLOW_ERROR;
688   }
689 cannot_decode:
690   {
691     GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL), ("unknown ogm format"));
692     return GST_FLOW_ERROR;
693   }
694 }
695
696 static GstFlowReturn
697 gst_ogm_parse_comment_packet (GstOgmParse * ogm, GstBuffer * buf)
698 {
699   GstFlowReturn ret;
700
701   if (ogm->srcpad == NULL) {
702     GST_DEBUG ("no source pad");
703     return GST_FLOW_WRONG_STATE;
704   }
705
706   /* if this is not a subtitle stream, push the vorbiscomment packet
707    * on downstream, the respective decoder will handle it; if it is
708    * a subtitle stream, we will have to handle the comment ourself */
709   if (ogm->hdr.streamtype[0] == 't') {
710     GstTagList *tags;
711
712     tags = gst_tag_list_from_vorbiscomment_buffer (buf,
713         (guint8 *) "\003vorbis", 7, NULL);
714
715     if (tags) {
716       GST_DEBUG_OBJECT (ogm, "tags = %" GST_PTR_FORMAT, tags);
717       gst_element_found_tags_for_pad (GST_ELEMENT (ogm), ogm->srcpad, tags);
718     } else {
719       GST_DEBUG_OBJECT (ogm, "failed to extract tags from vorbis comment");
720     }
721     /* do not push packet downstream, just let parent unref it */
722     ret = GST_FLOW_OK;
723   } else {
724     buf = gst_buffer_copy (buf);
725     gst_buffer_set_caps (buf, GST_PAD_CAPS (ogm->srcpad));
726     ret = gst_pad_push (ogm->srcpad, buf);
727   }
728
729   return ret;
730 }
731
732 static void
733 gst_ogm_text_parse_strip_trailing_zeroes (GstOgmParse * ogm, GstBuffer * buf)
734 {
735   const guint8 *data;
736   guint size;
737
738   g_assert (gst_buffer_is_metadata_writable (buf));
739
740   /* zeroes are not valid UTF-8 characters, so strip them from output */
741   data = GST_BUFFER_DATA (buf);
742   size = GST_BUFFER_SIZE (buf);
743   while (size > 0 && data[size - 1] == '\0') {
744     --size;
745   }
746
747   GST_BUFFER_SIZE (buf) = size;
748 }
749
750 static GstFlowReturn
751 gst_ogm_parse_data_packet (GstOgmParse * ogm, GstBuffer * buf)
752 {
753   GstFlowReturn ret;
754   const guint8 *data;
755   GstBuffer *sbuf;
756   gboolean keyframe;
757   guint size, len, n, xsize = 0;
758
759   data = GST_BUFFER_DATA (buf);
760   size = GST_BUFFER_SIZE (buf);
761
762   if ((data[0] & 0x01) != 0)
763     goto invalid_startcode;
764
765   /* data - push on */
766   len = ((data[0] & 0xc0) >> 6) | ((data[0] & 0x02) << 1);
767   keyframe = (((data[0] & 0x08) >> 3) != 0);
768
769   if ((1 + len) > size)
770     goto buffer_too_small;
771
772   for (n = len; n > 0; n--) {
773     xsize = (xsize << 8) | data[n];
774   }
775
776   GST_LOG_OBJECT (ogm, "[0x%02x] samples: %d, hdrbytes: %d, datasize: %d",
777       data[0], xsize, len, size - len - 1);
778
779   sbuf = gst_buffer_create_sub (buf, len + 1, size - len - 1);
780
781   if (GST_BUFFER_OFFSET_END_IS_VALID (buf))
782     ogm->next_granulepos = GST_BUFFER_OFFSET_END (buf);
783
784   switch (ogm->hdr.streamtype[0]) {
785     case 't':
786     case 'v':{
787       GstClockTime ts, next_ts;
788       guint samples;
789
790       samples = (ogm->hdr.streamtype[0] == 'v') ? 1 : xsize;
791
792       if (!keyframe) {
793         GST_BUFFER_FLAG_SET (sbuf, GST_BUFFER_FLAG_DELTA_UNIT);
794       }
795
796       /* shouldn't this be granulepos - samples? (tpm) */
797       ts = gst_util_uint64_scale (ogm->next_granulepos,
798           ogm->hdr.time_unit * GST_SECOND, 10000000);
799       next_ts = gst_util_uint64_scale (ogm->next_granulepos + samples,
800           ogm->hdr.time_unit * GST_SECOND, 10000000);
801
802       GST_BUFFER_TIMESTAMP (sbuf) = ts;
803       GST_BUFFER_DURATION (sbuf) = next_ts - ts;
804
805       ogm->next_granulepos += samples;
806
807       if (ogm->hdr.streamtype[0] == 't') {
808         gst_ogm_text_parse_strip_trailing_zeroes (ogm, sbuf);
809       }
810       break;
811     }
812     case 'a':{
813       GstClockTime ts, next_ts;
814
815       /* shouldn't this be granulepos - samples? (tpm) */
816       ts = gst_util_uint64_scale_int (ogm->next_granulepos,
817           GST_SECOND, ogm->hdr.samples_per_unit);
818       next_ts = gst_util_uint64_scale_int (ogm->next_granulepos + xsize,
819           GST_SECOND, ogm->hdr.samples_per_unit);
820
821       GST_BUFFER_TIMESTAMP (sbuf) = ts;
822       GST_BUFFER_DURATION (sbuf) = next_ts - ts;
823
824       ogm->next_granulepos += xsize;
825       break;
826     }
827     default:
828       g_assert_not_reached ();
829       break;
830   }
831
832   if (ogm->srcpad) {
833     gst_buffer_set_caps (sbuf, GST_PAD_CAPS (ogm->srcpad));
834     GST_LOG_OBJECT (ogm, "Pushing buffer with ts=%" GST_TIME_FORMAT,
835         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (sbuf)));
836     ret = gst_pad_push (ogm->srcpad, sbuf);
837     if (ret != GST_FLOW_OK) {
838       GST_DEBUG_OBJECT (ogm, "Flow on %s:%s = %s",
839           GST_DEBUG_PAD_NAME (ogm->srcpad), gst_flow_get_name (ret));
840     }
841   } else {
842     ret = GST_FLOW_WRONG_STATE;
843   }
844
845   return ret;
846
847 /* ERRORS */
848 invalid_startcode:
849   {
850     GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL),
851         ("unexpected packet startcode 0x%02x", data[0]));
852     return GST_FLOW_ERROR;
853   }
854 buffer_too_small:
855   {
856     GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL),
857         ("buffer too small, len+1=%u, size=%u", len + 1, size));
858     return GST_FLOW_ERROR;
859   }
860 }
861
862 static GstFlowReturn
863 gst_ogm_parse_chain (GstPad * pad, GstBuffer * buf)
864 {
865   GstFlowReturn ret = GST_FLOW_OK;
866   GstOgmParse *ogm = GST_OGM_PARSE (GST_PAD_PARENT (pad));
867   guint8 *data = GST_BUFFER_DATA (buf);
868   guint size = GST_BUFFER_SIZE (buf);
869
870   if (size < 1)
871     goto buffer_too_small;
872
873   GST_LOG_OBJECT (ogm, "Packet with start code 0x%02x", data[0]);
874
875   switch (data[0]) {
876     case 0x01:{
877       ret = gst_ogm_parse_stream_header (ogm, data + 1, size - 1);
878       break;
879     }
880     case 0x03:{
881       ret = gst_ogm_parse_comment_packet (ogm, buf);
882       break;
883     }
884     default:{
885       ret = gst_ogm_parse_data_packet (ogm, buf);
886       break;
887     }
888   }
889
890   gst_buffer_unref (buf);
891
892   if (ret != GST_FLOW_OK) {
893     GST_DEBUG_OBJECT (ogm, "Flow: %s", gst_flow_get_name (ret));
894   }
895
896   return ret;
897
898 /* ERRORS */
899 buffer_too_small:
900   {
901     GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL), ("buffer too small"));
902     gst_buffer_unref (buf);
903     return GST_FLOW_ERROR;
904   }
905 }
906
907 static gboolean
908 gst_ogm_parse_sink_event (GstPad * pad, GstEvent * event)
909 {
910   GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));
911   gboolean res;
912
913   GST_LOG_OBJECT (ogm, "processing %s event", GST_EVENT_TYPE_NAME (event));
914
915   GST_OBJECT_LOCK (ogm);
916   if (ogm->srcpad == NULL) {
917     ogm->cached_events = g_list_append (ogm->cached_events, event);
918     GST_OBJECT_UNLOCK (ogm);
919     res = TRUE;
920   } else {
921     GST_OBJECT_UNLOCK (ogm);
922     res = gst_pad_event_default (pad, event);
923   }
924
925   gst_object_unref (ogm);
926   return res;
927 }
928
929 static GstStateChangeReturn
930 gst_ogm_parse_change_state (GstElement * element, GstStateChange transition)
931 {
932   GstStateChangeReturn ret;
933   GstOgmParse *ogm = GST_OGM_PARSE (element);
934
935   ret = parent_class->change_state (element, transition);
936   if (ret != GST_STATE_CHANGE_SUCCESS)
937     return ret;
938
939   switch (transition) {
940     case GST_STATE_CHANGE_PAUSED_TO_READY:
941       if (ogm->srcpad) {
942         gst_pad_set_active (ogm->srcpad, FALSE);
943         gst_element_remove_pad (element, ogm->srcpad);
944         ogm->srcpad = NULL;
945       }
946       memset (&ogm->hdr, 0, sizeof (ogm->hdr));
947       ogm->next_granulepos = 0;
948       g_list_foreach (ogm->cached_events, (GFunc) gst_mini_object_unref, NULL);
949       g_list_free (ogm->cached_events);
950       ogm->cached_events = NULL;
951       break;
952     default:
953       break;
954   }
955
956   return ret;
957 }
958
959 gboolean
960 gst_ogm_parse_plugin_init (GstPlugin * plugin)
961 {
962   gst_riff_init ();
963
964   GST_DEBUG_CATEGORY_INIT (gst_ogm_parse_debug, "ogmparse", 0, "ogm parser");
965
966   return gst_element_register (plugin, "ogmaudioparse", GST_RANK_PRIMARY,
967       GST_TYPE_OGM_AUDIO_PARSE) &&
968       gst_element_register (plugin, "ogmvideoparse", GST_RANK_PRIMARY,
969       GST_TYPE_OGM_VIDEO_PARSE) &&
970       gst_element_register (plugin, "ogmtextparse", GST_RANK_PRIMARY,
971       GST_TYPE_OGM_TEXT_PARSE);
972 }