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