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