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