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,
195 GST_ELEMENT_REGISTER_DEFINE (rmdemux, "rmdemux",
196 GST_RANK_PRIMARY, GST_TYPE_RMDEMUX);
199 gst_rmdemux_base_init (GstRMDemuxClass * klass)
201 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
203 gst_element_class_add_static_pad_template (element_class,
204 &gst_rmdemux_sink_template);
205 gst_element_class_add_static_pad_template (element_class,
206 &gst_rmdemux_videosrc_template);
207 gst_element_class_add_static_pad_template (element_class,
208 &gst_rmdemux_audiosrc_template);
209 gst_element_class_set_static_metadata (element_class, "RealMedia Demuxer",
211 "Demultiplex a RealMedia file into audio and video streams",
212 "David Schleef <ds@schleef.org>");
216 gst_rmdemux_class_init (GstRMDemuxClass * klass)
218 GObjectClass *gobject_class;
219 GstElementClass *gstelement_class;
221 gobject_class = (GObjectClass *) klass;
222 gstelement_class = (GstElementClass *) klass;
224 parent_class = g_type_class_peek_parent (klass);
226 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rmdemux_change_state);
228 GST_DEBUG_CATEGORY_INIT (rmdemux_debug, "rmdemux",
229 0, "Demuxer for Realmedia streams");
231 gobject_class->finalize = gst_rmdemux_finalize;
235 gst_rmdemux_finalize (GObject * object)
237 GstRMDemux *rmdemux = GST_RMDEMUX (object);
239 if (rmdemux->adapter) {
240 g_object_unref (rmdemux->adapter);
241 rmdemux->adapter = NULL;
243 if (rmdemux->flowcombiner) {
244 gst_flow_combiner_free (rmdemux->flowcombiner);
245 rmdemux->flowcombiner = NULL;
248 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
252 gst_rmdemux_init (GstRMDemux * rmdemux)
255 gst_pad_new_from_static_template (&gst_rmdemux_sink_template, "sink");
256 gst_pad_set_event_function (rmdemux->sinkpad,
257 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_event));
258 gst_pad_set_chain_function (rmdemux->sinkpad,
259 GST_DEBUG_FUNCPTR (gst_rmdemux_chain));
260 gst_pad_set_activate_function (rmdemux->sinkpad,
261 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate));
262 gst_pad_set_activatemode_function (rmdemux->sinkpad,
263 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate_mode));
265 gst_element_add_pad (GST_ELEMENT (rmdemux), rmdemux->sinkpad);
267 rmdemux->adapter = gst_adapter_new ();
268 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
269 rmdemux->base_ts = GST_CLOCK_TIME_NONE;
270 rmdemux->need_newsegment = TRUE;
271 rmdemux->have_group_id = FALSE;
272 rmdemux->group_id = G_MAXUINT;
273 rmdemux->flowcombiner = gst_flow_combiner_new ();
274 rmdemux->seek_seqnum = GST_SEQNUM_INVALID;
276 gst_rm_utils_run_tests ();
280 gst_rmdemux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
284 switch (GST_EVENT_TYPE (event)) {
285 case GST_EVENT_SEGMENT:
286 gst_event_unref (event);
290 ret = gst_pad_event_default (pad, parent, event);
297 gst_rmdemux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
301 GstRMDemux *rmdemux = GST_RMDEMUX (parent);
303 GST_LOG_OBJECT (rmdemux, "handling src event");
305 switch (GST_EVENT_TYPE (event)) {
311 GST_LOG_OBJECT (rmdemux, "Event on src: SEEK");
313 seqnum = gst_event_get_seqnum (event);
314 if (seqnum == rmdemux->seek_seqnum) {
316 "Drop duplicated SEEK event seqnum %" G_GUINT32_FORMAT, seqnum);
317 gst_event_unref (event);
321 /* can't seek if we are not seekable, FIXME could pass the
322 * seek query upstream after converting it to bytes using
323 * the average bitrate of the stream. */
324 if (!rmdemux->seekable) {
326 GST_DEBUG ("seek on non seekable stream");
330 GST_OBJECT_LOCK (rmdemux);
331 /* check if we can do the seek now */
332 running = rmdemux->running;
333 GST_OBJECT_UNLOCK (rmdemux);
335 /* now do the seek */
337 ret = gst_rmdemux_perform_seek (rmdemux, event);
341 gst_event_unref (event);
345 GST_LOG_OBJECT (rmdemux, "Event on src: type=%d", GST_EVENT_TYPE (event));
346 ret = gst_pad_event_default (pad, parent, event);
353 GST_DEBUG ("error handling event");
354 gst_event_unref (event);
358 /* Validate that this looks like a reasonable point to seek to */
360 gst_rmdemux_validate_offset (GstRMDemux * rmdemux)
363 GstFlowReturn flowret;
364 guint16 version, length;
369 flowret = gst_pad_pull_range (rmdemux->sinkpad, rmdemux->offset, 4, &buffer);
371 if (flowret != GST_FLOW_OK) {
372 GST_DEBUG_OBJECT (rmdemux, "Failed to pull data at offset %d",
376 /* TODO: Can we also be seeking to a 'DATA' chunk header? Check this.
377 * Also, for the case we currently handle, can we check any more? It's pretty
378 * sucky to not be validating a little more heavily than this... */
379 /* This should now be the start of a data packet header. That begins with
380 * a 2-byte 'version' field, which has to be 0 or 1, then a length. I'm not
381 * certain what values are valid for length, but it must always be at least
382 * 4 bytes, and we can check that it won't take us past our known total size
385 gst_buffer_map (buffer, &map, GST_MAP_READ);
386 version = RMDEMUX_GUINT16_GET (map.data);
387 if (version != 0 && version != 1) {
388 GST_DEBUG_OBJECT (rmdemux, "Expected version 0 or 1, got %d",
393 length = RMDEMUX_GUINT16_GET (map.data + 2);
394 /* TODO: Also check against total stream length */
396 GST_DEBUG_OBJECT (rmdemux, "Expected length >= 4, got %d", (int) length);
399 gst_buffer_unmap (buffer, &map);
402 rmdemux->offset += 4;
403 gst_adapter_clear (rmdemux->adapter);
404 gst_adapter_push (rmdemux->adapter, buffer);
406 GST_WARNING_OBJECT (rmdemux, "Failed to validate seek offset at %d",
408 gst_buffer_unref (buffer);
415 find_seek_offset_bytes (GstRMDemux * rmdemux, guint target)
419 gboolean ret = FALSE;
421 for (cur = rmdemux->streams; cur; cur = cur->next) {
422 GstRMDemuxStream *stream = cur->data;
424 /* Search backwards through this stream's index until we find the first
425 * timestamp before our target time */
426 for (i = stream->index_length - 1; i >= 0; i--) {
427 if (stream->index[i].offset <= target) {
428 /* Set the seek_offset for the stream so we don't bother parsing it
429 * until we've passed that point */
430 stream->seek_offset = stream->index[i].offset;
431 rmdemux->offset = stream->index[i].offset;
441 find_seek_offset_time (GstRMDemux * rmdemux, GstClockTime time)
444 gboolean ret = FALSE;
446 GstClockTime earliest = GST_CLOCK_TIME_NONE;
449 for (cur = rmdemux->streams; cur; cur = cur->next, n_stream++) {
450 GstRMDemuxStream *stream = cur->data;
452 /* Search backwards through this stream's index until we find the first
453 * timestamp before our target time */
454 for (i = stream->index_length - 1; i >= 0; i--) {
455 if (stream->index[i].timestamp <= time) {
456 /* Set the seek_offset for the stream so we don't bother parsing it
457 * until we've passed that point */
458 stream->seek_offset = stream->index[i].offset;
460 /* If it's also the earliest timestamp we've seen of all streams, then
463 if (earliest == GST_CLOCK_TIME_NONE ||
464 stream->index[i].timestamp < earliest) {
465 earliest = stream->index[i].timestamp;
466 rmdemux->offset = stream->index[i].offset;
467 GST_DEBUG_OBJECT (rmdemux,
468 "We're looking for %" GST_TIME_FORMAT
469 " and we found that stream %d has the latest index at %"
470 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start), n_stream,
471 GST_TIME_ARGS (earliest));
479 stream->discont = TRUE;
485 gst_rmdemux_perform_seek (GstRMDemux * rmdemux, GstEvent * event)
493 GstSeekType cur_type, stop_type;
496 guint32 seqnum = GST_SEQNUM_INVALID;
500 GST_DEBUG_OBJECT (rmdemux, "seek with event");
502 seqnum = gst_event_get_seqnum (event);
503 gst_event_parse_seek (event, &rate, &format, &flags,
504 &cur_type, &cur, &stop_type, &stop);
506 /* we can only seek on time */
507 if (format != GST_FORMAT_TIME) {
508 GST_DEBUG_OBJECT (rmdemux, "can only seek on TIME");
511 /* cannot yet do backwards playback */
513 GST_DEBUG_OBJECT (rmdemux, "can only seek with positive rate, not %lf",
518 GST_DEBUG_OBJECT (rmdemux, "seek without event");
524 GST_DEBUG_OBJECT (rmdemux, "seek, rate %g", rate);
526 flush = flags & GST_SEEK_FLAG_FLUSH;
528 /* first step is to unlock the streaming thread if it is
529 * blocked in a chain call, we do this by starting the flush. */
531 fl_event = gst_event_new_flush_start ();
532 if (seqnum != GST_SEQNUM_INVALID)
533 gst_event_set_seqnum (fl_event, seqnum);
534 gst_pad_push_event (rmdemux->sinkpad, fl_event);
536 fl_event = gst_event_new_flush_start ();
537 if (seqnum != GST_SEQNUM_INVALID)
538 gst_event_set_seqnum (fl_event, seqnum);
539 gst_rmdemux_send_event (rmdemux, fl_event);
541 gst_pad_pause_task (rmdemux->sinkpad);
544 GST_LOG_OBJECT (rmdemux, "Done starting flushes");
546 /* now grab the stream lock so that streaming cannot continue, for
547 * non flushing seeks when the element is in PAUSED this could block
549 GST_PAD_STREAM_LOCK (rmdemux->sinkpad);
551 GST_LOG_OBJECT (rmdemux, "Took streamlock");
554 if (!gst_segment_do_seek (&rmdemux->segment, rate, format, flags,
555 cur_type, cur, stop_type, stop, &update)) {
560 rmdemux->seek_seqnum = seqnum;
563 GST_DEBUG_OBJECT (rmdemux, "segment positions set to %" GST_TIME_FORMAT "-%"
564 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start),
565 GST_TIME_ARGS (rmdemux->segment.stop));
567 /* we need to stop flushing on the sinkpad as we're going to use it
568 * next. We can do this as we have the STREAM lock now. */
569 fl_event = gst_event_new_flush_stop (TRUE);
570 if (seqnum != GST_SEQNUM_INVALID)
571 gst_event_set_seqnum (fl_event, seqnum);
572 gst_pad_push_event (rmdemux->sinkpad, fl_event);
574 GST_LOG_OBJECT (rmdemux, "Pushed FLUSH_STOP event");
576 /* For each stream, find the first index offset equal to or before our seek
577 * target. Of these, find the smallest offset. That's where we seek to.
579 * Then we pull 4 bytes from that offset, and validate that we've seeked to a
580 * what looks like a plausible packet.
581 * If that fails, restart, with the seek target set to one less than the
582 * offset we just tried. If we run out of places to try, treat that as a fatal
585 if (!find_seek_offset_time (rmdemux, rmdemux->segment.position)) {
586 GST_LOG_OBJECT (rmdemux, "Failed to find seek offset by time");
591 GST_LOG_OBJECT (rmdemux, "Validating offset %u", rmdemux->offset);
592 validated = gst_rmdemux_validate_offset (rmdemux);
594 GST_INFO_OBJECT (rmdemux, "Failed to validate offset at %u",
596 if (!find_seek_offset_bytes (rmdemux, rmdemux->offset - 1)) {
600 validated = gst_rmdemux_validate_offset (rmdemux);
603 GST_LOG_OBJECT (rmdemux, "Found final offset. Excellent!");
605 /* now we have a new position, prepare for streaming again */
607 /* Reset the demuxer state */
608 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
611 fl_event = gst_event_new_flush_stop (TRUE);
612 if (seqnum != GST_SEQNUM_INVALID)
613 gst_event_set_seqnum (fl_event, seqnum);
614 gst_rmdemux_send_event (rmdemux, fl_event);
617 /* must send newsegment event from streaming thread, so just set flag */
618 rmdemux->need_newsegment = TRUE;
620 /* notify start of new segment */
621 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
623 gst_message_new_segment_start (GST_OBJECT_CAST (rmdemux),
624 GST_FORMAT_TIME, rmdemux->segment.position);
625 if (seqnum != GST_SEQNUM_INVALID)
626 gst_message_set_seqnum (msg, seqnum);
627 gst_element_post_message (GST_ELEMENT_CAST (rmdemux), msg);
630 /* restart our task since it might have been stopped when we did the
632 gst_pad_start_task (rmdemux->sinkpad, (GstTaskFunction) gst_rmdemux_loop,
633 rmdemux->sinkpad, NULL);
637 /* streaming can continue now */
638 GST_PAD_STREAM_UNLOCK (rmdemux->sinkpad);
644 GST_DEBUG_OBJECT (rmdemux, "seek failed");
651 gst_rmdemux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
653 gboolean res = FALSE;
656 rmdemux = GST_RMDEMUX (parent);
658 switch (GST_QUERY_TYPE (query)) {
659 case GST_QUERY_POSITION:
660 GST_DEBUG_OBJECT (rmdemux, "Position query: no idea from demuxer!");
662 case GST_QUERY_DURATION:{
665 gst_query_parse_duration (query, &fmt, NULL);
666 if (fmt == GST_FORMAT_TIME) {
667 GST_OBJECT_LOCK (rmdemux);
668 if (G_LIKELY (rmdemux->running)) {
669 gst_query_set_duration (query, GST_FORMAT_TIME, rmdemux->duration);
670 GST_DEBUG_OBJECT (rmdemux, "duration set to %" GST_TIME_FORMAT,
671 GST_TIME_ARGS (rmdemux->duration));
674 GST_OBJECT_UNLOCK (rmdemux);
678 case GST_QUERY_SEEKING:{
681 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
682 if (fmt == GST_FORMAT_TIME) {
683 GST_OBJECT_LOCK (rmdemux);
684 if (G_LIKELY (rmdemux->running)) {
685 gst_query_set_seeking (query, GST_FORMAT_TIME, rmdemux->seekable,
686 0, rmdemux->duration);
689 GST_OBJECT_UNLOCK (rmdemux);
693 case GST_QUERY_SEGMENT:
698 format = rmdemux->segment.format;
701 gst_segment_to_stream_time (&rmdemux->segment, format,
702 rmdemux->segment.start);
703 if ((stop = rmdemux->segment.stop) == -1)
704 stop = rmdemux->segment.duration;
706 stop = gst_segment_to_stream_time (&rmdemux->segment, format, stop);
708 gst_query_set_segment (query, rmdemux->segment.rate, format, start, stop);
713 res = gst_pad_query_default (pad, parent, query);
721 gst_rmdemux_free_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
723 g_object_unref (stream->adapter);
724 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
725 if (stream->pending_tags)
726 gst_tag_list_unref (stream->pending_tags);
727 if (stream->subpackets)
728 g_ptr_array_free (stream->subpackets, TRUE);
729 g_free (stream->index);
734 gst_rmdemux_reset (GstRMDemux * rmdemux)
738 GST_OBJECT_LOCK (rmdemux);
739 rmdemux->running = FALSE;
740 GST_OBJECT_UNLOCK (rmdemux);
742 for (cur = rmdemux->streams; cur; cur = cur->next) {
743 GstRMDemuxStream *stream = cur->data;
745 gst_flow_combiner_remove_pad (rmdemux->flowcombiner, stream->pad);
746 gst_element_remove_pad (GST_ELEMENT (rmdemux), stream->pad);
747 gst_rmdemux_free_stream (rmdemux, stream);
749 g_slist_free (rmdemux->streams);
750 rmdemux->streams = NULL;
751 rmdemux->n_audio_streams = 0;
752 rmdemux->n_video_streams = 0;
754 if (rmdemux->pending_tags != NULL) {
755 gst_tag_list_unref (rmdemux->pending_tags);
756 rmdemux->pending_tags = NULL;
759 gst_adapter_clear (rmdemux->adapter);
760 rmdemux->state = RMDEMUX_STATE_HEADER;
761 rmdemux->have_pads = FALSE;
763 gst_segment_init (&rmdemux->segment, GST_FORMAT_UNDEFINED);
764 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
765 rmdemux->base_ts = GST_CLOCK_TIME_NONE;
766 rmdemux->need_newsegment = TRUE;
768 rmdemux->have_group_id = FALSE;
769 rmdemux->group_id = G_MAXUINT;
771 rmdemux->seek_seqnum = GST_SEQNUM_INVALID;
774 static GstStateChangeReturn
775 gst_rmdemux_change_state (GstElement * element, GstStateChange transition)
777 GstRMDemux *rmdemux = GST_RMDEMUX (element);
778 GstStateChangeReturn res;
780 switch (transition) {
781 case GST_STATE_CHANGE_NULL_TO_READY:
783 case GST_STATE_CHANGE_READY_TO_PAUSED:
784 rmdemux->state = RMDEMUX_STATE_HEADER;
785 rmdemux->have_pads = FALSE;
786 gst_segment_init (&rmdemux->segment, GST_FORMAT_TIME);
787 rmdemux->running = FALSE;
789 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
795 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
797 switch (transition) {
798 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
800 case GST_STATE_CHANGE_PAUSED_TO_READY:{
801 gst_rmdemux_reset (rmdemux);
804 case GST_STATE_CHANGE_READY_TO_NULL:
813 /* this function is called when the pad is activated and should start
816 * We check if we can do random access to decide if we work push or
820 gst_rmdemux_sink_activate (GstPad * sinkpad, GstObject * parent)
825 query = gst_query_new_scheduling ();
827 if (!gst_pad_peer_query (sinkpad, query)) {
828 gst_query_unref (query);
832 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
833 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
834 gst_query_unref (query);
839 GST_DEBUG_OBJECT (sinkpad, "activating pull");
840 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
844 GST_DEBUG_OBJECT (sinkpad, "activating push");
845 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
850 gst_rmdemux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
851 GstPadMode mode, gboolean active)
856 demux = GST_RMDEMUX (parent);
859 case GST_PAD_MODE_PUSH:
860 demux->seekable = FALSE;
861 demux->running = active;
864 case GST_PAD_MODE_PULL:
866 demux->seekable = TRUE;
868 demux->loop_state = RMDEMUX_LOOP_STATE_HEADER;
869 demux->data_offset = G_MAXUINT;
871 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_rmdemux_loop,
874 res = gst_pad_stop_task (sinkpad);
885 /* random access mode - just pass over to our chain function */
887 gst_rmdemux_loop (GstPad * pad)
891 GstFlowReturn ret = GST_FLOW_OK;
894 rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
896 GST_LOG_OBJECT (rmdemux, "loop with state=%d and offset=0x%x",
897 rmdemux->loop_state, rmdemux->offset);
899 switch (rmdemux->state) {
900 case RMDEMUX_STATE_HEADER:
903 case RMDEMUX_STATE_HEADER_DATA:
906 case RMDEMUX_STATE_DATA_PACKET:
907 size = rmdemux->avg_packet_size;
909 case RMDEMUX_STATE_EOS:
910 GST_LOG_OBJECT (rmdemux, "At EOS, pausing task");
914 GST_LOG_OBJECT (rmdemux, "Default: requires %d bytes (state is %d)",
915 (int) rmdemux->size, rmdemux->state);
916 size = rmdemux->size;
920 ret = gst_pad_pull_range (pad, rmdemux->offset, size, &buffer);
921 if (ret != GST_FLOW_OK) {
922 if (rmdemux->offset == rmdemux->index_offset) {
923 /* The index isn't available so forget about it */
924 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
925 rmdemux->offset = rmdemux->data_offset;
926 GST_OBJECT_LOCK (rmdemux);
927 rmdemux->running = TRUE;
928 rmdemux->seekable = FALSE;
929 GST_OBJECT_UNLOCK (rmdemux);
932 GST_DEBUG_OBJECT (rmdemux, "Unable to pull %d bytes at offset 0x%08x "
933 "(pull_range returned flow %s, state is %d)", (gint) size,
934 rmdemux->offset, gst_flow_get_name (ret), GST_STATE (rmdemux));
939 size = gst_buffer_get_size (buffer);
941 /* Defer to the chain function */
942 ret = gst_rmdemux_chain (pad, GST_OBJECT_CAST (rmdemux), buffer);
943 if (ret != GST_FLOW_OK) {
944 GST_DEBUG_OBJECT (rmdemux, "Chain flow failed at offset 0x%08x",
949 rmdemux->offset += size;
951 switch (rmdemux->loop_state) {
952 case RMDEMUX_LOOP_STATE_HEADER:
953 if (rmdemux->offset >= rmdemux->data_offset) {
954 /* It's the end of the header */
955 rmdemux->loop_state = RMDEMUX_LOOP_STATE_INDEX;
956 rmdemux->offset = rmdemux->index_offset;
959 case RMDEMUX_LOOP_STATE_INDEX:
960 if (rmdemux->state == RMDEMUX_STATE_HEADER) {
961 if (rmdemux->index_offset == 0) {
962 /* We've read the last index */
963 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
964 rmdemux->offset = rmdemux->data_offset;
965 GST_OBJECT_LOCK (rmdemux);
966 rmdemux->running = TRUE;
967 GST_OBJECT_UNLOCK (rmdemux);
969 /* Get the next index */
970 rmdemux->offset = rmdemux->index_offset;
974 case RMDEMUX_LOOP_STATE_DATA:
983 const gchar *reason = gst_flow_get_name (ret);
985 GST_LOG_OBJECT (rmdemux, "pausing task, reason %s", reason);
986 rmdemux->segment_running = FALSE;
987 gst_pad_pause_task (rmdemux->sinkpad);
989 if (ret == GST_FLOW_EOS) {
990 /* perform EOS logic */
991 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
994 /* for segment playback we need to post when (in stream time)
995 * we stopped, this is either stop (when set) or the duration. */
996 if ((stop = rmdemux->segment.stop) == -1)
997 stop = rmdemux->segment.duration;
999 GST_LOG_OBJECT (rmdemux, "Sending segment done, at end of segment");
1000 gst_element_post_message (GST_ELEMENT (rmdemux),
1001 gst_message_new_segment_done (GST_OBJECT (rmdemux),
1002 GST_FORMAT_TIME, stop));
1003 gst_rmdemux_send_event (rmdemux,
1004 gst_event_new_segment_done (GST_FORMAT_TIME, stop));
1006 /* normal playback, send EOS to all linked pads */
1007 GST_LOG_OBJECT (rmdemux, "Sending EOS, at end of stream");
1008 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1010 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1011 GST_ELEMENT_FLOW_ERROR (rmdemux, ret);
1012 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1019 gst_rmdemux_fourcc_isplausible (guint32 fourcc)
1023 for (i = 0; i < 4; i++) {
1024 if (!isprint ((int) ((unsigned char *) (&fourcc))[i])) {
1031 static GstFlowReturn
1032 gst_rmdemux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1034 GstFlowReturn ret = GST_FLOW_OK;
1039 GstRMDemux *rmdemux = GST_RMDEMUX (parent);
1041 if (rmdemux->base_ts == -1) {
1042 if (GST_BUFFER_DTS_IS_VALID (buffer))
1043 rmdemux->base_ts = GST_BUFFER_DTS (buffer);
1045 rmdemux->base_ts = GST_BUFFER_PTS (buffer);
1047 GST_LOG_OBJECT (rmdemux, "base_ts %" GST_TIME_FORMAT,
1048 GST_TIME_ARGS (rmdemux->base_ts));
1051 gst_adapter_push (rmdemux->adapter, buffer);
1053 GST_LOG_OBJECT (rmdemux, "Chaining buffer of size %" G_GSIZE_FORMAT,
1054 gst_buffer_get_size (buffer));
1057 avail = gst_adapter_available (rmdemux->adapter);
1059 GST_LOG_OBJECT (rmdemux, "looping in chain, avail %u", avail);
1060 switch (rmdemux->state) {
1061 case RMDEMUX_STATE_HEADER:
1063 if (gst_adapter_available (rmdemux->adapter) < HEADER_SIZE)
1066 data = gst_adapter_map (rmdemux->adapter, HEADER_SIZE);
1068 rmdemux->object_id = RMDEMUX_FOURCC_GET (data + 0);
1069 rmdemux->size = RMDEMUX_GUINT32_GET (data + 4) - HEADER_SIZE;
1070 rmdemux->object_version = RMDEMUX_GUINT16_GET (data + 8);
1072 /* Sanity-check. We assume that the FOURCC is printable ASCII */
1073 if (!gst_rmdemux_fourcc_isplausible (rmdemux->object_id)) {
1074 /* Failed. Remain in HEADER state, try again... We flush only
1075 * the actual FOURCC, not the entire header, because we could
1076 * need to resync anywhere at all... really, this should never
1078 GST_WARNING_OBJECT (rmdemux, "Bogus looking header, unprintable "
1080 gst_adapter_unmap (rmdemux->adapter);
1081 gst_adapter_flush (rmdemux->adapter, 4);
1086 GST_LOG_OBJECT (rmdemux, "header found with object_id=%"
1088 " size=%08x object_version=%d",
1089 GST_FOURCC_ARGS (rmdemux->object_id), rmdemux->size,
1090 rmdemux->object_version);
1092 gst_adapter_unmap (rmdemux->adapter);
1093 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1095 switch (rmdemux->object_id) {
1096 case GST_MAKE_FOURCC ('.', 'R', 'M', 'F'):
1097 rmdemux->state = RMDEMUX_STATE_HEADER_RMF;
1099 case GST_MAKE_FOURCC ('P', 'R', 'O', 'P'):
1100 rmdemux->state = RMDEMUX_STATE_HEADER_PROP;
1102 case GST_MAKE_FOURCC ('M', 'D', 'P', 'R'):
1103 rmdemux->state = RMDEMUX_STATE_HEADER_MDPR;
1105 case GST_MAKE_FOURCC ('I', 'N', 'D', 'X'):
1106 rmdemux->state = RMDEMUX_STATE_HEADER_INDX;
1108 case GST_MAKE_FOURCC ('D', 'A', 'T', 'A'):
1109 rmdemux->state = RMDEMUX_STATE_HEADER_DATA;
1111 case GST_MAKE_FOURCC ('C', 'O', 'N', 'T'):
1112 rmdemux->state = RMDEMUX_STATE_HEADER_CONT;
1115 rmdemux->state = RMDEMUX_STATE_HEADER_UNKNOWN;
1120 case RMDEMUX_STATE_HEADER_UNKNOWN:
1122 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1125 GST_WARNING_OBJECT (rmdemux, "Unknown object_id %" GST_FOURCC_FORMAT,
1126 GST_FOURCC_ARGS (rmdemux->object_id));
1128 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1129 rmdemux->state = RMDEMUX_STATE_HEADER;
1132 case RMDEMUX_STATE_HEADER_RMF:
1134 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1137 if ((rmdemux->object_version == 0) || (rmdemux->object_version == 1)) {
1138 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1139 gst_rmdemux_parse__rmf (rmdemux, data, rmdemux->size);
1140 gst_adapter_unmap (rmdemux->adapter);
1141 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1143 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1145 rmdemux->state = RMDEMUX_STATE_HEADER;
1148 case RMDEMUX_STATE_HEADER_PROP:
1150 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1153 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1154 gst_rmdemux_parse_prop (rmdemux, data, rmdemux->size);
1155 gst_adapter_unmap (rmdemux->adapter);
1156 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1158 rmdemux->state = RMDEMUX_STATE_HEADER;
1161 case RMDEMUX_STATE_HEADER_MDPR:
1163 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1166 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1167 gst_rmdemux_parse_mdpr (rmdemux, data, rmdemux->size);
1168 gst_adapter_unmap (rmdemux->adapter);
1169 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1171 rmdemux->state = RMDEMUX_STATE_HEADER;
1174 case RMDEMUX_STATE_HEADER_CONT:
1176 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1179 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1180 gst_rmdemux_parse_cont (rmdemux, data, rmdemux->size);
1181 gst_adapter_unmap (rmdemux->adapter);
1182 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1184 rmdemux->state = RMDEMUX_STATE_HEADER;
1187 case RMDEMUX_STATE_HEADER_DATA:
1189 /* If we haven't already done so then signal there are no more pads */
1190 if (!rmdemux->have_pads) {
1191 GST_LOG_OBJECT (rmdemux, "no more pads");
1192 gst_element_no_more_pads (GST_ELEMENT (rmdemux));
1193 rmdemux->have_pads = TRUE;
1196 /* The actual header is only 8 bytes */
1197 rmdemux->size = DATA_SIZE;
1198 GST_LOG_OBJECT (rmdemux, "data available %" G_GSIZE_FORMAT,
1199 gst_adapter_available (rmdemux->adapter));
1200 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1203 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1204 gst_rmdemux_parse_data (rmdemux, data, rmdemux->size);
1205 gst_adapter_unmap (rmdemux->adapter);
1206 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1208 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
1211 case RMDEMUX_STATE_HEADER_INDX:
1213 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1216 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1217 rmdemux->size = gst_rmdemux_parse_indx (rmdemux, data, rmdemux->size);
1218 /* Only flush the header */
1219 gst_adapter_unmap (rmdemux->adapter);
1220 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1222 rmdemux->state = RMDEMUX_STATE_INDX_DATA;
1225 case RMDEMUX_STATE_INDX_DATA:
1227 /* There's not always an data to get... */
1228 if (rmdemux->size > 0) {
1229 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1232 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1233 gst_rmdemux_parse_indx_data (rmdemux, data, rmdemux->size);
1234 gst_adapter_unmap (rmdemux->adapter);
1235 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1238 rmdemux->state = RMDEMUX_STATE_HEADER;
1241 case RMDEMUX_STATE_DATA_PACKET:
1245 if (gst_adapter_available (rmdemux->adapter) < 2)
1248 gst_adapter_copy (rmdemux->adapter, header, 0, 2);
1249 version = RMDEMUX_GUINT16_GET (header);
1250 GST_LOG_OBJECT (rmdemux, "Data packet with version=%d", version);
1252 if (version == 0 || version == 1) {
1255 if (gst_adapter_available (rmdemux->adapter) < 4)
1258 gst_adapter_copy (rmdemux->adapter, header, 0, 4);
1260 length = RMDEMUX_GUINT16_GET (header + 2);
1261 GST_LOG_OBJECT (rmdemux, "Got length %d", length);
1264 GST_LOG_OBJECT (rmdemux, "length too small, dropping");
1265 /* Invalid, just drop it */
1266 gst_adapter_flush (rmdemux->adapter, 4);
1270 avail = gst_adapter_available (rmdemux->adapter);
1274 GST_LOG_OBJECT (rmdemux, "we have %u available and we needed %d",
1277 /* flush version and length */
1278 gst_adapter_flush (rmdemux->adapter, 4);
1281 buffer = gst_adapter_take_buffer (rmdemux->adapter, length);
1283 ret = gst_rmdemux_parse_packet (rmdemux, buffer, version);
1284 rmdemux->chunk_index++;
1287 if (rmdemux->chunk_index == rmdemux->n_chunks || length == 0)
1288 rmdemux->state = RMDEMUX_STATE_HEADER;
1291 gst_adapter_flush (rmdemux->adapter, 2);
1293 if (rmdemux->data_offset == 0) {
1294 GST_LOG_OBJECT (rmdemux,
1295 "No further data, internal demux state EOS");
1296 rmdemux->state = RMDEMUX_STATE_EOS;
1298 rmdemux->state = RMDEMUX_STATE_HEADER;
1302 case RMDEMUX_STATE_EOS:
1303 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1306 GST_WARNING_OBJECT (rmdemux, "Unhandled state %d", rmdemux->state);
1315 static GstRMDemuxStream *
1316 gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux, int id)
1320 for (cur = rmdemux->streams; cur; cur = cur->next) {
1321 GstRMDemuxStream *stream = cur->data;
1323 if (stream->id == id) {
1332 gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event)
1336 for (cur = rmdemux->streams; cur; cur = cur->next) {
1337 GstRMDemuxStream *stream = cur->data;
1339 GST_DEBUG_OBJECT (rmdemux, "Pushing %s event on pad %s",
1340 GST_EVENT_TYPE_NAME (event), GST_PAD_NAME (stream->pad));
1342 switch (GST_EVENT_TYPE (event)) {
1343 case GST_EVENT_FLUSH_STOP:
1344 stream->last_ts = -1;
1345 stream->next_ts = -1;
1346 stream->last_seq = -1;
1347 stream->next_seq = -1;
1352 gst_event_ref (event);
1353 gst_pad_push_event (stream->pad, event);
1355 gst_event_unref (event);
1359 gst_rmdemux_add_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
1361 GstCaps *stream_caps = NULL;
1362 const gchar *codec_tag = NULL;
1363 gchar *codec_name = NULL;
1367 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
1368 char *name = g_strdup_printf ("video_%u", rmdemux->n_video_streams);
1371 gst_pad_new_from_static_template (&gst_rmdemux_videosrc_template, name);
1374 codec_tag = GST_TAG_VIDEO_CODEC;
1376 switch (stream->fourcc) {
1377 case GST_RM_VDO_RV10:
1380 case GST_RM_VDO_RV20:
1383 case GST_RM_VDO_RV30:
1386 case GST_RM_VDO_RV40:
1390 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1391 "fourcc", G_TYPE_UINT, stream->fourcc, NULL);
1392 GST_WARNING_OBJECT (rmdemux,
1393 "Unknown video FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1394 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1399 gst_caps_new_simple ("video/x-pn-realvideo", "rmversion", G_TYPE_INT,
1401 "format", G_TYPE_INT,
1402 (int) stream->format,
1403 "subformat", G_TYPE_INT, (int) stream->subformat, NULL);
1407 gst_caps_set_simple (stream_caps,
1408 "width", G_TYPE_INT, stream->width,
1409 "height", G_TYPE_INT, stream->height,
1410 "framerate", GST_TYPE_FRACTION, stream->framerate_numerator,
1411 stream->framerate_denominator, NULL);
1413 rmdemux->n_video_streams++;
1415 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
1416 char *name = g_strdup_printf ("audio_%u", rmdemux->n_audio_streams);
1419 gst_pad_new_from_static_template (&gst_rmdemux_audiosrc_template, name);
1420 GST_LOG_OBJECT (rmdemux, "Created audio pad \"%s\"", name);
1423 codec_tag = GST_TAG_AUDIO_CODEC;
1425 switch (stream->fourcc) {
1426 /* Older RealAudio Codecs */
1427 case GST_RM_AUD_14_4:
1431 case GST_RM_AUD_28_8:
1435 /* DolbyNet (Dolby AC3, low bitrate) */
1436 case GST_RM_AUD_DNET:
1438 gst_caps_new_simple ("audio/x-ac3", "rate", G_TYPE_INT,
1439 (int) stream->rate, NULL);
1440 stream->needs_descrambling = TRUE;
1441 stream->subpackets_needed = 1;
1442 stream->subpackets = NULL;
1446 case GST_RM_AUD_RAAC:
1447 case GST_RM_AUD_RACP:
1449 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
1450 (int) 4, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
1451 if (stream->extra_data_size > 0) {
1452 /* strip off an unknown byte in the extra data */
1453 stream->extra_data_size--;
1454 stream->extra_data++;
1456 stream->needs_descrambling = TRUE;
1457 stream->subpackets_needed = 1;
1458 stream->subpackets = NULL;
1462 case GST_RM_AUD_ATRC:
1463 stream_caps = gst_caps_new_empty_simple ("audio/x-vnd.sony.atrac3");
1464 stream->needs_descrambling = TRUE;
1465 stream->subpackets_needed = stream->height;
1466 stream->subpackets = NULL;
1469 /* RealAudio G2 audio */
1470 case GST_RM_AUD_COOK:
1472 stream->needs_descrambling = TRUE;
1473 stream->subpackets_needed = stream->height;
1474 stream->subpackets = NULL;
1477 /* RALF is lossless */
1478 case GST_RM_AUD_RALF:
1479 GST_DEBUG_OBJECT (rmdemux, "RALF");
1480 stream_caps = gst_caps_new_empty_simple ("audio/x-ralf-mpeg4-generic");
1483 case GST_RM_AUD_SIPR:
1485 if (stream->flavor > 3) {
1486 GST_WARNING_OBJECT (rmdemux, "bad SIPR flavor %d, freeing it",
1488 g_object_unref (stream->pad);
1489 gst_rmdemux_free_stream (rmdemux, stream);
1493 GST_DEBUG_OBJECT (rmdemux, "SIPR");
1494 stream_caps = gst_caps_new_empty_simple ("audio/x-sipro");
1495 stream->needs_descrambling = TRUE;
1496 stream->subpackets_needed = stream->height;
1497 stream->subpackets = NULL;
1498 stream->leaf_size = sipr_subpk_size[stream->flavor];
1503 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1504 "fourcc", G_TYPE_UINT, stream->fourcc, NULL);
1505 GST_WARNING_OBJECT (rmdemux,
1506 "Unknown audio FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1507 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1513 gst_caps_new_simple ("audio/x-pn-realaudio", "raversion", G_TYPE_INT,
1514 (int) version, NULL);
1518 gst_caps_set_simple (stream_caps,
1519 "flavor", G_TYPE_INT, (int) stream->flavor,
1520 "rate", G_TYPE_INT, (int) stream->rate,
1521 "channels", G_TYPE_INT, (int) stream->n_channels,
1522 "width", G_TYPE_INT, (int) stream->sample_width,
1523 "leaf_size", G_TYPE_INT, (int) stream->leaf_size,
1524 "packet_size", G_TYPE_INT, (int) stream->packet_size,
1525 "bitrate", G_TYPE_INT, (int) stream->bitrate,
1526 "height", G_TYPE_INT, (int) stream->height, NULL);
1528 rmdemux->n_audio_streams++;
1530 GST_WARNING_OBJECT (rmdemux, "not adding stream of type %d, freeing it",
1532 gst_rmdemux_free_stream (rmdemux, stream);
1536 GST_PAD_ELEMENT_PRIVATE (stream->pad) = stream;
1537 rmdemux->streams = g_slist_append (rmdemux->streams, stream);
1538 GST_LOG_OBJECT (rmdemux, "n_streams is now %d",
1539 g_slist_length (rmdemux->streams));
1541 GST_LOG ("stream->pad = %p, stream_caps = %" GST_PTR_FORMAT, stream->pad,
1544 if (stream->pad && stream_caps) {
1547 GST_LOG_OBJECT (rmdemux, "%d bytes of extra data for stream %s",
1548 stream->extra_data_size, GST_PAD_NAME (stream->pad));
1550 /* add codec_data if there is any */
1551 if (stream->extra_data_size > 0) {
1554 buffer = gst_buffer_new_and_alloc (stream->extra_data_size);
1555 gst_buffer_fill (buffer, 0, stream->extra_data, stream->extra_data_size);
1557 gst_caps_set_simple (stream_caps, "codec_data", GST_TYPE_BUFFER,
1560 gst_buffer_unref (buffer);
1563 gst_pad_use_fixed_caps (stream->pad);
1565 gst_pad_set_event_function (stream->pad,
1566 GST_DEBUG_FUNCPTR (gst_rmdemux_src_event));
1567 gst_pad_set_query_function (stream->pad,
1568 GST_DEBUG_FUNCPTR (gst_rmdemux_src_query));
1570 GST_DEBUG_OBJECT (rmdemux, "adding pad %s with caps %" GST_PTR_FORMAT
1571 ", stream_id=%d", GST_PAD_NAME (stream->pad), stream_caps, stream->id);
1572 gst_pad_set_active (stream->pad, TRUE);
1575 gst_pad_create_stream_id_printf (stream->pad,
1576 GST_ELEMENT_CAST (rmdemux), "%03u", stream->id);
1579 gst_pad_get_sticky_event (rmdemux->sinkpad, GST_EVENT_STREAM_START, 0);
1581 if (gst_event_parse_group_id (event, &rmdemux->group_id))
1582 rmdemux->have_group_id = TRUE;
1584 rmdemux->have_group_id = FALSE;
1585 gst_event_unref (event);
1586 } else if (!rmdemux->have_group_id) {
1587 rmdemux->have_group_id = TRUE;
1588 rmdemux->group_id = gst_util_group_id_next ();
1591 event = gst_event_new_stream_start (stream_id);
1592 if (rmdemux->have_group_id)
1593 gst_event_set_group_id (event, rmdemux->group_id);
1595 gst_pad_push_event (stream->pad, event);
1598 gst_pad_set_caps (stream->pad, stream_caps);
1600 codec_name = gst_pb_utils_get_codec_description (stream_caps);
1602 /* save for later, we must send the tags after the newsegment event */
1603 if (codec_tag != NULL && codec_name != NULL) {
1604 if (stream->pending_tags == NULL)
1605 stream->pending_tags = gst_tag_list_new_empty ();
1606 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_KEEP,
1607 codec_tag, codec_name, NULL);
1608 g_free (codec_name);
1610 gst_element_add_pad (GST_ELEMENT_CAST (rmdemux), stream->pad);
1611 gst_flow_combiner_add_pad (rmdemux->flowcombiner, stream->pad);
1617 gst_caps_unref (stream_caps);
1621 re_skip_pascal_string (const guint8 * ptr)
1631 gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data, int length)
1633 GST_LOG_OBJECT (rmdemux, "file_version: %d", RMDEMUX_GUINT32_GET (data));
1634 GST_LOG_OBJECT (rmdemux, "num_headers: %d", RMDEMUX_GUINT32_GET (data + 4));
1638 gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data, int length)
1640 GST_LOG_OBJECT (rmdemux, "max bitrate: %d", RMDEMUX_GUINT32_GET (data));
1641 GST_LOG_OBJECT (rmdemux, "avg bitrate: %d", RMDEMUX_GUINT32_GET (data + 4));
1642 GST_LOG_OBJECT (rmdemux, "max packet size: %d",
1643 RMDEMUX_GUINT32_GET (data + 8));
1644 rmdemux->avg_packet_size = RMDEMUX_GUINT32_GET (data + 12);
1645 GST_LOG_OBJECT (rmdemux, "avg packet size: %d", rmdemux->avg_packet_size);
1646 rmdemux->num_packets = RMDEMUX_GUINT32_GET (data + 16);
1647 GST_LOG_OBJECT (rmdemux, "number of packets: %d", rmdemux->num_packets);
1649 GST_LOG_OBJECT (rmdemux, "duration: %d", RMDEMUX_GUINT32_GET (data + 20));
1650 rmdemux->duration = RMDEMUX_GUINT32_GET (data + 20) * GST_MSECOND;
1652 GST_LOG_OBJECT (rmdemux, "preroll: %d", RMDEMUX_GUINT32_GET (data + 24));
1653 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 28);
1654 GST_LOG_OBJECT (rmdemux, "offset of INDX section: 0x%08x",
1655 rmdemux->index_offset);
1656 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 32);
1657 GST_LOG_OBJECT (rmdemux, "offset of DATA section: 0x%08x",
1658 rmdemux->data_offset);
1659 GST_LOG_OBJECT (rmdemux, "n streams: %d", RMDEMUX_GUINT16_GET (data + 36));
1660 GST_LOG_OBJECT (rmdemux, "flags: 0x%04x", RMDEMUX_GUINT16_GET (data + 38));
1664 gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux, const guint8 * data, int length)
1666 GstRMDemuxStream *stream;
1667 char *stream1_type_string;
1668 char *stream2_type_string;
1672 guint32 max_bitrate;
1673 guint32 avg_bitrate;
1675 stream = g_new0 (GstRMDemuxStream, 1);
1677 stream->id = RMDEMUX_GUINT16_GET (data);
1678 stream->index = NULL;
1679 stream->seek_offset = 0;
1680 stream->last_ts = -1;
1681 stream->next_ts = -1;
1682 stream->discont = TRUE;
1683 stream->adapter = gst_adapter_new ();
1684 GST_LOG_OBJECT (rmdemux, "stream_number=%d", stream->id);
1686 /* parse the bitrates */
1687 max_bitrate = RMDEMUX_GUINT32_GET (data + 2);
1688 avg_bitrate = RMDEMUX_GUINT32_GET (data + 6);
1689 stream->bitrate = avg_bitrate;
1690 GST_LOG_OBJECT (rmdemux, "Stream max bitrate=%u", max_bitrate);
1691 GST_LOG_OBJECT (rmdemux, "Stream avg bitrate=%u", avg_bitrate);
1692 if (max_bitrate != 0) {
1693 if (stream->pending_tags == NULL)
1694 stream->pending_tags = gst_tag_list_new_empty ();
1695 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1696 GST_TAG_MAXIMUM_BITRATE, max_bitrate, NULL);
1698 if (avg_bitrate != 0) {
1699 if (stream->pending_tags == NULL)
1700 stream->pending_tags = gst_tag_list_new_empty ();
1701 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1702 GST_TAG_BITRATE, avg_bitrate, NULL);
1706 stream1_type_string = gst_rm_utils_read_string8 (data + offset,
1707 length - offset, &str_len);
1709 stream2_type_string = gst_rm_utils_read_string8 (data + offset,
1710 length - offset, &str_len);
1713 /* stream1_type_string for audio and video stream is a "put_whatever_you_want" field :
1715 * - "[The ]Video/Audio Stream" (File produced by an official Real encoder)
1716 * - "RealVideoPremierePlugIn-VIDEO/AUDIO" (File produced by Abobe Premiere)
1718 * so, we should not rely on it to know which stream type it is
1721 GST_LOG_OBJECT (rmdemux, "stream type: %s", stream1_type_string);
1722 GST_LOG_OBJECT (rmdemux, "MIME type=%s", stream2_type_string);
1724 if (strcmp (stream2_type_string, "video/x-pn-realvideo") == 0) {
1725 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1726 } else if (strcmp (stream2_type_string,
1727 "video/x-pn-multirate-realvideo") == 0) {
1728 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1729 } else if (strcmp (stream2_type_string, "audio/x-pn-realaudio") == 0) {
1730 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1731 } else if (strcmp (stream2_type_string,
1732 "audio/x-pn-multirate-realaudio") == 0) {
1733 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1734 } else if (strcmp (stream2_type_string,
1735 "audio/x-pn-multirate-realaudio-live") == 0) {
1736 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1737 } else if (strcmp (stream2_type_string, "audio/x-ralf-mpeg4-generic") == 0) {
1738 /* Another audio type found in the real testsuite */
1739 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1740 } else if (strcmp (stream1_type_string, "") == 0 &&
1741 strcmp (stream2_type_string, "logical-fileinfo") == 0) {
1742 stream_type = GST_RMDEMUX_STREAM_FILEINFO;
1744 stream_type = GST_RMDEMUX_STREAM_UNKNOWN;
1745 GST_WARNING_OBJECT (rmdemux, "unknown stream type \"%s\",\"%s\"",
1746 stream1_type_string, stream2_type_string);
1748 g_free (stream1_type_string);
1749 g_free (stream2_type_string);
1753 stream->subtype = stream_type;
1754 switch (stream_type) {
1756 case GST_RMDEMUX_STREAM_VIDEO:
1757 /* RV10/RV20/RV30/RV40 => video/x-pn-realvideo, version=1,2,3,4 */
1758 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 8);
1759 stream->width = RMDEMUX_GUINT16_GET (data + offset + 12);
1760 stream->height = RMDEMUX_GUINT16_GET (data + offset + 14);
1761 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 16);
1762 stream->subformat = RMDEMUX_GUINT32_GET (data + offset + 26);
1763 stream->format = RMDEMUX_GUINT32_GET (data + offset + 30);
1764 stream->extra_data_size = length - (offset + 26);
1765 stream->extra_data = (guint8 *) data + offset + 26;
1766 /* Natural way to represent framerates here requires unsigned 32 bit
1767 * numerator, which we don't have. For the nasty case, approximate...
1770 guint32 numerator = RMDEMUX_GUINT16_GET (data + offset + 22) * 65536 +
1771 RMDEMUX_GUINT16_GET (data + offset + 24);
1772 if (numerator > G_MAXINT) {
1773 stream->framerate_numerator = (gint) (numerator >> 1);
1774 stream->framerate_denominator = 32768;
1776 stream->framerate_numerator = (gint) numerator;
1777 stream->framerate_denominator = 65536;
1781 GST_DEBUG_OBJECT (rmdemux,
1782 "Video stream with fourcc=%" GST_FOURCC_FORMAT
1783 " width=%d height=%d rate=%d framerate=%d/%d subformat=%x format=%x extra_data_size=%d",
1784 GST_FOURCC_ARGS (stream->fourcc), stream->width, stream->height,
1785 stream->rate, stream->framerate_numerator,
1786 stream->framerate_denominator, stream->subformat, stream->format,
1787 stream->extra_data_size);
1789 case GST_RMDEMUX_STREAM_AUDIO:{
1790 stream->version = RMDEMUX_GUINT16_GET (data + offset + 4);
1791 GST_INFO ("stream version = %u", stream->version);
1792 switch (stream->version) {
1794 stream->fourcc = GST_RM_AUD_14_4;
1795 stream->packet_size = 20;
1796 stream->rate = 8000;
1797 stream->n_channels = 1;
1798 stream->sample_width = 16;
1800 stream->leaf_size = 0;
1804 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1805 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1806 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1807 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1808 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1809 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 48);
1810 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 52);
1811 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 54);
1812 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 62);
1813 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 69);
1814 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1815 stream->extra_data_size);
1816 if (length - (offset + 73) >= stream->extra_data_size) {
1817 stream->extra_data = (guint8 *) data + offset + 73;
1819 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1820 stream->extra_data_size = 0;
1824 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1825 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1826 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1827 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1828 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1829 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 54);
1830 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 58);
1831 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 60);
1832 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 66);
1833 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 74);
1834 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1835 stream->extra_data_size);
1836 if (length - (offset + 78) >= stream->extra_data_size) {
1837 stream->extra_data = (guint8 *) data + offset + 78;
1839 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1840 stream->extra_data_size = 0;
1844 GST_WARNING_OBJECT (rmdemux, "Unhandled audio stream version %d",
1849 /* 14_4, 28_8, cook, dnet, sipr, raac, racp, ralf, atrc */
1850 GST_DEBUG_OBJECT (rmdemux,
1851 "Audio stream with rate=%d sample_width=%d n_channels=%d",
1852 stream->rate, stream->sample_width, stream->n_channels);
1856 case GST_RMDEMUX_STREAM_FILEINFO:
1860 /* Length of this section */
1861 GST_DEBUG_OBJECT (rmdemux, "length2: 0x%08x",
1862 RMDEMUX_GUINT32_GET (data + offset));
1865 /* Unknown : 00 00 00 00 */
1868 /* Number of variables that would follow (loop iterations) */
1869 element_nb = RMDEMUX_GUINT32_GET (data + offset);
1872 while (element_nb) {
1873 /* Category Id : 00 00 00 XX 00 00 */
1877 offset += re_skip_pascal_string (data + offset);
1879 /* Variable Value Type */
1880 /* 00 00 00 00 00 => integer/boolean, preceded by length */
1881 /* 00 00 00 02 00 => pascal string, preceded by length, no trailing \0 */
1884 /* Variable Value */
1885 offset += re_skip_pascal_string (data + offset);
1891 case GST_RMDEMUX_STREAM_UNKNOWN:
1896 gst_rmdemux_add_stream (rmdemux, stream);
1900 gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data, int length)
1905 n = RMDEMUX_GUINT32_GET (data);
1906 id = RMDEMUX_GUINT16_GET (data + 4);
1907 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 6);
1909 GST_DEBUG_OBJECT (rmdemux, "Number of indices=%d Stream ID=%d length=%d", n,
1912 /* Point to the next index_stream */
1913 rmdemux->index_stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
1915 /* Return the length of the index */
1920 gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux, const guint8 * data,
1925 GstRMDemuxIndex *index;
1927 /* The number of index records */
1930 if (rmdemux->index_stream == NULL)
1933 /* don't parse the index a second time when operating pull-based and
1934 * reaching the end of the file */
1935 if (rmdemux->index_stream->index_length > 0) {
1936 GST_DEBUG_OBJECT (rmdemux, "Already have an index for this stream");
1940 index = g_malloc (sizeof (GstRMDemuxIndex) * n);
1941 rmdemux->index_stream->index = index;
1942 rmdemux->index_stream->index_length = n;
1944 for (i = 0; i < n; i++) {
1945 index[i].timestamp = RMDEMUX_GUINT32_GET (data + 2) * GST_MSECOND;
1946 index[i].offset = RMDEMUX_GUINT32_GET (data + 6);
1948 GST_DEBUG_OBJECT (rmdemux, "Index found for timestamp=%f (at offset=%x)",
1949 gst_guint64_to_gdouble (index[i].timestamp) / GST_SECOND,
1956 gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data, int length)
1958 rmdemux->n_chunks = RMDEMUX_GUINT32_GET (data);
1959 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 4);
1960 rmdemux->chunk_index = 0;
1961 GST_DEBUG_OBJECT (rmdemux, "Data chunk found with %d packets "
1962 "(next data at 0x%08x)", rmdemux->n_chunks, rmdemux->data_offset);
1966 gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data, int length)
1970 tags = gst_rm_utils_read_tags (data, length, gst_rm_utils_read_string16);
1973 GstTagList *old_tags = rmdemux->pending_tags;
1975 GST_LOG_OBJECT (rmdemux, "tags: %" GST_PTR_FORMAT, tags);
1977 rmdemux->pending_tags =
1978 gst_tag_list_merge (old_tags, tags, GST_TAG_MERGE_APPEND);
1980 gst_tag_list_unref (tags);
1982 gst_tag_list_unref (old_tags);
1984 gst_tag_list_set_scope (rmdemux->pending_tags, GST_TAG_SCOPE_GLOBAL);
1989 gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
1990 GstRMDemuxStream * stream)
1992 if (stream->subpackets == NULL || stream->subpackets->len == 0)
1995 GST_DEBUG_OBJECT (rmdemux, "discarding %u previously collected subpackets",
1996 stream->subpackets->len);
1997 g_ptr_array_foreach (stream->subpackets, (GFunc) gst_mini_object_unref, NULL);
1998 g_ptr_array_set_size (stream->subpackets, 0);
2001 static GstFlowReturn
2002 gst_rmdemux_descramble_audio (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
2004 GstFlowReturn ret = GST_FLOW_ERROR;
2007 guint packet_size = stream->packet_size;
2008 guint height = stream->subpackets->len;
2009 guint leaf_size = stream->leaf_size;
2012 g_assert (stream->height == height);
2014 GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size,
2017 outbuf = gst_buffer_new_and_alloc (height * packet_size);
2018 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
2020 for (p = 0; p < height; ++p) {
2021 GstBuffer *b = g_ptr_array_index (stream->subpackets, p);
2024 gst_buffer_map (b, &map, GST_MAP_READ);
2027 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (b);
2028 GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (b);
2031 for (x = 0; x < packet_size / leaf_size; ++x) {
2034 idx = height * x + ((height + 1) / 2) * (p % 2) + (p / 2);
2036 /* GST_LOG ("%3u => %3u", (height * p) + x, idx); */
2037 memcpy (outmap.data + leaf_size * idx, map.data + leaf_size * x,
2040 gst_buffer_unmap (b, &map);
2042 gst_buffer_unmap (outbuf, &outmap);
2044 /* some decoders, such as realaudiodec, need to be fed in packet units */
2045 for (p = 0; p < height; ++p) {
2049 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, p * packet_size,
2052 GST_LOG_OBJECT (rmdemux, "pushing buffer dts %" GST_TIME_FORMAT ", pts %"
2053 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (subbuf)),
2054 GST_TIME_ARGS (GST_BUFFER_PTS (subbuf)));
2056 if (stream->discont) {
2057 GST_BUFFER_FLAG_SET (subbuf, GST_BUFFER_FLAG_DISCONT);
2058 stream->discont = FALSE;
2061 ret = gst_pad_push (stream->pad, subbuf);
2062 if (ret != GST_FLOW_OK)
2066 gst_buffer_unref (outbuf);
2068 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2073 static GstFlowReturn
2074 gst_rmdemux_descramble_dnet_audio (GstRMDemux * rmdemux,
2075 GstRMDemuxStream * stream)
2079 buf = g_ptr_array_index (stream->subpackets, 0);
2080 g_ptr_array_index (stream->subpackets, 0) = NULL;
2081 g_ptr_array_set_size (stream->subpackets, 0);
2083 buf = gst_rm_utils_descramble_dnet_buffer (buf);
2085 if (stream->discont) {
2086 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2087 stream->discont = FALSE;
2089 return gst_pad_push (stream->pad, buf);
2092 static GstFlowReturn
2093 gst_rmdemux_descramble_mp4a_audio (GstRMDemux * rmdemux,
2094 GstRMDemuxStream * stream)
2097 GstBuffer *buf, *outbuf;
2098 guint frames, index, i;
2100 GstClockTime timestamp;
2104 buf = g_ptr_array_index (stream->subpackets, 0);
2105 g_ptr_array_index (stream->subpackets, 0) = NULL;
2106 g_ptr_array_set_size (stream->subpackets, 0);
2108 gst_buffer_map (buf, &map, GST_MAP_READ);
2109 timestamp = GST_BUFFER_PTS (buf);
2111 frames = (map.data[1] & 0xf0) >> 4;
2112 index = 2 * frames + 2;
2114 for (i = 0; i < frames; i++) {
2115 guint len = (map.data[i * 2 + 2] << 8) | map.data[i * 2 + 3];
2117 outbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, index, len);
2119 GST_BUFFER_PTS (outbuf) = timestamp;
2120 GST_BUFFER_DTS (outbuf) = timestamp;
2125 if (stream->discont) {
2126 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2127 stream->discont = FALSE;
2129 res = gst_pad_push (stream->pad, outbuf);
2130 if (res != GST_FLOW_OK)
2133 gst_buffer_unmap (buf, &map);
2134 gst_buffer_unref (buf);
2138 static GstFlowReturn
2139 gst_rmdemux_descramble_sipr_audio (GstRMDemux * rmdemux,
2140 GstRMDemuxStream * stream)
2145 guint packet_size = stream->packet_size;
2146 guint height = stream->subpackets->len;
2149 g_assert (stream->height == height);
2151 GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size,
2152 stream->leaf_size, height);
2154 outbuf = gst_buffer_new_and_alloc (height * packet_size);
2155 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
2157 for (p = 0; p < height; ++p) {
2158 GstBuffer *b = g_ptr_array_index (stream->subpackets, p);
2161 GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (b);
2162 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (b);
2165 gst_buffer_extract (b, 0, outmap.data + packet_size * p, packet_size);
2167 gst_buffer_unmap (outbuf, &outmap);
2169 GST_LOG_OBJECT (rmdemux, "pushing buffer dts %" GST_TIME_FORMAT ", pts %"
2170 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (outbuf)),
2171 GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)));
2173 if (stream->discont) {
2174 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2175 stream->discont = FALSE;
2178 outbuf = gst_rm_utils_descramble_sipr_buffer (outbuf);
2180 ret = gst_pad_push (stream->pad, outbuf);
2182 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2187 static GstFlowReturn
2188 gst_rmdemux_handle_scrambled_packet (GstRMDemux * rmdemux,
2189 GstRMDemuxStream * stream, GstBuffer * buf, gboolean keyframe)
2193 if (stream->subpackets == NULL)
2194 stream->subpackets = g_ptr_array_sized_new (stream->subpackets_needed);
2196 GST_LOG ("Got subpacket %u/%u, len=%" G_GSIZE_FORMAT ", key=%d",
2197 stream->subpackets->len + 1, stream->subpackets_needed,
2198 gst_buffer_get_size (buf), keyframe);
2200 if (keyframe && stream->subpackets->len > 0) {
2201 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2204 g_ptr_array_add (stream->subpackets, buf);
2206 if (stream->subpackets->len < stream->subpackets_needed)
2209 g_assert (stream->subpackets->len >= 1);
2211 switch (stream->fourcc) {
2212 case GST_RM_AUD_DNET:
2213 ret = gst_rmdemux_descramble_dnet_audio (rmdemux, stream);
2215 case GST_RM_AUD_COOK:
2216 case GST_RM_AUD_ATRC:
2217 ret = gst_rmdemux_descramble_audio (rmdemux, stream);
2219 case GST_RM_AUD_RAAC:
2220 case GST_RM_AUD_RACP:
2221 ret = gst_rmdemux_descramble_mp4a_audio (rmdemux, stream);
2223 case GST_RM_AUD_SIPR:
2224 ret = gst_rmdemux_descramble_sipr_audio (rmdemux, stream);
2227 ret = GST_FLOW_ERROR;
2228 g_assert_not_reached ();
2234 #define PARSE_NUMBER(data, size, number, label) \
2238 number = GST_READ_UINT16_BE (data); \
2239 if (!(number & 0xc000)) { \
2242 number = GST_READ_UINT32_BE (data); \
2252 static GstFlowReturn
2253 gst_rmdemux_parse_video_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2254 GstBuffer * in, guint offset, guint16 version,
2255 GstClockTime timestamp, gboolean key)
2262 gst_buffer_map (in, &map, GST_MAP_READ);
2264 if (map.size < offset)
2265 goto not_enough_data;
2267 data = map.data + offset;
2268 size = map.size - offset;
2270 /* if size <= 2, we want this method to return the same GstFlowReturn as it
2271 * was previously for that given stream. */
2272 ret = GST_PAD_LAST_FLOW_RETURN (stream->pad);
2278 guint pkg_subseq = 0, pkg_seqnum = G_MAXUINT;
2279 guint fragment_size;
2280 GstBuffer *fragment;
2282 pkg_header = *data++;
2286 * bit 7: 1=last block in block chain
2287 * bit 6: 1=short header (only one block?)
2289 if ((pkg_header & 0xc0) == 0x40) {
2290 /* skip unknown byte */
2296 if ((pkg_header & 0x40) == 0) {
2297 pkg_subseq = (*data++) & 0x7f;
2304 PARSE_NUMBER (data, size, pkg_length, not_enough_data);
2307 PARSE_NUMBER (data, size, pkg_offset, not_enough_data);
2311 goto not_enough_data;
2313 pkg_seqnum = *data++;
2317 GST_DEBUG_OBJECT (rmdemux,
2318 "seq %d, subseq %d, offset %d, length %d, size %" G_GSIZE_FORMAT
2319 ", header %02x", pkg_seqnum, pkg_subseq, pkg_offset, pkg_length, size,
2322 /* calc size of fragment */
2323 if ((pkg_header & 0xc0) == 0x80) {
2324 fragment_size = pkg_offset;
2326 if ((pkg_header & 0xc0) == 0)
2327 fragment_size = size;
2329 fragment_size = pkg_length;
2331 GST_DEBUG_OBJECT (rmdemux, "fragment size %d", fragment_size);
2333 if (map.size < (data - map.data) + fragment_size)
2334 goto not_enough_data;
2336 /* get the fragment */
2338 gst_buffer_copy_region (in, GST_BUFFER_COPY_ALL, data - map.data,
2341 if (pkg_subseq == 1) {
2342 GST_DEBUG_OBJECT (rmdemux, "start new fragment");
2343 gst_adapter_clear (stream->adapter);
2344 stream->frag_current = 0;
2345 stream->frag_count = 0;
2346 stream->frag_length = pkg_length;
2347 } else if (pkg_subseq == 0) {
2348 GST_DEBUG_OBJECT (rmdemux, "non fragmented packet");
2349 stream->frag_current = 0;
2350 stream->frag_count = 0;
2351 stream->frag_length = fragment_size;
2354 /* put fragment in adapter */
2355 gst_adapter_push (stream->adapter, fragment);
2356 stream->frag_offset[stream->frag_count] = stream->frag_current;
2357 stream->frag_current += fragment_size;
2358 stream->frag_count++;
2360 if (stream->frag_count > MAX_FRAGS)
2361 goto too_many_fragments;
2363 GST_DEBUG_OBJECT (rmdemux, "stored fragment in adapter %d/%d",
2364 stream->frag_current, stream->frag_length);
2366 /* flush fragment when complete */
2367 if (stream->frag_current >= stream->frag_length) {
2374 /* calculate header size, which is:
2375 * 1 byte for the number of fragments - 1
2376 * for each fragment:
2377 * 4 bytes 0x00000001 little endian
2378 * 4 bytes fragment offset
2380 * This is also the matroska header for realvideo, the decoder needs the
2381 * fragment offsets, both in ffmpeg and real .so, so we just give it that
2382 * in front of the data.
2384 header_size = 1 + (8 * (stream->frag_count));
2386 GST_DEBUG_OBJECT (rmdemux,
2387 "fragmented completed. count %d, header_size %u", stream->frag_count,
2390 avail = gst_adapter_available (stream->adapter);
2392 out = gst_buffer_new_and_alloc (header_size + avail);
2393 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
2394 outdata = outmap.data;
2397 *outdata++ = stream->frag_count - 1;
2398 for (i = 0; i < stream->frag_count; i++) {
2399 GST_WRITE_UINT32_LE (outdata, 0x00000001);
2401 GST_WRITE_UINT32_LE (outdata, stream->frag_offset[i]);
2405 /* copy packet data after the header now */
2406 gst_adapter_copy (stream->adapter, outdata, 0, avail);
2407 gst_adapter_flush (stream->adapter, avail);
2409 stream->frag_current = 0;
2410 stream->frag_count = 0;
2411 stream->frag_length = 0;
2413 if (timestamp != -1) {
2414 if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts)
2415 timestamp -= rmdemux->first_ts;
2419 if (rmdemux->base_ts != -1)
2420 timestamp += rmdemux->base_ts;
2422 gst_buffer_unmap (out, &outmap);
2425 GST_BUFFER_DTS (out) = timestamp;
2426 GST_BUFFER_PTS (out) = GST_CLOCK_TIME_NONE;
2428 GST_LOG_OBJECT (rmdemux, "pushing timestamp %" GST_TIME_FORMAT,
2429 GST_TIME_ARGS (timestamp));
2431 if (stream->discont) {
2432 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT);
2433 stream->discont = FALSE;
2437 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
2440 ret = gst_pad_push (stream->pad, out);
2441 ret = gst_flow_combiner_update_flow (rmdemux->flowcombiner, ret);
2442 if (ret != GST_FLOW_OK)
2445 timestamp = GST_CLOCK_TIME_NONE;
2447 data += fragment_size;
2448 size -= fragment_size;
2450 GST_DEBUG_OBJECT (rmdemux, "%" G_GSIZE_FORMAT " bytes left", size);
2453 gst_buffer_unmap (in, &map);
2454 gst_buffer_unref (in);
2461 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2468 GST_ELEMENT_ERROR (rmdemux, STREAM, DECODE,
2469 ("Got more fragments (%u) than can be handled (%u)",
2470 stream->frag_count, MAX_FRAGS), (NULL));
2471 ret = GST_FLOW_ERROR;
2476 static GstFlowReturn
2477 gst_rmdemux_parse_audio_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2478 GstBuffer * in, guint offset, guint16 version,
2479 GstClockTime timestamp, gboolean key)
2484 if (gst_buffer_get_size (in) < offset)
2485 goto not_enough_data;
2487 buffer = gst_buffer_copy_region (in, GST_BUFFER_COPY_MEMORY, offset, -1);
2489 if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts)
2490 timestamp -= rmdemux->first_ts;
2494 if (rmdemux->base_ts != -1)
2495 timestamp += rmdemux->base_ts;
2497 GST_BUFFER_PTS (buffer) = timestamp;
2498 GST_BUFFER_DTS (buffer) = timestamp;
2500 if (stream->needs_descrambling) {
2501 GST_LOG_OBJECT (rmdemux, "descramble timestamp %" GST_TIME_FORMAT,
2502 GST_TIME_ARGS (timestamp));
2503 ret = gst_rmdemux_handle_scrambled_packet (rmdemux, stream, buffer, key);
2505 GST_LOG_OBJECT (rmdemux,
2506 "Pushing buffer of size %" G_GSIZE_FORMAT ", timestamp %"
2507 GST_TIME_FORMAT "to pad %s", gst_buffer_get_size (buffer),
2508 GST_TIME_ARGS (timestamp), GST_PAD_NAME (stream->pad));
2510 if (stream->discont) {
2511 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2512 stream->discont = FALSE;
2514 ret = gst_pad_push (stream->pad, buffer);
2518 gst_buffer_unref (in);
2525 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2532 static GstFlowReturn
2533 gst_rmdemux_parse_packet (GstRMDemux * rmdemux, GstBuffer * in, guint16 version)
2536 GstRMDemuxStream *stream;
2538 GstFlowReturn cret, ret;
2539 GstClockTime timestamp;
2546 gst_buffer_map (in, &map, GST_MAP_READ);
2550 if (size < 4 + 6 + 1 + 2)
2551 goto not_enough_data;
2554 id = RMDEMUX_GUINT16_GET (data);
2556 stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
2557 if (!stream || !stream->pad)
2558 goto unknown_stream;
2560 /* timestamp in Msec */
2561 ts = RMDEMUX_GUINT32_GET (data + 2);
2562 timestamp = ts * GST_MSECOND;
2564 rmdemux->segment.position = timestamp;
2566 GST_LOG_OBJECT (rmdemux, "Parsing a packet for stream=%d, timestamp=%"
2567 GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT ", version=%d, ts=%u", id,
2568 GST_TIME_ARGS (timestamp), size, version, ts);
2570 if (rmdemux->first_ts == GST_CLOCK_TIME_NONE) {
2571 GST_DEBUG_OBJECT (rmdemux, "First timestamp: %" GST_TIME_FORMAT,
2572 GST_TIME_ARGS (timestamp));
2573 rmdemux->first_ts = timestamp;
2576 /* skip stream_id and timestamp */
2581 flags = GST_READ_UINT8 (data + 1);
2586 /* version 1 has an extra byte */
2589 goto not_enough_data;
2594 offset = data - map.data;
2595 gst_buffer_unmap (in, &map);
2597 key = (flags & 0x02) != 0;
2598 GST_DEBUG_OBJECT (rmdemux, "flags %d, Keyframe %d", flags, key);
2600 if (rmdemux->need_newsegment) {
2603 event = gst_event_new_segment (&rmdemux->segment);
2605 GST_DEBUG_OBJECT (rmdemux, "sending NEWSEGMENT event, segment.start= %"
2606 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start));
2608 gst_rmdemux_send_event (rmdemux, event);
2609 rmdemux->need_newsegment = FALSE;
2611 if (rmdemux->pending_tags != NULL) {
2612 gst_rmdemux_send_event (rmdemux,
2613 gst_event_new_tag (rmdemux->pending_tags));
2614 rmdemux->pending_tags = NULL;
2618 if (stream->pending_tags != NULL) {
2619 GST_LOG_OBJECT (stream->pad, "tags %" GST_PTR_FORMAT, stream->pending_tags);
2620 gst_pad_push_event (stream->pad, gst_event_new_tag (stream->pending_tags));
2621 stream->pending_tags = NULL;
2624 if ((rmdemux->offset + size) <= stream->seek_offset) {
2625 GST_DEBUG_OBJECT (rmdemux,
2626 "Stream %d is skipping: seek_offset=%d, offset=%d, size=%"
2627 G_GSIZE_FORMAT, stream->id, stream->seek_offset, rmdemux->offset, size);
2629 gst_buffer_unref (in);
2633 /* do special headers */
2634 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
2636 gst_rmdemux_parse_video_packet (rmdemux, stream, in, offset,
2637 version, timestamp, key);
2638 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
2640 gst_rmdemux_parse_audio_packet (rmdemux, stream, in, offset,
2641 version, timestamp, key);
2643 gst_buffer_unref (in);
2647 cret = gst_flow_combiner_update_pad_flow (rmdemux->flowcombiner, stream->pad,
2656 GST_WARNING_OBJECT (rmdemux, "No stream for stream id %d in parsing "
2658 gst_buffer_unmap (in, &map);
2659 gst_buffer_unref (in);
2666 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2668 gst_buffer_unmap (in, &map);
2669 gst_buffer_unref (in);