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;
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_partial(AVIOContext *s, unsigned char *buf, int size)
529 {
530  int len;
531 
532  if (size < 0)
533  return -1;
534 
535  if (s->read_packet && s->write_flag) {
536  len = s->read_packet(s->opaque, buf, size);
537  if (len > 0)
538  s->pos += len;
539  return len;
540  }
541 
542  len = s->buf_end - s->buf_ptr;
543  if (len == 0) {
544  /* Reset the buf_end pointer to the start of the buffer, to make sure
545  * the fill_buffer call tries to read as much data as fits into the
546  * full buffer, instead of just what space is left after buf_end.
547  * This avoids returning partial packets at the end of the buffer,
548  * for packet based inputs.
549  */
550  s->buf_end = s->buf_ptr = s->buffer;
551  fill_buffer(s);
552  len = s->buf_end - s->buf_ptr;
553  }
554  if (len > size)
555  len = size;
556  memcpy(buf, s->buf_ptr, len);
557  s->buf_ptr += len;
558  if (!len) {
559  if (s->error) return s->error;
560  if (url_feof(s)) return AVERROR_EOF;
561  }
562  return len;
563 }
564 
565 unsigned int avio_rl16(AVIOContext *s)
566 {
567  unsigned int val;
568  val = avio_r8(s);
569  val |= avio_r8(s) << 8;
570  return val;
571 }
572 
573 unsigned int avio_rl24(AVIOContext *s)
574 {
575  unsigned int val;
576  val = avio_rl16(s);
577  val |= avio_r8(s) << 16;
578  return val;
579 }
580 
581 unsigned int avio_rl32(AVIOContext *s)
582 {
583  unsigned int val;
584  val = avio_rl16(s);
585  val |= avio_rl16(s) << 16;
586  return val;
587 }
588 
589 uint64_t avio_rl64(AVIOContext *s)
590 {
591  uint64_t val;
592  val = (uint64_t)avio_rl32(s);
593  val |= (uint64_t)avio_rl32(s) << 32;
594  return val;
595 }
596 
597 unsigned int avio_rb16(AVIOContext *s)
598 {
599  unsigned int val;
600  val = avio_r8(s) << 8;
601  val |= avio_r8(s);
602  return val;
603 }
604 
605 unsigned int avio_rb24(AVIOContext *s)
606 {
607  unsigned int val;
608  val = avio_rb16(s) << 8;
609  val |= avio_r8(s);
610  return val;
611 }
612 unsigned int avio_rb32(AVIOContext *s)
613 {
614  unsigned int val;
615  val = avio_rb16(s) << 16;
616  val |= avio_rb16(s);
617  return val;
618 }
619 
620 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
621 {
622  int i = 0;
623  char c;
624 
625  do {
626  c = avio_r8(s);
627  if (c && i < maxlen-1)
628  buf[i++] = c;
629  } while (c != '\n' && c);
630 
631  buf[i] = 0;
632  return i;
633 }
634 
635 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
636 {
637  int i;
638 
639  if (buflen <= 0)
640  return AVERROR(EINVAL);
641  // reserve 1 byte for terminating 0
642  buflen = FFMIN(buflen - 1, maxlen);
643  for (i = 0; i < buflen; i++)
644  if (!(buf[i] = avio_r8(s)))
645  return i + 1;
646  buf[i] = 0;
647  for (; i < maxlen; i++)
648  if (!avio_r8(s))
649  return i + 1;
650  return maxlen;
651 }
652 
653 #define GET_STR16(type, read) \
654  int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
655 {\
656  char* q = buf;\
657  int ret = 0;\
658  if (buflen <= 0) \
659  return AVERROR(EINVAL); \
660  while (ret + 1 < maxlen) {\
661  uint8_t tmp;\
662  uint32_t ch;\
663  GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
664  if (!ch)\
665  break;\
666  PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
667  }\
668  *q = 0;\
669  return ret;\
670 }\
671 
673 GET_STR16(be, avio_rb16)
674 
675 #undef GET_STR16
676 
677 uint64_t avio_rb64(AVIOContext *s)
678 {
679  uint64_t val;
680  val = (uint64_t)avio_rb32(s) << 32;
681  val |= (uint64_t)avio_rb32(s);
682  return val;
683 }
684 
686  uint64_t val = 0;
687  int tmp;
688 
689  do{
690  tmp = avio_r8(bc);
691  val= (val<<7) + (tmp&127);
692  }while(tmp&128);
693  return val;
694 }
695 
697 {
698  uint8_t *buffer;
699  int buffer_size, max_packet_size;
700 
701  max_packet_size = h->max_packet_size;
702  if (max_packet_size) {
703  buffer_size = max_packet_size; /* no need to bufferize more than one packet */
704  } else {
705  buffer_size = IO_BUFFER_SIZE;
706  }
707  buffer = av_malloc(buffer_size);
708  if (!buffer)
709  return AVERROR(ENOMEM);
710 
711  *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
712  (void*)ffurl_read, (void*)ffurl_write, (void*)ffurl_seek);
713  if (!*s) {
714  av_free(buffer);
715  return AVERROR(ENOMEM);
716  }
717  (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
718  (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
719  (*s)->max_packet_size = max_packet_size;
720  if(h->prot) {
721  (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
722  (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
723  }
724  (*s)->av_class = &ffio_url_class;
725  return 0;
726 }
727 
728 int ffio_ensure_seekback(AVIOContext *s, int buf_size)
729 {
730  uint8_t *buffer;
731  int max_buffer_size = s->max_packet_size ?
733 
734  buf_size += s->buf_ptr - s->buffer + max_buffer_size;
735 
736  if (buf_size < s->buffer_size || s->seekable)
737  return 0;
738  av_assert0(!s->write_flag);
739 
740  buffer = av_malloc(buf_size);
741  if (!buffer)
742  return AVERROR(ENOMEM);
743 
744  memcpy(buffer, s->buffer, s->buffer_size);
745  av_free(s->buffer);
746  s->buf_ptr = buffer + (s->buf_ptr - s->buffer);
747  s->buf_end = buffer + (s->buf_end - s->buffer);
748  s->buffer = buffer;
749  s->buffer_size = buf_size;
750  return 0;
751 }
752 
753 int ffio_set_buf_size(AVIOContext *s, int buf_size)
754 {
755  uint8_t *buffer;
756  buffer = av_malloc(buf_size);
757  if (!buffer)
758  return AVERROR(ENOMEM);
759 
760  av_free(s->buffer);
761  s->buffer = buffer;
762  s->buffer_size = buf_size;
763  s->buf_ptr = buffer;
765  return 0;
766 }
767 
768 static int url_resetbuf(AVIOContext *s, int flags)
769 {
770  av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
771 
772  if (flags & AVIO_FLAG_WRITE) {
773  s->buf_end = s->buffer + s->buffer_size;
774  s->write_flag = 1;
775  } else {
776  s->buf_end = s->buffer;
777  s->write_flag = 0;
778  }
779  return 0;
780 }
781 
782 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
783 {
784  int64_t buffer_start;
785  int buffer_size;
786  int overlap, new_size, alloc_size;
787  uint8_t *buf = *bufp;
788 
789  if (s->write_flag) {
790  av_freep(bufp);
791  return AVERROR(EINVAL);
792  }
793 
794  buffer_size = s->buf_end - s->buffer;
795 
796  /* the buffers must touch or overlap */
797  if ((buffer_start = s->pos - buffer_size) > buf_size) {
798  av_freep(bufp);
799  return AVERROR(EINVAL);
800  }
801 
802  overlap = buf_size - buffer_start;
803  new_size = buf_size + buffer_size - overlap;
804 
805  alloc_size = FFMAX(s->buffer_size, new_size);
806  if (alloc_size > buf_size)
807  if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
808  return AVERROR(ENOMEM);
809 
810  if (new_size > buf_size) {
811  memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
812  buf_size = new_size;
813  }
814 
815  av_free(s->buffer);
816  s->buf_ptr = s->buffer = buf;
817  s->buffer_size = alloc_size;
818  s->pos = buf_size;
819  s->buf_end = s->buf_ptr + buf_size;
820  s->eof_reached = 0;
821  s->must_flush = 0;
822 
823  return 0;
824 }
825 
826 int avio_open(AVIOContext **s, const char *filename, int flags)
827 {
828  return avio_open2(s, filename, flags, NULL, NULL);
829 }
830 
831 int avio_open2(AVIOContext **s, const char *filename, int flags,
833 {
834  URLContext *h;
835  int err;
836 
837  err = ffurl_open(&h, filename, flags, int_cb, options);
838  if (err < 0)
839  return err;
840  err = ffio_fdopen(s, h);
841  if (err < 0) {
842  ffurl_close(h);
843  return err;
844  }
845  return 0;
846 }
847 
849 {
850  URLContext *h;
851 
852  if (!s)
853  return 0;
854 
855  avio_flush(s);
856  h = s->opaque;
857  av_freep(&s->buffer);
858  if (s->write_flag)
859  av_log(s, AV_LOG_DEBUG, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
860  else
861  av_log(s, AV_LOG_DEBUG, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
862  av_free(s);
863  return ffurl_close(h);
864 }
865 
867 {
868  int ret = avio_close(*s);
869  *s = NULL;
870  return ret;
871 }
872 
873 int avio_printf(AVIOContext *s, const char *fmt, ...)
874 {
875  va_list ap;
876  char buf[4096];
877  int ret;
878 
879  va_start(ap, fmt);
880  ret = vsnprintf(buf, sizeof(buf), fmt, ap);
881  va_end(ap);
882  avio_write(s, buf, strlen(buf));
883  return ret;
884 }
885 
886 int avio_pause(AVIOContext *s, int pause)
887 {
888  if (!s->read_pause)
889  return AVERROR(ENOSYS);
890  return s->read_pause(s->opaque, pause);
891 }
892 
893 int64_t avio_seek_time(AVIOContext *s, int stream_index,
894  int64_t timestamp, int flags)
895 {
896  URLContext *h = s->opaque;
897  int64_t ret;
898  if (!s->read_seek)
899  return AVERROR(ENOSYS);
900  ret = s->read_seek(h, stream_index, timestamp, flags);
901  if (ret >= 0) {
902  int64_t pos;
903  s->buf_ptr = s->buf_end; // Flush buffer
904  pos = s->seek(h, 0, SEEK_CUR);
905  if (pos >= 0)
906  s->pos = pos;
907  else if (pos != AVERROR(ENOSYS))
908  ret = pos;
909  }
910  return ret;
911 }
912 
913 /* output in a dynamic buffer */
914 
915 typedef struct DynBuffer {
920 } DynBuffer;
921 
922 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
923 {
924  DynBuffer *d = opaque;
925  unsigned new_size, new_allocated_size;
926 
927  /* reallocate buffer if needed */
928  new_size = d->pos + buf_size;
929  new_allocated_size = d->allocated_size;
930  if (new_size < d->pos || new_size > INT_MAX/2)
931  return -1;
932  while (new_size > new_allocated_size) {
933  if (!new_allocated_size)
934  new_allocated_size = new_size;
935  else
936  new_allocated_size += new_allocated_size / 2 + 1;
937  }
938 
939  if (new_allocated_size > d->allocated_size) {
940  d->buffer = av_realloc_f(d->buffer, 1, new_allocated_size);
941  if(d->buffer == NULL)
942  return AVERROR(ENOMEM);
943  d->allocated_size = new_allocated_size;
944  }
945  memcpy(d->buffer + d->pos, buf, buf_size);
946  d->pos = new_size;
947  if (d->pos > d->size)
948  d->size = d->pos;
949  return buf_size;
950 }
951 
952 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
953 {
954  unsigned char buf1[4];
955  int ret;
956 
957  /* packetized write: output the header */
958  AV_WB32(buf1, buf_size);
959  ret = dyn_buf_write(opaque, buf1, 4);
960  if (ret < 0)
961  return ret;
962 
963  /* then the data */
964  return dyn_buf_write(opaque, buf, buf_size);
965 }
966 
967 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
968 {
969  DynBuffer *d = opaque;
970 
971  if (whence == SEEK_CUR)
972  offset += d->pos;
973  else if (whence == SEEK_END)
974  offset += d->size;
975  if (offset < 0 || offset > 0x7fffffffLL)
976  return -1;
977  d->pos = offset;
978  return 0;
979 }
980 
981 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
982 {
983  DynBuffer *d;
984  unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
985 
986  if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
987  return -1;
988  d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
989  if (!d)
990  return AVERROR(ENOMEM);
991  d->io_buffer_size = io_buffer_size;
992  *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
993  max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
994  max_packet_size ? NULL : dyn_buf_seek);
995  if(!*s) {
996  av_free(d);
997  return AVERROR(ENOMEM);
998  }
999  (*s)->max_packet_size = max_packet_size;
1000  return 0;
1001 }
1002 
1004 {
1005  return url_open_dyn_buf_internal(s, 0);
1006 }
1007 
1008 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1009 {
1010  if (max_packet_size <= 0)
1011  return -1;
1012  return url_open_dyn_buf_internal(s, max_packet_size);
1013 }
1014 
1016 {
1017  DynBuffer *d = s->opaque;
1018  int size;
1019  static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1020  int padding = 0;
1021 
1022  /* don't attempt to pad fixed-size packet buffers */
1023  if (!s->max_packet_size) {
1024  avio_write(s, padbuf, sizeof(padbuf));
1025  padding = FF_INPUT_BUFFER_PADDING_SIZE;
1026  }
1027 
1028  avio_flush(s);
1029 
1030  *pbuffer = d->buffer;
1031  size = d->size;
1032  av_free(d);
1033  av_free(s);
1034  return size - padding;
1035 }