[FFmpeg-devel] [PATCH] avformat/aviobuf: Improve readability of aviobuf
Bryan Huh
bryan at box.com
Mon Nov 16 03:02:11 CET 2015
No functional changes in this commit, mostly adding comments for
improved readability. Also minor re-arrangements of variables.
---
libavformat/avio.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++
libavformat/aviobuf.c | 16 +++++++++-------
2 files changed, 56 insertions(+), 7 deletions(-)
diff --git a/libavformat/avio.h b/libavformat/avio.h
index 06dd7f5..abe13b2 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -122,6 +122,53 @@ typedef struct AVIOContext {
* to any av_opt_* functions in that case.
*/
const AVClass *av_class;
+
+ /*
+ * The following shows the relationship between buffer, buf_ptr, buf_end, buf_size,
+ * and pos, when reading and when writing (since AVIOContext is used for both):
+ *
+ **********************************************************************************
+ * READING
+ **********************************************************************************
+ *
+ * | buffer_size |
+ * |---------------------------------------|
+ * | |
+ *
+ * buffer buf_ptr buf_end
+ * +---------------+-----------------------+
+ * |/ / / / / / / /|/ / / / / / /| |
+ * read buffer: |/ / consumed / | to be read /| |
+ * |/ / / / / / / /|/ / / / / / /| |
+ * +---------------+-----------------------+
+ *
+ * pos
+ * +-------------------------------------------+-----------------+
+ * input file: | | |
+ * +-------------------------------------------+-----------------+
+ *
+ *
+ **********************************************************************************
+ * WRITING
+ **********************************************************************************
+ *
+ * | buffer_size |
+ * |-------------------------------|
+ * | |
+ *
+ * buffer buf_ptr buf_end
+ * +-------------------+-----------+
+ * |/ / / / / / / / / /| |
+ * write buffer: | / to be flushed / | |
+ * |/ / / / / / / / / /| |
+ * +-------------------+-----------+
+ *
+ * pos
+ * +--------------------------+-----------------------------------+
+ * output file: | | |
+ * +--------------------------+-----------------------------------+
+ *
+ */
unsigned char *buffer; /**< Start of the buffer. */
int buffer_size; /**< Maximum buffer size */
unsigned char *buf_ptr; /**< Current position in the buffer */
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 1b3d5f5..c2aa8dc 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -213,6 +213,7 @@ int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
return AVERROR(EINVAL);
buffer_size = s->buf_end - s->buffer;
+ // pos is the absolute position that the beginning of s->buffer corresponds to in the file
pos = s->pos - (s->write_flag ? 0 : buffer_size);
if (whence != SEEK_CUR && whence != SEEK_SET)
@@ -227,13 +228,13 @@ int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
if (offset < 0)
return AVERROR(EINVAL);
- offset1 = offset - pos;
+ offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
if (!s->must_flush && (!s->direct || !s->seek) &&
offset1 >= 0 && offset1 <= buffer_size - s->write_flag) {
/* can do the seek inside the buffer */
s->buf_ptr = s->buffer + offset1;
} else if ((!s->seekable ||
- offset1 <= s->buf_end + s->short_seek_threshold - s->buffer) &&
+ offset1 <= buffer_size + s->short_seek_threshold) &&
!s->write_flag && offset1 >= 0 &&
(!s->direct || !s->seek) &&
(whence != SEEK_END || force)) {
@@ -241,7 +242,7 @@ int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fill_buffer(s);
if (s->eof_reached)
return AVERROR_EOF;
- s->buf_ptr = s->buf_end + offset - s->pos;
+ s->buf_ptr = s->buf_end - (s->pos - offset);
} else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
int64_t res;
@@ -541,13 +542,13 @@ int avio_read(AVIOContext *s, unsigned char *buf, int size)
size1 = size;
while (size > 0) {
- len = s->buf_end - s->buf_ptr;
- if (len > size)
- len = size;
+ len = FFMIN(s->buf_end - s->buf_ptr, size);
if (len == 0 || s->write_flag) {
- if((s->direct || size > s->buffer_size) && !s->update_checksum){
+ if((s->direct || size > s->buffer_size) && !s->update_checksum) {
+ // bypass the buffer and read data directly into buf
if(s->read_packet)
len = s->read_packet(s->opaque, buf, size);
+
if (len <= 0) {
/* do not modify buffer if EOF reached so that a seek back can
be done without rereading data */
@@ -560,6 +561,7 @@ int avio_read(AVIOContext *s, unsigned char *buf, int size)
s->bytes_read += len;
size -= len;
buf += len;
+ // reset the buffer
s->buf_ptr = s->buffer;
s->buf_end = s->buffer/* + len*/;
}
--
1.7.1
More information about the ffmpeg-devel
mailing list