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 gst_segment_do_seek (&rmdemux->segment, rate, format, flags,
531 cur_type, cur, stop_type, stop, &update);
534 GST_DEBUG_OBJECT (rmdemux, "segment positions set to %" GST_TIME_FORMAT "-%"
535 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start),
536 GST_TIME_ARGS (rmdemux->segment.stop));
538 /* we need to stop flushing on the sinkpad as we're going to use it
539 * next. We can do this as we have the STREAM lock now. */
540 gst_pad_push_event (rmdemux->sinkpad, gst_event_new_flush_stop (TRUE));
542 GST_LOG_OBJECT (rmdemux, "Pushed FLUSH_STOP event");
544 /* For each stream, find the first index offset equal to or before our seek
545 * target. Of these, find the smallest offset. That's where we seek to.
547 * Then we pull 4 bytes from that offset, and validate that we've seeked to a
548 * what looks like a plausible packet.
549 * If that fails, restart, with the seek target set to one less than the
550 * offset we just tried. If we run out of places to try, treat that as a fatal
553 if (!find_seek_offset_time (rmdemux, rmdemux->segment.position)) {
554 GST_LOG_OBJECT (rmdemux, "Failed to find seek offset by time");
559 GST_LOG_OBJECT (rmdemux, "Validating offset %u", rmdemux->offset);
560 validated = gst_rmdemux_validate_offset (rmdemux);
562 GST_INFO_OBJECT (rmdemux, "Failed to validate offset at %u",
564 if (!find_seek_offset_bytes (rmdemux, rmdemux->offset - 1)) {
568 validated = gst_rmdemux_validate_offset (rmdemux);
571 GST_LOG_OBJECT (rmdemux, "Found final offset. Excellent!");
573 /* now we have a new position, prepare for streaming again */
575 /* Reset the demuxer state */
576 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
579 gst_rmdemux_send_event (rmdemux, gst_event_new_flush_stop (TRUE));
581 /* must send newsegment event from streaming thread, so just set flag */
582 rmdemux->need_newsegment = TRUE;
584 /* notify start of new segment */
585 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
586 gst_element_post_message (GST_ELEMENT_CAST (rmdemux),
587 gst_message_new_segment_start (GST_OBJECT_CAST (rmdemux),
588 GST_FORMAT_TIME, rmdemux->segment.position));
591 /* restart our task since it might have been stopped when we did the
593 gst_pad_start_task (rmdemux->sinkpad, (GstTaskFunction) gst_rmdemux_loop,
594 rmdemux->sinkpad, NULL);
598 /* streaming can continue now */
599 GST_PAD_STREAM_UNLOCK (rmdemux->sinkpad);
605 GST_DEBUG_OBJECT (rmdemux, "seek failed");
612 gst_rmdemux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
614 gboolean res = FALSE;
617 rmdemux = GST_RMDEMUX (parent);
619 switch (GST_QUERY_TYPE (query)) {
620 case GST_QUERY_POSITION:
621 GST_DEBUG_OBJECT (rmdemux, "Position query: no idea from demuxer!");
623 case GST_QUERY_DURATION:{
626 gst_query_parse_duration (query, &fmt, NULL);
627 if (fmt == GST_FORMAT_TIME) {
628 GST_OBJECT_LOCK (rmdemux);
629 if (G_LIKELY (rmdemux->running)) {
630 gst_query_set_duration (query, GST_FORMAT_TIME, rmdemux->duration);
631 GST_DEBUG_OBJECT (rmdemux, "duration set to %" GST_TIME_FORMAT,
632 GST_TIME_ARGS (rmdemux->duration));
635 GST_OBJECT_UNLOCK (rmdemux);
639 case GST_QUERY_SEEKING:{
642 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
643 if (fmt == GST_FORMAT_TIME) {
644 GST_OBJECT_LOCK (rmdemux);
645 if (G_LIKELY (rmdemux->running)) {
646 gst_query_set_seeking (query, GST_FORMAT_TIME, rmdemux->seekable,
647 0, rmdemux->duration);
650 GST_OBJECT_UNLOCK (rmdemux);
654 case GST_QUERY_SEGMENT:
659 format = rmdemux->segment.format;
662 gst_segment_to_stream_time (&rmdemux->segment, format,
663 rmdemux->segment.start);
664 if ((stop = rmdemux->segment.stop) == -1)
665 stop = rmdemux->segment.duration;
667 stop = gst_segment_to_stream_time (&rmdemux->segment, format, stop);
669 gst_query_set_segment (query, rmdemux->segment.rate, format, start, stop);
674 res = gst_pad_query_default (pad, parent, query);
682 gst_rmdemux_free_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
684 g_object_unref (stream->adapter);
685 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
686 if (stream->pending_tags)
687 gst_tag_list_unref (stream->pending_tags);
688 if (stream->subpackets)
689 g_ptr_array_free (stream->subpackets, TRUE);
690 g_free (stream->index);
695 gst_rmdemux_reset (GstRMDemux * rmdemux)
699 GST_OBJECT_LOCK (rmdemux);
700 rmdemux->running = FALSE;
701 GST_OBJECT_UNLOCK (rmdemux);
703 for (cur = rmdemux->streams; cur; cur = cur->next) {
704 GstRMDemuxStream *stream = cur->data;
706 gst_flow_combiner_remove_pad (rmdemux->flowcombiner, stream->pad);
707 gst_element_remove_pad (GST_ELEMENT (rmdemux), stream->pad);
708 gst_rmdemux_free_stream (rmdemux, stream);
710 g_slist_free (rmdemux->streams);
711 rmdemux->streams = NULL;
712 rmdemux->n_audio_streams = 0;
713 rmdemux->n_video_streams = 0;
715 if (rmdemux->pending_tags != NULL) {
716 gst_tag_list_unref (rmdemux->pending_tags);
717 rmdemux->pending_tags = NULL;
720 gst_adapter_clear (rmdemux->adapter);
721 rmdemux->state = RMDEMUX_STATE_HEADER;
722 rmdemux->have_pads = FALSE;
724 gst_segment_init (&rmdemux->segment, GST_FORMAT_UNDEFINED);
725 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
726 rmdemux->base_ts = GST_CLOCK_TIME_NONE;
727 rmdemux->need_newsegment = TRUE;
729 rmdemux->have_group_id = FALSE;
730 rmdemux->group_id = G_MAXUINT;
733 static GstStateChangeReturn
734 gst_rmdemux_change_state (GstElement * element, GstStateChange transition)
736 GstRMDemux *rmdemux = GST_RMDEMUX (element);
737 GstStateChangeReturn res;
739 switch (transition) {
740 case GST_STATE_CHANGE_NULL_TO_READY:
742 case GST_STATE_CHANGE_READY_TO_PAUSED:
743 rmdemux->state = RMDEMUX_STATE_HEADER;
744 rmdemux->have_pads = FALSE;
745 gst_segment_init (&rmdemux->segment, GST_FORMAT_TIME);
746 rmdemux->running = FALSE;
748 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
754 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
756 switch (transition) {
757 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
759 case GST_STATE_CHANGE_PAUSED_TO_READY:{
760 gst_rmdemux_reset (rmdemux);
763 case GST_STATE_CHANGE_READY_TO_NULL:
772 /* this function is called when the pad is activated and should start
775 * We check if we can do random access to decide if we work push or
779 gst_rmdemux_sink_activate (GstPad * sinkpad, GstObject * parent)
784 query = gst_query_new_scheduling ();
786 if (!gst_pad_peer_query (sinkpad, query)) {
787 gst_query_unref (query);
791 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
792 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
793 gst_query_unref (query);
798 GST_DEBUG_OBJECT (sinkpad, "activating pull");
799 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
803 GST_DEBUG_OBJECT (sinkpad, "activating push");
804 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
809 gst_rmdemux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
810 GstPadMode mode, gboolean active)
815 demux = GST_RMDEMUX (parent);
818 case GST_PAD_MODE_PUSH:
819 demux->seekable = FALSE;
820 demux->running = active;
823 case GST_PAD_MODE_PULL:
825 demux->seekable = TRUE;
827 demux->loop_state = RMDEMUX_LOOP_STATE_HEADER;
828 demux->data_offset = G_MAXUINT;
830 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_rmdemux_loop,
833 res = gst_pad_stop_task (sinkpad);
844 /* random access mode - just pass over to our chain function */
846 gst_rmdemux_loop (GstPad * pad)
850 GstFlowReturn ret = GST_FLOW_OK;
853 rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
855 GST_LOG_OBJECT (rmdemux, "loop with state=%d and offset=0x%x",
856 rmdemux->loop_state, rmdemux->offset);
858 switch (rmdemux->state) {
859 case RMDEMUX_STATE_HEADER:
862 case RMDEMUX_STATE_HEADER_DATA:
865 case RMDEMUX_STATE_DATA_PACKET:
866 size = rmdemux->avg_packet_size;
868 case RMDEMUX_STATE_EOS:
869 GST_LOG_OBJECT (rmdemux, "At EOS, pausing task");
873 GST_LOG_OBJECT (rmdemux, "Default: requires %d bytes (state is %d)",
874 (int) rmdemux->size, rmdemux->state);
875 size = rmdemux->size;
879 ret = gst_pad_pull_range (pad, rmdemux->offset, size, &buffer);
880 if (ret != GST_FLOW_OK) {
881 if (rmdemux->offset == rmdemux->index_offset) {
882 /* The index isn't available so forget about it */
883 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
884 rmdemux->offset = rmdemux->data_offset;
885 GST_OBJECT_LOCK (rmdemux);
886 rmdemux->running = TRUE;
887 rmdemux->seekable = FALSE;
888 GST_OBJECT_UNLOCK (rmdemux);
891 GST_DEBUG_OBJECT (rmdemux, "Unable to pull %d bytes at offset 0x%08x "
892 "(pull_range returned flow %s, state is %d)", (gint) size,
893 rmdemux->offset, gst_flow_get_name (ret), GST_STATE (rmdemux));
898 size = gst_buffer_get_size (buffer);
900 /* Defer to the chain function */
901 ret = gst_rmdemux_chain (pad, GST_OBJECT_CAST (rmdemux), buffer);
902 if (ret != GST_FLOW_OK) {
903 GST_DEBUG_OBJECT (rmdemux, "Chain flow failed at offset 0x%08x",
908 rmdemux->offset += size;
910 switch (rmdemux->loop_state) {
911 case RMDEMUX_LOOP_STATE_HEADER:
912 if (rmdemux->offset >= rmdemux->data_offset) {
913 /* It's the end of the header */
914 rmdemux->loop_state = RMDEMUX_LOOP_STATE_INDEX;
915 rmdemux->offset = rmdemux->index_offset;
918 case RMDEMUX_LOOP_STATE_INDEX:
919 if (rmdemux->state == RMDEMUX_STATE_HEADER) {
920 if (rmdemux->index_offset == 0) {
921 /* We've read the last index */
922 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
923 rmdemux->offset = rmdemux->data_offset;
924 GST_OBJECT_LOCK (rmdemux);
925 rmdemux->running = TRUE;
926 GST_OBJECT_UNLOCK (rmdemux);
928 /* Get the next index */
929 rmdemux->offset = rmdemux->index_offset;
933 case RMDEMUX_LOOP_STATE_DATA:
942 const gchar *reason = gst_flow_get_name (ret);
944 GST_LOG_OBJECT (rmdemux, "pausing task, reason %s", reason);
945 rmdemux->segment_running = FALSE;
946 gst_pad_pause_task (rmdemux->sinkpad);
948 if (ret == GST_FLOW_EOS) {
949 /* perform EOS logic */
950 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
953 /* for segment playback we need to post when (in stream time)
954 * we stopped, this is either stop (when set) or the duration. */
955 if ((stop = rmdemux->segment.stop) == -1)
956 stop = rmdemux->segment.duration;
958 GST_LOG_OBJECT (rmdemux, "Sending segment done, at end of segment");
959 gst_element_post_message (GST_ELEMENT (rmdemux),
960 gst_message_new_segment_done (GST_OBJECT (rmdemux),
961 GST_FORMAT_TIME, stop));
962 gst_rmdemux_send_event (rmdemux,
963 gst_event_new_segment_done (GST_FORMAT_TIME, stop));
965 /* normal playback, send EOS to all linked pads */
966 GST_LOG_OBJECT (rmdemux, "Sending EOS, at end of stream");
967 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
969 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
970 GST_ELEMENT_FLOW_ERROR (rmdemux, ret);
971 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
978 gst_rmdemux_fourcc_isplausible (guint32 fourcc)
982 for (i = 0; i < 4; i++) {
983 if (!isprint ((int) ((unsigned char *) (&fourcc))[i])) {
991 gst_rmdemux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
993 GstFlowReturn ret = GST_FLOW_OK;
998 GstRMDemux *rmdemux = GST_RMDEMUX (parent);
1000 if (rmdemux->base_ts == -1) {
1001 if (GST_BUFFER_DTS_IS_VALID (buffer))
1002 rmdemux->base_ts = GST_BUFFER_DTS (buffer);
1004 rmdemux->base_ts = GST_BUFFER_PTS (buffer);
1006 GST_LOG_OBJECT (rmdemux, "base_ts %" GST_TIME_FORMAT,
1007 GST_TIME_ARGS (rmdemux->base_ts));
1010 gst_adapter_push (rmdemux->adapter, buffer);
1012 GST_LOG_OBJECT (rmdemux, "Chaining buffer of size %" G_GSIZE_FORMAT,
1013 gst_buffer_get_size (buffer));
1016 avail = gst_adapter_available (rmdemux->adapter);
1018 GST_LOG_OBJECT (rmdemux, "looping in chain, avail %u", avail);
1019 switch (rmdemux->state) {
1020 case RMDEMUX_STATE_HEADER:
1022 if (gst_adapter_available (rmdemux->adapter) < HEADER_SIZE)
1025 data = gst_adapter_map (rmdemux->adapter, HEADER_SIZE);
1027 rmdemux->object_id = RMDEMUX_FOURCC_GET (data + 0);
1028 rmdemux->size = RMDEMUX_GUINT32_GET (data + 4) - HEADER_SIZE;
1029 rmdemux->object_version = RMDEMUX_GUINT16_GET (data + 8);
1031 /* Sanity-check. We assume that the FOURCC is printable ASCII */
1032 if (!gst_rmdemux_fourcc_isplausible (rmdemux->object_id)) {
1033 /* Failed. Remain in HEADER state, try again... We flush only
1034 * the actual FOURCC, not the entire header, because we could
1035 * need to resync anywhere at all... really, this should never
1037 GST_WARNING_OBJECT (rmdemux, "Bogus looking header, unprintable "
1039 gst_adapter_unmap (rmdemux->adapter);
1040 gst_adapter_flush (rmdemux->adapter, 4);
1045 GST_LOG_OBJECT (rmdemux, "header found with object_id=%"
1047 " size=%08x object_version=%d",
1048 GST_FOURCC_ARGS (rmdemux->object_id), rmdemux->size,
1049 rmdemux->object_version);
1051 gst_adapter_unmap (rmdemux->adapter);
1052 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1054 switch (rmdemux->object_id) {
1055 case GST_MAKE_FOURCC ('.', 'R', 'M', 'F'):
1056 rmdemux->state = RMDEMUX_STATE_HEADER_RMF;
1058 case GST_MAKE_FOURCC ('P', 'R', 'O', 'P'):
1059 rmdemux->state = RMDEMUX_STATE_HEADER_PROP;
1061 case GST_MAKE_FOURCC ('M', 'D', 'P', 'R'):
1062 rmdemux->state = RMDEMUX_STATE_HEADER_MDPR;
1064 case GST_MAKE_FOURCC ('I', 'N', 'D', 'X'):
1065 rmdemux->state = RMDEMUX_STATE_HEADER_INDX;
1067 case GST_MAKE_FOURCC ('D', 'A', 'T', 'A'):
1068 rmdemux->state = RMDEMUX_STATE_HEADER_DATA;
1070 case GST_MAKE_FOURCC ('C', 'O', 'N', 'T'):
1071 rmdemux->state = RMDEMUX_STATE_HEADER_CONT;
1074 rmdemux->state = RMDEMUX_STATE_HEADER_UNKNOWN;
1079 case RMDEMUX_STATE_HEADER_UNKNOWN:
1081 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1084 GST_WARNING_OBJECT (rmdemux, "Unknown object_id %" GST_FOURCC_FORMAT,
1085 GST_FOURCC_ARGS (rmdemux->object_id));
1087 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1088 rmdemux->state = RMDEMUX_STATE_HEADER;
1091 case RMDEMUX_STATE_HEADER_RMF:
1093 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1096 if ((rmdemux->object_version == 0) || (rmdemux->object_version == 1)) {
1097 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1098 gst_rmdemux_parse__rmf (rmdemux, data, rmdemux->size);
1099 gst_adapter_unmap (rmdemux->adapter);
1100 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1102 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1104 rmdemux->state = RMDEMUX_STATE_HEADER;
1107 case RMDEMUX_STATE_HEADER_PROP:
1109 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1112 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1113 gst_rmdemux_parse_prop (rmdemux, data, rmdemux->size);
1114 gst_adapter_unmap (rmdemux->adapter);
1115 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1117 rmdemux->state = RMDEMUX_STATE_HEADER;
1120 case RMDEMUX_STATE_HEADER_MDPR:
1122 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1125 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1126 gst_rmdemux_parse_mdpr (rmdemux, data, rmdemux->size);
1127 gst_adapter_unmap (rmdemux->adapter);
1128 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1130 rmdemux->state = RMDEMUX_STATE_HEADER;
1133 case RMDEMUX_STATE_HEADER_CONT:
1135 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1138 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1139 gst_rmdemux_parse_cont (rmdemux, data, rmdemux->size);
1140 gst_adapter_unmap (rmdemux->adapter);
1141 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1143 rmdemux->state = RMDEMUX_STATE_HEADER;
1146 case RMDEMUX_STATE_HEADER_DATA:
1148 /* If we haven't already done so then signal there are no more pads */
1149 if (!rmdemux->have_pads) {
1150 GST_LOG_OBJECT (rmdemux, "no more pads");
1151 gst_element_no_more_pads (GST_ELEMENT (rmdemux));
1152 rmdemux->have_pads = TRUE;
1155 /* The actual header is only 8 bytes */
1156 rmdemux->size = DATA_SIZE;
1157 GST_LOG_OBJECT (rmdemux, "data available %" G_GSIZE_FORMAT,
1158 gst_adapter_available (rmdemux->adapter));
1159 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1162 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1163 gst_rmdemux_parse_data (rmdemux, data, rmdemux->size);
1164 gst_adapter_unmap (rmdemux->adapter);
1165 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1167 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
1170 case RMDEMUX_STATE_HEADER_INDX:
1172 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1175 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1176 rmdemux->size = gst_rmdemux_parse_indx (rmdemux, data, rmdemux->size);
1177 /* Only flush the header */
1178 gst_adapter_unmap (rmdemux->adapter);
1179 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1181 rmdemux->state = RMDEMUX_STATE_INDX_DATA;
1184 case RMDEMUX_STATE_INDX_DATA:
1186 /* There's not always an data to get... */
1187 if (rmdemux->size > 0) {
1188 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1191 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1192 gst_rmdemux_parse_indx_data (rmdemux, data, rmdemux->size);
1193 gst_adapter_unmap (rmdemux->adapter);
1194 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1197 rmdemux->state = RMDEMUX_STATE_HEADER;
1200 case RMDEMUX_STATE_DATA_PACKET:
1204 if (gst_adapter_available (rmdemux->adapter) < 2)
1207 gst_adapter_copy (rmdemux->adapter, header, 0, 2);
1208 version = RMDEMUX_GUINT16_GET (header);
1209 GST_LOG_OBJECT (rmdemux, "Data packet with version=%d", version);
1211 if (version == 0 || version == 1) {
1214 if (gst_adapter_available (rmdemux->adapter) < 4)
1217 gst_adapter_copy (rmdemux->adapter, header, 0, 4);
1219 length = RMDEMUX_GUINT16_GET (header + 2);
1220 GST_LOG_OBJECT (rmdemux, "Got length %d", length);
1223 GST_LOG_OBJECT (rmdemux, "length too small, dropping");
1224 /* Invalid, just drop it */
1225 gst_adapter_flush (rmdemux->adapter, 4);
1229 avail = gst_adapter_available (rmdemux->adapter);
1233 GST_LOG_OBJECT (rmdemux, "we have %u available and we needed %d",
1236 /* flush version and length */
1237 gst_adapter_flush (rmdemux->adapter, 4);
1240 buffer = gst_adapter_take_buffer (rmdemux->adapter, length);
1242 ret = gst_rmdemux_parse_packet (rmdemux, buffer, version);
1243 rmdemux->chunk_index++;
1246 if (rmdemux->chunk_index == rmdemux->n_chunks || length == 0)
1247 rmdemux->state = RMDEMUX_STATE_HEADER;
1250 gst_adapter_flush (rmdemux->adapter, 2);
1252 if (rmdemux->data_offset == 0) {
1253 GST_LOG_OBJECT (rmdemux,
1254 "No further data, internal demux state EOS");
1255 rmdemux->state = RMDEMUX_STATE_EOS;
1257 rmdemux->state = RMDEMUX_STATE_HEADER;
1261 case RMDEMUX_STATE_EOS:
1262 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1265 GST_WARNING_OBJECT (rmdemux, "Unhandled state %d", rmdemux->state);
1274 static GstRMDemuxStream *
1275 gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux, int id)
1279 for (cur = rmdemux->streams; cur; cur = cur->next) {
1280 GstRMDemuxStream *stream = cur->data;
1282 if (stream->id == id) {
1291 gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event)
1295 for (cur = rmdemux->streams; cur; cur = cur->next) {
1296 GstRMDemuxStream *stream = cur->data;
1298 GST_DEBUG_OBJECT (rmdemux, "Pushing %s event on pad %s",
1299 GST_EVENT_TYPE_NAME (event), GST_PAD_NAME (stream->pad));
1301 switch (GST_EVENT_TYPE (event)) {
1302 case GST_EVENT_FLUSH_STOP:
1303 stream->last_ts = -1;
1304 stream->next_ts = -1;
1305 stream->last_seq = -1;
1306 stream->next_seq = -1;
1311 gst_event_ref (event);
1312 gst_pad_push_event (stream->pad, event);
1314 gst_event_unref (event);
1318 gst_rmdemux_add_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
1320 GstCaps *stream_caps = NULL;
1321 const gchar *codec_tag = NULL;
1322 gchar *codec_name = NULL;
1326 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
1327 char *name = g_strdup_printf ("video_%u", rmdemux->n_video_streams);
1330 gst_pad_new_from_static_template (&gst_rmdemux_videosrc_template, name);
1333 codec_tag = GST_TAG_VIDEO_CODEC;
1335 switch (stream->fourcc) {
1336 case GST_RM_VDO_RV10:
1339 case GST_RM_VDO_RV20:
1342 case GST_RM_VDO_RV30:
1345 case GST_RM_VDO_RV40:
1349 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1350 "fourcc", G_TYPE_UINT, stream->fourcc, NULL);
1351 GST_WARNING_OBJECT (rmdemux,
1352 "Unknown video FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1353 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1358 gst_caps_new_simple ("video/x-pn-realvideo", "rmversion", G_TYPE_INT,
1360 "format", G_TYPE_INT,
1361 (int) stream->format,
1362 "subformat", G_TYPE_INT, (int) stream->subformat, NULL);
1366 gst_caps_set_simple (stream_caps,
1367 "width", G_TYPE_INT, stream->width,
1368 "height", G_TYPE_INT, stream->height,
1369 "framerate", GST_TYPE_FRACTION, stream->framerate_numerator,
1370 stream->framerate_denominator, NULL);
1372 rmdemux->n_video_streams++;
1374 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
1375 char *name = g_strdup_printf ("audio_%u", rmdemux->n_audio_streams);
1378 gst_pad_new_from_static_template (&gst_rmdemux_audiosrc_template, name);
1379 GST_LOG_OBJECT (rmdemux, "Created audio pad \"%s\"", name);
1382 codec_tag = GST_TAG_AUDIO_CODEC;
1384 switch (stream->fourcc) {
1385 /* Older RealAudio Codecs */
1386 case GST_RM_AUD_14_4:
1390 case GST_RM_AUD_28_8:
1394 /* DolbyNet (Dolby AC3, low bitrate) */
1395 case GST_RM_AUD_DNET:
1397 gst_caps_new_simple ("audio/x-ac3", "rate", G_TYPE_INT,
1398 (int) stream->rate, NULL);
1399 stream->needs_descrambling = TRUE;
1400 stream->subpackets_needed = 1;
1401 stream->subpackets = NULL;
1405 case GST_RM_AUD_RAAC:
1406 case GST_RM_AUD_RACP:
1408 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
1409 (int) 4, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
1410 if (stream->extra_data_size > 0) {
1411 /* strip off an unknown byte in the extra data */
1412 stream->extra_data_size--;
1413 stream->extra_data++;
1415 stream->needs_descrambling = TRUE;
1416 stream->subpackets_needed = 1;
1417 stream->subpackets = NULL;
1421 case GST_RM_AUD_ATRC:
1422 stream_caps = gst_caps_new_empty_simple ("audio/x-vnd.sony.atrac3");
1423 stream->needs_descrambling = TRUE;
1424 stream->subpackets_needed = stream->height;
1425 stream->subpackets = NULL;
1428 /* RealAudio G2 audio */
1429 case GST_RM_AUD_COOK:
1431 stream->needs_descrambling = TRUE;
1432 stream->subpackets_needed = stream->height;
1433 stream->subpackets = NULL;
1436 /* RALF is lossless */
1437 case GST_RM_AUD_RALF:
1438 GST_DEBUG_OBJECT (rmdemux, "RALF");
1439 stream_caps = gst_caps_new_empty_simple ("audio/x-ralf-mpeg4-generic");
1442 case GST_RM_AUD_SIPR:
1444 if (stream->flavor > 3) {
1445 GST_WARNING_OBJECT (rmdemux, "bad SIPR flavor %d, freeing it",
1447 g_object_unref (stream->pad);
1448 gst_rmdemux_free_stream (rmdemux, stream);
1452 GST_DEBUG_OBJECT (rmdemux, "SIPR");
1453 stream_caps = gst_caps_new_empty_simple ("audio/x-sipro");
1454 stream->needs_descrambling = TRUE;
1455 stream->subpackets_needed = stream->height;
1456 stream->subpackets = NULL;
1457 stream->leaf_size = sipr_subpk_size[stream->flavor];
1462 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1463 "fourcc", G_TYPE_UINT, stream->fourcc, NULL);
1464 GST_WARNING_OBJECT (rmdemux,
1465 "Unknown audio FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1466 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1472 gst_caps_new_simple ("audio/x-pn-realaudio", "raversion", G_TYPE_INT,
1473 (int) version, NULL);
1477 gst_caps_set_simple (stream_caps,
1478 "flavor", G_TYPE_INT, (int) stream->flavor,
1479 "rate", G_TYPE_INT, (int) stream->rate,
1480 "channels", G_TYPE_INT, (int) stream->n_channels,
1481 "width", G_TYPE_INT, (int) stream->sample_width,
1482 "leaf_size", G_TYPE_INT, (int) stream->leaf_size,
1483 "packet_size", G_TYPE_INT, (int) stream->packet_size,
1484 "bitrate", G_TYPE_INT, (int) stream->bitrate,
1485 "height", G_TYPE_INT, (int) stream->height, NULL);
1487 rmdemux->n_audio_streams++;
1489 GST_WARNING_OBJECT (rmdemux, "not adding stream of type %d, freeing it",
1491 gst_rmdemux_free_stream (rmdemux, stream);
1495 GST_PAD_ELEMENT_PRIVATE (stream->pad) = stream;
1496 rmdemux->streams = g_slist_append (rmdemux->streams, stream);
1497 GST_LOG_OBJECT (rmdemux, "n_streams is now %d",
1498 g_slist_length (rmdemux->streams));
1500 GST_LOG ("stream->pad = %p, stream_caps = %" GST_PTR_FORMAT, stream->pad,
1503 if (stream->pad && stream_caps) {
1506 GST_LOG_OBJECT (rmdemux, "%d bytes of extra data for stream %s",
1507 stream->extra_data_size, GST_PAD_NAME (stream->pad));
1509 /* add codec_data if there is any */
1510 if (stream->extra_data_size > 0) {
1513 buffer = gst_buffer_new_and_alloc (stream->extra_data_size);
1514 gst_buffer_fill (buffer, 0, stream->extra_data, stream->extra_data_size);
1516 gst_caps_set_simple (stream_caps, "codec_data", GST_TYPE_BUFFER,
1519 gst_buffer_unref (buffer);
1522 gst_pad_use_fixed_caps (stream->pad);
1524 gst_pad_set_event_function (stream->pad,
1525 GST_DEBUG_FUNCPTR (gst_rmdemux_src_event));
1526 gst_pad_set_query_function (stream->pad,
1527 GST_DEBUG_FUNCPTR (gst_rmdemux_src_query));
1529 GST_DEBUG_OBJECT (rmdemux, "adding pad %s with caps %" GST_PTR_FORMAT
1530 ", stream_id=%d", GST_PAD_NAME (stream->pad), stream_caps, stream->id);
1531 gst_pad_set_active (stream->pad, TRUE);
1534 gst_pad_create_stream_id_printf (stream->pad,
1535 GST_ELEMENT_CAST (rmdemux), "%03u", stream->id);
1538 gst_pad_get_sticky_event (rmdemux->sinkpad, GST_EVENT_STREAM_START, 0);
1540 if (gst_event_parse_group_id (event, &rmdemux->group_id))
1541 rmdemux->have_group_id = TRUE;
1543 rmdemux->have_group_id = FALSE;
1544 gst_event_unref (event);
1545 } else if (!rmdemux->have_group_id) {
1546 rmdemux->have_group_id = TRUE;
1547 rmdemux->group_id = gst_util_group_id_next ();
1550 event = gst_event_new_stream_start (stream_id);
1551 if (rmdemux->have_group_id)
1552 gst_event_set_group_id (event, rmdemux->group_id);
1554 gst_pad_push_event (stream->pad, event);
1557 gst_pad_set_caps (stream->pad, stream_caps);
1559 codec_name = gst_pb_utils_get_codec_description (stream_caps);
1561 /* save for later, we must send the tags after the newsegment event */
1562 if (codec_tag != NULL && codec_name != NULL) {
1563 if (stream->pending_tags == NULL)
1564 stream->pending_tags = gst_tag_list_new_empty ();
1565 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_KEEP,
1566 codec_tag, codec_name, NULL);
1567 g_free (codec_name);
1569 gst_element_add_pad (GST_ELEMENT_CAST (rmdemux), stream->pad);
1570 gst_flow_combiner_add_pad (rmdemux->flowcombiner, stream->pad);
1576 gst_caps_unref (stream_caps);
1580 re_skip_pascal_string (const guint8 * ptr)
1590 gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data, int length)
1592 GST_LOG_OBJECT (rmdemux, "file_version: %d", RMDEMUX_GUINT32_GET (data));
1593 GST_LOG_OBJECT (rmdemux, "num_headers: %d", RMDEMUX_GUINT32_GET (data + 4));
1597 gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data, int length)
1599 GST_LOG_OBJECT (rmdemux, "max bitrate: %d", RMDEMUX_GUINT32_GET (data));
1600 GST_LOG_OBJECT (rmdemux, "avg bitrate: %d", RMDEMUX_GUINT32_GET (data + 4));
1601 GST_LOG_OBJECT (rmdemux, "max packet size: %d",
1602 RMDEMUX_GUINT32_GET (data + 8));
1603 rmdemux->avg_packet_size = RMDEMUX_GUINT32_GET (data + 12);
1604 GST_LOG_OBJECT (rmdemux, "avg packet size: %d", rmdemux->avg_packet_size);
1605 rmdemux->num_packets = RMDEMUX_GUINT32_GET (data + 16);
1606 GST_LOG_OBJECT (rmdemux, "number of packets: %d", rmdemux->num_packets);
1608 GST_LOG_OBJECT (rmdemux, "duration: %d", RMDEMUX_GUINT32_GET (data + 20));
1609 rmdemux->duration = RMDEMUX_GUINT32_GET (data + 20) * GST_MSECOND;
1611 GST_LOG_OBJECT (rmdemux, "preroll: %d", RMDEMUX_GUINT32_GET (data + 24));
1612 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 28);
1613 GST_LOG_OBJECT (rmdemux, "offset of INDX section: 0x%08x",
1614 rmdemux->index_offset);
1615 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 32);
1616 GST_LOG_OBJECT (rmdemux, "offset of DATA section: 0x%08x",
1617 rmdemux->data_offset);
1618 GST_LOG_OBJECT (rmdemux, "n streams: %d", RMDEMUX_GUINT16_GET (data + 36));
1619 GST_LOG_OBJECT (rmdemux, "flags: 0x%04x", RMDEMUX_GUINT16_GET (data + 38));
1623 gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux, const guint8 * data, int length)
1625 GstRMDemuxStream *stream;
1626 char *stream1_type_string;
1627 char *stream2_type_string;
1631 guint32 max_bitrate;
1632 guint32 avg_bitrate;
1634 stream = g_new0 (GstRMDemuxStream, 1);
1636 stream->id = RMDEMUX_GUINT16_GET (data);
1637 stream->index = NULL;
1638 stream->seek_offset = 0;
1639 stream->last_ts = -1;
1640 stream->next_ts = -1;
1641 stream->discont = TRUE;
1642 stream->adapter = gst_adapter_new ();
1643 GST_LOG_OBJECT (rmdemux, "stream_number=%d", stream->id);
1645 /* parse the bitrates */
1646 max_bitrate = RMDEMUX_GUINT32_GET (data + 2);
1647 avg_bitrate = RMDEMUX_GUINT32_GET (data + 6);
1648 stream->bitrate = avg_bitrate;
1649 GST_LOG_OBJECT (rmdemux, "Stream max bitrate=%u", max_bitrate);
1650 GST_LOG_OBJECT (rmdemux, "Stream avg bitrate=%u", avg_bitrate);
1651 if (max_bitrate != 0) {
1652 if (stream->pending_tags == NULL)
1653 stream->pending_tags = gst_tag_list_new_empty ();
1654 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1655 GST_TAG_MAXIMUM_BITRATE, max_bitrate, NULL);
1657 if (avg_bitrate != 0) {
1658 if (stream->pending_tags == NULL)
1659 stream->pending_tags = gst_tag_list_new_empty ();
1660 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1661 GST_TAG_BITRATE, avg_bitrate, NULL);
1665 stream1_type_string = gst_rm_utils_read_string8 (data + offset,
1666 length - offset, &str_len);
1668 stream2_type_string = gst_rm_utils_read_string8 (data + offset,
1669 length - offset, &str_len);
1672 /* stream1_type_string for audio and video stream is a "put_whatever_you_want" field :
1674 * - "[The ]Video/Audio Stream" (File produced by an official Real encoder)
1675 * - "RealVideoPremierePlugIn-VIDEO/AUDIO" (File produced by Abobe Premiere)
1677 * so, we should not rely on it to know which stream type it is
1680 GST_LOG_OBJECT (rmdemux, "stream type: %s", stream1_type_string);
1681 GST_LOG_OBJECT (rmdemux, "MIME type=%s", stream2_type_string);
1683 if (strcmp (stream2_type_string, "video/x-pn-realvideo") == 0) {
1684 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1685 } else if (strcmp (stream2_type_string,
1686 "video/x-pn-multirate-realvideo") == 0) {
1687 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1688 } else if (strcmp (stream2_type_string, "audio/x-pn-realaudio") == 0) {
1689 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1690 } else if (strcmp (stream2_type_string,
1691 "audio/x-pn-multirate-realaudio") == 0) {
1692 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1693 } else if (strcmp (stream2_type_string,
1694 "audio/x-pn-multirate-realaudio-live") == 0) {
1695 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1696 } else if (strcmp (stream2_type_string, "audio/x-ralf-mpeg4-generic") == 0) {
1697 /* Another audio type found in the real testsuite */
1698 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1699 } else if (strcmp (stream1_type_string, "") == 0 &&
1700 strcmp (stream2_type_string, "logical-fileinfo") == 0) {
1701 stream_type = GST_RMDEMUX_STREAM_FILEINFO;
1703 stream_type = GST_RMDEMUX_STREAM_UNKNOWN;
1704 GST_WARNING_OBJECT (rmdemux, "unknown stream type \"%s\",\"%s\"",
1705 stream1_type_string, stream2_type_string);
1707 g_free (stream1_type_string);
1708 g_free (stream2_type_string);
1712 stream->subtype = stream_type;
1713 switch (stream_type) {
1715 case GST_RMDEMUX_STREAM_VIDEO:
1716 /* RV10/RV20/RV30/RV40 => video/x-pn-realvideo, version=1,2,3,4 */
1717 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 8);
1718 stream->width = RMDEMUX_GUINT16_GET (data + offset + 12);
1719 stream->height = RMDEMUX_GUINT16_GET (data + offset + 14);
1720 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 16);
1721 stream->subformat = RMDEMUX_GUINT32_GET (data + offset + 26);
1722 stream->format = RMDEMUX_GUINT32_GET (data + offset + 30);
1723 stream->extra_data_size = length - (offset + 26);
1724 stream->extra_data = (guint8 *) data + offset + 26;
1725 /* Natural way to represent framerates here requires unsigned 32 bit
1726 * numerator, which we don't have. For the nasty case, approximate...
1729 guint32 numerator = RMDEMUX_GUINT16_GET (data + offset + 22) * 65536 +
1730 RMDEMUX_GUINT16_GET (data + offset + 24);
1731 if (numerator > G_MAXINT) {
1732 stream->framerate_numerator = (gint) (numerator >> 1);
1733 stream->framerate_denominator = 32768;
1735 stream->framerate_numerator = (gint) numerator;
1736 stream->framerate_denominator = 65536;
1740 GST_DEBUG_OBJECT (rmdemux,
1741 "Video stream with fourcc=%" GST_FOURCC_FORMAT
1742 " width=%d height=%d rate=%d framerate=%d/%d subformat=%x format=%x extra_data_size=%d",
1743 GST_FOURCC_ARGS (stream->fourcc), stream->width, stream->height,
1744 stream->rate, stream->framerate_numerator,
1745 stream->framerate_denominator, stream->subformat, stream->format,
1746 stream->extra_data_size);
1748 case GST_RMDEMUX_STREAM_AUDIO:{
1749 stream->version = RMDEMUX_GUINT16_GET (data + offset + 4);
1750 GST_INFO ("stream version = %u", stream->version);
1751 switch (stream->version) {
1753 stream->fourcc = GST_RM_AUD_14_4;
1754 stream->packet_size = 20;
1755 stream->rate = 8000;
1756 stream->n_channels = 1;
1757 stream->sample_width = 16;
1759 stream->leaf_size = 0;
1763 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1764 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1765 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1766 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1767 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1768 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 48);
1769 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 52);
1770 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 54);
1771 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 62);
1772 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 69);
1773 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1774 stream->extra_data_size);
1775 if (length - (offset + 73) >= stream->extra_data_size) {
1776 stream->extra_data = (guint8 *) data + offset + 73;
1778 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1779 stream->extra_data_size = 0;
1783 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1784 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1785 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1786 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1787 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1788 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 54);
1789 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 58);
1790 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 60);
1791 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 66);
1792 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 74);
1793 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1794 stream->extra_data_size);
1795 if (length - (offset + 78) >= stream->extra_data_size) {
1796 stream->extra_data = (guint8 *) data + offset + 78;
1798 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1799 stream->extra_data_size = 0;
1803 GST_WARNING_OBJECT (rmdemux, "Unhandled audio stream version %d",
1808 /* 14_4, 28_8, cook, dnet, sipr, raac, racp, ralf, atrc */
1809 GST_DEBUG_OBJECT (rmdemux,
1810 "Audio stream with rate=%d sample_width=%d n_channels=%d",
1811 stream->rate, stream->sample_width, stream->n_channels);
1815 case GST_RMDEMUX_STREAM_FILEINFO:
1819 /* Length of this section */
1820 GST_DEBUG_OBJECT (rmdemux, "length2: 0x%08x",
1821 RMDEMUX_GUINT32_GET (data + offset));
1824 /* Unknown : 00 00 00 00 */
1827 /* Number of variables that would follow (loop iterations) */
1828 element_nb = RMDEMUX_GUINT32_GET (data + offset);
1831 while (element_nb) {
1832 /* Category Id : 00 00 00 XX 00 00 */
1836 offset += re_skip_pascal_string (data + offset);
1838 /* Variable Value Type */
1839 /* 00 00 00 00 00 => integer/boolean, preceded by length */
1840 /* 00 00 00 02 00 => pascal string, preceded by length, no trailing \0 */
1843 /* Variable Value */
1844 offset += re_skip_pascal_string (data + offset);
1850 case GST_RMDEMUX_STREAM_UNKNOWN:
1855 gst_rmdemux_add_stream (rmdemux, stream);
1859 gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data, int length)
1864 n = RMDEMUX_GUINT32_GET (data);
1865 id = RMDEMUX_GUINT16_GET (data + 4);
1866 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 6);
1868 GST_DEBUG_OBJECT (rmdemux, "Number of indices=%d Stream ID=%d length=%d", n,
1871 /* Point to the next index_stream */
1872 rmdemux->index_stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
1874 /* Return the length of the index */
1879 gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux, const guint8 * data,
1884 GstRMDemuxIndex *index;
1886 /* The number of index records */
1889 if (rmdemux->index_stream == NULL)
1892 /* don't parse the index a second time when operating pull-based and
1893 * reaching the end of the file */
1894 if (rmdemux->index_stream->index_length > 0) {
1895 GST_DEBUG_OBJECT (rmdemux, "Already have an index for this stream");
1899 index = g_malloc (sizeof (GstRMDemuxIndex) * n);
1900 rmdemux->index_stream->index = index;
1901 rmdemux->index_stream->index_length = n;
1903 for (i = 0; i < n; i++) {
1904 index[i].timestamp = RMDEMUX_GUINT32_GET (data + 2) * GST_MSECOND;
1905 index[i].offset = RMDEMUX_GUINT32_GET (data + 6);
1907 GST_DEBUG_OBJECT (rmdemux, "Index found for timestamp=%f (at offset=%x)",
1908 gst_guint64_to_gdouble (index[i].timestamp) / GST_SECOND,
1915 gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data, int length)
1917 rmdemux->n_chunks = RMDEMUX_GUINT32_GET (data);
1918 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 4);
1919 rmdemux->chunk_index = 0;
1920 GST_DEBUG_OBJECT (rmdemux, "Data chunk found with %d packets "
1921 "(next data at 0x%08x)", rmdemux->n_chunks, rmdemux->data_offset);
1925 gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data, int length)
1929 tags = gst_rm_utils_read_tags (data, length, gst_rm_utils_read_string16);
1932 GstTagList *old_tags = rmdemux->pending_tags;
1934 GST_LOG_OBJECT (rmdemux, "tags: %" GST_PTR_FORMAT, tags);
1936 rmdemux->pending_tags =
1937 gst_tag_list_merge (old_tags, tags, GST_TAG_MERGE_APPEND);
1939 gst_tag_list_unref (tags);
1941 gst_tag_list_unref (old_tags);
1943 gst_tag_list_set_scope (rmdemux->pending_tags, GST_TAG_SCOPE_GLOBAL);
1948 gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
1949 GstRMDemuxStream * stream)
1951 if (stream->subpackets == NULL || stream->subpackets->len == 0)
1954 GST_DEBUG_OBJECT (rmdemux, "discarding %u previously collected subpackets",
1955 stream->subpackets->len);
1956 g_ptr_array_foreach (stream->subpackets, (GFunc) gst_mini_object_unref, NULL);
1957 g_ptr_array_set_size (stream->subpackets, 0);
1960 static GstFlowReturn
1961 gst_rmdemux_descramble_audio (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
1963 GstFlowReturn ret = GST_FLOW_ERROR;
1966 guint packet_size = stream->packet_size;
1967 guint height = stream->subpackets->len;
1968 guint leaf_size = stream->leaf_size;
1971 g_assert (stream->height == height);
1973 GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size,
1976 outbuf = gst_buffer_new_and_alloc (height * packet_size);
1977 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
1979 for (p = 0; p < height; ++p) {
1980 GstBuffer *b = g_ptr_array_index (stream->subpackets, p);
1983 gst_buffer_map (b, &map, GST_MAP_READ);
1986 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (b);
1987 GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (b);
1990 for (x = 0; x < packet_size / leaf_size; ++x) {
1993 idx = height * x + ((height + 1) / 2) * (p % 2) + (p / 2);
1995 /* GST_LOG ("%3u => %3u", (height * p) + x, idx); */
1996 memcpy (outmap.data + leaf_size * idx, map.data + leaf_size * x,
1999 gst_buffer_unmap (b, &map);
2001 gst_buffer_unmap (outbuf, &outmap);
2003 /* some decoders, such as realaudiodec, need to be fed in packet units */
2004 for (p = 0; p < height; ++p) {
2008 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, p * packet_size,
2011 GST_LOG_OBJECT (rmdemux, "pushing buffer dts %" GST_TIME_FORMAT ", pts %"
2012 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (subbuf)),
2013 GST_TIME_ARGS (GST_BUFFER_PTS (subbuf)));
2015 if (stream->discont) {
2016 GST_BUFFER_FLAG_SET (subbuf, GST_BUFFER_FLAG_DISCONT);
2017 stream->discont = FALSE;
2020 ret = gst_pad_push (stream->pad, subbuf);
2021 if (ret != GST_FLOW_OK)
2025 gst_buffer_unref (outbuf);
2027 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2032 static GstFlowReturn
2033 gst_rmdemux_descramble_dnet_audio (GstRMDemux * rmdemux,
2034 GstRMDemuxStream * stream)
2038 buf = g_ptr_array_index (stream->subpackets, 0);
2039 g_ptr_array_index (stream->subpackets, 0) = NULL;
2040 g_ptr_array_set_size (stream->subpackets, 0);
2042 buf = gst_rm_utils_descramble_dnet_buffer (buf);
2044 if (stream->discont) {
2045 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2046 stream->discont = FALSE;
2048 return gst_pad_push (stream->pad, buf);
2051 static GstFlowReturn
2052 gst_rmdemux_descramble_mp4a_audio (GstRMDemux * rmdemux,
2053 GstRMDemuxStream * stream)
2056 GstBuffer *buf, *outbuf;
2057 guint frames, index, i;
2059 GstClockTime timestamp;
2063 buf = g_ptr_array_index (stream->subpackets, 0);
2064 g_ptr_array_index (stream->subpackets, 0) = NULL;
2065 g_ptr_array_set_size (stream->subpackets, 0);
2067 gst_buffer_map (buf, &map, GST_MAP_READ);
2068 timestamp = GST_BUFFER_PTS (buf);
2070 frames = (map.data[1] & 0xf0) >> 4;
2071 index = 2 * frames + 2;
2073 for (i = 0; i < frames; i++) {
2074 guint len = (map.data[i * 2 + 2] << 8) | map.data[i * 2 + 3];
2076 outbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, index, len);
2078 GST_BUFFER_PTS (outbuf) = timestamp;
2079 GST_BUFFER_DTS (outbuf) = timestamp;
2084 if (stream->discont) {
2085 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2086 stream->discont = FALSE;
2088 res = gst_pad_push (stream->pad, outbuf);
2089 if (res != GST_FLOW_OK)
2092 gst_buffer_unmap (buf, &map);
2093 gst_buffer_unref (buf);
2097 static GstFlowReturn
2098 gst_rmdemux_descramble_sipr_audio (GstRMDemux * rmdemux,
2099 GstRMDemuxStream * stream)
2104 guint packet_size = stream->packet_size;
2105 guint height = stream->subpackets->len;
2108 g_assert (stream->height == height);
2110 GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size,
2111 stream->leaf_size, height);
2113 outbuf = gst_buffer_new_and_alloc (height * packet_size);
2114 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
2116 for (p = 0; p < height; ++p) {
2117 GstBuffer *b = g_ptr_array_index (stream->subpackets, p);
2120 GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (b);
2121 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (b);
2124 gst_buffer_extract (b, 0, outmap.data + packet_size * p, packet_size);
2126 gst_buffer_unmap (outbuf, &outmap);
2128 GST_LOG_OBJECT (rmdemux, "pushing buffer dts %" GST_TIME_FORMAT ", pts %"
2129 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (outbuf)),
2130 GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)));
2132 if (stream->discont) {
2133 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2134 stream->discont = FALSE;
2137 outbuf = gst_rm_utils_descramble_sipr_buffer (outbuf);
2139 ret = gst_pad_push (stream->pad, outbuf);
2141 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2146 static GstFlowReturn
2147 gst_rmdemux_handle_scrambled_packet (GstRMDemux * rmdemux,
2148 GstRMDemuxStream * stream, GstBuffer * buf, gboolean keyframe)
2152 if (stream->subpackets == NULL)
2153 stream->subpackets = g_ptr_array_sized_new (stream->subpackets_needed);
2155 GST_LOG ("Got subpacket %u/%u, len=%" G_GSIZE_FORMAT ", key=%d",
2156 stream->subpackets->len + 1, stream->subpackets_needed,
2157 gst_buffer_get_size (buf), keyframe);
2159 if (keyframe && stream->subpackets->len > 0) {
2160 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2163 g_ptr_array_add (stream->subpackets, buf);
2165 if (stream->subpackets->len < stream->subpackets_needed)
2168 g_assert (stream->subpackets->len >= 1);
2170 switch (stream->fourcc) {
2171 case GST_RM_AUD_DNET:
2172 ret = gst_rmdemux_descramble_dnet_audio (rmdemux, stream);
2174 case GST_RM_AUD_COOK:
2175 case GST_RM_AUD_ATRC:
2176 ret = gst_rmdemux_descramble_audio (rmdemux, stream);
2178 case GST_RM_AUD_RAAC:
2179 case GST_RM_AUD_RACP:
2180 ret = gst_rmdemux_descramble_mp4a_audio (rmdemux, stream);
2182 case GST_RM_AUD_SIPR:
2183 ret = gst_rmdemux_descramble_sipr_audio (rmdemux, stream);
2186 ret = GST_FLOW_ERROR;
2187 g_assert_not_reached ();
2193 #define PARSE_NUMBER(data, size, number, label) \
2197 number = GST_READ_UINT16_BE (data); \
2198 if (!(number & 0xc000)) { \
2201 number = GST_READ_UINT32_BE (data); \
2211 static GstFlowReturn
2212 gst_rmdemux_parse_video_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2213 GstBuffer * in, guint offset, guint16 version,
2214 GstClockTime timestamp, gboolean key)
2221 gst_buffer_map (in, &map, GST_MAP_READ);
2223 data = map.data + offset;
2224 size = map.size - offset;
2226 /* if size <= 2, we want this method to return the same GstFlowReturn as it
2227 * was previously for that given stream. */
2228 ret = GST_PAD_LAST_FLOW_RETURN (stream->pad);
2234 guint pkg_subseq = 0, pkg_seqnum = G_MAXUINT;
2235 guint fragment_size;
2236 GstBuffer *fragment;
2238 pkg_header = *data++;
2242 * bit 7: 1=last block in block chain
2243 * bit 6: 1=short header (only one block?)
2245 if ((pkg_header & 0xc0) == 0x40) {
2246 /* skip unknown byte */
2252 if ((pkg_header & 0x40) == 0) {
2253 pkg_subseq = (*data++) & 0x7f;
2260 PARSE_NUMBER (data, size, pkg_length, not_enough_data);
2263 PARSE_NUMBER (data, size, pkg_offset, not_enough_data);
2267 goto not_enough_data;
2269 pkg_seqnum = *data++;
2273 GST_DEBUG_OBJECT (rmdemux,
2274 "seq %d, subseq %d, offset %d, length %d, size %" G_GSIZE_FORMAT
2275 ", header %02x", pkg_seqnum, pkg_subseq, pkg_offset, pkg_length, size,
2278 /* calc size of fragment */
2279 if ((pkg_header & 0xc0) == 0x80) {
2280 fragment_size = pkg_offset;
2282 if ((pkg_header & 0xc0) == 0)
2283 fragment_size = size;
2285 fragment_size = pkg_length;
2287 GST_DEBUG_OBJECT (rmdemux, "fragment size %d", fragment_size);
2289 /* get the fragment */
2291 gst_buffer_copy_region (in, GST_BUFFER_COPY_ALL, data - map.data,
2294 if (pkg_subseq == 1) {
2295 GST_DEBUG_OBJECT (rmdemux, "start new fragment");
2296 gst_adapter_clear (stream->adapter);
2297 stream->frag_current = 0;
2298 stream->frag_count = 0;
2299 stream->frag_length = pkg_length;
2300 } else if (pkg_subseq == 0) {
2301 GST_DEBUG_OBJECT (rmdemux, "non fragmented packet");
2302 stream->frag_current = 0;
2303 stream->frag_count = 0;
2304 stream->frag_length = fragment_size;
2307 /* put fragment in adapter */
2308 gst_adapter_push (stream->adapter, fragment);
2309 stream->frag_offset[stream->frag_count] = stream->frag_current;
2310 stream->frag_current += fragment_size;
2311 stream->frag_count++;
2313 if (stream->frag_count > MAX_FRAGS)
2314 goto too_many_fragments;
2316 GST_DEBUG_OBJECT (rmdemux, "stored fragment in adapter %d/%d",
2317 stream->frag_current, stream->frag_length);
2319 /* flush fragment when complete */
2320 if (stream->frag_current >= stream->frag_length) {
2327 /* calculate header size, which is:
2328 * 1 byte for the number of fragments - 1
2329 * for each fragment:
2330 * 4 bytes 0x00000001 little endian
2331 * 4 bytes fragment offset
2333 * This is also the matroska header for realvideo, the decoder needs the
2334 * fragment offsets, both in ffmpeg and real .so, so we just give it that
2335 * in front of the data.
2337 header_size = 1 + (8 * (stream->frag_count));
2339 GST_DEBUG_OBJECT (rmdemux,
2340 "fragmented completed. count %d, header_size %u", stream->frag_count,
2343 avail = gst_adapter_available (stream->adapter);
2345 out = gst_buffer_new_and_alloc (header_size + avail);
2346 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
2347 outdata = outmap.data;
2350 *outdata++ = stream->frag_count - 1;
2351 for (i = 0; i < stream->frag_count; i++) {
2352 GST_WRITE_UINT32_LE (outdata, 0x00000001);
2354 GST_WRITE_UINT32_LE (outdata, stream->frag_offset[i]);
2358 /* copy packet data after the header now */
2359 gst_adapter_copy (stream->adapter, outdata, 0, avail);
2360 gst_adapter_flush (stream->adapter, avail);
2362 stream->frag_current = 0;
2363 stream->frag_count = 0;
2364 stream->frag_length = 0;
2366 if (timestamp != -1) {
2367 if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts)
2368 timestamp -= rmdemux->first_ts;
2372 if (rmdemux->base_ts != -1)
2373 timestamp += rmdemux->base_ts;
2375 gst_buffer_unmap (out, &outmap);
2378 GST_BUFFER_DTS (out) = timestamp;
2379 GST_BUFFER_PTS (out) = GST_CLOCK_TIME_NONE;
2381 GST_LOG_OBJECT (rmdemux, "pushing timestamp %" GST_TIME_FORMAT,
2382 GST_TIME_ARGS (timestamp));
2384 if (stream->discont) {
2385 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT);
2386 stream->discont = FALSE;
2390 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
2393 ret = gst_pad_push (stream->pad, out);
2394 ret = gst_flow_combiner_update_flow (rmdemux->flowcombiner, ret);
2395 if (ret != GST_FLOW_OK)
2398 timestamp = GST_CLOCK_TIME_NONE;
2400 data += fragment_size;
2401 size -= fragment_size;
2403 GST_DEBUG_OBJECT (rmdemux, "%" G_GSIZE_FORMAT " bytes left", size);
2406 gst_buffer_unmap (in, &map);
2407 gst_buffer_unref (in);
2414 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2421 GST_ELEMENT_ERROR (rmdemux, STREAM, DECODE,
2422 ("Got more fragments (%u) than can be handled (%u)",
2423 stream->frag_count, MAX_FRAGS), (NULL));
2424 ret = GST_FLOW_ERROR;
2429 static GstFlowReturn
2430 gst_rmdemux_parse_audio_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2431 GstBuffer * in, guint offset, guint16 version,
2432 GstClockTime timestamp, gboolean key)
2437 buffer = gst_buffer_copy_region (in, GST_BUFFER_COPY_MEMORY, offset, -1);
2439 if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts)
2440 timestamp -= rmdemux->first_ts;
2444 if (rmdemux->base_ts != -1)
2445 timestamp += rmdemux->base_ts;
2447 GST_BUFFER_PTS (buffer) = timestamp;
2448 GST_BUFFER_DTS (buffer) = timestamp;
2450 if (stream->needs_descrambling) {
2451 GST_LOG_OBJECT (rmdemux, "descramble timestamp %" GST_TIME_FORMAT,
2452 GST_TIME_ARGS (timestamp));
2453 ret = gst_rmdemux_handle_scrambled_packet (rmdemux, stream, buffer, key);
2455 GST_LOG_OBJECT (rmdemux,
2456 "Pushing buffer of size %" G_GSIZE_FORMAT ", timestamp %"
2457 GST_TIME_FORMAT "to pad %s", gst_buffer_get_size (buffer),
2458 GST_TIME_ARGS (timestamp), GST_PAD_NAME (stream->pad));
2460 if (stream->discont) {
2461 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2462 stream->discont = FALSE;
2464 ret = gst_pad_push (stream->pad, buffer);
2467 gst_buffer_unref (in);
2472 static GstFlowReturn
2473 gst_rmdemux_parse_packet (GstRMDemux * rmdemux, GstBuffer * in, guint16 version)
2476 GstRMDemuxStream *stream;
2478 GstFlowReturn cret, ret;
2479 GstClockTime timestamp;
2486 gst_buffer_map (in, &map, GST_MAP_READ);
2491 id = RMDEMUX_GUINT16_GET (data);
2493 stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
2494 if (!stream || !stream->pad)
2495 goto unknown_stream;
2497 /* timestamp in Msec */
2498 ts = RMDEMUX_GUINT32_GET (data + 2);
2499 timestamp = ts * GST_MSECOND;
2501 rmdemux->segment.position = timestamp;
2503 GST_LOG_OBJECT (rmdemux, "Parsing a packet for stream=%d, timestamp=%"
2504 GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT ", version=%d, ts=%u", id,
2505 GST_TIME_ARGS (timestamp), size, version, ts);
2507 if (rmdemux->first_ts == GST_CLOCK_TIME_NONE) {
2508 GST_DEBUG_OBJECT (rmdemux, "First timestamp: %" GST_TIME_FORMAT,
2509 GST_TIME_ARGS (timestamp));
2510 rmdemux->first_ts = timestamp;
2513 /* skip stream_id and timestamp */
2518 flags = GST_READ_UINT8 (data + 1);
2523 /* version 1 has an extra byte */
2528 offset = data - map.data;
2529 gst_buffer_unmap (in, &map);
2531 key = (flags & 0x02) != 0;
2532 GST_DEBUG_OBJECT (rmdemux, "flags %d, Keyframe %d", flags, key);
2534 if (rmdemux->need_newsegment) {
2537 event = gst_event_new_segment (&rmdemux->segment);
2539 GST_DEBUG_OBJECT (rmdemux, "sending NEWSEGMENT event, segment.start= %"
2540 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start));
2542 gst_rmdemux_send_event (rmdemux, event);
2543 rmdemux->need_newsegment = FALSE;
2545 if (rmdemux->pending_tags != NULL) {
2546 gst_rmdemux_send_event (rmdemux,
2547 gst_event_new_tag (rmdemux->pending_tags));
2548 rmdemux->pending_tags = NULL;
2552 if (stream->pending_tags != NULL) {
2553 GST_LOG_OBJECT (stream->pad, "tags %" GST_PTR_FORMAT, stream->pending_tags);
2554 gst_pad_push_event (stream->pad, gst_event_new_tag (stream->pending_tags));
2555 stream->pending_tags = NULL;
2558 if ((rmdemux->offset + size) <= stream->seek_offset) {
2559 GST_DEBUG_OBJECT (rmdemux,
2560 "Stream %d is skipping: seek_offset=%d, offset=%d, size=%"
2561 G_GSIZE_FORMAT, stream->id, stream->seek_offset, rmdemux->offset, size);
2563 gst_buffer_unref (in);
2567 /* do special headers */
2568 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
2570 gst_rmdemux_parse_video_packet (rmdemux, stream, in, offset,
2571 version, timestamp, key);
2572 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
2574 gst_rmdemux_parse_audio_packet (rmdemux, stream, in, offset,
2575 version, timestamp, key);
2577 gst_buffer_unref (in);
2581 cret = gst_flow_combiner_update_pad_flow (rmdemux->flowcombiner, stream->pad,
2590 GST_WARNING_OBJECT (rmdemux, "No stream for stream id %d in parsing "
2592 gst_buffer_unmap (in, &map);
2593 gst_buffer_unref (in);
2599 gst_rmdemux_plugin_init (GstPlugin * plugin)
2601 return gst_element_register (plugin, "rmdemux",
2602 GST_RANK_PRIMARY, GST_TYPE_RMDEMUX);