FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/crc.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/log.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/avassert.h"
28 #include "avformat.h"
29 #include "avio.h"
30 #include "avio_internal.h"
31 #include "internal.h"
32 #include "url.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 4096
43 
44 static void *ffio_url_child_next(void *obj, void *prev)
45 {
46  AVIOContext *s = obj;
47  return prev ? NULL : s->opaque;
48 }
49 
50 static const AVClass *ffio_url_child_class_next(const AVClass *prev)
51 {
52  return prev ? NULL : &ffurl_context_class;
53 }
54 
55 static const AVOption ffio_url_options[] = {
56  { NULL },
57 };
58 
60  .class_name = "AVIOContext",
61  .item_name = av_default_item_name,
62  .version = LIBAVUTIL_VERSION_INT,
63  .option = ffio_url_options,
64  .child_next = ffio_url_child_next,
65  .child_class_next = ffio_url_child_class_next,
66 };
67 
68 static void fill_buffer(AVIOContext *s);
69 static int url_resetbuf(AVIOContext *s, int flags);
70 
72  unsigned char *buffer,
73  int buffer_size,
74  int write_flag,
75  void *opaque,
76  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
77  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
78  int64_t (*seek)(void *opaque, int64_t offset, int whence))
79 {
80  s->buffer = buffer;
81  s->buffer_size = buffer_size;
82  s->buf_ptr = buffer;
83  s->opaque = opaque;
84  s->direct = 0;
85 
86  url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
87 
90  s->seek = seek;
91  s->pos = 0;
92  s->must_flush = 0;
93  s->eof_reached = 0;
94  s->error = 0;
96  s->max_packet_size = 0;
97  s->update_checksum = NULL;
98 
99  if (!read_packet && !write_flag) {
100  s->pos = buffer_size;
101  s->buf_end = s->buffer + buffer_size;
102  }
103  s->read_pause = NULL;
104  s->read_seek = NULL;
105 
106  return 0;
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, uint8_t *buf, int buf_size),
116  int64_t (*seek)(void *opaque, int64_t offset, int whence))
117 {
118  AVIOContext *s = av_mallocz(sizeof(AVIOContext));
119  if (!s)
120  return NULL;
121  ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
122  read_packet, write_packet, seek);
123  return s;
124 }
125 
126 static void writeout(AVIOContext *s, const uint8_t *data, int len)
127 {
128  if (s->write_packet && !s->error) {
129  int ret = s->write_packet(s->opaque, (uint8_t *)data, len);
130  if (ret < 0) {
131  s->error = ret;
132  }
133  }
134  s->pos += len;
135 }
136 
137 static void flush_buffer(AVIOContext *s)
138 {
139  if (s->buf_ptr > s->buffer) {
140  writeout(s, s->buffer, s->buf_ptr - s->buffer);
141  if (s->update_checksum) {
143  s->buf_ptr - s->checksum_ptr);
144  s->checksum_ptr = s->buffer;
145  }
146  }
147  s->buf_ptr = s->buffer;
148 }
149 
150 void avio_w8(AVIOContext *s, int b)
151 {
152  av_assert2(b>=-128 && b<=255);
153  *s->buf_ptr++ = b;
154  if (s->buf_ptr >= s->buf_end)
155  flush_buffer(s);
156 }
157 
158 void ffio_fill(AVIOContext *s, int b, int count)
159 {
160  while (count > 0) {
161  int len = FFMIN(s->buf_end - s->buf_ptr, count);
162  memset(s->buf_ptr, b, len);
163  s->buf_ptr += len;
164 
165  if (s->buf_ptr >= s->buf_end)
166  flush_buffer(s);
167 
168  count -= len;
169  }
170 }
171 
172 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
173 {
174  if (s->direct && !s->update_checksum) {
175  avio_flush(s);
176  writeout(s, buf, size);
177  return;
178  }
179  while (size > 0) {
180  int len = FFMIN(s->buf_end - s->buf_ptr, size);
181  memcpy(s->buf_ptr, buf, len);
182  s->buf_ptr += len;
183 
184  if (s->buf_ptr >= s->buf_end)
185  flush_buffer(s);
186 
187  buf += len;
188  size -= len;
189  }
190 }
191 
193 {
194  flush_buffer(s);
195  s->must_flush = 0;
196 }
197 
198 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
199 {
200  int64_t offset1;
201  int64_t pos;
202  int force = whence & AVSEEK_FORCE;
203  whence &= ~AVSEEK_FORCE;
204 
205  if(!s)
206  return AVERROR(EINVAL);
207 
208  pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
209 
210  if (whence != SEEK_CUR && whence != SEEK_SET)
211  return AVERROR(EINVAL);
212 
213  if (whence == SEEK_CUR) {
214  offset1 = pos + (s->buf_ptr - s->buffer);
215  if (offset == 0)
216  return offset1;
217  offset += offset1;
218  }
219  offset1 = offset - pos;
220  if (!s->must_flush && (!s->direct || !s->seek) &&
221  offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
222  /* can do the seek inside the buffer */
223  s->buf_ptr = s->buffer + offset1;
224  } else if ((!s->seekable ||
225  offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
226  !s->write_flag && offset1 >= 0 &&
227  (!s->direct || !s->seek) &&
228  (whence != SEEK_END || force)) {
229  while(s->pos < offset && !s->eof_reached)
230  fill_buffer(s);
231  if (s->eof_reached)
232  return AVERROR_EOF;
233  s->buf_ptr = s->buf_end + offset - s->pos;
234  } else {
235  int64_t res;
236 
237  if (s->write_flag) {
238  flush_buffer(s);
239  s->must_flush = 1;
240  }
241  if (!s->seek)
242  return AVERROR(EPIPE);
243  if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
244  return res;
245  s->seek_count ++;
246  if (!s->write_flag)
247  s->buf_end = s->buffer;
248  s->buf_ptr = s->buffer;
249  s->pos = offset;
250  }
251  s->eof_reached = 0;
252  return offset;
253 }
254 
255 int64_t avio_skip(AVIOContext *s, int64_t offset)
256 {
257  return avio_seek(s, offset, SEEK_CUR);
258 }
259 
261 {
262  int64_t size;
263 
264  if (!s)
265  return AVERROR(EINVAL);
266 
267  if (!s->seek)
268  return AVERROR(ENOSYS);
269  size = s->seek(s->opaque, 0, AVSEEK_SIZE);
270  if (size < 0) {
271  if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
272  return size;
273  size++;
274  s->seek(s->opaque, s->pos, SEEK_SET);
275  }
276  return size;
277 }
278 
280 {
281  if(!s)
282  return 0;
283  if(s->eof_reached){
284  s->eof_reached=0;
285  fill_buffer(s);
286  }
287  return s->eof_reached;
288 }
289 
290 void avio_wl32(AVIOContext *s, unsigned int val)
291 {
292  avio_w8(s, (uint8_t) val );
293  avio_w8(s, (uint8_t)(val >> 8 ));
294  avio_w8(s, (uint8_t)(val >> 16));
295  avio_w8(s, val >> 24 );
296 }
297 
298 void avio_wb32(AVIOContext *s, unsigned int val)
299 {
300  avio_w8(s, val >> 24 );
301  avio_w8(s, (uint8_t)(val >> 16));
302  avio_w8(s, (uint8_t)(val >> 8 ));
303  avio_w8(s, (uint8_t) val );
304 }
305 
306 int avio_put_str(AVIOContext *s, const char *str)
307 {
308  int len = 1;
309  if (str) {
310  len += strlen(str);
311  avio_write(s, (const unsigned char *) str, len);
312  } else
313  avio_w8(s, 0);
314  return len;
315 }
316 
317 int avio_put_str16le(AVIOContext *s, const char *str)
318 {
319  const uint8_t *q = str;
320  int ret = 0;
321 
322  while (*q) {
323  uint32_t ch;
324  uint16_t tmp;
325 
326  GET_UTF8(ch, *q++, break;)
327  PUT_UTF16(ch, tmp, avio_wl16(s, tmp); ret += 2;)
328  }
329  avio_wl16(s, 0);
330  ret += 2;
331  return ret;
332 }
333 
334 int ff_get_v_length(uint64_t val)
335 {
336  int i = 1;
337 
338  while (val >>= 7)
339  i++;
340 
341  return i;
342 }
343 
344 void ff_put_v(AVIOContext *bc, uint64_t val)
345 {
346  int i = ff_get_v_length(val);
347 
348  while (--i > 0)
349  avio_w8(bc, 128 | (uint8_t)(val >> (7*i)));
350 
351  avio_w8(bc, val & 127);
352 }
353 
354 void avio_wl64(AVIOContext *s, uint64_t val)
355 {
356  avio_wl32(s, (uint32_t)(val & 0xffffffff));
357  avio_wl32(s, (uint32_t)(val >> 32));
358 }
359 
360 void avio_wb64(AVIOContext *s, uint64_t val)
361 {
362  avio_wb32(s, (uint32_t)(val >> 32));
363  avio_wb32(s, (uint32_t)(val & 0xffffffff));
364 }
365 
366 void avio_wl16(AVIOContext *s, unsigned int val)
367 {
368  avio_w8(s, (uint8_t)val);
369  avio_w8(s, (int)val >> 8);
370 }
371 
372 void avio_wb16(AVIOContext *s, unsigned int val)
373 {
374  avio_w8(s, (int)val >> 8);
375  avio_w8(s, (uint8_t)val);
376 }
377 
378 void avio_wl24(AVIOContext *s, unsigned int val)
379 {
380  avio_wl16(s, val & 0xffff);
381  avio_w8(s, (int)val >> 16);
382 }
383 
384 void avio_wb24(AVIOContext *s, unsigned int val)
385 {
386  avio_wb16(s, (int)val >> 8);
387  avio_w8(s, (uint8_t)val);
388 }
389 
390 /* Input stream */
391 
392 static void fill_buffer(AVIOContext *s)
393 {
394  uint8_t *dst = !s->max_packet_size &&
395  s->buf_end - s->buffer < s->buffer_size ?
396  s->buf_end : s->buffer;
397  int len = s->buffer_size - (dst - s->buffer);
398  int max_buffer_size = s->max_packet_size ?
400 
401  /* can't fill the buffer without read_packet, just set EOF if appropriate */
402  if (!s->read_packet && s->buf_ptr >= s->buf_end)
403  s->eof_reached = 1;
404 
405  /* no need to do anything if EOF already reached */
406  if (s->eof_reached)
407  return;
408 
409  if (s->update_checksum && dst == s->buffer) {
410  if (s->buf_end > s->checksum_ptr)
412  s->buf_end - s->checksum_ptr);
413  s->checksum_ptr = s->buffer;
414  }
415 
416  /* make buffer smaller in case it ended up large after probing */
417  if (s->read_packet && s->buffer_size > max_buffer_size) {
418  ffio_set_buf_size(s, max_buffer_size);
419 
420  s->checksum_ptr = dst = s->buffer;
421  len = s->buffer_size;
422  }
423 
424  if (s->read_packet)
425  len = s->read_packet(s->opaque, dst, len);
426  else
427  len = 0;
428  if (len <= 0) {
429  /* do not modify buffer if EOF reached so that a seek back can
430  be done without rereading data */
431  s->eof_reached = 1;
432  if (len < 0)
433  s->error = len;
434  } else {
435  s->pos += len;
436  s->buf_ptr = dst;
437  s->buf_end = dst + len;
438  s->bytes_read += len;
439  }
440 }
441 
442 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
443  unsigned int len)
444 {
445  return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
446 }
447 
449 {
451  s->buf_ptr - s->checksum_ptr);
452  s->update_checksum = NULL;
453  return s->checksum;
454 }
455 
457  unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
458  unsigned long checksum)
459 {
460  s->update_checksum = update_checksum;
461  if (s->update_checksum) {
462  s->checksum = checksum;
463  s->checksum_ptr = s->buf_ptr;
464  }
465 }
466 
467 /* XXX: put an inline version */
469 {
470  if (s->buf_ptr >= s->buf_end)
471  fill_buffer(s);
472  if (s->buf_ptr < s->buf_end)
473  return *s->buf_ptr++;
474  return 0;
475 }
476 
477 int avio_read(AVIOContext *s, unsigned char *buf, int size)
478 {
479  int len, size1;
480 
481  size1 = size;
482  while (size > 0) {
483  len = s->buf_end - s->buf_ptr;
484  if (len > size)
485  len = size;
486  if (len == 0 || s->write_flag) {
487  if((s->direct || size > s->buffer_size) && !s->update_checksum){
488  if(s->read_packet)
489  len = s->read_packet(s->opaque, buf, size);
490  if (len <= 0) {
491  /* do not modify buffer if EOF reached so that a seek back can
492  be done without rereading data */
493  s->eof_reached = 1;
494  if(len<0)
495  s->error= len;
496  break;
497  } else {
498  s->pos += len;
499  s->bytes_read += len;
500  size -= len;
501  buf += len;
502  s->buf_ptr = s->buffer;
503  s->buf_end = s->buffer/* + len*/;
504  }
505  } else {
506  fill_buffer(s);
507  len = s->buf_end - s->buf_ptr;
508  if (len == 0)
509  break;
510  }
511  } else {
512  memcpy(buf, s->buf_ptr, len);
513  buf += len;
514  s->buf_ptr += len;
515  size -= len;
516  }
517  }
518  if (size1 == size) {
519  if (s->error) return s->error;
520  if (url_feof(s)) return AVERROR_EOF;
521  }
522  return size1 - size;
523 }
524 
525 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
526 {
527  int len;
528 
529  if (size < 0)
530  return -1;
531 
532  if (s->read_packet && s->write_flag) {
533  len = s->read_packet(s->opaque, buf, size);
534  if (len > 0)
535  s->pos += len;
536  return len;
537  }
538 
539  len = s->buf_end - s->buf_ptr;
540  if (len == 0) {
541  /* Reset the buf_end pointer to the start of the buffer, to make sure
542  * the fill_buffer call tries to read as much data as fits into the
543  * full buffer, instead of just what space is left after buf_end.
544  * This avoids returning partial packets at the end of the buffer,
545  * for packet based inputs.
546  */
547  s->buf_end = s->buf_ptr = s->buffer;
548  fill_buffer(s);
549  len = s->buf_end - s->buf_ptr;
550  }
551  if (len > size)
552  len = size;
553  memcpy(buf, s->buf_ptr, len);
554  s->buf_ptr += len;
555  if (!len) {
556  if (s->error) return s->error;
557  if (url_feof(s)) return AVERROR_EOF;
558  }
559  return len;
560 }
561 
562 unsigned int avio_rl16(AVIOContext *s)
563 {
564  unsigned int val;
565  val = avio_r8(s);
566  val |= avio_r8(s) << 8;
567  return val;
568 }
569 
570 unsigned int avio_rl24(AVIOContext *s)
571 {
572  unsigned int val;
573  val = avio_rl16(s);
574  val |= avio_r8(s) << 16;
575  return val;
576 }
577 
578 unsigned int avio_rl32(AVIOContext *s)
579 {
580  unsigned int val;
581  val = avio_rl16(s);
582  val |= avio_rl16(s) << 16;
583  return val;
584 }
585 
586 uint64_t avio_rl64(AVIOContext *s)
587 {
588  uint64_t val;
589  val = (uint64_t)avio_rl32(s);
590  val |= (uint64_t)avio_rl32(s) << 32;
591  return val;
592 }
593 
594 unsigned int avio_rb16(AVIOContext *s)
595 {
596  unsigned int val;
597  val = avio_r8(s) << 8;
598  val |= avio_r8(s);
599  return val;
600 }
601 
602 unsigned int avio_rb24(AVIOContext *s)
603 {
604  unsigned int val;
605  val = avio_rb16(s) << 8;
606  val |= avio_r8(s);
607  return val;
608 }
609 unsigned int avio_rb32(AVIOContext *s)
610 {
611  unsigned int val;
612  val = avio_rb16(s) << 16;
613  val |= avio_rb16(s);
614  return val;
615 }
616 
617 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
618 {
619  int i = 0;
620  char c;
621 
622  do {
623  c = avio_r8(s);
624  if (c && i < maxlen-1)
625  buf[i++] = c;
626  } while (c != '\n' && c);
627 
628  buf[i] = 0;
629  return i;
630 }
631 
632 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
633 {
634  int i;
635 
636  if (buflen <= 0)
637  return AVERROR(EINVAL);
638  // reserve 1 byte for terminating 0
639  buflen = FFMIN(buflen - 1, maxlen);
640  for (i = 0; i < buflen; i++)
641  if (!(buf[i] = avio_r8(s)))
642  return i + 1;
643  buf[i] = 0;
644  for (; i < maxlen; i++)
645  if (!avio_r8(s))
646  return i + 1;
647  return maxlen;
648 }
649 
650 #define GET_STR16(type, read) \
651  int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
652 {\
653  char* q = buf;\
654  int ret = 0;\
655  if (buflen <= 0) \
656  return AVERROR(EINVAL); \
657  while (ret + 1 < maxlen) {\
658  uint8_t tmp;\
659  uint32_t ch;\
660  GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
661  if (!ch)\
662  break;\
663  PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
664  }\
665  *q = 0;\
666  return ret;\
667 }\
668 
670 GET_STR16(be, avio_rb16)
671 
672 #undef GET_STR16
673 
674 uint64_t avio_rb64(AVIOContext *s)
675 {
676  uint64_t val;
677  val = (uint64_t)avio_rb32(s) << 32;
678  val |= (uint64_t)avio_rb32(s);
679  return val;
680 }
681 
683  uint64_t val = 0;
684  int tmp;
685 
686  do{
687  tmp = avio_r8(bc);
688  val= (val<<7) + (tmp&127);
689  }while(tmp&128);
690  return val;
691 }
692 
694 {
695  uint8_t *buffer;
696  int buffer_size, max_packet_size;
697 
698  max_packet_size = h->max_packet_size;
699  if (max_packet_size) {
700  buffer_size = max_packet_size; /* no need to bufferize more than one packet */
701  } else {
702  buffer_size = IO_BUFFER_SIZE;
703  }
704  buffer = av_malloc(buffer_size);
705  if (!buffer)
706  return AVERROR(ENOMEM);
707 
708  *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
709  (void*)ffurl_read, (void*)ffurl_write, (void*)ffurl_seek);
710  if (!*s) {
711  av_free(buffer);
712  return AVERROR(ENOMEM);
713  }
714  (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
715  (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
716  (*s)->max_packet_size = max_packet_size;
717  if(h->prot) {
718  (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
719  (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
720  }
721  (*s)->av_class = &ffio_url_class;
722  return 0;
723 }
724 
725 int ffio_set_buf_size(AVIOContext *s, int buf_size)
726 {
727  uint8_t *buffer;
728  buffer = av_malloc(buf_size);
729  if (!buffer)
730  return AVERROR(ENOMEM);
731 
732  av_free(s->buffer);
733  s->buffer = buffer;
734  s->buffer_size = buf_size;
735  s->buf_ptr = buffer;
737  return 0;
738 }
739 
740 static int url_resetbuf(AVIOContext *s, int flags)
741 {
742  av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
743 
744  if (flags & AVIO_FLAG_WRITE) {
745  s->buf_end = s->buffer + s->buffer_size;
746  s->write_flag = 1;
747  } else {
748  s->buf_end = s->buffer;
749  s->write_flag = 0;
750  }
751  return 0;
752 }
753 
754 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
755 {
756  int64_t buffer_start;
757  int buffer_size;
758  int overlap, new_size, alloc_size;
759  uint8_t *buf = *bufp;
760 
761  if (s->write_flag) {
762  av_freep(bufp);
763  return AVERROR(EINVAL);
764  }
765 
766  buffer_size = s->buf_end - s->buffer;
767 
768  /* the buffers must touch or overlap */
769  if ((buffer_start = s->pos - buffer_size) > buf_size) {
770  av_freep(bufp);
771  return AVERROR(EINVAL);
772  }
773 
774  overlap = buf_size - buffer_start;
775  new_size = buf_size + buffer_size - overlap;
776 
777  alloc_size = FFMAX(s->buffer_size, new_size);
778  if (alloc_size > buf_size)
779  if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
780  return AVERROR(ENOMEM);
781 
782  if (new_size > buf_size) {
783  memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
784  buf_size = new_size;
785  }
786 
787  av_free(s->buffer);
788  s->buf_ptr = s->buffer = buf;
789  s->buffer_size = alloc_size;
790  s->pos = buf_size;
791  s->buf_end = s->buf_ptr + buf_size;
792  s->eof_reached = 0;
793  s->must_flush = 0;
794 
795  return 0;
796 }
797 
798 int avio_open(AVIOContext **s, const char *filename, int flags)
799 {
800  return avio_open2(s, filename, flags, NULL, NULL);
801 }
802 
803 int avio_open2(AVIOContext **s, const char *filename, int flags,
805 {
806  URLContext *h;
807  int err;
808 
809  err = ffurl_open(&h, filename, flags, int_cb, options);
810  if (err < 0)
811  return err;
812  err = ffio_fdopen(s, h);
813  if (err < 0) {
814  ffurl_close(h);
815  return err;
816  }
817  return 0;
818 }
819 
821 {
822  URLContext *h;
823 
824  if (!s)
825  return 0;
826 
827  avio_flush(s);
828  h = s->opaque;
829  av_freep(&s->buffer);
830  if (!s->write_flag)
831  av_log(s, AV_LOG_DEBUG, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
832  av_free(s);
833  return ffurl_close(h);
834 }
835 
837 {
838  int ret = avio_close(*s);
839  *s = NULL;
840  return ret;
841 }
842 
843 int avio_printf(AVIOContext *s, const char *fmt, ...)
844 {
845  va_list ap;
846  char buf[4096];
847  int ret;
848 
849  va_start(ap, fmt);
850  ret = vsnprintf(buf, sizeof(buf), fmt, ap);
851  va_end(ap);
852  avio_write(s, buf, strlen(buf));
853  return ret;
854 }
855 
856 int avio_pause(AVIOContext *s, int pause)
857 {
858  if (!s->read_pause)
859  return AVERROR(ENOSYS);
860  return s->read_pause(s->opaque, pause);
861 }
862 
863 int64_t avio_seek_time(AVIOContext *s, int stream_index,
864  int64_t timestamp, int flags)
865 {
866  URLContext *h = s->opaque;
867  int64_t ret;
868  if (!s->read_seek)
869  return AVERROR(ENOSYS);
870  ret = s->read_seek(h, stream_index, timestamp, flags);
871  if (ret >= 0) {
872  int64_t pos;
873  s->buf_ptr = s->buf_end; // Flush buffer
874  pos = s->seek(h, 0, SEEK_CUR);
875  if (pos >= 0)
876  s->pos = pos;
877  else if (pos != AVERROR(ENOSYS))
878  ret = pos;
879  }
880  return ret;
881 }
882 
883 /* output in a dynamic buffer */
884 
885 typedef struct DynBuffer {
890 } DynBuffer;
891 
892 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
893 {
894  DynBuffer *d = opaque;
895  unsigned new_size, new_allocated_size;
896 
897  /* reallocate buffer if needed */
898  new_size = d->pos + buf_size;
899  new_allocated_size = d->allocated_size;
900  if (new_size < d->pos || new_size > INT_MAX/2)
901  return -1;
902  while (new_size > new_allocated_size) {
903  if (!new_allocated_size)
904  new_allocated_size = new_size;
905  else
906  new_allocated_size += new_allocated_size / 2 + 1;
907  }
908 
909  if (new_allocated_size > d->allocated_size) {
910  d->buffer = av_realloc_f(d->buffer, 1, new_allocated_size);
911  if(d->buffer == NULL)
912  return AVERROR(ENOMEM);
913  d->allocated_size = new_allocated_size;
914  }
915  memcpy(d->buffer + d->pos, buf, buf_size);
916  d->pos = new_size;
917  if (d->pos > d->size)
918  d->size = d->pos;
919  return buf_size;
920 }
921 
922 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
923 {
924  unsigned char buf1[4];
925  int ret;
926 
927  /* packetized write: output the header */
928  AV_WB32(buf1, buf_size);
929  ret = dyn_buf_write(opaque, buf1, 4);
930  if (ret < 0)
931  return ret;
932 
933  /* then the data */
934  return dyn_buf_write(opaque, buf, buf_size);
935 }
936 
937 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
938 {
939  DynBuffer *d = opaque;
940 
941  if (whence == SEEK_CUR)
942  offset += d->pos;
943  else if (whence == SEEK_END)
944  offset += d->size;
945  if (offset < 0 || offset > 0x7fffffffLL)
946  return -1;
947  d->pos = offset;
948  return 0;
949 }
950 
951 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
952 {
953  DynBuffer *d;
954  unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
955 
956  if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
957  return -1;
958  d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
959  if (!d)
960  return AVERROR(ENOMEM);
961  d->io_buffer_size = io_buffer_size;
962  *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
963  max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
964  max_packet_size ? NULL : dyn_buf_seek);
965  if(!*s) {
966  av_free(d);
967  return AVERROR(ENOMEM);
968  }
969  (*s)->max_packet_size = max_packet_size;
970  return 0;
971 }
972 
974 {
975  return url_open_dyn_buf_internal(s, 0);
976 }
977 
978 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
979 {
980  if (max_packet_size <= 0)
981  return -1;
982  return url_open_dyn_buf_internal(s, max_packet_size);
983 }
984 
986 {
987  DynBuffer *d = s->opaque;
988  int size;
989  static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
990  int padding = 0;
991 
992  /* don't attempt to pad fixed-size packet buffers */
993  if (!s->max_packet_size) {
994  avio_write(s, padbuf, sizeof(padbuf));
996  }
997 
998  avio_flush(s);
999 
1000  *pbuffer = d->buffer;
1001  size = d->size;
1002  av_free(d);
1003  av_free(s);
1004  return size - padding;
1005 }