matroska-mux.c 51.2 KB
Newer Older
1
2
/* GStreamer Matroska muxer/demuxer
 * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3
 * (c) 2005 Michal Benes <michal.benes@xeris.cz>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 *
 * matroska-mux.c: matroska file/stream muxer
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

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

#include "matroska-mux.h"
#include "matroska-ids.h"

33
GST_DEBUG_CATEGORY_STATIC (matroskamux_debug);
34
35
#define GST_CAT_DEFAULT matroskamux_debug

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
36
37
enum
{
38
39
40
41
  /* FILL ME */
  LAST_SIGNAL
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
42
43
enum
{
44
  ARG_0,
45
46
  ARG_WRITING_APP,
  ARG_MATROSKA_VERSION
47
      /* FILL ME */
48
49
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
50
51
52
53
54
static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-matroska")
    );
55

David Schleef's avatar
David Schleef committed
56
57
58
#define COMMON_VIDEO_CAPS \
  "width = (int) [ 16, 4096 ], " \
  "height = (int) [ 16, 4096 ], " \
59
  "framerate = (fraction) [ 0, MAX ]"
60

David Schleef's avatar
David Schleef committed
61
static GstStaticPadTemplate videosink_templ =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
62
63
64
65
    GST_STATIC_PAD_TEMPLATE ("video_%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ("video/mpeg, "
66
67
68
        "mpegversion = (int) { 1, 2, 4 }, "
        "systemstream = (boolean) false, "
        COMMON_VIDEO_CAPS "; "
69
70
        "video/x-h264, "
        COMMON_VIDEO_CAPS "; "
71
72
73
74
        "video/x-divx, "
        COMMON_VIDEO_CAPS "; "
        "video/x-xvid, "
        COMMON_VIDEO_CAPS "; "
75
76
77
78
79
80
        "video/x-huffyuv, "
        COMMON_VIDEO_CAPS "; "
        "video/x-dv, "
        COMMON_VIDEO_CAPS "; "
        "video/x-h263, "
        COMMON_VIDEO_CAPS "; "
81
82
        "video/x-msmpeg, "
        COMMON_VIDEO_CAPS "; "
83
        "image/jpeg, "
84
85
86
        COMMON_VIDEO_CAPS "; "
        "video/x-raw-yuv, "
        "format = (fourcc) { YUY2, I420 }, " COMMON_VIDEO_CAPS)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
87
    );
David Schleef's avatar
David Schleef committed
88
89
90
91

#define COMMON_AUDIO_CAPS \
  "channels = (int) [ 1, 8 ], " \
  "rate = (int) [ 8000, 96000 ]"
92
93
94
95

/* FIXME:
 * * audio/x-raw-float: endianness needs defining.
 */
David Schleef's avatar
David Schleef committed
96
static GstStaticPadTemplate audiosink_templ =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
97
98
99
100
    GST_STATIC_PAD_TEMPLATE ("audio_%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ("audio/mpeg, "
101
102
103
104
105
106
107
108
        "mpegversion = (int) 1, "
        "layer = (int) [ 1, 3 ], "
        COMMON_AUDIO_CAPS "; "
        "audio/mpeg, "
        "mpegversion = (int) { 2, 4 }, "
        COMMON_AUDIO_CAPS "; "
        "audio/x-ac3, "
        COMMON_AUDIO_CAPS "; "
109
110
        "audio/x-vorbis, "
        COMMON_AUDIO_CAPS "; "
111
112
113
114
        "audio/x-raw-int, "
        "width = (int) { 8, 16, 24 }, "
        "depth = (int) { 8, 16, 24 }, "
        "endianness = (int) { BIG_ENDIAN, LITTLE_ENDIAN }, "
115
116
        "signed = (boolean) { true, false }, "
        COMMON_AUDIO_CAPS ";"
117
        "audio/x-tta, "
118
119
        "width = (int) { 8, 16, 24 }, "
        "channels = (int) { 1, 2 }, " "rate = (int) [ 8000, 96000 ]")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
120
    );
David Schleef's avatar
David Schleef committed
121
122

static GstStaticPadTemplate subtitlesink_templ =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
123
124
125
126
GST_STATIC_PAD_TEMPLATE ("subtitle_%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS_ANY);
127

128
129
static GArray *used_uids;

130
131
132
133
134
135
136
137
138
139
140
141
static void
_do_init (GType matroskamux_type)
{
  GST_DEBUG_CATEGORY_INIT (matroskamux_debug, "matroskamux", 0,
      "Matroska muxer");
}

GST_BOILERPLATE_FULL (GstMatroskaMux, gst_matroska_mux, GstElement,
    GST_TYPE_ELEMENT, _do_init);

/* Matroska muxer destructor */
static void gst_matroska_mux_finalize (GObject * object);
142

143
144
145
/* Pads collected callback */
static GstFlowReturn
gst_matroska_mux_collected (GstCollectPads * pads, gpointer user_data);
146
147

/* pad functions */
148
149
static gboolean gst_matroska_mux_handle_src_event (GstPad * pad,
    GstEvent * event);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
150
151
static GstPad *gst_matroska_mux_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name);
152
153

/* gst internal change state handler */
154
155
static GstStateChangeReturn
gst_matroska_mux_change_state (GstElement * element, GstStateChange transition);
156
157

/* gobject bla bla */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
158
159
160
161
static void gst_matroska_mux_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_matroska_mux_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
162
163

/* reset muxer */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
164
static void gst_matroska_mux_reset (GstElement * element);
165

166
167
168
/* uid generation */
static guint32 gst_matroska_mux_create_uid ();

169
170
171
/*static guint gst_matroska_mux_signals[LAST_SIGNAL] = { 0 };*/

static void
172
gst_matroska_mux_base_init (gpointer g_class)
173
{
174
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
175
  static const GstElementDetails gst_matroska_mux_details =
Wim Taymans's avatar
Wim Taymans committed
176
177
178
179
      GST_ELEMENT_DETAILS ("Matroska muxer",
      "Codec/Muxer",
      "Muxes video/audio/subtitle streams into a matroska stream",
      "Ronald Bultje <rbultje@ronald.bitfreak.net>");
180
181

  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
182
      gst_static_pad_template_get (&videosink_templ));
183
  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
184
      gst_static_pad_template_get (&audiosink_templ));
185
  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
186
      gst_static_pad_template_get (&subtitlesink_templ));
187
  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
188
189
      gst_static_pad_template_get (&src_templ));
  gst_element_class_set_details (element_class, &gst_matroska_mux_details);
190
191
192
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
193
gst_matroska_mux_class_init (GstMatroskaMuxClass * klass)
194
195
196
197
198
199
200
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

201
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_matroska_mux_finalize);
202
203
204
205

  gobject_class->get_property = gst_matroska_mux_get_property;
  gobject_class->set_property = gst_matroska_mux_set_property;

206
207
208
209
  g_object_class_install_property (gobject_class, ARG_WRITING_APP,
      g_param_spec_string ("writing-app", "Writing application.",
          "The name the application that creates the matroska file.",
          NULL, G_PARAM_READWRITE));
210
211
212
213
  g_object_class_install_property (gobject_class, ARG_MATROSKA_VERSION,
      g_param_spec_int ("version", "Matroska version",
          "This parameter determines what matroska features can be used.",
          1, 2, 1, G_PARAM_READWRITE));
214

215
216
217
218
  gstelement_class->change_state = gst_matroska_mux_change_state;
  gstelement_class->request_new_pad = gst_matroska_mux_request_new_pad;
}

219
220
221
222
223
224
225
226

/**
 * gst_matroska_mux_init:
 * @mux: #GstMatroskaMux that should be initialized.
 * @g_class: Class of the muxer.
 *
 * Matroska muxer constructor.
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
227
static void
228
gst_matroska_mux_init (GstMatroskaMux * mux, GstMatroskaMuxClass * g_class)
229
{
230
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
231

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
232
  mux->srcpad =
233
234
235
      gst_pad_new_from_template (gst_element_class_get_pad_template
      (gstelement_class, "src"), "src");
  gst_pad_set_event_function (mux->srcpad, gst_matroska_mux_handle_src_event);
236
237
  gst_element_add_pad (GST_ELEMENT (mux), mux->srcpad);

238
239
  mux->collect = gst_collect_pads_new ();
  gst_collect_pads_set_function (mux->collect,
240
241
      (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_matroska_mux_collected),
      mux);
242

243
244
245
  mux->ebml_write = gst_ebml_write_new (mux->srcpad);

  /* initialize internal variables */
246
  mux->index = NULL;
247
  mux->matroska_version = 1;
248

249
  /* Initialize all variables */
250
251
252
  gst_matroska_mux_reset (GST_ELEMENT (mux));
}

253
254
255
256
257
258
259
260
261
262
263
264
265
266

/**
 * gst_matroska_mux_finalize:
 * @object: #GstMatroskaMux that should be finalized.
 *
 * Finalize matroska muxer.
 */
static void
gst_matroska_mux_finalize (GObject * object)
{
  GstMatroskaMux *mux = GST_MATROSKA_MUX (object);

  gst_object_unref (mux->collect);
  gst_object_unref (mux->ebml_write);
267
268
  if (mux->writing_app)
    g_free (mux->writing_app);
269
270
271
272
273
274
275
276
277
278
279
280

  G_OBJECT_CLASS (parent_class)->finalize (object);
}


/**
 * gst_matroska_mux_create_uid:
 *
 * Generate new unused track UID.
 *
 * Returns: New track UID.
 */
281
static guint32
282
gst_matroska_mux_create_uid (void)
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
{
  guint32 uid = 0;
  GRand *rand = g_rand_new ();

  while (!uid) {
    guint i;

    uid = g_rand_int (rand);
    for (i = 0; i < used_uids->len; i++) {
      if (g_array_index (used_uids, guint32, i) == uid) {
        uid = 0;
        break;
      }
    }
    g_array_append_val (used_uids, uid);
  }
  g_free (rand);
  return uid;
}

303
304
305
306
307
308
309

/**
 * gst_matroska_mux_reset:
 * @element: #GstMatroskaMux that should be reseted.
 *
 * Reset matroska muxer back to initial state.
 */
310
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
311
gst_matroska_mux_reset (GstElement * element)
312
313
{
  GstMatroskaMux *mux = GST_MATROSKA_MUX (element);
314
315
316
317
  GSList *walk;

  /* reset EBML write */
  gst_ebml_write_reset (mux->ebml_write);
318
319
320
321
322

  /* reset input */
  mux->state = GST_MATROSKA_MUX_STATE_START;

  /* clean up existing streams */
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  while ((walk = mux->collect->data) != NULL) {
    GstMatroskaPad *collect_pad;
    GstPad *thepad;

    collect_pad = (GstMatroskaPad *) walk->data;
    thepad = collect_pad->collect.pad;

    /* free track information */
    if (collect_pad->track != NULL) {
      g_free (collect_pad->track->codec_id);
      g_free (collect_pad->track->codec_name);
      g_free (collect_pad->track->name);
      g_free (collect_pad->track->language);
      g_free (collect_pad->track->codec_priv);
      g_free (collect_pad->track);
      collect_pad->track = NULL;
339
    }
340
341
342
343
344

    /* free cached buffer */
    if (collect_pad->buffer != NULL) {
      gst_buffer_unref (collect_pad->buffer);
      collect_pad->buffer = NULL;
345
    }
346
347

    /* remove from collectpads */
348
    gst_collect_pads_remove_pad (mux->collect, thepad);
349
350
351
352
353
354
  }
  mux->num_streams = 0;
  mux->num_a_streams = 0;
  mux->num_t_streams = 0;
  mux->num_v_streams = 0;

355
356
  /* reset writing_app */
  if (mux->writing_app) {
357
    g_free (mux->writing_app);
358
359
  }
  mux->writing_app = g_strdup ("GStreamer Matroska muxer");
360
361
362
363
364
365
366
367
368

  /* reset indexes */
  mux->num_indexes = 0;
  g_free (mux->index);
  mux->index = NULL;

  /* reset timers */
  mux->time_scale = 1000000;
  mux->duration = 0;
369
370
371

  /* reset uid array */
  if (used_uids) {
372
    g_array_free (used_uids, TRUE);
373
374
375
  }
  /* arbitrary size, 10 should be enough in most cases */
  used_uids = g_array_sized_new (FALSE, FALSE, sizeof (guint32), 10);
376
377
378
379
380

  /* reset cluster */
  mux->cluster = 0;
  mux->cluster_time = 0;
  mux->cluster_pos = 0;
381
382
383
384
385

  /* reset meta-seek index */
  mux->num_meta_indexes = 0;
  g_free (mux->meta_index);
  mux->meta_index = NULL;
386
387
}

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**
 * gst_matroska_mux_handle_src_event:
 * @pad: Pad which received the event.
 * @event: Received event.
 *
 * handle events - copied from oggmux without understanding 
 *
 * Returns: #TRUE on success.
 */
static gboolean
gst_matroska_mux_handle_src_event (GstPad * pad, GstEvent * event)
{
  GstEventType type;

  type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;

  switch (type) {
    case GST_EVENT_SEEK:
      /* disable seeking for now */
      return FALSE;
    default:
      break;
  }

  return gst_pad_event_default (pad, event);
}


/**
 * gst_matroska_mux_video_pad_setcaps:
 * @pad: Pad which got the caps.
 * @caps: New caps.
 *
 * Setcaps function for video sink pad.
 *
 * Returns: #TRUE on success.
 */
static gboolean
gst_matroska_mux_video_pad_setcaps (GstPad * pad, GstCaps * caps)
427
428
429
{
  GstMatroskaTrackContext *context = NULL;
  GstMatroskaTrackVideoContext *videocontext;
430
  GstMatroskaPad *collect_pad;
431
  const gchar *mimetype;
432
  gint width, height, pixel_width, pixel_height;
433
  const GValue *framerate;
David Schleef's avatar
David Schleef committed
434
  GstStructure *structure;
435
436

  /* find context */
437
438
439
440
  collect_pad = (GstMatroskaPad *) gst_pad_get_element_private (pad);
  g_assert (collect_pad);
  context = collect_pad->track;
  g_assert (context);
441
442
443
444
  g_assert (context->type == GST_MATROSKA_TRACK_TYPE_VIDEO);
  videocontext = (GstMatroskaTrackVideoContext *) context;

  /* gst -> matroska ID'ing */
David Schleef's avatar
David Schleef committed
445
446
447
448
449
450
451
  structure = gst_caps_get_structure (caps, 0);

  mimetype = gst_structure_get_name (structure);

  /* get general properties */
  gst_structure_get_int (structure, "width", &width);
  gst_structure_get_int (structure, "height", &height);
452
453
454
  framerate = gst_structure_get_value (structure, "framerate");
  if (framerate == NULL || !GST_VALUE_HOLDS_FRACTION (framerate))
    return FALSE;
David Schleef's avatar
David Schleef committed
455
456
457

  videocontext->pixel_width = width;
  videocontext->pixel_height = height;
Jan Schmidt's avatar
Jan Schmidt committed
458
  context->default_duration = gst_util_uint64_scale_int (GST_SECOND,
459
460
      gst_value_get_fraction_denominator (framerate),
      gst_value_get_fraction_numerator (framerate));
David Schleef's avatar
David Schleef committed
461

462
463
  if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
          &pixel_width, &pixel_height)) {
David Schleef's avatar
David Schleef committed
464
465
466
467
468
469
    if (pixel_width > pixel_height) {
      videocontext->display_width = width * pixel_width / pixel_height;
      videocontext->display_height = height;
    } else if (pixel_width < pixel_height) {
      videocontext->display_width = width;
      videocontext->display_height = height * pixel_height / pixel_width;
470
471
472
473
    } else {
      videocontext->display_width = 0;
      videocontext->display_height = 0;
    }
David Schleef's avatar
David Schleef committed
474
475
476
477
  } else {
    videocontext->display_width = 0;
    videocontext->display_height = 0;
  }
478

David Schleef's avatar
David Schleef committed
479
480
481
482
483
484
485
486
487
  videocontext->asr_mode = GST_MATROSKA_ASPECT_RATIO_MODE_FREE;
  videocontext->eye_mode = GST_MATROSKA_EYE_MODE_MONO;
  videocontext->fourcc = 0;

  /* find type */
  if (!strcmp (mimetype, "video/x-raw-yuv")) {
    context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_UNCOMPRESSED);
    gst_structure_get_fourcc (structure, "format", &videocontext->fourcc);

488
    return TRUE;
489
  } else if (!strcmp (mimetype, "image/jpeg")) {
David Schleef's avatar
David Schleef committed
490
491
    context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MJPEG);

492
    return TRUE;
493
494
495
496
497
  } else if (!strcmp (mimetype, "video/x-xvid") /* MS/VfW compatibility cases */
      ||!strcmp (mimetype, "video/x-huffyuv")
      || !strcmp (mimetype, "video/x-divx")
      || !strcmp (mimetype, "video/x-dv")
      || !strcmp (mimetype, "video/x-h263")) {
498
    BITMAPINFOHEADER *bih;
499
500
    const GValue *codec_data;
    gint size = sizeof (BITMAPINFOHEADER);
501

502
503
    bih = g_new0 (BITMAPINFOHEADER, 1);
    GST_WRITE_UINT32_LE (&bih->bi_size, size);
504
505
506
507
508
509
    GST_WRITE_UINT32_LE (&bih->bi_width, videocontext->pixel_width);
    GST_WRITE_UINT32_LE (&bih->bi_height, videocontext->pixel_height);
    GST_WRITE_UINT16_LE (&bih->bi_planes, (guint16) 1);
    GST_WRITE_UINT16_LE (&bih->bi_bit_count, (guint16) 24);
    GST_WRITE_UINT32_LE (&bih->bi_size_image, videocontext->pixel_width *
        videocontext->pixel_height * 3);
David Schleef's avatar
David Schleef committed
510

511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
    if (!strcmp (mimetype, "video/x-xvid"))
      GST_WRITE_UINT32_LE (&bih->bi_compression, GST_STR_FOURCC ("XVID"));
    else if (!strcmp (mimetype, "video/x-huffyuv"))
      GST_WRITE_UINT32_LE (&bih->bi_compression, GST_STR_FOURCC ("HFYU"));
    else if (!strcmp (mimetype, "video/x-dv"))
      GST_WRITE_UINT32_LE (&bih->bi_compression, GST_STR_FOURCC ("DVSD"));
    else if (!strcmp (mimetype, "video/x-h263"))
      GST_WRITE_UINT32_LE (&bih->bi_compression, GST_STR_FOURCC ("H263"));
    else if (!strcmp (mimetype, "video/x-divx")) {
      gint divxversion;

      gst_structure_get_int (structure, "divxversion", &divxversion);
      switch (divxversion) {
        case 3:
          GST_WRITE_UINT32_LE (&bih->bi_compression, GST_STR_FOURCC ("DIV3"));
          break;
        case 4:
          GST_WRITE_UINT32_LE (&bih->bi_compression, GST_STR_FOURCC ("DIVX"));
          break;
        case 5:
          GST_WRITE_UINT32_LE (&bih->bi_compression, GST_STR_FOURCC ("DX50"));
          break;
      }
David Schleef's avatar
David Schleef committed
534
    }
535

536
537
538
539
540
541
542
543
544
545
546
547
    /* process codec private/initialization data, if any */
    codec_data = gst_structure_get_value (structure, "codec_data");
    if (codec_data) {
      GstBuffer *codec_data_buf;

      codec_data_buf = g_value_peek_pointer (codec_data);
      size += GST_BUFFER_SIZE (codec_data_buf);
      bih = g_realloc (bih, size);
      GST_WRITE_UINT32_LE (&bih->bi_size, size);
      memcpy ((guint8 *) bih + sizeof (BITMAPINFOHEADER),
          GST_BUFFER_DATA (codec_data_buf), GST_BUFFER_SIZE (codec_data_buf));
    }
548
549
550

    context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC);
    context->codec_priv = (gpointer) bih;
551
    context->codec_priv_size = size;
David Schleef's avatar
David Schleef committed
552

553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
    return TRUE;
  } else if (!strcmp (mimetype, "video/x-h264")) {
    const GValue *codec_data;

    context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_AVC);

    if (context->codec_priv != NULL) {
      g_free (context->codec_priv);
      context->codec_priv = NULL;
      context->codec_priv_size = 0;
    }


    /* Create avcC header */
    codec_data = gst_structure_get_value (structure, "codec_data");

    if (codec_data != NULL) {
      guint8 *priv_data = NULL;
      guint priv_data_size = 0;

      GstBuffer *codec_data_buf = g_value_peek_pointer (codec_data);

      priv_data_size = GST_BUFFER_SIZE (codec_data_buf);
      priv_data = g_malloc0 (priv_data_size);

      memcpy (priv_data, GST_BUFFER_DATA (codec_data_buf), priv_data_size);

      context->codec_priv = priv_data;
      context->codec_priv_size = priv_data_size;
    }

    return TRUE;
David Schleef's avatar
David Schleef committed
585
586
587
588
589
590
  } else if (!strcmp (mimetype, "video/mpeg")) {
    gint mpegversion;

    gst_structure_get_int (structure, "mpegversion", &mpegversion);
    switch (mpegversion) {
      case 1:
591
592
        context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MPEG1);
        break;
David Schleef's avatar
David Schleef committed
593
      case 2:
594
595
        context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MPEG2);
        break;
596
      case 4:
597
598
        context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP);
        break;
599
600
      default:
        return FALSE;
David Schleef's avatar
David Schleef committed
601
    }
602

603
    return TRUE;
David Schleef's avatar
David Schleef committed
604
605
  } else if (!strcmp (mimetype, "video/x-msmpeg")) {
    context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3);
606

607
    return TRUE;
608
609
  }

610
  return FALSE;
611
612
}

613
614
615
616
617
618
619
620
621
622
623
624

/**
 * gst_matroska_mux_audio_pad_setcaps:
 * @pad: Pad which got the caps.
 * @caps: New caps.
 *
 * Setcaps function for audio sink pad.
 *
 * Returns: #TRUE on success.
 */
static gboolean
gst_matroska_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
625
626
627
{
  GstMatroskaTrackContext *context = NULL;
  GstMatroskaTrackAudioContext *audiocontext;
628
  GstMatroskaPad *collect_pad;
629
  const gchar *mimetype;
630
  gint samplerate = 0, channels = 0;
David Schleef's avatar
David Schleef committed
631
  GstStructure *structure;
632
633

  /* find context */
634
635
636
637
  collect_pad = (GstMatroskaPad *) gst_pad_get_element_private (pad);
  g_assert (collect_pad);
  context = collect_pad->track;
  g_assert (context);
638
639
640
  g_assert (context->type == GST_MATROSKA_TRACK_TYPE_AUDIO);
  audiocontext = (GstMatroskaTrackAudioContext *) context;

David Schleef's avatar
David Schleef committed
641
642
643
644
645
646
647
648
649
650
  structure = gst_caps_get_structure (caps, 0);
  mimetype = gst_structure_get_name (structure);

  /* general setup */
  gst_structure_get_int (structure, "rate", &samplerate);
  gst_structure_get_int (structure, "channels", &channels);

  audiocontext->samplerate = samplerate;
  audiocontext->channels = channels;
  audiocontext->bitdepth = 0;
651
  context->default_duration = 0;
David Schleef's avatar
David Schleef committed
652
653
654
655
656
657

  if (!strcmp (mimetype, "audio/mpeg")) {
    gint mpegversion = 0;

    gst_structure_get_int (structure, "mpegversion", &mpegversion);
    switch (mpegversion) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
658
      case 1:{
659
660
661
662
663
664
        gint layer;

        gst_structure_get_int (structure, "layer", &layer);
        switch (layer) {
          case 1:
            context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L1);
665
666
            context->default_duration =
                384 * GST_SECOND / audiocontext->samplerate;
667
668
669
            break;
          case 2:
            context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L2);
670
671
            context->default_duration =
                1152 * GST_SECOND / audiocontext->samplerate;
672
673
674
            break;
          case 3:
            context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L3);
675
676
            context->default_duration =
                1152 * GST_SECOND / audiocontext->samplerate;
677
            break;
678
679
          default:
            return FALSE;
680
681
        }
        break;
682
      }
David Schleef's avatar
David Schleef committed
683
      case 2:
684
685
        context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_MPEG2 "MAIN");
        break;
David Schleef's avatar
David Schleef committed
686
      case 4:
687
688
        context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_MPEG4 "MAIN");
        break;
689
690
      default:
        return FALSE;
691
    }
David Schleef's avatar
David Schleef committed
692

693
    return TRUE;
David Schleef's avatar
David Schleef committed
694
695
696
697
  } else if (!strcmp (mimetype, "audio/x-raw-int")) {
    gint endianness, width, depth;
    gboolean signedness;

698
699
700
701
702
703
    if (!gst_structure_get_int (structure, "endianness", &endianness) ||
        !gst_structure_get_int (structure, "width", &width) ||
        !gst_structure_get_int (structure, "depth", &depth) ||
        !gst_structure_get_int (structure, "signed", &signedness))
      return FALSE;

David Schleef's avatar
David Schleef committed
704
    if (width != depth ||
705
        (width == 8 && signedness) || (width == 16 && !signedness))
706
      return FALSE;
David Schleef's avatar
David Schleef committed
707
708
709
710
711
712
713

    audiocontext->bitdepth = depth;
    if (endianness == G_BIG_ENDIAN)
      context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE);
    else
      context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_PCM_INT_LE);

714
    return TRUE;
David Schleef's avatar
David Schleef committed
715
716
717
  } else if (!strcmp (mimetype, "audio/x-raw-float")) {
    /* FIXME: endianness is undefined */
  } else if (!strcmp (mimetype, "audio/x-vorbis")) {
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
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
    const GValue *streamheader;
    guint8 *priv_data = NULL;
    guint priv_data_size = 0;

    context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_VORBIS);

    if (context->codec_priv != NULL) {
      g_free (context->codec_priv);
      context->codec_priv = NULL;
      context->codec_priv_size = 0;
    }
    streamheader = gst_structure_get_value (structure, "streamheader");

    if (streamheader != NULL) {
      if (G_VALUE_TYPE (streamheader) == GST_TYPE_ARRAY) {
        GArray *bufarr = g_value_peek_pointer (streamheader);
        gint i;
        gint offset;

        if (bufarr->len == 3) {
          GstBuffer *buf[3];

          for (i = 0; i < bufarr->len; i++) {
            GValue *bufval = &g_array_index (bufarr, GValue, i);

            if (G_VALUE_TYPE (bufval) == GST_TYPE_BUFFER) {
              buf[i] = g_value_peek_pointer (bufval);
            }
          }

          priv_data_size = 1;
          priv_data_size += GST_BUFFER_SIZE (buf[0]) / 0xff + 1;
          priv_data_size += GST_BUFFER_SIZE (buf[1]) / 0xff + 1;

          for (i = 0; i < 3; ++i) {
            priv_data_size += GST_BUFFER_SIZE (buf[i]);
          }

          priv_data = g_malloc0 (priv_data_size);

          priv_data[0] = 2;
          offset = 1;

          for (i = 0; i < GST_BUFFER_SIZE (buf[0]) / 0xff; ++i) {
            priv_data[offset++] = 0xff;
          }
          priv_data[offset++] = GST_BUFFER_SIZE (buf[0]) % 0xff;

          for (i = 0; i < GST_BUFFER_SIZE (buf[1]) / 0xff; ++i) {
            priv_data[offset++] = 0xff;
          }
          priv_data[offset++] = GST_BUFFER_SIZE (buf[1]) % 0xff;

          for (i = 0; i < 3; ++i) {
            memcpy (priv_data + offset, GST_BUFFER_DATA (buf[i]),
                GST_BUFFER_SIZE (buf[i]));
            offset += GST_BUFFER_SIZE (buf[i]);
          }
776

777
778
779
780
781
          if (GST_BUFFER_SIZE (buf[0]) < 1 + 6 + 4) {
            GST_WARNING ("First vorbis header too small, ignoring");
          } else {
            if (memcmp (GST_BUFFER_DATA (buf[0]) + 1, "vorbis", 6) == 0) {
              guint8 *hdr = GST_BUFFER_DATA (buf[0]) + 1 + 6 + 4;
782

783
784
785
              audiocontext->channels = GST_READ_UINT8 (hdr);
              audiocontext->samplerate = GST_READ_UINT32_LE (hdr + 1);
            }
786
          }
787
        } else {
788
          GST_WARNING ("Vorbis header does not contain "
789
790
791
792
793
794
              "three buffers (found %d buffers), Ignoring.", bufarr->len);
        }
      }
    }

    if (priv_data == NULL) {
795
      GST_WARNING ("Could not write Vorbis header into codec private data. "
796
797
798
799
800
801
802
803
          "You will probably not be able to play the stream.");
    }

    context->codec_priv_size = priv_data_size;
    context->codec_priv = (gpointer) priv_data;

    return TRUE;

David Schleef's avatar
David Schleef committed
804
805
806
  } else if (!strcmp (mimetype, "audio/x-ac3")) {
    context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_AC3);

807
    return TRUE;
808
  } else if (!strcmp (mimetype, "audio/x-tta")) {
809
810
    gint width;

811
812
813
    /* TTA frame duration */
    context->default_duration = 1.04489795918367346939 * GST_SECOND;

814
815
816
817
    gst_structure_get_int (structure, "width", &width);
    audiocontext->bitdepth = width;
    context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_TTA);

818
    return TRUE;
819
820
  }

821
  return FALSE;
822
823
}

824
825
826
827
828
829
830
831
832
833
834
835

/**
 * gst_matroska_mux_subtitle_pad_setcaps:
 * @pad: Pad which got the caps.
 * @caps: New caps.
 *
 * Setcaps function for subtitle sink pad.
 *
 * Returns: #TRUE on success.
 */
static gboolean
gst_matroska_mux_subtitle_pad_setcaps (GstPad * pad, GstCaps * caps)
836
837
838
839
840
{
  /* Consider this as boilerplate code for now. There is
   * no single subtitle creation element in GStreamer,
   * neither do I know how subtitling works at all. */

841
  return FALSE;
842
843
}

844
845
846
847
848
849
850
851
852
853
854

/**
 * gst_matroska_mux_request_new_pad:
 * @element: #GstMatroskaMux.
 * @templ: #GstPadTemplate.
 * @pad_name: New pad name.
 *
 * Request pad function for sink templates.
 *
 * Returns: New #GstPad.
 */
855
static GstPad *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
856
857
gst_matroska_mux_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * pad_name)
858
859
860
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
  GstMatroskaMux *mux = GST_MATROSKA_MUX (element);
861
862
  GstMatroskaPad *collect_pad;
  GstPad *newpad = NULL;
863
  gchar *name = NULL;
864
  GstPadSetCapsFunction setcapsfunc = NULL;
865
866
867
868
  GstMatroskaTrackContext *context = NULL;

  if (templ == gst_element_class_get_pad_template (klass, "audio_%d")) {
    name = g_strdup_printf ("audio_%d", mux->num_a_streams++);
869
    setcapsfunc = gst_matroska_mux_audio_pad_setcaps;
870
    context = (GstMatroskaTrackContext *)
871
        g_new0 (GstMatroskaTrackAudioContext, 1);
872
873
874
875
    context->type = GST_MATROSKA_TRACK_TYPE_AUDIO;
    context->name = g_strdup ("Audio");
  } else if (templ == gst_element_class_get_pad_template (klass, "video_%d")) {
    name = g_strdup_printf ("video_%d", mux->num_v_streams++);
876
    setcapsfunc = gst_matroska_mux_video_pad_setcaps;
877
    context = (GstMatroskaTrackContext *)
878
        g_new0 (GstMatroskaTrackVideoContext, 1);
879
880
881
882
    context->type = GST_MATROSKA_TRACK_TYPE_VIDEO;
    context->name = g_strdup ("Video");
  } else if (templ == gst_element_class_get_pad_template (klass, "subtitle_%d")) {
    name = g_strdup_printf ("subtitle_%d", mux->num_t_streams++);
883
    setcapsfunc = gst_matroska_mux_subtitle_pad_setcaps;
884
    context = (GstMatroskaTrackContext *)
885
        g_new0 (GstMatroskaTrackSubtitleContext, 1);
886
887
888
    context->type = GST_MATROSKA_TRACK_TYPE_SUBTITLE;
    context->name = g_strdup ("Subtitle");
  } else {
889
    GST_WARNING_OBJECT (mux, "This is not our template!");
890
891
892
    return NULL;
  }

893
  newpad = gst_pad_new_from_template (templ, name);
894
  g_free (name);
895
  collect_pad = (GstMatroskaPad *)
896
      gst_collect_pads_add_pad (mux->collect, newpad, sizeof (GstMatroskaPad));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
897
  context->flags = GST_MATROSKA_TRACK_ENABLED | GST_MATROSKA_TRACK_DEFAULT;
898
899
  collect_pad->track = context;
  collect_pad->buffer = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
900

901
902
903
904
  gst_pad_set_setcaps_function (newpad, setcapsfunc);
  gst_element_add_pad (element, newpad);

  return newpad;
905
906
}

907
908
909
910
911
912
913
914

/**
 * gst_matroska_mux_track_header:
 * @mux: #GstMatroskaMux
 * @context: Tack context.
 *
 * Write a track header.
 */
915
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
916
917
gst_matroska_mux_track_header (GstMatroskaMux * mux,
    GstMatroskaTrackContext * context)
918
{
919
  GstEbmlWrite *ebml = mux->ebml_write;
920
921
922
923
924
925
  guint64 master;

  /* track type goes before the type-specific stuff */
  gst_ebml_write_uint (ebml, GST_MATROSKA_ID_TRACKNUMBER, context->num);
  gst_ebml_write_uint (ebml, GST_MATROSKA_ID_TRACKTYPE, context->type);

926
927
928
929
930
931
932
  gst_ebml_write_uint (ebml, GST_MATROSKA_ID_TRACKUID,
      gst_matroska_mux_create_uid ());
  if (context->default_duration) {
    gst_ebml_write_uint (ebml, GST_MATROSKA_ID_TRACKDEFAULTDURATION,
        context->default_duration);
  }

933
934
  /* type-specific stuff */
  switch (context->type) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
935
    case GST_MATROSKA_TRACK_TYPE_VIDEO:{
936
      GstMatroskaTrackVideoContext *videocontext =
937
          (GstMatroskaTrackVideoContext *) context;
938

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
939
      master = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_TRACKVIDEO);
940
      gst_ebml_write_uint (ebml, GST_MATROSKA_ID_VIDEOPIXELWIDTH,
941
          videocontext->pixel_width);
942
      gst_ebml_write_uint (ebml, GST_MATROSKA_ID_VIDEOPIXELHEIGHT,
943
          videocontext->pixel_height);
944
      if (videocontext->display_width && videocontext->display_height) {
945
946
947
948
        gst_ebml_write_uint (ebml, GST_MATROSKA_ID_VIDEODISPLAYWIDTH,
            videocontext->display_width);
        gst_ebml_write_uint (ebml, GST_MATROSKA_ID_VIDEODISPLAYHEIGHT,
            videocontext->display_height);
949
950
      }
      if (context->flags & GST_MATROSKA_VIDEOTRACK_INTERLACED)
951
        gst_ebml_write_uint (ebml, GST_MATROSKA_ID_VIDEOFLAGINTERLACED, 1);
952
      if (videocontext->fourcc) {
953
        guint32 fcc_le = GUINT32_TO_LE (videocontext->fourcc);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
954

955
956
        gst_ebml_write_binary (ebml, GST_MATROSKA_ID_VIDEOCOLOURSPACE,
            (gpointer) & fcc_le, 4);
957
958
959
960
961
962
      }
      gst_ebml_write_master_finish (ebml, master);

      break;
    }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
963
    case GST_MATROSKA_TRACK_TYPE_AUDIO:{
964
      GstMatroskaTrackAudioContext *audiocontext =
965
          (GstMatroskaTrackAudioContext *) context;
966

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
967
      master = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_TRACKAUDIO);
968
      if (audiocontext->samplerate != 8000)
969
970
        gst_ebml_write_float (ebml, GST_MATROSKA_ID_AUDIOSAMPLINGFREQ,
            audiocontext->samplerate);
971
      if (audiocontext->channels != 1)
972
973
        gst_ebml_write_uint (ebml, GST_MATROSKA_ID_AUDIOCHANNELS,
            audiocontext->channels);
974
      if (audiocontext->bitdepth) {
975
976
        gst_ebml_write_uint (ebml, GST_MATROSKA_ID_AUDIOBITDEPTH,
            audiocontext->bitdepth);
977
      }
978
979
980
981
982
983
984
985
986
987
      gst_ebml_write_master_finish (ebml, master);

      break;
    }

    default:
      /* doesn't need type-specific data */
      break;
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
988
  gst_ebml_write_ascii (ebml, GST_MATROSKA_ID_CODECID, context->codec_id);
989
990
  if (context->codec_priv)
    gst_ebml_write_binary (ebml, GST_MATROSKA_ID_CODECPRIVATE,
991
        context->codec_priv, context->codec_priv_size);
992
993
994
995
  /* FIXME: until we have a nice way of getting the codecname
   * out of the caps, I'm not going to enable this. Too much
   * (useless, double, boring) work... */
  /*gst_ebml_write_utf8 (ebml, GST_MATROSKA_ID_CODECNAME,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
996
997
     context->codec_name); */
  gst_ebml_write_utf8 (ebml, GST_MATROSKA_ID_TRACKNAME, context->name);
998
999
}

1000
1001
1002
1003
1004
1005
1006

/**
 * gst_matroska_mux_start:
 * @mux: #GstMatroskaMux
 *
 * Start a new matroska file (write headers etc...)
 */
1007
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1008
gst_matroska_mux_start (GstMatroskaMux * mux)
1009
{
1010
  GstEbmlWrite *ebml = mux->ebml_write;
1011
  guint32 seekhead_id[] = { GST_MATROSKA_ID_INFO,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1012
1013
    GST_MATROSKA_ID_TRACKS,
    GST_MATROSKA_ID_CUES,
1014
    GST_MATROSKA_ID_SEEKHEAD,
1015
#if 0
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1016
    GST_MATROSKA_ID_TAGS,
1017
#endif
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1018
1019
    0
  };
1020
  guint64 master, child;
1021
1022
  GSList *collected;
  int i;
1023
  guint tracknum = 1;
1024
  gdouble duration = 0;
1025
1026
1027
  guint32 *segment_uid = (guint32 *) g_malloc (16);
  GRand *rand = g_rand_new ();
  GTimeVal time = { 0, 0 };
1028
1029

  /* we start with a EBML header */
1030
  gst_ebml_write_header (ebml, "matroska", mux->matroska_version);
1031
1032

  /* start a segment */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1033
1034
  mux->segment_pos =
      gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_SEGMENT);
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
  mux->segment_master = ebml->pos;

  /* the rest of the header is cached */
  gst_ebml_write_set_cache (ebml, 0x1000);

  /* seekhead (table of contents) - we set the positions later */
  mux->seekhead_pos = ebml->pos;
  master = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_SEEKHEAD);
  for (i = 0; seekhead_id[i] != 0; i++) {
    child = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_SEEKENTRY);
    gst_ebml_write_uint (ebml, GST_MATROSKA_ID_SEEKID, seekhead_id[i]);
    gst_ebml_write_uint (ebml, GST_MATROSKA_ID_SEEKPOSITION, -1);
    gst_ebml_write_master_finish (ebml, child);
  }
  gst_ebml_write_master_finish (ebml, master);

  /* segment info */
  mux->info_pos = ebml->pos;
  master = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_INFO);
1054
1055
1056
1057
1058
1059
  for (i = 0; i < 4; i++) {
    segment_uid[i] = g_rand_int (rand);
  }
  g_free (rand);
  gst_ebml_write_binary (ebml, GST_MATROSKA_ID_SEGMENTUID,
      (guint8 *) segment_uid, 16);
1060
  g_free (segment_uid);
1061
1062
  gst_ebml_write_uint (ebml, GST_MATROSKA_ID_TIMECODESCALE, mux->time_scale);
  mux->duration_pos = ebml->pos;
1063
  /* get duration */
1064
1065
1066
  for (collected = mux->collect->data; collected;
      collected = g_slist_next (collected)) {
    GstMatroskaPad *collect_pad;
1067
1068
1069
    GstFormat format = GST_FORMAT_TIME;
    GstPad *thepad, *peerpad;
    gint64 trackduration;
1070
1071
1072
1073
1074

    collect_pad = (GstMatroskaPad *) collected->data;
    thepad = collect_pad->collect.pad;

    /* Query the total length of the track. */
1075
    peerpad = gst_pad_get_peer (thepad);
1076
    GST_DEBUG_OBJECT (thepad, "querying duration");
1077
    if (gst_pad_query_duration (peerpad, &format, &trackduration)) {
1078
1079
      GST_DEBUG_OBJECT (thepad, "%duration: %" GST_TIME_FORMAT,
          GST_TIME_ARGS (trackduration));
1080
1081
1082
1083
      if ((gdouble) trackduration > duration) {
        duration = (gdouble) trackduration;
      }
    }
1084
    gst_object_unref (peerpad);
1085
1086
  }
  gst_ebml_write_float (ebml, GST_MATROSKA_ID_DURATION,
1087
      duration / gst_guint64_to_gdouble (mux->time_scale));
1088
  gst_ebml_write_utf8 (ebml, GST_MATROSKA_ID_MUXINGAPP,
1089
      "GStreamer plugin version " PACKAGE_VERSION);
1090
1091
  if (mux->writing_app && mux->writing_app[0]) {
    gst_ebml_write_utf8 (ebml, GST_MATROSKA_ID_WRITINGAPP, mux->writing_app);
1092
  }
1093
1094
  g_get_current_time (&time);
  gst_ebml_write_date (ebml, GST_MATROSKA_ID_DATEUTC, time.tv_sec);
1095
1096
1097
1098
1099
  gst_ebml_write_master_finish (ebml, master);

  /* tracks */
  mux->tracks_pos = ebml->pos;
  master = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_TRACKS);
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109

  for (collected = mux->collect->data; collected;
      collected = g_slist_next (collected)) {

    GstMatroskaPad *collect_pad;
    GstPad *thepad;

    collect_pad = (GstMatroskaPad *) collected->data;
    thepad = collect_pad->collect.pad;

1110
1111
    if (gst_pad_is_linked (thepad) && gst_pad_is_active (thepad) &&
        collect_pad->track->codec_id != 0) {
1112
      collect_pad->track->num = tracknum++;
1113
      child = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_TRACKENTRY);
1114
      gst_matroska_mux_track_header (mux, collect_pad->track);
1115
1116
1117
1118
1119
1120
1121
1122
1123
      gst_ebml_write_master_finish (ebml, child);
    }
  }
  gst_ebml_write_master_finish (ebml, master);

  /* lastly, flush the cache */
  gst_ebml_write_flush_cache (ebml);
}

1124
1125
1126
1127
1128
1129
1130

/**
 * gst_matroska_mux_finish:
 * @mux: #GstMatroskaMux
 *
 * Finish a new matroska file (write index etc...)
 */
1131
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1132
gst_matroska_mux_finish (GstMatroskaMux * mux)
1133
{
1134
  GstEbmlWrite *ebml = mux->ebml_write;
1135
  guint64 pos;
1136
  guint64 duration = 0;
1137
  GSList *collected;
1138

1139
1140
1141
1142
1143
  /* finish last cluster */
  if (mux->cluster) {
    gst_ebml_write_master_finish (ebml, mux->cluster);
  }

1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
  /* cues */
  if (mux->index != NULL) {
    guint n;
    guint64 master, pointentry_master, trackpos_master;

    mux->cues_pos = ebml->pos;
    gst_ebml_write_set_cache (ebml, 12 + 41 * mux->num_indexes);
    master = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_CUES);

    for (n = 0; n < mux->num_indexes; n++) {
      GstMatroskaIndex *idx = &mux->index[n];

      pointentry_master = gst_ebml_write_master_start (ebml,
1157
          GST_MATROSKA_ID_POINTENTRY);
1158
      gst_ebml_write_uint (ebml, GST_MATROSKA_ID_CUETIME,
1159
          idx->time / mux->time_scale);
1160
      trackpos_master = gst_ebml_write_master_start (ebml,
1161
          GST_MATROSKA_ID_CUETRACKPOSITION);