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