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.

209 lines
7.2KB

  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. char filename[1]; /* specified filename */
  33. };
  34. typedef struct URLContext URLContext;
  35. typedef struct URLPollEntry {
  36. URLContext *handle;
  37. int events;
  38. int revents;
  39. } URLPollEntry;
  40. #define URL_RDONLY 0
  41. #define URL_WRONLY 1
  42. #define URL_RDWR 2
  43. typedef int URLInterruptCB(void);
  44. int url_open(URLContext **h, const char *filename, int flags);
  45. int url_read(URLContext *h, unsigned char *buf, int size);
  46. int url_write(URLContext *h, unsigned char *buf, int size);
  47. offset_t url_seek(URLContext *h, offset_t pos, int whence);
  48. int url_close(URLContext *h);
  49. int url_exist(const char *filename);
  50. offset_t url_filesize(URLContext *h);
  51. int url_get_max_packet_size(URLContext *h);
  52. void url_get_filename(URLContext *h, char *buf, int buf_size);
  53. /* the callback is called in blocking functions to test regulary if
  54. asynchronous interruption is needed. -EINTR is returned in this
  55. case by the interrupted function. 'NULL' means no interrupt
  56. callback is given. */
  57. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
  58. /* not implemented */
  59. int url_poll(URLPollEntry *poll_table, int n, int timeout);
  60. /**
  61. * passing this as the "whence" parameter to a seek function causes it to
  62. * return the filesize without seeking anywhere, supporting this is optional
  63. * if its not supprted then the seek function will return <0
  64. */
  65. #define AVSEEK_SIZE 0x10000
  66. typedef struct URLProtocol {
  67. const char *name;
  68. int (*url_open)(URLContext *h, const char *filename, int flags);
  69. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  70. int (*url_write)(URLContext *h, unsigned char *buf, int size);
  71. offset_t (*url_seek)(URLContext *h, offset_t pos, int whence);
  72. int (*url_close)(URLContext *h);
  73. struct URLProtocol *next;
  74. } URLProtocol;
  75. extern URLProtocol *first_protocol;
  76. extern URLInterruptCB *url_interrupt_cb;
  77. int register_protocol(URLProtocol *protocol);
  78. typedef struct {
  79. unsigned char *buffer;
  80. int buffer_size;
  81. unsigned char *buf_ptr, *buf_end;
  82. void *opaque;
  83. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
  84. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
  85. offset_t (*seek)(void *opaque, offset_t offset, int whence);
  86. offset_t pos; /* position in the file of the current buffer */
  87. int must_flush; /* true if the next seek should flush */
  88. int eof_reached; /* true if eof reached */
  89. int write_flag; /* true if open for writing */
  90. int is_streamed;
  91. int max_packet_size;
  92. unsigned long checksum;
  93. unsigned char *checksum_ptr;
  94. unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
  95. int error; ///< contains the error code or 0 if no error happened
  96. } ByteIOContext;
  97. int init_put_byte(ByteIOContext *s,
  98. unsigned char *buffer,
  99. int buffer_size,
  100. int write_flag,
  101. void *opaque,
  102. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  103. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  104. offset_t (*seek)(void *opaque, offset_t offset, int whence));
  105. void put_byte(ByteIOContext *s, int b);
  106. void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
  107. void put_le64(ByteIOContext *s, uint64_t val);
  108. void put_be64(ByteIOContext *s, uint64_t val);
  109. void put_le32(ByteIOContext *s, unsigned int val);
  110. void put_be32(ByteIOContext *s, unsigned int val);
  111. void put_le24(ByteIOContext *s, unsigned int val);
  112. void put_be24(ByteIOContext *s, unsigned int val);
  113. void put_le16(ByteIOContext *s, unsigned int val);
  114. void put_be16(ByteIOContext *s, unsigned int val);
  115. void put_tag(ByteIOContext *s, const char *tag);
  116. void put_strz(ByteIOContext *s, const char *buf);
  117. offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
  118. void url_fskip(ByteIOContext *s, offset_t offset);
  119. offset_t url_ftell(ByteIOContext *s);
  120. offset_t url_fsize(ByteIOContext *s);
  121. int url_feof(ByteIOContext *s);
  122. int url_ferror(ByteIOContext *s);
  123. #define URL_EOF (-1)
  124. int url_fgetc(ByteIOContext *s);
  125. #ifdef __GNUC__
  126. int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
  127. #else
  128. int url_fprintf(ByteIOContext *s, const char *fmt, ...);
  129. #endif
  130. char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
  131. void put_flush_packet(ByteIOContext *s);
  132. int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
  133. int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
  134. int get_byte(ByteIOContext *s);
  135. unsigned int get_le24(ByteIOContext *s);
  136. unsigned int get_le32(ByteIOContext *s);
  137. uint64_t get_le64(ByteIOContext *s);
  138. unsigned int get_le16(ByteIOContext *s);
  139. char *get_strz(ByteIOContext *s, char *buf, int maxlen);
  140. unsigned int get_be16(ByteIOContext *s);
  141. unsigned int get_be24(ByteIOContext *s);
  142. unsigned int get_be32(ByteIOContext *s);
  143. uint64_t get_be64(ByteIOContext *s);
  144. static inline int url_is_streamed(ByteIOContext *s)
  145. {
  146. return s->is_streamed;
  147. }
  148. int url_fdopen(ByteIOContext *s, URLContext *h);
  149. int url_setbufsize(ByteIOContext *s, int buf_size);
  150. int url_fopen(ByteIOContext *s, const char *filename, int flags);
  151. int url_fclose(ByteIOContext *s);
  152. URLContext *url_fileno(ByteIOContext *s);
  153. int url_fget_max_packet_size(ByteIOContext *s);
  154. int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags);
  155. int url_close_buf(ByteIOContext *s);
  156. int url_open_dyn_buf(ByteIOContext *s);
  157. int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size);
  158. int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
  159. unsigned long get_checksum(ByteIOContext *s);
  160. void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
  161. /* file.c */
  162. extern URLProtocol file_protocol;
  163. extern URLProtocol pipe_protocol;
  164. /* udp.c */
  165. extern URLProtocol udp_protocol;
  166. int udp_set_remote_url(URLContext *h, const char *uri);
  167. int udp_get_local_port(URLContext *h);
  168. int udp_get_file_handle(URLContext *h);
  169. /* tcp.c */
  170. extern URLProtocol tcp_protocol;
  171. /* http.c */
  172. extern URLProtocol http_protocol;
  173. #endif