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 typedef struct _stream_header_video
66 } stream_header_video;
68 typedef struct _stream_header_audio
72 gint32 avgbytespersec;
73 } stream_header_audio;
75 /* sizeof(stream_header) might differ due to structure packing and
76 * alignment differences on some architectures, so not using that */
77 #define OGM_STREAM_HEADER_SIZE (8+4+4+8+8+4+4+4+8)
79 typedef struct _stream_header
84 /* size of the structure */
87 /* in reference time */
90 gint64 samples_per_unit;
96 gint32 bits_per_sample;
100 stream_header_video video;
101 stream_header_audio audio;
102 /* text has no additional data */
106 typedef struct _GstOgmParse
111 GstPad *srcpad, *sinkpad;
112 GstPadTemplate *srcpadtempl;
114 /* we need to cache events that we receive before creating the source pad */
115 GList *cached_events;
120 /* expected next granulepos (used for timestamp guessing) */
121 guint64 next_granulepos;
124 typedef struct _GstOgmParseClass
126 GstElementClass parent_class;
129 static GstStaticPadTemplate sink_factory_video =
130 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
131 GST_STATIC_CAPS ("application/x-ogm-video"));
132 static GstStaticPadTemplate sink_factory_audio =
133 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
134 GST_STATIC_CAPS ("application/x-ogm-audio"));
135 static GstStaticPadTemplate sink_factory_text =
136 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
137 GST_STATIC_CAPS ("application/x-ogm-text"));
138 static GstPadTemplate *video_src_templ, *audio_src_templ, *text_src_templ;
140 static GType gst_ogm_audio_parse_get_type (void);
141 static GType gst_ogm_video_parse_get_type (void);
142 static GType gst_ogm_text_parse_get_type (void);
143 static GType gst_ogm_parse_get_type (void);
145 static void gst_ogm_audio_parse_base_init (GstOgmParseClass * klass);
146 static void gst_ogm_video_parse_base_init (GstOgmParseClass * klass);
147 static void gst_ogm_text_parse_base_init (GstOgmParseClass * klass);
148 static void gst_ogm_parse_class_init (GstOgmParseClass * klass);
149 static void gst_ogm_parse_init (GstOgmParse * ogm);
150 static void gst_ogm_video_parse_init (GstOgmParse * ogm);
151 static void gst_ogm_audio_parse_init (GstOgmParse * ogm);
152 static void gst_ogm_text_parse_init (GstOgmParse * ogm);
154 static const GstQueryType *gst_ogm_parse_get_sink_querytypes (GstPad * pad);
155 static gboolean gst_ogm_parse_sink_event (GstPad * pad, GstEvent * event);
156 static gboolean gst_ogm_parse_sink_query (GstPad * pad, GstQuery * query);
157 static gboolean gst_ogm_parse_sink_convert (GstPad * pad, GstFormat src_format,
158 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
160 static GstFlowReturn gst_ogm_parse_chain (GstPad * pad, GstBuffer * buffer);
162 static GstStateChangeReturn gst_ogm_parse_change_state (GstElement * element,
163 GstStateChange transition);
165 static GstElementClass *parent_class = NULL;
168 gst_ogm_parse_get_type (void)
170 static GType ogm_parse_type = 0;
172 if (!ogm_parse_type) {
173 static const GTypeInfo ogm_parse_info = {
174 sizeof (GstOgmParseClass),
177 (GClassInitFunc) gst_ogm_parse_class_init,
180 sizeof (GstOgmParse),
182 (GInstanceInitFunc) gst_ogm_parse_init,
186 g_type_register_static (GST_TYPE_ELEMENT,
187 "GstOgmParse", &ogm_parse_info, 0);
190 return ogm_parse_type;
194 gst_ogm_audio_parse_get_type (void)
196 static GType ogm_audio_parse_type = 0;
198 if (!ogm_audio_parse_type) {
199 static const GTypeInfo ogm_audio_parse_info = {
200 sizeof (GstOgmParseClass),
201 (GBaseInitFunc) gst_ogm_audio_parse_base_init,
206 sizeof (GstOgmParse),
208 (GInstanceInitFunc) gst_ogm_audio_parse_init,
211 ogm_audio_parse_type =
212 g_type_register_static (GST_TYPE_OGM_PARSE,
213 "GstOgmAudioParse", &ogm_audio_parse_info, 0);
216 return ogm_audio_parse_type;
220 gst_ogm_video_parse_get_type (void)
222 static GType ogm_video_parse_type = 0;
224 if (!ogm_video_parse_type) {
225 static const GTypeInfo ogm_video_parse_info = {
226 sizeof (GstOgmParseClass),
227 (GBaseInitFunc) gst_ogm_video_parse_base_init,
232 sizeof (GstOgmParse),
234 (GInstanceInitFunc) gst_ogm_video_parse_init,
237 ogm_video_parse_type =
238 g_type_register_static (GST_TYPE_OGM_PARSE,
239 "GstOgmVideoParse", &ogm_video_parse_info, 0);
242 return ogm_video_parse_type;
246 gst_ogm_text_parse_get_type (void)
248 static GType ogm_text_parse_type = 0;
250 if (!ogm_text_parse_type) {
251 static const GTypeInfo ogm_text_parse_info = {
252 sizeof (GstOgmParseClass),
253 (GBaseInitFunc) gst_ogm_text_parse_base_init,
258 sizeof (GstOgmParse),
260 (GInstanceInitFunc) gst_ogm_text_parse_init,
263 ogm_text_parse_type =
264 g_type_register_static (GST_TYPE_OGM_PARSE,
265 "GstOgmTextParse", &ogm_text_parse_info, 0);
268 return ogm_text_parse_type;
272 gst_ogm_audio_parse_base_init (GstOgmParseClass * klass)
274 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
275 GstCaps *caps = gst_riff_create_audio_template_caps ();
277 gst_element_class_set_details_simple (element_class,
278 "OGM audio stream parser", "Codec/Decoder/Audio",
279 "parse an OGM audio header and stream",
280 "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
282 gst_element_class_add_pad_template (element_class,
283 gst_static_pad_template_get (&sink_factory_audio));
284 audio_src_templ = gst_pad_template_new ("src",
285 GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
286 gst_element_class_add_pad_template (element_class, audio_src_templ);
290 gst_ogm_video_parse_base_init (GstOgmParseClass * klass)
292 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
293 GstCaps *caps = gst_riff_create_video_template_caps ();
295 gst_element_class_set_details_simple (element_class,
296 "OGM video stream parser", "Codec/Decoder/Video",
297 "parse an OGM video header and stream",
298 "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
300 gst_element_class_add_pad_template (element_class,
301 gst_static_pad_template_get (&sink_factory_video));
302 video_src_templ = gst_pad_template_new ("src",
303 GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
304 gst_element_class_add_pad_template (element_class, video_src_templ);
308 gst_ogm_text_parse_base_init (GstOgmParseClass * klass)
310 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
311 GstCaps *caps = gst_caps_new_simple ("text/plain", NULL, NULL);
313 gst_element_class_set_details_simple (element_class,
314 "OGM text stream parser", "Codec/Decoder/Subtitle",
315 "parse an OGM text header and stream",
316 "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
318 gst_element_class_add_pad_template (element_class,
319 gst_static_pad_template_get (&sink_factory_text));
320 text_src_templ = gst_pad_template_new ("src",
321 GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
322 gst_element_class_add_pad_template (element_class, text_src_templ);
326 gst_ogm_parse_class_init (GstOgmParseClass * klass)
328 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
330 parent_class = g_type_class_peek_parent (klass);
332 gstelement_class->change_state =
333 GST_DEBUG_FUNCPTR (gst_ogm_parse_change_state);
337 gst_ogm_parse_init (GstOgmParse * ogm)
339 memset (&ogm->hdr, 0, sizeof (ogm->hdr));
340 ogm->next_granulepos = 0;
342 ogm->cached_events = NULL;
346 gst_ogm_audio_parse_init (GstOgmParse * ogm)
348 ogm->sinkpad = gst_pad_new_from_static_template (&sink_factory_audio, "sink");
349 gst_pad_set_query_function (ogm->sinkpad,
350 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_query));
351 gst_pad_set_chain_function (ogm->sinkpad,
352 GST_DEBUG_FUNCPTR (gst_ogm_parse_chain));
353 gst_pad_set_event_function (ogm->sinkpad,
354 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_event));
355 gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);
358 ogm->srcpadtempl = audio_src_templ;
362 gst_ogm_video_parse_init (GstOgmParse * ogm)
364 ogm->sinkpad = gst_pad_new_from_static_template (&sink_factory_video, "sink");
365 gst_pad_set_query_function (ogm->sinkpad,
366 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_query));
367 gst_pad_set_chain_function (ogm->sinkpad,
368 GST_DEBUG_FUNCPTR (gst_ogm_parse_chain));
369 gst_pad_set_event_function (ogm->sinkpad,
370 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_event));
371 gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);
374 ogm->srcpadtempl = video_src_templ;
378 gst_ogm_text_parse_init (GstOgmParse * ogm)
380 ogm->sinkpad = gst_pad_new_from_static_template (&sink_factory_text, "sink");
381 gst_pad_set_query_type_function (ogm->sinkpad,
382 gst_ogm_parse_get_sink_querytypes);
383 gst_pad_set_query_function (ogm->sinkpad,
384 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_query));
385 gst_pad_set_chain_function (ogm->sinkpad,
386 GST_DEBUG_FUNCPTR (gst_ogm_parse_chain));
387 gst_pad_set_event_function (ogm->sinkpad,
388 GST_DEBUG_FUNCPTR (gst_ogm_parse_sink_event));
389 gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);
392 ogm->srcpadtempl = text_src_templ;
395 static const GstQueryType *
396 gst_ogm_parse_get_sink_querytypes (GstPad * pad)
398 static const GstQueryType types[] = {
407 gst_ogm_parse_sink_convert (GstPad * pad,
408 GstFormat src_format, gint64 src_value,
409 GstFormat * dest_format, gint64 * dest_value)
411 gboolean res = FALSE;
412 GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));
414 switch (src_format) {
415 case GST_FORMAT_DEFAULT:
416 switch (*dest_format) {
417 case GST_FORMAT_TIME:
418 switch (ogm->hdr.streamtype[0]) {
420 *dest_value = GST_SECOND * src_value / ogm->hdr.samples_per_unit;
425 *dest_value = (GST_SECOND / 10000000) *
426 ogm->hdr.time_unit * src_value;
437 case GST_FORMAT_TIME:
438 switch (*dest_format) {
439 case GST_FORMAT_DEFAULT:
440 switch (ogm->hdr.streamtype[0]) {
442 *dest_value = ogm->hdr.samples_per_unit * src_value / GST_SECOND;
447 *dest_value = src_value /
448 ((GST_SECOND / 10000000) * ogm->hdr.time_unit);
463 gst_object_unref (ogm);
468 gst_ogm_parse_sink_query (GstPad * pad, GstQuery * query)
470 GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));
472 gboolean res = FALSE;
474 switch (GST_QUERY_TYPE (query)) {
475 case GST_QUERY_POSITION:
479 gst_query_parse_position (query, &format, NULL);
481 if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME)
484 if ((res = gst_ogm_parse_sink_convert (pad,
485 GST_FORMAT_DEFAULT, ogm->next_granulepos, &format, &val))) {
486 /* don't know the total length here.. */
487 gst_query_set_position (query, format, val);
491 case GST_QUERY_CONVERT:
493 GstFormat src_fmt, dest_fmt;
494 gint64 src_val, dest_val;
497 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
498 if ((res = gst_ogm_parse_sink_convert (pad, src_fmt, src_val,
499 &dest_fmt, &dest_val))) {
500 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
505 res = gst_pad_query_default (pad, query);
509 gst_object_unref (ogm);
514 gst_ogm_parse_stream_header (GstOgmParse * ogm, const guint8 * data, guint size)
516 GstCaps *caps = NULL;
519 if (size < OGM_STREAM_HEADER_SIZE)
520 goto buffer_too_small;
522 if (!memcmp (data, "video\000\000\000", 8)) {
523 ogm->hdr.s.video.width = GST_READ_UINT32_LE (&data[44]);
524 ogm->hdr.s.video.height = GST_READ_UINT32_LE (&data[48]);
525 } else if (!memcmp (data, "audio\000\000\000", 8)) {
526 ogm->hdr.s.audio.channels = GST_READ_UINT32_LE (&data[44]);
527 ogm->hdr.s.audio.blockalign = GST_READ_UINT32_LE (&data[46]);
528 ogm->hdr.s.audio.avgbytespersec = GST_READ_UINT32_LE (&data[48]);
529 } else if (!memcmp (data, "text\000\000\000\000", 8)) {
534 memcpy (ogm->hdr.streamtype, &data[0], 8);
535 memcpy (ogm->hdr.subtype, &data[8], 4);
536 ogm->hdr.subtype[4] = '\0';
537 ogm->hdr.size = GST_READ_UINT32_LE (&data[12]);
538 ogm->hdr.time_unit = GST_READ_UINT64_LE (&data[16]);
539 ogm->hdr.samples_per_unit = GST_READ_UINT64_LE (&data[24]);
540 ogm->hdr.default_len = GST_READ_UINT32_LE (&data[32]);
541 ogm->hdr.buffersize = GST_READ_UINT32_LE (&data[36]);
542 ogm->hdr.bits_per_sample = GST_READ_UINT32_LE (&data[40]);
544 switch (ogm->hdr.streamtype[0]) {
548 if (sscanf (ogm->hdr.subtype, "%04x", &codec_id) != 1) {
549 GST_WARNING_OBJECT (ogm, "cannot parse subtype %s", ogm->hdr.subtype);
553 gst_riff_create_audio_caps (codec_id, NULL, NULL, NULL, NULL, NULL);
556 GST_WARNING_OBJECT (ogm, "no audio caps for codec %u found", codec_id);
557 caps = gst_caps_new_simple ("audio/x-ogm-unknown", "codec_id",
558 G_TYPE_INT, (gint) codec_id, NULL);
561 gst_caps_set_simple (caps,
562 "channels", G_TYPE_INT, ogm->hdr.s.audio.channels,
563 "rate", G_TYPE_INT, ogm->hdr.samples_per_unit, NULL);
565 GST_LOG_OBJECT (ogm, "Type: %s, subtype: 0x%04x, channels: %d, "
566 "samplerate: %d, blockalign: %d, bps: %d, caps = %" GST_PTR_FORMAT,
567 ogm->hdr.streamtype, codec_id, ogm->hdr.s.audio.channels,
568 (gint) ogm->hdr.samples_per_unit, ogm->hdr.s.audio.blockalign,
569 ogm->hdr.s.audio.avgbytespersec, caps);
576 fourcc = GST_MAKE_FOURCC (ogm->hdr.subtype[0],
577 ogm->hdr.subtype[1], ogm->hdr.subtype[2], ogm->hdr.subtype[3]);
579 caps = gst_riff_create_video_caps (fourcc, NULL, NULL, NULL, NULL, NULL);
582 GST_WARNING_OBJECT (ogm, "could not find video caps for fourcc %"
583 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
584 caps = gst_caps_new_simple ("video/x-ogm-unknown", "fourcc",
585 GST_TYPE_FOURCC, fourcc, NULL);
589 GST_LOG_OBJECT (ogm, "Type: %s, subtype: %" GST_FOURCC_FORMAT
590 ", size: %dx%d, timeunit: %" G_GINT64_FORMAT
591 " (fps: %lf), s/u: %" G_GINT64_FORMAT ", "
592 "def.len: %d, bufsize: %d, bps: %d, caps = %" GST_PTR_FORMAT,
593 ogm->hdr.streamtype, GST_FOURCC_ARGS (fourcc),
594 ogm->hdr.s.video.width, ogm->hdr.s.video.height,
595 ogm->hdr.time_unit, 10000000. / ogm->hdr.time_unit,
596 ogm->hdr.samples_per_unit, ogm->hdr.default_len,
597 ogm->hdr.buffersize, ogm->hdr.bits_per_sample, caps);
599 /* GST_TYPE_FRACTION contains gint */
600 if (ogm->hdr.time_unit > G_MAXINT || ogm->hdr.time_unit < G_MININT)
601 GST_WARNING_OBJECT (ogm, "timeunit is out of range");
603 time_unit = (gint) CLAMP (ogm->hdr.time_unit, G_MININT, G_MAXINT);
604 gst_caps_set_simple (caps,
605 "width", G_TYPE_INT, ogm->hdr.s.video.width,
606 "height", G_TYPE_INT, ogm->hdr.s.video.height,
607 "framerate", GST_TYPE_FRACTION, 10000000, time_unit, NULL);
611 GST_LOG_OBJECT (ogm, "Type: %s, s/u: %" G_GINT64_FORMAT
612 ", timeunit=%" G_GINT64_FORMAT,
613 ogm->hdr.streamtype, ogm->hdr.samples_per_unit, ogm->hdr.time_unit);
614 caps = gst_caps_new_simple ("text/plain", NULL);
618 g_assert_not_reached ();
625 GstCaps *current_caps = GST_PAD_CAPS (ogm->srcpad);
627 if (current_caps && caps && !gst_caps_is_equal (current_caps, caps)) {
628 GST_WARNING_OBJECT (ogm, "Already an existing pad %s:%s",
629 GST_DEBUG_PAD_NAME (ogm->srcpad));
630 gst_pad_set_active (ogm->srcpad, FALSE);
631 gst_element_remove_pad (GST_ELEMENT (ogm), ogm->srcpad);
634 GST_DEBUG_OBJECT (ogm, "Existing pad has the same caps, do nothing");
638 if (ogm->srcpad == NULL) {
639 GList *l, *cached_events;
641 ogm->srcpad = gst_pad_new_from_template (ogm->srcpadtempl, "src");
642 gst_pad_use_fixed_caps (ogm->srcpad);
643 gst_pad_set_caps (ogm->srcpad, caps);
644 gst_pad_set_active (ogm->srcpad, TRUE);
645 gst_element_add_pad (GST_ELEMENT (ogm), ogm->srcpad);
646 GST_INFO_OBJECT (ogm, "Added pad %s:%s with caps %" GST_PTR_FORMAT,
647 GST_DEBUG_PAD_NAME (ogm->srcpad), caps);
649 GST_OBJECT_LOCK (ogm);
650 cached_events = ogm->cached_events;
651 ogm->cached_events = NULL;
652 GST_OBJECT_UNLOCK (ogm);
654 for (l = cached_events; l; l = l->next) {
655 GstEvent *event = GST_EVENT_CAST (l->data);
657 GST_DEBUG_OBJECT (ogm, "Pushing cached event %" GST_PTR_FORMAT, event);
658 gst_pad_push_event (ogm->srcpad, event);
660 g_list_free (cached_events);
665 tags = gst_tag_list_new ();
666 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_SUBTITLE_CODEC,
668 gst_element_found_tags_for_pad (GST_ELEMENT (ogm), ogm->srcpad, tags);
672 gst_caps_unref (caps);
679 GST_ELEMENT_ERROR (ogm, STREAM, WRONG_TYPE, ("Buffer too small"), (NULL));
680 return GST_FLOW_ERROR;
684 GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL), ("unknown ogm format"));
685 return GST_FLOW_ERROR;
690 gst_ogm_parse_comment_packet (GstOgmParse * ogm, GstBuffer * buf)
694 if (ogm->srcpad == NULL) {
695 GST_DEBUG ("no source pad");
696 return GST_FLOW_WRONG_STATE;
699 /* if this is not a subtitle stream, push the vorbiscomment packet
700 * on downstream, the respective decoder will handle it; if it is
701 * a subtitle stream, we will have to handle the comment ourself */
702 if (ogm->hdr.streamtype[0] == 't') {
705 tags = gst_tag_list_from_vorbiscomment_buffer (buf,
706 (guint8 *) "\003vorbis", 7, NULL);
709 GST_DEBUG_OBJECT (ogm, "tags = %" GST_PTR_FORMAT, tags);
710 gst_element_found_tags_for_pad (GST_ELEMENT (ogm), ogm->srcpad, tags);
712 GST_DEBUG_OBJECT (ogm, "failed to extract tags from vorbis comment");
714 /* do not push packet downstream, just let parent unref it */
717 buf = gst_buffer_copy (buf);
718 gst_buffer_set_caps (buf, GST_PAD_CAPS (ogm->srcpad));
719 ret = gst_pad_push (ogm->srcpad, buf);
726 gst_ogm_text_parse_strip_trailing_zeroes (GstOgmParse * ogm, GstBuffer * buf)
731 g_assert (gst_buffer_is_metadata_writable (buf));
733 /* zeroes are not valid UTF-8 characters, so strip them from output */
734 data = GST_BUFFER_DATA (buf);
735 size = GST_BUFFER_SIZE (buf);
736 while (size > 0 && data[size - 1] == '\0') {
740 GST_BUFFER_SIZE (buf) = size;
744 gst_ogm_parse_data_packet (GstOgmParse * ogm, GstBuffer * buf)
750 guint size, len, n, xsize = 0;
752 data = GST_BUFFER_DATA (buf);
753 size = GST_BUFFER_SIZE (buf);
755 if ((data[0] & 0x01) != 0)
756 goto invalid_startcode;
759 len = ((data[0] & 0xc0) >> 6) | ((data[0] & 0x02) << 1);
760 keyframe = (((data[0] & 0x08) >> 3) != 0);
762 if ((1 + len) > size)
763 goto buffer_too_small;
765 for (n = len; n > 0; n--) {
766 xsize = (xsize << 8) | data[n];
769 GST_LOG_OBJECT (ogm, "[0x%02x] samples: %d, hdrbytes: %d, datasize: %d",
770 data[0], xsize, len, size - len - 1);
772 sbuf = gst_buffer_create_sub (buf, len + 1, size - len - 1);
774 if (GST_BUFFER_OFFSET_END_IS_VALID (buf))
775 ogm->next_granulepos = GST_BUFFER_OFFSET_END (buf);
777 switch (ogm->hdr.streamtype[0]) {
780 GstClockTime ts, next_ts;
783 samples = (ogm->hdr.streamtype[0] == 'v') ? 1 : xsize;
786 GST_BUFFER_FLAG_SET (sbuf, GST_BUFFER_FLAG_DELTA_UNIT);
789 /* shouldn't this be granulepos - samples? (tpm) */
790 ts = gst_util_uint64_scale (ogm->next_granulepos,
791 ogm->hdr.time_unit * GST_SECOND, 10000000);
792 next_ts = gst_util_uint64_scale (ogm->next_granulepos + samples,
793 ogm->hdr.time_unit * GST_SECOND, 10000000);
795 GST_BUFFER_TIMESTAMP (sbuf) = ts;
796 GST_BUFFER_DURATION (sbuf) = next_ts - ts;
798 ogm->next_granulepos += samples;
800 if (ogm->hdr.streamtype[0] == 't') {
801 gst_ogm_text_parse_strip_trailing_zeroes (ogm, sbuf);
806 GstClockTime ts, next_ts;
808 /* shouldn't this be granulepos - samples? (tpm) */
809 ts = gst_util_uint64_scale_int (ogm->next_granulepos,
810 GST_SECOND, ogm->hdr.samples_per_unit);
811 next_ts = gst_util_uint64_scale_int (ogm->next_granulepos + xsize,
812 GST_SECOND, ogm->hdr.samples_per_unit);
814 GST_BUFFER_TIMESTAMP (sbuf) = ts;
815 GST_BUFFER_DURATION (sbuf) = next_ts - ts;
817 ogm->next_granulepos += xsize;
821 g_assert_not_reached ();
826 gst_buffer_set_caps (sbuf, GST_PAD_CAPS (ogm->srcpad));
827 GST_LOG_OBJECT (ogm, "Pushing buffer with ts=%" GST_TIME_FORMAT,
828 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (sbuf)));
829 ret = gst_pad_push (ogm->srcpad, sbuf);
830 if (ret != GST_FLOW_OK) {
831 GST_DEBUG_OBJECT (ogm, "Flow on %s:%s = %s",
832 GST_DEBUG_PAD_NAME (ogm->srcpad), gst_flow_get_name (ret));
835 ret = GST_FLOW_WRONG_STATE;
843 GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL),
844 ("unexpected packet startcode 0x%02x", data[0]));
845 return GST_FLOW_ERROR;
849 GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL),
850 ("buffer too small, len+1=%u, size=%u", len + 1, size));
851 return GST_FLOW_ERROR;
856 gst_ogm_parse_chain (GstPad * pad, GstBuffer * buf)
858 GstFlowReturn ret = GST_FLOW_OK;
859 GstOgmParse *ogm = GST_OGM_PARSE (GST_PAD_PARENT (pad));
860 guint8 *data = GST_BUFFER_DATA (buf);
861 guint size = GST_BUFFER_SIZE (buf);
864 goto buffer_too_small;
866 GST_LOG_OBJECT (ogm, "Packet with start code 0x%02x", data[0]);
870 ret = gst_ogm_parse_stream_header (ogm, data + 1, size - 1);
874 ret = gst_ogm_parse_comment_packet (ogm, buf);
878 ret = gst_ogm_parse_data_packet (ogm, buf);
883 gst_buffer_unref (buf);
885 if (ret != GST_FLOW_OK) {
886 GST_DEBUG_OBJECT (ogm, "Flow: %s", gst_flow_get_name (ret));
894 GST_ELEMENT_ERROR (ogm, STREAM, DECODE, (NULL), ("buffer too small"));
895 gst_buffer_unref (buf);
896 return GST_FLOW_ERROR;
901 gst_ogm_parse_sink_event (GstPad * pad, GstEvent * event)
903 GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));
906 GST_LOG_OBJECT (ogm, "processing %s event", GST_EVENT_TYPE_NAME (event));
908 GST_OBJECT_LOCK (ogm);
909 if (ogm->srcpad == NULL) {
910 ogm->cached_events = g_list_append (ogm->cached_events, event);
911 GST_OBJECT_UNLOCK (ogm);
914 GST_OBJECT_UNLOCK (ogm);
915 res = gst_pad_event_default (pad, event);
918 gst_object_unref (ogm);
922 static GstStateChangeReturn
923 gst_ogm_parse_change_state (GstElement * element, GstStateChange transition)
925 GstStateChangeReturn ret;
926 GstOgmParse *ogm = GST_OGM_PARSE (element);
928 ret = parent_class->change_state (element, transition);
929 if (ret != GST_STATE_CHANGE_SUCCESS)
932 switch (transition) {
933 case GST_STATE_CHANGE_PAUSED_TO_READY:
935 gst_pad_set_active (ogm->srcpad, FALSE);
936 gst_element_remove_pad (element, ogm->srcpad);
939 memset (&ogm->hdr, 0, sizeof (ogm->hdr));
940 ogm->next_granulepos = 0;
941 g_list_foreach (ogm->cached_events, (GFunc) gst_mini_object_unref, NULL);
942 g_list_free (ogm->cached_events);
943 ogm->cached_events = NULL;
953 gst_ogm_parse_plugin_init (GstPlugin * plugin)
957 GST_DEBUG_CATEGORY_INIT (gst_ogm_parse_debug, "ogmparse", 0, "ogm parser");
959 return gst_element_register (plugin, "ogmaudioparse", GST_RANK_PRIMARY,
960 GST_TYPE_OGM_AUDIO_PARSE) &&
961 gst_element_register (plugin, "ogmvideoparse", GST_RANK_PRIMARY,
962 GST_TYPE_OGM_VIDEO_PARSE) &&
963 gst_element_register (plugin, "ogmtextparse", GST_RANK_PRIMARY,
964 GST_TYPE_OGM_TEXT_PARSE);