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., 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, 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 "gstmpegdefs.h"
52 #include "gstmpegdemux.h"
54 #define MAX_DVD_AUDIO_STREAMS 8
55 #define MAX_DVD_SUBPICTURE_STREAMS 32
56 #define BLOCK_SZ 32768
57 #define SCAN_SCR_SZ 12
58 #define SCAN_PTS_SZ 80
60 #define SEGMENT_THRESHOLD (300*GST_MSECOND)
61 #define VIDEO_SEGMENT_THRESHOLD (500*GST_MSECOND)
70 /* We clamp scr delta with 0 so negative bytes won't be possible */
71 #define GSTTIME_TO_BYTES(time) \
72 ((time != -1) ? gst_util_uint64_scale (MAX(0,(gint64) (GSTTIME_TO_MPEGTIME(time))), demux->scr_rate_n, demux->scr_rate_d) : -1)
73 #define BYTES_TO_GSTTIME(bytes) ((bytes != -1) ? MPEGTIME_TO_GSTTIME(gst_util_uint64_scale (bytes, demux->scr_rate_d, demux->scr_rate_n)) : -1)
75 #define ADAPTER_OFFSET_FLUSH(_bytes_) demux->adapter_offset += (_bytes_)
77 GST_DEBUG_CATEGORY_STATIC (gstflupsdemux_debug);
78 #define GST_CAT_DEFAULT (gstflupsdemux_debug)
80 /* MPEG2Demux signals and args */
94 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
97 GST_STATIC_CAPS ("video/mpeg, "
98 "mpegversion = (int) { 1, 2 }, "
99 "systemstream = (boolean) TRUE;" "video/x-cdxa")
102 static GstStaticPadTemplate video_template =
103 GST_STATIC_PAD_TEMPLATE ("video_%02x",
106 GST_STATIC_CAPS ("video/mpeg, "
107 "mpegversion = (int) { 1, 2, 4 }, " "systemstream = (boolean) FALSE;"
111 static GstStaticPadTemplate audio_template =
112 GST_STATIC_PAD_TEMPLATE ("audio_%02x",
115 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1;"
116 "audio/mpeg, mpegversion = (int) 4, stream-format = (string) { adts, loas };"
117 "audio/x-private1-lpcm; "
118 "audio/x-private1-ac3;" "audio/x-private1-dts;" "audio/ac3")
121 static GstStaticPadTemplate subpicture_template =
122 GST_STATIC_PAD_TEMPLATE ("subpicture_%02x",
125 GST_STATIC_CAPS ("video/x-dvd-subpicture")
128 static GstStaticPadTemplate private_template =
129 GST_STATIC_PAD_TEMPLATE ("private_%d",
132 GST_STATIC_CAPS_ANY);
134 static void gst_flups_demux_base_init (GstFluPSDemuxClass * klass);
135 static void gst_flups_demux_class_init (GstFluPSDemuxClass * klass);
136 static void gst_flups_demux_init (GstFluPSDemux * demux);
137 static void gst_flups_demux_finalize (GstFluPSDemux * demux);
138 static void gst_flups_demux_reset (GstFluPSDemux * demux);
140 static gboolean gst_flups_demux_sink_event (GstPad * pad, GstEvent * event);
141 static GstFlowReturn gst_flups_demux_chain (GstPad * pad, GstBuffer * buffer);
142 static gboolean gst_flups_demux_sink_activate (GstPad * sinkpad);
143 static gboolean gst_flups_demux_sink_activate_push (GstPad * sinkpad,
145 static gboolean gst_flups_demux_sink_activate_pull (GstPad * sinkpad,
147 static void gst_flups_demux_loop (GstPad * pad);
149 static gboolean gst_flups_demux_src_event (GstPad * pad, GstEvent * event);
150 static gboolean gst_flups_demux_src_query (GstPad * pad, GstQuery * query);
151 static const GstQueryType *gst_flups_demux_src_query_type (GstPad * pad);
153 static GstStateChangeReturn gst_flups_demux_change_state (GstElement * element,
154 GstStateChange transition);
156 static inline gboolean gst_flups_demux_scan_forward_ts (GstFluPSDemux * demux,
157 guint64 * pos, SCAN_MODE mode, guint64 * rts);
158 static inline gboolean gst_flups_demux_scan_backward_ts (GstFluPSDemux * demux,
159 guint64 * pos, SCAN_MODE mode, guint64 * rts);
161 static inline void gst_flups_demux_send_segment_updates (GstFluPSDemux * demux,
162 GstClockTime new_time);
163 static inline void gst_flups_demux_clear_times (GstFluPSDemux * demux);
165 static GstElementClass *parent_class = NULL;
167 /*static guint gst_flups_demux_signals[LAST_SIGNAL] = { 0 };*/
170 gst_flups_demux_get_type (void)
172 static GType flups_demux_type = 0;
174 if (!flups_demux_type) {
175 static const GTypeInfo flups_demux_info = {
176 sizeof (GstFluPSDemuxClass),
177 (GBaseInitFunc) gst_flups_demux_base_init,
179 (GClassInitFunc) gst_flups_demux_class_init,
182 sizeof (GstFluPSDemux),
184 (GInstanceInitFunc) gst_flups_demux_init,
188 g_type_register_static (GST_TYPE_ELEMENT, "GstMpegPSDemux",
189 &flups_demux_info, 0);
191 GST_DEBUG_CATEGORY_INIT (gstflupsdemux_debug, "mpegpsdemux", 0,
192 "MPEG program stream demultiplexer element");
195 return flups_demux_type;
199 gst_flups_demux_base_init (GstFluPSDemuxClass * klass)
201 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
203 klass->sink_template = gst_static_pad_template_get (&sink_template);
204 klass->video_template = gst_static_pad_template_get (&video_template);
205 klass->audio_template = gst_static_pad_template_get (&audio_template);
206 klass->subpicture_template =
207 gst_static_pad_template_get (&subpicture_template);
208 klass->private_template = gst_static_pad_template_get (&private_template);
210 gst_element_class_add_pad_template (element_class, klass->video_template);
211 gst_element_class_add_pad_template (element_class, klass->audio_template);
212 gst_element_class_add_pad_template (element_class,
213 klass->subpicture_template);
214 gst_element_class_add_pad_template (element_class, klass->private_template);
215 gst_element_class_add_pad_template (element_class, klass->sink_template);
217 gst_element_class_set_details_simple (element_class,
218 "The Fluendo MPEG Program Stream Demuxer", "Codec/Demuxer",
219 "Demultiplexes MPEG Program Streams", "Wim Taymans <wim@fluendo.com>");
223 gst_flups_demux_class_init (GstFluPSDemuxClass * klass)
225 GObjectClass *gobject_class;
226 GstElementClass *gstelement_class;
228 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
230 gobject_class = (GObjectClass *) klass;
231 gstelement_class = (GstElementClass *) klass;
233 gobject_class->finalize = (GObjectFinalizeFunc) gst_flups_demux_finalize;
235 gstelement_class->change_state = gst_flups_demux_change_state;
239 gst_flups_demux_init (GstFluPSDemux * demux)
241 GstFluPSDemuxClass *klass = GST_FLUPS_DEMUX_GET_CLASS (demux);
243 demux->sinkpad = gst_pad_new_from_template (klass->sink_template, "sink");
244 gst_pad_set_event_function (demux->sinkpad,
245 GST_DEBUG_FUNCPTR (gst_flups_demux_sink_event));
246 gst_pad_set_chain_function (demux->sinkpad,
247 GST_DEBUG_FUNCPTR (gst_flups_demux_chain));
248 gst_pad_set_activate_function (demux->sinkpad,
249 GST_DEBUG_FUNCPTR (gst_flups_demux_sink_activate));
250 gst_pad_set_activatepull_function (demux->sinkpad,
251 GST_DEBUG_FUNCPTR (gst_flups_demux_sink_activate_pull));
252 gst_pad_set_activatepush_function (demux->sinkpad,
253 GST_DEBUG_FUNCPTR (gst_flups_demux_sink_activate_push));
255 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
258 g_malloc0 (sizeof (GstFluPSStream *) * (GST_FLUPS_DEMUX_MAX_STREAMS));
259 demux->streams_found =
260 g_malloc0 (sizeof (GstFluPSStream *) * (GST_FLUPS_DEMUX_MAX_STREAMS));
261 demux->found_count = 0;
266 gst_flups_demux_finalize (GstFluPSDemux * demux)
268 gst_flups_demux_reset (demux);
269 g_free (demux->streams);
270 g_free (demux->streams_found);
272 G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (demux));
276 gst_flups_demux_reset (GstFluPSDemux * demux)
278 /* Clean up the streams and pads we allocated */
282 for (i = 0; i < GST_FLUPS_DEMUX_MAX_STREAMS; i++) {
283 GstFluPSStream *stream = demux->streams[i];
285 if (stream != NULL) {
287 gst_element_remove_pad (GST_ELEMENT_CAST (demux), stream->pad);
290 demux->streams[i] = NULL;
293 memset (demux->streams_found, 0,
294 sizeof (GstFluPSStream *) * (GST_FLUPS_DEMUX_MAX_STREAMS));
295 demux->found_count = 0;
296 p_ev = &demux->lang_codes;
298 gst_event_replace (p_ev, NULL);
301 static GstFluPSStream *
302 gst_flups_demux_create_stream (GstFluPSDemux * demux, gint id, gint stream_type)
304 GstFluPSStream *stream;
305 GstPadTemplate *template;
307 GstFluPSDemuxClass *klass = GST_FLUPS_DEMUX_GET_CLASS (demux);
309 GstClockTime threshold = SEGMENT_THRESHOLD;
315 GST_DEBUG_OBJECT (demux, "create stream id 0x%02x, type 0x%02x", id,
318 switch (stream_type) {
322 case ST_GST_VIDEO_MPEG1_OR_2:
324 gint mpeg_version = 1;
325 if (stream_type == ST_VIDEO_MPEG2 ||
326 (stream_type == ST_GST_VIDEO_MPEG1_OR_2 && demux->is_mpeg2_pack)) {
329 if (stream_type == ST_VIDEO_MPEG4) {
333 template = klass->video_template;
334 name = g_strdup_printf ("video_%02x", id);
335 caps = gst_caps_new_simple ("video/mpeg",
336 "mpegversion", G_TYPE_INT, mpeg_version,
337 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
338 threshold = VIDEO_SEGMENT_THRESHOLD;
343 template = klass->audio_template;
344 name = g_strdup_printf ("audio_%02x", id);
345 caps = gst_caps_new_simple ("audio/mpeg",
346 "mpegversion", G_TYPE_INT, 1, NULL);
348 case ST_PRIVATE_SECTIONS:
349 case ST_PRIVATE_DATA:
353 case ST_AUDIO_AAC_ADTS:
354 template = klass->audio_template;
355 name = g_strdup_printf ("audio_%02x", id);
356 caps = gst_caps_new_simple ("audio/mpeg",
357 "mpegversion", G_TYPE_INT, 4,
358 "stream-format", G_TYPE_STRING, "adts", NULL);
360 case ST_AUDIO_AAC_LOAS: // LATM/LOAS AAC syntax
361 template = klass->audio_template;
362 name = g_strdup_printf ("audio_%02x", id);
363 caps = gst_caps_new_simple ("audio/mpeg",
364 "mpegversion", G_TYPE_INT, 4,
365 "stream-format", G_TYPE_STRING, "loas", NULL);
368 template = klass->video_template;
369 name = g_strdup_printf ("video_%02x", id);
370 caps = gst_caps_new_simple ("video/x-h264", NULL);
371 threshold = VIDEO_SEGMENT_THRESHOLD;
373 case ST_PS_AUDIO_AC3:
374 template = klass->audio_template;
375 name = g_strdup_printf ("audio_%02x", id);
376 caps = gst_caps_new_simple ("audio/x-private1-ac3", NULL);
378 case ST_PS_AUDIO_DTS:
379 template = klass->audio_template;
380 name = g_strdup_printf ("audio_%02x", id);
381 caps = gst_caps_new_simple ("audio/x-private1-dts", NULL);
383 case ST_PS_AUDIO_LPCM:
384 template = klass->audio_template;
385 name = g_strdup_printf ("audio_%02x", id);
386 caps = gst_caps_new_simple ("audio/x-private1-lpcm", NULL);
388 case ST_PS_DVD_SUBPICTURE:
389 template = klass->subpicture_template;
390 name = g_strdup_printf ("subpicture_%02x", id);
391 caps = gst_caps_new_simple ("video/x-dvd-subpicture", NULL);
393 case ST_GST_AUDIO_RAWA52:
394 template = klass->audio_template;
395 name = g_strdup_printf ("audio_%02x", id);
396 caps = gst_caps_new_simple ("audio/ac3", NULL);
402 if (name == NULL || template == NULL || caps == NULL) {
406 gst_caps_unref (caps);
410 stream = g_new0 (GstFluPSStream, 1);
412 stream->discont = TRUE;
413 stream->need_segment = TRUE;
414 stream->notlinked = FALSE;
415 stream->type = stream_type;
416 stream->pad = gst_pad_new_from_template (template, name);
417 stream->segment_thresh = threshold;
418 gst_pad_set_event_function (stream->pad,
419 GST_DEBUG_FUNCPTR (gst_flups_demux_src_event));
420 gst_pad_set_query_function (stream->pad,
421 GST_DEBUG_FUNCPTR (gst_flups_demux_src_query));
422 gst_pad_set_query_type_function (stream->pad,
423 GST_DEBUG_FUNCPTR (gst_flups_demux_src_query_type));
424 gst_pad_use_fixed_caps (stream->pad);
425 gst_pad_set_caps (stream->pad, caps);
426 gst_caps_unref (caps);
427 GST_DEBUG_OBJECT (demux, "create pad %s, caps %" GST_PTR_FORMAT, name, caps);
434 static GstFluPSStream *
435 gst_flups_demux_get_stream (GstFluPSDemux * demux, gint id, gint type)
437 GstFluPSStream *stream = demux->streams[id];
439 if (stream == NULL) {
440 if (!(stream = gst_flups_demux_create_stream (demux, id, type)))
443 GST_DEBUG_OBJECT (demux, "adding pad for stream id 0x%02x type 0x%02x", id,
446 gst_pad_set_active (stream->pad, TRUE);
447 gst_element_add_pad (GST_ELEMENT (demux), stream->pad);
449 demux->streams[id] = stream;
450 demux->streams_found[demux->found_count++] = stream;
457 GST_DEBUG_OBJECT (demux, "unknown stream id 0x%02x type 0x%02x", id, type);
463 gst_flups_demux_send_data (GstFluPSDemux * demux, GstFluPSStream * stream,
466 GstFlowReturn result;
474 if (G_UNLIKELY (demux->next_pts != G_MAXUINT64))
475 timestamp = MPEGTIME_TO_GSTTIME (demux->next_pts);
477 timestamp = GST_CLOCK_TIME_NONE;
480 if (G_UNLIKELY (stream->need_segment)) {
481 gint64 time, start, stop;
482 GstEvent *newsegment;
484 GST_DEBUG ("timestamp:%" GST_TIME_FORMAT " base_time %" GST_TIME_FORMAT
485 " src_segment.start:%" GST_TIME_FORMAT " .stop:%" GST_TIME_FORMAT,
486 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (demux->base_time),
487 GST_TIME_ARGS (demux->src_segment.start),
488 GST_TIME_ARGS (demux->src_segment.stop));
490 if (GST_CLOCK_TIME_IS_VALID (demux->base_time) &&
491 GST_CLOCK_TIME_IS_VALID (demux->src_segment.start))
492 start = demux->base_time + demux->src_segment.start;
496 if (GST_CLOCK_TIME_IS_VALID (demux->src_segment.stop) &&
497 GST_CLOCK_TIME_IS_VALID (demux->base_time))
498 stop = demux->base_time + demux->src_segment.stop;
502 if (timestamp != GST_CLOCK_TIME_NONE) {
503 if (demux->src_segment.rate > 0) {
504 if (GST_CLOCK_DIFF (start, timestamp) > GST_SECOND)
507 if (GST_CLOCK_DIFF (stop, timestamp) > GST_SECOND)
511 if (GST_CLOCK_TIME_IS_VALID (demux->base_time) && start > demux->base_time)
512 time = start - demux->base_time;
516 GST_INFO_OBJECT (demux, "sending new segment: rate %g applied_rate %g "
517 "start: %" GST_TIME_FORMAT ", stop: %" GST_TIME_FORMAT
518 ", time: %" GST_TIME_FORMAT " to pad %" GST_PTR_FORMAT,
519 demux->sink_segment.rate, demux->sink_segment.applied_rate,
520 GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
521 GST_TIME_ARGS (time), stream->pad);
523 newsegment = gst_event_new_new_segment_full (FALSE,
524 demux->sink_segment.rate, demux->sink_segment.applied_rate,
525 GST_FORMAT_TIME, start, stop, time);
527 gst_pad_push_event (stream->pad, newsegment);
529 stream->need_segment = FALSE;
532 /* OK, sent new segment now prepare the buffer for sending */
534 gst_buffer_set_caps (buf, GST_PAD_CAPS (stream->pad));
535 GST_BUFFER_TIMESTAMP (buf) = timestamp;
537 /* update position in the segment */
538 gst_segment_set_last_stop (&demux->src_segment, GST_FORMAT_TIME,
539 MPEGTIME_TO_GSTTIME (demux->current_scr - demux->first_scr));
540 GST_LOG_OBJECT (demux, "last stop position is now %" GST_TIME_FORMAT
541 " current scr is %" GST_TIME_FORMAT,
542 GST_TIME_ARGS (demux->src_segment.last_stop),
543 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->current_scr)));
545 if (demux->src_segment.last_stop != GST_CLOCK_TIME_NONE) {
546 GstClockTime new_time = demux->base_time + demux->src_segment.last_stop;
548 if (stream->last_ts == GST_CLOCK_TIME_NONE || stream->last_ts < new_time) {
550 g_print ("last_ts update on pad %s to time %" GST_TIME_FORMAT "\n",
551 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (cur_scr_time));
553 stream->last_ts = new_time;
556 gst_flups_demux_send_segment_updates (demux, new_time);
559 /* Set the buffer discont flag, and clear discont state on the stream */
560 if (stream->discont) {
561 GST_DEBUG_OBJECT (demux, "marking discont buffer");
562 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
564 stream->discont = FALSE;
566 size = GST_BUFFER_SIZE (buf);
568 demux->next_pts = G_MAXUINT64;
569 demux->next_dts = G_MAXUINT64;
571 result = gst_pad_push (stream->pad, buf);
572 GST_DEBUG_OBJECT (demux, "pushed stream id 0x%02x type 0x%02x, time: %"
573 GST_TIME_FORMAT ", size %d. result: %s",
574 stream->id, stream->type, GST_TIME_ARGS (timestamp),
575 size, gst_flow_get_name (result));
582 GST_DEBUG_OBJECT (demux, "no stream given");
583 gst_buffer_unref (buf);
589 gst_flups_demux_mark_discont (GstFluPSDemux * demux, gboolean discont,
590 gboolean need_segment)
592 gint i, count = demux->found_count;
594 /* mark discont on all streams */
595 for (i = 0; i < count; i++) {
596 GstFluPSStream *stream = demux->streams_found[i];
598 if (G_LIKELY (stream)) {
599 stream->discont |= discont;
600 stream->need_segment |= need_segment;
601 GST_DEBUG_OBJECT (demux, "marked stream as discont %d, need_segment %d",
602 stream->discont, stream->need_segment);
607 static inline gboolean
608 gst_flups_demux_send_event (GstFluPSDemux * demux, GstEvent * event)
610 gint i, count = demux->found_count;
611 gboolean ret = FALSE;
613 for (i = 0; i < count; i++) {
614 GstFluPSStream *stream = demux->streams_found[i];
616 if (stream && !stream->notlinked) {
617 (void) gst_event_ref (event);
619 if (!gst_pad_push_event (stream->pad, event)) {
620 GST_DEBUG_OBJECT (stream, "event %s was not handled correctly",
621 GST_EVENT_TYPE_NAME (event));
623 /* If at least one push returns TRUE, then we return TRUE. */
624 GST_DEBUG_OBJECT (stream, "event %s was handled correctly",
625 GST_EVENT_TYPE_NAME (event));
631 gst_event_unref (event);
636 gst_flups_demux_handle_dvd_event (GstFluPSDemux * demux, GstEvent * event)
638 const GstStructure *structure = gst_event_get_structure (event);
639 const char *type = gst_structure_get_string (structure, "event");
641 gchar cur_stream_name[32];
642 GstFluPSStream *temp G_GNUC_UNUSED;
644 if (strcmp (type, "dvd-lang-codes") == 0) {
646 /* Store the language codes event on the element, then iterate over the
647 * streams it specifies and retrieve them. The stream creation code then
648 * creates the pad appropriately and sends tag events as needed */
649 p_ev = &demux->lang_codes, event;
650 gst_event_replace (p_ev, event);
652 GST_DEBUG_OBJECT (demux, "Handling language codes event");
654 /* Create a video pad to ensure have it before emit no more pads */
655 (void) gst_flups_demux_get_stream (demux, 0xe0, ST_VIDEO_MPEG2);
657 /* Read out the languages for audio streams and request each one that
659 for (i = 0; i < MAX_DVD_AUDIO_STREAMS; i++) {
663 g_snprintf (cur_stream_name, 32, "audio-%d-format", i);
664 if (!gst_structure_get_int (structure, cur_stream_name, &stream_format))
667 switch (stream_format) {
670 stream_id = 0x80 + i;
671 temp = gst_flups_demux_get_stream (demux, stream_id, ST_PS_AUDIO_AC3);
675 /* MPEG audio without and with extension stream are
676 * treated the same */
677 stream_id = 0xC0 + i;
678 temp = gst_flups_demux_get_stream (demux, stream_id, ST_AUDIO_MPEG1);
682 stream_id = 0xA0 + i;
684 gst_flups_demux_get_stream (demux, stream_id, ST_PS_AUDIO_LPCM);
688 stream_id = 0x88 + i;
689 temp = gst_flups_demux_get_stream (demux, stream_id, ST_PS_AUDIO_DTS);
692 /* FIXME: What range is SDDS? */
695 GST_WARNING_OBJECT (demux,
696 "Unknown audio stream format in language code event: %d",
702 /* And subtitle streams */
703 for (i = 0; i < MAX_DVD_SUBPICTURE_STREAMS; i++) {
704 g_snprintf (cur_stream_name, 32, "subpicture-%d-format", i);
706 if (gst_structure_get_string (structure, cur_stream_name) == NULL)
709 /* Retrieve the subpicture stream to force pad creation */
710 temp = gst_flups_demux_get_stream (demux, 0x20 + i, ST_PS_DVD_SUBPICTURE);
713 GST_DEBUG_OBJECT (demux, "Created all pads from Language Codes event, "
714 "signalling no-more-pads");
716 gst_element_no_more_pads (GST_ELEMENT (demux));
717 demux->need_no_more_pads = FALSE;
720 gst_event_unref (event);
725 gst_flups_demux_flush (GstFluPSDemux * demux)
727 GST_DEBUG_OBJECT (demux, "flushing demuxer");
728 gst_adapter_clear (demux->adapter);
729 gst_adapter_clear (demux->rev_adapter);
730 gst_pes_filter_drain (&demux->filter);
731 gst_flups_demux_clear_times (demux);
732 demux->adapter_offset = G_MAXUINT64;
733 demux->current_scr = G_MAXUINT64;
734 demux->bytes_since_scr = 0;
738 gst_flups_demux_clear_times (GstFluPSDemux * demux)
740 gint i, count = demux->found_count;
742 /* Clear the last ts for all streams */
743 for (i = 0; i < count; i++) {
744 GstFluPSStream *stream = demux->streams_found[i];
746 if (G_LIKELY (stream)) {
747 stream->last_seg_start = stream->last_ts = GST_CLOCK_TIME_NONE;
753 gst_flups_demux_send_segment_updates (GstFluPSDemux * demux,
754 GstClockTime new_start)
756 GstClockTime base_time, stop, time;
757 gint i, count = demux->found_count;
758 GstEvent *event = NULL;
760 /* Advance all lagging streams by sending a segment update */
761 if ((base_time = demux->base_time) == GST_CLOCK_TIME_NONE)
764 stop = demux->src_segment.stop;
765 if (stop != GST_CLOCK_TIME_NONE)
768 if (new_start > stop)
771 time = demux->src_segment.time;
772 time += new_start - (demux->src_segment.start + base_time);
774 /* FIXME: Handle reverse playback */
775 for (i = 0; i < count; i++) {
776 GstFluPSStream *stream = demux->streams_found[i];
779 if (stream->last_ts == GST_CLOCK_TIME_NONE ||
780 stream->last_ts < demux->src_segment.start + base_time)
781 stream->last_ts = demux->src_segment.start + base_time;
783 if (stream->last_ts + stream->segment_thresh < new_start) {
785 g_print ("Segment update to pad %s time %" GST_TIME_FORMAT " stop now %"
786 GST_TIME_FORMAT "\n", GST_PAD_NAME (stream->pad),
787 GST_TIME_ARGS (new_start), GST_TIME_ARGS (demux->src_segment.stop));
789 GST_DEBUG_OBJECT (demux,
790 "Segment update to pad %s time %" GST_TIME_FORMAT,
791 GST_PAD_NAME (stream->pad), GST_TIME_ARGS (new_start));
793 event = gst_event_new_new_segment_full (TRUE,
794 demux->src_segment.rate, demux->src_segment.applied_rate,
795 GST_FORMAT_TIME, new_start, stop, time);
797 gst_event_ref (event);
798 gst_pad_push_event (stream->pad, event);
799 stream->last_seg_start = stream->last_ts = new_start;
800 stream->need_segment = FALSE;
806 gst_event_unref (event);
810 gst_flups_demux_close_segment (GstFluPSDemux * demux)
812 gint i, count = demux->found_count;
813 GstEvent *event = NULL;
816 GST_INFO_OBJECT (demux, "closing running segment %" GST_SEGMENT_FORMAT,
817 &demux->src_segment);
819 /* FIXME: Need to send a different segment-close to each pad where the
820 * last_seg_start != clock_time_none, as that indicates a sparse-stream
821 * event was sent there */
823 if ((base_time = demux->base_time) == (guint64) - 1)
826 /* Close the current segment for a linear playback */
827 if (demux->src_segment.rate >= 0) {
828 /* for forward playback, we played from start to last_stop */
829 event = gst_event_new_new_segment (TRUE,
830 demux->src_segment.rate, demux->src_segment.format,
831 demux->src_segment.start + base_time,
832 demux->src_segment.last_stop + base_time, demux->src_segment.time);
836 if ((stop = demux->src_segment.stop) == -1)
837 stop = demux->src_segment.duration;
839 /* for reverse playback, we played from stop to last_stop. */
840 event = gst_event_new_new_segment (TRUE,
841 demux->src_segment.rate, demux->src_segment.format,
842 demux->src_segment.last_stop + base_time,
843 stop + base_time, demux->src_segment.last_stop);
847 for (i = 0; i < count; i++) {
848 GstFluPSStream *stream = demux->streams_found[i];
850 if (stream && !stream->notlinked && !stream->need_segment) {
851 (void) gst_event_ref (event);
853 if (!gst_pad_push_event (stream->pad, event)) {
854 GST_DEBUG_OBJECT (stream, "event %s was not handled correctly",
855 GST_EVENT_TYPE_NAME (event));
857 /* If at least one push returns TRUE, then we return TRUE. */
858 GST_DEBUG_OBJECT (stream, "event %s was handled correctly",
859 GST_EVENT_TYPE_NAME (event));
864 gst_event_unref (event);
868 static inline gboolean
869 have_open_streams (GstFluPSDemux * demux)
871 return (demux->streams_found[0] != NULL);
875 gst_flups_demux_sink_event (GstPad * pad, GstEvent * event)
878 GstFluPSDemux *demux;
880 demux = GST_FLUPS_DEMUX (gst_pad_get_parent (pad));
882 switch (GST_EVENT_TYPE (event)) {
883 case GST_EVENT_FLUSH_START:
884 gst_flups_demux_send_event (demux, event);
886 case GST_EVENT_FLUSH_STOP:
887 gst_flups_demux_send_event (demux, event);
888 gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
889 gst_flups_demux_flush (demux);
891 case GST_EVENT_NEWSEGMENT:
896 gint64 start, stop, time;
898 /* Close current segment */
899 gst_flups_demux_close_segment (demux);
904 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
905 &format, &start, &stop, &time);
906 gst_segment_set_newsegment_full (&demux->sink_segment, update, rate,
907 arate, format, start, stop, time);
909 if (format == GST_FORMAT_BYTES && demux->scr_rate_n != G_MAXUINT64
910 && demux->scr_rate_d != G_MAXUINT64) {
912 gst_segment_set_newsegment_full (&demux->src_segment, update, rate,
913 arate, GST_FORMAT_TIME, BYTES_TO_GSTTIME (start),
914 BYTES_TO_GSTTIME (stop), BYTES_TO_GSTTIME (time));
919 GST_INFO_OBJECT (demux, "received new segment: rate %g "
920 "format %d, start: %" G_GINT64_FORMAT ", stop: %" G_GINT64_FORMAT
921 ", time: %" G_GINT64_FORMAT, rate, format, start, stop, time);
923 /* we need to emit a new segment */
924 gst_flups_demux_mark_discont (demux, TRUE, TRUE);
925 gst_event_unref (event);
930 GST_INFO_OBJECT (demux, "Received EOS");
931 if (!gst_flups_demux_send_event (demux, event)
932 && !have_open_streams (demux)) {
933 GST_WARNING_OBJECT (demux, "EOS and no streams open");
934 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
935 ("Internal data stream error."), ("No valid streams detected"));
938 case GST_EVENT_CUSTOM_DOWNSTREAM:
939 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
941 const GstStructure *structure = gst_event_get_structure (event);
943 if (structure != NULL
944 && gst_structure_has_name (structure, "application/x-gst-dvd")) {
945 res = gst_flups_demux_handle_dvd_event (demux, event);
947 gst_flups_demux_send_event (demux, event);
952 gst_flups_demux_send_event (demux, event);
956 gst_object_unref (demux);
962 gst_flups_demux_handle_seek_push (GstFluPSDemux * demux, GstEvent * event)
964 gboolean res = FALSE;
968 GstSeekType start_type, stop_type;
970 gint64 bstart, bstop;
973 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
976 GST_DEBUG_OBJECT (demux, "seek event, rate: %f start: %" GST_TIME_FORMAT
977 " stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
978 GST_TIME_ARGS (stop));
980 if (format == GST_FORMAT_BYTES) {
981 GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format);
985 GST_DEBUG_OBJECT (demux, "seek - trying directly upstream first");
987 /* first try original format seek */
988 (void) gst_event_ref (event);
989 if ((res = gst_pad_push_event (demux->sinkpad, event)))
992 if (format != GST_FORMAT_TIME) {
993 /* From here down, we only support time based seeks */
994 GST_DEBUG_OBJECT (demux, "seek not supported on format %d", format);
998 /* We need to convert to byte based seek and we need a scr_rate for that. */
999 if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64) {
1000 GST_DEBUG_OBJECT (demux, "seek not possible, no scr_rate");
1004 GST_DEBUG_OBJECT (demux, "try with scr_rate interpolation");
1006 bstart = GSTTIME_TO_BYTES (start);
1007 bstop = GSTTIME_TO_BYTES (stop);
1009 GST_DEBUG_OBJECT (demux, "in bytes bstart %" G_GINT64_FORMAT " bstop %"
1010 G_GINT64_FORMAT, bstart, bstop);
1011 bevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, start_type,
1012 bstart, stop_type, bstop);
1014 res = gst_pad_push_event (demux->sinkpad, bevent);
1017 gst_event_unref (event);
1022 gst_event_unref (event);
1029 gst_flups_demux_do_seek (GstFluPSDemux * demux, GstSegment * seeksegment)
1032 guint64 fscr, offset;
1033 guint64 scr = GSTTIME_TO_MPEGTIME (seeksegment->last_stop + demux->base_time);
1034 guint64 scr_rate_n = demux->last_scr_offset - demux->first_scr_offset;
1035 guint64 scr_rate_d = demux->last_scr - demux->first_scr;
1037 /* In some clips the PTS values are completely unaligned with SCR values.
1038 * To improve the seek in that situation we apply a factor considering the
1039 * relationship between last PTS and last SCR */
1040 if (demux->last_scr > demux->last_pts)
1041 scr = gst_util_uint64_scale (scr, demux->last_scr, demux->last_pts);
1043 scr = MIN (demux->last_scr, scr);
1044 scr = MAX (demux->first_scr, scr);
1047 GST_INFO_OBJECT (demux, "sink segment configured %" GST_SEGMENT_FORMAT
1048 ", trying to go at SCR: %" G_GUINT64_FORMAT, &demux->sink_segment, scr);
1051 MIN (gst_util_uint64_scale (scr - demux->first_scr, scr_rate_n,
1052 scr_rate_d), demux->sink_segment.stop);
1054 found = gst_flups_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr);
1056 found = gst_flups_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr);
1059 while (found && fscr < scr) {
1061 found = gst_flups_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &fscr);
1064 while (found && fscr > scr && offset > 0) {
1066 found = gst_flups_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &fscr);
1069 GST_INFO_OBJECT (demux, "doing seek at offset %" G_GUINT64_FORMAT
1070 " SCR: %" G_GUINT64_FORMAT " %" GST_TIME_FORMAT,
1071 offset, fscr, GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (fscr)));
1072 gst_segment_set_last_stop (&demux->sink_segment, GST_FORMAT_BYTES, offset);
1076 gst_flups_demux_handle_seek_pull (GstFluPSDemux * demux, GstEvent * event)
1080 GstSeekType start_type, stop_type;
1083 gboolean update, flush;
1084 GstSegment seeksegment;
1085 GstClockTime first_pts = MPEGTIME_TO_GSTTIME (demux->first_pts);
1087 gst_event_parse_seek (event, &rate, &format, &flags,
1088 &start_type, &start, &stop_type, &stop);
1090 if (format != GST_FORMAT_TIME)
1093 GST_DEBUG_OBJECT (demux, "Seek requested start %" GST_TIME_FORMAT " stop %"
1094 GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1096 /* We need to convert to byte based seek and we need a scr_rate for that. */
1097 if (demux->scr_rate_n == G_MAXUINT64 || demux->scr_rate_d == G_MAXUINT64)
1100 flush = flags & GST_SEEK_FLAG_FLUSH;
1101 /* keyframe = flags & GST_SEEK_FLAG_KEY_UNIT; *//* FIXME */
1104 /* Flush start up and downstream to make sure data flow and loops are
1106 demux->flushing = TRUE;
1107 gst_flups_demux_send_event (demux, gst_event_new_flush_start ());
1108 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_start ());
1110 /* Pause the pulling task */
1111 gst_pad_pause_task (demux->sinkpad);
1114 /* Take the stream lock */
1115 GST_PAD_STREAM_LOCK (demux->sinkpad);
1118 /* Stop flushing upstream we need to pull */
1119 demux->flushing = FALSE;
1120 gst_pad_push_event (demux->sinkpad, gst_event_new_flush_stop ());
1123 /* Work on a copy until we are sure the seek succeeded. */
1124 memcpy (&seeksegment, &demux->src_segment, sizeof (GstSegment));
1126 GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
1127 &demux->src_segment);
1129 /* Apply the seek to our segment */
1130 gst_segment_set_seek (&seeksegment, rate, format, flags,
1131 start_type, start, stop_type, stop, &update);
1133 GST_DEBUG_OBJECT (demux, "seek segment configured %" GST_SEGMENT_FORMAT,
1136 if (flush || seeksegment.last_stop != demux->src_segment.last_stop) {
1137 /* Do the actual seeking */
1138 gst_flups_demux_do_seek (demux, &seeksegment);
1141 /* check the limits */
1142 if (seeksegment.rate > 0.0) {
1143 if (seeksegment.start < first_pts - demux->base_time) {
1144 seeksegment.start = first_pts - demux->base_time;
1145 seeksegment.last_stop = seeksegment.start;
1149 /* update the rate in our src segment */
1150 demux->sink_segment.rate = rate;
1152 GST_DEBUG_OBJECT (demux, "seek segment adjusted %" GST_SEGMENT_FORMAT,
1156 /* Stop flushing, the sinks are at time 0 now */
1157 gst_flups_demux_send_event (demux, gst_event_new_flush_stop ());
1159 gst_flups_demux_close_segment (demux);
1162 if (flush || seeksegment.last_stop != demux->src_segment.last_stop) {
1163 gst_flups_demux_flush (demux);
1166 /* Ok seek succeeded, take the newly configured segment */
1167 memcpy (&demux->src_segment, &seeksegment, sizeof (GstSegment));
1169 /* Notify about the start of a new segment */
1170 if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
1171 gst_element_post_message (GST_ELEMENT (demux),
1172 gst_message_new_segment_start (GST_OBJECT (demux),
1173 demux->src_segment.format, demux->src_segment.last_stop));
1176 /* Tell all the stream a new segment is needed */
1177 gst_flups_demux_mark_discont (demux, TRUE, TRUE);
1179 gst_pad_start_task (demux->sinkpad,
1180 (GstTaskFunction) gst_flups_demux_loop, demux->sinkpad);
1182 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
1184 gst_event_unref (event);
1190 GST_WARNING_OBJECT (demux, "we only support seeking in TIME or BYTES "
1192 gst_event_unref (event);
1197 GST_WARNING_OBJECT (demux, "seek not possible, no scr_rate");
1198 gst_event_unref (event);
1204 gst_flups_demux_src_event (GstPad * pad, GstEvent * event)
1206 gboolean res = FALSE;
1207 GstFluPSDemux *demux;
1209 demux = GST_FLUPS_DEMUX (gst_pad_get_parent (pad));
1211 switch (GST_EVENT_TYPE (event)) {
1212 case GST_EVENT_SEEK:
1213 if (demux->random_access) {
1214 res = gst_flups_demux_handle_seek_pull (demux, event);
1216 res = gst_flups_demux_handle_seek_push (demux, event);
1220 res = gst_pad_push_event (demux->sinkpad, event);
1224 gst_object_unref (demux);
1229 static const GstQueryType *
1230 gst_flups_demux_src_query_type (GstPad * pad)
1232 static const GstQueryType types[] = {
1243 gst_flups_demux_src_query (GstPad * pad, GstQuery * query)
1245 gboolean res = FALSE;
1246 GstFluPSDemux *demux;
1248 demux = GST_FLUPS_DEMUX (gst_pad_get_parent (pad));
1250 GST_LOG_OBJECT (demux, "Have query of type %d on pad %" GST_PTR_FORMAT,
1251 GST_QUERY_TYPE (query), pad);
1253 switch (GST_QUERY_TYPE (query)) {
1254 case GST_QUERY_POSITION:
1259 gst_query_parse_position (query, &format, NULL);
1261 if (format != GST_FORMAT_TIME) {
1262 GST_DEBUG_OBJECT (demux, "position not supported for format %d",
1267 pos = demux->src_segment.last_stop - demux->src_segment.start;
1268 GST_LOG_OBJECT (demux, "Position %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
1269 gst_query_set_position (query, format, pos);
1273 case GST_QUERY_DURATION:
1279 gst_query_parse_duration (query, &format, NULL);
1281 if (G_LIKELY (format == GST_FORMAT_TIME &&
1282 GST_CLOCK_TIME_IS_VALID (demux->src_segment.duration))) {
1283 gst_query_set_duration (query, GST_FORMAT_TIME,
1284 demux->src_segment.duration);
1289 if ((peer = gst_pad_get_peer (demux->sinkpad)) == NULL) {
1290 GST_DEBUG_OBJECT (demux, "duration not possible, no peer");
1294 /* For any format other than bytes, see if upstream knows first */
1295 if (format == GST_FORMAT_BYTES) {
1296 GST_DEBUG_OBJECT (demux, "duration not supported for format %d",
1298 gst_object_unref (peer);
1302 if (gst_pad_query (peer, query)) {
1303 gst_object_unref (peer);
1308 /* Upstream didn't know, so we can only answer TIME queries from
1310 if (format != GST_FORMAT_TIME) {
1311 GST_DEBUG_OBJECT (demux, "duration not supported for format %d",
1313 gst_object_unref (peer);
1317 if (demux->mux_rate == -1) {
1318 GST_DEBUG_OBJECT (demux, "duration not possible, no mux_rate");
1319 gst_object_unref (peer);
1323 gst_query_set_duration (query, GST_FORMAT_BYTES, -1);
1325 if (!gst_pad_query (peer, query)) {
1326 GST_LOG_OBJECT (demux, "query on peer pad failed");
1327 gst_object_unref (peer);
1330 gst_object_unref (peer);
1332 gst_query_parse_duration (query, &format, &duration);
1334 GST_LOG_OBJECT (demux,
1335 "query on peer pad reported bytes %" G_GUINT64_FORMAT, duration);
1337 duration = BYTES_TO_GSTTIME (duration);
1339 GST_LOG_OBJECT (demux, "converted to time %" GST_TIME_FORMAT,
1340 GST_TIME_ARGS (duration));
1342 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
1346 case GST_QUERY_SEEKING:{
1349 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
1352 if (demux->random_access) {
1353 /* In pull mode we can seek in TIME format if we have the SCR */
1354 if (fmt != GST_FORMAT_TIME || demux->scr_rate_n == G_MAXUINT64
1355 || demux->scr_rate_d == G_MAXUINT64)
1356 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1358 gst_query_set_seeking (query, fmt, TRUE, 0, -1);
1360 if (fmt == GST_FORMAT_BYTES) {
1361 /* Seeking in BYTES format not supported at all */
1362 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1364 GstQuery *peerquery;
1367 /* Then ask upstream */
1368 res = gst_pad_peer_query (demux->sinkpad, query);
1370 /* If upstream can handle seeks we're done, if it
1371 * can't we still have our TIME->BYTES conversion seek
1373 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
1374 if (seekable || fmt != GST_FORMAT_TIME)
1378 /* We can seek if upstream supports BYTES seeks and we
1381 peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
1382 res = gst_pad_peer_query (demux->sinkpad, peerquery);
1383 if (!res || demux->scr_rate_n == G_MAXUINT64
1384 || demux->scr_rate_d == G_MAXUINT64) {
1385 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1387 gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
1389 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, -1);
1391 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1394 gst_query_unref (peerquery);
1401 res = gst_pad_query_default (pad, query);
1406 gst_object_unref (demux);
1412 gst_object_unref (demux);
1419 gst_flups_demux_reset_psm (GstFluPSDemux * demux)
1423 #define FILL_TYPE(start, stop, type) \
1424 for (i=start; i <= stop; i++) \
1425 demux->psm[i] = type;
1427 FILL_TYPE (0x00, 0x1f, -1);
1428 FILL_TYPE (0x20, 0x3f, ST_PS_DVD_SUBPICTURE);
1429 FILL_TYPE (0x40, 0x7f, -1);
1430 FILL_TYPE (0x80, 0x87, ST_PS_AUDIO_AC3);
1431 FILL_TYPE (0x88, 0x9f, ST_PS_AUDIO_DTS);
1432 FILL_TYPE (0xa0, 0xaf, ST_PS_AUDIO_LPCM);
1433 FILL_TYPE (0xbd, 0xbd, -1);
1434 FILL_TYPE (0xc0, 0xdf, ST_AUDIO_MPEG1);
1435 FILL_TYPE (0xe0, 0xef, ST_GST_VIDEO_MPEG1_OR_2);
1436 FILL_TYPE (0xf0, 0xff, -1);
1443 * pack_start_code 32 bslbf -+
1445 * system_clock_reference_base [32..30] 3 bslbf |
1446 * marker_bit 1 bslbf |
1447 * system_clock_reference_base [29..15] 15 bslbf |
1448 * marker_bit 1 bslbf |
1449 * system_clock_reference_base [14..0] 15 bslbf |
1450 * marker_bit 1 bslbf | 112 bits
1451 * system_clock_reference_extension 9 ubslbf |
1452 * marker_bit 1 bslbf |
1453 * program_mux_rate 22 ubslbf |
1454 * marker_bit 1 bslbf |
1455 * marker_bit 1 bslbf |
1456 * reserved 5 bslbf |
1457 * pack_stuffing_length 3 ubslbf -+
1459 * for (i = 0; i < pack_stuffing_length; i++) {
1460 * stuffing_byte '1111 1111' 8 bslbf
1463 * 112 bits = 14 bytes, as max value for pack_stuffing_length is 7, then
1464 * in total it's needed 14 + 7 = 21 bytes.
1466 #define PACK_START_SIZE 21
1468 static GstFlowReturn
1469 gst_flups_demux_parse_pack_start (GstFluPSDemux * demux)
1474 guint64 scr, scr_adjusted, new_rate;
1477 guint avail = gst_adapter_available (demux->adapter);
1479 GST_DEBUG ("parsing pack start");
1481 if (G_UNLIKELY (avail < PACK_START_SIZE))
1482 goto need_more_data;
1484 data = gst_adapter_peek (demux->adapter, PACK_START_SIZE);
1486 /* skip start code */
1489 scr1 = GST_READ_UINT32_BE (data);
1490 scr2 = GST_READ_UINT32_BE (data + 4);
1492 /* fixed length to begin with, start code and two scr values */
1495 /* start parsing the stream */
1496 if ((*data & 0xc0) == 0x40) {
1499 guint8 stuffing_bytes;
1501 GST_DEBUG ("Found MPEG2 stream");
1502 demux->is_mpeg2_pack = TRUE;
1504 /* mpeg2 has more data */
1507 /* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
1510 if (G_UNLIKELY ((scr1 & 0xc4000400) != 0x44000400))
1513 scr = ((guint64) scr1 & 0x38000000) << 3;
1514 scr |= ((guint64) scr1 & 0x03fff800) << 4;
1515 scr |= ((guint64) scr1 & 0x000003ff) << 5;
1516 scr |= ((guint64) scr2 & 0xf8000000) >> 27;
1518 /* marker:1==1 ! scr_ext:9 ! marker:1==1 */
1519 if (G_UNLIKELY ((scr2 & 0x04010000) != 0x04010000))
1522 scr_ext = (scr2 & 0x03fe0000) >> 17;
1523 /* We keep the offset of this scr */
1524 demux->cur_scr_offset = demux->adapter_offset + 12;
1526 GST_DEBUG_OBJECT (demux, "SCR: 0x%08" G_GINT64_MODIFIER "x SCRE: 0x%08x",
1530 scr = (scr * 300 + scr_ext % 300) / 300;
1532 /* SCR has been converted into units of 90Khz ticks to make it comparable
1533 to DTS/PTS, that also implies 1 tick rounding error */
1535 /* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
1536 next32 = GST_READ_UINT32_BE (data);
1537 if (G_UNLIKELY ((next32 & 0x00000300) != 0x00000300))
1540 new_rate = (next32 & 0xfffffc00) >> 10;
1542 stuffing_bytes = (next32 & 0x07);
1543 GST_DEBUG_OBJECT (demux, "stuffing bytes: %d", stuffing_bytes);
1546 length += stuffing_bytes;
1547 while (stuffing_bytes--) {
1548 if (*data++ != 0xff)
1552 GST_DEBUG ("Found MPEG1 stream");
1553 demux->is_mpeg2_pack = FALSE;
1556 if (G_UNLIKELY ((scr1 & 0xf1000100) != 0x21000100))
1559 if (G_UNLIKELY ((scr2 & 0x01800001) != 0x01800001))
1562 /* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
1563 scr = ((guint64) scr1 & 0x0e000000) << 5;
1564 scr |= ((guint64) scr1 & 0x00fffe00) << 6;
1565 scr |= ((guint64) scr1 & 0x000000ff) << 7;
1566 scr |= ((guint64) scr2 & 0xfe000000) >> 25;
1568 /* We keep the offset of this scr */
1569 demux->cur_scr_offset = demux->adapter_offset + 8;
1571 /* marker:1==1 ! mux_rate:22 ! marker:1==1 */
1572 new_rate = (scr2 & 0x007ffffe) >> 1;
1576 new_rate *= MPEG_MUX_RATE_MULT;
1578 /* scr adjusted is the new scr found + the colected adjustment */
1579 scr_adjusted = scr + demux->scr_adjust;
1581 GST_LOG_OBJECT (demux,
1582 "SCR: %" G_GINT64_FORMAT " (%" G_GINT64_FORMAT "), mux_rate %"
1583 G_GINT64_FORMAT ", GStreamer Time:%" GST_TIME_FORMAT,
1584 scr, scr_adjusted, new_rate,
1585 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME ((guint64) scr)));
1587 /* keep the first src in order to calculate delta time */
1588 if (G_UNLIKELY (demux->first_scr == G_MAXUINT64)) {
1589 demux->first_scr = scr;
1590 demux->first_scr_offset = demux->cur_scr_offset;
1591 demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
1592 /* at begin consider the new_rate as the scr rate, bytes/clock ticks */
1593 scr_rate_n = new_rate;
1594 scr_rate_d = CLOCK_FREQ;
1595 } else if (G_LIKELY (demux->first_scr_offset != demux->cur_scr_offset)) {
1596 /* estimate byte rate related to the SCR */
1597 scr_rate_n = demux->cur_scr_offset - demux->first_scr_offset;
1598 scr_rate_d = scr_adjusted - demux->first_scr;
1600 scr_rate_n = demux->scr_rate_n;
1601 scr_rate_d = demux->scr_rate_d;
1604 GST_DEBUG_OBJECT (demux, "%s mode scr: %" G_GUINT64_FORMAT " at %"
1605 G_GUINT64_FORMAT ", first scr: %" G_GUINT64_FORMAT
1606 " at %" G_GUINT64_FORMAT ", scr rate: %" G_GUINT64_FORMAT
1607 "/%" G_GUINT64_FORMAT "(%f)",
1608 ((demux->sink_segment.rate >= 0.0) ? "forward" : "backward"),
1609 scr, demux->cur_scr_offset,
1610 demux->first_scr, demux->first_scr_offset,
1611 scr_rate_n, scr_rate_d, (float) scr_rate_n / scr_rate_d);
1613 /* adjustment of the SCR */
1614 if (G_LIKELY (demux->current_scr != G_MAXUINT64)) {
1616 guint64 old_scr, old_mux_rate, bss, adjust = 0;
1618 /* keep SCR of the previous packet */
1619 old_scr = demux->current_scr;
1620 old_mux_rate = demux->mux_rate;
1622 /* Bytes since SCR is the amount we placed in the adapter since then
1623 * (demux->bytes_since_scr) minus the amount remaining in the adapter,
1624 * clamped to >= 0 */
1625 bss = MAX (0, (gint) (demux->bytes_since_scr - avail));
1627 /* estimate the new SCR using the previous one according the notes
1628 on point 2.5.2.2 of the ISO/IEC 13818-1 document */
1629 if (old_mux_rate != 0)
1630 adjust = (bss * CLOCK_FREQ) / old_mux_rate;
1632 if (demux->sink_segment.rate >= 0.0)
1633 demux->next_scr = old_scr + adjust;
1635 demux->next_scr = old_scr - adjust;
1637 GST_DEBUG_OBJECT (demux,
1638 "bss: %" G_GUINT64_FORMAT ", next_scr: %" G_GUINT64_FORMAT
1639 ", old_scr: %" G_GUINT64_FORMAT ", scr: %" G_GUINT64_FORMAT,
1640 bss, demux->next_scr, old_scr, scr_adjusted);
1642 /* calculate the absolute deference between the last scr and
1644 if (G_UNLIKELY (old_scr > scr_adjusted))
1645 diff = old_scr - scr_adjusted;
1647 diff = scr_adjusted - old_scr;
1649 /* if the difference is more than 1 second we need to reconfigure
1651 if (G_UNLIKELY (diff > CLOCK_FREQ)) {
1652 demux->scr_adjust = demux->next_scr - scr;
1653 GST_DEBUG_OBJECT (demux, "discont found, diff: %" G_GINT64_FORMAT
1654 ", adjust %" G_GINT64_FORMAT, diff, demux->scr_adjust);
1655 scr_adjusted = demux->next_scr;
1656 /* don't update rate estimation on disconts */
1657 scr_rate_n = demux->scr_rate_n;
1658 scr_rate_d = demux->scr_rate_d;
1660 demux->next_scr = scr_adjusted;
1664 /* update the current_scr and rate members */
1665 demux->mux_rate = new_rate;
1666 demux->current_scr = scr_adjusted;
1667 demux->scr_rate_n = scr_rate_n;
1668 demux->scr_rate_d = scr_rate_d;
1670 /* Reset the bytes_since_scr value to count the data remaining in the
1672 demux->bytes_since_scr = avail;
1674 gst_adapter_flush (demux->adapter, length);
1675 ADAPTER_OFFSET_FLUSH (length);
1680 GST_DEBUG_OBJECT (demux, "lost sync");
1681 return GST_FLOW_LOST_SYNC;
1685 GST_DEBUG_OBJECT (demux, "need more data");
1686 return GST_FLOW_NEED_MORE_DATA;
1691 * system_header () {
1692 * system_header_start_code 32 bslbf -+
1693 * header_length 16 uimsbf |
1694 * marker_bit 1 bslbf |
1695 * rate_bound 22 uimsbf |
1696 * marker_bit 1 bslbf |
1697 * audio_bound 6 uimsbf |
1698 * fixed_flag 1 bslbf |
1699 * CSPS_flag 1 bslbf | 96 bits
1700 * system_audio_lock_flag 1 bslbf |
1701 * system_video_lock_flag 1 bslbf |
1702 * marker_bit 1 bslbf |
1703 * video_bound 5 uimsbf |
1704 * packet_rate_restriction_flag 1 bslbf |
1705 * reserved_bits 7 bslbf -+
1706 * while (nextbits () = = '1') {
1707 * stream_id 8 uimsbf -+
1708 * '11' 2 bslbf | 24 bits
1709 * P-STD_buffer_bound_scale 1 bslbf |
1710 * P-STD_buffer_size_bound 13 uimsbf -+
1713 * 96 bits = 12 bytes, 24 bits = 3 bytes.
1716 static GstFlowReturn
1717 gst_flups_demux_parse_sys_head (GstFluPSDemux * demux)
1723 /* start code + length */
1724 if (!(data = gst_adapter_peek (demux->adapter, 6)))
1725 goto need_more_data;
1727 /* skip start code */
1730 length = GST_READ_UINT16_BE (data);
1731 GST_DEBUG_OBJECT (demux, "length %d", length);
1735 if (!(data = gst_adapter_peek (demux->adapter, length)))
1736 goto need_more_data;
1738 /* skip start code and length */
1741 /* marker:1==1 ! rate_bound:22 | marker:1==1 */
1742 if ((*data & 0x80) != 0x80)
1743 goto marker_expected;
1748 if ((data[2] & 0x01) != 0x01)
1749 goto marker_expected;
1751 rate_bound = ((guint32) data[0] & 0x7f) << 15;
1752 rate_bound |= ((guint32) data[1]) << 7;
1753 rate_bound |= ((guint32) data[2] & 0xfe) >> 1;
1754 rate_bound *= MPEG_MUX_RATE_MULT;
1756 GST_DEBUG_OBJECT (demux, "rate bound %u", rate_bound);
1761 /* audio_bound:6==1 ! fixed:1 | constrained:1 */
1766 /* max number of simultaneous audio streams active */
1767 audio_bound = (data[0] & 0xfc) >> 2;
1768 /* fixed or variable bitrate */
1769 fixed = (data[0] & 0x02) == 0x02;
1770 /* meeting constraints */
1771 csps = (data[0] & 0x01) == 0x01;
1773 GST_DEBUG_OBJECT (demux, "audio_bound %d, fixed %d, constrained %d",
1774 audio_bound, fixed, csps);
1778 /* audio_lock:1 | video_lock:1 | marker:1==1 | video_bound:5 */
1780 gboolean audio_lock;
1781 gboolean video_lock;
1784 audio_lock = (data[0] & 0x80) == 0x80;
1785 video_lock = (data[0] & 0x40) == 0x40;
1787 if ((data[0] & 0x20) != 0x20)
1788 goto marker_expected;
1790 /* max number of simultaneous video streams active */
1791 video_bound = (data[0] & 0x1f);
1793 GST_DEBUG_OBJECT (demux, "audio_lock %d, video_lock %d, video_bound %d",
1794 audio_lock, video_lock, video_bound);
1798 /* packet_rate_restriction:1 | reserved:7==0x7F */
1800 gboolean packet_rate_restriction;
1802 if ((data[0] & 0x7f) != 0x7f)
1803 goto marker_expected;
1805 /* only valid if csps is set */
1807 packet_rate_restriction = (data[0] & 0x80) == 0x80;
1809 GST_DEBUG_OBJECT (demux, "packet_rate_restriction %d",
1810 packet_rate_restriction);
1816 gint stream_count = (length - 12) / 3;
1819 GST_DEBUG_OBJECT (demux, "number of streams: %d ", stream_count);
1821 for (i = 0; i < stream_count; i++) {
1823 gboolean STD_buffer_bound_scale;
1824 guint16 STD_buffer_size_bound;
1825 guint32 buf_byte_size_bound;
1827 stream_id = *data++;
1828 if (!(stream_id & 0x80))
1831 /* check marker bits */
1832 if ((*data & 0xC0) != 0xC0)
1833 goto no_placeholder_bits;
1835 STD_buffer_bound_scale = *data & 0x20;
1836 STD_buffer_size_bound = ((guint16) (*data++ & 0x1F)) << 8;
1837 STD_buffer_size_bound |= *data++;
1839 if (STD_buffer_bound_scale == 0) {
1840 buf_byte_size_bound = STD_buffer_size_bound * 128;
1842 buf_byte_size_bound = STD_buffer_size_bound * 1024;
1845 GST_DEBUG_OBJECT (demux, "STD_buffer_bound_scale %d",
1846 STD_buffer_bound_scale);
1847 GST_DEBUG_OBJECT (demux, "STD_buffer_size_bound %d or %d bytes",
1848 STD_buffer_size_bound, buf_byte_size_bound);
1852 gst_adapter_flush (demux->adapter, length);
1853 ADAPTER_OFFSET_FLUSH (length);
1859 GST_DEBUG_OBJECT (demux, "expecting marker");
1860 return GST_FLOW_LOST_SYNC;
1862 no_placeholder_bits:
1864 GST_DEBUG_OBJECT (demux, "expecting placeholder bit values"
1865 " '11' after stream id");
1866 return GST_FLOW_LOST_SYNC;
1870 GST_DEBUG_OBJECT (demux, "error in system header length");
1871 return GST_FLOW_LOST_SYNC;
1875 GST_DEBUG_OBJECT (demux, "need more data");
1876 return GST_FLOW_NEED_MORE_DATA;
1880 static GstFlowReturn
1881 gst_flups_demux_parse_psm (GstFluPSDemux * demux)
1883 guint16 length = 0, info_length = 0, es_map_length = 0;
1884 guint8 psm_version = 0;
1885 const guint8 *data, *es_map_base;
1886 gboolean applicable;
1888 /* start code + length */
1889 if (!(data = gst_adapter_peek (demux->adapter, 6)))
1890 goto need_more_data;
1892 /* skip start code */
1895 length = GST_READ_UINT16_BE (data);
1896 GST_DEBUG_OBJECT (demux, "length %u", length);
1898 if (G_UNLIKELY (length > 0x3FA))
1903 if (!(data = gst_adapter_peek (demux->adapter, length)))
1904 goto need_more_data;
1906 /* skip start code and length */
1909 /* Read PSM applicable bit together with version */
1910 psm_version = GST_READ_UINT8 (data);
1911 applicable = (psm_version & 0x80) >> 7;
1912 psm_version &= 0x1F;
1913 GST_DEBUG_OBJECT (demux, "PSM version %u (applicable now %u)", psm_version,
1916 /* Jump over version and marker bit */
1919 /* Read PS info length */
1920 info_length = GST_READ_UINT16_BE (data);
1921 /* Cap it to PSM length - needed bytes for ES map length and CRC */
1922 info_length = MIN (length - 16, info_length);
1923 GST_DEBUG_OBJECT (demux, "PS info length %u bytes", info_length);
1925 /* Jump over that section */
1926 data += (2 + info_length);
1928 /* Read ES map length */
1929 es_map_length = GST_READ_UINT16_BE (data);
1930 /* Cap it to PSM remaining length - CRC */
1931 es_map_length = MIN (length - (16 + info_length), es_map_length);
1932 GST_DEBUG_OBJECT (demux, "ES map length %u bytes", es_map_length);
1934 /* Jump over the size */
1937 /* Now read the ES map */
1939 while (es_map_base + 4 <= data + es_map_length) {
1940 guint8 stream_type = 0, stream_id = 0;
1941 guint16 stream_info_length = 0;
1943 stream_type = GST_READ_UINT8 (es_map_base);
1945 stream_id = GST_READ_UINT8 (es_map_base);
1947 stream_info_length = GST_READ_UINT16_BE (es_map_base);
1949 /* Cap stream_info_length */
1950 stream_info_length = MIN (data + es_map_length - es_map_base,
1951 stream_info_length);
1953 GST_DEBUG_OBJECT (demux, "Stream type %02X with id %02X and %u bytes info",
1954 stream_type, stream_id, stream_info_length);
1955 if (G_LIKELY (stream_id != 0xbd))
1956 demux->psm[stream_id] = stream_type;
1958 /* Ignore stream type for private_stream_1 and discover it looking at
1960 * Fixes demuxing some clips with lpcm that was wrongly declared as
1962 GST_DEBUG_OBJECT (demux, "stream type for private_stream_1 ignored");
1964 es_map_base += stream_info_length;
1967 gst_adapter_flush (demux->adapter, length);
1968 ADAPTER_OFFSET_FLUSH (length);
1973 GST_DEBUG_OBJECT (demux, "error in PSM length");
1974 return GST_FLOW_LOST_SYNC;
1978 GST_DEBUG_OBJECT (demux, "need more data");
1979 return GST_FLOW_NEED_MORE_DATA;
1984 gst_flups_demux_resync_cb (GstPESFilter * filter, GstFluPSDemux * demux)
1988 static GstFlowReturn
1989 gst_flups_demux_data_cb (GstPESFilter * filter, gboolean first,
1990 GstBuffer * buffer, GstFluPSDemux * demux)
1993 GstFlowReturn ret = GST_FLOW_OK;
2001 data = GST_BUFFER_DATA (buffer);
2002 datalen = GST_BUFFER_SIZE (buffer);
2004 start_code = filter->start_code;
2008 /* find the stream type */
2009 stream_type = demux->psm[id];
2010 if (stream_type == -1) {
2011 /* no stream type, if PS1, get the new id */
2012 if (start_code == ID_PRIVATE_STREAM_1 && datalen >= 2) {
2015 /* VDR writes A52 streams without any header bytes
2016 * (see ftp://ftp.mplayerhq.hu/MPlayer/samples/MPEG-VOB/vdr-AC3) */
2018 guint hdr = GST_READ_UINT32_BE (data);
2020 if (G_UNLIKELY ((hdr & 0xffff0000) == AC3_SYNC_WORD)) {
2022 stream_type = demux->psm[id] = ST_GST_AUDIO_RAWA52;
2023 GST_DEBUG_OBJECT (demux, "Found VDR raw A52 stream");
2027 if (G_LIKELY (stream_type == -1)) {
2028 /* new id is in the first byte */
2029 id = data[offset++];
2033 stream_type = demux->psm[id];
2035 /* Now, if it's a subpicture stream - no more, otherwise
2036 * take the first byte too, since it's the frame count in audio
2037 * streams and our backwards compat convention is to strip it off */
2038 if (stream_type != ST_PS_DVD_SUBPICTURE) {
2039 /* Number of audio frames in this packet */
2040 nframes = data[offset++];
2042 GST_DEBUG_OBJECT (demux, "private type 0x%02x, %d frames", id,
2045 GST_DEBUG_OBJECT (demux, "private type 0x%02x, stream type %d", id,
2050 if (stream_type == -1)
2051 goto unknown_stream_type;
2053 if (filter->pts != -1) {
2054 demux->next_pts = filter->pts + demux->scr_adjust;
2055 GST_DEBUG_OBJECT (demux, "PTS = %" G_GUINT64_FORMAT
2056 "(%" G_GUINT64_FORMAT ")", filter->pts, demux->next_pts);
2058 demux->next_pts = G_MAXUINT64;
2060 if (filter->dts != -1) {
2061 demux->next_dts = filter->dts + demux->scr_adjust;
2063 demux->next_dts = demux->next_pts;
2065 GST_DEBUG_OBJECT (demux, "DTS = orig %" G_GUINT64_FORMAT
2066 " (%" G_GUINT64_FORMAT ")", filter->dts, demux->next_dts);
2068 demux->current_stream = gst_flups_demux_get_stream (demux, id, stream_type);
2071 if (G_UNLIKELY (demux->current_stream == NULL)) {
2072 GST_DEBUG_OBJECT (demux, "Dropping buffer for unknown stream id 0x%02x",
2077 /* After 2 seconds of bitstream emit no more pads */
2078 if (demux->need_no_more_pads
2079 && (demux->current_scr - demux->first_scr) > 2 * CLOCK_FREQ) {
2080 GST_DEBUG_OBJECT (demux, "no more pads, notifying");
2081 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
2082 demux->need_no_more_pads = FALSE;
2085 /* If the stream is not-linked, don't bother creating a sub-buffer
2086 * to send to it, unless we're processing a discont (which resets
2087 * the not-linked status and tries again */
2088 if (demux->current_stream->discont) {
2089 GST_DEBUG_OBJECT (demux, "stream is discont");
2090 demux->current_stream->notlinked = FALSE;
2093 if (demux->current_stream->notlinked == FALSE) {
2094 out_buf = gst_buffer_create_sub (buffer, offset, datalen);
2096 ret = gst_flups_demux_send_data (demux, demux->current_stream, out_buf);
2097 if (ret == GST_FLOW_NOT_LINKED) {
2098 demux->current_stream->notlinked = TRUE;
2104 gst_buffer_unref (buffer);
2109 unknown_stream_type:
2111 GST_DEBUG_OBJECT (demux, "unknown stream type %02x", id);
2118 gst_flups_demux_resync (GstFluPSDemux * demux, gboolean save)
2126 avail = gst_adapter_available (demux->adapter);
2127 if (G_UNLIKELY (avail < 4))
2130 /* Common case, read 4 bytes an check it */
2131 data = gst_adapter_peek (demux->adapter, 4);
2133 /* read currect code */
2134 code = GST_READ_UINT32_BE (data);
2136 /* The common case is that the sync code is at 0 bytes offset */
2137 if (G_LIKELY ((code & 0xffffff00) == 0x100L)) {
2138 GST_LOG_OBJECT (demux, "Found resync code %08x after 0 bytes", code);
2139 demux->last_sync_code = code;
2143 /* Otherwise, we are starting at byte 4 and we need to search
2144 the sync code in all available data in the adapter */
2146 if (offset >= avail)
2147 goto need_data; /* Not enough data to find sync */
2149 data = gst_adapter_peek (demux->adapter, avail);
2152 code = (code << 8) | data[offset++];
2153 found = (code & 0xffffff00) == 0x100L;
2154 } while (offset < avail && !found);
2156 if (!save || demux->sink_segment.rate >= 0.0) {
2157 GST_LOG_OBJECT (demux, "flushing %d bytes", offset - 4);
2158 /* forward playback, we can discard and flush the skipped bytes */
2159 gst_adapter_flush (demux->adapter, offset - 4);
2160 ADAPTER_OFFSET_FLUSH (offset - 4);
2163 GST_LOG_OBJECT (demux, "reverse saving %d bytes", offset - 4);
2164 /* reverse playback, we keep the flushed bytes and we will append them to
2165 * the next buffer in the chain function, which is the previous buffer in
2167 gst_adapter_push (demux->rev_adapter,
2168 gst_adapter_take_buffer (demux->adapter, offset - 4));
2170 GST_LOG_OBJECT (demux, "reverse saving %d bytes", avail);
2171 /* nothing found, keep all bytes */
2172 gst_adapter_push (demux->rev_adapter,
2173 gst_adapter_take_buffer (demux->adapter, avail));
2178 GST_LOG_OBJECT (demux, "Found resync code %08x after %d bytes",
2180 demux->last_sync_code = code;
2182 GST_LOG_OBJECT (demux, "No resync after skipping %d", offset);
2189 GST_LOG_OBJECT (demux, "we need more data for resync %d", avail);
2194 static inline gboolean
2195 gst_flups_demux_is_pes_sync (guint32 sync)
2197 return ((sync & 0xfc) == 0xbc) ||
2198 ((sync & 0xe0) == 0xc0) || ((sync & 0xf0) == 0xe0);
2201 static inline gboolean
2202 gst_flups_demux_scan_ts (GstFluPSDemux * demux, const guint8 * data,
2203 SCAN_MODE mode, guint64 * rts)
2205 gboolean ret = FALSE;
2211 /* read the 4 bytes for the sync code */
2212 code = GST_READ_UINT32_BE (data);
2213 if (G_LIKELY (code != ID_PS_PACK_START_CODE))
2216 /* skip start code */
2219 scr1 = GST_READ_UINT32_BE (data);
2220 scr2 = GST_READ_UINT32_BE (data + 4);
2222 /* start parsing the stream */
2223 if ((*data & 0xc0) == 0x40) {
2226 guint8 stuffing_bytes;
2228 /* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
2231 if ((scr1 & 0xc4000400) != 0x44000400)
2234 scr = ((guint64) scr1 & 0x38000000) << 3;
2235 scr |= ((guint64) scr1 & 0x03fff800) << 4;
2236 scr |= ((guint64) scr1 & 0x000003ff) << 5;
2237 scr |= ((guint64) scr2 & 0xf8000000) >> 27;
2239 /* marker:1==1 ! scr_ext:9 ! marker:1==1 */
2240 if ((scr2 & 0x04010000) != 0x04010000)
2243 scr_ext = (scr2 & 0x03fe0000) >> 17;
2246 scr = (scr * 300 + scr_ext % 300) / 300;
2248 /* SCR has been converted into units of 90Khz ticks to make it comparable
2249 to DTS/PTS, that also implies 1 tick rounding error */
2251 /* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
2252 next32 = GST_READ_UINT32_BE (data);
2253 if ((next32 & 0x00000300) != 0x00000300)
2256 stuffing_bytes = (next32 & 0x07);
2258 while (stuffing_bytes--) {
2259 if (*data++ != 0xff)
2264 if ((scr1 & 0xf1000100) != 0x21000100)
2267 if ((scr2 & 0x01800001) != 0x01800001)
2270 /* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
2271 scr = ((guint64) scr1 & 0x0e000000) << 5;
2272 scr |= ((guint64) scr1 & 0x00fffe00) << 6;
2273 scr |= ((guint64) scr1 & 0x000000ff) << 7;
2274 scr |= ((guint64) scr2 & 0xfe000000) >> 25;
2278 if (mode == SCAN_SCR) {
2283 /* read the 4 bytes for the PES sync code */
2284 code = GST_READ_UINT32_BE (data);
2285 if (!gst_flups_demux_is_pes_sync (code))
2289 case ID_PS_PROGRAM_STREAM_MAP:
2290 case ID_PRIVATE_STREAM_2:
2293 case ID_PROGRAM_STREAM_DIRECTORY:
2294 case ID_DSMCC_STREAM:
2295 case ID_ITU_TREC_H222_TYPE_E_STREAM:
2296 case ID_PADDING_STREAM:
2302 /* skip sync code and size */
2307 /* stuffing bits, first two bits are '10' for mpeg2 pes so this code is
2315 /* STD buffer size, never for mpeg2 */
2316 if ((*data & 0xc0) == 0x40)
2319 /* PTS but no DTS, never for mpeg2 */
2320 if ((*data & 0xf0) == 0x20) {
2321 READ_TS (data, pts, beach);
2323 /* PTS and DTS, never for mpeg2 */
2324 else if ((*data & 0xf0) == 0x30) {
2325 READ_TS (data, pts, beach);
2326 READ_TS (data, dts, beach);
2327 } else if ((*data & 0xc0) == 0x80) {
2332 * 2: PES_scrambling_control
2334 * 1: data_alignment_indicator
2336 * 1: original_or_copy
2340 if ((flags & 0xc0) != 0x80)
2346 * 1: DSM_trick_mode_flag
2347 * 1: additional_copy_info_flag
2349 * 1: PES_extension_flag
2353 /* 8: PES_header_data_length */
2356 /* only DTS: this is invalid */
2357 if ((flags & 0xc0) == 0x40)
2361 if ((flags & 0x80)) {
2362 READ_TS (data, pts, beach);
2365 if ((flags & 0x40)) {
2366 READ_TS (data, dts, beach);
2370 if (mode == SCAN_DTS && dts != -1) {
2375 if (mode == SCAN_PTS && pts != -1) {
2383 static inline gboolean
2384 gst_flups_demux_scan_forward_ts (GstFluPSDemux * demux, guint64 * pos,
2385 SCAN_MODE mode, guint64 * rts)
2387 GstFlowReturn ret = GST_FLOW_OK;
2388 GstBuffer *buffer = NULL;
2389 guint64 offset = *pos;
2390 gboolean found = FALSE;
2392 guint scan_sz = (mode == SCAN_SCR ? SCAN_SCR_SZ : SCAN_PTS_SZ);
2393 guint cursor, to_read = BLOCK_SZ;
2398 if (offset + scan_sz > demux->sink_segment.stop)
2401 if (offset + to_read > demux->sink_segment.stop)
2402 to_read = demux->sink_segment.stop - offset;
2404 /* read some data */
2405 ret = gst_pad_pull_range (demux->sinkpad, offset, to_read, &buffer);
2406 if (G_UNLIKELY (ret != GST_FLOW_OK))
2409 data = GST_BUFFER_DATA (buffer);
2410 end_scan = GST_BUFFER_SIZE (buffer) - scan_sz;
2411 /* scan the block */
2412 for (cursor = 0; !found && cursor <= end_scan; cursor++) {
2413 found = gst_flups_demux_scan_ts (demux, data++, mode, &ts);
2416 /* done with the buffer, unref it */
2417 gst_buffer_unref (buffer);
2421 *pos = offset + cursor - 1;
2425 } while (!found && offset < demux->sink_segment.stop);
2430 static inline gboolean
2431 gst_flups_demux_scan_backward_ts (GstFluPSDemux * demux, guint64 * pos,
2432 SCAN_MODE mode, guint64 * rts)
2434 GstFlowReturn ret = GST_FLOW_OK;
2435 GstBuffer *buffer = NULL;
2436 guint64 offset = *pos;
2437 gboolean found = FALSE;
2439 guint scan_sz = (mode == SCAN_SCR ? SCAN_SCR_SZ : SCAN_PTS_SZ);
2440 guint cursor, to_read = BLOCK_SZ;
2445 if (offset < scan_sz - 1)
2448 if (offset > BLOCK_SZ)
2451 to_read = offset + 1;
2454 /* read some data */
2455 ret = gst_pad_pull_range (demux->sinkpad, offset, to_read, &buffer);
2456 if (G_UNLIKELY (ret != GST_FLOW_OK))
2459 start_scan = GST_BUFFER_SIZE (buffer) - scan_sz;
2460 data = GST_BUFFER_DATA (buffer) + start_scan;
2461 /* scan the block */
2462 for (cursor = (start_scan + 1); !found && cursor > 0; cursor--) {
2463 found = gst_flups_demux_scan_ts (demux, data--, mode, &ts);
2466 /* done with the buffer, unref it */
2467 gst_buffer_unref (buffer);
2471 *pos = offset + cursor;
2474 } while (!found && offset > 0);
2479 static inline gboolean
2480 gst_flups_sink_get_duration (GstFluPSDemux * demux)
2482 gboolean res = FALSE;
2484 GstFormat format = GST_FORMAT_BYTES;
2490 /* init the sink segment */
2491 gst_segment_init (&demux->sink_segment, format);
2493 /* get peer to figure out length */
2494 if ((peer = gst_pad_get_peer (demux->sinkpad)) == NULL)
2497 res = gst_pad_query_duration (peer, &format, &length);
2498 gst_object_unref (peer);
2500 if (!res || length <= 0)
2503 GST_DEBUG_OBJECT (demux, "file length %" G_GINT64_FORMAT, length);
2505 /* update the sink segment */
2506 demux->sink_segment.stop = length;
2507 gst_segment_set_duration (&demux->sink_segment, format, length);
2508 gst_segment_set_last_stop (&demux->sink_segment, format, 0);
2510 /* Scan for notorious SCR and PTS to calculate the duration */
2511 /* scan for first SCR in the stream */
2512 offset = demux->sink_segment.start;
2513 gst_flups_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &demux->first_scr);
2514 GST_DEBUG_OBJECT (demux, "First SCR: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2515 " in packet starting at %" G_GUINT64_FORMAT,
2516 demux->first_scr, GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_scr)),
2518 demux->first_scr_offset = offset;
2519 /* scan for last SCR in the stream */
2520 offset = demux->sink_segment.stop;
2521 gst_flups_demux_scan_backward_ts (demux, &offset, SCAN_SCR, &demux->last_scr);
2522 GST_DEBUG_OBJECT (demux, "Last SCR: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2523 " in packet starting at %" G_GUINT64_FORMAT,
2524 demux->last_scr, GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->last_scr)),
2526 demux->last_scr_offset = offset;
2527 /* scan for first PTS in the stream */
2528 offset = demux->sink_segment.start;
2529 gst_flups_demux_scan_forward_ts (demux, &offset, SCAN_PTS, &demux->first_pts);
2530 GST_DEBUG_OBJECT (demux, "First PTS: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2531 " in packet starting at %" G_GUINT64_FORMAT,
2532 demux->first_pts, GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_pts)),
2534 if (demux->first_pts != G_MAXUINT64) {
2535 /* scan for last PTS in the stream */
2536 offset = demux->sink_segment.stop;
2537 gst_flups_demux_scan_backward_ts (demux, &offset, SCAN_PTS,
2539 GST_DEBUG_OBJECT (demux,
2540 "Last PTS: %" G_GINT64_FORMAT " %" GST_TIME_FORMAT
2541 " in packet starting at %" G_GUINT64_FORMAT, demux->last_pts,
2542 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->last_pts)), offset);
2544 /* Detect wrong SCR values */
2545 if (demux->first_scr > demux->last_scr) {
2546 GST_DEBUG_OBJECT (demux, "Wrong SCR values detected, searching for "
2547 "a better first SCR value");
2548 offset = demux->first_scr_offset;
2549 for (i = 0; i < 10; i++) {
2551 gst_flups_demux_scan_forward_ts (demux, &offset, SCAN_SCR, &scr);
2552 if (scr < demux->last_scr) {
2553 demux->first_scr = scr;
2554 demux->first_scr_offset = offset;
2555 /* Start demuxing from the right place */
2556 demux->sink_segment.last_stop = offset;
2557 GST_DEBUG_OBJECT (demux, "Replaced First SCR: %" G_GINT64_FORMAT
2558 " %" GST_TIME_FORMAT " in packet starting at %" G_GUINT64_FORMAT,
2560 GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->first_scr)), offset);
2565 /* Set the base_time and avg rate */
2566 demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
2567 demux->scr_rate_n = demux->last_scr_offset - demux->first_scr_offset;
2568 demux->scr_rate_d = demux->last_scr - demux->first_scr;
2570 if (G_LIKELY (demux->first_pts != G_MAXUINT64 &&
2571 demux->last_pts != G_MAXUINT64)) {
2572 /* update the src segment */
2573 demux->src_segment.start =
2574 MPEGTIME_TO_GSTTIME (demux->first_pts) - demux->base_time;
2575 demux->src_segment.stop = -1;
2576 gst_segment_set_duration (&demux->src_segment, GST_FORMAT_TIME,
2577 MPEGTIME_TO_GSTTIME (demux->last_pts - demux->first_pts));
2578 gst_segment_set_last_stop (&demux->src_segment, GST_FORMAT_TIME,
2579 demux->src_segment.start);
2581 GST_INFO_OBJECT (demux, "sink segment configured %" GST_SEGMENT_FORMAT,
2582 &demux->sink_segment);
2583 GST_INFO_OBJECT (demux, "src segment configured %" GST_SEGMENT_FORMAT,
2584 &demux->src_segment);
2592 static inline GstFlowReturn
2593 gst_flups_demux_pull_block (GstPad * pad, GstFluPSDemux * demux,
2594 guint64 offset, guint size)
2596 GstFlowReturn ret = GST_FLOW_OK;
2598 ret = gst_pad_pull_range (pad, offset, size, &buffer);
2599 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2600 GST_DEBUG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
2601 " size %u failed", offset, size);
2604 GST_LOG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
2605 " size %u done", offset, size);
2607 if (demux->sink_segment.rate < 0) {
2608 GST_LOG_OBJECT (demux, "setting discont flag on backward rate");
2609 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2611 ret = gst_flups_demux_chain (pad, buffer);
2618 gst_flups_demux_loop (GstPad * pad)
2620 GstFluPSDemux *demux;
2621 GstFlowReturn ret = GST_FLOW_OK;
2624 demux = GST_FLUPS_DEMUX (gst_pad_get_parent (pad));
2626 if (G_UNLIKELY (demux->flushing)) {
2627 ret = GST_FLOW_WRONG_STATE;
2631 if (G_UNLIKELY (demux->sink_segment.format == GST_FORMAT_UNDEFINED))
2632 gst_flups_sink_get_duration (demux);
2634 offset = demux->sink_segment.last_stop;
2635 if (demux->sink_segment.rate >= 0) {
2636 guint size = BLOCK_SZ;
2637 if (G_LIKELY (demux->sink_segment.stop != -1)) {
2638 size = MIN (size, demux->sink_segment.stop - offset);
2641 ret = gst_flups_demux_pull_block (pad, demux, offset, size);
2643 /* pause if something went wrong */
2644 if (G_UNLIKELY (ret != GST_FLOW_OK))
2647 /* update our position */
2649 gst_segment_set_last_stop (&demux->sink_segment, GST_FORMAT_BYTES, offset);
2651 /* check EOS condition */
2652 if ((demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) &&
2653 ((demux->sink_segment.last_stop >= demux->sink_segment.stop) ||
2654 (demux->src_segment.stop != -1 &&
2655 demux->src_segment.last_stop >= demux->src_segment.stop))) {
2656 GST_DEBUG_OBJECT (demux, "forward mode using segment reached end of "
2657 "segment pos %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
2658 " pos in bytes %" G_GUINT64_FORMAT " stop in bytes %"
2659 G_GUINT64_FORMAT, GST_TIME_ARGS (demux->src_segment.last_stop),
2660 GST_TIME_ARGS (demux->src_segment.stop),
2661 demux->sink_segment.last_stop, demux->sink_segment.stop);
2662 ret = GST_FLOW_UNEXPECTED;
2665 } else { /* Reverse playback */
2666 guint64 size = MIN (offset, BLOCK_SZ);
2669 ret = gst_flups_demux_pull_block (pad, demux, offset - size, size);
2671 /* pause if something went wrong */
2672 if (G_UNLIKELY (ret != GST_FLOW_OK))
2675 /* update our position */
2677 gst_segment_set_last_stop (&demux->sink_segment, GST_FORMAT_BYTES, offset);
2679 /* check EOS condition */
2680 if (demux->sink_segment.last_stop <= demux->sink_segment.start ||
2681 demux->src_segment.last_stop <= demux->src_segment.start) {
2682 GST_DEBUG_OBJECT (demux, "reverse mode using segment reached end of "
2683 "segment pos %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
2684 " pos in bytes %" G_GUINT64_FORMAT " stop in bytes %"
2685 G_GUINT64_FORMAT, GST_TIME_ARGS (demux->src_segment.last_stop),
2686 GST_TIME_ARGS (demux->src_segment.start),
2687 demux->sink_segment.last_stop, demux->sink_segment.start);
2688 ret = GST_FLOW_UNEXPECTED;
2693 gst_object_unref (demux);
2699 const gchar *reason = gst_flow_get_name (ret);
2701 GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
2702 gst_pad_pause_task (pad);
2704 if (ret == GST_FLOW_UNEXPECTED) {
2705 /* perform EOS logic */
2706 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
2707 if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
2710 /* for segment playback we need to post when (in stream time)
2711 * we stopped, this is either stop (when set) or the duration. */
2712 if ((stop = demux->src_segment.stop) == -1)
2713 stop = demux->src_segment.duration;
2715 if (demux->sink_segment.rate >= 0) {
2716 GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
2717 gst_element_post_message (GST_ELEMENT_CAST (demux),
2718 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
2719 GST_FORMAT_TIME, stop));
2720 } else { /* Reverse playback */
2721 GST_LOG_OBJECT (demux, "Sending segment done, at beginning of "
2723 gst_element_post_message (GST_ELEMENT_CAST (demux),
2724 gst_message_new_segment_done (GST_OBJECT_CAST (demux),
2725 GST_FORMAT_TIME, demux->src_segment.start));
2728 /* normal playback, send EOS to all linked pads */
2729 gst_element_no_more_pads (GST_ELEMENT (demux));
2730 GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
2731 if (!gst_flups_demux_send_event (demux, gst_event_new_eos ())
2732 && !have_open_streams (demux)) {
2733 GST_WARNING_OBJECT (demux, "EOS and no streams open");
2734 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
2735 ("Internal data stream error."), ("No valid streams detected"));
2738 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2739 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
2740 ("Internal data stream error."),
2741 ("stream stopped, reason %s", reason));
2742 gst_flups_demux_send_event (demux, gst_event_new_eos ());
2745 gst_object_unref (demux);
2750 /* If we can pull that's prefered */
2752 gst_flups_demux_sink_activate (GstPad * sinkpad)
2754 if (gst_pad_check_pull_range (sinkpad)) {
2755 return gst_pad_activate_pull (sinkpad, TRUE);
2757 return gst_pad_activate_push (sinkpad, TRUE);
2761 /* This function gets called when we activate ourselves in push mode. */
2763 gst_flups_demux_sink_activate_push (GstPad * sinkpad, gboolean active)
2765 GstFluPSDemux *demux;
2767 demux = GST_FLUPS_DEMUX (gst_pad_get_parent (sinkpad));
2769 demux->random_access = FALSE;
2771 gst_object_unref (demux);
2776 /* this function gets called when we activate ourselves in pull mode.
2777 * We can perform random access to the resource and we start a task
2778 * to start reading */
2780 gst_flups_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
2782 GstFluPSDemux *demux;
2784 demux = GST_FLUPS_DEMUX (gst_pad_get_parent (sinkpad));
2787 GST_DEBUG ("pull mode activated");
2788 demux->random_access = TRUE;
2789 gst_object_unref (demux);
2790 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_flups_demux_loop,
2793 demux->random_access = FALSE;
2794 gst_object_unref (demux);
2795 return gst_pad_stop_task (sinkpad);
2799 static GstFlowReturn
2800 gst_flups_demux_chain (GstPad * pad, GstBuffer * buffer)
2802 GstFluPSDemux *demux = GST_FLUPS_DEMUX (gst_pad_get_parent (pad));
2803 GstFlowReturn ret = GST_FLOW_OK;
2805 gboolean save, discont;
2807 discont = GST_BUFFER_IS_DISCONT (buffer);
2810 GST_LOG_OBJECT (demux, "Received buffer with discont flag and"
2811 " offset %" G_GUINT64_FORMAT, GST_BUFFER_OFFSET (buffer));
2813 gst_pes_filter_drain (&demux->filter);
2814 gst_flups_demux_mark_discont (demux, TRUE, FALSE);
2816 /* mark discont on all streams */
2817 if (demux->sink_segment.rate >= 0.0) {
2818 demux->current_scr = G_MAXUINT64;
2819 demux->bytes_since_scr = 0;
2822 GST_LOG_OBJECT (demux, "Received buffer with offset %" G_GUINT64_FORMAT,
2823 GST_BUFFER_OFFSET (buffer));
2826 /* We keep the offset to interpolate SCR */
2827 demux->adapter_offset = GST_BUFFER_OFFSET (buffer);
2829 gst_adapter_push (demux->adapter, buffer);
2830 demux->bytes_since_scr += GST_BUFFER_SIZE (buffer);
2832 avail = gst_adapter_available (demux->rev_adapter);
2834 GST_LOG_OBJECT (demux, "appending %u saved bytes", avail);
2835 /* if we have a previous reverse chunk, append this now */
2836 /* FIXME this code assumes we receive discont buffers all thei
2838 gst_adapter_push (demux->adapter,
2839 gst_adapter_take_buffer (demux->rev_adapter, avail));
2842 avail = gst_adapter_available (demux->adapter);
2843 GST_LOG_OBJECT (demux, "avail now: %d, state %d", avail, demux->filter.state);
2845 switch (demux->filter.state) {
2846 case STATE_DATA_SKIP:
2847 case STATE_DATA_PUSH:
2848 ret = gst_pes_filter_process (&demux->filter);
2850 case STATE_HEADER_PARSE:
2857 case GST_FLOW_NEED_MORE_DATA:
2858 /* Go and get more data */
2861 case GST_FLOW_LOST_SYNC:
2862 /* for FLOW_OK or lost-sync, carry onto resync */
2868 /* Any other return value should be sent upstream immediately */
2872 /* align adapter data to sync boundary, we keep the data up to the next sync
2875 while (gst_flups_demux_resync (demux, save)) {
2876 gboolean ps_sync = TRUE;
2877 if (G_UNLIKELY (demux->flushing)) {
2878 ret = GST_FLOW_WRONG_STATE;
2882 /* now switch on last synced byte */
2883 switch (demux->last_sync_code) {
2884 case ID_PS_PACK_START_CODE:
2885 ret = gst_flups_demux_parse_pack_start (demux);
2887 case ID_PS_SYSTEM_HEADER_START_CODE:
2888 ret = gst_flups_demux_parse_sys_head (demux);
2890 case ID_PS_END_CODE:
2893 case ID_PS_PROGRAM_STREAM_MAP:
2894 ret = gst_flups_demux_parse_psm (demux);
2897 if (gst_flups_demux_is_pes_sync (demux->last_sync_code)) {
2898 ret = gst_pes_filter_process (&demux->filter);
2900 GST_DEBUG_OBJECT (demux, "sync_code=%08x, non PES sync found"
2901 ", continuing", demux->last_sync_code);
2903 ret = GST_FLOW_LOST_SYNC;
2907 /* if we found a ps sync, we stop saving the data, any non-ps sync gets
2908 * saved up to the next ps sync. */
2913 case GST_FLOW_NEED_MORE_DATA:
2914 GST_DEBUG_OBJECT (demux, "need more data");
2917 case GST_FLOW_LOST_SYNC:
2918 if (!save || demux->sink_segment.rate >= 0.0) {
2919 GST_DEBUG_OBJECT (demux, "flushing 3 bytes");
2920 gst_adapter_flush (demux->adapter, 3);
2921 ADAPTER_OFFSET_FLUSH (3);
2923 GST_DEBUG_OBJECT (demux, "saving 3 bytes");
2924 gst_adapter_push (demux->rev_adapter,
2925 gst_adapter_take_buffer (demux->adapter, 3));
2934 gst_object_unref (demux);
2939 static GstStateChangeReturn
2940 gst_flups_demux_change_state (GstElement * element, GstStateChange transition)
2942 GstFluPSDemux *demux = GST_FLUPS_DEMUX (element);
2943 GstStateChangeReturn result;
2945 switch (transition) {
2946 case GST_STATE_CHANGE_NULL_TO_READY:
2947 demux->adapter = gst_adapter_new ();
2948 demux->rev_adapter = gst_adapter_new ();
2949 demux->adapter_offset = G_MAXUINT64;
2950 gst_pes_filter_init (&demux->filter, demux->adapter,
2951 &demux->adapter_offset);
2952 gst_pes_filter_set_callbacks (&demux->filter,
2953 (GstPESFilterData) gst_flups_demux_data_cb,
2954 (GstPESFilterResync) gst_flups_demux_resync_cb, demux);
2955 demux->filter.gather_pes = TRUE;
2956 demux->first_scr = G_MAXUINT64;
2957 demux->last_scr = G_MAXUINT64;
2958 demux->current_scr = G_MAXUINT64;
2959 demux->base_time = G_MAXUINT64;
2960 demux->scr_rate_n = G_MAXUINT64;
2961 demux->scr_rate_d = G_MAXUINT64;
2962 demux->first_pts = G_MAXUINT64;
2963 demux->last_pts = G_MAXUINT64;
2965 case GST_STATE_CHANGE_READY_TO_PAUSED:
2966 demux->current_scr = G_MAXUINT64;
2967 demux->mux_rate = G_MAXUINT64;
2968 demux->next_pts = G_MAXUINT64;
2969 demux->next_dts = G_MAXUINT64;
2970 demux->first_scr = G_MAXUINT64;
2971 demux->last_scr = G_MAXUINT64;
2972 demux->base_time = G_MAXUINT64;
2973 demux->scr_rate_n = G_MAXUINT64;
2974 demux->scr_rate_d = G_MAXUINT64;
2975 demux->need_no_more_pads = TRUE;
2976 demux->first_pts = G_MAXUINT64;
2977 demux->last_pts = G_MAXUINT64;
2978 gst_flups_demux_reset_psm (demux);
2979 gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
2980 gst_segment_init (&demux->src_segment, GST_FORMAT_TIME);
2981 gst_flups_demux_flush (demux);
2987 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2989 switch (transition) {
2990 case GST_STATE_CHANGE_PAUSED_TO_READY:
2991 gst_flups_demux_reset (demux);
2993 case GST_STATE_CHANGE_READY_TO_NULL:
2994 gst_pes_filter_uninit (&demux->filter);
2995 g_object_unref (demux->adapter);
2996 demux->adapter = NULL;
2997 g_object_unref (demux->rev_adapter);
2998 demux->rev_adapter = NULL;
3008 gst_flups_demux_plugin_init (GstPlugin * plugin)
3010 if (!gst_element_register (plugin, "mpegpsdemux",
3011 GST_RANK_PRIMARY, GST_TYPE_FLUPS_DEMUX))