Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "avformat.h"
28 #include "avio.h"
29 #include "avio_internal.h"
30 #include "internal.h"
31 #include "url.h"
32 #include <stdarg.h>
33 
34 #define IO_BUFFER_SIZE 32768
35 
41 #define SHORT_SEEK_THRESHOLD 4096
42 
43 static void *ffio_url_child_next(void *obj, void *prev)
44 {
45  AVIOContext *s = obj;
46  return prev ? NULL : s->opaque;
47 }
48 
49 static const AVClass *ffio_url_child_class_next(const AVClass *prev)
50 {
51  return prev ? NULL : &ffurl_context_class;
52 }
53 
54 static const AVOption ffio_url_options[] = {
55  { NULL },
56 };
57 
59  .class_name = "AVIOContext",
60  .item_name = av_default_item_name,
61  .version = LIBAVUTIL_VERSION_INT,
62  .option = ffio_url_options,
63  .child_next = ffio_url_child_next,
64  .child_class_next = ffio_url_child_class_next,
65 };
66 
67 static void fill_buffer(AVIOContext *s);
68 static int url_resetbuf(AVIOContext *s, int flags);
69 
71  unsigned char *buffer,
72  int buffer_size,
73  int write_flag,
74  void *opaque,
75  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
76  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
77  int64_t (*seek)(void *opaque, int64_t offset, int whence))
78 {
79  s->buffer = buffer;
80  s->buffer_size = buffer_size;
81  s->buf_ptr = buffer;
82  s->opaque = opaque;
83 
84  url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
85 
88  s->seek = seek;
89  s->pos = 0;
90  s->must_flush = 0;
91  s->eof_reached = 0;
92  s->error = 0;
93  s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
94  s->max_packet_size = 0;
95  s->update_checksum = NULL;
96 
97  if (!read_packet && !write_flag) {
98  s->pos = buffer_size;
99  s->buf_end = s->buffer + buffer_size;
100  }
101  s->read_pause = NULL;
102  s->read_seek = NULL;
103 
104  return 0;
105 }
106 
108  unsigned char *buffer,
109  int buffer_size,
110  int write_flag,
111  void *opaque,
112  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
113  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
114  int64_t (*seek)(void *opaque, int64_t offset, int whence))
115 {
116  AVIOContext *s = av_mallocz(sizeof(AVIOContext));
117  if (!s)
118  return NULL;
119  ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
120  read_packet, write_packet, seek);
121  return s;
122 }
123 
124 static void flush_buffer(AVIOContext *s)
125 {
126  if (s->buf_ptr > s->buffer) {
127  if (s->write_packet && !s->error) {
128  int ret = s->write_packet(s->opaque, s->buffer,
129  s->buf_ptr - s->buffer);
130  if (ret < 0) {
131  s->error = ret;
132  }
133  }
134  if (s->update_checksum) {
136  s->buf_ptr - s->checksum_ptr);
137  s->checksum_ptr = s->buffer;
138  }
139  s->pos += s->buf_ptr - s->buffer;
140  }
141  s->buf_ptr = s->buffer;
142 }
143 
144 void avio_w8(AVIOContext *s, int b)
145 {
146  *s->buf_ptr++ = b;
147  if (s->buf_ptr >= s->buf_end)
148  flush_buffer(s);
149 }
150 
151 void ffio_fill(AVIOContext *s, int b, int count)
152 {
153  while (count > 0) {
154  int len = FFMIN(s->buf_end - s->buf_ptr, count);
155  memset(s->buf_ptr, b, len);
156  s->buf_ptr += len;
157 
158  if (s->buf_ptr >= s->buf_end)
159  flush_buffer(s);
160 
161  count -= len;
162  }
163 }
164 
165 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
166 {
167  while (size > 0) {
168  int len = FFMIN(s->buf_end - s->buf_ptr, size);
169  memcpy(s->buf_ptr, buf, len);
170  s->buf_ptr += len;
171 
172  if (s->buf_ptr >= s->buf_end)
173  flush_buffer(s);
174 
175  buf += len;
176  size -= len;
177  }
178 }
179 
181 {
182  flush_buffer(s);
183  s->must_flush = 0;
184 }
185 
186 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
187 {
188  int64_t offset1;
189  int64_t pos;
190  int force = whence & AVSEEK_FORCE;
191  whence &= ~AVSEEK_FORCE;
192 
193  if(!s)
194  return AVERROR(EINVAL);
195 
196  pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
197 
198  if (whence != SEEK_CUR && whence != SEEK_SET)
199  return AVERROR(EINVAL);
200 
201  if (whence == SEEK_CUR) {
202  offset1 = pos + (s->buf_ptr - s->buffer);
203  if (offset == 0)
204  return offset1;
205  offset += offset1;
206  }
207  offset1 = offset - pos;
208  if (!s->must_flush &&
209  offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
210  /* can do the seek inside the buffer */
211  s->buf_ptr = s->buffer + offset1;
212  } else if ((!s->seekable ||
213  offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
214  !s->write_flag && offset1 >= 0 &&
215  (whence != SEEK_END || force)) {
216  while(s->pos < offset && !s->eof_reached)
217  fill_buffer(s);
218  if (s->eof_reached)
219  return AVERROR_EOF;
220  s->buf_ptr = s->buf_end + offset - s->pos;
221  } else {
222  int64_t res;
223 
224  if (s->write_flag) {
225  flush_buffer(s);
226  s->must_flush = 1;
227  }
228  if (!s->seek)
229  return AVERROR(EPIPE);
230  if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
231  return res;
232  if (!s->write_flag)
233  s->buf_end = s->buffer;
234  s->buf_ptr = s->buffer;
235  s->pos = offset;
236  }
237  s->eof_reached = 0;
238  return offset;
239 }
240 
242 {
243  int64_t size;
244 
245  if (!s)
246  return AVERROR(EINVAL);
247 
248  if (!s->seek)
249  return AVERROR(ENOSYS);
250  size = s->seek(s->opaque, 0, AVSEEK_SIZE);
251  if (size < 0) {
252  if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
253  return size;
254  size++;
255  s->seek(s->opaque, s->pos, SEEK_SET);
256  }
257  return size;
258 }
259 
261 {
262  if(!s)
263  return 0;
264  if(s->eof_reached){
265  s->eof_reached=0;
266  fill_buffer(s);
267  }
268  return s->eof_reached;
269 }
270 
271 void avio_wl32(AVIOContext *s, unsigned int val)
272 {
273  avio_w8(s, val);
274  avio_w8(s, val >> 8);
275  avio_w8(s, val >> 16);
276  avio_w8(s, val >> 24);
277 }
278 
279 void avio_wb32(AVIOContext *s, unsigned int val)
280 {
281  avio_w8(s, val >> 24);
282  avio_w8(s, val >> 16);
283  avio_w8(s, val >> 8);
284  avio_w8(s, val);
285 }
286 
287 int avio_put_str(AVIOContext *s, const char *str)
288 {
289  int len = 1;
290  if (str) {
291  len += strlen(str);
292  avio_write(s, (const unsigned char *) str, len);
293  } else
294  avio_w8(s, 0);
295  return len;
296 }
297 
298 int avio_put_str16le(AVIOContext *s, const char *str)
299 {
300  const uint8_t *q = str;
301  int ret = 0;
302 
303  while (*q) {
304  uint32_t ch;
305  uint16_t tmp;
306 
307  GET_UTF8(ch, *q++, break;)
308  PUT_UTF16(ch, tmp, avio_wl16(s, tmp); ret += 2;)
309  }
310  avio_wl16(s, 0);
311  ret += 2;
312  return ret;
313 }
314 
315 int ff_get_v_length(uint64_t val)
316 {
317  int i = 1;
318 
319  while (val >>= 7)
320  i++;
321 
322  return i;
323 }
324 
325 void ff_put_v(AVIOContext *bc, uint64_t val)
326 {
327  int i = ff_get_v_length(val);
328 
329  while (--i > 0)
330  avio_w8(bc, 128 | (val >> (7 * i)));
331 
332  avio_w8(bc, val & 127);
333 }
334 
335 void avio_wl64(AVIOContext *s, uint64_t val)
336 {
337  avio_wl32(s, (uint32_t)(val & 0xffffffff));
338  avio_wl32(s, (uint32_t)(val >> 32));
339 }
340 
341 void avio_wb64(AVIOContext *s, uint64_t val)
342 {
343  avio_wb32(s, (uint32_t)(val >> 32));
344  avio_wb32(s, (uint32_t)(val & 0xffffffff));
345 }
346 
347 void avio_wl16(AVIOContext *s, unsigned int val)
348 {
349  avio_w8(s, val);
350  avio_w8(s, val >> 8);
351 }
352 
353 void avio_wb16(AVIOContext *s, unsigned int val)
354 {
355  avio_w8(s, val >> 8);
356  avio_w8(s, val);
357 }
358 
359 void avio_wl24(AVIOContext *s, unsigned int val)
360 {
361  avio_wl16(s, val & 0xffff);
362  avio_w8(s, val >> 16);
363 }
364 
365 void avio_wb24(AVIOContext *s, unsigned int val)
366 {
367  avio_wb16(s, val >> 8);
368  avio_w8(s, val);
369 }
370 
371 /* Input stream */
372 
373 static void fill_buffer(AVIOContext *s)
374 {
375  uint8_t *dst = !s->max_packet_size &&
376  s->buf_end - s->buffer < s->buffer_size ?
377  s->buf_end : s->buffer;
378  int len = s->buffer_size - (dst - s->buffer);
379  int max_buffer_size = s->max_packet_size ?
381 
382  /* can't fill the buffer without read_packet, just set EOF if appropriate */
383  if (!s->read_packet && s->buf_ptr >= s->buf_end)
384  s->eof_reached = 1;
385 
386  /* no need to do anything if EOF already reached */
387  if (s->eof_reached)
388  return;
389 
390  if (s->update_checksum && dst == s->buffer) {
391  if (s->buf_end > s->checksum_ptr)
393  s->buf_end - s->checksum_ptr);
394  s->checksum_ptr = s->buffer;
395  }
396 
397  /* make buffer smaller in case it ended up large after probing */
398  if (s->buffer_size > max_buffer_size) {
399  ffio_set_buf_size(s, max_buffer_size);
400 
401  s->checksum_ptr = dst = s->buffer;
402  len = s->buffer_size;
403  }
404 
405  if (s->read_packet)
406  len = s->read_packet(s->opaque, dst, len);
407  else
408  len = 0;
409  if (len <= 0) {
410  /* do not modify buffer if EOF reached so that a seek back can
411  be done without rereading data */
412  s->eof_reached = 1;
413  if (len < 0)
414  s->error = len;
415  } else {
416  s->pos += len;
417  s->buf_ptr = dst;
418  s->buf_end = dst + len;
419  }
420 }
421 
422 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
423  unsigned int len)
424 {
425  return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
426 }
427 
428 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
429  unsigned int len)
430 {
431  return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
432 }
433 
435 {
437  s->buf_ptr - s->checksum_ptr);
438  s->update_checksum = NULL;
439  return s->checksum;
440 }
441 
443  unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
444  unsigned long checksum)
445 {
446  s->update_checksum = update_checksum;
447  if (s->update_checksum) {
448  s->checksum = checksum;
449  s->checksum_ptr = s->buf_ptr;
450  }
451 }
452 
453 /* XXX: put an inline version */
455 {
456  if (s->buf_ptr >= s->buf_end)
457  fill_buffer(s);
458  if (s->buf_ptr < s->buf_end)
459  return *s->buf_ptr++;
460  return 0;
461 }
462 
463 int avio_read(AVIOContext *s, unsigned char *buf, int size)
464 {
465  int len, size1;
466 
467  size1 = size;
468  while (size > 0) {
469  len = s->buf_end - s->buf_ptr;
470  if (len > size)
471  len = size;
472  if (len == 0 || s->write_flag) {
473  if(size > s->buffer_size && !s->update_checksum){
474  if(s->read_packet)
475  len = s->read_packet(s->opaque, buf, size);
476  if (len <= 0) {
477  /* do not modify buffer if EOF reached so that a seek back can
478  be done without rereading data */
479  s->eof_reached = 1;
480  if(len<0)
481  s->error= len;
482  break;
483  } else {
484  s->pos += len;
485  size -= len;
486  buf += len;
487  s->buf_ptr = s->buffer;
488  s->buf_end = s->buffer/* + len*/;
489  }
490  } else {
491  fill_buffer(s);
492  len = s->buf_end - s->buf_ptr;
493  if (len == 0)
494  break;
495  }
496  } else {
497  memcpy(buf, s->buf_ptr, len);
498  buf += len;
499  s->buf_ptr += len;
500  size -= len;
501  }
502  }
503  if (size1 == size) {
504  if (s->error) return s->error;
505  if (s->eof_reached) return AVERROR_EOF;
506  }
507  return size1 - size;
508 }
509 
510 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
511 {
512  if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
513  *data = s->buf_ptr;
514  s->buf_ptr += size;
515  return size;
516  } else {
517  *data = buf;
518  return avio_read(s, buf, size);
519  }
520 }
521 
522 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
523 {
524  int len;
525 
526  if (size < 0)
527  return -1;
528 
529  if (s->read_packet && s->write_flag) {
530  len = s->read_packet(s->opaque, buf, size);
531  if (len > 0)
532  s->pos += len;
533  return len;
534  }
535 
536  len = s->buf_end - s->buf_ptr;
537  if (len == 0) {
538  /* Reset the buf_end pointer to the start of the buffer, to make sure
539  * the fill_buffer call tries to read as much data as fits into the
540  * full buffer, instead of just what space is left after buf_end.
541  * This avoids returning partial packets at the end of the buffer,
542  * for packet based inputs.
543  */
544  s->buf_end = s->buf_ptr = s->buffer;
545  fill_buffer(s);
546  len = s->buf_end - s->buf_ptr;
547  }
548  if (len > size)
549  len = size;
550  memcpy(buf, s->buf_ptr, len);
551  s->buf_ptr += len;
552  if (!len) {
553  if (s->error) return s->error;
554  if (s->eof_reached) return AVERROR_EOF;
555  }
556  return len;
557 }
558 
559 unsigned int avio_rl16(AVIOContext *s)
560 {
561  unsigned int val;
562  val = avio_r8(s);
563  val |= avio_r8(s) << 8;
564  return val;
565 }
566 
567 unsigned int avio_rl24(AVIOContext *s)
568 {
569  unsigned int val;
570  val = avio_rl16(s);
571  val |= avio_r8(s) << 16;
572  return val;
573 }
574 
575 unsigned int avio_rl32(AVIOContext *s)
576 {
577  unsigned int val;
578  val = avio_rl16(s);
579  val |= avio_rl16(s) << 16;
580  return val;
581 }
582 
583 uint64_t avio_rl64(AVIOContext *s)
584 {
585  uint64_t val;
586  val = (uint64_t)avio_rl32(s);
587  val |= (uint64_t)avio_rl32(s) << 32;
588  return val;
589 }
590 
591 unsigned int avio_rb16(AVIOContext *s)
592 {
593  unsigned int val;
594  val = avio_r8(s) << 8;
595  val |= avio_r8(s);
596  return val;
597 }
598 
599 unsigned int avio_rb24(AVIOContext *s)
600 {
601  unsigned int val;
602  val = avio_rb16(s) << 8;
603  val |= avio_r8(s);
604  return val;
605 }
606 unsigned int avio_rb32(AVIOContext *s)
607 {
608  unsigned int val;
609  val = avio_rb16(s) << 16;
610  val |= avio_rb16(s);
611  return val;
612 }
613 
614 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
615 {
616  int i = 0;
617  char c;
618 
619  do {
620  c = avio_r8(s);
621  if (c && i < maxlen-1)
622  buf[i++] = c;
623  } while (c != '\n' && c);
624 
625  buf[i] = 0;
626  return i;
627 }
628 
629 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
630 {
631  int i;
632 
633  if (buflen <= 0)
634  return AVERROR(EINVAL);
635  // reserve 1 byte for terminating 0
636  buflen = FFMIN(buflen - 1, maxlen);
637  for (i = 0; i < buflen; i++)
638  if (!(buf[i] = avio_r8(s)))
639  return i + 1;
640  buf[i] = 0;
641  for (; i < maxlen; i++)
642  if (!avio_r8(s))
643  return i + 1;
644  return maxlen;
645 }
646 
647 #define GET_STR16(type, read) \
648  int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
649 {\
650  char* q = buf;\
651  int ret = 0;\
652  if (buflen <= 0) \
653  return AVERROR(EINVAL); \
654  while (ret + 1 < maxlen) {\
655  uint8_t tmp;\
656  uint32_t ch;\
657  GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
658  if (!ch)\
659  break;\
660  PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
661  }\
662  *q = 0;\
663  return ret;\
664 }\
665 
667 GET_STR16(be, avio_rb16)
668 
669 #undef GET_STR16
670 
671 uint64_t avio_rb64(AVIOContext *s)
672 {
673  uint64_t val;
674  val = (uint64_t)avio_rb32(s) << 32;
675  val |= (uint64_t)avio_rb32(s);
676  return val;
677 }
678 
680  uint64_t val = 0;
681  int tmp;
682 
683  do{
684  tmp = avio_r8(bc);
685  val= (val<<7) + (tmp&127);
686  }while(tmp&128);
687  return val;
688 }
689 
691 {
692  uint8_t *buffer;
693  int buffer_size, max_packet_size;
694 
695  max_packet_size = h->max_packet_size;
696  if (max_packet_size) {
697  buffer_size = max_packet_size; /* no need to bufferize more than one packet */
698  } else {
699  buffer_size = IO_BUFFER_SIZE;
700  }
701  buffer = av_malloc(buffer_size);
702  if (!buffer)
703  return AVERROR(ENOMEM);
704 
705  *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
707  if (!*s) {
708  av_free(buffer);
709  return AVERROR(ENOMEM);
710  }
711  (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
712  (*s)->max_packet_size = max_packet_size;
713  if(h->prot) {
714  (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
715  (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
716  }
717  (*s)->av_class = &ffio_url_class;
718  return 0;
719 }
720 
721 int ffio_set_buf_size(AVIOContext *s, int buf_size)
722 {
723  uint8_t *buffer;
724  buffer = av_malloc(buf_size);
725  if (!buffer)
726  return AVERROR(ENOMEM);
727 
728  av_free(s->buffer);
729  s->buffer = buffer;
730  s->buffer_size = buf_size;
731  s->buf_ptr = buffer;
733  return 0;
734 }
735 
736 static int url_resetbuf(AVIOContext *s, int flags)
737 {
738  assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
739 
740  if (flags & AVIO_FLAG_WRITE) {
741  s->buf_end = s->buffer + s->buffer_size;
742  s->write_flag = 1;
743  } else {
744  s->buf_end = s->buffer;
745  s->write_flag = 0;
746  }
747  return 0;
748 }
749 
750 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
751 {
752  int64_t buffer_start;
753  int buffer_size;
754  int overlap, new_size, alloc_size;
755 
756  if (s->write_flag)
757  return AVERROR(EINVAL);
758 
759  buffer_size = s->buf_end - s->buffer;
760 
761  /* the buffers must touch or overlap */
762  if ((buffer_start = s->pos - buffer_size) > buf_size)
763  return AVERROR(EINVAL);
764 
765  overlap = buf_size - buffer_start;
766  new_size = buf_size + buffer_size - overlap;
767 
768  alloc_size = FFMAX(s->buffer_size, new_size);
769  if (alloc_size > buf_size)
770  if (!(buf = av_realloc(buf, alloc_size)))
771  return AVERROR(ENOMEM);
772 
773  if (new_size > buf_size) {
774  memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
775  buf_size = new_size;
776  }
777 
778  av_free(s->buffer);
779  s->buf_ptr = s->buffer = buf;
780  s->buffer_size = alloc_size;
781  s->pos = buf_size;
782  s->buf_end = s->buf_ptr + buf_size;
783  s->eof_reached = 0;
784  s->must_flush = 0;
785 
786  return 0;
787 }
788 
789 int avio_open(AVIOContext **s, const char *filename, int flags)
790 {
791  return avio_open2(s, filename, flags, NULL, NULL);
792 }
793 
794 int avio_open2(AVIOContext **s, const char *filename, int flags,
796 {
797  URLContext *h;
798  int err;
799 
800  err = ffurl_open(&h, filename, flags, int_cb, options);
801  if (err < 0)
802  return err;
803  err = ffio_fdopen(s, h);
804  if (err < 0) {
805  ffurl_close(h);
806  return err;
807  }
808  return 0;
809 }
810 
812 {
813  URLContext *h;
814 
815  if (!s)
816  return 0;
817 
818  avio_flush(s);
819  h = s->opaque;
820  av_freep(&s->buffer);
821  av_free(s);
822  return ffurl_close(h);
823 }
824 
826 {
827  int ret = avio_close(*s);
828  *s = NULL;
829  return ret;
830 }
831 
832 int avio_printf(AVIOContext *s, const char *fmt, ...)
833 {
834  va_list ap;
835  char buf[4096];
836  int ret;
837 
838  va_start(ap, fmt);
839  ret = vsnprintf(buf, sizeof(buf), fmt, ap);
840  va_end(ap);
841  avio_write(s, buf, strlen(buf));
842  return ret;
843 }
844 
845 int avio_pause(AVIOContext *s, int pause)
846 {
847  if (!s->read_pause)
848  return AVERROR(ENOSYS);
849  return s->read_pause(s->opaque, pause);
850 }
851 
852 int64_t avio_seek_time(AVIOContext *s, int stream_index,
853  int64_t timestamp, int flags)
854 {
855  URLContext *h = s->opaque;
856  int64_t ret;
857  if (!s->read_seek)
858  return AVERROR(ENOSYS);
859  ret = s->read_seek(h, stream_index, timestamp, flags);
860  if (ret >= 0) {
861  int64_t pos;
862  s->buf_ptr = s->buf_end; // Flush buffer
863  pos = s->seek(h, 0, SEEK_CUR);
864  if (pos >= 0)
865  s->pos = pos;
866  else if (pos != AVERROR(ENOSYS))
867  ret = pos;
868  }
869  return ret;
870 }
871 
872 /* output in a dynamic buffer */
873 
874 typedef struct DynBuffer {
879 } DynBuffer;
880 
881 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
882 {
883  DynBuffer *d = opaque;
884  unsigned new_size, new_allocated_size;
885 
886  /* reallocate buffer if needed */
887  new_size = d->pos + buf_size;
888  new_allocated_size = d->allocated_size;
889  if (new_size < d->pos || new_size > INT_MAX/2)
890  return -1;
891  while (new_size > new_allocated_size) {
892  if (!new_allocated_size)
893  new_allocated_size = new_size;
894  else
895  new_allocated_size += new_allocated_size / 2 + 1;
896  }
897 
898  if (new_allocated_size > d->allocated_size) {
899  int err;
900  if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
901  d->allocated_size = 0;
902  d->size = 0;
903  return err;
904  }
905  d->allocated_size = new_allocated_size;
906  }
907  memcpy(d->buffer + d->pos, buf, buf_size);
908  d->pos = new_size;
909  if (d->pos > d->size)
910  d->size = d->pos;
911  return buf_size;
912 }
913 
914 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
915 {
916  unsigned char buf1[4];
917  int ret;
918 
919  /* packetized write: output the header */
920  AV_WB32(buf1, buf_size);
921  ret = dyn_buf_write(opaque, buf1, 4);
922  if (ret < 0)
923  return ret;
924 
925  /* then the data */
926  return dyn_buf_write(opaque, buf, buf_size);
927 }
928 
929 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
930 {
931  DynBuffer *d = opaque;
932 
933  if (whence == SEEK_CUR)
934  offset += d->pos;
935  else if (whence == SEEK_END)
936  offset += d->size;
937  if (offset < 0 || offset > 0x7fffffffLL)
938  return -1;
939  d->pos = offset;
940  return 0;
941 }
942 
943 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
944 {
945  DynBuffer *d;
946  unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
947 
948  if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
949  return -1;
950  d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
951  if (!d)
952  return AVERROR(ENOMEM);
953  d->io_buffer_size = io_buffer_size;
954  *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
955  max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
956  max_packet_size ? NULL : dyn_buf_seek);
957  if(!*s) {
958  av_free(d);
959  return AVERROR(ENOMEM);
960  }
961  (*s)->max_packet_size = max_packet_size;
962  return 0;
963 }
964 
966 {
967  return url_open_dyn_buf_internal(s, 0);
968 }
969 
970 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
971 {
972  if (max_packet_size <= 0)
973  return -1;
974  return url_open_dyn_buf_internal(s, max_packet_size);
975 }
976 
978 {
979  DynBuffer *d;
980  int size;
981  static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
982  int padding = 0;
983 
984  if (!s) {
985  *pbuffer = NULL;
986  return 0;
987  }
988 
989  /* don't attempt to pad fixed-size packet buffers */
990  if (!s->max_packet_size) {
991  avio_write(s, padbuf, sizeof(padbuf));
993  }
994 
995  avio_flush(s);
996 
997  d = s->opaque;
998  *pbuffer = d->buffer;
999  size = d->size;
1000  av_free(d);
1001  av_free(s);
1002  return size - padding;
1003 }
1004 
1005 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1006 {
1007  DynBuffer *d = opaque;
1008 
1009  d->pos += buf_size;
1010  if (d->pos > d->size)
1011  d->size = d->pos;
1012  return buf_size;
1013 }
1014 
1016 {
1017  int ret = url_open_dyn_buf_internal(s, 0);
1018  if (ret >= 0) {
1019  AVIOContext *pb = *s;
1021  }
1022  return ret;
1023 }
1024 
1026 {
1027  DynBuffer *d = s->opaque;
1028  int size;
1029 
1030  avio_flush(s);
1031 
1032  size = d->size;
1033  av_free(d);
1034  av_free(s);
1035  return size;
1036 }
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:335
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition: aviobuf.c:107
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
Definition: aviobuf.c:929
Bytestream IO Context.
Definition: avio.h:68
Buffered I/O operations.
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:671
int64_t(* read_seek)(void *opaque, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp in stream with the specified stream_index.
Definition: avio.h:112
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:965
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:252
int size
uint8_t io_buffer[1]
Definition: aviobuf.c:878
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:353
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:311
AVOption.
Definition: opt.h:234
int avio_feof(AVIOContext *s)
Definition: aviobuf.c:260
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:84
unsigned char * buf_end
End of the data, may be less than buffer+buffer_size if the read function returned less data than req...
Definition: avio.h:85
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:276
int write_flag
true if open for writing
Definition: avio.h:97
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:48
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:522
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:151
#define AVIO_FLAG_READ
read-only
Definition: avio.h:294
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:567
struct URLProtocol * prot
Definition: url.h:43
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:295
unsigned char * buffer
Start of the buffer.
Definition: avio.h:82
void avio_wl24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:359
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
int flags
Definition: url.h:46
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:825
int64_t(* seek)(void *opaque, int64_t offset, int whence)
Definition: avio.h:93
void * opaque
A private pointer, passed to the read/write/seek/...
Definition: avio.h:89
static const AVOption ffio_url_options[]
Definition: aviobuf.c:54
static const AVClass * ffio_url_child_class_next(const AVClass *prev)
Definition: aviobuf.c:49
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:279
#define IO_BUFFER_SIZE
Definition: aviobuf.c:34
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:287
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:271
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
Public dictionary API.
uint8_t
AVOptions.
#define AV_WB32(p, d)
Definition: intreadwrite.h:239
static void fill_buffer(AVIOContext *s)
Definition: aviobuf.c:373
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:297
#define b
Definition: input.c:52
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:434
void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1015
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:70
const char data[16]
Definition: mxf.c:70
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
static int flags
Definition: log.c:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
int ffio_set_buf_size(AVIOContext *s, int buf_size)
Definition: aviobuf.c:721
int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
Read size bytes from AVIOContext, returning a pointer.
Definition: aviobuf.c:510
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:365
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:347
const OptionDef options[]
Definition: avconv_opt.c:2187
static void flush_buffer(AVIOContext *s)
Definition: aviobuf.c:124
int max_packet_size
Definition: avio.h:98
Callback for checking whether to abort blocking functions.
Definition: avio.h:51
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
int io_buffer_size
Definition: aviobuf.c:877
int(* write_packet)(void *opaque, uint8_t *buf, int buf_size)
Definition: avio.h:92
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:599
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:914
int(* read_packet)(void *opaque, uint8_t *buf, int buf_size)
Definition: avio.h:91
#define AVERROR(e)
Definition: error.h:43
unsigned long checksum
Definition: avio.h:99
#define SHORT_SEEK_THRESHOLD
Do seeks within this distance ahead of the current buffer by skipping data instead of calling the pro...
Definition: aviobuf.c:41
#define FFMAX(a, b)
Definition: common.h:55
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:559
int avio_pause(AVIOContext *s, int pause)
Pause and resume playing - only meaningful if using a network streaming protocol (e.g.
Definition: aviobuf.c:845
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:575
int size
Definition: aviobuf.c:875
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
const AVIOInterruptCB int_cb
Definition: avconv.c:141
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
#define FFMIN(a, b)
Definition: common.h:57
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:977
unsigned char * checksum_ptr
Definition: avio.h:100
int avio_printf(AVIOContext *s, const char *fmt,...)
Definition: aviobuf.c:832
int allocated_size
Definition: aviobuf.c:875
int must_flush
true if the next seek should flush
Definition: avio.h:95
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:881
int(* url_read_pause)(URLContext *h, int pause)
Definition: url.h:80
static char buffer[20]
Definition: seek-test.c:31
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:614
#define GET_STR16(type, read)
Definition: aviobuf.c:647
unsigned long(* update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size)
Definition: avio.h:101
int buffer_size
Maximum buffer size.
Definition: avio.h:83
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:341
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:463
uint8_t le
Definition: crc.c:250
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:38
NULL
Definition: eval.c:55
int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
Open a write only packetized memory stream with a maximum packet size of 'max_packet_size'.
Definition: aviobuf.c:970
int avio_open(AVIOContext **s, const char *filename, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:789
int64_t(* url_read_seek)(URLContext *h, int stream_index, int64_t timestamp, int flags)
Definition: url.h:81
av_default_item_name
Definition: dnxhdenc.c:52
static int url_resetbuf(AVIOContext *s, int flags)
Definition: aviobuf.c:736
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:583
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:811
Definition: url.h:41
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
Describe the class of an AVClass context structure.
Definition: log.h:33
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
#define AVSEEK_FORCE
Oring this flag as into the "whence" parameter to a seek function causes it to seek by any means (lik...
Definition: avio.h:198
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:591
int pos
Definition: aviobuf.c:875
#define PUT_UTF16(val, tmp, PUT_16BIT)
Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).
Definition: common.h:339
int error
contains the error code or 0 if no error happened
Definition: avio.h:102
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file...
Definition: aviobuf.c:750
int(* read_pause)(void *opaque, int pause)
Pause or resume playback for network streaming protocols - e.g.
Definition: avio.h:106
int ff_get_v_length(uint64_t val)
Get the length in bytes which is needed to store val as v.
Definition: aviobuf.c:315
int ffurl_close(URLContext *h)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:297
const AVClass ffurl_context_class
Definition: avio.c:76
int avio_open2(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:794
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:241
Main libavformat public API header.
const AVClass ffio_url_class
Definition: aviobuf.c:58
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:629
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h...
Definition: avio.c:287
static void * ffio_url_child_next(void *obj, void *prev)
Definition: aviobuf.c:43
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:211
int ffio_fdopen(AVIOContext **s, URLContext *h)
Create and initialize a AVIOContext for accessing the resource referenced by the URLContext h...
Definition: aviobuf.c:690
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
Definition: aviobuf.c:442
int64_t pos
position in the file of the current buffer
Definition: avio.h:94
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition: avio.h:190
uint64_t ffio_read_varlen(AVIOContext *bc)
Definition: aviobuf.c:679
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:606
uint8_t * buffer
Definition: aviobuf.c:876
int eof_reached
true if eof reached
Definition: avio.h:96
void ff_put_v(AVIOContext *bc, uint64_t val)
Put val using a variable number of bytes.
Definition: aviobuf.c:325
int len
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1025
static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:1005
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
Definition: aviobuf.c:943
int max_packet_size
if non zero, the stream is packetized with this max packet size
Definition: url.h:47
unbuffered private I/O API
int avio_put_str16le(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16LE and write it.
Definition: aviobuf.c:298
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:422
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:454
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
Definition: avio.c:262
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
int64_t avio_seek_time(AVIOContext *s, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp relative to some component stream.
Definition: aviobuf.c:852
unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:428