blob: 1f5e3d474b880b6146086dcc33da59eba332d49c [file] [log] [blame]
Anton Khirnove731b8d2011-02-20 10:04:131/*
Anton Khirnove731b8d2011-02-20 10:04:132 * 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"
Anton Khirnovc486dad2011-04-07 18:25:5223#include "url.h"
Anton Khirnove731b8d2011-02-20 10:04:1324
Martin Storsjö1dee0ac2011-11-06 21:03:4525#include "libavutil/log.h"
26
Martin Storsjöc86d8ae2015-02-27 23:06:2827extern const AVClass ff_avio_class;
Martin Storsjö1dee0ac2011-11-06 21:03:4528
Andreas Rheinhardt45bfe8b2021-08-04 14:52:0729typedef struct FFIOContext {
30 AVIOContext pub;
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 */
39 int short_seek_threshold;
40
41 enum AVIODataMarkerType current_type;
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 /**
Jan Ekström682bafd2021-10-17 21:35:4855 * Bytes written statistic
56 */
57 int64_t bytes_written;
58
59 /**
Andreas Rheinhardt45bfe8b2021-08-04 14:52:0760 * seek statistic
61 */
62 int seek_count;
63
64 /**
65 * writeout statistic
66 */
67 int writeout_count;
68
69 /**
70 * Original buffer size
71 * used after probing to ensure seekback and to reset the buffer size
72 */
73 int orig_buffer_size;
Jan Ekströmd39b58d2021-10-13 19:21:5174
75 /**
76 * Written output size
77 * is updated each time a successful writeout ends up further position-wise
78 */
79 int64_t written_output_size;
Andreas Rheinhardt45bfe8b2021-08-04 14:52:0780} FFIOContext;
81
82static av_always_inline FFIOContext *ffiocontext(AVIOContext *ctx)
83{
84 return (FFIOContext*)ctx;
85}
86
87void ffio_init_context(FFIOContext *s,
Anton Khirnove731b8d2011-02-20 10:04:1388 unsigned char *buffer,
89 int buffer_size,
90 int write_flag,
91 void *opaque,
92 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
93 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
94 int64_t (*seek)(void *opaque, int64_t offset, int whence));
95
96
Anton Khirnovb3db9ce2011-02-21 18:28:1697/**
Ben Avisondaf1e0d2013-07-31 22:46:0898 * Read size bytes from AVIOContext, returning a pointer.
99 * Note that the data pointed at by the returned pointer is only
100 * valid until the next call that references the same IO context.
101 * @param s IO context
102 * @param buf pointer to buffer into which to assemble the requested
103 * data if it is not available in contiguous addresses in the
104 * underlying buffer
105 * @param size number of bytes requested
106 * @param data address at which to store pointer: this will be a
107 * a direct pointer into the underlying buffer if the requested
108 * number of bytes are available at contiguous addresses, otherwise
109 * will be a copy of buf
110 * @return number of bytes read or AVERROR
111 */
Ben Avison5afe1d22013-08-07 22:22:38112int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data);
Ben Avisondaf1e0d2013-07-31 22:46:08113
Andreas Rheinhardta46e78d2021-09-22 22:55:44114void ffio_fill(AVIOContext *s, int b, int64_t count);
Anton Khirnov0ac8e2b2011-02-21 19:02:20115
Ronald S. Bultjefd085bc2011-03-04 13:33:49116static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
117{
118 avio_wl32(pb, MKTAG(s[0], s[1], s[2], s[3]));
119}
Anton Khirnov0abdb292011-02-24 06:36:02120
Anton Khirnovf1ef2cd2011-03-06 19:08:30121/**
122 * Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file.
123 * Used after probing to avoid seeking.
124 * Joins buf and s->buffer, taking any overlap into consideration.
125 * @note s->buffer must overlap with buf or they can't be joined and the function fails
126 *
127 * @param s The read-only AVIOContext to rewind
128 * @param buf The probe buffer containing the first buf_size bytes of the file
129 * @param buf_size The size of buf
Michael Niedermayerd5ec8ba2013-09-15 13:25:09130 * @return >= 0 in case of success, a negative value corresponding to an
Anton Khirnovf1ef2cd2011-03-06 19:08:30131 * AVERROR code in case of failure
132 */
Michael Niedermayer120b38b2012-10-13 00:08:59133int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_size);
Anton Khirnovf1ef2cd2011-03-06 19:08:30134
Anton Khirnov4839c192011-03-14 19:38:57135uint64_t ffio_read_varlen(AVIOContext *bc);
136
Vittorio Giovarabff03492015-05-26 13:24:38137/**
138 * Read size bytes from AVIOContext into buf.
139 * Check that exactly size bytes have been read.
140 * @return number of bytes read or AVERROR
141 */
142int ffio_read_size(AVIOContext *s, unsigned char *buf, int size);
143
Michael Niedermayer186ec172013-06-30 16:18:13144/**
tomajsjiang3d1506c2019-07-04 03:58:41145 * Reallocate a given buffer for AVIOContext.
146 *
147 * @param s the AVIOContext to realloc.
148 * @param buf_size required new buffer size.
149 * @return 0 on success, a negative AVERROR on failure.
150 */
151int ffio_realloc_buf(AVIOContext *s, int buf_size);
152
153/**
Michael Niedermayer186ec172013-06-30 16:18:13154 * Ensures that the requested seekback buffer size will be available
155 *
156 * Will ensure that when reading sequentially up to buf_size, seeking
157 * within the current pos and pos+buf_size is possible.
Marton Balintda74a742020-09-28 21:48:34158 * Once the stream position moves outside this window or another
159 * ffio_ensure_seekback call requests a buffer outside this window this
160 * guarantee is lost.
Michael Niedermayer186ec172013-06-30 16:18:13161 */
Carl Eugen Hoyosff9a1542014-07-30 09:09:25162int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size);
Michael Niedermayer186ec172013-06-30 16:18:13163
Michael Niedermayere39eeb12011-12-19 10:46:34164int ffio_limit(AVIOContext *s, int size);
165
Anton Khirnov4c4427a2011-03-17 11:56:25166void ffio_init_checksum(AVIOContext *s,
167 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
168 unsigned long checksum);
Anton Khirnovb64030f2011-03-17 12:04:38169unsigned long ffio_get_checksum(AVIOContext *s);
Anton Khirnovd09cc222011-03-17 12:02:54170unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
171 unsigned int len);
James Almer155f4e92016-08-04 19:17:09172unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
173 unsigned int len);
Alessandro Ghedini32d05932014-04-13 12:23:57174unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
175 unsigned int len);
Anton Khirnov933e90a2011-03-14 19:38:59176
Anton Khirnov403ee832011-03-17 07:19:54177/**
178 * Open a write only packetized memory stream with a maximum packet
179 * size of 'max_packet_size'. The stream is stored in a memory buffer
Diego Biurrun523c7bd2012-12-07 15:07:51180 * with a big-endian 4 byte header giving the packet size in bytes.
Anton Khirnov403ee832011-03-17 07:19:54181 *
182 * @param s new IO context
183 * @param max_packet_size maximum packet size (must be > 0)
184 * @return zero if no error.
185 */
186int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size);
187
Anton Khirnov724f6a02011-03-17 07:44:44188/**
189 * Create and initialize a AVIOContext for accessing the
190 * resource referenced by the URLContext h.
191 * @note When the URLContext h has been opened in read+write mode, the
192 * AVIOContext can be used only for writing.
193 *
194 * @param s Used to return the pointer to the created AVIOContext.
195 * In case of failure the pointed to value is set to NULL.
Michael Niedermayerd5ec8ba2013-09-15 13:25:09196 * @return >= 0 in case of success, a negative value corresponding to an
Anton Khirnov724f6a02011-03-17 07:44:44197 * AVERROR code in case of failure
198 */
199int ffio_fdopen(AVIOContext **s, URLContext *h);
200
Martin Storsjö3627ce22013-09-18 18:50:50201/**
Jeyapal, Karthick62f63b22017-11-29 06:31:34202 * Return the URLContext associated with the AVIOContext
203 *
204 * @param s IO context
205 * @return pointer to URLContext or NULL.
206 */
207URLContext *ffio_geturlcontext(AVIOContext *s);
208
Pierre-Anthony Lemieuxc8b5f282021-12-15 00:35:14209
210/**
211 * Read url related dictionary options from the AVIOContext and write to the given dictionary
212 */
213int ffio_copy_url_options(AVIOContext* pb, AVDictionary** avio_opts);
214
Jeyapal, Karthick62f63b22017-11-29 06:31:34215/**
Martin Storsjö3627ce22013-09-18 18:50:50216 * Open a write-only fake memory stream. The written data is not stored
217 * anywhere - this is only used for measuring the amount of data
218 * written.
219 *
220 * @param s new IO context
221 * @return zero if no error.
222 */
223int ffio_open_null_buf(AVIOContext **s);
224
Michael Niedermayer1dba8372016-01-30 01:17:50225int ffio_open_whitelist(AVIOContext **s, const char *url, int flags,
226 const AVIOInterruptCB *int_cb, AVDictionary **options,
Derek Buitenhuis93629732016-03-03 17:14:26227 const char *whitelist, const char *blacklist);
Michael Niedermayer1dba8372016-01-30 01:17:50228
Martin Storsjö3627ce22013-09-18 18:50:50229/**
230 * Close a null buffer.
231 *
232 * @param s an IO context opened by ffio_open_null_buf
233 * @return the number of bytes written to the null buffer
234 */
235int ffio_close_null_buf(AVIOContext *s);
236
Martin Storsjö8a273a72015-02-24 11:23:30237/**
Andreas Rheinhardt639728f2019-11-26 06:53:52238 * Reset a dynamic buffer.
239 *
240 * Resets everything, but keeps the allocated buffer for later use.
241 */
242void ffio_reset_dyn_buf(AVIOContext *s);
243
244/**
Martin Storsjö8a273a72015-02-24 11:23:30245 * Free a dynamic buffer.
246 *
247 * @param s a pointer to an IO context opened by avio_open_dyn_buf()
248 */
249void ffio_free_dyn_buf(AVIOContext **s);
250
Andreas Rheinhardtfd101c92021-07-23 02:01:44251struct AVBPrint;
252/**
253 * Read a whole line of text from AVIOContext to an AVBPrint buffer overwriting
254 * its contents. Stop reading after reaching a \\r, a \\n, a \\r\\n, a \\0 or
255 * EOF. The line ending characters are NOT included in the buffer, but they
256 * are skipped on the input.
257 *
258 * @param s the read-only AVIOContext
259 * @param bp the AVBPrint buffer
260 * @return the length of the read line not including the line endings,
261 * negative on error, or if the buffer becomes truncated.
262 */
263int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp);
264
Jan Ekström151f46e2021-09-20 11:30:14265/**
266 * Read a whole null-terminated string of text from AVIOContext to an AVBPrint
Jan Ekström847fd8d2021-09-20 11:51:42267 * buffer overwriting its contents. Stop reading after reaching the maximum
268 * length, a \\0 or EOF.
Jan Ekström151f46e2021-09-20 11:30:14269 *
270 * @param s the read-only AVIOContext
271 * @param bp the AVBPrint buffer
Jan Ekström847fd8d2021-09-20 11:51:42272 * @param max_len the maximum length to be read from the AVIOContext.
273 * Negative (< 0) values signal that there is no known maximum
274 * length applicable. A maximum length of zero means that the
275 * AVIOContext is not touched, and the function returns
276 * with a read length of zero. In all cases the AVBprint
277 * is cleared.
Jan Ekström151f46e2021-09-20 11:30:14278 * @return the length of the read string not including the terminating null,
279 * negative on error, or if the buffer becomes truncated.
280 */
Jan Ekström847fd8d2021-09-20 11:51:42281int64_t ff_read_string_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp,
282 int64_t max_len);
Jan Ekström151f46e2021-09-20 11:30:14283
Diego Biurrun153382e2011-05-17 14:58:04284#endif /* AVFORMAT_AVIO_INTERNAL_H */