buffer: add api to get the current memory offset

Also return the offset in a GstMemory block with the get_sizes() method. This
allows us to figure out how much prefix there is unused.
Change the resize function so that a negative offset can be given. This would
make it possible to resize the buffer so that the prefix becomes available.
Add gst_buffer_get_sizes() to return the offset and maxsize as well as the size.
Also change the buffer resize method so that we can specify a negative offset
to remove prefix bytes.
This commit is contained in:
Wim Taymans 2011-07-11 16:17:57 +02:00
parent 21e0d113cf
commit abf6293639
5 changed files with 76 additions and 35 deletions

View file

@ -307,7 +307,7 @@ gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
/* copy and make regions of the memory */
for (i = 0; i < len && left > 0; i++) {
mem = GST_BUFFER_MEM_PTR (src, i);
bsize = gst_memory_get_sizes (mem, NULL);
bsize = gst_memory_get_sizes (mem, NULL, NULL);
if (bsize <= skip) {
/* don't copy buffer */
@ -743,43 +743,71 @@ gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, guint length)
}
/**
* gst_buffer_get_size:
* gst_buffer_get_sizes:
* @buffer: a #GstBuffer.
* @offset: a pointer to the offset
* @maxsize: a pointer to the maxsize
*
* Get the total size of all memory blocks in @buffer.
*
* When not %NULL, @offset will contain the offset of the data in the first
* memory block in @buffer and @maxsize will contain the sum of the size
* and @offset and the amount of extra padding on the last memory block.
* @offset and @maxsize can be used to resize the buffer with
* gst_buffer_resize().
*
* Returns: the total size of the memory in @buffer.
*/
gsize
gst_buffer_get_size (GstBuffer * buffer)
gst_buffer_get_sizes (GstBuffer * buffer, gsize * offset, gsize * maxsize)
{
guint i, size, len;
guint i, len;
gsize extra, size, offs;
g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
len = GST_BUFFER_MEM_LEN (buffer);
size = 0;
size = offs = extra = 0;
for (i = 0; i < len; i++) {
size += gst_memory_get_sizes (GST_BUFFER_MEM_PTR (buffer, i), NULL);
gsize s, o, ms;
s = gst_memory_get_sizes (GST_BUFFER_MEM_PTR (buffer, i), &o, &ms);
/* add sizes */
size += s;
/* keep offset of first memory block */
if (i == 0)
offs = o;
/* this is the amount of extra bytes in this block, we only keep this for
* the last block */
else if (i + 1 == len)
extra = ms - (o + s);
}
if (offset)
*offset = offs;
if (maxsize)
*maxsize = offs + size + extra;
return size;
}
/**
* gst_buffer_resize:
* @buffer: a #GstBuffer.
* @offset: the new offset
* @offset: the offset adjustement
* @size: the new size
*
* Set the total size of the buffer
*/
void
gst_buffer_resize (GstBuffer * buffer, gsize offset, gsize size)
gst_buffer_resize (GstBuffer * buffer, gssize offset, gsize size)
{
guint len;
guint si, di;
gsize bsize, bufsize;
gsize bsize, bufsize, bufoffs, bufmax;
GstMemory *mem;
GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSIZE_FORMAT "-%" G_GSIZE_FORMAT,
@ -787,8 +815,10 @@ gst_buffer_resize (GstBuffer * buffer, gsize offset, gsize size)
g_return_if_fail (gst_buffer_is_writable (buffer));
bufsize = gst_buffer_get_size (buffer);
/* FIXME, handle negative offsets */
bufsize = gst_buffer_get_sizes (buffer, &bufoffs, &bufmax);
g_return_if_fail (bufsize >= offset);
if (size == -1)
size = bufsize - offset;
g_return_if_fail (bufsize >= offset + size);
@ -798,27 +828,27 @@ gst_buffer_resize (GstBuffer * buffer, gsize offset, gsize size)
/* copy and trim */
for (di = si = 0; si < len && size > 0; si++) {
mem = GST_BUFFER_MEM_PTR (buffer, si);
bsize = gst_memory_get_sizes (mem, NULL);
bsize = gst_memory_get_sizes (mem, NULL, NULL);
if (bsize <= offset) {
/* remove buffer */
gst_memory_unref (mem);
offset -= bsize;
} else {
gsize tocopy;
gsize left;
tocopy = MIN (bsize - offset, size);
if (tocopy < bsize) {
left = MIN (bsize - offset, size);
if (left < bsize) {
/* we need to clip something */
if (GST_MEMORY_IS_WRITABLE (mem)) {
gst_memory_resize (mem, offset, tocopy);
gst_memory_resize (mem, offset, left);
} else {
GstMemory *tmp;
if (mem->flags & GST_MEMORY_FLAG_NO_SHARE)
tmp = gst_memory_copy (mem, offset, tocopy);
tmp = gst_memory_copy (mem, offset, left);
else
tmp = gst_memory_share (mem, offset, tocopy);
tmp = gst_memory_share (mem, offset, left);
gst_memory_unref (mem);
mem = tmp;
@ -826,7 +856,7 @@ gst_buffer_resize (GstBuffer * buffer, gsize offset, gsize size)
offset = 0;
}
GST_BUFFER_MEM_PTR (buffer, di++) = mem;
size -= tocopy;
size -= left;
}
}
GST_BUFFER_MEM_LEN (buffer) = di;

View file

@ -254,9 +254,16 @@ gint gst_buffer_memcmp (GstBuffer *buffer, gsize offset,
void gst_buffer_memset (GstBuffer *buffer, gsize offset,
guint8 val, gsize size);
gsize gst_buffer_get_size (GstBuffer *buffer);
void gst_buffer_resize (GstBuffer *buffer, gsize offset, gsize size);
gsize gst_buffer_get_sizes (GstBuffer *buffer, gsize *offset, gsize *maxsize);
void gst_buffer_resize (GstBuffer *buffer, gssize offset, gsize size);
/**
* gst_buffer_get_size:
* @b: a #GstBuffer.
*
* Get the size of @b.
*/
#define gst_buffer_get_size(b) gst_buffer_get_sizes ((b), NULL, NULL)
/**
* gst_buffer_set_size:
* @b: a #GstBuffer.

View file

@ -176,8 +176,10 @@ _default_mem_alloc (const GstAllocator * allocator, gsize maxsize, gsize align)
}
static gsize
_default_mem_get_sizes (GstMemoryDefault * mem, gsize * maxsize)
_default_mem_get_sizes (GstMemoryDefault * mem, gsize * offset, gsize * maxsize)
{
if (offset)
*offset = mem->offset;
if (maxsize)
*maxsize = mem->maxsize;
@ -185,7 +187,7 @@ _default_mem_get_sizes (GstMemoryDefault * mem, gsize * maxsize)
}
static void
_default_mem_resize (GstMemoryDefault * mem, gsize offset, gsize size)
_default_mem_resize (GstMemoryDefault * mem, gssize offset, gsize size)
{
g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
@ -399,18 +401,19 @@ gst_memory_unref (GstMemory * mem)
/**
* gst_memory_get_sizes:
* @mem: a #GstMemory
* @offset: pointer to offset
* @maxsize: pointer to maxsize
*
* Get the current @size and @maxsize of @mem.
* Get the current @size, @offset and @maxsize of @mem.
*
* Returns: the current sizes of @mem
*/
gsize
gst_memory_get_sizes (GstMemory * mem, gsize * maxsize)
gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
{
g_return_val_if_fail (mem != NULL, 0);
return mem->allocator->info.get_sizes (mem, maxsize);
return mem->allocator->info.get_sizes (mem, offset, maxsize);
}
/**
@ -423,7 +426,7 @@ gst_memory_get_sizes (GstMemory * mem, gsize * maxsize)
* less than the maxsize of @mem.
*/
void
gst_memory_resize (GstMemory * mem, gsize offset, gsize size)
gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
{
g_return_if_fail (mem != NULL);
g_return_if_fail (GST_MEMORY_IS_WRITABLE (mem));

View file

@ -127,27 +127,28 @@ typedef GstMemory * (*GstMemoryAllocFunction) (const GstAllocator *allocator,
/**
* GstMemoryGetSizesFunction:
* @mem: a #GstMemory
* @offset: result pointer for offset
* @maxsize: result pointer for maxsize
*
* Retrieve the size and maxsize of @mem.
* Retrieve the size, offset and maxsize of @mem.
*
* Returns: the size of @mem and the maximum allocated size in @maxsize.
* Returns: the size of @mem, the offset and the maximum allocated size in @maxsize.
*/
typedef gsize (*GstMemoryGetSizesFunction) (GstMemory *mem, gsize *maxsize);
typedef gsize (*GstMemoryGetSizesFunction) (GstMemory *mem, gsize *offset, gsize *maxsize);
/**
* GstMemoryResizeFunction:
* @mem: a #GstMemory
* @offset: the new offset
* @offset: the offset adjustement
* @size: the new size
*
* Adjust the size and offset of @mem. @offset bytes will be skipped from the
* Adjust the size and offset of @mem. @offset bytes will be adjusted from the
* current first byte in @mem as retrieved with gst_memory_map() and the new
* size will be set to @size.
*
* @size can be set to -1, which will only adjust the offset.
*/
typedef void (*GstMemoryResizeFunction) (GstMemory *mem, gsize offset, gsize size);
typedef void (*GstMemoryResizeFunction) (GstMemory *mem, gssize offset, gsize size);
/**
* GstMemoryMapFunction:
@ -283,8 +284,8 @@ GstMemory * gst_memory_ref (GstMemory *mem);
void gst_memory_unref (GstMemory *mem);
/* getting/setting memory properties */
gsize gst_memory_get_sizes (GstMemory *mem, gsize *maxsize);
void gst_memory_resize (GstMemory *mem, gsize offset, gsize size);
gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
/* retrieving data */
gpointer gst_memory_map (GstMemory *mem, gsize *size, gsize *maxsize,

View file

@ -102,7 +102,7 @@ EXPORTS
gst_buffer_fill
gst_buffer_flag_get_type
gst_buffer_get_meta
gst_buffer_get_size
gst_buffer_get_sizes
gst_buffer_is_span_fast
gst_buffer_iterate_meta
gst_buffer_join