FFmpeg
aviobuf.c
Go to the documentation of this file.
1 /*
2  * buffered I/O
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/bprint.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mem.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 <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 fill_buffer(AVIOContext *s);
46 static int url_resetbuf(AVIOContext *s, int flags);
47 /** @warning must be called before any I/O */
48 static int set_buf_size(AVIOContext *s, int buf_size);
49 
51  unsigned char *buffer,
52  int buffer_size,
53  int write_flag,
54  void *opaque,
55  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
56  int (*write_packet)(void *opaque, const uint8_t *buf, int buf_size),
57  int64_t (*seek)(void *opaque, int64_t offset, int whence))
58 {
59  AVIOContext *const s = &ctx->pub;
60 
61  memset(ctx, 0, sizeof(*ctx));
62 
63  s->buffer = buffer;
64  ctx->orig_buffer_size =
65  s->buffer_size = buffer_size;
66  s->buf_ptr = buffer;
67  s->buf_ptr_max = buffer;
68  s->opaque = opaque;
69  s->direct = 0;
70 
72 
73  s->write_packet = write_packet;
74  s->read_packet = read_packet;
75  s->seek = seek;
76  s->pos = 0;
77  s->eof_reached = 0;
78  s->error = 0;
79  s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
80  s->min_packet_size = 0;
81  s->max_packet_size = 0;
82  s->update_checksum = NULL;
83  ctx->short_seek_threshold = SHORT_SEEK_THRESHOLD;
84 
85  if (!read_packet && !write_flag) {
86  s->pos = buffer_size;
87  s->buf_end = s->buffer + buffer_size;
88  }
89  s->read_pause = NULL;
90  s->read_seek = NULL;
91 
92  s->write_data_type = NULL;
93  s->ignore_boundary_point = 0;
94  ctx->current_type = AVIO_DATA_MARKER_UNKNOWN;
95  ctx->last_time = AV_NOPTS_VALUE;
96  ctx->short_seek_get = NULL;
97 }
98 
99 void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
100 {
101  ffio_init_context(s, (unsigned char*)buffer, buffer_size, 0, NULL, NULL, NULL, NULL);
102 }
103 
104 void ffio_init_write_context(FFIOContext *s, uint8_t *buffer, int buffer_size)
105 {
106  ffio_init_context(s, buffer, buffer_size, 1, NULL, NULL, NULL, NULL);
107 }
108 
110  unsigned char *buffer,
111  int buffer_size,
112  int write_flag,
113  void *opaque,
114  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
115  int (*write_packet)(void *opaque, const uint8_t *buf, int buf_size),
116  int64_t (*seek)(void *opaque, int64_t offset, int whence))
117 {
118  FFIOContext *s = av_malloc(sizeof(*s));
119  if (!s)
120  return NULL;
121  ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
123  return &s->pub;
124 }
125 
127 {
128  AVIOContext *s = *ps;
129  if (s) {
130  av_freep(&s->protocol_whitelist);
131  av_freep(&s->protocol_blacklist);
132  }
133  av_freep(ps);
134 }
135 
136 static void writeout(AVIOContext *s, const uint8_t *data, int len)
137 {
138  FFIOContext *const ctx = ffiocontext(s);
139  if (!s->error) {
140  int ret = 0;
141  if (s->write_data_type)
142  ret = s->write_data_type(s->opaque, data,
143  len,
144  ctx->current_type,
145  ctx->last_time);
146  else if (s->write_packet)
147  ret = s->write_packet(s->opaque, data, len);
148  if (ret < 0) {
149  s->error = ret;
150  } else {
151  ctx->bytes_written += len;
152  s->bytes_written = ctx->bytes_written;
153 
154  if (s->pos + len > ctx->written_output_size) {
155  ctx->written_output_size = s->pos + len;
156  }
157  }
158  }
159  if (ctx->current_type == AVIO_DATA_MARKER_SYNC_POINT ||
160  ctx->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
161  ctx->current_type = AVIO_DATA_MARKER_UNKNOWN;
162  }
163  ctx->last_time = AV_NOPTS_VALUE;
164  ctx->writeout_count++;
165  s->pos += len;
166 }
167 
169 {
170  s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);
171  if (s->write_flag && s->buf_ptr_max > s->buffer) {
172  writeout(s, s->buffer, s->buf_ptr_max - s->buffer);
173  if (s->update_checksum) {
174  s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
175  s->buf_ptr_max - s->checksum_ptr);
176  s->checksum_ptr = s->buffer;
177  }
178  }
179  s->buf_ptr = s->buf_ptr_max = s->buffer;
180  if (!s->write_flag)
181  s->buf_end = s->buffer;
182 }
183 
184 void avio_w8(AVIOContext *s, int b)
185 {
186  av_assert2(b>=-128 && b<=255);
187  *s->buf_ptr++ = b;
188  if (s->buf_ptr >= s->buf_end)
189  flush_buffer(s);
190 }
191 
192 void ffio_fill(AVIOContext *s, int b, int64_t count)
193 {
194  while (count > 0) {
195  int len = FFMIN(s->buf_end - s->buf_ptr, count);
196  memset(s->buf_ptr, b, len);
197  s->buf_ptr += len;
198 
199  if (s->buf_ptr >= s->buf_end)
200  flush_buffer(s);
201 
202  count -= len;
203  }
204 }
205 
206 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
207 {
208  if (size <= 0)
209  return;
210  if (s->direct && !s->update_checksum) {
211  avio_flush(s);
212  writeout(s, buf, size);
213  return;
214  }
215  do {
216  int len = FFMIN(s->buf_end - s->buf_ptr, size);
217  memcpy(s->buf_ptr, buf, len);
218  s->buf_ptr += len;
219 
220  if (s->buf_ptr >= s->buf_end)
221  flush_buffer(s);
222 
223  buf += len;
224  size -= len;
225  } while (size > 0);
226 }
227 
229 {
230  int seekback = s->write_flag ? FFMIN(0, s->buf_ptr - s->buf_ptr_max) : 0;
231  flush_buffer(s);
232  if (seekback)
233  avio_seek(s, seekback, SEEK_CUR);
234 }
235 
237 {
238  FFIOContext *const ctx = ffiocontext(s);
239  int64_t offset1;
240  int64_t pos;
241  int buffer_size;
242  int short_seek;
243  whence &= ~AVSEEK_FORCE; // force flag does nothing
244 
245  if(!s)
246  return AVERROR(EINVAL);
247 
248  if ((whence & AVSEEK_SIZE))
249  return s->seek ? s->seek(s->opaque, offset, AVSEEK_SIZE) : AVERROR(ENOSYS);
250 
251  buffer_size = s->buf_end - s->buffer;
252  // pos is the absolute position that the beginning of s->buffer corresponds to in the file
253  pos = s->pos - (s->write_flag ? 0 : buffer_size);
254 
255  if (whence != SEEK_CUR && whence != SEEK_SET)
256  return AVERROR(EINVAL);
257 
258  if (whence == SEEK_CUR) {
259  offset1 = pos + (s->buf_ptr - s->buffer);
260  if (offset == 0)
261  return offset1;
262  if (offset > INT64_MAX - offset1)
263  return AVERROR(EINVAL);
264  offset += offset1;
265  }
266  if (offset < 0)
267  return AVERROR(EINVAL);
268 
269  short_seek = ctx->short_seek_threshold;
270  if (ctx->short_seek_get) {
271  int tmp = ctx->short_seek_get(s->opaque);
272  short_seek = FFMAX(tmp, short_seek);
273  }
274 
275  offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
276  s->buf_ptr_max = FFMAX(s->buf_ptr_max, s->buf_ptr);
277  if ((!s->direct || !s->seek) &&
278  offset1 >= 0 && offset1 <= (s->write_flag ? s->buf_ptr_max - s->buffer : buffer_size)) {
279  /* can do the seek inside the buffer */
280  s->buf_ptr = s->buffer + offset1;
281  } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
282  offset1 <= buffer_size + short_seek) &&
283  !s->write_flag && offset1 >= 0 &&
284  (!s->direct || !s->seek)) {
285  while(s->pos < offset && !s->eof_reached)
286  fill_buffer(s);
287  if (s->eof_reached)
288  return AVERROR_EOF;
289  s->buf_ptr = s->buf_end - (s->pos - offset);
290  } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
291  int64_t res;
292 
293  pos -= FFMIN(buffer_size>>1, pos);
294  if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
295  return res;
296  s->buf_end =
297  s->buf_ptr = s->buffer;
298  s->pos = pos;
299  s->eof_reached = 0;
300  fill_buffer(s);
301  return avio_seek(s, offset, SEEK_SET);
302  } else {
303  int64_t res;
304  if (s->write_flag) {
305  flush_buffer(s);
306  }
307  if (!s->seek)
308  return AVERROR(EPIPE);
309  if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
310  return res;
311  ctx->seek_count++;
312  if (!s->write_flag)
313  s->buf_end = s->buffer;
314  s->buf_ptr = s->buf_ptr_max = s->buffer;
315  s->pos = offset;
316  }
317  s->eof_reached = 0;
318  return offset;
319 }
320 
322 {
323  return avio_seek(s, offset, SEEK_CUR);
324 }
325 
327 {
328  FFIOContext *const ctx = ffiocontext(s);
329  int64_t size;
330 
331  if (!s)
332  return AVERROR(EINVAL);
333 
334  if (ctx->written_output_size)
335  return ctx->written_output_size;
336 
337  if (!s->seek)
338  return AVERROR(ENOSYS);
339  size = s->seek(s->opaque, 0, AVSEEK_SIZE);
340  if (size < 0) {
341  if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
342  return size;
343  size++;
344  s->seek(s->opaque, s->pos, SEEK_SET);
345  }
346  return size;
347 }
348 
350 {
351  if(!s)
352  return 0;
353  if(s->eof_reached){
354  s->eof_reached=0;
355  fill_buffer(s);
356  }
357  return s->eof_reached;
358 }
359 
360 void avio_wl32(AVIOContext *s, unsigned int val)
361 {
362  avio_w8(s, (uint8_t) val );
363  avio_w8(s, (uint8_t)(val >> 8 ));
364  avio_w8(s, (uint8_t)(val >> 16));
365  avio_w8(s, val >> 24 );
366 }
367 
368 void avio_wb32(AVIOContext *s, unsigned int val)
369 {
370  avio_w8(s, val >> 24 );
371  avio_w8(s, (uint8_t)(val >> 16));
372  avio_w8(s, (uint8_t)(val >> 8 ));
373  avio_w8(s, (uint8_t) val );
374 }
375 
376 int avio_put_str(AVIOContext *s, const char *str)
377 {
378  int len = 1;
379  if (str) {
380  len += strlen(str);
381  avio_write(s, (const unsigned char *) str, len);
382  } else
383  avio_w8(s, 0);
384  return len;
385 }
386 
387 static inline int put_str16(AVIOContext *s, const char *str, const int be)
388 {
389  const uint8_t *q = str;
390  int ret = 0;
391  int err = 0;
392 
393  while (*q) {
394  uint32_t ch;
395  uint16_t tmp;
396 
397  GET_UTF8(ch, *q++, goto invalid;)
398  PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
399  ret += 2;)
400  continue;
401 invalid:
402  av_log(s, AV_LOG_ERROR, "Invalid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
403  err = AVERROR(EINVAL);
404  if (!*(q-1))
405  break;
406  }
407  if (be)
408  avio_wb16(s, 0);
409  else
410  avio_wl16(s, 0);
411  if (err)
412  return err;
413  ret += 2;
414  return ret;
415 }
416 
417 #define PUT_STR16(type, big_endian) \
418 int avio_put_str16 ## type(AVIOContext *s, const char *str) \
419 { \
420 return put_str16(s, str, big_endian); \
421 }
422 
423 PUT_STR16(le, 0)
424 PUT_STR16(be, 1)
425 
426 #undef PUT_STR16
427 
428 void avio_wl64(AVIOContext *s, uint64_t val)
429 {
430  avio_wl32(s, (uint32_t)(val & 0xffffffff));
431  avio_wl32(s, (uint32_t)(val >> 32));
432 }
433 
434 void avio_wb64(AVIOContext *s, uint64_t val)
435 {
436  avio_wb32(s, (uint32_t)(val >> 32));
437  avio_wb32(s, (uint32_t)(val & 0xffffffff));
438 }
439 
440 void avio_wl16(AVIOContext *s, unsigned int val)
441 {
442  avio_w8(s, (uint8_t)val);
443  avio_w8(s, (int)val >> 8);
444 }
445 
446 void avio_wb16(AVIOContext *s, unsigned int val)
447 {
448  avio_w8(s, (int)val >> 8);
449  avio_w8(s, (uint8_t)val);
450 }
451 
452 void avio_wl24(AVIOContext *s, unsigned int val)
453 {
454  avio_wl16(s, val & 0xffff);
455  avio_w8(s, (int)val >> 16);
456 }
457 
458 void avio_wb24(AVIOContext *s, unsigned int val)
459 {
460  avio_wb16(s, (int)val >> 8);
461  avio_w8(s, (uint8_t)val);
462 }
463 
465 {
466  FFIOContext *const ctx = ffiocontext(s);
468  if (s->buf_ptr - s->buffer >= s->min_packet_size)
469  avio_flush(s);
470  return;
471  }
472  if (!s->write_data_type)
473  return;
474  // If ignoring boundary points, just treat it as unknown
475  if (type == AVIO_DATA_MARKER_BOUNDARY_POINT && s->ignore_boundary_point)
477  // Avoid unnecessary flushes if we are already in non-header/trailer
478  // data and setting the type to unknown
480  (ctx->current_type != AVIO_DATA_MARKER_HEADER &&
481  ctx->current_type != AVIO_DATA_MARKER_TRAILER))
482  return;
483 
484  switch (type) {
487  // For header/trailer, ignore a new marker of the same type;
488  // consecutive header/trailer markers can be merged.
489  if (type == ctx->current_type)
490  return;
491  break;
492  }
493 
494  // If we've reached here, we have a new, noteworthy marker.
495  // Flush the previous data and mark the start of the new data.
496  avio_flush(s);
497  ctx->current_type = type;
498  ctx->last_time = time;
499 }
500 
501 static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
502 {
503  int ret;
504 
505  if (!s->read_packet)
506  return AVERROR(EINVAL);
507  ret = s->read_packet(s->opaque, buf, size);
508  av_assert2(ret || s->max_packet_size);
509  return ret;
510 }
511 
512 /* Input stream */
513 
514 static void fill_buffer(AVIOContext *s)
515 {
516  FFIOContext *const ctx = (FFIOContext *)s;
517  int max_buffer_size = s->max_packet_size ?
518  s->max_packet_size : IO_BUFFER_SIZE;
519  uint8_t *dst = s->buf_end - s->buffer + max_buffer_size <= s->buffer_size ?
520  s->buf_end : s->buffer;
521  int len = s->buffer_size - (dst - s->buffer);
522 
523  /* can't fill the buffer without read_packet, just set EOF if appropriate */
524  if (!s->read_packet && s->buf_ptr >= s->buf_end)
525  s->eof_reached = 1;
526 
527  /* no need to do anything if EOF already reached */
528  if (s->eof_reached)
529  return;
530 
531  if (s->update_checksum && dst == s->buffer) {
532  if (s->buf_end > s->checksum_ptr)
533  s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
534  s->buf_end - s->checksum_ptr);
535  s->checksum_ptr = s->buffer;
536  }
537 
538  /* make buffer smaller in case it ended up large after probing */
539  if (s->read_packet && ctx->orig_buffer_size &&
540  s->buffer_size > ctx->orig_buffer_size && len >= ctx->orig_buffer_size) {
541  if (dst == s->buffer && s->buf_ptr != dst) {
542  int ret = set_buf_size(s, ctx->orig_buffer_size);
543  if (ret < 0)
544  av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
545 
546  s->checksum_ptr = dst = s->buffer;
547  }
548  len = ctx->orig_buffer_size;
549  }
550 
552  if (len == AVERROR_EOF) {
553  /* do not modify buffer if EOF reached so that a seek back can
554  be done without rereading data */
555  s->eof_reached = 1;
556  } else if (len < 0) {
557  s->eof_reached = 1;
558  s->error= len;
559  } else {
560  s->pos += len;
561  s->buf_ptr = dst;
562  s->buf_end = dst + len;
564  s->bytes_read = ffiocontext(s)->bytes_read;
565  }
566 }
567 
568 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
569  unsigned int len)
570 {
571  return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
572 }
573 
574 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
575  unsigned int len)
576 {
577  return av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), checksum, buf, len);
578 }
579 
580 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
581  unsigned int len)
582 {
583  return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
584 }
585 
587 {
588  s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
589  s->buf_ptr - s->checksum_ptr);
590  s->update_checksum = NULL;
591  return s->checksum;
592 }
593 
595  unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
596  unsigned long checksum)
597 {
598  s->update_checksum = update_checksum;
599  if (s->update_checksum) {
600  s->checksum = checksum;
601  s->checksum_ptr = s->buf_ptr;
602  }
603 }
604 
605 /* XXX: put an inline version */
607 {
608  if (s->buf_ptr >= s->buf_end)
609  fill_buffer(s);
610  if (s->buf_ptr < s->buf_end)
611  return *s->buf_ptr++;
612  return 0;
613 }
614 
615 int avio_read(AVIOContext *s, unsigned char *buf, int size)
616 {
617  int len, size1;
618 
619  size1 = size;
620  while (size > 0) {
621  len = FFMIN(s->buf_end - s->buf_ptr, size);
622  if (len == 0 || s->write_flag) {
623  if((s->direct || size > s->buffer_size) && !s->update_checksum && s->read_packet) {
624  // bypass the buffer and read data directly into buf
625  len = read_packet_wrapper(s, buf, size);
626  if (len == AVERROR_EOF) {
627  /* do not modify buffer if EOF reached so that a seek back can
628  be done without rereading data */
629  s->eof_reached = 1;
630  break;
631  } else if (len < 0) {
632  s->eof_reached = 1;
633  s->error= len;
634  break;
635  } else {
636  s->pos += len;
638  s->bytes_read = ffiocontext(s)->bytes_read;
639  size -= len;
640  buf += len;
641  // reset the buffer
642  s->buf_ptr = s->buffer;
643  s->buf_end = s->buffer/* + len*/;
644  }
645  } else {
646  fill_buffer(s);
647  len = s->buf_end - s->buf_ptr;
648  if (len == 0)
649  break;
650  }
651  } else {
652  memcpy(buf, s->buf_ptr, len);
653  buf += len;
654  s->buf_ptr += len;
655  size -= len;
656  }
657  }
658  if (size1 == size) {
659  if (s->error) return s->error;
660  if (avio_feof(s)) return AVERROR_EOF;
661  }
662  return size1 - size;
663 }
664 
665 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
666 {
667  int ret = avio_read(s, buf, size);
668  if (ret == size)
669  return ret;
670  if (ret < 0 && ret != AVERROR_EOF)
671  return ret;
672  return AVERROR_INVALIDDATA;
673 }
674 
675 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
676 {
677  if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
678  *data = s->buf_ptr;
679  s->buf_ptr += size;
680  return size;
681  } else {
682  *data = buf;
683  return avio_read(s, buf, size);
684  }
685 }
686 
687 int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
688 {
689  int len;
690 
691  if (size < 0)
692  return AVERROR(EINVAL);
693 
694  if (s->read_packet && s->write_flag) {
695  len = read_packet_wrapper(s, buf, size);
696  if (len > 0)
697  s->pos += len;
698  return len;
699  }
700 
701  len = s->buf_end - s->buf_ptr;
702  if (len == 0) {
703  fill_buffer(s);
704  len = s->buf_end - s->buf_ptr;
705  }
706  if (len > size)
707  len = size;
708  memcpy(buf, s->buf_ptr, len);
709  s->buf_ptr += len;
710  if (!len) {
711  if (s->error) return s->error;
712  if (avio_feof(s)) return AVERROR_EOF;
713  }
714  return len;
715 }
716 
717 unsigned int avio_rl16(AVIOContext *s)
718 {
719  unsigned int val;
720  val = avio_r8(s);
721  val |= avio_r8(s) << 8;
722  return val;
723 }
724 
725 unsigned int avio_rl24(AVIOContext *s)
726 {
727  unsigned int val;
728  val = avio_rl16(s);
729  val |= avio_r8(s) << 16;
730  return val;
731 }
732 
733 unsigned int avio_rl32(AVIOContext *s)
734 {
735  unsigned int val;
736  val = avio_rl16(s);
737  val |= avio_rl16(s) << 16;
738  return val;
739 }
740 
742 {
743  uint64_t val;
744  val = (uint64_t)avio_rl32(s);
745  val |= (uint64_t)avio_rl32(s) << 32;
746  return val;
747 }
748 
749 unsigned int avio_rb16(AVIOContext *s)
750 {
751  unsigned int val;
752  val = avio_r8(s) << 8;
753  val |= avio_r8(s);
754  return val;
755 }
756 
757 unsigned int avio_rb24(AVIOContext *s)
758 {
759  unsigned int val;
760  val = avio_rb16(s) << 8;
761  val |= avio_r8(s);
762  return val;
763 }
764 unsigned int avio_rb32(AVIOContext *s)
765 {
766  unsigned int val;
767  val = avio_rb16(s) << 16;
768  val |= avio_rb16(s);
769  return val;
770 }
771 
772 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
773 {
774  int i = 0;
775  char c;
776 
777  do {
778  c = avio_r8(s);
779  if (c && i < maxlen-1)
780  buf[i++] = c;
781  } while (c != '\n' && c != '\r' && c);
782  if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
783  avio_skip(s, -1);
784 
785  buf[i] = 0;
786  return i;
787 }
788 
789 int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
790 {
791  int len = ff_get_line(s, buf, maxlen);
792  while (len > 0 && av_isspace(buf[len - 1]))
793  buf[--len] = '\0';
794  return len;
795 }
796 
801 
804  int64_t max_len)
805 {
806  int len, end;
807  int64_t read = 0;
808  char tmp[1024];
809  char c;
810 
811  if (!max_len)
812  return 0;
813 
814  do {
815  len = 0;
816  do {
817  c = avio_r8(s);
818  end = ((mode == FFBPrintReadLine && (c == '\r' || c == '\n')) ||
819  c == '\0');
820  if (!end)
821  tmp[len++] = c;
822  } while (!end && len < sizeof(tmp) &&
823  ((max_len < 0) || (read + len < max_len)));
825  read += len;
826  } while (!end && ((max_len < 0) || (read < max_len)));
827 
828  if (mode == FFBPrintReadLine &&
829  c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
830  avio_skip(s, -1);
831 
832  if (!c && s->error)
833  return s->error;
834 
835  if (!c && !read && avio_feof(s))
836  return AVERROR_EOF;
837 
838  return read;
839 }
840 
843  int64_t max_len)
844 {
845  int64_t ret;
846 
847  av_bprint_clear(bp);
848  ret = read_string_to_bprint(s, bp, mode, max_len);
849  if (ret < 0)
850  return ret;
851 
852  if (!av_bprint_is_complete(bp))
853  return AVERROR(ENOMEM);
854 
855  return bp->len;
856 }
857 
859 {
861 }
862 
864  int64_t max_len)
865 {
867 }
868 
869 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
870 {
871  int i;
872 
873  if (buflen <= 0)
874  return AVERROR(EINVAL);
875  // reserve 1 byte for terminating 0
876  buflen = FFMIN(buflen - 1, maxlen);
877  for (i = 0; i < buflen; i++)
878  if (!(buf[i] = avio_r8(s)))
879  return i + 1;
880  buf[i] = 0;
881  for (; i < maxlen; i++)
882  if (!avio_r8(s))
883  return i + 1;
884  return maxlen;
885 }
886 
887 #define GET_STR16(type, read) \
888  int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
889 {\
890  char* q = buf;\
891  int ret = 0;\
892  if (buflen <= 0) \
893  return AVERROR(EINVAL); \
894  while (ret + 1 < maxlen) {\
895  uint8_t tmp;\
896  uint32_t ch;\
897  GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
898  if (!ch)\
899  break;\
900  PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
901  }\
902  *q = 0;\
903  return ret;\
904 }\
905 
906 GET_STR16(le, avio_rl16)
908 
909 #undef GET_STR16
910 
912 {
913  uint64_t val;
914  val = (uint64_t)avio_rb32(s) << 32;
915  val |= (uint64_t)avio_rb32(s);
916  return val;
917 }
918 
920  uint64_t val = 0;
921  int tmp;
922 
923  do{
924  tmp = avio_r8(bc);
925  val= (val<<7) + (tmp&127);
926  }while(tmp&128);
927  return val;
928 }
929 
930 unsigned int ffio_read_leb(AVIOContext *s) {
931  int more, i = 0;
932  unsigned leb = 0;
933 
934  do {
935  int byte = avio_r8(s);
936  unsigned bits = byte & 0x7f;
937  more = byte & 0x80;
938  if (i <= 4)
939  leb |= bits << (i * 7);
940  if (++i == 8)
941  break;
942  } while (more);
943 
944  return leb;
945 }
946 
947 void ffio_write_leb(AVIOContext *s, unsigned val)
948 {
949  int len;
950  uint8_t byte;
951 
952  len = (av_log2(val) + 7) / 7;
953 
954  for (int i = 0; i < len; i++) {
955  byte = val >> (7 * i) & 0x7f;
956  if (i < len - 1)
957  byte |= 0x80;
958 
959  avio_w8(s, byte);
960  }
961 }
962 
963 void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size,
964  const unsigned char *ending)
965 {
966  int ending_len = ending ? strlen(ending) : 1;
967  if (!ending)
968  ending = "\n";
969  if (size < 0)
970  size = strlen(buf);
971 
972  while (size > 0) {
973  size_t len = 0;
974  char last = 0;
975  for (; len < size; len++) {
976  last = buf[len];
977  if (last == '\r' || last == '\n')
978  break;
979  }
980 
981  avio_write(s, buf, len);
982  avio_write(s, ending, ending_len);
983 
984  buf += len + 1;
985  size -= len + 1;
986 
987  if (size > 0 && last == '\r' && buf[0] == '\n') {
988  buf++;
989  size--;
990  }
991  }
992 }
993 
995 {
996  const char *opts[] = {
997  "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", NULL };
998  const char **opt = opts;
999  uint8_t *buf = NULL;
1000  int ret = 0;
1001 
1002  while (*opt) {
1003  if (av_opt_get(pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
1004  if (buf[0] != '\0') {
1005  ret = av_dict_set(avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
1006  if (ret < 0)
1007  return ret;
1008  } else {
1009  av_freep(&buf);
1010  }
1011  }
1012  opt++;
1013  }
1014 
1015  return ret;
1016 }
1017 
1019 {
1020  if (s->update_checksum && s->buf_ptr > s->checksum_ptr) {
1021  s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
1022  s->buf_ptr - s->checksum_ptr);
1023  }
1024 }
1025 
1027 {
1028  uint8_t *buffer;
1029  int max_buffer_size = s->max_packet_size ?
1030  s->max_packet_size : IO_BUFFER_SIZE;
1031  ptrdiff_t filled = s->buf_end - s->buf_ptr;
1032 
1033  if (buf_size <= s->buf_end - s->buf_ptr)
1034  return 0;
1035 
1036  if (buf_size > INT_MAX - max_buffer_size)
1037  return AVERROR(EINVAL);
1038 
1039  buf_size += max_buffer_size - 1;
1040 
1041  if (buf_size + s->buf_ptr - s->buffer <= s->buffer_size || s->seekable || !s->read_packet)
1042  return 0;
1043  av_assert0(!s->write_flag);
1044 
1045  if (buf_size <= s->buffer_size) {
1046  update_checksum(s);
1047  memmove(s->buffer, s->buf_ptr, filled);
1048  } else {
1049  buffer = av_malloc(buf_size);
1050  if (!buffer)
1051  return AVERROR(ENOMEM);
1052  update_checksum(s);
1053  memcpy(buffer, s->buf_ptr, filled);
1054  av_free(s->buffer);
1055  s->buffer = buffer;
1056  s->buffer_size = buf_size;
1057  }
1058  s->buf_ptr = s->buffer;
1059  s->buf_end = s->buffer + filled;
1060  s->checksum_ptr = s->buffer;
1061  return 0;
1062 }
1063 
1065 {
1066  FFIOContext *const ctx = ffiocontext(s);
1067  if (ctx->maxsize >= 0) {
1068  int64_t pos = avio_tell(s);
1069  int64_t remaining = ctx->maxsize - pos;
1070  if (remaining < size) {
1071  int64_t newsize = avio_size(s);
1072  if (!ctx->maxsize || ctx->maxsize < newsize)
1073  ctx->maxsize = newsize - !newsize;
1074  if (pos > ctx->maxsize && ctx->maxsize >= 0)
1075  ctx->maxsize = AVERROR(EIO);
1076  if (ctx->maxsize >= 0)
1077  remaining = ctx->maxsize - pos;
1078  }
1079 
1080  if (ctx->maxsize >= 0 && remaining < size && size > 1) {
1081  av_log(NULL, remaining ? AV_LOG_ERROR : AV_LOG_DEBUG,
1082  "Truncating packet of size %d to %"PRId64"\n",
1083  size, remaining + !remaining);
1084  size = remaining + !remaining;
1085  }
1086  }
1087  return size;
1088 }
1089 
1090 static int set_buf_size(AVIOContext *s, int buf_size)
1091 {
1092  uint8_t *buffer;
1093  buffer = av_malloc(buf_size);
1094  if (!buffer)
1095  return AVERROR(ENOMEM);
1096 
1097  av_free(s->buffer);
1098  s->buffer = buffer;
1100  s->buffer_size = buf_size;
1101  s->buf_ptr = s->buf_ptr_max = buffer;
1102  url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
1103  return 0;
1104 }
1105 
1106 int ffio_realloc_buf(AVIOContext *s, int buf_size)
1107 {
1108  uint8_t *buffer;
1109  int data_size;
1110 
1111  if (!s->buffer_size)
1112  return set_buf_size(s, buf_size);
1113 
1114  if (buf_size <= s->buffer_size)
1115  return 0;
1116 
1117  buffer = av_malloc(buf_size);
1118  if (!buffer)
1119  return AVERROR(ENOMEM);
1120 
1121  data_size = s->write_flag ? (s->buf_ptr - s->buffer) : (s->buf_end - s->buf_ptr);
1122  if (data_size > 0)
1123  memcpy(buffer, s->write_flag ? s->buffer : s->buf_ptr, data_size);
1124  av_free(s->buffer);
1125  s->buffer = buffer;
1126  ffiocontext(s)->orig_buffer_size = buf_size;
1127  s->buffer_size = buf_size;
1128  s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer;
1129  if (s->write_flag)
1130  s->buf_ptr_max = s->buffer + data_size;
1131 
1132  s->buf_end = s->write_flag ? (s->buffer + s->buffer_size) : (s->buf_ptr + data_size);
1133 
1134  return 0;
1135 }
1136 
1137 static int url_resetbuf(AVIOContext *s, int flags)
1138 {
1140 
1141  if (flags & AVIO_FLAG_WRITE) {
1142  s->buf_end = s->buffer + s->buffer_size;
1143  s->write_flag = 1;
1144  } else {
1145  s->buf_end = s->buffer;
1146  s->write_flag = 0;
1147  }
1148  return 0;
1149 }
1150 
1151 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
1152 {
1153  int64_t buffer_start;
1154  int buffer_size;
1155  int overlap, new_size, alloc_size;
1156  uint8_t *buf = *bufp;
1157 
1158  if (s->write_flag) {
1159  av_freep(bufp);
1160  return AVERROR(EINVAL);
1161  }
1162 
1163  buffer_size = s->buf_end - s->buffer;
1164 
1165  /* the buffers must touch or overlap */
1166  if ((buffer_start = s->pos - buffer_size) > buf_size) {
1167  av_freep(bufp);
1168  return AVERROR(EINVAL);
1169  }
1170 
1171  overlap = buf_size - buffer_start;
1172  new_size = buf_size + buffer_size - overlap;
1173 
1174  alloc_size = FFMAX(s->buffer_size, new_size);
1175  if (alloc_size > buf_size)
1176  if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
1177  return AVERROR(ENOMEM);
1178 
1179  if (new_size > buf_size) {
1180  memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
1181  buf_size = new_size;
1182  }
1183 
1184  av_free(s->buffer);
1185  s->buf_ptr = s->buffer = buf;
1186  s->buffer_size = alloc_size;
1187  s->pos = buf_size;
1188  s->buf_end = s->buf_ptr + buf_size;
1189  s->eof_reached = 0;
1190 
1191  return 0;
1192 }
1193 
1194 int avio_vprintf(AVIOContext *s, const char *fmt, va_list ap)
1195 {
1196  AVBPrint bp;
1197 
1198  av_bprint_init(&bp, 0, INT_MAX);
1199  av_vbprintf(&bp, fmt, ap);
1200  if (!av_bprint_is_complete(&bp)) {
1201  av_bprint_finalize(&bp, NULL);
1202  s->error = AVERROR(ENOMEM);
1203  return AVERROR(ENOMEM);
1204  }
1205  avio_write(s, bp.str, bp.len);
1206  av_bprint_finalize(&bp, NULL);
1207  return bp.len;
1208 }
1209 
1210 int avio_printf(AVIOContext *s, const char *fmt, ...)
1211 {
1212  va_list ap;
1213  int ret;
1214 
1215  va_start(ap, fmt);
1216  ret = avio_vprintf(s, fmt, ap);
1217  va_end(ap);
1218 
1219  return ret;
1220 }
1221 
1222 void avio_print_string_array(AVIOContext *s, const char *const strings[])
1223 {
1224  for(; *strings; strings++)
1225  avio_write(s, (const unsigned char *)*strings, strlen(*strings));
1226 }
1227 
1228 int avio_pause(AVIOContext *s, int pause)
1229 {
1230  if (!s->read_pause)
1231  return AVERROR(ENOSYS);
1232  return s->read_pause(s->opaque, pause);
1233 }
1234 
1236  int64_t timestamp, int flags)
1237 {
1238  int64_t ret;
1239  if (!s->read_seek)
1240  return AVERROR(ENOSYS);
1241  ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1242  if (ret >= 0) {
1243  int64_t pos;
1244  s->buf_ptr = s->buf_end; // Flush buffer
1245  pos = s->seek(s->opaque, 0, SEEK_CUR);
1246  if (pos >= 0)
1247  s->pos = pos;
1248  else if (pos != AVERROR(ENOSYS))
1249  ret = pos;
1250  }
1251  return ret;
1252 }
1253 
1254 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1255 {
1256  int ret;
1257  char buf[1024];
1258  while (max_size) {
1259  ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1260  if (ret == AVERROR_EOF)
1261  return 0;
1262  if (ret <= 0)
1263  return ret;
1264  av_bprint_append_data(pb, buf, ret);
1265  if (!av_bprint_is_complete(pb))
1266  return AVERROR(ENOMEM);
1267  max_size -= ret;
1268  }
1269  return 0;
1270 }
1271 
1272 /* output in a dynamic buffer */
1273 
1274 typedef struct DynBuffer {
1276  uint8_t *buffer;
1278  uint8_t io_buffer[1];
1279 } DynBuffer;
1280 
1281 static int dyn_buf_write(void *opaque, const uint8_t *buf, int buf_size)
1282 {
1283  DynBuffer *d = opaque;
1284  unsigned new_size;
1285 
1286  /* reallocate buffer if needed */
1287  new_size = (unsigned)d->pos + buf_size;
1288  if (new_size < d->pos || new_size > INT_MAX)
1289  return AVERROR(ERANGE);
1290  if (new_size > d->allocated_size) {
1291  unsigned new_allocated_size = d->allocated_size ? d->allocated_size
1292  : new_size;
1293  int err;
1294  while (new_size > new_allocated_size)
1295  new_allocated_size += new_allocated_size / 2 + 1;
1296 
1297  new_allocated_size = FFMIN(new_allocated_size, INT_MAX);
1298 
1299  if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1300  d->allocated_size = 0;
1301  d->size = 0;
1302  return err;
1303  }
1304  d->allocated_size = new_allocated_size;
1305  }
1306  memcpy(d->buffer + d->pos, buf, buf_size);
1307  d->pos = new_size;
1308  if (d->pos > d->size)
1309  d->size = d->pos;
1310  return buf_size;
1311 }
1312 
1313 static int dyn_packet_buf_write(void *opaque, const uint8_t *buf, int buf_size)
1314 {
1315  unsigned char buf1[4];
1316  int ret;
1317 
1318  /* packetized write: output the header */
1319  AV_WB32(buf1, buf_size);
1320  ret = dyn_buf_write(opaque, buf1, 4);
1321  if (ret < 0)
1322  return ret;
1323 
1324  /* then the data */
1325  return dyn_buf_write(opaque, buf, buf_size);
1326 }
1327 
1328 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1329 {
1330  DynBuffer *d = opaque;
1331 
1332  if (whence == SEEK_CUR)
1333  offset += d->pos;
1334  else if (whence == SEEK_END)
1335  offset += d->size;
1336  if (offset < 0)
1337  return AVERROR(EINVAL);
1338  if (offset > INT_MAX)
1339  return AVERROR(ERANGE);
1340  d->pos = offset;
1341  return 0;
1342 }
1343 
1344 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1345 {
1346  struct { FFIOContext pb; DynBuffer d; } *ret;
1347  DynBuffer *d;
1348  unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1349 
1350  if (sizeof(*ret) + io_buffer_size < io_buffer_size)
1351  return AVERROR(ERANGE);
1352  ret = av_mallocz(sizeof(*ret) + io_buffer_size);
1353  if (!ret)
1354  return AVERROR(ENOMEM);
1355  d = &ret->d;
1356  d->io_buffer_size = io_buffer_size;
1357  ffio_init_context(&ret->pb, d->io_buffer, d->io_buffer_size, 1, d, NULL,
1358  max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1359  max_packet_size ? NULL : dyn_buf_seek);
1360  *s = &ret->pb.pub;
1361  (*s)->max_packet_size = max_packet_size;
1362  return 0;
1363 }
1364 
1366 {
1367  return url_open_dyn_buf_internal(s, 0);
1368 }
1369 
1370 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1371 {
1372  if (max_packet_size <= 0)
1373  return AVERROR(EINVAL);
1374  return url_open_dyn_buf_internal(s, max_packet_size);
1375 }
1376 
1377 int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1378 {
1379  DynBuffer *d;
1380 
1381  if (!s) {
1382  *pbuffer = NULL;
1383  return 0;
1384  }
1385  d = s->opaque;
1386 
1387  if (!s->error && !d->size) {
1388  *pbuffer = d->io_buffer;
1389  return FFMAX(s->buf_ptr, s->buf_ptr_max) - s->buffer;
1390  }
1391 
1392  avio_flush(s);
1393 
1394  *pbuffer = d->buffer;
1395 
1396  return d->size;
1397 }
1398 
1400 {
1401  DynBuffer *d = s->opaque;
1402  int max_packet_size = s->max_packet_size;
1403 
1405  1, d, NULL, s->write_packet, s->seek);
1406  s->max_packet_size = max_packet_size;
1407  d->pos = d->size = 0;
1408 }
1409 
1410 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1411 {
1412  DynBuffer *d;
1413  int size;
1414  int padding = 0;
1415 
1416  if (!s) {
1417  *pbuffer = NULL;
1418  return 0;
1419  }
1420 
1421  /* don't attempt to pad fixed-size packet buffers */
1422  if (!s->max_packet_size) {
1424  padding = AV_INPUT_BUFFER_PADDING_SIZE;
1425  }
1426 
1427  avio_flush(s);
1428 
1429  d = s->opaque;
1430  *pbuffer = d->buffer;
1431  size = d->size;
1432 
1433  avio_context_free(&s);
1434 
1435  return size - padding;
1436 }
1437 
1439 {
1440  DynBuffer *d;
1441 
1442  if (!*s)
1443  return;
1444 
1445  d = (*s)->opaque;
1446  av_free(d->buffer);
1448 }
1449 
1450 static int null_buf_write(void *opaque, const uint8_t *buf, int buf_size)
1451 {
1452  DynBuffer *d = opaque;
1453 
1454  d->pos += buf_size;
1455  if (d->pos > d->size)
1456  d->size = d->pos;
1457  return buf_size;
1458 }
1459 
1461 {
1462  int ret = url_open_dyn_buf_internal(s, 0);
1463  if (ret >= 0) {
1464  AVIOContext *pb = *s;
1466  }
1467  return ret;
1468 }
1469 
1471 {
1472  DynBuffer *d = s->opaque;
1473  int size;
1474 
1475  avio_flush(s);
1476 
1477  size = d->size;
1478 
1479  avio_context_free(&s);
1480 
1481  return size;
1482 }
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:930
AV_OPT_SEARCH_CHILDREN
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:605
flags
const SwsFlags flags[]
Definition: swscale.c:61
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:789
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:216
put_str16
static int put_str16(AVIOContext *s, const char *str, const int be)
Definition: aviobuf.c:387
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:349
avio_print_string_array
void avio_print_string_array(AVIOContext *s, const char *const strings[])
Write a NULL terminated array of strings to the context.
Definition: aviobuf.c:1222
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:127
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:1377
url_open_dyn_buf_internal
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
Definition: aviobuf.c:1344
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:725
dyn_buf_seek
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
Definition: aviobuf.c:1328
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
avio_wl24
void avio_wl24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:452
av_isspace
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:218
mode
Definition: swscale.c:56
ffio_get_checksum
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:586
DynBuffer::buffer
uint8_t * buffer
Definition: aviobuf.c:1276
ffio_open_null_buf
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1460
b
#define b
Definition: input.c:42
AVSEEK_SIZE
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition: avio.h:468
IO_BUFFER_SIZE
#define IO_BUFFER_SIZE
Definition: aviobuf.c:36
data
const char data[16]
Definition: mxf.c:149
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:1235
DynBuffer::io_buffer
uint8_t io_buffer[1]
Definition: aviobuf.c:1278
avio_put_str
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:376
AVDictionary
Definition: dict.c:32
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:360
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:1254
ff_crcEDB88320_update
unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:574
FFBPrintReadString
@ FFBPrintReadString
Definition: aviobuf.c:798
AVIODataMarkerType
AVIODataMarkerType
Different data types that can be returned via the AVIO write_data_type callback.
Definition: avio.h:110
FFIOContext
Definition: avio_internal.h:28
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:184
update_checksum
static void update_checksum(AVIOContext *s)
Definition: aviobuf.c:1018
fill_buffer
static void fill_buffer(AVIOContext *s)
Definition: aviobuf.c:514
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
crc.h
ffio_reset_dyn_buf
void ffio_reset_dyn_buf(AVIOContext *s)
Reset a dynamic buffer.
Definition: aviobuf.c:1399
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
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:77
avio_wb24
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:458
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:440
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:488
flush_buffer
static void flush_buffer(AVIOContext *s)
Definition: aviobuf.c:168
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:994
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
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:464
avio_flush
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:228
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:863
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
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:368
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:665
null_buf_write
static int null_buf_write(void *opaque, const uint8_t *buf, int buf_size)
Definition: aviobuf.c:1450
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:1410
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:963
DynBuffer::pos
int pos
Definition: aviobuf.c:1275
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:192
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:675
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:618
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
dyn_packet_buf_write
static int dyn_packet_buf_write(void *opaque, const uint8_t *buf, int buf_size)
Definition: aviobuf.c:1313
ctx
AVFormatContext * ctx
Definition: movenc.c:49
avio_context_free
void avio_context_free(AVIOContext **ps)
Free the supplied IO context and everything associated with it.
Definition: aviobuf.c:126
if
if(ret)
Definition: filter_design.txt:179
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:104
av_realloc_f
#define av_realloc_f(p, o, n)
Definition: tableprint_vlc.h:33
internal.h
opts
AVDictionary * opts
Definition: movenc.c:51
ffio_limit
int ffio_limit(AVIOContext *s, int size)
Definition: aviobuf.c:1064
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:615
NULL
#define NULL
Definition: coverity.c:32
DynBuffer
Definition: aviobuf.c:1274
tmp
static uint8_t tmp[20]
Definition: aes_ctr.c:47
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:139
PUT_UTF16
#define PUT_UTF16(val, tmp, PUT_16BIT)
Definition: common.h:575
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:1210
read_string_to_bprint
static int64_t read_string_to_bprint(AVIOContext *s, AVBPrint *bp, FFBPrintReadStringMode mode, int64_t max_len)
Definition: aviobuf.c:802
seek
static void BS_FUNC() seek(BSCTX *bc, unsigned pos)
Seek to the given bit position.
Definition: bitstream_template.h:407
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:43
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:1194
read_packet_wrapper
static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
Definition: aviobuf.c:501
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:1228
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:415
avio_rb24
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:757
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:749
ffio_realloc_buf
int ffio_realloc_buf(AVIOContext *s, int buf_size)
Reallocate a given buffer for AVIOContext.
Definition: aviobuf.c:1106
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
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:858
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:235
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1438
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:1026
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:188
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:247
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:717
avio_rl64
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:741
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:1151
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:121
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
dyn_buf_write
static int dyn_buf_write(void *opaque, const uint8_t *buf, int buf_size)
Definition: aviobuf.c:1281
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:733
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:206
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:236
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:594
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:68
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:1370
bprint.h
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
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:109
avio_internal.h
PUT_STR16
#define PUT_STR16(type, big_endian)
Definition: aviobuf.c:417
FFBPrintReadLine
@ FFBPrintReadLine
Definition: aviobuf.c:799
ffio_read_varlen
uint64_t ffio_read_varlen(AVIOContext *bc)
Definition: aviobuf.c:919
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:841
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:57
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
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:256
ff_get_line
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:772
GET_STR16
#define GET_STR16(type, read)
Definition: aviobuf.c:887
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:99
AV_CRC_32_IEEE
@ AV_CRC_32_IEEE
Definition: crc.h:52
DynBuffer::io_buffer_size
int io_buffer_size
Definition: aviobuf.c:1277
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:434
AVIO_DATA_MARKER_UNKNOWN
@ AVIO_DATA_MARKER_UNKNOWN
This is any, unlabelled data.
Definition: avio.h:134
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:869
ret
ret
Definition: filter_design.txt:187
pos
unsigned int pos
Definition: spdifenc.c:414
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:568
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:606
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:1137
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:99
defs.h
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:227
AVIO_DATA_MARKER_HEADER
@ AVIO_DATA_MARKER_HEADER
Header data; this needs to be present for the stream to be decodeable.
Definition: avio.h:114
ff_crcA001_update
unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:580
DynBuffer::size
int size
Definition: aviobuf.c:1275
AVSEEK_FORCE
#define AVSEEK_FORCE
OR'ing this flag into the "whence" parameter to a seek function causes it to seek by any means (like ...
Definition: avio.h:476
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:235
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:617
mem.h
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1365
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:446
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
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:86
DynBuffer::allocated_size
int allocated_size
Definition: aviobuf.c:1275
set_buf_size
static int set_buf_size(AVIOContext *s, int buf_size)
Definition: aviobuf.c:1090
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:764
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:50
av_opt_get
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:1215
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:1470
h
h
Definition: vp9dsp_template.c:2070
writeout
static void writeout(AVIOContext *s, const uint8_t *data, int len)
Definition: aviobuf.c:136
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:326
avio_read_partial
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:687
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:148
avio_wl64
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:428
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:321
ffio_write_leb
void ffio_write_leb(AVIOContext *s, unsigned val)
Definition: aviobuf.c:947
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:239
avio_rb64
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:911
FFBPrintReadStringMode
FFBPrintReadStringMode
Definition: aviobuf.c:797
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:145