FFmpeg
aviobuf.c
Go to the documentation of this file.
1 /*
2  * buffered I/O
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/bprint.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/log.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/avassert.h"
30 #include "libavcodec/defs.h"
31 #include "avio.h"
32 #include "avio_internal.h"
33 #include "internal.h"
34 #include "url.h"
35 #include <stdarg.h>
36 
37 #define IO_BUFFER_SIZE 32768
38 
39 /**
40  * Do seeks within this distance ahead of the current buffer by skipping
41  * data instead of calling the protocol seek function, for seekable
42  * protocols.
43  */
44 #define SHORT_SEEK_THRESHOLD 32768
45 
46 static void *ff_avio_child_next(void *obj, void *prev)
47 {
48  AVIOContext *s = obj;
49  return prev ? NULL : s->opaque;
50 }
51 
52 static const AVClass *child_class_iterate(void **iter)
53 {
54  const AVClass *c = *iter ? NULL : &ffurl_context_class;
55  *iter = (void*)(uintptr_t)c;
56  return c;
57 }
58 
59 #define OFFSET(x) offsetof(AVIOContext,x)
60 #define E AV_OPT_FLAG_ENCODING_PARAM
61 #define D AV_OPT_FLAG_DECODING_PARAM
62 static const AVOption ff_avio_options[] = {
63  {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
64  { NULL },
65 };
66 
68  .class_name = "AVIOContext",
69  .item_name = av_default_item_name,
70  .version = LIBAVUTIL_VERSION_INT,
71  .option = ff_avio_options,
72  .child_next = ff_avio_child_next,
73  .child_class_iterate = child_class_iterate,
74 };
75 
76 static void fill_buffer(AVIOContext *s);
77 static int url_resetbuf(AVIOContext *s, int flags);
78 /** @warning must be called before any I/O */
79 static int set_buf_size(AVIOContext *s, int buf_size);
80 
82  unsigned char *buffer,
83  int buffer_size,
84  int write_flag,
85  void *opaque,
86  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
88  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
89 #else
90  int (*write_packet)(void *opaque, const uint8_t *buf, int buf_size),
91 #endif
92  int64_t (*seek)(void *opaque, int64_t offset, int whence))
93 {
94  AVIOContext *const s = &ctx->pub;
95 
96  memset(ctx, 0, sizeof(*ctx));
97 
98  s->buffer = buffer;
99  ctx->orig_buffer_size =
100  s->buffer_size = buffer_size;
101  s->buf_ptr = buffer;
102  s->buf_ptr_max = buffer;
103  s->opaque = opaque;
104  s->direct = 0;
105 
106  url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
107 
108  s->write_packet = write_packet;
109  s->read_packet = read_packet;
110  s->seek = seek;
111  s->pos = 0;
112  s->eof_reached = 0;
113  s->error = 0;
114  s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
115  s->min_packet_size = 0;
116  s->max_packet_size = 0;
117  s->update_checksum = NULL;
118  ctx->short_seek_threshold = SHORT_SEEK_THRESHOLD;
119 
120  if (!read_packet && !write_flag) {
121  s->pos = buffer_size;
122  s->buf_end = s->buffer + buffer_size;
123  }
124  s->read_pause = NULL;
125  s->read_seek = NULL;
126 
127  s->write_data_type = NULL;
128  s->ignore_boundary_point = 0;
129  ctx->current_type = AVIO_DATA_MARKER_UNKNOWN;
130  ctx->last_time = AV_NOPTS_VALUE;
131  ctx->short_seek_get = NULL;
132 }
133 
134 void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
135 {
136  ffio_init_context(s, (unsigned char*)buffer, buffer_size, 0, NULL, NULL, NULL, NULL);
137 }
138 
139 void ffio_init_write_context(FFIOContext *s, uint8_t *buffer, int buffer_size)
140 {
141  ffio_init_context(s, buffer, buffer_size, 1, NULL, NULL, NULL, NULL);
142 }
143 
145  unsigned char *buffer,
146  int buffer_size,
147  int write_flag,
148  void *opaque,
149  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
151  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
152 #else
153  int (*write_packet)(void *opaque, const uint8_t *buf, int buf_size),
154 #endif
155  int64_t (*seek)(void *opaque, int64_t offset, int whence))
156 {
157  FFIOContext *s = av_malloc(sizeof(*s));
158  if (!s)
159  return NULL;
160  ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
162  return &s->pub;
163 }
164 
166 {
167  av_freep(ps);
168 }
169 
170 static void writeout(AVIOContext *s, const uint8_t *data, int len)
171 {
172  FFIOContext *const ctx = ffiocontext(s);
173  if (!s->error) {
174  int ret = 0;
175  if (s->write_data_type)
176 #if FF_API_AVIO_WRITE_NONCONST
177  ret = s->write_data_type(s->opaque, (uint8_t *)data,
178 #else
179  ret = s->write_data_type(s->opaque, data,
180 #endif
181  len,
182  ctx->current_type,
183  ctx->last_time);
184  else if (s->write_packet)
186  ret = s->write_packet(s->opaque, (uint8_t *)data, len);
187 #else
188  ret = s->write_packet(s->opaque, data, len);
189 #endif
190  if (ret < 0) {
191  s->error = ret;
192  } else {
193  ctx->bytes_written += len;
194  s->bytes_written = ctx->bytes_written;
195 
196  if (s->pos + len > ctx->written_output_size) {
197  ctx->written_output_size = s->pos + len;
198  }
199  }
200  }
201  if (ctx->current_type == AVIO_DATA_MARKER_SYNC_POINT ||
202  ctx->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
203  ctx->current_type = AVIO_DATA_MARKER_UNKNOWN;
204  }
205  ctx->last_time = AV_NOPTS_VALUE;
206  ctx->writeout_count++;
207  s->pos += len;
208 }
209 
211 {
212  s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);
213  if (s->write_flag && s->buf_ptr_max > s->buffer) {
214  writeout(s, s->buffer, s->buf_ptr_max - s->buffer);
215  if (s->update_checksum) {
216  s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
217  s->buf_ptr_max - s->checksum_ptr);
218  s->checksum_ptr = s->buffer;
219  }
220  }
221  s->buf_ptr = s->buf_ptr_max = s->buffer;
222  if (!s->write_flag)
223  s->buf_end = s->buffer;
224 }
225 
226 void avio_w8(AVIOContext *s, int b)
227 {
228  av_assert2(b>=-128 && b<=255);
229  *s->buf_ptr++ = b;
230  if (s->buf_ptr >= s->buf_end)
231  flush_buffer(s);
232 }
233 
234 void ffio_fill(AVIOContext *s, int b, int64_t count)
235 {
236  while (count > 0) {
237  int len = FFMIN(s->buf_end - s->buf_ptr, count);
238  memset(s->buf_ptr, b, len);
239  s->buf_ptr += len;
240 
241  if (s->buf_ptr >= s->buf_end)
242  flush_buffer(s);
243 
244  count -= len;
245  }
246 }
247 
248 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
249 {
250  if (size <= 0)
251  return;
252  if (s->direct && !s->update_checksum) {
253  avio_flush(s);
254  writeout(s, buf, size);
255  return;
256  }
257  do {
258  int len = FFMIN(s->buf_end - s->buf_ptr, size);
259  memcpy(s->buf_ptr, buf, len);
260  s->buf_ptr += len;
261 
262  if (s->buf_ptr >= s->buf_end)
263  flush_buffer(s);
264 
265  buf += len;
266  size -= len;
267  } while (size > 0);
268 }
269 
271 {
272  int seekback = s->write_flag ? FFMIN(0, s->buf_ptr - s->buf_ptr_max) : 0;
273  flush_buffer(s);
274  if (seekback)
275  avio_seek(s, seekback, SEEK_CUR);
276 }
277 
278 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
279 {
280  FFIOContext *const ctx = ffiocontext(s);
281  int64_t offset1;
282  int64_t pos;
283  int force = whence & AVSEEK_FORCE;
284  int buffer_size;
285  int short_seek;
286  whence &= ~AVSEEK_FORCE;
287 
288  if(!s)
289  return AVERROR(EINVAL);
290 
291  if ((whence & AVSEEK_SIZE))
292  return s->seek ? s->seek(s->opaque, offset, AVSEEK_SIZE) : AVERROR(ENOSYS);
293 
294  buffer_size = s->buf_end - s->buffer;
295  // pos is the absolute position that the beginning of s->buffer corresponds to in the file
296  pos = s->pos - (s->write_flag ? 0 : buffer_size);
297 
298  if (whence != SEEK_CUR && whence != SEEK_SET)
299  return AVERROR(EINVAL);
300 
301  if (whence == SEEK_CUR) {
302  offset1 = pos + (s->buf_ptr - s->buffer);
303  if (offset == 0)
304  return offset1;
305  if (offset > INT64_MAX - offset1)
306  return AVERROR(EINVAL);
307  offset += offset1;
308  }
309  if (offset < 0)
310  return AVERROR(EINVAL);
311 
312  short_seek = ctx->short_seek_threshold;
313  if (ctx->short_seek_get) {
314  int tmp = ctx->short_seek_get(s->opaque);
315  short_seek = FFMAX(tmp, short_seek);
316  }
317 
318  offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
319  s->buf_ptr_max = FFMAX(s->buf_ptr_max, s->buf_ptr);
320  if ((!s->direct || !s->seek) &&
321  offset1 >= 0 && offset1 <= (s->write_flag ? s->buf_ptr_max - s->buffer : buffer_size)) {
322  /* can do the seek inside the buffer */
323  s->buf_ptr = s->buffer + offset1;
324  } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
325  offset1 <= buffer_size + short_seek) &&
326  !s->write_flag && offset1 >= 0 &&
327  (!s->direct || !s->seek) &&
328  (whence != SEEK_END || force)) {
329  while(s->pos < offset && !s->eof_reached)
330  fill_buffer(s);
331  if (s->eof_reached)
332  return AVERROR_EOF;
333  s->buf_ptr = s->buf_end - (s->pos - offset);
334  } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
335  int64_t res;
336 
337  pos -= FFMIN(buffer_size>>1, pos);
338  if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
339  return res;
340  s->buf_end =
341  s->buf_ptr = s->buffer;
342  s->pos = pos;
343  s->eof_reached = 0;
344  fill_buffer(s);
345  return avio_seek(s, offset, SEEK_SET | force);
346  } else {
347  int64_t res;
348  if (s->write_flag) {
349  flush_buffer(s);
350  }
351  if (!s->seek)
352  return AVERROR(EPIPE);
353  if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
354  return res;
355  ctx->seek_count++;
356  if (!s->write_flag)
357  s->buf_end = s->buffer;
358  s->buf_ptr = s->buf_ptr_max = s->buffer;
359  s->pos = offset;
360  }
361  s->eof_reached = 0;
362  return offset;
363 }
364 
365 int64_t avio_skip(AVIOContext *s, int64_t offset)
366 {
367  return avio_seek(s, offset, SEEK_CUR);
368 }
369 
371 {
372  FFIOContext *const ctx = ffiocontext(s);
373  int64_t size;
374 
375  if (!s)
376  return AVERROR(EINVAL);
377 
378  if (ctx->written_output_size)
379  return ctx->written_output_size;
380 
381  if (!s->seek)
382  return AVERROR(ENOSYS);
383  size = s->seek(s->opaque, 0, AVSEEK_SIZE);
384  if (size < 0) {
385  if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
386  return size;
387  size++;
388  s->seek(s->opaque, s->pos, SEEK_SET);
389  }
390  return size;
391 }
392 
394 {
395  if(!s)
396  return 0;
397  if(s->eof_reached){
398  s->eof_reached=0;
399  fill_buffer(s);
400  }
401  return s->eof_reached;
402 }
403 
404 void avio_wl32(AVIOContext *s, unsigned int val)
405 {
406  avio_w8(s, (uint8_t) val );
407  avio_w8(s, (uint8_t)(val >> 8 ));
408  avio_w8(s, (uint8_t)(val >> 16));
409  avio_w8(s, val >> 24 );
410 }
411 
412 void avio_wb32(AVIOContext *s, unsigned int val)
413 {
414  avio_w8(s, val >> 24 );
415  avio_w8(s, (uint8_t)(val >> 16));
416  avio_w8(s, (uint8_t)(val >> 8 ));
417  avio_w8(s, (uint8_t) val );
418 }
419 
420 int avio_put_str(AVIOContext *s, const char *str)
421 {
422  int len = 1;
423  if (str) {
424  len += strlen(str);
425  avio_write(s, (const unsigned char *) str, len);
426  } else
427  avio_w8(s, 0);
428  return len;
429 }
430 
431 static inline int put_str16(AVIOContext *s, const char *str, const int be)
432 {
433  const uint8_t *q = str;
434  int ret = 0;
435  int err = 0;
436 
437  while (*q) {
438  uint32_t ch;
439  uint16_t tmp;
440 
441  GET_UTF8(ch, *q++, goto invalid;)
442  PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
443  ret += 2;)
444  continue;
445 invalid:
446  av_log(s, AV_LOG_ERROR, "Invalid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
447  err = AVERROR(EINVAL);
448  if (!*(q-1))
449  break;
450  }
451  if (be)
452  avio_wb16(s, 0);
453  else
454  avio_wl16(s, 0);
455  if (err)
456  return err;
457  ret += 2;
458  return ret;
459 }
460 
461 #define PUT_STR16(type, big_endian) \
462 int avio_put_str16 ## type(AVIOContext *s, const char *str) \
463 { \
464 return put_str16(s, str, big_endian); \
465 }
466 
467 PUT_STR16(le, 0)
468 PUT_STR16(be, 1)
469 
470 #undef PUT_STR16
471 
472 void avio_wl64(AVIOContext *s, uint64_t val)
473 {
474  avio_wl32(s, (uint32_t)(val & 0xffffffff));
475  avio_wl32(s, (uint32_t)(val >> 32));
476 }
477 
478 void avio_wb64(AVIOContext *s, uint64_t val)
479 {
480  avio_wb32(s, (uint32_t)(val >> 32));
481  avio_wb32(s, (uint32_t)(val & 0xffffffff));
482 }
483 
484 void avio_wl16(AVIOContext *s, unsigned int val)
485 {
486  avio_w8(s, (uint8_t)val);
487  avio_w8(s, (int)val >> 8);
488 }
489 
490 void avio_wb16(AVIOContext *s, unsigned int val)
491 {
492  avio_w8(s, (int)val >> 8);
493  avio_w8(s, (uint8_t)val);
494 }
495 
496 void avio_wl24(AVIOContext *s, unsigned int val)
497 {
498  avio_wl16(s, val & 0xffff);
499  avio_w8(s, (int)val >> 16);
500 }
501 
502 void avio_wb24(AVIOContext *s, unsigned int val)
503 {
504  avio_wb16(s, (int)val >> 8);
505  avio_w8(s, (uint8_t)val);
506 }
507 
509 {
510  FFIOContext *const ctx = ffiocontext(s);
512  if (s->buf_ptr - s->buffer >= s->min_packet_size)
513  avio_flush(s);
514  return;
515  }
516  if (!s->write_data_type)
517  return;
518  // If ignoring boundary points, just treat it as unknown
519  if (type == AVIO_DATA_MARKER_BOUNDARY_POINT && s->ignore_boundary_point)
521  // Avoid unnecessary flushes if we are already in non-header/trailer
522  // data and setting the type to unknown
524  (ctx->current_type != AVIO_DATA_MARKER_HEADER &&
525  ctx->current_type != AVIO_DATA_MARKER_TRAILER))
526  return;
527 
528  switch (type) {
531  // For header/trailer, ignore a new marker of the same type;
532  // consecutive header/trailer markers can be merged.
533  if (type == ctx->current_type)
534  return;
535  break;
536  }
537 
538  // If we've reached here, we have a new, noteworthy marker.
539  // Flush the previous data and mark the start of the new data.
540  avio_flush(s);
541  ctx->current_type = type;
542  ctx->last_time = time;
543 }
544 
545 static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
546 {
547  int ret;
548 
549  if (!s->read_packet)
550  return AVERROR(EINVAL);
551  ret = s->read_packet(s->opaque, buf, size);
552  av_assert2(ret || s->max_packet_size);
553  return ret;
554 }
555 
556 /* Input stream */
557 
558 static void fill_buffer(AVIOContext *s)
559 {
560  FFIOContext *const ctx = (FFIOContext *)s;
561  int max_buffer_size = s->max_packet_size ?
562  s->max_packet_size : IO_BUFFER_SIZE;
563  uint8_t *dst = s->buf_end - s->buffer + max_buffer_size <= s->buffer_size ?
564  s->buf_end : s->buffer;
565  int len = s->buffer_size - (dst - s->buffer);
566 
567  /* can't fill the buffer without read_packet, just set EOF if appropriate */
568  if (!s->read_packet && s->buf_ptr >= s->buf_end)
569  s->eof_reached = 1;
570 
571  /* no need to do anything if EOF already reached */
572  if (s->eof_reached)
573  return;
574 
575  if (s->update_checksum && dst == s->buffer) {
576  if (s->buf_end > s->checksum_ptr)
577  s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
578  s->buf_end - s->checksum_ptr);
579  s->checksum_ptr = s->buffer;
580  }
581 
582  /* make buffer smaller in case it ended up large after probing */
583  if (s->read_packet && ctx->orig_buffer_size &&
584  s->buffer_size > ctx->orig_buffer_size && len >= ctx->orig_buffer_size) {
585  if (dst == s->buffer && s->buf_ptr != dst) {
586  int ret = set_buf_size(s, ctx->orig_buffer_size);
587  if (ret < 0)
588  av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
589 
590  s->checksum_ptr = dst = s->buffer;
591  }
592  len = ctx->orig_buffer_size;
593  }
594 
595  len = read_packet_wrapper(s, dst, len);
596  if (len == AVERROR_EOF) {
597  /* do not modify buffer if EOF reached so that a seek back can
598  be done without rereading data */
599  s->eof_reached = 1;
600  } else if (len < 0) {
601  s->eof_reached = 1;
602  s->error= len;
603  } else {
604  s->pos += len;
605  s->buf_ptr = dst;
606  s->buf_end = dst + len;
608  s->bytes_read = ffiocontext(s)->bytes_read;
609  }
610 }
611 
612 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
613  unsigned int len)
614 {
615  return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
616 }
617 
618 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
619  unsigned int len)
620 {
621  return av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), checksum, buf, len);
622 }
623 
624 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
625  unsigned int len)
626 {
627  return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
628 }
629 
631 {
632  s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
633  s->buf_ptr - s->checksum_ptr);
634  s->update_checksum = NULL;
635  return s->checksum;
636 }
637 
639  unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
640  unsigned long checksum)
641 {
642  s->update_checksum = update_checksum;
643  if (s->update_checksum) {
644  s->checksum = checksum;
645  s->checksum_ptr = s->buf_ptr;
646  }
647 }
648 
649 /* XXX: put an inline version */
651 {
652  if (s->buf_ptr >= s->buf_end)
653  fill_buffer(s);
654  if (s->buf_ptr < s->buf_end)
655  return *s->buf_ptr++;
656  return 0;
657 }
658 
659 int avio_read(AVIOContext *s, unsigned char *buf, int size)
660 {
661  int len, size1;
662 
663  size1 = size;
664  while (size > 0) {
665  len = FFMIN(s->buf_end - s->buf_ptr, size);
666  if (len == 0 || s->write_flag) {
667  if((s->direct || size > s->buffer_size) && !s->update_checksum && s->read_packet) {
668  // bypass the buffer and read data directly into buf
669  len = read_packet_wrapper(s, buf, size);
670  if (len == AVERROR_EOF) {
671  /* do not modify buffer if EOF reached so that a seek back can
672  be done without rereading data */
673  s->eof_reached = 1;
674  break;
675  } else if (len < 0) {
676  s->eof_reached = 1;
677  s->error= len;
678  break;
679  } else {
680  s->pos += len;
682  s->bytes_read = ffiocontext(s)->bytes_read;
683  size -= len;
684  buf += len;
685  // reset the buffer
686  s->buf_ptr = s->buffer;
687  s->buf_end = s->buffer/* + len*/;
688  }
689  } else {
690  fill_buffer(s);
691  len = s->buf_end - s->buf_ptr;
692  if (len == 0)
693  break;
694  }
695  } else {
696  memcpy(buf, s->buf_ptr, len);
697  buf += len;
698  s->buf_ptr += len;
699  size -= len;
700  }
701  }
702  if (size1 == size) {
703  if (s->error) return s->error;
704  if (avio_feof(s)) return AVERROR_EOF;
705  }
706  return size1 - size;
707 }
708 
709 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
710 {
711  int ret = avio_read(s, buf, size);
712  if (ret == size)
713  return ret;
714  if (ret < 0 && ret != AVERROR_EOF)
715  return ret;
716  return AVERROR_INVALIDDATA;
717 }
718 
719 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
720 {
721  if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
722  *data = s->buf_ptr;
723  s->buf_ptr += size;
724  return size;
725  } else {
726  *data = buf;
727  return avio_read(s, buf, size);
728  }
729 }
730 
731 int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
732 {
733  int len;
734 
735  if (size < 0)
736  return AVERROR(EINVAL);
737 
738  if (s->read_packet && s->write_flag) {
739  len = read_packet_wrapper(s, buf, size);
740  if (len > 0)
741  s->pos += len;
742  return len;
743  }
744 
745  len = s->buf_end - s->buf_ptr;
746  if (len == 0) {
747  fill_buffer(s);
748  len = s->buf_end - s->buf_ptr;
749  }
750  if (len > size)
751  len = size;
752  memcpy(buf, s->buf_ptr, len);
753  s->buf_ptr += len;
754  if (!len) {
755  if (s->error) return s->error;
756  if (avio_feof(s)) return AVERROR_EOF;
757  }
758  return len;
759 }
760 
761 unsigned int avio_rl16(AVIOContext *s)
762 {
763  unsigned int val;
764  val = avio_r8(s);
765  val |= avio_r8(s) << 8;
766  return val;
767 }
768 
769 unsigned int avio_rl24(AVIOContext *s)
770 {
771  unsigned int val;
772  val = avio_rl16(s);
773  val |= avio_r8(s) << 16;
774  return val;
775 }
776 
777 unsigned int avio_rl32(AVIOContext *s)
778 {
779  unsigned int val;
780  val = avio_rl16(s);
781  val |= avio_rl16(s) << 16;
782  return val;
783 }
784 
786 {
787  uint64_t val;
788  val = (uint64_t)avio_rl32(s);
789  val |= (uint64_t)avio_rl32(s) << 32;
790  return val;
791 }
792 
793 unsigned int avio_rb16(AVIOContext *s)
794 {
795  unsigned int val;
796  val = avio_r8(s) << 8;
797  val |= avio_r8(s);
798  return val;
799 }
800 
801 unsigned int avio_rb24(AVIOContext *s)
802 {
803  unsigned int val;
804  val = avio_rb16(s) << 8;
805  val |= avio_r8(s);
806  return val;
807 }
808 unsigned int avio_rb32(AVIOContext *s)
809 {
810  unsigned int val;
811  val = avio_rb16(s) << 16;
812  val |= avio_rb16(s);
813  return val;
814 }
815 
816 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
817 {
818  int i = 0;
819  char c;
820 
821  do {
822  c = avio_r8(s);
823  if (c && i < maxlen-1)
824  buf[i++] = c;
825  } while (c != '\n' && c != '\r' && c);
826  if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
827  avio_skip(s, -1);
828 
829  buf[i] = 0;
830  return i;
831 }
832 
833 int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
834 {
835  int len = ff_get_line(s, buf, maxlen);
836  while (len > 0 && av_isspace(buf[len - 1]))
837  buf[--len] = '\0';
838  return len;
839 }
840 
845 
846 static int64_t read_string_to_bprint(AVIOContext *s, AVBPrint *bp,
848  int64_t max_len)
849 {
850  int len, end;
851  int64_t read = 0;
852  char tmp[1024];
853  char c;
854 
855  if (!max_len)
856  return 0;
857 
858  do {
859  len = 0;
860  do {
861  c = avio_r8(s);
862  end = ((mode == FFBPrintReadLine && (c == '\r' || c == '\n')) ||
863  c == '\0');
864  if (!end)
865  tmp[len++] = c;
866  } while (!end && len < sizeof(tmp) &&
867  ((max_len < 0) || (read + len < max_len)));
869  read += len;
870  } while (!end && ((max_len < 0) || (read < max_len)));
871 
872  if (mode == FFBPrintReadLine &&
873  c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
874  avio_skip(s, -1);
875 
876  if (!c && s->error)
877  return s->error;
878 
879  if (!c && !read && avio_feof(s))
880  return AVERROR_EOF;
881 
882  return read;
883 }
884 
885 static int64_t read_string_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp,
887  int64_t max_len)
888 {
889  int64_t ret;
890 
891  av_bprint_clear(bp);
892  ret = read_string_to_bprint(s, bp, mode, max_len);
893  if (ret < 0)
894  return ret;
895 
896  if (!av_bprint_is_complete(bp))
897  return AVERROR(ENOMEM);
898 
899  return bp->len;
900 }
901 
903 {
905 }
906 
908  int64_t max_len)
909 {
911 }
912 
913 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
914 {
915  int i;
916 
917  if (buflen <= 0)
918  return AVERROR(EINVAL);
919  // reserve 1 byte for terminating 0
920  buflen = FFMIN(buflen - 1, maxlen);
921  for (i = 0; i < buflen; i++)
922  if (!(buf[i] = avio_r8(s)))
923  return i + 1;
924  buf[i] = 0;
925  for (; i < maxlen; i++)
926  if (!avio_r8(s))
927  return i + 1;
928  return maxlen;
929 }
930 
931 #define GET_STR16(type, read) \
932  int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
933 {\
934  char* q = buf;\
935  int ret = 0;\
936  if (buflen <= 0) \
937  return AVERROR(EINVAL); \
938  while (ret + 1 < maxlen) {\
939  uint8_t tmp;\
940  uint32_t ch;\
941  GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
942  if (!ch)\
943  break;\
944  PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
945  }\
946  *q = 0;\
947  return ret;\
948 }\
949 
950 GET_STR16(le, avio_rl16)
952 
953 #undef GET_STR16
954 
956 {
957  uint64_t val;
958  val = (uint64_t)avio_rb32(s) << 32;
959  val |= (uint64_t)avio_rb32(s);
960  return val;
961 }
962 
964  uint64_t val = 0;
965  int tmp;
966 
967  do{
968  tmp = avio_r8(bc);
969  val= (val<<7) + (tmp&127);
970  }while(tmp&128);
971  return val;
972 }
973 
974 unsigned int ffio_read_leb(AVIOContext *s) {
975  int more, i = 0;
976  unsigned leb = 0;
977 
978  do {
979  int byte = avio_r8(s);
980  unsigned bits = byte & 0x7f;
981  more = byte & 0x80;
982  if (i <= 4)
983  leb |= bits << (i * 7);
984  if (++i == 8)
985  break;
986  } while (more);
987 
988  return leb;
989 }
990 
991 void ffio_write_leb(AVIOContext *s, unsigned val)
992 {
993  int len;
994  uint8_t byte;
995 
996  len = (av_log2(val) + 7) / 7;
997 
998  for (int i = 0; i < len; i++) {
999  byte = val >> (7 * i) & 0x7f;
1000  if (i < len - 1)
1001  byte |= 0x80;
1002 
1003  avio_w8(s, byte);
1004  }
1005 }
1006 
1007 void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size,
1008  const unsigned char *ending)
1009 {
1010  int ending_len = ending ? strlen(ending) : 1;
1011  if (!ending)
1012  ending = "\n";
1013  if (size < 0)
1014  size = strlen(buf);
1015 
1016  while (size > 0) {
1017  size_t len = 0;
1018  char last = 0;
1019  for (; len < size; len++) {
1020  last = buf[len];
1021  if (last == '\r' || last == '\n')
1022  break;
1023  }
1024 
1025  avio_write(s, buf, len);
1026  avio_write(s, ending, ending_len);
1027 
1028  buf += len + 1;
1029  size -= len + 1;
1030 
1031  if (size > 0 && last == '\r' && buf[0] == '\n') {
1032  buf++;
1033  size--;
1034  }
1035  }
1036 }
1037 
1039 {
1040  uint8_t *buffer = NULL;
1041  int buffer_size, max_packet_size;
1042 
1043  max_packet_size = h->max_packet_size;
1044  if (max_packet_size) {
1045  buffer_size = max_packet_size; /* no need to bufferize more than one packet */
1046  } else {
1047  buffer_size = IO_BUFFER_SIZE;
1048  }
1049  if (!(h->flags & AVIO_FLAG_WRITE) && h->is_streamed) {
1050  if (buffer_size > INT_MAX/2)
1051  return AVERROR(EINVAL);
1052  buffer_size *= 2;
1053  }
1054  buffer = av_malloc(buffer_size);
1055  if (!buffer)
1056  return AVERROR(ENOMEM);
1057 
1058  *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
1060  if (!*s) {
1061  av_freep(&buffer);
1062  return AVERROR(ENOMEM);
1063  }
1064  (*s)->protocol_whitelist = av_strdup(h->protocol_whitelist);
1065  if (!(*s)->protocol_whitelist && h->protocol_whitelist) {
1066  avio_closep(s);
1067  return AVERROR(ENOMEM);
1068  }
1069  (*s)->protocol_blacklist = av_strdup(h->protocol_blacklist);
1070  if (!(*s)->protocol_blacklist && h->protocol_blacklist) {
1071  avio_closep(s);
1072  return AVERROR(ENOMEM);
1073  }
1074  (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
1075 
1076  (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
1077  (*s)->max_packet_size = max_packet_size;
1078  (*s)->min_packet_size = h->min_packet_size;
1079  if(h->prot) {
1080  (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
1081  (*s)->read_seek =
1082  (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
1083 
1084  if (h->prot->url_read_seek)
1085  (*s)->seekable |= AVIO_SEEKABLE_TIME;
1086  }
1087  ((FFIOContext*)(*s))->short_seek_get = ffurl_get_short_seek;
1088  (*s)->av_class = &ff_avio_class;
1089  return 0;
1090 }
1091 
1093 {
1094  if (!s)
1095  return NULL;
1096 
1097  if (s->opaque && s->read_packet == ffurl_read2)
1098  return s->opaque;
1099  else
1100  return NULL;
1101 }
1102 
1104 {
1105  const char *opts[] = {
1106  "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", NULL };
1107  const char **opt = opts;
1108  uint8_t *buf = NULL;
1109  int ret = 0;
1110 
1111  while (*opt) {
1112  if (av_opt_get(pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
1113  if (buf[0] != '\0') {
1114  ret = av_dict_set(avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
1115  if (ret < 0)
1116  return ret;
1117  } else {
1118  av_freep(&buf);
1119  }
1120  }
1121  opt++;
1122  }
1123 
1124  return ret;
1125 }
1126 
1128 {
1129  if (s->update_checksum && s->buf_ptr > s->checksum_ptr) {
1130  s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
1131  s->buf_ptr - s->checksum_ptr);
1132  }
1133 }
1134 
1135 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
1136 {
1137  uint8_t *buffer;
1138  int max_buffer_size = s->max_packet_size ?
1139  s->max_packet_size : IO_BUFFER_SIZE;
1140  ptrdiff_t filled = s->buf_end - s->buf_ptr;
1141 
1142  if (buf_size <= s->buf_end - s->buf_ptr)
1143  return 0;
1144 
1145  if (buf_size > INT_MAX - max_buffer_size)
1146  return AVERROR(EINVAL);
1147 
1148  buf_size += max_buffer_size - 1;
1149 
1150  if (buf_size + s->buf_ptr - s->buffer <= s->buffer_size || s->seekable || !s->read_packet)
1151  return 0;
1152  av_assert0(!s->write_flag);
1153 
1154  if (buf_size <= s->buffer_size) {
1155  update_checksum(s);
1156  memmove(s->buffer, s->buf_ptr, filled);
1157  } else {
1158  buffer = av_malloc(buf_size);
1159  if (!buffer)
1160  return AVERROR(ENOMEM);
1161  update_checksum(s);
1162  memcpy(buffer, s->buf_ptr, filled);
1163  av_free(s->buffer);
1164  s->buffer = buffer;
1165  s->buffer_size = buf_size;
1166  }
1167  s->buf_ptr = s->buffer;
1168  s->buf_end = s->buffer + filled;
1169  s->checksum_ptr = s->buffer;
1170  return 0;
1171 }
1172 
1174 {
1175  FFIOContext *const ctx = ffiocontext(s);
1176  if (ctx->maxsize >= 0) {
1177  int64_t pos = avio_tell(s);
1178  int64_t remaining = ctx->maxsize - pos;
1179  if (remaining < size) {
1180  int64_t newsize = avio_size(s);
1181  if (!ctx->maxsize || ctx->maxsize < newsize)
1182  ctx->maxsize = newsize - !newsize;
1183  if (pos > ctx->maxsize && ctx->maxsize >= 0)
1184  ctx->maxsize = AVERROR(EIO);
1185  if (ctx->maxsize >= 0)
1186  remaining = ctx->maxsize - pos;
1187  }
1188 
1189  if (ctx->maxsize >= 0 && remaining < size && size > 1) {
1190  av_log(NULL, remaining ? AV_LOG_ERROR : AV_LOG_DEBUG,
1191  "Truncating packet of size %d to %"PRId64"\n",
1192  size, remaining + !remaining);
1193  size = remaining + !remaining;
1194  }
1195  }
1196  return size;
1197 }
1198 
1199 static int set_buf_size(AVIOContext *s, int buf_size)
1200 {
1201  uint8_t *buffer;
1202  buffer = av_malloc(buf_size);
1203  if (!buffer)
1204  return AVERROR(ENOMEM);
1205 
1206  av_free(s->buffer);
1207  s->buffer = buffer;
1209  s->buffer_size = buf_size;
1210  s->buf_ptr = s->buf_ptr_max = buffer;
1211  url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
1212  return 0;
1213 }
1214 
1215 int ffio_realloc_buf(AVIOContext *s, int buf_size)
1216 {
1217  uint8_t *buffer;
1218  int data_size;
1219 
1220  if (!s->buffer_size)
1221  return set_buf_size(s, buf_size);
1222 
1223  if (buf_size <= s->buffer_size)
1224  return 0;
1225 
1226  buffer = av_malloc(buf_size);
1227  if (!buffer)
1228  return AVERROR(ENOMEM);
1229 
1230  data_size = s->write_flag ? (s->buf_ptr - s->buffer) : (s->buf_end - s->buf_ptr);
1231  if (data_size > 0)
1232  memcpy(buffer, s->write_flag ? s->buffer : s->buf_ptr, data_size);
1233  av_free(s->buffer);
1234  s->buffer = buffer;
1235  ffiocontext(s)->orig_buffer_size = buf_size;
1236  s->buffer_size = buf_size;
1237  s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer;
1238  if (s->write_flag)
1239  s->buf_ptr_max = s->buffer + data_size;
1240 
1241  s->buf_end = s->write_flag ? (s->buffer + s->buffer_size) : (s->buf_ptr + data_size);
1242 
1243  return 0;
1244 }
1245 
1246 static int url_resetbuf(AVIOContext *s, int flags)
1247 {
1249 
1250  if (flags & AVIO_FLAG_WRITE) {
1251  s->buf_end = s->buffer + s->buffer_size;
1252  s->write_flag = 1;
1253  } else {
1254  s->buf_end = s->buffer;
1255  s->write_flag = 0;
1256  }
1257  return 0;
1258 }
1259 
1260 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
1261 {
1262  int64_t buffer_start;
1263  int buffer_size;
1264  int overlap, new_size, alloc_size;
1265  uint8_t *buf = *bufp;
1266 
1267  if (s->write_flag) {
1268  av_freep(bufp);
1269  return AVERROR(EINVAL);
1270  }
1271 
1272  buffer_size = s->buf_end - s->buffer;
1273 
1274  /* the buffers must touch or overlap */
1275  if ((buffer_start = s->pos - buffer_size) > buf_size) {
1276  av_freep(bufp);
1277  return AVERROR(EINVAL);
1278  }
1279 
1280  overlap = buf_size - buffer_start;
1281  new_size = buf_size + buffer_size - overlap;
1282 
1283  alloc_size = FFMAX(s->buffer_size, new_size);
1284  if (alloc_size > buf_size)
1285  if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
1286  return AVERROR(ENOMEM);
1287 
1288  if (new_size > buf_size) {
1289  memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
1290  buf_size = new_size;
1291  }
1292 
1293  av_free(s->buffer);
1294  s->buf_ptr = s->buffer = buf;
1295  s->buffer_size = alloc_size;
1296  s->pos = buf_size;
1297  s->buf_end = s->buf_ptr + buf_size;
1298  s->eof_reached = 0;
1299 
1300  return 0;
1301 }
1302 
1303 int avio_open(AVIOContext **s, const char *filename, int flags)
1304 {
1305  return avio_open2(s, filename, flags, NULL, NULL);
1306 }
1307 
1308 int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
1310  const char *whitelist, const char *blacklist
1311  )
1312 {
1313  URLContext *h;
1314  int err;
1315 
1316  *s = NULL;
1317 
1318  err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist, blacklist, NULL);
1319  if (err < 0)
1320  return err;
1321  err = ffio_fdopen(s, h);
1322  if (err < 0) {
1323  ffurl_close(h);
1324  return err;
1325  }
1326  return 0;
1327 }
1328 
1329 int avio_open2(AVIOContext **s, const char *filename, int flags,
1331 {
1332  return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
1333 }
1334 
1336 {
1337  FFIOContext *const ctx = ffiocontext(s);
1338  URLContext *h;
1339  int ret, error;
1340 
1341  if (!s)
1342  return 0;
1343 
1344  avio_flush(s);
1345  h = s->opaque;
1346  s->opaque = NULL;
1347 
1348  av_freep(&s->buffer);
1349  if (s->write_flag)
1351  "Statistics: %"PRId64" bytes written, %d seeks, %d writeouts\n",
1352  ctx->bytes_written, ctx->seek_count, ctx->writeout_count);
1353  else
1354  av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n",
1355  ctx->bytes_read, ctx->seek_count);
1356  av_opt_free(s);
1357 
1358  error = s->error;
1359  avio_context_free(&s);
1360 
1361  ret = ffurl_close(h);
1362  if (ret < 0)
1363  return ret;
1364 
1365  return error;
1366 }
1367 
1369 {
1370  int ret = avio_close(*s);
1371  *s = NULL;
1372  return ret;
1373 }
1374 
1375 int avio_vprintf(AVIOContext *s, const char *fmt, va_list ap)
1376 {
1377  AVBPrint bp;
1378 
1379  av_bprint_init(&bp, 0, INT_MAX);
1380  av_vbprintf(&bp, fmt, ap);
1381  if (!av_bprint_is_complete(&bp)) {
1382  av_bprint_finalize(&bp, NULL);
1383  s->error = AVERROR(ENOMEM);
1384  return AVERROR(ENOMEM);
1385  }
1386  avio_write(s, bp.str, bp.len);
1387  av_bprint_finalize(&bp, NULL);
1388  return bp.len;
1389 }
1390 
1391 int avio_printf(AVIOContext *s, const char *fmt, ...)
1392 {
1393  va_list ap;
1394  int ret;
1395 
1396  va_start(ap, fmt);
1397  ret = avio_vprintf(s, fmt, ap);
1398  va_end(ap);
1399 
1400  return ret;
1401 }
1402 
1403 void avio_print_string_array(AVIOContext *s, const char *strings[])
1404 {
1405  for(; *strings; strings++)
1406  avio_write(s, (const unsigned char *)*strings, strlen(*strings));
1407 }
1408 
1409 int avio_pause(AVIOContext *s, int pause)
1410 {
1411  if (!s->read_pause)
1412  return AVERROR(ENOSYS);
1413  return s->read_pause(s->opaque, pause);
1414 }
1415 
1416 int64_t avio_seek_time(AVIOContext *s, int stream_index,
1417  int64_t timestamp, int flags)
1418 {
1419  int64_t ret;
1420  if (!s->read_seek)
1421  return AVERROR(ENOSYS);
1422  ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1423  if (ret >= 0) {
1424  int64_t pos;
1425  s->buf_ptr = s->buf_end; // Flush buffer
1426  pos = s->seek(s->opaque, 0, SEEK_CUR);
1427  if (pos >= 0)
1428  s->pos = pos;
1429  else if (pos != AVERROR(ENOSYS))
1430  ret = pos;
1431  }
1432  return ret;
1433 }
1434 
1435 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1436 {
1437  int ret;
1438  char buf[1024];
1439  while (max_size) {
1440  ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1441  if (ret == AVERROR_EOF)
1442  return 0;
1443  if (ret <= 0)
1444  return ret;
1445  av_bprint_append_data(pb, buf, ret);
1446  if (!av_bprint_is_complete(pb))
1447  return AVERROR(ENOMEM);
1448  max_size -= ret;
1449  }
1450  return 0;
1451 }
1452 
1454 {
1455  int ret;
1456  URLContext *sc = s->opaque;
1457  URLContext *cc = NULL;
1458  ret = ffurl_accept(sc, &cc);
1459  if (ret < 0)
1460  return ret;
1461  return ffio_fdopen(c, cc);
1462 }
1463 
1465 {
1466  URLContext *cc = c->opaque;
1467  return ffurl_handshake(cc);
1468 }
1469 
1470 /* output in a dynamic buffer */
1471 
1472 typedef struct DynBuffer {
1474  uint8_t *buffer;
1476  uint8_t io_buffer[1];
1477 } DynBuffer;
1478 
1479 #if FF_API_AVIO_WRITE_NONCONST
1480 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1481 #else
1482 static int dyn_buf_write(void *opaque, const uint8_t *buf, int buf_size)
1483 #endif
1484 {
1485  DynBuffer *d = opaque;
1486  unsigned new_size;
1487 
1488  /* reallocate buffer if needed */
1489  new_size = (unsigned)d->pos + buf_size;
1490  if (new_size < d->pos || new_size > INT_MAX)
1491  return AVERROR(ERANGE);
1492  if (new_size > d->allocated_size) {
1493  unsigned new_allocated_size = d->allocated_size ? d->allocated_size
1494  : new_size;
1495  int err;
1496  while (new_size > new_allocated_size)
1497  new_allocated_size += new_allocated_size / 2 + 1;
1498 
1499  new_allocated_size = FFMIN(new_allocated_size, INT_MAX);
1500 
1501  if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1502  d->allocated_size = 0;
1503  d->size = 0;
1504  return err;
1505  }
1506  d->allocated_size = new_allocated_size;
1507  }
1508  memcpy(d->buffer + d->pos, buf, buf_size);
1509  d->pos = new_size;
1510  if (d->pos > d->size)
1511  d->size = d->pos;
1512  return buf_size;
1513 }
1514 
1515 #if FF_API_AVIO_WRITE_NONCONST
1516 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1517 #else
1518 static int dyn_packet_buf_write(void *opaque, const uint8_t *buf, int buf_size)
1519 #endif
1520 {
1521  unsigned char buf1[4];
1522  int ret;
1523 
1524  /* packetized write: output the header */
1525  AV_WB32(buf1, buf_size);
1526  ret = dyn_buf_write(opaque, buf1, 4);
1527  if (ret < 0)
1528  return ret;
1529 
1530  /* then the data */
1531  return dyn_buf_write(opaque, buf, buf_size);
1532 }
1533 
1534 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1535 {
1536  DynBuffer *d = opaque;
1537 
1538  if (whence == SEEK_CUR)
1539  offset += d->pos;
1540  else if (whence == SEEK_END)
1541  offset += d->size;
1542  if (offset < 0)
1543  return AVERROR(EINVAL);
1544  if (offset > INT_MAX)
1545  return AVERROR(ERANGE);
1546  d->pos = offset;
1547  return 0;
1548 }
1549 
1550 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1551 {
1552  struct { FFIOContext pb; DynBuffer d; } *ret;
1553  DynBuffer *d;
1554  unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1555 
1556  if (sizeof(*ret) + io_buffer_size < io_buffer_size)
1557  return AVERROR(ERANGE);
1558  ret = av_mallocz(sizeof(*ret) + io_buffer_size);
1559  if (!ret)
1560  return AVERROR(ENOMEM);
1561  d = &ret->d;
1562  d->io_buffer_size = io_buffer_size;
1563  ffio_init_context(&ret->pb, d->io_buffer, d->io_buffer_size, 1, d, NULL,
1564  max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1565  max_packet_size ? NULL : dyn_buf_seek);
1566  *s = &ret->pb.pub;
1567  (*s)->max_packet_size = max_packet_size;
1568  return 0;
1569 }
1570 
1572 {
1573  return url_open_dyn_buf_internal(s, 0);
1574 }
1575 
1576 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1577 {
1578  if (max_packet_size <= 0)
1579  return AVERROR(EINVAL);
1580  return url_open_dyn_buf_internal(s, max_packet_size);
1581 }
1582 
1583 int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1584 {
1585  DynBuffer *d;
1586 
1587  if (!s) {
1588  *pbuffer = NULL;
1589  return 0;
1590  }
1591  d = s->opaque;
1592 
1593  if (!s->error && !d->size) {
1594  *pbuffer = d->io_buffer;
1595  return FFMAX(s->buf_ptr, s->buf_ptr_max) - s->buffer;
1596  }
1597 
1598  avio_flush(s);
1599 
1600  *pbuffer = d->buffer;
1601 
1602  return d->size;
1603 }
1604 
1606 {
1607  DynBuffer *d = s->opaque;
1608  int max_packet_size = s->max_packet_size;
1609 
1610  ffio_init_context(ffiocontext(s), d->io_buffer, d->io_buffer_size,
1611  1, d, NULL, s->write_packet, s->seek);
1612  s->max_packet_size = max_packet_size;
1613  d->pos = d->size = 0;
1614 }
1615 
1616 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1617 {
1618  DynBuffer *d;
1619  int size;
1620  int padding = 0;
1621 
1622  if (!s) {
1623  *pbuffer = NULL;
1624  return 0;
1625  }
1626 
1627  /* don't attempt to pad fixed-size packet buffers */
1628  if (!s->max_packet_size) {
1630  padding = AV_INPUT_BUFFER_PADDING_SIZE;
1631  }
1632 
1633  avio_flush(s);
1634 
1635  d = s->opaque;
1636  *pbuffer = d->buffer;
1637  size = d->size;
1638 
1639  avio_context_free(&s);
1640 
1641  return size - padding;
1642 }
1643 
1645 {
1646  DynBuffer *d;
1647 
1648  if (!*s)
1649  return;
1650 
1651  d = (*s)->opaque;
1652  av_free(d->buffer);
1654 }
1655 
1656 #if FF_API_AVIO_WRITE_NONCONST
1657 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1658 #else
1659 static int null_buf_write(void *opaque, const uint8_t *buf, int buf_size)
1660 #endif
1661 {
1662  DynBuffer *d = opaque;
1663 
1664  d->pos += buf_size;
1665  if (d->pos > d->size)
1666  d->size = d->pos;
1667  return buf_size;
1668 }
1669 
1671 {
1672  int ret = url_open_dyn_buf_internal(s, 0);
1673  if (ret >= 0) {
1674  AVIOContext *pb = *s;
1676  }
1677  return ret;
1678 }
1679 
1681 {
1682  DynBuffer *d = s->opaque;
1683  int size;
1684 
1685  avio_flush(s);
1686 
1687  size = d->size;
1688 
1689  avio_context_free(&s);
1690 
1691  return size;
1692 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
ffio_read_leb
unsigned int ffio_read_leb(AVIOContext *s)
Read a unsigned integer coded as a variable number of up to eight little-endian bytes,...
Definition: aviobuf.c:974
ff_get_chomp_line
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:833
be
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it be(in the first position) for now. Options ------- Then comes the options array. This is what will define the user accessible options. For example
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
put_str16
static int put_str16(AVIOContext *s, const char *str, const int be)
Definition: aviobuf.c:431
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:393
AVIO_DATA_MARKER_BOUNDARY_POINT
@ 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:133
ffurl_context_class
const AVClass ffurl_context_class
Definition: avio.c:63
D
#define D
Definition: aviobuf.c:61
av_bprint_is_complete
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:218
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
avio_get_dyn_buf
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1583
url_open_dyn_buf_internal
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
Definition: aviobuf.c:1550
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
ffiocontext
static av_always_inline FFIOContext * ffiocontext(AVIOContext *ctx)
Definition: avio_internal.h:81
avio_rl24
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:769
dyn_buf_seek
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
Definition: aviobuf.c:1534
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
avio_wl24
void avio_wl24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:496
av_isspace
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:218
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
ffio_get_checksum
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:630
DynBuffer::buffer
uint8_t * buffer
Definition: aviobuf.c:1474
ffio_open_null_buf
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1670
ffio_geturlcontext
URLContext * ffio_geturlcontext(AVIOContext *s)
Return the URLContext associated with the AVIOContext.
Definition: aviobuf.c:1092
AVOption
AVOption.
Definition: opt.h:251
b
#define b
Definition: input.c:41
AVSEEK_SIZE
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:487
IO_BUFFER_SIZE
#define IO_BUFFER_SIZE
Definition: aviobuf.c:37
data
const char data[16]
Definition: mxf.c:148
avio_seek_time
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:1416
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
avio_accept
int avio_accept(AVIOContext *s, AVIOContext **c)
Accept and allocate a client context on a server context.
Definition: aviobuf.c:1453
ffurl_close
int ffurl_close(URLContext *h)
Definition: avio.c:462
DynBuffer::io_buffer
uint8_t io_buffer[1]
Definition: aviobuf.c:1476
avio_put_str
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:420
AVDictionary
Definition: dict.c:34
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:404
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
avio_read_to_bprint
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:1435
ff_crcEDB88320_update
unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:618
FFBPrintReadString
@ FFBPrintReadString
Definition: aviobuf.c:842
AVIODataMarkerType
AVIODataMarkerType
Different data types that can be returned via the AVIO write_data_type callback.
Definition: avio.h:116
FFIOContext
Definition: avio_internal.h:28
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:226
update_checksum
static void update_checksum(AVIOContext *s)
Definition: aviobuf.c:1127
fill_buffer
static void fill_buffer(AVIOContext *s)
Definition: aviobuf.c:558
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVIOInterruptCB
Callback for checking whether to abort blocking functions.
Definition: avio.h:59
crc.h
ffio_reset_dyn_buf
void ffio_reset_dyn_buf(AVIOContext *s)
Reset a dynamic buffer.
Definition: aviobuf.c:1605
ffurl_get_short_seek
int ffurl_get_short_seek(void *urlcontext)
Return the current short seek threshold value for this URL.
Definition: avio.c:651
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:513
AV_CRC_16_ANSI_LE
@ AV_CRC_16_ANSI_LE
Definition: crc.h:54
val
static double val(void *priv, double ch)
Definition: aeval.c:78
avio_wb24
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:502
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:484
ffio_open_whitelist
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:1308
OFFSET
#define OFFSET(x)
Definition: aviobuf.c:59
ff_avio_child_next
static void * ff_avio_child_next(void *obj, void *prev)
Definition: aviobuf.c:46
avio_print_string_array
void avio_print_string_array(AVIOContext *s, const char *strings[])
Write a NULL terminated array of strings to the context.
Definition: aviobuf.c:1403
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
GET_UTF8
#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:472
flush_buffer
static void flush_buffer(AVIOContext *s)
Definition: aviobuf.c:210
avassert.h
ffio_copy_url_options
int ffio_copy_url_options(AVIOContext *pb, AVDictionary **avio_opts)
Read url related dictionary options from the AVIOContext and write to the given dictionary.
Definition: aviobuf.c:1103
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
avio_write_marker
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:508
avio_flush
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:270
ff_read_string_to_bprint_overwrite
int64_t ff_read_string_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp, int64_t max_len)
Read a whole null-terminated string of text from AVIOContext to an AVBPrint buffer overwriting its co...
Definition: aviobuf.c:907
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
ffurl_open_whitelist
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:300
FFIOContext::bytes_read
int64_t bytes_read
Bytes read statistic.
Definition: avio_internal.h:51
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:412
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:709
null_buf_write
static int null_buf_write(void *opaque, const uint8_t *buf, int buf_size)
Definition: aviobuf.c:1659
ff_avio_class
const AVClass ff_avio_class
Definition: aviobuf.c:67
avio_close_dyn_buf
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1616
ffio_write_lines
void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size, const unsigned char *ending)
Write a sequence of text lines, converting line endings.
Definition: aviobuf.c:1007
DynBuffer::pos
int pos
Definition: aviobuf.c:1473
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:234
ffio_read_indirect
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:719
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:637
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
dyn_packet_buf_write
static int dyn_packet_buf_write(void *opaque, const uint8_t *buf, int buf_size)
Definition: aviobuf.c:1518
ctx
AVFormatContext * ctx
Definition: movenc.c:48
avio_context_free
void avio_context_free(AVIOContext **ps)
Free the supplied IO context and everything associated with it.
Definition: aviobuf.c:165
if
if(ret)
Definition: filter_design.txt:179
ffurl_accept
int ffurl_accept(URLContext *s, URLContext **c)
Accept an URLContext c on an URLContext s.
Definition: avio.c:220
ffio_init_write_context
void ffio_init_write_context(FFIOContext *s, uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for writing.
Definition: aviobuf.c:139
av_realloc_f
#define av_realloc_f(p, o, n)
Definition: tableprint_vlc.h:32
internal.h
opts
AVDictionary * opts
Definition: movenc.c:50
ffio_limit
int ffio_limit(AVIOContext *s, int size)
Definition: aviobuf.c:1173
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:659
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_avio_options
static const AVOption ff_avio_options[]
Definition: aviobuf.c:62
NULL
#define NULL
Definition: coverity.c:32
DynBuffer
Definition: aviobuf.c:1472
AVIO_DATA_MARKER_TRAILER
@ AVIO_DATA_MARKER_TRAILER
Trailer data, which doesn't contain actual content, but only for finalizing the output file.
Definition: avio.h:145
PUT_UTF16
#define PUT_UTF16(val, tmp, PUT_16BIT)
Definition: common.h:559
FFIOContext::orig_buffer_size
int orig_buffer_size
Original buffer size used after probing to ensure seekback and to reset the buffer size.
Definition: avio_internal.h:72
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...)
Definition: aviobuf.c:1391
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
read_string_to_bprint
static int64_t read_string_to_bprint(AVIOContext *s, AVBPrint *bp, FFBPrintReadStringMode mode, int64_t max_len)
Definition: aviobuf.c:846
seek
static void BS_FUNC() seek(BSCTX *bc, unsigned pos)
Seek to the given bit position.
Definition: bitstream_template.h:399
av_opt_free
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1729
SHORT_SEEK_THRESHOLD
#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:44
avio_vprintf
int avio_vprintf(AVIOContext *s, const char *fmt, va_list ap)
Writes a formatted string to the context taking a va_list.
Definition: aviobuf.c:1375
read_packet_wrapper
static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
Definition: aviobuf.c:545
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
avio_pause
int avio_pause(AVIOContext *s, int pause)
Pause and resume playing - only meaningful if using a network streaming protocol (e....
Definition: aviobuf.c:1409
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:417
avio_rb24
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:801
options
const OptionDef options[]
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:793
ffio_realloc_buf
int ffio_realloc_buf(AVIOContext *s, int buf_size)
Reallocate a given buffer for AVIOContext.
Definition: aviobuf.c:1215
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
ff_read_line_to_bprint_overwrite
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:902
byte
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:99
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1644
size
int size
Definition: twinvq_data.h:10344
avio.h
ffio_ensure_seekback
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:1135
av_reallocp
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:186
avio_open
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:1303
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVIO_SEEKABLE_TIME
#define AVIO_SEEKABLE_TIME
Seeking by timestamp with avio_seek_time() is possible.
Definition: avio.h:46
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:761
avio_rl64
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:785
ffio_rewind_with_probe_data
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:1260
AVIO_DATA_MARKER_SYNC_POINT
@ AVIO_DATA_MARKER_SYNC_POINT
A point in the output bytestream where a decoder can start decoding (i.e.
Definition: avio.h:127
avio_close
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1335
AV_OPT_SEARCH_CHILDREN
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:563
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
avio_open2
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:1329
dyn_buf_write
static int dyn_buf_write(void *opaque, const uint8_t *buf, int buf_size)
Definition: aviobuf.c:1482
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:777
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:248
ffurl_write2
int ffurl_write2(void *urlcontext, const uint8_t *buf, int size)
Definition: avio.c:414
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:278
ffio_init_checksum
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:638
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
ffio_open_dyn_packet_buf
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:1576
bprint.h
URLContext
Definition: url.h:37
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
avio_alloc_context
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, const 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:144
avio_internal.h
PUT_STR16
#define PUT_STR16(type, big_endian)
Definition: aviobuf.c:461
FFBPrintReadLine
@ FFBPrintReadLine
Definition: aviobuf.c:843
internal.h
ffio_read_varlen
uint64_t ffio_read_varlen(AVIOContext *bc)
Definition: aviobuf.c:963
read_string_to_bprint_overwrite
static int64_t read_string_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp, FFBPrintReadStringMode mode, int64_t max_len)
Definition: aviobuf.c:885
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
url.h
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
ff_get_line
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:816
GET_STR16
#define GET_STR16(type, read)
Definition: aviobuf.c:931
len
int len
Definition: vorbis_enc_data.h:426
ffio_init_read_context
void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for reading.
Definition: aviobuf.c:134
int_cb
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:324
AV_CRC_32_IEEE
@ AV_CRC_32_IEEE
Definition: crc.h:52
DynBuffer::io_buffer_size
int io_buffer_size
Definition: aviobuf.c:1475
write_packet
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition: ffmpeg_mux.c:209
avio_wb64
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:478
AVIO_DATA_MARKER_UNKNOWN
@ AVIO_DATA_MARKER_UNKNOWN
This is any, unlabelled data.
Definition: avio.h:140
avio_get_str
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:913
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
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:71
ffurl_read2
int ffurl_read2(void *urlcontext, uint8_t *buf, int size)
Definition: avio.c:395
ffurl_seek2
int64_t ffurl_seek2(void *urlcontext, int64_t pos, int whence)
Definition: avio.c:428
pos
unsigned int pos
Definition: spdifenc.c:413
dict.h
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
ff_crc04C11DB7_update
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:612
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:650
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
av_crc
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
url_resetbuf
static int url_resetbuf(AVIOContext *s, int flags)
Definition: aviobuf.c:1246
av_vbprintf
void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg)
Append a formatted string to a print buffer.
Definition: bprint.c:122
mode
mode
Definition: ebur128.h:83
defs.h
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
AVIO_DATA_MARKER_HEADER
@ AVIO_DATA_MARKER_HEADER
Header data; this needs to be present for the stream to be decodeable.
Definition: avio.h:120
ff_crcA001_update
unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:624
DynBuffer::size
int size
Definition: aviobuf.c:1473
AVSEEK_FORCE
#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:495
AVIO_FLAG_DIRECT
#define AVIO_FLAG_DIRECT
Use direct mode.
Definition: avio.h:663
AV_CRC_32_IEEE_LE
@ AV_CRC_32_IEEE_LE
Definition: crc.h:53
AVIOContext::write_packet
int(* write_packet)(void *opaque, const uint8_t *buf, int buf_size)
Definition: avio.h:244
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:636
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:270
ffio_fdopen
int ffio_fdopen(AVIOContext **s, URLContext *h)
Create and initialize a AVIOContext for accessing the resource referenced by the URLContext h.
Definition: aviobuf.c:1038
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1571
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:490
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
ffurl_handshake
int ffurl_handshake(URLContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition: avio.c:228
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
DynBuffer::allocated_size
int allocated_size
Definition: aviobuf.c:1473
set_buf_size
static int set_buf_size(AVIOContext *s, int buf_size)
Definition: aviobuf.c:1199
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:808
d
d
Definition: ffmpeg_filter.c:424
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
ffio_init_context
void ffio_init_context(FFIOContext *ctx, 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, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:81
av_opt_get
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:847
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ffio_close_null_buf
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1680
h
h
Definition: vp9dsp_template.c:2038
FF_API_AVIO_WRITE_NONCONST
#define FF_API_AVIO_WRITE_NONCONST
Definition: version_major.h:48
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
avio_handshake
int avio_handshake(AVIOContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition: aviobuf.c:1464
writeout
static void writeout(AVIOContext *s, const uint8_t *data, int len)
Definition: aviobuf.c:170
child_class_iterate
static const AVClass * child_class_iterate(void **iter)
Definition: aviobuf.c:52
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:370
avio_read_partial
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:731
int
int
Definition: ffmpeg_filter.c:424
av_bprint_append_data
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:163
avio_wl64
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:472
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:365
ffio_write_leb
void ffio_write_leb(AVIOContext *s, unsigned val)
Definition: aviobuf.c:991
read
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
Definition: bitstream_template.h:231
avio_rb64
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:955
FFBPrintReadStringMode
FFBPrintReadStringMode
Definition: aviobuf.c:841
avio_closep
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:1368
AVIO_DATA_MARKER_FLUSH_POINT
@ AVIO_DATA_MARKER_FLUSH_POINT
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition: avio.h:151