Commit 31444400 authored by Lukasz Marek's avatar Lukasz Marek Committed by Michael Niedermayer

lavu/buffer: add release function

new function allows to unref buffer and obtain its data.
Signed-off-by: 's avatarLukasz Marek <lukasz.m.luki@gmail.com>
Signed-off-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent 774239be
......@@ -117,6 +117,35 @@ void av_buffer_unref(AVBufferRef **buf)
}
}
int av_buffer_release(AVBufferRef **buf, uint8_t **data)
{
AVBuffer *b;
int ret = 0;
if (data)
*data = NULL;
if (!buf || !*buf)
return 0;
b = (*buf)->buffer;
av_freep(buf);
if (data && avpriv_atomic_int_get(&b->refcount) > 1) {
*data = av_memdup(b->data, b->size);
if (!*data)
ret = AVERROR(ENOMEM);
}
if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
if (data && !*data) {
ret = 0;
*data = b->data;
} else
b->free(b->opaque, b->data);
av_freep(&b);
}
return ret;
}
int av_buffer_is_writable(const AVBufferRef *buf)
{
if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
......
......@@ -154,6 +154,18 @@ AVBufferRef *av_buffer_ref(AVBufferRef *buf);
*/
void av_buffer_unref(AVBufferRef **buf);
/**
* Free a given reference and pass underlaying data to user provided pointer.
* If there is more than one reference then data is copied.
*
* @param buf the reference to be released. The pointer is set to NULL on return.
* @param data pointer to be passed with underlaying data.
* @return 0 on success, a negative AVERROR on failure.
*
* @note on error buffer is properly released and *data is set to NULL.
*/
int av_buffer_release(AVBufferRef **buf, uint8_t **data);
/**
* @return 1 if the caller may write to the data referred to by buf (which is
* true if and only if buf is the only reference to the underlying AVBuffer).
......
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