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