]> git.sesse.net Git - ffmpeg/blob - libavformat/cache.c
vp9: make above buffer pointer 32-byte aligned.
[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/opt.h"
34 #include "libavutil/tree.h"
35 #include "avformat.h"
36 #include <fcntl.h>
37 #if HAVE_IO_H
38 #include <io.h>
39 #endif
40 #if HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #include <sys/stat.h>
44 #include <stdlib.h>
45 #include "os_support.h"
46 #include "url.h"
47
48 typedef struct CacheEntry {
49     int64_t logical_pos;
50     int64_t physical_pos;
51     int size;
52 } CacheEntry;
53
54 typedef struct Context {
55     AVClass *class;
56     int fd;
57     struct AVTreeNode *root;
58     int64_t logical_pos;
59     int64_t cache_pos;
60     int64_t inner_pos;
61     int64_t end;
62     int is_true_eof;
63     URLContext *inner;
64     int64_t cache_hit, cache_miss;
65     int read_ahead_limit;
66 } Context;
67
68 static int cmp(void *key, const void *node)
69 {
70     return (*(int64_t *) key) - ((const CacheEntry *) node)->logical_pos;
71 }
72
73 static int cache_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
74 {
75     char *buffername;
76     Context *c= h->priv_data;
77
78     av_strstart(arg, "cache:", &arg);
79
80     c->fd = av_tempfile("ffcache", &buffername, 0, h);
81     if (c->fd < 0){
82         av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
83         return c->fd;
84     }
85
86     unlink(buffername);
87     av_freep(&buffername);
88
89     return ffurl_open(&c->inner, arg, flags, &h->interrupt_callback, options);
90 }
91
92 static int add_entry(URLContext *h, const unsigned char *buf, int size)
93 {
94     Context *c= h->priv_data;
95     int64_t pos = -1;
96     int ret;
97     CacheEntry *entry = NULL, *next[2] = {NULL, NULL};
98     CacheEntry *entry_ret;
99     struct AVTreeNode *node = NULL;
100
101     //FIXME avoid lseek
102     pos = lseek(c->fd, 0, SEEK_END);
103     if (pos < 0) {
104         ret = AVERROR(errno);
105         av_log(h, AV_LOG_ERROR, "seek in cache failed\n");
106         goto fail;
107     }
108     c->cache_pos = pos;
109
110     ret = write(c->fd, buf, size);
111     if (ret < 0) {
112         ret = AVERROR(errno);
113         av_log(h, AV_LOG_ERROR, "write in cache failed\n");
114         goto fail;
115     }
116     c->cache_pos += ret;
117
118     entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
119
120     if (!entry)
121         entry = next[0];
122
123     if (!entry ||
124         entry->logical_pos  + entry->size != c->logical_pos ||
125         entry->physical_pos + entry->size != pos
126     ) {
127         entry = av_malloc(sizeof(*entry));
128         node = av_tree_node_alloc();
129         if (!entry || !node) {
130             ret = AVERROR(ENOMEM);
131             goto fail;
132         }
133         entry->logical_pos = c->logical_pos;
134         entry->physical_pos = pos;
135         entry->size = ret;
136
137         entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
138         if (entry_ret && entry_ret != entry) {
139             ret = -1;
140             av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
141             goto fail;
142         }
143     } else
144         entry->size += ret;
145
146     return 0;
147 fail:
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
150     av_free(entry);
151     av_free(node);
152     return ret;
153 }
154
155 static int cache_read(URLContext *h, unsigned char *buf, int size)
156 {
157     Context *c= h->priv_data;
158     CacheEntry *entry, *next[2] = {NULL, NULL};
159     int r;
160
161     entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
162
163     if (!entry)
164         entry = next[0];
165
166     if (entry) {
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;
171
172             if (c->cache_pos != physical_target) {
173                 r = lseek(c->fd, physical_target, SEEK_SET);
174             } else
175                 r = c->cache_pos;
176
177             if (r >= 0) {
178                 c->cache_pos = r;
179                 r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));
180             }
181
182             if (r > 0) {
183                 c->cache_pos += r;
184                 c->logical_pos += r;
185                 c->cache_hit ++;
186                 return r;
187             }
188         }
189     }
190
191     // Cache miss or some kind of fault with the cache
192
193     if (c->logical_pos != c->inner_pos) {
194         r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);
195         if (r<0) {
196             av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");
197             return r;
198         }
199         c->inner_pos = r;
200     }
201
202     r = ffurl_read(c->inner, buf, size);
203     if (r == 0 && size>0) {
204         c->is_true_eof = 1;
205         av_assert0(c->end >= c->logical_pos);
206     }
207     if (r<=0)
208         return r;
209     c->inner_pos += r;
210
211     c->cache_miss ++;
212
213     add_entry(h, buf, r);
214     c->logical_pos += r;
215     c->end = FFMAX(c->end, c->logical_pos);
216
217     return r;
218 }
219
220 static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
221 {
222     Context *c= h->priv_data;
223     int64_t ret;
224
225     if (whence == AVSEEK_SIZE) {
226         pos= ffurl_seek(c->inner, pos, whence);
227         if(pos <= 0){
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);
231         }
232         if (pos > 0)
233             c->is_true_eof = 1;
234         c->end = FFMAX(c->end, pos);
235         return pos;
236     }
237
238     if (whence == SEEK_CUR) {
239         whence = SEEK_SET;
240         pos += c->logical_pos;
241     } else if (whence == SEEK_END && c->is_true_eof) {
242 resolve_eof:
243         whence = SEEK_SET;
244         pos += c->end;
245     }
246
247     if (whence == SEEK_SET && pos >= 0 && pos < c->end) {
248         //Seems within filesize, assume it will not fail.
249         c->logical_pos = pos;
250         return pos;
251     }
252
253     //cache miss
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) {
259             uint8_t tmp[32768];
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);
267                     goto resolve_eof;
268                 }
269                 if (ret < 0) {
270                     return ret;
271                 }
272             }
273             return c->logical_pos;
274         }
275     }
276
277     if (ret >= 0) {
278         c->logical_pos = ret;
279         c->end = FFMAX(c->end, ret);
280     }
281
282     return ret;
283 }
284
285 static int cache_close(URLContext *h)
286 {
287     Context *c= h->priv_data;
288
289     av_log(h, AV_LOG_INFO, "Statistics, cache hits:%"PRId64" cache misses:%"PRId64"\n",
290            c->cache_hit, c->cache_miss);
291
292     close(c->fd);
293     ffurl_close(c->inner);
294     av_tree_destroy(c->root);
295
296     return 0;
297 }
298
299 #define OFFSET(x) offsetof(Context, x)
300 #define D AV_OPT_FLAG_DECODING_PARAM
301
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 },
304     {NULL},
305 };
306
307 static const AVClass cache_context_class = {
308     .class_name = "Cache",
309     .item_name  = av_default_item_name,
310     .option     = options,
311     .version    = LIBAVUTIL_VERSION_INT,
312 };
313
314 URLProtocol ff_cache_protocol = {
315     .name                = "cache",
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,
322 };