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