memory: add method to create mapped memory

Add a method to create a GstMemory with the desired mapping. Makes a copy of the
memory if it is currently in use.
This commit is contained in:
Wim Taymans 2012-01-20 14:56:49 +01:00
parent a521252845
commit 93074b899c
4 changed files with 53 additions and 14 deletions

View file

@ -963,23 +963,13 @@ gst_buffer_map (GstBuffer * buffer, GstMapInfo * info, GstMapFlags flags)
goto not_writable;
mem = gst_buffer_get_merged_memory (buffer);
if (mem == NULL)
if (G_UNLIKELY (mem == NULL))
goto no_memory;
/* now try to map */
if (!gst_memory_map (mem, info, flags) && write) {
GstMemory *copy;
/* make a (writable) copy */
copy = gst_memory_copy (mem, 0, -1);
gst_memory_unref (mem);
mem = copy;
if (G_UNLIKELY (mem == NULL))
goto cannot_map;
/* try again */
if (!gst_memory_map (mem, info, flags))
goto cannot_map;
}
mem = gst_memory_make_mapped (mem, info, flags);
if (G_UNLIKELY (mem == NULL))
goto cannot_map;
/* if the buffer is writable, replace the memory */
if (writable)

View file

@ -499,6 +499,53 @@ gst_memory_unlock (GstMemory * mem)
} while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
}
/**
* gst_memory_make_mapped:
* @mem: (transfer full): a #GstMemory
* @info: (out): pointer for info
* @flags: mapping flags
*
* Create a #GstMemory object that is mapped with @flags. If @mem is mappable
* with @flags, this function returns the mapped @mem directly. Otherwise a
* mapped copy of @mem is returned.
*
* This function takes ownership of old @mem and returns a reference to a new
* #GstMemory.
*
* Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
* a mapping is not possible.
*/
GstMemory *
gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
{
GstMemory *result;
if (gst_memory_map (mem, info, flags)) {
result = mem;
} else {
result = gst_memory_copy (mem, 0, -1);
if (result == NULL)
goto cannot_copy;
if (!gst_memory_map (result, info, flags))
goto cannot_map;
}
return result;
/* ERRORS */
cannot_copy:
{
GST_DEBUG ("cannot copy memory %p", mem);
return NULL;
}
cannot_map:
{
GST_DEBUG ("cannot map memory %p with flags %d", mem, flags);
return NULL;
}
}
/**
* gst_memory_map:
* @mem: a #GstMemory

View file

@ -277,6 +277,7 @@ void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
/* retrieving data */
gboolean gst_memory_is_writable (GstMemory *mem);
GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
gboolean gst_memory_map (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
void gst_memory_unmap (GstMemory *mem, GstMapInfo *info);

View file

@ -510,6 +510,7 @@ EXPORTS
gst_memory_get_type
gst_memory_is_span
gst_memory_is_writable
gst_memory_make_mapped
gst_memory_map
gst_memory_new_wrapped
gst_memory_ref