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