1 /* GStreamer Matroska muxer/demuxer
2 * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3 * (c) 2006 Tim-Philipp Müller <tim centricular net>
4 * (c) 2008 Sebastian Dröge <slomo@circular-chaos.org>
5 * (c) 2011 Debarshi Ray <rishi@gnu.org>
7 * matroska-demux.c: matroska file/stream demuxer
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
25 /* TODO: check CRC32 if present
26 * TODO: there can be a segment after the first segment. Handle like
27 * chained oggs. Fixes #334082
28 * TODO: Test samples: http://www.matroska.org/samples/matrix/index.html
29 * http://samples.mplayerhq.hu/Matroska/
30 * TODO: check if demuxing is done correct for all codecs according to spec
31 * TODO: seeking with incomplete or without CUE
35 * SECTION:element-matroskademux
37 * matroskademux demuxes a Matroska file into the different contained streams.
40 * <title>Example launch line</title>
42 * gst-launch-1.0 -v filesrc location=/path/to/mkv ! matroskademux ! vorbisdec ! audioconvert ! audioresample ! autoaudiosink
43 * ]| This pipeline demuxes a Matroska file and outputs the contained Vorbis audio.
54 #include <glib/gprintf.h>
56 /* For AVI compatibility mode
57 and for fourcc stuff */
58 #include <gst/riff/riff-read.h>
59 #include <gst/riff/riff-ids.h>
60 #include <gst/riff/riff-media.h>
62 #include <gst/audio/audio.h>
63 #include <gst/tag/tag.h>
64 #include <gst/pbutils/pbutils.h>
65 #include <gst/video/video.h>
67 #include "matroska-demux.h"
68 #include "matroska-ids.h"
70 GST_DEBUG_CATEGORY_STATIC (matroskademux_debug);
71 #define GST_CAT_DEFAULT matroskademux_debug
73 #define DEBUG_ELEMENT_START(demux, ebml, element) \
74 GST_DEBUG_OBJECT (demux, "Parsing " element " element at offset %" \
75 G_GUINT64_FORMAT, gst_ebml_read_get_pos (ebml))
77 #define DEBUG_ELEMENT_STOP(demux, ebml, element, ret) \
78 GST_DEBUG_OBJECT (demux, "Parsing " element " element " \
79 " finished with '%s'", gst_flow_get_name (ret))
89 #define DEFAULT_MAX_GAP_TIME (2 * GST_SECOND)
90 #define INVALID_DATA_THRESHOLD (2 * 1024 * 1024)
92 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
95 GST_STATIC_CAPS ("audio/x-matroska; video/x-matroska; "
96 "video/x-matroska-3d; audio/webm; video/webm")
99 /* TODO: fill in caps! */
101 static GstStaticPadTemplate audio_src_templ =
102 GST_STATIC_PAD_TEMPLATE ("audio_%u",
105 GST_STATIC_CAPS ("ANY")
108 static GstStaticPadTemplate video_src_templ =
109 GST_STATIC_PAD_TEMPLATE ("video_%u",
112 GST_STATIC_CAPS ("ANY")
115 static GstStaticPadTemplate subtitle_src_templ =
116 GST_STATIC_PAD_TEMPLATE ("subtitle_%u",
119 GST_STATIC_CAPS ("text/x-raw, format=pango-markup; application/x-ssa; "
120 "application/x-ass;application/x-usf; subpicture/x-dvd; "
121 "subpicture/x-pgs; subtitle/x-kate; " "application/x-subtitle-unknown")
124 static GstFlowReturn gst_matroska_demux_parse_id (GstMatroskaDemux * demux,
125 guint32 id, guint64 length, guint needed);
127 /* element functions */
128 static void gst_matroska_demux_loop (GstPad * pad);
130 static gboolean gst_matroska_demux_element_send_event (GstElement * element,
132 static gboolean gst_matroska_demux_element_query (GstElement * element,
136 static gboolean gst_matroska_demux_sink_activate (GstPad * sinkpad,
138 static gboolean gst_matroska_demux_sink_activate_mode (GstPad * sinkpad,
139 GstObject * parent, GstPadMode mode, gboolean active);
141 static gboolean gst_matroska_demux_handle_seek_event (GstMatroskaDemux * demux,
142 GstPad * pad, GstEvent * event);
143 static gboolean gst_matroska_demux_handle_src_event (GstPad * pad,
144 GstObject * parent, GstEvent * event);
145 static gboolean gst_matroska_demux_handle_src_query (GstPad * pad,
146 GstObject * parent, GstQuery * query);
148 static gboolean gst_matroska_demux_handle_sink_event (GstPad * pad,
149 GstObject * parent, GstEvent * event);
150 static GstFlowReturn gst_matroska_demux_chain (GstPad * pad,
151 GstObject * object, GstBuffer * buffer);
153 static GstStateChangeReturn
154 gst_matroska_demux_change_state (GstElement * element,
155 GstStateChange transition);
158 gst_matroska_demux_set_index (GstElement * element, GstIndex * index);
159 static GstIndex *gst_matroska_demux_get_index (GstElement * element);
163 static GstCaps *gst_matroska_demux_video_caps (GstMatroskaTrackVideoContext
164 * videocontext, const gchar * codec_id, guint8 * data, guint size,
165 gchar ** codec_name, guint32 * riff_fourcc);
166 static GstCaps *gst_matroska_demux_audio_caps (GstMatroskaTrackAudioContext
167 * audiocontext, const gchar * codec_id, guint8 * data, guint size,
168 gchar ** codec_name, guint16 * riff_audio_fmt);
170 * gst_matroska_demux_subtitle_caps (GstMatroskaTrackSubtitleContext *
171 subtitlecontext, const gchar * codec_id, gpointer data, guint size);
174 static void gst_matroska_demux_reset (GstElement * element);
175 static gboolean perform_seek_to_offset (GstMatroskaDemux * demux,
176 gdouble rate, guint64 offset, guint32 seqnum, GstSeekFlags flags);
178 /* gobject functions */
179 static void gst_matroska_demux_set_property (GObject * object,
180 guint prop_id, const GValue * value, GParamSpec * pspec);
181 static void gst_matroska_demux_get_property (GObject * object,
182 guint prop_id, GValue * value, GParamSpec * pspec);
184 GType gst_matroska_demux_get_type (void);
185 #define parent_class gst_matroska_demux_parent_class
186 G_DEFINE_TYPE (GstMatroskaDemux, gst_matroska_demux, GST_TYPE_ELEMENT);
189 gst_matroska_demux_finalize (GObject * object)
191 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (object);
193 gst_matroska_read_common_finalize (&demux->common);
194 gst_flow_combiner_free (demux->flowcombiner);
195 G_OBJECT_CLASS (parent_class)->finalize (object);
199 gst_matroska_demux_class_init (GstMatroskaDemuxClass * klass)
201 GObjectClass *gobject_class = (GObjectClass *) klass;
202 GstElementClass *gstelement_class = (GstElementClass *) klass;
204 GST_DEBUG_CATEGORY_INIT (matroskademux_debug, "matroskademux", 0,
207 gobject_class->finalize = gst_matroska_demux_finalize;
209 gobject_class->get_property = gst_matroska_demux_get_property;
210 gobject_class->set_property = gst_matroska_demux_set_property;
212 g_object_class_install_property (gobject_class, PROP_MAX_GAP_TIME,
213 g_param_spec_uint64 ("max-gap-time", "Maximum gap time",
214 "The demuxer sends out segment events for skipping "
215 "gaps longer than this (0 = disabled).", 0, G_MAXUINT64,
216 DEFAULT_MAX_GAP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218 gstelement_class->change_state =
219 GST_DEBUG_FUNCPTR (gst_matroska_demux_change_state);
220 gstelement_class->send_event =
221 GST_DEBUG_FUNCPTR (gst_matroska_demux_element_send_event);
222 gstelement_class->query =
223 GST_DEBUG_FUNCPTR (gst_matroska_demux_element_query);
225 gstelement_class->set_index =
226 GST_DEBUG_FUNCPTR (gst_matroska_demux_set_index);
227 gstelement_class->get_index =
228 GST_DEBUG_FUNCPTR (gst_matroska_demux_get_index);
231 gst_element_class_add_static_pad_template (gstelement_class,
233 gst_element_class_add_static_pad_template (gstelement_class,
235 gst_element_class_add_static_pad_template (gstelement_class,
236 &subtitle_src_templ);
237 gst_element_class_add_static_pad_template (gstelement_class, &sink_templ);
239 gst_element_class_set_static_metadata (gstelement_class, "Matroska demuxer",
241 "Demuxes Matroska/WebM streams into video/audio/subtitles",
242 "GStreamer maintainers <gstreamer-devel@lists.freedesktop.org>");
246 gst_matroska_demux_init (GstMatroskaDemux * demux)
248 demux->common.sinkpad = gst_pad_new_from_static_template (&sink_templ,
250 gst_pad_set_activate_function (demux->common.sinkpad,
251 GST_DEBUG_FUNCPTR (gst_matroska_demux_sink_activate));
252 gst_pad_set_activatemode_function (demux->common.sinkpad,
253 GST_DEBUG_FUNCPTR (gst_matroska_demux_sink_activate_mode));
254 gst_pad_set_chain_function (demux->common.sinkpad,
255 GST_DEBUG_FUNCPTR (gst_matroska_demux_chain));
256 gst_pad_set_event_function (demux->common.sinkpad,
257 GST_DEBUG_FUNCPTR (gst_matroska_demux_handle_sink_event));
258 gst_element_add_pad (GST_ELEMENT (demux), demux->common.sinkpad);
260 /* init defaults for common read context */
261 gst_matroska_read_common_init (&demux->common);
263 /* property defaults */
264 demux->max_gap_time = DEFAULT_MAX_GAP_TIME;
266 GST_OBJECT_FLAG_SET (demux, GST_ELEMENT_FLAG_INDEXABLE);
268 demux->flowcombiner = gst_flow_combiner_new ();
271 gst_matroska_demux_reset (GST_ELEMENT (demux));
275 gst_matroska_demux_reset (GstElement * element)
277 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
279 GST_DEBUG_OBJECT (demux, "Resetting state");
281 gst_matroska_read_common_reset (GST_ELEMENT (demux), &demux->common);
283 demux->num_a_streams = 0;
284 demux->num_t_streams = 0;
285 demux->num_v_streams = 0;
287 demux->have_group_id = FALSE;
288 demux->group_id = G_MAXUINT;
291 demux->tracks_parsed = FALSE;
293 if (demux->clusters) {
294 g_array_free (demux->clusters, TRUE);
295 demux->clusters = NULL;
298 g_list_foreach (demux->seek_parsed,
299 (GFunc) gst_matroska_read_common_free_parsed_el, NULL);
300 g_list_free (demux->seek_parsed);
301 demux->seek_parsed = NULL;
303 demux->last_stop_end = GST_CLOCK_TIME_NONE;
304 demux->seek_block = 0;
305 demux->stream_start_time = GST_CLOCK_TIME_NONE;
306 demux->to_time = GST_CLOCK_TIME_NONE;
307 demux->cluster_time = GST_CLOCK_TIME_NONE;
308 demux->cluster_offset = 0;
309 demux->next_cluster_offset = 0;
310 demux->index_offset = 0;
311 demux->seekable = FALSE;
312 demux->need_segment = FALSE;
313 demux->segment_seqnum = 0;
314 demux->requested_seek_time = GST_CLOCK_TIME_NONE;
315 demux->seek_offset = -1;
316 demux->building_index = FALSE;
317 if (demux->seek_event) {
318 gst_event_unref (demux->seek_event);
319 demux->seek_event = NULL;
322 demux->seek_index = NULL;
323 demux->seek_entry = 0;
325 if (demux->new_segment) {
326 gst_event_unref (demux->new_segment);
327 demux->new_segment = NULL;
330 demux->invalid_duration = FALSE;
332 demux->cached_length = G_MAXUINT64;
334 gst_flow_combiner_clear (demux->flowcombiner);
338 gst_matroska_decode_buffer (GstMatroskaTrackContext * context, GstBuffer * buf)
344 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
346 GST_DEBUG ("decoding buffer %p", buf);
348 gst_buffer_map (buf, &map, GST_MAP_READ);
352 g_return_val_if_fail (size > 0, buf);
354 if (gst_matroska_decode_data (context->encodings, &data, &size,
355 GST_MATROSKA_TRACK_ENCODING_SCOPE_FRAME, FALSE)) {
356 gst_buffer_unmap (buf, &map);
357 gst_buffer_unref (buf);
358 return gst_buffer_new_wrapped (data, size);
360 GST_DEBUG ("decode data failed");
361 gst_buffer_unmap (buf, &map);
362 gst_buffer_unref (buf);
368 gst_matroska_demux_add_stream_headers_to_caps (GstMatroskaDemux * demux,
369 GstBufferList * list, GstCaps * caps)
372 GValue arr_val = G_VALUE_INIT;
373 GValue buf_val = G_VALUE_INIT;
376 g_assert (gst_caps_is_writable (caps));
378 g_value_init (&arr_val, GST_TYPE_ARRAY);
379 g_value_init (&buf_val, GST_TYPE_BUFFER);
381 num = gst_buffer_list_length (list);
382 for (i = 0; i < num; ++i) {
383 g_value_set_boxed (&buf_val, gst_buffer_list_get (list, i));
384 gst_value_array_append_value (&arr_val, &buf_val);
387 s = gst_caps_get_structure (caps, 0);
388 gst_structure_take_value (s, "streamheader", &arr_val);
389 g_value_unset (&buf_val);
393 gst_matroska_demux_add_stream (GstMatroskaDemux * demux, GstEbmlRead * ebml)
395 GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
396 GstMatroskaTrackContext *context;
397 GstPadTemplate *templ = NULL;
398 GstStreamFlags stream_flags;
399 GstCaps *caps = NULL;
400 GstTagList *cached_taglist;
401 gchar *padname = NULL;
403 guint32 id, riff_fourcc = 0;
404 guint16 riff_audio_fmt = 0;
405 GstEvent *stream_start;
409 DEBUG_ELEMENT_START (demux, ebml, "TrackEntry");
411 /* start with the master */
412 if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
413 DEBUG_ELEMENT_STOP (demux, ebml, "TrackEntry", ret);
417 /* allocate generic... if we know the type, we'll g_renew()
418 * with the precise type */
419 context = g_new0 (GstMatroskaTrackContext, 1);
420 g_ptr_array_add (demux->common.src, context);
421 context->index = demux->common.num_streams;
422 context->index_writer_id = -1;
423 context->type = 0; /* no type yet */
424 context->default_duration = 0;
426 context->set_discont = TRUE;
427 context->timecodescale = 1.0;
429 GST_MATROSKA_TRACK_ENABLED | GST_MATROSKA_TRACK_DEFAULT |
430 GST_MATROSKA_TRACK_LACING;
431 context->from_time = GST_CLOCK_TIME_NONE;
432 context->from_offset = -1;
433 context->to_offset = G_MAXINT64;
434 context->alignment = 1;
435 context->dts_only = FALSE;
436 context->intra_only = FALSE;
437 context->tags = gst_tag_list_new_empty ();
438 demux->common.num_streams++;
439 g_assert (demux->common.src->len == demux->common.num_streams);
441 GST_DEBUG_OBJECT (demux, "Stream number %d", context->index);
443 /* try reading the trackentry headers */
444 while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
445 if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
449 /* track number (unique stream ID) */
450 case GST_MATROSKA_ID_TRACKNUMBER:{
453 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
457 GST_ERROR_OBJECT (demux, "Invalid TrackNumber 0");
458 ret = GST_FLOW_ERROR;
460 } else if (!gst_matroska_read_common_tracknumber_unique (&demux->common,
462 GST_ERROR_OBJECT (demux, "TrackNumber %" G_GUINT64_FORMAT
463 " is not unique", num);
464 ret = GST_FLOW_ERROR;
468 GST_DEBUG_OBJECT (demux, "TrackNumber: %" G_GUINT64_FORMAT, num);
472 /* track UID (unique identifier) */
473 case GST_MATROSKA_ID_TRACKUID:{
476 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
480 GST_ERROR_OBJECT (demux, "Invalid TrackUID 0");
481 ret = GST_FLOW_ERROR;
485 GST_DEBUG_OBJECT (demux, "TrackUID: %" G_GUINT64_FORMAT, num);
490 /* track type (video, audio, combined, subtitle, etc.) */
491 case GST_MATROSKA_ID_TRACKTYPE:{
494 if ((ret = gst_ebml_read_uint (ebml, &id, &track_type)) != GST_FLOW_OK) {
498 if (context->type != 0 && context->type != track_type) {
499 GST_WARNING_OBJECT (demux,
500 "More than one tracktype defined in a TrackEntry - skipping");
502 } else if (track_type < 1 || track_type > 254) {
503 GST_WARNING_OBJECT (demux, "Invalid TrackType %" G_GUINT64_FORMAT,
508 GST_DEBUG_OBJECT (demux, "TrackType: %" G_GUINT64_FORMAT, track_type);
510 /* ok, so we're actually going to reallocate this thing */
511 switch (track_type) {
512 case GST_MATROSKA_TRACK_TYPE_VIDEO:
513 gst_matroska_track_init_video_context (&context);
515 case GST_MATROSKA_TRACK_TYPE_AUDIO:
516 gst_matroska_track_init_audio_context (&context);
518 case GST_MATROSKA_TRACK_TYPE_SUBTITLE:
519 gst_matroska_track_init_subtitle_context (&context);
521 case GST_MATROSKA_TRACK_TYPE_COMPLEX:
522 case GST_MATROSKA_TRACK_TYPE_LOGO:
523 case GST_MATROSKA_TRACK_TYPE_BUTTONS:
524 case GST_MATROSKA_TRACK_TYPE_CONTROL:
526 GST_WARNING_OBJECT (demux,
527 "Unknown or unsupported TrackType %" G_GUINT64_FORMAT,
532 g_ptr_array_index (demux->common.src, demux->common.num_streams - 1)
537 /* tracktype specific stuff for video */
538 case GST_MATROSKA_ID_TRACKVIDEO:{
539 GstMatroskaTrackVideoContext *videocontext;
541 DEBUG_ELEMENT_START (demux, ebml, "TrackVideo");
543 if (!gst_matroska_track_init_video_context (&context)) {
544 GST_WARNING_OBJECT (demux,
545 "TrackVideo element in non-video track - ignoring track");
546 ret = GST_FLOW_ERROR;
548 } else if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
551 videocontext = (GstMatroskaTrackVideoContext *) context;
552 g_ptr_array_index (demux->common.src, demux->common.num_streams - 1)
555 while (ret == GST_FLOW_OK &&
556 gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
557 if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
561 /* Should be one level up but some broken muxers write it here. */
562 case GST_MATROSKA_ID_TRACKDEFAULTDURATION:{
565 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
569 GST_WARNING_OBJECT (demux, "Invalid TrackDefaultDuration 0");
573 GST_DEBUG_OBJECT (demux,
574 "TrackDefaultDuration: %" G_GUINT64_FORMAT, num);
575 context->default_duration = num;
579 /* video framerate */
580 /* NOTE: This one is here only for backward compatibility.
581 * Use _TRACKDEFAULDURATION one level up. */
582 case GST_MATROSKA_ID_VIDEOFRAMERATE:{
585 if ((ret = gst_ebml_read_float (ebml, &id, &num)) != GST_FLOW_OK)
589 GST_WARNING_OBJECT (demux, "Invalid TrackVideoFPS %lf", num);
593 GST_DEBUG_OBJECT (demux, "TrackVideoFrameRate: %lf", num);
594 if (context->default_duration == 0)
595 context->default_duration =
596 gst_gdouble_to_guint64 ((gdouble) GST_SECOND * (1.0 / num));
597 videocontext->default_fps = num;
601 /* width of the size to display the video at */
602 case GST_MATROSKA_ID_VIDEODISPLAYWIDTH:{
605 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
609 GST_WARNING_OBJECT (demux, "Invalid TrackVideoDisplayWidth 0");
613 GST_DEBUG_OBJECT (demux,
614 "TrackVideoDisplayWidth: %" G_GUINT64_FORMAT, num);
615 videocontext->display_width = num;
619 /* height of the size to display the video at */
620 case GST_MATROSKA_ID_VIDEODISPLAYHEIGHT:{
623 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
627 GST_WARNING_OBJECT (demux, "Invalid TrackVideoDisplayHeight 0");
631 GST_DEBUG_OBJECT (demux,
632 "TrackVideoDisplayHeight: %" G_GUINT64_FORMAT, num);
633 videocontext->display_height = num;
637 /* width of the video in the file */
638 case GST_MATROSKA_ID_VIDEOPIXELWIDTH:{
641 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
645 GST_WARNING_OBJECT (demux, "Invalid TrackVideoPixelWidth 0");
649 GST_DEBUG_OBJECT (demux,
650 "TrackVideoPixelWidth: %" G_GUINT64_FORMAT, num);
651 videocontext->pixel_width = num;
655 /* height of the video in the file */
656 case GST_MATROSKA_ID_VIDEOPIXELHEIGHT:{
659 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
663 GST_WARNING_OBJECT (demux, "Invalid TrackVideoPixelHeight 0");
667 GST_DEBUG_OBJECT (demux,
668 "TrackVideoPixelHeight: %" G_GUINT64_FORMAT, num);
669 videocontext->pixel_height = num;
673 /* whether the video is interlaced */
674 case GST_MATROSKA_ID_VIDEOFLAGINTERLACED:{
677 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
681 context->flags |= GST_MATROSKA_VIDEOTRACK_INTERLACED;
683 context->flags &= ~GST_MATROSKA_VIDEOTRACK_INTERLACED;
684 GST_DEBUG_OBJECT (demux, "TrackVideoInterlaced: %d",
685 (context->flags & GST_MATROSKA_VIDEOTRACK_INTERLACED) ? 1 :
690 /* aspect ratio behaviour */
691 case GST_MATROSKA_ID_VIDEOASPECTRATIOTYPE:{
694 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
697 if (num != GST_MATROSKA_ASPECT_RATIO_MODE_FREE &&
698 num != GST_MATROSKA_ASPECT_RATIO_MODE_KEEP &&
699 num != GST_MATROSKA_ASPECT_RATIO_MODE_FIXED) {
700 GST_WARNING_OBJECT (demux,
701 "Unknown TrackVideoAspectRatioType 0x%x", (guint) num);
704 GST_DEBUG_OBJECT (demux,
705 "TrackVideoAspectRatioType: %" G_GUINT64_FORMAT, num);
706 videocontext->asr_mode = num;
710 /* colourspace (only matters for raw video) fourcc */
711 case GST_MATROSKA_ID_VIDEOCOLOURSPACE:{
716 gst_ebml_read_binary (ebml, &id, &data,
717 &datalen)) != GST_FLOW_OK)
722 GST_WARNING_OBJECT (demux,
723 "Invalid TrackVideoColourSpace length %" G_GUINT64_FORMAT,
728 memcpy (&videocontext->fourcc, data, 4);
729 GST_DEBUG_OBJECT (demux,
730 "TrackVideoColourSpace: %" GST_FOURCC_FORMAT,
731 GST_FOURCC_ARGS (videocontext->fourcc));
735 case GST_MATROSKA_ID_VIDEOSTEREOMODE:
739 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
742 GST_DEBUG_OBJECT (demux, "StereoMode: %" G_GUINT64_FORMAT, num);
745 case GST_MATROSKA_STEREO_MODE_SBS_RL:
746 videocontext->multiview_flags =
747 GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
749 case GST_MATROSKA_STEREO_MODE_SBS_LR:
750 videocontext->multiview_mode =
751 GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE;
753 case GST_MATROSKA_STEREO_MODE_TB_RL:
754 videocontext->multiview_flags =
755 GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
757 case GST_MATROSKA_STEREO_MODE_TB_LR:
758 videocontext->multiview_mode =
759 GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM;
761 case GST_MATROSKA_STEREO_MODE_CHECKER_RL:
762 videocontext->multiview_flags =
763 GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
765 case GST_MATROSKA_STEREO_MODE_CHECKER_LR:
766 videocontext->multiview_mode =
767 GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD;
769 case GST_MATROSKA_STEREO_MODE_FBF_RL:
770 videocontext->multiview_flags =
771 GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
773 case GST_MATROSKA_STEREO_MODE_FBF_LR:
774 videocontext->multiview_mode =
775 GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME;
776 /* FIXME: In frame-by-frame mode, left/right frame buffers are
777 * laced within one block, and we'll need to apply FIRST_IN_BUNDLE
778 * accordingly. See http://www.matroska.org/technical/specs/index.html#StereoMode */
779 GST_FIXME_OBJECT (demux,
780 "Frame-by-frame stereoscopic mode not fully implemented");
787 GST_WARNING_OBJECT (demux,
788 "Unknown TrackVideo subelement 0x%x - ignoring", id);
790 case GST_MATROSKA_ID_VIDEODISPLAYUNIT:
791 case GST_MATROSKA_ID_VIDEOPIXELCROPBOTTOM:
792 case GST_MATROSKA_ID_VIDEOPIXELCROPTOP:
793 case GST_MATROSKA_ID_VIDEOPIXELCROPLEFT:
794 case GST_MATROSKA_ID_VIDEOPIXELCROPRIGHT:
795 case GST_MATROSKA_ID_VIDEOGAMMAVALUE:
796 ret = gst_ebml_read_skip (ebml);
801 DEBUG_ELEMENT_STOP (demux, ebml, "TrackVideo", ret);
805 /* tracktype specific stuff for audio */
806 case GST_MATROSKA_ID_TRACKAUDIO:{
807 GstMatroskaTrackAudioContext *audiocontext;
809 DEBUG_ELEMENT_START (demux, ebml, "TrackAudio");
811 if (!gst_matroska_track_init_audio_context (&context)) {
812 GST_WARNING_OBJECT (demux,
813 "TrackAudio element in non-audio track - ignoring track");
814 ret = GST_FLOW_ERROR;
818 if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK)
821 audiocontext = (GstMatroskaTrackAudioContext *) context;
822 g_ptr_array_index (demux->common.src, demux->common.num_streams - 1)
825 while (ret == GST_FLOW_OK &&
826 gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
827 if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
832 case GST_MATROSKA_ID_AUDIOSAMPLINGFREQ:{
835 if ((ret = gst_ebml_read_float (ebml, &id, &num)) != GST_FLOW_OK)
840 GST_WARNING_OBJECT (demux,
841 "Invalid TrackAudioSamplingFrequency %lf", num);
845 GST_DEBUG_OBJECT (demux, "TrackAudioSamplingFrequency: %lf", num);
846 audiocontext->samplerate = num;
851 case GST_MATROSKA_ID_AUDIOBITDEPTH:{
854 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
858 GST_WARNING_OBJECT (demux, "Invalid TrackAudioBitDepth 0");
862 GST_DEBUG_OBJECT (demux, "TrackAudioBitDepth: %" G_GUINT64_FORMAT,
864 audiocontext->bitdepth = num;
869 case GST_MATROSKA_ID_AUDIOCHANNELS:{
872 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
876 GST_WARNING_OBJECT (demux, "Invalid TrackAudioChannels 0");
880 GST_DEBUG_OBJECT (demux, "TrackAudioChannels: %" G_GUINT64_FORMAT,
882 audiocontext->channels = num;
887 GST_WARNING_OBJECT (demux,
888 "Unknown TrackAudio subelement 0x%x - ignoring", id);
890 case GST_MATROSKA_ID_AUDIOCHANNELPOSITIONS:
891 case GST_MATROSKA_ID_AUDIOOUTPUTSAMPLINGFREQ:
892 ret = gst_ebml_read_skip (ebml);
897 DEBUG_ELEMENT_STOP (demux, ebml, "TrackAudio", ret);
902 /* codec identifier */
903 case GST_MATROSKA_ID_CODECID:{
906 if ((ret = gst_ebml_read_ascii (ebml, &id, &text)) != GST_FLOW_OK)
909 GST_DEBUG_OBJECT (demux, "CodecID: %s", GST_STR_NULL (text));
910 context->codec_id = text;
914 /* codec private data */
915 case GST_MATROSKA_ID_CODECPRIVATE:{
920 gst_ebml_read_binary (ebml, &id, &data, &size)) != GST_FLOW_OK)
923 context->codec_priv = data;
924 context->codec_priv_size = size;
926 GST_DEBUG_OBJECT (demux, "CodecPrivate of size %" G_GUINT64_FORMAT,
931 /* name of the codec */
932 case GST_MATROSKA_ID_CODECNAME:{
935 if ((ret = gst_ebml_read_utf8 (ebml, &id, &text)) != GST_FLOW_OK)
938 GST_DEBUG_OBJECT (demux, "CodecName: %s", GST_STR_NULL (text));
939 context->codec_name = text;
944 case GST_MATROSKA_ID_CODECDELAY:{
947 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
950 context->codec_delay = num;
952 GST_DEBUG_OBJECT (demux, "CodecDelay: %" GST_TIME_FORMAT,
953 GST_TIME_ARGS (num));
958 case GST_MATROSKA_ID_SEEKPREROLL:{
961 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
964 context->seek_preroll = num;
966 GST_DEBUG_OBJECT (demux, "SeekPreroll: %" GST_TIME_FORMAT,
967 GST_TIME_ARGS (num));
971 /* name of this track */
972 case GST_MATROSKA_ID_TRACKNAME:{
975 if ((ret = gst_ebml_read_utf8 (ebml, &id, &text)) != GST_FLOW_OK)
978 context->name = text;
979 GST_DEBUG_OBJECT (demux, "TrackName: %s", GST_STR_NULL (text));
983 /* language (matters for audio/subtitles, mostly) */
984 case GST_MATROSKA_ID_TRACKLANGUAGE:{
987 if ((ret = gst_ebml_read_utf8 (ebml, &id, &text)) != GST_FLOW_OK)
991 context->language = text;
994 if (strlen (context->language) >= 4 && context->language[3] == '-')
995 context->language[3] = '\0';
997 GST_DEBUG_OBJECT (demux, "TrackLanguage: %s",
998 GST_STR_NULL (context->language));
1002 /* whether this is actually used */
1003 case GST_MATROSKA_ID_TRACKFLAGENABLED:{
1006 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
1010 context->flags |= GST_MATROSKA_TRACK_ENABLED;
1012 context->flags &= ~GST_MATROSKA_TRACK_ENABLED;
1014 GST_DEBUG_OBJECT (demux, "TrackEnabled: %d",
1015 (context->flags & GST_MATROSKA_TRACK_ENABLED) ? 1 : 0);
1019 /* whether it's the default for this track type */
1020 case GST_MATROSKA_ID_TRACKFLAGDEFAULT:{
1023 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
1027 context->flags |= GST_MATROSKA_TRACK_DEFAULT;
1029 context->flags &= ~GST_MATROSKA_TRACK_DEFAULT;
1031 GST_DEBUG_OBJECT (demux, "TrackDefault: %d",
1032 (context->flags & GST_MATROSKA_TRACK_DEFAULT) ? 1 : 0);
1036 /* whether the track must be used during playback */
1037 case GST_MATROSKA_ID_TRACKFLAGFORCED:{
1040 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
1044 context->flags |= GST_MATROSKA_TRACK_FORCED;
1046 context->flags &= ~GST_MATROSKA_TRACK_FORCED;
1048 GST_DEBUG_OBJECT (demux, "TrackForced: %d",
1049 (context->flags & GST_MATROSKA_TRACK_FORCED) ? 1 : 0);
1053 /* lacing (like MPEG, where blocks don't end/start on frame
1055 case GST_MATROSKA_ID_TRACKFLAGLACING:{
1058 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
1062 context->flags |= GST_MATROSKA_TRACK_LACING;
1064 context->flags &= ~GST_MATROSKA_TRACK_LACING;
1066 GST_DEBUG_OBJECT (demux, "TrackLacing: %d",
1067 (context->flags & GST_MATROSKA_TRACK_LACING) ? 1 : 0);
1071 /* default length (in time) of one data block in this track */
1072 case GST_MATROSKA_ID_TRACKDEFAULTDURATION:{
1075 if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
1080 GST_WARNING_OBJECT (demux, "Invalid TrackDefaultDuration 0");
1084 GST_DEBUG_OBJECT (demux, "TrackDefaultDuration: %" G_GUINT64_FORMAT,
1086 context->default_duration = num;
1090 case GST_MATROSKA_ID_CONTENTENCODINGS:{
1091 ret = gst_matroska_read_common_read_track_encodings (&demux->common,
1096 case GST_MATROSKA_ID_TRACKTIMECODESCALE:{
1099 if ((ret = gst_ebml_read_float (ebml, &id, &num)) != GST_FLOW_OK)
1103 GST_WARNING_OBJECT (demux, "Invalid TrackTimeCodeScale %lf", num);
1107 GST_DEBUG_OBJECT (demux, "TrackTimeCodeScale: %lf", num);
1108 context->timecodescale = num;
1113 GST_WARNING ("Unknown TrackEntry subelement 0x%x - ignoring", id);
1116 /* we ignore these because they're nothing useful (i.e. crap)
1117 * or simply not implemented yet. */
1118 case GST_MATROSKA_ID_TRACKMINCACHE:
1119 case GST_MATROSKA_ID_TRACKMAXCACHE:
1120 case GST_MATROSKA_ID_MAXBLOCKADDITIONID:
1121 case GST_MATROSKA_ID_TRACKATTACHMENTLINK:
1122 case GST_MATROSKA_ID_TRACKOVERLAY:
1123 case GST_MATROSKA_ID_TRACKTRANSLATE:
1124 case GST_MATROSKA_ID_TRACKOFFSET:
1125 case GST_MATROSKA_ID_CODECSETTINGS:
1126 case GST_MATROSKA_ID_CODECINFOURL:
1127 case GST_MATROSKA_ID_CODECDOWNLOADURL:
1128 case GST_MATROSKA_ID_CODECDECODEALL:
1129 ret = gst_ebml_read_skip (ebml);
1134 DEBUG_ELEMENT_STOP (demux, ebml, "TrackEntry", ret);
1136 /* Decode codec private data if necessary */
1137 if (context->encodings && context->encodings->len > 0 && context->codec_priv
1138 && context->codec_priv_size > 0) {
1139 if (!gst_matroska_decode_data (context->encodings,
1140 &context->codec_priv, &context->codec_priv_size,
1141 GST_MATROSKA_TRACK_ENCODING_SCOPE_CODEC_DATA, TRUE)) {
1142 GST_WARNING_OBJECT (demux, "Decoding codec private data failed");
1143 ret = GST_FLOW_ERROR;
1147 if (context->type == 0 || context->codec_id == NULL || (ret != GST_FLOW_OK
1148 && ret != GST_FLOW_EOS)) {
1149 if (ret == GST_FLOW_OK || ret == GST_FLOW_EOS)
1150 GST_WARNING_OBJECT (ebml, "Unknown stream/codec in track entry header");
1152 demux->common.num_streams--;
1153 g_ptr_array_remove_index (demux->common.src, demux->common.num_streams);
1154 g_assert (demux->common.src->len == demux->common.num_streams);
1155 gst_matroska_track_free (context);
1160 /* check for a cached track taglist */
1162 (GstTagList *) g_hash_table_lookup (demux->common.cached_track_taglists,
1163 GUINT_TO_POINTER (context->uid));
1165 gst_tag_list_insert (context->tags, cached_taglist, GST_TAG_MERGE_APPEND);
1167 /* now create the GStreamer connectivity */
1168 switch (context->type) {
1169 case GST_MATROSKA_TRACK_TYPE_VIDEO:{
1170 GstMatroskaTrackVideoContext *videocontext =
1171 (GstMatroskaTrackVideoContext *) context;
1173 padname = g_strdup_printf ("video_%u", demux->num_v_streams++);
1174 templ = gst_element_class_get_pad_template (klass, "video_%u");
1175 caps = gst_matroska_demux_video_caps (videocontext,
1176 context->codec_id, context->codec_priv,
1177 context->codec_priv_size, &codec, &riff_fourcc);
1180 gst_tag_list_add (context->tags, GST_TAG_MERGE_REPLACE,
1181 GST_TAG_VIDEO_CODEC, codec, NULL);
1182 context->tags_changed = TRUE;
1188 case GST_MATROSKA_TRACK_TYPE_AUDIO:{
1189 GstMatroskaTrackAudioContext *audiocontext =
1190 (GstMatroskaTrackAudioContext *) context;
1192 padname = g_strdup_printf ("audio_%u", demux->num_a_streams++);
1193 templ = gst_element_class_get_pad_template (klass, "audio_%u");
1194 caps = gst_matroska_demux_audio_caps (audiocontext,
1195 context->codec_id, context->codec_priv, context->codec_priv_size,
1196 &codec, &riff_audio_fmt);
1199 gst_tag_list_add (context->tags, GST_TAG_MERGE_REPLACE,
1200 GST_TAG_AUDIO_CODEC, codec, NULL);
1201 context->tags_changed = TRUE;
1207 case GST_MATROSKA_TRACK_TYPE_SUBTITLE:{
1208 GstMatroskaTrackSubtitleContext *subtitlecontext =
1209 (GstMatroskaTrackSubtitleContext *) context;
1211 padname = g_strdup_printf ("subtitle_%u", demux->num_t_streams++);
1212 templ = gst_element_class_get_pad_template (klass, "subtitle_%u");
1213 caps = gst_matroska_demux_subtitle_caps (subtitlecontext,
1214 context->codec_id, context->codec_priv, context->codec_priv_size);
1218 case GST_MATROSKA_TRACK_TYPE_COMPLEX:
1219 case GST_MATROSKA_TRACK_TYPE_LOGO:
1220 case GST_MATROSKA_TRACK_TYPE_BUTTONS:
1221 case GST_MATROSKA_TRACK_TYPE_CONTROL:
1223 /* we should already have quit by now */
1224 g_assert_not_reached ();
1227 if ((context->language == NULL || *context->language == '\0') &&
1228 (context->type == GST_MATROSKA_TRACK_TYPE_AUDIO ||
1229 context->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)) {
1230 GST_LOG ("stream %d: language=eng (assuming default)", context->index);
1231 context->language = g_strdup ("eng");
1234 if (context->language) {
1237 /* Matroska contains ISO 639-2B codes, we want ISO 639-1 */
1238 lang = gst_tag_get_language_code (context->language);
1239 gst_tag_list_add (context->tags, GST_TAG_MERGE_REPLACE,
1240 GST_TAG_LANGUAGE_CODE, (lang) ? lang : context->language, NULL);
1241 context->tags_changed = TRUE;
1245 GST_WARNING_OBJECT (demux, "could not determine caps for stream with "
1246 "codec_id='%s'", context->codec_id);
1247 switch (context->type) {
1248 case GST_MATROSKA_TRACK_TYPE_VIDEO:
1249 caps = gst_caps_new_empty_simple ("video/x-unknown");
1251 case GST_MATROSKA_TRACK_TYPE_AUDIO:
1252 caps = gst_caps_new_empty_simple ("audio/x-unknown");
1254 case GST_MATROSKA_TRACK_TYPE_SUBTITLE:
1255 caps = gst_caps_new_empty_simple ("application/x-subtitle-unknown");
1257 case GST_MATROSKA_TRACK_TYPE_COMPLEX:
1259 caps = gst_caps_new_empty_simple ("application/x-matroska-unknown");
1262 gst_caps_set_simple (caps, "codec-id", G_TYPE_STRING, context->codec_id,
1265 /* add any unrecognised riff fourcc / audio format, but after codec-id */
1266 if (context->type == GST_MATROSKA_TRACK_TYPE_AUDIO && riff_audio_fmt != 0)
1267 gst_caps_set_simple (caps, "format", G_TYPE_INT, riff_audio_fmt, NULL);
1268 else if (context->type == GST_MATROSKA_TRACK_TYPE_VIDEO && riff_fourcc != 0) {
1269 gchar *fstr = g_strdup_printf ("%" GST_FOURCC_FORMAT,
1270 GST_FOURCC_ARGS (riff_fourcc));
1271 gst_caps_set_simple (caps, "fourcc", G_TYPE_STRING, fstr, NULL);
1274 } else if (context->stream_headers != NULL) {
1275 gst_matroska_demux_add_stream_headers_to_caps (demux,
1276 context->stream_headers, caps);
1279 /* the pad in here */
1280 context->pad = gst_pad_new_from_template (templ, padname);
1281 context->caps = caps;
1283 gst_pad_set_event_function (context->pad,
1284 GST_DEBUG_FUNCPTR (gst_matroska_demux_handle_src_event));
1285 gst_pad_set_query_function (context->pad,
1286 GST_DEBUG_FUNCPTR (gst_matroska_demux_handle_src_query));
1288 GST_INFO_OBJECT (demux, "Adding pad '%s' with caps %" GST_PTR_FORMAT,
1291 gst_pad_set_element_private (context->pad, context);
1293 gst_pad_use_fixed_caps (context->pad);
1294 gst_pad_set_active (context->pad, TRUE);
1297 gst_pad_create_stream_id_printf (context->pad, GST_ELEMENT_CAST (demux),
1298 "%03" G_GUINT64_FORMAT ":%03" G_GUINT64_FORMAT,
1299 context->num, context->uid);
1301 gst_pad_get_sticky_event (demux->common.sinkpad, GST_EVENT_STREAM_START,
1304 if (gst_event_parse_group_id (stream_start, &demux->group_id))
1305 demux->have_group_id = TRUE;
1307 demux->have_group_id = FALSE;
1308 gst_event_unref (stream_start);
1309 } else if (!demux->have_group_id) {
1310 demux->have_group_id = TRUE;
1311 demux->group_id = gst_util_group_id_next ();
1314 stream_start = gst_event_new_stream_start (stream_id);
1316 if (demux->have_group_id)
1317 gst_event_set_group_id (stream_start, demux->group_id);
1318 stream_flags = GST_STREAM_FLAG_NONE;
1319 if (context->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
1320 stream_flags |= GST_STREAM_FLAG_SPARSE;
1321 if (context->flags & GST_MATROSKA_TRACK_DEFAULT)
1322 stream_flags |= GST_STREAM_FLAG_SELECT;
1323 gst_event_set_stream_flags (stream_start, stream_flags);
1324 gst_pad_push_event (context->pad, stream_start);
1325 gst_pad_set_caps (context->pad, context->caps);
1328 if (demux->common.global_tags) {
1329 GstEvent *tag_event;
1331 gst_tag_list_add (demux->common.global_tags, GST_TAG_MERGE_REPLACE,
1332 GST_TAG_CONTAINER_FORMAT, "Matroska", NULL);
1333 GST_DEBUG_OBJECT (context->pad, "Sending global_tags %p: %" GST_PTR_FORMAT,
1334 demux->common.global_tags, demux->common.global_tags);
1337 gst_event_new_tag (gst_tag_list_copy (demux->common.global_tags));
1339 gst_pad_push_event (context->pad, tag_event);
1342 if (G_UNLIKELY (context->tags_changed)) {
1343 GST_DEBUG_OBJECT (context->pad, "Sending tags %p: %"
1344 GST_PTR_FORMAT, context->tags, context->tags);
1345 gst_pad_push_event (context->pad,
1346 gst_event_new_tag (gst_tag_list_copy (context->tags)));
1347 context->tags_changed = FALSE;
1350 gst_element_add_pad (GST_ELEMENT (demux), context->pad);
1351 gst_flow_combiner_add_pad (demux->flowcombiner, context->pad);
1360 gst_matroska_demux_query (GstMatroskaDemux * demux, GstPad * pad,
1363 gboolean res = FALSE;
1364 GstMatroskaTrackContext *context = NULL;
1367 context = gst_pad_get_element_private (pad);
1370 switch (GST_QUERY_TYPE (query)) {
1371 case GST_QUERY_POSITION:
1375 gst_query_parse_position (query, &format, NULL);
1378 if (format == GST_FORMAT_TIME) {
1379 GST_OBJECT_LOCK (demux);
1381 gst_query_set_position (query, GST_FORMAT_TIME,
1382 MAX (context->pos, demux->stream_start_time) -
1383 demux->stream_start_time);
1385 gst_query_set_position (query, GST_FORMAT_TIME,
1386 MAX (demux->common.segment.position, demux->stream_start_time) -
1387 demux->stream_start_time);
1388 GST_OBJECT_UNLOCK (demux);
1389 } else if (format == GST_FORMAT_DEFAULT && context
1390 && context->default_duration) {
1391 GST_OBJECT_LOCK (demux);
1392 gst_query_set_position (query, GST_FORMAT_DEFAULT,
1393 context->pos / context->default_duration);
1394 GST_OBJECT_UNLOCK (demux);
1396 GST_DEBUG_OBJECT (demux,
1397 "only position query in TIME and DEFAULT format is supported");
1403 case GST_QUERY_DURATION:
1407 gst_query_parse_duration (query, &format, NULL);
1410 if (format == GST_FORMAT_TIME) {
1411 GST_OBJECT_LOCK (demux);
1412 gst_query_set_duration (query, GST_FORMAT_TIME,
1413 demux->common.segment.duration);
1414 GST_OBJECT_UNLOCK (demux);
1415 } else if (format == GST_FORMAT_DEFAULT && context
1416 && context->default_duration) {
1417 GST_OBJECT_LOCK (demux);
1418 gst_query_set_duration (query, GST_FORMAT_DEFAULT,
1419 demux->common.segment.duration / context->default_duration);
1420 GST_OBJECT_UNLOCK (demux);
1422 GST_DEBUG_OBJECT (demux,
1423 "only duration query in TIME and DEFAULT format is supported");
1429 case GST_QUERY_SEEKING:
1433 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
1434 GST_OBJECT_LOCK (demux);
1435 if (fmt == GST_FORMAT_TIME) {
1438 if (demux->streaming) {
1439 /* assuming we'll be able to get an index ... */
1440 seekable = demux->seekable;
1445 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
1446 0, demux->common.segment.duration);
1449 GST_OBJECT_UNLOCK (demux);
1452 case GST_QUERY_SEGMENT:
1457 format = demux->common.segment.format;
1460 gst_segment_to_stream_time (&demux->common.segment, format,
1461 demux->common.segment.start);
1462 if ((stop = demux->common.segment.stop) == -1)
1463 stop = demux->common.segment.duration;
1466 gst_segment_to_stream_time (&demux->common.segment, format, stop);
1468 gst_query_set_segment (query, demux->common.segment.rate, format, start,
1475 res = gst_pad_query_default (pad, (GstObject *) demux, query);
1478 GST_ELEMENT_CLASS (parent_class)->query (GST_ELEMENT_CAST (demux),
1487 gst_matroska_demux_element_query (GstElement * element, GstQuery * query)
1489 return gst_matroska_demux_query (GST_MATROSKA_DEMUX (element), NULL, query);
1493 gst_matroska_demux_handle_src_query (GstPad * pad, GstObject * parent,
1496 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
1498 return gst_matroska_demux_query (demux, pad, query);
1501 /* returns FALSE if there are no pads to deliver event to,
1502 * otherwise TRUE (whatever the outcome of event sending),
1503 * takes ownership of the passed event! */
1505 gst_matroska_demux_send_event (GstMatroskaDemux * demux, GstEvent * event)
1507 gboolean ret = FALSE;
1510 g_return_val_if_fail (event != NULL, FALSE);
1512 GST_DEBUG_OBJECT (demux, "Sending event of type %s to all source pads",
1513 GST_EVENT_TYPE_NAME (event));
1515 g_assert (demux->common.src->len == demux->common.num_streams);
1516 for (i = 0; i < demux->common.src->len; i++) {
1517 GstMatroskaTrackContext *stream;
1519 stream = g_ptr_array_index (demux->common.src, i);
1520 gst_event_ref (event);
1521 gst_pad_push_event (stream->pad, event);
1525 gst_event_unref (event);
1530 gst_matroska_demux_send_tags (GstMatroskaDemux * demux)
1534 if (G_UNLIKELY (demux->common.global_tags_changed)) {
1535 GstEvent *tag_event;
1536 gst_tag_list_add (demux->common.global_tags, GST_TAG_MERGE_REPLACE,
1537 GST_TAG_CONTAINER_FORMAT, "Matroska", NULL);
1538 GST_DEBUG_OBJECT (demux, "Sending global_tags %p : %" GST_PTR_FORMAT,
1539 demux->common.global_tags, demux->common.global_tags);
1542 gst_event_new_tag (gst_tag_list_copy (demux->common.global_tags));
1544 for (i = 0; i < demux->common.src->len; i++) {
1545 GstMatroskaTrackContext *stream;
1547 stream = g_ptr_array_index (demux->common.src, i);
1548 gst_pad_push_event (stream->pad, gst_event_ref (tag_event));
1551 gst_event_unref (tag_event);
1552 demux->common.global_tags_changed = FALSE;
1555 g_assert (demux->common.src->len == demux->common.num_streams);
1556 for (i = 0; i < demux->common.src->len; i++) {
1557 GstMatroskaTrackContext *stream;
1559 stream = g_ptr_array_index (demux->common.src, i);
1561 if (G_UNLIKELY (stream->tags_changed)) {
1562 GST_DEBUG_OBJECT (demux, "Sending tags %p for pad %s:%s : %"
1563 GST_PTR_FORMAT, stream->tags,
1564 GST_DEBUG_PAD_NAME (stream->pad), stream->tags);
1565 gst_pad_push_event (stream->pad,
1566 gst_event_new_tag (gst_tag_list_copy (stream->tags)));
1567 stream->tags_changed = FALSE;
1573 gst_matroska_demux_element_send_event (GstElement * element, GstEvent * event)
1575 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
1578 g_return_val_if_fail (event != NULL, FALSE);
1580 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
1581 /* no seeking until we are (safely) ready */
1582 if (demux->common.state != GST_MATROSKA_READ_STATE_DATA) {
1583 GST_DEBUG_OBJECT (demux, "not ready for seeking yet");
1584 gst_event_unref (event);
1587 res = gst_matroska_demux_handle_seek_event (demux, NULL, event);
1589 GST_WARNING_OBJECT (demux, "Unhandled event of type %s",
1590 GST_EVENT_TYPE_NAME (event));
1593 gst_event_unref (event);
1598 gst_matroska_demux_move_to_entry (GstMatroskaDemux * demux,
1599 GstMatroskaIndex * entry, gboolean reset, gboolean update)
1603 GST_OBJECT_LOCK (demux);
1606 /* seek (relative to matroska segment) */
1607 /* position might be invalid; will error when streaming resumes ... */
1608 demux->common.offset = entry->pos + demux->common.ebml_segment_start;
1609 demux->next_cluster_offset = 0;
1611 GST_DEBUG_OBJECT (demux,
1612 "Seeked to offset %" G_GUINT64_FORMAT ", block %d, " "time %"
1613 GST_TIME_FORMAT, entry->pos + demux->common.ebml_segment_start,
1614 entry->block, GST_TIME_ARGS (entry->time));
1616 /* update the time */
1617 gst_matroska_read_common_reset_streams (&demux->common, entry->time, TRUE);
1618 gst_flow_combiner_reset (demux->flowcombiner);
1619 demux->common.segment.position = entry->time;
1620 demux->seek_block = entry->block;
1621 demux->seek_first = TRUE;
1622 demux->last_stop_end = GST_CLOCK_TIME_NONE;
1625 for (i = 0; i < demux->common.src->len; i++) {
1626 GstMatroskaTrackContext *stream = g_ptr_array_index (demux->common.src, i);
1629 stream->to_offset = G_MAXINT64;
1631 if (stream->from_offset != -1)
1632 stream->to_offset = stream->from_offset;
1634 stream->from_offset = -1;
1635 stream->from_time = GST_CLOCK_TIME_NONE;
1638 GST_OBJECT_UNLOCK (demux);
1644 gst_matroska_cluster_compare (gint64 * i1, gint64 * i2)
1654 /* searches for a cluster start from @pos,
1655 * return GST_FLOW_OK and cluster position in @pos if found */
1656 static GstFlowReturn
1657 gst_matroska_demux_search_cluster (GstMatroskaDemux * demux, gint64 * pos)
1659 gint64 newpos = *pos;
1661 GstFlowReturn ret = GST_FLOW_OK;
1662 const guint chunk = 64 * 1024;
1663 GstBuffer *buf = NULL;
1665 gpointer data = NULL;
1670 gint64 oldpos, oldlength;
1672 orig_offset = demux->common.offset;
1674 GST_LOG_OBJECT (demux, "searching cluster following offset %" G_GINT64_FORMAT,
1677 if (demux->clusters) {
1680 cpos = gst_util_array_binary_search (demux->clusters->data,
1681 demux->clusters->len, sizeof (gint64),
1682 (GCompareDataFunc) gst_matroska_cluster_compare,
1683 GST_SEARCH_MODE_AFTER, pos, NULL);
1686 GST_DEBUG_OBJECT (demux,
1687 "cluster reported at offset %" G_GINT64_FORMAT, *cpos);
1688 demux->common.offset = *cpos;
1689 ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
1690 GST_ELEMENT_CAST (demux), &id, &length, &needed);
1691 if (ret == GST_FLOW_OK && id == GST_MATROSKA_ID_CLUSTER) {
1698 /* read in at newpos and scan for ebml cluster id */
1699 oldpos = oldlength = -1;
1701 GstByteReader reader;
1705 gst_buffer_unmap (buf, &map);
1706 gst_buffer_unref (buf);
1709 ret = gst_pad_pull_range (demux->common.sinkpad, newpos, chunk, &buf);
1710 if (ret != GST_FLOW_OK)
1712 GST_DEBUG_OBJECT (demux,
1713 "read buffer size %" G_GSIZE_FORMAT " at offset %" G_GINT64_FORMAT,
1714 gst_buffer_get_size (buf), newpos);
1715 gst_buffer_map (buf, &map, GST_MAP_READ);
1718 if (oldpos == newpos && oldlength == map.size) {
1719 GST_ERROR_OBJECT (demux, "Stuck at same position");
1720 ret = GST_FLOW_ERROR;
1724 oldlength = map.size;
1727 gst_byte_reader_init (&reader, data, size);
1729 cluster_pos = gst_byte_reader_masked_scan_uint32 (&reader, 0xffffffff,
1730 GST_MATROSKA_ID_CLUSTER, 0, gst_byte_reader_get_remaining (&reader));
1731 if (cluster_pos >= 0) {
1732 newpos += cluster_pos;
1733 /* prepare resuming at next byte */
1734 if (!gst_byte_reader_skip (&reader, cluster_pos + 1)) {
1735 GST_DEBUG_OBJECT (demux, "Need more data -> continue");
1738 GST_DEBUG_OBJECT (demux,
1739 "found cluster ebml id at offset %" G_GINT64_FORMAT, newpos);
1740 /* extra checks whether we really sync'ed to a cluster:
1741 * - either it is the first and only cluster
1742 * - either there is a cluster after this one
1743 * - either cluster length is undefined
1745 /* ok if first cluster (there may not a subsequent one) */
1746 if (newpos == demux->first_cluster_offset) {
1747 GST_DEBUG_OBJECT (demux, "cluster is first cluster -> OK");
1750 demux->common.offset = newpos;
1751 ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
1752 GST_ELEMENT_CAST (demux), &id, &length, &needed);
1753 if (ret != GST_FLOW_OK) {
1754 GST_DEBUG_OBJECT (demux, "need more data -> continue");
1757 g_assert (id == GST_MATROSKA_ID_CLUSTER);
1758 GST_DEBUG_OBJECT (demux, "cluster size %" G_GUINT64_FORMAT ", prefix %d",
1760 /* ok if undefined length or first cluster */
1761 if (length == GST_EBML_SIZE_UNKNOWN || length == G_MAXUINT64) {
1762 GST_DEBUG_OBJECT (demux, "cluster has undefined length -> OK");
1766 demux->common.offset += length + needed;
1767 ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
1768 GST_ELEMENT_CAST (demux), &id, &length, &needed);
1769 if (ret != GST_FLOW_OK) {
1770 /* we skipped one byte in the reader above, need to accomodate for
1771 * that when resuming skipping from the reader instead of reading a
1776 GST_DEBUG_OBJECT (demux, "next element is %scluster",
1777 id == GST_MATROSKA_ID_CLUSTER ? "" : "not ");
1778 if (id == GST_MATROSKA_ID_CLUSTER)
1781 * we skipped one byte in the reader above, need to accomodate for
1782 * that when resuming skipping from the reader instead of reading a
1787 /* partial cluster id may have been in tail of buffer */
1788 newpos += MAX (gst_byte_reader_get_remaining (&reader), 4) - 3;
1793 gst_buffer_unmap (buf, &map);
1794 gst_buffer_unref (buf);
1799 demux->common.offset = orig_offset;
1804 /* bisect and scan through file for cluster starting before @time,
1805 * returns fake index entry with corresponding info on cluster */
1806 static GstMatroskaIndex *
1807 gst_matroska_demux_search_pos (GstMatroskaDemux * demux, GstClockTime time)
1809 GstMatroskaIndex *entry = NULL;
1810 GstMatroskaReadState current_state;
1811 GstClockTime otime, prev_cluster_time, current_cluster_time, cluster_time;
1812 gint64 opos, newpos, startpos = 0, current_offset;
1813 gint64 prev_cluster_offset = -1, current_cluster_offset, cluster_offset;
1814 const guint chunk = 64 * 1024;
1820 /* (under)estimate new position, resync using cluster ebml id,
1821 * and scan forward to appropriate cluster
1822 * (and re-estimate if need to go backward) */
1824 prev_cluster_time = GST_CLOCK_TIME_NONE;
1826 /* store some current state */
1827 current_state = demux->common.state;
1828 g_return_val_if_fail (current_state == GST_MATROSKA_READ_STATE_DATA, NULL);
1830 current_cluster_offset = demux->cluster_offset;
1831 current_cluster_time = demux->cluster_time;
1832 current_offset = demux->common.offset;
1834 demux->common.state = GST_MATROSKA_READ_STATE_SCANNING;
1836 /* estimate using start and current position */
1837 GST_OBJECT_LOCK (demux);
1838 opos = demux->common.offset - demux->common.ebml_segment_start;
1839 otime = demux->common.segment.position;
1840 GST_OBJECT_UNLOCK (demux);
1843 time = MAX (time, demux->stream_start_time);
1845 /* avoid division by zero in first estimation below */
1846 if (otime <= demux->stream_start_time)
1850 GST_LOG_OBJECT (demux,
1851 "opos: %" G_GUINT64_FORMAT ", otime: %" GST_TIME_FORMAT ", %"
1852 GST_TIME_FORMAT " in stream time (start %" GST_TIME_FORMAT "), time %"
1853 GST_TIME_FORMAT, opos, GST_TIME_ARGS (otime),
1854 GST_TIME_ARGS (otime - demux->stream_start_time),
1855 GST_TIME_ARGS (demux->stream_start_time), GST_TIME_ARGS (time));
1857 if (otime <= demux->stream_start_time) {
1861 gst_util_uint64_scale (opos - demux->common.ebml_segment_start,
1862 time - demux->stream_start_time,
1863 otime - demux->stream_start_time) - chunk;
1867 /* favour undershoot */
1868 newpos = newpos * 90 / 100;
1869 newpos += demux->common.ebml_segment_start;
1871 GST_DEBUG_OBJECT (demux,
1872 "estimated offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
1873 GST_TIME_ARGS (time), newpos);
1875 /* and at least start scanning before previous scan start to avoid looping */
1876 startpos = startpos * 90 / 100;
1877 if (startpos && startpos < newpos)
1880 /* read in at newpos and scan for ebml cluster id */
1884 ret = gst_matroska_demux_search_cluster (demux, &newpos);
1885 if (ret == GST_FLOW_EOS) {
1886 /* heuristic HACK */
1887 newpos = startpos * 80 / 100;
1888 GST_DEBUG_OBJECT (demux, "EOS; "
1889 "new estimated offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
1890 GST_TIME_ARGS (time), newpos);
1893 } else if (ret != GST_FLOW_OK) {
1900 /* then start scanning and parsing for cluster time,
1901 * re-estimate if overshoot, otherwise next cluster and so on */
1902 demux->common.offset = newpos;
1903 demux->cluster_time = cluster_time = GST_CLOCK_TIME_NONE;
1905 guint64 cluster_size = 0;
1907 /* peek and parse some elements */
1908 ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
1909 GST_ELEMENT_CAST (demux), &id, &length, &needed);
1910 if (ret != GST_FLOW_OK)
1912 GST_LOG_OBJECT (demux, "Offset %" G_GUINT64_FORMAT ", Element id 0x%x, "
1913 "size %" G_GUINT64_FORMAT ", needed %d", demux->common.offset, id,
1915 ret = gst_matroska_demux_parse_id (demux, id, length, needed);
1916 if (ret != GST_FLOW_OK)
1919 if (id == GST_MATROSKA_ID_CLUSTER) {
1920 cluster_time = GST_CLOCK_TIME_NONE;
1921 if (length == G_MAXUINT64)
1924 cluster_size = length + needed;
1926 if (demux->cluster_time != GST_CLOCK_TIME_NONE &&
1927 cluster_time == GST_CLOCK_TIME_NONE) {
1928 cluster_time = demux->cluster_time * demux->common.time_scale;
1929 cluster_offset = demux->cluster_offset;
1930 GST_DEBUG_OBJECT (demux, "found cluster at offset %" G_GINT64_FORMAT
1931 " with time %" GST_TIME_FORMAT, cluster_offset,
1932 GST_TIME_ARGS (cluster_time));
1933 if (cluster_time > time) {
1934 GST_DEBUG_OBJECT (demux, "overshot target");
1935 /* cluster overshoots */
1936 if (cluster_offset == demux->first_cluster_offset) {
1937 /* but no prev one */
1938 GST_DEBUG_OBJECT (demux, "but using first cluster anyway");
1939 prev_cluster_time = cluster_time;
1940 prev_cluster_offset = cluster_offset;
1943 if (prev_cluster_time != GST_CLOCK_TIME_NONE) {
1944 /* prev cluster did not overshoot, so prev cluster is target */
1947 /* re-estimate using this new position info */
1948 opos = cluster_offset;
1949 otime = cluster_time;
1953 /* cluster undershoots, goto next one */
1954 prev_cluster_time = cluster_time;
1955 prev_cluster_offset = cluster_offset;
1956 /* skip cluster if length is defined,
1957 * otherwise will be skippingly parsed into */
1959 GST_DEBUG_OBJECT (demux, "skipping to next cluster");
1960 demux->common.offset = cluster_offset + cluster_size;
1961 demux->cluster_time = GST_CLOCK_TIME_NONE;
1963 GST_DEBUG_OBJECT (demux, "parsing/skipping cluster elements");
1970 if (ret == GST_FLOW_EOS) {
1971 if (prev_cluster_time != GST_CLOCK_TIME_NONE)
1977 entry = g_new0 (GstMatroskaIndex, 1);
1978 entry->time = prev_cluster_time;
1979 entry->pos = prev_cluster_offset - demux->common.ebml_segment_start;
1980 GST_DEBUG_OBJECT (demux, "simulated index entry; time %" GST_TIME_FORMAT
1981 ", pos %" G_GUINT64_FORMAT, GST_TIME_ARGS (entry->time), entry->pos);
1985 /* restore some state */
1986 demux->cluster_offset = current_cluster_offset;
1987 demux->cluster_time = current_cluster_time;
1988 demux->common.offset = current_offset;
1989 demux->common.state = current_state;
1995 gst_matroska_demux_handle_seek_event (GstMatroskaDemux * demux,
1996 GstPad * pad, GstEvent * event)
1998 GstMatroskaIndex *entry = NULL;
1999 GstMatroskaIndex scan_entry;
2001 GstSeekType cur_type, stop_type;
2003 gboolean flush, keyunit, before, after, snap_next;
2006 GstMatroskaTrackContext *track = NULL;
2007 GstSegment seeksegment = { 0, };
2008 gboolean update = TRUE;
2009 gboolean pad_locked = FALSE;
2011 GstSearchMode snap_dir;
2013 g_return_val_if_fail (event != NULL, FALSE);
2016 track = gst_pad_get_element_private (pad);
2018 GST_DEBUG_OBJECT (demux, "Have seek %" GST_PTR_FORMAT, event);
2020 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
2022 seqnum = gst_event_get_seqnum (event);
2024 /* we can only seek on time */
2025 if (format != GST_FORMAT_TIME) {
2026 GST_DEBUG_OBJECT (demux, "Can only seek on TIME");
2030 /* copy segment, we need this because we still need the old
2031 * segment when we close the current segment. */
2032 memcpy (&seeksegment, &demux->common.segment, sizeof (GstSegment));
2034 /* pull mode without index means that the actual duration is not known,
2035 * we might be playing a file that's still being recorded
2036 * so, invalidate our current duration, which is only a moving target,
2037 * and should not be used to clamp anything */
2038 if (!demux->streaming && !demux->common.index && demux->invalid_duration) {
2039 seeksegment.duration = GST_CLOCK_TIME_NONE;
2042 GST_DEBUG_OBJECT (demux, "configuring seek");
2043 /* Subtract stream_start_time so we always seek on a segment
2045 if (GST_CLOCK_TIME_IS_VALID (demux->stream_start_time)) {
2046 seeksegment.start -= demux->stream_start_time;
2047 seeksegment.position -= demux->stream_start_time;
2048 if (GST_CLOCK_TIME_IS_VALID (seeksegment.stop))
2049 seeksegment.stop -= demux->stream_start_time;
2051 seeksegment.stop = seeksegment.duration;
2054 gst_segment_do_seek (&seeksegment, rate, format, flags,
2055 cur_type, cur, stop_type, stop, &update);
2057 /* Restore the clip timestamp offset */
2058 if (GST_CLOCK_TIME_IS_VALID (demux->stream_start_time)) {
2059 seeksegment.position += demux->stream_start_time;
2060 seeksegment.start += demux->stream_start_time;
2061 if (!GST_CLOCK_TIME_IS_VALID (seeksegment.stop))
2062 seeksegment.stop = seeksegment.duration;
2063 if (GST_CLOCK_TIME_IS_VALID (seeksegment.stop))
2064 seeksegment.stop += demux->stream_start_time;
2067 /* restore segment duration (if any effect),
2068 * would be determined again when parsing, but anyway ... */
2069 seeksegment.duration = demux->common.segment.duration;
2071 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
2072 keyunit = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
2073 after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
2074 before = ! !(flags & GST_SEEK_FLAG_SNAP_BEFORE);
2076 /* always do full update if flushing,
2077 * otherwise problems might arise downstream with missing keyframes etc */
2078 update = update || flush;
2080 GST_DEBUG_OBJECT (demux, "New segment %" GST_SEGMENT_FORMAT, &seeksegment);
2082 /* check sanity before we start flushing and all that */
2083 snap_next = after && !before;
2084 if (seeksegment.rate < 0)
2085 snap_dir = snap_next ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER;
2087 snap_dir = snap_next ? GST_SEARCH_MODE_AFTER : GST_SEARCH_MODE_BEFORE;
2089 GST_OBJECT_LOCK (demux);
2090 track = gst_matroska_read_common_get_seek_track (&demux->common, track);
2091 if ((entry = gst_matroska_read_common_do_index_seek (&demux->common, track,
2092 seeksegment.position, &demux->seek_index, &demux->seek_entry,
2093 snap_dir)) == NULL) {
2094 /* pull mode without index can scan later on */
2095 if (demux->streaming) {
2096 GST_DEBUG_OBJECT (demux, "No matching seek entry in index");
2097 GST_OBJECT_UNLOCK (demux);
2099 } else if (rate < 0.0) {
2100 /* FIXME: We should build an index during playback or when scanning
2101 * that can be used here. The reverse playback code requires seek_index
2102 * and seek_entry to be set!
2104 GST_DEBUG_OBJECT (demux,
2105 "No matching seek entry in index, needed for reverse playback");
2106 GST_OBJECT_UNLOCK (demux);
2110 GST_DEBUG_OBJECT (demux, "Seek position looks sane");
2111 GST_OBJECT_UNLOCK (demux);
2114 /* only have to update some segment,
2115 * but also still have to honour flush and so on */
2116 GST_DEBUG_OBJECT (demux, "... no update");
2117 /* bad goto, bad ... */
2121 if (demux->streaming)
2126 GstEvent *flush_event = gst_event_new_flush_start ();
2127 gst_event_set_seqnum (flush_event, seqnum);
2128 GST_DEBUG_OBJECT (demux, "Starting flush");
2129 gst_pad_push_event (demux->common.sinkpad, gst_event_ref (flush_event));
2130 gst_matroska_demux_send_event (demux, flush_event);
2132 GST_DEBUG_OBJECT (demux, "Non-flushing seek, pausing task");
2133 gst_pad_pause_task (demux->common.sinkpad);
2137 GST_PAD_STREAM_LOCK (demux->common.sinkpad);
2142 /* now grab the stream lock so that streaming cannot continue, for
2143 * non flushing seeks when the element is in PAUSED this could block
2145 GST_DEBUG_OBJECT (demux, "Waiting for streaming to stop");
2146 GST_PAD_STREAM_LOCK (demux->common.sinkpad);
2149 /* pull mode without index can do some scanning */
2150 if (!demux->streaming && !entry) {
2151 GstEvent *flush_event;
2153 /* need to stop flushing upstream as we need it next */
2155 flush_event = gst_event_new_flush_stop (TRUE);
2156 gst_event_set_seqnum (flush_event, seqnum);
2157 gst_pad_push_event (demux->common.sinkpad, flush_event);
2159 entry = gst_matroska_demux_search_pos (demux, seeksegment.position);
2160 /* keep local copy */
2162 scan_entry = *entry;
2164 entry = &scan_entry;
2166 GST_DEBUG_OBJECT (demux, "Scan failed to find matching position");
2168 flush_event = gst_event_new_flush_stop (TRUE);
2169 gst_event_set_seqnum (flush_event, seqnum);
2170 gst_matroska_demux_send_event (demux, flush_event);
2177 if (keyunit && seeksegment.rate > 0) {
2178 GST_DEBUG_OBJECT (demux, "seek to key unit, adjusting segment start from %"
2179 GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
2180 GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (entry->time));
2181 seeksegment.start = MAX (entry->time, demux->stream_start_time);
2182 seeksegment.position = seeksegment.start;
2183 seeksegment.time = seeksegment.start - demux->stream_start_time;
2184 } else if (keyunit) {
2185 GST_DEBUG_OBJECT (demux, "seek to key unit, adjusting segment stop from %"
2186 GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
2187 GST_TIME_ARGS (seeksegment.stop), GST_TIME_ARGS (entry->time));
2188 seeksegment.stop = MAX (entry->time, demux->stream_start_time);
2189 seeksegment.position = seeksegment.stop;
2192 if (demux->streaming) {
2193 GST_OBJECT_LOCK (demux);
2194 /* track real position we should start at */
2195 GST_DEBUG_OBJECT (demux, "storing segment start");
2196 demux->requested_seek_time = seeksegment.position;
2197 demux->seek_offset = entry->pos + demux->common.ebml_segment_start;
2198 GST_OBJECT_UNLOCK (demux);
2199 /* need to seek to cluster start to pick up cluster time */
2200 /* upstream takes care of flushing and all that
2201 * ... and newsegment event handling takes care of the rest */
2202 return perform_seek_to_offset (demux, rate,
2203 entry->pos + demux->common.ebml_segment_start, seqnum, flags);
2208 GstEvent *flush_event = gst_event_new_flush_stop (TRUE);
2209 gst_event_set_seqnum (flush_event, seqnum);
2210 GST_DEBUG_OBJECT (demux, "Stopping flush");
2211 gst_pad_push_event (demux->common.sinkpad, gst_event_ref (flush_event));
2212 gst_matroska_demux_send_event (demux, flush_event);
2215 GST_OBJECT_LOCK (demux);
2216 /* now update the real segment info */
2217 GST_DEBUG_OBJECT (demux, "Committing new seek segment");
2218 memcpy (&demux->common.segment, &seeksegment, sizeof (GstSegment));
2219 GST_OBJECT_UNLOCK (demux);
2221 /* update some (segment) state */
2222 if (!gst_matroska_demux_move_to_entry (demux, entry, TRUE, update))
2225 /* notify start of new segment */
2226 if (demux->common.segment.flags & GST_SEEK_FLAG_SEGMENT) {
2229 msg = gst_message_new_segment_start (GST_OBJECT (demux),
2230 GST_FORMAT_TIME, demux->common.segment.start);
2231 gst_message_set_seqnum (msg, seqnum);
2232 gst_element_post_message (GST_ELEMENT (demux), msg);
2235 GST_OBJECT_LOCK (demux);
2236 if (demux->new_segment)
2237 gst_event_unref (demux->new_segment);
2239 /* On port from 0.10, discarded !update (for segment.update) here, FIXME? */
2240 demux->new_segment = gst_event_new_segment (&demux->common.segment);
2241 gst_event_set_seqnum (demux->new_segment, seqnum);
2242 if (demux->common.segment.rate < 0 && demux->common.segment.stop == -1)
2243 demux->to_time = demux->common.segment.position;
2245 demux->to_time = GST_CLOCK_TIME_NONE;
2246 demux->segment_seqnum = seqnum;
2247 GST_OBJECT_UNLOCK (demux);
2249 /* restart our task since it might have been stopped when we did the
2251 gst_pad_start_task (demux->common.sinkpad,
2252 (GstTaskFunction) gst_matroska_demux_loop, demux->common.sinkpad, NULL);
2254 /* streaming can continue now */
2256 GST_PAD_STREAM_UNLOCK (demux->common.sinkpad);
2264 GST_PAD_STREAM_UNLOCK (demux->common.sinkpad);
2266 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL), ("Got a seek error"));
2272 * Handle whether we can perform the seek event or if we have to let the chain
2273 * function handle seeks to build the seek indexes first.
2276 gst_matroska_demux_handle_seek_push (GstMatroskaDemux * demux, GstPad * pad,
2280 GstSeekType cur_type, stop_type;
2285 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
2290 /* we can only seek on time */
2291 if (format != GST_FORMAT_TIME) {
2292 GST_DEBUG_OBJECT (demux, "Can only seek on TIME");
2296 if (stop_type != GST_SEEK_TYPE_NONE && stop != GST_CLOCK_TIME_NONE) {
2297 GST_DEBUG_OBJECT (demux, "Seek end-time not supported in streaming mode");
2301 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
2302 GST_DEBUG_OBJECT (demux,
2303 "Non-flushing seek not supported in streaming mode");
2307 if (flags & GST_SEEK_FLAG_SEGMENT) {
2308 GST_DEBUG_OBJECT (demux, "Segment seek not supported in streaming mode");
2312 /* check for having parsed index already */
2313 if (!demux->common.index_parsed) {
2314 gboolean building_index;
2317 if (!demux->index_offset) {
2318 GST_DEBUG_OBJECT (demux, "no index (location); no seek in push mode");
2322 GST_OBJECT_LOCK (demux);
2323 /* handle the seek event in the chain function */
2324 demux->common.state = GST_MATROSKA_READ_STATE_SEEK;
2325 /* no more seek can be issued until state reset to _DATA */
2327 /* copy the event */
2328 if (demux->seek_event)
2329 gst_event_unref (demux->seek_event);
2330 demux->seek_event = gst_event_ref (event);
2332 /* set the building_index flag so that only one thread can setup the
2333 * structures for index seeking. */
2334 building_index = demux->building_index;
2335 if (!building_index) {
2336 demux->building_index = TRUE;
2337 offset = demux->index_offset;
2339 GST_OBJECT_UNLOCK (demux);
2341 if (!building_index) {
2342 /* seek to the first subindex or legacy index */
2343 GST_INFO_OBJECT (demux, "Seeking to Cues at %" G_GUINT64_FORMAT, offset);
2344 return perform_seek_to_offset (demux, rate, offset,
2345 gst_event_get_seqnum (event), GST_SEEK_FLAG_NONE);
2348 /* well, we are handling it already */
2352 /* delegate to tweaked regular seek */
2353 return gst_matroska_demux_handle_seek_event (demux, pad, event);
2357 gst_matroska_demux_handle_src_event (GstPad * pad, GstObject * parent,
2360 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
2361 gboolean res = TRUE;
2363 switch (GST_EVENT_TYPE (event)) {
2364 case GST_EVENT_SEEK:
2365 /* no seeking until we are (safely) ready */
2366 if (demux->common.state != GST_MATROSKA_READ_STATE_DATA) {
2367 GST_DEBUG_OBJECT (demux, "not ready for seeking yet");
2368 gst_event_unref (event);
2373 guint32 seqnum = gst_event_get_seqnum (event);
2374 if (seqnum == demux->segment_seqnum) {
2375 GST_LOG_OBJECT (pad,
2376 "Drop duplicated SEEK event seqnum %" G_GUINT32_FORMAT, seqnum);
2377 gst_event_unref (event);
2382 if (!demux->streaming)
2383 res = gst_matroska_demux_handle_seek_event (demux, pad, event);
2385 res = gst_matroska_demux_handle_seek_push (demux, pad, event);
2386 gst_event_unref (event);
2391 GstMatroskaTrackContext *context = gst_pad_get_element_private (pad);
2392 if (context->type == GST_MATROSKA_TRACK_TYPE_VIDEO) {
2393 GstMatroskaTrackVideoContext *videocontext =
2394 (GstMatroskaTrackVideoContext *) context;
2396 GstClockTimeDiff diff;
2397 GstClockTime timestamp;
2399 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
2401 GST_OBJECT_LOCK (demux);
2402 videocontext->earliest_time = timestamp + diff;
2403 GST_OBJECT_UNLOCK (demux);
2406 gst_event_unref (event);
2410 case GST_EVENT_TOC_SELECT:
2413 GstTocEntry *entry = NULL;
2414 GstEvent *seek_event;
2417 if (!demux->common.toc) {
2418 GST_DEBUG_OBJECT (demux, "no TOC to select");
2421 gst_event_parse_toc_select (event, &uid);
2423 GST_OBJECT_LOCK (demux);
2424 entry = gst_toc_find_entry (demux->common.toc, uid);
2425 if (entry == NULL) {
2426 GST_OBJECT_UNLOCK (demux);
2427 GST_WARNING_OBJECT (demux, "no TOC entry with given UID: %s", uid);
2430 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2431 GST_OBJECT_UNLOCK (demux);
2432 seek_event = gst_event_new_seek (1.0,
2434 GST_SEEK_FLAG_FLUSH,
2435 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2436 res = gst_matroska_demux_handle_seek_event (demux, pad, seek_event);
2437 gst_event_unref (seek_event);
2441 GST_WARNING_OBJECT (demux, "received empty TOC select event");
2445 gst_event_unref (event);
2449 /* events we don't need to handle */
2450 case GST_EVENT_NAVIGATION:
2451 gst_event_unref (event);
2455 case GST_EVENT_LATENCY:
2457 res = gst_pad_push_event (demux->common.sinkpad, event);
2464 static GstFlowReturn
2465 gst_matroska_demux_seek_to_previous_keyframe (GstMatroskaDemux * demux)
2467 GstFlowReturn ret = GST_FLOW_EOS;
2468 gboolean done = TRUE;
2471 g_return_val_if_fail (demux->seek_index, GST_FLOW_EOS);
2472 g_return_val_if_fail (demux->seek_entry < demux->seek_index->len,
2475 GST_DEBUG_OBJECT (demux, "locating previous keyframe");
2477 if (!demux->seek_entry) {
2478 GST_DEBUG_OBJECT (demux, "no earlier index entry");
2482 for (i = 0; i < demux->common.src->len; i++) {
2483 GstMatroskaTrackContext *stream = g_ptr_array_index (demux->common.src, i);
2485 GST_DEBUG_OBJECT (demux, "segment start %" GST_TIME_FORMAT
2486 ", stream %d at %" GST_TIME_FORMAT,
2487 GST_TIME_ARGS (demux->common.segment.start), stream->index,
2488 GST_TIME_ARGS (stream->from_time));
2489 if (GST_CLOCK_TIME_IS_VALID (stream->from_time)) {
2490 if (stream->from_time > demux->common.segment.start) {
2491 GST_DEBUG_OBJECT (demux, "stream %d not finished yet", stream->index);
2495 /* nothing pushed for this stream;
2496 * likely seek entry did not start at keyframe, so all was skipped.
2497 * So we need an earlier entry */
2503 GstMatroskaIndex *entry;
2505 entry = &g_array_index (demux->seek_index, GstMatroskaIndex,
2506 --demux->seek_entry);
2507 if (!gst_matroska_demux_move_to_entry (demux, entry, FALSE, TRUE))
2517 static GstFlowReturn
2518 gst_matroska_demux_parse_tracks (GstMatroskaDemux * demux, GstEbmlRead * ebml)
2520 GstFlowReturn ret = GST_FLOW_OK;
2523 DEBUG_ELEMENT_START (demux, ebml, "Tracks");
2525 if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
2526 DEBUG_ELEMENT_STOP (demux, ebml, "Tracks", ret);
2530 while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
2531 if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
2535 /* one track within the "all-tracks" header */
2536 case GST_MATROSKA_ID_TRACKENTRY:
2537 ret = gst_matroska_demux_add_stream (demux, ebml);
2541 ret = gst_matroska_read_common_parse_skip (&demux->common, ebml,
2546 DEBUG_ELEMENT_STOP (demux, ebml, "Tracks", ret);
2548 demux->tracks_parsed = TRUE;
2554 * Read signed/unsigned "EBML" numbers.
2555 * Return: number of bytes processed.
2559 gst_matroska_ebmlnum_uint (guint8 * data, guint size, guint64 * num)
2561 gint len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
2569 while (read <= 8 && !(total & len_mask)) {
2576 if ((total &= (len_mask - 1)) == len_mask - 1)
2581 if (data[n] == 0xff)
2583 total = (total << 8) | data[n];
2587 if (read == num_ffs && total != 0)
2596 gst_matroska_ebmlnum_sint (guint8 * data, guint size, gint64 * num)
2601 /* read as unsigned number first */
2602 if ((res = gst_matroska_ebmlnum_uint (data, size, &unum)) < 0)
2606 if (unum == G_MAXUINT64)
2609 *num = unum - ((1 << ((7 * res) - 1)) - 1);
2615 * Mostly used for subtitles. We add void filler data for each
2616 * lagging stream to make sure we don't deadlock.
2620 gst_matroska_demux_sync_streams (GstMatroskaDemux * demux)
2622 GstClockTime gap_threshold;
2625 GST_OBJECT_LOCK (demux);
2627 GST_LOG_OBJECT (demux, "Sync to %" GST_TIME_FORMAT,
2628 GST_TIME_ARGS (demux->common.segment.position));
2630 g_assert (demux->common.num_streams == demux->common.src->len);
2631 for (stream_nr = 0; stream_nr < demux->common.src->len; stream_nr++) {
2632 GstMatroskaTrackContext *context;
2634 context = g_ptr_array_index (demux->common.src, stream_nr);
2636 GST_LOG_OBJECT (demux,
2637 "Checking for resync on stream %d (%" GST_TIME_FORMAT ")", stream_nr,
2638 GST_TIME_ARGS (context->pos));
2640 /* Only send gap events on non-subtitle streams if lagging way behind.
2641 * The 0.5 second threshold for subtitle streams is also quite random. */
2642 if (context->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
2643 gap_threshold = GST_SECOND / 2;
2645 gap_threshold = 3 * GST_SECOND;
2647 /* Lag need only be considered if we have advanced into requested segment */
2648 if (GST_CLOCK_TIME_IS_VALID (context->pos) &&
2649 GST_CLOCK_TIME_IS_VALID (demux->common.segment.position) &&
2650 demux->common.segment.position > demux->common.segment.start &&
2651 context->pos + gap_threshold < demux->common.segment.position) {
2654 guint64 start = context->pos;
2655 guint64 stop = demux->common.segment.position - gap_threshold;
2657 GST_DEBUG_OBJECT (demux,
2658 "Synchronizing stream %d with other by advancing time from %"
2659 GST_TIME_FORMAT " to %" GST_TIME_FORMAT, stream_nr,
2660 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
2662 context->pos = stop;
2664 event = gst_event_new_gap (start, stop - start);
2665 GST_OBJECT_UNLOCK (demux);
2666 gst_pad_push_event (context->pad, event);
2667 GST_OBJECT_LOCK (demux);
2671 GST_OBJECT_UNLOCK (demux);
2674 static GstFlowReturn
2675 gst_matroska_demux_push_stream_headers (GstMatroskaDemux * demux,
2676 GstMatroskaTrackContext * stream)
2678 GstFlowReturn ret = GST_FLOW_OK;
2681 num = gst_buffer_list_length (stream->stream_headers);
2682 for (i = 0; i < num; ++i) {
2685 buf = gst_buffer_list_get (stream->stream_headers, i);
2686 buf = gst_buffer_copy (buf);
2688 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
2690 if (stream->set_discont) {
2691 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2692 stream->set_discont = FALSE;
2694 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
2697 /* push out all headers in one go and use last flow return */
2698 ret = gst_pad_push (stream->pad, buf);
2701 /* don't need these any longer */
2702 gst_buffer_list_unref (stream->stream_headers);
2703 stream->stream_headers = NULL;
2706 ret = gst_flow_combiner_update_flow (demux->flowcombiner, ret);
2712 gst_matroska_demux_push_dvd_clut_change_event (GstMatroskaDemux * demux,
2713 GstMatroskaTrackContext * stream)
2717 g_assert (!strcmp (stream->codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_VOBSUB));
2719 if (!stream->codec_priv)
2722 /* ideally, VobSub private data should be parsed and stored more convenient
2723 * elsewhere, but for now, only interested in a small part */
2725 /* make sure we have terminating 0 */
2726 buf = g_strndup (stream->codec_priv, stream->codec_priv_size);
2728 /* just locate and parse palette part */
2729 start = strstr (buf, "palette:");
2734 guint8 r, g, b, y, u, v;
2737 while (g_ascii_isspace (*start))
2739 for (i = 0; i < 16; i++) {
2740 if (sscanf (start, "%06x", &col) != 1)
2743 while ((*start == ',') || g_ascii_isspace (*start))
2745 /* sigh, need to convert this from vobsub pseudo-RGB to YUV */
2746 r = (col >> 16) & 0xff;
2747 g = (col >> 8) & 0xff;
2749 y = CLAMP ((0.1494 * r + 0.6061 * g + 0.2445 * b) * 219 / 255 + 16, 0,
2751 u = CLAMP (0.6066 * r - 0.4322 * g - 0.1744 * b + 128, 0, 255);
2752 v = CLAMP (-0.08435 * r - 0.3422 * g + 0.4266 * b + 128, 0, 255);
2753 clut[i] = (y << 16) | (u << 8) | v;
2756 /* got them all without problems; build and send event */
2760 s = gst_structure_new ("application/x-gst-dvd", "event", G_TYPE_STRING,
2761 "dvd-spu-clut-change", "clut00", G_TYPE_INT, clut[0], "clut01",
2762 G_TYPE_INT, clut[1], "clut02", G_TYPE_INT, clut[2], "clut03",
2763 G_TYPE_INT, clut[3], "clut04", G_TYPE_INT, clut[4], "clut05",
2764 G_TYPE_INT, clut[5], "clut06", G_TYPE_INT, clut[6], "clut07",
2765 G_TYPE_INT, clut[7], "clut08", G_TYPE_INT, clut[8], "clut09",
2766 G_TYPE_INT, clut[9], "clut10", G_TYPE_INT, clut[10], "clut11",
2767 G_TYPE_INT, clut[11], "clut12", G_TYPE_INT, clut[12], "clut13",
2768 G_TYPE_INT, clut[13], "clut14", G_TYPE_INT, clut[14], "clut15",
2769 G_TYPE_INT, clut[15], NULL);
2771 gst_pad_push_event (stream->pad,
2772 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s));
2779 gst_matroska_demux_push_codec_data_all (GstMatroskaDemux * demux)
2783 g_assert (demux->common.num_streams == demux->common.src->len);
2784 for (stream_nr = 0; stream_nr < demux->common.src->len; stream_nr++) {
2785 GstMatroskaTrackContext *stream;
2787 stream = g_ptr_array_index (demux->common.src, stream_nr);
2789 if (stream->send_stream_headers) {
2790 if (stream->stream_headers != NULL) {
2791 gst_matroska_demux_push_stream_headers (demux, stream);
2793 /* FIXME: perhaps we can just disable and skip this stream then */
2794 GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
2795 ("Failed to extract stream headers from codec private data"));
2797 stream->send_stream_headers = FALSE;
2800 if (stream->send_dvd_event) {
2801 gst_matroska_demux_push_dvd_clut_change_event (demux, stream);
2802 /* FIXME: should we send this event again after (flushing) seek ? */
2803 stream->send_dvd_event = FALSE;
2809 static GstFlowReturn
2810 gst_matroska_demux_add_mpeg_seq_header (GstElement * element,
2811 GstMatroskaTrackContext * stream, GstBuffer ** buf)
2814 guint seq_header_len;
2815 guint32 header, tmp;
2817 if (stream->codec_state) {
2818 seq_header = stream->codec_state;
2819 seq_header_len = stream->codec_state_size;
2820 } else if (stream->codec_priv) {
2821 seq_header = stream->codec_priv;
2822 seq_header_len = stream->codec_priv_size;
2827 /* Sequence header only needed for keyframes */
2828 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_DELTA_UNIT))
2831 if (gst_buffer_get_size (*buf) < 4)
2834 gst_buffer_extract (*buf, 0, &tmp, sizeof (guint32));
2835 header = GUINT32_FROM_BE (tmp);
2837 /* Sequence start code, if not found prepend */
2838 if (header != 0x000001b3) {
2841 GST_DEBUG_OBJECT (element, "Prepending MPEG sequence header");
2843 newbuf = gst_buffer_new_wrapped (g_memdup (seq_header, seq_header_len),
2846 gst_buffer_copy_into (newbuf, *buf, GST_BUFFER_COPY_TIMESTAMPS |
2847 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_MEMORY, 0,
2848 gst_buffer_get_size (*buf));
2850 gst_buffer_unref (*buf);
2857 static GstFlowReturn
2858 gst_matroska_demux_add_wvpk_header (GstElement * element,
2859 GstMatroskaTrackContext * stream, GstBuffer ** buf)
2861 GstMatroskaTrackAudioContext *audiocontext =
2862 (GstMatroskaTrackAudioContext *) stream;
2863 GstBuffer *newbuf = NULL;
2864 GstMapInfo map, outmap;
2865 guint8 *buf_data, *data;
2873 wvh.version = GST_READ_UINT16_LE (stream->codec_priv);
2876 wvh.total_samples = -1;
2877 wvh.block_index = audiocontext->wvpk_block_index;
2879 if (audiocontext->channels <= 2) {
2880 guint32 block_samples, tmp;
2881 gsize size = gst_buffer_get_size (*buf);
2883 gst_buffer_extract (*buf, 0, &tmp, sizeof (guint32));
2884 block_samples = GUINT32_FROM_LE (tmp);
2885 /* we need to reconstruct the header of the wavpack block */
2887 /* -20 because ck_size is the size of the wavpack block -8
2888 * and lace_size is the size of the wavpack block + 12
2889 * (the three guint32 of the header that already are in the buffer) */
2890 wvh.ck_size = size + sizeof (Wavpack4Header) - 20;
2892 /* block_samples, flags and crc are already in the buffer */
2893 newbuf = gst_buffer_new_allocate (NULL, sizeof (Wavpack4Header) - 12, NULL);
2895 gst_buffer_map (newbuf, &outmap, GST_MAP_WRITE);
2901 GST_WRITE_UINT32_LE (data + 4, wvh.ck_size);
2902 GST_WRITE_UINT16_LE (data + 8, wvh.version);
2903 GST_WRITE_UINT8 (data + 10, wvh.track_no);
2904 GST_WRITE_UINT8 (data + 11, wvh.index_no);
2905 GST_WRITE_UINT32_LE (data + 12, wvh.total_samples);
2906 GST_WRITE_UINT32_LE (data + 16, wvh.block_index);
2907 gst_buffer_unmap (newbuf, &outmap);
2909 /* Append data from buf: */
2910 gst_buffer_copy_into (newbuf, *buf, GST_BUFFER_COPY_TIMESTAMPS |
2911 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_MEMORY, 0, size);
2913 gst_buffer_unref (*buf);
2915 audiocontext->wvpk_block_index += block_samples;
2917 guint8 *outdata = NULL;
2919 gsize buf_size, size, out_size = 0;
2920 guint32 block_samples, flags, crc, blocksize;
2922 gst_buffer_map (*buf, &map, GST_MAP_READ);
2923 buf_data = map.data;
2924 buf_size = map.size;
2927 GST_ERROR_OBJECT (element, "Too small wavpack buffer");
2928 gst_buffer_unmap (*buf, &map);
2929 return GST_FLOW_ERROR;
2935 block_samples = GST_READ_UINT32_LE (data);
2940 flags = GST_READ_UINT32_LE (data);
2943 crc = GST_READ_UINT32_LE (data);
2946 blocksize = GST_READ_UINT32_LE (data);
2950 if (blocksize == 0 || size < blocksize)
2953 g_assert ((newbuf == NULL) == (outdata == NULL));
2955 if (newbuf == NULL) {
2956 out_size = sizeof (Wavpack4Header) + blocksize;
2957 newbuf = gst_buffer_new_allocate (NULL, out_size, NULL);
2959 gst_buffer_copy_into (newbuf, *buf,
2960 GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS, 0, -1);
2963 gst_buffer_map (newbuf, &outmap, GST_MAP_WRITE);
2964 outdata = outmap.data;
2966 gst_buffer_unmap (newbuf, &outmap);
2967 out_size += sizeof (Wavpack4Header) + blocksize;
2968 gst_buffer_set_size (newbuf, out_size);
2969 gst_buffer_map (newbuf, &outmap, GST_MAP_WRITE);
2970 outdata = outmap.data;
2973 outdata[outpos] = 'w';
2974 outdata[outpos + 1] = 'v';
2975 outdata[outpos + 2] = 'p';
2976 outdata[outpos + 3] = 'k';
2979 GST_WRITE_UINT32_LE (outdata + outpos,
2980 blocksize + sizeof (Wavpack4Header) - 8);
2981 GST_WRITE_UINT16_LE (outdata + outpos + 4, wvh.version);
2982 GST_WRITE_UINT8 (outdata + outpos + 6, wvh.track_no);
2983 GST_WRITE_UINT8 (outdata + outpos + 7, wvh.index_no);
2984 GST_WRITE_UINT32_LE (outdata + outpos + 8, wvh.total_samples);
2985 GST_WRITE_UINT32_LE (outdata + outpos + 12, wvh.block_index);
2986 GST_WRITE_UINT32_LE (outdata + outpos + 16, block_samples);
2987 GST_WRITE_UINT32_LE (outdata + outpos + 20, flags);
2988 GST_WRITE_UINT32_LE (outdata + outpos + 24, crc);
2991 memmove (outdata + outpos, data, blocksize);
2992 outpos += blocksize;
2996 gst_buffer_unmap (*buf, &map);
2997 gst_buffer_unref (*buf);
3000 gst_buffer_unmap (newbuf, &outmap);
3003 audiocontext->wvpk_block_index += block_samples;
3009 static GstFlowReturn
3010 gst_matroska_demux_add_prores_header (GstElement * element,
3011 GstMatroskaTrackContext * stream, GstBuffer ** buf)
3013 GstBuffer *newbuf = gst_buffer_new_allocate (NULL, 8, NULL);
3017 if (!gst_buffer_map (newbuf, &map, GST_MAP_WRITE)) {
3018 GST_ERROR ("Failed to map newly allocated buffer");
3019 return GST_FLOW_ERROR;
3022 frame_size = gst_buffer_get_size (*buf);
3024 GST_WRITE_UINT32_BE (map.data, frame_size);
3030 gst_buffer_unmap (newbuf, &map);
3031 *buf = gst_buffer_append (newbuf, *buf);
3036 /* @text must be null-terminated */
3038 gst_matroska_demux_subtitle_chunk_has_tag (GstElement * element,
3043 g_return_val_if_fail (text != NULL, FALSE);
3045 /* yes, this might all lead to false positives ... */
3046 tag = (gchar *) text;
3047 while ((tag = strchr (tag, '<'))) {
3049 if (*tag != '\0' && *(tag + 1) == '>') {
3050 /* some common convenience ones */
3051 /* maybe any character will do here ? */
3064 if (strstr (text, "<span"))
3070 static GstFlowReturn
3071 gst_matroska_demux_check_subtitle_buffer (GstElement * element,
3072 GstMatroskaTrackContext * stream, GstBuffer ** buf)
3074 GstMatroskaTrackSubtitleContext *sub_stream;
3075 const gchar *encoding;
3080 gboolean needs_unmap = TRUE;
3082 sub_stream = (GstMatroskaTrackSubtitleContext *) stream;
3084 if (!gst_buffer_get_size (*buf) || !gst_buffer_map (*buf, &map, GST_MAP_READ))
3087 /* The subtitle buffer we push out should not include a NUL terminator as
3088 * part of the data. */
3089 if (map.data[map.size - 1] == '\0') {
3090 gst_buffer_set_size (*buf, map.size - 1);
3091 gst_buffer_unmap (*buf, &map);
3092 gst_buffer_map (*buf, &map, GST_MAP_READ);
3095 if (!sub_stream->invalid_utf8) {
3096 if (g_utf8_validate ((gchar *) map.data, map.size, NULL)) {
3099 GST_WARNING_OBJECT (element, "subtitle stream %" G_GUINT64_FORMAT
3100 " is not valid UTF-8, this is broken according to the matroska"
3101 " specification", stream->num);
3102 sub_stream->invalid_utf8 = TRUE;
3105 /* file with broken non-UTF8 subtitle, do the best we can do to fix it */
3106 encoding = g_getenv ("GST_SUBTITLE_ENCODING");
3107 if (encoding == NULL || *encoding == '\0') {
3108 /* if local encoding is UTF-8 and no encoding specified
3109 * via the environment variable, assume ISO-8859-15 */
3110 if (g_get_charset (&encoding)) {
3111 encoding = "ISO-8859-15";
3116 g_convert_with_fallback ((gchar *) map.data, map.size, "UTF-8", encoding,
3117 (char *) "*", NULL, NULL, &err);
3120 GST_LOG_OBJECT (element, "could not convert string from '%s' to UTF-8: %s",
3121 encoding, err->message);
3125 /* invalid input encoding, fall back to ISO-8859-15 (always succeeds) */
3126 encoding = "ISO-8859-15";
3128 g_convert_with_fallback ((gchar *) map.data, map.size, "UTF-8",
3129 encoding, (char *) "*", NULL, NULL, NULL);
3132 GST_LOG_OBJECT (element, "converted subtitle text from %s to UTF-8 %s",
3133 encoding, (err) ? "(using ISO-8859-15 as fallback)" : "");
3136 utf8 = g_strdup ("invalid subtitle");
3138 newbuf = gst_buffer_new_wrapped (utf8, strlen (utf8));
3139 gst_buffer_unmap (*buf, &map);
3140 gst_buffer_copy_into (newbuf, *buf,
3141 GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_META,
3143 gst_buffer_unref (*buf);
3146 gst_buffer_map (*buf, &map, GST_MAP_READ);
3150 if (sub_stream->check_markup) {
3151 /* caps claim markup text, so we need to escape text,
3152 * except if text is already markup and then needs no further escaping */
3153 sub_stream->seen_markup_tag = sub_stream->seen_markup_tag ||
3154 gst_matroska_demux_subtitle_chunk_has_tag (element, (gchar *) map.data);
3156 if (!sub_stream->seen_markup_tag) {
3157 utf8 = g_markup_escape_text ((gchar *) map.data, map.size);
3159 newbuf = gst_buffer_new_wrapped (utf8, strlen (utf8));
3160 gst_buffer_unmap (*buf, &map);
3161 gst_buffer_copy_into (newbuf, *buf,
3162 GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS |
3163 GST_BUFFER_COPY_META, 0, -1);
3164 gst_buffer_unref (*buf);
3167 needs_unmap = FALSE;
3172 gst_buffer_unmap (*buf, &map);
3177 static GstFlowReturn
3178 gst_matroska_demux_check_aac (GstElement * element,
3179 GstMatroskaTrackContext * stream, GstBuffer ** buf)
3184 gst_buffer_extract (*buf, 0, data, 2);
3185 size = gst_buffer_get_size (*buf);
3187 if (size > 2 && data[0] == 0xff && (data[1] >> 4 == 0x0f)) {
3190 /* tss, ADTS data, remove codec_data
3191 * still assume it is at least parsed */
3192 stream->caps = gst_caps_make_writable (stream->caps);
3193 s = gst_caps_get_structure (stream->caps, 0);
3195 gst_structure_remove_field (s, "codec_data");
3196 gst_pad_set_caps (stream->pad, stream->caps);
3197 GST_DEBUG_OBJECT (element, "ADTS AAC audio data; removing codec-data, "
3198 "new caps: %" GST_PTR_FORMAT, stream->caps);
3201 /* disable subsequent checking */
3202 stream->postprocess_frame = NULL;
3208 gst_matroska_demux_align_buffer (GstMatroskaDemux * demux,
3209 GstBuffer * buffer, gsize alignment)
3213 gst_buffer_map (buffer, &map, GST_MAP_READ);
3215 if (map.size < sizeof (guintptr)) {
3216 gst_buffer_unmap (buffer, &map);
3220 if (((guintptr) map.data) & (alignment - 1)) {
3221 GstBuffer *new_buffer;
3222 GstAllocationParams params = { 0, alignment - 1, 0, 0, };
3224 new_buffer = gst_buffer_new_allocate (NULL,
3225 gst_buffer_get_size (buffer), ¶ms);
3227 /* Copy data "by hand", so ensure alignment is kept: */
3228 gst_buffer_fill (new_buffer, 0, map.data, map.size);
3230 gst_buffer_copy_into (new_buffer, buffer, GST_BUFFER_COPY_METADATA, 0, -1);
3231 GST_DEBUG_OBJECT (demux,
3232 "We want output aligned on %" G_GSIZE_FORMAT ", reallocated",
3235 gst_buffer_unmap (buffer, &map);
3236 gst_buffer_unref (buffer);
3241 gst_buffer_unmap (buffer, &map);
3245 static GstFlowReturn
3246 gst_matroska_demux_parse_blockgroup_or_simpleblock (GstMatroskaDemux * demux,
3247 GstEbmlRead * ebml, guint64 cluster_time, guint64 cluster_offset,
3248 gboolean is_simpleblock)
3250 GstMatroskaTrackContext *stream = NULL;
3251 GstFlowReturn ret = GST_FLOW_OK;
3252 gboolean readblock = FALSE;
3254 guint64 block_duration = -1;
3255 gint64 block_discardpadding = 0;
3256 GstBuffer *buf = NULL;
3258 gint stream_num = -1, n, laces = 0;
3260 gint *lace_size = NULL;
3263 gint64 referenceblock = 0;
3265 GstClockTime buffer_timestamp;
3267 offset = gst_ebml_read_get_offset (ebml);
3269 while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
3270 if (!is_simpleblock) {
3271 if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK) {
3275 id = GST_MATROSKA_ID_SIMPLEBLOCK;
3279 /* one block inside the group. Note, block parsing is one
3280 * of the harder things, so this code is a bit complicated.
3281 * See http://www.matroska.org/ for documentation. */
3282 case GST_MATROSKA_ID_SIMPLEBLOCK:
3283 case GST_MATROSKA_ID_BLOCK:
3289 gst_buffer_unmap (buf, &map);
3290 gst_buffer_unref (buf);
3293 if ((ret = gst_ebml_read_buffer (ebml, &id, &buf)) != GST_FLOW_OK)
3296 gst_buffer_map (buf, &map, GST_MAP_READ);
3300 /* first byte(s): blocknum */
3301 if ((n = gst_matroska_ebmlnum_uint (data, size, &num)) < 0)
3306 /* fetch stream from num */
3307 stream_num = gst_matroska_read_common_stream_from_num (&demux->common,
3309 if (G_UNLIKELY (size < 3)) {
3310 GST_WARNING_OBJECT (demux, "Invalid size %u", size);
3311 /* non-fatal, try next block(group) */
3314 } else if (G_UNLIKELY (stream_num < 0 ||
3315 stream_num >= demux->common.num_streams)) {
3316 /* let's not give up on a stray invalid track number */
3317 GST_WARNING_OBJECT (demux,
3318 "Invalid stream %d for track number %" G_GUINT64_FORMAT
3319 "; ignoring block", stream_num, num);
3323 stream = g_ptr_array_index (demux->common.src, stream_num);
3325 /* time (relative to cluster time) */
3326 time = ((gint16) GST_READ_UINT16_BE (data));
3329 flags = GST_READ_UINT8 (data);
3333 GST_LOG_OBJECT (demux, "time %" G_GUINT64_FORMAT ", flags %d", time,
3336 switch ((flags & 0x06) >> 1) {
3337 case 0x0: /* no lacing */
3339 lace_size = g_new (gint, 1);
3340 lace_size[0] = size;
3343 case 0x1: /* xiph lacing */
3344 case 0x2: /* fixed-size lacing */
3345 case 0x3: /* EBML lacing */
3347 goto invalid_lacing;
3348 laces = GST_READ_UINT8 (data) + 1;
3351 lace_size = g_new0 (gint, laces);
3353 switch ((flags & 0x06) >> 1) {
3354 case 0x1: /* xiph lacing */ {
3355 guint temp, total = 0;
3357 for (n = 0; ret == GST_FLOW_OK && n < laces - 1; n++) {
3360 goto invalid_lacing;
3361 temp = GST_READ_UINT8 (data);
3362 lace_size[n] += temp;
3368 total += lace_size[n];
3370 lace_size[n] = size - total;
3374 case 0x2: /* fixed-size lacing */
3375 for (n = 0; n < laces; n++)
3376 lace_size[n] = size / laces;
3379 case 0x3: /* EBML lacing */ {
3382 if ((n = gst_matroska_ebmlnum_uint (data, size, &num)) < 0)
3386 total = lace_size[0] = num;
3387 for (n = 1; ret == GST_FLOW_OK && n < laces - 1; n++) {
3391 if ((r = gst_matroska_ebmlnum_sint (data, size, &snum)) < 0)
3395 lace_size[n] = lace_size[n - 1] + snum;
3396 total += lace_size[n];
3399 lace_size[n] = size - total;
3406 if (ret != GST_FLOW_OK)
3413 case GST_MATROSKA_ID_BLOCKDURATION:{
3414 ret = gst_ebml_read_uint (ebml, &id, &block_duration);
3415 GST_DEBUG_OBJECT (demux, "BlockDuration: %" G_GUINT64_FORMAT,
3420 case GST_MATROSKA_ID_DISCARDPADDING:{
3421 ret = gst_ebml_read_sint (ebml, &id, &block_discardpadding);
3422 GST_DEBUG_OBJECT (demux, "DiscardPadding: %" GST_STIME_FORMAT,
3423 GST_STIME_ARGS (block_discardpadding));
3427 case GST_MATROSKA_ID_REFERENCEBLOCK:{
3428 ret = gst_ebml_read_sint (ebml, &id, &referenceblock);
3429 GST_DEBUG_OBJECT (demux, "ReferenceBlock: %" G_GINT64_FORMAT,
3434 case GST_MATROSKA_ID_CODECSTATE:{
3436 guint64 data_len = 0;
3439 gst_ebml_read_binary (ebml, &id, &data,
3440 &data_len)) != GST_FLOW_OK)
3443 if (G_UNLIKELY (stream == NULL)) {
3444 GST_WARNING_OBJECT (demux,
3445 "Unexpected CodecState subelement - ignoring");
3449 g_free (stream->codec_state);
3450 stream->codec_state = data;
3451 stream->codec_state_size = data_len;
3453 /* Decode if necessary */
3454 if (stream->encodings && stream->encodings->len > 0
3455 && stream->codec_state && stream->codec_state_size > 0) {
3456 if (!gst_matroska_decode_data (stream->encodings,
3457 &stream->codec_state, &stream->codec_state_size,
3458 GST_MATROSKA_TRACK_ENCODING_SCOPE_CODEC_DATA, TRUE)) {
3459 GST_WARNING_OBJECT (demux, "Decoding codec state failed");
3463 GST_DEBUG_OBJECT (demux, "CodecState of %" G_GSIZE_FORMAT " bytes",
3464 stream->codec_state_size);
3469 ret = gst_matroska_read_common_parse_skip (&demux->common, ebml,
3473 case GST_MATROSKA_ID_BLOCKVIRTUAL:
3474 case GST_MATROSKA_ID_BLOCKADDITIONS:
3475 case GST_MATROSKA_ID_REFERENCEPRIORITY:
3476 case GST_MATROSKA_ID_REFERENCEVIRTUAL:
3477 case GST_MATROSKA_ID_SLICES:
3478 GST_DEBUG_OBJECT (demux,
3479 "Skipping BlockGroup subelement 0x%x - ignoring", id);
3480 ret = gst_ebml_read_skip (ebml);
3488 /* reading a number or so could have failed */
3489 if (ret != GST_FLOW_OK)
3492 if (ret == GST_FLOW_OK && readblock) {
3493 gboolean invisible_frame = FALSE;
3494 gboolean delta_unit = FALSE;
3495 guint64 duration = 0;
3496 gint64 lace_time = 0;
3498 stream = g_ptr_array_index (demux->common.src, stream_num);
3500 if (cluster_time != GST_CLOCK_TIME_NONE) {
3501 /* FIXME: What to do with negative timestamps? Give timestamp 0 or -1?
3502 * Drop unless the lace contains timestamp 0? */
3503 if (time < 0 && (-time) > cluster_time) {
3506 if (stream->timecodescale == 1.0)
3507 lace_time = (cluster_time + time) * demux->common.time_scale;
3510 gst_util_guint64_to_gdouble ((cluster_time + time) *
3511 demux->common.time_scale) * stream->timecodescale;
3514 lace_time = GST_CLOCK_TIME_NONE;
3517 /* need to refresh segment info ASAP */
3518 if (GST_CLOCK_TIME_IS_VALID (lace_time) && demux->need_segment) {
3519 GstSegment *segment = &demux->common.segment;
3521 GstEvent *segment_event;
3523 if (!GST_CLOCK_TIME_IS_VALID (demux->stream_start_time)) {
3524 demux->stream_start_time = lace_time;
3525 GST_DEBUG_OBJECT (demux,
3526 "Setting stream start time to %" GST_TIME_FORMAT,
3527 GST_TIME_ARGS (lace_time));
3529 clace_time = MAX (lace_time, demux->stream_start_time);
3530 if (GST_CLOCK_TIME_IS_VALID (demux->common.segment.position) &&
3531 demux->common.segment.position != 0) {
3532 GST_DEBUG_OBJECT (demux,
3533 "using stored seek position %" GST_TIME_FORMAT,
3534 GST_TIME_ARGS (demux->common.segment.position));
3535 clace_time = demux->common.segment.position + demux->stream_start_time;
3536 segment->position = GST_CLOCK_TIME_NONE;
3538 segment->start = clace_time;
3539 segment->stop = GST_CLOCK_TIME_NONE;
3540 segment->time = segment->start - demux->stream_start_time;
3541 segment->position = segment->start - demux->stream_start_time;
3542 GST_DEBUG_OBJECT (demux,
3543 "generated segment starting at %" GST_TIME_FORMAT ": %"
3544 GST_SEGMENT_FORMAT, GST_TIME_ARGS (lace_time), segment);
3545 /* now convey our segment notion downstream */
3546 segment_event = gst_event_new_segment (segment);
3547 if (demux->segment_seqnum)
3548 gst_event_set_seqnum (segment_event, demux->segment_seqnum);
3549 gst_matroska_demux_send_event (demux, segment_event);
3550 demux->need_segment = FALSE;
3551 demux->segment_seqnum = 0;
3554 /* send pending codec data headers for all streams,
3555 * before we perform sync across all streams */
3556 gst_matroska_demux_push_codec_data_all (demux);
3558 if (block_duration != -1) {
3559 if (stream->timecodescale == 1.0)
3560 duration = gst_util_uint64_scale (block_duration,
3561 demux->common.time_scale, 1);
3564 gst_util_gdouble_to_guint64 (gst_util_guint64_to_gdouble
3565 (gst_util_uint64_scale (block_duration, demux->common.time_scale,
3566 1)) * stream->timecodescale);
3567 } else if (stream->default_duration) {
3568 duration = stream->default_duration * laces;
3570 /* else duration is diff between timecode of this and next block */
3572 if (stream->type == GST_MATROSKA_TRACK_TYPE_VIDEO) {
3573 /* For SimpleBlock, look at the keyframe bit in flags. Otherwise,
3574 a ReferenceBlock implies that this is not a keyframe. In either
3575 case, it only makes sense for video streams. */
3576 if ((is_simpleblock && !(flags & 0x80)) || referenceblock) {
3578 invisible_frame = ((flags & 0x08)) &&
3579 (!strcmp (stream->codec_id, GST_MATROSKA_CODEC_ID_VIDEO_VP8) ||
3580 !strcmp (stream->codec_id, GST_MATROSKA_CODEC_ID_VIDEO_VP9));
3583 /* If we're doing a keyframe-only trickmode, only push keyframes on video
3587 segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
3588 GST_LOG_OBJECT (demux, "Skipping non-keyframe on stream %d",
3595 for (n = 0; n < laces; n++) {
3598 if (G_UNLIKELY (lace_size[n] > size)) {
3599 GST_WARNING_OBJECT (demux, "Invalid lace size");
3603 /* QoS for video track with an index. the assumption is that
3604 index entries point to keyframes, but if that is not true we
3605 will instad skip until the next keyframe. */
3606 if (GST_CLOCK_TIME_IS_VALID (lace_time) &&
3607 stream->type == GST_MATROSKA_TRACK_TYPE_VIDEO &&
3608 stream->index_table && demux->common.segment.rate > 0.0) {
3609 GstMatroskaTrackVideoContext *videocontext =
3610 (GstMatroskaTrackVideoContext *) stream;
3611 GstClockTime earliest_time;
3612 GstClockTime earliest_stream_time;
3614 GST_OBJECT_LOCK (demux);
3615 earliest_time = videocontext->earliest_time;
3616 GST_OBJECT_UNLOCK (demux);
3617 earliest_stream_time =
3618 gst_segment_position_from_running_time (&demux->common.segment,
3619 GST_FORMAT_TIME, earliest_time);
3621 if (GST_CLOCK_TIME_IS_VALID (lace_time) &&
3622 GST_CLOCK_TIME_IS_VALID (earliest_stream_time) &&
3623 lace_time <= earliest_stream_time) {
3624 /* find index entry (keyframe) <= earliest_stream_time */
3625 GstMatroskaIndex *entry =
3626 gst_util_array_binary_search (stream->index_table->data,
3627 stream->index_table->len, sizeof (GstMatroskaIndex),
3628 (GCompareDataFunc) gst_matroska_index_seek_find,
3629 GST_SEARCH_MODE_BEFORE, &earliest_stream_time, NULL);
3631 /* if that entry (keyframe) is after the current the current
3632 buffer, we can skip pushing (and thus decoding) all
3633 buffers until that keyframe. */
3634 if (entry && GST_CLOCK_TIME_IS_VALID (entry->time) &&
3635 entry->time > lace_time) {
3636 GST_LOG_OBJECT (demux, "Skipping lace before late keyframe");
3637 stream->set_discont = TRUE;
3643 sub = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL,
3644 gst_buffer_get_size (buf) - size, lace_size[n]);
3645 GST_DEBUG_OBJECT (demux, "created subbuffer %p", sub);
3648 GST_BUFFER_FLAG_SET (sub, GST_BUFFER_FLAG_DELTA_UNIT);
3650 GST_BUFFER_FLAG_UNSET (sub, GST_BUFFER_FLAG_DELTA_UNIT);
3652 if (invisible_frame)
3653 GST_BUFFER_FLAG_SET (sub, GST_BUFFER_FLAG_DECODE_ONLY);
3655 if (stream->encodings != NULL && stream->encodings->len > 0)
3656 sub = gst_matroska_decode_buffer (stream, sub);
3659 GST_WARNING_OBJECT (demux, "Decoding buffer failed");
3663 if (!stream->dts_only) {
3664 GST_BUFFER_PTS (sub) = lace_time;
3666 GST_BUFFER_DTS (sub) = lace_time;
3667 if (stream->intra_only)
3668 GST_BUFFER_PTS (sub) = lace_time;
3671 buffer_timestamp = gst_matroska_track_get_buffer_timestamp (stream, sub);
3673 if (GST_CLOCK_TIME_IS_VALID (lace_time)) {
3674 GstClockTime last_stop_end;
3676 /* Check if this stream is after segment stop */
3677 if (GST_CLOCK_TIME_IS_VALID (demux->common.segment.stop) &&
3678 lace_time >= demux->common.segment.stop) {
3679 GST_DEBUG_OBJECT (demux,
3680 "Stream %d after segment stop %" GST_TIME_FORMAT, stream->index,
3681 GST_TIME_ARGS (demux->common.segment.stop));
3682 gst_buffer_unref (sub);
3685 if (offset >= stream->to_offset
3686 || (GST_CLOCK_TIME_IS_VALID (demux->to_time)
3687 && lace_time > demux->to_time)) {
3688 GST_DEBUG_OBJECT (demux, "Stream %d after playback section",
3690 gst_buffer_unref (sub);
3694 /* handle gaps, e.g. non-zero start-time, or an cue index entry
3695 * that landed us with timestamps not quite intended */
3696 GST_OBJECT_LOCK (demux);
3697 if (demux->max_gap_time &&
3698 GST_CLOCK_TIME_IS_VALID (demux->last_stop_end) &&
3699 demux->common.segment.rate > 0.0) {
3700 GstClockTimeDiff diff;
3702 /* only send segments with increasing start times,
3703 * otherwise if these go back and forth downstream (sinks) increase
3704 * accumulated time and running_time */
3705 diff = GST_CLOCK_DIFF (demux->last_stop_end, lace_time);
3706 if (diff > 0 && diff > demux->max_gap_time
3707 && lace_time > demux->common.segment.start
3708 && (!GST_CLOCK_TIME_IS_VALID (demux->common.segment.stop)
3709 || lace_time < demux->common.segment.stop)) {
3711 GST_DEBUG_OBJECT (demux,
3712 "Gap of %" G_GINT64_FORMAT " ns detected in"
3713 "stream %d (%" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "). "
3714 "Sending updated SEGMENT events", diff,
3715 stream->index, GST_TIME_ARGS (stream->pos),
3716 GST_TIME_ARGS (lace_time));
3718 event = gst_event_new_gap (demux->last_stop_end, diff);
3719 GST_OBJECT_UNLOCK (demux);
3720 gst_pad_push_event (stream->pad, event);
3721 GST_OBJECT_LOCK (demux);
3725 if (!GST_CLOCK_TIME_IS_VALID (demux->common.segment.position)
3726 || demux->common.segment.position < lace_time) {
3727 demux->common.segment.position = lace_time;
3729 GST_OBJECT_UNLOCK (demux);
3731 last_stop_end = lace_time;
3733 GST_BUFFER_DURATION (sub) = duration / laces;
3734 last_stop_end += GST_BUFFER_DURATION (sub);
3737 if (!GST_CLOCK_TIME_IS_VALID (demux->last_stop_end) ||
3738 demux->last_stop_end < last_stop_end)
3739 demux->last_stop_end = last_stop_end;
3741 GST_OBJECT_LOCK (demux);
3742 if (demux->common.segment.duration == -1 ||
3743 demux->stream_start_time + demux->common.segment.duration <
3745 demux->common.segment.duration =
3746 last_stop_end - demux->stream_start_time;
3747 GST_OBJECT_UNLOCK (demux);
3748 if (!demux->invalid_duration) {
3749 gst_element_post_message (GST_ELEMENT_CAST (demux),
3750 gst_message_new_duration_changed (GST_OBJECT_CAST (demux)));
3751 demux->invalid_duration = TRUE;
3754 GST_OBJECT_UNLOCK (demux);
3758 stream->pos = lace_time;
3760 gst_matroska_demux_sync_streams (demux);
3762 if (stream->set_discont) {
3763 GST_DEBUG_OBJECT (demux, "marking DISCONT");
3764 GST_BUFFER_FLAG_SET (sub, GST_BUFFER_FLAG_DISCONT);
3765 stream->set_discont = FALSE;
3767 GST_BUFFER_FLAG_UNSET (sub, GST_BUFFER_FLAG_DISCONT);
3770 /* reverse playback book-keeping */
3771 if (!GST_CLOCK_TIME_IS_VALID (stream->from_time))
3772 stream->from_time = lace_time;
3773 if (stream->from_offset == -1)
3774 stream->from_offset = offset;
3776 GST_DEBUG_OBJECT (demux,
3777 "Pushing lace %d, data of size %" G_GSIZE_FORMAT
3778 " for stream %d, time=%" GST_TIME_FORMAT " and duration=%"
3779 GST_TIME_FORMAT, n, gst_buffer_get_size (sub), stream_num,
3780 GST_TIME_ARGS (buffer_timestamp),
3781 GST_TIME_ARGS (GST_BUFFER_DURATION (sub)));
3784 if (demux->common.element_index) {
3785 if (stream->index_writer_id == -1)
3786 gst_index_get_writer_id (demux->common.element_index,
3787 GST_OBJECT (stream->pad), &stream->index_writer_id);
3789 GST_LOG_OBJECT (demux, "adding association %" GST_TIME_FORMAT "-> %"
3790 G_GUINT64_FORMAT " for writer id %d",
3791 GST_TIME_ARGS (buffer_timestamp), cluster_offset,
3792 stream->index_writer_id);
3793 gst_index_add_association (demux->common.element_index,
3794 stream->index_writer_id, GST_BUFFER_FLAG_IS_SET (sub,
3795 GST_BUFFER_FLAG_DELTA_UNIT) ? 0 : GST_ASSOCIATION_FLAG_KEY_UNIT,
3796 GST_FORMAT_TIME, buffer_timestamp, GST_FORMAT_BYTES, cluster_offset,
3801 /* Postprocess the buffers depending on the codec used */
3802 if (stream->postprocess_frame) {
3803 GST_LOG_OBJECT (demux, "running post process");
3804 ret = stream->postprocess_frame (GST_ELEMENT (demux), stream, &sub);
3807 /* At this point, we have a sub-buffer pointing at data within a larger
3808 buffer. This data might not be aligned with anything. If the data is
3809 raw samples though, we want it aligned to the raw type (eg, 4 bytes
3810 for 32 bit samples, etc), or bad things will happen downstream as
3811 elements typically assume minimal alignment.
3812 Therefore, create an aligned copy if necessary. */
3813 sub = gst_matroska_demux_align_buffer (demux, sub, stream->alignment);
3815 if (!strcmp (stream->codec_id, GST_MATROSKA_CODEC_ID_AUDIO_OPUS)) {
3816 guint64 start_clip = 0, end_clip = 0;
3818 /* Codec delay is part of the timestamps */
3819 if (GST_BUFFER_PTS_IS_VALID (sub) && stream->codec_delay) {
3820 if (GST_BUFFER_PTS (sub) > stream->codec_delay) {
3821 GST_BUFFER_PTS (sub) -= stream->codec_delay;
3823 GST_BUFFER_PTS (sub) = 0;
3825 gst_util_uint64_scale_round (stream->codec_delay, 48000,
3828 if (GST_BUFFER_DURATION_IS_VALID (sub)) {
3829 if (GST_BUFFER_DURATION (sub) > stream->codec_delay)
3830 GST_BUFFER_DURATION (sub) -= stream->codec_delay;
3832 GST_BUFFER_DURATION (sub) = 0;
3837 if (block_discardpadding) {
3839 gst_util_uint64_scale_round (block_discardpadding, 48000,
3843 if (start_clip || end_clip) {
3844 gst_buffer_add_audio_clipping_meta (sub, GST_FORMAT_DEFAULT,
3845 start_clip, end_clip);
3849 if (GST_BUFFER_PTS_IS_VALID (sub)) {
3850 stream->pos = GST_BUFFER_PTS (sub);
3851 if (GST_BUFFER_DURATION_IS_VALID (sub))
3852 stream->pos += GST_BUFFER_DURATION (sub);
3853 } else if (GST_BUFFER_DTS_IS_VALID (sub)) {
3854 stream->pos = GST_BUFFER_DTS (sub);
3855 if (GST_BUFFER_DURATION_IS_VALID (sub))
3856 stream->pos += GST_BUFFER_DURATION (sub);
3859 ret = gst_pad_push (stream->pad, sub);
3861 if (demux->common.segment.rate < 0) {
3862 if (lace_time > demux->common.segment.stop && ret == GST_FLOW_EOS) {
3863 /* In reverse playback we can get a GST_FLOW_EOS when
3864 * we are at the end of the segment, so we just need to jump
3865 * back to the previous section. */
3866 GST_DEBUG_OBJECT (demux, "downstream has reached end of segment");
3871 ret = gst_flow_combiner_update_pad_flow (demux->flowcombiner,
3875 size -= lace_size[n];
3876 if (lace_time != GST_CLOCK_TIME_NONE && duration)
3877 lace_time += duration / laces;
3879 lace_time = GST_CLOCK_TIME_NONE;
3885 gst_buffer_unmap (buf, &map);
3886 gst_buffer_unref (buf);
3898 ret = gst_flow_combiner_update_pad_flow (demux->flowcombiner, stream->pad,
3904 GST_ELEMENT_WARNING (demux, STREAM, DEMUX, (NULL), ("Invalid lacing size"));
3905 /* non-fatal, try next block(group) */
3911 GST_ELEMENT_WARNING (demux, STREAM, DEMUX, (NULL), ("Data error"));
3912 /* non-fatal, try next block(group) */
3918 /* return FALSE if block(group) should be skipped (due to a seek) */
3919 static inline gboolean
3920 gst_matroska_demux_seek_block (GstMatroskaDemux * demux)
3922 if (G_UNLIKELY (demux->seek_block)) {
3923 if (!(--demux->seek_block)) {
3926 GST_LOG_OBJECT (demux, "should skip block due to seek");
3934 static GstFlowReturn
3935 gst_matroska_demux_parse_contents_seekentry (GstMatroskaDemux * demux,
3939 guint64 seek_pos = (guint64) - 1;
3940 guint32 seek_id = 0;
3943 DEBUG_ELEMENT_START (demux, ebml, "Seek");
3945 if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
3946 DEBUG_ELEMENT_STOP (demux, ebml, "Seek", ret);
3950 while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
3951 if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
3955 case GST_MATROSKA_ID_SEEKID:
3959 if ((ret = gst_ebml_read_uint (ebml, &id, &t)) != GST_FLOW_OK)
3962 GST_DEBUG_OBJECT (demux, "SeekID: %" G_GUINT64_FORMAT, t);
3967 case GST_MATROSKA_ID_SEEKPOSITION:
3971 if ((ret = gst_ebml_read_uint (ebml, &id, &t)) != GST_FLOW_OK)
3974 if (t > G_MAXINT64) {
3975 GST_WARNING_OBJECT (demux,
3976 "Too large SeekPosition %" G_GUINT64_FORMAT, t);
3980 GST_DEBUG_OBJECT (demux, "SeekPosition: %" G_GUINT64_FORMAT, t);
3986 ret = gst_matroska_read_common_parse_skip (&demux->common, ebml,
3992 if (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)
3995 if (!seek_id || seek_pos == (guint64) - 1) {
3996 GST_WARNING_OBJECT (demux, "Incomplete seekhead entry (0x%x/%"
3997 G_GUINT64_FORMAT ")", seek_id, seek_pos);
4002 case GST_MATROSKA_ID_SEEKHEAD:
4005 case GST_MATROSKA_ID_CUES:
4006 case GST_MATROSKA_ID_TAGS:
4007 case GST_MATROSKA_ID_TRACKS:
4008 case GST_MATROSKA_ID_SEGMENTINFO:
4009 case GST_MATROSKA_ID_ATTACHMENTS:
4010 case GST_MATROSKA_ID_CHAPTERS:
4012 guint64 before_pos, length;
4016 length = gst_matroska_read_common_get_length (&demux->common);
4017 before_pos = demux->common.offset;
4019 if (length == (guint64) - 1) {
4020 GST_DEBUG_OBJECT (demux, "no upstream length, skipping SeakHead entry");
4024 /* check for validity */
4025 if (seek_pos + demux->common.ebml_segment_start + 12 >= length) {
4026 GST_WARNING_OBJECT (demux,
4027 "SeekHead reference lies outside file!" " (%"
4028 G_GUINT64_FORMAT "+%" G_GUINT64_FORMAT "+12 >= %"
4029 G_GUINT64_FORMAT ")", seek_pos, demux->common.ebml_segment_start,
4034 /* only pick up index location when streaming */
4035 if (demux->streaming) {
4036 if (seek_id == GST_MATROSKA_ID_CUES) {
4037 demux->index_offset = seek_pos + demux->common.ebml_segment_start;
4038 GST_DEBUG_OBJECT (demux, "Cues located at offset %" G_GUINT64_FORMAT,
4039 demux->index_offset);
4045 demux->common.offset = seek_pos + demux->common.ebml_segment_start;
4048 if ((ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
4049 GST_ELEMENT_CAST (demux), &id, &length, &needed)) !=
4053 if (id != seek_id) {
4054 GST_WARNING_OBJECT (demux,
4055 "We looked for ID=0x%x but got ID=0x%x (pos=%" G_GUINT64_FORMAT ")",
4056 seek_id, id, seek_pos + demux->common.ebml_segment_start);
4059 ret = gst_matroska_demux_parse_id (demux, id, length, needed);
4064 demux->common.offset = before_pos;
4068 case GST_MATROSKA_ID_CLUSTER:
4070 guint64 pos = seek_pos + demux->common.ebml_segment_start;
4072 GST_LOG_OBJECT (demux, "Cluster position");
4073 if (G_UNLIKELY (!demux->clusters))
4074 demux->clusters = g_array_sized_new (TRUE, TRUE, sizeof (guint64), 100);
4075 g_array_append_val (demux->clusters, pos);
4080 GST_DEBUG_OBJECT (demux, "Ignoring Seek entry for ID=0x%x", seek_id);
4083 DEBUG_ELEMENT_STOP (demux, ebml, "Seek", ret);
4088 static GstFlowReturn
4089 gst_matroska_demux_parse_contents (GstMatroskaDemux * demux, GstEbmlRead * ebml)
4091 GstFlowReturn ret = GST_FLOW_OK;
4094 DEBUG_ELEMENT_START (demux, ebml, "SeekHead");
4096 if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
4097 DEBUG_ELEMENT_STOP (demux, ebml, "SeekHead", ret);
4101 while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
4102 if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
4106 case GST_MATROSKA_ID_SEEKENTRY:
4108 ret = gst_matroska_demux_parse_contents_seekentry (demux, ebml);
4109 /* Ignore EOS and errors here */
4110 if (ret != GST_FLOW_OK) {
4111 GST_DEBUG_OBJECT (demux, "Ignoring %s", gst_flow_get_name (ret));
4118 ret = gst_matroska_read_common_parse_skip (&demux->common,
4119 ebml, "SeekHead", id);
4124 DEBUG_ELEMENT_STOP (demux, ebml, "SeekHead", ret);
4126 /* Sort clusters by position for easier searching */
4127 if (demux->clusters)
4128 g_array_sort (demux->clusters, (GCompareFunc) gst_matroska_cluster_compare);
4133 #define GST_FLOW_OVERFLOW GST_FLOW_CUSTOM_ERROR
4135 #define MAX_BLOCK_SIZE (15 * 1024 * 1024)
4137 static inline GstFlowReturn
4138 gst_matroska_demux_check_read_size (GstMatroskaDemux * demux, guint64 bytes)
4140 if (G_UNLIKELY (bytes > MAX_BLOCK_SIZE)) {
4141 /* only a few blocks are expected/allowed to be large,
4142 * and will be recursed into, whereas others will be read and must fit */
4143 if (demux->streaming) {
4144 /* fatal in streaming case, as we can't step over easily */
4145 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL),
4146 ("reading large block of size %" G_GUINT64_FORMAT " not supported; "
4147 "file might be corrupt.", bytes));
4148 return GST_FLOW_ERROR;
4150 /* indicate higher level to quietly give up */
4151 GST_DEBUG_OBJECT (demux,
4152 "too large block of size %" G_GUINT64_FORMAT, bytes);
4153 return GST_FLOW_ERROR;
4160 /* returns TRUE if we truely are in error state, and should give up */
4161 static inline GstFlowReturn
4162 gst_matroska_demux_check_parse_error (GstMatroskaDemux * demux)
4164 if (!demux->streaming && demux->next_cluster_offset > 0) {
4165 /* just repositioning to where next cluster should be and try from there */
4166 GST_WARNING_OBJECT (demux, "parse error, trying next cluster expected at %"
4167 G_GUINT64_FORMAT, demux->next_cluster_offset);
4168 demux->common.offset = demux->next_cluster_offset;
4169 demux->next_cluster_offset = 0;
4175 /* sigh, one last attempt above and beyond call of duty ...;
4176 * search for cluster mark following current pos */
4177 pos = demux->common.offset;
4178 GST_WARNING_OBJECT (demux, "parse error, looking for next cluster");
4179 if ((ret = gst_matroska_demux_search_cluster (demux, &pos)) != GST_FLOW_OK) {
4180 /* did not work, give up */
4183 GST_DEBUG_OBJECT (demux, "... found at %" G_GUINT64_FORMAT, pos);
4184 /* try that position */
4185 demux->common.offset = pos;
4191 static inline GstFlowReturn
4192 gst_matroska_demux_flush (GstMatroskaDemux * demux, guint flush)
4194 GST_LOG_OBJECT (demux, "skipping %d bytes", flush);
4195 demux->common.offset += flush;
4196 if (demux->streaming) {
4199 /* hard to skip large blocks when streaming */
4200 ret = gst_matroska_demux_check_read_size (demux, flush);
4201 if (ret != GST_FLOW_OK)
4203 if (flush <= gst_adapter_available (demux->common.adapter))
4204 gst_adapter_flush (demux->common.adapter, flush);
4206 return GST_FLOW_EOS;
4211 /* initializes @ebml with @bytes from input stream at current offset.
4212 * Returns EOS if insufficient available,
4213 * ERROR if too much was attempted to read. */
4214 static inline GstFlowReturn
4215 gst_matroska_demux_take (GstMatroskaDemux * demux, guint64 bytes,
4218 GstBuffer *buffer = NULL;
4219 GstFlowReturn ret = GST_FLOW_OK;
4221 GST_LOG_OBJECT (demux, "taking %" G_GUINT64_FORMAT " bytes for parsing",
4223 ret = gst_matroska_demux_check_read_size (demux, bytes);
4224 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
4225 if (!demux->streaming) {
4226 /* in pull mode, we can skip */
4227 if ((ret = gst_matroska_demux_flush (demux, bytes)) == GST_FLOW_OK)
4228 ret = GST_FLOW_OVERFLOW;
4230 /* otherwise fatal */
4231 ret = GST_FLOW_ERROR;
4235 if (demux->streaming) {
4236 if (gst_adapter_available (demux->common.adapter) >= bytes)
4237 buffer = gst_adapter_take_buffer (demux->common.adapter, bytes);
4241 ret = gst_matroska_read_common_peek_bytes (&demux->common,
4242 demux->common.offset, bytes, &buffer, NULL);
4243 if (G_LIKELY (buffer)) {
4244 gst_ebml_read_init (ebml, GST_ELEMENT_CAST (demux), buffer,
4245 demux->common.offset);
4246 demux->common.offset += bytes;
4253 gst_matroska_demux_check_seekability (GstMatroskaDemux * demux)
4256 gboolean seekable = FALSE;
4257 gint64 start = -1, stop = -1;
4259 query = gst_query_new_seeking (GST_FORMAT_BYTES);
4260 if (!gst_pad_peer_query (demux->common.sinkpad, query)) {
4261 GST_DEBUG_OBJECT (demux, "seeking query failed");
4265 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
4267 /* try harder to query upstream size if we didn't get it the first time */
4268 if (seekable && stop == -1) {
4269 GST_DEBUG_OBJECT (demux, "doing duration query to fix up unset stop");
4270 gst_pad_peer_query_duration (demux->common.sinkpad, GST_FORMAT_BYTES,
4274 /* if upstream doesn't know the size, it's likely that it's not seekable in
4275 * practice even if it technically may be seekable */
4276 if (seekable && (start != 0 || stop <= start)) {
4277 GST_DEBUG_OBJECT (demux, "seekable but unknown start/stop -> disable");
4282 GST_INFO_OBJECT (demux, "seekable: %d (%" G_GUINT64_FORMAT " - %"
4283 G_GUINT64_FORMAT ")", seekable, start, stop);
4284 demux->seekable = seekable;
4286 gst_query_unref (query);
4289 static GstFlowReturn
4290 gst_matroska_demux_find_tracks (GstMatroskaDemux * demux)
4296 GstFlowReturn ret = GST_FLOW_OK;
4298 GST_WARNING_OBJECT (demux,
4299 "Found Cluster element before Tracks, searching Tracks");
4302 before_pos = demux->common.offset;
4304 /* Search Tracks element */
4306 ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
4307 GST_ELEMENT_CAST (demux), &id, &length, &needed);
4308 if (ret != GST_FLOW_OK)
4311 if (id != GST_MATROSKA_ID_TRACKS) {
4312 /* we may be skipping large cluster here, so forego size check etc */
4313 /* ... but we can't skip undefined size; force error */
4314 if (length == G_MAXUINT64) {
4315 ret = gst_matroska_demux_check_read_size (demux, length);
4318 demux->common.offset += needed;
4319 demux->common.offset += length;
4324 /* will lead to track parsing ... */
4325 ret = gst_matroska_demux_parse_id (demux, id, length, needed);
4330 demux->common.offset = before_pos;
4335 #define GST_READ_CHECK(stmt) \
4337 if (G_UNLIKELY ((ret = (stmt)) != GST_FLOW_OK)) { \
4338 if (ret == GST_FLOW_OVERFLOW) { \
4339 ret = GST_FLOW_OK; \
4345 static GstFlowReturn
4346 gst_matroska_demux_parse_id (GstMatroskaDemux * demux, guint32 id,
4347 guint64 length, guint needed)
4349 GstEbmlRead ebml = { 0, };
4350 GstFlowReturn ret = GST_FLOW_OK;
4353 GST_LOG_OBJECT (demux, "Parsing Element id 0x%x, "
4354 "size %" G_GUINT64_FORMAT ", prefix %d", id, length, needed);
4356 /* if we plan to read and parse this element, we need prefix (id + length)
4357 * and the contents */
4358 /* mind about overflow wrap-around when dealing with undefined size */
4360 if (G_LIKELY (length != G_MAXUINT64))
4363 switch (demux->common.state) {
4364 case GST_MATROSKA_READ_STATE_START:
4366 case GST_EBML_ID_HEADER:
4367 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4368 ret = gst_matroska_read_common_parse_header (&demux->common, &ebml);
4369 if (ret != GST_FLOW_OK)
4371 demux->common.state = GST_MATROSKA_READ_STATE_SEGMENT;
4372 gst_matroska_demux_check_seekability (demux);
4375 goto invalid_header;
4379 case GST_MATROSKA_READ_STATE_SEGMENT:
4381 case GST_MATROSKA_ID_SEGMENT:
4382 /* eat segment prefix */
4383 GST_READ_CHECK (gst_matroska_demux_flush (demux, needed));
4384 GST_DEBUG_OBJECT (demux,
4385 "Found Segment start at offset %" G_GUINT64_FORMAT " with size %"
4386 G_GUINT64_FORMAT, demux->common.offset, length);
4387 /* seeks are from the beginning of the segment,
4388 * after the segment ID/length */
4389 demux->common.ebml_segment_start = demux->common.offset;
4391 length = G_MAXUINT64;
4392 demux->common.ebml_segment_length = length;
4393 demux->common.state = GST_MATROSKA_READ_STATE_HEADER;
4396 GST_WARNING_OBJECT (demux,
4397 "Expected a Segment ID (0x%x), but received 0x%x!",
4398 GST_MATROSKA_ID_SEGMENT, id);
4399 GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4403 case GST_MATROSKA_READ_STATE_SCANNING:
4404 if (id != GST_MATROSKA_ID_CLUSTER &&
4405 id != GST_MATROSKA_ID_CLUSTERTIMECODE) {
4406 if (demux->common.start_resync_offset != -1) {
4407 /* we need to skip byte per byte if we are scanning for a new cluster
4408 * after invalid data is found
4414 if (demux->common.start_resync_offset != -1) {
4415 GST_LOG_OBJECT (demux, "Resync done, new cluster found!");
4416 demux->common.start_resync_offset = -1;
4417 demux->common.state = demux->common.state_to_restore;
4421 case GST_MATROSKA_READ_STATE_HEADER:
4422 case GST_MATROSKA_READ_STATE_DATA:
4423 case GST_MATROSKA_READ_STATE_SEEK:
4425 case GST_MATROSKA_ID_SEGMENTINFO:
4426 if (!demux->common.segmentinfo_parsed) {
4427 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4428 ret = gst_matroska_read_common_parse_info (&demux->common,
4429 GST_ELEMENT_CAST (demux), &ebml);
4430 if (ret == GST_FLOW_OK)
4431 gst_matroska_demux_send_tags (demux);
4433 GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4436 case GST_MATROSKA_ID_TRACKS:
4437 if (!demux->tracks_parsed) {
4438 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4439 ret = gst_matroska_demux_parse_tracks (demux, &ebml);
4441 GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4444 case GST_MATROSKA_ID_CLUSTER:
4445 if (G_UNLIKELY (!demux->tracks_parsed)) {
4446 if (demux->streaming) {
4447 GST_DEBUG_OBJECT (demux, "Cluster before Track");
4448 goto not_streamable;
4450 ret = gst_matroska_demux_find_tracks (demux);
4451 if (!demux->tracks_parsed)
4455 if (G_UNLIKELY (demux->common.state
4456 == GST_MATROSKA_READ_STATE_HEADER)) {
4457 demux->common.state = GST_MATROSKA_READ_STATE_DATA;
4458 demux->first_cluster_offset = demux->common.offset;
4459 GST_DEBUG_OBJECT (demux, "signaling no more pads");
4460 gst_element_no_more_pads (GST_ELEMENT (demux));
4461 /* send initial segment - we wait till we know the first
4462 incoming timestamp, so we can properly set the start of
4464 demux->need_segment = TRUE;
4466 demux->cluster_time = GST_CLOCK_TIME_NONE;
4467 demux->cluster_offset = demux->common.offset;
4468 if (G_UNLIKELY (!demux->seek_first && demux->seek_block)) {
4469 GST_DEBUG_OBJECT (demux, "seek target block %" G_GUINT64_FORMAT
4470 " not found in Cluster, trying next Cluster's first block instead",
4472 demux->seek_block = 0;
4474 demux->seek_first = FALSE;
4475 /* record next cluster for recovery */
4476 if (read != G_MAXUINT64)
4477 demux->next_cluster_offset = demux->cluster_offset + read;
4478 /* eat cluster prefix */
4479 gst_matroska_demux_flush (demux, needed);
4481 case GST_MATROSKA_ID_CLUSTERTIMECODE:
4485 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4486 if ((ret = gst_ebml_read_uint (&ebml, &id, &num)) != GST_FLOW_OK)
4488 GST_DEBUG_OBJECT (demux, "ClusterTimeCode: %" G_GUINT64_FORMAT, num);
4489 demux->cluster_time = num;
4491 if (demux->common.element_index) {
4492 if (demux->common.element_index_writer_id == -1)
4493 gst_index_get_writer_id (demux->common.element_index,
4494 GST_OBJECT (demux), &demux->common.element_index_writer_id);
4495 GST_LOG_OBJECT (demux, "adding association %" GST_TIME_FORMAT "-> %"
4496 G_GUINT64_FORMAT " for writer id %d",
4497 GST_TIME_ARGS (demux->cluster_time), demux->cluster_offset,
4498 demux->common.element_index_writer_id);
4499 gst_index_add_association (demux->common.element_index,
4500 demux->common.element_index_writer_id,
4501 GST_ASSOCIATION_FLAG_KEY_UNIT,
4502 GST_FORMAT_TIME, demux->cluster_time,
4503 GST_FORMAT_BYTES, demux->cluster_offset, NULL);
4508 case GST_MATROSKA_ID_BLOCKGROUP:
4509 if (!gst_matroska_demux_seek_block (demux))
4511 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4512 DEBUG_ELEMENT_START (demux, &ebml, "BlockGroup");
4513 if ((ret = gst_ebml_read_master (&ebml, &id)) == GST_FLOW_OK) {
4514 ret = gst_matroska_demux_parse_blockgroup_or_simpleblock (demux,
4515 &ebml, demux->cluster_time, demux->cluster_offset, FALSE);
4517 DEBUG_ELEMENT_STOP (demux, &ebml, "BlockGroup", ret);
4519 case GST_MATROSKA_ID_SIMPLEBLOCK:
4520 if (!gst_matroska_demux_seek_block (demux))
4522 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4523 DEBUG_ELEMENT_START (demux, &ebml, "SimpleBlock");
4524 ret = gst_matroska_demux_parse_blockgroup_or_simpleblock (demux,
4525 &ebml, demux->cluster_time, demux->cluster_offset, TRUE);
4526 DEBUG_ELEMENT_STOP (demux, &ebml, "SimpleBlock", ret);
4528 case GST_MATROSKA_ID_ATTACHMENTS:
4529 if (!demux->common.attachments_parsed) {
4530 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4531 ret = gst_matroska_read_common_parse_attachments (&demux->common,
4532 GST_ELEMENT_CAST (demux), &ebml);
4533 if (ret == GST_FLOW_OK)
4534 gst_matroska_demux_send_tags (demux);
4536 GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4539 case GST_MATROSKA_ID_TAGS:
4540 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4541 ret = gst_matroska_read_common_parse_metadata (&demux->common,
4542 GST_ELEMENT_CAST (demux), &ebml);
4543 if (ret == GST_FLOW_OK)
4544 gst_matroska_demux_send_tags (demux);
4546 case GST_MATROSKA_ID_CHAPTERS:
4547 if (!demux->common.chapters_parsed) {
4548 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4550 gst_matroska_read_common_parse_chapters (&demux->common, &ebml);
4552 if (demux->common.toc) {
4553 gst_matroska_demux_send_event (demux,
4554 gst_event_new_toc (demux->common.toc, FALSE));
4557 GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4559 case GST_MATROSKA_ID_SEEKHEAD:
4560 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4561 ret = gst_matroska_demux_parse_contents (demux, &ebml);
4563 case GST_MATROSKA_ID_CUES:
4564 if (demux->common.index_parsed) {
4565 GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4568 GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4569 ret = gst_matroska_read_common_parse_index (&demux->common, &ebml);
4570 /* only push based; delayed index building */
4571 if (ret == GST_FLOW_OK
4572 && demux->common.state == GST_MATROSKA_READ_STATE_SEEK) {
4575 GST_OBJECT_LOCK (demux);
4576 event = demux->seek_event;
4577 demux->seek_event = NULL;
4578 GST_OBJECT_UNLOCK (demux);
4581 /* unlikely to fail, since we managed to seek to this point */
4582 if (!gst_matroska_demux_handle_seek_event (demux, NULL, event)) {
4583 gst_event_unref (event);
4586 gst_event_unref (event);
4587 /* resume data handling, main thread clear to seek again */
4588 GST_OBJECT_LOCK (demux);
4589 demux->common.state = GST_MATROSKA_READ_STATE_DATA;
4590 GST_OBJECT_UNLOCK (demux);
4593 case GST_MATROSKA_ID_POSITION:
4594 case GST_MATROSKA_ID_PREVSIZE:
4595 case GST_MATROSKA_ID_ENCRYPTEDBLOCK:
4596 case GST_MATROSKA_ID_SILENTTRACKS:
4597 GST_DEBUG_OBJECT (demux,
4598 "Skipping Cluster subelement 0x%x - ignoring", id);
4602 GST_DEBUG_OBJECT (demux, "skipping Element 0x%x", id);
4603 GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4609 if (ret == GST_FLOW_PARSE)
4613 gst_ebml_read_clear (&ebml);
4619 /* simply exit, maybe not enough data yet */
4620 /* no ebml to clear if read error */
4625 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL),
4626 ("Failed to parse Element 0x%x", id));
4627 ret = GST_FLOW_ERROR;
4632 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL),
4633 ("File layout does not permit streaming"));
4634 ret = GST_FLOW_ERROR;
4639 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL),
4640 ("No Tracks element found"));
4641 ret = GST_FLOW_ERROR;
4646 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL), ("Invalid header"));
4647 ret = GST_FLOW_ERROR;
4652 GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL), ("Failed to seek"));
4653 ret = GST_FLOW_ERROR;
4659 gst_matroska_demux_loop (GstPad * pad)
4661 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (GST_PAD_PARENT (pad));
4667 /* If we have to close a segment, send a new segment to do this now */
4668 if (G_LIKELY (demux->common.state == GST_MATROSKA_READ_STATE_DATA)) {
4669 if (G_UNLIKELY (demux->new_segment)) {
4670 gst_matroska_demux_send_event (demux, demux->new_segment);
4671 demux->new_segment = NULL;
4675 ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
4676 GST_ELEMENT_CAST (demux), &id, &length, &needed);
4677 if (ret == GST_FLOW_EOS) {
4679 } else if (ret == GST_FLOW_FLUSHING) {
4681 } else if (ret != GST_FLOW_OK) {
4682 ret = gst_matroska_demux_check_parse_error (demux);
4684 /* Only handle EOS as no error if we're outside the segment already */
4685 if (ret == GST_FLOW_EOS && (demux->common.ebml_segment_length != G_MAXUINT64
4686 && demux->common.offset >=
4687 demux->common.ebml_segment_start +
4688 demux->common.ebml_segment_length))
4690 else if (ret != GST_FLOW_OK)
4696 GST_LOG_OBJECT (demux, "Offset %" G_GUINT64_FORMAT ", Element id 0x%x, "
4697 "size %" G_GUINT64_FORMAT ", needed %d", demux->common.offset, id,
4700 ret = gst_matroska_demux_parse_id (demux, id, length, needed);
4701 if (ret == GST_FLOW_EOS)
4703 if (ret != GST_FLOW_OK)
4706 /* check if we're at the end of a configured segment */
4707 if (G_LIKELY (demux->common.src->len)) {
4710 g_assert (demux->common.num_streams == demux->common.src->len);
4711 for (i = 0; i < demux->common.src->len; i++) {
4712 GstMatroskaTrackContext *context = g_ptr_array_index (demux->common.src,
4714 GST_DEBUG_OBJECT (context->pad, "pos %" GST_TIME_FORMAT,
4715 GST_TIME_ARGS (context->pos));
4716 if (context->eos == FALSE)
4720 GST_INFO_OBJECT (demux, "All streams are EOS");
4726 if (G_UNLIKELY (demux->cached_length == G_MAXUINT64 ||
4727 demux->common.offset >= demux->cached_length)) {
4728 demux->cached_length = gst_matroska_read_common_get_length (&demux->common);
4729 if (demux->common.offset == demux->cached_length) {
4730 GST_LOG_OBJECT (demux, "Reached end of stream");
4741 if (demux->common.segment.rate < 0.0) {
4742 ret = gst_matroska_demux_seek_to_previous_keyframe (demux);
4743 if (ret == GST_FLOW_OK)
4750 const gchar *reason = gst_flow_get_name (ret);
4751 gboolean push_eos = FALSE;
4753 GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
4754 gst_pad_pause_task (demux->common.sinkpad);
4756 if (ret == GST_FLOW_EOS) {
4757 /* perform EOS logic */
4759 /* If we were in the headers, make sure we send no-more-pads.
4760 This will ensure decodebin does not get stuck thinking
4761 the chain is not complete yet, and waiting indefinitely. */
4762 if (G_UNLIKELY (demux->common.state == GST_MATROSKA_READ_STATE_HEADER)) {
4763 if (demux->common.src->len == 0) {
4764 GST_ELEMENT_ERROR (demux, STREAM, FAILED, (NULL),
4765 ("No pads created"));
4767 GST_ELEMENT_WARNING (demux, STREAM, DEMUX, (NULL),
4768 ("Failed to finish reading headers"));
4770 gst_element_no_more_pads (GST_ELEMENT (demux));
4773 if (demux->common.segment.flags & GST_SEEK_FLAG_SEGMENT) {
4778 /* for segment playback we need to post when (in stream time)
4779 * we stopped, this is either stop (when set) or the duration. */
4780 if ((stop = demux->common.segment.stop) == -1)
4781 stop = demux->last_stop_end;
4783 GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
4784 msg = gst_message_new_segment_done (GST_OBJECT (demux), GST_FORMAT_TIME,
4786 if (demux->segment_seqnum)
4787 gst_message_set_seqnum (msg, demux->segment_seqnum);
4788 gst_element_post_message (GST_ELEMENT (demux), msg);
4790 event = gst_event_new_segment_done (GST_FORMAT_TIME, stop);
4791 if (demux->segment_seqnum)
4792 gst_event_set_seqnum (event, demux->segment_seqnum);
4793 gst_matroska_demux_send_event (demux, event);
4797 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4798 /* for fatal errors we post an error message */
4799 GST_ELEMENT_FLOW_ERROR (demux, ret);
4805 /* send EOS, and prevent hanging if no streams yet */
4806 GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
4807 event = gst_event_new_eos ();
4808 if (demux->segment_seqnum)
4809 gst_event_set_seqnum (event, demux->segment_seqnum);
4810 if (!gst_matroska_demux_send_event (demux, event) &&
4811 (ret == GST_FLOW_EOS)) {
4812 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
4813 (NULL), ("got eos but no streams (yet)"));
4821 * Create and push a flushing seek event upstream
4824 perform_seek_to_offset (GstMatroskaDemux * demux, gdouble rate, guint64 offset,
4825 guint32 seqnum, GstSeekFlags flags)
4830 GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
4833 gst_event_new_seek (rate, GST_FORMAT_BYTES,
4834 flags | GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
4835 GST_SEEK_TYPE_SET, offset, GST_SEEK_TYPE_NONE, -1);
4836 gst_event_set_seqnum (event, seqnum);
4838 res = gst_pad_push_event (demux->common.sinkpad, event);
4840 /* segment event will update offset */
4844 static GstFlowReturn
4845 gst_matroska_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
4847 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
4849 GstFlowReturn ret = GST_FLOW_OK;
4854 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (buffer))) {
4855 GST_DEBUG_OBJECT (demux, "got DISCONT");
4856 gst_adapter_clear (demux->common.adapter);
4857 GST_OBJECT_LOCK (demux);
4858 gst_matroska_read_common_reset_streams (&demux->common,
4859 GST_CLOCK_TIME_NONE, FALSE);
4860 GST_OBJECT_UNLOCK (demux);
4863 gst_adapter_push (demux->common.adapter, buffer);
4867 available = gst_adapter_available (demux->common.adapter);
4869 ret = gst_matroska_read_common_peek_id_length_push (&demux->common,
4870 GST_ELEMENT_CAST (demux), &id, &length, &needed);
4871 if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)) {
4872 if (demux->common.ebml_segment_length != G_MAXUINT64
4873 && demux->common.offset >=
4874 demux->common.ebml_segment_start + demux->common.ebml_segment_length) {
4877 gint64 bytes_scanned;
4878 if (demux->common.start_resync_offset == -1) {
4879 demux->common.start_resync_offset = demux->common.offset;
4880 demux->common.state_to_restore = demux->common.state;
4882 bytes_scanned = demux->common.offset - demux->common.start_resync_offset;
4883 if (bytes_scanned <= INVALID_DATA_THRESHOLD) {
4884 GST_WARNING_OBJECT (demux,
4885 "parse error, looking for next cluster, actual offset %"
4886 G_GUINT64_FORMAT ", start resync offset %" G_GUINT64_FORMAT,
4887 demux->common.offset, demux->common.start_resync_offset);
4888 demux->common.state = GST_MATROSKA_READ_STATE_SCANNING;
4891 GST_WARNING_OBJECT (demux,
4892 "unrecoverable parse error, next cluster not found and threshold "
4893 "exceeded, bytes scanned %" G_GINT64_FORMAT, bytes_scanned);
4899 GST_LOG_OBJECT (demux, "Offset %" G_GUINT64_FORMAT ", Element id 0x%x, "
4900 "size %" G_GUINT64_FORMAT ", needed %d, available %d",
4901 demux->common.offset, id, length, needed, available);
4903 if (needed > available)
4906 ret = gst_matroska_demux_parse_id (demux, id, length, needed);
4907 if (ret == GST_FLOW_EOS) {
4908 /* need more data */
4910 } else if (ret != GST_FLOW_OK) {
4917 gst_matroska_demux_handle_sink_event (GstPad * pad, GstObject * parent,
4920 gboolean res = TRUE;
4921 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
4923 GST_DEBUG_OBJECT (demux,
4924 "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
4926 switch (GST_EVENT_TYPE (event)) {
4927 case GST_EVENT_SEGMENT:
4929 const GstSegment *segment;
4931 /* some debug output */
4932 gst_event_parse_segment (event, &segment);
4933 /* FIXME: do we need to update segment base here (like accum in 0.10)? */
4934 GST_DEBUG_OBJECT (demux,
4935 "received format %d segment %" GST_SEGMENT_FORMAT, segment->format,
4938 if (demux->common.state < GST_MATROSKA_READ_STATE_DATA) {
4939 GST_DEBUG_OBJECT (demux, "still starting");
4943 /* we only expect a BYTE segment, e.g. following a seek */
4944 if (segment->format != GST_FORMAT_BYTES) {
4945 GST_DEBUG_OBJECT (demux, "unsupported segment format, ignoring");
4949 GST_DEBUG_OBJECT (demux, "clearing segment state");
4950 GST_OBJECT_LOCK (demux);
4951 /* clear current segment leftover */
4952 gst_adapter_clear (demux->common.adapter);
4953 /* and some streaming setup */
4954 demux->common.offset = segment->start;
4955 /* accumulate base based on current position */
4956 if (GST_CLOCK_TIME_IS_VALID (demux->common.segment.position))
4957 demux->common.segment.base +=
4958 (MAX (demux->common.segment.position, demux->stream_start_time)
4959 - demux->stream_start_time) / fabs (demux->common.segment.rate);
4960 /* do not know where we are;
4961 * need to come across a cluster and generate segment */
4962 demux->common.segment.position = GST_CLOCK_TIME_NONE;
4963 demux->cluster_time = GST_CLOCK_TIME_NONE;
4964 demux->cluster_offset = 0;
4965 demux->need_segment = TRUE;
4966 demux->segment_seqnum = gst_event_get_seqnum (event);
4967 /* but keep some of the upstream segment */
4968 demux->common.segment.rate = segment->rate;
4969 demux->common.segment.flags = segment->flags;
4970 /* also check if need to keep some of the requested seek position */
4971 if (demux->seek_offset == segment->start) {
4972 GST_DEBUG_OBJECT (demux, "position matches requested seek");
4973 demux->common.segment.position = demux->requested_seek_time;
4975 GST_DEBUG_OBJECT (demux, "unexpected segment position");
4977 demux->requested_seek_time = GST_CLOCK_TIME_NONE;
4978 demux->seek_offset = -1;
4979 GST_OBJECT_UNLOCK (demux);
4981 /* chain will send initial segment after pads have been added,
4982 * or otherwise come up with one */
4983 GST_DEBUG_OBJECT (demux, "eating event");
4984 gst_event_unref (event);
4990 if (demux->common.state != GST_MATROSKA_READ_STATE_DATA
4991 && demux->common.state != GST_MATROSKA_READ_STATE_SCANNING) {
4992 gst_event_unref (event);
4993 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
4994 (NULL), ("got eos and didn't receive a complete header object"));
4995 } else if (demux->common.num_streams == 0) {
4996 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
4997 (NULL), ("got eos but no streams (yet)"));
4999 gst_matroska_demux_send_event (demux, event);
5003 case GST_EVENT_FLUSH_STOP:
5007 gst_adapter_clear (demux->common.adapter);
5008 GST_OBJECT_LOCK (demux);
5009 gst_matroska_read_common_reset_streams (&demux->common,
5010 GST_CLOCK_TIME_NONE, TRUE);
5011 gst_flow_combiner_reset (demux->flowcombiner);
5012 dur = demux->common.segment.duration;
5013 gst_segment_init (&demux->common.segment, GST_FORMAT_TIME);
5014 demux->common.segment.duration = dur;
5015 demux->cluster_time = GST_CLOCK_TIME_NONE;
5016 demux->cluster_offset = 0;
5017 GST_OBJECT_UNLOCK (demux);
5021 res = gst_pad_event_default (pad, parent, event);
5029 gst_matroska_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
5031 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
5033 gboolean pull_mode = FALSE;
5035 query = gst_query_new_scheduling ();
5037 if (gst_pad_peer_query (sinkpad, query))
5038 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
5039 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
5041 gst_query_unref (query);
5044 GST_DEBUG ("going to pull mode");
5045 demux->streaming = FALSE;
5046 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
5048 GST_DEBUG ("going to push (streaming) mode");
5049 demux->streaming = TRUE;
5050 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
5055 gst_matroska_demux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
5056 GstPadMode mode, gboolean active)
5059 case GST_PAD_MODE_PULL:
5061 /* if we have a scheduler we can start the task */
5062 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_matroska_demux_loop,
5065 gst_pad_stop_task (sinkpad);
5068 case GST_PAD_MODE_PUSH:
5076 gst_matroska_demux_video_caps (GstMatroskaTrackVideoContext *
5077 videocontext, const gchar * codec_id, guint8 * data, guint size,
5078 gchar ** codec_name, guint32 * riff_fourcc)
5080 GstMatroskaTrackContext *context = (GstMatroskaTrackContext *) videocontext;
5081 GstCaps *caps = NULL;
5083 g_assert (videocontext != NULL);
5084 g_assert (codec_name != NULL);
5089 /* TODO: check if we have all codec types from matroska-ids.h
5090 * check if we have to do more special things with codec_private
5093 * GST_MATROSKA_CODEC_ID_VIDEO_QUICKTIME
5094 * GST_MATROSKA_CODEC_ID_VIDEO_SNOW
5097 if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC)) {
5098 gst_riff_strf_vids *vids = NULL;
5101 GstBuffer *buf = NULL;
5103 vids = (gst_riff_strf_vids *) data;
5105 /* assure size is big enough */
5107 GST_WARNING ("Too small BITMAPINFOHEADER (%d bytes)", size);
5110 if (size < sizeof (gst_riff_strf_vids)) {
5111 vids = g_new (gst_riff_strf_vids, 1);
5112 memcpy (vids, data, size);
5115 context->dts_only = TRUE; /* VFW files only store DTS */
5117 /* little-endian -> byte-order */
5118 vids->size = GUINT32_FROM_LE (vids->size);
5119 vids->width = GUINT32_FROM_LE (vids->width);
5120 vids->height = GUINT32_FROM_LE (vids->height);
5121 vids->planes = GUINT16_FROM_LE (vids->planes);
5122 vids->bit_cnt = GUINT16_FROM_LE (vids->bit_cnt);
5123 vids->compression = GUINT32_FROM_LE (vids->compression);
5124 vids->image_size = GUINT32_FROM_LE (vids->image_size);
5125 vids->xpels_meter = GUINT32_FROM_LE (vids->xpels_meter);
5126 vids->ypels_meter = GUINT32_FROM_LE (vids->ypels_meter);
5127 vids->num_colors = GUINT32_FROM_LE (vids->num_colors);
5128 vids->imp_colors = GUINT32_FROM_LE (vids->imp_colors);
5130 if (size > sizeof (gst_riff_strf_vids)) { /* some extra_data */
5131 gsize offset = sizeof (gst_riff_strf_vids);
5134 gst_buffer_new_wrapped (g_memdup ((guint8 *) vids + offset,
5135 size - offset), size - offset);
5139 *riff_fourcc = vids->compression;
5141 caps = gst_riff_create_video_caps (vids->compression, NULL, vids,
5142 buf, NULL, codec_name);
5145 GST_WARNING ("Unhandled RIFF fourcc %" GST_FOURCC_FORMAT,
5146 GST_FOURCC_ARGS (vids->compression));
5148 static GstStaticCaps intra_caps = GST_STATIC_CAPS ("image/jpeg; "
5149 "video/x-raw; image/png; video/x-dv; video/x-huffyuv; video/x-ffv; "
5150 "video/x-compressed-yuv");
5151 GstCaps *tmp = gst_static_caps_get (&intra_caps);
5153 context->intra_only =
5154 gst_caps_can_intersect (tmp, caps);
5155 gst_caps_unref(tmp);
5159 gst_buffer_unref (buf);
5161 if (vids != (gst_riff_strf_vids *) data)
5164 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_UNCOMPRESSED)) {
5166 GstVideoFormat format;
5168 gst_video_info_init (&info);
5169 switch (videocontext->fourcc) {
5170 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
5171 format = GST_VIDEO_FORMAT_I420;
5173 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
5174 format = GST_VIDEO_FORMAT_YUY2;
5176 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
5177 format = GST_VIDEO_FORMAT_YV12;
5179 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
5180 format = GST_VIDEO_FORMAT_UYVY;
5182 case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
5183 format = GST_VIDEO_FORMAT_AYUV;
5185 case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
5186 case GST_MAKE_FOURCC ('Y', '8', ' ', ' '):
5187 format = GST_VIDEO_FORMAT_GRAY8;
5189 case GST_MAKE_FOURCC ('R', 'G', 'B', 24):
5190 format = GST_VIDEO_FORMAT_RGB;
5192 case GST_MAKE_FOURCC ('B', 'G', 'R', 24):
5193 format = GST_VIDEO_FORMAT_BGR;
5196 GST_DEBUG ("Unknown fourcc %" GST_FOURCC_FORMAT,
5197 GST_FOURCC_ARGS (videocontext->fourcc));
5201 context->intra_only = TRUE;
5203 gst_video_info_set_format (&info, format, videocontext->pixel_width,
5204 videocontext->pixel_height);
5205 caps = gst_video_info_to_caps (&info);
5206 *codec_name = gst_pb_utils_get_codec_description (caps);
5207 context->alignment = 32;
5208 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_SP)) {
5209 caps = gst_caps_new_simple ("video/x-divx",
5210 "divxversion", G_TYPE_INT, 4, NULL);
5211 *codec_name = g_strdup ("MPEG-4 simple profile");
5212 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP) ||
5213 !strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_AP)) {
5214 caps = gst_caps_new_simple ("video/mpeg",
5215 "mpegversion", G_TYPE_INT, 4,
5216 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
5220 priv = gst_buffer_new_wrapped (g_memdup (data, size), size);
5221 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, NULL);
5222 gst_buffer_unref (priv);
5224 gst_codec_utils_mpeg4video_caps_set_level_and_profile (caps, data, size);
5226 if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP))
5227 *codec_name = g_strdup ("MPEG-4 advanced simple profile");
5229 *codec_name = g_strdup ("MPEG-4 advanced profile");
5230 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3)) {
5232 caps = gst_caps_new_full (gst_structure_new ("video/x-divx",
5233 "divxversion", G_TYPE_INT, 3, NULL),
5234 gst_structure_new ("video/x-msmpeg",
5235 "msmpegversion", G_TYPE_INT, 43, NULL), NULL);
5237 caps = gst_caps_new_simple ("video/x-msmpeg",
5238 "msmpegversion", G_TYPE_INT, 43, NULL);
5239 *codec_name = g_strdup ("Microsoft MPEG-4 v.3");
5240 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG1) ||
5241 !strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG2)) {
5244 if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG1))
5249 caps = gst_caps_new_simple ("video/mpeg",
5250 "systemstream", G_TYPE_BOOLEAN, FALSE,
5251 "mpegversion", G_TYPE_INT, mpegversion, NULL);
5252 *codec_name = g_strdup_printf ("MPEG-%d video", mpegversion);
5253 context->postprocess_frame = gst_matroska_demux_add_mpeg_seq_header;
5254 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MJPEG)) {
5255 caps = gst_caps_new_empty_simple ("image/jpeg");
5256 *codec_name = g_strdup ("Motion-JPEG");
5257 context->intra_only = TRUE;
5258 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_AVC)) {
5259 caps = gst_caps_new_empty_simple ("video/x-h264");
5263 /* First byte is the version, second is the profile indication, and third
5264 * is the 5 contraint_set_flags and 3 reserved bits. Fourth byte is the
5265 * level indication. */
5266 gst_codec_utils_h264_caps_set_level_and_profile (caps, data + 1,
5269 priv = gst_buffer_new_wrapped (g_memdup (data, size), size);
5270 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, NULL);
5271 gst_buffer_unref (priv);
5273 gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, "avc",
5274 "alignment", G_TYPE_STRING, "au", NULL);
5276 GST_WARNING ("No codec data found, assuming output is byte-stream");
5277 gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, "byte-stream",
5280 *codec_name = g_strdup ("H264");
5281 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEGH_HEVC)) {
5282 caps = gst_caps_new_empty_simple ("video/x-h265");
5286 gst_codec_utils_h265_caps_set_level_tier_and_profile (caps, data + 1,
5289 priv = gst_buffer_new_wrapped (g_memdup (data, size), size);
5290 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, NULL);
5291 gst_buffer_unref (priv);
5293 gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, "hvc1",
5294 "alignment", G_TYPE_STRING, "au", NULL);
5296 GST_WARNING ("No codec data found, assuming output is byte-stream");
5297 gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, "byte-stream",
5300 *codec_name = g_strdup ("HEVC");
5301 } else if ((!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO1)) ||
5302 (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO2)) ||
5303 (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO3)) ||
5304 (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO4))) {
5305 gint rmversion = -1;
5307 if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO1))
5309 else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO2))
5311 else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO3))
5313 else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO4))
5316 caps = gst_caps_new_simple ("video/x-pn-realvideo",
5317 "rmversion", G_TYPE_INT, rmversion, NULL);
5318 GST_DEBUG ("data:%p, size:0x%x", data, size);
5319 /* We need to extract the extradata ! */
5320 if (data && (size >= 0x22)) {
5325 subformat = GST_READ_UINT32_BE (data + 0x1a);
5326 rformat = GST_READ_UINT32_BE (data + 0x1e);
5329 gst_buffer_new_wrapped (g_memdup (data + 0x1a, size - 0x1a),
5331 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, "format",
5332 G_TYPE_INT, rformat, "subformat", G_TYPE_INT, subformat, NULL);
5333 gst_buffer_unref (priv);
5336 *codec_name = g_strdup_printf ("RealVideo %d.0", rmversion);
5337 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_THEORA)) {
5338 caps = gst_caps_new_empty_simple ("video/x-theora");
5339 context->stream_headers =
5340 gst_matroska_parse_xiph_stream_headers (context->codec_priv,
5341 context->codec_priv_size);
5342 /* FIXME: mark stream as broken and skip if there are no stream headers */
5343 context->send_stream_headers = TRUE;
5344 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_DIRAC)) {
5345 caps = gst_caps_new_empty_simple ("video/x-dirac");
5346 *codec_name = g_strdup_printf ("Dirac");
5347 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_VP8)) {
5348 caps = gst_caps_new_empty_simple ("video/x-vp8");
5349 *codec_name = g_strdup_printf ("On2 VP8");
5350 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_VP9)) {
5351 caps = gst_caps_new_empty_simple ("video/x-vp9");
5352 *codec_name = g_strdup_printf ("On2 VP9");
5353 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_PRORES)) {
5355 const gchar *variant, *variant_descr = "";
5357 /* Expect a fourcc in the codec private data */
5358 if (!data || size < 4) {
5359 GST_WARNING ("No or too small PRORESS fourcc (%d bytes)", size);
5363 fourcc = GST_STR_FOURCC (data);
5365 case GST_MAKE_FOURCC ('a', 'p', 'c', 's'):
5366 variant_descr = " 4:2:2 LT";
5369 case GST_MAKE_FOURCC ('a', 'p', 'c', 'h'):
5371 variant_descr = " 4:2:2 HQ";
5373 case GST_MAKE_FOURCC ('a', 'p', '4', 'h'):
5375 variant_descr = " 4:4:4:4";
5377 case GST_MAKE_FOURCC ('a', 'p', 'c', 'o'):
5379 variant_descr = " 4:2:2 Proxy";
5381 case GST_MAKE_FOURCC ('a', 'p', 'c', 'n'):
5383 variant = "standard";
5384 variant_descr = " 4:2:2 SD";
5388 GST_LOG ("Prores video, codec fourcc %" GST_FOURCC_FORMAT,
5389 GST_FOURCC_ARGS (fourcc));
5391 caps = gst_caps_new_simple ("video/x-prores",
5392 "format", G_TYPE_STRING, variant, NULL);
5393 *codec_name = g_strdup_printf ("Apple ProRes%s", variant_descr);
5394 context->postprocess_frame = gst_matroska_demux_add_prores_header;
5396 GST_WARNING ("Unknown codec '%s', cannot build Caps", codec_id);
5402 GstStructure *structure;
5404 for (i = 0; i < gst_caps_get_size (caps); i++) {
5405 structure = gst_caps_get_structure (caps, i);
5407 /* FIXME: use the real unit here! */
5408 GST_DEBUG ("video size %dx%d, target display size %dx%d (any unit)",
5409 videocontext->pixel_width,
5410 videocontext->pixel_height,
5411 videocontext->display_width, videocontext->display_height);
5413 /* pixel width and height are the w and h of the video in pixels */
5414 if (videocontext->pixel_width > 0 && videocontext->pixel_height > 0) {
5415 gint w = videocontext->pixel_width;
5416 gint h = videocontext->pixel_height;
5418 gst_structure_set (structure,
5419 "width", G_TYPE_INT, w, "height", G_TYPE_INT, h, NULL);
5422 if (videocontext->display_width > 0 || videocontext->display_height > 0) {
5425 if (videocontext->display_width <= 0)
5426 videocontext->display_width = videocontext->pixel_width;
5427 if (videocontext->display_height <= 0)
5428 videocontext->display_height = videocontext->pixel_height;
5430 /* calculate the pixel aspect ratio using the display and pixel w/h */
5431 n = videocontext->display_width * videocontext->pixel_height;
5432 d = videocontext->display_height * videocontext->pixel_width;
5433 GST_DEBUG ("setting PAR to %d/%d", n, d);
5434 gst_structure_set (structure, "pixel-aspect-ratio",
5436 videocontext->display_width * videocontext->pixel_height,
5437 videocontext->display_height * videocontext->pixel_width, NULL);
5440 if (videocontext->default_fps > 0.0) {
5443 gst_util_double_to_fraction (videocontext->default_fps, &fps_n, &fps_d);
5445 GST_DEBUG ("using default fps %d/%d", fps_n, fps_d);
5447 gst_structure_set (structure, "framerate", GST_TYPE_FRACTION, fps_n,
5449 } else if (context->default_duration > 0) {
5452 gst_video_guess_framerate (context->default_duration, &fps_n, &fps_d);
5454 GST_INFO ("using default duration %" G_GUINT64_FORMAT
5455 " framerate %d/%d", context->default_duration, fps_n, fps_d);
5457 gst_structure_set (structure, "framerate", GST_TYPE_FRACTION,
5458 fps_n, fps_d, NULL);
5460 gst_structure_set (structure, "framerate", GST_TYPE_FRACTION,
5464 if (videocontext->parent.flags & GST_MATROSKA_VIDEOTRACK_INTERLACED)
5465 gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
5468 if (videocontext->multiview_mode != GST_VIDEO_MULTIVIEW_MODE_NONE) {
5469 if (gst_video_multiview_guess_half_aspect (videocontext->multiview_mode,
5470 videocontext->pixel_width, videocontext->pixel_height,
5471 videocontext->display_width * videocontext->pixel_height,
5472 videocontext->display_height * videocontext->pixel_width)) {
5473 videocontext->multiview_flags |= GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT;
5475 gst_caps_set_simple (caps,
5476 "multiview-mode", G_TYPE_STRING,
5477 gst_video_multiview_mode_to_caps_string
5478 (videocontext->multiview_mode), "multiview-flags",
5479 GST_TYPE_VIDEO_MULTIVIEW_FLAGSET, videocontext->multiview_flags,
5480 GST_FLAG_SET_MASK_EXACT, NULL);
5483 caps = gst_caps_simplify (caps);
5490 * Some AAC specific code... *sigh*
5491 * FIXME: maybe we should use '15' and code the sample rate explicitly
5492 * if the sample rate doesn't match the predefined rates exactly? (tpm)
5496 aac_rate_idx (gint rate)
5500 else if (75132 <= rate)
5502 else if (55426 <= rate)
5504 else if (46009 <= rate)
5506 else if (37566 <= rate)
5508 else if (27713 <= rate)
5510 else if (23004 <= rate)
5512 else if (18783 <= rate)
5514 else if (13856 <= rate)
5516 else if (11502 <= rate)
5518 else if (9391 <= rate)
5525 aac_profile_idx (const gchar * codec_id)
5529 if (strlen (codec_id) <= 12)
5531 else if (!strncmp (&codec_id[12], "MAIN", 4))
5533 else if (!strncmp (&codec_id[12], "LC", 2))
5535 else if (!strncmp (&codec_id[12], "SSR", 3))
5544 round_up_pow2 (guint n)
5555 #define AAC_SYNC_EXTENSION_TYPE 0x02b7
5558 gst_matroska_demux_audio_caps (GstMatroskaTrackAudioContext *
5559 audiocontext, const gchar * codec_id, guint8 * data, guint size,
5560 gchar ** codec_name, guint16 * riff_audio_fmt)
5562 GstMatroskaTrackContext *context = (GstMatroskaTrackContext *) audiocontext;
5563 GstCaps *caps = NULL;
5565 g_assert (audiocontext != NULL);
5566 g_assert (codec_name != NULL);
5569 *riff_audio_fmt = 0;
5571 /* TODO: check if we have all codec types from matroska-ids.h
5572 * check if we have to do more special things with codec_private
5573 * check if we need bitdepth in different places too
5574 * implement channel position magic
5576 * GST_MATROSKA_CODEC_ID_AUDIO_AC3_BSID9
5577 * GST_MATROSKA_CODEC_ID_AUDIO_AC3_BSID10
5578 * GST_MATROSKA_CODEC_ID_AUDIO_QUICKTIME_QDMC
5579 * GST_MATROSKA_CODEC_ID_AUDIO_QUICKTIME_QDM2
5582 if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L1) ||
5583 !strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L2) ||
5584 !strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L3)) {
5587 if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L1))
5589 else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L2))
5594 caps = gst_caps_new_simple ("audio/mpeg",
5595 "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer, NULL);
5596 *codec_name = g_strdup_printf ("MPEG-1 layer %d", layer);
5597 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE) ||
5598 !strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_PCM_INT_LE)) {
5601 GstAudioFormat format;
5603 sign = (audiocontext->bitdepth != 8);
5604 if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE))
5605 endianness = G_BIG_ENDIAN;
5607 endianness = G_LITTLE_ENDIAN;
5609 format = gst_audio_format_build_integer (sign, endianness,
5610 audiocontext->bitdepth, audiocontext->bitdepth);
5612 /* FIXME: Channel mask and reordering */
5613 caps = gst_caps_new_simple ("audio/x-raw",
5614 "format", G_TYPE_STRING, gst_audio_format_to_string (format),
5615 "layout", G_TYPE_STRING, "interleaved",
5616 "channel-mask", GST_TYPE_BITMASK,
5617 gst_audio_channel_get_fallback_mask (audiocontext->channels), NULL);
5619 *codec_name = g_strdup_printf ("Raw %d-bit PCM audio",
5620 audiocontext->bitdepth);
5621 context->alignment = GST_ROUND_UP_8 (audiocontext->bitdepth) / 8;
5622 context->alignment = round_up_pow2 (context->alignment);
5623 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_PCM_FLOAT)) {
5624 const gchar *format;
5625 if (audiocontext->bitdepth == 32)
5629 /* FIXME: Channel mask and reordering */
5630 caps = gst_caps_new_simple ("audio/x-raw",
5631 "format", G_TYPE_STRING, format,
5632 "layout", G_TYPE_STRING, "interleaved",
5633 "channel-mask", GST_TYPE_BITMASK,
5634 gst_audio_channel_get_fallback_mask (audiocontext->channels), NULL);
5635 *codec_name = g_strdup_printf ("Raw %d-bit floating-point audio",
5636 audiocontext->bitdepth);
5637 context->alignment = audiocontext->bitdepth / 8;
5638 context->alignment = round_up_pow2 (context->alignment);
5639 } else if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_AC3,
5640 strlen (GST_MATROSKA_CODEC_ID_AUDIO_AC3))) {
5641 caps = gst_caps_new_simple ("audio/x-ac3",
5642 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
5643 *codec_name = g_strdup ("AC-3 audio");
5644 } else if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_EAC3,
5645 strlen (GST_MATROSKA_CODEC_ID_AUDIO_EAC3))) {
5646 caps = gst_caps_new_simple ("audio/x-eac3",
5647 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
5648 *codec_name = g_strdup ("E-AC-3 audio");
5649 } else if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_TRUEHD,
5650 strlen (GST_MATROSKA_CODEC_ID_AUDIO_TRUEHD))) {
5651 caps = gst_caps_new_empty_simple ("audio/x-true-hd");
5652 *codec_name = g_strdup ("Dolby TrueHD");
5653 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_DTS)) {
5654 caps = gst_caps_new_empty_simple ("audio/x-dts");
5655 *codec_name = g_strdup ("DTS audio");
5656 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_VORBIS)) {
5657 caps = gst_caps_new_empty_simple ("audio/x-vorbis");
5658 context->stream_headers =
5659 gst_matroska_parse_xiph_stream_headers (context->codec_priv,
5660 context->codec_priv_size);
5661 /* FIXME: mark stream as broken and skip if there are no stream headers */
5662 context->send_stream_headers = TRUE;
5663 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_FLAC)) {
5664 caps = gst_caps_new_empty_simple ("audio/x-flac");
5665 context->stream_headers =
5666 gst_matroska_parse_flac_stream_headers (context->codec_priv,
5667 context->codec_priv_size);
5668 /* FIXME: mark stream as broken and skip if there are no stream headers */
5669 context->send_stream_headers = TRUE;
5670 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_SPEEX)) {
5671 caps = gst_caps_new_empty_simple ("audio/x-speex");
5672 context->stream_headers =
5673 gst_matroska_parse_speex_stream_headers (context->codec_priv,
5674 context->codec_priv_size);
5675 /* FIXME: mark stream as broken and skip if there are no stream headers */
5676 context->send_stream_headers = TRUE;
5677 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_OPUS)) {
5680 if (context->codec_priv_size >= 19) {
5681 if (audiocontext->samplerate)
5682 GST_WRITE_UINT32_LE ((guint8 *) context->codec_priv + 12,
5683 audiocontext->samplerate);
5684 if (context->codec_delay) {
5686 gst_util_uint64_scale_round (context->codec_delay, 48000,
5688 GST_WRITE_UINT16_LE ((guint8 *) context->codec_priv + 10, delay);
5692 gst_buffer_new_wrapped (g_memdup (context->codec_priv,
5693 context->codec_priv_size), context->codec_priv_size);
5694 caps = gst_codec_utils_opus_create_caps_from_header (tmp, NULL);
5695 gst_buffer_unref (tmp);
5696 *codec_name = g_strdup ("Opus");
5697 } else if (context->codec_priv_size == 0) {
5698 GST_WARNING ("No Opus codec data found, trying to create one");
5699 if (audiocontext->channels <= 2) {
5700 guint8 streams, coupled, channels;
5704 audiocontext->samplerate == 0 ? 48000 : audiocontext->samplerate;
5705 channels = audiocontext->channels == 0 ? 2 : audiocontext->channels;
5706 if (channels == 1) {
5715 gst_codec_utils_opus_create_caps (samplerate, channels, 0, streams,
5718 *codec_name = g_strdup ("Opus");
5720 GST_WARNING ("Failed to create Opus caps from audio context");
5723 GST_WARNING ("No Opus codec data, and not enough info to create one");
5726 GST_WARNING ("Invalid Opus codec data size (got %" G_GSIZE_FORMAT
5727 ", expected 19)", context->codec_priv_size);
5729 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_ACM)) {
5730 gst_riff_strf_auds auds;
5732 if (data && size >= 18) {
5733 GstBuffer *codec_data = NULL;
5735 /* little-endian -> byte-order */
5736 auds.format = GST_READ_UINT16_LE (data);
5737 auds.channels = GST_READ_UINT16_LE (data + 2);
5738 auds.rate = GST_READ_UINT32_LE (data + 4);
5739 auds.av_bps = GST_READ_UINT32_LE (data + 8);
5740 auds.blockalign = GST_READ_UINT16_LE (data + 12);
5741 auds.bits_per_sample = GST_READ_UINT16_LE (data + 16);
5743 /* 18 is the waveformatex size */
5745 codec_data = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY,
5746 data + 18, size - 18, 0, size - 18, NULL, NULL);
5750 *riff_audio_fmt = auds.format;
5752 /* FIXME: Handle reorder map */
5753 caps = gst_riff_create_audio_caps (auds.format, NULL, &auds, NULL,
5754 codec_data, codec_name, NULL);
5756 gst_buffer_unref (codec_data);
5759 GST_WARNING ("Unhandled RIFF audio format 0x%02x", auds.format);
5762 GST_WARNING ("Invalid codec data size (%d expected, got %d)", 18, size);
5764 } else if (g_str_has_prefix (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_AAC)) {
5765 GstBuffer *priv = NULL;
5767 gint rate_idx, profile;
5768 guint8 *data = NULL;
5770 /* unspecified AAC profile with opaque private codec data */
5771 if (strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_AAC) == 0) {
5772 if (context->codec_priv_size >= 2) {
5773 guint obj_type, freq_index, explicit_freq_bytes = 0;
5775 codec_id = GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG4;
5777 freq_index = (GST_READ_UINT16_BE (context->codec_priv) & 0x780) >> 7;
5778 obj_type = (GST_READ_UINT16_BE (context->codec_priv) & 0xF800) >> 11;
5779 if (freq_index == 15)
5780 explicit_freq_bytes = 3;
5781 GST_DEBUG ("obj_type = %u, freq_index = %u", obj_type, freq_index);
5782 priv = gst_buffer_new_wrapped (g_memdup (context->codec_priv,
5783 context->codec_priv_size), context->codec_priv_size);
5784 /* assume SBR if samplerate <= 24kHz */
5785 if (obj_type == 5 || (freq_index >= 6 && freq_index != 15) ||
5786 (context->codec_priv_size == (5 + explicit_freq_bytes))) {
5787 audiocontext->samplerate *= 2;
5790 GST_WARNING ("Opaque A_AAC codec ID, but no codec private data");
5791 /* this is pretty broken;
5792 * maybe we need to make up some default private,
5793 * or maybe ADTS data got dumped in.
5794 * Let's set up some private data now, and check actual data later */
5795 /* just try this and see what happens ... */
5796 codec_id = GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG4;
5797 context->postprocess_frame = gst_matroska_demux_check_aac;
5801 /* make up decoder-specific data if it is not supplied */
5805 priv = gst_buffer_new_allocate (NULL, 5, NULL);
5806 gst_buffer_map (priv, &map, GST_MAP_WRITE);
5808 rate_idx = aac_rate_idx (audiocontext->samplerate);
5809 profile = aac_profile_idx (codec_id);
5811 data[0] = ((profile + 1) << 3) | ((rate_idx & 0xE) >> 1);
5812 data[1] = ((rate_idx & 0x1) << 7) | (audiocontext->channels << 3);
5814 if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG2,
5815 strlen (GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG2))) {
5817 gst_buffer_unmap (priv, &map);
5818 gst_buffer_set_size (priv, 2);
5819 } else if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG4,
5820 strlen (GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG4))) {
5823 if (g_strrstr (codec_id, "SBR")) {
5824 /* HE-AAC (aka SBR AAC) */
5825 audiocontext->samplerate *= 2;
5826 rate_idx = aac_rate_idx (audiocontext->samplerate);
5827 data[2] = AAC_SYNC_EXTENSION_TYPE >> 3;
5828 data[3] = ((AAC_SYNC_EXTENSION_TYPE & 0x07) << 5) | 5;
5829 data[4] = (1 << 7) | (rate_idx << 3);
5830 gst_buffer_unmap (priv, &map);
5832 gst_buffer_unmap (priv, &map);
5833 gst_buffer_set_size (priv, 2);
5836 gst_buffer_unmap (priv, &map);
5837 gst_buffer_unref (priv);
5839 GST_ERROR ("Unknown AAC profile and no codec private data");
5844 caps = gst_caps_new_simple ("audio/mpeg",
5845 "mpegversion", G_TYPE_INT, mpegversion,
5846 "framed", G_TYPE_BOOLEAN, TRUE,
5847 "stream-format", G_TYPE_STRING, "raw", NULL);
5848 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, NULL);
5849 if (context->codec_priv && context->codec_priv_size > 0)
5850 gst_codec_utils_aac_caps_set_level_and_profile (caps,
5851 context->codec_priv, context->codec_priv_size);
5852 *codec_name = g_strdup_printf ("MPEG-%d AAC audio", mpegversion);
5853 gst_buffer_unref (priv);
5855 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_TTA)) {
5856 caps = gst_caps_new_simple ("audio/x-tta",
5857 "width", G_TYPE_INT, audiocontext->bitdepth, NULL);
5858 *codec_name = g_strdup ("TTA audio");
5859 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_WAVPACK4)) {
5860 caps = gst_caps_new_simple ("audio/x-wavpack",
5861 "width", G_TYPE_INT, audiocontext->bitdepth,
5862 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
5863 *codec_name = g_strdup ("Wavpack audio");
5864 context->postprocess_frame = gst_matroska_demux_add_wvpk_header;
5865 audiocontext->wvpk_block_index = 0;
5866 } else if ((!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_14_4)) ||
5867 (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_28_8)) ||
5868 (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_COOK))) {
5869 gint raversion = -1;
5871 if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_14_4))
5873 else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_COOK))
5878 caps = gst_caps_new_simple ("audio/x-pn-realaudio",
5879 "raversion", G_TYPE_INT, raversion, NULL);
5880 /* Extract extra information from caps, mapping varies based on codec */
5881 if (data && (size >= 0x50)) {
5888 guint extra_data_size;
5890 GST_ERROR ("real audio raversion:%d", raversion);
5891 if (raversion == 8) {
5893 flavor = GST_READ_UINT16_BE (data + 22);
5894 packet_size = GST_READ_UINT32_BE (data + 24);
5895 height = GST_READ_UINT16_BE (data + 40);
5896 leaf_size = GST_READ_UINT16_BE (data + 44);
5897 sample_width = GST_READ_UINT16_BE (data + 58);
5898 extra_data_size = GST_READ_UINT32_BE (data + 74);
5901 ("flavor:%d, packet_size:%d, height:%d, leaf_size:%d, sample_width:%d, extra_data_size:%d",
5902 flavor, packet_size, height, leaf_size, sample_width,
5904 gst_caps_set_simple (caps, "flavor", G_TYPE_INT, flavor, "packet_size",
5905 G_TYPE_INT, packet_size, "height", G_TYPE_INT, height, "leaf_size",
5906 G_TYPE_INT, leaf_size, "width", G_TYPE_INT, sample_width, NULL);
5908 if ((size - 78) >= extra_data_size) {
5909 priv = gst_buffer_new_wrapped (g_memdup (data + 78, extra_data_size),
5911 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, NULL);
5912 gst_buffer_unref (priv);
5917 *codec_name = g_strdup_printf ("RealAudio %d.0", raversion);
5918 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_SIPR)) {
5919 caps = gst_caps_new_empty_simple ("audio/x-sipro");
5920 *codec_name = g_strdup ("Sipro/ACELP.NET Voice Codec");
5921 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_RALF)) {
5922 caps = gst_caps_new_empty_simple ("audio/x-ralf-mpeg4-generic");
5923 *codec_name = g_strdup ("Real Audio Lossless");
5924 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_ATRC)) {
5925 caps = gst_caps_new_empty_simple ("audio/x-vnd.sony.atrac3");
5926 *codec_name = g_strdup ("Sony ATRAC3");
5928 GST_WARNING ("Unknown codec '%s', cannot build Caps", codec_id);
5933 if (audiocontext->samplerate > 0 && audiocontext->channels > 0) {
5936 for (i = 0; i < gst_caps_get_size (caps); i++) {
5937 gst_structure_set (gst_caps_get_structure (caps, i),
5938 "channels", G_TYPE_INT, audiocontext->channels,
5939 "rate", G_TYPE_INT, audiocontext->samplerate, NULL);
5943 caps = gst_caps_simplify (caps);
5950 gst_matroska_demux_subtitle_caps (GstMatroskaTrackSubtitleContext *
5951 subtitlecontext, const gchar * codec_id, gpointer data, guint size)
5953 GstCaps *caps = NULL;
5954 GstMatroskaTrackContext *context =
5955 (GstMatroskaTrackContext *) subtitlecontext;
5957 /* for backwards compatibility */
5958 if (!g_ascii_strcasecmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_ASCII))
5959 codec_id = GST_MATROSKA_CODEC_ID_SUBTITLE_UTF8;
5960 else if (!g_ascii_strcasecmp (codec_id, "S_SSA"))
5961 codec_id = GST_MATROSKA_CODEC_ID_SUBTITLE_SSA;
5962 else if (!g_ascii_strcasecmp (codec_id, "S_ASS"))
5963 codec_id = GST_MATROSKA_CODEC_ID_SUBTITLE_ASS;
5964 else if (!g_ascii_strcasecmp (codec_id, "S_USF"))
5965 codec_id = GST_MATROSKA_CODEC_ID_SUBTITLE_USF;
5967 /* TODO: Add GST_MATROSKA_CODEC_ID_SUBTITLE_BMP support
5968 * Check if we have to do something with codec_private */
5969 if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_UTF8)) {
5970 /* well, plain text simply does not have a lot of markup ... */
5971 caps = gst_caps_new_simple ("text/x-raw", "format", G_TYPE_STRING,
5972 "pango-markup", NULL);
5973 context->postprocess_frame = gst_matroska_demux_check_subtitle_buffer;
5974 subtitlecontext->check_markup = TRUE;
5975 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_SSA)) {
5976 caps = gst_caps_new_empty_simple ("application/x-ssa");
5977 context->postprocess_frame = gst_matroska_demux_check_subtitle_buffer;
5978 subtitlecontext->check_markup = FALSE;
5979 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_ASS)) {
5980 caps = gst_caps_new_empty_simple ("application/x-ass");
5981 context->postprocess_frame = gst_matroska_demux_check_subtitle_buffer;
5982 subtitlecontext->check_markup = FALSE;
5983 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_USF)) {
5984 caps = gst_caps_new_empty_simple ("application/x-usf");
5985 context->postprocess_frame = gst_matroska_demux_check_subtitle_buffer;
5986 subtitlecontext->check_markup = FALSE;
5987 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_VOBSUB)) {
5988 caps = gst_caps_new_empty_simple ("subpicture/x-dvd");
5989 ((GstMatroskaTrackContext *) subtitlecontext)->send_dvd_event = TRUE;
5990 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_HDMVPGS)) {
5991 caps = gst_caps_new_empty_simple ("subpicture/x-pgs");
5992 } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_KATE)) {
5993 caps = gst_caps_new_empty_simple ("subtitle/x-kate");
5994 context->stream_headers =
5995 gst_matroska_parse_xiph_stream_headers (context->codec_priv,
5996 context->codec_priv_size);
5997 /* FIXME: mark stream as broken and skip if there are no stream headers */
5998 context->send_stream_headers = TRUE;
6000 GST_DEBUG ("Unknown subtitle stream: codec_id='%s'", codec_id);
6001 caps = gst_caps_new_empty_simple ("application/x-subtitle-unknown");
6004 if (data != NULL && size > 0) {
6007 buf = gst_buffer_new_wrapped (g_memdup (data, size), size);
6008 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
6009 gst_buffer_unref (buf);
6017 gst_matroska_demux_set_index (GstElement * element, GstIndex * index)
6019 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
6021 GST_OBJECT_LOCK (demux);
6022 if (demux->common.element_index)
6023 gst_object_unref (demux->common.element_index);
6024 demux->common.element_index = index ? gst_object_ref (index) : NULL;
6025 GST_OBJECT_UNLOCK (demux);
6026 GST_DEBUG_OBJECT (demux, "Set index %" GST_PTR_FORMAT,
6027 demux->common.element_index);
6031 gst_matroska_demux_get_index (GstElement * element)
6033 GstIndex *result = NULL;
6034 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
6036 GST_OBJECT_LOCK (demux);
6037 if (demux->common.element_index)
6038 result = gst_object_ref (demux->common.element_index);
6039 GST_OBJECT_UNLOCK (demux);
6041 GST_DEBUG_OBJECT (demux, "Returning index %" GST_PTR_FORMAT, result);
6047 static GstStateChangeReturn
6048 gst_matroska_demux_change_state (GstElement * element,
6049 GstStateChange transition)
6051 GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
6052 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
6054 /* handle upwards state changes here */
6055 switch (transition) {
6060 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6062 /* handle downwards state changes */
6063 switch (transition) {
6064 case GST_STATE_CHANGE_PAUSED_TO_READY:
6065 gst_matroska_demux_reset (GST_ELEMENT (demux));
6075 gst_matroska_demux_set_property (GObject * object,
6076 guint prop_id, const GValue * value, GParamSpec * pspec)
6078 GstMatroskaDemux *demux;
6080 g_return_if_fail (GST_IS_MATROSKA_DEMUX (object));
6081 demux = GST_MATROSKA_DEMUX (object);
6084 case PROP_MAX_GAP_TIME:
6085 GST_OBJECT_LOCK (demux);
6086 demux->max_gap_time = g_value_get_uint64 (value);
6087 GST_OBJECT_UNLOCK (demux);
6090 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
6096 gst_matroska_demux_get_property (GObject * object,
6097 guint prop_id, GValue * value, GParamSpec * pspec)
6099 GstMatroskaDemux *demux;
6101 g_return_if_fail (GST_IS_MATROSKA_DEMUX (object));
6102 demux = GST_MATROSKA_DEMUX (object);
6105 case PROP_MAX_GAP_TIME:
6106 GST_OBJECT_LOCK (demux);
6107 g_value_set_uint64 (value, demux->max_gap_time);
6108 GST_OBJECT_UNLOCK (demux);
6111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
6117 gst_matroska_demux_plugin_init (GstPlugin * plugin)
6121 /* parser helper separate debug */
6122 GST_DEBUG_CATEGORY_INIT (ebmlread_debug, "ebmlread",
6123 0, "EBML stream helper class");
6125 /* create an elementfactory for the matroska_demux element */
6126 if (!gst_element_register (plugin, "matroskademux",
6127 GST_RANK_PRIMARY, GST_TYPE_MATROSKA_DEMUX))