Merge branch 'master' into 0.11
[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, guint8 ** data_out, 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_TIMESTAMP (buf) = ebml->timestamp;
292
293   *data_out = GST_BUFFER_DATA (buf);
294
295   return buf;
296 }
297
298
299 /**
300  * gst_ebml_write_element_id:
301  * @data_inout: Pointer to data pointer
302  * @id: Element ID that should be written.
303  * 
304  * Write element ID into a buffer.
305  */
306 static void
307 gst_ebml_write_element_id (guint8 ** data_inout, guint32 id)
308 {
309   guint8 *data = *data_inout;
310   guint bytes = 4, mask = 0x10;
311
312   /* get ID length */
313   while (!(id & (mask << ((bytes - 1) * 8))) && bytes > 0) {
314     mask <<= 1;
315     bytes--;
316   }
317
318   /* if invalid ID, use dummy */
319   if (bytes == 0) {
320     GST_WARNING ("Invalid ID, voiding");
321     bytes = 1;
322     id = GST_EBML_ID_VOID;
323   }
324
325   /* write out, BE */
326   *data_inout += bytes;
327   while (bytes--) {
328     data[bytes] = id & 0xff;
329     id >>= 8;
330   }
331 }
332
333
334 /**
335  * gst_ebml_write_element_size:
336  * @data_inout: Pointer to data pointer
337  * @size: Element length.
338  *
339  * Write element length into a buffer.
340  */
341 static void
342 gst_ebml_write_element_size (guint8 ** data_inout, guint64 size)
343 {
344   guint8 *data = *data_inout;
345   guint bytes = 1, mask = 0x80;
346
347   if (size != GST_EBML_SIZE_UNKNOWN) {
348     /* how many bytes? - use mask-1 because an all-1 bitset is not allowed */
349     while ((size >> ((bytes - 1) * 8)) >= (mask - 1) && bytes <= 8) {
350       mask >>= 1;
351       bytes++;
352     }
353
354     /* if invalid size, use max. */
355     if (bytes > 8) {
356       GST_WARNING ("Invalid size, writing size unknown");
357       mask = 0x01;
358       bytes = 8;
359       /* Now here's a real FIXME: we cannot read those yet! */
360       size = GST_EBML_SIZE_UNKNOWN;
361     }
362   } else {
363     mask = 0x01;
364     bytes = 8;
365   }
366
367   /* write out, BE, with length size marker */
368   *data_inout += bytes;
369   while (bytes-- > 0) {
370     data[bytes] = size & 0xff;
371     size >>= 8;
372     if (!bytes)
373       *data |= mask;
374   }
375 }
376
377
378 /**
379  * gst_ebml_write_element_data:
380  * @data_inout: Pointer to data pointer
381  * @write: Data that should be written.
382  * @length: Length of the data.
383  *
384  * Write element data into a buffer.
385  */
386 static void
387 gst_ebml_write_element_data (guint8 ** data_inout, guint8 * write,
388     guint64 length)
389 {
390   memcpy (*data_inout, write, length);
391   *data_inout += length;
392 }
393
394
395 /**
396  * gst_ebml_write_element_push:
397  * @ebml: #GstEbmlWrite
398  * @buf: #GstBuffer to be written.
399  * @buf_data: Start of data to push from @buf (or NULL for whole buffer).
400  * @buf_data_end: Data pointer positioned after the last byte in @buf_data (or
401  * NULL for whole buffer).
402  * 
403  * Write out buffer by moving it to the next element.
404  */
405 static void
406 gst_ebml_write_element_push (GstEbmlWrite * ebml, GstBuffer * buf,
407     guint8 * buf_data, guint8 * buf_data_end)
408 {
409   guint data_size;
410
411   if (!buf_data)
412     buf_data = GST_BUFFER_DATA (buf);
413
414   if (buf_data_end)
415     data_size = buf_data_end - buf_data;
416   else
417     data_size = GST_BUFFER_SIZE (buf);
418
419   ebml->pos += data_size;
420
421   /* if there's no cache, then don't push it! */
422   if (ebml->writing_streamheader) {
423     gst_byte_writer_put_data (ebml->streamheader, buf_data, data_size);
424   }
425   if (ebml->cache) {
426     gst_byte_writer_put_data (ebml->cache, buf_data, data_size);
427     gst_buffer_unref (buf);
428     return;
429   }
430
431   if (ebml->last_write_result == GST_FLOW_OK) {
432     buf = gst_buffer_make_metadata_writable (buf);
433     gst_buffer_set_caps (buf, ebml->caps);
434     GST_BUFFER_OFFSET (buf) = ebml->pos - data_size;
435     GST_BUFFER_OFFSET_END (buf) = ebml->pos;
436     if (ebml->writing_streamheader) {
437       GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
438     }
439     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
440
441     if (GST_BUFFER_OFFSET (buf) != ebml->last_pos) {
442       gst_ebml_writer_send_new_segment_event (ebml, GST_BUFFER_OFFSET (buf));
443       GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
444     }
445     ebml->last_pos = ebml->pos;
446     ebml->last_write_result = gst_pad_push (ebml->srcpad, buf);
447   } else {
448     gst_buffer_unref (buf);
449   }
450 }
451
452
453 /**
454  * gst_ebml_write_seek:
455  * @ebml: #GstEbmlWrite
456  * @pos: Seek position.
457  * 
458  * Seek.
459  */
460 void
461 gst_ebml_write_seek (GstEbmlWrite * ebml, guint64 pos)
462 {
463   if (ebml->writing_streamheader) {
464     GST_DEBUG ("wanting to seek to pos %" G_GUINT64_FORMAT, pos);
465     if (pos >= ebml->streamheader_pos &&
466         pos <= ebml->streamheader_pos + ebml->streamheader->parent.size) {
467       gst_byte_writer_set_pos (ebml->streamheader,
468           pos - ebml->streamheader_pos);
469       GST_DEBUG ("seeked in streamheader to position %" G_GUINT64_FORMAT,
470           pos - ebml->streamheader_pos);
471     } else {
472       GST_WARNING
473           ("we are writing streamheader still and seek is out of bounds");
474     }
475   }
476   /* Cache seeking. A bit dangerous, we assume the client writer
477    * knows what he's doing... */
478   if (ebml->cache) {
479     /* within bounds? */
480     if (pos >= ebml->cache_pos &&
481         pos <= ebml->cache_pos + ebml->cache->parent.size) {
482       GST_DEBUG ("seeking in cache to %" G_GUINT64_FORMAT, pos);
483       ebml->pos = pos;
484       gst_byte_writer_set_pos (ebml->cache, ebml->pos - ebml->cache_pos);
485       return;
486     } else {
487       GST_LOG ("Seek outside cache range. Clearing...");
488       gst_ebml_write_flush_cache (ebml, FALSE, GST_CLOCK_TIME_NONE);
489     }
490   }
491
492   GST_INFO ("scheduling seek to %" G_GUINT64_FORMAT, pos);
493   ebml->pos = pos;
494 }
495
496
497 /**
498  * gst_ebml_write_get_uint_size:
499  * @num: Number to be encoded.
500  * 
501  * Get number of bytes needed to write a uint.
502  *
503  * Returns: Encoded uint length.
504  */
505 static guint
506 gst_ebml_write_get_uint_size (guint64 num)
507 {
508   guint size = 1;
509
510   /* get size */
511   while (num >= (G_GINT64_CONSTANT (1) << (size * 8)) && size < 8) {
512     size++;
513   }
514
515   return size;
516 }
517
518
519 /**
520  * gst_ebml_write_set_uint:
521  * @data_inout: Pointer to data pointer
522  * @num: Number to be written.
523  * @size: Encoded number length.
524  *
525  * Write an uint into a buffer.
526  */
527 static void
528 gst_ebml_write_set_uint (guint8 ** data_inout, guint64 num, guint size)
529 {
530   guint8 *data = *data_inout;
531
532   *data_inout += size;
533
534   while (size-- > 0) {
535     data[size] = num & 0xff;
536     num >>= 8;
537   }
538 }
539
540
541 /**
542  * gst_ebml_write_uint:
543  * @ebml: #GstEbmlWrite
544  * @id: Element ID.
545  * @num: Number to be written.
546  *
547  * Write uint element.
548  */
549 void
550 gst_ebml_write_uint (GstEbmlWrite * ebml, guint32 id, guint64 num)
551 {
552   GstBuffer *buf;
553   guint8 *data_start, *data_end;
554   guint size = gst_ebml_write_get_uint_size (num);
555
556   buf = gst_ebml_write_element_new (ebml, &data_start, sizeof (num));
557   data_end = data_start;
558
559   /* write */
560   gst_ebml_write_element_id (&data_end, id);
561   gst_ebml_write_element_size (&data_end, size);
562   gst_ebml_write_set_uint (&data_end, num, size);
563   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
564 }
565
566
567 /**
568  * gst_ebml_write_sint:
569  * @ebml: #GstEbmlWrite
570  * @id: Element ID.
571  * @num: Number to be written.
572  *
573  * Write sint element.
574  */
575 void
576 gst_ebml_write_sint (GstEbmlWrite * ebml, guint32 id, gint64 num)
577 {
578   GstBuffer *buf;
579   guint8 *data_start, *data_end;
580   /* if the signed number is on the edge of a extra-byte,
581    * then we'll fall over when detecting it. Example: if I
582    * have a number (-)0x8000 (G_MINSHORT), then my abs()<<1
583    * will be 0x10000; this is G_MAXUSHORT+1! So: if (<0) -1. */
584   guint64 unum = (num < 0 ? (-num - 1) << 1 : num << 1);
585   guint size = gst_ebml_write_get_uint_size (unum);
586
587   buf = gst_ebml_write_element_new (ebml, &data_start, sizeof (num));
588   data_end = data_start;
589
590   /* make unsigned */
591   if (num >= 0) {
592     unum = num;
593   } else {
594     unum = 0x80 << (size - 1);
595     unum += num;
596     unum |= 0x80 << (size - 1);
597   }
598
599   /* write */
600   gst_ebml_write_element_id (&data_end, id);
601   gst_ebml_write_element_size (&data_end, size);
602   gst_ebml_write_set_uint (&data_end, unum, size);
603   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
604 }
605
606
607 /**
608  * gst_ebml_write_float:
609  * @ebml: #GstEbmlWrite
610  * @id: Element ID.
611  * @num: Number to be written.
612  *
613  * Write float element.
614  */
615 void
616 gst_ebml_write_float (GstEbmlWrite * ebml, guint32 id, gdouble num)
617 {
618   GstBuffer *buf;
619   guint8 *data_start, *data_end;
620
621   buf = gst_ebml_write_element_new (ebml, &data_start, sizeof (num));
622   data_end = data_start;
623
624   gst_ebml_write_element_id (&data_end, id);
625   gst_ebml_write_element_size (&data_end, 8);
626   num = GDOUBLE_TO_BE (num);
627   gst_ebml_write_element_data (&data_end, (guint8 *) & num, 8);
628   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
629 }
630
631
632 /**
633  * gst_ebml_write_ascii:
634  * @ebml: #GstEbmlWrite
635  * @id: Element ID.
636  * @str: String to be written.
637  *
638  * Write string element.
639  */
640 void
641 gst_ebml_write_ascii (GstEbmlWrite * ebml, guint32 id, const gchar * str)
642 {
643   gint len = strlen (str) + 1;  /* add trailing '\0' */
644   GstBuffer *buf;
645   guint8 *data_start, *data_end;
646
647   buf = gst_ebml_write_element_new (ebml, &data_start, len);
648   data_end = data_start;
649
650   gst_ebml_write_element_id (&data_end, id);
651   gst_ebml_write_element_size (&data_end, len);
652   gst_ebml_write_element_data (&data_end, (guint8 *) str, len);
653   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
654 }
655
656
657 /**
658  * gst_ebml_write_utf8:
659  * @ebml: #GstEbmlWrite
660  * @id: Element ID.
661  * @str: String to be written.
662  *
663  * Write utf8 encoded string element.
664  */
665 void
666 gst_ebml_write_utf8 (GstEbmlWrite * ebml, guint32 id, const gchar * str)
667 {
668   gst_ebml_write_ascii (ebml, id, str);
669 }
670
671
672 /**
673  * gst_ebml_write_date:
674  * @ebml: #GstEbmlWrite
675  * @id: Element ID.
676  * @date: Date in seconds since the unix epoch.
677  *
678  * Write date element.
679  */
680 void
681 gst_ebml_write_date (GstEbmlWrite * ebml, guint32 id, gint64 date)
682 {
683   gst_ebml_write_sint (ebml, id, (date - GST_EBML_DATE_OFFSET) * GST_SECOND);
684 }
685
686 /**
687  * gst_ebml_write_master_start:
688  * @ebml: #GstEbmlWrite
689  * @id: Element ID.
690  *
691  * Start wiriting mater element.
692  *
693  * Master writing is annoying. We use a size marker of
694  * the max. allowed length, so that we can later fill it
695  * in validly. 
696  *
697  * Returns: Master starting position.
698  */
699 guint64
700 gst_ebml_write_master_start (GstEbmlWrite * ebml, guint32 id)
701 {
702   guint64 pos = ebml->pos;
703   GstBuffer *buf;
704   guint8 *data_start, *data_end;
705
706   buf = gst_ebml_write_element_new (ebml, &data_start, 0);
707   data_end = data_start;
708
709   gst_ebml_write_element_id (&data_end, id);
710   pos += data_end - data_start;
711   gst_ebml_write_element_size (&data_end, GST_EBML_SIZE_UNKNOWN);
712   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
713
714   return pos;
715 }
716
717
718 /**
719  * gst_ebml_write_master_finish_full:
720  * @ebml: #GstEbmlWrite
721  * @startpos: Master starting position.
722  *
723  * Finish writing master element.  Size of master element is difference between
724  * current position and the element start, and @extra_size added to this.
725  */
726 void
727 gst_ebml_write_master_finish_full (GstEbmlWrite * ebml, guint64 startpos,
728     guint64 extra_size)
729 {
730   guint64 pos = ebml->pos;
731   GstBuffer *buf;
732
733   gst_ebml_write_seek (ebml, startpos);
734
735   buf = gst_buffer_new_and_alloc (8);
736   GST_WRITE_UINT64_BE (GST_BUFFER_DATA (buf),
737       (G_GINT64_CONSTANT (1) << 56) | (pos - startpos - 8 + extra_size));
738
739   gst_ebml_write_element_push (ebml, buf, NULL, NULL);
740   gst_ebml_write_seek (ebml, pos);
741 }
742
743 void
744 gst_ebml_write_master_finish (GstEbmlWrite * ebml, guint64 startpos)
745 {
746   gst_ebml_write_master_finish_full (ebml, startpos, 0);
747 }
748
749 /**
750  * gst_ebml_write_binary:
751  * @ebml: #GstEbmlWrite
752  * @id: Element ID.
753  * @binary: Data to be written.
754  * @length: Length of the data
755  *
756  * Write an element with binary data.
757  */
758 void
759 gst_ebml_write_binary (GstEbmlWrite * ebml,
760     guint32 id, guint8 * binary, guint64 length)
761 {
762   GstBuffer *buf;
763   guint8 *data_start, *data_end;
764
765   buf = gst_ebml_write_element_new (ebml, &data_start, length);
766   data_end = data_start;
767
768   gst_ebml_write_element_id (&data_end, id);
769   gst_ebml_write_element_size (&data_end, length);
770   gst_ebml_write_element_data (&data_end, binary, length);
771   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
772 }
773
774
775 /**
776  * gst_ebml_write_buffer_header:
777  * @ebml: #GstEbmlWrite
778  * @id: Element ID.
779  * @length: Length of the data
780  * 
781  * Write header of the binary element (use with gst_ebml_write_buffer function).
782  * 
783  * For things like video frames and audio samples,
784  * you want to use this function, as it doesn't have
785  * the overhead of memcpy() that other functions
786  * such as write_binary() do have.
787  */
788 void
789 gst_ebml_write_buffer_header (GstEbmlWrite * ebml, guint32 id, guint64 length)
790 {
791   GstBuffer *buf;
792   guint8 *data_start, *data_end;
793
794   buf = gst_ebml_write_element_new (ebml, &data_start, 0);
795   data_end = data_start;
796
797   gst_ebml_write_element_id (&data_end, id);
798   gst_ebml_write_element_size (&data_end, length);
799   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
800 }
801
802
803 /**
804  * gst_ebml_write_buffer:
805  * @ebml: #GstEbmlWrite
806  * @buf: #GstBuffer cointaining the data.
807  *
808  * Write  binary element (see gst_ebml_write_buffer_header).
809  */
810 void
811 gst_ebml_write_buffer (GstEbmlWrite * ebml, GstBuffer * buf)
812 {
813   gst_ebml_write_element_push (ebml, buf, NULL, NULL);
814 }
815
816
817 /**
818  * gst_ebml_replace_uint:
819  * @ebml: #GstEbmlWrite
820  * @pos: Position of the uint that should be replaced.
821  * @num: New value.
822  *
823  * Replace uint with a new value.
824  * 
825  * When replacing a uint, we assume that it is *always*
826  * 8-byte, since that's the safest guess we can do. This
827  * is just for simplicity.
828  *
829  * FIXME: this function needs to be replaced with something
830  * proper. This is a crude hack.
831  */
832 void
833 gst_ebml_replace_uint (GstEbmlWrite * ebml, guint64 pos, guint64 num)
834 {
835   guint64 oldpos = ebml->pos;
836   GstBuffer *buf = gst_buffer_new_and_alloc (8);
837   guint8 *data_start, *data_end;
838
839   data_start = GST_BUFFER_DATA (buf);
840   data_end = data_start;
841
842   gst_ebml_write_seek (ebml, pos);
843   gst_ebml_write_set_uint (&data_end, num, 8);
844
845   gst_ebml_write_element_push (ebml, buf, data_start, data_end);
846   gst_ebml_write_seek (ebml, oldpos);
847 }
848
849 /**
850  * gst_ebml_write_header:
851  * @ebml: #GstEbmlWrite
852  * @doctype: Document type.
853  * @version: Document type version.
854  * 
855  * Write EBML header.
856  */
857 void
858 gst_ebml_write_header (GstEbmlWrite * ebml, const gchar * doctype,
859     guint version)
860 {
861   guint64 pos;
862
863   /* write the basic EBML header */
864   gst_ebml_write_set_cache (ebml, 0x40);
865   pos = gst_ebml_write_master_start (ebml, GST_EBML_ID_HEADER);
866 #if (GST_EBML_VERSION != 1)
867   gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLVERSION, GST_EBML_VERSION);
868   gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLREADVERSION, GST_EBML_VERSION);
869 #endif
870 #if 0
871   /* we don't write these until they're "non-default" (never!) */
872   gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLMAXIDLENGTH, sizeof (guint32));
873   gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLMAXSIZELENGTH, sizeof (guint64));
874 #endif
875   gst_ebml_write_ascii (ebml, GST_EBML_ID_DOCTYPE, doctype);
876   gst_ebml_write_uint (ebml, GST_EBML_ID_DOCTYPEVERSION, version);
877   gst_ebml_write_uint (ebml, GST_EBML_ID_DOCTYPEREADVERSION, version);
878   gst_ebml_write_master_finish (ebml, pos);
879   gst_ebml_write_flush_cache (ebml, FALSE, 0);
880 }