matroska-demux.c 190 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
53
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>
#include <string.h>

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

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

#include <gst/base/gsttypefindhelper.h>

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

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

#include "lzo.h"

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

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

80
81
82
83
84
85
86
87
88
#define DEBUG_ELEMENT_START(demux, ebml, element) \
    GST_DEBUG_OBJECT (demux, "Parsing " element " element at offset %" \
        G_GUINT64_FORMAT, ebml->offset)

#define DEBUG_ELEMENT_STOP(demux, ebml, element, ret) \
    GST_DEBUG_OBJECT (demux, "Parsing " element " element at offset %" \
        G_GUINT64_FORMAT " finished with '%s'", ebml->offset, \
	gst_flow_get_name (ret))

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
99
100
static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-matroska")
    );
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
static GstFlowReturn gst_matroska_demux_parse_contents (GstMatroskaDemux *
128
    demux);
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
179
GST_BOILERPLATE (GstMatroskaDemux, gst_matroska_demux, GstEbmlRead,
    GST_TYPE_EBML_READ);
180
181

static void
182
gst_matroska_demux_base_init (gpointer klass)
183
184
185
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

186
187
188
189
190
191
  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));
192
  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
193
      gst_static_pad_template_get (&sink_templ));
194

195
196
197
  gst_element_class_set_details_simple (element_class, "Matroska demuxer",
      "Codec/Demuxer",
      "Demuxes a Matroska Stream into video/audio/subtitles",
198
      "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
199
200
}

201
202
203
204
205
206
207
208
209
210
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;
  }

211
212
213
214
215
  if (demux->global_tags) {
    gst_tag_list_free (demux->global_tags);
    demux->global_tags = NULL;
  }

216
217
  g_object_unref (demux->adapter);

218
219
220
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

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

227
228
  GST_DEBUG_CATEGORY_INIT (matroskademux_debug, "matroskademux", 0,
      "Matroska demuxer");
229

230
231
  gobject_class->finalize = gst_matroska_demux_finalize;

232
233
234
235
  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);
236
237
  gstelement_class->query =
      GST_DEBUG_FUNCPTR (gst_matroska_demux_element_query);
238
239
240
241
242

  gstelement_class->set_index =
      GST_DEBUG_FUNCPTR (gst_matroska_demux_set_index);
  gstelement_class->get_index =
      GST_DEBUG_FUNCPTR (gst_matroska_demux_get_index);
243
244
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
245
static void
246
247
gst_matroska_demux_init (GstMatroskaDemux * demux,
    GstMatroskaDemuxClass * klass)
248
{
249
  demux->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
250
251
252
253
  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));
254
255
256
257
  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));
258
259
260
261
  gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
  GST_EBML_READ (demux)->sinkpad = demux->sinkpad;

  /* initial stream no. */
262
263
  demux->src = NULL;

264
265
  demux->writing_app = NULL;
  demux->muxing_app = NULL;
266
  demux->index = NULL;
267
  demux->global_tags = NULL;
268

269
270
  demux->adapter = gst_adapter_new ();

271
272
273
274
  /* finish off */
  gst_matroska_demux_reset (GST_ELEMENT (demux));
}

275
276
277
278
279
280
281
282
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);
283
  g_free (track->codec_state);
284
285
286
287
288
289
290
291
292
293
294
295
296
297

  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);
  }

298
299
300
  if (track->pending_tags)
    gst_tag_list_free (track->pending_tags);

301
302
303
  if (track->index_table)
    g_array_free (track->index_table, TRUE);

304
305
306
  g_free (track);
}

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * 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 */
324
325
326
  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);
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

    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;
}

344
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
345
gst_matroska_demux_reset (GstElement * element)
346
347
348
349
{
  GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
  guint i;

350
351
  GST_DEBUG_OBJECT (demux, "Resetting state");

352
353
354
355
  /* reset input */
  demux->state = GST_MATROSKA_DEMUX_STATE_START;

  /* clean up existing streams */
356
357
358
359
360
361
362
363
364
365
  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);
366
    }
367
    g_ptr_array_free (demux->src, TRUE);
368
  }
369
370
  demux->src = g_ptr_array_new ();

371
372
373
374
375
376
377
378
379
380
381
382
  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 */
383
384
385
386
  if (demux->index) {
    g_array_free (demux->index, TRUE);
    demux->index = NULL;
  }
387
388

  /* reset timers */
389
  demux->clock = NULL;
390
391
  demux->time_scale = 1000000;
  demux->created = G_MININT64;
392
393

  demux->index_parsed = FALSE;
394
395
  demux->tracks_parsed = FALSE;
  demux->segmentinfo_parsed = FALSE;
396
  demux->attachments_parsed = FALSE;
397

398
399
400
401
  g_list_foreach (demux->tags_parsed, (GFunc) gst_ebml_level_free, NULL);
  g_list_free (demux->tags_parsed);
  demux->tags_parsed = NULL;

402
  gst_segment_init (&demux->segment, GST_FORMAT_TIME);
403
  demux->duration = -1;
404
  demux->last_stop_end = GST_CLOCK_TIME_NONE;
405
  demux->seek_block = 0;
406

407
408
409
410
  demux->offset = 0;
  demux->cluster_time = GST_CLOCK_TIME_NONE;
  demux->cluster_offset = 0;

411
412
413
414
415
416
417
418
419
  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;
  }
420
421
422
423
424
425

  if (demux->element_index) {
    gst_object_unref (demux->element_index);
    demux->element_index = NULL;
  }
  demux->element_index_writer_id = -1;
426
427
428
429
430

  if (demux->global_tags) {
    gst_tag_list_free (demux->global_tags);
  }
  demux->global_tags = gst_tag_list_new ();
431
432
433
}

static gint
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
434
gst_matroska_demux_stream_from_num (GstMatroskaDemux * demux, guint track_num)
435
436
437
{
  guint n;

438
439
440
441
442
  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) {
443
444
445
446
      return n;
    }
  }

447
448
449
  if (n == demux->num_streams)
    GST_WARNING_OBJECT (demux,
        "Failed to find corresponding pad for tracknum %d", track_num);
450
451
452
453

  return -1;
}

454
static gint
455
456
gst_matroska_demux_encoding_cmp (GstMatroskaTrackEncoding * a,
    GstMatroskaTrackEncoding * b)
457
{
458
459
460
461
462
463
464
  if (b->order > a->order)
    return 1;
  else if (b->order < a->order)
    return -1;
  else
    return 0;
}
465

466
467
468
469
static gboolean
gst_matroska_demux_encoding_order_unique (GArray * encodings, guint64 order)
{
  gint i;
470

471
472
  if (encodings == NULL || encodings->len == 0)
    return TRUE;
473

474
475
476
  for (i = 0; i < encodings->len; i++)
    if (g_array_index (encodings, GstMatroskaTrackEncoding, i).order == order)
      return FALSE;
477

478
  return TRUE;
479
480
}

481
static GstFlowReturn
482
483
gst_matroska_demux_read_track_encoding (GstMatroskaDemux * demux,
    GstMatroskaTrackContext * context)
484
{
485
486
  GstMatroskaTrackEncoding enc = { 0, };
  GstEbmlRead *ebml = GST_EBML_READ (demux);
487
  GstFlowReturn ret;
488
489
  guint32 id;

490
491
492
493
  DEBUG_ELEMENT_START (demux, ebml, "ContentEncoding");
  /* Set default values */
  enc.scope = 1;
  /* All other default values are 0 */
494

495
496
497
498
  if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
    DEBUG_ELEMENT_STOP (demux, ebml, "ContentEncoding", ret);
    return ret;
  }
499

500
  while (ret == GST_FLOW_OK) {
501
    if ((ret = gst_ebml_peek_id (ebml, &demux->level_up, &id)) != GST_FLOW_OK)
502
      break;
503
504

    if (demux->level_up) {
505
506
507
508
509
      demux->level_up--;
      break;
    }

    switch (id) {
510
511
      case GST_MATROSKA_ID_CONTENTENCODINGORDER:{
        guint64 num;
512

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

516
517
518
519
        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;
520
521
522
          break;
        }

523
524
525
526
527
528
529
530
531
532
533
534
        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) {
535
          GST_ERROR_OBJECT (demux, "Invalid ContentEncodingScope %"
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
573
574
              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;

575
        while (ret == GST_FLOW_OK) {
576
577
          if ((ret = gst_ebml_peek_id (ebml, &demux->level_up,
                      &id)) != GST_FLOW_OK)
578
            break;
579
580

          if (demux->level_up) {
581
582
583
584
585
            demux->level_up--;
            break;
          }

          switch (id) {
586
            case GST_MATROSKA_ID_CONTENTCOMPALGO:{
587
588
              guint64 num;

589
              if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK) {
590
591
                break;
              }
592
593
594
595
              if (num > 3) {
                GST_ERROR_OBJECT (demux, "Invalid ContentCompAlgo %"
                    G_GUINT64_FORMAT, num);
                ret = GST_FLOW_ERROR;
596
597
                break;
              }
598
599
600
601
              GST_DEBUG_OBJECT (demux, "ContentCompAlgo: %" G_GUINT64_FORMAT,
                  num);
              enc.comp_algo = num;

602
603
              break;
            }
604
605
606
            case GST_MATROSKA_ID_CONTENTCOMPSETTINGS:{
              guint8 *data;
              guint64 size;
607

608
609
610
611
              if ((ret =
                      gst_ebml_read_binary (ebml, &id, &data,
                          &size)) != GST_FLOW_OK) {
                break;
612
              }
613
614
615
616
              enc.comp_settings = data;
              enc.comp_settings_length = size;
              GST_DEBUG_OBJECT (demux,
                  "ContentCompSettings of size %" G_GUINT64_FORMAT, size);
617
618
619
              break;
            }
            default:
620
621
              GST_WARNING_OBJECT (demux,
                  "Unknown ContentCompression subelement 0x%x - ignoring", id);
622
              ret = gst_ebml_read_skip (ebml);
623
624
625
626
627
628
629
630
              break;
          }

          if (demux->level_up) {
            demux->level_up--;
            break;
          }
        }
631
        DEBUG_ELEMENT_STOP (demux, ebml, "ContentCompression", ret);
Jan Schmidt's avatar
Jan Schmidt committed
632
        break;
633
634
      }

635
636
637
638
639
      case GST_MATROSKA_ID_CONTENTENCRYPTION:
        GST_ERROR_OBJECT (demux, "Encrypted tracks not yet supported");
        gst_ebml_read_skip (ebml);
        ret = GST_FLOW_ERROR;
        break;
640
      default:
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
        GST_WARNING_OBJECT (demux,
            "Unknown ContentEncoding subelement 0x%x - ignoring", id);
        ret = gst_ebml_read_skip (ebml);
        break;
    }

    if (demux->level_up) {
      demux->level_up--;
      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;
}

664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
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;

738
    if (BZ2_bzDecompressInit (&bzstream, 0, 0) != BZ_OK) {
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
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
864
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
      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 {
    g_assert_not_reached ();
  }

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);

906
907
  GST_DEBUG ("decoding buffer %p", buf);

908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
  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 {
925
    GST_DEBUG ("decode data failed");
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
    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;

946
947
    if ((enc->scope & GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING)
        == 0)
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
      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;
}

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
static GstFlowReturn
gst_matroska_demux_read_track_encodings (GstMatroskaDemux * demux,
    GstMatroskaTrackContext * context)
{
  GstFlowReturn ret;
  GstEbmlRead *ebml = GST_EBML_READ (demux);
  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);

  while (ret == GST_FLOW_OK) {
    if ((ret = gst_ebml_peek_id (ebml, &demux->level_up, &id)) != GST_FLOW_OK)
      break;

    if (demux->level_up) {
      demux->level_up--;
      break;
    }

    switch (id) {
      case GST_MATROSKA_ID_CONTENTENCODING:
        ret = gst_matroska_demux_read_track_encoding (demux, context);
        break;
      default:
        GST_WARNING_OBJECT (demux,
            "Unknown ContentEncodings subelement 0x%x - ignoring", id);
1011
        ret = gst_ebml_read_skip (ebml);
1012
1013
1014
1015
1016
1017
1018
1019
1020
        break;
    }

    if (demux->level_up) {
      demux->level_up--;
      break;
    }
  }

1021
1022
1023
1024
  DEBUG_ELEMENT_STOP (demux, ebml, "ContentEncodings", ret);
  if (ret != GST_FLOW_OK && ret != GST_FLOW_UNEXPECTED)
    return ret;

1025
  /* Sort encodings according to their order */
1026
1027
1028
  g_array_sort (context->encodings,
      (GCompareFunc) gst_matroska_demux_encoding_cmp);

1029
  return gst_matroska_decode_content_encodings (context->encodings);
1030
1031
}

1032
1033
1034
1035
1036
static gboolean
gst_matroska_demux_tracknumber_unique (GstMatroskaDemux * demux, guint64 num)
{
  gint i;

1037
1038
1039
1040
1041
  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)
1042
      return FALSE;
1043
  }
1044
1045
1046
1047

  return TRUE;
}

1048
static GstFlowReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1049
gst_matroska_demux_add_stream (GstMatroskaDemux * demux)
1050
1051
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
1052
  GstEbmlRead *ebml = GST_EBML_READ (demux);
1053
1054
1055
1056
  GstMatroskaTrackContext *context;
  GstPadTemplate *templ = NULL;
  GstCaps *caps = NULL;
  gchar *padname = NULL;
1057
  GstFlowReturn ret;
1058
  guint32 id;
1059
1060
  GstTagList *list = NULL;
  gchar *codec = NULL;
1061

1062
1063
1064
1065
1066
1067
1068
1069
  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;
  }

1070
1071
1072
  /* allocate generic... if we know the type, we'll g_renew()
   * with the precise type */
  context = g_new0 (GstMatroskaTrackContext, 1);
1073
  g_ptr_array_add (demux->src, context);
1074
  context->index = demux->num_streams;
1075
  context->index_writer_id = -1;
1076
  context->type = 0;            /* no type yet */
1077
  context->default_duration = 0;
1078
  context->pos = 0;
1079
  context->set_discont = TRUE;
1080
1081
1082
1083
  context->timecodescale = 1.0;
  context->flags =
      GST_MATROSKA_TRACK_ENABLED | GST_MATROSKA_TRACK_DEFAULT |
      GST_MATROSKA_TRACK_LACING;
1084
  context->last_flow = GST_FLOW_OK;
1085
  demux->num_streams++;
1086
  g_assert (demux->src->len == demux->num_streams);
1087

1088
  GST_DEBUG_OBJECT (demux, "Stream number %d", context->index);
1089
1090

  /* try reading the trackentry headers */
1091
  while (ret == GST_FLOW_OK) {
1092
    if ((ret = gst_ebml_peek_id (ebml, &demux->level_up, &id)) != GST_FLOW_OK)
1093
      break;
1094
1095

    if (demux->level_up) {
1096
1097
1098
1099
1100
      demux->level_up--;
      break;
    }

    switch (id) {
1101
        /* track number (unique stream ID) */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1102
      case GST_MATROSKA_ID_TRACKNUMBER:{
1103
1104
        guint64 num;

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

1108
        if (num == 0) {
1109
1110
1111
1112
1113
1114
          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);
1115
1116
1117
1118
          ret = GST_FLOW_ERROR;
          break;
        }

1119
        GST_DEBUG_OBJECT (demux, "TrackNumber: %" G_GUINT64_FORMAT, num);
1120
1121
        context->num = num;
        break;
1122
      }
1123
        /* track UID (unique identifier) */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1124
      case GST_MATROSKA_ID_TRACKUID:{
1125
1126
        guint64 num;

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

1130
        if (num == 0) {
1131
          GST_ERROR_OBJECT (demux, "Invalid TrackUID 0");
1132
1133
1134
1135
          ret = GST_FLOW_ERROR;
          break;
        }

1136
        GST_DEBUG_OBJECT (demux, "TrackUID: %" G_GUINT64_FORMAT, num);
1137
1138
        context->uid = num;
        break;
1139
1140
      }

1141
        /* track type (video, audio, combined, subtitle, etc.) */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1142
      case GST_MATROSKA_ID_TRACKTYPE:{
1143
        guint64 track_type;
1144

1145
        if ((ret = gst_ebml_read_uint (ebml, &id, &track_type)) != GST_FLOW_OK) {
1146
1147
          break;
        }
1148
1149

        if (context->type != 0 && context->type != track_type) {
1150
1151
          GST_WARNING_OBJECT (demux,
              "More than one tracktype defined in a TrackEntry - skipping");
1152
          break;
1153
        } else if (track_type < 1 || track_type > 254) {
1154
1155
          GST_WARNING_OBJECT (demux, "Invalid TrackType %" G_GUINT64_FORMAT,
              track_type);
1156
          break;
1157
1158
        }

1159
1160
        GST_DEBUG_OBJECT (demux, "TrackType: %" G_GUINT64_FORMAT, track_type);

1161
        /* ok, so we're actually going to reallocate this thing */
1162
        switch (track_type) {
1163
          case GST_MATROSKA_TRACK_TYPE_VIDEO:
1164
            gst_matroska_track_init_video_context (&context);
1165
1166
            break;
          case GST_MATROSKA_TRACK_TYPE_AUDIO:
1167
            gst_matroska_track_init_audio_context (&context);
1168
1169
            break;
          case GST_MATROSKA_TRACK_TYPE_SUBTITLE:
1170
            gst_matroska_track_init_subtitle_context (&context);
1171
            break;
1172
          case GST_MATROSKA_TRACK_TYPE_COMPLEX:
1173
          case GST_MATROSKA_TRACK_TYPE_LOGO:
1174
          case GST_MATROSKA_TRACK_TYPE_BUTTONS:
1175
1176
          case GST_MATROSKA_TRACK_TYPE_CONTROL:
          default:
1177
1178
1179
            GST_WARNING_OBJECT (demux,
                "Unknown or unsupported TrackType %" G_GUINT64_FORMAT,
                track_type);
1180
1181
1182
            context->type = 0;
            break;
        }
1183
        g_ptr_array_index (demux->src, demux->num_streams - 1) = context;
1184
        break;
1185
1186
      }

1187
        /* tracktype specific stuff for video */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1188
      case GST_MATROSKA_ID_TRACKVIDEO:{
1189
1190
        GstMatroskaTrackVideoContext *videocontext;

1191
1192
        DEBUG_ELEMENT_START (demux, ebml, "TrackVideo");

1193
        if (!gst_matroska_track_init_video_context (&context)) {
1194
1195
          GST_WARNING_OBJECT (demux,
              "TrackVideo element in non-video track - ignoring track");
1196
          ret = GST_FLOW_ERROR;
1197
          break;
1198
        } else if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
1199
1200
1201
          break;
        }
        videocontext = (GstMatroskaTrackVideoContext *) context;
1202
        g_ptr_array_index (demux->src, demux->num_streams - 1) = context;
1203

1204
1205
1206
        while (ret == GST_FLOW_OK) {
          if ((ret =
                  gst_ebml_peek_id (ebml, &demux->level_up,
1207
                      &id)) != GST_FLOW_OK)
1208
            break;
1209
1210

          if (demux->level_up) {
1211
1212
1213
1214
1215
            demux->level_up--;
            break;
          }

          switch (id) {
1216
              /* Should be one level up but some broken muxers write it here. */
1217
1218
1219
            case GST_MATROSKA_ID_TRACKDEFAULTDURATION:{
              guint64 num;

1220
              if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
1221
                break;
1222
1223

              if (num == 0) {
1224
                GST_WARNING_OBJECT (demux, "Invalid TrackDefaultDuration 0");
1225
1226
1227
                break;
              }

1228
1229
              GST_DEBUG_OBJECT (demux,
                  "TrackDefaultDuration: %" G_GUINT64_FORMAT, num);
1230
1231
1232
1233
1234
              context->default_duration = num;
              break;
            }

              /* video framerate */
1235
1236
              /* NOTE: This one is here only for backward compatibility.
               * Use _TRACKDEFAULDURATION one level up. */
1237
1238
1239
            case GST_MATROSKA_ID_VIDEOFRAMERATE:{
              gdouble num;

1240
              if ((ret = gst_ebml_read_float (ebml, &id, &num)) != GST_FLOW_OK)
1241
                break;
1242
1243

              if (num <= 0.0) {
1244
                GST_WARNING_OBJECT (demux, "Invalid TrackVideoFPS %lf", num);
1245
                break;
1246
              }
1247

1248
              GST_DEBUG_OBJECT (demux, "TrackVideoFrameRate: %lf", num);
1249
1250
1251
1252
              if (context->default_duration == 0)
                context->default_duration =
                    gst_gdouble_to_guint64 ((gdouble) GST_SECOND * (1.0 / num));
              videocontext->default_fps = num;
1253
1254
1255
1256
1257
1258
1259
              break;
            }

              /* width of the size to display the video at */
            case GST_MATROSKA_ID_VIDEODISPLAYWIDTH:{
              guint64 num;

1260
              if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
1261
                break;
1262
1263

              if (num == 0) {
1264
                GST_WARNING_OBJECT (demux, "Invalid TrackVideoDisplayWidth 0");
1265
1266
1267
                break;
              }

1268
1269
              GST_DEBUG_OBJECT (demux,
                  "TrackVideoDisplayWidth: %" G_GUINT64_FORMAT, num);
1270
1271
1272
1273
1274
1275
1276
1277
              videocontext->display_width = num;
              break;
            }

              /* height of the size to display the video at */
            case GST_MATROSKA_ID_VIDEODISPLAYHEIGHT:{
              guint64 num;