avio.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * unbuffered io for ffmpeg system
  3. * copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVIO_H
  22. #define AVIO_H
  23. /* output byte stream handling */
  24. typedef int64_t offset_t;
  25. /* unbuffered I/O */
  26. struct URLContext {
  27. struct URLProtocol *prot;
  28. int flags;
  29. int is_streamed; /**< true if streamed (no seek possible), default = false */
  30. int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
  31. void *priv_data;
  32. #if LIBAVFORMAT_VERSION_INT >= (52<<16)
  33. char *filename; /**< specified filename */
  34. #else
  35. char filename[1]; /**< specified filename */
  36. #endif
  37. };
  38. typedef struct URLContext URLContext;
  39. typedef struct URLPollEntry {
  40. URLContext *handle;
  41. int events;
  42. int revents;
  43. } URLPollEntry;
  44. #define URL_RDONLY 0
  45. #define URL_WRONLY 1
  46. #define URL_RDWR 2
  47. typedef int URLInterruptCB(void);
  48. int url_open(URLContext **h, const char *filename, int flags);
  49. int url_read(URLContext *h, unsigned char *buf, int size);
  50. int url_write(URLContext *h, unsigned char *buf, int size);
  51. offset_t url_seek(URLContext *h, offset_t pos, int whence);
  52. int url_close(URLContext *h);
  53. int url_exist(const char *filename);
  54. offset_t url_filesize(URLContext *h);
  55. /**
  56. * Return the maximum packet size associated to packetized file
  57. * handle. If the file is not packetized (stream like http or file on
  58. * disk), then 0 is returned.
  59. *
  60. * @param h file handle
  61. * @return maximum packet size in bytes
  62. */
  63. int url_get_max_packet_size(URLContext *h);
  64. void url_get_filename(URLContext *h, char *buf, int buf_size);
  65. /**
  66. * the callback is called in blocking functions to test regulary if
  67. * asynchronous interruption is needed. AVERROR(EINTR) is returned
  68. * in this case by the interrupted function. 'NULL' means no interrupt
  69. * callback is given. i
  70. */
  71. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
  72. /* not implemented */
  73. int url_poll(URLPollEntry *poll_table, int n, int timeout);
  74. /**
  75. * passing this as the "whence" parameter to a seek function causes it to
  76. * return the filesize without seeking anywhere, supporting this is optional
  77. * if its not supprted then the seek function will return <0
  78. */
  79. #define AVSEEK_SIZE 0x10000
  80. typedef struct URLProtocol {
  81. const char *name;
  82. int (*url_open)(URLContext *h, const char *filename, int flags);
  83. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  84. int (*url_write)(URLContext *h, unsigned char *buf, int size);
  85. offset_t (*url_seek)(URLContext *h, offset_t pos, int whence);
  86. int (*url_close)(URLContext *h);
  87. struct URLProtocol *next;
  88. } URLProtocol;
  89. extern URLProtocol *first_protocol;
  90. extern URLInterruptCB *url_interrupt_cb;
  91. int register_protocol(URLProtocol *protocol);
  92. typedef struct {
  93. unsigned char *buffer;
  94. int buffer_size;
  95. unsigned char *buf_ptr, *buf_end;
  96. void *opaque;
  97. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
  98. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
  99. offset_t (*seek)(void *opaque, offset_t offset, int whence);
  100. offset_t pos; /**< position in the file of the current buffer */
  101. int must_flush; /**< true if the next seek should flush */
  102. int eof_reached; /**< true if eof reached */
  103. int write_flag; /**< true if open for writing */
  104. int is_streamed;
  105. int max_packet_size;
  106. unsigned long checksum;
  107. unsigned char *checksum_ptr;
  108. unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
  109. int error; ///< contains the error code or 0 if no error happened
  110. } ByteIOContext;
  111. int init_put_byte(ByteIOContext *s,
  112. unsigned char *buffer,
  113. int buffer_size,
  114. int write_flag,
  115. void *opaque,
  116. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  117. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  118. offset_t (*seek)(void *opaque, offset_t offset, int whence));
  119. void put_byte(ByteIOContext *s, int b);
  120. void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
  121. void put_le64(ByteIOContext *s, uint64_t val);
  122. void put_be64(ByteIOContext *s, uint64_t val);
  123. void put_le32(ByteIOContext *s, unsigned int val);
  124. void put_be32(ByteIOContext *s, unsigned int val);
  125. void put_le24(ByteIOContext *s, unsigned int val);
  126. void put_be24(ByteIOContext *s, unsigned int val);
  127. void put_le16(ByteIOContext *s, unsigned int val);
  128. void put_be16(ByteIOContext *s, unsigned int val);
  129. void put_tag(ByteIOContext *s, const char *tag);
  130. void put_strz(ByteIOContext *s, const char *buf);
  131. offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
  132. void url_fskip(ByteIOContext *s, offset_t offset);
  133. offset_t url_ftell(ByteIOContext *s);
  134. offset_t url_fsize(ByteIOContext *s);
  135. int url_feof(ByteIOContext *s);
  136. int url_ferror(ByteIOContext *s);
  137. #define URL_EOF (-1)
  138. /** @note return URL_EOF (-1) if EOF */
  139. int url_fgetc(ByteIOContext *s);
  140. /** @warning currently size is limited */
  141. #ifdef __GNUC__
  142. int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
  143. #else
  144. int url_fprintf(ByteIOContext *s, const char *fmt, ...);
  145. #endif
  146. /** @note unlike fgets, the EOL character is not returned and a whole
  147. line is parsed. return NULL if first char read was EOF */
  148. char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
  149. void put_flush_packet(ByteIOContext *s);
  150. int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
  151. int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
  152. /** @note return 0 if EOF, so you cannot use it if EOF handling is
  153. necessary */
  154. int get_byte(ByteIOContext *s);
  155. unsigned int get_le24(ByteIOContext *s);
  156. unsigned int get_le32(ByteIOContext *s);
  157. uint64_t get_le64(ByteIOContext *s);
  158. unsigned int get_le16(ByteIOContext *s);
  159. char *get_strz(ByteIOContext *s, char *buf, int maxlen);
  160. unsigned int get_be16(ByteIOContext *s);
  161. unsigned int get_be24(ByteIOContext *s);
  162. unsigned int get_be32(ByteIOContext *s);
  163. uint64_t get_be64(ByteIOContext *s);
  164. static inline int url_is_streamed(ByteIOContext *s)
  165. {
  166. return s->is_streamed;
  167. }
  168. int url_fdopen(ByteIOContext *s, URLContext *h);
  169. /** @warning must be called before any I/O */
  170. int url_setbufsize(ByteIOContext *s, int buf_size);
  171. /** @note when opened as read/write, the buffers are only used for
  172. reading */
  173. int url_fopen(ByteIOContext *s, const char *filename, int flags);
  174. int url_fclose(ByteIOContext *s);
  175. URLContext *url_fileno(ByteIOContext *s);
  176. /**
  177. * Return the maximum packet size associated to packetized buffered file
  178. * handle. If the file is not packetized (stream like http or file on
  179. * disk), then 0 is returned.
  180. *
  181. * @param h buffered file handle
  182. * @return maximum packet size in bytes
  183. */
  184. int url_fget_max_packet_size(ByteIOContext *s);
  185. int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags);
  186. /** return the written or read size */
  187. int url_close_buf(ByteIOContext *s);
  188. /**
  189. * Open a write only memory stream.
  190. *
  191. * @param s new IO context
  192. * @return zero if no error.
  193. */
  194. int url_open_dyn_buf(ByteIOContext *s);
  195. /**
  196. * Open a write only packetized memory stream with a maximum packet
  197. * size of 'max_packet_size'. The stream is stored in a memory buffer
  198. * with a big endian 4 byte header giving the packet size in bytes.
  199. *
  200. * @param s new IO context
  201. * @param max_packet_size maximum packet size (must be > 0)
  202. * @return zero if no error.
  203. */
  204. int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size);
  205. /**
  206. * Return the written size and a pointer to the buffer. The buffer
  207. * must be freed with av_free().
  208. * @param s IO context
  209. * @param pointer to a byte buffer
  210. * @return the length of the byte buffer
  211. */
  212. int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
  213. unsigned long get_checksum(ByteIOContext *s);
  214. void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
  215. /* file.c */
  216. extern URLProtocol file_protocol;
  217. extern URLProtocol pipe_protocol;
  218. /* udp.c */
  219. extern URLProtocol udp_protocol;
  220. int udp_set_remote_url(URLContext *h, const char *uri);
  221. int udp_get_local_port(URLContext *h);
  222. int udp_get_file_handle(URLContext *h);
  223. /* tcp.c */
  224. extern URLProtocol tcp_protocol;
  225. /* http.c */
  226. extern URLProtocol http_protocol;
  227. #endif