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.

624 lines
21KB

  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
  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. #include "libavutil/log.h"
  32. #include "libavformat/version.h"
  33. /* unbuffered I/O */
  34. /**
  35. * URL Context.
  36. * New fields can be added to the end with minor version bumps.
  37. * Removal, reordering and changes to existing fields require a major
  38. * version bump.
  39. * sizeof(URLContext) must not be used outside libav*.
  40. */
  41. typedef struct URLContext {
  42. #if FF_API_URL_CLASS
  43. const AVClass *av_class; ///< information for av_log(). Set by url_open().
  44. #endif
  45. struct URLProtocol *prot;
  46. int flags;
  47. int is_streamed; /**< true if streamed (no seek possible), default = false */
  48. int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
  49. void *priv_data;
  50. char *filename; /**< specified URL */
  51. int is_connected;
  52. } URLContext;
  53. typedef struct URLPollEntry {
  54. URLContext *handle;
  55. int events;
  56. int revents;
  57. } URLPollEntry;
  58. /**
  59. * @defgroup open_modes URL open modes
  60. * The flags argument to url_open and cosins must be one of the following
  61. * constants, optionally ORed with other flags.
  62. * @{
  63. */
  64. #define URL_RDONLY 0 /**< read-only */
  65. #define URL_WRONLY 1 /**< write-only */
  66. #define URL_RDWR 2 /**< read-write */
  67. /**
  68. * @}
  69. */
  70. /**
  71. * Use non-blocking mode.
  72. * If this flag is set, operations on the context will return
  73. * AVERROR(EAGAIN) if they can not be performed immediately.
  74. * If this flag is not set, operations on the context will never return
  75. * AVERROR(EAGAIN).
  76. * Note that this flag does not affect the opening/connecting of the
  77. * context. Connecting a protocol will always block if necessary (e.g. on
  78. * network protocols) but never hang (e.g. on busy devices).
  79. * Warning: non-blocking protocols is work-in-progress; this flag may be
  80. * silently ignored.
  81. */
  82. #define URL_FLAG_NONBLOCK 4
  83. typedef int URLInterruptCB(void);
  84. /**
  85. * Create a URLContext for accessing to the resource indicated by
  86. * url, and open it using the URLProtocol up.
  87. *
  88. * @param puc pointer to the location where, in case of success, the
  89. * function puts the pointer to the created URLContext
  90. * @param flags flags which control how the resource indicated by url
  91. * is to be opened
  92. * @return 0 in case of success, a negative value corresponding to an
  93. * AVERROR code in case of failure
  94. */
  95. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  96. const char *url, int flags);
  97. /**
  98. * Create a URLContext for accessing to the resource indicated by
  99. * url, but do not initiate the connection yet.
  100. *
  101. * @param puc pointer to the location where, in case of success, the
  102. * function puts the pointer to the created URLContext
  103. * @param flags flags which control how the resource indicated by url
  104. * is to be opened
  105. * @return 0 in case of success, a negative value corresponding to an
  106. * AVERROR code in case of failure
  107. */
  108. int url_alloc(URLContext **h, const char *url, int flags);
  109. /**
  110. * Connect an URLContext that has been allocated by url_alloc
  111. */
  112. int url_connect(URLContext *h);
  113. /**
  114. * Create an URLContext for accessing to the resource indicated by
  115. * url, and open it.
  116. *
  117. * @param puc pointer to the location where, in case of success, the
  118. * function puts the pointer to the created URLContext
  119. * @param flags flags which control how the resource indicated by url
  120. * is to be opened
  121. * @return 0 in case of success, a negative value corresponding to an
  122. * AVERROR code in case of failure
  123. */
  124. int url_open(URLContext **h, const char *url, int flags);
  125. /**
  126. * Read up to size bytes from the resource accessed by h, and store
  127. * the read bytes in buf.
  128. *
  129. * @return The number of bytes actually read, or a negative value
  130. * corresponding to an AVERROR code in case of error. A value of zero
  131. * indicates that it is not possible to read more from the accessed
  132. * resource (except if the value of the size argument is also zero).
  133. */
  134. int url_read(URLContext *h, unsigned char *buf, int size);
  135. /**
  136. * Read as many bytes as possible (up to size), calling the
  137. * read function multiple times if necessary.
  138. * This makes special short-read handling in applications
  139. * unnecessary, if the return value is < size then it is
  140. * certain there was either an error or the end of file was reached.
  141. */
  142. int url_read_complete(URLContext *h, unsigned char *buf, int size);
  143. /**
  144. * Write size bytes from buf to the resource accessed by h.
  145. *
  146. * @return the number of bytes actually written, or a negative value
  147. * corresponding to an AVERROR code in case of failure
  148. */
  149. int url_write(URLContext *h, const unsigned char *buf, int size);
  150. /**
  151. * Passing this as the "whence" parameter to a seek function causes it to
  152. * return the filesize without seeking anywhere. Supporting this is optional.
  153. * If it is not supported then the seek function will return <0.
  154. */
  155. #define AVSEEK_SIZE 0x10000
  156. /**
  157. * Change the position that will be used by the next read/write
  158. * operation on the resource accessed by h.
  159. *
  160. * @param pos specifies the new position to set
  161. * @param whence specifies how pos should be interpreted, it must be
  162. * one of SEEK_SET (seek from the beginning), SEEK_CUR (seek from the
  163. * current position), SEEK_END (seek from the end), or AVSEEK_SIZE
  164. * (return the filesize of the requested resource, pos is ignored).
  165. * @return a negative value corresponding to an AVERROR code in case
  166. * of failure, or the resulting file position, measured in bytes from
  167. * the beginning of the file. You can use this feature together with
  168. * SEEK_CUR to read the current file position.
  169. */
  170. int64_t url_seek(URLContext *h, int64_t pos, int whence);
  171. /**
  172. * Close the resource accessed by the URLContext h, and free the
  173. * memory used by it.
  174. *
  175. * @return a negative value if an error condition occurred, 0
  176. * otherwise
  177. */
  178. int url_close(URLContext *h);
  179. /**
  180. * Return a non-zero value if the resource indicated by url
  181. * exists, 0 otherwise.
  182. */
  183. int url_exist(const char *url);
  184. /**
  185. * Return the filesize of the resource accessed by h, AVERROR(ENOSYS)
  186. * if the operation is not supported by h, or another negative value
  187. * corresponding to an AVERROR error code in case of failure.
  188. */
  189. int64_t url_filesize(URLContext *h);
  190. /**
  191. * Return the file descriptor associated with this URL. For RTP, this
  192. * will return only the RTP file descriptor, not the RTCP file descriptor.
  193. *
  194. * @return the file descriptor associated with this URL, or <0 on error.
  195. */
  196. int url_get_file_handle(URLContext *h);
  197. /**
  198. * Return the maximum packet size associated to packetized file
  199. * handle. If the file is not packetized (stream like HTTP or file on
  200. * disk), then 0 is returned.
  201. *
  202. * @param h file handle
  203. * @return maximum packet size in bytes
  204. */
  205. int url_get_max_packet_size(URLContext *h);
  206. /**
  207. * Copy the filename of the resource accessed by h to buf.
  208. *
  209. * @param buf_size size in bytes of buf
  210. */
  211. void url_get_filename(URLContext *h, char *buf, int buf_size);
  212. /**
  213. * The callback is called in blocking functions to test regulary if
  214. * asynchronous interruption is needed. AVERROR(EINTR) is returned
  215. * in this case by the interrupted function. 'NULL' means no interrupt
  216. * callback is given.
  217. */
  218. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
  219. /* not implemented */
  220. int url_poll(URLPollEntry *poll_table, int n, int timeout);
  221. /**
  222. * Pause and resume playing - only meaningful if using a network streaming
  223. * protocol (e.g. MMS).
  224. * @param pause 1 for pause, 0 for resume
  225. */
  226. int av_url_read_pause(URLContext *h, int pause);
  227. /**
  228. * Seek to a given timestamp relative to some component stream.
  229. * Only meaningful if using a network streaming protocol (e.g. MMS.).
  230. * @param stream_index The stream index that the timestamp is relative to.
  231. * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
  232. * units from the beginning of the presentation.
  233. * If a stream_index >= 0 is used and the protocol does not support
  234. * seeking based on component streams, the call will fail with ENOTSUP.
  235. * @param timestamp timestamp in AVStream.time_base units
  236. * or if there is no stream specified then in AV_TIME_BASE units.
  237. * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
  238. * and AVSEEK_FLAG_ANY. The protocol may silently ignore
  239. * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
  240. * fail with ENOTSUP if used and not supported.
  241. * @return >= 0 on success
  242. * @see AVInputFormat::read_seek
  243. */
  244. int64_t av_url_read_seek(URLContext *h, int stream_index,
  245. int64_t timestamp, int flags);
  246. /**
  247. * Oring this flag as into the "whence" parameter to a seek function causes it to
  248. * seek by any means (like reopening and linear reading) or other normally unreasonble
  249. * means that can be extreemly slow.
  250. * This may be ignored by the seek code.
  251. */
  252. #define AVSEEK_FORCE 0x20000
  253. typedef struct URLProtocol {
  254. const char *name;
  255. int (*url_open)(URLContext *h, const char *url, int flags);
  256. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  257. int (*url_write)(URLContext *h, const unsigned char *buf, int size);
  258. int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
  259. int (*url_close)(URLContext *h);
  260. struct URLProtocol *next;
  261. int (*url_read_pause)(URLContext *h, int pause);
  262. int64_t (*url_read_seek)(URLContext *h, int stream_index,
  263. int64_t timestamp, int flags);
  264. int (*url_get_file_handle)(URLContext *h);
  265. int priv_data_size;
  266. const AVClass *priv_data_class;
  267. } URLProtocol;
  268. #if FF_API_REGISTER_PROTOCOL
  269. extern URLProtocol *first_protocol;
  270. #endif
  271. extern URLInterruptCB *url_interrupt_cb;
  272. /**
  273. * If protocol is NULL, returns the first registered protocol,
  274. * if protocol is non-NULL, returns the next registered protocol after protocol,
  275. * or NULL if protocol is the last one.
  276. */
  277. URLProtocol *av_protocol_next(URLProtocol *p);
  278. #if FF_API_REGISTER_PROTOCOL
  279. /**
  280. * @deprecated Use av_register_protocol() instead.
  281. */
  282. attribute_deprecated int register_protocol(URLProtocol *protocol);
  283. /**
  284. * @deprecated Use av_register_protocol2() instead.
  285. */
  286. attribute_deprecated int av_register_protocol(URLProtocol *protocol);
  287. #endif
  288. /**
  289. * Register the URLProtocol protocol.
  290. *
  291. * @param size the size of the URLProtocol struct referenced
  292. */
  293. int av_register_protocol2(URLProtocol *protocol, int size);
  294. /**
  295. * Bytestream IO Context.
  296. * New fields can be added to the end with minor version bumps.
  297. * Removal, reordering and changes to existing fields require a major
  298. * version bump.
  299. * sizeof(ByteIOContext) must not be used outside libav*.
  300. */
  301. typedef struct {
  302. unsigned char *buffer;
  303. int buffer_size;
  304. unsigned char *buf_ptr, *buf_end;
  305. void *opaque;
  306. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
  307. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
  308. int64_t (*seek)(void *opaque, int64_t offset, int whence);
  309. int64_t pos; /**< position in the file of the current buffer */
  310. int must_flush; /**< true if the next seek should flush */
  311. int eof_reached; /**< true if eof reached */
  312. int write_flag; /**< true if open for writing */
  313. int is_streamed;
  314. int max_packet_size;
  315. unsigned long checksum;
  316. unsigned char *checksum_ptr;
  317. unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
  318. int error; ///< contains the error code or 0 if no error happened
  319. int (*read_pause)(void *opaque, int pause);
  320. int64_t (*read_seek)(void *opaque, int stream_index,
  321. int64_t timestamp, int flags);
  322. } ByteIOContext;
  323. int init_put_byte(ByteIOContext *s,
  324. unsigned char *buffer,
  325. int buffer_size,
  326. int write_flag,
  327. void *opaque,
  328. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  329. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  330. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  331. ByteIOContext *av_alloc_put_byte(
  332. unsigned char *buffer,
  333. int buffer_size,
  334. int write_flag,
  335. void *opaque,
  336. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  337. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  338. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  339. void put_byte(ByteIOContext *s, int b);
  340. void put_nbyte(ByteIOContext *s, int b, int count);
  341. void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
  342. void put_le64(ByteIOContext *s, uint64_t val);
  343. void put_be64(ByteIOContext *s, uint64_t val);
  344. void put_le32(ByteIOContext *s, unsigned int val);
  345. void put_be32(ByteIOContext *s, unsigned int val);
  346. void put_le24(ByteIOContext *s, unsigned int val);
  347. void put_be24(ByteIOContext *s, unsigned int val);
  348. void put_le16(ByteIOContext *s, unsigned int val);
  349. void put_be16(ByteIOContext *s, unsigned int val);
  350. void put_tag(ByteIOContext *s, const char *tag);
  351. #if FF_API_OLD_AVIO
  352. attribute_deprecated void put_strz(ByteIOContext *s, const char *buf);
  353. #endif
  354. /**
  355. * Write a NULL-terminated string.
  356. * @return number of bytes written.
  357. */
  358. int avio_put_str(ByteIOContext *s, const char *str);
  359. /**
  360. * Convert an UTF-8 string to UTF-16LE and write it.
  361. * @return number of bytes written.
  362. */
  363. int avio_put_str16le(ByteIOContext *s, const char *str);
  364. /**
  365. * fseek() equivalent for ByteIOContext.
  366. * @return new position or AVERROR.
  367. */
  368. int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence);
  369. /**
  370. * Skip given number of bytes forward.
  371. * @param offset number of bytes
  372. * @return 0 on success, <0 on error
  373. */
  374. int url_fskip(ByteIOContext *s, int64_t offset);
  375. /**
  376. * ftell() equivalent for ByteIOContext.
  377. * @return position or AVERROR.
  378. */
  379. int64_t url_ftell(ByteIOContext *s);
  380. /**
  381. * Get the filesize.
  382. * @return filesize or AVERROR
  383. */
  384. int64_t url_fsize(ByteIOContext *s);
  385. /**
  386. * feof() equivalent for ByteIOContext.
  387. * @return non zero if and only if end of file
  388. */
  389. int url_feof(ByteIOContext *s);
  390. int url_ferror(ByteIOContext *s);
  391. int av_url_read_fpause(ByteIOContext *h, int pause);
  392. int64_t av_url_read_fseek(ByteIOContext *h, int stream_index,
  393. int64_t timestamp, int flags);
  394. #define URL_EOF (-1)
  395. /** @note return URL_EOF (-1) if EOF */
  396. int url_fgetc(ByteIOContext *s);
  397. /** @warning currently size is limited */
  398. #ifdef __GNUC__
  399. int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
  400. #else
  401. int url_fprintf(ByteIOContext *s, const char *fmt, ...);
  402. #endif
  403. /** @note unlike fgets, the EOL character is not returned and a whole
  404. line is parsed. return NULL if first char read was EOF */
  405. char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
  406. void put_flush_packet(ByteIOContext *s);
  407. /**
  408. * Read size bytes from ByteIOContext into buf.
  409. * @return number of bytes read or AVERROR
  410. */
  411. int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
  412. /**
  413. * Read size bytes from ByteIOContext into buf.
  414. * This reads at most 1 packet. If that is not enough fewer bytes will be
  415. * returned.
  416. * @return number of bytes read or AVERROR
  417. */
  418. int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
  419. /** @note return 0 if EOF, so you cannot use it if EOF handling is
  420. necessary */
  421. int get_byte(ByteIOContext *s);
  422. unsigned int get_le24(ByteIOContext *s);
  423. unsigned int get_le32(ByteIOContext *s);
  424. uint64_t get_le64(ByteIOContext *s);
  425. unsigned int get_le16(ByteIOContext *s);
  426. /**
  427. * Read a UTF-16 string from pb and convert it to UTF-8.
  428. * The reading will terminate when either a null or invalid character was
  429. * encountered or maxlen bytes have been read.
  430. * @return number of bytes read (is always <= maxlen)
  431. */
  432. int avio_get_str16le(ByteIOContext *pb, int maxlen, char *buf, int buflen);
  433. int avio_get_str16be(ByteIOContext *pb, int maxlen, char *buf, int buflen);
  434. char *get_strz(ByteIOContext *s, char *buf, int maxlen);
  435. unsigned int get_be16(ByteIOContext *s);
  436. unsigned int get_be24(ByteIOContext *s);
  437. unsigned int get_be32(ByteIOContext *s);
  438. uint64_t get_be64(ByteIOContext *s);
  439. uint64_t ff_get_v(ByteIOContext *bc);
  440. static inline int url_is_streamed(ByteIOContext *s)
  441. {
  442. return s->is_streamed;
  443. }
  444. /**
  445. * Create and initialize a ByteIOContext for accessing the
  446. * resource referenced by the URLContext h.
  447. * @note When the URLContext h has been opened in read+write mode, the
  448. * ByteIOContext can be used only for writing.
  449. *
  450. * @param s Used to return the pointer to the created ByteIOContext.
  451. * In case of failure the pointed to value is set to NULL.
  452. * @return 0 in case of success, a negative value corresponding to an
  453. * AVERROR code in case of failure
  454. */
  455. int url_fdopen(ByteIOContext **s, URLContext *h);
  456. /** @warning must be called before any I/O */
  457. int url_setbufsize(ByteIOContext *s, int buf_size);
  458. #if FF_API_URL_RESETBUF
  459. /** Reset the buffer for reading or writing.
  460. * @note Will drop any data currently in the buffer without transmitting it.
  461. * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
  462. * to set up the buffer for writing. */
  463. int url_resetbuf(ByteIOContext *s, int flags);
  464. #endif
  465. /**
  466. * Rewind the ByteIOContext using the specified buffer containing the first buf_size bytes of the file.
  467. * Used after probing to avoid seeking.
  468. * Joins buf and s->buffer, taking any overlap into consideration.
  469. * @note s->buffer must overlap with buf or they can't be joined and the function fails
  470. * @note This function is NOT part of the public API
  471. *
  472. * @param s The read-only ByteIOContext to rewind
  473. * @param buf The probe buffer containing the first buf_size bytes of the file
  474. * @param buf_size The size of buf
  475. * @return 0 in case of success, a negative value corresponding to an
  476. * AVERROR code in case of failure
  477. */
  478. int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size);
  479. /**
  480. * Create and initialize a ByteIOContext for accessing the
  481. * resource indicated by url.
  482. * @note When the resource indicated by url has been opened in
  483. * read+write mode, the ByteIOContext can be used only for writing.
  484. *
  485. * @param s Used to return the pointer to the created ByteIOContext.
  486. * In case of failure the pointed to value is set to NULL.
  487. * @param flags flags which control how the resource indicated by url
  488. * is to be opened
  489. * @return 0 in case of success, a negative value corresponding to an
  490. * AVERROR code in case of failure
  491. */
  492. int url_fopen(ByteIOContext **s, const char *url, int flags);
  493. int url_fclose(ByteIOContext *s);
  494. URLContext *url_fileno(ByteIOContext *s);
  495. /**
  496. * Return the maximum packet size associated to packetized buffered file
  497. * handle. If the file is not packetized (stream like http or file on
  498. * disk), then 0 is returned.
  499. *
  500. * @param s buffered file handle
  501. * @return maximum packet size in bytes
  502. */
  503. int url_fget_max_packet_size(ByteIOContext *s);
  504. int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags);
  505. /** return the written or read size */
  506. int url_close_buf(ByteIOContext *s);
  507. /**
  508. * Open a write only memory stream.
  509. *
  510. * @param s new IO context
  511. * @return zero if no error.
  512. */
  513. int url_open_dyn_buf(ByteIOContext **s);
  514. /**
  515. * Open a write only packetized memory stream with a maximum packet
  516. * size of 'max_packet_size'. The stream is stored in a memory buffer
  517. * with a big endian 4 byte header giving the packet size in bytes.
  518. *
  519. * @param s new IO context
  520. * @param max_packet_size maximum packet size (must be > 0)
  521. * @return zero if no error.
  522. */
  523. int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size);
  524. /**
  525. * Return the written size and a pointer to the buffer. The buffer
  526. * must be freed with av_free(). If the buffer is opened with
  527. * url_open_dyn_buf, then padding of FF_INPUT_BUFFER_PADDING_SIZE is
  528. * added; if opened with url_open_dyn_packet_buf, no padding is added.
  529. *
  530. * @param s IO context
  531. * @param pbuffer pointer to a byte buffer
  532. * @return the length of the byte buffer
  533. */
  534. int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
  535. unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
  536. unsigned int len);
  537. unsigned long get_checksum(ByteIOContext *s);
  538. void init_checksum(ByteIOContext *s,
  539. unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
  540. unsigned long checksum);
  541. /* udp.c */
  542. int udp_set_remote_url(URLContext *h, const char *uri);
  543. int udp_get_local_port(URLContext *h);
  544. #if FF_API_UDP_GET_FILE
  545. int udp_get_file_handle(URLContext *h);
  546. #endif
  547. #endif /* AVFORMAT_AVIO_H */