avio.h 8.71 KB
Newer Older
1 2 3 4
/*
 * unbuffered io for ffmpeg system
 * copyright (c) 2001 Fabrice Bellard
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19 20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
21 22 23
#ifndef AVIO_H
#define AVIO_H

24 25
#include <stdint.h>

Fabrice Bellard's avatar
Fabrice Bellard committed
26 27
/* output byte stream handling */

28
typedef int64_t offset_t;
Fabrice Bellard's avatar
Fabrice Bellard committed
29 30 31 32 33

/* unbuffered I/O */

struct URLContext {
    struct URLProtocol *prot;
34
    int flags;
35 36
    int is_streamed;  /**< true if streamed (no seek possible), default = false */
    int max_packet_size;  /**< if non zero, the stream is packetized with this max packet size */
Fabrice Bellard's avatar
Fabrice Bellard committed
37
    void *priv_data;
38
#if LIBAVFORMAT_VERSION_INT >= (52<<16)
39
    char *filename; /**< specified filename */
40
#else
41
    char filename[1]; /**< specified filename */
42
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
43 44 45 46 47 48 49 50 51 52 53 54
};

typedef struct URLContext URLContext;

typedef struct URLPollEntry {
    URLContext *handle;
    int events;
    int revents;
} URLPollEntry;

#define URL_RDONLY 0
#define URL_WRONLY 1
55 56
#define URL_RDWR   2

57 58
typedef int URLInterruptCB(void);

Fabrice Bellard's avatar
Fabrice Bellard committed
59 60 61 62 63 64 65
int url_open(URLContext **h, const char *filename, int flags);
int url_read(URLContext *h, unsigned char *buf, int size);
int url_write(URLContext *h, unsigned char *buf, int size);
offset_t url_seek(URLContext *h, offset_t pos, int whence);
int url_close(URLContext *h);
int url_exist(const char *filename);
offset_t url_filesize(URLContext *h);
66

67
/**
68 69 70 71 72 73 74
 * Return the maximum packet size associated to packetized file
 * handle. If the file is not packetized (stream like http or file on
 * disk), then 0 is returned.
 *
 * @param h file handle
 * @return maximum packet size in bytes
 */
75
int url_get_max_packet_size(URLContext *h);
Fabrice Bellard's avatar
Fabrice Bellard committed
76 77
void url_get_filename(URLContext *h, char *buf, int buf_size);

78 79 80 81 82 83
/**
 * the callback is called in blocking functions to test regulary if
 * asynchronous interruption is needed. AVERROR(EINTR) is returned
 * in this case by the interrupted function. 'NULL' means no interrupt
 * callback is given. i
 */
84 85
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);

Fabrice Bellard's avatar
Fabrice Bellard committed
86 87 88
/* not implemented */
int url_poll(URLPollEntry *poll_table, int n, int timeout);

Michael Niedermayer's avatar
Michael Niedermayer committed
89
/**
Diego Biurrun's avatar
Diego Biurrun committed
90 91 92
 * Passing this as the "whence" parameter to a seek function causes it to
 * return the filesize without seeking anywhere. Supporting this is optional.
 * If it is not supported then the seek function will return <0.
Michael Niedermayer's avatar
Michael Niedermayer committed
93
 */
94 95
#define AVSEEK_SIZE 0x10000

Fabrice Bellard's avatar
Fabrice Bellard committed
96 97 98 99 100 101 102 103 104 105 106
typedef struct URLProtocol {
    const char *name;
    int (*url_open)(URLContext *h, const char *filename, int flags);
    int (*url_read)(URLContext *h, unsigned char *buf, int size);
    int (*url_write)(URLContext *h, unsigned char *buf, int size);
    offset_t (*url_seek)(URLContext *h, offset_t pos, int whence);
    int (*url_close)(URLContext *h);
    struct URLProtocol *next;
} URLProtocol;

extern URLProtocol *first_protocol;
107
extern URLInterruptCB *url_interrupt_cb;
Fabrice Bellard's avatar
Fabrice Bellard committed
108 109 110 111 112 113 114 115

int register_protocol(URLProtocol *protocol);

typedef struct {
    unsigned char *buffer;
    int buffer_size;
    unsigned char *buf_ptr, *buf_end;
    void *opaque;
116
    int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
Michael Niedermayer's avatar
Michael Niedermayer committed
117
    int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
118
    offset_t (*seek)(void *opaque, offset_t offset, int whence);
119 120 121 122
    offset_t pos; /**< position in the file of the current buffer */
    int must_flush; /**< true if the next seek should flush */
    int eof_reached; /**< true if eof reached */
    int write_flag;  /**< true if open for writing */
Fabrice Bellard's avatar
Fabrice Bellard committed
123
    int is_streamed;
124
    int max_packet_size;
125 126 127
    unsigned long checksum;
    unsigned char *checksum_ptr;
    unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
Michael Niedermayer's avatar
Michael Niedermayer committed
128
    int error;         ///< contains the error code or 0 if no error happened
Fabrice Bellard's avatar
Fabrice Bellard committed
129 130 131 132 133 134 135
} ByteIOContext;

int init_put_byte(ByteIOContext *s,
                  unsigned char *buffer,
                  int buffer_size,
                  int write_flag,
                  void *opaque,
136
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
Michael Niedermayer's avatar
Michael Niedermayer committed
137
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
138
                  offset_t (*seek)(void *opaque, offset_t offset, int whence));
Fabrice Bellard's avatar
Fabrice Bellard committed
139 140

void put_byte(ByteIOContext *s, int b);
141
void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
142 143
void put_le64(ByteIOContext *s, uint64_t val);
void put_be64(ByteIOContext *s, uint64_t val);
Fabrice Bellard's avatar
Fabrice Bellard committed
144 145
void put_le32(ByteIOContext *s, unsigned int val);
void put_be32(ByteIOContext *s, unsigned int val);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
146
void put_le24(ByteIOContext *s, unsigned int val);
147
void put_be24(ByteIOContext *s, unsigned int val);
Fabrice Bellard's avatar
Fabrice Bellard committed
148 149
void put_le16(ByteIOContext *s, unsigned int val);
void put_be16(ByteIOContext *s, unsigned int val);
Zdenek Kabelac's avatar
Zdenek Kabelac committed
150
void put_tag(ByteIOContext *s, const char *tag);
Fabrice Bellard's avatar
Fabrice Bellard committed
151

152
void put_strz(ByteIOContext *s, const char *buf);
153

Fabrice Bellard's avatar
Fabrice Bellard committed
154 155 156
offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
void url_fskip(ByteIOContext *s, offset_t offset);
offset_t url_ftell(ByteIOContext *s);
157
offset_t url_fsize(ByteIOContext *s);
Fabrice Bellard's avatar
Fabrice Bellard committed
158
int url_feof(ByteIOContext *s);
Michael Niedermayer's avatar
Michael Niedermayer committed
159
int url_ferror(ByteIOContext *s);
Fabrice Bellard's avatar
Fabrice Bellard committed
160

161
#define URL_EOF (-1)
162
/** @note return URL_EOF (-1) if EOF */
163
int url_fgetc(ByteIOContext *s);
164

165
/** @warning currently size is limited */
166
#ifdef __GNUC__
167
int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
168 169 170
#else
int url_fprintf(ByteIOContext *s, const char *fmt, ...);
#endif
171

172
/** @note unlike fgets, the EOL character is not returned and a whole
173
   line is parsed. return NULL if first char read was EOF */
174 175
char *url_fgets(ByteIOContext *s, char *buf, int buf_size);

Fabrice Bellard's avatar
Fabrice Bellard committed
176 177 178
void put_flush_packet(ByteIOContext *s);

int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
179
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
180

181
/** @note return 0 if EOF, so you cannot use it if EOF handling is
182
   necessary */
Fabrice Bellard's avatar
Fabrice Bellard committed
183
int get_byte(ByteIOContext *s);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
184
unsigned int get_le24(ByteIOContext *s);
Fabrice Bellard's avatar
Fabrice Bellard committed
185
unsigned int get_le32(ByteIOContext *s);
186
uint64_t get_le64(ByteIOContext *s);
Fabrice Bellard's avatar
Fabrice Bellard committed
187 188
unsigned int get_le16(ByteIOContext *s);

189
char *get_strz(ByteIOContext *s, char *buf, int maxlen);
Fabrice Bellard's avatar
Fabrice Bellard committed
190
unsigned int get_be16(ByteIOContext *s);
191
unsigned int get_be24(ByteIOContext *s);
Fabrice Bellard's avatar
Fabrice Bellard committed
192
unsigned int get_be32(ByteIOContext *s);
193
uint64_t get_be64(ByteIOContext *s);
Fabrice Bellard's avatar
Fabrice Bellard committed
194

195
static inline int url_is_streamed(ByteIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
196 197 198 199 200
{
    return s->is_streamed;
}

int url_fdopen(ByteIOContext *s, URLContext *h);
201

202
/** @warning must be called before any I/O */
Fabrice Bellard's avatar
Fabrice Bellard committed
203
int url_setbufsize(ByteIOContext *s, int buf_size);
204

205
/** @note when opened as read/write, the buffers are only used for
206
   reading */
Fabrice Bellard's avatar
Fabrice Bellard committed
207 208 209
int url_fopen(ByteIOContext *s, const char *filename, int flags);
int url_fclose(ByteIOContext *s);
URLContext *url_fileno(ByteIOContext *s);
210

211
/**
212 213 214 215 216 217 218
 * Return the maximum packet size associated to packetized buffered file
 * handle. If the file is not packetized (stream like http or file on
 * disk), then 0 is returned.
 *
 * @param h buffered file handle
 * @return maximum packet size in bytes
 */
219
int url_fget_max_packet_size(ByteIOContext *s);
Fabrice Bellard's avatar
Fabrice Bellard committed
220

221
int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags);
222

223
/** return the written or read size */
Fabrice Bellard's avatar
Fabrice Bellard committed
224 225
int url_close_buf(ByteIOContext *s);

226
/**
227 228 229 230 231
 * Open a write only memory stream.
 *
 * @param s new IO context
 * @return zero if no error.
 */
232
int url_open_dyn_buf(ByteIOContext *s);
233

234
/**
235 236 237 238 239 240 241 242
 * Open a write only packetized memory stream with a maximum packet
 * size of 'max_packet_size'.  The stream is stored in a memory buffer
 * with a big endian 4 byte header giving the packet size in bytes.
 *
 * @param s new IO context
 * @param max_packet_size maximum packet size (must be > 0)
 * @return zero if no error.
 */
243
int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size);
244

245
/**
246 247 248 249 250 251
 * Return the written size and a pointer to the buffer. The buffer
 *  must be freed with av_free().
 * @param s IO context
 * @param pointer to a byte buffer
 * @return the length of the byte buffer
 */
252
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
253

254 255 256
unsigned long get_checksum(ByteIOContext *s);
void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);

Fabrice Bellard's avatar
Fabrice Bellard committed
257
/* udp.c */
258 259 260 261 262 263
int udp_set_remote_url(URLContext *h, const char *uri);
int udp_get_local_port(URLContext *h);
int udp_get_file_handle(URLContext *h);

#endif