avio.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. * 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. /* unbuffered I/O */
  33. /**
  34. * URL Context.
  35. * New fields can be added to the end with minor version bumps.
  36. * Removal, reordering and changes to existing fields require a major
  37. * version bump.
  38. * sizeof(URLContext) must not be used outside libav*.
  39. */
  40. typedef struct URLContext {
  41. #if LIBAVFORMAT_VERSION_MAJOR >= 53
  42. const AVClass *av_class; ///< information for av_log(). Set by url_open().
  43. #endif
  44. struct URLProtocol *prot;
  45. int flags;
  46. int is_streamed; /**< true if streamed (no seek possible), default = false */
  47. int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
  48. void *priv_data;
  49. char *filename; /**< specified URL */
  50. int is_connected;
  51. } URLContext;
  52. typedef struct URLPollEntry {
  53. URLContext *handle;
  54. int events;
  55. int revents;
  56. } URLPollEntry;
  57. #define URL_RDONLY 0
  58. #define URL_WRONLY 1
  59. #define URL_RDWR 2
  60. typedef int URLInterruptCB(void);
  61. /**
  62. * Create a URLContext for accessing to the resource indicated by
  63. * url, and open it using the URLProtocol up.
  64. *
  65. * @param puc pointer to the location where, in case of success, the
  66. * function puts the pointer to the created URLContext
  67. * @param flags flags which control how the resource indicated by url
  68. * is to be opened
  69. * @return 0 in case of success, a negative value corresponding to an
  70. * AVERROR code in case of failure
  71. */
  72. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  73. const char *url, int flags);
  74. /**
  75. * Create a URLContext for accessing to the resource indicated by
  76. * url, but do not initiate the connection yet.
  77. *
  78. * @param puc pointer to the location where, in case of success, the
  79. * function puts the pointer to the created URLContext
  80. * @param flags flags which control how the resource indicated by url
  81. * is to be opened
  82. * @return 0 in case of success, a negative value corresponding to an
  83. * AVERROR code in case of failure
  84. */
  85. int url_alloc(URLContext **h, const char *url, int flags);
  86. /**
  87. * Connect an URLContext that has been allocated by url_alloc
  88. */
  89. int url_connect(URLContext *h);
  90. /**
  91. * Create an URLContext for accessing to the resource indicated by
  92. * url, and open it.
  93. *
  94. * @param puc pointer to the location where, in case of success, the
  95. * function puts the pointer to the created URLContext
  96. * @param flags flags which control how the resource indicated by url
  97. * is to be opened
  98. * @return 0 in case of success, a negative value corresponding to an
  99. * AVERROR code in case of failure
  100. */
  101. int url_open(URLContext **h, const char *url, int flags);
  102. /**
  103. * Read up to size bytes from the resource accessed by h, and store
  104. * the read bytes in buf.
  105. *
  106. * @return The number of bytes actually read, or a negative value
  107. * corresponding to an AVERROR code in case of error. A value of zero
  108. * indicates that it is not possible to read more from the accessed
  109. * resource (except if the value of the size argument is also zero).
  110. */
  111. int url_read(URLContext *h, unsigned char *buf, int size);
  112. /**
  113. * Read as many bytes as possible (up to size), calling the
  114. * read function multiple times if necessary.
  115. * Will also retry if the read function returns AVERROR(EAGAIN).
  116. * This makes special short-read handling in applications
  117. * unnecessary, if the return value is < size then it is
  118. * certain there was either an error or the end of file was reached.
  119. */
  120. int url_read_complete(URLContext *h, unsigned char *buf, int size);
  121. int url_write(URLContext *h, const unsigned char *buf, int size);
  122. /**
  123. * Change the position that will be used by the next read/write
  124. * operation on the resource accessed by h.
  125. *
  126. * @param pos specifies the new position to set
  127. * @param whence specifies how pos should be interpreted, it must be
  128. * one of SEEK_SET (seek from the beginning), SEEK_CUR (seek from the
  129. * current position), SEEK_END (seek from the end), or AVSEEK_SIZE
  130. * (return the filesize of the requested resource, pos is ignored).
  131. * @return a negative value corresponding to an AVERROR code in case
  132. * of failure, or the resulting file position, measured in bytes from
  133. * the beginning of the file. You can use this feature together with
  134. * SEEK_CUR to read the current file position.
  135. */
  136. int64_t url_seek(URLContext *h, int64_t pos, int whence);
  137. /**
  138. * Close the resource accessed by the URLContext h, and free the
  139. * memory used by it.
  140. *
  141. * @return a negative value if an error condition occurred, 0
  142. * otherwise
  143. */
  144. int url_close(URLContext *h);
  145. /**
  146. * Return a non-zero value if the resource indicated by url
  147. * exists, 0 otherwise.
  148. */
  149. int url_exist(const char *url);
  150. int64_t url_filesize(URLContext *h);
  151. /**
  152. * Return the file descriptor associated with this URL. For RTP, this
  153. * will return only the RTP file descriptor, not the RTCP file descriptor.
  154. * To get both, use rtp_get_file_handles().
  155. *
  156. * @return the file descriptor associated with this URL, or <0 on error.
  157. */
  158. int url_get_file_handle(URLContext *h);
  159. /**
  160. * Return the maximum packet size associated to packetized file
  161. * handle. If the file is not packetized (stream like HTTP or file on
  162. * disk), then 0 is returned.
  163. *
  164. * @param h file handle
  165. * @return maximum packet size in bytes
  166. */
  167. int url_get_max_packet_size(URLContext *h);
  168. void url_get_filename(URLContext *h, char *buf, int buf_size);
  169. /**
  170. * The callback is called in blocking functions to test regulary if
  171. * asynchronous interruption is needed. AVERROR(EINTR) is returned
  172. * in this case by the interrupted function. 'NULL' means no interrupt
  173. * callback is given.
  174. */
  175. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
  176. /* not implemented */
  177. int url_poll(URLPollEntry *poll_table, int n, int timeout);
  178. /**
  179. * Pause and resume playing - only meaningful if using a network streaming
  180. * protocol (e.g. MMS).
  181. * @param pause 1 for pause, 0 for resume
  182. */
  183. int av_url_read_pause(URLContext *h, int pause);
  184. /**
  185. * Seek to a given timestamp relative to some component stream.
  186. * Only meaningful if using a network streaming protocol (e.g. MMS.).
  187. * @param stream_index The stream index that the timestamp is relative to.
  188. * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
  189. * units from the beginning of the presentation.
  190. * If a stream_index >= 0 is used and the protocol does not support
  191. * seeking based on component streams, the call will fail with ENOTSUP.
  192. * @param timestamp timestamp in AVStream.time_base units
  193. * or if there is no stream specified then in AV_TIME_BASE units.
  194. * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
  195. * and AVSEEK_FLAG_ANY. The protocol may silently ignore
  196. * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
  197. * fail with ENOTSUP if used and not supported.
  198. * @return >= 0 on success
  199. * @see AVInputFormat::read_seek
  200. */
  201. int64_t av_url_read_seek(URLContext *h, int stream_index,
  202. int64_t timestamp, int flags);
  203. /**
  204. * Passing this as the "whence" parameter to a seek function causes it to
  205. * return the filesize without seeking anywhere. Supporting this is optional.
  206. * If it is not supported then the seek function will return <0.
  207. */
  208. #define AVSEEK_SIZE 0x10000
  209. /**
  210. * Oring this flag as into the "whence" parameter to a seek function causes it to
  211. * seek by any means (like reopening and linear reading) or other normally unreasonble
  212. * means that can be extreemly slow.
  213. * This may be ignored by the seek code.
  214. */
  215. #define AVSEEK_FORCE 0x20000
  216. typedef struct URLProtocol {
  217. const char *name;
  218. int (*url_open)(URLContext *h, const char *url, int flags);
  219. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  220. int (*url_write)(URLContext *h, const unsigned char *buf, int size);
  221. int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
  222. int (*url_close)(URLContext *h);
  223. struct URLProtocol *next;
  224. int (*url_read_pause)(URLContext *h, int pause);
  225. int64_t (*url_read_seek)(URLContext *h, int stream_index,
  226. int64_t timestamp, int flags);
  227. int (*url_get_file_handle)(URLContext *h);
  228. int priv_data_size;
  229. const AVClass *priv_data_class;
  230. } URLProtocol;
  231. #if LIBAVFORMAT_VERSION_MAJOR < 53
  232. extern URLProtocol *first_protocol;
  233. #endif
  234. extern URLInterruptCB *url_interrupt_cb;
  235. /**
  236. * If protocol is NULL, returns the first registered protocol,
  237. * if protocol is non-NULL, returns the next registered protocol after protocol,
  238. * or NULL if protocol is the last one.
  239. */
  240. URLProtocol *av_protocol_next(URLProtocol *p);
  241. #if LIBAVFORMAT_VERSION_MAJOR < 53
  242. /**
  243. * @deprecated Use av_register_protocol() instead.
  244. */
  245. attribute_deprecated int register_protocol(URLProtocol *protocol);
  246. /**
  247. * @deprecated Use av_register_protocol2() instead.
  248. */
  249. attribute_deprecated int av_register_protocol(URLProtocol *protocol);
  250. #endif
  251. /**
  252. * Register the URLProtocol protocol.
  253. *
  254. * @param size the size of the URLProtocol struct referenced
  255. */
  256. int av_register_protocol2(URLProtocol *protocol, int size);
  257. /**
  258. * Bytestream IO Context.
  259. * New fields can be added to the end with minor version bumps.
  260. * Removal, reordering and changes to existing fields require a major
  261. * version bump.
  262. * sizeof(ByteIOContext) must not be used outside libav*.
  263. */
  264. typedef struct {
  265. unsigned char *buffer;
  266. int buffer_size;
  267. unsigned char *buf_ptr, *buf_end;
  268. void *opaque;
  269. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
  270. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
  271. int64_t (*seek)(void *opaque, int64_t offset, int whence);
  272. int64_t pos; /**< position in the file of the current buffer */
  273. int must_flush; /**< true if the next seek should flush */
  274. int eof_reached; /**< true if eof reached */
  275. int write_flag; /**< true if open for writing */
  276. int is_streamed;
  277. int max_packet_size;
  278. unsigned long checksum;
  279. unsigned char *checksum_ptr;
  280. unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
  281. int error; ///< contains the error code or 0 if no error happened
  282. int (*read_pause)(void *opaque, int pause);
  283. int64_t (*read_seek)(void *opaque, int stream_index,
  284. int64_t timestamp, int flags);
  285. } ByteIOContext;
  286. int init_put_byte(ByteIOContext *s,
  287. unsigned char *buffer,
  288. int buffer_size,
  289. int write_flag,
  290. void *opaque,
  291. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  292. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  293. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  294. ByteIOContext *av_alloc_put_byte(
  295. unsigned char *buffer,
  296. int buffer_size,
  297. int write_flag,
  298. void *opaque,
  299. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  300. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  301. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  302. void put_byte(ByteIOContext *s, int b);
  303. void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
  304. void put_le64(ByteIOContext *s, uint64_t val);
  305. void put_be64(ByteIOContext *s, uint64_t val);
  306. void put_le32(ByteIOContext *s, unsigned int val);
  307. void put_be32(ByteIOContext *s, unsigned int val);
  308. void put_le24(ByteIOContext *s, unsigned int val);
  309. void put_be24(ByteIOContext *s, unsigned int val);
  310. void put_le16(ByteIOContext *s, unsigned int val);
  311. void put_be16(ByteIOContext *s, unsigned int val);
  312. void put_tag(ByteIOContext *s, const char *tag);
  313. void put_strz(ByteIOContext *s, const char *buf);
  314. /**
  315. * fseek() equivalent for ByteIOContext.
  316. * @return new position or AVERROR.
  317. */
  318. int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence);
  319. /**
  320. * Skip given number of bytes forward.
  321. * @param offset number of bytes
  322. * @return 0 on success, <0 on error
  323. */
  324. int url_fskip(ByteIOContext *s, int64_t offset);
  325. /**
  326. * ftell() equivalent for ByteIOContext.
  327. * @return position or AVERROR.
  328. */
  329. int64_t url_ftell(ByteIOContext *s);
  330. /**
  331. * Get the filesize.
  332. * @return filesize or AVERROR
  333. */
  334. int64_t url_fsize(ByteIOContext *s);
  335. /**
  336. * feof() equivalent for ByteIOContext.
  337. * @return non zero if and only if end of file
  338. */
  339. int url_feof(ByteIOContext *s);
  340. int url_ferror(ByteIOContext *s);
  341. int av_url_read_fpause(ByteIOContext *h, int pause);
  342. int64_t av_url_read_fseek(ByteIOContext *h, int stream_index,
  343. int64_t timestamp, int flags);
  344. #define URL_EOF (-1)
  345. /** @note return URL_EOF (-1) if EOF */
  346. int url_fgetc(ByteIOContext *s);
  347. /** @warning currently size is limited */
  348. #ifdef __GNUC__
  349. int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
  350. #else
  351. int url_fprintf(ByteIOContext *s, const char *fmt, ...);
  352. #endif
  353. /** @note unlike fgets, the EOL character is not returned and a whole
  354. line is parsed. return NULL if first char read was EOF */
  355. char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
  356. void put_flush_packet(ByteIOContext *s);
  357. /**
  358. * Read size bytes from ByteIOContext into buf.
  359. * @return number of bytes read or AVERROR
  360. */
  361. int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
  362. /**
  363. * Read size bytes from ByteIOContext into buf.
  364. * This reads at most 1 packet. If that is not enough fewer bytes will be
  365. * returned.
  366. * @return number of bytes read or AVERROR
  367. */
  368. int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
  369. /** @note return 0 if EOF, so you cannot use it if EOF handling is
  370. necessary */
  371. int get_byte(ByteIOContext *s);
  372. unsigned int get_le24(ByteIOContext *s);
  373. unsigned int get_le32(ByteIOContext *s);
  374. uint64_t get_le64(ByteIOContext *s);
  375. unsigned int get_le16(ByteIOContext *s);
  376. char *get_strz(ByteIOContext *s, char *buf, int maxlen);
  377. unsigned int get_be16(ByteIOContext *s);
  378. unsigned int get_be24(ByteIOContext *s);
  379. unsigned int get_be32(ByteIOContext *s);
  380. uint64_t get_be64(ByteIOContext *s);
  381. uint64_t ff_get_v(ByteIOContext *bc);
  382. static inline int url_is_streamed(ByteIOContext *s)
  383. {
  384. return s->is_streamed;
  385. }
  386. /**
  387. * Create and initialize a ByteIOContext for accessing the
  388. * resource referenced by the URLContext h.
  389. * @note When the URLContext h has been opened in read+write mode, the
  390. * ByteIOContext can be used only for writing.
  391. *
  392. * @param s Used to return the pointer to the created ByteIOContext.
  393. * In case of failure the pointed to value is set to NULL.
  394. * @return 0 in case of success, a negative value corresponding to an
  395. * AVERROR code in case of failure
  396. */
  397. int url_fdopen(ByteIOContext **s, URLContext *h);
  398. /** @warning must be called before any I/O */
  399. int url_setbufsize(ByteIOContext *s, int buf_size);
  400. #if LIBAVFORMAT_VERSION_MAJOR < 53
  401. /** Reset the buffer for reading or writing.
  402. * @note Will drop any data currently in the buffer without transmitting it.
  403. * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
  404. * to set up the buffer for writing. */
  405. int url_resetbuf(ByteIOContext *s, int flags);
  406. #endif
  407. /**
  408. * Rewind the ByteIOContext using the specified buffer containing the first buf_size bytes of the file.
  409. * Used after probing to avoid seeking.
  410. * Joins buf and s->buffer, taking any overlap into consideration.
  411. * @note s->buffer must overlap with buf or they can't be joined and the function fails
  412. * @note This function is NOT part of the public API
  413. *
  414. * @param s The read-only ByteIOContext to rewind
  415. * @param buf The probe buffer containing the first buf_size bytes of the file
  416. * @param buf_size The size of buf
  417. * @return 0 in case of success, a negative value corresponding to an
  418. * AVERROR code in case of failure
  419. */
  420. int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size);
  421. /**
  422. * Create and initialize a ByteIOContext for accessing the
  423. * resource indicated by url.
  424. * @note When the resource indicated by url has been opened in
  425. * read+write mode, the ByteIOContext can be used only for writing.
  426. *
  427. * @param s Used to return the pointer to the created ByteIOContext.
  428. * In case of failure the pointed to value is set to NULL.
  429. * @param flags flags which control how the resource indicated by url
  430. * is to be opened
  431. * @return 0 in case of success, a negative value corresponding to an
  432. * AVERROR code in case of failure
  433. */
  434. int url_fopen(ByteIOContext **s, const char *url, int flags);
  435. int url_fclose(ByteIOContext *s);
  436. URLContext *url_fileno(ByteIOContext *s);
  437. /**
  438. * Return the maximum packet size associated to packetized buffered file
  439. * handle. If the file is not packetized (stream like http or file on
  440. * disk), then 0 is returned.
  441. *
  442. * @param s buffered file handle
  443. * @return maximum packet size in bytes
  444. */
  445. int url_fget_max_packet_size(ByteIOContext *s);
  446. int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags);
  447. /** return the written or read size */
  448. int url_close_buf(ByteIOContext *s);
  449. /**
  450. * Open a write only memory stream.
  451. *
  452. * @param s new IO context
  453. * @return zero if no error.
  454. */
  455. int url_open_dyn_buf(ByteIOContext **s);
  456. /**
  457. * Open a write only packetized memory stream with a maximum packet
  458. * size of 'max_packet_size'. The stream is stored in a memory buffer
  459. * with a big endian 4 byte header giving the packet size in bytes.
  460. *
  461. * @param s new IO context
  462. * @param max_packet_size maximum packet size (must be > 0)
  463. * @return zero if no error.
  464. */
  465. int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size);
  466. /**
  467. * Return the written size and a pointer to the buffer. The buffer
  468. * must be freed with av_free(). If the buffer is opened with
  469. * url_open_dyn_buf, then padding of FF_INPUT_BUFFER_PADDING_SIZE is
  470. * added; if opened with url_open_dyn_packet_buf, no padding is added.
  471. *
  472. * @param s IO context
  473. * @param pbuffer pointer to a byte buffer
  474. * @return the length of the byte buffer
  475. */
  476. int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
  477. unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
  478. unsigned int len);
  479. unsigned long get_checksum(ByteIOContext *s);
  480. void init_checksum(ByteIOContext *s,
  481. unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
  482. unsigned long checksum);
  483. /* udp.c */
  484. int udp_set_remote_url(URLContext *h, const char *uri);
  485. int udp_get_local_port(URLContext *h);
  486. #if (LIBAVFORMAT_VERSION_MAJOR <= 52)
  487. int udp_get_file_handle(URLContext *h);
  488. #endif
  489. #endif /* AVFORMAT_AVIO_H */