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