diff options
author | Andreas Rheinhardt <andreas.rheinhardt@outlook.com> | 2021-08-04 16:52:07 +0200 |
---|---|---|
committer | Andreas Rheinhardt <andreas.rheinhardt@outlook.com> | 2021-08-25 23:01:54 +0200 |
commit | 45bfe8b838275235412777dd430206d9a24eb3ee (patch) | |
tree | d35a1ee7436b0259fd26d32bc80482c0a9f2927e /libavformat/aviobuf.c | |
parent | 530ac6aa305aeda631c77f8a17e96c14c7ab1a1c (diff) | |
download | ffmpeg-45bfe8b838275235412777dd430206d9a24eb3ee.tar.gz |
avformat/avio: Move internal AVIOContext fields to avio_internal.h
Currently AVIOContext's private fields are all over AVIOContext.
This commit moves them into a new structure in avio_internal.h instead.
Said structure contains the public AVIOContext as its first element
in order to avoid having to allocate a separate AVIOContextInternal
which is costly for those use cases where one just wants to access
an already existing buffer via the AVIOContext-API.
For these cases ffio_init_context() can't fail and always returned zero,
which was typically not checked. Therefore it has been made to not
return anything.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Diffstat (limited to 'libavformat/aviobuf.c')
-rw-r--r-- | libavformat/aviobuf.c | 105 |
1 files changed, 57 insertions, 48 deletions
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 1f0819b328..a33efacfc4 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -77,7 +77,7 @@ static int url_resetbuf(AVIOContext *s, int flags); /** @warning must be called before any I/O */ static int set_buf_size(AVIOContext *s, int buf_size); -int ffio_init_context(AVIOContext *s, +void ffio_init_context(FFIOContext *ctx, unsigned char *buffer, int buffer_size, int write_flag, @@ -86,10 +86,12 @@ int ffio_init_context(AVIOContext *s, int (*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t (*seek)(void *opaque, int64_t offset, int whence)) { - memset(s, 0, sizeof(AVIOContext)); + AVIOContext *const s = &ctx->pub; + + memset(ctx, 0, sizeof(*ctx)); s->buffer = buffer; - s->orig_buffer_size = + ctx->orig_buffer_size = s->buffer_size = buffer_size; s->buf_ptr = buffer; s->buf_ptr_max = buffer; @@ -108,7 +110,7 @@ int ffio_init_context(AVIOContext *s, s->min_packet_size = 0; s->max_packet_size = 0; s->update_checksum = NULL; - s->short_seek_threshold = SHORT_SEEK_THRESHOLD; + ctx->short_seek_threshold = SHORT_SEEK_THRESHOLD; if (!read_packet && !write_flag) { s->pos = buffer_size; @@ -119,12 +121,10 @@ int ffio_init_context(AVIOContext *s, s->write_data_type = NULL; s->ignore_boundary_point = 0; - s->current_type = AVIO_DATA_MARKER_UNKNOWN; - s->last_time = AV_NOPTS_VALUE; - s->short_seek_get = NULL; + ctx->current_type = AVIO_DATA_MARKER_UNKNOWN; + ctx->last_time = AV_NOPTS_VALUE; + ctx->short_seek_get = NULL; s->written = 0; - - return 0; } AVIOContext *avio_alloc_context( @@ -136,12 +136,12 @@ AVIOContext *avio_alloc_context( int (*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t (*seek)(void *opaque, int64_t offset, int whence)) { - AVIOContext *s = av_malloc(sizeof(AVIOContext)); + FFIOContext *s = av_malloc(sizeof(*s)); if (!s) return NULL; ffio_init_context(s, buffer, buffer_size, write_flag, opaque, read_packet, write_packet, seek); - return s; + return &s->pub; } void avio_context_free(AVIOContext **ps) @@ -151,13 +151,14 @@ void avio_context_free(AVIOContext **ps) static void writeout(AVIOContext *s, const uint8_t *data, int len) { + FFIOContext *const ctx = ffiocontext(s); if (!s->error) { int ret = 0; if (s->write_data_type) ret = s->write_data_type(s->opaque, (uint8_t *)data, len, - s->current_type, - s->last_time); + ctx->current_type, + ctx->last_time); else if (s->write_packet) ret = s->write_packet(s->opaque, (uint8_t *)data, len); if (ret < 0) { @@ -167,12 +168,12 @@ static void writeout(AVIOContext *s, const uint8_t *data, int len) s->written = s->pos + len; } } - if (s->current_type == AVIO_DATA_MARKER_SYNC_POINT || - s->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) { - s->current_type = AVIO_DATA_MARKER_UNKNOWN; + if (ctx->current_type == AVIO_DATA_MARKER_SYNC_POINT || + ctx->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) { + ctx->current_type = AVIO_DATA_MARKER_UNKNOWN; } - s->last_time = AV_NOPTS_VALUE; - s->writeout_count ++; + ctx->last_time = AV_NOPTS_VALUE; + ctx->writeout_count++; s->pos += len; } @@ -244,6 +245,7 @@ void avio_flush(AVIOContext *s) int64_t avio_seek(AVIOContext *s, int64_t offset, int whence) { + FFIOContext *const ctx = ffiocontext(s); int64_t offset1; int64_t pos; int force = whence & AVSEEK_FORCE; @@ -275,9 +277,9 @@ int64_t avio_seek(AVIOContext *s, int64_t offset, int whence) if (offset < 0) return AVERROR(EINVAL); - short_seek = s->short_seek_threshold; - if (s->short_seek_get) { - int tmp = s->short_seek_get(s->opaque); + short_seek = ctx->short_seek_threshold; + if (ctx->short_seek_get) { + int tmp = ctx->short_seek_get(s->opaque); short_seek = FFMAX(tmp, short_seek); } @@ -318,7 +320,7 @@ int64_t avio_seek(AVIOContext *s, int64_t offset, int whence) return AVERROR(EPIPE); if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0) return res; - s->seek_count ++; + ctx->seek_count++; if (!s->write_flag) s->buf_end = s->buffer; s->buf_ptr = s->buf_ptr_max = s->buffer; @@ -472,6 +474,7 @@ void avio_wb24(AVIOContext *s, unsigned int val) void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type) { + FFIOContext *const ctx = ffiocontext(s); if (type == AVIO_DATA_MARKER_FLUSH_POINT) { if (s->buf_ptr - s->buffer >= s->min_packet_size) avio_flush(s); @@ -485,8 +488,8 @@ void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType typ // Avoid unnecessary flushes if we are already in non-header/trailer // data and setting the type to unknown if (type == AVIO_DATA_MARKER_UNKNOWN && - (s->current_type != AVIO_DATA_MARKER_HEADER && - s->current_type != AVIO_DATA_MARKER_TRAILER)) + (ctx->current_type != AVIO_DATA_MARKER_HEADER && + ctx->current_type != AVIO_DATA_MARKER_TRAILER)) return; switch (type) { @@ -494,7 +497,7 @@ void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType typ case AVIO_DATA_MARKER_TRAILER: // For header/trailer, ignore a new marker of the same type; // consecutive header/trailer markers can be merged. - if (type == s->current_type) + if (type == ctx->current_type) return; break; } @@ -502,8 +505,8 @@ void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType typ // If we've reached here, we have a new, noteworthy marker. // Flush the previous data and mark the start of the new data. avio_flush(s); - s->current_type = type; - s->last_time = time; + ctx->current_type = type; + ctx->last_time = time; } static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size) @@ -521,6 +524,7 @@ static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size) static void fill_buffer(AVIOContext *s) { + FFIOContext *const ctx = (FFIOContext *)s; int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE; uint8_t *dst = s->buf_end - s->buffer + max_buffer_size <= s->buffer_size ? @@ -543,15 +547,16 @@ static void fill_buffer(AVIOContext *s) } /* make buffer smaller in case it ended up large after probing */ - if (s->read_packet && s->orig_buffer_size && s->buffer_size > s->orig_buffer_size && len >= s->orig_buffer_size) { + if (s->read_packet && ctx->orig_buffer_size && + s->buffer_size > ctx->orig_buffer_size && len >= ctx->orig_buffer_size) { if (dst == s->buffer && s->buf_ptr != dst) { - int ret = set_buf_size(s, s->orig_buffer_size); + int ret = set_buf_size(s, ctx->orig_buffer_size); if (ret < 0) av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n"); s->checksum_ptr = dst = s->buffer; } - len = s->orig_buffer_size; + len = ctx->orig_buffer_size; } len = read_packet_wrapper(s, dst, len); @@ -566,7 +571,7 @@ static void fill_buffer(AVIOContext *s) s->pos += len; s->buf_ptr = dst; s->buf_end = dst + len; - s->bytes_read += len; + ffiocontext(s)->bytes_read += len; } } @@ -639,7 +644,7 @@ int avio_read(AVIOContext *s, unsigned char *buf, int size) break; } else { s->pos += len; - s->bytes_read += len; + ffiocontext(s)->bytes_read += len; size -= len; buf += len; // reset the buffer @@ -955,7 +960,7 @@ int ffio_fdopen(AVIOContext **s, URLContext *h) if (h->prot->url_read_seek) (*s)->seekable |= AVIO_SEEKABLE_TIME; } - (*s)->short_seek_get = (int (*)(void *))ffurl_get_short_seek; + ((FFIOContext*)(*s))->short_seek_get = (int (*)(void *))ffurl_get_short_seek; (*s)->av_class = &ff_avio_class; return 0; fail: @@ -1019,20 +1024,21 @@ int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size) int ffio_limit(AVIOContext *s, int size) { - if (s->maxsize>= 0) { + FFIOContext *const ctx = ffiocontext(s); + if (ctx->maxsize >= 0) { int64_t pos = avio_tell(s); - int64_t remaining = s->maxsize - pos; + int64_t remaining = ctx->maxsize - pos; if (remaining < size) { int64_t newsize = avio_size(s); - if (!s->maxsize || s->maxsize<newsize) - s->maxsize = newsize - !newsize; - if (pos > s->maxsize && s->maxsize >= 0) - s->maxsize = AVERROR(EIO); - if (s->maxsize >= 0) - remaining = s->maxsize - pos; + if (!ctx->maxsize || ctx->maxsize < newsize) + ctx->maxsize = newsize - !newsize; + if (pos > ctx->maxsize && ctx->maxsize >= 0) + ctx->maxsize = AVERROR(EIO); + if (ctx->maxsize >= 0) + remaining = ctx->maxsize - pos; } - if (s->maxsize >= 0 && remaining < size && size > 1) { + if (ctx->maxsize >= 0 && remaining < size && size > 1) { av_log(NULL, remaining ? AV_LOG_ERROR : AV_LOG_DEBUG, "Truncating packet of size %d to %"PRId64"\n", size, remaining + !remaining); @@ -1051,7 +1057,7 @@ static int set_buf_size(AVIOContext *s, int buf_size) av_free(s->buffer); s->buffer = buffer; - s->orig_buffer_size = + ffiocontext(s)->orig_buffer_size = s->buffer_size = buf_size; s->buf_ptr = s->buf_ptr_max = buffer; url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ); @@ -1078,7 +1084,7 @@ int ffio_realloc_buf(AVIOContext *s, int buf_size) memcpy(buffer, s->write_flag ? s->buffer : s->buf_ptr, data_size); av_free(s->buffer); s->buffer = buffer; - s->orig_buffer_size = buf_size; + ffiocontext(s)->orig_buffer_size = buf_size; s->buffer_size = buf_size; s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer; if (s->write_flag) @@ -1180,6 +1186,7 @@ int avio_open2(AVIOContext **s, const char *filename, int flags, int avio_close(AVIOContext *s) { + FFIOContext *const ctx = ffiocontext(s); URLContext *h; if (!s) @@ -1191,9 +1198,11 @@ int avio_close(AVIOContext *s) av_freep(&s->buffer); if (s->write_flag) - av_log(s, AV_LOG_VERBOSE, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count); + av_log(s, AV_LOG_VERBOSE, "Statistics: %d seeks, %d writeouts\n", + ctx->seek_count, ctx->writeout_count); else - av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count); + av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n", + ctx->bytes_read, ctx->seek_count); av_opt_free(s); avio_context_free(&s); @@ -1427,8 +1436,8 @@ void ffio_reset_dyn_buf(AVIOContext *s) DynBuffer *d = s->opaque; int max_packet_size = s->max_packet_size; - ffio_init_context(s, d->io_buffer, d->io_buffer_size, 1, d, NULL, - s->write_packet, s->seek); + ffio_init_context(ffiocontext(s), d->io_buffer, d->io_buffer_size, + 1, d, NULL, s->write_packet, s->seek); s->max_packet_size = max_packet_size; d->pos = d->size = 0; } |