annotate ffmpeg/libavformat/avio.h @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * copyright (c) 2001 Fabrice Bellard
yading@11 3 *
yading@11 4 * This file is part of FFmpeg.
yading@11 5 *
yading@11 6 * FFmpeg is free software; you can redistribute it and/or
yading@11 7 * modify it under the terms of the GNU Lesser General Public
yading@11 8 * License as published by the Free Software Foundation; either
yading@11 9 * version 2.1 of the License, or (at your option) any later version.
yading@11 10 *
yading@11 11 * FFmpeg is distributed in the hope that it will be useful,
yading@11 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 14 * Lesser General Public License for more details.
yading@11 15 *
yading@11 16 * You should have received a copy of the GNU Lesser General Public
yading@11 17 * License along with FFmpeg; if not, write to the Free Software
yading@11 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 19 */
yading@11 20 #ifndef AVFORMAT_AVIO_H
yading@11 21 #define AVFORMAT_AVIO_H
yading@11 22
yading@11 23 /**
yading@11 24 * @file
yading@11 25 * @ingroup lavf_io
yading@11 26 * Buffered I/O operations
yading@11 27 */
yading@11 28
yading@11 29 #include <stdint.h>
yading@11 30
yading@11 31 #include "libavutil/common.h"
yading@11 32 #include "libavutil/dict.h"
yading@11 33 #include "libavutil/log.h"
yading@11 34
yading@11 35 #include "libavformat/version.h"
yading@11 36
yading@11 37
yading@11 38 #define AVIO_SEEKABLE_NORMAL 0x0001 /**< Seeking works like for a local file */
yading@11 39
yading@11 40 /**
yading@11 41 * Callback for checking whether to abort blocking functions.
yading@11 42 * AVERROR_EXIT is returned in this case by the interrupted
yading@11 43 * function. During blocking operations, callback is called with
yading@11 44 * opaque as parameter. If the callback returns 1, the
yading@11 45 * blocking operation will be aborted.
yading@11 46 *
yading@11 47 * No members can be added to this struct without a major bump, if
yading@11 48 * new elements have been added after this struct in AVFormatContext
yading@11 49 * or AVIOContext.
yading@11 50 */
yading@11 51 typedef struct AVIOInterruptCB {
yading@11 52 int (*callback)(void*);
yading@11 53 void *opaque;
yading@11 54 } AVIOInterruptCB;
yading@11 55
yading@11 56 /**
yading@11 57 * Bytestream IO Context.
yading@11 58 * New fields can be added to the end with minor version bumps.
yading@11 59 * Removal, reordering and changes to existing fields require a major
yading@11 60 * version bump.
yading@11 61 * sizeof(AVIOContext) must not be used outside libav*.
yading@11 62 *
yading@11 63 * @note None of the function pointers in AVIOContext should be called
yading@11 64 * directly, they should only be set by the client application
yading@11 65 * when implementing custom I/O. Normally these are set to the
yading@11 66 * function pointers specified in avio_alloc_context()
yading@11 67 */
yading@11 68 typedef struct AVIOContext {
yading@11 69 /**
yading@11 70 * A class for private options.
yading@11 71 *
yading@11 72 * If this AVIOContext is created by avio_open2(), av_class is set and
yading@11 73 * passes the options down to protocols.
yading@11 74 *
yading@11 75 * If this AVIOContext is manually allocated, then av_class may be set by
yading@11 76 * the caller.
yading@11 77 *
yading@11 78 * warning -- this field can be NULL, be sure to not pass this AVIOContext
yading@11 79 * to any av_opt_* functions in that case.
yading@11 80 */
yading@11 81 const AVClass *av_class;
yading@11 82 unsigned char *buffer; /**< Start of the buffer. */
yading@11 83 int buffer_size; /**< Maximum buffer size */
yading@11 84 unsigned char *buf_ptr; /**< Current position in the buffer */
yading@11 85 unsigned char *buf_end; /**< End of the data, may be less than
yading@11 86 buffer+buffer_size if the read function returned
yading@11 87 less data than requested, e.g. for streams where
yading@11 88 no more data has been received yet. */
yading@11 89 void *opaque; /**< A private pointer, passed to the read/write/seek/...
yading@11 90 functions. */
yading@11 91 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
yading@11 92 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
yading@11 93 int64_t (*seek)(void *opaque, int64_t offset, int whence);
yading@11 94 int64_t pos; /**< position in the file of the current buffer */
yading@11 95 int must_flush; /**< true if the next seek should flush */
yading@11 96 int eof_reached; /**< true if eof reached */
yading@11 97 int write_flag; /**< true if open for writing */
yading@11 98 int max_packet_size;
yading@11 99 unsigned long checksum;
yading@11 100 unsigned char *checksum_ptr;
yading@11 101 unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
yading@11 102 int error; /**< contains the error code or 0 if no error happened */
yading@11 103 /**
yading@11 104 * Pause or resume playback for network streaming protocols - e.g. MMS.
yading@11 105 */
yading@11 106 int (*read_pause)(void *opaque, int pause);
yading@11 107 /**
yading@11 108 * Seek to a given timestamp in stream with the specified stream_index.
yading@11 109 * Needed for some network streaming protocols which don't support seeking
yading@11 110 * to byte position.
yading@11 111 */
yading@11 112 int64_t (*read_seek)(void *opaque, int stream_index,
yading@11 113 int64_t timestamp, int flags);
yading@11 114 /**
yading@11 115 * A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
yading@11 116 */
yading@11 117 int seekable;
yading@11 118
yading@11 119 /**
yading@11 120 * max filesize, used to limit allocations
yading@11 121 * This field is internal to libavformat and access from outside is not allowed.
yading@11 122 */
yading@11 123 int64_t maxsize;
yading@11 124
yading@11 125 /**
yading@11 126 * avio_read and avio_write should if possible be satisfied directly
yading@11 127 * instead of going through a buffer, and avio_seek will always
yading@11 128 * call the underlying seek function directly.
yading@11 129 */
yading@11 130 int direct;
yading@11 131
yading@11 132 /**
yading@11 133 * Bytes read statistic
yading@11 134 * This field is internal to libavformat and access from outside is not allowed.
yading@11 135 */
yading@11 136 int64_t bytes_read;
yading@11 137
yading@11 138 /**
yading@11 139 * seek statistic
yading@11 140 * This field is internal to libavformat and access from outside is not allowed.
yading@11 141 */
yading@11 142 int seek_count;
yading@11 143
yading@11 144 /**
yading@11 145 * writeout statistic
yading@11 146 * This field is internal to libavformat and access from outside is not allowed.
yading@11 147 */
yading@11 148 int writeout_count;
yading@11 149 } AVIOContext;
yading@11 150
yading@11 151 /* unbuffered I/O */
yading@11 152
yading@11 153 /**
yading@11 154 * Return AVIO_FLAG_* access flags corresponding to the access permissions
yading@11 155 * of the resource in url, or a negative value corresponding to an
yading@11 156 * AVERROR code in case of failure. The returned access flags are
yading@11 157 * masked by the value in flags.
yading@11 158 *
yading@11 159 * @note This function is intrinsically unsafe, in the sense that the
yading@11 160 * checked resource may change its existence or permission status from
yading@11 161 * one call to another. Thus you should not trust the returned value,
yading@11 162 * unless you are sure that no other processes are accessing the
yading@11 163 * checked resource.
yading@11 164 */
yading@11 165 int avio_check(const char *url, int flags);
yading@11 166
yading@11 167 /**
yading@11 168 * Allocate and initialize an AVIOContext for buffered I/O. It must be later
yading@11 169 * freed with av_free().
yading@11 170 *
yading@11 171 * @param buffer Memory block for input/output operations via AVIOContext.
yading@11 172 * The buffer must be allocated with av_malloc() and friends.
yading@11 173 * @param buffer_size The buffer size is very important for performance.
yading@11 174 * For protocols with fixed blocksize it should be set to this blocksize.
yading@11 175 * For others a typical size is a cache page, e.g. 4kb.
yading@11 176 * @param write_flag Set to 1 if the buffer should be writable, 0 otherwise.
yading@11 177 * @param opaque An opaque pointer to user-specific data.
yading@11 178 * @param read_packet A function for refilling the buffer, may be NULL.
yading@11 179 * @param write_packet A function for writing the buffer contents, may be NULL.
yading@11 180 * The function may not change the input buffers content.
yading@11 181 * @param seek A function for seeking to specified byte position, may be NULL.
yading@11 182 *
yading@11 183 * @return Allocated AVIOContext or NULL on failure.
yading@11 184 */
yading@11 185 AVIOContext *avio_alloc_context(
yading@11 186 unsigned char *buffer,
yading@11 187 int buffer_size,
yading@11 188 int write_flag,
yading@11 189 void *opaque,
yading@11 190 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
yading@11 191 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
yading@11 192 int64_t (*seek)(void *opaque, int64_t offset, int whence));
yading@11 193
yading@11 194 void avio_w8(AVIOContext *s, int b);
yading@11 195 void avio_write(AVIOContext *s, const unsigned char *buf, int size);
yading@11 196 void avio_wl64(AVIOContext *s, uint64_t val);
yading@11 197 void avio_wb64(AVIOContext *s, uint64_t val);
yading@11 198 void avio_wl32(AVIOContext *s, unsigned int val);
yading@11 199 void avio_wb32(AVIOContext *s, unsigned int val);
yading@11 200 void avio_wl24(AVIOContext *s, unsigned int val);
yading@11 201 void avio_wb24(AVIOContext *s, unsigned int val);
yading@11 202 void avio_wl16(AVIOContext *s, unsigned int val);
yading@11 203 void avio_wb16(AVIOContext *s, unsigned int val);
yading@11 204
yading@11 205 /**
yading@11 206 * Write a NULL-terminated string.
yading@11 207 * @return number of bytes written.
yading@11 208 */
yading@11 209 int avio_put_str(AVIOContext *s, const char *str);
yading@11 210
yading@11 211 /**
yading@11 212 * Convert an UTF-8 string to UTF-16LE and write it.
yading@11 213 * @return number of bytes written.
yading@11 214 */
yading@11 215 int avio_put_str16le(AVIOContext *s, const char *str);
yading@11 216
yading@11 217 /**
yading@11 218 * Passing this as the "whence" parameter to a seek function causes it to
yading@11 219 * return the filesize without seeking anywhere. Supporting this is optional.
yading@11 220 * If it is not supported then the seek function will return <0.
yading@11 221 */
yading@11 222 #define AVSEEK_SIZE 0x10000
yading@11 223
yading@11 224 /**
yading@11 225 * Oring this flag as into the "whence" parameter to a seek function causes it to
yading@11 226 * seek by any means (like reopening and linear reading) or other normally unreasonable
yading@11 227 * means that can be extremely slow.
yading@11 228 * This may be ignored by the seek code.
yading@11 229 */
yading@11 230 #define AVSEEK_FORCE 0x20000
yading@11 231
yading@11 232 /**
yading@11 233 * fseek() equivalent for AVIOContext.
yading@11 234 * @return new position or AVERROR.
yading@11 235 */
yading@11 236 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence);
yading@11 237
yading@11 238 /**
yading@11 239 * Skip given number of bytes forward
yading@11 240 * @return new position or AVERROR.
yading@11 241 */
yading@11 242 int64_t avio_skip(AVIOContext *s, int64_t offset);
yading@11 243
yading@11 244 /**
yading@11 245 * ftell() equivalent for AVIOContext.
yading@11 246 * @return position or AVERROR.
yading@11 247 */
yading@11 248 static av_always_inline int64_t avio_tell(AVIOContext *s)
yading@11 249 {
yading@11 250 return avio_seek(s, 0, SEEK_CUR);
yading@11 251 }
yading@11 252
yading@11 253 /**
yading@11 254 * Get the filesize.
yading@11 255 * @return filesize or AVERROR
yading@11 256 */
yading@11 257 int64_t avio_size(AVIOContext *s);
yading@11 258
yading@11 259 /**
yading@11 260 * feof() equivalent for AVIOContext.
yading@11 261 * @return non zero if and only if end of file
yading@11 262 */
yading@11 263 int url_feof(AVIOContext *s);
yading@11 264
yading@11 265 /** @warning currently size is limited */
yading@11 266 int avio_printf(AVIOContext *s, const char *fmt, ...) av_printf_format(2, 3);
yading@11 267
yading@11 268 /**
yading@11 269 * Force flushing of buffered data to the output s.
yading@11 270 *
yading@11 271 * Force the buffered data to be immediately written to the output,
yading@11 272 * without to wait to fill the internal buffer.
yading@11 273 */
yading@11 274 void avio_flush(AVIOContext *s);
yading@11 275
yading@11 276 /**
yading@11 277 * Read size bytes from AVIOContext into buf.
yading@11 278 * @return number of bytes read or AVERROR
yading@11 279 */
yading@11 280 int avio_read(AVIOContext *s, unsigned char *buf, int size);
yading@11 281
yading@11 282 /**
yading@11 283 * @name Functions for reading from AVIOContext
yading@11 284 * @{
yading@11 285 *
yading@11 286 * @note return 0 if EOF, so you cannot use it if EOF handling is
yading@11 287 * necessary
yading@11 288 */
yading@11 289 int avio_r8 (AVIOContext *s);
yading@11 290 unsigned int avio_rl16(AVIOContext *s);
yading@11 291 unsigned int avio_rl24(AVIOContext *s);
yading@11 292 unsigned int avio_rl32(AVIOContext *s);
yading@11 293 uint64_t avio_rl64(AVIOContext *s);
yading@11 294 unsigned int avio_rb16(AVIOContext *s);
yading@11 295 unsigned int avio_rb24(AVIOContext *s);
yading@11 296 unsigned int avio_rb32(AVIOContext *s);
yading@11 297 uint64_t avio_rb64(AVIOContext *s);
yading@11 298 /**
yading@11 299 * @}
yading@11 300 */
yading@11 301
yading@11 302 /**
yading@11 303 * Read a string from pb into buf. The reading will terminate when either
yading@11 304 * a NULL character was encountered, maxlen bytes have been read, or nothing
yading@11 305 * more can be read from pb. The result is guaranteed to be NULL-terminated, it
yading@11 306 * will be truncated if buf is too small.
yading@11 307 * Note that the string is not interpreted or validated in any way, it
yading@11 308 * might get truncated in the middle of a sequence for multi-byte encodings.
yading@11 309 *
yading@11 310 * @return number of bytes read (is always <= maxlen).
yading@11 311 * If reading ends on EOF or error, the return value will be one more than
yading@11 312 * bytes actually read.
yading@11 313 */
yading@11 314 int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen);
yading@11 315
yading@11 316 /**
yading@11 317 * Read a UTF-16 string from pb and convert it to UTF-8.
yading@11 318 * The reading will terminate when either a null or invalid character was
yading@11 319 * encountered or maxlen bytes have been read.
yading@11 320 * @return number of bytes read (is always <= maxlen)
yading@11 321 */
yading@11 322 int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen);
yading@11 323 int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen);
yading@11 324
yading@11 325
yading@11 326 /**
yading@11 327 * @name URL open modes
yading@11 328 * The flags argument to avio_open must be one of the following
yading@11 329 * constants, optionally ORed with other flags.
yading@11 330 * @{
yading@11 331 */
yading@11 332 #define AVIO_FLAG_READ 1 /**< read-only */
yading@11 333 #define AVIO_FLAG_WRITE 2 /**< write-only */
yading@11 334 #define AVIO_FLAG_READ_WRITE (AVIO_FLAG_READ|AVIO_FLAG_WRITE) /**< read-write pseudo flag */
yading@11 335 /**
yading@11 336 * @}
yading@11 337 */
yading@11 338
yading@11 339 /**
yading@11 340 * Use non-blocking mode.
yading@11 341 * If this flag is set, operations on the context will return
yading@11 342 * AVERROR(EAGAIN) if they can not be performed immediately.
yading@11 343 * If this flag is not set, operations on the context will never return
yading@11 344 * AVERROR(EAGAIN).
yading@11 345 * Note that this flag does not affect the opening/connecting of the
yading@11 346 * context. Connecting a protocol will always block if necessary (e.g. on
yading@11 347 * network protocols) but never hang (e.g. on busy devices).
yading@11 348 * Warning: non-blocking protocols is work-in-progress; this flag may be
yading@11 349 * silently ignored.
yading@11 350 */
yading@11 351 #define AVIO_FLAG_NONBLOCK 8
yading@11 352
yading@11 353 /**
yading@11 354 * Use direct mode.
yading@11 355 * avio_read and avio_write should if possible be satisfied directly
yading@11 356 * instead of going through a buffer, and avio_seek will always
yading@11 357 * call the underlying seek function directly.
yading@11 358 */
yading@11 359 #define AVIO_FLAG_DIRECT 0x8000
yading@11 360
yading@11 361 /**
yading@11 362 * Create and initialize a AVIOContext for accessing the
yading@11 363 * resource indicated by url.
yading@11 364 * @note When the resource indicated by url has been opened in
yading@11 365 * read+write mode, the AVIOContext can be used only for writing.
yading@11 366 *
yading@11 367 * @param s Used to return the pointer to the created AVIOContext.
yading@11 368 * In case of failure the pointed to value is set to NULL.
yading@11 369 * @param flags flags which control how the resource indicated by url
yading@11 370 * is to be opened
yading@11 371 * @return 0 in case of success, a negative value corresponding to an
yading@11 372 * AVERROR code in case of failure
yading@11 373 */
yading@11 374 int avio_open(AVIOContext **s, const char *url, int flags);
yading@11 375
yading@11 376 /**
yading@11 377 * Create and initialize a AVIOContext for accessing the
yading@11 378 * resource indicated by url.
yading@11 379 * @note When the resource indicated by url has been opened in
yading@11 380 * read+write mode, the AVIOContext can be used only for writing.
yading@11 381 *
yading@11 382 * @param s Used to return the pointer to the created AVIOContext.
yading@11 383 * In case of failure the pointed to value is set to NULL.
yading@11 384 * @param flags flags which control how the resource indicated by url
yading@11 385 * is to be opened
yading@11 386 * @param int_cb an interrupt callback to be used at the protocols level
yading@11 387 * @param options A dictionary filled with protocol-private options. On return
yading@11 388 * this parameter will be destroyed and replaced with a dict containing options
yading@11 389 * that were not found. May be NULL.
yading@11 390 * @return 0 in case of success, a negative value corresponding to an
yading@11 391 * AVERROR code in case of failure
yading@11 392 */
yading@11 393 int avio_open2(AVIOContext **s, const char *url, int flags,
yading@11 394 const AVIOInterruptCB *int_cb, AVDictionary **options);
yading@11 395
yading@11 396 /**
yading@11 397 * Close the resource accessed by the AVIOContext s and free it.
yading@11 398 * This function can only be used if s was opened by avio_open().
yading@11 399 *
yading@11 400 * The internal buffer is automatically flushed before closing the
yading@11 401 * resource.
yading@11 402 *
yading@11 403 * @return 0 on success, an AVERROR < 0 on error.
yading@11 404 * @see avio_closep
yading@11 405 */
yading@11 406 int avio_close(AVIOContext *s);
yading@11 407
yading@11 408 /**
yading@11 409 * Close the resource accessed by the AVIOContext *s, free it
yading@11 410 * and set the pointer pointing to it to NULL.
yading@11 411 * This function can only be used if s was opened by avio_open().
yading@11 412 *
yading@11 413 * The internal buffer is automatically flushed before closing the
yading@11 414 * resource.
yading@11 415 *
yading@11 416 * @return 0 on success, an AVERROR < 0 on error.
yading@11 417 * @see avio_close
yading@11 418 */
yading@11 419 int avio_closep(AVIOContext **s);
yading@11 420
yading@11 421
yading@11 422 /**
yading@11 423 * Open a write only memory stream.
yading@11 424 *
yading@11 425 * @param s new IO context
yading@11 426 * @return zero if no error.
yading@11 427 */
yading@11 428 int avio_open_dyn_buf(AVIOContext **s);
yading@11 429
yading@11 430 /**
yading@11 431 * Return the written size and a pointer to the buffer. The buffer
yading@11 432 * must be freed with av_free().
yading@11 433 * Padding of FF_INPUT_BUFFER_PADDING_SIZE is added to the buffer.
yading@11 434 *
yading@11 435 * @param s IO context
yading@11 436 * @param pbuffer pointer to a byte buffer
yading@11 437 * @return the length of the byte buffer
yading@11 438 */
yading@11 439 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
yading@11 440
yading@11 441 /**
yading@11 442 * Iterate through names of available protocols.
yading@11 443 *
yading@11 444 * @param opaque A private pointer representing current protocol.
yading@11 445 * It must be a pointer to NULL on first iteration and will
yading@11 446 * be updated by successive calls to avio_enum_protocols.
yading@11 447 * @param output If set to 1, iterate over output protocols,
yading@11 448 * otherwise over input protocols.
yading@11 449 *
yading@11 450 * @return A static string containing the name of current protocol or NULL
yading@11 451 */
yading@11 452 const char *avio_enum_protocols(void **opaque, int output);
yading@11 453
yading@11 454 /**
yading@11 455 * Pause and resume playing - only meaningful if using a network streaming
yading@11 456 * protocol (e.g. MMS).
yading@11 457 * @param pause 1 for pause, 0 for resume
yading@11 458 */
yading@11 459 int avio_pause(AVIOContext *h, int pause);
yading@11 460
yading@11 461 /**
yading@11 462 * Seek to a given timestamp relative to some component stream.
yading@11 463 * Only meaningful if using a network streaming protocol (e.g. MMS.).
yading@11 464 * @param stream_index The stream index that the timestamp is relative to.
yading@11 465 * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
yading@11 466 * units from the beginning of the presentation.
yading@11 467 * If a stream_index >= 0 is used and the protocol does not support
yading@11 468 * seeking based on component streams, the call will fail.
yading@11 469 * @param timestamp timestamp in AVStream.time_base units
yading@11 470 * or if there is no stream specified then in AV_TIME_BASE units.
yading@11 471 * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
yading@11 472 * and AVSEEK_FLAG_ANY. The protocol may silently ignore
yading@11 473 * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
yading@11 474 * fail if used and not supported.
yading@11 475 * @return >= 0 on success
yading@11 476 * @see AVInputFormat::read_seek
yading@11 477 */
yading@11 478 int64_t avio_seek_time(AVIOContext *h, int stream_index,
yading@11 479 int64_t timestamp, int flags);
yading@11 480
yading@11 481 #endif /* AVFORMAT_AVIO_H */