diff mbox

[FFmpeg-devel] avformat/async: allow to set buffer size [v2]

Message ID 1506065133-9805-1-git-send-email-jjsuwa.sys3175@gmail.com
State New
Headers show

Commit Message

Takayuki 'January June' Suwa Sept. 22, 2017, 7:25 a.m. UTC
- add "-asyncbufsize" option to async:
- refactor async.c
  - remove READ_BACK_CAPACITY and the 3rd arg of ring_init()
  - replace some magicnums with symconst macros
---
 doc/protocols.texi  |  6 ++++++
 libavformat/async.c | 18 +++++++++++-------
 2 files changed, 17 insertions(+), 7 deletions(-)

Comments

Michael Niedermayer Sept. 23, 2017, 11:58 p.m. UTC | #1
On Fri, Sep 22, 2017 at 04:25:33PM +0900, Takayuki 'January June' Suwa wrote:
> - add "-asyncbufsize" option to async:
> - refactor async.c
>   - remove READ_BACK_CAPACITY and the 3rd arg of ring_init()
>   - replace some magicnums with symconst macros

This should be several patches as they are independant changes

[...]
> @@ -466,6 +469,7 @@ static int64_t async_seek(URLContext *h, int64_t pos, int whence)
>  #define D AV_OPT_FLAG_DECODING_PARAM
>  
>  static const AVOption options[] = {

> +    { "asyncbufsize", "Amount in bytes that may be used for asynchronous data buffering", OFFSET(asyncbufsize), AV_OPT_TYPE_INT, { .i64 = BUFFER_CAPACITY }, 0, INT_MAX, D },

the minimum should probably be BUFFER_CAPACITY_MIN and not 0

[...]
diff mbox

Patch

diff --git a/doc/protocols.texi b/doc/protocols.texi
index a7968ff56e3..f321664d7bf 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -57,6 +57,12 @@  Asynchronous data filling wrapper for input stream.
 
 Fill data in a background thread, to decouple I/O operation from demux thread.
 
+The accepted options are:
+@table @option
+@item asyncbufsize
+Set asynchronous data buffer size, in bytes. Default value is 8 MiB.
+@end table
+
 @example
 async:@var{URL}
 async:http://host/resource
diff --git a/libavformat/async.c b/libavformat/async.c
index 54dbd2312a2..28fda66fdd0 100644
--- a/libavformat/async.c
+++ b/libavformat/async.c
@@ -41,8 +41,9 @@ 
 #include <unistd.h>
 #endif
 
-#define BUFFER_CAPACITY         (4 * 1024 * 1024)
-#define READ_BACK_CAPACITY      (4 * 1024 * 1024)
+#define BUFFER_CAPACITY         (8 * 1024 * 1024)
+#define BUFFER_CAPACITY_MIN     4096
+#define BUFFER_FETCH_SIZE       4096
 #define SHORT_SEEK_THRESHOLD    (256 * 1024)
 
 typedef struct RingBuffer
@@ -78,16 +79,18 @@  typedef struct Context {
 
     int             abort_request;
     AVIOInterruptCB interrupt_callback;
+
+    int             asyncbufsize;
 } Context;
 
-static int ring_init(RingBuffer *ring, unsigned int capacity, int read_back_capacity)
+static int ring_init(RingBuffer *ring, int capacity)
 {
     memset(ring, 0, sizeof(RingBuffer));
-    ring->fifo = av_fifo_alloc(capacity + read_back_capacity);
+    ring->fifo = av_fifo_alloc(capacity);
     if (!ring->fifo)
         return AVERROR(ENOMEM);
 
-    ring->read_back_capacity = read_back_capacity;
+    ring->read_back_capacity = capacity / 2;
     return 0;
 }
 
@@ -220,7 +223,7 @@  static void *async_buffer_task(void *arg)
         }
         pthread_mutex_unlock(&c->mutex);
 
-        to_copy = FFMIN(4096, fifo_space);
+        to_copy = FFMIN(BUFFER_FETCH_SIZE, fifo_space);
         ret = ring_generic_write(ring, (void *)h, to_copy, wrapped_url_read);
 
         pthread_mutex_lock(&c->mutex);
@@ -245,7 +248,7 @@  static int async_open(URLContext *h, const char *arg, int flags, AVDictionary **
 
     av_strstart(arg, "async:", &arg);
 
-    ret = ring_init(&c->ring, BUFFER_CAPACITY, READ_BACK_CAPACITY);
+    ret = ring_init(&c->ring, FFMAX(BUFFER_CAPACITY_MIN, c->asyncbufsize));
     if (ret < 0)
         goto fifo_fail;
 
@@ -466,6 +469,7 @@  static int64_t async_seek(URLContext *h, int64_t pos, int whence)
 #define D AV_OPT_FLAG_DECODING_PARAM
 
 static const AVOption options[] = {
+    { "asyncbufsize", "Amount in bytes that may be used for asynchronous data buffering", OFFSET(asyncbufsize), AV_OPT_TYPE_INT, { .i64 = BUFFER_CAPACITY }, 0, INT_MAX, D },
     {NULL},
 };