avio.h 18 KB

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