avio.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 libavformat/avio.h
  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. /* unbuffered I/O */
  32. /**
  33. * URL Context.
  34. * New fields can be added to the end with minor version bumps.
  35. * Removal, reordering and changes to existing fields require a major
  36. * version bump.
  37. * sizeof(URLContext) must not be used outside libav*.
  38. */
  39. struct URLContext {
  40. #if LIBAVFORMAT_VERSION_MAJOR >= 53
  41. const AVClass *av_class; ///< information for av_log(). Set by url_open().
  42. #endif
  43. struct URLProtocol *prot;
  44. int flags;
  45. int is_streamed; /**< true if streamed (no seek possible), default = false */
  46. int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
  47. void *priv_data;
  48. char *filename; /**< specified filename */
  49. };
  50. typedef struct URLContext URLContext;
  51. typedef struct URLPollEntry {
  52. URLContext *handle;
  53. int events;
  54. int revents;
  55. } URLPollEntry;
  56. #define URL_RDONLY 0
  57. #define URL_WRONLY 1
  58. #define URL_RDWR 2
  59. typedef int URLInterruptCB(void);
  60. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  61. const char *filename, int flags);
  62. int url_open(URLContext **h, const char *filename, int flags);
  63. int url_read(URLContext *h, unsigned char *buf, int size);
  64. int url_write(URLContext *h, unsigned char *buf, int size);
  65. int64_t url_seek(URLContext *h, int64_t pos, int whence);
  66. int url_close(URLContext *h);
  67. int url_exist(const char *filename);
  68. int64_t url_filesize(URLContext *h);
  69. /**
  70. * Return the maximum packet size associated to packetized file
  71. * handle. If the file is not packetized (stream like HTTP or file on
  72. * disk), then 0 is returned.
  73. *
  74. * @param h file handle
  75. * @return maximum packet size in bytes
  76. */
  77. int url_get_max_packet_size(URLContext *h);
  78. void url_get_filename(URLContext *h, char *buf, int buf_size);
  79. /**
  80. * The callback is called in blocking functions to test regulary if
  81. * asynchronous interruption is needed. AVERROR(EINTR) is returned
  82. * in this case by the interrupted function. 'NULL' means no interrupt
  83. * callback is given.
  84. */
  85. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
  86. /* not implemented */
  87. int url_poll(URLPollEntry *poll_table, int n, int timeout);
  88. /**
  89. * Pause and resume playing - only meaningful if using a network streaming
  90. * protocol (e.g. MMS).
  91. * @param pause 1 for pause, 0 for resume
  92. */
  93. int av_url_read_pause(URLContext *h, int pause);
  94. /**
  95. * Seek to a given timestamp relative to some component stream.
  96. * Only meaningful if using a network streaming protocol (e.g. MMS.).
  97. * @param stream_index The stream index that the timestamp is relative to.
  98. * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
  99. * units from the beginning of the presentation.
  100. * If a stream_index >= 0 is used and the protocol does not support
  101. * seeking based on component streams, the call will fail with ENOTSUP.
  102. * @param timestamp timestamp in AVStream.time_base units
  103. * or if there is no stream specified then in AV_TIME_BASE units.
  104. * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
  105. * and AVSEEK_FLAG_ANY. The protocol may silently ignore
  106. * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
  107. * fail with ENOTSUP if used and not supported.
  108. * @return >= 0 on success
  109. * @see AVInputFormat::read_seek
  110. */
  111. int64_t av_url_read_seek(URLContext *h, int stream_index,
  112. int64_t timestamp, int flags);
  113. /**
  114. * Passing this as the "whence" parameter to a seek function causes it to
  115. * return the filesize without seeking anywhere. Supporting this is optional.
  116. * If it is not supported then the seek function will return <0.
  117. */
  118. #define AVSEEK_SIZE 0x10000
  119. typedef struct URLProtocol {
  120. const char *name;
  121. int (*url_open)(URLContext *h, const char *filename, int flags);
  122. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  123. int (*url_write)(URLContext *h, unsigned char *buf, int size);
  124. int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
  125. int (*url_close)(URLContext *h);
  126. struct URLProtocol *next;
  127. int (*url_read_pause)(URLContext *h, int pause);
  128. int64_t (*url_read_seek)(URLContext *h, int stream_index,
  129. int64_t timestamp, int flags);
  130. } URLProtocol;
  131. #if LIBAVFORMAT_VERSION_MAJOR < 53
  132. extern URLProtocol *first_protocol;
  133. #endif
  134. extern URLInterruptCB *url_interrupt_cb;
  135. /**
  136. * If protocol is NULL, returns the first registered protocol,
  137. * if protocol is non-NULL, returns the next registered protocol after protocol,
  138. * or NULL if protocol is the last one.
  139. */
  140. URLProtocol *av_protocol_next(URLProtocol *p);
  141. #if LIBAVFORMAT_VERSION_MAJOR < 53
  142. /**
  143. * @deprecated Use av_register_protocol() instead.
  144. */
  145. attribute_deprecated int register_protocol(URLProtocol *protocol);
  146. #endif
  147. int av_register_protocol(URLProtocol *protocol);
  148. /**
  149. * Bytestream IO Context.
  150. * New fields can be added to the end with minor version bumps.
  151. * Removal, reordering and changes to existing fields require a major
  152. * version bump.
  153. * sizeof(ByteIOContext) must not be used outside libav*.
  154. */
  155. typedef struct {
  156. unsigned char *buffer;
  157. int buffer_size;
  158. unsigned char *buf_ptr, *buf_end;
  159. void *opaque;
  160. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
  161. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
  162. int64_t (*seek)(void *opaque, int64_t offset, int whence);
  163. int64_t pos; /**< position in the file of the current buffer */
  164. int must_flush; /**< true if the next seek should flush */
  165. int eof_reached; /**< true if eof reached */
  166. int write_flag; /**< true if open for writing */
  167. int is_streamed;
  168. int max_packet_size;
  169. unsigned long checksum;
  170. unsigned char *checksum_ptr;
  171. unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
  172. int error; ///< contains the error code or 0 if no error happened
  173. int (*read_pause)(void *opaque, int pause);
  174. int64_t (*read_seek)(void *opaque, int stream_index,
  175. int64_t timestamp, int flags);
  176. } ByteIOContext;
  177. int init_put_byte(ByteIOContext *s,
  178. unsigned char *buffer,
  179. int buffer_size,
  180. int write_flag,
  181. void *opaque,
  182. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  183. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  184. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  185. ByteIOContext *av_alloc_put_byte(
  186. unsigned char *buffer,
  187. int buffer_size,
  188. int write_flag,
  189. void *opaque,
  190. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  191. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  192. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  193. void put_byte(ByteIOContext *s, int b);
  194. void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
  195. void put_le64(ByteIOContext *s, uint64_t val);
  196. void put_be64(ByteIOContext *s, uint64_t val);
  197. void put_le32(ByteIOContext *s, unsigned int val);
  198. void put_be32(ByteIOContext *s, unsigned int val);
  199. void put_le24(ByteIOContext *s, unsigned int val);
  200. void put_be24(ByteIOContext *s, unsigned int val);
  201. void put_le16(ByteIOContext *s, unsigned int val);
  202. void put_be16(ByteIOContext *s, unsigned int val);
  203. void put_tag(ByteIOContext *s, const char *tag);
  204. void put_strz(ByteIOContext *s, const char *buf);
  205. /**
  206. * fseek() equivalent for ByteIOContext.
  207. * @return new position or AVERROR.
  208. */
  209. int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence);
  210. /**
  211. * Skip given number of bytes forward.
  212. * @param offset number of bytes
  213. */
  214. void url_fskip(ByteIOContext *s, int64_t offset);
  215. /**
  216. * ftell() equivalent for ByteIOContext.
  217. * @return position or AVERROR.
  218. */
  219. int64_t url_ftell(ByteIOContext *s);
  220. /**
  221. * Gets the filesize.
  222. * @return filesize or AVERROR
  223. */
  224. int64_t url_fsize(ByteIOContext *s);
  225. /**
  226. * feof() equivalent for ByteIOContext.
  227. * @return non zero if and only if end of file
  228. */
  229. int url_feof(ByteIOContext *s);
  230. int url_ferror(ByteIOContext *s);
  231. int av_url_read_fpause(ByteIOContext *h, int pause);
  232. int64_t av_url_read_fseek(ByteIOContext *h, int stream_index,
  233. int64_t timestamp, int flags);
  234. #define URL_EOF (-1)
  235. /** @note return URL_EOF (-1) if EOF */
  236. int url_fgetc(ByteIOContext *s);
  237. /** @warning currently size is limited */
  238. #ifdef __GNUC__
  239. int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
  240. #else
  241. int url_fprintf(ByteIOContext *s, const char *fmt, ...);
  242. #endif
  243. /** @note unlike fgets, the EOL character is not returned and a whole
  244. line is parsed. return NULL if first char read was EOF */
  245. char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
  246. void put_flush_packet(ByteIOContext *s);
  247. /**
  248. * Reads size bytes from ByteIOContext into buf.
  249. * @returns number of bytes read or AVERROR
  250. */
  251. int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
  252. /**
  253. * Reads size bytes from ByteIOContext into buf.
  254. * This reads at most 1 packet. If that is not enough fewer bytes will be
  255. * returned.
  256. * @returns number of bytes read or AVERROR
  257. */
  258. int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
  259. /** @note return 0 if EOF, so you cannot use it if EOF handling is
  260. necessary */
  261. int get_byte(ByteIOContext *s);
  262. unsigned int get_le24(ByteIOContext *s);
  263. unsigned int get_le32(ByteIOContext *s);
  264. uint64_t get_le64(ByteIOContext *s);
  265. unsigned int get_le16(ByteIOContext *s);
  266. char *get_strz(ByteIOContext *s, char *buf, int maxlen);
  267. unsigned int get_be16(ByteIOContext *s);
  268. unsigned int get_be24(ByteIOContext *s);
  269. unsigned int get_be32(ByteIOContext *s);
  270. uint64_t get_be64(ByteIOContext *s);
  271. uint64_t ff_get_v(ByteIOContext *bc);
  272. static inline int url_is_streamed(ByteIOContext *s)
  273. {
  274. return s->is_streamed;
  275. }
  276. /** @note when opened as read/write, the buffers are only used for
  277. writing */
  278. int url_fdopen(ByteIOContext **s, URLContext *h);
  279. /** @warning must be called before any I/O */
  280. int url_setbufsize(ByteIOContext *s, int buf_size);
  281. /** Reset the buffer for reading or writing.
  282. * @note Will drop any data currently in the buffer without transmitting it.
  283. * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
  284. * to set up the buffer for writing. */
  285. int url_resetbuf(ByteIOContext *s, int flags);
  286. /** @note when opened as read/write, the buffers are only used for
  287. writing */
  288. int url_fopen(ByteIOContext **s, const char *filename, int flags);
  289. int url_fclose(ByteIOContext *s);
  290. URLContext *url_fileno(ByteIOContext *s);
  291. /**
  292. * Return the maximum packet size associated to packetized buffered file
  293. * handle. If the file is not packetized (stream like http or file on
  294. * disk), then 0 is returned.
  295. *
  296. * @param s buffered file handle
  297. * @return maximum packet size in bytes
  298. */
  299. int url_fget_max_packet_size(ByteIOContext *s);
  300. int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags);
  301. /** return the written or read size */
  302. int url_close_buf(ByteIOContext *s);
  303. /**
  304. * Open a write only memory stream.
  305. *
  306. * @param s new IO context
  307. * @return zero if no error.
  308. */
  309. int url_open_dyn_buf(ByteIOContext **s);
  310. /**
  311. * Open a write only packetized memory stream with a maximum packet
  312. * size of 'max_packet_size'. The stream is stored in a memory buffer
  313. * with a big endian 4 byte header giving the packet size in bytes.
  314. *
  315. * @param s new IO context
  316. * @param max_packet_size maximum packet size (must be > 0)
  317. * @return zero if no error.
  318. */
  319. int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size);
  320. /**
  321. * Return the written size and a pointer to the buffer. The buffer
  322. * must be freed with av_free().
  323. * @param s IO context
  324. * @param pbuffer pointer to a byte buffer
  325. * @return the length of the byte buffer
  326. */
  327. int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
  328. unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
  329. unsigned int len);
  330. unsigned long get_checksum(ByteIOContext *s);
  331. void init_checksum(ByteIOContext *s,
  332. unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
  333. unsigned long checksum);
  334. /* udp.c */
  335. int udp_set_remote_url(URLContext *h, const char *uri);
  336. int udp_get_local_port(URLContext *h);
  337. int udp_get_file_handle(URLContext *h);
  338. #endif /* AVFORMAT_AVIO_H */