summaryrefslogtreecommitdiffstats
path: root/gst/matroska
diff options
context:
space:
mode:
authorRonald S. Bultje <rbultje@ronald.bitfreak.net>2003-12-07 20:00:41 +0000
committerRonald S. Bultje <rbultje@ronald.bitfreak.net>2003-12-07 20:00:41 +0000
commit12e856c26d690c02cc05a2e945b49e2df538307a (patch)
treecbb1cf43fa0dcbb477775b3ade47e4b16e057b5e /gst/matroska
parent396a5c812901cfb1e16a8b4f55f122e229293b98 (diff)
Riff, EBML, fourcc etc. work. Not fully finished, but better than what we used to have and definately worth a first b...
Original commit message from CVS: Riff, EBML, fourcc etc. work. Not fully finished, but better than what we used to have and definately worth a first broad testing. I've revived rifflib. Rifflib used to be a bytestream-for-riff, which just dup'ed bytestream. I've rewritten rifflib to be a modern riff- chunk parser that uses bytestream fully, plus adds some extra functions so that riff file parsing becomes extremely easy. It also contains some small usability functions for strh/strf and metadata parsing. Note that it doesn't use the new tagging yet, that's a TODO. Avidemux has been rewritten to use this. I think we all agreed that avidemux was pretty much a big mess, which is because it used all sort of bytestream magic all around the place. It was just ugly. This is a lot nicer, very complete and safe. I think this is far more robust than what the old avidemux could ever have been. Of course, it might contain bugs, please let me know. EBML writing has also been implemented. This is useful for matroska. I'm intending to modify avidemux (with a riffwriter) similarly. Maybe I'll change wavparse/-enc too to use rifflib. Lastly, several plugins have been modified to use rifflib's fourcc parsing instead of their own. this puts fourcc parsing in one central place, which should make it a lot simpler to add new fourccs. We might want to move this to its own lib instead of rifflib. Enjoy!
Diffstat (limited to 'gst/matroska')
-rw-r--r--gst/matroska/Makefile.am8
-rw-r--r--gst/matroska/ebml-read.c127
-rw-r--r--gst/matroska/ebml-read.h4
-rw-r--r--gst/matroska/ebml-write.c580
-rw-r--r--gst/matroska/ebml-write.h126
-rw-r--r--gst/matroska/matroska-ids.h4
-rw-r--r--gst/matroska/matroska-mux.c961
-rw-r--r--gst/matroska/matroska-mux.h103
-rw-r--r--gst/matroska/matroska.c4
9 files changed, 1884 insertions, 33 deletions
diff --git a/gst/matroska/Makefile.am b/gst/matroska/Makefile.am
index b0235d89..7efb21af 100644
--- a/gst/matroska/Makefile.am
+++ b/gst/matroska/Makefile.am
@@ -2,14 +2,18 @@ plugin_LTLIBRARIES = libgstmatroska.la
libgstmatroska_la_SOURCES = \
ebml-read.c \
+ ebml-write.c \
matroska.c \
- matroska-demux.c
+ matroska-demux.c \
+ matroska-mux.c
noinst_HEADERS = \
ebml-ids.h \
ebml-read.h \
+ ebml-write.h \
matroska-demux.h \
- matroska-ids.h
+ matroska-ids.h \
+ matroska-mux.h
libgstmatroska_la_CFLAGS = $(GST_CFLAGS)
libgstmatroska_la_LIBADD =
diff --git a/gst/matroska/ebml-read.c b/gst/matroska/ebml-read.c
index a6b9225d..67b9b5f3 100644
--- a/gst/matroska/ebml-read.c
+++ b/gst/matroska/ebml-read.c
@@ -157,7 +157,20 @@ gst_ebml_read_element_id (GstEbmlRead *ebml,
guint32 total;
if (gst_bytestream_peek_bytes (ebml->bs, &data, 1) != 1) {
- /*gst_element_error (GST_ELEMENT (ebml), "Read error");*/
+ GstEvent *event = NULL;
+ guint32 remaining;
+
+ /* Here, we might encounter EOS */
+ gst_bytestream_get_status (ebml->bs, &remaining, &event);
+ if (event && GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
+ gst_pad_event_default (ebml->sinkpad, event);
+ } else {
+ guint64 pos = gst_bytestream_tell (ebml->bs);
+ gst_event_unref (event);
+ gst_element_error (GST_ELEMENT (ebml),
+ "Read error at position %llu (0x%llx)",
+ pos, pos);
+ }
return -1;
}
total = data[0];
@@ -166,13 +179,18 @@ gst_ebml_read_element_id (GstEbmlRead *ebml,
len_mask >>= 1;
}
if (read > 4) {
+ guint64 pos = gst_bytestream_tell (ebml->bs);
gst_element_error (GST_ELEMENT (ebml),
- "Invalid EBML ID size tag (0x%x)", data[0]);
+ "Invalid EBML ID size tag (0x%x) at position %llu (0x%llx)",
+ data[0], pos, pos);
return -1;
}
if (gst_bytestream_peek_bytes (ebml->bs, &data, read) != read) {
- /*gst_element_error (GST_ELEMENT (ebml), "Read error");*/
+ guint64 pos = gst_bytestream_tell (ebml->bs);
+ gst_element_error (GST_ELEMENT (ebml),
+ "Read error at position %llu (0x%llx)",
+ pos, pos);
return -1;
}
while (n < read)
@@ -201,7 +219,10 @@ gst_ebml_read_element_length (GstEbmlRead *ebml,
guint64 total;
if (gst_bytestream_peek_bytes (ebml->bs, &data, 1) != 1) {
- /*gst_element_error (GST_ELEMENT (ebml), "Read error");*/
+ guint64 pos = gst_bytestream_tell (ebml->bs);
+ gst_element_error (GST_ELEMENT (ebml),
+ "Read error at position %llu (0x%llx)",
+ pos, pos);
return -1;
}
total = data[0];
@@ -210,15 +231,20 @@ gst_ebml_read_element_length (GstEbmlRead *ebml,
len_mask >>= 1;
}
if (read > 8) {
+ guint64 pos = gst_bytestream_tell (ebml->bs);
gst_element_error (GST_ELEMENT (ebml),
- "Invalid EBML length size tag (0x%x)", data[0]);
+ "Invalid EBML length size tag (0x%x) at position %llu (0x%llx)",
+ data[0], pos, pos);
return -1;
}
if ((total &= (len_mask - 1)) == len_mask - 1)
num_ffs++;
if (gst_bytestream_peek_bytes (ebml->bs, &data, read) != read) {
- /*gst_element_error (GST_ELEMENT (ebml), "Read error");*/
+ guint64 pos = gst_bytestream_tell (ebml->bs);
+ gst_element_error (GST_ELEMENT (ebml),
+ "Read error at position %llu (0x%llx)",
+ pos, pos);
return -1;
}
while (n < read) {
@@ -228,12 +254,6 @@ gst_ebml_read_element_length (GstEbmlRead *ebml,
n++;
}
- if (!total) {
- gst_element_error (GST_ELEMENT (ebml),
- "Invalid length 0");
- return -1;
- }
-
if (read == num_ffs)
*length = G_MAXUINT64;
else
@@ -254,7 +274,10 @@ gst_ebml_read_element_data (GstEbmlRead *ebml,
GstBuffer *buf = NULL;
if (gst_bytestream_peek (ebml->bs, &buf, length) != length) {
- /*gst_element_error (GST_ELEMENT (ebml), "Read error");*/
+ guint64 pos = gst_bytestream_tell (ebml->bs);
+ gst_element_error (GST_ELEMENT (ebml),
+ "Read error at position %llu (0x%llx)",
+ pos, pos);
if (buf)
gst_buffer_unref (buf);
return NULL;
@@ -276,16 +299,12 @@ gst_ebml_peek_id (GstEbmlRead *ebml,
guint *level_up)
{
guint32 id;
- guint my_level_up;
- g_return_val_if_fail (level_up != NULL, 0);
+ g_assert (level_up);
- if (gst_ebml_read_element_id (ebml, &id, &my_level_up) < 0)
+ if (gst_ebml_read_element_id (ebml, &id, level_up) < 0)
return 0;
- if (level_up)
- *level_up = my_level_up;
-
return id;
}
@@ -293,11 +312,47 @@ gst_ebml_peek_id (GstEbmlRead *ebml,
* Seek to a given offset.
*/
-void
+GstEvent *
gst_ebml_read_seek (GstEbmlRead *ebml,
guint64 offset)
{
- gst_bytestream_seek (ebml->bs, offset, GST_SEEK_METHOD_SET);
+ guint32 remaining;
+ GstEvent *event;
+ guchar *data;
+
+ /* first, flush remaining buffers */
+ gst_bytestream_get_status (ebml->bs, &remaining, &event);
+ if (event) {
+ g_warning ("Unexpected event before seek");
+ gst_event_unref (event);
+ }
+ if (remaining)
+ gst_bytestream_flush_fast (ebml->bs, remaining);
+
+ /* now seek */
+ if (!gst_bytestream_seek (ebml->bs, offset, GST_SEEK_METHOD_SET)) {
+ gst_element_error (GST_ELEMENT (ebml),
+ "Seek to position %llu (0x%llx) failed",
+ offset, offset);
+ return NULL;
+ }
+
+ /* and now, peek a new byte. This will fail because there's a
+ * pending event. Then, take the event and return it. */
+ if (gst_bytestream_peek_bytes (ebml->bs, &data, 1))
+ g_warning ("Unexpected data after seek");
+
+ /* get the discont event and return */
+ gst_bytestream_get_status (ebml->bs, &remaining, &event);
+ if (!event || GST_EVENT_TYPE (event) != GST_EVENT_DISCONTINUOUS) {
+ gst_element_error (GST_ELEMENT (ebml),
+ "No discontinuity event after seek");
+ if (event)
+ gst_event_unref (event);
+ return NULL;
+ }
+
+ return event;
}
/*
@@ -308,8 +363,9 @@ gboolean
gst_ebml_read_skip (GstEbmlRead *ebml)
{
gint bytes;
- guint32 id;
+ guint32 id, remaining;
guint64 length;
+ GstEvent *event;
if ((bytes = gst_ebml_read_element_id (ebml, &id, NULL)) < 0)
return FALSE;
@@ -319,7 +375,23 @@ gst_ebml_read_skip (GstEbmlRead *ebml)
return FALSE;
gst_bytestream_flush_fast (ebml->bs, bytes);
- return gst_bytestream_flush (ebml->bs, length);
+ /* do we have enough bytes left to skip? */
+ gst_bytestream_get_status (ebml->bs, &remaining, &event);
+ if (event) {
+ g_warning ("Unexpected event before skip");
+ gst_event_unref (event);
+ }
+
+ if (remaining >= length)
+ return gst_bytestream_flush (ebml->bs, length);
+
+ if (!(event = gst_ebml_read_seek (ebml,
+ gst_bytestream_tell (ebml->bs) + length)))
+ return FALSE;
+
+ gst_event_unref (event);
+
+ return TRUE;
}
/*
@@ -365,7 +437,8 @@ gst_ebml_read_uint (GstEbmlRead *ebml,
size = GST_BUFFER_SIZE (buf);
if (size < 1 || size > 8) {
gst_element_error (GST_ELEMENT (ebml),
- "Invalid integer element size %d", size);
+ "Invalid integer element size %d at position %llu (0x%llu)",
+ size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf));
gst_buffer_unref (buf);
return FALSE;
}
@@ -400,7 +473,8 @@ gst_ebml_read_sint (GstEbmlRead *ebml,
size = GST_BUFFER_SIZE (buf);
if (size < 1 || size > 8) {
gst_element_error (GST_ELEMENT (ebml),
- "Invalid integer element size %d", size);
+ "Invalid integer element size %d at position %llu (0x%llx)",
+ size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf));
gst_buffer_unref (buf);
return FALSE;
}
@@ -439,7 +513,8 @@ gst_ebml_read_float (GstEbmlRead *ebml,
if (size != 4 && size != 8 && size != 10) {
gst_element_error (GST_ELEMENT (ebml),
- "Invalid float element size %d", size);
+ "Invalid float element size %d at position %llu (0x%llx)",
+ size, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf));
gst_buffer_unref (buf);
return FALSE;
}
diff --git a/gst/matroska/ebml-read.h b/gst/matroska/ebml-read.h
index 52e4e141..d78d13ce 100644
--- a/gst/matroska/ebml-read.h
+++ b/gst/matroska/ebml-read.h
@@ -63,7 +63,7 @@ GType gst_ebml_read_get_type (void);
guint32 gst_ebml_peek_id (GstEbmlRead *ebml,
guint *level_up);
-void gst_ebml_read_seek (GstEbmlRead *ebml,
+GstEvent *gst_ebml_read_seek (GstEbmlRead *ebml,
guint64 offset);
gboolean gst_ebml_read_skip (GstEbmlRead *ebml);
gboolean gst_ebml_read_buffer (GstEbmlRead *ebml,
@@ -91,7 +91,7 @@ gboolean gst_ebml_read_master (GstEbmlRead *ebml,
guint32 *id);
gboolean gst_ebml_read_binary (GstEbmlRead *ebml,
guint32 *id,
- guchar **binary,
+ guint8 **binary,
guint64 *length);
gboolean gst_ebml_read_header (GstEbmlRead *read,
gchar **doctype,
diff --git a/gst/matroska/ebml-write.c b/gst/matroska/ebml-write.c
new file mode 100644
index 00000000..9c768953
--- /dev/null
+++ b/gst/matroska/ebml-write.c
@@ -0,0 +1,580 @@
+/* GStreamer EBML I/O
+ * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
+ *
+ * ebml-write.c: write EBML data to file/stream
+ *
+ * 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 <string.h>
+
+#include "ebml-write.h"
+#include "ebml-ids.h"
+
+enum {
+ /* FILL ME */
+ LAST_SIGNAL
+};
+
+static void gst_ebml_write_class_init (GstEbmlWriteClass *klass);
+static void gst_ebml_write_init (GstEbmlWrite *ebml);
+static GstElementStateReturn
+ gst_ebml_write_change_state (GstElement *element);
+
+static GstElementClass *parent_class = NULL;
+
+GType
+gst_ebml_write_get_type (void)
+{
+ static GType gst_ebml_write_type = 0;
+
+ if (!gst_ebml_write_type) {
+ static const GTypeInfo gst_ebml_write_info = {
+ sizeof (GstEbmlWriteClass),
+ NULL,
+ NULL,
+ (GClassInitFunc) gst_ebml_write_class_init,
+ NULL,
+ NULL,
+ sizeof (GstEbmlWrite),
+ 0,
+ (GInstanceInitFunc) gst_ebml_write_init,
+ };
+
+ gst_ebml_write_type =
+ g_type_register_static (GST_TYPE_ELEMENT, "GstEbmlWrite",
+ &gst_ebml_write_info, 0);
+ }
+
+ return gst_ebml_write_type;
+}
+
+static void
+gst_ebml_write_class_init (GstEbmlWriteClass *klass)
+{
+ GstElementClass *gstelement_class = (GstElementClass *) klass;
+
+ parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
+
+ gstelement_class->change_state = gst_ebml_write_change_state;
+}
+
+static void
+gst_ebml_write_init (GstEbmlWrite *ebml)
+{
+ ebml->srcpad = NULL;
+ ebml->pos = 0;
+
+ ebml->cache = NULL;
+}
+
+static GstElementStateReturn
+gst_ebml_write_change_state (GstElement *element)
+{
+ GstEbmlWrite *ebml = GST_EBML_WRITE (element);
+
+ switch (GST_STATE_TRANSITION (element)) {
+ case GST_STATE_PAUSED_TO_READY:
+ ebml->pos = 0;
+ break;
+ default:
+ break;
+ }
+
+ if (GST_ELEMENT_CLASS (parent_class)->change_state)
+ return GST_ELEMENT_CLASS (parent_class)->change_state (element);
+
+ return GST_STATE_SUCCESS;
+}
+
+/*
+ * Caching.
+ *
+ * The idea is that you use this for writing a lot
+ * of small elements. This will just "queue" all of
+ * them and they'll be pushed to the next element all
+ * at once. This saves memory and time for buffer
+ * allocation and init, and it looks better.
+ */
+
+void
+gst_ebml_write_set_cache (GstEbmlWrite *ebml,
+ guint size)
+{
+ return;
+ g_return_if_fail (ebml->cache == NULL);
+
+ ebml->cache = gst_buffer_new_and_alloc (size);
+ GST_BUFFER_SIZE (ebml->cache) = 0;
+ GST_BUFFER_OFFSET (ebml->cache) = ebml->pos;
+ ebml->handled = 0;
+}
+
+void
+gst_ebml_write_flush_cache (GstEbmlWrite *ebml)
+{
+ if (!ebml->cache)
+ return;
+
+ /* this is very important. It may fail, in which case the client
+ * programmer didn't use the cache somewhere. That's fatal. */
+ g_assert (ebml->handled == GST_BUFFER_SIZE (ebml->cache));
+ g_assert (GST_BUFFER_SIZE (ebml->cache) +
+ GST_BUFFER_OFFSET (ebml->cache) == ebml->pos);
+
+ gst_pad_push (ebml->srcpad, GST_DATA (ebml->cache));
+ ebml->cache = NULL;
+ ebml->handled = 0;
+}
+
+/*
+ * One-element buffer, in case of no cache. If there is
+ * a cache, use that instead.
+ */
+
+static GstBuffer *
+gst_ebml_write_element_new (GstEbmlWrite *ebml,
+ guint size)
+{
+ /* Create new buffer of size + ID + length */
+ GstBuffer *buf;
+
+ /* length, ID */
+ size += 12;
+
+ /* prefer cache */
+ if (ebml->cache) {
+ if (GST_BUFFER_MAXSIZE (ebml->cache) -
+ GST_BUFFER_SIZE (ebml->cache) < size) {
+ GST_LOG ("Cache available, but too small. Clearing...");
+ gst_ebml_write_flush_cache (ebml);
+ } else {
+ return ebml->cache;
+ }
+ }
+
+ /* else, use a one-element buffer. This is slower */
+ buf = gst_buffer_new_and_alloc (size);
+ GST_BUFFER_SIZE (buf) = 0;
+
+ return buf;
+}
+
+/*
+ * Write element ID into a buffer.
+ */
+
+static void
+gst_ebml_write_element_id (GstBuffer *buf,
+ guint32 id)
+{
+ guint8 *data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
+ guint bytes = 4, mask = 0x10;
+
+ /* get ID length */
+ while (!(id & (mask << ((bytes - 1) * 8))) && bytes > 0) {
+ mask <<= 1;
+ bytes--;
+ }
+
+ /* if invalid ID, use dummy */
+ if (bytes == 0) {
+ GST_WARNING ("Invalid ID, voiding");
+ bytes = 1;
+ id = GST_EBML_ID_VOID;
+ }
+
+ /* write out, BE */
+ GST_BUFFER_SIZE (buf) += bytes;
+ while (bytes--) {
+ data[bytes] = id & 0xff;
+ id >>= 8;
+ }
+}
+
+/*
+ * Write element length into a buffer.
+ */
+
+static void
+gst_ebml_write_element_size (GstBuffer *buf,
+ guint64 size)
+{
+ guint8 *data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
+ guint bytes = 1, mask = 0x80;
+
+ /* how many bytes? */
+ while ((size >> ((bytes - 1) * 8)) >= mask && bytes <= 8) {
+ mask >>= 1;
+ bytes++;
+ }
+
+ /* if invalid size, use max. */
+ if (bytes > 8) {
+ GST_WARNING ("Invalid size, maximizing");
+ mask = 0x01;
+ bytes = 8;
+ /* Now here's a real FIXME: we cannot read those yet! */
+ size = 0x00ffffffffffffffLLU;
+ }
+
+ /* write out, BE, with length size marker */
+ GST_BUFFER_SIZE (buf) += bytes;
+ while (bytes-- > 0) {
+ data[bytes] = size & 0xff;
+ size >>= 8;
+ if (!bytes)
+ *data |= mask;
+ }
+}
+
+/*
+ * Write element data into a buffer.
+ */
+
+static void
+gst_ebml_write_element_data (GstBuffer *buf,
+ guint8 *write,
+ guint64 length)
+{
+ guint8 *data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
+
+ memcpy (data, write, length);
+ GST_BUFFER_SIZE (buf) += length;
+}
+
+/*
+ * Write out buffer by moving it to the next element.
+ */
+
+static void
+gst_ebml_write_element_push (GstEbmlWrite *ebml,
+ GstBuffer *buf)
+{
+ guint data_size = GST_BUFFER_SIZE (buf) - ebml->handled;
+
+ ebml->pos += data_size;
+ if (buf == ebml->cache) {
+ ebml->handled += data_size;
+ }
+
+ /* if there's no cache, then don't push it! */
+ if (ebml->cache)
+ g_assert (buf == ebml->cache);
+ else
+ gst_pad_push (ebml->srcpad, GST_DATA (buf));
+}
+
+/*
+ * Seek.
+ */
+
+void
+gst_ebml_write_seek (GstEbmlWrite *ebml,
+ guint64 pos)
+{
+ GstEvent *seek;
+
+ /* Cache seeking. A bit dangerous, we assume the client writer
+ * knows what he's doing... */
+ if (ebml->cache) {
+ /* within bounds? */
+ if (pos >= GST_BUFFER_OFFSET (ebml->cache) &&
+ pos < GST_BUFFER_OFFSET (ebml->cache) + GST_BUFFER_MAXSIZE (ebml->cache)) {
+ GST_BUFFER_SIZE (ebml->cache) = pos - GST_BUFFER_OFFSET (ebml->cache);
+ if (ebml->pos > pos)
+ ebml->handled -= ebml->pos - pos;
+ else
+ ebml->handled += pos - ebml->pos;
+ ebml->pos = pos;
+ } else {
+ GST_LOG ("Seek outside cache range. Clearing...");
+ gst_ebml_write_flush_cache (ebml);
+ }
+ }
+
+ seek = gst_event_new_seek (GST_FORMAT_BYTES |
+ GST_SEEK_METHOD_SET,
+ pos);
+ gst_pad_push (ebml->srcpad, GST_DATA (seek));
+ ebml->pos = pos;
+}
+
+/*
+ * Get no. bytes needed to write a uint.
+ */
+
+static guint
+gst_ebml_write_get_uint_size (guint64 num)
+{
+ guint size = 1;
+
+ /* get size */
+ while (num >= (1LLU << (size * 8)) && size < 8) {
+ size++;
+ }
+
+ return size;
+}
+
+
+/*
+ * Write an uint into a buffer.
+ */
+
+static void
+gst_ebml_write_set_uint (GstBuffer *buf,
+ guint64 num,
+ guint size)
+{
+ guint8 *data;
+
+ data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
+ GST_BUFFER_SIZE (buf) += size;
+ while (size-- > 0) {
+ data[size] = num & 0xff;
+ num >>= 8;
+ }
+}
+
+/*
+ * Data type wrappers.
+ */
+
+void
+gst_ebml_write_uint (GstEbmlWrite *ebml,
+ guint32 id,
+ guint64 num)
+{
+ GstBuffer *buf = gst_ebml_write_element_new (ebml, sizeof (num));
+ guint size = gst_ebml_write_get_uint_size (num);
+
+ /* write */
+ gst_ebml_write_element_id (buf, id);
+ gst_ebml_write_element_size (buf, size);
+ gst_ebml_write_set_uint (buf, num, size);
+ gst_ebml_write_element_push (ebml, buf);
+}
+
+void
+gst_ebml_write_sint (GstEbmlWrite *ebml,
+ guint32 id,
+ gint64 num)
+{
+ GstBuffer *buf = gst_ebml_write_element_new (ebml, sizeof (num));
+ /* if the signed number is on the edge of a extra-byte,
+ * then we'll fall over when detecting it. Example: if I
+ * have a number (-)0x8000 (G_MINSHORT), then my abs()<<1
+ * will be 0x10000; this is G_MAXUSHORT+1! So: if (<0) -1. */
+ guint64 unum = (num < 0 ? (-num - 1) << 1 : num << 1);
+ guint size = gst_ebml_write_get_uint_size (unum);
+
+ /* make unsigned */
+ unum = (num < 0 ? -num : num) + (1LLU << ((8 * size) - 1));
+
+ /* write */
+ gst_ebml_write_element_id (buf, id);
+ gst_ebml_write_element_size (buf, size);
+ gst_ebml_write_set_uint (buf, unum, size);
+ gst_ebml_write_element_push (ebml, buf);
+}
+
+void
+gst_ebml_write_float (GstEbmlWrite *ebml,
+ guint32 id,
+ gdouble num)
+{
+ gint n;
+ GstBuffer *buf = gst_ebml_write_element_new (ebml, sizeof (num));
+
+ gst_ebml_write_element_id (buf, id);
+ gst_ebml_write_element_size (buf, 8);
+#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
+ for (n = 0; n < 8; n++)
+ GST_BUFFER_DATA (buf)[GST_BUFFER_SIZE (buf)] = ((guint8 *) &num)[7-n];
+ GST_BUFFER_SIZE (buf) += 8;
+#else
+ gst_ebml_write_element_data (buf, (guint8 *) &num, 8);
+#endif
+ gst_ebml_write_element_push (ebml, buf);
+}
+
+void
+gst_ebml_write_ascii (GstEbmlWrite *ebml,
+ guint32 id,
+ const gchar *str)
+{
+ gint len = strlen (str) + 1; /* add trailing '\0' */
+ GstBuffer *buf = gst_ebml_write_element_new (ebml, len);
+
+ gst_ebml_write_element_id (buf, id);
+ gst_ebml_write_element_size (buf, len);
+ gst_ebml_write_element_data (buf, (guint8 *) str, len);
+ gst_ebml_write_element_push (ebml, buf);
+}
+
+void
+gst_ebml_write_utf8 (GstEbmlWrite *ebml,
+ guint32 id,
+ const gchar *str)
+{
+ gst_ebml_write_ascii (ebml, id, str);
+}
+
+void
+gst_ebml_write_date (GstEbmlWrite *ebml,
+ guint32 id,
+ gint64 date)
+{
+ gst_ebml_write_sint (ebml, id, date);
+}
+
+/*
+ * Master writing is annoying. We use a size marker of
+ * the max. allowed length, so that we can later fill it
+ * in validly.
+ */
+
+guint64
+gst_ebml_write_master_start (GstEbmlWrite *ebml,
+ guint32 id)
+{
+ guint64 pos = ebml->pos, t;
+ GstBuffer *buf = gst_ebml_write_element_new (ebml, 0);
+
+ t = GST_BUFFER_SIZE (buf);
+ gst_ebml_write_element_id (buf, id);
+ pos += GST_BUFFER_SIZE (buf) - t;
+ gst_ebml_write_element_size (buf, -1);
+ gst_ebml_write_element_push (ebml, buf);
+
+ return pos;
+}
+
+void
+gst_ebml_write_master_finish (GstEbmlWrite *ebml,
+ guint64 startpos)
+{
+ guint64 pos = ebml->pos;
+ GstBuffer *buf;
+
+ gst_ebml_write_seek (ebml, startpos);
+ buf = gst_ebml_write_element_new (ebml, 0);
+ startpos = GUINT64_TO_BE ((1LLU << 56) | (pos - startpos - 8));
+ memcpy (GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf),
+ (guint8 *) &startpos, 8);
+ GST_BUFFER_SIZE (buf) += 8;
+ gst_ebml_write_element_push (ebml, buf);
+ gst_ebml_write_seek (ebml, pos);
+}
+
+void
+gst_ebml_write_binary (GstEbmlWrite *ebml,
+ guint32 id,
+ guint8 *binary,
+ guint64 length)
+{
+ GstBuffer *buf = gst_ebml_write_element_new (ebml, length);
+
+ gst_ebml_write_element_id (buf, id);
+ gst_ebml_write_element_size (buf, length);
+ gst_ebml_write_element_data (buf, binary, length);
+ gst_ebml_write_element_push (ebml, buf);
+}
+
+/*
+ * For things like video frames and audio samples,
+ * you want to use this function, as it doesn't have
+ * the overhead of memcpy() that other functions
+ * such as write_binary() do have.
+ */
+
+void
+gst_ebml_write_buffer_header (GstEbmlWrite *ebml,
+ guint32 id,
+ guint64 length)
+{
+ GstBuffer *buf = gst_ebml_write_element_new (ebml, 0);
+
+ gst_ebml_write_element_id (buf, id);
+ gst_ebml_write_element_size (buf, length);
+ gst_ebml_write_element_push (ebml, buf);
+}
+
+void
+gst_ebml_write_buffer (GstEbmlWrite *ebml,
+ GstBuffer *data)
+{
+ gst_ebml_write_element_push (ebml, data);
+}
+
+/*
+ * When replacing a uint, we assume that it is *always*
+ * 8-byte, since that's the safest guess we can do. This
+ * is just for simplicity.
+ *
+ * FIXME: this function needs to be replaced with something
+ * proper. This is a crude hack.
+ */
+
+void
+gst_ebml_replace_uint (GstEbmlWrite *ebml,
+ guint64 pos,
+ guint64 num)
+{
+ guint64 oldpos = ebml->pos;
+ GstBuffer *buf = gst_buffer_new_and_alloc (8);
+
+ gst_ebml_write_seek (ebml, pos);
+ GST_BUFFER_SIZE (buf) = 0;
+ gst_ebml_write_set_uint (buf, num, 8);
+ gst_ebml_write_element_push (ebml, buf);
+ gst_ebml_write_seek (ebml, oldpos);
+}
+
+/*
+ * Write EBML header.
+ */
+
+void
+gst_ebml_write_header (GstEbmlWrite *ebml,
+ gchar *doctype,
+ guint version)
+{
+ guint64 pos;
+
+ /* write the basic EBML header */
+ gst_ebml_write_set_cache (ebml, 0x40);
+ pos = gst_ebml_write_master_start (ebml, GST_EBML_ID_HEADER);
+#if (GST_EBML_VERSION != 1)
+ gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLVERSION, GST_EBML_VERSION);
+ gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLREADVERSION, GST_EBML_VERSION);
+#endif
+#if 0
+ /* we don't write these until they're "non-default" (never!) */
+ gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLMAXIDLENGTH, sizeof (guint32));
+ gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLMAXSIZELENGTH, sizeof (guint64));
+#endif
+ gst_ebml_write_ascii (ebml, GST_EBML_ID_DOCTYPE, doctype);
+ gst_ebml_write_uint (ebml, GST_EBML_ID_DOCTYPEVERSION, version);
+ gst_ebml_write_uint (ebml, GST_EBML_ID_DOCTYPEREADVERSION, version);
+ gst_ebml_write_master_finish (ebml, pos);
+ gst_ebml_write_flush_cache (ebml);
+}
diff --git a/gst/matroska/ebml-write.h b/gst/matroska/ebml-write.h
new file mode 100644
index 00000000..43d2f8b2
--- /dev/null
+++ b/gst/matroska/ebml-write.h
@@ -0,0 +1,126 @@
+/* GStreamer EBML I/O
+ * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
+ *
+ * ebml-write.c: write EBML data to file/stream
+ *
+ * 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.
+ */
+
+#ifndef __GST_EBML_WRITE_H__
+#define __GST_EBML_WRITE_H__
+
+#include <glib.h>
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+
+#define GST_TYPE_EBML_WRITE \
+ (gst_ebml_write_get_type ())
+#define GST_EBML_WRITE(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_EBML_WRITE, GstEbmlWrite))
+#define GST_EBML_WRITE_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_EBML_WRITE, GstEbmlWriteClass))
+#define GST_IS_EBML_WRITE(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_EBML_WRITE))
+#define GST_IS_EBML_WRITE_CLASS(obj) \
+ (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_EBML_WRITE))
+#define GST_EBML_WRITE_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_EBML_WRITE, GstEbmlWriteClass))
+
+typedef struct _GstEbmlWrite {
+ GstElement parent;
+
+ GstPad *srcpad;
+ guint64 pos;
+
+ GstBuffer *cache;
+ guint handled;
+} GstEbmlWrite;
+
+typedef struct _GstEbmlWriteClass {
+ GstElementClass parent;
+} GstEbmlWriteClass;
+
+GType gst_ebml_write_get_type (void);
+
+/*
+ * Caching means that we do not push one buffer for
+ * each element, but fill this one until a flush.
+ */
+void gst_ebml_write_set_cache (GstEbmlWrite *ebml,
+ guint size);
+void gst_ebml_write_flush_cache (GstEbmlWrite *ebml);
+
+/*
+ * Seeking.
+ */
+void gst_ebml_write_seek (GstEbmlWrite *ebml,
+ guint64 pos);
+
+/*
+ * Data writing.
+ */
+void gst_ebml_write_uint (GstEbmlWrite *ebml,
+ guint32 id,
+ guint64 num);
+void gst_ebml_write_sint (GstEbmlWrite *ebml,
+ guint32 id,
+ gint64 num);
+void gst_ebml_write_float (GstEbmlWrite *ebml,
+ guint32 id,
+ gdouble num);
+void gst_ebml_write_ascii (GstEbmlWrite *ebml,
+ guint32 id,
+ const gchar *str);
+void gst_ebml_write_utf8 (GstEbmlWrite *ebml,
+ guint32 id,
+ const gchar *str);
+void gst_ebml_write_date (GstEbmlWrite *ebml,
+ guint32 id,
+ gint64 date);
+guint64 gst_ebml_write_master_start (GstEbmlWrite *ebml,
+ guint32 id);
+void gst_ebml_write_master_finish (GstEbmlWrite *ebml,
+ guint64 startpos);
+void gst_ebml_write_binary (GstEbmlWrite *ebml,
+ guint32 id,
+ guchar *binary,
+ guint64 length);
+void gst_ebml_write_header (GstEbmlWrite *ebml,
+ gchar *doctype,
+ guint version);
+
+/*
+ * Note: this is supposed to be used only for media data.
+ */
+void gst_ebml_write_buffer_header (GstEbmlWrite *ebml,
+ guint32 id,
+ guint64 length);
+void gst_ebml_write_buffer (GstEbmlWrite *ebml,
+ GstBuffer *data);
+
+/*
+ * A hack, basically... See matroska-mux.c. I should actually
+ * make a nice _replace_element_with_size() or so, but this
+ * works for now.
+ */
+void gst_ebml_replace_uint (GstEbmlWrite *ebml,
+ guint64 pos,
+ guint64 num);
+
+G_END_DECLS
+
+#endif /* __GST_EBML_WRITE_H__ */
diff --git a/gst/matroska/matroska-ids.h b/gst/matroska/matroska-ids.h
index 455925ef..45c13baa 100644
--- a/gst/matroska/matroska-ids.h
+++ b/gst/matroska/matroska-ids.h
@@ -25,7 +25,7 @@
#include "ebml-ids.h"
/*
- * EBML element IDs. max. 32-bit.
+ * Matroska element IDs. max. 32-bit.
*/
/* toplevel segment */
@@ -141,7 +141,7 @@
#define GST_MATROSKA_CODEC_ID_AUDIO_ACM "A_MS/ACM"
#define GST_MATROSKA_CODEC_ID_AUDIO_MPEG2 "A_AAC/MPEG2/"
#define GST_MATROSKA_CODEC_ID_AUDIO_MPEG4 "A_AAC/MPEG4/"
-/* TODO: AC3-9/10, Real, Musepack, Quicktime */
+/* TODO: AC3-9/10 (?), Real, Musepack, Quicktime */
/*
* Enumerations for various types (mapping from binary
diff --git a/gst/matroska/matroska-mux.c b/gst/matroska/matroska-mux.c
new file mode 100644
index 00000000..2f268d8a
--- /dev/null
+++ b/gst/matroska/matroska-mux.c
@@ -0,0 +1,961 @@
+/* GStreamer Matroska muxer/demuxer
+ * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
+ *
+ * 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"
+
+enum {
+ /* FILL ME */
+ LAST_SIGNAL
+};
+
+enum {
+ ARG_0,
+ ARG_METADATA,
+ /* FILL ME */
+};
+
+GST_PAD_TEMPLATE_FACTORY (src_templ,
+ "src",
+ GST_PAD_SRC,
+ GST_PAD_ALWAYS,
+ GST_CAPS_NEW (
+ "matroskamux_src",
+ "video/x-matroska",
+ NULL
+ )
+)
+
+/* FIXME: caps */
+
+GST_PAD_TEMPLATE_FACTORY (videosink_templ,
+ "video_%d",
+ GST_PAD_SINK,
+ GST_PAD_REQUEST,
+ NULL
+)
+
+GST_PAD_TEMPLATE_FACTORY (audiosink_templ,
+ "audio_%d",
+ GST_PAD_SINK,
+ GST_PAD_REQUEST,
+ NULL
+)
+
+GST_PAD_TEMPLATE_FACTORY (subtitlesink_templ,
+ "subtitle_%d",
+ GST_PAD_SINK,
+ GST_PAD_REQUEST,
+ NULL
+)
+
+/* gobject magic foo */
+static void gst_matroska_mux_base_init (GstMatroskaMuxClass *klass);
+static void gst_matroska_mux_class_init (GstMatroskaMuxClass *klass);
+static void gst_matroska_mux_init (GstMatroskaMux *mux);
+
+/* element functions */
+static void gst_matroska_mux_loop (GstElement *element);
+
+/* pad functions */
+static GstPad * gst_matroska_mux_request_new_pad (GstElement *element,
+ GstPadTemplate *templ,
+ const gchar *name);
+
+/* gst internal change state handler */
+static GstElementStateReturn
+ gst_matroska_mux_change_state (GstElement *element);
+
+/* gobject bla bla */
+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);
+
+/* reset muxer */
+static void gst_matroska_mux_reset (GstElement *element);
+
+static GstEbmlWriteClass *parent_class = NULL;
+/*static guint gst_matroska_mux_signals[LAST_SIGNAL] = { 0 };*/
+
+GType
+gst_matroska_mux_get_type (void)
+{
+ static GType gst_matroska_mux_type = 0;
+
+ if (!gst_matroska_mux_type) {
+ static const GTypeInfo gst_matroska_mux_info = {
+ sizeof (GstMatroskaMuxClass),
+ (GBaseInitFunc) gst_matroska_mux_base_init,
+ NULL,
+ (GClassInitFunc) gst_matroska_mux_class_init,
+ NULL,
+ NULL,
+ sizeof (GstMatroskaMux),
+ 0,
+ (GInstanceInitFunc) gst_matroska_mux_init,
+ };
+
+ gst_matroska_mux_type =
+ g_type_register_static (GST_TYPE_EBML_WRITE,
+ "GstMatroskaMmux",
+ &gst_matroska_mux_info, 0);
+ }
+
+ return gst_matroska_mux_type;
+}
+
+static void
+gst_matroska_mux_base_init (GstMatroskaMuxClass *klass)
+{
+ GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
+ static GstElementDetails gst_matroska_mux_details = {
+ "Matroska muxer",
+ "Codec/Muxer",
+ "Muxes video/audio/subtitle streams into a matroska stream",
+ "Ronald Bultje <rbultje@ronald.bitfreak.net>"
+ };
+
+ gst_element_class_add_pad_template (element_class,
+ GST_PAD_TEMPLATE_GET (videosink_templ));
+ gst_element_class_add_pad_template (element_class,
+ GST_PAD_TEMPLATE_GET (audiosink_templ));
+ gst_element_class_add_pad_template (element_class,
+ GST_PAD_TEMPLATE_GET (subtitlesink_templ));
+ gst_element_class_add_pad_template (element_class,
+ GST_PAD_TEMPLATE_GET (src_templ));
+ gst_element_class_set_details (element_class,
+ &gst_matroska_mux_details);
+}
+
+static void
+gst_matroska_mux_class_init (GstMatroskaMuxClass *klass)
+{
+ GObjectClass *gobject_class;
+ GstElementClass *gstelement_class;
+
+ gobject_class = (GObjectClass *) klass;
+ gstelement_class = (GstElementClass *) klass;
+
+ g_object_class_install_property (gobject_class, ARG_METADATA,
+ g_param_spec_boxed ("metadata", "Metadata", "Metadata",
+ GST_TYPE_CAPS, G_PARAM_READWRITE));
+
+ parent_class = g_type_class_ref (GST_TYPE_EBML_WRITE);
+
+ gobject_class->get_property = gst_matroska_mux_get_property;
+ gobject_class->set_property = gst_matroska_mux_set_property;
+
+ gstelement_class->change_state = gst_matroska_mux_change_state;
+ gstelement_class->request_new_pad = gst_matroska_mux_request_new_pad;
+}
+
+static void
+gst_matroska_mux_init (GstMatroskaMux *mux)
+{
+ GstElementClass *klass = GST_ELEMENT_GET_CLASS (mux);
+ gint i;
+
+ mux->srcpad = gst_pad_new_from_template (
+ gst_element_class_get_pad_template (klass, "src"), "src");
+ gst_element_add_pad (GST_ELEMENT (mux), mux->srcpad);
+ GST_EBML_WRITE (mux)->srcpad = mux->srcpad;
+
+ gst_element_set_loop_function (GST_ELEMENT (mux),
+ gst_matroska_mux_loop);
+
+ /* initial stream no. */
+ for (i = 0; i < GST_MATROSKA_MUX_MAX_STREAMS; i++) {
+ mux->sink[i].buffer = NULL;
+ mux->sink[i].track = NULL;
+ }
+ mux->index = NULL;
+
+ /* finish off */
+ gst_matroska_mux_reset (GST_ELEMENT (mux));
+}
+
+static void
+gst_matroska_mux_reset (GstElement *element)
+{
+ GstMatroskaMux *mux = GST_MATROSKA_MUX (element);
+ guint i;
+
+ /* reset input */
+ mux->state = GST_MATROSKA_MUX_STATE_START;
+
+ /* clean up existing streams */
+ for (i = 0; i < GST_MATROSKA_MUX_MAX_STREAMS; i++) {
+ if (mux->sink[i].track != NULL) {
+ if (mux->sink[i].track->pad != NULL) {
+ gst_element_remove_pad (GST_ELEMENT (mux), mux->sink[i].track->pad);
+ }
+ g_free (mux->sink[i].track->codec_id);
+ g_free (mux->sink[i].track->codec_name);
+ g_free (mux->sink[i].track->name);
+ g_free (mux->sink[i].track->language);
+ g_free (mux->sink[i].track->codec_priv);
+ g_free (mux->sink[i].track);
+ mux->sink[i].track = NULL;
+ }
+ if (mux->sink[i].buffer != NULL) {
+ gst_buffer_unref (mux->sink[i].buffer);
+ mux->sink[i].buffer = NULL;
+ }
+ mux->sink[i].eos = FALSE;
+ }
+ mux->num_streams = 0;
+ mux->num_a_streams = 0;
+ mux->num_t_streams = 0;
+ mux->num_v_streams = 0;
+
+ /* reset media info (to default) */
+ gst_caps_replace (&mux->metadata,
+ GST_CAPS_NEW ("matroska_metadata",
+ "application/x-gst-metadata",
+ "application", GST_PROPS_STRING (""),
+ "date", GST_PROPS_STRING ("")));
+
+ /* reset indexes */
+ mux->num_indexes = 0;
+ g_free (mux->index);
+ mux->index = NULL;
+
+ /* reset timers */
+ mux->time_scale = 1000000;
+ mux->duration = 0;
+}
+
+static GstPadLinkReturn
+gst_matroska_mux_video_pad_link (GstPad *pad,
+ GstCaps *caps)
+{
+ GstMatroskaTrackContext *context = NULL;
+ GstMatroskaTrackVideoContext *videocontext;
+ GstMatroskaMux *mux = GST_MATROSKA_MUX (gst_pad_get_parent (pad));
+ const gchar *mimetype;
+ gint width, height, pixel_width, pixel_height, i;
+ gfloat framerate;
+
+ if (!GST_CAPS_IS_FIXED (caps))
+ return GST_PAD_LINK_DELAYED;
+
+ /* find context */
+ for (i = 0; i < mux->num_streams; i++) {
+ if (mux->sink[i].track && mux->sink[i].track->pad &&
+ mux->sink[i].track->pad == pad) {
+ context = mux->sink[i].track;
+ break;
+ }
+ }
+ g_assert (i < mux->num_streams);
+ g_assert (context->type == GST_MATROSKA_TRACK_TYPE_VIDEO);
+ videocontext = (GstMatroskaTrackVideoContext *) context;
+
+ /* gst -> matroska ID'ing */
+ for (; caps != NULL; caps = caps->next) {
+ mimetype = gst_caps_get_mime (caps);
+
+ /* get general properties */
+ gst_caps_get (caps,
+ "width", &width,
+ "height", &height,
+ "framerate", &framerate,
+ NULL);
+ videocontext->pixel_width = width;
+ videocontext->pixel_height = height;
+ context->default_duration = GST_SECOND / framerate;
+
+ if (gst_caps_has_property (caps, "pixel_width") &&
+ gst_caps_has_property (caps, "pixel_height")) {
+ gst_caps_get (caps,
+ "pixel_width", &pixel_width,
+ "pixel_height", &pixel_height,
+ NULL);
+ 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;
+ } else {
+ videocontext->display_width = 0;
+ videocontext->display_height = 0;
+ }
+ } else {
+ videocontext->display_width = 0;
+ videocontext->display_height = 0;
+ }
+
+ 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_caps_get_fourcc_int (caps, "format", &videocontext->fourcc);
+
+ return GST_PAD_LINK_OK;
+ } else if (!strcmp (mimetype, "video/x-jpeg")) {
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MJPEG);
+
+ return GST_PAD_LINK_OK;
+ } else if (!strcmp (mimetype, "video/x-divx")) {
+ gint divxversion;
+
+ gst_caps_get_int (caps, "divxversion", &divxversion);
+ switch (divxversion) {
+ case 3:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3);
+ break;
+ case 4:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_SP);
+ break;
+ case 5:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP);
+ break;
+ }
+
+ return GST_PAD_LINK_OK;
+ } else if (!strcmp (mimetype, "video/x-xvid")) {
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP);
+
+ return GST_PAD_LINK_OK;
+ } else if (!strcmp (mimetype, "video/mpeg")) {
+ gint mpegversion;
+
+ gst_caps_get_int (caps, "mpegversion", &mpegversion);
+ switch (mpegversion) {
+ case 1:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MPEG1);
+ break;
+ case 2:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MPEG2);
+ break;
+ case 3:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP);
+ break;
+ }
+
+ return GST_PAD_LINK_OK;
+ } else if (!strcmp (mimetype, "video/x-msmpeg")) {
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3);
+
+ return GST_PAD_LINK_OK;
+ }
+ }
+
+ return GST_PAD_LINK_REFUSED;
+}
+
+static GstPadLinkReturn
+gst_matroska_mux_audio_pad_link (GstPad *pad,
+ GstCaps *caps)
+{
+ GstMatroskaTrackContext *context = NULL;
+ GstMatroskaTrackAudioContext *audiocontext;
+ GstMatroskaMux *mux = GST_MATROSKA_MUX (gst_pad_get_parent (pad));
+ const gchar *mimetype;
+ gint samplerate, channels, i;
+
+ if (!GST_CAPS_IS_FIXED (caps))
+ return GST_PAD_LINK_DELAYED;
+
+ /* find context */
+ for (i = 0; i < mux->num_streams; i++) {
+ if (mux->sink[i].track && mux->sink[i].track->pad &&
+ mux->sink[i].track->pad == pad) {
+ context = mux->sink[i].track;
+ break;
+ }
+ }
+ g_assert (i < mux->num_streams);
+ g_assert (context->type == GST_MATROSKA_TRACK_TYPE_AUDIO);
+ audiocontext = (GstMatroskaTrackAudioContext *) context;
+
+ for (; caps != NULL; caps = caps->next) {
+ mimetype = gst_caps_get_mime (caps);
+
+ /* general setup */
+ gst_caps_get (caps,
+ "rate", &samplerate,
+ "channels", &channels,
+ NULL);
+ audiocontext->samplerate = samplerate;
+ audiocontext->channels = channels;
+ audiocontext->bitdepth = 16;
+
+ if (!strcmp (mimetype, "audio/mpeg")) {
+ gint mpegversion = 1;
+
+ gst_caps_get_int (caps, "mpegversion", &mpegversion);
+ switch (mpegversion) {
+ case 1: {
+ gint layer;
+
+ gst_caps_get_int (caps, "layer", &layer);
+ switch (layer) {
+ case 1:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L1);
+ break;
+ case 2:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L2);
+ break;
+ case 3:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_MPEG1_L3);
+ break;
+ }
+ break;
+ }
+ case 2:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_MPEG2
+ "MAIN");
+ break;
+ case 4:
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_MPEG4
+ "MAIN");
+ break;
+ }
+
+ return GST_PAD_LINK_OK;
+ } else if (!strcmp (mimetype, "audio/x-raw-int")) {
+ gint endianness, width, depth;
+ gboolean signedness;
+
+ gst_caps_get (caps,
+ "endianness", &endianness,
+ "width", &width,
+ "depth", &depth,
+ "signed", &signedness,
+ NULL);
+ if (width != depth ||
+ (width == 8 && signedness) || (width == 16 && !signedness))
+ continue;
+
+ 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);
+
+ return GST_PAD_LINK_OK;
+ } else if (!strcmp (mimetype, "audio/x-raw-float")) {
+ /* FIXME: endianness is undefined */
+ } else if (!strcmp (mimetype, "audio/x-vorbis")) {
+ /* FIXME: private data setup needs work */
+ } else if (!strcmp (mimetype, "audio/x-ac3")) {
+ context->codec_id = g_strdup (GST_MATROSKA_CODEC_ID_AUDIO_AC3);
+
+ return GST_PAD_LINK_OK;
+ }
+ }
+
+ return GST_PAD_LINK_REFUSED;
+}
+
+static GstPadLinkReturn
+gst_matroska_mux_subtitle_pad_link (GstPad *pad,
+ GstCaps *caps)
+{
+ /* 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. */
+
+ return GST_PAD_LINK_REFUSED;
+}
+
+static GstPad *
+gst_matroska_mux_request_new_pad (GstElement *element,
+ GstPadTemplate *templ,
+ const gchar *pad_name)
+{
+ GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
+ GstMatroskaMux *mux = GST_MATROSKA_MUX (element);
+ GstPad *pad = NULL;
+ gchar *name = NULL;
+ GstPadLinkFunction linkfunc = NULL;
+ GstMatroskaTrackContext *context = NULL;
+
+ if (templ == gst_element_class_get_pad_template (klass, "audio_%d")) {
+ name = g_strdup_printf ("audio_%d", mux->num_a_streams++);
+ linkfunc = gst_matroska_mux_audio_pad_link;
+ context = (GstMatroskaTrackContext *)
+ g_new0 (GstMatroskaTrackAudioContext, 1);
+ 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++);
+ linkfunc = gst_matroska_mux_video_pad_link;
+ context = (GstMatroskaTrackContext *)
+ g_new0 (GstMatroskaTrackVideoContext, 1);
+ 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++);
+ linkfunc = gst_matroska_mux_subtitle_pad_link;
+ context = (GstMatroskaTrackContext *)
+ g_new0 (GstMatroskaTrackSubtitleContext, 1);
+ context->type = GST_MATROSKA_TRACK_TYPE_SUBTITLE;
+ context->name = g_strdup ("Subtitle");
+ } else {
+ g_warning ("matroskamux: this is not our template!");
+ return NULL;
+ }
+
+ pad = gst_pad_new_from_template (templ, name);
+ g_free (name);
+ gst_element_add_pad (element, pad);
+ gst_pad_set_link_function (pad, linkfunc);
+ context->index = mux->num_streams++;
+ mux->sink[context->index].track = context;
+ context->pad = pad;
+ context->flags = GST_MATROSKA_TRACK_ENABLED |
+ GST_MATROSKA_TRACK_DEFAULT;
+
+ return pad;
+}
+
+static void
+gst_matroska_mux_track_header (GstMatroskaMux *mux,
+ GstMatroskaTrackContext *context)
+{
+ GstEbmlWrite *ebml = GST_EBML_WRITE (mux);
+ 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);
+
+ /* type-specific stuff */
+ switch (context->type) {
+ case GST_MATROSKA_TRACK_TYPE_VIDEO: {
+ GstMatroskaTrackVideoContext *videocontext =
+ (GstMatroskaTrackVideoContext *) context;
+
+ /* framerate, but not in the video part */
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_TRACKDEFAULTDURATION,
+ context->default_duration);
+
+ master = gst_ebml_write_master_start (ebml,
+ GST_MATROSKA_ID_TRACKVIDEO);
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_VIDEOPIXELWIDTH,
+ videocontext->pixel_width);
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_VIDEOPIXELHEIGHT,
+ videocontext->pixel_height);
+ if (videocontext->display_width && videocontext->display_height) {
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_VIDEODISPLAYWIDTH,
+ videocontext->display_width);
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_VIDEODISPLAYHEIGHT,
+ videocontext->display_height);
+ }
+ if (context->flags & GST_MATROSKA_VIDEOTRACK_INTERLACED)
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_VIDEOFLAGINTERLACED, 1);
+ if (videocontext->fourcc) {
+ guint32 fcc_le = GUINT32_TO_LE (videocontext->fourcc);
+ gst_ebml_write_binary (ebml, GST_MATROSKA_ID_VIDEOCOLOURSPACE,
+ (gpointer) &fcc_le, 4);
+ }
+ gst_ebml_write_master_finish (ebml, master);
+
+ break;
+ }
+
+ case GST_MATROSKA_TRACK_TYPE_AUDIO: {
+ GstMatroskaTrackAudioContext *audiocontext =
+ (GstMatroskaTrackAudioContext *) context;
+
+ master = gst_ebml_write_master_start (ebml,
+ GST_MATROSKA_ID_TRACKAUDIO);
+ if (audiocontext->samplerate != 8000)
+ gst_ebml_write_float (ebml, GST_MATROSKA_ID_AUDIOSAMPLINGFREQ,
+ audiocontext->samplerate);
+ if (audiocontext->channels != 1)
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_AUDIOCHANNELS,
+ audiocontext->channels);
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_AUDIOBITDEPTH,
+ audiocontext->bitdepth);
+ gst_ebml_write_master_finish (ebml, master);
+
+ break;
+ }
+
+ default:
+ /* doesn't need type-specific data */
+ break;
+ }
+
+ gst_ebml_write_ascii (ebml, GST_MATROSKA_ID_CODECID,
+ context->codec_id);
+ if (context->codec_priv)
+ gst_ebml_write_binary (ebml, GST_MATROSKA_ID_CODECPRIVATE,
+ context->codec_priv, context->codec_priv_size);
+ /* 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,
+ context->codec_name);*/
+ gst_ebml_write_utf8 (ebml, GST_MATROSKA_ID_TRACKNAME,
+ context->name);
+}
+
+static void
+gst_matroska_mux_start (GstMatroskaMux *mux)
+{
+ GstEbmlWrite *ebml = GST_EBML_WRITE (mux);
+ guint32 seekhead_id[] = { GST_MATROSKA_ID_INFO,
+ GST_MATROSKA_ID_TRACKS,
+ GST_MATROSKA_ID_CUES,
+#if 0
+ GST_MATROSKA_ID_TAGS,
+#endif
+ 0 };
+ guint64 master, child;
+ gint i;
+ guint tracknum = 1;
+
+ /* we start with a EBML header */
+ gst_ebml_write_header (ebml, "matroska", 1);
+
+ /* start a segment */
+ mux->segment_pos = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_SEGMENT);
+ 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);
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_TIMECODESCALE, mux->time_scale);
+ mux->duration_pos = ebml->pos;
+ gst_ebml_write_float (ebml, GST_MATROSKA_ID_DURATION, 0);
+ gst_ebml_write_utf8 (ebml, GST_MATROSKA_ID_MUXINGAPP, "GStreamer");
+ if (mux->metadata &&
+ gst_caps_has_property (mux->metadata, "application")) {
+ const gchar *app;
+
+ gst_caps_get_string (mux->metadata, "application", &app);
+ if (app && app[0]) {
+ gst_ebml_write_utf8 (ebml, GST_MATROSKA_ID_WRITINGAPP, app);
+ }
+ }
+ /* FIXME: how do I get this? Automatic? Via tags? */
+ /*gst_ebml_write_date (ebml, GST_MATROSKA_ID_DATEUTC, 0);*/
+ gst_ebml_write_master_finish (ebml, master);
+
+ /* tracks */
+ mux->tracks_pos = ebml->pos;
+ master = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_TRACKS);
+ for (i = 0; i < mux->num_streams; i++) {
+ if (GST_PAD_IS_USABLE (mux->sink[i].track->pad)) {
+ mux->sink[i].track->num = tracknum++;
+ child = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_TRACKENTRY);
+ gst_matroska_mux_track_header (mux, mux->sink[i].track);
+ gst_ebml_write_master_finish (ebml, child);
+ }
+ }
+ gst_ebml_write_master_finish (ebml, master);
+
+ /* lastly, flush the cache */
+ gst_ebml_write_flush_cache (ebml);
+}
+
+static void
+gst_matroska_mux_finish (GstMatroskaMux *mux)
+{
+ GstEbmlWrite *ebml = GST_EBML_WRITE (mux);
+ guint64 pos;
+
+ /* 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,
+ GST_MATROSKA_ID_POINTENTRY);
+ gst_ebml_write_date (ebml, GST_MATROSKA_ID_CUETIME,
+ idx->time / mux->time_scale);
+ trackpos_master = gst_ebml_write_master_start (ebml,
+ GST_MATROSKA_ID_CUETRACKPOSITION);
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_CUETRACK, idx->track);
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_CUECLUSTERPOSITION,
+ idx->pos - mux->segment_master);
+ gst_ebml_write_master_finish (ebml, trackpos_master);
+ gst_ebml_write_master_finish (ebml, pointentry_master);
+ }
+
+ gst_ebml_write_master_finish (ebml, master);
+ gst_ebml_write_flush_cache (ebml);
+ }
+
+ /* FIXME: tags */
+
+ /* update seekhead. We know that:
+ * - a seekhead contains 4 entries.
+ * - order of entries is as above.
+ * - a seekhead has a 4-byte header + 8-byte length
+ * - each entry is 2-byte master, 2-byte ID pointer,
+ * 2-byte length pointer, all 8/1-byte length, 4-
+ * byte ID and 8-byte length pointer, where the
+ * length pointer starts at 20.
+ * - all entries are local to the segment (so pos - segment_master).
+ * - so each entry is at 12 + 20 + num * 28. */
+ gst_ebml_replace_uint (ebml, mux->seekhead_pos + 32,
+ mux->info_pos - mux->segment_master);
+ gst_ebml_replace_uint (ebml, mux->seekhead_pos + 60,
+ mux->tracks_pos - mux->segment_master);
+ if (mux->index != NULL) {
+ gst_ebml_replace_uint (ebml, mux->seekhead_pos + 88,
+ mux->cues_pos - mux->segment_master);
+ } else {
+ /* void'ify */
+ guint64 my_pos = ebml->pos;
+ gst_ebml_write_seek (ebml, mux->seekhead_pos + 68);
+ gst_ebml_write_buffer_header (ebml, GST_EBML_ID_VOID, 26);
+ gst_ebml_write_seek (ebml, my_pos);
+ }
+#if 0
+ gst_ebml_replace_uint (ebml, mux->seekhead_pos + 116,
+ mux->tags_pos - mux->segment_master);
+#endif
+
+ /* update duration */
+ pos = GST_EBML_WRITE (mux)->pos;
+ gst_ebml_write_seek (ebml, mux->duration_pos);
+ gst_ebml_write_float (ebml, GST_MATROSKA_ID_DURATION,
+ mux->duration / mux->time_scale);
+ gst_ebml_write_seek (ebml, pos);
+
+ /* finish segment - this also writes element length */
+ gst_ebml_write_master_finish (ebml, mux->segment_pos);
+}
+
+static gint
+gst_matroska_mux_prepare_data (GstMatroskaMux *mux)
+{
+ gint i, first = -1;
+
+ for (i = 0; i < mux->num_streams; i++) {
+ while (!mux->sink[i].eos && !mux->sink[i].buffer &&
+ mux->sink[i].track->num > 0 &&
+ GST_PAD_IS_USABLE (mux->sink[i].track->pad)) {
+ GstData *data;
+
+ data = gst_pad_pull (mux->sink[i].track->pad);
+ if (GST_IS_EVENT (data)) {
+ if (GST_EVENT_TYPE (GST_EVENT (data)) == GST_EVENT_EOS)
+ mux->sink[i].eos = TRUE;
+ gst_event_unref (GST_EVENT (data));
+ } else {
+ mux->sink[i].buffer = GST_BUFFER (data);
+ }
+ }
+
+ if (mux->sink[i].buffer) {
+ if (first < 0 || GST_BUFFER_TIMESTAMP (mux->sink[i].buffer) <
+ GST_BUFFER_TIMESTAMP (mux->sink[first].buffer))
+ first = i;
+ }
+ }
+
+ return first;
+}
+
+static void
+gst_matroska_mux_write_data (GstMatroskaMux *mux)
+{
+ GstEbmlWrite *ebml = GST_EBML_WRITE (mux);
+ GstBuffer *buf, *hdr;
+ gint i;
+ guint64 cluster, blockgroup;
+
+ /* which stream-num to write from? */
+ if ((i = gst_matroska_mux_prepare_data (mux)) < 0) {
+ GstEvent *event = gst_event_new (GST_EVENT_EOS);
+
+ gst_matroska_mux_finish (mux);
+ gst_pad_push (mux->srcpad, GST_DATA (event));
+ gst_element_set_eos (GST_ELEMENT (mux));
+
+ return;
+ }
+
+ /* write data */
+ buf = mux->sink[i].buffer;
+ mux->sink[i].buffer = NULL;
+
+ /* We currently write an index entry for each keyframe in a
+ * video track. This can be largely improved, such as doing
+ * one for each keyframe or each second (for all-keyframe
+ * streams), only the *first* video track or the audio track
+ * if we have no video tracks. But that'll come later... */
+ if (mux->sink[i].track->type == GST_MATROSKA_TRACK_TYPE_VIDEO &&
+ GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_KEY_UNIT)) {
+ GstMatroskaIndex *idx;
+
+ if (mux->num_indexes % 32 == 0) {
+ mux->index = g_renew (GstMatroskaIndex, mux->index,
+ mux->num_indexes + 32);
+ }
+ idx = &mux->index[mux->num_indexes++];
+
+ idx->pos = ebml->pos;
+ idx->time = GST_BUFFER_TIMESTAMP (buf);
+ idx->track = mux->sink[i].track->num;
+ }
+
+ /* write one cluster with one blockgroup with one block with
+ * one slice (*breath*).
+ * FIXME: lacing, multiple frames/cluster, etc. */
+ cluster = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_CLUSTER);
+ gst_ebml_write_uint (ebml, GST_MATROSKA_ID_CLUSTERTIMECODE,
+ GST_BUFFER_TIMESTAMP (buf) / mux->time_scale);
+ blockgroup = gst_ebml_write_master_start (ebml, GST_MATROSKA_ID_BLOCKGROUP);
+ gst_ebml_write_buffer_header (ebml, GST_MATROSKA_ID_BLOCK,
+ GST_BUFFER_SIZE (buf) + 4);
+ hdr = gst_buffer_new_and_alloc (4);
+ /* track num - FIXME: what if num >= 0x80 (unlikely)? */
+ GST_BUFFER_DATA (hdr)[0] = mux->sink[i].track->num | 0x80;
+ /* time relative to clustertime - we don't use this yet */
+ * (guint16 *) &GST_BUFFER_DATA (hdr)[1] = GUINT16_TO_BE (0);
+ /* flags - no lacing (yet) */
+ GST_BUFFER_DATA (hdr)[3] = 0;
+ gst_ebml_write_buffer (ebml, hdr);
+ gst_ebml_write_buffer (ebml, buf);
+ gst_ebml_write_master_finish (ebml, blockgroup);
+ gst_ebml_write_master_finish (ebml, cluster);
+}
+
+static void
+gst_matroska_mux_loop (GstElement *element)
+{
+ GstMatroskaMux *mux = GST_MATROSKA_MUX (element);
+
+ /* start with a header */
+ if (mux->state == GST_MATROSKA_MUX_STATE_START) {
+ mux->state = GST_MATROSKA_MUX_STATE_HEADER;
+ gst_matroska_mux_start (mux);
+ mux->state = GST_MATROSKA_MUX_STATE_DATA;
+ }
+
+ /* do one single buffer */
+ gst_matroska_mux_write_data (mux);
+}
+
+static GstElementStateReturn
+gst_matroska_mux_change_state (GstElement *element)
+{
+ GstMatroskaMux *mux = GST_MATROSKA_MUX (element);
+
+ switch (GST_STATE_TRANSITION (element)) {
+ case GST_STATE_PAUSED_TO_READY:
+ gst_matroska_mux_reset (GST_ELEMENT (mux));
+ break;
+ default:
+ break;
+ }
+
+ if (((GstElementClass *) parent_class)->change_state)
+ return ((GstElementClass *) parent_class)->change_state (element);
+
+ return GST_STATE_SUCCESS;
+}
+
+static void
+gst_matroska_mux_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GstMatroskaMux *mux;
+
+ g_return_if_fail (GST_IS_MATROSKA_MUX (object));
+ mux = GST_MATROSKA_MUX (object);
+
+ switch (prop_id) {
+ case ARG_METADATA:
+ gst_caps_replace (&mux->metadata,
+ g_value_get_boxed (value));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gst_matroska_mux_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GstMatroskaMux *mux;
+
+ g_return_if_fail (GST_IS_MATROSKA_MUX (object));
+ mux = GST_MATROSKA_MUX (object);
+
+ switch (prop_id) {
+ case ARG_METADATA:
+ g_value_set_boxed (value, mux->metadata);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+gboolean
+gst_matroska_mux_plugin_init (GstPlugin *plugin)
+{
+ return gst_element_register (plugin, "matroskamux",
+ GST_RANK_NONE,
+ GST_TYPE_MATROSKA_MUX);
+}
diff --git a/gst/matroska/matroska-mux.h b/gst/matroska/matroska-mux.h
new file mode 100644
index 00000000..b89f0d22
--- /dev/null
+++ b/gst/matroska/matroska-mux.h
@@ -0,0 +1,103 @@
+/* GStreamer Matroska muxer/demuxer
+ * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
+ *
+ * matroska-mux.h: matroska file/stream muxer object types
+ *
+ * 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.
+ */
+
+#ifndef __GST_MATROSKA_MUX_H__
+#define __GST_MATROSKA_MUX_H__
+
+#include <gst/gst.h>
+
+#include "ebml-write.h"
+#include "matroska-ids.h"
+
+G_BEGIN_DECLS
+
+#define GST_TYPE_MATROSKA_MUX \
+ (gst_matroska_mux_get_type ())
+#define GST_MATROSKA_MUX(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MATROSKA_MUX, GstMatroskaMux))
+#define GST_MATROSKA_MUX_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MATROSKA_MUX, GstMatroskaMux))
+#define GST_IS_MATROSKA_MUX(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MATROSKA_MUX))
+#define GST_IS_MATROSKA_MUX_CLASS(obj) \
+ (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MATROSKA_MUX))
+
+#define GST_MATROSKA_MUX_MAX_STREAMS 64
+
+typedef enum {
+ GST_MATROSKA_MUX_STATE_START,
+ GST_MATROSKA_MUX_STATE_HEADER,
+ GST_MATROSKA_MUX_STATE_DATA,
+} GstMatroskaMuxState;
+
+typedef struct _GstMatroskaMux {
+ GstEbmlWrite parent;
+
+ /* pads */
+ GstPad *srcpad;
+ struct {
+ GstMatroskaTrackContext *track;
+ GstBuffer *buffer;
+ gboolean eos;
+ } sink[GST_MATROSKA_MUX_MAX_STREAMS];
+ guint num_streams,
+ num_v_streams, num_a_streams, num_t_streams;
+
+ /* metadata - includes writing_app and creation_time */
+ GstCaps *metadata;
+
+ /* state */
+ GstMatroskaMuxState state;
+
+ /* a cue (index) table */
+ GstMatroskaIndex *index;
+ guint num_indexes;
+
+ /* timescale in the file */
+ guint64 time_scale;
+
+ /* length, position (time, ns) */
+ guint64 duration;
+
+ /* byte-positions of master-elements (for replacing contents) */
+ guint64 segment_pos,
+ seekhead_pos,
+ cues_pos,
+#if 0
+ tags_pos,
+#endif
+ info_pos,
+ tracks_pos,
+ duration_pos;
+ guint64 segment_master;
+} GstMatroskaMux;
+
+typedef struct _GstMatroskaMuxClass {
+ GstEbmlWriteClass parent;
+} GstMatroskaMuxClass;
+
+GType gst_matroska_mux_get_type (void);
+
+gboolean gst_matroska_mux_plugin_init (GstPlugin *plugin);
+
+G_END_DECLS
+
+#endif /* __GST_MATROSKA_MUX_H__ */
diff --git a/gst/matroska/matroska.c b/gst/matroska/matroska.c
index feb44d63..49657818 100644
--- a/gst/matroska/matroska.c
+++ b/gst/matroska/matroska.c
@@ -24,11 +24,13 @@
#endif
#include "matroska-demux.h"
+#include "matroska-mux.h"
static gboolean
plugin_init (GstPlugin *plugin)
{
- return (gst_matroska_demux_plugin_init (plugin));
+ return (gst_matroska_demux_plugin_init (plugin) &&
+ gst_matroska_mux_plugin_init (plugin));
}
GST_PLUGIN_DEFINE (