yading@11: /* yading@11: * copyright (c) 2001 Fabrice Bellard yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: #ifndef AVFORMAT_AVIO_H yading@11: #define AVFORMAT_AVIO_H yading@11: yading@11: /** yading@11: * @file yading@11: * @ingroup lavf_io yading@11: * Buffered I/O operations yading@11: */ yading@11: yading@11: #include yading@11: yading@11: #include "libavutil/common.h" yading@11: #include "libavutil/dict.h" yading@11: #include "libavutil/log.h" yading@11: yading@11: #include "libavformat/version.h" yading@11: yading@11: yading@11: #define AVIO_SEEKABLE_NORMAL 0x0001 /**< Seeking works like for a local file */ yading@11: yading@11: /** yading@11: * Callback for checking whether to abort blocking functions. yading@11: * AVERROR_EXIT is returned in this case by the interrupted yading@11: * function. During blocking operations, callback is called with yading@11: * opaque as parameter. If the callback returns 1, the yading@11: * blocking operation will be aborted. yading@11: * yading@11: * No members can be added to this struct without a major bump, if yading@11: * new elements have been added after this struct in AVFormatContext yading@11: * or AVIOContext. yading@11: */ yading@11: typedef struct AVIOInterruptCB { yading@11: int (*callback)(void*); yading@11: void *opaque; yading@11: } AVIOInterruptCB; yading@11: yading@11: /** yading@11: * Bytestream IO Context. yading@11: * New fields can be added to the end with minor version bumps. yading@11: * Removal, reordering and changes to existing fields require a major yading@11: * version bump. yading@11: * sizeof(AVIOContext) must not be used outside libav*. yading@11: * yading@11: * @note None of the function pointers in AVIOContext should be called yading@11: * directly, they should only be set by the client application yading@11: * when implementing custom I/O. Normally these are set to the yading@11: * function pointers specified in avio_alloc_context() yading@11: */ yading@11: typedef struct AVIOContext { yading@11: /** yading@11: * A class for private options. yading@11: * yading@11: * If this AVIOContext is created by avio_open2(), av_class is set and yading@11: * passes the options down to protocols. yading@11: * yading@11: * If this AVIOContext is manually allocated, then av_class may be set by yading@11: * the caller. yading@11: * yading@11: * warning -- this field can be NULL, be sure to not pass this AVIOContext yading@11: * to any av_opt_* functions in that case. yading@11: */ yading@11: const AVClass *av_class; yading@11: unsigned char *buffer; /**< Start of the buffer. */ yading@11: int buffer_size; /**< Maximum buffer size */ yading@11: unsigned char *buf_ptr; /**< Current position in the buffer */ yading@11: unsigned char *buf_end; /**< End of the data, may be less than yading@11: buffer+buffer_size if the read function returned yading@11: less data than requested, e.g. for streams where yading@11: no more data has been received yet. */ yading@11: void *opaque; /**< A private pointer, passed to the read/write/seek/... yading@11: functions. */ yading@11: int (*read_packet)(void *opaque, uint8_t *buf, int buf_size); yading@11: int (*write_packet)(void *opaque, uint8_t *buf, int buf_size); yading@11: int64_t (*seek)(void *opaque, int64_t offset, int whence); yading@11: int64_t pos; /**< position in the file of the current buffer */ yading@11: int must_flush; /**< true if the next seek should flush */ yading@11: int eof_reached; /**< true if eof reached */ yading@11: int write_flag; /**< true if open for writing */ yading@11: int max_packet_size; yading@11: unsigned long checksum; yading@11: unsigned char *checksum_ptr; yading@11: unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size); yading@11: int error; /**< contains the error code or 0 if no error happened */ yading@11: /** yading@11: * Pause or resume playback for network streaming protocols - e.g. MMS. yading@11: */ yading@11: int (*read_pause)(void *opaque, int pause); yading@11: /** yading@11: * Seek to a given timestamp in stream with the specified stream_index. yading@11: * Needed for some network streaming protocols which don't support seeking yading@11: * to byte position. yading@11: */ yading@11: int64_t (*read_seek)(void *opaque, int stream_index, yading@11: int64_t timestamp, int flags); yading@11: /** yading@11: * A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable. yading@11: */ yading@11: int seekable; yading@11: yading@11: /** yading@11: * max filesize, used to limit allocations yading@11: * This field is internal to libavformat and access from outside is not allowed. yading@11: */ yading@11: int64_t maxsize; yading@11: yading@11: /** yading@11: * avio_read and avio_write should if possible be satisfied directly yading@11: * instead of going through a buffer, and avio_seek will always yading@11: * call the underlying seek function directly. yading@11: */ yading@11: int direct; yading@11: yading@11: /** yading@11: * Bytes read statistic yading@11: * This field is internal to libavformat and access from outside is not allowed. yading@11: */ yading@11: int64_t bytes_read; yading@11: yading@11: /** yading@11: * seek statistic yading@11: * This field is internal to libavformat and access from outside is not allowed. yading@11: */ yading@11: int seek_count; yading@11: yading@11: /** yading@11: * writeout statistic yading@11: * This field is internal to libavformat and access from outside is not allowed. yading@11: */ yading@11: int writeout_count; yading@11: } AVIOContext; yading@11: yading@11: /* unbuffered I/O */ yading@11: yading@11: /** yading@11: * Return AVIO_FLAG_* access flags corresponding to the access permissions yading@11: * of the resource in url, or a negative value corresponding to an yading@11: * AVERROR code in case of failure. The returned access flags are yading@11: * masked by the value in flags. yading@11: * yading@11: * @note This function is intrinsically unsafe, in the sense that the yading@11: * checked resource may change its existence or permission status from yading@11: * one call to another. Thus you should not trust the returned value, yading@11: * unless you are sure that no other processes are accessing the yading@11: * checked resource. yading@11: */ yading@11: int avio_check(const char *url, int flags); yading@11: yading@11: /** yading@11: * Allocate and initialize an AVIOContext for buffered I/O. It must be later yading@11: * freed with av_free(). yading@11: * yading@11: * @param buffer Memory block for input/output operations via AVIOContext. yading@11: * The buffer must be allocated with av_malloc() and friends. yading@11: * @param buffer_size The buffer size is very important for performance. yading@11: * For protocols with fixed blocksize it should be set to this blocksize. yading@11: * For others a typical size is a cache page, e.g. 4kb. yading@11: * @param write_flag Set to 1 if the buffer should be writable, 0 otherwise. yading@11: * @param opaque An opaque pointer to user-specific data. yading@11: * @param read_packet A function for refilling the buffer, may be NULL. yading@11: * @param write_packet A function for writing the buffer contents, may be NULL. yading@11: * The function may not change the input buffers content. yading@11: * @param seek A function for seeking to specified byte position, may be NULL. yading@11: * yading@11: * @return Allocated AVIOContext or NULL on failure. yading@11: */ yading@11: AVIOContext *avio_alloc_context( yading@11: unsigned char *buffer, yading@11: int buffer_size, yading@11: int write_flag, yading@11: void *opaque, yading@11: int (*read_packet)(void *opaque, uint8_t *buf, int buf_size), yading@11: int (*write_packet)(void *opaque, uint8_t *buf, int buf_size), yading@11: int64_t (*seek)(void *opaque, int64_t offset, int whence)); yading@11: yading@11: void avio_w8(AVIOContext *s, int b); yading@11: void avio_write(AVIOContext *s, const unsigned char *buf, int size); yading@11: void avio_wl64(AVIOContext *s, uint64_t val); yading@11: void avio_wb64(AVIOContext *s, uint64_t val); yading@11: void avio_wl32(AVIOContext *s, unsigned int val); yading@11: void avio_wb32(AVIOContext *s, unsigned int val); yading@11: void avio_wl24(AVIOContext *s, unsigned int val); yading@11: void avio_wb24(AVIOContext *s, unsigned int val); yading@11: void avio_wl16(AVIOContext *s, unsigned int val); yading@11: void avio_wb16(AVIOContext *s, unsigned int val); yading@11: yading@11: /** yading@11: * Write a NULL-terminated string. yading@11: * @return number of bytes written. yading@11: */ yading@11: int avio_put_str(AVIOContext *s, const char *str); yading@11: yading@11: /** yading@11: * Convert an UTF-8 string to UTF-16LE and write it. yading@11: * @return number of bytes written. yading@11: */ yading@11: int avio_put_str16le(AVIOContext *s, const char *str); yading@11: yading@11: /** yading@11: * Passing this as the "whence" parameter to a seek function causes it to yading@11: * return the filesize without seeking anywhere. Supporting this is optional. yading@11: * If it is not supported then the seek function will return <0. yading@11: */ yading@11: #define AVSEEK_SIZE 0x10000 yading@11: yading@11: /** yading@11: * Oring this flag as into the "whence" parameter to a seek function causes it to yading@11: * seek by any means (like reopening and linear reading) or other normally unreasonable yading@11: * means that can be extremely slow. yading@11: * This may be ignored by the seek code. yading@11: */ yading@11: #define AVSEEK_FORCE 0x20000 yading@11: yading@11: /** yading@11: * fseek() equivalent for AVIOContext. yading@11: * @return new position or AVERROR. yading@11: */ yading@11: int64_t avio_seek(AVIOContext *s, int64_t offset, int whence); yading@11: yading@11: /** yading@11: * Skip given number of bytes forward yading@11: * @return new position or AVERROR. yading@11: */ yading@11: int64_t avio_skip(AVIOContext *s, int64_t offset); yading@11: yading@11: /** yading@11: * ftell() equivalent for AVIOContext. yading@11: * @return position or AVERROR. yading@11: */ yading@11: static av_always_inline int64_t avio_tell(AVIOContext *s) yading@11: { yading@11: return avio_seek(s, 0, SEEK_CUR); yading@11: } yading@11: yading@11: /** yading@11: * Get the filesize. yading@11: * @return filesize or AVERROR yading@11: */ yading@11: int64_t avio_size(AVIOContext *s); yading@11: yading@11: /** yading@11: * feof() equivalent for AVIOContext. yading@11: * @return non zero if and only if end of file yading@11: */ yading@11: int url_feof(AVIOContext *s); yading@11: yading@11: /** @warning currently size is limited */ yading@11: int avio_printf(AVIOContext *s, const char *fmt, ...) av_printf_format(2, 3); yading@11: yading@11: /** yading@11: * Force flushing of buffered data to the output s. yading@11: * yading@11: * Force the buffered data to be immediately written to the output, yading@11: * without to wait to fill the internal buffer. yading@11: */ yading@11: void avio_flush(AVIOContext *s); yading@11: yading@11: /** yading@11: * Read size bytes from AVIOContext into buf. yading@11: * @return number of bytes read or AVERROR yading@11: */ yading@11: int avio_read(AVIOContext *s, unsigned char *buf, int size); yading@11: yading@11: /** yading@11: * @name Functions for reading from AVIOContext yading@11: * @{ yading@11: * yading@11: * @note return 0 if EOF, so you cannot use it if EOF handling is yading@11: * necessary yading@11: */ yading@11: int avio_r8 (AVIOContext *s); yading@11: unsigned int avio_rl16(AVIOContext *s); yading@11: unsigned int avio_rl24(AVIOContext *s); yading@11: unsigned int avio_rl32(AVIOContext *s); yading@11: uint64_t avio_rl64(AVIOContext *s); yading@11: unsigned int avio_rb16(AVIOContext *s); yading@11: unsigned int avio_rb24(AVIOContext *s); yading@11: unsigned int avio_rb32(AVIOContext *s); yading@11: uint64_t avio_rb64(AVIOContext *s); yading@11: /** yading@11: * @} yading@11: */ yading@11: yading@11: /** yading@11: * Read a string from pb into buf. The reading will terminate when either yading@11: * a NULL character was encountered, maxlen bytes have been read, or nothing yading@11: * more can be read from pb. The result is guaranteed to be NULL-terminated, it yading@11: * will be truncated if buf is too small. yading@11: * Note that the string is not interpreted or validated in any way, it yading@11: * might get truncated in the middle of a sequence for multi-byte encodings. yading@11: * yading@11: * @return number of bytes read (is always <= maxlen). yading@11: * If reading ends on EOF or error, the return value will be one more than yading@11: * bytes actually read. yading@11: */ yading@11: int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen); yading@11: yading@11: /** yading@11: * Read a UTF-16 string from pb and convert it to UTF-8. yading@11: * The reading will terminate when either a null or invalid character was yading@11: * encountered or maxlen bytes have been read. yading@11: * @return number of bytes read (is always <= maxlen) yading@11: */ yading@11: int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen); yading@11: int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen); yading@11: yading@11: yading@11: /** yading@11: * @name URL open modes yading@11: * The flags argument to avio_open must be one of the following yading@11: * constants, optionally ORed with other flags. yading@11: * @{ yading@11: */ yading@11: #define AVIO_FLAG_READ 1 /**< read-only */ yading@11: #define AVIO_FLAG_WRITE 2 /**< write-only */ yading@11: #define AVIO_FLAG_READ_WRITE (AVIO_FLAG_READ|AVIO_FLAG_WRITE) /**< read-write pseudo flag */ yading@11: /** yading@11: * @} yading@11: */ yading@11: yading@11: /** yading@11: * Use non-blocking mode. yading@11: * If this flag is set, operations on the context will return yading@11: * AVERROR(EAGAIN) if they can not be performed immediately. yading@11: * If this flag is not set, operations on the context will never return yading@11: * AVERROR(EAGAIN). yading@11: * Note that this flag does not affect the opening/connecting of the yading@11: * context. Connecting a protocol will always block if necessary (e.g. on yading@11: * network protocols) but never hang (e.g. on busy devices). yading@11: * Warning: non-blocking protocols is work-in-progress; this flag may be yading@11: * silently ignored. yading@11: */ yading@11: #define AVIO_FLAG_NONBLOCK 8 yading@11: yading@11: /** yading@11: * Use direct mode. yading@11: * avio_read and avio_write should if possible be satisfied directly yading@11: * instead of going through a buffer, and avio_seek will always yading@11: * call the underlying seek function directly. yading@11: */ yading@11: #define AVIO_FLAG_DIRECT 0x8000 yading@11: yading@11: /** yading@11: * Create and initialize a AVIOContext for accessing the yading@11: * resource indicated by url. yading@11: * @note When the resource indicated by url has been opened in yading@11: * read+write mode, the AVIOContext can be used only for writing. yading@11: * yading@11: * @param s Used to return the pointer to the created AVIOContext. yading@11: * In case of failure the pointed to value is set to NULL. yading@11: * @param flags flags which control how the resource indicated by url yading@11: * is to be opened yading@11: * @return 0 in case of success, a negative value corresponding to an yading@11: * AVERROR code in case of failure yading@11: */ yading@11: int avio_open(AVIOContext **s, const char *url, int flags); yading@11: yading@11: /** yading@11: * Create and initialize a AVIOContext for accessing the yading@11: * resource indicated by url. yading@11: * @note When the resource indicated by url has been opened in yading@11: * read+write mode, the AVIOContext can be used only for writing. yading@11: * yading@11: * @param s Used to return the pointer to the created AVIOContext. yading@11: * In case of failure the pointed to value is set to NULL. yading@11: * @param flags flags which control how the resource indicated by url yading@11: * is to be opened yading@11: * @param int_cb an interrupt callback to be used at the protocols level yading@11: * @param options A dictionary filled with protocol-private options. On return yading@11: * this parameter will be destroyed and replaced with a dict containing options yading@11: * that were not found. May be NULL. yading@11: * @return 0 in case of success, a negative value corresponding to an yading@11: * AVERROR code in case of failure yading@11: */ yading@11: int avio_open2(AVIOContext **s, const char *url, int flags, yading@11: const AVIOInterruptCB *int_cb, AVDictionary **options); yading@11: yading@11: /** yading@11: * Close the resource accessed by the AVIOContext s and free it. yading@11: * This function can only be used if s was opened by avio_open(). yading@11: * yading@11: * The internal buffer is automatically flushed before closing the yading@11: * resource. yading@11: * yading@11: * @return 0 on success, an AVERROR < 0 on error. yading@11: * @see avio_closep yading@11: */ yading@11: int avio_close(AVIOContext *s); yading@11: yading@11: /** yading@11: * Close the resource accessed by the AVIOContext *s, free it yading@11: * and set the pointer pointing to it to NULL. yading@11: * This function can only be used if s was opened by avio_open(). yading@11: * yading@11: * The internal buffer is automatically flushed before closing the yading@11: * resource. yading@11: * yading@11: * @return 0 on success, an AVERROR < 0 on error. yading@11: * @see avio_close yading@11: */ yading@11: int avio_closep(AVIOContext **s); yading@11: yading@11: yading@11: /** yading@11: * Open a write only memory stream. yading@11: * yading@11: * @param s new IO context yading@11: * @return zero if no error. yading@11: */ yading@11: int avio_open_dyn_buf(AVIOContext **s); yading@11: yading@11: /** yading@11: * Return the written size and a pointer to the buffer. The buffer yading@11: * must be freed with av_free(). yading@11: * Padding of FF_INPUT_BUFFER_PADDING_SIZE is added to the buffer. yading@11: * yading@11: * @param s IO context yading@11: * @param pbuffer pointer to a byte buffer yading@11: * @return the length of the byte buffer yading@11: */ yading@11: int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer); yading@11: yading@11: /** yading@11: * Iterate through names of available protocols. yading@11: * yading@11: * @param opaque A private pointer representing current protocol. yading@11: * It must be a pointer to NULL on first iteration and will yading@11: * be updated by successive calls to avio_enum_protocols. yading@11: * @param output If set to 1, iterate over output protocols, yading@11: * otherwise over input protocols. yading@11: * yading@11: * @return A static string containing the name of current protocol or NULL yading@11: */ yading@11: const char *avio_enum_protocols(void **opaque, int output); yading@11: yading@11: /** yading@11: * Pause and resume playing - only meaningful if using a network streaming yading@11: * protocol (e.g. MMS). yading@11: * @param pause 1 for pause, 0 for resume yading@11: */ yading@11: int avio_pause(AVIOContext *h, int pause); yading@11: yading@11: /** yading@11: * Seek to a given timestamp relative to some component stream. yading@11: * Only meaningful if using a network streaming protocol (e.g. MMS.). yading@11: * @param stream_index The stream index that the timestamp is relative to. yading@11: * If stream_index is (-1) the timestamp should be in AV_TIME_BASE yading@11: * units from the beginning of the presentation. yading@11: * If a stream_index >= 0 is used and the protocol does not support yading@11: * seeking based on component streams, the call will fail. yading@11: * @param timestamp timestamp in AVStream.time_base units yading@11: * or if there is no stream specified then in AV_TIME_BASE units. yading@11: * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE yading@11: * and AVSEEK_FLAG_ANY. The protocol may silently ignore yading@11: * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will yading@11: * fail if used and not supported. yading@11: * @return >= 0 on success yading@11: * @see AVInputFormat::read_seek yading@11: */ yading@11: int64_t avio_seek_time(AVIOContext *h, int stream_index, yading@11: int64_t timestamp, int flags); yading@11: yading@11: #endif /* AVFORMAT_AVIO_H */