FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/cache.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 117 154 76.0%
Functions: 7 7 100.0%
Branches: 59 106 55.7%

Line Branch Exec Source
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_open.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/tree.h"
36 #include "avio.h"
37 #include <fcntl.h>
38 #if HAVE_IO_H
39 #include <io.h>
40 #endif
41 #if HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #include <sys/stat.h>
45 #include <stdlib.h>
46 #include "os_support.h"
47 #include "url.h"
48
49 typedef struct CacheEntry {
50 int64_t logical_pos;
51 int64_t physical_pos;
52 int size;
53 } CacheEntry;
54
55 typedef struct CacheContext {
56 AVClass *class;
57 int fd;
58 char *filename;
59 struct AVTreeNode *root;
60 int64_t logical_pos;
61 int64_t cache_pos;
62 int64_t inner_pos;
63 int64_t end;
64 int is_true_eof;
65 URLContext *inner;
66 int64_t cache_hit, cache_miss;
67 int read_ahead_limit;
68 } CacheContext;
69
70 33 static int cmp(const void *key, const void *node)
71 {
72 33 return FFDIFFSIGN(*(const int64_t *)key, ((const CacheEntry *) node)->logical_pos);
73 }
74
75 1 static int cache_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
76 {
77 1 CacheContext *c = h->priv_data;
78 int ret;
79 char *buffername;
80
81 1 av_strstart(arg, "cache:", &arg);
82
83 1 c->fd = avpriv_tempfile("ffcache", &buffername, 0, h);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (c->fd < 0){
85 av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
86 return c->fd;
87 }
88
89 1 ret = unlink(buffername);
90
91
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ret >= 0)
92 1 av_freep(&buffername);
93 else
94 c->filename = buffername;
95
96 1 return ffurl_open_whitelist(&c->inner, arg, flags, &h->interrupt_callback,
97 options, h->protocol_whitelist, h->protocol_blacklist, h);
98 }
99
100 8 static int add_entry(URLContext *h, const unsigned char *buf, int size)
101 {
102 8 CacheContext *c = h->priv_data;
103 8 int64_t pos = -1;
104 int ret;
105 8 CacheEntry *entry = NULL, *next[2] = {NULL, NULL};
106 CacheEntry *entry_ret;
107 8 struct AVTreeNode *node = NULL;
108
109 //FIXME avoid lseek
110 8 pos = lseek(c->fd, 0, SEEK_END);
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (pos < 0) {
112 ret = AVERROR(errno);
113 av_log(h, AV_LOG_ERROR, "seek in cache failed\n");
114 goto fail;
115 }
116 8 c->cache_pos = pos;
117
118 8 ret = write(c->fd, buf, size);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0) {
120 ret = AVERROR(errno);
121 av_log(h, AV_LOG_ERROR, "write in cache failed\n");
122 goto fail;
123 }
124 8 c->cache_pos += ret;
125
126 8 entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
127
128
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!entry)
129 8 entry = next[0];
130
131
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 if (!entry ||
132
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 entry->logical_pos + entry->size != c->logical_pos ||
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 entry->physical_pos + entry->size != pos
134 ) {
135 1 entry = av_malloc(sizeof(*entry));
136 1 node = av_tree_node_alloc();
137
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!entry || !node) {
138 ret = AVERROR(ENOMEM);
139 goto fail;
140 }
141 1 entry->logical_pos = c->logical_pos;
142 1 entry->physical_pos = pos;
143 1 entry->size = ret;
144
145 1 entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
146
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (entry_ret && entry_ret != entry) {
147 ret = -1;
148 av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
149 goto fail;
150 }
151 } else
152 7 entry->size += ret;
153
154 8 return 0;
155 fail:
156 //we could truncate the file to pos here if pos >=0 but ftruncate isn't available in VS so
157 //for simplicty we just leave the file a bit larger
158 av_free(entry);
159 av_free(node);
160 return ret;
161 }
162
163 27 static int cache_read(URLContext *h, unsigned char *buf, int size)
164 {
165 27 CacheContext *c = h->priv_data;
166 27 CacheEntry *entry, *next[2] = {NULL, NULL};
167 int64_t r;
168
169 27 entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
170
171
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6 times.
27 if (!entry)
172 21 entry = next[0];
173
174
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
27 if (entry) {
175 26 int64_t in_block_pos = c->logical_pos - entry->logical_pos;
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 av_assert0(entry->logical_pos <= c->logical_pos);
177
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8 times.
26 if (in_block_pos < entry->size) {
178 18 int64_t physical_target = entry->physical_pos + in_block_pos;
179
180
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
18 if (c->cache_pos != physical_target) {
181 13 r = lseek(c->fd, physical_target, SEEK_SET);
182 } else
183 5 r = c->cache_pos;
184
185
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (r >= 0) {
186 18 c->cache_pos = r;
187 18 r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));
188 }
189
190
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (r > 0) {
191 18 c->cache_pos += r;
192 18 c->logical_pos += r;
193 18 c->cache_hit ++;
194 18 return r;
195 }
196 }
197 }
198
199 // Cache miss or some kind of fault with the cache
200
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (c->logical_pos != c->inner_pos) {
202 r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);
203 if (r<0) {
204 av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");
205 return r;
206 }
207 c->inner_pos = r;
208 }
209
210 9 r = ffurl_read(c->inner, buf, size);
211
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
9 if (r == AVERROR_EOF && size>0) {
212 1 c->is_true_eof = 1;
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(c->end >= c->logical_pos);
214 }
215
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (r<=0)
216 1 return r;
217 8 c->inner_pos += r;
218
219 8 c->cache_miss ++;
220
221 8 add_entry(h, buf, r);
222 8 c->logical_pos += r;
223 8 c->end = FFMAX(c->end, c->logical_pos);
224
225 8 return r;
226 }
227
228 27 static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
229 {
230 27 CacheContext *c = h->priv_data;
231 int64_t ret;
232
233
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 22 times.
27 if (whence == AVSEEK_SIZE) {
234 5 pos= ffurl_seek(c->inner, pos, whence);
235
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if(pos <= 0){
236 5 pos= ffurl_seek(c->inner, -1, SEEK_END);
237
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if (ffurl_seek(c->inner, c->inner_pos, SEEK_SET) < 0)
238 5 av_log(h, AV_LOG_ERROR, "Inner protocol failed to seekback end : %"PRId64"\n", pos);
239 }
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (pos > 0)
241 c->is_true_eof = 1;
242 5 c->end = FFMAX(c->end, pos);
243 5 return pos;
244 }
245
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (whence == SEEK_CUR) {
247 whence = SEEK_SET;
248 pos += c->logical_pos;
249
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
22 } else if (whence == SEEK_END && c->is_true_eof) {
250 4 resolve_eof:
251 5 whence = SEEK_SET;
252 5 pos += c->end;
253 }
254
255
4/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
23 if (whence == SEEK_SET && pos >= 0 && pos < c->end) {
256 //Seems within filesize, assume it will not fail.
257 22 c->logical_pos = pos;
258 22 return pos;
259 }
260
261 //cache miss
262 1 ret= ffurl_seek(c->inner, pos, whence);
263
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 if ((whence == SEEK_SET && pos >= c->logical_pos ||
264
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 whence == SEEK_END && pos <= 0) && ret < 0) {
265
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if ( (whence == SEEK_SET && c->read_ahead_limit >= pos - c->logical_pos)
266
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 || c->read_ahead_limit < 0) {
267 uint8_t tmp[32768];
268
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 while (c->logical_pos < pos || whence == SEEK_END) {
269 8 int size = sizeof(tmp);
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (whence == SEEK_SET)
271 size = FFMIN(sizeof(tmp), pos - c->logical_pos);
272 8 ret = cache_read(h, tmp, size);
273
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
8 if (ret == AVERROR_EOF && whence == SEEK_END) {
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(c->is_true_eof);
275 1 goto resolve_eof;
276 }
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0) {
278 return ret;
279 }
280 }
281 return c->logical_pos;
282 }
283 }
284
285 if (ret >= 0) {
286 c->logical_pos = ret;
287 c->end = FFMAX(c->end, ret);
288 }
289
290 return ret;
291 }
292
293 1 static int enu_free(void *opaque, void *elem)
294 {
295 1 av_free(elem);
296 1 return 0;
297 }
298
299 1 static int cache_close(URLContext *h)
300 {
301 1 CacheContext *c = h->priv_data;
302 int ret;
303
304 1 av_log(h, AV_LOG_INFO, "Statistics, cache hits:%"PRId64" cache misses:%"PRId64"\n",
305 c->cache_hit, c->cache_miss);
306
307 1 close(c->fd);
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (c->filename) {
309 ret = unlink(c->filename);
310 if (ret < 0)
311 av_log(h, AV_LOG_ERROR, "Could not delete %s.\n", c->filename);
312 av_freep(&c->filename);
313 }
314 1 ffurl_closep(&c->inner);
315 1 av_tree_enumerate(c->root, NULL, NULL, enu_free);
316 1 av_tree_destroy(c->root);
317
318 1 return 0;
319 }
320
321 #define OFFSET(x) offsetof(CacheContext, x)
322 #define D AV_OPT_FLAG_DECODING_PARAM
323
324 static const AVOption options[] = {
325 { "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 },
326 {NULL},
327 };
328
329 static const AVClass cache_context_class = {
330 .class_name = "cache",
331 .item_name = av_default_item_name,
332 .option = options,
333 .version = LIBAVUTIL_VERSION_INT,
334 };
335
336 const URLProtocol ff_cache_protocol = {
337 .name = "cache",
338 .url_open2 = cache_open,
339 .url_read = cache_read,
340 .url_seek = cache_seek,
341 .url_close = cache_close,
342 .priv_data_size = sizeof(CacheContext),
343 .priv_data_class = &cache_context_class,
344 };
345