matroskademux: resurrect some flow return handling
[platform/upstream/gst-plugins-good.git] / gst / matroska / matroska-demux.c
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>
6  *
7  * matroska-demux.c: matroska file/stream demuxer
8  *
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.
13  *
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.
18  *
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.
23  */
24
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
32  */
33
34 /**
35  * SECTION:element-matroskademux
36  *
37  * matroskademux demuxes a Matroska file into the different contained streams.
38  *
39  * <refsect2>
40  * <title>Example launch line</title>
41  * |[
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.
44  * </refsect2>
45  */
46
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include <math.h>
53 #include <string.h>
54 #include <glib/gprintf.h>
55
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>
61
62 #include <gst/audio/audio.h>
63 #include <gst/tag/tag.h>
64 #include <gst/pbutils/pbutils.h>
65 #include <gst/video/video.h>
66
67 #include "matroska-demux.h"
68 #include "matroska-ids.h"
69
70 GST_DEBUG_CATEGORY_STATIC (matroskademux_debug);
71 #define GST_CAT_DEFAULT matroskademux_debug
72
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))
76
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))
80
81 enum
82 {
83   ARG_0,
84   ARG_METADATA,
85   ARG_STREAMINFO,
86   ARG_MAX_GAP_TIME
87 };
88
89 #define  DEFAULT_MAX_GAP_TIME      (2 * GST_SECOND)
90
91 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
92     GST_PAD_SINK,
93     GST_PAD_ALWAYS,
94     GST_STATIC_CAPS ("audio/x-matroska; video/x-matroska; "
95         "video/x-matroska-3d; audio/webm; video/webm")
96     );
97
98 /* TODO: fill in caps! */
99
100 static GstStaticPadTemplate audio_src_templ =
101 GST_STATIC_PAD_TEMPLATE ("audio_%u",
102     GST_PAD_SRC,
103     GST_PAD_SOMETIMES,
104     GST_STATIC_CAPS ("ANY")
105     );
106
107 static GstStaticPadTemplate video_src_templ =
108 GST_STATIC_PAD_TEMPLATE ("video_%u",
109     GST_PAD_SRC,
110     GST_PAD_SOMETIMES,
111     GST_STATIC_CAPS ("ANY")
112     );
113
114 static GstStaticPadTemplate subtitle_src_templ =
115     GST_STATIC_PAD_TEMPLATE ("subtitle_%u",
116     GST_PAD_SRC,
117     GST_PAD_SOMETIMES,
118     GST_STATIC_CAPS ("text/x-raw, format=pango-markup; application/x-ssa; "
119         "application/x-ass;application/x-usf; subpicture/x-dvd; "
120         "subpicture/x-pgs; subtitle/x-kate; " "application/x-subtitle-unknown")
121     );
122
123 static GstFlowReturn gst_matroska_demux_parse_id (GstMatroskaDemux * demux,
124     guint32 id, guint64 length, guint needed);
125
126 /* element functions */
127 static void gst_matroska_demux_loop (GstPad * pad);
128
129 static gboolean gst_matroska_demux_element_send_event (GstElement * element,
130     GstEvent * event);
131 static gboolean gst_matroska_demux_element_query (GstElement * element,
132     GstQuery * query);
133
134 /* pad functions */
135 static gboolean gst_matroska_demux_sink_activate (GstPad * sinkpad,
136     GstObject * parent);
137 static gboolean gst_matroska_demux_sink_activate_mode (GstPad * sinkpad,
138     GstObject * parent, GstPadMode mode, gboolean active);
139
140 static gboolean gst_matroska_demux_handle_seek_event (GstMatroskaDemux * demux,
141     GstPad * pad, GstEvent * event);
142 static gboolean gst_matroska_demux_handle_src_event (GstPad * pad,
143     GstObject * parent, GstEvent * event);
144 static gboolean gst_matroska_demux_handle_src_query (GstPad * pad,
145     GstObject * parent, GstQuery * query);
146
147 static gboolean gst_matroska_demux_handle_sink_event (GstPad * pad,
148     GstObject * parent, GstEvent * event);
149 static GstFlowReturn gst_matroska_demux_chain (GstPad * pad,
150     GstObject * object, GstBuffer * buffer);
151
152 static GstStateChangeReturn
153 gst_matroska_demux_change_state (GstElement * element,
154     GstStateChange transition);
155 #if 0
156 static void
157 gst_matroska_demux_set_index (GstElement * element, GstIndex * index);
158 static GstIndex *gst_matroska_demux_get_index (GstElement * element);
159 #endif
160
161 /* caps functions */
162 static GstCaps *gst_matroska_demux_video_caps (GstMatroskaTrackVideoContext
163     * videocontext, const gchar * codec_id, guint8 * data, guint size,
164     gchar ** codec_name, guint32 * riff_fourcc);
165 static GstCaps *gst_matroska_demux_audio_caps (GstMatroskaTrackAudioContext
166     * audiocontext, const gchar * codec_id, guint8 * data, guint size,
167     gchar ** codec_name, guint16 * riff_audio_fmt);
168 static GstCaps
169     * gst_matroska_demux_subtitle_caps (GstMatroskaTrackSubtitleContext *
170     subtitlecontext, const gchar * codec_id, gpointer data, guint size);
171
172 /* stream methods */
173 static void gst_matroska_demux_reset (GstElement * element);
174 static gboolean perform_seek_to_offset (GstMatroskaDemux * demux,
175     gdouble rate, guint64 offset, guint32 seqnum);
176
177 /* gobject functions */
178 static void gst_matroska_demux_set_property (GObject * object,
179     guint prop_id, const GValue * value, GParamSpec * pspec);
180 static void gst_matroska_demux_get_property (GObject * object,
181     guint prop_id, GValue * value, GParamSpec * pspec);
182
183 GType gst_matroska_demux_get_type (void);
184 #define parent_class gst_matroska_demux_parent_class
185 G_DEFINE_TYPE (GstMatroskaDemux, gst_matroska_demux, GST_TYPE_ELEMENT);
186
187 static void
188 gst_matroska_demux_finalize (GObject * object)
189 {
190   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (object);
191
192   gst_matroska_read_common_finalize (&demux->common);
193   gst_flow_combiner_free (demux->flowcombiner);
194   G_OBJECT_CLASS (parent_class)->finalize (object);
195 }
196
197 static void
198 gst_matroska_demux_class_init (GstMatroskaDemuxClass * klass)
199 {
200   GObjectClass *gobject_class = (GObjectClass *) klass;
201   GstElementClass *gstelement_class = (GstElementClass *) klass;
202
203   GST_DEBUG_CATEGORY_INIT (matroskademux_debug, "matroskademux", 0,
204       "Matroska demuxer");
205
206   gobject_class->finalize = gst_matroska_demux_finalize;
207
208   gobject_class->get_property = gst_matroska_demux_get_property;
209   gobject_class->set_property = gst_matroska_demux_set_property;
210
211   g_object_class_install_property (gobject_class, ARG_MAX_GAP_TIME,
212       g_param_spec_uint64 ("max-gap-time", "Maximum gap time",
213           "The demuxer sends out segment events for skipping "
214           "gaps longer than this (0 = disabled).", 0, G_MAXUINT64,
215           DEFAULT_MAX_GAP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216
217   gstelement_class->change_state =
218       GST_DEBUG_FUNCPTR (gst_matroska_demux_change_state);
219   gstelement_class->send_event =
220       GST_DEBUG_FUNCPTR (gst_matroska_demux_element_send_event);
221   gstelement_class->query =
222       GST_DEBUG_FUNCPTR (gst_matroska_demux_element_query);
223 #if 0
224   gstelement_class->set_index =
225       GST_DEBUG_FUNCPTR (gst_matroska_demux_set_index);
226   gstelement_class->get_index =
227       GST_DEBUG_FUNCPTR (gst_matroska_demux_get_index);
228 #endif
229
230   gst_element_class_add_pad_template (gstelement_class,
231       gst_static_pad_template_get (&video_src_templ));
232   gst_element_class_add_pad_template (gstelement_class,
233       gst_static_pad_template_get (&audio_src_templ));
234   gst_element_class_add_pad_template (gstelement_class,
235       gst_static_pad_template_get (&subtitle_src_templ));
236   gst_element_class_add_pad_template (gstelement_class,
237       gst_static_pad_template_get (&sink_templ));
238
239   gst_element_class_set_static_metadata (gstelement_class, "Matroska demuxer",
240       "Codec/Demuxer",
241       "Demuxes Matroska/WebM streams into video/audio/subtitles",
242       "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
243 }
244
245 static void
246 gst_matroska_demux_init (GstMatroskaDemux * demux)
247 {
248   demux->common.sinkpad = gst_pad_new_from_static_template (&sink_templ,
249       "sink");
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);
259
260   /* init defaults for common read context */
261   gst_matroska_read_common_init (&demux->common);
262
263   /* property defaults */
264   demux->max_gap_time = DEFAULT_MAX_GAP_TIME;
265
266   GST_OBJECT_FLAG_SET (demux, GST_ELEMENT_FLAG_INDEXABLE);
267
268   demux->flowcombiner = gst_flow_combiner_new ();
269
270   /* finish off */
271   gst_matroska_demux_reset (GST_ELEMENT (demux));
272 }
273
274 static void
275 gst_matroska_demux_reset (GstElement * element)
276 {
277   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
278
279   GST_DEBUG_OBJECT (demux, "Resetting state");
280
281   gst_matroska_read_common_reset (GST_ELEMENT (demux), &demux->common);
282
283   demux->num_a_streams = 0;
284   demux->num_t_streams = 0;
285   demux->num_v_streams = 0;
286
287   demux->have_group_id = FALSE;
288   demux->group_id = G_MAXUINT;
289
290   demux->clock = NULL;
291   demux->tracks_parsed = FALSE;
292
293   if (demux->clusters) {
294     g_array_free (demux->clusters, TRUE);
295     demux->clusters = NULL;
296   }
297
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;
302
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;
320   }
321
322   demux->seek_index = NULL;
323   demux->seek_entry = 0;
324
325   if (demux->new_segment) {
326     gst_event_unref (demux->new_segment);
327     demux->new_segment = NULL;
328   }
329
330   demux->invalid_duration = FALSE;
331
332   demux->cached_length = G_MAXUINT64;
333
334   gst_flow_combiner_clear (demux->flowcombiner);
335 }
336
337 static GstBuffer *
338 gst_matroska_decode_buffer (GstMatroskaTrackContext * context, GstBuffer * buf)
339 {
340   GstMapInfo map;
341   gpointer data;
342   gsize size;
343
344   g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
345
346   GST_DEBUG ("decoding buffer %p", buf);
347
348   gst_buffer_map (buf, &map, GST_MAP_READ);
349   data = map.data;
350   size = map.size;
351
352   g_return_val_if_fail (size > 0, buf);
353
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);
359   } else {
360     GST_DEBUG ("decode data failed");
361     gst_buffer_unmap (buf, &map);
362     gst_buffer_unref (buf);
363     return NULL;
364   }
365 }
366
367 static void
368 gst_matroska_demux_add_stream_headers_to_caps (GstMatroskaDemux * demux,
369     GstBufferList * list, GstCaps * caps)
370 {
371   GstStructure *s;
372   GValue arr_val = G_VALUE_INIT;
373   GValue buf_val = G_VALUE_INIT;
374   gint i, num;
375
376   g_assert (gst_caps_is_writable (caps));
377
378   g_value_init (&arr_val, GST_TYPE_ARRAY);
379   g_value_init (&buf_val, GST_TYPE_BUFFER);
380
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);
385   }
386
387   s = gst_caps_get_structure (caps, 0);
388   gst_structure_take_value (s, "streamheader", &arr_val);
389   g_value_unset (&buf_val);
390 }
391
392 static GstFlowReturn
393 gst_matroska_demux_add_stream (GstMatroskaDemux * demux, GstEbmlRead * ebml)
394 {
395   GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
396   GstMatroskaTrackContext *context;
397   GstPadTemplate *templ = NULL;
398   GstStreamFlags stream_flags;
399   GstCaps *caps = NULL;
400   gchar *padname = NULL;
401   GstFlowReturn ret;
402   guint32 id, riff_fourcc = 0;
403   guint16 riff_audio_fmt = 0;
404   GstEvent *stream_start;
405   gchar *codec = NULL;
406   gchar *stream_id;
407
408   DEBUG_ELEMENT_START (demux, ebml, "TrackEntry");
409
410   /* start with the master */
411   if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
412     DEBUG_ELEMENT_STOP (demux, ebml, "TrackEntry", ret);
413     return ret;
414   }
415
416   /* allocate generic... if we know the type, we'll g_renew()
417    * with the precise type */
418   context = g_new0 (GstMatroskaTrackContext, 1);
419   g_ptr_array_add (demux->common.src, context);
420   context->index = demux->common.num_streams;
421   context->index_writer_id = -1;
422   context->type = 0;            /* no type yet */
423   context->default_duration = 0;
424   context->pos = 0;
425   context->set_discont = TRUE;
426   context->timecodescale = 1.0;
427   context->flags =
428       GST_MATROSKA_TRACK_ENABLED | GST_MATROSKA_TRACK_DEFAULT |
429       GST_MATROSKA_TRACK_LACING;
430   context->from_time = GST_CLOCK_TIME_NONE;
431   context->from_offset = -1;
432   context->to_offset = G_MAXINT64;
433   context->alignment = 1;
434   context->dts_only = FALSE;
435   context->intra_only = FALSE;
436   context->tags = gst_tag_list_new_empty ();
437   demux->common.num_streams++;
438   g_assert (demux->common.src->len == demux->common.num_streams);
439
440   GST_DEBUG_OBJECT (demux, "Stream number %d", context->index);
441
442   /* try reading the trackentry headers */
443   while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
444     if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
445       break;
446
447     switch (id) {
448         /* track number (unique stream ID) */
449       case GST_MATROSKA_ID_TRACKNUMBER:{
450         guint64 num;
451
452         if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
453           break;
454
455         if (num == 0) {
456           GST_ERROR_OBJECT (demux, "Invalid TrackNumber 0");
457           ret = GST_FLOW_ERROR;
458           break;
459         } else if (!gst_matroska_read_common_tracknumber_unique (&demux->common,
460                 num)) {
461           GST_ERROR_OBJECT (demux, "TrackNumber %" G_GUINT64_FORMAT
462               " is not unique", num);
463           ret = GST_FLOW_ERROR;
464           break;
465         }
466
467         GST_DEBUG_OBJECT (demux, "TrackNumber: %" G_GUINT64_FORMAT, num);
468         context->num = num;
469         break;
470       }
471         /* track UID (unique identifier) */
472       case GST_MATROSKA_ID_TRACKUID:{
473         guint64 num;
474
475         if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
476           break;
477
478         if (num == 0) {
479           GST_ERROR_OBJECT (demux, "Invalid TrackUID 0");
480           ret = GST_FLOW_ERROR;
481           break;
482         }
483
484         GST_DEBUG_OBJECT (demux, "TrackUID: %" G_GUINT64_FORMAT, num);
485         context->uid = num;
486         break;
487       }
488
489         /* track type (video, audio, combined, subtitle, etc.) */
490       case GST_MATROSKA_ID_TRACKTYPE:{
491         guint64 track_type;
492
493         if ((ret = gst_ebml_read_uint (ebml, &id, &track_type)) != GST_FLOW_OK) {
494           break;
495         }
496
497         if (context->type != 0 && context->type != track_type) {
498           GST_WARNING_OBJECT (demux,
499               "More than one tracktype defined in a TrackEntry - skipping");
500           break;
501         } else if (track_type < 1 || track_type > 254) {
502           GST_WARNING_OBJECT (demux, "Invalid TrackType %" G_GUINT64_FORMAT,
503               track_type);
504           break;
505         }
506
507         GST_DEBUG_OBJECT (demux, "TrackType: %" G_GUINT64_FORMAT, track_type);
508
509         /* ok, so we're actually going to reallocate this thing */
510         switch (track_type) {
511           case GST_MATROSKA_TRACK_TYPE_VIDEO:
512             gst_matroska_track_init_video_context (&context);
513             break;
514           case GST_MATROSKA_TRACK_TYPE_AUDIO:
515             gst_matroska_track_init_audio_context (&context);
516             break;
517           case GST_MATROSKA_TRACK_TYPE_SUBTITLE:
518             gst_matroska_track_init_subtitle_context (&context);
519             break;
520           case GST_MATROSKA_TRACK_TYPE_COMPLEX:
521           case GST_MATROSKA_TRACK_TYPE_LOGO:
522           case GST_MATROSKA_TRACK_TYPE_BUTTONS:
523           case GST_MATROSKA_TRACK_TYPE_CONTROL:
524           default:
525             GST_WARNING_OBJECT (demux,
526                 "Unknown or unsupported TrackType %" G_GUINT64_FORMAT,
527                 track_type);
528             context->type = 0;
529             break;
530         }
531         g_ptr_array_index (demux->common.src, demux->common.num_streams - 1)
532             = context;
533         break;
534       }
535
536         /* tracktype specific stuff for video */
537       case GST_MATROSKA_ID_TRACKVIDEO:{
538         GstMatroskaTrackVideoContext *videocontext;
539
540         DEBUG_ELEMENT_START (demux, ebml, "TrackVideo");
541
542         if (!gst_matroska_track_init_video_context (&context)) {
543           GST_WARNING_OBJECT (demux,
544               "TrackVideo element in non-video track - ignoring track");
545           ret = GST_FLOW_ERROR;
546           break;
547         } else if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
548           break;
549         }
550         videocontext = (GstMatroskaTrackVideoContext *) context;
551         g_ptr_array_index (demux->common.src, demux->common.num_streams - 1)
552             = context;
553
554         while (ret == GST_FLOW_OK &&
555             gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
556           if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
557             break;
558
559           switch (id) {
560               /* Should be one level up but some broken muxers write it here. */
561             case GST_MATROSKA_ID_TRACKDEFAULTDURATION:{
562               guint64 num;
563
564               if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
565                 break;
566
567               if (num == 0) {
568                 GST_WARNING_OBJECT (demux, "Invalid TrackDefaultDuration 0");
569                 break;
570               }
571
572               GST_DEBUG_OBJECT (demux,
573                   "TrackDefaultDuration: %" G_GUINT64_FORMAT, num);
574               context->default_duration = num;
575               break;
576             }
577
578               /* video framerate */
579               /* NOTE: This one is here only for backward compatibility.
580                * Use _TRACKDEFAULDURATION one level up. */
581             case GST_MATROSKA_ID_VIDEOFRAMERATE:{
582               gdouble num;
583
584               if ((ret = gst_ebml_read_float (ebml, &id, &num)) != GST_FLOW_OK)
585                 break;
586
587               if (num <= 0.0) {
588                 GST_WARNING_OBJECT (demux, "Invalid TrackVideoFPS %lf", num);
589                 break;
590               }
591
592               GST_DEBUG_OBJECT (demux, "TrackVideoFrameRate: %lf", num);
593               if (context->default_duration == 0)
594                 context->default_duration =
595                     gst_gdouble_to_guint64 ((gdouble) GST_SECOND * (1.0 / num));
596               videocontext->default_fps = num;
597               break;
598             }
599
600               /* width of the size to display the video at */
601             case GST_MATROSKA_ID_VIDEODISPLAYWIDTH:{
602               guint64 num;
603
604               if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
605                 break;
606
607               if (num == 0) {
608                 GST_WARNING_OBJECT (demux, "Invalid TrackVideoDisplayWidth 0");
609                 break;
610               }
611
612               GST_DEBUG_OBJECT (demux,
613                   "TrackVideoDisplayWidth: %" G_GUINT64_FORMAT, num);
614               videocontext->display_width = num;
615               break;
616             }
617
618               /* height of the size to display the video at */
619             case GST_MATROSKA_ID_VIDEODISPLAYHEIGHT:{
620               guint64 num;
621
622               if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
623                 break;
624
625               if (num == 0) {
626                 GST_WARNING_OBJECT (demux, "Invalid TrackVideoDisplayHeight 0");
627                 break;
628               }
629
630               GST_DEBUG_OBJECT (demux,
631                   "TrackVideoDisplayHeight: %" G_GUINT64_FORMAT, num);
632               videocontext->display_height = num;
633               break;
634             }
635
636               /* width of the video in the file */
637             case GST_MATROSKA_ID_VIDEOPIXELWIDTH:{
638               guint64 num;
639
640               if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
641                 break;
642
643               if (num == 0) {
644                 GST_WARNING_OBJECT (demux, "Invalid TrackVideoPixelWidth 0");
645                 break;
646               }
647
648               GST_DEBUG_OBJECT (demux,
649                   "TrackVideoPixelWidth: %" G_GUINT64_FORMAT, num);
650               videocontext->pixel_width = num;
651               break;
652             }
653
654               /* height of the video in the file */
655             case GST_MATROSKA_ID_VIDEOPIXELHEIGHT:{
656               guint64 num;
657
658               if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
659                 break;
660
661               if (num == 0) {
662                 GST_WARNING_OBJECT (demux, "Invalid TrackVideoPixelHeight 0");
663                 break;
664               }
665
666               GST_DEBUG_OBJECT (demux,
667                   "TrackVideoPixelHeight: %" G_GUINT64_FORMAT, num);
668               videocontext->pixel_height = num;
669               break;
670             }
671
672               /* whether the video is interlaced */
673             case GST_MATROSKA_ID_VIDEOFLAGINTERLACED:{
674               guint64 num;
675
676               if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
677                 break;
678
679               if (num)
680                 context->flags |= GST_MATROSKA_VIDEOTRACK_INTERLACED;
681               else
682                 context->flags &= ~GST_MATROSKA_VIDEOTRACK_INTERLACED;
683               GST_DEBUG_OBJECT (demux, "TrackVideoInterlaced: %d",
684                   (context->flags & GST_MATROSKA_VIDEOTRACK_INTERLACED) ? 1 :
685                   0);
686               break;
687             }
688
689               /* aspect ratio behaviour */
690             case GST_MATROSKA_ID_VIDEOASPECTRATIOTYPE:{
691               guint64 num;
692
693               if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
694                 break;
695
696               if (num != GST_MATROSKA_ASPECT_RATIO_MODE_FREE &&
697                   num != GST_MATROSKA_ASPECT_RATIO_MODE_KEEP &&
698                   num != GST_MATROSKA_ASPECT_RATIO_MODE_FIXED) {
699                 GST_WARNING_OBJECT (demux,
700                     "Unknown TrackVideoAspectRatioType 0x%x", (guint) num);
701                 break;
702               }
703               GST_DEBUG_OBJECT (demux,
704                   "TrackVideoAspectRatioType: %" G_GUINT64_FORMAT, num);
705               videocontext->asr_mode = num;
706               break;
707             }
708
709               /* colourspace (only matters for raw video) fourcc */
710             case GST_MATROSKA_ID_VIDEOCOLOURSPACE:{
711               guint8 *data;
712               guint64 datalen;
713
714               if ((ret =
715                       gst_ebml_read_binary (ebml, &id, &data,
716                           &datalen)) != GST_FLOW_OK)
717                 break;
718
719               if (datalen != 4) {
720                 g_free (data);
721                 GST_WARNING_OBJECT (demux,
722                     "Invalid TrackVideoColourSpace length %" G_GUINT64_FORMAT,
723                     datalen);
724                 break;
725               }
726
727               memcpy (&videocontext->fourcc, data, 4);
728               GST_DEBUG_OBJECT (demux,
729                   "TrackVideoColourSpace: %" GST_FOURCC_FORMAT,
730                   GST_FOURCC_ARGS (videocontext->fourcc));
731               g_free (data);
732               break;
733             }
734
735             default:
736               GST_WARNING_OBJECT (demux,
737                   "Unknown TrackVideo subelement 0x%x - ignoring", id);
738               /* fall through */
739             case GST_MATROSKA_ID_VIDEOSTEREOMODE:
740             case GST_MATROSKA_ID_VIDEODISPLAYUNIT:
741             case GST_MATROSKA_ID_VIDEOPIXELCROPBOTTOM:
742             case GST_MATROSKA_ID_VIDEOPIXELCROPTOP:
743             case GST_MATROSKA_ID_VIDEOPIXELCROPLEFT:
744             case GST_MATROSKA_ID_VIDEOPIXELCROPRIGHT:
745             case GST_MATROSKA_ID_VIDEOGAMMAVALUE:
746               ret = gst_ebml_read_skip (ebml);
747               break;
748           }
749         }
750
751         DEBUG_ELEMENT_STOP (demux, ebml, "TrackVideo", ret);
752         break;
753       }
754
755         /* tracktype specific stuff for audio */
756       case GST_MATROSKA_ID_TRACKAUDIO:{
757         GstMatroskaTrackAudioContext *audiocontext;
758
759         DEBUG_ELEMENT_START (demux, ebml, "TrackAudio");
760
761         if (!gst_matroska_track_init_audio_context (&context)) {
762           GST_WARNING_OBJECT (demux,
763               "TrackAudio element in non-audio track - ignoring track");
764           ret = GST_FLOW_ERROR;
765           break;
766         }
767
768         if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK)
769           break;
770
771         audiocontext = (GstMatroskaTrackAudioContext *) context;
772         g_ptr_array_index (demux->common.src, demux->common.num_streams - 1)
773             = context;
774
775         while (ret == GST_FLOW_OK &&
776             gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
777           if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
778             break;
779
780           switch (id) {
781               /* samplerate */
782             case GST_MATROSKA_ID_AUDIOSAMPLINGFREQ:{
783               gdouble num;
784
785               if ((ret = gst_ebml_read_float (ebml, &id, &num)) != GST_FLOW_OK)
786                 break;
787
788
789               if (num <= 0.0) {
790                 GST_WARNING_OBJECT (demux,
791                     "Invalid TrackAudioSamplingFrequency %lf", num);
792                 break;
793               }
794
795               GST_DEBUG_OBJECT (demux, "TrackAudioSamplingFrequency: %lf", num);
796               audiocontext->samplerate = num;
797               break;
798             }
799
800               /* bitdepth */
801             case GST_MATROSKA_ID_AUDIOBITDEPTH:{
802               guint64 num;
803
804               if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
805                 break;
806
807               if (num == 0) {
808                 GST_WARNING_OBJECT (demux, "Invalid TrackAudioBitDepth 0");
809                 break;
810               }
811
812               GST_DEBUG_OBJECT (demux, "TrackAudioBitDepth: %" G_GUINT64_FORMAT,
813                   num);
814               audiocontext->bitdepth = num;
815               break;
816             }
817
818               /* channels */
819             case GST_MATROSKA_ID_AUDIOCHANNELS:{
820               guint64 num;
821
822               if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
823                 break;
824
825               if (num == 0) {
826                 GST_WARNING_OBJECT (demux, "Invalid TrackAudioChannels 0");
827                 break;
828               }
829
830               GST_DEBUG_OBJECT (demux, "TrackAudioChannels: %" G_GUINT64_FORMAT,
831                   num);
832               audiocontext->channels = num;
833               break;
834             }
835
836             default:
837               GST_WARNING_OBJECT (demux,
838                   "Unknown TrackAudio subelement 0x%x - ignoring", id);
839               /* fall through */
840             case GST_MATROSKA_ID_AUDIOCHANNELPOSITIONS:
841             case GST_MATROSKA_ID_AUDIOOUTPUTSAMPLINGFREQ:
842               ret = gst_ebml_read_skip (ebml);
843               break;
844           }
845         }
846
847         DEBUG_ELEMENT_STOP (demux, ebml, "TrackAudio", ret);
848
849         break;
850       }
851
852         /* codec identifier */
853       case GST_MATROSKA_ID_CODECID:{
854         gchar *text;
855
856         if ((ret = gst_ebml_read_ascii (ebml, &id, &text)) != GST_FLOW_OK)
857           break;
858
859         GST_DEBUG_OBJECT (demux, "CodecID: %s", GST_STR_NULL (text));
860         context->codec_id = text;
861         break;
862       }
863
864         /* codec private data */
865       case GST_MATROSKA_ID_CODECPRIVATE:{
866         guint8 *data;
867         guint64 size;
868
869         if ((ret =
870                 gst_ebml_read_binary (ebml, &id, &data, &size)) != GST_FLOW_OK)
871           break;
872
873         context->codec_priv = data;
874         context->codec_priv_size = size;
875
876         GST_DEBUG_OBJECT (demux, "CodecPrivate of size %" G_GUINT64_FORMAT,
877             size);
878         break;
879       }
880
881         /* name of the codec */
882       case GST_MATROSKA_ID_CODECNAME:{
883         gchar *text;
884
885         if ((ret = gst_ebml_read_utf8 (ebml, &id, &text)) != GST_FLOW_OK)
886           break;
887
888         GST_DEBUG_OBJECT (demux, "CodecName: %s", GST_STR_NULL (text));
889         context->codec_name = text;
890         break;
891       }
892
893         /* name of this track */
894       case GST_MATROSKA_ID_TRACKNAME:{
895         gchar *text;
896
897         if ((ret = gst_ebml_read_utf8 (ebml, &id, &text)) != GST_FLOW_OK)
898           break;
899
900         context->name = text;
901         GST_DEBUG_OBJECT (demux, "TrackName: %s", GST_STR_NULL (text));
902         break;
903       }
904
905         /* language (matters for audio/subtitles, mostly) */
906       case GST_MATROSKA_ID_TRACKLANGUAGE:{
907         gchar *text;
908
909         if ((ret = gst_ebml_read_utf8 (ebml, &id, &text)) != GST_FLOW_OK)
910           break;
911
912
913         context->language = text;
914
915         /* fre-ca => fre */
916         if (strlen (context->language) >= 4 && context->language[3] == '-')
917           context->language[3] = '\0';
918
919         GST_DEBUG_OBJECT (demux, "TrackLanguage: %s",
920             GST_STR_NULL (context->language));
921         break;
922       }
923
924         /* whether this is actually used */
925       case GST_MATROSKA_ID_TRACKFLAGENABLED:{
926         guint64 num;
927
928         if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
929           break;
930
931         if (num)
932           context->flags |= GST_MATROSKA_TRACK_ENABLED;
933         else
934           context->flags &= ~GST_MATROSKA_TRACK_ENABLED;
935
936         GST_DEBUG_OBJECT (demux, "TrackEnabled: %d",
937             (context->flags & GST_MATROSKA_TRACK_ENABLED) ? 1 : 0);
938         break;
939       }
940
941         /* whether it's the default for this track type */
942       case GST_MATROSKA_ID_TRACKFLAGDEFAULT:{
943         guint64 num;
944
945         if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
946           break;
947
948         if (num)
949           context->flags |= GST_MATROSKA_TRACK_DEFAULT;
950         else
951           context->flags &= ~GST_MATROSKA_TRACK_DEFAULT;
952
953         GST_DEBUG_OBJECT (demux, "TrackDefault: %d",
954             (context->flags & GST_MATROSKA_TRACK_DEFAULT) ? 1 : 0);
955         break;
956       }
957
958         /* whether the track must be used during playback */
959       case GST_MATROSKA_ID_TRACKFLAGFORCED:{
960         guint64 num;
961
962         if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
963           break;
964
965         if (num)
966           context->flags |= GST_MATROSKA_TRACK_FORCED;
967         else
968           context->flags &= ~GST_MATROSKA_TRACK_FORCED;
969
970         GST_DEBUG_OBJECT (demux, "TrackForced: %d",
971             (context->flags & GST_MATROSKA_TRACK_FORCED) ? 1 : 0);
972         break;
973       }
974
975         /* lacing (like MPEG, where blocks don't end/start on frame
976          * boundaries) */
977       case GST_MATROSKA_ID_TRACKFLAGLACING:{
978         guint64 num;
979
980         if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
981           break;
982
983         if (num)
984           context->flags |= GST_MATROSKA_TRACK_LACING;
985         else
986           context->flags &= ~GST_MATROSKA_TRACK_LACING;
987
988         GST_DEBUG_OBJECT (demux, "TrackLacing: %d",
989             (context->flags & GST_MATROSKA_TRACK_LACING) ? 1 : 0);
990         break;
991       }
992
993         /* default length (in time) of one data block in this track */
994       case GST_MATROSKA_ID_TRACKDEFAULTDURATION:{
995         guint64 num;
996
997         if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
998           break;
999
1000
1001         if (num == 0) {
1002           GST_WARNING_OBJECT (demux, "Invalid TrackDefaultDuration 0");
1003           break;
1004         }
1005
1006         GST_DEBUG_OBJECT (demux, "TrackDefaultDuration: %" G_GUINT64_FORMAT,
1007             num);
1008         context->default_duration = num;
1009         break;
1010       }
1011
1012       case GST_MATROSKA_ID_CONTENTENCODINGS:{
1013         ret = gst_matroska_read_common_read_track_encodings (&demux->common,
1014             ebml, context);
1015         break;
1016       }
1017
1018       case GST_MATROSKA_ID_TRACKTIMECODESCALE:{
1019         gdouble num;
1020
1021         if ((ret = gst_ebml_read_float (ebml, &id, &num)) != GST_FLOW_OK)
1022           break;
1023
1024         if (num <= 0.0) {
1025           GST_WARNING_OBJECT (demux, "Invalid TrackTimeCodeScale %lf", num);
1026           break;
1027         }
1028
1029         GST_DEBUG_OBJECT (demux, "TrackTimeCodeScale: %lf", num);
1030         context->timecodescale = num;
1031         break;
1032       }
1033
1034       default:
1035         GST_WARNING ("Unknown TrackEntry subelement 0x%x - ignoring", id);
1036         /* pass-through */
1037
1038         /* we ignore these because they're nothing useful (i.e. crap)
1039          * or simply not implemented yet. */
1040       case GST_MATROSKA_ID_TRACKMINCACHE:
1041       case GST_MATROSKA_ID_TRACKMAXCACHE:
1042       case GST_MATROSKA_ID_MAXBLOCKADDITIONID:
1043       case GST_MATROSKA_ID_TRACKATTACHMENTLINK:
1044       case GST_MATROSKA_ID_TRACKOVERLAY:
1045       case GST_MATROSKA_ID_TRACKTRANSLATE:
1046       case GST_MATROSKA_ID_TRACKOFFSET:
1047       case GST_MATROSKA_ID_CODECSETTINGS:
1048       case GST_MATROSKA_ID_CODECINFOURL:
1049       case GST_MATROSKA_ID_CODECDOWNLOADURL:
1050       case GST_MATROSKA_ID_CODECDECODEALL:
1051         ret = gst_ebml_read_skip (ebml);
1052         break;
1053     }
1054   }
1055
1056   DEBUG_ELEMENT_STOP (demux, ebml, "TrackEntry", ret);
1057
1058   /* Decode codec private data if necessary */
1059   if (context->encodings && context->encodings->len > 0 && context->codec_priv
1060       && context->codec_priv_size > 0) {
1061     if (!gst_matroska_decode_data (context->encodings,
1062             &context->codec_priv, &context->codec_priv_size,
1063             GST_MATROSKA_TRACK_ENCODING_SCOPE_CODEC_DATA, TRUE)) {
1064       GST_WARNING_OBJECT (demux, "Decoding codec private data failed");
1065       ret = GST_FLOW_ERROR;
1066     }
1067   }
1068
1069   if (context->type == 0 || context->codec_id == NULL || (ret != GST_FLOW_OK
1070           && ret != GST_FLOW_EOS)) {
1071     if (ret == GST_FLOW_OK || ret == GST_FLOW_EOS)
1072       GST_WARNING_OBJECT (ebml, "Unknown stream/codec in track entry header");
1073
1074     demux->common.num_streams--;
1075     g_ptr_array_remove_index (demux->common.src, demux->common.num_streams);
1076     g_assert (demux->common.src->len == demux->common.num_streams);
1077     if (context) {
1078       gst_matroska_track_free (context);
1079     }
1080
1081     return ret;
1082   }
1083
1084   /* now create the GStreamer connectivity */
1085   switch (context->type) {
1086     case GST_MATROSKA_TRACK_TYPE_VIDEO:{
1087       GstMatroskaTrackVideoContext *videocontext =
1088           (GstMatroskaTrackVideoContext *) context;
1089
1090       padname = g_strdup_printf ("video_%u", demux->num_v_streams++);
1091       templ = gst_element_class_get_pad_template (klass, "video_%u");
1092       caps = gst_matroska_demux_video_caps (videocontext,
1093           context->codec_id, context->codec_priv,
1094           context->codec_priv_size, &codec, &riff_fourcc);
1095
1096       if (codec) {
1097         gst_tag_list_add (context->tags, GST_TAG_MERGE_REPLACE,
1098             GST_TAG_VIDEO_CODEC, codec, NULL);
1099         context->tags_changed = TRUE;
1100         g_free (codec);
1101       }
1102       break;
1103     }
1104
1105     case GST_MATROSKA_TRACK_TYPE_AUDIO:{
1106       GstMatroskaTrackAudioContext *audiocontext =
1107           (GstMatroskaTrackAudioContext *) context;
1108
1109       padname = g_strdup_printf ("audio_%u", demux->num_a_streams++);
1110       templ = gst_element_class_get_pad_template (klass, "audio_%u");
1111       caps = gst_matroska_demux_audio_caps (audiocontext,
1112           context->codec_id, context->codec_priv, context->codec_priv_size,
1113           &codec, &riff_audio_fmt);
1114
1115       if (codec) {
1116         gst_tag_list_add (context->tags, GST_TAG_MERGE_REPLACE,
1117             GST_TAG_AUDIO_CODEC, codec, NULL);
1118         context->tags_changed = TRUE;
1119         g_free (codec);
1120       }
1121       break;
1122     }
1123
1124     case GST_MATROSKA_TRACK_TYPE_SUBTITLE:{
1125       GstMatroskaTrackSubtitleContext *subtitlecontext =
1126           (GstMatroskaTrackSubtitleContext *) context;
1127
1128       padname = g_strdup_printf ("subtitle_%u", demux->num_t_streams++);
1129       templ = gst_element_class_get_pad_template (klass, "subtitle_%u");
1130       caps = gst_matroska_demux_subtitle_caps (subtitlecontext,
1131           context->codec_id, context->codec_priv, context->codec_priv_size);
1132       break;
1133     }
1134
1135     case GST_MATROSKA_TRACK_TYPE_COMPLEX:
1136     case GST_MATROSKA_TRACK_TYPE_LOGO:
1137     case GST_MATROSKA_TRACK_TYPE_BUTTONS:
1138     case GST_MATROSKA_TRACK_TYPE_CONTROL:
1139     default:
1140       /* we should already have quit by now */
1141       g_assert_not_reached ();
1142   }
1143
1144   if ((context->language == NULL || *context->language == '\0') &&
1145       (context->type == GST_MATROSKA_TRACK_TYPE_AUDIO ||
1146           context->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)) {
1147     GST_LOG ("stream %d: language=eng (assuming default)", context->index);
1148     context->language = g_strdup ("eng");
1149   }
1150
1151   if (context->language) {
1152     const gchar *lang;
1153
1154     /* Matroska contains ISO 639-2B codes, we want ISO 639-1 */
1155     lang = gst_tag_get_language_code (context->language);
1156     gst_tag_list_add (context->tags, GST_TAG_MERGE_REPLACE,
1157         GST_TAG_LANGUAGE_CODE, (lang) ? lang : context->language, NULL);
1158     context->tags_changed = TRUE;
1159   }
1160
1161   if (caps == NULL) {
1162     GST_WARNING_OBJECT (demux, "could not determine caps for stream with "
1163         "codec_id='%s'", context->codec_id);
1164     switch (context->type) {
1165       case GST_MATROSKA_TRACK_TYPE_VIDEO:
1166         caps = gst_caps_new_empty_simple ("video/x-unknown");
1167         break;
1168       case GST_MATROSKA_TRACK_TYPE_AUDIO:
1169         caps = gst_caps_new_empty_simple ("audio/x-unknown");
1170         break;
1171       case GST_MATROSKA_TRACK_TYPE_SUBTITLE:
1172         caps = gst_caps_new_empty_simple ("application/x-subtitle-unknown");
1173         break;
1174       case GST_MATROSKA_TRACK_TYPE_COMPLEX:
1175       default:
1176         caps = gst_caps_new_empty_simple ("application/x-matroska-unknown");
1177         break;
1178     }
1179     gst_caps_set_simple (caps, "codec-id", G_TYPE_STRING, context->codec_id,
1180         NULL);
1181
1182     /* add any unrecognised riff fourcc / audio format, but after codec-id */
1183     if (context->type == GST_MATROSKA_TRACK_TYPE_AUDIO && riff_audio_fmt != 0)
1184       gst_caps_set_simple (caps, "format", G_TYPE_INT, riff_audio_fmt, NULL);
1185     else if (context->type == GST_MATROSKA_TRACK_TYPE_VIDEO && riff_fourcc != 0) {
1186       gchar *fstr = g_strdup_printf ("%" GST_FOURCC_FORMAT,
1187           GST_FOURCC_ARGS (riff_fourcc));
1188       gst_caps_set_simple (caps, "fourcc", G_TYPE_STRING, fstr, NULL);
1189       g_free (fstr);
1190     }
1191   } else if (context->stream_headers != NULL) {
1192     gst_matroska_demux_add_stream_headers_to_caps (demux,
1193         context->stream_headers, caps);
1194   }
1195
1196   /* the pad in here */
1197   context->pad = gst_pad_new_from_template (templ, padname);
1198   context->caps = caps;
1199
1200   gst_pad_set_event_function (context->pad,
1201       GST_DEBUG_FUNCPTR (gst_matroska_demux_handle_src_event));
1202   gst_pad_set_query_function (context->pad,
1203       GST_DEBUG_FUNCPTR (gst_matroska_demux_handle_src_query));
1204
1205   GST_INFO_OBJECT (demux, "Adding pad '%s' with caps %" GST_PTR_FORMAT,
1206       padname, caps);
1207
1208   gst_pad_set_element_private (context->pad, context);
1209
1210   gst_pad_use_fixed_caps (context->pad);
1211   gst_pad_set_active (context->pad, TRUE);
1212
1213   stream_id =
1214       gst_pad_create_stream_id_printf (context->pad, GST_ELEMENT_CAST (demux),
1215       "%03" G_GUINT64_FORMAT, context->uid);
1216   stream_start =
1217       gst_pad_get_sticky_event (demux->common.sinkpad, GST_EVENT_STREAM_START,
1218       0);
1219   if (stream_start) {
1220     if (gst_event_parse_group_id (stream_start, &demux->group_id))
1221       demux->have_group_id = TRUE;
1222     else
1223       demux->have_group_id = FALSE;
1224     gst_event_unref (stream_start);
1225   } else if (!demux->have_group_id) {
1226     demux->have_group_id = TRUE;
1227     demux->group_id = gst_util_group_id_next ();
1228   }
1229
1230   stream_start = gst_event_new_stream_start (stream_id);
1231   g_free (stream_id);
1232   if (demux->have_group_id)
1233     gst_event_set_group_id (stream_start, demux->group_id);
1234   stream_flags = GST_STREAM_FLAG_NONE;
1235   if (context->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
1236     stream_flags |= GST_STREAM_FLAG_SPARSE;
1237   if (context->flags & GST_MATROSKA_TRACK_DEFAULT)
1238     stream_flags |= GST_STREAM_FLAG_SELECT;
1239   gst_event_set_stream_flags (stream_start, stream_flags);
1240   gst_pad_push_event (context->pad, stream_start);
1241   gst_pad_set_caps (context->pad, context->caps);
1242
1243   gst_element_add_pad (GST_ELEMENT (demux), context->pad);
1244   gst_flow_combiner_add_pad (demux->flowcombiner, context->pad);
1245
1246   g_free (padname);
1247
1248   /* tadaah! */
1249   return ret;
1250 }
1251
1252 static gboolean
1253 gst_matroska_demux_query (GstMatroskaDemux * demux, GstPad * pad,
1254     GstQuery * query)
1255 {
1256   gboolean res = FALSE;
1257   GstMatroskaTrackContext *context = NULL;
1258
1259   if (pad) {
1260     context = gst_pad_get_element_private (pad);
1261   }
1262
1263   switch (GST_QUERY_TYPE (query)) {
1264     case GST_QUERY_POSITION:
1265     {
1266       GstFormat format;
1267
1268       gst_query_parse_position (query, &format, NULL);
1269
1270       res = TRUE;
1271       if (format == GST_FORMAT_TIME) {
1272         GST_OBJECT_LOCK (demux);
1273         if (context)
1274           gst_query_set_position (query, GST_FORMAT_TIME,
1275               MAX (context->pos, demux->stream_start_time) -
1276               demux->stream_start_time);
1277         else
1278           gst_query_set_position (query, GST_FORMAT_TIME,
1279               MAX (demux->common.segment.position, demux->stream_start_time) -
1280               demux->stream_start_time);
1281         GST_OBJECT_UNLOCK (demux);
1282       } else if (format == GST_FORMAT_DEFAULT && context
1283           && context->default_duration) {
1284         GST_OBJECT_LOCK (demux);
1285         gst_query_set_position (query, GST_FORMAT_DEFAULT,
1286             context->pos / context->default_duration);
1287         GST_OBJECT_UNLOCK (demux);
1288       } else {
1289         GST_DEBUG_OBJECT (demux,
1290             "only position query in TIME and DEFAULT format is supported");
1291         res = FALSE;
1292       }
1293
1294       break;
1295     }
1296     case GST_QUERY_DURATION:
1297     {
1298       GstFormat format;
1299
1300       gst_query_parse_duration (query, &format, NULL);
1301
1302       res = TRUE;
1303       if (format == GST_FORMAT_TIME) {
1304         GST_OBJECT_LOCK (demux);
1305         gst_query_set_duration (query, GST_FORMAT_TIME,
1306             demux->common.segment.duration);
1307         GST_OBJECT_UNLOCK (demux);
1308       } else if (format == GST_FORMAT_DEFAULT && context
1309           && context->default_duration) {
1310         GST_OBJECT_LOCK (demux);
1311         gst_query_set_duration (query, GST_FORMAT_DEFAULT,
1312             demux->common.segment.duration / context->default_duration);
1313         GST_OBJECT_UNLOCK (demux);
1314       } else {
1315         GST_DEBUG_OBJECT (demux,
1316             "only duration query in TIME and DEFAULT format is supported");
1317         res = FALSE;
1318       }
1319       break;
1320     }
1321
1322     case GST_QUERY_SEEKING:
1323     {
1324       GstFormat fmt;
1325
1326       gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
1327       GST_OBJECT_LOCK (demux);
1328       if (fmt == GST_FORMAT_TIME) {
1329         gboolean seekable;
1330
1331         if (demux->streaming) {
1332           /* assuming we'll be able to get an index ... */
1333           seekable = demux->seekable;
1334         } else {
1335           seekable = TRUE;
1336         }
1337
1338         gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
1339             0, demux->common.segment.duration);
1340         res = TRUE;
1341       }
1342       GST_OBJECT_UNLOCK (demux);
1343       break;
1344     }
1345     case GST_QUERY_SEGMENT:
1346     {
1347       GstFormat format;
1348       gint64 start, stop;
1349
1350       format = demux->common.segment.format;
1351
1352       start =
1353           gst_segment_to_stream_time (&demux->common.segment, format,
1354           demux->common.segment.start);
1355       if ((stop = demux->common.segment.stop) == -1)
1356         stop = demux->common.segment.duration;
1357       else
1358         stop =
1359             gst_segment_to_stream_time (&demux->common.segment, format, stop);
1360
1361       gst_query_set_segment (query, demux->common.segment.rate, format, start,
1362           stop);
1363       res = TRUE;
1364       break;
1365     }
1366     default:
1367       if (pad)
1368         res = gst_pad_query_default (pad, (GstObject *) demux, query);
1369       else
1370         res =
1371             GST_ELEMENT_CLASS (parent_class)->query (GST_ELEMENT_CAST (demux),
1372             query);
1373       break;
1374   }
1375
1376   return res;
1377 }
1378
1379 static gboolean
1380 gst_matroska_demux_element_query (GstElement * element, GstQuery * query)
1381 {
1382   return gst_matroska_demux_query (GST_MATROSKA_DEMUX (element), NULL, query);
1383 }
1384
1385 static gboolean
1386 gst_matroska_demux_handle_src_query (GstPad * pad, GstObject * parent,
1387     GstQuery * query)
1388 {
1389   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
1390
1391   return gst_matroska_demux_query (demux, pad, query);
1392 }
1393
1394 /* returns FALSE if there are no pads to deliver event to,
1395  * otherwise TRUE (whatever the outcome of event sending),
1396  * takes ownership of the passed event! */
1397 static gboolean
1398 gst_matroska_demux_send_event (GstMatroskaDemux * demux, GstEvent * event)
1399 {
1400   gboolean ret = FALSE;
1401   gint i;
1402
1403   g_return_val_if_fail (event != NULL, FALSE);
1404
1405   GST_DEBUG_OBJECT (demux, "Sending event of type %s to all source pads",
1406       GST_EVENT_TYPE_NAME (event));
1407
1408   g_assert (demux->common.src->len == demux->common.num_streams);
1409   for (i = 0; i < demux->common.src->len; i++) {
1410     GstMatroskaTrackContext *stream;
1411
1412     stream = g_ptr_array_index (demux->common.src, i);
1413     gst_event_ref (event);
1414     gst_pad_push_event (stream->pad, event);
1415     ret = TRUE;
1416   }
1417
1418   gst_event_unref (event);
1419   return ret;
1420 }
1421
1422 static void
1423 gst_matroska_demux_send_tags (GstMatroskaDemux * demux)
1424 {
1425   gint i;
1426
1427   if (G_UNLIKELY (demux->common.global_tags_changed)) {
1428     GstEvent *tag_event;
1429     gst_tag_list_add (demux->common.global_tags, GST_TAG_MERGE_REPLACE,
1430         GST_TAG_CONTAINER_FORMAT, "Matroska", NULL);
1431     GST_DEBUG_OBJECT (demux, "Sending global_tags %p : %" GST_PTR_FORMAT,
1432         demux->common.global_tags, demux->common.global_tags);
1433
1434     tag_event =
1435         gst_event_new_tag (gst_tag_list_copy (demux->common.global_tags));
1436
1437     for (i = 0; i < demux->common.src->len; i++) {
1438       GstMatroskaTrackContext *stream;
1439
1440       stream = g_ptr_array_index (demux->common.src, i);
1441       gst_pad_push_event (stream->pad, gst_event_ref (tag_event));
1442     }
1443
1444     gst_event_unref (tag_event);
1445     demux->common.global_tags_changed = FALSE;
1446   }
1447
1448   g_assert (demux->common.src->len == demux->common.num_streams);
1449   for (i = 0; i < demux->common.src->len; i++) {
1450     GstMatroskaTrackContext *stream;
1451
1452     stream = g_ptr_array_index (demux->common.src, i);
1453
1454     if (G_UNLIKELY (stream->tags_changed)) {
1455       GST_DEBUG_OBJECT (demux, "Sending tags %p for pad %s:%s : %"
1456           GST_PTR_FORMAT, stream->tags,
1457           GST_DEBUG_PAD_NAME (stream->pad), stream->tags);
1458       gst_pad_push_event (stream->pad,
1459           gst_event_new_tag (gst_tag_list_copy (stream->tags)));
1460       stream->tags_changed = FALSE;
1461     }
1462   }
1463 }
1464
1465 static gboolean
1466 gst_matroska_demux_element_send_event (GstElement * element, GstEvent * event)
1467 {
1468   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
1469   gboolean res;
1470
1471   g_return_val_if_fail (event != NULL, FALSE);
1472
1473   if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
1474     res = gst_matroska_demux_handle_seek_event (demux, NULL, event);
1475   } else {
1476     GST_WARNING_OBJECT (demux, "Unhandled event of type %s",
1477         GST_EVENT_TYPE_NAME (event));
1478     res = FALSE;
1479   }
1480   gst_event_unref (event);
1481   return res;
1482 }
1483
1484 static gboolean
1485 gst_matroska_demux_move_to_entry (GstMatroskaDemux * demux,
1486     GstMatroskaIndex * entry, gboolean reset, gboolean update)
1487 {
1488   gint i;
1489
1490   GST_OBJECT_LOCK (demux);
1491
1492   if (update) {
1493     /* seek (relative to matroska segment) */
1494     /* position might be invalid; will error when streaming resumes ... */
1495     demux->common.offset = entry->pos + demux->common.ebml_segment_start;
1496     demux->next_cluster_offset = 0;
1497
1498     GST_DEBUG_OBJECT (demux,
1499         "Seeked to offset %" G_GUINT64_FORMAT ", block %d, " "time %"
1500         GST_TIME_FORMAT, entry->pos + demux->common.ebml_segment_start,
1501         entry->block, GST_TIME_ARGS (entry->time));
1502
1503     /* update the time */
1504     gst_matroska_read_common_reset_streams (&demux->common, entry->time, TRUE);
1505     gst_flow_combiner_reset (demux->flowcombiner);
1506     demux->common.segment.position = entry->time;
1507     demux->seek_block = entry->block;
1508     demux->seek_first = TRUE;
1509     demux->last_stop_end = GST_CLOCK_TIME_NONE;
1510   }
1511
1512   for (i = 0; i < demux->common.src->len; i++) {
1513     GstMatroskaTrackContext *stream = g_ptr_array_index (demux->common.src, i);
1514
1515     if (reset) {
1516       stream->to_offset = G_MAXINT64;
1517     } else {
1518       if (stream->from_offset != -1)
1519         stream->to_offset = stream->from_offset;
1520     }
1521     stream->from_offset = -1;
1522     stream->from_time = GST_CLOCK_TIME_NONE;
1523   }
1524
1525   GST_OBJECT_UNLOCK (demux);
1526
1527   return TRUE;
1528 }
1529
1530 static gint
1531 gst_matroska_cluster_compare (gint64 * i1, gint64 * i2)
1532 {
1533   if (*i1 < *i2)
1534     return -1;
1535   else if (*i1 > *i2)
1536     return 1;
1537   else
1538     return 0;
1539 }
1540
1541 /* searches for a cluster start from @pos,
1542  * return GST_FLOW_OK and cluster position in @pos if found */
1543 static GstFlowReturn
1544 gst_matroska_demux_search_cluster (GstMatroskaDemux * demux, gint64 * pos)
1545 {
1546   gint64 newpos = *pos;
1547   gint64 orig_offset;
1548   GstFlowReturn ret = GST_FLOW_OK;
1549   const guint chunk = 64 * 1024;
1550   GstBuffer *buf = NULL;
1551   GstMapInfo map;
1552   gpointer data = NULL;
1553   gsize size;
1554   guint64 length;
1555   guint32 id;
1556   guint needed;
1557   gint64 oldpos, oldlength;
1558
1559   orig_offset = demux->common.offset;
1560
1561   GST_LOG_OBJECT (demux, "searching cluster following offset %" G_GINT64_FORMAT,
1562       *pos);
1563
1564   if (demux->clusters) {
1565     gint64 *cpos;
1566
1567     cpos = gst_util_array_binary_search (demux->clusters->data,
1568         demux->clusters->len, sizeof (gint64),
1569         (GCompareDataFunc) gst_matroska_cluster_compare,
1570         GST_SEARCH_MODE_AFTER, pos, NULL);
1571     /* sanity check */
1572     if (cpos) {
1573       GST_DEBUG_OBJECT (demux,
1574           "cluster reported at offset %" G_GINT64_FORMAT, *cpos);
1575       demux->common.offset = *cpos;
1576       ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
1577           GST_ELEMENT_CAST (demux), &id, &length, &needed);
1578       if (ret == GST_FLOW_OK && id == GST_MATROSKA_ID_CLUSTER) {
1579         newpos = *cpos;
1580         goto exit;
1581       }
1582     }
1583   }
1584
1585   /* read in at newpos and scan for ebml cluster id */
1586   oldpos = oldlength = -1;
1587   while (1) {
1588     GstByteReader reader;
1589     gint cluster_pos;
1590
1591     if (buf != NULL) {
1592       gst_buffer_unmap (buf, &map);
1593       gst_buffer_unref (buf);
1594       buf = NULL;
1595     }
1596     ret = gst_pad_pull_range (demux->common.sinkpad, newpos, chunk, &buf);
1597     if (ret != GST_FLOW_OK)
1598       break;
1599     GST_DEBUG_OBJECT (demux,
1600         "read buffer size %" G_GSIZE_FORMAT " at offset %" G_GINT64_FORMAT,
1601         gst_buffer_get_size (buf), newpos);
1602     gst_buffer_map (buf, &map, GST_MAP_READ);
1603     data = map.data;
1604     size = map.size;
1605     if (oldpos == newpos && oldlength == map.size) {
1606       GST_ERROR_OBJECT (demux, "Stuck at same position");
1607       ret = GST_FLOW_ERROR;
1608       goto exit;
1609     } else {
1610       oldpos = newpos;
1611       oldlength = map.size;
1612     }
1613
1614     gst_byte_reader_init (&reader, data, size);
1615   resume:
1616     cluster_pos = gst_byte_reader_masked_scan_uint32 (&reader, 0xffffffff,
1617         GST_MATROSKA_ID_CLUSTER, 0, gst_byte_reader_get_remaining (&reader));
1618     if (cluster_pos >= 0) {
1619       newpos += cluster_pos;
1620       /* prepare resuming at next byte */
1621       if (!gst_byte_reader_skip (&reader, cluster_pos + 1)) {
1622         GST_DEBUG_OBJECT (demux, "Need more data -> continue");
1623         continue;
1624       }
1625       GST_DEBUG_OBJECT (demux,
1626           "found cluster ebml id at offset %" G_GINT64_FORMAT, newpos);
1627       /* extra checks whether we really sync'ed to a cluster:
1628        * - either it is the first and only cluster
1629        * - either there is a cluster after this one
1630        * - either cluster length is undefined
1631        */
1632       /* ok if first cluster (there may not a subsequent one) */
1633       if (newpos == demux->first_cluster_offset) {
1634         GST_DEBUG_OBJECT (demux, "cluster is first cluster -> OK");
1635         break;
1636       }
1637       demux->common.offset = newpos;
1638       ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
1639           GST_ELEMENT_CAST (demux), &id, &length, &needed);
1640       if (ret != GST_FLOW_OK) {
1641         GST_DEBUG_OBJECT (demux, "need more data -> continue");
1642         continue;
1643       }
1644       g_assert (id == GST_MATROSKA_ID_CLUSTER);
1645       GST_DEBUG_OBJECT (demux, "cluster size %" G_GUINT64_FORMAT ", prefix %d",
1646           length, needed);
1647       /* ok if undefined length or first cluster */
1648       if (length == GST_EBML_SIZE_UNKNOWN || length == G_MAXUINT64) {
1649         GST_DEBUG_OBJECT (demux, "cluster has undefined length -> OK");
1650         break;
1651       }
1652       /* skip cluster */
1653       demux->common.offset += length + needed;
1654       ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
1655           GST_ELEMENT_CAST (demux), &id, &length, &needed);
1656       if (ret != GST_FLOW_OK)
1657         goto resume;
1658       GST_DEBUG_OBJECT (demux, "next element is %scluster",
1659           id == GST_MATROSKA_ID_CLUSTER ? "" : "not ");
1660       if (id == GST_MATROSKA_ID_CLUSTER)
1661         break;
1662       /* not ok, resume */
1663       goto resume;
1664     } else {
1665       /* partial cluster id may have been in tail of buffer */
1666       newpos += MAX (gst_byte_reader_get_remaining (&reader), 4) - 3;
1667     }
1668   }
1669
1670   if (buf) {
1671     gst_buffer_unmap (buf, &map);
1672     gst_buffer_unref (buf);
1673     buf = NULL;
1674   }
1675
1676 exit:
1677   demux->common.offset = orig_offset;
1678   *pos = newpos;
1679   return ret;
1680 }
1681
1682 /* bisect and scan through file for cluster starting before @time,
1683  * returns fake index entry with corresponding info on cluster */
1684 static GstMatroskaIndex *
1685 gst_matroska_demux_search_pos (GstMatroskaDemux * demux, GstClockTime time)
1686 {
1687   GstMatroskaIndex *entry = NULL;
1688   GstMatroskaReadState current_state;
1689   GstClockTime otime, prev_cluster_time, current_cluster_time, cluster_time;
1690   gint64 opos, newpos, startpos = 0, current_offset;
1691   gint64 prev_cluster_offset = -1, current_cluster_offset, cluster_offset;
1692   const guint chunk = 64 * 1024;
1693   GstFlowReturn ret;
1694   guint64 length;
1695   guint32 id;
1696   guint needed;
1697
1698   /* (under)estimate new position, resync using cluster ebml id,
1699    * and scan forward to appropriate cluster
1700    * (and re-estimate if need to go backward) */
1701
1702   prev_cluster_time = GST_CLOCK_TIME_NONE;
1703
1704   /* store some current state */
1705   current_state = demux->common.state;
1706   g_return_val_if_fail (current_state == GST_MATROSKA_READ_STATE_DATA, NULL);
1707
1708   current_cluster_offset = demux->cluster_offset;
1709   current_cluster_time = demux->cluster_time;
1710   current_offset = demux->common.offset;
1711
1712   demux->common.state = GST_MATROSKA_READ_STATE_SCANNING;
1713
1714   /* estimate using start and current position */
1715   GST_OBJECT_LOCK (demux);
1716   opos = demux->common.offset - demux->common.ebml_segment_start;
1717   otime = demux->common.segment.position;
1718   GST_OBJECT_UNLOCK (demux);
1719
1720   /* sanitize */
1721   time = MAX (time, demux->stream_start_time);
1722
1723   /* avoid division by zero in first estimation below */
1724   if (otime <= demux->stream_start_time)
1725     otime = time;
1726
1727 retry:
1728   GST_LOG_OBJECT (demux,
1729       "opos: %" G_GUINT64_FORMAT ", otime: %" GST_TIME_FORMAT ", %"
1730       GST_TIME_FORMAT " in stream time (start %" GST_TIME_FORMAT "), time %"
1731       GST_TIME_FORMAT, opos, GST_TIME_ARGS (otime),
1732       GST_TIME_ARGS (otime - demux->stream_start_time),
1733       GST_TIME_ARGS (demux->stream_start_time), GST_TIME_ARGS (time));
1734
1735   if (otime <= demux->stream_start_time) {
1736     newpos = 0;
1737   } else {
1738     newpos =
1739         gst_util_uint64_scale (opos - demux->common.ebml_segment_start,
1740         time - demux->stream_start_time,
1741         otime - demux->stream_start_time) - chunk;
1742     if (newpos < 0)
1743       newpos = 0;
1744   }
1745   /* favour undershoot */
1746   newpos = newpos * 90 / 100;
1747   newpos += demux->common.ebml_segment_start;
1748
1749   GST_DEBUG_OBJECT (demux,
1750       "estimated offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
1751       GST_TIME_ARGS (time), newpos);
1752
1753   /* and at least start scanning before previous scan start to avoid looping */
1754   startpos = startpos * 90 / 100;
1755   if (startpos && startpos < newpos)
1756     newpos = startpos;
1757
1758   /* read in at newpos and scan for ebml cluster id */
1759   startpos = newpos;
1760   while (1) {
1761
1762     ret = gst_matroska_demux_search_cluster (demux, &newpos);
1763     if (ret == GST_FLOW_EOS) {
1764       /* heuristic HACK */
1765       newpos = startpos * 80 / 100;
1766       GST_DEBUG_OBJECT (demux, "EOS; "
1767           "new estimated offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
1768           GST_TIME_ARGS (time), newpos);
1769       startpos = newpos;
1770       continue;
1771     } else if (ret != GST_FLOW_OK) {
1772       goto exit;
1773     } else {
1774       break;
1775     }
1776   }
1777
1778   /* then start scanning and parsing for cluster time,
1779    * re-estimate if overshoot, otherwise next cluster and so on */
1780   demux->common.offset = newpos;
1781   demux->cluster_time = cluster_time = GST_CLOCK_TIME_NONE;
1782   while (1) {
1783     guint64 cluster_size = 0;
1784
1785     /* peek and parse some elements */
1786     ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
1787         GST_ELEMENT_CAST (demux), &id, &length, &needed);
1788     if (ret != GST_FLOW_OK)
1789       goto error;
1790     GST_LOG_OBJECT (demux, "Offset %" G_GUINT64_FORMAT ", Element id 0x%x, "
1791         "size %" G_GUINT64_FORMAT ", needed %d", demux->common.offset, id,
1792         length, needed);
1793     ret = gst_matroska_demux_parse_id (demux, id, length, needed);
1794     if (ret != GST_FLOW_OK)
1795       goto error;
1796
1797     if (id == GST_MATROSKA_ID_CLUSTER) {
1798       cluster_time = GST_CLOCK_TIME_NONE;
1799       if (length == G_MAXUINT64)
1800         cluster_size = 0;
1801       else
1802         cluster_size = length + needed;
1803     }
1804     if (demux->cluster_time != GST_CLOCK_TIME_NONE &&
1805         cluster_time == GST_CLOCK_TIME_NONE) {
1806       cluster_time = demux->cluster_time * demux->common.time_scale;
1807       cluster_offset = demux->cluster_offset;
1808       GST_DEBUG_OBJECT (demux, "found cluster at offset %" G_GINT64_FORMAT
1809           " with time %" GST_TIME_FORMAT, cluster_offset,
1810           GST_TIME_ARGS (cluster_time));
1811       if (cluster_time > time) {
1812         GST_DEBUG_OBJECT (demux, "overshot target");
1813         /* cluster overshoots */
1814         if (cluster_offset == demux->first_cluster_offset) {
1815           /* but no prev one */
1816           GST_DEBUG_OBJECT (demux, "but using first cluster anyway");
1817           prev_cluster_time = cluster_time;
1818           prev_cluster_offset = cluster_offset;
1819           break;
1820         }
1821         if (prev_cluster_time != GST_CLOCK_TIME_NONE) {
1822           /* prev cluster did not overshoot, so prev cluster is target */
1823           break;
1824         } else {
1825           /* re-estimate using this new position info */
1826           opos = cluster_offset;
1827           otime = cluster_time;
1828           goto retry;
1829         }
1830       } else {
1831         /* cluster undershoots, goto next one */
1832         prev_cluster_time = cluster_time;
1833         prev_cluster_offset = cluster_offset;
1834         /* skip cluster if length is defined,
1835          * otherwise will be skippingly parsed into */
1836         if (cluster_size) {
1837           GST_DEBUG_OBJECT (demux, "skipping to next cluster");
1838           demux->common.offset = cluster_offset + cluster_size;
1839           demux->cluster_time = GST_CLOCK_TIME_NONE;
1840         } else {
1841           GST_DEBUG_OBJECT (demux, "parsing/skipping cluster elements");
1842         }
1843       }
1844     }
1845     continue;
1846
1847   error:
1848     if (ret == GST_FLOW_EOS) {
1849       if (prev_cluster_time != GST_CLOCK_TIME_NONE)
1850         break;
1851     }
1852     goto exit;
1853   }
1854
1855   entry = g_new0 (GstMatroskaIndex, 1);
1856   entry->time = prev_cluster_time;
1857   entry->pos = prev_cluster_offset - demux->common.ebml_segment_start;
1858   GST_DEBUG_OBJECT (demux, "simulated index entry; time %" GST_TIME_FORMAT
1859       ", pos %" G_GUINT64_FORMAT, GST_TIME_ARGS (entry->time), entry->pos);
1860
1861 exit:
1862
1863   /* restore some state */
1864   demux->cluster_offset = current_cluster_offset;
1865   demux->cluster_time = current_cluster_time;
1866   demux->common.offset = current_offset;
1867   demux->common.state = current_state;
1868
1869   return entry;
1870 }
1871
1872 static gboolean
1873 gst_matroska_demux_handle_seek_event (GstMatroskaDemux * demux,
1874     GstPad * pad, GstEvent * event)
1875 {
1876   GstMatroskaIndex *entry = NULL;
1877   GstMatroskaIndex scan_entry;
1878   GstSeekFlags flags;
1879   GstSeekType cur_type, stop_type;
1880   GstFormat format;
1881   gboolean flush, keyunit, before, after, snap_next;
1882   gdouble rate;
1883   gint64 cur, stop;
1884   GstMatroskaTrackContext *track = NULL;
1885   GstSegment seeksegment = { 0, };
1886   gboolean update = TRUE;
1887   gboolean pad_locked = FALSE;
1888   guint32 seqnum;
1889   GstSearchMode snap_dir;
1890
1891   g_return_val_if_fail (event != NULL, FALSE);
1892
1893   if (pad)
1894     track = gst_pad_get_element_private (pad);
1895
1896   GST_DEBUG_OBJECT (demux, "Have seek %" GST_PTR_FORMAT, event);
1897
1898   gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
1899       &stop_type, &stop);
1900   seqnum = gst_event_get_seqnum (event);
1901
1902   /* we can only seek on time */
1903   if (format != GST_FORMAT_TIME) {
1904     GST_DEBUG_OBJECT (demux, "Can only seek on TIME");
1905     return FALSE;
1906   }
1907
1908   /* copy segment, we need this because we still need the old
1909    * segment when we close the current segment. */
1910   memcpy (&seeksegment, &demux->common.segment, sizeof (GstSegment));
1911
1912   /* pull mode without index means that the actual duration is not known,
1913    * we might be playing a file that's still being recorded
1914    * so, invalidate our current duration, which is only a moving target,
1915    * and should not be used to clamp anything */
1916   if (!demux->streaming && !demux->common.index && demux->invalid_duration) {
1917     seeksegment.duration = GST_CLOCK_TIME_NONE;
1918   }
1919
1920   GST_DEBUG_OBJECT (demux, "configuring seek");
1921   /* Subtract stream_start_time so we always seek on a segment
1922    * in stream time */
1923   if (GST_CLOCK_TIME_IS_VALID (demux->stream_start_time)) {
1924     seeksegment.start -= demux->stream_start_time;
1925     seeksegment.position -= demux->stream_start_time;
1926     if (GST_CLOCK_TIME_IS_VALID (seeksegment.stop))
1927       seeksegment.stop -= demux->stream_start_time;
1928     else
1929       seeksegment.stop = seeksegment.duration;
1930   }
1931
1932   gst_segment_do_seek (&seeksegment, rate, format, flags,
1933       cur_type, cur, stop_type, stop, &update);
1934
1935   /* Restore the clip timestamp offset */
1936   if (GST_CLOCK_TIME_IS_VALID (demux->stream_start_time)) {
1937     seeksegment.position += demux->stream_start_time;
1938     seeksegment.start += demux->stream_start_time;
1939     if (!GST_CLOCK_TIME_IS_VALID (seeksegment.stop))
1940       seeksegment.stop = seeksegment.duration;
1941     if (GST_CLOCK_TIME_IS_VALID (seeksegment.stop))
1942       seeksegment.stop += demux->stream_start_time;
1943   }
1944
1945   /* restore segment duration (if any effect),
1946    * would be determined again when parsing, but anyway ... */
1947   seeksegment.duration = demux->common.segment.duration;
1948
1949   flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
1950   keyunit = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
1951   after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
1952   before = ! !(flags & GST_SEEK_FLAG_SNAP_BEFORE);
1953
1954   /* always do full update if flushing,
1955    * otherwise problems might arise downstream with missing keyframes etc */
1956   update = update || flush;
1957
1958   GST_DEBUG_OBJECT (demux, "New segment %" GST_SEGMENT_FORMAT, &seeksegment);
1959
1960   /* check sanity before we start flushing and all that */
1961   snap_next = after && !before;
1962   if (seeksegment.rate < 0)
1963     snap_dir = snap_next ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER;
1964   else
1965     snap_dir = snap_next ? GST_SEARCH_MODE_AFTER : GST_SEARCH_MODE_BEFORE;
1966
1967   GST_OBJECT_LOCK (demux);
1968   track = gst_matroska_read_common_get_seek_track (&demux->common, track);
1969   if ((entry = gst_matroska_read_common_do_index_seek (&demux->common, track,
1970               seeksegment.position, &demux->seek_index, &demux->seek_entry,
1971               snap_dir)) == NULL) {
1972     /* pull mode without index can scan later on */
1973     if (demux->streaming) {
1974       GST_DEBUG_OBJECT (demux, "No matching seek entry in index");
1975       GST_OBJECT_UNLOCK (demux);
1976       return FALSE;
1977     } else if (rate < 0.0) {
1978       /* FIXME: We should build an index during playback or when scanning
1979        * that can be used here. The reverse playback code requires seek_index
1980        * and seek_entry to be set!
1981        */
1982       GST_DEBUG_OBJECT (demux,
1983           "No matching seek entry in index, needed for reverse playback");
1984       GST_OBJECT_UNLOCK (demux);
1985       return FALSE;
1986     }
1987   }
1988   GST_DEBUG_OBJECT (demux, "Seek position looks sane");
1989   GST_OBJECT_UNLOCK (demux);
1990
1991   if (!update) {
1992     /* only have to update some segment,
1993      * but also still have to honour flush and so on */
1994     GST_DEBUG_OBJECT (demux, "... no update");
1995     /* bad goto, bad ... */
1996     goto next;
1997   }
1998
1999   if (demux->streaming)
2000     goto finish;
2001
2002 next:
2003   if (flush) {
2004     GstEvent *flush_event = gst_event_new_flush_start ();
2005     gst_event_set_seqnum (flush_event, seqnum);
2006     GST_DEBUG_OBJECT (demux, "Starting flush");
2007     gst_pad_push_event (demux->common.sinkpad, gst_event_ref (flush_event));
2008     gst_matroska_demux_send_event (demux, flush_event);
2009   } else {
2010     GST_DEBUG_OBJECT (demux, "Non-flushing seek, pausing task");
2011     gst_pad_pause_task (demux->common.sinkpad);
2012   }
2013   /* ouch */
2014   if (!update) {
2015     GST_PAD_STREAM_LOCK (demux->common.sinkpad);
2016     pad_locked = TRUE;
2017     goto exit;
2018   }
2019
2020   /* now grab the stream lock so that streaming cannot continue, for
2021    * non flushing seeks when the element is in PAUSED this could block
2022    * forever. */
2023   GST_DEBUG_OBJECT (demux, "Waiting for streaming to stop");
2024   GST_PAD_STREAM_LOCK (demux->common.sinkpad);
2025   pad_locked = TRUE;
2026
2027   /* pull mode without index can do some scanning */
2028   if (!demux->streaming && !entry) {
2029     GstEvent *flush_event;
2030
2031     /* need to stop flushing upstream as we need it next */
2032     if (flush) {
2033       flush_event = gst_event_new_flush_stop (TRUE);
2034       gst_event_set_seqnum (flush_event, seqnum);
2035       gst_pad_push_event (demux->common.sinkpad, flush_event);
2036     }
2037     entry = gst_matroska_demux_search_pos (demux, seeksegment.position);
2038     /* keep local copy */
2039     if (entry) {
2040       scan_entry = *entry;
2041       g_free (entry);
2042       entry = &scan_entry;
2043     } else {
2044       GST_DEBUG_OBJECT (demux, "Scan failed to find matching position");
2045       if (flush) {
2046         flush_event = gst_event_new_flush_stop (TRUE);
2047         gst_event_set_seqnum (flush_event, seqnum);
2048         gst_matroska_demux_send_event (demux, flush_event);
2049       }
2050       goto seek_error;
2051     }
2052   }
2053
2054 finish:
2055   if (keyunit) {
2056     GST_DEBUG_OBJECT (demux, "seek to key unit, adjusting segment start from %"
2057         GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
2058         GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (entry->time));
2059     seeksegment.start = MAX (entry->time, demux->stream_start_time);
2060     seeksegment.position = seeksegment.start;
2061     seeksegment.time = seeksegment.start - demux->stream_start_time;
2062   }
2063
2064   if (demux->streaming) {
2065     GST_OBJECT_LOCK (demux);
2066     /* track real position we should start at */
2067     GST_DEBUG_OBJECT (demux, "storing segment start");
2068     demux->requested_seek_time = seeksegment.position;
2069     demux->seek_offset = entry->pos + demux->common.ebml_segment_start;
2070     GST_OBJECT_UNLOCK (demux);
2071     /* need to seek to cluster start to pick up cluster time */
2072     /* upstream takes care of flushing and all that
2073      * ... and newsegment event handling takes care of the rest */
2074     return perform_seek_to_offset (demux, rate,
2075         entry->pos + demux->common.ebml_segment_start, seqnum);
2076   }
2077
2078 exit:
2079   if (flush) {
2080     GstEvent *flush_event = gst_event_new_flush_stop (TRUE);
2081     gst_event_set_seqnum (flush_event, seqnum);
2082     GST_DEBUG_OBJECT (demux, "Stopping flush");
2083     gst_pad_push_event (demux->common.sinkpad, gst_event_ref (flush_event));
2084     gst_matroska_demux_send_event (demux, flush_event);
2085   }
2086
2087   GST_OBJECT_LOCK (demux);
2088   /* now update the real segment info */
2089   GST_DEBUG_OBJECT (demux, "Committing new seek segment");
2090   memcpy (&demux->common.segment, &seeksegment, sizeof (GstSegment));
2091   GST_OBJECT_UNLOCK (demux);
2092
2093   /* update some (segment) state */
2094   if (!gst_matroska_demux_move_to_entry (demux, entry, TRUE, update))
2095     goto seek_error;
2096
2097   /* notify start of new segment */
2098   if (demux->common.segment.flags & GST_SEEK_FLAG_SEGMENT) {
2099     GstMessage *msg;
2100
2101     msg = gst_message_new_segment_start (GST_OBJECT (demux),
2102         GST_FORMAT_TIME, demux->common.segment.start);
2103     gst_message_set_seqnum (msg, seqnum);
2104     gst_element_post_message (GST_ELEMENT (demux), msg);
2105   }
2106
2107   GST_OBJECT_LOCK (demux);
2108   if (demux->new_segment)
2109     gst_event_unref (demux->new_segment);
2110
2111   /* On port from 0.10, discarded !update (for segment.update) here, FIXME? */
2112   demux->new_segment = gst_event_new_segment (&demux->common.segment);
2113   gst_event_set_seqnum (demux->new_segment, seqnum);
2114   if (demux->common.segment.rate < 0 && demux->common.segment.stop == -1)
2115     demux->to_time = demux->common.segment.position;
2116   else
2117     demux->to_time = GST_CLOCK_TIME_NONE;
2118   GST_OBJECT_UNLOCK (demux);
2119
2120   /* restart our task since it might have been stopped when we did the
2121    * flush. */
2122   gst_pad_start_task (demux->common.sinkpad,
2123       (GstTaskFunction) gst_matroska_demux_loop, demux->common.sinkpad, NULL);
2124
2125   /* streaming can continue now */
2126   if (pad_locked) {
2127     GST_PAD_STREAM_UNLOCK (demux->common.sinkpad);
2128   }
2129
2130   return TRUE;
2131
2132 seek_error:
2133   {
2134     if (pad_locked) {
2135       GST_PAD_STREAM_UNLOCK (demux->common.sinkpad);
2136     }
2137     GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL), ("Got a seek error"));
2138     return FALSE;
2139   }
2140 }
2141
2142 /*
2143  * Handle whether we can perform the seek event or if we have to let the chain
2144  * function handle seeks to build the seek indexes first.
2145  */
2146 static gboolean
2147 gst_matroska_demux_handle_seek_push (GstMatroskaDemux * demux, GstPad * pad,
2148     GstEvent * event)
2149 {
2150   GstSeekFlags flags;
2151   GstSeekType cur_type, stop_type;
2152   GstFormat format;
2153   gdouble rate;
2154   gint64 cur, stop;
2155
2156   gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
2157       &stop_type, &stop);
2158
2159   /* sanity checks */
2160
2161   /* we can only seek on time */
2162   if (format != GST_FORMAT_TIME) {
2163     GST_DEBUG_OBJECT (demux, "Can only seek on TIME");
2164     return FALSE;
2165   }
2166
2167   if (stop_type != GST_SEEK_TYPE_NONE && stop != GST_CLOCK_TIME_NONE) {
2168     GST_DEBUG_OBJECT (demux, "Seek end-time not supported in streaming mode");
2169     return FALSE;
2170   }
2171
2172   if (!(flags & GST_SEEK_FLAG_FLUSH)) {
2173     GST_DEBUG_OBJECT (demux,
2174         "Non-flushing seek not supported in streaming mode");
2175     return FALSE;
2176   }
2177
2178   if (flags & GST_SEEK_FLAG_SEGMENT) {
2179     GST_DEBUG_OBJECT (demux, "Segment seek not supported in streaming mode");
2180     return FALSE;
2181   }
2182
2183   /* check for having parsed index already */
2184   if (!demux->common.index_parsed) {
2185     gboolean building_index;
2186     guint64 offset = 0;
2187
2188     if (!demux->index_offset) {
2189       GST_DEBUG_OBJECT (demux, "no index (location); no seek in push mode");
2190       return FALSE;
2191     }
2192
2193     GST_OBJECT_LOCK (demux);
2194     /* handle the seek event in the chain function */
2195     demux->common.state = GST_MATROSKA_READ_STATE_SEEK;
2196     /* no more seek can be issued until state reset to _DATA */
2197
2198     /* copy the event */
2199     if (demux->seek_event)
2200       gst_event_unref (demux->seek_event);
2201     demux->seek_event = gst_event_ref (event);
2202
2203     /* set the building_index flag so that only one thread can setup the
2204      * structures for index seeking. */
2205     building_index = demux->building_index;
2206     if (!building_index) {
2207       demux->building_index = TRUE;
2208       offset = demux->index_offset;
2209     }
2210     GST_OBJECT_UNLOCK (demux);
2211
2212     if (!building_index) {
2213       /* seek to the first subindex or legacy index */
2214       GST_INFO_OBJECT (demux, "Seeking to Cues at %" G_GUINT64_FORMAT, offset);
2215       return perform_seek_to_offset (demux, rate, offset,
2216           gst_event_get_seqnum (event));
2217     }
2218
2219     /* well, we are handling it already */
2220     return TRUE;
2221   }
2222
2223   /* delegate to tweaked regular seek */
2224   return gst_matroska_demux_handle_seek_event (demux, pad, event);
2225 }
2226
2227 static gboolean
2228 gst_matroska_demux_handle_src_event (GstPad * pad, GstObject * parent,
2229     GstEvent * event)
2230 {
2231   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
2232   gboolean res = TRUE;
2233
2234   switch (GST_EVENT_TYPE (event)) {
2235     case GST_EVENT_SEEK:
2236       /* no seeking until we are (safely) ready */
2237       if (demux->common.state != GST_MATROSKA_READ_STATE_DATA) {
2238         GST_DEBUG_OBJECT (demux, "not ready for seeking yet");
2239         return FALSE;
2240       }
2241       if (!demux->streaming)
2242         res = gst_matroska_demux_handle_seek_event (demux, pad, event);
2243       else
2244         res = gst_matroska_demux_handle_seek_push (demux, pad, event);
2245       gst_event_unref (event);
2246       break;
2247
2248     case GST_EVENT_QOS:
2249     {
2250       GstMatroskaTrackContext *context = gst_pad_get_element_private (pad);
2251       if (context->type == GST_MATROSKA_TRACK_TYPE_VIDEO) {
2252         GstMatroskaTrackVideoContext *videocontext =
2253             (GstMatroskaTrackVideoContext *) context;
2254         gdouble proportion;
2255         GstClockTimeDiff diff;
2256         GstClockTime timestamp;
2257
2258         gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
2259
2260         GST_OBJECT_LOCK (demux);
2261         videocontext->earliest_time = timestamp + diff;
2262         GST_OBJECT_UNLOCK (demux);
2263       }
2264       res = TRUE;
2265       gst_event_unref (event);
2266       break;
2267     }
2268
2269     case GST_EVENT_TOC_SELECT:
2270     {
2271       char *uid = NULL;
2272       GstTocEntry *entry = NULL;
2273       GstEvent *seek_event;
2274       gint64 start_pos;
2275
2276       if (!demux->common.toc) {
2277         GST_DEBUG_OBJECT (demux, "no TOC to select");
2278         return FALSE;
2279       } else {
2280         gst_event_parse_toc_select (event, &uid);
2281         if (uid != NULL) {
2282           GST_OBJECT_LOCK (demux);
2283           entry = gst_toc_find_entry (demux->common.toc, uid);
2284           if (entry == NULL) {
2285             GST_OBJECT_UNLOCK (demux);
2286             GST_WARNING_OBJECT (demux, "no TOC entry with given UID: %s", uid);
2287             res = FALSE;
2288           } else {
2289             gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2290             GST_OBJECT_UNLOCK (demux);
2291             seek_event = gst_event_new_seek (1.0,
2292                 GST_FORMAT_TIME,
2293                 GST_SEEK_FLAG_FLUSH,
2294                 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2295             res = gst_matroska_demux_handle_seek_event (demux, pad, seek_event);
2296             gst_event_unref (seek_event);
2297           }
2298           g_free (uid);
2299         } else {
2300           GST_WARNING_OBJECT (demux, "received empty TOC select event");
2301           res = FALSE;
2302         }
2303       }
2304       gst_event_unref (event);
2305       break;
2306     }
2307
2308       /* events we don't need to handle */
2309     case GST_EVENT_NAVIGATION:
2310       gst_event_unref (event);
2311       res = FALSE;
2312       break;
2313
2314     case GST_EVENT_LATENCY:
2315     default:
2316       res = gst_pad_push_event (demux->common.sinkpad, event);
2317       break;
2318   }
2319
2320   return res;
2321 }
2322
2323 static GstFlowReturn
2324 gst_matroska_demux_seek_to_previous_keyframe (GstMatroskaDemux * demux)
2325 {
2326   GstFlowReturn ret = GST_FLOW_EOS;
2327   gboolean done = TRUE;
2328   gint i;
2329
2330   g_return_val_if_fail (demux->seek_index, GST_FLOW_EOS);
2331   g_return_val_if_fail (demux->seek_entry < demux->seek_index->len,
2332       GST_FLOW_EOS);
2333
2334   GST_DEBUG_OBJECT (demux, "locating previous keyframe");
2335
2336   if (!demux->seek_entry) {
2337     GST_DEBUG_OBJECT (demux, "no earlier index entry");
2338     goto exit;
2339   }
2340
2341   for (i = 0; i < demux->common.src->len; i++) {
2342     GstMatroskaTrackContext *stream = g_ptr_array_index (demux->common.src, i);
2343
2344     GST_DEBUG_OBJECT (demux, "segment start %" GST_TIME_FORMAT
2345         ", stream %d at %" GST_TIME_FORMAT,
2346         GST_TIME_ARGS (demux->common.segment.start), stream->index,
2347         GST_TIME_ARGS (stream->from_time));
2348     if (GST_CLOCK_TIME_IS_VALID (stream->from_time)) {
2349       if (stream->from_time > demux->common.segment.start) {
2350         GST_DEBUG_OBJECT (demux, "stream %d not finished yet", stream->index);
2351         done = FALSE;
2352       }
2353     } else {
2354       /* nothing pushed for this stream;
2355        * likely seek entry did not start at keyframe, so all was skipped.
2356        * So we need an earlier entry */
2357       done = FALSE;
2358     }
2359   }
2360
2361   if (!done) {
2362     GstMatroskaIndex *entry;
2363
2364     entry = &g_array_index (demux->seek_index, GstMatroskaIndex,
2365         --demux->seek_entry);
2366     if (!gst_matroska_demux_move_to_entry (demux, entry, FALSE, TRUE))
2367       goto exit;
2368
2369     ret = GST_FLOW_OK;
2370   }
2371
2372 exit:
2373   return ret;
2374 }
2375
2376 static GstFlowReturn
2377 gst_matroska_demux_parse_tracks (GstMatroskaDemux * demux, GstEbmlRead * ebml)
2378 {
2379   GstFlowReturn ret = GST_FLOW_OK;
2380   guint32 id;
2381
2382   DEBUG_ELEMENT_START (demux, ebml, "Tracks");
2383
2384   if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
2385     DEBUG_ELEMENT_STOP (demux, ebml, "Tracks", ret);
2386     return ret;
2387   }
2388
2389   while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
2390     if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
2391       break;
2392
2393     switch (id) {
2394         /* one track within the "all-tracks" header */
2395       case GST_MATROSKA_ID_TRACKENTRY:
2396         ret = gst_matroska_demux_add_stream (demux, ebml);
2397         break;
2398
2399       default:
2400         ret = gst_matroska_read_common_parse_skip (&demux->common, ebml,
2401             "Track", id);
2402         break;
2403     }
2404   }
2405   DEBUG_ELEMENT_STOP (demux, ebml, "Tracks", ret);
2406
2407   demux->tracks_parsed = TRUE;
2408
2409   return ret;
2410 }
2411
2412 /*
2413  * Read signed/unsigned "EBML" numbers.
2414  * Return: number of bytes processed.
2415  */
2416
2417 static gint
2418 gst_matroska_ebmlnum_uint (guint8 * data, guint size, guint64 * num)
2419 {
2420   gint len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
2421   guint64 total;
2422
2423   if (size <= 0) {
2424     return -1;
2425   }
2426
2427   total = data[0];
2428   while (read <= 8 && !(total & len_mask)) {
2429     read++;
2430     len_mask >>= 1;
2431   }
2432   if (read > 8)
2433     return -1;
2434
2435   if ((total &= (len_mask - 1)) == len_mask - 1)
2436     num_ffs++;
2437   if (size < read)
2438     return -1;
2439   while (n < read) {
2440     if (data[n] == 0xff)
2441       num_ffs++;
2442     total = (total << 8) | data[n];
2443     n++;
2444   }
2445
2446   if (read == num_ffs && total != 0)
2447     *num = G_MAXUINT64;
2448   else
2449     *num = total;
2450
2451   return read;
2452 }
2453
2454 static gint
2455 gst_matroska_ebmlnum_sint (guint8 * data, guint size, gint64 * num)
2456 {
2457   guint64 unum;
2458   gint res;
2459
2460   /* read as unsigned number first */
2461   if ((res = gst_matroska_ebmlnum_uint (data, size, &unum)) < 0)
2462     return -1;
2463
2464   /* make signed */
2465   if (unum == G_MAXUINT64)
2466     *num = G_MAXINT64;
2467   else
2468     *num = unum - ((1 << ((7 * res) - 1)) - 1);
2469
2470   return res;
2471 }
2472
2473 /*
2474  * Mostly used for subtitles. We add void filler data for each
2475  * lagging stream to make sure we don't deadlock.
2476  */
2477
2478 static void
2479 gst_matroska_demux_sync_streams (GstMatroskaDemux * demux)
2480 {
2481   gint stream_nr;
2482
2483   GST_OBJECT_LOCK (demux);
2484
2485   GST_LOG_OBJECT (demux, "Sync to %" GST_TIME_FORMAT,
2486       GST_TIME_ARGS (demux->common.segment.position));
2487
2488   g_assert (demux->common.num_streams == demux->common.src->len);
2489   for (stream_nr = 0; stream_nr < demux->common.src->len; stream_nr++) {
2490     GstMatroskaTrackContext *context;
2491
2492     context = g_ptr_array_index (demux->common.src, stream_nr);
2493
2494     GST_LOG_OBJECT (demux,
2495         "Checking for resync on stream %d (%" GST_TIME_FORMAT ")", stream_nr,
2496         GST_TIME_ARGS (context->pos));
2497
2498     if (G_LIKELY (context->type != GST_MATROSKA_TRACK_TYPE_SUBTITLE)) {
2499       GST_LOG_OBJECT (demux, "Skipping sync on non-subtitle stream");
2500       continue;
2501     }
2502
2503     /* does it lag? 0.5 seconds is a random threshold...
2504      * lag need only be considered if we have advanced into requested segment */
2505     if (GST_CLOCK_TIME_IS_VALID (context->pos) &&
2506         GST_CLOCK_TIME_IS_VALID (demux->common.segment.position) &&
2507         demux->common.segment.position > demux->common.segment.start &&
2508         context->pos + (GST_SECOND / 2) < demux->common.segment.position) {
2509
2510       GstEvent *event;
2511       guint64 start = context->pos;
2512       guint64 stop = demux->common.segment.position - (GST_SECOND / 2);
2513
2514       GST_DEBUG_OBJECT (demux,
2515           "Synchronizing stream %d with other by advancing time from %"
2516           GST_TIME_FORMAT " to %" GST_TIME_FORMAT, stream_nr,
2517           GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
2518
2519       context->pos = stop;
2520
2521       event = gst_event_new_gap (start, stop - start);
2522       GST_OBJECT_UNLOCK (demux);
2523       gst_pad_push_event (context->pad, event);
2524       GST_OBJECT_LOCK (demux);
2525     }
2526   }
2527
2528   GST_OBJECT_UNLOCK (demux);
2529 }
2530
2531 static GstFlowReturn
2532 gst_matroska_demux_push_stream_headers (GstMatroskaDemux * demux,
2533     GstMatroskaTrackContext * stream)
2534 {
2535   GstFlowReturn ret = GST_FLOW_OK;
2536   gint i, num;
2537
2538   num = gst_buffer_list_length (stream->stream_headers);
2539   for (i = 0; i < num; ++i) {
2540     GstBuffer *buf;
2541
2542     buf = gst_buffer_list_get (stream->stream_headers, i);
2543     buf = gst_buffer_copy (buf);
2544
2545     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
2546
2547     if (stream->set_discont) {
2548       GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2549       stream->set_discont = FALSE;
2550     } else {
2551       GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
2552     }
2553
2554     /* push out all headers in one go and use last flow return */
2555     ret = gst_pad_push (stream->pad, buf);
2556   }
2557
2558   /* don't need these any  longer */
2559   gst_buffer_list_unref (stream->stream_headers);
2560   stream->stream_headers = NULL;
2561
2562   /* combine flows */
2563   ret = gst_flow_combiner_update_flow (demux->flowcombiner, ret);
2564
2565   return ret;
2566 }
2567
2568 static void
2569 gst_matroska_demux_push_dvd_clut_change_event (GstMatroskaDemux * demux,
2570     GstMatroskaTrackContext * stream)
2571 {
2572   gchar *buf, *start;
2573
2574   g_assert (!strcmp (stream->codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_VOBSUB));
2575
2576   if (!stream->codec_priv)
2577     return;
2578
2579   /* ideally, VobSub private data should be parsed and stored more convenient
2580    * elsewhere, but for now, only interested in a small part */
2581
2582   /* make sure we have terminating 0 */
2583   buf = g_strndup (stream->codec_priv, stream->codec_priv_size);
2584
2585   /* just locate and parse palette part */
2586   start = strstr (buf, "palette:");
2587   if (start) {
2588     gint i;
2589     guint32 clut[16];
2590     guint32 col;
2591     guint8 r, g, b, y, u, v;
2592
2593     start += 8;
2594     while (g_ascii_isspace (*start))
2595       start++;
2596     for (i = 0; i < 16; i++) {
2597       if (sscanf (start, "%06x", &col) != 1)
2598         break;
2599       start += 6;
2600       while ((*start == ',') || g_ascii_isspace (*start))
2601         start++;
2602       /* sigh, need to convert this from vobsub pseudo-RGB to YUV */
2603       r = (col >> 16) & 0xff;
2604       g = (col >> 8) & 0xff;
2605       b = col & 0xff;
2606       y = CLAMP ((0.1494 * r + 0.6061 * g + 0.2445 * b) * 219 / 255 + 16, 0,
2607           255);
2608       u = CLAMP (0.6066 * r - 0.4322 * g - 0.1744 * b + 128, 0, 255);
2609       v = CLAMP (-0.08435 * r - 0.3422 * g + 0.4266 * b + 128, 0, 255);
2610       clut[i] = (y << 16) | (u << 8) | v;
2611     }
2612
2613     /* got them all without problems; build and send event */
2614     if (i == 16) {
2615       GstStructure *s;
2616
2617       s = gst_structure_new ("application/x-gst-dvd", "event", G_TYPE_STRING,
2618           "dvd-spu-clut-change", "clut00", G_TYPE_INT, clut[0], "clut01",
2619           G_TYPE_INT, clut[1], "clut02", G_TYPE_INT, clut[2], "clut03",
2620           G_TYPE_INT, clut[3], "clut04", G_TYPE_INT, clut[4], "clut05",
2621           G_TYPE_INT, clut[5], "clut06", G_TYPE_INT, clut[6], "clut07",
2622           G_TYPE_INT, clut[7], "clut08", G_TYPE_INT, clut[8], "clut09",
2623           G_TYPE_INT, clut[9], "clut10", G_TYPE_INT, clut[10], "clut11",
2624           G_TYPE_INT, clut[11], "clut12", G_TYPE_INT, clut[12], "clut13",
2625           G_TYPE_INT, clut[13], "clut14", G_TYPE_INT, clut[14], "clut15",
2626           G_TYPE_INT, clut[15], NULL);
2627
2628       gst_pad_push_event (stream->pad,
2629           gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s));
2630     }
2631   }
2632   g_free (buf);
2633 }
2634
2635 static void
2636 gst_matroska_demux_push_codec_data_all (GstMatroskaDemux * demux)
2637 {
2638   gint stream_nr;
2639
2640   GST_OBJECT_LOCK (demux);
2641
2642   g_assert (demux->common.num_streams == demux->common.src->len);
2643   for (stream_nr = 0; stream_nr < demux->common.src->len; stream_nr++) {
2644     GstMatroskaTrackContext *stream;
2645
2646     stream = g_ptr_array_index (demux->common.src, stream_nr);
2647
2648     if (stream->send_stream_headers) {
2649       if (stream->stream_headers != NULL) {
2650         gst_matroska_demux_push_stream_headers (demux, stream);
2651       } else {
2652         /* FIXME: perhaps we can just disable and skip this stream then */
2653         GST_ELEMENT_ERROR (demux, STREAM, DECODE, (NULL),
2654             ("Failed to extract stream headers from codec private data"));
2655       }
2656       stream->send_stream_headers = FALSE;
2657     }
2658
2659     if (stream->send_dvd_event) {
2660       gst_matroska_demux_push_dvd_clut_change_event (demux, stream);
2661       /* FIXME: should we send this event again after (flushing) seek ? */
2662       stream->send_dvd_event = FALSE;
2663     }
2664   }
2665
2666   GST_OBJECT_UNLOCK (demux);
2667 }
2668
2669 static GstFlowReturn
2670 gst_matroska_demux_add_mpeg_seq_header (GstElement * element,
2671     GstMatroskaTrackContext * stream, GstBuffer ** buf)
2672 {
2673   guint8 *seq_header;
2674   guint seq_header_len;
2675   guint32 header, tmp;
2676
2677   if (stream->codec_state) {
2678     seq_header = stream->codec_state;
2679     seq_header_len = stream->codec_state_size;
2680   } else if (stream->codec_priv) {
2681     seq_header = stream->codec_priv;
2682     seq_header_len = stream->codec_priv_size;
2683   } else {
2684     return GST_FLOW_OK;
2685   }
2686
2687   /* Sequence header only needed for keyframes */
2688   if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_DELTA_UNIT))
2689     return GST_FLOW_OK;
2690
2691   if (gst_buffer_get_size (*buf) < 4)
2692     return GST_FLOW_OK;
2693
2694   gst_buffer_extract (*buf, 0, &tmp, sizeof (guint32));
2695   header = GUINT32_FROM_BE (tmp);
2696
2697   /* Sequence start code, if not found prepend */
2698   if (header != 0x000001b3) {
2699     GstBuffer *newbuf;
2700
2701     GST_DEBUG_OBJECT (element, "Prepending MPEG sequence header");
2702
2703     newbuf = gst_buffer_new_wrapped (g_memdup (seq_header, seq_header_len),
2704         seq_header_len);
2705
2706     gst_buffer_copy_into (newbuf, *buf, GST_BUFFER_COPY_TIMESTAMPS |
2707         GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_MEMORY, 0,
2708         gst_buffer_get_size (*buf));
2709
2710     gst_buffer_unref (*buf);
2711     *buf = newbuf;
2712   }
2713
2714   return GST_FLOW_OK;
2715 }
2716
2717 static GstFlowReturn
2718 gst_matroska_demux_add_wvpk_header (GstElement * element,
2719     GstMatroskaTrackContext * stream, GstBuffer ** buf)
2720 {
2721   GstMatroskaTrackAudioContext *audiocontext =
2722       (GstMatroskaTrackAudioContext *) stream;
2723   GstBuffer *newbuf = NULL;
2724   GstMapInfo map, outmap;
2725   guint8 *buf_data, *data;
2726   Wavpack4Header wvh;
2727
2728   wvh.ck_id[0] = 'w';
2729   wvh.ck_id[1] = 'v';
2730   wvh.ck_id[2] = 'p';
2731   wvh.ck_id[3] = 'k';
2732
2733   wvh.version = GST_READ_UINT16_LE (stream->codec_priv);
2734   wvh.track_no = 0;
2735   wvh.index_no = 0;
2736   wvh.total_samples = -1;
2737   wvh.block_index = audiocontext->wvpk_block_index;
2738
2739   if (audiocontext->channels <= 2) {
2740     guint32 block_samples, tmp;
2741     gsize size = gst_buffer_get_size (*buf);
2742
2743     gst_buffer_extract (*buf, 0, &tmp, sizeof (guint32));
2744     block_samples = GUINT32_FROM_LE (tmp);
2745     /* we need to reconstruct the header of the wavpack block */
2746
2747     /* -20 because ck_size is the size of the wavpack block -8
2748      * and lace_size is the size of the wavpack block + 12
2749      * (the three guint32 of the header that already are in the buffer) */
2750     wvh.ck_size = size + sizeof (Wavpack4Header) - 20;
2751
2752     /* block_samples, flags and crc are already in the buffer */
2753     newbuf = gst_buffer_new_allocate (NULL, sizeof (Wavpack4Header) - 12, NULL);
2754
2755     gst_buffer_map (newbuf, &outmap, GST_MAP_WRITE);
2756     data = outmap.data;
2757     data[0] = 'w';
2758     data[1] = 'v';
2759     data[2] = 'p';
2760     data[3] = 'k';
2761     GST_WRITE_UINT32_LE (data + 4, wvh.ck_size);
2762     GST_WRITE_UINT16_LE (data + 8, wvh.version);
2763     GST_WRITE_UINT8 (data + 10, wvh.track_no);
2764     GST_WRITE_UINT8 (data + 11, wvh.index_no);
2765     GST_WRITE_UINT32_LE (data + 12, wvh.total_samples);
2766     GST_WRITE_UINT32_LE (data + 16, wvh.block_index);
2767
2768     /* Append data from buf: */
2769     gst_buffer_copy_into (newbuf, *buf, GST_BUFFER_COPY_TIMESTAMPS |
2770         GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_MEMORY, 0, size);
2771
2772     gst_buffer_unref (*buf);
2773     *buf = newbuf;
2774     audiocontext->wvpk_block_index += block_samples;
2775   } else {
2776     guint8 *outdata = NULL;
2777     guint outpos = 0;
2778     gsize buf_size, size, out_size = 0;
2779     guint32 block_samples, flags, crc, blocksize;
2780
2781     gst_buffer_map (*buf, &map, GST_MAP_READ);
2782     buf_data = map.data;
2783     buf_size = map.size;
2784
2785     if (buf_size < 4) {
2786       GST_ERROR_OBJECT (element, "Too small wavpack buffer");
2787       gst_buffer_unmap (*buf, &map);
2788       return GST_FLOW_ERROR;
2789     }
2790
2791     data = buf_data;
2792     size = buf_size;
2793
2794     block_samples = GST_READ_UINT32_LE (data);
2795     data += 4;
2796     size -= 4;
2797
2798     while (size > 12) {
2799       flags = GST_READ_UINT32_LE (data);
2800       data += 4;
2801       size -= 4;
2802       crc = GST_READ_UINT32_LE (data);
2803       data += 4;
2804       size -= 4;
2805       blocksize = GST_READ_UINT32_LE (data);
2806       data += 4;
2807       size -= 4;
2808
2809       if (blocksize == 0 || size < blocksize)
2810         break;
2811
2812       g_assert ((newbuf == NULL) == (outdata == NULL));
2813
2814       if (newbuf == NULL) {
2815         out_size = sizeof (Wavpack4Header) + blocksize;
2816         newbuf = gst_buffer_new_allocate (NULL, out_size, NULL);
2817
2818         gst_buffer_copy_into (newbuf, *buf,
2819             GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS, 0, -1);
2820
2821         outpos = 0;
2822         gst_buffer_map (newbuf, &outmap, GST_MAP_WRITE);
2823         outdata = outmap.data;
2824       } else {
2825         gst_buffer_unmap (newbuf, &outmap);
2826         out_size += sizeof (Wavpack4Header) + blocksize;
2827         gst_buffer_set_size (newbuf, out_size);
2828         gst_buffer_map (newbuf, &outmap, GST_MAP_WRITE);
2829         outdata = outmap.data;
2830       }
2831
2832       outdata[outpos] = 'w';
2833       outdata[outpos + 1] = 'v';
2834       outdata[outpos + 2] = 'p';
2835       outdata[outpos + 3] = 'k';
2836       outpos += 4;
2837
2838       GST_WRITE_UINT32_LE (outdata + outpos,
2839           blocksize + sizeof (Wavpack4Header) - 8);
2840       GST_WRITE_UINT16_LE (outdata + outpos + 4, wvh.version);
2841       GST_WRITE_UINT8 (outdata + outpos + 6, wvh.track_no);
2842       GST_WRITE_UINT8 (outdata + outpos + 7, wvh.index_no);
2843       GST_WRITE_UINT32_LE (outdata + outpos + 8, wvh.total_samples);
2844       GST_WRITE_UINT32_LE (outdata + outpos + 12, wvh.block_index);
2845       GST_WRITE_UINT32_LE (outdata + outpos + 16, block_samples);
2846       GST_WRITE_UINT32_LE (outdata + outpos + 20, flags);
2847       GST_WRITE_UINT32_LE (outdata + outpos + 24, crc);
2848       outpos += 28;
2849
2850       memmove (outdata + outpos, data, blocksize);
2851       outpos += blocksize;
2852       data += blocksize;
2853       size -= blocksize;
2854     }
2855     gst_buffer_unmap (*buf, &map);
2856     gst_buffer_unref (*buf);
2857
2858     if (newbuf)
2859       gst_buffer_unmap (newbuf, &outmap);
2860
2861     *buf = newbuf;
2862     audiocontext->wvpk_block_index += block_samples;
2863   }
2864
2865   return GST_FLOW_OK;
2866 }
2867
2868 /* @text must be null-terminated */
2869 static gboolean
2870 gst_matroska_demux_subtitle_chunk_has_tag (GstElement * element,
2871     const gchar * text)
2872 {
2873   gchar *tag;
2874
2875   g_return_val_if_fail (text != NULL, FALSE);
2876
2877   /* yes, this might all lead to false positives ... */
2878   tag = (gchar *) text;
2879   while ((tag = strchr (tag, '<'))) {
2880     tag++;
2881     if (*tag != '\0' && *(tag + 1) == '>') {
2882       /* some common convenience ones */
2883       /* maybe any character will do here ? */
2884       switch (*tag) {
2885         case 'b':
2886         case 'i':
2887         case 'u':
2888         case 's':
2889           return TRUE;
2890         default:
2891           return FALSE;
2892       }
2893     }
2894   }
2895
2896   if (strstr (text, "<span"))
2897     return TRUE;
2898
2899   return FALSE;
2900 }
2901
2902 static GstFlowReturn
2903 gst_matroska_demux_check_subtitle_buffer (GstElement * element,
2904     GstMatroskaTrackContext * stream, GstBuffer ** buf)
2905 {
2906   GstMatroskaTrackSubtitleContext *sub_stream;
2907   const gchar *encoding;
2908   GError *err = NULL;
2909   GstBuffer *newbuf;
2910   gchar *utf8;
2911   GstMapInfo map;
2912   gboolean needs_unmap = TRUE;
2913
2914   sub_stream = (GstMatroskaTrackSubtitleContext *) stream;
2915
2916   if (!gst_buffer_get_size (*buf) || !gst_buffer_map (*buf, &map, GST_MAP_READ))
2917     return GST_FLOW_OK;
2918
2919   /* Need \0-terminator at the end */
2920   if (map.data[map.size - 1] != '\0') {
2921     newbuf = gst_buffer_new_and_alloc (map.size + 1);
2922
2923     /* Copy old buffer and add a 0 at the end */
2924     gst_buffer_fill (newbuf, 0, map.data, map.size);
2925     gst_buffer_memset (newbuf, map.size, 0, 1);
2926     gst_buffer_unmap (*buf, &map);
2927
2928     gst_buffer_copy_into (newbuf, *buf,
2929         GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS |
2930         GST_BUFFER_COPY_META, 0, -1);
2931     gst_buffer_unref (*buf);
2932     *buf = newbuf;
2933     gst_buffer_map (*buf, &map, GST_MAP_READ);
2934   }
2935
2936   if (!sub_stream->invalid_utf8) {
2937     if (g_utf8_validate ((gchar *) map.data, map.size - 1, NULL)) {
2938       goto next;
2939     }
2940     GST_WARNING_OBJECT (element, "subtitle stream %" G_GUINT64_FORMAT
2941         " is not valid UTF-8, this is broken according to the matroska"
2942         " specification", stream->num);
2943     sub_stream->invalid_utf8 = TRUE;
2944   }
2945
2946   /* file with broken non-UTF8 subtitle, do the best we can do to fix it */
2947   encoding = g_getenv ("GST_SUBTITLE_ENCODING");
2948   if (encoding == NULL || *encoding == '\0') {
2949     /* if local encoding is UTF-8 and no encoding specified
2950      * via the environment variable, assume ISO-8859-15 */
2951     if (g_get_charset (&encoding)) {
2952       encoding = "ISO-8859-15";
2953     }
2954   }
2955
2956   utf8 =
2957       g_convert_with_fallback ((gchar *) map.data, map.size, "UTF-8", encoding,
2958       (char *) "*", NULL, NULL, &err);
2959
2960   if (err) {
2961     GST_LOG_OBJECT (element, "could not convert string from '%s' to UTF-8: %s",
2962         encoding, err->message);
2963     g_error_free (err);
2964     g_free (utf8);
2965
2966     /* invalid input encoding, fall back to ISO-8859-15 (always succeeds) */
2967     encoding = "ISO-8859-15";
2968     utf8 =
2969         g_convert_with_fallback ((gchar *) map.data, map.size, "UTF-8",
2970         encoding, (char *) "*", NULL, NULL, NULL);
2971   }
2972
2973   GST_LOG_OBJECT (element, "converted subtitle text from %s to UTF-8 %s",
2974       encoding, (err) ? "(using ISO-8859-15 as fallback)" : "");
2975
2976   if (utf8 == NULL)
2977     utf8 = g_strdup ("invalid subtitle");
2978
2979   newbuf = gst_buffer_new_wrapped (utf8, strlen (utf8));
2980   gst_buffer_unmap (*buf, &map);
2981   gst_buffer_copy_into (newbuf, *buf,
2982       GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_META,
2983       0, -1);
2984   gst_buffer_unref (*buf);
2985
2986   *buf = newbuf;
2987   gst_buffer_map (*buf, &map, GST_MAP_READ);
2988
2989 next:
2990
2991   if (sub_stream->check_markup) {
2992     /* caps claim markup text, so we need to escape text,
2993      * except if text is already markup and then needs no further escaping */
2994     sub_stream->seen_markup_tag = sub_stream->seen_markup_tag ||
2995         gst_matroska_demux_subtitle_chunk_has_tag (element, (gchar *) map.data);
2996
2997     if (!sub_stream->seen_markup_tag) {
2998       utf8 = g_markup_escape_text ((gchar *) map.data, map.size);
2999
3000       newbuf = gst_buffer_new_wrapped (utf8, strlen (utf8));
3001       gst_buffer_unmap (*buf, &map);
3002       gst_buffer_copy_into (newbuf, *buf,
3003           GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS |
3004           GST_BUFFER_COPY_META, 0, -1);
3005       gst_buffer_unref (*buf);
3006
3007       *buf = newbuf;
3008       needs_unmap = FALSE;
3009     }
3010   }
3011
3012   if (needs_unmap)
3013     gst_buffer_unmap (*buf, &map);
3014
3015   return GST_FLOW_OK;
3016 }
3017
3018 static GstFlowReturn
3019 gst_matroska_demux_check_aac (GstElement * element,
3020     GstMatroskaTrackContext * stream, GstBuffer ** buf)
3021 {
3022   guint8 data[2];
3023   guint size;
3024
3025   gst_buffer_extract (*buf, 0, data, 2);
3026   size = gst_buffer_get_size (*buf);
3027
3028   if (size > 2 && data[0] == 0xff && (data[1] >> 4 == 0x0f)) {
3029     GstStructure *s;
3030
3031     /* tss, ADTS data, remove codec_data
3032      * still assume it is at least parsed */
3033     stream->caps = gst_caps_make_writable (stream->caps);
3034     s = gst_caps_get_structure (stream->caps, 0);
3035     g_assert (s);
3036     gst_structure_remove_field (s, "codec_data");
3037     gst_pad_set_caps (stream->pad, stream->caps);
3038     GST_DEBUG_OBJECT (element, "ADTS AAC audio data; removing codec-data, "
3039         "new caps: %" GST_PTR_FORMAT, stream->caps);
3040   }
3041
3042   /* disable subsequent checking */
3043   stream->postprocess_frame = NULL;
3044
3045   return GST_FLOW_OK;
3046 }
3047
3048 static GstBuffer *
3049 gst_matroska_demux_align_buffer (GstMatroskaDemux * demux,
3050     GstBuffer * buffer, gsize alignment)
3051 {
3052   GstMapInfo map;
3053
3054   gst_buffer_map (buffer, &map, GST_MAP_READ);
3055
3056   if (map.size < sizeof (guintptr)) {
3057     gst_buffer_unmap (buffer, &map);
3058     return buffer;
3059   }
3060
3061   if (((guintptr) map.data) & (alignment - 1)) {
3062     GstBuffer *new_buffer;
3063     GstAllocationParams params = { 0, alignment - 1, 0, 0, };
3064
3065     new_buffer = gst_buffer_new_allocate (NULL,
3066         gst_buffer_get_size (buffer), &params);
3067
3068     /* Copy data "by hand", so ensure alignment is kept: */
3069     gst_buffer_fill (new_buffer, 0, map.data, map.size);
3070
3071     gst_buffer_copy_into (new_buffer, buffer, GST_BUFFER_COPY_METADATA, 0, -1);
3072     GST_DEBUG_OBJECT (demux,
3073         "We want output aligned on %" G_GSIZE_FORMAT ", reallocated",
3074         alignment);
3075
3076     gst_buffer_unmap (buffer, &map);
3077     gst_buffer_unref (buffer);
3078
3079     return new_buffer;
3080   }
3081
3082   gst_buffer_unmap (buffer, &map);
3083   return buffer;
3084 }
3085
3086 static GstFlowReturn
3087 gst_matroska_demux_parse_blockgroup_or_simpleblock (GstMatroskaDemux * demux,
3088     GstEbmlRead * ebml, guint64 cluster_time, guint64 cluster_offset,
3089     gboolean is_simpleblock)
3090 {
3091   GstMatroskaTrackContext *stream = NULL;
3092   GstFlowReturn ret = GST_FLOW_OK;
3093   gboolean readblock = FALSE;
3094   guint32 id;
3095   guint64 block_duration = -1;
3096   GstBuffer *buf = NULL;
3097   GstMapInfo map;
3098   gint stream_num = -1, n, laces = 0;
3099   guint size = 0;
3100   gint *lace_size = NULL;
3101   gint64 time = 0;
3102   gint flags = 0;
3103   gint64 referenceblock = 0;
3104   gint64 offset;
3105   GstClockTime buffer_timestamp;
3106
3107   offset = gst_ebml_read_get_offset (ebml);
3108
3109   while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
3110     if (!is_simpleblock) {
3111       if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK) {
3112         goto data_error;
3113       }
3114     } else {
3115       id = GST_MATROSKA_ID_SIMPLEBLOCK;
3116     }
3117
3118     switch (id) {
3119         /* one block inside the group. Note, block parsing is one
3120          * of the harder things, so this code is a bit complicated.
3121          * See http://www.matroska.org/ for documentation. */
3122       case GST_MATROSKA_ID_SIMPLEBLOCK:
3123       case GST_MATROSKA_ID_BLOCK:
3124       {
3125         guint64 num;
3126         guint8 *data;
3127
3128         if (buf) {
3129           gst_buffer_unmap (buf, &map);
3130           gst_buffer_unref (buf);
3131           buf = NULL;
3132         }
3133         if ((ret = gst_ebml_read_buffer (ebml, &id, &buf)) != GST_FLOW_OK)
3134           break;
3135
3136         gst_buffer_map (buf, &map, GST_MAP_READ);
3137         data = map.data;
3138         size = map.size;
3139
3140         /* first byte(s): blocknum */
3141         if ((n = gst_matroska_ebmlnum_uint (data, size, &num)) < 0)
3142           goto data_error;
3143         data += n;
3144         size -= n;
3145
3146         /* fetch stream from num */
3147         stream_num = gst_matroska_read_common_stream_from_num (&demux->common,
3148             num);
3149         if (G_UNLIKELY (size < 3)) {
3150           GST_WARNING_OBJECT (demux, "Invalid size %u", size);
3151           /* non-fatal, try next block(group) */
3152           ret = GST_FLOW_OK;
3153           goto done;
3154         } else if (G_UNLIKELY (stream_num < 0 ||
3155                 stream_num >= demux->common.num_streams)) {
3156           /* let's not give up on a stray invalid track number */
3157           GST_WARNING_OBJECT (demux,
3158               "Invalid stream %d for track number %" G_GUINT64_FORMAT
3159               "; ignoring block", stream_num, num);
3160           goto done;
3161         }
3162
3163         stream = g_ptr_array_index (demux->common.src, stream_num);
3164
3165         /* time (relative to cluster time) */
3166         time = ((gint16) GST_READ_UINT16_BE (data));
3167         data += 2;
3168         size -= 2;
3169         flags = GST_READ_UINT8 (data);
3170         data += 1;
3171         size -= 1;
3172
3173         GST_LOG_OBJECT (demux, "time %" G_GUINT64_FORMAT ", flags %d", time,
3174             flags);
3175
3176         switch ((flags & 0x06) >> 1) {
3177           case 0x0:            /* no lacing */
3178             laces = 1;
3179             lace_size = g_new (gint, 1);
3180             lace_size[0] = size;
3181             break;
3182
3183           case 0x1:            /* xiph lacing */
3184           case 0x2:            /* fixed-size lacing */
3185           case 0x3:            /* EBML lacing */
3186             if (size == 0)
3187               goto invalid_lacing;
3188             laces = GST_READ_UINT8 (data) + 1;
3189             data += 1;
3190             size -= 1;
3191             lace_size = g_new0 (gint, laces);
3192
3193             switch ((flags & 0x06) >> 1) {
3194               case 0x1:        /* xiph lacing */  {
3195                 guint temp, total = 0;
3196
3197                 for (n = 0; ret == GST_FLOW_OK && n < laces - 1; n++) {
3198                   while (1) {
3199                     if (size == 0)
3200                       goto invalid_lacing;
3201                     temp = GST_READ_UINT8 (data);
3202                     lace_size[n] += temp;
3203                     data += 1;
3204                     size -= 1;
3205                     if (temp != 0xff)
3206                       break;
3207                   }
3208                   total += lace_size[n];
3209                 }
3210                 lace_size[n] = size - total;
3211                 break;
3212               }
3213
3214               case 0x2:        /* fixed-size lacing */
3215                 for (n = 0; n < laces; n++)
3216                   lace_size[n] = size / laces;
3217                 break;
3218
3219               case 0x3:        /* EBML lacing */  {
3220                 guint total;
3221
3222                 if ((n = gst_matroska_ebmlnum_uint (data, size, &num)) < 0)
3223                   goto data_error;
3224                 data += n;
3225                 size -= n;
3226                 total = lace_size[0] = num;
3227                 for (n = 1; ret == GST_FLOW_OK && n < laces - 1; n++) {
3228                   gint64 snum;
3229                   gint r;
3230
3231                   if ((r = gst_matroska_ebmlnum_sint (data, size, &snum)) < 0)
3232                     goto data_error;
3233                   data += r;
3234                   size -= r;
3235                   lace_size[n] = lace_size[n - 1] + snum;
3236                   total += lace_size[n];
3237                 }
3238                 if (n < laces)
3239                   lace_size[n] = size - total;
3240                 break;
3241               }
3242             }
3243             break;
3244         }
3245
3246         if (ret != GST_FLOW_OK)
3247           break;
3248
3249         readblock = TRUE;
3250         break;
3251       }
3252
3253       case GST_MATROSKA_ID_BLOCKDURATION:{
3254         ret = gst_ebml_read_uint (ebml, &id, &block_duration);
3255         GST_DEBUG_OBJECT (demux, "BlockDuration: %" G_GUINT64_FORMAT,
3256             block_duration);
3257         break;
3258       }
3259
3260       case GST_MATROSKA_ID_REFERENCEBLOCK:{
3261         ret = gst_ebml_read_sint (ebml, &id, &referenceblock);
3262         GST_DEBUG_OBJECT (demux, "ReferenceBlock: %" G_GINT64_FORMAT,
3263             referenceblock);
3264         break;
3265       }
3266
3267       case GST_MATROSKA_ID_CODECSTATE:{
3268         guint8 *data;
3269         guint64 data_len = 0;
3270
3271         if ((ret =
3272                 gst_ebml_read_binary (ebml, &id, &data,
3273                     &data_len)) != GST_FLOW_OK)
3274           break;
3275
3276         if (G_UNLIKELY (stream == NULL)) {
3277           GST_WARNING_OBJECT (demux,
3278               "Unexpected CodecState subelement - ignoring");
3279           break;
3280         }
3281
3282         g_free (stream->codec_state);
3283         stream->codec_state = data;
3284         stream->codec_state_size = data_len;
3285
3286         /* Decode if necessary */
3287         if (stream->encodings && stream->encodings->len > 0
3288             && stream->codec_state && stream->codec_state_size > 0) {
3289           if (!gst_matroska_decode_data (stream->encodings,
3290                   &stream->codec_state, &stream->codec_state_size,
3291                   GST_MATROSKA_TRACK_ENCODING_SCOPE_CODEC_DATA, TRUE)) {
3292             GST_WARNING_OBJECT (demux, "Decoding codec state failed");
3293           }
3294         }
3295
3296         GST_DEBUG_OBJECT (demux, "CodecState of %" G_GSIZE_FORMAT " bytes",
3297             stream->codec_state_size);
3298         break;
3299       }
3300
3301       default:
3302         ret = gst_matroska_read_common_parse_skip (&demux->common, ebml,
3303             "BlockGroup", id);
3304         break;
3305
3306       case GST_MATROSKA_ID_BLOCKVIRTUAL:
3307       case GST_MATROSKA_ID_BLOCKADDITIONS:
3308       case GST_MATROSKA_ID_REFERENCEPRIORITY:
3309       case GST_MATROSKA_ID_REFERENCEVIRTUAL:
3310       case GST_MATROSKA_ID_SLICES:
3311         GST_DEBUG_OBJECT (demux,
3312             "Skipping BlockGroup subelement 0x%x - ignoring", id);
3313         ret = gst_ebml_read_skip (ebml);
3314         break;
3315     }
3316
3317     if (is_simpleblock)
3318       break;
3319   }
3320
3321   /* reading a number or so could have failed */
3322   if (ret != GST_FLOW_OK)
3323     goto data_error;
3324
3325   if (ret == GST_FLOW_OK && readblock) {
3326     gboolean invisible_frame = FALSE;
3327     gboolean delta_unit = FALSE;
3328     guint64 duration = 0;
3329     gint64 lace_time = 0;
3330
3331     stream = g_ptr_array_index (demux->common.src, stream_num);
3332
3333     if (cluster_time != GST_CLOCK_TIME_NONE) {
3334       /* FIXME: What to do with negative timestamps? Give timestamp 0 or -1?
3335        * Drop unless the lace contains timestamp 0? */
3336       if (time < 0 && (-time) > cluster_time) {
3337         lace_time = 0;
3338       } else {
3339         if (stream->timecodescale == 1.0)
3340           lace_time = (cluster_time + time) * demux->common.time_scale;
3341         else
3342           lace_time =
3343               gst_util_guint64_to_gdouble ((cluster_time + time) *
3344               demux->common.time_scale) * stream->timecodescale;
3345       }
3346     } else {
3347       lace_time = GST_CLOCK_TIME_NONE;
3348     }
3349
3350     /* need to refresh segment info ASAP */
3351     if (GST_CLOCK_TIME_IS_VALID (lace_time) && demux->need_segment) {
3352       GstSegment *segment = &demux->common.segment;
3353       guint64 clace_time;
3354       GstEvent *segment_event;
3355
3356       if (!GST_CLOCK_TIME_IS_VALID (demux->stream_start_time)) {
3357         demux->stream_start_time = lace_time;
3358         GST_DEBUG_OBJECT (demux,
3359             "Setting stream start time to %" GST_TIME_FORMAT,
3360             GST_TIME_ARGS (lace_time));
3361       }
3362       clace_time = MAX (lace_time, demux->stream_start_time);
3363       if (GST_CLOCK_TIME_IS_VALID (demux->common.segment.position) &&
3364           demux->common.segment.position != 0) {
3365         GST_DEBUG_OBJECT (demux,
3366             "using stored seek position %" GST_TIME_FORMAT,
3367             GST_TIME_ARGS (demux->common.segment.position));
3368         clace_time = demux->common.segment.position + demux->stream_start_time;
3369         segment->position = GST_CLOCK_TIME_NONE;
3370       }
3371       segment->start = clace_time;
3372       segment->stop = GST_CLOCK_TIME_NONE;
3373       segment->time = segment->start - demux->stream_start_time;
3374       segment->position = segment->start - demux->stream_start_time;
3375       GST_DEBUG_OBJECT (demux,
3376           "generated segment starting at %" GST_TIME_FORMAT ": %"
3377           GST_SEGMENT_FORMAT, GST_TIME_ARGS (lace_time), segment);
3378       /* now convey our segment notion downstream */
3379       segment_event = gst_event_new_segment (segment);
3380       if (demux->segment_seqnum)
3381         gst_event_set_seqnum (segment_event, demux->segment_seqnum);
3382       gst_matroska_demux_send_event (demux, segment_event);
3383       demux->need_segment = FALSE;
3384       demux->segment_seqnum = 0;
3385     }
3386
3387     /* send pending codec data headers for all streams,
3388      * before we perform sync across all streams */
3389     gst_matroska_demux_push_codec_data_all (demux);
3390
3391     if (block_duration != -1) {
3392       if (stream->timecodescale == 1.0)
3393         duration = gst_util_uint64_scale (block_duration,
3394             demux->common.time_scale, 1);
3395       else
3396         duration =
3397             gst_util_gdouble_to_guint64 (gst_util_guint64_to_gdouble
3398             (gst_util_uint64_scale (block_duration, demux->common.time_scale,
3399                     1)) * stream->timecodescale);
3400     } else if (stream->default_duration) {
3401       duration = stream->default_duration * laces;
3402     }
3403     /* else duration is diff between timecode of this and next block */
3404
3405     /* For SimpleBlock, look at the keyframe bit in flags. Otherwise,
3406        a ReferenceBlock implies that this is not a keyframe. In either
3407        case, it only makes sense for video streams. */
3408     if (stream->type == GST_MATROSKA_TRACK_TYPE_VIDEO) {
3409       if ((is_simpleblock && !(flags & 0x80)) || referenceblock) {
3410         delta_unit = TRUE;
3411         invisible_frame = ((flags & 0x08)) &&
3412             (!strcmp (stream->codec_id, GST_MATROSKA_CODEC_ID_VIDEO_VP8) ||
3413             !strcmp (stream->codec_id, GST_MATROSKA_CODEC_ID_VIDEO_VP9));
3414       }
3415     }
3416
3417     for (n = 0; n < laces; n++) {
3418       GstBuffer *sub;
3419
3420       if (G_UNLIKELY (lace_size[n] > size)) {
3421         GST_WARNING_OBJECT (demux, "Invalid lace size");
3422         break;
3423       }
3424
3425       /* QoS for video track with an index. the assumption is that
3426          index entries point to keyframes, but if that is not true we
3427          will instad skip until the next keyframe. */
3428       if (GST_CLOCK_TIME_IS_VALID (lace_time) &&
3429           stream->type == GST_MATROSKA_TRACK_TYPE_VIDEO &&
3430           stream->index_table && demux->common.segment.rate > 0.0) {
3431         GstMatroskaTrackVideoContext *videocontext =
3432             (GstMatroskaTrackVideoContext *) stream;
3433         GstClockTime earliest_time;
3434         GstClockTime earliest_stream_time;
3435
3436         GST_OBJECT_LOCK (demux);
3437         earliest_time = videocontext->earliest_time;
3438         GST_OBJECT_UNLOCK (demux);
3439         earliest_stream_time = gst_segment_to_position (&demux->common.segment,
3440             GST_FORMAT_TIME, earliest_time);
3441
3442         if (GST_CLOCK_TIME_IS_VALID (lace_time) &&
3443             GST_CLOCK_TIME_IS_VALID (earliest_stream_time) &&
3444             lace_time <= earliest_stream_time) {
3445           /* find index entry (keyframe) <= earliest_stream_time */
3446           GstMatroskaIndex *entry =
3447               gst_util_array_binary_search (stream->index_table->data,
3448               stream->index_table->len, sizeof (GstMatroskaIndex),
3449               (GCompareDataFunc) gst_matroska_index_seek_find,
3450               GST_SEARCH_MODE_BEFORE, &earliest_stream_time, NULL);
3451
3452           /* if that entry (keyframe) is after the current the current
3453              buffer, we can skip pushing (and thus decoding) all
3454              buffers until that keyframe. */
3455           if (entry && GST_CLOCK_TIME_IS_VALID (entry->time) &&
3456               entry->time > lace_time) {
3457             GST_LOG_OBJECT (demux, "Skipping lace before late keyframe");
3458             stream->set_discont = TRUE;
3459             goto next_lace;
3460           }
3461         }
3462       }
3463
3464       sub = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL,
3465           gst_buffer_get_size (buf) - size, lace_size[n]);
3466       GST_DEBUG_OBJECT (demux, "created subbuffer %p", sub);
3467
3468       if (delta_unit)
3469         GST_BUFFER_FLAG_SET (sub, GST_BUFFER_FLAG_DELTA_UNIT);
3470       else
3471         GST_BUFFER_FLAG_UNSET (sub, GST_BUFFER_FLAG_DELTA_UNIT);
3472
3473       if (invisible_frame)
3474         GST_BUFFER_FLAG_SET (sub, GST_BUFFER_FLAG_DECODE_ONLY);
3475
3476       if (stream->encodings != NULL && stream->encodings->len > 0)
3477         sub = gst_matroska_decode_buffer (stream, sub);
3478
3479       if (sub == NULL) {
3480         GST_WARNING_OBJECT (demux, "Decoding buffer failed");
3481         goto next_lace;
3482       }
3483
3484       buffer_timestamp = gst_matroska_track_get_buffer_timestamp (stream, sub);
3485
3486       if (!stream->dts_only) {
3487         GST_BUFFER_PTS (sub) = lace_time;
3488       } else {
3489         GST_BUFFER_DTS (sub) = lace_time;
3490         if (stream->intra_only)
3491           GST_BUFFER_PTS (sub) = lace_time;
3492       }
3493
3494       if (GST_CLOCK_TIME_IS_VALID (lace_time)) {
3495         GstClockTime last_stop_end;
3496
3497         /* Check if this stream is after segment stop */
3498         if (GST_CLOCK_TIME_IS_VALID (demux->common.segment.stop) &&
3499             lace_time >= demux->common.segment.stop) {
3500           GST_DEBUG_OBJECT (demux,
3501               "Stream %d after segment stop %" GST_TIME_FORMAT, stream->index,
3502               GST_TIME_ARGS (demux->common.segment.stop));
3503           gst_buffer_unref (sub);
3504           goto eos;
3505         }
3506         if (offset >= stream->to_offset
3507             || (GST_CLOCK_TIME_IS_VALID (demux->to_time)
3508                 && lace_time > demux->to_time)) {
3509           GST_DEBUG_OBJECT (demux, "Stream %d after playback section",
3510               stream->index);
3511           gst_buffer_unref (sub);
3512           goto eos;
3513         }
3514
3515         /* handle gaps, e.g. non-zero start-time, or an cue index entry
3516          * that landed us with timestamps not quite intended */
3517         GST_OBJECT_LOCK (demux);
3518         if (demux->max_gap_time &&
3519             GST_CLOCK_TIME_IS_VALID (demux->last_stop_end) &&
3520             demux->common.segment.rate > 0.0) {
3521           GstClockTimeDiff diff;
3522
3523           /* only send segments with increasing start times,
3524            * otherwise if these go back and forth downstream (sinks) increase
3525            * accumulated time and running_time */
3526           diff = GST_CLOCK_DIFF (demux->last_stop_end, lace_time);
3527           if (diff > 0 && diff > demux->max_gap_time
3528               && lace_time > demux->common.segment.start
3529               && (!GST_CLOCK_TIME_IS_VALID (demux->common.segment.stop)
3530                   || lace_time < demux->common.segment.stop)) {
3531             GstEvent *event;
3532             GST_DEBUG_OBJECT (demux,
3533                 "Gap of %" G_GINT64_FORMAT " ns detected in"
3534                 "stream %d (%" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "). "
3535                 "Sending updated SEGMENT events", diff,
3536                 stream->index, GST_TIME_ARGS (stream->pos),
3537                 GST_TIME_ARGS (lace_time));
3538
3539             event = gst_event_new_gap (demux->last_stop_end, diff);
3540             GST_OBJECT_UNLOCK (demux);
3541             gst_pad_push_event (stream->pad, event);
3542             GST_OBJECT_LOCK (demux);
3543           }
3544         }
3545
3546         if (!GST_CLOCK_TIME_IS_VALID (demux->common.segment.position)
3547             || demux->common.segment.position < lace_time) {
3548           demux->common.segment.position = lace_time;
3549         }
3550         GST_OBJECT_UNLOCK (demux);
3551
3552         last_stop_end = lace_time;
3553         if (duration) {
3554           GST_BUFFER_DURATION (sub) = duration / laces;
3555           last_stop_end += GST_BUFFER_DURATION (sub);
3556         }
3557
3558         if (!GST_CLOCK_TIME_IS_VALID (demux->last_stop_end) ||
3559             demux->last_stop_end < last_stop_end)
3560           demux->last_stop_end = last_stop_end;
3561
3562         GST_OBJECT_LOCK (demux);
3563         if (demux->common.segment.duration == -1 ||
3564             demux->stream_start_time + demux->common.segment.duration <
3565             last_stop_end) {
3566           demux->common.segment.duration =
3567               last_stop_end - demux->stream_start_time;
3568           GST_OBJECT_UNLOCK (demux);
3569           if (!demux->invalid_duration) {
3570             gst_element_post_message (GST_ELEMENT_CAST (demux),
3571                 gst_message_new_duration_changed (GST_OBJECT_CAST (demux)));
3572             demux->invalid_duration = TRUE;
3573           }
3574         } else {
3575           GST_OBJECT_UNLOCK (demux);
3576         }
3577       }
3578
3579       stream->pos = lace_time;
3580
3581       gst_matroska_demux_sync_streams (demux);
3582
3583       if (stream->set_discont) {
3584         GST_DEBUG_OBJECT (demux, "marking DISCONT");
3585         GST_BUFFER_FLAG_SET (sub, GST_BUFFER_FLAG_DISCONT);
3586         stream->set_discont = FALSE;
3587       } else {
3588         GST_BUFFER_FLAG_UNSET (sub, GST_BUFFER_FLAG_DISCONT);
3589       }
3590
3591       /* reverse playback book-keeping */
3592       if (!GST_CLOCK_TIME_IS_VALID (stream->from_time))
3593         stream->from_time = lace_time;
3594       if (stream->from_offset == -1)
3595         stream->from_offset = offset;
3596
3597       GST_DEBUG_OBJECT (demux,
3598           "Pushing lace %d, data of size %" G_GSIZE_FORMAT
3599           " for stream %d, time=%" GST_TIME_FORMAT " and duration=%"
3600           GST_TIME_FORMAT, n, gst_buffer_get_size (sub), stream_num,
3601           GST_TIME_ARGS (buffer_timestamp),
3602           GST_TIME_ARGS (GST_BUFFER_DURATION (sub)));
3603
3604 #if 0
3605       if (demux->common.element_index) {
3606         if (stream->index_writer_id == -1)
3607           gst_index_get_writer_id (demux->common.element_index,
3608               GST_OBJECT (stream->pad), &stream->index_writer_id);
3609
3610         GST_LOG_OBJECT (demux, "adding association %" GST_TIME_FORMAT "-> %"
3611             G_GUINT64_FORMAT " for writer id %d",
3612             GST_TIME_ARGS (buffer_timestamp), cluster_offset,
3613             stream->index_writer_id);
3614         gst_index_add_association (demux->common.element_index,
3615             stream->index_writer_id, GST_BUFFER_FLAG_IS_SET (sub,
3616                 GST_BUFFER_FLAG_DELTA_UNIT) ? 0 : GST_ASSOCIATION_FLAG_KEY_UNIT,
3617             GST_FORMAT_TIME, buffer_timestamp, GST_FORMAT_BYTES, cluster_offset,
3618             NULL);
3619       }
3620 #endif
3621
3622       /* Postprocess the buffers depending on the codec used */
3623       if (stream->postprocess_frame) {
3624         GST_LOG_OBJECT (demux, "running post process");
3625         ret = stream->postprocess_frame (GST_ELEMENT (demux), stream, &sub);
3626       }
3627
3628       /* At this point, we have a sub-buffer pointing at data within a larger
3629          buffer. This data might not be aligned with anything. If the data is
3630          raw samples though, we want it aligned to the raw type (eg, 4 bytes
3631          for 32 bit samples, etc), or bad things will happen downstream as
3632          elements typically assume minimal alignment.
3633          Therefore, create an aligned copy if necessary. */
3634       g_assert (stream->alignment <= G_MEM_ALIGN);
3635       sub = gst_matroska_demux_align_buffer (demux, sub, stream->alignment);
3636
3637       if (GST_BUFFER_PTS_IS_VALID (sub)) {
3638         stream->pos = GST_BUFFER_PTS (sub);
3639         if (GST_BUFFER_DURATION_IS_VALID (sub))
3640           stream->pos += GST_BUFFER_DURATION (sub);
3641       } else if (GST_BUFFER_DTS_IS_VALID (sub)) {
3642         stream->pos = GST_BUFFER_DTS (sub);
3643         if (GST_BUFFER_DURATION_IS_VALID (sub))
3644           stream->pos += GST_BUFFER_DURATION (sub);
3645       }
3646
3647       ret = gst_pad_push (stream->pad, sub);
3648
3649       if (demux->common.segment.rate < 0) {
3650         if (lace_time > demux->common.segment.stop && ret == GST_FLOW_EOS) {
3651           /* In reverse playback we can get a GST_FLOW_EOS when
3652            * we are at the end of the segment, so we just need to jump
3653            * back to the previous section. */
3654           GST_DEBUG_OBJECT (demux, "downstream has reached end of segment");
3655           ret = GST_FLOW_OK;
3656         }
3657       }
3658       /* combine flows */
3659       ret = gst_flow_combiner_update_pad_flow (demux->flowcombiner,
3660           stream->pad, ret);
3661
3662     next_lace:
3663       size -= lace_size[n];
3664       if (lace_time != GST_CLOCK_TIME_NONE && duration)
3665         lace_time += duration / laces;
3666       else
3667         lace_time = GST_CLOCK_TIME_NONE;
3668     }
3669   }
3670
3671 done:
3672   if (buf) {
3673     gst_buffer_unmap (buf, &map);
3674     gst_buffer_unref (buf);
3675   }
3676   g_free (lace_size);
3677
3678   return ret;
3679
3680   /* EXITS */
3681 eos:
3682   {
3683     stream->eos = TRUE;
3684     ret = GST_FLOW_OK;
3685     /* combine flows */
3686     ret = gst_flow_combiner_update_pad_flow (demux->flowcombiner, stream->pad,
3687         ret);
3688     goto done;
3689   }
3690 invalid_lacing:
3691   {
3692     GST_ELEMENT_WARNING (demux, STREAM, DEMUX, (NULL), ("Invalid lacing size"));
3693     /* non-fatal, try next block(group) */
3694     ret = GST_FLOW_OK;
3695     goto done;
3696   }
3697 data_error:
3698   {
3699     GST_ELEMENT_WARNING (demux, STREAM, DEMUX, (NULL), ("Data error"));
3700     /* non-fatal, try next block(group) */
3701     ret = GST_FLOW_OK;
3702     goto done;
3703   }
3704 }
3705
3706 /* return FALSE if block(group) should be skipped (due to a seek) */
3707 static inline gboolean
3708 gst_matroska_demux_seek_block (GstMatroskaDemux * demux)
3709 {
3710   if (G_UNLIKELY (demux->seek_block)) {
3711     if (!(--demux->seek_block)) {
3712       return TRUE;
3713     } else {
3714       GST_LOG_OBJECT (demux, "should skip block due to seek");
3715       return FALSE;
3716     }
3717   } else {
3718     return TRUE;
3719   }
3720 }
3721
3722 static GstFlowReturn
3723 gst_matroska_demux_parse_contents_seekentry (GstMatroskaDemux * demux,
3724     GstEbmlRead * ebml)
3725 {
3726   GstFlowReturn ret;
3727   guint64 seek_pos = (guint64) - 1;
3728   guint32 seek_id = 0;
3729   guint32 id;
3730
3731   DEBUG_ELEMENT_START (demux, ebml, "Seek");
3732
3733   if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
3734     DEBUG_ELEMENT_STOP (demux, ebml, "Seek", ret);
3735     return ret;
3736   }
3737
3738   while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
3739     if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
3740       break;
3741
3742     switch (id) {
3743       case GST_MATROSKA_ID_SEEKID:
3744       {
3745         guint64 t;
3746
3747         if ((ret = gst_ebml_read_uint (ebml, &id, &t)) != GST_FLOW_OK)
3748           break;
3749
3750         GST_DEBUG_OBJECT (demux, "SeekID: %" G_GUINT64_FORMAT, t);
3751         seek_id = t;
3752         break;
3753       }
3754
3755       case GST_MATROSKA_ID_SEEKPOSITION:
3756       {
3757         guint64 t;
3758
3759         if ((ret = gst_ebml_read_uint (ebml, &id, &t)) != GST_FLOW_OK)
3760           break;
3761
3762         if (t > G_MAXINT64) {
3763           GST_WARNING_OBJECT (demux,
3764               "Too large SeekPosition %" G_GUINT64_FORMAT, t);
3765           break;
3766         }
3767
3768         GST_DEBUG_OBJECT (demux, "SeekPosition: %" G_GUINT64_FORMAT, t);
3769         seek_pos = t;
3770         break;
3771       }
3772
3773       default:
3774         ret = gst_matroska_read_common_parse_skip (&demux->common, ebml,
3775             "SeekHead", id);
3776         break;
3777     }
3778   }
3779
3780   if (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)
3781     return ret;
3782
3783   if (!seek_id || seek_pos == (guint64) - 1) {
3784     GST_WARNING_OBJECT (demux, "Incomplete seekhead entry (0x%x/%"
3785         G_GUINT64_FORMAT ")", seek_id, seek_pos);
3786     return GST_FLOW_OK;
3787   }
3788
3789   switch (seek_id) {
3790     case GST_MATROSKA_ID_SEEKHEAD:
3791     {
3792     }
3793     case GST_MATROSKA_ID_CUES:
3794     case GST_MATROSKA_ID_TAGS:
3795     case GST_MATROSKA_ID_TRACKS:
3796     case GST_MATROSKA_ID_SEGMENTINFO:
3797     case GST_MATROSKA_ID_ATTACHMENTS:
3798     case GST_MATROSKA_ID_CHAPTERS:
3799     {
3800       guint64 before_pos, length;
3801       guint needed;
3802
3803       /* remember */
3804       length = gst_matroska_read_common_get_length (&demux->common);
3805       before_pos = demux->common.offset;
3806
3807       if (length == (guint64) - 1) {
3808         GST_DEBUG_OBJECT (demux, "no upstream length, skipping SeakHead entry");
3809         break;
3810       }
3811
3812       /* check for validity */
3813       if (seek_pos + demux->common.ebml_segment_start + 12 >= length) {
3814         GST_WARNING_OBJECT (demux,
3815             "SeekHead reference lies outside file!" " (%"
3816             G_GUINT64_FORMAT "+%" G_GUINT64_FORMAT "+12 >= %"
3817             G_GUINT64_FORMAT ")", seek_pos, demux->common.ebml_segment_start,
3818             length);
3819         break;
3820       }
3821
3822       /* only pick up index location when streaming */
3823       if (demux->streaming) {
3824         if (seek_id == GST_MATROSKA_ID_CUES) {
3825           demux->index_offset = seek_pos + demux->common.ebml_segment_start;
3826           GST_DEBUG_OBJECT (demux, "Cues located at offset %" G_GUINT64_FORMAT,
3827               demux->index_offset);
3828         }
3829         break;
3830       }
3831
3832       /* seek */
3833       demux->common.offset = seek_pos + demux->common.ebml_segment_start;
3834
3835       /* check ID */
3836       if ((ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
3837                   GST_ELEMENT_CAST (demux), &id, &length, &needed)) !=
3838           GST_FLOW_OK)
3839         goto finish;
3840
3841       if (id != seek_id) {
3842         GST_WARNING_OBJECT (demux,
3843             "We looked for ID=0x%x but got ID=0x%x (pos=%" G_GUINT64_FORMAT ")",
3844             seek_id, id, seek_pos + demux->common.ebml_segment_start);
3845       } else {
3846         /* now parse */
3847         ret = gst_matroska_demux_parse_id (demux, id, length, needed);
3848       }
3849
3850     finish:
3851       /* seek back */
3852       demux->common.offset = before_pos;
3853       break;
3854     }
3855
3856     case GST_MATROSKA_ID_CLUSTER:
3857     {
3858       guint64 pos = seek_pos + demux->common.ebml_segment_start;
3859
3860       GST_LOG_OBJECT (demux, "Cluster position");
3861       if (G_UNLIKELY (!demux->clusters))
3862         demux->clusters = g_array_sized_new (TRUE, TRUE, sizeof (guint64), 100);
3863       g_array_append_val (demux->clusters, pos);
3864       break;
3865     }
3866
3867     default:
3868       GST_DEBUG_OBJECT (demux, "Ignoring Seek entry for ID=0x%x", seek_id);
3869       break;
3870   }
3871   DEBUG_ELEMENT_STOP (demux, ebml, "Seek", ret);
3872
3873   return ret;
3874 }
3875
3876 static GstFlowReturn
3877 gst_matroska_demux_parse_contents (GstMatroskaDemux * demux, GstEbmlRead * ebml)
3878 {
3879   GstFlowReturn ret = GST_FLOW_OK;
3880   guint32 id;
3881
3882   DEBUG_ELEMENT_START (demux, ebml, "SeekHead");
3883
3884   if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
3885     DEBUG_ELEMENT_STOP (demux, ebml, "SeekHead", ret);
3886     return ret;
3887   }
3888
3889   while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
3890     if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
3891       break;
3892
3893     switch (id) {
3894       case GST_MATROSKA_ID_SEEKENTRY:
3895       {
3896         ret = gst_matroska_demux_parse_contents_seekentry (demux, ebml);
3897         /* Ignore EOS and errors here */
3898         if (ret != GST_FLOW_OK) {
3899           GST_DEBUG_OBJECT (demux, "Ignoring %s", gst_flow_get_name (ret));
3900           ret = GST_FLOW_OK;
3901         }
3902         break;
3903       }
3904
3905       default:
3906         ret = gst_matroska_read_common_parse_skip (&demux->common,
3907             ebml, "SeekHead", id);
3908         break;
3909     }
3910   }
3911
3912   DEBUG_ELEMENT_STOP (demux, ebml, "SeekHead", ret);
3913
3914   /* Sort clusters by position for easier searching */
3915   if (demux->clusters)
3916     g_array_sort (demux->clusters, (GCompareFunc) gst_matroska_cluster_compare);
3917
3918   return ret;
3919 }
3920
3921 #define GST_FLOW_OVERFLOW   GST_FLOW_CUSTOM_ERROR
3922
3923 #define MAX_BLOCK_SIZE (15 * 1024 * 1024)
3924
3925 static inline GstFlowReturn
3926 gst_matroska_demux_check_read_size (GstMatroskaDemux * demux, guint64 bytes)
3927 {
3928   if (G_UNLIKELY (bytes > MAX_BLOCK_SIZE)) {
3929     /* only a few blocks are expected/allowed to be large,
3930      * and will be recursed into, whereas others will be read and must fit */
3931     if (demux->streaming) {
3932       /* fatal in streaming case, as we can't step over easily */
3933       GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL),
3934           ("reading large block of size %" G_GUINT64_FORMAT " not supported; "
3935               "file might be corrupt.", bytes));
3936       return GST_FLOW_ERROR;
3937     } else {
3938       /* indicate higher level to quietly give up */
3939       GST_DEBUG_OBJECT (demux,
3940           "too large block of size %" G_GUINT64_FORMAT, bytes);
3941       return GST_FLOW_ERROR;
3942     }
3943   } else {
3944     return GST_FLOW_OK;
3945   }
3946 }
3947
3948 /* returns TRUE if we truely are in error state, and should give up */
3949 static inline GstFlowReturn
3950 gst_matroska_demux_check_parse_error (GstMatroskaDemux * demux)
3951 {
3952   if (!demux->streaming && demux->next_cluster_offset > 0) {
3953     /* just repositioning to where next cluster should be and try from there */
3954     GST_WARNING_OBJECT (demux, "parse error, trying next cluster expected at %"
3955         G_GUINT64_FORMAT, demux->next_cluster_offset);
3956     demux->common.offset = demux->next_cluster_offset;
3957     demux->next_cluster_offset = 0;
3958     return GST_FLOW_OK;
3959   } else {
3960     gint64 pos;
3961     GstFlowReturn ret;
3962
3963     /* sigh, one last attempt above and beyond call of duty ...;
3964      * search for cluster mark following current pos */
3965     pos = demux->common.offset;
3966     GST_WARNING_OBJECT (demux, "parse error, looking for next cluster");
3967     if ((ret = gst_matroska_demux_search_cluster (demux, &pos)) != GST_FLOW_OK) {
3968       /* did not work, give up */
3969       return ret;
3970     } else {
3971       GST_DEBUG_OBJECT (demux, "... found at  %" G_GUINT64_FORMAT, pos);
3972       /* try that position */
3973       demux->common.offset = pos;
3974       return GST_FLOW_OK;
3975     }
3976   }
3977 }
3978
3979 static inline GstFlowReturn
3980 gst_matroska_demux_flush (GstMatroskaDemux * demux, guint flush)
3981 {
3982   GST_LOG_OBJECT (demux, "skipping %d bytes", flush);
3983   demux->common.offset += flush;
3984   if (demux->streaming) {
3985     GstFlowReturn ret;
3986
3987     /* hard to skip large blocks when streaming */
3988     ret = gst_matroska_demux_check_read_size (demux, flush);
3989     if (ret != GST_FLOW_OK)
3990       return ret;
3991     if (flush <= gst_adapter_available (demux->common.adapter))
3992       gst_adapter_flush (demux->common.adapter, flush);
3993     else
3994       return GST_FLOW_EOS;
3995   }
3996   return GST_FLOW_OK;
3997 }
3998
3999 /* initializes @ebml with @bytes from input stream at current offset.
4000  * Returns EOS if insufficient available,
4001  * ERROR if too much was attempted to read. */
4002 static inline GstFlowReturn
4003 gst_matroska_demux_take (GstMatroskaDemux * demux, guint64 bytes,
4004     GstEbmlRead * ebml)
4005 {
4006   GstBuffer *buffer = NULL;
4007   GstFlowReturn ret = GST_FLOW_OK;
4008
4009   GST_LOG_OBJECT (demux, "taking %" G_GUINT64_FORMAT " bytes for parsing",
4010       bytes);
4011   ret = gst_matroska_demux_check_read_size (demux, bytes);
4012   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
4013     if (!demux->streaming) {
4014       /* in pull mode, we can skip */
4015       if ((ret = gst_matroska_demux_flush (demux, bytes)) == GST_FLOW_OK)
4016         ret = GST_FLOW_OVERFLOW;
4017     } else {
4018       /* otherwise fatal */
4019       ret = GST_FLOW_ERROR;
4020     }
4021     goto exit;
4022   }
4023   if (demux->streaming) {
4024     if (gst_adapter_available (demux->common.adapter) >= bytes)
4025       buffer = gst_adapter_take_buffer (demux->common.adapter, bytes);
4026     else
4027       ret = GST_FLOW_EOS;
4028   } else
4029     ret = gst_matroska_read_common_peek_bytes (&demux->common,
4030         demux->common.offset, bytes, &buffer, NULL);
4031   if (G_LIKELY (buffer)) {
4032     gst_ebml_read_init (ebml, GST_ELEMENT_CAST (demux), buffer,
4033         demux->common.offset);
4034     demux->common.offset += bytes;
4035   }
4036 exit:
4037   return ret;
4038 }
4039
4040 static void
4041 gst_matroska_demux_check_seekability (GstMatroskaDemux * demux)
4042 {
4043   GstQuery *query;
4044   gboolean seekable = FALSE;
4045   gint64 start = -1, stop = -1;
4046
4047   query = gst_query_new_seeking (GST_FORMAT_BYTES);
4048   if (!gst_pad_peer_query (demux->common.sinkpad, query)) {
4049     GST_DEBUG_OBJECT (demux, "seeking query failed");
4050     goto done;
4051   }
4052
4053   gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
4054
4055   /* try harder to query upstream size if we didn't get it the first time */
4056   if (seekable && stop == -1) {
4057     GST_DEBUG_OBJECT (demux, "doing duration query to fix up unset stop");
4058     gst_pad_peer_query_duration (demux->common.sinkpad, GST_FORMAT_BYTES,
4059         &stop);
4060   }
4061
4062   /* if upstream doesn't know the size, it's likely that it's not seekable in
4063    * practice even if it technically may be seekable */
4064   if (seekable && (start != 0 || stop <= start)) {
4065     GST_DEBUG_OBJECT (demux, "seekable but unknown start/stop -> disable");
4066     seekable = FALSE;
4067   }
4068
4069 done:
4070   GST_INFO_OBJECT (demux, "seekable: %d (%" G_GUINT64_FORMAT " - %"
4071       G_GUINT64_FORMAT ")", seekable, start, stop);
4072   demux->seekable = seekable;
4073
4074   gst_query_unref (query);
4075 }
4076
4077 static GstFlowReturn
4078 gst_matroska_demux_find_tracks (GstMatroskaDemux * demux)
4079 {
4080   guint32 id;
4081   guint64 before_pos;
4082   guint64 length;
4083   guint needed;
4084   GstFlowReturn ret = GST_FLOW_OK;
4085
4086   GST_WARNING_OBJECT (demux,
4087       "Found Cluster element before Tracks, searching Tracks");
4088
4089   /* remember */
4090   before_pos = demux->common.offset;
4091
4092   /* Search Tracks element */
4093   while (TRUE) {
4094     ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
4095         GST_ELEMENT_CAST (demux), &id, &length, &needed);
4096     if (ret != GST_FLOW_OK)
4097       break;
4098
4099     if (id != GST_MATROSKA_ID_TRACKS) {
4100       /* we may be skipping large cluster here, so forego size check etc */
4101       /* ... but we can't skip undefined size; force error */
4102       if (length == G_MAXUINT64) {
4103         ret = gst_matroska_demux_check_read_size (demux, length);
4104         break;
4105       } else {
4106         demux->common.offset += needed;
4107         demux->common.offset += length;
4108       }
4109       continue;
4110     }
4111
4112     /* will lead to track parsing ... */
4113     ret = gst_matroska_demux_parse_id (demux, id, length, needed);
4114     break;
4115   }
4116
4117   /* seek back */
4118   demux->common.offset = before_pos;
4119
4120   return ret;
4121 }
4122
4123 #define GST_READ_CHECK(stmt)  \
4124 G_STMT_START { \
4125   if (G_UNLIKELY ((ret = (stmt)) != GST_FLOW_OK)) { \
4126     if (ret == GST_FLOW_OVERFLOW) { \
4127       ret = GST_FLOW_OK; \
4128     } \
4129     goto read_error; \
4130   } \
4131 } G_STMT_END
4132
4133 static GstFlowReturn
4134 gst_matroska_demux_parse_id (GstMatroskaDemux * demux, guint32 id,
4135     guint64 length, guint needed)
4136 {
4137   GstEbmlRead ebml = { 0, };
4138   GstFlowReturn ret = GST_FLOW_OK;
4139   guint64 read;
4140
4141   GST_LOG_OBJECT (demux, "Parsing Element id 0x%x, "
4142       "size %" G_GUINT64_FORMAT ", prefix %d", id, length, needed);
4143
4144   /* if we plan to read and parse this element, we need prefix (id + length)
4145    * and the contents */
4146   /* mind about overflow wrap-around when dealing with undefined size */
4147   read = length;
4148   if (G_LIKELY (length != G_MAXUINT64))
4149     read += needed;
4150
4151   switch (demux->common.state) {
4152     case GST_MATROSKA_READ_STATE_START:
4153       switch (id) {
4154         case GST_EBML_ID_HEADER:
4155           GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4156           ret = gst_matroska_read_common_parse_header (&demux->common, &ebml);
4157           if (ret != GST_FLOW_OK)
4158             goto parse_failed;
4159           demux->common.state = GST_MATROSKA_READ_STATE_SEGMENT;
4160           gst_matroska_demux_check_seekability (demux);
4161           break;
4162         default:
4163           goto invalid_header;
4164           break;
4165       }
4166       break;
4167     case GST_MATROSKA_READ_STATE_SEGMENT:
4168       switch (id) {
4169         case GST_MATROSKA_ID_SEGMENT:
4170           /* eat segment prefix */
4171           GST_READ_CHECK (gst_matroska_demux_flush (demux, needed));
4172           GST_DEBUG_OBJECT (demux,
4173               "Found Segment start at offset %" G_GUINT64_FORMAT " with size %"
4174               G_GUINT64_FORMAT, demux->common.offset, length);
4175           /* seeks are from the beginning of the segment,
4176            * after the segment ID/length */
4177           demux->common.ebml_segment_start = demux->common.offset;
4178           if (length == 0)
4179             length = G_MAXUINT64;
4180           demux->common.ebml_segment_length = length;
4181           demux->common.state = GST_MATROSKA_READ_STATE_HEADER;
4182           break;
4183         default:
4184           GST_WARNING_OBJECT (demux,
4185               "Expected a Segment ID (0x%x), but received 0x%x!",
4186               GST_MATROSKA_ID_SEGMENT, id);
4187           GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4188           break;
4189       }
4190       break;
4191     case GST_MATROSKA_READ_STATE_SCANNING:
4192       if (id != GST_MATROSKA_ID_CLUSTER &&
4193           id != GST_MATROSKA_ID_CLUSTERTIMECODE)
4194         goto skip;
4195       /* fall-through */
4196     case GST_MATROSKA_READ_STATE_HEADER:
4197     case GST_MATROSKA_READ_STATE_DATA:
4198     case GST_MATROSKA_READ_STATE_SEEK:
4199       switch (id) {
4200         case GST_MATROSKA_ID_SEGMENTINFO:
4201           if (!demux->common.segmentinfo_parsed) {
4202             GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4203             ret = gst_matroska_read_common_parse_info (&demux->common,
4204                 GST_ELEMENT_CAST (demux), &ebml);
4205             if (ret == GST_FLOW_OK)
4206               gst_matroska_demux_send_tags (demux);
4207           } else {
4208             GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4209           }
4210           break;
4211         case GST_MATROSKA_ID_TRACKS:
4212           if (!demux->tracks_parsed) {
4213             GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4214             ret = gst_matroska_demux_parse_tracks (demux, &ebml);
4215           } else {
4216             GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4217           }
4218           break;
4219         case GST_MATROSKA_ID_CLUSTER:
4220           if (G_UNLIKELY (!demux->tracks_parsed)) {
4221             if (demux->streaming) {
4222               GST_DEBUG_OBJECT (demux, "Cluster before Track");
4223               goto not_streamable;
4224             } else {
4225               ret = gst_matroska_demux_find_tracks (demux);
4226               if (!demux->tracks_parsed)
4227                 goto no_tracks;
4228             }
4229           }
4230           if (G_UNLIKELY (demux->common.state
4231                   == GST_MATROSKA_READ_STATE_HEADER)) {
4232             demux->common.state = GST_MATROSKA_READ_STATE_DATA;
4233             demux->first_cluster_offset = demux->common.offset;
4234             GST_DEBUG_OBJECT (demux, "signaling no more pads");
4235             gst_element_no_more_pads (GST_ELEMENT (demux));
4236             /* send initial segment - we wait till we know the first
4237                incoming timestamp, so we can properly set the start of
4238                the segment. */
4239             demux->need_segment = TRUE;
4240           }
4241           demux->cluster_time = GST_CLOCK_TIME_NONE;
4242           demux->cluster_offset = demux->common.offset;
4243           if (G_UNLIKELY (!demux->seek_first && demux->seek_block)) {
4244             GST_DEBUG_OBJECT (demux, "seek target block %" G_GUINT64_FORMAT
4245                 " not found in Cluster, trying next Cluster's first block instead",
4246                 demux->seek_block);
4247             demux->seek_block = 0;
4248           }
4249           demux->seek_first = FALSE;
4250           /* record next cluster for recovery */
4251           if (read != G_MAXUINT64)
4252             demux->next_cluster_offset = demux->cluster_offset + read;
4253           /* eat cluster prefix */
4254           gst_matroska_demux_flush (demux, needed);
4255           break;
4256         case GST_MATROSKA_ID_CLUSTERTIMECODE:
4257         {
4258           guint64 num;
4259
4260           GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4261           if ((ret = gst_ebml_read_uint (&ebml, &id, &num)) != GST_FLOW_OK)
4262             goto parse_failed;
4263           GST_DEBUG_OBJECT (demux, "ClusterTimeCode: %" G_GUINT64_FORMAT, num);
4264           demux->cluster_time = num;
4265 #if 0
4266           if (demux->common.element_index) {
4267             if (demux->common.element_index_writer_id == -1)
4268               gst_index_get_writer_id (demux->common.element_index,
4269                   GST_OBJECT (demux), &demux->common.element_index_writer_id);
4270             GST_LOG_OBJECT (demux, "adding association %" GST_TIME_FORMAT "-> %"
4271                 G_GUINT64_FORMAT " for writer id %d",
4272                 GST_TIME_ARGS (demux->cluster_time), demux->cluster_offset,
4273                 demux->common.element_index_writer_id);
4274             gst_index_add_association (demux->common.element_index,
4275                 demux->common.element_index_writer_id,
4276                 GST_ASSOCIATION_FLAG_KEY_UNIT,
4277                 GST_FORMAT_TIME, demux->cluster_time,
4278                 GST_FORMAT_BYTES, demux->cluster_offset, NULL);
4279           }
4280 #endif
4281           break;
4282         }
4283         case GST_MATROSKA_ID_BLOCKGROUP:
4284           if (!gst_matroska_demux_seek_block (demux))
4285             goto skip;
4286           GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4287           DEBUG_ELEMENT_START (demux, &ebml, "BlockGroup");
4288           if ((ret = gst_ebml_read_master (&ebml, &id)) == GST_FLOW_OK) {
4289             ret = gst_matroska_demux_parse_blockgroup_or_simpleblock (demux,
4290                 &ebml, demux->cluster_time, demux->cluster_offset, FALSE);
4291           }
4292           DEBUG_ELEMENT_STOP (demux, &ebml, "BlockGroup", ret);
4293           break;
4294         case GST_MATROSKA_ID_SIMPLEBLOCK:
4295           if (!gst_matroska_demux_seek_block (demux))
4296             goto skip;
4297           GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4298           DEBUG_ELEMENT_START (demux, &ebml, "SimpleBlock");
4299           ret = gst_matroska_demux_parse_blockgroup_or_simpleblock (demux,
4300               &ebml, demux->cluster_time, demux->cluster_offset, TRUE);
4301           DEBUG_ELEMENT_STOP (demux, &ebml, "SimpleBlock", ret);
4302           break;
4303         case GST_MATROSKA_ID_ATTACHMENTS:
4304           if (!demux->common.attachments_parsed) {
4305             GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4306             ret = gst_matroska_read_common_parse_attachments (&demux->common,
4307                 GST_ELEMENT_CAST (demux), &ebml);
4308             if (ret == GST_FLOW_OK)
4309               gst_matroska_demux_send_tags (demux);
4310           } else {
4311             GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4312           }
4313           break;
4314         case GST_MATROSKA_ID_TAGS:
4315           GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4316           ret = gst_matroska_read_common_parse_metadata (&demux->common,
4317               GST_ELEMENT_CAST (demux), &ebml);
4318           if (ret == GST_FLOW_OK)
4319             gst_matroska_demux_send_tags (demux);
4320           break;
4321         case GST_MATROSKA_ID_CHAPTERS:
4322           if (!demux->common.chapters_parsed) {
4323             GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4324             ret =
4325                 gst_matroska_read_common_parse_chapters (&demux->common, &ebml);
4326
4327             if (demux->common.toc) {
4328               gst_matroska_demux_send_event (demux,
4329                   gst_event_new_toc (demux->common.toc, FALSE));
4330             }
4331           } else
4332             GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4333           break;
4334         case GST_MATROSKA_ID_SEEKHEAD:
4335           GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4336           ret = gst_matroska_demux_parse_contents (demux, &ebml);
4337           break;
4338         case GST_MATROSKA_ID_CUES:
4339           if (demux->common.index_parsed) {
4340             GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4341             break;
4342           }
4343           GST_READ_CHECK (gst_matroska_demux_take (demux, read, &ebml));
4344           ret = gst_matroska_read_common_parse_index (&demux->common, &ebml);
4345           /* only push based; delayed index building */
4346           if (ret == GST_FLOW_OK
4347               && demux->common.state == GST_MATROSKA_READ_STATE_SEEK) {
4348             GstEvent *event;
4349
4350             GST_OBJECT_LOCK (demux);
4351             event = demux->seek_event;
4352             demux->seek_event = NULL;
4353             GST_OBJECT_UNLOCK (demux);
4354
4355             g_assert (event);
4356             /* unlikely to fail, since we managed to seek to this point */
4357             if (!gst_matroska_demux_handle_seek_event (demux, NULL, event))
4358               goto seek_failed;
4359             /* resume data handling, main thread clear to seek again */
4360             GST_OBJECT_LOCK (demux);
4361             demux->common.state = GST_MATROSKA_READ_STATE_DATA;
4362             GST_OBJECT_UNLOCK (demux);
4363           }
4364           break;
4365         case GST_MATROSKA_ID_POSITION:
4366         case GST_MATROSKA_ID_PREVSIZE:
4367         case GST_MATROSKA_ID_ENCRYPTEDBLOCK:
4368         case GST_MATROSKA_ID_SILENTTRACKS:
4369           GST_DEBUG_OBJECT (demux,
4370               "Skipping Cluster subelement 0x%x - ignoring", id);
4371           /* fall-through */
4372         default:
4373         skip:
4374           GST_DEBUG_OBJECT (demux, "skipping Element 0x%x", id);
4375           GST_READ_CHECK (gst_matroska_demux_flush (demux, read));
4376           break;
4377       }
4378       break;
4379   }
4380
4381   if (ret == GST_FLOW_PARSE)
4382     goto parse_failed;
4383
4384 exit:
4385   gst_ebml_read_clear (&ebml);
4386   return ret;
4387
4388   /* ERRORS */
4389 read_error:
4390   {
4391     /* simply exit, maybe not enough data yet */
4392     /* no ebml to clear if read error */
4393     return ret;
4394   }
4395 parse_failed:
4396   {
4397     GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL),
4398         ("Failed to parse Element 0x%x", id));
4399     ret = GST_FLOW_ERROR;
4400     goto exit;
4401   }
4402 not_streamable:
4403   {
4404     GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL),
4405         ("File layout does not permit streaming"));
4406     ret = GST_FLOW_ERROR;
4407     goto exit;
4408   }
4409 no_tracks:
4410   {
4411     GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL),
4412         ("No Tracks element found"));
4413     ret = GST_FLOW_ERROR;
4414     goto exit;
4415   }
4416 invalid_header:
4417   {
4418     GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL), ("Invalid header"));
4419     ret = GST_FLOW_ERROR;
4420     goto exit;
4421   }
4422 seek_failed:
4423   {
4424     GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL), ("Failed to seek"));
4425     ret = GST_FLOW_ERROR;
4426     goto exit;
4427   }
4428 }
4429
4430 static void
4431 gst_matroska_demux_loop (GstPad * pad)
4432 {
4433   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (GST_PAD_PARENT (pad));
4434   GstFlowReturn ret;
4435   guint32 id;
4436   guint64 length;
4437   guint needed;
4438
4439   /* If we have to close a segment, send a new segment to do this now */
4440   if (G_LIKELY (demux->common.state == GST_MATROSKA_READ_STATE_DATA)) {
4441     if (G_UNLIKELY (demux->new_segment)) {
4442       gst_matroska_demux_send_event (demux, demux->new_segment);
4443       demux->new_segment = NULL;
4444     }
4445   }
4446
4447   ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
4448       GST_ELEMENT_CAST (demux), &id, &length, &needed);
4449   if (ret == GST_FLOW_EOS) {
4450     goto eos;
4451   } else if (ret == GST_FLOW_FLUSHING) {
4452     goto pause;
4453   } else if (ret != GST_FLOW_OK) {
4454     ret = gst_matroska_demux_check_parse_error (demux);
4455
4456     /* Only handle EOS as no error if we're outside the segment already */
4457     if (ret == GST_FLOW_EOS && (demux->common.ebml_segment_length != G_MAXUINT64
4458             && demux->common.offset >=
4459             demux->common.ebml_segment_start +
4460             demux->common.ebml_segment_length))
4461       goto eos;
4462     else if (ret != GST_FLOW_OK)
4463       goto pause;
4464     else
4465       return;
4466   }
4467
4468   GST_LOG_OBJECT (demux, "Offset %" G_GUINT64_FORMAT ", Element id 0x%x, "
4469       "size %" G_GUINT64_FORMAT ", needed %d", demux->common.offset, id,
4470       length, needed);
4471
4472   ret = gst_matroska_demux_parse_id (demux, id, length, needed);
4473   if (ret == GST_FLOW_EOS)
4474     goto eos;
4475   if (ret != GST_FLOW_OK)
4476     goto pause;
4477
4478   /* check if we're at the end of a configured segment */
4479   if (G_LIKELY (demux->common.src->len)) {
4480     guint i;
4481
4482     g_assert (demux->common.num_streams == demux->common.src->len);
4483     for (i = 0; i < demux->common.src->len; i++) {
4484       GstMatroskaTrackContext *context = g_ptr_array_index (demux->common.src,
4485           i);
4486       GST_DEBUG_OBJECT (context->pad, "pos %" GST_TIME_FORMAT,
4487           GST_TIME_ARGS (context->pos));
4488       if (context->eos == FALSE)
4489         goto next;
4490     }
4491
4492     GST_INFO_OBJECT (demux, "All streams are EOS");
4493     ret = GST_FLOW_EOS;
4494     goto eos;
4495   }
4496
4497 next:
4498   if (G_UNLIKELY (demux->cached_length == G_MAXUINT64 ||
4499           demux->common.offset >= demux->cached_length)) {
4500     demux->cached_length = gst_matroska_read_common_get_length (&demux->common);
4501     if (demux->common.offset == demux->cached_length) {
4502       GST_LOG_OBJECT (demux, "Reached end of stream");
4503       ret = GST_FLOW_EOS;
4504       goto eos;
4505     }
4506   }
4507
4508   return;
4509
4510   /* ERRORS */
4511 eos:
4512   {
4513     if (demux->common.segment.rate < 0.0) {
4514       ret = gst_matroska_demux_seek_to_previous_keyframe (demux);
4515       if (ret == GST_FLOW_OK)
4516         return;
4517     }
4518     /* fall-through */
4519   }
4520 pause:
4521   {
4522     const gchar *reason = gst_flow_get_name (ret);
4523     gboolean push_eos = FALSE;
4524
4525     GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
4526     gst_pad_pause_task (demux->common.sinkpad);
4527
4528     if (ret == GST_FLOW_EOS) {
4529       /* perform EOS logic */
4530
4531       /* If we were in the headers, make sure we send no-more-pads.
4532          This will ensure decodebin2 does not get stuck thinking
4533          the chain is not complete yet, and waiting indefinitely. */
4534       if (G_UNLIKELY (demux->common.state == GST_MATROSKA_READ_STATE_HEADER)) {
4535         if (demux->common.src->len == 0) {
4536           GST_ELEMENT_ERROR (demux, STREAM, FAILED, (NULL),
4537               ("No pads created"));
4538         } else {
4539           GST_ELEMENT_WARNING (demux, STREAM, DEMUX, (NULL),
4540               ("Failed to finish reading headers"));
4541         }
4542         gst_element_no_more_pads (GST_ELEMENT (demux));
4543       }
4544
4545       if (demux->common.segment.flags & GST_SEEK_FLAG_SEGMENT) {
4546         gint64 stop;
4547
4548         /* for segment playback we need to post when (in stream time)
4549          * we stopped, this is either stop (when set) or the duration. */
4550         if ((stop = demux->common.segment.stop) == -1)
4551           stop = demux->last_stop_end;
4552
4553         GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
4554         gst_element_post_message (GST_ELEMENT (demux),
4555             gst_message_new_segment_done (GST_OBJECT (demux), GST_FORMAT_TIME,
4556                 stop));
4557         gst_matroska_demux_send_event (demux,
4558             gst_event_new_segment_done (GST_FORMAT_TIME, stop));
4559       } else {
4560         push_eos = TRUE;
4561       }
4562     } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4563       /* for fatal errors we post an error message */
4564       GST_ELEMENT_ERROR (demux, STREAM, FAILED, (NULL),
4565           ("stream stopped, reason %s", reason));
4566       push_eos = TRUE;
4567     }
4568     if (push_eos) {
4569       /* send EOS, and prevent hanging if no streams yet */
4570       GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
4571       if (!gst_matroska_demux_send_event (demux, gst_event_new_eos ()) &&
4572           (ret == GST_FLOW_EOS)) {
4573         GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
4574             (NULL), ("got eos but no streams (yet)"));
4575       }
4576     }
4577     return;
4578   }
4579 }
4580
4581 /*
4582  * Create and push a flushing seek event upstream
4583  */
4584 static gboolean
4585 perform_seek_to_offset (GstMatroskaDemux * demux, gdouble rate, guint64 offset,
4586     guint32 seqnum)
4587 {
4588   GstEvent *event;
4589   gboolean res = 0;
4590
4591   GST_DEBUG_OBJECT (demux, "Seeking to %" G_GUINT64_FORMAT, offset);
4592
4593   event =
4594       gst_event_new_seek (rate, GST_FORMAT_BYTES,
4595       GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
4596       GST_SEEK_TYPE_NONE, -1);
4597   gst_event_set_seqnum (event, seqnum);
4598
4599   res = gst_pad_push_event (demux->common.sinkpad, event);
4600
4601   /* segment event will update offset */
4602   return res;
4603 }
4604
4605 static GstFlowReturn
4606 gst_matroska_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
4607 {
4608   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
4609   guint available;
4610   GstFlowReturn ret = GST_FLOW_OK;
4611   guint needed = 0;
4612   guint32 id;
4613   guint64 length;
4614
4615   if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (buffer))) {
4616     GST_DEBUG_OBJECT (demux, "got DISCONT");
4617     gst_adapter_clear (demux->common.adapter);
4618     GST_OBJECT_LOCK (demux);
4619     gst_matroska_read_common_reset_streams (&demux->common,
4620         GST_CLOCK_TIME_NONE, FALSE);
4621     GST_OBJECT_UNLOCK (demux);
4622   }
4623
4624   gst_adapter_push (demux->common.adapter, buffer);
4625   buffer = NULL;
4626
4627 next:
4628   available = gst_adapter_available (demux->common.adapter);
4629
4630   ret = gst_matroska_read_common_peek_id_length_push (&demux->common,
4631       GST_ELEMENT_CAST (demux), &id, &length, &needed);
4632   if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)) {
4633     if (demux->common.ebml_segment_length != G_MAXUINT64
4634         && demux->common.offset >=
4635         demux->common.ebml_segment_start + demux->common.ebml_segment_length)
4636       ret = GST_FLOW_EOS;
4637     return ret;
4638   }
4639
4640   GST_LOG_OBJECT (demux, "Offset %" G_GUINT64_FORMAT ", Element id 0x%x, "
4641       "size %" G_GUINT64_FORMAT ", needed %d, available %d",
4642       demux->common.offset, id, length, needed, available);
4643
4644   if (needed > available)
4645     return GST_FLOW_OK;
4646
4647   ret = gst_matroska_demux_parse_id (demux, id, length, needed);
4648   if (ret == GST_FLOW_EOS) {
4649     /* need more data */
4650     return GST_FLOW_OK;
4651   } else if (ret != GST_FLOW_OK) {
4652     return ret;
4653   } else
4654     goto next;
4655 }
4656
4657 static gboolean
4658 gst_matroska_demux_handle_sink_event (GstPad * pad, GstObject * parent,
4659     GstEvent * event)
4660 {
4661   gboolean res = TRUE;
4662   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
4663
4664   GST_DEBUG_OBJECT (demux,
4665       "have event type %s: %p on sink pad", GST_EVENT_TYPE_NAME (event), event);
4666
4667   switch (GST_EVENT_TYPE (event)) {
4668     case GST_EVENT_SEGMENT:
4669     {
4670       const GstSegment *segment;
4671
4672       /* some debug output */
4673       gst_event_parse_segment (event, &segment);
4674       /* FIXME: do we need to update segment base here (like accum in 0.10)? */
4675       GST_DEBUG_OBJECT (demux,
4676           "received format %d segment %" GST_SEGMENT_FORMAT, segment->format,
4677           segment);
4678
4679       if (demux->common.state < GST_MATROSKA_READ_STATE_DATA) {
4680         GST_DEBUG_OBJECT (demux, "still starting");
4681         goto exit;
4682       }
4683
4684       /* we only expect a BYTE segment, e.g. following a seek */
4685       if (segment->format != GST_FORMAT_BYTES) {
4686         GST_DEBUG_OBJECT (demux, "unsupported segment format, ignoring");
4687         goto exit;
4688       }
4689
4690       GST_DEBUG_OBJECT (demux, "clearing segment state");
4691       GST_OBJECT_LOCK (demux);
4692       /* clear current segment leftover */
4693       gst_adapter_clear (demux->common.adapter);
4694       /* and some streaming setup */
4695       demux->common.offset = segment->start;
4696       /* accumulate base based on current position */
4697       if (GST_CLOCK_TIME_IS_VALID (demux->common.segment.position))
4698         demux->common.segment.base +=
4699             (MAX (demux->common.segment.position, demux->stream_start_time)
4700             - demux->stream_start_time) / fabs (demux->common.segment.rate);
4701       /* do not know where we are;
4702        * need to come across a cluster and generate segment */
4703       demux->common.segment.position = GST_CLOCK_TIME_NONE;
4704       demux->cluster_time = GST_CLOCK_TIME_NONE;
4705       demux->cluster_offset = 0;
4706       demux->need_segment = TRUE;
4707       demux->segment_seqnum = gst_event_get_seqnum (event);
4708       /* but keep some of the upstream segment */
4709       demux->common.segment.rate = segment->rate;
4710       /* also check if need to keep some of the requested seek position */
4711       if (demux->seek_offset == segment->start) {
4712         GST_DEBUG_OBJECT (demux, "position matches requested seek");
4713         demux->common.segment.position = demux->requested_seek_time;
4714       } else {
4715         GST_DEBUG_OBJECT (demux, "unexpected segment position");
4716       }
4717       demux->requested_seek_time = GST_CLOCK_TIME_NONE;
4718       demux->seek_offset = -1;
4719       GST_OBJECT_UNLOCK (demux);
4720     exit:
4721       /* chain will send initial segment after pads have been added,
4722        * or otherwise come up with one */
4723       GST_DEBUG_OBJECT (demux, "eating event");
4724       gst_event_unref (event);
4725       res = TRUE;
4726       break;
4727     }
4728     case GST_EVENT_EOS:
4729     {
4730       if (demux->common.state != GST_MATROSKA_READ_STATE_DATA) {
4731         gst_event_unref (event);
4732         GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
4733             (NULL), ("got eos and didn't receive a complete header object"));
4734       } else if (demux->common.num_streams == 0) {
4735         GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
4736             (NULL), ("got eos but no streams (yet)"));
4737       } else {
4738         gst_matroska_demux_send_event (demux, event);
4739       }
4740       break;
4741     }
4742     case GST_EVENT_FLUSH_STOP:
4743     {
4744       guint64 dur;
4745
4746       gst_adapter_clear (demux->common.adapter);
4747       GST_OBJECT_LOCK (demux);
4748       gst_matroska_read_common_reset_streams (&demux->common,
4749           GST_CLOCK_TIME_NONE, TRUE);
4750       gst_flow_combiner_reset (demux->flowcombiner);
4751       dur = demux->common.segment.duration;
4752       gst_segment_init (&demux->common.segment, GST_FORMAT_TIME);
4753       demux->common.segment.duration = dur;
4754       demux->cluster_time = GST_CLOCK_TIME_NONE;
4755       demux->cluster_offset = 0;
4756       GST_OBJECT_UNLOCK (demux);
4757       /* fall-through */
4758     }
4759     default:
4760       res = gst_pad_event_default (pad, parent, event);
4761       break;
4762   }
4763
4764   return res;
4765 }
4766
4767 static gboolean
4768 gst_matroska_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
4769 {
4770   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
4771   GstQuery *query;
4772   gboolean pull_mode = FALSE;
4773
4774   query = gst_query_new_scheduling ();
4775
4776   if (gst_pad_peer_query (sinkpad, query))
4777     pull_mode = gst_query_has_scheduling_mode_with_flags (query,
4778         GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
4779
4780   gst_query_unref (query);
4781
4782   if (pull_mode) {
4783     GST_DEBUG ("going to pull mode");
4784     demux->streaming = FALSE;
4785     return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
4786   } else {
4787     GST_DEBUG ("going to push (streaming) mode");
4788     demux->streaming = TRUE;
4789     return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
4790   }
4791 }
4792
4793 static gboolean
4794 gst_matroska_demux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
4795     GstPadMode mode, gboolean active)
4796 {
4797   switch (mode) {
4798     case GST_PAD_MODE_PULL:
4799       if (active) {
4800         /* if we have a scheduler we can start the task */
4801         gst_pad_start_task (sinkpad, (GstTaskFunction) gst_matroska_demux_loop,
4802             sinkpad, NULL);
4803       } else {
4804         gst_pad_stop_task (sinkpad);
4805       }
4806       return TRUE;
4807     case GST_PAD_MODE_PUSH:
4808       return TRUE;
4809     default:
4810       return FALSE;
4811   }
4812 }
4813
4814 static GstCaps *
4815 gst_matroska_demux_video_caps (GstMatroskaTrackVideoContext *
4816     videocontext, const gchar * codec_id, guint8 * data, guint size,
4817     gchar ** codec_name, guint32 * riff_fourcc)
4818 {
4819   GstMatroskaTrackContext *context = (GstMatroskaTrackContext *) videocontext;
4820   GstCaps *caps = NULL;
4821
4822   g_assert (videocontext != NULL);
4823   g_assert (codec_name != NULL);
4824
4825   if (riff_fourcc)
4826     *riff_fourcc = 0;
4827
4828   /* TODO: check if we have all codec types from matroska-ids.h
4829    *       check if we have to do more special things with codec_private
4830    *
4831    * Add support for
4832    *  GST_MATROSKA_CODEC_ID_VIDEO_QUICKTIME
4833    *  GST_MATROSKA_CODEC_ID_VIDEO_SNOW
4834    */
4835
4836   if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC)) {
4837     gst_riff_strf_vids *vids = NULL;
4838
4839     if (data) {
4840       GstBuffer *buf = NULL;
4841
4842       vids = (gst_riff_strf_vids *) data;
4843
4844       /* assure size is big enough */
4845       if (size < 24) {
4846         GST_WARNING ("Too small BITMAPINFOHEADER (%d bytes)", size);
4847         return NULL;
4848       }
4849       if (size < sizeof (gst_riff_strf_vids)) {
4850         vids = g_new (gst_riff_strf_vids, 1);
4851         memcpy (vids, data, size);
4852       }
4853
4854       context->dts_only = TRUE; /* VFW files only store DTS */
4855
4856       /* little-endian -> byte-order */
4857       vids->size = GUINT32_FROM_LE (vids->size);
4858       vids->width = GUINT32_FROM_LE (vids->width);
4859       vids->height = GUINT32_FROM_LE (vids->height);
4860       vids->planes = GUINT16_FROM_LE (vids->planes);
4861       vids->bit_cnt = GUINT16_FROM_LE (vids->bit_cnt);
4862       vids->compression = GUINT32_FROM_LE (vids->compression);
4863       vids->image_size = GUINT32_FROM_LE (vids->image_size);
4864       vids->xpels_meter = GUINT32_FROM_LE (vids->xpels_meter);
4865       vids->ypels_meter = GUINT32_FROM_LE (vids->ypels_meter);
4866       vids->num_colors = GUINT32_FROM_LE (vids->num_colors);
4867       vids->imp_colors = GUINT32_FROM_LE (vids->imp_colors);
4868
4869       if (size > sizeof (gst_riff_strf_vids)) { /* some extra_data */
4870         gsize offset = sizeof (gst_riff_strf_vids);
4871
4872         buf =
4873             gst_buffer_new_wrapped (g_memdup ((guint8 *) vids + offset,
4874                 size - offset), size - offset);
4875       }
4876
4877       if (riff_fourcc)
4878         *riff_fourcc = vids->compression;
4879
4880       caps = gst_riff_create_video_caps (vids->compression, NULL, vids,
4881           buf, NULL, codec_name);
4882
4883       if (caps == NULL) {
4884         GST_WARNING ("Unhandled RIFF fourcc %" GST_FOURCC_FORMAT,
4885             GST_FOURCC_ARGS (vids->compression));
4886       } else {
4887         static GstStaticCaps intra_caps = GST_STATIC_CAPS ("image/jpeg; "
4888             "video/x-raw; image/png; video/x-dv; video/x-huffyuv; video/x-ffv; "
4889             "video/x-compressed-yuv");
4890         context->intra_only =
4891             gst_caps_can_intersect (gst_static_caps_get (&intra_caps), caps);
4892       }
4893
4894       if (buf)
4895         gst_buffer_unref (buf);
4896
4897       if (vids != (gst_riff_strf_vids *) data)
4898         g_free (vids);
4899     }
4900   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_UNCOMPRESSED)) {
4901     GstVideoInfo info;
4902     GstVideoFormat format;
4903
4904     gst_video_info_init (&info);
4905     switch (videocontext->fourcc) {
4906       case GST_MAKE_FOURCC ('I', '4', '2', '0'):
4907         format = GST_VIDEO_FORMAT_I420;
4908         break;
4909       case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
4910         format = GST_VIDEO_FORMAT_YUY2;
4911         break;
4912       case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
4913         format = GST_VIDEO_FORMAT_YV12;
4914         break;
4915       case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
4916         format = GST_VIDEO_FORMAT_UYVY;
4917         break;
4918       case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
4919         format = GST_VIDEO_FORMAT_AYUV;
4920         break;
4921       case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
4922       case GST_MAKE_FOURCC ('Y', '8', ' ', ' '):
4923         format = GST_VIDEO_FORMAT_GRAY8;
4924         break;
4925       case GST_MAKE_FOURCC ('R', 'G', 'B', 24):
4926         format = GST_VIDEO_FORMAT_RGB;
4927         break;
4928       case GST_MAKE_FOURCC ('B', 'G', 'R', 24):
4929         format = GST_VIDEO_FORMAT_BGR;
4930         break;
4931       default:
4932         GST_DEBUG ("Unknown fourcc %" GST_FOURCC_FORMAT,
4933             GST_FOURCC_ARGS (videocontext->fourcc));
4934         return NULL;
4935     }
4936
4937     context->intra_only = TRUE;
4938
4939     gst_video_info_set_format (&info, format, videocontext->pixel_width,
4940         videocontext->pixel_height);
4941     caps = gst_video_info_to_caps (&info);
4942     *codec_name = gst_pb_utils_get_codec_description (caps);
4943   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_SP)) {
4944     caps = gst_caps_new_simple ("video/x-divx",
4945         "divxversion", G_TYPE_INT, 4, NULL);
4946     *codec_name = g_strdup ("MPEG-4 simple profile");
4947   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP) ||
4948       !strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_AP)) {
4949     caps = gst_caps_new_simple ("video/mpeg",
4950         "mpegversion", G_TYPE_INT, 4,
4951         "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
4952     if (data) {
4953       GstBuffer *priv;
4954
4955       priv = gst_buffer_new_wrapped (g_memdup (data, size), size);
4956       gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, NULL);
4957       gst_buffer_unref (priv);
4958
4959       gst_codec_utils_mpeg4video_caps_set_level_and_profile (caps, data, size);
4960     }
4961     if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP))
4962       *codec_name = g_strdup ("MPEG-4 advanced simple profile");
4963     else
4964       *codec_name = g_strdup ("MPEG-4 advanced profile");
4965   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3)) {
4966 #if 0
4967     caps = gst_caps_new_full (gst_structure_new ("video/x-divx",
4968             "divxversion", G_TYPE_INT, 3, NULL),
4969         gst_structure_new ("video/x-msmpeg",
4970             "msmpegversion", G_TYPE_INT, 43, NULL), NULL);
4971 #endif
4972     caps = gst_caps_new_simple ("video/x-msmpeg",
4973         "msmpegversion", G_TYPE_INT, 43, NULL);
4974     *codec_name = g_strdup ("Microsoft MPEG-4 v.3");
4975   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG1) ||
4976       !strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG2)) {
4977     gint mpegversion;
4978
4979     if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG1))
4980       mpegversion = 1;
4981     else
4982       mpegversion = 2;
4983
4984     caps = gst_caps_new_simple ("video/mpeg",
4985         "systemstream", G_TYPE_BOOLEAN, FALSE,
4986         "mpegversion", G_TYPE_INT, mpegversion, NULL);
4987     *codec_name = g_strdup_printf ("MPEG-%d video", mpegversion);
4988     context->postprocess_frame = gst_matroska_demux_add_mpeg_seq_header;
4989   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MJPEG)) {
4990     caps = gst_caps_new_empty_simple ("image/jpeg");
4991     *codec_name = g_strdup ("Motion-JPEG");
4992     context->intra_only = TRUE;
4993   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_AVC)) {
4994     caps = gst_caps_new_empty_simple ("video/x-h264");
4995     if (data) {
4996       GstBuffer *priv;
4997
4998       /* First byte is the version, second is the profile indication, and third
4999        * is the 5 contraint_set_flags and 3 reserved bits. Fourth byte is the
5000        * level indication. */
5001       gst_codec_utils_h264_caps_set_level_and_profile (caps, data + 1,
5002           size - 1);
5003
5004       priv = gst_buffer_new_wrapped (g_memdup (data, size), size);
5005       gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, NULL);
5006       gst_buffer_unref (priv);
5007
5008       gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, "avc",
5009           "alignment", G_TYPE_STRING, "au", NULL);
5010     } else {
5011       GST_WARNING ("No codec data found, assuming output is byte-stream");
5012       gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, "byte-stream",
5013           NULL);
5014     }
5015     *codec_name = g_strdup ("H264");
5016   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_MPEGH_HEVC)) {
5017     caps = gst_caps_new_empty_simple ("video/x-h265");
5018     if (data) {
5019       GstBuffer *priv;
5020
5021       gst_codec_utils_h265_caps_set_level_tier_and_profile (caps, data + 1,
5022           size - 1);
5023
5024       priv = gst_buffer_new_wrapped (g_memdup (data, size), size);
5025       gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, NULL);
5026       gst_buffer_unref (priv);
5027
5028       gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, "hvc1",
5029           "alignment", G_TYPE_STRING, "au", NULL);
5030     } else {
5031       GST_WARNING ("No codec data found, assuming output is byte-stream");
5032       gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, "byte-stream",
5033           NULL);
5034     }
5035     *codec_name = g_strdup ("HEVC");
5036   } else if ((!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO1)) ||
5037       (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO2)) ||
5038       (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO3)) ||
5039       (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO4))) {
5040     gint rmversion = -1;
5041
5042     if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO1))
5043       rmversion = 1;
5044     else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO2))
5045       rmversion = 2;
5046     else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO3))
5047       rmversion = 3;
5048     else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_REALVIDEO4))
5049       rmversion = 4;
5050
5051     caps = gst_caps_new_simple ("video/x-pn-realvideo",
5052         "rmversion", G_TYPE_INT, rmversion, NULL);
5053     GST_DEBUG ("data:%p, size:0x%x", data, size);
5054     /* We need to extract the extradata ! */
5055     if (data && (size >= 0x22)) {
5056       GstBuffer *priv;
5057       guint rformat;
5058       guint subformat;
5059
5060       subformat = GST_READ_UINT32_BE (data + 0x1a);
5061       rformat = GST_READ_UINT32_BE (data + 0x1e);
5062
5063       priv =
5064           gst_buffer_new_wrapped (g_memdup (data + 0x1a, size - 0x1a),
5065           size - 0x1a);
5066       gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, "format",
5067           G_TYPE_INT, rformat, "subformat", G_TYPE_INT, subformat, NULL);
5068       gst_buffer_unref (priv);
5069
5070     }
5071     *codec_name = g_strdup_printf ("RealVideo %d.0", rmversion);
5072   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_THEORA)) {
5073     caps = gst_caps_new_empty_simple ("video/x-theora");
5074     context->stream_headers =
5075         gst_matroska_parse_xiph_stream_headers (context->codec_priv,
5076         context->codec_priv_size);
5077     /* FIXME: mark stream as broken and skip if there are no stream headers */
5078     context->send_stream_headers = TRUE;
5079   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_DIRAC)) {
5080     caps = gst_caps_new_empty_simple ("video/x-dirac");
5081     *codec_name = g_strdup_printf ("Dirac");
5082   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_VP8)) {
5083     caps = gst_caps_new_empty_simple ("video/x-vp8");
5084     *codec_name = g_strdup_printf ("On2 VP8");
5085   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_VIDEO_VP9)) {
5086     caps = gst_caps_new_empty_simple ("video/x-vp9");
5087     *codec_name = g_strdup_printf ("On2 VP9");
5088   } else {
5089     GST_WARNING ("Unknown codec '%s', cannot build Caps", codec_id);
5090     return NULL;
5091   }
5092
5093   if (caps != NULL) {
5094     int i;
5095     GstStructure *structure;
5096
5097     for (i = 0; i < gst_caps_get_size (caps); i++) {
5098       structure = gst_caps_get_structure (caps, i);
5099
5100       /* FIXME: use the real unit here! */
5101       GST_DEBUG ("video size %dx%d, target display size %dx%d (any unit)",
5102           videocontext->pixel_width,
5103           videocontext->pixel_height,
5104           videocontext->display_width, videocontext->display_height);
5105
5106       /* pixel width and height are the w and h of the video in pixels */
5107       if (videocontext->pixel_width > 0 && videocontext->pixel_height > 0) {
5108         gint w = videocontext->pixel_width;
5109         gint h = videocontext->pixel_height;
5110
5111         gst_structure_set (structure,
5112             "width", G_TYPE_INT, w, "height", G_TYPE_INT, h, NULL);
5113       }
5114
5115       if (videocontext->display_width > 0 || videocontext->display_height > 0) {
5116         int n, d;
5117
5118         if (videocontext->display_width <= 0)
5119           videocontext->display_width = videocontext->pixel_width;
5120         if (videocontext->display_height <= 0)
5121           videocontext->display_height = videocontext->pixel_height;
5122
5123         /* calculate the pixel aspect ratio using the display and pixel w/h */
5124         n = videocontext->display_width * videocontext->pixel_height;
5125         d = videocontext->display_height * videocontext->pixel_width;
5126         GST_DEBUG ("setting PAR to %d/%d", n, d);
5127         gst_structure_set (structure, "pixel-aspect-ratio",
5128             GST_TYPE_FRACTION,
5129             videocontext->display_width * videocontext->pixel_height,
5130             videocontext->display_height * videocontext->pixel_width, NULL);
5131       }
5132
5133       if (videocontext->default_fps > 0.0) {
5134         gint fps_n, fps_d;
5135
5136         gst_util_double_to_fraction (videocontext->default_fps, &fps_n, &fps_d);
5137
5138         GST_DEBUG ("using default fps %d/%d", fps_n, fps_d);
5139
5140         gst_structure_set (structure, "framerate", GST_TYPE_FRACTION, fps_n,
5141             fps_d, NULL);
5142       } else if (context->default_duration > 0) {
5143         int fps_n, fps_d;
5144
5145         gst_video_guess_framerate (context->default_duration, &fps_n, &fps_d);
5146
5147         GST_INFO ("using default duration %" G_GUINT64_FORMAT
5148             " framerate %d/%d", context->default_duration, fps_n, fps_d);
5149
5150         gst_structure_set (structure, "framerate", GST_TYPE_FRACTION,
5151             fps_n, fps_d, NULL);
5152       } else {
5153         gst_structure_set (structure, "framerate", GST_TYPE_FRACTION,
5154             0, 1, NULL);
5155       }
5156
5157       if (videocontext->parent.flags & GST_MATROSKA_VIDEOTRACK_INTERLACED)
5158         gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
5159             "mixed", NULL);
5160     }
5161
5162     caps = gst_caps_simplify (caps);
5163   }
5164
5165   return caps;
5166 }
5167
5168 /*
5169  * Some AAC specific code... *sigh*
5170  * FIXME: maybe we should use '15' and code the sample rate explicitly
5171  * if the sample rate doesn't match the predefined rates exactly? (tpm)
5172  */
5173
5174 static gint
5175 aac_rate_idx (gint rate)
5176 {
5177   if (92017 <= rate)
5178     return 0;
5179   else if (75132 <= rate)
5180     return 1;
5181   else if (55426 <= rate)
5182     return 2;
5183   else if (46009 <= rate)
5184     return 3;
5185   else if (37566 <= rate)
5186     return 4;
5187   else if (27713 <= rate)
5188     return 5;
5189   else if (23004 <= rate)
5190     return 6;
5191   else if (18783 <= rate)
5192     return 7;
5193   else if (13856 <= rate)
5194     return 8;
5195   else if (11502 <= rate)
5196     return 9;
5197   else if (9391 <= rate)
5198     return 10;
5199   else
5200     return 11;
5201 }
5202
5203 static gint
5204 aac_profile_idx (const gchar * codec_id)
5205 {
5206   gint profile;
5207
5208   if (strlen (codec_id) <= 12)
5209     profile = 3;
5210   else if (!strncmp (&codec_id[12], "MAIN", 4))
5211     profile = 0;
5212   else if (!strncmp (&codec_id[12], "LC", 2))
5213     profile = 1;
5214   else if (!strncmp (&codec_id[12], "SSR", 3))
5215     profile = 2;
5216   else
5217     profile = 3;
5218
5219   return profile;
5220 }
5221
5222 static guint
5223 round_up_pow2 (guint n)
5224 {
5225   n = n - 1;
5226   n = n | (n >> 1);
5227   n = n | (n >> 2);
5228   n = n | (n >> 4);
5229   n = n | (n >> 8);
5230   n = n | (n >> 16);
5231   return n + 1;
5232 }
5233
5234 #define AAC_SYNC_EXTENSION_TYPE 0x02b7
5235
5236 static GstCaps *
5237 gst_matroska_demux_audio_caps (GstMatroskaTrackAudioContext *
5238     audiocontext, const gchar * codec_id, guint8 * data, guint size,
5239     gchar ** codec_name, guint16 * riff_audio_fmt)
5240 {
5241   GstMatroskaTrackContext *context = (GstMatroskaTrackContext *) audiocontext;
5242   GstCaps *caps = NULL;
5243
5244   g_assert (audiocontext != NULL);
5245   g_assert (codec_name != NULL);
5246
5247   if (riff_audio_fmt)
5248     *riff_audio_fmt = 0;
5249
5250   /* TODO: check if we have all codec types from matroska-ids.h
5251    *       check if we have to do more special things with codec_private
5252    *       check if we need bitdepth in different places too
5253    *       implement channel position magic
5254    * Add support for:
5255    *  GST_MATROSKA_CODEC_ID_AUDIO_AC3_BSID9
5256    *  GST_MATROSKA_CODEC_ID_AUDIO_AC3_BSID10
5257    *  GST_MATROSKA_CODEC_ID_AUDIO_QUICKTIME_QDMC
5258    *  GST_MATROSKA_CODEC_ID_AUDIO_QUICKTIME_QDM2
5259    */
5260
5261   if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L1) ||
5262       !strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L2) ||
5263       !strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L3)) {
5264     gint layer;
5265
5266     if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L1))
5267       layer = 1;
5268     else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L2))
5269       layer = 2;
5270     else
5271       layer = 3;
5272
5273     caps = gst_caps_new_simple ("audio/mpeg",
5274         "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer, NULL);
5275     *codec_name = g_strdup_printf ("MPEG-1 layer %d", layer);
5276   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE) ||
5277       !strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_PCM_INT_LE)) {
5278     gboolean sign;
5279     gint endianness;
5280     GstAudioFormat format;
5281
5282     sign = (audiocontext->bitdepth != 8);
5283     if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE))
5284       endianness = G_BIG_ENDIAN;
5285     else
5286       endianness = G_LITTLE_ENDIAN;
5287
5288     format = gst_audio_format_build_integer (sign, endianness,
5289         audiocontext->bitdepth, audiocontext->bitdepth);
5290
5291     /* FIXME: Channel mask and reordering */
5292     caps = gst_caps_new_simple ("audio/x-raw",
5293         "format", G_TYPE_STRING, gst_audio_format_to_string (format),
5294         "layout", G_TYPE_STRING, "interleaved", NULL);
5295
5296     *codec_name = g_strdup_printf ("Raw %d-bit PCM audio",
5297         audiocontext->bitdepth);
5298     context->alignment = GST_ROUND_UP_8 (audiocontext->bitdepth) / 8;
5299     context->alignment = round_up_pow2 (context->alignment);
5300   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_PCM_FLOAT)) {
5301     const gchar *format;
5302     if (audiocontext->bitdepth == 32)
5303       format = "F32LE";
5304     else
5305       format = "F64LE";
5306     /* FIXME: Channel mask and reordering */
5307     caps = gst_caps_new_simple ("audio/x-raw",
5308         "format", G_TYPE_STRING, format,
5309         "layout", G_TYPE_STRING, "interleaved", NULL);
5310     *codec_name = g_strdup_printf ("Raw %d-bit floating-point audio",
5311         audiocontext->bitdepth);
5312     context->alignment = audiocontext->bitdepth / 8;
5313     context->alignment = round_up_pow2 (context->alignment);
5314   } else if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_AC3,
5315           strlen (GST_MATROSKA_CODEC_ID_AUDIO_AC3))) {
5316     caps = gst_caps_new_simple ("audio/x-ac3",
5317         "framed", G_TYPE_BOOLEAN, TRUE, NULL);
5318     *codec_name = g_strdup ("AC-3 audio");
5319   } else if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_EAC3,
5320           strlen (GST_MATROSKA_CODEC_ID_AUDIO_EAC3))) {
5321     caps = gst_caps_new_simple ("audio/x-eac3",
5322         "framed", G_TYPE_BOOLEAN, TRUE, NULL);
5323     *codec_name = g_strdup ("E-AC-3 audio");
5324   } else if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_TRUEHD,
5325           strlen (GST_MATROSKA_CODEC_ID_AUDIO_TRUEHD))) {
5326     caps = gst_caps_new_empty_simple ("audio/x-true-hd");
5327     *codec_name = g_strdup ("Dolby TrueHD");
5328   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_DTS)) {
5329     caps = gst_caps_new_empty_simple ("audio/x-dts");
5330     *codec_name = g_strdup ("DTS audio");
5331   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_VORBIS)) {
5332     caps = gst_caps_new_empty_simple ("audio/x-vorbis");
5333     context->stream_headers =
5334         gst_matroska_parse_xiph_stream_headers (context->codec_priv,
5335         context->codec_priv_size);
5336     /* FIXME: mark stream as broken and skip if there are no stream headers */
5337     context->send_stream_headers = TRUE;
5338   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_FLAC)) {
5339     caps = gst_caps_new_empty_simple ("audio/x-flac");
5340     context->stream_headers =
5341         gst_matroska_parse_flac_stream_headers (context->codec_priv,
5342         context->codec_priv_size);
5343     /* FIXME: mark stream as broken and skip if there are no stream headers */
5344     context->send_stream_headers = TRUE;
5345   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_SPEEX)) {
5346     caps = gst_caps_new_empty_simple ("audio/x-speex");
5347     context->stream_headers =
5348         gst_matroska_parse_speex_stream_headers (context->codec_priv,
5349         context->codec_priv_size);
5350     /* FIXME: mark stream as broken and skip if there are no stream headers */
5351     context->send_stream_headers = TRUE;
5352   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_OPUS)) {
5353     caps = gst_caps_new_empty_simple ("audio/x-opus");
5354     *codec_name = g_strdup ("Opus");
5355     context->stream_headers =
5356         gst_matroska_parse_opus_stream_headers (context->codec_priv,
5357         context->codec_priv_size);
5358     if (context->stream_headers) {
5359       /* There was a valid header. Multistream headers are more than
5360        * 19 bytes, as they include an extra channel mapping table. */
5361       gboolean multistream = (context->codec_priv_size > 19);
5362       gst_caps_set_simple (caps, "multistream", G_TYPE_BOOLEAN, multistream,
5363           NULL);
5364     }
5365     /* FIXME: mark stream as broken and skip if there are no stream headers */
5366     context->send_stream_headers = TRUE;
5367   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_ACM)) {
5368     gst_riff_strf_auds auds;
5369
5370     if (data && size >= 18) {
5371       GstBuffer *codec_data = NULL;
5372
5373       /* little-endian -> byte-order */
5374       auds.format = GST_READ_UINT16_LE (data);
5375       auds.channels = GST_READ_UINT16_LE (data + 2);
5376       auds.rate = GST_READ_UINT32_LE (data + 4);
5377       auds.av_bps = GST_READ_UINT32_LE (data + 8);
5378       auds.blockalign = GST_READ_UINT16_LE (data + 12);
5379       auds.bits_per_sample = GST_READ_UINT16_LE (data + 16);
5380
5381       /* 18 is the waveformatex size */
5382       if (size > 18) {
5383         codec_data = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY,
5384             data + 18, size - 18, 0, size - 18, NULL, NULL);
5385       }
5386
5387       if (riff_audio_fmt)
5388         *riff_audio_fmt = auds.format;
5389
5390       /* FIXME: Handle reorder map */
5391       caps = gst_riff_create_audio_caps (auds.format, NULL, &auds, NULL,
5392           codec_data, codec_name, NULL);
5393       if (codec_data)
5394         gst_buffer_unref (codec_data);
5395
5396       if (caps == NULL) {
5397         GST_WARNING ("Unhandled RIFF audio format 0x%02x", auds.format);
5398       }
5399     } else {
5400       GST_WARNING ("Invalid codec data size (%d expected, got %d)", 18, size);
5401     }
5402   } else if (g_str_has_prefix (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_AAC)) {
5403     GstBuffer *priv = NULL;
5404     gint mpegversion;
5405     gint rate_idx, profile;
5406     guint8 *data = NULL;
5407
5408     /* unspecified AAC profile with opaque private codec data */
5409     if (strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_AAC) == 0) {
5410       if (context->codec_priv_size >= 2) {
5411         guint obj_type, freq_index, explicit_freq_bytes = 0;
5412
5413         codec_id = GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG4;
5414         mpegversion = 4;
5415         freq_index = (GST_READ_UINT16_BE (context->codec_priv) & 0x780) >> 7;
5416         obj_type = (GST_READ_UINT16_BE (context->codec_priv) & 0xF800) >> 11;
5417         if (freq_index == 15)
5418           explicit_freq_bytes = 3;
5419         GST_DEBUG ("obj_type = %u, freq_index = %u", obj_type, freq_index);
5420         priv = gst_buffer_new_wrapped (g_memdup (context->codec_priv,
5421                 context->codec_priv_size), context->codec_priv_size);
5422         /* assume SBR if samplerate <= 24kHz */
5423         if (obj_type == 5 || (freq_index >= 6 && freq_index != 15) ||
5424             (context->codec_priv_size == (5 + explicit_freq_bytes))) {
5425           audiocontext->samplerate *= 2;
5426         }
5427       } else {
5428         GST_WARNING ("Opaque A_AAC codec ID, but no codec private data");
5429         /* this is pretty broken;
5430          * maybe we need to make up some default private,
5431          * or maybe ADTS data got dumped in.
5432          * Let's set up some private data now, and check actual data later */
5433         /* just try this and see what happens ... */
5434         codec_id = GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG4;
5435         context->postprocess_frame = gst_matroska_demux_check_aac;
5436       }
5437     }
5438
5439     /* make up decoder-specific data if it is not supplied */
5440     if (priv == NULL) {
5441       GstMapInfo map;
5442
5443       priv = gst_buffer_new_allocate (NULL, 5, NULL);
5444       gst_buffer_map (priv, &map, GST_MAP_WRITE);
5445       data = map.data;
5446       rate_idx = aac_rate_idx (audiocontext->samplerate);
5447       profile = aac_profile_idx (codec_id);
5448
5449       data[0] = ((profile + 1) << 3) | ((rate_idx & 0xE) >> 1);
5450       data[1] = ((rate_idx & 0x1) << 7) | (audiocontext->channels << 3);
5451
5452       if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG2,
5453               strlen (GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG2))) {
5454         mpegversion = 2;
5455         gst_buffer_unmap (priv, &map);
5456         gst_buffer_set_size (priv, 2);
5457       } else if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG4,
5458               strlen (GST_MATROSKA_CODEC_ID_AUDIO_AAC_MPEG4))) {
5459         mpegversion = 4;
5460
5461         if (g_strrstr (codec_id, "SBR")) {
5462           /* HE-AAC (aka SBR AAC) */
5463           audiocontext->samplerate *= 2;
5464           rate_idx = aac_rate_idx (audiocontext->samplerate);
5465           data[2] = AAC_SYNC_EXTENSION_TYPE >> 3;
5466           data[3] = ((AAC_SYNC_EXTENSION_TYPE & 0x07) << 5) | 5;
5467           data[4] = (1 << 7) | (rate_idx << 3);
5468           gst_buffer_unmap (priv, &map);
5469         } else {
5470           gst_buffer_unmap (priv, &map);
5471           gst_buffer_set_size (priv, 2);
5472         }
5473       } else {
5474         gst_buffer_unmap (priv, &map);
5475         gst_buffer_unref (priv);
5476         priv = NULL;
5477         GST_ERROR ("Unknown AAC profile and no codec private data");
5478       }
5479     }
5480
5481     if (priv) {
5482       caps = gst_caps_new_simple ("audio/mpeg",
5483           "mpegversion", G_TYPE_INT, mpegversion,
5484           "framed", G_TYPE_BOOLEAN, TRUE,
5485           "stream-format", G_TYPE_STRING, "raw", NULL);
5486       gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, NULL);
5487       if (context->codec_priv && context->codec_priv_size > 0)
5488         gst_codec_utils_aac_caps_set_level_and_profile (caps,
5489             context->codec_priv, context->codec_priv_size);
5490       *codec_name = g_strdup_printf ("MPEG-%d AAC audio", mpegversion);
5491       gst_buffer_unref (priv);
5492     }
5493   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_TTA)) {
5494     caps = gst_caps_new_simple ("audio/x-tta",
5495         "width", G_TYPE_INT, audiocontext->bitdepth, NULL);
5496     *codec_name = g_strdup ("TTA audio");
5497   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_WAVPACK4)) {
5498     caps = gst_caps_new_simple ("audio/x-wavpack",
5499         "width", G_TYPE_INT, audiocontext->bitdepth,
5500         "framed", G_TYPE_BOOLEAN, TRUE, NULL);
5501     *codec_name = g_strdup ("Wavpack audio");
5502     context->postprocess_frame = gst_matroska_demux_add_wvpk_header;
5503     audiocontext->wvpk_block_index = 0;
5504   } else if ((!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_14_4)) ||
5505       (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_28_8)) ||
5506       (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_COOK))) {
5507     gint raversion = -1;
5508
5509     if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_14_4))
5510       raversion = 1;
5511     else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_COOK))
5512       raversion = 8;
5513     else
5514       raversion = 2;
5515
5516     caps = gst_caps_new_simple ("audio/x-pn-realaudio",
5517         "raversion", G_TYPE_INT, raversion, NULL);
5518     /* Extract extra information from caps, mapping varies based on codec */
5519     if (data && (size >= 0x50)) {
5520       GstBuffer *priv;
5521       guint flavor;
5522       guint packet_size;
5523       guint height;
5524       guint leaf_size;
5525       guint sample_width;
5526       guint extra_data_size;
5527
5528       GST_ERROR ("real audio raversion:%d", raversion);
5529       if (raversion == 8) {
5530         /* COOK */
5531         flavor = GST_READ_UINT16_BE (data + 22);
5532         packet_size = GST_READ_UINT32_BE (data + 24);
5533         height = GST_READ_UINT16_BE (data + 40);
5534         leaf_size = GST_READ_UINT16_BE (data + 44);
5535         sample_width = GST_READ_UINT16_BE (data + 58);
5536         extra_data_size = GST_READ_UINT32_BE (data + 74);
5537
5538         GST_ERROR
5539             ("flavor:%d, packet_size:%d, height:%d, leaf_size:%d, sample_width:%d, extra_data_size:%d",
5540             flavor, packet_size, height, leaf_size, sample_width,
5541             extra_data_size);
5542         gst_caps_set_simple (caps, "flavor", G_TYPE_INT, flavor, "packet_size",
5543             G_TYPE_INT, packet_size, "height", G_TYPE_INT, height, "leaf_size",
5544             G_TYPE_INT, leaf_size, "width", G_TYPE_INT, sample_width, NULL);
5545
5546         if ((size - 78) >= extra_data_size) {
5547           priv = gst_buffer_new_wrapped (g_memdup (data + 78, extra_data_size),
5548               extra_data_size);
5549           gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, priv, NULL);
5550           gst_buffer_unref (priv);
5551         }
5552       }
5553     }
5554
5555     *codec_name = g_strdup_printf ("RealAudio %d.0", raversion);
5556   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_SIPR)) {
5557     caps = gst_caps_new_empty_simple ("audio/x-sipro");
5558     *codec_name = g_strdup ("Sipro/ACELP.NET Voice Codec");
5559   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_RALF)) {
5560     caps = gst_caps_new_empty_simple ("audio/x-ralf-mpeg4-generic");
5561     *codec_name = g_strdup ("Real Audio Lossless");
5562   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_REAL_ATRC)) {
5563     caps = gst_caps_new_empty_simple ("audio/x-vnd.sony.atrac3");
5564     *codec_name = g_strdup ("Sony ATRAC3");
5565   } else {
5566     GST_WARNING ("Unknown codec '%s', cannot build Caps", codec_id);
5567     return NULL;
5568   }
5569
5570   if (caps != NULL) {
5571     if (audiocontext->samplerate > 0 && audiocontext->channels > 0) {
5572       gint i;
5573
5574       for (i = 0; i < gst_caps_get_size (caps); i++) {
5575         gst_structure_set (gst_caps_get_structure (caps, i),
5576             "channels", G_TYPE_INT, audiocontext->channels,
5577             "rate", G_TYPE_INT, audiocontext->samplerate, NULL);
5578       }
5579     }
5580
5581     caps = gst_caps_simplify (caps);
5582   }
5583
5584   return caps;
5585 }
5586
5587 static GstCaps *
5588 gst_matroska_demux_subtitle_caps (GstMatroskaTrackSubtitleContext *
5589     subtitlecontext, const gchar * codec_id, gpointer data, guint size)
5590 {
5591   GstCaps *caps = NULL;
5592   GstMatroskaTrackContext *context =
5593       (GstMatroskaTrackContext *) subtitlecontext;
5594
5595   /* for backwards compatibility */
5596   if (!g_ascii_strcasecmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_ASCII))
5597     codec_id = GST_MATROSKA_CODEC_ID_SUBTITLE_UTF8;
5598   else if (!g_ascii_strcasecmp (codec_id, "S_SSA"))
5599     codec_id = GST_MATROSKA_CODEC_ID_SUBTITLE_SSA;
5600   else if (!g_ascii_strcasecmp (codec_id, "S_ASS"))
5601     codec_id = GST_MATROSKA_CODEC_ID_SUBTITLE_ASS;
5602   else if (!g_ascii_strcasecmp (codec_id, "S_USF"))
5603     codec_id = GST_MATROSKA_CODEC_ID_SUBTITLE_USF;
5604
5605   /* TODO: Add GST_MATROSKA_CODEC_ID_SUBTITLE_BMP support
5606    * Check if we have to do something with codec_private */
5607   if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_UTF8)) {
5608     /* well, plain text simply does not have a lot of markup ... */
5609     caps = gst_caps_new_simple ("text/x-raw", "format", G_TYPE_STRING,
5610         "pango-markup", NULL);
5611     context->postprocess_frame = gst_matroska_demux_check_subtitle_buffer;
5612     subtitlecontext->check_markup = TRUE;
5613   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_SSA)) {
5614     caps = gst_caps_new_empty_simple ("application/x-ssa");
5615     context->postprocess_frame = gst_matroska_demux_check_subtitle_buffer;
5616     subtitlecontext->check_markup = FALSE;
5617   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_ASS)) {
5618     caps = gst_caps_new_empty_simple ("application/x-ass");
5619     context->postprocess_frame = gst_matroska_demux_check_subtitle_buffer;
5620     subtitlecontext->check_markup = FALSE;
5621   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_USF)) {
5622     caps = gst_caps_new_empty_simple ("application/x-usf");
5623     context->postprocess_frame = gst_matroska_demux_check_subtitle_buffer;
5624     subtitlecontext->check_markup = FALSE;
5625   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_VOBSUB)) {
5626     caps = gst_caps_new_empty_simple ("subpicture/x-dvd");
5627     ((GstMatroskaTrackContext *) subtitlecontext)->send_dvd_event = TRUE;
5628   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_HDMVPGS)) {
5629     caps = gst_caps_new_empty_simple ("subpicture/x-pgs");
5630   } else if (!strcmp (codec_id, GST_MATROSKA_CODEC_ID_SUBTITLE_KATE)) {
5631     caps = gst_caps_new_empty_simple ("subtitle/x-kate");
5632     context->stream_headers =
5633         gst_matroska_parse_xiph_stream_headers (context->codec_priv,
5634         context->codec_priv_size);
5635     /* FIXME: mark stream as broken and skip if there are no stream headers */
5636     context->send_stream_headers = TRUE;
5637   } else {
5638     GST_DEBUG ("Unknown subtitle stream: codec_id='%s'", codec_id);
5639     caps = gst_caps_new_empty_simple ("application/x-subtitle-unknown");
5640   }
5641
5642   if (data != NULL && size > 0) {
5643     GstBuffer *buf;
5644
5645     buf = gst_buffer_new_wrapped (g_memdup (data, size), size);
5646     gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
5647     gst_buffer_unref (buf);
5648   }
5649
5650   return caps;
5651 }
5652
5653 #if 0
5654 static void
5655 gst_matroska_demux_set_index (GstElement * element, GstIndex * index)
5656 {
5657   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
5658
5659   GST_OBJECT_LOCK (demux);
5660   if (demux->common.element_index)
5661     gst_object_unref (demux->common.element_index);
5662   demux->common.element_index = index ? gst_object_ref (index) : NULL;
5663   GST_OBJECT_UNLOCK (demux);
5664   GST_DEBUG_OBJECT (demux, "Set index %" GST_PTR_FORMAT,
5665       demux->common.element_index);
5666 }
5667
5668 static GstIndex *
5669 gst_matroska_demux_get_index (GstElement * element)
5670 {
5671   GstIndex *result = NULL;
5672   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
5673
5674   GST_OBJECT_LOCK (demux);
5675   if (demux->common.element_index)
5676     result = gst_object_ref (demux->common.element_index);
5677   GST_OBJECT_UNLOCK (demux);
5678
5679   GST_DEBUG_OBJECT (demux, "Returning index %" GST_PTR_FORMAT, result);
5680
5681   return result;
5682 }
5683 #endif
5684
5685 static GstStateChangeReturn
5686 gst_matroska_demux_change_state (GstElement * element,
5687     GstStateChange transition)
5688 {
5689   GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
5690   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
5691
5692   /* handle upwards state changes here */
5693   switch (transition) {
5694     default:
5695       break;
5696   }
5697
5698   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5699
5700   /* handle downwards state changes */
5701   switch (transition) {
5702     case GST_STATE_CHANGE_PAUSED_TO_READY:
5703       gst_matroska_demux_reset (GST_ELEMENT (demux));
5704       break;
5705     default:
5706       break;
5707   }
5708
5709   return ret;
5710 }
5711
5712 static void
5713 gst_matroska_demux_set_property (GObject * object,
5714     guint prop_id, const GValue * value, GParamSpec * pspec)
5715 {
5716   GstMatroskaDemux *demux;
5717
5718   g_return_if_fail (GST_IS_MATROSKA_DEMUX (object));
5719   demux = GST_MATROSKA_DEMUX (object);
5720
5721   switch (prop_id) {
5722     case ARG_MAX_GAP_TIME:
5723       GST_OBJECT_LOCK (demux);
5724       demux->max_gap_time = g_value_get_uint64 (value);
5725       GST_OBJECT_UNLOCK (demux);
5726       break;
5727     default:
5728       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5729       break;
5730   }
5731 }
5732
5733 static void
5734 gst_matroska_demux_get_property (GObject * object,
5735     guint prop_id, GValue * value, GParamSpec * pspec)
5736 {
5737   GstMatroskaDemux *demux;
5738
5739   g_return_if_fail (GST_IS_MATROSKA_DEMUX (object));
5740   demux = GST_MATROSKA_DEMUX (object);
5741
5742   switch (prop_id) {
5743     case ARG_MAX_GAP_TIME:
5744       GST_OBJECT_LOCK (demux);
5745       g_value_set_uint64 (value, demux->max_gap_time);
5746       GST_OBJECT_UNLOCK (demux);
5747       break;
5748     default:
5749       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5750       break;
5751   }
5752 }
5753
5754 gboolean
5755 gst_matroska_demux_plugin_init (GstPlugin * plugin)
5756 {
5757   gst_riff_init ();
5758
5759   /* parser helper separate debug */
5760   GST_DEBUG_CATEGORY_INIT (ebmlread_debug, "ebmlread",
5761       0, "EBML stream helper class");
5762
5763   /* create an elementfactory for the matroska_demux element */
5764   if (!gst_element_register (plugin, "matroskademux",
5765           GST_RANK_PRIMARY, GST_TYPE_MATROSKA_DEMUX))
5766     return FALSE;
5767
5768   return TRUE;
5769 }