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