Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
云duo
FFmpeg
提交
8e401dbe
F
FFmpeg
项目概览
云duo
/
FFmpeg
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
FFmpeg
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8e401dbe
编写于
10月 07, 2012
作者:
A
Anton Khirnov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
lavu: add a new API for reference-counted data buffers.
上级
65f1d45d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
453 addition
and
0 deletion
+453
-0
libavutil/Makefile
libavutil/Makefile
+2
-0
libavutil/buffer.c
libavutil/buffer.c
+194
-0
libavutil/buffer.h
libavutil/buffer.h
+197
-0
libavutil/buffer_internal.h
libavutil/buffer_internal.h
+60
-0
未找到文件。
libavutil/Makefile
浏览文件 @
8e401dbe
...
...
@@ -11,6 +11,7 @@ HEADERS = adler32.h \
base64.h
\
blowfish.h
\
bswap.h
\
buffer.h
\
channel_layout.h
\
common.h
\
cpu.h
\
...
...
@@ -59,6 +60,7 @@ OBJS = adler32.o \
avstring.o
\
base64.o
\
blowfish.o
\
buffer.o
\
channel_layout.o
\
cpu.o
\
crc.o
\
...
...
libavutil/buffer.c
0 → 100644
浏览文件 @
8e401dbe
/*
* This file is part of Libav.
*
* Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Libav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <string.h>
#include "atomic.h"
#include "buffer_internal.h"
#include "common.h"
#include "mem.h"
AVBufferRef
*
av_buffer_create
(
uint8_t
*
data
,
int
size
,
void
(
*
free
)(
void
*
opaque
,
uint8_t
*
data
),
void
*
opaque
,
int
flags
)
{
AVBufferRef
*
ref
=
NULL
;
AVBuffer
*
buf
=
NULL
;
buf
=
av_mallocz
(
sizeof
(
*
buf
));
if
(
!
buf
)
return
NULL
;
buf
->
data
=
data
;
buf
->
size
=
size
;
buf
->
free
=
free
?
free
:
av_buffer_default_free
;
buf
->
opaque
=
opaque
;
buf
->
refcount
=
1
;
if
(
flags
&
AV_BUFFER_FLAG_READONLY
)
buf
->
flags
|=
BUFFER_FLAG_READONLY
;
ref
=
av_mallocz
(
sizeof
(
*
ref
));
if
(
!
ref
)
{
av_freep
(
&
buf
);
return
NULL
;
}
ref
->
buffer
=
buf
;
ref
->
data
=
data
;
ref
->
size
=
size
;
return
ref
;
}
void
av_buffer_default_free
(
void
*
opaque
,
uint8_t
*
data
)
{
av_free
(
data
);
}
AVBufferRef
*
av_buffer_alloc
(
int
size
)
{
AVBufferRef
*
ret
=
NULL
;
uint8_t
*
data
=
NULL
;
data
=
av_malloc
(
size
);
if
(
!
data
)
return
NULL
;
ret
=
av_buffer_create
(
data
,
size
,
av_buffer_default_free
,
NULL
,
0
);
if
(
!
ret
)
av_freep
(
&
data
);
return
ret
;
}
AVBufferRef
*
av_buffer_allocz
(
int
size
)
{
AVBufferRef
*
ret
=
av_buffer_alloc
(
size
);
if
(
!
ret
)
return
NULL
;
memset
(
ret
->
data
,
0
,
size
);
return
ret
;
}
AVBufferRef
*
av_buffer_ref
(
AVBufferRef
*
buf
)
{
AVBufferRef
*
ret
=
av_mallocz
(
sizeof
(
*
ret
));
if
(
!
ret
)
return
NULL
;
*
ret
=
*
buf
;
avpriv_atomic_int_add_and_fetch
(
&
buf
->
buffer
->
refcount
,
1
);
return
ret
;
}
void
av_buffer_unref
(
AVBufferRef
**
buf
)
{
AVBuffer
*
b
;
if
(
!
buf
||
!*
buf
)
return
;
b
=
(
*
buf
)
->
buffer
;
av_freep
(
buf
);
if
(
!
avpriv_atomic_int_add_and_fetch
(
&
b
->
refcount
,
-
1
))
{
b
->
free
(
b
->
opaque
,
b
->
data
);
av_freep
(
&
b
);
}
}
int
av_buffer_is_writable
(
const
AVBufferRef
*
buf
)
{
if
(
buf
->
buffer
->
flags
&
AV_BUFFER_FLAG_READONLY
)
return
0
;
return
avpriv_atomic_int_add_and_fetch
(
&
buf
->
buffer
->
refcount
,
0
)
==
1
;
}
int
av_buffer_make_writable
(
AVBufferRef
**
pbuf
)
{
AVBufferRef
*
newbuf
,
*
buf
=
*
pbuf
;
if
(
av_buffer_is_writable
(
buf
))
return
0
;
newbuf
=
av_buffer_alloc
(
buf
->
size
);
if
(
!
newbuf
)
return
AVERROR
(
ENOMEM
);
memcpy
(
newbuf
->
data
,
buf
->
data
,
buf
->
size
);
av_buffer_unref
(
pbuf
);
*
pbuf
=
newbuf
;
return
0
;
}
int
av_buffer_realloc
(
AVBufferRef
**
pbuf
,
int
size
)
{
AVBufferRef
*
buf
=
*
pbuf
;
uint8_t
*
tmp
;
if
(
!
buf
)
{
/* allocate a new buffer with av_realloc(), so it will be reallocatable
* later */
uint8_t
*
data
=
av_realloc
(
NULL
,
size
);
if
(
!
data
)
return
AVERROR
(
ENOMEM
);
buf
=
av_buffer_create
(
data
,
size
,
av_buffer_default_free
,
NULL
,
0
);
if
(
!
buf
)
{
av_freep
(
&
data
);
return
AVERROR
(
ENOMEM
);
}
buf
->
buffer
->
flags
|=
BUFFER_FLAG_REALLOCATABLE
;
*
pbuf
=
buf
;
return
0
;
}
else
if
(
buf
->
size
==
size
)
return
0
;
if
(
!
(
buf
->
buffer
->
flags
&
BUFFER_FLAG_REALLOCATABLE
)
||
!
av_buffer_is_writable
(
buf
))
{
/* cannot realloc, allocate a new reallocable buffer and copy data */
AVBufferRef
*
new
=
NULL
;
av_buffer_realloc
(
&
new
,
size
);
if
(
!
new
)
return
AVERROR
(
ENOMEM
);
memcpy
(
new
->
data
,
buf
->
data
,
FFMIN
(
size
,
buf
->
size
));
av_buffer_unref
(
pbuf
);
*
pbuf
=
new
;
return
0
;
}
tmp
=
av_realloc
(
buf
->
buffer
->
data
,
size
);
if
(
!
tmp
)
return
AVERROR
(
ENOMEM
);
buf
->
buffer
->
data
=
buf
->
data
=
tmp
;
buf
->
buffer
->
size
=
buf
->
size
=
size
;
return
0
;
}
libavutil/buffer.h
0 → 100644
浏览文件 @
8e401dbe
/*
* This file is part of Libav.
*
* Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Libav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* @ingroup lavu_buffer
* refcounted data buffer API
*/
#ifndef AVUTIL_BUFFER_H
#define AVUTIL_BUFFER_H
#include <stdint.h>
/**
* @defgroup lavu_buffer AVBuffer
* @ingroup lavu_data
*
* @{
* AVBuffer is an API for reference-counted data buffers.
*
* There are two core objects in this API -- AVBuffer and AVBufferRef. AVBuffer
* represents the data buffer itself; it is opaque and not meant to be accessed
* by the caller directly, but only through AVBufferRef. However, the caller may
* e.g. compare two AVBuffer pointers to check whether two different references
* are describing the same data buffer. AVBufferRef represents a single
* reference to an AVBuffer and it is the object that may be manipulated by the
* caller directly.
*
* There are two functions provided for creating a new AVBuffer with a single
* reference -- av_buffer_alloc() to just allocate a new buffer, and
* av_buffer_create() to wrap an existing array in an AVBuffer. From an existing
* reference, additional references may be created with av_buffer_ref().
* Use av_buffer_unref() to free a reference (this will automatically free the
* data once all the references are freed).
*
* The convention throughout this API and the rest of Libav is such that the
* buffer is considered writable if there exists only one reference to it (and
* it has not been marked as read-only). The av_buffer_is_writable() function is
* provided to check whether this is true and av_buffer_make_writable() will
* automatically create a new writable buffer when necessary.
* Of course nothing prevents the calling code from violating this convention,
* however that is safe only when all the existing references are under its
* control.
*
* @note Referencing and unreferencing the buffers is thread-safe and thus
* may be done from multiple threads simultaneously without any need for
* additional locking.
*
* @note Two different references to the same buffer can point to different
* parts of the buffer (i.e. their AVBufferRef.data will not be equal).
*/
/**
* A reference counted buffer type. It is opaque and is meant to be used through
* references (AVBufferRef).
*/
typedef
struct
AVBuffer
AVBuffer
;
/**
* A reference to a data buffer.
*
* The size of this struct is not a part of the public ABI and it is not meant
* to be allocated directly.
*/
typedef
struct
AVBufferRef
{
AVBuffer
*
buffer
;
/**
* The data buffer. It is considered writable if and only if
* this is the only reference to the buffer, in which case
* av_buffer_is_writable() returns 1.
*/
uint8_t
*
data
;
/**
* Size of data in bytes.
*/
int
size
;
}
AVBufferRef
;
/**
* Allocate an AVBuffer of the given size using av_malloc().
*
* @return an AVBufferRef of given size or NULL when out of memory
*/
AVBufferRef
*
av_buffer_alloc
(
int
size
);
/**
* Same as av_buffer_alloc(), except the returned buffer will be initialized
* to zero.
*/
AVBufferRef
*
av_buffer_allocz
(
int
size
);
/**
* Always treat the buffer as read-only, even when it has only one
* reference.
*/
#define AV_BUFFER_FLAG_READONLY (1 << 0)
/**
* Create an AVBuffer from an existing array.
*
* If this function is successful, data is owned by the AVBuffer. The caller may
* only access data through the returned AVBufferRef and references derived from
* it.
* If this function fails, data is left untouched.
* @param data data array
* @param size size of data in bytes
* @param free a callback for freeing data
* @param opaque parameter to be passed to free
* @param flags a combination of AV_BUFFER_FLAG_*
*
* @return an AVBufferRef referring to data on success, NULL on failure.
*/
AVBufferRef
*
av_buffer_create
(
uint8_t
*
data
,
int
size
,
void
(
*
free
)(
void
*
opaque
,
uint8_t
*
data
),
void
*
opaque
,
int
flags
);
/**
* Default free callback, which calls av_free() on the buffer data.
* This function is meant to be passed to av_buffer_create(), not called
* directly.
*/
void
av_buffer_default_free
(
void
*
opaque
,
uint8_t
*
data
);
/**
* Create a new reference to an AVBuffer.
*
* @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on
* failure.
*/
AVBufferRef
*
av_buffer_ref
(
AVBufferRef
*
buf
);
/**
* Free a given reference and automatically free the buffer if there are no more
* references to it.
*
* @param buf the reference to be freed. The pointer is set to NULL on return.
*/
void
av_buffer_unref
(
AVBufferRef
**
buf
);
/**
* @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).
* Return 0 otherwise.
* A positive answer is valid until av_buffer_ref() is called on buf.
*/
int
av_buffer_is_writable
(
const
AVBufferRef
*
buf
);
/**
* Create a writable reference from a given buffer reference, avoiding data copy
* if possible.
*
* @param buf buffer reference to make writable. On success, buf is either left
* untouched, or it is unreferenced and a new writable AVBufferRef is
* written in its place. On failure, buf is left untouched.
* @return 0 on success, a negative AVERROR on failure.
*/
int
av_buffer_make_writable
(
AVBufferRef
**
buf
);
/**
* Reallocate a given buffer.
*
* @param buf a buffer reference to reallocate. On success, buf will be
* unreferenced and a new reference with the required size will be
* written in its place. On failure buf will be left untouched. *buf
* may be NULL, then a new buffer is allocated.
* @param size required new buffer size.
* @return 0 on success, a negative AVERROR on failure.
*
* @note the buffer is actually reallocated with av_realloc() only if it was
* initially allocated through av_buffer_realloc(NULL) and there is only one
* reference to it (i.e. the one passed to this function). In all other cases
* a new buffer is allocated and the data is copied.
*/
int
av_buffer_realloc
(
AVBufferRef
**
buf
,
int
size
);
/**
* @}
*/
#endif
/* AVUTIL_BUFFER_H */
libavutil/buffer_internal.h
0 → 100644
浏览文件 @
8e401dbe
/*
* This file is part of Libav.
*
* Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Libav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVUTIL_BUFFER_INTERNAL_H
#define AVUTIL_BUFFER_INTERNAL_H
#include <stdint.h>
#include "buffer.h"
/**
* The buffer is always treated as read-only.
*/
#define BUFFER_FLAG_READONLY (1 << 0)
/**
* The buffer was av_realloc()ed, so it is reallocatable.
*/
#define BUFFER_FLAG_REALLOCATABLE (1 << 1)
struct
AVBuffer
{
uint8_t
*
data
;
/**< data described by this buffer */
int
size
;
/**< size of data in bytes */
/**
* number of existing AVBufferRef instances referring to this buffer
*/
volatile
int
refcount
;
/**
* a callback for freeing the data
*/
void
(
*
free
)(
void
*
opaque
,
uint8_t
*
data
);
/**
* an opaque pointer, to be used by the freeing callback
*/
void
*
opaque
;
/**
* A combination of BUFFER_FLAG_*
*/
int
flags
;
};
#endif
/* AVUTIL_BUFFER_INTERNAL_H */
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录