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 DEFAULT_GAP_THRESHOLD (300*GST_MSECOND)
63 #define VIDEO_GAP_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 GST_DEBUG_CATEGORY_EXTERN (mpegpspesfilter_debug);
86 /* MPEG2Demux signals and args */
100 #define DEFAULT_IGNORE_SCR FALSE
102 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
105 GST_STATIC_CAPS ("video/mpeg, "
106 "mpegversion = (int) { 1, 2 }, "
107 "systemstream = (boolean) TRUE;" "video/x-cdxa")
110 static GstStaticPadTemplate video_template =
111 GST_STATIC_PAD_TEMPLATE ("video_%02x",
114 GST_STATIC_CAPS ("video/mpeg, "
115 "mpegversion = (int) { 1, 2, 4 }, " "systemstream = (boolean) FALSE, "
116 "parsed = (boolean) FALSE; " "video/x-h264, "
117 "stream-format=(string)byte-stream; " "video/x-h265, "
118 "stream-format=(string)byte-stream;")
121 static GstStaticPadTemplate audio_template =
122 GST_STATIC_PAD_TEMPLATE ("audio_%02x",
125 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1;"
126 "audio/mpeg, mpegversion = (int) 4, stream-format = (string) { adts, loas };"
127 "audio/x-private1-lpcm; "
128 "audio/x-private1-ac3;" "audio/x-private1-dts;" "audio/ac3")
131 static GstStaticPadTemplate subpicture_template =
132 GST_STATIC_PAD_TEMPLATE ("subpicture_%02x",
135 GST_STATIC_CAPS ("subpicture/x-dvd")
138 static GstStaticPadTemplate private_template =
139 GST_STATIC_PAD_TEMPLATE ("private_%d",
142 GST_STATIC_CAPS_ANY);
144 static void gst_ps_demux_base_init (GstPsDemuxClass * klass);
145 static void gst_ps_demux_class_init (GstPsDemuxClass * klass);
146 static void gst_ps_demux_init (GstPsDemux * demux);
147 static void gst_ps_demux_finalize (GstPsDemux * demux);
148 static void gst_ps_demux_set_property (GObject * object, guint prop_id,
149 const GValue * value, GParamSpec * pspec);
150 static void gst_ps_demux_get_property (GObject * object, guint prop_id,
151 GValue * value, GParamSpec * pspec);
152 static void gst_ps_demux_reset (GstPsDemux * demux);
154 static gboolean gst_ps_demux_sink_event (GstPad * pad, GstObject * parent,
156 static GstFlowReturn gst_ps_demux_chain (GstPad * pad, GstObject * parent,
158 static gboolean gst_ps_demux_sink_activate (GstPad * sinkpad,
160 static gboolean gst_ps_demux_sink_activate_mode (GstPad * pad,
161 GstObject * parent, GstPadMode mode, gboolean active);
162 static void gst_ps_demux_loop (GstPad * pad);
164 static gboolean gst_ps_demux_src_event (GstPad * pad, GstObject * parent,
166 static gboolean gst_ps_demux_src_query (GstPad * pad, GstObject * parent,
169 static GstStateChangeReturn gst_ps_demux_change_state (GstElement * element,
170 GstStateChange transition);
172 static inline gboolean gst_ps_demux_scan_forward_ts (GstPsDemux * demux,
173 guint64 * pos, SCAN_MODE mode, guint64 * rts, gint limit);
174 static inline gboolean gst_ps_demux_scan_backward_ts (GstPsDemux * demux,
175 guint64 * pos, SCAN_MODE mode, guint64 * rts, gint limit);
177 static inline void gst_ps_demux_send_gap_updates (GstPsDemux * demux,
178 GstClockTime new_time);
179 static inline void gst_ps_demux_clear_times (GstPsDemux * demux);
181 static void gst_ps_demux_reset_psm (GstPsDemux * demux);
182 static void gst_ps_demux_flush (GstPsDemux * demux);
184 static GstElementClass *parent_class = NULL;
186 static void gst_segment_set_position (GstSegment * segment, GstFormat format,
188 static void gst_segment_set_duration (GstSegment * segment, GstFormat format,
191 /*static guint gst_ps_demux_signals[LAST_SIGNAL] = { 0 };*/
194 gst_ps_demux_get_type (void)
196 static GType ps_demux_type = 0;
198 if (!ps_demux_type) {
199 static const GTypeInfo ps_demux_info = {
200 sizeof (GstPsDemuxClass),
201 (GBaseInitFunc) gst_ps_demux_base_init,
203 (GClassInitFunc) gst_ps_demux_class_init,
208 (GInstanceInitFunc) gst_ps_demux_init,
213 g_type_register_static (GST_TYPE_ELEMENT, "GstMpegPSDemux",
216 GST_DEBUG_CATEGORY_INIT (gstflupsdemux_debug, "mpegpsdemux", 0,
217 "MPEG program stream demultiplexer element");
220 return ps_demux_type;
223 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (mpegpsdemux, "mpegpsdemux",
224 GST_RANK_PRIMARY, GST_TYPE_PS_DEMUX,
225 GST_DEBUG_CATEGORY_INIT (mpegpspesfilter_debug, "mpegpspesfilter", 0,
226 "MPEG-PS PES filter"));
229 gst_ps_demux_base_init (GstPsDemuxClass * klass)
231 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
233 klass->sink_template = gst_static_pad_template_get (&sink_template);
234 klass->video_template = gst_static_pad_template_get (&video_template);
235 klass->audio_template = gst_static_pad_template_get (&audio_template);
236 klass->subpicture_template =
237 gst_static_pad_template_get (&subpicture_template);
238 klass->private_template = gst_static_pad_template_get (&private_template);
240 gst_element_class_add_pad_template (element_class, klass->video_template);
241 gst_element_class_add_pad_template (element_class, klass->audio_template);
242 gst_element_class_add_pad_template (element_class,
243 klass->subpicture_template);
244 gst_element_class_add_pad_template (element_class, klass->private_template);
245 gst_element_class_add_pad_template (element_class, klass->sink_template);
247 gst_element_class_set_static_metadata (element_class,
248 "MPEG Program Stream Demuxer", "Codec/Demuxer",
249 "Demultiplexes MPEG Program Streams", "Wim Taymans <wim@fluendo.com>");
253 gst_ps_demux_class_init (GstPsDemuxClass * klass)
255 GObjectClass *gobject_class;
256 GstElementClass *gstelement_class;
258 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
260 gobject_class = (GObjectClass *) klass;
261 gstelement_class = (GstElementClass *) klass;
263 gobject_class->finalize = (GObjectFinalizeFunc) gst_ps_demux_finalize;
264 gobject_class->set_property = gst_ps_demux_set_property;
265 gobject_class->get_property = gst_ps_demux_get_property;
267 gstelement_class->change_state = gst_ps_demux_change_state;
270 * GstPsDemux:ignore-scr:
272 * Ignore SCR (System Clock Reference) data from MPEG-PS Pack Header.
273 * This can help with playback of some broken files.
277 g_object_class_install_property (gobject_class, PROP_IGNORE_SCR,
278 g_param_spec_boolean ("ignore-scr", "Ignore SCR data for timing",
279 "Ignore SCR data for timing", DEFAULT_IGNORE_SCR,
280 G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
281 G_PARAM_STATIC_STRINGS));
285 gst_ps_demux_init (GstPsDemux * demux)
287 GstPsDemuxClass *klass = GST_PS_DEMUX_GET_CLASS (demux);
289 demux->sinkpad = gst_pad_new_from_template (klass->sink_template, "sink");
290 gst_pad_set_event_function (demux->sinkpad,
291 GST_DEBUG_FUNCPTR (gst_ps_demux_sink_event));
292 gst_pad_set_chain_function (demux->sinkpad,
293 GST_DEBUG_FUNCPTR (gst_ps_demux_chain));
294 gst_pad_set_activate_function (demux->sinkpad,
295 GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate));
296 gst_pad_set_activatemode_function (demux->sinkpad,
297 GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate_mode));
299 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
302 g_malloc0 (sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
303 demux->streams_found =
304 g_malloc0 (sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
305 demux->found_count = 0;
307 demux->adapter = gst_adapter_new ();
308 demux->rev_adapter = gst_adapter_new ();
309 demux->flowcombiner = gst_flow_combiner_new ();
311 gst_ps_demux_reset (demux);
313 demux->ignore_scr = DEFAULT_IGNORE_SCR;
317 gst_ps_demux_finalize (GstPsDemux * demux)
319 gst_ps_demux_reset (demux);
320 g_free (demux->streams);
321 g_free (demux->streams_found);
323 gst_flow_combiner_free (demux->flowcombiner);
324 g_object_unref (demux->adapter);
325 g_object_unref (demux->rev_adapter);
327 G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (demux));
331 gst_ps_demux_set_property (GObject * object, guint prop_id,
332 const GValue * value, GParamSpec * pspec)
334 GstPsDemux *demux = GST_PS_DEMUX (object);
337 case PROP_IGNORE_SCR:
338 demux->ignore_scr = g_value_get_boolean (value);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 gst_ps_demux_get_property (GObject * object, guint prop_id,
347 GValue * value, GParamSpec * pspec)
349 GstPsDemux *demux = GST_PS_DEMUX (object);
352 case PROP_IGNORE_SCR:
353 g_value_set_boolean (value, demux->ignore_scr);
356 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 gst_ps_demux_reset (GstPsDemux * demux)
363 /* Clean up the streams and pads we allocated */
366 for (i = 0; i < GST_PS_DEMUX_MAX_STREAMS; i++) {
367 GstPsStream *stream = demux->streams[i];
369 if (stream != NULL) {
370 if (stream->pad && GST_PAD_PARENT (stream->pad)) {
371 gst_flow_combiner_remove_pad (demux->flowcombiner, stream->pad);
372 gst_element_remove_pad (GST_ELEMENT_CAST (demux), stream->pad);
374 gst_object_unref (stream->pad);
377 if (stream->pending_tags)
378 gst_tag_list_unref (stream->pending_tags);
380 demux->streams[i] = NULL;
383 memset (demux->streams_found, 0,
384 sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
385 demux->found_count = 0;
387 gst_adapter_clear (demux->adapter);
388 gst_adapter_clear (demux->rev_adapter);
390 demux->adapter_offset = G_MAXUINT64;
391 demux->first_scr = G_MAXUINT64;
392 demux->last_scr = G_MAXUINT64;
393 demux->current_scr = G_MAXUINT64;
394 demux->base_time = G_MAXUINT64;
395 demux->scr_rate_n = G_MAXUINT64;
396 demux->scr_rate_d = G_MAXUINT64;
397 demux->first_pts = G_MAXUINT64;
398 demux->last_pts = G_MAXUINT64;
399 demux->mux_rate = G_MAXUINT64;
400 demux->next_pts = G_MAXUINT64;
401 demux->next_dts = G_MAXUINT64;
402 demux->need_no_more_pads = TRUE;
403 gst_ps_demux_reset_psm (demux);
404 gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
405 gst_segment_init (&demux->src_segment, GST_FORMAT_TIME);
406 gst_ps_demux_flush (demux);
407 demux->have_group_id = FALSE;
408 demux->group_id = G_MAXUINT;
412 gst_ps_demux_create_stream (GstPsDemux * demux, gint id, gint stream_type,
416 GstPadTemplate *template;
418 GstPsDemuxClass *klass = GST_PS_DEMUX_GET_CLASS (demux);
420 GstClockTime gap_threshold = DEFAULT_GAP_THRESHOLD;
428 GST_DEBUG_OBJECT (demux, "create stream id 0x%02x, type 0x%02x", id,
431 switch (stream_type) {
435 case ST_GST_VIDEO_MPEG1_OR_2:
437 gint mpeg_version = 1;
438 if (stream_type == ST_VIDEO_MPEG2 ||
439 (stream_type == ST_GST_VIDEO_MPEG1_OR_2 && demux->is_mpeg2_pack)) {
442 if (stream_type == ST_VIDEO_MPEG4) {
446 template = klass->video_template;
447 name = g_strdup_printf ("video_%02x", id);
448 caps = gst_caps_new_simple ("video/mpeg",
449 "mpegversion", G_TYPE_INT, mpeg_version,
450 "systemstream", G_TYPE_BOOLEAN, FALSE,
451 "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
452 gap_threshold = VIDEO_GAP_THRESHOLD;
457 template = klass->audio_template;
458 name = g_strdup_printf ("audio_%02x", id);
460 caps = gst_caps_new_simple ("audio/mpeg",
461 "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer, NULL);
463 caps = gst_caps_new_simple ("audio/mpeg",
464 "mpegversion", G_TYPE_INT, 1, NULL);
467 case ST_PRIVATE_SECTIONS:
468 case ST_PRIVATE_DATA:
472 case ST_AUDIO_AAC_ADTS:
473 template = klass->audio_template;
474 name = g_strdup_printf ("audio_%02x", id);
475 caps = gst_caps_new_simple ("audio/mpeg",
476 "mpegversion", G_TYPE_INT, 4,
477 "stream-format", G_TYPE_STRING, "adts", NULL);
479 case ST_AUDIO_AAC_LOAS: // LATM/LOAS AAC syntax
480 template = klass->audio_template;
481 name = g_strdup_printf ("audio_%02x", id);
482 caps = gst_caps_new_simple ("audio/mpeg",
483 "mpegversion", G_TYPE_INT, 4,
484 "stream-format", G_TYPE_STRING, "loas", NULL);
487 template = klass->video_template;
488 name = g_strdup_printf ("video_%02x", id);
489 caps = gst_caps_new_simple ("video/x-h264",
490 "stream-format", G_TYPE_STRING, "byte-stream", NULL);
491 gap_threshold = VIDEO_GAP_THRESHOLD;
494 template = klass->video_template;
495 name = g_strdup_printf ("video_%02x", id);
496 caps = gst_caps_new_simple ("video/x-h265",
497 "stream-format", G_TYPE_STRING, "byte-stream", NULL);
498 gap_threshold = VIDEO_GAP_THRESHOLD;
501 case ST_PS_AUDIO_AC3:
502 template = klass->audio_template;
503 name = g_strdup_printf ("audio_%02x", id);
504 caps = gst_caps_new_empty_simple ("audio/x-private1-ac3");
506 case ST_PS_AUDIO_DTS:
507 template = klass->audio_template;
508 name = g_strdup_printf ("audio_%02x", id);
509 caps = gst_caps_new_empty_simple ("audio/x-private1-dts");
511 case ST_PS_AUDIO_LPCM:
512 template = klass->audio_template;
513 name = g_strdup_printf ("audio_%02x", id);
514 caps = gst_caps_new_empty_simple ("audio/x-private1-lpcm");
516 case ST_PS_DVD_SUBPICTURE:
517 template = klass->subpicture_template;
518 name = g_strdup_printf ("subpicture_%02x", id);
519 caps = gst_caps_new_empty_simple ("subpicture/x-dvd");
521 case ST_GST_AUDIO_RAWA52:
522 template = klass->audio_template;
523 name = g_strdup_printf ("audio_%02x", id);
524 caps = gst_caps_new_empty_simple ("audio/ac3");
530 if (name == NULL || template == NULL || caps == NULL) {
533 gst_caps_unref (caps);
537 stream = g_new0 (GstPsStream, 1);
539 stream->discont = TRUE;
540 stream->need_segment = TRUE;
541 stream->notlinked = FALSE;
542 stream->type = stream_type;
543 stream->pending_tags = NULL;
544 stream->pad = gst_pad_new_from_template (template, name);
545 stream->gap_threshold = gap_threshold;
546 stream->nb_out_buffers = 0;
547 stream->gap_ref_buffers = 0;
548 stream->gap_ref_pts = GST_CLOCK_TIME_NONE;
550 gst_pad_set_event_function (stream->pad,
551 GST_DEBUG_FUNCPTR (gst_ps_demux_src_event));
552 gst_pad_set_query_function (stream->pad,
553 GST_DEBUG_FUNCPTR (gst_ps_demux_src_query));
554 gst_pad_use_fixed_caps (stream->pad);
556 /* needed for set_caps to work */
557 if (!gst_pad_set_active (stream->pad, TRUE)) {
558 GST_WARNING_OBJECT (demux, "Failed to activate pad %" GST_PTR_FORMAT,
563 gst_pad_create_stream_id_printf (stream->pad, GST_ELEMENT_CAST (demux),
566 event = gst_pad_get_sticky_event (demux->sinkpad, GST_EVENT_STREAM_START, 0);
568 if (gst_event_parse_group_id (event, &demux->group_id))
569 demux->have_group_id = TRUE;
571 demux->have_group_id = FALSE;
572 gst_event_unref (event);
573 } else if (!demux->have_group_id) {
574 demux->have_group_id = TRUE;
575 demux->group_id = gst_util_group_id_next ();
577 event = gst_event_new_stream_start (stream_id);
578 if (demux->have_group_id)
579 gst_event_set_group_id (event, demux->group_id);
581 gst_pad_push_event (stream->pad, event);
584 gst_pad_set_caps (stream->pad, caps);
586 if (!stream->pending_tags)
587 stream->pending_tags = gst_tag_list_new_empty ();
588 gst_pb_utils_add_codec_description_to_tag_list (stream->pending_tags, NULL,
591 GST_DEBUG_OBJECT (demux, "create pad %s, caps %" GST_PTR_FORMAT, name, caps);
592 gst_caps_unref (caps);
599 gst_ps_demux_get_stream (GstPsDemux * demux, gint id, gint type, gint layer)
601 GstPsStream *stream = demux->streams[id];
603 if (stream == NULL) {
604 if (!(stream = gst_ps_demux_create_stream (demux, id, type, layer)))
607 GST_DEBUG_OBJECT (demux, "adding pad for stream id 0x%02x type 0x%02x", id,
610 demux->streams[id] = stream;
611 demux->streams_found[demux->found_count++] = stream;
613 if (demux->need_no_more_pads) {
614 gst_element_add_pad (GST_ELEMENT (demux), stream->pad);
615 gst_flow_combiner_add_pad (demux->flowcombiner, stream->pad);
617 /* only likely to confuse decodebin etc, so discard */
618 /* FIXME should perform full switch protocol:
619 * add a whole new set of pads, drop old and no-more-pads again */
620 GST_DEBUG_OBJECT (demux,
621 "but already signalled no-more-pads; not adding");
622 gst_object_ref_sink (stream->pad);
630 GST_DEBUG_OBJECT (demux, "unknown stream id 0x%02x type 0x%02x", id, type);
636 gst_ps_demux_get_stream_from_pad (GstPsDemux * demux, GstPad * srcpad)
640 count = demux->found_count;
641 for (i = 0; i < count; i++) {
642 GstPsStream *stream = demux->streams_found[i];
644 if (stream && stream->pad == srcpad)
648 GST_DEBUG_OBJECT (srcpad, "no stream found for pad!");
653 gst_ps_demux_send_segment (GstPsDemux * demux, GstPsStream * stream,
657 if (G_UNLIKELY (stream->need_segment)) {
659 GstEvent *segment_event;
661 GST_DEBUG ("PTS timestamp:%" GST_TIME_FORMAT " base_time %" GST_TIME_FORMAT
662 " src_segment.start:%" GST_TIME_FORMAT " .stop:%" GST_TIME_FORMAT,
663 GST_TIME_ARGS (pts), GST_TIME_ARGS (demux->base_time),
664 GST_TIME_ARGS (demux->src_segment.start),
665 GST_TIME_ARGS (demux->src_segment.stop));
667 /* we should be in sync with downstream, so start from our segment notion,
668 * which also includes proper base_time etc, tweak it a bit and send */
669 gst_segment_copy_into (&demux->src_segment, &segment);
670 if (GST_CLOCK_TIME_IS_VALID (demux->base_time)) {
671 if (GST_CLOCK_TIME_IS_VALID (segment.start))
672 segment.start += demux->base_time;
673 if (GST_CLOCK_TIME_IS_VALID (segment.stop))
674 segment.stop += demux->base_time;
675 segment.time = segment.start - demux->base_time;
678 segment_event = gst_event_new_segment (&segment);
679 if (demux->segment_seqnum)
680 gst_event_set_seqnum (segment_event, demux->segment_seqnum);
682 demux->segment_seqnum = gst_event_get_seqnum (segment_event);
683 GST_INFO_OBJECT (demux, "sending segment event %" GST_SEGMENT_FORMAT
684 " to pad %" GST_PTR_FORMAT, &segment, stream->pad);
686 gst_pad_push_event (stream->pad, segment_event);
688 stream->need_segment = FALSE;
691 if (G_UNLIKELY (stream->pending_tags)) {
692 GST_DEBUG_OBJECT (demux, "Sending pending_tags %p for pad %s:%s : %"
693 GST_PTR_FORMAT, stream->pending_tags,
694 GST_DEBUG_PAD_NAME (stream->pad), stream->pending_tags);
695 gst_pad_push_event (stream->pad, gst_event_new_tag (stream->pending_tags));
696 stream->pending_tags = NULL;
701 gst_ps_demux_send_data (GstPsDemux * demux, GstPsStream * stream,
704 GstFlowReturn result;
705 GstClockTime pts = GST_CLOCK_TIME_NONE, dts = GST_CLOCK_TIME_NONE;
712 if (G_UNLIKELY (demux->next_pts != G_MAXUINT64))
713 pts = MPEGTIME_TO_GSTTIME (demux->next_pts);
714 if (G_UNLIKELY (demux->next_dts != G_MAXUINT64))
715 dts = MPEGTIME_TO_GSTTIME (demux->next_dts);
717 gst_ps_demux_send_segment (demux, stream, pts);
719 /* Ignores DTS if PTS < DTS. Maybe additional sanity checking is possible
720 * by comparing 33bits timestap rollover case, but PTS < DTS is already
722 if (GST_CLOCK_TIME_IS_VALID (pts) && GST_CLOCK_TIME_IS_VALID (dts) &&
724 GST_WARNING_OBJECT (demux, "PTS (%" GST_TIME_FORMAT ") < DTS (%"
725 GST_TIME_FORMAT ")", GST_TIME_ARGS (pts), GST_TIME_ARGS (dts));
726 dts = GST_CLOCK_TIME_NONE;
729 /* OK, sent new segment now prepare the buffer for sending */
730 GST_BUFFER_PTS (buf) = pts;
731 GST_BUFFER_DTS (buf) = dts;
733 /* If we have no DTS but a PTS that means both are the same,
734 * if we have neither than we don't know the current position */
736 if (ts == GST_CLOCK_TIME_NONE)
739 /* update position in the segment */
740 if (ts != GST_CLOCK_TIME_NONE && (stream->last_ts == GST_CLOCK_TIME_NONE
741 || stream->last_ts < ts)) {
742 GST_LOG_OBJECT (demux,
743 "last_ts update on pad %s to time %" GST_TIME_FORMAT
744 ", current scr is %" GST_TIME_FORMAT, GST_PAD_NAME (stream->pad),
746 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->current_scr)));
747 stream->last_ts = ts;
748 if (demux->src_segment.position == GST_CLOCK_TIME_NONE
749 || stream->last_ts > demux->src_segment.position)
750 gst_segment_set_position (&demux->src_segment, GST_FORMAT_TIME,
754 /* Set the buffer discont flag, and clear discont state on the stream */
755 if (stream->discont) {
756 GST_DEBUG_OBJECT (demux, "discont buffer to pad %" GST_PTR_FORMAT
757 " with PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT,
758 stream->pad, GST_TIME_ARGS (pts), GST_TIME_ARGS (dts));
759 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
761 stream->discont = FALSE;
763 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
766 demux->next_pts = G_MAXUINT64;
767 demux->next_dts = G_MAXUINT64;
769 GST_LOG_OBJECT (demux, "pushing stream id 0x%02x type 0x%02x, pts time: %"
770 GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT,
771 stream->id, stream->type, GST_TIME_ARGS (pts), gst_buffer_get_size (buf));
772 result = gst_pad_push (stream->pad, buf);
773 stream->nb_out_buffers += 1;
775 GST_LOG_OBJECT (demux, "result: %s", gst_flow_get_name (result));
782 GST_DEBUG_OBJECT (demux, "no stream given");
783 gst_buffer_unref (buf);
789 gst_ps_demux_mark_discont (GstPsDemux * demux, gboolean discont,
790 gboolean need_segment)
792 gint i, count = demux->found_count;
794 /* mark discont on all streams */
795 for (i = 0; i < count; i++) {
796 GstPsStream *stream = demux->streams_found[i];
798 if (G_LIKELY (stream)) {
799 stream->discont |= discont;
800 stream->need_segment |= need_segment;
802 demux->segment_seqnum = 0;
803 GST_DEBUG_OBJECT (demux, "marked stream as discont %d, need_segment %d",
804 stream->discont, stream->need_segment);
810 gst_ps_demux_send_event (GstPsDemux * demux, GstEvent * event)
812 gint i, count = demux->found_count;
813 gboolean ret = FALSE;
815 for (i = 0; i < count; i++) {
816 GstPsStream *stream = demux->streams_found[i];
819 if (!gst_pad_push_event (stream->pad, gst_event_ref (event))) {
820 GST_DEBUG_OBJECT (stream->pad, "%s event was not handled",
821 GST_EVENT_TYPE_NAME (event));
823 /* If at least one push returns TRUE, then we return TRUE. */
824 GST_DEBUG_OBJECT (stream->pad, "%s event was handled",
825 GST_EVENT_TYPE_NAME (event));
831 gst_event_unref (event);
836 gst_ps_demux_handle_dvd_event (GstPsDemux * demux, GstEvent * event)
838 const GstStructure *structure = gst_event_get_structure (event);
839 const char *type = gst_structure_get_string (structure, "event");
841 gchar cur_stream_name[32];
842 GstPsStream *temp = NULL;
843 const gchar *lang_code;
845 if (strcmp (type, "dvd-lang-codes") == 0) {
846 GST_DEBUG_OBJECT (demux, "Handling language codes event");
848 /* Create a video pad to ensure have it before emit no more pads */
849 (void) gst_ps_demux_get_stream (demux, 0xe0, ST_VIDEO_MPEG2, 0);
851 /* Read out the languages for audio streams and request each one that
853 for (i = 0; i < MAX_DVD_AUDIO_STREAMS; i++) {
857 g_snprintf (cur_stream_name, 32, "audio-%d-format", i);
858 if (!gst_structure_get_int (structure, cur_stream_name, &stream_format))
861 g_snprintf (cur_stream_name, 32, "audio-%d-stream", i);
862 if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
864 if (stream_id < 0 || stream_id >= MAX_DVD_AUDIO_STREAMS)
867 switch (stream_format) {
871 GST_DEBUG_OBJECT (demux,
872 "Audio stream %d format %d ID 0x%02x - AC3", i,
873 stream_format, stream_id);
874 temp = gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_AC3, 0);
878 /* MPEG audio without and with extension stream are
879 * treated the same */
881 GST_DEBUG_OBJECT (demux,
882 "Audio stream %d format %d ID 0x%02x - MPEG audio", i,
883 stream_format, stream_id);
884 temp = gst_ps_demux_get_stream (demux, stream_id, ST_AUDIO_MPEG1, 0);
889 GST_DEBUG_OBJECT (demux,
890 "Audio stream %d format %d ID 0x%02x - DVD LPCM", i,
891 stream_format, stream_id);
893 gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_LPCM, 0);
898 GST_DEBUG_OBJECT (demux,
899 "Audio stream %d format %d ID 0x%02x - DTS", i,
900 stream_format, stream_id);
901 temp = gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_DTS, 0);
904 /* FIXME: What range is SDDS? */
906 GST_WARNING_OBJECT (demux,
907 "Unknown audio stream format in language code event: %d",
916 g_snprintf (cur_stream_name, 32, "audio-%d-language", i);
917 lang_code = gst_structure_get_string (structure, cur_stream_name);
919 GstTagList *list = temp->pending_tags;
922 list = gst_tag_list_new_empty ();
923 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
924 GST_TAG_LANGUAGE_CODE, lang_code, NULL);
925 temp->pending_tags = list;
929 /* And subtitle streams */
930 for (i = 0; i < MAX_DVD_SUBPICTURE_STREAMS; i++) {
933 g_snprintf (cur_stream_name, 32, "subpicture-%d-format", i);
934 if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
937 g_snprintf (cur_stream_name, 32, "subpicture-%d-stream", i);
938 if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
940 if (stream_id < 0 || stream_id >= MAX_DVD_SUBPICTURE_STREAMS)
943 GST_DEBUG_OBJECT (demux, "Subpicture stream %d ID 0x%02x", i,
946 /* Retrieve the subpicture stream to force pad creation */
947 temp = gst_ps_demux_get_stream (demux, 0x20 + stream_id,
948 ST_PS_DVD_SUBPICTURE, 0);
952 g_snprintf (cur_stream_name, 32, "subpicture-%d-language", i);
953 lang_code = gst_structure_get_string (structure, cur_stream_name);
955 GstTagList *list = temp->pending_tags;
958 list = gst_tag_list_new_empty ();
959 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
960 GST_TAG_LANGUAGE_CODE, lang_code, NULL);
961 temp->pending_tags = list;
965 GST_DEBUG_OBJECT (demux, "Created all pads from Language Codes event, "
966 "signalling no-more-pads");
968 gst_element_no_more_pads (GST_ELEMENT (demux));
969 demux->need_no_more_pads = FALSE;
971 /* forward to all pads, e.g. dvd clut event */
972 gst_event_ref (event);
973 gst_ps_demux_send_event (demux, event);
976 gst_event_unref (event);
981 gst_ps_demux_flush (GstPsDemux * demux)
983 GST_DEBUG_OBJECT (demux, "flushing demuxer");
984 gst_adapter_clear (demux->adapter);
985 gst_adapter_clear (demux->rev_adapter);
986 gst_pes_filter_drain (&demux->filter);
987 gst_ps_demux_clear_times (demux);
988 demux->adapter_offset = G_MAXUINT64;
989 demux->current_scr = G_MAXUINT64;
990 demux->bytes_since_scr = 0;
994 gst_ps_demux_clear_times (GstPsDemux * demux)
996 gint i, count = demux->found_count;
998 gst_flow_combiner_reset (demux->flowcombiner);
999 /* Clear the last ts for all streams */
1000 for (i = 0; i < count; i++) {
1001 GstPsStream *stream = demux->streams_found[i];
1003 if (G_LIKELY (stream)) {
1004 stream->last_ts = GST_CLOCK_TIME_NONE;
1010 gst_ps_demux_send_gap_updates (GstPsDemux * demux, GstClockTime time)
1012 GstClockTime base_time, stop;
1013 gint i, count = demux->found_count;
1014 GstEvent *event = NULL;
1016 if (time == GST_CLOCK_TIME_NONE)
1019 /* Advance all lagging streams by sending a gap event */
1020 if ((base_time = demux->base_time) == GST_CLOCK_TIME_NONE)
1023 stop = demux->src_segment.stop;
1024 if (stop != GST_CLOCK_TIME_NONE)
1030 /* FIXME: Handle reverse playback */
1031 for (i = 0; i < count; i++) {
1032 GstPsStream *stream = demux->streams_found[i];
1035 if (stream->last_ts == GST_CLOCK_TIME_NONE ||
1036 stream->last_ts < demux->src_segment.start + base_time)
1037 stream->last_ts = demux->src_segment.start + base_time;
1039 if (stream->last_ts + stream->gap_threshold < time &&
1040 stream->nb_out_buffers == stream->gap_ref_buffers &&
1041 stream->gap_ref_pts != stream->last_ts) {
1042 /* should send segment info before gap event */
1043 gst_ps_demux_send_segment (demux, stream, GST_CLOCK_TIME_NONE);
1045 GST_LOG_OBJECT (demux,
1046 "Sending gap update to pad %s from time %" GST_TIME_FORMAT " to %"
1047 GST_TIME_FORMAT, GST_PAD_NAME (stream->pad),
1048 GST_TIME_ARGS (stream->last_ts), GST_TIME_ARGS (time));
1049 event = gst_event_new_gap (stream->last_ts, time - stream->last_ts);
1050 gst_pad_push_event (stream->pad, event);
1051 stream->last_ts = time;
1054 /* Update GAP tracking vars so we don't re-check this stream for a while */
1055 stream->gap_ref_pts = time;
1056 if (stream->last_ts != GST_CLOCK_TIME_NONE && stream->last_ts > time)
1057 stream->gap_ref_pts = stream->last_ts;
1062 static inline gboolean
1063 have_open_streams (GstPsDemux * demux)
1065 return (demux->streams_found[0] != NULL);
1069 gst_ps_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1071 gboolean res = TRUE;
1072 GstPsDemux *demux = GST_PS_DEMUX (parent);
1074 switch (GST_EVENT_TYPE (event)) {
1075 case GST_EVENT_FLUSH_START:
1076 gst_ps_demux_send_event (demux, event);
1078 case GST_EVENT_FLUSH_STOP:
1079 gst_ps_demux_send_event (demux, event);
1080 gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
1081 gst_ps_demux_flush (demux);
1083 case GST_EVENT_SEGMENT:
1085 const GstSegment *segment;
1087 gst_event_parse_segment (event, &segment);
1088 gst_segment_copy_into (segment, &demux->sink_segment);
1090 GST_INFO_OBJECT (demux, "received segment %" GST_SEGMENT_FORMAT, segment);
1092 /* we need to emit a new segment */
1093 gst_ps_demux_mark_discont (demux, TRUE, TRUE);
1095 if (segment->format == GST_FORMAT_BYTES
1096 && demux->scr_rate_n != G_MAXUINT64
1097 && demux->scr_rate_d != G_MAXUINT64) {
1098 demux->src_segment.rate = segment->rate;
1099 demux->src_segment.applied_rate = segment->applied_rate;
1100 demux->src_segment.format = GST_FORMAT_TIME;
1101 demux->src_segment.start = BYTES_TO_GSTTIME (segment->start);
1102 demux->src_segment.stop = BYTES_TO_GSTTIME (segment->stop);
1103 demux->src_segment.time = BYTES_TO_GSTTIME (segment->time);
1104 } else if (segment->format == GST_FORMAT_TIME) {
1105 /* we expect our timeline (SCR, PTS) to match the one from upstream,
1106 * if not, will adjust with offset later on */
1107 gst_segment_copy_into (segment, &demux->src_segment);
1110 gst_event_unref (event);
1115 GST_INFO_OBJECT (demux, "Received EOS");
1116 if (!gst_ps_demux_send_event (demux, event)
1117 && !have_open_streams (demux)) {
1118 GST_WARNING_OBJECT (demux, "EOS and no streams open");
1119 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
1120 ("Internal data stream error."), ("No valid streams detected"));
1123 case GST_EVENT_CUSTOM_DOWNSTREAM:
1124 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1126 const GstStructure *structure = gst_event_get_structure (event);
1128 if (structure != NULL
1129 && gst_structure_has_name (structure, "application/x-gst-dvd")) {
1130 res = gst_ps_demux_handle_dvd_event (demux, event);
1132 gst_ps_demux_send_event (demux, event);
1136 case GST_EVENT_CAPS:
1137 gst_event_unref (event);
1140 gst_ps_demux_send_event (demux, event);
1148 gst_ps_demux_handle_seek_push (GstPsDemux * demux, GstEvent * event)
1150 gboolean res = FALSE;
1154 GstSeekType start_type, stop_type;
1156 gint64 bstart, bstop;
1159 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
1162 GST_DEBUG_OBJECT (demux, "seek event, rate: %f start: %" GST_TIME_FORMAT
1163 " stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
1164 GST_TIME_ARGS (stop));
1166 if (format == GST_FORMAT_BYTES) {
1167 GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format);
1171 GST_DEBUG_OBJECT (demux, "seek - trying directly upstream first");
1173 /* first try original format seek */
1174 (void) gst_event_ref (event);
1175 if ((res = gst_pad_push_event (demux->sinkpad, event)))
1178 if (format != GST_FORMAT_TIME) {
1179 /* From here down, we only support time based seeks */
1180 GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format);
1184 /* We need to convert to byte based seek and we need a scr_rate for that. */
1185 if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64) {
1186 GST_DEBUG_OBJECT (demux, "seek not possible, no scr_rate");
1190 GST_DEBUG_OBJECT (demux, "try with scr_rate interpolation");
1192 bstart = GSTTIME_TO_BYTES ((guint64) start);
1193 bstop = GSTTIME_TO_BYTES ((guint64) stop);
1195 GST_DEBUG_OBJECT (demux, "in bytes bstart %" G_GINT64_FORMAT " bstop %"
1196 G_GINT64_FORMAT, bstart, bstop);
1197 bevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, start_type,
1198 bstart, stop_type, bstop);
1200 res = gst_pad_push_event (demux->sinkpad, bevent);
1203 gst_event_unref (event);
1208 gst_event_unref (event);
1214 #define MAX_RECURSION_COUNT 100
1216 /* Binary search for requested SCR */
1217 static inline guint64
1218 find_offset (GstPsDemux * demux, guint64 scr,
1219 guint64 min_scr, guint64 min_scr_offset,
1220 guint64 max_scr, guint64 max_scr_offset, int recursion_count)
1222 guint64 scr_rate_n = max_scr_offset - min_scr_offset;
1223 guint64 scr_rate_d = max_scr - min_scr;
1227 if (recursion_count > MAX_RECURSION_COUNT) {
1231 offset = min_scr_offset +
1232 MIN (gst_util_uint64_scale (scr - min_scr, scr_rate_n,
1233 scr_rate_d), demux->sink_segment.stop);
1235 if (!gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0)) {
1236 gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1239 if (fscr == scr || fscr == min_scr || fscr == max_scr) {
1244 return find_offset (demux, scr, fscr, offset, max_scr, max_scr_offset,
1245 recursion_count + 1);
1247 return find_offset (demux, scr, min_scr, min_scr_offset, fscr, offset,
1248 recursion_count + 1);
1252 static inline gboolean
1253 gst_ps_demux_do_seek (GstPsDemux * demux, GstSegment * seeksegment)
1256 guint64 fscr, offset;
1257 guint64 scr = GSTTIME_TO_MPEGTIME (seeksegment->position + demux->base_time);
1259 /* In some clips the PTS values are completely unaligned with SCR values.
1260 * To improve the seek in that situation we apply a factor considering the
1261 * relationship between last PTS and last SCR */
1262 if (demux->last_scr > demux->last_pts)
1263 scr = gst_util_uint64_scale (scr, demux->last_scr, demux->last_pts);
1265 scr = MIN (demux->last_scr, scr);
1266 scr = MAX (demux->first_scr, scr);
1269 GST_INFO_OBJECT (demux, "sink segment configured %" GST_SEGMENT_FORMAT
1270 ", trying to go at SCR: %" G_GUINT64_FORMAT, &demux->sink_segment, scr);
1273 find_offset (demux, scr, demux->first_scr, demux->first_scr_offset,
1274 demux->last_scr, demux->last_scr_offset, 0);
1276 if (offset == (guint64) - 1) {
1280 found = gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1282 found = gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1284 while (found && fscr < scr) {
1286 found = gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1289 while (found && fscr > scr && offset > 0) {
1291 found = gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1294 GST_INFO_OBJECT (demux, "doing seek at offset %" G_GUINT64_FORMAT
1295 " SCR: %" G_GUINT64_FORMAT " %" GST_TIME_FORMAT,
1296 offset, fscr, GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (fscr)));
1298 gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
1304 gst_ps_demux_handle_seek_pull (GstPsDemux * demux, GstEvent * event)
1308 GstSeekType start_type, stop_type;
1311 gboolean update, flush, accurate;
1312 GstSegment seeksegment;
1313 GstClockTime first_pts = MPEGTIME_TO_GSTTIME (demux->first_pts);
1314 guint32 seek_seqnum = gst_event_get_seqnum (event);
1316 gst_event_parse_seek (event, &rate, &format, &flags,
1317 &start_type, &start, &stop_type, &stop);
1319 if (format != GST_FORMAT_TIME)
1322 GST_DEBUG_OBJECT (demux, "Seek requested start %" GST_TIME_FORMAT " stop %"
1323 GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1325 /* We need to convert to byte based seek and we need a scr_rate for that. */
1326 if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64)
1329 flush = flags & GST_SEEK_FLAG_FLUSH;
1330 accurate = flags & GST_SEEK_FLAG_ACCURATE;
1332 /* keyframe = flags & GST_SEEK_FLAG_KEY_UNIT; *//* FIXME */
1335 GstEvent *event = gst_event_new_flush_start ();
1336 gst_event_set_seqnum (event, seek_seqnum);
1337 /* Flush start up and downstream to make sure data flow and loops are
1339 demux->flushing = TRUE;
1340 gst_ps_demux_send_event (demux, event);
1341 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_start ());
1343 /* Pause the pulling task */
1344 gst_pad_pause_task (demux->sinkpad);
1347 /* Take the stream lock */
1348 GST_PAD_STREAM_LOCK (demux->sinkpad);
1351 /* Stop flushing upstream we need to pull */
1352 demux->flushing = FALSE;
1353 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_stop (TRUE));
1356 /* Work on a copy until we are sure the seek succeeded. */
1357 memcpy (&seeksegment, &demux->src_segment, sizeof (GstSegment));
1359 GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
1360 &demux->src_segment);
1362 /* Apply the seek to our segment */
1363 if (!gst_segment_do_seek (&seeksegment, rate, format, flags,
1364 start_type, start, stop_type, stop, &update))
1367 GST_DEBUG_OBJECT (demux, "seek segment configured %" GST_SEGMENT_FORMAT,
1370 if (flush || seeksegment.position != demux->src_segment.position) {
1371 /* Do the actual seeking */
1372 if (!gst_ps_demux_do_seek (demux, &seeksegment)) {
1373 #ifdef TIZEN_FEATURE_MPEGDEMUX_MODIFICATION
1381 /* check the limits */
1382 if (seeksegment.rate > 0.0 && first_pts != G_MAXUINT64
1383 && seeksegment.start < first_pts - demux->base_time) {
1384 seeksegment.position = first_pts - demux->base_time;
1386 seeksegment.start = seeksegment.position;
1389 /* update the rate in our src segment */
1390 demux->sink_segment.rate = rate;
1392 GST_DEBUG_OBJECT (demux, "seek segment adjusted %" GST_SEGMENT_FORMAT,
1396 GstEvent *event = gst_event_new_flush_stop (TRUE);
1397 /* Stop flushing, the sinks are at time 0 now */
1398 gst_event_set_seqnum (event, seek_seqnum);
1399 gst_ps_demux_send_event (demux, event);
1402 if (flush || seeksegment.position != demux->src_segment.position) {
1403 gst_ps_demux_flush (demux);
1406 /* Ok seek succeeded, take the newly configured segment */
1407 memcpy (&demux->src_segment, &seeksegment, sizeof (GstSegment));
1409 /* Notify about the start of a new segment */
1410 if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
1411 gst_element_post_message (GST_ELEMENT (demux),
1412 gst_message_new_segment_start (GST_OBJECT (demux),
1413 demux->src_segment.format, demux->src_segment.position));
1416 /* Tell all the stream a new segment is needed */
1417 gst_ps_demux_mark_discont (demux, TRUE, TRUE);
1419 /* Update the segment_seqnum with the seek event seqnum */
1420 demux->segment_seqnum = seek_seqnum;
1422 gst_pad_start_task (demux->sinkpad,
1423 (GstTaskFunction) gst_ps_demux_loop, demux->sinkpad, NULL);
1425 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
1427 gst_event_unref (event);
1433 GST_WARNING_OBJECT (demux, "we only support seeking in TIME or BYTES "
1435 gst_event_unref (event);
1440 GST_WARNING_OBJECT (demux, "seek not possible, no scr_rate");
1441 gst_event_unref (event);
1446 GST_WARNING_OBJECT (demux, "couldn't perform seek");
1447 #ifdef TIZEN_FEATURE_MPEGDEMUX_MODIFICATION
1448 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
1450 gst_event_unref (event);
1456 gst_ps_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1458 gboolean res = FALSE;
1459 GstPsDemux *demux = GST_PS_DEMUX (parent);
1461 switch (GST_EVENT_TYPE (event)) {
1462 case GST_EVENT_SEEK:
1463 if (demux->random_access) {
1464 res = gst_ps_demux_handle_seek_pull (demux, event);
1466 res = gst_ps_demux_handle_seek_push (demux, event);
1469 case GST_EVENT_RECONFIGURE:{
1470 GstPsStream *stream;
1472 stream = gst_ps_demux_get_stream_from_pad (demux, pad);
1474 stream->notlinked = FALSE;
1476 gst_event_unref (event);
1481 res = gst_pad_push_event (demux->sinkpad, event);
1489 gst_ps_demux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1491 gboolean res = FALSE;
1492 GstPsDemux *demux = GST_PS_DEMUX (parent);
1494 GST_LOG_OBJECT (demux, "Have query of type %d on pad %" GST_PTR_FORMAT,
1495 GST_QUERY_TYPE (query), pad);
1497 switch (GST_QUERY_TYPE (query)) {
1498 case GST_QUERY_POSITION:
1503 /* See if upstream can immediately answer */
1504 res = gst_pad_peer_query (demux->sinkpad, query);
1508 gst_query_parse_position (query, &format, NULL);
1510 if (format != GST_FORMAT_TIME) {
1511 GST_DEBUG_OBJECT (demux, "position not supported for format: %s",
1512 gst_format_get_name (format));
1516 pos = demux->src_segment.position - demux->src_segment.start;
1517 GST_LOG_OBJECT (demux, "Position %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
1519 gst_query_set_position (query, format, pos);
1523 case GST_QUERY_DURATION:
1527 GstQuery *byte_query;
1529 gst_query_parse_duration (query, &format, NULL);
1531 if (G_LIKELY (format == GST_FORMAT_TIME &&
1532 GST_CLOCK_TIME_IS_VALID (demux->src_segment.duration))) {
1533 gst_query_set_duration (query, GST_FORMAT_TIME,
1534 demux->src_segment.duration);
1539 /* For any format other than bytes, see if upstream knows first */
1540 if (format == GST_FORMAT_BYTES) {
1541 GST_DEBUG_OBJECT (demux, "duration not supported for format: %s",
1542 gst_format_get_name (format));
1546 if (gst_pad_peer_query (demux->sinkpad, query)) {
1551 /* Upstream didn't know, so we can only answer TIME queries from
1553 if (format != GST_FORMAT_TIME) {
1554 GST_DEBUG_OBJECT (demux, "duration not supported for format: %s",
1555 gst_format_get_name (format));
1559 if (demux->mux_rate == -1) {
1560 GST_DEBUG_OBJECT (demux, "duration not possible, no mux_rate");
1564 byte_query = gst_query_new_duration (GST_FORMAT_BYTES);
1566 if (!gst_pad_peer_query (demux->sinkpad, byte_query)) {
1567 GST_LOG_OBJECT (demux, "query on peer pad failed");
1568 gst_query_unref (byte_query);
1572 gst_query_parse_duration (byte_query, &format, &duration);
1573 gst_query_unref (byte_query);
1575 GST_LOG_OBJECT (demux,
1576 "query on peer pad reported bytes %" G_GUINT64_FORMAT, duration);
1578 duration = BYTES_TO_GSTTIME ((guint64) duration);
1580 GST_LOG_OBJECT (demux, "converted to time %" GST_TIME_FORMAT,
1581 GST_TIME_ARGS (duration));
1583 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
1587 case GST_QUERY_SEEKING:{
1590 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
1593 if (demux->random_access) {
1594 /* In pull mode we can seek in TIME format if we have the SCR */
1595 if (fmt != GST_FORMAT_TIME || demux->scr_rate_n == G_MAXUINT64
1596 || demux->scr_rate_d == G_MAXUINT64) {
1597 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1600 if (GST_CLOCK_TIME_IS_VALID (demux->src_segment.duration))
1601 dur = demux->src_segment.duration;
1602 gst_query_set_seeking (query, fmt, TRUE, 0, dur);
1605 if (fmt == GST_FORMAT_BYTES) {
1606 /* Seeking in BYTES format not supported at all */
1607 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1609 GstQuery *peerquery;
1612 /* Then ask upstream */
1613 res = gst_pad_peer_query (demux->sinkpad, query);
1615 /* If upstream can handle seeks we're done, if it
1616 * can't we still have our TIME->BYTES conversion seek
1618 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
1619 if (seekable || fmt != GST_FORMAT_TIME)
1623 /* We can seek if upstream supports BYTES seeks and we
1626 peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
1627 res = gst_pad_peer_query (demux->sinkpad, peerquery);
1628 if (!res || demux->scr_rate_n == G_MAXUINT64
1629 || demux->scr_rate_d == G_MAXUINT64) {
1630 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1632 gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
1634 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, -1);
1636 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1639 gst_query_unref (peerquery);
1645 case GST_QUERY_SEGMENT:{
1649 format = demux->src_segment.format;
1652 gst_segment_to_stream_time (&demux->src_segment, format,
1653 demux->src_segment.start);
1654 if ((stop = demux->src_segment.stop) == -1)
1655 stop = demux->src_segment.duration;
1657 stop = gst_segment_to_stream_time (&demux->src_segment, format, stop);
1659 gst_query_set_segment (query, demux->src_segment.rate, format, start,
1665 res = gst_pad_query_default (pad, parent, query);
1676 gst_ps_demux_reset_psm (GstPsDemux * demux)
1680 #define FILL_TYPE(start, stop, type) \
1681 for (i=start; i <= stop; i++) \
1682 demux->psm[i] = type;
1684 /* Initialize all fields to -1 first */
1685 FILL_TYPE (0x00, GST_PS_DEMUX_MAX_PSM - 1, -1);
1687 FILL_TYPE (0x20, 0x3f, ST_PS_DVD_SUBPICTURE);
1689 FILL_TYPE (0x80, 0x87, ST_PS_AUDIO_AC3);
1690 FILL_TYPE (0x88, 0x9f, ST_PS_AUDIO_DTS);
1691 FILL_TYPE (0xa0, 0xaf, ST_PS_AUDIO_LPCM);
1693 FILL_TYPE (0xc0, 0xdf, ST_AUDIO_MPEG1);
1694 FILL_TYPE (0xe0, 0xef, ST_GST_VIDEO_MPEG1_OR_2);
1701 * pack_start_code 32 bslbf -+
1703 * system_clock_reference_base [32..30] 3 bslbf |
1704 * marker_bit 1 bslbf |
1705 * system_clock_reference_base [29..15] 15 bslbf |
1706 * marker_bit 1 bslbf |
1707 * system_clock_reference_base [14..0] 15 bslbf |
1708 * marker_bit 1 bslbf | 112 bits
1709 * system_clock_reference_extension 9 ubslbf |
1710 * marker_bit 1 bslbf |
1711 * program_mux_rate 22 ubslbf |
1712 * marker_bit 1 bslbf |
1713 * marker_bit 1 bslbf |
1714 * reserved 5 bslbf |
1715 * pack_stuffing_length 3 ubslbf -+
1717 * for (i = 0; i < pack_stuffing_length; i++) {
1718 * stuffing_byte '1111 1111' 8 bslbf
1721 * 112 bits = 14 bytes, as max value for pack_stuffing_length is 7, then
1722 * in total it's needed 14 + 7 = 21 bytes.
1724 #define PACK_START_SIZE 21
1726 static GstFlowReturn
1727 gst_ps_demux_parse_pack_start (GstPsDemux * demux)
1732 guint64 scr, scr_adjusted, new_rate;
1735 guint avail = gst_adapter_available (demux->adapter);
1737 GST_LOG ("parsing pack start");
1739 if (G_UNLIKELY (avail < PACK_START_SIZE))
1740 goto need_more_data;
1742 data = gst_adapter_map (demux->adapter, PACK_START_SIZE);
1744 /* skip start code */
1747 scr1 = GST_READ_UINT32_BE (data);
1748 scr2 = GST_READ_UINT32_BE (data + 4);
1750 /* fixed length to begin with, start code and two scr values */
1753 /* start parsing the stream */
1754 if ((*data & 0xc0) == 0x40) {
1757 guint8 stuffing_bytes;
1759 GST_LOG ("Found MPEG2 stream");
1760 demux->is_mpeg2_pack = TRUE;
1762 /* mpeg2 has more data */
1765 /* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
1768 if (G_UNLIKELY ((scr1 & 0xc4000400) != 0x44000400))
1771 scr = ((guint64) scr1 & 0x38000000) << 3;
1772 scr |= ((guint64) scr1 & 0x03fff800) << 4;
1773 scr |= ((guint64) scr1 & 0x000003ff) << 5;
1774 scr |= ((guint64) scr2 & 0xf8000000) >> 27;
1776 /* marker:1==1 ! scr_ext:9 ! marker:1==1 */
1777 if (G_UNLIKELY ((scr2 & 0x04010000) != 0x04010000))
1780 scr_ext = (scr2 & 0x03fe0000) >> 17;
1781 /* We keep the offset of this scr */
1782 demux->cur_scr_offset = demux->adapter_offset + 12;
1784 GST_LOG_OBJECT (demux, "SCR: 0x%08" G_GINT64_MODIFIER "x SCRE: 0x%08x",
1788 scr = (scr * 300 + scr_ext % 300) / 300;
1790 /* SCR has been converted into units of 90Khz ticks to make it comparable
1791 to DTS/PTS, that also implies 1 tick rounding error */
1793 /* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
1794 next32 = GST_READ_UINT32_BE (data);
1795 if (G_UNLIKELY ((next32 & 0x00000300) != 0x00000300))
1798 new_rate = (next32 & 0xfffffc00) >> 10;
1800 stuffing_bytes = (next32 & 0x07);
1801 GST_LOG_OBJECT (demux, "stuffing bytes: %d", stuffing_bytes);
1804 length += stuffing_bytes;
1805 while (stuffing_bytes--) {
1806 if (*data++ != 0xff)
1810 GST_DEBUG ("Found MPEG1 stream");
1811 demux->is_mpeg2_pack = FALSE;
1814 if (G_UNLIKELY ((scr1 & 0xf1000100) != 0x21000100))
1817 if (G_UNLIKELY ((scr2 & 0x01800001) != 0x01800001))
1820 /* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
1821 scr = ((guint64) scr1 & 0x0e000000) << 5;
1822 scr |= ((guint64) scr1 & 0x00fffe00) << 6;
1823 scr |= ((guint64) scr1 & 0x000000ff) << 7;
1824 scr |= ((guint64) scr2 & 0xfe000000) >> 25;
1826 /* We keep the offset of this scr */
1827 demux->cur_scr_offset = demux->adapter_offset + 8;
1829 /* marker:1==1 ! mux_rate:22 ! marker:1==1 */
1830 new_rate = (scr2 & 0x007ffffe) >> 1;
1835 if (demux->ignore_scr) {
1836 /* update only first/current_scr with raw scr value to start streaming
1837 * after parsing 2 seconds long data with no-more-pad */
1838 if (demux->first_scr == G_MAXUINT64) {
1839 demux->first_scr = scr;
1840 demux->first_scr_offset = demux->cur_scr_offset;
1843 demux->current_scr = scr;
1848 new_rate *= MPEG_MUX_RATE_MULT;
1850 /* scr adjusted is the new scr found + the colected adjustment */
1851 scr_adjusted = scr + demux->scr_adjust;
1853 GST_LOG_OBJECT (demux,
1854 "SCR: %" G_GINT64_FORMAT " (%" G_GINT64_FORMAT "), mux_rate %"
1855 G_GINT64_FORMAT ", GStreamer Time:%" GST_TIME_FORMAT,
1856 scr, scr_adjusted, new_rate,
1857 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME ((guint64) scr)));
1859 /* keep the first src in order to calculate delta time */
1860 if (G_UNLIKELY (demux->first_scr == G_MAXUINT64)) {
1863 demux->first_scr = scr;
1864 demux->first_scr_offset = demux->cur_scr_offset;
1865 demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
1866 GST_DEBUG_OBJECT (demux, "determined base_time %" GST_TIME_FORMAT,
1867 GST_TIME_ARGS (demux->base_time));
1868 /* at begin consider the new_rate as the scr rate, bytes/clock ticks */
1869 scr_rate_n = new_rate;
1870 scr_rate_d = CLOCK_FREQ;
1871 /* our SCR timeline might have offset wrt upstream timeline */
1872 if (demux->sink_segment.format == GST_FORMAT_TIME) {
1873 if (demux->sink_segment.start > demux->base_time)
1874 diff = -(demux->sink_segment.start - demux->base_time);
1876 diff = demux->base_time - demux->sink_segment.start;
1877 if (diff > GST_SECOND) {
1878 GST_DEBUG_OBJECT (demux, "diff of %" GST_TIME_FORMAT
1879 " wrt upstream start %" GST_TIME_FORMAT "; adjusting base",
1880 GST_TIME_ARGS (diff), GST_TIME_ARGS (demux->sink_segment.start));
1881 demux->base_time += diff;
1884 } else if (G_LIKELY (demux->first_scr_offset != demux->cur_scr_offset)) {
1885 /* estimate byte rate related to the SCR */
1886 scr_rate_n = demux->cur_scr_offset - demux->first_scr_offset;
1887 scr_rate_d = scr_adjusted - demux->first_scr;
1889 scr_rate_n = demux->scr_rate_n;
1890 scr_rate_d = demux->scr_rate_d;
1893 GST_LOG_OBJECT (demux, "%s mode scr: %" G_GUINT64_FORMAT " at %"
1894 G_GUINT64_FORMAT ", first scr: %" G_GUINT64_FORMAT
1895 " at %" G_GUINT64_FORMAT ", scr rate: %" G_GUINT64_FORMAT
1896 "/%" G_GUINT64_FORMAT "(%f)",
1897 ((demux->sink_segment.rate >= 0.0) ? "forward" : "backward"),
1898 scr, demux->cur_scr_offset,
1899 demux->first_scr, demux->first_scr_offset,
1900 scr_rate_n, scr_rate_d, (float) scr_rate_n / scr_rate_d);
1902 /* adjustment of the SCR */
1903 if (G_LIKELY (demux->current_scr != G_MAXUINT64)) {
1905 guint64 old_scr, old_mux_rate, bss, adjust = 0;
1907 /* keep SCR of the previous packet */
1908 old_scr = demux->current_scr;
1909 old_mux_rate = demux->mux_rate;
1911 /* Bytes since SCR is the amount we placed in the adapter since then
1912 * (demux->bytes_since_scr) minus the amount remaining in the adapter,
1913 * clamped to >= 0 */
1914 bss = MAX (0, (gint) (demux->bytes_since_scr - avail));
1916 /* estimate the new SCR using the previous one according the notes
1917 on point 2.5.2.2 of the ISO/IEC 13818-1 document */
1918 if (old_mux_rate != 0)
1919 adjust = (bss * CLOCK_FREQ) / old_mux_rate;
1921 if (demux->sink_segment.rate >= 0.0)
1922 demux->next_scr = old_scr + adjust;
1924 demux->next_scr = old_scr - adjust;
1926 GST_LOG_OBJECT (demux,
1927 "bss: %" G_GUINT64_FORMAT ", next_scr: %" G_GUINT64_FORMAT
1928 ", old_scr: %" G_GUINT64_FORMAT ", scr: %" G_GUINT64_FORMAT,
1929 bss, demux->next_scr, old_scr, scr_adjusted);
1931 /* calculate the absolute deference between the last scr and
1933 if (G_UNLIKELY (old_scr > scr_adjusted))
1934 diff = old_scr - scr_adjusted;
1936 diff = scr_adjusted - old_scr;
1938 /* if the difference is more than 1 second we need to reconfigure
1940 if (G_UNLIKELY (diff > CLOCK_FREQ)) {
1941 demux->scr_adjust = demux->next_scr - scr;
1942 GST_LOG_OBJECT (demux, "discont found, diff: %" G_GINT64_FORMAT
1943 ", adjust %" G_GINT64_FORMAT, diff, demux->scr_adjust);
1944 scr_adjusted = demux->next_scr;
1945 /* don't update rate estimation on disconts */
1946 scr_rate_n = demux->scr_rate_n;
1947 scr_rate_d = demux->scr_rate_d;
1949 demux->next_scr = scr_adjusted;
1953 /* update the current_scr and rate members */
1954 demux->mux_rate = new_rate;
1955 demux->current_scr = scr_adjusted;
1956 demux->scr_rate_n = scr_rate_n;
1957 demux->scr_rate_d = scr_rate_d;
1959 /* Reset the bytes_since_scr value to count the data remaining in the
1961 demux->bytes_since_scr = avail;
1963 /* Now check for all streams if they're behind the new SCR and if
1964 * they are then move them forward to the SCR position */
1965 gst_ps_demux_send_gap_updates (demux,
1966 MPEGTIME_TO_GSTTIME (demux->current_scr - demux->first_scr));
1969 gst_adapter_unmap (demux->adapter);
1970 gst_adapter_flush (demux->adapter, length);
1971 ADAPTER_OFFSET_FLUSH (length);
1977 GST_DEBUG_OBJECT (demux, "lost sync");
1978 gst_adapter_unmap (demux->adapter);
1979 return GST_FLOW_LOST_SYNC;
1983 GST_DEBUG_OBJECT (demux, "need more data");
1984 return GST_FLOW_NEED_MORE_DATA;
1989 * system_header () {
1990 * system_header_start_code 32 bslbf -+
1991 * header_length 16 uimsbf |
1992 * marker_bit 1 bslbf |
1993 * rate_bound 22 uimsbf |
1994 * marker_bit 1 bslbf |
1995 * audio_bound 6 uimsbf |
1996 * fixed_flag 1 bslbf |
1997 * CSPS_flag 1 bslbf | 96 bits
1998 * system_audio_lock_flag 1 bslbf |
1999 * system_video_lock_flag 1 bslbf |
2000 * marker_bit 1 bslbf |
2001 * video_bound 5 uimsbf |
2002 * packet_rate_restriction_flag 1 bslbf |
2003 * reserved_bits 7 bslbf -+
2004 * while (nextbits () = = '1') {
2005 * stream_id 8 uimsbf -+
2006 * '11' 2 bslbf | 24 bits
2007 * P-STD_buffer_bound_scale 1 bslbf |
2008 * P-STD_buffer_size_bound 13 uimsbf -+
2011 * 96 bits = 12 bytes, 24 bits = 3 bytes.
2014 static GstFlowReturn
2015 gst_ps_demux_parse_sys_head (GstPsDemux * demux)
2019 #ifndef GST_DISABLE_GST_DEBUG
2023 if (gst_adapter_available (demux->adapter) < 6)
2024 goto need_more_data;
2026 /* start code + length */
2027 data = gst_adapter_map (demux->adapter, 6);
2029 /* skip start code */
2032 length = GST_READ_UINT16_BE (data);
2033 GST_DEBUG_OBJECT (demux, "length %d", length);
2037 gst_adapter_unmap (demux->adapter);
2038 if (gst_adapter_available (demux->adapter) < length)
2039 goto need_more_data;
2041 data = gst_adapter_map (demux->adapter, length);
2043 /* skip start code and length */
2046 /* marker:1==1 ! rate_bound:22 | marker:1==1 */
2047 if ((*data & 0x80) != 0x80)
2048 goto marker_expected;
2053 if ((data[2] & 0x01) != 0x01)
2054 goto marker_expected;
2056 rate_bound = ((guint32) data[0] & 0x7f) << 15;
2057 rate_bound |= ((guint32) data[1]) << 7;
2058 rate_bound |= ((guint32) data[2] & 0xfe) >> 1;
2059 rate_bound *= MPEG_MUX_RATE_MULT;
2061 GST_DEBUG_OBJECT (demux, "rate bound %u", rate_bound);
2066 /* audio_bound:6==1 ! fixed:1 | constrained:1 */
2068 #ifndef GST_DISABLE_GST_DEBUG
2072 /* max number of simultaneous audio streams active */
2073 audio_bound = (data[0] & 0xfc) >> 2;
2074 /* fixed or variable bitrate */
2075 fixed = (data[0] & 0x02) == 0x02;
2076 /* meeting constraints */
2077 csps = (data[0] & 0x01) == 0x01;
2079 GST_DEBUG_OBJECT (demux, "audio_bound %d, fixed %d, constrained %d",
2080 audio_bound, fixed, csps);
2085 /* audio_lock:1 | video_lock:1 | marker:1==1 | video_bound:5 */
2087 #ifndef GST_DISABLE_GST_DEBUG
2088 gboolean audio_lock;
2089 gboolean video_lock;
2092 audio_lock = (data[0] & 0x80) == 0x80;
2093 video_lock = (data[0] & 0x40) == 0x40;
2096 if ((data[0] & 0x20) != 0x20)
2097 goto marker_expected;
2099 #ifndef GST_DISABLE_GST_DEBUG
2100 /* max number of simultaneous video streams active */
2101 video_bound = (data[0] & 0x1f);
2103 GST_DEBUG_OBJECT (demux, "audio_lock %d, video_lock %d, video_bound %d",
2104 audio_lock, video_lock, video_bound);
2109 /* packet_rate_restriction:1 | reserved:7==0x7F */
2111 #ifndef GST_DISABLE_GST_DEBUG
2112 gboolean packet_rate_restriction;
2114 if ((data[0] & 0x7f) != 0x7f)
2115 goto marker_expected;
2116 #ifndef GST_DISABLE_GST_DEBUG
2117 /* only valid if csps is set */
2119 packet_rate_restriction = (data[0] & 0x80) == 0x80;
2121 GST_DEBUG_OBJECT (demux, "packet_rate_restriction %d",
2122 packet_rate_restriction);
2129 gint stream_count = (length - 12) / 3;
2132 GST_DEBUG_OBJECT (demux, "number of streams: %d ", stream_count);
2134 for (i = 0; i < stream_count; i++) {
2136 #ifndef GST_DISABLE_GST_DEBUG
2137 gboolean STD_buffer_bound_scale;
2138 guint16 STD_buffer_size_bound;
2139 guint32 buf_byte_size_bound;
2141 stream_id = *data++;
2142 if (!(stream_id & 0x80))
2145 /* check marker bits */
2146 if ((*data & 0xC0) != 0xC0)
2147 goto no_placeholder_bits;
2148 #ifndef GST_DISABLE_GST_DEBUG
2149 STD_buffer_bound_scale = *data & 0x20;
2150 STD_buffer_size_bound = ((guint16) (*data++ & 0x1F)) << 8;
2151 STD_buffer_size_bound |= *data++;
2153 if (STD_buffer_bound_scale == 0) {
2154 buf_byte_size_bound = STD_buffer_size_bound * 128;
2156 buf_byte_size_bound = STD_buffer_size_bound * 1024;
2159 GST_DEBUG_OBJECT (demux, "STD_buffer_bound_scale %d",
2160 STD_buffer_bound_scale);
2161 GST_DEBUG_OBJECT (demux, "STD_buffer_size_bound %d or %d bytes",
2162 STD_buffer_size_bound, buf_byte_size_bound);
2167 gst_adapter_unmap (demux->adapter);
2168 gst_adapter_flush (demux->adapter, length);
2169 ADAPTER_OFFSET_FLUSH (length);
2175 GST_DEBUG_OBJECT (demux, "expecting marker");
2176 gst_adapter_unmap (demux->adapter);
2177 return GST_FLOW_LOST_SYNC;
2179 no_placeholder_bits:
2181 GST_DEBUG_OBJECT (demux, "expecting placeholder bit values"
2182 " '11' after stream id");
2183 gst_adapter_unmap (demux->adapter);
2184 return GST_FLOW_LOST_SYNC;
2188 GST_DEBUG_OBJECT (demux, "error in system header length");
2189 gst_adapter_unmap (demux->adapter);
2190 return GST_FLOW_LOST_SYNC;
2194 GST_DEBUG_OBJECT (demux, "need more data");
2195 gst_adapter_unmap (demux->adapter);
2196 return GST_FLOW_NEED_MORE_DATA;
2200 static GstFlowReturn
2201 gst_ps_demux_parse_psm (GstPsDemux * demux)
2203 guint16 psm_length, info_length = 0, es_map_length = 0;
2204 guint8 psm_version = 0;
2206 #ifndef GST_DISABLE_GST_DEBUG
2207 gboolean applicable;
2210 /* Need at least 6 bytes for start code + length */
2211 if (gst_adapter_available (demux->adapter) < 6)
2212 goto need_more_data;
2217 /* start code + length */
2218 data = gst_adapter_map (demux->adapter, 6);
2219 /* skip start code */
2221 psm_length = GST_READ_UINT16_BE (data);
2222 GST_DEBUG_OBJECT (demux, "PSM length %u", psm_length);
2224 if (G_UNLIKELY (psm_length > 0x3FA))
2226 psm_length += 6; /* Add start code + size to length */
2228 gst_adapter_unmap (demux->adapter);
2230 if (gst_adapter_available (demux->adapter) < psm_length)
2231 goto need_more_data;
2233 data = gst_adapter_map (demux->adapter, psm_length);
2235 gst_byte_reader_init (&br, data, psm_length);
2238 /* skip start code and length */
2239 if (!gst_byte_reader_skip (&br, 6))
2242 /* Read PSM applicable bit together with version */
2243 if (!gst_byte_reader_get_uint8 (&br, &psm_version))
2245 #ifndef GST_DISABLE_GST_DEBUG
2246 applicable = (psm_version & 0x80) >> 7;
2248 psm_version &= 0x1F;
2249 GST_DEBUG_OBJECT (demux, "PSM version %u (applicable now %u)", psm_version,
2252 /* Jump over the next byte (marker bit) */
2253 if (!gst_byte_reader_skip (&br, 1))
2256 /* Read PS info length */
2257 if (!gst_byte_reader_get_uint16_be (&br, &info_length))
2259 GST_DEBUG_OBJECT (demux, "PS info length %u bytes", info_length);
2260 /* Skip the PS info, we don't use it */
2261 if (!gst_byte_reader_skip (&br, info_length))
2264 /* Read ES map length */
2265 if (!gst_byte_reader_get_uint16_be (&br, &es_map_length))
2267 GST_DEBUG_OBJECT (demux, "ES map length %u bytes", es_map_length);
2269 /* Now read the ES map */
2271 GstByteReader es_map_br;
2272 if (!gst_byte_reader_get_sub_reader (&br, &es_map_br, es_map_length))
2275 while (gst_byte_reader_get_remaining (&es_map_br) >= 4) {
2276 guint8 stream_type = 0, stream_id = 0;
2277 guint16 stream_info_length = 0;
2279 if (!gst_byte_reader_get_uint8 (&es_map_br, &stream_type) ||
2280 !gst_byte_reader_get_uint8 (&es_map_br, &stream_id) ||
2281 !gst_byte_reader_get_uint16_be (&es_map_br, &stream_info_length))
2284 GST_DEBUG_OBJECT (demux,
2285 "Stream type %02X with id %02X and %u bytes info", stream_type,
2286 stream_id, stream_info_length);
2288 if (G_LIKELY (stream_id != 0xbd))
2289 demux->psm[stream_id] = stream_type;
2291 /* Ignore stream type for private_stream_1 and discover it looking at
2293 * Fixes demuxing some clips with lpcm that was wrongly declared as
2295 GST_DEBUG_OBJECT (demux, "stream type for private_stream_1 ignored");
2298 /* FIXME: We could use the descriptors instead of skipping them */
2299 if (!gst_byte_reader_skip (&es_map_br, stream_info_length))
2303 /* We ignore the 4-byte CRC at the end */
2305 gst_adapter_unmap (demux->adapter);
2306 gst_adapter_flush (demux->adapter, psm_length);
2307 ADAPTER_OFFSET_FLUSH (psm_length);
2311 GST_DEBUG_OBJECT (demux, "Failed to parse PSM. Skipping");
2312 gst_adapter_unmap (demux->adapter);
2313 gst_adapter_flush (demux->adapter, psm_length);
2314 ADAPTER_OFFSET_FLUSH (psm_length);
2315 return GST_FLOW_LOST_SYNC;
2318 GST_DEBUG_OBJECT (demux, "error in PSM length");
2319 gst_adapter_unmap (demux->adapter);
2320 return GST_FLOW_LOST_SYNC;
2324 GST_DEBUG_OBJECT (demux, "need more data");
2325 return GST_FLOW_NEED_MORE_DATA;
2330 gst_ps_demux_resync_cb (GstPESFilter * filter, GstPsDemux * demux)
2334 static GstFlowReturn
2335 gst_ps_demux_data_cb (GstPESFilter * filter, gboolean first,
2336 GstBuffer * buffer, GstPsDemux * demux)
2339 GstFlowReturn ret = GST_FLOW_OK;
2346 gst_buffer_map (buffer, &map, GST_MAP_READ);
2348 start_code = filter->start_code;
2352 /* find the stream type */
2353 stream_type = demux->psm[id];
2354 if (stream_type == -1) {
2355 /* no stream type, if PS1, get the new id */
2356 if (start_code == ID_PRIVATE_STREAM_1 && datalen >= 2) {
2357 /* VDR writes A52 streams without any header bytes
2358 * (see ftp://ftp.mplayerhq.hu/MPlayer/samples/MPEG-VOB/vdr-AC3) */
2360 guint hdr = GST_READ_UINT32_BE (map.data);
2361 if (G_UNLIKELY ((hdr & 0xffff0000) == AC3_SYNC_WORD)) {
2363 stream_type = demux->psm[id] = ST_GST_AUDIO_RAWA52;
2364 GST_DEBUG_OBJECT (demux, "Found VDR raw A52 stream");
2368 if (G_LIKELY (stream_type == -1)) {
2369 /* new id is in the first byte */
2370 id = map.data[offset++];
2373 stream_type = demux->psm[id];
2374 /* Now, if it's a subpicture stream - no more, otherwise
2375 * take the first byte too, since it's the frame count in audio
2376 * streams and our backwards compat convention is to strip it off */
2377 if (stream_type != ST_PS_DVD_SUBPICTURE) {
2378 /* Number of audio frames in this packet */
2379 #ifndef GST_DISABLE_GST_DEBUG
2381 nframes = map.data[offset];
2382 GST_LOG_OBJECT (demux, "private type 0x%02x, %d frames", id,
2388 GST_LOG_OBJECT (demux, "private type 0x%02x, stream type %d",
2393 if (stream_type == -1)
2394 goto unknown_stream_type;
2395 } else if (stream_type == ST_AUDIO_MPEG1 || stream_type == ST_AUDIO_MPEG2) {
2397 guint hdr = GST_READ_UINT16_BE (map.data);
2398 if ((hdr & 0xfff0) == 0xfff0) {
2399 switch (hdr & 0x06) {
2410 GST_WARNING_OBJECT (demux, "unknown mpeg audio layer");
2416 if (filter->pts != -1) {
2417 demux->next_pts = filter->pts + demux->scr_adjust;
2418 GST_LOG_OBJECT (demux, "stream 0x%02x PTS = orig %" G_GUINT64_FORMAT
2419 " (%" G_GUINT64_FORMAT ")", id, filter->pts, demux->next_pts);
2421 demux->next_pts = G_MAXUINT64;
2422 if (filter->dts != -1) {
2423 demux->next_dts = filter->dts + demux->scr_adjust;
2424 GST_LOG_OBJECT (demux, "stream 0x%02x DTS = orig %" G_GUINT64_FORMAT
2425 " (%" G_GUINT64_FORMAT ")", id, filter->dts, demux->next_dts);
2427 demux->next_dts = demux->next_pts;
2430 demux->current_stream =
2431 gst_ps_demux_get_stream (demux, id, stream_type, layer);
2434 if (G_UNLIKELY (demux->current_stream == NULL)) {
2435 GST_DEBUG_OBJECT (demux, "Dropping buffer for unknown stream id 0x%02x",
2440 /* After 2 seconds of bitstream emit no more pads */
2441 if (demux->need_no_more_pads
2442 && (demux->current_scr - demux->first_scr) > 2 * CLOCK_FREQ) {
2443 GST_DEBUG_OBJECT (demux, "no more pads, notifying");
2444 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
2445 demux->need_no_more_pads = FALSE;
2448 /* If the stream is not-linked, don't bother creating a sub-buffer
2449 * to send to it, unless we're processing a discont (which resets
2450 * the not-linked status and tries again */
2451 if (demux->current_stream->discont) {
2452 GST_DEBUG_OBJECT (demux, "stream is discont");
2453 demux->current_stream->notlinked = FALSE;
2456 if (demux->current_stream->notlinked == FALSE) {
2458 gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, datalen);
2459 ret = gst_ps_demux_send_data (demux, demux->current_stream, out_buf);
2460 if (ret == GST_FLOW_NOT_LINKED) {
2461 demux->current_stream->notlinked = TRUE;
2466 gst_buffer_unmap (buffer, &map);
2467 gst_buffer_unref (buffer);
2470 unknown_stream_type:
2472 GST_DEBUG_OBJECT (demux, "unknown stream type %02x", id);
2479 gst_ps_demux_resync (GstPsDemux * demux, gboolean save)
2486 avail = gst_adapter_available (demux->adapter);
2487 if (G_UNLIKELY (avail < 4))
2489 /* Common case, read 4 bytes an check it */
2490 data = gst_adapter_map (demux->adapter, 4);
2491 /* read currect code */
2492 code = GST_READ_UINT32_BE (data);
2493 /* The common case is that the sync code is at 0 bytes offset */
2494 if (G_LIKELY ((code & 0xffffff00) == 0x100L)) {
2495 GST_LOG_OBJECT (demux, "Found resync code %08x after 0 bytes", code);
2496 demux->last_sync_code = code;
2497 gst_adapter_unmap (demux->adapter);
2501 /* Otherwise, we are starting at byte 4 and we need to search
2502 the sync code in all available data in the adapter */
2504 if (offset >= avail)
2505 goto need_data; /* Not enough data to find sync */
2506 data = gst_adapter_map (demux->adapter, avail);
2508 code = (code << 8) | data[offset++];
2509 found = (code & 0xffffff00) == 0x100L;
2510 } while (offset < avail && !found);
2511 gst_adapter_unmap (demux->adapter);
2512 if (!save || demux->sink_segment.rate >= 0.0) {
2513 GST_LOG_OBJECT (demux, "flushing %d bytes", offset - 4);
2514 /* forward playback, we can discard and flush the skipped bytes */
2515 gst_adapter_flush (demux->adapter, offset - 4);
2516 ADAPTER_OFFSET_FLUSH (offset - 4);
2519 GST_LOG_OBJECT (demux, "reverse saving %d bytes", offset - 4);
2520 /* reverse playback, we keep the flushed bytes and we will append them to
2521 * the next buffer in the chain function, which is the previous buffer in
2523 gst_adapter_push (demux->rev_adapter,
2524 gst_adapter_take_buffer (demux->adapter, offset - 4));
2526 GST_LOG_OBJECT (demux, "reverse saving %d bytes", avail);
2527 /* nothing found, keep all bytes */
2528 gst_adapter_push (demux->rev_adapter,
2529 gst_adapter_take_buffer (demux->adapter, avail));
2534 GST_LOG_OBJECT (demux, "Found resync code %08x after %d bytes",
2536 demux->last_sync_code = code;
2538 GST_LOG_OBJECT (demux, "No resync after skipping %d", offset);
2544 GST_LOG_OBJECT (demux, "we need more data for resync %d", avail);
2549 static inline gboolean
2550 gst_ps_demux_is_pes_sync (guint32 sync)
2552 return ((sync & 0xfc) == 0xbc) ||
2553 ((sync & 0xe0) == 0xc0) || ((sync & 0xf0) == 0xe0);
2556 static inline gboolean
2557 gst_ps_demux_scan_ts (GstPsDemux * demux, const guint8 * data,
2558 SCAN_MODE mode, guint64 * rts, const guint8 * end)
2560 gboolean ret = FALSE;
2566 /* read the 4 bytes for the sync code */
2567 code = GST_READ_UINT32_BE (data);
2568 if (G_LIKELY (code != ID_PS_PACK_START_CODE))
2570 if (data + 12 > end)
2572 /* skip start code */
2574 scr1 = GST_READ_UINT32_BE (data);
2575 scr2 = GST_READ_UINT32_BE (data + 4);
2576 /* start parsing the stream */
2577 if ((*data & 0xc0) == 0x40) {
2578 /* MPEG-2 PACK header */
2581 guint8 stuffing_bytes;
2582 /* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
2584 if ((scr1 & 0xc4000400) != 0x44000400)
2586 scr = ((guint64) scr1 & 0x38000000) << 3;
2587 scr |= ((guint64) scr1 & 0x03fff800) << 4;
2588 scr |= ((guint64) scr1 & 0x000003ff) << 5;
2589 scr |= ((guint64) scr2 & 0xf8000000) >> 27;
2590 /* marker:1==1 ! scr_ext:9 ! marker:1==1 */
2591 if ((scr2 & 0x04010000) != 0x04010000)
2593 scr_ext = (scr2 & 0x03fe0000) >> 17;
2595 scr = (scr * 300 + scr_ext % 300) / 300;
2597 /* SCR has been converted into units of 90Khz ticks to make it comparable
2598 to DTS/PTS, that also implies 1 tick rounding error */
2603 /* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
2604 next32 = GST_READ_UINT32_BE (data);
2605 if ((next32 & 0x00000300) != 0x00000300)
2607 stuffing_bytes = (next32 & 0x07);
2609 if (data + stuffing_bytes > end)
2611 while (stuffing_bytes--) {
2612 if (*data++ != 0xff)
2616 /* MPEG-1 pack header */
2618 if ((scr1 & 0xf1000100) != 0x21000100)
2620 if ((scr2 & 0x01800001) != 0x01800001)
2622 /* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
2623 scr = ((guint64) scr1 & 0x0e000000) << 5;
2624 scr |= ((guint64) scr1 & 0x00fffe00) << 6;
2625 scr |= ((guint64) scr1 & 0x000000ff) << 7;
2626 scr |= ((guint64) scr2 & 0xfe000000) >> 25;
2630 if (mode == SCAN_SCR) {
2636 /* Possible optional System header here */
2640 code = GST_READ_UINT32_BE (data);
2641 len = GST_READ_UINT16_BE (data + 4);
2642 if (code == ID_PS_SYSTEM_HEADER_START_CODE) {
2643 /* Found a system header, skip it */
2644 /* Check for sufficient data - system header, plus enough
2645 * left over for the PES packet header */
2646 if (data + 6 + len + 6 > end)
2649 /* read the 4 bytes for the PES sync code */
2650 code = GST_READ_UINT32_BE (data);
2651 len = GST_READ_UINT16_BE (data + 4);
2654 /* Check we have enough data left for reading the PES packet */
2655 if (data + 6 + len > end)
2657 if (!gst_ps_demux_is_pes_sync (code))
2660 case ID_PS_PROGRAM_STREAM_MAP:
2661 case ID_PRIVATE_STREAM_2:
2664 case ID_PROGRAM_STREAM_DIRECTORY:
2665 case ID_DSMCC_STREAM:
2666 case ID_ITU_TREC_H222_TYPE_E_STREAM:
2667 case ID_PADDING_STREAM:
2673 /* skip sync code and size */
2676 /* stuffing bits, first two bits are '10' for mpeg2 pes so this code is
2684 /* STD buffer size, never for mpeg2 */
2685 if ((*data & 0xc0) == 0x40)
2687 /* PTS but no DTS, never for mpeg2 */
2688 if ((*data & 0xf0) == 0x20) {
2689 READ_TS (data, pts, beach);
2691 /* PTS and DTS, never for mpeg2 */
2692 else if ((*data & 0xf0) == 0x30) {
2693 READ_TS (data, pts, beach);
2694 READ_TS (data, dts, beach);
2695 } else if ((*data & 0xc0) == 0x80) {
2699 * 2: PES_scrambling_control
2701 * 1: data_alignment_indicator
2703 * 1: original_or_copy
2706 if ((flags & 0xc0) != 0x80)
2711 * 1: DSM_trick_mode_flag
2712 * 1: additional_copy_info_flag
2714 * 1: PES_extension_flag
2717 /* 8: PES_header_data_length */
2719 /* only DTS: this is invalid */
2720 if ((flags & 0xc0) == 0x40)
2723 if ((flags & 0x80)) {
2724 READ_TS (data, pts, beach);
2727 if ((flags & 0x40)) {
2728 READ_TS (data, dts, beach);
2732 if (mode == SCAN_DTS && dts != (guint64) - 1) {
2737 if (mode == SCAN_PTS && pts != (guint64) - 1) {
2745 static inline gboolean
2746 gst_ps_demux_scan_forward_ts (GstPsDemux * demux, guint64 * pos,
2747 SCAN_MODE mode, guint64 * rts, gint limit)
2749 GstFlowReturn ret = GST_FLOW_OK;
2751 guint64 offset = *pos;
2752 gboolean found = FALSE;
2754 guint scan_sz = (mode == SCAN_SCR ? SCAN_SCR_SZ : SCAN_PTS_SZ);
2755 guint cursor, to_read = BLOCK_SZ;
2759 /* Check we can get at least scan_sz bytes */
2760 if (offset + scan_sz > demux->sink_segment.stop)
2762 /* Don't go further than 'limit' bytes */
2763 if (limit && offset > *pos + limit)
2765 if (offset + to_read > demux->sink_segment.stop)
2766 to_read = demux->sink_segment.stop - offset;
2767 /* read some data */
2769 ret = gst_pad_pull_range (demux->sinkpad, offset, to_read, &buffer);
2770 if (G_UNLIKELY (ret != GST_FLOW_OK))
2772 gst_buffer_map (buffer, &map, GST_MAP_READ);
2773 /* may get a short buffer at the end of the file */
2774 if (G_UNLIKELY (map.size <= scan_sz)) {
2775 gst_buffer_unmap (buffer, &map);
2776 gst_buffer_unref (buffer);
2780 end_scan = map.size - scan_sz;
2781 /* scan the block */
2782 for (cursor = 0; !found && cursor <= end_scan; cursor++) {
2783 found = gst_ps_demux_scan_ts (demux, map.data + cursor, mode, &ts,
2784 map.data + map.size);
2787 /* done with the buffer, unref it */
2788 gst_buffer_unmap (buffer, &map);
2789 gst_buffer_unref (buffer);
2792 *pos = offset + cursor - 1;
2796 } while (!found && offset < demux->sink_segment.stop);
2800 static inline gboolean
2801 gst_ps_demux_scan_backward_ts (GstPsDemux * demux, guint64 * pos,
2802 SCAN_MODE mode, guint64 * rts, gint limit)
2804 GstFlowReturn ret = GST_FLOW_OK;
2806 guint64 offset = *pos;
2807 gboolean found = FALSE;
2809 guint scan_sz = (mode == SCAN_SCR ? SCAN_SCR_SZ : SCAN_PTS_SZ);
2810 guint cursor, to_read = BLOCK_SZ;
2815 /* Check we have at least scan_sz bytes available */
2816 if (offset < scan_sz - 1)
2818 /* Don't go backward past the start or 'limit' bytes */
2819 if (limit && offset + limit < *pos)
2821 if (offset > BLOCK_SZ)
2824 to_read = offset + 1;
2827 /* read some data */
2829 ret = gst_pad_pull_range (demux->sinkpad, offset, to_read, &buffer);
2830 if (G_UNLIKELY (ret != GST_FLOW_OK))
2832 gst_buffer_map (buffer, &map, GST_MAP_READ);
2833 /* may get a short buffer at the end of the file */
2834 if (G_UNLIKELY (map.size <= scan_sz)) {
2835 gst_buffer_unmap (buffer, &map);
2836 gst_buffer_unref (buffer);
2840 start_scan = map.size - scan_sz;
2841 data = map.data + start_scan;
2842 /* scan the block */
2843 for (cursor = (start_scan + 1); !found && cursor > 0; cursor--) {
2844 found = gst_ps_demux_scan_ts (demux, data--, mode, &ts,
2845 map.data + map.size);
2848 /* done with the buffer, unref it */
2849 gst_buffer_unmap (buffer, &map);
2850 gst_buffer_unref (buffer);
2853 *pos = offset + cursor;
2856 } while (!found && offset > 0);
2860 static inline gboolean
2861 gst_ps_sink_get_duration (GstPsDemux * demux)
2863 gboolean res = FALSE;
2865 GstFormat format = GST_FORMAT_BYTES;
2870 /* init the sink segment */
2871 gst_segment_init (&demux->sink_segment, format);
2872 /* get peer to figure out length */
2873 if ((peer = gst_pad_get_peer (demux->sinkpad)) == NULL)
2875 res = gst_pad_query_duration (peer, format, &length);
2876 gst_object_unref (peer);
2877 if (!res || length <= 0)
2879 GST_DEBUG_OBJECT (demux, "file length %" G_GINT64_FORMAT, length);
2880 /* update the sink segment */
2881 demux->sink_segment.stop = length;
2882 gst_segment_set_duration (&demux->sink_segment, format, length);
2883 gst_segment_set_position (&demux->sink_segment, format, 0);
2884 /* Scan for notorious SCR and PTS to calculate the duration */
2885 /* scan for first SCR in the stream */
2886 offset = demux->sink_segment.start;
2887 gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR,
2888 &demux->first_scr, DURATION_SCAN_LIMIT);
2889 GST_DEBUG_OBJECT (demux,
2890 "First SCR: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2891 " in packet starting at %" G_GUINT64_FORMAT, demux->first_scr,
2892 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_scr)), offset);
2893 demux->first_scr_offset = offset;
2894 /* scan for last SCR in the stream */
2895 offset = demux->sink_segment.stop;
2896 gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR,
2897 &demux->last_scr, DURATION_SCAN_LIMIT);
2898 GST_DEBUG_OBJECT (demux,
2899 "Last SCR: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2900 " in packet starting at %" G_GUINT64_FORMAT, demux->last_scr,
2901 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->last_scr)), offset);
2902 demux->last_scr_offset = offset;
2903 /* scan for first PTS in the stream */
2904 offset = demux->sink_segment.start;
2905 gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_PTS,
2906 &demux->first_pts, DURATION_SCAN_LIMIT);
2907 GST_DEBUG_OBJECT (demux,
2908 "First PTS: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2909 " in packet starting at %" G_GUINT64_FORMAT, demux->first_pts,
2910 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_pts)), offset);
2911 if (demux->first_pts != G_MAXUINT64) {
2912 /* scan for last PTS in the stream */
2913 offset = demux->sink_segment.stop;
2914 gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_PTS,
2915 &demux->last_pts, DURATION_SCAN_LIMIT);
2916 GST_DEBUG_OBJECT (demux,
2917 "Last PTS: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2918 " in packet starting at %" G_GUINT64_FORMAT, demux->last_pts,
2919 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->last_pts)), offset);
2921 /* Detect wrong SCR values */
2922 if (demux->first_scr > demux->last_scr) {
2923 GST_DEBUG_OBJECT (demux, "Wrong SCR values detected, searching for "
2924 "a better first SCR value");
2925 offset = demux->first_scr_offset;
2926 for (i = 0; i < 10; i++) {
2928 gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &scr, 0);
2929 if (scr < demux->last_scr) {
2930 demux->first_scr = scr;
2931 demux->first_scr_offset = offset;
2932 /* Start demuxing from the right place */
2933 demux->sink_segment.position = offset;
2934 GST_DEBUG_OBJECT (demux, "Replaced First SCR: %" G_GINT64_FORMAT
2935 " %" GST_TIME_FORMAT " in packet starting at %"
2936 G_GUINT64_FORMAT, demux->first_scr,
2937 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_scr)), offset);
2942 /* Set the base_time and avg rate */
2943 demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
2944 demux->scr_rate_n = demux->last_scr_offset - demux->first_scr_offset;
2945 demux->scr_rate_d = demux->last_scr - demux->first_scr;
2946 if (G_LIKELY (demux->first_pts != G_MAXUINT64 &&
2947 demux->last_pts != G_MAXUINT64)) {
2948 /* update the src segment */
2949 demux->src_segment.format = GST_FORMAT_TIME;
2950 demux->src_segment.start =
2951 MPEGTIME_TO_GSTTIME (demux->first_pts) - demux->base_time;
2952 demux->src_segment.stop = -1;
2953 gst_segment_set_duration (&demux->src_segment, GST_FORMAT_TIME,
2954 MPEGTIME_TO_GSTTIME (demux->last_pts - demux->first_pts));
2955 gst_segment_set_position (&demux->src_segment, GST_FORMAT_TIME,
2956 demux->src_segment.start);
2958 GST_INFO_OBJECT (demux, "sink segment configured %" GST_SEGMENT_FORMAT,
2959 &demux->sink_segment);
2960 GST_INFO_OBJECT (demux, "src segment configured %" GST_SEGMENT_FORMAT,
2961 &demux->src_segment);
2967 static inline GstFlowReturn
2968 gst_ps_demux_pull_block (GstPad * pad, GstPsDemux * demux,
2969 guint64 offset, guint size)
2972 GstBuffer *buffer = NULL;
2973 ret = gst_pad_pull_range (pad, offset, size, &buffer);
2974 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2975 GST_DEBUG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
2976 " size %u failed", offset, size);
2979 GST_LOG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
2980 " size %u done", offset, size);
2981 if (demux->sink_segment.rate < 0) {
2982 GST_LOG_OBJECT (demux, "setting discont flag on backward rate");
2983 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2985 ret = gst_ps_demux_chain (pad, GST_OBJECT (demux), buffer);
2991 gst_ps_demux_loop (GstPad * pad)
2994 GstFlowReturn ret = GST_FLOW_OK;
2996 demux = GST_PS_DEMUX (gst_pad_get_parent (pad));
2997 if (G_UNLIKELY (demux->flushing)) {
2998 ret = GST_FLOW_FLUSHING;
3002 if (G_UNLIKELY (demux->sink_segment.format == GST_FORMAT_UNDEFINED))
3003 gst_ps_sink_get_duration (demux);
3004 offset = demux->sink_segment.position;
3005 if (demux->sink_segment.rate >= 0) {
3006 guint size = BLOCK_SZ;
3007 if (G_LIKELY (demux->sink_segment.stop != (guint64) - 1)) {
3008 size = MIN (size, demux->sink_segment.stop - offset);
3011 ret = gst_ps_demux_pull_block (pad, demux, offset, size);
3012 /* pause if something went wrong */
3013 if (G_UNLIKELY (ret != GST_FLOW_OK))
3015 /* update our position */
3017 gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
3018 /* check EOS condition */
3019 /* FIXME: The src_segment.stop is not including the SCR after seek(set) */
3020 if ((demux->sink_segment.position >= demux->sink_segment.stop) ||
3021 (demux->src_segment.stop != (guint64) - 1 &&
3022 demux->src_segment.position >=
3023 demux->src_segment.stop + demux->base_time)) {
3024 GST_DEBUG_OBJECT (demux,
3025 "forward mode using segment reached end of " "segment pos %"
3026 GST_TIME_FORMAT " stop %" GST_TIME_FORMAT " pos in bytes %"
3027 G_GUINT64_FORMAT " stop in bytes %" G_GUINT64_FORMAT,
3028 GST_TIME_ARGS (demux->src_segment.position),
3029 GST_TIME_ARGS (demux->src_segment.stop),
3030 demux->sink_segment.position, demux->sink_segment.stop);
3034 } else { /* Reverse playback */
3035 guint64 size = MIN (offset, BLOCK_SZ);
3037 ret = gst_ps_demux_pull_block (pad, demux, offset - size, size);
3038 /* pause if something went wrong */
3039 if (G_UNLIKELY (ret != GST_FLOW_OK))
3041 /* update our position */
3043 gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
3044 /* check EOS condition */
3045 if (demux->sink_segment.position <= demux->sink_segment.start ||
3046 demux->src_segment.position <= demux->src_segment.start) {
3047 GST_DEBUG_OBJECT (demux,
3048 "reverse mode using segment reached end of " "segment pos %"
3049 GST_TIME_FORMAT " stop %" GST_TIME_FORMAT " pos in bytes %"
3050 G_GUINT64_FORMAT " stop in bytes %" G_GUINT64_FORMAT,
3051 GST_TIME_ARGS (demux->src_segment.position),
3052 GST_TIME_ARGS (demux->src_segment.start),
3053 demux->sink_segment.position, demux->sink_segment.start);
3059 gst_object_unref (demux);
3063 const gchar *reason = gst_flow_get_name (ret);
3064 GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
3065 gst_pad_pause_task (pad);
3066 if (ret == GST_FLOW_EOS) {
3067 /* perform EOS logic */
3068 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
3069 if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
3071 /* for segment playback we need to post when (in stream time)
3072 * we stopped, this is either stop (when set) or the duration. */
3073 if ((stop = demux->src_segment.stop) == -1)
3074 stop = demux->src_segment.duration;
3075 if (demux->sink_segment.rate >= 0) {
3076 GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
3077 gst_element_post_message (GST_ELEMENT_CAST (demux),
3078 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
3079 GST_FORMAT_TIME, stop));
3080 gst_ps_demux_send_event (demux,
3081 gst_event_new_segment_done (GST_FORMAT_TIME, stop));
3082 } else { /* Reverse playback */
3083 GST_LOG_OBJECT (demux,
3084 "Sending segment done, at beginning of " "segment");
3085 gst_element_post_message (GST_ELEMENT_CAST (demux),
3086 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
3087 GST_FORMAT_TIME, demux->src_segment.start));
3088 gst_ps_demux_send_event (demux,
3089 gst_event_new_segment_done (GST_FORMAT_TIME,
3090 demux->src_segment.start));
3094 /* normal playback, send EOS to all linked pads */
3095 gst_element_no_more_pads (GST_ELEMENT (demux));
3096 GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
3097 event = gst_event_new_eos ();
3098 if (demux->segment_seqnum)
3099 gst_event_set_seqnum (event, demux->segment_seqnum);
3100 if (!gst_ps_demux_send_event (demux, event)
3101 && !have_open_streams (demux)) {
3102 GST_WARNING_OBJECT (demux, "EOS and no streams open");
3103 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
3104 ("Internal data stream error."), ("No valid streams detected"));
3107 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
3109 GST_ELEMENT_FLOW_ERROR (demux, ret);
3110 event = gst_event_new_eos ();
3111 if (demux->segment_seqnum)
3112 gst_event_set_seqnum (event, demux->segment_seqnum);
3113 gst_ps_demux_send_event (demux, event);
3116 gst_object_unref (demux);
3121 /* If we can pull that's preferred */
3123 gst_ps_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
3125 gboolean res = FALSE;
3126 GstQuery *query = gst_query_new_scheduling ();
3127 if (gst_pad_peer_query (sinkpad, query)) {
3128 if (gst_query_has_scheduling_mode_with_flags (query,
3129 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE)) {
3130 res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
3132 res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
3135 res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
3138 gst_query_unref (query);
3142 /* This function gets called when we activate ourselves in push mode. */
3144 gst_ps_demux_sink_activate_push (GstPad * sinkpad, GstObject * parent,
3147 GstPsDemux *demux = GST_PS_DEMUX (parent);
3148 demux->random_access = FALSE;
3152 /* this function gets called when we activate ourselves in pull mode.
3153 * We can perform random access to the resource and we start a task
3154 * to start reading */
3156 gst_ps_demux_sink_activate_pull (GstPad * sinkpad, GstObject * parent,
3159 GstPsDemux *demux = GST_PS_DEMUX (parent);
3161 GST_DEBUG ("pull mode activated");
3162 demux->random_access = TRUE;
3163 return gst_pad_start_task (sinkpad,
3164 (GstTaskFunction) gst_ps_demux_loop, sinkpad, NULL);
3166 demux->random_access = FALSE;
3167 return gst_pad_stop_task (sinkpad);
3172 gst_ps_demux_sink_activate_mode (GstPad * pad, GstObject * parent,
3173 GstPadMode mode, gboolean active)
3175 if (mode == GST_PAD_MODE_PUSH) {
3176 return gst_ps_demux_sink_activate_push (pad, parent, active);
3177 } else if (mode == GST_PAD_MODE_PULL) {
3178 return gst_ps_demux_sink_activate_pull (pad, parent, active);
3183 /* EOS and NOT_LINKED need to be combined. This means that we return:
3185 * GST_FLOW_NOT_LINKED: when all pads NOT_LINKED.
3186 * GST_FLOW_EOS: when all pads EOS or NOT_LINKED.
3188 static GstFlowReturn
3189 gst_ps_demux_combine_flows (GstPsDemux * demux, GstFlowReturn ret)
3191 GST_LOG_OBJECT (demux, "flow return: %s", gst_flow_get_name (ret));
3192 ret = gst_flow_combiner_update_flow (demux->flowcombiner, ret);
3193 if (G_UNLIKELY (demux->need_no_more_pads && ret == GST_FLOW_NOT_LINKED))
3195 GST_LOG_OBJECT (demux, "combined flow return: %s", gst_flow_get_name (ret));
3199 static GstFlowReturn
3200 gst_ps_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3202 GstPsDemux *demux = GST_PS_DEMUX (parent);
3203 GstFlowReturn ret = GST_FLOW_OK;
3205 gboolean save, discont;
3206 discont = GST_BUFFER_IS_DISCONT (buffer);
3208 GST_LOG_OBJECT (demux,
3209 "Received buffer with discont flag and" " offset %"
3210 G_GUINT64_FORMAT, GST_BUFFER_OFFSET (buffer));
3211 gst_pes_filter_drain (&demux->filter);
3212 gst_ps_demux_mark_discont (demux, TRUE, FALSE);
3213 /* mark discont on all streams */
3214 if (demux->sink_segment.rate >= 0.0) {
3215 demux->current_scr = G_MAXUINT64;
3216 demux->bytes_since_scr = 0;
3219 GST_LOG_OBJECT (demux,
3220 "Received buffer with offset %" G_GUINT64_FORMAT,
3221 GST_BUFFER_OFFSET (buffer));
3224 /* We keep the offset to interpolate SCR */
3225 demux->adapter_offset = GST_BUFFER_OFFSET (buffer);
3226 gst_adapter_push (demux->adapter, buffer);
3227 demux->bytes_since_scr += gst_buffer_get_size (buffer);
3228 avail = gst_adapter_available (demux->rev_adapter);
3230 GST_LOG_OBJECT (demux, "appending %u saved bytes", avail);
3231 /* if we have a previous reverse chunk, append this now */
3232 /* FIXME this code assumes we receive discont buffers all thei
3234 gst_adapter_push (demux->adapter,
3235 gst_adapter_take_buffer (demux->rev_adapter, avail));
3238 avail = gst_adapter_available (demux->adapter);
3239 GST_LOG_OBJECT (demux, "avail now: %d, state %d", avail, demux->filter.state);
3240 switch (demux->filter.state) {
3241 case STATE_DATA_SKIP:
3242 case STATE_DATA_PUSH:
3243 ret = gst_pes_filter_process (&demux->filter);
3245 case STATE_HEADER_PARSE:
3252 case GST_FLOW_NEED_MORE_DATA:
3253 /* Go and get more data */
3256 case GST_FLOW_LOST_SYNC:
3257 /* for FLOW_OK or lost-sync, carry onto resync */
3263 /* Any other return value should be sent upstream immediately */
3267 /* align adapter data to sync boundary, we keep the data up to the next sync
3270 while (gst_ps_demux_resync (demux, save)) {
3271 gboolean ps_sync = TRUE;
3272 if (G_UNLIKELY (demux->flushing)) {
3273 ret = GST_FLOW_FLUSHING;
3277 /* now switch on last synced byte */
3278 switch (demux->last_sync_code) {
3279 case ID_PS_PACK_START_CODE:
3280 ret = gst_ps_demux_parse_pack_start (demux);
3282 case ID_PS_SYSTEM_HEADER_START_CODE:
3283 ret = gst_ps_demux_parse_sys_head (demux);
3285 case ID_PS_END_CODE:
3286 /* Skip final 4 bytes */
3287 gst_adapter_flush (demux->adapter, 4);
3288 ADAPTER_OFFSET_FLUSH (4);
3291 case ID_PS_PROGRAM_STREAM_MAP:
3292 ret = gst_ps_demux_parse_psm (demux);
3295 if (gst_ps_demux_is_pes_sync (demux->last_sync_code)) {
3296 ret = gst_pes_filter_process (&demux->filter);
3298 GST_DEBUG_OBJECT (demux, "sync_code=%08x, non PES sync found"
3299 ", continuing", demux->last_sync_code);
3301 ret = GST_FLOW_LOST_SYNC;
3305 /* if we found a ps sync, we stop saving the data, any non-ps sync gets
3306 * saved up to the next ps sync. */
3310 case GST_FLOW_NEED_MORE_DATA:
3311 GST_DEBUG_OBJECT (demux, "need more data");
3314 case GST_FLOW_LOST_SYNC:
3315 if (!save || demux->sink_segment.rate >= 0.0) {
3316 GST_DEBUG_OBJECT (demux, "flushing 3 bytes");
3317 gst_adapter_flush (demux->adapter, 3);
3318 ADAPTER_OFFSET_FLUSH (3);
3320 GST_DEBUG_OBJECT (demux, "saving 3 bytes");
3321 gst_adapter_push (demux->rev_adapter,
3322 gst_adapter_take_buffer (demux->adapter, 3));
3327 ret = gst_ps_demux_combine_flows (demux, ret);
3328 if (ret != GST_FLOW_OK)
3337 static GstStateChangeReturn
3338 gst_ps_demux_change_state (GstElement * element, GstStateChange transition)
3340 GstPsDemux *demux = GST_PS_DEMUX (element);
3341 GstStateChangeReturn result;
3342 switch (transition) {
3343 case GST_STATE_CHANGE_NULL_TO_READY:
3344 gst_pes_filter_init (&demux->filter, demux->adapter,
3345 &demux->adapter_offset);
3346 gst_pes_filter_set_callbacks (&demux->filter,
3347 (GstPESFilterData) gst_ps_demux_data_cb,
3348 (GstPESFilterResync) gst_ps_demux_resync_cb, demux);
3349 demux->filter.gather_pes = TRUE;
3351 case GST_STATE_CHANGE_READY_TO_PAUSED:
3357 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3358 switch (transition) {
3359 case GST_STATE_CHANGE_PAUSED_TO_READY:
3360 gst_ps_demux_reset (demux);
3362 case GST_STATE_CHANGE_READY_TO_NULL:
3363 gst_pes_filter_uninit (&demux->filter);
3373 gst_segment_set_position (GstSegment * segment, GstFormat format,
3376 if (segment->format == GST_FORMAT_UNDEFINED) {
3377 segment->format = format;
3379 segment->position = position;
3383 gst_segment_set_duration (GstSegment * segment, GstFormat format,
3386 if (segment->format == GST_FORMAT_UNDEFINED) {
3387 segment->format = format;
3389 segment->duration = duration;