FFmpeg
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 32768
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 #if FF_API_CHILD_CLASS_NEXT
52 static const AVClass *ff_avio_child_class_next(const AVClass *prev)
53 {
54  return prev ? NULL : &ffurl_context_class;
55 }
56 #endif
57 
58 static const AVClass *child_class_iterate(void **iter)
59 {
60  const AVClass *c = *iter ? NULL : &ffurl_context_class;
61  *iter = (void*)(uintptr_t)c;
62  return c;
63 }
64 
65 #define OFFSET(x) offsetof(AVIOContext,x)
66 #define E AV_OPT_FLAG_ENCODING_PARAM
67 #define D AV_OPT_FLAG_DECODING_PARAM
68 static const AVOption ff_avio_options[] = {
69  {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
70  { NULL },
71 };
72 
74  .class_name = "AVIOContext",
75  .item_name = av_default_item_name,
76  .version = LIBAVUTIL_VERSION_INT,
77  .option = ff_avio_options,
78  .child_next = ff_avio_child_next,
79 #if FF_API_CHILD_CLASS_NEXT
80  .child_class_next = ff_avio_child_class_next,
81 #endif
82  .child_class_iterate = child_class_iterate,
83 };
84 
85 static void fill_buffer(AVIOContext *s);
86 static int url_resetbuf(AVIOContext *s, int flags);
87 
89  unsigned char *buffer,
90  int buffer_size,
91  int write_flag,
92  void *opaque,
93  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
94  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
95  int64_t (*seek)(void *opaque, int64_t offset, int whence))
96 {
97  memset(s, 0, sizeof(AVIOContext));
98 
99  s->buffer = buffer;
100  s->orig_buffer_size =
101  s->buffer_size = buffer_size;
102  s->buf_ptr = buffer;
103  s->buf_ptr_max = buffer;
104  s->opaque = opaque;
105  s->direct = 0;
106 
107  url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
108 
111  s->seek = seek;
112  s->pos = 0;
113  s->eof_reached = 0;
114  s->error = 0;
115  s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
116  s->min_packet_size = 0;
117  s->max_packet_size = 0;
118  s->update_checksum = NULL;
120 
121  if (!read_packet && !write_flag) {
122  s->pos = buffer_size;
123  s->buf_end = s->buffer + buffer_size;
124  }
125  s->read_pause = NULL;
126  s->read_seek = NULL;
127 
128  s->write_data_type = NULL;
129  s->ignore_boundary_point = 0;
132  s->short_seek_get = NULL;
133  s->written = 0;
134 
135  return 0;
136 }
137 
139  unsigned char *buffer,
140  int buffer_size,
141  int write_flag,
142  void *opaque,
143  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
144  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
145  int64_t (*seek)(void *opaque, int64_t offset, int whence))
146 {
147  AVIOContext *s = av_malloc(sizeof(AVIOContext));
148  if (!s)
149  return NULL;
150  ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
151  read_packet, write_packet, seek);
152  return s;
153 }
154 
156 {
157  av_freep(ps);
158 }
159 
160 static void writeout(AVIOContext *s, const uint8_t *data, int len)
161 {
162  if (!s->error) {
163  int ret = 0;
164  if (s->write_data_type)
165  ret = s->write_data_type(s->opaque, (uint8_t *)data,
166  len,
167  s->current_type,
168  s->last_time);
169  else if (s->write_packet)
170  ret = s->write_packet(s->opaque, (uint8_t *)data, len);
171  if (ret < 0) {
172  s->error = ret;
173  } else {
174  if (s->pos + len > s->written)
175  s->written = s->pos + len;
176  }
177  }
181  }
183  s->writeout_count ++;
184  s->pos += len;
185 }
186 
187 static void flush_buffer(AVIOContext *s)
188 {
189  s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);
190  if (s->write_flag && s->buf_ptr_max > s->buffer) {
191  writeout(s, s->buffer, s->buf_ptr_max - s->buffer);
192  if (s->update_checksum) {
194  s->buf_ptr_max - s->checksum_ptr);
195  s->checksum_ptr = s->buffer;
196  }
197  }
198  s->buf_ptr = s->buf_ptr_max = s->buffer;
199  if (!s->write_flag)
200  s->buf_end = s->buffer;
201 }
202 
203 void avio_w8(AVIOContext *s, int b)
204 {
205  av_assert2(b>=-128 && b<=255);
206  *s->buf_ptr++ = b;
207  if (s->buf_ptr >= s->buf_end)
208  flush_buffer(s);
209 }
210 
211 void ffio_fill(AVIOContext *s, int b, int count)
212 {
213  while (count > 0) {
214  int len = FFMIN(s->buf_end - s->buf_ptr, count);
215  memset(s->buf_ptr, b, len);
216  s->buf_ptr += len;
217 
218  if (s->buf_ptr >= s->buf_end)
219  flush_buffer(s);
220 
221  count -= len;
222  }
223 }
224 
225 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
226 {
227  if (s->direct && !s->update_checksum) {
228  avio_flush(s);
229  writeout(s, buf, size);
230  return;
231  }
232  while (size > 0) {
233  int len = FFMIN(s->buf_end - s->buf_ptr, size);
234  memcpy(s->buf_ptr, buf, len);
235  s->buf_ptr += len;
236 
237  if (s->buf_ptr >= s->buf_end)
238  flush_buffer(s);
239 
240  buf += len;
241  size -= len;
242  }
243 }
244 
246 {
247  int seekback = s->write_flag ? FFMIN(0, s->buf_ptr - s->buf_ptr_max) : 0;
248  flush_buffer(s);
249  if (seekback)
250  avio_seek(s, seekback, SEEK_CUR);
251 }
252 
253 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
254 {
255  int64_t offset1;
256  int64_t pos;
257  int force = whence & AVSEEK_FORCE;
258  int buffer_size;
259  int short_seek;
260  whence &= ~AVSEEK_FORCE;
261 
262  if(!s)
263  return AVERROR(EINVAL);
264 
265  if ((whence & AVSEEK_SIZE))
266  return s->seek ? s->seek(s->opaque, offset, AVSEEK_SIZE) : AVERROR(ENOSYS);
267 
268  buffer_size = s->buf_end - s->buffer;
269  // pos is the absolute position that the beginning of s->buffer corresponds to in the file
270  pos = s->pos - (s->write_flag ? 0 : buffer_size);
271 
272  if (whence != SEEK_CUR && whence != SEEK_SET)
273  return AVERROR(EINVAL);
274 
275  if (whence == SEEK_CUR) {
276  offset1 = pos + (s->buf_ptr - s->buffer);
277  if (offset == 0)
278  return offset1;
279  if (offset > INT64_MAX - offset1)
280  return AVERROR(EINVAL);
281  offset += offset1;
282  }
283  if (offset < 0)
284  return AVERROR(EINVAL);
285 
286  if (s->short_seek_get) {
287  short_seek = s->short_seek_get(s->opaque);
288  /* fallback to default short seek */
289  if (short_seek <= 0)
290  short_seek = s->short_seek_threshold;
291  } else
292  short_seek = s->short_seek_threshold;
293 
294  offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
295  s->buf_ptr_max = FFMAX(s->buf_ptr_max, s->buf_ptr);
296  if ((!s->direct || !s->seek) &&
297  offset1 >= 0 && offset1 <= (s->write_flag ? s->buf_ptr_max - s->buffer : buffer_size)) {
298  /* can do the seek inside the buffer */
299  s->buf_ptr = s->buffer + offset1;
300  } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
301  offset1 <= buffer_size + short_seek) &&
302  !s->write_flag && offset1 >= 0 &&
303  (!s->direct || !s->seek) &&
304  (whence != SEEK_END || force)) {
305  while(s->pos < offset && !s->eof_reached)
306  fill_buffer(s);
307  if (s->eof_reached)
308  return AVERROR_EOF;
309  s->buf_ptr = s->buf_end - (s->pos - offset);
310  } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
311  int64_t res;
312 
313  pos -= FFMIN(buffer_size>>1, pos);
314  if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
315  return res;
316  s->buf_end =
317  s->buf_ptr = s->buffer;
318  s->pos = pos;
319  s->eof_reached = 0;
320  fill_buffer(s);
321  return avio_seek(s, offset, SEEK_SET | force);
322  } else {
323  int64_t res;
324  if (s->write_flag) {
325  flush_buffer(s);
326  }
327  if (!s->seek)
328  return AVERROR(EPIPE);
329  if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
330  return res;
331  s->seek_count ++;
332  if (!s->write_flag)
333  s->buf_end = s->buffer;
334  s->buf_ptr = s->buf_ptr_max = s->buffer;
335  s->pos = offset;
336  }
337  s->eof_reached = 0;
338  return offset;
339 }
340 
341 int64_t avio_skip(AVIOContext *s, int64_t offset)
342 {
343  return avio_seek(s, offset, SEEK_CUR);
344 }
345 
347 {
348  int64_t size;
349 
350  if (!s)
351  return AVERROR(EINVAL);
352 
353  if (s->written)
354  return s->written;
355 
356  if (!s->seek)
357  return AVERROR(ENOSYS);
358  size = s->seek(s->opaque, 0, AVSEEK_SIZE);
359  if (size < 0) {
360  if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
361  return size;
362  size++;
363  s->seek(s->opaque, s->pos, SEEK_SET);
364  }
365  return size;
366 }
367 
369 {
370  if(!s)
371  return 0;
372  if(s->eof_reached){
373  s->eof_reached=0;
374  fill_buffer(s);
375  }
376  return s->eof_reached;
377 }
378 
379 void avio_wl32(AVIOContext *s, unsigned int val)
380 {
381  avio_w8(s, (uint8_t) val );
382  avio_w8(s, (uint8_t)(val >> 8 ));
383  avio_w8(s, (uint8_t)(val >> 16));
384  avio_w8(s, val >> 24 );
385 }
386 
387 void avio_wb32(AVIOContext *s, unsigned int val)
388 {
389  avio_w8(s, val >> 24 );
390  avio_w8(s, (uint8_t)(val >> 16));
391  avio_w8(s, (uint8_t)(val >> 8 ));
392  avio_w8(s, (uint8_t) val );
393 }
394 
395 int avio_put_str(AVIOContext *s, const char *str)
396 {
397  int len = 1;
398  if (str) {
399  len += strlen(str);
400  avio_write(s, (const unsigned char *) str, len);
401  } else
402  avio_w8(s, 0);
403  return len;
404 }
405 
406 static inline int put_str16(AVIOContext *s, const char *str, const int be)
407 {
408  const uint8_t *q = str;
409  int ret = 0;
410  int err = 0;
411 
412  while (*q) {
413  uint32_t ch;
414  uint16_t tmp;
415 
416  GET_UTF8(ch, *q++, goto invalid;)
417  PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
418  ret += 2;)
419  continue;
420 invalid:
421  av_log(s, AV_LOG_ERROR, "Invalid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
422  err = AVERROR(EINVAL);
423  if (!*(q-1))
424  break;
425  }
426  if (be)
427  avio_wb16(s, 0);
428  else
429  avio_wl16(s, 0);
430  if (err)
431  return err;
432  ret += 2;
433  return ret;
434 }
435 
436 #define PUT_STR16(type, big_endian) \
437 int avio_put_str16 ## type(AVIOContext *s, const char *str) \
438 { \
439 return put_str16(s, str, big_endian); \
440 }
441 
442 PUT_STR16(le, 0)
443 PUT_STR16(be, 1)
444 
445 #undef PUT_STR16
446 
447 void avio_wl64(AVIOContext *s, uint64_t val)
448 {
449  avio_wl32(s, (uint32_t)(val & 0xffffffff));
450  avio_wl32(s, (uint32_t)(val >> 32));
451 }
452 
453 void avio_wb64(AVIOContext *s, uint64_t val)
454 {
455  avio_wb32(s, (uint32_t)(val >> 32));
456  avio_wb32(s, (uint32_t)(val & 0xffffffff));
457 }
458 
459 void avio_wl16(AVIOContext *s, unsigned int val)
460 {
461  avio_w8(s, (uint8_t)val);
462  avio_w8(s, (int)val >> 8);
463 }
464 
465 void avio_wb16(AVIOContext *s, unsigned int val)
466 {
467  avio_w8(s, (int)val >> 8);
468  avio_w8(s, (uint8_t)val);
469 }
470 
471 void avio_wl24(AVIOContext *s, unsigned int val)
472 {
473  avio_wl16(s, val & 0xffff);
474  avio_w8(s, (int)val >> 16);
475 }
476 
477 void avio_wb24(AVIOContext *s, unsigned int val)
478 {
479  avio_wb16(s, (int)val >> 8);
480  avio_w8(s, (uint8_t)val);
481 }
482 
484 {
485  if (type == AVIO_DATA_MARKER_FLUSH_POINT) {
486  if (s->buf_ptr - s->buffer >= s->min_packet_size)
487  avio_flush(s);
488  return;
489  }
490  if (!s->write_data_type)
491  return;
492  // If ignoring boundary points, just treat it as unknown
495  // Avoid unnecessary flushes if we are already in non-header/trailer
496  // data and setting the type to unknown
497  if (type == AVIO_DATA_MARKER_UNKNOWN &&
500  return;
501 
502  switch (type) {
505  // For header/trailer, ignore a new marker of the same type;
506  // consecutive header/trailer markers can be merged.
507  if (type == s->current_type)
508  return;
509  break;
510  }
511 
512  // If we've reached here, we have a new, noteworthy marker.
513  // Flush the previous data and mark the start of the new data.
514  avio_flush(s);
515  s->current_type = type;
516  s->last_time = time;
517 }
518 
519 static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
520 {
521  int ret;
522 
523  if (!s->read_packet)
524  return AVERROR(EINVAL);
525  ret = s->read_packet(s->opaque, buf, size);
526 #if FF_API_OLD_AVIO_EOF_0
527  if (!ret && !s->max_packet_size) {
528  av_log(NULL, AV_LOG_WARNING, "Invalid return value 0 for stream protocol\n");
529  ret = AVERROR_EOF;
530  }
531 #else
532  av_assert2(ret || s->max_packet_size);
533 #endif
534  return ret;
535 }
536 
537 /* Input stream */
538 
539 static void fill_buffer(AVIOContext *s)
540 {
541  int max_buffer_size = s->max_packet_size ?
543  uint8_t *dst = s->buf_end - s->buffer + max_buffer_size <= s->buffer_size ?
544  s->buf_end : s->buffer;
545  int len = s->buffer_size - (dst - s->buffer);
546 
547  /* can't fill the buffer without read_packet, just set EOF if appropriate */
548  if (!s->read_packet && s->buf_ptr >= s->buf_end)
549  s->eof_reached = 1;
550 
551  /* no need to do anything if EOF already reached */
552  if (s->eof_reached)
553  return;
554 
555  if (s->update_checksum && dst == s->buffer) {
556  if (s->buf_end > s->checksum_ptr)
558  s->buf_end - s->checksum_ptr);
559  s->checksum_ptr = s->buffer;
560  }
561 
562  /* make buffer smaller in case it ended up large after probing */
563  if (s->read_packet && s->orig_buffer_size && s->buffer_size > s->orig_buffer_size && len >= s->orig_buffer_size) {
564  if (dst == s->buffer && s->buf_ptr != dst) {
566  if (ret < 0)
567  av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
568 
569  s->checksum_ptr = dst = s->buffer;
570  }
571  len = s->orig_buffer_size;
572  }
573 
574  len = read_packet_wrapper(s, dst, len);
575  if (len == AVERROR_EOF) {
576  /* do not modify buffer if EOF reached so that a seek back can
577  be done without rereading data */
578  s->eof_reached = 1;
579  } else if (len < 0) {
580  s->eof_reached = 1;
581  s->error= len;
582  } else {
583  s->pos += len;
584  s->buf_ptr = dst;
585  s->buf_end = dst + len;
586  s->bytes_read += len;
587  }
588 }
589 
590 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
591  unsigned int len)
592 {
593  return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
594 }
595 
596 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
597  unsigned int len)
598 {
599  return av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), checksum, buf, len);
600 }
601 
602 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
603  unsigned int len)
604 {
605  return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
606 }
607 
609 {
611  s->buf_ptr - s->checksum_ptr);
612  s->update_checksum = NULL;
613  return s->checksum;
614 }
615 
617  unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
618  unsigned long checksum)
619 {
621  if (s->update_checksum) {
622  s->checksum = checksum;
623  s->checksum_ptr = s->buf_ptr;
624  }
625 }
626 
627 /* XXX: put an inline version */
629 {
630  if (s->buf_ptr >= s->buf_end)
631  fill_buffer(s);
632  if (s->buf_ptr < s->buf_end)
633  return *s->buf_ptr++;
634  return 0;
635 }
636 
637 int avio_read(AVIOContext *s, unsigned char *buf, int size)
638 {
639  int len, size1;
640 
641  size1 = size;
642  while (size > 0) {
643  len = FFMIN(s->buf_end - s->buf_ptr, size);
644  if (len == 0 || s->write_flag) {
645  if((s->direct || size > s->buffer_size) && !s->update_checksum) {
646  // bypass the buffer and read data directly into buf
647  len = read_packet_wrapper(s, buf, size);
648  if (len == AVERROR_EOF) {
649  /* do not modify buffer if EOF reached so that a seek back can
650  be done without rereading data */
651  s->eof_reached = 1;
652  break;
653  } else if (len < 0) {
654  s->eof_reached = 1;
655  s->error= len;
656  break;
657  } else {
658  s->pos += len;
659  s->bytes_read += len;
660  size -= len;
661  buf += len;
662  // reset the buffer
663  s->buf_ptr = s->buffer;
664  s->buf_end = s->buffer/* + len*/;
665  }
666  } else {
667  fill_buffer(s);
668  len = s->buf_end - s->buf_ptr;
669  if (len == 0)
670  break;
671  }
672  } else {
673  memcpy(buf, s->buf_ptr, len);
674  buf += len;
675  s->buf_ptr += len;
676  size -= len;
677  }
678  }
679  if (size1 == size) {
680  if (s->error) return s->error;
681  if (avio_feof(s)) return AVERROR_EOF;
682  }
683  return size1 - size;
684 }
685 
686 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
687 {
688  int ret = avio_read(s, buf, size);
689  if (ret == size)
690  return ret;
691  if (ret < 0 && ret != AVERROR_EOF)
692  return ret;
693  return AVERROR_INVALIDDATA;
694 }
695 
696 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
697 {
698  if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
699  *data = s->buf_ptr;
700  s->buf_ptr += size;
701  return size;
702  } else {
703  *data = buf;
704  return avio_read(s, buf, size);
705  }
706 }
707 
708 int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
709 {
710  int len;
711 
712  if (size < 0)
713  return AVERROR(EINVAL);
714 
715  if (s->read_packet && s->write_flag) {
716  len = read_packet_wrapper(s, buf, size);
717  if (len > 0)
718  s->pos += len;
719  return len;
720  }
721 
722  len = s->buf_end - s->buf_ptr;
723  if (len == 0) {
724  fill_buffer(s);
725  len = s->buf_end - s->buf_ptr;
726  }
727  if (len > size)
728  len = size;
729  memcpy(buf, s->buf_ptr, len);
730  s->buf_ptr += len;
731  if (!len) {
732  if (s->error) return s->error;
733  if (avio_feof(s)) return AVERROR_EOF;
734  }
735  return len;
736 }
737 
738 unsigned int avio_rl16(AVIOContext *s)
739 {
740  unsigned int val;
741  val = avio_r8(s);
742  val |= avio_r8(s) << 8;
743  return val;
744 }
745 
746 unsigned int avio_rl24(AVIOContext *s)
747 {
748  unsigned int val;
749  val = avio_rl16(s);
750  val |= avio_r8(s) << 16;
751  return val;
752 }
753 
754 unsigned int avio_rl32(AVIOContext *s)
755 {
756  unsigned int val;
757  val = avio_rl16(s);
758  val |= avio_rl16(s) << 16;
759  return val;
760 }
761 
762 uint64_t avio_rl64(AVIOContext *s)
763 {
764  uint64_t val;
765  val = (uint64_t)avio_rl32(s);
766  val |= (uint64_t)avio_rl32(s) << 32;
767  return val;
768 }
769 
770 unsigned int avio_rb16(AVIOContext *s)
771 {
772  unsigned int val;
773  val = avio_r8(s) << 8;
774  val |= avio_r8(s);
775  return val;
776 }
777 
778 unsigned int avio_rb24(AVIOContext *s)
779 {
780  unsigned int val;
781  val = avio_rb16(s) << 8;
782  val |= avio_r8(s);
783  return val;
784 }
785 unsigned int avio_rb32(AVIOContext *s)
786 {
787  unsigned int val;
788  val = avio_rb16(s) << 16;
789  val |= avio_rb16(s);
790  return val;
791 }
792 
793 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
794 {
795  int i = 0;
796  char c;
797 
798  do {
799  c = avio_r8(s);
800  if (c && i < maxlen-1)
801  buf[i++] = c;
802  } while (c != '\n' && c != '\r' && c);
803  if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
804  avio_skip(s, -1);
805 
806  buf[i] = 0;
807  return i;
808 }
809 
810 int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
811 {
812  int len = ff_get_line(s, buf, maxlen);
813  while (len > 0 && av_isspace(buf[len - 1]))
814  buf[--len] = '\0';
815  return len;
816 }
817 
818 int64_t ff_read_line_to_bprint(AVIOContext *s, AVBPrint *bp)
819 {
820  int len, end;
821  int64_t read = 0;
822  char tmp[1024];
823  char c;
824 
825  do {
826  len = 0;
827  do {
828  c = avio_r8(s);
829  end = (c == '\r' || c == '\n' || c == '\0');
830  if (!end)
831  tmp[len++] = c;
832  } while (!end && len < sizeof(tmp));
833  av_bprint_append_data(bp, tmp, len);
834  read += len;
835  } while (!end);
836 
837  if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
838  avio_skip(s, -1);
839 
840  if (!c && s->error)
841  return s->error;
842 
843  if (!c && !read && avio_feof(s))
844  return AVERROR_EOF;
845 
846  return read;
847 }
848 
850 {
851  int64_t ret;
852 
853  av_bprint_clear(bp);
854  ret = ff_read_line_to_bprint(s, bp);
855  if (ret < 0)
856  return ret;
857 
858  if (!av_bprint_is_complete(bp))
859  return AVERROR(ENOMEM);
860 
861  return bp->len;
862 }
863 
864 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
865 {
866  int i;
867 
868  if (buflen <= 0)
869  return AVERROR(EINVAL);
870  // reserve 1 byte for terminating 0
871  buflen = FFMIN(buflen - 1, maxlen);
872  for (i = 0; i < buflen; i++)
873  if (!(buf[i] = avio_r8(s)))
874  return i + 1;
875  buf[i] = 0;
876  for (; i < maxlen; i++)
877  if (!avio_r8(s))
878  return i + 1;
879  return maxlen;
880 }
881 
882 #define GET_STR16(type, read) \
883  int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
884 {\
885  char* q = buf;\
886  int ret = 0;\
887  if (buflen <= 0) \
888  return AVERROR(EINVAL); \
889  while (ret + 1 < maxlen) {\
890  uint8_t tmp;\
891  uint32_t ch;\
892  GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
893  if (!ch)\
894  break;\
895  PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
896  }\
897  *q = 0;\
898  return ret;\
899 }\
900 
901 GET_STR16(le, avio_rl16)
902 GET_STR16(be, avio_rb16)
903 
904 #undef GET_STR16
905 
906 uint64_t avio_rb64(AVIOContext *s)
907 {
908  uint64_t val;
909  val = (uint64_t)avio_rb32(s) << 32;
910  val |= (uint64_t)avio_rb32(s);
911  return val;
912 }
913 
915  uint64_t val = 0;
916  int tmp;
917 
918  do{
919  tmp = avio_r8(bc);
920  val= (val<<7) + (tmp&127);
921  }while(tmp&128);
922  return val;
923 }
924 
926 {
927  uint8_t *buffer = NULL;
928  int buffer_size, max_packet_size;
929 
930  max_packet_size = h->max_packet_size;
931  if (max_packet_size) {
932  buffer_size = max_packet_size; /* no need to bufferize more than one packet */
933  } else {
934  buffer_size = IO_BUFFER_SIZE;
935  }
936  if (!(h->flags & AVIO_FLAG_WRITE) && h->is_streamed) {
937  if (buffer_size > INT_MAX/2)
938  return AVERROR(EINVAL);
939  buffer_size *= 2;
940  }
941  buffer = av_malloc(buffer_size);
942  if (!buffer)
943  return AVERROR(ENOMEM);
944 
945  *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
946  (int (*)(void *, uint8_t *, int)) ffurl_read,
947  (int (*)(void *, uint8_t *, int)) ffurl_write,
948  (int64_t (*)(void *, int64_t, int))ffurl_seek);
949  if (!*s)
950  goto fail;
951 
952  (*s)->protocol_whitelist = av_strdup(h->protocol_whitelist);
953  if (!(*s)->protocol_whitelist && h->protocol_whitelist) {
954  avio_closep(s);
955  goto fail;
956  }
957  (*s)->protocol_blacklist = av_strdup(h->protocol_blacklist);
958  if (!(*s)->protocol_blacklist && h->protocol_blacklist) {
959  avio_closep(s);
960  goto fail;
961  }
962  (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
963 
964  (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
965  (*s)->max_packet_size = max_packet_size;
966  (*s)->min_packet_size = h->min_packet_size;
967  if(h->prot) {
968  (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
969  (*s)->read_seek =
970  (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
971 
972  if (h->prot->url_read_seek)
973  (*s)->seekable |= AVIO_SEEKABLE_TIME;
974  }
975  (*s)->short_seek_get = (int (*)(void *))ffurl_get_short_seek;
976  (*s)->av_class = &ff_avio_class;
977  return 0;
978 fail:
979  av_freep(&buffer);
980  return AVERROR(ENOMEM);
981 }
982 
984 {
985  if (!s)
986  return NULL;
987 
988  if (s->opaque && s->read_packet == (int (*)(void *, uint8_t *, int))ffurl_read)
989  return s->opaque;
990  else
991  return NULL;
992 }
993 
995 {
996  if (s->update_checksum && s->buf_ptr > s->checksum_ptr) {
998  s->buf_ptr - s->checksum_ptr);
999  }
1000 }
1001 
1002 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
1003 {
1004  uint8_t *buffer;
1005  int max_buffer_size = s->max_packet_size ?
1007  ptrdiff_t filled = s->buf_end - s->buf_ptr;
1008 
1009  if (buf_size <= s->buf_end - s->buf_ptr)
1010  return 0;
1011 
1012  buf_size += max_buffer_size - 1;
1013 
1014  if (buf_size + s->buf_ptr - s->buffer <= s->buffer_size || s->seekable || !s->read_packet)
1015  return 0;
1016  av_assert0(!s->write_flag);
1017 
1018  if (buf_size <= s->buffer_size) {
1019  update_checksum(s);
1020  memmove(s->buffer, s->buf_ptr, filled);
1021  } else {
1022  buffer = av_malloc(buf_size);
1023  if (!buffer)
1024  return AVERROR(ENOMEM);
1025  update_checksum(s);
1026  memcpy(buffer, s->buf_ptr, filled);
1027  av_free(s->buffer);
1028  s->buffer = buffer;
1029  s->buffer_size = buf_size;
1030  }
1031  s->buf_ptr = s->buffer;
1032  s->buf_end = s->buffer + filled;
1033  s->checksum_ptr = s->buffer;
1034  return 0;
1035 }
1036 
1037 int ffio_set_buf_size(AVIOContext *s, int buf_size)
1038 {
1039  uint8_t *buffer;
1040  buffer = av_malloc(buf_size);
1041  if (!buffer)
1042  return AVERROR(ENOMEM);
1043 
1044  av_free(s->buffer);
1045  s->buffer = buffer;
1046  s->orig_buffer_size =
1047  s->buffer_size = buf_size;
1048  s->buf_ptr = s->buf_ptr_max = buffer;
1050  return 0;
1051 }
1052 
1053 int ffio_realloc_buf(AVIOContext *s, int buf_size)
1054 {
1055  uint8_t *buffer;
1056  int data_size;
1057 
1058  if (!s->buffer_size)
1059  return ffio_set_buf_size(s, buf_size);
1060 
1061  if (buf_size <= s->buffer_size)
1062  return 0;
1063 
1064  buffer = av_malloc(buf_size);
1065  if (!buffer)
1066  return AVERROR(ENOMEM);
1067 
1068  data_size = s->write_flag ? (s->buf_ptr - s->buffer) : (s->buf_end - s->buf_ptr);
1069  if (data_size > 0)
1070  memcpy(buffer, s->write_flag ? s->buffer : s->buf_ptr, data_size);
1071  av_free(s->buffer);
1072  s->buffer = buffer;
1073  s->orig_buffer_size = buf_size;
1074  s->buffer_size = buf_size;
1075  s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer;
1076  if (s->write_flag)
1077  s->buf_ptr_max = s->buffer + data_size;
1078 
1079  s->buf_end = s->write_flag ? (s->buffer + s->buffer_size) : (s->buf_ptr + data_size);
1080 
1081  return 0;
1082 }
1083 
1084 static int url_resetbuf(AVIOContext *s, int flags)
1085 {
1086  av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
1087 
1088  if (flags & AVIO_FLAG_WRITE) {
1089  s->buf_end = s->buffer + s->buffer_size;
1090  s->write_flag = 1;
1091  } else {
1092  s->buf_end = s->buffer;
1093  s->write_flag = 0;
1094  }
1095  return 0;
1096 }
1097 
1098 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
1099 {
1100  int64_t buffer_start;
1101  int buffer_size;
1102  int overlap, new_size, alloc_size;
1103  uint8_t *buf = *bufp;
1104 
1105  if (s->write_flag) {
1106  av_freep(bufp);
1107  return AVERROR(EINVAL);
1108  }
1109 
1110  buffer_size = s->buf_end - s->buffer;
1111 
1112  /* the buffers must touch or overlap */
1113  if ((buffer_start = s->pos - buffer_size) > buf_size) {
1114  av_freep(bufp);
1115  return AVERROR(EINVAL);
1116  }
1117 
1118  overlap = buf_size - buffer_start;
1119  new_size = buf_size + buffer_size - overlap;
1120 
1121  alloc_size = FFMAX(s->buffer_size, new_size);
1122  if (alloc_size > buf_size)
1123  if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
1124  return AVERROR(ENOMEM);
1125 
1126  if (new_size > buf_size) {
1127  memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
1128  buf_size = new_size;
1129  }
1130 
1131  av_free(s->buffer);
1132  s->buf_ptr = s->buffer = buf;
1133  s->buffer_size = alloc_size;
1134  s->pos = buf_size;
1135  s->buf_end = s->buf_ptr + buf_size;
1136  s->eof_reached = 0;
1137 
1138  return 0;
1139 }
1140 
1141 int avio_open(AVIOContext **s, const char *filename, int flags)
1142 {
1143  return avio_open2(s, filename, flags, NULL, NULL);
1144 }
1145 
1146 int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
1148  const char *whitelist, const char *blacklist
1149  )
1150 {
1151  URLContext *h;
1152  int err;
1153 
1154  *s = NULL;
1155 
1156  err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist, blacklist, NULL);
1157  if (err < 0)
1158  return err;
1159  err = ffio_fdopen(s, h);
1160  if (err < 0) {
1161  ffurl_close(h);
1162  return err;
1163  }
1164  return 0;
1165 }
1166 
1167 int avio_open2(AVIOContext **s, const char *filename, int flags,
1169 {
1170  return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
1171 }
1172 
1174 {
1175  URLContext *h;
1176 
1177  if (!s)
1178  return 0;
1179 
1180  avio_flush(s);
1181  h = s->opaque;
1182  s->opaque = NULL;
1183 
1184  av_freep(&s->buffer);
1185  if (s->write_flag)
1186  av_log(s, AV_LOG_VERBOSE, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
1187  else
1188  av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
1189  av_opt_free(s);
1190 
1191  avio_context_free(&s);
1192 
1193  return ffurl_close(h);
1194 }
1195 
1197 {
1198  int ret = avio_close(*s);
1199  *s = NULL;
1200  return ret;
1201 }
1202 
1203 int avio_printf(AVIOContext *s, const char *fmt, ...)
1204 {
1205  va_list ap;
1206  AVBPrint bp;
1207 
1208  av_bprint_init(&bp, 0, INT_MAX);
1209  va_start(ap, fmt);
1210  av_vbprintf(&bp, fmt, ap);
1211  va_end(ap);
1212  if (!av_bprint_is_complete(&bp)) {
1213  av_bprint_finalize(&bp, NULL);
1214  s->error = AVERROR(ENOMEM);
1215  return AVERROR(ENOMEM);
1216  }
1217  avio_write(s, bp.str, bp.len);
1218  av_bprint_finalize(&bp, NULL);
1219  return bp.len;
1220 }
1221 
1222 void avio_print_string_array(AVIOContext *s, const char *strings[])
1223 {
1224  for(; *strings; strings++)
1225  avio_write(s, (const unsigned char *)*strings, strlen(*strings));
1226 }
1227 
1228 int avio_pause(AVIOContext *s, int pause)
1229 {
1230  if (!s->read_pause)
1231  return AVERROR(ENOSYS);
1232  return s->read_pause(s->opaque, pause);
1233 }
1234 
1235 int64_t avio_seek_time(AVIOContext *s, int stream_index,
1236  int64_t timestamp, int flags)
1237 {
1238  int64_t ret;
1239  if (!s->read_seek)
1240  return AVERROR(ENOSYS);
1241  ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1242  if (ret >= 0) {
1243  int64_t pos;
1244  s->buf_ptr = s->buf_end; // Flush buffer
1245  pos = s->seek(s->opaque, 0, SEEK_CUR);
1246  if (pos >= 0)
1247  s->pos = pos;
1248  else if (pos != AVERROR(ENOSYS))
1249  ret = pos;
1250  }
1251  return ret;
1252 }
1253 
1254 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1255 {
1256  int ret;
1257  char buf[1024];
1258  while (max_size) {
1259  ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1260  if (ret == AVERROR_EOF)
1261  return 0;
1262  if (ret <= 0)
1263  return ret;
1264  av_bprint_append_data(pb, buf, ret);
1265  if (!av_bprint_is_complete(pb))
1266  return AVERROR(ENOMEM);
1267  max_size -= ret;
1268  }
1269  return 0;
1270 }
1271 
1273 {
1274  int ret;
1275  URLContext *sc = s->opaque;
1276  URLContext *cc = NULL;
1277  ret = ffurl_accept(sc, &cc);
1278  if (ret < 0)
1279  return ret;
1280  return ffio_fdopen(c, cc);
1281 }
1282 
1284 {
1285  URLContext *cc = c->opaque;
1286  return ffurl_handshake(cc);
1287 }
1288 
1289 /* output in a dynamic buffer */
1290 
1291 typedef struct DynBuffer {
1296 } DynBuffer;
1297 
1298 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1299 {
1300  DynBuffer *d = opaque;
1301  unsigned new_size;
1302 
1303  /* reallocate buffer if needed */
1304  new_size = (unsigned)d->pos + buf_size;
1305  if (new_size < d->pos || new_size > INT_MAX)
1306  return AVERROR(ERANGE);
1307  if (new_size > d->allocated_size) {
1308  unsigned new_allocated_size = d->allocated_size ? d->allocated_size
1309  : new_size;
1310  int err;
1311  while (new_size > new_allocated_size)
1312  new_allocated_size += new_allocated_size / 2 + 1;
1313 
1314  new_allocated_size = FFMIN(new_allocated_size, INT_MAX);
1315 
1316  if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1317  d->allocated_size = 0;
1318  d->size = 0;
1319  return err;
1320  }
1321  d->allocated_size = new_allocated_size;
1322  }
1323  memcpy(d->buffer + d->pos, buf, buf_size);
1324  d->pos = new_size;
1325  if (d->pos > d->size)
1326  d->size = d->pos;
1327  return buf_size;
1328 }
1329 
1330 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1331 {
1332  unsigned char buf1[4];
1333  int ret;
1334 
1335  /* packetized write: output the header */
1336  AV_WB32(buf1, buf_size);
1337  ret = dyn_buf_write(opaque, buf1, 4);
1338  if (ret < 0)
1339  return ret;
1340 
1341  /* then the data */
1342  return dyn_buf_write(opaque, buf, buf_size);
1343 }
1344 
1345 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1346 {
1347  DynBuffer *d = opaque;
1348 
1349  if (whence == SEEK_CUR)
1350  offset += d->pos;
1351  else if (whence == SEEK_END)
1352  offset += d->size;
1353  if (offset < 0)
1354  return AVERROR(EINVAL);
1355  if (offset > INT_MAX)
1356  return AVERROR(ERANGE);
1357  d->pos = offset;
1358  return 0;
1359 }
1360 
1361 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1362 {
1363  DynBuffer *d;
1364  unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1365 
1366  if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1367  return AVERROR(ERANGE);
1368  d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1369  if (!d)
1370  return AVERROR(ENOMEM);
1372  *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1373  max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1374  max_packet_size ? NULL : dyn_buf_seek);
1375  if(!*s) {
1376  av_free(d);
1377  return AVERROR(ENOMEM);
1378  }
1379  (*s)->max_packet_size = max_packet_size;
1380  return 0;
1381 }
1382 
1384 {
1385  return url_open_dyn_buf_internal(s, 0);
1386 }
1387 
1388 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1389 {
1390  if (max_packet_size <= 0)
1391  return AVERROR(EINVAL);
1392  return url_open_dyn_buf_internal(s, max_packet_size);
1393 }
1394 
1396 {
1397  DynBuffer *d;
1398 
1399  if (!s) {
1400  *pbuffer = NULL;
1401  return 0;
1402  }
1403  d = s->opaque;
1404 
1405  if (!s->error && !d->size) {
1406  *pbuffer = d->io_buffer;
1407  return FFMAX(s->buf_ptr, s->buf_ptr_max) - s->buffer;
1408  }
1409 
1410  avio_flush(s);
1411 
1412  *pbuffer = d->buffer;
1413 
1414  return d->size;
1415 }
1416 
1418 {
1419  DynBuffer *d = s->opaque;
1420  int max_packet_size = s->max_packet_size;
1421 
1423  s->write_packet, s->seek);
1424  s->max_packet_size = max_packet_size;
1425  d->pos = d->size = 0;
1426 }
1427 
1429 {
1430  DynBuffer *d;
1431  int size;
1432  static const char padbuf[AV_INPUT_BUFFER_PADDING_SIZE] = {0};
1433  int padding = 0;
1434 
1435  if (!s) {
1436  *pbuffer = NULL;
1437  return 0;
1438  }
1439 
1440  /* don't attempt to pad fixed-size packet buffers */
1441  if (!s->max_packet_size) {
1442  avio_write(s, padbuf, sizeof(padbuf));
1443  padding = AV_INPUT_BUFFER_PADDING_SIZE;
1444  }
1445 
1446  avio_flush(s);
1447 
1448  d = s->opaque;
1449  *pbuffer = d->buffer;
1450  size = d->size;
1451  av_free(d);
1452 
1453  avio_context_free(&s);
1454 
1455  return size - padding;
1456 }
1457 
1459 {
1460  DynBuffer *d;
1461 
1462  if (!*s)
1463  return;
1464 
1465  d = (*s)->opaque;
1466  av_free(d->buffer);
1467  av_free(d);
1468  avio_context_free(s);
1469 }
1470 
1471 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1472 {
1473  DynBuffer *d = opaque;
1474 
1475  d->pos += buf_size;
1476  if (d->pos > d->size)
1477  d->size = d->pos;
1478  return buf_size;
1479 }
1480 
1482 {
1483  int ret = url_open_dyn_buf_internal(s, 0);
1484  if (ret >= 0) {
1485  AVIOContext *pb = *s;
1487  }
1488  return ret;
1489 }
1490 
1492 {
1493  DynBuffer *d = s->opaque;
1494  int size;
1495 
1496  avio_flush(s);
1497 
1498  size = d->size;
1499  av_free(d);
1500 
1501  avio_context_free(&s);
1502 
1503  return size;
1504 }
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:703
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:447
#define NULL
Definition: coverity.c:32
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:138
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
Definition: aviobuf.c:1345
Bytestream IO Context.
Definition: avio.h:161
#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:906
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1383
#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:426
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:310
static void update_checksum(AVIOContext *s)
Definition: aviobuf.c:994
#define av_realloc_f(p, o, n)
uint8_t io_buffer[1]
Definition: aviobuf.c:1295
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:465
AVOption.
Definition: opt.h:248
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int64_t last_time
Definition: avio.h:332
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:368
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:228
int writeout_count
writeout statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:291
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:229
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:423
#define D
Definition: aviobuf.c:67
int write_flag
true if open for writing
Definition: avio.h:240
void ffio_reset_dyn_buf(AVIOContext *s)
Reset a dynamic buffer.
Definition: aviobuf.c:1417
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:45
static const AVClass * child_class_iterate(void **iter)
Definition: aviobuf.c:58
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1395
#define AVIO_SEEKABLE_TIME
Seeking by timestamp with avio_seek_time() is possible.
Definition: avio.h:45
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:211
#define AVIO_FLAG_READ
read-only
Definition: avio.h:674
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:746
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:227
int min_packet_size
Try to buffer at least this amount of data before flushing it.
Definition: avio.h:351
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:1254
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:675
unsigned char * buffer
Start of the buffer.
Definition: avio.h:226
#define OFFSET(x)
Definition: aviobuf.c:65
GLint GLenum type
Definition: opengl_enc.c:104
void avio_wl24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:471
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:203
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:1196
void * opaque
A private pointer, passed to the read/write/seek/...
Definition: avio.h:233
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:387
#define IO_BUFFER_SIZE
Definition: aviobuf.c:36
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
Trailer data, which doesn&#39;t contain actual content, but only for finalizing the output file...
Definition: avio.h:140
int avio_accept(AVIOContext *s, AVIOContext **c)
Accept and allocate a client context on a server context.
Definition: aviobuf.c:1272
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:395
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:379
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:64
AVOptions.
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition: avio.h:146
static void fill_buffer(AVIOContext *s)
Definition: aviobuf.c:539
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:608
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:483
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:245
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1481
URLContext * ffio_geturlcontext(AVIOContext *s)
Return the URLContext associated with the AVIOContext.
Definition: aviobuf.c:983
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:88
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition: avio.h:128
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:158
Public header for CRC hash function implementation.
int64_t bytes_read
Bytes read statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:279
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
int ffio_set_buf_size(AVIOContext *s, int buf_size)
Definition: aviobuf.c:1037
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:696
ptrdiff_t size
Definition: opengl_enc.c:100
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:477
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:459
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:1146
unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:596
#define av_log(a,...)
void avio_print_string_array(AVIOContext *s, const char *strings[])
Write a NULL terminated array of strings to the context.
Definition: aviobuf.c:1222
static void flush_buffer(AVIOContext *s)
Definition: aviobuf.c:187
int max_packet_size
Definition: avio.h:241
Callback for checking whether to abort blocking functions.
Definition: avio.h:58
int64_t written
Definition: avio.h:340
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int io_buffer_size
Definition: aviobuf.c:1294
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:778
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:490
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:1330
const char * protocol_whitelist
Definition: url.h:49
static const AVOption ff_avio_options[]
Definition: aviobuf.c:68
int ffio_realloc_buf(AVIOContext *s, int buf_size)
Reallocate a given buffer for AVIOContext.
Definition: aviobuf.c:1053
int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp)
Read a whole line of text from AVIOContext to an AVBPrint buffer overwriting its contents.
Definition: aviobuf.c:849
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
unsigned int pos
Definition: spdifenc.c:410
simple assert() macros that are a bit more flexible than ISO C assert().
unsigned long checksum
Definition: avio.h:242
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
GLsizei count
Definition: opengl_enc.c:108
#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:273
#define fail()
Definition: checkasm.h:123
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:738
static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
Definition: aviobuf.c:519
int seek_count
seek statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:285
int avio_pause(AVIOContext *s, int pause)
Pause and resume playing - only meaningful if using a network streaming protocol (e.g.
Definition: aviobuf.c:1228
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:754
This is any, unlabelled data.
Definition: avio.h:135
int size
Definition: aviobuf.c:1292
int(* read_packet)(void *opaque, uint8_t *buf, int buf_size)
Definition: avio.h:235
#define b
Definition: input.c:41
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:686
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int ffurl_get_short_seek(URLContext *h)
Return the current short seek threshold value for this URL.
Definition: avio.c:652
#define FFMIN(a, b)
Definition: common.h:96
const AVClass ff_avio_class
Definition: aviobuf.c:73
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1428
unsigned char * checksum_ptr
Definition: avio.h:243
int avio_printf(AVIOContext *s, const char *fmt,...)
Definition: aviobuf.c:1203
AVIODataMarkerType
Different data types that can be returned via the AVIO write_data_type callback.
Definition: avio.h:111
int64_t(* seek)(void *opaque, int64_t offset, int whence)
Definition: avio.h:237
int allocated_size
Definition: aviobuf.c:1292
int ffurl_handshake(URLContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition: avio.c:238
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:392
void avio_context_free(AVIOContext **ps)
Free the supplied IO context and everything associated with it.
Definition: aviobuf.c:155
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:1298
#define s(width, name)
Definition: cbs_vp9.c:257
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:793
#define GET_STR16(type, read)
Definition: aviobuf.c:882
static volatile int checksum
Definition: adler32.c:30
int ffurl_accept(URLContext *s, URLContext **c)
Accept an URLContext c on an URLContext s.
Definition: avio.c:230
int buffer_size
Maximum buffer size.
Definition: avio.h:227
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:453
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
if(ret)
int(* short_seek_get)(void *opaque)
A callback that is used instead of short_seek_threshold.
Definition: avio.h:338
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:40
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 &#39;max_packet_size&#39;.
Definition: aviobuf.c:1388
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:161
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
const char * protocol_blacklist
Definition: url.h:50
#define PUT_STR16(type, big_endian)
Definition: aviobuf.c:436
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:1141
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it be(in the first position) for now.Options-------Then comes the options array.This is what will define the user accessible options.For example
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:1098
static int url_resetbuf(AVIOContext *s, int flags)
Definition: aviobuf.c:1084
int(* url_read_pause)(URLContext *h, int pause)
Definition: url.h:82
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:762
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1173
int(* write_packet)(void *opaque, uint8_t *buf, int buf_size)
Definition: avio.h:236
Definition: url.h:38
void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg)
Append a formatted string to a print buffer.
Definition: bprint.c:117
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:225
#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:539
#define AVIO_FLAG_DIRECT
Use direct mode.
Definition: avio.h:701
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:770
int pos
Definition: aviobuf.c:1292
int short_seek_threshold
Threshold to favor readahead over seek.
Definition: avio.h:304
#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:513
int ignore_boundary_point
If set, don&#39;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:326
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1458
int error
contains the error code or 0 if no error happened
Definition: avio.h:245
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:1002
#define flags(name, subs,...)
Definition: cbs_av1.c:560
int ffurl_close(URLContext *h)
Definition: avio.c:469
const AVClass ffurl_context_class
Definition: avio.c:64
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:227
int orig_buffer_size
Original buffer size used internally after probing and ensure seekback to reset the buffer size This ...
Definition: avio.h:298
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
enum AVIODataMarkerType current_type
Internal, not meant to be used from outside of AVIOContext.
Definition: avio.h:331
int avio_handshake(AVIOContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition: aviobuf.c:1283
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:1167
int(* read_pause)(void *opaque, int pause)
Pause or resume playback for network streaming protocols - e.g.
Definition: avio.h:249
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:346
Main libavformat public API header.
int
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1610
const OptionDef options[]
Definition: ffmpeg_opt.c:3400
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:864
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:436
const struct URLProtocol * prot
Definition: url.h:40
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:319
unsigned long(* update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size)
Definition: avio.h:244
int ffio_fdopen(AVIOContext **s, URLContext *h)
Create and initialize a AVIOContext for accessing the resource referenced by the URLContext h...
Definition: aviobuf.c:925
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:616
int64_t pos
position in the file of the current buffer
Definition: avio.h:238
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:531
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
uint64_t ffio_read_varlen(AVIOContext *bc)
Definition: aviobuf.c:914
int64_t ff_read_line_to_bprint(AVIOContext *s, AVBPrint *bp)
Read a whole line of text from AVIOContext to an AVBPrint buffer.
Definition: aviobuf.c:818
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:785
unsigned char * buf_ptr_max
Maximum reached position before a backward seek in the write buffer, used keeping track of already wr...
Definition: avio.h:346
A point in the output bytestream where a decoder can start decoding (i.e.
Definition: avio.h:122
#define av_free(p)
uint8_t * buffer
Definition: aviobuf.c:1293
static int put_str16(AVIOContext *s, const char *str, const int be)
Definition: aviobuf.c:406
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:239
int len
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1491
static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:1471
static void writeout(AVIOContext *s, const uint8_t *data, int len)
Definition: aviobuf.c:160
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:708
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
Definition: aviobuf.c:1361
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:341
#define av_freep(p)
unbuffered private I/O API
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
Same as ff_get_line but strip the white-space characters in the text tail.
Definition: aviobuf.c:810
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:590
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
int min_packet_size
if non zero, the stream is packetized with this min packet size
Definition: url.h:51
static double val(void *priv, double ch)
Definition: aeval.c:76
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:409
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:255
int i
Definition: input.c:407
Header data; this needs to be present for the stream to be decodeable.
Definition: avio.h:115
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
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:1235
unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:602
GLuint buffer
Definition: opengl_enc.c:101
int64_t(* url_read_seek)(URLContext *h, int stream_index, int64_t timestamp, int flags)
Definition: url.h:83
static uint8_t tmp[11]
Definition: aes_ctr.c:26