From a12373b2ee14ed943315db372ddbb21dc54d58cd Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 28 Mar 2011 10:28:02 +0200 Subject: [PATCH] buffer: add more methods --- gst/gstbuffer.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ gst/gstbuffer.h | 4 ++++ gst/gstmemory.h | 6 ++++-- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/gst/gstbuffer.c b/gst/gstbuffer.c index 03aa45a1da..86ced8557a 100644 --- a/gst/gstbuffer.c +++ b/gst/gstbuffer.c @@ -577,6 +577,20 @@ gst_buffer_get_size (GstBuffer * buffer) return size; } +/** + * gst_buffer_set_size: + * @buffer: a #GstBuffer. + * @size: the new size + * + * Set the total size of the buffer + */ +void +gst_buffer_set_size (GstBuffer * buffer, gsize size) +{ + /* FIXME */ + g_warning ("gst_buffer_set_size not imlpemented"); +} + /** * gst_buffer_map: * @buffer: a #GstBuffer. @@ -681,6 +695,47 @@ gst_buffer_unmap (GstBuffer * buffer, gpointer data, gsize size) return result; } +/** + * gst_buffer_fill: + * @buffer: a #GstBuffer. + * @offset: the offset to fill + * @src: the source address + * @size: the size to fill + * + * Copy @size bytes fro @src to @buffer at @offset. + */ +void +gst_buffer_fill (GstBuffer * buffer, gsize offset, gpointer src, gsize size) +{ + GPtrArray *arr = (GPtrArray *) buffer->memory; + gsize i, len; + guint8 *ptr = src; + + len = arr->len; + + for (i = 0; i < len && size > 0; i++) { + guint8 *data; + gsize ssize, tocopy; + GstMemory *mem; + + mem = g_ptr_array_index (arr, i); + + data = gst_memory_map (mem, &ssize, NULL, GST_MAP_WRITE); + if (ssize > offset) { + /* we have enough */ + tocopy = MIN (ssize - offset, size); + memcpy (data + offset, ptr, tocopy); + size -= tocopy; + ptr += tocopy; + offset = 0; + } else { + /* offset past buffer, skip */ + offset -= ssize; + } + gst_memory_unmap (mem, data, ssize); + } +} + /** * gst_buffer_extract: * @buffer: a #GstBuffer. diff --git a/gst/gstbuffer.h b/gst/gstbuffer.h index 71d80ace56..99d4be18d0 100644 --- a/gst/gstbuffer.h +++ b/gst/gstbuffer.h @@ -300,10 +300,14 @@ void gst_buffer_take_memory (GstBuffer *buffer, GstMemory *mem); GstMemory * gst_buffer_peek_memory (GstBuffer *buffer, guint idx); void gst_buffer_remove_memory (GstBuffer *buffer, guint idx); +void gst_buffer_fill (GstBuffer *buffer, gsize offset, + gpointer src, gsize size); void gst_buffer_extract (GstBuffer *buffer, gsize offset, gpointer dest, gsize size); gsize gst_buffer_get_size (GstBuffer *buffer); +void gst_buffer_set_size (GstBuffer *buffer, gsize size); +void gst_buffer_trim (GstBuffer *buffer, gsize offset, gsize size); /* getting memory */ gpointer gst_buffer_map (GstBuffer *buffer, gsize *size, gsize *maxsize, diff --git a/gst/gstmemory.h b/gst/gstmemory.h index 8861ff9725..99b2ee91ac 100644 --- a/gst/gstmemory.h +++ b/gst/gstmemory.h @@ -60,10 +60,12 @@ struct _GstMemory { }; typedef enum { - GST_MAP_READ, - GST_MAP_WRITE, + GST_MAP_READ = (1 << 0), + GST_MAP_WRITE = (1 << 1), } GstMapFlags; +#define GST_MAP_READWRITE (GST_MAP_READ | GST_MAP_WRITE) + /** * GST_MEMORY_TRACE_NAME: *