summaryrefslogtreecommitdiff
path: root/libavcodec
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2013-03-08 17:28:42 +0100
committerMichael Niedermayer <michaelni@gmx.at>2013-03-08 19:12:03 +0100
commit2653e125204569b1e9439ee2671c6ebb23a94b80 (patch)
tree4176f76bccc8cdd1c85b9d329a82867eda37d397 /libavcodec
parent532f31a695c9530ce67a847be00d72e6e8acfd11 (diff)
parent1afddbe59e96af75f1c07605afc95615569f388f (diff)
Merge commit '1afddbe59e96af75f1c07605afc95615569f388f'
* commit '1afddbe59e96af75f1c07605afc95615569f388f': avpacket: use AVBuffer to allow refcounting the packets. Conflicts: libavcodec/avpacket.c libavcodec/utils.c libavdevice/v4l2.c libavformat/avidec.c libavformat/flacdec.c libavformat/id3v2.c libavformat/matroskaenc.c libavformat/mux.c libavformat/utils.c Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavcodec')
-rw-r--r--libavcodec/avcodec.h42
-rw-r--r--libavcodec/avpacket.c158
-rw-r--r--libavcodec/utils.c23
-rw-r--r--libavcodec/version.h3
4 files changed, 169 insertions, 57 deletions
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index c4db4d761f..2d338f0a45 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -29,6 +29,7 @@
#include <errno.h>
#include "libavutil/samplefmt.h"
#include "libavutil/avutil.h"
+#include "libavutil/buffer.h"
#include "libavutil/cpu.h"
#include "libavutil/channel_layout.h"
#include "libavutil/dict.h"
@@ -1009,18 +1010,24 @@ enum AVPacketSideDataType {
* ABI. Thus it may be allocated on stack and no new fields can be added to it
* without libavcodec and libavformat major bump.
*
- * The semantics of data ownership depends on the destruct field.
- * If it is set, the packet data is dynamically allocated and is valid
- * indefinitely until av_free_packet() is called (which in turn calls the
- * destruct callback to free the data). If destruct is not set, the packet data
- * is typically backed by some static buffer somewhere and is only valid for a
- * limited time (e.g. until the next read call when demuxing).
+ * The semantics of data ownership depends on the buf or destruct (deprecated)
+ * fields. If either is set, the packet data is dynamically allocated and is
+ * valid indefinitely until av_free_packet() is called (which in turn calls
+ * av_buffer_unref()/the destruct callback to free the data). If neither is set,
+ * the packet data is typically backed by some static buffer somewhere and is
+ * only valid for a limited time (e.g. until the next read call when demuxing).
*
* The side data is always allocated with av_malloc() and is freed in
* av_free_packet().
*/
typedef struct AVPacket {
/**
+ * A reference to the reference-counted buffer where the packet data is
+ * stored.
+ * May be NULL, then the packet data is not reference-counted.
+ */
+ AVBufferRef *buf;
+ /**
* Presentation timestamp in AVStream->time_base units; the time at which
* the decompressed packet will be presented to the user.
* Can be AV_NOPTS_VALUE if it is not stored in the file.
@@ -1059,8 +1066,12 @@ typedef struct AVPacket {
* Equals next_pts - this_pts in presentation order.
*/
int duration;
+#if FF_API_DESTRUCT_PACKET
+ attribute_deprecated
void (*destruct)(struct AVPacket *);
+ attribute_deprecated
void *priv;
+#endif
int64_t pos; ///< byte position in stream, -1 if unknown
/**
@@ -3776,10 +3787,14 @@ void avsubtitle_free(AVSubtitle *sub);
* @{
*/
+#if FF_API_DESTRUCT_PACKET
/**
* Default packet destructor.
+ * @deprecated use the AVBuffer API instead
*/
+attribute_deprecated
void av_destruct_packet(AVPacket *pkt);
+#endif
/**
* Initialize optional fields of a packet with default values.
@@ -3818,6 +3833,21 @@ void av_shrink_packet(AVPacket *pkt, int size);
int av_grow_packet(AVPacket *pkt, int grow_by);
/**
+ * Initialize a reference-counted packet from av_malloc()ed data.
+ *
+ * @param pkt packet to be initialized. This function will set the data, size,
+ * buf and destruct fields, all others are left untouched.
+ * @param data Data allocated by av_malloc() to be used as packet data. If this
+ * function returns successfully, the data is owned by the underlying AVBuffer.
+ * The caller may not access the data through other means.
+ * @param size size of data in bytes, without the padding. I.e. the full buffer
+ * size is assumed to be size + FF_INPUT_BUFFER_PADDING_SIZE.
+ *
+ * @return 0 on success, a negative AVERROR on error
+ */
+int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size);
+
+/**
* @warning This is a hack - the packet memory allocation stuff is broken. The
* packet is allocated if it was not really allocated.
*/
diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index dea72e5d73..16496e28c4 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -22,6 +22,7 @@
#include <string.h>
#include "libavutil/avassert.h"
+#include "libavutil/common.h"
#include "libavutil/mem.h"
#include "avcodec.h"
#include "bytestream.h"
@@ -36,6 +37,7 @@ void ff_packet_free_side_data(AVPacket *pkt)
pkt->side_data_elems = 0;
}
+#if FF_API_DESTRUCT_PACKET
void av_destruct_packet(AVPacket *pkt)
{
av_free(pkt->data);
@@ -43,6 +45,14 @@ void av_destruct_packet(AVPacket *pkt)
pkt->size = 0;
}
+/* a dummy destruct callback for the callers that assume AVPacket.destruct ==
+ * NULL => static data */
+static void dummy_destruct_packet(AVPacket *pkt)
+{
+ av_assert0(0);
+}
+#endif
+
void av_init_packet(AVPacket *pkt)
{
pkt->pts = AV_NOPTS_VALUE;
@@ -52,27 +62,35 @@ void av_init_packet(AVPacket *pkt)
pkt->convergence_duration = 0;
pkt->flags = 0;
pkt->stream_index = 0;
+#if FF_API_DESTRUCT_PACKET
pkt->destruct = NULL;
+#endif
+ pkt->buf = NULL;
pkt->side_data = NULL;
pkt->side_data_elems = 0;
}
int av_new_packet(AVPacket *pkt, int size)
{
- uint8_t *data = NULL;
- if ((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
- data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
- if (data) {
- memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
- } else
- size = 0;
+ AVBufferRef *buf = NULL;
+
+ if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
+ return AVERROR(EINVAL);
+
+ av_buffer_realloc(&buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
+ if (!buf)
+ return AVERROR(ENOMEM);
+
+ memset(buf->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
av_init_packet(pkt);
- pkt->data = data;
+ pkt->buf = buf;
+ pkt->data = buf->data;
pkt->size = size;
- pkt->destruct = av_destruct_packet;
- if (!data)
- return AVERROR(ENOMEM);
+#if FF_API_DESTRUCT_PACKET
+ pkt->destruct = dummy_destruct_packet;
+#endif
+
return 0;
}
@@ -86,33 +104,71 @@ void av_shrink_packet(AVPacket *pkt, int size)
int av_grow_packet(AVPacket *pkt, int grow_by)
{
- void *new_ptr;
+ int new_size;
av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
if (!pkt->size)
return av_new_packet(pkt, grow_by);
if ((unsigned)grow_by >
INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
return -1;
- new_ptr = av_realloc(pkt->data,
- pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE);
- if (!new_ptr)
- return AVERROR(ENOMEM);
- pkt->data = new_ptr;
+
+ new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
+ if (pkt->buf) {
+ int ret = av_buffer_realloc(&pkt->buf, new_size);
+ if (ret < 0)
+ return ret;
+ } else {
+ pkt->buf = av_buffer_alloc(new_size);
+ if (!pkt->buf)
+ return AVERROR(ENOMEM);
+ memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
+#if FF_API_DESTRUCT_PACKET
+ pkt->destruct = dummy_destruct_packet;
+#endif
+ }
+ pkt->data = pkt->buf->data;
pkt->size += grow_by;
memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+
+ return 0;
+}
+
+int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
+{
+ if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
+ return AVERROR(EINVAL);
+
+ pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
+ av_buffer_default_free, NULL, 0);
+ if (!pkt->buf)
+ return AVERROR(ENOMEM);
+
+ pkt->data = data;
+ pkt->size = size;
+#if FF_API_DESTRUCT_PACKET
+ pkt->destruct = dummy_destruct_packet;
+#endif
+
return 0;
}
-#define DUP_DATA(dst, src, size, padding) \
+#define ALLOC_MALLOC(data, size) data = av_malloc(size)
+#define ALLOC_BUF(data, size) \
+do { \
+ av_buffer_realloc(&pkt->buf, size); \
+ data = pkt->buf ? pkt->buf->data : NULL; \
+} while (0)
+
+#define DUP_DATA(dst, src, size, padding, ALLOC) \
do { \
void *data; \
if (padding) { \
if ((unsigned)(size) > \
(unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
goto failed_alloc; \
- data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); \
+ ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE); \
} else { \
- data = av_malloc(size); \
+ ALLOC(data, size); \
} \
if (!data) \
goto failed_alloc; \
@@ -124,31 +180,33 @@ int av_grow_packet(AVPacket *pkt, int grow_by)
} while (0)
/* Makes duplicates of data, side_data, but does not copy any other fields */
-static int copy_packet_data(AVPacket *dst, AVPacket *src)
+static int copy_packet_data(AVPacket *pkt, AVPacket *src)
{
- dst->data = NULL;
- dst->side_data = NULL;
- DUP_DATA(dst->data, src->data, dst->size, 1);
- dst->destruct = av_destruct_packet;
-
- if (dst->side_data_elems) {
+ pkt->data = NULL;
+ pkt->side_data = NULL;
+ DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
+#if FF_API_DESTRUCT_PACKET
+ pkt->destruct = dummy_destruct_packet;
+#endif
+
+ if (pkt->side_data_elems) {
int i;
- DUP_DATA(dst->side_data, src->side_data,
- dst->side_data_elems * sizeof(*dst->side_data), 0);
- memset(dst->side_data, 0,
- dst->side_data_elems * sizeof(*dst->side_data));
- for (i = 0; i < dst->side_data_elems; i++) {
- DUP_DATA(dst->side_data[i].data, src->side_data[i].data,
- src->side_data[i].size, 1);
- dst->side_data[i].size = src->side_data[i].size;
- dst->side_data[i].type = src->side_data[i].type;
+ DUP_DATA(pkt->side_data, src->side_data,
+ pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
+ memset(pkt->side_data, 0,
+ pkt->side_data_elems * sizeof(*pkt->side_data));
+ for (i = 0; i < pkt->side_data_elems; i++) {
+ DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
+ src->side_data[i].size, 1, ALLOC_MALLOC);
+ pkt->side_data[i].size = src->side_data[i].size;
+ pkt->side_data[i].type = src->side_data[i].type;
}
}
return 0;
failed_alloc:
- av_destruct_packet(dst);
+ av_destruct_packet(pkt);
return AVERROR(ENOMEM);
}
@@ -156,7 +214,11 @@ int av_dup_packet(AVPacket *pkt)
{
AVPacket tmp_pkt;
- if (pkt->destruct == NULL && pkt->data) {
+ if (!pkt->buf && pkt->data
+#if FF_API_DESTRUCT_PACKET
+ && !pkt->destruct
+#endif
+ ) {
tmp_pkt = *pkt;
return copy_packet_data(pkt, &tmp_pkt);
}
@@ -166,6 +228,7 @@ int av_dup_packet(AVPacket *pkt)
int av_copy_packet(AVPacket *dst, AVPacket *src)
{
*dst = *src;
+ dst->buf = av_buffer_ref(src->buf);
return copy_packet_data(dst, src);
}
@@ -174,8 +237,13 @@ void av_free_packet(AVPacket *pkt)
if (pkt) {
int i;
- if (pkt->destruct)
+ if (pkt->buf)
+ av_buffer_unref(&pkt->buf);
+#if FF_API_DESTRUCT_PACKET
+ else if (pkt->destruct)
pkt->destruct(pkt);
+ pkt->destruct = NULL;
+#endif
pkt->data = NULL;
pkt->size = 0;
@@ -230,6 +298,7 @@ uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
int av_packet_merge_side_data(AVPacket *pkt){
if(pkt->side_data_elems){
+ AVBufferRef *buf;
int i;
uint8_t *p;
uint64_t size= pkt->size + 8LL + FF_INPUT_BUFFER_PADDING_SIZE;
@@ -239,11 +308,14 @@ int av_packet_merge_side_data(AVPacket *pkt){
}
if (size > INT_MAX)
return AVERROR(EINVAL);
- p = av_malloc(size);
- if (!p)
+ buf = av_buffer_alloc(size);
+ if (!buf)
return AVERROR(ENOMEM);
- pkt->data = p;
- pkt->destruct = av_destruct_packet;
+ pkt->buf = buf;
+ pkt->data = p = buf->data;
+#if FF_API_DESTRUCT_PACKET
+ pkt->destruct = dummy_destruct_packet;
+#endif
pkt->size = size - FF_INPUT_BUFFER_PADDING_SIZE;
bytestream_put_buffer(&p, old.data, old.size);
for (i=old.side_data_elems-1; i>=0; i--) {
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 72450decfd..c077b8a385 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1162,7 +1162,10 @@ int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
}
if (avpkt->data) {
+ AVBufferRef *buf = avpkt->buf;
+#if FF_API_DESTRUCT_PACKET
void *destruct = avpkt->destruct;
+#endif
if (avpkt->size < size) {
av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
@@ -1170,7 +1173,10 @@ int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
}
av_init_packet(avpkt);
+#if FF_API_DESTRUCT_PACKET
avpkt->destruct = destruct;
+#endif
+ avpkt->buf = buf;
avpkt->size = size;
return 0;
} else {
@@ -1318,6 +1324,7 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
avpkt->size = user_pkt.size;
ret = -1;
}
+ avpkt->buf = user_pkt.buf;
avpkt->data = user_pkt.data;
avpkt->destruct = user_pkt.destruct;
} else {
@@ -1329,9 +1336,9 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
if (!ret) {
if (needs_realloc && avpkt->data) {
- uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
- if (new_data)
- avpkt->data = new_data;
+ ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
+ if (ret >= 0)
+ avpkt->data = avpkt->buf->data;
}
avctx->frame_number++;
@@ -1515,6 +1522,7 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
avpkt->size = user_pkt.size;
ret = -1;
}
+ avpkt->buf = user_pkt.buf;
avpkt->data = user_pkt.data;
avpkt->destruct = user_pkt.destruct;
} else {
@@ -1530,11 +1538,10 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
avpkt->pts = avpkt->dts = frame->pts;
- if (needs_realloc && avpkt->data &&
- avpkt->destruct == av_destruct_packet) {
- uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
- if (new_data)
- avpkt->data = new_data;
+ if (needs_realloc && avpkt->data) {
+ ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
+ if (ret >= 0)
+ avpkt->data = avpkt->buf->data;
}
avctx->frame_number++;
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 67fdc25215..3e5fa945ee 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -109,5 +109,8 @@
#ifndef FF_API_DEINTERLACE
#define FF_API_DEINTERLACE (LIBAVCODEC_VERSION_MAJOR < 56)
#endif
+#ifndef FF_API_DESTRUCT_PACKET
+#define FF_API_DESTRUCT_PACKET (LIBAVCODEC_VERSION_MAJOR < 56)
+#endif
#endif /* AVCODEC_VERSION_H */