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.

633 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 even though 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. /**
  184. * Threshold to favor readahead over seek.
  185. * This is current internal only, do not use from outside.
  186. */
  187. int short_seek_threshold;
  188. } AVIOContext;
  189. /* unbuffered I/O */
  190. /**
  191. * Return the name of the protocol that will handle the passed URL.
  192. *
  193. * NULL is returned if no protocol could be found for the given URL.
  194. *
  195. * @return Name of the protocol or NULL.
  196. */
  197. const char *avio_find_protocol_name(const char *url);
  198. /**
  199. * Return AVIO_FLAG_* access flags corresponding to the access permissions
  200. * of the resource in url, or a negative value corresponding to an
  201. * AVERROR code in case of failure. The returned access flags are
  202. * masked by the value in flags.
  203. *
  204. * @note This function is intrinsically unsafe, in the sense that the
  205. * checked resource may change its existence or permission status from
  206. * one call to another. Thus you should not trust the returned value,
  207. * unless you are sure that no other processes are accessing the
  208. * checked resource.
  209. */
  210. int avio_check(const char *url, int flags);
  211. /**
  212. * Open directory for reading.
  213. *
  214. * @param s directory read context. Pointer to a NULL pointer must be passed.
  215. * @param url directory to be listed.
  216. * @param options A dictionary filled with protocol-private options. On return
  217. * this parameter will be destroyed and replaced with a dictionary
  218. * containing options that were not found. May be NULL.
  219. * @return >=0 on success or negative on error.
  220. */
  221. int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options);
  222. /**
  223. * Get next directory entry.
  224. *
  225. * Returned entry must be freed with avio_free_directory_entry(). In particular
  226. * it may outlive AVIODirContext.
  227. *
  228. * @param s directory read context.
  229. * @param[out] next next entry or NULL when no more entries.
  230. * @return >=0 on success or negative on error. End of list is not considered an
  231. * error.
  232. */
  233. int avio_read_dir(AVIODirContext *s, AVIODirEntry **next);
  234. /**
  235. * Close directory.
  236. *
  237. * @note Entries created using avio_read_dir() are not deleted and must be
  238. * freeded with avio_free_directory_entry().
  239. *
  240. * @param s directory read context.
  241. * @return >=0 on success or negative on error.
  242. */
  243. int avio_close_dir(AVIODirContext **s);
  244. /**
  245. * Free entry allocated by avio_read_dir().
  246. *
  247. * @param entry entry to be freed.
  248. */
  249. void avio_free_directory_entry(AVIODirEntry **entry);
  250. /**
  251. * Allocate and initialize an AVIOContext for buffered I/O. It must be later
  252. * freed with av_free().
  253. *
  254. * @param buffer Memory block for input/output operations via AVIOContext.
  255. * The buffer must be allocated with av_malloc() and friends.
  256. * It may be freed and replaced with a new buffer by libavformat.
  257. * AVIOContext.buffer holds the buffer currently in use,
  258. * which must be later freed with av_free().
  259. * @param buffer_size The buffer size is very important for performance.
  260. * For protocols with fixed blocksize it should be set to this blocksize.
  261. * For others a typical size is a cache page, e.g. 4kb.
  262. * @param write_flag Set to 1 if the buffer should be writable, 0 otherwise.
  263. * @param opaque An opaque pointer to user-specific data.
  264. * @param read_packet A function for refilling the buffer, may be NULL.
  265. * @param write_packet A function for writing the buffer contents, may be NULL.
  266. * The function may not change the input buffers content.
  267. * @param seek A function for seeking to specified byte position, may be NULL.
  268. *
  269. * @return Allocated AVIOContext or NULL on failure.
  270. */
  271. AVIOContext *avio_alloc_context(
  272. unsigned char *buffer,
  273. int buffer_size,
  274. int write_flag,
  275. void *opaque,
  276. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  277. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  278. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  279. void avio_w8(AVIOContext *s, int b);
  280. void avio_write(AVIOContext *s, const unsigned char *buf, int size);
  281. void avio_wl64(AVIOContext *s, uint64_t val);
  282. void avio_wb64(AVIOContext *s, uint64_t val);
  283. void avio_wl32(AVIOContext *s, unsigned int val);
  284. void avio_wb32(AVIOContext *s, unsigned int val);
  285. void avio_wl24(AVIOContext *s, unsigned int val);
  286. void avio_wb24(AVIOContext *s, unsigned int val);
  287. void avio_wl16(AVIOContext *s, unsigned int val);
  288. void avio_wb16(AVIOContext *s, unsigned int val);
  289. /**
  290. * Write a NULL-terminated string.
  291. * @return number of bytes written.
  292. */
  293. int avio_put_str(AVIOContext *s, const char *str);
  294. /**
  295. * Convert an UTF-8 string to UTF-16LE and write it.
  296. * @param s the AVIOContext
  297. * @param str NULL-terminated UTF-8 string
  298. *
  299. * @return number of bytes written.
  300. */
  301. int avio_put_str16le(AVIOContext *s, const char *str);
  302. /**
  303. * Convert an UTF-8 string to UTF-16BE and write it.
  304. * @param s the AVIOContext
  305. * @param str NULL-terminated UTF-8 string
  306. *
  307. * @return number of bytes written.
  308. */
  309. int avio_put_str16be(AVIOContext *s, const char *str);
  310. /**
  311. * Passing this as the "whence" parameter to a seek function causes it to
  312. * return the filesize without seeking anywhere. Supporting this is optional.
  313. * If it is not supported then the seek function will return <0.
  314. */
  315. #define AVSEEK_SIZE 0x10000
  316. /**
  317. * Oring this flag as into the "whence" parameter to a seek function causes it to
  318. * seek by any means (like reopening and linear reading) or other normally unreasonable
  319. * means that can be extremely slow.
  320. * This may be ignored by the seek code.
  321. */
  322. #define AVSEEK_FORCE 0x20000
  323. /**
  324. * fseek() equivalent for AVIOContext.
  325. * @return new position or AVERROR.
  326. */
  327. int64_t avio_seek(AVIOContext *s, int64_t offset, int whence);
  328. /**
  329. * Skip given number of bytes forward
  330. * @return new position or AVERROR.
  331. */
  332. int64_t avio_skip(AVIOContext *s, int64_t offset);
  333. /**
  334. * ftell() equivalent for AVIOContext.
  335. * @return position or AVERROR.
  336. */
  337. static av_always_inline int64_t avio_tell(AVIOContext *s)
  338. {
  339. return avio_seek(s, 0, SEEK_CUR);
  340. }
  341. /**
  342. * Get the filesize.
  343. * @return filesize or AVERROR
  344. */
  345. int64_t avio_size(AVIOContext *s);
  346. /**
  347. * feof() equivalent for AVIOContext.
  348. * @return non zero if and only if end of file
  349. */
  350. int avio_feof(AVIOContext *s);
  351. #if FF_API_URL_FEOF
  352. /**
  353. * @deprecated use avio_feof()
  354. */
  355. attribute_deprecated
  356. int url_feof(AVIOContext *s);
  357. #endif
  358. /** @warning currently size is limited */
  359. int avio_printf(AVIOContext *s, const char *fmt, ...) av_printf_format(2, 3);
  360. /**
  361. * Force flushing of buffered data.
  362. *
  363. * For write streams, force the buffered data to be immediately written to the output,
  364. * without to wait to fill the internal buffer.
  365. *
  366. * For read streams, discard all currently buffered data, and advance the
  367. * reported file position to that of the underlying stream. This does not
  368. * read new data, and does not perform any seeks.
  369. */
  370. void avio_flush(AVIOContext *s);
  371. /**
  372. * Read size bytes from AVIOContext into buf.
  373. * @return number of bytes read or AVERROR
  374. */
  375. int avio_read(AVIOContext *s, unsigned char *buf, int size);
  376. /**
  377. * @name Functions for reading from AVIOContext
  378. * @{
  379. *
  380. * @note return 0 if EOF, so you cannot use it if EOF handling is
  381. * necessary
  382. */
  383. int avio_r8 (AVIOContext *s);
  384. unsigned int avio_rl16(AVIOContext *s);
  385. unsigned int avio_rl24(AVIOContext *s);
  386. unsigned int avio_rl32(AVIOContext *s);
  387. uint64_t avio_rl64(AVIOContext *s);
  388. unsigned int avio_rb16(AVIOContext *s);
  389. unsigned int avio_rb24(AVIOContext *s);
  390. unsigned int avio_rb32(AVIOContext *s);
  391. uint64_t avio_rb64(AVIOContext *s);
  392. /**
  393. * @}
  394. */
  395. /**
  396. * Read a string from pb into buf. The reading will terminate when either
  397. * a NULL character was encountered, maxlen bytes have been read, or nothing
  398. * more can be read from pb. The result is guaranteed to be NULL-terminated, it
  399. * will be truncated if buf is too small.
  400. * Note that the string is not interpreted or validated in any way, it
  401. * might get truncated in the middle of a sequence for multi-byte encodings.
  402. *
  403. * @return number of bytes read (is always <= maxlen).
  404. * If reading ends on EOF or error, the return value will be one more than
  405. * bytes actually read.
  406. */
  407. int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen);
  408. /**
  409. * Read a UTF-16 string from pb and convert it to UTF-8.
  410. * The reading will terminate when either a null or invalid character was
  411. * encountered or maxlen bytes have been read.
  412. * @return number of bytes read (is always <= maxlen)
  413. */
  414. int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen);
  415. int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen);
  416. /**
  417. * @name URL open modes
  418. * The flags argument to avio_open must be one of the following
  419. * constants, optionally ORed with other flags.
  420. * @{
  421. */
  422. #define AVIO_FLAG_READ 1 /**< read-only */
  423. #define AVIO_FLAG_WRITE 2 /**< write-only */
  424. #define AVIO_FLAG_READ_WRITE (AVIO_FLAG_READ|AVIO_FLAG_WRITE) /**< read-write pseudo flag */
  425. /**
  426. * @}
  427. */
  428. /**
  429. * Use non-blocking mode.
  430. * If this flag is set, operations on the context will return
  431. * AVERROR(EAGAIN) if they can not be performed immediately.
  432. * If this flag is not set, operations on the context will never return
  433. * AVERROR(EAGAIN).
  434. * Note that this flag does not affect the opening/connecting of the
  435. * context. Connecting a protocol will always block if necessary (e.g. on
  436. * network protocols) but never hang (e.g. on busy devices).
  437. * Warning: non-blocking protocols is work-in-progress; this flag may be
  438. * silently ignored.
  439. */
  440. #define AVIO_FLAG_NONBLOCK 8
  441. /**
  442. * Use direct mode.
  443. * avio_read and avio_write should if possible be satisfied directly
  444. * instead of going through a buffer, and avio_seek will always
  445. * call the underlying seek function directly.
  446. */
  447. #define AVIO_FLAG_DIRECT 0x8000
  448. /**
  449. * Create and initialize a AVIOContext for accessing the
  450. * resource indicated by url.
  451. * @note When the resource indicated by url has been opened in
  452. * read+write mode, the AVIOContext can be used only for writing.
  453. *
  454. * @param s Used to return the pointer to the created AVIOContext.
  455. * In case of failure the pointed to value is set to NULL.
  456. * @param url resource to access
  457. * @param flags flags which control how the resource indicated by url
  458. * is to be opened
  459. * @return >= 0 in case of success, a negative value corresponding to an
  460. * AVERROR code in case of failure
  461. */
  462. int avio_open(AVIOContext **s, const char *url, int flags);
  463. /**
  464. * Create and initialize a AVIOContext for accessing the
  465. * resource indicated by url.
  466. * @note When the resource indicated by url has been opened in
  467. * read+write mode, the AVIOContext can be used only for writing.
  468. *
  469. * @param s Used to return the pointer to the created AVIOContext.
  470. * In case of failure the pointed to value is set to NULL.
  471. * @param url resource to access
  472. * @param flags flags which control how the resource indicated by url
  473. * is to be opened
  474. * @param int_cb an interrupt callback to be used at the protocols level
  475. * @param options A dictionary filled with protocol-private options. On return
  476. * this parameter will be destroyed and replaced with a dict containing options
  477. * that were not found. May be NULL.
  478. * @return >= 0 in case of success, a negative value corresponding to an
  479. * AVERROR code in case of failure
  480. */
  481. int avio_open2(AVIOContext **s, const char *url, int flags,
  482. const AVIOInterruptCB *int_cb, AVDictionary **options);
  483. /**
  484. * Close the resource accessed by the AVIOContext s and free it.
  485. * This function can only be used if s was opened by avio_open().
  486. *
  487. * The internal buffer is automatically flushed before closing the
  488. * resource.
  489. *
  490. * @return 0 on success, an AVERROR < 0 on error.
  491. * @see avio_closep
  492. */
  493. int avio_close(AVIOContext *s);
  494. /**
  495. * Close the resource accessed by the AVIOContext *s, free it
  496. * and set the pointer pointing to it to NULL.
  497. * This function can only be used if s was opened by avio_open().
  498. *
  499. * The internal buffer is automatically flushed before closing the
  500. * resource.
  501. *
  502. * @return 0 on success, an AVERROR < 0 on error.
  503. * @see avio_close
  504. */
  505. int avio_closep(AVIOContext **s);
  506. /**
  507. * Open a write only memory stream.
  508. *
  509. * @param s new IO context
  510. * @return zero if no error.
  511. */
  512. int avio_open_dyn_buf(AVIOContext **s);
  513. /**
  514. * Return the written size and a pointer to the buffer. The buffer
  515. * must be freed with av_free().
  516. * Padding of FF_INPUT_BUFFER_PADDING_SIZE is added to the buffer.
  517. *
  518. * @param s IO context
  519. * @param pbuffer pointer to a byte buffer
  520. * @return the length of the byte buffer
  521. */
  522. int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
  523. /**
  524. * Iterate through names of available protocols.
  525. *
  526. * @param opaque A private pointer representing current protocol.
  527. * It must be a pointer to NULL on first iteration and will
  528. * be updated by successive calls to avio_enum_protocols.
  529. * @param output If set to 1, iterate over output protocols,
  530. * otherwise over input protocols.
  531. *
  532. * @return A static string containing the name of current protocol or NULL
  533. */
  534. const char *avio_enum_protocols(void **opaque, int output);
  535. /**
  536. * Pause and resume playing - only meaningful if using a network streaming
  537. * protocol (e.g. MMS).
  538. *
  539. * @param h IO context from which to call the read_pause function pointer
  540. * @param pause 1 for pause, 0 for resume
  541. */
  542. int avio_pause(AVIOContext *h, int pause);
  543. /**
  544. * Seek to a given timestamp relative to some component stream.
  545. * Only meaningful if using a network streaming protocol (e.g. MMS.).
  546. *
  547. * @param h IO context from which to call the seek function pointers
  548. * @param stream_index The stream index that the timestamp is relative to.
  549. * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
  550. * units from the beginning of the presentation.
  551. * If a stream_index >= 0 is used and the protocol does not support
  552. * seeking based on component streams, the call will fail.
  553. * @param timestamp timestamp in AVStream.time_base units
  554. * or if there is no stream specified then in AV_TIME_BASE units.
  555. * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
  556. * and AVSEEK_FLAG_ANY. The protocol may silently ignore
  557. * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
  558. * fail if used and not supported.
  559. * @return >= 0 on success
  560. * @see AVInputFormat::read_seek
  561. */
  562. int64_t avio_seek_time(AVIOContext *h, int stream_index,
  563. int64_t timestamp, int flags);
  564. /* Avoid a warning. The header can not be included because it breaks c++. */
  565. struct AVBPrint;
  566. /**
  567. * Read contents of h into print buffer, up to max_size bytes, or up to EOF.
  568. *
  569. * @return 0 for success (max_size bytes read or EOF reached), negative error
  570. * code otherwise
  571. */
  572. int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size);
  573. #endif /* AVFORMAT_AVIO_H */