matroska-demux.c 214 KB
Newer Older
1
2
/* GStreamer Matroska muxer/demuxer
 * (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
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * matroska-demux.c: matroska file/stream demuxer
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

24
/* TODO: check CRC32 if present
25
26
27
28
29
30
31
32
 * TODO: there can be a segment after the first segment. Handle like
 *       chained oggs. Fixes #334082
 * TODO: Test samples: http://www.matroska.org/samples/matrix/index.html
 *                     http://samples.mplayerhq.hu/Matroska/
 * TODO: check if demuxing is done correct for all codecs according to spec
 * TODO: seeking with incomplete or without CUE
 */

33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * SECTION:element-matroskademux
 *
 * matroskademux demuxes a Matroska file into the different contained streams.
 *
 * <refsect2>
 * <title>Example launch line</title>
 * |[
 * gst-launch -v filesrc location=/path/to/mkv ! matroskademux ! vorbisdec ! audioconvert ! audioresample ! autoaudiosink
 * ]| This pipeline demuxes a Matroska file and outputs the contained Vorbis audio.
 * </refsect2>
 */


47
48
49
50
51
52
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>
#include <string.h>
53
#include <glib/gprintf.h>
54

55
56
/* For AVI compatibility mode
   and for fourcc stuff */
57
#include <gst/riff/riff-read.h>
58
59
#include <gst/riff/riff-ids.h>
#include <gst/riff/riff-media.h>
60

61
62
63
64
#include <gst/tag/tag.h>

#include <gst/base/gsttypefindhelper.h>

65
66
67
68
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif

69
70
71
72
73
74
#ifdef HAVE_BZ2
#include <bzlib.h>
#endif

#include "lzo.h"

75
76
77
#include "matroska-demux.h"
#include "matroska-ids.h"

78
GST_DEBUG_CATEGORY_STATIC (matroskademux_debug);
79
80
#define GST_CAT_DEFAULT matroskademux_debug

81
82
#define DEBUG_ELEMENT_START(demux, ebml, element) \
    GST_DEBUG_OBJECT (demux, "Parsing " element " element at offset %" \
83
        G_GUINT64_FORMAT, gst_ebml_read_get_pos (ebml))
84
85

#define DEBUG_ELEMENT_STOP(demux, ebml, element, ret) \
86
87
    GST_DEBUG_OBJECT (demux, "Parsing " element " element " \
        " finished with '%s'", gst_flow_get_name (ret))
88

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
89
90
enum
{
91
92
  ARG_0,
  ARG_METADATA,
93
  ARG_STREAMINFO
94
95
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
96
97
98
static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
99
    GST_STATIC_CAPS ("video/x-matroska; video/webm")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
100
    );
101

102
103
/* TODO: fill in caps! */

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
static GstStaticPadTemplate audio_src_templ =
GST_STATIC_PAD_TEMPLATE ("audio_%02d",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS ("ANY")
    );

static GstStaticPadTemplate video_src_templ =
GST_STATIC_PAD_TEMPLATE ("video_%02d",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS ("ANY")
    );

static GstStaticPadTemplate subtitle_src_templ =
    GST_STATIC_PAD_TEMPLATE ("subtitle_%02d",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS ("text/plain; application/x-ssa; application/x-ass; "
123
        "application/x-usf; video/x-dvd-subpicture; "
124
        "subpicture/x-pgs; subtitle/x-kate; " "application/x-subtitle-unknown")
125
126
    );

127
128
static GstFlowReturn gst_matroska_demux_parse_id (GstMatroskaDemux * demux,
    guint32 id, guint64 length, guint needed);
129

130
/* element functions */
131
132
133
static void gst_matroska_demux_loop (GstPad * pad);

static gboolean gst_matroska_demux_element_send_event (GstElement * element,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
134
    GstEvent * event);
135
136
137
static gboolean gst_matroska_demux_element_query (GstElement * element,
    GstQuery * query);

138
/* pad functions */
139
140
141
static gboolean gst_matroska_demux_sink_activate_pull (GstPad * sinkpad,
    gboolean active);
static gboolean gst_matroska_demux_sink_activate (GstPad * sinkpad);
142

143
static gboolean gst_matroska_demux_handle_seek_event (GstMatroskaDemux * demux,
144
    GstPad * pad, GstEvent * event);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
145
146
147
148
149
static gboolean gst_matroska_demux_handle_src_event (GstPad * pad,
    GstEvent * event);
static const GstQueryType *gst_matroska_demux_get_src_query_types (GstPad *
    pad);
static gboolean gst_matroska_demux_handle_src_query (GstPad * pad,
150
    GstQuery * query);
151

152
153
154
155
156
static gboolean gst_matroska_demux_handle_sink_event (GstPad * pad,
    GstEvent * event);
static GstFlowReturn gst_matroska_demux_chain (GstPad * pad,
    GstBuffer * buffer);

157
158
159
static GstStateChangeReturn
gst_matroska_demux_change_state (GstElement * element,
    GstStateChange transition);
160
161
162
static void
gst_matroska_demux_set_index (GstElement * element, GstIndex * index);
static GstIndex *gst_matroska_demux_get_index (GstElement * element);
163
164

/* caps functions */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
165
166
static GstCaps *gst_matroska_demux_video_caps (GstMatroskaTrackVideoContext
    * videocontext,
Jan Schmidt's avatar
Jan Schmidt committed
167
    const gchar * codec_id, guint8 * data, guint size, gchar ** codec_name);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
168
169
static GstCaps *gst_matroska_demux_audio_caps (GstMatroskaTrackAudioContext
    * audiocontext,
Jan Schmidt's avatar
Jan Schmidt committed
170
    const gchar * codec_id, guint8 * data, guint size, gchar ** codec_name);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
171
172
173
static GstCaps
    * gst_matroska_demux_subtitle_caps (GstMatroskaTrackSubtitleContext *
    subtitlecontext, const gchar * codec_id, gpointer data, guint size);
174
175

/* stream methods */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
176
static void gst_matroska_demux_reset (GstElement * element);
177
178
static gboolean perform_seek_to_offset (GstMatroskaDemux * demux,
    guint64 offset);
179

180
GType gst_matroska_demux_get_type (void);
181
182
GST_BOILERPLATE (GstMatroskaDemux, gst_matroska_demux, GstElement,
    GST_TYPE_ELEMENT);
183
184

static void
185
gst_matroska_demux_base_init (gpointer klass)
186
187
188
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

189
190
191
192
193
194
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&video_src_templ));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&audio_src_templ));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&subtitle_src_templ));
195
  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
196
      gst_static_pad_template_get (&sink_templ));
197

198
199
  gst_element_class_set_details_simple (element_class, "Matroska demuxer",
      "Codec/Demuxer",
200
      "Demuxes Matroska/WebM streams into video/audio/subtitles",
201
      "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
202
203
}

204
205
206
207
208
209
210
211
212
213
static void
gst_matroska_demux_finalize (GObject * object)
{
  GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (object);

  if (demux->src) {
    g_ptr_array_free (demux->src, TRUE);
    demux->src = NULL;
  }

214
215
216
217
218
  if (demux->global_tags) {
    gst_tag_list_free (demux->global_tags);
    demux->global_tags = NULL;
  }

219
220
  g_object_unref (demux->adapter);

221
222
223
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

224
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
225
gst_matroska_demux_class_init (GstMatroskaDemuxClass * klass)
226
{
227
228
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstElementClass *gstelement_class = (GstElementClass *) klass;
229

230
231
232
233
  /* parser helper separate debug */
  GST_DEBUG_CATEGORY_INIT (ebmlread_debug, "ebmlread",
      0, "EBML stream helper class");

234
235
  GST_DEBUG_CATEGORY_INIT (matroskademux_debug, "matroskademux", 0,
      "Matroska demuxer");
236

237
238
  gobject_class->finalize = gst_matroska_demux_finalize;

239
240
241
242
  gstelement_class->change_state =
      GST_DEBUG_FUNCPTR (gst_matroska_demux_change_state);
  gstelement_class->send_event =
      GST_DEBUG_FUNCPTR (gst_matroska_demux_element_send_event);
243
244
  gstelement_class->query =
      GST_DEBUG_FUNCPTR (gst_matroska_demux_element_query);
245
246
247
248
249

  gstelement_class->set_index =
      GST_DEBUG_FUNCPTR (gst_matroska_demux_set_index);
  gstelement_class->get_index =
      GST_DEBUG_FUNCPTR (gst_matroska_demux_get_index);
250
251
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
252
static void
253
254
gst_matroska_demux_init (GstMatroskaDemux * demux,
    GstMatroskaDemuxClass * klass)
255
{
256
  demux->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
257
258
259
260
  gst_pad_set_activate_function (demux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_matroska_demux_sink_activate));
  gst_pad_set_activatepull_function (demux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_matroska_demux_sink_activate_pull));
261
262
263
264
  gst_pad_set_chain_function (demux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_matroska_demux_chain));
  gst_pad_set_event_function (demux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_matroska_demux_handle_sink_event));
265
266
267
  gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);

  /* initial stream no. */
268
269
  demux->src = NULL;

270
271
  demux->writing_app = NULL;
  demux->muxing_app = NULL;
272
  demux->index = NULL;
273
  demux->global_tags = NULL;
274

275
276
  demux->adapter = gst_adapter_new ();

277
278
279
280
  /* finish off */
  gst_matroska_demux_reset (GST_ELEMENT (demux));
}

281
282
283
284
285
286
287
288
static void
gst_matroska_track_free (GstMatroskaTrackContext * track)
{
  g_free (track->codec_id);
  g_free (track->codec_name);
  g_free (track->name);
  g_free (track->language);
  g_free (track->codec_priv);
289
  g_free (track->codec_state);
290
291
292
293
294
295
296
297
298
299
300
301
302
303

  if (track->encodings != NULL) {
    int i;

    for (i = 0; i < track->encodings->len; ++i) {
      GstMatroskaTrackEncoding *enc = &g_array_index (track->encodings,
          GstMatroskaTrackEncoding,
          i);

      g_free (enc->comp_settings);
    }
    g_array_free (track->encodings, TRUE);
  }

304
305
306
  if (track->pending_tags)
    gst_tag_list_free (track->pending_tags);

307
308
309
  if (track->index_table)
    g_array_free (track->index_table, TRUE);

310
311
312
  g_free (track);
}

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * Returns the aggregated GstFlowReturn.
 */
static GstFlowReturn
gst_matroska_demux_combine_flows (GstMatroskaDemux * demux,
    GstMatroskaTrackContext * track, GstFlowReturn ret)
{
  guint i;

  /* store the value */
  track->last_flow = ret;

  /* any other error that is not-linked can be returned right away */
  if (ret != GST_FLOW_NOT_LINKED)
    goto done;

  /* only return NOT_LINKED if all other pads returned NOT_LINKED */
330
331
332
  g_assert (demux->src->len == demux->num_streams);
  for (i = 0; i < demux->src->len; i++) {
    GstMatroskaTrackContext *ostream = g_ptr_array_index (demux->src, i);
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

    if (ostream == NULL)
      continue;

    ret = ostream->last_flow;
    /* some other return value (must be SUCCESS but we can return
     * other values as well) */
    if (ret != GST_FLOW_NOT_LINKED)
      goto done;
  }
  /* if we get here, all other pads were unlinked and we return
   * NOT_LINKED then */
done:
  GST_LOG_OBJECT (demux, "combined return %s", gst_flow_get_name (ret));
  return ret;
}

350
351
352
353
354
355
static void
gst_matroska_demux_free_parsed_el (gpointer mem, gpointer user_data)
{
  g_slice_free (guint64, mem);
}

356
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
357
gst_matroska_demux_reset (GstElement * element)
358
359
360
361
{
  GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
  guint i;

362
363
  GST_DEBUG_OBJECT (demux, "Resetting state");

364
365
366
367
  /* reset input */
  demux->state = GST_MATROSKA_DEMUX_STATE_START;

  /* clean up existing streams */
368
369
370
371
372
373
374
375
376
377
  if (demux->src) {
    g_assert (demux->src->len == demux->num_streams);
    for (i = 0; i < demux->src->len; i++) {
      GstMatroskaTrackContext *context = g_ptr_array_index (demux->src, i);

      if (context->pad != NULL)
        gst_element_remove_pad (GST_ELEMENT (demux), context->pad);

      gst_caps_replace (&context->caps, NULL);
      gst_matroska_track_free (context);
378
    }
379
    g_ptr_array_free (demux->src, TRUE);
380
  }
381
382
  demux->src = g_ptr_array_new ();

383
384
385
386
387
388
389
390
391
392
393
394
  demux->num_streams = 0;
  demux->num_a_streams = 0;
  demux->num_t_streams = 0;
  demux->num_v_streams = 0;

  /* reset media info */
  g_free (demux->writing_app);
  demux->writing_app = NULL;
  g_free (demux->muxing_app);
  demux->muxing_app = NULL;

  /* reset indexes */
395
396
397
398
  if (demux->index) {
    g_array_free (demux->index, TRUE);
    demux->index = NULL;
  }
399
400

  /* reset timers */
401
  demux->clock = NULL;
402
403
  demux->time_scale = 1000000;
  demux->created = G_MININT64;
404
405

  demux->index_parsed = FALSE;
406
407
  demux->tracks_parsed = FALSE;
  demux->segmentinfo_parsed = FALSE;
408
  demux->attachments_parsed = FALSE;
409

410
411
  g_list_foreach (demux->tags_parsed,
      (GFunc) gst_matroska_demux_free_parsed_el, NULL);
412
413
414
  g_list_free (demux->tags_parsed);
  demux->tags_parsed = NULL;

415
416
417
418
419
  g_list_foreach (demux->seek_parsed,
      (GFunc) gst_matroska_demux_free_parsed_el, NULL);
  g_list_free (demux->seek_parsed);
  demux->seek_parsed = NULL;

420
  gst_segment_init (&demux->segment, GST_FORMAT_TIME);
421
  demux->last_stop_end = GST_CLOCK_TIME_NONE;
422
  demux->seek_block = 0;
423

424
425
426
  demux->offset = 0;
  demux->cluster_time = GST_CLOCK_TIME_NONE;
  demux->cluster_offset = 0;
427
428
429
430
431
432
433
434
  demux->index_offset = 0;
  demux->seekable = FALSE;
  demux->need_newsegment = FALSE;
  demux->building_index = FALSE;
  if (demux->seek_event) {
    gst_event_unref (demux->seek_event);
    demux->seek_event = NULL;
  }
435

436
437
438
  demux->seek_index = NULL;
  demux->seek_entry = 0;

439
440
441
442
443
444
445
446
447
  if (demux->close_segment) {
    gst_event_unref (demux->close_segment);
    demux->close_segment = NULL;
  }

  if (demux->new_segment) {
    gst_event_unref (demux->new_segment);
    demux->new_segment = NULL;
  }
448
449
450
451
452
453

  if (demux->element_index) {
    gst_object_unref (demux->element_index);
    demux->element_index = NULL;
  }
  demux->element_index_writer_id = -1;
454
455
456
457
458

  if (demux->global_tags) {
    gst_tag_list_free (demux->global_tags);
  }
  demux->global_tags = gst_tag_list_new ();
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572

  if (demux->cached_buffer) {
    gst_buffer_unref (demux->cached_buffer);
    demux->cached_buffer = NULL;
  }
}

/*
 * Calls pull_range for (offset,size) without advancing our offset
 */
static GstFlowReturn
gst_matroska_demux_peek_bytes (GstMatroskaDemux * demux, guint64 offset,
    guint size, GstBuffer ** p_buf, guint8 ** bytes)
{
  GstFlowReturn ret;

  /* Caching here actually makes much less difference than one would expect.
   * We do it mainly to avoid pulling buffers of 1 byte all the time */
  if (demux->cached_buffer) {
    guint64 cache_offset = GST_BUFFER_OFFSET (demux->cached_buffer);
    guint cache_size = GST_BUFFER_SIZE (demux->cached_buffer);

    if (cache_offset <= demux->offset &&
        (demux->offset + size) <= (cache_offset + cache_size)) {
      if (p_buf)
        *p_buf = gst_buffer_create_sub (demux->cached_buffer,
            demux->offset - cache_offset, size);
      if (bytes)
        *bytes = GST_BUFFER_DATA (demux->cached_buffer) + demux->offset -
            cache_offset;
      return GST_FLOW_OK;
    }
    /* not enough data in the cache, free cache and get a new one */
    gst_buffer_unref (demux->cached_buffer);
    demux->cached_buffer = NULL;
  }

  /* refill the cache */
  ret = gst_pad_pull_range (demux->sinkpad, demux->offset,
      MAX (size, 64 * 1024), &demux->cached_buffer);
  if (ret != GST_FLOW_OK) {
    demux->cached_buffer = NULL;
    return ret;
  }

  if (GST_BUFFER_SIZE (demux->cached_buffer) >= size) {
    if (p_buf)
      *p_buf = gst_buffer_create_sub (demux->cached_buffer, 0, size);
    if (bytes)
      *bytes = GST_BUFFER_DATA (demux->cached_buffer);
    return GST_FLOW_OK;
  }

  /* Not possible to get enough data, try a last time with
   * requesting exactly the size we need */
  gst_buffer_unref (demux->cached_buffer);
  demux->cached_buffer = NULL;

  ret =
      gst_pad_pull_range (demux->sinkpad, demux->offset, size,
      &demux->cached_buffer);
  if (ret != GST_FLOW_OK) {
    GST_DEBUG_OBJECT (demux, "pull_range returned %d", ret);
    if (p_buf)
      *p_buf = NULL;
    if (bytes)
      *bytes = NULL;
    return ret;
  }

  if (GST_BUFFER_SIZE (demux->cached_buffer) < size) {
    GST_WARNING_OBJECT (demux, "Dropping short buffer at offset %"
        G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", demux->offset,
        size, GST_BUFFER_SIZE (demux->cached_buffer));

    gst_buffer_unref (demux->cached_buffer);
    demux->cached_buffer = NULL;
    if (p_buf)
      *p_buf = NULL;
    if (bytes)
      *bytes = NULL;
    return GST_FLOW_UNEXPECTED;
  }

  if (p_buf)
    *p_buf = gst_buffer_create_sub (demux->cached_buffer, 0, size);
  if (bytes)
    *bytes = GST_BUFFER_DATA (demux->cached_buffer);

  return GST_FLOW_OK;
}

static const guint8 *
gst_matroska_demux_peek_pull (GstMatroskaDemux * demux, guint peek)
{
  guint8 *data = NULL;

  gst_matroska_demux_peek_bytes (demux, demux->offset, peek, NULL, &data);
  return data;
}

static GstFlowReturn
gst_matroska_demux_peek_id_length_pull (GstMatroskaDemux * demux, guint32 * _id,
    guint64 * _length, guint * _needed)
{
  return gst_ebml_peek_id_length (_id, _length, _needed,
      (GstPeekData) gst_matroska_demux_peek_pull, (gpointer) demux,
      GST_ELEMENT_CAST (demux), demux->offset);
}

static gint64
gst_matroska_demux_get_length (GstMatroskaDemux * demux)
{
  GstFormat fmt = GST_FORMAT_BYTES;
573
  gint64 end = -1;
574
575
576

  if (!gst_pad_query_peer_duration (demux->sinkpad, &fmt, &end) ||
      fmt != GST_FORMAT_BYTES || end < 0)
577
    GST_DEBUG_OBJECT (demux, "no upstream length");
578
579

  return end;
580
581
582
}

static gint
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
583
gst_matroska_demux_stream_from_num (GstMatroskaDemux * demux, guint track_num)
584
585
586
{
  guint n;

587
588
589
590
591
  g_assert (demux->src->len == demux->num_streams);
  for (n = 0; n < demux->src->len; n++) {
    GstMatroskaTrackContext *context = g_ptr_array_index (demux->src, n);

    if (context->num == track_num) {
592
593
594
595
      return n;
    }
  }

596
597
598
  if (n == demux->num_streams)
    GST_WARNING_OBJECT (demux,
        "Failed to find corresponding pad for tracknum %d", track_num);
599
600
601
602

  return -1;
}

603
static gint
604
605
gst_matroska_demux_encoding_cmp (GstMatroskaTrackEncoding * a,
    GstMatroskaTrackEncoding * b)
606
{
607
608
609
610
611
612
613
  if (b->order > a->order)
    return 1;
  else if (b->order < a->order)
    return -1;
  else
    return 0;
}
614

615
616
617
618
static gboolean
gst_matroska_demux_encoding_order_unique (GArray * encodings, guint64 order)
{
  gint i;
619

620
621
  if (encodings == NULL || encodings->len == 0)
    return TRUE;
622

623
624
625
  for (i = 0; i < encodings->len; i++)
    if (g_array_index (encodings, GstMatroskaTrackEncoding, i).order == order)
      return FALSE;
626

627
  return TRUE;
628
629
}

630
static GstFlowReturn
631
gst_matroska_demux_read_track_encoding (GstMatroskaDemux * demux,
632
    GstEbmlRead * ebml, GstMatroskaTrackContext * context)
633
{
634
  GstMatroskaTrackEncoding enc = { 0, };
635
  GstFlowReturn ret;
636
637
  guint32 id;

638
639
640
641
  DEBUG_ELEMENT_START (demux, ebml, "ContentEncoding");
  /* Set default values */
  enc.scope = 1;
  /* All other default values are 0 */
642

643
644
645
646
  if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
    DEBUG_ELEMENT_STOP (demux, ebml, "ContentEncoding", ret);
    return ret;
  }
647

648
649
  while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
    if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
650
651
652
      break;

    switch (id) {
653
654
      case GST_MATROSKA_ID_CONTENTENCODINGORDER:{
        guint64 num;
655

656
657
        if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
          break;
658

659
660
661
662
        if (!gst_matroska_demux_encoding_order_unique (context->encodings, num)) {
          GST_ERROR_OBJECT (demux, "ContentEncodingOrder %" G_GUINT64_FORMAT
              "is not unique for track %d", num, context->num);
          ret = GST_FLOW_ERROR;
663
664
665
          break;
        }

666
667
668
669
670
671
672
673
674
675
676
677
        GST_DEBUG_OBJECT (demux, "ContentEncodingOrder: %" G_GUINT64_FORMAT,
            num);
        enc.order = num;
        break;
      }
      case GST_MATROSKA_ID_CONTENTENCODINGSCOPE:{
        guint64 num;

        if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
          break;

        if (num > 7 && num == 0) {
678
          GST_ERROR_OBJECT (demux, "Invalid ContentEncodingScope %"
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
              G_GUINT64_FORMAT, num);
          ret = GST_FLOW_ERROR;
          break;
        }

        GST_DEBUG_OBJECT (demux, "ContentEncodingScope: %" G_GUINT64_FORMAT,
            num);
        enc.scope = num;

        break;
      }
      case GST_MATROSKA_ID_CONTENTENCODINGTYPE:{
        guint64 num;

        if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
          break;

        if (num > 1) {
          GST_ERROR_OBJECT (demux, "Invalid ContentEncodingType %"
              G_GUINT64_FORMAT, num);
          ret = GST_FLOW_ERROR;
          break;
        } else if (num != 0) {
          GST_ERROR_OBJECT (demux, "Encrypted tracks are not supported yet");
          ret = GST_FLOW_ERROR;
          break;
        }
        GST_DEBUG_OBJECT (demux, "ContentEncodingType: %" G_GUINT64_FORMAT,
            num);
        enc.type = num;
        break;
      }
      case GST_MATROSKA_ID_CONTENTCOMPRESSION:{

        DEBUG_ELEMENT_START (demux, ebml, "ContentCompression");

        if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK)
          break;

718
719
720
        while (ret == GST_FLOW_OK &&
            gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
          if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
721
722
723
            break;

          switch (id) {
724
            case GST_MATROSKA_ID_CONTENTCOMPALGO:{
725
726
              guint64 num;

727
              if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK) {
728
729
                break;
              }
730
731
732
733
              if (num > 3) {
                GST_ERROR_OBJECT (demux, "Invalid ContentCompAlgo %"
                    G_GUINT64_FORMAT, num);
                ret = GST_FLOW_ERROR;
734
735
                break;
              }
736
737
738
739
              GST_DEBUG_OBJECT (demux, "ContentCompAlgo: %" G_GUINT64_FORMAT,
                  num);
              enc.comp_algo = num;

740
741
              break;
            }
742
743
744
            case GST_MATROSKA_ID_CONTENTCOMPSETTINGS:{
              guint8 *data;
              guint64 size;
745

746
747
748
749
              if ((ret =
                      gst_ebml_read_binary (ebml, &id, &data,
                          &size)) != GST_FLOW_OK) {
                break;
750
              }
751
752
753
754
              enc.comp_settings = data;
              enc.comp_settings_length = size;
              GST_DEBUG_OBJECT (demux,
                  "ContentCompSettings of size %" G_GUINT64_FORMAT, size);
755
756
757
              break;
            }
            default:
758
759
              GST_WARNING_OBJECT (demux,
                  "Unknown ContentCompression subelement 0x%x - ignoring", id);
760
              ret = gst_ebml_read_skip (ebml);
761
762
763
              break;
          }
        }
764
        DEBUG_ELEMENT_STOP (demux, ebml, "ContentCompression", ret);
Jan Schmidt's avatar
Jan Schmidt committed
765
        break;
766
767
      }

768
769
770
771
772
      case GST_MATROSKA_ID_CONTENTENCRYPTION:
        GST_ERROR_OBJECT (demux, "Encrypted tracks not yet supported");
        gst_ebml_read_skip (ebml);
        ret = GST_FLOW_ERROR;
        break;
773
      default:
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
        GST_WARNING_OBJECT (demux,
            "Unknown ContentEncoding subelement 0x%x - ignoring", id);
        ret = gst_ebml_read_skip (ebml);
        break;
    }
  }

  DEBUG_ELEMENT_STOP (demux, ebml, "ContentEncoding", ret);
  if (ret != GST_FLOW_OK && ret != GST_FLOW_UNEXPECTED)
    return ret;

  /* TODO: Check if the combination of values is valid */

  g_array_append_val (context->encodings, enc);

  return ret;
}

792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
static gboolean
gst_matroska_decompress_data (GstMatroskaTrackEncoding * enc,
    guint8 ** data_out, guint * size_out,
    GstMatroskaTrackCompressionAlgorithm algo)
{
  guint8 *new_data = NULL;
  guint new_size = 0;
  guint8 *data = *data_out;
  guint size = *size_out;
  gboolean ret = TRUE;

  if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_ZLIB) {
#ifdef HAVE_ZLIB
    /* zlib encoded data */
    z_stream zstream;
    guint orig_size;
    int result;

    orig_size = size;
    zstream.zalloc = (alloc_func) 0;
    zstream.zfree = (free_func) 0;
    zstream.opaque = (voidpf) 0;
    if (inflateInit (&zstream) != Z_OK) {
      GST_WARNING ("zlib initialization failed.");
      ret = FALSE;
      goto out;
    }
    zstream.next_in = (Bytef *) data;
    zstream.avail_in = orig_size;
    new_size = orig_size;
    new_data = g_malloc (new_size);
    zstream.avail_out = new_size;
    zstream.next_out = (Bytef *) new_data;

    do {
      result = inflate (&zstream, Z_NO_FLUSH);
      if (result != Z_OK && result != Z_STREAM_END) {
        GST_WARNING ("zlib decompression failed.");
        g_free (new_data);
        inflateEnd (&zstream);
        break;
      }
      new_size += 4000;
      new_data = g_realloc (new_data, new_size);
      zstream.next_out = (Bytef *) (new_data + zstream.total_out);
      zstream.avail_out += 4000;
    } while (zstream.avail_in != 0 && result != Z_STREAM_END);

    if (result != Z_STREAM_END) {
      ret = FALSE;
      goto out;
    } else {
      new_size = zstream.total_out;
      inflateEnd (&zstream);
    }
#else
    GST_WARNING ("zlib encoded tracks not supported.");
    ret = FALSE;
    goto out;
#endif
  } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_BZLIB) {
#ifdef HAVE_BZ2
    /* bzip2 encoded data */
    bz_stream bzstream;
    guint orig_size;
    int result;

    bzstream.bzalloc = NULL;
    bzstream.bzfree = NULL;
    bzstream.opaque = NULL;
    orig_size = size;

864
    if (BZ2_bzDecompressInit (&bzstream, 0, 0) != BZ_OK) {
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
      GST_WARNING ("bzip2 initialization failed.");
      ret = FALSE;
      goto out;
    }

    bzstream.next_in = (char *) data;
    bzstream.avail_in = orig_size;
    new_size = orig_size;
    new_data = g_malloc (new_size);
    bzstream.avail_out = new_size;
    bzstream.next_out = (char *) new_data;

    do {
      result = BZ2_bzDecompress (&bzstream);
      if (result != BZ_OK && result != BZ_STREAM_END) {
        GST_WARNING ("bzip2 decompression failed.");
        g_free (new_data);
        BZ2_bzDecompressEnd (&bzstream);
        break;
      }
      new_size += 4000;
      new_data = g_realloc (new_data, new_size);
      bzstream.next_out = (char *) (new_data + bzstream.total_out_lo32);
      bzstream.avail_out += 4000;
    } while (bzstream.avail_in != 0 && result != BZ_STREAM_END);

    if (result != BZ_STREAM_END) {
      ret = FALSE;
      goto out;
    } else {
      new_size = bzstream.total_out_lo32;
      BZ2_bzDecompressEnd (&bzstream);
    }
#else
    GST_WARNING ("bzip2 encoded tracks not supported.");
    ret = FALSE;
    goto out;
#endif
  } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_LZO1X) {
    /* lzo encoded data */
    int result;
    int orig_size, out_size;

    orig_size = size;
    out_size = size;
    new_size = size;
    new_data = g_malloc (new_size);

    do {
      orig_size = size;
      out_size = new_size;

      result = lzo1x_decode (new_data, &out_size, data, &orig_size);

      if (orig_size > 0) {
        new_size += 4000;
        new_data = g_realloc (new_data, new_size);
      }
    } while (orig_size > 0 && result == LZO_OUTPUT_FULL);

    new_size -= out_size;

    if (result != LZO_OUTPUT_FULL) {
      GST_WARNING ("lzo decompression failed");
      g_free (new_data);

      ret = FALSE;
      goto out;
    }

  } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_HEADERSTRIP) {
    /* header stripped encoded data */
    if (enc->comp_settings_length > 0) {
      new_data = g_malloc (size + enc->comp_settings_length);
      new_size = size + enc->comp_settings_length;

      memcpy (new_data, enc->comp_settings, enc->comp_settings_length);
      memcpy (new_data + enc->comp_settings_length, data, size);
    }
  } else {
945
946
    GST_ERROR ("invalid compression algorithm %d", algo);
    ret = FALSE;
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
  }

out:

  if (!ret) {
    *data_out = NULL;
    *size_out = 0;
  } else {
    *data_out = new_data;
    *size_out = new_size;
  }

  return ret;
}

static gboolean
gst_matroska_decode_data (GArray * encodings, guint8 ** data_out,
    guint * size_out, GstMatroskaTrackEncodingScope scope, gboolean free)
{
  guint8 *data;
  guint size;
  gboolean ret = TRUE;
  gint i;

  g_return_val_if_fail (encodings != NULL, FALSE);
  g_return_val_if_fail (data_out != NULL && *data_out != NULL, FALSE);
  g_return_val_if_fail (size_out != NULL, FALSE);

  data = *data_out;
  size = *size_out;

  for (i = 0; i < encodings->len; i++) {
    GstMatroskaTrackEncoding *enc =
        &g_array_index (encodings, GstMatroskaTrackEncoding, i);
    guint8 *new_data = NULL;
    guint new_size = 0;

    if ((enc->scope & scope) == 0)
      continue;

    /* Encryption not supported yet */
    if (enc->type != 0) {
      ret = FALSE;
      break;
    }

    new_data = data;
    new_size = size;

    ret =
        gst_matroska_decompress_data (enc, &new_data, &new_size,
        enc->comp_algo);

    if (!ret)
      break;

    if ((data == *data_out && free) || (data != *data_out))
      g_free (data);

    data = new_data;
    size = new_size;
  }

  if (!ret) {
    if ((data == *data_out && free) || (data != *data_out))
      g_free (data);

    *data_out = NULL;
    *size_out = 0;
  } else {
    *data_out = data;
    *size_out = size;
  }

  return ret;
}

static GstBuffer *
gst_matroska_decode_buffer (GstMatroskaTrackContext * context, GstBuffer * buf)
{
  guint8 *data;
  guint size;
  GstBuffer *new_buf;

  g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);

1033
1034
  GST_DEBUG ("decoding buffer %p", buf);

1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
  data = GST_BUFFER_DATA (buf);
  size = GST_BUFFER_SIZE (buf);

  g_return_val_if_fail (data != NULL && size > 0, buf);

  if (gst_matroska_decode_data (context->encodings, &data, &size,
          GST_MATROSKA_TRACK_ENCODING_SCOPE_FRAME, FALSE)) {
    new_buf = gst_buffer_new ();
    GST_BUFFER_MALLOCDATA (new_buf) = (guint8 *) data;
    GST_BUFFER_DATA (new_buf) = (guint8 *) data;
    GST_BUFFER_SIZE (new_buf) = size;

    gst_buffer_unref (buf);
    buf = new_buf;

    return buf;
  } else {
1052
    GST_DEBUG ("decode data failed");
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
    gst_buffer_unref (buf);
    return NULL;
  }
}

static GstFlowReturn
gst_matroska_decode_content_encodings (GArray * encodings)
{
  gint i;

  if (encodings == NULL)
    return GST_FLOW_OK;

  for (i = 0; i < encodings->len; i++) {
    GstMatroskaTrackEncoding *enc =
        &g_array_index (encodings, GstMatroskaTrackEncoding, i);
    GstMatroskaTrackEncoding *enc2;
    guint8 *data = NULL;
    guint size;

1073
1074
    if ((enc->scope & GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING)
        == 0)
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
      continue;

    /* Encryption not supported yet */
    if (enc->type != 0)
      return GST_FLOW_ERROR;

    if (i + 1 >= encodings->len)
      return GST_FLOW_ERROR;

    enc2 = &g_array_index (encodings, GstMatroskaTrackEncoding, i + 1);

    if (enc->comp_settings_length == 0)
      continue;

    data = enc->comp_settings;
    size = enc->comp_settings_length;

    if (!gst_matroska_decompress_data (enc, &data, &size, enc->comp_algo))
      return GST_FLOW_ERROR;

    g_free (enc->comp_settings);

    enc->comp_settings = data;
    enc->comp_settings_length = size;
  }

  return GST_FLOW_OK;
}

1104
1105
static GstFlowReturn
gst_matroska_demux_read_track_encodings (GstMatroskaDemux * demux,
1106
    GstEbmlRead * ebml, GstMatroskaTrackContext * context)
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
{
  GstFlowReturn ret;
  guint32 id;

  DEBUG_ELEMENT_START (demux, ebml, "ContentEncodings");

  if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
    DEBUG_ELEMENT_STOP (demux, ebml, "ContentEncodings", ret);
    return ret;
  }

  context->encodings =
      g_array_sized_new (FALSE, FALSE, sizeof (GstMatroskaTrackEncoding), 1);

1121
1122
  while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
    if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
1123
1124
1125
1126
      break;

    switch (id) {
      case GST_MATROSKA_ID_CONTENTENCODING:
1127
        ret = gst_matroska_demux_read_track_encoding (demux, ebml, context);
1128
1129
1130
1131
        break;
      default:
        GST_WARNING_OBJECT (demux,
            "Unknown ContentEncodings subelement 0x%x - ignoring", id);
1132
        ret = gst_ebml_read_skip (ebml);
1133
1134
1135
1136
        break;
    }
  }

1137
1138
1139
1140
  DEBUG_ELEMENT_STOP (demux, ebml, "ContentEncodings", ret);
  if (ret != GST_FLOW_OK && ret != GST_FLOW_UNEXPECTED)
    return ret;

1141
  /* Sort encodings according to their order */
1142
1143
1144
  g_array_sort (context->encodings,
      (GCompareFunc) gst_matroska_demux_encoding_cmp);

1145
  return gst_matroska_decode_content_encodings (context->encodings);
1146
1147
}

1148
1149
1150
1151
1152
static gboolean
gst_matroska_demux_tracknumber_unique (GstMatroskaDemux * demux, guint64 num)
{
  gint i;

1153
1154
1155
1156
1157
  g_assert (demux->src->len == demux->num_streams);
  for (i = 0; i < demux->src->len; i++) {
    GstMatroskaTrackContext *context = g_ptr_array_index (demux->src, i);

    if (context->num == num)
1158
      return FALSE;
1159
  }
1160
1161
1162
1163

  return TRUE;
}

1164
static GstFlowReturn
1165
gst_matroska_demux_add_stream (GstMatroskaDemux * demux, GstEbmlRead * ebml)
1166
1167
1168
1169
1170
1171
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
  GstMatroskaTrackContext *context;
  GstPadTemplate *templ = NULL;
  GstCaps *caps = NULL;
  gchar *padname = NULL;
1172
  GstFlowReturn ret;
1173
  guint32 id;
1174
1175
  GstTagList *list = NULL;
  gchar *codec = NULL;
1176

1177
1178
1179
1180
1181
1182
1183
1184
  DEBUG_ELEMENT_START (demux, ebml, "TrackEntry");

  /* start with the master */
  if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
    DEBUG_ELEMENT_STOP (demux, ebml, "TrackEntry", ret);
    return ret;
  }

1185
1186
1187
  /* allocate generic... if we know the type, we'll g_renew()
   * with the precise type */
  context = g_new0 (GstMatroskaTrackContext, 1);
1188
  g_ptr_array_add (demux->src, context);
1189
  context->index = demux->num_streams;
1190
  context->index_writer_id = -1;
1191
  context->type = 0;            /* no type yet */
1192
  context->default_duration = 0;
1193
  context->pos = 0;
1194
  context->set_discont = TRUE;
1195
1196
1197
1198
  context->timecodescale = 1.0;
  context->flags =
      GST_MATROSKA_TRACK_ENABLED | GST_MATROSKA_TRACK_DEFAULT |
      GST_MATROSKA_TRACK_LACING;
1199
  context->last_flow = GST_FLOW_OK;
1200
  context->to_offset = G_MAXINT64;
1201
  demux->num_streams++;
1202
  g_assert (demux->src->len == demux->num_streams);
1203

1204
  GST_DEBUG_OBJECT (demux, "Stream number %d", context->index);
1205
1206

  /* try reading the trackentry headers */
1207
1208
  while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
    if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
1209
      break;
1210

1211
    switch (id) {
1212
        /* track number (unique stream ID) */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1213
      case GST_MATROSKA_ID_TRACKNUMBER:{
1214
1215
        guint64 num;

1216
        if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
1217
          break;
1218

1219
        if (num == 0) {
1220
1221
1222
1223
1224
1225
          GST_ERROR_OBJECT (demux, "Invalid TrackNumber 0");
          ret = GST_FLOW_ERROR;
          break;
        } else if (!gst_matroska_demux_tracknumber_unique (demux, num)) {
          GST_ERROR_OBJECT (demux, "TrackNumber %" G_GUINT64_FORMAT
              " is not unique", num);
1226
1227
1228
1229
          ret = GST_FLOW_ERROR;
          break;
        }

1230
        GST_DEBUG_OBJECT (demux, "TrackNumber: %" G_GUINT64_FORMAT, num);
1231
1232
        context->num = num;
        break;
1233
      }
1234
        /* track UID (unique identifier) */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1235
      case GST_MATROSKA_ID_TRACKUID:{
1236
1237
        guint64 num;

1238
        if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
1239
          break;
1240

1241
        if (num == 0) {
1242
          GST_ERROR_OBJECT (demux, "Invalid TrackUID 0");
1243
1244
1245
1246
          ret = GST_FLOW_ERROR;
          break;
        }

1247
        GST_DEBUG_OBJECT (demux, "TrackUID: %" G_GUINT64_FORMAT, num);
1248
1249
        context->uid = num;
        break;
1250
1251
      }

1252
        /* track type (video, audio, combined, subtitle, etc.) */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1253
      case GST_MATROSKA_ID_TRACKTYPE:{
1254
        guint64 track_type;
1255

1256
        if ((ret = gst_ebml_read_uint (ebml, &id, &track_type)) != GST_FLOW_OK) {
1257
1258
          break;
        }
1259
1260

        if (context->type != 0 && context->type != track_type) {
1261
1262
          GST_WARNING_OBJECT (demux,
              "More than one tracktype defined in a TrackEntry - skipping");
1263
          break;
1264
        } else if (track_type < 1 || track_type > 254) {
1265
1266
          GST_WARNING_OBJECT (demux, "Invalid TrackType %" G_GUINT64_FORMAT,
              track_type);
1267
          break;
1268
1269
        }

1270
1271
        GST_DEBUG_OBJECT (demux, "TrackType: %" G_GUINT64_FORMAT, track_type);

1272
        /* ok, so we're actually going to reallocate this thing */
1273
        switch (track_type) {
1274
          case GST_MATROSKA_TRACK_TYPE_VIDEO:
1275
            gst_matroska_track_init_video_context (&context);
1276
1277
            break;
          case GST_MATROSKA_TRACK_TYPE_AUDIO:
1278
            gst_matroska_track_init_audio_context (&context);
1279
1280
            break;
          case GST_MATROSKA_TRACK_TYPE_SUBTITLE:
1281
            gst_matroska_track_init_subtitle_context (&context);
1282
            break;
1283
          case GST_MATROSKA_TRACK_TYPE_COMPLEX:
1284
          case GST_MATROSKA_TRACK_TYPE_LOGO:
1285
          case GST_MATROSKA_TRACK_TYPE_BUTTONS:
1286
1287
          case GST_MATROSKA_TRACK_TYPE_CONTROL:
          default:
1288
1289
1290
            GST_WARNING_OBJECT (demux,
                "Unknown or unsupported TrackType %" G_GUINT64_FORMAT,
                track_type);
1291
1292
1293
            context->type = 0;
            break;
        }
1294
        g_ptr_array_index (demux->src, demux->num_streams - 1) = context;
1295
        break;
1296
1297
      }

1298
        /* tracktype specific stuff for video */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1299
      case GST_MATROSKA_ID_TRACKVIDEO:{
1300
1301
        GstMatroskaTrackVideoContext *videocontext;

1302
1303
        DEBUG_ELEMENT_START (demux, ebml, "TrackVideo");

1304
        if (!gst_matroska_track_init_video_context (&context)) {
1305
1306
          GST_WARNING_OBJECT (demux,
              "TrackVideo element in non-video track - ignoring track");
1307
          ret = GST_FLOW_ERROR;
1308
          break;
1309
        } else if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
1310
1311
1312
          break;
        }
        videocontext = (GstMatroskaTrackVideoContext *) context;
1313
        g_ptr_array_index (demux->src, demux->num_streams - 1) = context;
1314

1315
1316
1317
        while (ret == GST_FLOW_OK &&
            gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
          if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
1318
1319
1320
            break;

          switch (id) {
1321
              /* Should be one level up but some broken muxers write it here. */
1322
1323
1324
            case GST_MATROSKA_ID_TRACKDEFAULTDURATION:{
              guint64 num;

1325
              if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
1326
                break;
1327
1328

              if (num == 0) {
1329
                GST_WARNING_OBJECT (demux, "Invalid TrackDefaultDuration 0");
1330
1331
1332
                break;
              }

1333
1334
              GST_DEBUG_OBJECT (