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