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.

275 lines
8.9KB

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