matroskamux: fix matroskamux ! matroskademux
[platform/upstream/gstreamer.git] / gst / matroska / ebml-write.c
1 /* GStreamer EBML I/O
2  * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  * (c) 2005 Michal Benes <michal.benes@xeris.cz>
4  *
5  * ebml-write.c: write EBML data to file/stream
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28
29 #include "ebml-write.h"
30 #include "ebml-ids.h"
31
32
33 GST_DEBUG_CATEGORY_STATIC (gst_ebml_write_debug);
34 #define GST_CAT_DEFAULT gst_ebml_write_debug
35
36 #define _do_init \
37       GST_DEBUG_CATEGORY_INIT (gst_ebml_write_debug, "ebmlwrite", 0, "Write EBML structured data")
38 #define parent_class gst_ebml_write_parent_class
39 G_DEFINE_TYPE_WITH_CODE (GstEbmlWrite, gst_ebml_write, GST_TYPE_OBJECT,
40     _do_init);
41
42 static void gst_ebml_write_finalize (GObject * object);
43
44 static void
45 gst_ebml_write_class_init (GstEbmlWriteClass * klass)
46 {
47   GObjectClass *object = G_OBJECT_CLASS (klass);
48
49   object->finalize = gst_ebml_write_finalize;
50 }
51
52 static void
53 gst_ebml_write_init (GstEbmlWrite * ebml)
54 {
55   ebml->srcpad = NULL;
56   ebml->pos = 0;
57   ebml->last_pos = G_MAXUINT64; /* force segment event */
58
59   ebml->cache = NULL;
60   ebml->streamheader = NULL;
61   ebml->streamheader_pos = 0;
62   ebml->writing_streamheader = FALSE;
63   ebml->caps = NULL;
64 }
65
66 static void
67 gst_ebml_write_finalize (GObject * object)
68 {
69   GstEbmlWrite *ebml = GST_EBML_WRITE (object);
70
71   gst_object_unref (ebml->srcpad);
72
73   if (ebml->cache) {
74     gst_byte_writer_free (ebml->cache);
75     ebml->cache = NULL;
76   }
77
78   if (ebml->streamheader) {
79     gst_byte_writer_free (ebml->streamheader);
80     ebml->streamheader = NULL;
81   }
82
83   if (ebml->caps) {
84     gst_caps_unref (ebml->caps);
85     ebml->caps = NULL;
86   }
87
88   G_OBJECT_CLASS (parent_class)->finalize (object);
89 }
90
91
92 /**
93  * gst_ebml_write_new:
94  * @srcpad: Source pad to which the output will be pushed.
95  *
96  * Creates a new #GstEbmlWrite.
97  *
98  * Returns: a new #GstEbmlWrite
99  */
100 GstEbmlWrite *
101 gst_ebml_write_new (GstPad * srcpad)
102 {
103   GstEbmlWrite *ebml =
104       GST_EBML_WRITE (g_object_new (GST_TYPE_EBML_WRITE, NULL));
105
106   ebml->srcpad = gst_object_ref (srcpad);
107   ebml->timestamp = GST_CLOCK_TIME_NONE;
108
109   gst_ebml_write_reset (ebml);
110
111   return ebml;
112 }
113
114
115 /**
116  * gst_ebml_write_reset:
117  * @ebml: a #GstEbmlWrite.
118  *
119  * Reset internal state of #GstEbmlWrite.
120  */
121 void
122 gst_ebml_write_reset (GstEbmlWrite * ebml)
123 {
124   ebml->pos = 0;
125   ebml->last_pos = G_MAXUINT64; /* force segment event */
126
127   if (ebml->cache) {
128     gst_byte_writer_free (ebml->cache);
129     ebml->cache = NULL;
130   }
131
132   if (ebml->caps) {
133     gst_caps_unref (ebml->caps);
134     ebml->caps = NULL;
135   }
136
137   ebml->last_write_result = GST_FLOW_OK;
138   ebml->timestamp = GST_CLOCK_TIME_NONE;
139 }
140
141
142 /**
143  * gst_ebml_last_write_result:
144  * @ebml: a #GstEbmlWrite.
145  *
146  * Returns: GST_FLOW_OK if there was not write error since the last call of
147  *          gst_ebml_last_write_result or code of the error.
148  */
149 GstFlowReturn
150 gst_ebml_last_write_result (GstEbmlWrite * ebml)
151 {
152   GstFlowReturn res = ebml->last_write_result;
153
154   ebml->last_write_result = GST_FLOW_OK;
155
156   return res;
157 }
158
159
160 void
161 gst_ebml_start_streamheader (GstEbmlWrite * ebml)
162 {
163   g_return_if_fail (ebml->streamheader == NULL);
164
165   GST_DEBUG ("Starting streamheader at %" G_GUINT64_FORMAT, ebml->pos);
166   ebml->streamheader = gst_byte_writer_new_with_size (1000, FALSE);
167   ebml->streamheader_pos = ebml->pos;
168   ebml->writing_streamheader = TRUE;
169 }
170
171 GstBuffer *
172 gst_ebml_stop_streamheader (GstEbmlWrite * ebml)
173 {
174   GstBuffer *buffer;
175
176   if (!ebml->streamheader)
177     return NULL;
178
179   buffer = gst_byte_writer_free_and_get_buffer (ebml->streamheader);
180   ebml->streamheader = NULL;
181   GST_DEBUG ("Streamheader was size %" G_GSIZE_FORMAT,
182       gst_buffer_get_size (buffer));
183
184   ebml->writing_streamheader = FALSE;
185   return buffer;
186 }
187
188 /**
189  * gst_ebml_write_set_cache:
190  * @ebml: a #GstEbmlWrite.
191  * @size: size of the cache.
192  * Create a cache.
193  *
194  * The idea is that you use this for writing a lot
195  * of small elements. This will just "queue" all of
196  * them and they'll be pushed to the next element all
197  * at once. This saves memory and time for buffer
198  * allocation and init, and it looks better.
199  */
200 void
201 gst_ebml_write_set_cache (GstEbmlWrite * ebml, guint size)
202 {
203   g_return_if_fail (ebml->cache == NULL);
204
205   GST_DEBUG ("Starting cache at %" G_GUINT64_FORMAT, ebml->pos);
206   ebml->cache = gst_byte_writer_new_with_size (size, FALSE);
207   ebml->cache_pos = ebml->pos;
208 }
209
210 static gboolean
211 gst_ebml_writer_send_segment_event (GstEbmlWrite * ebml, guint64 new_pos)
212 {
213   GstSegment segment;
214   gboolean res;
215
216   GST_INFO ("seeking to %" G_GUINT64_FORMAT, new_pos);
217
218   gst_segment_init (&segment, GST_FORMAT_BYTES);
219   segment.start = new_pos;
220   segment.stop = -1;
221   segment.position = 0;
222
223   res = gst_pad_push_event (ebml->srcpad, gst_event_new_segment (&segment));
224
225   if (!res)
226     GST_WARNING ("seek to %" G_GUINT64_FORMAT "failed", new_pos);
227
228   return res;
229 }
230
231 /**
232  * gst_ebml_write_flush_cache:
233  * @ebml:      a #GstEbmlWrite.
234  * @timestamp: timestamp of the buffer.
235  *
236  * Flush the cache.
237  */
238 void
239 gst_ebml_write_flush_cache (GstEbmlWrite * ebml, gboolean is_keyframe,
240     GstClockTime timestamp)
241 {
242   GstBuffer *buffer;
243
244   if (!ebml->cache)
245     return;
246
247   buffer = gst_byte_writer_free_and_get_buffer (ebml->cache);
248   ebml->cache = NULL;
249   GST_DEBUG ("Flushing cache of size %" G_GSIZE_FORMAT,
250       gst_buffer_get_size (buffer));
251   GST_BUFFER_TIMESTAMP (buffer) = timestamp;
252   GST_BUFFER_OFFSET (buffer) = ebml->pos - gst_buffer_get_size (buffer);
253   GST_BUFFER_OFFSET_END (buffer) = ebml->pos;
254   if (ebml->last_write_result == GST_FLOW_OK) {
255     if (GST_BUFFER_OFFSET (buffer) != ebml->last_pos) {
256       gst_ebml_writer_send_segment_event (ebml, GST_BUFFER_OFFSET (buffer));
257       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
258     } else {
259       GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DISCONT);
260     }
261     if (ebml->writing_streamheader) {
262       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_HEADER);
263     }
264     if (!is_keyframe) {
265       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
266     }
267     ebml->last_pos = ebml->pos;
268     ebml->last_write_result = gst_pad_push (ebml->srcpad, buffer);
269   } else {
270     gst_buffer_unref (buffer);
271   }
272 }
273
274
275 /**
276  * gst_ebml_write_element_new:
277  * @ebml: a #GstEbmlWrite.
278  * @size: size of the requested buffer.
279  *
280  * Create a buffer for one element. If there is
281  * a cache, use that instead.
282  *
283  * Returns: A new #GstBuffer.
284  */
285 static GstBuffer *
286 gst_ebml_write_element_new (GstEbmlWrite * ebml, GstMapInfo * map, guint size)
287 {
288   /* Create new buffer of size + ID + length */
289   GstBuffer *buf;
290
291   /* length, ID */
292   size += 12;
293
294   buf = gst_buffer_new_and_alloc (size);
295   GST_BUFFER_TIMESTAMP (buf) = ebml->timestamp;
296
297   /* FIXME unmap not possible */
298   gst_buffer_map (buf, map, GST_MAP_WRITE);
299
300   return buf;
301 }
302
303
304 /**
305  * gst_ebml_write_element_id:
306  * @data_inout: Pointer to data pointer
307  * @id: Element ID that should be written.
308  * 
309  * Write element ID into a buffer.
310  */
311 static void
312 gst_ebml_write_element_id (guint8 ** data_inout, guint32 id)
313 {
314   guint8 *data = *data_inout;
315   guint bytes = 4, mask = 0x10;
316
317   /* get ID length */
318   while (!(id & (mask << ((bytes - 1) * 8))) && bytes > 0) {
319     mask <<= 1;
320     bytes--;
321   }
322
323   /* if invalid ID, use dummy */
324   if (bytes == 0) {
325     GST_WARNING ("Invalid ID, voiding");
326     bytes = 1;
327     id = GST_EBML_ID_VOID;
328   }
329
330   /* write out, BE */
331   *data_inout += bytes;
332   while (bytes--) {
333     data[bytes] = id & 0xff;
334     id >>= 8;
335   }
336 }
337
338
339 /**
340  * gst_ebml_write_element_size:
341  * @data_inout: Pointer to data pointer
342  * @size: Element length.
343  *
344  * Write element length into a buffer.
345  */
346 static void
347 gst_ebml_write_element_size (guint8 ** data_inout, guint64 size)
348 {
349   guint8 *data = *data_inout;
350   guint bytes = 1, mask = 0x80;
351
352   if (size != GST_EBML_SIZE_UNKNOWN) {
353     /* how many bytes? - use mask-1 because an all-1 bitset is not allowed */
354     while (bytes <= 8 && (size >> ((bytes - 1) * 8)) >= (mask - 1)) {
355       mask >>= 1;
356       bytes++;
357     }
358
359     /* if invalid size, use max. */
360     if (bytes > 8) {
361       GST_WARNING ("Invalid size, writing size unknown");
362       mask = 0x01;
363       bytes = 8;
364       /* Now here's a real FIXME: we cannot read those yet! */
365       size = GST_EBML_SIZE_UNKNOWN;
366     }
367   } else {
368     mask = 0x01;
369     bytes = 8;
370   }
371
372   /* write out, BE, with length size marker */
373   *data_inout += bytes;
374   while (bytes-- > 0) {
375     data[bytes] = size & 0xff;
376     size >>= 8;
377     if (!bytes)
378       *data |= mask;
379   }
380 }
381
382
383 /**
384  * gst_ebml_write_element_data:
385  * @data_inout: Pointer to data pointer
386  * @write: Data that should be written.
387  * @length: Length of the data.
388  *
389  * Write element data into a buffer.
390  */
391 static void
392 gst_ebml_write_element_data (guint8 ** data_inout, guint8 * write,
393     guint64 length)
394 {
395   memcpy (*data_inout, write, length);
396   *data_inout += length;
397 }
398
399
400 /**
401  * gst_ebml_write_element_push:
402  * @ebml: #GstEbmlWrite
403  * @buf: #GstBuffer to be written.
404  * @buf_data: Start of data to push from @buf (or NULL for whole buffer).
405  * @buf_data_end: Data pointer positioned after the last byte in @buf_data (or
406  * NULL for whole buffer).
407  * 
408  * Write out buffer by moving it to the next element.
409  */
410 static void
411 gst_ebml_write_element_push (GstEbmlWrite * ebml, GstBuffer * buf,
412     guint8 * buf_data, guint8 * buf_data_end)
413 {
414   GstMapInfo map;
415   guint data_size;
416
417   map.data = NULL;
418
419   if (buf_data_end)
420     data_size = buf_data_end - buf_data;
421   else
422     data_size = gst_buffer_get_size (buf);
423
424   ebml->pos += data_size;
425
426   /* if there's no cache, then don't push it! */
427   if (ebml->writing_streamheader) {
428     if (!buf_data) {
429       gst_buffer_map (buf, &map, GST_MAP_READ);
430       buf_data = map.data;
431     }
432     if (!buf_data)
433       GST_WARNING ("Failed to map buffer");
434     else if (!gst_byte_writer_put_data (ebml->streamheader, buf_data,
435             data_size))
436       GST_WARNING ("Error writing data to streamheader");
437   }
438   if (ebml->cache) {
439     if (!buf_data) {
440       gst_buffer_map (buf, &map, GST_MAP_READ);
441       buf_data = map.data;
442     }
443     if (!buf_data)
444       GST_WARNING ("Failed to map buffer");
445     else if (!gst_byte_writer_put_data (ebml->cache, buf_data, data_size))
446       GST_WARNING ("Error writing data to cache");
447     if (map.data)
448       gst_buffer_unmap (buf, &map);
449     gst_buffer_unref (buf);
450     return;
451   }
452
453   if (buf_data && map.data)
454     gst_buffer_unmap (buf, &map);
455
456   if (ebml->last_write_result == GST_FLOW_OK) {
457     buf = gst_buffer_make_writable (buf);
458     GST_BUFFER_OFFSET (buf) = ebml->pos - data_size;
459     GST_BUFFER_OFFSET_END (buf) = ebml->pos;
460     if (ebml->writing_streamheader) {
461       GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
462     }
463     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
464
465     if (GST_BUFFER_OFFSET (buf) != ebml->last_pos) {
466       gst_ebml_writer_send_segment_event (ebml, GST_BUFFER_OFFSET (buf));
467       GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
468     } else {
469       GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
470     }
471     ebml->last_pos = ebml->pos;
472     ebml->last_write_result = gst_pad_push (ebml->srcpad, buf);
473   } else {
474     gst_buffer_unref (buf);
475   }
476 }
477
478
479 /**
480  * gst_ebml_write_seek:
481  * @ebml: #GstEbmlWrite
482  * @pos: Seek position.
483  * 
484  * Seek.
485  */
486 void
487 gst_ebml_write_seek (GstEbmlWrite * ebml, guint64 pos)
488 {
489   if (ebml->writing_streamheader) {
490     GST_DEBUG ("wanting to seek to pos %" G_GUINT64_FORMAT, pos);
491     if (pos >= ebml->streamheader_pos &&
492         pos <= ebml->streamheader_pos + ebml->streamheader->parent.size) {
493       gst_byte_writer_set_pos (ebml->streamheader,
494           pos - ebml->streamheader_pos);
495       GST_DEBUG ("seeked in streamheader to position %" G_GUINT64_FORMAT,
496           pos - ebml->streamheader_pos);
497     } else {
498       GST_WARNING
499           ("we are writing streamheader still and seek is out of bounds");
500     }
501   }
502   /* Cache seeking. A bit dangerous, we assume the client writer
503    * knows what he's doing... */
504   if (ebml->cache) {
505     /* within bounds? */
506     if (pos >= ebml->cache_pos &&
507         pos <= ebml->cache_pos + ebml->cache->parent.size) {
508       GST_DEBUG ("seeking in cache to %" G_GUINT64_FORMAT, pos);
509       ebml->pos = pos;
510       gst_byte_writer_set_pos (ebml->cache, ebml->pos - ebml->cache_pos);
511       return;
512     } else {
513       GST_LOG ("Seek outside cache range. Clearing...");
514       gst_ebml_write_flush_cache (ebml, FALSE, GST_CLOCK_TIME_NONE);
515     }
516   }
517
518   GST_INFO ("scheduling seek to %" G_GUINT64_FORMAT, pos);
519   ebml->pos = pos;
520 }
521
522
523 /**
524  * gst_ebml_write_get_uint_size:
525  * @num: Number to be encoded.
526  * 
527  * Get number of bytes needed to write a uint.
528  *
529  * Returns: Encoded uint length.
530  */
531 static guint
532 gst_ebml_write_get_uint_size (guint64 num)
533 {
534   guint size = 1;
535
536   /* get size */
537   while (size < 8 && num >= (G_GINT64_CONSTANT (1) << (size * 8))) {
538     size++;
539   }
540
541   return size;
542 }
543
544
545 /**
546  * gst_ebml_write_set_uint:
547  * @data_inout: Pointer to data pointer
548  * @num: Number to be written.
549  * @size: Encoded number length.
550  *
551  * Write an uint into a buffer.
552  */
553 static void
554 gst_ebml_write_set_uint (guint8 ** data_inout, guint64 num, guint size)
555 {
556   guint8 *data = *data_inout;
557
558   *data_inout += size;
559
560   while (size-- > 0) {
561     data[size] = num & 0xff;
562     num >>= 8;
563   }
564 }
565
566
567 /**
568  * gst_ebml_write_uint:
569  * @ebml: #GstEbmlWrite
570  * @id: Element ID.
571  * @num: Number to be written.
572  *
573  * Write uint element.
574  */
575 void
576 gst_ebml_write_uint (GstEbmlWrite * ebml, guint32 id, guint64 num)
577 {
578   GstBuffer *buf;
579   guint8 *data_start, *data_end;
580   guint size = gst_ebml_write_get_uint_size (num);
581   GstMapInfo map;
582
583   buf = gst_ebml_write_element_new (ebml, &map, sizeof (num));
584   data_end = data_start = map.data;
585
586   /* write */
587   gst_ebml_write_element_id (&data_end, id);
588   gst_ebml_write_element_size (&data_end, size);
589   gst_ebml_write_set_uint (&data_end, num, size);
590   gst_buffer_unmap (buf, &map);
591   gst_buffer_set_size (buf, (data_end - data_start));
592
593   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
594 }
595
596
597 /**
598  * gst_ebml_write_sint:
599  * @ebml: #GstEbmlWrite
600  * @id: Element ID.
601  * @num: Number to be written.
602  *
603  * Write sint element.
604  */
605 void
606 gst_ebml_write_sint (GstEbmlWrite * ebml, guint32 id, gint64 num)
607 {
608   GstBuffer *buf;
609   guint8 *data_start, *data_end;
610   GstMapInfo map;
611
612   /* if the signed number is on the edge of a extra-byte,
613    * then we'll fall over when detecting it. Example: if I
614    * have a number (-)0x8000 (G_MINSHORT), then my abs()<<1
615    * will be 0x10000; this is G_MAXUSHORT+1! So: if (<0) -1. */
616   guint64 unum = (num < 0 ? (-num - 1) << 1 : num << 1);
617   guint size = gst_ebml_write_get_uint_size (unum);
618
619   buf = gst_ebml_write_element_new (ebml, &map, sizeof (num));
620   data_end = data_start = map.data;
621
622   /* make unsigned */
623   if (num >= 0) {
624     unum = num;
625   } else {
626     unum = ((guint64) 0x80) << ((size - 1) * 8);
627     unum += num;
628     unum |= ((guint64) 0x80) << ((size - 1) * 8);
629   }
630
631   /* write */
632   gst_ebml_write_element_id (&data_end, id);
633   gst_ebml_write_element_size (&data_end, size);
634   gst_ebml_write_set_uint (&data_end, unum, size);
635   gst_buffer_unmap (buf, &map);
636   gst_buffer_set_size (buf, (data_end - data_start));
637
638   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
639 }
640
641
642 /**
643  * gst_ebml_write_float:
644  * @ebml: #GstEbmlWrite
645  * @id: Element ID.
646  * @num: Number to be written.
647  *
648  * Write float element.
649  */
650 void
651 gst_ebml_write_float (GstEbmlWrite * ebml, guint32 id, gdouble num)
652 {
653   GstBuffer *buf;
654   GstMapInfo map;
655   guint8 *data_start, *data_end;
656
657   buf = gst_ebml_write_element_new (ebml, &map, sizeof (num));
658   data_end = data_start = map.data;
659
660   gst_ebml_write_element_id (&data_end, id);
661   gst_ebml_write_element_size (&data_end, 8);
662   num = GDOUBLE_TO_BE (num);
663   gst_ebml_write_element_data (&data_end, (guint8 *) & num, 8);
664   gst_buffer_unmap (buf, &map);
665   gst_buffer_set_size (buf, (data_end - data_start));
666
667   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
668 }
669
670
671 /**
672  * gst_ebml_write_ascii:
673  * @ebml: #GstEbmlWrite
674  * @id: Element ID.
675  * @str: String to be written.
676  *
677  * Write string element.
678  */
679 void
680 gst_ebml_write_ascii (GstEbmlWrite * ebml, guint32 id, const gchar * str)
681 {
682   gint len = strlen (str) + 1;  /* add trailing '\0' */
683   GstBuffer *buf;
684   GstMapInfo map;
685   guint8 *data_start, *data_end;
686
687   buf = gst_ebml_write_element_new (ebml, &map, len);
688   data_end = data_start = map.data;
689
690   gst_ebml_write_element_id (&data_end, id);
691   gst_ebml_write_element_size (&data_end, len);
692   gst_ebml_write_element_data (&data_end, (guint8 *) str, len);
693   gst_buffer_unmap (buf, &map);
694   gst_buffer_set_size (buf, (data_end - data_start));
695
696   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
697 }
698
699
700 /**
701  * gst_ebml_write_utf8:
702  * @ebml: #GstEbmlWrite
703  * @id: Element ID.
704  * @str: String to be written.
705  *
706  * Write utf8 encoded string element.
707  */
708 void
709 gst_ebml_write_utf8 (GstEbmlWrite * ebml, guint32 id, const gchar * str)
710 {
711   gst_ebml_write_ascii (ebml, id, str);
712 }
713
714
715 /**
716  * gst_ebml_write_date:
717  * @ebml: #GstEbmlWrite
718  * @id: Element ID.
719  * @date: Date in seconds since the unix epoch.
720  *
721  * Write date element.
722  */
723 void
724 gst_ebml_write_date (GstEbmlWrite * ebml, guint32 id, gint64 date)
725 {
726   gst_ebml_write_sint (ebml, id, (date - GST_EBML_DATE_OFFSET) * GST_SECOND);
727 }
728
729 /**
730  * gst_ebml_write_master_start:
731  * @ebml: #GstEbmlWrite
732  * @id: Element ID.
733  *
734  * Start wiriting mater element.
735  *
736  * Master writing is annoying. We use a size marker of
737  * the max. allowed length, so that we can later fill it
738  * in validly. 
739  *
740  * Returns: Master starting position.
741  */
742 guint64
743 gst_ebml_write_master_start (GstEbmlWrite * ebml, guint32 id)
744 {
745   guint64 pos = ebml->pos;
746   GstBuffer *buf;
747   GstMapInfo map;
748   guint8 *data_start, *data_end;
749
750   buf = gst_ebml_write_element_new (ebml, &map, 0);
751   data_end = data_start = map.data;
752
753   gst_ebml_write_element_id (&data_end, id);
754   pos += data_end - data_start;
755   gst_ebml_write_element_size (&data_end, GST_EBML_SIZE_UNKNOWN);
756   gst_buffer_unmap (buf, &map);
757   gst_buffer_set_size (buf, (data_end - data_start));
758
759   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
760
761   return pos;
762 }
763
764
765 /**
766  * gst_ebml_write_master_finish_full:
767  * @ebml: #GstEbmlWrite
768  * @startpos: Master starting position.
769  *
770  * Finish writing master element.  Size of master element is difference between
771  * current position and the element start, and @extra_size added to this.
772  */
773 void
774 gst_ebml_write_master_finish_full (GstEbmlWrite * ebml, guint64 startpos,
775     guint64 extra_size)
776 {
777   guint64 pos = ebml->pos;
778   guint8 *data = g_malloc (8);
779   GstBuffer *buf = gst_buffer_new_wrapped (data, 8);
780
781   gst_ebml_write_seek (ebml, startpos);
782
783   GST_WRITE_UINT64_BE (data,
784       (G_GINT64_CONSTANT (1) << 56) | (pos - startpos - 8 + extra_size));
785
786   gst_ebml_write_element_push (ebml, buf, NULL, NULL);
787   gst_ebml_write_seek (ebml, pos);
788 }
789
790 void
791 gst_ebml_write_master_finish (GstEbmlWrite * ebml, guint64 startpos)
792 {
793   gst_ebml_write_master_finish_full (ebml, startpos, 0);
794 }
795
796 /**
797  * gst_ebml_write_binary:
798  * @ebml: #GstEbmlWrite
799  * @id: Element ID.
800  * @binary: Data to be written.
801  * @length: Length of the data
802  *
803  * Write an element with binary data.
804  */
805 void
806 gst_ebml_write_binary (GstEbmlWrite * ebml,
807     guint32 id, guint8 * binary, guint64 length)
808 {
809   GstBuffer *buf;
810   GstMapInfo map;
811   guint8 *data_start, *data_end;
812
813   buf = gst_ebml_write_element_new (ebml, &map, length);
814   data_end = data_start = map.data;
815
816   gst_ebml_write_element_id (&data_end, id);
817   gst_ebml_write_element_size (&data_end, length);
818   gst_ebml_write_element_data (&data_end, binary, length);
819   gst_buffer_unmap (buf, &map);
820   gst_buffer_set_size (buf, (data_end - data_start));
821
822   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
823 }
824
825
826 /**
827  * gst_ebml_write_buffer_header:
828  * @ebml: #GstEbmlWrite
829  * @id: Element ID.
830  * @length: Length of the data
831  * 
832  * Write header of the binary element (use with gst_ebml_write_buffer function).
833  * 
834  * For things like video frames and audio samples,
835  * you want to use this function, as it doesn't have
836  * the overhead of memcpy() that other functions
837  * such as write_binary() do have.
838  */
839 void
840 gst_ebml_write_buffer_header (GstEbmlWrite * ebml, guint32 id, guint64 length)
841 {
842   GstBuffer *buf;
843   GstMapInfo map;
844   guint8 *data_start, *data_end;
845
846   buf = gst_ebml_write_element_new (ebml, &map, 0);
847   data_end = data_start = map.data;
848
849   gst_ebml_write_element_id (&data_end, id);
850   gst_ebml_write_element_size (&data_end, length);
851   gst_buffer_unmap (buf, &map);
852   gst_buffer_set_size (buf, (data_end - data_start));
853
854   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
855 }
856
857
858 /**
859  * gst_ebml_write_buffer:
860  * @ebml: #GstEbmlWrite
861  * @buf: #GstBuffer cointaining the data.
862  *
863  * Write  binary element (see gst_ebml_write_buffer_header).
864  */
865 void
866 gst_ebml_write_buffer (GstEbmlWrite * ebml, GstBuffer * buf)
867 {
868   gst_ebml_write_element_push (ebml, buf, NULL, NULL);
869 }
870
871
872 /**
873  * gst_ebml_replace_uint:
874  * @ebml: #GstEbmlWrite
875  * @pos: Position of the uint that should be replaced.
876  * @num: New value.
877  *
878  * Replace uint with a new value.
879  * 
880  * When replacing a uint, we assume that it is *always*
881  * 8-byte, since that's the safest guess we can do. This
882  * is just for simplicity.
883  *
884  * FIXME: this function needs to be replaced with something
885  * proper. This is a crude hack.
886  */
887 void
888 gst_ebml_replace_uint (GstEbmlWrite * ebml, guint64 pos, guint64 num)
889 {
890   guint64 oldpos = ebml->pos;
891   guint8 *data_start, *data_end;
892   GstBuffer *buf;
893
894   data_start = g_malloc (8);
895   data_end = data_start;
896   buf = gst_buffer_new_wrapped (data_start, 8);
897
898   gst_ebml_write_seek (ebml, pos);
899   gst_ebml_write_set_uint (&data_end, num, 8);
900
901   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
902   gst_ebml_write_seek (ebml, oldpos);
903 }
904
905 /**
906  * gst_ebml_write_header:
907  * @ebml: #GstEbmlWrite
908  * @doctype: Document type.
909  * @version: Document type version.
910  * 
911  * Write EBML header.
912  */
913 void
914 gst_ebml_write_header (GstEbmlWrite * ebml, const gchar * doctype,
915     guint version)
916 {
917   guint64 pos;
918
919   /* write the basic EBML header */
920   gst_ebml_write_set_cache (ebml, 0x40);
921   pos = gst_ebml_write_master_start (ebml, GST_EBML_ID_HEADER);
922 #if (GST_EBML_VERSION != 1)
923   gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLVERSION, GST_EBML_VERSION);
924   gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLREADVERSION, GST_EBML_VERSION);
925 #endif
926 #if 0
927   /* we don't write these until they're "non-default" (never!) */
928   gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLMAXIDLENGTH, sizeof (guint32));
929   gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLMAXSIZELENGTH, sizeof (guint64));
930 #endif
931   gst_ebml_write_ascii (ebml, GST_EBML_ID_DOCTYPE, doctype);
932   gst_ebml_write_uint (ebml, GST_EBML_ID_DOCTYPEVERSION, version);
933   gst_ebml_write_uint (ebml, GST_EBML_ID_DOCTYPEREADVERSION, version);
934   gst_ebml_write_master_finish (ebml, pos);
935   gst_ebml_write_flush_cache (ebml, FALSE, 0);
936 }