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(void *key, const void *node)
70 return (*(int64_t *) key) - ((const CacheEntry *) node)->logical_pos;
73 static int cache_open(URLContext *h, const char *arg, int flags)
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, NULL);
92 static int add_entry(URLContext *h, const unsigned char *buf, int size)
94 Context *c= h->priv_data;
97 CacheEntry *entry = av_malloc(sizeof(*entry));
98 CacheEntry *entry_ret;
99 struct AVTreeNode *node = av_tree_node_alloc();
101 if (!entry || !node) {
102 ret = AVERROR(ENOMEM);
107 pos = lseek(c->fd, 0, SEEK_END);
109 ret = AVERROR(errno);
110 av_log(h, AV_LOG_ERROR, "seek in cache failed\n");
114 ret = write(c->fd, buf, size);
116 ret = AVERROR(errno);
117 av_log(h, AV_LOG_ERROR, "write in cache failed\n");
121 entry->logical_pos = c->logical_pos;
122 entry->physical_pos = pos;
125 entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
126 if (entry_ret && entry_ret != entry) {
128 av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
131 c->cache_pos = entry->physical_pos + entry->size;
136 ftruncate(c->fd, pos);
142 static int cache_read(URLContext *h, unsigned char *buf, int size)
144 Context *c= h->priv_data;
145 CacheEntry *entry, *next[2] = {NULL, NULL};
148 entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
154 int64_t in_block_pos = c->logical_pos - entry->logical_pos;
155 av_assert0(entry->logical_pos <= c->logical_pos);
156 if (in_block_pos < entry->size) {
157 int64_t physical_target = entry->physical_pos + in_block_pos;
158 //FIXME avoid seek if unneeded
159 r = lseek(c->fd, physical_target, SEEK_SET);
161 r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));
171 // Cache miss or some kind of fault with the cache
173 if (c->logical_pos != c->inner_pos) {
174 r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);
176 av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");
182 r = ffurl_read(c->inner, buf, size);
183 if (r == 0 && size>0) {
185 av_assert0(c->end >= c->logical_pos);
193 add_entry(h, buf, r);
195 c->end = FFMAX(c->end, c->logical_pos);
200 static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
202 Context *c= h->priv_data;
205 if (whence == AVSEEK_SIZE) {
206 pos= ffurl_seek(c->inner, pos, whence);
208 pos= ffurl_seek(c->inner, -1, SEEK_END);
209 if (ffurl_seek(c->inner, c->inner_pos, SEEK_SET) < 0)
210 av_log(h, AV_LOG_ERROR, "Inner protocol failed to seekback end : %"PRId64"\n", pos);
214 c->end = FFMAX(c->end, pos);
218 if (whence == SEEK_CUR) {
220 pos += c->logical_pos;
221 } else if (whence == SEEK_END && c->is_true_eof) {
227 if (whence == SEEK_SET && pos >= 0 && pos < c->end) {
228 //Seems within filesize, assume it will not fail.
229 c->logical_pos = pos;
234 ret= ffurl_seek(c->inner, pos, whence);
235 if ((whence == SEEK_SET && pos >= c->logical_pos ||
236 whence == SEEK_END && pos <= 0) && ret < 0) {
237 if ( (whence == SEEK_SET && c->read_ahead_limit >= pos - c->logical_pos)
238 || c->read_ahead_limit < 0) {
240 while (c->logical_pos < pos || whence == SEEK_END) {
241 int size = sizeof(tmp);
242 if (whence == SEEK_SET)
243 size = FFMIN(sizeof(tmp), pos - c->logical_pos);
244 ret = cache_read(h, tmp, size);
245 if (ret == 0 && whence == SEEK_END) {
246 av_assert0(c->is_true_eof);
253 return c->logical_pos;
258 c->logical_pos = ret;
259 c->end = FFMAX(c->end, ret);
265 static int cache_close(URLContext *h)
267 Context *c= h->priv_data;
269 av_log(h, AV_LOG_INFO, "Statistics, cache hits:%"PRId64" cache misses:%"PRId64"\n",
270 c->cache_hit, c->cache_miss);
273 ffurl_close(c->inner);
274 av_tree_destroy(c->root);
279 #define OFFSET(x) offsetof(Context, x)
280 #define D AV_OPT_FLAG_DECODING_PARAM
282 static const AVOption options[] = {
283 { "read_ahead_limit", "Amount in bytes that may be read ahead when seeking isnt supported, -1 for unlimited", OFFSET(read_ahead_limit), AV_OPT_TYPE_INT, { .i64 = 65536 }, -1, INT_MAX, D },
287 static const AVClass cache_context_class = {
288 .class_name = "Cache",
289 .item_name = av_default_item_name,
291 .version = LIBAVUTIL_VERSION_INT,
294 URLProtocol ff_cache_protocol = {
296 .url_open = cache_open,
297 .url_read = cache_read,
298 .url_seek = cache_seek,
299 .url_close = cache_close,
300 .priv_data_size = sizeof(Context),
301 .priv_data_class = &cache_context_class,