FFmpeg
avio_internal.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVFORMAT_AVIO_INTERNAL_H
20 #define AVFORMAT_AVIO_INTERNAL_H
21 
22 #include "avio.h"
23 #include "url.h"
24 
25 #include "libavutil/log.h"
26 
27 extern const AVClass ff_avio_class;
28 
29 typedef struct FFIOContext {
31  /**
32  * A callback that is used instead of short_seek_threshold.
33  */
34  int (*short_seek_get)(void *opaque);
35 
36  /**
37  * Threshold to favor readahead over seek.
38  */
40 
42  int64_t last_time;
43 
44  /**
45  * max filesize, used to limit allocations
46  */
47  int64_t maxsize;
48 
49  /**
50  * Bytes read statistic
51  */
52  int64_t bytes_read;
53 
54  /**
55  * seek statistic
56  */
58 
59  /**
60  * writeout statistic
61  */
63 
64  /**
65  * Original buffer size
66  * used after probing to ensure seekback and to reset the buffer size
67  */
69 } FFIOContext;
70 
72 {
73  return (FFIOContext*)ctx;
74 }
75 
77  unsigned char *buffer,
78  int buffer_size,
79  int write_flag,
80  void *opaque,
81  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
82  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
83  int64_t (*seek)(void *opaque, int64_t offset, int whence));
84 
85 
86 /**
87  * Read size bytes from AVIOContext, returning a pointer.
88  * Note that the data pointed at by the returned pointer is only
89  * valid until the next call that references the same IO context.
90  * @param s IO context
91  * @param buf pointer to buffer into which to assemble the requested
92  * data if it is not available in contiguous addresses in the
93  * underlying buffer
94  * @param size number of bytes requested
95  * @param data address at which to store pointer: this will be a
96  * a direct pointer into the underlying buffer if the requested
97  * number of bytes are available at contiguous addresses, otherwise
98  * will be a copy of buf
99  * @return number of bytes read or AVERROR
100  */
101 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data);
102 
103 void ffio_fill(AVIOContext *s, int b, int64_t count);
104 
105 static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
106 {
107  avio_wl32(pb, MKTAG(s[0], s[1], s[2], s[3]));
108 }
109 
110 /**
111  * Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file.
112  * Used after probing to avoid seeking.
113  * Joins buf and s->buffer, taking any overlap into consideration.
114  * @note s->buffer must overlap with buf or they can't be joined and the function fails
115  *
116  * @param s The read-only AVIOContext to rewind
117  * @param buf The probe buffer containing the first buf_size bytes of the file
118  * @param buf_size The size of buf
119  * @return >= 0 in case of success, a negative value corresponding to an
120  * AVERROR code in case of failure
121  */
122 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_size);
123 
124 uint64_t ffio_read_varlen(AVIOContext *bc);
125 
126 /**
127  * Read size bytes from AVIOContext into buf.
128  * Check that exactly size bytes have been read.
129  * @return number of bytes read or AVERROR
130  */
131 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size);
132 
133 /**
134  * Reallocate a given buffer for AVIOContext.
135  *
136  * @param s the AVIOContext to realloc.
137  * @param buf_size required new buffer size.
138  * @return 0 on success, a negative AVERROR on failure.
139  */
140 int ffio_realloc_buf(AVIOContext *s, int buf_size);
141 
142 /**
143  * Ensures that the requested seekback buffer size will be available
144  *
145  * Will ensure that when reading sequentially up to buf_size, seeking
146  * within the current pos and pos+buf_size is possible.
147  * Once the stream position moves outside this window or another
148  * ffio_ensure_seekback call requests a buffer outside this window this
149  * guarantee is lost.
150  */
151 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size);
152 
153 int ffio_limit(AVIOContext *s, int size);
154 
156  unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
157  unsigned long checksum);
158 unsigned long ffio_get_checksum(AVIOContext *s);
159 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
160  unsigned int len);
161 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
162  unsigned int len);
163 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
164  unsigned int len);
165 
166 /**
167  * Open a write only packetized memory stream with a maximum packet
168  * size of 'max_packet_size'. The stream is stored in a memory buffer
169  * with a big-endian 4 byte header giving the packet size in bytes.
170  *
171  * @param s new IO context
172  * @param max_packet_size maximum packet size (must be > 0)
173  * @return zero if no error.
174  */
175 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size);
176 
177 /**
178  * Create and initialize a AVIOContext for accessing the
179  * resource referenced by the URLContext h.
180  * @note When the URLContext h has been opened in read+write mode, the
181  * AVIOContext can be used only for writing.
182  *
183  * @param s Used to return the pointer to the created AVIOContext.
184  * In case of failure the pointed to value is set to NULL.
185  * @return >= 0 in case of success, a negative value corresponding to an
186  * AVERROR code in case of failure
187  */
189 
190 /**
191  * Return the URLContext associated with the AVIOContext
192  *
193  * @param s IO context
194  * @return pointer to URLContext or NULL.
195  */
197 
198 /**
199  * Open a write-only fake memory stream. The written data is not stored
200  * anywhere - this is only used for measuring the amount of data
201  * written.
202  *
203  * @param s new IO context
204  * @return zero if no error.
205  */
207 
208 int ffio_open_whitelist(AVIOContext **s, const char *url, int flags,
210  const char *whitelist, const char *blacklist);
211 
212 /**
213  * Close a null buffer.
214  *
215  * @param s an IO context opened by ffio_open_null_buf
216  * @return the number of bytes written to the null buffer
217  */
219 
220 /**
221  * Reset a dynamic buffer.
222  *
223  * Resets everything, but keeps the allocated buffer for later use.
224  */
226 
227 /**
228  * Free a dynamic buffer.
229  *
230  * @param s a pointer to an IO context opened by avio_open_dyn_buf()
231  */
233 
234 struct AVBPrint;
235 /**
236  * Read a whole line of text from AVIOContext to an AVBPrint buffer overwriting
237  * its contents. Stop reading after reaching a \\r, a \\n, a \\r\\n, a \\0 or
238  * EOF. The line ending characters are NOT included in the buffer, but they
239  * are skipped on the input.
240  *
241  * @param s the read-only AVIOContext
242  * @param bp the AVBPrint buffer
243  * @return the length of the read line not including the line endings,
244  * negative on error, or if the buffer becomes truncated.
245  */
246 int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp);
247 
248 #endif /* AVFORMAT_AVIO_INTERNAL_H */
ffio_realloc_buf
int ffio_realloc_buf(AVIOContext *s, int buf_size)
Reallocate a given buffer for AVIOContext.
Definition: aviobuf.c:1067
ffio_wfourcc
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:105
ffiocontext
static av_always_inline FFIOContext * ffiocontext(AVIOContext *ctx)
Definition: avio_internal.h:71
ffio_read_indirect
int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
Read size bytes from AVIOContext, returning a pointer.
Definition: aviobuf.c:684
ff_read_line_to_bprint_overwrite
int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp)
Read a whole line of text from AVIOContext to an AVBPrint buffer overwriting its contents.
Definition: aviobuf.c:837
b
#define b
Definition: input.c:40
data
const char data[16]
Definition: mxf.c:143
FFIOContext::short_seek_get
int(* short_seek_get)(void *opaque)
A callback that is used instead of short_seek_threshold.
Definition: avio_internal.h:34
ffio_open_whitelist
int ffio_open_whitelist(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist)
Definition: aviobuf.c:1160
ff_avio_class
const AVClass ff_avio_class
Definition: aviobuf.c:66
AVDictionary
Definition: dict.c:30
AVIODataMarkerType
AVIODataMarkerType
Different data types that can be returned via the AVIO write_data_type callback.
Definition: avio.h:111
FFIOContext::short_seek_threshold
int short_seek_threshold
Threshold to favor readahead over seek.
Definition: avio_internal.h:39
FFIOContext
Definition: avio_internal.h:29
ffio_get_checksum
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:596
update_checksum
static void update_checksum(AVIOContext *s)
Definition: aviobuf.c:982
AVIOInterruptCB
Callback for checking whether to abort blocking functions.
Definition: avio.h:58
ffio_open_null_buf
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1494
FFIOContext::bytes_read
int64_t bytes_read
Bytes read statistic.
Definition: avio_internal.h:52
FFIOContext::seek_count
int seek_count
seek statistic
Definition: avio_internal.h:57
s
#define s(width, name)
Definition: cbs_vp9.c:257
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_crcEDB88320_update
unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:584
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ffio_close_null_buf
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1504
FFIOContext::orig_buffer_size
int orig_buffer_size
Original buffer size used after probing to ensure seekback and to reset the buffer size.
Definition: avio_internal.h:68
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:204
c
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
Definition: undefined.txt:32
FFIOContext::maxsize
int64_t maxsize
max filesize, used to limit allocations
Definition: avio_internal.h:47
options
const OptionDef options[]
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
ffio_reset_dyn_buf
void ffio_reset_dyn_buf(AVIOContext *s)
Reset a dynamic buffer.
Definition: aviobuf.c:1433
FFIOContext::writeout_count
int writeout_count
writeout statistic
Definition: avio_internal.h:62
FFIOContext::pub
AVIOContext pub
Definition: avio_internal.h:30
size
int size
Definition: twinvq_data.h:10344
avio.h
ffio_init_checksum
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
Definition: aviobuf.c:604
ffio_open_dyn_packet_buf
int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
Open a write only packetized memory stream with a maximum packet size of 'max_packet_size'.
Definition: aviobuf.c:1404
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:371
ffio_ensure_seekback
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:990
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
ffio_read_varlen
uint64_t ffio_read_varlen(AVIOContext *bc)
Definition: aviobuf.c:902
ff_crc04C11DB7_update
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:578
ffio_limit
int ffio_limit(AVIOContext *s, int size)
Definition: aviobuf.c:1025
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:731
ffio_fdopen
int ffio_fdopen(AVIOContext **s, URLContext *h)
Create and initialize a AVIOContext for accessing the resource referenced by the URLContext h.
Definition: aviobuf.c:913
URLContext
Definition: url.h:38
log.h
ffio_init_context
void ffio_init_context(FFIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:80
av_always_inline
#define av_always_inline
Definition: attributes.h:49
url.h
ff_crcA001_update
unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:590
len
int len
Definition: vorbis_enc_data.h:426
int_cb
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:513
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1472
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
FFIOContext::current_type
enum AVIODataMarkerType current_type
Definition: avio_internal.h:41
checksum
static volatile int checksum
Definition: adler32.c:30
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
ffio_rewind_with_probe_data
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_size)
Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file.
Definition: aviobuf.c:1112
FFIOContext::last_time
int64_t last_time
Definition: avio_internal.h:42
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:674
h
h
Definition: vp9dsp_template.c:2038
int
int
Definition: ffmpeg_filter.c:156
ffio_geturlcontext
URLContext * ffio_geturlcontext(AVIOContext *s)
Return the URLContext associated with the AVIOContext.
Definition: aviobuf.c:971