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