summaryrefslogtreecommitdiff
path: root/libavformat
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2021-08-04 13:58:31 +0200
committerAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2021-08-25 23:01:54 +0200
commitf5f984c9c37fb55eb4e21dfdd95a3bcf01258373 (patch)
tree68294f3bc6e8329897b2762d6c93141d7d788072 /libavformat
parent45bfe8b838275235412777dd430206d9a24eb3ee (diff)
downloadffmpeg-f5f984c9c37fb55eb4e21dfdd95a3bcf01258373.tar.gz
avformat/aviobuf: Avoid allocation when using dynamic buffer
This can be achieved by allocating the AVIOContext and the dynamic buffer's opaque and internal write buffer together. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Diffstat (limited to 'libavformat')
-rw-r--r--libavformat/aviobuf.c22
1 files changed, 9 insertions, 13 deletions
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index a33efacfc4..9b13bf238e 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -1377,22 +1377,21 @@ static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
{
+ struct { FFIOContext pb; DynBuffer d; } *ret;
DynBuffer *d;
unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
- if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
+ if (sizeof(*ret) + io_buffer_size < io_buffer_size)
return AVERROR(ERANGE);
- d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
- if (!d)
+ ret = av_mallocz(sizeof(*ret) + io_buffer_size);
+ if (!ret)
return AVERROR(ENOMEM);
+ d = &ret->d;
d->io_buffer_size = io_buffer_size;
- *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
- max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
- max_packet_size ? NULL : dyn_buf_seek);
- if(!*s) {
- av_free(d);
- return AVERROR(ENOMEM);
- }
+ ffio_init_context(&ret->pb, d->io_buffer, d->io_buffer_size, 1, d, NULL,
+ max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
+ max_packet_size ? NULL : dyn_buf_seek);
+ *s = &ret->pb.pub;
(*s)->max_packet_size = max_packet_size;
return 0;
}
@@ -1465,7 +1464,6 @@ int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
d = s->opaque;
*pbuffer = d->buffer;
size = d->size;
- av_free(d);
avio_context_free(&s);
@@ -1481,7 +1479,6 @@ void ffio_free_dyn_buf(AVIOContext **s)
d = (*s)->opaque;
av_free(d->buffer);
- av_free(d);
avio_context_free(s);
}
@@ -1513,7 +1510,6 @@ int ffio_close_null_buf(AVIOContext *s)
avio_flush(s);
size = d->size;
- av_free(d);
avio_context_free(&s);