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