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 module ffmpeg.libavformat.avio; 21 import std.stdint; 22 import std.stdio; 23 import ffmpeg.libavutil.avutil; 24 import ffmpeg.libavformat.avformat_version; 25 26 @nogc nothrow extern(C): 27 /** 28 * @file 29 * @ingroup lavf_io 30 * Buffered I/O operations 31 */ 32 33 const int AVIO_SEEKABLE_NORMAL = 0x0001; /**< Seeking works like for a local file */ 34 35 /** 36 * Callback for checking whether to abort blocking functions. 37 * AVERROR_EXIT is returned in this case by the interrupted 38 * function. During blocking operations, callback is called with 39 * opaque as parameter. If the callback returns 1, the 40 * blocking operation will be aborted. 41 * 42 * No members can be added to this struct without a major bump, if 43 * new elements have been added after this struct in AVFormatContext 44 * or AVIOContext. 45 */ 46 struct AVIOInterruptCB { 47 int function (void*) callback; 48 void *opaque; 49 } 50 51 /** 52 * Bytestream IO Context. 53 * New fields can be added to the end with minor version bumps. 54 * Removal, reordering and changes to existing fields require a major 55 * version bump. 56 * sizeof(AVIOContext) must not be used outside libav*. 57 * 58 * @note None of the function pointers in AVIOContext should be called 59 * directly, they should only be set by the client application 60 * when implementing custom I/O. Normally these are set to the 61 * function pointers specified in avio_alloc_context() 62 */ 63 struct AVIOContext { 64 /** 65 * A class for private options. 66 * 67 * If this AVIOContext is created by avio_open2(), av_class is set and 68 * passes the options down to protocols. 69 * 70 * If this AVIOContext is manually allocated, then av_class may be set by 71 * the caller. 72 * 73 * warning -- this field can be NULL, be sure to not pass this AVIOContext 74 * to any av_opt_* functions in that case. 75 */ 76 const AVClass *av_class; 77 ubyte *buffer; /**< Start of the buffer. */ 78 int buffer_size; /**< Maximum buffer size */ 79 ubyte *buf_ptr; /**< Current position in the buffer */ 80 ubyte *buf_end; /**< End of the data, may be less than 81 buffer+buffer_size if the read function returned 82 less data than requested, e.g. for streams where 83 no more data has been received yet. */ 84 void *opaque; /**< A private pointer, passed to the read/write/seek/... 85 functions. */ 86 int function(void *opaque, uint8_t *buf, int buf_size) read_packet; 87 int function(void *opaque, uint8_t *buf, int buf_size) write_packet; 88 int64_t function(void *opaque, int64_t offset, int whence) seek; 89 int64_t pos; /**< position in the file of the current buffer */ 90 int must_flush; /**< true if the next seek should flush */ 91 int eof_reached; /**< true if eof reached */ 92 int write_flag; /**< true if open for writing */ 93 int max_packet_size; 94 uint checksum; 95 ubyte *checksum_ptr; 96 uint function(uint checksum, const uint8_t *buf, uint size) update_checksum; 97 int error; /**< contains the error code or 0 if no error happened */ 98 /** 99 * Pause or resume playback for network streaming protocols - e.g. MMS. 100 */ 101 int function(void *opaque, int pause) read_pause; 102 /** 103 * Seek to a given timestamp in stream with the specified stream_index. 104 * Needed for some network streaming protocols which don't support seeking 105 * to byte position. 106 */ 107 int64_t function(void *opaque, int stream_index, 108 int64_t timestamp, int flags) read_seek; 109 /** 110 * A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable. 111 */ 112 int seekable; 113 114 /** 115 * max filesize, used to limit allocations 116 * This field is internal to libavformat and access from outside is not allowed. 117 */ 118 int64_t maxsize; 119 120 /** 121 * avio_read and avio_write should if possible be satisfied directly 122 * instead of going through a buffer, and avio_seek will always 123 * call the underlying seek function directly. 124 */ 125 int direct; 126 127 /** 128 * Bytes read statistic 129 * This field is internal to libavformat and access from outside is not allowed. 130 */ 131 int64_t bytes_read; 132 133 /** 134 * seek statistic 135 * This field is internal to libavformat and access from outside is not allowed. 136 */ 137 int seek_count; 138 139 /** 140 * writeout statistic 141 * This field is internal to libavformat and access from outside is not allowed. 142 */ 143 int writeout_count; 144 145 /** 146 * Original buffer size 147 * used internally after probing and ensure seekback to reset the buffer size 148 * This field is internal to libavformat and access from outside is not allowed. 149 */ 150 int orig_buffer_size; 151 } 152 153 /* unbuffered I/O */ 154 155 /** 156 * Return the name of the protocol that will handle the passed URL. 157 * 158 * NULL is returned if no protocol could be found for the given URL. 159 * 160 * @return Name of the protocol or NULL. 161 */ 162 char *avio_find_protocol_name(const char *url); 163 164 /** 165 * Return AVIO_FLAG_* access flags corresponding to the access permissions 166 * of the resource in url, or a negative value corresponding to an 167 * AVERROR code in case of failure. The returned access flags are 168 * masked by the value in flags. 169 * 170 * @note This function is intrinsically unsafe, in the sense that the 171 * checked resource may change its existence or permission status from 172 * one call to another. Thus you should not trust the returned value, 173 * unless you are sure that no other processes are accessing the 174 * checked resource. 175 */ 176 int avio_check(const char *url, int flags); 177 178 /** 179 * Allocate and initialize an AVIOContext for buffered I/O. It must be later 180 * freed with av_free(). 181 * 182 * @param buffer Memory block for input/output operations via AVIOContext. 183 * The buffer must be allocated with av_malloc() and friends. 184 * @param buffer_size The buffer size is very important for performance. 185 * For protocols with fixed blocksize it should be set to this blocksize. 186 * For others a typical size is a cache page, e.g. 4kb. 187 * @param write_flag Set to 1 if the buffer should be writable, 0 otherwise. 188 * @param opaque An opaque pointer to user-specific data. 189 * @param read_packet A function for refilling the buffer, may be NULL. 190 * @param write_packet A function for writing the buffer contents, may be NULL. 191 * The function may not change the input buffers content. 192 * @param seek A function for seeking to specified byte position, may be NULL. 193 * 194 * @return Allocated AVIOContext or NULL on failure. 195 */ 196 AVIOContext *avio_alloc_context( 197 ubyte *buffer, 198 int buffer_size, 199 int write_flag, 200 void *opaque, 201 int function(void *opaque, uint8_t *buf, int buf_size) read_packet, 202 int function(void *opaque, uint8_t *buf, int buf_size) write_packet, 203 int64_t function(void *opaque, int64_t offset, int whence) seek); 204 205 void avio_w8(AVIOContext *s, int b); 206 void avio_write(AVIOContext *s, const ubyte *buf, int size); 207 void avio_wl64(AVIOContext *s, uint64_t val); 208 void avio_wb64(AVIOContext *s, uint64_t val); 209 void avio_wl32(AVIOContext *s, uint val); 210 void avio_wb32(AVIOContext *s, uint val); 211 void avio_wl24(AVIOContext *s, uint val); 212 void avio_wb24(AVIOContext *s, uint val); 213 void avio_wl16(AVIOContext *s, uint val); 214 void avio_wb16(AVIOContext *s, uint val); 215 216 /** 217 * Write a NULL-terminated string. 218 * @return number of bytes written. 219 */ 220 int avio_put_str(AVIOContext *s, const char *str); 221 222 /** 223 * Convert an UTF-8 string to UTF-16LE and write it. 224 * @return number of bytes written. 225 */ 226 int avio_put_str16le(AVIOContext *s, const char *str); 227 228 /** 229 * Passing this as the "whence" parameter to a seek function causes it to 230 * return the filesize without seeking anywhere. Supporting this is optional. 231 * If it is not supported then the seek function will return <0. 232 */ 233 enum AVSEEK_SIZE = 0x10000; 234 235 /** 236 * Oring this flag as into the "whence" parameter to a seek function causes it to 237 * seek by any means (like reopening and linear reading) or other normally unreasonable 238 * means that can be extremely slow. 239 * This may be ignored by the seek code. 240 */ 241 enum AVSEEK_FORCE = 0x20000; 242 243 /** 244 * fseek() equivalent for AVIOContext. 245 * @return new position or AVERROR. 246 */ 247 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence); 248 249 /** 250 * Skip given number of bytes forward 251 * @return new position or AVERROR. 252 */ 253 int64_t avio_skip(AVIOContext *s, int64_t offset); 254 255 /** 256 * ftell() equivalent for AVIOContext. 257 * @return position or AVERROR. 258 */ 259 static int64_t avio_tell(AVIOContext *s) 260 { 261 return avio_seek(s, 0, SEEK_CUR); 262 } 263 264 /** 265 * Get the filesize. 266 * @return filesize or AVERROR 267 */ 268 int64_t avio_size(AVIOContext *s); 269 270 /** 271 * feof() equivalent for AVIOContext. 272 * @return non zero if and only if end of file 273 */ 274 int avio_feof(AVIOContext *s); 275 static if (FF_API_URL_FEOF) { 276 /** 277 * @deprecated use avio_feof() 278 */ 279 deprecated 280 int url_feof(AVIOContext *s); 281 } 282 283 /** @warning currently size is limited */ 284 int avio_printf(AVIOContext *s, const char *fmt, ...); 285 286 /** 287 * Force flushing of buffered data to the output s. 288 * 289 * Force the buffered data to be immediately written to the output, 290 * without to wait to fill the internal buffer. 291 */ 292 void avio_flush(AVIOContext *s); 293 294 /** 295 * Read size bytes from AVIOContext into buf. 296 * @return number of bytes read or AVERROR 297 */ 298 int avio_read(AVIOContext *s, ubyte *buf, int size); 299 300 /** 301 * @name Functions for reading from AVIOContext 302 * @{ 303 * 304 * @note return 0 if EOF, so you cannot use it if EOF handling is 305 * necessary 306 */ 307 int avio_r8 (AVIOContext *s); 308 uint avio_rl16(AVIOContext *s); 309 uint avio_rl24(AVIOContext *s); 310 uint avio_rl32(AVIOContext *s); 311 uint64_t avio_rl64(AVIOContext *s); 312 uint avio_rb16(AVIOContext *s); 313 uint avio_rb24(AVIOContext *s); 314 uint avio_rb32(AVIOContext *s); 315 uint64_t avio_rb64(AVIOContext *s); 316 /** 317 * @} 318 */ 319 320 /** 321 * Read a string from pb into buf. The reading will terminate when either 322 * a NULL character was encountered, maxlen bytes have been read, or nothing 323 * more can be read from pb. The result is guaranteed to be NULL-terminated, it 324 * will be truncated if buf is too small. 325 * Note that the string is not interpreted or validated in any way, it 326 * might get truncated in the middle of a sequence for multi-byte encodings. 327 * 328 * @return number of bytes read (is always <= maxlen). 329 * If reading ends on EOF or error, the return value will be one more than 330 * bytes actually read. 331 */ 332 int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen); 333 334 /** 335 * Read a UTF-16 string from pb and convert it to UTF-8. 336 * The reading will terminate when either a null or invalid character was 337 * encountered or maxlen bytes have been read. 338 * @return number of bytes read (is always <= maxlen) 339 */ 340 int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen); 341 int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen); 342 343 344 /** 345 * @name URL open modes 346 * The flags argument to avio_open must be one of the following 347 * constants, optionally ORed with other flags. 348 * @{ 349 */ 350 enum AVIO_FLAG_READ = 1; /**< read-only */ 351 enum AVIO_FLAG_WRITE = 2; /**< write-only */ 352 enum AVIO_FLAG_READ_WRITE = (AVIO_FLAG_READ|AVIO_FLAG_WRITE); /**< read-write pseudo flag */ 353 /** 354 * @} 355 */ 356 357 /** 358 * Use non-blocking mode. 359 * If this flag is set, operations on the context will return 360 * AVERROR(EAGAIN) if they can not be performed immediately. 361 * If this flag is not set, operations on the context will never return 362 * AVERROR(EAGAIN). 363 * Note that this flag does not affect the opening/connecting of the 364 * context. Connecting a protocol will always block if necessary (e.g. on 365 * network protocols) but never hang (e.g. on busy devices). 366 * Warning: non-blocking protocols is work-in-progress; this flag may be 367 * silently ignored. 368 */ 369 enum AVIO_FLAG_NONBLOCK = 8; 370 371 /** 372 * Use direct mode. 373 * avio_read and avio_write should if possible be satisfied directly 374 * instead of going through a buffer, and avio_seek will always 375 * call the underlying seek function directly. 376 */ 377 enum AVIO_FLAG_DIRECT = 0x8000; 378 379 /** 380 * Create and initialize a AVIOContext for accessing the 381 * resource indicated by url. 382 * @note When the resource indicated by url has been opened in 383 * read+write mode, the AVIOContext can be used only for writing. 384 * 385 * @param s Used to return the pointer to the created AVIOContext. 386 * In case of failure the pointed to value is set to NULL. 387 * @param url resource to access 388 * @param flags flags which control how the resource indicated by url 389 * is to be opened 390 * @return >= 0 in case of success, a negative value corresponding to an 391 * AVERROR code in case of failure 392 */ 393 int avio_open(AVIOContext **s, const char *url, int flags); 394 395 /** 396 * Create and initialize a AVIOContext for accessing the 397 * resource indicated by url. 398 * @note When the resource indicated by url has been opened in 399 * read+write mode, the AVIOContext can be used only for writing. 400 * 401 * @param s Used to return the pointer to the created AVIOContext. 402 * In case of failure the pointed to value is set to NULL. 403 * @param url resource to access 404 * @param flags flags which control how the resource indicated by url 405 * is to be opened 406 * @param int_cb an interrupt callback to be used at the protocols level 407 * @param options A dictionary filled with protocol-private options. On return 408 * this parameter will be destroyed and replaced with a dict containing options 409 * that were not found. May be NULL. 410 * @return >= 0 in case of success, a negative value corresponding to an 411 * AVERROR code in case of failure 412 */ 413 int avio_open2(AVIOContext **s, const char *url, int flags, 414 const AVIOInterruptCB *int_cb, AVDictionary **options); 415 416 /** 417 * Close the resource accessed by the AVIOContext s and free it. 418 * This function can only be used if s was opened by avio_open(). 419 * 420 * The internal buffer is automatically flushed before closing the 421 * resource. 422 * 423 * @return 0 on success, an AVERROR < 0 on error. 424 * @see avio_closep 425 */ 426 int avio_close(AVIOContext *s); 427 428 /** 429 * Close the resource accessed by the AVIOContext *s, free it 430 * and set the pointer pointing to it to NULL. 431 * This function can only be used if s was opened by avio_open(). 432 * 433 * The internal buffer is automatically flushed before closing the 434 * resource. 435 * 436 * @return 0 on success, an AVERROR < 0 on error. 437 * @see avio_close 438 */ 439 int avio_closep(AVIOContext **s); 440 441 442 /** 443 * Open a write only memory stream. 444 * 445 * @param s new IO context 446 * @return zero if no error. 447 */ 448 int avio_open_dyn_buf(AVIOContext **s); 449 450 /** 451 * Return the written size and a pointer to the buffer. The buffer 452 * must be freed with av_free(). 453 * Padding of FF_INPUT_BUFFER_PADDING_SIZE is added to the buffer. 454 * 455 * @param s IO context 456 * @param pbuffer pointer to a byte buffer 457 * @return the length of the byte buffer 458 */ 459 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer); 460 461 /** 462 * Iterate through names of available protocols. 463 * 464 * @param opaque A private pointer representing current protocol. 465 * It must be a pointer to NULL on first iteration and will 466 * be updated by successive calls to avio_enum_protocols. 467 * @param output If set to 1, iterate over output protocols, 468 * otherwise over input protocols. 469 * 470 * @return A static string containing the name of current protocol or NULL 471 */ 472 char *avio_enum_protocols(void **opaque, int output); 473 474 /** 475 * Pause and resume playing - only meaningful if using a network streaming 476 * protocol (e.g. MMS). 477 * 478 * @param h IO context from which to call the read_pause function pointer 479 * @param pause 1 for pause, 0 for resume 480 */ 481 int avio_pause(AVIOContext *h, int pause); 482 483 /** 484 * Seek to a given timestamp relative to some component stream. 485 * Only meaningful if using a network streaming protocol (e.g. MMS.). 486 * 487 * @param h IO context from which to call the seek function pointers 488 * @param stream_index The stream index that the timestamp is relative to. 489 * If stream_index is (-1) the timestamp should be in AV_TIME_BASE 490 * units from the beginning of the presentation. 491 * If a stream_index >= 0 is used and the protocol does not support 492 * seeking based on component streams, the call will fail. 493 * @param timestamp timestamp in AVStream.time_base units 494 * or if there is no stream specified then in AV_TIME_BASE units. 495 * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE 496 * and AVSEEK_FLAG_ANY. The protocol may silently ignore 497 * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will 498 * fail if used and not supported. 499 * @return >= 0 on success 500 * @see AVInputFormat::read_seek 501 */ 502 int64_t avio_seek_time(AVIOContext *h, int stream_index, 503 int64_t timestamp, int flags); 504 505 /* Avoid a warning. The header can not be included because it breaks c++. */ 506 struct AVBPrint; 507 508 /** 509 * Read contents of h into print buffer, up to max_size bytes, or up to EOF. 510 * 511 * @return 0 for success (max_size bytes read or EOF reached), negative error 512 * code otherwise 513 */ 514 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size); 515