Debian Codegraph @

gst/realmedia/rmdemux.c

raw
1 /* GStreamer RealMedia demuxer
2 * Copyright (C) <1999> Erik Walthinsen <[email protected]>
3 * Copyright (C) <2003> David A. Schleef <[email protected]>
4 * Copyright (C) <2004> Stephane Loeuillet <[email protected]>
5 * Copyright (C) <2005> Owen Fraser-Green <[email protected]>
6 * Copyright (C) <2005> Michael Smith <fluendo.com>
7 * Copyright (C) <2006> Wim Taymans <[email protected]>
8 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
9 * Copyright (C) <2007> Wim Taymans <[email protected]>
10 *
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.
15 *
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.
20 *
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.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "rmdemux.h"
32 #include "rmutils.h"
33
34 #include <string.h>
35 #include <ctype.h>
36
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
41 #define DATA_SIZE 8
42
43 #define MAX_FRAGS 256
44
45 static const guint8 sipr_subpk_size[4] = { 29, 19, 37, 20 };
46
47 typedef struct _GstRMDemuxIndex GstRMDemuxIndex;
48
49 struct _GstRMDemuxStream
50 {
51 guint32 subtype;
52 guint32 fourcc;
53 guint32 subformat;
54 guint32 format;
55
56 int id;
57 GstPad *pad;
58 gboolean discont;
59 int timescale;
60
61 int sample_index;
62 GstRMDemuxIndex *index;
63 int index_length;
64 gint framerate_numerator;
65 gint framerate_denominator;
66 guint32 seek_offset;
67
68 guint16 width;
69 guint16 height;
70 guint16 flavor;
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 */
76 guint16 version;
77 gsize extra_data_size; /* codec_data_length */
78 guint8 *extra_data; /* extras */
79 gsize extra_data_offset; /* offset into extra_data */
80 guint32 bitrate;
81
82 gboolean needs_descrambling;
83 guint subpackets_needed; /* subpackets needed for descrambling */
84 GPtrArray *subpackets; /* array containing subpacket GstBuffers */
85
86 /* Variables needed for fixing timestamps. */
87 GstClockTime next_ts, last_ts;
88 guint16 next_seq, last_seq;
89
90 gint frag_seqnum;
91 gint frag_subseq;
92 guint frag_length;
93 guint frag_current;
94 guint frag_count;
95 guint frag_offset[MAX_FRAGS];
96 GstAdapter *adapter;
97
98 GstTagList *pending_tags;
99 };
100
101 struct _GstRMDemuxIndex
102 {
103 guint32 offset;
104 GstClockTime timestamp;
105 };
106
107 static GstStaticPadTemplate gst_rmdemux_sink_template =
108 GST_STATIC_PAD_TEMPLATE ("sink",
109 GST_PAD_SINK,
110 GST_PAD_ALWAYS,
111 GST_STATIC_CAPS ("application/vnd.rn-realmedia")
112 );
113
114 static GstStaticPadTemplate gst_rmdemux_videosrc_template =
115 GST_STATIC_PAD_TEMPLATE ("video_%u",
116 GST_PAD_SRC,
117 GST_PAD_SOMETIMES,
118 GST_STATIC_CAPS_ANY);
119
120 static GstStaticPadTemplate gst_rmdemux_audiosrc_template =
121 GST_STATIC_PAD_TEMPLATE ("audio_%u",
122 GST_PAD_SRC,
123 GST_PAD_SOMETIMES,
124 GST_STATIC_CAPS_ANY);
125
126 GST_DEBUG_CATEGORY_STATIC (rmdemux_debug);
127 #define GST_CAT_DEFAULT rmdemux_debug
128
129 static GstElementClass *parent_class = NULL;
130
131 static void gst_rmdemux_class_init (GstRMDemuxClass * klass);
132 static void gst_rmdemux_base_init (GstRMDemuxClass * klass);
133 static void gst_rmdemux_init (GstRMDemux * rmdemux);
134 static void gst_rmdemux_finalize (GObject * object);
135 static GstStateChangeReturn gst_rmdemux_change_state (GstElement * element,
136 GstStateChange transition);
137 static GstFlowReturn gst_rmdemux_chain (GstPad * pad, GstObject * parent,
138 GstBuffer * buffer);
139 static void gst_rmdemux_loop (GstPad * pad);
140 static gboolean gst_rmdemux_sink_activate (GstPad * sinkpad,
141 GstObject * parent);
142 static gboolean gst_rmdemux_sink_activate_mode (GstPad * sinkpad,
143 GstObject * parent, GstPadMode mode, gboolean active);
144 static gboolean gst_rmdemux_sink_event (GstPad * pad, GstObject * parent,
145 GstEvent * event);
146 static gboolean gst_rmdemux_src_event (GstPad * pad, GstObject * parent,
147 GstEvent * event);
148 static void gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event);
149 static gboolean gst_rmdemux_src_query (GstPad * pad, GstObject * parent,
150 GstQuery * query);
151 static gboolean gst_rmdemux_perform_seek (GstRMDemux * rmdemux,
152 GstEvent * event);
153
154 static gboolean gst_rmdemux_parse__rmf (GstRMDemux * rmdemux,
155 const guint8 * data, gsize length);
156 static gboolean gst_rmdemux_parse_prop (GstRMDemux * rmdemux,
157 const guint8 * data, gsize length);
158 static gboolean gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux,
159 const guint8 * data, gsize length);
160 static gboolean gst_rmdemux_parse_indx (GstRMDemux * rmdemux,
161 const guint8 * data, gsize length, gsize * index_length);
162 static gboolean gst_rmdemux_parse_data (GstRMDemux * rmdemux,
163 const guint8 * data, gsize length);
164 static gboolean gst_rmdemux_parse_cont (GstRMDemux * rmdemux,
165 const guint8 * data, gsize length);
166 static GstFlowReturn gst_rmdemux_parse_packet (GstRMDemux * rmdemux,
167 GstBuffer * in, guint16 version);
168 static gboolean gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux,
169 const guint8 * data, gsize length);
170 static void gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
171 GstRMDemuxStream * stream);
172 static GstRMDemuxStream *gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux,
173 int id);
174
175 static GType
176 gst_rmdemux_get_type (void)
177 {
178 static GType rmdemux_type = 0;
179
180 if (!rmdemux_type) {
181 static const GTypeInfo rmdemux_info = {
182 sizeof (GstRMDemuxClass),
183 (GBaseInitFunc) gst_rmdemux_base_init, NULL,
184 (GClassInitFunc) gst_rmdemux_class_init,
185 NULL, NULL, sizeof (GstRMDemux), 0,
186 (GInstanceInitFunc) gst_rmdemux_init,
187 };
188
189 rmdemux_type =
190 g_type_register_static (GST_TYPE_ELEMENT, "GstRMDemux", &rmdemux_info,
191 0);
192 }
193 return rmdemux_type;
194 }
195
196 GST_ELEMENT_REGISTER_DEFINE (rmdemux, "rmdemux",
197 GST_RANK_PRIMARY, GST_TYPE_RMDEMUX);
198
199 static void
200 gst_rmdemux_base_init (GstRMDemuxClass * klass)
201 {
202 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
203
204 gst_element_class_add_static_pad_template (element_class,
205 &gst_rmdemux_sink_template);
206 gst_element_class_add_static_pad_template (element_class,
207 &gst_rmdemux_videosrc_template);
208 gst_element_class_add_static_pad_template (element_class,
209 &gst_rmdemux_audiosrc_template);
210 gst_element_class_set_static_metadata (element_class, "RealMedia Demuxer",
211 "Codec/Demuxer",
212 "Demultiplex a RealMedia file into audio and video streams",
213 "David Schleef <[email protected]>");
214 }
215
216 static void
217 gst_rmdemux_class_init (GstRMDemuxClass * klass)
218 {
219 GObjectClass *gobject_class;
220 GstElementClass *gstelement_class;
221
222 gobject_class = (GObjectClass *) klass;
223 gstelement_class = (GstElementClass *) klass;
224
225 parent_class = g_type_class_peek_parent (klass);
226
227 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rmdemux_change_state);
228
229 GST_DEBUG_CATEGORY_INIT (rmdemux_debug, "rmdemux",
230 0, "Demuxer for Realmedia streams");
231
232 gobject_class->finalize = gst_rmdemux_finalize;
233 }
234
235 static void
236 gst_rmdemux_finalize (GObject * object)
237 {
238 GstRMDemux *rmdemux = GST_RMDEMUX (object);
239
240 if (rmdemux->adapter) {
241 g_object_unref (rmdemux->adapter);
242 rmdemux->adapter = NULL;
243 }
244 if (rmdemux->flowcombiner) {
245 gst_flow_combiner_free (rmdemux->flowcombiner);
246 rmdemux->flowcombiner = NULL;
247 }
248
249 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
250 }
251
252 static void
253 gst_rmdemux_init (GstRMDemux * rmdemux)
254 {
255 rmdemux->sinkpad =
256 gst_pad_new_from_static_template (&gst_rmdemux_sink_template, "sink");
257 gst_pad_set_event_function (rmdemux->sinkpad,
258 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_event));
259 gst_pad_set_chain_function (rmdemux->sinkpad,
260 GST_DEBUG_FUNCPTR (gst_rmdemux_chain));
261 gst_pad_set_activate_function (rmdemux->sinkpad,
262 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate));
263 gst_pad_set_activatemode_function (rmdemux->sinkpad,
264 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate_mode));
265
266 gst_element_add_pad (GST_ELEMENT (rmdemux), rmdemux->sinkpad);
267
268 rmdemux->adapter = gst_adapter_new ();
269 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
270 rmdemux->base_ts = GST_CLOCK_TIME_NONE;
271 rmdemux->need_newsegment = TRUE;
272 rmdemux->have_group_id = FALSE;
273 rmdemux->group_id = G_MAXUINT;
274 rmdemux->flowcombiner = gst_flow_combiner_new ();
275 rmdemux->seek_seqnum = GST_SEQNUM_INVALID;
276
277 gst_rm_utils_run_tests ();
278 }
279
280 static gboolean
281 gst_rmdemux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
282 {
283 gboolean ret;
284
285 switch (GST_EVENT_TYPE (event)) {
286 case GST_EVENT_SEGMENT:
287 gst_event_unref (event);
288 ret = TRUE;
289 break;
290 default:
291 ret = gst_pad_event_default (pad, parent, event);
292 break;
293 }
294 return ret;
295 }
296
297 static gboolean
298 gst_rmdemux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
299 {
300 gboolean ret = TRUE;
301
302 GstRMDemux *rmdemux = GST_RMDEMUX (parent);
303
304 GST_LOG_OBJECT (rmdemux, "handling src event");
305
306 switch (GST_EVENT_TYPE (event)) {
307 case GST_EVENT_SEEK:
308 {
309 gboolean running;
310 guint32 seqnum;
311
312 GST_LOG_OBJECT (rmdemux, "Event on src: SEEK");
313
314 seqnum = gst_event_get_seqnum (event);
315 if (seqnum == rmdemux->seek_seqnum) {
316 GST_LOG_OBJECT (pad,
317 "Drop duplicated SEEK event seqnum %" G_GUINT32_FORMAT, seqnum);
318 gst_event_unref (event);
319 break;
320 }
321
322 /* can't seek if we are not seekable, FIXME could pass the
323 * seek query upstream after converting it to bytes using
324 * the average bitrate of the stream. */
325 if (!rmdemux->seekable) {
326 ret = FALSE;
327 GST_DEBUG ("seek on non seekable stream");
328 goto done_unref;
329 }
330
331 GST_OBJECT_LOCK (rmdemux);
332 /* check if we can do the seek now */
333 running = rmdemux->running;
334 GST_OBJECT_UNLOCK (rmdemux);
335
336 /* now do the seek */
337 if (running) {
338 ret = gst_rmdemux_perform_seek (rmdemux, event);
339 } else
340 ret = TRUE;
341
342 gst_event_unref (event);
343 break;
344 }
345 default:
346 GST_LOG_OBJECT (rmdemux, "Event on src: type=%d", GST_EVENT_TYPE (event));
347 ret = gst_pad_event_default (pad, parent, event);
348 break;
349 }
350
351 return ret;
352
353 done_unref:
354 GST_DEBUG ("error handling event");
355 gst_event_unref (event);
356 return ret;
357 }
358
359 /* Validate that this looks like a reasonable point to seek to */
360 static gboolean
361 gst_rmdemux_validate_offset (GstRMDemux * rmdemux)
362 {
363 GstBuffer *buffer;
364 GstFlowReturn flowret;
365 guint16 version, length;
366 gboolean ret = TRUE;
367 GstMapInfo map;
368
369 buffer = NULL;
370 flowret = gst_pad_pull_range (rmdemux->sinkpad, rmdemux->offset, 4, &buffer);
371
372 if (flowret != GST_FLOW_OK) {
373 GST_DEBUG_OBJECT (rmdemux, "Failed to pull data at offset %d",
374 rmdemux->offset);
375 return FALSE;
376 }
377 if (gst_buffer_get_size (buffer) < 4) {
378 gst_buffer_unref (buffer);
379 GST_DEBUG_OBJECT (rmdemux, "Short read at offset %d", rmdemux->offset);
380 return FALSE;
381 }
382 /* TODO: Can we also be seeking to a 'DATA' chunk header? Check this.
383 * Also, for the case we currently handle, can we check any more? It's pretty
384 * sucky to not be validating a little more heavily than this... */
385 /* This should now be the start of a data packet header. That begins with
386 * a 2-byte 'version' field, which has to be 0 or 1, then a length. I'm not
387 * certain what values are valid for length, but it must always be at least
388 * 4 bytes, and we can check that it won't take us past our known total size
389 */
390
391 gst_buffer_map (buffer, &map, GST_MAP_READ);
392 version = RMDEMUX_GUINT16_GET (map.data);
393 if (version != 0 && version != 1) {
394 GST_DEBUG_OBJECT (rmdemux, "Expected version 0 or 1, got %d",
395 (int) version);
396 ret = FALSE;
397 }
398
399 length = RMDEMUX_GUINT16_GET (map.data + 2);
400 /* TODO: Also check against total stream length */
401 if (length < 4) {
402 GST_DEBUG_OBJECT (rmdemux, "Expected length >= 4, got %d", (int) length);
403 ret = FALSE;
404 }
405 gst_buffer_unmap (buffer, &map);
406
407 if (ret) {
408 rmdemux->offset += 4;
409 gst_adapter_clear (rmdemux->adapter);
410 gst_adapter_push (rmdemux->adapter, buffer);
411 } else {
412 GST_WARNING_OBJECT (rmdemux, "Failed to validate seek offset at %d",
413 rmdemux->offset);
414 gst_buffer_unref (buffer);
415 }
416
417 return ret;
418 }
419
420 static gboolean
421 find_seek_offset_bytes (GstRMDemux * rmdemux, guint target)
422 {
423 int i;
424 GSList *cur;
425 gboolean ret = FALSE;
426
427 for (cur = rmdemux->streams; cur; cur = cur->next) {
428 GstRMDemuxStream *stream = cur->data;
429
430 /* Search backwards through this stream's index until we find the first
431 * timestamp before our target time */
432 for (i = stream->index_length - 1; i >= 0; i--) {
433 if (stream->index[i].offset <= target) {
434 /* Set the seek_offset for the stream so we don't bother parsing it
435 * until we've passed that point */
436 stream->seek_offset = stream->index[i].offset;
437 rmdemux->offset = stream->index[i].offset;
438 ret = TRUE;
439 break;
440 }
441 }
442 }
443 return ret;
444 }
445
446 static gboolean
447 find_seek_offset_time (GstRMDemux * rmdemux, GstClockTime time)
448 {
449 int i, n_stream;
450 gboolean ret = FALSE;
451 GSList *cur;
452 GstClockTime earliest = GST_CLOCK_TIME_NONE;
453
454 n_stream = 0;
455 for (cur = rmdemux->streams; cur; cur = cur->next, n_stream++) {
456 GstRMDemuxStream *stream = cur->data;
457
458 /* Search backwards through this stream's index until we find the first
459 * timestamp before our target time */
460 for (i = stream->index_length - 1; i >= 0; i--) {
461 if (stream->index[i].timestamp <= time) {
462 /* Set the seek_offset for the stream so we don't bother parsing it
463 * until we've passed that point */
464 stream->seek_offset = stream->index[i].offset;
465
466 /* If it's also the earliest timestamp we've seen of all streams, then
467 * that's our target!
468 */
469 if (earliest == GST_CLOCK_TIME_NONE ||
470 stream->index[i].timestamp < earliest) {
471 earliest = stream->index[i].timestamp;
472 rmdemux->offset = stream->index[i].offset;
473 GST_DEBUG_OBJECT (rmdemux,
474 "We're looking for %" GST_TIME_FORMAT
475 " and we found that stream %d has the latest index at %"
476 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start), n_stream,
477 GST_TIME_ARGS (earliest));
478 }
479
480 ret = TRUE;
481
482 break;
483 }
484 }
485 stream->discont = TRUE;
486 }
487 return ret;
488 }
489
490 static gboolean
491 gst_rmdemux_perform_seek (GstRMDemux * rmdemux, GstEvent * event)
492 {
493 gboolean validated;
494 gboolean ret = TRUE;
495 gboolean flush;
496 GstFormat format;
497 gdouble rate;
498 GstSeekFlags flags;
499 GstSeekType cur_type, stop_type;
500 gint64 cur, stop;
501 gboolean update;
502 guint32 seqnum = GST_SEQNUM_INVALID;
503 GstEvent *fl_event;
504
505 if (event) {
506 GST_DEBUG_OBJECT (rmdemux, "seek with event");
507
508 seqnum = gst_event_get_seqnum (event);
509 gst_event_parse_seek (event, &rate, &format, &flags,
510 &cur_type, &cur, &stop_type, &stop);
511
512 /* we can only seek on time */
513 if (format != GST_FORMAT_TIME) {
514 GST_DEBUG_OBJECT (rmdemux, "can only seek on TIME");
515 goto error;
516 }
517 /* cannot yet do backwards playback */
518 if (rate <= 0.0) {
519 GST_DEBUG_OBJECT (rmdemux, "can only seek with positive rate, not %lf",
520 rate);
521 goto error;
522 }
523 } else {
524 GST_DEBUG_OBJECT (rmdemux, "seek without event");
525
526 flags = 0;
527 rate = 1.0;
528 }
529
530 GST_DEBUG_OBJECT (rmdemux, "seek, rate %g", rate);
531
532 flush = flags & GST_SEEK_FLAG_FLUSH;
533
534 /* first step is to unlock the streaming thread if it is
535 * blocked in a chain call, we do this by starting the flush. */
536 if (flush) {
537 fl_event = gst_event_new_flush_start ();
538 if (seqnum != GST_SEQNUM_INVALID)
539 gst_event_set_seqnum (fl_event, seqnum);
540 gst_pad_push_event (rmdemux->sinkpad, fl_event);
541
542 fl_event = gst_event_new_flush_start ();
543 if (seqnum != GST_SEQNUM_INVALID)
544 gst_event_set_seqnum (fl_event, seqnum);
545 gst_rmdemux_send_event (rmdemux, fl_event);
546 } else {
547 gst_pad_pause_task (rmdemux->sinkpad);
548 }
549
550 GST_LOG_OBJECT (rmdemux, "Done starting flushes");
551
552 /* now grab the stream lock so that streaming cannot continue, for
553 * non flushing seeks when the element is in PAUSED this could block
554 * forever. */
555 GST_PAD_STREAM_LOCK (rmdemux->sinkpad);
556
557 GST_LOG_OBJECT (rmdemux, "Took streamlock");
558
559 if (event) {
560 if (!gst_segment_do_seek (&rmdemux->segment, rate, format, flags,
561 cur_type, cur, stop_type, stop, &update)) {
562 ret = FALSE;
563 goto done;
564 }
565
566 rmdemux->seek_seqnum = seqnum;
567 }
568
569 GST_DEBUG_OBJECT (rmdemux, "segment positions set to %" GST_TIME_FORMAT "-%"
570 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start),
571 GST_TIME_ARGS (rmdemux->segment.stop));
572
573 /* we need to stop flushing on the sinkpad as we're going to use it
574 * next. We can do this as we have the STREAM lock now. */
575 fl_event = gst_event_new_flush_stop (TRUE);
576 if (seqnum != GST_SEQNUM_INVALID)
577 gst_event_set_seqnum (fl_event, seqnum);
578 gst_pad_push_event (rmdemux->sinkpad, fl_event);
579
580 GST_LOG_OBJECT (rmdemux, "Pushed FLUSH_STOP event");
581
582 /* For each stream, find the first index offset equal to or before our seek
583 * target. Of these, find the smallest offset. That's where we seek to.
584 *
585 * Then we pull 4 bytes from that offset, and validate that we've seeked to a
586 * what looks like a plausible packet.
587 * If that fails, restart, with the seek target set to one less than the
588 * offset we just tried. If we run out of places to try, treat that as a fatal
589 * error.
590 */
591 if (!find_seek_offset_time (rmdemux, rmdemux->segment.position)) {
592 GST_LOG_OBJECT (rmdemux, "Failed to find seek offset by time");
593 ret = FALSE;
594 goto done;
595 }
596
597 GST_LOG_OBJECT (rmdemux, "Validating offset %u", rmdemux->offset);
598 validated = gst_rmdemux_validate_offset (rmdemux);
599 while (!validated) {
600 GST_INFO_OBJECT (rmdemux, "Failed to validate offset at %u",
601 rmdemux->offset);
602 if (!find_seek_offset_bytes (rmdemux, rmdemux->offset - 1)) {
603 ret = FALSE;
604 goto done;
605 }
606 validated = gst_rmdemux_validate_offset (rmdemux);
607 }
608
609 GST_LOG_OBJECT (rmdemux, "Found final offset. Excellent!");
610
611 /* now we have a new position, prepare for streaming again */
612 {
613 /* Reset the demuxer state */
614 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
615
616 if (flush) {
617 fl_event = gst_event_new_flush_stop (TRUE);
618 if (seqnum != GST_SEQNUM_INVALID)
619 gst_event_set_seqnum (fl_event, seqnum);
620 gst_rmdemux_send_event (rmdemux, fl_event);
621 }
622
623 /* must send newsegment event from streaming thread, so just set flag */
624 rmdemux->need_newsegment = TRUE;
625
626 /* notify start of new segment */
627 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
628 GstMessage *msg =
629 gst_message_new_segment_start (GST_OBJECT_CAST (rmdemux),
630 GST_FORMAT_TIME, rmdemux->segment.position);
631 if (seqnum != GST_SEQNUM_INVALID)
632 gst_message_set_seqnum (msg, seqnum);
633 gst_element_post_message (GST_ELEMENT_CAST (rmdemux), msg);
634 }
635
636 /* restart our task since it might have been stopped when we did the
637 * flush. */
638 gst_pad_start_task (rmdemux->sinkpad, (GstTaskFunction) gst_rmdemux_loop,
639 rmdemux->sinkpad, NULL);
640 }
641
642 done:
643 /* streaming can continue now */
644 GST_PAD_STREAM_UNLOCK (rmdemux->sinkpad);
645
646 return ret;
647
648 error:
649 {
650 GST_DEBUG_OBJECT (rmdemux, "seek failed");
651 return FALSE;
652 }
653 }
654
655
656 static gboolean
657 gst_rmdemux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
658 {
659 gboolean res = FALSE;
660 GstRMDemux *rmdemux;
661
662 rmdemux = GST_RMDEMUX (parent);
663
664 switch (GST_QUERY_TYPE (query)) {
665 case GST_QUERY_POSITION:
666 GST_DEBUG_OBJECT (rmdemux, "Position query: no idea from demuxer!");
667 break;
668 case GST_QUERY_DURATION:{
669 GstFormat fmt;
670
671 gst_query_parse_duration (query, &fmt, NULL);
672 if (fmt == GST_FORMAT_TIME) {
673 GST_OBJECT_LOCK (rmdemux);
674 if (G_LIKELY (rmdemux->running)) {
675 gst_query_set_duration (query, GST_FORMAT_TIME, rmdemux->duration);
676 GST_DEBUG_OBJECT (rmdemux, "duration set to %" GST_TIME_FORMAT,
677 GST_TIME_ARGS (rmdemux->duration));
678 res = TRUE;
679 }
680 GST_OBJECT_UNLOCK (rmdemux);
681 }
682 break;
683 }
684 case GST_QUERY_SEEKING:{
685 GstFormat fmt;
686
687 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
688 if (fmt == GST_FORMAT_TIME) {
689 GST_OBJECT_LOCK (rmdemux);
690 if (G_LIKELY (rmdemux->running)) {
691 gst_query_set_seeking (query, GST_FORMAT_TIME, rmdemux->seekable,
692 0, rmdemux->duration);
693 res = TRUE;
694 }
695 GST_OBJECT_UNLOCK (rmdemux);
696 }
697 break;
698 }
699 case GST_QUERY_SEGMENT:
700 {
701 GstFormat format;
702 gint64 start, stop;
703
704 format = rmdemux->segment.format;
705
706 start =
707 gst_segment_to_stream_time (&rmdemux->segment, format,
708 rmdemux->segment.start);
709 if ((stop = rmdemux->segment.stop) == -1)
710 stop = rmdemux->segment.duration;
711 else
712 stop = gst_segment_to_stream_time (&rmdemux->segment, format, stop);
713
714 gst_query_set_segment (query, rmdemux->segment.rate, format, start, stop);
715 res = TRUE;
716 break;
717 }
718 default:
719 res = gst_pad_query_default (pad, parent, query);
720 break;
721 }
722
723 return res;
724 }
725
726 static void
727 gst_rmdemux_free_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
728 {
729 g_object_unref (stream->adapter);
730 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
731 if (stream->pending_tags)
732 gst_tag_list_unref (stream->pending_tags);
733 if (stream->subpackets)
734 g_ptr_array_free (stream->subpackets, TRUE);
735 g_free (stream->index);
736 g_free (stream->extra_data);
737 g_free (stream);
738 }
739
740 static void
741 gst_rmdemux_reset (GstRMDemux * rmdemux)
742 {
743 GSList *cur;
744
745 GST_OBJECT_LOCK (rmdemux);
746 rmdemux->running = FALSE;
747 GST_OBJECT_UNLOCK (rmdemux);
748
749 for (cur = rmdemux->streams; cur; cur = cur->next) {
750 GstRMDemuxStream *stream = cur->data;
751
752 gst_flow_combiner_remove_pad (rmdemux->flowcombiner, stream->pad);
753 gst_element_remove_pad (GST_ELEMENT (rmdemux), stream->pad);
754 gst_rmdemux_free_stream (rmdemux, stream);
755 }
756 g_slist_free (rmdemux->streams);
757 rmdemux->streams = NULL;
758 rmdemux->n_audio_streams = 0;
759 rmdemux->n_video_streams = 0;
760
761 if (rmdemux->pending_tags != NULL) {
762 gst_tag_list_unref (rmdemux->pending_tags);
763 rmdemux->pending_tags = NULL;
764 }
765
766 gst_adapter_clear (rmdemux->adapter);
767 rmdemux->state = RMDEMUX_STATE_HEADER;
768 rmdemux->have_pads = FALSE;
769
770 gst_segment_init (&rmdemux->segment, GST_FORMAT_UNDEFINED);
771 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
772 rmdemux->base_ts = GST_CLOCK_TIME_NONE;
773 rmdemux->need_newsegment = TRUE;
774
775 rmdemux->have_group_id = FALSE;
776 rmdemux->group_id = G_MAXUINT;
777
778 rmdemux->seek_seqnum = GST_SEQNUM_INVALID;
779 }
780
781 static GstStateChangeReturn
782 gst_rmdemux_change_state (GstElement * element, GstStateChange transition)
783 {
784 GstRMDemux *rmdemux = GST_RMDEMUX (element);
785 GstStateChangeReturn res;
786
787 switch (transition) {
788 case GST_STATE_CHANGE_NULL_TO_READY:
789 break;
790 case GST_STATE_CHANGE_READY_TO_PAUSED:
791 rmdemux->state = RMDEMUX_STATE_HEADER;
792 rmdemux->have_pads = FALSE;
793 gst_segment_init (&rmdemux->segment, GST_FORMAT_TIME);
794 rmdemux->running = FALSE;
795 break;
796 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
797 break;
798 default:
799 break;
800 }
801
802 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
803
804 switch (transition) {
805 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
806 break;
807 case GST_STATE_CHANGE_PAUSED_TO_READY:{
808 gst_rmdemux_reset (rmdemux);
809 break;
810 }
811 case GST_STATE_CHANGE_READY_TO_NULL:
812 break;
813 default:
814 break;
815 }
816
817 return res;
818 }
819
820 /* this function is called when the pad is activated and should start
821 * processing data.
822 *
823 * We check if we can do random access to decide if we work push or
824 * pull based.
825 */
826 static gboolean
827 gst_rmdemux_sink_activate (GstPad * sinkpad, GstObject * parent)
828 {
829 GstQuery *query;
830 gboolean pull_mode;
831
832 query = gst_query_new_scheduling ();
833
834 if (!gst_pad_peer_query (sinkpad, query)) {
835 gst_query_unref (query);
836 goto activate_push;
837 }
838
839 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
840 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
841 gst_query_unref (query);
842
843 if (!pull_mode)
844 goto activate_push;
845
846 GST_DEBUG_OBJECT (sinkpad, "activating pull");
847 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
848
849 activate_push:
850 {
851 GST_DEBUG_OBJECT (sinkpad, "activating push");
852 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
853 }
854 }
855
856 static gboolean
857 gst_rmdemux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
858 GstPadMode mode, gboolean active)
859 {
860 gboolean res;
861 GstRMDemux *demux;
862
863 demux = GST_RMDEMUX (parent);
864
865 switch (mode) {
866 case GST_PAD_MODE_PUSH:
867 demux->seekable = FALSE;
868 demux->running = active;
869 res = TRUE;
870 break;
871 case GST_PAD_MODE_PULL:
872 if (active) {
873 demux->seekable = TRUE;
874 demux->offset = 0;
875 demux->loop_state = RMDEMUX_LOOP_STATE_HEADER;
876 demux->data_offset = G_MAXUINT;
877 res =
878 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_rmdemux_loop,
879 sinkpad, NULL);
880 } else {
881 res = gst_pad_stop_task (sinkpad);
882 }
883 break;
884 default:
885 res = FALSE;
886 break;
887 }
888 return res;
889 }
890
891
892 /* random access mode - just pass over to our chain function */
893 static void
894 gst_rmdemux_loop (GstPad * pad)
895 {
896 GstRMDemux *rmdemux;
897 GstBuffer *buffer;
898 GstFlowReturn ret = GST_FLOW_OK;
899 guint size;
900
901 rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
902
903 GST_LOG_OBJECT (rmdemux, "loop with state=%d and offset=0x%x",
904 rmdemux->loop_state, rmdemux->offset);
905
906 switch (rmdemux->state) {
907 case RMDEMUX_STATE_HEADER:
908 size = HEADER_SIZE;
909 break;
910 case RMDEMUX_STATE_HEADER_DATA:
911 size = DATA_SIZE;
912 break;
913 case RMDEMUX_STATE_DATA_PACKET:
914 size = rmdemux->avg_packet_size;
915 break;
916 case RMDEMUX_STATE_EOS:
917 GST_LOG_OBJECT (rmdemux, "At EOS, pausing task");
918 ret = GST_FLOW_EOS;
919 goto need_pause;
920 default:
921 GST_LOG_OBJECT (rmdemux, "Default: requires %d bytes (state is %d)",
922 (int) rmdemux->size, rmdemux->state);
923 size = rmdemux->size;
924 }
925
926 buffer = NULL;
927 ret = gst_pad_pull_range (pad, rmdemux->offset, size, &buffer);
928 if (ret != GST_FLOW_OK) {
929 if (rmdemux->offset == rmdemux->index_offset) {
930 /* The index isn't available so forget about it */
931 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
932 rmdemux->offset = rmdemux->data_offset;
933 GST_OBJECT_LOCK (rmdemux);
934 rmdemux->running = TRUE;
935 rmdemux->seekable = FALSE;
936 GST_OBJECT_UNLOCK (rmdemux);
937 return;
938 } else {
939 GST_DEBUG_OBJECT (rmdemux, "Unable to pull %d bytes at offset 0x%08x "
940 "(pull_range returned flow %s, state is %d)", (gint) size,
941 rmdemux->offset, gst_flow_get_name (ret), GST_STATE (rmdemux));
942 goto need_pause;
943 }
944 }
945
946 size = gst_buffer_get_size (buffer);
947
948 /* Defer to the chain function */
949 ret = gst_rmdemux_chain (pad, GST_OBJECT_CAST (rmdemux), buffer);
950 if (ret != GST_FLOW_OK) {
951 GST_DEBUG_OBJECT (rmdemux, "Chain flow failed at offset 0x%08x",
952 rmdemux->offset);
953 goto need_pause;
954 }
955
956 rmdemux->offset += size;
957
958 switch (rmdemux->loop_state) {
959 case RMDEMUX_LOOP_STATE_HEADER:
960 if (rmdemux->offset >= rmdemux->data_offset) {
961 /* It's the end of the header */
962 rmdemux->loop_state = RMDEMUX_LOOP_STATE_INDEX;
963 rmdemux->offset = rmdemux->index_offset;
964 }
965 break;
966 case RMDEMUX_LOOP_STATE_INDEX:
967 if (rmdemux->state == RMDEMUX_STATE_HEADER) {
968 if (rmdemux->index_offset == 0) {
969 /* We've read the last index */
970 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
971 rmdemux->offset = rmdemux->data_offset;
972 GST_OBJECT_LOCK (rmdemux);
973 rmdemux->running = TRUE;
974 GST_OBJECT_UNLOCK (rmdemux);
975 } else {
976 /* Get the next index */
977 rmdemux->offset = rmdemux->index_offset;
978 }
979 }
980 break;
981 case RMDEMUX_LOOP_STATE_DATA:
982 break;
983 }
984
985 return;
986
987 /* ERRORS */
988 need_pause:
989 {
990 const gchar *reason = gst_flow_get_name (ret);
991
992 GST_LOG_OBJECT (rmdemux, "pausing task, reason %s", reason);
993 rmdemux->segment_running = FALSE;
994 gst_pad_pause_task (rmdemux->sinkpad);
995
996 if (ret == GST_FLOW_EOS) {
997 /* perform EOS logic */
998 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
999 gint64 stop;
1000
1001 /* for segment playback we need to post when (in stream time)
1002 * we stopped, this is either stop (when set) or the duration. */
1003 if ((stop = rmdemux->segment.stop) == -1)
1004 stop = rmdemux->segment.duration;
1005
1006 GST_LOG_OBJECT (rmdemux, "Sending segment done, at end of segment");
1007 gst_element_post_message (GST_ELEMENT (rmdemux),
1008 gst_message_new_segment_done (GST_OBJECT (rmdemux),
1009 GST_FORMAT_TIME, stop));
1010 gst_rmdemux_send_event (rmdemux,
1011 gst_event_new_segment_done (GST_FORMAT_TIME, stop));
1012 } else {
1013 /* normal playback, send EOS to all linked pads */
1014 GST_LOG_OBJECT (rmdemux, "Sending EOS, at end of stream");
1015 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1016 }
1017 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1018 GST_ELEMENT_FLOW_ERROR (rmdemux, ret);
1019 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1020 }
1021 return;
1022 }
1023 }
1024
1025 static gboolean
1026 gst_rmdemux_fourcc_isplausible (guint32 fourcc)
1027 {
1028 int i;
1029
1030 for (i = 0; i < 4; i++) {
1031 if (!isprint ((int) ((unsigned char *) (&fourcc))[i])) {
1032 return FALSE;
1033 }
1034 }
1035 return TRUE;
1036 }
1037
1038 static GstFlowReturn
1039 gst_rmdemux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1040 {
1041 GstFlowReturn ret = GST_FLOW_OK;
1042 const guint8 *data;
1043 guint16 version;
1044 guint avail;
1045
1046 GstRMDemux *rmdemux = GST_RMDEMUX (parent);
1047
1048 if (rmdemux->base_ts == -1) {
1049 if (GST_BUFFER_DTS_IS_VALID (buffer))
1050 rmdemux->base_ts = GST_BUFFER_DTS (buffer);
1051 else
1052 rmdemux->base_ts = GST_BUFFER_PTS (buffer);
1053
1054 GST_LOG_OBJECT (rmdemux, "base_ts %" GST_TIME_FORMAT,
1055 GST_TIME_ARGS (rmdemux->base_ts));
1056 }
1057
1058 gst_adapter_push (rmdemux->adapter, buffer);
1059
1060 GST_LOG_OBJECT (rmdemux, "Chaining buffer of size %" G_GSIZE_FORMAT,
1061 gst_buffer_get_size (buffer));
1062
1063 while (TRUE) {
1064 avail = gst_adapter_available (rmdemux->adapter);
1065
1066 GST_LOG_OBJECT (rmdemux, "looping in chain, avail %u", avail);
1067 switch (rmdemux->state) {
1068 case RMDEMUX_STATE_HEADER:
1069 {
1070 guint32 size;
1071 if (gst_adapter_available (rmdemux->adapter) < HEADER_SIZE)
1072 goto unlock;
1073
1074 data = gst_adapter_map (rmdemux->adapter, HEADER_SIZE);
1075
1076 rmdemux->object_id = RMDEMUX_FOURCC_GET (data + 0);
1077 size = RMDEMUX_GUINT32_GET (data + 4);
1078 if (size < HEADER_SIZE) {
1079 GST_WARNING_OBJECT (rmdemux, "Bogus looking header, too small size");
1080 gst_adapter_unmap (rmdemux->adapter);
1081 gst_adapter_flush (rmdemux->adapter, 4);
1082
1083 break;
1084 }
1085 rmdemux->size = size - HEADER_SIZE;
1086 rmdemux->object_version = RMDEMUX_GUINT16_GET (data + 8);
1087
1088 /* Sanity-check. We assume that the FOURCC is printable ASCII */
1089 if (!gst_rmdemux_fourcc_isplausible (rmdemux->object_id)) {
1090 /* Failed. Remain in HEADER state, try again... We flush only
1091 * the actual FOURCC, not the entire header, because we could
1092 * need to resync anywhere at all... really, this should never
1093 * happen. */
1094 GST_WARNING_OBJECT (rmdemux, "Bogus looking header, unprintable "
1095 "FOURCC");
1096 gst_adapter_unmap (rmdemux->adapter);
1097 gst_adapter_flush (rmdemux->adapter, 4);
1098
1099 break;
1100 }
1101
1102 GST_LOG_OBJECT (rmdemux, "header found with object_id=%"
1103 GST_FOURCC_FORMAT
1104 " size=%08" G_GSIZE_FORMAT " object_version=%d",
1105 GST_FOURCC_ARGS (rmdemux->object_id), rmdemux->size,
1106 rmdemux->object_version);
1107
1108 gst_adapter_unmap (rmdemux->adapter);
1109 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1110
1111 switch (rmdemux->object_id) {
1112 case GST_MAKE_FOURCC ('.', 'R', 'M', 'F'):
1113 rmdemux->state = RMDEMUX_STATE_HEADER_RMF;
1114 break;
1115 case GST_MAKE_FOURCC ('P', 'R', 'O', 'P'):
1116 rmdemux->state = RMDEMUX_STATE_HEADER_PROP;
1117 break;
1118 case GST_MAKE_FOURCC ('M', 'D', 'P', 'R'):
1119 rmdemux->state = RMDEMUX_STATE_HEADER_MDPR;
1120 break;
1121 case GST_MAKE_FOURCC ('I', 'N', 'D', 'X'):
1122 rmdemux->state = RMDEMUX_STATE_HEADER_INDX;
1123 break;
1124 case GST_MAKE_FOURCC ('D', 'A', 'T', 'A'):
1125 rmdemux->state = RMDEMUX_STATE_HEADER_DATA;
1126 break;
1127 case GST_MAKE_FOURCC ('C', 'O', 'N', 'T'):
1128 rmdemux->state = RMDEMUX_STATE_HEADER_CONT;
1129 break;
1130 default:
1131 rmdemux->state = RMDEMUX_STATE_HEADER_UNKNOWN;
1132 break;
1133 }
1134 break;
1135 }
1136 case RMDEMUX_STATE_HEADER_UNKNOWN:
1137 {
1138 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1139 goto unlock;
1140
1141 GST_WARNING_OBJECT (rmdemux, "Unknown object_id %" GST_FOURCC_FORMAT,
1142 GST_FOURCC_ARGS (rmdemux->object_id));
1143
1144 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1145 rmdemux->state = RMDEMUX_STATE_HEADER;
1146 break;
1147 }
1148 case RMDEMUX_STATE_HEADER_RMF:
1149 {
1150 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1151 goto unlock;
1152
1153 if ((rmdemux->object_version == 0) || (rmdemux->object_version == 1)) {
1154 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1155 if (!gst_rmdemux_parse__rmf (rmdemux, data, rmdemux->size))
1156 GST_WARNING_OBJECT (rmdemux, "Failed parsing _rmf");
1157 gst_adapter_unmap (rmdemux->adapter);
1158 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1159 } else {
1160 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1161 }
1162 rmdemux->state = RMDEMUX_STATE_HEADER;
1163 break;
1164 }
1165 case RMDEMUX_STATE_HEADER_PROP:
1166 {
1167 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1168 goto unlock;
1169
1170 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1171 if (!gst_rmdemux_parse_prop (rmdemux, data, rmdemux->size))
1172 GST_WARNING_OBJECT (rmdemux, "Failed parsing prop");
1173 gst_adapter_unmap (rmdemux->adapter);
1174 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1175
1176 rmdemux->state = RMDEMUX_STATE_HEADER;
1177 break;
1178 }
1179 case RMDEMUX_STATE_HEADER_MDPR:
1180 {
1181 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1182 goto unlock;
1183
1184 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1185 if (!gst_rmdemux_parse_mdpr (rmdemux, data, rmdemux->size))
1186 GST_WARNING_OBJECT (rmdemux, "Failed parsing mdpr");
1187 gst_adapter_unmap (rmdemux->adapter);
1188 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1189
1190 rmdemux->state = RMDEMUX_STATE_HEADER;
1191 break;
1192 }
1193 case RMDEMUX_STATE_HEADER_CONT:
1194 {
1195 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1196 goto unlock;
1197
1198 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1199 if (!gst_rmdemux_parse_cont (rmdemux, data, rmdemux->size))
1200 GST_WARNING_OBJECT (rmdemux, "Failed parsing cont");
1201 gst_adapter_unmap (rmdemux->adapter);
1202 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1203
1204 rmdemux->state = RMDEMUX_STATE_HEADER;
1205 break;
1206 }
1207 case RMDEMUX_STATE_HEADER_DATA:
1208 {
1209 gsize orig_size;
1210
1211 /* If we haven't already done so then signal there are no more pads */
1212 if (!rmdemux->have_pads) {
1213 GST_LOG_OBJECT (rmdemux, "no more pads");
1214 gst_element_no_more_pads (GST_ELEMENT (rmdemux));
1215 rmdemux->have_pads = TRUE;
1216 }
1217
1218 /* The actual header is only 8 bytes */
1219 orig_size = rmdemux->size;
1220 rmdemux->size = DATA_SIZE;
1221 GST_LOG_OBJECT (rmdemux, "data available %" G_GSIZE_FORMAT,
1222 gst_adapter_available (rmdemux->adapter));
1223 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1224 goto unlock;
1225
1226 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1227 if (!gst_rmdemux_parse_data (rmdemux, data, rmdemux->size)) {
1228 GST_WARNING_OBJECT (rmdemux, "Failed parsing header data");
1229 gst_adapter_unmap (rmdemux->adapter);
1230 /* skip over whole packet size */
1231 gst_adapter_flush (rmdemux->adapter, orig_size);
1232 rmdemux->state = RMDEMUX_STATE_HEADER;
1233 break;
1234 }
1235 gst_adapter_unmap (rmdemux->adapter);
1236 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1237
1238 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
1239 break;
1240 }
1241 case RMDEMUX_STATE_HEADER_INDX:
1242 {
1243 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1244 goto unlock;
1245
1246 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1247 if (!gst_rmdemux_parse_indx (rmdemux, data, rmdemux->size,
1248 &rmdemux->size)) {
1249 GST_WARNING_OBJECT (rmdemux, "Failed parsing header indx");
1250 gst_adapter_unmap (rmdemux->adapter);
1251 /* skip over whole packet size */
1252 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1253 rmdemux->state = RMDEMUX_STATE_HEADER;
1254 break;
1255 }
1256 /* Only flush the header */
1257 gst_adapter_unmap (rmdemux->adapter);
1258 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1259
1260 rmdemux->state = RMDEMUX_STATE_INDX_DATA;
1261 break;
1262 }
1263 case RMDEMUX_STATE_INDX_DATA:
1264 {
1265 /* There's not always an data to get... */
1266 if (rmdemux->size > 0) {
1267 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1268 goto unlock;
1269
1270 data = gst_adapter_map (rmdemux->adapter, rmdemux->size);
1271 if (!gst_rmdemux_parse_indx_data (rmdemux, data, rmdemux->size))
1272 GST_WARNING_OBJECT (rmdemux, "Failed parsing indx data");
1273 gst_adapter_unmap (rmdemux->adapter);
1274 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1275 }
1276
1277 rmdemux->state = RMDEMUX_STATE_HEADER;
1278 break;
1279 }
1280 case RMDEMUX_STATE_DATA_PACKET:
1281 {
1282 guint8 header[4];
1283
1284 if (gst_adapter_available (rmdemux->adapter) < 2)
1285 goto unlock;
1286
1287 gst_adapter_copy (rmdemux->adapter, header, 0, 2);
1288 version = RMDEMUX_GUINT16_GET (header);
1289 GST_LOG_OBJECT (rmdemux, "Data packet with version=%d", version);
1290
1291 if (version == 0 || version == 1) {
1292 guint16 length;
1293
1294 if (gst_adapter_available (rmdemux->adapter) < 4)
1295 goto unlock;
1296
1297 gst_adapter_copy (rmdemux->adapter, header, 0, 4);
1298
1299 length = RMDEMUX_GUINT16_GET (header + 2);
1300 GST_LOG_OBJECT (rmdemux, "Got length %d", length);
1301
1302 if (length < 4) {
1303 GST_LOG_OBJECT (rmdemux, "length too small, dropping");
1304 /* Invalid, just drop it */
1305 gst_adapter_flush (rmdemux->adapter, 4);
1306 } else {
1307 GstBuffer *buffer;
1308
1309 avail = gst_adapter_available (rmdemux->adapter);
1310 if (avail < length)
1311 goto unlock;
1312
1313 GST_LOG_OBJECT (rmdemux, "we have %u available and we needed %d",
1314 avail, length);
1315
1316 /* flush version and length */
1317 gst_adapter_flush (rmdemux->adapter, 4);
1318 length -= 4;
1319
1320 buffer = gst_adapter_take_buffer (rmdemux->adapter, length);
1321
1322 ret = gst_rmdemux_parse_packet (rmdemux, buffer, version);
1323 rmdemux->chunk_index++;
1324 }
1325
1326 if (rmdemux->chunk_index == rmdemux->n_chunks || length == 0)
1327 rmdemux->state = RMDEMUX_STATE_HEADER;
1328 } else {
1329 /* Stream done */
1330 gst_adapter_flush (rmdemux->adapter, 2);
1331
1332 if (rmdemux->data_offset == 0) {
1333 GST_LOG_OBJECT (rmdemux,
1334 "No further data, internal demux state EOS");
1335 rmdemux->state = RMDEMUX_STATE_EOS;
1336 } else
1337 rmdemux->state = RMDEMUX_STATE_HEADER;
1338 }
1339 break;
1340 }
1341 case RMDEMUX_STATE_EOS:
1342 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1343 goto unlock;
1344 default:
1345 GST_WARNING_OBJECT (rmdemux, "Unhandled state %d", rmdemux->state);
1346 goto unlock;
1347 }
1348 }
1349
1350 unlock:
1351 return ret;
1352 }
1353
1354 static GstRMDemuxStream *
1355 gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux, int id)
1356 {
1357 GSList *cur;
1358
1359 for (cur = rmdemux->streams; cur; cur = cur->next) {
1360 GstRMDemuxStream *stream = cur->data;
1361
1362 if (stream->id == id) {
1363 return stream;
1364 }
1365 }
1366
1367 return NULL;
1368 }
1369
1370 static void
1371 gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event)
1372 {
1373 GSList *cur;
1374
1375 for (cur = rmdemux->streams; cur; cur = cur->next) {
1376 GstRMDemuxStream *stream = cur->data;
1377
1378 GST_DEBUG_OBJECT (rmdemux, "Pushing %s event on pad %s",
1379 GST_EVENT_TYPE_NAME (event), GST_PAD_NAME (stream->pad));
1380
1381 switch (GST_EVENT_TYPE (event)) {
1382 case GST_EVENT_FLUSH_STOP:
1383 stream->last_ts = -1;
1384 stream->next_ts = -1;
1385 stream->last_seq = -1;
1386 stream->next_seq = -1;
1387 break;
1388 default:
1389 break;
1390 }
1391 gst_event_ref (event);
1392 gst_pad_push_event (stream->pad, event);
1393 }
1394 gst_event_unref (event);
1395 }
1396
1397 static void
1398 gst_rmdemux_add_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
1399 {
1400 GstCaps *stream_caps = NULL;
1401 const gchar *codec_tag = NULL;
1402 gchar *codec_name = NULL;
1403 gchar *stream_id;
1404 int version = 0;
1405
1406 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
1407 char *name = g_strdup_printf ("video_%u", rmdemux->n_video_streams);
1408
1409 stream->pad =
1410 gst_pad_new_from_static_template (&gst_rmdemux_videosrc_template, name);
1411 g_free (name);
1412
1413 codec_tag = GST_TAG_VIDEO_CODEC;
1414
1415 switch (stream->fourcc) {
1416 case GST_RM_VDO_RV10:
1417 version = 1;
1418 break;
1419 case GST_RM_VDO_RV20:
1420 version = 2;
1421 break;
1422 case GST_RM_VDO_RV30:
1423 version = 3;
1424 break;
1425 case GST_RM_VDO_RV40:
1426 version = 4;
1427 break;
1428 default:
1429 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1430 "fourcc", G_TYPE_UINT, stream->fourcc, NULL);
1431 GST_WARNING_OBJECT (rmdemux,
1432 "Unknown video FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1433 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1434 }
1435
1436 if (version) {
1437 stream_caps =
1438 gst_caps_new_simple ("video/x-pn-realvideo", "rmversion", G_TYPE_INT,
1439 (int) version,
1440 "format", G_TYPE_INT,
1441 (int) stream->format,
1442 "subformat", G_TYPE_INT, (int) stream->subformat, NULL);
1443 }
1444
1445 if (stream_caps) {
1446 gst_caps_set_simple (stream_caps,
1447 "width", G_TYPE_INT, stream->width,
1448 "height", G_TYPE_INT, stream->height,
1449 "framerate", GST_TYPE_FRACTION, stream->framerate_numerator,
1450 stream->framerate_denominator, NULL);
1451 }
1452 rmdemux->n_video_streams++;
1453
1454 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
1455 char *name = g_strdup_printf ("audio_%u", rmdemux->n_audio_streams);
1456
1457 stream->pad =
1458 gst_pad_new_from_static_template (&gst_rmdemux_audiosrc_template, name);
1459 GST_LOG_OBJECT (rmdemux, "Created audio pad \"%s\"", name);
1460 g_free (name);
1461
1462 codec_tag = GST_TAG_AUDIO_CODEC;
1463
1464 switch (stream->fourcc) {
1465 /* Older RealAudio Codecs */
1466 case GST_RM_AUD_14_4:
1467 version = 1;
1468 break;
1469
1470 case GST_RM_AUD_28_8:
1471 version = 2;
1472 break;
1473
1474 /* DolbyNet (Dolby AC3, low bitrate) */
1475 case GST_RM_AUD_DNET:
1476 stream_caps =
1477 gst_caps_new_simple ("audio/x-ac3", "rate", G_TYPE_INT,
1478 (int) stream->rate, NULL);
1479 stream->needs_descrambling = TRUE;
1480 stream->subpackets_needed = 1;
1481 stream->subpackets = NULL;
1482 break;
1483
1484 /* MPEG-4 based */
1485 case GST_RM_AUD_RAAC:
1486 case GST_RM_AUD_RACP:
1487 stream_caps =
1488 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
1489 (int) 4, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
1490 if (stream->extra_data_size > 0) {
1491 /* strip off an unknown byte in the extra data */
1492 stream->extra_data_offset++;
1493 }
1494 stream->needs_descrambling = TRUE;
1495 stream->subpackets_needed = 1;
1496 stream->subpackets = NULL;
1497 break;
1498
1499 /* Sony ATRAC3 */
1500 case GST_RM_AUD_ATRC:
1501 stream_caps = gst_caps_new_empty_simple ("audio/x-vnd.sony.atrac3");
1502 stream->needs_descrambling = TRUE;
1503 stream->subpackets_needed = stream->height;
1504 stream->subpackets = NULL;
1505 break;
1506
1507 /* RealAudio G2 audio */
1508 case GST_RM_AUD_COOK:
1509 version = 8;
1510 stream->needs_descrambling = TRUE;
1511 stream->subpackets_needed = stream->height;
1512 stream->subpackets = NULL;
1513 break;
1514
1515 /* RALF is lossless */
1516 case GST_RM_AUD_RALF:
1517 GST_DEBUG_OBJECT (rmdemux, "RALF");
1518 stream_caps = gst_caps_new_empty_simple ("audio/x-ralf-mpeg4-generic");
1519 break;
1520
1521 case GST_RM_AUD_SIPR:
1522
1523 if (stream->flavor > 3) {
1524 GST_WARNING_OBJECT (rmdemux, "bad SIPR flavor %d, freeing it",
1525 stream->flavor);
1526 g_object_unref (stream->pad);
1527 gst_rmdemux_free_stream (rmdemux, stream);
1528 goto beach;
1529 }
1530
1531 GST_DEBUG_OBJECT (rmdemux, "SIPR");
1532 stream_caps = gst_caps_new_empty_simple ("audio/x-sipro");
1533 stream->needs_descrambling = TRUE;
1534 stream->subpackets_needed = stream->height;
1535 stream->subpackets = NULL;
1536 stream->leaf_size = sipr_subpk_size[stream->flavor];
1537
1538 break;
1539
1540 default:
1541 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1542 "fourcc", G_TYPE_UINT, stream->fourcc, NULL);
1543 GST_WARNING_OBJECT (rmdemux,
1544 "Unknown audio FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1545 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1546 break;
1547 }
1548
1549 if (version) {
1550 stream_caps =
1551 gst_caps_new_simple ("audio/x-pn-realaudio", "raversion", G_TYPE_INT,
1552 (int) version, NULL);
1553 }
1554
1555 if (stream_caps) {
1556 gst_caps_set_simple (stream_caps,
1557 "flavor", G_TYPE_INT, (int) stream->flavor,
1558 "rate", G_TYPE_INT, (int) stream->rate,
1559 "channels", G_TYPE_INT, (int) stream->n_channels,
1560 "width", G_TYPE_INT, (int) stream->sample_width,
1561 "leaf_size", G_TYPE_INT, (int) stream->leaf_size,
1562 "packet_size", G_TYPE_INT, (int) stream->packet_size,
1563 "bitrate", G_TYPE_INT, (int) stream->bitrate,
1564 "height", G_TYPE_INT, (int) stream->height, NULL);
1565 }
1566 rmdemux->n_audio_streams++;
1567 } else {
1568 GST_WARNING_OBJECT (rmdemux, "not adding stream of type %d, freeing it",
1569 stream->subtype);
1570 gst_rmdemux_free_stream (rmdemux, stream);
1571 goto beach;
1572 }
1573
1574 GST_PAD_ELEMENT_PRIVATE (stream->pad) = stream;
1575 rmdemux->streams = g_slist_append (rmdemux->streams, stream);
1576 GST_LOG_OBJECT (rmdemux, "n_streams is now %d",
1577 g_slist_length (rmdemux->streams));
1578
1579 GST_LOG ("stream->pad = %p, stream_caps = %" GST_PTR_FORMAT, stream->pad,
1580 stream_caps);
1581
1582 if (stream->pad && stream_caps) {
1583 GstEvent *event;
1584
1585 GST_LOG_OBJECT (rmdemux,
1586 "%" G_GSIZE_FORMAT " bytes of extra data for stream %s",
1587 stream->extra_data_size - stream->extra_data_offset,
1588 GST_PAD_NAME (stream->pad));
1589
1590 /* add codec_data if there is any */
1591 if (stream->extra_data_size - stream->extra_data_offset > 0) {
1592 GstBuffer *buffer;
1593
1594 buffer =
1595 gst_buffer_new_and_alloc (stream->extra_data_size -
1596 stream->extra_data_offset);
1597 gst_buffer_fill (buffer, 0,
1598 stream->extra_data + stream->extra_data_offset,
1599 stream->extra_data_size - stream->extra_data_offset);
1600
1601 gst_caps_set_simple (stream_caps, "codec_data", GST_TYPE_BUFFER,
1602 buffer, NULL);
1603
1604 gst_buffer_unref (buffer);
1605 }
1606
1607 gst_pad_use_fixed_caps (stream->pad);
1608
1609 gst_pad_set_event_function (stream->pad,
1610 GST_DEBUG_FUNCPTR (gst_rmdemux_src_event));
1611 gst_pad_set_query_function (stream->pad,
1612 GST_DEBUG_FUNCPTR (gst_rmdemux_src_query));
1613
1614 GST_DEBUG_OBJECT (rmdemux, "adding pad %s with caps %" GST_PTR_FORMAT
1615 ", stream_id=%d", GST_PAD_NAME (stream->pad), stream_caps, stream->id);
1616 gst_pad_set_active (stream->pad, TRUE);
1617
1618 stream_id =
1619 gst_pad_create_stream_id_printf (stream->pad,
1620 GST_ELEMENT_CAST (rmdemux), "%03u", stream->id);
1621
1622 event =
1623 gst_pad_get_sticky_event (rmdemux->sinkpad, GST_EVENT_STREAM_START, 0);
1624 if (event) {
1625 if (gst_event_parse_group_id (event, &rmdemux->group_id))
1626 rmdemux->have_group_id = TRUE;
1627 else
1628 rmdemux->have_group_id = FALSE;
1629 gst_event_unref (event);
1630 } else if (!rmdemux->have_group_id) {
1631 rmdemux->have_group_id = TRUE;
1632 rmdemux->group_id = gst_util_group_id_next ();
1633 }
1634
1635 event = gst_event_new_stream_start (stream_id);
1636 if (rmdemux->have_group_id)
1637 gst_event_set_group_id (event, rmdemux->group_id);
1638
1639 gst_pad_push_event (stream->pad, event);
1640 g_free (stream_id);
1641
1642 gst_pad_set_caps (stream->pad, stream_caps);
1643
1644 codec_name = gst_pb_utils_get_codec_description (stream_caps);
1645
1646 /* save for later, we must send the tags after the newsegment event */
1647 if (codec_tag != NULL && codec_name != NULL) {
1648 if (stream->pending_tags == NULL)
1649 stream->pending_tags = gst_tag_list_new_empty ();
1650 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_KEEP,
1651 codec_tag, codec_name, NULL);
1652 g_free (codec_name);
1653 }
1654 gst_element_add_pad (GST_ELEMENT_CAST (rmdemux), stream->pad);
1655 gst_flow_combiner_add_pad (rmdemux->flowcombiner, stream->pad);
1656 }
1657
1658 beach:
1659
1660 if (stream_caps)
1661 gst_caps_unref (stream_caps);
1662 }
1663
1664 static gsize
1665 re_skip_pascal_string (const guint8 * ptr)
1666 {
1667 gsize length;
1668
1669 length = ptr[0];
1670
1671 return length + 1;
1672 }
1673
1674 static gboolean
1675 gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data, gsize length)
1676 {
1677 if (length < 8)
1678 return FALSE;
1679
1680 GST_LOG_OBJECT (rmdemux, "file_version: %d", RMDEMUX_GUINT32_GET (data));
1681 GST_LOG_OBJECT (rmdemux, "num_headers: %d", RMDEMUX_GUINT32_GET (data + 4));
1682
1683 return TRUE;
1684 }
1685
1686 static gboolean
1687 gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data, gsize length)
1688 {
1689 if (length < 40)
1690 return FALSE;
1691
1692 GST_LOG_OBJECT (rmdemux, "max bitrate: %d", RMDEMUX_GUINT32_GET (data));
1693 GST_LOG_OBJECT (rmdemux, "avg bitrate: %d", RMDEMUX_GUINT32_GET (data + 4));
1694 GST_LOG_OBJECT (rmdemux, "max packet size: %d",
1695 RMDEMUX_GUINT32_GET (data + 8));
1696 rmdemux->avg_packet_size = RMDEMUX_GUINT32_GET (data + 12);
1697 GST_LOG_OBJECT (rmdemux, "avg packet size: %d", rmdemux->avg_packet_size);
1698 rmdemux->num_packets = RMDEMUX_GUINT32_GET (data + 16);
1699 GST_LOG_OBJECT (rmdemux, "number of packets: %d", rmdemux->num_packets);
1700
1701 GST_LOG_OBJECT (rmdemux, "duration: %d", RMDEMUX_GUINT32_GET (data + 20));
1702 rmdemux->duration = RMDEMUX_GUINT32_GET (data + 20) * GST_MSECOND;
1703
1704 GST_LOG_OBJECT (rmdemux, "preroll: %d", RMDEMUX_GUINT32_GET (data + 24));
1705 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 28);
1706 GST_LOG_OBJECT (rmdemux, "offset of INDX section: 0x%08x",
1707 rmdemux->index_offset);
1708 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 32);
1709 GST_LOG_OBJECT (rmdemux, "offset of DATA section: 0x%08x",
1710 rmdemux->data_offset);
1711 GST_LOG_OBJECT (rmdemux, "n streams: %d", RMDEMUX_GUINT16_GET (data + 36));
1712 GST_LOG_OBJECT (rmdemux, "flags: 0x%04x", RMDEMUX_GUINT16_GET (data + 38));
1713
1714 return TRUE;
1715 }
1716
1717 static gboolean
1718 gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux, const guint8 * data, gsize length)
1719 {
1720 GstRMDemuxStream *stream;
1721 char *stream1_type_string;
1722 char *stream2_type_string;
1723 gsize str_len = 0;
1724 int stream_type;
1725 guint32 max_bitrate;
1726 guint32 avg_bitrate;
1727
1728 if (length < 10)
1729 return FALSE;
1730
1731 stream = g_new0 (GstRMDemuxStream, 1);
1732
1733 stream->id = RMDEMUX_GUINT16_GET (data);
1734 stream->index = NULL;
1735 stream->seek_offset = 0;
1736 stream->last_ts = -1;
1737 stream->next_ts = -1;
1738 stream->discont = TRUE;
1739 stream->adapter = gst_adapter_new ();
1740 GST_LOG_OBJECT (rmdemux, "stream_number=%d", stream->id);
1741
1742 /* parse the bitrates */
1743 max_bitrate = RMDEMUX_GUINT32_GET (data + 2);
1744 avg_bitrate = RMDEMUX_GUINT32_GET (data + 6);
1745 stream->bitrate = avg_bitrate;
1746 GST_LOG_OBJECT (rmdemux, "Stream max bitrate=%u", max_bitrate);
1747 GST_LOG_OBJECT (rmdemux, "Stream avg bitrate=%u", avg_bitrate);
1748 if (max_bitrate != 0) {
1749 if (stream->pending_tags == NULL)
1750 stream->pending_tags = gst_tag_list_new_empty ();
1751 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1752 GST_TAG_MAXIMUM_BITRATE, max_bitrate, NULL);
1753 }
1754 if (avg_bitrate != 0) {
1755 if (stream->pending_tags == NULL)
1756 stream->pending_tags = gst_tag_list_new_empty ();
1757 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE,
1758 GST_TAG_BITRATE, avg_bitrate, NULL);
1759 }
1760
1761 if (length < 30) {
1762 gst_rmdemux_free_stream (rmdemux, stream);
1763 return FALSE;
1764 }
1765 data += 30;
1766 length -= 30;
1767 stream1_type_string = gst_rm_utils_read_string8 (data, length, &str_len);
1768 data += str_len;
1769 length -= str_len;
1770 stream2_type_string = gst_rm_utils_read_string8 (data, length, &str_len);
1771 data += str_len;
1772 length -= str_len;
1773
1774 /* stream1_type_string for audio and video stream is a "put_whatever_you_want" field :
1775 * observed values :
1776 * - "[The ]Video/Audio Stream" (File produced by an official Real encoder)
1777 * - "RealVideoPremierePlugIn-VIDEO/AUDIO" (File produced by Abobe Premiere)
1778 *
1779 * so, we should not rely on it to know which stream type it is
1780 */
1781
1782 if (!stream1_type_string || !stream2_type_string) {
1783 g_free (stream1_type_string);
1784 g_free (stream2_type_string);
1785 gst_rmdemux_free_stream (rmdemux, stream);
1786 return FALSE;
1787 }
1788
1789 GST_LOG_OBJECT (rmdemux, "stream type: %s", stream1_type_string);
1790 GST_LOG_OBJECT (rmdemux, "MIME type=%s", stream2_type_string);
1791
1792 if (strcmp (stream2_type_string, "video/x-pn-realvideo") == 0) {
1793 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1794 } else if (strcmp (stream2_type_string,
1795 "video/x-pn-multirate-realvideo") == 0) {
1796 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1797 } else if (strcmp (stream2_type_string, "audio/x-pn-realaudio") == 0) {
1798 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1799 } else if (strcmp (stream2_type_string,
1800 "audio/x-pn-multirate-realaudio") == 0) {
1801 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1802 } else if (strcmp (stream2_type_string,
1803 "audio/x-pn-multirate-realaudio-live") == 0) {
1804 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1805 } else if (strcmp (stream2_type_string, "audio/x-ralf-mpeg4-generic") == 0) {
1806 /* Another audio type found in the real testsuite */
1807 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1808 } else if (strcmp (stream1_type_string, "") == 0 &&
1809 strcmp (stream2_type_string, "logical-fileinfo") == 0) {
1810 stream_type = GST_RMDEMUX_STREAM_FILEINFO;
1811 } else {
1812 stream_type = GST_RMDEMUX_STREAM_UNKNOWN;
1813 GST_WARNING_OBJECT (rmdemux, "unknown stream type \"%s\",\"%s\"",
1814 stream1_type_string, stream2_type_string);
1815 }
1816 g_free (stream1_type_string);
1817 g_free (stream2_type_string);
1818
1819 if (length < 4) {
1820 gst_rmdemux_free_stream (rmdemux, stream);
1821 return FALSE;
1822 }
1823 data += 4;
1824 length -= 4;
1825
1826 stream->subtype = stream_type;
1827 switch (stream_type) {
1828
1829 case GST_RMDEMUX_STREAM_VIDEO:
1830 if (length < 34) {
1831 gst_rmdemux_free_stream (rmdemux, stream);
1832 return FALSE;
1833 }
1834 /* RV10/RV20/RV30/RV40 => video/x-pn-realvideo, version=1,2,3,4 */
1835 stream->fourcc = RMDEMUX_FOURCC_GET (data + 8);
1836 stream->width = RMDEMUX_GUINT16_GET (data + 12);
1837 stream->height = RMDEMUX_GUINT16_GET (data + 14);
1838 stream->rate = RMDEMUX_GUINT16_GET (data + 16);
1839 stream->subformat = RMDEMUX_GUINT32_GET (data + 26);
1840 stream->format = RMDEMUX_GUINT32_GET (data + 30);
1841 stream->extra_data_size = length - 26;
1842 stream->extra_data = g_memdup2 (data + 26, length - 26);
1843 /* Natural way to represent framerates here requires unsigned 32 bit
1844 * numerator, which we don't have. For the nasty case, approximate...
1845 */
1846 {
1847 guint32 numerator = RMDEMUX_GUINT16_GET (data + 22) * 65536 +
1848 RMDEMUX_GUINT16_GET (data + 24);
1849 if (numerator > G_MAXINT) {
1850 stream->framerate_numerator = (gint) (numerator >> 1);
1851 stream->framerate_denominator = 32768;
1852 } else {
1853 stream->framerate_numerator = (gint) numerator;
1854 stream->framerate_denominator = 65536;
1855 }
1856 }
1857
1858 GST_DEBUG_OBJECT (rmdemux,
1859 "Video stream with fourcc=%" GST_FOURCC_FORMAT
1860 " width=%d height=%d rate=%d framerate=%d/%d subformat=%x format=%x extra_data_size=%"
1861 G_GSIZE_FORMAT, GST_FOURCC_ARGS (stream->fourcc), stream->width,
1862 stream->height, stream->rate, stream->framerate_numerator,
1863 stream->framerate_denominator, stream->subformat, stream->format,
1864 stream->extra_data_size);
1865 break;
1866 case GST_RMDEMUX_STREAM_AUDIO:{
1867 if (length < 6) {
1868 gst_rmdemux_free_stream (rmdemux, stream);
1869 return FALSE;
1870 }
1871 stream->version = RMDEMUX_GUINT16_GET (data + 4);
1872 GST_INFO ("stream version = %u", stream->version);
1873 switch (stream->version) {
1874 case 3:
1875 stream->fourcc = GST_RM_AUD_14_4;
1876 stream->packet_size = 20;
1877 stream->rate = 8000;
1878 stream->n_channels = 1;
1879 stream->sample_width = 16;
1880 stream->flavor = 1;
1881 stream->leaf_size = 0;
1882 stream->height = 0;
1883 break;
1884 case 4:
1885 if (length < 73) {
1886 gst_rmdemux_free_stream (rmdemux, stream);
1887 return FALSE;
1888 }
1889 stream->flavor = RMDEMUX_GUINT16_GET (data + 22);
1890 stream->packet_size = RMDEMUX_GUINT32_GET (data + 24);
1891 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + 42); */
1892 stream->leaf_size = RMDEMUX_GUINT16_GET (data + 44);
1893 stream->height = RMDEMUX_GUINT16_GET (data + 40);
1894 stream->rate = RMDEMUX_GUINT16_GET (data + 48);
1895 stream->sample_width = RMDEMUX_GUINT16_GET (data + 52);
1896 stream->n_channels = RMDEMUX_GUINT16_GET (data + 54);
1897 stream->fourcc = RMDEMUX_FOURCC_GET (data + 62);
1898 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + 69);
1899 GST_DEBUG_OBJECT (rmdemux,
1900 "%" G_GSIZE_FORMAT " bytes of extra codec data",
1901 stream->extra_data_size);
1902 if (length - 73 >= stream->extra_data_size) {
1903 stream->extra_data = g_memdup2 (data + 73, stream->extra_data_size);
1904 } else {
1905 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1906 stream->extra_data_size = 0;
1907 }
1908 break;
1909 case 5:
1910 if (length < 78) {
1911 gst_rmdemux_free_stream (rmdemux, stream);
1912 return FALSE;
1913 }
1914 stream->flavor = RMDEMUX_GUINT16_GET (data + 22);
1915 stream->packet_size = RMDEMUX_GUINT32_GET (data + 24);
1916 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + 42); */
1917 stream->leaf_size = RMDEMUX_GUINT16_GET (data + 44);
1918 stream->height = RMDEMUX_GUINT16_GET (data + 40);
1919 stream->rate = RMDEMUX_GUINT16_GET (data + 54);
1920 stream->sample_width = RMDEMUX_GUINT16_GET (data + 58);
1921 stream->n_channels = RMDEMUX_GUINT16_GET (data + 60);
1922 stream->fourcc = RMDEMUX_FOURCC_GET (data + 66);
1923 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + 74);
1924 GST_DEBUG_OBJECT (rmdemux,
1925 "%" G_GSIZE_FORMAT " bytes of extra codec data",
1926 stream->extra_data_size);
1927 if (length - 78 >= stream->extra_data_size) {
1928 stream->extra_data = g_memdup2 (data + 78, stream->extra_data_size);
1929 } else {
1930 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1931 stream->extra_data_size = 0;
1932 }
1933 break;
1934 default:{
1935 GST_WARNING_OBJECT (rmdemux, "Unhandled audio stream version %d",
1936 stream->version);
1937 break;
1938 }
1939 }
1940 /* 14_4, 28_8, cook, dnet, sipr, raac, racp, ralf, atrc */
1941 GST_DEBUG_OBJECT (rmdemux,
1942 "Audio stream with rate=%d sample_width=%d n_channels=%d",
1943 stream->rate, stream->sample_width, stream->n_channels);
1944
1945 break;
1946 }
1947 case GST_RMDEMUX_STREAM_FILEINFO:
1948 {
1949 int element_nb;
1950
1951 if (length < 12) {
1952 gst_rmdemux_free_stream (rmdemux, stream);
1953 return FALSE;
1954 }
1955
1956 /* Length of this section */
1957 GST_DEBUG_OBJECT (rmdemux, "length2: 0x%08x", RMDEMUX_GUINT32_GET (data));
1958
1959 /* Unknown : 00 00 00 00 */
1960
1961 /* Number of variables that would follow (loop iterations) */
1962 element_nb = RMDEMUX_GUINT32_GET (data + 8);
1963
1964 data += 12;
1965 length -= 12;
1966
1967 while (element_nb) {
1968 gsize str_len;
1969
1970 /* Category Id : 00 00 00 XX 00 00 */
1971 if (length < 6 + 1) {
1972 gst_rmdemux_free_stream (rmdemux, stream);
1973 return FALSE;
1974 }
1975 data += 6;
1976 length -= 6;
1977
1978 /* Variable Name */
1979 str_len = re_skip_pascal_string (data);
1980 if (length < str_len) {
1981 gst_rmdemux_free_stream (rmdemux, stream);
1982 return FALSE;
1983 }
1984 data += str_len;
1985 length -= str_len;
1986
1987 /* Variable Value Type */
1988 /* 00 00 00 00 00 => integer/boolean, preceded by length */
1989 /* 00 00 00 02 00 => pascal string, preceded by length, no trailing \0 */
1990 if (length < 5 + 1) {
1991 gst_rmdemux_free_stream (rmdemux, stream);
1992 return FALSE;
1993 }
1994 data += 5;
1995 length -= 5;
1996
1997 /* Variable Value */
1998 str_len = re_skip_pascal_string (data);
1999 if (length < str_len) {
2000 gst_rmdemux_free_stream (rmdemux, stream);
2001 return FALSE;
2002 }
2003 data += str_len;
2004 length -= str_len;
2005
2006 element_nb--;
2007 }
2008 }
2009 break;
2010 case GST_RMDEMUX_STREAM_UNKNOWN:
2011 default:
2012 break;
2013 }
2014
2015 gst_rmdemux_add_stream (rmdemux, stream);
2016
2017 return TRUE;
2018 }
2019
2020 static gboolean
2021 gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data, gsize length,
2022 gsize * index_length)
2023 {
2024 int n;
2025 int id;
2026
2027 if (length < 10)
2028 return FALSE;
2029
2030 n = RMDEMUX_GUINT32_GET (data);
2031 id = RMDEMUX_GUINT16_GET (data + 4);
2032 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 6);
2033
2034 GST_DEBUG_OBJECT (rmdemux,
2035 "Number of indices=%d Stream ID=%d length=%" G_GSIZE_FORMAT, n, id,
2036 length);
2037
2038 /* Point to the next index_stream */
2039 rmdemux->index_stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
2040
2041 /* Return the length of the index */
2042 if (!g_size_checked_mul (index_length, 14, n))
2043 return FALSE;
2044
2045 return TRUE;
2046 }
2047
2048 static gboolean
2049 gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux, const guint8 * data,
2050 gsize length)
2051 {
2052 int i;
2053 int n;
2054 GstRMDemuxIndex *index;
2055
2056 /* The number of index records */
2057 n = length / 14;
2058
2059 if (rmdemux->index_stream == NULL)
2060 return TRUE;
2061
2062 /* don't parse the index a second time when operating pull-based and
2063 * reaching the end of the file */
2064 if (rmdemux->index_stream->index_length > 0) {
2065 GST_DEBUG_OBJECT (rmdemux, "Already have an index for this stream");
2066 return TRUE;
2067 }
2068
2069 index = g_new (GstRMDemuxIndex, n);
2070 rmdemux->index_stream->index = index;
2071 rmdemux->index_stream->index_length = n;
2072
2073 for (i = 0; i < n; i++) {
2074 index[i].timestamp = RMDEMUX_GUINT32_GET (data + 2) * GST_MSECOND;
2075 index[i].offset = RMDEMUX_GUINT32_GET (data + 6);
2076
2077 GST_DEBUG_OBJECT (rmdemux, "Index found for timestamp=%f (at offset=%x)",
2078 gst_guint64_to_gdouble (index[i].timestamp) / GST_SECOND,
2079 index[i].offset);
2080 data += 14;
2081 }
2082
2083 return TRUE;
2084 }
2085
2086 static gboolean
2087 gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data, gsize length)
2088 {
2089 if (length < 8)
2090 return FALSE;
2091
2092 rmdemux->n_chunks = RMDEMUX_GUINT32_GET (data);
2093 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 4);
2094 rmdemux->chunk_index = 0;
2095 GST_DEBUG_OBJECT (rmdemux, "Data chunk found with %d packets "
2096 "(next data at 0x%08x)", rmdemux->n_chunks, rmdemux->data_offset);
2097
2098 return TRUE;
2099 }
2100
2101 static gboolean
2102 gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data, gsize length)
2103 {
2104 GstTagList *tags;
2105
2106 tags = gst_rm_utils_read_tags (data, length, gst_rm_utils_read_string16);
2107
2108 if (tags) {
2109 GstTagList *old_tags = rmdemux->pending_tags;
2110
2111 GST_LOG_OBJECT (rmdemux, "tags: %" GST_PTR_FORMAT, tags);
2112
2113 rmdemux->pending_tags =
2114 gst_tag_list_merge (old_tags, tags, GST_TAG_MERGE_APPEND);
2115
2116 gst_tag_list_unref (tags);
2117 if (old_tags)
2118 gst_tag_list_unref (old_tags);
2119
2120 gst_tag_list_set_scope (rmdemux->pending_tags, GST_TAG_SCOPE_GLOBAL);
2121 }
2122
2123 return TRUE;
2124 }
2125
2126 static void
2127 gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
2128 GstRMDemuxStream * stream)
2129 {
2130 if (stream->subpackets == NULL || stream->subpackets->len == 0)
2131 return;
2132
2133 GST_DEBUG_OBJECT (rmdemux, "discarding %u previously collected subpackets",
2134 stream->subpackets->len);
2135 g_ptr_array_foreach (stream->subpackets, (GFunc) gst_mini_object_unref, NULL);
2136 g_ptr_array_set_size (stream->subpackets, 0);
2137 }
2138
2139 static GstFlowReturn
2140 gst_rmdemux_descramble_audio (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
2141 {
2142 GstFlowReturn ret = GST_FLOW_ERROR;
2143 GstBuffer *outbuf;
2144 GstMapInfo outmap;
2145 guint packet_size = stream->packet_size;
2146 guint height = stream->subpackets->len;
2147 guint leaf_size = stream->leaf_size;
2148 guint size;
2149 guint p, x;
2150
2151 g_assert (stream->height == height);
2152
2153 GST_LOG_OBJECT (rmdemux, "packet_size = %u, leaf_size = %u, height= %u",
2154 packet_size, leaf_size, height);
2155
2156 if (!g_uint_checked_mul (&size, height, packet_size)) {
2157 GST_WARNING_OBJECT (rmdemux, "overflowing audio packet size");
2158 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2159 return GST_FLOW_OK;
2160 }
2161
2162 if (stream->leaf_size == 0) {
2163 GST_WARNING_OBJECT (rmdemux, "invalid audio leaf packet size");
2164 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2165 return GST_FLOW_OK;
2166 }
2167
2168 outbuf = gst_buffer_new_and_alloc (size);
2169 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
2170
2171 for (p = 0; p < height; ++p) {
2172 GstBuffer *b = g_ptr_array_index (stream->subpackets, p);
2173 GstMapInfo map;
2174
2175 if (gst_buffer_get_size (b) < packet_size) {
2176 GST_ERROR_OBJECT (rmdemux, "too small audio subpacket");
2177 gst_buffer_unmap (outbuf, &outmap);
2178 gst_buffer_unref (outbuf);
2179 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2180 return GST_FLOW_OK;
2181 }
2182
2183 gst_buffer_map (b, &map, GST_MAP_READ);
2184
2185 if (p == 0) {
2186 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (b);
2187 GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (b);
2188 }
2189
2190 for (x = 0; x < packet_size / leaf_size; ++x) {
2191 guint idx;
2192
2193 idx = height * x + ((height + 1) / 2) * (p % 2) + (p / 2);
2194
2195 /* GST_LOG ("%3u => %3u", (height * p) + x, idx); */
2196 memcpy (outmap.data + leaf_size * idx, map.data + leaf_size * x,
2197 leaf_size);
2198 }
2199 gst_buffer_unmap (b, &map);
2200 }
2201 gst_buffer_unmap (outbuf, &outmap);
2202
2203 /* some decoders, such as realaudiodec, need to be fed in packet units */
2204 for (p = 0; p < height; ++p) {
2205 GstBuffer *subbuf;
2206
2207 subbuf =
2208 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, p * packet_size,
2209 packet_size);
2210
2211 GST_LOG_OBJECT (rmdemux, "pushing buffer dts %" GST_TIME_FORMAT ", pts %"
2212 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (subbuf)),
2213 GST_TIME_ARGS (GST_BUFFER_PTS (subbuf)));
2214
2215 if (stream->discont) {
2216 GST_BUFFER_FLAG_SET (subbuf, GST_BUFFER_FLAG_DISCONT);
2217 stream->discont = FALSE;
2218 }
2219
2220 ret = gst_pad_push (stream->pad, subbuf);
2221 if (ret != GST_FLOW_OK)
2222 break;
2223 }
2224
2225 gst_buffer_unref (outbuf);
2226
2227 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2228
2229 return ret;
2230 }
2231
2232 static GstFlowReturn
2233 gst_rmdemux_descramble_dnet_audio (GstRMDemux * rmdemux,
2234 GstRMDemuxStream * stream)
2235 {
2236 GstBuffer *buf;
2237
2238 buf = g_ptr_array_index (stream->subpackets, 0);
2239 g_ptr_array_index (stream->subpackets, 0) = NULL;
2240 g_ptr_array_set_size (stream->subpackets, 0);
2241
2242 buf = gst_rm_utils_descramble_dnet_buffer (buf);
2243
2244 if (stream->discont) {
2245 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2246 stream->discont = FALSE;
2247 }
2248 return gst_pad_push (stream->pad, buf);
2249 }
2250
2251 static GstFlowReturn
2252 gst_rmdemux_descramble_mp4a_audio (GstRMDemux * rmdemux,
2253 GstRMDemuxStream * stream)
2254 {
2255 GstFlowReturn res;
2256 GstBuffer *buf, *outbuf;
2257 guint frames, index, i;
2258 GstMapInfo map;
2259 GstClockTime timestamp;
2260
2261 res = GST_FLOW_OK;
2262
2263 buf = g_ptr_array_index (stream->subpackets, 0);
2264 g_ptr_array_index (stream->subpackets, 0) = NULL;
2265 g_ptr_array_set_size (stream->subpackets, 0);
2266
2267 gst_buffer_map (buf, &map, GST_MAP_READ);
2268 timestamp = GST_BUFFER_PTS (buf);
2269
2270 if (map.size < 2) {
2271 GST_WARNING_OBJECT (rmdemux, "Too small mp4a buffer");
2272 gst_buffer_unmap (buf, &map);
2273 gst_buffer_unref (buf);
2274 return GST_FLOW_OK;
2275 }
2276 frames = (map.data[1] & 0xf0) >> 4;
2277 index = 2 * frames + 2;
2278
2279 if (map.size < 2 * frames + 2) {
2280 GST_WARNING_OBJECT (rmdemux, "Too small mp4a buffer");
2281 gst_buffer_unmap (buf, &map);
2282 gst_buffer_unref (buf);
2283 return GST_FLOW_OK;
2284 }
2285
2286 for (i = 0; i < frames; i++) {
2287 guint len = (map.data[i * 2 + 2] << 8) | map.data[i * 2 + 3];
2288
2289 if (map.size < index + len) {
2290 GST_WARNING_OBJECT (rmdemux, "Too small mp4a buffer");
2291 gst_buffer_unmap (buf, &map);
2292 gst_buffer_unref (buf);
2293 return GST_FLOW_OK;
2294 }
2295
2296 outbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, index, len);
2297 if (i == 0) {
2298 GST_BUFFER_PTS (outbuf) = timestamp;
2299 GST_BUFFER_DTS (outbuf) = timestamp;
2300 }
2301
2302 index += len;
2303
2304 if (stream->discont) {
2305 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2306 stream->discont = FALSE;
2307 }
2308 res = gst_pad_push (stream->pad, outbuf);
2309 if (res != GST_FLOW_OK)
2310 break;
2311 }
2312 gst_buffer_unmap (buf, &map);
2313 gst_buffer_unref (buf);
2314 return res;
2315 }
2316
2317 static GstFlowReturn
2318 gst_rmdemux_descramble_sipr_audio (GstRMDemux * rmdemux,
2319 GstRMDemuxStream * stream)
2320 {
2321 GstFlowReturn ret;
2322 GstBuffer *outbuf;
2323 GstMapInfo outmap;
2324 guint packet_size = stream->packet_size;
2325 guint height = stream->subpackets->len;
2326 guint size;
2327 guint p;
2328
2329 g_assert (stream->height == height);
2330
2331 GST_LOG_OBJECT (rmdemux, "packet_size = %u, leaf_size = %u, height= %u",
2332 packet_size, stream->leaf_size, height);
2333
2334 if (!g_uint_checked_mul (&size, height, packet_size)) {
2335 GST_WARNING_OBJECT (rmdemux, "overflowing SIPR audio packet size");
2336 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2337 return GST_FLOW_OK;
2338 }
2339
2340 outbuf = gst_buffer_new_and_alloc (size);
2341 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
2342
2343 for (p = 0; p < height; ++p) {
2344 GstBuffer *b = g_ptr_array_index (stream->subpackets, p);
2345
2346 if (p == 0) {
2347 GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (b);
2348 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (b);
2349 }
2350
2351 if (gst_buffer_get_size (b) < packet_size) {
2352 GST_ERROR_OBJECT (rmdemux, "too small sipr audio subpacket");
2353 gst_buffer_unmap (outbuf, &outmap);
2354 gst_buffer_unref (outbuf);
2355 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2356 return GST_FLOW_OK;
2357 }
2358
2359 gst_buffer_extract (b, 0, outmap.data + packet_size * p, packet_size);
2360 }
2361 gst_buffer_unmap (outbuf, &outmap);
2362
2363 GST_LOG_OBJECT (rmdemux, "pushing buffer dts %" GST_TIME_FORMAT ", pts %"
2364 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (outbuf)),
2365 GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)));
2366
2367 if (stream->discont) {
2368 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2369 stream->discont = FALSE;
2370 }
2371
2372 outbuf = gst_rm_utils_descramble_sipr_buffer (outbuf);
2373 if (outbuf) {
2374 ret = gst_pad_push (stream->pad, outbuf);
2375 } else {
2376 GST_WARNING_OBJECT (rmdemux, "failed to descramble buffer");
2377 ret = GST_FLOW_OK;
2378 }
2379
2380 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2381
2382 return ret;
2383 }
2384
2385 static GstFlowReturn
2386 gst_rmdemux_handle_scrambled_packet (GstRMDemux * rmdemux,
2387 GstRMDemuxStream * stream, GstBuffer * buf, gboolean keyframe)
2388 {
2389 GstFlowReturn ret;
2390
2391 if (stream->subpackets == NULL)
2392 stream->subpackets = g_ptr_array_sized_new (stream->subpackets_needed);
2393
2394 GST_LOG ("Got subpacket %u/%u, len=%" G_GSIZE_FORMAT ", key=%d",
2395 stream->subpackets->len + 1, stream->subpackets_needed,
2396 gst_buffer_get_size (buf), keyframe);
2397
2398 if (keyframe && stream->subpackets->len > 0) {
2399 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
2400 }
2401
2402 g_ptr_array_add (stream->subpackets, buf);
2403
2404 if (stream->subpackets->len < stream->subpackets_needed)
2405 return GST_FLOW_OK;
2406
2407 g_assert (stream->subpackets->len >= 1);
2408
2409 switch (stream->fourcc) {
2410 case GST_RM_AUD_DNET:
2411 ret = gst_rmdemux_descramble_dnet_audio (rmdemux, stream);
2412 break;
2413 case GST_RM_AUD_COOK:
2414 case GST_RM_AUD_ATRC:
2415 ret = gst_rmdemux_descramble_audio (rmdemux, stream);
2416 break;
2417 case GST_RM_AUD_RAAC:
2418 case GST_RM_AUD_RACP:
2419 ret = gst_rmdemux_descramble_mp4a_audio (rmdemux, stream);
2420 break;
2421 case GST_RM_AUD_SIPR:
2422 ret = gst_rmdemux_descramble_sipr_audio (rmdemux, stream);
2423 break;
2424 default:
2425 ret = GST_FLOW_ERROR;
2426 g_assert_not_reached ();
2427 }
2428
2429 return ret;
2430 }
2431
2432 #define PARSE_NUMBER(data, size, number, label) \
2433 G_STMT_START { \
2434 if (size < 2) \
2435 goto label; \
2436 number = GST_READ_UINT16_BE (data); \
2437 if (!(number & 0xc000)) { \
2438 if (size < 4) \
2439 goto label; \
2440 number = GST_READ_UINT32_BE (data); \
2441 data += 4; \
2442 size -= 4; \
2443 } else { \
2444 number &= 0x3fff; \
2445 data += 2; \
2446 size -= 2; \
2447 } \
2448 } G_STMT_END
2449
2450 static GstFlowReturn
2451 gst_rmdemux_parse_video_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2452 GstBuffer * in, guint offset, guint16 version,
2453 GstClockTime timestamp, gboolean key)
2454 {
2455 GstFlowReturn ret;
2456 GstMapInfo map;
2457 const guint8 *data;
2458 gsize size;
2459
2460 gst_buffer_map (in, &map, GST_MAP_READ);
2461
2462 if (map.size < offset)
2463 goto not_enough_data;
2464
2465 data = map.data + offset;
2466 size = map.size - offset;
2467
2468 /* if size <= 2, we want this method to return the same GstFlowReturn as it
2469 * was previously for that given stream. */
2470 ret = GST_PAD_LAST_FLOW_RETURN (stream->pad);
2471
2472 while (size > 2) {
2473 guint8 pkg_header;
2474 guint pkg_offset;
2475 guint pkg_length;
2476 guint pkg_subseq = 0, pkg_seqnum = G_MAXUINT;
2477 guint fragment_size;
2478 GstBuffer *fragment;
2479
2480 pkg_header = *data++;
2481 size--;
2482
2483 /* packet header
2484 * bit 7: 1=last block in block chain
2485 * bit 6: 1=short header (only one block?)
2486 */
2487 if ((pkg_header & 0xc0) == 0x40) {
2488 /* skip unknown byte */
2489 data++;
2490 size--;
2491 pkg_offset = 0;
2492 pkg_length = size;
2493 } else {
2494 if ((pkg_header & 0x40) == 0) {
2495 pkg_subseq = (*data++) & 0x7f;
2496 size--;
2497 } else {
2498 pkg_subseq = 0;
2499 }
2500
2501 /* length */
2502 PARSE_NUMBER (data, size, pkg_length, not_enough_data);
2503
2504 /* offset */
2505 PARSE_NUMBER (data, size, pkg_offset, not_enough_data);
2506
2507 /* seqnum */
2508 if (size < 1)
2509 goto not_enough_data;
2510
2511 pkg_seqnum = *data++;
2512 size--;
2513 }
2514
2515 GST_DEBUG_OBJECT (rmdemux,
2516 "seq %d, subseq %d, offset %d, length %d, size %" G_GSIZE_FORMAT
2517 ", header %02x", pkg_seqnum, pkg_subseq, pkg_offset, pkg_length, size,
2518 pkg_header);
2519
2520 /* calc size of fragment */
2521 if ((pkg_header & 0xc0) == 0x80) {
2522 fragment_size = pkg_offset;
2523 } else {
2524 if ((pkg_header & 0xc0) == 0)
2525 fragment_size = size;
2526 else
2527 fragment_size = pkg_length;
2528 }
2529 GST_DEBUG_OBJECT (rmdemux, "fragment size %d", fragment_size);
2530
2531 if (fragment_size > map.size
2532 || (data - map.data) > map.size - fragment_size)
2533 goto not_enough_data;
2534
2535 /* get the fragment */
2536 fragment =
2537 gst_buffer_copy_region (in, GST_BUFFER_COPY_ALL, data - map.data,
2538 fragment_size);
2539
2540 if (pkg_subseq == 1) {
2541 GST_DEBUG_OBJECT (rmdemux, "start new fragment");
2542 gst_adapter_clear (stream->adapter);
2543 stream->frag_current = 0;
2544 stream->frag_count = 0;
2545 stream->frag_length = pkg_length;
2546 } else if (pkg_subseq == 0) {
2547 GST_DEBUG_OBJECT (rmdemux, "non fragmented packet");
2548 stream->frag_current = 0;
2549 stream->frag_count = 0;
2550 stream->frag_length = fragment_size;
2551 }
2552
2553 if (stream->frag_count >= MAX_FRAGS) {
2554 gst_buffer_unref (fragment);
2555 goto too_many_fragments;
2556 }
2557
2558 /* put fragment in adapter */
2559 gst_adapter_push (stream->adapter, fragment);
2560 stream->frag_offset[stream->frag_count] = stream->frag_current;
2561 stream->frag_current += fragment_size;
2562 stream->frag_count++;
2563
2564 GST_DEBUG_OBJECT (rmdemux, "stored fragment in adapter %d/%d",
2565 stream->frag_current, stream->frag_length);
2566
2567 /* flush fragment when complete */
2568 if (stream->frag_current >= stream->frag_length) {
2569 GstBuffer *out;
2570 GstMapInfo outmap;
2571 guint8 *outdata;
2572 guint header_size;
2573 gint i, avail;
2574
2575 /* calculate header size, which is:
2576 * 1 byte for the number of fragments - 1
2577 * for each fragment:
2578 * 4 bytes 0x00000001 little endian
2579 * 4 bytes fragment offset
2580 *
2581 * This is also the matroska header for realvideo, the decoder needs the
2582 * fragment offsets, both in ffmpeg and real .so, so we just give it that
2583 * in front of the data.
2584 */
2585 header_size = 1 + (8 * (stream->frag_count));
2586
2587 GST_DEBUG_OBJECT (rmdemux,
2588 "fragmented completed. count %d, header_size %u", stream->frag_count,
2589 header_size);
2590
2591 avail = gst_adapter_available (stream->adapter);
2592
2593 out = gst_buffer_new_and_alloc (header_size + avail);
2594 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
2595 outdata = outmap.data;
2596
2597 /* create header */
2598 *outdata++ = stream->frag_count - 1;
2599 for (i = 0; i < stream->frag_count; i++) {
2600 GST_WRITE_UINT32_LE (outdata, 0x00000001);
2601 outdata += 4;
2602 GST_WRITE_UINT32_LE (outdata, stream->frag_offset[i]);
2603 outdata += 4;
2604 }
2605
2606 /* copy packet data after the header now */
2607 gst_adapter_copy (stream->adapter, outdata, 0, avail);
2608 gst_adapter_flush (stream->adapter, avail);
2609
2610 stream->frag_current = 0;
2611 stream->frag_count = 0;
2612 stream->frag_length = 0;
2613
2614 if (timestamp != -1) {
2615 if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts)
2616 timestamp -= rmdemux->first_ts;
2617 else
2618 timestamp = 0;
2619
2620 if (rmdemux->base_ts != -1)
2621 timestamp += rmdemux->base_ts;
2622 }
2623 gst_buffer_unmap (out, &outmap);
2624
2625 /* video has DTS */
2626 GST_BUFFER_DTS (out) = timestamp;
2627 GST_BUFFER_PTS (out) = GST_CLOCK_TIME_NONE;
2628
2629 GST_LOG_OBJECT (rmdemux, "pushing timestamp %" GST_TIME_FORMAT,
2630 GST_TIME_ARGS (timestamp));
2631
2632 if (stream->discont) {
2633 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT);
2634 stream->discont = FALSE;
2635 }
2636
2637 if (!key) {
2638 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
2639 }
2640
2641 ret = gst_pad_push (stream->pad, out);
2642 ret = gst_flow_combiner_update_flow (rmdemux->flowcombiner, ret);
2643 if (ret != GST_FLOW_OK)
2644 break;
2645
2646 timestamp = GST_CLOCK_TIME_NONE;
2647 }
2648 data += fragment_size;
2649 size -= fragment_size;
2650 }
2651 GST_DEBUG_OBJECT (rmdemux, "%" G_GSIZE_FORMAT " bytes left", size);
2652
2653 done:
2654 gst_buffer_unmap (in, &map);
2655 gst_buffer_unref (in);
2656
2657 return ret;
2658
2659 /* ERRORS */
2660 not_enough_data:
2661 {
2662 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2663 (NULL));
2664 ret = GST_FLOW_OK;
2665 goto done;
2666 }
2667 too_many_fragments:
2668 {
2669 GST_ELEMENT_ERROR (rmdemux, STREAM, DECODE,
2670 ("Got more fragments (%u) than can be handled (%u)",
2671 stream->frag_count, MAX_FRAGS), (NULL));
2672 ret = GST_FLOW_ERROR;
2673 goto done;
2674 }
2675 }
2676
2677 static GstFlowReturn
2678 gst_rmdemux_parse_audio_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
2679 GstBuffer * in, guint offset, guint16 version,
2680 GstClockTime timestamp, gboolean key)
2681 {
2682 GstFlowReturn ret;
2683 GstBuffer *buffer;
2684
2685 if (gst_buffer_get_size (in) < offset)
2686 goto not_enough_data;
2687
2688 buffer = gst_buffer_copy_region (in, GST_BUFFER_COPY_MEMORY, offset, -1);
2689
2690 if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts)
2691 timestamp -= rmdemux->first_ts;
2692 else
2693 timestamp = 0;
2694
2695 if (rmdemux->base_ts != -1)
2696 timestamp += rmdemux->base_ts;
2697
2698 GST_BUFFER_PTS (buffer) = timestamp;
2699 GST_BUFFER_DTS (buffer) = timestamp;
2700
2701 if (stream->needs_descrambling) {
2702 GST_LOG_OBJECT (rmdemux, "descramble timestamp %" GST_TIME_FORMAT,
2703 GST_TIME_ARGS (timestamp));
2704 ret = gst_rmdemux_handle_scrambled_packet (rmdemux, stream, buffer, key);
2705 } else {
2706 GST_LOG_OBJECT (rmdemux,
2707 "Pushing buffer of size %" G_GSIZE_FORMAT ", timestamp %"
2708 GST_TIME_FORMAT "to pad %s", gst_buffer_get_size (buffer),
2709 GST_TIME_ARGS (timestamp), GST_PAD_NAME (stream->pad));
2710
2711 if (stream->discont) {
2712 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2713 stream->discont = FALSE;
2714 }
2715 ret = gst_pad_push (stream->pad, buffer);
2716 }
2717
2718 done:
2719 gst_buffer_unref (in);
2720
2721 return ret;
2722
2723 /* ERRORS */
2724 not_enough_data:
2725 {
2726 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2727 (NULL));
2728 ret = GST_FLOW_OK;
2729 goto done;
2730 }
2731 }
2732
2733 static GstFlowReturn
2734 gst_rmdemux_parse_packet (GstRMDemux * rmdemux, GstBuffer * in, guint16 version)
2735 {
2736 guint16 id;
2737 GstRMDemuxStream *stream;
2738 gsize size, offset;
2739 GstFlowReturn cret, ret;
2740 GstClockTime timestamp;
2741 gboolean key;
2742 GstMapInfo map;
2743 guint8 *data;
2744 guint8 flags;
2745 guint32 ts;
2746
2747 gst_buffer_map (in, &map, GST_MAP_READ);
2748 data = map.data;
2749 size = map.size;
2750
2751 if (size < 2 + 4 + 2)
2752 goto not_enough_data;
2753
2754 /* stream number */
2755 id = RMDEMUX_GUINT16_GET (data);
2756
2757 stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
2758 if (!stream || !stream->pad)
2759 goto unknown_stream;
2760
2761 /* timestamp in Msec */
2762 ts = RMDEMUX_GUINT32_GET (data + 2);
2763 timestamp = ts * GST_MSECOND;
2764
2765 rmdemux->segment.position = timestamp;
2766
2767 GST_LOG_OBJECT (rmdemux, "Parsing a packet for stream=%d, timestamp=%"
2768 GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT ", version=%d, ts=%u", id,
2769 GST_TIME_ARGS (timestamp), size, version, ts);
2770
2771 if (rmdemux->first_ts == GST_CLOCK_TIME_NONE) {
2772 GST_DEBUG_OBJECT (rmdemux, "First timestamp: %" GST_TIME_FORMAT,
2773 GST_TIME_ARGS (timestamp));
2774 rmdemux->first_ts = timestamp;
2775 }
2776
2777 /* skip stream_id and timestamp */
2778 data += (2 + 4);
2779 size -= (2 + 4);
2780
2781 /* get flags */
2782 flags = GST_READ_UINT8 (data + 1);
2783
2784 data += 2;
2785 size -= 2;
2786
2787 /* version 1 has an extra byte */
2788 if (version == 1) {
2789 if (size < 1)
2790 goto not_enough_data;
2791 data += 1;
2792 size -= 1;
2793 }
2794 offset = data - map.data;
2795 gst_buffer_unmap (in, &map);
2796
2797 key = (flags & 0x02) != 0;
2798 GST_DEBUG_OBJECT (rmdemux, "flags %d, Keyframe %d", flags, key);
2799
2800 if (rmdemux->need_newsegment) {
2801 GstEvent *event;
2802
2803 event = gst_event_new_segment (&rmdemux->segment);
2804
2805 GST_DEBUG_OBJECT (rmdemux, "sending NEWSEGMENT event, segment.start= %"
2806 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start));
2807
2808 gst_rmdemux_send_event (rmdemux, event);
2809 rmdemux->need_newsegment = FALSE;
2810
2811 if (rmdemux->pending_tags != NULL) {
2812 gst_rmdemux_send_event (rmdemux,
2813 gst_event_new_tag (rmdemux->pending_tags));
2814 rmdemux->pending_tags = NULL;
2815 }
2816 }
2817
2818 if (stream->pending_tags != NULL) {
2819 GST_LOG_OBJECT (stream->pad, "tags %" GST_PTR_FORMAT, stream->pending_tags);
2820 gst_pad_push_event (stream->pad, gst_event_new_tag (stream->pending_tags));
2821 stream->pending_tags = NULL;
2822 }
2823
2824 if ((rmdemux->offset + size) <= stream->seek_offset) {
2825 GST_DEBUG_OBJECT (rmdemux,
2826 "Stream %d is skipping: seek_offset=%d, offset=%d, size=%"
2827 G_GSIZE_FORMAT, stream->id, stream->seek_offset, rmdemux->offset, size);
2828 cret = GST_FLOW_OK;
2829 gst_buffer_unref (in);
2830 goto beach;
2831 }
2832
2833 /* do special headers */
2834 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
2835 ret =
2836 gst_rmdemux_parse_video_packet (rmdemux, stream, in, offset,
2837 version, timestamp, key);
2838 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
2839 ret =
2840 gst_rmdemux_parse_audio_packet (rmdemux, stream, in, offset,
2841 version, timestamp, key);
2842 } else {
2843 gst_buffer_unref (in);
2844 ret = GST_FLOW_OK;
2845 }
2846
2847 cret = gst_flow_combiner_update_pad_flow (rmdemux->flowcombiner, stream->pad,
2848 ret);
2849
2850 beach:
2851 return cret;
2852
2853 /* ERRORS */
2854 unknown_stream:
2855 {
2856 GST_WARNING_OBJECT (rmdemux, "No stream for stream id %d in parsing "
2857 "data packet", id);
2858 gst_buffer_unmap (in, &map);
2859 gst_buffer_unref (in);
2860 return GST_FLOW_OK;
2861 }
2862
2863 /* ERRORS */
2864 not_enough_data:
2865 {
2866 GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."),
2867 (NULL));
2868 gst_buffer_unmap (in, &map);
2869 gst_buffer_unref (in);
2870 return GST_FLOW_OK;
2871 }
2872 }