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