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_pad_template (element_class,
201 gst_static_pad_template_get (&gst_rmdemux_sink_template));
202 gst_element_class_add_pad_template (element_class,
203 gst_static_pad_template_get (&gst_rmdemux_videosrc_template));
204 gst_element_class_add_pad_template (element_class,
205 gst_static_pad_template_get (&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_ERROR (rmdemux, STREAM, FAILED,
971 (NULL), ("stream stopped, reason %s", reason));
972 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
979 gst_rmdemux_fourcc_isplausible (guint32 fourcc)
983 for (i = 0; i < 4; i++) {
984 if (!isprint ((int) ((unsigned char *) (&fourcc))[i])) {
992 gst_rmdemux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
994 GstFlowReturn ret = GST_FLOW_OK;
999 GstRMDemux *rmdemux = GST_RMDEMUX (parent);
1001 if (rmdemux->base_ts == -1) {
1002 if (GST_BUFFER_DTS_IS_VALID (buffer))
1003 rmdemux->base_ts = GST_BUFFER_DTS (buffer);
1005 rmdemux->base_ts = GST_BUFFER_PTS (buffer);
1007 GST_LOG_OBJECT (rmdemux, "base_ts %" GST_TIME_FORMAT,
1008 GST_TIME_ARGS (rmdemux->base_ts));
1011 gst_adapter_push (rmdemux->adapter, buffer);
1013 GST_LOG_OBJECT (rmdemux, "Chaining buffer of size %" G_GSIZE_FORMAT,
1014 gst_buffer_get_size (buffer));
1017 avail = gst_adapter_available (rmdemux->adapter);
1019 GST_LOG_OBJECT (rmdemux, "looping in chain, avail %u", avail);
1020 switch (rmdemux->state) {
1021 case RMDEMUX_STATE_HEADER:
1023 if (gst_adapter_available (rmdemux->adapter) < HEADER_SIZE)
1026 data = gst_adapter_map (rmdemux->adapter, HEADER_SIZE);
1028 rmdemux->object_id = RMDEMUX_FOURCC_GET (data + 0);
1029 rmdemux->size = RMDEMUX_GUINT32_GET (data + 4) - HEADER_SIZE;
1030 rmdemux->object_version = RMDEMUX_GUINT16_GET (data + 8);
1032 /* Sanity-check. We assume that the FOURCC is printable ASCII */
1033 if (!gst_rmdemux_fourcc_isplausible (rmdemux->object_id)) {
1034 /* Failed. Remain in HEADER state, try again... We flush only
1035 * the actual FOURCC, not the entire header, because we could
1036 * need to resync anywhere at all... really, this should never
1038 GST_WARNING_OBJECT (rmdemux, "Bogus looking header, unprintable "
1040 gst_adapter_unmap (rmdemux->adapter);
1041 gst_adapter_flush (rmdemux->adapter, 4);
1046 GST_LOG_OBJECT (rmdemux, "header found with object_id=%"
1048 " size=%08x object_version=%d",
1049 GST_FOURCC_ARGS (rmdemux->object_id), rmdemux->size,
1050 rmdemux->object_version);
1052 gst_adapter_unmap (rmdemux->adapter);
1053 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1055 switch (rmdemux->object_id) {
1056 case GST_MAKE_FOURCC ('.', 'R', 'M', 'F'):
1057 rmdemux->state = RMDEMUX_STATE_HEADER_RMF;
1059 case GST_MAKE_FOURCC ('P', 'R', 'O', 'P'):
1060 rmdemux->state = RMDEMUX_STATE_HEADER_PROP;
1062 case GST_MAKE_FOURCC ('M', 'D', 'P', 'R'):
1063 rmdemux->state = RMDEMUX_STATE_HEADER_MDPR;
1065 case GST_MAKE_FOURCC ('I', 'N', 'D', 'X'):
1066 rmdemux->state = RMDEMUX_STATE_HEADER_INDX;
1068 case GST_MAKE_FOURCC ('D', 'A', 'T', 'A'):
1069 rmdemux->state = RMDEMUX_STATE_HEADER_DATA;
1071 case GST_MAKE_FOURCC ('C', 'O', 'N', 'T'):
1072 rmdemux->state = RMDEMUX_STATE_HEADER_CONT;
1075 rmdemux->state = RMDEMUX_STATE_HEADER_UNKNOWN;
1080 case RMDEMUX_STATE_HEADER_UNKNOWN:
1082 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1085 GST_WARNING_OBJECT (rmdemux, "Unknown object_id %" GST_FOURCC_FORMAT,
1086 GST_FOURCC_ARGS (rmdemux->object_id));
1088 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1089 rmdemux->state = RMDEMUX_STATE_HEADER;
1092 case RMDEMUX_STATE_HEADER_RMF:
1094 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1097 if ((rmdemux->object_version == 0) || (rmdemux->object_version == 1)) {
1098 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1099 gst_rmdemux_parse__rmf (rmdemux, data, rmdemux->size);
1100 gst_adapter_unmap (rmdemux->adapter);
1101 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1103 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1105 rmdemux->state = RMDEMUX_STATE_HEADER;
1108 case RMDEMUX_STATE_HEADER_PROP:
1110 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1113 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1114 gst_rmdemux_parse_prop (rmdemux, data, rmdemux->size);
1115 gst_adapter_unmap (rmdemux->adapter);
1116 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1118 rmdemux->state = RMDEMUX_STATE_HEADER;
1121 case RMDEMUX_STATE_HEADER_MDPR:
1123 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1126 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1127 gst_rmdemux_parse_mdpr (rmdemux, data, rmdemux->size);
1128 gst_adapter_unmap (rmdemux->adapter);
1129 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1131 rmdemux->state = RMDEMUX_STATE_HEADER;
1134 case RMDEMUX_STATE_HEADER_CONT:
1136 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1139 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1140 gst_rmdemux_parse_cont (rmdemux, data, rmdemux->size);
1141 gst_adapter_unmap (rmdemux->adapter);
1142 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1144 rmdemux->state = RMDEMUX_STATE_HEADER;
1147 case RMDEMUX_STATE_HEADER_DATA:
1149 /* If we haven't already done so then signal there are no more pads */
1150 if (!rmdemux->have_pads) {
1151 GST_LOG_OBJECT (rmdemux, "no more pads");
1152 gst_element_no_more_pads (GST_ELEMENT (rmdemux));
1153 rmdemux->have_pads = TRUE;
1156 /* The actual header is only 8 bytes */
1157 rmdemux->size = DATA_SIZE;
1158 GST_LOG_OBJECT (rmdemux, "data available %" G_GSIZE_FORMAT,
1159 gst_adapter_available (rmdemux->adapter));
1160 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1163 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1164 gst_rmdemux_parse_data (rmdemux, data, rmdemux->size);
1165 gst_adapter_unmap (rmdemux->adapter);
1166 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1168 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
1171 case RMDEMUX_STATE_HEADER_INDX:
1173 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1176 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1177 rmdemux->size = gst_rmdemux_parse_indx (rmdemux, data, rmdemux->size);
1178 /* Only flush the header */
1179 gst_adapter_unmap (rmdemux->adapter);
1180 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1182 rmdemux->state = RMDEMUX_STATE_INDX_DATA;
1185 case RMDEMUX_STATE_INDX_DATA:
1187 /* There's not always an data to get... */
1188 if (rmdemux->size > 0) {
1189 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1192 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1193 gst_rmdemux_parse_indx_data (rmdemux, data, rmdemux->size);
1194 gst_adapter_unmap (rmdemux->adapter);
1195 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1198 rmdemux->state = RMDEMUX_STATE_HEADER;
1201 case RMDEMUX_STATE_DATA_PACKET:
1205 if (gst_adapter_available (rmdemux->adapter) < 2)
1208 gst_adapter_copy (rmdemux->adapter, header, 0, 2);
1209 version = RMDEMUX_GUINT16_GET (header);
1210 GST_LOG_OBJECT (rmdemux, "Data packet with version=%d", version);
1212 if (version == 0 || version == 1) {
1215 if (gst_adapter_available (rmdemux->adapter) < 4)
1218 gst_adapter_copy (rmdemux->adapter, header, 0, 4);
1220 length = RMDEMUX_GUINT16_GET (header + 2);
1221 GST_LOG_OBJECT (rmdemux, "Got length %d", length);
1224 GST_LOG_OBJECT (rmdemux, "length too small, dropping");
1225 /* Invalid, just drop it */
1226 gst_adapter_flush (rmdemux->adapter, 4);
1230 avail = gst_adapter_available (rmdemux->adapter);
1234 GST_LOG_OBJECT (rmdemux, "we have %u available and we needed %d",
1237 /* flush version and length */
1238 gst_adapter_flush (rmdemux->adapter, 4);
1241 buffer = gst_adapter_take_buffer (rmdemux->adapter, length);
1243 ret = gst_rmdemux_parse_packet (rmdemux, buffer, version);
1244 rmdemux->chunk_index++;
1247 if (rmdemux->chunk_index == rmdemux->n_chunks || length == 0)
1248 rmdemux->state = RMDEMUX_STATE_HEADER;
1251 gst_adapter_flush (rmdemux->adapter, 2);
1253 if (rmdemux->data_offset == 0) {
1254 GST_LOG_OBJECT (rmdemux,
1255 "No further data, internal demux state EOS");
1256 rmdemux->state = RMDEMUX_STATE_EOS;
1258 rmdemux->state = RMDEMUX_STATE_HEADER;
1262 case RMDEMUX_STATE_EOS:
1263 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1266 GST_WARNING_OBJECT (rmdemux, "Unhandled state %d", rmdemux->state);
1275 static GstRMDemuxStream *
1276 gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux, int id)
1280 for (cur = rmdemux->streams; cur; cur = cur->next) {
1281 GstRMDemuxStream *stream = cur->data;
1283 if (stream->id == id) {
1292 gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event)
1296 for (cur = rmdemux->streams; cur; cur = cur->next) {
1297 GstRMDemuxStream *stream = cur->data;
1299 GST_DEBUG_OBJECT (rmdemux, "Pushing %s event on pad %s",
1300 GST_EVENT_TYPE_NAME (event), GST_PAD_NAME (stream->pad));
1302 switch (GST_EVENT_TYPE (event)) {
1303 case GST_EVENT_FLUSH_STOP:
1304 stream->last_ts = -1;
1305 stream->next_ts = -1;
1306 stream->last_seq = -1;
1307 stream->next_seq = -1;
1312 gst_event_ref (event);
1313 gst_pad_push_event (stream->pad, event);
1315 gst_event_unref (event);
1319 gst_rmdemux_add_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
1321 GstCaps *stream_caps = NULL;
1322 const gchar *codec_tag = NULL;
1323 gchar *codec_name = NULL;
1327 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
1328 char *name = g_strdup_printf ("video_%u", rmdemux->n_video_streams);
1331 gst_pad_new_from_static_template (&gst_rmdemux_videosrc_template, name);
1334 codec_tag = GST_TAG_VIDEO_CODEC;
1336 switch (stream->fourcc) {
1337 case GST_RM_VDO_RV10:
1340 case GST_RM_VDO_RV20:
1343 case GST_RM_VDO_RV30:
1346 case GST_RM_VDO_RV40:
1350 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1351 "fourcc", G_TYPE_UINT, stream->fourcc, NULL);
1352 GST_WARNING_OBJECT (rmdemux,
1353 "Unknown video FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1354 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1359 gst_caps_new_simple ("video/x-pn-realvideo", "rmversion", G_TYPE_INT,
1361 "format", G_TYPE_INT,
1362 (int) stream->format,
1363 "subformat", G_TYPE_INT, (int) stream->subformat, NULL);
1367 gst_caps_set_simple (stream_caps,
1368 "width", G_TYPE_INT, stream->width,
1369 "height", G_TYPE_INT, stream->height,
1370 "framerate", GST_TYPE_FRACTION, stream->framerate_numerator,
1371 stream->framerate_denominator, NULL);
1373 rmdemux->n_video_streams++;
1375 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
1376 char *name = g_strdup_printf ("audio_%u", rmdemux->n_audio_streams);
1379 gst_pad_new_from_static_template (&gst_rmdemux_audiosrc_template, name);
1380 GST_LOG_OBJECT (rmdemux, "Created audio pad \"%s\"", name);
1383 codec_tag = GST_TAG_AUDIO_CODEC;
1385 switch (stream->fourcc) {
1386 /* Older RealAudio Codecs */
1387 case GST_RM_AUD_14_4:
1391 case GST_RM_AUD_28_8:
1395 /* DolbyNet (Dolby AC3, low bitrate) */
1396 case GST_RM_AUD_DNET:
1398 gst_caps_new_simple ("audio/x-ac3", "rate", G_TYPE_INT,
1399 (int) stream->rate, NULL);
1400 stream->needs_descrambling = TRUE;
1401 stream->subpackets_needed = 1;
1402 stream->subpackets = NULL;
1406 case GST_RM_AUD_RAAC:
1407 case GST_RM_AUD_RACP:
1409 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
1410 (int) 4, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
1411 if (stream->extra_data_size > 0) {
1412 /* strip off an unknown byte in the extra data */
1413 stream->extra_data_size--;
1414 stream->extra_data++;
1416 stream->needs_descrambling = TRUE;
1417 stream->subpackets_needed = 1;
1418 stream->subpackets = NULL;
1422 case GST_RM_AUD_ATRC:
1423 stream_caps = gst_caps_new_empty_simple ("audio/x-vnd.sony.atrac3");
1424 stream->needs_descrambling = TRUE;
1425 stream->subpackets_needed = stream->height;
1426 stream->subpackets = NULL;
1429 /* RealAudio G2 audio */
1430 case GST_RM_AUD_COOK:
1432 stream->needs_descrambling = TRUE;
1433 stream->subpackets_needed = stream->height;
1434 stream->subpackets = NULL;
1437 /* RALF is lossless */
1438 case GST_RM_AUD_RALF:
1439 GST_DEBUG_OBJECT (rmdemux, "RALF");
1440 stream_caps = gst_caps_new_empty_simple ("audio/x-ralf-mpeg4-generic");
1443 case GST_RM_AUD_SIPR:
1445 if (stream->flavor > 3) {
1446 GST_WARNING_OBJECT (rmdemux, "bad SIPR flavor %d, freeing it",
1448 g_object_unref (stream->pad);
1449 gst_rmdemux_free_stream (rmdemux, stream);
1453 GST_DEBUG_OBJECT (rmdemux, "SIPR");
1454 stream_caps = gst_caps_new_empty_simple ("audio/x-sipro");
1455 stream->needs_descrambling = TRUE;
1456 stream->subpackets_needed = stream->height;
1457 stream->subpackets = NULL;
1458 stream->leaf_size = sipr_subpk_size[stream->flavor];
1463 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1464 "fourcc", G_TYPE_UINT, stream->fourcc, NULL);
1465 GST_WARNING_OBJECT (rmdemux,
1466 "Unknown audio FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1467 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1473 gst_caps_new_simple ("audio/x-pn-realaudio", "raversion", G_TYPE_INT,
1474 (int) version, NULL);
1478 gst_caps_set_simple (stream_caps,
1479 "flavor", G_TYPE_INT, (int) stream->flavor,
1480 "rate", G_TYPE_INT, (int) stream->rate,
1481 "channels", G_TYPE_INT, (int) stream->n_channels,
1482 "width", G_TYPE_INT, (int) stream->sample_width,
1483 "leaf_size", G_TYPE_INT, (int) stream->leaf_size,
1484 "packet_size", G_TYPE_INT, (int) stream->packet_size,
1485 "bitrate", G_TYPE_INT, (int) stream->bitrate,
1486 "height", G_TYPE_INT, (int) stream->height, NULL);
1488 rmdemux->n_audio_streams++;
1490 GST_WARNING_OBJECT (rmdemux, "not adding stream of type %d, freeing it",
1492 gst_rmdemux_free_stream (rmdemux, stream);
1496 GST_PAD_ELEMENT_PRIVATE (stream->pad) = stream;
1497 rmdemux->streams = g_slist_append (rmdemux->streams, stream);
1498 GST_LOG_OBJECT (rmdemux, "n_streams is now %d",
1499 g_slist_length (rmdemux->streams));
1501 GST_LOG ("stream->pad = %p, stream_caps = %" GST_PTR_FORMAT, stream->pad,
1504 if (stream->pad && stream_caps) {
1507 GST_LOG_OBJECT (rmdemux, "%d bytes of extra data for stream %s",
1508 stream->extra_data_size, GST_PAD_NAME (stream->pad));
1510 /* add codec_data if there is any */
1511 if (stream->extra_data_size > 0) {
1514 buffer = gst_buffer_new_and_alloc (stream->extra_data_size);
1515 gst_buffer_fill (buffer, 0, stream->extra_data, stream->extra_data_size);
1517 gst_caps_set_simple (stream_caps, "codec_data", GST_TYPE_BUFFER,
1520 gst_buffer_unref (buffer);
1523 gst_pad_use_fixed_caps (stream->pad);
1525 gst_pad_set_event_function (stream->pad,
1526 GST_DEBUG_FUNCPTR (gst_rmdemux_src_event));
1527 gst_pad_set_query_function (stream->pad,
1528 GST_DEBUG_FUNCPTR (gst_rmdemux_src_query));
1530 GST_DEBUG_OBJECT (rmdemux, "adding pad %s with caps %" GST_PTR_FORMAT
1531 ", stream_id=%d", GST_PAD_NAME (stream->pad), stream_caps, stream->id);
1532 gst_pad_set_active (stream->pad, TRUE);
1535 gst_pad_create_stream_id_printf (stream->pad,
1536 GST_ELEMENT_CAST (rmdemux), "%03u", stream->id);
1539 gst_pad_get_sticky_event (rmdemux->sinkpad, GST_EVENT_STREAM_START, 0);
1541 if (gst_event_parse_group_id (event, &rmdemux->group_id))
1542 rmdemux->have_group_id = TRUE;
1544 rmdemux->have_group_id = FALSE;
1545 gst_event_unref (event);
1546 } else if (!rmdemux->have_group_id) {
1547 rmdemux->have_group_id = TRUE;
1548 rmdemux->group_id = gst_util_group_id_next ();
1551 event = gst_event_new_stream_start (stream_id);
1552 if (rmdemux->have_group_id)
1553 gst_event_set_group_id (event, rmdemux->group_id);
1555 gst_pad_push_event (stream->pad, event);
1558 gst_pad_set_caps (stream->pad, stream_caps);
1560 codec_name = gst_pb_utils_get_codec_description (stream_caps);
1562 /* save for later, we must send the tags after the newsegment event */
1563 if (codec_tag != NULL && codec_name != NULL) {
1564 if (stream->pending_tags == NULL)
1565 stream->pending_tags = gst_tag_list_new_empty ();
1566 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_KEEP,
1567 codec_tag, codec_name, NULL);
1568 g_free (codec_name);
1570 gst_element_add_pad (GST_ELEMENT_CAST (rmdemux), stream->pad);
1571 gst_flow_combiner_add_pad (rmdemux->flowcombiner, stream->pad);
1577 gst_caps_unref (stream_caps);
1581 re_skip_pascal_string (const guint8 * ptr)
1591 gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data, int length)
1593 GST_LOG_OBJECT (rmdemux, "file_version: %d", RMDEMUX_GUINT32_GET (data));
1594 GST_LOG_OBJECT (rmdemux, "num_headers: %d", RMDEMUX_GUINT32_GET (data + 4));
1598 gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data, int length)
1600 GST_LOG_OBJECT (rmdemux, "max bitrate: %d", RMDEMUX_GUINT32_GET (data));
1601 GST_LOG_OBJECT (rmdemux, "avg bitrate: %d", RMDEMUX_GUINT32_GET (data + 4));
1602 GST_LOG_OBJECT (rmdemux, "max packet size: %d",
1603 RMDEMUX_GUINT32_GET (data + 8));
1604 rmdemux->avg_packet_size = RMDEMUX_GUINT32_GET (data + 12);
1605 GST_LOG_OBJECT (rmdemux, "avg packet size: %d", rmdemux->avg_packet_size);
1606 rmdemux->num_packets = RMDEMUX_GUINT32_GET (data + 16);
1607 GST_LOG_OBJECT (rmdemux, "number of packets: %d", rmdemux->num_packets);
1609 GST_LOG_OBJECT (rmdemux, "duration: %d", RMDEMUX_GUINT32_GET (data + 20));
1610 rmdemux->duration = RMDEMUX_GUINT32_GET (data + 20) * GST_MSECOND;
1612 GST_LOG_OBJECT (rmdemux, "preroll: %d", RMDEMUX_GUINT32_GET (data + 24));
1613 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 28);
1614 GST_LOG_OBJECT (rmdemux, "offset of INDX section: 0x%08x",
1615 rmdemux->index_offset);
1616 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 32);
1617 GST_LOG_OBJECT (rmdemux, "offset of DATA section: 0x%08x",
1618 rmdemux->data_offset);
1619 GST_LOG_OBJECT (rmdemux, "n streams: %d", RMDEMUX_GUINT16_GET (data + 36));
1620 GST_LOG_OBJECT (rmdemux, "flags: 0x%04x", RMDEMUX_GUINT16_GET (data + 38));
1624 gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux, const guint8 * data, int length)
1626 GstRMDemuxStream *stream;
1627 char *stream1_type_string;
1628 char *stream2_type_string;
1632 guint32 max_bitrate;
1633 guint32 avg_bitrate;
1635 stream = g_new0 (GstRMDemuxStream, 1);
1637 stream->id = RMDEMUX_GUINT16_GET (data);
1638 stream->index = NULL;
1639 stream->seek_offset = 0;
1640 stream->last_ts = -1;
1641 stream->next_ts = -1;
1642 stream->discont = TRUE;
1643 stream->adapter = gst_adapter_new ();
1644 GST_LOG_OBJECT (rmdemux, "stream_number=%d", stream->id);
1646 /* parse the bitrates */
1647 max_bitrate = RMDEMUX_GUINT32_GET (data + 2);
1648 avg_bitrate = RMDEMUX_GUINT32_GET (data + 6);
1649 stream->bitrate = avg_bitrate;
1650 GST_LOG_OBJECT (rmdemux, "Stream max bitrate=%u", max_bitrate);
1651 GST_LOG_OBJECT (rmdemux, "Stream avg bitrate=%u", avg_bitrate);
1652 if (max_bitrate != 0) {
1653 if (stream->pending_tags == NULL)
1654 stream->pending_tags = gst_tag_list_new_empty ();
1655 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1656 GST_TAG_MAXIMUM_BITRATE, max_bitrate, NULL);
1658 if (avg_bitrate != 0) {
1659 if (stream->pending_tags == NULL)
1660 stream->pending_tags = gst_tag_list_new_empty ();
1661 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1662 GST_TAG_BITRATE, avg_bitrate, NULL);
1666 stream1_type_string = gst_rm_utils_read_string8 (data + offset,
1667 length - offset, &str_len);
1669 stream2_type_string = gst_rm_utils_read_string8 (data + offset,
1670 length - offset, &str_len);
1673 /* stream1_type_string for audio and video stream is a "put_whatever_you_want" field :
1675 * - "[The ]Video/Audio Stream" (File produced by an official Real encoder)
1676 * - "RealVideoPremierePlugIn-VIDEO/AUDIO" (File produced by Abobe Premiere)
1678 * so, we should not rely on it to know which stream type it is
1681 GST_LOG_OBJECT (rmdemux, "stream type: %s", stream1_type_string);
1682 GST_LOG_OBJECT (rmdemux, "MIME type=%s", stream2_type_string);
1684 if (strcmp (stream2_type_string, "video/x-pn-realvideo") == 0) {
1685 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1686 } else if (strcmp (stream2_type_string,
1687 "video/x-pn-multirate-realvideo") == 0) {
1688 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1689 } else if (strcmp (stream2_type_string, "audio/x-pn-realaudio") == 0) {
1690 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1691 } else if (strcmp (stream2_type_string,
1692 "audio/x-pn-multirate-realaudio") == 0) {
1693 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1694 } else if (strcmp (stream2_type_string,
1695 "audio/x-pn-multirate-realaudio-live") == 0) {
1696 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1697 } else if (strcmp (stream2_type_string, "audio/x-ralf-mpeg4-generic") == 0) {
1698 /* Another audio type found in the real testsuite */
1699 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1700 } else if (strcmp (stream1_type_string, "") == 0 &&
1701 strcmp (stream2_type_string, "logical-fileinfo") == 0) {
1702 stream_type = GST_RMDEMUX_STREAM_FILEINFO;
1704 stream_type = GST_RMDEMUX_STREAM_UNKNOWN;
1705 GST_WARNING_OBJECT (rmdemux, "unknown stream type \"%s\",\"%s\"",
1706 stream1_type_string, stream2_type_string);
1708 g_free (stream1_type_string);
1709 g_free (stream2_type_string);
1713 stream->subtype = stream_type;
1714 switch (stream_type) {
1716 case GST_RMDEMUX_STREAM_VIDEO:
1717 /* RV10/RV20/RV30/RV40 => video/x-pn-realvideo, version=1,2,3,4 */
1718 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 8);
1719 stream->width = RMDEMUX_GUINT16_GET (data + offset + 12);
1720 stream->height = RMDEMUX_GUINT16_GET (data + offset + 14);
1721 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 16);
1722 stream->subformat = RMDEMUX_GUINT32_GET (data + offset + 26);
1723 stream->format = RMDEMUX_GUINT32_GET (data + offset + 30);
1724 stream->extra_data_size = length - (offset + 26);
1725 stream->extra_data = (guint8 *) data + offset + 26;
1726 /* Natural way to represent framerates here requires unsigned 32 bit
1727 * numerator, which we don't have. For the nasty case, approximate...
1730 guint32 numerator = RMDEMUX_GUINT16_GET (data + offset + 22) * 65536 +
1731 RMDEMUX_GUINT16_GET (data + offset + 24);
1732 if (numerator > G_MAXINT) {
1733 stream->framerate_numerator = (gint) (numerator >> 1);
1734 stream->framerate_denominator = 32768;
1736 stream->framerate_numerator = (gint) numerator;
1737 stream->framerate_denominator = 65536;
1741 GST_DEBUG_OBJECT (rmdemux,
1742 "Video stream with fourcc=%" GST_FOURCC_FORMAT
1743 " width=%d height=%d rate=%d framerate=%d/%d subformat=%x format=%x extra_data_size=%d",
1744 GST_FOURCC_ARGS (stream->fourcc), stream->width, stream->height,
1745 stream->rate, stream->framerate_numerator,
1746 stream->framerate_denominator, stream->subformat, stream->format,
1747 stream->extra_data_size);
1749 case GST_RMDEMUX_STREAM_AUDIO:{
1750 stream->version = RMDEMUX_GUINT16_GET (data + offset + 4);
1751 GST_INFO ("stream version = %u", stream->version);
1752 switch (stream->version) {
1754 stream->fourcc = GST_RM_AUD_14_4;
1755 stream->packet_size = 20;
1756 stream->rate = 8000;
1757 stream->n_channels = 1;
1758 stream->sample_width = 16;
1760 stream->leaf_size = 0;
1764 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1765 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1766 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1767 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1768 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1769 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 48);
1770 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 52);
1771 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 54);
1772 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 62);
1773 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 69);
1774 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1775 stream->extra_data_size);
1776 if (length - (offset + 73) >= stream->extra_data_size) {
1777 stream->extra_data = (guint8 *) data + offset + 73;
1779 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1780 stream->extra_data_size = 0;
1784 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1785 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1786 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1787 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1788 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1789 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 54);
1790 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 58);
1791 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 60);
1792 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 66);
1793 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 74);
1794 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1795 stream->extra_data_size);
1796 if (length - (offset + 78) >= stream->extra_data_size) {
1797 stream->extra_data = (guint8 *) data + offset + 78;
1799 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1800 stream->extra_data_size = 0;
1804 GST_WARNING_OBJECT (rmdemux, "Unhandled audio stream version %d",
1809 /* 14_4, 28_8, cook, dnet, sipr, raac, racp, ralf, atrc */
1810 GST_DEBUG_OBJECT (rmdemux,
1811 "Audio stream with rate=%d sample_width=%d n_channels=%d",
1812 stream->rate, stream->sample_width, stream->n_channels);
1816 case GST_RMDEMUX_STREAM_FILEINFO:
1820 /* Length of this section */
1821 GST_DEBUG_OBJECT (rmdemux, "length2: 0x%08x",
1822 RMDEMUX_GUINT32_GET (data + offset));
1825 /* Unknown : 00 00 00 00 */
1828 /* Number of variables that would follow (loop iterations) */
1829 element_nb = RMDEMUX_GUINT32_GET (data + offset);
1832 while (element_nb) {
1833 /* Category Id : 00 00 00 XX 00 00 */
1837 offset += re_skip_pascal_string (data + offset);
1839 /* Variable Value Type */
1840 /* 00 00 00 00 00 => integer/boolean, preceded by length */
1841 /* 00 00 00 02 00 => pascal string, preceded by length, no trailing \0 */
1844 /* Variable Value */
1845 offset += re_skip_pascal_string (data + offset);
1851 case GST_RMDEMUX_STREAM_UNKNOWN:
1856 gst_rmdemux_add_stream (rmdemux, stream);
1860 gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data, int length)
1865 n = RMDEMUX_GUINT32_GET (data);
1866 id = RMDEMUX_GUINT16_GET (data + 4);
1867 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 6);
1869 GST_DEBUG_OBJECT (rmdemux, "Number of indices=%d Stream ID=%d length=%d", n,
1872 /* Point to the next index_stream */
1873 rmdemux->index_stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
1875 /* Return the length of the index */
1880 gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux, const guint8 * data,
1885 GstRMDemuxIndex *index;
1887 /* The number of index records */
1890 if (rmdemux->index_stream == NULL)
1893 /* don't parse the index a second time when operating pull-based and
1894 * reaching the end of the file */
1895 if (rmdemux->index_stream->index_length > 0) {
1896 GST_DEBUG_OBJECT (rmdemux, "Already have an index for this stream");
1900 index = g_malloc (sizeof (GstRMDemuxIndex) * n);
1901 rmdemux->index_stream->index = index;
1902 rmdemux->index_stream->index_length = n;
1904 for (i = 0; i < n; i++) {
1905 index[i].timestamp = RMDEMUX_GUINT32_GET (data + 2) * GST_MSECOND;
1906 index[i].offset = RMDEMUX_GUINT32_GET (data + 6);
1908 GST_DEBUG_OBJECT (rmdemux, "Index found for timestamp=%f (at offset=%x)",
1909 gst_guint64_to_gdouble (index[i].timestamp) / GST_SECOND,
1916 gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data, int length)
1918 rmdemux->n_chunks = RMDEMUX_GUINT32_GET (data);
1919 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 4);
1920 rmdemux->chunk_index = 0;
1921 GST_DEBUG_OBJECT (rmdemux, "Data chunk found with %d packets "
1922 "(next data at 0x%08x)", rmdemux->n_chunks, rmdemux->data_offset);
1926 gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data, int length)
1930 tags = gst_rm_utils_read_tags (data, length, gst_rm_utils_read_string16);
1933 GstTagList *old_tags = rmdemux->pending_tags;
1935 GST_LOG_OBJECT (rmdemux, "tags: %" GST_PTR_FORMAT, tags);
1937 rmdemux->pending_tags =
1938 gst_tag_list_merge (old_tags, tags, GST_TAG_MERGE_APPEND);
1940 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);