1 /* GStreamer RealMedia demuxer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
4 * Copyright (C) <2004> Stephane Loeuillet <gstreamer@leroutier.net>
5 * Copyright (C) <2005> Owen Fraser-Green <owen@discobabe.net>
6 * Copyright (C) <2005> Michael Smith <fluendo.com>
7 * Copyright (C) <2006> Wim Taymans <wim@fluendo.com>
8 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
9 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
37 #define RMDEMUX_GUINT32_GET(a) GST_READ_UINT32_BE(a)
38 #define RMDEMUX_GUINT16_GET(a) GST_READ_UINT16_BE(a)
39 #define RMDEMUX_FOURCC_GET(a) GST_READ_UINT32_LE(a)
40 #define HEADER_SIZE 10
45 static const guint8 sipr_subpk_size[4] = { 29, 19, 37, 20 };
47 typedef struct _GstRMDemuxIndex GstRMDemuxIndex;
49 struct _GstRMDemuxStream
62 GstRMDemuxIndex *index;
64 gint framerate_numerator;
65 gint framerate_denominator;
71 guint16 rate; /* samplerate */
72 guint16 n_channels; /* channels */
73 guint16 sample_width; /* bits_per_sample */
74 guint16 leaf_size; /* subpacket_size */
75 guint32 packet_size; /* coded_frame_size */
77 guint32 extra_data_size; /* codec_data_length */
78 guint8 *extra_data; /* extras */
81 gboolean needs_descrambling;
82 guint subpackets_needed; /* subpackets needed for descrambling */
83 GPtrArray *subpackets; /* array containing subpacket GstBuffers */
85 /* Variables needed for fixing timestamps. */
86 GstClockTime next_ts, last_ts;
87 guint16 next_seq, last_seq;
94 guint frag_offset[MAX_FRAGS];
97 GstTagList *pending_tags;
100 struct _GstRMDemuxIndex
103 GstClockTime timestamp;
106 static GstStaticPadTemplate gst_rmdemux_sink_template =
107 GST_STATIC_PAD_TEMPLATE ("sink",
110 GST_STATIC_CAPS ("application/vnd.rn-realmedia")
113 static GstStaticPadTemplate gst_rmdemux_videosrc_template =
114 GST_STATIC_PAD_TEMPLATE ("video_%u",
117 GST_STATIC_CAPS_ANY);
119 static GstStaticPadTemplate gst_rmdemux_audiosrc_template =
120 GST_STATIC_PAD_TEMPLATE ("audio_%u",
123 GST_STATIC_CAPS_ANY);
125 GST_DEBUG_CATEGORY_STATIC (rmdemux_debug);
126 #define GST_CAT_DEFAULT rmdemux_debug
128 static GstElementClass *parent_class = NULL;
130 static void gst_rmdemux_class_init (GstRMDemuxClass * klass);
131 static void gst_rmdemux_base_init (GstRMDemuxClass * klass);
132 static void gst_rmdemux_init (GstRMDemux * rmdemux);
133 static void gst_rmdemux_finalize (GObject * object);
134 static GstStateChangeReturn gst_rmdemux_change_state (GstElement * element,
135 GstStateChange transition);
136 static GstFlowReturn gst_rmdemux_chain (GstPad * pad, GstObject * parent,
138 static void gst_rmdemux_loop (GstPad * pad);
139 static gboolean gst_rmdemux_sink_activate (GstPad * sinkpad,
141 static gboolean gst_rmdemux_sink_activate_mode (GstPad * sinkpad,
142 GstObject * parent, GstPadMode mode, gboolean active);
143 static gboolean gst_rmdemux_sink_event (GstPad * pad, GstObject * parent,
145 static gboolean gst_rmdemux_src_event (GstPad * pad, GstObject * parent,
147 static void gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event);
148 static gboolean gst_rmdemux_src_query (GstPad * pad, GstObject * parent,
150 static gboolean gst_rmdemux_perform_seek (GstRMDemux * rmdemux,
153 static void gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data,
155 static void gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data,
157 static void gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux,
158 const guint8 * data, int length);
159 static guint gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data,
161 static void gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data,
163 static void gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data,
165 static GstFlowReturn gst_rmdemux_parse_packet (GstRMDemux * rmdemux,
166 GstBuffer * in, guint16 version);
167 static void gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux,
168 const guint8 * data, int length);
169 static void gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
170 GstRMDemuxStream * stream);
171 static GstRMDemuxStream *gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux,
175 gst_rmdemux_get_type (void)
177 static GType rmdemux_type = 0;
180 static const GTypeInfo rmdemux_info = {
181 sizeof (GstRMDemuxClass),
182 (GBaseInitFunc) gst_rmdemux_base_init, NULL,
183 (GClassInitFunc) gst_rmdemux_class_init,
184 NULL, NULL, sizeof (GstRMDemux), 0,
185 (GInstanceInitFunc) gst_rmdemux_init,
189 g_type_register_static (GST_TYPE_ELEMENT, "GstRMDemux", &rmdemux_info,
196 gst_rmdemux_base_init (GstRMDemuxClass * klass)
198 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
200 gst_element_class_add_static_pad_template (element_class,
201 &gst_rmdemux_sink_template);
202 gst_element_class_add_static_pad_template (element_class,
203 &gst_rmdemux_videosrc_template);
204 gst_element_class_add_static_pad_template (element_class,
205 &gst_rmdemux_audiosrc_template);
206 gst_element_class_set_static_metadata (element_class, "RealMedia Demuxer",
208 "Demultiplex a RealMedia file into audio and video streams",
209 "David Schleef <ds@schleef.org>");
213 gst_rmdemux_class_init (GstRMDemuxClass * klass)
215 GObjectClass *gobject_class;
216 GstElementClass *gstelement_class;
218 gobject_class = (GObjectClass *) klass;
219 gstelement_class = (GstElementClass *) klass;
221 parent_class = g_type_class_peek_parent (klass);
223 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rmdemux_change_state);
225 GST_DEBUG_CATEGORY_INIT (rmdemux_debug, "rmdemux",
226 0, "Demuxer for Realmedia streams");
228 gobject_class->finalize = gst_rmdemux_finalize;
232 gst_rmdemux_finalize (GObject * object)
234 GstRMDemux *rmdemux = GST_RMDEMUX (object);
236 if (rmdemux->adapter) {
237 g_object_unref (rmdemux->adapter);
238 rmdemux->adapter = NULL;
240 if (rmdemux->flowcombiner) {
241 gst_flow_combiner_free (rmdemux->flowcombiner);
242 rmdemux->flowcombiner = NULL;
245 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
249 gst_rmdemux_init (GstRMDemux * rmdemux)
252 gst_pad_new_from_static_template (&gst_rmdemux_sink_template, "sink");
253 gst_pad_set_event_function (rmdemux->sinkpad,
254 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_event));
255 gst_pad_set_chain_function (rmdemux->sinkpad,
256 GST_DEBUG_FUNCPTR (gst_rmdemux_chain));
257 gst_pad_set_activate_function (rmdemux->sinkpad,
258 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate));
259 gst_pad_set_activatemode_function (rmdemux->sinkpad,
260 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate_mode));
262 gst_element_add_pad (GST_ELEMENT (rmdemux), rmdemux->sinkpad);
264 rmdemux->adapter = gst_adapter_new ();
265 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
266 rmdemux->base_ts = GST_CLOCK_TIME_NONE;
267 rmdemux->need_newsegment = TRUE;
268 rmdemux->have_group_id = FALSE;
269 rmdemux->group_id = G_MAXUINT;
270 rmdemux->flowcombiner = gst_flow_combiner_new ();
272 gst_rm_utils_run_tests ();
276 gst_rmdemux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
280 switch (GST_EVENT_TYPE (event)) {
281 case GST_EVENT_SEGMENT:
282 gst_event_unref (event);
286 ret = gst_pad_event_default (pad, parent, event);
293 gst_rmdemux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
297 GstRMDemux *rmdemux = GST_RMDEMUX (parent);
299 GST_LOG_OBJECT (rmdemux, "handling src event");
301 switch (GST_EVENT_TYPE (event)) {
306 GST_LOG_OBJECT (rmdemux, "Event on src: SEEK");
307 /* can't seek if we are not seekable, FIXME could pass the
308 * seek query upstream after converting it to bytes using
309 * the average bitrate of the stream. */
310 if (!rmdemux->seekable) {
312 GST_DEBUG ("seek on non seekable stream");
316 GST_OBJECT_LOCK (rmdemux);
317 /* check if we can do the seek now */
318 running = rmdemux->running;
319 GST_OBJECT_UNLOCK (rmdemux);
321 /* now do the seek */
323 ret = gst_rmdemux_perform_seek (rmdemux, event);
327 gst_event_unref (event);
331 GST_LOG_OBJECT (rmdemux, "Event on src: type=%d", GST_EVENT_TYPE (event));
332 ret = gst_pad_event_default (pad, parent, event);
339 GST_DEBUG ("error handling event");
340 gst_event_unref (event);
344 /* Validate that this looks like a reasonable point to seek to */
346 gst_rmdemux_validate_offset (GstRMDemux * rmdemux)
349 GstFlowReturn flowret;
350 guint16 version, length;
355 flowret = gst_pad_pull_range (rmdemux->sinkpad, rmdemux->offset, 4, &buffer);
357 if (flowret != GST_FLOW_OK) {
358 GST_DEBUG_OBJECT (rmdemux, "Failed to pull data at offset %d",
362 /* TODO: Can we also be seeking to a 'DATA' chunk header? Check this.
363 * Also, for the case we currently handle, can we check any more? It's pretty
364 * sucky to not be validating a little more heavily than this... */
365 /* This should now be the start of a data packet header. That begins with
366 * a 2-byte 'version' field, which has to be 0 or 1, then a length. I'm not
367 * certain what values are valid for length, but it must always be at least
368 * 4 bytes, and we can check that it won't take us past our known total size
371 gst_buffer_map (buffer, &map, GST_MAP_READ);
372 version = RMDEMUX_GUINT16_GET (map.data);
373 if (version != 0 && version != 1) {
374 GST_DEBUG_OBJECT (rmdemux, "Expected version 0 or 1, got %d",
379 length = RMDEMUX_GUINT16_GET (map.data + 2);
380 /* TODO: Also check against total stream length */
382 GST_DEBUG_OBJECT (rmdemux, "Expected length >= 4, got %d", (int) length);
385 gst_buffer_unmap (buffer, &map);
388 rmdemux->offset += 4;
389 gst_adapter_clear (rmdemux->adapter);
390 gst_adapter_push (rmdemux->adapter, buffer);
392 GST_WARNING_OBJECT (rmdemux, "Failed to validate seek offset at %d",
394 gst_buffer_unref (buffer);
401 find_seek_offset_bytes (GstRMDemux * rmdemux, guint target)
405 gboolean ret = FALSE;
407 for (cur = rmdemux->streams; cur; cur = cur->next) {
408 GstRMDemuxStream *stream = cur->data;
410 /* Search backwards through this stream's index until we find the first
411 * timestamp before our target time */
412 for (i = stream->index_length - 1; i >= 0; i--) {
413 if (stream->index[i].offset <= target) {
414 /* Set the seek_offset for the stream so we don't bother parsing it
415 * until we've passed that point */
416 stream->seek_offset = stream->index[i].offset;
417 rmdemux->offset = stream->index[i].offset;
427 find_seek_offset_time (GstRMDemux * rmdemux, GstClockTime time)
430 gboolean ret = FALSE;
432 GstClockTime earliest = GST_CLOCK_TIME_NONE;
435 for (cur = rmdemux->streams; cur; cur = cur->next, n_stream++) {
436 GstRMDemuxStream *stream = cur->data;
438 /* Search backwards through this stream's index until we find the first
439 * timestamp before our target time */
440 for (i = stream->index_length - 1; i >= 0; i--) {
441 if (stream->index[i].timestamp <= time) {
442 /* Set the seek_offset for the stream so we don't bother parsing it
443 * until we've passed that point */
444 stream->seek_offset = stream->index[i].offset;
446 /* If it's also the earliest timestamp we've seen of all streams, then
449 if (earliest == GST_CLOCK_TIME_NONE ||
450 stream->index[i].timestamp < earliest) {
451 earliest = stream->index[i].timestamp;
452 rmdemux->offset = stream->index[i].offset;
453 GST_DEBUG_OBJECT (rmdemux,
454 "We're looking for %" GST_TIME_FORMAT
455 " and we found that stream %d has the latest index at %"
456 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start), n_stream,
457 GST_TIME_ARGS (earliest));
465 stream->discont = TRUE;
471 gst_rmdemux_perform_seek (GstRMDemux * rmdemux, GstEvent * event)
479 GstSeekType cur_type, stop_type;
484 GST_DEBUG_OBJECT (rmdemux, "seek with event");
486 gst_event_parse_seek (event, &rate, &format, &flags,
487 &cur_type, &cur, &stop_type, &stop);
489 /* we can only seek on time */
490 if (format != GST_FORMAT_TIME) {
491 GST_DEBUG_OBJECT (rmdemux, "can only seek on TIME");
494 /* cannot yet do backwards playback */
496 GST_DEBUG_OBJECT (rmdemux, "can only seek with positive rate, not %lf",
501 GST_DEBUG_OBJECT (rmdemux, "seek without event");
507 GST_DEBUG_OBJECT (rmdemux, "seek, rate %g", rate);
509 flush = flags & GST_SEEK_FLAG_FLUSH;
511 /* first step is to unlock the streaming thread if it is
512 * blocked in a chain call, we do this by starting the flush. */
514 gst_pad_push_event (rmdemux->sinkpad, gst_event_new_flush_start ());
515 gst_rmdemux_send_event (rmdemux, gst_event_new_flush_start ());
517 gst_pad_pause_task (rmdemux->sinkpad);
520 GST_LOG_OBJECT (rmdemux, "Done starting flushes");
522 /* now grab the stream lock so that streaming cannot continue, for
523 * non flushing seeks when the element is in PAUSED this could block
525 GST_PAD_STREAM_LOCK (rmdemux->sinkpad);
527 GST_LOG_OBJECT (rmdemux, "Took streamlock");
530 if (!gst_segment_do_seek (&rmdemux->segment, rate, format, flags,
531 cur_type, cur, stop_type, stop, &update)) {
537 GST_DEBUG_OBJECT (rmdemux, "segment positions set to %" GST_TIME_FORMAT "-%"
538 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start),
539 GST_TIME_ARGS (rmdemux->segment.stop));
541 /* we need to stop flushing on the sinkpad as we're going to use it
542 * next. We can do this as we have the STREAM lock now. */
543 gst_pad_push_event (rmdemux->sinkpad, gst_event_new_flush_stop (TRUE));
545 GST_LOG_OBJECT (rmdemux, "Pushed FLUSH_STOP event");
547 /* For each stream, find the first index offset equal to or before our seek
548 * target. Of these, find the smallest offset. That's where we seek to.
550 * Then we pull 4 bytes from that offset, and validate that we've seeked to a
551 * what looks like a plausible packet.
552 * If that fails, restart, with the seek target set to one less than the
553 * offset we just tried. If we run out of places to try, treat that as a fatal
556 if (!find_seek_offset_time (rmdemux, rmdemux->segment.position)) {
557 GST_LOG_OBJECT (rmdemux, "Failed to find seek offset by time");
562 GST_LOG_OBJECT (rmdemux, "Validating offset %u", rmdemux->offset);
563 validated = gst_rmdemux_validate_offset (rmdemux);
565 GST_INFO_OBJECT (rmdemux, "Failed to validate offset at %u",
567 if (!find_seek_offset_bytes (rmdemux, rmdemux->offset - 1)) {
571 validated = gst_rmdemux_validate_offset (rmdemux);
574 GST_LOG_OBJECT (rmdemux, "Found final offset. Excellent!");
576 /* now we have a new position, prepare for streaming again */
578 /* Reset the demuxer state */
579 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
582 gst_rmdemux_send_event (rmdemux, gst_event_new_flush_stop (TRUE));
584 /* must send newsegment event from streaming thread, so just set flag */
585 rmdemux->need_newsegment = TRUE;
587 /* notify start of new segment */
588 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
589 gst_element_post_message (GST_ELEMENT_CAST (rmdemux),
590 gst_message_new_segment_start (GST_OBJECT_CAST (rmdemux),
591 GST_FORMAT_TIME, rmdemux->segment.position));
594 /* restart our task since it might have been stopped when we did the
596 gst_pad_start_task (rmdemux->sinkpad, (GstTaskFunction) gst_rmdemux_loop,
597 rmdemux->sinkpad, NULL);
601 /* streaming can continue now */
602 GST_PAD_STREAM_UNLOCK (rmdemux->sinkpad);
608 GST_DEBUG_OBJECT (rmdemux, "seek failed");
615 gst_rmdemux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
617 gboolean res = FALSE;
620 rmdemux = GST_RMDEMUX (parent);
622 switch (GST_QUERY_TYPE (query)) {
623 case GST_QUERY_POSITION:
624 GST_DEBUG_OBJECT (rmdemux, "Position query: no idea from demuxer!");
626 case GST_QUERY_DURATION:{
629 gst_query_parse_duration (query, &fmt, NULL);
630 if (fmt == GST_FORMAT_TIME) {
631 GST_OBJECT_LOCK (rmdemux);
632 if (G_LIKELY (rmdemux->running)) {
633 gst_query_set_duration (query, GST_FORMAT_TIME, rmdemux->duration);
634 GST_DEBUG_OBJECT (rmdemux, "duration set to %" GST_TIME_FORMAT,
635 GST_TIME_ARGS (rmdemux->duration));
638 GST_OBJECT_UNLOCK (rmdemux);
642 case GST_QUERY_SEEKING:{
645 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
646 if (fmt == GST_FORMAT_TIME) {
647 GST_OBJECT_LOCK (rmdemux);
648 if (G_LIKELY (rmdemux->running)) {
649 gst_query_set_seeking (query, GST_FORMAT_TIME, rmdemux->seekable,
650 0, rmdemux->duration);
653 GST_OBJECT_UNLOCK (rmdemux);
657 case GST_QUERY_SEGMENT:
662 format = rmdemux->segment.format;
665 gst_segment_to_stream_time (&rmdemux->segment, format,
666 rmdemux->segment.start);
667 if ((stop = rmdemux->segment.stop) == -1)
668 stop = rmdemux->segment.duration;
670 stop = gst_segment_to_stream_time (&rmdemux->segment, format, stop);
672 gst_query_set_segment (query, rmdemux->segment.rate, format, start, stop);
677 res = gst_pad_query_default (pad, parent, query);
685 gst_rmdemux_free_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
687 g_object_unref (stream->adapter);
688 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
689 if (stream->pending_tags)
690 gst_tag_list_unref (stream->pending_tags);
691 if (stream->subpackets)
692 g_ptr_array_free (stream->subpackets, TRUE);
693 g_free (stream->index);
698 gst_rmdemux_reset (GstRMDemux * rmdemux)
702 GST_OBJECT_LOCK (rmdemux);
703 rmdemux->running = FALSE;
704 GST_OBJECT_UNLOCK (rmdemux);
706 for (cur = rmdemux->streams; cur; cur = cur->next) {
707 GstRMDemuxStream *stream = cur->data;
709 gst_flow_combiner_remove_pad (rmdemux->flowcombiner, stream->pad);
710 gst_element_remove_pad (GST_ELEMENT (rmdemux), stream->pad);
711 gst_rmdemux_free_stream (rmdemux, stream);
713 g_slist_free (rmdemux->streams);
714 rmdemux->streams = NULL;
715 rmdemux->n_audio_streams = 0;
716 rmdemux->n_video_streams = 0;
718 if (rmdemux->pending_tags != NULL) {
719 gst_tag_list_unref (rmdemux->pending_tags);
720 rmdemux->pending_tags = NULL;
723 gst_adapter_clear (rmdemux->adapter);
724 rmdemux->state = RMDEMUX_STATE_HEADER;
725 rmdemux->have_pads = FALSE;
727 gst_segment_init (&rmdemux->segment, GST_FORMAT_UNDEFINED);
728 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
729 rmdemux->base_ts = GST_CLOCK_TIME_NONE;
730 rmdemux->need_newsegment = TRUE;
732 rmdemux->have_group_id = FALSE;
733 rmdemux->group_id = G_MAXUINT;
736 static GstStateChangeReturn
737 gst_rmdemux_change_state (GstElement * element, GstStateChange transition)
739 GstRMDemux *rmdemux = GST_RMDEMUX (element);
740 GstStateChangeReturn res;
742 switch (transition) {
743 case GST_STATE_CHANGE_NULL_TO_READY:
745 case GST_STATE_CHANGE_READY_TO_PAUSED:
746 rmdemux->state = RMDEMUX_STATE_HEADER;
747 rmdemux->have_pads = FALSE;
748 gst_segment_init (&rmdemux->segment, GST_FORMAT_TIME);
749 rmdemux->running = FALSE;
751 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
757 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
759 switch (transition) {
760 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
762 case GST_STATE_CHANGE_PAUSED_TO_READY:{
763 gst_rmdemux_reset (rmdemux);
766 case GST_STATE_CHANGE_READY_TO_NULL:
775 /* this function is called when the pad is activated and should start
778 * We check if we can do random access to decide if we work push or
782 gst_rmdemux_sink_activate (GstPad * sinkpad, GstObject * parent)
787 query = gst_query_new_scheduling ();
789 if (!gst_pad_peer_query (sinkpad, query)) {
790 gst_query_unref (query);
794 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
795 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
796 gst_query_unref (query);
801 GST_DEBUG_OBJECT (sinkpad, "activating pull");
802 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
806 GST_DEBUG_OBJECT (sinkpad, "activating push");
807 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
812 gst_rmdemux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
813 GstPadMode mode, gboolean active)
818 demux = GST_RMDEMUX (parent);
821 case GST_PAD_MODE_PUSH:
822 demux->seekable = FALSE;
823 demux->running = active;
826 case GST_PAD_MODE_PULL:
828 demux->seekable = TRUE;
830 demux->loop_state = RMDEMUX_LOOP_STATE_HEADER;
831 demux->data_offset = G_MAXUINT;
833 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_rmdemux_loop,
836 res = gst_pad_stop_task (sinkpad);
847 /* random access mode - just pass over to our chain function */
849 gst_rmdemux_loop (GstPad * pad)
853 GstFlowReturn ret = GST_FLOW_OK;
856 rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
858 GST_LOG_OBJECT (rmdemux, "loop with state=%d and offset=0x%x",
859 rmdemux->loop_state, rmdemux->offset);
861 switch (rmdemux->state) {
862 case RMDEMUX_STATE_HEADER:
865 case RMDEMUX_STATE_HEADER_DATA:
868 case RMDEMUX_STATE_DATA_PACKET:
869 size = rmdemux->avg_packet_size;
871 case RMDEMUX_STATE_EOS:
872 GST_LOG_OBJECT (rmdemux, "At EOS, pausing task");
876 GST_LOG_OBJECT (rmdemux, "Default: requires %d bytes (state is %d)",
877 (int) rmdemux->size, rmdemux->state);
878 size = rmdemux->size;
882 ret = gst_pad_pull_range (pad, rmdemux->offset, size, &buffer);
883 if (ret != GST_FLOW_OK) {
884 if (rmdemux->offset == rmdemux->index_offset) {
885 /* The index isn't available so forget about it */
886 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
887 rmdemux->offset = rmdemux->data_offset;
888 GST_OBJECT_LOCK (rmdemux);
889 rmdemux->running = TRUE;
890 rmdemux->seekable = FALSE;
891 GST_OBJECT_UNLOCK (rmdemux);
894 GST_DEBUG_OBJECT (rmdemux, "Unable to pull %d bytes at offset 0x%08x "
895 "(pull_range returned flow %s, state is %d)", (gint) size,
896 rmdemux->offset, gst_flow_get_name (ret), GST_STATE (rmdemux));
901 size = gst_buffer_get_size (buffer);
903 /* Defer to the chain function */
904 ret = gst_rmdemux_chain (pad, GST_OBJECT_CAST (rmdemux), buffer);
905 if (ret != GST_FLOW_OK) {
906 GST_DEBUG_OBJECT (rmdemux, "Chain flow failed at offset 0x%08x",
911 rmdemux->offset += size;
913 switch (rmdemux->loop_state) {
914 case RMDEMUX_LOOP_STATE_HEADER:
915 if (rmdemux->offset >= rmdemux->data_offset) {
916 /* It's the end of the header */
917 rmdemux->loop_state = RMDEMUX_LOOP_STATE_INDEX;
918 rmdemux->offset = rmdemux->index_offset;
921 case RMDEMUX_LOOP_STATE_INDEX:
922 if (rmdemux->state == RMDEMUX_STATE_HEADER) {
923 if (rmdemux->index_offset == 0) {
924 /* We've read the last index */
925 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
926 rmdemux->offset = rmdemux->data_offset;
927 GST_OBJECT_LOCK (rmdemux);
928 rmdemux->running = TRUE;
929 GST_OBJECT_UNLOCK (rmdemux);
931 /* Get the next index */
932 rmdemux->offset = rmdemux->index_offset;
936 case RMDEMUX_LOOP_STATE_DATA:
945 const gchar *reason = gst_flow_get_name (ret);
947 GST_LOG_OBJECT (rmdemux, "pausing task, reason %s", reason);
948 rmdemux->segment_running = FALSE;
949 gst_pad_pause_task (rmdemux->sinkpad);
951 if (ret == GST_FLOW_EOS) {
952 /* perform EOS logic */
953 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
956 /* for segment playback we need to post when (in stream time)
957 * we stopped, this is either stop (when set) or the duration. */
958 if ((stop = rmdemux->segment.stop) == -1)
959 stop = rmdemux->segment.duration;
961 GST_LOG_OBJECT (rmdemux, "Sending segment done, at end of segment");
962 gst_element_post_message (GST_ELEMENT (rmdemux),
963 gst_message_new_segment_done (GST_OBJECT (rmdemux),
964 GST_FORMAT_TIME, stop));
965 gst_rmdemux_send_event (rmdemux,
966 gst_event_new_segment_done (GST_FORMAT_TIME, stop));
968 /* normal playback, send EOS to all linked pads */
969 GST_LOG_OBJECT (rmdemux, "Sending EOS, at end of stream");
970 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
972 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
973 GST_ELEMENT_FLOW_ERROR (rmdemux, ret);
974 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
981 gst_rmdemux_fourcc_isplausible (guint32 fourcc)
985 for (i = 0; i < 4; i++) {
986 if (!isprint ((int) ((unsigned char *) (&fourcc))[i])) {
994 gst_rmdemux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
996 GstFlowReturn ret = GST_FLOW_OK;
1001 GstRMDemux *rmdemux = GST_RMDEMUX (parent);
1003 if (rmdemux->base_ts == -1) {
1004 if (GST_BUFFER_DTS_IS_VALID (buffer))
1005 rmdemux->base_ts = GST_BUFFER_DTS (buffer);
1007 rmdemux->base_ts = GST_BUFFER_PTS (buffer);
1009 GST_LOG_OBJECT (rmdemux, "base_ts %" GST_TIME_FORMAT,
1010 GST_TIME_ARGS (rmdemux->base_ts));
1013 gst_adapter_push (rmdemux->adapter, buffer);
1015 GST_LOG_OBJECT (rmdemux, "Chaining buffer of size %" G_GSIZE_FORMAT,
1016 gst_buffer_get_size (buffer));
1019 avail = gst_adapter_available (rmdemux->adapter);
1021 GST_LOG_OBJECT (rmdemux, "looping in chain, avail %u", avail);
1022 switch (rmdemux->state) {
1023 case RMDEMUX_STATE_HEADER:
1025 if (gst_adapter_available (rmdemux->adapter) < HEADER_SIZE)
1028 data = gst_adapter_map (rmdemux->adapter, HEADER_SIZE);
1030 rmdemux->object_id = RMDEMUX_FOURCC_GET (data + 0);
1031 rmdemux->size = RMDEMUX_GUINT32_GET (data + 4) - HEADER_SIZE;
1032 rmdemux->object_version = RMDEMUX_GUINT16_GET (data + 8);
1034 /* Sanity-check. We assume that the FOURCC is printable ASCII */
1035 if (!gst_rmdemux_fourcc_isplausible (rmdemux->object_id)) {
1036 /* Failed. Remain in HEADER state, try again... We flush only
1037 * the actual FOURCC, not the entire header, because we could
1038 * need to resync anywhere at all... really, this should never
1040 GST_WARNING_OBJECT (rmdemux, "Bogus looking header, unprintable "
1042 gst_adapter_unmap (rmdemux->adapter);
1043 gst_adapter_flush (rmdemux->adapter, 4);
1048 GST_LOG_OBJECT (rmdemux, "header found with object_id=%"
1050 " size=%08x object_version=%d",
1051 GST_FOURCC_ARGS (rmdemux->object_id), rmdemux->size,
1052 rmdemux->object_version);
1054 gst_adapter_unmap (rmdemux->adapter);
1055 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1057 switch (rmdemux->object_id) {
1058 case GST_MAKE_FOURCC ('.', 'R', 'M', 'F'):
1059 rmdemux->state = RMDEMUX_STATE_HEADER_RMF;
1061 case GST_MAKE_FOURCC ('P', 'R', 'O', 'P'):
1062 rmdemux->state = RMDEMUX_STATE_HEADER_PROP;
1064 case GST_MAKE_FOURCC ('M', 'D', 'P', 'R'):
1065 rmdemux->state = RMDEMUX_STATE_HEADER_MDPR;
1067 case GST_MAKE_FOURCC ('I', 'N', 'D', 'X'):
1068 rmdemux->state = RMDEMUX_STATE_HEADER_INDX;
1070 case GST_MAKE_FOURCC ('D', 'A', 'T', 'A'):
1071 rmdemux->state = RMDEMUX_STATE_HEADER_DATA;
1073 case GST_MAKE_FOURCC ('C', 'O', 'N', 'T'):
1074 rmdemux->state = RMDEMUX_STATE_HEADER_CONT;
1077 rmdemux->state = RMDEMUX_STATE_HEADER_UNKNOWN;
1082 case RMDEMUX_STATE_HEADER_UNKNOWN:
1084 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1087 GST_WARNING_OBJECT (rmdemux, "Unknown object_id %" GST_FOURCC_FORMAT,
1088 GST_FOURCC_ARGS (rmdemux->object_id));
1090 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1091 rmdemux->state = RMDEMUX_STATE_HEADER;
1094 case RMDEMUX_STATE_HEADER_RMF:
1096 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1099 if ((rmdemux->object_version == 0) || (rmdemux->object_version == 1)) {
1100 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1101 gst_rmdemux_parse__rmf (rmdemux, data, rmdemux->size);
1102 gst_adapter_unmap (rmdemux->adapter);
1103 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1105 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1107 rmdemux->state = RMDEMUX_STATE_HEADER;
1110 case RMDEMUX_STATE_HEADER_PROP:
1112 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1115 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1116 gst_rmdemux_parse_prop (rmdemux, data, rmdemux->size);
1117 gst_adapter_unmap (rmdemux->adapter);
1118 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1120 rmdemux->state = RMDEMUX_STATE_HEADER;
1123 case RMDEMUX_STATE_HEADER_MDPR:
1125 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1128 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1129 gst_rmdemux_parse_mdpr (rmdemux, data, rmdemux->size);
1130 gst_adapter_unmap (rmdemux->adapter);
1131 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1133 rmdemux->state = RMDEMUX_STATE_HEADER;
1136 case RMDEMUX_STATE_HEADER_CONT:
1138 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1141 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1142 gst_rmdemux_parse_cont (rmdemux, data, rmdemux->size);
1143 gst_adapter_unmap (rmdemux->adapter);
1144 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1146 rmdemux->state = RMDEMUX_STATE_HEADER;
1149 case RMDEMUX_STATE_HEADER_DATA:
1151 /* If we haven't already done so then signal there are no more pads */
1152 if (!rmdemux->have_pads) {
1153 GST_LOG_OBJECT (rmdemux, "no more pads");
1154 gst_element_no_more_pads (GST_ELEMENT (rmdemux));
1155 rmdemux->have_pads = TRUE;
1158 /* The actual header is only 8 bytes */
1159 rmdemux->size = DATA_SIZE;
1160 GST_LOG_OBJECT (rmdemux, "data available %" G_GSIZE_FORMAT,
1161 gst_adapter_available (rmdemux->adapter));
1162 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1165 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1166 gst_rmdemux_parse_data (rmdemux, data, rmdemux->size);
1167 gst_adapter_unmap (rmdemux->adapter);
1168 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1170 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
1173 case RMDEMUX_STATE_HEADER_INDX:
1175 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1178 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1179 rmdemux->size = gst_rmdemux_parse_indx (rmdemux, data, rmdemux->size);
1180 /* Only flush the header */
1181 gst_adapter_unmap (rmdemux->adapter);
1182 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1184 rmdemux->state = RMDEMUX_STATE_INDX_DATA;
1187 case RMDEMUX_STATE_INDX_DATA:
1189 /* There's not always an data to get... */
1190 if (rmdemux->size > 0) {
1191 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1194 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1195 gst_rmdemux_parse_indx_data (rmdemux, data, rmdemux->size);
1196 gst_adapter_unmap (rmdemux->adapter);
1197 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1200 rmdemux->state = RMDEMUX_STATE_HEADER;
1203 case RMDEMUX_STATE_DATA_PACKET:
1207 if (gst_adapter_available (rmdemux->adapter) < 2)
1210 gst_adapter_copy (rmdemux->adapter, header, 0, 2);
1211 version = RMDEMUX_GUINT16_GET (header);
1212 GST_LOG_OBJECT (rmdemux, "Data packet with version=%d", version);
1214 if (version == 0 || version == 1) {
1217 if (gst_adapter_available (rmdemux->adapter) < 4)
1220 gst_adapter_copy (rmdemux->adapter, header, 0, 4);
1222 length = RMDEMUX_GUINT16_GET (header + 2);
1223 GST_LOG_OBJECT (rmdemux, "Got length %d", length);
1226 GST_LOG_OBJECT (rmdemux, "length too small, dropping");
1227 /* Invalid, just drop it */
1228 gst_adapter_flush (rmdemux->adapter, 4);
1232 avail = gst_adapter_available (rmdemux->adapter);
1236 GST_LOG_OBJECT (rmdemux, "we have %u available and we needed %d",
1239 /* flush version and length */
1240 gst_adapter_flush (rmdemux->adapter, 4);
1243 buffer = gst_adapter_take_buffer (rmdemux->adapter, length);
1245 ret = gst_rmdemux_parse_packet (rmdemux, buffer, version);
1246 rmdemux->chunk_index++;
1249 if (rmdemux->chunk_index == rmdemux->n_chunks || length == 0)
1250 rmdemux->state = RMDEMUX_STATE_HEADER;
1253 gst_adapter_flush (rmdemux->adapter, 2);
1255 if (rmdemux->data_offset == 0) {
1256 GST_LOG_OBJECT (rmdemux,
1257 "No further data, internal demux state EOS");
1258 rmdemux->state = RMDEMUX_STATE_EOS;
1260 rmdemux->state = RMDEMUX_STATE_HEADER;
1264 case RMDEMUX_STATE_EOS:
1265 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1268 GST_WARNING_OBJECT (rmdemux, "Unhandled state %d", rmdemux->state);
1277 static GstRMDemuxStream *
1278 gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux, int id)
1282 for (cur = rmdemux->streams; cur; cur = cur->next) {
1283 GstRMDemuxStream *stream = cur->data;
1285 if (stream->id == id) {
1294 gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event)
1298 for (cur = rmdemux->streams; cur; cur = cur->next) {
1299 GstRMDemuxStream *stream = cur->data;
1301 GST_DEBUG_OBJECT (rmdemux, "Pushing %s event on pad %s",
1302 GST_EVENT_TYPE_NAME (event), GST_PAD_NAME (stream->pad));
1304 switch (GST_EVENT_TYPE (event)) {
1305 case GST_EVENT_FLUSH_STOP:
1306 stream->last_ts = -1;
1307 stream->next_ts = -1;
1308 stream->last_seq = -1;
1309 stream->next_seq = -1;
1314 gst_event_ref (event);
1315 gst_pad_push_event (stream->pad, event);
1317 gst_event_unref (event);
1321 gst_rmdemux_add_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
1323 GstCaps *stream_caps = NULL;
1324 const gchar *codec_tag = NULL;
1325 gchar *codec_name = NULL;
1329 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
1330 char *name = g_strdup_printf ("video_%u", rmdemux->n_video_streams);
1333 gst_pad_new_from_static_template (&gst_rmdemux_videosrc_template, name);
1336 codec_tag = GST_TAG_VIDEO_CODEC;
1338 switch (stream->fourcc) {
1339 case GST_RM_VDO_RV10:
1342 case GST_RM_VDO_RV20:
1345 case GST_RM_VDO_RV30:
1348 case GST_RM_VDO_RV40:
1352 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1353 "fourcc", G_TYPE_UINT, stream->fourcc, NULL);
1354 GST_WARNING_OBJECT (rmdemux,
1355 "Unknown video FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1356 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1361 gst_caps_new_simple ("video/x-pn-realvideo", "rmversion", G_TYPE_INT,
1363 "format", G_TYPE_INT,
1364 (int) stream->format,
1365 "subformat", G_TYPE_INT, (int) stream->subformat, NULL);
1369 gst_caps_set_simple (stream_caps,
1370 "width", G_TYPE_INT, stream->width,
1371 "height", G_TYPE_INT, stream->height,
1372 "framerate", GST_TYPE_FRACTION, stream->framerate_numerator,
1373 stream->framerate_denominator, NULL);
1375 rmdemux->n_video_streams++;
1377 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
1378 char *name = g_strdup_printf ("audio_%u", rmdemux->n_audio_streams);
1381 gst_pad_new_from_static_template (&gst_rmdemux_audiosrc_template, name);
1382 GST_LOG_OBJECT (rmdemux, "Created audio pad \"%s\"", name);
1385 codec_tag = GST_TAG_AUDIO_CODEC;
1387 switch (stream->fourcc) {
1388 /* Older RealAudio Codecs */
1389 case GST_RM_AUD_14_4:
1393 case GST_RM_AUD_28_8:
1397 /* DolbyNet (Dolby AC3, low bitrate) */
1398 case GST_RM_AUD_DNET:
1400 gst_caps_new_simple ("audio/x-ac3", "rate", G_TYPE_INT,
1401 (int) stream->rate, NULL);
1402 stream->needs_descrambling = TRUE;
1403 stream->subpackets_needed = 1;
1404 stream->subpackets = NULL;
1408 case GST_RM_AUD_RAAC:
1409 case GST_RM_AUD_RACP:
1411 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
1412 (int) 4, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
1413 if (stream->extra_data_size > 0) {
1414 /* strip off an unknown byte in the extra data */
1415 stream->extra_data_size--;
1416 stream->extra_data++;
1418 stream->needs_descrambling = TRUE;
1419 stream->subpackets_needed = 1;
1420 stream->subpackets = NULL;
1424 case GST_RM_AUD_ATRC:
1425 stream_caps = gst_caps_new_empty_simple ("audio/x-vnd.sony.atrac3");
1426 stream->needs_descrambling = TRUE;
1427 stream->subpackets_needed = stream->height;
1428 stream->subpackets = NULL;
1431 /* RealAudio G2 audio */
1432 case GST_RM_AUD_COOK:
1434 stream->needs_descrambling = TRUE;
1435 stream->subpackets_needed = stream->height;
1436 stream->subpackets = NULL;
1439 /* RALF is lossless */
1440 case GST_RM_AUD_RALF:
1441 GST_DEBUG_OBJECT (rmdemux, "RALF");
1442 stream_caps = gst_caps_new_empty_simple ("audio/x-ralf-mpeg4-generic");
1445 case GST_RM_AUD_SIPR:
1447 if (stream->flavor > 3) {
1448 GST_WARNING_OBJECT (rmdemux, "bad SIPR flavor %d, freeing it",
1450 g_object_unref (stream->pad);
1451 gst_rmdemux_free_stream (rmdemux, stream);
1455 GST_DEBUG_OBJECT (rmdemux, "SIPR");
1456 stream_caps = gst_caps_new_empty_simple ("audio/x-sipro");
1457 stream->needs_descrambling = TRUE;
1458 stream->subpackets_needed = stream->height;
1459 stream->subpackets = NULL;
1460 stream->leaf_size = sipr_subpk_size[stream->flavor];
1465 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1466 "fourcc", G_TYPE_UINT, stream->fourcc, NULL);
1467 GST_WARNING_OBJECT (rmdemux,
1468 "Unknown audio FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1469 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1475 gst_caps_new_simple ("audio/x-pn-realaudio", "raversion", G_TYPE_INT,
1476 (int) version, NULL);
1480 gst_caps_set_simple (stream_caps,
1481 "flavor", G_TYPE_INT, (int) stream->flavor,
1482 "rate", G_TYPE_INT, (int) stream->rate,
1483 "channels", G_TYPE_INT, (int) stream->n_channels,
1484 "width", G_TYPE_INT, (int) stream->sample_width,
1485 "leaf_size", G_TYPE_INT, (int) stream->leaf_size,
1486 "packet_size", G_TYPE_INT, (int) stream->packet_size,
1487 "bitrate", G_TYPE_INT, (int) stream->bitrate,
1488 "height", G_TYPE_INT, (int) stream->height, NULL);
1490 rmdemux->n_audio_streams++;
1492 GST_WARNING_OBJECT (rmdemux, "not adding stream of type %d, freeing it",
1494 gst_rmdemux_free_stream (rmdemux, stream);
1498 GST_PAD_ELEMENT_PRIVATE (stream->pad) = stream;
1499 rmdemux->streams = g_slist_append (rmdemux->streams, stream);
1500 GST_LOG_OBJECT (rmdemux, "n_streams is now %d",
1501 g_slist_length (rmdemux->streams));
1503 GST_LOG ("stream->pad = %p, stream_caps = %" GST_PTR_FORMAT, stream->pad,
1506 if (stream->pad && stream_caps) {
1509 GST_LOG_OBJECT (rmdemux, "%d bytes of extra data for stream %s",
1510 stream->extra_data_size, GST_PAD_NAME (stream->pad));
1512 /* add codec_data if there is any */
1513 if (stream->extra_data_size > 0) {
1516 buffer = gst_buffer_new_and_alloc (stream->extra_data_size);
1517 gst_buffer_fill (buffer, 0, stream->extra_data, stream->extra_data_size);
1519 gst_caps_set_simple (stream_caps, "codec_data", GST_TYPE_BUFFER,
1522 gst_buffer_unref (buffer);
1525 gst_pad_use_fixed_caps (stream->pad);
1527 gst_pad_set_event_function (stream->pad,
1528 GST_DEBUG_FUNCPTR (gst_rmdemux_src_event));
1529 gst_pad_set_query_function (stream->pad,
1530 GST_DEBUG_FUNCPTR (gst_rmdemux_src_query));
1532 GST_DEBUG_OBJECT (rmdemux, "adding pad %s with caps %" GST_PTR_FORMAT
1533 ", stream_id=%d", GST_PAD_NAME (stream->pad), stream_caps, stream->id);
1534 gst_pad_set_active (stream->pad, TRUE);
1537 gst_pad_create_stream_id_printf (stream->pad,
1538 GST_ELEMENT_CAST (rmdemux), "%03u", stream->id);
1541 gst_pad_get_sticky_event (rmdemux->sinkpad, GST_EVENT_STREAM_START, 0);
1543 if (gst_event_parse_group_id (event, &rmdemux->group_id))
1544 rmdemux->have_group_id = TRUE;
1546 rmdemux->have_group_id = FALSE;
1547 gst_event_unref (event);
1548 } else if (!rmdemux->have_group_id) {
1549 rmdemux->have_group_id = TRUE;
1550 rmdemux->group_id = gst_util_group_id_next ();
1553 event = gst_event_new_stream_start (stream_id);
1554 if (rmdemux->have_group_id)
1555 gst_event_set_group_id (event, rmdemux->group_id);
1557 gst_pad_push_event (stream->pad, event);
1560 gst_pad_set_caps (stream->pad, stream_caps);
1562 codec_name = gst_pb_utils_get_codec_description (stream_caps);
1564 /* save for later, we must send the tags after the newsegment event */
1565 if (codec_tag != NULL && codec_name != NULL) {
1566 if (stream->pending_tags == NULL)
1567 stream->pending_tags = gst_tag_list_new_empty ();
1568 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_KEEP,
1569 codec_tag, codec_name, NULL);
1570 g_free (codec_name);
1572 gst_element_add_pad (GST_ELEMENT_CAST (rmdemux), stream->pad);
1573 gst_flow_combiner_add_pad (rmdemux->flowcombiner, stream->pad);
1579 gst_caps_unref (stream_caps);
1583 re_skip_pascal_string (const guint8 * ptr)
1593 gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data, int length)
1595 GST_LOG_OBJECT (rmdemux, "file_version: %d", RMDEMUX_GUINT32_GET (data));
1596 GST_LOG_OBJECT (rmdemux, "num_headers: %d", RMDEMUX_GUINT32_GET (data + 4));
1600 gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data, int length)
1602 GST_LOG_OBJECT (rmdemux, "max bitrate: %d", RMDEMUX_GUINT32_GET (data));
1603 GST_LOG_OBJECT (rmdemux, "avg bitrate: %d", RMDEMUX_GUINT32_GET (data + 4));
1604 GST_LOG_OBJECT (rmdemux, "max packet size: %d",
1605 RMDEMUX_GUINT32_GET (data + 8));
1606 rmdemux->avg_packet_size = RMDEMUX_GUINT32_GET (data + 12);
1607 GST_LOG_OBJECT (rmdemux, "avg packet size: %d", rmdemux->avg_packet_size);
1608 rmdemux->num_packets = RMDEMUX_GUINT32_GET (data + 16);
1609 GST_LOG_OBJECT (rmdemux, "number of packets: %d", rmdemux->num_packets);
1611 GST_LOG_OBJECT (rmdemux, "duration: %d", RMDEMUX_GUINT32_GET (data + 20));
1612 rmdemux->duration = RMDEMUX_GUINT32_GET (data + 20) * GST_MSECOND;
1614 GST_LOG_OBJECT (rmdemux, "preroll: %d", RMDEMUX_GUINT32_GET (data + 24));
1615 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 28);
1616 GST_LOG_OBJECT (rmdemux, "offset of INDX section: 0x%08x",
1617 rmdemux->index_offset);
1618 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 32);
1619 GST_LOG_OBJECT (rmdemux, "offset of DATA section: 0x%08x",
1620 rmdemux->data_offset);
1621 GST_LOG_OBJECT (rmdemux, "n streams: %d", RMDEMUX_GUINT16_GET (data + 36));
1622 GST_LOG_OBJECT (rmdemux, "flags: 0x%04x", RMDEMUX_GUINT16_GET (data + 38));
1626 gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux, const guint8 * data, int length)
1628 GstRMDemuxStream *stream;
1629 char *stream1_type_string;
1630 char *stream2_type_string;
1634 guint32 max_bitrate;
1635 guint32 avg_bitrate;
1637 stream = g_new0 (GstRMDemuxStream, 1);
1639 stream->id = RMDEMUX_GUINT16_GET (data);
1640 stream->index = NULL;
1641 stream->seek_offset = 0;
1642 stream->last_ts = -1;
1643 stream->next_ts = -1;
1644 stream->discont = TRUE;
1645 stream->adapter = gst_adapter_new ();
1646 GST_LOG_OBJECT (rmdemux, "stream_number=%d", stream->id);
1648 /* parse the bitrates */
1649 max_bitrate = RMDEMUX_GUINT32_GET (data + 2);
1650 avg_bitrate = RMDEMUX_GUINT32_GET (data + 6);
1651 stream->bitrate = avg_bitrate;
1652 GST_LOG_OBJECT (rmdemux, "Stream max bitrate=%u", max_bitrate);
1653 GST_LOG_OBJECT (rmdemux, "Stream avg bitrate=%u", avg_bitrate);
1654 if (max_bitrate != 0) {
1655 if (stream->pending_tags == NULL)
1656 stream->pending_tags = gst_tag_list_new_empty ();
1657 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1658 GST_TAG_MAXIMUM_BITRATE, max_bitrate, NULL);
1660 if (avg_bitrate != 0) {
1661 if (stream->pending_tags == NULL)
1662 stream->pending_tags = gst_tag_list_new_empty ();
1663 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1664 GST_TAG_BITRATE, avg_bitrate, NULL);
1668 stream1_type_string = gst_rm_utils_read_string8 (data + offset,
1669 length - offset, &str_len);
1671 stream2_type_string = gst_rm_utils_read_string8 (data + offset,
1672 length - offset, &str_len);
1675 /* stream1_type_string for audio and video stream is a "put_whatever_you_want" field :
1677 * - "[The ]Video/Audio Stream" (File produced by an official Real encoder)
1678 * - "RealVideoPremierePlugIn-VIDEO/AUDIO" (File produced by Abobe Premiere)
1680 * so, we should not rely on it to know which stream type it is
1683 GST_LOG_OBJECT (rmdemux, "stream type: %s", stream1_type_string);
1684 GST_LOG_OBJECT (rmdemux, "MIME type=%s", stream2_type_string);
1686 if (strcmp (stream2_type_string, "video/x-pn-realvideo") == 0) {
1687 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1688 } else if (strcmp (stream2_type_string,
1689 "video/x-pn-multirate-realvideo") == 0) {
1690 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1691 } else if (strcmp (stream2_type_string, "audio/x-pn-realaudio") == 0) {
1692 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1693 } else if (strcmp (stream2_type_string,
1694 "audio/x-pn-multirate-realaudio") == 0) {
1695 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1696 } else if (strcmp (stream2_type_string,
1697 "audio/x-pn-multirate-realaudio-live") == 0) {
1698 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1699 } else if (strcmp (stream2_type_string, "audio/x-ralf-mpeg4-generic") == 0) {
1700 /* Another audio type found in the real testsuite */
1701 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1702 } else if (strcmp (stream1_type_string, "") == 0 &&
1703 strcmp (stream2_type_string, "logical-fileinfo") == 0) {
1704 stream_type = GST_RMDEMUX_STREAM_FILEINFO;
1706 stream_type = GST_RMDEMUX_STREAM_UNKNOWN;
1707 GST_WARNING_OBJECT (rmdemux, "unknown stream type \"%s\",\"%s\"",
1708 stream1_type_string, stream2_type_string);
1710 g_free (stream1_type_string);
1711 g_free (stream2_type_string);
1715 stream->subtype = stream_type;
1716 switch (stream_type) {
1718 case GST_RMDEMUX_STREAM_VIDEO:
1719 /* RV10/RV20/RV30/RV40 => video/x-pn-realvideo, version=1,2,3,4 */
1720 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 8);
1721 stream->width = RMDEMUX_GUINT16_GET (data + offset + 12);
1722 stream->height = RMDEMUX_GUINT16_GET (data + offset + 14);
1723 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 16);
1724 stream->subformat = RMDEMUX_GUINT32_GET (data + offset + 26);
1725 stream->format = RMDEMUX_GUINT32_GET (data + offset + 30);
1726 stream->extra_data_size = length - (offset + 26);
1727 stream->extra_data = (guint8 *) data + offset + 26;
1728 /* Natural way to represent framerates here requires unsigned 32 bit
1729 * numerator, which we don't have. For the nasty case, approximate...
1732 guint32 numerator = RMDEMUX_GUINT16_GET (data + offset + 22) * 65536 +
1733 RMDEMUX_GUINT16_GET (data + offset + 24);
1734 if (numerator > G_MAXINT) {
1735 stream->framerate_numerator = (gint) (numerator >> 1);
1736 stream->framerate_denominator = 32768;
1738 stream->framerate_numerator = (gint) numerator;
1739 stream->framerate_denominator = 65536;
1743 GST_DEBUG_OBJECT (rmdemux,
1744 "Video stream with fourcc=%" GST_FOURCC_FORMAT
1745 " width=%d height=%d rate=%d framerate=%d/%d subformat=%x format=%x extra_data_size=%d",
1746 GST_FOURCC_ARGS (stream->fourcc), stream->width, stream->height,
1747 stream->rate, stream->framerate_numerator,
1748 stream->framerate_denominator, stream->subformat, stream->format,
1749 stream->extra_data_size);
1751 case GST_RMDEMUX_STREAM_AUDIO:{
1752 stream->version = RMDEMUX_GUINT16_GET (data + offset + 4);
1753 GST_INFO ("stream version = %u", stream->version);
1754 switch (stream->version) {
1756 stream->fourcc = GST_RM_AUD_14_4;
1757 stream->packet_size = 20;
1758 stream->rate = 8000;
1759 stream->n_channels = 1;
1760 stream->sample_width = 16;
1762 stream->leaf_size = 0;
1766 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1767 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1768 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1769 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1770 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1771 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 48);
1772 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 52);
1773 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 54);
1774 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 62);
1775 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 69);
1776 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1777 stream->extra_data_size);
1778 if (length - (offset + 73) >= stream->extra_data_size) {
1779 stream->extra_data = (guint8 *) data + offset + 73;
1781 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1782 stream->extra_data_size = 0;
1786 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1787 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1788 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1789 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1790 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1791 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 54);
1792 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 58);
1793 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 60);
1794 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 66);
1795 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 74);
1796 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1797 stream->extra_data_size);
1798 if (length - (offset + 78) >= stream->extra_data_size) {
1799 stream->extra_data = (guint8 *) data + offset + 78;
1801 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1802 stream->extra_data_size = 0;
1806 GST_WARNING_OBJECT (rmdemux, "Unhandled audio stream version %d",
1811 /* 14_4, 28_8, cook, dnet, sipr, raac, racp, ralf, atrc */
1812 GST_DEBUG_OBJECT (rmdemux,
1813 "Audio stream with rate=%d sample_width=%d n_channels=%d",
1814 stream->rate, stream->sample_width, stream->n_channels);
1818 case GST_RMDEMUX_STREAM_FILEINFO:
1822 /* Length of this section */
1823 GST_DEBUG_OBJECT (rmdemux, "length2: 0x%08x",
1824 RMDEMUX_GUINT32_GET (data + offset));
1827 /* Unknown : 00 00 00 00 */
1830 /* Number of variables that would follow (loop iterations) */
1831 element_nb = RMDEMUX_GUINT32_GET (data + offset);
1834 while (element_nb) {
1835 /* Category Id : 00 00 00 XX 00 00 */
1839 offset += re_skip_pascal_string (data + offset);
1841 /* Variable Value Type */
1842 /* 00 00 00 00 00 => integer/boolean, preceded by length */
1843 /* 00 00 00 02 00 => pascal string, preceded by length, no trailing \0 */
1846 /* Variable Value */
1847 offset += re_skip_pascal_string (data + offset);
1853 case GST_RMDEMUX_STREAM_UNKNOWN:
1858 gst_rmdemux_add_stream (rmdemux, stream);
1862 gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data, int length)
1867 n = RMDEMUX_GUINT32_GET (data);
1868 id = RMDEMUX_GUINT16_GET (data + 4);
1869 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 6);
1871 GST_DEBUG_OBJECT (rmdemux, "Number of indices=%d Stream ID=%d length=%d", n,
1874 /* Point to the next index_stream */
1875 rmdemux->index_stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
1877 /* Return the length of the index */
1882 gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux, const guint8 * data,
1887 GstRMDemuxIndex *index;
1889 /* The number of index records */
1892 if (rmdemux->index_stream == NULL)
1895 /* don't parse the index a second time when operating pull-based and
1896 * reaching the end of the file */
1897 if (rmdemux->index_stream->index_length > 0) {
1898 GST_DEBUG_OBJECT (rmdemux, "Already have an index for this stream");
1902 index = g_malloc (sizeof (GstRMDemuxIndex) * n);
1903 rmdemux->index_stream->index = index;
1904 rmdemux->index_stream->index_length = n;
1906 for (i = 0; i < n; i++) {
1907 index[i].timestamp = RMDEMUX_GUINT32_GET (data + 2) * GST_MSECOND;
1908 index[i].offset = RMDEMUX_GUINT32_GET (data + 6);
1910 GST_DEBUG_OBJECT (rmdemux, "Index found for timestamp=%f (at offset=%x)",
1911 gst_guint64_to_gdouble (index[i].timestamp) / GST_SECOND,
1918 gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data, int length)
1920 rmdemux->n_chunks = RMDEMUX_GUINT32_GET (data);
1921 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 4);
1922 rmdemux->chunk_index = 0;
1923 GST_DEBUG_OBJECT (rmdemux, "Data chunk found with %d packets "
1924 "(next data at 0x%08x)", rmdemux->n_chunks, rmdemux->data_offset);
1928 gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data, int length)
1932 tags = gst_rm_utils_read_tags (data, length, gst_rm_utils_read_string16);
1935 GstTagList *old_tags = rmdemux->pending_tags;
1937 GST_LOG_OBJECT (rmdemux, "tags: %" GST_PTR_FORMAT, tags);
1939 rmdemux->pending_tags =
1940 gst_tag_list_merge (old_tags, tags, GST_TAG_MERGE_APPEND);
1942 gst_tag_list_unref (tags);
1944 gst_tag_list_unref (old_tags);
1946 gst_tag_list_set_scope (rmdemux->pending_tags, GST_TAG_SCOPE_GLOBAL);
1951 gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
1952 GstRMDemuxStream * stream)
1954 if (stream->subpackets == NULL || stream->subpackets->len == 0)
1957 GST_DEBUG_OBJECT (rmdemux, "discarding %u previously collected subpackets",
1958 stream->subpackets->len);
1959 g_ptr_array_foreach (stream->subpackets, (GFunc) gst_mini_object_unref, NULL);
1960 g_ptr_array_set_size (stream->subpackets, 0);
1963 static GstFlowReturn
1964 gst_rmdemux_descramble_audio (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
1966 GstFlowReturn ret = GST_FLOW_ERROR;
1969 guint packet_size = stream->packet_size;
1970 guint height = stream->subpackets->len;
1971 guint leaf_size = stream->leaf_size;
1974 g_assert (stream->height == height);
1976 GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size,
1979 outbuf = gst_buffer_new_and_alloc (height * packet_size);
1980 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
1982 for (p = 0; p < height; ++p) {
1983 GstBuffer *b = g_ptr_array_index (stream->subpackets, p);
1986 gst_buffer_map (b, &map, GST_MAP_READ);
1989 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (b);
1990 GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (b);
1993 for (x = 0; x < packet_size / leaf_size; ++x) {
1996 idx = height * x + ((height + 1) / 2) * (p % 2) + (p / 2);
1998 /* GST_LOG ("%3u => %3u", (height * p) + x, idx); */
1999 memcpy (outmap.data + leaf_size * idx, map.data + leaf_size * x,
2002 gst_buffer_unmap (b, &map);
2004 gst_buffer_unmap (outbuf, &outmap);
2006 /* some decoders, such as realaudiodec, need to be fed in packet units */
2007 for (p = 0; p < height; ++p) {
2011 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, p * packet_size,
2014 GST_LOG_OBJECT (rmdemux, "pushing buffer dts %" GST_TIME_FORMAT ", pts %"
2015 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (subbuf)),
2016 GST_TIME_ARGS (GST_BUFFER_PTS (subbuf)));
2018 if (stream->discont) {
2019 GST_BUFFER_FLAG_SET (subbuf, GST_BUFFER_FLAG_DISCONT);
2020 stream->discont = FALSE;
2023 ret = gst_pad_push (stream->pad, subbuf);
2024 if (ret != GST_FLOW_OK)
2028 gst_buffer_unref (outbuf);
2030 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2035 static GstFlowReturn
2036 gst_rmdemux_descramble_dnet_audio (GstRMDemux * rmdemux,
2037 GstRMDemuxStream * stream)
2041 buf = g_ptr_array_index (stream->subpackets, 0);
2042 g_ptr_array_index (stream->subpackets, 0) = NULL;
2043 g_ptr_array_set_size (stream->subpackets, 0);
2045 buf = gst_rm_utils_descramble_dnet_buffer (buf);
2047 if (stream->discont) {
2048 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2049 stream->discont = FALSE;
2051 return gst_pad_push (stream->pad, buf);
2054 static GstFlowReturn
2055 gst_rmdemux_descramble_mp4a_audio (GstRMDemux * rmdemux,
2056 GstRMDemuxStream * stream)
2059 GstBuffer *buf, *outbuf;
2060 guint frames, index, i;
2062 GstClockTime timestamp;
2066 buf = g_ptr_array_index (stream->subpackets, 0);
2067 g_ptr_array_index (stream->subpackets, 0) = NULL;
2068 g_ptr_array_set_size (stream->subpackets, 0);
2070 gst_buffer_map (buf, &map, GST_MAP_READ);
2071 timestamp = GST_BUFFER_PTS (buf);
2073 frames = (map.data[1] & 0xf0) >> 4;
2074 index = 2 * frames + 2;
2076 for (i = 0; i < frames; i++) {
2077 guint len = (map.data[i * 2 + 2] << 8) | map.data[i * 2 + 3];
2079 outbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, index, len);
2081 GST_BUFFER_PTS (outbuf) = timestamp;
2082 GST_BUFFER_DTS (outbuf) = timestamp;
2087 if (stream->discont) {
2088 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2089 stream->discont = FALSE;
2091 res = gst_pad_push (stream->pad, outbuf);
2092 if (res != GST_FLOW_OK)
2095 gst_buffer_unmap (buf, &map);
2096 gst_buffer_unref (buf);
2100 static GstFlowReturn
2101 gst_rmdemux_descramble_sipr_audio (GstRMDemux * rmdemux,
2102 GstRMDemuxStream * stream)
2107 guint packet_size = stream->packet_size;
2108 guint height = stream->subpackets->len;
2111 g_assert (stream->height == height);
2113 GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size,
2114 stream->leaf_size, height);
2116 outbuf = gst_buffer_new_and_alloc (height * packet_size);
2117 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
2119 for (p = 0; p < height; ++p) {
2120 GstBuffer *b = g_ptr_array_index (stream->subpackets, p);
2123 GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (b);
2124 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (b);
2127 gst_buffer_extract (b, 0, outmap.data + packet_size * p, packet_size);
2129 gst_buffer_unmap (outbuf, &outmap);
2131 GST_LOG_OBJECT (rmdemux, "pushing buffer dts %" GST_TIME_FORMAT ", pts %"
2132 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (outbuf)),
2133 GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)));
2135 if (stream->discont) {
2136 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2137 stream->discont = FALSE;
2140 outbuf = gst_rm_utils_descramble_sipr_buffer (outbuf);
2142 ret = gst_pad_push (stream->pad, outbuf);
2144 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2149 static GstFlowReturn
2150 gst_rmdemux_handle_scrambled_packet (GstRMDemux * rmdemux,
2151 GstRMDemuxStream * stream, GstBuffer * buf, gboolean keyframe)
2155 if (stream->subpackets == NULL)
2156 stream->subpackets = g_ptr_array_sized_new (stream->subpackets_needed);
2158 GST_LOG ("Got subpacket %u/%u, len=%" G_GSIZE_FORMAT ", key=%d",
2159 stream->subpackets->len + 1, stream->subpackets_needed,
2160 gst_buffer_get_size (buf), keyframe);
2162 if (keyframe && stream->subpackets->len > 0) {
2163 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2166 g_ptr_array_add (stream->subpackets, buf);
2168 if (stream->subpackets->len < stream->subpackets_needed)
2171 g_assert (stream->subpackets->len >= 1);
2173 switch (stream->fourcc) {
2174 case GST_RM_AUD_DNET:
2175 ret = gst_rmdemux_descramble_dnet_audio (rmdemux, stream);
2177 case GST_RM_AUD_COOK:
2178 case GST_RM_AUD_ATRC:
2179 ret = gst_rmdemux_descramble_audio (rmdemux, stream);
2181 case GST_RM_AUD_RAAC:
2182 case GST_RM_AUD_RACP:
2183 ret = gst_rmdemux_descramble_mp4a_audio (rmdemux, stream);
2185 case GST_RM_AUD_SIPR:
2186 ret = gst_rmdemux_descramble_sipr_audio (rmdemux, stream);
2189 ret = GST_FLOW_ERROR;
2190 g_assert_not_reached ();
2196 #define PARSE_NUMBER(data, size, number, label) \
2200 number = GST_READ_UINT16_BE (data); \
2201 if (!(number & 0xc000)) { \
2204 number = GST_READ_UINT32_BE (data); \
2214 static GstFlowReturn
2215 gst_rmdemux_parse_video_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2216 GstBuffer * in, guint offset, guint16 version,
2217 GstClockTime timestamp, gboolean key)
2224 gst_buffer_map (in, &map, GST_MAP_READ);
2226 if (map.size < offset)
2227 goto not_enough_data;
2229 data = map.data + offset;
2230 size = map.size - offset;
2232 /* if size <= 2, we want this method to return the same GstFlowReturn as it
2233 * was previously for that given stream. */
2234 ret = GST_PAD_LAST_FLOW_RETURN (stream->pad);
2240 guint pkg_subseq = 0, pkg_seqnum = G_MAXUINT;
2241 guint fragment_size;
2242 GstBuffer *fragment;
2244 pkg_header = *data++;
2248 * bit 7: 1=last block in block chain
2249 * bit 6: 1=short header (only one block?)
2251 if ((pkg_header & 0xc0) == 0x40) {
2252 /* skip unknown byte */
2258 if ((pkg_header & 0x40) == 0) {
2259 pkg_subseq = (*data++) & 0x7f;
2266 PARSE_NUMBER (data, size, pkg_length, not_enough_data);
2269 PARSE_NUMBER (data, size, pkg_offset, not_enough_data);
2273 goto not_enough_data;
2275 pkg_seqnum = *data++;
2279 GST_DEBUG_OBJECT (rmdemux,
2280 "seq %d, subseq %d, offset %d, length %d, size %" G_GSIZE_FORMAT
2281 ", header %02x", pkg_seqnum, pkg_subseq, pkg_offset, pkg_length, size,
2284 /* calc size of fragment */
2285 if ((pkg_header & 0xc0) == 0x80) {
2286 fragment_size = pkg_offset;
2288 if ((pkg_header & 0xc0) == 0)
2289 fragment_size = size;
2291 fragment_size = pkg_length;
2293 GST_DEBUG_OBJECT (rmdemux, "fragment size %d", fragment_size);
2295 if (map.size < (data - map.data) + fragment_size)
2296 goto not_enough_data;
2298 /* get the fragment */
2300 gst_buffer_copy_region (in, GST_BUFFER_COPY_ALL, data - map.data,
2303 if (pkg_subseq == 1) {
2304 GST_DEBUG_OBJECT (rmdemux, "start new fragment");
2305 gst_adapter_clear (stream->adapter);
2306 stream->frag_current = 0;
2307 stream->frag_count = 0;
2308 stream->frag_length = pkg_length;
2309 } else if (pkg_subseq == 0) {
2310 GST_DEBUG_OBJECT (rmdemux, "non fragmented packet");
2311 stream->frag_current = 0;
2312 stream->frag_count = 0;
2313 stream->frag_length = fragment_size;
2316 /* put fragment in adapter */
2317 gst_adapter_push (stream->adapter, fragment);
2318 stream->frag_offset[stream->frag_count] = stream->frag_current;
2319 stream->frag_current += fragment_size;
2320 stream->frag_count++;
2322 if (stream->frag_count > MAX_FRAGS)
2323 goto too_many_fragments;
2325 GST_DEBUG_OBJECT (rmdemux, "stored fragment in adapter %d/%d",
2326 stream->frag_current, stream->frag_length);
2328 /* flush fragment when complete */
2329 if (stream->frag_current >= stream->frag_length) {
2336 /* calculate header size, which is:
2337 * 1 byte for the number of fragments - 1
2338 * for each fragment:
2339 * 4 bytes 0x00000001 little endian
2340 * 4 bytes fragment offset
2342 * This is also the matroska header for realvideo, the decoder needs the
2343 * fragment offsets, both in ffmpeg and real .so, so we just give it that
2344 * in front of the data.
2346 header_size = 1 + (8 * (stream->frag_count));
2348 GST_DEBUG_OBJECT (rmdemux,
2349 "fragmented completed. count %d, header_size %u", stream->frag_count,
2352 avail = gst_adapter_available (stream->adapter);
2354 out = gst_buffer_new_and_alloc (header_size + avail);
2355 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
2356 outdata = outmap.data;
2359 *outdata++ = stream->frag_count - 1;
2360 for (i = 0; i < stream->frag_count; i++) {
2361 GST_WRITE_UINT32_LE (outdata, 0x00000001);
2363 GST_WRITE_UINT32_LE (outdata, stream->frag_offset[i]);
2367 /* copy packet data after the header now */
2368 gst_adapter_copy (stream->adapter, outdata, 0, avail);
2369 gst_adapter_flush (stream->adapter, avail);
2371 stream->frag_current = 0;
2372 stream->frag_count = 0;
2373 stream->frag_length = 0;
2375 if (timestamp != -1) {
2376 if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts)
2377 timestamp -= rmdemux->first_ts;
2381 if (rmdemux->base_ts != -1)
2382 timestamp += rmdemux->base_ts;
2384 gst_buffer_unmap (out, &outmap);
2387 GST_BUFFER_DTS (out) = timestamp;
2388 GST_BUFFER_PTS (out) = GST_CLOCK_TIME_NONE;
2390 GST_LOG_OBJECT (rmdemux, "pushing timestamp %" GST_TIME_FORMAT,
2391 GST_TIME_ARGS (timestamp));
2393 if (stream->discont) {
2394 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT);
2395 stream->discont = FALSE;
2399 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
2402 ret = gst_pad_push (stream->pad, out);
2403 ret = gst_flow_combiner_update_flow (rmdemux->flowcombiner, ret);
2404 if (ret != GST_FLOW_OK)
2407 timestamp = GST_CLOCK_TIME_NONE;
2409 data += fragment_size;
2410 size -= fragment_size;
2412 GST_DEBUG_OBJECT (rmdemux, "%" G_GSIZE_FORMAT " bytes left", size);
2415 gst_buffer_unmap (in, &map);
2416 gst_buffer_unref (in);
2423 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2430 GST_ELEMENT_ERROR (rmdemux, STREAM, DECODE,
2431 ("Got more fragments (%u) than can be handled (%u)",
2432 stream->frag_count, MAX_FRAGS), (NULL));
2433 ret = GST_FLOW_ERROR;
2438 static GstFlowReturn
2439 gst_rmdemux_parse_audio_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2440 GstBuffer * in, guint offset, guint16 version,
2441 GstClockTime timestamp, gboolean key)
2446 if (gst_buffer_get_size (in) < offset)
2447 goto not_enough_data;
2449 buffer = gst_buffer_copy_region (in, GST_BUFFER_COPY_MEMORY, offset, -1);
2451 if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts)
2452 timestamp -= rmdemux->first_ts;
2456 if (rmdemux->base_ts != -1)
2457 timestamp += rmdemux->base_ts;
2459 GST_BUFFER_PTS (buffer) = timestamp;
2460 GST_BUFFER_DTS (buffer) = timestamp;
2462 if (stream->needs_descrambling) {
2463 GST_LOG_OBJECT (rmdemux, "descramble timestamp %" GST_TIME_FORMAT,
2464 GST_TIME_ARGS (timestamp));
2465 ret = gst_rmdemux_handle_scrambled_packet (rmdemux, stream, buffer, key);
2467 GST_LOG_OBJECT (rmdemux,
2468 "Pushing buffer of size %" G_GSIZE_FORMAT ", timestamp %"
2469 GST_TIME_FORMAT "to pad %s", gst_buffer_get_size (buffer),
2470 GST_TIME_ARGS (timestamp), GST_PAD_NAME (stream->pad));
2472 if (stream->discont) {
2473 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2474 stream->discont = FALSE;
2476 ret = gst_pad_push (stream->pad, buffer);
2480 gst_buffer_unref (in);
2487 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2494 static GstFlowReturn
2495 gst_rmdemux_parse_packet (GstRMDemux * rmdemux, GstBuffer * in, guint16 version)
2498 GstRMDemuxStream *stream;
2500 GstFlowReturn cret, ret;
2501 GstClockTime timestamp;
2508 gst_buffer_map (in, &map, GST_MAP_READ);
2512 if (size < 4 + 6 + 1 + 2)
2513 goto not_enough_data;
2516 id = RMDEMUX_GUINT16_GET (data);
2518 stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
2519 if (!stream || !stream->pad)
2520 goto unknown_stream;
2522 /* timestamp in Msec */
2523 ts = RMDEMUX_GUINT32_GET (data + 2);
2524 timestamp = ts * GST_MSECOND;
2526 rmdemux->segment.position = timestamp;
2528 GST_LOG_OBJECT (rmdemux, "Parsing a packet for stream=%d, timestamp=%"
2529 GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT ", version=%d, ts=%u", id,
2530 GST_TIME_ARGS (timestamp), size, version, ts);
2532 if (rmdemux->first_ts == GST_CLOCK_TIME_NONE) {
2533 GST_DEBUG_OBJECT (rmdemux, "First timestamp: %" GST_TIME_FORMAT,
2534 GST_TIME_ARGS (timestamp));
2535 rmdemux->first_ts = timestamp;
2538 /* skip stream_id and timestamp */
2543 flags = GST_READ_UINT8 (data + 1);
2548 /* version 1 has an extra byte */
2551 goto not_enough_data;
2556 offset = data - map.data;
2557 gst_buffer_unmap (in, &map);
2559 key = (flags & 0x02) != 0;
2560 GST_DEBUG_OBJECT (rmdemux, "flags %d, Keyframe %d", flags, key);
2562 if (rmdemux->need_newsegment) {
2565 event = gst_event_new_segment (&rmdemux->segment);
2567 GST_DEBUG_OBJECT (rmdemux, "sending NEWSEGMENT event, segment.start= %"
2568 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start));
2570 gst_rmdemux_send_event (rmdemux, event);
2571 rmdemux->need_newsegment = FALSE;
2573 if (rmdemux->pending_tags != NULL) {
2574 gst_rmdemux_send_event (rmdemux,
2575 gst_event_new_tag (rmdemux->pending_tags));
2576 rmdemux->pending_tags = NULL;
2580 if (stream->pending_tags != NULL) {
2581 GST_LOG_OBJECT (stream->pad, "tags %" GST_PTR_FORMAT, stream->pending_tags);
2582 gst_pad_push_event (stream->pad, gst_event_new_tag (stream->pending_tags));
2583 stream->pending_tags = NULL;
2586 if ((rmdemux->offset + size) <= stream->seek_offset) {
2587 GST_DEBUG_OBJECT (rmdemux,
2588 "Stream %d is skipping: seek_offset=%d, offset=%d, size=%"
2589 G_GSIZE_FORMAT, stream->id, stream->seek_offset, rmdemux->offset, size);
2591 gst_buffer_unref (in);
2595 /* do special headers */
2596 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
2598 gst_rmdemux_parse_video_packet (rmdemux, stream, in, offset,
2599 version, timestamp, key);
2600 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
2602 gst_rmdemux_parse_audio_packet (rmdemux, stream, in, offset,
2603 version, timestamp, key);
2605 gst_buffer_unref (in);
2609 cret = gst_flow_combiner_update_pad_flow (rmdemux->flowcombiner, stream->pad,
2618 GST_WARNING_OBJECT (rmdemux, "No stream for stream id %d in parsing "
2620 gst_buffer_unmap (in, &map);
2621 gst_buffer_unref (in);
2628 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2630 gst_buffer_unmap (in, &map);
2631 gst_buffer_unref (in);
2637 gst_rmdemux_plugin_init (GstPlugin * plugin)
2639 return gst_element_register (plugin, "rmdemux",
2640 GST_RANK_PRIMARY, GST_TYPE_RMDEMUX);