avio.h 8.62 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

glantau's avatar
glantau committed
24 25
/* output byte stream handling */

kabi's avatar
kabi committed
26
typedef int64_t offset_t;
glantau's avatar
glantau committed
27 28 29 30 31

/* unbuffered I/O */

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

typedef struct URLContext URLContext;

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

#define URL_RDONLY 0
#define URL_WRONLY 1
53 54
#define URL_RDWR   2

bellard's avatar
bellard committed
55 56
typedef int URLInterruptCB(void);

glantau's avatar
glantau committed
57 58 59 60 61 62 63
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);
64
int url_get_max_packet_size(URLContext *h);
bellard's avatar
bellard committed
65 66
void url_get_filename(URLContext *h, char *buf, int buf_size);

bellard's avatar
bellard committed
67
/* the callback is called in blocking functions to test regulary if
68 69
   asynchronous interruption is needed. AVERROR(EINTR) is returned
   in this case by the interrupted function. 'NULL' means no interrupt
bellard's avatar
bellard committed
70 71 72
   callback is given. */
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);

glantau's avatar
glantau committed
73 74 75
/* not implemented */
int url_poll(URLPollEntry *poll_table, int n, int timeout);

michael's avatar
michael committed
76 77 78 79 80
/**
 * passing this as the "whence" parameter to a seek function causes it to
 * return the filesize without seeking anywhere, supporting this is optional
 * if its not supprted then the seek function will return <0
 */
81 82
#define AVSEEK_SIZE 0x10000

glantau's avatar
glantau committed
83 84 85 86 87 88 89 90 91 92 93
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;
bellard's avatar
bellard committed
94
extern URLInterruptCB *url_interrupt_cb;
glantau's avatar
glantau committed
95 96 97 98 99 100 101 102

int register_protocol(URLProtocol *protocol);

typedef struct {
    unsigned char *buffer;
    int buffer_size;
    unsigned char *buf_ptr, *buf_end;
    void *opaque;
kabi's avatar
kabi committed
103
    int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
michael's avatar
michael committed
104
    int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
105
    offset_t (*seek)(void *opaque, offset_t offset, int whence);
glantau's avatar
glantau committed
106 107 108 109 110
    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 */
    int is_streamed;
111
    int max_packet_size;
112 113 114
    unsigned long checksum;
    unsigned char *checksum_ptr;
    unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
michael's avatar
michael committed
115
    int error;         ///< contains the error code or 0 if no error happened
glantau's avatar
glantau committed
116 117 118 119 120 121 122
} ByteIOContext;

int init_put_byte(ByteIOContext *s,
                  unsigned char *buffer,
                  int buffer_size,
                  int write_flag,
                  void *opaque,
kabi's avatar
kabi committed
123
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
michael's avatar
michael committed
124
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
125
                  offset_t (*seek)(void *opaque, offset_t offset, int whence));
glantau's avatar
glantau committed
126 127

void put_byte(ByteIOContext *s, int b);
128
void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
kabi's avatar
kabi committed
129 130
void put_le64(ByteIOContext *s, uint64_t val);
void put_be64(ByteIOContext *s, uint64_t val);
glantau's avatar
glantau committed
131 132
void put_le32(ByteIOContext *s, unsigned int val);
void put_be32(ByteIOContext *s, unsigned int val);
aurel's avatar
aurel committed
133
void put_le24(ByteIOContext *s, unsigned int val);
michael's avatar
michael committed
134
void put_be24(ByteIOContext *s, unsigned int val);
glantau's avatar
glantau committed
135 136
void put_le16(ByteIOContext *s, unsigned int val);
void put_be16(ByteIOContext *s, unsigned int val);
kabi's avatar
kabi committed
137
void put_tag(ByteIOContext *s, const char *tag);
glantau's avatar
glantau committed
138

139
void put_strz(ByteIOContext *s, const char *buf);
140

glantau's avatar
glantau committed
141 142 143
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);
144
offset_t url_fsize(ByteIOContext *s);
glantau's avatar
glantau committed
145
int url_feof(ByteIOContext *s);
michael's avatar
michael committed
146
int url_ferror(ByteIOContext *s);
glantau's avatar
glantau committed
147

148
#define URL_EOF (-1)
149
/* NOTE: return URL_EOF (-1) if EOF */
150
int url_fgetc(ByteIOContext *s);
151 152

/* XXX: currently size is limited */
153
#ifdef __GNUC__
154
int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
155 156 157
#else
int url_fprintf(ByteIOContext *s, const char *fmt, ...);
#endif
158 159 160

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

glantau's avatar
glantau committed
163 164 165
void put_flush_packet(ByteIOContext *s);

int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
166
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
167 168 169

/* NOTE: return 0 if EOF, so you cannot use it if EOF handling is
   necessary */
glantau's avatar
glantau committed
170
int get_byte(ByteIOContext *s);
aurel's avatar
aurel committed
171
unsigned int get_le24(ByteIOContext *s);
glantau's avatar
glantau committed
172
unsigned int get_le32(ByteIOContext *s);
kabi's avatar
kabi committed
173
uint64_t get_le64(ByteIOContext *s);
glantau's avatar
glantau committed
174 175
unsigned int get_le16(ByteIOContext *s);

176
char *get_strz(ByteIOContext *s, char *buf, int maxlen);
glantau's avatar
glantau committed
177
unsigned int get_be16(ByteIOContext *s);
michael's avatar
michael committed
178
unsigned int get_be24(ByteIOContext *s);
glantau's avatar
glantau committed
179
unsigned int get_be32(ByteIOContext *s);
kabi's avatar
kabi committed
180
uint64_t get_be64(ByteIOContext *s);
glantau's avatar
glantau committed
181

182
static inline int url_is_streamed(ByteIOContext *s)
glantau's avatar
glantau committed
183 184 185 186 187
{
    return s->is_streamed;
}

int url_fdopen(ByteIOContext *s, URLContext *h);
188 189

/* XXX: must be called before any I/O */
glantau's avatar
glantau committed
190
int url_setbufsize(ByteIOContext *s, int buf_size);
191 192 193

/* NOTE: when opened as read/write, the buffers are only used for
   reading */
glantau's avatar
glantau committed
194 195 196
int url_fopen(ByteIOContext *s, const char *filename, int flags);
int url_fclose(ByteIOContext *s);
URLContext *url_fileno(ByteIOContext *s);
197 198 199 200 201 202 203 204 205

/*
 * 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
 */
206
int url_fget_max_packet_size(ByteIOContext *s);
glantau's avatar
glantau committed
207

kabi's avatar
kabi committed
208
int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags);
209 210

/* return the written or read size */
glantau's avatar
glantau committed
211 212
int url_close_buf(ByteIOContext *s);

213 214 215 216 217 218
/*
 * Open a write only memory stream.
 *
 * @param s new IO context
 * @return zero if no error.
 */
219
int url_open_dyn_buf(ByteIOContext *s);
220 221 222 223 224 225 226 227 228 229

/*
 * 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.
 */
230
int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size);
231 232 233 234 235 236 237 238

/*
 * 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
 */
kabi's avatar
kabi committed
239
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
240

241 242 243
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);

glantau's avatar
glantau committed
244 245 246 247 248 249
/* file.c */
extern URLProtocol file_protocol;
extern URLProtocol pipe_protocol;

/* udp.c */
extern URLProtocol udp_protocol;
250 251 252 253 254 255
int udp_set_remote_url(URLContext *h, const char *uri);
int udp_get_local_port(URLContext *h);
int udp_get_file_handle(URLContext *h);

/* tcp.c  */
extern URLProtocol tcp_protocol;
glantau's avatar
glantau committed
256 257 258

/* http.c */
extern URLProtocol http_protocol;
259 260 261

#endif