1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * This file is part of FFmpeg.
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
5cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
6cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
7cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
8cabdff1aSopenharmony_ci *
9cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
10cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
11cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12cabdff1aSopenharmony_ci * Lesser General Public License for more details.
13cabdff1aSopenharmony_ci *
14cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
15cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
16cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17cabdff1aSopenharmony_ci */
18cabdff1aSopenharmony_ci
19cabdff1aSopenharmony_ci#ifndef AVFORMAT_AVIO_INTERNAL_H
20cabdff1aSopenharmony_ci#define AVFORMAT_AVIO_INTERNAL_H
21cabdff1aSopenharmony_ci
22cabdff1aSopenharmony_ci#include "avio.h"
23cabdff1aSopenharmony_ci#include "url.h"
24cabdff1aSopenharmony_ci
25cabdff1aSopenharmony_ci#include "libavutil/log.h"
26cabdff1aSopenharmony_ci
27cabdff1aSopenharmony_ciextern const AVClass ff_avio_class;
28cabdff1aSopenharmony_ci
29cabdff1aSopenharmony_citypedef struct FFIOContext {
30cabdff1aSopenharmony_ci    AVIOContext pub;
31cabdff1aSopenharmony_ci    /**
32cabdff1aSopenharmony_ci     * A callback that is used instead of short_seek_threshold.
33cabdff1aSopenharmony_ci     */
34cabdff1aSopenharmony_ci    int (*short_seek_get)(void *opaque);
35cabdff1aSopenharmony_ci
36cabdff1aSopenharmony_ci    /**
37cabdff1aSopenharmony_ci     * Threshold to favor readahead over seek.
38cabdff1aSopenharmony_ci     */
39cabdff1aSopenharmony_ci    int short_seek_threshold;
40cabdff1aSopenharmony_ci
41cabdff1aSopenharmony_ci    enum AVIODataMarkerType current_type;
42cabdff1aSopenharmony_ci    int64_t last_time;
43cabdff1aSopenharmony_ci
44cabdff1aSopenharmony_ci    /**
45cabdff1aSopenharmony_ci     * max filesize, used to limit allocations
46cabdff1aSopenharmony_ci     */
47cabdff1aSopenharmony_ci    int64_t maxsize;
48cabdff1aSopenharmony_ci
49cabdff1aSopenharmony_ci    /**
50cabdff1aSopenharmony_ci     * Bytes read statistic
51cabdff1aSopenharmony_ci     */
52cabdff1aSopenharmony_ci    int64_t bytes_read;
53cabdff1aSopenharmony_ci
54cabdff1aSopenharmony_ci    /**
55cabdff1aSopenharmony_ci     * Bytes written statistic
56cabdff1aSopenharmony_ci     */
57cabdff1aSopenharmony_ci    int64_t bytes_written;
58cabdff1aSopenharmony_ci
59cabdff1aSopenharmony_ci    /**
60cabdff1aSopenharmony_ci     * seek statistic
61cabdff1aSopenharmony_ci     */
62cabdff1aSopenharmony_ci    int seek_count;
63cabdff1aSopenharmony_ci
64cabdff1aSopenharmony_ci    /**
65cabdff1aSopenharmony_ci     * writeout statistic
66cabdff1aSopenharmony_ci     */
67cabdff1aSopenharmony_ci    int writeout_count;
68cabdff1aSopenharmony_ci
69cabdff1aSopenharmony_ci    /**
70cabdff1aSopenharmony_ci     * Original buffer size
71cabdff1aSopenharmony_ci     * used after probing to ensure seekback and to reset the buffer size
72cabdff1aSopenharmony_ci     */
73cabdff1aSopenharmony_ci    int orig_buffer_size;
74cabdff1aSopenharmony_ci
75cabdff1aSopenharmony_ci    /**
76cabdff1aSopenharmony_ci     * Written output size
77cabdff1aSopenharmony_ci     * is updated each time a successful writeout ends up further position-wise
78cabdff1aSopenharmony_ci     */
79cabdff1aSopenharmony_ci    int64_t written_output_size;
80cabdff1aSopenharmony_ci} FFIOContext;
81cabdff1aSopenharmony_ci
82cabdff1aSopenharmony_cistatic av_always_inline FFIOContext *ffiocontext(AVIOContext *ctx)
83cabdff1aSopenharmony_ci{
84cabdff1aSopenharmony_ci    return (FFIOContext*)ctx;
85cabdff1aSopenharmony_ci}
86cabdff1aSopenharmony_ci
87cabdff1aSopenharmony_civoid ffio_init_context(FFIOContext *s,
88cabdff1aSopenharmony_ci                  unsigned char *buffer,
89cabdff1aSopenharmony_ci                  int buffer_size,
90cabdff1aSopenharmony_ci                  int write_flag,
91cabdff1aSopenharmony_ci                  void *opaque,
92cabdff1aSopenharmony_ci                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
93cabdff1aSopenharmony_ci                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
94cabdff1aSopenharmony_ci                  int64_t (*seek)(void *opaque, int64_t offset, int whence));
95cabdff1aSopenharmony_ci
96cabdff1aSopenharmony_ci
97cabdff1aSopenharmony_ci/**
98cabdff1aSopenharmony_ci * Read size bytes from AVIOContext, returning a pointer.
99cabdff1aSopenharmony_ci * Note that the data pointed at by the returned pointer is only
100cabdff1aSopenharmony_ci * valid until the next call that references the same IO context.
101cabdff1aSopenharmony_ci * @param s IO context
102cabdff1aSopenharmony_ci * @param buf pointer to buffer into which to assemble the requested
103cabdff1aSopenharmony_ci *    data if it is not available in contiguous addresses in the
104cabdff1aSopenharmony_ci *    underlying buffer
105cabdff1aSopenharmony_ci * @param size number of bytes requested
106cabdff1aSopenharmony_ci * @param data address at which to store pointer: this will be a
107cabdff1aSopenharmony_ci *    a direct pointer into the underlying buffer if the requested
108cabdff1aSopenharmony_ci *    number of bytes are available at contiguous addresses, otherwise
109cabdff1aSopenharmony_ci *    will be a copy of buf
110cabdff1aSopenharmony_ci * @return number of bytes read or AVERROR
111cabdff1aSopenharmony_ci */
112cabdff1aSopenharmony_ciint ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data);
113cabdff1aSopenharmony_ci
114cabdff1aSopenharmony_civoid ffio_fill(AVIOContext *s, int b, int64_t count);
115cabdff1aSopenharmony_ci
116cabdff1aSopenharmony_cistatic av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
117cabdff1aSopenharmony_ci{
118cabdff1aSopenharmony_ci    avio_wl32(pb, MKTAG(s[0], s[1], s[2], s[3]));
119cabdff1aSopenharmony_ci}
120cabdff1aSopenharmony_ci
121cabdff1aSopenharmony_ci/**
122cabdff1aSopenharmony_ci * Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file.
123cabdff1aSopenharmony_ci * Used after probing to avoid seeking.
124cabdff1aSopenharmony_ci * Joins buf and s->buffer, taking any overlap into consideration.
125cabdff1aSopenharmony_ci * @note s->buffer must overlap with buf or they can't be joined and the function fails
126cabdff1aSopenharmony_ci *
127cabdff1aSopenharmony_ci * @param s The read-only AVIOContext to rewind
128cabdff1aSopenharmony_ci * @param buf The probe buffer containing the first buf_size bytes of the file
129cabdff1aSopenharmony_ci * @param buf_size The size of buf
130cabdff1aSopenharmony_ci * @return >= 0 in case of success, a negative value corresponding to an
131cabdff1aSopenharmony_ci * AVERROR code in case of failure
132cabdff1aSopenharmony_ci */
133cabdff1aSopenharmony_ciint ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_size);
134cabdff1aSopenharmony_ci
135cabdff1aSopenharmony_ciuint64_t ffio_read_varlen(AVIOContext *bc);
136cabdff1aSopenharmony_ci
137cabdff1aSopenharmony_ci/**
138cabdff1aSopenharmony_ci * Read size bytes from AVIOContext into buf.
139cabdff1aSopenharmony_ci * Check that exactly size bytes have been read.
140cabdff1aSopenharmony_ci * @return number of bytes read or AVERROR
141cabdff1aSopenharmony_ci */
142cabdff1aSopenharmony_ciint ffio_read_size(AVIOContext *s, unsigned char *buf, int size);
143cabdff1aSopenharmony_ci
144cabdff1aSopenharmony_ci/**
145cabdff1aSopenharmony_ci * Reallocate a given buffer for AVIOContext.
146cabdff1aSopenharmony_ci *
147cabdff1aSopenharmony_ci * @param s the AVIOContext to realloc.
148cabdff1aSopenharmony_ci * @param buf_size required new buffer size.
149cabdff1aSopenharmony_ci * @return 0 on success, a negative AVERROR on failure.
150cabdff1aSopenharmony_ci */
151cabdff1aSopenharmony_ciint ffio_realloc_buf(AVIOContext *s, int buf_size);
152cabdff1aSopenharmony_ci
153cabdff1aSopenharmony_ci/**
154cabdff1aSopenharmony_ci * Ensures that the requested seekback buffer size will be available
155cabdff1aSopenharmony_ci *
156cabdff1aSopenharmony_ci * Will ensure that when reading sequentially up to buf_size, seeking
157cabdff1aSopenharmony_ci * within the current pos and pos+buf_size is possible.
158cabdff1aSopenharmony_ci * Once the stream position moves outside this window or another
159cabdff1aSopenharmony_ci * ffio_ensure_seekback call requests a buffer outside this window this
160cabdff1aSopenharmony_ci * guarantee is lost.
161cabdff1aSopenharmony_ci */
162cabdff1aSopenharmony_ciint ffio_ensure_seekback(AVIOContext *s, int64_t buf_size);
163cabdff1aSopenharmony_ci
164cabdff1aSopenharmony_ciint ffio_limit(AVIOContext *s, int size);
165cabdff1aSopenharmony_ci
166cabdff1aSopenharmony_civoid ffio_init_checksum(AVIOContext *s,
167cabdff1aSopenharmony_ci                        unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
168cabdff1aSopenharmony_ci                        unsigned long checksum);
169cabdff1aSopenharmony_ciunsigned long ffio_get_checksum(AVIOContext *s);
170cabdff1aSopenharmony_ciunsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
171cabdff1aSopenharmony_ci                                    unsigned int len);
172cabdff1aSopenharmony_ciunsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
173cabdff1aSopenharmony_ci                                    unsigned int len);
174cabdff1aSopenharmony_ciunsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
175cabdff1aSopenharmony_ci                                unsigned int len);
176cabdff1aSopenharmony_ci
177cabdff1aSopenharmony_ci/**
178cabdff1aSopenharmony_ci * Open a write only packetized memory stream with a maximum packet
179cabdff1aSopenharmony_ci * size of 'max_packet_size'.  The stream is stored in a memory buffer
180cabdff1aSopenharmony_ci * with a big-endian 4 byte header giving the packet size in bytes.
181cabdff1aSopenharmony_ci *
182cabdff1aSopenharmony_ci * @param s new IO context
183cabdff1aSopenharmony_ci * @param max_packet_size maximum packet size (must be > 0)
184cabdff1aSopenharmony_ci * @return zero if no error.
185cabdff1aSopenharmony_ci */
186cabdff1aSopenharmony_ciint ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size);
187cabdff1aSopenharmony_ci
188cabdff1aSopenharmony_ci/**
189cabdff1aSopenharmony_ci * Create and initialize a AVIOContext for accessing the
190cabdff1aSopenharmony_ci * resource referenced by the URLContext h.
191cabdff1aSopenharmony_ci * @note When the URLContext h has been opened in read+write mode, the
192cabdff1aSopenharmony_ci * AVIOContext can be used only for writing.
193cabdff1aSopenharmony_ci *
194cabdff1aSopenharmony_ci * @param s Used to return the pointer to the created AVIOContext.
195cabdff1aSopenharmony_ci * In case of failure the pointed to value is set to NULL.
196cabdff1aSopenharmony_ci * @return >= 0 in case of success, a negative value corresponding to an
197cabdff1aSopenharmony_ci * AVERROR code in case of failure
198cabdff1aSopenharmony_ci */
199cabdff1aSopenharmony_ciint ffio_fdopen(AVIOContext **s, URLContext *h);
200cabdff1aSopenharmony_ci
201cabdff1aSopenharmony_ci/**
202cabdff1aSopenharmony_ci * Return the URLContext associated with the AVIOContext
203cabdff1aSopenharmony_ci *
204cabdff1aSopenharmony_ci * @param s IO context
205cabdff1aSopenharmony_ci * @return pointer to URLContext or NULL.
206cabdff1aSopenharmony_ci */
207cabdff1aSopenharmony_ciURLContext *ffio_geturlcontext(AVIOContext *s);
208cabdff1aSopenharmony_ci
209cabdff1aSopenharmony_ci
210cabdff1aSopenharmony_ci/**
211cabdff1aSopenharmony_ci * Read url related dictionary options from the AVIOContext and write to the given dictionary
212cabdff1aSopenharmony_ci */
213cabdff1aSopenharmony_ciint ffio_copy_url_options(AVIOContext* pb, AVDictionary** avio_opts);
214cabdff1aSopenharmony_ci
215cabdff1aSopenharmony_ci/**
216cabdff1aSopenharmony_ci * Open a write-only fake memory stream. The written data is not stored
217cabdff1aSopenharmony_ci * anywhere - this is only used for measuring the amount of data
218cabdff1aSopenharmony_ci * written.
219cabdff1aSopenharmony_ci *
220cabdff1aSopenharmony_ci * @param s new IO context
221cabdff1aSopenharmony_ci * @return zero if no error.
222cabdff1aSopenharmony_ci */
223cabdff1aSopenharmony_ciint ffio_open_null_buf(AVIOContext **s);
224cabdff1aSopenharmony_ci
225cabdff1aSopenharmony_ciint ffio_open_whitelist(AVIOContext **s, const char *url, int flags,
226cabdff1aSopenharmony_ci                         const AVIOInterruptCB *int_cb, AVDictionary **options,
227cabdff1aSopenharmony_ci                         const char *whitelist, const char *blacklist);
228cabdff1aSopenharmony_ci
229cabdff1aSopenharmony_ci/**
230cabdff1aSopenharmony_ci * Close a null buffer.
231cabdff1aSopenharmony_ci *
232cabdff1aSopenharmony_ci * @param s an IO context opened by ffio_open_null_buf
233cabdff1aSopenharmony_ci * @return the number of bytes written to the null buffer
234cabdff1aSopenharmony_ci */
235cabdff1aSopenharmony_ciint ffio_close_null_buf(AVIOContext *s);
236cabdff1aSopenharmony_ci
237cabdff1aSopenharmony_ci/**
238cabdff1aSopenharmony_ci * Reset a dynamic buffer.
239cabdff1aSopenharmony_ci *
240cabdff1aSopenharmony_ci * Resets everything, but keeps the allocated buffer for later use.
241cabdff1aSopenharmony_ci */
242cabdff1aSopenharmony_civoid ffio_reset_dyn_buf(AVIOContext *s);
243cabdff1aSopenharmony_ci
244cabdff1aSopenharmony_ci/**
245cabdff1aSopenharmony_ci * Free a dynamic buffer.
246cabdff1aSopenharmony_ci *
247cabdff1aSopenharmony_ci * @param s a pointer to an IO context opened by avio_open_dyn_buf()
248cabdff1aSopenharmony_ci */
249cabdff1aSopenharmony_civoid ffio_free_dyn_buf(AVIOContext **s);
250cabdff1aSopenharmony_ci
251cabdff1aSopenharmony_cistruct AVBPrint;
252cabdff1aSopenharmony_ci/**
253cabdff1aSopenharmony_ci * Read a whole line of text from AVIOContext to an AVBPrint buffer overwriting
254cabdff1aSopenharmony_ci * its contents. Stop reading after reaching a \\r, a \\n, a \\r\\n, a \\0 or
255cabdff1aSopenharmony_ci * EOF. The line ending characters are NOT included in the buffer, but they
256cabdff1aSopenharmony_ci * are skipped on the input.
257cabdff1aSopenharmony_ci *
258cabdff1aSopenharmony_ci * @param s the read-only AVIOContext
259cabdff1aSopenharmony_ci * @param bp the AVBPrint buffer
260cabdff1aSopenharmony_ci * @return the length of the read line not including the line endings,
261cabdff1aSopenharmony_ci *         negative on error, or if the buffer becomes truncated.
262cabdff1aSopenharmony_ci */
263cabdff1aSopenharmony_ciint64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp);
264cabdff1aSopenharmony_ci
265cabdff1aSopenharmony_ci/**
266cabdff1aSopenharmony_ci * Read a whole null-terminated string of text from AVIOContext to an AVBPrint
267cabdff1aSopenharmony_ci * buffer overwriting its contents. Stop reading after reaching the maximum
268cabdff1aSopenharmony_ci * length, a \\0 or EOF.
269cabdff1aSopenharmony_ci *
270cabdff1aSopenharmony_ci * @param s the read-only AVIOContext
271cabdff1aSopenharmony_ci * @param bp the AVBPrint buffer
272cabdff1aSopenharmony_ci * @param max_len the maximum length to be read from the AVIOContext.
273cabdff1aSopenharmony_ci *                Negative (< 0) values signal that there is no known maximum
274cabdff1aSopenharmony_ci *                length applicable. A maximum length of zero means that the
275cabdff1aSopenharmony_ci *                AVIOContext is not touched, and the function returns
276cabdff1aSopenharmony_ci *                with a read length of zero. In all cases the AVBprint
277cabdff1aSopenharmony_ci *                is cleared.
278cabdff1aSopenharmony_ci * @return the length of the read string not including the terminating null,
279cabdff1aSopenharmony_ci *         negative on error, or if the buffer becomes truncated.
280cabdff1aSopenharmony_ci */
281cabdff1aSopenharmony_ciint64_t ff_read_string_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp,
282cabdff1aSopenharmony_ci                                           int64_t max_len);
283cabdff1aSopenharmony_ci
284cabdff1aSopenharmony_ci#endif /* AVFORMAT_AVIO_INTERNAL_H */
285