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.

618 lines
22KB

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