You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

513 lines
18KB

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