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 
24 #include "libavutil/log.h"
25 
26 extern const AVClass ff_avio_class;
27 
28 typedef struct FFIOContext {
30  /**
31  * A callback that is used instead of short_seek_threshold.
32  */
33  int (*short_seek_get)(void *opaque);
34 
35  /**
36  * Threshold to favor readahead over seek.
37  */
39 
41  int64_t last_time;
42 
43  /**
44  * max filesize, used to limit allocations
45  */
46  int64_t maxsize;
47 
48  /**
49  * Bytes read statistic
50  */
51  int64_t bytes_read;
52 
53  /**
54  * Bytes written statistic
55  */
56  int64_t bytes_written;
57 
58  /**
59  * seek statistic
60  */
62 
63  /**
64  * writeout statistic
65  */
67 
68  /**
69  * Original buffer size
70  * used after probing to ensure seekback and to reset the buffer size
71  */
73 
74  /**
75  * Written output size
76  * is updated each time a successful writeout ends up further position-wise
77  */
79 } FFIOContext;
80 
82 {
83  return (FFIOContext*)ctx;
84 }
85 
87  unsigned char *buffer,
88  int buffer_size,
89  int write_flag,
90  void *opaque,
91  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
93  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
94 #else
95  int (*write_packet)(void *opaque, const uint8_t *buf, int buf_size),
96 #endif
97  int64_t (*seek)(void *opaque, int64_t offset, int whence));
98 
99 /**
100  * Wrap a buffer in an AVIOContext for reading.
101  */
102 void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size);
103 
104 /**
105  * Wrap a buffer in an AVIOContext for writing.
106  */
107 void ffio_init_write_context(FFIOContext *s, uint8_t *buffer, int buffer_size);
108 
109 /**
110  * Read size bytes from AVIOContext, returning a pointer.
111  * Note that the data pointed at by the returned pointer is only
112  * valid until the next call that references the same IO context.
113  * @param s IO context
114  * @param buf pointer to buffer into which to assemble the requested
115  * data if it is not available in contiguous addresses in the
116  * underlying buffer
117  * @param size number of bytes requested
118  * @param data address at which to store pointer: this will be a
119  * a direct pointer into the underlying buffer if the requested
120  * number of bytes are available at contiguous addresses, otherwise
121  * will be a copy of buf
122  * @return number of bytes read or AVERROR
123  */
124 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data);
125 
126 void ffio_fill(AVIOContext *s, int b, int64_t count);
127 
128 static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
129 {
130  avio_wl32(pb, MKTAG(s[0], s[1], s[2], s[3]));
131 }
132 
133 /**
134  * Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file.
135  * Used after probing to avoid seeking.
136  * Joins buf and s->buffer, taking any overlap into consideration.
137  * @note s->buffer must overlap with buf or they can't be joined and the function fails
138  *
139  * @param s The read-only AVIOContext to rewind
140  * @param buf The probe buffer containing the first buf_size bytes of the file
141  * @param buf_size The size of buf
142  * @return >= 0 in case of success, a negative value corresponding to an
143  * AVERROR code in case of failure
144  */
145 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_size);
146 
147 uint64_t ffio_read_varlen(AVIOContext *bc);
148 
149 /**
150  * Read a unsigned integer coded as a variable number of up to eight
151  * little-endian bytes, where the MSB in a byte signals another byte
152  * must be read.
153  * All coded bytes are read, but values > UINT_MAX are truncated.
154  */
155 unsigned int ffio_read_leb(AVIOContext *s);
156 
157 void ffio_write_leb(AVIOContext *s, unsigned val);
158 
159 /**
160  * Write a sequence of text lines, converting line endings.
161  * All input line endings (LF, CRLF, CR) are converted to the configured line ending.
162  * @param s The AVIOContext to write to
163  * @param buf The buffer to write
164  * @param size The size of the buffer, or <0 to use the full length of a null-terminated string
165  * @param ending The line ending sequence to convert to, or NULL for \n
166  */
167 void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size,
168  const unsigned char *ending);
169 
170 /**
171  * Read size bytes from AVIOContext into buf.
172  * Check that exactly size bytes have been read.
173  * @return number of bytes read or AVERROR
174  */
175 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size);
176 
177 /**
178  * Reallocate a given buffer for AVIOContext.
179  *
180  * @param s the AVIOContext to realloc.
181  * @param buf_size required new buffer size.
182  * @return 0 on success, a negative AVERROR on failure.
183  */
184 int ffio_realloc_buf(AVIOContext *s, int buf_size);
185 
186 /**
187  * Ensures that the requested seekback buffer size will be available
188  *
189  * Will ensure that when reading sequentially up to buf_size, seeking
190  * within the current pos and pos+buf_size is possible.
191  * Once the stream position moves outside this window or another
192  * ffio_ensure_seekback call requests a buffer outside this window this
193  * guarantee is lost.
194  */
195 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size);
196 
197 int ffio_limit(AVIOContext *s, int size);
198 
200  unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
201  unsigned long checksum);
202 unsigned long ffio_get_checksum(AVIOContext *s);
203 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
204  unsigned int len);
205 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
206  unsigned int len);
207 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
208  unsigned int len);
209 
210 /**
211  * Open a write only packetized memory stream with a maximum packet
212  * size of 'max_packet_size'. The stream is stored in a memory buffer
213  * with a big-endian 4 byte header giving the packet size in bytes.
214  *
215  * @param s new IO context
216  * @param max_packet_size maximum packet size (must be > 0)
217  * @return zero if no error.
218  */
219 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size);
220 
221 /**
222  * Return the URLContext associated with the AVIOContext
223  *
224  * @param s IO context
225  * @return pointer to URLContext or NULL.
226  */
228 
229 /**
230  * Create and initialize a AVIOContext for accessing the
231  * resource referenced by the URLContext h.
232  * @note When the URLContext h has been opened in read+write mode, the
233  * AVIOContext can be used only for writing.
234  *
235  * @param s Used to return the pointer to the created AVIOContext.
236  * In case of failure the pointed to value is set to NULL.
237  * @return >= 0 in case of success, a negative value corresponding to an
238  * AVERROR code in case of failure
239  */
240 int ffio_fdopen(AVIOContext **s, struct URLContext *h);
241 
242 
243 /**
244  * Read url related dictionary options from the AVIOContext and write to the given dictionary
245  */
246 int ffio_copy_url_options(AVIOContext* pb, AVDictionary** avio_opts);
247 
248 /**
249  * Open a write-only fake memory stream. The written data is not stored
250  * anywhere - this is only used for measuring the amount of data
251  * written.
252  *
253  * @param s new IO context
254  * @return zero if no error.
255  */
257 
258 int ffio_open_whitelist(AVIOContext **s, const char *url, int flags,
260  const char *whitelist, const char *blacklist);
261 
262 /**
263  * Close a null buffer.
264  *
265  * @param s an IO context opened by ffio_open_null_buf
266  * @return the number of bytes written to the null buffer
267  */
269 
270 /**
271  * Reset a dynamic buffer.
272  *
273  * Resets everything, but keeps the allocated buffer for later use.
274  */
276 
277 /**
278  * Free a dynamic buffer.
279  *
280  * @param s a pointer to an IO context opened by avio_open_dyn_buf()
281  */
283 
284 struct AVBPrint;
285 /**
286  * Read a whole line of text from AVIOContext to an AVBPrint buffer overwriting
287  * its contents. Stop reading after reaching a \\r, a \\n, a \\r\\n, a \\0 or
288  * EOF. The line ending characters are NOT included in the buffer, but they
289  * are skipped on the input.
290  *
291  * @param s the read-only AVIOContext
292  * @param bp the AVBPrint buffer
293  * @return the length of the read line not including the line endings,
294  * negative on error, or if the buffer becomes truncated.
295  */
296 int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp);
297 
298 /**
299  * Read a whole null-terminated string of text from AVIOContext to an AVBPrint
300  * buffer overwriting its contents. Stop reading after reaching the maximum
301  * length, a \\0 or EOF.
302  *
303  * @param s the read-only AVIOContext
304  * @param bp the AVBPrint buffer
305  * @param max_len the maximum length to be read from the AVIOContext.
306  * Negative (< 0) values signal that there is no known maximum
307  * length applicable. A maximum length of zero means that the
308  * AVIOContext is not touched, and the function returns
309  * with a read length of zero. In all cases the AVBprint
310  * is cleared.
311  * @return the length of the read string not including the terminating null,
312  * negative on error, or if the buffer becomes truncated.
313  */
314 int64_t ff_read_string_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp,
315  int64_t max_len);
316 
317 #endif /* AVFORMAT_AVIO_INTERNAL_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, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:81
ffio_copy_url_options
int ffio_copy_url_options(AVIOContext *pb, AVDictionary **avio_opts)
Read url related dictionary options from the AVIOContext and write to the given dictionary.
Definition: aviobuf.c:1103
ffio_realloc_buf
int ffio_realloc_buf(AVIOContext *s, int buf_size)
Reallocate a given buffer for AVIOContext.
Definition: aviobuf.c:1215
ffio_wfourcc
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:128
ffiocontext
static av_always_inline FFIOContext * ffiocontext(AVIOContext *ctx)
Definition: avio_internal.h:81
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:719
FFIOContext::written_output_size
int64_t written_output_size
Written output size is updated each time a successful writeout ends up further position-wise.
Definition: avio_internal.h:78
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:902
b
#define b
Definition: input.c:41
data
const char data[16]
Definition: mxf.c:148
FFIOContext::short_seek_get
int(* short_seek_get)(void *opaque)
A callback that is used instead of short_seek_threshold.
Definition: avio_internal.h:33
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:1308
ff_avio_class
const AVClass ff_avio_class
Definition: aviobuf.c:67
AVDictionary
Definition: dict.c:34
ffio_init_read_context
void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for reading.
Definition: aviobuf.c:134
AVIODataMarkerType
AVIODataMarkerType
Different data types that can be returned via the AVIO write_data_type callback.
Definition: avio.h:116
FFIOContext::short_seek_threshold
int short_seek_threshold
Threshold to favor readahead over seek.
Definition: avio_internal.h:38
FFIOContext
Definition: avio_internal.h:28
ffio_get_checksum
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:630
update_checksum
static void update_checksum(AVIOContext *s)
Definition: aviobuf.c:1127
AVIOInterruptCB
Callback for checking whether to abort blocking functions.
Definition: avio.h:59
ffio_fdopen
int ffio_fdopen(AVIOContext **s, struct URLContext *h)
Create and initialize a AVIOContext for accessing the resource referenced by the URLContext h.
Definition: aviobuf.c:1038
ffio_open_null_buf
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1670
val
static double val(void *priv, double ch)
Definition: aeval.c:78
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
FFIOContext::bytes_read
int64_t bytes_read
Bytes read statistic.
Definition: avio_internal.h:51
FFIOContext::seek_count
int seek_count
seek statistic
Definition: avio_internal.h:61
s
#define s(width, name)
Definition: cbs_vp9.c:198
ffio_read_leb
unsigned int ffio_read_leb(AVIOContext *s)
Read a unsigned integer coded as a variable number of up to eight little-endian bytes,...
Definition: aviobuf.c:974
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ffio_write_leb
void ffio_write_leb(AVIOContext *s, unsigned val)
Definition: aviobuf.c:991
ff_crcEDB88320_update
unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:618
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:1680
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:72
seek
static void BS_FUNC() seek(BSCTX *bc, unsigned pos)
Seek to the given bit position.
Definition: bitstream_template.h:399
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:234
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:46
options
const OptionDef options[]
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
ffio_reset_dyn_buf
void ffio_reset_dyn_buf(AVIOContext *s)
Reset a dynamic buffer.
Definition: aviobuf.c:1605
FFIOContext::writeout_count
int writeout_count
writeout statistic
Definition: avio_internal.h:66
FFIOContext::pub
AVIOContext pub
Definition: avio_internal.h:29
ffio_geturlcontext
struct URLContext * ffio_geturlcontext(AVIOContext *s)
Return the URLContext associated with the AVIOContext.
Definition: aviobuf.c:1092
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:638
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:1576
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:404
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:1135
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:963
ff_crc04C11DB7_update
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:612
ffio_limit
int ffio_limit(AVIOContext *s, int size)
Definition: aviobuf.c:1173
URLContext
Definition: url.h:37
log.h
av_always_inline
#define av_always_inline
Definition: attributes.h:49
ff_crcA001_update
unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:624
len
int len
Definition: vorbis_enc_data.h:426
int_cb
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:324
write_packet
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition: ffmpeg_mux.c:209
ff_read_string_to_bprint_overwrite
int64_t ff_read_string_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp, int64_t max_len)
Read a whole null-terminated string of text from AVIOContext to an AVBPrint buffer overwriting its co...
Definition: aviobuf.c:907
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1644
FFIOContext::current_type
enum AVIODataMarkerType current_type
Definition: avio_internal.h:40
ffio_write_lines
void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size, const unsigned char *ending)
Write a sequence of text lines, converting line endings.
Definition: aviobuf.c:1007
ffio_init_write_context
void ffio_init_write_context(FFIOContext *s, uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for writing.
Definition: aviobuf.c:139
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:1260
FFIOContext::last_time
int64_t last_time
Definition: avio_internal.h:41
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
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:709
h
h
Definition: vp9dsp_template.c:2038
FF_API_AVIO_WRITE_NONCONST
#define FF_API_AVIO_WRITE_NONCONST
Definition: version_major.h:48
int
int
Definition: ffmpeg_filter.c:424
FFIOContext::bytes_written
int64_t bytes_written
Bytes written statistic.
Definition: avio_internal.h:56