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