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