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