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.

569 lines
20KB

  1. /*
  2. * copyright (c) 2001 Fabrice Bellard
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. * unbuffered I/O operations
  25. *
  26. * @warning This file has to be considered an internal but installed
  27. * header, so it should not be directly included in your projects.
  28. */
  29. #include <stdint.h>
  30. #include "libavutil/common.h"
  31. #include "libavutil/log.h"
  32. #include "libavformat/version.h"
  33. /* unbuffered I/O */
  34. /**
  35. * URL Context.
  36. * New fields can be added to the end with minor version bumps.
  37. * Removal, reordering and changes to existing fields require a major
  38. * version bump.
  39. * sizeof(URLContext) must not be used outside libav*.
  40. */
  41. typedef struct URLContext {
  42. #if FF_API_URL_CLASS
  43. const AVClass *av_class; ///< information for av_log(). Set by url_open().
  44. #endif
  45. struct URLProtocol *prot;
  46. int flags;
  47. int is_streamed; /**< true if streamed (no seek possible), default = false */
  48. int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
  49. void *priv_data;
  50. char *filename; /**< specified URL */
  51. int is_connected;
  52. } URLContext;
  53. #if FF_API_OLD_AVIO
  54. typedef struct URLPollEntry {
  55. URLContext *handle;
  56. int events;
  57. int revents;
  58. } URLPollEntry;
  59. #endif
  60. /**
  61. * @defgroup open_modes URL open modes
  62. * The flags argument to url_open and cosins must be one of the following
  63. * constants, optionally ORed with other flags.
  64. * @{
  65. */
  66. #define URL_RDONLY 0 /**< read-only */
  67. #define URL_WRONLY 1 /**< write-only */
  68. #define URL_RDWR 2 /**< read-write */
  69. /**
  70. * @}
  71. */
  72. /**
  73. * Use non-blocking mode.
  74. * If this flag is set, operations on the context will return
  75. * AVERROR(EAGAIN) if they can not be performed immediately.
  76. * If this flag is not set, operations on the context will never return
  77. * AVERROR(EAGAIN).
  78. * Note that this flag does not affect the opening/connecting of the
  79. * context. Connecting a protocol will always block if necessary (e.g. on
  80. * network protocols) but never hang (e.g. on busy devices).
  81. * Warning: non-blocking protocols is work-in-progress; this flag may be
  82. * silently ignored.
  83. */
  84. #define URL_FLAG_NONBLOCK 4
  85. typedef int URLInterruptCB(void);
  86. #if FF_API_OLD_AVIO
  87. /**
  88. * @defgroup old_url_funcs Old url_* functions
  89. * @deprecated use the buffered API based on AVIOContext instead
  90. * @{
  91. */
  92. attribute_deprecated int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  93. const char *url, int flags);
  94. attribute_deprecated int url_alloc(URLContext **h, const char *url, int flags);
  95. attribute_deprecated int url_connect(URLContext *h);
  96. attribute_deprecated int url_open(URLContext **h, const char *url, int flags);
  97. attribute_deprecated int url_read(URLContext *h, unsigned char *buf, int size);
  98. attribute_deprecated int url_read_complete(URLContext *h, unsigned char *buf, int size);
  99. attribute_deprecated int url_write(URLContext *h, const unsigned char *buf, int size);
  100. attribute_deprecated int64_t url_seek(URLContext *h, int64_t pos, int whence);
  101. attribute_deprecated int url_close(URLContext *h);
  102. attribute_deprecated int64_t url_filesize(URLContext *h);
  103. attribute_deprecated int url_get_file_handle(URLContext *h);
  104. attribute_deprecated int url_get_max_packet_size(URLContext *h);
  105. attribute_deprecated void url_get_filename(URLContext *h, char *buf, int buf_size);
  106. attribute_deprecated int av_url_read_pause(URLContext *h, int pause);
  107. attribute_deprecated int64_t av_url_read_seek(URLContext *h, int stream_index,
  108. int64_t timestamp, int flags);
  109. #endif
  110. /**
  111. * Return a non-zero value if the resource indicated by url
  112. * exists, 0 otherwise.
  113. */
  114. int url_exist(const char *url);
  115. /**
  116. * The callback is called in blocking functions to test regulary if
  117. * asynchronous interruption is needed. AVERROR_EXIT is returned
  118. * in this case by the interrupted function. 'NULL' means no interrupt
  119. * callback is given.
  120. */
  121. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
  122. #if FF_API_OLD_AVIO
  123. /* not implemented */
  124. attribute_deprecated int url_poll(URLPollEntry *poll_table, int n, int timeout);
  125. #define URL_PROTOCOL_FLAG_NESTED_SCHEME 1 /*< The protocol name can be the first part of a nested protocol scheme */
  126. #endif
  127. typedef struct URLProtocol {
  128. const char *name;
  129. int (*url_open)(URLContext *h, const char *url, int flags);
  130. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  131. int (*url_write)(URLContext *h, const unsigned char *buf, int size);
  132. int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
  133. int (*url_close)(URLContext *h);
  134. struct URLProtocol *next;
  135. int (*url_read_pause)(URLContext *h, int pause);
  136. int64_t (*url_read_seek)(URLContext *h, int stream_index,
  137. int64_t timestamp, int flags);
  138. int (*url_get_file_handle)(URLContext *h);
  139. int priv_data_size;
  140. const AVClass *priv_data_class;
  141. int flags;
  142. } URLProtocol;
  143. #if FF_API_REGISTER_PROTOCOL
  144. extern URLProtocol *first_protocol;
  145. #endif
  146. extern URLInterruptCB *url_interrupt_cb;
  147. /**
  148. * If protocol is NULL, returns the first registered protocol,
  149. * if protocol is non-NULL, returns the next registered protocol after protocol,
  150. * or NULL if protocol is the last one.
  151. */
  152. URLProtocol *av_protocol_next(URLProtocol *p);
  153. #if FF_API_REGISTER_PROTOCOL
  154. /**
  155. * @deprecated Use av_register_protocol() instead.
  156. */
  157. attribute_deprecated int register_protocol(URLProtocol *protocol);
  158. /**
  159. * @deprecated Use av_register_protocol2() instead.
  160. */
  161. attribute_deprecated int av_register_protocol(URLProtocol *protocol);
  162. #endif
  163. /**
  164. * Register the URLProtocol protocol.
  165. *
  166. * @param size the size of the URLProtocol struct referenced
  167. */
  168. int av_register_protocol2(URLProtocol *protocol, int size);
  169. #define AVIO_SEEKABLE_NORMAL 0x0001 /**< Seeking works like for a local file */
  170. /**
  171. * @}
  172. */
  173. /**
  174. * Bytestream IO Context.
  175. * New fields can be added to the end with minor version bumps.
  176. * Removal, reordering and changes to existing fields require a major
  177. * version bump.
  178. * sizeof(AVIOContext) must not be used outside libav*.
  179. */
  180. typedef struct {
  181. unsigned char *buffer;
  182. int buffer_size;
  183. unsigned char *buf_ptr, *buf_end;
  184. void *opaque;
  185. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
  186. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
  187. int64_t (*seek)(void *opaque, int64_t offset, int whence);
  188. int64_t pos; /**< position in the file of the current buffer */
  189. int must_flush; /**< true if the next seek should flush */
  190. int eof_reached; /**< true if eof reached */
  191. int write_flag; /**< true if open for writing */
  192. #if FF_API_OLD_AVIO
  193. attribute_deprecated int is_streamed;
  194. #endif
  195. int max_packet_size;
  196. unsigned long checksum;
  197. unsigned char *checksum_ptr;
  198. unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
  199. int error; ///< contains the error code or 0 if no error happened
  200. int (*read_pause)(void *opaque, int pause);
  201. int64_t (*read_seek)(void *opaque, int stream_index,
  202. int64_t timestamp, int flags);
  203. /**
  204. * A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
  205. */
  206. int seekable;
  207. } AVIOContext;
  208. #if FF_API_OLD_AVIO
  209. typedef attribute_deprecated AVIOContext ByteIOContext;
  210. attribute_deprecated int init_put_byte(AVIOContext *s,
  211. unsigned char *buffer,
  212. int buffer_size,
  213. int write_flag,
  214. void *opaque,
  215. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  216. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  217. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  218. attribute_deprecated AVIOContext *av_alloc_put_byte(
  219. unsigned char *buffer,
  220. int buffer_size,
  221. int write_flag,
  222. void *opaque,
  223. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  224. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  225. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  226. /**
  227. * @defgroup old_avio_funcs Old put_/get_*() functions
  228. * @deprecated use the avio_ -prefixed functions instead.
  229. * @{
  230. */
  231. attribute_deprecated int get_buffer(AVIOContext *s, unsigned char *buf, int size);
  232. attribute_deprecated int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size);
  233. attribute_deprecated int get_byte(AVIOContext *s);
  234. attribute_deprecated unsigned int get_le16(AVIOContext *s);
  235. attribute_deprecated unsigned int get_le24(AVIOContext *s);
  236. attribute_deprecated unsigned int get_le32(AVIOContext *s);
  237. attribute_deprecated uint64_t get_le64(AVIOContext *s);
  238. attribute_deprecated unsigned int get_be16(AVIOContext *s);
  239. attribute_deprecated unsigned int get_be24(AVIOContext *s);
  240. attribute_deprecated unsigned int get_be32(AVIOContext *s);
  241. attribute_deprecated uint64_t get_be64(AVIOContext *s);
  242. attribute_deprecated void put_byte(AVIOContext *s, int b);
  243. attribute_deprecated void put_nbyte(AVIOContext *s, int b, int count);
  244. attribute_deprecated void put_buffer(AVIOContext *s, const unsigned char *buf, int size);
  245. attribute_deprecated void put_le64(AVIOContext *s, uint64_t val);
  246. attribute_deprecated void put_be64(AVIOContext *s, uint64_t val);
  247. attribute_deprecated void put_le32(AVIOContext *s, unsigned int val);
  248. attribute_deprecated void put_be32(AVIOContext *s, unsigned int val);
  249. attribute_deprecated void put_le24(AVIOContext *s, unsigned int val);
  250. attribute_deprecated void put_be24(AVIOContext *s, unsigned int val);
  251. attribute_deprecated void put_le16(AVIOContext *s, unsigned int val);
  252. attribute_deprecated void put_be16(AVIOContext *s, unsigned int val);
  253. attribute_deprecated void put_tag(AVIOContext *s, const char *tag);
  254. /**
  255. * @}
  256. */
  257. attribute_deprecated int av_url_read_fpause(AVIOContext *h, int pause);
  258. attribute_deprecated int64_t av_url_read_fseek (AVIOContext *h, int stream_index,
  259. int64_t timestamp, int flags);
  260. /**
  261. * @defgroup old_url_f_funcs Old url_f* functions
  262. * @deprecated use the avio_ -prefixed functions instead.
  263. * @{
  264. */
  265. attribute_deprecated int url_fopen( AVIOContext **s, const char *url, int flags);
  266. attribute_deprecated int url_fclose(AVIOContext *s);
  267. attribute_deprecated int64_t url_fseek(AVIOContext *s, int64_t offset, int whence);
  268. attribute_deprecated int url_fskip(AVIOContext *s, int64_t offset);
  269. attribute_deprecated int64_t url_ftell(AVIOContext *s);
  270. attribute_deprecated int64_t url_fsize(AVIOContext *s);
  271. #define URL_EOF (-1)
  272. attribute_deprecated int url_fgetc(AVIOContext *s);
  273. attribute_deprecated int url_setbufsize(AVIOContext *s, int buf_size);
  274. #ifdef __GNUC__
  275. attribute_deprecated int url_fprintf(AVIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
  276. #else
  277. attribute_deprecated int url_fprintf(AVIOContext *s, const char *fmt, ...);
  278. #endif
  279. attribute_deprecated void put_flush_packet(AVIOContext *s);
  280. attribute_deprecated int url_open_dyn_buf(AVIOContext **s);
  281. attribute_deprecated int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size);
  282. attribute_deprecated int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
  283. attribute_deprecated int url_fdopen(AVIOContext **s, URLContext *h);
  284. /**
  285. * @}
  286. */
  287. /**
  288. * @deprecated use AVIOContext.eof_reached
  289. */
  290. attribute_deprecated int url_feof(AVIOContext *s);
  291. attribute_deprecated int url_ferror(AVIOContext *s);
  292. attribute_deprecated int udp_set_remote_url(URLContext *h, const char *uri);
  293. attribute_deprecated int udp_get_local_port(URLContext *h);
  294. attribute_deprecated void init_checksum(AVIOContext *s,
  295. unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
  296. unsigned long checksum);
  297. attribute_deprecated unsigned long get_checksum(AVIOContext *s);
  298. #endif
  299. /**
  300. * Allocate and initialize an AVIOContext for buffered I/O. It must be later
  301. * freed with av_free().
  302. *
  303. * @param buffer Memory block for input/output operations via AVIOContext.
  304. * @param buffer_size The buffer size is very important for performance.
  305. * For protocols with fixed blocksize it should be set to this blocksize.
  306. * For others a typical size is a cache page, e.g. 4kb.
  307. * @param write_flag Set to 1 if the buffer should be writable, 0 otherwise.
  308. * @param opaque An opaque pointer to user-specific data.
  309. * @param read_packet A function for refilling the buffer, may be NULL.
  310. * @param write_packet A function for writing the buffer contents, may be NULL.
  311. * @param seek A function for seeking to specified byte position, may be NULL.
  312. *
  313. * @return Allocated AVIOContext or NULL on failure.
  314. */
  315. AVIOContext *avio_alloc_context(
  316. unsigned char *buffer,
  317. int buffer_size,
  318. int write_flag,
  319. void *opaque,
  320. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  321. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  322. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  323. void avio_w8(AVIOContext *s, int b);
  324. void avio_write(AVIOContext *s, const unsigned char *buf, int size);
  325. void avio_wl64(AVIOContext *s, uint64_t val);
  326. void avio_wb64(AVIOContext *s, uint64_t val);
  327. void avio_wl32(AVIOContext *s, unsigned int val);
  328. void avio_wb32(AVIOContext *s, unsigned int val);
  329. void avio_wl24(AVIOContext *s, unsigned int val);
  330. void avio_wb24(AVIOContext *s, unsigned int val);
  331. void avio_wl16(AVIOContext *s, unsigned int val);
  332. void avio_wb16(AVIOContext *s, unsigned int val);
  333. #if FF_API_OLD_AVIO
  334. attribute_deprecated void put_strz(AVIOContext *s, const char *buf);
  335. #endif
  336. /**
  337. * Write a NULL-terminated string.
  338. * @return number of bytes written.
  339. */
  340. int avio_put_str(AVIOContext *s, const char *str);
  341. /**
  342. * Convert an UTF-8 string to UTF-16LE and write it.
  343. * @return number of bytes written.
  344. */
  345. int avio_put_str16le(AVIOContext *s, const char *str);
  346. /**
  347. * Passing this as the "whence" parameter to a seek function causes it to
  348. * return the filesize without seeking anywhere. Supporting this is optional.
  349. * If it is not supported then the seek function will return <0.
  350. */
  351. #define AVSEEK_SIZE 0x10000
  352. /**
  353. * Oring this flag as into the "whence" parameter to a seek function causes it to
  354. * seek by any means (like reopening and linear reading) or other normally unreasonble
  355. * means that can be extreemly slow.
  356. * This may be ignored by the seek code.
  357. */
  358. #define AVSEEK_FORCE 0x20000
  359. /**
  360. * fseek() equivalent for AVIOContext.
  361. * @return new position or AVERROR.
  362. */
  363. int64_t avio_seek(AVIOContext *s, int64_t offset, int whence);
  364. /**
  365. * Skip given number of bytes forward
  366. * @return new position or AVERROR.
  367. */
  368. static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
  369. {
  370. return avio_seek(s, offset, SEEK_CUR);
  371. }
  372. /**
  373. * ftell() equivalent for AVIOContext.
  374. * @return position or AVERROR.
  375. */
  376. static av_always_inline int64_t avio_tell(AVIOContext *s)
  377. {
  378. return avio_seek(s, 0, SEEK_CUR);
  379. }
  380. /**
  381. * Get the filesize.
  382. * @return filesize or AVERROR
  383. */
  384. int64_t avio_size(AVIOContext *s);
  385. /** @warning currently size is limited */
  386. #ifdef __GNUC__
  387. int avio_printf(AVIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
  388. #else
  389. int avio_printf(AVIOContext *s, const char *fmt, ...);
  390. #endif
  391. #if FF_API_OLD_AVIO
  392. /** @note unlike fgets, the EOL character is not returned and a whole
  393. line is parsed. return NULL if first char read was EOF */
  394. attribute_deprecated char *url_fgets(AVIOContext *s, char *buf, int buf_size);
  395. #endif
  396. void avio_flush(AVIOContext *s);
  397. /**
  398. * Read size bytes from AVIOContext into buf.
  399. * @return number of bytes read or AVERROR
  400. */
  401. int avio_read(AVIOContext *s, unsigned char *buf, int size);
  402. /** @note return 0 if EOF, so you cannot use it if EOF handling is
  403. necessary */
  404. int avio_r8 (AVIOContext *s);
  405. unsigned int avio_rl16(AVIOContext *s);
  406. unsigned int avio_rl24(AVIOContext *s);
  407. unsigned int avio_rl32(AVIOContext *s);
  408. uint64_t avio_rl64(AVIOContext *s);
  409. /**
  410. * Read a string from pb into buf. The reading will terminate when either
  411. * a NULL character was encountered, maxlen bytes have been read, or nothing
  412. * more can be read from pb. The result is guaranteed to be NULL-terminated, it
  413. * will be truncated if buf is too small.
  414. * Note that the string is not interpreted or validated in any way, it
  415. * might get truncated in the middle of a sequence for multi-byte encodings.
  416. *
  417. * @return number of bytes read (is always <= maxlen).
  418. * If reading ends on EOF or error, the return value will be one more than
  419. * bytes actually read.
  420. */
  421. int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen);
  422. /**
  423. * Read a UTF-16 string from pb and convert it to UTF-8.
  424. * The reading will terminate when either a null or invalid character was
  425. * encountered or maxlen bytes have been read.
  426. * @return number of bytes read (is always <= maxlen)
  427. */
  428. int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen);
  429. int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen);
  430. #if FF_API_OLD_AVIO
  431. /**
  432. * @deprecated use avio_get_str instead
  433. */
  434. attribute_deprecated char *get_strz(AVIOContext *s, char *buf, int maxlen);
  435. #endif
  436. unsigned int avio_rb16(AVIOContext *s);
  437. unsigned int avio_rb24(AVIOContext *s);
  438. unsigned int avio_rb32(AVIOContext *s);
  439. uint64_t avio_rb64(AVIOContext *s);
  440. #if FF_API_OLD_AVIO
  441. /**
  442. * @deprecated Use AVIOContext.seekable field directly.
  443. */
  444. attribute_deprecated static inline int url_is_streamed(AVIOContext *s)
  445. {
  446. return !s->seekable;
  447. }
  448. #endif
  449. #if FF_API_URL_RESETBUF
  450. /** Reset the buffer for reading or writing.
  451. * @note Will drop any data currently in the buffer without transmitting it.
  452. * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
  453. * to set up the buffer for writing. */
  454. int url_resetbuf(AVIOContext *s, int flags);
  455. #endif
  456. /**
  457. * Create and initialize a AVIOContext for accessing the
  458. * resource indicated by url.
  459. * @note When the resource indicated by url has been opened in
  460. * read+write mode, the AVIOContext can be used only for writing.
  461. *
  462. * @param s Used to return the pointer to the created AVIOContext.
  463. * In case of failure the pointed to value is set to NULL.
  464. * @param flags flags which control how the resource indicated by url
  465. * is to be opened
  466. * @return 0 in case of success, a negative value corresponding to an
  467. * AVERROR code in case of failure
  468. */
  469. int avio_open(AVIOContext **s, const char *url, int flags);
  470. int avio_close(AVIOContext *s);
  471. #if FF_API_OLD_AVIO
  472. attribute_deprecated URLContext *url_fileno(AVIOContext *s);
  473. /**
  474. * @deprecated use AVIOContext.max_packet_size directly.
  475. */
  476. attribute_deprecated int url_fget_max_packet_size(AVIOContext *s);
  477. attribute_deprecated int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags);
  478. /** return the written or read size */
  479. attribute_deprecated int url_close_buf(AVIOContext *s);
  480. #endif
  481. /**
  482. * Open a write only memory stream.
  483. *
  484. * @param s new IO context
  485. * @return zero if no error.
  486. */
  487. int avio_open_dyn_buf(AVIOContext **s);
  488. /**
  489. * Return the written size and a pointer to the buffer. The buffer
  490. * must be freed with av_free().
  491. * Padding of FF_INPUT_BUFFER_PADDING_SIZE is added to the buffer.
  492. *
  493. * @param s IO context
  494. * @param pbuffer pointer to a byte buffer
  495. * @return the length of the byte buffer
  496. */
  497. int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
  498. #if FF_API_UDP_GET_FILE
  499. int udp_get_file_handle(URLContext *h);
  500. #endif
  501. #endif /* AVFORMAT_AVIO_H */