FFmpeg
avio.h
Go to the documentation of this file.
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 
29 #include <stdint.h>
30 
31 #include "libavutil/common.h"
32 #include "libavutil/dict.h"
33 #include "libavutil/log.h"
34 
35 #include "libavformat/version.h"
36 
40 #define AVIO_SEEKABLE_NORMAL (1 << 0)
41 
45 #define AVIO_SEEKABLE_TIME (1 << 1)
46 
58 typedef struct AVIOInterruptCB {
59  int (*callback)(void*);
60  void *opaque;
62 
67  AVIO_ENTRY_UNKNOWN,
68  AVIO_ENTRY_BLOCK_DEVICE,
69  AVIO_ENTRY_CHARACTER_DEVICE,
70  AVIO_ENTRY_DIRECTORY,
71  AVIO_ENTRY_NAMED_PIPE,
72  AVIO_ENTRY_SYMBOLIC_LINK,
73  AVIO_ENTRY_SOCKET,
74  AVIO_ENTRY_FILE,
75  AVIO_ENTRY_SERVER,
76  AVIO_ENTRY_SHARE,
77  AVIO_ENTRY_WORKGROUP,
78 };
79 
86 typedef struct AVIODirEntry {
87  char *name;
88  int type;
89  int utf8;
91  int64_t size;
94  int64_t access_timestamp;
98  int64_t user_id;
99  int64_t group_id;
100  int64_t filemode;
101 } AVIODirEntry;
102 
103 typedef struct AVIODirContext {
104  struct URLContext *url_context;
106 
147 };
148 
161 typedef struct AVIOContext {
175 
176  /*
177  * The following shows the relationship between buffer, buf_ptr,
178  * buf_ptr_max, buf_end, buf_size, and pos, when reading and when writing
179  * (since AVIOContext is used for both):
180  *
181  **********************************************************************************
182  * READING
183  **********************************************************************************
184  *
185  * | buffer_size |
186  * |---------------------------------------|
187  * | |
188  *
189  * buffer buf_ptr buf_end
190  * +---------------+-----------------------+
191  * |/ / / / / / / /|/ / / / / / /| |
192  * read buffer: |/ / consumed / | to be read /| |
193  * |/ / / / / / / /|/ / / / / / /| |
194  * +---------------+-----------------------+
195  *
196  * pos
197  * +-------------------------------------------+-----------------+
198  * input file: | | |
199  * +-------------------------------------------+-----------------+
200  *
201  *
202  **********************************************************************************
203  * WRITING
204  **********************************************************************************
205  *
206  * | buffer_size |
207  * |--------------------------------------|
208  * | |
209  *
210  * buf_ptr_max
211  * buffer (buf_ptr) buf_end
212  * +-----------------------+--------------+
213  * |/ / / / / / / / / / / /| |
214  * write buffer: | / / to be flushed / / | |
215  * |/ / / / / / / / / / / /| |
216  * +-----------------------+--------------+
217  * buf_ptr can be in this
218  * due to a backward seek
219  *
220  * pos
221  * +-------------+----------------------------------------------+
222  * output file: | | |
223  * +-------------+----------------------------------------------+
224  *
225  */
226  unsigned char *buffer;
228  unsigned char *buf_ptr;
229  unsigned char *buf_end;
233  void *opaque;
235  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
236  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
237  int64_t (*seek)(void *opaque, int64_t offset, int whence);
238  int64_t pos;
241  int max_packet_size;
242  unsigned long checksum;
243  unsigned char *checksum_ptr;
244  unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
245  int error;
249  int (*read_pause)(void *opaque, int pause);
255  int64_t (*read_seek)(void *opaque, int stream_index,
256  int64_t timestamp, int flags);
260  int seekable;
261 
266  int64_t maxsize;
267 
273  int direct;
274 
279  int64_t bytes_read;
280 
286 
292 
299 
305 
309  const char *protocol_whitelist;
310 
314  const char *protocol_blacklist;
315 
319  int (*write_data_type)(void *opaque, uint8_t *buf, int buf_size,
320  enum AVIODataMarkerType type, int64_t time);
327 
331  enum AVIODataMarkerType current_type;
332  int64_t last_time;
333 
338  int (*short_seek_get)(void *opaque);
339 
340  int64_t written;
341 
346  unsigned char *buf_ptr_max;
347 
352 } AVIOContext;
353 
361 const char *avio_find_protocol_name(const char *url);
362 
375 int avio_check(const char *url, int flags);
376 
386 int avpriv_io_move(const char *url_src, const char *url_dst);
387 
394 int avpriv_io_delete(const char *url);
395 
406 int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options);
407 
420 
431 
438 
463  unsigned char *buffer,
464  int buffer_size,
465  int write_flag,
466  void *opaque,
467  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
468  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
469  int64_t (*seek)(void *opaque, int64_t offset, int whence));
470 
478 
479 void avio_w8(AVIOContext *s, int b);
480 void avio_write(AVIOContext *s, const unsigned char *buf, int size);
481 void avio_wl64(AVIOContext *s, uint64_t val);
482 void avio_wb64(AVIOContext *s, uint64_t val);
483 void avio_wl32(AVIOContext *s, unsigned int val);
484 void avio_wb32(AVIOContext *s, unsigned int val);
485 void avio_wl24(AVIOContext *s, unsigned int val);
486 void avio_wb24(AVIOContext *s, unsigned int val);
487 void avio_wl16(AVIOContext *s, unsigned int val);
488 void avio_wb16(AVIOContext *s, unsigned int val);
489 
494 int avio_put_str(AVIOContext *s, const char *str);
495 
503 int avio_put_str16le(AVIOContext *s, const char *str);
504 
512 int avio_put_str16be(AVIOContext *s, const char *str);
513 
524 void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type);
525 
531 #define AVSEEK_SIZE 0x10000
532 
539 #define AVSEEK_FORCE 0x20000
540 
545 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence);
546 
551 int64_t avio_skip(AVIOContext *s, int64_t offset);
552 
557 static av_always_inline int64_t avio_tell(AVIOContext *s)
558 {
559  return avio_seek(s, 0, SEEK_CUR);
560 }
561 
566 int64_t avio_size(AVIOContext *s);
567 
572 int avio_feof(AVIOContext *s);
573 
575 int avio_printf(AVIOContext *s, const char *fmt, ...) av_printf_format(2, 3);
576 
587 void avio_flush(AVIOContext *s);
588 
593 int avio_read(AVIOContext *s, unsigned char *buf, int size);
594 
602 int avio_read_partial(AVIOContext *s, unsigned char *buf, int size);
603 
611 int avio_r8 (AVIOContext *s);
612 unsigned int avio_rl16(AVIOContext *s);
613 unsigned int avio_rl24(AVIOContext *s);
614 unsigned int avio_rl32(AVIOContext *s);
615 uint64_t avio_rl64(AVIOContext *s);
616 unsigned int avio_rb16(AVIOContext *s);
617 unsigned int avio_rb24(AVIOContext *s);
618 unsigned int avio_rb32(AVIOContext *s);
619 uint64_t avio_rb64(AVIOContext *s);
636 int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen);
637 
644 int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen);
645 int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen);
646 
647 
654 #define AVIO_FLAG_READ 1
655 #define AVIO_FLAG_WRITE 2
656 #define AVIO_FLAG_READ_WRITE (AVIO_FLAG_READ|AVIO_FLAG_WRITE)
673 #define AVIO_FLAG_NONBLOCK 8
674 
681 #define AVIO_FLAG_DIRECT 0x8000
682 
697 int avio_open(AVIOContext **s, const char *url, int flags);
698 
717 int avio_open2(AVIOContext **s, const char *url, int flags,
718  const AVIOInterruptCB *int_cb, AVDictionary **options);
719 
730 int avio_close(AVIOContext *s);
731 
743 int avio_closep(AVIOContext **s);
744 
745 
753 
764 int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
765 
775 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
776 
788 const char *avio_enum_protocols(void **opaque, int output);
789 
797 int avio_pause(AVIOContext *h, int pause);
798 
818 int64_t avio_seek_time(AVIOContext *h, int stream_index,
819  int64_t timestamp, int flags);
820 
821 /* Avoid a warning. The header can not be included because it breaks c++. */
822 struct AVBPrint;
823 
830 int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size);
831 
839 int avio_accept(AVIOContext *s, AVIOContext **c);
840 
861 #endif /* AVFORMAT_AVIO_H */
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:1154
Bytestream IO Context.
Definition: avio.h:161
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:336
struct AVIODirEntry AVIODirEntry
Describes single entry of the directory.
int avpriv_io_delete(const char *url)
Delete a resource.
Definition: avio.c:521
int avio_put_str16be(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16BE and write it.
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1420
int64_t filemode
Unix file mode, -1 if unknown.
Definition: avio.h:100
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:228
int writeout_count
writeout statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:291
unsigned char * buf_end
End of the data, may be less than buffer+buffer_size if the read function returned less data than req...
Definition: avio.h:229
int write_flag
true if open for writing
Definition: avio.h:240
int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size)
Read contents of h into print buffer, up to max_size bytes, or up to EOF.
Definition: aviobuf.c:1261
Describes single entry of the directory.
Definition: avio.h:86
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
int min_packet_size
Try to buffer at least this amount of data before flushing it.
Definition: avio.h:351
unsigned char * buffer
Start of the buffer.
Definition: avio.h:226
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
void * opaque
A private pointer, passed to the read/write/seek/...
Definition: avio.h:233
int64_t maxsize
max filesize, used to limit allocations This field is internal to libavformat and access from outside...
Definition: avio.h:266
int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a UTF-16 string from pb and convert it to UTF-8.
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1391
Trailer data, which doesn&#39;t contain actual content, but only for finalizing the output file...
Definition: avio.h:140
int64_t modification_timestamp
Time of last modification in microseconds since unix epoch, -1 if unknown.
Definition: avio.h:92
Public dictionary API.
int avio_accept(AVIOContext *s, AVIOContext **c)
Accept and allocate a client context on a server context.
Definition: aviobuf.c:1279
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition: avio.h:146
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:493
const AVClass * av_class
A class for private options.
Definition: avio.h:174
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition: avio.h:128
int64_t bytes_read
Bytes read statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:279
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:648
int avio_check(const char *url, int flags)
Return AVIO_FLAG_* access flags corresponding to the access permissions of the resource in url...
Definition: avio.c:480
Callback for checking whether to abort blocking functions.
Definition: avio.h:58
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition: aviobuf.c:131
char * name
Filename.
Definition: avio.h:87
Definition: dict.c:30
const char * avio_enum_protocols(void **opaque, int output)
Iterate through names of available protocols.
Definition: protocols.c:94
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1190
int utf8
Set to 1 when name is encoded with UTF-8, 0 otherwise.
Definition: avio.h:89
const char * protocol_whitelist
&#39;,&#39; separated list of allowed protocols.
Definition: avio.h:309
int direct
avio_read and avio_write should if possible be satisfied directly instead of going through a buffer...
Definition: avio.h:273
const char * protocol_blacklist
&#39;,&#39; separated list of disallowed protocols.
Definition: avio.h:314
int seek_count
seek statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:285
struct AVIOInterruptCB AVIOInterruptCB
Callback for checking whether to abort blocking functions.
This is any, unlabelled data.
Definition: avio.h:135
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:238
void avio_free_directory_entry(AVIODirEntry **entry)
Free entry allocated by avio_read_dir().
Definition: avio.c:603
int64_t access_timestamp
Time of last access in microseconds since unix epoch, -1 if unknown.
Definition: avio.h:94
AVIODataMarkerType
Different data types that can be returned via the AVIO write_data_type callback.
Definition: avio.h:111
Libavformat version macros.
int64_t size
File size in bytes, -1 if unknown.
Definition: avio.h:91
int buffer_size
Maximum buffer size.
Definition: avio.h:227
Definition: avio.h:103
int avpriv_io_move(const char *url_src, const char *url_dst)
Move or rename a resource.
Definition: avio.c:499
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:385
int type
Type of the entry.
Definition: avio.h:88
Definition: url.h:38
struct AVIOContext AVIOContext
Bytestream IO Context.
Describe the class of an AVClass context structure.
Definition: log.h:67
int64_t group_id
Group ID of owner, -1 if unknown.
Definition: avio.h:99
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition: avio.c:473
int avio_put_str16le(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16LE and write it.
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:1178
int short_seek_threshold
Threshold to favor readahead over seek.
Definition: avio.h:304
int avio_handshake(AVIOContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition: aviobuf.c:1291
int avio_close_dir(AVIODirContext **s)
Close directory.
Definition: avio.c:588
int ignore_boundary_point
If set, don&#39;t call write_data_type separately for AVIO_DATA_MARKER_BOUNDARY_POINT, but ignore them and treat them as AVIO_DATA_MARKER_UNKNOWN (to avoid needlessly small chunks of data returned from the callback).
Definition: avio.h:326
int error
contains the error code or 0 if no error happened
Definition: avio.h:245
int avio_pause(AVIOContext *h, int pause)
Pause and resume playing - only meaningful if using a network streaming protocol (e.g.
Definition: aviobuf.c:1235
int orig_buffer_size
Original buffer size used internally after probing and ensure seekback to reset the buffer size This ...
Definition: avio.h:298
common internal and external API header
int64_t status_change_timestamp
Time of last status change in microseconds since unix epoch, -1 if unknown.
Definition: avio.h:96
AVIODirEntryType
Directory entry types.
Definition: avio.h:66
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
Definition: aviobuf.c:148
int64_t pos
position in the file of the current buffer
Definition: avio.h:238
int64_t avio_seek_time(AVIOContext *h, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp relative to some component stream.
Definition: aviobuf.c:1242
unsigned char * buf_ptr_max
Maximum reached position before a backward seek in the write buffer, used keeping track of already wr...
Definition: avio.h:346
int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
Get next directory entry.
Definition: avio.c:575
A point in the output bytestream where a decoder can start decoding (i.e.
Definition: avio.h:122
int eof_reached
true if eof reached
Definition: avio.h:239
int64_t user_id
User ID of owner, -1 if unknown.
Definition: avio.h:98
int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
Open directory for reading.
Definition: avio.c:537
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:880
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:358
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:1215
Header data; this needs to be present for the stream to be decodeable.
Definition: avio.h:115
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1403