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., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, 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 typedef struct _GstRMDemuxIndex GstRMDemuxIndex;
47 struct _GstRMDemuxStream
56 GstFlowReturn last_flow;
61 GstRMDemuxIndex *index;
63 gint framerate_numerator;
64 gint framerate_denominator;
70 guint16 rate; /* samplerate */
71 guint16 n_channels; /* channels */
72 guint16 sample_width; /* bits_per_sample */
73 guint16 leaf_size; /* subpacket_size */
74 guint32 packet_size; /* coded_frame_size */
76 guint32 extra_data_size; /* codec_data_length */
77 guint8 *extra_data; /* extras */
79 gboolean needs_descrambling;
80 guint subpackets_needed; /* subpackets needed for descrambling */
81 GPtrArray *subpackets; /* array containing subpacket GstBuffers */
83 /* Variables needed for fixing timestamps. */
84 GstClockTime next_ts, last_ts;
85 guint16 next_seq, last_seq;
92 guint frag_offset[MAX_FRAGS];
95 GstTagList *pending_tags;
98 struct _GstRMDemuxIndex
101 GstClockTime timestamp;
104 static GstStaticPadTemplate gst_rmdemux_sink_template =
105 GST_STATIC_PAD_TEMPLATE ("sink",
108 GST_STATIC_CAPS ("application/vnd.rn-realmedia")
111 static GstStaticPadTemplate gst_rmdemux_videosrc_template =
112 GST_STATIC_PAD_TEMPLATE ("video_%02d",
115 GST_STATIC_CAPS_ANY);
117 static GstStaticPadTemplate gst_rmdemux_audiosrc_template =
118 GST_STATIC_PAD_TEMPLATE ("audio_%02d",
121 GST_STATIC_CAPS_ANY);
123 GST_DEBUG_CATEGORY_STATIC (rmdemux_debug);
124 #define GST_CAT_DEFAULT rmdemux_debug
126 static GstElementClass *parent_class = NULL;
128 static void gst_rmdemux_class_init (GstRMDemuxClass * klass);
129 static void gst_rmdemux_base_init (GstRMDemuxClass * klass);
130 static void gst_rmdemux_init (GstRMDemux * rmdemux);
131 static void gst_rmdemux_finalize (GObject * object);
132 static GstStateChangeReturn gst_rmdemux_change_state (GstElement * element,
133 GstStateChange transition);
134 static GstFlowReturn gst_rmdemux_chain (GstPad * pad, GstBuffer * buffer);
135 static void gst_rmdemux_loop (GstPad * pad);
136 static gboolean gst_rmdemux_sink_activate (GstPad * sinkpad);
137 static gboolean gst_rmdemux_sink_activate_push (GstPad * sinkpad,
139 static gboolean gst_rmdemux_sink_activate_pull (GstPad * sinkpad,
141 static gboolean gst_rmdemux_sink_event (GstPad * pad, GstEvent * event);
142 static gboolean gst_rmdemux_src_event (GstPad * pad, GstEvent * event);
143 static void gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event);
144 static const GstQueryType *gst_rmdemux_src_query_types (GstPad * pad);
145 static gboolean gst_rmdemux_src_query (GstPad * pad, GstQuery * query);
146 static gboolean gst_rmdemux_perform_seek (GstRMDemux * rmdemux,
149 static void gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data,
151 static void gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data,
153 static void gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux,
154 const guint8 * data, int length);
155 static guint gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data,
157 static void gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data,
159 static void gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data,
161 static GstFlowReturn gst_rmdemux_parse_packet (GstRMDemux * rmdemux,
162 GstBuffer * in, guint16 version);
163 static void gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux,
164 const guint8 * data, int length);
165 static void gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
166 GstRMDemuxStream * stream);
167 static GstRMDemuxStream *gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux,
171 gst_rmdemux_get_type (void)
173 static GType rmdemux_type = 0;
176 static const GTypeInfo rmdemux_info = {
177 sizeof (GstRMDemuxClass),
178 (GBaseInitFunc) gst_rmdemux_base_init, NULL,
179 (GClassInitFunc) gst_rmdemux_class_init,
180 NULL, NULL, sizeof (GstRMDemux), 0,
181 (GInstanceInitFunc) gst_rmdemux_init,
185 g_type_register_static (GST_TYPE_ELEMENT, "GstRMDemux", &rmdemux_info,
192 gst_rmdemux_base_init (GstRMDemuxClass * klass)
194 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
196 gst_element_class_add_pad_template (element_class,
197 gst_static_pad_template_get (&gst_rmdemux_sink_template));
198 gst_element_class_add_pad_template (element_class,
199 gst_static_pad_template_get (&gst_rmdemux_videosrc_template));
200 gst_element_class_add_pad_template (element_class,
201 gst_static_pad_template_get (&gst_rmdemux_audiosrc_template));
202 gst_element_class_set_details_simple (element_class, "RealMedia Demuxer",
204 "Demultiplex a RealMedia file into audio and video streams",
205 "David Schleef <ds@schleef.org>");
209 gst_rmdemux_class_init (GstRMDemuxClass * klass)
211 GObjectClass *gobject_class;
212 GstElementClass *gstelement_class;
214 gobject_class = (GObjectClass *) klass;
215 gstelement_class = (GstElementClass *) klass;
217 parent_class = g_type_class_peek_parent (klass);
219 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rmdemux_change_state);
221 GST_DEBUG_CATEGORY_INIT (rmdemux_debug, "rmdemux",
222 0, "Demuxer for Realmedia streams");
224 gobject_class->finalize = gst_rmdemux_finalize;
228 gst_rmdemux_finalize (GObject * object)
230 GstRMDemux *rmdemux = GST_RMDEMUX (object);
232 if (rmdemux->adapter) {
233 g_object_unref (rmdemux->adapter);
234 rmdemux->adapter = NULL;
237 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
241 gst_rmdemux_init (GstRMDemux * rmdemux)
244 gst_pad_new_from_static_template (&gst_rmdemux_sink_template, "sink");
245 gst_pad_set_event_function (rmdemux->sinkpad,
246 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_event));
247 gst_pad_set_chain_function (rmdemux->sinkpad,
248 GST_DEBUG_FUNCPTR (gst_rmdemux_chain));
249 gst_pad_set_activate_function (rmdemux->sinkpad,
250 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate));
251 gst_pad_set_activatepull_function (rmdemux->sinkpad,
252 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate_pull));
253 gst_pad_set_activatepush_function (rmdemux->sinkpad,
254 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate_push));
256 gst_element_add_pad (GST_ELEMENT (rmdemux), rmdemux->sinkpad);
258 rmdemux->adapter = gst_adapter_new ();
259 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
260 rmdemux->base_ts = GST_CLOCK_TIME_NONE;
261 rmdemux->need_newsegment = TRUE;
265 gst_rmdemux_sink_event (GstPad * pad, GstEvent * event)
270 rmdemux = GST_RMDEMUX (gst_pad_get_parent (pad));
272 GST_LOG_OBJECT (pad, "%s event", GST_EVENT_TYPE_NAME (event));
274 switch (GST_EVENT_TYPE (event)) {
275 case GST_EVENT_NEWSEGMENT:
276 gst_event_unref (event);
280 ret = gst_pad_event_default (pad, event);
284 gst_object_unref (rmdemux);
289 gst_rmdemux_src_event (GstPad * pad, GstEvent * event)
293 GstRMDemux *rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
295 GST_LOG_OBJECT (rmdemux, "handling src event");
297 switch (GST_EVENT_TYPE (event)) {
302 GST_LOG_OBJECT (rmdemux, "Event on src: SEEK");
303 /* can't seek if we are not seekable, FIXME could pass the
304 * seek query upstream after converting it to bytes using
305 * the average bitrate of the stream. */
306 if (!rmdemux->seekable) {
308 GST_DEBUG ("seek on non seekable stream");
312 GST_OBJECT_LOCK (rmdemux);
313 /* check if we can do the seek now */
314 running = rmdemux->running;
315 GST_OBJECT_UNLOCK (rmdemux);
317 /* now do the seek */
319 ret = gst_rmdemux_perform_seek (rmdemux, event);
323 gst_event_unref (event);
327 GST_LOG_OBJECT (rmdemux, "Event on src: type=%d", GST_EVENT_TYPE (event));
328 ret = gst_pad_event_default (pad, event);
335 GST_DEBUG ("error handling event");
336 gst_event_unref (event);
340 /* Validate that this looks like a reasonable point to seek to */
342 gst_rmdemux_validate_offset (GstRMDemux * rmdemux)
345 GstFlowReturn flowret;
346 guint16 version, length;
349 flowret = gst_pad_pull_range (rmdemux->sinkpad, rmdemux->offset, 4, &buffer);
351 if (flowret != GST_FLOW_OK) {
352 GST_DEBUG_OBJECT (rmdemux, "Failed to pull data at offset %d",
356 /* TODO: Can we also be seeking to a 'DATA' chunk header? Check this.
357 * Also, for the case we currently handle, can we check any more? It's pretty
358 * sucky to not be validating a little more heavily than this... */
359 /* This should now be the start of a data packet header. That begins with
360 * a 2-byte 'version' field, which has to be 0 or 1, then a length. I'm not
361 * certain what values are valid for length, but it must always be at least
362 * 4 bytes, and we can check that it won't take us past our known total size
365 version = RMDEMUX_GUINT16_GET (GST_BUFFER_DATA (buffer));
366 if (version != 0 && version != 1) {
367 GST_DEBUG_OBJECT (rmdemux, "Expected version 0 or 1, got %d",
372 length = RMDEMUX_GUINT16_GET (GST_BUFFER_DATA (buffer) + 2);
373 /* TODO: Also check against total stream length */
375 GST_DEBUG_OBJECT (rmdemux, "Expected length >= 4, got %d", (int) length);
380 rmdemux->offset += 4;
381 gst_adapter_clear (rmdemux->adapter);
382 gst_adapter_push (rmdemux->adapter, buffer);
384 GST_WARNING_OBJECT (rmdemux, "Failed to validate seek offset at %d",
392 find_seek_offset_bytes (GstRMDemux * rmdemux, guint target)
396 gboolean ret = FALSE;
401 for (cur = rmdemux->streams; cur; cur = cur->next) {
402 GstRMDemuxStream *stream = cur->data;
404 /* Search backwards through this stream's index until we find the first
405 * timestamp before our target time */
406 for (i = stream->index_length - 1; i >= 0; i--) {
407 if (stream->index[i].offset <= target) {
408 /* Set the seek_offset for the stream so we don't bother parsing it
409 * until we've passed that point */
410 stream->seek_offset = stream->index[i].offset;
411 rmdemux->offset = stream->index[i].offset;
421 find_seek_offset_time (GstRMDemux * rmdemux, GstClockTime time)
424 gboolean ret = FALSE;
426 GstClockTime earliest = GST_CLOCK_TIME_NONE;
429 for (cur = rmdemux->streams; cur; cur = cur->next, n_stream++) {
430 GstRMDemuxStream *stream = cur->data;
432 /* Search backwards through this stream's index until we find the first
433 * timestamp before our target time */
434 for (i = stream->index_length - 1; i >= 0; i--) {
435 if (stream->index[i].timestamp <= time) {
436 /* Set the seek_offset for the stream so we don't bother parsing it
437 * until we've passed that point */
438 stream->seek_offset = stream->index[i].offset;
440 /* If it's also the earliest timestamp we've seen of all streams, then
443 if (earliest == GST_CLOCK_TIME_NONE ||
444 stream->index[i].timestamp < earliest) {
445 earliest = stream->index[i].timestamp;
446 rmdemux->offset = stream->index[i].offset;
447 GST_DEBUG_OBJECT (rmdemux,
448 "We're looking for %" GST_TIME_FORMAT
449 " and we found that stream %d has the latest index at %"
450 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start), n_stream,
451 GST_TIME_ARGS (earliest));
459 stream->discont = TRUE;
465 gst_rmdemux_perform_seek (GstRMDemux * rmdemux, GstEvent * event)
473 GstSeekType cur_type, stop_type;
478 GST_DEBUG_OBJECT (rmdemux, "seek with event");
480 gst_event_parse_seek (event, &rate, &format, &flags,
481 &cur_type, &cur, &stop_type, &stop);
483 /* we can only seek on time */
484 if (format != GST_FORMAT_TIME) {
485 GST_DEBUG_OBJECT (rmdemux, "can only seek on TIME");
488 /* cannot yet do backwards playback */
490 GST_DEBUG_OBJECT (rmdemux, "can only seek with positive rate, not %lf",
495 GST_DEBUG_OBJECT (rmdemux, "seek without event");
501 GST_DEBUG_OBJECT (rmdemux, "seek, rate %g", rate);
503 flush = flags & GST_SEEK_FLAG_FLUSH;
505 /* first step is to unlock the streaming thread if it is
506 * blocked in a chain call, we do this by starting the flush. */
508 gst_pad_push_event (rmdemux->sinkpad, gst_event_new_flush_start ());
509 gst_rmdemux_send_event (rmdemux, gst_event_new_flush_start ());
511 gst_pad_pause_task (rmdemux->sinkpad);
514 GST_LOG_OBJECT (rmdemux, "Done starting flushes");
516 /* now grab the stream lock so that streaming cannot continue, for
517 * non flushing seeks when the element is in PAUSED this could block
519 GST_PAD_STREAM_LOCK (rmdemux->sinkpad);
521 GST_LOG_OBJECT (rmdemux, "Took streamlock");
523 /* close current segment first */
524 if (rmdemux->segment_running && !flush) {
527 newseg = gst_event_new_new_segment (TRUE, rmdemux->segment.rate,
528 GST_FORMAT_TIME, rmdemux->segment.start,
529 rmdemux->segment.last_stop, rmdemux->segment.time);
531 gst_rmdemux_send_event (rmdemux, newseg);
535 gst_segment_set_seek (&rmdemux->segment, rate, format, flags,
536 cur_type, cur, stop_type, stop, &update);
539 GST_DEBUG_OBJECT (rmdemux, "segment positions set to %" GST_TIME_FORMAT "-%"
540 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start),
541 GST_TIME_ARGS (rmdemux->segment.stop));
543 /* we need to stop flushing on the sinkpad as we're going to use it
544 * next. We can do this as we have the STREAM lock now. */
545 gst_pad_push_event (rmdemux->sinkpad, gst_event_new_flush_stop ());
547 GST_LOG_OBJECT (rmdemux, "Pushed FLUSH_STOP event");
549 /* For each stream, find the first index offset equal to or before our seek
550 * target. Of these, find the smallest offset. That's where we seek to.
552 * Then we pull 4 bytes from that offset, and validate that we've seeked to a
553 * what looks like a plausible packet.
554 * If that fails, restart, with the seek target set to one less than the
555 * offset we just tried. If we run out of places to try, treat that as a fatal
558 if (!find_seek_offset_time (rmdemux, rmdemux->segment.last_stop)) {
559 GST_LOG_OBJECT (rmdemux, "Failed to find seek offset by time");
564 GST_LOG_OBJECT (rmdemux, "Validating offset %u", rmdemux->offset);
565 validated = gst_rmdemux_validate_offset (rmdemux);
567 GST_INFO_OBJECT (rmdemux, "Failed to validate offset at %u",
569 if (!find_seek_offset_bytes (rmdemux, rmdemux->offset - 1)) {
573 validated = gst_rmdemux_validate_offset (rmdemux);
576 GST_LOG_OBJECT (rmdemux, "Found final offset. Excellent!");
578 /* now we have a new position, prepare for streaming again */
580 /* Reset the demuxer state */
581 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
584 gst_rmdemux_send_event (rmdemux, gst_event_new_flush_stop ());
586 /* must send newsegment event from streaming thread, so just set flag */
587 rmdemux->need_newsegment = TRUE;
589 /* notify start of new segment */
590 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
591 gst_element_post_message (GST_ELEMENT_CAST (rmdemux),
592 gst_message_new_segment_start (GST_OBJECT_CAST (rmdemux),
593 GST_FORMAT_TIME, rmdemux->segment.last_stop));
596 /* restart our task since it might have been stopped when we did the
598 gst_pad_start_task (rmdemux->sinkpad, (GstTaskFunction) gst_rmdemux_loop,
603 /* streaming can continue now */
604 GST_PAD_STREAM_UNLOCK (rmdemux->sinkpad);
610 GST_DEBUG_OBJECT (rmdemux, "seek failed");
617 gst_rmdemux_src_query (GstPad * pad, GstQuery * query)
619 gboolean res = FALSE;
622 rmdemux = GST_RMDEMUX (gst_pad_get_parent (pad));
624 switch (GST_QUERY_TYPE (query)) {
625 case GST_QUERY_POSITION:
626 GST_DEBUG_OBJECT (rmdemux, "Position query: no idea from demuxer!");
628 case GST_QUERY_DURATION:{
631 gst_query_parse_duration (query, &fmt, NULL);
632 if (fmt == GST_FORMAT_TIME) {
633 GST_OBJECT_LOCK (rmdemux);
634 if (G_LIKELY (rmdemux->running)) {
635 gst_query_set_duration (query, GST_FORMAT_TIME, rmdemux->duration);
636 GST_DEBUG_OBJECT (rmdemux, "duration set to %" GST_TIME_FORMAT,
637 GST_TIME_ARGS (rmdemux->duration));
640 GST_OBJECT_UNLOCK (rmdemux);
644 case GST_QUERY_SEEKING:{
647 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
648 if (fmt == GST_FORMAT_TIME) {
649 GST_OBJECT_LOCK (rmdemux);
650 if (G_LIKELY (rmdemux->running)) {
651 gst_query_set_seeking (query, GST_FORMAT_TIME, rmdemux->seekable,
652 0, rmdemux->duration);
655 GST_OBJECT_UNLOCK (rmdemux);
660 res = gst_pad_query_default (pad, query);
664 gst_object_unref (rmdemux);
668 static const GstQueryType *
669 gst_rmdemux_src_query_types (GstPad * pad)
671 static const GstQueryType query_types[] = {
682 gst_rmdemux_reset (GstRMDemux * rmdemux)
686 GST_OBJECT_LOCK (rmdemux);
687 rmdemux->running = FALSE;
688 GST_OBJECT_UNLOCK (rmdemux);
690 for (cur = rmdemux->streams; cur; cur = cur->next) {
691 GstRMDemuxStream *stream = cur->data;
693 g_object_unref (stream->adapter);
694 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
695 gst_element_remove_pad (GST_ELEMENT (rmdemux), stream->pad);
696 if (stream->pending_tags)
697 gst_tag_list_free (stream->pending_tags);
698 if (stream->subpackets)
699 g_ptr_array_free (stream->subpackets, TRUE);
700 g_free (stream->index);
703 g_slist_free (rmdemux->streams);
704 rmdemux->streams = NULL;
705 rmdemux->n_audio_streams = 0;
706 rmdemux->n_video_streams = 0;
708 gst_adapter_clear (rmdemux->adapter);
709 rmdemux->state = RMDEMUX_STATE_HEADER;
710 rmdemux->have_pads = FALSE;
712 gst_segment_init (&rmdemux->segment, GST_FORMAT_UNDEFINED);
713 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
714 rmdemux->base_ts = GST_CLOCK_TIME_NONE;
715 rmdemux->need_newsegment = TRUE;
718 static GstStateChangeReturn
719 gst_rmdemux_change_state (GstElement * element, GstStateChange transition)
721 GstRMDemux *rmdemux = GST_RMDEMUX (element);
722 GstStateChangeReturn res;
724 switch (transition) {
725 case GST_STATE_CHANGE_NULL_TO_READY:
727 case GST_STATE_CHANGE_READY_TO_PAUSED:
728 rmdemux->state = RMDEMUX_STATE_HEADER;
729 rmdemux->have_pads = FALSE;
730 gst_segment_init (&rmdemux->segment, GST_FORMAT_TIME);
731 rmdemux->running = FALSE;
733 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
739 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
741 switch (transition) {
742 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
744 case GST_STATE_CHANGE_PAUSED_TO_READY:{
745 gst_rmdemux_reset (rmdemux);
748 case GST_STATE_CHANGE_READY_TO_NULL:
757 /* this function is called when the pad is activated and should start
760 * We check if we can do random access to decide if we work push or
764 gst_rmdemux_sink_activate (GstPad * sinkpad)
766 if (gst_pad_check_pull_range (sinkpad)) {
767 return gst_pad_activate_pull (sinkpad, TRUE);
769 return gst_pad_activate_push (sinkpad, TRUE);
773 /* this function gets called when we activate ourselves in push mode.
774 * We cannot seek (ourselves) in the stream */
776 gst_rmdemux_sink_activate_push (GstPad * pad, gboolean active)
780 rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
782 GST_DEBUG_OBJECT (rmdemux, "activate_push");
784 rmdemux->seekable = FALSE;
789 /* this function gets called when we activate ourselves in pull mode.
790 * We can perform random access to the resource and we start a task
791 * to start reading */
793 gst_rmdemux_sink_activate_pull (GstPad * pad, gboolean active)
797 rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
799 GST_DEBUG_OBJECT (rmdemux, "activate_pull");
802 rmdemux->seekable = TRUE;
804 rmdemux->loop_state = RMDEMUX_LOOP_STATE_HEADER;
805 rmdemux->data_offset = G_MAXUINT;
807 return gst_pad_start_task (pad, (GstTaskFunction) gst_rmdemux_loop, pad);
809 return gst_pad_stop_task (pad);
813 /* random access mode - just pass over to our chain function */
815 gst_rmdemux_loop (GstPad * pad)
819 GstFlowReturn ret = GST_FLOW_OK;
822 rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
824 GST_LOG_OBJECT (rmdemux, "loop with state=%d and offset=0x%x",
825 rmdemux->loop_state, rmdemux->offset);
827 switch (rmdemux->state) {
828 case RMDEMUX_STATE_HEADER:
831 case RMDEMUX_STATE_HEADER_DATA:
834 case RMDEMUX_STATE_DATA_PACKET:
835 size = rmdemux->avg_packet_size;
837 case RMDEMUX_STATE_EOS:
838 GST_LOG_OBJECT (rmdemux, "At EOS, pausing task");
839 ret = GST_FLOW_UNEXPECTED;
842 GST_LOG_OBJECT (rmdemux, "Default: requires %d bytes (state is %d)",
843 (int) rmdemux->size, rmdemux->state);
844 size = rmdemux->size;
847 ret = gst_pad_pull_range (pad, rmdemux->offset, size, &buffer);
848 if (ret != GST_FLOW_OK) {
849 if (rmdemux->offset == rmdemux->index_offset) {
850 /* The index isn't available so forget about it */
851 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
852 rmdemux->offset = rmdemux->data_offset;
853 GST_OBJECT_LOCK (rmdemux);
854 rmdemux->running = TRUE;
855 rmdemux->seekable = FALSE;
856 GST_OBJECT_UNLOCK (rmdemux);
859 GST_DEBUG_OBJECT (rmdemux, "Unable to pull %d bytes at offset 0x%08x "
860 "(pull_range returned flow %s, state is %d)", (gint) size,
861 rmdemux->offset, gst_flow_get_name (ret), GST_STATE (rmdemux));
866 size = GST_BUFFER_SIZE (buffer);
868 /* Defer to the chain function */
869 ret = gst_rmdemux_chain (pad, buffer);
870 if (ret != GST_FLOW_OK) {
871 GST_DEBUG_OBJECT (rmdemux, "Chain flow failed at offset 0x%08x",
876 rmdemux->offset += size;
878 switch (rmdemux->loop_state) {
879 case RMDEMUX_LOOP_STATE_HEADER:
880 if (rmdemux->offset >= rmdemux->data_offset) {
881 /* It's the end of the header */
882 rmdemux->loop_state = RMDEMUX_LOOP_STATE_INDEX;
883 rmdemux->offset = rmdemux->index_offset;
886 case RMDEMUX_LOOP_STATE_INDEX:
887 if (rmdemux->state == RMDEMUX_STATE_HEADER) {
888 if (rmdemux->index_offset == 0) {
889 /* We've read the last index */
890 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
891 rmdemux->offset = rmdemux->data_offset;
892 GST_OBJECT_LOCK (rmdemux);
893 rmdemux->running = TRUE;
894 GST_OBJECT_UNLOCK (rmdemux);
896 /* Get the next index */
897 rmdemux->offset = rmdemux->index_offset;
901 case RMDEMUX_LOOP_STATE_DATA:
910 const gchar *reason = gst_flow_get_name (ret);
912 GST_LOG_OBJECT (rmdemux, "pausing task, reason %s", reason);
913 rmdemux->segment_running = FALSE;
914 gst_pad_pause_task (rmdemux->sinkpad);
916 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
917 if (ret == GST_FLOW_UNEXPECTED) {
918 /* perform EOS logic */
919 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
922 /* for segment playback we need to post when (in stream time)
923 * we stopped, this is either stop (when set) or the duration. */
924 if ((stop = rmdemux->segment.stop) == -1)
925 stop = rmdemux->segment.duration;
927 GST_LOG_OBJECT (rmdemux, "Sending segment done, at end of segment");
928 gst_element_post_message (GST_ELEMENT (rmdemux),
929 gst_message_new_segment_done (GST_OBJECT (rmdemux),
930 GST_FORMAT_TIME, stop));
932 /* normal playback, send EOS to all linked pads */
933 GST_LOG_OBJECT (rmdemux, "Sending EOS, at end of stream");
934 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
937 GST_ELEMENT_ERROR (rmdemux, STREAM, FAILED,
938 (NULL), ("stream stopped, reason %s", reason));
939 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
947 gst_rmdemux_fourcc_isplausible (guint32 fourcc)
951 for (i = 0; i < 4; i++) {
952 if (!isprint ((int) ((unsigned char *) (&fourcc))[i])) {
960 gst_rmdemux_chain (GstPad * pad, GstBuffer * buffer)
962 GstFlowReturn ret = GST_FLOW_OK;
967 GstRMDemux *rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
969 if (rmdemux->base_ts == -1) {
970 rmdemux->base_ts = GST_BUFFER_TIMESTAMP (buffer);
971 GST_LOG_OBJECT (rmdemux, "base_ts %" GST_TIME_FORMAT,
972 GST_TIME_ARGS (rmdemux->base_ts));
975 gst_adapter_push (rmdemux->adapter, buffer);
977 GST_LOG_OBJECT (rmdemux, "Chaining buffer of size %d",
978 GST_BUFFER_SIZE (buffer));
981 avail = gst_adapter_available (rmdemux->adapter);
983 GST_LOG_OBJECT (rmdemux, "looping in chain, avail %u", avail);
984 switch (rmdemux->state) {
985 case RMDEMUX_STATE_HEADER:
987 if (gst_adapter_available (rmdemux->adapter) < HEADER_SIZE)
990 data = gst_adapter_peek (rmdemux->adapter, HEADER_SIZE);
992 rmdemux->object_id = RMDEMUX_FOURCC_GET (data + 0);
993 rmdemux->size = RMDEMUX_GUINT32_GET (data + 4) - HEADER_SIZE;
994 rmdemux->object_version = RMDEMUX_GUINT16_GET (data + 8);
996 /* Sanity-check. We assume that the FOURCC is printable ASCII */
997 if (!gst_rmdemux_fourcc_isplausible (rmdemux->object_id)) {
998 /* Failed. Remain in HEADER state, try again... We flush only
999 * the actual FOURCC, not the entire header, because we could
1000 * need to resync anywhere at all... really, this should never
1002 GST_WARNING_OBJECT (rmdemux, "Bogus looking header, unprintable "
1004 gst_adapter_flush (rmdemux->adapter, 4);
1009 GST_LOG_OBJECT (rmdemux, "header found with object_id=%"
1011 " size=%08x object_version=%d",
1012 GST_FOURCC_ARGS (rmdemux->object_id), rmdemux->size,
1013 rmdemux->object_version);
1015 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1017 switch (rmdemux->object_id) {
1018 case GST_MAKE_FOURCC ('.', 'R', 'M', 'F'):
1019 rmdemux->state = RMDEMUX_STATE_HEADER_RMF;
1021 case GST_MAKE_FOURCC ('P', 'R', 'O', 'P'):
1022 rmdemux->state = RMDEMUX_STATE_HEADER_PROP;
1024 case GST_MAKE_FOURCC ('M', 'D', 'P', 'R'):
1025 rmdemux->state = RMDEMUX_STATE_HEADER_MDPR;
1027 case GST_MAKE_FOURCC ('I', 'N', 'D', 'X'):
1028 rmdemux->state = RMDEMUX_STATE_HEADER_INDX;
1030 case GST_MAKE_FOURCC ('D', 'A', 'T', 'A'):
1031 rmdemux->state = RMDEMUX_STATE_HEADER_DATA;
1033 case GST_MAKE_FOURCC ('C', 'O', 'N', 'T'):
1034 rmdemux->state = RMDEMUX_STATE_HEADER_CONT;
1037 rmdemux->state = RMDEMUX_STATE_HEADER_UNKNOWN;
1042 case RMDEMUX_STATE_HEADER_UNKNOWN:
1044 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1047 GST_WARNING_OBJECT (rmdemux, "Unknown object_id %" GST_FOURCC_FORMAT,
1048 GST_FOURCC_ARGS (rmdemux->object_id));
1050 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1051 rmdemux->state = RMDEMUX_STATE_HEADER;
1054 case RMDEMUX_STATE_HEADER_RMF:
1056 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1059 if ((rmdemux->object_version == 0) || (rmdemux->object_version == 1)) {
1060 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1062 gst_rmdemux_parse__rmf (rmdemux, data, rmdemux->size);
1065 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1066 rmdemux->state = RMDEMUX_STATE_HEADER;
1069 case RMDEMUX_STATE_HEADER_PROP:
1071 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1073 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1075 gst_rmdemux_parse_prop (rmdemux, data, rmdemux->size);
1077 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1078 rmdemux->state = RMDEMUX_STATE_HEADER;
1081 case RMDEMUX_STATE_HEADER_MDPR:
1083 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1085 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1087 gst_rmdemux_parse_mdpr (rmdemux, data, rmdemux->size);
1089 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1090 rmdemux->state = RMDEMUX_STATE_HEADER;
1093 case RMDEMUX_STATE_HEADER_CONT:
1095 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1097 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1099 gst_rmdemux_parse_cont (rmdemux, data, rmdemux->size);
1101 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1102 rmdemux->state = RMDEMUX_STATE_HEADER;
1105 case RMDEMUX_STATE_HEADER_DATA:
1107 /* If we haven't already done so then signal there are no more pads */
1108 if (!rmdemux->have_pads) {
1109 GST_LOG_OBJECT (rmdemux, "no more pads");
1110 gst_element_no_more_pads (GST_ELEMENT (rmdemux));
1111 rmdemux->have_pads = TRUE;
1114 /* The actual header is only 8 bytes */
1115 rmdemux->size = DATA_SIZE;
1116 GST_LOG_OBJECT (rmdemux, "data available %d",
1117 gst_adapter_available (rmdemux->adapter));
1118 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1121 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1123 gst_rmdemux_parse_data (rmdemux, data, rmdemux->size);
1125 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1127 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
1130 case RMDEMUX_STATE_HEADER_INDX:
1132 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1134 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1136 rmdemux->size = gst_rmdemux_parse_indx (rmdemux, data, rmdemux->size);
1138 /* Only flush the header */
1139 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1141 rmdemux->state = RMDEMUX_STATE_INDX_DATA;
1144 case RMDEMUX_STATE_INDX_DATA:
1146 /* There's not always an data to get... */
1147 if (rmdemux->size > 0) {
1148 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1151 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1153 gst_rmdemux_parse_indx_data (rmdemux, data, rmdemux->size);
1155 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1158 rmdemux->state = RMDEMUX_STATE_HEADER;
1161 case RMDEMUX_STATE_DATA_PACKET:
1163 if (gst_adapter_available (rmdemux->adapter) < 2)
1166 data = gst_adapter_peek (rmdemux->adapter, 2);
1167 version = RMDEMUX_GUINT16_GET (data);
1168 GST_LOG_OBJECT (rmdemux, "Data packet with version=%d", version);
1170 if (version == 0 || version == 1) {
1173 if (gst_adapter_available (rmdemux->adapter) < 4)
1175 data = gst_adapter_peek (rmdemux->adapter, 4);
1177 length = RMDEMUX_GUINT16_GET (data + 2);
1178 GST_LOG_OBJECT (rmdemux, "Got length %d", length);
1181 GST_LOG_OBJECT (rmdemux, "length too small, dropping");
1182 /* Invalid, just drop it */
1183 gst_adapter_flush (rmdemux->adapter, 4);
1187 avail = gst_adapter_available (rmdemux->adapter);
1191 GST_LOG_OBJECT (rmdemux, "we have %u available and we needed %d",
1194 /* flush version and length */
1195 gst_adapter_flush (rmdemux->adapter, 4);
1198 buffer = gst_adapter_take_buffer (rmdemux->adapter, length);
1200 ret = gst_rmdemux_parse_packet (rmdemux, buffer, version);
1201 rmdemux->chunk_index++;
1204 if (rmdemux->chunk_index == rmdemux->n_chunks || length == 0)
1205 rmdemux->state = RMDEMUX_STATE_HEADER;
1208 gst_adapter_flush (rmdemux->adapter, 2);
1210 if (rmdemux->data_offset == 0) {
1211 GST_LOG_OBJECT (rmdemux,
1212 "No further data, internal demux state EOS");
1213 rmdemux->state = RMDEMUX_STATE_EOS;
1215 rmdemux->state = RMDEMUX_STATE_HEADER;
1219 case RMDEMUX_STATE_EOS:
1220 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1223 GST_WARNING_OBJECT (rmdemux, "Unhandled state %d", rmdemux->state);
1232 static GstRMDemuxStream *
1233 gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux, int id)
1237 for (cur = rmdemux->streams; cur; cur = cur->next) {
1238 GstRMDemuxStream *stream = cur->data;
1240 if (stream->id == id) {
1249 gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event)
1253 for (cur = rmdemux->streams; cur; cur = cur->next) {
1254 GstRMDemuxStream *stream = cur->data;
1256 GST_DEBUG_OBJECT (rmdemux, "Pushing %s event on pad %s",
1257 GST_EVENT_TYPE_NAME (event), GST_PAD_NAME (stream->pad));
1259 switch (GST_EVENT_TYPE (event)) {
1260 case GST_EVENT_FLUSH_STOP:
1261 stream->last_ts = -1;
1262 stream->next_ts = -1;
1263 stream->last_seq = -1;
1264 stream->next_seq = -1;
1265 stream->last_flow = GST_FLOW_OK;
1270 gst_event_ref (event);
1271 gst_pad_push_event (stream->pad, event);
1273 gst_event_unref (event);
1277 gst_rmdemux_add_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
1279 GstCaps *stream_caps = NULL;
1280 const gchar *codec_tag = NULL;
1281 const gchar *codec_name = NULL;
1284 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
1285 char *name = g_strdup_printf ("video_%02d", rmdemux->n_video_streams);
1288 gst_pad_new_from_static_template (&gst_rmdemux_videosrc_template, name);
1291 codec_tag = GST_TAG_VIDEO_CODEC;
1293 switch (stream->fourcc) {
1294 case GST_RM_VDO_RV10:
1295 codec_name = "Real Video 1.0";
1298 case GST_RM_VDO_RV20:
1299 codec_name = "Real Video 2.0";
1302 case GST_RM_VDO_RV30:
1303 codec_name = "Real Video 3.0";
1306 case GST_RM_VDO_RV40:
1307 codec_name = "Real Video 4.0";
1311 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1312 "fourcc", GST_TYPE_FOURCC, stream->fourcc, NULL);
1313 GST_WARNING_OBJECT (rmdemux,
1314 "Unknown video FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1315 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1320 gst_caps_new_simple ("video/x-pn-realvideo", "rmversion", G_TYPE_INT,
1322 "format", G_TYPE_INT,
1323 (int) stream->format,
1324 "subformat", G_TYPE_INT, (int) stream->subformat, NULL);
1328 gst_caps_set_simple (stream_caps,
1329 "width", G_TYPE_INT, stream->width,
1330 "height", G_TYPE_INT, stream->height,
1331 "framerate", GST_TYPE_FRACTION, stream->framerate_numerator,
1332 stream->framerate_denominator, NULL);
1334 rmdemux->n_video_streams++;
1336 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
1337 char *name = g_strdup_printf ("audio_%02d", rmdemux->n_audio_streams);
1340 gst_pad_new_from_static_template (&gst_rmdemux_audiosrc_template, name);
1341 GST_LOG_OBJECT (rmdemux, "Created audio pad \"%s\"", name);
1344 codec_tag = GST_TAG_AUDIO_CODEC;
1346 switch (stream->fourcc) {
1347 /* Older RealAudio Codecs */
1348 case GST_RM_AUD_14_4:
1349 codec_name = "Real Audio 14.4kbps";
1353 case GST_RM_AUD_28_8:
1354 codec_name = "Real Audio 28.8kbps";
1358 /* DolbyNet (Dolby AC3, low bitrate) */
1359 case GST_RM_AUD_DNET:
1360 codec_name = "AC-3 audio";
1362 gst_caps_new_simple ("audio/x-ac3", "rate", G_TYPE_INT,
1363 (int) stream->rate, NULL);
1364 stream->needs_descrambling = TRUE;
1365 stream->subpackets_needed = 1;
1366 stream->subpackets = NULL;
1370 case GST_RM_AUD_RAAC:
1371 case GST_RM_AUD_RACP:
1372 codec_name = "MPEG4 audio";
1374 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
1375 (int) 4, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
1376 if (stream->extra_data_size > 0) {
1377 /* strip off an unknown byte in the extra data */
1378 stream->extra_data_size--;
1379 stream->extra_data++;
1381 stream->needs_descrambling = TRUE;
1382 stream->subpackets_needed = 1;
1383 stream->subpackets = NULL;
1387 case GST_RM_AUD_ATRC:
1388 codec_name = "Sony ATRAC3";
1389 stream_caps = gst_caps_new_simple ("audio/x-vnd.sony.atrac3", NULL);
1392 /* RealAudio G2 audio */
1393 case GST_RM_AUD_COOK:
1394 codec_name = "Real Audio G2 (Cook)";
1396 stream->needs_descrambling = TRUE;
1397 stream->subpackets_needed = stream->height;
1398 stream->subpackets = NULL;
1401 /* RALF is lossless */
1402 case GST_RM_AUD_RALF:
1403 /* FIXME: codec_name = */
1404 GST_DEBUG_OBJECT (rmdemux, "RALF");
1405 stream_caps = gst_caps_new_simple ("audio/x-ralf-mpeg4-generic", NULL);
1408 /* Sipro/ACELP.NET Voice Codec (MIME unknown) */
1409 case GST_RM_AUD_SIPR:
1410 /* FIXME: codec_name = */
1411 stream_caps = gst_caps_new_simple ("audio/x-sipro", NULL);
1415 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1416 "fourcc", GST_TYPE_FOURCC, stream->fourcc, NULL);
1417 GST_WARNING_OBJECT (rmdemux,
1418 "Unknown audio FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1419 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1425 gst_caps_new_simple ("audio/x-pn-realaudio", "raversion", G_TYPE_INT,
1426 (int) version, NULL);
1430 gst_caps_set_simple (stream_caps,
1431 "flavor", G_TYPE_INT, (int) stream->flavor,
1432 "rate", G_TYPE_INT, (int) stream->rate,
1433 "channels", G_TYPE_INT, (int) stream->n_channels,
1434 "width", G_TYPE_INT, (int) stream->sample_width,
1435 "leaf_size", G_TYPE_INT, (int) stream->leaf_size,
1436 "packet_size", G_TYPE_INT, (int) stream->packet_size,
1437 "height", G_TYPE_INT, (int) stream->height, NULL);
1439 rmdemux->n_audio_streams++;
1441 GST_WARNING_OBJECT (rmdemux, "not adding stream of type %d, freeing it",
1447 GST_PAD_ELEMENT_PRIVATE (stream->pad) = stream;
1448 rmdemux->streams = g_slist_append (rmdemux->streams, stream);
1449 GST_LOG_OBJECT (rmdemux, "n_streams is now %d",
1450 g_slist_length (rmdemux->streams));
1452 GST_LOG ("stream->pad = %p, stream_caps = %" GST_PTR_FORMAT, stream->pad,
1455 if (stream->pad && stream_caps) {
1457 GST_LOG_OBJECT (rmdemux, "%d bytes of extra data for stream %s",
1458 stream->extra_data_size, GST_PAD_NAME (stream->pad));
1460 /* add codec_data if there is any */
1461 if (stream->extra_data_size > 0) {
1464 buffer = gst_buffer_new_and_alloc (stream->extra_data_size);
1465 memcpy (GST_BUFFER_DATA (buffer), stream->extra_data,
1466 stream->extra_data_size);
1468 gst_caps_set_simple (stream_caps, "codec_data", GST_TYPE_BUFFER,
1471 gst_buffer_unref (buffer);
1474 gst_pad_use_fixed_caps (stream->pad);
1476 gst_pad_set_caps (stream->pad, stream_caps);
1477 gst_pad_set_event_function (stream->pad,
1478 GST_DEBUG_FUNCPTR (gst_rmdemux_src_event));
1479 gst_pad_set_query_type_function (stream->pad,
1480 GST_DEBUG_FUNCPTR (gst_rmdemux_src_query_types));
1481 gst_pad_set_query_function (stream->pad,
1482 GST_DEBUG_FUNCPTR (gst_rmdemux_src_query));
1484 GST_DEBUG_OBJECT (rmdemux, "adding pad %s with caps %" GST_PTR_FORMAT
1485 ", stream_id=%d", GST_PAD_NAME (stream->pad), stream_caps, stream->id);
1486 gst_pad_set_active (stream->pad, TRUE);
1487 gst_element_add_pad (GST_ELEMENT_CAST (rmdemux), stream->pad);
1489 /* save for later, we must send the tags after the newsegment event */
1490 if (codec_name != NULL && codec_tag != NULL) {
1491 if (stream->pending_tags == NULL)
1492 stream->pending_tags = gst_tag_list_new ();
1493 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_KEEP,
1494 codec_tag, codec_name, NULL);
1501 gst_caps_unref (stream_caps);
1505 re_skip_pascal_string (const guint8 * ptr)
1515 gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data, int length)
1517 GST_LOG_OBJECT (rmdemux, "file_version: %d", RMDEMUX_GUINT32_GET (data));
1518 GST_LOG_OBJECT (rmdemux, "num_headers: %d", RMDEMUX_GUINT32_GET (data + 4));
1522 gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data, int length)
1524 GST_LOG_OBJECT (rmdemux, "max bitrate: %d", RMDEMUX_GUINT32_GET (data));
1525 GST_LOG_OBJECT (rmdemux, "avg bitrate: %d", RMDEMUX_GUINT32_GET (data + 4));
1526 GST_LOG_OBJECT (rmdemux, "max packet size: %d",
1527 RMDEMUX_GUINT32_GET (data + 8));
1528 rmdemux->avg_packet_size = RMDEMUX_GUINT32_GET (data + 12);
1529 GST_LOG_OBJECT (rmdemux, "avg packet size: %d", rmdemux->avg_packet_size);
1530 rmdemux->num_packets = RMDEMUX_GUINT32_GET (data + 16);
1531 GST_LOG_OBJECT (rmdemux, "number of packets: %d", rmdemux->num_packets);
1533 GST_LOG_OBJECT (rmdemux, "duration: %d", RMDEMUX_GUINT32_GET (data + 20));
1534 rmdemux->duration = RMDEMUX_GUINT32_GET (data + 20) * GST_MSECOND;
1536 GST_LOG_OBJECT (rmdemux, "preroll: %d", RMDEMUX_GUINT32_GET (data + 24));
1537 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 28);
1538 GST_LOG_OBJECT (rmdemux, "offset of INDX section: 0x%08x",
1539 rmdemux->index_offset);
1540 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 32);
1541 GST_LOG_OBJECT (rmdemux, "offset of DATA section: 0x%08x",
1542 rmdemux->data_offset);
1543 GST_LOG_OBJECT (rmdemux, "n streams: %d", RMDEMUX_GUINT16_GET (data + 36));
1544 GST_LOG_OBJECT (rmdemux, "flags: 0x%04x", RMDEMUX_GUINT16_GET (data + 38));
1548 gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux, const guint8 * data, int length)
1550 GstRMDemuxStream *stream;
1551 char *stream1_type_string;
1552 char *stream2_type_string;
1556 guint32 max_bitrate;
1557 guint32 avg_bitrate;
1559 stream = g_new0 (GstRMDemuxStream, 1);
1561 stream->id = RMDEMUX_GUINT16_GET (data);
1562 stream->index = NULL;
1563 stream->seek_offset = 0;
1564 stream->last_ts = -1;
1565 stream->next_ts = -1;
1566 stream->last_flow = GST_FLOW_OK;
1567 stream->discont = TRUE;
1568 stream->adapter = gst_adapter_new ();
1569 GST_LOG_OBJECT (rmdemux, "stream_number=%d", stream->id);
1571 /* parse the bitrates */
1572 max_bitrate = RMDEMUX_GUINT32_GET (data + 2);
1573 avg_bitrate = RMDEMUX_GUINT32_GET (data + 6);
1574 GST_LOG_OBJECT (rmdemux, "Stream max bitrate=%u", max_bitrate);
1575 GST_LOG_OBJECT (rmdemux, "Stream avg bitrate=%u", avg_bitrate);
1576 if (max_bitrate != 0) {
1577 if (stream->pending_tags == NULL)
1578 stream->pending_tags = gst_tag_list_new ();
1579 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1580 GST_TAG_MAXIMUM_BITRATE, max_bitrate, NULL);
1582 if (avg_bitrate != 0) {
1583 if (stream->pending_tags == NULL)
1584 stream->pending_tags = gst_tag_list_new ();
1585 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1586 GST_TAG_BITRATE, avg_bitrate, NULL);
1590 stream1_type_string = gst_rm_utils_read_string8 (data + offset,
1591 length - offset, &str_len);
1593 stream2_type_string = gst_rm_utils_read_string8 (data + offset,
1594 length - offset, &str_len);
1597 /* stream1_type_string for audio and video stream is a "put_whatever_you_want" field :
1599 * - "[The ]Video/Audio Stream" (File produced by an official Real encoder)
1600 * - "RealVideoPremierePlugIn-VIDEO/AUDIO" (File produced by Abobe Premiere)
1602 * so, we should not rely on it to know which stream type it is
1605 GST_LOG_OBJECT (rmdemux, "stream type: %s", stream1_type_string);
1606 GST_LOG_OBJECT (rmdemux, "MIME type=%s", stream2_type_string);
1608 if (strcmp (stream2_type_string, "video/x-pn-realvideo") == 0) {
1609 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1610 } else if (strcmp (stream2_type_string,
1611 "video/x-pn-multirate-realvideo") == 0) {
1612 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1613 } else if (strcmp (stream2_type_string, "audio/x-pn-realaudio") == 0) {
1614 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1615 } else if (strcmp (stream2_type_string,
1616 "audio/x-pn-multirate-realaudio") == 0) {
1617 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1618 } else if (strcmp (stream2_type_string,
1619 "audio/x-pn-multirate-realaudio-live") == 0) {
1620 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1621 } else if (strcmp (stream2_type_string, "audio/x-ralf-mpeg4-generic") == 0) {
1622 /* Another audio type found in the real testsuite */
1623 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1624 } else if (strcmp (stream1_type_string, "") == 0 &&
1625 strcmp (stream2_type_string, "logical-fileinfo") == 0) {
1626 stream_type = GST_RMDEMUX_STREAM_FILEINFO;
1628 stream_type = GST_RMDEMUX_STREAM_UNKNOWN;
1629 GST_WARNING_OBJECT (rmdemux, "unknown stream type \"%s\",\"%s\"",
1630 stream1_type_string, stream2_type_string);
1632 g_free (stream1_type_string);
1633 g_free (stream2_type_string);
1637 stream->subtype = stream_type;
1638 switch (stream_type) {
1640 case GST_RMDEMUX_STREAM_VIDEO:
1641 /* RV10/RV20/RV30/RV40 => video/x-pn-realvideo, version=1,2,3,4 */
1642 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 8);
1643 stream->width = RMDEMUX_GUINT16_GET (data + offset + 12);
1644 stream->height = RMDEMUX_GUINT16_GET (data + offset + 14);
1645 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 16);
1646 stream->subformat = RMDEMUX_GUINT32_GET (data + offset + 26);
1647 stream->format = RMDEMUX_GUINT32_GET (data + offset + 30);
1648 stream->extra_data_size = length - (offset + 26);
1649 stream->extra_data = (guint8 *) data + offset + 26;
1650 /* Natural way to represent framerates here requires unsigned 32 bit
1651 * numerator, which we don't have. For the nasty case, approximate...
1654 guint32 numerator = RMDEMUX_GUINT16_GET (data + offset + 22) * 65536 +
1655 RMDEMUX_GUINT16_GET (data + offset + 24);
1656 if (numerator > G_MAXINT) {
1657 stream->framerate_numerator = (gint) (numerator >> 1);
1658 stream->framerate_denominator = 32768;
1660 stream->framerate_numerator = (gint) numerator;
1661 stream->framerate_denominator = 65536;
1665 GST_DEBUG_OBJECT (rmdemux,
1666 "Video stream with fourcc=%" GST_FOURCC_FORMAT
1667 " width=%d height=%d rate=%d framerate=%d/%d subformat=%x format=%x extra_data_size=%d",
1668 GST_FOURCC_ARGS (stream->fourcc), stream->width, stream->height,
1669 stream->rate, stream->framerate_numerator,
1670 stream->framerate_denominator, stream->subformat, stream->format,
1671 stream->extra_data_size);
1673 case GST_RMDEMUX_STREAM_AUDIO:{
1674 stream->version = RMDEMUX_GUINT16_GET (data + offset + 4);
1675 GST_INFO ("stream version = %u", stream->version);
1676 switch (stream->version) {
1678 stream->fourcc = GST_RM_AUD_14_4;
1679 stream->packet_size = 20;
1680 stream->rate = 8000;
1681 stream->n_channels = 1;
1682 stream->sample_width = 16;
1684 stream->leaf_size = 0;
1688 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1689 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1690 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1691 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1692 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1693 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 48);
1694 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 52);
1695 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 54);
1696 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 62);
1697 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 69);
1698 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1699 stream->extra_data_size);
1700 if (length - (offset + 73) >= stream->extra_data_size) {
1701 stream->extra_data = (guint8 *) data + offset + 73;
1703 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1704 stream->extra_data_size = 0;
1708 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1709 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1710 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1711 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1712 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1713 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 54);
1714 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 58);
1715 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 60);
1716 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 66);
1717 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 74);
1718 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1719 stream->extra_data_size);
1720 if (length - (offset + 78) >= stream->extra_data_size) {
1721 stream->extra_data = (guint8 *) data + offset + 78;
1723 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1724 stream->extra_data_size = 0;
1728 GST_WARNING_OBJECT (rmdemux, "Unhandled audio stream version %d",
1734 /* 14_4, 28_8, cook, dnet, sipr, raac, racp, ralf, atrc */
1735 GST_DEBUG_OBJECT (rmdemux,
1736 "Audio stream with rate=%d sample_width=%d n_channels=%d",
1737 stream->rate, stream->sample_width, stream->n_channels);
1741 case GST_RMDEMUX_STREAM_FILEINFO:
1745 /* Length of this section */
1746 GST_DEBUG_OBJECT (rmdemux, "length2: 0x%08x",
1747 RMDEMUX_GUINT32_GET (data + offset));
1750 /* Unknown : 00 00 00 00 */
1753 /* Number of variables that would follow (loop iterations) */
1754 element_nb = RMDEMUX_GUINT32_GET (data + offset);
1757 while (element_nb) {
1758 /* Category Id : 00 00 00 XX 00 00 */
1762 offset += re_skip_pascal_string (data + offset);
1764 /* Variable Value Type */
1765 /* 00 00 00 00 00 => integer/boolean, preceded by length */
1766 /* 00 00 00 02 00 => pascal string, preceded by length, no trailing \0 */
1769 /* Variable Value */
1770 offset += re_skip_pascal_string (data + offset);
1776 case GST_RMDEMUX_STREAM_UNKNOWN:
1781 gst_rmdemux_add_stream (rmdemux, stream);
1785 gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data, int length)
1790 n = RMDEMUX_GUINT32_GET (data);
1791 id = RMDEMUX_GUINT16_GET (data + 4);
1792 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 6);
1794 GST_DEBUG_OBJECT (rmdemux, "Number of indices=%d Stream ID=%d length=%d", n,
1797 /* Point to the next index_stream */
1798 rmdemux->index_stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
1800 /* Return the length of the index */
1805 gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux, const guint8 * data,
1810 GstRMDemuxIndex *index;
1812 /* The number of index records */
1815 if (rmdemux->index_stream == NULL)
1818 /* don't parse the index a second time when operating pull-based and
1819 * reaching the end of the file */
1820 if (rmdemux->index_stream->index_length > 0) {
1821 GST_DEBUG_OBJECT (rmdemux, "Already have an index for this stream");
1825 index = g_malloc (sizeof (GstRMDemuxIndex) * n);
1826 rmdemux->index_stream->index = index;
1827 rmdemux->index_stream->index_length = n;
1829 for (i = 0; i < n; i++) {
1830 index[i].timestamp = RMDEMUX_GUINT32_GET (data + 2) * GST_MSECOND;
1831 index[i].offset = RMDEMUX_GUINT32_GET (data + 6);
1833 GST_DEBUG_OBJECT (rmdemux, "Index found for timestamp=%f (at offset=%x)",
1834 gst_guint64_to_gdouble (index[i].timestamp) / GST_SECOND,
1841 gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data, int length)
1843 rmdemux->n_chunks = RMDEMUX_GUINT32_GET (data);
1844 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 4);
1845 rmdemux->chunk_index = 0;
1846 GST_DEBUG_OBJECT (rmdemux, "Data chunk found with %d packets "
1847 "(next data at 0x%08x)", rmdemux->n_chunks, rmdemux->data_offset);
1851 gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data, int length)
1855 tags = gst_rm_utils_read_tags (data, length, gst_rm_utils_read_string16);
1857 gst_element_found_tags (GST_ELEMENT (rmdemux), tags);
1861 static GstFlowReturn
1862 gst_rmdemux_combine_flows (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
1867 /* store the value */
1868 stream->last_flow = ret;
1870 /* if it's success we can return the value right away */
1871 if (GST_FLOW_IS_SUCCESS (ret))
1874 /* any other error that is not-linked can be returned right
1876 if (ret != GST_FLOW_NOT_LINKED)
1879 for (cur = rmdemux->streams; cur; cur = cur->next) {
1880 GstRMDemuxStream *ostream = cur->data;
1882 ret = ostream->last_flow;
1883 /* some other return value (must be SUCCESS but we can return
1884 * other values as well) */
1885 if (ret != GST_FLOW_NOT_LINKED)
1888 /* if we get here, all other pads were unlinked and we return
1889 * NOT_LINKED then */
1895 gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
1896 GstRMDemuxStream * stream)
1898 if (stream->subpackets == NULL || stream->subpackets->len == 0)
1901 GST_DEBUG_OBJECT (rmdemux, "discarding %u previously collected subpackets",
1902 stream->subpackets->len);
1903 g_ptr_array_foreach (stream->subpackets, (GFunc) gst_mini_object_unref, NULL);
1904 g_ptr_array_set_size (stream->subpackets, 0);
1907 static GstFlowReturn
1908 gst_rmdemux_descramble_cook_audio (GstRMDemux * rmdemux,
1909 GstRMDemuxStream * stream)
1913 guint packet_size = stream->packet_size;
1914 guint height = stream->subpackets->len;
1915 guint leaf_size = stream->leaf_size;
1918 g_assert (stream->height == height);
1920 GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size,
1923 ret = gst_pad_alloc_buffer_and_set_caps (stream->pad,
1924 GST_BUFFER_OFFSET_NONE, height * packet_size,
1925 GST_PAD_CAPS (stream->pad), &outbuf);
1927 if (ret != GST_FLOW_OK)
1930 for (p = 0; p < height; ++p) {
1931 GstBuffer *b = g_ptr_array_index (stream->subpackets, p);
1932 guint8 *b_data = GST_BUFFER_DATA (b);
1935 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (b);
1937 for (x = 0; x < packet_size / leaf_size; ++x) {
1940 idx = height * x + ((height + 1) / 2) * (p % 2) + (p / 2);
1941 /* GST_LOG ("%3u => %3u", (height * p) + x, idx); */
1942 memcpy (GST_BUFFER_DATA (outbuf) + leaf_size * idx, b_data, leaf_size);
1943 b_data += leaf_size;
1947 /* some decoders, such as realaudiodec, need to be fed in packet units */
1948 for (p = 0; p < height; ++p) {
1951 subbuf = gst_buffer_create_sub (outbuf, p * packet_size, packet_size);
1953 GST_LOG_OBJECT (rmdemux, "pushing buffer timestamp %" GST_TIME_FORMAT,
1954 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (subbuf)));
1956 if (stream->discont) {
1957 GST_BUFFER_FLAG_SET (subbuf, GST_BUFFER_FLAG_DISCONT);
1958 stream->discont = FALSE;
1961 gst_buffer_set_caps (subbuf, GST_PAD_CAPS (stream->pad));
1962 ret = gst_pad_push (stream->pad, subbuf);
1963 if (ret != GST_FLOW_OK)
1967 gst_buffer_unref (outbuf);
1971 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
1976 static GstFlowReturn
1977 gst_rmdemux_descramble_dnet_audio (GstRMDemux * rmdemux,
1978 GstRMDemuxStream * stream)
1982 buf = g_ptr_array_index (stream->subpackets, 0);
1983 g_ptr_array_index (stream->subpackets, 0) = NULL;
1984 g_ptr_array_set_size (stream->subpackets, 0);
1986 buf = gst_rm_utils_descramble_dnet_buffer (buf);
1988 if (stream->discont) {
1989 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1990 stream->discont = FALSE;
1992 return gst_pad_push (stream->pad, buf);
1995 static GstFlowReturn
1996 gst_rmdemux_descramble_mp4a_audio (GstRMDemux * rmdemux,
1997 GstRMDemuxStream * stream)
2000 GstBuffer *buf, *outbuf;
2001 guint frames, index, i;
2003 GstClockTime timestamp;
2007 buf = g_ptr_array_index (stream->subpackets, 0);
2008 g_ptr_array_index (stream->subpackets, 0) = NULL;
2009 g_ptr_array_set_size (stream->subpackets, 0);
2011 data = GST_BUFFER_DATA (buf);
2012 timestamp = GST_BUFFER_TIMESTAMP (buf);
2014 frames = (data[1] & 0xf0) >> 4;
2015 index = 2 * frames + 2;
2017 for (i = 0; i < frames; i++) {
2018 guint len = (data[i * 2 + 2] << 8) | data[i * 2 + 3];
2020 outbuf = gst_buffer_create_sub (buf, index, len);
2022 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
2023 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (stream->pad));
2027 if (stream->discont) {
2028 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2029 stream->discont = FALSE;
2031 res = gst_pad_push (stream->pad, outbuf);
2032 if (res != GST_FLOW_OK)
2035 gst_buffer_unref (buf);
2039 static GstFlowReturn
2040 gst_rmdemux_handle_scrambled_packet (GstRMDemux * rmdemux,
2041 GstRMDemuxStream * stream, GstBuffer * buf, gboolean keyframe)
2045 if (stream->subpackets == NULL)
2046 stream->subpackets = g_ptr_array_sized_new (stream->subpackets_needed);
2048 GST_LOG ("Got subpacket %u/%u, len=%u, key=%d", stream->subpackets->len + 1,
2049 stream->subpackets_needed, GST_BUFFER_SIZE (buf), keyframe);
2051 if (keyframe && stream->subpackets->len > 0) {
2052 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2055 g_ptr_array_add (stream->subpackets, buf);
2057 if (stream->subpackets->len < stream->subpackets_needed)
2060 g_assert (stream->subpackets->len >= 1);
2062 switch (stream->fourcc) {
2063 case GST_RM_AUD_DNET:
2064 ret = gst_rmdemux_descramble_dnet_audio (rmdemux, stream);
2066 case GST_RM_AUD_COOK:
2067 ret = gst_rmdemux_descramble_cook_audio (rmdemux, stream);
2069 case GST_RM_AUD_RAAC:
2070 case GST_RM_AUD_RACP:
2071 ret = gst_rmdemux_descramble_mp4a_audio (rmdemux, stream);
2074 g_assert_not_reached ();
2081 gst_rmdemux_fix_timestamp (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2082 guint8 * data, GstClockTime timestamp)
2086 GstClockTime ts = timestamp;
2088 if (timestamp == GST_CLOCK_TIME_NONE)
2091 /* only adjust when we have a stream with B frames */
2092 if (stream->format < 0x20200002)
2095 /* Fix timestamp. */
2096 switch (stream->fourcc) {
2097 case GST_RM_VDO_RV10:
2099 case GST_RM_VDO_RV20:
2102 * Bit 1- 2: frame type
2104 * Bit 10-22: sequence number
2107 frame_type = (data[0] >> 6) & 0x03;
2108 seq = ((data[1] & 0x7f) << 6) + ((data[2] & 0xfc) >> 2);
2111 case GST_RM_VDO_RV30:
2115 * Bit 3: skip packet if 1
2116 * Bit 4- 5: frame type
2118 * Bit 13-25: sequence number
2121 frame_type = (data[0] >> 3) & 0x03;
2122 seq = ((data[1] & 0x0f) << 9) + (data[2] << 1) + ((data[3] & 0x80) >> 7);
2125 case GST_RM_VDO_RV40:
2128 * Bit 1: skip packet if 1
2129 * Bit 2- 3: frame type
2131 * Bit 14-26: sequence number
2134 frame_type = (data[0] >> 5) & 0x03;
2135 seq = ((data[1] & 0x07) << 10) + (data[2] << 2) + ((data[3] & 0xc0) >> 6);
2139 goto unknown_version;
2142 switch (frame_type) {
2146 GST_LOG_OBJECT (rmdemux, "I frame %d", frame_type);
2148 if (stream->next_ts == -1)
2149 stream->next_ts = timestamp;
2151 timestamp = stream->next_ts;
2152 stream->last_ts = stream->next_ts;
2153 stream->next_ts = ts;
2154 stream->last_seq = stream->next_seq;
2155 stream->next_seq = seq;
2160 GST_LOG_OBJECT (rmdemux, "P frame");
2162 timestamp = stream->last_ts = stream->next_ts;
2163 if (seq < stream->next_seq)
2164 stream->next_ts += (seq + 0x2000 - stream->next_seq) * GST_MSECOND;
2166 stream->next_ts += (seq - stream->next_seq) * GST_MSECOND;
2167 stream->last_seq = stream->next_seq;
2168 stream->next_seq = seq;
2173 GST_LOG_OBJECT (rmdemux, "B frame");
2175 if (seq < stream->last_seq) {
2177 (seq + 0x2000 - stream->last_seq) * GST_MSECOND + stream->last_ts;
2179 timestamp = (seq - stream->last_seq) * GST_MSECOND + stream->last_ts;
2184 goto unknown_frame_type;
2188 GST_LOG_OBJECT (rmdemux,
2189 "timestamp %" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT, GST_TIME_ARGS (ts),
2190 GST_TIME_ARGS (timestamp));
2197 GST_ELEMENT_ERROR (rmdemux, STREAM, DECODE,
2198 ("Unknown version: %i.", stream->version), (NULL));
2199 return GST_FLOW_ERROR;
2204 GST_ELEMENT_ERROR (rmdemux, STREAM, DECODE, ("Unknown frame type %d.",
2205 frame_type), (NULL));
2206 return GST_FLOW_ERROR;
2210 #define PARSE_NUMBER(data, size, number, label) \
2214 number = GST_READ_UINT16_BE (data); \
2215 if (!(number & 0xc000)) { \
2218 number = GST_READ_UINT32_BE (data); \
2228 static GstFlowReturn
2229 gst_rmdemux_parse_video_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2230 GstBuffer * in, guint offset, guint16 version,
2231 GstClockTime timestamp, gboolean key)
2234 const guint8 *data, *base;
2237 base = GST_BUFFER_DATA (in);
2238 data = base + offset;
2239 size = GST_BUFFER_SIZE (in) - offset;
2240 /* if size <= 2, we want this method to return the same GstFlowReturn as it
2241 * was previously for that given stream. */
2242 ret = stream->last_flow;
2248 guint pkg_subseq = 0, pkg_seqnum = G_MAXUINT;
2249 guint fragment_size;
2250 GstBuffer *fragment;
2252 pkg_header = *data++;
2256 * bit 7: 1=last block in block chain
2257 * bit 6: 1=short header (only one block?)
2259 if ((pkg_header & 0xc0) == 0x40) {
2260 /* skip unknown byte */
2266 if ((pkg_header & 0x40) == 0) {
2267 pkg_subseq = (*data++) & 0x7f;
2274 PARSE_NUMBER (data, size, pkg_length, not_enough_data);
2277 PARSE_NUMBER (data, size, pkg_offset, not_enough_data);
2281 goto not_enough_data;
2283 pkg_seqnum = *data++;
2287 GST_DEBUG_OBJECT (rmdemux,
2288 "seq %d, subseq %d, offset %d, length %d, size %d, header %02x",
2289 pkg_seqnum, pkg_subseq, pkg_offset, pkg_length, size, pkg_header);
2291 /* calc size of fragment */
2292 if ((pkg_header & 0xc0) == 0x80) {
2293 fragment_size = pkg_offset;
2295 if ((pkg_header & 0xc0) == 0)
2296 fragment_size = size;
2298 fragment_size = pkg_length;
2300 GST_DEBUG_OBJECT (rmdemux, "fragment size %d", fragment_size);
2302 /* get the fragment */
2303 fragment = gst_buffer_create_sub (in, data - base, fragment_size);
2305 if (pkg_subseq == 1) {
2306 GST_DEBUG_OBJECT (rmdemux, "start new fragment");
2307 gst_adapter_clear (stream->adapter);
2308 stream->frag_current = 0;
2309 stream->frag_count = 0;
2310 stream->frag_length = pkg_length;
2311 } else if (pkg_subseq == 0) {
2312 GST_DEBUG_OBJECT (rmdemux, "non fragmented packet");
2313 stream->frag_current = 0;
2314 stream->frag_count = 0;
2315 stream->frag_length = fragment_size;
2318 /* put fragment in adapter */
2319 gst_adapter_push (stream->adapter, fragment);
2320 stream->frag_offset[stream->frag_count] = stream->frag_current;
2321 stream->frag_current += fragment_size;
2322 stream->frag_count++;
2324 if (stream->frag_count > MAX_FRAGS)
2325 goto too_many_fragments;
2327 GST_DEBUG_OBJECT (rmdemux, "stored fragment in adapter %d/%d",
2328 stream->frag_current, stream->frag_length);
2330 /* flush fragment when complete */
2331 if (stream->frag_current >= stream->frag_length) {
2337 /* calculate header size, which is:
2338 * 1 byte for the number of fragments - 1
2339 * for each fragment:
2340 * 4 bytes 0x00000001 little endian
2341 * 4 bytes fragment offset
2343 * This is also the matroska header for realvideo, the decoder needs the
2344 * fragment offsets, both in ffmpeg and real .so, so we just give it that
2345 * in front of the data.
2347 header_size = 1 + (8 * (stream->frag_count));
2349 GST_DEBUG_OBJECT (rmdemux,
2350 "fragmented completed. count %d, header_size %u", stream->frag_count,
2353 avail = gst_adapter_available (stream->adapter);
2355 out = gst_buffer_new_and_alloc (header_size + avail);
2356 outdata = GST_BUFFER_DATA (out);
2359 *outdata++ = stream->frag_count - 1;
2360 for (i = 0; i < stream->frag_count; i++) {
2361 GST_WRITE_UINT32_LE (outdata, 0x00000001);
2363 GST_WRITE_UINT32_LE (outdata, stream->frag_offset[i]);
2367 /* copy packet data after the header now */
2368 gst_adapter_copy (stream->adapter, outdata, 0, avail);
2369 gst_adapter_flush (stream->adapter, avail);
2371 stream->frag_current = 0;
2372 stream->frag_count = 0;
2373 stream->frag_length = 0;
2375 gst_buffer_set_caps (out, GST_PAD_CAPS (stream->pad));
2377 if (timestamp != -1) {
2378 if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts)
2379 timestamp -= rmdemux->first_ts;
2383 if (rmdemux->base_ts != -1)
2384 timestamp += rmdemux->base_ts;
2387 gst_rmdemux_fix_timestamp (rmdemux, stream, outdata, timestamp);
2389 GST_BUFFER_TIMESTAMP (out) = timestamp;
2391 GST_LOG_OBJECT (rmdemux, "pushing timestamp %" GST_TIME_FORMAT,
2392 GST_TIME_ARGS (timestamp));
2394 if (stream->discont) {
2395 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT);
2396 stream->discont = FALSE;
2399 ret = gst_pad_push (stream->pad, out);
2400 ret = gst_rmdemux_combine_flows (rmdemux, stream, ret);
2401 if (ret != GST_FLOW_OK)
2404 timestamp = GST_CLOCK_TIME_NONE;
2406 data += fragment_size;
2407 size -= fragment_size;
2409 GST_DEBUG_OBJECT (rmdemux, "%d bytes left", size);
2411 gst_buffer_unref (in);
2418 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2420 gst_buffer_unref (in);
2425 GST_ELEMENT_ERROR (rmdemux, STREAM, DECODE,
2426 ("Got more fragments (%u) than can be handled (%u)",
2427 stream->frag_count, MAX_FRAGS), (NULL));
2428 gst_buffer_unref (in);
2429 return GST_FLOW_ERROR;
2433 static GstFlowReturn
2434 gst_rmdemux_parse_audio_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2435 GstBuffer * in, guint offset, guint16 version,
2436 GstClockTime timestamp, gboolean key)
2438 GstFlowReturn ret, cret;
2443 data = GST_BUFFER_DATA (in) + offset;
2444 size = GST_BUFFER_SIZE (in) - offset;
2446 ret = gst_pad_alloc_buffer_and_set_caps (stream->pad,
2447 GST_BUFFER_OFFSET_NONE, size, GST_PAD_CAPS (stream->pad), &buffer);
2449 cret = gst_rmdemux_combine_flows (rmdemux, stream, ret);
2450 if (ret != GST_FLOW_OK)
2453 memcpy (GST_BUFFER_DATA (buffer), (guint8 *) data, size);
2455 if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts)
2456 timestamp -= rmdemux->first_ts;
2460 if (rmdemux->base_ts != -1)
2461 timestamp += rmdemux->base_ts;
2463 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
2465 if (stream->needs_descrambling) {
2466 GST_LOG_OBJECT (rmdemux, "descramble timestamp %" GST_TIME_FORMAT,
2467 GST_TIME_ARGS (timestamp));
2468 ret = gst_rmdemux_handle_scrambled_packet (rmdemux, stream, buffer, key);
2470 GST_LOG_OBJECT (rmdemux,
2471 "Pushing buffer of size %d, timestamp %" GST_TIME_FORMAT "to pad %s",
2472 GST_BUFFER_SIZE (buffer), GST_TIME_ARGS (timestamp),
2473 GST_PAD_NAME (stream->pad));
2475 if (stream->discont) {
2476 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2477 stream->discont = FALSE;
2479 ret = gst_pad_push (stream->pad, buffer);
2482 gst_buffer_unref (in);
2489 GST_DEBUG_OBJECT (rmdemux, "pad alloc returned %d", ret);
2490 gst_buffer_unref (in);
2495 static GstFlowReturn
2496 gst_rmdemux_parse_packet (GstRMDemux * rmdemux, GstBuffer * in, guint16 version)
2499 GstRMDemuxStream *stream;
2501 GstFlowReturn cret, ret;
2502 GstClockTime timestamp;
2504 guint8 *data, *base;
2508 base = data = GST_BUFFER_DATA (in);
2509 size = GST_BUFFER_SIZE (in);
2512 id = RMDEMUX_GUINT16_GET (data);
2514 stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
2515 if (!stream || !stream->pad)
2516 goto unknown_stream;
2518 /* timestamp in Msec */
2519 ts = RMDEMUX_GUINT32_GET (data + 2);
2520 timestamp = ts * GST_MSECOND;
2522 gst_segment_set_last_stop (&rmdemux->segment, GST_FORMAT_TIME, timestamp);
2524 GST_LOG_OBJECT (rmdemux, "Parsing a packet for stream=%d, timestamp=%"
2525 GST_TIME_FORMAT ", size %u, version=%d, ts=%u", id,
2526 GST_TIME_ARGS (timestamp), size, version, ts);
2528 if (rmdemux->first_ts == GST_CLOCK_TIME_NONE) {
2529 GST_DEBUG_OBJECT (rmdemux, "First timestamp: %" GST_TIME_FORMAT,
2530 GST_TIME_ARGS (timestamp));
2531 rmdemux->first_ts = timestamp;
2534 /* skip stream_id and timestamp */
2539 flags = GST_READ_UINT8 (data + 1);
2544 /* version 1 has an extra byte */
2549 key = (flags & 0x02) != 0;
2550 GST_DEBUG_OBJECT (rmdemux, "flags %d, Keyframe %d", flags, key);
2552 if (rmdemux->need_newsegment) {
2555 event = gst_event_new_new_segment (FALSE, rmdemux->segment.rate,
2556 rmdemux->segment.format, rmdemux->segment.start,
2557 rmdemux->segment.stop, rmdemux->segment.time);
2559 GST_DEBUG_OBJECT (rmdemux, "sending NEWSEGMENT event, segment.start= %"
2560 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start));
2562 gst_rmdemux_send_event (rmdemux, event);
2563 rmdemux->need_newsegment = FALSE;
2566 if (stream->pending_tags != NULL) {
2567 GST_LOG_OBJECT (stream->pad, "tags %" GST_PTR_FORMAT, stream->pending_tags);
2568 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (rmdemux), stream->pad,
2569 stream->pending_tags);
2570 stream->pending_tags = NULL;
2573 if ((rmdemux->offset + size) <= stream->seek_offset) {
2574 GST_DEBUG_OBJECT (rmdemux,
2575 "Stream %d is skipping: seek_offset=%d, offset=%d, size=%u",
2576 stream->id, stream->seek_offset, rmdemux->offset, size);
2581 /* do special headers */
2582 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
2584 gst_rmdemux_parse_video_packet (rmdemux, stream, in, data - base,
2585 version, timestamp, key);
2586 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
2588 gst_rmdemux_parse_audio_packet (rmdemux, stream, in, data - base,
2589 version, timestamp, key);
2593 cret = gst_rmdemux_combine_flows (rmdemux, stream, ret);
2601 GST_WARNING_OBJECT (rmdemux, "No stream for stream id %d in parsing "
2608 gst_rmdemux_plugin_init (GstPlugin * plugin)
2610 return gst_element_register (plugin, "rmdemux",
2611 GST_RANK_PRIMARY, GST_TYPE_RMDEMUX);