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>
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.
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.
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.
29 #include <gst/tag/tag.h>
30 #include <gst/riff/riff-media.h>
31 #include <gst/riff/riff-read.h>
35 GST_DEBUG_CATEGORY_STATIC (gst_ogm_parse_debug);
36 #define GST_CAT_DEFAULT gst_ogm_parse_debug
38 #define GST_TYPE_OGM_VIDEO_PARSE (gst_ogm_video_parse_get_type())
39 #define GST_IS_OGM_VIDEO_PARSE(obj) \
40 (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_VIDEO_PARSE))
42 #define GST_TYPE_OGM_AUDIO_PARSE (gst_ogm_audio_parse_get_type())
43 #define GST_IS_OGM_AUDIO_PARSE(obj) \
44 (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_AUDIO_PARSE))
46 #define GST_TYPE_OGM_TEXT_PARSE (gst_ogm_text_parse_get_type())
47 #define GST_IS_OGM_TEXT_PARSE(obj) \
48 (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_TEXT_PARSE))
50 #define GST_TYPE_OGM_PARSE (gst_ogm_parse_get_type())
51 #define GST_OGM_PARSE(obj) \
52 (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_OGM_PARSE, GstOgmParse))
53 #define GST_OGM_PARSE_CLASS(klass) \
54 (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_OGM_PARSE, GstOgmParse))
55 #define GST_IS_OGM_PARSE(obj) \
56 (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_PARSE))
57 #define GST_IS_OGM_PARSE_CLASS(klass) \
58 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_OGM_PARSE))
59 #define GST_OGM_PARSE_GET_CLASS(obj) \
60 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_OGM_PARSE, GstOgmParseClass))
62 static const GstElementDetails gst_ogm_audio_parse_details =
63 GST_ELEMENT_DETAILS ("OGM audio stream parser",
64 "Codec/Decoder/Audio",
65 "parse an OGM audio header and stream",
66 "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
68 static const GstElementDetails gst_ogm_video_parse_details =
69 GST_ELEMENT_DETAILS ("OGM video stream parser",
70 "Codec/Decoder/Video",
71 "parse an OGM video header and stream",
72 "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
74 static const GstElementDetails gst_ogm_text_parse_details =
75 GST_ELEMENT_DETAILS ("OGM text stream parser",
76 "Codec/Decoder/Subtitle",
77 "parse an OGM text header and stream",
78 "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
80 typedef struct _stream_header_video
84 } stream_header_video;
86 typedef struct _stream_header_audio
90 gint32 avgbytespersec;
91 } stream_header_audio;
93 /* sizeof(stream_header) might differ due to structure packing and
94 * alignment differences on some architectures, so not using that */
95 #define OGM_STREAM_HEADER_SIZE (8+4+4+8+8+4+4+4+8)
97 typedef struct _stream_header
100 gchar subtype[4 + 1];
102 /* size of the structure */
105 /* in reference time */
108 gint64 samples_per_unit;
114 gint32 bits_per_sample;
118 stream_header_video video;
119 stream_header_audio audio;
120 /* text has no additional data */
124 typedef struct _GstOgmParse
129 GstPad *srcpad, *sinkpad;
130 GstPadTemplate *srcpadtempl;
132 /* we need to cache events that we receive before creating the source pad */
133 GList *cached_events;
138 /* expected next granulepos (used for timestamp guessing) */
139 guint64 next_granulepos;
142 typedef struct _GstOgmParseClass
144 GstElementClass parent_class;
147 static GstStaticPadTemplate sink_factory_video =
148 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
149 GST_STATIC_CAPS ("application/x-ogm-video"));
150 static GstStaticPadTemplate sink_factory_audio =
151 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
152 GST_STATIC_CAPS ("application/x-ogm-audio"));
153 static GstStaticPadTemplate sink_factory_text =
154 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
155 GST_STATIC_CAPS ("application/x-ogm-text"));
156 static GstPadTemplate *video_src_templ, *audio_src_templ, *text_src_templ;
158 static GType gst_ogm_audio_parse_get_type (void);
159 static GType gst_ogm_video_parse_get_type (void);
160 static GType gst_ogm_text_parse_get_type (void);
161 static GType gst_ogm_parse_get_type (void);
163 static void gst_ogm_audio_parse_base_init (GstOgmParseClass * klass);
164 static void gst_ogm_video_parse_base_init (GstOgmParseClass * klass);
165 static void gst_ogm_text_parse_base_init (GstOgmParseClass * klass);
166 static void gst_ogm_parse_class_init (GstOgmParseClass * klass);
167 static void gst_ogm_parse_init (GstOgmParse * ogm);
168 static void gst_ogm_video_parse_init (GstOgmParse * ogm);
169 static void gst_ogm_audio_parse_init (GstOgmParse * ogm);
170 static void gst_ogm_text_parse_init (GstOgmParse * ogm);
172 static const GstQueryType *gst_ogm_parse_get_sink_querytypes (GstPad * pad);
173 static gboolean gst_ogm_parse_sink_event (GstPad * pad, GstEvent * event);
174 static gboolean gst_ogm_parse_sink_query (GstPad * pad, GstQuery * query);
175 static gboolean gst_ogm_parse_sink_convert (GstPad * pad, GstFormat src_format,
176 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
178 static GstFlowReturn gst_ogm_parse_chain (GstPad * pad, GstBuffer * buffer);
180 static GstStateChangeReturn gst_ogm_parse_change_state (GstElement * element,
181 GstStateChange transition);
183 static GstElementClass *parent_class = NULL;
186 gst_ogm_parse_get_type (void)
188 static GType ogm_parse_type = 0;
190 if (!ogm_parse_type) {
191 static const GTypeInfo ogm_parse_info = {
192 sizeof (GstOgmParseClass),
195 (GClassInitFunc) gst_ogm_parse_class_init,
198 sizeof (GstOgmParse),
200 (GInstanceInitFunc) gst_ogm_parse_init,
204 g_type_register_static (GST_TYPE_ELEMENT,
205 "GstOgmParse", &ogm_parse_info, 0);
208 return ogm_parse_type;
212 gst_ogm_audio_parse_get_type (void)
214 static GType ogm_audio_parse_type = 0;
216 if (!ogm_audio_parse_type) {
217 static const GTypeInfo ogm_audio_parse_info = {
218 sizeof (GstOgmParseClass),
219 (GBaseInitFunc) gst_ogm_audio_parse_base_init,
224 sizeof (GstOgmParse),
226 (GInstanceInitFunc) gst_ogm_audio_parse_init,
229 ogm_audio_parse_type =
230 g_type_register_static (GST_TYPE_OGM_PARSE,
231 "GstOgmAudioParse", &ogm_audio_parse_info, 0);
234 return ogm_audio_parse_type;
238 gst_ogm_video_parse_get_type (void)
240 static GType ogm_video_parse_type = 0;
242 if (!ogm_video_parse_type) {
243 static const GTypeInfo ogm_video_parse_info = {
244 sizeof (GstOgmParseClass),
245 (GBaseInitFunc) gst_ogm_video_parse_base_init,
250 sizeof (GstOgmParse),
252 (GInstanceInitFunc) gst_ogm_video_parse_init,
255 ogm_video_parse_type =
256 g_type_register_static (GST_TYPE_OGM_PARSE,
257 "GstOgmVideoParse", &ogm_video_parse_info, 0);
260 return ogm_video_parse_type;
264 gst_ogm_text_parse_get_type (void)
266 static GType ogm_text_parse_type = 0;
268 if (!ogm_text_parse_type) {
269 static const GTypeInfo ogm_text_parse_info = {
270 sizeof (GstOgmParseClass),
271 (GBaseInitFunc) gst_ogm_text_parse_base_init,
276 sizeof (GstOgmParse),
278 (GInstanceInitFunc) gst_ogm_text_parse_init,
281 ogm_text_parse_type =
282 g_type_register_static (GST_TYPE_OGM_PARSE,
283 "GstOgmTextParse", &ogm_text_parse_info, 0);
286 return ogm_text_parse_type;
290 gst_ogm_audio_parse_base_init (GstOgmParseClass * klass)
292 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
293 GstCaps *caps = gst_riff_create_audio_template_caps ();
295 gst_element_class_set_details (element_class, &gst_ogm_audio_parse_details);
297 gst_element_class_add_pad_template (element_class,
298 gst_static_pad_template_get (&sink_factory_audio));
299 audio_src_templ = gst_pad_template_new ("src",
300 GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
301 gst_element_class_add_pad_template (element_class, audio_src_templ);
305 gst_ogm_video_parse_base_init (GstOgmParseClass * klass)
307 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
308 GstCaps *caps = gst_riff_create_video_template_caps ();
310 gst_element_class_set_details (element_class, &gst_ogm_video_parse_details);
312 gst_element_class_add_pad_template (element_class,
313 gst_static_pad_template_get (&sink_factory_video));
314 video_src_templ = gst_pad_template_new ("src",
315 GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
316 gst_element_class_add_pad_template (element_class, video_src_templ);
320 gst_ogm_text_parse_base_init (GstOgmParseClass * klass)
322 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
323 GstCaps *caps = gst_caps_new_simple ("text/plain", NULL, NULL);
325 gst_element_class_set_details (element_class, &gst_ogm_text_parse_details);
327 gst_element_class_add_pad_template (element_class,
328 gst_static_pad_template_get (&sink_factory_text));
329 text_src_templ = gst_pad_template_new ("src",
330 GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
331 gst_element_class_add_pad_template (element_class, text_src_templ);
335 gst_ogm_parse_class_init (GstOgmParseClass * klass)
337 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
339 parent_class = g_type_class_peek_parent (klass);
341 gstelement_class->change_state =
342 GST_DEBUG_FUNCPTR (gst_ogm_parse_change_state);
346 gst_ogm_parse_init (GstOgmParse * ogm)
348 memset (&ogm->hdr, 0, sizeof (ogm->hdr));
349 ogm->next_granulepos = 0;
351 ogm->cached_events = NULL;
355 gst_ogm_audio_parse_init (GstOgmParse * ogm)
357 ogm->sinkpad = gst_pad_new_from_static_template (&sink_factory_audio, "sink");
358 gst_pad_set_query_function (ogm->sinkpad,
359 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_query));
360 gst_pad_set_chain_function (ogm->sinkpad,
361 GST_DEBUG_FUNCPTR (gst_ogm_parse_chain));
362 gst_pad_set_event_function (ogm->sinkpad,
363 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_event));
364 gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);
367 ogm->srcpadtempl = audio_src_templ;
371 gst_ogm_video_parse_init (GstOgmParse * ogm)
373 ogm->sinkpad = gst_pad_new_from_static_template (&sink_factory_video, "sink");
374 gst_pad_set_query_function (ogm->sinkpad,
375 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_query));
376 gst_pad_set_chain_function (ogm->sinkpad,
377 GST_DEBUG_FUNCPTR (gst_ogm_parse_chain));
378 gst_pad_set_event_function (ogm->sinkpad,
379 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_event));
380 gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);
383 ogm->srcpadtempl = video_src_templ;
387 gst_ogm_text_parse_init (GstOgmParse * ogm)
389 ogm->sinkpad = gst_pad_new_from_static_template (&sink_factory_text, "sink");
390 gst_pad_set_query_type_function (ogm->sinkpad,
391 gst_ogm_parse_get_sink_querytypes);
392 gst_pad_set_query_function (ogm->sinkpad,
393 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_query));
394 gst_pad_set_chain_function (ogm->sinkpad,
395 GST_DEBUG_FUNCPTR (gst_ogm_parse_chain));
396 gst_pad_set_event_function (ogm->sinkpad,
397 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_event));
398 gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);
401 ogm->srcpadtempl = text_src_templ;
404 static const GstQueryType *
405 gst_ogm_parse_get_sink_querytypes (GstPad * pad)
407 static const GstQueryType types[] = {
416 gst_ogm_parse_sink_convert (GstPad * pad,
417 GstFormat src_format, gint64 src_value,
418 GstFormat * dest_format, gint64 * dest_value)
420 gboolean res = FALSE;
421 GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));
423 switch (src_format) {
424 case GST_FORMAT_DEFAULT:
425 switch (*dest_format) {
426 case GST_FORMAT_TIME:
427 switch (ogm->hdr.streamtype[0]) {
429 *dest_value = GST_SECOND * src_value / ogm->hdr.samples_per_unit;
434 *dest_value = (GST_SECOND / 10000000) *
435 ogm->hdr.time_unit * src_value;
446 case GST_FORMAT_TIME:
447 switch (*dest_format) {
448 case GST_FORMAT_DEFAULT:
449 switch (ogm->hdr.streamtype[0]) {
451 *dest_value = ogm->hdr.samples_per_unit * src_value / GST_SECOND;
456 *dest_value = src_value /
457 ((GST_SECOND / 10000000) * ogm->hdr.time_unit);
472 gst_object_unref (ogm);
477 gst_ogm_parse_sink_query (GstPad * pad, GstQuery * query)
479 GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));
481 gboolean res = FALSE;
483 switch (GST_QUERY_TYPE (query)) {
484 case GST_QUERY_POSITION:
488 gst_query_parse_position (query, &format, NULL);
490 if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME)
493 if ((res = gst_ogm_parse_sink_convert (pad,
494 GST_FORMAT_DEFAULT, ogm->next_granulepos, &format, &val))) {
495 /* don't know the total length here.. */
496 gst_query_set_position (query, format, val);
500 case GST_QUERY_CONVERT:
502 GstFormat src_fmt, dest_fmt;
503 gint64 src_val, dest_val;
506 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
507 if ((res = gst_ogm_parse_sink_convert (pad, src_fmt, src_val,
508 &dest_fmt, &dest_val))) {
509 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
514 res = gst_pad_query_default (pad, query);
518 gst_object_unref (ogm);
523 gst_ogm_parse_stream_header (GstOgmParse * ogm, const guint8 * data, guint size)
525 GstCaps *caps = NULL;
528 if (size < OGM_STREAM_HEADER_SIZE)
529 goto buffer_too_small;
531 if (!memcmp (data, "video\000\000\000", 8)) {
532 ogm->hdr.s.video.width = GST_READ_UINT32_LE (&data[44]);
533 ogm->hdr.s.video.height = GST_READ_UINT32_LE (&data[48]);
534 } else if (!memcmp (data, "audio\000\000\000", 8)) {
535 ogm->hdr.s.audio.channels = GST_READ_UINT32_LE (&data[44]);
536 ogm->hdr.s.audio.blockalign = GST_READ_UINT32_LE (&data[46]);
537 ogm->hdr.s.audio.avgbytespersec = GST_READ_UINT32_LE (&data[48]);
538 } else if (!memcmp (data, "text\000\000\000\000", 8)) {
543 memcpy (ogm->hdr.streamtype, &data[0], 8);
544 memcpy (ogm->hdr.subtype, &data[8], 4);
545 ogm->hdr.subtype[4] = '\0';
546 ogm->hdr.size = GST_READ_UINT32_LE (&data[12]);
547 ogm->hdr.time_unit = GST_READ_UINT64_LE (&data[16]);
548 ogm->hdr.samples_per_unit = GST_READ_UINT64_LE (&data[24]);
549 ogm->hdr.default_len = GST_READ_UINT32_LE (&data[32]);
550 ogm->hdr.buffersize = GST_READ_UINT32_LE (&data[36]);
551 ogm->hdr.bits_per_sample = GST_READ_UINT32_LE (&data[40]);
553 switch (ogm->hdr.streamtype[0]) {
557 if (sscanf (ogm->hdr.subtype, "%04x", &codec_id) != 1) {
558 GST_WARNING_OBJECT (ogm, "cannot parse subtype %s", ogm->hdr.subtype);
562 gst_riff_create_audio_caps (codec_id, NULL, NULL, NULL, NULL, NULL);
565 GST_WARNING_OBJECT (ogm, "no audio caps for codec %u found", codec_id);
566 caps = gst_caps_new_simple ("audio/x-ogm-unknown", "codec_id",
567 G_TYPE_INT, (gint) codec_id, NULL);
570 gst_caps_set_simple (caps,
571 "channels", G_TYPE_INT, ogm->hdr.s.audio.channels,
572 "rate", G_TYPE_INT, ogm->hdr.samples_per_unit, NULL);
574 GST_LOG_OBJECT (ogm, "Type: %s, subtype: 0x%04x, channels: %d, "
575 "samplerate: %d, blockalign: %d, bps: %d, caps = %" GST_PTR_FORMAT,
576 ogm->hdr.streamtype, codec_id, ogm->hdr.s.audio.channels,
577 (gint) ogm->hdr.samples_per_unit, ogm->hdr.s.audio.blockalign,
578 ogm->hdr.s.audio.avgbytespersec, caps);
585 fourcc = GST_MAKE_FOURCC (ogm->hdr.subtype[0],
586 ogm->hdr.subtype[1], ogm->hdr.subtype[2], ogm->hdr.subtype[3]);
588 caps = gst_riff_create_video_caps (fourcc, NULL, NULL, NULL, NULL, NULL);
591 GST_WARNING_OBJECT (ogm, "could not find video caps for fourcc %"
592 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
593 caps = gst_caps_new_simple ("video/x-ogm-unknown", "fourcc",
594 GST_TYPE_FOURCC, fourcc, NULL);
598 GST_LOG_OBJECT (ogm, "Type: %s, subtype: %" GST_FOURCC_FORMAT
599 ", size: %dx%d, timeunit: %" G_GINT64_FORMAT
600 " (fps: %lf), s/u: %" G_GINT64_FORMAT ", "
601 "def.len: %d, bufsize: %d, bps: %d, caps = %" GST_PTR_FORMAT,
602 ogm->hdr.streamtype, GST_FOURCC_ARGS (fourcc),
603 ogm->hdr.s.video.width, ogm->hdr.s.video.height,
604 ogm->hdr.time_unit, 10000000. / ogm->hdr.time_unit,
605 ogm->hdr.samples_per_unit, ogm->hdr.default_len,
606 ogm->hdr.buffersize, ogm->hdr.bits_per_sample, caps);
608 /* GST_TYPE_FRACTION contains gint */
609 if (ogm->hdr.time_unit > G_MAXINT || ogm->hdr.time_unit < G_MININT)
610 GST_WARNING_OBJECT (ogm, "timeunit is out of range");
612 time_unit = (gint) CLAMP (ogm->hdr.time_unit, G_MININT, G_MAXINT);
613 gst_caps_set_simple (caps,
614 "width", G_TYPE_INT, ogm->hdr.s.video.width,
615 "height", G_TYPE_INT, ogm->hdr.s.video.height,
616 "framerate", GST_TYPE_FRACTION, 10000000, time_unit, NULL);
620 GST_LOG_OBJECT (ogm, "Type: %s, s/u: %" G_GINT64_FORMAT
621 ", timeunit=%" G_GINT64_FORMAT,
622 ogm->hdr.streamtype, ogm->hdr.samples_per_unit, ogm->hdr.time_unit);
623 caps = gst_caps_new_simple ("text/plain", NULL);
627 g_assert_not_reached ();
634 GstCaps *current_caps = GST_PAD_CAPS (ogm->srcpad);
636 if (current_caps && caps && !gst_caps_is_equal (current_caps, caps)) {
637 GST_WARNING_OBJECT (ogm, "Already an existing pad %s:%s",
638 GST_DEBUG_PAD_NAME (ogm->srcpad));
639 gst_pad_set_active (ogm->srcpad, FALSE);
640 gst_element_remove_pad (GST_ELEMENT (ogm), ogm->srcpad);
643 GST_DEBUG_OBJECT (ogm, "Existing pad has the same caps, do nothing");
647 if (ogm->srcpad == NULL) {
648 GList *l, *cached_events;
650 ogm->srcpad = gst_pad_new_from_template (ogm->srcpadtempl, "src");
651 gst_pad_use_fixed_caps (ogm->srcpad);
652 gst_pad_set_caps (ogm->srcpad, caps);
653 gst_pad_set_active (ogm->srcpad, TRUE);
654 gst_element_add_pad (GST_ELEMENT (ogm), ogm->srcpad);
655 GST_INFO_OBJECT (ogm, "Added pad %s:%s with caps %" GST_PTR_FORMAT,
656 GST_DEBUG_PAD_NAME (ogm->srcpad), caps);
658 GST_OBJECT_LOCK (ogm);
659 cached_events = ogm->cached_events;
660 ogm->cached_events = NULL;
661 GST_OBJECT_UNLOCK (ogm);
663 for (l = cached_events; l; l = l->next) {
664 GstEvent *event = GST_EVENT_CAST (l->data);
666 GST_DEBUG_OBJECT (ogm, "Pushing cached event %" GST_PTR_FORMAT, event);
667 gst_pad_push_event (ogm->srcpad, event);
669 g_list_free (cached_events);
674 tags = gst_tag_list_new ();
675 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_SUBTITLE_CODEC,
677 gst_element_found_tags_for_pad (GST_ELEMENT (ogm), ogm->srcpad, tags);
681 gst_caps_unref (caps);
688 GST_ELEMENT_ERROR (ogm, STREAM, WRONG_TYPE, ("Buffer too small"), (NULL));
689 return GST_FLOW_ERROR;
693 GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL), ("unknown ogm format"));
694 return GST_FLOW_ERROR;
699 gst_ogm_parse_comment_packet (GstOgmParse * ogm, GstBuffer * buf)
703 if (ogm->srcpad == NULL) {
704 GST_DEBUG ("no source pad");
705 return GST_FLOW_WRONG_STATE;
708 /* if this is not a subtitle stream, push the vorbiscomment packet
709 * on downstream, the respective decoder will handle it; if it is
710 * a subtitle stream, we will have to handle the comment ourself */
711 if (ogm->hdr.streamtype[0] == 't') {
714 tags = gst_tag_list_from_vorbiscomment_buffer (buf,
715 (guint8 *) "\003vorbis", 7, NULL);
718 GST_DEBUG_OBJECT (ogm, "tags = %" GST_PTR_FORMAT, tags);
719 gst_element_found_tags_for_pad (GST_ELEMENT (ogm), ogm->srcpad, tags);
721 GST_DEBUG_OBJECT (ogm, "failed to extract tags from vorbis comment");
723 /* do not push packet downstream, just let parent unref it */
726 buf = gst_buffer_copy (buf);
727 gst_buffer_set_caps (buf, GST_PAD_CAPS (ogm->srcpad));
728 ret = gst_pad_push (ogm->srcpad, buf);
735 gst_ogm_text_parse_strip_trailing_zeroes (GstOgmParse * ogm, GstBuffer * buf)
740 g_assert (gst_buffer_is_metadata_writable (buf));
742 /* zeroes are not valid UTF-8 characters, so strip them from output */
743 data = GST_BUFFER_DATA (buf);
744 size = GST_BUFFER_SIZE (buf);
745 while (size > 0 && data[size - 1] == '\0') {
749 GST_BUFFER_SIZE (buf) = size;
753 gst_ogm_parse_data_packet (GstOgmParse * ogm, GstBuffer * buf)
759 guint size, len, n, xsize = 0;
761 data = GST_BUFFER_DATA (buf);
762 size = GST_BUFFER_SIZE (buf);
764 if ((data[0] & 0x01) != 0)
765 goto invalid_startcode;
768 len = ((data[0] & 0xc0) >> 6) | ((data[0] & 0x02) << 1);
769 keyframe = (((data[0] & 0x08) >> 3) != 0);
771 if ((1 + len) > size)
772 goto buffer_too_small;
774 for (n = len; n > 0; n--) {
775 xsize = (xsize << 8) | data[n];
778 GST_LOG_OBJECT (ogm, "[0x%02x] samples: %d, hdrbytes: %d, datasize: %d",
779 data[0], xsize, len, size - len - 1);
781 sbuf = gst_buffer_create_sub (buf, len + 1, size - len - 1);
783 if (GST_BUFFER_OFFSET_END_IS_VALID (buf))
784 ogm->next_granulepos = GST_BUFFER_OFFSET_END (buf);
786 switch (ogm->hdr.streamtype[0]) {
789 GstClockTime ts, next_ts;
792 samples = (ogm->hdr.streamtype[0] == 'v') ? 1 : xsize;
795 GST_BUFFER_FLAG_SET (sbuf, GST_BUFFER_FLAG_DELTA_UNIT);
798 /* shouldn't this be granulepos - samples? (tpm) */
799 ts = gst_util_uint64_scale (ogm->next_granulepos,
800 ogm->hdr.time_unit * GST_SECOND, 10000000);
801 next_ts = gst_util_uint64_scale (ogm->next_granulepos + samples,
802 ogm->hdr.time_unit * GST_SECOND, 10000000);
804 GST_BUFFER_TIMESTAMP (sbuf) = ts;
805 GST_BUFFER_DURATION (sbuf) = next_ts - ts;
807 ogm->next_granulepos += samples;
809 if (ogm->hdr.streamtype[0] == 't') {
810 gst_ogm_text_parse_strip_trailing_zeroes (ogm, sbuf);
815 GstClockTime ts, next_ts;
817 /* shouldn't this be granulepos - samples? (tpm) */
818 ts = gst_util_uint64_scale_int (ogm->next_granulepos,
819 GST_SECOND, ogm->hdr.samples_per_unit);
820 next_ts = gst_util_uint64_scale_int (ogm->next_granulepos + xsize,
821 GST_SECOND, ogm->hdr.samples_per_unit);
823 GST_BUFFER_TIMESTAMP (sbuf) = ts;
824 GST_BUFFER_DURATION (sbuf) = next_ts - ts;
826 ogm->next_granulepos += xsize;
830 g_assert_not_reached ();
835 gst_buffer_set_caps (sbuf, GST_PAD_CAPS (ogm->srcpad));
836 GST_LOG_OBJECT (ogm, "Pushing buffer with ts=%" GST_TIME_FORMAT,
837 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (sbuf)));
838 ret = gst_pad_push (ogm->srcpad, sbuf);
839 if (ret != GST_FLOW_OK) {
840 GST_DEBUG_OBJECT (ogm, "Flow on %s:%s = %s",
841 GST_DEBUG_PAD_NAME (ogm->srcpad), gst_flow_get_name (ret));
844 ret = GST_FLOW_WRONG_STATE;
852 GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL),
853 ("unexpected packet startcode 0x%02x", data[0]));
854 return GST_FLOW_ERROR;
858 GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL),
859 ("buffer too small, len+1=%u, size=%u", len + 1, size));
860 return GST_FLOW_ERROR;
865 gst_ogm_parse_chain (GstPad * pad, GstBuffer * buf)
867 GstFlowReturn ret = GST_FLOW_OK;
868 GstOgmParse *ogm = GST_OGM_PARSE (GST_PAD_PARENT (pad));
869 guint8 *data = GST_BUFFER_DATA (buf);
870 guint size = GST_BUFFER_SIZE (buf);
873 goto buffer_too_small;
875 GST_LOG_OBJECT (ogm, "Packet with start code 0x%02x", data[0]);
879 ret = gst_ogm_parse_stream_header (ogm, data + 1, size - 1);
883 ret = gst_ogm_parse_comment_packet (ogm, buf);
887 ret = gst_ogm_parse_data_packet (ogm, buf);
892 gst_buffer_unref (buf);
894 if (ret != GST_FLOW_OK) {
895 GST_DEBUG_OBJECT (ogm, "Flow: %s", gst_flow_get_name (ret));
903 GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL), ("buffer too small"));
904 gst_buffer_unref (buf);
905 return GST_FLOW_ERROR;
910 gst_ogm_parse_sink_event (GstPad * pad, GstEvent * event)
912 GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));
915 GST_LOG_OBJECT (ogm, "processing %s event", GST_EVENT_TYPE_NAME (event));
917 GST_OBJECT_LOCK (ogm);
918 if (ogm->srcpad == NULL) {
919 ogm->cached_events = g_list_append (ogm->cached_events, event);
920 GST_OBJECT_UNLOCK (ogm);
923 GST_OBJECT_UNLOCK (ogm);
924 res = gst_pad_event_default (pad, event);
927 gst_object_unref (ogm);
931 static GstStateChangeReturn
932 gst_ogm_parse_change_state (GstElement * element, GstStateChange transition)
934 GstStateChangeReturn ret;
935 GstOgmParse *ogm = GST_OGM_PARSE (element);
937 ret = parent_class->change_state (element, transition);
938 if (ret != GST_STATE_CHANGE_SUCCESS)
941 switch (transition) {
942 case GST_STATE_CHANGE_PAUSED_TO_READY:
944 gst_pad_set_active (ogm->srcpad, FALSE);
945 gst_element_remove_pad (element, ogm->srcpad);
948 memset (&ogm->hdr, 0, sizeof (ogm->hdr));
949 ogm->next_granulepos = 0;
950 g_list_foreach (ogm->cached_events, (GFunc) gst_mini_object_unref, NULL);
951 g_list_free (ogm->cached_events);
952 ogm->cached_events = NULL;
962 gst_ogm_parse_plugin_init (GstPlugin * plugin)
966 GST_DEBUG_CATEGORY_INIT (gst_ogm_parse_debug, "ogmparse", 0, "ogm parser");
968 return gst_element_register (plugin, "ogmaudioparse", GST_RANK_PRIMARY,
969 GST_TYPE_OGM_AUDIO_PARSE) &&
970 gst_element_register (plugin, "ogmvideoparse", GST_RANK_PRIMARY,
971 GST_TYPE_OGM_VIDEO_PARSE) &&
972 gst_element_register (plugin, "ogmtextparse", GST_RANK_PRIMARY,
973 GST_TYPE_OGM_TEXT_PARSE);