FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
aviobuf.c
Go to the documentation of this file.
1 /*
2  * buffered I/O
3  * Copyright (c) 2000,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 
22 #include "libavutil/bprint.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/log.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
29 #include "avformat.h"
30 #include "avio.h"
31 #include "avio_internal.h"
32 #include "internal.h"
33 #include "url.h"
34 #include <stdarg.h>
35 
36 #define IO_BUFFER_SIZE 32768
37 
38 /**
39  * Do seeks within this distance ahead of the current buffer by skipping
40  * data instead of calling the protocol seek function, for seekable
41  * protocols.
42  */
43 #define SHORT_SEEK_THRESHOLD 4096
44 
45 static void *ff_avio_child_next(void *obj, void *prev)
46 {
47  AVIOContext *s = obj;
48  return prev ? NULL : s->opaque;
49 }
50 
51 static const AVClass *ff_avio_child_class_next(const AVClass *prev)
52 {
53  return prev ? NULL : &ffurl_context_class;
54 }
55 
56 #define OFFSET(x) offsetof(AVIOContext,x)
57 #define E AV_OPT_FLAG_ENCODING_PARAM
58 #define D AV_OPT_FLAG_DECODING_PARAM
59 static const AVOption ff_avio_options[] = {
60  {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, D },
61  { NULL },
62 };
63 
65  .class_name = "AVIOContext",
66  .item_name = av_default_item_name,
67  .version = LIBAVUTIL_VERSION_INT,
68  .option = ff_avio_options,
69  .child_next = ff_avio_child_next,
70  .child_class_next = ff_avio_child_class_next,
71 };
72 
73 static void fill_buffer(AVIOContext *s);
74 static int url_resetbuf(AVIOContext *s, int flags);
75 
77  unsigned char *buffer,
78  int buffer_size,
79  int write_flag,
80  void *opaque,
81  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
82  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
83  int64_t (*seek)(void *opaque, int64_t offset, int whence))
84 {
85  s->buffer = buffer;
86  s->orig_buffer_size =
87  s->buffer_size = buffer_size;
88  s->buf_ptr = buffer;
89  s->opaque = opaque;
90  s->direct = 0;
91 
92  url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
93 
96  s->seek = seek;
97  s->pos = 0;
98  s->must_flush = 0;
99  s->eof_reached = 0;
100  s->error = 0;
101  s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
102  s->max_packet_size = 0;
103  s->update_checksum = NULL;
105 
106  if (!read_packet && !write_flag) {
107  s->pos = buffer_size;
108  s->buf_end = s->buffer + buffer_size;
109  }
110  s->read_pause = NULL;
111  s->read_seek = NULL;
112 
113  return 0;
114 }
115 
117  unsigned char *buffer,
118  int buffer_size,
119  int write_flag,
120  void *opaque,
121  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
122  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
123  int64_t (*seek)(void *opaque, int64_t offset, int whence))
124 {
125  AVIOContext *s = av_mallocz(sizeof(AVIOContext));
126  if (!s)
127  return NULL;
128  ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
129  read_packet, write_packet, seek);
130  return s;
131 }
132 
133 static void writeout(AVIOContext *s, const uint8_t *data, int len)
134 {
135  if (s->write_packet && !s->error) {
136  int ret = s->write_packet(s->opaque, (uint8_t *)data, len);
137  if (ret < 0) {
138  s->error = ret;
139  }
140  }
141  s->writeout_count ++;
142  s->pos += len;
143 }
144 
145 static void flush_buffer(AVIOContext *s)
146 {
147  if (s->write_flag && s->buf_ptr > s->buffer) {
148  writeout(s, s->buffer, s->buf_ptr - s->buffer);
149  if (s->update_checksum) {
151  s->buf_ptr - s->checksum_ptr);
152  s->checksum_ptr = s->buffer;
153  }
154  }
155  s->buf_ptr = s->buffer;
156  if (!s->write_flag)
157  s->buf_end = s->buffer;
158 }
159 
160 void avio_w8(AVIOContext *s, int b)
161 {
162  av_assert2(b>=-128 && b<=255);
163  *s->buf_ptr++ = b;
164  if (s->buf_ptr >= s->buf_end)
165  flush_buffer(s);
166 }
167 
168 void ffio_fill(AVIOContext *s, int b, int count)
169 {
170  while (count > 0) {
171  int len = FFMIN(s->buf_end - s->buf_ptr, count);
172  memset(s->buf_ptr, b, len);
173  s->buf_ptr += len;
174 
175  if (s->buf_ptr >= s->buf_end)
176  flush_buffer(s);
177 
178  count -= len;
179  }
180 }
181 
182 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
183 {
184  if (s->direct && !s->update_checksum) {
185  avio_flush(s);
186  writeout(s, buf, size);
187  return;
188  }
189  while (size > 0) {
190  int len = FFMIN(s->buf_end - s->buf_ptr, size);
191  memcpy(s->buf_ptr, buf, len);
192  s->buf_ptr += len;
193 
194  if (s->buf_ptr >= s->buf_end)
195  flush_buffer(s);
196 
197  buf += len;
198  size -= len;
199  }
200 }
201 
203 {
204  flush_buffer(s);
205  s->must_flush = 0;
206 }
207 
208 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
209 {
210  int64_t offset1;
211  int64_t pos;
212  int force = whence & AVSEEK_FORCE;
213  int buffer_size;
214  whence &= ~AVSEEK_FORCE;
215 
216  if(!s)
217  return AVERROR(EINVAL);
218 
219  buffer_size = s->buf_end - s->buffer;
220  // pos is the absolute position that the beginning of s->buffer corresponds to in the file
221  pos = s->pos - (s->write_flag ? 0 : buffer_size);
222 
223  if (whence != SEEK_CUR && whence != SEEK_SET)
224  return AVERROR(EINVAL);
225 
226  if (whence == SEEK_CUR) {
227  offset1 = pos + (s->buf_ptr - s->buffer);
228  if (offset == 0)
229  return offset1;
230  offset += offset1;
231  }
232  if (offset < 0)
233  return AVERROR(EINVAL);
234 
235  offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
236  if (!s->must_flush && (!s->direct || !s->seek) &&
237  offset1 >= 0 && offset1 <= buffer_size - s->write_flag) {
238  /* can do the seek inside the buffer */
239  s->buf_ptr = s->buffer + offset1;
240  } else if ((!s->seekable ||
241  offset1 <= buffer_size + s->short_seek_threshold) &&
242  !s->write_flag && offset1 >= 0 &&
243  (!s->direct || !s->seek) &&
244  (whence != SEEK_END || force)) {
245  while(s->pos < offset && !s->eof_reached)
246  fill_buffer(s);
247  if (s->eof_reached)
248  return AVERROR_EOF;
249  s->buf_ptr = s->buf_end - (s->pos - offset);
250  } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
251  int64_t res;
252 
253  pos -= FFMIN(buffer_size>>1, pos);
254  if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
255  return res;
256  s->buf_end =
257  s->buf_ptr = s->buffer;
258  s->pos = pos;
259  s->eof_reached = 0;
260  fill_buffer(s);
261  return avio_seek(s, offset, SEEK_SET | force);
262  } else {
263  int64_t res;
264  if (s->write_flag) {
265  flush_buffer(s);
266  s->must_flush = 1;
267  }
268  if (!s->seek)
269  return AVERROR(EPIPE);
270  if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
271  return res;
272  s->seek_count ++;
273  if (!s->write_flag)
274  s->buf_end = s->buffer;
275  s->buf_ptr = s->buffer;
276  s->pos = offset;
277  }
278  s->eof_reached = 0;
279  return offset;
280 }
281 
282 int64_t avio_skip(AVIOContext *s, int64_t offset)
283 {
284  return avio_seek(s, offset, SEEK_CUR);
285 }
286 
288 {
289  int64_t size;
290 
291  if (!s)
292  return AVERROR(EINVAL);
293 
294  if (!s->seek)
295  return AVERROR(ENOSYS);
296  size = s->seek(s->opaque, 0, AVSEEK_SIZE);
297  if (size < 0) {
298  if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
299  return size;
300  size++;
301  s->seek(s->opaque, s->pos, SEEK_SET);
302  }
303  return size;
304 }
305 
307 {
308  if(!s)
309  return 0;
310  if(s->eof_reached){
311  s->eof_reached=0;
312  fill_buffer(s);
313  }
314  return s->eof_reached;
315 }
316 
317 #if FF_API_URL_FEOF
318 int url_feof(AVIOContext *s)
319 {
320  return avio_feof(s);
321 }
322 #endif
323 
324 void avio_wl32(AVIOContext *s, unsigned int val)
325 {
326  avio_w8(s, (uint8_t) val );
327  avio_w8(s, (uint8_t)(val >> 8 ));
328  avio_w8(s, (uint8_t)(val >> 16));
329  avio_w8(s, val >> 24 );
330 }
331 
332 void avio_wb32(AVIOContext *s, unsigned int val)
333 {
334  avio_w8(s, val >> 24 );
335  avio_w8(s, (uint8_t)(val >> 16));
336  avio_w8(s, (uint8_t)(val >> 8 ));
337  avio_w8(s, (uint8_t) val );
338 }
339 
340 int avio_put_str(AVIOContext *s, const char *str)
341 {
342  int len = 1;
343  if (str) {
344  len += strlen(str);
345  avio_write(s, (const unsigned char *) str, len);
346  } else
347  avio_w8(s, 0);
348  return len;
349 }
350 
351 static inline int put_str16(AVIOContext *s, const char *str, const int be)
352 {
353  const uint8_t *q = str;
354  int ret = 0;
355  int err = 0;
356 
357  while (*q) {
358  uint32_t ch;
359  uint16_t tmp;
360 
361  GET_UTF8(ch, *q++, goto invalid;)
362  PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
363  ret += 2;)
364  continue;
365 invalid:
366  av_log(s, AV_LOG_ERROR, "Invaid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
367  err = AVERROR(EINVAL);
368  if (!*(q-1))
369  break;
370  }
371  if (be)
372  avio_wb16(s, 0);
373  else
374  avio_wl16(s, 0);
375  if (err)
376  return err;
377  ret += 2;
378  return ret;
379 }
380 
381 #define PUT_STR16(type, big_endian) \
382 int avio_put_str16 ## type(AVIOContext *s, const char *str) \
383 { \
384 return put_str16(s, str, big_endian); \
385 }
386 
387 PUT_STR16(le, 0)
388 PUT_STR16(be, 1)
389 
390 #undef PUT_STR16
391 
392 int ff_get_v_length(uint64_t val)
393 {
394  int i = 1;
395 
396  while (val >>= 7)
397  i++;
398 
399  return i;
400 }
401 
402 void ff_put_v(AVIOContext *bc, uint64_t val)
403 {
404  int i = ff_get_v_length(val);
405 
406  while (--i > 0)
407  avio_w8(bc, 128 | (uint8_t)(val >> (7*i)));
408 
409  avio_w8(bc, val & 127);
410 }
411 
412 void avio_wl64(AVIOContext *s, uint64_t val)
413 {
414  avio_wl32(s, (uint32_t)(val & 0xffffffff));
415  avio_wl32(s, (uint32_t)(val >> 32));
416 }
417 
418 void avio_wb64(AVIOContext *s, uint64_t val)
419 {
420  avio_wb32(s, (uint32_t)(val >> 32));
421  avio_wb32(s, (uint32_t)(val & 0xffffffff));
422 }
423 
424 void avio_wl16(AVIOContext *s, unsigned int val)
425 {
426  avio_w8(s, (uint8_t)val);
427  avio_w8(s, (int)val >> 8);
428 }
429 
430 void avio_wb16(AVIOContext *s, unsigned int val)
431 {
432  avio_w8(s, (int)val >> 8);
433  avio_w8(s, (uint8_t)val);
434 }
435 
436 void avio_wl24(AVIOContext *s, unsigned int val)
437 {
438  avio_wl16(s, val & 0xffff);
439  avio_w8(s, (int)val >> 16);
440 }
441 
442 void avio_wb24(AVIOContext *s, unsigned int val)
443 {
444  avio_wb16(s, (int)val >> 8);
445  avio_w8(s, (uint8_t)val);
446 }
447 
448 /* Input stream */
449 
450 static void fill_buffer(AVIOContext *s)
451 {
452  int max_buffer_size = s->max_packet_size ?
454  uint8_t *dst = s->buf_end - s->buffer + max_buffer_size < s->buffer_size ?
455  s->buf_end : s->buffer;
456  int len = s->buffer_size - (dst - s->buffer);
457 
458  /* can't fill the buffer without read_packet, just set EOF if appropriate */
459  if (!s->read_packet && s->buf_ptr >= s->buf_end)
460  s->eof_reached = 1;
461 
462  /* no need to do anything if EOF already reached */
463  if (s->eof_reached)
464  return;
465 
466  if (s->update_checksum && dst == s->buffer) {
467  if (s->buf_end > s->checksum_ptr)
469  s->buf_end - s->checksum_ptr);
470  s->checksum_ptr = s->buffer;
471  }
472 
473  /* make buffer smaller in case it ended up large after probing */
474  if (s->read_packet && s->orig_buffer_size && s->buffer_size > s->orig_buffer_size) {
475  if (dst == s->buffer) {
476  int ret = ffio_set_buf_size(s, s->orig_buffer_size);
477  if (ret < 0)
478  av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
479 
480  s->checksum_ptr = dst = s->buffer;
481  }
482  av_assert0(len >= s->orig_buffer_size);
483  len = s->orig_buffer_size;
484  }
485 
486  if (s->read_packet)
487  len = s->read_packet(s->opaque, dst, len);
488  else
489  len = 0;
490  if (len <= 0) {
491  /* do not modify buffer if EOF reached so that a seek back can
492  be done without rereading data */
493  s->eof_reached = 1;
494  if (len < 0)
495  s->error = len;
496  } else {
497  s->pos += len;
498  s->buf_ptr = dst;
499  s->buf_end = dst + len;
500  s->bytes_read += len;
501  }
502 }
503 
504 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
505  unsigned int len)
506 {
507  return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
508 }
509 
510 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
511  unsigned int len)
512 {
513  return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
514 }
515 
517 {
519  s->buf_ptr - s->checksum_ptr);
520  s->update_checksum = NULL;
521  return s->checksum;
522 }
523 
525  unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
526  unsigned long checksum)
527 {
528  s->update_checksum = update_checksum;
529  if (s->update_checksum) {
530  s->checksum = checksum;
531  s->checksum_ptr = s->buf_ptr;
532  }
533 }
534 
535 /* XXX: put an inline version */
537 {
538  if (s->buf_ptr >= s->buf_end)
539  fill_buffer(s);
540  if (s->buf_ptr < s->buf_end)
541  return *s->buf_ptr++;
542  return 0;
543 }
544 
545 int avio_read(AVIOContext *s, unsigned char *buf, int size)
546 {
547  int len, size1;
548 
549  size1 = size;
550  while (size > 0) {
551  len = FFMIN(s->buf_end - s->buf_ptr, size);
552  if (len == 0 || s->write_flag) {
553  if((s->direct || size > s->buffer_size) && !s->update_checksum) {
554  // bypass the buffer and read data directly into buf
555  if(s->read_packet)
556  len = s->read_packet(s->opaque, buf, size);
557 
558  if (len <= 0) {
559  /* do not modify buffer if EOF reached so that a seek back can
560  be done without rereading data */
561  s->eof_reached = 1;
562  if(len<0)
563  s->error= len;
564  break;
565  } else {
566  s->pos += len;
567  s->bytes_read += len;
568  size -= len;
569  buf += len;
570  // reset the buffer
571  s->buf_ptr = s->buffer;
572  s->buf_end = s->buffer/* + len*/;
573  }
574  } else {
575  fill_buffer(s);
576  len = s->buf_end - s->buf_ptr;
577  if (len == 0)
578  break;
579  }
580  } else {
581  memcpy(buf, s->buf_ptr, len);
582  buf += len;
583  s->buf_ptr += len;
584  size -= len;
585  }
586  }
587  if (size1 == size) {
588  if (s->error) return s->error;
589  if (avio_feof(s)) return AVERROR_EOF;
590  }
591  return size1 - size;
592 }
593 
594 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
595 {
596  int ret = avio_read(s, buf, size);
597  if (ret != size)
598  return AVERROR_INVALIDDATA;
599  return ret;
600 }
601 
602 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
603 {
604  if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
605  *data = s->buf_ptr;
606  s->buf_ptr += size;
607  return size;
608  } else {
609  *data = buf;
610  return avio_read(s, buf, size);
611  }
612 }
613 
614 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
615 {
616  int len;
617 
618  if (size < 0)
619  return -1;
620 
621  if (s->read_packet && s->write_flag) {
622  len = s->read_packet(s->opaque, buf, size);
623  if (len > 0)
624  s->pos += len;
625  return len;
626  }
627 
628  len = s->buf_end - s->buf_ptr;
629  if (len == 0) {
630  /* Reset the buf_end pointer to the start of the buffer, to make sure
631  * the fill_buffer call tries to read as much data as fits into the
632  * full buffer, instead of just what space is left after buf_end.
633  * This avoids returning partial packets at the end of the buffer,
634  * for packet based inputs.
635  */
636  s->buf_end = s->buf_ptr = s->buffer;
637  fill_buffer(s);
638  len = s->buf_end - s->buf_ptr;
639  }
640  if (len > size)
641  len = size;
642  memcpy(buf, s->buf_ptr, len);
643  s->buf_ptr += len;
644  if (!len) {
645  if (s->error) return s->error;
646  if (avio_feof(s)) return AVERROR_EOF;
647  }
648  return len;
649 }
650 
651 unsigned int avio_rl16(AVIOContext *s)
652 {
653  unsigned int val;
654  val = avio_r8(s);
655  val |= avio_r8(s) << 8;
656  return val;
657 }
658 
659 unsigned int avio_rl24(AVIOContext *s)
660 {
661  unsigned int val;
662  val = avio_rl16(s);
663  val |= avio_r8(s) << 16;
664  return val;
665 }
666 
667 unsigned int avio_rl32(AVIOContext *s)
668 {
669  unsigned int val;
670  val = avio_rl16(s);
671  val |= avio_rl16(s) << 16;
672  return val;
673 }
674 
675 uint64_t avio_rl64(AVIOContext *s)
676 {
677  uint64_t val;
678  val = (uint64_t)avio_rl32(s);
679  val |= (uint64_t)avio_rl32(s) << 32;
680  return val;
681 }
682 
683 unsigned int avio_rb16(AVIOContext *s)
684 {
685  unsigned int val;
686  val = avio_r8(s) << 8;
687  val |= avio_r8(s);
688  return val;
689 }
690 
691 unsigned int avio_rb24(AVIOContext *s)
692 {
693  unsigned int val;
694  val = avio_rb16(s) << 8;
695  val |= avio_r8(s);
696  return val;
697 }
698 unsigned int avio_rb32(AVIOContext *s)
699 {
700  unsigned int val;
701  val = avio_rb16(s) << 16;
702  val |= avio_rb16(s);
703  return val;
704 }
705 
706 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
707 {
708  int i = 0;
709  char c;
710 
711  do {
712  c = avio_r8(s);
713  if (c && i < maxlen-1)
714  buf[i++] = c;
715  } while (c != '\n' && c != '\r' && c);
716  if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
717  avio_skip(s, -1);
718 
719  buf[i] = 0;
720  return i;
721 }
722 
723 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
724 {
725  int i;
726 
727  if (buflen <= 0)
728  return AVERROR(EINVAL);
729  // reserve 1 byte for terminating 0
730  buflen = FFMIN(buflen - 1, maxlen);
731  for (i = 0; i < buflen; i++)
732  if (!(buf[i] = avio_r8(s)))
733  return i + 1;
734  buf[i] = 0;
735  for (; i < maxlen; i++)
736  if (!avio_r8(s))
737  return i + 1;
738  return maxlen;
739 }
740 
741 #define GET_STR16(type, read) \
742  int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
743 {\
744  char* q = buf;\
745  int ret = 0;\
746  if (buflen <= 0) \
747  return AVERROR(EINVAL); \
748  while (ret + 1 < maxlen) {\
749  uint8_t tmp;\
750  uint32_t ch;\
751  GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
752  if (!ch)\
753  break;\
754  PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
755  }\
756  *q = 0;\
757  return ret;\
758 }\
759 
761 GET_STR16(be, avio_rb16)
762 
763 #undef GET_STR16
764 
765 uint64_t avio_rb64(AVIOContext *s)
766 {
767  uint64_t val;
768  val = (uint64_t)avio_rb32(s) << 32;
769  val |= (uint64_t)avio_rb32(s);
770  return val;
771 }
772 
774  uint64_t val = 0;
775  int tmp;
776 
777  do{
778  tmp = avio_r8(bc);
779  val= (val<<7) + (tmp&127);
780  }while(tmp&128);
781  return val;
782 }
783 
785 {
786  uint8_t *buffer;
787  int buffer_size, max_packet_size;
788 
789  max_packet_size = h->max_packet_size;
790  if (max_packet_size) {
791  buffer_size = max_packet_size; /* no need to bufferize more than one packet */
792  } else {
793  buffer_size = IO_BUFFER_SIZE;
794  }
795  buffer = av_malloc(buffer_size);
796  if (!buffer)
797  return AVERROR(ENOMEM);
798 
799  *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
800  (int (*)(void *, uint8_t *, int)) ffurl_read,
801  (int (*)(void *, uint8_t *, int)) ffurl_write,
802  (int64_t (*)(void *, int64_t, int)) ffurl_seek);
803  if (!*s) {
804  av_free(buffer);
805  return AVERROR(ENOMEM);
806  }
807  (*s)->protocol_whitelist = av_strdup(h->protocol_whitelist);
808  if (!(*s)->protocol_whitelist && h->protocol_whitelist) {
809  avio_closep(s);
810  return AVERROR(ENOMEM);
811  }
812  (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
813  (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
814  (*s)->max_packet_size = max_packet_size;
815  if(h->prot) {
816  (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
817  (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
818  }
819  (*s)->av_class = &ff_avio_class;
820  return 0;
821 }
822 
823 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
824 {
825  uint8_t *buffer;
826  int max_buffer_size = s->max_packet_size ?
828  int filled = s->buf_end - s->buffer;
829  ptrdiff_t checksum_ptr_offset = s->checksum_ptr ? s->checksum_ptr - s->buffer : -1;
830 
831  buf_size += s->buf_ptr - s->buffer + max_buffer_size;
832 
833  if (buf_size < filled || s->seekable || !s->read_packet)
834  return 0;
835  av_assert0(!s->write_flag);
836 
837  buffer = av_malloc(buf_size);
838  if (!buffer)
839  return AVERROR(ENOMEM);
840 
841  memcpy(buffer, s->buffer, filled);
842  av_free(s->buffer);
843  s->buf_ptr = buffer + (s->buf_ptr - s->buffer);
844  s->buf_end = buffer + (s->buf_end - s->buffer);
845  s->buffer = buffer;
846  s->buffer_size = buf_size;
847  if (checksum_ptr_offset >= 0)
848  s->checksum_ptr = s->buffer + checksum_ptr_offset;
849  return 0;
850 }
851 
852 int ffio_set_buf_size(AVIOContext *s, int buf_size)
853 {
854  uint8_t *buffer;
855  buffer = av_malloc(buf_size);
856  if (!buffer)
857  return AVERROR(ENOMEM);
858 
859  av_free(s->buffer);
860  s->buffer = buffer;
861  s->orig_buffer_size =
862  s->buffer_size = buf_size;
863  s->buf_ptr = buffer;
865  return 0;
866 }
867 
868 static int url_resetbuf(AVIOContext *s, int flags)
869 {
870  av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
871 
872  if (flags & AVIO_FLAG_WRITE) {
873  s->buf_end = s->buffer + s->buffer_size;
874  s->write_flag = 1;
875  } else {
876  s->buf_end = s->buffer;
877  s->write_flag = 0;
878  }
879  return 0;
880 }
881 
882 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
883 {
884  int64_t buffer_start;
885  int buffer_size;
886  int overlap, new_size, alloc_size;
887  uint8_t *buf = *bufp;
888 
889  if (s->write_flag) {
890  av_freep(bufp);
891  return AVERROR(EINVAL);
892  }
893 
894  buffer_size = s->buf_end - s->buffer;
895 
896  /* the buffers must touch or overlap */
897  if ((buffer_start = s->pos - buffer_size) > buf_size) {
898  av_freep(bufp);
899  return AVERROR(EINVAL);
900  }
901 
902  overlap = buf_size - buffer_start;
903  new_size = buf_size + buffer_size - overlap;
904 
905  alloc_size = FFMAX(s->buffer_size, new_size);
906  if (alloc_size > buf_size)
907  if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
908  return AVERROR(ENOMEM);
909 
910  if (new_size > buf_size) {
911  memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
912  buf_size = new_size;
913  }
914 
915  av_free(s->buffer);
916  s->buf_ptr = s->buffer = buf;
917  s->buffer_size = alloc_size;
918  s->pos = buf_size;
919  s->buf_end = s->buf_ptr + buf_size;
920  s->eof_reached = 0;
921  s->must_flush = 0;
922 
923  return 0;
924 }
925 
926 int avio_open(AVIOContext **s, const char *filename, int flags)
927 {
928  return avio_open2(s, filename, flags, NULL, NULL);
929 }
930 
931 int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
933  const char *whitelist
934  )
935 {
936  URLContext *h;
937  int err;
938 
939  err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist);
940  if (err < 0)
941  return err;
942  err = ffio_fdopen(s, h);
943  if (err < 0) {
944  ffurl_close(h);
945  return err;
946  }
947  return 0;
948 }
949 
950 int avio_open2(AVIOContext **s, const char *filename, int flags,
952 {
953  return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL);
954 }
955 
956 int ffio_open2_wrapper(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags,
958 {
959  return ffio_open_whitelist(pb, url, flags, int_cb, options, s->protocol_whitelist);
960 }
961 
963 {
964  URLContext *h;
965 
966  if (!s)
967  return 0;
968 
969  avio_flush(s);
970  h = s->opaque;
971  av_freep(&s->buffer);
972  if (s->write_flag)
973  av_log(s, AV_LOG_DEBUG, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
974  else
975  av_log(s, AV_LOG_DEBUG, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
976  av_opt_free(s);
977  av_free(s);
978  return ffurl_close(h);
979 }
980 
982 {
983  int ret = avio_close(*s);
984  *s = NULL;
985  return ret;
986 }
987 
988 int avio_printf(AVIOContext *s, const char *fmt, ...)
989 {
990  va_list ap;
991  char buf[4096]; /* update doc entry in avio.h if changed */
992  int ret;
993 
994  va_start(ap, fmt);
995  ret = vsnprintf(buf, sizeof(buf), fmt, ap);
996  va_end(ap);
997  avio_write(s, buf, strlen(buf));
998  return ret;
999 }
1000 
1001 int avio_pause(AVIOContext *s, int pause)
1002 {
1003  if (!s->read_pause)
1004  return AVERROR(ENOSYS);
1005  return s->read_pause(s->opaque, pause);
1006 }
1007 
1008 int64_t avio_seek_time(AVIOContext *s, int stream_index,
1009  int64_t timestamp, int flags)
1010 {
1011  URLContext *h = s->opaque;
1012  int64_t ret;
1013  if (!s->read_seek)
1014  return AVERROR(ENOSYS);
1015  ret = s->read_seek(h, stream_index, timestamp, flags);
1016  if (ret >= 0) {
1017  int64_t pos;
1018  s->buf_ptr = s->buf_end; // Flush buffer
1019  pos = s->seek(h, 0, SEEK_CUR);
1020  if (pos >= 0)
1021  s->pos = pos;
1022  else if (pos != AVERROR(ENOSYS))
1023  ret = pos;
1024  }
1025  return ret;
1026 }
1027 
1028 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1029 {
1030  int ret;
1031  char buf[1024];
1032  while (max_size) {
1033  ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1034  if (ret == AVERROR_EOF)
1035  return 0;
1036  if (ret <= 0)
1037  return ret;
1038  av_bprint_append_data(pb, buf, ret);
1039  if (!av_bprint_is_complete(pb))
1040  return AVERROR(ENOMEM);
1041  max_size -= ret;
1042  }
1043  return 0;
1044 }
1045 
1047 {
1048  int ret;
1049  URLContext *sc = s->opaque;
1050  URLContext *cc = NULL;
1051  ret = ffurl_accept(sc, &cc);
1052  if (ret < 0)
1053  return ret;
1054  return ffio_fdopen(c, cc);
1055 }
1056 
1058 {
1059  URLContext *cc = c->opaque;
1060  return ffurl_handshake(cc);
1061 }
1062 
1063 /* output in a dynamic buffer */
1064 
1065 typedef struct DynBuffer {
1070 } DynBuffer;
1071 
1072 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1073 {
1074  DynBuffer *d = opaque;
1075  unsigned new_size, new_allocated_size;
1076 
1077  /* reallocate buffer if needed */
1078  new_size = d->pos + buf_size;
1079  new_allocated_size = d->allocated_size;
1080  if (new_size < d->pos || new_size > INT_MAX/2)
1081  return -1;
1082  while (new_size > new_allocated_size) {
1083  if (!new_allocated_size)
1084  new_allocated_size = new_size;
1085  else
1086  new_allocated_size += new_allocated_size / 2 + 1;
1087  }
1088 
1089  if (new_allocated_size > d->allocated_size) {
1090  int err;
1091  if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1092  d->allocated_size = 0;
1093  d->size = 0;
1094  return err;
1095  }
1096  d->allocated_size = new_allocated_size;
1097  }
1098  memcpy(d->buffer + d->pos, buf, buf_size);
1099  d->pos = new_size;
1100  if (d->pos > d->size)
1101  d->size = d->pos;
1102  return buf_size;
1103 }
1104 
1105 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1106 {
1107  unsigned char buf1[4];
1108  int ret;
1109 
1110  /* packetized write: output the header */
1111  AV_WB32(buf1, buf_size);
1112  ret = dyn_buf_write(opaque, buf1, 4);
1113  if (ret < 0)
1114  return ret;
1115 
1116  /* then the data */
1117  return dyn_buf_write(opaque, buf, buf_size);
1118 }
1119 
1120 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1121 {
1122  DynBuffer *d = opaque;
1123 
1124  if (whence == SEEK_CUR)
1125  offset += d->pos;
1126  else if (whence == SEEK_END)
1127  offset += d->size;
1128  if (offset < 0 || offset > 0x7fffffffLL)
1129  return -1;
1130  d->pos = offset;
1131  return 0;
1132 }
1133 
1134 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1135 {
1136  DynBuffer *d;
1137  unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1138 
1139  if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1140  return -1;
1141  d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1142  if (!d)
1143  return AVERROR(ENOMEM);
1144  d->io_buffer_size = io_buffer_size;
1145  *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1146  max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1147  max_packet_size ? NULL : dyn_buf_seek);
1148  if(!*s) {
1149  av_free(d);
1150  return AVERROR(ENOMEM);
1151  }
1152  (*s)->max_packet_size = max_packet_size;
1153  return 0;
1154 }
1155 
1157 {
1158  return url_open_dyn_buf_internal(s, 0);
1159 }
1160 
1161 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1162 {
1163  if (max_packet_size <= 0)
1164  return -1;
1165  return url_open_dyn_buf_internal(s, max_packet_size);
1166 }
1167 
1169 {
1170  DynBuffer *d;
1171  int size;
1172  static const char padbuf[AV_INPUT_BUFFER_PADDING_SIZE] = {0};
1173  int padding = 0;
1174 
1175  if (!s) {
1176  *pbuffer = NULL;
1177  return 0;
1178  }
1179 
1180  /* don't attempt to pad fixed-size packet buffers */
1181  if (!s->max_packet_size) {
1182  avio_write(s, padbuf, sizeof(padbuf));
1183  padding = AV_INPUT_BUFFER_PADDING_SIZE;
1184  }
1185 
1186  avio_flush(s);
1187 
1188  d = s->opaque;
1189  *pbuffer = d->buffer;
1190  size = d->size;
1191  av_free(d);
1192  av_free(s);
1193  return size - padding;
1194 }
1195 
1197 {
1198  uint8_t *tmp;
1199  if (!*s)
1200  return;
1201  avio_close_dyn_buf(*s, &tmp);
1202  av_free(tmp);
1203  *s = NULL;
1204 }
1205 
1206 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1207 {
1208  DynBuffer *d = opaque;
1209 
1210  d->pos += buf_size;
1211  if (d->pos > d->size)
1212  d->size = d->pos;
1213  return buf_size;
1214 }
1215 
1217 {
1218  int ret = url_open_dyn_buf_internal(s, 0);
1219  if (ret >= 0) {
1220  AVIOContext *pb = *s;
1222  }
1223  return ret;
1224 }
1225 
1227 {
1228  DynBuffer *d = s->opaque;
1229  int size;
1230 
1231  avio_flush(s);
1232 
1233  size = d->size;
1234  av_free(d);
1235  av_free(s);
1236  return size;
1237 }
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:412
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
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:116
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
Definition: aviobuf.c:1120
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Buffered I/O operations.
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:765
int64_t(* read_seek)(void *opaque, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp in stream with the specified stream_index.
Definition: avio.h:202
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1156
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:361
#define av_realloc_f(p, o, n)
uint8_t io_buffer[1]
Definition: aviobuf.c:1069
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:430
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
const char * fmt
Definition: avisynth_c.h:632
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:306
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:174
int writeout_count
writeout statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:238
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:175
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:433
#define D
Definition: aviobuf.c:58
int write_flag
true if open for writing
Definition: avio.h:187
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:46
#define vsnprintf
Definition: snprintf.h:36
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:614
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:168
const char * b
Definition: vf_curves.c:109
#define AVIO_FLAG_READ
read-only
Definition: avio.h:537
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:659
struct URLProtocol * prot
Definition: url.h:41
int avio_read_to_bprint(AVIOContext *h, 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:1028
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:538
unsigned char * buffer
Start of the buffer.
Definition: avio.h:172
#define OFFSET(x)
Definition: aviobuf.c:56
void avio_wl24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:436
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:160
int flags
Definition: url.h:44
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:981
int64_t(* seek)(void *opaque, int64_t offset, int whence)
Definition: avio.h:183
void * opaque
A private pointer, passed to the read/write/seek/...
Definition: avio.h:179
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:332
#define IO_BUFFER_SIZE
Definition: aviobuf.c:36
static const AVClass * ff_avio_child_class_next(const AVClass *prev)
Definition: aviobuf.c:51
Format I/O context.
Definition: avformat.h:1314
int avio_accept(AVIOContext *s, AVIOContext **c)
Accept and allocate a client context on a server context.
Definition: aviobuf.c:1046
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:340
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:324
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Public dictionary API.
uint8_t
#define av_malloc(s)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
AVOptions.
static void fill_buffer(AVIOContext *s)
Definition: aviobuf.c:450
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:516
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:202
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1216
int ffio_init_context(AVIOContext *s, 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))
Definition: aviobuf.c:76
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:158
int64_t bytes_read
Bytes read statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:226
char * protocol_whitelist
',' separated list of allowed protocols.
Definition: avformat.h:1846
#define AVERROR_EOF
End of file.
Definition: error.h:55
int ffio_set_buf_size(AVIOContext *s, int buf_size)
Definition: aviobuf.c:852
int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
Read size bytes from AVIOContext, returning a pointer.
Definition: aviobuf.c:602
ptrdiff_t size
Definition: opengl_enc.c:101
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:442
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:424
const OptionDef options[]
Definition: ffserver.c:3962
#define av_log(a,...)
static void flush_buffer(AVIOContext *s)
Definition: aviobuf.c:145
int max_packet_size
Definition: avio.h:188
Callback for checking whether to abort blocking functions.
Definition: avio.h:50
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int io_buffer_size
Definition: aviobuf.c:1068
int(* write_packet)(void *opaque, uint8_t *buf, int buf_size)
Definition: avio.h:182
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:691
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:457
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:1105
const char * protocol_whitelist
Definition: url.h:50
int(* read_packet)(void *opaque, uint8_t *buf, int buf_size)
Definition: avio.h:181
av_default_item_name
static const AVOption ff_avio_options[]
Definition: aviobuf.c:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
simple assert() macros that are a bit more flexible than ISO C assert().
unsigned long checksum
Definition: avio.h:189
static void * ff_avio_child_next(void *obj, void *prev)
Definition: aviobuf.c:45
#define SHORT_SEEK_THRESHOLD
Do seeks within this distance ahead of the current buffer by skipping data instead of calling the pro...
Definition: aviobuf.c:43
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
int direct
avio_read and avio_write should if possible be satisfied directly instead of going through a buffer...
Definition: avio.h:220
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:651
int seek_count
seek statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:232
int avio_pause(AVIOContext *s, int pause)
Pause and resume playing - only meaningful if using a network streaming protocol (e.g.
Definition: aviobuf.c:1001
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:667
int size
Definition: aviobuf.c:1066
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:207
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:594
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:208
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define FFMIN(a, b)
Definition: common.h:96
const AVClass ff_avio_class
Definition: aviobuf.c:64
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1168
unsigned char * checksum_ptr
Definition: avio.h:190
int avio_printf(AVIOContext *s, const char *fmt,...)
Definition: aviobuf.c:988
int ffio_open2_wrapper(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Definition: aviobuf.c:956
int allocated_size
Definition: aviobuf.c:1066
int ffurl_handshake(URLContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition: avio.c:266
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:356
int must_flush
true if the next seek should flush
Definition: avio.h:185
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:1072
int(* url_read_pause)(URLContext *h, int pause)
Definition: url.h:82
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:706
#define GET_STR16(type, read)
Definition: aviobuf.c:741
unsigned long(* update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size)
Definition: avio.h:191
int ffurl_accept(URLContext *s, URLContext **c)
Accept an URLContext c on an URLContext s.
Definition: avio.c:258
int buffer_size
Maximum buffer size.
Definition: avio.h:173
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:418
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
uint8_t le
Definition: crc.c:294
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:37
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:336
int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
Open a write only packetized memory stream with a maximum packet size of 'max_packet_size'.
Definition: aviobuf.c:1161
#define PUT_STR16(type, big_endian)
Definition: aviobuf.c:381
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
int avio_open(AVIOContext **s, const char *filename, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:926
int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist)
Definition: aviobuf.c:931
int64_t(* url_read_seek)(URLContext *h, int stream_index, int64_t timestamp, int flags)
Definition: url.h:83
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file...
Definition: aviobuf.c:882
static int url_resetbuf(AVIOContext *s, int flags)
Definition: aviobuf.c:868
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:675
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:962
void * buf
Definition: avisynth_c.h:553
Definition: url.h:39
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:182
#define AVSEEK_FORCE
Oring this flag as into the "whence" parameter to a seek function causes it to seek by any means (lik...
Definition: avio.h:424
#define AVIO_FLAG_DIRECT
Use direct mode.
Definition: avio.h:564
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:683
int pos
Definition: aviobuf.c:1066
int short_seek_threshold
Threshold to favor readahead over seek.
Definition: avio.h:251
#define PUT_UTF16(val, tmp, PUT_16BIT)
Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).
Definition: common.h:448
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1196
int error
contains the error code or 0 if no error happened
Definition: avio.h:192
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:823
int(* read_pause)(void *opaque, int pause)
Pause or resume playback for network streaming protocols - e.g.
Definition: avio.h:196
int ff_get_v_length(uint64_t val)
Get the length in bytes which is needed to store val as v.
Definition: aviobuf.c:392
static int flags
Definition: cpu.c:47
int ffurl_close(URLContext *h)
Definition: avio.c:479
const AVClass ffurl_context_class
Definition: avio.c:83
int orig_buffer_size
Original buffer size used internally after probing and ensure seekback to reset the buffer size This ...
Definition: avio.h:245
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:342
int avio_handshake(AVIOContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition: aviobuf.c:1057
int avio_open2(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:950
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:287
Main libavformat public API header.
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1447
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:723
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h...
Definition: avio.c:446
int ffio_fdopen(AVIOContext **s, URLContext *h)
Create and initialize a AVIOContext for accessing the resource referenced by the URLContext h...
Definition: aviobuf.c:784
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
Definition: aviobuf.c:524
static double c[64]
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:187
int64_t pos
position in the file of the current buffer
Definition: avio.h:184
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition: avio.h:416
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:635
uint64_t ffio_read_varlen(AVIOContext *bc)
Definition: aviobuf.c:773
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:698
#define av_free(p)
uint8_t * buffer
Definition: aviobuf.c:1067
static int put_str16(AVIOContext *s, const char *str, const int be)
Definition: aviobuf.c:351
int eof_reached
true if eof reached
Definition: avio.h:186
void ff_put_v(AVIOContext *bc, uint64_t val)
Put val using a variable number of bytes.
Definition: aviobuf.c:402
int len
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1226
static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:1206
static void writeout(AVIOContext *s, const uint8_t *data, int len)
Definition: aviobuf.c:133
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
Definition: aviobuf.c:1134
int max_packet_size
if non zero, the stream is packetized with this max packet size
Definition: url.h:45
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:282
#define av_freep(p)
unbuffered private I/O API
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:504
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
Definition: avio.c:419
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
int64_t avio_seek_time(AVIOContext *s, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp relative to some component stream.
Definition: aviobuf.c:1008
unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:510
GLuint buffer
Definition: opengl_enc.c:102