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.

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