[FFmpeg-devel] cache: read ahead to avoid excessive small requests

Robert Nagy ronag89 at gmail.com
Sat Sep 22 20:32:28 EEST 2018


diff --git a/libavformat/cache.c b/libavformat/cache.c
index 66bbbf54c9..48ff5ab363 100644
--- a/libavformat/cache.c
+++ b/libavformat/cache.c
@@ -153,6 +153,38 @@ fail:
     return ret;
 }

+static int cache_read_ahead(URLContext *h)
+{
+    Context *c= h->priv_data;
+    int64_t r, read_ahead, pos;
+    uint8_t buf[32768];
+
+    pos = c->logical_pos;
+    read_ahead = c->read_ahead_limit < 0
+        ? 512 * 512
+        : FFMIN(512 * 512, c->read_ahead_limit);
+
+    while (read_ahead > 0) {
+        r = ffurl_read(c->inner, buf, FFMIN(read_ahead, sizeof(buf)));
+        if (r == AVERROR_EOF) {
+            c->is_true_eof = 1;
+            av_assert0(c->end >= c->logical_pos);
+        }
+        if (r<=0)
+            break;
+        c->inner_pos += r;
+
+        add_entry(h, buf, r);
+        c->logical_pos += r;
+        c->end = FFMAX(c->end, c->logical_pos);
+        read_ahead -= r;
+    }
+
+    c->logical_pos = pos;
+
+    return r < 0 ? r : 0;
+}
+
 static int cache_read(URLContext *h, unsigned char *buf, int size)
 {
     Context *c= h->priv_data;
@@ -215,6 +247,10 @@ static int cache_read(URLContext *h, unsigned
char *buf, int size)
     c->logical_pos += r;
     c->end = FFMAX(c->end, c->logical_pos);

+    // Fake filling thread to avoid excessive
+    // small reads.
+    cache_read_ahead(h);
+
     return r;
 }


More information about the ffmpeg-devel mailing list