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