diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2013-03-08 17:28:42 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2013-03-08 19:12:03 +0100 |
commit | 2653e125204569b1e9439ee2671c6ebb23a94b80 (patch) | |
tree | 4176f76bccc8cdd1c85b9d329a82867eda37d397 /libavcodec/avpacket.c | |
parent | 532f31a695c9530ce67a847be00d72e6e8acfd11 (diff) | |
parent | 1afddbe59e96af75f1c07605afc95615569f388f (diff) | |
download | ffmpeg-2653e125204569b1e9439ee2671c6ebb23a94b80.tar.gz |
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/avpacket.c')
-rw-r--r-- | libavcodec/avpacket.c | 158 |
1 files changed, 115 insertions, 43 deletions
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--) { |