2 * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3 * (c) 2005 Michal Benes <michal.benes@xeris.cz>
5 * ebml-write.c: write EBML data to file/stream
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.
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.
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.
29 #include "ebml-write.h"
33 GST_DEBUG_CATEGORY_STATIC (gst_ebml_write_debug);
34 #define GST_CAT_DEFAULT gst_ebml_write_debug
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,
41 static void gst_ebml_write_finalize (GObject * object);
44 gst_ebml_write_base_init (gpointer g_class)
49 gst_ebml_write_class_init (GstEbmlWriteClass * klass)
51 GObjectClass *object = G_OBJECT_CLASS (klass);
53 object->finalize = gst_ebml_write_finalize;
57 gst_ebml_write_init (GstEbmlWrite * ebml, GstEbmlWriteClass * klass)
61 ebml->last_pos = G_MAXUINT64; /* force newsegment event */
64 ebml->streamheader = NULL;
65 ebml->streamheader_pos = 0;
66 ebml->writing_streamheader = FALSE;
71 gst_ebml_write_finalize (GObject * object)
73 GstEbmlWrite *ebml = GST_EBML_WRITE (object);
75 gst_object_unref (ebml->srcpad);
78 gst_byte_writer_free (ebml->cache);
82 if (ebml->streamheader) {
83 gst_byte_writer_free (ebml->streamheader);
84 ebml->streamheader = NULL;
88 gst_caps_unref (ebml->caps);
92 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
98 * @srcpad: Source pad to which the output will be pushed.
100 * Creates a new #GstEbmlWrite.
102 * Returns: a new #GstEbmlWrite
105 gst_ebml_write_new (GstPad * srcpad)
108 GST_EBML_WRITE (g_object_new (GST_TYPE_EBML_WRITE, NULL));
110 ebml->srcpad = gst_object_ref (srcpad);
111 ebml->timestamp = GST_CLOCK_TIME_NONE;
113 gst_ebml_write_reset (ebml);
120 * gst_ebml_write_reset:
121 * @ebml: a #GstEbmlWrite.
123 * Reset internal state of #GstEbmlWrite.
126 gst_ebml_write_reset (GstEbmlWrite * ebml)
129 ebml->last_pos = G_MAXUINT64; /* force newsegment event */
132 gst_byte_writer_free (ebml->cache);
137 gst_caps_unref (ebml->caps);
141 ebml->last_write_result = GST_FLOW_OK;
142 ebml->timestamp = GST_CLOCK_TIME_NONE;
147 * gst_ebml_last_write_result:
148 * @ebml: a #GstEbmlWrite.
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.
154 gst_ebml_last_write_result (GstEbmlWrite * ebml)
156 GstFlowReturn res = ebml->last_write_result;
158 ebml->last_write_result = GST_FLOW_OK;
165 gst_ebml_start_streamheader (GstEbmlWrite * ebml)
167 g_return_if_fail (ebml->streamheader == NULL);
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;
176 gst_ebml_stop_streamheader (GstEbmlWrite * ebml)
180 if (!ebml->streamheader)
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));
187 ebml->writing_streamheader = FALSE;
192 * gst_ebml_write_set_cache:
193 * @ebml: a #GstEbmlWrite.
194 * @size: size of the cache.
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.
204 gst_ebml_write_set_cache (GstEbmlWrite * ebml, guint size)
206 g_return_if_fail (ebml->cache == NULL);
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;
214 gst_ebml_writer_send_new_segment_event (GstEbmlWrite * ebml, guint64 new_pos)
218 GST_INFO ("seeking to %" G_GUINT64_FORMAT, new_pos);
220 res = gst_pad_push_event (ebml->srcpad,
221 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, new_pos, -1, 0));
224 GST_WARNING ("seek to %" G_GUINT64_FORMAT "failed", new_pos);
230 * gst_ebml_write_flush_cache:
231 * @ebml: a #GstEbmlWrite.
232 * @timestamp: timestamp of the buffer.
237 gst_ebml_write_flush_cache (GstEbmlWrite * ebml, gboolean is_keyframe,
238 GstClockTime timestamp)
245 buffer = gst_byte_writer_free_and_get_buffer (ebml->cache);
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);
257 if (ebml->writing_streamheader) {
258 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
261 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
263 ebml->last_pos = ebml->pos;
264 ebml->last_write_result = gst_pad_push (ebml->srcpad, buffer);
266 gst_buffer_unref (buffer);
272 * gst_ebml_write_element_new:
273 * @ebml: a #GstEbmlWrite.
274 * @size: size of the requested buffer.
276 * Create a buffer for one element. If there is
277 * a cache, use that instead.
279 * Returns: A new #GstBuffer.
282 gst_ebml_write_element_new (GstEbmlWrite * ebml, guint size)
284 /* Create new buffer of size + ID + length */
290 buf = gst_buffer_new_and_alloc (size);
291 GST_BUFFER_SIZE (buf) = 0;
292 GST_BUFFER_TIMESTAMP (buf) = ebml->timestamp;
299 * gst_ebml_write_element_id:
300 * @buf: Buffer to which id should be written.
301 * @id: Element ID that should be written.
303 * Write element ID into a buffer.
306 gst_ebml_write_element_id (GstBuffer * buf, guint32 id)
308 guint8 *data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
309 guint bytes = 4, mask = 0x10;
312 while (!(id & (mask << ((bytes - 1) * 8))) && bytes > 0) {
317 /* if invalid ID, use dummy */
319 GST_WARNING ("Invalid ID, voiding");
321 id = GST_EBML_ID_VOID;
325 GST_BUFFER_SIZE (buf) += bytes;
327 data[bytes] = id & 0xff;
334 * gst_ebml_write_element_size:
335 * @buf: #GstBuffer to which size should be written.
336 * @size: Element length.
338 * Write element length into a buffer.
341 gst_ebml_write_element_size (GstBuffer * buf, guint64 size)
343 guint8 *data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
344 guint bytes = 1, mask = 0x80;
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) {
353 /* if invalid size, use max. */
355 GST_WARNING ("Invalid size, writing size unknown");
358 /* Now here's a real FIXME: we cannot read those yet! */
359 size = GST_EBML_SIZE_UNKNOWN;
366 /* write out, BE, with length size marker */
367 GST_BUFFER_SIZE (buf) += bytes;
368 while (bytes-- > 0) {
369 data[bytes] = size & 0xff;
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.
383 * Write element data into a buffer.
386 gst_ebml_write_element_data (GstBuffer * buf, guint8 * write, guint64 length)
388 guint8 *data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
390 memcpy (data, write, length);
391 GST_BUFFER_SIZE (buf) += length;
396 * gst_ebml_write_element_push:
397 * @ebml: #GstEbmlWrite
398 * @buf: #GstBuffer to be written.
400 * Write out buffer by moving it to the next element.
403 gst_ebml_write_element_push (GstEbmlWrite * ebml, GstBuffer * buf)
405 guint data_size = GST_BUFFER_SIZE (buf);
407 ebml->pos += data_size;
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),
415 gst_byte_writer_put_data (ebml->cache, GST_BUFFER_DATA (buf), data_size);
416 gst_buffer_unref (buf);
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);
428 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
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);
434 ebml->last_pos = ebml->pos;
435 ebml->last_write_result = gst_pad_push (ebml->srcpad, buf);
437 gst_buffer_unref (buf);
443 * gst_ebml_write_seek:
444 * @ebml: #GstEbmlWrite
445 * @pos: Seek position.
450 gst_ebml_write_seek (GstEbmlWrite * ebml, guint64 pos)
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);
462 ("we are writing streamheader still and seek is out of bounds");
465 /* Cache seeking. A bit dangerous, we assume the client writer
466 * knows what he's doing... */
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);
473 gst_byte_writer_set_pos (ebml->cache, ebml->pos - ebml->cache_pos);
476 GST_LOG ("Seek outside cache range. Clearing...");
477 gst_ebml_write_flush_cache (ebml, FALSE, GST_CLOCK_TIME_NONE);
481 GST_INFO ("scheduling seek to %" G_GUINT64_FORMAT, pos);
487 * gst_ebml_write_get_uint_size:
488 * @num: Number to be encoded.
490 * Get number of bytes needed to write a uint.
492 * Returns: Encoded uint length.
495 gst_ebml_write_get_uint_size (guint64 num)
500 while (num >= (G_GINT64_CONSTANT (1) << (size * 8)) && size < 8) {
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.
514 * Write an uint into a buffer.
517 gst_ebml_write_set_uint (GstBuffer * buf, guint64 num, guint size)
521 data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
522 GST_BUFFER_SIZE (buf) += size;
524 data[size] = num & 0xff;
531 * gst_ebml_write_uint:
532 * @ebml: #GstEbmlWrite
534 * @num: Number to be written.
536 * Write uint element.
539 gst_ebml_write_uint (GstEbmlWrite * ebml, guint32 id, guint64 num)
541 GstBuffer *buf = gst_ebml_write_element_new (ebml, sizeof (num));
542 guint size = gst_ebml_write_get_uint_size (num);
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);
553 * gst_ebml_write_sint:
554 * @ebml: #GstEbmlWrite
556 * @num: Number to be written.
558 * Write sint element.
561 gst_ebml_write_sint (GstEbmlWrite * ebml, guint32 id, gint64 num)
563 GstBuffer *buf = gst_ebml_write_element_new (ebml, sizeof (num));
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);
576 unum = 0x80 << (size - 1);
578 unum |= 0x80 << (size - 1);
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);
590 * gst_ebml_write_float:
591 * @ebml: #GstEbmlWrite
593 * @num: Number to be written.
595 * Write float element.
598 gst_ebml_write_float (GstEbmlWrite * ebml, guint32 id, gdouble num)
600 GstBuffer *buf = gst_ebml_write_element_new (ebml, sizeof (num));
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);
611 * gst_ebml_write_ascii:
612 * @ebml: #GstEbmlWrite
614 * @str: String to be written.
616 * Write string element.
619 gst_ebml_write_ascii (GstEbmlWrite * ebml, guint32 id, const gchar * str)
621 gint len = strlen (str) + 1; /* add trailing '\0' */
622 GstBuffer *buf = gst_ebml_write_element_new (ebml, len);
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);
632 * gst_ebml_write_utf8:
633 * @ebml: #GstEbmlWrite
635 * @str: String to be written.
637 * Write utf8 encoded string element.
640 gst_ebml_write_utf8 (GstEbmlWrite * ebml, guint32 id, const gchar * str)
642 gst_ebml_write_ascii (ebml, id, str);
647 * gst_ebml_write_date:
648 * @ebml: #GstEbmlWrite
650 * @date: Date in seconds since the unix epoch.
652 * Write date element.
655 gst_ebml_write_date (GstEbmlWrite * ebml, guint32 id, gint64 date)
657 gst_ebml_write_sint (ebml, id, (date - GST_EBML_DATE_OFFSET) * GST_SECOND);
661 * gst_ebml_write_master_start:
662 * @ebml: #GstEbmlWrite
665 * Start wiriting mater element.
667 * Master writing is annoying. We use a size marker of
668 * the max. allowed length, so that we can later fill it
671 * Returns: Master starting position.
674 gst_ebml_write_master_start (GstEbmlWrite * ebml, guint32 id)
676 guint64 pos = ebml->pos, t;
677 GstBuffer *buf = gst_ebml_write_element_new (ebml, 0);
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);
690 * gst_ebml_write_master_finish_full:
691 * @ebml: #GstEbmlWrite
692 * @startpos: Master starting position.
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.
698 gst_ebml_write_master_finish_full (GstEbmlWrite * ebml, guint64 startpos,
701 guint64 pos = ebml->pos;
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);
713 gst_ebml_write_master_finish (GstEbmlWrite * ebml, guint64 startpos)
715 gst_ebml_write_master_finish_full (ebml, startpos, 0);
719 * gst_ebml_write_binary:
720 * @ebml: #GstEbmlWrite
722 * @binary: Data to be written.
723 * @length: Length of the data
725 * Write an element with binary data.
728 gst_ebml_write_binary (GstEbmlWrite * ebml,
729 guint32 id, guint8 * binary, guint64 length)
731 GstBuffer *buf = gst_ebml_write_element_new (ebml, length);
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);
741 * gst_ebml_write_buffer_header:
742 * @ebml: #GstEbmlWrite
744 * @length: Length of the data
746 * Write header of the binary element (use with gst_ebml_write_buffer function).
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.
754 gst_ebml_write_buffer_header (GstEbmlWrite * ebml, guint32 id, guint64 length)
756 GstBuffer *buf = gst_ebml_write_element_new (ebml, 0);
758 gst_ebml_write_element_id (buf, id);
759 gst_ebml_write_element_size (buf, length);
760 gst_ebml_write_element_push (ebml, buf);
765 * gst_ebml_write_buffer:
766 * @ebml: #GstEbmlWrite
767 * @data: #GstBuffer cointaining the data.
769 * Write binary element (see gst_ebml_write_buffer_header).
772 gst_ebml_write_buffer (GstEbmlWrite * ebml, GstBuffer * data)
774 gst_ebml_write_element_push (ebml, data);
779 * gst_ebml_replace_uint:
780 * @ebml: #GstEbmlWrite
781 * @pos: Position of the uint that should be replaced.
784 * Replace uint with a new value.
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.
790 * FIXME: this function needs to be replaced with something
791 * proper. This is a crude hack.
794 gst_ebml_replace_uint (GstEbmlWrite * ebml, guint64 pos, guint64 num)
796 guint64 oldpos = ebml->pos;
797 GstBuffer *buf = gst_buffer_new_and_alloc (8);
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);
807 * gst_ebml_write_header:
808 * @ebml: #GstEbmlWrite
809 * @doctype: Document type.
810 * @version: Document type version.
815 gst_ebml_write_header (GstEbmlWrite * ebml, const gchar * doctype,
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);
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));
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);