• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
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     /**
55      * Bytes written statistic
56      */
57     int64_t bytes_written;
58 
59     /**
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;
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;
80 } FFIOContext;
81 
ffiocontext(AVIOContext * ctx)82 static av_always_inline FFIOContext *ffiocontext(AVIOContext *ctx)
83 {
84     return (FFIOContext*)ctx;
85 }
86 
87 void ffio_init_context(FFIOContext *s,
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 
97 /**
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  */
112 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data);
113 
114 void ffio_fill(AVIOContext *s, int b, int64_t count);
115 
ffio_wfourcc(AVIOContext * pb,const uint8_t * s)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 }
120 
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
130  * @return >= 0 in case of success, a negative value corresponding to an
131  * AVERROR code in case of failure
132  */
133 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_size);
134 
135 uint64_t ffio_read_varlen(AVIOContext *bc);
136 
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 
144 /**
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 /**
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.
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.
161  */
162 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size);
163 
164 int ffio_limit(AVIOContext *s, int size);
165 
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);
169 unsigned long ffio_get_checksum(AVIOContext *s);
170 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
171                                     unsigned int len);
172 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
173                                     unsigned int len);
174 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
175                                 unsigned int len);
176 
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
180  * with a big-endian 4 byte header giving the packet size in bytes.
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 
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.
196  * @return >= 0 in case of success, a negative value corresponding to an
197  * AVERROR code in case of failure
198  */
199 int ffio_fdopen(AVIOContext **s, URLContext *h);
200 
201 /**
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 
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 
215 /**
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 
225 int ffio_open_whitelist(AVIOContext **s, const char *url, int flags,
226                          const AVIOInterruptCB *int_cb, AVDictionary **options,
227                          const char *whitelist, const char *blacklist);
228 
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 
237 /**
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 /**
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 
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 
265 /**
266  * Read a whole null-terminated string of text from AVIOContext to an AVBPrint
267  * buffer overwriting its contents. Stop reading after reaching the maximum
268  * length, a \\0 or EOF.
269  *
270  * @param s the read-only AVIOContext
271  * @param bp the AVBPrint buffer
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.
278  * @return the length of the read string not including the terminating null,
279  *         negative on error, or if the buffer becomes truncated.
280  */
281 int64_t ff_read_string_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp,
282                                            int64_t max_len);
283 
284 #endif /* AVFORMAT_AVIO_INTERNAL_H */
285