2 * Input cache protocol.
3 * Copyright (c) 2011,2014 Michael Niedermayer
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Based on file.c by Fabrice Bellard
26 * support keeping files
27 * support filling with a background thread
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/file.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/tree.h"
45 #include "os_support.h"
48 typedef struct CacheEntry {
54 typedef struct Context {
57 struct AVTreeNode *root;
64 int64_t cache_hit, cache_miss;
68 static int cmp(const void *key, const void *node)
70 return FFDIFFSIGN(*(const int64_t *)key, ((const CacheEntry *) node)->logical_pos);
73 static int cache_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
76 Context *c= h->priv_data;
78 av_strstart(arg, "cache:", &arg);
80 c->fd = av_tempfile("ffcache", &buffername, 0, h);
82 av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
87 av_freep(&buffername);
89 return ffurl_open(&c->inner, arg, flags, &h->interrupt_callback, options);
92 static int add_entry(URLContext *h, const unsigned char *buf, int size)
94 Context *c= h->priv_data;
97 CacheEntry *entry = NULL, *next[2] = {NULL, NULL};
98 CacheEntry *entry_ret;
99 struct AVTreeNode *node = NULL;
102 pos = lseek(c->fd, 0, SEEK_END);
104 ret = AVERROR(errno);
105 av_log(h, AV_LOG_ERROR, "seek in cache failed\n");
110 ret = write(c->fd, buf, size);
112 ret = AVERROR(errno);
113 av_log(h, AV_LOG_ERROR, "write in cache failed\n");
118 entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
124 entry->logical_pos + entry->size != c->logical_pos ||
125 entry->physical_pos + entry->size != pos
127 entry = av_malloc(sizeof(*entry));
128 node = av_tree_node_alloc();
129 if (!entry || !node) {
130 ret = AVERROR(ENOMEM);
133 entry->logical_pos = c->logical_pos;
134 entry->physical_pos = pos;
137 entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
138 if (entry_ret && entry_ret != entry) {
140 av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
148 //we could truncate the file to pos here if pos >=0 but ftruncate isn't available in VS so
149 //for simplicty we just leave the file a bit larger
155 static int cache_read(URLContext *h, unsigned char *buf, int size)
157 Context *c= h->priv_data;
158 CacheEntry *entry, *next[2] = {NULL, NULL};
161 entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
167 int64_t in_block_pos = c->logical_pos - entry->logical_pos;
168 av_assert0(entry->logical_pos <= c->logical_pos);
169 if (in_block_pos < entry->size) {
170 int64_t physical_target = entry->physical_pos + in_block_pos;
172 if (c->cache_pos != physical_target) {
173 r = lseek(c->fd, physical_target, SEEK_SET);
179 r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));
191 // Cache miss or some kind of fault with the cache
193 if (c->logical_pos != c->inner_pos) {
194 r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);
196 av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");
202 r = ffurl_read(c->inner, buf, size);
203 if (r == 0 && size>0) {
205 av_assert0(c->end >= c->logical_pos);
213 add_entry(h, buf, r);
215 c->end = FFMAX(c->end, c->logical_pos);
220 static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
222 Context *c= h->priv_data;
225 if (whence == AVSEEK_SIZE) {
226 pos= ffurl_seek(c->inner, pos, whence);
228 pos= ffurl_seek(c->inner, -1, SEEK_END);
229 if (ffurl_seek(c->inner, c->inner_pos, SEEK_SET) < 0)
230 av_log(h, AV_LOG_ERROR, "Inner protocol failed to seekback end : %"PRId64"\n", pos);
234 c->end = FFMAX(c->end, pos);
238 if (whence == SEEK_CUR) {
240 pos += c->logical_pos;
241 } else if (whence == SEEK_END && c->is_true_eof) {
247 if (whence == SEEK_SET && pos >= 0 && pos < c->end) {
248 //Seems within filesize, assume it will not fail.
249 c->logical_pos = pos;
254 ret= ffurl_seek(c->inner, pos, whence);
255 if ((whence == SEEK_SET && pos >= c->logical_pos ||
256 whence == SEEK_END && pos <= 0) && ret < 0) {
257 if ( (whence == SEEK_SET && c->read_ahead_limit >= pos - c->logical_pos)
258 || c->read_ahead_limit < 0) {
260 while (c->logical_pos < pos || whence == SEEK_END) {
261 int size = sizeof(tmp);
262 if (whence == SEEK_SET)
263 size = FFMIN(sizeof(tmp), pos - c->logical_pos);
264 ret = cache_read(h, tmp, size);
265 if (ret == 0 && whence == SEEK_END) {
266 av_assert0(c->is_true_eof);
273 return c->logical_pos;
278 c->logical_pos = ret;
279 c->end = FFMAX(c->end, ret);
285 static int cache_close(URLContext *h)
287 Context *c= h->priv_data;
289 av_log(h, AV_LOG_INFO, "Statistics, cache hits:%"PRId64" cache misses:%"PRId64"\n",
290 c->cache_hit, c->cache_miss);
293 ffurl_close(c->inner);
294 av_tree_destroy(c->root);
299 #define OFFSET(x) offsetof(Context, x)
300 #define D AV_OPT_FLAG_DECODING_PARAM
302 static const AVOption options[] = {
303 { "read_ahead_limit", "Amount in bytes that may be read ahead when seeking isn't supported, -1 for unlimited", OFFSET(read_ahead_limit), AV_OPT_TYPE_INT, { .i64 = 65536 }, -1, INT_MAX, D },
307 static const AVClass cache_context_class = {
308 .class_name = "Cache",
309 .item_name = av_default_item_name,
311 .version = LIBAVUTIL_VERSION_INT,
314 URLProtocol ff_cache_protocol = {
316 .url_open2 = cache_open,
317 .url_read = cache_read,
318 .url_seek = cache_seek,
319 .url_close = cache_close,
320 .priv_data_size = sizeof(Context),
321 .priv_data_class = &cache_context_class,