FFmpeg coverage


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