Link libavdevice against libavutil, libavcodec, and libavformat
[libav.git] / libavformat / avio.h
CommitLineData
04d7f601
DB
1/*
2 * unbuffered io for ffmpeg system
3 * copyright (c) 2001 Fabrice Bellard
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
04d7f601
DB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
04d7f601 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
04d7f601
DB
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
04d7f601
DB
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
5b21bdab
DB
21#ifndef FFMPEG_AVIO_H
22#define FFMPEG_AVIO_H
f031df23 23
99545457
MR
24#include <stdint.h>
25
de6d9b64
FB
26/* output byte stream handling */
27
0c1a9eda 28typedef int64_t offset_t;
de6d9b64
FB
29
30/* unbuffered I/O */
31
32struct URLContext {
33 struct URLProtocol *prot;
115329f1 34 int flags;
c3316802
PI
35 int is_streamed; /**< true if streamed (no seek possible), default = false */
36 int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
de6d9b64 37 void *priv_data;
62181004 38#if LIBAVFORMAT_VERSION_INT >= (52<<16)
c3316802 39 char *filename; /**< specified filename */
62181004 40#else
c3316802 41 char filename[1]; /**< specified filename */
62181004 42#endif
de6d9b64
FB
43};
44
de6d9b64
FB
45typedef struct URLContext URLContext;
46
47typedef struct URLPollEntry {
48 URLContext *handle;
49 int events;
50 int revents;
51} URLPollEntry;
52
53#define URL_RDONLY 0
54#define URL_WRONLY 1
f031df23
FB
55#define URL_RDWR 2
56
019ac05a
FB
57typedef int URLInterruptCB(void);
58
de6d9b64
FB
59int url_open(URLContext **h, const char *filename, int flags);
60int url_read(URLContext *h, unsigned char *buf, int size);
61int url_write(URLContext *h, unsigned char *buf, int size);
62offset_t url_seek(URLContext *h, offset_t pos, int whence);
de6d9b64
FB
63int url_close(URLContext *h);
64int url_exist(const char *filename);
65offset_t url_filesize(URLContext *h);
c306748c 66
c3316802 67/**
c306748c
PI
68 * Return the maximum packet size associated to packetized file
69 * handle. If the file is not packetized (stream like http or file on
70 * disk), then 0 is returned.
71 *
72 * @param h file handle
73 * @return maximum packet size in bytes
74 */
f031df23 75int url_get_max_packet_size(URLContext *h);
f746a046
FB
76void url_get_filename(URLContext *h, char *buf, int buf_size);
77
c3316802
PI
78/**
79 * the callback is called in blocking functions to test regulary if
80 * asynchronous interruption is needed. AVERROR(EINTR) is returned
81 * in this case by the interrupted function. 'NULL' means no interrupt
82 * callback is given. i
83 */
019ac05a
FB
84void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
85
de6d9b64
FB
86/* not implemented */
87int url_poll(URLPollEntry *poll_table, int n, int timeout);
88
aa38b097 89/**
90b5b51e
DB
90 * Passing this as the "whence" parameter to a seek function causes it to
91 * return the filesize without seeking anywhere. Supporting this is optional.
92 * If it is not supported then the seek function will return <0.
aa38b097 93 */
8e287af0
MN
94#define AVSEEK_SIZE 0x10000
95
de6d9b64
FB
96typedef struct URLProtocol {
97 const char *name;
98 int (*url_open)(URLContext *h, const char *filename, int flags);
99 int (*url_read)(URLContext *h, unsigned char *buf, int size);
100 int (*url_write)(URLContext *h, unsigned char *buf, int size);
101 offset_t (*url_seek)(URLContext *h, offset_t pos, int whence);
102 int (*url_close)(URLContext *h);
de6d9b64
FB
103 struct URLProtocol *next;
104} URLProtocol;
105
106extern URLProtocol *first_protocol;
019ac05a 107extern URLInterruptCB *url_interrupt_cb;
de6d9b64
FB
108
109int register_protocol(URLProtocol *protocol);
110
111typedef struct {
112 unsigned char *buffer;
113 int buffer_size;
114 unsigned char *buf_ptr, *buf_end;
115 void *opaque;
0c1a9eda 116 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
576ae256 117 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
536ffa59 118 offset_t (*seek)(void *opaque, offset_t offset, int whence);
c3316802
PI
119 offset_t pos; /**< position in the file of the current buffer */
120 int must_flush; /**< true if the next seek should flush */
121 int eof_reached; /**< true if eof reached */
122 int write_flag; /**< true if open for writing */
de6d9b64 123 int is_streamed;
f031df23 124 int max_packet_size;
ee9f36a8
MN
125 unsigned long checksum;
126 unsigned char *checksum_ptr;
127 unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
576ae256 128 int error; ///< contains the error code or 0 if no error happened
de6d9b64
FB
129} ByteIOContext;
130
131int init_put_byte(ByteIOContext *s,
132 unsigned char *buffer,
133 int buffer_size,
134 int write_flag,
135 void *opaque,
0c1a9eda 136 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
576ae256 137 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
536ffa59 138 offset_t (*seek)(void *opaque, offset_t offset, int whence));
de6d9b64
FB
139
140void put_byte(ByteIOContext *s, int b);
75bdb984 141void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
0c1a9eda
ZK
142void put_le64(ByteIOContext *s, uint64_t val);
143void put_be64(ByteIOContext *s, uint64_t val);
de6d9b64
FB
144void put_le32(ByteIOContext *s, unsigned int val);
145void put_be32(ByteIOContext *s, unsigned int val);
ea395e8c 146void put_le24(ByteIOContext *s, unsigned int val);
a254c574 147void put_be24(ByteIOContext *s, unsigned int val);
de6d9b64
FB
148void put_le16(ByteIOContext *s, unsigned int val);
149void put_be16(ByteIOContext *s, unsigned int val);
0570bf06 150void put_tag(ByteIOContext *s, const char *tag);
de6d9b64 151
3b4b29dc 152void put_strz(ByteIOContext *s, const char *buf);
75bdb984 153
de6d9b64
FB
154offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
155void url_fskip(ByteIOContext *s, offset_t offset);
156offset_t url_ftell(ByteIOContext *s);
a965c478 157offset_t url_fsize(ByteIOContext *s);
de6d9b64 158int url_feof(ByteIOContext *s);
576ae256 159int url_ferror(ByteIOContext *s);
de6d9b64 160
f031df23 161#define URL_EOF (-1)
c3316802 162/** @note return URL_EOF (-1) if EOF */
f031df23 163int url_fgetc(ByteIOContext *s);
a8c5ab27 164
c3316802 165/** @warning currently size is limited */
572f992e 166#ifdef __GNUC__
08f29f82 167int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
572f992e
MN
168#else
169int url_fprintf(ByteIOContext *s, const char *fmt, ...);
170#endif
a8c5ab27 171
c3316802 172/** @note unlike fgets, the EOL character is not returned and a whole
a8c5ab27 173 line is parsed. return NULL if first char read was EOF */
f031df23
FB
174char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
175
de6d9b64
FB
176void put_flush_packet(ByteIOContext *s);
177
178int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
e15dec10 179int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
a8c5ab27 180
c3316802 181/** @note return 0 if EOF, so you cannot use it if EOF handling is
a8c5ab27 182 necessary */
de6d9b64 183int get_byte(ByteIOContext *s);
ea395e8c 184unsigned int get_le24(ByteIOContext *s);
de6d9b64 185unsigned int get_le32(ByteIOContext *s);
0c1a9eda 186uint64_t get_le64(ByteIOContext *s);
de6d9b64
FB
187unsigned int get_le16(ByteIOContext *s);
188
3b4b29dc 189char *get_strz(ByteIOContext *s, char *buf, int maxlen);
de6d9b64 190unsigned int get_be16(ByteIOContext *s);
a254c574 191unsigned int get_be24(ByteIOContext *s);
de6d9b64 192unsigned int get_be32(ByteIOContext *s);
0c1a9eda 193uint64_t get_be64(ByteIOContext *s);
de6d9b64 194
7798b42d 195uint64_t ff_get_v(ByteIOContext *bc);
897d3eef 196
7ac13f0c 197static inline int url_is_streamed(ByteIOContext *s)
de6d9b64
FB
198{
199 return s->is_streamed;
200}
de6d9b64 201
3576f9c6
BA
202/** @note when opened as read/write, the buffers are only used for
203 writing */
899681cd 204int url_fdopen(ByteIOContext **s, URLContext *h);
a8c5ab27 205
c3316802 206/** @warning must be called before any I/O */
de6d9b64 207int url_setbufsize(ByteIOContext *s, int buf_size);
770d9daf
BA
208/** Reset the buffer for reading or writing.
209 * @note Will drop any data currently in the buffer without transmitting it.
210 * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
211 * to set up the buffer for writing. */
212int url_resetbuf(ByteIOContext *s, int flags);
a8c5ab27 213
c3316802 214/** @note when opened as read/write, the buffers are only used for
3576f9c6 215 writing */
899681cd 216int url_fopen(ByteIOContext **s, const char *filename, int flags);
de6d9b64
FB
217int url_fclose(ByteIOContext *s);
218URLContext *url_fileno(ByteIOContext *s);
a8c5ab27 219
c3316802 220/**
a8c5ab27
PI
221 * Return the maximum packet size associated to packetized buffered file
222 * handle. If the file is not packetized (stream like http or file on
223 * disk), then 0 is returned.
224 *
abfe87d4 225 * @param s buffered file handle
a8c5ab27
PI
226 * @return maximum packet size in bytes
227 */
f031df23 228int url_fget_max_packet_size(ByteIOContext *s);
de6d9b64 229
899681cd 230int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags);
a8c5ab27 231
c3316802 232/** return the written or read size */
de6d9b64
FB
233int url_close_buf(ByteIOContext *s);
234
c3316802 235/**
a8c5ab27
PI
236 * Open a write only memory stream.
237 *
238 * @param s new IO context
239 * @return zero if no error.
240 */
899681cd 241int url_open_dyn_buf(ByteIOContext **s);
a8c5ab27 242
c3316802 243/**
a8c5ab27
PI
244 * Open a write only packetized memory stream with a maximum packet
245 * size of 'max_packet_size'. The stream is stored in a memory buffer
246 * with a big endian 4 byte header giving the packet size in bytes.
247 *
248 * @param s new IO context
249 * @param max_packet_size maximum packet size (must be > 0)
250 * @return zero if no error.
251 */
899681cd 252int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size);
a8c5ab27 253
c3316802 254/**
a8c5ab27
PI
255 * Return the written size and a pointer to the buffer. The buffer
256 * must be freed with av_free().
257 * @param s IO context
e8b7c70f 258 * @param pbuffer pointer to a byte buffer
a8c5ab27
PI
259 * @return the length of the byte buffer
260 */
0c1a9eda 261int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
f031df23 262
992e78f5 263unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len);
ee9f36a8
MN
264unsigned long get_checksum(ByteIOContext *s);
265void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
ee9f36a8 266
de6d9b64 267/* udp.c */
f031df23
FB
268int udp_set_remote_url(URLContext *h, const char *uri);
269int udp_get_local_port(URLContext *h);
270int udp_get_file_handle(URLContext *h);
271
5b21bdab 272#endif /* FFMPEG_AVIO_H */