Commit 54fd50bf authored by Wim Taymans's avatar Wim Taymans

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.
parent a59defb9
......@@ -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 */
......
......@@ -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);
......
......@@ -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;
}
......
......@@ -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:
......
......@@ -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;
......
......@@ -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));
}
......
......@@ -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));
}
......
......@@ -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)));
......
......@@ -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));
......
......@@ -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));
......
......@@ -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));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment