]> git.sesse.net Git - ffmpeg/blob - libavformat/cache.c
48a342f0ec5bd3c6931159a6739842cab67ceaf8
[ffmpeg] / libavformat / cache.c
1 /*
2  * Input cache protocol.
3  * Copyright (c) 2011,2014 Michael Niedermayer
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  *
21  * Based on file.c by Fabrice Bellard
22  */
23
24 /**
25  * @TODO
26  *      support keeping files
27  *      support filling with a background thread
28  */
29
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/file.h"
33 #include "libavutil/tree.h"
34 #include "avformat.h"
35 #include <fcntl.h>
36 #if HAVE_IO_H
37 #include <io.h>
38 #endif
39 #if HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #include <sys/stat.h>
43 #include <stdlib.h>
44 #include "os_support.h"
45 #include "url.h"
46
47 typedef struct CacheEntry {
48     int64_t logical_pos;
49     int64_t physical_pos;
50     int size;
51 } CacheEntry;
52
53 typedef struct Context {
54     int fd;
55     struct AVTreeNode *root;
56     int64_t logical_pos;
57     int64_t cache_pos;
58     int64_t inner_pos;
59     int64_t end;
60     int is_true_eof;
61     URLContext *inner;
62     int64_t cache_hit, cache_miss;
63 } Context;
64
65 static int cmp(void *key, const void *node)
66 {
67     return (*(int64_t *) key) - ((const CacheEntry *) node)->logical_pos;
68 }
69
70 static int cache_open(URLContext *h, const char *arg, int flags)
71 {
72     char *buffername;
73     Context *c= h->priv_data;
74
75     av_strstart(arg, "cache:", &arg);
76
77     c->fd = av_tempfile("ffcache", &buffername, 0, h);
78     if (c->fd < 0){
79         av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
80         return c->fd;
81     }
82
83     unlink(buffername);
84     av_freep(&buffername);
85
86     return ffurl_open(&c->inner, arg, flags, &h->interrupt_callback, NULL);
87 }
88
89 static int add_entry(URLContext *h, const unsigned char *buf, int size)
90 {
91     Context *c= h->priv_data;
92     int64_t pos;
93     int ret;
94     CacheEntry *entry = av_malloc(sizeof(*entry));
95     CacheEntry *entry_ret;
96     struct AVTreeNode *node = av_tree_node_alloc();
97
98     if (!entry || !node) {
99         ret = AVERROR(ENOMEM);
100         goto fail;
101     }
102
103     //FIXME avoid lseek
104     pos = lseek(c->fd, 0, SEEK_END);
105     if (pos < 0) {
106         ret = AVERROR(errno);
107         av_log(h, AV_LOG_ERROR, "seek in cache failed\n");
108         goto fail;
109     }
110
111     ret = write(c->fd, buf, size);
112     if (ret < 0) {
113         ret = AVERROR(errno);
114         av_log(h, AV_LOG_ERROR, "write in cache failed\n");
115         goto fail;
116     }
117
118     entry->logical_pos = c->logical_pos;
119     entry->physical_pos = pos;
120     entry->size = ret;
121
122     entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
123     if (entry_ret && entry_ret != entry) {
124         ret = -1;
125         av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
126         goto fail;
127     }
128     c->cache_pos = entry->physical_pos + entry->size;
129
130     return 0;
131 fail:
132     av_free(entry);
133     av_free(node);
134     return ret;
135 }
136
137 static int cache_read(URLContext *h, unsigned char *buf, int size)
138 {
139     Context *c= h->priv_data;
140     CacheEntry *entry, *next[2] = {NULL, NULL};
141     int r;
142
143     entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
144
145     if (!entry)
146         entry = next[0];
147
148     if (entry) {
149         int64_t in_block_pos = c->logical_pos - entry->logical_pos;
150         av_assert0(entry->logical_pos <= c->logical_pos);
151         if (in_block_pos < entry->size) {
152             int64_t physical_target = entry->physical_pos + in_block_pos;
153             //FIXME avoid seek if unneeded
154             r = lseek(c->fd, physical_target, SEEK_SET);
155             if (r >= 0)
156                 r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));
157
158             if (r > 0) {
159                 c->logical_pos += r;
160                 c->cache_hit ++;
161                 return r;
162             }
163         }
164     }
165
166     // Cache miss or some kind of fault with the cache
167
168     if (c->logical_pos != c->inner_pos) {
169         r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);
170         if (r<0) {
171             av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");
172             return r;
173         }
174         c->inner_pos = r;
175     }
176
177     r = ffurl_read(c->inner, buf, size);
178     if (r == 0 && size>0) {
179         c->is_true_eof = 1;
180         av_assert0(c->end >= c->logical_pos);
181     }
182     if (r<=0)
183         return r;
184     c->inner_pos += r;
185
186     c->cache_miss ++;
187
188     add_entry(h, buf, r);
189     c->logical_pos += r;
190     c->end = FFMAX(c->end, c->logical_pos);
191
192     return r;
193 }
194
195 static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
196 {
197     Context *c= h->priv_data;
198     int64_t ret;
199
200     if (whence == AVSEEK_SIZE) {
201         pos= ffurl_seek(c->inner, pos, whence);
202         if(pos <= 0){
203             pos= ffurl_seek(c->inner, -1, SEEK_END);
204             if (ffurl_seek(c->inner, c->inner_pos, SEEK_SET) < 0)
205                 av_log(h, AV_LOG_ERROR, "Inner protocol failed to seekback end : %"PRId64"\n", pos);
206         }
207         if (pos > 0)
208             c->is_true_eof = 1;
209         c->end = FFMAX(c->end, pos);
210         return pos;
211     }
212
213     if (whence == SEEK_CUR) {
214         whence = SEEK_SET;
215         pos += c->logical_pos;
216     } else if (whence == SEEK_END && c->is_true_eof) {
217         whence = SEEK_SET;
218         pos += c->end;
219     }
220
221     if (whence == SEEK_SET && pos >= 0 && pos < c->end) {
222         //Seems within filesize, assume it will not fail.
223         c->logical_pos = pos;
224         return pos;
225     }
226
227     //cache miss
228     ret= ffurl_seek(c->inner, pos, whence);
229
230     if (ret >= 0) {
231         c->logical_pos = ret;
232         c->end = FFMAX(c->end, ret);
233     }
234
235     return ret;
236 }
237
238 static int cache_close(URLContext *h)
239 {
240     Context *c= h->priv_data;
241
242     av_log(h, AV_LOG_INFO, "Statistics, cache hits:%"PRId64" cache misses:%"PRId64"\n",
243            c->cache_hit, c->cache_miss);
244
245     close(c->fd);
246     ffurl_close(c->inner);
247     av_tree_destroy(c->root);
248
249
250     return 0;
251 }
252
253 URLProtocol ff_cache_protocol = {
254     .name                = "cache",
255     .url_open            = cache_open,
256     .url_read            = cache_read,
257     .url_seek            = cache_seek,
258     .url_close           = cache_close,
259     .priv_data_size      = sizeof(Context),
260 };