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