FFmpeg: libavformat/cache.c Source File
Go to the documentation of this file. 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
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>
48
54
69
70 static int cmp(
const void *
key,
const void *node)
71 {
73 }
74
76 {
79 char *buffername;
80
82
87 }
88
89 ret = unlink(buffername);
90
93 else
94 c->filename = buffername;
95
97 options,
h->protocol_whitelist,
h->protocol_blacklist,
h);
98 }
99
101 {
108
109 //FIXME avoid lseek
110 pos = lseek(
c->fd, 0, SEEK_END);
115 }
117
123 }
125
127
130
132 entry->logical_pos +
entry->size !=
c->logical_pos ||
134 ) {
137 if (!
entry || !node) {
140 }
141 entry->logical_pos =
c->logical_pos;
144
146 if (entry_ret && entry_ret !=
entry) {
150 }
151 } else
153
154 return 0;
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
161 }
162
164 {
168
170
173
177 if (in_block_pos < entry->
size) {
178 int64_t physical_target =
entry->physical_pos + in_block_pos;
179
180 if (
c->cache_pos != physical_target) {
181 r = lseek(
c->fd, physical_target, SEEK_SET);
182 } else
184
188 }
189
195 }
196 }
197 }
198
199 // Cache miss or some kind of fault with the cache
200
201 if (
c->logical_pos !=
c->inner_pos) {
206 }
208 }
209
214 }
218
220
223 c->end =
FFMAX(
c->end,
c->logical_pos);
224
226 }
227
229 {
232
239 }
244 }
245
246 if (whence == SEEK_CUR) {
247 whence = SEEK_SET;
248 pos +=
c->logical_pos;
249 }
else if (whence == SEEK_END &&
c->is_true_eof) {
250 resolve_eof:
251 whence = SEEK_SET;
253 }
254
255 if (whence == SEEK_SET &&
pos >= 0 && pos < c->end) {
256 //Seems within filesize, assume it will not fail.
257 c->logical_pos =
pos;
259 }
260
261 //cache miss
263 if ((whence == SEEK_SET &&
pos >=
c->logical_pos ||
264 whence == SEEK_END &&
pos <= 0) &&
ret < 0) {
265 if ( (whence == SEEK_SET &&
c->read_ahead_limit >=
pos -
c->logical_pos)
266 ||
c->read_ahead_limit < 0) {
268 while (
c->logical_pos <
pos || whence == SEEK_END) {
270 if (whence == SEEK_SET)
275 goto resolve_eof;
276 }
279 }
280 }
281 return c->logical_pos;
282 }
283 }
284
286 c->logical_pos =
ret;
288 }
289
291 }
292
294 {
296 return 0;
297 }
298
300 {
303
305 c->cache_hit,
c->cache_miss);
306
309 ret = unlink(
c->filename);
313 }
317
318 return 0;
319 }
320
321 #define OFFSET(x) offsetof(CacheContext, x)
322 #define D AV_OPT_FLAG_DECODING_PARAM
323
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 },
327 };
328
334 };
335
344 };
static int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h.
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
#define AVERROR_EOF
End of file.
void * av_tree_insert(AVTreeNode **tp, void *key, int(*cmp)(const void *key, const void *b), AVTreeNode **next)
Insert or remove an element.
static int cache_read(URLContext *h, unsigned char *buf, int size)
static int cmp(const void *key, const void *node)
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
const URLProtocol ff_cache_protocol
struct AVTreeNode * av_tree_node_alloc(void)
Allocate an AVTreeNode.
void av_tree_enumerate(AVTreeNode *t, void *opaque, int(*cmp)(void *opaque, void *elem), int(*enu)(void *opaque, void *elem))
Apply enu(opaque, &elem) to all the elements in the tree in a given range.
static int cache_close(URLContext *h)
static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
#define FFDIFFSIGN(x, y)
Comparator.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int cache_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
static const AVOption options[]
const char * av_default_item_name(void *ptr)
Return the context name.
void av_tree_destroy(AVTreeNode *t)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static int add_entry(URLContext *h, const unsigned char *buf, int size)
int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
Wrapper to work around the lack of mkstemp() on mingw.
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
#define AV_LOG_INFO
Standard information.
static int enu_free(void *opaque, void *elem)
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
@ AV_OPT_TYPE_INT
Underlying C type is int.
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(const void *key, const void *b), void *next[2])
static const AVClass cache_context_class
@TODO support keeping files support filling with a background thread
#define flags(name, subs,...)
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Generated on Fri Aug 22 2025 13:59:26 for FFmpeg by
doxygen
1.8.17