2 * copyright (c) 2001 Fabrice Bellard
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifndef AVFORMAT_AVIO_H
21 #define AVFORMAT_AVIO_H
25 * unbuffered I/O operations
27 * @warning This file has to be considered an internal but installed
28 * header, so it should not be directly included in your projects.
33 #include "libavutil/common.h"
34 #include "libavutil/log.h"
36 #include "libavformat/version.h"
42 * New fields can be added to the end with minor version bumps.
43 * Removal, reordering and changes to existing fields require a major
45 * sizeof(URLContext) must not be used outside libav*.
47 typedef struct URLContext {
49 const AVClass *av_class; ///< information for av_log(). Set by url_open().
51 struct URLProtocol *prot;
53 int is_streamed; /**< true if streamed (no seek possible), default = false */
54 int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
56 char *filename; /**< specified URL */
60 typedef struct URLPollEntry {
67 * @defgroup open_modes URL open modes
68 * The flags argument to url_open and cosins must be one of the following
69 * constants, optionally ORed with other flags.
72 #define URL_RDONLY 0 /**< read-only */
73 #define URL_WRONLY 1 /**< write-only */
74 #define URL_RDWR 2 /**< read-write */
80 * Use non-blocking mode.
81 * If this flag is set, operations on the context will return
82 * AVERROR(EAGAIN) if they can not be performed immediately.
83 * If this flag is not set, operations on the context will never return
85 * Note that this flag does not affect the opening/connecting of the
86 * context. Connecting a protocol will always block if necessary (e.g. on
87 * network protocols) but never hang (e.g. on busy devices).
88 * Warning: non-blocking protocols is work-in-progress; this flag may be
91 #define URL_FLAG_NONBLOCK 4
93 typedef int URLInterruptCB(void);
96 * Create a URLContext for accessing to the resource indicated by
97 * url, and open it using the URLProtocol up.
99 * @param puc pointer to the location where, in case of success, the
100 * function puts the pointer to the created URLContext
101 * @param flags flags which control how the resource indicated by url
103 * @return 0 in case of success, a negative value corresponding to an
104 * AVERROR code in case of failure
106 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
107 const char *url, int flags);
110 * Create a URLContext for accessing to the resource indicated by
111 * url, but do not initiate the connection yet.
113 * @param puc pointer to the location where, in case of success, the
114 * function puts the pointer to the created URLContext
115 * @param flags flags which control how the resource indicated by url
117 * @return 0 in case of success, a negative value corresponding to an
118 * AVERROR code in case of failure
120 int url_alloc(URLContext **h, const char *url, int flags);
123 * Connect an URLContext that has been allocated by url_alloc
125 int url_connect(URLContext *h);
128 * Create an URLContext for accessing to the resource indicated by
131 * @param puc pointer to the location where, in case of success, the
132 * function puts the pointer to the created URLContext
133 * @param flags flags which control how the resource indicated by url
135 * @return 0 in case of success, a negative value corresponding to an
136 * AVERROR code in case of failure
138 int url_open(URLContext **h, const char *url, int flags);
141 * Read up to size bytes from the resource accessed by h, and store
142 * the read bytes in buf.
144 * @return The number of bytes actually read, or a negative value
145 * corresponding to an AVERROR code in case of error. A value of zero
146 * indicates that it is not possible to read more from the accessed
147 * resource (except if the value of the size argument is also zero).
149 int url_read(URLContext *h, unsigned char *buf, int size);
152 * Read as many bytes as possible (up to size), calling the
153 * read function multiple times if necessary.
154 * This makes special short-read handling in applications
155 * unnecessary, if the return value is < size then it is
156 * certain there was either an error or the end of file was reached.
158 int url_read_complete(URLContext *h, unsigned char *buf, int size);
161 * Write size bytes from buf to the resource accessed by h.
163 * @return the number of bytes actually written, or a negative value
164 * corresponding to an AVERROR code in case of failure
166 int url_write(URLContext *h, const unsigned char *buf, int size);
169 * Passing this as the "whence" parameter to a seek function causes it to
170 * return the filesize without seeking anywhere. Supporting this is optional.
171 * If it is not supported then the seek function will return <0.
173 #define AVSEEK_SIZE 0x10000
176 * Change the position that will be used by the next read/write
177 * operation on the resource accessed by h.
179 * @param pos specifies the new position to set
180 * @param whence specifies how pos should be interpreted, it must be
181 * one of SEEK_SET (seek from the beginning), SEEK_CUR (seek from the
182 * current position), SEEK_END (seek from the end), or AVSEEK_SIZE
183 * (return the filesize of the requested resource, pos is ignored).
184 * @return a negative value corresponding to an AVERROR code in case
185 * of failure, or the resulting file position, measured in bytes from
186 * the beginning of the file. You can use this feature together with
187 * SEEK_CUR to read the current file position.
189 int64_t url_seek(URLContext *h, int64_t pos, int whence);
192 * Close the resource accessed by the URLContext h, and free the
195 * @return a negative value if an error condition occurred, 0
198 int url_close(URLContext *h);
201 * Return a non-zero value if the resource indicated by url
202 * exists, 0 otherwise.
204 int url_exist(const char *url);
207 * Return the filesize of the resource accessed by h, AVERROR(ENOSYS)
208 * if the operation is not supported by h, or another negative value
209 * corresponding to an AVERROR error code in case of failure.
211 int64_t url_filesize(URLContext *h);
214 * Return the file descriptor associated with this URL. For RTP, this
215 * will return only the RTP file descriptor, not the RTCP file descriptor.
217 * @return the file descriptor associated with this URL, or <0 on error.
219 int url_get_file_handle(URLContext *h);
222 * Return the maximum packet size associated to packetized file
223 * handle. If the file is not packetized (stream like HTTP or file on
224 * disk), then 0 is returned.
226 * @param h file handle
227 * @return maximum packet size in bytes
229 int url_get_max_packet_size(URLContext *h);
232 * Copy the filename of the resource accessed by h to buf.
234 * @param buf_size size in bytes of buf
236 void url_get_filename(URLContext *h, char *buf, int buf_size);
239 * The callback is called in blocking functions to test regulary if
240 * asynchronous interruption is needed. AVERROR(EINTR) is returned
241 * in this case by the interrupted function. 'NULL' means no interrupt
244 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
246 /* not implemented */
247 int url_poll(URLPollEntry *poll_table, int n, int timeout);
250 * Pause and resume playing - only meaningful if using a network streaming
251 * protocol (e.g. MMS).
252 * @param pause 1 for pause, 0 for resume
254 int av_url_read_pause(URLContext *h, int pause);
257 * Seek to a given timestamp relative to some component stream.
258 * Only meaningful if using a network streaming protocol (e.g. MMS.).
259 * @param stream_index The stream index that the timestamp is relative to.
260 * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
261 * units from the beginning of the presentation.
262 * If a stream_index >= 0 is used and the protocol does not support
263 * seeking based on component streams, the call will fail with ENOTSUP.
264 * @param timestamp timestamp in AVStream.time_base units
265 * or if there is no stream specified then in AV_TIME_BASE units.
266 * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
267 * and AVSEEK_FLAG_ANY. The protocol may silently ignore
268 * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
269 * fail with ENOTSUP if used and not supported.
270 * @return >= 0 on success
271 * @see AVInputFormat::read_seek
273 int64_t av_url_read_seek(URLContext *h, int stream_index,
274 int64_t timestamp, int flags);
277 * Oring this flag as into the "whence" parameter to a seek function causes it to
278 * seek by any means (like reopening and linear reading) or other normally unreasonble
279 * means that can be extreemly slow.
280 * This may be ignored by the seek code.
282 #define AVSEEK_FORCE 0x20000
284 typedef struct URLProtocol {
286 int (*url_open)(URLContext *h, const char *url, int flags);
287 int (*url_read)(URLContext *h, unsigned char *buf, int size);
288 int (*url_write)(URLContext *h, const unsigned char *buf, int size);
289 int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
290 int (*url_close)(URLContext *h);
291 struct URLProtocol *next;
292 int (*url_read_pause)(URLContext *h, int pause);
293 int64_t (*url_read_seek)(URLContext *h, int stream_index,
294 int64_t timestamp, int flags);
295 int (*url_get_file_handle)(URLContext *h);
297 const AVClass *priv_data_class;
300 #if FF_API_REGISTER_PROTOCOL
301 extern URLProtocol *first_protocol;
304 extern URLInterruptCB *url_interrupt_cb;
307 * If protocol is NULL, returns the first registered protocol,
308 * if protocol is non-NULL, returns the next registered protocol after protocol,
309 * or NULL if protocol is the last one.
311 URLProtocol *av_protocol_next(URLProtocol *p);
313 #if FF_API_REGISTER_PROTOCOL
315 * @deprecated Use av_register_protocol() instead.
317 attribute_deprecated int register_protocol(URLProtocol *protocol);
320 * @deprecated Use av_register_protocol2() instead.
322 attribute_deprecated int av_register_protocol(URLProtocol *protocol);
326 * Register the URLProtocol protocol.
328 * @param size the size of the URLProtocol struct referenced
330 int av_register_protocol2(URLProtocol *protocol, int size);
333 * Bytestream IO Context.
334 * New fields can be added to the end with minor version bumps.
335 * Removal, reordering and changes to existing fields require a major
337 * sizeof(AVIOContext) must not be used outside libav*.
340 unsigned char *buffer;
342 unsigned char *buf_ptr, *buf_end;
344 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
345 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
346 int64_t (*seek)(void *opaque, int64_t offset, int whence);
347 int64_t pos; /**< position in the file of the current buffer */
348 int must_flush; /**< true if the next seek should flush */
349 int eof_reached; /**< true if eof reached */
350 int write_flag; /**< true if open for writing */
353 unsigned long checksum;
354 unsigned char *checksum_ptr;
355 unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
356 int error; ///< contains the error code or 0 if no error happened
357 int (*read_pause)(void *opaque, int pause);
358 int64_t (*read_seek)(void *opaque, int stream_index,
359 int64_t timestamp, int flags);
363 typedef attribute_deprecated AVIOContext ByteIOContext;
366 int init_put_byte(AVIOContext *s,
367 unsigned char *buffer,
371 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
372 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
373 int64_t (*seek)(void *opaque, int64_t offset, int whence));
374 AVIOContext *av_alloc_put_byte(
375 unsigned char *buffer,
379 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
380 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
381 int64_t (*seek)(void *opaque, int64_t offset, int whence));
383 void put_byte(AVIOContext *s, int b);
384 void put_nbyte(AVIOContext *s, int b, int count);
385 void put_buffer(AVIOContext *s, const unsigned char *buf, int size);
386 void put_le64(AVIOContext *s, uint64_t val);
387 void put_be64(AVIOContext *s, uint64_t val);
388 void put_le32(AVIOContext *s, unsigned int val);
389 void put_be32(AVIOContext *s, unsigned int val);
390 void put_le24(AVIOContext *s, unsigned int val);
391 void put_be24(AVIOContext *s, unsigned int val);
392 void put_le16(AVIOContext *s, unsigned int val);
393 void put_be16(AVIOContext *s, unsigned int val);
394 void put_tag(AVIOContext *s, const char *tag);
397 attribute_deprecated void put_strz(AVIOContext *s, const char *buf);
401 * Write a NULL-terminated string.
402 * @return number of bytes written.
404 int avio_put_str(AVIOContext *s, const char *str);
407 * Convert an UTF-8 string to UTF-16LE and write it.
408 * @return number of bytes written.
410 int avio_put_str16le(AVIOContext *s, const char *str);
413 * fseek() equivalent for AVIOContext.
414 * @return new position or AVERROR.
416 int64_t url_fseek(AVIOContext *s, int64_t offset, int whence);
419 * Skip given number of bytes forward.
420 * @param offset number of bytes
421 * @return 0 on success, <0 on error
423 int url_fskip(AVIOContext *s, int64_t offset);
426 * ftell() equivalent for AVIOContext.
427 * @return position or AVERROR.
429 int64_t url_ftell(AVIOContext *s);
433 * @return filesize or AVERROR
435 int64_t url_fsize(AVIOContext *s);
438 * feof() equivalent for AVIOContext.
439 * @return non zero if and only if end of file
441 int url_feof(AVIOContext *s);
443 int url_ferror(AVIOContext *s);
445 int av_url_read_fpause(AVIOContext *h, int pause);
446 int64_t av_url_read_fseek(AVIOContext *h, int stream_index,
447 int64_t timestamp, int flags);
450 /** @note return URL_EOF (-1) if EOF */
451 int url_fgetc(AVIOContext *s);
453 /** @warning currently size is limited */
455 int url_fprintf(AVIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
457 int url_fprintf(AVIOContext *s, const char *fmt, ...);
460 /** @note unlike fgets, the EOL character is not returned and a whole
461 line is parsed. return NULL if first char read was EOF */
462 char *url_fgets(AVIOContext *s, char *buf, int buf_size);
464 void put_flush_packet(AVIOContext *s);
468 * Read size bytes from AVIOContext into buf.
469 * @return number of bytes read or AVERROR
471 int get_buffer(AVIOContext *s, unsigned char *buf, int size);
474 * Read size bytes from AVIOContext into buf.
475 * This reads at most 1 packet. If that is not enough fewer bytes will be
477 * @return number of bytes read or AVERROR
479 int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size);
481 /** @note return 0 if EOF, so you cannot use it if EOF handling is
483 int get_byte(AVIOContext *s);
484 unsigned int get_le24(AVIOContext *s);
485 unsigned int get_le32(AVIOContext *s);
486 uint64_t get_le64(AVIOContext *s);
487 unsigned int get_le16(AVIOContext *s);
490 * Read a UTF-16 string from pb and convert it to UTF-8.
491 * The reading will terminate when either a null or invalid character was
492 * encountered or maxlen bytes have been read.
493 * @return number of bytes read (is always <= maxlen)
495 int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen);
496 int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen);
498 char *get_strz(AVIOContext *s, char *buf, int maxlen);
499 unsigned int get_be16(AVIOContext *s);
500 unsigned int get_be24(AVIOContext *s);
501 unsigned int get_be32(AVIOContext *s);
502 uint64_t get_be64(AVIOContext *s);
504 uint64_t ff_get_v(AVIOContext *bc);
506 static inline int url_is_streamed(AVIOContext *s)
508 return s->is_streamed;
512 * Create and initialize a AVIOContext for accessing the
513 * resource referenced by the URLContext h.
514 * @note When the URLContext h has been opened in read+write mode, the
515 * AVIOContext can be used only for writing.
517 * @param s Used to return the pointer to the created AVIOContext.
518 * In case of failure the pointed to value is set to NULL.
519 * @return 0 in case of success, a negative value corresponding to an
520 * AVERROR code in case of failure
522 int url_fdopen(AVIOContext **s, URLContext *h);
524 /** @warning must be called before any I/O */
525 int url_setbufsize(AVIOContext *s, int buf_size);
526 #if FF_API_URL_RESETBUF
527 /** Reset the buffer for reading or writing.
528 * @note Will drop any data currently in the buffer without transmitting it.
529 * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
530 * to set up the buffer for writing. */
531 int url_resetbuf(AVIOContext *s, int flags);
535 * Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file.
536 * Used after probing to avoid seeking.
537 * Joins buf and s->buffer, taking any overlap into consideration.
538 * @note s->buffer must overlap with buf or they can't be joined and the function fails
539 * @note This function is NOT part of the public API
541 * @param s The read-only AVIOContext to rewind
542 * @param buf The probe buffer containing the first buf_size bytes of the file
543 * @param buf_size The size of buf
544 * @return 0 in case of success, a negative value corresponding to an
545 * AVERROR code in case of failure
547 int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size);
550 * Create and initialize a AVIOContext for accessing the
551 * resource indicated by url.
552 * @note When the resource indicated by url has been opened in
553 * read+write mode, the AVIOContext can be used only for writing.
555 * @param s Used to return the pointer to the created AVIOContext.
556 * In case of failure the pointed to value is set to NULL.
557 * @param flags flags which control how the resource indicated by url
559 * @return 0 in case of success, a negative value corresponding to an
560 * AVERROR code in case of failure
562 int url_fopen(AVIOContext **s, const char *url, int flags);
564 int url_fclose(AVIOContext *s);
565 URLContext *url_fileno(AVIOContext *s);
568 * Return the maximum packet size associated to packetized buffered file
569 * handle. If the file is not packetized (stream like http or file on
570 * disk), then 0 is returned.
572 * @param s buffered file handle
573 * @return maximum packet size in bytes
575 int url_fget_max_packet_size(AVIOContext *s);
577 int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags);
579 /** return the written or read size */
580 int url_close_buf(AVIOContext *s);
583 * Open a write only memory stream.
585 * @param s new IO context
586 * @return zero if no error.
588 int url_open_dyn_buf(AVIOContext **s);
591 * Open a write only packetized memory stream with a maximum packet
592 * size of 'max_packet_size'. The stream is stored in a memory buffer
593 * with a big endian 4 byte header giving the packet size in bytes.
595 * @param s new IO context
596 * @param max_packet_size maximum packet size (must be > 0)
597 * @return zero if no error.
599 int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size);
602 * Return the written size and a pointer to the buffer. The buffer
603 * must be freed with av_free(). If the buffer is opened with
604 * url_open_dyn_buf, then padding of FF_INPUT_BUFFER_PADDING_SIZE is
605 * added; if opened with url_open_dyn_packet_buf, no padding is added.
607 * @param s IO context
608 * @param pbuffer pointer to a byte buffer
609 * @return the length of the byte buffer
611 int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
613 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
615 unsigned long get_checksum(AVIOContext *s);
616 void init_checksum(AVIOContext *s,
617 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
618 unsigned long checksum);
621 int udp_set_remote_url(URLContext *h, const char *uri);
622 int udp_get_local_port(URLContext *h);
623 #if FF_API_UDP_GET_FILE
624 int udp_get_file_handle(URLContext *h);
627 #endif /* AVFORMAT_AVIO_H */