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