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
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,
42 static void gst_ebml_write_finalize (GObject * object);
45 gst_ebml_write_class_init (GstEbmlWriteClass * klass)
47 GObjectClass *object = G_OBJECT_CLASS (klass);
49 object->finalize = gst_ebml_write_finalize;
53 gst_ebml_write_init (GstEbmlWrite * ebml)
57 ebml->last_pos = G_MAXUINT64; /* force segment event */
60 ebml->streamheader = NULL;
61 ebml->streamheader_pos = 0;
62 ebml->writing_streamheader = FALSE;
67 gst_ebml_write_finalize (GObject * object)
69 GstEbmlWrite *ebml = GST_EBML_WRITE (object);
71 gst_object_unref (ebml->srcpad);
74 gst_byte_writer_free (ebml->cache);
78 if (ebml->streamheader) {
79 gst_byte_writer_free (ebml->streamheader);
80 ebml->streamheader = NULL;
84 gst_caps_unref (ebml->caps);
88 G_OBJECT_CLASS (parent_class)->finalize (object);
94 * @srcpad: Source pad to which the output will be pushed.
96 * Creates a new #GstEbmlWrite.
98 * Returns: a new #GstEbmlWrite
101 gst_ebml_write_new (GstPad * srcpad)
104 GST_EBML_WRITE (g_object_new (GST_TYPE_EBML_WRITE, NULL));
106 ebml->srcpad = gst_object_ref (srcpad);
107 ebml->timestamp = GST_CLOCK_TIME_NONE;
109 gst_ebml_write_reset (ebml);
116 * gst_ebml_write_reset:
117 * @ebml: a #GstEbmlWrite.
119 * Reset internal state of #GstEbmlWrite.
122 gst_ebml_write_reset (GstEbmlWrite * ebml)
125 ebml->last_pos = G_MAXUINT64; /* force segment event */
128 gst_byte_writer_free (ebml->cache);
133 gst_caps_unref (ebml->caps);
137 ebml->last_write_result = GST_FLOW_OK;
138 ebml->timestamp = GST_CLOCK_TIME_NONE;
143 * gst_ebml_last_write_result:
144 * @ebml: a #GstEbmlWrite.
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.
150 gst_ebml_last_write_result (GstEbmlWrite * ebml)
152 GstFlowReturn res = ebml->last_write_result;
154 ebml->last_write_result = GST_FLOW_OK;
161 gst_ebml_start_streamheader (GstEbmlWrite * ebml)
163 g_return_if_fail (ebml->streamheader == NULL);
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;
172 gst_ebml_stop_streamheader (GstEbmlWrite * ebml)
176 if (!ebml->streamheader)
179 buffer = gst_byte_writer_free_and_get_buffer (ebml->streamheader);
180 ebml->streamheader = NULL;
181 GST_DEBUG ("Streamheader was size %d", gst_buffer_get_size (buffer));
183 ebml->writing_streamheader = FALSE;
188 * gst_ebml_write_set_cache:
189 * @ebml: a #GstEbmlWrite.
190 * @size: size of the cache.
193 * The idea is that you use this for writing a lot
194 * of small elements. This will just "queue" all of
195 * them and they'll be pushed to the next element all
196 * at once. This saves memory and time for buffer
197 * allocation and init, and it looks better.
200 gst_ebml_write_set_cache (GstEbmlWrite * ebml, guint size)
202 g_return_if_fail (ebml->cache == NULL);
204 GST_DEBUG ("Starting cache at %" G_GUINT64_FORMAT, ebml->pos);
205 ebml->cache = gst_byte_writer_new_with_size (size, FALSE);
206 ebml->cache_pos = ebml->pos;
210 gst_ebml_writer_send_segment_event (GstEbmlWrite * ebml, guint64 new_pos)
215 GST_INFO ("seeking to %" G_GUINT64_FORMAT, new_pos);
217 gst_segment_init (&segment, GST_FORMAT_BYTES);
218 segment.start = new_pos;
220 segment.position = 0;
222 res = gst_pad_push_event (ebml->srcpad, gst_event_new_segment (&segment));
225 GST_WARNING ("seek to %" G_GUINT64_FORMAT "failed", new_pos);
231 * gst_ebml_write_flush_cache:
232 * @ebml: a #GstEbmlWrite.
233 * @timestamp: timestamp of the buffer.
238 gst_ebml_write_flush_cache (GstEbmlWrite * ebml, gboolean is_keyframe,
239 GstClockTime timestamp)
246 buffer = gst_byte_writer_free_and_get_buffer (ebml->cache);
248 GST_DEBUG ("Flushing cache of size %d", gst_buffer_get_size (buffer));
249 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
250 GST_BUFFER_OFFSET (buffer) = ebml->pos - gst_buffer_get_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_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, guint8 ** data_out, guint size)
284 /* Create new buffer of size + ID + length */
290 buf = gst_buffer_new_and_alloc (size);
291 GST_BUFFER_TIMESTAMP (buf) = ebml->timestamp;
293 *data_out = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
300 * gst_ebml_write_element_id:
301 * @data_inout: Pointer to data pointer
302 * @id: Element ID that should be written.
304 * Write element ID into a buffer.
307 gst_ebml_write_element_id (guint8 ** data_inout, guint32 id)
309 guint8 *data = *data_inout;
310 guint bytes = 4, mask = 0x10;
313 while (!(id & (mask << ((bytes - 1) * 8))) && bytes > 0) {
318 /* if invalid ID, use dummy */
320 GST_WARNING ("Invalid ID, voiding");
322 id = GST_EBML_ID_VOID;
326 *data_inout += bytes;
328 data[bytes] = id & 0xff;
335 * gst_ebml_write_element_size:
336 * @data_inout: Pointer to data pointer
337 * @size: Element length.
339 * Write element length into a buffer.
342 gst_ebml_write_element_size (guint8 ** data_inout, guint64 size)
344 guint8 *data = *data_inout;
345 guint bytes = 1, mask = 0x80;
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) {
354 /* if invalid size, use max. */
356 GST_WARNING ("Invalid size, writing size unknown");
359 /* Now here's a real FIXME: we cannot read those yet! */
360 size = GST_EBML_SIZE_UNKNOWN;
367 /* write out, BE, with length size marker */
368 *data_inout += bytes;
369 while (bytes-- > 0) {
370 data[bytes] = size & 0xff;
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.
384 * Write element data into a buffer.
387 gst_ebml_write_element_data (guint8 ** data_inout, guint8 * write,
390 memcpy (*data_inout, write, length);
391 *data_inout += length;
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).
403 * Write out buffer by moving it to the next element.
406 gst_ebml_write_element_push (GstEbmlWrite * ebml, GstBuffer * buf,
407 guint8 * buf_data, guint8 * buf_data_end)
412 data_size = buf_data_end - buf_data;
414 data_size = gst_buffer_get_size (buf);
416 ebml->pos += data_size;
418 /* if there's no cache, then don't push it! */
419 if (ebml->writing_streamheader) {
421 buf_data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
422 gst_byte_writer_put_data (ebml->streamheader, buf_data, data_size);
426 buf_data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
427 gst_byte_writer_put_data (ebml->cache, buf_data, data_size);
428 gst_buffer_unmap (buf, buf_data, -1);
429 gst_buffer_unref (buf);
434 gst_buffer_unmap (buf, buf_data, -1);
436 if (ebml->last_write_result == GST_FLOW_OK) {
437 buf = gst_buffer_make_writable (buf);
438 GST_BUFFER_OFFSET (buf) = ebml->pos - data_size;
439 GST_BUFFER_OFFSET_END (buf) = ebml->pos;
440 if (ebml->writing_streamheader) {
441 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
443 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
445 if (GST_BUFFER_OFFSET (buf) != ebml->last_pos) {
446 gst_ebml_writer_send_segment_event (ebml, GST_BUFFER_OFFSET (buf));
447 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
449 ebml->last_pos = ebml->pos;
450 ebml->last_write_result = gst_pad_push (ebml->srcpad, buf);
452 gst_buffer_unref (buf);
458 * gst_ebml_write_seek:
459 * @ebml: #GstEbmlWrite
460 * @pos: Seek position.
465 gst_ebml_write_seek (GstEbmlWrite * ebml, guint64 pos)
467 if (ebml->writing_streamheader) {
468 GST_DEBUG ("wanting to seek to pos %" G_GUINT64_FORMAT, pos);
469 if (pos >= ebml->streamheader_pos &&
470 pos <= ebml->streamheader_pos + ebml->streamheader->parent.size) {
471 gst_byte_writer_set_pos (ebml->streamheader,
472 pos - ebml->streamheader_pos);
473 GST_DEBUG ("seeked in streamheader to position %" G_GUINT64_FORMAT,
474 pos - ebml->streamheader_pos);
477 ("we are writing streamheader still and seek is out of bounds");
480 /* Cache seeking. A bit dangerous, we assume the client writer
481 * knows what he's doing... */
484 if (pos >= ebml->cache_pos &&
485 pos <= ebml->cache_pos + ebml->cache->parent.size) {
486 GST_DEBUG ("seeking in cache to %" G_GUINT64_FORMAT, pos);
488 gst_byte_writer_set_pos (ebml->cache, ebml->pos - ebml->cache_pos);
491 GST_LOG ("Seek outside cache range. Clearing...");
492 gst_ebml_write_flush_cache (ebml, FALSE, GST_CLOCK_TIME_NONE);
496 GST_INFO ("scheduling seek to %" G_GUINT64_FORMAT, pos);
502 * gst_ebml_write_get_uint_size:
503 * @num: Number to be encoded.
505 * Get number of bytes needed to write a uint.
507 * Returns: Encoded uint length.
510 gst_ebml_write_get_uint_size (guint64 num)
515 while (num >= (G_GINT64_CONSTANT (1) << (size * 8)) && size < 8) {
524 * gst_ebml_write_set_uint:
525 * @data_inout: Pointer to data pointer
526 * @num: Number to be written.
527 * @size: Encoded number length.
529 * Write an uint into a buffer.
532 gst_ebml_write_set_uint (guint8 ** data_inout, guint64 num, guint size)
534 guint8 *data = *data_inout;
539 data[size] = num & 0xff;
546 * gst_ebml_write_uint:
547 * @ebml: #GstEbmlWrite
549 * @num: Number to be written.
551 * Write uint element.
554 gst_ebml_write_uint (GstEbmlWrite * ebml, guint32 id, guint64 num)
557 guint8 *data_start, *data_end;
558 guint size = gst_ebml_write_get_uint_size (num);
560 buf = gst_ebml_write_element_new (ebml, &data_start, sizeof (num));
561 data_end = data_start;
564 gst_ebml_write_element_id (&data_end, id);
565 gst_ebml_write_element_size (&data_end, size);
566 gst_ebml_write_set_uint (&data_end, num, size);
567 gst_ebml_write_element_push (ebml, buf, data_start, data_end);
572 * gst_ebml_write_sint:
573 * @ebml: #GstEbmlWrite
575 * @num: Number to be written.
577 * Write sint element.
580 gst_ebml_write_sint (GstEbmlWrite * ebml, guint32 id, gint64 num)
583 guint8 *data_start, *data_end;
584 /* if the signed number is on the edge of a extra-byte,
585 * then we'll fall over when detecting it. Example: if I
586 * have a number (-)0x8000 (G_MINSHORT), then my abs()<<1
587 * will be 0x10000; this is G_MAXUSHORT+1! So: if (<0) -1. */
588 guint64 unum = (num < 0 ? (-num - 1) << 1 : num << 1);
589 guint size = gst_ebml_write_get_uint_size (unum);
591 buf = gst_ebml_write_element_new (ebml, &data_start, sizeof (num));
592 data_end = data_start;
598 unum = 0x80 << (size - 1);
600 unum |= 0x80 << (size - 1);
604 gst_ebml_write_element_id (&data_end, id);
605 gst_ebml_write_element_size (&data_end, size);
606 gst_ebml_write_set_uint (&data_end, unum, size);
607 gst_ebml_write_element_push (ebml, buf, data_start, data_end);
612 * gst_ebml_write_float:
613 * @ebml: #GstEbmlWrite
615 * @num: Number to be written.
617 * Write float element.
620 gst_ebml_write_float (GstEbmlWrite * ebml, guint32 id, gdouble num)
623 guint8 *data_start, *data_end;
625 buf = gst_ebml_write_element_new (ebml, &data_start, sizeof (num));
626 data_end = data_start;
628 gst_ebml_write_element_id (&data_end, id);
629 gst_ebml_write_element_size (&data_end, 8);
630 num = GDOUBLE_TO_BE (num);
631 gst_ebml_write_element_data (&data_end, (guint8 *) & num, 8);
632 gst_ebml_write_element_push (ebml, buf, data_start, data_end);
637 * gst_ebml_write_ascii:
638 * @ebml: #GstEbmlWrite
640 * @str: String to be written.
642 * Write string element.
645 gst_ebml_write_ascii (GstEbmlWrite * ebml, guint32 id, const gchar * str)
647 gint len = strlen (str) + 1; /* add trailing '\0' */
649 guint8 *data_start, *data_end;
651 buf = gst_ebml_write_element_new (ebml, &data_start, len);
652 data_end = data_start;
654 gst_ebml_write_element_id (&data_end, id);
655 gst_ebml_write_element_size (&data_end, len);
656 gst_ebml_write_element_data (&data_end, (guint8 *) str, len);
657 gst_ebml_write_element_push (ebml, buf, data_start, data_end);
662 * gst_ebml_write_utf8:
663 * @ebml: #GstEbmlWrite
665 * @str: String to be written.
667 * Write utf8 encoded string element.
670 gst_ebml_write_utf8 (GstEbmlWrite * ebml, guint32 id, const gchar * str)
672 gst_ebml_write_ascii (ebml, id, str);
677 * gst_ebml_write_date:
678 * @ebml: #GstEbmlWrite
680 * @date: Date in seconds since the unix epoch.
682 * Write date element.
685 gst_ebml_write_date (GstEbmlWrite * ebml, guint32 id, gint64 date)
687 gst_ebml_write_sint (ebml, id, (date - GST_EBML_DATE_OFFSET) * GST_SECOND);
691 * gst_ebml_write_master_start:
692 * @ebml: #GstEbmlWrite
695 * Start wiriting mater element.
697 * Master writing is annoying. We use a size marker of
698 * the max. allowed length, so that we can later fill it
701 * Returns: Master starting position.
704 gst_ebml_write_master_start (GstEbmlWrite * ebml, guint32 id)
706 guint64 pos = ebml->pos;
708 guint8 *data_start, *data_end;
710 buf = gst_ebml_write_element_new (ebml, &data_start, 0);
711 data_end = data_start;
713 gst_ebml_write_element_id (&data_end, id);
714 pos += data_end - data_start;
715 gst_ebml_write_element_size (&data_end, GST_EBML_SIZE_UNKNOWN);
716 gst_ebml_write_element_push (ebml, buf, data_start, data_end);
723 * gst_ebml_write_master_finish_full:
724 * @ebml: #GstEbmlWrite
725 * @startpos: Master starting position.
727 * Finish writing master element. Size of master element is difference between
728 * current position and the element start, and @extra_size added to this.
731 gst_ebml_write_master_finish_full (GstEbmlWrite * ebml, guint64 startpos,
734 guint64 pos = ebml->pos;
735 guint8 *data = g_malloc (8);
736 GstBuffer *buf = gst_buffer_new_wrapped (data, 8);
738 gst_ebml_write_seek (ebml, startpos);
740 GST_WRITE_UINT64_BE (data,
741 (G_GINT64_CONSTANT (1) << 56) | (pos - startpos - 8 + extra_size));
743 gst_ebml_write_element_push (ebml, buf, NULL, NULL);
744 gst_ebml_write_seek (ebml, pos);
748 gst_ebml_write_master_finish (GstEbmlWrite * ebml, guint64 startpos)
750 gst_ebml_write_master_finish_full (ebml, startpos, 0);
754 * gst_ebml_write_binary:
755 * @ebml: #GstEbmlWrite
757 * @binary: Data to be written.
758 * @length: Length of the data
760 * Write an element with binary data.
763 gst_ebml_write_binary (GstEbmlWrite * ebml,
764 guint32 id, guint8 * binary, guint64 length)
767 guint8 *data_start, *data_end;
769 buf = gst_ebml_write_element_new (ebml, &data_start, length);
770 data_end = data_start;
772 gst_ebml_write_element_id (&data_end, id);
773 gst_ebml_write_element_size (&data_end, length);
774 gst_ebml_write_element_data (&data_end, binary, length);
775 gst_ebml_write_element_push (ebml, buf, data_start, data_end);
780 * gst_ebml_write_buffer_header:
781 * @ebml: #GstEbmlWrite
783 * @length: Length of the data
785 * Write header of the binary element (use with gst_ebml_write_buffer function).
787 * For things like video frames and audio samples,
788 * you want to use this function, as it doesn't have
789 * the overhead of memcpy() that other functions
790 * such as write_binary() do have.
793 gst_ebml_write_buffer_header (GstEbmlWrite * ebml, guint32 id, guint64 length)
796 guint8 *data_start, *data_end;
798 buf = gst_ebml_write_element_new (ebml, &data_start, 0);
799 data_end = data_start;
801 gst_ebml_write_element_id (&data_end, id);
802 gst_ebml_write_element_size (&data_end, length);
803 gst_ebml_write_element_push (ebml, buf, data_start, data_end);
808 * gst_ebml_write_buffer:
809 * @ebml: #GstEbmlWrite
810 * @buf: #GstBuffer cointaining the data.
812 * Write binary element (see gst_ebml_write_buffer_header).
815 gst_ebml_write_buffer (GstEbmlWrite * ebml, GstBuffer * buf)
817 gst_ebml_write_element_push (ebml, buf, NULL, NULL);
822 * gst_ebml_replace_uint:
823 * @ebml: #GstEbmlWrite
824 * @pos: Position of the uint that should be replaced.
827 * Replace uint with a new value.
829 * When replacing a uint, we assume that it is *always*
830 * 8-byte, since that's the safest guess we can do. This
831 * is just for simplicity.
833 * FIXME: this function needs to be replaced with something
834 * proper. This is a crude hack.
837 gst_ebml_replace_uint (GstEbmlWrite * ebml, guint64 pos, guint64 num)
839 guint64 oldpos = ebml->pos;
840 guint8 *data_start, *data_end;
843 data_start = g_malloc (8);
844 data_end = data_start;
845 buf = gst_buffer_new_wrapped (data_start, 8);
847 gst_ebml_write_seek (ebml, pos);
848 gst_ebml_write_set_uint (&data_end, num, 8);
850 gst_ebml_write_element_push (ebml, buf, data_start, data_end);
851 gst_ebml_write_seek (ebml, oldpos);
855 * gst_ebml_write_header:
856 * @ebml: #GstEbmlWrite
857 * @doctype: Document type.
858 * @version: Document type version.
863 gst_ebml_write_header (GstEbmlWrite * ebml, const gchar * doctype,
868 /* write the basic EBML header */
869 gst_ebml_write_set_cache (ebml, 0x40);
870 pos = gst_ebml_write_master_start (ebml, GST_EBML_ID_HEADER);
871 #if (GST_EBML_VERSION != 1)
872 gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLVERSION, GST_EBML_VERSION);
873 gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLREADVERSION, GST_EBML_VERSION);
876 /* we don't write these until they're "non-default" (never!) */
877 gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLMAXIDLENGTH, sizeof (guint32));
878 gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLMAXSIZELENGTH, sizeof (guint64));
880 gst_ebml_write_ascii (ebml, GST_EBML_ID_DOCTYPE, doctype);
881 gst_ebml_write_uint (ebml, GST_EBML_ID_DOCTYPEVERSION, version);
882 gst_ebml_write_uint (ebml, GST_EBML_ID_DOCTYPEREADVERSION, version);
883 gst_ebml_write_master_finish (ebml, pos);
884 gst_ebml_write_flush_cache (ebml, FALSE, 0);