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.

621 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. * @return number of bytes written.
  292. */
  293. int avio_put_str16le(AVIOContext *s, const char *str);
  294. /**
  295. * Convert an UTF-8 string to UTF-16BE and write it.
  296. * @return number of bytes written.
  297. */
  298. int avio_put_str16be(AVIOContext *s, const char *str);
  299. /**
  300. * Passing this as the "whence" parameter to a seek function causes it to
  301. * return the filesize without seeking anywhere. Supporting this is optional.
  302. * If it is not supported then the seek function will return <0.
  303. */
  304. #define AVSEEK_SIZE 0x10000
  305. /**
  306. * Oring this flag as into the "whence" parameter to a seek function causes it to
  307. * seek by any means (like reopening and linear reading) or other normally unreasonable
  308. * means that can be extremely slow.
  309. * This may be ignored by the seek code.
  310. */
  311. #define AVSEEK_FORCE 0x20000
  312. /**
  313. * fseek() equivalent for AVIOContext.
  314. * @return new position or AVERROR.
  315. */
  316. int64_t avio_seek(AVIOContext *s, int64_t offset, int whence);
  317. /**
  318. * Skip given number of bytes forward
  319. * @return new position or AVERROR.
  320. */
  321. int64_t avio_skip(AVIOContext *s, int64_t offset);
  322. /**
  323. * ftell() equivalent for AVIOContext.
  324. * @return position or AVERROR.
  325. */
  326. static av_always_inline int64_t avio_tell(AVIOContext *s)
  327. {
  328. return avio_seek(s, 0, SEEK_CUR);
  329. }
  330. /**
  331. * Get the filesize.
  332. * @return filesize or AVERROR
  333. */
  334. int64_t avio_size(AVIOContext *s);
  335. /**
  336. * feof() equivalent for AVIOContext.
  337. * @return non zero if and only if end of file
  338. */
  339. int avio_feof(AVIOContext *s);
  340. #if FF_API_URL_FEOF
  341. /**
  342. * @deprecated use avio_feof()
  343. */
  344. attribute_deprecated
  345. int url_feof(AVIOContext *s);
  346. #endif
  347. /** @warning currently size is limited */
  348. int avio_printf(AVIOContext *s, const char *fmt, ...) av_printf_format(2, 3);
  349. /**
  350. * Force flushing of buffered data.
  351. *
  352. * For write streams, force the buffered data to be immediately written to the output,
  353. * without to wait to fill the internal buffer.
  354. *
  355. * For read streams, discard all currently buffered data, and advance the
  356. * reported file position to that of the underlying stream. This does not
  357. * read new data, and does not perform any seeks.
  358. */
  359. void avio_flush(AVIOContext *s);
  360. /**
  361. * Read size bytes from AVIOContext into buf.
  362. * @return number of bytes read or AVERROR
  363. */
  364. int avio_read(AVIOContext *s, unsigned char *buf, int size);
  365. /**
  366. * @name Functions for reading from AVIOContext
  367. * @{
  368. *
  369. * @note return 0 if EOF, so you cannot use it if EOF handling is
  370. * necessary
  371. */
  372. int avio_r8 (AVIOContext *s);
  373. unsigned int avio_rl16(AVIOContext *s);
  374. unsigned int avio_rl24(AVIOContext *s);
  375. unsigned int avio_rl32(AVIOContext *s);
  376. uint64_t avio_rl64(AVIOContext *s);
  377. unsigned int avio_rb16(AVIOContext *s);
  378. unsigned int avio_rb24(AVIOContext *s);
  379. unsigned int avio_rb32(AVIOContext *s);
  380. uint64_t avio_rb64(AVIOContext *s);
  381. /**
  382. * @}
  383. */
  384. /**
  385. * Read a string from pb into buf. The reading will terminate when either
  386. * a NULL character was encountered, maxlen bytes have been read, or nothing
  387. * more can be read from pb. The result is guaranteed to be NULL-terminated, it
  388. * will be truncated if buf is too small.
  389. * Note that the string is not interpreted or validated in any way, it
  390. * might get truncated in the middle of a sequence for multi-byte encodings.
  391. *
  392. * @return number of bytes read (is always <= maxlen).
  393. * If reading ends on EOF or error, the return value will be one more than
  394. * bytes actually read.
  395. */
  396. int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen);
  397. /**
  398. * Read a UTF-16 string from pb and convert it to UTF-8.
  399. * The reading will terminate when either a null or invalid character was
  400. * encountered or maxlen bytes have been read.
  401. * @return number of bytes read (is always <= maxlen)
  402. */
  403. int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen);
  404. int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen);
  405. /**
  406. * @name URL open modes
  407. * The flags argument to avio_open must be one of the following
  408. * constants, optionally ORed with other flags.
  409. * @{
  410. */
  411. #define AVIO_FLAG_READ 1 /**< read-only */
  412. #define AVIO_FLAG_WRITE 2 /**< write-only */
  413. #define AVIO_FLAG_READ_WRITE (AVIO_FLAG_READ|AVIO_FLAG_WRITE) /**< read-write pseudo flag */
  414. /**
  415. * @}
  416. */
  417. /**
  418. * Use non-blocking mode.
  419. * If this flag is set, operations on the context will return
  420. * AVERROR(EAGAIN) if they can not be performed immediately.
  421. * If this flag is not set, operations on the context will never return
  422. * AVERROR(EAGAIN).
  423. * Note that this flag does not affect the opening/connecting of the
  424. * context. Connecting a protocol will always block if necessary (e.g. on
  425. * network protocols) but never hang (e.g. on busy devices).
  426. * Warning: non-blocking protocols is work-in-progress; this flag may be
  427. * silently ignored.
  428. */
  429. #define AVIO_FLAG_NONBLOCK 8
  430. /**
  431. * Use direct mode.
  432. * avio_read and avio_write should if possible be satisfied directly
  433. * instead of going through a buffer, and avio_seek will always
  434. * call the underlying seek function directly.
  435. */
  436. #define AVIO_FLAG_DIRECT 0x8000
  437. /**
  438. * Create and initialize a AVIOContext for accessing the
  439. * resource indicated by url.
  440. * @note When the resource indicated by url has been opened in
  441. * read+write mode, the AVIOContext can be used only for writing.
  442. *
  443. * @param s Used to return the pointer to the created AVIOContext.
  444. * In case of failure the pointed to value is set to NULL.
  445. * @param url resource to access
  446. * @param flags flags which control how the resource indicated by url
  447. * is to be opened
  448. * @return >= 0 in case of success, a negative value corresponding to an
  449. * AVERROR code in case of failure
  450. */
  451. int avio_open(AVIOContext **s, const char *url, int flags);
  452. /**
  453. * Create and initialize a AVIOContext for accessing the
  454. * resource indicated by url.
  455. * @note When the resource indicated by url has been opened in
  456. * read+write mode, the AVIOContext can be used only for writing.
  457. *
  458. * @param s Used to return the pointer to the created AVIOContext.
  459. * In case of failure the pointed to value is set to NULL.
  460. * @param url resource to access
  461. * @param flags flags which control how the resource indicated by url
  462. * is to be opened
  463. * @param int_cb an interrupt callback to be used at the protocols level
  464. * @param options A dictionary filled with protocol-private options. On return
  465. * this parameter will be destroyed and replaced with a dict containing options
  466. * that were not found. May be NULL.
  467. * @return >= 0 in case of success, a negative value corresponding to an
  468. * AVERROR code in case of failure
  469. */
  470. int avio_open2(AVIOContext **s, const char *url, int flags,
  471. const AVIOInterruptCB *int_cb, AVDictionary **options);
  472. /**
  473. * Close the resource accessed by the AVIOContext s and free it.
  474. * This function can only be used if s was opened by avio_open().
  475. *
  476. * The internal buffer is automatically flushed before closing the
  477. * resource.
  478. *
  479. * @return 0 on success, an AVERROR < 0 on error.
  480. * @see avio_closep
  481. */
  482. int avio_close(AVIOContext *s);
  483. /**
  484. * Close the resource accessed by the AVIOContext *s, free it
  485. * and set the pointer pointing to it to NULL.
  486. * This function can only be used if s was opened by avio_open().
  487. *
  488. * The internal buffer is automatically flushed before closing the
  489. * resource.
  490. *
  491. * @return 0 on success, an AVERROR < 0 on error.
  492. * @see avio_close
  493. */
  494. int avio_closep(AVIOContext **s);
  495. /**
  496. * Open a write only memory stream.
  497. *
  498. * @param s new IO context
  499. * @return zero if no error.
  500. */
  501. int avio_open_dyn_buf(AVIOContext **s);
  502. /**
  503. * Return the written size and a pointer to the buffer. The buffer
  504. * must be freed with av_free().
  505. * Padding of FF_INPUT_BUFFER_PADDING_SIZE is added to the buffer.
  506. *
  507. * @param s IO context
  508. * @param pbuffer pointer to a byte buffer
  509. * @return the length of the byte buffer
  510. */
  511. int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
  512. /**
  513. * Iterate through names of available protocols.
  514. *
  515. * @param opaque A private pointer representing current protocol.
  516. * It must be a pointer to NULL on first iteration and will
  517. * be updated by successive calls to avio_enum_protocols.
  518. * @param output If set to 1, iterate over output protocols,
  519. * otherwise over input protocols.
  520. *
  521. * @return A static string containing the name of current protocol or NULL
  522. */
  523. const char *avio_enum_protocols(void **opaque, int output);
  524. /**
  525. * Pause and resume playing - only meaningful if using a network streaming
  526. * protocol (e.g. MMS).
  527. *
  528. * @param h IO context from which to call the read_pause function pointer
  529. * @param pause 1 for pause, 0 for resume
  530. */
  531. int avio_pause(AVIOContext *h, int pause);
  532. /**
  533. * Seek to a given timestamp relative to some component stream.
  534. * Only meaningful if using a network streaming protocol (e.g. MMS.).
  535. *
  536. * @param h IO context from which to call the seek function pointers
  537. * @param stream_index The stream index that the timestamp is relative to.
  538. * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
  539. * units from the beginning of the presentation.
  540. * If a stream_index >= 0 is used and the protocol does not support
  541. * seeking based on component streams, the call will fail.
  542. * @param timestamp timestamp in AVStream.time_base units
  543. * or if there is no stream specified then in AV_TIME_BASE units.
  544. * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
  545. * and AVSEEK_FLAG_ANY. The protocol may silently ignore
  546. * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
  547. * fail if used and not supported.
  548. * @return >= 0 on success
  549. * @see AVInputFormat::read_seek
  550. */
  551. int64_t avio_seek_time(AVIOContext *h, int stream_index,
  552. int64_t timestamp, int flags);
  553. /* Avoid a warning. The header can not be included because it breaks c++. */
  554. struct AVBPrint;
  555. /**
  556. * Read contents of h into print buffer, up to max_size bytes, or up to EOF.
  557. *
  558. * @return 0 for success (max_size bytes read or EOF reached), negative error
  559. * code otherwise
  560. */
  561. int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size);
  562. #endif /* AVFORMAT_AVIO_H */