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