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.
28 #include <gst/floatcast/floatcast.h>
30 #include "ebml-write.h"
34 GST_DEBUG_CATEGORY_STATIC (gst_ebml_write_debug);
35 #define GST_CAT_DEFAULT gst_ebml_write_debug
37 #define _do_init(thing) \
38 GST_DEBUG_CATEGORY_INIT (gst_ebml_write_debug, "ebmlwrite", 0, "Write EBML structured data")
39 GST_BOILERPLATE_FULL (GstEbmlWrite, gst_ebml_write, GstObject, GST_TYPE_OBJECT,
42 static void gst_ebml_write_finalize (GObject * object);
45 gst_ebml_write_base_init (gpointer g_class)
50 gst_ebml_write_class_init (GstEbmlWriteClass * klass)
52 GObjectClass *object = G_OBJECT_CLASS (klass);
54 object->finalize = gst_ebml_write_finalize;
58 gst_ebml_write_init (GstEbmlWrite * ebml, GstEbmlWriteClass * klass)
62 ebml->last_pos = G_MAXUINT64; /* force newsegment event */
65 ebml->streamheader = NULL;
66 ebml->streamheader_pos = 0;
67 ebml->writing_streamheader = FALSE;
72 gst_ebml_write_finalize (GObject * object)
74 GstEbmlWrite *ebml = GST_EBML_WRITE (object);
76 gst_object_unref (ebml->srcpad);
79 gst_byte_writer_free (ebml->cache);
83 if (ebml->streamheader) {
84 gst_byte_writer_free (ebml->streamheader);
85 ebml->streamheader = NULL;
89 gst_caps_unref (ebml->caps);
93 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
99 * @srcpad: Source pad to which the output will be pushed.
101 * Creates a new #GstEbmlWrite.
103 * Returns: a new #GstEbmlWrite
106 gst_ebml_write_new (GstPad * srcpad)
109 GST_EBML_WRITE (g_object_new (GST_TYPE_EBML_WRITE, NULL));
111 ebml->srcpad = gst_object_ref (srcpad);
112 ebml->timestamp = GST_CLOCK_TIME_NONE;
114 gst_ebml_write_reset (ebml);
121 * gst_ebml_write_reset:
122 * @ebml: a #GstEbmlWrite.
124 * Reset internal state of #GstEbmlWrite.
127 gst_ebml_write_reset (GstEbmlWrite * ebml)
130 ebml->last_pos = G_MAXUINT64; /* force newsegment event */
133 gst_byte_writer_free (ebml->cache);
138 gst_caps_unref (ebml->caps);
142 ebml->last_write_result = GST_FLOW_OK;
143 ebml->timestamp = GST_CLOCK_TIME_NONE;
148 * gst_ebml_last_write_result:
149 * @ebml: a #GstEbmlWrite.
151 * Returns: GST_FLOW_OK if there was not write error since the last call of
152 * gst_ebml_last_write_result or code of the error.
155 gst_ebml_last_write_result (GstEbmlWrite * ebml)
157 GstFlowReturn res = ebml->last_write_result;
159 ebml->last_write_result = GST_FLOW_OK;
166 gst_ebml_start_streamheader (GstEbmlWrite * ebml)
168 g_return_if_fail (ebml->streamheader == NULL);
170 GST_DEBUG ("Starting streamheader at %" G_GUINT64_FORMAT, ebml->pos);
171 ebml->streamheader = gst_byte_writer_new_with_size (1000, FALSE);
172 ebml->streamheader_pos = ebml->pos;
173 ebml->writing_streamheader = TRUE;
177 gst_ebml_stop_streamheader (GstEbmlWrite * ebml)
181 if (!ebml->streamheader)
184 buffer = gst_byte_writer_free_and_get_buffer (ebml->streamheader);
185 ebml->streamheader = NULL;
186 GST_DEBUG ("Streamheader was size %d", GST_BUFFER_SIZE (buffer));
188 ebml->writing_streamheader = FALSE;
193 * gst_ebml_write_set_cache:
194 * @ebml: a #GstEbmlWrite.
195 * @size: size of the cache.
198 * The idea is that you use this for writing a lot
199 * of small elements. This will just "queue" all of
200 * them and they'll be pushed to the next element all
201 * at once. This saves memory and time for buffer
202 * allocation and init, and it looks better.
205 gst_ebml_write_set_cache (GstEbmlWrite * ebml, guint size)
207 g_return_if_fail (ebml->cache == NULL);
209 GST_DEBUG ("Starting cache at %" G_GUINT64_FORMAT, ebml->pos);
210 ebml->cache = gst_byte_writer_new_with_size (size, FALSE);
211 ebml->cache_pos = ebml->pos;
215 gst_ebml_writer_send_new_segment_event (GstEbmlWrite * ebml, guint64 new_pos)
219 GST_INFO ("seeking to %" G_GUINT64_FORMAT, new_pos);
221 res = gst_pad_push_event (ebml->srcpad,
222 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, new_pos, -1, 0));
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_SIZE (buffer));
249 gst_buffer_set_caps (buffer, ebml->caps);
250 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
251 GST_BUFFER_OFFSET (buffer) = ebml->pos - GST_BUFFER_SIZE (buffer);
252 GST_BUFFER_OFFSET_END (buffer) = ebml->pos;
253 if (ebml->last_write_result == GST_FLOW_OK) {
254 if (GST_BUFFER_OFFSET (buffer) != ebml->last_pos) {
255 gst_ebml_writer_send_new_segment_event (ebml, GST_BUFFER_OFFSET (buffer));
256 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
258 if (ebml->writing_streamheader) {
259 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
262 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
264 ebml->last_pos = ebml->pos;
265 ebml->last_write_result = gst_pad_push (ebml->srcpad, buffer);
267 gst_buffer_unref (buffer);
273 * gst_ebml_write_element_new:
274 * @ebml: a #GstEbmlWrite.
275 * @size: size of the requested buffer.
277 * Create a buffer for one element. If there is
278 * a cache, use that instead.
280 * Returns: A new #GstBuffer.
283 gst_ebml_write_element_new (GstEbmlWrite * ebml, guint size)
285 /* Create new buffer of size + ID + length */
291 buf = gst_buffer_new_and_alloc (size);
292 GST_BUFFER_SIZE (buf) = 0;
293 GST_BUFFER_TIMESTAMP (buf) = ebml->timestamp;
300 * gst_ebml_write_element_id:
301 * @buf: Buffer to which id should be written.
302 * @id: Element ID that should be written.
304 * Write element ID into a buffer.
307 gst_ebml_write_element_id (GstBuffer * buf, guint32 id)
309 guint8 *data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
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 GST_BUFFER_SIZE (buf) += bytes;
328 data[bytes] = id & 0xff;
335 * gst_ebml_write_element_size:
336 * @buf: #GstBuffer to which size should be written.
337 * @size: Element length.
339 * Write element length into a buffer.
342 gst_ebml_write_element_size (GstBuffer * buf, guint64 size)
344 guint8 *data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
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 GST_BUFFER_SIZE (buf) += bytes;
369 while (bytes-- > 0) {
370 data[bytes] = size & 0xff;
379 * gst_ebml_write_element_data:
380 * @buf: #GstBuffer to which data should be written.
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 (GstBuffer * buf, guint8 * write, guint64 length)
389 guint8 *data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
391 memcpy (data, write, length);
392 GST_BUFFER_SIZE (buf) += length;
397 * gst_ebml_write_element_push:
398 * @ebml: #GstEbmlWrite
399 * @buf: #GstBuffer to be written.
401 * Write out buffer by moving it to the next element.
404 gst_ebml_write_element_push (GstEbmlWrite * ebml, GstBuffer * buf)
406 guint data_size = GST_BUFFER_SIZE (buf);
408 ebml->pos += data_size;
410 /* if there's no cache, then don't push it! */
411 if (ebml->writing_streamheader) {
412 gst_byte_writer_put_data (ebml->streamheader, GST_BUFFER_DATA (buf),
416 gst_byte_writer_put_data (ebml->cache, GST_BUFFER_DATA (buf), data_size);
417 gst_buffer_unref (buf);
421 if (ebml->last_write_result == GST_FLOW_OK) {
422 buf = gst_buffer_make_metadata_writable (buf);
423 gst_buffer_set_caps (buf, ebml->caps);
424 GST_BUFFER_OFFSET (buf) = ebml->pos - GST_BUFFER_SIZE (buf);
425 GST_BUFFER_OFFSET_END (buf) = ebml->pos;
426 if (ebml->writing_streamheader) {
427 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
429 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
431 if (GST_BUFFER_OFFSET (buf) != ebml->last_pos) {
432 gst_ebml_writer_send_new_segment_event (ebml, GST_BUFFER_OFFSET (buf));
433 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
435 ebml->last_pos = ebml->pos;
436 ebml->last_write_result = gst_pad_push (ebml->srcpad, buf);
438 gst_buffer_unref (buf);
444 * gst_ebml_write_seek:
445 * @ebml: #GstEbmlWrite
446 * @pos: Seek position.
451 gst_ebml_write_seek (GstEbmlWrite * ebml, guint64 pos)
453 if (ebml->writing_streamheader) {
454 GST_DEBUG ("wanting to seek to pos %" G_GUINT64_FORMAT, pos);
455 if (pos >= ebml->streamheader_pos &&
456 pos <= ebml->streamheader_pos + ebml->streamheader->parent.size) {
457 gst_byte_writer_set_pos (ebml->streamheader,
458 pos - ebml->streamheader_pos);
459 GST_DEBUG ("seeked in streamheader to position %" G_GUINT64_FORMAT,
460 pos - ebml->streamheader_pos);
463 ("we are writing streamheader still and seek is out of bounds");
466 /* Cache seeking. A bit dangerous, we assume the client writer
467 * knows what he's doing... */
470 if (pos >= ebml->cache_pos &&
471 pos <= ebml->cache_pos + ebml->cache->parent.size) {
472 GST_DEBUG ("seeking in cache to %" G_GUINT64_FORMAT, pos);
474 gst_byte_writer_set_pos (ebml->cache, ebml->pos - ebml->cache_pos);
477 GST_LOG ("Seek outside cache range. Clearing...");
478 gst_ebml_write_flush_cache (ebml, FALSE, GST_CLOCK_TIME_NONE);
482 GST_INFO ("scheduling seek to %" G_GUINT64_FORMAT, pos);
488 * gst_ebml_write_get_uint_size:
489 * @num: Number to be encoded.
491 * Get number of bytes needed to write a uint.
493 * Returns: Encoded uint length.
496 gst_ebml_write_get_uint_size (guint64 num)
501 while (num >= (G_GINT64_CONSTANT (1) << (size * 8)) && size < 8) {
510 * gst_ebml_write_set_uint:
511 * @buf: #GstBuffer to which ithe number should be written.
512 * @num: Number to be written.
513 * @size: Encoded number length.
515 * Write an uint into a buffer.
518 gst_ebml_write_set_uint (GstBuffer * buf, guint64 num, guint size)
522 data = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
523 GST_BUFFER_SIZE (buf) += size;
525 data[size] = num & 0xff;
532 * gst_ebml_write_uint:
533 * @ebml: #GstEbmlWrite
535 * @num: Number to be written.
537 * Write uint element.
540 gst_ebml_write_uint (GstEbmlWrite * ebml, guint32 id, guint64 num)
542 GstBuffer *buf = gst_ebml_write_element_new (ebml, sizeof (num));
543 guint size = gst_ebml_write_get_uint_size (num);
546 gst_ebml_write_element_id (buf, id);
547 gst_ebml_write_element_size (buf, size);
548 gst_ebml_write_set_uint (buf, num, size);
549 gst_ebml_write_element_push (ebml, buf);
554 * gst_ebml_write_sint:
555 * @ebml: #GstEbmlWrite
557 * @num: Number to be written.
559 * Write sint element.
562 gst_ebml_write_sint (GstEbmlWrite * ebml, guint32 id, gint64 num)
564 GstBuffer *buf = gst_ebml_write_element_new (ebml, sizeof (num));
566 /* if the signed number is on the edge of a extra-byte,
567 * then we'll fall over when detecting it. Example: if I
568 * have a number (-)0x8000 (G_MINSHORT), then my abs()<<1
569 * will be 0x10000; this is G_MAXUSHORT+1! So: if (<0) -1. */
570 guint64 unum = (num < 0 ? (-num - 1) << 1 : num << 1);
571 guint size = gst_ebml_write_get_uint_size (unum);
577 unum = 0x80 << (size - 1);
579 unum |= 0x80 << (size - 1);
583 gst_ebml_write_element_id (buf, id);
584 gst_ebml_write_element_size (buf, size);
585 gst_ebml_write_set_uint (buf, unum, size);
586 gst_ebml_write_element_push (ebml, buf);
591 * gst_ebml_write_float:
592 * @ebml: #GstEbmlWrite
594 * @num: Number to be written.
596 * Write float element.
599 gst_ebml_write_float (GstEbmlWrite * ebml, guint32 id, gdouble num)
601 GstBuffer *buf = gst_ebml_write_element_new (ebml, sizeof (num));
603 gst_ebml_write_element_id (buf, id);
604 gst_ebml_write_element_size (buf, 8);
605 num = GDOUBLE_TO_BE (num);
606 gst_ebml_write_element_data (buf, (guint8 *) & num, 8);
607 gst_ebml_write_element_push (ebml, buf);
612 * gst_ebml_write_ascii:
613 * @ebml: #GstEbmlWrite
615 * @str: String to be written.
617 * Write string element.
620 gst_ebml_write_ascii (GstEbmlWrite * ebml, guint32 id, const gchar * str)
622 gint len = strlen (str) + 1; /* add trailing '\0' */
623 GstBuffer *buf = gst_ebml_write_element_new (ebml, len);
625 gst_ebml_write_element_id (buf, id);
626 gst_ebml_write_element_size (buf, len);
627 gst_ebml_write_element_data (buf, (guint8 *) str, len);
628 gst_ebml_write_element_push (ebml, buf);
633 * gst_ebml_write_utf8:
634 * @ebml: #GstEbmlWrite
636 * @str: String to be written.
638 * Write utf8 encoded string element.
641 gst_ebml_write_utf8 (GstEbmlWrite * ebml, guint32 id, const gchar * str)
643 gst_ebml_write_ascii (ebml, id, str);
648 * gst_ebml_write_date:
649 * @ebml: #GstEbmlWrite
651 * @date: Date in seconds since the unix epoch.
653 * Write date element.
656 gst_ebml_write_date (GstEbmlWrite * ebml, guint32 id, gint64 date)
658 gst_ebml_write_sint (ebml, id, (date - GST_EBML_DATE_OFFSET) * GST_SECOND);
662 * gst_ebml_write_master_start:
663 * @ebml: #GstEbmlWrite
666 * Start wiriting mater element.
668 * Master writing is annoying. We use a size marker of
669 * the max. allowed length, so that we can later fill it
672 * Returns: Master starting position.
675 gst_ebml_write_master_start (GstEbmlWrite * ebml, guint32 id)
677 guint64 pos = ebml->pos, t;
678 GstBuffer *buf = gst_ebml_write_element_new (ebml, 0);
680 t = GST_BUFFER_SIZE (buf);
681 gst_ebml_write_element_id (buf, id);
682 pos += GST_BUFFER_SIZE (buf) - t;
683 gst_ebml_write_element_size (buf, GST_EBML_SIZE_UNKNOWN);
684 gst_ebml_write_element_push (ebml, buf);
691 * gst_ebml_write_master_finish_full:
692 * @ebml: #GstEbmlWrite
693 * @startpos: Master starting position.
695 * Finish writing master element. Size of master element is difference between
696 * current position and the element start, and @extra_size added to this.
699 gst_ebml_write_master_finish_full (GstEbmlWrite * ebml, guint64 startpos,
702 guint64 pos = ebml->pos;
705 gst_ebml_write_seek (ebml, startpos);
706 buf = gst_buffer_new_and_alloc (8);
707 GST_WRITE_UINT64_BE (GST_BUFFER_DATA (buf),
708 (G_GINT64_CONSTANT (1) << 56) | (pos - startpos - 8 + extra_size));
709 gst_ebml_write_element_push (ebml, buf);
710 gst_ebml_write_seek (ebml, pos);
714 gst_ebml_write_master_finish (GstEbmlWrite * ebml, guint64 startpos)
716 gst_ebml_write_master_finish_full (ebml, startpos, 0);
720 * gst_ebml_write_binary:
721 * @ebml: #GstEbmlWrite
723 * @binary: Data to be written.
724 * @length: Length of the data
726 * Write an element with binary data.
729 gst_ebml_write_binary (GstEbmlWrite * ebml,
730 guint32 id, guint8 * binary, guint64 length)
732 GstBuffer *buf = gst_ebml_write_element_new (ebml, length);
734 gst_ebml_write_element_id (buf, id);
735 gst_ebml_write_element_size (buf, length);
736 gst_ebml_write_element_data (buf, binary, length);
737 gst_ebml_write_element_push (ebml, buf);
742 * gst_ebml_write_buffer_header:
743 * @ebml: #GstEbmlWrite
745 * @length: Length of the data
747 * Write header of the binary element (use with gst_ebml_write_buffer function).
749 * For things like video frames and audio samples,
750 * you want to use this function, as it doesn't have
751 * the overhead of memcpy() that other functions
752 * such as write_binary() do have.
755 gst_ebml_write_buffer_header (GstEbmlWrite * ebml, guint32 id, guint64 length)
757 GstBuffer *buf = gst_ebml_write_element_new (ebml, 0);
759 gst_ebml_write_element_id (buf, id);
760 gst_ebml_write_element_size (buf, length);
761 gst_ebml_write_element_push (ebml, buf);
766 * gst_ebml_write_buffer:
767 * @ebml: #GstEbmlWrite
768 * @data: #GstBuffer cointaining the data.
770 * Write binary element (see gst_ebml_write_buffer_header).
773 gst_ebml_write_buffer (GstEbmlWrite * ebml, GstBuffer * data)
775 gst_ebml_write_element_push (ebml, data);
780 * gst_ebml_replace_uint:
781 * @ebml: #GstEbmlWrite
782 * @pos: Position of the uint that should be replaced.
785 * Replace uint with a new value.
787 * When replacing a uint, we assume that it is *always*
788 * 8-byte, since that's the safest guess we can do. This
789 * is just for simplicity.
791 * FIXME: this function needs to be replaced with something
792 * proper. This is a crude hack.
795 gst_ebml_replace_uint (GstEbmlWrite * ebml, guint64 pos, guint64 num)
797 guint64 oldpos = ebml->pos;
798 GstBuffer *buf = gst_buffer_new_and_alloc (8);
800 gst_ebml_write_seek (ebml, pos);
801 GST_BUFFER_SIZE (buf) = 0;
802 gst_ebml_write_set_uint (buf, num, 8);
803 gst_ebml_write_element_push (ebml, buf);
804 gst_ebml_write_seek (ebml, oldpos);
808 * gst_ebml_write_header:
809 * @ebml: #GstEbmlWrite
810 * @doctype: Document type.
811 * @version: Document type version.
816 gst_ebml_write_header (GstEbmlWrite * ebml, const gchar * doctype,
821 /* write the basic EBML header */
822 gst_ebml_write_set_cache (ebml, 0x40);
823 pos = gst_ebml_write_master_start (ebml, GST_EBML_ID_HEADER);
824 #if (GST_EBML_VERSION != 1)
825 gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLVERSION, GST_EBML_VERSION);
826 gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLREADVERSION, GST_EBML_VERSION);
829 /* we don't write these until they're "non-default" (never!) */
830 gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLMAXIDLENGTH, sizeof (guint32));
831 gst_ebml_write_uint (ebml, GST_EBML_ID_EBMLMAXSIZELENGTH, sizeof (guint64));
833 gst_ebml_write_ascii (ebml, GST_EBML_ID_DOCTYPE, doctype);
834 gst_ebml_write_uint (ebml, GST_EBML_ID_DOCTYPEVERSION, version);
835 gst_ebml_write_uint (ebml, GST_EBML_ID_DOCTYPEREADVERSION, version);
836 gst_ebml_write_master_finish (ebml, pos);
837 gst_ebml_write_flush_cache (ebml, FALSE, 0);