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