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