Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 1 | /* |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 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" |
Anton Khirnov | c486dad | 2011-04-07 18:25:52 | [diff] [blame] | 23 | #include "url.h" |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 24 | |
Martin Storsjö | 1dee0ac | 2011-11-06 21:03:45 | [diff] [blame] | 25 | #include "libavutil/log.h" |
| 26 | |
Martin Storsjö | c86d8ae | 2015-02-27 23:06:28 | [diff] [blame] | 27 | extern const AVClass ff_avio_class; |
Martin Storsjö | 1dee0ac | 2011-11-06 21:03:45 | [diff] [blame] | 28 | |
Andreas Rheinhardt | 45bfe8b | 2021-08-04 14:52:07 | [diff] [blame] | 29 | typedef 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öm | 682bafd | 2021-10-17 21:35:48 | [diff] [blame] | 55 | * Bytes written statistic |
| 56 | */ |
| 57 | int64_t bytes_written; |
| 58 | |
| 59 | /** |
Andreas Rheinhardt | 45bfe8b | 2021-08-04 14:52:07 | [diff] [blame] | 60 | * 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öm | d39b58d | 2021-10-13 19:21:51 | [diff] [blame] | 74 | |
| 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 Rheinhardt | 45bfe8b | 2021-08-04 14:52:07 | [diff] [blame] | 80 | } FFIOContext; |
| 81 | |
| 82 | static av_always_inline FFIOContext *ffiocontext(AVIOContext *ctx) |
| 83 | { |
| 84 | return (FFIOContext*)ctx; |
| 85 | } |
| 86 | |
| 87 | void ffio_init_context(FFIOContext *s, |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 88 | 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 Khirnov | b3db9ce | 2011-02-21 18:28:16 | [diff] [blame] | 97 | /** |
Ben Avison | daf1e0d | 2013-07-31 22:46:08 | [diff] [blame] | 98 | * 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 Avison | 5afe1d2 | 2013-08-07 22:22:38 | [diff] [blame] | 112 | int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data); |
Ben Avison | daf1e0d | 2013-07-31 22:46:08 | [diff] [blame] | 113 | |
Andreas Rheinhardt | a46e78d | 2021-09-22 22:55:44 | [diff] [blame] | 114 | void ffio_fill(AVIOContext *s, int b, int64_t count); |
Anton Khirnov | 0ac8e2b | 2011-02-21 19:02:20 | [diff] [blame] | 115 | |
Ronald S. Bultje | fd085bc | 2011-03-04 13:33:49 | [diff] [blame] | 116 | static 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 Khirnov | 0abdb29 | 2011-02-24 06:36:02 | [diff] [blame] | 120 | |
Anton Khirnov | f1ef2cd | 2011-03-06 19:08:30 | [diff] [blame] | 121 | /** |
| 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 Niedermayer | d5ec8ba | 2013-09-15 13:25:09 | [diff] [blame] | 130 | * @return >= 0 in case of success, a negative value corresponding to an |
Anton Khirnov | f1ef2cd | 2011-03-06 19:08:30 | [diff] [blame] | 131 | * AVERROR code in case of failure |
| 132 | */ |
Michael Niedermayer | 120b38b | 2012-10-13 00:08:59 | [diff] [blame] | 133 | int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_size); |
Anton Khirnov | f1ef2cd | 2011-03-06 19:08:30 | [diff] [blame] | 134 | |
Anton Khirnov | 4839c19 | 2011-03-14 19:38:57 | [diff] [blame] | 135 | uint64_t ffio_read_varlen(AVIOContext *bc); |
| 136 | |
Vittorio Giovara | bff0349 | 2015-05-26 13:24:38 | [diff] [blame] | 137 | /** |
| 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 | */ |
| 142 | int ffio_read_size(AVIOContext *s, unsigned char *buf, int size); |
| 143 | |
Michael Niedermayer | 186ec17 | 2013-06-30 16:18:13 | [diff] [blame] | 144 | /** |
tomajsjiang | 3d1506c | 2019-07-04 03:58:41 | [diff] [blame] | 145 | * 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 | */ |
| 151 | int ffio_realloc_buf(AVIOContext *s, int buf_size); |
| 152 | |
| 153 | /** |
Michael Niedermayer | 186ec17 | 2013-06-30 16:18:13 | [diff] [blame] | 154 | * 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 Balint | da74a74 | 2020-09-28 21:48:34 | [diff] [blame] | 158 | * 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 Niedermayer | 186ec17 | 2013-06-30 16:18:13 | [diff] [blame] | 161 | */ |
Carl Eugen Hoyos | ff9a154 | 2014-07-30 09:09:25 | [diff] [blame] | 162 | int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size); |
Michael Niedermayer | 186ec17 | 2013-06-30 16:18:13 | [diff] [blame] | 163 | |
Michael Niedermayer | e39eeb1 | 2011-12-19 10:46:34 | [diff] [blame] | 164 | int ffio_limit(AVIOContext *s, int size); |
| 165 | |
Anton Khirnov | 4c4427a | 2011-03-17 11:56:25 | [diff] [blame] | 166 | void 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 Khirnov | b64030f | 2011-03-17 12:04:38 | [diff] [blame] | 169 | unsigned long ffio_get_checksum(AVIOContext *s); |
Anton Khirnov | d09cc22 | 2011-03-17 12:02:54 | [diff] [blame] | 170 | unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, |
| 171 | unsigned int len); |
James Almer | 155f4e9 | 2016-08-04 19:17:09 | [diff] [blame] | 172 | unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf, |
| 173 | unsigned int len); |
Alessandro Ghedini | 32d0593 | 2014-04-13 12:23:57 | [diff] [blame] | 174 | unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, |
| 175 | unsigned int len); |
Anton Khirnov | 933e90a | 2011-03-14 19:38:59 | [diff] [blame] | 176 | |
Anton Khirnov | 403ee83 | 2011-03-17 07:19:54 | [diff] [blame] | 177 | /** |
| 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 Biurrun | 523c7bd | 2012-12-07 15:07:51 | [diff] [blame] | 180 | * with a big-endian 4 byte header giving the packet size in bytes. |
Anton Khirnov | 403ee83 | 2011-03-17 07:19:54 | [diff] [blame] | 181 | * |
| 182 | * @param s new IO context |
| 183 | * @param max_packet_size maximum packet size (must be > 0) |
| 184 | * @return zero if no error. |
| 185 | */ |
| 186 | int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size); |
| 187 | |
Anton Khirnov | 724f6a0 | 2011-03-17 07:44:44 | [diff] [blame] | 188 | /** |
| 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 Niedermayer | d5ec8ba | 2013-09-15 13:25:09 | [diff] [blame] | 196 | * @return >= 0 in case of success, a negative value corresponding to an |
Anton Khirnov | 724f6a0 | 2011-03-17 07:44:44 | [diff] [blame] | 197 | * AVERROR code in case of failure |
| 198 | */ |
| 199 | int ffio_fdopen(AVIOContext **s, URLContext *h); |
| 200 | |
Martin Storsjö | 3627ce2 | 2013-09-18 18:50:50 | [diff] [blame] | 201 | /** |
Jeyapal, Karthick | 62f63b2 | 2017-11-29 06:31:34 | [diff] [blame] | 202 | * Return the URLContext associated with the AVIOContext |
| 203 | * |
| 204 | * @param s IO context |
| 205 | * @return pointer to URLContext or NULL. |
| 206 | */ |
| 207 | URLContext *ffio_geturlcontext(AVIOContext *s); |
| 208 | |
Pierre-Anthony Lemieux | c8b5f28 | 2021-12-15 00:35:14 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * Read url related dictionary options from the AVIOContext and write to the given dictionary |
| 212 | */ |
| 213 | int ffio_copy_url_options(AVIOContext* pb, AVDictionary** avio_opts); |
| 214 | |
Jeyapal, Karthick | 62f63b2 | 2017-11-29 06:31:34 | [diff] [blame] | 215 | /** |
Martin Storsjö | 3627ce2 | 2013-09-18 18:50:50 | [diff] [blame] | 216 | * 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 | */ |
| 223 | int ffio_open_null_buf(AVIOContext **s); |
| 224 | |
Michael Niedermayer | 1dba837 | 2016-01-30 01:17:50 | [diff] [blame] | 225 | int ffio_open_whitelist(AVIOContext **s, const char *url, int flags, |
| 226 | const AVIOInterruptCB *int_cb, AVDictionary **options, |
Derek Buitenhuis | 9362973 | 2016-03-03 17:14:26 | [diff] [blame] | 227 | const char *whitelist, const char *blacklist); |
Michael Niedermayer | 1dba837 | 2016-01-30 01:17:50 | [diff] [blame] | 228 | |
Martin Storsjö | 3627ce2 | 2013-09-18 18:50:50 | [diff] [blame] | 229 | /** |
| 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 | */ |
| 235 | int ffio_close_null_buf(AVIOContext *s); |
| 236 | |
Martin Storsjö | 8a273a7 | 2015-02-24 11:23:30 | [diff] [blame] | 237 | /** |
Andreas Rheinhardt | 639728f | 2019-11-26 06:53:52 | [diff] [blame] | 238 | * Reset a dynamic buffer. |
| 239 | * |
| 240 | * Resets everything, but keeps the allocated buffer for later use. |
| 241 | */ |
| 242 | void ffio_reset_dyn_buf(AVIOContext *s); |
| 243 | |
| 244 | /** |
Martin Storsjö | 8a273a7 | 2015-02-24 11:23:30 | [diff] [blame] | 245 | * Free a dynamic buffer. |
| 246 | * |
| 247 | * @param s a pointer to an IO context opened by avio_open_dyn_buf() |
| 248 | */ |
| 249 | void ffio_free_dyn_buf(AVIOContext **s); |
| 250 | |
Andreas Rheinhardt | fd101c9 | 2021-07-23 02:01:44 | [diff] [blame] | 251 | struct 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 | */ |
| 263 | int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp); |
| 264 | |
Jan Ekström | 151f46e | 2021-09-20 11:30:14 | [diff] [blame] | 265 | /** |
| 266 | * Read a whole null-terminated string of text from AVIOContext to an AVBPrint |
Jan Ekström | 847fd8d | 2021-09-20 11:51:42 | [diff] [blame] | 267 | * buffer overwriting its contents. Stop reading after reaching the maximum |
| 268 | * length, a \\0 or EOF. |
Jan Ekström | 151f46e | 2021-09-20 11:30:14 | [diff] [blame] | 269 | * |
| 270 | * @param s the read-only AVIOContext |
| 271 | * @param bp the AVBPrint buffer |
Jan Ekström | 847fd8d | 2021-09-20 11:51:42 | [diff] [blame] | 272 | * @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öm | 151f46e | 2021-09-20 11:30:14 | [diff] [blame] | 278 | * @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öm | 847fd8d | 2021-09-20 11:51:42 | [diff] [blame] | 281 | int64_t ff_read_string_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp, |
| 282 | int64_t max_len); |
Jan Ekström | 151f46e | 2021-09-20 11:30:14 | [diff] [blame] | 283 | |
Diego Biurrun | 153382e | 2011-05-17 14:58:04 | [diff] [blame] | 284 | #endif /* AVFORMAT_AVIO_INTERNAL_H */ |