2 * This library is licensed under 2 different licenses and you
3 * can choose to use it under the terms of either one of them. The
4 * two licenses are the MPL 1.1 and the LGPL.
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.1 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/.
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15 * License for the specific language governing rights and limitations
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Library General Public
22 * License as published by the Free Software Foundation; either
23 * version 2 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Library General Public License for more details.
30 * You should have received a copy of the GNU Library General Public
31 * License along with this library; if not, write to the
32 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
33 * Boston, MA 02110-1301, USA.
35 * The Original Code is Fluendo MPEG Demuxer plugin.
37 * The Initial Developer of the Original Code is Fluendo, S.L.
38 * Portions created by Fluendo, S.L. are Copyright (C) 2005
39 * Fluendo, S.L. All Rights Reserved.
41 * Contributor(s): Wim Taymans <wim@fluendo.com>
42 * Jan Schmidt <thaytan@noraisin.net>
51 #include <gst/tag/tag.h>
52 #include <gst/pbutils/pbutils.h>
53 #include <gst/base/gstbytereader.h>
55 #include "gstmpegdefs.h"
56 #include "gstmpegdemux.h"
58 #define BLOCK_SZ 32768
59 #define SCAN_SCR_SZ 12
60 #define SCAN_PTS_SZ 80
62 #define SEGMENT_THRESHOLD (300*GST_MSECOND)
63 #define VIDEO_SEGMENT_THRESHOLD (500*GST_MSECOND)
65 #define DURATION_SCAN_LIMIT 4 * 1024 * 1024
74 /* We clamp scr delta with 0 so negative bytes won't be possible */
75 #define GSTTIME_TO_BYTES(time) \
76 ((time != -1) ? gst_util_uint64_scale (MAX(0,(gint64) (GSTTIME_TO_MPEGTIME(time))), demux->scr_rate_n, demux->scr_rate_d) : -1)
77 #define BYTES_TO_GSTTIME(bytes) ((bytes != -1) ? MPEGTIME_TO_GSTTIME(gst_util_uint64_scale (bytes, demux->scr_rate_d, demux->scr_rate_n)) : -1)
79 #define ADAPTER_OFFSET_FLUSH(_bytes_) demux->adapter_offset += (_bytes_)
81 GST_DEBUG_CATEGORY_STATIC (gstflupsdemux_debug);
82 #define GST_CAT_DEFAULT (gstflupsdemux_debug)
84 /* MPEG2Demux signals and args */
97 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
100 GST_STATIC_CAPS ("video/mpeg, "
101 "mpegversion = (int) { 1, 2 }, "
102 "systemstream = (boolean) TRUE;" "video/x-cdxa")
105 static GstStaticPadTemplate video_template =
106 GST_STATIC_PAD_TEMPLATE ("video_%02x",
109 GST_STATIC_CAPS ("video/mpeg, "
110 "mpegversion = (int) { 1, 2, 4 }, " "systemstream = (boolean) FALSE, "
111 "parsed = (boolean) FALSE; " "video/x-h264, "
112 "stream-format=(string)byte-stream; " "video/x-h265, "
113 "stream-format=(string)byte-stream;")
116 static GstStaticPadTemplate audio_template =
117 GST_STATIC_PAD_TEMPLATE ("audio_%02x",
120 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1;"
121 "audio/mpeg, mpegversion = (int) 4, stream-format = (string) { adts, loas };"
122 "audio/x-private1-lpcm; "
123 "audio/x-private1-ac3;" "audio/x-private1-dts;" "audio/ac3")
126 static GstStaticPadTemplate subpicture_template =
127 GST_STATIC_PAD_TEMPLATE ("subpicture_%02x",
130 GST_STATIC_CAPS ("subpicture/x-dvd")
133 static GstStaticPadTemplate private_template =
134 GST_STATIC_PAD_TEMPLATE ("private_%d",
137 GST_STATIC_CAPS_ANY);
139 static void gst_ps_demux_base_init (GstPsDemuxClass * klass);
140 static void gst_ps_demux_class_init (GstPsDemuxClass * klass);
141 static void gst_ps_demux_init (GstPsDemux * demux);
142 static void gst_ps_demux_finalize (GstPsDemux * demux);
143 static void gst_ps_demux_reset (GstPsDemux * demux);
145 static gboolean gst_ps_demux_sink_event (GstPad * pad, GstObject * parent,
147 static GstFlowReturn gst_ps_demux_chain (GstPad * pad, GstObject * parent,
149 static gboolean gst_ps_demux_sink_activate (GstPad * sinkpad,
151 static gboolean gst_ps_demux_sink_activate_mode (GstPad * pad,
152 GstObject * parent, GstPadMode mode, gboolean active);
153 static void gst_ps_demux_loop (GstPad * pad);
155 static gboolean gst_ps_demux_src_event (GstPad * pad, GstObject * parent,
157 static gboolean gst_ps_demux_src_query (GstPad * pad, GstObject * parent,
160 static GstStateChangeReturn gst_ps_demux_change_state (GstElement * element,
161 GstStateChange transition);
163 static inline gboolean gst_ps_demux_scan_forward_ts (GstPsDemux * demux,
164 guint64 * pos, SCAN_MODE mode, guint64 * rts, gint limit);
165 static inline gboolean gst_ps_demux_scan_backward_ts (GstPsDemux * demux,
166 guint64 * pos, SCAN_MODE mode, guint64 * rts, gint limit);
168 static inline void gst_ps_demux_send_gap_updates (GstPsDemux * demux,
169 GstClockTime new_time);
170 static inline void gst_ps_demux_clear_times (GstPsDemux * demux);
172 static void gst_ps_demux_reset_psm (GstPsDemux * demux);
173 static void gst_ps_demux_flush (GstPsDemux * demux);
175 static GstElementClass *parent_class = NULL;
177 static void gst_segment_set_position (GstSegment * segment, GstFormat format,
179 static void gst_segment_set_duration (GstSegment * segment, GstFormat format,
182 /*static guint gst_ps_demux_signals[LAST_SIGNAL] = { 0 };*/
185 gst_ps_demux_get_type (void)
187 static GType ps_demux_type = 0;
189 if (!ps_demux_type) {
190 static const GTypeInfo ps_demux_info = {
191 sizeof (GstPsDemuxClass),
192 (GBaseInitFunc) gst_ps_demux_base_init,
194 (GClassInitFunc) gst_ps_demux_class_init,
199 (GInstanceInitFunc) gst_ps_demux_init,
204 g_type_register_static (GST_TYPE_ELEMENT, "GstMpegPSDemux",
207 GST_DEBUG_CATEGORY_INIT (gstflupsdemux_debug, "mpegpsdemux", 0,
208 "MPEG program stream demultiplexer element");
211 return ps_demux_type;
215 gst_ps_demux_base_init (GstPsDemuxClass * klass)
217 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
219 klass->sink_template = gst_static_pad_template_get (&sink_template);
220 klass->video_template = gst_static_pad_template_get (&video_template);
221 klass->audio_template = gst_static_pad_template_get (&audio_template);
222 klass->subpicture_template =
223 gst_static_pad_template_get (&subpicture_template);
224 klass->private_template = gst_static_pad_template_get (&private_template);
226 gst_element_class_add_pad_template (element_class, klass->video_template);
227 gst_element_class_add_pad_template (element_class, klass->audio_template);
228 gst_element_class_add_pad_template (element_class,
229 klass->subpicture_template);
230 gst_element_class_add_pad_template (element_class, klass->private_template);
231 gst_element_class_add_pad_template (element_class, klass->sink_template);
233 gst_element_class_set_static_metadata (element_class,
234 "MPEG Program Stream Demuxer", "Codec/Demuxer",
235 "Demultiplexes MPEG Program Streams", "Wim Taymans <wim@fluendo.com>");
239 gst_ps_demux_class_init (GstPsDemuxClass * klass)
241 GObjectClass *gobject_class;
242 GstElementClass *gstelement_class;
244 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
246 gobject_class = (GObjectClass *) klass;
247 gstelement_class = (GstElementClass *) klass;
249 gobject_class->finalize = (GObjectFinalizeFunc) gst_ps_demux_finalize;
251 gstelement_class->change_state = gst_ps_demux_change_state;
255 gst_ps_demux_init (GstPsDemux * demux)
257 GstPsDemuxClass *klass = GST_PS_DEMUX_GET_CLASS (demux);
259 demux->sinkpad = gst_pad_new_from_template (klass->sink_template, "sink");
260 gst_pad_set_event_function (demux->sinkpad,
261 GST_DEBUG_FUNCPTR (gst_ps_demux_sink_event));
262 gst_pad_set_chain_function (demux->sinkpad,
263 GST_DEBUG_FUNCPTR (gst_ps_demux_chain));
264 gst_pad_set_activate_function (demux->sinkpad,
265 GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate));
266 gst_pad_set_activatemode_function (demux->sinkpad,
267 GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate_mode));
269 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
272 g_malloc0 (sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
273 demux->streams_found =
274 g_malloc0 (sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
275 demux->found_count = 0;
277 demux->adapter = gst_adapter_new ();
278 demux->rev_adapter = gst_adapter_new ();
279 demux->flowcombiner = gst_flow_combiner_new ();
281 gst_ps_demux_reset (demux);
285 gst_ps_demux_finalize (GstPsDemux * demux)
287 gst_ps_demux_reset (demux);
288 g_free (demux->streams);
289 g_free (demux->streams_found);
291 gst_flow_combiner_free (demux->flowcombiner);
292 g_object_unref (demux->adapter);
293 g_object_unref (demux->rev_adapter);
295 G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (demux));
299 gst_ps_demux_reset (GstPsDemux * demux)
301 /* Clean up the streams and pads we allocated */
304 for (i = 0; i < GST_PS_DEMUX_MAX_STREAMS; i++) {
305 GstPsStream *stream = demux->streams[i];
307 if (stream != NULL) {
308 if (stream->pad && GST_PAD_PARENT (stream->pad)) {
309 gst_flow_combiner_remove_pad (demux->flowcombiner, stream->pad);
310 gst_element_remove_pad (GST_ELEMENT_CAST (demux), stream->pad);
312 gst_object_unref (stream->pad);
315 if (stream->pending_tags)
316 gst_tag_list_unref (stream->pending_tags);
318 demux->streams[i] = NULL;
321 memset (demux->streams_found, 0,
322 sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
323 demux->found_count = 0;
325 gst_adapter_clear (demux->adapter);
326 gst_adapter_clear (demux->rev_adapter);
328 demux->adapter_offset = G_MAXUINT64;
329 demux->first_scr = G_MAXUINT64;
330 demux->last_scr = G_MAXUINT64;
331 demux->current_scr = G_MAXUINT64;
332 demux->base_time = G_MAXUINT64;
333 demux->scr_rate_n = G_MAXUINT64;
334 demux->scr_rate_d = G_MAXUINT64;
335 demux->first_pts = G_MAXUINT64;
336 demux->last_pts = G_MAXUINT64;
337 demux->mux_rate = G_MAXUINT64;
338 demux->next_pts = G_MAXUINT64;
339 demux->next_dts = G_MAXUINT64;
340 demux->need_no_more_pads = TRUE;
341 demux->adjust_segment = TRUE;
342 gst_ps_demux_reset_psm (demux);
343 gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
344 gst_segment_init (&demux->src_segment, GST_FORMAT_TIME);
345 gst_ps_demux_flush (demux);
346 demux->have_group_id = FALSE;
347 demux->group_id = G_MAXUINT;
351 gst_ps_demux_create_stream (GstPsDemux * demux, gint id, gint stream_type,
355 GstPadTemplate *template;
357 GstPsDemuxClass *klass = GST_PS_DEMUX_GET_CLASS (demux);
359 GstClockTime threshold = SEGMENT_THRESHOLD;
367 GST_DEBUG_OBJECT (demux, "create stream id 0x%02x, type 0x%02x", id,
370 switch (stream_type) {
374 case ST_GST_VIDEO_MPEG1_OR_2:
376 gint mpeg_version = 1;
377 if (stream_type == ST_VIDEO_MPEG2 ||
378 (stream_type == ST_GST_VIDEO_MPEG1_OR_2 && demux->is_mpeg2_pack)) {
381 if (stream_type == ST_VIDEO_MPEG4) {
385 template = klass->video_template;
386 name = g_strdup_printf ("video_%02x", id);
387 caps = gst_caps_new_simple ("video/mpeg",
388 "mpegversion", G_TYPE_INT, mpeg_version,
389 "systemstream", G_TYPE_BOOLEAN, FALSE,
390 "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
391 threshold = VIDEO_SEGMENT_THRESHOLD;
396 template = klass->audio_template;
397 name = g_strdup_printf ("audio_%02x", id);
399 caps = gst_caps_new_simple ("audio/mpeg",
400 "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer, NULL);
402 caps = gst_caps_new_simple ("audio/mpeg",
403 "mpegversion", G_TYPE_INT, 1, NULL);
406 case ST_PRIVATE_SECTIONS:
407 case ST_PRIVATE_DATA:
411 case ST_AUDIO_AAC_ADTS:
412 template = klass->audio_template;
413 name = g_strdup_printf ("audio_%02x", id);
414 caps = gst_caps_new_simple ("audio/mpeg",
415 "mpegversion", G_TYPE_INT, 4,
416 "stream-format", G_TYPE_STRING, "adts", NULL);
418 case ST_AUDIO_AAC_LOAS: // LATM/LOAS AAC syntax
419 template = klass->audio_template;
420 name = g_strdup_printf ("audio_%02x", id);
421 caps = gst_caps_new_simple ("audio/mpeg",
422 "mpegversion", G_TYPE_INT, 4,
423 "stream-format", G_TYPE_STRING, "loas", NULL);
426 template = klass->video_template;
427 name = g_strdup_printf ("video_%02x", id);
428 caps = gst_caps_new_simple ("video/x-h264",
429 "stream-format", G_TYPE_STRING, "byte-stream", NULL);
430 threshold = VIDEO_SEGMENT_THRESHOLD;
433 template = klass->video_template;
434 name = g_strdup_printf ("video_%02x", id);
435 caps = gst_caps_new_simple ("video/x-h265",
436 "stream-format", G_TYPE_STRING, "byte-stream", NULL);
437 threshold = VIDEO_SEGMENT_THRESHOLD;
440 case ST_PS_AUDIO_AC3:
441 template = klass->audio_template;
442 name = g_strdup_printf ("audio_%02x", id);
443 caps = gst_caps_new_empty_simple ("audio/x-private1-ac3");
445 case ST_PS_AUDIO_DTS:
446 template = klass->audio_template;
447 name = g_strdup_printf ("audio_%02x", id);
448 caps = gst_caps_new_empty_simple ("audio/x-private1-dts");
450 case ST_PS_AUDIO_LPCM:
451 template = klass->audio_template;
452 name = g_strdup_printf ("audio_%02x", id);
453 caps = gst_caps_new_empty_simple ("audio/x-private1-lpcm");
455 case ST_PS_DVD_SUBPICTURE:
456 template = klass->subpicture_template;
457 name = g_strdup_printf ("subpicture_%02x", id);
458 caps = gst_caps_new_empty_simple ("subpicture/x-dvd");
460 case ST_GST_AUDIO_RAWA52:
461 template = klass->audio_template;
462 name = g_strdup_printf ("audio_%02x", id);
463 caps = gst_caps_new_empty_simple ("audio/ac3");
469 if (name == NULL || template == NULL || caps == NULL) {
472 gst_caps_unref (caps);
476 stream = g_new0 (GstPsStream, 1);
478 stream->discont = TRUE;
479 stream->need_segment = TRUE;
480 stream->notlinked = FALSE;
481 stream->type = stream_type;
482 stream->pending_tags = NULL;
483 stream->pad = gst_pad_new_from_template (template, name);
484 stream->segment_thresh = threshold;
485 gst_pad_set_event_function (stream->pad,
486 GST_DEBUG_FUNCPTR (gst_ps_demux_src_event));
487 gst_pad_set_query_function (stream->pad,
488 GST_DEBUG_FUNCPTR (gst_ps_demux_src_query));
489 gst_pad_use_fixed_caps (stream->pad);
491 /* needed for set_caps to work */
492 if (!gst_pad_set_active (stream->pad, TRUE)) {
493 GST_WARNING_OBJECT (demux, "Failed to activate pad %" GST_PTR_FORMAT,
498 gst_pad_create_stream_id_printf (stream->pad, GST_ELEMENT_CAST (demux),
501 event = gst_pad_get_sticky_event (demux->sinkpad, GST_EVENT_STREAM_START, 0);
503 if (gst_event_parse_group_id (event, &demux->group_id))
504 demux->have_group_id = TRUE;
506 demux->have_group_id = FALSE;
507 gst_event_unref (event);
508 } else if (!demux->have_group_id) {
509 demux->have_group_id = TRUE;
510 demux->group_id = gst_util_group_id_next ();
512 event = gst_event_new_stream_start (stream_id);
513 if (demux->have_group_id)
514 gst_event_set_group_id (event, demux->group_id);
516 gst_pad_push_event (stream->pad, event);
519 gst_pad_set_caps (stream->pad, caps);
521 if (!stream->pending_tags)
522 stream->pending_tags = gst_tag_list_new_empty ();
523 gst_pb_utils_add_codec_description_to_tag_list (stream->pending_tags, NULL,
526 GST_DEBUG_OBJECT (demux, "create pad %s, caps %" GST_PTR_FORMAT, name, caps);
527 gst_caps_unref (caps);
534 gst_ps_demux_get_stream (GstPsDemux * demux, gint id, gint type, gint layer)
536 GstPsStream *stream = demux->streams[id];
538 if (stream == NULL) {
539 if (!(stream = gst_ps_demux_create_stream (demux, id, type, layer)))
542 GST_DEBUG_OBJECT (demux, "adding pad for stream id 0x%02x type 0x%02x", id,
545 demux->streams[id] = stream;
546 demux->streams_found[demux->found_count++] = stream;
548 if (demux->need_no_more_pads) {
549 gst_element_add_pad (GST_ELEMENT (demux), stream->pad);
550 gst_flow_combiner_add_pad (demux->flowcombiner, stream->pad);
552 /* only likely to confuse decodebin etc, so discard */
553 /* FIXME should perform full switch protocol:
554 * add a whole new set of pads, drop old and no-more-pads again */
555 GST_DEBUG_OBJECT (demux,
556 "but already signalled no-more-pads; not adding");
557 gst_object_ref_sink (stream->pad);
565 GST_DEBUG_OBJECT (demux, "unknown stream id 0x%02x type 0x%02x", id, type);
571 gst_ps_demux_get_stream_from_pad (GstPsDemux * demux, GstPad * srcpad)
575 count = demux->found_count;
576 for (i = 0; i < count; i++) {
577 GstPsStream *stream = demux->streams_found[i];
579 if (stream && stream->pad == srcpad)
583 GST_DEBUG_OBJECT (srcpad, "no stream found for pad!");
588 gst_ps_demux_send_segment (GstPsDemux * demux, GstPsStream * stream,
592 if (G_UNLIKELY (stream->need_segment)) {
595 GST_DEBUG ("PTS timestamp:%" GST_TIME_FORMAT " base_time %" GST_TIME_FORMAT
596 " src_segment.start:%" GST_TIME_FORMAT " .stop:%" GST_TIME_FORMAT,
597 GST_TIME_ARGS (pts), GST_TIME_ARGS (demux->base_time),
598 GST_TIME_ARGS (demux->src_segment.start),
599 GST_TIME_ARGS (demux->src_segment.stop));
601 /* adjust segment start if estimating a seek was off quite a bit,
602 * make sure to do for all streams though to preserve a/v sync */
603 /* FIXME such adjustment tends to be frowned upon */
604 if (pts != GST_CLOCK_TIME_NONE && demux->adjust_segment) {
605 if (demux->src_segment.rate > 0) {
606 if (GST_CLOCK_DIFF (demux->src_segment.start, pts) > GST_SECOND)
607 demux->src_segment.start = pts - demux->base_time;
609 if (GST_CLOCK_DIFF (demux->src_segment.stop, pts) > GST_SECOND)
610 demux->src_segment.stop = pts - demux->base_time;
613 demux->adjust_segment = FALSE;
615 /* we should be in sync with downstream, so start from our segment notion,
616 * which also includes proper base_time etc, tweak it a bit and send */
617 gst_segment_copy_into (&demux->src_segment, &segment);
618 if (GST_CLOCK_TIME_IS_VALID (demux->base_time)) {
619 if (GST_CLOCK_TIME_IS_VALID (segment.start))
620 segment.start += demux->base_time;
621 if (GST_CLOCK_TIME_IS_VALID (segment.stop))
622 segment.stop += demux->base_time;
623 segment.time = segment.start - demux->base_time;
626 GST_INFO_OBJECT (demux, "sending segment event %" GST_SEGMENT_FORMAT
627 " to pad %" GST_PTR_FORMAT, &segment, stream->pad);
629 gst_pad_push_event (stream->pad, gst_event_new_segment (&segment));
631 stream->need_segment = FALSE;
634 if (G_UNLIKELY (stream->pending_tags)) {
635 GST_DEBUG_OBJECT (demux, "Sending pending_tags %p for pad %s:%s : %"
636 GST_PTR_FORMAT, stream->pending_tags,
637 GST_DEBUG_PAD_NAME (stream->pad), stream->pending_tags);
638 gst_pad_push_event (stream->pad, gst_event_new_tag (stream->pending_tags));
639 stream->pending_tags = NULL;
644 gst_ps_demux_send_data (GstPsDemux * demux, GstPsStream * stream,
647 GstFlowReturn result;
648 GstClockTime pts = GST_CLOCK_TIME_NONE, dts = GST_CLOCK_TIME_NONE;
655 if (G_UNLIKELY (demux->next_pts != G_MAXUINT64))
656 pts = MPEGTIME_TO_GSTTIME (demux->next_pts);
657 if (G_UNLIKELY (demux->next_dts != G_MAXUINT64))
658 dts = MPEGTIME_TO_GSTTIME (demux->next_dts);
660 gst_ps_demux_send_segment (demux, stream, pts);
662 /* OK, sent new segment now prepare the buffer for sending */
663 GST_BUFFER_PTS (buf) = pts;
664 GST_BUFFER_DTS (buf) = dts;
666 /* If we have no DTS but a PTS that means both are the same,
667 * if we have neither than we don't know the current position */
669 if (ts == GST_CLOCK_TIME_NONE)
672 /* update position in the segment */
673 if (ts != GST_CLOCK_TIME_NONE && (stream->last_ts == GST_CLOCK_TIME_NONE
674 || stream->last_ts < ts)) {
675 GST_LOG_OBJECT (demux,
676 "last_ts update on pad %s to time %" GST_TIME_FORMAT
677 ", current scr is %" GST_TIME_FORMAT, GST_PAD_NAME (stream->pad),
679 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->current_scr)));
680 stream->last_ts = ts;
681 if (demux->src_segment.position == GST_CLOCK_TIME_NONE
682 || stream->last_ts > demux->src_segment.position)
683 gst_segment_set_position (&demux->src_segment, GST_FORMAT_TIME,
687 /* Set the buffer discont flag, and clear discont state on the stream */
688 if (stream->discont) {
689 GST_DEBUG_OBJECT (demux, "discont buffer to pad %" GST_PTR_FORMAT
690 " with PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT,
691 stream->pad, GST_TIME_ARGS (pts), GST_TIME_ARGS (dts));
692 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
694 stream->discont = FALSE;
696 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
699 demux->next_pts = G_MAXUINT64;
700 demux->next_dts = G_MAXUINT64;
702 GST_LOG_OBJECT (demux, "pushing stream id 0x%02x type 0x%02x, pts time: %"
703 GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT,
704 stream->id, stream->type, GST_TIME_ARGS (pts), gst_buffer_get_size (buf));
705 result = gst_pad_push (stream->pad, buf);
706 GST_LOG_OBJECT (demux, "result: %s", gst_flow_get_name (result));
713 GST_DEBUG_OBJECT (demux, "no stream given");
714 gst_buffer_unref (buf);
720 gst_ps_demux_mark_discont (GstPsDemux * demux, gboolean discont,
721 gboolean need_segment)
723 gint i, count = demux->found_count;
725 /* mark discont on all streams */
726 for (i = 0; i < count; i++) {
727 GstPsStream *stream = demux->streams_found[i];
729 if (G_LIKELY (stream)) {
730 stream->discont |= discont;
731 stream->need_segment |= need_segment;
732 demux->adjust_segment |= need_segment;
733 GST_DEBUG_OBJECT (demux, "marked stream as discont %d, need_segment %d",
734 stream->discont, stream->need_segment);
740 gst_ps_demux_send_event (GstPsDemux * demux, GstEvent * event)
742 gint i, count = demux->found_count;
743 gboolean ret = FALSE;
745 for (i = 0; i < count; i++) {
746 GstPsStream *stream = demux->streams_found[i];
749 if (!gst_pad_push_event (stream->pad, gst_event_ref (event))) {
750 GST_DEBUG_OBJECT (stream->pad, "%s event was not handled",
751 GST_EVENT_TYPE_NAME (event));
753 /* If at least one push returns TRUE, then we return TRUE. */
754 GST_DEBUG_OBJECT (stream->pad, "%s event was handled",
755 GST_EVENT_TYPE_NAME (event));
761 gst_event_unref (event);
766 gst_ps_demux_handle_dvd_event (GstPsDemux * demux, GstEvent * event)
768 const GstStructure *structure = gst_event_get_structure (event);
769 const char *type = gst_structure_get_string (structure, "event");
771 gchar cur_stream_name[32];
772 GstPsStream *temp = NULL;
773 const gchar *lang_code;
775 if (strcmp (type, "dvd-lang-codes") == 0) {
776 GST_DEBUG_OBJECT (demux, "Handling language codes event");
778 /* Create a video pad to ensure have it before emit no more pads */
779 (void) gst_ps_demux_get_stream (demux, 0xe0, ST_VIDEO_MPEG2, 0);
781 /* Read out the languages for audio streams and request each one that
783 for (i = 0; i < MAX_DVD_AUDIO_STREAMS; i++) {
787 g_snprintf (cur_stream_name, 32, "audio-%d-format", i);
788 if (!gst_structure_get_int (structure, cur_stream_name, &stream_format))
791 g_snprintf (cur_stream_name, 32, "audio-%d-stream", i);
792 if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
794 if (stream_id < 0 || stream_id >= MAX_DVD_AUDIO_STREAMS)
797 switch (stream_format) {
801 GST_DEBUG_OBJECT (demux,
802 "Audio stream %d format %d ID 0x%02x - AC3", i,
803 stream_format, stream_id);
804 temp = gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_AC3, 0);
808 /* MPEG audio without and with extension stream are
809 * treated the same */
811 GST_DEBUG_OBJECT (demux,
812 "Audio stream %d format %d ID 0x%02x - MPEG audio", i,
813 stream_format, stream_id);
814 temp = gst_ps_demux_get_stream (demux, stream_id, ST_AUDIO_MPEG1, 0);
819 GST_DEBUG_OBJECT (demux,
820 "Audio stream %d format %d ID 0x%02x - DVD LPCM", i,
821 stream_format, stream_id);
823 gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_LPCM, 0);
828 GST_DEBUG_OBJECT (demux,
829 "Audio stream %d format %d ID 0x%02x - DTS", i,
830 stream_format, stream_id);
831 temp = gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_DTS, 0);
834 /* FIXME: What range is SDDS? */
836 GST_WARNING_OBJECT (demux,
837 "Unknown audio stream format in language code event: %d",
846 g_snprintf (cur_stream_name, 32, "audio-%d-language", i);
847 lang_code = gst_structure_get_string (structure, cur_stream_name);
849 GstTagList *list = temp->pending_tags;
852 list = gst_tag_list_new_empty ();
853 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
854 GST_TAG_LANGUAGE_CODE, lang_code, NULL);
855 temp->pending_tags = list;
859 /* And subtitle streams */
860 for (i = 0; i < MAX_DVD_SUBPICTURE_STREAMS; i++) {
863 g_snprintf (cur_stream_name, 32, "subpicture-%d-format", i);
864 if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
867 g_snprintf (cur_stream_name, 32, "subpicture-%d-stream", i);
868 if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
870 if (stream_id < 0 || stream_id >= MAX_DVD_SUBPICTURE_STREAMS)
873 GST_DEBUG_OBJECT (demux, "Subpicture stream %d ID 0x%02x", i,
876 /* Retrieve the subpicture stream to force pad creation */
877 temp = gst_ps_demux_get_stream (demux, 0x20 + stream_id,
878 ST_PS_DVD_SUBPICTURE, 0);
882 g_snprintf (cur_stream_name, 32, "subpicture-%d-language", i);
883 lang_code = gst_structure_get_string (structure, cur_stream_name);
885 GstTagList *list = temp->pending_tags;
888 list = gst_tag_list_new_empty ();
889 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
890 GST_TAG_LANGUAGE_CODE, lang_code, NULL);
891 temp->pending_tags = list;
895 GST_DEBUG_OBJECT (demux, "Created all pads from Language Codes event, "
896 "signalling no-more-pads");
898 gst_element_no_more_pads (GST_ELEMENT (demux));
899 demux->need_no_more_pads = FALSE;
901 /* forward to all pads, e.g. dvd clut event */
902 gst_event_ref (event);
903 gst_ps_demux_send_event (demux, event);
906 gst_event_unref (event);
911 gst_ps_demux_flush (GstPsDemux * demux)
913 GST_DEBUG_OBJECT (demux, "flushing demuxer");
914 gst_adapter_clear (demux->adapter);
915 gst_adapter_clear (demux->rev_adapter);
916 gst_pes_filter_drain (&demux->filter);
917 gst_ps_demux_clear_times (demux);
918 demux->adapter_offset = G_MAXUINT64;
919 demux->current_scr = G_MAXUINT64;
920 demux->bytes_since_scr = 0;
924 gst_ps_demux_clear_times (GstPsDemux * demux)
926 gint i, count = demux->found_count;
928 gst_flow_combiner_reset (demux->flowcombiner);
929 /* Clear the last ts for all streams */
930 for (i = 0; i < count; i++) {
931 GstPsStream *stream = demux->streams_found[i];
933 if (G_LIKELY (stream)) {
934 stream->last_ts = GST_CLOCK_TIME_NONE;
940 gst_ps_demux_send_gap_updates (GstPsDemux * demux, GstClockTime new_start)
942 GstClockTime base_time, stop;
943 gint i, count = demux->found_count;
944 GstEvent *event = NULL;
946 if (new_start == GST_CLOCK_TIME_NONE)
949 /* Advance all lagging streams by sending a gap event */
950 if ((base_time = demux->base_time) == GST_CLOCK_TIME_NONE)
953 stop = demux->src_segment.stop;
954 if (stop != GST_CLOCK_TIME_NONE)
957 if (new_start > stop)
960 /* FIXME: Handle reverse playback */
961 for (i = 0; i < count; i++) {
962 GstPsStream *stream = demux->streams_found[i];
965 if (stream->last_ts == GST_CLOCK_TIME_NONE ||
966 stream->last_ts < demux->src_segment.start + base_time)
967 stream->last_ts = demux->src_segment.start + base_time;
969 if (stream->last_ts + stream->segment_thresh < new_start) {
970 /* should send segment info before gap event */
971 gst_ps_demux_send_segment (demux, stream, GST_CLOCK_TIME_NONE);
973 GST_LOG_OBJECT (demux,
974 "Sending gap update to pad %s from time %" GST_TIME_FORMAT " to %"
975 GST_TIME_FORMAT, GST_PAD_NAME (stream->pad),
976 GST_TIME_ARGS (stream->last_ts), GST_TIME_ARGS (new_start));
978 gst_event_new_gap (stream->last_ts, new_start - stream->last_ts);
979 gst_pad_push_event (stream->pad, event);
980 stream->last_ts = new_start;
986 static inline gboolean
987 have_open_streams (GstPsDemux * demux)
989 return (demux->streams_found[0] != NULL);
993 gst_ps_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
996 GstPsDemux *demux = GST_PS_DEMUX (parent);
998 switch (GST_EVENT_TYPE (event)) {
999 case GST_EVENT_FLUSH_START:
1000 gst_ps_demux_send_event (demux, event);
1002 case GST_EVENT_FLUSH_STOP:
1003 gst_ps_demux_send_event (demux, event);
1004 gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
1005 gst_ps_demux_flush (demux);
1007 case GST_EVENT_SEGMENT:
1009 const GstSegment *segment;
1011 gst_event_parse_segment (event, &segment);
1012 gst_segment_copy_into (segment, &demux->sink_segment);
1014 GST_INFO_OBJECT (demux, "received segment %" GST_SEGMENT_FORMAT, segment);
1016 /* we need to emit a new segment */
1017 gst_ps_demux_mark_discont (demux, TRUE, TRUE);
1019 if (segment->format == GST_FORMAT_BYTES
1020 && demux->scr_rate_n != G_MAXUINT64
1021 && demux->scr_rate_d != G_MAXUINT64) {
1022 demux->src_segment.rate = segment->rate;
1023 demux->src_segment.applied_rate = segment->applied_rate;
1024 demux->src_segment.format = GST_FORMAT_TIME;
1025 demux->src_segment.start = BYTES_TO_GSTTIME (segment->start);
1026 demux->src_segment.stop = BYTES_TO_GSTTIME (segment->stop);
1027 demux->src_segment.time = BYTES_TO_GSTTIME (segment->time);
1028 } else if (segment->format == GST_FORMAT_TIME) {
1029 /* we expect our timeline (SCR, PTS) to match the one from upstream,
1030 * if not, will adjust with offset later on */
1031 gst_segment_copy_into (segment, &demux->src_segment);
1032 /* accept upstream segment without adjusting */
1033 demux->adjust_segment = FALSE;
1036 gst_event_unref (event);
1041 GST_INFO_OBJECT (demux, "Received EOS");
1042 if (!gst_ps_demux_send_event (demux, event)
1043 && !have_open_streams (demux)) {
1044 GST_WARNING_OBJECT (demux, "EOS and no streams open");
1045 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
1046 ("Internal data stream error."), ("No valid streams detected"));
1049 case GST_EVENT_CUSTOM_DOWNSTREAM:
1050 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1052 const GstStructure *structure = gst_event_get_structure (event);
1054 if (structure != NULL
1055 && gst_structure_has_name (structure, "application/x-gst-dvd")) {
1056 res = gst_ps_demux_handle_dvd_event (demux, event);
1058 gst_ps_demux_send_event (demux, event);
1062 case GST_EVENT_CAPS:
1063 gst_event_unref (event);
1066 gst_ps_demux_send_event (demux, event);
1074 gst_ps_demux_handle_seek_push (GstPsDemux * demux, GstEvent * event)
1076 gboolean res = FALSE;
1080 GstSeekType start_type, stop_type;
1082 gint64 bstart, bstop;
1085 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
1088 GST_DEBUG_OBJECT (demux, "seek event, rate: %f start: %" GST_TIME_FORMAT
1089 " stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
1090 GST_TIME_ARGS (stop));
1092 if (format == GST_FORMAT_BYTES) {
1093 GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format);
1097 GST_DEBUG_OBJECT (demux, "seek - trying directly upstream first");
1099 /* first try original format seek */
1100 (void) gst_event_ref (event);
1101 if ((res = gst_pad_push_event (demux->sinkpad, event)))
1104 if (format != GST_FORMAT_TIME) {
1105 /* From here down, we only support time based seeks */
1106 GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format);
1110 /* We need to convert to byte based seek and we need a scr_rate for that. */
1111 if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64) {
1112 GST_DEBUG_OBJECT (demux, "seek not possible, no scr_rate");
1116 GST_DEBUG_OBJECT (demux, "try with scr_rate interpolation");
1118 bstart = GSTTIME_TO_BYTES ((guint64) start);
1119 bstop = GSTTIME_TO_BYTES ((guint64) stop);
1121 GST_DEBUG_OBJECT (demux, "in bytes bstart %" G_GINT64_FORMAT " bstop %"
1122 G_GINT64_FORMAT, bstart, bstop);
1123 bevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, start_type,
1124 bstart, stop_type, bstop);
1126 res = gst_pad_push_event (demux->sinkpad, bevent);
1129 gst_event_unref (event);
1134 gst_event_unref (event);
1140 #define MAX_RECURSION_COUNT 100
1142 /* Binary search for requested SCR */
1143 static inline guint64
1144 find_offset (GstPsDemux * demux, guint64 scr,
1145 guint64 min_scr, guint64 min_scr_offset,
1146 guint64 max_scr, guint64 max_scr_offset, int recursion_count)
1148 guint64 scr_rate_n = max_scr_offset - min_scr_offset;
1149 guint64 scr_rate_d = max_scr - min_scr;
1153 if (recursion_count > MAX_RECURSION_COUNT) {
1157 offset = min_scr_offset +
1158 MIN (gst_util_uint64_scale (scr - min_scr, scr_rate_n,
1159 scr_rate_d), demux->sink_segment.stop);
1161 if (!gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0)) {
1162 gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1165 if (fscr == scr || fscr == min_scr || fscr == max_scr) {
1170 return find_offset (demux, scr, fscr, offset, max_scr, max_scr_offset,
1171 recursion_count + 1);
1173 return find_offset (demux, scr, min_scr, min_scr_offset, fscr, offset,
1174 recursion_count + 1);
1178 static inline gboolean
1179 gst_ps_demux_do_seek (GstPsDemux * demux, GstSegment * seeksegment)
1182 guint64 fscr, offset;
1183 guint64 scr = GSTTIME_TO_MPEGTIME (seeksegment->position + demux->base_time);
1185 /* In some clips the PTS values are completely unaligned with SCR values.
1186 * To improve the seek in that situation we apply a factor considering the
1187 * relationship between last PTS and last SCR */
1188 if (demux->last_scr > demux->last_pts)
1189 scr = gst_util_uint64_scale (scr, demux->last_scr, demux->last_pts);
1191 scr = MIN (demux->last_scr, scr);
1192 scr = MAX (demux->first_scr, scr);
1195 GST_INFO_OBJECT (demux, "sink segment configured %" GST_SEGMENT_FORMAT
1196 ", trying to go at SCR: %" G_GUINT64_FORMAT, &demux->sink_segment, scr);
1199 find_offset (demux, scr, demux->first_scr, demux->first_scr_offset,
1200 demux->last_scr, demux->last_scr_offset, 0);
1202 if (offset == (guint64) - 1) {
1206 found = gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1208 found = gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1210 while (found && fscr < scr) {
1212 found = gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1215 while (found && fscr > scr && offset > 0) {
1217 found = gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1220 GST_INFO_OBJECT (demux, "doing seek at offset %" G_GUINT64_FORMAT
1221 " SCR: %" G_GUINT64_FORMAT " %" GST_TIME_FORMAT,
1222 offset, fscr, GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (fscr)));
1224 gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
1230 gst_ps_demux_handle_seek_pull (GstPsDemux * demux, GstEvent * event)
1234 GstSeekType start_type, stop_type;
1237 gboolean update, flush;
1238 GstSegment seeksegment;
1239 GstClockTime first_pts = MPEGTIME_TO_GSTTIME (demux->first_pts);
1241 gst_event_parse_seek (event, &rate, &format, &flags,
1242 &start_type, &start, &stop_type, &stop);
1244 if (format != GST_FORMAT_TIME)
1247 GST_DEBUG_OBJECT (demux, "Seek requested start %" GST_TIME_FORMAT " stop %"
1248 GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1250 /* We need to convert to byte based seek and we need a scr_rate for that. */
1251 if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64)
1254 flush = flags & GST_SEEK_FLAG_FLUSH;
1255 /* keyframe = flags & GST_SEEK_FLAG_KEY_UNIT; *//* FIXME */
1258 /* Flush start up and downstream to make sure data flow and loops are
1260 demux->flushing = TRUE;
1261 gst_ps_demux_send_event (demux, gst_event_new_flush_start ());
1262 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_start ());
1264 /* Pause the pulling task */
1265 gst_pad_pause_task (demux->sinkpad);
1268 /* Take the stream lock */
1269 GST_PAD_STREAM_LOCK (demux->sinkpad);
1272 /* Stop flushing upstream we need to pull */
1273 demux->flushing = FALSE;
1274 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_stop (TRUE));
1277 /* Work on a copy until we are sure the seek succeeded. */
1278 memcpy (&seeksegment, &demux->src_segment, sizeof (GstSegment));
1280 GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
1281 &demux->src_segment);
1283 /* Apply the seek to our segment */
1284 if (!gst_segment_do_seek (&seeksegment, rate, format, flags,
1285 start_type, start, stop_type, stop, &update))
1288 GST_DEBUG_OBJECT (demux, "seek segment configured %" GST_SEGMENT_FORMAT,
1291 if (flush || seeksegment.position != demux->src_segment.position) {
1292 /* Do the actual seeking */
1293 if (!gst_ps_demux_do_seek (demux, &seeksegment)) {
1298 /* check the limits */
1299 if (seeksegment.rate > 0.0 && first_pts != G_MAXUINT64) {
1300 if (seeksegment.start < first_pts - demux->base_time) {
1301 seeksegment.start = first_pts - demux->base_time;
1302 seeksegment.position = seeksegment.start;
1306 /* update the rate in our src segment */
1307 demux->sink_segment.rate = rate;
1309 GST_DEBUG_OBJECT (demux, "seek segment adjusted %" GST_SEGMENT_FORMAT,
1313 /* Stop flushing, the sinks are at time 0 now */
1314 gst_ps_demux_send_event (demux, gst_event_new_flush_stop (TRUE));
1317 if (flush || seeksegment.position != demux->src_segment.position) {
1318 gst_ps_demux_flush (demux);
1321 /* Ok seek succeeded, take the newly configured segment */
1322 memcpy (&demux->src_segment, &seeksegment, sizeof (GstSegment));
1324 /* Notify about the start of a new segment */
1325 if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
1326 gst_element_post_message (GST_ELEMENT (demux),
1327 gst_message_new_segment_start (GST_OBJECT (demux),
1328 demux->src_segment.format, demux->src_segment.position));
1331 /* Tell all the stream a new segment is needed */
1332 gst_ps_demux_mark_discont (demux, TRUE, TRUE);
1334 gst_pad_start_task (demux->sinkpad,
1335 (GstTaskFunction) gst_ps_demux_loop, demux->sinkpad, NULL);
1337 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
1339 gst_event_unref (event);
1345 GST_WARNING_OBJECT (demux, "we only support seeking in TIME or BYTES "
1347 gst_event_unref (event);
1352 GST_WARNING_OBJECT (demux, "seek not possible, no scr_rate");
1353 gst_event_unref (event);
1358 GST_WARNING_OBJECT (demux, "couldn't perform seek");
1359 gst_event_unref (event);
1365 gst_ps_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1367 gboolean res = FALSE;
1368 GstPsDemux *demux = GST_PS_DEMUX (parent);
1370 switch (GST_EVENT_TYPE (event)) {
1371 case GST_EVENT_SEEK:
1372 if (demux->random_access) {
1373 res = gst_ps_demux_handle_seek_pull (demux, event);
1375 res = gst_ps_demux_handle_seek_push (demux, event);
1378 case GST_EVENT_RECONFIGURE:{
1379 GstPsStream *stream;
1381 stream = gst_ps_demux_get_stream_from_pad (demux, pad);
1383 stream->notlinked = FALSE;
1385 gst_event_unref (event);
1390 res = gst_pad_push_event (demux->sinkpad, event);
1398 gst_ps_demux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1400 gboolean res = FALSE;
1401 GstPsDemux *demux = GST_PS_DEMUX (parent);
1403 GST_LOG_OBJECT (demux, "Have query of type %d on pad %" GST_PTR_FORMAT,
1404 GST_QUERY_TYPE (query), pad);
1406 switch (GST_QUERY_TYPE (query)) {
1407 case GST_QUERY_POSITION:
1412 /* See if upstream can immediately answer */
1413 res = gst_pad_peer_query (demux->sinkpad, query);
1417 gst_query_parse_position (query, &format, NULL);
1419 if (format != GST_FORMAT_TIME) {
1420 GST_DEBUG_OBJECT (demux, "position not supported for format: %s",
1421 gst_format_get_name (format));
1425 pos = demux->src_segment.position - demux->src_segment.start;
1426 GST_LOG_OBJECT (demux, "Position %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
1428 gst_query_set_position (query, format, pos);
1432 case GST_QUERY_DURATION:
1436 GstQuery *byte_query;
1438 gst_query_parse_duration (query, &format, NULL);
1440 if (G_LIKELY (format == GST_FORMAT_TIME &&
1441 GST_CLOCK_TIME_IS_VALID (demux->src_segment.duration))) {
1442 gst_query_set_duration (query, GST_FORMAT_TIME,
1443 demux->src_segment.duration);
1448 /* For any format other than bytes, see if upstream knows first */
1449 if (format == GST_FORMAT_BYTES) {
1450 GST_DEBUG_OBJECT (demux, "duration not supported for format: %s",
1451 gst_format_get_name (format));
1455 if (gst_pad_peer_query (demux->sinkpad, query)) {
1460 /* Upstream didn't know, so we can only answer TIME queries from
1462 if (format != GST_FORMAT_TIME) {
1463 GST_DEBUG_OBJECT (demux, "duration not supported for format: %s",
1464 gst_format_get_name (format));
1468 if (demux->mux_rate == -1) {
1469 GST_DEBUG_OBJECT (demux, "duration not possible, no mux_rate");
1473 byte_query = gst_query_new_duration (GST_FORMAT_BYTES);
1475 if (!gst_pad_peer_query (demux->sinkpad, byte_query)) {
1476 GST_LOG_OBJECT (demux, "query on peer pad failed");
1477 gst_query_unref (byte_query);
1481 gst_query_parse_duration (byte_query, &format, &duration);
1482 gst_query_unref (byte_query);
1484 GST_LOG_OBJECT (demux,
1485 "query on peer pad reported bytes %" G_GUINT64_FORMAT, duration);
1487 duration = BYTES_TO_GSTTIME ((guint64) duration);
1489 GST_LOG_OBJECT (demux, "converted to time %" GST_TIME_FORMAT,
1490 GST_TIME_ARGS (duration));
1492 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
1496 case GST_QUERY_SEEKING:{
1499 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
1502 if (demux->random_access) {
1503 /* In pull mode we can seek in TIME format if we have the SCR */
1504 if (fmt != GST_FORMAT_TIME || demux->scr_rate_n == G_MAXUINT64
1505 || demux->scr_rate_d == G_MAXUINT64)
1506 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1508 gst_query_set_seeking (query, fmt, TRUE, 0, -1);
1510 if (fmt == GST_FORMAT_BYTES) {
1511 /* Seeking in BYTES format not supported at all */
1512 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1514 GstQuery *peerquery;
1517 /* Then ask upstream */
1518 res = gst_pad_peer_query (demux->sinkpad, query);
1520 /* If upstream can handle seeks we're done, if it
1521 * can't we still have our TIME->BYTES conversion seek
1523 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
1524 if (seekable || fmt != GST_FORMAT_TIME)
1528 /* We can seek if upstream supports BYTES seeks and we
1531 peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
1532 res = gst_pad_peer_query (demux->sinkpad, peerquery);
1533 if (!res || demux->scr_rate_n == G_MAXUINT64
1534 || demux->scr_rate_d == G_MAXUINT64) {
1535 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1537 gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
1539 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, -1);
1541 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1544 gst_query_unref (peerquery);
1550 case GST_QUERY_SEGMENT:{
1554 format = demux->src_segment.format;
1557 gst_segment_to_stream_time (&demux->src_segment, format,
1558 demux->src_segment.start);
1559 if ((stop = demux->src_segment.stop) == -1)
1560 stop = demux->src_segment.duration;
1562 stop = gst_segment_to_stream_time (&demux->src_segment, format, stop);
1564 gst_query_set_segment (query, demux->src_segment.rate, format, start,
1570 res = gst_pad_query_default (pad, parent, query);
1581 gst_ps_demux_reset_psm (GstPsDemux * demux)
1585 #define FILL_TYPE(start, stop, type) \
1586 for (i=start; i <= stop; i++) \
1587 demux->psm[i] = type;
1589 /* Initialize all fields to -1 first */
1590 FILL_TYPE (0x00, GST_PS_DEMUX_MAX_PSM - 1, -1);
1592 FILL_TYPE (0x20, 0x3f, ST_PS_DVD_SUBPICTURE);
1594 FILL_TYPE (0x80, 0x87, ST_PS_AUDIO_AC3);
1595 FILL_TYPE (0x88, 0x9f, ST_PS_AUDIO_DTS);
1596 FILL_TYPE (0xa0, 0xaf, ST_PS_AUDIO_LPCM);
1598 FILL_TYPE (0xc0, 0xdf, ST_AUDIO_MPEG1);
1599 FILL_TYPE (0xe0, 0xef, ST_GST_VIDEO_MPEG1_OR_2);
1606 * pack_start_code 32 bslbf -+
1608 * system_clock_reference_base [32..30] 3 bslbf |
1609 * marker_bit 1 bslbf |
1610 * system_clock_reference_base [29..15] 15 bslbf |
1611 * marker_bit 1 bslbf |
1612 * system_clock_reference_base [14..0] 15 bslbf |
1613 * marker_bit 1 bslbf | 112 bits
1614 * system_clock_reference_extension 9 ubslbf |
1615 * marker_bit 1 bslbf |
1616 * program_mux_rate 22 ubslbf |
1617 * marker_bit 1 bslbf |
1618 * marker_bit 1 bslbf |
1619 * reserved 5 bslbf |
1620 * pack_stuffing_length 3 ubslbf -+
1622 * for (i = 0; i < pack_stuffing_length; i++) {
1623 * stuffing_byte '1111 1111' 8 bslbf
1626 * 112 bits = 14 bytes, as max value for pack_stuffing_length is 7, then
1627 * in total it's needed 14 + 7 = 21 bytes.
1629 #define PACK_START_SIZE 21
1631 static GstFlowReturn
1632 gst_ps_demux_parse_pack_start (GstPsDemux * demux)
1637 guint64 scr, scr_adjusted, new_rate;
1640 guint avail = gst_adapter_available (demux->adapter);
1642 GST_LOG ("parsing pack start");
1644 if (G_UNLIKELY (avail < PACK_START_SIZE))
1645 goto need_more_data;
1647 data = gst_adapter_map (demux->adapter, PACK_START_SIZE);
1649 /* skip start code */
1652 scr1 = GST_READ_UINT32_BE (data);
1653 scr2 = GST_READ_UINT32_BE (data + 4);
1655 /* fixed length to begin with, start code and two scr values */
1658 /* start parsing the stream */
1659 if ((*data & 0xc0) == 0x40) {
1662 guint8 stuffing_bytes;
1664 GST_LOG ("Found MPEG2 stream");
1665 demux->is_mpeg2_pack = TRUE;
1667 /* mpeg2 has more data */
1670 /* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
1673 if (G_UNLIKELY ((scr1 & 0xc4000400) != 0x44000400))
1676 scr = ((guint64) scr1 & 0x38000000) << 3;
1677 scr |= ((guint64) scr1 & 0x03fff800) << 4;
1678 scr |= ((guint64) scr1 & 0x000003ff) << 5;
1679 scr |= ((guint64) scr2 & 0xf8000000) >> 27;
1681 /* marker:1==1 ! scr_ext:9 ! marker:1==1 */
1682 if (G_UNLIKELY ((scr2 & 0x04010000) != 0x04010000))
1685 scr_ext = (scr2 & 0x03fe0000) >> 17;
1686 /* We keep the offset of this scr */
1687 demux->cur_scr_offset = demux->adapter_offset + 12;
1689 GST_LOG_OBJECT (demux, "SCR: 0x%08" G_GINT64_MODIFIER "x SCRE: 0x%08x",
1693 scr = (scr * 300 + scr_ext % 300) / 300;
1695 /* SCR has been converted into units of 90Khz ticks to make it comparable
1696 to DTS/PTS, that also implies 1 tick rounding error */
1698 /* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
1699 next32 = GST_READ_UINT32_BE (data);
1700 if (G_UNLIKELY ((next32 & 0x00000300) != 0x00000300))
1703 new_rate = (next32 & 0xfffffc00) >> 10;
1705 stuffing_bytes = (next32 & 0x07);
1706 GST_LOG_OBJECT (demux, "stuffing bytes: %d", stuffing_bytes);
1709 length += stuffing_bytes;
1710 while (stuffing_bytes--) {
1711 if (*data++ != 0xff)
1715 GST_DEBUG ("Found MPEG1 stream");
1716 demux->is_mpeg2_pack = FALSE;
1719 if (G_UNLIKELY ((scr1 & 0xf1000100) != 0x21000100))
1722 if (G_UNLIKELY ((scr2 & 0x01800001) != 0x01800001))
1725 /* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
1726 scr = ((guint64) scr1 & 0x0e000000) << 5;
1727 scr |= ((guint64) scr1 & 0x00fffe00) << 6;
1728 scr |= ((guint64) scr1 & 0x000000ff) << 7;
1729 scr |= ((guint64) scr2 & 0xfe000000) >> 25;
1731 /* We keep the offset of this scr */
1732 demux->cur_scr_offset = demux->adapter_offset + 8;
1734 /* marker:1==1 ! mux_rate:22 ! marker:1==1 */
1735 new_rate = (scr2 & 0x007ffffe) >> 1;
1739 new_rate *= MPEG_MUX_RATE_MULT;
1741 /* scr adjusted is the new scr found + the colected adjustment */
1742 scr_adjusted = scr + demux->scr_adjust;
1744 GST_LOG_OBJECT (demux,
1745 "SCR: %" G_GINT64_FORMAT " (%" G_GINT64_FORMAT "), mux_rate %"
1746 G_GINT64_FORMAT ", GStreamer Time:%" GST_TIME_FORMAT,
1747 scr, scr_adjusted, new_rate,
1748 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME ((guint64) scr)));
1750 /* keep the first src in order to calculate delta time */
1751 if (G_UNLIKELY (demux->first_scr == G_MAXUINT64)) {
1754 demux->first_scr = scr;
1755 demux->first_scr_offset = demux->cur_scr_offset;
1756 demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
1757 GST_DEBUG_OBJECT (demux, "determined base_time %" GST_TIME_FORMAT,
1758 GST_TIME_ARGS (demux->base_time));
1759 /* at begin consider the new_rate as the scr rate, bytes/clock ticks */
1760 scr_rate_n = new_rate;
1761 scr_rate_d = CLOCK_FREQ;
1762 /* our SCR timeline might have offset wrt upstream timeline */
1763 if (demux->sink_segment.format == GST_FORMAT_TIME) {
1764 if (demux->sink_segment.start > demux->base_time)
1765 diff = -(demux->sink_segment.start - demux->base_time);
1767 diff = demux->base_time - demux->sink_segment.start;
1768 if (diff > GST_SECOND) {
1769 GST_DEBUG_OBJECT (demux, "diff of %" GST_TIME_FORMAT
1770 " wrt upstream start %" GST_TIME_FORMAT "; adjusting base",
1771 GST_TIME_ARGS (diff), GST_TIME_ARGS (demux->sink_segment.start));
1772 demux->base_time += diff;
1775 } else if (G_LIKELY (demux->first_scr_offset != demux->cur_scr_offset)) {
1776 /* estimate byte rate related to the SCR */
1777 scr_rate_n = demux->cur_scr_offset - demux->first_scr_offset;
1778 scr_rate_d = scr_adjusted - demux->first_scr;
1780 scr_rate_n = demux->scr_rate_n;
1781 scr_rate_d = demux->scr_rate_d;
1784 GST_LOG_OBJECT (demux, "%s mode scr: %" G_GUINT64_FORMAT " at %"
1785 G_GUINT64_FORMAT ", first scr: %" G_GUINT64_FORMAT
1786 " at %" G_GUINT64_FORMAT ", scr rate: %" G_GUINT64_FORMAT
1787 "/%" G_GUINT64_FORMAT "(%f)",
1788 ((demux->sink_segment.rate >= 0.0) ? "forward" : "backward"),
1789 scr, demux->cur_scr_offset,
1790 demux->first_scr, demux->first_scr_offset,
1791 scr_rate_n, scr_rate_d, (float) scr_rate_n / scr_rate_d);
1793 /* adjustment of the SCR */
1794 if (G_LIKELY (demux->current_scr != G_MAXUINT64)) {
1796 guint64 old_scr, old_mux_rate, bss, adjust = 0;
1798 /* keep SCR of the previous packet */
1799 old_scr = demux->current_scr;
1800 old_mux_rate = demux->mux_rate;
1802 /* Bytes since SCR is the amount we placed in the adapter since then
1803 * (demux->bytes_since_scr) minus the amount remaining in the adapter,
1804 * clamped to >= 0 */
1805 bss = MAX (0, (gint) (demux->bytes_since_scr - avail));
1807 /* estimate the new SCR using the previous one according the notes
1808 on point 2.5.2.2 of the ISO/IEC 13818-1 document */
1809 if (old_mux_rate != 0)
1810 adjust = (bss * CLOCK_FREQ) / old_mux_rate;
1812 if (demux->sink_segment.rate >= 0.0)
1813 demux->next_scr = old_scr + adjust;
1815 demux->next_scr = old_scr - adjust;
1817 GST_LOG_OBJECT (demux,
1818 "bss: %" G_GUINT64_FORMAT ", next_scr: %" G_GUINT64_FORMAT
1819 ", old_scr: %" G_GUINT64_FORMAT ", scr: %" G_GUINT64_FORMAT,
1820 bss, demux->next_scr, old_scr, scr_adjusted);
1822 /* calculate the absolute deference between the last scr and
1824 if (G_UNLIKELY (old_scr > scr_adjusted))
1825 diff = old_scr - scr_adjusted;
1827 diff = scr_adjusted - old_scr;
1829 /* if the difference is more than 1 second we need to reconfigure
1831 if (G_UNLIKELY (diff > CLOCK_FREQ)) {
1832 demux->scr_adjust = demux->next_scr - scr;
1833 GST_LOG_OBJECT (demux, "discont found, diff: %" G_GINT64_FORMAT
1834 ", adjust %" G_GINT64_FORMAT, diff, demux->scr_adjust);
1835 scr_adjusted = demux->next_scr;
1836 /* don't update rate estimation on disconts */
1837 scr_rate_n = demux->scr_rate_n;
1838 scr_rate_d = demux->scr_rate_d;
1840 demux->next_scr = scr_adjusted;
1844 /* update the current_scr and rate members */
1845 demux->mux_rate = new_rate;
1846 demux->current_scr = scr_adjusted;
1847 demux->scr_rate_n = scr_rate_n;
1848 demux->scr_rate_d = scr_rate_d;
1850 /* Reset the bytes_since_scr value to count the data remaining in the
1852 demux->bytes_since_scr = avail;
1854 gst_adapter_unmap (demux->adapter);
1855 gst_adapter_flush (demux->adapter, length);
1856 ADAPTER_OFFSET_FLUSH (length);
1858 /* Now check for all streams if they're behind the new SCR and if
1859 * they are then move them forward to the SCR position */
1860 gst_ps_demux_send_gap_updates (demux,
1861 MPEGTIME_TO_GSTTIME (demux->current_scr - demux->first_scr));
1867 GST_DEBUG_OBJECT (demux, "lost sync");
1868 gst_adapter_unmap (demux->adapter);
1869 return GST_FLOW_LOST_SYNC;
1873 GST_DEBUG_OBJECT (demux, "need more data");
1874 return GST_FLOW_NEED_MORE_DATA;
1879 * system_header () {
1880 * system_header_start_code 32 bslbf -+
1881 * header_length 16 uimsbf |
1882 * marker_bit 1 bslbf |
1883 * rate_bound 22 uimsbf |
1884 * marker_bit 1 bslbf |
1885 * audio_bound 6 uimsbf |
1886 * fixed_flag 1 bslbf |
1887 * CSPS_flag 1 bslbf | 96 bits
1888 * system_audio_lock_flag 1 bslbf |
1889 * system_video_lock_flag 1 bslbf |
1890 * marker_bit 1 bslbf |
1891 * video_bound 5 uimsbf |
1892 * packet_rate_restriction_flag 1 bslbf |
1893 * reserved_bits 7 bslbf -+
1894 * while (nextbits () = = '1') {
1895 * stream_id 8 uimsbf -+
1896 * '11' 2 bslbf | 24 bits
1897 * P-STD_buffer_bound_scale 1 bslbf |
1898 * P-STD_buffer_size_bound 13 uimsbf -+
1901 * 96 bits = 12 bytes, 24 bits = 3 bytes.
1904 static GstFlowReturn
1905 gst_ps_demux_parse_sys_head (GstPsDemux * demux)
1909 #ifndef GST_DISABLE_GST_DEBUG
1913 if (gst_adapter_available (demux->adapter) < 6)
1914 goto need_more_data;
1916 /* start code + length */
1917 data = gst_adapter_map (demux->adapter, 6);
1919 /* skip start code */
1922 length = GST_READ_UINT16_BE (data);
1923 GST_DEBUG_OBJECT (demux, "length %d", length);
1927 gst_adapter_unmap (demux->adapter);
1928 if (gst_adapter_available (demux->adapter) < length)
1929 goto need_more_data;
1931 data = gst_adapter_map (demux->adapter, length);
1933 /* skip start code and length */
1936 /* marker:1==1 ! rate_bound:22 | marker:1==1 */
1937 if ((*data & 0x80) != 0x80)
1938 goto marker_expected;
1943 if ((data[2] & 0x01) != 0x01)
1944 goto marker_expected;
1946 rate_bound = ((guint32) data[0] & 0x7f) << 15;
1947 rate_bound |= ((guint32) data[1]) << 7;
1948 rate_bound |= ((guint32) data[2] & 0xfe) >> 1;
1949 rate_bound *= MPEG_MUX_RATE_MULT;
1951 GST_DEBUG_OBJECT (demux, "rate bound %u", rate_bound);
1956 /* audio_bound:6==1 ! fixed:1 | constrained:1 */
1958 #ifndef GST_DISABLE_GST_DEBUG
1962 /* max number of simultaneous audio streams active */
1963 audio_bound = (data[0] & 0xfc) >> 2;
1964 /* fixed or variable bitrate */
1965 fixed = (data[0] & 0x02) == 0x02;
1966 /* meeting constraints */
1967 csps = (data[0] & 0x01) == 0x01;
1969 GST_DEBUG_OBJECT (demux, "audio_bound %d, fixed %d, constrained %d",
1970 audio_bound, fixed, csps);
1975 /* audio_lock:1 | video_lock:1 | marker:1==1 | video_bound:5 */
1977 #ifndef GST_DISABLE_GST_DEBUG
1978 gboolean audio_lock;
1979 gboolean video_lock;
1982 audio_lock = (data[0] & 0x80) == 0x80;
1983 video_lock = (data[0] & 0x40) == 0x40;
1986 if ((data[0] & 0x20) != 0x20)
1987 goto marker_expected;
1989 #ifndef GST_DISABLE_GST_DEBUG
1990 /* max number of simultaneous video streams active */
1991 video_bound = (data[0] & 0x1f);
1993 GST_DEBUG_OBJECT (demux, "audio_lock %d, video_lock %d, video_bound %d",
1994 audio_lock, video_lock, video_bound);
1999 /* packet_rate_restriction:1 | reserved:7==0x7F */
2001 #ifndef GST_DISABLE_GST_DEBUG
2002 gboolean packet_rate_restriction;
2004 if ((data[0] & 0x7f) != 0x7f)
2005 goto marker_expected;
2006 #ifndef GST_DISABLE_GST_DEBUG
2007 /* only valid if csps is set */
2009 packet_rate_restriction = (data[0] & 0x80) == 0x80;
2011 GST_DEBUG_OBJECT (demux, "packet_rate_restriction %d",
2012 packet_rate_restriction);
2019 gint stream_count = (length - 12) / 3;
2022 GST_DEBUG_OBJECT (demux, "number of streams: %d ", stream_count);
2024 for (i = 0; i < stream_count; i++) {
2026 #ifndef GST_DISABLE_GST_DEBUG
2027 gboolean STD_buffer_bound_scale;
2028 guint16 STD_buffer_size_bound;
2029 guint32 buf_byte_size_bound;
2031 stream_id = *data++;
2032 if (!(stream_id & 0x80))
2035 /* check marker bits */
2036 if ((*data & 0xC0) != 0xC0)
2037 goto no_placeholder_bits;
2038 #ifndef GST_DISABLE_GST_DEBUG
2039 STD_buffer_bound_scale = *data & 0x20;
2040 STD_buffer_size_bound = ((guint16) (*data++ & 0x1F)) << 8;
2041 STD_buffer_size_bound |= *data++;
2043 if (STD_buffer_bound_scale == 0) {
2044 buf_byte_size_bound = STD_buffer_size_bound * 128;
2046 buf_byte_size_bound = STD_buffer_size_bound * 1024;
2049 GST_DEBUG_OBJECT (demux, "STD_buffer_bound_scale %d",
2050 STD_buffer_bound_scale);
2051 GST_DEBUG_OBJECT (demux, "STD_buffer_size_bound %d or %d bytes",
2052 STD_buffer_size_bound, buf_byte_size_bound);
2057 gst_adapter_unmap (demux->adapter);
2058 gst_adapter_flush (demux->adapter, length);
2059 ADAPTER_OFFSET_FLUSH (length);
2065 GST_DEBUG_OBJECT (demux, "expecting marker");
2066 gst_adapter_unmap (demux->adapter);
2067 return GST_FLOW_LOST_SYNC;
2069 no_placeholder_bits:
2071 GST_DEBUG_OBJECT (demux, "expecting placeholder bit values"
2072 " '11' after stream id");
2073 gst_adapter_unmap (demux->adapter);
2074 return GST_FLOW_LOST_SYNC;
2078 GST_DEBUG_OBJECT (demux, "error in system header length");
2079 gst_adapter_unmap (demux->adapter);
2080 return GST_FLOW_LOST_SYNC;
2084 GST_DEBUG_OBJECT (demux, "need more data");
2085 gst_adapter_unmap (demux->adapter);
2086 return GST_FLOW_NEED_MORE_DATA;
2090 static GstFlowReturn
2091 gst_ps_demux_parse_psm (GstPsDemux * demux)
2093 guint16 psm_length, info_length = 0, es_map_length = 0;
2094 guint8 psm_version = 0;
2096 #ifndef GST_DISABLE_GST_DEBUG
2097 gboolean applicable;
2100 /* Need at least 6 bytes for start code + length */
2101 if (gst_adapter_available (demux->adapter) < 6)
2102 goto need_more_data;
2107 /* start code + length */
2108 data = gst_adapter_map (demux->adapter, 6);
2109 /* skip start code */
2111 psm_length = GST_READ_UINT16_BE (data);
2112 GST_DEBUG_OBJECT (demux, "PSM length %u", psm_length);
2114 if (G_UNLIKELY (psm_length > 0x3FA))
2116 psm_length += 6; /* Add start code + size to length */
2118 gst_adapter_unmap (demux->adapter);
2120 if (gst_adapter_available (demux->adapter) < psm_length)
2121 goto need_more_data;
2123 data = gst_adapter_map (demux->adapter, psm_length);
2125 gst_byte_reader_init (&br, data, psm_length);
2128 /* skip start code and length */
2129 if (!gst_byte_reader_skip (&br, 6))
2132 /* Read PSM applicable bit together with version */
2133 if (!gst_byte_reader_get_uint8 (&br, &psm_version))
2135 #ifndef GST_DISABLE_GST_DEBUG
2136 applicable = (psm_version & 0x80) >> 7;
2138 psm_version &= 0x1F;
2139 GST_DEBUG_OBJECT (demux, "PSM version %u (applicable now %u)", psm_version,
2142 /* Jump over the next byte (marker bit) */
2143 if (!gst_byte_reader_skip (&br, 1))
2146 /* Read PS info length */
2147 if (!gst_byte_reader_get_uint16_be (&br, &info_length))
2149 GST_DEBUG_OBJECT (demux, "PS info length %u bytes", info_length);
2150 /* Skip the PS info, we don't use it */
2151 if (!gst_byte_reader_skip (&br, info_length))
2154 /* Read ES map length */
2155 if (!gst_byte_reader_get_uint16_be (&br, &es_map_length))
2157 GST_DEBUG_OBJECT (demux, "ES map length %u bytes", es_map_length);
2159 /* Now read the ES map */
2161 GstByteReader es_map_br;
2162 if (!gst_byte_reader_get_sub_reader (&br, &es_map_br, es_map_length))
2165 while (gst_byte_reader_get_remaining (&es_map_br) >= 4) {
2166 guint8 stream_type = 0, stream_id = 0;
2167 guint16 stream_info_length = 0;
2169 if (!gst_byte_reader_get_uint8 (&es_map_br, &stream_type) ||
2170 !gst_byte_reader_get_uint8 (&es_map_br, &stream_id) ||
2171 !gst_byte_reader_get_uint16_be (&es_map_br, &stream_info_length))
2174 GST_DEBUG_OBJECT (demux,
2175 "Stream type %02X with id %02X and %u bytes info", stream_type,
2176 stream_id, stream_info_length);
2178 if (G_LIKELY (stream_id != 0xbd))
2179 demux->psm[stream_id] = stream_type;
2181 /* Ignore stream type for private_stream_1 and discover it looking at
2183 * Fixes demuxing some clips with lpcm that was wrongly declared as
2185 GST_DEBUG_OBJECT (demux, "stream type for private_stream_1 ignored");
2188 /* FIXME: We could use the descriptors instead of skipping them */
2189 if (!gst_byte_reader_skip (&es_map_br, stream_info_length))
2193 /* We ignore the 4-byte CRC at the end */
2195 gst_adapter_unmap (demux->adapter);
2196 gst_adapter_flush (demux->adapter, psm_length);
2197 ADAPTER_OFFSET_FLUSH (psm_length);
2201 GST_DEBUG_OBJECT (demux, "Failed to parse PSM. Skipping");
2202 gst_adapter_unmap (demux->adapter);
2203 gst_adapter_flush (demux->adapter, psm_length);
2204 ADAPTER_OFFSET_FLUSH (psm_length);
2205 return GST_FLOW_LOST_SYNC;
2208 GST_DEBUG_OBJECT (demux, "error in PSM length");
2209 gst_adapter_unmap (demux->adapter);
2210 return GST_FLOW_LOST_SYNC;
2214 GST_DEBUG_OBJECT (demux, "need more data");
2215 return GST_FLOW_NEED_MORE_DATA;
2220 gst_ps_demux_resync_cb (GstPESFilter * filter, GstPsDemux * demux)
2224 static GstFlowReturn
2225 gst_ps_demux_data_cb (GstPESFilter * filter, gboolean first,
2226 GstBuffer * buffer, GstPsDemux * demux)
2229 GstFlowReturn ret = GST_FLOW_OK;
2236 gst_buffer_map (buffer, &map, GST_MAP_READ);
2238 start_code = filter->start_code;
2242 /* find the stream type */
2243 stream_type = demux->psm[id];
2244 if (stream_type == -1) {
2245 /* no stream type, if PS1, get the new id */
2246 if (start_code == ID_PRIVATE_STREAM_1 && datalen >= 2) {
2247 /* VDR writes A52 streams without any header bytes
2248 * (see ftp://ftp.mplayerhq.hu/MPlayer/samples/MPEG-VOB/vdr-AC3) */
2250 guint hdr = GST_READ_UINT32_BE (map.data);
2251 if (G_UNLIKELY ((hdr & 0xffff0000) == AC3_SYNC_WORD)) {
2253 stream_type = demux->psm[id] = ST_GST_AUDIO_RAWA52;
2254 GST_DEBUG_OBJECT (demux, "Found VDR raw A52 stream");
2258 if (G_LIKELY (stream_type == -1)) {
2259 /* new id is in the first byte */
2260 id = map.data[offset++];
2263 stream_type = demux->psm[id];
2264 /* Now, if it's a subpicture stream - no more, otherwise
2265 * take the first byte too, since it's the frame count in audio
2266 * streams and our backwards compat convention is to strip it off */
2267 if (stream_type != ST_PS_DVD_SUBPICTURE) {
2268 /* Number of audio frames in this packet */
2269 #ifndef GST_DISABLE_GST_DEBUG
2271 nframes = map.data[offset];
2272 GST_LOG_OBJECT (demux, "private type 0x%02x, %d frames", id,
2278 GST_LOG_OBJECT (demux, "private type 0x%02x, stream type %d",
2283 if (stream_type == -1)
2284 goto unknown_stream_type;
2285 } else if (stream_type == ST_AUDIO_MPEG1 || stream_type == ST_AUDIO_MPEG2) {
2287 guint hdr = GST_READ_UINT16_BE (map.data);
2288 if ((hdr & 0xfff0) == 0xfff0) {
2289 switch (hdr & 0x06) {
2300 GST_WARNING_OBJECT (demux, "unknown mpeg audio layer");
2306 if (filter->pts != -1) {
2307 demux->next_pts = filter->pts + demux->scr_adjust;
2308 GST_LOG_OBJECT (demux, "stream 0x%02x PTS = orig %" G_GUINT64_FORMAT
2309 " (%" G_GUINT64_FORMAT ")", id, filter->pts, demux->next_pts);
2311 demux->next_pts = G_MAXUINT64;
2312 if (filter->dts != -1) {
2313 demux->next_dts = filter->dts + demux->scr_adjust;
2314 GST_LOG_OBJECT (demux, "stream 0x%02x DTS = orig %" G_GUINT64_FORMAT
2315 " (%" G_GUINT64_FORMAT ")", id, filter->dts, demux->next_dts);
2317 demux->next_dts = demux->next_pts;
2320 demux->current_stream =
2321 gst_ps_demux_get_stream (demux, id, stream_type, layer);
2324 if (G_UNLIKELY (demux->current_stream == NULL)) {
2325 GST_DEBUG_OBJECT (demux, "Dropping buffer for unknown stream id 0x%02x",
2330 /* After 2 seconds of bitstream emit no more pads */
2331 if (demux->need_no_more_pads
2332 && (demux->current_scr - demux->first_scr) > 2 * CLOCK_FREQ) {
2333 GST_DEBUG_OBJECT (demux, "no more pads, notifying");
2334 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
2335 demux->need_no_more_pads = FALSE;
2338 /* If the stream is not-linked, don't bother creating a sub-buffer
2339 * to send to it, unless we're processing a discont (which resets
2340 * the not-linked status and tries again */
2341 if (demux->current_stream->discont) {
2342 GST_DEBUG_OBJECT (demux, "stream is discont");
2343 demux->current_stream->notlinked = FALSE;
2346 if (demux->current_stream->notlinked == FALSE) {
2348 gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, datalen);
2349 ret = gst_ps_demux_send_data (demux, demux->current_stream, out_buf);
2350 if (ret == GST_FLOW_NOT_LINKED) {
2351 demux->current_stream->notlinked = TRUE;
2356 gst_buffer_unmap (buffer, &map);
2357 gst_buffer_unref (buffer);
2360 unknown_stream_type:
2362 GST_DEBUG_OBJECT (demux, "unknown stream type %02x", id);
2369 gst_ps_demux_resync (GstPsDemux * demux, gboolean save)
2376 avail = gst_adapter_available (demux->adapter);
2377 if (G_UNLIKELY (avail < 4))
2379 /* Common case, read 4 bytes an check it */
2380 data = gst_adapter_map (demux->adapter, 4);
2381 /* read currect code */
2382 code = GST_READ_UINT32_BE (data);
2383 /* The common case is that the sync code is at 0 bytes offset */
2384 if (G_LIKELY ((code & 0xffffff00) == 0x100L)) {
2385 GST_LOG_OBJECT (demux, "Found resync code %08x after 0 bytes", code);
2386 demux->last_sync_code = code;
2387 gst_adapter_unmap (demux->adapter);
2391 /* Otherwise, we are starting at byte 4 and we need to search
2392 the sync code in all available data in the adapter */
2394 if (offset >= avail)
2395 goto need_data; /* Not enough data to find sync */
2396 data = gst_adapter_map (demux->adapter, avail);
2398 code = (code << 8) | data[offset++];
2399 found = (code & 0xffffff00) == 0x100L;
2400 } while (offset < avail && !found);
2401 gst_adapter_unmap (demux->adapter);
2402 if (!save || demux->sink_segment.rate >= 0.0) {
2403 GST_LOG_OBJECT (demux, "flushing %d bytes", offset - 4);
2404 /* forward playback, we can discard and flush the skipped bytes */
2405 gst_adapter_flush (demux->adapter, offset - 4);
2406 ADAPTER_OFFSET_FLUSH (offset - 4);
2409 GST_LOG_OBJECT (demux, "reverse saving %d bytes", offset - 4);
2410 /* reverse playback, we keep the flushed bytes and we will append them to
2411 * the next buffer in the chain function, which is the previous buffer in
2413 gst_adapter_push (demux->rev_adapter,
2414 gst_adapter_take_buffer (demux->adapter, offset - 4));
2416 GST_LOG_OBJECT (demux, "reverse saving %d bytes", avail);
2417 /* nothing found, keep all bytes */
2418 gst_adapter_push (demux->rev_adapter,
2419 gst_adapter_take_buffer (demux->adapter, avail));
2424 GST_LOG_OBJECT (demux, "Found resync code %08x after %d bytes",
2426 demux->last_sync_code = code;
2428 GST_LOG_OBJECT (demux, "No resync after skipping %d", offset);
2434 GST_LOG_OBJECT (demux, "we need more data for resync %d", avail);
2439 static inline gboolean
2440 gst_ps_demux_is_pes_sync (guint32 sync)
2442 return ((sync & 0xfc) == 0xbc) ||
2443 ((sync & 0xe0) == 0xc0) || ((sync & 0xf0) == 0xe0);
2446 static inline gboolean
2447 gst_ps_demux_scan_ts (GstPsDemux * demux, const guint8 * data,
2448 SCAN_MODE mode, guint64 * rts, const guint8 * end)
2450 gboolean ret = FALSE;
2456 /* read the 4 bytes for the sync code */
2457 code = GST_READ_UINT32_BE (data);
2458 if (G_LIKELY (code != ID_PS_PACK_START_CODE))
2460 if (data + 12 > end)
2462 /* skip start code */
2464 scr1 = GST_READ_UINT32_BE (data);
2465 scr2 = GST_READ_UINT32_BE (data + 4);
2466 /* start parsing the stream */
2467 if ((*data & 0xc0) == 0x40) {
2468 /* MPEG-2 PACK header */
2471 guint8 stuffing_bytes;
2472 /* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
2474 if ((scr1 & 0xc4000400) != 0x44000400)
2476 scr = ((guint64) scr1 & 0x38000000) << 3;
2477 scr |= ((guint64) scr1 & 0x03fff800) << 4;
2478 scr |= ((guint64) scr1 & 0x000003ff) << 5;
2479 scr |= ((guint64) scr2 & 0xf8000000) >> 27;
2480 /* marker:1==1 ! scr_ext:9 ! marker:1==1 */
2481 if ((scr2 & 0x04010000) != 0x04010000)
2483 scr_ext = (scr2 & 0x03fe0000) >> 17;
2485 scr = (scr * 300 + scr_ext % 300) / 300;
2487 /* SCR has been converted into units of 90Khz ticks to make it comparable
2488 to DTS/PTS, that also implies 1 tick rounding error */
2493 /* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
2494 next32 = GST_READ_UINT32_BE (data);
2495 if ((next32 & 0x00000300) != 0x00000300)
2497 stuffing_bytes = (next32 & 0x07);
2499 if (data + stuffing_bytes > end)
2501 while (stuffing_bytes--) {
2502 if (*data++ != 0xff)
2506 /* MPEG-1 pack header */
2508 if ((scr1 & 0xf1000100) != 0x21000100)
2510 if ((scr2 & 0x01800001) != 0x01800001)
2512 /* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
2513 scr = ((guint64) scr1 & 0x0e000000) << 5;
2514 scr |= ((guint64) scr1 & 0x00fffe00) << 6;
2515 scr |= ((guint64) scr1 & 0x000000ff) << 7;
2516 scr |= ((guint64) scr2 & 0xfe000000) >> 25;
2520 if (mode == SCAN_SCR) {
2526 /* Possible optional System header here */
2530 code = GST_READ_UINT32_BE (data);
2531 len = GST_READ_UINT16_BE (data + 4);
2532 if (code == ID_PS_SYSTEM_HEADER_START_CODE) {
2533 /* Found a system header, skip it */
2534 /* Check for sufficient data - system header, plus enough
2535 * left over for the PES packet header */
2536 if (data + 6 + len + 6 > end)
2539 /* read the 4 bytes for the PES sync code */
2540 code = GST_READ_UINT32_BE (data);
2541 len = GST_READ_UINT16_BE (data + 4);
2544 /* Check we have enough data left for reading the PES packet */
2545 if (data + 6 + len > end)
2547 if (!gst_ps_demux_is_pes_sync (code))
2550 case ID_PS_PROGRAM_STREAM_MAP:
2551 case ID_PRIVATE_STREAM_2:
2554 case ID_PROGRAM_STREAM_DIRECTORY:
2555 case ID_DSMCC_STREAM:
2556 case ID_ITU_TREC_H222_TYPE_E_STREAM:
2557 case ID_PADDING_STREAM:
2563 /* skip sync code and size */
2566 /* stuffing bits, first two bits are '10' for mpeg2 pes so this code is
2574 /* STD buffer size, never for mpeg2 */
2575 if ((*data & 0xc0) == 0x40)
2577 /* PTS but no DTS, never for mpeg2 */
2578 if ((*data & 0xf0) == 0x20) {
2579 READ_TS (data, pts, beach);
2581 /* PTS and DTS, never for mpeg2 */
2582 else if ((*data & 0xf0) == 0x30) {
2583 READ_TS (data, pts, beach);
2584 READ_TS (data, dts, beach);
2585 } else if ((*data & 0xc0) == 0x80) {
2589 * 2: PES_scrambling_control
2591 * 1: data_alignment_indicator
2593 * 1: original_or_copy
2596 if ((flags & 0xc0) != 0x80)
2601 * 1: DSM_trick_mode_flag
2602 * 1: additional_copy_info_flag
2604 * 1: PES_extension_flag
2607 /* 8: PES_header_data_length */
2609 /* only DTS: this is invalid */
2610 if ((flags & 0xc0) == 0x40)
2613 if ((flags & 0x80)) {
2614 READ_TS (data, pts, beach);
2617 if ((flags & 0x40)) {
2618 READ_TS (data, dts, beach);
2622 if (mode == SCAN_DTS && dts != (guint64) - 1) {
2627 if (mode == SCAN_PTS && pts != (guint64) - 1) {
2635 static inline gboolean
2636 gst_ps_demux_scan_forward_ts (GstPsDemux * demux, guint64 * pos,
2637 SCAN_MODE mode, guint64 * rts, gint limit)
2639 GstFlowReturn ret = GST_FLOW_OK;
2641 guint64 offset = *pos;
2642 gboolean found = FALSE;
2644 guint scan_sz = (mode == SCAN_SCR ? SCAN_SCR_SZ : SCAN_PTS_SZ);
2645 guint cursor, to_read = BLOCK_SZ;
2649 /* Check we can get at least scan_sz bytes */
2650 if (offset + scan_sz > demux->sink_segment.stop)
2652 /* Don't go further than 'limit' bytes */
2653 if (limit && offset > *pos + limit)
2655 if (offset + to_read > demux->sink_segment.stop)
2656 to_read = demux->sink_segment.stop - offset;
2657 /* read some data */
2659 ret = gst_pad_pull_range (demux->sinkpad, offset, to_read, &buffer);
2660 if (G_UNLIKELY (ret != GST_FLOW_OK))
2662 gst_buffer_map (buffer, &map, GST_MAP_READ);
2663 /* may get a short buffer at the end of the file */
2664 if (G_UNLIKELY (map.size <= scan_sz)) {
2665 gst_buffer_unmap (buffer, &map);
2666 gst_buffer_unref (buffer);
2670 end_scan = map.size - scan_sz;
2671 /* scan the block */
2672 for (cursor = 0; !found && cursor <= end_scan; cursor++) {
2673 found = gst_ps_demux_scan_ts (demux, map.data + cursor, mode, &ts,
2674 map.data + map.size);
2677 /* done with the buffer, unref it */
2678 gst_buffer_unmap (buffer, &map);
2679 gst_buffer_unref (buffer);
2682 *pos = offset + cursor - 1;
2686 } while (!found && offset < demux->sink_segment.stop);
2690 static inline gboolean
2691 gst_ps_demux_scan_backward_ts (GstPsDemux * demux, guint64 * pos,
2692 SCAN_MODE mode, guint64 * rts, gint limit)
2694 GstFlowReturn ret = GST_FLOW_OK;
2696 guint64 offset = *pos;
2697 gboolean found = FALSE;
2699 guint scan_sz = (mode == SCAN_SCR ? SCAN_SCR_SZ : SCAN_PTS_SZ);
2700 guint cursor, to_read = BLOCK_SZ;
2705 /* Check we have at least scan_sz bytes available */
2706 if (offset < scan_sz - 1)
2708 /* Don't go backward past the start or 'limit' bytes */
2709 if (limit && offset + limit < *pos)
2711 if (offset > BLOCK_SZ)
2714 to_read = offset + 1;
2717 /* read some data */
2719 ret = gst_pad_pull_range (demux->sinkpad, offset, to_read, &buffer);
2720 if (G_UNLIKELY (ret != GST_FLOW_OK))
2722 gst_buffer_map (buffer, &map, GST_MAP_READ);
2723 /* may get a short buffer at the end of the file */
2724 if (G_UNLIKELY (map.size <= scan_sz)) {
2725 gst_buffer_unmap (buffer, &map);
2726 gst_buffer_unref (buffer);
2730 start_scan = map.size - scan_sz;
2731 data = map.data + start_scan;
2732 /* scan the block */
2733 for (cursor = (start_scan + 1); !found && cursor > 0; cursor--) {
2734 found = gst_ps_demux_scan_ts (demux, data--, mode, &ts,
2735 map.data + map.size);
2738 /* done with the buffer, unref it */
2739 gst_buffer_unmap (buffer, &map);
2740 gst_buffer_unref (buffer);
2743 *pos = offset + cursor;
2746 } while (!found && offset > 0);
2750 static inline gboolean
2751 gst_ps_sink_get_duration (GstPsDemux * demux)
2753 gboolean res = FALSE;
2755 GstFormat format = GST_FORMAT_BYTES;
2760 /* init the sink segment */
2761 gst_segment_init (&demux->sink_segment, format);
2762 /* get peer to figure out length */
2763 if ((peer = gst_pad_get_peer (demux->sinkpad)) == NULL)
2765 res = gst_pad_query_duration (peer, format, &length);
2766 gst_object_unref (peer);
2767 if (!res || length <= 0)
2769 GST_DEBUG_OBJECT (demux, "file length %" G_GINT64_FORMAT, length);
2770 /* update the sink segment */
2771 demux->sink_segment.stop = length;
2772 gst_segment_set_duration (&demux->sink_segment, format, length);
2773 gst_segment_set_position (&demux->sink_segment, format, 0);
2774 /* Scan for notorious SCR and PTS to calculate the duration */
2775 /* scan for first SCR in the stream */
2776 offset = demux->sink_segment.start;
2777 gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR,
2778 &demux->first_scr, DURATION_SCAN_LIMIT);
2779 GST_DEBUG_OBJECT (demux,
2780 "First SCR: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2781 " in packet starting at %" G_GUINT64_FORMAT, demux->first_scr,
2782 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_scr)), offset);
2783 demux->first_scr_offset = offset;
2784 /* scan for last SCR in the stream */
2785 offset = demux->sink_segment.stop;
2786 gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR,
2787 &demux->last_scr, DURATION_SCAN_LIMIT);
2788 GST_DEBUG_OBJECT (demux,
2789 "Last SCR: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2790 " in packet starting at %" G_GUINT64_FORMAT, demux->last_scr,
2791 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->last_scr)), offset);
2792 demux->last_scr_offset = offset;
2793 /* scan for first PTS in the stream */
2794 offset = demux->sink_segment.start;
2795 gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_PTS,
2796 &demux->first_pts, DURATION_SCAN_LIMIT);
2797 GST_DEBUG_OBJECT (demux,
2798 "First PTS: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2799 " in packet starting at %" G_GUINT64_FORMAT, demux->first_pts,
2800 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_pts)), offset);
2801 if (demux->first_pts != G_MAXUINT64) {
2802 /* scan for last PTS in the stream */
2803 offset = demux->sink_segment.stop;
2804 gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_PTS,
2805 &demux->last_pts, DURATION_SCAN_LIMIT);
2806 GST_DEBUG_OBJECT (demux,
2807 "Last PTS: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2808 " in packet starting at %" G_GUINT64_FORMAT, demux->last_pts,
2809 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->last_pts)), offset);
2811 /* Detect wrong SCR values */
2812 if (demux->first_scr > demux->last_scr) {
2813 GST_DEBUG_OBJECT (demux, "Wrong SCR values detected, searching for "
2814 "a better first SCR value");
2815 offset = demux->first_scr_offset;
2816 for (i = 0; i < 10; i++) {
2818 gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &scr, 0);
2819 if (scr < demux->last_scr) {
2820 demux->first_scr = scr;
2821 demux->first_scr_offset = offset;
2822 /* Start demuxing from the right place */
2823 demux->sink_segment.position = offset;
2824 GST_DEBUG_OBJECT (demux, "Replaced First SCR: %" G_GINT64_FORMAT
2825 " %" GST_TIME_FORMAT " in packet starting at %"
2826 G_GUINT64_FORMAT, demux->first_scr,
2827 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_scr)), offset);
2832 /* Set the base_time and avg rate */
2833 demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
2834 demux->scr_rate_n = demux->last_scr_offset - demux->first_scr_offset;
2835 demux->scr_rate_d = demux->last_scr - demux->first_scr;
2836 if (G_LIKELY (demux->first_pts != G_MAXUINT64 &&
2837 demux->last_pts != G_MAXUINT64)) {
2838 /* update the src segment */
2839 demux->src_segment.format = GST_FORMAT_TIME;
2840 demux->src_segment.start =
2841 MPEGTIME_TO_GSTTIME (demux->first_pts) - demux->base_time;
2842 demux->src_segment.stop = -1;
2843 gst_segment_set_duration (&demux->src_segment, GST_FORMAT_TIME,
2844 MPEGTIME_TO_GSTTIME (demux->last_pts - demux->first_pts));
2845 gst_segment_set_position (&demux->src_segment, GST_FORMAT_TIME,
2846 demux->src_segment.start);
2848 GST_INFO_OBJECT (demux, "sink segment configured %" GST_SEGMENT_FORMAT,
2849 &demux->sink_segment);
2850 GST_INFO_OBJECT (demux, "src segment configured %" GST_SEGMENT_FORMAT,
2851 &demux->src_segment);
2857 static inline GstFlowReturn
2858 gst_ps_demux_pull_block (GstPad * pad, GstPsDemux * demux,
2859 guint64 offset, guint size)
2862 GstBuffer *buffer = NULL;
2863 ret = gst_pad_pull_range (pad, offset, size, &buffer);
2864 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2865 GST_DEBUG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
2866 " size %u failed", offset, size);
2869 GST_LOG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
2870 " size %u done", offset, size);
2871 if (demux->sink_segment.rate < 0) {
2872 GST_LOG_OBJECT (demux, "setting discont flag on backward rate");
2873 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2875 ret = gst_ps_demux_chain (pad, GST_OBJECT (demux), buffer);
2881 gst_ps_demux_loop (GstPad * pad)
2884 GstFlowReturn ret = GST_FLOW_OK;
2886 demux = GST_PS_DEMUX (gst_pad_get_parent (pad));
2887 if (G_UNLIKELY (demux->flushing)) {
2888 ret = GST_FLOW_FLUSHING;
2892 if (G_UNLIKELY (demux->sink_segment.format == GST_FORMAT_UNDEFINED))
2893 gst_ps_sink_get_duration (demux);
2894 offset = demux->sink_segment.position;
2895 if (demux->sink_segment.rate >= 0) {
2896 guint size = BLOCK_SZ;
2897 if (G_LIKELY (demux->sink_segment.stop != (guint64) - 1)) {
2898 size = MIN (size, demux->sink_segment.stop - offset);
2901 ret = gst_ps_demux_pull_block (pad, demux, offset, size);
2902 /* pause if something went wrong */
2903 if (G_UNLIKELY (ret != GST_FLOW_OK))
2905 /* update our position */
2907 gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
2908 /* check EOS condition */
2909 if ((demux->sink_segment.position >= demux->sink_segment.stop) ||
2910 (demux->src_segment.stop != (guint64) - 1 &&
2911 demux->src_segment.position >= demux->src_segment.stop)) {
2912 GST_DEBUG_OBJECT (demux,
2913 "forward mode using segment reached end of " "segment pos %"
2914 GST_TIME_FORMAT " stop %" GST_TIME_FORMAT " pos in bytes %"
2915 G_GUINT64_FORMAT " stop in bytes %" G_GUINT64_FORMAT,
2916 GST_TIME_ARGS (demux->src_segment.position),
2917 GST_TIME_ARGS (demux->src_segment.stop),
2918 demux->sink_segment.position, demux->sink_segment.stop);
2922 } else { /* Reverse playback */
2923 guint64 size = MIN (offset, BLOCK_SZ);
2925 ret = gst_ps_demux_pull_block (pad, demux, offset - size, size);
2926 /* pause if something went wrong */
2927 if (G_UNLIKELY (ret != GST_FLOW_OK))
2929 /* update our position */
2931 gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
2932 /* check EOS condition */
2933 if (demux->sink_segment.position <= demux->sink_segment.start ||
2934 demux->src_segment.position <= demux->src_segment.start) {
2935 GST_DEBUG_OBJECT (demux,
2936 "reverse mode using segment reached end of " "segment pos %"
2937 GST_TIME_FORMAT " stop %" GST_TIME_FORMAT " pos in bytes %"
2938 G_GUINT64_FORMAT " stop in bytes %" G_GUINT64_FORMAT,
2939 GST_TIME_ARGS (demux->src_segment.position),
2940 GST_TIME_ARGS (demux->src_segment.start),
2941 demux->sink_segment.position, demux->sink_segment.start);
2947 gst_object_unref (demux);
2951 const gchar *reason = gst_flow_get_name (ret);
2952 GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
2953 gst_pad_pause_task (pad);
2954 if (ret == GST_FLOW_EOS) {
2955 /* perform EOS logic */
2956 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
2957 if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
2959 /* for segment playback we need to post when (in stream time)
2960 * we stopped, this is either stop (when set) or the duration. */
2961 if ((stop = demux->src_segment.stop) == -1)
2962 stop = demux->src_segment.duration;
2963 if (demux->sink_segment.rate >= 0) {
2964 GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
2965 gst_element_post_message (GST_ELEMENT_CAST (demux),
2966 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
2967 GST_FORMAT_TIME, stop));
2968 gst_ps_demux_send_event (demux,
2969 gst_event_new_segment_done (GST_FORMAT_TIME, stop));
2970 } else { /* Reverse playback */
2971 GST_LOG_OBJECT (demux,
2972 "Sending segment done, at beginning of " "segment");
2973 gst_element_post_message (GST_ELEMENT_CAST (demux),
2974 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
2975 GST_FORMAT_TIME, demux->src_segment.start));
2976 gst_ps_demux_send_event (demux,
2977 gst_event_new_segment_done (GST_FORMAT_TIME,
2978 demux->src_segment.start));
2981 /* normal playback, send EOS to all linked pads */
2982 gst_element_no_more_pads (GST_ELEMENT (demux));
2983 GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
2984 if (!gst_ps_demux_send_event (demux, gst_event_new_eos ())
2985 && !have_open_streams (demux)) {
2986 GST_WARNING_OBJECT (demux, "EOS and no streams open");
2987 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
2988 ("Internal data stream error."), ("No valid streams detected"));
2991 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2992 GST_ELEMENT_FLOW_ERROR (demux, ret);
2993 gst_ps_demux_send_event (demux, gst_event_new_eos ());
2996 gst_object_unref (demux);
3001 /* If we can pull that's preferred */
3003 gst_ps_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
3005 gboolean res = FALSE;
3006 GstQuery *query = gst_query_new_scheduling ();
3007 if (gst_pad_peer_query (sinkpad, query)) {
3008 if (gst_query_has_scheduling_mode_with_flags (query,
3009 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE)) {
3010 res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
3012 res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
3015 res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
3018 gst_query_unref (query);
3022 /* This function gets called when we activate ourselves in push mode. */
3024 gst_ps_demux_sink_activate_push (GstPad * sinkpad, GstObject * parent,
3027 GstPsDemux *demux = GST_PS_DEMUX (parent);
3028 demux->random_access = FALSE;
3032 /* this function gets called when we activate ourselves in pull mode.
3033 * We can perform random access to the resource and we start a task
3034 * to start reading */
3036 gst_ps_demux_sink_activate_pull (GstPad * sinkpad, GstObject * parent,
3039 GstPsDemux *demux = GST_PS_DEMUX (parent);
3041 GST_DEBUG ("pull mode activated");
3042 demux->random_access = TRUE;
3043 return gst_pad_start_task (sinkpad,
3044 (GstTaskFunction) gst_ps_demux_loop, sinkpad, NULL);
3046 demux->random_access = FALSE;
3047 return gst_pad_stop_task (sinkpad);
3052 gst_ps_demux_sink_activate_mode (GstPad * pad, GstObject * parent,
3053 GstPadMode mode, gboolean active)
3055 if (mode == GST_PAD_MODE_PUSH) {
3056 return gst_ps_demux_sink_activate_push (pad, parent, active);
3057 } else if (mode == GST_PAD_MODE_PULL) {
3058 return gst_ps_demux_sink_activate_pull (pad, parent, active);
3063 /* EOS and NOT_LINKED need to be combined. This means that we return:
3065 * GST_FLOW_NOT_LINKED: when all pads NOT_LINKED.
3066 * GST_FLOW_EOS: when all pads EOS or NOT_LINKED.
3068 static GstFlowReturn
3069 gst_ps_demux_combine_flows (GstPsDemux * demux, GstFlowReturn ret)
3071 GST_LOG_OBJECT (demux, "flow return: %s", gst_flow_get_name (ret));
3072 ret = gst_flow_combiner_update_flow (demux->flowcombiner, ret);
3073 if (G_UNLIKELY (demux->need_no_more_pads && ret == GST_FLOW_NOT_LINKED))
3075 GST_LOG_OBJECT (demux, "combined flow return: %s", gst_flow_get_name (ret));
3079 static GstFlowReturn
3080 gst_ps_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3082 GstPsDemux *demux = GST_PS_DEMUX (parent);
3083 GstFlowReturn ret = GST_FLOW_OK;
3085 gboolean save, discont;
3086 discont = GST_BUFFER_IS_DISCONT (buffer);
3088 GST_LOG_OBJECT (demux,
3089 "Received buffer with discont flag and" " offset %"
3090 G_GUINT64_FORMAT, GST_BUFFER_OFFSET (buffer));
3091 gst_pes_filter_drain (&demux->filter);
3092 gst_ps_demux_mark_discont (demux, TRUE, FALSE);
3093 /* mark discont on all streams */
3094 if (demux->sink_segment.rate >= 0.0) {
3095 demux->current_scr = G_MAXUINT64;
3096 demux->bytes_since_scr = 0;
3099 GST_LOG_OBJECT (demux,
3100 "Received buffer with offset %" G_GUINT64_FORMAT,
3101 GST_BUFFER_OFFSET (buffer));
3104 /* We keep the offset to interpolate SCR */
3105 demux->adapter_offset = GST_BUFFER_OFFSET (buffer);
3106 gst_adapter_push (demux->adapter, buffer);
3107 demux->bytes_since_scr += gst_buffer_get_size (buffer);
3108 avail = gst_adapter_available (demux->rev_adapter);
3110 GST_LOG_OBJECT (demux, "appending %u saved bytes", avail);
3111 /* if we have a previous reverse chunk, append this now */
3112 /* FIXME this code assumes we receive discont buffers all thei
3114 gst_adapter_push (demux->adapter,
3115 gst_adapter_take_buffer (demux->rev_adapter, avail));
3118 avail = gst_adapter_available (demux->adapter);
3119 GST_LOG_OBJECT (demux, "avail now: %d, state %d", avail, demux->filter.state);
3120 switch (demux->filter.state) {
3121 case STATE_DATA_SKIP:
3122 case STATE_DATA_PUSH:
3123 ret = gst_pes_filter_process (&demux->filter);
3125 case STATE_HEADER_PARSE:
3132 case GST_FLOW_NEED_MORE_DATA:
3133 /* Go and get more data */
3136 case GST_FLOW_LOST_SYNC:
3137 /* for FLOW_OK or lost-sync, carry onto resync */
3143 /* Any other return value should be sent upstream immediately */
3147 /* align adapter data to sync boundary, we keep the data up to the next sync
3150 while (gst_ps_demux_resync (demux, save)) {
3151 gboolean ps_sync = TRUE;
3152 if (G_UNLIKELY (demux->flushing)) {
3153 ret = GST_FLOW_FLUSHING;
3157 /* now switch on last synced byte */
3158 switch (demux->last_sync_code) {
3159 case ID_PS_PACK_START_CODE:
3160 ret = gst_ps_demux_parse_pack_start (demux);
3162 case ID_PS_SYSTEM_HEADER_START_CODE:
3163 ret = gst_ps_demux_parse_sys_head (demux);
3165 case ID_PS_END_CODE:
3166 /* Skip final 4 bytes */
3167 gst_adapter_flush (demux->adapter, 4);
3168 ADAPTER_OFFSET_FLUSH (4);
3171 case ID_PS_PROGRAM_STREAM_MAP:
3172 ret = gst_ps_demux_parse_psm (demux);
3175 if (gst_ps_demux_is_pes_sync (demux->last_sync_code)) {
3176 ret = gst_pes_filter_process (&demux->filter);
3178 GST_DEBUG_OBJECT (demux, "sync_code=%08x, non PES sync found"
3179 ", continuing", demux->last_sync_code);
3181 ret = GST_FLOW_LOST_SYNC;
3185 /* if we found a ps sync, we stop saving the data, any non-ps sync gets
3186 * saved up to the next ps sync. */
3190 case GST_FLOW_NEED_MORE_DATA:
3191 GST_DEBUG_OBJECT (demux, "need more data");
3194 case GST_FLOW_LOST_SYNC:
3195 if (!save || demux->sink_segment.rate >= 0.0) {
3196 GST_DEBUG_OBJECT (demux, "flushing 3 bytes");
3197 gst_adapter_flush (demux->adapter, 3);
3198 ADAPTER_OFFSET_FLUSH (3);
3200 GST_DEBUG_OBJECT (demux, "saving 3 bytes");
3201 gst_adapter_push (demux->rev_adapter,
3202 gst_adapter_take_buffer (demux->adapter, 3));
3207 ret = gst_ps_demux_combine_flows (demux, ret);
3208 if (ret != GST_FLOW_OK)
3217 static GstStateChangeReturn
3218 gst_ps_demux_change_state (GstElement * element, GstStateChange transition)
3220 GstPsDemux *demux = GST_PS_DEMUX (element);
3221 GstStateChangeReturn result;
3222 switch (transition) {
3223 case GST_STATE_CHANGE_NULL_TO_READY:
3224 gst_pes_filter_init (&demux->filter, demux->adapter,
3225 &demux->adapter_offset);
3226 gst_pes_filter_set_callbacks (&demux->filter,
3227 (GstPESFilterData) gst_ps_demux_data_cb,
3228 (GstPESFilterResync) gst_ps_demux_resync_cb, demux);
3229 demux->filter.gather_pes = TRUE;
3231 case GST_STATE_CHANGE_READY_TO_PAUSED:
3237 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3238 switch (transition) {
3239 case GST_STATE_CHANGE_PAUSED_TO_READY:
3240 gst_ps_demux_reset (demux);
3242 case GST_STATE_CHANGE_READY_TO_NULL:
3243 gst_pes_filter_uninit (&demux->filter);
3253 gst_segment_set_position (GstSegment * segment, GstFormat format,
3256 if (segment->format == GST_FORMAT_UNDEFINED) {
3257 segment->format = format;
3259 segment->position = position;
3263 gst_segment_set_duration (GstSegment * segment, GstFormat format,
3266 if (segment->format == GST_FORMAT_UNDEFINED) {
3267 segment->format = format;
3269 segment->duration = duration;