From 54fd50bfe123609ac27fae669953173078bf417e Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 13 Jun 2011 16:31:53 +0200 Subject: [PATCH] buffer: add index to _take_memory() Add an index to gst_buffer_take_memory() so that we can also insert memory at a certain offset. This is mostly interesting to prepend a header memory block to the buffer. --- gst/gstbuffer.c | 36 ++++++++++++++++++++----------- gst/gstbuffer.h | 2 +- gst/gstbufferpool.c | 2 +- libs/gst/base/gstadapter.c | 2 +- libs/gst/base/gstbaseparse.c | 2 +- libs/gst/base/gstbytewriter.c | 2 +- plugins/elements/gstfakesrc.c | 2 +- tests/check/gst/gstbuffer.c | 2 +- tests/check/libs/bitreader.c | 2 +- tests/check/libs/bytereader.c | 2 +- tests/check/libs/typefindhelper.c | 2 +- 11 files changed, 34 insertions(+), 22 deletions(-) diff --git a/gst/gstbuffer.c b/gst/gstbuffer.c index d335c05a1d..a78a9c6546 100644 --- a/gst/gstbuffer.c +++ b/gst/gstbuffer.c @@ -197,9 +197,9 @@ _replace_memory (GstBuffer * buffer, GstMemory * mem) } static inline void -_memory_add (GstBuffer * buffer, GstMemory * mem) +_memory_add (GstBuffer * buffer, guint idx, GstMemory * mem) { - guint len = GST_BUFFER_MEM_LEN (buffer); + guint i, len = GST_BUFFER_MEM_LEN (buffer); if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) { /* too many buffer, span them. */ @@ -210,8 +210,16 @@ _memory_add (GstBuffer * buffer, GstMemory * mem) /* we now have 1 single spanned buffer */ len = 1; } - /* and append the new buffer */ - GST_BUFFER_MEM_PTR (buffer, len) = mem; + + if (idx == -1) + idx = len; + + for (i = len; i > idx; i--) { + /* move buffers to insert, FIXME, we need to insert first and then merge */ + GST_BUFFER_MEM_PTR (buffer, i) = GST_BUFFER_MEM_PTR (buffer, i - 1); + } + /* and insert the new buffer */ + GST_BUFFER_MEM_PTR (buffer, idx) = mem; GST_BUFFER_MEM_LEN (buffer) = len + 1; } @@ -319,7 +327,7 @@ gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src, } else { mem = gst_memory_ref (mem); } - _memory_add (dest, mem); + _memory_add (dest, -1, mem); left -= tocopy; } } @@ -498,7 +506,7 @@ gst_buffer_new_allocate (const GstMemoryAllocator * allocator, gsize size, newbuf = gst_buffer_new (); if (mem != NULL) - _memory_add (newbuf, mem); + _memory_add (newbuf, -1, mem); GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d from allocator %p", newbuf, size, allocator); @@ -516,7 +524,7 @@ gst_buffer_new_allocate (const GstMemoryAllocator * allocator, gsize size, if (size > 0) { mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL, size, 0, size); - _memory_add (newbuf, mem); + _memory_add (newbuf, -1, mem); } #endif @@ -543,7 +551,7 @@ gst_buffer_new_allocate (const GstMemoryAllocator * allocator, gsize size, GST_BUFFER_BUFMEM (newbuf) = mem; if (size > 0) - _memory_add (newbuf, gst_memory_ref (mem)); + _memory_add (newbuf, -1, gst_memory_ref (mem)); #endif return newbuf; @@ -575,19 +583,21 @@ gst_buffer_n_memory (GstBuffer * buffer) /** * gst_buffer_take_memory: * @buffer: a #GstBuffer. + * @idx: the index to add the memory * @mem: a #GstMemory. * - * Add the memory block @mem to @buffer. This function takes ownership of @mem + * Add the memory block @mem to @buffer at @idx. This function takes ownership of @mem * and thus doesn't increase its refcount. */ void -gst_buffer_take_memory (GstBuffer * buffer, GstMemory * mem) +gst_buffer_take_memory (GstBuffer * buffer, guint idx, GstMemory * mem) { g_return_if_fail (GST_IS_BUFFER (buffer)); g_return_if_fail (gst_buffer_is_writable (buffer)); g_return_if_fail (mem != NULL); + g_return_if_fail (idx == -1 || idx < GST_BUFFER_MEM_LEN (buffer)); - _memory_add (buffer, mem); + _memory_add (buffer, idx, mem); } static GstMemory * @@ -1004,6 +1014,8 @@ gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size) * @size: the size to compare * * Compare @size bytes starting from @offset in @buffer with the memory in @mem. + * + * Returns: 0 if the memory is equal. */ gint gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem, @@ -1255,7 +1267,7 @@ gst_buffer_span (GstBuffer * buf1, gsize offset, GstBuffer * buf2, gsize size) span = _gst_buffer_arr_span (mem, len, 2, offset, size, FALSE); newbuf = gst_buffer_new (); - _memory_add (newbuf, span); + _memory_add (newbuf, -1, span); #if 0 /* if the offset is 0, the new buffer has the same timestamp as buf1 */ diff --git a/gst/gstbuffer.h b/gst/gstbuffer.h index 0b3b170043..48774e6f27 100644 --- a/gst/gstbuffer.h +++ b/gst/gstbuffer.h @@ -230,7 +230,7 @@ GstBuffer * gst_buffer_new_allocate (const GstMemoryAllocator * allocator /* memory blocks */ guint gst_buffer_n_memory (GstBuffer *buffer); -void gst_buffer_take_memory (GstBuffer *buffer, GstMemory *mem); +void gst_buffer_take_memory (GstBuffer *buffer, guint idx, GstMemory *mem); GstMemory * gst_buffer_peek_memory (GstBuffer *buffer, guint idx, GstMapFlags flags); void gst_buffer_remove_memory_range (GstBuffer *buffer, guint idx, guint length); diff --git a/gst/gstbufferpool.c b/gst/gstbufferpool.c index c5c939b23c..90f697e3dc 100644 --- a/gst/gstbufferpool.c +++ b/gst/gstbufferpool.c @@ -167,7 +167,7 @@ default_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer, mem = gst_memory_allocator_alloc (NULL, priv->size + priv->prefix, priv->align); gst_memory_resize (mem, priv->prefix, priv->size); - gst_buffer_take_memory (*buffer, mem); + gst_buffer_take_memory (*buffer, -1, mem); return GST_FLOW_OK; } diff --git a/libs/gst/base/gstadapter.c b/libs/gst/base/gstadapter.c index 663f8fca88..94fbabf9cd 100644 --- a/libs/gst/base/gstadapter.c +++ b/libs/gst/base/gstadapter.c @@ -732,7 +732,7 @@ gst_adapter_take_buffer (GstAdapter * adapter, gsize nbytes) data = gst_adapter_take_internal (adapter, nbytes); buffer = gst_buffer_new (); - gst_buffer_take_memory (buffer, + gst_buffer_take_memory (buffer, -1, gst_memory_new_wrapped (0, data, g_free, nbytes, 0, nbytes)); done: diff --git a/libs/gst/base/gstbaseparse.c b/libs/gst/base/gstbaseparse.c index 0e3f39b16b..eef74c1293 100644 --- a/libs/gst/base/gstbaseparse.c +++ b/libs/gst/base/gstbaseparse.c @@ -2261,7 +2261,7 @@ gst_base_parse_chain (GstPad * pad, GstBuffer * buffer) /* always pass all available data */ data = gst_adapter_map (parse->priv->adapter, av); - gst_buffer_take_memory (tmpbuf, + gst_buffer_take_memory (tmpbuf, -1, gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, (gpointer) data, NULL, av, 0, av)); GST_BUFFER_OFFSET (tmpbuf) = parse->priv->offset; diff --git a/libs/gst/base/gstbytewriter.c b/libs/gst/base/gstbytewriter.c index fa54688210..969b705143 100644 --- a/libs/gst/base/gstbytewriter.c +++ b/libs/gst/base/gstbytewriter.c @@ -260,7 +260,7 @@ gst_byte_writer_reset_and_get_buffer (GstByteWriter * writer) buffer = gst_buffer_new (); if (data != NULL) { - gst_buffer_take_memory (buffer, + gst_buffer_take_memory (buffer, -1, gst_memory_new_wrapped (0, data, g_free, size, 0, size)); } diff --git a/plugins/elements/gstfakesrc.c b/plugins/elements/gstfakesrc.c index afe05fe4fb..df46d18cf2 100644 --- a/plugins/elements/gstfakesrc.c +++ b/plugins/elements/gstfakesrc.c @@ -686,7 +686,7 @@ gst_fake_src_alloc_buffer (GstFakeSrc * src, guint size) if (do_prepare) gst_fake_src_prepare_buffer (src, data, size); - gst_buffer_take_memory (buf, + gst_buffer_take_memory (buf, -1, gst_memory_new_wrapped (0, data, g_free, size, 0, size)); } diff --git a/tests/check/gst/gstbuffer.c b/tests/check/gst/gstbuffer.c index 96fa7c7a12..b37d943a58 100644 --- a/tests/check/gst/gstbuffer.c +++ b/tests/check/gst/gstbuffer.c @@ -246,7 +246,7 @@ create_read_only_buffer (void) buf = gst_buffer_new (); /* assign some read-only data to the new buffer */ - gst_buffer_take_memory (buf, + gst_buffer_take_memory (buf, -1, gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, (gpointer) ro_memory, NULL, sizeof (ro_memory), 0, sizeof (ro_memory))); diff --git a/tests/check/libs/bitreader.c b/tests/check/libs/bitreader.c index bdedbebe16..346b00e898 100644 --- a/tests/check/libs/bitreader.c +++ b/tests/check/libs/bitreader.c @@ -49,7 +49,7 @@ GST_START_TEST (test_initialization) guint8 *bdata; gsize bsize; - gst_buffer_take_memory (buffer, + gst_buffer_take_memory (buffer, -1, gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, data, NULL, 4, 0, 4)); fail_unless (gst_bit_reader_get_bits_uint8 (&reader, &x, 8)); diff --git a/tests/check/libs/bytereader.c b/tests/check/libs/bytereader.c index 19af966d36..dfb7ef4e73 100644 --- a/tests/check/libs/bytereader.c +++ b/tests/check/libs/bytereader.c @@ -49,7 +49,7 @@ GST_START_TEST (test_initialization) guint8 *bdata; gsize bsize; - gst_buffer_take_memory (buffer, + gst_buffer_take_memory (buffer, -1, gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, data, NULL, 4, 0, 4)); fail_unless (gst_byte_reader_get_uint8 (&reader, &x)); diff --git a/tests/check/libs/typefindhelper.c b/tests/check/libs/typefindhelper.c index 349911ad35..8a55f84755 100644 --- a/tests/check/libs/typefindhelper.c +++ b/tests/check/libs/typefindhelper.c @@ -55,7 +55,7 @@ GST_START_TEST (test_buffer_range) buf = gst_buffer_new (); fail_unless (buf != NULL); - gst_buffer_take_memory (buf, + gst_buffer_take_memory (buf, -1, gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, (gpointer) vorbisid, NULL, 30, 0, 30));