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.

200 lines
6.9KB

  1. /*
  2. * unbuffered io for ffmpeg system
  3. * copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #ifndef AVIO_H
  20. #define AVIO_H
  21. /* output byte stream handling */
  22. typedef int64_t offset_t;
  23. /* unbuffered I/O */
  24. struct URLContext {
  25. struct URLProtocol *prot;
  26. int flags;
  27. int is_streamed; /* true if streamed (no seek possible), default = false */
  28. int max_packet_size; /* if non zero, the stream is packetized with this max packet size */
  29. void *priv_data;
  30. char filename[1]; /* specified filename */
  31. };
  32. typedef struct URLContext URLContext;
  33. typedef struct URLPollEntry {
  34. URLContext *handle;
  35. int events;
  36. int revents;
  37. } URLPollEntry;
  38. #define URL_RDONLY 0
  39. #define URL_WRONLY 1
  40. #define URL_RDWR 2
  41. typedef int URLInterruptCB(void);
  42. int url_open(URLContext **h, const char *filename, int flags);
  43. int url_read(URLContext *h, unsigned char *buf, int size);
  44. int url_write(URLContext *h, unsigned char *buf, int size);
  45. offset_t url_seek(URLContext *h, offset_t pos, int whence);
  46. int url_close(URLContext *h);
  47. int url_exist(const char *filename);
  48. offset_t url_filesize(URLContext *h);
  49. int url_get_max_packet_size(URLContext *h);
  50. void url_get_filename(URLContext *h, char *buf, int buf_size);
  51. /* the callback is called in blocking functions to test regulary if
  52. asynchronous interruption is needed. -EINTR is returned in this
  53. case by the interrupted function. 'NULL' means no interrupt
  54. callback is given. */
  55. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
  56. /* not implemented */
  57. int url_poll(URLPollEntry *poll_table, int n, int timeout);
  58. typedef struct URLProtocol {
  59. const char *name;
  60. int (*url_open)(URLContext *h, const char *filename, int flags);
  61. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  62. int (*url_write)(URLContext *h, unsigned char *buf, int size);
  63. offset_t (*url_seek)(URLContext *h, offset_t pos, int whence);
  64. int (*url_close)(URLContext *h);
  65. struct URLProtocol *next;
  66. } URLProtocol;
  67. extern URLProtocol *first_protocol;
  68. extern URLInterruptCB *url_interrupt_cb;
  69. int register_protocol(URLProtocol *protocol);
  70. typedef struct {
  71. unsigned char *buffer;
  72. int buffer_size;
  73. unsigned char *buf_ptr, *buf_end;
  74. void *opaque;
  75. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
  76. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
  77. offset_t (*seek)(void *opaque, offset_t offset, int whence);
  78. offset_t pos; /* position in the file of the current buffer */
  79. int must_flush; /* true if the next seek should flush */
  80. int eof_reached; /* true if eof reached */
  81. int write_flag; /* true if open for writing */
  82. int is_streamed;
  83. int max_packet_size;
  84. unsigned long checksum;
  85. unsigned char *checksum_ptr;
  86. unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
  87. int error; ///< contains the error code or 0 if no error happened
  88. } ByteIOContext;
  89. int init_put_byte(ByteIOContext *s,
  90. unsigned char *buffer,
  91. int buffer_size,
  92. int write_flag,
  93. void *opaque,
  94. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  95. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  96. offset_t (*seek)(void *opaque, offset_t offset, int whence));
  97. void put_byte(ByteIOContext *s, int b);
  98. void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
  99. void put_le64(ByteIOContext *s, uint64_t val);
  100. void put_be64(ByteIOContext *s, uint64_t val);
  101. void put_le32(ByteIOContext *s, unsigned int val);
  102. void put_be32(ByteIOContext *s, unsigned int val);
  103. void put_le24(ByteIOContext *s, unsigned int val);
  104. void put_be24(ByteIOContext *s, unsigned int val);
  105. void put_le16(ByteIOContext *s, unsigned int val);
  106. void put_be16(ByteIOContext *s, unsigned int val);
  107. void put_tag(ByteIOContext *s, const char *tag);
  108. void put_strz(ByteIOContext *s, const char *buf);
  109. offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
  110. void url_fskip(ByteIOContext *s, offset_t offset);
  111. offset_t url_ftell(ByteIOContext *s);
  112. offset_t url_fsize(ByteIOContext *s);
  113. int url_feof(ByteIOContext *s);
  114. int url_ferror(ByteIOContext *s);
  115. #define URL_EOF (-1)
  116. int url_fgetc(ByteIOContext *s);
  117. #ifdef __GNUC__
  118. int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
  119. #else
  120. int url_fprintf(ByteIOContext *s, const char *fmt, ...);
  121. #endif
  122. char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
  123. void put_flush_packet(ByteIOContext *s);
  124. int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
  125. int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
  126. int get_byte(ByteIOContext *s);
  127. unsigned int get_le24(ByteIOContext *s);
  128. unsigned int get_le32(ByteIOContext *s);
  129. uint64_t get_le64(ByteIOContext *s);
  130. unsigned int get_le16(ByteIOContext *s);
  131. char *get_strz(ByteIOContext *s, char *buf, int maxlen);
  132. unsigned int get_be16(ByteIOContext *s);
  133. unsigned int get_be24(ByteIOContext *s);
  134. unsigned int get_be32(ByteIOContext *s);
  135. uint64_t get_be64(ByteIOContext *s);
  136. static inline int url_is_streamed(ByteIOContext *s)
  137. {
  138. return s->is_streamed;
  139. }
  140. int url_fdopen(ByteIOContext *s, URLContext *h);
  141. int url_setbufsize(ByteIOContext *s, int buf_size);
  142. int url_fopen(ByteIOContext *s, const char *filename, int flags);
  143. int url_fclose(ByteIOContext *s);
  144. URLContext *url_fileno(ByteIOContext *s);
  145. int url_fget_max_packet_size(ByteIOContext *s);
  146. int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags);
  147. int url_close_buf(ByteIOContext *s);
  148. int url_open_dyn_buf(ByteIOContext *s);
  149. int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size);
  150. int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
  151. unsigned long get_checksum(ByteIOContext *s);
  152. void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
  153. /* file.c */
  154. extern URLProtocol file_protocol;
  155. extern URLProtocol pipe_protocol;
  156. /* udp.c */
  157. extern URLProtocol udp_protocol;
  158. int udp_set_remote_url(URLContext *h, const char *uri);
  159. int udp_get_local_port(URLContext *h);
  160. int udp_get_file_handle(URLContext *h);
  161. /* tcp.c */
  162. extern URLProtocol tcp_protocol;
  163. /* http.c */
  164. extern URLProtocol http_protocol;
  165. #endif