2 * This library is licensed under 2 different licenses and you
3 * can choose to use it under the terms of either one of them. The
4 * two licenses are the MPL 1.1 and the LGPL.
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.1 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/.
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15 * License for the specific language governing rights and limitations
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Library General Public
22 * License as published by the Free Software Foundation; either
23 * version 2 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Library General Public License for more details.
30 * You should have received a copy of the GNU Library General Public
31 * License along with this library; if not, write to the
32 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
33 * Boston, MA 02110-1301, USA.
35 * The Original Code is Fluendo MPEG Demuxer plugin.
37 * The Initial Developer of the Original Code is Fluendo, S.L.
38 * Portions created by Fluendo, S.L. are Copyright (C) 2005
39 * Fluendo, S.L. All Rights Reserved.
41 * Contributor(s): Wim Taymans <wim@fluendo.com>
42 * Jan Schmidt <thaytan@noraisin.net>
51 #include <gst/tag/tag.h>
52 #include <gst/pbutils/pbutils.h>
53 #include <gst/base/gstbytereader.h>
55 #include "gstmpegdefs.h"
56 #include "gstmpegdemux.h"
58 #define BLOCK_SZ 32768
59 #define SCAN_SCR_SZ 12
60 #define SCAN_PTS_SZ 80
62 #define SEGMENT_THRESHOLD (300*GST_MSECOND)
63 #define VIDEO_SEGMENT_THRESHOLD (500*GST_MSECOND)
65 #define DURATION_SCAN_LIMIT 4 * 1024 * 1024
74 /* We clamp scr delta with 0 so negative bytes won't be possible */
75 #define GSTTIME_TO_BYTES(time) \
76 ((time != -1) ? gst_util_uint64_scale (MAX(0,(gint64) (GSTTIME_TO_MPEGTIME(time))), demux->scr_rate_n, demux->scr_rate_d) : -1)
77 #define BYTES_TO_GSTTIME(bytes) ((bytes != -1) ? MPEGTIME_TO_GSTTIME(gst_util_uint64_scale (bytes, demux->scr_rate_d, demux->scr_rate_n)) : -1)
79 #define ADAPTER_OFFSET_FLUSH(_bytes_) demux->adapter_offset += (_bytes_)
81 GST_DEBUG_CATEGORY_STATIC (gstflupsdemux_debug);
82 #define GST_CAT_DEFAULT (gstflupsdemux_debug)
84 /* MPEG2Demux signals and args */
97 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
100 GST_STATIC_CAPS ("video/mpeg, "
101 "mpegversion = (int) { 1, 2 }, "
102 "systemstream = (boolean) TRUE;" "video/x-cdxa")
105 static GstStaticPadTemplate video_template =
106 GST_STATIC_PAD_TEMPLATE ("video_%02x",
109 GST_STATIC_CAPS ("video/mpeg, "
110 "mpegversion = (int) { 1, 2, 4 }, " "systemstream = (boolean) FALSE, "
111 "parsed = (boolean) FALSE; " "video/x-h264, "
112 "stream-format=(string)byte-stream; " "video/x-h265, "
113 "stream-format=(string)byte-stream;")
116 static GstStaticPadTemplate audio_template =
117 GST_STATIC_PAD_TEMPLATE ("audio_%02x",
120 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1;"
121 "audio/mpeg, mpegversion = (int) 4, stream-format = (string) { adts, loas };"
122 "audio/x-private1-lpcm; "
123 "audio/x-private1-ac3;" "audio/x-private1-dts;" "audio/ac3")
126 static GstStaticPadTemplate subpicture_template =
127 GST_STATIC_PAD_TEMPLATE ("subpicture_%02x",
130 GST_STATIC_CAPS ("subpicture/x-dvd")
133 static GstStaticPadTemplate private_template =
134 GST_STATIC_PAD_TEMPLATE ("private_%d",
137 GST_STATIC_CAPS_ANY);
139 static void gst_ps_demux_base_init (GstPsDemuxClass * klass);
140 static void gst_ps_demux_class_init (GstPsDemuxClass * klass);
141 static void gst_ps_demux_init (GstPsDemux * demux);
142 static void gst_ps_demux_finalize (GstPsDemux * demux);
143 static void gst_ps_demux_reset (GstPsDemux * demux);
145 static gboolean gst_ps_demux_sink_event (GstPad * pad, GstObject * parent,
147 static GstFlowReturn gst_ps_demux_chain (GstPad * pad, GstObject * parent,
149 static gboolean gst_ps_demux_sink_activate (GstPad * sinkpad,
151 static gboolean gst_ps_demux_sink_activate_mode (GstPad * pad,
152 GstObject * parent, GstPadMode mode, gboolean active);
153 static void gst_ps_demux_loop (GstPad * pad);
155 static gboolean gst_ps_demux_src_event (GstPad * pad, GstObject * parent,
157 static gboolean gst_ps_demux_src_query (GstPad * pad, GstObject * parent,
160 static GstStateChangeReturn gst_ps_demux_change_state (GstElement * element,
161 GstStateChange transition);
163 static inline gboolean gst_ps_demux_scan_forward_ts (GstPsDemux * demux,
164 guint64 * pos, SCAN_MODE mode, guint64 * rts, gint limit);
165 static inline gboolean gst_ps_demux_scan_backward_ts (GstPsDemux * demux,
166 guint64 * pos, SCAN_MODE mode, guint64 * rts, gint limit);
168 static inline void gst_ps_demux_send_gap_updates (GstPsDemux * demux,
169 GstClockTime new_time);
170 static inline void gst_ps_demux_clear_times (GstPsDemux * demux);
172 static void gst_ps_demux_reset_psm (GstPsDemux * demux);
173 static void gst_ps_demux_flush (GstPsDemux * demux);
175 static GstElementClass *parent_class = NULL;
177 static void gst_segment_set_position (GstSegment * segment, GstFormat format,
179 static void gst_segment_set_duration (GstSegment * segment, GstFormat format,
182 /*static guint gst_ps_demux_signals[LAST_SIGNAL] = { 0 };*/
185 gst_ps_demux_get_type (void)
187 static GType ps_demux_type = 0;
189 if (!ps_demux_type) {
190 static const GTypeInfo ps_demux_info = {
191 sizeof (GstPsDemuxClass),
192 (GBaseInitFunc) gst_ps_demux_base_init,
194 (GClassInitFunc) gst_ps_demux_class_init,
199 (GInstanceInitFunc) gst_ps_demux_init,
204 g_type_register_static (GST_TYPE_ELEMENT, "GstMpegPSDemux",
207 GST_DEBUG_CATEGORY_INIT (gstflupsdemux_debug, "mpegpsdemux", 0,
208 "MPEG program stream demultiplexer element");
211 return ps_demux_type;
215 gst_ps_demux_base_init (GstPsDemuxClass * klass)
217 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
219 klass->sink_template = gst_static_pad_template_get (&sink_template);
220 klass->video_template = gst_static_pad_template_get (&video_template);
221 klass->audio_template = gst_static_pad_template_get (&audio_template);
222 klass->subpicture_template =
223 gst_static_pad_template_get (&subpicture_template);
224 klass->private_template = gst_static_pad_template_get (&private_template);
226 gst_element_class_add_pad_template (element_class, klass->video_template);
227 gst_element_class_add_pad_template (element_class, klass->audio_template);
228 gst_element_class_add_pad_template (element_class,
229 klass->subpicture_template);
230 gst_element_class_add_pad_template (element_class, klass->private_template);
231 gst_element_class_add_pad_template (element_class, klass->sink_template);
233 gst_element_class_set_static_metadata (element_class,
234 "MPEG Program Stream Demuxer", "Codec/Demuxer",
235 "Demultiplexes MPEG Program Streams", "Wim Taymans <wim@fluendo.com>");
239 gst_ps_demux_class_init (GstPsDemuxClass * klass)
241 GObjectClass *gobject_class;
242 GstElementClass *gstelement_class;
244 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
246 gobject_class = (GObjectClass *) klass;
247 gstelement_class = (GstElementClass *) klass;
249 gobject_class->finalize = (GObjectFinalizeFunc) gst_ps_demux_finalize;
251 gstelement_class->change_state = gst_ps_demux_change_state;
255 gst_ps_demux_init (GstPsDemux * demux)
257 GstPsDemuxClass *klass = GST_PS_DEMUX_GET_CLASS (demux);
259 demux->sinkpad = gst_pad_new_from_template (klass->sink_template, "sink");
260 gst_pad_set_event_function (demux->sinkpad,
261 GST_DEBUG_FUNCPTR (gst_ps_demux_sink_event));
262 gst_pad_set_chain_function (demux->sinkpad,
263 GST_DEBUG_FUNCPTR (gst_ps_demux_chain));
264 gst_pad_set_activate_function (demux->sinkpad,
265 GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate));
266 gst_pad_set_activatemode_function (demux->sinkpad,
267 GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate_mode));
269 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
272 g_malloc0 (sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
273 demux->streams_found =
274 g_malloc0 (sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
275 demux->found_count = 0;
277 demux->adapter = gst_adapter_new ();
278 demux->rev_adapter = gst_adapter_new ();
279 demux->flowcombiner = gst_flow_combiner_new ();
281 gst_ps_demux_reset (demux);
285 gst_ps_demux_finalize (GstPsDemux * demux)
287 gst_ps_demux_reset (demux);
288 g_free (demux->streams);
289 g_free (demux->streams_found);
291 gst_flow_combiner_free (demux->flowcombiner);
292 g_object_unref (demux->adapter);
293 g_object_unref (demux->rev_adapter);
295 G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (demux));
299 gst_ps_demux_reset (GstPsDemux * demux)
301 /* Clean up the streams and pads we allocated */
304 for (i = 0; i < GST_PS_DEMUX_MAX_STREAMS; i++) {
305 GstPsStream *stream = demux->streams[i];
307 if (stream != NULL) {
308 if (stream->pad && GST_PAD_PARENT (stream->pad)) {
309 gst_flow_combiner_remove_pad (demux->flowcombiner, stream->pad);
310 gst_element_remove_pad (GST_ELEMENT_CAST (demux), stream->pad);
312 gst_object_unref (stream->pad);
315 if (stream->pending_tags)
316 gst_tag_list_unref (stream->pending_tags);
318 demux->streams[i] = NULL;
321 memset (demux->streams_found, 0,
322 sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
323 demux->found_count = 0;
325 gst_adapter_clear (demux->adapter);
326 gst_adapter_clear (demux->rev_adapter);
328 demux->adapter_offset = G_MAXUINT64;
329 demux->first_scr = G_MAXUINT64;
330 demux->last_scr = G_MAXUINT64;
331 demux->current_scr = G_MAXUINT64;
332 demux->base_time = G_MAXUINT64;
333 demux->scr_rate_n = G_MAXUINT64;
334 demux->scr_rate_d = G_MAXUINT64;
335 demux->first_pts = G_MAXUINT64;
336 demux->last_pts = G_MAXUINT64;
337 demux->mux_rate = G_MAXUINT64;
338 demux->next_pts = G_MAXUINT64;
339 demux->next_dts = G_MAXUINT64;
340 demux->need_no_more_pads = TRUE;
341 demux->adjust_segment = TRUE;
342 gst_ps_demux_reset_psm (demux);
343 gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
344 gst_segment_init (&demux->src_segment, GST_FORMAT_TIME);
345 gst_ps_demux_flush (demux);
346 demux->have_group_id = FALSE;
347 demux->group_id = G_MAXUINT;
351 gst_ps_demux_create_stream (GstPsDemux * demux, gint id, gint stream_type)
354 GstPadTemplate *template;
356 GstPsDemuxClass *klass = GST_PS_DEMUX_GET_CLASS (demux);
358 GstClockTime threshold = SEGMENT_THRESHOLD;
366 GST_DEBUG_OBJECT (demux, "create stream id 0x%02x, type 0x%02x", id,
369 switch (stream_type) {
373 case ST_GST_VIDEO_MPEG1_OR_2:
375 gint mpeg_version = 1;
376 if (stream_type == ST_VIDEO_MPEG2 ||
377 (stream_type == ST_GST_VIDEO_MPEG1_OR_2 && demux->is_mpeg2_pack)) {
380 if (stream_type == ST_VIDEO_MPEG4) {
384 template = klass->video_template;
385 name = g_strdup_printf ("video_%02x", id);
386 caps = gst_caps_new_simple ("video/mpeg",
387 "mpegversion", G_TYPE_INT, mpeg_version,
388 "systemstream", G_TYPE_BOOLEAN, FALSE,
389 "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
390 threshold = VIDEO_SEGMENT_THRESHOLD;
395 template = klass->audio_template;
396 name = g_strdup_printf ("audio_%02x", id);
397 caps = gst_caps_new_simple ("audio/mpeg",
398 "mpegversion", G_TYPE_INT, 1, NULL);
400 case ST_PRIVATE_SECTIONS:
401 case ST_PRIVATE_DATA:
405 case ST_AUDIO_AAC_ADTS:
406 template = klass->audio_template;
407 name = g_strdup_printf ("audio_%02x", id);
408 caps = gst_caps_new_simple ("audio/mpeg",
409 "mpegversion", G_TYPE_INT, 4,
410 "stream-format", G_TYPE_STRING, "adts", NULL);
412 case ST_AUDIO_AAC_LOAS: // LATM/LOAS AAC syntax
413 template = klass->audio_template;
414 name = g_strdup_printf ("audio_%02x", id);
415 caps = gst_caps_new_simple ("audio/mpeg",
416 "mpegversion", G_TYPE_INT, 4,
417 "stream-format", G_TYPE_STRING, "loas", NULL);
420 template = klass->video_template;
421 name = g_strdup_printf ("video_%02x", id);
422 caps = gst_caps_new_simple ("video/x-h264",
423 "stream-format", G_TYPE_STRING, "byte-stream", NULL);
424 threshold = VIDEO_SEGMENT_THRESHOLD;
427 template = klass->video_template;
428 name = g_strdup_printf ("video_%02x", id);
429 caps = gst_caps_new_simple ("video/x-h265",
430 "stream-format", G_TYPE_STRING, "byte-stream", NULL);
431 threshold = VIDEO_SEGMENT_THRESHOLD;
434 case ST_PS_AUDIO_AC3:
435 template = klass->audio_template;
436 name = g_strdup_printf ("audio_%02x", id);
437 caps = gst_caps_new_empty_simple ("audio/x-private1-ac3");
439 case ST_PS_AUDIO_DTS:
440 template = klass->audio_template;
441 name = g_strdup_printf ("audio_%02x", id);
442 caps = gst_caps_new_empty_simple ("audio/x-private1-dts");
444 case ST_PS_AUDIO_LPCM:
445 template = klass->audio_template;
446 name = g_strdup_printf ("audio_%02x", id);
447 caps = gst_caps_new_empty_simple ("audio/x-private1-lpcm");
449 case ST_PS_DVD_SUBPICTURE:
450 template = klass->subpicture_template;
451 name = g_strdup_printf ("subpicture_%02x", id);
452 caps = gst_caps_new_empty_simple ("subpicture/x-dvd");
454 case ST_GST_AUDIO_RAWA52:
455 template = klass->audio_template;
456 name = g_strdup_printf ("audio_%02x", id);
457 caps = gst_caps_new_empty_simple ("audio/ac3");
463 if (name == NULL || template == NULL || caps == NULL) {
466 gst_caps_unref (caps);
470 stream = g_new0 (GstPsStream, 1);
472 stream->discont = TRUE;
473 stream->need_segment = TRUE;
474 stream->notlinked = FALSE;
475 stream->type = stream_type;
476 stream->pending_tags = NULL;
477 stream->pad = gst_pad_new_from_template (template, name);
478 stream->segment_thresh = threshold;
479 gst_pad_set_event_function (stream->pad,
480 GST_DEBUG_FUNCPTR (gst_ps_demux_src_event));
481 gst_pad_set_query_function (stream->pad,
482 GST_DEBUG_FUNCPTR (gst_ps_demux_src_query));
483 gst_pad_use_fixed_caps (stream->pad);
485 /* needed for set_caps to work */
486 if (!gst_pad_set_active (stream->pad, TRUE)) {
487 GST_WARNING_OBJECT (demux, "Failed to activate pad %" GST_PTR_FORMAT,
492 gst_pad_create_stream_id_printf (stream->pad, GST_ELEMENT_CAST (demux),
495 event = gst_pad_get_sticky_event (demux->sinkpad, GST_EVENT_STREAM_START, 0);
497 if (gst_event_parse_group_id (event, &demux->group_id))
498 demux->have_group_id = TRUE;
500 demux->have_group_id = FALSE;
501 gst_event_unref (event);
502 } else if (!demux->have_group_id) {
503 demux->have_group_id = TRUE;
504 demux->group_id = gst_util_group_id_next ();
506 event = gst_event_new_stream_start (stream_id);
507 if (demux->have_group_id)
508 gst_event_set_group_id (event, demux->group_id);
510 gst_pad_push_event (stream->pad, event);
513 gst_pad_set_caps (stream->pad, caps);
515 if (!stream->pending_tags)
516 stream->pending_tags = gst_tag_list_new_empty ();
517 gst_pb_utils_add_codec_description_to_tag_list (stream->pending_tags, NULL,
520 GST_DEBUG_OBJECT (demux, "create pad %s, caps %" GST_PTR_FORMAT, name, caps);
521 gst_caps_unref (caps);
528 gst_ps_demux_get_stream (GstPsDemux * demux, gint id, gint type)
530 GstPsStream *stream = demux->streams[id];
532 if (stream == NULL) {
533 if (!(stream = gst_ps_demux_create_stream (demux, id, type)))
536 GST_DEBUG_OBJECT (demux, "adding pad for stream id 0x%02x type 0x%02x", id,
539 if (demux->need_no_more_pads) {
540 gst_element_add_pad (GST_ELEMENT (demux), stream->pad);
541 gst_flow_combiner_add_pad (demux->flowcombiner, stream->pad);
543 /* only likely to confuse decodebin etc, so discard */
544 /* FIXME should perform full switch protocol:
545 * add a whole new set of pads, drop old and no-more-pads again */
546 GST_DEBUG_OBJECT (demux,
547 "but already signalled no-more-pads; not adding");
548 gst_object_ref_sink (stream->pad);
551 demux->streams[id] = stream;
552 demux->streams_found[demux->found_count++] = stream;
559 GST_DEBUG_OBJECT (demux, "unknown stream id 0x%02x type 0x%02x", id, type);
565 gst_ps_demux_get_stream_from_pad (GstPsDemux * demux, GstPad * srcpad)
569 count = demux->found_count;
570 for (i = 0; i < count; i++) {
571 GstPsStream *stream = demux->streams_found[i];
573 if (stream && stream->pad == srcpad)
577 GST_DEBUG_OBJECT (srcpad, "no stream found for pad!");
582 gst_ps_demux_send_segment (GstPsDemux * demux, GstPsStream * stream,
586 if (G_UNLIKELY (stream->need_segment)) {
589 GST_DEBUG ("PTS timestamp:%" GST_TIME_FORMAT " base_time %" GST_TIME_FORMAT
590 " src_segment.start:%" GST_TIME_FORMAT " .stop:%" GST_TIME_FORMAT,
591 GST_TIME_ARGS (pts), GST_TIME_ARGS (demux->base_time),
592 GST_TIME_ARGS (demux->src_segment.start),
593 GST_TIME_ARGS (demux->src_segment.stop));
595 /* adjust segment start if estimating a seek was off quite a bit,
596 * make sure to do for all streams though to preserve a/v sync */
597 /* FIXME such adjustment tends to be frowned upon */
598 if (pts != GST_CLOCK_TIME_NONE && demux->adjust_segment) {
599 if (demux->src_segment.rate > 0) {
600 if (GST_CLOCK_DIFF (demux->src_segment.start, pts) > GST_SECOND)
601 demux->src_segment.start = pts - demux->base_time;
603 if (GST_CLOCK_DIFF (demux->src_segment.stop, pts) > GST_SECOND)
604 demux->src_segment.stop = pts - demux->base_time;
607 demux->adjust_segment = FALSE;
609 /* we should be in sync with downstream, so start from our segment notion,
610 * which also includes proper base_time etc, tweak it a bit and send */
611 gst_segment_copy_into (&demux->src_segment, &segment);
612 if (GST_CLOCK_TIME_IS_VALID (demux->base_time)) {
613 if (GST_CLOCK_TIME_IS_VALID (segment.start))
614 segment.start += demux->base_time;
615 if (GST_CLOCK_TIME_IS_VALID (segment.stop))
616 segment.stop += demux->base_time;
617 segment.time = segment.start - demux->base_time;
620 GST_INFO_OBJECT (demux, "sending segment event %" GST_SEGMENT_FORMAT
621 " to pad %" GST_PTR_FORMAT, &segment, stream->pad);
623 gst_pad_push_event (stream->pad, gst_event_new_segment (&segment));
625 stream->need_segment = FALSE;
628 if (G_UNLIKELY (stream->pending_tags)) {
629 GST_DEBUG_OBJECT (demux, "Sending pending_tags %p for pad %s:%s : %"
630 GST_PTR_FORMAT, stream->pending_tags,
631 GST_DEBUG_PAD_NAME (stream->pad), stream->pending_tags);
632 gst_pad_push_event (stream->pad, gst_event_new_tag (stream->pending_tags));
633 stream->pending_tags = NULL;
638 gst_ps_demux_send_data (GstPsDemux * demux, GstPsStream * stream,
641 GstFlowReturn result;
642 GstClockTime pts = GST_CLOCK_TIME_NONE, dts = GST_CLOCK_TIME_NONE;
648 if (G_UNLIKELY (demux->next_pts != G_MAXUINT64))
649 pts = MPEGTIME_TO_GSTTIME (demux->next_pts);
650 if (G_UNLIKELY (demux->next_dts != G_MAXUINT64))
651 dts = MPEGTIME_TO_GSTTIME (demux->next_dts);
653 gst_ps_demux_send_segment (demux, stream, pts);
655 /* OK, sent new segment now prepare the buffer for sending */
656 GST_BUFFER_PTS (buf) = pts;
657 GST_BUFFER_DTS (buf) = dts;
659 /* update position in the segment */
660 gst_segment_set_position (&demux->src_segment, GST_FORMAT_TIME,
661 MPEGTIME_TO_GSTTIME (demux->current_scr - demux->first_scr));
663 GST_LOG_OBJECT (demux, "last stop position is now %" GST_TIME_FORMAT
664 " current scr is %" GST_TIME_FORMAT,
665 GST_TIME_ARGS (demux->src_segment.position),
666 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->current_scr)));
668 if (demux->src_segment.position != GST_CLOCK_TIME_NONE &&
669 demux->base_time != GST_CLOCK_TIME_NONE) {
670 GstClockTime new_time = demux->base_time + demux->src_segment.position;
672 if (stream->last_ts == GST_CLOCK_TIME_NONE || stream->last_ts < new_time) {
673 GST_LOG_OBJECT (demux,
674 "last_ts update on pad %s to time %" GST_TIME_FORMAT,
675 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (new_time));
676 stream->last_ts = new_time;
679 gst_ps_demux_send_gap_updates (demux, new_time);
682 /* Set the buffer discont flag, and clear discont state on the stream */
683 if (stream->discont) {
684 GST_DEBUG_OBJECT (demux, "discont buffer to pad %" GST_PTR_FORMAT
685 " with PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT,
686 stream->pad, GST_TIME_ARGS (pts), GST_TIME_ARGS (dts));
687 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
689 stream->discont = FALSE;
691 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
694 demux->next_pts = G_MAXUINT64;
695 demux->next_dts = G_MAXUINT64;
697 GST_LOG_OBJECT (demux, "pushing stream id 0x%02x type 0x%02x, pts time: %"
698 GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT,
699 stream->id, stream->type, GST_TIME_ARGS (pts), gst_buffer_get_size (buf));
700 result = gst_pad_push (stream->pad, buf);
701 GST_LOG_OBJECT (demux, "result: %s", gst_flow_get_name (result));
708 GST_DEBUG_OBJECT (demux, "no stream given");
709 gst_buffer_unref (buf);
715 gst_ps_demux_mark_discont (GstPsDemux * demux, gboolean discont,
716 gboolean need_segment)
718 gint i, count = demux->found_count;
720 /* mark discont on all streams */
721 for (i = 0; i < count; i++) {
722 GstPsStream *stream = demux->streams_found[i];
724 if (G_LIKELY (stream)) {
725 stream->discont |= discont;
726 stream->need_segment |= need_segment;
727 demux->adjust_segment |= need_segment;
728 GST_DEBUG_OBJECT (demux, "marked stream as discont %d, need_segment %d",
729 stream->discont, stream->need_segment);
735 gst_ps_demux_send_event (GstPsDemux * demux, GstEvent * event)
737 gint i, count = demux->found_count;
738 gboolean ret = FALSE;
740 for (i = 0; i < count; i++) {
741 GstPsStream *stream = demux->streams_found[i];
744 if (!gst_pad_push_event (stream->pad, gst_event_ref (event))) {
745 GST_DEBUG_OBJECT (stream->pad, "%s event was not handled",
746 GST_EVENT_TYPE_NAME (event));
748 /* If at least one push returns TRUE, then we return TRUE. */
749 GST_DEBUG_OBJECT (stream->pad, "%s event was handled",
750 GST_EVENT_TYPE_NAME (event));
756 gst_event_unref (event);
761 gst_ps_demux_handle_dvd_event (GstPsDemux * demux, GstEvent * event)
763 const GstStructure *structure = gst_event_get_structure (event);
764 const char *type = gst_structure_get_string (structure, "event");
766 gchar cur_stream_name[32];
767 GstPsStream *temp = NULL;
768 const gchar *lang_code;
770 if (strcmp (type, "dvd-lang-codes") == 0) {
771 GST_DEBUG_OBJECT (demux, "Handling language codes event");
773 /* Create a video pad to ensure have it before emit no more pads */
774 (void) gst_ps_demux_get_stream (demux, 0xe0, ST_VIDEO_MPEG2);
776 /* Read out the languages for audio streams and request each one that
778 for (i = 0; i < MAX_DVD_AUDIO_STREAMS; i++) {
782 g_snprintf (cur_stream_name, 32, "audio-%d-format", i);
783 if (!gst_structure_get_int (structure, cur_stream_name, &stream_format))
786 g_snprintf (cur_stream_name, 32, "audio-%d-stream", i);
787 if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
789 if (stream_id < 0 || stream_id >= MAX_DVD_AUDIO_STREAMS)
792 switch (stream_format) {
796 GST_DEBUG_OBJECT (demux,
797 "Audio stream %d format %d ID 0x%02x - AC3", i,
798 stream_format, stream_id);
799 temp = gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_AC3);
803 /* MPEG audio without and with extension stream are
804 * treated the same */
806 GST_DEBUG_OBJECT (demux,
807 "Audio stream %d format %d ID 0x%02x - MPEG audio", i,
808 stream_format, stream_id);
809 temp = gst_ps_demux_get_stream (demux, stream_id, ST_AUDIO_MPEG1);
814 GST_DEBUG_OBJECT (demux,
815 "Audio stream %d format %d ID 0x%02x - DVD LPCM", i,
816 stream_format, stream_id);
817 temp = gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_LPCM);
822 GST_DEBUG_OBJECT (demux,
823 "Audio stream %d format %d ID 0x%02x - DTS", i,
824 stream_format, stream_id);
825 temp = gst_ps_demux_get_stream (demux, stream_id, ST_PS_AUDIO_DTS);
828 /* FIXME: What range is SDDS? */
830 GST_WARNING_OBJECT (demux,
831 "Unknown audio stream format in language code event: %d",
840 g_snprintf (cur_stream_name, 32, "audio-%d-language", i);
841 lang_code = gst_structure_get_string (structure, cur_stream_name);
843 GstTagList *list = temp->pending_tags;
846 list = gst_tag_list_new_empty ();
847 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
848 GST_TAG_LANGUAGE_CODE, lang_code, NULL);
849 temp->pending_tags = list;
853 /* And subtitle streams */
854 for (i = 0; i < MAX_DVD_SUBPICTURE_STREAMS; i++) {
857 g_snprintf (cur_stream_name, 32, "subpicture-%d-format", i);
858 if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
861 g_snprintf (cur_stream_name, 32, "subpicture-%d-stream", i);
862 if (!gst_structure_get_int (structure, cur_stream_name, &stream_id))
864 if (stream_id < 0 || stream_id >= MAX_DVD_SUBPICTURE_STREAMS)
867 GST_DEBUG_OBJECT (demux, "Subpicture stream %d ID 0x%02x", i,
870 /* Retrieve the subpicture stream to force pad creation */
871 temp = gst_ps_demux_get_stream (demux, 0x20 + stream_id,
872 ST_PS_DVD_SUBPICTURE);
876 g_snprintf (cur_stream_name, 32, "subpicture-%d-language", i);
877 lang_code = gst_structure_get_string (structure, cur_stream_name);
879 GstTagList *list = temp->pending_tags;
882 list = gst_tag_list_new_empty ();
883 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
884 GST_TAG_LANGUAGE_CODE, lang_code, NULL);
885 temp->pending_tags = list;
889 GST_DEBUG_OBJECT (demux, "Created all pads from Language Codes event, "
890 "signalling no-more-pads");
892 gst_element_no_more_pads (GST_ELEMENT (demux));
893 demux->need_no_more_pads = FALSE;
895 /* forward to all pads, e.g. dvd clut event */
896 gst_event_ref (event);
897 gst_ps_demux_send_event (demux, event);
900 gst_event_unref (event);
905 gst_ps_demux_flush (GstPsDemux * demux)
907 GST_DEBUG_OBJECT (demux, "flushing demuxer");
908 gst_adapter_clear (demux->adapter);
909 gst_adapter_clear (demux->rev_adapter);
910 gst_pes_filter_drain (&demux->filter);
911 gst_ps_demux_clear_times (demux);
912 demux->adapter_offset = G_MAXUINT64;
913 demux->current_scr = G_MAXUINT64;
914 demux->bytes_since_scr = 0;
918 gst_ps_demux_clear_times (GstPsDemux * demux)
920 gint i, count = demux->found_count;
922 gst_flow_combiner_reset (demux->flowcombiner);
923 /* Clear the last ts for all streams */
924 for (i = 0; i < count; i++) {
925 GstPsStream *stream = demux->streams_found[i];
927 if (G_LIKELY (stream)) {
928 stream->last_ts = GST_CLOCK_TIME_NONE;
934 gst_ps_demux_send_gap_updates (GstPsDemux * demux, GstClockTime new_start)
936 GstClockTime base_time, stop;
937 gint i, count = demux->found_count;
938 GstEvent *event = NULL;
940 /* Advance all lagging streams by sending a gap event */
941 if ((base_time = demux->base_time) == GST_CLOCK_TIME_NONE)
944 stop = demux->src_segment.stop;
945 if (stop != GST_CLOCK_TIME_NONE)
948 if (new_start > stop)
951 /* FIXME: Handle reverse playback */
952 for (i = 0; i < count; i++) {
953 GstPsStream *stream = demux->streams_found[i];
956 if (stream->last_ts == GST_CLOCK_TIME_NONE ||
957 stream->last_ts < demux->src_segment.start + base_time)
958 stream->last_ts = demux->src_segment.start + base_time;
960 if (stream->last_ts + stream->segment_thresh < new_start) {
961 /* should send segment info before gap event */
962 gst_ps_demux_send_segment (demux, stream, GST_CLOCK_TIME_NONE);
964 GST_LOG_OBJECT (demux,
965 "Sending gap update to pad %s time %" GST_TIME_FORMAT,
966 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (new_start));
968 gst_event_new_gap (stream->last_ts, new_start - stream->last_ts);
969 gst_pad_push_event (stream->pad, event);
970 stream->last_ts = new_start;
976 static inline gboolean
977 have_open_streams (GstPsDemux * demux)
979 return (demux->streams_found[0] != NULL);
983 gst_ps_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
986 GstPsDemux *demux = GST_PS_DEMUX (parent);
988 switch (GST_EVENT_TYPE (event)) {
989 case GST_EVENT_FLUSH_START:
990 gst_ps_demux_send_event (demux, event);
992 case GST_EVENT_FLUSH_STOP:
993 gst_ps_demux_send_event (demux, event);
994 gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
995 gst_ps_demux_flush (demux);
997 case GST_EVENT_SEGMENT:
999 const GstSegment *segment;
1001 gst_event_parse_segment (event, &segment);
1002 gst_segment_copy_into (segment, &demux->sink_segment);
1004 GST_INFO_OBJECT (demux, "received segment %" GST_SEGMENT_FORMAT, segment);
1006 /* we need to emit a new segment */
1007 gst_ps_demux_mark_discont (demux, TRUE, TRUE);
1009 if (segment->format == GST_FORMAT_BYTES
1010 && demux->scr_rate_n != G_MAXUINT64
1011 && demux->scr_rate_d != G_MAXUINT64) {
1012 demux->src_segment.rate = segment->rate;
1013 demux->src_segment.applied_rate = segment->applied_rate;
1014 demux->src_segment.format = GST_FORMAT_TIME;
1015 demux->src_segment.start = BYTES_TO_GSTTIME (segment->start);
1016 demux->src_segment.stop = BYTES_TO_GSTTIME (segment->stop);
1017 demux->src_segment.time = BYTES_TO_GSTTIME (segment->time);
1018 } else if (segment->format == GST_FORMAT_TIME) {
1019 /* we expect our timeline (SCR, PTS) to match the one from upstream,
1020 * if not, will adjust with offset later on */
1021 gst_segment_copy_into (segment, &demux->src_segment);
1022 /* accept upstream segment without adjusting */
1023 demux->adjust_segment = FALSE;
1026 gst_event_unref (event);
1031 GST_INFO_OBJECT (demux, "Received EOS");
1032 if (!gst_ps_demux_send_event (demux, event)
1033 && !have_open_streams (demux)) {
1034 GST_WARNING_OBJECT (demux, "EOS and no streams open");
1035 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
1036 ("Internal data stream error."), ("No valid streams detected"));
1039 case GST_EVENT_CUSTOM_DOWNSTREAM:
1040 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1042 const GstStructure *structure = gst_event_get_structure (event);
1044 if (structure != NULL
1045 && gst_structure_has_name (structure, "application/x-gst-dvd")) {
1046 res = gst_ps_demux_handle_dvd_event (demux, event);
1048 gst_ps_demux_send_event (demux, event);
1052 case GST_EVENT_CAPS:
1053 gst_event_unref (event);
1056 gst_ps_demux_send_event (demux, event);
1064 gst_ps_demux_handle_seek_push (GstPsDemux * demux, GstEvent * event)
1066 gboolean res = FALSE;
1070 GstSeekType start_type, stop_type;
1072 gint64 bstart, bstop;
1075 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
1078 GST_DEBUG_OBJECT (demux, "seek event, rate: %f start: %" GST_TIME_FORMAT
1079 " stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
1080 GST_TIME_ARGS (stop));
1082 if (format == GST_FORMAT_BYTES) {
1083 GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format);
1087 GST_DEBUG_OBJECT (demux, "seek - trying directly upstream first");
1089 /* first try original format seek */
1090 (void) gst_event_ref (event);
1091 if ((res = gst_pad_push_event (demux->sinkpad, event)))
1094 if (format != GST_FORMAT_TIME) {
1095 /* From here down, we only support time based seeks */
1096 GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format);
1100 /* We need to convert to byte based seek and we need a scr_rate for that. */
1101 if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64) {
1102 GST_DEBUG_OBJECT (demux, "seek not possible, no scr_rate");
1106 GST_DEBUG_OBJECT (demux, "try with scr_rate interpolation");
1108 bstart = GSTTIME_TO_BYTES ((guint64) start);
1109 bstop = GSTTIME_TO_BYTES ((guint64) stop);
1111 GST_DEBUG_OBJECT (demux, "in bytes bstart %" G_GINT64_FORMAT " bstop %"
1112 G_GINT64_FORMAT, bstart, bstop);
1113 bevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, start_type,
1114 bstart, stop_type, bstop);
1116 res = gst_pad_push_event (demux->sinkpad, bevent);
1119 gst_event_unref (event);
1124 gst_event_unref (event);
1130 #define MAX_RECURSION_COUNT 100
1132 /* Binary search for requested SCR */
1133 static inline guint64
1134 find_offset (GstPsDemux * demux, guint64 scr,
1135 guint64 min_scr, guint64 min_scr_offset,
1136 guint64 max_scr, guint64 max_scr_offset, int recursion_count)
1138 guint64 scr_rate_n = max_scr_offset - min_scr_offset;
1139 guint64 scr_rate_d = max_scr - min_scr;
1143 if (recursion_count > MAX_RECURSION_COUNT) {
1147 offset = min_scr_offset +
1148 MIN (gst_util_uint64_scale (scr - min_scr, scr_rate_n,
1149 scr_rate_d), demux->sink_segment.stop);
1151 if (!gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0)) {
1152 gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1155 if (fscr == scr || fscr == min_scr || fscr == max_scr) {
1160 return find_offset (demux, scr, fscr, offset, max_scr, max_scr_offset,
1161 recursion_count + 1);
1163 return find_offset (demux, scr, min_scr, min_scr_offset, fscr, offset,
1164 recursion_count + 1);
1168 static inline gboolean
1169 gst_ps_demux_do_seek (GstPsDemux * demux, GstSegment * seeksegment)
1172 guint64 fscr, offset;
1173 guint64 scr = GSTTIME_TO_MPEGTIME (seeksegment->position + demux->base_time);
1175 /* In some clips the PTS values are completely unaligned with SCR values.
1176 * To improve the seek in that situation we apply a factor considering the
1177 * relationship between last PTS and last SCR */
1178 if (demux->last_scr > demux->last_pts)
1179 scr = gst_util_uint64_scale (scr, demux->last_scr, demux->last_pts);
1181 scr = MIN (demux->last_scr, scr);
1182 scr = MAX (demux->first_scr, scr);
1185 GST_INFO_OBJECT (demux, "sink segment configured %" GST_SEGMENT_FORMAT
1186 ", trying to go at SCR: %" G_GUINT64_FORMAT, &demux->sink_segment, scr);
1189 find_offset (demux, scr, demux->first_scr, demux->first_scr_offset,
1190 demux->last_scr, demux->last_scr_offset, 0);
1192 if (offset == (guint64) - 1) {
1196 found = gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1198 found = gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1200 while (found && fscr < scr) {
1202 found = gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1205 while (found && fscr > scr && offset > 0) {
1207 found = gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr, 0);
1210 GST_INFO_OBJECT (demux, "doing seek at offset %" G_GUINT64_FORMAT
1211 " SCR: %" G_GUINT64_FORMAT " %" GST_TIME_FORMAT,
1212 offset, fscr, GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (fscr)));
1214 gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
1220 gst_ps_demux_handle_seek_pull (GstPsDemux * demux, GstEvent * event)
1224 GstSeekType start_type, stop_type;
1227 gboolean update, flush;
1228 GstSegment seeksegment;
1229 GstClockTime first_pts = MPEGTIME_TO_GSTTIME (demux->first_pts);
1231 gst_event_parse_seek (event, &rate, &format, &flags,
1232 &start_type, &start, &stop_type, &stop);
1234 if (format != GST_FORMAT_TIME)
1237 GST_DEBUG_OBJECT (demux, "Seek requested start %" GST_TIME_FORMAT " stop %"
1238 GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1240 /* We need to convert to byte based seek and we need a scr_rate for that. */
1241 if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64)
1244 flush = flags & GST_SEEK_FLAG_FLUSH;
1245 /* keyframe = flags & GST_SEEK_FLAG_KEY_UNIT; *//* FIXME */
1248 /* Flush start up and downstream to make sure data flow and loops are
1250 demux->flushing = TRUE;
1251 gst_ps_demux_send_event (demux, gst_event_new_flush_start ());
1252 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_start ());
1254 /* Pause the pulling task */
1255 gst_pad_pause_task (demux->sinkpad);
1258 /* Take the stream lock */
1259 GST_PAD_STREAM_LOCK (demux->sinkpad);
1262 /* Stop flushing upstream we need to pull */
1263 demux->flushing = FALSE;
1264 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_stop (TRUE));
1267 /* Work on a copy until we are sure the seek succeeded. */
1268 memcpy (&seeksegment, &demux->src_segment, sizeof (GstSegment));
1270 GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
1271 &demux->src_segment);
1273 /* Apply the seek to our segment */
1274 if (!gst_segment_do_seek (&seeksegment, rate, format, flags,
1275 start_type, start, stop_type, stop, &update))
1278 GST_DEBUG_OBJECT (demux, "seek segment configured %" GST_SEGMENT_FORMAT,
1281 if (flush || seeksegment.position != demux->src_segment.position) {
1282 /* Do the actual seeking */
1283 if (!gst_ps_demux_do_seek (demux, &seeksegment)) {
1284 #ifdef TIZEN_FEATURE_MPEGDEMUX_MODIFICATION
1292 /* check the limits */
1293 if (seeksegment.rate > 0.0 && first_pts != G_MAXUINT64) {
1294 if (seeksegment.start < first_pts - demux->base_time) {
1295 seeksegment.start = first_pts - demux->base_time;
1296 seeksegment.position = seeksegment.start;
1300 /* update the rate in our src segment */
1301 demux->sink_segment.rate = rate;
1303 GST_DEBUG_OBJECT (demux, "seek segment adjusted %" GST_SEGMENT_FORMAT,
1307 /* Stop flushing, the sinks are at time 0 now */
1308 gst_ps_demux_send_event (demux, gst_event_new_flush_stop (TRUE));
1311 if (flush || seeksegment.position != demux->src_segment.position) {
1312 gst_ps_demux_flush (demux);
1315 /* Ok seek succeeded, take the newly configured segment */
1316 memcpy (&demux->src_segment, &seeksegment, sizeof (GstSegment));
1318 /* Notify about the start of a new segment */
1319 if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
1320 gst_element_post_message (GST_ELEMENT (demux),
1321 gst_message_new_segment_start (GST_OBJECT (demux),
1322 demux->src_segment.format, demux->src_segment.position));
1325 /* Tell all the stream a new segment is needed */
1326 gst_ps_demux_mark_discont (demux, TRUE, TRUE);
1328 gst_pad_start_task (demux->sinkpad,
1329 (GstTaskFunction) gst_ps_demux_loop, demux->sinkpad, NULL);
1331 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
1333 gst_event_unref (event);
1339 GST_WARNING_OBJECT (demux, "we only support seeking in TIME or BYTES "
1341 gst_event_unref (event);
1346 GST_WARNING_OBJECT (demux, "seek not possible, no scr_rate");
1347 gst_event_unref (event);
1352 GST_WARNING_OBJECT (demux, "couldn't perform seek");
1353 #ifdef TIZEN_FEATURE_MPEGDEMUX_MODIFICATION
1354 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
1356 gst_event_unref (event);
1362 gst_ps_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1364 gboolean res = FALSE;
1365 GstPsDemux *demux = GST_PS_DEMUX (parent);
1367 switch (GST_EVENT_TYPE (event)) {
1368 case GST_EVENT_SEEK:
1369 if (demux->random_access) {
1370 res = gst_ps_demux_handle_seek_pull (demux, event);
1372 res = gst_ps_demux_handle_seek_push (demux, event);
1375 case GST_EVENT_RECONFIGURE:{
1376 GstPsStream *stream;
1378 stream = gst_ps_demux_get_stream_from_pad (demux, pad);
1380 stream->notlinked = FALSE;
1382 gst_event_unref (event);
1387 res = gst_pad_push_event (demux->sinkpad, event);
1395 gst_ps_demux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1397 gboolean res = FALSE;
1398 GstPsDemux *demux = GST_PS_DEMUX (parent);
1400 GST_LOG_OBJECT (demux, "Have query of type %d on pad %" GST_PTR_FORMAT,
1401 GST_QUERY_TYPE (query), pad);
1403 switch (GST_QUERY_TYPE (query)) {
1404 case GST_QUERY_POSITION:
1409 /* See if upstream can immediately answer */
1410 res = gst_pad_peer_query (demux->sinkpad, query);
1414 gst_query_parse_position (query, &format, NULL);
1416 if (format != GST_FORMAT_TIME) {
1417 GST_DEBUG_OBJECT (demux, "position not supported for format: %s",
1418 gst_format_get_name (format));
1422 pos = demux->src_segment.position - demux->src_segment.start;
1423 GST_LOG_OBJECT (demux, "Position %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
1425 gst_query_set_position (query, format, pos);
1429 case GST_QUERY_DURATION:
1433 GstQuery *byte_query;
1435 gst_query_parse_duration (query, &format, NULL);
1437 if (G_LIKELY (format == GST_FORMAT_TIME &&
1438 GST_CLOCK_TIME_IS_VALID (demux->src_segment.duration))) {
1439 gst_query_set_duration (query, GST_FORMAT_TIME,
1440 demux->src_segment.duration);
1445 /* For any format other than bytes, see if upstream knows first */
1446 if (format == GST_FORMAT_BYTES) {
1447 GST_DEBUG_OBJECT (demux, "duration not supported for format: %s",
1448 gst_format_get_name (format));
1452 if (gst_pad_peer_query (demux->sinkpad, query)) {
1457 /* Upstream didn't know, so we can only answer TIME queries from
1459 if (format != GST_FORMAT_TIME) {
1460 GST_DEBUG_OBJECT (demux, "duration not supported for format: %s",
1461 gst_format_get_name (format));
1465 if (demux->mux_rate == -1) {
1466 GST_DEBUG_OBJECT (demux, "duration not possible, no mux_rate");
1470 byte_query = gst_query_new_duration (GST_FORMAT_BYTES);
1472 if (!gst_pad_peer_query (demux->sinkpad, byte_query)) {
1473 GST_LOG_OBJECT (demux, "query on peer pad failed");
1474 gst_query_unref (byte_query);
1478 gst_query_parse_duration (byte_query, &format, &duration);
1479 gst_query_unref (byte_query);
1481 GST_LOG_OBJECT (demux,
1482 "query on peer pad reported bytes %" G_GUINT64_FORMAT, duration);
1484 duration = BYTES_TO_GSTTIME ((guint64) duration);
1486 GST_LOG_OBJECT (demux, "converted to time %" GST_TIME_FORMAT,
1487 GST_TIME_ARGS (duration));
1489 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
1493 case GST_QUERY_SEEKING:{
1496 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
1499 if (demux->random_access) {
1500 /* In pull mode we can seek in TIME format if we have the SCR */
1501 if (fmt != GST_FORMAT_TIME || demux->scr_rate_n == G_MAXUINT64
1502 || demux->scr_rate_d == G_MAXUINT64)
1503 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1505 gst_query_set_seeking (query, fmt, TRUE, 0, -1);
1507 if (fmt == GST_FORMAT_BYTES) {
1508 /* Seeking in BYTES format not supported at all */
1509 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1511 GstQuery *peerquery;
1514 /* Then ask upstream */
1515 res = gst_pad_peer_query (demux->sinkpad, query);
1517 /* If upstream can handle seeks we're done, if it
1518 * can't we still have our TIME->BYTES conversion seek
1520 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
1521 if (seekable || fmt != GST_FORMAT_TIME)
1525 /* We can seek if upstream supports BYTES seeks and we
1528 peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
1529 res = gst_pad_peer_query (demux->sinkpad, peerquery);
1530 if (!res || demux->scr_rate_n == G_MAXUINT64
1531 || demux->scr_rate_d == G_MAXUINT64) {
1532 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1534 gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
1536 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, -1);
1538 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1541 gst_query_unref (peerquery);
1547 case GST_QUERY_SEGMENT:{
1551 format = demux->src_segment.format;
1554 gst_segment_to_stream_time (&demux->src_segment, format,
1555 demux->src_segment.start);
1556 if ((stop = demux->src_segment.stop) == -1)
1557 stop = demux->src_segment.duration;
1559 stop = gst_segment_to_stream_time (&demux->src_segment, format, stop);
1561 gst_query_set_segment (query, demux->src_segment.rate, format, start,
1567 res = gst_pad_query_default (pad, parent, query);
1578 gst_ps_demux_reset_psm (GstPsDemux * demux)
1582 #define FILL_TYPE(start, stop, type) \
1583 for (i=start; i <= stop; i++) \
1584 demux->psm[i] = type;
1586 /* Initialize all fields to -1 first */
1587 FILL_TYPE (0x00, GST_PS_DEMUX_MAX_PSM - 1, -1);
1589 FILL_TYPE (0x20, 0x3f, ST_PS_DVD_SUBPICTURE);
1591 FILL_TYPE (0x80, 0x87, ST_PS_AUDIO_AC3);
1592 FILL_TYPE (0x88, 0x9f, ST_PS_AUDIO_DTS);
1593 FILL_TYPE (0xa0, 0xaf, ST_PS_AUDIO_LPCM);
1595 FILL_TYPE (0xc0, 0xdf, ST_AUDIO_MPEG1);
1596 FILL_TYPE (0xe0, 0xef, ST_GST_VIDEO_MPEG1_OR_2);
1603 * pack_start_code 32 bslbf -+
1605 * system_clock_reference_base [32..30] 3 bslbf |
1606 * marker_bit 1 bslbf |
1607 * system_clock_reference_base [29..15] 15 bslbf |
1608 * marker_bit 1 bslbf |
1609 * system_clock_reference_base [14..0] 15 bslbf |
1610 * marker_bit 1 bslbf | 112 bits
1611 * system_clock_reference_extension 9 ubslbf |
1612 * marker_bit 1 bslbf |
1613 * program_mux_rate 22 ubslbf |
1614 * marker_bit 1 bslbf |
1615 * marker_bit 1 bslbf |
1616 * reserved 5 bslbf |
1617 * pack_stuffing_length 3 ubslbf -+
1619 * for (i = 0; i < pack_stuffing_length; i++) {
1620 * stuffing_byte '1111 1111' 8 bslbf
1623 * 112 bits = 14 bytes, as max value for pack_stuffing_length is 7, then
1624 * in total it's needed 14 + 7 = 21 bytes.
1626 #define PACK_START_SIZE 21
1628 static GstFlowReturn
1629 gst_ps_demux_parse_pack_start (GstPsDemux * demux)
1634 guint64 scr, scr_adjusted, new_rate;
1637 guint avail = gst_adapter_available (demux->adapter);
1639 GST_LOG ("parsing pack start");
1641 if (G_UNLIKELY (avail < PACK_START_SIZE))
1642 goto need_more_data;
1644 data = gst_adapter_map (demux->adapter, PACK_START_SIZE);
1646 /* skip start code */
1649 scr1 = GST_READ_UINT32_BE (data);
1650 scr2 = GST_READ_UINT32_BE (data + 4);
1652 /* fixed length to begin with, start code and two scr values */
1655 /* start parsing the stream */
1656 if ((*data & 0xc0) == 0x40) {
1659 guint8 stuffing_bytes;
1661 GST_LOG ("Found MPEG2 stream");
1662 demux->is_mpeg2_pack = TRUE;
1664 /* mpeg2 has more data */
1667 /* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
1670 if (G_UNLIKELY ((scr1 & 0xc4000400) != 0x44000400))
1673 scr = ((guint64) scr1 & 0x38000000) << 3;
1674 scr |= ((guint64) scr1 & 0x03fff800) << 4;
1675 scr |= ((guint64) scr1 & 0x000003ff) << 5;
1676 scr |= ((guint64) scr2 & 0xf8000000) >> 27;
1678 /* marker:1==1 ! scr_ext:9 ! marker:1==1 */
1679 if (G_UNLIKELY ((scr2 & 0x04010000) != 0x04010000))
1682 scr_ext = (scr2 & 0x03fe0000) >> 17;
1683 /* We keep the offset of this scr */
1684 demux->cur_scr_offset = demux->adapter_offset + 12;
1686 GST_LOG_OBJECT (demux, "SCR: 0x%08" G_GINT64_MODIFIER "x SCRE: 0x%08x",
1690 scr = (scr * 300 + scr_ext % 300) / 300;
1692 /* SCR has been converted into units of 90Khz ticks to make it comparable
1693 to DTS/PTS, that also implies 1 tick rounding error */
1695 /* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
1696 next32 = GST_READ_UINT32_BE (data);
1697 if (G_UNLIKELY ((next32 & 0x00000300) != 0x00000300))
1700 new_rate = (next32 & 0xfffffc00) >> 10;
1702 stuffing_bytes = (next32 & 0x07);
1703 GST_LOG_OBJECT (demux, "stuffing bytes: %d", stuffing_bytes);
1706 length += stuffing_bytes;
1707 while (stuffing_bytes--) {
1708 if (*data++ != 0xff)
1712 GST_DEBUG ("Found MPEG1 stream");
1713 demux->is_mpeg2_pack = FALSE;
1716 if (G_UNLIKELY ((scr1 & 0xf1000100) != 0x21000100))
1719 if (G_UNLIKELY ((scr2 & 0x01800001) != 0x01800001))
1722 /* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
1723 scr = ((guint64) scr1 & 0x0e000000) << 5;
1724 scr |= ((guint64) scr1 & 0x00fffe00) << 6;
1725 scr |= ((guint64) scr1 & 0x000000ff) << 7;
1726 scr |= ((guint64) scr2 & 0xfe000000) >> 25;
1728 /* We keep the offset of this scr */
1729 demux->cur_scr_offset = demux->adapter_offset + 8;
1731 /* marker:1==1 ! mux_rate:22 ! marker:1==1 */
1732 new_rate = (scr2 & 0x007ffffe) >> 1;
1736 new_rate *= MPEG_MUX_RATE_MULT;
1738 /* scr adjusted is the new scr found + the colected adjustment */
1739 scr_adjusted = scr + demux->scr_adjust;
1741 GST_LOG_OBJECT (demux,
1742 "SCR: %" G_GINT64_FORMAT " (%" G_GINT64_FORMAT "), mux_rate %"
1743 G_GINT64_FORMAT ", GStreamer Time:%" GST_TIME_FORMAT,
1744 scr, scr_adjusted, new_rate,
1745 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME ((guint64) scr)));
1747 /* keep the first src in order to calculate delta time */
1748 if (G_UNLIKELY (demux->first_scr == G_MAXUINT64)) {
1751 demux->first_scr = scr;
1752 demux->first_scr_offset = demux->cur_scr_offset;
1753 demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
1754 GST_DEBUG_OBJECT (demux, "determined base_time %" GST_TIME_FORMAT,
1755 GST_TIME_ARGS (demux->base_time));
1756 /* at begin consider the new_rate as the scr rate, bytes/clock ticks */
1757 scr_rate_n = new_rate;
1758 scr_rate_d = CLOCK_FREQ;
1759 /* our SCR timeline might have offset wrt upstream timeline */
1760 if (demux->sink_segment.format == GST_FORMAT_TIME) {
1761 if (demux->sink_segment.start > demux->base_time)
1762 diff = -(demux->sink_segment.start - demux->base_time);
1764 diff = demux->base_time - demux->sink_segment.start;
1765 if (diff > GST_SECOND) {
1766 GST_DEBUG_OBJECT (demux, "diff of %" GST_TIME_FORMAT
1767 " wrt upstream start %" GST_TIME_FORMAT "; adjusting base",
1768 GST_TIME_ARGS (diff), GST_TIME_ARGS (demux->sink_segment.start));
1769 demux->base_time += diff;
1772 } else if (G_LIKELY (demux->first_scr_offset != demux->cur_scr_offset)) {
1773 /* estimate byte rate related to the SCR */
1774 scr_rate_n = demux->cur_scr_offset - demux->first_scr_offset;
1775 scr_rate_d = scr_adjusted - demux->first_scr;
1777 scr_rate_n = demux->scr_rate_n;
1778 scr_rate_d = demux->scr_rate_d;
1781 GST_LOG_OBJECT (demux, "%s mode scr: %" G_GUINT64_FORMAT " at %"
1782 G_GUINT64_FORMAT ", first scr: %" G_GUINT64_FORMAT
1783 " at %" G_GUINT64_FORMAT ", scr rate: %" G_GUINT64_FORMAT
1784 "/%" G_GUINT64_FORMAT "(%f)",
1785 ((demux->sink_segment.rate >= 0.0) ? "forward" : "backward"),
1786 scr, demux->cur_scr_offset,
1787 demux->first_scr, demux->first_scr_offset,
1788 scr_rate_n, scr_rate_d, (float) scr_rate_n / scr_rate_d);
1790 /* adjustment of the SCR */
1791 if (G_LIKELY (demux->current_scr != G_MAXUINT64)) {
1793 guint64 old_scr, old_mux_rate, bss, adjust = 0;
1795 /* keep SCR of the previous packet */
1796 old_scr = demux->current_scr;
1797 old_mux_rate = demux->mux_rate;
1799 /* Bytes since SCR is the amount we placed in the adapter since then
1800 * (demux->bytes_since_scr) minus the amount remaining in the adapter,
1801 * clamped to >= 0 */
1802 bss = MAX (0, (gint) (demux->bytes_since_scr - avail));
1804 /* estimate the new SCR using the previous one according the notes
1805 on point 2.5.2.2 of the ISO/IEC 13818-1 document */
1806 if (old_mux_rate != 0)
1807 adjust = (bss * CLOCK_FREQ) / old_mux_rate;
1809 if (demux->sink_segment.rate >= 0.0)
1810 demux->next_scr = old_scr + adjust;
1812 demux->next_scr = old_scr - adjust;
1814 GST_LOG_OBJECT (demux,
1815 "bss: %" G_GUINT64_FORMAT ", next_scr: %" G_GUINT64_FORMAT
1816 ", old_scr: %" G_GUINT64_FORMAT ", scr: %" G_GUINT64_FORMAT,
1817 bss, demux->next_scr, old_scr, scr_adjusted);
1819 /* calculate the absolute deference between the last scr and
1821 if (G_UNLIKELY (old_scr > scr_adjusted))
1822 diff = old_scr - scr_adjusted;
1824 diff = scr_adjusted - old_scr;
1826 /* if the difference is more than 1 second we need to reconfigure
1828 if (G_UNLIKELY (diff > CLOCK_FREQ)) {
1829 demux->scr_adjust = demux->next_scr - scr;
1830 GST_LOG_OBJECT (demux, "discont found, diff: %" G_GINT64_FORMAT
1831 ", adjust %" G_GINT64_FORMAT, diff, demux->scr_adjust);
1832 scr_adjusted = demux->next_scr;
1833 /* don't update rate estimation on disconts */
1834 scr_rate_n = demux->scr_rate_n;
1835 scr_rate_d = demux->scr_rate_d;
1837 demux->next_scr = scr_adjusted;
1841 /* update the current_scr and rate members */
1842 demux->mux_rate = new_rate;
1843 demux->current_scr = scr_adjusted;
1844 demux->scr_rate_n = scr_rate_n;
1845 demux->scr_rate_d = scr_rate_d;
1847 /* Reset the bytes_since_scr value to count the data remaining in the
1849 demux->bytes_since_scr = avail;
1851 gst_adapter_unmap (demux->adapter);
1852 gst_adapter_flush (demux->adapter, length);
1853 ADAPTER_OFFSET_FLUSH (length);
1858 GST_DEBUG_OBJECT (demux, "lost sync");
1859 gst_adapter_unmap (demux->adapter);
1860 return GST_FLOW_LOST_SYNC;
1864 GST_DEBUG_OBJECT (demux, "need more data");
1865 return GST_FLOW_NEED_MORE_DATA;
1870 * system_header () {
1871 * system_header_start_code 32 bslbf -+
1872 * header_length 16 uimsbf |
1873 * marker_bit 1 bslbf |
1874 * rate_bound 22 uimsbf |
1875 * marker_bit 1 bslbf |
1876 * audio_bound 6 uimsbf |
1877 * fixed_flag 1 bslbf |
1878 * CSPS_flag 1 bslbf | 96 bits
1879 * system_audio_lock_flag 1 bslbf |
1880 * system_video_lock_flag 1 bslbf |
1881 * marker_bit 1 bslbf |
1882 * video_bound 5 uimsbf |
1883 * packet_rate_restriction_flag 1 bslbf |
1884 * reserved_bits 7 bslbf -+
1885 * while (nextbits () = = '1') {
1886 * stream_id 8 uimsbf -+
1887 * '11' 2 bslbf | 24 bits
1888 * P-STD_buffer_bound_scale 1 bslbf |
1889 * P-STD_buffer_size_bound 13 uimsbf -+
1892 * 96 bits = 12 bytes, 24 bits = 3 bytes.
1895 static GstFlowReturn
1896 gst_ps_demux_parse_sys_head (GstPsDemux * demux)
1900 #ifndef GST_DISABLE_GST_DEBUG
1904 if (gst_adapter_available (demux->adapter) < 6)
1905 goto need_more_data;
1907 /* start code + length */
1908 data = gst_adapter_map (demux->adapter, 6);
1910 /* skip start code */
1913 length = GST_READ_UINT16_BE (data);
1914 GST_DEBUG_OBJECT (demux, "length %d", length);
1918 gst_adapter_unmap (demux->adapter);
1919 if (gst_adapter_available (demux->adapter) < length)
1920 goto need_more_data;
1922 data = gst_adapter_map (demux->adapter, length);
1924 /* skip start code and length */
1927 /* marker:1==1 ! rate_bound:22 | marker:1==1 */
1928 if ((*data & 0x80) != 0x80)
1929 goto marker_expected;
1934 if ((data[2] & 0x01) != 0x01)
1935 goto marker_expected;
1937 rate_bound = ((guint32) data[0] & 0x7f) << 15;
1938 rate_bound |= ((guint32) data[1]) << 7;
1939 rate_bound |= ((guint32) data[2] & 0xfe) >> 1;
1940 rate_bound *= MPEG_MUX_RATE_MULT;
1942 GST_DEBUG_OBJECT (demux, "rate bound %u", rate_bound);
1947 /* audio_bound:6==1 ! fixed:1 | constrained:1 */
1949 #ifndef GST_DISABLE_GST_DEBUG
1953 /* max number of simultaneous audio streams active */
1954 audio_bound = (data[0] & 0xfc) >> 2;
1955 /* fixed or variable bitrate */
1956 fixed = (data[0] & 0x02) == 0x02;
1957 /* meeting constraints */
1958 csps = (data[0] & 0x01) == 0x01;
1960 GST_DEBUG_OBJECT (demux, "audio_bound %d, fixed %d, constrained %d",
1961 audio_bound, fixed, csps);
1966 /* audio_lock:1 | video_lock:1 | marker:1==1 | video_bound:5 */
1968 #ifndef GST_DISABLE_GST_DEBUG
1969 gboolean audio_lock;
1970 gboolean video_lock;
1973 audio_lock = (data[0] & 0x80) == 0x80;
1974 video_lock = (data[0] & 0x40) == 0x40;
1977 if ((data[0] & 0x20) != 0x20)
1978 goto marker_expected;
1980 #ifndef GST_DISABLE_GST_DEBUG
1981 /* max number of simultaneous video streams active */
1982 video_bound = (data[0] & 0x1f);
1984 GST_DEBUG_OBJECT (demux, "audio_lock %d, video_lock %d, video_bound %d",
1985 audio_lock, video_lock, video_bound);
1990 /* packet_rate_restriction:1 | reserved:7==0x7F */
1992 #ifndef GST_DISABLE_GST_DEBUG
1993 gboolean packet_rate_restriction;
1995 if ((data[0] & 0x7f) != 0x7f)
1996 goto marker_expected;
1997 #ifndef GST_DISABLE_GST_DEBUG
1998 /* only valid if csps is set */
2000 packet_rate_restriction = (data[0] & 0x80) == 0x80;
2002 GST_DEBUG_OBJECT (demux, "packet_rate_restriction %d",
2003 packet_rate_restriction);
2010 gint stream_count = (length - 12) / 3;
2013 GST_DEBUG_OBJECT (demux, "number of streams: %d ", stream_count);
2015 for (i = 0; i < stream_count; i++) {
2017 #ifndef GST_DISABLE_GST_DEBUG
2018 gboolean STD_buffer_bound_scale;
2019 guint16 STD_buffer_size_bound;
2020 guint32 buf_byte_size_bound;
2022 stream_id = *data++;
2023 if (!(stream_id & 0x80))
2026 /* check marker bits */
2027 if ((*data & 0xC0) != 0xC0)
2028 goto no_placeholder_bits;
2029 #ifndef GST_DISABLE_GST_DEBUG
2030 STD_buffer_bound_scale = *data & 0x20;
2031 STD_buffer_size_bound = ((guint16) (*data++ & 0x1F)) << 8;
2032 STD_buffer_size_bound |= *data++;
2034 if (STD_buffer_bound_scale == 0) {
2035 buf_byte_size_bound = STD_buffer_size_bound * 128;
2037 buf_byte_size_bound = STD_buffer_size_bound * 1024;
2040 GST_DEBUG_OBJECT (demux, "STD_buffer_bound_scale %d",
2041 STD_buffer_bound_scale);
2042 GST_DEBUG_OBJECT (demux, "STD_buffer_size_bound %d or %d bytes",
2043 STD_buffer_size_bound, buf_byte_size_bound);
2048 gst_adapter_unmap (demux->adapter);
2049 gst_adapter_flush (demux->adapter, length);
2050 ADAPTER_OFFSET_FLUSH (length);
2056 GST_DEBUG_OBJECT (demux, "expecting marker");
2057 gst_adapter_unmap (demux->adapter);
2058 return GST_FLOW_LOST_SYNC;
2060 no_placeholder_bits:
2062 GST_DEBUG_OBJECT (demux, "expecting placeholder bit values"
2063 " '11' after stream id");
2064 gst_adapter_unmap (demux->adapter);
2065 return GST_FLOW_LOST_SYNC;
2069 GST_DEBUG_OBJECT (demux, "error in system header length");
2070 gst_adapter_unmap (demux->adapter);
2071 return GST_FLOW_LOST_SYNC;
2075 GST_DEBUG_OBJECT (demux, "need more data");
2076 gst_adapter_unmap (demux->adapter);
2077 return GST_FLOW_NEED_MORE_DATA;
2081 static GstFlowReturn
2082 gst_ps_demux_parse_psm (GstPsDemux * demux)
2084 guint16 psm_length, info_length = 0, es_map_length = 0;
2085 guint8 psm_version = 0;
2087 #ifndef GST_DISABLE_GST_DEBUG
2088 gboolean applicable;
2091 /* Need at least 6 bytes for start code + length */
2092 if (gst_adapter_available (demux->adapter) < 6)
2093 goto need_more_data;
2098 /* start code + length */
2099 data = gst_adapter_map (demux->adapter, 6);
2100 /* skip start code */
2102 psm_length = GST_READ_UINT16_BE (data);
2103 GST_DEBUG_OBJECT (demux, "PSM length %u", psm_length);
2105 if (G_UNLIKELY (psm_length > 0x3FA))
2107 psm_length += 6; /* Add start code + size to length */
2109 gst_adapter_unmap (demux->adapter);
2111 if (gst_adapter_available (demux->adapter) < psm_length)
2112 goto need_more_data;
2114 data = gst_adapter_map (demux->adapter, psm_length);
2116 gst_byte_reader_init (&br, data, psm_length);
2119 /* skip start code and length */
2120 if (!gst_byte_reader_skip (&br, 6))
2123 /* Read PSM applicable bit together with version */
2124 if (!gst_byte_reader_get_uint8 (&br, &psm_version))
2126 #ifndef GST_DISABLE_GST_DEBUG
2127 applicable = (psm_version & 0x80) >> 7;
2129 psm_version &= 0x1F;
2130 GST_DEBUG_OBJECT (demux, "PSM version %u (applicable now %u)", psm_version,
2133 /* Jump over the next byte (marker bit) */
2134 if (!gst_byte_reader_skip (&br, 1))
2137 /* Read PS info length */
2138 if (!gst_byte_reader_get_uint16_be (&br, &info_length))
2140 GST_DEBUG_OBJECT (demux, "PS info length %u bytes", info_length);
2141 /* Skip the PS info, we don't use it */
2142 if (!gst_byte_reader_skip (&br, info_length))
2145 /* Read ES map length */
2146 if (!gst_byte_reader_get_uint16_be (&br, &es_map_length))
2148 GST_DEBUG_OBJECT (demux, "ES map length %u bytes", es_map_length);
2150 /* Now read the ES map */
2152 GstByteReader es_map_br;
2153 if (!gst_byte_reader_get_sub_reader (&br, &es_map_br, es_map_length))
2156 while (gst_byte_reader_get_remaining (&es_map_br) >= 4) {
2157 guint8 stream_type = 0, stream_id = 0;
2158 guint16 stream_info_length = 0;
2160 if (!gst_byte_reader_get_uint8 (&es_map_br, &stream_type) ||
2161 !gst_byte_reader_get_uint8 (&es_map_br, &stream_id) ||
2162 !gst_byte_reader_get_uint16_be (&es_map_br, &stream_info_length))
2165 GST_DEBUG_OBJECT (demux,
2166 "Stream type %02X with id %02X and %u bytes info", stream_type,
2167 stream_id, stream_info_length);
2169 if (G_LIKELY (stream_id != 0xbd))
2170 demux->psm[stream_id] = stream_type;
2172 /* Ignore stream type for private_stream_1 and discover it looking at
2174 * Fixes demuxing some clips with lpcm that was wrongly declared as
2176 GST_DEBUG_OBJECT (demux, "stream type for private_stream_1 ignored");
2179 /* FIXME: We could use the descriptors instead of skipping them */
2180 if (!gst_byte_reader_skip (&es_map_br, stream_info_length))
2184 /* We ignore the 4-byte CRC at the end */
2186 gst_adapter_unmap (demux->adapter);
2187 gst_adapter_flush (demux->adapter, psm_length);
2188 ADAPTER_OFFSET_FLUSH (psm_length);
2192 GST_DEBUG_OBJECT (demux, "Failed to parse PSM. Skipping");
2193 gst_adapter_unmap (demux->adapter);
2194 gst_adapter_flush (demux->adapter, psm_length);
2195 ADAPTER_OFFSET_FLUSH (psm_length);
2196 return GST_FLOW_LOST_SYNC;
2199 GST_DEBUG_OBJECT (demux, "error in PSM length");
2200 gst_adapter_unmap (demux->adapter);
2201 return GST_FLOW_LOST_SYNC;
2205 GST_DEBUG_OBJECT (demux, "need more data");
2206 return GST_FLOW_NEED_MORE_DATA;
2211 gst_ps_demux_resync_cb (GstPESFilter * filter, GstPsDemux * demux)
2215 static GstFlowReturn
2216 gst_ps_demux_data_cb (GstPESFilter * filter, gboolean first,
2217 GstBuffer * buffer, GstPsDemux * demux)
2220 GstFlowReturn ret = GST_FLOW_OK;
2227 gst_buffer_map (buffer, &map, GST_MAP_READ);
2229 start_code = filter->start_code;
2232 /* find the stream type */
2233 stream_type = demux->psm[id];
2234 if (stream_type == -1) {
2235 /* no stream type, if PS1, get the new id */
2236 if (start_code == ID_PRIVATE_STREAM_1 && datalen >= 2) {
2237 /* VDR writes A52 streams without any header bytes
2238 * (see ftp://ftp.mplayerhq.hu/MPlayer/samples/MPEG-VOB/vdr-AC3) */
2240 guint hdr = GST_READ_UINT32_BE (map.data);
2241 if (G_UNLIKELY ((hdr & 0xffff0000) == AC3_SYNC_WORD)) {
2243 stream_type = demux->psm[id] = ST_GST_AUDIO_RAWA52;
2244 GST_DEBUG_OBJECT (demux, "Found VDR raw A52 stream");
2248 if (G_LIKELY (stream_type == -1)) {
2249 /* new id is in the first byte */
2250 id = map.data[offset++];
2253 stream_type = demux->psm[id];
2254 /* Now, if it's a subpicture stream - no more, otherwise
2255 * take the first byte too, since it's the frame count in audio
2256 * streams and our backwards compat convention is to strip it off */
2257 if (stream_type != ST_PS_DVD_SUBPICTURE) {
2258 /* Number of audio frames in this packet */
2259 #ifndef GST_DISABLE_GST_DEBUG
2261 nframes = map.data[offset];
2262 GST_LOG_OBJECT (demux, "private type 0x%02x, %d frames", id,
2268 GST_LOG_OBJECT (demux, "private type 0x%02x, stream type %d",
2273 if (stream_type == -1)
2274 goto unknown_stream_type;
2276 if (filter->pts != -1) {
2277 demux->next_pts = filter->pts + demux->scr_adjust;
2278 GST_LOG_OBJECT (demux, "stream 0x%02x PTS = orig %" G_GUINT64_FORMAT
2279 " (%" G_GUINT64_FORMAT ")", id, filter->pts, demux->next_pts);
2281 demux->next_pts = G_MAXUINT64;
2282 if (filter->dts != -1) {
2283 demux->next_dts = filter->dts + demux->scr_adjust;
2284 GST_LOG_OBJECT (demux, "stream 0x%02x DTS = orig %" G_GUINT64_FORMAT
2285 " (%" G_GUINT64_FORMAT ")", id, filter->dts, demux->next_dts);
2287 demux->next_dts = demux->next_pts;
2290 demux->current_stream = gst_ps_demux_get_stream (demux, id, stream_type);
2293 if (G_UNLIKELY (demux->current_stream == NULL)) {
2294 GST_DEBUG_OBJECT (demux, "Dropping buffer for unknown stream id 0x%02x",
2299 /* After 2 seconds of bitstream emit no more pads */
2300 if (demux->need_no_more_pads
2301 && (demux->current_scr - demux->first_scr) > 2 * CLOCK_FREQ) {
2302 GST_DEBUG_OBJECT (demux, "no more pads, notifying");
2303 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
2304 demux->need_no_more_pads = FALSE;
2307 /* If the stream is not-linked, don't bother creating a sub-buffer
2308 * to send to it, unless we're processing a discont (which resets
2309 * the not-linked status and tries again */
2310 if (demux->current_stream->discont) {
2311 GST_DEBUG_OBJECT (demux, "stream is discont");
2312 demux->current_stream->notlinked = FALSE;
2315 if (demux->current_stream->notlinked == FALSE) {
2317 gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, datalen);
2318 ret = gst_ps_demux_send_data (demux, demux->current_stream, out_buf);
2319 if (ret == GST_FLOW_NOT_LINKED) {
2320 demux->current_stream->notlinked = TRUE;
2325 gst_buffer_unmap (buffer, &map);
2326 gst_buffer_unref (buffer);
2329 unknown_stream_type:
2331 GST_DEBUG_OBJECT (demux, "unknown stream type %02x", id);
2338 gst_ps_demux_resync (GstPsDemux * demux, gboolean save)
2345 avail = gst_adapter_available (demux->adapter);
2346 if (G_UNLIKELY (avail < 4))
2348 /* Common case, read 4 bytes an check it */
2349 data = gst_adapter_map (demux->adapter, 4);
2350 /* read currect code */
2351 code = GST_READ_UINT32_BE (data);
2352 /* The common case is that the sync code is at 0 bytes offset */
2353 if (G_LIKELY ((code & 0xffffff00) == 0x100L)) {
2354 GST_LOG_OBJECT (demux, "Found resync code %08x after 0 bytes", code);
2355 demux->last_sync_code = code;
2356 gst_adapter_unmap (demux->adapter);
2360 /* Otherwise, we are starting at byte 4 and we need to search
2361 the sync code in all available data in the adapter */
2363 if (offset >= avail)
2364 goto need_data; /* Not enough data to find sync */
2365 data = gst_adapter_map (demux->adapter, avail);
2367 code = (code << 8) | data[offset++];
2368 found = (code & 0xffffff00) == 0x100L;
2369 } while (offset < avail && !found);
2370 gst_adapter_unmap (demux->adapter);
2371 if (!save || demux->sink_segment.rate >= 0.0) {
2372 GST_LOG_OBJECT (demux, "flushing %d bytes", offset - 4);
2373 /* forward playback, we can discard and flush the skipped bytes */
2374 gst_adapter_flush (demux->adapter, offset - 4);
2375 ADAPTER_OFFSET_FLUSH (offset - 4);
2378 GST_LOG_OBJECT (demux, "reverse saving %d bytes", offset - 4);
2379 /* reverse playback, we keep the flushed bytes and we will append them to
2380 * the next buffer in the chain function, which is the previous buffer in
2382 gst_adapter_push (demux->rev_adapter,
2383 gst_adapter_take_buffer (demux->adapter, offset - 4));
2385 GST_LOG_OBJECT (demux, "reverse saving %d bytes", avail);
2386 /* nothing found, keep all bytes */
2387 gst_adapter_push (demux->rev_adapter,
2388 gst_adapter_take_buffer (demux->adapter, avail));
2393 GST_LOG_OBJECT (demux, "Found resync code %08x after %d bytes",
2395 demux->last_sync_code = code;
2397 GST_LOG_OBJECT (demux, "No resync after skipping %d", offset);
2403 GST_LOG_OBJECT (demux, "we need more data for resync %d", avail);
2408 static inline gboolean
2409 gst_ps_demux_is_pes_sync (guint32 sync)
2411 return ((sync & 0xfc) == 0xbc) ||
2412 ((sync & 0xe0) == 0xc0) || ((sync & 0xf0) == 0xe0);
2415 static inline gboolean
2416 gst_ps_demux_scan_ts (GstPsDemux * demux, const guint8 * data,
2417 SCAN_MODE mode, guint64 * rts, const guint8 * end)
2419 gboolean ret = FALSE;
2425 /* read the 4 bytes for the sync code */
2426 code = GST_READ_UINT32_BE (data);
2427 if (G_LIKELY (code != ID_PS_PACK_START_CODE))
2429 if (data + 12 > end)
2431 /* skip start code */
2433 scr1 = GST_READ_UINT32_BE (data);
2434 scr2 = GST_READ_UINT32_BE (data + 4);
2435 /* start parsing the stream */
2436 if ((*data & 0xc0) == 0x40) {
2437 /* MPEG-2 PACK header */
2440 guint8 stuffing_bytes;
2441 /* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
2443 if ((scr1 & 0xc4000400) != 0x44000400)
2445 scr = ((guint64) scr1 & 0x38000000) << 3;
2446 scr |= ((guint64) scr1 & 0x03fff800) << 4;
2447 scr |= ((guint64) scr1 & 0x000003ff) << 5;
2448 scr |= ((guint64) scr2 & 0xf8000000) >> 27;
2449 /* marker:1==1 ! scr_ext:9 ! marker:1==1 */
2450 if ((scr2 & 0x04010000) != 0x04010000)
2452 scr_ext = (scr2 & 0x03fe0000) >> 17;
2454 scr = (scr * 300 + scr_ext % 300) / 300;
2456 /* SCR has been converted into units of 90Khz ticks to make it comparable
2457 to DTS/PTS, that also implies 1 tick rounding error */
2462 /* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
2463 next32 = GST_READ_UINT32_BE (data);
2464 if ((next32 & 0x00000300) != 0x00000300)
2466 stuffing_bytes = (next32 & 0x07);
2468 if (data + stuffing_bytes > end)
2470 while (stuffing_bytes--) {
2471 if (*data++ != 0xff)
2475 /* MPEG-1 pack header */
2477 if ((scr1 & 0xf1000100) != 0x21000100)
2479 if ((scr2 & 0x01800001) != 0x01800001)
2481 /* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
2482 scr = ((guint64) scr1 & 0x0e000000) << 5;
2483 scr |= ((guint64) scr1 & 0x00fffe00) << 6;
2484 scr |= ((guint64) scr1 & 0x000000ff) << 7;
2485 scr |= ((guint64) scr2 & 0xfe000000) >> 25;
2489 if (mode == SCAN_SCR) {
2495 /* Possible optional System header here */
2499 code = GST_READ_UINT32_BE (data);
2500 len = GST_READ_UINT16_BE (data + 4);
2501 if (code == ID_PS_SYSTEM_HEADER_START_CODE) {
2502 /* Found a system header, skip it */
2503 /* Check for sufficient data - system header, plus enough
2504 * left over for the PES packet header */
2505 if (data + 6 + len + 6 > end)
2508 /* read the 4 bytes for the PES sync code */
2509 code = GST_READ_UINT32_BE (data);
2510 len = GST_READ_UINT16_BE (data + 4);
2513 /* Check we have enough data left for reading the PES packet */
2514 if (data + 6 + len > end)
2516 if (!gst_ps_demux_is_pes_sync (code))
2519 case ID_PS_PROGRAM_STREAM_MAP:
2520 case ID_PRIVATE_STREAM_2:
2523 case ID_PROGRAM_STREAM_DIRECTORY:
2524 case ID_DSMCC_STREAM:
2525 case ID_ITU_TREC_H222_TYPE_E_STREAM:
2526 case ID_PADDING_STREAM:
2532 /* skip sync code and size */
2535 /* stuffing bits, first two bits are '10' for mpeg2 pes so this code is
2543 /* STD buffer size, never for mpeg2 */
2544 if ((*data & 0xc0) == 0x40)
2546 /* PTS but no DTS, never for mpeg2 */
2547 if ((*data & 0xf0) == 0x20) {
2548 READ_TS (data, pts, beach);
2550 /* PTS and DTS, never for mpeg2 */
2551 else if ((*data & 0xf0) == 0x30) {
2552 READ_TS (data, pts, beach);
2553 READ_TS (data, dts, beach);
2554 } else if ((*data & 0xc0) == 0x80) {
2558 * 2: PES_scrambling_control
2560 * 1: data_alignment_indicator
2562 * 1: original_or_copy
2565 if ((flags & 0xc0) != 0x80)
2570 * 1: DSM_trick_mode_flag
2571 * 1: additional_copy_info_flag
2573 * 1: PES_extension_flag
2576 /* 8: PES_header_data_length */
2578 /* only DTS: this is invalid */
2579 if ((flags & 0xc0) == 0x40)
2582 if ((flags & 0x80)) {
2583 READ_TS (data, pts, beach);
2586 if ((flags & 0x40)) {
2587 READ_TS (data, dts, beach);
2591 if (mode == SCAN_DTS && dts != (guint64) - 1) {
2596 if (mode == SCAN_PTS && pts != (guint64) - 1) {
2604 static inline gboolean
2605 gst_ps_demux_scan_forward_ts (GstPsDemux * demux, guint64 * pos,
2606 SCAN_MODE mode, guint64 * rts, gint limit)
2608 GstFlowReturn ret = GST_FLOW_OK;
2610 guint64 offset = *pos;
2611 gboolean found = FALSE;
2613 guint scan_sz = (mode == SCAN_SCR ? SCAN_SCR_SZ : SCAN_PTS_SZ);
2614 guint cursor, to_read = BLOCK_SZ;
2618 /* Check we can get at least scan_sz bytes */
2619 if (offset + scan_sz > demux->sink_segment.stop)
2621 /* Don't go further than 'limit' bytes */
2622 if (limit && offset > *pos + limit)
2624 if (offset + to_read > demux->sink_segment.stop)
2625 to_read = demux->sink_segment.stop - offset;
2626 /* read some data */
2628 ret = gst_pad_pull_range (demux->sinkpad, offset, to_read, &buffer);
2629 if (G_UNLIKELY (ret != GST_FLOW_OK))
2631 gst_buffer_map (buffer, &map, GST_MAP_READ);
2632 /* may get a short buffer at the end of the file */
2633 if (G_UNLIKELY (map.size <= scan_sz)) {
2634 gst_buffer_unmap (buffer, &map);
2635 gst_buffer_unref (buffer);
2639 end_scan = map.size - scan_sz;
2640 /* scan the block */
2641 for (cursor = 0; !found && cursor <= end_scan; cursor++) {
2642 found = gst_ps_demux_scan_ts (demux, map.data + cursor, mode, &ts,
2643 map.data + map.size);
2646 /* done with the buffer, unref it */
2647 gst_buffer_unmap (buffer, &map);
2648 gst_buffer_unref (buffer);
2651 *pos = offset + cursor - 1;
2655 } while (!found && offset < demux->sink_segment.stop);
2659 static inline gboolean
2660 gst_ps_demux_scan_backward_ts (GstPsDemux * demux, guint64 * pos,
2661 SCAN_MODE mode, guint64 * rts, gint limit)
2663 GstFlowReturn ret = GST_FLOW_OK;
2665 guint64 offset = *pos;
2666 gboolean found = FALSE;
2668 guint scan_sz = (mode == SCAN_SCR ? SCAN_SCR_SZ : SCAN_PTS_SZ);
2669 guint cursor, to_read = BLOCK_SZ;
2674 /* Check we have at least scan_sz bytes available */
2675 if (offset < scan_sz - 1)
2677 /* Don't go backward past the start or 'limit' bytes */
2678 if (limit && offset + limit < *pos)
2680 if (offset > BLOCK_SZ)
2683 to_read = offset + 1;
2686 /* read some data */
2688 ret = gst_pad_pull_range (demux->sinkpad, offset, to_read, &buffer);
2689 if (G_UNLIKELY (ret != GST_FLOW_OK))
2691 gst_buffer_map (buffer, &map, GST_MAP_READ);
2692 /* may get a short buffer at the end of the file */
2693 if (G_UNLIKELY (map.size <= scan_sz)) {
2694 gst_buffer_unmap (buffer, &map);
2695 gst_buffer_unref (buffer);
2699 start_scan = map.size - scan_sz;
2700 data = map.data + start_scan;
2701 /* scan the block */
2702 for (cursor = (start_scan + 1); !found && cursor > 0; cursor--) {
2703 found = gst_ps_demux_scan_ts (demux, data--, mode, &ts,
2704 map.data + map.size);
2707 /* done with the buffer, unref it */
2708 gst_buffer_unmap (buffer, &map);
2709 gst_buffer_unref (buffer);
2712 *pos = offset + cursor;
2715 } while (!found && offset > 0);
2719 static inline gboolean
2720 gst_ps_sink_get_duration (GstPsDemux * demux)
2722 gboolean res = FALSE;
2724 GstFormat format = GST_FORMAT_BYTES;
2729 /* init the sink segment */
2730 gst_segment_init (&demux->sink_segment, format);
2731 /* get peer to figure out length */
2732 if ((peer = gst_pad_get_peer (demux->sinkpad)) == NULL)
2734 res = gst_pad_query_duration (peer, format, &length);
2735 gst_object_unref (peer);
2736 if (!res || length <= 0)
2738 GST_DEBUG_OBJECT (demux, "file length %" G_GINT64_FORMAT, length);
2739 /* update the sink segment */
2740 demux->sink_segment.stop = length;
2741 gst_segment_set_duration (&demux->sink_segment, format, length);
2742 gst_segment_set_position (&demux->sink_segment, format, 0);
2743 /* Scan for notorious SCR and PTS to calculate the duration */
2744 /* scan for first SCR in the stream */
2745 offset = demux->sink_segment.start;
2746 gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR,
2747 &demux->first_scr, DURATION_SCAN_LIMIT);
2748 GST_DEBUG_OBJECT (demux,
2749 "First SCR: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2750 " in packet starting at %" G_GUINT64_FORMAT, demux->first_scr,
2751 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_scr)), offset);
2752 demux->first_scr_offset = offset;
2753 /* scan for last SCR in the stream */
2754 offset = demux->sink_segment.stop;
2755 gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_SCR,
2756 &demux->last_scr, DURATION_SCAN_LIMIT);
2757 GST_DEBUG_OBJECT (demux,
2758 "Last SCR: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2759 " in packet starting at %" G_GUINT64_FORMAT, demux->last_scr,
2760 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->last_scr)), offset);
2761 demux->last_scr_offset = offset;
2762 /* scan for first PTS in the stream */
2763 offset = demux->sink_segment.start;
2764 gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_PTS,
2765 &demux->first_pts, DURATION_SCAN_LIMIT);
2766 GST_DEBUG_OBJECT (demux,
2767 "First PTS: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2768 " in packet starting at %" G_GUINT64_FORMAT, demux->first_pts,
2769 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_pts)), offset);
2770 if (demux->first_pts != G_MAXUINT64) {
2771 /* scan for last PTS in the stream */
2772 offset = demux->sink_segment.stop;
2773 gst_ps_demux_scan_backward_ts (demux, &offset, SCAN_PTS,
2774 &demux->last_pts, DURATION_SCAN_LIMIT);
2775 GST_DEBUG_OBJECT (demux,
2776 "Last PTS: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2777 " in packet starting at %" G_GUINT64_FORMAT, demux->last_pts,
2778 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->last_pts)), offset);
2780 /* Detect wrong SCR values */
2781 if (demux->first_scr > demux->last_scr) {
2782 GST_DEBUG_OBJECT (demux, "Wrong SCR values detected, searching for "
2783 "a better first SCR value");
2784 offset = demux->first_scr_offset;
2785 for (i = 0; i < 10; i++) {
2787 gst_ps_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &scr, 0);
2788 if (scr < demux->last_scr) {
2789 demux->first_scr = scr;
2790 demux->first_scr_offset = offset;
2791 /* Start demuxing from the right place */
2792 demux->sink_segment.position = offset;
2793 GST_DEBUG_OBJECT (demux, "Replaced First SCR: %" G_GINT64_FORMAT
2794 " %" GST_TIME_FORMAT " in packet starting at %"
2795 G_GUINT64_FORMAT, demux->first_scr,
2796 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_scr)), offset);
2801 /* Set the base_time and avg rate */
2802 demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
2803 demux->scr_rate_n = demux->last_scr_offset - demux->first_scr_offset;
2804 demux->scr_rate_d = demux->last_scr - demux->first_scr;
2805 if (G_LIKELY (demux->first_pts != G_MAXUINT64 &&
2806 demux->last_pts != G_MAXUINT64)) {
2807 /* update the src segment */
2808 demux->src_segment.format = GST_FORMAT_TIME;
2809 demux->src_segment.start =
2810 MPEGTIME_TO_GSTTIME (demux->first_pts) - demux->base_time;
2811 demux->src_segment.stop = -1;
2812 gst_segment_set_duration (&demux->src_segment, GST_FORMAT_TIME,
2813 MPEGTIME_TO_GSTTIME (demux->last_pts - demux->first_pts));
2814 gst_segment_set_position (&demux->src_segment, GST_FORMAT_TIME,
2815 demux->src_segment.start);
2817 GST_INFO_OBJECT (demux, "sink segment configured %" GST_SEGMENT_FORMAT,
2818 &demux->sink_segment);
2819 GST_INFO_OBJECT (demux, "src segment configured %" GST_SEGMENT_FORMAT,
2820 &demux->src_segment);
2826 static inline GstFlowReturn
2827 gst_ps_demux_pull_block (GstPad * pad, GstPsDemux * demux,
2828 guint64 offset, guint size)
2831 GstBuffer *buffer = NULL;
2832 ret = gst_pad_pull_range (pad, offset, size, &buffer);
2833 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2834 GST_DEBUG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
2835 " size %u failed", offset, size);
2838 GST_LOG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
2839 " size %u done", offset, size);
2840 if (demux->sink_segment.rate < 0) {
2841 GST_LOG_OBJECT (demux, "setting discont flag on backward rate");
2842 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2844 ret = gst_ps_demux_chain (pad, GST_OBJECT (demux), buffer);
2850 gst_ps_demux_loop (GstPad * pad)
2853 GstFlowReturn ret = GST_FLOW_OK;
2855 demux = GST_PS_DEMUX (gst_pad_get_parent (pad));
2856 if (G_UNLIKELY (demux->flushing)) {
2857 ret = GST_FLOW_FLUSHING;
2861 if (G_UNLIKELY (demux->sink_segment.format == GST_FORMAT_UNDEFINED))
2862 gst_ps_sink_get_duration (demux);
2863 offset = demux->sink_segment.position;
2864 if (demux->sink_segment.rate >= 0) {
2865 guint size = BLOCK_SZ;
2866 if (G_LIKELY (demux->sink_segment.stop != (guint64) - 1)) {
2867 size = MIN (size, demux->sink_segment.stop - offset);
2870 ret = gst_ps_demux_pull_block (pad, demux, offset, size);
2871 /* pause if something went wrong */
2872 if (G_UNLIKELY (ret != GST_FLOW_OK))
2874 /* update our position */
2876 gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
2877 /* check EOS condition */
2878 if ((demux->sink_segment.position >= demux->sink_segment.stop) ||
2879 (demux->src_segment.stop != (guint64) - 1 &&
2880 demux->src_segment.position >= demux->src_segment.stop)) {
2881 GST_DEBUG_OBJECT (demux,
2882 "forward mode using segment reached end of " "segment pos %"
2883 GST_TIME_FORMAT " stop %" GST_TIME_FORMAT " pos in bytes %"
2884 G_GUINT64_FORMAT " stop in bytes %" G_GUINT64_FORMAT,
2885 GST_TIME_ARGS (demux->src_segment.position),
2886 GST_TIME_ARGS (demux->src_segment.stop),
2887 demux->sink_segment.position, demux->sink_segment.stop);
2891 } else { /* Reverse playback */
2892 guint64 size = MIN (offset, BLOCK_SZ);
2894 ret = gst_ps_demux_pull_block (pad, demux, offset - size, size);
2895 /* pause if something went wrong */
2896 if (G_UNLIKELY (ret != GST_FLOW_OK))
2898 /* update our position */
2900 gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
2901 /* check EOS condition */
2902 if (demux->sink_segment.position <= demux->sink_segment.start ||
2903 demux->src_segment.position <= demux->src_segment.start) {
2904 GST_DEBUG_OBJECT (demux,
2905 "reverse mode using segment reached end of " "segment pos %"
2906 GST_TIME_FORMAT " stop %" GST_TIME_FORMAT " pos in bytes %"
2907 G_GUINT64_FORMAT " stop in bytes %" G_GUINT64_FORMAT,
2908 GST_TIME_ARGS (demux->src_segment.position),
2909 GST_TIME_ARGS (demux->src_segment.start),
2910 demux->sink_segment.position, demux->sink_segment.start);
2916 gst_object_unref (demux);
2920 const gchar *reason = gst_flow_get_name (ret);
2921 GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
2922 gst_pad_pause_task (pad);
2923 if (ret == GST_FLOW_EOS) {
2924 /* perform EOS logic */
2925 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
2926 if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
2928 /* for segment playback we need to post when (in stream time)
2929 * we stopped, this is either stop (when set) or the duration. */
2930 if ((stop = demux->src_segment.stop) == -1)
2931 stop = demux->src_segment.duration;
2932 if (demux->sink_segment.rate >= 0) {
2933 GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
2934 gst_element_post_message (GST_ELEMENT_CAST (demux),
2935 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
2936 GST_FORMAT_TIME, stop));
2937 gst_ps_demux_send_event (demux,
2938 gst_event_new_segment_done (GST_FORMAT_TIME, stop));
2939 } else { /* Reverse playback */
2940 GST_LOG_OBJECT (demux,
2941 "Sending segment done, at beginning of " "segment");
2942 gst_element_post_message (GST_ELEMENT_CAST (demux),
2943 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
2944 GST_FORMAT_TIME, demux->src_segment.start));
2945 gst_ps_demux_send_event (demux,
2946 gst_event_new_segment_done (GST_FORMAT_TIME,
2947 demux->src_segment.start));
2950 /* normal playback, send EOS to all linked pads */
2951 gst_element_no_more_pads (GST_ELEMENT (demux));
2952 GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
2953 if (!gst_ps_demux_send_event (demux, gst_event_new_eos ())
2954 && !have_open_streams (demux)) {
2955 GST_WARNING_OBJECT (demux, "EOS and no streams open");
2956 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
2957 ("Internal data stream error."), ("No valid streams detected"));
2960 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2961 GST_ELEMENT_FLOW_ERROR (demux, ret);
2962 gst_ps_demux_send_event (demux, gst_event_new_eos ());
2965 gst_object_unref (demux);
2970 /* If we can pull that's prefered */
2972 gst_ps_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
2974 gboolean res = FALSE;
2975 GstQuery *query = gst_query_new_scheduling ();
2976 if (gst_pad_peer_query (sinkpad, query)) {
2977 if (gst_query_has_scheduling_mode_with_flags (query,
2978 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE)) {
2979 res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2981 res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2984 res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2987 gst_query_unref (query);
2991 /* This function gets called when we activate ourselves in push mode. */
2993 gst_ps_demux_sink_activate_push (GstPad * sinkpad, GstObject * parent,
2996 GstPsDemux *demux = GST_PS_DEMUX (parent);
2997 demux->random_access = FALSE;
3001 /* this function gets called when we activate ourselves in pull mode.
3002 * We can perform random access to the resource and we start a task
3003 * to start reading */
3005 gst_ps_demux_sink_activate_pull (GstPad * sinkpad, GstObject * parent,
3008 GstPsDemux *demux = GST_PS_DEMUX (parent);
3010 GST_DEBUG ("pull mode activated");
3011 demux->random_access = TRUE;
3012 return gst_pad_start_task (sinkpad,
3013 (GstTaskFunction) gst_ps_demux_loop, sinkpad, NULL);
3015 demux->random_access = FALSE;
3016 return gst_pad_stop_task (sinkpad);
3021 gst_ps_demux_sink_activate_mode (GstPad * pad, GstObject * parent,
3022 GstPadMode mode, gboolean active)
3024 if (mode == GST_PAD_MODE_PUSH) {
3025 return gst_ps_demux_sink_activate_push (pad, parent, active);
3026 } else if (mode == GST_PAD_MODE_PULL) {
3027 return gst_ps_demux_sink_activate_pull (pad, parent, active);
3032 /* EOS and NOT_LINKED need to be combined. This means that we return:
3034 * GST_FLOW_NOT_LINKED: when all pads NOT_LINKED.
3035 * GST_FLOW_EOS: when all pads EOS or NOT_LINKED.
3037 static GstFlowReturn
3038 gst_ps_demux_combine_flows (GstPsDemux * demux, GstFlowReturn ret)
3040 GST_LOG_OBJECT (demux, "flow return: %s", gst_flow_get_name (ret));
3041 ret = gst_flow_combiner_update_flow (demux->flowcombiner, ret);
3042 if (G_UNLIKELY (demux->need_no_more_pads && ret == GST_FLOW_NOT_LINKED))
3044 GST_LOG_OBJECT (demux, "combined flow return: %s", gst_flow_get_name (ret));
3048 static GstFlowReturn
3049 gst_ps_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3051 GstPsDemux *demux = GST_PS_DEMUX (parent);
3052 GstFlowReturn ret = GST_FLOW_OK;
3054 gboolean save, discont;
3055 discont = GST_BUFFER_IS_DISCONT (buffer);
3057 GST_LOG_OBJECT (demux,
3058 "Received buffer with discont flag and" " offset %"
3059 G_GUINT64_FORMAT, GST_BUFFER_OFFSET (buffer));
3060 gst_pes_filter_drain (&demux->filter);
3061 gst_ps_demux_mark_discont (demux, TRUE, FALSE);
3062 /* mark discont on all streams */
3063 if (demux->sink_segment.rate >= 0.0) {
3064 demux->current_scr = G_MAXUINT64;
3065 demux->bytes_since_scr = 0;
3068 GST_LOG_OBJECT (demux,
3069 "Received buffer with offset %" G_GUINT64_FORMAT,
3070 GST_BUFFER_OFFSET (buffer));
3073 /* We keep the offset to interpolate SCR */
3074 demux->adapter_offset = GST_BUFFER_OFFSET (buffer);
3075 gst_adapter_push (demux->adapter, buffer);
3076 demux->bytes_since_scr += gst_buffer_get_size (buffer);
3077 avail = gst_adapter_available (demux->rev_adapter);
3079 GST_LOG_OBJECT (demux, "appending %u saved bytes", avail);
3080 /* if we have a previous reverse chunk, append this now */
3081 /* FIXME this code assumes we receive discont buffers all thei
3083 gst_adapter_push (demux->adapter,
3084 gst_adapter_take_buffer (demux->rev_adapter, avail));
3087 avail = gst_adapter_available (demux->adapter);
3088 GST_LOG_OBJECT (demux, "avail now: %d, state %d", avail, demux->filter.state);
3089 switch (demux->filter.state) {
3090 case STATE_DATA_SKIP:
3091 case STATE_DATA_PUSH:
3092 ret = gst_pes_filter_process (&demux->filter);
3094 case STATE_HEADER_PARSE:
3101 case GST_FLOW_NEED_MORE_DATA:
3102 /* Go and get more data */
3105 case GST_FLOW_LOST_SYNC:
3106 /* for FLOW_OK or lost-sync, carry onto resync */
3112 /* Any other return value should be sent upstream immediately */
3116 /* align adapter data to sync boundary, we keep the data up to the next sync
3119 while (gst_ps_demux_resync (demux, save)) {
3120 gboolean ps_sync = TRUE;
3121 if (G_UNLIKELY (demux->flushing)) {
3122 ret = GST_FLOW_FLUSHING;
3126 /* now switch on last synced byte */
3127 switch (demux->last_sync_code) {
3128 case ID_PS_PACK_START_CODE:
3129 ret = gst_ps_demux_parse_pack_start (demux);
3131 case ID_PS_SYSTEM_HEADER_START_CODE:
3132 ret = gst_ps_demux_parse_sys_head (demux);
3134 case ID_PS_END_CODE:
3135 /* Skip final 4 bytes */
3136 gst_adapter_flush (demux->adapter, 4);
3137 ADAPTER_OFFSET_FLUSH (4);
3140 case ID_PS_PROGRAM_STREAM_MAP:
3141 ret = gst_ps_demux_parse_psm (demux);
3144 if (gst_ps_demux_is_pes_sync (demux->last_sync_code)) {
3145 ret = gst_pes_filter_process (&demux->filter);
3147 GST_DEBUG_OBJECT (demux, "sync_code=%08x, non PES sync found"
3148 ", continuing", demux->last_sync_code);
3150 ret = GST_FLOW_LOST_SYNC;
3154 /* if we found a ps sync, we stop saving the data, any non-ps sync gets
3155 * saved up to the next ps sync. */
3159 case GST_FLOW_NEED_MORE_DATA:
3160 GST_DEBUG_OBJECT (demux, "need more data");
3163 case GST_FLOW_LOST_SYNC:
3164 if (!save || demux->sink_segment.rate >= 0.0) {
3165 GST_DEBUG_OBJECT (demux, "flushing 3 bytes");
3166 gst_adapter_flush (demux->adapter, 3);
3167 ADAPTER_OFFSET_FLUSH (3);
3169 GST_DEBUG_OBJECT (demux, "saving 3 bytes");
3170 gst_adapter_push (demux->rev_adapter,
3171 gst_adapter_take_buffer (demux->adapter, 3));
3176 ret = gst_ps_demux_combine_flows (demux, ret);
3177 if (ret != GST_FLOW_OK)
3186 static GstStateChangeReturn
3187 gst_ps_demux_change_state (GstElement * element, GstStateChange transition)
3189 GstPsDemux *demux = GST_PS_DEMUX (element);
3190 GstStateChangeReturn result;
3191 switch (transition) {
3192 case GST_STATE_CHANGE_NULL_TO_READY:
3193 gst_pes_filter_init (&demux->filter, demux->adapter,
3194 &demux->adapter_offset);
3195 gst_pes_filter_set_callbacks (&demux->filter,
3196 (GstPESFilterData) gst_ps_demux_data_cb,
3197 (GstPESFilterResync) gst_ps_demux_resync_cb, demux);
3198 demux->filter.gather_pes = TRUE;
3200 case GST_STATE_CHANGE_READY_TO_PAUSED:
3206 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3207 switch (transition) {
3208 case GST_STATE_CHANGE_PAUSED_TO_READY:
3209 gst_ps_demux_reset (demux);
3211 case GST_STATE_CHANGE_READY_TO_NULL:
3212 gst_pes_filter_uninit (&demux->filter);
3222 gst_segment_set_position (GstSegment * segment, GstFormat format,
3225 if (segment->format == GST_FORMAT_UNDEFINED) {
3226 segment->format = format;
3228 segment->position = position;
3232 gst_segment_set_duration (GstSegment * segment, GstFormat format,
3235 if (segment->format == GST_FORMAT_UNDEFINED) {
3236 segment->format = format;
3238 segment->duration = duration;