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